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