300e61052d74619b13b4fe8d862f036cf69a3a1d
[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 RCSID
51     ("$Header$");
52
53 #include <afs/sysincludes.h>    /* Standard vendor system headers */
54 #include <afsincludes.h>        /* Afs-based standard headers */
55 #include <afs/afs_stats.h>      /* statistics */
56 #include <sys/malloc.h>
57 #include <sys/namei.h>
58 #include <sys/unistd.h>
59 #ifndef AFS_FBSD50_ENV
60 #include <vm/vm_zone.h>
61 #endif
62 #include <vm/vm_page.h>
63 #include <vm/vm_object.h>
64 #include <vm/vm_pager.h>
65 #include <vm/vnode_pager.h>
66 extern int afs_pbuf_freecnt;
67
68 #ifdef AFS_FBSD60_ENV
69 static vop_access_t     afs_vop_access;
70 static vop_advlock_t    afs_vop_advlock;
71 static vop_close_t      afs_vop_close;
72 static vop_create_t     afs_vop_create;
73 static vop_fsync_t      afs_vop_fsync;
74 static vop_getattr_t    afs_vop_getattr;
75 static vop_getpages_t   afs_vop_getpages;
76 static vop_inactive_t   afs_vop_inactive;
77 static vop_ioctl_t      afs_vop_ioctl;
78 static vop_link_t       afs_vop_link;
79 static vop_lookup_t     afs_vop_lookup;
80 static vop_mkdir_t      afs_vop_mkdir;
81 static vop_mknod_t      afs_vop_mknod;
82 static vop_open_t       afs_vop_open;
83 static vop_pathconf_t   afs_vop_pathconf;
84 static vop_poll_t       afs_vop_poll;
85 static vop_print_t      afs_vop_print;
86 static vop_putpages_t   afs_vop_putpages;
87 static vop_read_t       afs_vop_read;
88 static vop_readdir_t    afs_vop_readdir;
89 static vop_readlink_t   afs_vop_readlink;
90 static vop_reclaim_t    afs_vop_reclaim;
91 static vop_remove_t     afs_vop_remove;
92 static vop_rename_t     afs_vop_rename;
93 static vop_rmdir_t      afs_vop_rmdir;
94 static vop_setattr_t    afs_vop_setattr;
95 static vop_strategy_t   afs_vop_strategy;
96 static vop_symlink_t    afs_vop_symlink;
97 static vop_write_t      afs_vop_write;
98
99 struct vop_vector afs_vnodeops = {
100         .vop_default =          &default_vnodeops,
101         .vop_access =           afs_vop_access,
102         .vop_advlock =          afs_vop_advlock,
103         .vop_close =            afs_vop_close,
104         .vop_create =           afs_vop_create,
105         .vop_fsync =            afs_vop_fsync,
106         .vop_getattr =          afs_vop_getattr,
107         .vop_getpages =         afs_vop_getpages,
108         .vop_inactive =         afs_vop_inactive,
109         .vop_ioctl =            afs_vop_ioctl,
110         .vop_lease =            VOP_NULL,
111         .vop_link =             afs_vop_link,
112         .vop_lookup =           afs_vop_lookup,
113         .vop_mkdir =            afs_vop_mkdir,
114         .vop_mknod =            afs_vop_mknod,
115         .vop_open =             afs_vop_open,
116         .vop_pathconf =         afs_vop_pathconf,
117         .vop_poll =             afs_vop_poll,
118         .vop_print =            afs_vop_print,
119         .vop_putpages =         afs_vop_putpages,
120         .vop_read =             afs_vop_read,
121         .vop_readdir =          afs_vop_readdir,
122         .vop_readlink =         afs_vop_readlink,
123         .vop_reclaim =          afs_vop_reclaim,
124         .vop_remove =           afs_vop_remove,
125         .vop_rename =           afs_vop_rename,
126         .vop_rmdir =            afs_vop_rmdir,
127         .vop_setattr =          afs_vop_setattr,
128         .vop_strategy =         afs_vop_strategy,
129         .vop_symlink =          afs_vop_symlink,
130         .vop_write =            afs_vop_write,
131 };
132
133 #else /* AFS_FBSD60_ENV */
134
135 int afs_vop_lookup(struct vop_lookup_args *);
136 int afs_vop_create(struct vop_create_args *);
137 int afs_vop_mknod(struct vop_mknod_args *);
138 int afs_vop_open(struct vop_open_args *);
139 int afs_vop_close(struct vop_close_args *);
140 int afs_vop_access(struct vop_access_args *);
141 int afs_vop_getattr(struct vop_getattr_args *);
142 int afs_vop_setattr(struct vop_setattr_args *);
143 int afs_vop_read(struct vop_read_args *);
144 int afs_vop_write(struct vop_write_args *);
145 int afs_vop_getpages(struct vop_getpages_args *);
146 int afs_vop_putpages(struct vop_putpages_args *);
147 int afs_vop_ioctl(struct vop_ioctl_args *);
148 static int afs_vop_pathconf(struct vop_pathconf_args *);
149 int afs_vop_poll(struct vop_poll_args *);
150 #ifndef AFS_FBSD50_ENV
151 int afs_vop_mmap(struct vop_mmap_args *);
152 #endif
153 int afs_vop_fsync(struct vop_fsync_args *);
154 int afs_vop_remove(struct vop_remove_args *);
155 int afs_vop_link(struct vop_link_args *);
156 int afs_vop_rename(struct vop_rename_args *);
157 int afs_vop_mkdir(struct vop_mkdir_args *);
158 int afs_vop_rmdir(struct vop_rmdir_args *);
159 int afs_vop_symlink(struct vop_symlink_args *);
160 int afs_vop_readdir(struct vop_readdir_args *);
161 int afs_vop_readlink(struct vop_readlink_args *);
162 int afs_vop_inactive(struct vop_inactive_args *);
163 int afs_vop_reclaim(struct vop_reclaim_args *);
164 int afs_vop_bmap(struct vop_bmap_args *);
165 int afs_vop_strategy(struct vop_strategy_args *);
166 int afs_vop_print(struct vop_print_args *);
167 int afs_vop_advlock(struct vop_advlock_args *);
168
169
170
171 /* Global vfs data structures for AFS. */
172 vop_t **afs_vnodeop_p;
173 struct vnodeopv_entry_desc afs_vnodeop_entries[] = {
174     {&vop_default_desc, (vop_t *) vop_defaultop},
175     {&vop_access_desc, (vop_t *) afs_vop_access},       /* access */
176     {&vop_advlock_desc, (vop_t *) afs_vop_advlock},     /* advlock */
177     {&vop_bmap_desc, (vop_t *) afs_vop_bmap},   /* bmap */
178 #ifndef AFS_FBSD50_ENV
179     {&vop_bwrite_desc, (vop_t *) vop_stdbwrite},
180 #endif
181     {&vop_close_desc, (vop_t *) afs_vop_close}, /* close */
182     {&vop_createvobject_desc, (vop_t *) vop_stdcreatevobject},
183     {&vop_destroyvobject_desc, (vop_t *) vop_stddestroyvobject},
184     {&vop_create_desc, (vop_t *) afs_vop_create},       /* create */
185     {&vop_fsync_desc, (vop_t *) afs_vop_fsync}, /* fsync */
186     {&vop_getattr_desc, (vop_t *) afs_vop_getattr},     /* getattr */
187     {&vop_getpages_desc, (vop_t *) afs_vop_getpages},   /* read */
188     {&vop_getvobject_desc, (vop_t *) vop_stdgetvobject},
189     {&vop_putpages_desc, (vop_t *) afs_vop_putpages},   /* write */
190     {&vop_inactive_desc, (vop_t *) afs_vop_inactive},   /* inactive */
191     {&vop_lease_desc, (vop_t *) vop_null},
192     {&vop_link_desc, (vop_t *) afs_vop_link},   /* link */
193     {&vop_lookup_desc, (vop_t *) afs_vop_lookup},       /* lookup */
194     {&vop_mkdir_desc, (vop_t *) afs_vop_mkdir}, /* mkdir */
195     {&vop_mknod_desc, (vop_t *) afs_vop_mknod}, /* mknod */
196 #ifndef AFS_FBSD50_ENV
197     {&vop_mmap_desc, (vop_t *) afs_vop_mmap},   /* mmap */
198 #endif
199     {&vop_open_desc, (vop_t *) afs_vop_open},   /* open */
200     {&vop_pathconf_desc, (vop_t *) afs_vop_pathconf},   /* pathconf */
201     {&vop_poll_desc, (vop_t *) afs_vop_poll},   /* select */
202     {&vop_print_desc, (vop_t *) afs_vop_print}, /* print */
203     {&vop_read_desc, (vop_t *) afs_vop_read},   /* read */
204     {&vop_readdir_desc, (vop_t *) afs_vop_readdir},     /* readdir */
205     {&vop_readlink_desc, (vop_t *) afs_vop_readlink},   /* readlink */
206     {&vop_reclaim_desc, (vop_t *) afs_vop_reclaim},     /* reclaim */
207     {&vop_remove_desc, (vop_t *) afs_vop_remove},       /* remove */
208     {&vop_rename_desc, (vop_t *) afs_vop_rename},       /* rename */
209     {&vop_rmdir_desc, (vop_t *) afs_vop_rmdir}, /* rmdir */
210     {&vop_setattr_desc, (vop_t *) afs_vop_setattr},     /* setattr */
211     {&vop_strategy_desc, (vop_t *) afs_vop_strategy},   /* strategy */
212     {&vop_symlink_desc, (vop_t *) afs_vop_symlink},     /* symlink */
213     {&vop_write_desc, (vop_t *) afs_vop_write}, /* write */
214     {&vop_ioctl_desc, (vop_t *) afs_vop_ioctl}, /* XXX ioctl */
215     /*{ &vop_seek_desc, afs_vop_seek }, *//* seek */
216     {NULL, NULL}
217 };
218 struct vnodeopv_desc afs_vnodeop_opv_desc =
219     { &afs_vnodeop_p, afs_vnodeop_entries };
220 #endif /* AFS_FBSD60_ENV */
221
222 #define GETNAME()       \
223     struct componentname *cnp = ap->a_cnp; \
224     char *name; \
225     MALLOC(name, char *, cnp->cn_namelen+1, M_TEMP, M_WAITOK); \
226     memcpy(name, cnp->cn_nameptr, cnp->cn_namelen); \
227     name[cnp->cn_namelen] = '\0'
228
229 #define DROPNAME() FREE(name, M_TEMP)
230
231 /* This is a bit of a cheat... */
232 #ifdef AFS_FBSD50_ENV
233 #define a_p a_td
234 #endif
235
236 /*
237  * Mosty copied from sys/ufs/ufs/ufs_vnops.c:ufs_pathconf().
238  * We should know the correct answers to these questions with
239  * respect to the AFS protocol (which may differ from the UFS
240  * values) but for the moment this will do.
241  */
242 static int
243 afs_vop_pathconf(struct vop_pathconf_args *ap)
244 {
245         int error;
246
247         error = 0;
248         switch (ap->a_name) {
249         case _PC_LINK_MAX:
250                 *ap->a_retval = LINK_MAX;
251                 break;
252         case _PC_NAME_MAX:
253                 *ap->a_retval = NAME_MAX;
254                 break;
255         case _PC_PATH_MAX:
256                 *ap->a_retval = PATH_MAX;
257                 break;
258         case _PC_PIPE_BUF:
259                 *ap->a_retval = PIPE_BUF;
260                 break;
261         case _PC_CHOWN_RESTRICTED:
262                 *ap->a_retval = 1;
263                 break;
264         case _PC_NO_TRUNC:
265                 *ap->a_retval = 1;
266                 break;
267 #ifdef _PC_ACL_EXTENDED
268         case _PC_ACL_EXTENDED:
269                 *ap->a_retval = 0;
270                 break;
271         case _PC_ACL_PATH_MAX:
272                 *ap->a_retval = 3;
273                 break;
274 #endif
275 #ifdef _PC_MAC_PRESENT
276         case _PC_MAC_PRESENT:
277                 *ap->a_retval = 0;
278                 break;
279 #endif
280 #ifdef _PC_ASYNC_IO
281         case _PC_ASYNC_IO:
282                 /* _PC_ASYNC_IO should have been handled by upper layers. */
283                 KASSERT(0, ("_PC_ASYNC_IO should not get here"));
284                 error = EINVAL;
285                 break;
286         case _PC_PRIO_IO:
287                 *ap->a_retval = 0;
288                 break;
289         case _PC_SYNC_IO:
290                 *ap->a_retval = 0;
291                 break;
292 #endif
293 #ifdef _PC_ALLOC_SIZE_MIN
294         case _PC_ALLOC_SIZE_MIN:
295                 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_bsize;
296                 break;
297 #endif
298 #ifdef _PC_FILESIZEBITS
299         case _PC_FILESIZEBITS:
300                 *ap->a_retval = 32; /* XXX */
301                 break;
302 #endif
303 #ifdef _PC_REC_INCR_XFER_SIZE
304         case _PC_REC_INCR_XFER_SIZE:
305                 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
306                 break;
307         case _PC_REC_MAX_XFER_SIZE:
308                 *ap->a_retval = -1; /* means ``unlimited'' */
309                 break;
310         case _PC_REC_MIN_XFER_SIZE:
311                 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
312                 break;
313         case _PC_REC_XFER_ALIGN:
314                 *ap->a_retval = PAGE_SIZE;
315                 break;
316 #endif
317 #ifdef _PC_SYMLINK_MAX
318         case _PC_SYMLINK_MAX:
319                 *ap->a_retval = MAXPATHLEN;
320                 break;
321 #endif
322         default:
323                 error = EINVAL;
324                 break;
325         }
326         return (error);
327 }
328
329 int
330 afs_vop_lookup(ap)
331      struct vop_lookup_args     /* {
332                                  * struct vnodeop_desc * a_desc;
333                                  * struct vnode *a_dvp;
334                                  * struct vnode **a_vpp;
335                                  * struct componentname *a_cnp;
336                                  * } */ *ap;
337 {
338     int error;
339     struct vcache *vcp;
340     struct vnode *vp, *dvp;
341     register int flags = ap->a_cnp->cn_flags;
342     int lockparent;             /* 1 => lockparent flag is set */
343     int wantparent;             /* 1 => wantparent or lockparent flag */
344 #ifdef AFS_FBSD50_ENV
345     struct thread *p = ap->a_cnp->cn_thread;
346 #else
347     struct proc *p = ap->a_cnp->cn_proc;
348 #endif
349     GETNAME();
350
351     lockparent = flags & LOCKPARENT;
352     wantparent = flags & (LOCKPARENT | WANTPARENT);
353
354     if (ap->a_dvp->v_type != VDIR) {
355         *ap->a_vpp = 0;
356         DROPNAME();
357         return ENOTDIR;
358     }
359     dvp = ap->a_dvp;
360     if (flags & ISDOTDOT)
361         VOP_UNLOCK(dvp, 0, p);
362     AFS_GLOCK();
363     error = afs_lookup(VTOAFS(dvp), name, &vcp, cnp->cn_cred);
364     AFS_GUNLOCK();
365     if (error) {
366         if (flags & ISDOTDOT)
367             VOP_LOCK(dvp, LK_EXCLUSIVE | LK_RETRY, p);
368         if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME)
369             && (flags & ISLASTCN) && error == ENOENT)
370             error = EJUSTRETURN;
371         if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
372             cnp->cn_flags |= SAVENAME;
373         DROPNAME();
374         *ap->a_vpp = 0;
375         return (error);
376     }
377     vp = AFSTOV(vcp);           /* always get a node if no error */
378
379     /* The parent directory comes in locked.  We unlock it on return
380      * unless the caller wants it left locked.
381      * we also always return the vnode locked. */
382
383     if (flags & ISDOTDOT) {
384         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
385         /* always return the child locked */
386         if (lockparent && (flags & ISLASTCN)
387             && (error = vn_lock(dvp, LK_EXCLUSIVE, p))) {
388             vput(vp);
389             DROPNAME();
390             return (error);
391         }
392     } else if (vp == dvp) {
393         /* they're the same; afs_lookup() already ref'ed the leaf.
394          * It came in locked, so we don't need to ref OR lock it */
395     } else {
396         if (!lockparent || !(flags & ISLASTCN))
397             VOP_UNLOCK(dvp, 0, p);      /* done with parent. */
398         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
399         /* always return the child locked */
400     }
401     *ap->a_vpp = vp;
402
403     if ((cnp->cn_nameiop == RENAME && wantparent && (flags & ISLASTCN))
404         || (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN)))
405         cnp->cn_flags |= SAVENAME;
406
407     DROPNAME();
408     return error;
409 }
410
411 int
412 afs_vop_create(ap)
413      struct vop_create_args     /* {
414                                  * struct vnode *a_dvp;
415                                  * struct vnode **a_vpp;
416                                  * struct componentname *a_cnp;
417                                  * struct vattr *a_vap;
418                                  * } */ *ap;
419 {
420     int error = 0;
421     struct vcache *vcp;
422     register struct vnode *dvp = ap->a_dvp;
423 #ifdef AFS_FBSD50_ENV
424     struct thread *p = ap->a_cnp->cn_thread;
425 #else
426     struct proc *p = ap->a_cnp->cn_proc;
427 #endif
428     GETNAME();
429
430     AFS_GLOCK();
431     error =
432         afs_create(VTOAFS(dvp), name, ap->a_vap,
433                    ap->a_vap->va_vaflags & VA_EXCLUSIVE ? EXCL : NONEXCL,
434                    ap->a_vap->va_mode, &vcp, cnp->cn_cred);
435     AFS_GUNLOCK();
436     if (error) {
437         DROPNAME();
438         return (error);
439     }
440
441     if (vcp) {
442         *ap->a_vpp = AFSTOV(vcp);
443         vn_lock(AFSTOV(vcp), LK_EXCLUSIVE | LK_RETRY, p);
444     } else
445         *ap->a_vpp = 0;
446
447     DROPNAME();
448     return error;
449 }
450
451 int
452 afs_vop_mknod(ap)
453      struct vop_mknod_args      /* {
454                                  * struct vnode *a_dvp;
455                                  * struct vnode **a_vpp;
456                                  * struct componentname *a_cnp;
457                                  * struct vattr *a_vap;
458                                  * } */ *ap;
459 {
460     return (ENODEV);
461 }
462
463 #if 0
464 static int
465 validate_vops(struct vnode *vp, int after)
466 {
467     int ret = 0;
468     struct vnodeopv_entry_desc *this;
469     for (this = afs_vnodeop_entries; this->opve_op; this++) {
470         if (vp->v_op[this->opve_op->vdesc_offset] != this->opve_impl) {
471             if (!ret) {
472                 printf("v_op %d ", after);
473                 vprint("check", vp);
474             }
475             ret = 1;
476             printf("For oper %d (%s), func is %p, not %p",
477                    this->opve_op->vdesc_offset, this->opve_op->vdesc_name,
478                    vp->v_op[this->opve_op->vdesc_offset], this->opve_impl);
479         }
480     }
481     return ret;
482 }
483 #endif
484 int
485 afs_vop_open(ap)
486      struct vop_open_args       /* {
487                                  * struct vnode *a_vp;
488                                  * int  a_mode;
489                                  * struct ucred *a_cred;
490                                  * struct proc *a_p;
491                                  * } */ *ap;
492 {
493     int error;
494     int bad;
495     struct vcache *vc = VTOAFS(ap->a_vp);
496     bad = 0;
497     AFS_GLOCK();
498     error = afs_open(&vc, ap->a_mode, ap->a_cred);
499 #ifdef DIAGNOSTIC
500     if (AFSTOV(vc) != ap->a_vp)
501         panic("AFS open changed vnode!");
502 #endif
503     osi_FlushPages(vc, ap->a_cred);
504     AFS_GUNLOCK();
505     return error;
506 }
507
508 int
509 afs_vop_close(ap)
510      struct vop_close_args      /* {
511                                  * struct vnode *a_vp;
512                                  * int  a_fflag;
513                                  * struct ucred *a_cred;
514                                  * struct proc *a_p;
515                                  * } */ *ap;
516 {
517     int code;
518     struct vcache *avc = VTOAFS(ap->a_vp);
519     AFS_GLOCK();
520     if (ap->a_cred)
521         code = afs_close(avc, ap->a_fflag, ap->a_cred);
522     else
523         code = afs_close(avc, ap->a_fflag, afs_osi_credp);
524     osi_FlushPages(avc, ap->a_cred);    /* hold bozon lock, but not basic vnode lock */
525     AFS_GUNLOCK();
526     return code;
527 }
528
529 int
530 afs_vop_access(ap)
531      struct vop_access_args     /* {
532                                  * struct vnode *a_vp;
533                                  * int  a_mode;
534                                  * struct ucred *a_cred;
535                                  * struct proc *a_p;
536                                  * } */ *ap;
537 {
538     int code;
539     AFS_GLOCK();
540     code = afs_access(VTOAFS(ap->a_vp), ap->a_mode, ap->a_cred);
541     AFS_GUNLOCK();
542     return code;
543 }
544
545 int
546 afs_vop_getattr(ap)
547      struct vop_getattr_args    /* {
548                                  * struct vnode *a_vp;
549                                  * struct vattr *a_vap;
550                                  * struct ucred *a_cred;
551                                  * struct proc *a_p;
552                                  * } */ *ap;
553 {
554     int code;
555     AFS_GLOCK();
556     code = afs_getattr(VTOAFS(ap->a_vp), ap->a_vap, ap->a_cred);
557     AFS_GUNLOCK();
558     return code;
559 }
560
561 int
562 afs_vop_setattr(ap)
563      struct vop_setattr_args    /* {
564                                  * struct vnode *a_vp;
565                                  * struct vattr *a_vap;
566                                  * struct ucred *a_cred;
567                                  * struct proc *a_p;
568                                  * } */ *ap;
569 {
570     int code;
571     AFS_GLOCK();
572     code = afs_setattr(VTOAFS(ap->a_vp), ap->a_vap, ap->a_cred);
573     AFS_GUNLOCK();
574     return code;
575 }
576
577 int
578 afs_vop_read(ap)
579      struct vop_read_args       /* {
580                                  * struct vnode *a_vp;
581                                  * struct uio *a_uio;
582                                  * int a_ioflag;
583                                  * struct ucred *a_cred;
584                                  * 
585                                  * } */ *ap;
586 {
587     int code;
588     struct vcache *avc = VTOAFS(ap->a_vp);
589     AFS_GLOCK();
590     osi_FlushPages(avc, ap->a_cred);    /* hold bozon lock, but not basic vnode lock */
591     code = afs_read(avc, ap->a_uio, ap->a_cred, 0, 0, 0);
592     AFS_GUNLOCK();
593     return code;
594 }
595
596 /* struct vop_getpages_args {
597  *      struct vnode *a_vp;
598  *      vm_page_t *a_m;
599  *      int a_count;
600  *      int a_reqpage;
601  *      vm_oofset_t a_offset;
602  * };
603  */
604 int
605 afs_vop_getpages(struct vop_getpages_args *ap)
606 {
607     int code;
608     int i, nextoff, size, toff, npages;
609     struct uio uio;
610     struct iovec iov;
611     struct buf *bp;
612     vm_offset_t kva;
613     vm_object_t object;
614     struct vnode *vp;
615     struct vcache *avc;
616
617 #ifdef AFS_FBSD50_ENV
618     GIANT_REQUIRED;
619 #endif
620     vp = ap->a_vp;
621     avc = VTOAFS(vp);
622     if ((object = vp->v_object) == NULL) {
623         printf("afs_getpages: called with non-merged cache vnode??\n");
624         return VM_PAGER_ERROR;
625     }
626     npages = btoc(ap->a_count);
627     /*
628      * If the requested page is partially valid, just return it and
629      * allow the pager to zero-out the blanks.  Partially valid pages
630      * can only occur at the file EOF.
631      */
632
633     {
634         vm_page_t m = ap->a_m[ap->a_reqpage];
635
636 #ifdef AFS_FBSD50_ENV
637         VM_OBJECT_LOCK(object);
638         vm_page_lock_queues();
639 #endif
640         if (m->valid != 0) {
641             /* handled by vm_fault now        */
642             /* vm_page_zero_invalid(m, TRUE); */
643             for (i = 0; i < npages; ++i) {
644                 if (i != ap->a_reqpage)
645                     vm_page_free(ap->a_m[i]);
646             }
647 #ifdef AFS_FBSD50_ENV
648             vm_page_unlock_queues();
649             VM_OBJECT_UNLOCK(object);
650 #endif
651             return (0);
652         }
653 #ifdef AFS_FBSD50_ENV
654         vm_page_unlock_queues();
655         VM_OBJECT_UNLOCK(object);
656 #endif
657     }
658     bp = getpbuf(&afs_pbuf_freecnt);
659
660     kva = (vm_offset_t) bp->b_data;
661     pmap_qenter(kva, ap->a_m, npages);
662 #ifdef AFS_FBSD50_ENV
663     cnt.v_vnodein++;
664     cnt.v_vnodepgsin += npages;
665 #endif
666
667     iov.iov_base = (caddr_t) kva;
668     iov.iov_len = ap->a_count;
669     uio.uio_iov = &iov;
670     uio.uio_iovcnt = 1;
671     uio.uio_offset = IDX_TO_OFF(ap->a_m[0]->pindex);
672     uio.uio_resid = ap->a_count;
673     uio.uio_segflg = UIO_SYSSPACE;
674     uio.uio_rw = UIO_READ;
675 #ifdef AFS_FBSD50_ENV
676     uio.uio_td = curthread;
677 #else
678     uio.uio_procp = curproc;
679 #endif
680
681     AFS_GLOCK();
682     osi_FlushPages(avc, osi_curcred()); /* hold bozon lock, but not basic vnode lock */
683     code = afs_read(avc, &uio, osi_curcred(), 0, 0, 0);
684     AFS_GUNLOCK();
685     pmap_qremove(kva, npages);
686
687     relpbuf(bp, &afs_pbuf_freecnt);
688
689     if (code && (uio.uio_resid == ap->a_count)) {
690 #ifdef AFS_FBSD50_ENV
691         VM_OBJECT_LOCK(object);
692         vm_page_lock_queues();
693 #endif
694         for (i = 0; i < npages; ++i) {
695             if (i != ap->a_reqpage)
696                 vm_page_free(ap->a_m[i]);
697         }
698 #ifdef AFS_FBSD50_ENV
699         vm_page_unlock_queues();
700         VM_OBJECT_UNLOCK(object);
701 #endif
702         return VM_PAGER_ERROR;
703     }
704
705     size = ap->a_count - uio.uio_resid;
706 #ifdef AFS_FBSD50_ENV
707     VM_OBJECT_LOCK(object);
708     vm_page_lock_queues();
709 #endif
710     for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
711         vm_page_t m;
712         nextoff = toff + PAGE_SIZE;
713         m = ap->a_m[i];
714
715         m->flags &= ~PG_ZERO;
716
717         if (nextoff <= size) {
718             /*
719              * Read operation filled an entire page
720              */
721             m->valid = VM_PAGE_BITS_ALL;
722             vm_page_undirty(m);
723         } else if (size > toff) {
724             /*
725              * Read operation filled a partial page.
726              */
727             m->valid = 0;
728             vm_page_set_validclean(m, 0, size - toff);
729             /* handled by vm_fault now        */
730             /* vm_page_zero_invalid(m, TRUE); */
731         }
732
733         if (i != ap->a_reqpage) {
734             /*
735              * Whether or not to leave the page activated is up in
736              * the air, but we should put the page on a page queue
737              * somewhere (it already is in the object).  Result:
738              * It appears that emperical results show that
739              * deactivating pages is best.
740              */
741
742             /*
743              * Just in case someone was asking for this page we
744              * now tell them that it is ok to use.
745              */
746             if (!code) {
747                 if (m->flags & PG_WANTED)
748                     vm_page_activate(m);
749                 else
750                     vm_page_deactivate(m);
751                 vm_page_wakeup(m);
752             } else {
753                 vm_page_free(m);
754             }
755         }
756     }
757 #ifdef AFS_FBSD50_ENV
758     vm_page_unlock_queues();
759     VM_OBJECT_UNLOCK(object);
760 #endif
761     return 0;
762 }
763
764 int
765 afs_vop_write(ap)
766      struct vop_write_args      /* {
767                                  * struct vnode *a_vp;
768                                  * struct uio *a_uio;
769                                  * int a_ioflag;
770                                  * struct ucred *a_cred;
771                                  * } */ *ap;
772 {
773     int code;
774     struct vcache *avc = VTOAFS(ap->a_vp);
775     AFS_GLOCK();
776     osi_FlushPages(avc, ap->a_cred);    /* hold bozon lock, but not basic vnode lock */
777     code =
778         afs_write(VTOAFS(ap->a_vp), ap->a_uio, ap->a_ioflag, ap->a_cred, 0);
779     AFS_GUNLOCK();
780     return code;
781 }
782
783 /*-
784  * struct vop_putpages_args {
785  *      struct vnode *a_vp;
786  *      vm_page_t *a_m;
787  *      int a_count;
788  *      int a_sync;
789  *      int *a_rtvals;
790  *      vm_oofset_t a_offset;
791  * };
792  */
793 /*
794  * All of the pages passed to us in ap->a_m[] are already marked as busy,
795  * so there is no additional locking required to set their flags.  -GAW
796  */
797 int
798 afs_vop_putpages(struct vop_putpages_args *ap)
799 {
800     int code;
801     int i, size, npages, sync;
802     struct uio uio;
803     struct iovec iov;
804     struct buf *bp;
805     vm_offset_t kva;
806     struct vnode *vp;
807     struct vcache *avc;
808
809 #ifdef AFS_FBSD50_ENV
810     GIANT_REQUIRED;
811 #endif
812
813     vp = ap->a_vp;
814     avc = VTOAFS(vp);
815     /* Perhaps these two checks should just be KASSERTs instead... */
816     if (vp->v_object == NULL) {
817         printf("afs_putpages: called with non-merged cache vnode??\n");
818         return VM_PAGER_ERROR;  /* XXX I think this is insufficient */
819     }
820     if (vType(avc) != VREG) {
821         printf("afs_putpages: not VREG");
822         return VM_PAGER_ERROR;  /* XXX I think this is insufficient */
823     }
824     npages = btoc(ap->a_count);
825     for (i = 0; i < npages; i++)
826         ap->a_rtvals[i] = VM_PAGER_AGAIN;
827     bp = getpbuf(&afs_pbuf_freecnt);
828
829     kva = (vm_offset_t) bp->b_data;
830     pmap_qenter(kva, ap->a_m, npages);
831 #ifdef AFS_FBSD50_ENV
832     cnt.v_vnodeout++;
833     cnt.v_vnodepgsout += ap->a_count;
834 #endif
835
836     iov.iov_base = (caddr_t) kva;
837     iov.iov_len = ap->a_count;
838     uio.uio_iov = &iov;
839     uio.uio_iovcnt = 1;
840     uio.uio_offset = IDX_TO_OFF(ap->a_m[0]->pindex);
841     uio.uio_resid = ap->a_count;
842     uio.uio_segflg = UIO_SYSSPACE;
843     uio.uio_rw = UIO_WRITE;
844 #ifdef AFS_FBSD50_ENV
845     uio.uio_td = curthread;
846 #else
847     uio.uio_procp = curproc;
848 #endif
849     sync = IO_VMIO;
850     if (ap->a_sync & VM_PAGER_PUT_SYNC)
851         sync |= IO_SYNC;
852     /*if (ap->a_sync & VM_PAGER_PUT_INVAL)
853      * sync |= IO_INVAL; */
854
855     AFS_GLOCK();
856     code = afs_write(avc, &uio, sync, osi_curcred(), 0);
857     AFS_GUNLOCK();
858
859     pmap_qremove(kva, npages);
860     relpbuf(bp, &afs_pbuf_freecnt);
861
862     if (!code) {
863         size = ap->a_count - uio.uio_resid;
864         for (i = 0; i < round_page(size) / PAGE_SIZE; i++) {
865             ap->a_rtvals[i] = VM_PAGER_OK;
866             vm_page_undirty(ap->a_m[i]);
867         }
868     }
869     return ap->a_rtvals[0];
870 }
871
872 int
873 afs_vop_ioctl(ap)
874      struct vop_ioctl_args      /* {
875                                  * struct vnode *a_vp;
876                                  * int  a_command;
877                                  * caddr_t  a_data;
878                                  * int  a_fflag;
879                                  * struct ucred *a_cred;
880                                  * struct proc *a_p;
881                                  * } */ *ap;
882 {
883     struct vcache *tvc = VTOAFS(ap->a_vp);
884     int error = 0;
885
886     /* in case we ever get in here... */
887
888     AFS_STATCNT(afs_ioctl);
889     if (((ap->a_command >> 8) & 0xff) == 'V') {
890         /* This is a VICEIOCTL call */
891         AFS_GLOCK();
892         error = HandleIoctl(tvc, NULL /*Not used */ ,
893                             ap->a_command, ap->a_data);
894         AFS_GUNLOCK();
895         return (error);
896     } else {
897         /* No-op call; just return. */
898         return (ENOTTY);
899     }
900 }
901
902 /* ARGSUSED */
903 int
904 afs_vop_poll(ap)
905      struct vop_poll_args       /* {
906                                  * struct vnode *a_vp;
907                                  * int  a_events;
908                                  * struct ucred *a_cred;
909                                  * struct proc *a_p;
910                                  * } */ *ap;
911 {
912     /*
913      * We should really check to see if I/O is possible.
914      */
915     return (1);
916 }
917
918 /*
919  * Mmap a file
920  *
921  * NB Currently unsupported.
922  */
923 /* ARGSUSED */
924 int
925 afs_vop_mmap(ap)
926      struct vop_mmap_args       /* {
927                                  * struct vnode *a_vp;
928                                  * int  a_fflags;
929                                  * struct ucred *a_cred;
930                                  * struct proc *a_p;
931                                  * } */ *ap;
932 {
933     return (EINVAL);
934 }
935
936 int
937 afs_vop_fsync(ap)
938      struct vop_fsync_args      /* {
939                                  * struct vnode *a_vp;
940                                  * struct ucred *a_cred;
941                                  * int a_waitfor;
942                                  * struct proc *a_p;
943                                  * } */ *ap;
944 {
945     int error;
946     register struct vnode *vp = ap->a_vp;
947
948     AFS_GLOCK();
949     /*vflushbuf(vp, wait); */
950 #ifdef AFS_FBSD60_ENV
951     error = afs_fsync(VTOAFS(vp), ap->a_td->td_ucred);
952 #else
953     if (ap->a_cred)
954         error = afs_fsync(VTOAFS(vp), ap->a_cred);
955     else
956         error = afs_fsync(VTOAFS(vp), afs_osi_credp);
957 #endif
958     AFS_GUNLOCK();
959     return error;
960 }
961
962 int
963 afs_vop_remove(ap)
964      struct vop_remove_args     /* {
965                                  * struct vnode *a_dvp;
966                                  * struct vnode *a_vp;
967                                  * struct componentname *a_cnp;
968                                  * } */ *ap;
969 {
970     int error = 0;
971     register struct vnode *vp = ap->a_vp;
972     register struct vnode *dvp = ap->a_dvp;
973
974     GETNAME();
975     AFS_GLOCK();
976     error = afs_remove(VTOAFS(dvp), name, cnp->cn_cred);
977     AFS_GUNLOCK();
978     cache_purge(vp);
979     DROPNAME();
980     return error;
981 }
982
983 int
984 afs_vop_link(ap)
985      struct vop_link_args       /* {
986                                  * struct vnode *a_vp;
987                                  * struct vnode *a_tdvp;
988                                  * struct componentname *a_cnp;
989                                  * } */ *ap;
990 {
991     int error = 0;
992     register struct vnode *dvp = ap->a_tdvp;
993     register struct vnode *vp = ap->a_vp;
994 #ifdef AFS_FBSD50_ENV
995     struct thread *p = ap->a_cnp->cn_thread;
996 #else
997     struct proc *p = ap->a_cnp->cn_proc;
998 #endif
999
1000     GETNAME();
1001     if (dvp->v_mount != vp->v_mount) {
1002         error = EXDEV;
1003         goto out;
1004     }
1005     if (vp->v_type == VDIR) {
1006         error = EISDIR;
1007         goto out;
1008     }
1009     if ((error = vn_lock(vp, LK_EXCLUSIVE, p)) != 0) {
1010         goto out;
1011     }
1012     AFS_GLOCK();
1013     error = afs_link(VTOAFS(vp), VTOAFS(dvp), name, cnp->cn_cred);
1014     AFS_GUNLOCK();
1015     if (dvp != vp)
1016         VOP_UNLOCK(vp, 0, p);
1017   out:
1018     DROPNAME();
1019     return error;
1020 }
1021
1022 int
1023 afs_vop_rename(ap)
1024      struct vop_rename_args     /* {
1025                                  * struct vnode *a_fdvp;
1026                                  * struct vnode *a_fvp;
1027                                  * struct componentname *a_fcnp;
1028                                  * struct vnode *a_tdvp;
1029                                  * struct vnode *a_tvp;
1030                                  * struct componentname *a_tcnp;
1031                                  * } */ *ap;
1032 {
1033     int error = 0;
1034     struct componentname *fcnp = ap->a_fcnp;
1035     char *fname;
1036     struct componentname *tcnp = ap->a_tcnp;
1037     char *tname;
1038     struct vnode *tvp = ap->a_tvp;
1039     register struct vnode *tdvp = ap->a_tdvp;
1040     struct vnode *fvp = ap->a_fvp;
1041     register struct vnode *fdvp = ap->a_fdvp;
1042 #ifdef AFS_FBSD50_ENV
1043     struct thread *p = fcnp->cn_thread;
1044 #else
1045     struct proc *p = fcnp->cn_proc;
1046 #endif
1047
1048     /*
1049      * Check for cross-device rename.
1050      */
1051     if ((fvp->v_mount != tdvp->v_mount)
1052         || (tvp && (fvp->v_mount != tvp->v_mount))) {
1053         error = EXDEV;
1054       abortit:
1055         if (tdvp == tvp)
1056             vrele(tdvp);
1057         else
1058             vput(tdvp);
1059         if (tvp)
1060             vput(tvp);
1061         vrele(fdvp);
1062         vrele(fvp);
1063         return (error);
1064     }
1065     /*
1066      * if fvp == tvp, we're just removing one name of a pair of
1067      * directory entries for the same element.  convert call into rename.
1068      ( (pinched from FreeBSD 4.4's ufs_rename())
1069      
1070      */
1071     if (fvp == tvp) {
1072         if (fvp->v_type == VDIR) {
1073             error = EINVAL;
1074             goto abortit;
1075         }
1076
1077         /* Release destination completely. */
1078         vput(tdvp);
1079         vput(tvp);
1080
1081         /* Delete source. */
1082         vrele(fdvp);
1083         vrele(fvp);
1084         fcnp->cn_flags &= ~MODMASK;
1085         fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1086         if ((fcnp->cn_flags & SAVESTART) == 0)
1087             panic("afs_rename: lost from startdir");
1088         fcnp->cn_nameiop = DELETE;
1089         VREF(fdvp);
1090         error = relookup(fdvp, &fvp, fcnp);
1091         if (error == 0)
1092             vrele(fdvp);
1093         if (fvp == NULL) {
1094             return (ENOENT);
1095         }
1096
1097         error = VOP_REMOVE(fdvp, fvp, fcnp);
1098         if (fdvp == fvp)
1099             vrele(fdvp);
1100         else
1101             vput(fdvp);
1102         vput(fvp);
1103         return (error);
1104     }
1105     if ((error = vn_lock(fvp, LK_EXCLUSIVE, p)) != 0)
1106         goto abortit;
1107
1108     MALLOC(fname, char *, fcnp->cn_namelen + 1, M_TEMP, M_WAITOK);
1109     memcpy(fname, fcnp->cn_nameptr, fcnp->cn_namelen);
1110     fname[fcnp->cn_namelen] = '\0';
1111     MALLOC(tname, char *, tcnp->cn_namelen + 1, M_TEMP, M_WAITOK);
1112     memcpy(tname, tcnp->cn_nameptr, tcnp->cn_namelen);
1113     tname[tcnp->cn_namelen] = '\0';
1114
1115
1116     AFS_GLOCK();
1117     /* XXX use "from" or "to" creds? NFS uses "to" creds */
1118     error =
1119         afs_rename(VTOAFS(fdvp), fname, VTOAFS(tdvp), tname, tcnp->cn_cred);
1120     AFS_GUNLOCK();
1121
1122     FREE(fname, M_TEMP);
1123     FREE(tname, M_TEMP);
1124     if (tdvp == tvp)
1125         vrele(tdvp);
1126     else
1127         vput(tdvp);
1128     if (tvp)
1129         vput(tvp);
1130     vrele(fdvp);
1131     vput(fvp);
1132     return error;
1133 }
1134
1135 int
1136 afs_vop_mkdir(ap)
1137      struct vop_mkdir_args      /* {
1138                                  * struct vnode *a_dvp;
1139                                  * struct vnode **a_vpp;
1140                                  * struct componentname *a_cnp;
1141                                  * struct vattr *a_vap;
1142                                  * } */ *ap;
1143 {
1144     register struct vnode *dvp = ap->a_dvp;
1145     register struct vattr *vap = ap->a_vap;
1146     int error = 0;
1147     struct vcache *vcp;
1148 #ifdef AFS_FBSD50_ENV
1149     struct thread *p = ap->a_cnp->cn_thread;
1150 #else
1151     struct proc *p = ap->a_cnp->cn_proc;
1152 #endif
1153
1154     GETNAME();
1155 #ifdef DIAGNOSTIC
1156     if ((cnp->cn_flags & HASBUF) == 0)
1157         panic("afs_vop_mkdir: no name");
1158 #endif
1159     AFS_GLOCK();
1160     error = afs_mkdir(VTOAFS(dvp), name, vap, &vcp, cnp->cn_cred);
1161     AFS_GUNLOCK();
1162     if (error) {
1163         DROPNAME();
1164         return (error);
1165     }
1166     if (vcp) {
1167         *ap->a_vpp = AFSTOV(vcp);
1168         vn_lock(AFSTOV(vcp), LK_EXCLUSIVE | LK_RETRY, p);
1169     } else
1170         *ap->a_vpp = 0;
1171     DROPNAME();
1172     return error;
1173 }
1174
1175 int
1176 afs_vop_rmdir(ap)
1177      struct vop_rmdir_args      /* {
1178                                  * struct vnode *a_dvp;
1179                                  * struct vnode *a_vp;
1180                                  * struct componentname *a_cnp;
1181                                  * } */ *ap;
1182 {
1183     int error = 0;
1184     register struct vnode *dvp = ap->a_dvp;
1185
1186     GETNAME();
1187     AFS_GLOCK();
1188     error = afs_rmdir(VTOAFS(dvp), name, cnp->cn_cred);
1189     AFS_GUNLOCK();
1190     DROPNAME();
1191     return error;
1192 }
1193
1194 /* struct vop_symlink_args {
1195  *      struct vnode *a_dvp;
1196  *      struct vnode **a_vpp;
1197  *      struct componentname *a_cnp;
1198  *      struct vattr *a_vap;
1199  *      char *a_target;
1200  * };
1201  */
1202 int
1203 afs_vop_symlink(struct vop_symlink_args *ap)
1204 {
1205     struct vnode *dvp;
1206     struct vnode *newvp;
1207     struct vcache *vcp;
1208     int error;
1209
1210     GETNAME();
1211     AFS_GLOCK();
1212
1213     dvp = ap->a_dvp;
1214     newvp = NULL;
1215
1216     error =
1217         afs_symlink(VTOAFS(dvp), name, ap->a_vap, ap->a_target, cnp->cn_cred);
1218     if (error == 0) {
1219         error = afs_lookup(VTOAFS(dvp), name, &vcp, cnp->cn_cred);
1220         if (error == 0) {
1221             newvp = AFSTOV(vcp);
1222 #ifdef AFS_FBSD50_ENV
1223             vn_lock(newvp, LK_EXCLUSIVE | LK_RETRY, cnp->cn_thread);
1224 #else
1225             vn_lock(newvp, LK_EXCLUSIVE | LK_RETRY, cnp->cn_proc);
1226 #endif
1227         }
1228     }
1229     AFS_GUNLOCK();
1230     DROPNAME();
1231     *(ap->a_vpp) = newvp;
1232     return error;
1233 }
1234
1235 int
1236 afs_vop_readdir(ap)
1237      struct vop_readdir_args    /* {
1238                                  * struct vnode *a_vp;
1239                                  * struct uio *a_uio;
1240                                  * struct ucred *a_cred;
1241                                  * int *a_eofflag;
1242                                  * u_long *a_cookies;
1243                                  * int ncookies;
1244                                  * } */ *ap;
1245 {
1246     int error;
1247     off_t off;
1248 /*    printf("readdir %x cookies %x ncookies %d\n", ap->a_vp, ap->a_cookies,
1249            ap->a_ncookies); */
1250     off = ap->a_uio->uio_offset;
1251     AFS_GLOCK();
1252     error =
1253         afs_readdir(VTOAFS(ap->a_vp), ap->a_uio, ap->a_cred, ap->a_eofflag);
1254     AFS_GUNLOCK();
1255     if (!error && ap->a_ncookies != NULL) {
1256         struct uio *uio = ap->a_uio;
1257         const struct dirent *dp, *dp_start, *dp_end;
1258         int ncookies;
1259         u_long *cookies, *cookiep;
1260
1261         if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1262             panic("afs_readdir: burned cookies");
1263         dp = (const struct dirent *)
1264             ((const char *)uio->uio_iov->iov_base - (uio->uio_offset - off));
1265
1266         dp_end = (const struct dirent *)uio->uio_iov->iov_base;
1267         for (dp_start = dp, ncookies = 0; dp < dp_end;
1268              dp = (const struct dirent *)((const char *)dp + dp->d_reclen))
1269             ncookies++;
1270
1271         MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP,
1272                M_WAITOK);
1273         for (dp = dp_start, cookiep = cookies; dp < dp_end;
1274              dp = (const struct dirent *)((const char *)dp + dp->d_reclen)) {
1275             off += dp->d_reclen;
1276             *cookiep++ = off;
1277         }
1278         *ap->a_cookies = cookies;
1279         *ap->a_ncookies = ncookies;
1280     }
1281
1282     return error;
1283 }
1284
1285 int
1286 afs_vop_readlink(ap)
1287      struct vop_readlink_args   /* {
1288                                  * struct vnode *a_vp;
1289                                  * struct uio *a_uio;
1290                                  * struct ucred *a_cred;
1291                                  * } */ *ap;
1292 {
1293     int error;
1294 /*    printf("readlink %x\n", ap->a_vp);*/
1295     AFS_GLOCK();
1296     error = afs_readlink(VTOAFS(ap->a_vp), ap->a_uio, ap->a_cred);
1297     AFS_GUNLOCK();
1298     return error;
1299 }
1300
1301 extern int prtactive;
1302
1303 int
1304 afs_vop_inactive(ap)
1305      struct vop_inactive_args   /* {
1306                                  * struct vnode *a_vp;
1307                                  * struct proc *a_p;
1308                                  * } */ *ap;
1309 {
1310     register struct vnode *vp = ap->a_vp;
1311
1312     if (prtactive && vp->v_usecount != 0)
1313         vprint("afs_vop_inactive(): pushing active", vp);
1314
1315     AFS_GLOCK();
1316     afs_InactiveVCache(VTOAFS(vp), 0);  /* decrs ref counts */
1317     AFS_GUNLOCK();
1318     VOP_UNLOCK(vp, 0, ap->a_p);
1319     return 0;
1320 }
1321
1322 /*
1323  * struct vop_reclaim_args {
1324  *      struct vnode *a_vp;
1325  * };
1326  */
1327 int
1328 afs_vop_reclaim(struct vop_reclaim_args *ap)
1329 {
1330     /* copied from ../OBSD/osi_vnodeops.c:afs_nbsd_reclaim() */
1331     int code, slept;
1332     struct vnode *vp = ap->a_vp;
1333     struct vcache *avc = VTOAFS(vp);
1334     int haveGlock = ISAFS_GLOCK();
1335     int haveVlock = CheckLock(&afs_xvcache);
1336
1337     if (!haveGlock)
1338         AFS_GLOCK();
1339     if (!haveVlock)
1340         ObtainWriteLock(&afs_xvcache, 901);
1341 #ifndef AFS_DISCON_ENV
1342     code = afs_FlushVCache(avc, &slept);        /* tosses our stuff from vnode */
1343 #else
1344     /* reclaim the vnode and the in-memory vcache, but keep the on-disk vcache */
1345     code = afs_FlushVS(avc);
1346 #endif
1347     if (!haveVlock)
1348         ReleaseWriteLock(&afs_xvcache);
1349     if (!haveGlock)
1350         AFS_GUNLOCK();
1351
1352     /*
1353      * XXX Pretend it worked, to prevent panic on shutdown
1354      * Garrett, please fix - Jim Rees
1355      */
1356     if (code)
1357         printf("afs_vop_reclaim: afs_FlushVCache failed code %d\n", code);
1358
1359 #ifdef AFS_FBSD60_ENV
1360     vnode_destroy_vobject(vp);
1361 #endif
1362     return 0;
1363 }
1364
1365 #ifndef AFS_FBSD60_ENV
1366 int
1367 afs_vop_bmap(ap)
1368      struct vop_bmap_args       /* {
1369                                  * struct vnode *a_vp;
1370                                  * daddr_t  a_bn;
1371                                  * struct vnode **a_vpp;
1372                                  * daddr_t *a_bnp;
1373                                  * int *a_runp;
1374                                  * int *a_runb;
1375                                  * } */ *ap;
1376 {
1377     if (ap->a_bnp) {
1378         *ap->a_bnp = ap->a_bn * (PAGE_SIZE / DEV_BSIZE);
1379     }
1380     if (ap->a_vpp) {
1381         *ap->a_vpp = ap->a_vp;
1382     }
1383     if (ap->a_runp != NULL)
1384         *ap->a_runp = 0;
1385     if (ap->a_runb != NULL)
1386         *ap->a_runb = 0;
1387
1388     return 0;
1389 }
1390 #endif
1391
1392 int
1393 afs_vop_strategy(ap)
1394      struct vop_strategy_args   /* {
1395                                  * struct buf *a_bp;
1396                                  * } */ *ap;
1397 {
1398     int error;
1399     AFS_GLOCK();
1400     error = afs_ustrategy(ap->a_bp);
1401     AFS_GUNLOCK();
1402     return error;
1403 }
1404
1405 int
1406 afs_vop_print(ap)
1407      struct vop_print_args      /* {
1408                                  * struct vnode *a_vp;
1409                                  * } */ *ap;
1410 {
1411     register struct vnode *vp = ap->a_vp;
1412     register struct vcache *vc = VTOAFS(ap->a_vp);
1413     int s = vc->states;
1414
1415 #ifdef AFS_FBSD50_ENV
1416     printf("tag %s, fid: %d.%x.%x.%x, opens %d, writers %d", vp->v_tag,
1417            (int)vc->fid.Cell, (u_int) vc->fid.Fid.Volume,
1418            (u_int) vc->fid.Fid.Vnode, (u_int) vc->fid.Fid.Unique, vc->opens,
1419            vc->execsOrWriters);
1420 #else
1421     printf("tag %d, fid: %ld.%x.%x.%x, opens %d, writers %d", vp->v_tag,
1422            vc->fid.Cell, (u_int) vc->fid.Fid.Volume,
1423            (u_int) vc->fid.Fid.Vnode, (u_int) vc->fid.Fid.Unique, vc->opens,
1424            vc->execsOrWriters);
1425 #endif
1426     printf("\n  states%s%s%s%s%s", (s & CStatd) ? " statd" : "",
1427            (s & CRO) ? " readonly" : "", (s & CDirty) ? " dirty" : "",
1428            (s & CMAPPED) ? " mapped" : "",
1429            (s & CVFlushed) ? " flush in progress" : "");
1430     printf("\n");
1431     return 0;
1432 }
1433
1434 /*
1435  * Advisory record locking support (fcntl() POSIX style)
1436  */
1437 int
1438 afs_vop_advlock(ap)
1439      struct vop_advlock_args    /* {
1440                                  * struct vnode *a_vp;
1441                                  * caddr_t  a_id;
1442                                  * int  a_op;
1443                                  * struct flock *a_fl;
1444                                  * int  a_flags;
1445                                  * } */ *ap;
1446 {
1447     int error;
1448     struct ucred cr = *osi_curcred();
1449
1450     AFS_GLOCK();
1451     error =
1452         afs_lockctl(VTOAFS(ap->a_vp), ap->a_fl, ap->a_op, &cr, (int)ap->a_id);
1453     AFS_GUNLOCK();
1454     return error;
1455 }