Fix build for separate objdirs
[openafs.git] / src / afs / SOLARIS / osi_vnodeops.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include "afs/param.h"
12
13
14 /*
15  * SOLARIS/osi_vnodeops.c
16  *
17  * Implements:
18  *
19  * Functions: AFS_TRYUP, _init, _info, _fini, afs_addmap, afs_delmap,
20  * afs_vmread, afs_vmwrite, afs_getpage, afs_GetOnePage, afs_putpage,
21  * afs_putapage, afs_nfsrdwr, afs_map, afs_PageLeft, afs_pathconf/afs_cntl,
22  * afs_ioctl, afs_rwlock, afs_rwunlock, afs_seek, afs_space, afs_dump,
23  * afs_cmp, afs_realvp, afs_pageio, afs_dumpctl, afs_dispose, afs_setsecattr,
24  * afs_getsecattr, gafs_open, gafs_close, gafs_getattr, gafs_setattr,
25  * gafs_access, gafs_lookup, gafs_create, gafs_remove, gafs_link,
26  * gafs_rename, gafs_mkdir, gafs_rmdir, gafs_readdir, gafs_symlink,
27  * gafs_readlink, gafs_fsync, afs_inactive, gafs_inactive, gafs_fid
28  *
29  *
30  * Variables: Afs_vnodeops
31  *
32  */
33 #include "afs/sysincludes.h"    /* Standard vendor system headers */
34 #include "afsincludes.h"        /* Afs-based standard headers */
35 #include "afs/afs_stats.h"      /* statistics */
36 #include "afs/nfsclient.h"
37
38
39 #include <sys/mman.h>
40 #include <vm/hat.h>
41 #include <vm/as.h>
42 #include <vm/page.h>
43 #include <vm/pvn.h>
44 #include <vm/seg.h>
45 #include <vm/seg_map.h>
46 #include <vm/seg_vn.h>
47 #include <vm/rm.h>
48 #if defined(AFS_SUN511_ENV)
49 #include <sys/vfs_opreg.h>
50 #endif
51 #include <sys/modctl.h>
52 #include <sys/syscall.h>
53 #include <sys/debug.h>
54 #include <sys/fs_subr.h>
55
56 /* Translate a faultcode_t as returned by some of the vm routines
57  * into a suitable errno value.
58  */
59 static int
60 afs_fc2errno(faultcode_t fc)
61 {
62     switch (FC_CODE(fc)) {
63     case 0:
64         return 0;
65
66     case FC_OBJERR:
67         return FC_ERRNO(fc);
68
69     default:
70         return EIO;
71     }
72 }
73
74
75 extern struct as kas;           /* kernel addr space */
76 extern unsigned char *afs_indexFlags;
77 extern afs_lock_t afs_xdcache;
78
79 /* Additional vnodeops for SunOS 4.0.x */
80 int afs_nfsrdwr(), afs_getpage(), afs_putpage(), afs_map();
81 int afs_dump(), afs_cmp(), afs_realvp(), afs_GetOnePage();
82
83 int afs_pvn_vptrunc;
84
85 int
86 afs_addmap(struct vnode *avp, offset_t offset, struct as *asp, 
87            caddr_t addr, int length, int prot, int maxprot, int flags, 
88            afs_ucred_t *credp)
89 {
90     /* XXX What should we do here?? XXX */
91     return (0);
92 }
93
94 int
95 afs_delmap(struct vnode *avp, offset_t offset, struct as *asp, 
96            caddr_t addr, int length, int prot, int maxprot, int flags, 
97            afs_ucred_t *credp)
98 {
99     /* XXX What should we do here?? XXX */
100     return (0);
101 }
102
103 #ifdef AFS_SUN510_ENV
104 int
105 afs_vmread(struct vnode *avp, struct uio *auio, int ioflag, 
106            afs_ucred_t *acred, caller_context_t *ct)
107 #else
108 int
109 afs_vmread(struct vnode *avp, struct uio *auio, int ioflag, 
110            afs_ucred_t *acred)
111 #endif
112 {
113     int code;
114
115     if (!RW_READ_HELD(&(VTOAFS(avp))->rwlock))
116         osi_Panic("afs_vmread: !rwlock");
117     AFS_GLOCK();
118     code = afs_nfsrdwr(VTOAFS(avp), auio, UIO_READ, ioflag, acred);
119     AFS_GUNLOCK();
120     return code;
121 }
122
123
124 #ifdef AFS_SUN510_ENV
125 int
126 afs_vmwrite(struct vnode *avp, struct uio *auio, int ioflag, 
127             afs_ucred_t *acred, caller_context_t *ct)
128 #else
129 int
130 afs_vmwrite(struct vnode *avp, struct uio *auio, int ioflag, 
131             afs_ucred_t *acred)
132 #endif
133 {
134     int code;
135
136     if (!RW_WRITE_HELD(&(VTOAFS(avp))->rwlock))
137         osi_Panic("afs_vmwrite: !rwlock");
138     AFS_GLOCK();
139     code = afs_nfsrdwr(VTOAFS(avp), auio, UIO_WRITE, ioflag, acred);
140     AFS_GUNLOCK();
141     return code;
142 }
143
144 int
145 afs_getpage(struct vnode *vp, offset_t off, u_int len, u_int *protp, 
146             struct page *pl[], u_int plsz, struct seg *seg, caddr_t addr, 
147             enum seg_rw rw, afs_ucred_t *acred)
148 {
149     afs_int32 code = 0;
150     AFS_STATCNT(afs_getpage);
151
152     if (vp->v_flag & VNOMAP)    /* File doesn't allow mapping */
153         return (ENOSYS);
154
155     AFS_GLOCK();
156
157     if (len <= PAGESIZE)
158         code =
159             afs_GetOnePage(vp, off, len, protp, pl, plsz, seg, addr, rw, acred);
160     else {
161         struct multiPage_range range;
162         struct vcache *vcp = VTOAFS(vp);
163
164         /* We've been asked to get more than one page. We must return all
165          * requested pages at once, all of them locked, which means all of
166          * these dcache entries cannot be kicked out of the cache before we
167          * return (since their pages cannot be invalidated).
168          *
169          * afs_GetOnePage will be called multiple times by pvn_getpages in
170          * order to get all of the requested pages. One of the later
171          * afs_GetOnePage calls may need to evict some cache entries in order
172          * to perform its read. If we try to kick out one of the entries an
173          * earlier afs_GetOnePage call used, we will deadlock since we have
174          * the page locked. So, to tell afs_GetDownD that it should skip over
175          * any entries we've read in due to this afs_getpage call, record the
176          * offset and length in avc->multiPage.
177          *
178          * Ideally we would just set something in each dcache as we get it,
179          * but that is rather difficult, since pvn_getpages doesn't let us
180          * retain any information between calls to afs_GetOnePage. So instead
181          * just record the offset and length, and let afs_GetDownD calculate
182          * which dcache entries should be skipped. */
183
184         range.off = off;
185         range.len = len;
186
187         ObtainWriteLock(&vcp->vlock, 548);
188         QAdd(&vcp->multiPage, &range.q);
189         ReleaseWriteLock(&vcp->vlock);
190         code =
191             pvn_getpages(afs_GetOnePage, vp, off, len, protp, pl, plsz, seg, addr, rw, acred);
192         ObtainWriteLock(&vcp->vlock, 549);
193         QRemove(&range.q);
194         ReleaseWriteLock(&vcp->vlock);
195     }
196     AFS_GUNLOCK();
197     return code;
198 }
199
200 /* Return all the pages from [off..off+len) in file */
201 int
202 afs_GetOnePage(struct vnode *vp, u_offset_t off, u_int alen, u_int *protp, 
203                struct page *pl[], u_int plsz, struct seg *seg, caddr_t addr, 
204                enum seg_rw rw, afs_ucred_t *acred)
205 {
206     struct page *page;
207     afs_int32 code = 0;
208     u_int len;
209     struct buf *buf;
210     afs_int32 tlen;
211     struct vcache *avc;
212     struct dcache *tdc;
213     int i, s, pexists;
214     int slot;
215     afs_size_t offset, nlen = 0;
216     struct vrequest treq;
217     afs_int32 mapForRead = 0, Code = 0;
218     u_offset_t toffset;
219
220     if (!acred)
221         osi_Panic("GetOnePage: !acred");
222
223     avc = VTOAFS(vp);           /* cast to afs vnode */
224
225     if (avc->credp              /*&& AFS_NFSXLATORREQ(acred) */
226         && AFS_NFSXLATORREQ(avc->credp)) {
227         acred = avc->credp;
228     }
229     if (code = afs_InitReq(&treq, acred))
230         return code;
231
232     if (!pl) {
233         /* This is a read-ahead request, e.g. due to madvise.  */
234         int plen = alen;
235         ObtainReadLock(&avc->lock);
236
237         while (plen > 0 && !afs_BBusy()) {
238             /* Obtain a dcache entry at off.  2 means don't fetch data. */
239             tdc =
240                 afs_GetDCache(avc, (afs_offs_t) off, &treq, &offset, &nlen,
241                               2);
242             if (!tdc)
243                 break;
244
245             /* Write-lock the dcache entry, if we don't succeed, just go on */
246             if (0 != NBObtainWriteLock(&tdc->lock, 642)) {
247                 afs_PutDCache(tdc);
248                 goto next_prefetch;
249             }
250
251             /* If we aren't already fetching this dcache entry, queue it */
252             if (!(tdc->mflags & DFFetchReq)) {
253                 struct brequest *bp;
254
255                 tdc->mflags |= DFFetchReq;
256                 bp = afs_BQueue(BOP_FETCH, avc, B_DONTWAIT, 0, acred,
257                                 (afs_size_t) off, (afs_size_t) 1, tdc,
258                                 (void *)0, (void *)0);
259                 if (!bp) {
260                     /* Unable to start background fetch; might as well stop */
261                     tdc->mflags &= ~DFFetchReq;
262                     ReleaseWriteLock(&tdc->lock);
263                     afs_PutDCache(tdc);
264                     break;
265                 }
266                 ReleaseWriteLock(&tdc->lock);
267             } else {
268                 ReleaseWriteLock(&tdc->lock);
269                 afs_PutDCache(tdc);
270             }
271
272           next_prefetch:
273             /* Adjust our offset and remaining length values */
274             off += nlen;
275             plen -= nlen;
276
277             /* If we aren't making progress for some reason, bail out */
278             if (nlen <= 0)
279                 break;
280         }
281
282         ReleaseReadLock(&avc->lock);
283         return 0;
284     }
285
286     len = PAGESIZE;
287     pl[0] = NULL;               /* Make sure it's empty */
288
289     /* first, obtain the proper lock for the VM system */
290
291     /* if this is a read request, map the page in read-only.  This will
292      * allow us to swap out the dcache entry if there are only read-only
293      * pages created for the chunk, which helps a *lot* when dealing
294      * with small caches.  Otherwise, we have to invalidate the vm
295      * pages for the range covered by a chunk when we swap out the
296      * chunk.
297      */
298     if (rw == S_READ || rw == S_EXEC)
299         mapForRead = 1;
300
301     if (protp)
302         *protp = PROT_ALL;
303
304   retry:
305     if (rw == S_WRITE || rw == S_CREATE)
306         tdc = afs_GetDCache(avc, (afs_offs_t) off, &treq, &offset, &nlen, 5);
307     else
308         tdc = afs_GetDCache(avc, (afs_offs_t) off, &treq, &offset, &nlen, 1);
309     if (!tdc)
310         return afs_CheckCode(EINVAL, &treq, 62);
311     code = afs_VerifyVCache(avc, &treq);
312     if (code) {
313         afs_PutDCache(tdc);
314         return afs_CheckCode(code, &treq, 44);  /* failed to get it */
315     }
316
317     ObtainReadLock(&avc->lock);
318
319     afs_Trace4(afs_iclSetp, CM_TRACE_PAGEIN, ICL_TYPE_POINTER, (afs_int32) vp,
320                ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(off), ICL_TYPE_LONG, len,
321                ICL_TYPE_LONG, (int)rw);
322
323     tlen = len;
324     slot = 0;
325     toffset = off;
326     /* Check to see if we're in the middle of a VM purge, and if we are, release
327      * the locks and try again when the VM purge is done. */
328     ObtainWriteLock(&avc->vlock, 550);
329     if (avc->activeV) {
330         ReleaseReadLock(&avc->lock);
331         ReleaseWriteLock(&avc->vlock);
332         afs_PutDCache(tdc);
333         /* Check activeV again, it may have been turned off
334          * while we were waiting for a lock in afs_PutDCache */
335         ObtainWriteLock(&avc->vlock, 574);
336         if (avc->activeV) {
337             avc->vstates |= VRevokeWait;
338             ReleaseWriteLock(&avc->vlock);
339             afs_osi_Sleep(&avc->vstates);
340         } else {
341             ReleaseWriteLock(&avc->vlock);
342         }
343         goto retry;
344     }
345     ReleaseWriteLock(&avc->vlock);
346
347     /* We're about to do stuff with our dcache entry..  Lock it. */
348     ObtainReadLock(&tdc->lock);
349
350     /* Check to see whether the cache entry is still valid */
351     if (!(avc->f.states & CStatd)
352         || !hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
353         ReleaseReadLock(&tdc->lock);
354         ReleaseReadLock(&avc->lock);
355         afs_PutDCache(tdc);
356         goto retry;
357     }
358
359     AFS_GUNLOCK();
360     while (1) {                 /* loop over all pages */
361         /* now, try to find the page in memory (it may already be intransit or laying
362          * around the free list */
363         page =
364             page_lookup(vp, toffset, (rw == S_CREATE ? SE_EXCL : SE_SHARED));
365         if (page)
366             goto nextpage;
367
368         /* if we make it here, we can't find the page in memory.  Do a real disk read
369          * from the cache to get the data */
370         Code |= 0x200;          /* XXX */
371         /* use PG_EXCL because we know the page does not exist already.  If it 
372          * actually does exist, we have somehow raced between lookup and create.
373          * As of 4/98, that shouldn't be possible, but we'll be defensive here
374          * in case someone tries to relax all the serialization of read and write
375          * operations with harmless things like stat. */
376         page =
377             page_create_va(vp, toffset, PAGESIZE, PG_WAIT | PG_EXCL, seg,
378                            addr);
379         if (!page) {
380             continue;
381         }
382         if (alen < PAGESIZE)
383             pagezero(page, alen, PAGESIZE - alen);
384
385         if (rw == S_CREATE) {
386             /* XXX Don't read from AFS in write only cases XXX */
387             page_io_unlock(page);
388         } else
389         {
390             /* now it is time to start I/O operation */
391             buf = pageio_setup(page, PAGESIZE, vp, B_READ);     /* allocate a buf structure */
392             buf->b_edev = 0;
393             buf->b_dev = 0;
394             buf->b_lblkno = lbtodb(toffset);
395             bp_mapin(buf);      /* map it in to our address space */
396
397             AFS_GLOCK();
398             /* afs_ustrategy will want to lock the dcache entry */
399             ReleaseReadLock(&tdc->lock);
400             code = afs_ustrategy(buf, acred);   /* do the I/O */
401             ObtainReadLock(&tdc->lock);
402             AFS_GUNLOCK();
403
404             /* Before freeing unmap the buffer */
405             bp_mapout(buf);
406             pageio_done(buf);
407             if (code) {
408                 goto bad;
409             }
410             page_io_unlock(page);
411         }
412
413         /* come here when we have another page (already held) to enter */
414       nextpage:
415         /* put page in array and continue */
416         /* The p_selock must be downgraded to a shared lock after the page is read */
417         if ((rw != S_CREATE) && !(PAGE_SHARED(page))) {
418             page_downgrade(page);
419         }
420         pl[slot++] = page;
421         code = page_iolock_assert(page);
422         code = 0;
423         toffset += PAGESIZE;
424         addr += PAGESIZE;
425         tlen -= PAGESIZE;
426         if (tlen <= 0)
427             break;              /* done all the pages */
428     }                           /* while (1) ... */
429
430     AFS_GLOCK();
431     pl[slot] = NULL;
432     ReleaseReadLock(&tdc->lock);
433
434     /* Prefetch next chunk if we're at a chunk boundary */
435     if (AFS_CHUNKOFFSET(off) == 0) {
436         if (!(tdc->mflags & DFNextStarted))
437             afs_PrefetchChunk(avc, tdc, acred, &treq);
438     }
439
440     ReleaseReadLock(&avc->lock);
441     ObtainWriteLock(&afs_xdcache, 246);
442     if (!mapForRead) {
443         /* track that we have dirty (or dirty-able) pages for this chunk. */
444         afs_indexFlags[tdc->index] |= IFDirtyPages;
445     }
446     afs_indexFlags[tdc->index] |= IFAnyPages;
447     ReleaseWriteLock(&afs_xdcache);
448     afs_PutDCache(tdc);
449     afs_Trace3(afs_iclSetp, CM_TRACE_PAGEINDONE, ICL_TYPE_LONG, code,
450                ICL_TYPE_LONG, (int)page, ICL_TYPE_LONG, Code);
451     return 0;
452
453   bad:
454     AFS_GLOCK();
455     afs_Trace3(afs_iclSetp, CM_TRACE_PAGEINDONE, ICL_TYPE_LONG, code,
456                ICL_TYPE_LONG, (int)page, ICL_TYPE_LONG, Code);
457     /* release all pages, drop locks, return code */
458     if (page)
459         pvn_read_done(page, B_ERROR);
460     ReleaseReadLock(&avc->lock);
461     ReleaseReadLock(&tdc->lock);
462     afs_PutDCache(tdc);
463     return code;
464 }
465
466 int
467 afs_putpage(struct vnode *vp, offset_t off, u_int len, int flags, 
468             afs_ucred_t *cred)
469 {
470     struct vcache *avc;
471     struct page *pages;
472     afs_int32 code = 0;
473     size_t tlen;
474     afs_offs_t endPos;
475     afs_int32 NPages = 0;
476     u_offset_t toff = off;
477     int didWriteLock;
478
479     AFS_STATCNT(afs_putpage);
480     if (vp->v_flag & VNOMAP)    /* file doesn't allow mapping */
481         return (ENOSYS);
482
483     /*
484      * Putpage (ASYNC) is called every sec to flush out dirty vm pages 
485      */
486     AFS_GLOCK();
487     afs_Trace4(afs_iclSetp, CM_TRACE_PAGEOUT, ICL_TYPE_POINTER,
488                (afs_int32) vp, ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(off),
489                ICL_TYPE_INT32, (afs_int32) len, ICL_TYPE_LONG, (int)flags);
490     avc = VTOAFS(vp);
491     ObtainSharedLock(&avc->lock, 247);
492     didWriteLock = 0;
493
494     /* Get a list of modified (or whatever) pages */
495     if (len) {
496         endPos = (afs_offs_t) off + len;        /* position we're supposed to write up to */
497         while ((afs_offs_t) toff < endPos
498                && (afs_offs_t) toff < avc->f.m.Length) {
499             /* If not invalidating pages use page_lookup_nowait to avoid reclaiming
500              * them from the free list
501              */
502             AFS_GUNLOCK();
503             if (flags & (B_FREE | B_INVAL))
504                 pages = page_lookup(vp, toff, SE_EXCL);
505             else
506                 pages = page_lookup_nowait(vp, toff, SE_SHARED);
507             if (!pages || !pvn_getdirty(pages, flags))
508                 tlen = PAGESIZE;
509             else {
510                 if (!didWriteLock) {
511                     AFS_GLOCK();
512                     didWriteLock = 1;
513                     UpgradeSToWLock(&avc->lock, 671);
514                     AFS_GUNLOCK();
515                 }
516                 NPages++;
517                 code = afs_putapage(vp, pages, &toff, &tlen, flags, cred);
518                 if (code) {
519                     AFS_GLOCK();
520                     break;
521                 }
522             }
523             toff += tlen;
524             AFS_GLOCK();
525         }
526     } else {
527         if (!didWriteLock) {
528             UpgradeSToWLock(&avc->lock, 670);
529             didWriteLock = 1;
530         }
531
532         AFS_GUNLOCK();
533         code = pvn_vplist_dirty(vp, toff, afs_putapage, flags, cred);
534         AFS_GLOCK();
535     }
536
537     if (code && !avc->vc_error) {
538         if (!didWriteLock) {
539             UpgradeSToWLock(&avc->lock, 669);
540             didWriteLock = 1;
541         }
542         avc->vc_error = code;
543     }
544
545     if (didWriteLock)
546         ReleaseWriteLock(&avc->lock);
547     else
548         ReleaseSharedLock(&avc->lock);
549     afs_Trace2(afs_iclSetp, CM_TRACE_PAGEOUTDONE, ICL_TYPE_LONG, code,
550                ICL_TYPE_LONG, NPages);
551     AFS_GUNLOCK();
552     return (code);
553 }
554
555
556 int
557 afs_putapage(struct vnode *vp, struct page *pages, u_offset_t * offp,
558              size_t * lenp, int flags, afs_ucred_t *credp)
559 {
560     struct buf *tbuf;
561     struct vcache *avc = VTOAFS(vp);
562     afs_int32 code = 0;
563     u_int tlen = PAGESIZE;
564     afs_offs_t off = (pages->p_offset / PAGESIZE) * PAGESIZE;
565
566     /*
567      * Now we've got the modified pages.  All pages are locked and held 
568      * XXX Find a kluster that fits in one block (or page). We also
569      * adjust the i/o if the file space is less than a while page. XXX
570      */
571     if (off + tlen > avc->f.m.Length) {
572         tlen = avc->f.m.Length - off;
573     }
574     /* can't call mapout with 0 length buffers (rmfree panics) */
575     if (((tlen >> 24) & 0xff) == 0xff) {
576         tlen = 0;
577     }
578     if ((int)tlen > 0) {
579         /*
580          * Can't call mapout with 0 length buffers since we'll get rmfree panics
581          */
582         tbuf = pageio_setup(pages, tlen, vp, B_WRITE | flags);
583         if (!tbuf)
584             return (ENOMEM);
585
586         tbuf->b_dev = 0;
587         tbuf->b_lblkno = lbtodb(pages->p_offset);
588         bp_mapin(tbuf);
589         AFS_GLOCK();
590         afs_Trace4(afs_iclSetp, CM_TRACE_PAGEOUTONE, ICL_TYPE_LONG, avc,
591                    ICL_TYPE_LONG, pages, ICL_TYPE_LONG, tlen, ICL_TYPE_OFFSET,
592                    ICL_HANDLE_OFFSET(off));
593         code = afs_ustrategy(tbuf, credp);      /* unlocks page */
594         AFS_GUNLOCK();
595         bp_mapout(tbuf);
596     }
597     pvn_write_done(pages, ((code) ? B_ERROR : 0) | B_WRITE | flags);
598     if ((int)tlen > 0)
599         pageio_done(tbuf);
600     if (offp)
601         *offp = off;
602     if (lenp)
603         *lenp = tlen;
604     return code;
605 }
606
607 int
608 afs_nfsrdwr(struct vcache *avc, struct uio *auio, enum uio_rw arw,
609             int ioflag, afs_ucred_t *acred)
610 {
611     afs_int32 code;
612     afs_int32 code2;
613     int counter;
614     afs_int32 mode, sflags;
615     char *data;
616     struct dcache *dcp, *dcp_newpage;
617     afs_size_t fileBase, size;
618     afs_size_t pageBase;
619     afs_int32 tsize;
620     afs_int32 pageOffset, extraResid = 0;
621     afs_size_t origLength;      /* length when reading/writing started */
622     long appendLength;  /* length when this call will finish */
623     int created;                /* created pages instead of faulting them */
624     int lockCode;
625     int didFakeOpen, eof;
626     struct vrequest treq;
627     caddr_t raddr;
628     u_int rsize;
629
630     AFS_STATCNT(afs_nfsrdwr);
631
632     /* can't read or write other things */
633     if (vType(avc) != VREG)
634         return EISDIR;
635
636     if (auio->uio_resid == 0)
637         return (0);
638
639     afs_Trace4(afs_iclSetp, CM_TRACE_VMRW, ICL_TYPE_POINTER, (afs_int32) avc,
640                ICL_TYPE_LONG, (arw == UIO_WRITE ? 1 : 0), ICL_TYPE_OFFSET,
641                ICL_HANDLE_OFFSET(auio->uio_loffset), ICL_TYPE_OFFSET,
642                ICL_HANDLE_OFFSET(auio->uio_resid));
643
644 #ifndef AFS_64BIT_CLIENT
645     if (AfsLargeFileUio(auio))  /* file is larger than 2 GB */
646         return (EFBIG);
647 #endif
648
649     if (!acred)
650         osi_Panic("rdwr: !acred");
651
652     if (code = afs_InitReq(&treq, acred))
653         return code;
654
655     /* It's not really possible to know if a write cause a growth in the
656      * cache size, we we wait for a cache drain for any write.
657      */
658     afs_MaybeWakeupTruncateDaemon();
659     while ((arw == UIO_WRITE)
660            && (afs_blocksUsed > PERCENT(CM_WAITFORDRAINPCT, afs_cacheBlocks))) {
661         if (afs_blocksUsed - afs_blocksDiscarded >
662             PERCENT(CM_WAITFORDRAINPCT, afs_cacheBlocks)) {
663             afs_WaitForCacheDrain = 1;
664             afs_osi_Sleep(&afs_WaitForCacheDrain);
665         }
666         afs_MaybeFreeDiscardedDCache();
667         afs_MaybeWakeupTruncateDaemon();
668     }
669     code = afs_VerifyVCache(avc, &treq);
670     if (code)
671         return afs_CheckCode(code, &treq, 45);
672
673     osi_FlushPages(avc, acred);
674
675     ObtainWriteLock(&avc->lock, 250);
676
677     /* adjust parameters when appending files */
678     if ((ioflag & IO_APPEND) && arw == UIO_WRITE) {
679         auio->uio_loffset = avc->f.m.Length;    /* write at EOF position */
680     }
681     if (auio->afsio_offset < 0 || (auio->afsio_offset + auio->uio_resid) < 0) {
682         ReleaseWriteLock(&avc->lock);
683         return EINVAL;
684     }
685 #ifndef AFS_64BIT_CLIENT
686     /* file is larger than 2GB */
687     if (AfsLargeFileSize(auio->uio_offset, auio->uio_resid)) {
688         ReleaseWriteLock(&avc->lock);
689         return EFBIG;
690     }
691 #endif
692
693     didFakeOpen = 0;            /* keep track of open so we can do close */
694     if (arw == UIO_WRITE) {
695         /* do ulimit processing; shrink resid or fail */
696         if (auio->uio_loffset + auio->afsio_resid > auio->uio_llimit) {
697             if (auio->uio_loffset >= auio->uio_llimit) {
698                 ReleaseWriteLock(&avc->lock);
699                 return EFBIG;
700             } else {
701                 /* track # of bytes we should write, but won't because of
702                  * ulimit; we must add this into the final resid value
703                  * so caller knows we punted some data.
704                  */
705                 extraResid = auio->uio_resid;
706                 auio->uio_resid = auio->uio_llimit - auio->uio_loffset;
707                 extraResid -= auio->uio_resid;
708             }
709         }
710         mode = S_WRITE;         /* segment map-in mode */
711         afs_FakeOpen(avc);      /* do this for writes, so data gets put back
712                                  * when we want it to be put back */
713         didFakeOpen = 1;        /* we'll be doing a fake open */
714         /* before starting any I/O, we must ensure that the file is big enough
715          * to hold the results (since afs_putpage will be called to force the I/O */
716         size = auio->afsio_resid + auio->afsio_offset;  /* new file size */
717         appendLength = size;
718         origLength = avc->f.m.Length;
719         if (size > avc->f.m.Length) {
720             afs_Trace4(afs_iclSetp, CM_TRACE_SETLENGTH, ICL_TYPE_STRING,
721                        __FILE__, ICL_TYPE_LONG, __LINE__, ICL_TYPE_OFFSET,
722                        ICL_HANDLE_OFFSET(avc->f.m.Length), ICL_TYPE_OFFSET,
723                        ICL_HANDLE_OFFSET(size));
724             avc->f.m.Length = size;     /* file grew */
725         }
726         avc->f.states |= CDirty;        /* Set the dirty bit */
727         avc->f.m.Date = osi_Time();     /* Set file date (for ranlib) */
728     } else {
729         mode = S_READ;          /* map-in read-only */
730         origLength = avc->f.m.Length;
731     }
732
733     if (acred && AFS_NFSXLATORREQ(acred)) {
734         if (arw == UIO_READ) {
735             if (!afs_AccessOK
736                 (avc, PRSFS_READ, &treq,
737                  CHECK_MODE_BITS | CMB_ALLOW_EXEC_AS_READ)) {
738                 ReleaseWriteLock(&avc->lock);
739                 return EACCES;
740             }
741         }
742         crhold(acred);
743         if (avc->credp) {
744             crfree(avc->credp);
745         }
746         avc->credp = acred;
747     }
748     counter = 0;                /* don't call afs_DoPartialWrite first time through. */
749     while (1) {
750         /* compute the amount of data to move into this block,
751          * based on auio->afsio_resid.  Note that we copy data in units of
752          * MAXBSIZE, not PAGESIZE.  This is because segmap_getmap panics if you
753          * call it with an offset based on blocks smaller than MAXBSIZE
754          * (implying that it should be named BSIZE, since it is clearly both a
755          * max and a min). */
756         size = auio->afsio_resid;       /* transfer size */     
757         fileBase = ((arw == UIO_READ) && (origLength < auio->uio_offset)) ? 
758             origLength : auio->afsio_offset;  /* start file position for xfr */
759         pageBase = fileBase & ~(MAXBSIZE - 1);  /* file position of the page */
760         pageOffset = fileBase & (MAXBSIZE - 1); /* xfr start's offset within page */
761         tsize = MAXBSIZE - pageOffset;  /* how much more fits in this page */
762         /* we'll read tsize bytes, but first must make sure tsize isn't too big */
763         if (tsize > size)
764             tsize = size;       /* don't read past end of request */
765         eof = 0;                /* flag telling us if we hit the EOF on the read */
766         if (arw == UIO_READ) {  /* we're doing a read operation */
767             /* don't read past EOF */
768             if (fileBase + tsize > origLength) {
769                 tsize = origLength - fileBase;
770                 eof = 1;        /* we did hit the EOF */
771                 if (tsize < 0)
772                     tsize = 0;  /* better safe than sorry */
773             }
774             sflags = 0;
775         } else {
776             /* Purge dirty chunks of file if there are too many dirty
777              * chunks. Inside the write loop, we only do this at a chunk
778              * boundary. Clean up partial chunk if necessary at end of loop.
779              */
780             if (counter > 0 && code == 0 && AFS_CHUNKOFFSET(fileBase) == 0) {
781                 code = afs_DoPartialWrite(avc, &treq);
782                 if (code)
783                     break;
784             }
785             /* write case, we ask segmap_release to call putpage.  Really, we
786              * don't have to do this on every page mapin, but for now we're
787              * lazy, and don't modify the rest of AFS to scan for modified
788              * pages on a close or other "synchronize with file server"
789              * operation.  This makes things a little cleaner, but probably
790              * hurts performance. */
791             sflags = SM_WRITE;
792         }
793         if (tsize <= 0) {
794             code = 0;
795             break;              /* nothing to transfer, we're done */
796         }
797         if (arw == UIO_WRITE)
798             avc->f.states |= CDirty;    /* may have been cleared by DoPartialWrite */
799
800         /* Before dropping lock, hold the chunk (create it if necessary).  This
801          * serves two purposes:  (1) Ensure Cache Truncate Daemon doesn't try
802          * to purge the chunk's pages while we have them locked.  This would
803          * cause deadlock because we might be waiting for the CTD to free up
804          * a chunk.  (2)  If we're writing past the original EOF, and we're
805          * at the base of the chunk, then make sure it exists online
806          * before we do the uiomove, since the segmap_release will
807          * write out to the chunk, causing it to get fetched if it hasn't
808          * been created yet.  The code that would otherwise notice that
809          * we're fetching a chunk past EOF won't work, since we've
810          * already adjusted the file size above.
811          */
812         ObtainWriteLock(&avc->vlock, 551);
813         while (avc->vstates & VPageCleaning) {
814             ReleaseWriteLock(&avc->vlock);
815             ReleaseWriteLock(&avc->lock);
816             afs_osi_Sleep(&avc->vstates);
817             ObtainWriteLock(&avc->lock, 334);
818             ObtainWriteLock(&avc->vlock, 552);
819         }
820         ReleaseWriteLock(&avc->vlock);
821         {
822             afs_size_t toff, tlen;
823             dcp = afs_GetDCache(avc, fileBase, &treq, &toff, &tlen, 2);
824             if (!dcp) {
825                 code = ENOENT;
826                 break;
827             }
828         }
829         ReleaseWriteLock(&avc->lock);   /* uiomove may page fault */
830         AFS_GUNLOCK();
831         data = segmap_getmap(segkmap, AFSTOV(avc), (u_offset_t) pageBase);
832         raddr = (caddr_t) (((uintptr_t) data + pageOffset) & PAGEMASK);
833         rsize =
834             (((u_int) data + pageOffset + tsize + PAGEOFFSET) & PAGEMASK) -
835             (u_int) raddr;
836         if (code == 0) {
837             /* if we're doing a write, and we're starting at the rounded
838              * down page base, and we're writing enough data to cover all
839              * created pages, then we must be writing all of the pages
840              * in this MAXBSIZE window that we're creating.
841              */
842             created = 0;
843             if (arw == UIO_WRITE && ((long)raddr == (long)data + pageOffset)
844                 && tsize >= rsize) {
845                 /* probably the dcache backing this guy is around, but if
846                  * not, we can't do this optimization, since we're creating
847                  * writable pages, which must be backed by a chunk.
848                  */
849                 AFS_GLOCK();
850                 dcp_newpage = afs_FindDCache(avc, pageBase);
851                 if (dcp_newpage
852                     && hsame(avc->f.m.DataVersion, dcp_newpage->f.versionNo)) {
853                     ObtainWriteLock(&avc->lock, 251);
854                     ObtainWriteLock(&avc->vlock, 576);
855                     ObtainReadLock(&dcp_newpage->lock);
856                     if ((avc->activeV == 0)
857                         && hsame(avc->f.m.DataVersion, dcp_newpage->f.versionNo)
858                         && !(dcp_newpage->dflags & (DFFetching))) {
859                         AFS_GUNLOCK();
860                         segmap_pagecreate(segkmap, raddr, rsize, 1);
861                         AFS_GLOCK();
862                         ObtainWriteLock(&afs_xdcache, 252);
863                         /* Mark the pages as created and dirty */
864                         afs_indexFlags[dcp_newpage->index]
865                             |= (IFAnyPages | IFDirtyPages);
866                         ReleaseWriteLock(&afs_xdcache);
867                         created = 1;
868                     }
869                     ReleaseReadLock(&dcp_newpage->lock);
870                     afs_PutDCache(dcp_newpage);
871                     ReleaseWriteLock(&avc->vlock);
872                     ReleaseWriteLock(&avc->lock);
873                 } else if (dcp_newpage)
874                     afs_PutDCache(dcp_newpage);
875                 AFS_GUNLOCK();
876             }
877             if (!created)
878                 code =
879                     afs_fc2errno(segmap_fault
880                                  (kas.a_hat, segkmap, raddr, rsize,
881                                   F_SOFTLOCK, mode));
882         }
883         if (code == 0) {
884             AFS_UIOMOVE(data + pageOffset, tsize, arw, auio, code);
885             segmap_fault(kas.a_hat, segkmap, raddr, rsize, F_SOFTUNLOCK,
886                          mode);
887         }
888         if (code == 0) {
889             code = segmap_release(segkmap, data, sflags);
890         } else {
891             (void)segmap_release(segkmap, data, 0);
892         }
893         AFS_GLOCK();
894         ObtainWriteLock(&avc->lock, 253);
895         counter++;
896         if (dcp)
897             afs_PutDCache(dcp);
898         if (code)
899             break;
900     }
901     if (didFakeOpen) {
902         afs_FakeClose(avc, acred);
903     }
904     if (arw == UIO_WRITE && (avc->f.states & CDirty)) {
905         code2 = afs_DoPartialWrite(avc, &treq);
906         if (!code)
907             code = code2;
908     }
909
910     if (!code && avc->vc_error) {
911         code = avc->vc_error;
912     }
913     ReleaseWriteLock(&avc->lock);
914     if (!code) {
915         if ((ioflag & FSYNC) && (arw == UIO_WRITE)
916             && !AFS_NFSXLATORREQ(acred))
917             code = afs_fsync(avc, 0, acred);
918     }
919     /* 
920      * If things worked, add in as remaining in request any bytes
921      * we didn't write due to file size ulimit.
922      */
923     if (code == 0 && extraResid > 0)
924         auio->uio_resid += extraResid;
925     return afs_CheckCode(code, &treq, 46);
926 }
927
928 int
929 afs_map(struct vnode *vp, offset_t off, struct as *as, caddr_t *addr, u_int len, u_char prot, u_char maxprot, u_int flags, afs_ucred_t *cred)
930 {
931     struct segvn_crargs crargs;
932     afs_int32 code;
933     struct vrequest treq;
934     struct vcache *avc = VTOAFS(vp);
935
936     AFS_STATCNT(afs_map);
937
938
939     /* check for reasonableness on segment bounds; apparently len can be < 0 */
940     if (off < 0 || off + len < 0) {
941         return (EINVAL);
942     }
943 #ifndef AFS_64BIT_CLIENT
944     if (AfsLargeFileSize(off, len)) {   /* file is larger than 2 GB */
945         code = EFBIG;
946         goto out;
947     }
948 #endif
949
950     if (vp->v_flag & VNOMAP)    /* File isn't allowed to be mapped */
951         return (ENOSYS);
952
953     if (vp->v_filocks)          /* if locked, disallow mapping */
954         return (EAGAIN);
955
956     AFS_GLOCK();
957     if (code = afs_InitReq(&treq, cred))
958         goto out;
959
960     if (vp->v_type != VREG) {
961         code = ENODEV;
962         goto out;
963     }
964
965     code = afs_VerifyVCache(avc, &treq);
966     if (code) {
967         goto out;
968     }
969     osi_FlushPages(avc, cred);  /* ensure old pages are gone */
970     avc->f.states |= CMAPPED;   /* flag cleared at afs_inactive */
971
972     AFS_GUNLOCK();
973     as_rangelock(as);
974     if ((flags & MAP_FIXED) == 0) {
975         map_addr(addr, len, off, 1, flags);
976         if (*addr == NULL) {
977             as_rangeunlock(as);
978             code = ENOMEM;
979             goto out1;
980         }
981     } else
982         (void)as_unmap(as, *addr, len); /* unmap old address space use */
983     /* setup the create parameter block for the call */
984     crargs.vp = AFSTOV(avc);
985     crargs.offset = (u_int) off;
986     crargs.cred = cred;
987     crargs.type = flags & MAP_TYPE;
988     crargs.prot = prot;
989     crargs.maxprot = maxprot;
990     crargs.amp = (struct anon_map *)0;
991     crargs.flags = flags & ~MAP_TYPE;
992
993     code = as_map(as, *addr, len, segvn_create, (char *)&crargs);
994     as_rangeunlock(as);
995   out1:
996     AFS_GLOCK();
997     code = afs_CheckCode(code, &treq, 47);
998     AFS_GUNLOCK();
999     return code;
1000   out:
1001     code = afs_CheckCode(code, &treq, 48);
1002     AFS_GUNLOCK();
1003     return code;
1004 }
1005
1006
1007 /*
1008  * For Now We use standard local kernel params for AFS system values. Change this
1009  * at some point.
1010  */
1011 int
1012 #ifdef AFS_SUN511_ENV
1013 afs_pathconf(struct vnode *vp, int cmd, u_long *outdatap,
1014              afs_ucred_t *credp, caller_context_t *ct)
1015 #else
1016 afs_pathconf(struct vnode *vp, int cmd, u_long *outdatap,
1017              afs_ucred_t *credp)
1018 #endif /* AFS_SUN511_ENV */
1019 {
1020     AFS_STATCNT(afs_cntl);
1021     switch (cmd) {
1022     case _PC_LINK_MAX:
1023         *outdatap = MAXLINK;
1024         break;
1025     case _PC_NAME_MAX:
1026         *outdatap = MAXNAMLEN;
1027         break;
1028     case _PC_PATH_MAX:
1029         *outdatap = MAXPATHLEN;
1030         break;
1031     case _PC_CHOWN_RESTRICTED:
1032         *outdatap = 1;
1033         break;
1034     case _PC_NO_TRUNC:
1035         *outdatap = 1;
1036         break;
1037     case _PC_FILESIZEBITS:
1038 #ifdef AFS_64BIT_CLIENT
1039         *outdatap = 64;
1040 #else
1041         *outdatap = 32;
1042 #endif
1043         break;
1044     default:
1045 #ifdef AFS_SUN511_ENV
1046         return fs_pathconf(vp, cmd, outdatap, credp, ct);
1047 #else
1048         return fs_pathconf(vp, cmd, outdatap, credp);
1049 #endif /* AFS_SUN511_ENV */
1050     }
1051     return 0;
1052 }
1053
1054 int
1055 afs_ioctl(struct vnode *vnp, int com, int arg, int flag, cred_t *credp, 
1056           int *rvalp)
1057 {
1058     return (ENOTTY);
1059 }
1060
1061 void
1062 afs_rwlock(struct vnode *vnp, int wlock)
1063 {
1064     rw_enter(&(VTOAFS(vnp))->rwlock, (wlock ? RW_WRITER : RW_READER));
1065 }
1066
1067
1068 void
1069 afs_rwunlock(struct vnode *vnp, int wlock)
1070 {
1071     rw_exit(&(VTOAFS(vnp))->rwlock);
1072 }
1073
1074
1075 /* NOT SUPPORTED */
1076 int
1077 afs_seek(struct vnode *vnp, offset_t ooff, offset_t *noffp)
1078 {
1079     int code = 0;
1080
1081 #ifndef AFS_64BIT_CLIENT
1082 # define __MAXOFF_T MAXOFF_T
1083 #else
1084 # define __MAXOFF_T MAXOFFSET_T
1085 #endif
1086
1087     if ((*noffp < 0 || *noffp > __MAXOFF_T))
1088         code = EINVAL;
1089     return code;
1090 }
1091
1092 int
1093 #ifdef AFS_SUN59_ENV
1094 afs_frlock(struct vnode *vnp, int cmd, struct flock64 *ap, int flag, 
1095            offset_t off, struct flk_callback *flkcb, afs_ucred_t *credp)
1096 #else
1097 afs_frlock(struct vnode *vnp, int cmd, struct flock64 *ap, int flag,
1098            offset_t off, afs_ucred_t *credp)
1099 #endif
1100 {
1101     afs_int32 code = 0;
1102     /*
1103      * Implement based on afs_lockctl
1104      */
1105     AFS_GLOCK();
1106 #ifdef AFS_SUN59_ENV
1107     if (flkcb)
1108         afs_warn("Don't know how to deal with flk_callback's!\n");
1109 #endif
1110     if ((cmd == F_GETLK) || (cmd == F_O_GETLK) || (cmd == F_SETLK)
1111         || (cmd == F_SETLKW)) {
1112         ap->l_pid = ttoproc(curthread)->p_pid;
1113         ap->l_sysid = 0;
1114
1115         AFS_GUNLOCK();
1116         code = convoff(vnp, ap, 0, off);
1117         if (code)
1118             return code;
1119         AFS_GLOCK();
1120     }
1121
1122     code = afs_lockctl(VTOAFS(vnp), ap, cmd, credp);
1123     AFS_GUNLOCK();
1124     return code;
1125 }
1126
1127
1128 int
1129 afs_space(struct vnode *vnp, int cmd, struct flock64 *ap, int flag, 
1130           offset_t off, afs_ucred_t *credp)
1131 {
1132     afs_int32 code = EINVAL;
1133     struct vattr vattr;
1134
1135     if ((cmd == F_FREESP)
1136         && ((code = convoff(vnp, ap, 0, off)) == 0)) {
1137         AFS_GLOCK();
1138         if (!ap->l_len) {
1139             vattr.va_mask = AT_SIZE;
1140             vattr.va_size = ap->l_start;
1141             code = afs_setattr(VTOAFS(vnp), &vattr, 0, credp);
1142         }
1143         AFS_GUNLOCK();
1144     }
1145     return (code);
1146 }
1147
1148 int
1149 afs_dump(struct vnode *vp, caddr_t addr, int i1, int i2)
1150 {
1151     AFS_STATCNT(afs_dump);
1152     afs_warn("AFS_DUMP. MUST IMPLEMENT THIS!!!\n");
1153     return EINVAL;
1154 }
1155
1156
1157 /* Nothing fancy here; just compare if vnodes are identical ones */
1158 int
1159 afs_cmp(struct vnode *vp1, struct vnode *vp2)
1160 {
1161     AFS_STATCNT(afs_cmp);
1162     return (vp1 == vp2);
1163 }
1164
1165
1166 int
1167 afs_realvp(struct vnode *vp, struct vnode **vpp)
1168 {
1169     AFS_STATCNT(afs_realvp);
1170     return EINVAL;
1171 }
1172
1173
1174 int
1175 afs_pageio(struct vnode *vp, struct page *pp, u_int ui1, u_int ui2, int i1, 
1176            struct cred *credp)
1177 {
1178     afs_warn("afs_pageio: Not implemented\n");
1179     return EINVAL;
1180 }
1181
1182 int
1183 #ifdef AFS_SUN59_ENV
1184 afs_dumpctl(struct vnode *vp, int i, int *blkp)
1185 #else
1186 afs_dumpctl(struct vnode *vp, int i)
1187 #endif
1188 {
1189     afs_warn("afs_dumpctl: Not implemented\n");
1190     return EINVAL;
1191 }
1192
1193 #ifdef  AFS_SUN511_ENV
1194 extern void
1195 afs_dispose(struct vnode *vp, struct page *p, int fl, int dn, struct cred *cr, struct caller_context_t *ct)
1196 {
1197     fs_dispose(vp, p, fl, dn, cr,ct);
1198 }
1199
1200 int
1201 afs_setsecattr(struct vnode *vp, vsecattr_t *vsecattr, int flag, struct cred *creds, struct caller_context_t *ct)
1202 {
1203     return ENOSYS;
1204 }
1205
1206 int
1207 afs_getsecattr(struct vnode *vp, vsecattr_t *vsecattr, int flag, struct cred *creds, struct caller_context_t *ct)
1208 {
1209   return fs_fab_acl(vp, vsecattr, flag, creds,ct);
1210 }
1211 #else
1212 extern void
1213 afs_dispose(struct vnode *vp, struct page *p, int fl, int dn, struct cred *cr)
1214 {
1215     fs_dispose(vp, p, fl, dn, cr);
1216 }
1217
1218 int
1219 afs_setsecattr(struct vnode *vp, vsecattr_t *vsecattr, int flag, 
1220                struct cred *creds)
1221 {
1222     return ENOSYS;
1223 }
1224
1225 int
1226 afs_getsecattr(struct vnode *vp, vsecattr_t *vsecattr, int flag, struct cred *creds)
1227 {
1228     return fs_fab_acl(vp, vsecattr, flag, creds);
1229 }
1230 #endif
1231
1232 #ifdef  AFS_GLOBAL_SUNLOCK
1233 extern int gafs_open(struct vcache **avcp, afs_int32 aflags, 
1234                      afs_ucred_t *acred);
1235 extern int gafs_close(struct vcache *avc, afs_int32 aflags, 
1236                       int count, offset_t offset, afs_ucred_t *acred);
1237 extern int afs_ioctl(struct vnode *vnp, int com, int arg, int flag, 
1238                      cred_t *credp, int *rvalp);
1239 extern int gafs_access(struct vcache *avc, afs_int32 amode,
1240                        int flags, afs_ucred_t *acred);
1241 extern int gafs_getattr(struct vcache *avc, 
1242                         struct vattr *attrs, int flags, 
1243                         afs_ucred_t *acred);
1244 extern int gafs_setattr(struct vcache *avc, 
1245                         struct vattr *attrs, int flags, 
1246                         afs_ucred_t *acred);
1247 extern int gafs_lookup(struct vcache *adp, char *aname, 
1248                        struct vcache **avcp, struct pathname *pnp,
1249                        int flags, struct vnode *rdir, afs_ucred_t *acred);
1250 extern int gafs_remove(struct vcache *adp, char *aname, 
1251                        afs_ucred_t *acred);
1252 extern int gafs_link(struct vcache *adp, struct vcache *avc,
1253                      char *aname, afs_ucred_t *acred);
1254 extern int gafs_rename(struct vcache *aodp, char *aname1,
1255                        struct vcache *andp, char *aname2,
1256                        afs_ucred_t *acred);
1257 extern int gafs_symlink(struct vcache *adp, char *aname, 
1258                         struct vattr *attrs, char *atargetName, 
1259                         afs_ucred_t *acred);
1260 extern int gafs_rmdir(struct vcache *adp, char *aname, 
1261                       struct vnode *cdirp, afs_ucred_t *acred);
1262 extern int gafs_mkdir(struct vcache *adp, char *aname, 
1263                       struct vattr *attrs, struct vcache **avcp, 
1264                       afs_ucred_t *acred);
1265 extern int gafs_fsync(struct vcache *avc, int flag, afs_ucred_t *acred);
1266 extern int gafs_readlink(struct vcache *avc, struct uio *auio, 
1267                          afs_ucred_t *acred);
1268 extern int gafs_readdir(struct vcache *avc, struct uio *auio,
1269                         afs_ucred_t *acred, int *eofp);
1270 extern void gafs_inactive(struct vcache *avc, 
1271                           afs_ucred_t *acred);
1272 extern int gafs_fid(struct vcache *avc, struct fid **fidpp);
1273 extern int gafs_create(struct vcache *adp, char *aname, 
1274                        struct vattr *attrs, enum vcexcl aexcl, int amode, 
1275                        struct vcache **avcp, afs_ucred_t *acred);
1276 #ifdef AFS_SUN511_ENV
1277 extern int afs_pathconf(struct vnode *vp, int cmd, u_long *outdatap,
1278                         afs_ucred_t *credp, caller_context_t *ct);
1279 #else
1280 extern int afs_pathconf(struct vnode *vp, int cmd, u_long *outdatap,
1281                         afs_ucred_t *credp);
1282 #endif /* AFS_SUN511_ENV */
1283
1284 #if defined(AFS_SUN511_ENV)
1285 /* The following list must always be NULL-terminated */
1286 const fs_operation_def_t afs_vnodeops_template[] = {
1287     VOPNAME_OPEN,               { .vop_open = gafs_open },
1288     VOPNAME_CLOSE,              { .vop_close = gafs_close },
1289     VOPNAME_READ,               { .vop_read = afs_vmread },
1290     VOPNAME_WRITE,              { .vop_write = afs_vmwrite },
1291     VOPNAME_IOCTL,              { .vop_ioctl = afs_ioctl },
1292     VOPNAME_SETFL,              { .vop_setfl = fs_setfl },
1293     VOPNAME_GETATTR,            { .vop_getattr = gafs_getattr },
1294     VOPNAME_SETATTR,            { .vop_setattr = gafs_setattr },
1295     VOPNAME_ACCESS,             { .vop_access = gafs_access },
1296     VOPNAME_LOOKUP,             { .vop_lookup = gafs_lookup },
1297     VOPNAME_CREATE,             { .vop_create = gafs_create },
1298     VOPNAME_REMOVE,             { .vop_remove = gafs_remove },
1299     VOPNAME_LINK,               { .vop_link = gafs_link },
1300     VOPNAME_RENAME,             { .vop_rename = gafs_rename },
1301     VOPNAME_MKDIR,              { .vop_mkdir = gafs_mkdir },
1302     VOPNAME_RMDIR,              { .vop_rmdir = gafs_rmdir },
1303     VOPNAME_READDIR,            { .vop_readdir = gafs_readdir },
1304     VOPNAME_SYMLINK,            { .vop_symlink = gafs_symlink },   
1305     VOPNAME_READLINK,           { .vop_readlink = gafs_readlink },
1306     VOPNAME_FSYNC,              { .vop_fsync = gafs_fsync },
1307     VOPNAME_INACTIVE,           { .vop_inactive = gafs_inactive },
1308     VOPNAME_FID,                { .vop_fid = gafs_fid },
1309     VOPNAME_RWLOCK,             { .vop_rwlock = afs_rwlock },
1310     VOPNAME_RWUNLOCK,           { .vop_rwunlock = afs_rwunlock },
1311     VOPNAME_SEEK,               { .vop_seek = afs_seek },
1312     VOPNAME_CMP,                { .vop_cmp = afs_cmp },
1313     VOPNAME_FRLOCK,             { .vop_frlock = afs_frlock },
1314     VOPNAME_SPACE,              { .vop_space = afs_space },
1315     VOPNAME_REALVP,             { .vop_realvp = afs_realvp },
1316     VOPNAME_GETPAGE,            { .vop_getpage = afs_getpage },
1317     VOPNAME_PUTPAGE,            { .vop_putpage = afs_putpage },
1318     VOPNAME_MAP,                { .vop_map = afs_map },
1319     VOPNAME_ADDMAP,             { .vop_addmap = afs_addmap },
1320     VOPNAME_DELMAP,             { .vop_delmap = afs_delmap },
1321     VOPNAME_POLL,               { .vop_poll = fs_poll },
1322     VOPNAME_PATHCONF,           { .vop_pathconf = afs_pathconf },
1323     VOPNAME_PAGEIO,             { .vop_pageio = afs_pageio },
1324     VOPNAME_DUMP,               { .vop_dump = afs_dump },
1325     VOPNAME_DUMPCTL,            { .vop_dumpctl = afs_dumpctl },   
1326     VOPNAME_DISPOSE,            { .vop_dispose = afs_dispose },
1327     VOPNAME_GETSECATTR,         { .vop_getsecattr = afs_getsecattr },
1328     VOPNAME_SETSECATTR,         { .vop_setsecattr = afs_setsecattr },
1329     VOPNAME_SHRLOCK,            { .vop_shrlock = fs_shrlock },
1330     NULL,                       NULL
1331 };
1332 vnodeops_t *afs_ops;
1333 #elif defined(AFS_SUN510_ENV)
1334 /* The following list must always be NULL-terminated */
1335 const fs_operation_def_t afs_vnodeops_template[] = {
1336     VOPNAME_OPEN,               gafs_open,
1337     VOPNAME_CLOSE,              gafs_close,
1338     VOPNAME_READ,               afs_vmread,
1339     VOPNAME_WRITE,              afs_vmwrite,
1340     VOPNAME_IOCTL,              afs_ioctl,
1341     VOPNAME_SETFL,              fs_setfl,
1342     VOPNAME_GETATTR,            gafs_getattr,
1343     VOPNAME_SETATTR,            gafs_setattr,
1344     VOPNAME_ACCESS,             gafs_access,
1345     VOPNAME_LOOKUP,             gafs_lookup,
1346     VOPNAME_CREATE,             gafs_create,
1347     VOPNAME_REMOVE,             gafs_remove,
1348     VOPNAME_LINK,               gafs_link,
1349     VOPNAME_RENAME,             gafs_rename,
1350     VOPNAME_MKDIR,              gafs_mkdir,
1351     VOPNAME_RMDIR,              gafs_rmdir,
1352     VOPNAME_READDIR,            gafs_readdir,
1353     VOPNAME_SYMLINK,            gafs_symlink,   
1354     VOPNAME_READLINK,           gafs_readlink,
1355     VOPNAME_FSYNC,              gafs_fsync,
1356     VOPNAME_INACTIVE,           gafs_inactive,
1357     VOPNAME_FID,                gafs_fid,
1358     VOPNAME_RWLOCK,             afs_rwlock,
1359     VOPNAME_RWUNLOCK,           afs_rwunlock,
1360     VOPNAME_SEEK,               afs_seek,
1361     VOPNAME_CMP,                afs_cmp,
1362     VOPNAME_FRLOCK,             afs_frlock,
1363     VOPNAME_SPACE,              afs_space,
1364     VOPNAME_REALVP,             afs_realvp,
1365     VOPNAME_GETPAGE,            afs_getpage,
1366     VOPNAME_PUTPAGE,            afs_putpage,
1367     VOPNAME_MAP,                afs_map,
1368     VOPNAME_ADDMAP,             afs_addmap,
1369     VOPNAME_DELMAP,             afs_delmap,
1370     VOPNAME_POLL,               fs_poll,
1371     VOPNAME_DUMP,               afs_dump,
1372     VOPNAME_PATHCONF,           afs_pathconf,
1373     VOPNAME_PAGEIO,             afs_pageio,
1374     VOPNAME_DUMPCTL,            afs_dumpctl,   
1375     VOPNAME_DISPOSE,            afs_dispose,
1376     VOPNAME_GETSECATTR,       afs_getsecattr,
1377     VOPNAME_SETSECATTR,         afs_setsecattr,
1378     VOPNAME_SHRLOCK,            fs_shrlock,
1379     NULL,                     NULL
1380 };
1381 struct vnodeops *afs_ops;
1382 #else
1383 struct vnodeops Afs_vnodeops = {
1384     gafs_open,
1385     gafs_close,
1386     afs_vmread,
1387     afs_vmwrite,
1388     afs_ioctl,
1389     fs_setfl,
1390     gafs_getattr,
1391     gafs_setattr,
1392     gafs_access,
1393     gafs_lookup,
1394     gafs_create,
1395     gafs_remove,
1396     gafs_link,
1397     gafs_rename,
1398     gafs_mkdir,
1399     gafs_rmdir,
1400     gafs_readdir,
1401     gafs_symlink,
1402     gafs_readlink,
1403     gafs_fsync,
1404     gafs_inactive,
1405     gafs_fid,
1406     afs_rwlock,
1407     afs_rwunlock,
1408     afs_seek,
1409     afs_cmp,
1410     afs_frlock,
1411     afs_space,
1412     afs_realvp,
1413     afs_getpage,
1414     afs_putpage,
1415     afs_map,
1416     afs_addmap,
1417     afs_delmap,
1418     fs_poll,
1419     afs_dump,
1420     afs_pathconf,
1421     afs_pageio,
1422     afs_dumpctl,
1423     afs_dispose,
1424     afs_setsecattr,
1425     afs_getsecattr,
1426     fs_shrlock,
1427 };
1428 struct vnodeops *afs_ops = &Afs_vnodeops;
1429 #endif
1430
1431 int
1432 gafs_open(struct vcache **avcp, afs_int32 aflags, 
1433           afs_ucred_t *acred)
1434 {
1435     int code;
1436
1437     AFS_GLOCK();
1438     code = afs_open(avcp, aflags, acred);
1439     AFS_GUNLOCK();
1440     return (code);
1441 }
1442
1443 int
1444 gafs_close(struct vcache *avc, afs_int32 aflags, int count, 
1445            offset_t offset, afs_ucred_t *acred)
1446 {
1447     int code;
1448
1449     AFS_GLOCK();
1450     code = afs_close(avc, aflags, count, offset, acred);
1451     AFS_GUNLOCK();
1452     return (code);
1453 }
1454
1455 int
1456 gafs_getattr(struct vcache *avc, struct vattr *attrs, 
1457              int flags, afs_ucred_t *acred)
1458 {
1459     int code;
1460
1461     AFS_GLOCK();
1462     code = afs_getattr(avc, attrs, flags, acred);
1463     AFS_GUNLOCK();
1464     return (code);
1465 }
1466
1467
1468 int
1469 gafs_setattr(struct vcache *avc, struct vattr *attrs, 
1470              int flags, afs_ucred_t *acred)
1471 {
1472     int code;
1473
1474     AFS_GLOCK();
1475     code = afs_setattr(avc, attrs, flags, acred);
1476     AFS_GUNLOCK();
1477     return (code);
1478 }
1479
1480
1481 int
1482 gafs_access(struct vcache *avc, afs_int32 amode, int flags, 
1483             afs_ucred_t *acred)
1484 {
1485     int code;
1486
1487     AFS_GLOCK();
1488     code = afs_access(avc, amode, flags, acred);
1489     AFS_GUNLOCK();
1490     return (code);
1491 }
1492
1493
1494 int
1495 gafs_lookup(struct vcache *adp, char *aname, 
1496             struct vcache **avcp, struct pathname *pnp, int flags, 
1497             struct vnode *rdir, afs_ucred_t *acred)
1498 {
1499     int code;
1500
1501     AFS_GLOCK();
1502     code = afs_lookup(adp, aname, avcp, pnp, flags, rdir, acred);
1503     AFS_GUNLOCK();
1504     return (code);
1505 }
1506
1507
1508 int
1509 gafs_create(struct vcache *adp, char *aname, struct vattr *attrs, 
1510             enum vcexcl aexcl, int amode, struct vcache **avcp, 
1511             afs_ucred_t *acred)
1512 {
1513     int code;
1514
1515     AFS_GLOCK();
1516     code = afs_create(adp, aname, attrs, aexcl, amode, avcp, acred);
1517     AFS_GUNLOCK();
1518     return (code);
1519 }
1520
1521 int
1522 gafs_remove(struct vcache *adp, char *aname, afs_ucred_t *acred)
1523 {
1524     int code;
1525
1526     AFS_GLOCK();
1527     code = afs_remove(adp, aname, acred);
1528     AFS_GUNLOCK();
1529     return (code);
1530 }
1531
1532 int
1533 gafs_link(struct vcache *adp, struct vcache *avc, 
1534           char *aname, afs_ucred_t *acred)
1535 {
1536     int code;
1537
1538     AFS_GLOCK();
1539     code = afs_link(adp, avc, aname, acred);
1540     AFS_GUNLOCK();
1541     return (code);
1542 }
1543
1544 int
1545 gafs_rename(struct vcache *aodp, char *aname1, 
1546             struct vcache *andp, char *aname2, 
1547             afs_ucred_t *acred)
1548 {
1549     int code;
1550
1551     AFS_GLOCK();
1552     code = afs_rename(aodp, aname1, andp, aname2, acred);
1553 #ifdef AFS_SUN510_ENV
1554     if (code == 0) {
1555         struct vcache *avcp = NULL;
1556         
1557         (void) afs_lookup(andp, aname2, &avcp, NULL, 0, NULL, acred);
1558         if (avcp) {
1559             struct vnode *vp = AFSTOV(avcp), *pvp = AFSTOV(andp);
1560             
1561             mutex_enter(&vp->v_lock);
1562             if (vp->v_path != NULL) {
1563                 kmem_free(vp->v_path, strlen(vp->v_path) + 1);
1564                 vp->v_path = NULL;
1565             }
1566             mutex_exit(&vp->v_lock);
1567             vn_setpath(afs_globalVp, pvp, vp, aname2, strlen(aname2));
1568
1569             AFS_RELE(avcp);
1570         }
1571     }
1572 #endif
1573     AFS_GUNLOCK();
1574     return (code);
1575 }
1576
1577 int
1578 gafs_mkdir(struct vcache *adp, char *aname, struct vattr *attrs, 
1579            struct vcache **avcp, afs_ucred_t *acred)
1580 {
1581     int code;
1582
1583     AFS_GLOCK();
1584     code = afs_mkdir(adp, aname, attrs, avcp, acred);
1585     AFS_GUNLOCK();
1586     return (code);
1587 }
1588
1589 int
1590 gafs_rmdir(struct vcache *adp, char *aname, struct vnode *cdirp, 
1591            afs_ucred_t *acred)
1592 {
1593     int code;
1594
1595     AFS_GLOCK();
1596     code = afs_rmdir(adp, aname, cdirp, acred);
1597     AFS_GUNLOCK();
1598     return (code);
1599 }
1600
1601
1602 int
1603 gafs_readdir(struct vcache *avc, struct uio *auio,
1604              afs_ucred_t *acred, int *eofp)
1605 {
1606     int code;
1607
1608     AFS_GLOCK();
1609     code = afs_readdir(avc, auio, acred, eofp);
1610     AFS_GUNLOCK();
1611     return (code);
1612 }
1613
1614 int
1615 gafs_symlink(struct vcache *adp, char *aname, struct vattr *attrs,
1616              char *atargetName, afs_ucred_t *acred)
1617 {
1618     int code;
1619
1620     AFS_GLOCK();
1621     code = afs_symlink(adp, aname, attrs, atargetName, acred);
1622     AFS_GUNLOCK();
1623     return (code);
1624 }
1625
1626
1627 int
1628 gafs_readlink(struct vcache *avc, struct uio *auio, afs_ucred_t *acred)
1629 {
1630     int code;
1631
1632     AFS_GLOCK();
1633     code = afs_readlink(avc, auio, acred);
1634     AFS_GUNLOCK();
1635     return (code);
1636 }
1637
1638 int
1639 gafs_fsync(struct vcache *avc, int flag, afs_ucred_t *acred)
1640 {
1641     int code;
1642
1643     AFS_GLOCK();
1644     code = afs_fsync(avc, flag, acred);
1645     AFS_GUNLOCK();
1646     return (code);
1647 }
1648
1649 int
1650 afs_inactive(struct vcache *avc, afs_ucred_t *acred)
1651 {
1652     struct vnode *vp = AFSTOV(avc);
1653     if (afs_shuttingdown)
1654         return 0;
1655
1656     /*
1657      * In Solaris and HPUX s800 and HP-UX10.0 they actually call us with
1658      * v_count 1 on last reference!
1659      */
1660     mutex_enter(&vp->v_lock);
1661     if (avc->vrefCount <= 0)
1662         osi_Panic("afs_inactive : v_count <=0\n");
1663
1664     /*
1665      * If more than 1 don't unmap the vnode but do decrement the ref count
1666      */
1667     vp->v_count--;
1668     if (vp->v_count > 0) {
1669         mutex_exit(&vp->v_lock);
1670         return 0;
1671     }
1672     mutex_exit(&vp->v_lock);
1673
1674     /*
1675      * Solaris calls VOP_OPEN on exec, but doesn't call VOP_CLOSE when
1676      * the executable exits.  So we clean up the open count here.
1677      *
1678      * Only do this for mvstat 0 vnodes: when using fakestat, we can't
1679      * lose the open count for volume roots (mvstat 2), even though they
1680      * will get VOP_INACTIVE'd when released by afs_PutFakeStat().
1681      */
1682     if (avc->opens > 0 && avc->mvstat == 0 && !(avc->f.states & CCore))
1683         avc->opens = avc->execsOrWriters = 0;
1684
1685     afs_InactiveVCache(avc, acred);
1686
1687     AFS_GUNLOCK();
1688     /* VFS_RELE must be called outside of GLOCK, since it can potentially
1689      * call afs_freevfs, which acquires GLOCK */
1690     VFS_RELE(afs_globalVFS);
1691     AFS_GLOCK();
1692
1693     return 0;
1694 }
1695
1696 void
1697 gafs_inactive(struct vcache *avc, afs_ucred_t *acred)
1698 {
1699     AFS_GLOCK();
1700     (void)afs_inactive(avc, acred);
1701     AFS_GUNLOCK();
1702 }
1703
1704
1705 int
1706 gafs_fid(struct vcache *avc, struct fid **fidpp)
1707 {
1708     int code;
1709
1710     AFS_GLOCK();
1711     code = afs_fid(avc, fidpp);
1712     AFS_GUNLOCK();
1713     return (code);
1714 }
1715
1716 #endif /* AFS_GLOBAL_SUNLOCK */