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