freebsd60-mount-20050429
[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     struct vcache *vc = VTOAFS(ap->a_vp);
495
496     AFS_GLOCK();
497     error = afs_open(&vc, ap->a_mode, ap->a_cred);
498 #ifdef DIAGNOSTIC
499     if (AFSTOV(vc) != ap->a_vp)
500         panic("AFS open changed vnode!");
501 #endif
502     AFS_GUNLOCK();
503 #ifdef AFS_FBSD60_ENV
504     vnode_create_vobject(ap->a_vp, vc->m.Length, ap->a_td);
505 #endif
506     osi_FlushPages(vc, ap->a_cred);
507     return error;
508 }
509
510 int
511 afs_vop_close(ap)
512      struct vop_close_args      /* {
513                                  * struct vnode *a_vp;
514                                  * int  a_fflag;
515                                  * struct ucred *a_cred;
516                                  * struct proc *a_p;
517                                  * } */ *ap;
518 {
519     int code;
520     struct vcache *avc = VTOAFS(ap->a_vp);
521     AFS_GLOCK();
522     if (ap->a_cred)
523         code = afs_close(avc, ap->a_fflag, ap->a_cred);
524     else
525         code = afs_close(avc, ap->a_fflag, afs_osi_credp);
526     osi_FlushPages(avc, ap->a_cred);    /* hold bozon lock, but not basic vnode lock */
527     AFS_GUNLOCK();
528     return code;
529 }
530
531 int
532 afs_vop_access(ap)
533      struct vop_access_args     /* {
534                                  * struct vnode *a_vp;
535                                  * int  a_mode;
536                                  * struct ucred *a_cred;
537                                  * struct proc *a_p;
538                                  * } */ *ap;
539 {
540     int code;
541     AFS_GLOCK();
542     code = afs_access(VTOAFS(ap->a_vp), ap->a_mode, ap->a_cred);
543     AFS_GUNLOCK();
544     return code;
545 }
546
547 int
548 afs_vop_getattr(ap)
549      struct vop_getattr_args    /* {
550                                  * struct vnode *a_vp;
551                                  * struct vattr *a_vap;
552                                  * struct ucred *a_cred;
553                                  * struct proc *a_p;
554                                  * } */ *ap;
555 {
556     int code;
557     AFS_GLOCK();
558     code = afs_getattr(VTOAFS(ap->a_vp), ap->a_vap, ap->a_cred);
559     AFS_GUNLOCK();
560     return code;
561 }
562
563 int
564 afs_vop_setattr(ap)
565      struct vop_setattr_args    /* {
566                                  * struct vnode *a_vp;
567                                  * struct vattr *a_vap;
568                                  * struct ucred *a_cred;
569                                  * struct proc *a_p;
570                                  * } */ *ap;
571 {
572     int code;
573     AFS_GLOCK();
574     code = afs_setattr(VTOAFS(ap->a_vp), ap->a_vap, ap->a_cred);
575     AFS_GUNLOCK();
576     return code;
577 }
578
579 int
580 afs_vop_read(ap)
581      struct vop_read_args       /* {
582                                  * struct vnode *a_vp;
583                                  * struct uio *a_uio;
584                                  * int a_ioflag;
585                                  * struct ucred *a_cred;
586                                  * 
587                                  * } */ *ap;
588 {
589     int code;
590     struct vcache *avc = VTOAFS(ap->a_vp);
591     AFS_GLOCK();
592     osi_FlushPages(avc, ap->a_cred);    /* hold bozon lock, but not basic vnode lock */
593     code = afs_read(avc, ap->a_uio, ap->a_cred, 0, 0, 0);
594     AFS_GUNLOCK();
595     return code;
596 }
597
598 /* struct vop_getpages_args {
599  *      struct vnode *a_vp;
600  *      vm_page_t *a_m;
601  *      int a_count;
602  *      int a_reqpage;
603  *      vm_oofset_t a_offset;
604  * };
605  */
606 int
607 afs_vop_getpages(struct vop_getpages_args *ap)
608 {
609     int code;
610     int i, nextoff, size, toff, npages;
611     struct uio uio;
612     struct iovec iov;
613     struct buf *bp;
614     vm_offset_t kva;
615     vm_object_t object;
616     struct vnode *vp;
617     struct vcache *avc;
618
619 #ifdef AFS_FBSD50_ENV
620     GIANT_REQUIRED;
621 #endif
622     vp = ap->a_vp;
623     avc = VTOAFS(vp);
624     if ((object = vp->v_object) == NULL) {
625         printf("afs_getpages: called with non-merged cache vnode??\n");
626         return VM_PAGER_ERROR;
627     }
628     npages = btoc(ap->a_count);
629     /*
630      * If the requested page is partially valid, just return it and
631      * allow the pager to zero-out the blanks.  Partially valid pages
632      * can only occur at the file EOF.
633      */
634
635     {
636         vm_page_t m = ap->a_m[ap->a_reqpage];
637
638 #ifdef AFS_FBSD50_ENV
639         VM_OBJECT_LOCK(object);
640         vm_page_lock_queues();
641 #endif
642         if (m->valid != 0) {
643             /* handled by vm_fault now        */
644             /* vm_page_zero_invalid(m, TRUE); */
645             for (i = 0; i < npages; ++i) {
646                 if (i != ap->a_reqpage)
647                     vm_page_free(ap->a_m[i]);
648             }
649 #ifdef AFS_FBSD50_ENV
650             vm_page_unlock_queues();
651             VM_OBJECT_UNLOCK(object);
652 #endif
653             return (0);
654         }
655 #ifdef AFS_FBSD50_ENV
656         vm_page_unlock_queues();
657         VM_OBJECT_UNLOCK(object);
658 #endif
659     }
660     bp = getpbuf(&afs_pbuf_freecnt);
661
662     kva = (vm_offset_t) bp->b_data;
663     pmap_qenter(kva, ap->a_m, npages);
664 #ifdef AFS_FBSD50_ENV
665     cnt.v_vnodein++;
666     cnt.v_vnodepgsin += npages;
667 #endif
668
669     iov.iov_base = (caddr_t) kva;
670     iov.iov_len = ap->a_count;
671     uio.uio_iov = &iov;
672     uio.uio_iovcnt = 1;
673     uio.uio_offset = IDX_TO_OFF(ap->a_m[0]->pindex);
674     uio.uio_resid = ap->a_count;
675     uio.uio_segflg = UIO_SYSSPACE;
676     uio.uio_rw = UIO_READ;
677 #ifdef AFS_FBSD50_ENV
678     uio.uio_td = curthread;
679 #else
680     uio.uio_procp = curproc;
681 #endif
682
683     AFS_GLOCK();
684     osi_FlushPages(avc, osi_curcred()); /* hold bozon lock, but not basic vnode lock */
685     code = afs_read(avc, &uio, osi_curcred(), 0, 0, 0);
686     AFS_GUNLOCK();
687     pmap_qremove(kva, npages);
688
689     relpbuf(bp, &afs_pbuf_freecnt);
690
691     if (code && (uio.uio_resid == ap->a_count)) {
692 #ifdef AFS_FBSD50_ENV
693         VM_OBJECT_LOCK(object);
694         vm_page_lock_queues();
695 #endif
696         for (i = 0; i < npages; ++i) {
697             if (i != ap->a_reqpage)
698                 vm_page_free(ap->a_m[i]);
699         }
700 #ifdef AFS_FBSD50_ENV
701         vm_page_unlock_queues();
702         VM_OBJECT_UNLOCK(object);
703 #endif
704         return VM_PAGER_ERROR;
705     }
706
707     size = ap->a_count - uio.uio_resid;
708 #ifdef AFS_FBSD50_ENV
709     VM_OBJECT_LOCK(object);
710     vm_page_lock_queues();
711 #endif
712     for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
713         vm_page_t m;
714         nextoff = toff + PAGE_SIZE;
715         m = ap->a_m[i];
716
717         m->flags &= ~PG_ZERO;
718
719         if (nextoff <= size) {
720             /*
721              * Read operation filled an entire page
722              */
723             m->valid = VM_PAGE_BITS_ALL;
724             vm_page_undirty(m);
725         } else if (size > toff) {
726             /*
727              * Read operation filled a partial page.
728              */
729             m->valid = 0;
730             vm_page_set_validclean(m, 0, size - toff);
731             /* handled by vm_fault now        */
732             /* vm_page_zero_invalid(m, TRUE); */
733         }
734
735         if (i != ap->a_reqpage) {
736             /*
737              * Whether or not to leave the page activated is up in
738              * the air, but we should put the page on a page queue
739              * somewhere (it already is in the object).  Result:
740              * It appears that emperical results show that
741              * deactivating pages is best.
742              */
743
744             /*
745              * Just in case someone was asking for this page we
746              * now tell them that it is ok to use.
747              */
748             if (!code) {
749                 if (m->flags & PG_WANTED)
750                     vm_page_activate(m);
751                 else
752                     vm_page_deactivate(m);
753                 vm_page_wakeup(m);
754             } else {
755                 vm_page_free(m);
756             }
757         }
758     }
759 #ifdef AFS_FBSD50_ENV
760     vm_page_unlock_queues();
761     VM_OBJECT_UNLOCK(object);
762 #endif
763     return 0;
764 }
765
766 int
767 afs_vop_write(ap)
768      struct vop_write_args      /* {
769                                  * struct vnode *a_vp;
770                                  * struct uio *a_uio;
771                                  * int a_ioflag;
772                                  * struct ucred *a_cred;
773                                  * } */ *ap;
774 {
775     int code;
776     struct vcache *avc = VTOAFS(ap->a_vp);
777     AFS_GLOCK();
778     osi_FlushPages(avc, ap->a_cred);    /* hold bozon lock, but not basic vnode lock */
779     code =
780         afs_write(VTOAFS(ap->a_vp), ap->a_uio, ap->a_ioflag, ap->a_cred, 0);
781     AFS_GUNLOCK();
782     return code;
783 }
784
785 /*-
786  * struct vop_putpages_args {
787  *      struct vnode *a_vp;
788  *      vm_page_t *a_m;
789  *      int a_count;
790  *      int a_sync;
791  *      int *a_rtvals;
792  *      vm_oofset_t a_offset;
793  * };
794  */
795 /*
796  * All of the pages passed to us in ap->a_m[] are already marked as busy,
797  * so there is no additional locking required to set their flags.  -GAW
798  */
799 int
800 afs_vop_putpages(struct vop_putpages_args *ap)
801 {
802     int code;
803     int i, size, npages, sync;
804     struct uio uio;
805     struct iovec iov;
806     struct buf *bp;
807     vm_offset_t kva;
808     struct vnode *vp;
809     struct vcache *avc;
810
811 #ifdef AFS_FBSD50_ENV
812     GIANT_REQUIRED;
813 #endif
814
815     vp = ap->a_vp;
816     avc = VTOAFS(vp);
817     /* Perhaps these two checks should just be KASSERTs instead... */
818     if (vp->v_object == NULL) {
819         printf("afs_putpages: called with non-merged cache vnode??\n");
820         return VM_PAGER_ERROR;  /* XXX I think this is insufficient */
821     }
822     if (vType(avc) != VREG) {
823         printf("afs_putpages: not VREG");
824         return VM_PAGER_ERROR;  /* XXX I think this is insufficient */
825     }
826     npages = btoc(ap->a_count);
827     for (i = 0; i < npages; i++)
828         ap->a_rtvals[i] = VM_PAGER_AGAIN;
829     bp = getpbuf(&afs_pbuf_freecnt);
830
831     kva = (vm_offset_t) bp->b_data;
832     pmap_qenter(kva, ap->a_m, npages);
833 #ifdef AFS_FBSD50_ENV
834     cnt.v_vnodeout++;
835     cnt.v_vnodepgsout += ap->a_count;
836 #endif
837
838     iov.iov_base = (caddr_t) kva;
839     iov.iov_len = ap->a_count;
840     uio.uio_iov = &iov;
841     uio.uio_iovcnt = 1;
842     uio.uio_offset = IDX_TO_OFF(ap->a_m[0]->pindex);
843     uio.uio_resid = ap->a_count;
844     uio.uio_segflg = UIO_SYSSPACE;
845     uio.uio_rw = UIO_WRITE;
846 #ifdef AFS_FBSD50_ENV
847     uio.uio_td = curthread;
848 #else
849     uio.uio_procp = curproc;
850 #endif
851     sync = IO_VMIO;
852     if (ap->a_sync & VM_PAGER_PUT_SYNC)
853         sync |= IO_SYNC;
854     /*if (ap->a_sync & VM_PAGER_PUT_INVAL)
855      * sync |= IO_INVAL; */
856
857     AFS_GLOCK();
858     code = afs_write(avc, &uio, sync, osi_curcred(), 0);
859     AFS_GUNLOCK();
860
861     pmap_qremove(kva, npages);
862     relpbuf(bp, &afs_pbuf_freecnt);
863
864     if (!code) {
865         size = ap->a_count - uio.uio_resid;
866         for (i = 0; i < round_page(size) / PAGE_SIZE; i++) {
867             ap->a_rtvals[i] = VM_PAGER_OK;
868             vm_page_undirty(ap->a_m[i]);
869         }
870     }
871     return ap->a_rtvals[0];
872 }
873
874 int
875 afs_vop_ioctl(ap)
876      struct vop_ioctl_args      /* {
877                                  * struct vnode *a_vp;
878                                  * int  a_command;
879                                  * caddr_t  a_data;
880                                  * int  a_fflag;
881                                  * struct ucred *a_cred;
882                                  * struct proc *a_p;
883                                  * } */ *ap;
884 {
885     struct vcache *tvc = VTOAFS(ap->a_vp);
886     int error = 0;
887
888     /* in case we ever get in here... */
889
890     AFS_STATCNT(afs_ioctl);
891     if (((ap->a_command >> 8) & 0xff) == 'V') {
892         /* This is a VICEIOCTL call */
893         AFS_GLOCK();
894         error = HandleIoctl(tvc, NULL /*Not used */ ,
895                             ap->a_command, ap->a_data);
896         AFS_GUNLOCK();
897         return (error);
898     } else {
899         /* No-op call; just return. */
900         return (ENOTTY);
901     }
902 }
903
904 /* ARGSUSED */
905 int
906 afs_vop_poll(ap)
907      struct vop_poll_args       /* {
908                                  * struct vnode *a_vp;
909                                  * int  a_events;
910                                  * struct ucred *a_cred;
911                                  * struct proc *a_p;
912                                  * } */ *ap;
913 {
914     /*
915      * We should really check to see if I/O is possible.
916      */
917     return (1);
918 }
919
920 /*
921  * Mmap a file
922  *
923  * NB Currently unsupported.
924  */
925 /* ARGSUSED */
926 int
927 afs_vop_mmap(ap)
928      struct vop_mmap_args       /* {
929                                  * struct vnode *a_vp;
930                                  * int  a_fflags;
931                                  * struct ucred *a_cred;
932                                  * struct proc *a_p;
933                                  * } */ *ap;
934 {
935     return (EINVAL);
936 }
937
938 int
939 afs_vop_fsync(ap)
940      struct vop_fsync_args      /* {
941                                  * struct vnode *a_vp;
942                                  * struct ucred *a_cred;
943                                  * int a_waitfor;
944                                  * struct proc *a_p;
945                                  * } */ *ap;
946 {
947     int error;
948     register struct vnode *vp = ap->a_vp;
949
950     AFS_GLOCK();
951     /*vflushbuf(vp, wait); */
952 #ifdef AFS_FBSD60_ENV
953     error = afs_fsync(VTOAFS(vp), ap->a_td->td_ucred);
954 #else
955     if (ap->a_cred)
956         error = afs_fsync(VTOAFS(vp), ap->a_cred);
957     else
958         error = afs_fsync(VTOAFS(vp), afs_osi_credp);
959 #endif
960     AFS_GUNLOCK();
961     return error;
962 }
963
964 int
965 afs_vop_remove(ap)
966      struct vop_remove_args     /* {
967                                  * struct vnode *a_dvp;
968                                  * struct vnode *a_vp;
969                                  * struct componentname *a_cnp;
970                                  * } */ *ap;
971 {
972     int error = 0;
973     register struct vnode *vp = ap->a_vp;
974     register struct vnode *dvp = ap->a_dvp;
975
976     GETNAME();
977     AFS_GLOCK();
978     error = afs_remove(VTOAFS(dvp), name, cnp->cn_cred);
979     AFS_GUNLOCK();
980     cache_purge(vp);
981     DROPNAME();
982     return error;
983 }
984
985 int
986 afs_vop_link(ap)
987      struct vop_link_args       /* {
988                                  * struct vnode *a_vp;
989                                  * struct vnode *a_tdvp;
990                                  * struct componentname *a_cnp;
991                                  * } */ *ap;
992 {
993     int error = 0;
994     register struct vnode *dvp = ap->a_tdvp;
995     register struct vnode *vp = ap->a_vp;
996 #ifdef AFS_FBSD50_ENV
997     struct thread *p = ap->a_cnp->cn_thread;
998 #else
999     struct proc *p = ap->a_cnp->cn_proc;
1000 #endif
1001
1002     GETNAME();
1003     if (dvp->v_mount != vp->v_mount) {
1004         error = EXDEV;
1005         goto out;
1006     }
1007     if (vp->v_type == VDIR) {
1008         error = EISDIR;
1009         goto out;
1010     }
1011     if ((error = vn_lock(vp, LK_EXCLUSIVE, p)) != 0) {
1012         goto out;
1013     }
1014     AFS_GLOCK();
1015     error = afs_link(VTOAFS(vp), VTOAFS(dvp), name, cnp->cn_cred);
1016     AFS_GUNLOCK();
1017     if (dvp != vp)
1018         VOP_UNLOCK(vp, 0, p);
1019   out:
1020     DROPNAME();
1021     return error;
1022 }
1023
1024 int
1025 afs_vop_rename(ap)
1026      struct vop_rename_args     /* {
1027                                  * struct vnode *a_fdvp;
1028                                  * struct vnode *a_fvp;
1029                                  * struct componentname *a_fcnp;
1030                                  * struct vnode *a_tdvp;
1031                                  * struct vnode *a_tvp;
1032                                  * struct componentname *a_tcnp;
1033                                  * } */ *ap;
1034 {
1035     int error = 0;
1036     struct componentname *fcnp = ap->a_fcnp;
1037     char *fname;
1038     struct componentname *tcnp = ap->a_tcnp;
1039     char *tname;
1040     struct vnode *tvp = ap->a_tvp;
1041     register struct vnode *tdvp = ap->a_tdvp;
1042     struct vnode *fvp = ap->a_fvp;
1043     register struct vnode *fdvp = ap->a_fdvp;
1044 #ifdef AFS_FBSD50_ENV
1045     struct thread *p = fcnp->cn_thread;
1046 #else
1047     struct proc *p = fcnp->cn_proc;
1048 #endif
1049
1050     /*
1051      * Check for cross-device rename.
1052      */
1053     if ((fvp->v_mount != tdvp->v_mount)
1054         || (tvp && (fvp->v_mount != tvp->v_mount))) {
1055         error = EXDEV;
1056       abortit:
1057         if (tdvp == tvp)
1058             vrele(tdvp);
1059         else
1060             vput(tdvp);
1061         if (tvp)
1062             vput(tvp);
1063         vrele(fdvp);
1064         vrele(fvp);
1065         return (error);
1066     }
1067     /*
1068      * if fvp == tvp, we're just removing one name of a pair of
1069      * directory entries for the same element.  convert call into rename.
1070      ( (pinched from FreeBSD 4.4's ufs_rename())
1071      
1072      */
1073     if (fvp == tvp) {
1074         if (fvp->v_type == VDIR) {
1075             error = EINVAL;
1076             goto abortit;
1077         }
1078
1079         /* Release destination completely. */
1080         vput(tdvp);
1081         vput(tvp);
1082
1083         /* Delete source. */
1084         vrele(fdvp);
1085         vrele(fvp);
1086         fcnp->cn_flags &= ~MODMASK;
1087         fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1088         if ((fcnp->cn_flags & SAVESTART) == 0)
1089             panic("afs_rename: lost from startdir");
1090         fcnp->cn_nameiop = DELETE;
1091         VREF(fdvp);
1092         error = relookup(fdvp, &fvp, fcnp);
1093         if (error == 0)
1094             vrele(fdvp);
1095         if (fvp == NULL) {
1096             return (ENOENT);
1097         }
1098
1099         error = VOP_REMOVE(fdvp, fvp, fcnp);
1100         if (fdvp == fvp)
1101             vrele(fdvp);
1102         else
1103             vput(fdvp);
1104         vput(fvp);
1105         return (error);
1106     }
1107     if ((error = vn_lock(fvp, LK_EXCLUSIVE, p)) != 0)
1108         goto abortit;
1109
1110     MALLOC(fname, char *, fcnp->cn_namelen + 1, M_TEMP, M_WAITOK);
1111     memcpy(fname, fcnp->cn_nameptr, fcnp->cn_namelen);
1112     fname[fcnp->cn_namelen] = '\0';
1113     MALLOC(tname, char *, tcnp->cn_namelen + 1, M_TEMP, M_WAITOK);
1114     memcpy(tname, tcnp->cn_nameptr, tcnp->cn_namelen);
1115     tname[tcnp->cn_namelen] = '\0';
1116
1117
1118     AFS_GLOCK();
1119     /* XXX use "from" or "to" creds? NFS uses "to" creds */
1120     error =
1121         afs_rename(VTOAFS(fdvp), fname, VTOAFS(tdvp), tname, tcnp->cn_cred);
1122     AFS_GUNLOCK();
1123
1124     FREE(fname, M_TEMP);
1125     FREE(tname, M_TEMP);
1126     if (tdvp == tvp)
1127         vrele(tdvp);
1128     else
1129         vput(tdvp);
1130     if (tvp)
1131         vput(tvp);
1132     vrele(fdvp);
1133     vput(fvp);
1134     return error;
1135 }
1136
1137 int
1138 afs_vop_mkdir(ap)
1139      struct vop_mkdir_args      /* {
1140                                  * struct vnode *a_dvp;
1141                                  * struct vnode **a_vpp;
1142                                  * struct componentname *a_cnp;
1143                                  * struct vattr *a_vap;
1144                                  * } */ *ap;
1145 {
1146     register struct vnode *dvp = ap->a_dvp;
1147     register struct vattr *vap = ap->a_vap;
1148     int error = 0;
1149     struct vcache *vcp;
1150 #ifdef AFS_FBSD50_ENV
1151     struct thread *p = ap->a_cnp->cn_thread;
1152 #else
1153     struct proc *p = ap->a_cnp->cn_proc;
1154 #endif
1155
1156     GETNAME();
1157 #ifdef DIAGNOSTIC
1158     if ((cnp->cn_flags & HASBUF) == 0)
1159         panic("afs_vop_mkdir: no name");
1160 #endif
1161     AFS_GLOCK();
1162     error = afs_mkdir(VTOAFS(dvp), name, vap, &vcp, cnp->cn_cred);
1163     AFS_GUNLOCK();
1164     if (error) {
1165         DROPNAME();
1166         return (error);
1167     }
1168     if (vcp) {
1169         *ap->a_vpp = AFSTOV(vcp);
1170         vn_lock(AFSTOV(vcp), LK_EXCLUSIVE | LK_RETRY, p);
1171     } else
1172         *ap->a_vpp = 0;
1173     DROPNAME();
1174     return error;
1175 }
1176
1177 int
1178 afs_vop_rmdir(ap)
1179      struct vop_rmdir_args      /* {
1180                                  * struct vnode *a_dvp;
1181                                  * struct vnode *a_vp;
1182                                  * struct componentname *a_cnp;
1183                                  * } */ *ap;
1184 {
1185     int error = 0;
1186     register struct vnode *dvp = ap->a_dvp;
1187
1188     GETNAME();
1189     AFS_GLOCK();
1190     error = afs_rmdir(VTOAFS(dvp), name, cnp->cn_cred);
1191     AFS_GUNLOCK();
1192     DROPNAME();
1193     return error;
1194 }
1195
1196 /* struct vop_symlink_args {
1197  *      struct vnode *a_dvp;
1198  *      struct vnode **a_vpp;
1199  *      struct componentname *a_cnp;
1200  *      struct vattr *a_vap;
1201  *      char *a_target;
1202  * };
1203  */
1204 int
1205 afs_vop_symlink(struct vop_symlink_args *ap)
1206 {
1207     struct vnode *dvp;
1208     struct vnode *newvp;
1209     struct vcache *vcp;
1210     int error;
1211
1212     GETNAME();
1213     AFS_GLOCK();
1214
1215     dvp = ap->a_dvp;
1216     newvp = NULL;
1217
1218     error =
1219         afs_symlink(VTOAFS(dvp), name, ap->a_vap, ap->a_target, cnp->cn_cred);
1220     if (error == 0) {
1221         error = afs_lookup(VTOAFS(dvp), name, &vcp, cnp->cn_cred);
1222         if (error == 0) {
1223             newvp = AFSTOV(vcp);
1224 #ifdef AFS_FBSD50_ENV
1225             vn_lock(newvp, LK_EXCLUSIVE | LK_RETRY, cnp->cn_thread);
1226 #else
1227             vn_lock(newvp, LK_EXCLUSIVE | LK_RETRY, cnp->cn_proc);
1228 #endif
1229         }
1230     }
1231     AFS_GUNLOCK();
1232     DROPNAME();
1233     *(ap->a_vpp) = newvp;
1234     return error;
1235 }
1236
1237 int
1238 afs_vop_readdir(ap)
1239      struct vop_readdir_args    /* {
1240                                  * struct vnode *a_vp;
1241                                  * struct uio *a_uio;
1242                                  * struct ucred *a_cred;
1243                                  * int *a_eofflag;
1244                                  * u_long *a_cookies;
1245                                  * int ncookies;
1246                                  * } */ *ap;
1247 {
1248     int error;
1249     off_t off;
1250 /*    printf("readdir %x cookies %x ncookies %d\n", ap->a_vp, ap->a_cookies,
1251            ap->a_ncookies); */
1252     off = ap->a_uio->uio_offset;
1253     AFS_GLOCK();
1254     error =
1255         afs_readdir(VTOAFS(ap->a_vp), ap->a_uio, ap->a_cred, ap->a_eofflag);
1256     AFS_GUNLOCK();
1257     if (!error && ap->a_ncookies != NULL) {
1258         struct uio *uio = ap->a_uio;
1259         const struct dirent *dp, *dp_start, *dp_end;
1260         int ncookies;
1261         u_long *cookies, *cookiep;
1262
1263         if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1264             panic("afs_readdir: burned cookies");
1265         dp = (const struct dirent *)
1266             ((const char *)uio->uio_iov->iov_base - (uio->uio_offset - off));
1267
1268         dp_end = (const struct dirent *)uio->uio_iov->iov_base;
1269         for (dp_start = dp, ncookies = 0; dp < dp_end;
1270              dp = (const struct dirent *)((const char *)dp + dp->d_reclen))
1271             ncookies++;
1272
1273         MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP,
1274                M_WAITOK);
1275         for (dp = dp_start, cookiep = cookies; dp < dp_end;
1276              dp = (const struct dirent *)((const char *)dp + dp->d_reclen)) {
1277             off += dp->d_reclen;
1278             *cookiep++ = off;
1279         }
1280         *ap->a_cookies = cookies;
1281         *ap->a_ncookies = ncookies;
1282     }
1283
1284     return error;
1285 }
1286
1287 int
1288 afs_vop_readlink(ap)
1289      struct vop_readlink_args   /* {
1290                                  * struct vnode *a_vp;
1291                                  * struct uio *a_uio;
1292                                  * struct ucred *a_cred;
1293                                  * } */ *ap;
1294 {
1295     int error;
1296 /*    printf("readlink %x\n", ap->a_vp);*/
1297     AFS_GLOCK();
1298     error = afs_readlink(VTOAFS(ap->a_vp), ap->a_uio, ap->a_cred);
1299     AFS_GUNLOCK();
1300     return error;
1301 }
1302
1303 extern int prtactive;
1304
1305 int
1306 afs_vop_inactive(ap)
1307      struct vop_inactive_args   /* {
1308                                  * struct vnode *a_vp;
1309                                  * struct proc *a_p;
1310                                  * } */ *ap;
1311 {
1312     register struct vnode *vp = ap->a_vp;
1313
1314     if (prtactive && vp->v_usecount != 0)
1315         vprint("afs_vop_inactive(): pushing active", vp);
1316
1317     AFS_GLOCK();
1318     afs_InactiveVCache(VTOAFS(vp), 0);  /* decrs ref counts */
1319     AFS_GUNLOCK();
1320     VOP_UNLOCK(vp, 0, ap->a_p);
1321     return 0;
1322 }
1323
1324 /*
1325  * struct vop_reclaim_args {
1326  *      struct vnode *a_vp;
1327  * };
1328  */
1329 int
1330 afs_vop_reclaim(struct vop_reclaim_args *ap)
1331 {
1332     /* copied from ../OBSD/osi_vnodeops.c:afs_nbsd_reclaim() */
1333     int code, slept;
1334     struct vnode *vp = ap->a_vp;
1335     struct vcache *avc = VTOAFS(vp);
1336     int haveGlock = ISAFS_GLOCK();
1337     int haveVlock = CheckLock(&afs_xvcache);
1338
1339     if (!haveGlock)
1340         AFS_GLOCK();
1341     if (!haveVlock)
1342         ObtainWriteLock(&afs_xvcache, 901);
1343 #ifndef AFS_DISCON_ENV
1344     code = afs_FlushVCache(avc, &slept);        /* tosses our stuff from vnode */
1345 #else
1346     /* reclaim the vnode and the in-memory vcache, but keep the on-disk vcache */
1347     code = afs_FlushVS(avc);
1348 #endif
1349     if (!haveVlock)
1350         ReleaseWriteLock(&afs_xvcache);
1351     if (!haveGlock)
1352         AFS_GUNLOCK();
1353
1354     /*
1355      * XXX Pretend it worked, to prevent panic on shutdown
1356      * Garrett, please fix - Jim Rees
1357      */
1358     if (code)
1359         printf("afs_vop_reclaim: afs_FlushVCache failed code %d\n", code);
1360 #ifdef AFS_FBSD60_ENV
1361     else
1362         vnode_destroy_vobject(vp);
1363 #endif
1364     return 0;
1365 }
1366
1367 #ifndef AFS_FBSD60_ENV
1368 int
1369 afs_vop_bmap(ap)
1370      struct vop_bmap_args       /* {
1371                                  * struct vnode *a_vp;
1372                                  * daddr_t  a_bn;
1373                                  * struct vnode **a_vpp;
1374                                  * daddr_t *a_bnp;
1375                                  * int *a_runp;
1376                                  * int *a_runb;
1377                                  * } */ *ap;
1378 {
1379     if (ap->a_bnp) {
1380         *ap->a_bnp = ap->a_bn * (PAGE_SIZE / DEV_BSIZE);
1381     }
1382     if (ap->a_vpp) {
1383         *ap->a_vpp = ap->a_vp;
1384     }
1385     if (ap->a_runp != NULL)
1386         *ap->a_runp = 0;
1387     if (ap->a_runb != NULL)
1388         *ap->a_runb = 0;
1389
1390     return 0;
1391 }
1392 #endif
1393
1394 int
1395 afs_vop_strategy(ap)
1396      struct vop_strategy_args   /* {
1397                                  * struct buf *a_bp;
1398                                  * } */ *ap;
1399 {
1400     int error;
1401     AFS_GLOCK();
1402     error = afs_ustrategy(ap->a_bp);
1403     AFS_GUNLOCK();
1404     return error;
1405 }
1406
1407 int
1408 afs_vop_print(ap)
1409      struct vop_print_args      /* {
1410                                  * struct vnode *a_vp;
1411                                  * } */ *ap;
1412 {
1413     register struct vnode *vp = ap->a_vp;
1414     register struct vcache *vc = VTOAFS(ap->a_vp);
1415     int s = vc->states;
1416
1417 #ifdef AFS_FBSD50_ENV
1418     printf("tag %s, fid: %d.%x.%x.%x, opens %d, writers %d", vp->v_tag,
1419            (int)vc->fid.Cell, (u_int) vc->fid.Fid.Volume,
1420            (u_int) vc->fid.Fid.Vnode, (u_int) vc->fid.Fid.Unique, vc->opens,
1421            vc->execsOrWriters);
1422 #else
1423     printf("tag %d, fid: %ld.%x.%x.%x, opens %d, writers %d", vp->v_tag,
1424            vc->fid.Cell, (u_int) vc->fid.Fid.Volume,
1425            (u_int) vc->fid.Fid.Vnode, (u_int) vc->fid.Fid.Unique, vc->opens,
1426            vc->execsOrWriters);
1427 #endif
1428     printf("\n  states%s%s%s%s%s", (s & CStatd) ? " statd" : "",
1429            (s & CRO) ? " readonly" : "", (s & CDirty) ? " dirty" : "",
1430            (s & CMAPPED) ? " mapped" : "",
1431            (s & CVFlushed) ? " flush in progress" : "");
1432     printf("\n");
1433     return 0;
1434 }
1435
1436 /*
1437  * Advisory record locking support (fcntl() POSIX style)
1438  */
1439 int
1440 afs_vop_advlock(ap)
1441      struct vop_advlock_args    /* {
1442                                  * struct vnode *a_vp;
1443                                  * caddr_t  a_id;
1444                                  * int  a_op;
1445                                  * struct flock *a_fl;
1446                                  * int  a_flags;
1447                                  * } */ *ap;
1448 {
1449     int error;
1450     struct ucred cr = *osi_curcred();
1451
1452     AFS_GLOCK();
1453     error =
1454         afs_lockctl(VTOAFS(ap->a_vp), ap->a_fl, ap->a_op, &cr, (int)ap->a_id);
1455     AFS_GUNLOCK();
1456     return error;
1457 }