freebsd-50-20030423
[openafs.git] / src / afs / FBSD / osi_vnodeops.c
1 #include <afsconfig.h>
2 #include <afs/param.h>
3
4 RCSID("$Header$");
5
6 #include <afs/sysincludes.h>            /* Standard vendor system headers */
7 #include <afsincludes.h>            /* Afs-based standard headers */
8 #include <afs/afs_stats.h>              /* statistics */
9 #include <sys/malloc.h>
10 #include <sys/namei.h>
11 #ifndef AFS_FBSD50_ENV
12 #include <vm/vm_zone.h>
13 #endif
14 #include <vm/vm_page.h>
15 #include <vm/vm_object.h>
16 #include <vm/vm_pager.h>
17 #include <vm/vnode_pager.h>
18 extern int afs_pbuf_freecnt;
19
20 int afs_vop_lookup(struct vop_lookup_args *);
21 int afs_vop_create(struct vop_create_args *);
22 int afs_vop_mknod(struct vop_mknod_args *);
23 int afs_vop_open(struct vop_open_args *);
24 int afs_vop_close(struct vop_close_args *);
25 int afs_vop_access(struct vop_access_args *);
26 int afs_vop_getattr(struct vop_getattr_args *);
27 int afs_vop_setattr(struct vop_setattr_args *);
28 int afs_vop_read(struct vop_read_args *);
29 int afs_vop_write(struct vop_write_args *);
30 int afs_vop_getpages(struct vop_getpages_args *);
31 int afs_vop_putpages(struct vop_putpages_args *);
32 int afs_vop_ioctl(struct vop_ioctl_args *);
33 int afs_vop_poll(struct vop_poll_args *);
34 int afs_vop_mmap(struct vop_mmap_args *);
35 int afs_vop_fsync(struct vop_fsync_args *);
36 int afs_vop_remove(struct vop_remove_args *);
37 int afs_vop_link(struct vop_link_args *);
38 int afs_vop_rename(struct vop_rename_args *);
39 int afs_vop_mkdir(struct vop_mkdir_args *);
40 int afs_vop_rmdir(struct vop_rmdir_args *);
41 int afs_vop_symlink(struct vop_symlink_args *);
42 int afs_vop_readdir(struct vop_readdir_args *);
43 int afs_vop_readlink(struct vop_readlink_args *);
44 int afs_vop_inactive(struct vop_inactive_args *);
45 int afs_vop_reclaim(struct vop_reclaim_args *);
46 int afs_vop_lock(struct vop_lock_args *);
47 int afs_vop_unlock(struct vop_unlock_args *);
48 int afs_vop_bmap(struct vop_bmap_args *);
49 int afs_vop_strategy(struct vop_strategy_args *);
50 int afs_vop_print(struct vop_print_args *);
51 int afs_vop_islocked(struct vop_islocked_args *);
52 int afs_vop_advlock(struct vop_advlock_args *);
53
54
55
56 /* Global vfs data structures for AFS. */
57 vop_t **afs_vnodeop_p;
58 struct vnodeopv_entry_desc afs_vnodeop_entries[] = {
59         { &vop_default_desc, (vop_t *) vop_eopnotsupp },
60         { &vop_access_desc, (vop_t *) afs_vop_access },          /* access */
61         { &vop_advlock_desc, (vop_t *) afs_vop_advlock },        /* advlock */
62         { &vop_bmap_desc, (vop_t *) afs_vop_bmap },              /* bmap */
63 #ifndef AFS_FBSD50_ENV
64         { &vop_bwrite_desc, (vop_t *) vop_stdbwrite },
65 #endif
66         { &vop_close_desc, (vop_t *) afs_vop_close },            /* close */
67         { &vop_createvobject_desc,  (vop_t *) vop_stdcreatevobject },
68         { &vop_destroyvobject_desc, (vop_t *) vop_stddestroyvobject },
69         { &vop_create_desc, (vop_t *) afs_vop_create },          /* create */
70         { &vop_fsync_desc, (vop_t *) afs_vop_fsync },            /* fsync */
71         { &vop_getattr_desc, (vop_t *) afs_vop_getattr },        /* getattr */
72         { &vop_getpages_desc, (vop_t *) afs_vop_getpages },              /* read */
73         { &vop_getvobject_desc, (vop_t *) vop_stdgetvobject },
74         { &vop_putpages_desc, (vop_t *) afs_vop_putpages },            /* write */
75         { &vop_inactive_desc, (vop_t *) afs_vop_inactive },      /* inactive */
76         { &vop_islocked_desc, (vop_t *) afs_vop_islocked },      /* islocked */
77         { &vop_lease_desc, (vop_t *) vop_null },
78         { &vop_link_desc, (vop_t *) afs_vop_link },              /* link */
79         { &vop_lock_desc, (vop_t *) afs_vop_lock },              /* lock */
80         { &vop_lookup_desc, (vop_t *) afs_vop_lookup },          /* lookup */
81         { &vop_mkdir_desc, (vop_t *) afs_vop_mkdir },            /* mkdir */
82         { &vop_mknod_desc, (vop_t *) afs_vop_mknod },            /* mknod */
83         { &vop_mmap_desc, (vop_t *) afs_vop_mmap },              /* mmap */
84         { &vop_open_desc, (vop_t *) afs_vop_open },              /* open */
85         { &vop_poll_desc, (vop_t *) afs_vop_poll },          /* select */
86         { &vop_print_desc, (vop_t *) afs_vop_print },            /* print */
87         { &vop_read_desc, (vop_t *) afs_vop_read },              /* read */
88         { &vop_readdir_desc, (vop_t *) afs_vop_readdir },        /* readdir */
89         { &vop_readlink_desc, (vop_t *) afs_vop_readlink },      /* readlink */
90         { &vop_reclaim_desc, (vop_t *) afs_vop_reclaim },        /* reclaim */
91         { &vop_remove_desc, (vop_t *) afs_vop_remove },          /* remove */
92         { &vop_rename_desc, (vop_t *) afs_vop_rename },          /* rename */
93         { &vop_rmdir_desc, (vop_t *) afs_vop_rmdir },            /* rmdir */
94         { &vop_setattr_desc, (vop_t *) afs_vop_setattr },        /* setattr */
95         { &vop_strategy_desc, (vop_t *) afs_vop_strategy },      /* strategy */
96         { &vop_symlink_desc, (vop_t *) afs_vop_symlink },        /* symlink */
97         { &vop_unlock_desc, (vop_t *) afs_vop_unlock },          /* unlock */
98         { &vop_write_desc, (vop_t *) afs_vop_write },            /* write */
99         { &vop_ioctl_desc, (vop_t *) afs_vop_ioctl }, /* XXX ioctl */
100         /*{ &vop_seek_desc, afs_vop_seek },*/              /* seek */
101         { NULL, NULL }
102 };
103 struct vnodeopv_desc afs_vnodeop_opv_desc =
104         { &afs_vnodeop_p, afs_vnodeop_entries };
105
106 #define GETNAME()       \
107     struct componentname *cnp = ap->a_cnp; \
108     char *name; \
109     MALLOC(name, char *, cnp->cn_namelen+1, M_TEMP, M_WAITOK); \
110     memcpy(name, cnp->cn_nameptr, cnp->cn_namelen); \
111     name[cnp->cn_namelen] = '\0'
112
113 #define DROPNAME() FREE(name, M_TEMP)
114         
115
116
117 int
118 afs_vop_lookup(ap)
119 struct vop_lookup_args /* {
120                           struct vnodeop_desc * a_desc;
121                           struct vnode *a_dvp;
122                           struct vnode **a_vpp;
123                           struct componentname *a_cnp;
124                           } */ *ap;
125 {
126     int error;
127     struct vcache *vcp;
128     struct vnode *vp, *dvp;
129     register int flags = ap->a_cnp->cn_flags;
130     int lockparent;                     /* 1 => lockparent flag is set */
131     int wantparent;                     /* 1 => wantparent or lockparent flag */
132 #ifdef AFS_FBSD50_ENV
133     struct thread *p = ap->a_cnp->cn_thread;
134 #else
135     struct proc *p = ap->a_cnp->cn_proc;
136 #endif
137     GETNAME();
138
139     lockparent = flags & LOCKPARENT;
140     wantparent = flags & (LOCKPARENT|WANTPARENT);
141
142     if (ap->a_dvp->v_type != VDIR) {
143         *ap->a_vpp = 0;
144         DROPNAME();
145         return ENOTDIR;
146     }
147     dvp = ap->a_dvp;
148     if (flags & ISDOTDOT) 
149        VOP_UNLOCK(dvp, 0, p);
150     AFS_GLOCK();
151     error = afs_lookup(VTOAFS(dvp), name, &vcp, cnp->cn_cred);
152     AFS_GUNLOCK();
153     if (error) {
154         if (flags & ISDOTDOT) 
155            VOP_LOCK(dvp, LK_EXCLUSIVE | LK_RETRY, p);
156         if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
157             (flags & ISLASTCN) && error == ENOENT)
158             error = EJUSTRETURN;
159         if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
160             cnp->cn_flags |= SAVENAME;
161         DROPNAME();
162         *ap->a_vpp = 0;
163         return (error);
164     }
165     vp = AFSTOV(vcp);  /* always get a node if no error */
166
167     /* The parent directory comes in locked.  We unlock it on return
168        unless the caller wants it left locked.
169        we also always return the vnode locked. */
170
171     if (flags & ISDOTDOT) {
172         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
173         /* always return the child locked */
174         if (lockparent && (flags & ISLASTCN) &&
175            (error = vn_lock(dvp, LK_EXCLUSIVE, p))) {
176             vput(vp);
177             DROPNAME();
178             return (error);
179         }
180     } else if (vp == dvp) {
181         /* they're the same; afs_lookup() already ref'ed the leaf.
182            It came in locked, so we don't need to ref OR lock it */
183     } else {
184         if (!lockparent || !(flags & ISLASTCN))
185             VOP_UNLOCK(dvp, 0, p);         /* done with parent. */
186         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
187         /* always return the child locked */
188     }
189     *ap->a_vpp = vp;
190
191     if ((cnp->cn_nameiop == RENAME && wantparent && (flags & ISLASTCN)) ||
192          (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN)))
193         cnp->cn_flags |= SAVENAME;
194
195     DROPNAME();
196     return error;
197 }
198
199 int
200 afs_vop_create(ap)
201         struct vop_create_args /* {
202                 struct vnode *a_dvp;
203                 struct vnode **a_vpp;
204                 struct componentname *a_cnp;
205                 struct vattr *a_vap;
206         } */ *ap;
207 {
208     int error = 0;
209     struct vcache *vcp;
210     register struct vnode *dvp = ap->a_dvp;
211 #ifdef AFS_FBSD50_ENV
212     struct thread *p = ap->a_cnp->cn_thread;
213 #else
214     struct proc *p = ap->a_cnp->cn_proc;
215 #endif
216     GETNAME();
217
218     AFS_GLOCK();
219     error = afs_create(VTOAFS(dvp), name, ap->a_vap, ap->a_vap->va_vaflags & VA_EXCLUSIVE? EXCL : NONEXCL,
220                        ap->a_vap->va_mode, &vcp,
221                        cnp->cn_cred);
222     AFS_GUNLOCK();
223     if (error) {
224         DROPNAME();
225         return(error);
226     }
227
228     if (vcp) {
229         *ap->a_vpp = AFSTOV(vcp);
230         vn_lock(AFSTOV(vcp), LK_EXCLUSIVE| LK_RETRY, p);
231     }
232     else *ap->a_vpp = 0;
233
234     DROPNAME();
235     return error;
236 }
237
238 int
239 afs_vop_mknod(ap)
240         struct vop_mknod_args /* {
241                 struct vnode *a_dvp;
242                 struct vnode **a_vpp;
243                 struct componentname *a_cnp;
244                 struct vattr *a_vap;
245         } */ *ap;
246 {
247     return(ENODEV);
248 }
249
250 #if 0
251 static int validate_vops(struct vnode *vp, int after) 
252 {
253    int ret=0;
254    struct vnodeopv_entry_desc *this;
255    for (this=afs_vnodeop_entries; this->opve_op; this++) {
256         if (vp->v_op[this->opve_op->vdesc_offset] != this->opve_impl) {
257             if (!ret) {
258                 printf("v_op %d ", after);
259                 vprint("check", vp);
260             }
261             ret=1;
262             printf("For oper %d (%s), func is %p, not %p",
263                     this->opve_op->vdesc_offset, this->opve_op->vdesc_name,
264                     vp->v_op[this->opve_op->vdesc_offset],  this->opve_impl);
265         }
266    }
267    return ret;
268
269 #endif
270 int
271 afs_vop_open(ap)
272         struct vop_open_args /* {
273                 struct vnode *a_vp;
274                 int  a_mode;
275                 struct ucred *a_cred;
276                 struct proc *a_p;
277         } */ *ap;
278 {
279     int error;
280     int bad;
281     struct vcache *vc = VTOAFS(ap->a_vp);
282     bad=0;
283     AFS_GLOCK();
284     error = afs_open(&vc, ap->a_mode, ap->a_cred);
285 #ifdef DIAGNOSTIC
286     if (AFSTOV(vc) != ap->a_vp)
287         panic("AFS open changed vnode!");
288 #endif
289     afs_BozonLock(&vc->pvnLock, vc);
290     osi_FlushPages(vc, ap->a_cred);
291     afs_BozonUnlock(&vc->pvnLock, vc);
292     AFS_GUNLOCK();
293     return error;
294 }
295
296 int
297 afs_vop_close(ap)
298         struct vop_close_args /* {
299                 struct vnode *a_vp;
300                 int  a_fflag;
301                 struct ucred *a_cred;
302                 struct proc *a_p;
303         } */ *ap;
304 {
305     int code;
306     struct vcache *avc=ap->a_vp;
307     AFS_GLOCK();
308     if (ap->a_cred) 
309         code=afs_close(avc, ap->a_fflag, ap->a_cred, ap->a_p);
310     else
311         code=afs_close(avc, ap->a_fflag, &afs_osi_cred, ap->a_p);
312     afs_BozonLock(&avc->pvnLock, avc);
313     osi_FlushPages(avc, ap->a_cred);        /* hold bozon lock, but not basic vnode lock */
314     afs_BozonUnlock(&avc->pvnLock, avc);
315     AFS_GUNLOCK();
316     return code;
317 }
318
319 int
320 afs_vop_access(ap)
321         struct vop_access_args /* {
322                 struct vnode *a_vp;
323                 int  a_mode;
324                 struct ucred *a_cred;
325                 struct proc *a_p;
326         } */ *ap;
327 {
328     int code;
329     AFS_GLOCK();
330     code=afs_access(VTOAFS(ap->a_vp), ap->a_mode, ap->a_cred);
331     AFS_GUNLOCK();
332     return code;
333 }
334 int
335 afs_vop_getattr(ap)
336         struct vop_getattr_args /* {
337                 struct vnode *a_vp;
338                 struct vattr *a_vap;
339                 struct ucred *a_cred;
340                 struct proc *a_p;
341         } */ *ap;
342 {
343     int code;
344     AFS_GLOCK();
345     code=afs_getattr(VTOAFS(ap->a_vp), ap->a_vap, ap->a_cred);
346     AFS_GUNLOCK();
347     return code;
348 }
349 int
350 afs_vop_setattr(ap)
351         struct vop_setattr_args /* {
352                 struct vnode *a_vp;
353                 struct vattr *a_vap;
354                 struct ucred *a_cred;
355                 struct proc *a_p;
356         } */ *ap;
357 {
358     int code;
359     AFS_GLOCK();
360     code=afs_setattr(VTOAFS(ap->a_vp), ap->a_vap, ap->a_cred);
361     AFS_GUNLOCK();
362     return code;
363 }int
364 afs_vop_read(ap)
365         struct vop_read_args /* {
366                 struct vnode *a_vp;
367                 struct uio *a_uio;
368                 int a_ioflag;
369                 struct ucred *a_cred;
370         
371 } */ *ap;
372 {
373     int code;
374     struct vcache *avc=VTOAFS(ap->a_vp);
375     AFS_GLOCK();
376     afs_BozonLock(&avc->pvnLock, avc);
377     osi_FlushPages(avc, ap->a_cred);        /* hold bozon lock, but not basic vnode lock */
378     code=afs_read(avc, ap->a_uio, ap->a_cred, 0, 0, 0);
379     afs_BozonUnlock(&avc->pvnLock, avc);
380     AFS_GUNLOCK();
381     return code;
382 }
383 int
384 afs_vop_getpages(ap)
385         struct vop_getpages_args /* {
386                 struct vnode *a_vp;
387                 vm_page_t *a_m;
388                 int a_count;
389                 int a_reqpage;
390                 vm_oofset_t a_offset;
391         } */ *ap;
392 {
393     int code;
394     int i, nextoff, size, toff, npages;
395     struct uio uio;
396     struct iovec iov;
397     struct buf *bp;
398     vm_offset_t kva;
399     struct vcache *avc=VTOAFS(ap->a_vp);
400
401     if (avc->v.v_object == NULL) {
402         printf("afs_getpages: called with non-merged cache vnode??\n");
403         return VM_PAGER_ERROR;
404     }
405     npages=btoc(ap->a_count);
406     /*
407      * If the requested page is partially valid, just return it and
408      * allow the pager to zero-out the blanks.  Partially valid pages
409      * can only occur at the file EOF.
410      */
411
412     {
413         vm_page_t m = ap->a_m[ap->a_reqpage];
414
415         if (m->valid != 0) {
416             /* handled by vm_fault now        */
417             /* vm_page_zero_invalid(m, TRUE); */
418             for (i = 0; i < npages; ++i) {
419                 if (i != ap->a_reqpage)
420                     vnode_pager_freepage(ap->a_m[i]);
421             }
422             return(0);
423         }
424     }
425     bp = getpbuf(&afs_pbuf_freecnt);
426     kva = (vm_offset_t) bp->b_data;
427     pmap_qenter(kva, ap->a_m, npages);
428     iov.iov_base = (caddr_t)kva;
429     iov.iov_len = ap->a_count;
430     uio.uio_iov = &iov;
431     uio.uio_iovcnt = 1;
432     uio.uio_offset = IDX_TO_OFF(ap->a_m[0]->pindex);
433     uio.uio_resid = ap->a_count;
434     uio.uio_segflg = UIO_SYSSPACE;
435     uio.uio_rw = UIO_READ;
436 #ifdef AFS_FBSD50_ENV
437     uio.uio_td = curthread;
438 #else
439     uio.uio_procp = curproc;
440 #endif
441     AFS_GLOCK();
442     afs_BozonLock(&avc->pvnLock, avc);
443     osi_FlushPages(avc, osi_curcred());  /* hold bozon lock, but not basic vnode lock */
444     code=afs_read(avc, &uio, osi_curcred(), 0, 0, 0);
445     afs_BozonUnlock(&avc->pvnLock, avc);
446     AFS_GUNLOCK();
447     pmap_qremove(kva, npages);
448
449     relpbuf(bp, &afs_pbuf_freecnt);
450     if (code && (uio.uio_resid == ap->a_count)) {
451         for (i = 0; i < npages; ++i) {
452             if (i != ap->a_reqpage)
453                 vnode_pager_freepage(ap->a_m[i]);
454         }
455         return VM_PAGER_ERROR;
456     }
457     size = ap->a_count - uio.uio_resid;
458     for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
459         vm_page_t m;
460         nextoff = toff + PAGE_SIZE;
461         m = ap->a_m[i];
462
463         m->flags &= ~PG_ZERO;
464
465         if (nextoff <= size) {
466             /*
467              * Read operation filled an entire page
468              */
469             m->valid = VM_PAGE_BITS_ALL;
470             vm_page_undirty(m);
471         } else if (size > toff) {
472             /*
473              * Read operation filled a partial page.
474              */
475             m->valid = 0;
476             vm_page_set_validclean(m, 0, size - toff);
477             /* handled by vm_fault now        */
478             /* vm_page_zero_invalid(m, TRUE); */
479         }
480         
481         if (i != ap->a_reqpage) {
482             /*
483              * Whether or not to leave the page activated is up in
484              * the air, but we should put the page on a page queue
485              * somewhere (it already is in the object).  Result:
486              * It appears that emperical results show that
487              * deactivating pages is best.
488              */
489
490             /*
491              * Just in case someone was asking for this page we
492              * now tell them that it is ok to use.
493              */
494             if (!code) {
495                 if (m->flags & PG_WANTED)
496                     vm_page_activate(m);
497                 else
498                     vm_page_deactivate(m);
499                 vm_page_wakeup(m);
500             } else {
501                 vnode_pager_freepage(m);
502             }
503         }
504     }
505     return 0;
506 }
507         
508 int
509 afs_vop_write(ap)
510         struct vop_write_args /* {
511                 struct vnode *a_vp;
512                 struct uio *a_uio;
513                 int a_ioflag;
514                 struct ucred *a_cred;
515         } */ *ap;
516 {
517     int code;
518     struct vcache *avc=VTOAFS(ap->a_vp);
519     AFS_GLOCK();
520     afs_BozonLock(&avc->pvnLock, avc);
521     osi_FlushPages(avc, ap->a_cred);        /* hold bozon lock, but not basic vnode lock */
522     code=afs_write(VTOAFS(ap->a_vp), ap->a_uio, ap->a_ioflag, ap->a_cred, 0);
523     afs_BozonUnlock(&avc->pvnLock, avc);
524     AFS_GUNLOCK();
525     return code;
526 }
527
528 int
529 afs_vop_putpages(ap)
530         struct vop_putpages_args /* {
531                 struct vnode *a_vp;
532                 vm_page_t *a_m;
533                 int a_count;
534                 int a_sync;
535                 int *a_rtvals;
536                 vm_oofset_t a_offset;
537         } */ *ap;
538 {
539     int code;
540     int i, size, npages, sync;
541     struct uio uio;
542     struct iovec iov;
543     struct buf *bp;
544     vm_offset_t kva;
545     struct vcache *avc=VTOAFS(ap->a_vp);
546
547     if (avc->v.v_object == NULL) {
548         printf("afs_putpages: called with non-merged cache vnode??\n");
549         return VM_PAGER_ERROR;
550     }
551     if (vType(avc) != VREG) {
552         printf("afs_putpages: not VREG");
553         return VM_PAGER_ERROR;
554     }
555     npages=btoc(ap->a_count);
556     for (i=0; i < npages; i++ ) ap->a_rtvals[i]=VM_PAGER_AGAIN;
557     bp = getpbuf(&afs_pbuf_freecnt);
558     kva = (vm_offset_t) bp->b_data;
559     pmap_qenter(kva, ap->a_m, npages);
560     iov.iov_base = (caddr_t)kva;
561     iov.iov_len = ap->a_count;
562     uio.uio_iov = &iov;
563     uio.uio_iovcnt = 1;
564     uio.uio_offset = IDX_TO_OFF(ap->a_m[0]->pindex);
565     uio.uio_resid = ap->a_count;
566     uio.uio_segflg = UIO_SYSSPACE;
567     uio.uio_rw = UIO_WRITE;
568 #ifdef AFS_FBSD50_ENV
569     uio.uio_td = curthread;
570 #else
571     uio.uio_procp = curproc;
572 #endif
573     sync = IO_VMIO;
574     if (ap->a_sync & VM_PAGER_PUT_SYNC)
575        sync |= IO_SYNC;
576     /*if (ap->a_sync & VM_PAGER_PUT_INVAL)
577        sync |= IO_INVAL;*/
578
579     AFS_GLOCK();
580     afs_BozonLock(&avc->pvnLock, avc);
581     code = afs_write(avc, &uio, sync, osi_curcred(), 0);
582     afs_BozonUnlock(&avc->pvnLock, avc);
583     AFS_GUNLOCK();
584     pmap_qremove(kva, npages);
585
586     relpbuf(bp, &afs_pbuf_freecnt);
587     if (!code) {
588         size = ap->a_count - uio.uio_resid;
589         for (i = 0; i < round_page(size) / PAGE_SIZE; i++) {
590             ap->a_rtvals[i]=VM_PAGER_OK;
591             ap->a_m[i]->dirty=0;
592         }
593         return VM_PAGER_ERROR;
594     }
595     return ap->a_rtvals[0];
596 }
597
598 int
599 afs_vop_ioctl(ap)
600         struct vop_ioctl_args /* {
601                 struct vnode *a_vp;
602                 int  a_command;
603                 caddr_t  a_data;
604                 int  a_fflag;
605                 struct ucred *a_cred;
606                 struct proc *a_p;
607         } */ *ap;
608 {
609     struct vcache *tvc = VTOAFS(ap->a_vp);
610     int error = 0;
611   
612     /* in case we ever get in here... */
613
614     AFS_STATCNT(afs_ioctl);
615     if (((ap->a_command >> 8) & 0xff) == 'V') {
616         /* This is a VICEIOCTL call */
617         AFS_GLOCK();
618         error = HandleIoctl(tvc, NULL /*Not used*/,
619                             ap->a_command, ap->a_data);
620         AFS_GUNLOCK();
621         return(error);
622     } else {
623         /* No-op call; just return. */
624         return(ENOTTY);
625     }
626 }
627
628 /* ARGSUSED */
629 int
630 afs_vop_poll(ap)
631         struct vop_poll_args /* {
632                 struct vnode *a_vp;
633                 int  a_events;
634                 struct ucred *a_cred;
635                 struct proc *a_p;
636         } */ *ap;
637 {
638     /*
639      * We should really check to see if I/O is possible.
640      */
641     return (1);
642 }
643 /*
644  * Mmap a file
645  *
646  * NB Currently unsupported.
647  */
648 /* ARGSUSED */
649 int
650 afs_vop_mmap(ap)
651         struct vop_mmap_args /* {
652                 struct vnode *a_vp;
653                 int  a_fflags;
654                 struct ucred *a_cred;
655                 struct proc *a_p;
656         } */ *ap;
657 {
658         return (EINVAL);
659 }
660
661 int
662 afs_vop_fsync(ap)
663         struct vop_fsync_args /* {
664                 struct vnode *a_vp;
665                 struct ucred *a_cred;
666                 int a_waitfor;
667                 struct proc *a_p;
668         } */ *ap;
669 {
670     int error;
671     register struct vnode *vp = ap->a_vp;
672
673     AFS_GLOCK();
674     /*vflushbuf(vp, wait);*/
675     if (ap->a_cred)
676         error = afs_fsync(VTOAFS(vp), ap->a_cred);
677     else
678         error = afs_fsync(VTOAFS(vp), &afs_osi_cred);
679     AFS_GUNLOCK();
680     return error;
681 }
682
683 int
684 afs_vop_remove(ap)
685         struct vop_remove_args /* {
686                 struct vnode *a_dvp;
687                 struct vnode *a_vp;
688                 struct componentname *a_cnp;
689         } */ *ap;
690 {
691     int error = 0;
692     register struct vnode *vp = ap->a_vp;
693     register struct vnode *dvp = ap->a_dvp;
694
695     GETNAME();
696     AFS_GLOCK();
697     error =  afs_remove(VTOAFS(dvp), name, cnp->cn_cred);
698     AFS_GUNLOCK();
699     cache_purge(vp);
700     DROPNAME();
701     return error;
702 }
703
704 int
705 afs_vop_link(ap)
706         struct vop_link_args /* {
707                 struct vnode *a_vp;
708                 struct vnode *a_tdvp;
709                 struct componentname *a_cnp;
710         } */ *ap;
711 {
712     int error = 0;
713     register struct vnode *dvp = ap->a_tdvp;
714     register struct vnode *vp = ap->a_vp;
715 #ifdef AFS_FBSD50_ENV
716     struct thread *p = ap->a_cnp->cn_thread;
717 #else
718     struct proc *p = ap->a_cnp->cn_proc;
719 #endif
720
721     GETNAME();
722     if (dvp->v_mount != vp->v_mount) {
723         error = EXDEV;
724         goto out;
725     }
726     if (vp->v_type == VDIR) {
727         error = EISDIR;
728         goto out;
729     }
730     if ((error = vn_lock(vp, LK_EXCLUSIVE, p)) != 0) {
731         goto out;
732     }
733     AFS_GLOCK();
734     error = afs_link(VTOAFS(vp), VTOAFS(dvp), name, cnp->cn_cred);
735     AFS_GUNLOCK();
736     if (dvp != vp)
737         VOP_UNLOCK(vp,0, p);
738 out:
739     DROPNAME();
740     return error;
741 }
742
743 int
744 afs_vop_rename(ap)
745         struct vop_rename_args  /* {
746                 struct vnode *a_fdvp;
747                 struct vnode *a_fvp;
748                 struct componentname *a_fcnp;
749                 struct vnode *a_tdvp;
750                 struct vnode *a_tvp;
751                 struct componentname *a_tcnp;
752         } */ *ap;
753 {
754     int error = 0;
755     struct componentname *fcnp = ap->a_fcnp;
756     char *fname;
757     struct componentname *tcnp = ap->a_tcnp;
758     char *tname;
759     struct vnode *tvp = ap->a_tvp;
760     register struct vnode *tdvp = ap->a_tdvp;
761     struct vnode *fvp = ap->a_fvp;
762     register struct vnode *fdvp = ap->a_fdvp;
763 #ifdef AFS_FBSD50_ENV
764     struct thread *p = fcnp->cn_thread;
765 #else
766     struct proc *p = fcnp->cn_proc;
767 #endif
768
769     /*
770      * Check for cross-device rename.
771      */
772     if ((fvp->v_mount != tdvp->v_mount) ||
773         (tvp && (fvp->v_mount != tvp->v_mount))) {
774         error = EXDEV;
775 abortit:
776         if (tdvp == tvp)
777             vrele(tdvp);
778         else
779             vput(tdvp);
780         if (tvp)
781             vput(tvp);
782         vrele(fdvp);
783         vrele(fvp);
784         return (error);
785     }
786     /*
787      * if fvp == tvp, we're just removing one name of a pair of
788      * directory entries for the same element.  convert call into rename.
789      ( (pinched from FreeBSD 4.4's ufs_rename())
790        
791      */
792     if (fvp == tvp) {
793         if (fvp->v_type == VDIR) {
794             error = EINVAL;
795             goto abortit;
796         }
797
798         /* Release destination completely. */
799         vput(tdvp);
800         vput(tvp);
801
802         /* Delete source. */
803         vrele(fdvp);
804         vrele(fvp);
805         fcnp->cn_flags &= ~MODMASK;
806         fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
807         if ((fcnp->cn_flags & SAVESTART) == 0)
808             panic("afs_rename: lost from startdir");
809         fcnp->cn_nameiop = DELETE;
810         VREF(fdvp);
811         error=relookup(fdvp, &fvp, fcnp);
812         if (error == 0)
813                 vrele(fdvp);
814         if (fvp == NULL) {
815                 return (ENOENT);
816         }
817         
818         error=VOP_REMOVE(fdvp, fvp, fcnp);
819         if (fdvp == fvp)
820             vrele(fdvp);
821         else
822             vput(fdvp);
823         vput(fvp);
824         return (error);
825     }
826     if ((error = vn_lock(fvp, LK_EXCLUSIVE, p)) != 0)
827         goto abortit;
828
829     MALLOC(fname, char *, fcnp->cn_namelen+1, M_TEMP, M_WAITOK);
830     memcpy(fname, fcnp->cn_nameptr, fcnp->cn_namelen);
831     fname[fcnp->cn_namelen] = '\0';
832     MALLOC(tname, char *, tcnp->cn_namelen+1, M_TEMP, M_WAITOK);
833     memcpy(tname, tcnp->cn_nameptr, tcnp->cn_namelen);
834     tname[tcnp->cn_namelen] = '\0';
835
836
837     AFS_GLOCK();
838     /* XXX use "from" or "to" creds? NFS uses "to" creds */
839     error = afs_rename(VTOAFS(fdvp), fname, VTOAFS(tdvp), tname, tcnp->cn_cred);
840     AFS_GUNLOCK();
841
842     FREE(fname, M_TEMP);
843     FREE(tname, M_TEMP);
844     if (tdvp == tvp)
845         vrele(tdvp);
846     else
847         vput(tdvp);
848     if (tvp)
849         vput(tvp);
850     vrele(fdvp);
851     vput(fvp);
852     return error;
853 }
854
855 int
856 afs_vop_mkdir(ap)
857         struct vop_mkdir_args /* {
858                 struct vnode *a_dvp;
859                 struct vnode **a_vpp;
860                 struct componentname *a_cnp;
861                 struct vattr *a_vap;
862         } */ *ap;
863 {
864     register struct vnode *dvp = ap->a_dvp;
865     register struct vattr *vap = ap->a_vap;
866     int error = 0;
867     struct vcache *vcp;
868 #ifdef AFS_FBSD50_ENV
869     struct thread *p = ap->a_cnp->cn_thread;
870 #else
871     struct proc *p = ap->a_cnp->cn_proc;
872 #endif
873
874     GETNAME();
875 #ifdef DIAGNOSTIC
876     if ((cnp->cn_flags & HASBUF) == 0)
877         panic("afs_vop_mkdir: no name");
878 #endif
879     AFS_GLOCK();
880     error = afs_mkdir(VTOAFS(dvp), name, vap, &vcp, cnp->cn_cred);
881     AFS_GUNLOCK();
882     if (error) {
883         vput(dvp);
884         DROPNAME();
885         return(error);
886     }
887     if (vcp) {
888         *ap->a_vpp = AFSTOV(vcp);
889         vn_lock(AFSTOV(vcp), LK_EXCLUSIVE|LK_RETRY, p);
890     } else
891         *ap->a_vpp = 0;
892     DROPNAME();
893     return error;
894 }
895
896 int
897 afs_vop_rmdir(ap)
898         struct vop_rmdir_args /* {
899                 struct vnode *a_dvp;
900                 struct vnode *a_vp;
901                 struct componentname *a_cnp;
902         } */ *ap;
903 {
904     int error = 0;
905     register struct vnode *dvp = ap->a_dvp;
906
907     GETNAME();
908     AFS_GLOCK();
909     error = afs_rmdir(VTOAFS(dvp), name, cnp->cn_cred);
910     AFS_GUNLOCK();
911     DROPNAME();
912     return error;
913 }
914
915 int
916 afs_vop_symlink(ap)
917         struct vop_symlink_args /* {
918                 struct vnode *a_dvp;
919                 struct vnode **a_vpp;
920                 struct componentname *a_cnp;
921                 struct vattr *a_vap;
922                 char *a_target;
923         } */ *ap;
924 {
925     register struct vnode *dvp = ap->a_dvp;
926     int error = 0;
927     /* NFS ignores a_vpp; so do we. */
928
929     GETNAME();
930     AFS_GLOCK();
931     error = afs_symlink(VTOAFS(dvp), name, ap->a_vap, ap->a_target,
932                         cnp->cn_cred);
933     AFS_GUNLOCK();
934     DROPNAME();
935     return error;
936 }
937
938 int
939 afs_vop_readdir(ap)
940         struct vop_readdir_args /* {
941                 struct vnode *a_vp;
942                 struct uio *a_uio;
943                 struct ucred *a_cred;
944                 int *a_eofflag;
945                 u_long *a_cookies;
946                 int ncookies;
947         } */ *ap;
948 {
949     int error;
950     off_t off;
951 /*    printf("readdir %x cookies %x ncookies %d\n", ap->a_vp, ap->a_cookies,
952            ap->a_ncookies); */
953     off=ap->a_uio->uio_offset;
954     AFS_GLOCK();
955     error= afs_readdir(VTOAFS(ap->a_vp), ap->a_uio, ap->a_cred,
956                        ap->a_eofflag);
957     AFS_GUNLOCK();
958     if (!error && ap->a_ncookies != NULL) {
959         struct uio *uio = ap->a_uio;
960         const struct dirent *dp, *dp_start, *dp_end;
961         int ncookies;
962         u_long *cookies, *cookiep;
963
964         if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
965             panic("afs_readdir: burned cookies");
966         dp = (const struct dirent *)
967             ((const char *)uio->uio_iov->iov_base - (uio->uio_offset - off));
968
969         dp_end = (const struct dirent *) uio->uio_iov->iov_base;
970         for (dp_start = dp, ncookies = 0;
971              dp < dp_end;
972              dp = (const struct dirent *)((const char *) dp + dp->d_reclen))
973             ncookies++;
974
975         MALLOC(cookies, u_long *, ncookies * sizeof(u_long),
976                M_TEMP, M_WAITOK);
977         for (dp = dp_start, cookiep = cookies;
978              dp < dp_end;
979              dp = (const struct dirent *)((const char *) dp + dp->d_reclen)) {
980             off += dp->d_reclen;
981             *cookiep++ = off;
982         }
983         *ap->a_cookies = cookies;
984         *ap->a_ncookies = ncookies;
985     }
986
987     return error;
988 }
989
990 int
991 afs_vop_readlink(ap)
992         struct vop_readlink_args /* {
993                 struct vnode *a_vp;
994                 struct uio *a_uio;
995                 struct ucred *a_cred;
996         } */ *ap;
997 {
998     int error;
999 /*    printf("readlink %x\n", ap->a_vp);*/
1000     AFS_GLOCK();
1001     error= afs_readlink(VTOAFS(ap->a_vp), ap->a_uio, ap->a_cred);
1002     AFS_GUNLOCK();
1003     return error;
1004 }
1005
1006 extern int prtactive;
1007
1008 int
1009 afs_vop_inactive(ap)
1010         struct vop_inactive_args /* {
1011                 struct vnode *a_vp;
1012                 struct proc *a_p;
1013         } */ *ap;
1014 {
1015     register struct vnode *vp = ap->a_vp;
1016
1017     if (prtactive && vp->v_usecount != 0)
1018         vprint("afs_vop_inactive(): pushing active", vp);
1019
1020     AFS_GLOCK();
1021     afs_InactiveVCache(VTOAFS(vp), 0);   /* decrs ref counts */
1022     AFS_GUNLOCK();
1023     VOP_UNLOCK(vp, 0, ap->a_p);
1024     return 0;
1025 }
1026
1027 int
1028 afs_vop_reclaim(ap)
1029         struct vop_reclaim_args /* {
1030                 struct vnode *a_vp;
1031         } */ *ap;
1032 {
1033     int error;
1034     int sl;
1035     register struct vnode *vp = ap->a_vp;
1036
1037     cache_purge(vp);                    /* just in case... */
1038
1039 #if 0 
1040     AFS_GLOCK();
1041     error = afs_FlushVCache(VTOAFS(vp), &sl); /* tosses our stuff from vnode */
1042     AFS_GUNLOCK();
1043     ubc_unlink(vp);
1044     if (!error && vp->v_data)
1045         panic("afs_reclaim: vnode not cleaned");
1046     return error;
1047 #else
1048     if (vp->v_usecount == 2) {
1049         vprint("reclaim count==2", vp);
1050     } else if (vp->v_usecount == 1) {
1051         vprint("reclaim count==1", vp);
1052     } else 
1053         vprint("reclaim bad count", vp);
1054
1055     return 0;
1056 #endif
1057 }
1058
1059 int
1060 afs_vop_lock(ap)
1061         struct vop_lock_args /* {
1062                 struct vnode *a_vp;
1063         } */ *ap;
1064 {
1065     register struct vnode *vp = ap->a_vp;
1066     register struct vcache *avc = VTOAFS(vp);
1067
1068 #ifdef AFS_FBSD50_ENV
1069     if (!strcmp(vp->v_tag, "none"))
1070 #else
1071     if (vp->v_tag == VT_NON)
1072 #endif
1073         return (ENOENT);
1074     return (lockmgr(&avc->rwlock, ap->a_flags, &vp->v_interlock,
1075                     ap->a_p));
1076 }
1077
1078 int
1079 afs_vop_unlock(ap)
1080         struct vop_unlock_args /* {
1081                 struct vnode *a_vp;
1082         } */ *ap;
1083 {
1084     struct vnode *vp = ap->a_vp;
1085     struct vcache *avc = VTOAFS(vp);
1086     return (lockmgr(&avc->rwlock, ap->a_flags | LK_RELEASE,
1087             &vp->v_interlock, ap->a_p));
1088
1089 }
1090
1091 int
1092 afs_vop_bmap(ap)
1093         struct vop_bmap_args /* {
1094                 struct vnode *a_vp;
1095                 daddr_t  a_bn;
1096                 struct vnode **a_vpp;
1097                 daddr_t *a_bnp;
1098                 int *a_runp;
1099                 int *a_runb;
1100         } */ *ap;
1101 {
1102     if (ap->a_bnp) {
1103         *ap->a_bnp = ap->a_bn * (PAGE_SIZE / DEV_BSIZE);
1104     }
1105     if (ap->a_vpp) {
1106         *ap->a_vpp = ap->a_vp;
1107     }
1108     if (ap->a_runp != NULL)
1109         *ap->a_runp = 0;
1110     if (ap->a_runb != NULL)
1111         *ap->a_runb = 0;
1112  
1113     return 0;
1114 }
1115 int
1116 afs_vop_strategy(ap)
1117         struct vop_strategy_args /* {
1118                 struct buf *a_bp;
1119         } */ *ap;
1120 {
1121     int error;
1122     AFS_GLOCK();
1123     error= afs_ustrategy(ap->a_bp);
1124     AFS_GUNLOCK();
1125     return error;
1126 }
1127 int
1128 afs_vop_print(ap)
1129         struct vop_print_args /* {
1130                 struct vnode *a_vp;
1131         } */ *ap;
1132 {
1133     register struct vnode *vp = ap->a_vp;
1134     register struct vcache *vc = VTOAFS(ap->a_vp);
1135     int s = vc->states;
1136     printf("tag %d, fid: %ld.%x.%x.%x, opens %d, writers %d", vp->v_tag, vc->fid.Cell,
1137            vc->fid.Fid.Volume, vc->fid.Fid.Vnode, vc->fid.Fid.Unique, vc->opens,
1138            vc->execsOrWriters);
1139     printf("\n  states%s%s%s%s%s", (s&CStatd) ? " statd" : "", (s&CRO) ? " readonly" : "",(s&CDirty) ? " dirty" : "",(s&CMAPPED) ? " mapped" : "", (s&CVFlushed) ? " flush in progress" : "");
1140     printf("\n");
1141     return 0;
1142 }
1143
1144 int
1145 afs_vop_islocked(ap)
1146         struct vop_islocked_args /* {
1147                 struct vnode *a_vp;
1148         } */ *ap;
1149 {
1150     struct vcache *vc = VTOAFS(ap->a_vp);
1151     return lockstatus(&vc->rwlock, ap->a_p);
1152 }
1153
1154 /*
1155  * Advisory record locking support (fcntl() POSIX style)
1156  */
1157 int
1158 afs_vop_advlock(ap)
1159         struct vop_advlock_args /* {
1160                 struct vnode *a_vp;
1161                 caddr_t  a_id;
1162                 int  a_op;
1163                 struct flock *a_fl;
1164                 int  a_flags;
1165         } */ *ap;
1166 {
1167     int error;
1168     struct ucred cr = *osi_curcred();
1169
1170     AFS_GLOCK();
1171     error= afs_lockctl(VTOAFS(ap->a_vp), ap->a_fl, ap->a_op, &cr,
1172                        (int) ap->a_id);
1173     AFS_GUNLOCK();
1174     return error;
1175 }