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