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