FBSD: Remove support for 8.x and 9.x
[openafs.git] / src / afs / FBSD / osi_vnodeops.c
1 /*
2  * A large chunk of this file appears to be copied directly from
3  * sys/nfsclient/nfs_bio.c, which has the following license:
4  */
5 /*
6  * Copyright (c) 1989, 1993
7  *      The Regents of the University of California.  All rights reserved.
8  *
9  * This code is derived from software contributed to Berkeley by
10  * Rick Macklem at The University of Guelph.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *      This product includes software developed by the University of
23  *      California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *      @(#)nfs_bio.c   8.9 (Berkeley) 3/30/95
41  */
42 /*
43  * Pursuant to a statement of U.C. Berkeley dated 1999-07-22, this license
44  * is amended to drop clause (3) above.
45  */
46
47 #include <afsconfig.h>
48 #include <afs/param.h>
49
50
51 #include <afs/sysincludes.h>    /* Standard vendor system headers */
52 #include <afsincludes.h>        /* Afs-based standard headers */
53 #include <afs/afs_stats.h>      /* statistics */
54 #include <sys/malloc.h>
55 #include <sys/namei.h>
56 #include <sys/unistd.h>
57 #if __FreeBSD_version >= 1000030
58 #include <sys/rwlock.h>
59 #endif
60 #include <vm/vm_page.h>
61 #include <vm/vm_object.h>
62 #include <vm/vm_pager.h>
63 #include <vm/vnode_pager.h>
64 extern int afs_pbuf_freecnt;
65
66 static vop_access_t     afs_vop_access;
67 static vop_advlock_t    afs_vop_advlock;
68 static vop_close_t      afs_vop_close;
69 static vop_create_t     afs_vop_create;
70 static vop_fsync_t      afs_vop_fsync;
71 static vop_getattr_t    afs_vop_getattr;
72 static vop_getpages_t   afs_vop_getpages;
73 static vop_inactive_t   afs_vop_inactive;
74 static vop_ioctl_t      afs_vop_ioctl;
75 static vop_link_t       afs_vop_link;
76 static vop_lookup_t     afs_vop_lookup;
77 static vop_mkdir_t      afs_vop_mkdir;
78 static vop_mknod_t      afs_vop_mknod;
79 static vop_open_t       afs_vop_open;
80 static vop_pathconf_t   afs_vop_pathconf;
81 static vop_print_t      afs_vop_print;
82 static vop_putpages_t   afs_vop_putpages;
83 static vop_read_t       afs_vop_read;
84 static vop_readdir_t    afs_vop_readdir;
85 static vop_readlink_t   afs_vop_readlink;
86 static vop_reclaim_t    afs_vop_reclaim;
87 static vop_remove_t     afs_vop_remove;
88 static vop_rename_t     afs_vop_rename;
89 static vop_rmdir_t      afs_vop_rmdir;
90 static vop_setattr_t    afs_vop_setattr;
91 static vop_strategy_t   afs_vop_strategy;
92 static vop_symlink_t    afs_vop_symlink;
93 static vop_write_t      afs_vop_write;
94
95 struct vop_vector afs_vnodeops = {
96         .vop_default =          &default_vnodeops,
97         .vop_access =           afs_vop_access,
98         .vop_advlock =          afs_vop_advlock,
99         .vop_close =            afs_vop_close,
100         .vop_create =           afs_vop_create,
101         .vop_fsync =            afs_vop_fsync,
102         .vop_getattr =          afs_vop_getattr,
103         .vop_getpages =         afs_vop_getpages,
104         .vop_inactive =         afs_vop_inactive,
105         .vop_ioctl =            afs_vop_ioctl,
106         .vop_link =             afs_vop_link,
107         .vop_lookup =           afs_vop_lookup,
108         .vop_mkdir =            afs_vop_mkdir,
109         .vop_mknod =            afs_vop_mknod,
110         .vop_open =             afs_vop_open,
111         .vop_pathconf =         afs_vop_pathconf,
112         .vop_print =            afs_vop_print,
113         .vop_putpages =         afs_vop_putpages,
114         .vop_read =             afs_vop_read,
115         .vop_readdir =          afs_vop_readdir,
116         .vop_readlink =         afs_vop_readlink,
117         .vop_reclaim =          afs_vop_reclaim,
118         .vop_remove =           afs_vop_remove,
119         .vop_rename =           afs_vop_rename,
120         .vop_rmdir =            afs_vop_rmdir,
121         .vop_setattr =          afs_vop_setattr,
122         .vop_strategy =         afs_vop_strategy,
123         .vop_symlink =          afs_vop_symlink,
124         .vop_write =            afs_vop_write,
125 };
126
127 #define GETNAME()       \
128     struct componentname *cnp = ap->a_cnp; \
129     char *name; \
130     MALLOC(name, char *, cnp->cn_namelen+1, M_TEMP, M_WAITOK); \
131     memcpy(name, cnp->cn_nameptr, cnp->cn_namelen); \
132     name[cnp->cn_namelen] = '\0'
133
134 #define DROPNAME() FREE(name, M_TEMP)
135
136 /*
137  * Here we define compatibility functions/macros for interfaces that
138  * have changed between different FreeBSD versions.
139  */
140 static __inline void ma_vm_page_lock_queues(void) {};
141 static __inline void ma_vm_page_unlock_queues(void) {};
142 static __inline void ma_vm_page_lock(vm_page_t m) { vm_page_lock(m); };
143 static __inline void ma_vm_page_unlock(vm_page_t m) { vm_page_unlock(m); };
144
145 #define ma_vn_lock(vp, flags, p) (vn_lock(vp, flags))
146 #define MA_VOP_LOCK(vp, flags, p) (VOP_LOCK(vp, flags))
147 #define MA_VOP_UNLOCK(vp, flags, p) (VOP_UNLOCK(vp, flags))
148
149 #define MA_PCPU_INC(c) PCPU_INC(c)
150 #define MA_PCPU_ADD(c, n) PCPU_ADD(c, n)
151
152 #if __FreeBSD_version >= 1000030
153 #define AFS_VM_OBJECT_WLOCK(o)  VM_OBJECT_WLOCK(o)
154 #define AFS_VM_OBJECT_WUNLOCK(o)        VM_OBJECT_WUNLOCK(o)
155 #else
156 #define AFS_VM_OBJECT_WLOCK(o)  VM_OBJECT_LOCK(o)
157 #define AFS_VM_OBJECT_WUNLOCK(o)        VM_OBJECT_UNLOCK(o)
158 #endif
159
160 /*
161  * Mosty copied from sys/ufs/ufs/ufs_vnops.c:ufs_pathconf().
162  * We should know the correct answers to these questions with
163  * respect to the AFS protocol (which may differ from the UFS
164  * values) but for the moment this will do.
165  */
166 static int
167 afs_vop_pathconf(struct vop_pathconf_args *ap)
168 {
169         int error;
170
171         error = 0;
172         switch (ap->a_name) {
173         case _PC_LINK_MAX:
174                 *ap->a_retval = LINK_MAX;
175                 break;
176         case _PC_NAME_MAX:
177                 *ap->a_retval = NAME_MAX;
178                 break;
179         case _PC_PATH_MAX:
180                 *ap->a_retval = PATH_MAX;
181                 break;
182         case _PC_PIPE_BUF:
183                 *ap->a_retval = PIPE_BUF;
184                 break;
185         case _PC_CHOWN_RESTRICTED:
186                 *ap->a_retval = 1;
187                 break;
188         case _PC_NO_TRUNC:
189                 *ap->a_retval = 1;
190                 break;
191 #ifdef _PC_ACL_EXTENDED
192         case _PC_ACL_EXTENDED:
193                 *ap->a_retval = 0;
194                 break;
195         case _PC_ACL_PATH_MAX:
196                 *ap->a_retval = 3;
197                 break;
198 #endif
199 #ifdef _PC_MAC_PRESENT
200         case _PC_MAC_PRESENT:
201                 *ap->a_retval = 0;
202                 break;
203 #endif
204 #ifdef _PC_ASYNC_IO
205         case _PC_ASYNC_IO:
206                 /* _PC_ASYNC_IO should have been handled by upper layers. */
207                 KASSERT(0, ("_PC_ASYNC_IO should not get here"));
208                 error = EINVAL;
209                 break;
210         case _PC_PRIO_IO:
211                 *ap->a_retval = 0;
212                 break;
213         case _PC_SYNC_IO:
214                 *ap->a_retval = 0;
215                 break;
216 #endif
217 #ifdef _PC_ALLOC_SIZE_MIN
218         case _PC_ALLOC_SIZE_MIN:
219                 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_bsize;
220                 break;
221 #endif
222 #ifdef _PC_FILESIZEBITS
223         case _PC_FILESIZEBITS:
224                 *ap->a_retval = 32; /* XXX */
225                 break;
226 #endif
227 #ifdef _PC_REC_INCR_XFER_SIZE
228         case _PC_REC_INCR_XFER_SIZE:
229                 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
230                 break;
231         case _PC_REC_MAX_XFER_SIZE:
232                 *ap->a_retval = -1; /* means ``unlimited'' */
233                 break;
234         case _PC_REC_MIN_XFER_SIZE:
235                 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
236                 break;
237         case _PC_REC_XFER_ALIGN:
238                 *ap->a_retval = PAGE_SIZE;
239                 break;
240 #endif
241 #ifdef _PC_SYMLINK_MAX
242         case _PC_SYMLINK_MAX:
243                 *ap->a_retval = MAXPATHLEN;
244                 break;
245 #endif
246         default:
247                 error = EINVAL;
248                 break;
249         }
250         return (error);
251 }
252
253 int
254 afs_vop_lookup(ap)
255      struct vop_lookup_args     /* {
256                                  * struct vnodeop_desc * a_desc;
257                                  * struct vnode *a_dvp;
258                                  * struct vnode **a_vpp;
259                                  * struct componentname *a_cnp;
260                                  * } */ *ap;
261 {
262     int error;
263     struct vcache *vcp;
264     struct vnode *vp, *dvp;
265     int flags = ap->a_cnp->cn_flags;
266     int lockparent;             /* 1 => lockparent flag is set */
267     int wantparent;             /* 1 => wantparent or lockparent flag */
268
269     dvp = ap->a_dvp;
270     if (dvp->v_type != VDIR) {
271         return ENOTDIR;
272     }
273
274     if ((flags & ISDOTDOT) && (dvp->v_vflag & VV_ROOT))
275         return EIO;
276
277     GETNAME();
278
279     lockparent = flags & LOCKPARENT;
280     wantparent = flags & (LOCKPARENT | WANTPARENT);
281
282 #if __FreeBSD_version < 1000021
283     cnp->cn_flags |= MPSAFE; /* steel */
284 #endif
285
286     if (flags & ISDOTDOT)
287         MA_VOP_UNLOCK(dvp, 0, p);
288
289     AFS_GLOCK();
290     error = afs_lookup(VTOAFS(dvp), name, &vcp, cnp->cn_cred);
291     AFS_GUNLOCK();
292
293     if (error) {
294         if (flags & ISDOTDOT)
295             MA_VOP_LOCK(dvp, LK_EXCLUSIVE | LK_RETRY, p);
296         if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME)
297             && (flags & ISLASTCN) && error == ENOENT)
298             error = EJUSTRETURN;
299         if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
300             cnp->cn_flags |= SAVENAME;
301         DROPNAME();
302         *ap->a_vpp = 0;
303         return (error);
304     }
305     vp = AFSTOV(vcp);           /* always get a node if no error */
306
307     /* The parent directory comes in locked.  We unlock it on return
308      * unless the caller wants it left locked.
309      * we also always return the vnode locked. */
310
311     if (flags & ISDOTDOT) {
312         /* vp before dvp since we go root to leaf, and .. comes first */
313         ma_vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
314         ma_vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, p);
315         /* always return the child locked */
316         if (lockparent && (flags & ISLASTCN)
317             && (error = ma_vn_lock(dvp, LK_EXCLUSIVE, p))) {
318             vput(vp);
319             DROPNAME();
320             return (error);
321         }
322     } else if (vp == dvp) {
323         /* they're the same; afs_lookup() already ref'ed the leaf.
324          * It came in locked, so we don't need to ref OR lock it */
325     } else {
326         ma_vn_lock(vp, LK_EXCLUSIVE | LK_CANRECURSE | LK_RETRY, p);
327         /* always return the child locked */
328     }
329     *ap->a_vpp = vp;
330
331     if ((cnp->cn_nameiop == RENAME && wantparent && (flags & ISLASTCN))
332         || (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN)))
333         cnp->cn_flags |= SAVENAME;
334
335     DROPNAME();
336     return error;
337 }
338
339 int
340 afs_vop_create(ap)
341      struct vop_create_args     /* {
342                                  * struct vnode *a_dvp;
343                                  * struct vnode **a_vpp;
344                                  * struct componentname *a_cnp;
345                                  * struct vattr *a_vap;
346                                  * } */ *ap;
347 {
348     int error = 0;
349     struct vcache *vcp;
350     struct vnode *dvp = ap->a_dvp;
351     GETNAME();
352
353     AFS_GLOCK();
354     error =
355         afs_create(VTOAFS(dvp), name, ap->a_vap,
356                    ap->a_vap->va_vaflags & VA_EXCLUSIVE ? EXCL : NONEXCL,
357                    ap->a_vap->va_mode, &vcp, cnp->cn_cred);
358     AFS_GUNLOCK();
359     if (error) {
360         DROPNAME();
361         return (error);
362     }
363
364     if (vcp) {
365         *ap->a_vpp = AFSTOV(vcp);
366         ma_vn_lock(AFSTOV(vcp), LK_EXCLUSIVE | LK_RETRY, p);
367     } else
368         *ap->a_vpp = 0;
369
370     DROPNAME();
371     return error;
372 }
373
374 int
375 afs_vop_mknod(ap)
376      struct vop_mknod_args      /* {
377                                  * struct vnode *a_dvp;
378                                  * struct vnode **a_vpp;
379                                  * struct componentname *a_cnp;
380                                  * struct vattr *a_vap;
381                                  * } */ *ap;
382 {
383     return (ENODEV);
384 }
385
386 int
387 afs_vop_open(ap)
388      struct vop_open_args       /* {
389                                  * struct vnode *a_vp;
390                                  * int  a_mode;
391                                  * struct ucred *a_cred;
392                                  * struct thread *a_td;
393                                  * struct file *a_fp;
394                                  * } */ *ap;
395 {
396     int error;
397     struct vcache *vc = VTOAFS(ap->a_vp);
398
399     AFS_GLOCK();
400     error = afs_open(&vc, ap->a_mode, ap->a_cred);
401 #ifdef DIAGNOSTIC
402     if (AFSTOV(vc) != ap->a_vp)
403         panic("AFS open changed vnode!");
404 #endif
405     AFS_GUNLOCK();
406     vnode_create_vobject(ap->a_vp, vc->f.m.Length, ap->a_td);
407     osi_FlushPages(vc, ap->a_cred);
408     return error;
409 }
410
411 int
412 afs_vop_close(ap)
413      struct vop_close_args      /* {
414                                  * struct vnode *a_vp;
415                                  * int  a_fflag;
416                                  * struct ucred *a_cred;
417                                  * struct thread *a_td;
418                                  * } */ *ap;
419 {
420     int code, iflag;
421     struct vnode *vp = ap->a_vp;
422     struct vcache *avc = VTOAFS(vp);
423
424     VI_LOCK(vp);
425     iflag = vp->v_iflag & VI_DOOMED;
426     VI_UNLOCK(vp);
427     if (iflag & VI_DOOMED) {
428         /* osi_FlushVCache (correctly) calls vgone() on recycled vnodes, we don't
429          * have an afs_close to process, in that case */
430         if (avc->opens != 0)
431             panic("afs_vop_close: doomed vnode %p has vcache %p with non-zero opens %d\n",
432                   vp, avc, avc->opens);
433         return 0;
434     }
435
436     AFS_GLOCK();
437     if (ap->a_cred)
438         code = afs_close(avc, ap->a_fflag, ap->a_cred);
439     else
440         code = afs_close(avc, ap->a_fflag, afs_osi_credp);
441     osi_FlushPages(avc, ap->a_cred);    /* hold GLOCK, but not basic vnode lock */
442     AFS_GUNLOCK();
443     return code;
444 }
445
446 int
447 afs_vop_access(ap)
448      struct vop_access_args     /* {
449                                  * struct vnode *a_vp;
450                                  * accmode_t a_accmode;
451                                  * struct ucred *a_cred;
452                                  * struct thread *a_td;
453                                  * } */ *ap;
454 {
455     int code;
456     AFS_GLOCK();
457     code = afs_access(VTOAFS(ap->a_vp), ap->a_accmode, ap->a_cred);
458     AFS_GUNLOCK();
459     return code;
460 }
461
462 int
463 afs_vop_getattr(ap)
464      struct vop_getattr_args    /* {
465                                  * struct vnode *a_vp;
466                                  * struct vattr *a_vap;
467                                  * struct ucred *a_cred;
468                                  * } */ *ap;
469 {
470     int code;
471
472     AFS_GLOCK();
473     code = afs_getattr(VTOAFS(ap->a_vp), ap->a_vap, ap->a_cred);
474     AFS_GUNLOCK();
475
476     return code;
477 }
478
479 int
480 afs_vop_setattr(ap)
481      struct vop_setattr_args    /* {
482                                  * struct vnode *a_vp;
483                                  * struct vattr *a_vap;
484                                  * struct ucred *a_cred;
485                                  * } */ *ap;
486 {
487     int code;
488     AFS_GLOCK();
489     code = afs_setattr(VTOAFS(ap->a_vp), ap->a_vap, ap->a_cred);
490     AFS_GUNLOCK();
491     return code;
492 }
493
494 int
495 afs_vop_read(ap)
496      struct vop_read_args       /* {
497                                  * struct vnode *a_vp;
498                                  * struct uio *a_uio;
499                                  * int a_ioflag;
500                                  * struct ucred *a_cred;
501                                  * 
502                                  * } */ *ap;
503 {
504     int code;
505     struct vcache *avc = VTOAFS(ap->a_vp);
506     AFS_GLOCK();
507     osi_FlushPages(avc, ap->a_cred);    /* hold GLOCK, but not basic vnode lock */
508     code = afs_read(avc, ap->a_uio, ap->a_cred, 0);
509     AFS_GUNLOCK();
510     return code;
511 }
512
513 /* struct vop_getpages_args {
514  *      struct vnode *a_vp;
515  *      vm_page_t *a_m;
516  *      int a_count;
517  *      int *a_rbehind;
518  *      int *a_rahead;
519  * };
520  */
521 int
522 afs_vop_getpages(struct vop_getpages_args *ap)
523 {
524     int code;
525     int i, nextoff, size, toff, npages, count;
526     struct uio uio;
527     struct iovec iov;
528     struct buf *bp;
529     vm_offset_t kva;
530     vm_object_t object;
531     vm_page_t *pages;
532     struct vnode *vp;
533     struct vcache *avc;
534
535     memset(&uio, 0, sizeof(uio));
536     memset(&iov, 0, sizeof(iov));
537
538     vp = ap->a_vp;
539     avc = VTOAFS(vp);
540     pages = ap->a_m;
541 #ifdef FBSD_VOP_GETPAGES_BUSIED
542     npages = ap->a_count;
543     if (ap->a_rbehind)
544         *ap->a_rbehind = 0;
545     if (ap->a_rahead)
546         *ap->a_rahead = 0;
547 #else
548     npages = btoc(ap->a_count);
549 #endif
550
551     if ((object = vp->v_object) == NULL) {
552         printf("afs_getpages: called with non-merged cache vnode??\n");
553         return VM_PAGER_ERROR;
554     }
555
556     /*
557      * If the requested page is partially valid, just return it and
558      * allow the pager to zero-out the blanks.  Partially valid pages
559      * can only occur at the file EOF.
560      */
561     {
562 #ifdef FBSD_VOP_GETPAGES_BUSIED
563         AFS_VM_OBJECT_WLOCK(object);
564         ma_vm_page_lock_queues();
565         if(pages[npages - 1]->valid != 0) {
566             if (--npages == 0) {
567                 ma_vm_page_unlock_queues();
568                 AFS_VM_OBJECT_WUNLOCK(object);
569                 return (VM_PAGER_OK);
570             }
571         }
572 #else
573         vm_page_t m = pages[ap->a_reqpage];
574         AFS_VM_OBJECT_WLOCK(object);
575         ma_vm_page_lock_queues();
576         if (m->valid != 0) {
577             /* handled by vm_fault now        */
578             /* vm_page_zero_invalid(m, TRUE); */
579             for (i = 0; i < npages; ++i) {
580                 if (i != ap->a_reqpage) {
581                     ma_vm_page_lock(pages[i]);
582                     vm_page_free(pages[i]);
583                     ma_vm_page_unlock(pages[i]);
584                 }
585             }
586             ma_vm_page_unlock_queues();
587             AFS_VM_OBJECT_WUNLOCK(object);
588             return (0);
589         }
590 #endif
591         ma_vm_page_unlock_queues();
592         AFS_VM_OBJECT_WUNLOCK(object);
593     }
594     bp = getpbuf(&afs_pbuf_freecnt);
595
596     kva = (vm_offset_t) bp->b_data;
597     pmap_qenter(kva, pages, npages);
598     MA_PCPU_INC(cnt.v_vnodein);
599     MA_PCPU_ADD(cnt.v_vnodepgsin, npages);
600
601 #ifdef FBSD_VOP_GETPAGES_BUSIED
602     count = ctob(npages);
603 #else
604     count = ap->a_count;
605 #endif
606     iov.iov_base = (caddr_t) kva;
607     iov.iov_len = count;
608     uio.uio_iov = &iov;
609     uio.uio_iovcnt = 1;
610     uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
611     uio.uio_resid = count;
612     uio.uio_segflg = UIO_SYSSPACE;
613     uio.uio_rw = UIO_READ;
614     uio.uio_td = curthread;
615
616     AFS_GLOCK();
617     osi_FlushPages(avc, osi_curcred()); /* hold GLOCK, but not basic vnode lock */
618     code = afs_read(avc, &uio, osi_curcred(), 0);
619     AFS_GUNLOCK();
620     pmap_qremove(kva, npages);
621
622     relpbuf(bp, &afs_pbuf_freecnt);
623
624     if (code && (uio.uio_resid == count)) {
625 #ifndef FBSD_VOP_GETPAGES_BUSIED
626         AFS_VM_OBJECT_WLOCK(object);
627         ma_vm_page_lock_queues();
628         for (i = 0; i < npages; ++i) {
629             if (i != ap->a_reqpage)
630                 vm_page_free(pages[i]);
631         }
632         ma_vm_page_unlock_queues();
633         AFS_VM_OBJECT_WUNLOCK(object);
634 #endif
635         return VM_PAGER_ERROR;
636     }
637
638     size = count - uio.uio_resid;
639     AFS_VM_OBJECT_WLOCK(object);
640     ma_vm_page_lock_queues();
641     for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
642         vm_page_t m;
643         nextoff = toff + PAGE_SIZE;
644         m = pages[i];
645
646         /* XXX not in nfsclient? */
647         m->flags &= ~PG_ZERO;
648
649         if (nextoff <= size) {
650             /*
651              * Read operation filled an entire page
652              */
653             m->valid = VM_PAGE_BITS_ALL;
654             KASSERT(m->dirty == 0, ("afs_getpages: page %p is dirty", m));
655         } else if (size > toff) {
656             /*
657              * Read operation filled a partial page.
658              */
659             m->valid = 0;
660             vm_page_set_validclean(m, 0, size - toff);
661             KASSERT(m->dirty == 0, ("afs_getpages: page %p is dirty", m));
662         }
663
664 #ifndef FBSD_VOP_GETPAGES_BUSIED
665         if (i != ap->a_reqpage) {
666 #if __FreeBSD_version >= 1000042
667             vm_page_readahead_finish(m);
668 #else
669             /*
670              * Whether or not to leave the page activated is up in
671              * the air, but we should put the page on a page queue
672              * somewhere (it already is in the object).  Result:
673              * It appears that emperical results show that
674              * deactivating pages is best.
675              */
676
677             /*
678              * Just in case someone was asking for this page we
679              * now tell them that it is ok to use.
680              */
681             if (!code) {
682                 if (m->oflags & VPO_WANTED) {
683                     ma_vm_page_lock(m);
684                     vm_page_activate(m);
685                     ma_vm_page_unlock(m);
686                 }
687                 else {
688                     ma_vm_page_lock(m);
689                     vm_page_deactivate(m);
690                     ma_vm_page_unlock(m);
691                 }
692                 vm_page_wakeup(m);
693             } else {
694                 ma_vm_page_lock(m);
695                 vm_page_free(m);
696                 ma_vm_page_unlock(m);
697             }
698 #endif  /* __FreeBSD_version 1000042 */
699         }
700 #endif   /* ndef FBSD_VOP_GETPAGES_BUSIED */
701     }
702     ma_vm_page_unlock_queues();
703     AFS_VM_OBJECT_WUNLOCK(object);
704     return VM_PAGER_OK;
705 }
706
707 int
708 afs_vop_write(ap)
709      struct vop_write_args      /* {
710                                  * struct vnode *a_vp;
711                                  * struct uio *a_uio;
712                                  * int a_ioflag;
713                                  * struct ucred *a_cred;
714                                  * } */ *ap;
715 {
716     int code;
717     struct vcache *avc = VTOAFS(ap->a_vp);
718     AFS_GLOCK();
719     osi_FlushPages(avc, ap->a_cred);    /* hold GLOCK, but not basic vnode lock */
720     code =
721         afs_write(VTOAFS(ap->a_vp), ap->a_uio, ap->a_ioflag, ap->a_cred, 0);
722     AFS_GUNLOCK();
723     return code;
724 }
725
726 /*-
727  * struct vop_putpages_args {
728  *      struct vnode *a_vp;
729  *      vm_page_t *a_m;
730  *      int a_count;
731  *      int a_sync;
732  *      int *a_rtvals;
733  *      vm_oofset_t a_offset;
734  * };
735  */
736 /*
737  * All of the pages passed to us in ap->a_m[] are already marked as busy,
738  * so there is no additional locking required to set their flags.  -GAW
739  */
740 int
741 afs_vop_putpages(struct vop_putpages_args *ap)
742 {
743     int code;
744     int i, size, npages, sync;
745     struct uio uio;
746     struct iovec iov;
747     struct buf *bp;
748     vm_offset_t kva;
749     struct vnode *vp;
750     struct vcache *avc;
751
752     memset(&uio, 0, sizeof(uio));
753     memset(&iov, 0, sizeof(iov));
754
755     vp = ap->a_vp;
756     avc = VTOAFS(vp);
757     /* Perhaps these two checks should just be KASSERTs instead... */
758     if (vp->v_object == NULL) {
759         printf("afs_putpages: called with non-merged cache vnode??\n");
760         return VM_PAGER_ERROR;  /* XXX I think this is insufficient */
761     }
762     if (vType(avc) != VREG) {
763         printf("afs_putpages: not VREG");
764         return VM_PAGER_ERROR;  /* XXX I think this is insufficient */
765     }
766     npages = btoc(ap->a_count);
767     for (i = 0; i < npages; i++)
768         ap->a_rtvals[i] = VM_PAGER_AGAIN;
769     bp = getpbuf(&afs_pbuf_freecnt);
770
771     kva = (vm_offset_t) bp->b_data;
772     pmap_qenter(kva, ap->a_m, npages);
773     MA_PCPU_INC(cnt.v_vnodeout);
774     MA_PCPU_ADD(cnt.v_vnodepgsout, ap->a_count);
775
776     iov.iov_base = (caddr_t) kva;
777     iov.iov_len = ap->a_count;
778     uio.uio_iov = &iov;
779     uio.uio_iovcnt = 1;
780     uio.uio_offset = IDX_TO_OFF(ap->a_m[0]->pindex);
781     uio.uio_resid = ap->a_count;
782     uio.uio_segflg = UIO_SYSSPACE;
783     uio.uio_rw = UIO_WRITE;
784     uio.uio_td = curthread;
785     sync = IO_VMIO;
786     if (ap->a_sync & VM_PAGER_PUT_SYNC)
787         sync |= IO_SYNC;
788     /*if (ap->a_sync & VM_PAGER_PUT_INVAL)
789      * sync |= IO_INVAL; */
790
791     AFS_GLOCK();
792     code = afs_write(avc, &uio, sync, osi_curcred(), 0);
793     AFS_GUNLOCK();
794
795     pmap_qremove(kva, npages);
796     relpbuf(bp, &afs_pbuf_freecnt);
797
798     if (!code) {
799         size = ap->a_count - uio.uio_resid;
800         for (i = 0; i < round_page(size) / PAGE_SIZE; i++) {
801             ap->a_rtvals[i] = VM_PAGER_OK;
802             vm_page_undirty(ap->a_m[i]);
803         }
804     }
805     return ap->a_rtvals[0];
806 }
807
808 int
809 afs_vop_ioctl(ap)
810      struct vop_ioctl_args      /* {
811                                  * struct vnode *a_vp;
812                                  * u_long a_command;
813                                  * void *a_data;
814                                  * int  a_fflag;
815                                  * struct ucred *a_cred;
816                                  * struct thread *a_td;
817                                  * } */ *ap;
818 {
819     struct vcache *tvc = VTOAFS(ap->a_vp);
820     int error = 0;
821
822     /* in case we ever get in here... */
823
824     AFS_STATCNT(afs_ioctl);
825     if (((ap->a_command >> 8) & 0xff) == 'V') {
826         /* This is a VICEIOCTL call */
827         AFS_GLOCK();
828         error = HandleIoctl(tvc, ap->a_command, ap->a_data);
829         AFS_GUNLOCK();
830         return (error);
831     } else {
832         /* No-op call; just return. */
833         return (ENOTTY);
834     }
835 }
836
837 int
838 afs_vop_fsync(ap)
839      struct vop_fsync_args      /* {
840                                  * struct vnode *a_vp;
841                                  * int a_waitfor;
842                                  * struct thread *td;
843                                  * } */ *ap;
844 {
845     int error;
846     struct vnode *vp = ap->a_vp;
847
848     AFS_GLOCK();
849     /*vflushbuf(vp, wait); */
850     error = afs_fsync(VTOAFS(vp), ap->a_td->td_ucred);
851     AFS_GUNLOCK();
852     return error;
853 }
854
855 int
856 afs_vop_remove(ap)
857      struct vop_remove_args     /* {
858                                  * struct vnode *a_dvp;
859                                  * struct vnode *a_vp;
860                                  * struct componentname *a_cnp;
861                                  * } */ *ap;
862 {
863     int error = 0;
864     struct vnode *vp = ap->a_vp;
865     struct vnode *dvp = ap->a_dvp;
866
867     GETNAME();
868     AFS_GLOCK();
869     error = afs_remove(VTOAFS(dvp), name, cnp->cn_cred);
870     AFS_GUNLOCK();
871     cache_purge(vp);
872     DROPNAME();
873     return error;
874 }
875
876 int
877 afs_vop_link(ap)
878      struct vop_link_args       /* {
879                                  * struct vnode *a_vp;
880                                  * struct vnode *a_tdvp;
881                                  * struct componentname *a_cnp;
882                                  * } */ *ap;
883 {
884     int error = 0;
885     struct vnode *dvp = ap->a_tdvp;
886     struct vnode *vp = ap->a_vp;
887
888     GETNAME();
889     if (dvp->v_mount != vp->v_mount) {
890         error = EXDEV;
891         goto out;
892     }
893     if (vp->v_type == VDIR) {
894         error = EISDIR;
895         goto out;
896     }
897     if ((error = ma_vn_lock(vp, LK_CANRECURSE | LK_EXCLUSIVE, p)) != 0) {
898         goto out;
899     }
900     AFS_GLOCK();
901     error = afs_link(VTOAFS(vp), VTOAFS(dvp), name, cnp->cn_cred);
902     AFS_GUNLOCK();
903     if (dvp != vp)
904         MA_VOP_UNLOCK(vp, 0, p);
905   out:
906     DROPNAME();
907     return error;
908 }
909
910 int
911 afs_vop_rename(ap)
912      struct vop_rename_args     /* {
913                                  * struct vnode *a_fdvp;
914                                  * struct vnode *a_fvp;
915                                  * struct componentname *a_fcnp;
916                                  * struct vnode *a_tdvp;
917                                  * struct vnode *a_tvp;
918                                  * struct componentname *a_tcnp;
919                                  * } */ *ap;
920 {
921     int error = 0;
922     struct componentname *fcnp = ap->a_fcnp;
923     char *fname;
924     struct componentname *tcnp = ap->a_tcnp;
925     char *tname;
926     struct vnode *tvp = ap->a_tvp;
927     struct vnode *tdvp = ap->a_tdvp;
928     struct vnode *fvp = ap->a_fvp;
929     struct vnode *fdvp = ap->a_fdvp;
930
931     /*
932      * Check for cross-device rename.
933      */
934     if ((fvp->v_mount != tdvp->v_mount)
935         || (tvp && (fvp->v_mount != tvp->v_mount))) {
936         error = EXDEV;
937       abortit:
938         if (tdvp == tvp)
939             vrele(tdvp);
940         else
941             vput(tdvp);
942         if (tvp)
943             vput(tvp);
944         vrele(fdvp);
945         vrele(fvp);
946         return (error);
947     }
948     /*
949      * if fvp == tvp, we're just removing one name of a pair of
950      * directory entries for the same element.  convert call into rename.
951      ( (pinched from FreeBSD 4.4's ufs_rename())
952      
953      */
954     if (fvp == tvp) {
955         if (fvp->v_type == VDIR) {
956             error = EINVAL;
957             goto abortit;
958         }
959
960         /* Release destination completely. */
961         vput(tdvp);
962         vput(tvp);
963
964         /* Delete source. */
965         vrele(fdvp);
966         vrele(fvp);
967         fcnp->cn_flags &= ~MODMASK;
968         fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
969         if ((fcnp->cn_flags & SAVESTART) == 0)
970             panic("afs_rename: lost from startdir");
971         fcnp->cn_nameiop = DELETE;
972         VREF(fdvp);
973         error = relookup(fdvp, &fvp, fcnp);
974         if (error == 0)
975             vrele(fdvp);
976         if (fvp == NULL) {
977             return (ENOENT);
978         }
979
980         error = VOP_REMOVE(fdvp, fvp, fcnp);
981         if (fdvp == fvp)
982             vrele(fdvp);
983         else
984             vput(fdvp);
985         vput(fvp);
986         return (error);
987     }
988     if ((error = ma_vn_lock(fvp, LK_EXCLUSIVE, p)) != 0)
989         goto abortit;
990
991     MALLOC(fname, char *, fcnp->cn_namelen + 1, M_TEMP, M_WAITOK);
992     memcpy(fname, fcnp->cn_nameptr, fcnp->cn_namelen);
993     fname[fcnp->cn_namelen] = '\0';
994     MALLOC(tname, char *, tcnp->cn_namelen + 1, M_TEMP, M_WAITOK);
995     memcpy(tname, tcnp->cn_nameptr, tcnp->cn_namelen);
996     tname[tcnp->cn_namelen] = '\0';
997
998
999     AFS_GLOCK();
1000     /* XXX use "from" or "to" creds? NFS uses "to" creds */
1001     error =
1002         afs_rename(VTOAFS(fdvp), fname, VTOAFS(tdvp), tname, tcnp->cn_cred);
1003     AFS_GUNLOCK();
1004
1005     FREE(fname, M_TEMP);
1006     FREE(tname, M_TEMP);
1007     if (tdvp == tvp)
1008         vrele(tdvp);
1009     else
1010         vput(tdvp);
1011     if (tvp)
1012         vput(tvp);
1013     vrele(fdvp);
1014     vput(fvp);
1015     return error;
1016 }
1017
1018 int
1019 afs_vop_mkdir(ap)
1020      struct vop_mkdir_args      /* {
1021                                  * struct vnode *a_dvp;
1022                                  * struct vnode **a_vpp;
1023                                  * struct componentname *a_cnp;
1024                                  * struct vattr *a_vap;
1025                                  * } */ *ap;
1026 {
1027     struct vnode *dvp = ap->a_dvp;
1028     struct vattr *vap = ap->a_vap;
1029     int error = 0;
1030     struct vcache *vcp;
1031
1032     GETNAME();
1033 #ifdef DIAGNOSTIC
1034     if ((cnp->cn_flags & HASBUF) == 0)
1035         panic("afs_vop_mkdir: no name");
1036 #endif
1037     AFS_GLOCK();
1038     error = afs_mkdir(VTOAFS(dvp), name, vap, &vcp, cnp->cn_cred);
1039     AFS_GUNLOCK();
1040     if (error) {
1041         DROPNAME();
1042         return (error);
1043     }
1044     if (vcp) {
1045         *ap->a_vpp = AFSTOV(vcp);
1046         ma_vn_lock(AFSTOV(vcp), LK_EXCLUSIVE | LK_RETRY, p);
1047     } else
1048         *ap->a_vpp = 0;
1049     DROPNAME();
1050     return error;
1051 }
1052
1053 int
1054 afs_vop_rmdir(ap)
1055      struct vop_rmdir_args      /* {
1056                                  * struct vnode *a_dvp;
1057                                  * struct vnode *a_vp;
1058                                  * struct componentname *a_cnp;
1059                                  * } */ *ap;
1060 {
1061     int error = 0;
1062     struct vnode *dvp = ap->a_dvp;
1063
1064     GETNAME();
1065     AFS_GLOCK();
1066     error = afs_rmdir(VTOAFS(dvp), name, cnp->cn_cred);
1067     AFS_GUNLOCK();
1068     DROPNAME();
1069     return error;
1070 }
1071
1072 /* struct vop_symlink_args {
1073  *      struct vnode *a_dvp;
1074  *      struct vnode **a_vpp;
1075  *      struct componentname *a_cnp;
1076  *      struct vattr *a_vap;
1077  *      char *a_target;
1078  * };
1079  */
1080 int
1081 afs_vop_symlink(struct vop_symlink_args *ap)
1082 {
1083     struct vnode *dvp;
1084     struct vnode *newvp;
1085     struct vcache *vcp;
1086     int error;
1087
1088     GETNAME();
1089     AFS_GLOCK();
1090
1091     dvp = ap->a_dvp;
1092     newvp = NULL;
1093
1094     error =
1095         afs_symlink(VTOAFS(dvp), name, ap->a_vap, ap->a_target, NULL,
1096                     cnp->cn_cred);
1097     if (error == 0) {
1098         error = afs_lookup(VTOAFS(dvp), name, &vcp, cnp->cn_cred);
1099         if (error == 0) {
1100             newvp = AFSTOV(vcp);
1101             ma_vn_lock(newvp, LK_EXCLUSIVE | LK_RETRY, cnp->cn_thread);
1102         }
1103     }
1104     AFS_GUNLOCK();
1105     DROPNAME();
1106     *(ap->a_vpp) = newvp;
1107     return error;
1108 }
1109
1110 int
1111 afs_vop_readdir(ap)
1112      struct vop_readdir_args    /* {
1113                                  * struct vnode *a_vp;
1114                                  * struct uio *a_uio;
1115                                  * struct ucred *a_cred;
1116                                  * int *a_eofflag;
1117                                  * u_long *a_cookies;
1118                                  * int ncookies;
1119                                  * } */ *ap;
1120 {
1121     int error;
1122     off_t off;
1123 /*    printf("readdir %x cookies %x ncookies %d\n", ap->a_vp, ap->a_cookies,
1124            ap->a_ncookies); */
1125     off = ap->a_uio->uio_offset;
1126     AFS_GLOCK();
1127     error =
1128         afs_readdir(VTOAFS(ap->a_vp), ap->a_uio, ap->a_cred, ap->a_eofflag);
1129     AFS_GUNLOCK();
1130     if (!error && ap->a_ncookies != NULL) {
1131         struct uio *uio = ap->a_uio;
1132         const struct dirent *dp, *dp_start, *dp_end;
1133         int ncookies;
1134         u_long *cookies, *cookiep;
1135
1136         if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1137             panic("afs_readdir: burned cookies");
1138         dp = (const struct dirent *)
1139             ((const char *)uio->uio_iov->iov_base - (uio->uio_offset - off));
1140
1141         dp_end = (const struct dirent *)uio->uio_iov->iov_base;
1142         for (dp_start = dp, ncookies = 0; dp < dp_end;
1143              dp = (const struct dirent *)((const char *)dp + dp->d_reclen))
1144             ncookies++;
1145
1146         MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP,
1147                M_WAITOK);
1148         for (dp = dp_start, cookiep = cookies; dp < dp_end;
1149              dp = (const struct dirent *)((const char *)dp + dp->d_reclen)) {
1150             off += dp->d_reclen;
1151             *cookiep++ = off;
1152         }
1153         *ap->a_cookies = cookies;
1154         *ap->a_ncookies = ncookies;
1155     }
1156
1157     return error;
1158 }
1159
1160 int
1161 afs_vop_readlink(ap)
1162      struct vop_readlink_args   /* {
1163                                  * struct vnode *a_vp;
1164                                  * struct uio *a_uio;
1165                                  * struct ucred *a_cred;
1166                                  * } */ *ap;
1167 {
1168     int error;
1169 /*    printf("readlink %x\n", ap->a_vp);*/
1170     AFS_GLOCK();
1171     error = afs_readlink(VTOAFS(ap->a_vp), ap->a_uio, ap->a_cred);
1172     AFS_GUNLOCK();
1173     return error;
1174 }
1175
1176 int
1177 afs_vop_inactive(ap)
1178      struct vop_inactive_args   /* {
1179                                  * struct vnode *a_vp;
1180                                  * struct thread *td;
1181                                  * } */ *ap;
1182 {
1183     struct vnode *vp = ap->a_vp;
1184
1185     AFS_GLOCK();
1186     afs_InactiveVCache(VTOAFS(vp), 0);  /* decrs ref counts */
1187     AFS_GUNLOCK();
1188     return 0;
1189 }
1190
1191 /*
1192  * struct vop_reclaim_args {
1193  *      struct vnode *a_vp;
1194  * };
1195  */
1196 int
1197 afs_vop_reclaim(struct vop_reclaim_args *ap)
1198 {
1199     /* copied from ../OBSD/osi_vnodeops.c:afs_nbsd_reclaim() */
1200     int code, slept;
1201     struct vnode *vp = ap->a_vp;
1202     struct vcache *avc = VTOAFS(vp);
1203     int haveGlock = ISAFS_GLOCK();
1204     int haveVlock = CheckLock(&afs_xvcache);
1205
1206     if (!haveGlock)
1207         AFS_GLOCK();
1208     if (!haveVlock)
1209         ObtainWriteLock(&afs_xvcache, 901);
1210     /* reclaim the vnode and the in-memory vcache, but keep the on-disk vcache */
1211     code = afs_FlushVCache(avc, &slept);
1212
1213     if (avc->f.states & CVInit) {
1214         avc->f.states &= ~CVInit;
1215         afs_osi_Wakeup(&avc->f.states);
1216     }
1217
1218     if (!haveVlock)
1219         ReleaseWriteLock(&afs_xvcache);
1220     if (!haveGlock)
1221         AFS_GUNLOCK();
1222
1223     if (code) {
1224         afs_warn("afs_vop_reclaim: afs_FlushVCache failed code %d vnode\n", code);
1225         VOP_PRINT(vp);
1226     }
1227
1228     /* basically, it must not fail */
1229     vnode_destroy_vobject(vp);
1230     vp->v_data = 0;
1231
1232     return 0;
1233 }
1234
1235 int
1236 afs_vop_strategy(ap)
1237      struct vop_strategy_args   /* {
1238                                  * struct buf *a_bp;
1239                                  * } */ *ap;
1240 {
1241     int error;
1242     AFS_GLOCK();
1243     error = afs_ustrategy(ap->a_bp, osi_curcred());
1244     AFS_GUNLOCK();
1245     return error;
1246 }
1247
1248 int
1249 afs_vop_print(ap)
1250      struct vop_print_args      /* {
1251                                  * struct vnode *a_vp;
1252                                  * } */ *ap;
1253 {
1254     struct vnode *vp = ap->a_vp;
1255     struct vcache *vc = VTOAFS(ap->a_vp);
1256     int s = vc->f.states;
1257
1258     printf("vc %p vp %p tag %s, fid: %d.%d.%d.%d, opens %d, writers %d", vc, vp, vp->v_tag,
1259            (int)vc->f.fid.Cell, (u_int) vc->f.fid.Fid.Volume,
1260            (u_int) vc->f.fid.Fid.Vnode, (u_int) vc->f.fid.Fid.Unique, vc->opens,
1261            vc->execsOrWriters);
1262     printf("\n  states%s%s%s%s%s", (s & CStatd) ? " statd" : "",
1263            (s & CRO) ? " readonly" : "", (s & CDirty) ? " dirty" : "",
1264            (s & CMAPPED) ? " mapped" : "",
1265            (s & CVFlushed) ? " flush in progress" : "");
1266     printf("\n");
1267     return 0;
1268 }
1269
1270 /*
1271  * Advisory record locking support (fcntl() POSIX style)
1272  */
1273 int
1274 afs_vop_advlock(ap)
1275      struct vop_advlock_args    /* {
1276                                  * struct vnode *a_vp;
1277                                  * caddr_t  a_id;
1278                                  * int  a_op;
1279                                  * struct flock *a_fl;
1280                                  * int  a_flags;
1281                                  * } */ *ap;
1282 {
1283     int error, a_op;
1284     struct ucred cr = *osi_curcred();
1285
1286     a_op = ap->a_op;
1287     if (a_op == F_UNLCK) {
1288         /*
1289          * When a_fl->type is F_UNLCK, FreeBSD passes in an a_op of F_UNLCK.
1290          * This is (confusingly) different than how you actually release a lock
1291          * with fcntl(), which is done with an a_op of F_SETLK and an l_type of
1292          * F_UNLCK. Pretend we were given an a_op of F_SETLK in this case,
1293          * since this is what afs_lockctl expects.
1294          */
1295         a_op = F_SETLK;
1296     }
1297
1298     AFS_GLOCK();
1299     error =
1300         afs_lockctl(VTOAFS(ap->a_vp),
1301                 ap->a_fl,
1302                 a_op, &cr,
1303                 (int)(intptr_t)ap->a_id);       /* XXX: no longer unique! */
1304     AFS_GUNLOCK();
1305     return error;
1306 }