e85beb939181a578c5bfe768ff9b067a67b697e8
[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 /**
467  * Dummy pvn_vplist_dirty() handler for non-writable vnodes.
468  */
469 static int
470 afs_never_putapage(struct vnode *vp, struct page *pages, u_offset_t * offp,
471              size_t * lenp, int flags, afs_ucred_t *credp)
472 {
473     struct vcache *avc = VTOAFS(vp);
474     osi_Assert((avc->f.states & CRO) != 0);
475     osi_Panic("Dirty pages while flushing a read-only volume vnode.");
476     return EIO; /* unreachable */
477 }
478
479 int
480 afs_putpage(struct vnode *vp, offset_t off, u_int len, int flags, 
481             afs_ucred_t *cred)
482 {
483     struct vcache *avc;
484     struct page *pages;
485     afs_int32 code = 0;
486     size_t tlen;
487     afs_offs_t endPos;
488     afs_int32 NPages = 0;
489     u_offset_t toff = off;
490     int didLock = 0;
491
492     AFS_STATCNT(afs_putpage);
493     if (vp->v_flag & VNOMAP)    /* file doesn't allow mapping */
494         return (ENOSYS);
495
496     /*
497      * Putpage (ASYNC) is called every sec to flush out dirty vm pages 
498      */
499     AFS_GLOCK();
500     afs_Trace4(afs_iclSetp, CM_TRACE_PAGEOUT, ICL_TYPE_POINTER,
501                (afs_int32) vp, ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(off),
502                ICL_TYPE_INT32, (afs_int32) len, ICL_TYPE_LONG, (int)flags);
503     avc = VTOAFS(vp);
504
505     /* Get a list of modified (or whatever) pages */
506     if (len) {
507         ObtainSharedLock(&avc->lock, 247);
508         didLock = SHARED_LOCK;
509         endPos = (afs_offs_t) off + len;        /* position we're supposed to write up to */
510         while ((afs_offs_t) toff < endPos
511                && (afs_offs_t) toff < avc->f.m.Length) {
512             /* If not invalidating pages use page_lookup_nowait to avoid reclaiming
513              * them from the free list
514              */
515             AFS_GUNLOCK();
516             if (flags & (B_FREE | B_INVAL))
517                 pages = page_lookup(vp, toff, SE_EXCL);
518             else
519                 pages = page_lookup_nowait(vp, toff, SE_SHARED);
520             if (!pages || !pvn_getdirty(pages, flags))
521                 tlen = PAGESIZE;
522             else {
523                 if (didLock == SHARED_LOCK) {
524                     AFS_GLOCK();
525                     didLock = WRITE_LOCK;
526                     UpgradeSToWLock(&avc->lock, 671);
527                     AFS_GUNLOCK();
528                 }
529                 NPages++;
530                 code = afs_putapage(vp, pages, &toff, &tlen, flags, cred);
531                 if (code) {
532                     AFS_GLOCK();
533                     break;
534                 }
535             }
536             toff += tlen;
537             AFS_GLOCK();
538         }
539     } else {
540         /*
541          * We normally arrive here due to a vm flush.
542          *
543          * If this vnode belongs to a writable volume, obtain a vcache lock
544          * then call pvn_vplist_dirty to free, invalidate, or to write out
545          * dirty pages with afs_putapage.  The afs_putapage routine requires a
546          * vcache lock, so we obtain it here before any page locks are taken.
547          * This locking order is done to avoid deadlocking due to races with
548          * afs_getpage, which also takes vcache and page locks.
549          *
550          * If this vnode belongs to a non-writable volume, then it will not
551          * contain dirty pages, so we do not need to lock the vcache and since
552          * afs_putapage will not be called.  Instead, forgo the vcache lock and
553          * call pvn_vplist_dirty to free, or invalidate pages. Pass a dummy
554          * page out handler to pvn_vplist_dirty which we do not expect to be
555          * called.  Panic if the dummy handler is called, since something went
556          * horribly wrong.
557          */
558         if ((avc->f.states & CRO) == 0) {
559             ObtainWriteLock(&avc->lock, 670);
560             didLock = WRITE_LOCK;
561         }
562         AFS_GUNLOCK();
563         if ((avc->f.states & CRO) == 0)
564             code = pvn_vplist_dirty(vp, toff, afs_putapage, flags, cred);
565         else
566             code = pvn_vplist_dirty(vp, toff, afs_never_putapage, flags, cred);
567         AFS_GLOCK();
568     }
569
570     if (code && !avc->vc_error) {
571         if (didLock == 0) {
572             ObtainWriteLock(&avc->lock, 668);
573             didLock = WRITE_LOCK;
574         } else if (didLock == SHARED_LOCK) {
575             UpgradeSToWLock(&avc->lock, 669);
576             didLock = WRITE_LOCK;
577         }
578         avc->vc_error = code;
579     }
580
581     if (didLock == WRITE_LOCK)
582         ReleaseWriteLock(&avc->lock);
583     else if (didLock == SHARED_LOCK)
584         ReleaseSharedLock(&avc->lock);
585     afs_Trace2(afs_iclSetp, CM_TRACE_PAGEOUTDONE, ICL_TYPE_LONG, code,
586                ICL_TYPE_LONG, NPages);
587     AFS_GUNLOCK();
588     return (code);
589 }
590
591
592 int
593 afs_putapage(struct vnode *vp, struct page *pages, u_offset_t * offp,
594              size_t * lenp, int flags, afs_ucred_t *credp)
595 {
596     struct buf *tbuf;
597     struct vcache *avc = VTOAFS(vp);
598     afs_int32 code = 0;
599     u_int tlen = PAGESIZE;
600     afs_offs_t off = (pages->p_offset / PAGESIZE) * PAGESIZE;
601
602     /*
603      * Now we've got the modified pages.  All pages are locked and held 
604      * XXX Find a kluster that fits in one block (or page). We also
605      * adjust the i/o if the file space is less than a while page. XXX
606      */
607     if (off + tlen > avc->f.m.Length) {
608         tlen = avc->f.m.Length - off;
609     }
610     /* can't call mapout with 0 length buffers (rmfree panics) */
611     if (((tlen >> 24) & 0xff) == 0xff) {
612         tlen = 0;
613     }
614     if ((int)tlen > 0) {
615         /*
616          * Can't call mapout with 0 length buffers since we'll get rmfree panics
617          */
618         tbuf = pageio_setup(pages, tlen, vp, B_WRITE | flags);
619         if (!tbuf)
620             return (ENOMEM);
621
622         tbuf->b_dev = 0;
623         tbuf->b_lblkno = lbtodb(pages->p_offset);
624         bp_mapin(tbuf);
625         AFS_GLOCK();
626         afs_Trace4(afs_iclSetp, CM_TRACE_PAGEOUTONE, ICL_TYPE_LONG, avc,
627                    ICL_TYPE_LONG, pages, ICL_TYPE_LONG, tlen, ICL_TYPE_OFFSET,
628                    ICL_HANDLE_OFFSET(off));
629         code = afs_ustrategy(tbuf, credp);      /* unlocks page */
630         AFS_GUNLOCK();
631         bp_mapout(tbuf);
632     }
633     pvn_write_done(pages, ((code) ? B_ERROR : 0) | B_WRITE | flags);
634     if ((int)tlen > 0)
635         pageio_done(tbuf);
636     if (offp)
637         *offp = off;
638     if (lenp)
639         *lenp = tlen;
640     return code;
641 }
642
643 int
644 afs_nfsrdwr(struct vcache *avc, struct uio *auio, enum uio_rw arw,
645             int ioflag, afs_ucred_t *acred)
646 {
647     afs_int32 code;
648     afs_int32 code2;
649     afs_int32 code_checkcode = 0;
650     int counter;
651     afs_int32 mode, sflags;
652     char *data;
653     struct dcache *dcp, *dcp_newpage;
654     afs_size_t fileBase, size;
655     afs_size_t pageBase;
656     afs_int32 tsize;
657     afs_int32 pageOffset, extraResid = 0;
658     afs_size_t origLength;      /* length when reading/writing started */
659     long appendLength;  /* length when this call will finish */
660     int created;                /* created pages instead of faulting them */
661     int lockCode;
662     int didFakeOpen, eof;
663     struct vrequest treq;
664     caddr_t raddr;
665     u_int rsize;
666
667     AFS_STATCNT(afs_nfsrdwr);
668
669     /* can't read or write other things */
670     if (vType(avc) != VREG)
671         return EISDIR;
672
673     if (auio->uio_resid == 0)
674         return (0);
675
676     afs_Trace4(afs_iclSetp, CM_TRACE_VMRW, ICL_TYPE_POINTER, (afs_int32) avc,
677                ICL_TYPE_LONG, (arw == UIO_WRITE ? 1 : 0), ICL_TYPE_OFFSET,
678                ICL_HANDLE_OFFSET(auio->uio_loffset), ICL_TYPE_OFFSET,
679                ICL_HANDLE_OFFSET(auio->uio_resid));
680
681 #ifndef AFS_64BIT_CLIENT
682     if (AfsLargeFileUio(auio))  /* file is larger than 2 GB */
683         return (EFBIG);
684 #endif
685
686     if (!acred)
687         osi_Panic("rdwr: !acred");
688
689     if (code = afs_InitReq(&treq, acred))
690         return code;
691
692     /* It's not really possible to know if a write cause a growth in the
693      * cache size, we we wait for a cache drain for any write.
694      */
695     afs_MaybeWakeupTruncateDaemon();
696     while ((arw == UIO_WRITE)
697            && (afs_blocksUsed > PERCENT(CM_WAITFORDRAINPCT, afs_cacheBlocks))) {
698         if (afs_blocksUsed - afs_blocksDiscarded >
699             PERCENT(CM_WAITFORDRAINPCT, afs_cacheBlocks)) {
700             afs_WaitForCacheDrain = 1;
701             afs_osi_Sleep(&afs_WaitForCacheDrain);
702         }
703         afs_MaybeFreeDiscardedDCache();
704         afs_MaybeWakeupTruncateDaemon();
705     }
706     code = afs_VerifyVCache(avc, &treq);
707     if (code)
708         return afs_CheckCode(code, &treq, 45);
709
710     osi_FlushPages(avc, acred);
711
712     ObtainWriteLock(&avc->lock, 250);
713
714     /* adjust parameters when appending files */
715     if ((ioflag & IO_APPEND) && arw == UIO_WRITE) {
716         auio->uio_loffset = avc->f.m.Length;    /* write at EOF position */
717     }
718     if (auio->afsio_offset < 0 || (auio->afsio_offset + auio->uio_resid) < 0) {
719         ReleaseWriteLock(&avc->lock);
720         return EINVAL;
721     }
722 #ifndef AFS_64BIT_CLIENT
723     /* file is larger than 2GB */
724     if (AfsLargeFileSize(auio->uio_offset, auio->uio_resid)) {
725         ReleaseWriteLock(&avc->lock);
726         return EFBIG;
727     }
728 #endif
729
730     didFakeOpen = 0;            /* keep track of open so we can do close */
731     if (arw == UIO_WRITE) {
732         /* do ulimit processing; shrink resid or fail */
733         if (auio->uio_loffset + auio->afsio_resid > auio->uio_llimit) {
734             if (auio->uio_loffset >= auio->uio_llimit) {
735                 ReleaseWriteLock(&avc->lock);
736                 return EFBIG;
737             } else {
738                 /* track # of bytes we should write, but won't because of
739                  * ulimit; we must add this into the final resid value
740                  * so caller knows we punted some data.
741                  */
742                 extraResid = auio->uio_resid;
743                 auio->uio_resid = auio->uio_llimit - auio->uio_loffset;
744                 extraResid -= auio->uio_resid;
745             }
746         }
747         mode = S_WRITE;         /* segment map-in mode */
748         afs_FakeOpen(avc);      /* do this for writes, so data gets put back
749                                  * when we want it to be put back */
750         didFakeOpen = 1;        /* we'll be doing a fake open */
751         /* before starting any I/O, we must ensure that the file is big enough
752          * to hold the results (since afs_putpage will be called to force the I/O */
753         size = auio->afsio_resid + auio->afsio_offset;  /* new file size */
754         appendLength = size;
755         origLength = avc->f.m.Length;
756         if (size > avc->f.m.Length) {
757             afs_Trace4(afs_iclSetp, CM_TRACE_SETLENGTH, ICL_TYPE_STRING,
758                        __FILE__, ICL_TYPE_LONG, __LINE__, ICL_TYPE_OFFSET,
759                        ICL_HANDLE_OFFSET(avc->f.m.Length), ICL_TYPE_OFFSET,
760                        ICL_HANDLE_OFFSET(size));
761             avc->f.m.Length = size;     /* file grew */
762         }
763         avc->f.states |= CDirty;        /* Set the dirty bit */
764         avc->f.m.Date = osi_Time();     /* Set file date (for ranlib) */
765     } else {
766         mode = S_READ;          /* map-in read-only */
767         origLength = avc->f.m.Length;
768     }
769
770     if (acred && AFS_NFSXLATORREQ(acred)) {
771         if (arw == UIO_READ) {
772             if (!afs_AccessOK
773                 (avc, PRSFS_READ, &treq,
774                  CHECK_MODE_BITS | CMB_ALLOW_EXEC_AS_READ)) {
775                 ReleaseWriteLock(&avc->lock);
776                 return EACCES;
777             }
778         }
779         crhold(acred);
780         if (avc->credp) {
781             crfree(avc->credp);
782         }
783         avc->credp = acred;
784     }
785     counter = 0;                /* don't call afs_DoPartialWrite first time through. */
786     while (1) {
787         /* compute the amount of data to move into this block,
788          * based on auio->afsio_resid.  Note that we copy data in units of
789          * MAXBSIZE, not PAGESIZE.  This is because segmap_getmap panics if you
790          * call it with an offset based on blocks smaller than MAXBSIZE
791          * (implying that it should be named BSIZE, since it is clearly both a
792          * max and a min). */
793         size = auio->afsio_resid;       /* transfer size */     
794         fileBase = ((arw == UIO_READ) && (origLength < auio->uio_offset)) ? 
795             origLength : auio->afsio_offset;  /* start file position for xfr */
796         pageBase = fileBase & ~(MAXBSIZE - 1);  /* file position of the page */
797         pageOffset = fileBase & (MAXBSIZE - 1); /* xfr start's offset within page */
798         tsize = MAXBSIZE - pageOffset;  /* how much more fits in this page */
799         /* we'll read tsize bytes, but first must make sure tsize isn't too big */
800         if (tsize > size)
801             tsize = size;       /* don't read past end of request */
802         eof = 0;                /* flag telling us if we hit the EOF on the read */
803         if (arw == UIO_READ) {  /* we're doing a read operation */
804             /* don't read past EOF */
805             if (fileBase + tsize > origLength) {
806                 tsize = origLength - fileBase;
807                 eof = 1;        /* we did hit the EOF */
808                 if (tsize < 0)
809                     tsize = 0;  /* better safe than sorry */
810             }
811             sflags = 0;
812         } else {
813             /* Purge dirty chunks of file if there are too many dirty
814              * chunks. Inside the write loop, we only do this at a chunk
815              * boundary. Clean up partial chunk if necessary at end of loop.
816              */
817             if (counter > 0 && code == 0 && AFS_CHUNKOFFSET(fileBase) == 0) {
818                 code = afs_DoPartialWrite(avc, &treq);
819                 if (code)
820                     break;
821             }
822             /* write case, we ask segmap_release to call putpage.  Really, we
823              * don't have to do this on every page mapin, but for now we're
824              * lazy, and don't modify the rest of AFS to scan for modified
825              * pages on a close or other "synchronize with file server"
826              * operation.  This makes things a little cleaner, but probably
827              * hurts performance. */
828             sflags = SM_WRITE;
829         }
830         if (tsize <= 0) {
831             code = 0;
832             break;              /* nothing to transfer, we're done */
833         }
834         if (arw == UIO_WRITE)
835             avc->f.states |= CDirty;    /* may have been cleared by DoPartialWrite */
836
837         /* Before dropping lock, hold the chunk (create it if necessary).  This
838          * serves two purposes:  (1) Ensure Cache Truncate Daemon doesn't try
839          * to purge the chunk's pages while we have them locked.  This would
840          * cause deadlock because we might be waiting for the CTD to free up
841          * a chunk.  (2)  If we're writing past the original EOF, and we're
842          * at the base of the chunk, then make sure it exists online
843          * before we do the uiomove, since the segmap_release will
844          * write out to the chunk, causing it to get fetched if it hasn't
845          * been created yet.  The code that would otherwise notice that
846          * we're fetching a chunk past EOF won't work, since we've
847          * already adjusted the file size above.
848          */
849         ObtainWriteLock(&avc->vlock, 551);
850         while (avc->vstates & VPageCleaning) {
851             ReleaseWriteLock(&avc->vlock);
852             ReleaseWriteLock(&avc->lock);
853             afs_osi_Sleep(&avc->vstates);
854             ObtainWriteLock(&avc->lock, 334);
855             ObtainWriteLock(&avc->vlock, 552);
856         }
857         ReleaseWriteLock(&avc->vlock);
858         {
859             afs_size_t toff, tlen;
860             dcp = afs_GetDCache(avc, fileBase, &treq, &toff, &tlen, 2);
861             if (!dcp) {
862                 code = EIO;
863                 break;
864             }
865         }
866         ReleaseWriteLock(&avc->lock);   /* uiomove may page fault */
867         AFS_GUNLOCK();
868         data = segmap_getmap(segkmap, AFSTOV(avc), (u_offset_t) pageBase);
869         raddr = (caddr_t) (((uintptr_t) data + pageOffset) & PAGEMASK);
870         rsize =
871             (((u_int) data + pageOffset + tsize + PAGEOFFSET) & PAGEMASK) -
872             (u_int) raddr;
873         if (code == 0) {
874             /* if we're doing a write, and we're starting at the rounded
875              * down page base, and we're writing enough data to cover all
876              * created pages, then we must be writing all of the pages
877              * in this MAXBSIZE window that we're creating.
878              */
879             created = 0;
880             if (arw == UIO_WRITE && ((long)raddr == (long)data + pageOffset)
881                 && tsize >= rsize) {
882                 /* probably the dcache backing this guy is around, but if
883                  * not, we can't do this optimization, since we're creating
884                  * writable pages, which must be backed by a chunk.
885                  */
886                 AFS_GLOCK();
887                 dcp_newpage = afs_FindDCache(avc, pageBase);
888                 if (dcp_newpage
889                     && hsame(avc->f.m.DataVersion, dcp_newpage->f.versionNo)) {
890                     ObtainWriteLock(&avc->lock, 251);
891                     ObtainWriteLock(&avc->vlock, 576);
892                     ObtainReadLock(&dcp_newpage->lock);
893                     if ((avc->activeV == 0)
894                         && hsame(avc->f.m.DataVersion, dcp_newpage->f.versionNo)
895                         && !(dcp_newpage->dflags & (DFFetching))) {
896                         AFS_GUNLOCK();
897                         segmap_pagecreate(segkmap, raddr, rsize, 1);
898                         AFS_GLOCK();
899                         ObtainWriteLock(&afs_xdcache, 252);
900                         /* Mark the pages as created and dirty */
901                         afs_indexFlags[dcp_newpage->index]
902                             |= (IFAnyPages | IFDirtyPages);
903                         ReleaseWriteLock(&afs_xdcache);
904                         created = 1;
905                     }
906                     ReleaseReadLock(&dcp_newpage->lock);
907                     afs_PutDCache(dcp_newpage);
908                     ReleaseWriteLock(&avc->vlock);
909                     ReleaseWriteLock(&avc->lock);
910                 } else if (dcp_newpage)
911                     afs_PutDCache(dcp_newpage);
912                 AFS_GUNLOCK();
913             }
914             if (!created)
915                 code =
916                     afs_fc2errno(segmap_fault
917                                  (kas.a_hat, segkmap, raddr, rsize,
918                                   F_SOFTLOCK, mode));
919         }
920         if (code == 0) {
921             AFS_UIOMOVE(data + pageOffset, tsize, arw, auio, code);
922             segmap_fault(kas.a_hat, segkmap, raddr, rsize, F_SOFTUNLOCK,
923                          mode);
924         }
925         if (code == 0) {
926             code = segmap_release(segkmap, data, sflags);
927         } else {
928             (void)segmap_release(segkmap, data, 0);
929         }
930         AFS_GLOCK();
931         ObtainWriteLock(&avc->lock, 253);
932         counter++;
933         if (dcp)
934             afs_PutDCache(dcp);
935         if (code)
936             break;
937     }
938     if (didFakeOpen) {
939         afs_FakeClose(avc, acred);
940     }
941     if (arw == UIO_WRITE && (avc->f.states & CDirty)) {
942         code2 = afs_DoPartialWrite(avc, &treq);
943         if (!code)
944             code = code2;
945     }
946
947     if (!code && avc->vc_error) {
948         code = code_checkcode = avc->vc_error;
949     }
950     ReleaseWriteLock(&avc->lock);
951     if (!code) {
952         if ((ioflag & FSYNC) && (arw == UIO_WRITE)
953             && !AFS_NFSXLATORREQ(acred))
954             code = afs_fsync(avc, 0, acred);
955     }
956     /* 
957      * If things worked, add in as remaining in request any bytes
958      * we didn't write due to file size ulimit.
959      */
960     if (code == 0 && extraResid > 0)
961         auio->uio_resid += extraResid;
962     if (code_checkcode) {
963         return code_checkcode;
964     } else {
965         return afs_CheckCode(code, &treq, 46);
966     }
967 }
968
969 int
970 afs_map(struct vnode *vp, offset_t off, struct as *as, caddr_t *addr, size_t len, u_char prot, u_char maxprot, u_int flags, afs_ucred_t *cred)
971 {
972     struct segvn_crargs crargs;
973     afs_int32 code;
974     struct vrequest treq;
975     struct vcache *avc = VTOAFS(vp);
976
977     AFS_STATCNT(afs_map);
978
979
980     /* check for reasonableness on segment bounds; apparently len can be < 0 */
981     if (off < 0 || off + len < 0) {
982         return (EINVAL);
983     }
984 #ifndef AFS_64BIT_CLIENT
985     if (AfsLargeFileSize(off, len)) {   /* file is larger than 2 GB */
986         code = EFBIG;
987         goto out;
988     }
989 #endif
990
991     if (vp->v_flag & VNOMAP)    /* File isn't allowed to be mapped */
992         return (ENOSYS);
993
994     if (vp->v_filocks)          /* if locked, disallow mapping */
995         return (EAGAIN);
996
997     AFS_GLOCK();
998     if (code = afs_InitReq(&treq, cred))
999         goto out;
1000
1001     if (vp->v_type != VREG) {
1002         code = ENODEV;
1003         goto out;
1004     }
1005
1006     code = afs_VerifyVCache(avc, &treq);
1007     if (code) {
1008         goto out;
1009     }
1010     osi_FlushPages(avc, cred);  /* ensure old pages are gone */
1011     avc->f.states |= CMAPPED;   /* flag cleared at afs_inactive */
1012
1013     AFS_GUNLOCK();
1014     as_rangelock(as);
1015     if ((flags & MAP_FIXED) == 0) {
1016 #ifdef MAPADDR_LACKS_VACALIGN
1017         map_addr(addr, len, off, flags);
1018 #else
1019         map_addr(addr, len, off, 1, flags);
1020 #endif
1021         if (*addr == NULL) {
1022             as_rangeunlock(as);
1023             code = ENOMEM;
1024             goto out1;
1025         }
1026     } else
1027         (void)as_unmap(as, *addr, len); /* unmap old address space use */
1028     /* setup the create parameter block for the call */
1029     crargs.vp = AFSTOV(avc);
1030     crargs.offset = (u_offset_t)off;
1031     crargs.cred = cred;
1032     crargs.type = flags & MAP_TYPE;
1033     crargs.prot = prot;
1034     crargs.maxprot = maxprot;
1035     crargs.amp = (struct anon_map *)0;
1036     crargs.flags = flags & ~MAP_TYPE;
1037
1038     code = as_map(as, *addr, len, segvn_create, (char *)&crargs);
1039     as_rangeunlock(as);
1040   out1:
1041     AFS_GLOCK();
1042     code = afs_CheckCode(code, &treq, 47);
1043     AFS_GUNLOCK();
1044     return code;
1045   out:
1046     code = afs_CheckCode(code, &treq, 48);
1047     AFS_GUNLOCK();
1048     return code;
1049 }
1050
1051
1052 /*
1053  * For Now We use standard local kernel params for AFS system values. Change this
1054  * at some point.
1055  */
1056 int
1057 #ifdef AFS_SUN511_ENV
1058 afs_pathconf(struct vnode *vp, int cmd, u_long *outdatap,
1059              afs_ucred_t *credp, caller_context_t *ct)
1060 #else
1061 afs_pathconf(struct vnode *vp, int cmd, u_long *outdatap,
1062              afs_ucred_t *credp)
1063 #endif /* AFS_SUN511_ENV */
1064 {
1065     AFS_STATCNT(afs_cntl);
1066     switch (cmd) {
1067     case _PC_LINK_MAX:
1068         *outdatap = MAXLINK;
1069         break;
1070     case _PC_NAME_MAX:
1071         *outdatap = MAXNAMLEN;
1072         break;
1073     case _PC_PATH_MAX:
1074         *outdatap = MAXPATHLEN;
1075         break;
1076     case _PC_CHOWN_RESTRICTED:
1077         *outdatap = 1;
1078         break;
1079     case _PC_NO_TRUNC:
1080         *outdatap = 1;
1081         break;
1082     case _PC_FILESIZEBITS:
1083 #ifdef AFS_64BIT_CLIENT
1084         *outdatap = 64;
1085 #else
1086         *outdatap = 32;
1087 #endif
1088         break;
1089     default:
1090 #ifdef AFS_SUN511_ENV
1091         return fs_pathconf(vp, cmd, outdatap, credp, ct);
1092 #else
1093         return fs_pathconf(vp, cmd, outdatap, credp);
1094 #endif /* AFS_SUN511_ENV */
1095     }
1096     return 0;
1097 }
1098
1099 int
1100 afs_ioctl(struct vnode *vnp, int com, int arg, int flag, cred_t *credp, 
1101           int *rvalp)
1102 {
1103     return (ENOTTY);
1104 }
1105
1106 void
1107 afs_rwlock(struct vnode *vnp, int wlock)
1108 {
1109     rw_enter(&(VTOAFS(vnp))->rwlock, (wlock ? RW_WRITER : RW_READER));
1110 }
1111
1112
1113 void
1114 afs_rwunlock(struct vnode *vnp, int wlock)
1115 {
1116     rw_exit(&(VTOAFS(vnp))->rwlock);
1117 }
1118
1119
1120 /* NOT SUPPORTED */
1121 int
1122 afs_seek(struct vnode *vnp, offset_t ooff, offset_t *noffp)
1123 {
1124     int code = 0;
1125
1126 #ifndef AFS_64BIT_CLIENT
1127 # define __MAXOFF_T MAXOFF_T
1128 #else
1129 # define __MAXOFF_T MAXOFFSET_T
1130 #endif
1131
1132     if ((*noffp < 0 || *noffp > __MAXOFF_T))
1133         code = EINVAL;
1134     return code;
1135 }
1136
1137 int
1138 #ifdef AFS_SUN59_ENV
1139 afs_frlock(struct vnode *vnp, int cmd, struct flock64 *ap, int flag, 
1140            offset_t off, struct flk_callback *flkcb, afs_ucred_t *credp)
1141 #else
1142 afs_frlock(struct vnode *vnp, int cmd, struct flock64 *ap, int flag,
1143            offset_t off, afs_ucred_t *credp)
1144 #endif
1145 {
1146     afs_int32 code = 0;
1147     /*
1148      * Implement based on afs_lockctl
1149      */
1150     AFS_GLOCK();
1151 #ifdef AFS_SUN59_ENV
1152     if (flkcb)
1153         afs_warn("Don't know how to deal with flk_callback's!\n");
1154 #endif
1155     if ((cmd == F_GETLK) || (cmd == F_O_GETLK) || (cmd == F_SETLK)
1156         || (cmd == F_SETLKW)) {
1157         ap->l_pid = ttoproc(curthread)->p_pid;
1158         ap->l_sysid = 0;
1159
1160         AFS_GUNLOCK();
1161         code = convoff(vnp, ap, 0, off);
1162         if (code)
1163             return code;
1164         AFS_GLOCK();
1165     }
1166
1167     code = afs_lockctl(VTOAFS(vnp), ap, cmd, credp);
1168     AFS_GUNLOCK();
1169     return code;
1170 }
1171
1172
1173 int
1174 afs_space(struct vnode *vnp, int cmd, struct flock64 *ap, int flag, 
1175           offset_t off, afs_ucred_t *credp)
1176 {
1177     afs_int32 code = EINVAL;
1178     struct vattr vattr;
1179
1180     if ((cmd == F_FREESP)
1181         && ((code = convoff(vnp, ap, 0, off)) == 0)) {
1182         AFS_GLOCK();
1183         if (!ap->l_len) {
1184             vattr.va_mask = AT_SIZE;
1185             vattr.va_size = ap->l_start;
1186             code = afs_setattr(VTOAFS(vnp), &vattr, 0, credp);
1187         }
1188         AFS_GUNLOCK();
1189     }
1190     return (code);
1191 }
1192
1193 int
1194 afs_dump(struct vnode *vp, caddr_t addr, int i1, int i2)
1195 {
1196     AFS_STATCNT(afs_dump);
1197     afs_warn("AFS_DUMP. MUST IMPLEMENT THIS!!!\n");
1198     return EINVAL;
1199 }
1200
1201
1202 /* Nothing fancy here; just compare if vnodes are identical ones */
1203 int
1204 afs_cmp(struct vnode *vp1, struct vnode *vp2)
1205 {
1206     AFS_STATCNT(afs_cmp);
1207     return (vp1 == vp2);
1208 }
1209
1210
1211 int
1212 afs_realvp(struct vnode *vp, struct vnode **vpp)
1213 {
1214     AFS_STATCNT(afs_realvp);
1215     return EINVAL;
1216 }
1217
1218
1219 int
1220 afs_pageio(struct vnode *vp, struct page *pp, u_int ui1, u_int ui2, int i1, 
1221            struct cred *credp)
1222 {
1223     afs_warn("afs_pageio: Not implemented\n");
1224     return EINVAL;
1225 }
1226
1227 int
1228 #ifdef AFS_SUN59_ENV
1229 afs_dumpctl(struct vnode *vp, int i, int *blkp)
1230 #else
1231 afs_dumpctl(struct vnode *vp, int i)
1232 #endif
1233 {
1234     afs_warn("afs_dumpctl: Not implemented\n");
1235     return EINVAL;
1236 }
1237
1238 #ifdef  AFS_SUN511_ENV
1239 extern void
1240 afs_dispose(struct vnode *vp, struct page *p, int fl, int dn, struct cred *cr, struct caller_context_t *ct)
1241 {
1242     fs_dispose(vp, p, fl, dn, cr,ct);
1243 }
1244
1245 int
1246 afs_setsecattr(struct vnode *vp, vsecattr_t *vsecattr, int flag, struct cred *creds, struct caller_context_t *ct)
1247 {
1248     return ENOSYS;
1249 }
1250
1251 int
1252 afs_getsecattr(struct vnode *vp, vsecattr_t *vsecattr, int flag, struct cred *creds, struct caller_context_t *ct)
1253 {
1254   return fs_fab_acl(vp, vsecattr, flag, creds,ct);
1255 }
1256 #else
1257 extern void
1258 afs_dispose(struct vnode *vp, struct page *p, int fl, int dn, struct cred *cr)
1259 {
1260     fs_dispose(vp, p, fl, dn, cr);
1261 }
1262
1263 int
1264 afs_setsecattr(struct vnode *vp, vsecattr_t *vsecattr, int flag, 
1265                struct cred *creds)
1266 {
1267     return ENOSYS;
1268 }
1269
1270 int
1271 afs_getsecattr(struct vnode *vp, vsecattr_t *vsecattr, int flag, struct cred *creds)
1272 {
1273     return fs_fab_acl(vp, vsecattr, flag, creds);
1274 }
1275 #endif
1276
1277 #ifdef  AFS_GLOBAL_SUNLOCK
1278 extern int gafs_open(struct vcache **avcp, afs_int32 aflags, 
1279                      afs_ucred_t *acred);
1280 extern int gafs_close(struct vcache *avc, afs_int32 aflags, 
1281                       int count, offset_t offset, afs_ucred_t *acred);
1282 extern int afs_ioctl(struct vnode *vnp, int com, int arg, int flag, 
1283                      cred_t *credp, int *rvalp);
1284 extern int gafs_access(struct vcache *avc, afs_int32 amode,
1285                        int flags, afs_ucred_t *acred);
1286 extern int gafs_getattr(struct vcache *avc, 
1287                         struct vattr *attrs, int flags, 
1288                         afs_ucred_t *acred);
1289 extern int gafs_setattr(struct vcache *avc, 
1290                         struct vattr *attrs, int flags, 
1291                         afs_ucred_t *acred);
1292 extern int gafs_lookup(struct vcache *adp, char *aname, 
1293                        struct vcache **avcp, struct pathname *pnp,
1294                        int flags, struct vnode *rdir, afs_ucred_t *acred);
1295 extern int gafs_remove(struct vcache *adp, char *aname, 
1296                        afs_ucred_t *acred);
1297 extern int gafs_link(struct vcache *adp, struct vcache *avc,
1298                      char *aname, afs_ucred_t *acred);
1299 extern int gafs_rename(struct vcache *aodp, char *aname1,
1300                        struct vcache *andp, char *aname2,
1301                        afs_ucred_t *acred);
1302 extern int gafs_symlink(struct vcache *adp, char *aname, 
1303                         struct vattr *attrs, char *atargetName, 
1304                         afs_ucred_t *acred);
1305 extern int gafs_rmdir(struct vcache *adp, char *aname, 
1306                       struct vnode *cdirp, afs_ucred_t *acred);
1307 extern int gafs_mkdir(struct vcache *adp, char *aname, 
1308                       struct vattr *attrs, struct vcache **avcp, 
1309                       afs_ucred_t *acred);
1310 extern int gafs_fsync(struct vcache *avc, int flag, afs_ucred_t *acred);
1311 extern int gafs_readlink(struct vcache *avc, struct uio *auio, 
1312                          afs_ucred_t *acred);
1313 extern int gafs_readdir(struct vcache *avc, struct uio *auio,
1314                         afs_ucred_t *acred, int *eofp);
1315 extern void gafs_inactive(struct vcache *avc, 
1316                           afs_ucred_t *acred);
1317 extern int gafs_fid(struct vcache *avc, struct fid **fidpp);
1318 extern int gafs_create(struct vcache *adp, char *aname, 
1319                        struct vattr *attrs, enum vcexcl aexcl, int amode, 
1320                        struct vcache **avcp, afs_ucred_t *acred);
1321 #ifdef AFS_SUN511_ENV
1322 extern int afs_pathconf(struct vnode *vp, int cmd, u_long *outdatap,
1323                         afs_ucred_t *credp, caller_context_t *ct);
1324 #else
1325 extern int afs_pathconf(struct vnode *vp, int cmd, u_long *outdatap,
1326                         afs_ucred_t *credp);
1327 #endif /* AFS_SUN511_ENV */
1328
1329 #if defined(AFS_SUN511_ENV)
1330 /* The following list must always be NULL-terminated */
1331 const fs_operation_def_t afs_vnodeops_template[] = {
1332     VOPNAME_OPEN,               { .vop_open = gafs_open },
1333     VOPNAME_CLOSE,              { .vop_close = gafs_close },
1334     VOPNAME_READ,               { .vop_read = afs_vmread },
1335     VOPNAME_WRITE,              { .vop_write = afs_vmwrite },
1336     VOPNAME_IOCTL,              { .vop_ioctl = afs_ioctl },
1337     VOPNAME_SETFL,              { .vop_setfl = fs_setfl },
1338     VOPNAME_GETATTR,            { .vop_getattr = gafs_getattr },
1339     VOPNAME_SETATTR,            { .vop_setattr = gafs_setattr },
1340     VOPNAME_ACCESS,             { .vop_access = gafs_access },
1341     VOPNAME_LOOKUP,             { .vop_lookup = gafs_lookup },
1342     VOPNAME_CREATE,             { .vop_create = gafs_create },
1343     VOPNAME_REMOVE,             { .vop_remove = gafs_remove },
1344     VOPNAME_LINK,               { .vop_link = gafs_link },
1345     VOPNAME_RENAME,             { .vop_rename = gafs_rename },
1346     VOPNAME_MKDIR,              { .vop_mkdir = gafs_mkdir },
1347     VOPNAME_RMDIR,              { .vop_rmdir = gafs_rmdir },
1348     VOPNAME_READDIR,            { .vop_readdir = gafs_readdir },
1349     VOPNAME_SYMLINK,            { .vop_symlink = gafs_symlink },   
1350     VOPNAME_READLINK,           { .vop_readlink = gafs_readlink },
1351     VOPNAME_FSYNC,              { .vop_fsync = gafs_fsync },
1352     VOPNAME_INACTIVE,           { .vop_inactive = gafs_inactive },
1353     VOPNAME_FID,                { .vop_fid = gafs_fid },
1354     VOPNAME_RWLOCK,             { .vop_rwlock = afs_rwlock },
1355     VOPNAME_RWUNLOCK,           { .vop_rwunlock = afs_rwunlock },
1356     VOPNAME_SEEK,               { .vop_seek = afs_seek },
1357     VOPNAME_CMP,                { .vop_cmp = afs_cmp },
1358     VOPNAME_FRLOCK,             { .vop_frlock = afs_frlock },
1359     VOPNAME_SPACE,              { .vop_space = afs_space },
1360     VOPNAME_REALVP,             { .vop_realvp = afs_realvp },
1361     VOPNAME_GETPAGE,            { .vop_getpage = afs_getpage },
1362     VOPNAME_PUTPAGE,            { .vop_putpage = afs_putpage },
1363     VOPNAME_MAP,                { .vop_map = afs_map },
1364     VOPNAME_ADDMAP,             { .vop_addmap = afs_addmap },
1365     VOPNAME_DELMAP,             { .vop_delmap = afs_delmap },
1366     VOPNAME_POLL,               { .vop_poll = fs_poll },
1367     VOPNAME_PATHCONF,           { .vop_pathconf = afs_pathconf },
1368     VOPNAME_PAGEIO,             { .vop_pageio = afs_pageio },
1369     VOPNAME_DUMP,               { .vop_dump = afs_dump },
1370     VOPNAME_DUMPCTL,            { .vop_dumpctl = afs_dumpctl },   
1371     VOPNAME_DISPOSE,            { .vop_dispose = afs_dispose },
1372     VOPNAME_GETSECATTR,         { .vop_getsecattr = afs_getsecattr },
1373     VOPNAME_SETSECATTR,         { .vop_setsecattr = afs_setsecattr },
1374     VOPNAME_SHRLOCK,            { .vop_shrlock = fs_shrlock },
1375     NULL,                       NULL
1376 };
1377 vnodeops_t *afs_ops;
1378 #elif defined(AFS_SUN510_ENV)
1379 /* The following list must always be NULL-terminated */
1380 const fs_operation_def_t afs_vnodeops_template[] = {
1381     VOPNAME_OPEN,               gafs_open,
1382     VOPNAME_CLOSE,              gafs_close,
1383     VOPNAME_READ,               afs_vmread,
1384     VOPNAME_WRITE,              afs_vmwrite,
1385     VOPNAME_IOCTL,              afs_ioctl,
1386     VOPNAME_SETFL,              fs_setfl,
1387     VOPNAME_GETATTR,            gafs_getattr,
1388     VOPNAME_SETATTR,            gafs_setattr,
1389     VOPNAME_ACCESS,             gafs_access,
1390     VOPNAME_LOOKUP,             gafs_lookup,
1391     VOPNAME_CREATE,             gafs_create,
1392     VOPNAME_REMOVE,             gafs_remove,
1393     VOPNAME_LINK,               gafs_link,
1394     VOPNAME_RENAME,             gafs_rename,
1395     VOPNAME_MKDIR,              gafs_mkdir,
1396     VOPNAME_RMDIR,              gafs_rmdir,
1397     VOPNAME_READDIR,            gafs_readdir,
1398     VOPNAME_SYMLINK,            gafs_symlink,   
1399     VOPNAME_READLINK,           gafs_readlink,
1400     VOPNAME_FSYNC,              gafs_fsync,
1401     VOPNAME_INACTIVE,           gafs_inactive,
1402     VOPNAME_FID,                gafs_fid,
1403     VOPNAME_RWLOCK,             afs_rwlock,
1404     VOPNAME_RWUNLOCK,           afs_rwunlock,
1405     VOPNAME_SEEK,               afs_seek,
1406     VOPNAME_CMP,                afs_cmp,
1407     VOPNAME_FRLOCK,             afs_frlock,
1408     VOPNAME_SPACE,              afs_space,
1409     VOPNAME_REALVP,             afs_realvp,
1410     VOPNAME_GETPAGE,            afs_getpage,
1411     VOPNAME_PUTPAGE,            afs_putpage,
1412     VOPNAME_MAP,                afs_map,
1413     VOPNAME_ADDMAP,             afs_addmap,
1414     VOPNAME_DELMAP,             afs_delmap,
1415     VOPNAME_POLL,               fs_poll,
1416     VOPNAME_DUMP,               afs_dump,
1417     VOPNAME_PATHCONF,           afs_pathconf,
1418     VOPNAME_PAGEIO,             afs_pageio,
1419     VOPNAME_DUMPCTL,            afs_dumpctl,   
1420     VOPNAME_DISPOSE,            afs_dispose,
1421     VOPNAME_GETSECATTR,       afs_getsecattr,
1422     VOPNAME_SETSECATTR,         afs_setsecattr,
1423     VOPNAME_SHRLOCK,            fs_shrlock,
1424     NULL,                     NULL
1425 };
1426 struct vnodeops *afs_ops;
1427 #else
1428 struct vnodeops Afs_vnodeops = {
1429     gafs_open,
1430     gafs_close,
1431     afs_vmread,
1432     afs_vmwrite,
1433     afs_ioctl,
1434     fs_setfl,
1435     gafs_getattr,
1436     gafs_setattr,
1437     gafs_access,
1438     gafs_lookup,
1439     gafs_create,
1440     gafs_remove,
1441     gafs_link,
1442     gafs_rename,
1443     gafs_mkdir,
1444     gafs_rmdir,
1445     gafs_readdir,
1446     gafs_symlink,
1447     gafs_readlink,
1448     gafs_fsync,
1449     gafs_inactive,
1450     gafs_fid,
1451     afs_rwlock,
1452     afs_rwunlock,
1453     afs_seek,
1454     afs_cmp,
1455     afs_frlock,
1456     afs_space,
1457     afs_realvp,
1458     afs_getpage,
1459     afs_putpage,
1460     afs_map,
1461     afs_addmap,
1462     afs_delmap,
1463     fs_poll,
1464     afs_dump,
1465     afs_pathconf,
1466     afs_pageio,
1467     afs_dumpctl,
1468     afs_dispose,
1469     afs_setsecattr,
1470     afs_getsecattr,
1471     fs_shrlock,
1472 };
1473 struct vnodeops *afs_ops = &Afs_vnodeops;
1474 #endif
1475
1476 int
1477 gafs_open(struct vcache **avcp, afs_int32 aflags, 
1478           afs_ucred_t *acred)
1479 {
1480     int code;
1481
1482     AFS_GLOCK();
1483     code = afs_open(avcp, aflags, acred);
1484     AFS_GUNLOCK();
1485     return (code);
1486 }
1487
1488 int
1489 gafs_close(struct vcache *avc, afs_int32 aflags, int count, 
1490            offset_t offset, afs_ucred_t *acred)
1491 {
1492     int code;
1493
1494     AFS_GLOCK();
1495     code = afs_close(avc, aflags, count, offset, acred);
1496     AFS_GUNLOCK();
1497     return (code);
1498 }
1499
1500 int
1501 gafs_getattr(struct vcache *avc, struct vattr *attrs, 
1502              int flags, afs_ucred_t *acred)
1503 {
1504     int code;
1505
1506     AFS_GLOCK();
1507     code = afs_getattr(avc, attrs, flags, acred);
1508     AFS_GUNLOCK();
1509     return (code);
1510 }
1511
1512
1513 int
1514 gafs_setattr(struct vcache *avc, struct vattr *attrs, 
1515              int flags, afs_ucred_t *acred)
1516 {
1517     int code;
1518
1519     AFS_GLOCK();
1520     code = afs_setattr(avc, attrs, flags, acred);
1521     AFS_GUNLOCK();
1522     return (code);
1523 }
1524
1525
1526 int
1527 gafs_access(struct vcache *avc, afs_int32 amode, int flags, 
1528             afs_ucred_t *acred)
1529 {
1530     int code;
1531
1532     AFS_GLOCK();
1533     code = afs_access(avc, amode, flags, acred);
1534     AFS_GUNLOCK();
1535     return (code);
1536 }
1537
1538
1539 int
1540 gafs_lookup(struct vcache *adp, char *aname, 
1541             struct vcache **avcp, struct pathname *pnp, int flags, 
1542             struct vnode *rdir, afs_ucred_t *acred)
1543 {
1544     int code;
1545
1546     AFS_GLOCK();
1547     code = afs_lookup(adp, aname, avcp, pnp, flags, rdir, acred);
1548     AFS_GUNLOCK();
1549     return (code);
1550 }
1551
1552
1553 int
1554 gafs_create(struct vcache *adp, char *aname, struct vattr *attrs, 
1555             enum vcexcl aexcl, int amode, struct vcache **avcp, 
1556             afs_ucred_t *acred)
1557 {
1558     int code;
1559
1560     AFS_GLOCK();
1561     code = afs_create(adp, aname, attrs, aexcl, amode, avcp, acred);
1562     AFS_GUNLOCK();
1563     return (code);
1564 }
1565
1566 int
1567 gafs_remove(struct vcache *adp, char *aname, afs_ucred_t *acred)
1568 {
1569     int code;
1570
1571     AFS_GLOCK();
1572     code = afs_remove(adp, aname, acred);
1573     AFS_GUNLOCK();
1574     return (code);
1575 }
1576
1577 int
1578 gafs_link(struct vcache *adp, struct vcache *avc, 
1579           char *aname, afs_ucred_t *acred)
1580 {
1581     int code;
1582
1583     AFS_GLOCK();
1584     code = afs_link(adp, avc, aname, acred);
1585     AFS_GUNLOCK();
1586     return (code);
1587 }
1588
1589 int
1590 gafs_rename(struct vcache *aodp, char *aname1, 
1591             struct vcache *andp, char *aname2, 
1592             afs_ucred_t *acred)
1593 {
1594     int code;
1595
1596     AFS_GLOCK();
1597     code = afs_rename(aodp, aname1, andp, aname2, acred);
1598 #ifdef AFS_SUN510_ENV
1599     if (code == 0) {
1600         struct vcache *avcp = NULL;
1601         
1602         (void) afs_lookup(andp, aname2, &avcp, NULL, 0, NULL, acred);
1603         if (avcp) {
1604             struct vnode *vp = AFSTOV(avcp), *pvp = AFSTOV(andp);
1605
1606 # ifdef HAVE_VN_RENAMEPATH
1607             vn_renamepath(pvp, vp, aname2, strlen(aname2));
1608 # else
1609             mutex_enter(&vp->v_lock);
1610             if (vp->v_path != NULL) {
1611                 kmem_free(vp->v_path, strlen(vp->v_path) + 1);
1612                 vp->v_path = NULL;
1613             }
1614             mutex_exit(&vp->v_lock);
1615             vn_setpath(afs_globalVp, pvp, vp, aname2, strlen(aname2));
1616 # endif /* !HAVE_VN_RENAMEPATH */
1617
1618             AFS_RELE(avcp);
1619         }
1620     }
1621 #endif
1622     AFS_GUNLOCK();
1623     return (code);
1624 }
1625
1626 int
1627 gafs_mkdir(struct vcache *adp, char *aname, struct vattr *attrs, 
1628            struct vcache **avcp, afs_ucred_t *acred)
1629 {
1630     int code;
1631
1632     AFS_GLOCK();
1633     code = afs_mkdir(adp, aname, attrs, avcp, acred);
1634     AFS_GUNLOCK();
1635     return (code);
1636 }
1637
1638 int
1639 gafs_rmdir(struct vcache *adp, char *aname, struct vnode *cdirp, 
1640            afs_ucred_t *acred)
1641 {
1642     int code;
1643
1644     AFS_GLOCK();
1645     code = afs_rmdir(adp, aname, cdirp, acred);
1646     AFS_GUNLOCK();
1647     return (code);
1648 }
1649
1650
1651 int
1652 gafs_readdir(struct vcache *avc, struct uio *auio,
1653              afs_ucred_t *acred, int *eofp)
1654 {
1655     int code;
1656
1657     AFS_GLOCK();
1658     code = afs_readdir(avc, auio, acred, eofp);
1659     AFS_GUNLOCK();
1660     return (code);
1661 }
1662
1663 int
1664 gafs_symlink(struct vcache *adp, char *aname, struct vattr *attrs,
1665              char *atargetName, afs_ucred_t *acred)
1666 {
1667     int code;
1668
1669     AFS_GLOCK();
1670     code = afs_symlink(adp, aname, attrs, atargetName, NULL, acred);
1671     AFS_GUNLOCK();
1672     return (code);
1673 }
1674
1675
1676 int
1677 gafs_readlink(struct vcache *avc, struct uio *auio, afs_ucred_t *acred)
1678 {
1679     int code;
1680
1681     AFS_GLOCK();
1682     code = afs_readlink(avc, auio, acred);
1683     AFS_GUNLOCK();
1684     return (code);
1685 }
1686
1687 int
1688 gafs_fsync(struct vcache *avc, int flag, afs_ucred_t *acred)
1689 {
1690     int code;
1691
1692     AFS_GLOCK();
1693     code = afs_fsync(avc, flag, acred);
1694     AFS_GUNLOCK();
1695     return (code);
1696 }
1697
1698 int
1699 afs_inactive(struct vcache *avc, afs_ucred_t *acred)
1700 {
1701     struct vnode *vp = AFSTOV(avc);
1702     if (afs_shuttingdown != AFS_RUNNING)
1703         return 0;
1704
1705     /*
1706      * In Solaris and HPUX s800 and HP-UX10.0 they actually call us with
1707      * v_count 1 on last reference!
1708      */
1709     mutex_enter(&vp->v_lock);
1710     if (avc->vrefCount <= 0)
1711         osi_Panic("afs_inactive : v_count <=0\n");
1712
1713     /*
1714      * If more than 1 don't unmap the vnode but do decrement the ref count
1715      */
1716     vp->v_count--;
1717     if (vp->v_count > 0) {
1718         mutex_exit(&vp->v_lock);
1719         return 0;
1720     }
1721     mutex_exit(&vp->v_lock);
1722
1723 #ifndef AFS_SUN511_ENV
1724     /*
1725      * Solaris calls VOP_OPEN on exec, but doesn't call VOP_CLOSE when
1726      * the executable exits.  So we clean up the open count here.
1727      *
1728      * Only do this for AFS_MVSTAT_FILE vnodes: when using fakestat, we can't
1729      * lose the open count for volume roots (AFS_MVSTAT_ROOT), even though they
1730      * will get VOP_INACTIVE'd when released by afs_PutFakeStat().
1731      */
1732     if (avc->opens > 0 && avc->mvstat == AFS_MVSTAT_FILE && !(avc->f.states & CCore))
1733         avc->opens = avc->execsOrWriters = 0;
1734 #endif
1735
1736     afs_InactiveVCache(avc, acred);
1737
1738     AFS_GUNLOCK();
1739     /* VFS_RELE must be called outside of GLOCK, since it can potentially
1740      * call afs_freevfs, which acquires GLOCK */
1741     VFS_RELE(afs_globalVFS);
1742     AFS_GLOCK();
1743
1744     return 0;
1745 }
1746
1747 void
1748 gafs_inactive(struct vcache *avc, afs_ucred_t *acred)
1749 {
1750     AFS_GLOCK();
1751     (void)afs_inactive(avc, acred);
1752     AFS_GUNLOCK();
1753 }
1754
1755
1756 int
1757 gafs_fid(struct vcache *avc, struct fid **fidpp)
1758 {
1759     int code;
1760
1761     AFS_GLOCK();
1762     code = afs_fid(avc, fidpp);
1763     AFS_GUNLOCK();
1764     return (code);
1765 }
1766
1767 #endif /* AFS_GLOBAL_SUNLOCK */