darwin vnops code cleanup
[openafs.git] / src / afs / DARWIN / osi_vnodeops.c
1 /*
2  * Portions Copyright (c) 2003 Apple Computer, Inc.  All rights reserved.
3  */
4 #include <afsconfig.h>
5 #include <afs/param.h>
6
7
8 #include <afs/sysincludes.h>    /* Standard vendor system headers */
9 #include <afsincludes.h>        /* Afs-based standard headers */
10 #include <afs/afs_stats.h>      /* statistics */
11 #include <sys/malloc.h>
12 #include <sys/namei.h>
13 #include <sys/ubc.h>
14 #include <vfs/vfs_support.h>
15 #ifdef AFS_DARWIN80_ENV
16 #include <sys/vnode_if.h>
17 #include <sys/kauth.h>
18 #endif
19
20 #ifdef AFS_DARWIN80_ENV
21 #define VOPPREF(x) &vnop_ ## x
22 #define VOPPROT(x) vnop_ ## x
23 #define OSI_UPL_ABORT_RANGE(pl, offset, size, flags) \
24   ubc_upl_abort_range((pl), (offset), (size), (flags))
25 #define OSI_UPL_COMMIT_RANGE(pl, offset, size, flags) \
26   ubc_upl_commit_range((pl), (offset), (size), (flags))
27 #define OSI_UPL_MAP(upl, offset) ubc_upl_map((upl), (offset))
28 #define OSI_UPL_UNMAP(upl) ubc_upl_unmap((upl))
29 #define VOP_ABORTOP(x, y)
30 #else
31 #define VOPPREF(x) &vop_ ## x
32 #define VOPPROT(x) vop_ ## x
33 #define OSI_UPL_ABORT_RANGE(pl, offset, size, flags) \
34   kernel_upl_abort_range((pl), (offset), (size), (flags))
35 #define OSI_UPL_COMMIT_RANGE(pl, offset, size, flags) \
36   kernel_upl_commit_range((pl), (offset), (size), (flags), \
37                           UPL_GET_INTERNAL_PAGE_LIST((pl)),\
38                                     MAX_UPL_TRANSFER)
39 #define OSI_UPL_MAP(upl, offset) kernel_upl_map(kernel_map, (upl), (offset))
40 #define OSI_UPL_UNMAP(upl) kernel_upl_unmap(kernel_map, (upl))
41 #endif
42
43 extern char afs_zeros[AFS_ZEROS];
44
45 int afs_vop_lookup(struct VOPPROT(lookup_args) *);
46 int afs_vop_create(struct VOPPROT(create_args) *);
47 int afs_vop_mknod(struct VOPPROT(mknod_args) *);
48 int afs_vop_open(struct VOPPROT(open_args) *);
49 int afs_vop_close(struct VOPPROT(close_args) *);
50 int afs_vop_access(struct VOPPROT(access_args) *);
51 int afs_vop_getattr(struct VOPPROT(getattr_args) *);
52 int afs_vop_setattr(struct VOPPROT(setattr_args) *);
53 int afs_vop_read(struct VOPPROT(read_args) *);
54 int afs_vop_write(struct VOPPROT(write_args) *);
55 int afs_vop_pagein(struct VOPPROT(pagein_args) *);
56 int afs_vop_pageout(struct VOPPROT(pageout_args) *);
57 int afs_vop_ioctl(struct VOPPROT(ioctl_args) *);
58 int afs_vop_select(struct VOPPROT(select_args) *);
59 int afs_vop_mmap(struct VOPPROT(mmap_args) *);
60 int afs_vop_fsync(struct VOPPROT(fsync_args) *);
61 int afs_vop_remove(struct VOPPROT(remove_args) *);
62 int afs_vop_link(struct VOPPROT(link_args) *);
63 int afs_vop_rename(struct VOPPROT(rename_args) *);
64 int afs_vop_mkdir(struct VOPPROT(mkdir_args) *);
65 int afs_vop_rmdir(struct VOPPROT(rmdir_args) *);
66 int afs_vop_symlink(struct VOPPROT(symlink_args) *);
67 int afs_vop_readdir(struct VOPPROT(readdir_args) *);
68 int afs_vop_readlink(struct VOPPROT(readlink_args) *);
69 int afs_vop_inactive(struct VOPPROT(inactive_args) *);
70 int afs_vop_reclaim(struct VOPPROT(reclaim_args) *);
71 int afs_vop_strategy(struct VOPPROT(strategy_args) *);
72 int afs_vop_pathconf(struct VOPPROT(pathconf_args) *);
73 int afs_vop_advlock(struct VOPPROT(advlock_args) *);
74 int afs_vop_blktooff __P((struct VOPPROT(blktooff_args) *));
75 int afs_vop_offtoblk __P((struct VOPPROT(offtoblk_args) *));
76 #ifndef AFS_DARWIN80_ENV
77 int afs_vop_truncate(struct VOPPROT(truncate_args) *);
78 int afs_vop_update(struct VOPPROT(update_args) *);
79 int afs_vop_lock(struct VOPPROT(lock_args) *);
80 int afs_vop_unlock(struct VOPPROT(unlock_args) *);
81 int afs_vop_bmap(struct VOPPROT(bmap_args) *);
82 int afs_vop_seek(struct VOPPROT(seek_args) *);
83 int afs_vop_cmap __P((struct VOPPROT(cmap_args) *));
84 int afs_vop_print(struct VOPPROT(print_args) *);
85 int afs_vop_islocked(struct VOPPROT(islocked_args) *);
86 #endif
87
88 #define afs_vop_opnotsupp \
89         ((int (*) __P((struct  vop_reallocblks_args *)))eopnotsupp)
90 #define afs_vop_valloc afs_vop_opnotsupp
91 #define afs_vop_vfree afs_vop_opnotsupp
92 #define afs_vop_blkatoff afs_vop_opnotsupp
93 #define afs_vop_reallocblks afs_vop_opnotsupp
94
95 /* Global vfs data structures for AFS. */
96 int (**afs_vnodeop_p) ();
97
98 #define VOPFUNC int (*)(void *)
99
100 struct vnodeopv_entry_desc afs_vnodeop_entries[] = {
101     {VOPPREF(default_desc), (VOPFUNC)vn_default_error},
102     {VOPPREF(lookup_desc), (VOPFUNC)afs_vop_lookup},    /* lookup */
103     {VOPPREF(create_desc), (VOPFUNC)afs_vop_create},    /* create */
104     {VOPPREF(mknod_desc), (VOPFUNC)afs_vop_mknod},      /* mknod */
105     {VOPPREF(open_desc), (VOPFUNC)afs_vop_open},        /* open */
106     {VOPPREF(close_desc), (VOPFUNC)afs_vop_close},      /* close */
107     {VOPPREF(access_desc), (VOPFUNC)afs_vop_access},    /* access */
108     {VOPPREF(getattr_desc), (VOPFUNC)afs_vop_getattr},  /* getattr */
109     {VOPPREF(setattr_desc), (VOPFUNC)afs_vop_setattr},  /* setattr */
110     {VOPPREF(read_desc), (VOPFUNC)afs_vop_read},        /* read */
111     {VOPPREF(write_desc), (VOPFUNC)afs_vop_write},      /* write */
112     {VOPPREF(pagein_desc), (VOPFUNC)afs_vop_pagein},    /* read */
113     {VOPPREF(pageout_desc), (VOPFUNC)afs_vop_pageout},  /* write */
114     {VOPPREF(ioctl_desc), (VOPFUNC)afs_vop_ioctl},      /* XXX ioctl */
115     {VOPPREF(select_desc), (VOPFUNC)afs_vop_select},    /* select */
116     {VOPPREF(mmap_desc), (VOPFUNC)afs_vop_mmap},        /* mmap */
117     {VOPPREF(fsync_desc), (VOPFUNC)afs_vop_fsync},      /* fsync */
118 #ifndef AFS_DARWIN80_ENV
119     {VOPPREF(seek_desc), (VOPFUNC)afs_vop_seek},        /* seek */
120 #endif
121     {VOPPREF(remove_desc), (VOPFUNC)afs_vop_remove},    /* remove */
122     {VOPPREF(link_desc), (VOPFUNC)afs_vop_link},        /* link */
123     {VOPPREF(rename_desc), (VOPFUNC)afs_vop_rename},    /* rename */
124     {VOPPREF(mkdir_desc), (VOPFUNC)afs_vop_mkdir},      /* mkdir */
125     {VOPPREF(rmdir_desc), (VOPFUNC)afs_vop_rmdir},      /* rmdir */
126     {VOPPREF(symlink_desc), (VOPFUNC)afs_vop_symlink},  /* symlink */
127     {VOPPREF(readdir_desc), (VOPFUNC)afs_vop_readdir},  /* readdir */
128     {VOPPREF(readlink_desc), (VOPFUNC)afs_vop_readlink},        /* readlink */
129 #ifndef AFS_DARWIN80_ENV
130     {VOPPREF(abortop_desc), (VOPFUNC)nop_abortop },             /* abortop */
131 #endif
132     {VOPPREF(inactive_desc), (VOPFUNC)afs_vop_inactive},        /* inactive */
133     {VOPPREF(reclaim_desc), (VOPFUNC)afs_vop_reclaim},  /* reclaim */
134 #ifndef AFS_DARWIN80_ENV
135     {VOPPREF(lock_desc), (VOPFUNC)afs_vop_lock},        /* lock */
136     {VOPPREF(unlock_desc), (VOPFUNC)afs_vop_unlock},    /* unlock */
137     {VOPPREF(bmap_desc), (VOPFUNC)afs_vop_bmap},        /* bmap */
138 #endif
139 #ifdef AFS_DARWIN80_ENV
140     {VOPPREF(strategy_desc), (VOPFUNC)err_strategy},    /* strategy */
141 #else
142     {VOPPREF(strategy_desc), (VOPFUNC)afs_vop_strategy},        /* strategy */
143 #endif
144 #ifndef AFS_DARWIN80_ENV
145     {VOPPREF(print_desc), (VOPFUNC)afs_vop_print},      /* print */
146     {VOPPREF(islocked_desc), (VOPFUNC)afs_vop_islocked},        /* islocked */
147 #endif
148     {VOPPREF(pathconf_desc), (VOPFUNC)afs_vop_pathconf},        /* pathconf */
149     {VOPPREF(advlock_desc), (VOPFUNC)afs_vop_advlock},  /* advlock */
150 #ifndef AFS_DARWIN80_ENV
151     {VOPPREF(blkatoff_desc), (VOPFUNC)afs_vop_blkatoff},        /* blkatoff */
152     {VOPPREF(valloc_desc), (VOPFUNC)afs_vop_valloc},    /* valloc */
153     {VOPPREF(reallocblks_desc), (VOPFUNC)afs_vop_reallocblks},  /* reallocblks */
154     {VOPPREF(vfree_desc), (VOPFUNC)afs_vop_vfree},      /* vfree */
155     {VOPPREF(update_desc), (VOPFUNC)afs_vop_update},    /* update */
156     {VOPPREF(cmap_desc), (VOPFUNC)afs_vop_cmap},        /* cmap */
157     {VOPPREF(truncate_desc), (VOPFUNC)afs_vop_truncate},        /* truncate */
158 #endif
159     {VOPPREF(blktooff_desc), (VOPFUNC)afs_vop_blktooff},        /* blktooff */
160     {VOPPREF(offtoblk_desc), (VOPFUNC)afs_vop_offtoblk},        /* offtoblk */
161     {VOPPREF(bwrite_desc), (VOPFUNC)vn_bwrite},
162     {(struct vnodeop_desc *)NULL, (void (*)())NULL}
163 };
164 struct vnodeopv_desc afs_vnodeop_opv_desc =
165     { &afs_vnodeop_p, afs_vnodeop_entries };
166
167 #ifdef AFS_DARWIN80_ENV
168 /* vfs structures for incompletely initialized vnodes */
169 int (**afs_dead_vnodeop_p) ();
170
171 struct vnodeopv_entry_desc afs_dead_vnodeop_entries[] = {
172     {VOPPREF(default_desc), (VOPFUNC)vn_default_error},
173     {VOPPREF(lookup_desc), (VOPFUNC)vn_default_error},  /* lookup */
174     {VOPPREF(create_desc), (VOPFUNC)err_create},        /* create */
175     {VOPPREF(mknod_desc), (VOPFUNC)err_mknod},  /* mknod */
176     {VOPPREF(open_desc), (VOPFUNC)err_open},    /* open */
177     {VOPPREF(close_desc), (VOPFUNC)err_close},  /* close */
178     {VOPPREF(access_desc), (VOPFUNC)err_access},        /* access */
179     {VOPPREF(getattr_desc), (VOPFUNC)err_getattr},      /* getattr */
180     {VOPPREF(setattr_desc), (VOPFUNC)err_setattr},      /* setattr */
181     {VOPPREF(read_desc), (VOPFUNC)err_read},    /* read */
182     {VOPPREF(write_desc), (VOPFUNC)err_write},  /* write */
183     {VOPPREF(pagein_desc), (VOPFUNC)err_pagein},        /* read */
184     {VOPPREF(pageout_desc), (VOPFUNC)err_pageout},      /* write */
185     {VOPPREF(ioctl_desc), (VOPFUNC)err_ioctl},  /* XXX ioctl */
186     {VOPPREF(select_desc), (VOPFUNC)nop_select},        /* select */
187     {VOPPREF(mmap_desc), (VOPFUNC)err_mmap},    /* mmap */
188     {VOPPREF(fsync_desc), (VOPFUNC)err_fsync},  /* fsync */
189     {VOPPREF(remove_desc), (VOPFUNC)err_remove},        /* remove */
190     {VOPPREF(link_desc), (VOPFUNC)err_link},    /* link */
191     {VOPPREF(rename_desc), (VOPFUNC)err_rename},        /* rename */
192     {VOPPREF(mkdir_desc), (VOPFUNC)err_mkdir},  /* mkdir */
193     {VOPPREF(rmdir_desc), (VOPFUNC)err_rmdir},  /* rmdir */
194     {VOPPREF(symlink_desc), (VOPFUNC)err_symlink},      /* symlink */
195     {VOPPREF(readdir_desc), (VOPFUNC)err_readdir},      /* readdir */
196     {VOPPREF(readlink_desc), (VOPFUNC)err_readlink},    /* readlink */
197     {VOPPREF(inactive_desc), (VOPFUNC)afs_vop_inactive},        /* inactive */
198     {VOPPREF(reclaim_desc), (VOPFUNC)afs_vop_reclaim},  /* reclaim */
199     {VOPPREF(strategy_desc), (VOPFUNC)err_strategy},    /* strategy */
200     {VOPPREF(pathconf_desc), (VOPFUNC)err_pathconf},    /* pathconf */
201     {VOPPREF(advlock_desc), (VOPFUNC)err_advlock},      /* advlock */
202     {VOPPREF(blktooff_desc), (VOPFUNC)err_blktooff},    /* blktooff */
203     {VOPPREF(offtoblk_desc), (VOPFUNC)err_offtoblk},    /* offtoblk */
204     {VOPPREF(bwrite_desc), (VOPFUNC)err_bwrite},
205     {(struct vnodeop_desc *)NULL, (void (*)())NULL}
206 };
207 struct vnodeopv_desc afs_dead_vnodeop_opv_desc =
208     { &afs_dead_vnodeop_p, afs_dead_vnodeop_entries };
209 #endif
210
211 #define GETNAME()       \
212     struct componentname *cnp = ap->a_cnp; \
213     char *name; \
214     MALLOC(name, char *, cnp->cn_namelen+1, M_TEMP, M_WAITOK); \
215     memcpy(name, cnp->cn_nameptr, cnp->cn_namelen); \
216     name[cnp->cn_namelen] = '\0'
217
218 #define DROPNAME() FREE(name, M_TEMP)
219
220 void 
221 darwin_vn_hold(struct vnode *vp)
222 {
223     int haveGlock=ISAFS_GLOCK(); 
224     struct vcache *tvc = VTOAFS(vp);
225
226 #ifndef AFS_DARWIN80_ENV
227     tvc->f.states |= CUBCinit;
228 #endif
229 #ifdef AFS_DARWIN80_ENV
230     osi_Assert((tvc->f.states & CVInit) == 0);
231     if (tvc->f.states & CDeadVnode)
232        osi_Assert(!vnode_isinuse(vp, 1));
233 #endif
234     if (haveGlock) AFS_GUNLOCK(); 
235
236 #ifdef AFS_DARWIN80_ENV
237         if (vnode_get(vp)) {
238            /* being terminated. kernel won't give us a ref. Now what? our
239               callers don't expect us to fail */
240            if (haveGlock) AFS_GLOCK(); 
241            return;
242         }
243         if (vnode_ref(vp)) {
244             vnode_put(vp);
245             if (haveGlock) AFS_GLOCK(); 
246             return;
247         }
248         vnode_put(vp);
249 #else
250     /* vget needed for 0 ref'd vnode in GetVCache to not panic in vref.
251        vref needed for multiref'd vnode in vnop_remove not to deadlock
252        ourselves during vop_inactive, except we also need to not reinst
253        the ubc... so we just call VREF there now anyway. */
254
255     if (VREFCOUNT_GT(tvc, 0))
256         VREF(((struct vnode *)(vp))); 
257      else 
258         afs_vget(afs_globalVFS, 0, (vp));
259 #endif
260
261 #ifndef AFS_DARWIN80_ENV
262     if (UBCINFOMISSING(vp) || UBCINFORECLAIMED(vp)) {
263         ubc_info_init(vp); 
264     }
265 #endif
266
267     if (haveGlock) AFS_GLOCK(); 
268 #ifndef AFS_DARWIN80_ENV
269     tvc->f.states &= ~CUBCinit;
270 #endif
271 }
272 int
273 afs_vop_lookup(ap)
274      struct VOPPROT(lookup_args)/* {
275                                  * struct vnodeop_desc * a_desc;
276                                  * struct vnode *a_dvp;
277                                  * struct vnode **a_vpp;
278                                  * struct componentname *a_cnp;
279                                  * } */ *ap;
280 {
281     int error;
282     struct vcache *vcp;
283     struct vnode *vp, *dvp;
284     register int flags = ap->a_cnp->cn_flags;
285     int lockparent;             /* 1 => lockparent flag is set */
286     int wantparent;             /* 1 => wantparent or lockparent flag */
287     struct proc *p;
288 #ifdef AFS_DARWIN80_ENV
289     vcp = VTOAFS(ap->a_dvp);
290     if (vcp->mvstat != 1) {
291         error = cache_lookup(ap->a_dvp, ap->a_vpp, ap->a_cnp);
292         if (error == -1) 
293             return 0;
294         if (error == ENOENT) 
295             return error;
296     }
297 #endif
298
299     GETNAME();
300     p = vop_cn_proc;
301
302     lockparent = flags & LOCKPARENT;
303     wantparent = flags & (LOCKPARENT | WANTPARENT);
304
305     if (!vnode_isdir(ap->a_dvp)) {
306         *ap->a_vpp = 0;
307         DROPNAME();
308         return ENOTDIR;
309     }
310     dvp = ap->a_dvp;
311 #ifndef AFS_DARWIN80_ENV
312     if (flags & ISDOTDOT)
313         VOP_UNLOCK(dvp, 0, p);
314 #endif
315     AFS_GLOCK();
316     error = afs_lookup(VTOAFS(dvp), name, &vcp, vop_cn_cred);
317     AFS_GUNLOCK();
318     if (error) {
319 #ifndef AFS_DARWIN80_ENV
320         if (flags & ISDOTDOT)
321             VOP_LOCK(dvp, LK_EXCLUSIVE | LK_RETRY, p);
322 #endif
323         if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME)
324             && (flags & ISLASTCN) && error == ENOENT)
325             error = EJUSTRETURN;
326 #ifndef AFS_DARWIN80_ENV
327         if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
328             cnp->cn_flags |= SAVENAME;
329 #endif
330         DROPNAME();
331         *ap->a_vpp = 0;
332         return (error);
333     }
334 #ifdef AFS_DARWIN80_ENV
335     if ((error=afs_darwin_finalizevnode(vcp, ap->a_dvp, ap->a_cnp, 0, 0))) {
336         DROPNAME();
337         *ap->a_vpp = 0;
338         return error;
339     }
340 #endif
341     vp = AFSTOV(vcp);           /* always get a node if no error */
342 #ifndef AFS_DARWIN80_ENV /* XXX needed for multi-mount thing, but can't have it yet */
343     vp->v_vfsp = dvp->v_vfsp;
344
345     if (UBCINFOMISSING(vp) ||
346         UBCINFORECLAIMED(vp)) {
347             ubc_info_init(vp);
348     }
349 #endif
350
351 #ifndef AFS_DARWIN80_ENV
352     /* The parent directory comes in locked.  We unlock it on return
353      * unless the caller wants it left locked.
354      * we also always return the vnode locked. */
355
356     if (flags & ISDOTDOT) {
357         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
358         /* always return the child locked */
359         if (lockparent && (flags & ISLASTCN)
360             && (error = vn_lock(dvp, LK_EXCLUSIVE, p))) {
361             vput(vp);
362             DROPNAME();
363             return (error);
364         }
365     } else if (vp == dvp) {
366         /* they're the same; afs_lookup() already ref'ed the leaf.
367          * It came in locked, so we don't need to ref OR lock it */
368     } else {
369         if (!lockparent || !(flags & ISLASTCN))
370             VOP_UNLOCK(dvp, 0, p);      /* done with parent. */
371         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
372         /* always return the child locked */
373     }
374 #endif
375     *ap->a_vpp = vp;
376
377 #ifndef AFS_DARWIN80_ENV
378     if ((cnp->cn_nameiop == RENAME && wantparent && (flags & ISLASTCN)
379          || (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))))
380         cnp->cn_flags |= SAVENAME;
381 #endif
382
383     DROPNAME();
384     return error;
385 }
386
387 int
388 afs_vop_create(ap)
389      struct VOPPROT(create_args)        /* {
390                                  * struct vnode *a_dvp;
391                                  * struct vnode **a_vpp;
392                                  * struct componentname *a_cnp;
393                                  * struct vattr *a_vap;
394                                  * } */ *ap;
395 {
396     int error = 0;
397     struct vcache *vcp;
398     register struct vnode *dvp = ap->a_dvp;
399     struct proc *p;
400     GETNAME();
401     p = vop_cn_proc;
402
403     /* vnode layer handles excl/nonexcl */
404     AFS_GLOCK();
405     error =
406         afs_create(VTOAFS(dvp), name, ap->a_vap, NONEXCL, ap->a_vap->va_mode,
407                    &vcp, vop_cn_cred);
408     AFS_GUNLOCK();
409     if (error) {
410 #ifndef AFS_DARWIN80_ENV
411         VOP_ABORTOP(dvp, cnp);
412         vput(dvp);
413 #endif
414         DROPNAME();
415         return (error);
416     }
417
418     if (vcp) {
419 #ifdef AFS_DARWIN80_ENV
420       if ((error=afs_darwin_finalizevnode(vcp, ap->a_dvp, ap->a_cnp, 0, 0))) {
421             DROPNAME();
422             *ap->a_vpp=0;
423             return error;
424         }
425 #endif
426         *ap->a_vpp = AFSTOV(vcp);
427 #ifndef AFS_DARWIN80_ENV /* XXX needed for multi-mount thing, but can't have it yet */
428         (*ap->a_vpp)->v_vfsp = dvp->v_vfsp;
429         vn_lock(*ap->a_vpp, LK_EXCLUSIVE | LK_RETRY, p);
430         if (UBCINFOMISSING(*ap->a_vpp) || UBCINFORECLAIMED(*ap->a_vpp)) {
431             vcp->f.states |= CUBCinit;
432             ubc_info_init(*ap->a_vpp);
433             vcp->f.states &= ~CUBCinit;
434         }
435 #endif
436     } else
437         *ap->a_vpp = 0;
438
439 #ifndef AFS_DARWIN80_ENV
440     if ((cnp->cn_flags & SAVESTART) == 0)
441         FREE_ZONE(cnp->cn_pnbuf, cnp->cn_pnlen, M_NAMEI);
442     vput(dvp);
443 #endif
444     DROPNAME();
445     return error;
446 }
447
448 int
449 afs_vop_mknod(ap)
450      struct VOPPROT(mknod_args) /* {
451                                  * struct vnode *a_dvp;
452                                  * struct vnode **a_vpp;
453                                  * struct componentname *a_cnp;
454                                  * struct vattr *a_vap;
455                                  * } */ *ap;
456 {
457 #ifndef AFS_DARWIN80_ENV
458     FREE_ZONE(ap->a_cnp->cn_pnbuf, ap->a_cnp->cn_pnlen, M_NAMEI);
459     vput(ap->a_dvp);
460 #endif
461     return (ENODEV);
462 }
463
464 int
465 afs_vop_open(ap)
466      struct VOPPROT(open_args)  /* {
467                                  * struct vnode *a_vp;
468                                  * int  a_mode;
469                                  * struct ucred *a_cred;
470                                  * struct proc *a_p;
471                                  * } */ *ap;
472 {
473     int error;
474     struct vnode *vp = ap->a_vp;
475     struct vcache *vc = VTOAFS(vp);
476 #if !defined(AFS_DARWIN80_ENV)
477     int didhold = 0;
478     /*----------------------------------------------------------------
479      * osi_VM_TryReclaim() removes the ubcinfo of a vnode, but that vnode
480      * can later be passed to vn_open(), which will skip the call to
481      * ubc_hold(), and when the ubcinfo is later added, the ui_refcount
482      * will be off.  So we compensate by calling ubc_hold() ourselves
483      * when ui_refcount is less than 2.  If an error occurs in afs_open()
484      * we must call ubc_rele(), which is what vn_open() would do if it
485      * was able to call ubc_hold() in the first place.
486      *----------------------------------------------------------------*/
487     if (vp->v_type == VREG && !(vp->v_flag & VSYSTEM)
488       && vp->v_ubcinfo->ui_refcount < 2)
489         didhold = ubc_hold(vp);
490 #endif /* !AFS_DARWIN80_ENV */
491     AFS_GLOCK();
492     error = afs_open(&vc, ap->a_mode, vop_cred);
493 #ifdef DIAGNOSTIC
494     if (AFSTOV(vc) != vp)
495         panic("AFS open changed vnode!");
496 #endif
497     osi_FlushPages(vc, vop_cred);
498     AFS_GUNLOCK();
499 #if !defined(AFS_DARWIN80_ENV)
500     if (error && didhold)
501         ubc_rele(vp);
502 #endif /* !AFS_DARWIN80_ENV */
503     return error;
504 }
505
506 int
507 afs_vop_close(ap)
508      struct VOPPROT(close_args) /* {
509                                  * struct vnode *a_vp;
510                                  * int  a_fflag;
511                                  * struct ucred *a_cred;
512                                  * struct proc *a_p;
513                                  * } */ *ap;
514 {
515     int code;
516     struct vnode *vp = ap->a_vp;
517     struct vcache *avc = VTOAFS(vp);
518     AFS_GLOCK();
519     if (vop_cred)
520         code = afs_close(avc, ap->a_fflag, vop_cred);
521     else
522         code = afs_close(avc, ap->a_fflag, &afs_osi_cred);
523     osi_FlushPages(avc, vop_cred);      /* hold bozon lock, but not basic vnode lock */
524     /* This is legit; it just forces the fstrace event to happen */
525     code = afs_CheckCode(code, NULL, 60);
526     AFS_GUNLOCK();
527
528     return code;
529 }
530
531 #ifdef AFS_DARWIN80_ENV
532 extern int afs_fakestat_enable;
533
534 int
535 afs_vop_access(ap)
536      struct VOPPROT(access_args)        /* {
537                                  * struct vnode *a_vp;
538                                  * int  a_action;
539                                  * vfs_context_t a_context;
540                                  * } */ *ap;
541 {
542     int code;
543     struct vrequest treq;
544     struct afs_fakestat_state fakestate;
545     struct vcache * tvc = VTOAFS(ap->a_vp);
546     int bits=0;
547     int cmb = CHECK_MODE_BITS;
548     AFS_GLOCK();
549     afs_InitFakeStat(&fakestate);
550     if ((code = afs_InitReq(&treq, vop_cred)))
551         goto out2;
552
553     code = afs_TryEvalFakeStat(&tvc, &fakestate, &treq);
554     if (code) {
555         code = afs_CheckCode(code, &treq, 55);
556         goto out;
557     }
558
559     code = afs_VerifyVCache(tvc, &treq);
560     if (code) {
561         code = afs_CheckCode(code, &treq, 56);
562         goto out;
563     }
564     if (afs_fakestat_enable && tvc->mvstat && !(tvc->f.states & CStatd)) {
565         code = 0;
566         goto out;
567     }
568     if (vnode_isdir(ap->a_vp)) {
569        if (ap->a_action & KAUTH_VNODE_LIST_DIRECTORY)
570           bits |= PRSFS_LOOKUP;
571        if (ap->a_action & KAUTH_VNODE_ADD_FILE)
572           bits |= PRSFS_INSERT;
573        if (ap->a_action & KAUTH_VNODE_SEARCH)
574           bits |= PRSFS_LOOKUP;
575        if (ap->a_action & KAUTH_VNODE_DELETE)
576           bits |= PRSFS_DELETE;
577        if (ap->a_action & KAUTH_VNODE_ADD_SUBDIRECTORY)
578           bits |= PRSFS_INSERT;
579        if (ap->a_action & KAUTH_VNODE_DELETE_CHILD)
580           bits |= PRSFS_DELETE;
581 #if 0 /* I'd argue this should be enforced on the parent. But that's ugly */
582        if (ap->a_action & KAUTH_VNODE_READ_ATTRIBUTES)
583           bits |= PRSFS_LOOKUP;
584        if (ap->a_action & KAUTH_VNODE_READ_SECURITY) /* mode bits/gid, not afs acl */
585           bits |= PRSFS_LOOKUP;
586 #endif
587     } else {
588        if (ap->a_action & KAUTH_VNODE_READ_DATA)
589           bits |= PRSFS_READ;
590        if (ap->a_action & KAUTH_VNODE_WRITE_DATA)
591           bits |= PRSFS_WRITE;
592        if (ap->a_action & KAUTH_VNODE_EXECUTE)
593           bits |= PRSFS_READ; /* and mode bits.... */
594        if (ap->a_action & KAUTH_VNODE_READ_ATTRIBUTES)
595           bits |= PRSFS_LOOKUP;
596        if (ap->a_action & KAUTH_VNODE_READ_SECURITY) /* mode bits/gid, not afs acl */
597           bits |= PRSFS_LOOKUP;
598        if ((ap->a_action & ((1 << 25) - 1)) == KAUTH_VNODE_EXECUTE)
599           /* if only exec, don't check for read mode bit */
600           /* high bits of ap->a_action are not for 'generic rights bits', and
601              so should not be checked (KAUTH_VNODE_ACCESS is often present
602              and needs to be masked off) */
603           cmb |= CMB_ALLOW_EXEC_AS_READ;
604     }
605     if (ap->a_action & KAUTH_VNODE_WRITE_ATTRIBUTES)
606        bits |= PRSFS_WRITE;
607 #if 0 /* no extended attributes */
608     if (ap->a_action & KAUTH_VNODE_READ_EXTATTRIBUTES)
609        bits |= PRSFS_READ;
610     if (ap->a_action & KAUTH_VNODE_WRITE_EXTATTRIBUTES)
611        bits |= PRSFS_WRITE;
612 #endif
613     if (ap->a_action & KAUTH_VNODE_WRITE_SECURITY)
614        bits |= PRSFS_WRITE;
615     /* we can't check for KAUTH_VNODE_TAKE_OWNERSHIP, so we always permit it */
616     
617     code = afs_AccessOK(tvc, bits, &treq, cmb);
618 #if defined(AFS_DARWIN80_ENV)
619     /* In a dropbox, cp on 10.4 behaves badly, looping on EACCES */
620     /* In a dropbox, Finder may reopen the file. Let it. */
621     if (code == 0 && ((bits &~(PRSFS_READ|PRSFS_WRITE)) == 0)) {
622         code = afs_AccessOK(tvc, PRSFS_ADMINISTER|PRSFS_INSERT|bits, &treq, cmb);
623     }
624 #endif
625     if (code == 1 && vnode_vtype(ap->a_vp) == VREG &&
626         ap->a_action & KAUTH_VNODE_EXECUTE &&
627         (tvc->f.m.Mode & 0100) != 0100) {
628         code = 0;
629     }
630     if (code) {
631         code= 0;               /* if access is ok */
632     } else {
633             code = afs_CheckCode(EACCES, &treq, 57);        /* failure code */
634     }
635 out:
636      afs_PutFakeStat(&fakestate);
637 out2:
638     AFS_GUNLOCK();
639     return code;
640 }
641 #else
642 int
643 afs_vop_access(ap)
644      struct VOPPROT(access_args)        /* {
645                                  * struct vnode *a_vp;
646                                  * int  a_mode;
647                                  * struct ucred *a_cred;
648                                  * struct proc *a_p;
649                                  * } */ *ap;
650 {
651     int code;
652     AFS_GLOCK();
653     code = afs_access(VTOAFS(ap->a_vp), ap->a_mode, vop_cred);
654     AFS_GUNLOCK();
655     return code;
656 }
657 #endif
658
659 int
660 afs_vop_getattr(ap)
661      struct VOPPROT(getattr_args)       /* {
662                                  * struct vnode *a_vp;
663                                  * struct vattr *a_vap;
664                                  * struct ucred *a_cred;
665                                  * struct proc *a_p;
666                                  * } */ *ap;
667 {
668     int code;
669
670     AFS_GLOCK();
671     code = afs_getattr(VTOAFS(ap->a_vp), ap->a_vap, vop_cred);
672     /* This is legit; it just forces the fstrace event to happen */
673     code = afs_CheckCode(code, NULL, 58);
674     AFS_GUNLOCK();
675 #ifdef AFS_DARWIN80_ENV
676     VATTR_SET_SUPPORTED(ap->a_vap, va_type);
677     VATTR_SET_SUPPORTED(ap->a_vap, va_mode);
678     VATTR_SET_SUPPORTED(ap->a_vap, va_uid);
679     VATTR_SET_SUPPORTED(ap->a_vap, va_gid);
680     VATTR_SET_SUPPORTED(ap->a_vap, va_fsid);
681     VATTR_SET_SUPPORTED(ap->a_vap, va_fileid);
682     VATTR_SET_SUPPORTED(ap->a_vap, va_nlink);
683     VATTR_SET_SUPPORTED(ap->a_vap, va_data_size);
684     VATTR_SET_SUPPORTED(ap->a_vap, va_access_time);
685     VATTR_SET_SUPPORTED(ap->a_vap, va_modify_time);
686     VATTR_SET_SUPPORTED(ap->a_vap, va_change_time);
687     VATTR_SET_SUPPORTED(ap->a_vap, va_gen);
688     VATTR_SET_SUPPORTED(ap->a_vap, va_flags);
689     VATTR_SET_SUPPORTED(ap->a_vap, va_iosize);
690     VATTR_SET_SUPPORTED(ap->a_vap, va_total_alloc);
691 #endif
692     return code;
693 }
694
695 int
696 afs_vop_setattr(ap)
697      struct VOPPROT(setattr_args)       /* {
698                                  * struct vnode *a_vp;
699                                  * struct vattr *a_vap;
700                                  * struct ucred *a_cred;
701                                  * struct proc *a_p;
702                                  * } */ *ap;
703 {
704     int code;
705     AFS_GLOCK();
706     code = afs_setattr(VTOAFS(ap->a_vp), ap->a_vap, vop_cred);
707     /* This is legit; it just forces the fstrace event to happen */
708     code = afs_CheckCode(code, NULL, 59);
709     AFS_GUNLOCK();
710     return code;
711 }
712
713 int
714 afs_vop_read(ap)
715      struct VOPPROT(read_args)  /* {
716                                  * struct vnode *a_vp;
717                                  * struct uio *a_uio;
718                                  * int a_ioflag;
719                                  * struct ucred *a_cred;
720                                  * } */ *ap;
721 {
722     int code;
723     struct vnode *vp = ap->a_vp;
724     struct vcache *avc = VTOAFS(vp);
725
726     if (vnode_isdir(ap->a_vp)) 
727         return EISDIR;
728 #ifdef AFS_DARWIN80_ENV
729     ubc_msync_range(ap->a_vp, AFS_UIO_OFFSET(ap->a_uio), AFS_UIO_OFFSET(ap->a_uio) + AFS_UIO_RESID(ap->a_uio), UBC_PUSHDIRTY);
730 #else
731     if (UBCINFOEXISTS(ap->a_vp)) {
732         ubc_clean(ap->a_vp, 0);
733     }
734 #endif
735     AFS_GLOCK();
736     osi_FlushPages(avc, vop_cred);      /* hold bozon lock, but not basic vnode lock */
737     code = afs_read(avc, ap->a_uio, vop_cred, 0, 0, 0);
738     AFS_GUNLOCK();
739     return code;
740 }
741
742 int
743 afs_vop_pagein(ap)
744      struct VOPPROT(pagein_args)        /* {
745                                  * struct vnode *a_vp;
746                                  * upl_t a_pl;
747                                  * vm_offset_t a_pl_offset;
748                                  * off_t a_f_offset;
749                                  * size_t a_size;
750                                  * struct ucred *a_cred;
751                                  * int a_flags;
752                                  * } */ *ap;
753 {
754     register struct vnode *vp = ap->a_vp;
755     upl_t pl = ap->a_pl;
756     size_t size = ap->a_size;
757     off_t f_offset = ap->a_f_offset;
758     vm_offset_t pl_offset = ap->a_pl_offset;
759     int flags = ap->a_flags;
760     struct ucred *cred;
761     vm_offset_t ioaddr;
762 #ifdef AFS_DARWIN80_ENV
763     struct uio *uio;
764 #else
765     struct uio auio;
766     struct iovec aiov;
767     struct uio *uio = &auio;
768 #endif
769     int nocommit = flags & UPL_NOCOMMIT;
770
771     int code;
772     struct vcache *tvc = VTOAFS(vp);
773 #ifndef AFS_DARWIN80_ENV
774     if (UBCINVALID(vp)) {
775 #if DIAGNOSTIC
776         panic("afs_vop_pagein: invalid vp");
777 #endif /* DIAGNOSTIC */
778         return (EPERM);
779     }
780
781     UBCINFOCHECK("afs_vop_pagein", vp);
782 #endif
783     if (pl == (upl_t) NULL) {
784         panic("afs_vop_pagein: no upl");
785     }
786
787     cred = ubc_getcred(vp);
788     if (cred == NOCRED)
789         cred = vop_cred;
790
791     if (size == 0) {
792         if (!nocommit)
793             OSI_UPL_ABORT_RANGE(pl, pl_offset, size,
794                                    UPL_ABORT_ERROR | UPL_ABORT_FREE_ON_EMPTY);
795         return (0);
796     }
797     if (f_offset < 0) {
798         if (!nocommit)
799             OSI_UPL_ABORT_RANGE(pl, pl_offset, size,
800                                    UPL_ABORT_ERROR | UPL_ABORT_FREE_ON_EMPTY);
801         return (EINVAL);
802     }
803     if (f_offset & PAGE_MASK)
804         panic("afs_vop_pagein: offset not page aligned");
805
806     OSI_UPL_MAP(pl, &ioaddr);
807     ioaddr += pl_offset;
808 #ifdef AFS_DARWIN80_ENV
809     uio = uio_create(1, f_offset, UIO_SYSSPACE32, UIO_READ);
810     uio_addiov(uio, CAST_USER_ADDR_T(ioaddr), size);
811 #else
812     auio.uio_iov = &aiov;
813     auio.uio_iovcnt = 1;
814     auio.uio_offset = f_offset;
815     auio.uio_segflg = UIO_SYSSPACE;
816     auio.uio_rw = UIO_READ;
817     auio.uio_procp = NULL;
818     auio.uio_resid = aiov.iov_len = size;
819     aiov.iov_base = (caddr_t) ioaddr;
820 #endif
821     AFS_GLOCK();
822     osi_FlushPages(tvc, vop_cred);      /* hold bozon lock, but not basic vnode lock */
823     code = afs_read(tvc, uio, cred, 0, 0, 0);
824     if (code == 0) {
825         ObtainWriteLock(&tvc->lock, 2);
826         tvc->f.states |= CMAPPED;
827         ReleaseWriteLock(&tvc->lock);
828     }
829     AFS_GUNLOCK();
830
831     /* Zero out rest of last page if there wasn't enough data in the file */
832     if (code == 0 && AFS_UIO_RESID(uio) > 0) {
833 #ifdef AFS_DARWIN80_ENV
834         memset(((caddr_t)ioaddr) + (size - AFS_UIO_RESID(uio)), 0,
835                AFS_UIO_RESID(uio));
836 #else
837         memset(aiov.iov_base, 0, auio.uio_resid);
838 #endif
839     }
840
841     OSI_UPL_UNMAP(pl);
842     if (!nocommit) {
843         if (code)
844             OSI_UPL_ABORT_RANGE(pl, pl_offset, size,
845                                    UPL_ABORT_ERROR | UPL_ABORT_FREE_ON_EMPTY);
846         else
847             OSI_UPL_COMMIT_RANGE(pl, pl_offset, size,
848                                     UPL_COMMIT_CLEAR_DIRTY |
849                                     UPL_COMMIT_FREE_ON_EMPTY);
850     }
851 #ifdef AFS_DARWIN80_ENV
852     uio_free(uio);
853 #endif
854     return code;
855 }
856
857 int
858 afs_vop_write(ap)
859      struct VOPPROT(write_args) /* {
860                                  * struct vnode *a_vp;
861                                  * struct uio *a_uio;
862                                  * int a_ioflag;
863                                  * struct ucred *a_cred;
864                                  * } */ *ap;
865 {
866     int code;
867     struct vcache *avc = VTOAFS(ap->a_vp);
868     void *object;
869 #ifdef AFS_DARWIN80_ENV
870     ubc_msync_range(ap->a_vp, AFS_UIO_OFFSET(ap->a_uio), AFS_UIO_OFFSET(ap->a_uio) + AFS_UIO_RESID(ap->a_uio), UBC_INVALIDATE);
871 #else
872     if (UBCINFOEXISTS(ap->a_vp)) {
873         ubc_clean(ap->a_vp, 1);
874     }
875     if (UBCINFOEXISTS(ap->a_vp))
876         osi_VM_NukePages(ap->a_vp, AFS_UIO_OFFSET(ap->a_uio),
877                          AFS_UIO_RESID(ap->a_uio));
878 #endif
879     AFS_GLOCK();
880     osi_FlushPages(avc, vop_cred);      /* hold bozon lock, but not basic vnode lock */
881     code =
882         afs_write(VTOAFS(ap->a_vp), ap->a_uio, ap->a_ioflag, vop_cred, 0);
883     AFS_GUNLOCK();
884     return code;
885 }
886
887 int
888 afs_vop_pageout(ap)
889      struct VOPPROT(pageout_args)       /* {
890                                  * struct vnode *a_vp;
891                                  * upl_t   a_pl,
892                                  * vm_offset_t   a_pl_offset,
893                                  * off_t         a_f_offset,
894                                  * size_t        a_size,
895                                  * struct ucred *a_cred,
896                                  * int           a_flags
897                                  * } */ *ap;
898 {
899     register struct vnode *vp = ap->a_vp;
900     upl_t pl = ap->a_pl;
901     size_t size = ap->a_size;
902     off_t f_offset = ap->a_f_offset;
903     vm_offset_t pl_offset = ap->a_pl_offset;
904     int flags = ap->a_flags;
905     struct ucred *cred;
906     vm_offset_t ioaddr;
907 #ifdef AFS_DARWIN80_ENV
908     struct uio *uio;
909 #else
910     struct uio auio;
911     struct iovec aiov;
912     struct uio *uio = &auio;
913 #endif
914     int nocommit = flags & UPL_NOCOMMIT;
915     int iosize;
916
917     int code;
918     struct vcache *tvc = VTOAFS(vp);
919 #ifndef AFS_DARWIN80_ENV
920     if (UBCINVALID(vp)) {
921 #if DIAGNOSTIC
922         panic("afs_vop_pageout: invalid vp");
923 #endif /* DIAGNOSTIC */
924         return (EPERM);
925     }
926
927     UBCINFOCHECK("afs_vop_pageout", vp);
928 #endif
929     if (pl == (upl_t) NULL) {
930         panic("afs_vop_pageout: no upl");
931     }
932 #if !defined(AFS_DARWIN80_ENV) /* XXX nfs now uses it's own bufs (struct nfsbuf)
933                                   maybe the generic
934                                   layer doesn't have them anymore? In any case,
935                                   we can't just copy code from nfs... */
936     {
937         int lbn, s;
938         struct buf *bp;
939         int biosize = DEV_BSIZE;
940
941         lbn = f_offset / DEV_BSIZE;
942
943         for (iosize = size; iosize > 0; iosize -= biosize, lbn++) {
944
945             s = splbio();
946             if (bp = incore(vp, lbn)) {
947                 if (ISSET(bp->b_flags, B_BUSY))
948                     panic("nfs_pageout: found BUSY buffer incore\n");
949
950                 bremfree(bp);
951                 SET(bp->b_flags, (B_BUSY | B_INVAL));
952                 brelse(bp);
953             }
954             splx(s);
955         }
956     }
957 #endif
958     cred = ubc_getcred(vp);
959     if (cred == NOCRED)
960         cred = vop_cred;
961
962     if (size == 0) {
963         if (!nocommit)
964             OSI_UPL_ABORT_RANGE(pl, pl_offset, size,
965                                    UPL_ABORT_FREE_ON_EMPTY);
966         return (0);
967     }
968     if (flags & (IO_APPEND | IO_SYNC))
969         panic("nfs_pageout: (IO_APPEND | IO_SYNC)");
970     if (f_offset < 0) {
971         if (!nocommit)
972             OSI_UPL_ABORT_RANGE(pl, pl_offset, size,
973                                    UPL_ABORT_FREE_ON_EMPTY);
974         return (EINVAL);
975     }
976     if (f_offset >= tvc->f.m.Length) {
977         if (!nocommit)
978             OSI_UPL_ABORT_RANGE(pl, pl_offset, size,
979                                    UPL_ABORT_FREE_ON_EMPTY);
980         return (EINVAL);
981     }
982
983     if (f_offset & PAGE_MASK)
984         panic("afs_vop_pageout: offset not page aligned");
985
986     /* size will always be a multiple of PAGE_SIZE */
987     /* pageout isn't supposed to extend files */
988     if (f_offset + size > tvc->f.m.Length) 
989         iosize = tvc->f.m.Length - f_offset;
990     else
991         iosize = size;
992
993     if (size > (iosize + (PAGE_SIZE - 1)) & ~PAGE_MASK && !nocommit)  {
994             int iosize_rnd=(iosize + (PAGE_SIZE - 1)) & ~PAGE_MASK;
995             OSI_UPL_ABORT_RANGE(pl, pl_offset + iosize_rnd,
996                                    size - iosize_rnd,
997                                    UPL_ABORT_FREE_ON_EMPTY);
998     }
999     OSI_UPL_MAP(pl, &ioaddr);
1000     ioaddr += pl_offset;
1001 #ifdef AFS_DARWIN80_ENV
1002     uio = uio_create(1, f_offset, UIO_SYSSPACE32, UIO_READ);
1003     uio_addiov(uio, CAST_USER_ADDR_T(ioaddr), size);
1004 #else
1005     auio.uio_iov = &aiov;
1006     auio.uio_iovcnt = 1;
1007     auio.uio_offset = f_offset;
1008     auio.uio_segflg = UIO_SYSSPACE;
1009     auio.uio_rw = UIO_WRITE;
1010     auio.uio_procp = NULL;
1011     auio.uio_resid = aiov.iov_len = iosize;
1012     aiov.iov_base = (caddr_t) ioaddr;
1013 #endif
1014     {
1015         /* USV?
1016          * check for partial page and clear the
1017          * contents past end of the file before
1018          * releasing it in the VM page cache
1019          */
1020         if ((f_offset < tvc->f.m.Length) && (f_offset + size) > tvc->f.m.Length) {
1021             size_t io = tvc->f.m.Length - f_offset;
1022
1023             memset((caddr_t) (ioaddr + pl_offset + io), 0, size - io);
1024         }
1025     }
1026
1027     AFS_GLOCK();
1028     osi_FlushPages(tvc, vop_cred);      /* hold bozon lock, but not basic vnode lock */
1029     ObtainWriteLock(&tvc->lock, 1);
1030     afs_FakeOpen(tvc);
1031     ReleaseWriteLock(&tvc->lock);
1032
1033     code = afs_write(tvc, uio, flags, cred, 0);
1034
1035     ObtainWriteLock(&tvc->lock, 1);
1036     afs_FakeClose(tvc, cred);
1037     ReleaseWriteLock(&tvc->lock);
1038     AFS_GUNLOCK();
1039     OSI_UPL_UNMAP(pl);
1040     if (!nocommit) {
1041         if (code)
1042             OSI_UPL_ABORT_RANGE(pl, pl_offset, size,
1043                                    UPL_ABORT_FREE_ON_EMPTY);
1044         else
1045             OSI_UPL_COMMIT_RANGE(pl, pl_offset, size,
1046                                     UPL_COMMIT_CLEAR_DIRTY |
1047                                     UPL_COMMIT_FREE_ON_EMPTY);
1048     }
1049
1050 #ifdef AFS_DARWIN80_ENV
1051     uio_free(uio);
1052 #endif
1053     return code;
1054 }
1055
1056 int
1057 afs_vop_ioctl(ap)
1058      struct VOPPROT(ioctl_args) /* {
1059                                  * struct vnode *a_vp;
1060                                  * int  a_command;
1061                                  * caddr_t  a_data;
1062                                  * int  a_fflag;
1063                                  * struct ucred *a_cred;
1064                                  * struct proc *a_p;
1065                                  * } */ *ap;
1066 {
1067     struct vcache *tvc = VTOAFS(ap->a_vp);
1068     struct afs_ioctl data;
1069     int error = 0;
1070
1071     /* in case we ever get in here... */
1072
1073     AFS_STATCNT(afs_ioctl);
1074     if (((ap->a_command >> 8) & 0xff) == 'V') {
1075         /* This is a VICEIOCTL call */
1076         AFS_GLOCK();
1077         error = HandleIoctl(tvc, ap->a_command, ap->a_data);
1078         AFS_GUNLOCK();
1079         return (error);
1080     } else {
1081         /* No-op call; just return. */
1082         return (ENOTTY);
1083     }
1084 }
1085
1086 /* ARGSUSED */
1087 int
1088 afs_vop_select(ap)
1089      struct VOPPROT(select_args)        /* {
1090                                  * struct vnode *a_vp;
1091                                  * int  a_which;
1092                                  * int  a_fflags;
1093                                  * struct ucred *a_cred;
1094                                  * struct proc *a_p;
1095                                  * } */ *ap;
1096 {
1097     /*
1098      * We should really check to see if I/O is possible.
1099      */
1100     return (1);
1101 }
1102
1103 /*
1104  * Mmap a file
1105  *
1106  * NB Currently unsupported.
1107  */
1108 /* ARGSUSED */
1109 int
1110 afs_vop_mmap(ap)
1111      struct VOPPROT(mmap_args)  /* {
1112                                  * struct vnode *a_vp;
1113                                  * int  a_fflags;
1114                                  * struct ucred *a_cred;
1115                                  * struct proc *a_p;
1116                                  * } */ *ap;
1117 {
1118     return (EINVAL);
1119 }
1120
1121 int
1122 afs_vop_fsync(ap)
1123      struct VOPPROT(fsync_args) /* {
1124                                  * struct vnode *a_vp;
1125                                  * struct ucred *a_cred;
1126                                  * int a_waitfor;
1127                                  * struct proc *a_p;
1128                                  * } */ *ap;
1129 {
1130     int wait = ap->a_waitfor == MNT_WAIT;
1131     int error;
1132     register struct vnode *vp = ap->a_vp;
1133     int haveGlock = ISAFS_GLOCK();
1134
1135     /* afs_vop_lookup glocks, can call us through vinvalbuf from GetVCache */
1136     if (!haveGlock) AFS_GLOCK();
1137     if (vop_cred)
1138         error = afs_fsync(VTOAFS(vp), vop_cred);
1139     else
1140         error = afs_fsync(VTOAFS(vp), &afs_osi_cred);
1141     if (!haveGlock) AFS_GUNLOCK();
1142     return error;
1143 }
1144
1145 #ifndef AFS_DARWIN80_ENV
1146 int
1147 afs_vop_seek(ap)
1148      struct VOPPROT(seek_args)  /* {
1149                                  * struct vnode *a_vp;
1150                                  * off_t  a_oldoff;
1151                                  * off_t  a_newoff;
1152                                  * struct ucred *a_cred;
1153                                  * } */ *ap;
1154 {
1155     if (ap->a_newoff > ULONG_MAX)       /* AFS doesn't support 64-bit offsets */
1156         return EINVAL;
1157     return (0);
1158 }
1159 #endif
1160
1161 int
1162 afs_vop_remove(ap)
1163      struct VOPPROT(remove_args)        /* {
1164                                  * struct vnode *a_dvp;
1165                                  * struct vnode *a_vp;
1166                                  * struct componentname *a_cnp;
1167                                  * } */ *ap;
1168 {
1169     int error = 0;
1170     register struct vnode *vp = ap->a_vp;
1171     register struct vnode *dvp = ap->a_dvp;
1172
1173 #ifdef AFS_DARWIN80_ENV
1174     if (ap->a_flags & VNODE_REMOVE_NODELETEBUSY) {
1175             /* Caller requested Carbon delete semantics */
1176             if (vnode_isinuse(vp, 0)) {
1177                     return EBUSY;
1178             }
1179     }
1180 #endif
1181
1182     GETNAME();
1183     AFS_GLOCK();
1184     error = afs_remove(VTOAFS(dvp), name, vop_cn_cred);
1185     error = afs_CheckCode(error, NULL, 61);
1186     AFS_GUNLOCK();
1187     cache_purge(vp);
1188     if (!error) {
1189 #ifdef AFS_DARWIN80_ENV
1190         struct vcache *tvc = VTOAFS(vp);
1191         
1192         if (!(tvc->f.states & CUnlinked)) {
1193             ubc_setsize(vp, (off_t)0);
1194             vnode_recycle(vp);
1195         }
1196 #else
1197         /* necessary so we don't deadlock ourselves in vclean */
1198         VOP_UNLOCK(vp, 0, cnp->cn_proc);
1199
1200         /* If crashes continue in ubc_hold, comment this out */
1201         (void)ubc_uncache(vp);
1202 #endif
1203     } else {
1204         /* should check for PRSFS_INSERT and not PRSFS_DELETE, but the
1205            goal here is to deal with Finder's unhappiness with resource
1206            forks that have no resources in a dropbox setting */
1207         if (name[0] == '.' && name[1] == '_' && error == EACCES) 
1208             error = 0;
1209     }
1210
1211 #ifndef AFS_DARWIN80_ENV
1212     vput(dvp);
1213     if (dvp == vp)
1214         vrele(vp);
1215     else
1216         vput(vp);
1217 #endif
1218
1219 #ifndef AFS_DARWIN80_ENV
1220     FREE_ZONE(cnp->cn_pnbuf, cnp->cn_pnlen, M_NAMEI);
1221 #endif
1222     DROPNAME();
1223     return error;
1224 }
1225
1226 int
1227 afs_vop_link(ap)
1228      struct VOPPROT(link_args)  /* {
1229                                  * struct vnode *a_vp;
1230                                  * struct vnode *a_tdvp;
1231                                  * struct componentname *a_cnp;
1232                                  * } */ *ap;
1233 {
1234     int error = 0;
1235     register struct vnode *dvp = ap->a_tdvp;
1236     register struct vnode *vp = ap->a_vp;
1237     struct proc *p;
1238
1239     GETNAME();
1240     p = vop_cn_proc;
1241     if (vnode_isdir(vp)) {
1242         VOP_ABORTOP(vp, cnp);
1243         error = EISDIR;
1244         goto out;
1245     }
1246 #ifndef AFS_DARWIN80_ENV
1247     if (error = vn_lock(vp, LK_EXCLUSIVE, p)) {
1248         VOP_ABORTOP(dvp, cnp);
1249         goto out;
1250     }
1251 #endif
1252     AFS_GLOCK();
1253     error = afs_link(VTOAFS(vp), VTOAFS(dvp), name, vop_cn_cred);
1254     AFS_GUNLOCK();
1255 #ifndef AFS_DARWIN80_ENV
1256     FREE_ZONE(cnp->cn_pnbuf, cnp->cn_pnlen, M_NAMEI);
1257 #endif
1258 #ifndef AFS_DARWIN80_ENV
1259     if (dvp != vp)
1260         VOP_UNLOCK(vp, 0, p);
1261 #endif
1262   out:
1263 #ifndef AFS_DARWIN80_ENV
1264     vput(dvp);
1265 #endif
1266     DROPNAME();
1267     return error;
1268 }
1269
1270 int
1271 afs_vop_rename(ap)
1272      struct VOPPROT(rename_args)        /* {
1273                                  * struct vnode *a_fdvp;
1274                                  * struct vnode *a_fvp;
1275                                  * struct componentname *a_fcnp;
1276                                  * struct vnode *a_tdvp;
1277                                  * struct vnode *a_tvp;
1278                                  * struct componentname *a_tcnp;
1279                                  * } */ *ap;
1280 {
1281     int error = 0;
1282     struct componentname *fcnp = ap->a_fcnp;
1283     char *fname;
1284     struct componentname *tcnp = ap->a_tcnp;
1285     char *tname;
1286     struct vnode *tvp = ap->a_tvp;
1287     register struct vnode *tdvp = ap->a_tdvp;
1288     struct vnode *fvp = ap->a_fvp;
1289     register struct vnode *fdvp = ap->a_fdvp;
1290     struct proc *p; 
1291
1292     p = cn_proc(fcnp);
1293
1294 #ifdef AFS_DARWIN80_ENV
1295     /*
1296      * generic code tests for v_mount equality, so we don't have to, but we
1297      * don't get the multiple-mount "benefits" of the old behavior
1298      * the generic code doesn't do this, so we really should, but all the
1299      * vrele's are wrong...
1300      */
1301 #else
1302     /* Check for cross-device rename.
1303      * For AFS, this means anything not in AFS-space
1304      */
1305     if ((0 != strcmp(tdvp->v_mount->mnt_stat.f_fstypename, "afs")) ||
1306         (tvp && (0 != strcmp(tvp->v_mount->mnt_stat.f_fstypename, "afs")))) {
1307         error = EXDEV;
1308         goto abortit;
1309     }
1310
1311     /*
1312      * if fvp == tvp, we're just removing one name of a pair of
1313      * directory entries for the same element.  convert call into rename.
1314      ( (pinched from NetBSD 1.0's ufs_rename())
1315      */
1316     if (fvp == tvp) {
1317         if (vnode_isdir(fvp)) {
1318             error = EINVAL;
1319           abortit:
1320             VOP_ABORTOP(tdvp, tcnp);    /* XXX, why not in NFS? */
1321             if (tdvp == tvp)
1322                 vrele(tdvp);
1323             else
1324                 vput(tdvp);
1325             if (tvp)
1326                 vput(tvp);
1327             VOP_ABORTOP(fdvp, fcnp);    /* XXX, why not in NFS? */
1328             vrele(fdvp);
1329             vrele(fvp);
1330             return (error);
1331         }
1332
1333         /* Release destination completely. */
1334         VOP_ABORTOP(tdvp, tcnp);
1335         vput(tdvp);
1336         vput(tvp);
1337         /* Delete source. */
1338         vrele(fdvp);
1339         vrele(fvp);
1340         fcnp->cn_flags &= ~MODMASK;
1341         fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1342         if ((fcnp->cn_flags & SAVESTART) == 0)
1343             panic("afs_rename: lost from startdir");
1344         fcnp->cn_nameiop = DELETE;
1345
1346         VREF(fdvp); 
1347         error=relookup(fdvp, &fvp, fcnp);
1348         if (error == 0)
1349             vrele(fdvp);
1350         if (fvp == NULL) {
1351             return (ENOENT);
1352         }
1353         error=VOP_REMOVE(fdvp, fvp, fcnp);
1354         
1355         if (fdvp == fvp)
1356             vrele(fdvp);
1357         else
1358             vput(fdvp);
1359         vput(fvp);
1360         return (error);
1361     }
1362     if (error = vn_lock(fvp, LK_EXCLUSIVE, p))
1363         goto abortit;
1364 #endif
1365
1366     MALLOC(fname, char *, fcnp->cn_namelen + 1, M_TEMP, M_WAITOK);
1367     memcpy(fname, fcnp->cn_nameptr, fcnp->cn_namelen);
1368     fname[fcnp->cn_namelen] = '\0';
1369     MALLOC(tname, char *, tcnp->cn_namelen + 1, M_TEMP, M_WAITOK);
1370     memcpy(tname, tcnp->cn_nameptr, tcnp->cn_namelen);
1371     tname[tcnp->cn_namelen] = '\0';
1372
1373
1374     AFS_GLOCK();
1375     /* XXX use "from" or "to" creds? NFS uses "to" creds */
1376     error =
1377         afs_rename(VTOAFS(fdvp), fname, VTOAFS(tdvp), tname, cn_cred(tcnp));
1378
1379 #if !defined(AFS_DARWIN80_ENV) 
1380     AFS_GUNLOCK();
1381     VOP_UNLOCK(fvp, 0, p);
1382     if (error)
1383         goto abortit;           /* XXX */
1384     if (tdvp == tvp)
1385         vrele(tdvp);
1386     else
1387         vput(tdvp);
1388     if (tvp)
1389         vput(tvp);
1390     vrele(fdvp);
1391     vrele(fvp);
1392 #else
1393 #ifdef notdef
1394     if (error == EXDEV) {
1395         /* The idea would be to have a userspace handler like afsdb to
1396          * run mv as the user, thus:
1397          */
1398         printf("su %d -c /bin/mv /afs/.:mount/%d:%d:%d:%d/%s /afs/.:mount/%d:%d:%d:%d/%s\n",
1399                afs_cr_uid(cn_cred(tcnp)), fvc->f.fid.Cell, fvc->f.fid.Fid.Volume,
1400                fvc->f.fid.Fid.Vnode, fvc->f.fid.Fid.Unique, fname, 
1401                tvc->f.fid.Cell, tvc->f.fid.Fid.Volume, tvc->f.fid.Fid.Vnode, 
1402                tvc->f.fid.Fid.Unique, tname);
1403     }
1404 #endif
1405     AFS_GUNLOCK();
1406
1407     cache_purge(fdvp);
1408     cache_purge(fvp);
1409     cache_purge(tdvp);
1410     if (tvp) {
1411         cache_purge(tvp);
1412         if (!error) {
1413             vnode_recycle(tvp);
1414         }
1415     }
1416     if (!error)
1417         cache_enter(tdvp, fvp, tcnp);
1418 #endif
1419     FREE(fname, M_TEMP);
1420     FREE(tname, M_TEMP);
1421     return error;
1422 }
1423
1424 int
1425 afs_vop_mkdir(ap)
1426      struct VOPPROT(mkdir_args) /* {
1427                                  * struct vnode *a_dvp;
1428                                  * struct vnode **a_vpp;
1429                                  * struct componentname *a_cnp;
1430                                  * struct vattr *a_vap;
1431                                  * } */ *ap;
1432 {
1433     register struct vnode *dvp = ap->a_dvp;
1434     register struct vattr *vap = ap->a_vap;
1435     int error = 0;
1436     struct vcache *vcp;
1437     struct proc *p;
1438
1439     GETNAME();
1440     p = vop_cn_proc;
1441 #if defined(DIAGNOSTIC) && !defined(AFS_DARWIN80_ENV)
1442     if ((cnp->cn_flags & HASBUF) == 0)
1443         panic("afs_vop_mkdir: no name");
1444 #endif
1445     AFS_GLOCK();
1446     error = afs_mkdir(VTOAFS(dvp), name, vap, &vcp, vop_cn_cred);
1447     AFS_GUNLOCK();
1448     if (error) {
1449 #ifndef AFS_DARWIN80_ENV
1450         VOP_ABORTOP(dvp, cnp);
1451         vput(dvp);
1452 #endif
1453         DROPNAME();
1454         return (error);
1455     }
1456     if (vcp) {
1457 #ifdef AFS_DARWIN80_ENV
1458         afs_darwin_finalizevnode(vcp, ap->a_dvp, ap->a_cnp, 0, 0);
1459 #endif
1460         *ap->a_vpp = AFSTOV(vcp);
1461 #ifndef AFS_DARWIN80_ENV /* XXX needed for multi-mount thing, but can't have it yet */
1462         (*ap->a_vpp)->v_vfsp = dvp->v_vfsp;
1463         vn_lock(*ap->a_vpp, LK_EXCLUSIVE | LK_RETRY, p);
1464 #endif
1465     } else
1466         *ap->a_vpp = 0;
1467     DROPNAME();
1468 #ifndef AFS_DARWIN80_ENV
1469     FREE_ZONE(cnp->cn_pnbuf, cnp->cn_pnlen, M_NAMEI);
1470     vput(dvp);
1471 #endif
1472     return error;
1473 }
1474
1475 int
1476 afs_vop_rmdir(ap)
1477      struct VOPPROT(rmdir_args) /* {
1478                                  * struct vnode *a_dvp;
1479                                  * struct vnode *a_vp;
1480                                  * struct componentname *a_cnp;
1481                                  * } */ *ap;
1482 {
1483     int error = 0;
1484     register struct vnode *vp = ap->a_vp;
1485     register struct vnode *dvp = ap->a_dvp;
1486
1487     GETNAME();
1488     if (dvp == vp) {
1489 #ifndef AFS_DARWIN80_ENV
1490         vrele(dvp);
1491         vput(vp);
1492         FREE_ZONE(cnp->cn_pnbuf, cnp->cn_pnlen, M_NAMEI);
1493 #endif
1494         DROPNAME();
1495         return (EINVAL);
1496     }
1497
1498     AFS_GLOCK();
1499     error = afs_rmdir(VTOAFS(dvp), name, vop_cn_cred);
1500     AFS_GUNLOCK();
1501     DROPNAME();
1502     cache_purge(dvp);
1503     cache_purge(vp);
1504 #ifndef AFS_DARWIN80_ENV
1505     vput(dvp);
1506     vput(vp);
1507 #endif
1508     return error;
1509 }
1510
1511 int
1512 afs_vop_symlink(ap)
1513      struct VOPPROT(symlink_args)       /* {
1514                                  * struct vnode *a_dvp;
1515                                  * struct vnode **a_vpp;
1516                                  * struct componentname *a_cnp;
1517                                  * struct vattr *a_vap;
1518                                  * char *a_target;
1519                                  * } */ *ap;
1520 {
1521     register struct vnode *dvp = ap->a_dvp;
1522     int error = 0;
1523     /* NFS ignores a_vpp; so do we. */
1524
1525     GETNAME();
1526     AFS_GLOCK();
1527     error =
1528         afs_symlink(VTOAFS(dvp), name, ap->a_vap, ap->a_target, vop_cn_cred);
1529     AFS_GUNLOCK();
1530     DROPNAME();
1531 #ifndef AFS_DARWIN80_ENV
1532     FREE_ZONE(cnp->cn_pnbuf, cnp->cn_pnlen, M_NAMEI);
1533     vput(dvp);
1534 #endif
1535     return error;
1536 }
1537
1538 int
1539 afs_vop_readdir(ap)
1540      struct VOPPROT(readdir_args)       /* {
1541                                  * struct vnode *a_vp;
1542                                  * struct uio *a_uio;
1543                                  * struct ucred *a_cred;
1544                                  * int *a_eofflag;
1545                                  * u_long *a_cookies;
1546                                  * int ncookies;
1547                                  * } */ *ap;
1548 {
1549     int error;
1550     off_t off;
1551 /*    printf("readdir %x cookies %x ncookies %d\n", ap->a_vp, ap->a_cookies,
1552            ap->a_ncookies); */
1553 #ifdef AFS_DARWIN80_ENV
1554     /* too much work for now */
1555     /* should only break nfs exports */
1556     if (ap->a_flags & (VNODE_READDIR_EXTENDED | VNODE_READDIR_REQSEEKOFF))
1557          return (EINVAL);
1558 #endif
1559     off = AFS_UIO_OFFSET(ap->a_uio);
1560     AFS_GLOCK();
1561     error =
1562         afs_readdir(VTOAFS(ap->a_vp), ap->a_uio, vop_cred, ap->a_eofflag);
1563     AFS_GUNLOCK();
1564 #ifndef AFS_DARWIN80_ENV
1565     if (!error && ap->a_ncookies != NULL) {
1566         struct uio *uio = ap->a_uio;
1567         const struct dirent *dp, *dp_start, *dp_end;
1568         int ncookies;
1569         u_long *cookies, *cookiep;
1570
1571         if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1572             panic("afs_readdir: burned cookies");
1573         dp = (const struct dirent *)
1574             ((const char *)uio->uio_iov->iov_base - (uio->uio_offset - off));
1575
1576         dp_end = (const struct dirent *)uio->uio_iov->iov_base;
1577         for (dp_start = dp, ncookies = 0; dp < dp_end;
1578              dp = (const struct dirent *)((const char *)dp + dp->d_reclen))
1579             ncookies++;
1580
1581         MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP,
1582                M_WAITOK);
1583         for (dp = dp_start, cookiep = cookies; dp < dp_end;
1584              dp = (const struct dirent *)((const char *)dp + dp->d_reclen)) {
1585             off += dp->d_reclen;
1586             *cookiep++ = off;
1587         }
1588         *ap->a_cookies = cookies;
1589         *ap->a_ncookies = ncookies;
1590     }
1591 #endif
1592
1593     return error;
1594 }
1595
1596 int
1597 afs_vop_readlink(ap)
1598      struct VOPPROT(readlink_args)      /* {
1599                                  * struct vnode *a_vp;
1600                                  * struct uio *a_uio;
1601                                  * struct ucred *a_cred;
1602                                  * } */ *ap;
1603 {
1604     int error;
1605 /*    printf("readlink %x\n", ap->a_vp);*/
1606     AFS_GLOCK();
1607     error = afs_readlink(VTOAFS(ap->a_vp), ap->a_uio, vop_cred);
1608     AFS_GUNLOCK();
1609     return error;
1610 }
1611
1612 extern int prtactive;
1613
1614 int
1615 afs_vop_inactive(ap)
1616      struct VOPPROT(inactive_args)      /* {
1617                                  * struct vnode *a_vp;
1618                                  * struct proc *a_p;
1619                                  * } */ *ap;
1620 {
1621     register struct vnode *vp = ap->a_vp;
1622     struct vcache *tvc = VTOAFS(vp);
1623 #ifndef AFS_DARWIN80_ENV
1624     if (prtactive && vp->v_usecount != 0)
1625         vprint("afs_vop_inactive(): pushing active", vp);
1626 #endif
1627     if (tvc) {
1628 #ifdef AFS_DARWIN80_ENV
1629         int unlinked = tvc->f.states & CUnlinked;
1630 #endif
1631         AFS_GLOCK();
1632         afs_InactiveVCache(tvc, 0);     /* decrs ref counts */
1633         AFS_GUNLOCK();
1634 #ifdef AFS_DARWIN80_ENV
1635         if (unlinked) {
1636             vnode_recycle(vp);
1637             cache_purge(vp);
1638         }
1639 #endif
1640     }
1641 #ifndef AFS_DARWIN80_ENV
1642     VOP_UNLOCK(vp, 0, ap->a_p);
1643 #endif
1644     return 0;
1645 }
1646
1647 int
1648 afs_vop_reclaim(ap)
1649      struct VOPPROT(reclaim_args)       /* {
1650                                  * struct vnode *a_vp;
1651                                  * } */ *ap;
1652 {
1653     int error = 0;
1654     int sl, writelocked;
1655     register struct vnode *vp = ap->a_vp;
1656     struct vcache *tvc = VTOAFS(vp);
1657
1658     osi_Assert(!ISAFS_GLOCK());
1659     cache_purge(vp);            /* just in case... */
1660     if (tvc) {
1661        AFS_GLOCK();
1662        writelocked = (0 == NBObtainWriteLock(&afs_xvcache, 335));
1663        if (!writelocked) {
1664            ObtainWriteLock(&afs_xvreclaim, 176);
1665 #ifdef AFS_DARWIN80_ENV
1666            vnode_clearfsnode(AFSTOV(tvc));
1667            vnode_removefsref(AFSTOV(tvc));
1668 #else
1669            tvc->v->v_data = NULL;  /* remove from vnode */
1670 #endif
1671            AFSTOV(tvc) = NULL;             /* also drop the ptr to vnode */
1672            tvc->f.states |= CVInit; /* also CDeadVnode? */
1673            tvc->nextfree = ReclaimedVCList;
1674            ReclaimedVCList = tvc;
1675            ReleaseWriteLock(&afs_xvreclaim);
1676        } else {
1677            error = afs_FlushVCache(tvc, &sl);   /* toss our stuff from vnode */
1678            if (tvc->f.states & (CVInit
1679 #ifdef AFS_DARWIN80_ENV
1680                               | CDeadVnode
1681 #endif
1682                    )) {
1683                tvc->f.states &= ~(CVInit
1684 #ifdef AFS_DARWIN80_ENV
1685                                 | CDeadVnode
1686 #endif
1687                    );
1688                afs_osi_Wakeup(&tvc->f.states);
1689            }
1690            if (!error && vnode_fsnode(vp))
1691                panic("afs_reclaim: vnode not cleaned");
1692            if (!error && (tvc->v != NULL)) 
1693                panic("afs_reclaim: vcache not cleaned");
1694            ReleaseWriteLock(&afs_xvcache);
1695        }
1696        AFS_GUNLOCK();
1697     }
1698     return error;
1699 }
1700
1701 /*
1702  * Return POSIX pathconf information applicable to ufs filesystems.
1703  */
1704 afs_vop_pathconf(ap)
1705      struct VOPPROT(pathconf_args)      /* {
1706                                  * struct vnode *a_vp;
1707                                  * int a_name;
1708                                  * int *a_retval;
1709                                  * } */ *ap;
1710 {
1711     AFS_STATCNT(afs_cntl);
1712     switch (ap->a_name) {
1713     case _PC_LINK_MAX:
1714         *ap->a_retval = LINK_MAX;
1715         break;
1716     case _PC_NAME_MAX:
1717         *ap->a_retval = NAME_MAX;
1718         break;
1719     case _PC_PATH_MAX:
1720         *ap->a_retval = PATH_MAX;
1721         break;
1722     case _PC_CHOWN_RESTRICTED:
1723         *ap->a_retval = 1;
1724         break;
1725     case _PC_NO_TRUNC:
1726         *ap->a_retval = 1;
1727         break;
1728     case _PC_PIPE_BUF:
1729         return EINVAL;
1730         break;
1731     case _PC_NAME_CHARS_MAX:
1732         *ap->a_retval = NAME_MAX;
1733         break;
1734     case _PC_CASE_SENSITIVE:
1735         *ap->a_retval = 1;
1736         break;
1737     case _PC_CASE_PRESERVING:
1738         *ap->a_retval = 1;
1739         break;
1740     default:
1741         return EINVAL;
1742     }
1743     return 0;
1744 }
1745
1746 /*
1747  * Advisory record locking support (fcntl() POSIX style)
1748  */
1749 int
1750 afs_vop_advlock(ap)
1751      struct VOPPROT(advlock_args)       /* {
1752                                  * struct vnode *a_vp;
1753                                  * caddr_t  a_id;
1754                                  * int  a_op;
1755                                  * struct flock *a_fl;
1756                                  * int  a_flags;
1757                                  * } */ *ap;
1758 {
1759     int error;
1760     struct ucred *tcr;
1761     int clid;
1762     int op;
1763 #ifdef AFS_DARWIN80_ENV
1764     proc_t p;
1765     tcr=vop_cred;
1766 #else
1767     struct proc *p = current_proc();
1768     struct ucred cr;
1769     pcred_readlock(p);
1770     cr = *p->p_cred->pc_ucred;
1771     pcred_unlock(p);
1772     tcr=&cr;
1773 #endif
1774     if (ap->a_flags & F_POSIX) {
1775 #ifdef AFS_DARWIN80_ENV
1776         p = (proc_t) ap->a_id;
1777         clid = proc_pid(p);
1778 #else
1779         p = (struct proc *) ap->a_id;
1780         clid = p->p_pid;
1781 #endif
1782     } else {
1783         clid = (int)ap->a_id;
1784     }
1785     if (ap->a_op == F_UNLCK) {
1786         op = F_SETLK;
1787     } else if (ap->a_op == F_SETLK && ap->a_flags & F_WAIT) {
1788         op = F_SETLKW;
1789     } else {
1790         op = ap->a_op;
1791     }
1792     AFS_GLOCK();
1793     error = afs_lockctl(VTOAFS(ap->a_vp), ap->a_fl, op, tcr, clid);
1794     AFS_GUNLOCK();
1795     return error;
1796 }
1797
1798 int
1799 afs_vop_blktooff(ap)
1800      struct VOPPROT(blktooff_args)      /* {
1801                                  * struct vnode *a_vp;
1802                                  * daddr_t a_lblkno;
1803                                  * off_t *a_offset;    
1804                                  * } */ *ap;
1805 {
1806     *ap->a_offset = (off_t) (ap->a_lblkno * DEV_BSIZE);
1807     return 0;
1808 }
1809
1810 int
1811 afs_vop_offtoblk(ap)
1812      struct VOPPROT(offtoblk_args)      /* {
1813                                  * struct vnode *a_vp;
1814                                  * off_t a_offset;    
1815                                  * daddr_t *a_lblkno;
1816                                  * } */ *ap;
1817 {
1818     *ap->a_lblkno = (daddr_t) (ap->a_offset / DEV_BSIZE);
1819
1820     return (0);
1821 }
1822
1823 #ifndef AFS_DARWIN80_ENV
1824 int
1825 afs_vop_lock(ap)
1826      struct VOPPROT(lock_args)  /* {
1827                                  * struct vnode *a_vp;
1828                                  * } */ *ap;
1829 {
1830     register struct vnode *vp = ap->a_vp;
1831     register struct vcache *avc = VTOAFS(vp);
1832
1833     if (vp->v_tag == VT_NON)
1834         return (ENOENT);
1835
1836     return (lockmgr(&avc->rwlock, ap->a_flags, &vp->v_interlock, ap->a_p));
1837 }
1838
1839 int
1840 afs_vop_unlock(ap)
1841      struct VOPPROT(unlock_args)        /* {
1842                                  * struct vnode *a_vp;
1843                                  * } */ *ap;
1844 {
1845     struct vnode *vp = ap->a_vp;
1846     struct vcache *avc = VTOAFS(vp);
1847
1848     return (lockmgr
1849             (&avc->rwlock, ap->a_flags | LK_RELEASE, &vp->v_interlock,
1850              ap->a_p));
1851
1852 }
1853
1854 int
1855 afs_vop_truncate(ap)
1856      struct VOPPROT(truncate_args)      /* {
1857                                  * struct vnode *a_vp;
1858                                  * off_t a_length;
1859                                  * int a_flags;
1860                                  * struct ucred *a_cred;
1861                                  * struct proc *a_p;
1862                                  * } */ *ap;
1863 {
1864     /* printf("stray afs_vop_truncate\n"); */
1865     return EOPNOTSUPP;
1866 }
1867
1868 int
1869 afs_vop_update(ap)
1870      struct VOPPROT(update_args)        /* {
1871                                  * struct vnode *a_vp;
1872                                  * struct timeval *a_access;
1873                                  * struct timeval *a_modify;
1874                                  * int a_waitfor;
1875                                  * } */ *ap;
1876 {
1877     /* printf("stray afs_vop_update\n"); */
1878     return EOPNOTSUPP;
1879 }
1880
1881 int
1882 afs_vop_bmap(ap)
1883      struct VOPPROT(bmap_args)  /* {
1884                                  * struct vnode *a_vp;
1885                                  * daddr_t  a_bn;
1886                                  * struct vnode **a_vpp;
1887                                  * daddr_t *a_bnp;
1888                                  * int *a_runp;
1889                                  * int *a_runb;
1890                                  * } */ *ap;
1891 {
1892     int error;
1893     if (ap->a_bnp) {
1894         *ap->a_bnp = ap->a_bn * (PAGE_SIZE / DEV_BSIZE);
1895     }
1896     if (ap->a_vpp) {
1897         *ap->a_vpp = ap->a_vp;
1898     }
1899     if (ap->a_runp != NULL)
1900         *ap->a_runp = 0;
1901 #ifdef notyet
1902     if (ap->a_runb != NULL)
1903         *ap->a_runb = 0;
1904 #endif
1905
1906     return 0;
1907 }
1908
1909 int
1910 afs_vop_strategy(ap)
1911      struct VOPPROT(strategy_args)      /* {
1912                                  * struct buf *a_bp;
1913                                  * } */ *ap;
1914 {
1915     int error;
1916     AFS_GLOCK();
1917     error = afs_ustrategy(ap->a_bp);
1918     AFS_GUNLOCK();
1919     return error;
1920 }
1921
1922 int
1923 afs_vop_print(ap)
1924      struct VOPPROT(print_args) /* {
1925                                  * struct vnode *a_vp;
1926                                  * } */ *ap;
1927 {
1928     register struct vnode *vp = ap->a_vp;
1929     register struct vcache *vc = VTOAFS(ap->a_vp);
1930     int s = vc->f.states;
1931     printf("tag %d, fid: %ld.%x.%x.%x, opens %d, writers %d", vp->v_tag,
1932            vc->f.fid.Cell, vc->f.fid.Fid.Volume, vc->f.fid.Fid.Vnode,
1933            vc->f.fid.Fid.Unique, vc->opens, vc->execsOrWriters);
1934     printf("\n  states%s%s%s%s%s", (s & CStatd) ? " statd" : "",
1935            (s & CRO) ? " readonly" : "", (s & CDirty) ? " dirty" : "",
1936            (s & CMAPPED) ? " mapped" : "",
1937            (s & CVFlushed) ? " flush in progress" : "");
1938     if (UBCISVALID(vp)) {
1939         printf("\n  UBC: ");
1940         if (UBCINFOEXISTS(vp)) {
1941             printf("exists, ");
1942             printf("refs %d%s%s", vp->v_ubcinfo->ui_refcount,
1943                    ubc_issetflags(vp, UI_HASOBJREF) ? " HASOBJREF" : "",
1944                    ubc_issetflags(vp, UI_WASMAPPED) ? " WASMAPPED" : "");
1945         } else
1946             printf("does not exist");
1947     }
1948     printf("\n");
1949     return 0;
1950 }
1951
1952 int
1953 afs_vop_islocked(ap)
1954      struct VOPPROT(islocked_args)      /* {
1955                                  * struct vnode *a_vp;
1956                                  * } */ *ap;
1957 {
1958     struct vcache *vc = VTOAFS(ap->a_vp);
1959     return lockstatus(&vc->rwlock);
1960 }
1961
1962 int
1963 afs_vop_cmap(ap)
1964      struct VOPPROT(cmap_args)  /* {
1965                                  * struct vnode *a_vp;
1966                                  * off_t a_foffset;    
1967                                  * size_t a_size;
1968                                  * daddr_t *a_bpn;
1969                                  * size_t *a_run;
1970                                  * void *a_poff;
1971                                  * } */ *ap;
1972 {
1973     *ap->a_bpn = (daddr_t) (ap->a_foffset / DEV_BSIZE);
1974     *ap->a_run = MAX(ap->a_size, AFS_CHUNKSIZE(ap->a_foffset));
1975     return 0;
1976 }
1977 #endif
1978
1979 int
1980 afs_darwin_getnewvnode(struct vcache *avc, int recycle)
1981 {
1982 #ifdef AFS_DARWIN80_ENV
1983     vnode_t vp;
1984     int error, dead;
1985     struct vnode_fsparam par;
1986
1987     memset(&par, 0, sizeof(struct vnode_fsparam));
1988     par.vnfs_vtype = VNON;
1989     par.vnfs_vops = afs_dead_vnodeop_p;
1990     par.vnfs_flags = VNFS_NOCACHE|VNFS_CANTCACHE;
1991     par.vnfs_mp = afs_globalVFS;
1992     par.vnfs_fsnode = avc;
1993
1994     error = vnode_create(VNCREATE_FLAVOR, VCREATESIZE, &par, &vp);
1995     if (!error) {
1996       vnode_addfsref(vp);
1997       vnode_ref(vp);
1998       avc->v = vp;
1999       if (recycle)
2000           vnode_recycle(vp); /* terminate as soon as iocount drops */
2001       avc->f.states |= CDeadVnode;
2002     }
2003     return error;
2004 #else
2005     while (getnewvnode(VT_AFS, afs_globalVFS, afs_vnodeop_p, &avc->v)) {
2006         /* no vnodes available, force an alloc (limits be damned)! */
2007         printf("failed to get vnode\n");
2008     }
2009     avc->v->v_data = (void *)avc;
2010     return 0;
2011 #endif
2012 }
2013 #ifdef AFS_DARWIN80_ENV
2014 /* if this fails, then tvc has been unrefed and may have been freed. 
2015    Don't touch! */
2016 int 
2017 afs_darwin_finalizevnode(struct vcache *avc, struct vnode *dvp, struct componentname *cnp, int isroot, int locked)
2018 {
2019     vnode_t ovp;
2020     vnode_t nvp;
2021     int error;
2022     struct vnode_fsparam par;
2023     if (!locked) {
2024         AFS_GLOCK();
2025         ObtainWriteLock(&avc->lock,325);
2026     }
2027     ovp = AFSTOV(avc);
2028     if (!(avc->f.states & CDeadVnode) && vnode_vtype(ovp) != VNON) {
2029         AFS_GUNLOCK();
2030 #if 0 /* unsupported */
2031         if (dvp && cnp)
2032             vnode_update_identity(ovp, dvp, cnp->cn_nameptr, cnp->cn_namelen,
2033                                   cnp->cn_hash,
2034                                   VNODE_UPDATE_PARENT|VNODE_UPDATE_NAME);
2035 #endif
2036         /* Can end up in reclaim... drop GLOCK */
2037         vnode_rele(ovp);
2038         AFS_GLOCK();
2039         if (!locked) {
2040             ReleaseWriteLock(&avc->lock);
2041             AFS_GUNLOCK();
2042         }
2043         return 0;
2044     }
2045     if ((avc->f.states & CDeadVnode) && vnode_vtype(ovp) != VNON)
2046         panic("vcache %p should not be CDeadVnode", avc);
2047     AFS_GUNLOCK();
2048     memset(&par, 0, sizeof(struct vnode_fsparam));
2049     par.vnfs_mp = afs_globalVFS;
2050     par.vnfs_vtype = avc->f.m.Type;
2051     par.vnfs_vops = afs_vnodeop_p;
2052     par.vnfs_filesize = avc->f.m.Length;
2053     par.vnfs_fsnode = avc;
2054     par.vnfs_dvp = dvp;
2055     if (cnp && (cnp->cn_flags & ISDOTDOT) == 0)
2056         par.vnfs_cnp = cnp;
2057     if (!dvp || !cnp || (cnp->cn_flags & MAKEENTRY) == 0)
2058         par.vnfs_flags = VNFS_NOCACHE;
2059     if (isroot)
2060         par.vnfs_markroot = 1;
2061     error = vnode_create(VNCREATE_FLAVOR, VCREATESIZE, &par, &nvp);
2062     if (!error) {
2063         vnode_addfsref(nvp);
2064         if ((avc->f.states & CDeadVnode) && vnode_vtype(ovp) != VNON)
2065             printf("vcache %p should not be CDeadVnode", avc);
2066         if (avc->v == ovp) {
2067             if (avc->f.states & CBulkFetching) {
2068                 vnode_recycle(ovp);
2069             }
2070             if (!(avc->f.states & CVInit)) {
2071                 vnode_clearfsnode(ovp);
2072                 vnode_removefsref(ovp);
2073             }
2074         }
2075         avc->v = nvp;
2076         avc->f.states &=~ CDeadVnode;
2077     }
2078     vnode_put(ovp);
2079     vnode_rele(ovp);
2080     AFS_GLOCK();
2081     if (!locked)
2082         ReleaseWriteLock(&avc->lock);
2083     if (!error)
2084         afs_osi_Wakeup(&avc->f.states);
2085     if (!locked)
2086         AFS_GUNLOCK();
2087     return error;
2088 }
2089 #endif