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