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