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