bozo: Introduce bnode_Wait()
[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     {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     {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     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     /*
291      * ._ file attribute mirroring touches this.
292      * we can't flag the vcache as there is none, so fail here.
293      * needed for fsevents support.
294      */
295     if (ap->a_context == afs_osi_ctxtp)
296         return ENOENT;
297     if (vcp->mvstat != AFS_MVSTAT_MTPT) {
298         error = cache_lookup(ap->a_dvp, ap->a_vpp, ap->a_cnp);
299         if (error == -1) 
300             return 0;
301         if (error == ENOENT) 
302             return error;
303     }
304 #endif
305
306     GETNAME();
307     p = vop_cn_proc;
308
309     lockparent = flags & LOCKPARENT;
310     wantparent = flags & (LOCKPARENT | WANTPARENT);
311
312     if (!vnode_isdir(ap->a_dvp)) {
313         *ap->a_vpp = 0;
314         DROPNAME();
315         return ENOTDIR;
316     }
317     dvp = ap->a_dvp;
318 #ifndef AFS_DARWIN80_ENV
319     if (flags & ISDOTDOT)
320         VOP_UNLOCK(dvp, 0, p);
321 #endif
322     AFS_GLOCK();
323     error = afs_lookup(VTOAFS(dvp), name, &vcp, vop_cn_cred);
324     AFS_GUNLOCK();
325     if (error) {
326 #ifndef AFS_DARWIN80_ENV
327         if (flags & ISDOTDOT)
328             VOP_LOCK(dvp, LK_EXCLUSIVE | LK_RETRY, p);
329 #endif
330         if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME)
331             && (flags & ISLASTCN) && error == ENOENT)
332             error = EJUSTRETURN;
333 #ifndef AFS_DARWIN80_ENV
334         if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
335             cnp->cn_flags |= SAVENAME;
336 #endif
337         DROPNAME();
338         *ap->a_vpp = 0;
339         return (error);
340     }
341 #ifdef AFS_DARWIN80_ENV
342     if ((error=afs_darwin_finalizevnode(vcp, ap->a_dvp, ap->a_cnp, 0, 0))) {
343         DROPNAME();
344         *ap->a_vpp = 0;
345         return error;
346     }
347 #endif
348     vp = AFSTOV(vcp);           /* always get a node if no error */
349 #ifndef AFS_DARWIN80_ENV /* XXX needed for multi-mount thing, but can't have it yet */
350     vp->v_vfsp = dvp->v_vfsp;
351
352     if (UBCINFOMISSING(vp) ||
353         UBCINFORECLAIMED(vp)) {
354             ubc_info_init(vp);
355     }
356 #endif
357
358 #ifndef AFS_DARWIN80_ENV
359     /* The parent directory comes in locked.  We unlock it on return
360      * unless the caller wants it left locked.
361      * we also always return the vnode locked. */
362
363     if (flags & ISDOTDOT) {
364         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
365         /* always return the child locked */
366         if (lockparent && (flags & ISLASTCN)
367             && (error = vn_lock(dvp, LK_EXCLUSIVE, p))) {
368             vput(vp);
369             DROPNAME();
370             return (error);
371         }
372     } else if (vp == dvp) {
373         /* they're the same; afs_lookup() already ref'ed the leaf.
374          * It came in locked, so we don't need to ref OR lock it */
375     } else {
376         if (!lockparent || !(flags & ISLASTCN))
377             VOP_UNLOCK(dvp, 0, p);      /* done with parent. */
378         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
379         /* always return the child locked */
380     }
381 #endif
382     *ap->a_vpp = vp;
383
384 #ifndef AFS_DARWIN80_ENV
385     if ((cnp->cn_nameiop == RENAME && wantparent && (flags & ISLASTCN)
386          || (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))))
387         cnp->cn_flags |= SAVENAME;
388 #endif
389
390     DROPNAME();
391     return error;
392 }
393
394 int
395 afs_vop_create(ap)
396      struct VOPPROT(create_args)        /* {
397                                  * struct vnode *a_dvp;
398                                  * struct vnode **a_vpp;
399                                  * struct componentname *a_cnp;
400                                  * struct vattr *a_vap;
401                                  * } */ *ap;
402 {
403     int error = 0;
404     struct vcache *vcp;
405     struct vnode *dvp = ap->a_dvp;
406     struct proc *p;
407     GETNAME();
408     p = vop_cn_proc;
409
410     /* vnode layer handles excl/nonexcl */
411     AFS_GLOCK();
412     error =
413         afs_create(VTOAFS(dvp), name, ap->a_vap, NONEXCL, ap->a_vap->va_mode,
414                    &vcp, vop_cn_cred);
415     AFS_GUNLOCK();
416     if (error) {
417 #ifndef AFS_DARWIN80_ENV
418         VOP_ABORTOP(dvp, cnp);
419         vput(dvp);
420 #endif
421         DROPNAME();
422         return (error);
423     }
424
425     if (vcp) {
426 #ifdef AFS_DARWIN80_ENV
427       if ((error=afs_darwin_finalizevnode(vcp, ap->a_dvp, ap->a_cnp, 0, 0))) {
428             DROPNAME();
429             *ap->a_vpp=0;
430             return error;
431         }
432 #endif
433         *ap->a_vpp = AFSTOV(vcp);
434 #ifndef AFS_DARWIN80_ENV /* XXX needed for multi-mount thing, but can't have it yet */
435         (*ap->a_vpp)->v_vfsp = dvp->v_vfsp;
436         vn_lock(*ap->a_vpp, LK_EXCLUSIVE | LK_RETRY, p);
437         if (UBCINFOMISSING(*ap->a_vpp) || UBCINFORECLAIMED(*ap->a_vpp)) {
438             vcp->f.states |= CUBCinit;
439             ubc_info_init(*ap->a_vpp);
440             vcp->f.states &= ~CUBCinit;
441         }
442 #endif
443     } else
444         *ap->a_vpp = 0;
445
446 #ifndef AFS_DARWIN80_ENV
447     if ((cnp->cn_flags & SAVESTART) == 0)
448         FREE_ZONE(cnp->cn_pnbuf, cnp->cn_pnlen, M_NAMEI);
449     vput(dvp);
450 #endif
451     DROPNAME();
452     return error;
453 }
454
455 int
456 afs_vop_mknod(ap)
457      struct VOPPROT(mknod_args) /* {
458                                  * struct vnode *a_dvp;
459                                  * struct vnode **a_vpp;
460                                  * struct componentname *a_cnp;
461                                  * struct vattr *a_vap;
462                                  * } */ *ap;
463 {
464 #ifndef AFS_DARWIN80_ENV
465     FREE_ZONE(ap->a_cnp->cn_pnbuf, ap->a_cnp->cn_pnlen, M_NAMEI);
466     vput(ap->a_dvp);
467 #endif
468     return (ENODEV);
469 }
470
471 int
472 afs_vop_open(ap)
473      struct VOPPROT(open_args)  /* {
474                                  * struct vnode *a_vp;
475                                  * int  a_mode;
476                                  * struct ucred *a_cred;
477                                  * struct proc *a_p;
478                                  * } */ *ap;
479 {
480     int error;
481     struct vnode *vp = ap->a_vp;
482     struct vcache *vc = VTOAFS(vp);
483 #if !defined(AFS_DARWIN80_ENV)
484     int didhold = 0;
485     /*----------------------------------------------------------------
486      * osi_VM_TryReclaim() removes the ubcinfo of a vnode, but that vnode
487      * can later be passed to vn_open(), which will skip the call to
488      * ubc_hold(), and when the ubcinfo is later added, the ui_refcount
489      * will be off.  So we compensate by calling ubc_hold() ourselves
490      * when ui_refcount is less than 2.  If an error occurs in afs_open()
491      * we must call ubc_rele(), which is what vn_open() would do if it
492      * was able to call ubc_hold() in the first place.
493      *----------------------------------------------------------------*/
494     if (vp->v_type == VREG && !(vp->v_flag & VSYSTEM)
495       && vp->v_ubcinfo->ui_refcount < 2)
496         didhold = ubc_hold(vp);
497 #endif /* !AFS_DARWIN80_ENV */
498     AFS_GLOCK();
499     error = afs_open(&vc, ap->a_mode, vop_cred);
500 #ifdef DIAGNOSTIC
501     if (AFSTOV(vc) != vp)
502         panic("AFS open changed vnode!");
503 #endif
504     osi_FlushPages(vc, vop_cred);
505     AFS_GUNLOCK();
506 #if !defined(AFS_DARWIN80_ENV)
507     if (error && didhold)
508         ubc_rele(vp);
509 #endif /* !AFS_DARWIN80_ENV */
510     return error;
511 }
512
513 int
514 afs_vop_close(ap)
515      struct VOPPROT(close_args) /* {
516                                  * struct vnode *a_vp;
517                                  * int  a_fflag;
518                                  * struct ucred *a_cred;
519                                  * struct proc *a_p;
520                                  * } */ *ap;
521 {
522     int code;
523     struct vnode *vp = ap->a_vp;
524     struct vcache *avc = VTOAFS(vp);
525     /* allows faking FSE_CONTENT_MODIFIED */
526     if (afs_osi_ctxtp == ap->a_context)
527         return 0;
528     AFS_GLOCK();
529     if (vop_cred)
530         code = afs_close(avc, ap->a_fflag, vop_cred);
531     else
532         code = afs_close(avc, ap->a_fflag, &afs_osi_cred);
533     osi_FlushPages(avc, vop_cred);      /* hold GLOCK, but not basic vnode lock */
534     /* This is legit; it just forces the fstrace event to happen */
535     code = afs_CheckCode(code, NULL, 60);
536     AFS_GUNLOCK();
537
538     return code;
539 }
540
541 #ifdef AFS_DARWIN80_ENV
542 extern int afs_fakestat_enable;
543
544 int
545 afs_vop_access(ap)
546      struct VOPPROT(access_args)        /* {
547                                  * struct vnode *a_vp;
548                                  * int  a_action;
549                                  * vfs_context_t a_context;
550                                  * } */ *ap;
551 {
552     int code;
553     struct vrequest treq;
554     struct afs_fakestat_state fakestate;
555     struct vcache * tvc = VTOAFS(ap->a_vp);
556     int bits=0;
557     int cmb = CHECK_MODE_BITS;
558 #ifdef AFS_DARWIN80_ENV
559     /*
560      * needed for fsevents. ._ file attribute mirroring touches this.
561      * we can't flag the vcache, as there is none, so fail here.
562      */
563     if (ap->a_context == afs_osi_ctxtp)
564         return ENOENT;
565 #endif
566     AFS_GLOCK();
567     afs_InitFakeStat(&fakestate);
568     if ((code = afs_InitReq(&treq, vop_cred)))
569         goto out2;
570
571     code = afs_TryEvalFakeStat(&tvc, &fakestate, &treq);
572     if (code) {
573         code = afs_CheckCode(code, &treq, 55);
574         goto out;
575     }
576
577     code = afs_VerifyVCache(tvc, &treq);
578     if (code) {
579         code = afs_CheckCode(code, &treq, 56);
580         goto out;
581     }
582     if (afs_fakestat_enable && tvc->mvstat != AFS_MVSTAT_FILE && !(tvc->f.states & CStatd)) {
583         code = 0;
584         goto out;
585     }
586     if (vnode_isdir(ap->a_vp)) {
587        if (ap->a_action & KAUTH_VNODE_LIST_DIRECTORY)
588           bits |= PRSFS_LOOKUP;
589        if (ap->a_action & KAUTH_VNODE_ADD_FILE)
590           bits |= PRSFS_INSERT;
591        if (ap->a_action & KAUTH_VNODE_SEARCH)
592           bits |= PRSFS_LOOKUP;
593        if (ap->a_action & KAUTH_VNODE_DELETE)
594           bits |= PRSFS_DELETE;
595        if (ap->a_action & KAUTH_VNODE_ADD_SUBDIRECTORY)
596           bits |= PRSFS_INSERT;
597        if (ap->a_action & KAUTH_VNODE_DELETE_CHILD)
598           bits |= PRSFS_DELETE;
599     } else {
600        if (ap->a_action & KAUTH_VNODE_READ_DATA)
601           bits |= PRSFS_READ;
602        if (ap->a_action & KAUTH_VNODE_WRITE_DATA)
603           bits |= PRSFS_WRITE;
604        if (ap->a_action & KAUTH_VNODE_EXECUTE)
605           bits |= PRSFS_READ; /* and mode bits.... */
606        if (ap->a_action & KAUTH_VNODE_READ_ATTRIBUTES)
607           bits |= PRSFS_LOOKUP;
608        if (ap->a_action & KAUTH_VNODE_READ_SECURITY) /* mode bits/gid, not afs acl */
609           bits |= PRSFS_LOOKUP;
610        if ((ap->a_action & ((1 << 25) - 1)) == KAUTH_VNODE_EXECUTE)
611           /* if only exec, don't check for read mode bit */
612           /* high bits of ap->a_action are not for 'generic rights bits', and
613              so should not be checked (KAUTH_VNODE_ACCESS is often present
614              and needs to be masked off) */
615           cmb |= CMB_ALLOW_EXEC_AS_READ;
616     }
617     if (ap->a_action & KAUTH_VNODE_WRITE_ATTRIBUTES)
618        bits |= PRSFS_WRITE;
619     if (ap->a_action & KAUTH_VNODE_WRITE_SECURITY)
620        bits |= PRSFS_WRITE;
621     /* we can't check for KAUTH_VNODE_TAKE_OWNERSHIP, so we always permit it */
622     
623     code = afs_AccessOK(tvc, bits, &treq, cmb);
624     /*
625      * Special cased dropbox handling:
626      * cp on 10.4 behaves badly, looping on EACCES
627      * Finder may reopen the file. Let it.
628      */
629     if (code == 0 && ((bits &~(PRSFS_READ|PRSFS_WRITE)) == 0))
630         code = afs_AccessOK(tvc, PRSFS_ADMINISTER|PRSFS_INSERT|bits, &treq, cmb);
631     /* Finder also treats dropboxes as insert+delete. fake it out. */
632     if (code == 0 && (bits == (PRSFS_INSERT|PRSFS_DELETE)))
633         code = afs_AccessOK(tvc, PRSFS_INSERT, &treq, cmb);
634
635     if (code == 1 && vnode_vtype(ap->a_vp) == VREG &&
636         ap->a_action & KAUTH_VNODE_EXECUTE &&
637         (tvc->f.m.Mode & 0100) != 0100) {
638         code = 0;
639     }
640     if (code) {
641         code= 0;               /* if access is ok */
642     } else {
643             code = afs_CheckCode(EACCES, &treq, 57);        /* failure code */
644     }
645 out:
646      afs_PutFakeStat(&fakestate);
647 out2:
648     AFS_GUNLOCK();
649     return code;
650 }
651 #else
652 int
653 afs_vop_access(ap)
654      struct VOPPROT(access_args)        /* {
655                                  * struct vnode *a_vp;
656                                  * int  a_mode;
657                                  * struct ucred *a_cred;
658                                  * struct proc *a_p;
659                                  * } */ *ap;
660 {
661     int code;
662     AFS_GLOCK();
663     code = afs_access(VTOAFS(ap->a_vp), ap->a_mode, vop_cred);
664     AFS_GUNLOCK();
665     return code;
666 }
667 #endif
668
669 int
670 afs_vop_getattr(ap)
671      struct VOPPROT(getattr_args)       /* {
672                                  * struct vnode *a_vp;
673                                  * struct vattr *a_vap;
674                                  * struct ucred *a_cred;
675                                  * struct proc *a_p;
676                                  * } */ *ap;
677 {
678     int code;
679
680 #ifdef AFS_DARWIN80_ENV
681     /* CEvent excludes the fsevent. our context excludes the ._ */
682     if ((VTOAFS(ap->a_vp)->f.states & CEvent) ||
683         (ap->a_context == afs_osi_ctxtp)){
684         struct vcache *avc = VTOAFS(ap->a_vp);
685         int isglock = ISAFS_GLOCK();
686
687         /* this is needed because of how and when we re-enter */
688         if (!isglock)
689           AFS_GLOCK();
690         /* do minimal work to return fake result for fsevents */
691         if (afs_fakestat_enable && VTOAFS(ap->a_vp)->mvstat == AFS_MVSTAT_MTPT) {
692             struct afs_fakestat_state fakestat;
693             struct vrequest treq;
694
695             code = afs_InitReq(&treq, vop_cred);
696             if (code) {
697                 if (!isglock)
698                     AFS_GUNLOCK();
699                 return code;
700             }
701             afs_InitFakeStat(&fakestat);
702             /* expects GLOCK */
703             code = afs_TryEvalFakeStat(&avc, &fakestat, &treq);
704             if (code) {
705                 if (!isglock)
706                     AFS_GUNLOCK();
707                 afs_PutFakeStat(&fakestat);
708                 return code;
709             }
710         }
711         code = afs_CopyOutAttrs(avc, ap->a_vap);
712         if (!isglock)
713             AFS_GUNLOCK();
714         if (0 && !code) {
715             /* tweak things so finder will recheck */
716             (ap->a_vap)->va_gid = ((ap->a_vap)->va_gid == 1) ? 2 : 1;
717             (ap->a_vap)->va_mode &= ~(VSGID);
718         }
719     } else
720 #endif
721     {
722         AFS_GLOCK();
723         code = afs_getattr(VTOAFS(ap->a_vp), ap->a_vap, vop_cred);
724         /* This is legit; it just forces the fstrace event to happen */
725         code = afs_CheckCode(code, NULL, 58);
726         AFS_GUNLOCK();
727     }
728 #ifdef AFS_DARWIN80_ENV
729     VATTR_SET_SUPPORTED(ap->a_vap, va_type);
730     VATTR_SET_SUPPORTED(ap->a_vap, va_mode);
731     VATTR_SET_SUPPORTED(ap->a_vap, va_uid);
732     VATTR_SET_SUPPORTED(ap->a_vap, va_gid);
733     VATTR_SET_SUPPORTED(ap->a_vap, va_fsid);
734     VATTR_SET_SUPPORTED(ap->a_vap, va_fileid);
735     VATTR_SET_SUPPORTED(ap->a_vap, va_nlink);
736     VATTR_SET_SUPPORTED(ap->a_vap, va_data_size);
737     VATTR_SET_SUPPORTED(ap->a_vap, va_access_time);
738     VATTR_SET_SUPPORTED(ap->a_vap, va_modify_time);
739     VATTR_SET_SUPPORTED(ap->a_vap, va_change_time);
740     VATTR_SET_SUPPORTED(ap->a_vap, va_gen);
741     VATTR_SET_SUPPORTED(ap->a_vap, va_flags);
742     VATTR_SET_SUPPORTED(ap->a_vap, va_iosize);
743     VATTR_SET_SUPPORTED(ap->a_vap, va_total_alloc);
744 #endif
745     return code;
746 }
747
748 int
749 afs_vop_setattr(ap)
750      struct VOPPROT(setattr_args)       /* {
751                                  * struct vnode *a_vp;
752                                  * struct vattr *a_vap;
753                                  * struct ucred *a_cred;
754                                  * struct proc *a_p;
755                                  * } */ *ap;
756 {
757     int code, pass = 0;
758     struct vcache *avc = VTOAFS(ap->a_vp);
759 #ifdef AFS_DARWIN80_ENV
760     /* fsevents tries to set attributes. drop it. */
761     if (ap->a_context == afs_osi_ctxtp)
762         return 0;
763 #endif
764     AFS_GLOCK();
765 retry:
766     code = afs_setattr(avc, ap->a_vap, vop_cred);
767     /* This is legit; it just forces the fstrace event to happen */
768     code = afs_CheckCode(code, NULL, 59);
769     if (!pass && code == EINVAL && (VATTR_IS_ACTIVE(ap->a_vap, va_mode) &&
770                                     (vType(avc) == VLNK))) {
771         VATTR_CLEAR_ACTIVE(ap->a_vap, va_mode);
772         pass++;
773         goto retry;
774     }
775     AFS_GUNLOCK();
776     return code;
777 }
778
779 int
780 afs_vop_read(ap)
781      struct VOPPROT(read_args)  /* {
782                                  * struct vnode *a_vp;
783                                  * struct uio *a_uio;
784                                  * int a_ioflag;
785                                  * struct ucred *a_cred;
786                                  * } */ *ap;
787 {
788     int code;
789     struct vnode *vp = ap->a_vp;
790     struct vcache *avc = VTOAFS(vp);
791
792     if (vnode_isdir(ap->a_vp)) 
793         return EISDIR;
794 #ifdef AFS_DARWIN80_ENV
795     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);
796 #else
797     if (UBCINFOEXISTS(ap->a_vp)) {
798         ubc_clean(ap->a_vp, 0);
799     }
800 #endif
801     AFS_GLOCK();
802     osi_FlushPages(avc, vop_cred);      /* hold GLOCK, but not basic vnode lock */
803     code = afs_read(avc, ap->a_uio, vop_cred, 0);
804     AFS_GUNLOCK();
805     return code;
806 }
807
808 int
809 afs_vop_pagein(ap)
810      struct VOPPROT(pagein_args)        /* {
811                                  * struct vnode *a_vp;
812                                  * upl_t a_pl;
813                                  * vm_offset_t a_pl_offset;
814                                  * off_t a_f_offset;
815                                  * size_t a_size;
816                                  * struct ucred *a_cred;
817                                  * int a_flags;
818                                  * } */ *ap;
819 {
820     struct vnode *vp = ap->a_vp;
821     upl_t pl = ap->a_pl;
822     size_t size = ap->a_size;
823     off_t f_offset = ap->a_f_offset;
824     vm_offset_t pl_offset = ap->a_pl_offset;
825     int flags = ap->a_flags;
826     struct ucred *cred;
827     vm_offset_t ioaddr;
828     int code;
829     struct vcache *tvc = VTOAFS(vp);
830     int nocommit = flags & UPL_NOCOMMIT;
831 #ifdef AFS_DARWIN80_ENV
832     struct uio *uio;
833 #else
834     struct uio auio;
835     struct iovec aiov;
836     struct uio *uio = &auio;
837
838     memset(&auio, 0, sizeof(auio));
839     memset(&aiov, 0, sizeof(aiov));
840 #endif
841
842 #ifndef AFS_DARWIN80_ENV
843     if (UBCINVALID(vp)) {
844 #if DIAGNOSTIC
845         panic("afs_vop_pagein: invalid vp");
846 #endif /* DIAGNOSTIC */
847         return (EPERM);
848     }
849
850     UBCINFOCHECK("afs_vop_pagein", vp);
851 #endif
852     if (pl == (upl_t) NULL) {
853         panic("afs_vop_pagein: no upl");
854     }
855
856     cred = ubc_getcred(vp);
857     if (cred == NOCRED)
858         cred = vop_cred;
859
860     if (size == 0) {
861         if (!nocommit)
862             OSI_UPL_ABORT_RANGE(pl, pl_offset, size,
863                                    UPL_ABORT_ERROR | UPL_ABORT_FREE_ON_EMPTY);
864         return (0);
865     }
866     if (f_offset < 0) {
867         if (!nocommit)
868             OSI_UPL_ABORT_RANGE(pl, pl_offset, size,
869                                    UPL_ABORT_ERROR | UPL_ABORT_FREE_ON_EMPTY);
870         return (EINVAL);
871     }
872     if (f_offset & PAGE_MASK)
873         panic("afs_vop_pagein: offset not page aligned");
874
875     OSI_UPL_MAP(pl, &ioaddr);
876     ioaddr += pl_offset;
877 #ifdef AFS_DARWIN80_ENV
878     uio = uio_create(1, f_offset, UIO_SYSSPACE32, UIO_READ);
879     uio_addiov(uio, CAST_USER_ADDR_T(ioaddr), size);
880 #else
881     auio.uio_iov = &aiov;
882     auio.uio_iovcnt = 1;
883     auio.uio_offset = f_offset;
884     auio.uio_segflg = UIO_SYSSPACE;
885     auio.uio_rw = UIO_READ;
886     auio.uio_procp = NULL;
887     auio.uio_resid = aiov.iov_len = size;
888     aiov.iov_base = (caddr_t) ioaddr;
889 #endif
890     AFS_GLOCK();
891     osi_FlushPages(tvc, vop_cred);      /* hold GLOCK, but not basic vnode lock */
892     code = afs_read(tvc, uio, cred, 0);
893     if (code == 0) {
894         ObtainWriteLock(&tvc->lock, 2);
895         tvc->f.states |= CMAPPED;
896         ReleaseWriteLock(&tvc->lock);
897     }
898     AFS_GUNLOCK();
899
900     /* Zero out rest of last page if there wasn't enough data in the file */
901     if (code == 0 && AFS_UIO_RESID(uio) > 0) {
902 #ifdef AFS_DARWIN80_ENV
903         memset(((caddr_t)ioaddr) + (size - AFS_UIO_RESID(uio)), 0,
904                AFS_UIO_RESID(uio));
905 #else
906         memset(aiov.iov_base, 0, auio.uio_resid);
907 #endif
908     }
909
910     OSI_UPL_UNMAP(pl);
911     if (!nocommit) {
912         if (code)
913             OSI_UPL_ABORT_RANGE(pl, pl_offset, size,
914                                    UPL_ABORT_ERROR | UPL_ABORT_FREE_ON_EMPTY);
915         else
916             OSI_UPL_COMMIT_RANGE(pl, pl_offset, size,
917                                     UPL_COMMIT_CLEAR_DIRTY |
918                                     UPL_COMMIT_FREE_ON_EMPTY);
919     }
920 #ifdef AFS_DARWIN80_ENV
921     uio_free(uio);
922 #endif
923     return code;
924 }
925
926 int
927 afs_vop_write(ap)
928      struct VOPPROT(write_args) /* {
929                                  * struct vnode *a_vp;
930                                  * struct uio *a_uio;
931                                  * int a_ioflag;
932                                  * struct ucred *a_cred;
933                                  * } */ *ap;
934 {
935     int code;
936     struct vcache *avc = VTOAFS(ap->a_vp);
937     void *object;
938 #ifdef AFS_DARWIN80_ENV
939     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);
940 #else
941     if (UBCINFOEXISTS(ap->a_vp)) {
942         ubc_clean(ap->a_vp, 1);
943     }
944     if (UBCINFOEXISTS(ap->a_vp))
945         osi_VM_NukePages(ap->a_vp, AFS_UIO_OFFSET(ap->a_uio),
946                          AFS_UIO_RESID(ap->a_uio));
947 #endif
948     AFS_GLOCK();
949     osi_FlushPages(avc, vop_cred);      /* hold GLOCK, but not basic vnode lock */
950     code =
951         afs_write(VTOAFS(ap->a_vp), ap->a_uio, ap->a_ioflag, vop_cred, 0);
952     AFS_GUNLOCK();
953     return code;
954 }
955
956 int
957 afs_vop_pageout(ap)
958      struct VOPPROT(pageout_args)       /* {
959                                  * struct vnode *a_vp;
960                                  * upl_t   a_pl,
961                                  * vm_offset_t   a_pl_offset,
962                                  * off_t         a_f_offset,
963                                  * size_t        a_size,
964                                  * struct ucred *a_cred,
965                                  * int           a_flags
966                                  * } */ *ap;
967 {
968     struct vnode *vp = ap->a_vp;
969     upl_t pl = ap->a_pl;
970     size_t size = ap->a_size;
971     off_t f_offset = ap->a_f_offset;
972     vm_offset_t pl_offset = ap->a_pl_offset;
973     int flags = ap->a_flags;
974     struct ucred *cred;
975     vm_offset_t ioaddr;
976     int nocommit = flags & UPL_NOCOMMIT;
977     int iosize;
978     int code;
979     struct vcache *tvc = VTOAFS(vp);
980 #ifdef AFS_DARWIN80_ENV
981     struct uio *uio;
982 #else
983     struct uio auio;
984     struct iovec aiov;
985     struct uio *uio = &auio;
986
987     memset(&auio, 0, sizeof(auio));
988     memset(&aiov, 0, sizeof(aiov));
989 #endif
990
991 #ifndef AFS_DARWIN80_ENV
992     if (UBCINVALID(vp)) {
993 #if DIAGNOSTIC
994         panic("afs_vop_pageout: invalid vp");
995 #endif /* DIAGNOSTIC */
996         return (EPERM);
997     }
998
999     UBCINFOCHECK("afs_vop_pageout", vp);
1000 #endif
1001     if (pl == (upl_t) NULL) {
1002         panic("afs_vop_pageout: no upl");
1003     }
1004 #if !defined(AFS_DARWIN80_ENV) /* XXX nfs now uses it's own bufs (struct nfsbuf)
1005                                   maybe the generic
1006                                   layer doesn't have them anymore? In any case,
1007                                   we can't just copy code from nfs... */
1008     {
1009         int lbn, s;
1010         struct buf *bp;
1011         int biosize = DEV_BSIZE;
1012
1013         lbn = f_offset / DEV_BSIZE;
1014
1015         for (iosize = size; iosize > 0; iosize -= biosize, lbn++) {
1016
1017             s = splbio();
1018             if (bp = incore(vp, lbn)) {
1019                 if (ISSET(bp->b_flags, B_BUSY))
1020                     panic("nfs_pageout: found BUSY buffer incore\n");
1021
1022                 bremfree(bp);
1023                 SET(bp->b_flags, (B_BUSY | B_INVAL));
1024                 brelse(bp);
1025             }
1026             splx(s);
1027         }
1028     }
1029 #endif
1030     cred = ubc_getcred(vp);
1031     if (cred == NOCRED)
1032         cred = vop_cred;
1033
1034     if (size == 0) {
1035         if (!nocommit)
1036             OSI_UPL_ABORT_RANGE(pl, pl_offset, size,
1037                                    UPL_ABORT_FREE_ON_EMPTY);
1038         return (0);
1039     }
1040     if (flags & (IO_APPEND | IO_SYNC))
1041         panic("nfs_pageout: (IO_APPEND | IO_SYNC)");
1042     if (f_offset < 0) {
1043         if (!nocommit)
1044             OSI_UPL_ABORT_RANGE(pl, pl_offset, size,
1045                                    UPL_ABORT_FREE_ON_EMPTY);
1046         return (EINVAL);
1047     }
1048     if (f_offset >= tvc->f.m.Length) {
1049         if (!nocommit)
1050             OSI_UPL_ABORT_RANGE(pl, pl_offset, size,
1051                                    UPL_ABORT_FREE_ON_EMPTY);
1052         return (EINVAL);
1053     }
1054
1055     if (f_offset & PAGE_MASK)
1056         panic("afs_vop_pageout: offset not page aligned");
1057
1058     /* size will always be a multiple of PAGE_SIZE */
1059     /* pageout isn't supposed to extend files */
1060     if (f_offset + size > tvc->f.m.Length) 
1061         iosize = tvc->f.m.Length - f_offset;
1062     else
1063         iosize = size;
1064
1065     if (size > (iosize + (PAGE_SIZE - 1)) & ~PAGE_MASK && !nocommit)  {
1066             int iosize_rnd=(iosize + (PAGE_SIZE - 1)) & ~PAGE_MASK;
1067             OSI_UPL_ABORT_RANGE(pl, pl_offset + iosize_rnd,
1068                                    size - iosize_rnd,
1069                                    UPL_ABORT_FREE_ON_EMPTY);
1070     }
1071     OSI_UPL_MAP(pl, &ioaddr);
1072     ioaddr += pl_offset;
1073 #ifdef AFS_DARWIN80_ENV
1074     uio = uio_create(1, f_offset, UIO_SYSSPACE32, UIO_READ);
1075     uio_addiov(uio, CAST_USER_ADDR_T(ioaddr), size);
1076 #else
1077     auio.uio_iov = &aiov;
1078     auio.uio_iovcnt = 1;
1079     auio.uio_offset = f_offset;
1080     auio.uio_segflg = UIO_SYSSPACE;
1081     auio.uio_rw = UIO_WRITE;
1082     auio.uio_procp = NULL;
1083     auio.uio_resid = aiov.iov_len = iosize;
1084     aiov.iov_base = (caddr_t) ioaddr;
1085 #endif
1086     {
1087         /* USV?
1088          * check for partial page and clear the
1089          * contents past end of the file before
1090          * releasing it in the VM page cache
1091          */
1092         if ((f_offset < tvc->f.m.Length) && (f_offset + size) > tvc->f.m.Length) {
1093             size_t io = tvc->f.m.Length - f_offset;
1094
1095             memset((caddr_t) (ioaddr + pl_offset + io), 0, size - io);
1096         }
1097     }
1098
1099     AFS_GLOCK();
1100     osi_FlushPages(tvc, vop_cred);      /* hold GLOCK, but not basic vnode lock */
1101     ObtainWriteLock(&tvc->lock, 1);
1102     afs_FakeOpen(tvc);
1103     ReleaseWriteLock(&tvc->lock);
1104
1105     code = afs_write(tvc, uio, flags, cred, 0);
1106
1107     ObtainWriteLock(&tvc->lock, 1);
1108     afs_FakeClose(tvc, cred);
1109     ReleaseWriteLock(&tvc->lock);
1110     AFS_GUNLOCK();
1111     OSI_UPL_UNMAP(pl);
1112     if (!nocommit) {
1113         if (code)
1114             OSI_UPL_ABORT_RANGE(pl, pl_offset, size,
1115                                    UPL_ABORT_FREE_ON_EMPTY);
1116         else
1117             OSI_UPL_COMMIT_RANGE(pl, pl_offset, size,
1118                                     UPL_COMMIT_CLEAR_DIRTY |
1119                                     UPL_COMMIT_FREE_ON_EMPTY);
1120     }
1121
1122 #ifdef AFS_DARWIN80_ENV
1123     uio_free(uio);
1124 #endif
1125     return code;
1126 }
1127
1128 int
1129 afs_vop_ioctl(ap)
1130      struct VOPPROT(ioctl_args) /* {
1131                                  * struct vnode *a_vp;
1132                                  * int  a_command;
1133                                  * caddr_t  a_data;
1134                                  * int  a_fflag;
1135                                  * struct ucred *a_cred;
1136                                  * struct proc *a_p;
1137                                  * } */ *ap;
1138 {
1139     struct vcache *tvc = VTOAFS(ap->a_vp);
1140     struct afs_ioctl data;
1141     int error = 0;
1142
1143     /* in case we ever get in here... */
1144
1145     AFS_STATCNT(afs_ioctl);
1146     if (((ap->a_command >> 8) & 0xff) == 'V') {
1147         /* This is a VICEIOCTL call */
1148         AFS_GLOCK();
1149         error = HandleIoctl(tvc, ap->a_command, ap->a_data);
1150         AFS_GUNLOCK();
1151         return (error);
1152     } else {
1153         /* No-op call; just return. */
1154         return (ENOTTY);
1155     }
1156 }
1157
1158 /* ARGSUSED */
1159 int
1160 afs_vop_select(ap)
1161      struct VOPPROT(select_args)        /* {
1162                                  * struct vnode *a_vp;
1163                                  * int  a_which;
1164                                  * int  a_fflags;
1165                                  * struct ucred *a_cred;
1166                                  * struct proc *a_p;
1167                                  * } */ *ap;
1168 {
1169     /*
1170      * We should really check to see if I/O is possible.
1171      */
1172     return (1);
1173 }
1174
1175 /*
1176  * Mmap a file
1177  *
1178  * NB Currently unsupported.
1179  */
1180 /* ARGSUSED */
1181 int
1182 afs_vop_mmap(ap)
1183      struct VOPPROT(mmap_args)  /* {
1184                                  * struct vnode *a_vp;
1185                                  * int  a_fflags;
1186                                  * struct ucred *a_cred;
1187                                  * struct proc *a_p;
1188                                  * } */ *ap;
1189 {
1190     return (EINVAL);
1191 }
1192
1193 int
1194 afs_vop_fsync(ap)
1195      struct VOPPROT(fsync_args) /* {
1196                                  * struct vnode *a_vp;
1197                                  * struct ucred *a_cred;
1198                                  * int a_waitfor;
1199                                  * struct proc *a_p;
1200                                  * } */ *ap;
1201 {
1202     int wait = ap->a_waitfor == MNT_WAIT;
1203     int error;
1204     struct vnode *vp = ap->a_vp;
1205     int haveGlock = ISAFS_GLOCK();
1206
1207     /* in order to recycle faked vnodes for bulkstat */
1208     if (VTOAFS(vp) == NULL)
1209         return ENOTSUP;
1210
1211     /* afs_vop_lookup glocks, can call us through vinvalbuf from GetVCache */
1212     if (!haveGlock) AFS_GLOCK();
1213     if (vop_cred)
1214         error = afs_fsync(VTOAFS(vp), vop_cred);
1215     else
1216         error = afs_fsync(VTOAFS(vp), &afs_osi_cred);
1217     if (!haveGlock) AFS_GUNLOCK();
1218     return error;
1219 }
1220
1221 #ifndef AFS_DARWIN80_ENV
1222 int
1223 afs_vop_seek(ap)
1224      struct VOPPROT(seek_args)  /* {
1225                                  * struct vnode *a_vp;
1226                                  * off_t  a_oldoff;
1227                                  * off_t  a_newoff;
1228                                  * struct ucred *a_cred;
1229                                  * } */ *ap;
1230 {
1231     if (ap->a_newoff > ULONG_MAX)       /* AFS doesn't support 64-bit offsets */
1232         return EINVAL;
1233     return (0);
1234 }
1235 #endif
1236
1237 int
1238 afs_vop_remove(ap)
1239      struct VOPPROT(remove_args)        /* {
1240                                  * struct vnode *a_dvp;
1241                                  * struct vnode *a_vp;
1242                                  * struct componentname *a_cnp;
1243                                  * } */ *ap;
1244 {
1245     int error = 0;
1246     struct vnode *vp = ap->a_vp;
1247     struct vnode *dvp = ap->a_dvp;
1248
1249 #ifdef AFS_DARWIN80_ENV
1250     if (ap->a_flags & VNODE_REMOVE_NODELETEBUSY) {
1251             /* Caller requested Carbon delete semantics */
1252             if (vnode_isinuse(vp, 0)) {
1253                     return EBUSY;
1254             }
1255     }
1256 #endif
1257
1258     GETNAME();
1259     AFS_GLOCK();
1260     error = afs_remove(VTOAFS(dvp), name, vop_cn_cred);
1261     error = afs_CheckCode(error, NULL, 61);
1262     AFS_GUNLOCK();
1263     cache_purge(vp);
1264     if (!error) {
1265 #ifdef AFS_DARWIN80_ENV
1266         struct vcache *tvc = VTOAFS(vp);
1267         
1268         if (!(tvc->f.states & CUnlinked)) {
1269             ubc_setsize(vp, (off_t)0);
1270             vnode_recycle(vp);
1271         }
1272 #else
1273         /* necessary so we don't deadlock ourselves in vclean */
1274         VOP_UNLOCK(vp, 0, cnp->cn_proc);
1275
1276         /* If crashes continue in ubc_hold, comment this out */
1277         (void)ubc_uncache(vp);
1278 #endif
1279     } else {
1280         /* should check for PRSFS_INSERT and not PRSFS_DELETE, but the
1281            goal here is to deal with Finder's unhappiness with resource
1282            forks that have no resources in a dropbox setting */
1283         if (name[0] == '.' && name[1] == '_' && error == EACCES) 
1284             error = 0;
1285     }
1286
1287 #ifndef AFS_DARWIN80_ENV
1288     vput(dvp);
1289     if (dvp == vp)
1290         vrele(vp);
1291     else
1292         vput(vp);
1293 #endif
1294
1295 #ifndef AFS_DARWIN80_ENV
1296     FREE_ZONE(cnp->cn_pnbuf, cnp->cn_pnlen, M_NAMEI);
1297 #endif
1298     DROPNAME();
1299     return error;
1300 }
1301
1302 int
1303 afs_vop_link(ap)
1304      struct VOPPROT(link_args)  /* {
1305                                  * struct vnode *a_vp;
1306                                  * struct vnode *a_tdvp;
1307                                  * struct componentname *a_cnp;
1308                                  * } */ *ap;
1309 {
1310     int error = 0;
1311     struct vnode *dvp = ap->a_tdvp;
1312     struct vnode *vp = ap->a_vp;
1313     struct proc *p;
1314
1315     GETNAME();
1316     p = vop_cn_proc;
1317     if (vnode_isdir(vp)) {
1318         VOP_ABORTOP(vp, cnp);
1319         error = EISDIR;
1320         goto out;
1321     }
1322 #ifndef AFS_DARWIN80_ENV
1323     if (error = vn_lock(vp, LK_EXCLUSIVE, p)) {
1324         VOP_ABORTOP(dvp, cnp);
1325         goto out;
1326     }
1327 #endif
1328     AFS_GLOCK();
1329     error = afs_link(VTOAFS(vp), VTOAFS(dvp), name, vop_cn_cred);
1330     AFS_GUNLOCK();
1331 #ifndef AFS_DARWIN80_ENV
1332     FREE_ZONE(cnp->cn_pnbuf, cnp->cn_pnlen, M_NAMEI);
1333 #endif
1334 #ifndef AFS_DARWIN80_ENV
1335     if (dvp != vp)
1336         VOP_UNLOCK(vp, 0, p);
1337 #endif
1338   out:
1339 #ifndef AFS_DARWIN80_ENV
1340     vput(dvp);
1341 #endif
1342     DROPNAME();
1343     return error;
1344 }
1345
1346 int
1347 afs_vop_rename(ap)
1348      struct VOPPROT(rename_args)        /* {
1349                                  * struct vnode *a_fdvp;
1350                                  * struct vnode *a_fvp;
1351                                  * struct componentname *a_fcnp;
1352                                  * struct vnode *a_tdvp;
1353                                  * struct vnode *a_tvp;
1354                                  * struct componentname *a_tcnp;
1355                                  * } */ *ap;
1356 {
1357     int error = 0;
1358     struct componentname *fcnp = ap->a_fcnp;
1359     char *fname;
1360     struct componentname *tcnp = ap->a_tcnp;
1361     char *tname;
1362     struct vnode *tvp = ap->a_tvp;
1363     struct vnode *tdvp = ap->a_tdvp;
1364     struct vnode *fvp = ap->a_fvp;
1365     struct vnode *fdvp = ap->a_fdvp;
1366     struct proc *p; 
1367
1368     p = cn_proc(fcnp);
1369
1370 #ifdef AFS_DARWIN80_ENV
1371     /*
1372      * generic code tests for v_mount equality, so we don't have to, but we
1373      * don't get the multiple-mount "benefits" of the old behavior
1374      * the generic code doesn't do this, so we really should, but all the
1375      * vrele's are wrong...
1376      */
1377 #else
1378     /* Check for cross-device rename.
1379      * For AFS, this means anything not in AFS-space
1380      */
1381     if ((0 != strcmp(tdvp->v_mount->mnt_stat.f_fstypename, "afs")) ||
1382         (tvp && (0 != strcmp(tvp->v_mount->mnt_stat.f_fstypename, "afs")))) {
1383         error = EXDEV;
1384         goto abortit;
1385     }
1386
1387     /*
1388      * if fvp == tvp, we're just removing one name of a pair of
1389      * directory entries for the same element.  convert call into rename.
1390      ( (pinched from NetBSD 1.0's ufs_rename())
1391      */
1392     if (fvp == tvp) {
1393         if (vnode_isdir(fvp)) {
1394             error = EINVAL;
1395           abortit:
1396             VOP_ABORTOP(tdvp, tcnp);    /* XXX, why not in NFS? */
1397             if (tdvp == tvp)
1398                 vrele(tdvp);
1399             else
1400                 vput(tdvp);
1401             if (tvp)
1402                 vput(tvp);
1403             VOP_ABORTOP(fdvp, fcnp);    /* XXX, why not in NFS? */
1404             vrele(fdvp);
1405             vrele(fvp);
1406             return (error);
1407         }
1408
1409         /* Release destination completely. */
1410         VOP_ABORTOP(tdvp, tcnp);
1411         vput(tdvp);
1412         vput(tvp);
1413         /* Delete source. */
1414         vrele(fdvp);
1415         vrele(fvp);
1416         fcnp->cn_flags &= ~MODMASK;
1417         fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1418         if ((fcnp->cn_flags & SAVESTART) == 0)
1419             panic("afs_rename: lost from startdir");
1420         fcnp->cn_nameiop = DELETE;
1421
1422         VREF(fdvp); 
1423         error=relookup(fdvp, &fvp, fcnp);
1424         if (error == 0)
1425             vrele(fdvp);
1426         if (fvp == NULL) {
1427             return (ENOENT);
1428         }
1429         error=VOP_REMOVE(fdvp, fvp, fcnp);
1430         
1431         if (fdvp == fvp)
1432             vrele(fdvp);
1433         else
1434             vput(fdvp);
1435         vput(fvp);
1436         return (error);
1437     }
1438     if (error = vn_lock(fvp, LK_EXCLUSIVE, p))
1439         goto abortit;
1440 #endif
1441
1442     MALLOC(fname, char *, fcnp->cn_namelen + 1, M_TEMP, M_WAITOK);
1443     memcpy(fname, fcnp->cn_nameptr, fcnp->cn_namelen);
1444     fname[fcnp->cn_namelen] = '\0';
1445     MALLOC(tname, char *, tcnp->cn_namelen + 1, M_TEMP, M_WAITOK);
1446     memcpy(tname, tcnp->cn_nameptr, tcnp->cn_namelen);
1447     tname[tcnp->cn_namelen] = '\0';
1448
1449
1450     AFS_GLOCK();
1451     /* XXX use "from" or "to" creds? NFS uses "to" creds */
1452     error =
1453         afs_rename(VTOAFS(fdvp), fname, VTOAFS(tdvp), tname, cn_cred(tcnp));
1454
1455 #if !defined(AFS_DARWIN80_ENV) 
1456     AFS_GUNLOCK();
1457     VOP_UNLOCK(fvp, 0, p);
1458     if (error)
1459         goto abortit;           /* XXX */
1460     if (tdvp == tvp)
1461         vrele(tdvp);
1462     else
1463         vput(tdvp);
1464     if (tvp)
1465         vput(tvp);
1466     vrele(fdvp);
1467     vrele(fvp);
1468 #else
1469     if (error == EXDEV) {
1470         struct brequest *tb;
1471         struct afs_uspc_param mvReq;
1472         struct vcache *tvc;
1473         struct vcache *fvc = VTOAFS(fdvp);
1474         int code = 0;
1475         struct afs_fakestat_state fakestate;
1476         int fakestatdone = 0;
1477
1478         tvc = VTOAFS(tdvp);
1479
1480         /* unrewritten mount point? */
1481         if (tvc->mvstat == AFS_MVSTAT_MTPT) {
1482             if (tvc->mvid.target_root && (tvc->f.states & CMValid)) {
1483                 struct vrequest treq;
1484
1485                 afs_InitFakeStat(&fakestate);
1486                 code = afs_InitReq(&treq, vop_cred);
1487                 if (!code) {
1488                     fakestatdone = 1;
1489                     code = afs_EvalFakeStat(&tvc, &fakestate, &treq);
1490                 } else
1491                     afs_PutFakeStat(&fakestate);
1492             }
1493         }
1494
1495         if (!code) {
1496             /* at some point in the future we should allow other types */
1497             mvReq.reqtype = AFS_USPC_UMV;
1498             mvReq.req.umv.id = afs_cr_uid(cn_cred(tcnp));
1499             mvReq.req.umv.idtype = IDTYPE_UID;
1500             mvReq.req.umv.sCell = fvc->f.fid.Cell;
1501             mvReq.req.umv.sVolume = fvc->f.fid.Fid.Volume;
1502             mvReq.req.umv.sVnode = fvc->f.fid.Fid.Vnode;
1503             mvReq.req.umv.sUnique = fvc->f.fid.Fid.Unique;
1504             mvReq.req.umv.dCell = tvc->f.fid.Cell;
1505             mvReq.req.umv.dVolume = tvc->f.fid.Fid.Volume;
1506             mvReq.req.umv.dVnode = tvc->f.fid.Fid.Vnode;
1507             mvReq.req.umv.dUnique = tvc->f.fid.Fid.Unique;
1508
1509             /*
1510              * su %d -c mv /afs/.:mount/%d:%d:%d:%d/%s
1511              * /afs/.:mount/%d:%d:%d:%d/%s where:
1512              * mvReq.req.umv.id, fvc->f.fid.Cell, fvc->f.fid.Fid.Volume,
1513              * fvc->f.fid.Fid.Vnode, fvc->f.fid.Fid.Unique, fname,
1514              * tvc->f.fid.Cell, tvc->f.fid.Fid.Volume, tvc->f.fid.Fid.Vnode,
1515              * tvc->f.fid.Fid.Unique, tname
1516              */
1517
1518             tb = afs_BQueue(BOP_MOVE, NULL, 0, 1, cn_cred(tcnp),
1519                             0L, 0L, &mvReq, fname, tname);
1520             /* wait to collect result */
1521             while ((tb->flags & BUVALID) == 0) {
1522                 tb->flags |= BUWAIT;
1523                 afs_osi_Sleep(tb);
1524             }
1525             /* if we succeeded, clear the error. otherwise, EXDEV */
1526             if (tb->code_raw == 0)
1527                 error = 0;
1528
1529             afs_BRelease(tb);
1530         }
1531
1532         if (fakestatdone)
1533             afs_PutFakeStat(&fakestate);
1534     }
1535     AFS_GUNLOCK();
1536
1537     cache_purge(fdvp);
1538     cache_purge(fvp);
1539     cache_purge(tdvp);
1540     if (tvp) {
1541         cache_purge(tvp);
1542         if (!error) {
1543             vnode_recycle(tvp);
1544         }
1545     }
1546     if (!error)
1547         cache_enter(tdvp, fvp, tcnp);
1548 #endif
1549     FREE(fname, M_TEMP);
1550     FREE(tname, M_TEMP);
1551     return error;
1552 }
1553
1554 int
1555 afs_vop_mkdir(ap)
1556      struct VOPPROT(mkdir_args) /* {
1557                                  * struct vnode *a_dvp;
1558                                  * struct vnode **a_vpp;
1559                                  * struct componentname *a_cnp;
1560                                  * struct vattr *a_vap;
1561                                  * } */ *ap;
1562 {
1563     struct vnode *dvp = ap->a_dvp;
1564     struct vattr *vap = ap->a_vap;
1565     int error = 0;
1566     struct vcache *vcp;
1567     struct proc *p;
1568
1569     GETNAME();
1570     p = vop_cn_proc;
1571 #if defined(DIAGNOSTIC) && !defined(AFS_DARWIN80_ENV)
1572     if ((cnp->cn_flags & HASBUF) == 0)
1573         panic("afs_vop_mkdir: no name");
1574 #endif
1575     AFS_GLOCK();
1576     error = afs_mkdir(VTOAFS(dvp), name, vap, &vcp, vop_cn_cred);
1577     AFS_GUNLOCK();
1578     if (error) {
1579 #ifndef AFS_DARWIN80_ENV
1580         VOP_ABORTOP(dvp, cnp);
1581         vput(dvp);
1582 #endif
1583         DROPNAME();
1584         return (error);
1585     }
1586     if (vcp) {
1587 #ifdef AFS_DARWIN80_ENV
1588         afs_darwin_finalizevnode(vcp, ap->a_dvp, ap->a_cnp, 0, 0);
1589 #endif
1590         *ap->a_vpp = AFSTOV(vcp);
1591 #ifndef AFS_DARWIN80_ENV /* XXX needed for multi-mount thing, but can't have it yet */
1592         (*ap->a_vpp)->v_vfsp = dvp->v_vfsp;
1593         vn_lock(*ap->a_vpp, LK_EXCLUSIVE | LK_RETRY, p);
1594 #endif
1595     } else
1596         *ap->a_vpp = 0;
1597     DROPNAME();
1598 #ifndef AFS_DARWIN80_ENV
1599     FREE_ZONE(cnp->cn_pnbuf, cnp->cn_pnlen, M_NAMEI);
1600     vput(dvp);
1601 #endif
1602     return error;
1603 }
1604
1605 int
1606 afs_vop_rmdir(ap)
1607      struct VOPPROT(rmdir_args) /* {
1608                                  * struct vnode *a_dvp;
1609                                  * struct vnode *a_vp;
1610                                  * struct componentname *a_cnp;
1611                                  * } */ *ap;
1612 {
1613     int error = 0;
1614     struct vnode *vp = ap->a_vp;
1615     struct vnode *dvp = ap->a_dvp;
1616
1617     GETNAME();
1618     if (dvp == vp) {
1619 #ifndef AFS_DARWIN80_ENV
1620         vrele(dvp);
1621         vput(vp);
1622         FREE_ZONE(cnp->cn_pnbuf, cnp->cn_pnlen, M_NAMEI);
1623 #endif
1624         DROPNAME();
1625         return (EINVAL);
1626     }
1627
1628     AFS_GLOCK();
1629     error = afs_rmdir(VTOAFS(dvp), name, vop_cn_cred);
1630     AFS_GUNLOCK();
1631     DROPNAME();
1632     cache_purge(dvp);
1633     cache_purge(vp);
1634 #ifndef AFS_DARWIN80_ENV
1635     vput(dvp);
1636     vput(vp);
1637 #endif
1638     return error;
1639 }
1640
1641 int
1642 afs_vop_symlink(ap)
1643      struct VOPPROT(symlink_args)       /* {
1644                                  * struct vnode *a_dvp;
1645                                  * struct vnode **a_vpp;
1646                                  * struct componentname *a_cnp;
1647                                  * struct vattr *a_vap;
1648                                  * char *a_target;
1649                                  * } */ *ap;
1650 {
1651     struct vnode *dvp = ap->a_dvp;
1652     struct vcache *pvc = NULL;
1653     int error = 0;
1654
1655     GETNAME();
1656     AFS_GLOCK();
1657     error = afs_symlink(VTOAFS(dvp), name, ap->a_vap, ap->a_target, &pvc,
1658                         vop_cn_cred);
1659     AFS_GUNLOCK();
1660 #ifndef AFS_DARWIN80_ENV
1661     FREE_ZONE(cnp->cn_pnbuf, cnp->cn_pnlen, M_NAMEI);
1662     vput(dvp);
1663 #endif
1664     *ap->a_vpp = NULL;
1665     if (!error) {
1666         error = afs_darwin_finalizevnode(pvc, dvp, ap->a_cnp, 0, 0);
1667         if (!error)
1668             *ap->a_vpp = AFSTOV(pvc);
1669     }
1670     DROPNAME();
1671     return error;
1672 }
1673
1674 int
1675 afs_vop_readdir(ap)
1676      struct VOPPROT(readdir_args)       /* {
1677                                  * struct vnode *a_vp;
1678                                  * struct uio *a_uio;
1679                                  * struct ucred *a_cred;
1680                                  * int *a_eofflag;
1681                                  * u_long *a_cookies;
1682                                  * int ncookies;
1683                                  * } */ *ap;
1684 {
1685     int error;
1686     off_t off;
1687 /*    printf("readdir %x cookies %x ncookies %d\n", ap->a_vp, ap->a_cookies,
1688            ap->a_ncookies); */
1689 #ifdef AFS_DARWIN80_ENV
1690     /* too much work for now */
1691     /* should only break nfs exports */
1692     if (ap->a_flags & (VNODE_READDIR_EXTENDED | VNODE_READDIR_REQSEEKOFF))
1693          return (EINVAL);
1694 #endif
1695     off = AFS_UIO_OFFSET(ap->a_uio);
1696     AFS_GLOCK();
1697     error =
1698         afs_readdir(VTOAFS(ap->a_vp), ap->a_uio, vop_cred, ap->a_eofflag);
1699     AFS_GUNLOCK();
1700 #ifndef AFS_DARWIN80_ENV
1701     if (!error && ap->a_ncookies != NULL) {
1702         struct uio *uio = ap->a_uio;
1703         const struct dirent *dp, *dp_start, *dp_end;
1704         int ncookies;
1705         u_long *cookies, *cookiep;
1706
1707         if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1708             panic("afs_readdir: burned cookies");
1709         dp = (const struct dirent *)
1710             ((const char *)uio->uio_iov->iov_base - (uio->uio_offset - off));
1711
1712         dp_end = (const struct dirent *)uio->uio_iov->iov_base;
1713         for (dp_start = dp, ncookies = 0; dp < dp_end;
1714              dp = (const struct dirent *)((const char *)dp + dp->d_reclen))
1715             ncookies++;
1716
1717         MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP,
1718                M_WAITOK);
1719         for (dp = dp_start, cookiep = cookies; dp < dp_end;
1720              dp = (const struct dirent *)((const char *)dp + dp->d_reclen)) {
1721             off += dp->d_reclen;
1722             *cookiep++ = off;
1723         }
1724         *ap->a_cookies = cookies;
1725         *ap->a_ncookies = ncookies;
1726     }
1727 #endif
1728
1729     return error;
1730 }
1731
1732 int
1733 afs_vop_readlink(ap)
1734      struct VOPPROT(readlink_args)      /* {
1735                                  * struct vnode *a_vp;
1736                                  * struct uio *a_uio;
1737                                  * struct ucred *a_cred;
1738                                  * } */ *ap;
1739 {
1740     int error;
1741 /*    printf("readlink %x\n", ap->a_vp);*/
1742     AFS_GLOCK();
1743     error = afs_readlink(VTOAFS(ap->a_vp), ap->a_uio, vop_cred);
1744     AFS_GUNLOCK();
1745     return error;
1746 }
1747
1748 extern int prtactive;
1749
1750 int
1751 afs_vop_inactive(ap)
1752      struct VOPPROT(inactive_args)      /* {
1753                                  * struct vnode *a_vp;
1754                                  * struct proc *a_p;
1755                                  * } */ *ap;
1756 {
1757     struct vnode *vp = ap->a_vp;
1758     struct vcache *tvc = VTOAFS(vp);
1759 #ifndef AFS_DARWIN80_ENV
1760     if (prtactive && vp->v_usecount != 0)
1761         vprint("afs_vop_inactive(): pushing active", vp);
1762 #endif
1763     if (tvc) {
1764 #ifdef AFS_DARWIN80_ENV
1765         int unlinked = tvc->f.states & CUnlinked;
1766 #endif
1767         AFS_GLOCK();
1768         afs_InactiveVCache(tvc, 0);     /* decrs ref counts */
1769         AFS_GUNLOCK();
1770 #ifdef AFS_DARWIN80_ENV
1771         if (unlinked) {
1772             vnode_recycle(vp);
1773             cache_purge(vp);
1774         }
1775 #endif
1776     }
1777 #ifndef AFS_DARWIN80_ENV
1778     VOP_UNLOCK(vp, 0, ap->a_p);
1779 #endif
1780     return 0;
1781 }
1782
1783 int
1784 afs_vop_reclaim(ap)
1785      struct VOPPROT(reclaim_args)       /* {
1786                                  * struct vnode *a_vp;
1787                                  * } */ *ap;
1788 {
1789     int error = 0;
1790     int sl, writelocked;
1791     struct vnode *vp = ap->a_vp;
1792     struct vcache *tvc = VTOAFS(vp);
1793
1794     osi_Assert(!ISAFS_GLOCK());
1795     cache_purge(vp);            /* just in case... */
1796     if (tvc) {
1797        AFS_GLOCK();
1798        writelocked = (0 == NBObtainWriteLock(&afs_xvcache, 335));
1799        if (!writelocked) {
1800            ObtainWriteLock(&afs_xvreclaim, 176);
1801 #ifdef AFS_DARWIN80_ENV
1802            vnode_clearfsnode(AFSTOV(tvc));
1803            vnode_removefsref(AFSTOV(tvc));
1804 #else
1805            tvc->v->v_data = NULL;  /* remove from vnode */
1806 #endif
1807            AFSTOV(tvc) = NULL;             /* also drop the ptr to vnode */
1808            tvc->f.states |= CVInit; /* also CDeadVnode? */
1809            tvc->nextfree = ReclaimedVCList;
1810            ReclaimedVCList = tvc;
1811            ReleaseWriteLock(&afs_xvreclaim);
1812        } else {
1813            error = afs_FlushVCache(tvc, &sl);   /* toss our stuff from vnode */
1814            if (tvc->f.states & (CVInit
1815 #ifdef AFS_DARWIN80_ENV
1816                               | CDeadVnode
1817 #endif
1818                    )) {
1819                tvc->f.states &= ~(CVInit
1820 #ifdef AFS_DARWIN80_ENV
1821                                 | CDeadVnode
1822 #endif
1823                    );
1824                afs_osi_Wakeup(&tvc->f.states);
1825            }
1826            if (!error && vnode_fsnode(vp))
1827                panic("afs_reclaim: vnode not cleaned");
1828            if (!error && (tvc->v != NULL)) 
1829                panic("afs_reclaim: vcache not cleaned");
1830            ReleaseWriteLock(&afs_xvcache);
1831        }
1832        AFS_GUNLOCK();
1833     }
1834     return error;
1835 }
1836
1837 /*
1838  * Return POSIX pathconf information applicable to ufs filesystems.
1839  */
1840 afs_vop_pathconf(ap)
1841      struct VOPPROT(pathconf_args)      /* {
1842                                  * struct vnode *a_vp;
1843                                  * int a_name;
1844                                  * int *a_retval;
1845                                  * } */ *ap;
1846 {
1847     AFS_STATCNT(afs_cntl);
1848     switch (ap->a_name) {
1849     case _PC_LINK_MAX:
1850         *ap->a_retval = LINK_MAX;
1851         break;
1852     case _PC_NAME_MAX:
1853         *ap->a_retval = NAME_MAX;
1854         break;
1855     case _PC_PATH_MAX:
1856         *ap->a_retval = PATH_MAX;
1857         break;
1858     case _PC_CHOWN_RESTRICTED:
1859         *ap->a_retval = 1;
1860         break;
1861     case _PC_NO_TRUNC:
1862         *ap->a_retval = 1;
1863         break;
1864     case _PC_PIPE_BUF:
1865         return EINVAL;
1866         break;
1867     case _PC_NAME_CHARS_MAX:
1868         *ap->a_retval = NAME_MAX;
1869         break;
1870     case _PC_CASE_SENSITIVE:
1871         *ap->a_retval = 1;
1872         break;
1873     case _PC_CASE_PRESERVING:
1874         *ap->a_retval = 1;
1875         break;
1876     default:
1877         return EINVAL;
1878     }
1879     return 0;
1880 }
1881
1882 /*
1883  * Advisory record locking support (fcntl() POSIX style)
1884  */
1885 int
1886 afs_vop_advlock(ap)
1887      struct VOPPROT(advlock_args)       /* {
1888                                  * struct vnode *a_vp;
1889                                  * caddr_t  a_id;
1890                                  * int  a_op;
1891                                  * struct flock *a_fl;
1892                                  * int  a_flags;
1893                                  * } */ *ap;
1894 {
1895     int error;
1896     struct ucred *tcr;
1897     int clid;
1898     int op;
1899 #ifdef AFS_DARWIN80_ENV
1900     proc_t p;
1901     tcr=vop_cred;
1902 #else
1903     struct proc *p = current_proc();
1904     struct ucred cr;
1905     pcred_readlock(p);
1906     cr = *p->p_cred->pc_ucred;
1907     pcred_unlock(p);
1908     tcr=&cr;
1909 #endif
1910     if (ap->a_flags & F_POSIX) {
1911 #ifdef AFS_DARWIN80_ENV
1912         p = (proc_t) ap->a_id;
1913         clid = proc_pid(p);
1914 #else
1915         p = (struct proc *) ap->a_id;
1916         clid = p->p_pid;
1917 #endif
1918     } else {
1919         clid = (int)ap->a_id;
1920     }
1921     if (ap->a_op == F_UNLCK) {
1922         op = F_SETLK;
1923     } else if (ap->a_op == F_SETLK && ap->a_flags & F_WAIT) {
1924         op = F_SETLKW;
1925     } else {
1926         op = ap->a_op;
1927     }
1928     AFS_GLOCK();
1929     error = afs_lockctl(VTOAFS(ap->a_vp), ap->a_fl, op, tcr, clid);
1930     AFS_GUNLOCK();
1931     return error;
1932 }
1933
1934 int
1935 afs_vop_blktooff(ap)
1936      struct VOPPROT(blktooff_args)      /* {
1937                                  * struct vnode *a_vp;
1938                                  * daddr_t a_lblkno;
1939                                  * off_t *a_offset;    
1940                                  * } */ *ap;
1941 {
1942     *ap->a_offset = (off_t) (ap->a_lblkno * DEV_BSIZE);
1943     return 0;
1944 }
1945
1946 int
1947 afs_vop_offtoblk(ap)
1948      struct VOPPROT(offtoblk_args)      /* {
1949                                  * struct vnode *a_vp;
1950                                  * off_t a_offset;    
1951                                  * daddr_t *a_lblkno;
1952                                  * } */ *ap;
1953 {
1954     *ap->a_lblkno = (daddr_t) (ap->a_offset / DEV_BSIZE);
1955
1956     return (0);
1957 }
1958
1959 #ifndef AFS_DARWIN80_ENV
1960 int
1961 afs_vop_lock(ap)
1962      struct VOPPROT(lock_args)  /* {
1963                                  * struct vnode *a_vp;
1964                                  * } */ *ap;
1965 {
1966     struct vnode *vp = ap->a_vp;
1967     struct vcache *avc = VTOAFS(vp);
1968
1969     if (vp->v_tag == VT_NON)
1970         return (ENOENT);
1971
1972     return (lockmgr(&avc->rwlock, ap->a_flags, &vp->v_interlock, ap->a_p));
1973 }
1974
1975 int
1976 afs_vop_unlock(ap)
1977      struct VOPPROT(unlock_args)        /* {
1978                                  * struct vnode *a_vp;
1979                                  * } */ *ap;
1980 {
1981     struct vnode *vp = ap->a_vp;
1982     struct vcache *avc = VTOAFS(vp);
1983
1984     return (lockmgr
1985             (&avc->rwlock, ap->a_flags | LK_RELEASE, &vp->v_interlock,
1986              ap->a_p));
1987
1988 }
1989
1990 int
1991 afs_vop_truncate(ap)
1992      struct VOPPROT(truncate_args)      /* {
1993                                  * struct vnode *a_vp;
1994                                  * off_t a_length;
1995                                  * int a_flags;
1996                                  * struct ucred *a_cred;
1997                                  * struct proc *a_p;
1998                                  * } */ *ap;
1999 {
2000     /* printf("stray afs_vop_truncate\n"); */
2001     return ENOTSUP;
2002 }
2003
2004 int
2005 afs_vop_update(ap)
2006      struct VOPPROT(update_args)        /* {
2007                                  * struct vnode *a_vp;
2008                                  * struct timeval *a_access;
2009                                  * struct timeval *a_modify;
2010                                  * int a_waitfor;
2011                                  * } */ *ap;
2012 {
2013     /* printf("stray afs_vop_update\n"); */
2014     return ENOTSUP;
2015 }
2016
2017 int
2018 afs_vop_bmap(ap)
2019      struct VOPPROT(bmap_args)  /* {
2020                                  * struct vnode *a_vp;
2021                                  * daddr_t  a_bn;
2022                                  * struct vnode **a_vpp;
2023                                  * daddr_t *a_bnp;
2024                                  * int *a_runp;
2025                                  * int *a_runb;
2026                                  * } */ *ap;
2027 {
2028     int error;
2029     if (ap->a_bnp) {
2030         *ap->a_bnp = ap->a_bn * (PAGE_SIZE / DEV_BSIZE);
2031     }
2032     if (ap->a_vpp) {
2033         *ap->a_vpp = ap->a_vp;
2034     }
2035     if (ap->a_runp != NULL)
2036         *ap->a_runp = 0;
2037
2038     return 0;
2039 }
2040
2041 int
2042 afs_vop_strategy(ap)
2043      struct VOPPROT(strategy_args)      /* {
2044                                  * struct buf *a_bp;
2045                                  * } */ *ap;
2046 {
2047     int error;
2048     AFS_GLOCK();
2049     error = afs_ustrategy(ap->a_bp);
2050     AFS_GUNLOCK();
2051     return error;
2052 }
2053
2054 int
2055 afs_vop_print(ap)
2056      struct VOPPROT(print_args) /* {
2057                                  * struct vnode *a_vp;
2058                                  * } */ *ap;
2059 {
2060     struct vnode *vp = ap->a_vp;
2061     struct vcache *vc = VTOAFS(ap->a_vp);
2062     int s = vc->f.states;
2063     printf("tag %d, fid: %ld.%x.%x.%x, opens %d, writers %d", vp->v_tag,
2064            vc->f.fid.Cell, vc->f.fid.Fid.Volume, vc->f.fid.Fid.Vnode,
2065            vc->f.fid.Fid.Unique, vc->opens, vc->execsOrWriters);
2066     printf("\n  states%s%s%s%s%s", (s & CStatd) ? " statd" : "",
2067            (s & CRO) ? " readonly" : "", (s & CDirty) ? " dirty" : "",
2068            (s & CMAPPED) ? " mapped" : "",
2069            (s & CVFlushed) ? " flush in progress" : "");
2070     if (UBCISVALID(vp)) {
2071         printf("\n  UBC: ");
2072         if (UBCINFOEXISTS(vp)) {
2073             printf("exists, ");
2074             printf("refs %d%s%s", vp->v_ubcinfo->ui_refcount,
2075                    ubc_issetflags(vp, UI_HASOBJREF) ? " HASOBJREF" : "",
2076                    ubc_issetflags(vp, UI_WASMAPPED) ? " WASMAPPED" : "");
2077         } else
2078             printf("does not exist");
2079     }
2080     printf("\n");
2081     return 0;
2082 }
2083
2084 int
2085 afs_vop_islocked(ap)
2086      struct VOPPROT(islocked_args)      /* {
2087                                  * struct vnode *a_vp;
2088                                  * } */ *ap;
2089 {
2090     struct vcache *vc = VTOAFS(ap->a_vp);
2091     return lockstatus(&vc->rwlock);
2092 }
2093
2094 int
2095 afs_vop_cmap(ap)
2096      struct VOPPROT(cmap_args)  /* {
2097                                  * struct vnode *a_vp;
2098                                  * off_t a_foffset;    
2099                                  * size_t a_size;
2100                                  * daddr_t *a_bpn;
2101                                  * size_t *a_run;
2102                                  * void *a_poff;
2103                                  * } */ *ap;
2104 {
2105     *ap->a_bpn = (daddr_t) (ap->a_foffset / DEV_BSIZE);
2106     *ap->a_run = MAX(ap->a_size, AFS_CHUNKSIZE(ap->a_foffset));
2107     return 0;
2108 }
2109 #endif
2110
2111 int
2112 afs_darwin_getnewvnode(struct vcache *avc)
2113 {
2114 #ifdef AFS_DARWIN80_ENV
2115     vnode_t vp;
2116     int error, dead;
2117     struct vnode_fsparam par;
2118
2119     memset(&par, 0, sizeof(struct vnode_fsparam));
2120     par.vnfs_vtype = VNON;
2121     par.vnfs_vops = afs_dead_vnodeop_p;
2122     par.vnfs_flags = VNFS_NOCACHE|VNFS_CANTCACHE;
2123     par.vnfs_mp = afs_globalVFS;
2124     par.vnfs_fsnode = avc;
2125
2126     error = vnode_create(VNCREATE_FLAVOR, VCREATESIZE, &par, &vp);
2127     if (!error) {
2128       vnode_addfsref(vp);
2129       vnode_ref(vp);
2130       avc->v = vp;
2131       vnode_recycle(vp); /* terminate as soon as iocount drops */
2132       avc->f.states |= CDeadVnode;
2133     }
2134     return error;
2135 #else
2136     while (getnewvnode(VT_AFS, afs_globalVFS, afs_vnodeop_p, &avc->v)) {
2137         /* no vnodes available, force an alloc (limits be damned)! */
2138         printf("failed to get vnode\n");
2139     }
2140     avc->v->v_data = (void *)avc;
2141     return 0;
2142 #endif
2143 }
2144 #ifdef AFS_DARWIN80_ENV
2145 /* if this fails, then tvc has been unrefed and may have been freed. 
2146    Don't touch! */
2147 int
2148 afs_darwin_finalizevnode(struct vcache *avc, struct vnode *dvp,
2149                          struct componentname *cnp, int isroot, int locked)
2150 {
2151     vnode_t ovp;
2152     vnode_t nvp;
2153     int error;
2154     struct vnode_fsparam par;
2155
2156     if (!locked) {
2157         AFS_GLOCK();
2158         ObtainWriteLock(&avc->lock,325);
2159     }
2160     ovp = AFSTOV(avc);
2161
2162     if (!(avc->f.states & CDeadVnode) && vnode_vtype(ovp) != VNON) {
2163         AFS_GUNLOCK();
2164         /* Can end up in reclaim... drop GLOCK */
2165         vnode_rele(ovp);
2166         AFS_GLOCK();
2167         if (!locked) {
2168             ReleaseWriteLock(&avc->lock);
2169             AFS_GUNLOCK();
2170         }
2171         return 0;
2172     }
2173
2174     if ((avc->f.states & CDeadVnode) && vnode_vtype(ovp) != VNON)
2175         panic("vcache %p should not be CDeadVnode", avc);
2176     AFS_GUNLOCK();
2177     memset(&par, 0, sizeof(struct vnode_fsparam));
2178     par.vnfs_mp = afs_globalVFS;
2179     par.vnfs_vtype = avc->f.m.Type;
2180     par.vnfs_vops = afs_vnodeop_p;
2181     par.vnfs_filesize = avc->f.m.Length;
2182     par.vnfs_fsnode = avc;
2183     par.vnfs_dvp = dvp;
2184     if (cnp && (cnp->cn_flags & ISDOTDOT) == 0)
2185         par.vnfs_cnp = cnp;
2186     if (!dvp || !cnp || (cnp->cn_flags & MAKEENTRY) == 0)
2187         par.vnfs_flags = VNFS_NOCACHE;
2188     if (isroot)
2189         par.vnfs_markroot = 1;
2190     error = vnode_create(VNCREATE_FLAVOR, VCREATESIZE, &par, &nvp);
2191     if (!error) {
2192         vnode_addfsref(nvp);
2193         if ((avc->f.states & CDeadVnode) && vnode_vtype(ovp) != VNON)
2194             printf("vcache %p should not be CDeadVnode", avc);
2195         if (avc->v == ovp) {
2196             if (!(avc->f.states & CVInit)) {
2197                 vnode_clearfsnode(ovp);
2198                 vnode_removefsref(ovp);
2199             }
2200             /* we're discarding on a fixup. mark for recycle */
2201             if (!(avc->f.states & CDeadVnode))
2202                 vnode_recycle(ovp);
2203         }
2204         avc->v = nvp;
2205         avc->f.states &=~ CDeadVnode;
2206         /* If we were carrying an extra ref for dirty, hold/push it. */
2207         if (avc->f.ddirty_flags) {
2208             vnode_get(nvp);
2209             vnode_ref(nvp);
2210         }
2211         /* If we were carrying an extra ref for shadow, hold/push it. */
2212         if (avc->f.shadow.vnode) {
2213             vnode_get(nvp);
2214             vnode_ref(nvp);
2215         }
2216     }
2217     /* Drop any extra dirty ref on the old vnode */
2218     if (avc->f.ddirty_flags) {
2219         vnode_put(ovp);
2220         vnode_rele(ovp);
2221     }
2222     /* Drop any extra shadow ref on the old vnode */
2223     if (avc->f.shadow.vnode) {
2224         vnode_put(ovp);
2225         vnode_rele(ovp);
2226     }
2227     /* If it's ref'd still, unmark stat'd to force new lookup */
2228     if ((vnode_vtype(ovp) != avc->f.m.Type) && VREFCOUNT_GT(avc, 1))
2229         avc->f.states &= ~CStatd;
2230
2231     vnode_put(ovp);
2232     vnode_rele(ovp);
2233     AFS_GLOCK();
2234     if (!locked)
2235         ReleaseWriteLock(&avc->lock);
2236     if (!error)
2237         afs_osi_Wakeup(&avc->f.states);
2238     if (!locked)
2239         AFS_GUNLOCK();
2240     return error;
2241 }
2242 #endif