]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/fs/coda/coda_vnops.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / fs / coda / coda_vnops.c
1 /*-
2  *             Coda: an Experimental Distributed File System
3  *                              Release 3.1
4  *
5  *           Copyright (c) 1987-1998 Carnegie Mellon University
6  *                          All Rights Reserved
7  *
8  * Permission  to  use, copy, modify and distribute this software and its
9  * documentation is hereby granted,  provided  that  both  the  copyright
10  * notice  and  this  permission  notice  appear  in  all  copies  of the
11  * software, derivative works or  modified  versions,  and  any  portions
12  * thereof, and that both notices appear in supporting documentation, and
13  * that credit is given to Carnegie Mellon University  in  all  documents
14  * and publicity pertaining to direct or indirect use of this code or its
15  * derivatives.
16  *
17  * CODA IS AN EXPERIMENTAL SOFTWARE SYSTEM AND IS  KNOWN  TO  HAVE  BUGS,
18  * SOME  OF  WHICH MAY HAVE SERIOUS CONSEQUENCES.  CARNEGIE MELLON ALLOWS
19  * FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.   CARNEGIE  MELLON
20  * DISCLAIMS  ANY  LIABILITY  OF  ANY  KIND  FOR  ANY  DAMAGES WHATSOEVER
21  * RESULTING DIRECTLY OR INDIRECTLY FROM THE USE OF THIS SOFTWARE  OR  OF
22  * ANY DERIVATIVE WORK.
23  *
24  * Carnegie  Mellon  encourages  users  of  this  software  to return any
25  * improvements or extensions that  they  make,  and  to  grant  Carnegie
26  * Mellon the rights to redistribute these changes without encumbrance.
27  *
28  *      @(#) src/sys/coda/coda_vnops.c,v 1.1.1.1 1998/08/29 21:14:52 rvb Exp $
29  */
30 /*
31  * Mach Operating System
32  * Copyright (c) 1990 Carnegie-Mellon University
33  * Copyright (c) 1989 Carnegie-Mellon University
34  * All rights reserved.  The CMU software License Agreement specifies
35  * the terms and conditions for use and redistribution.
36  */
37
38 /*
39  * This code was written for the Coda filesystem at Carnegie Mellon
40  * University.  Contributers include David Steere, James Kistler, and
41  * M. Satyanarayanan.
42  */
43
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/acct.h>
50 #include <sys/errno.h>
51 #include <sys/fcntl.h>
52 #include <sys/kernel.h>
53 #include <sys/lock.h>
54 #include <sys/malloc.h>
55 #include <sys/file.h>           /* Must come after sys/malloc.h */
56 #include <sys/mount.h>
57 #include <sys/mutex.h>
58 #include <sys/namei.h>
59 #include <sys/proc.h>
60 #include <sys/uio.h>
61 #include <sys/unistd.h>
62
63 #include <vm/vm.h>
64 #include <vm/vm_object.h>
65 #include <vm/vm_extern.h>
66
67 #include <fs/coda/coda.h>
68 #include <fs/coda/cnode.h>
69 #include <fs/coda/coda_vnops.h>
70 #include <fs/coda/coda_venus.h>
71 #include <fs/coda/coda_opstats.h>
72 #include <fs/coda/coda_subr.h>
73 #include <fs/coda/coda_pioctl.h>
74
75 /*
76  * These flags select various performance enhancements.
77  */
78 static int coda_attr_cache = 1;         /* Set to cache attributes. */
79 static int coda_symlink_cache = 1;      /* Set to cache symbolic links. */
80 static int coda_access_cache = 1;       /* Set to cache some access checks. */
81
82 /*
83  * Structure to keep track of vfs calls.
84  */
85 static struct coda_op_stats coda_vnodeopstats[CODA_VNODEOPS_SIZE];
86
87 #define MARK_ENTRY(op)          (coda_vnodeopstats[op].entries++)
88 #define MARK_INT_SAT(op)        (coda_vnodeopstats[op].sat_intrn++)
89 #define MARK_INT_FAIL(op)       (coda_vnodeopstats[op].unsat_intrn++)
90 #define MARK_INT_GEN(op)        (coda_vnodeopstats[op].gen_intrn++)
91
92 /*
93  * What we are delaying for in printf.
94  */
95 int coda_printf_delay = 0;      /* In microseconds */
96 int coda_vnop_print_entry = 0;
97 static int coda_lockdebug = 0;
98
99 /*
100  * Some FreeBSD details:
101  *
102  * codadev_modevent is called at boot time or module load time.
103  */
104 #define ENTRY do {                                                      \
105         if (coda_vnop_print_entry)                                      \
106                 myprintf(("Entered %s\n", __func__));                   \
107 } while (0)
108
109 /*
110  * Definition of the vnode operation vector.
111  */
112 struct vop_vector coda_vnodeops = {
113         .vop_default = &default_vnodeops,
114         .vop_cachedlookup = coda_lookup,        /* uncached lookup */
115         .vop_lookup = vfs_cache_lookup,         /* namecache lookup */
116         .vop_create = coda_create,              /* create */
117         .vop_open = coda_open,                  /* open */
118         .vop_close = coda_close,                /* close */
119         .vop_access = coda_access,              /* access */
120         .vop_getattr = coda_getattr,            /* getattr */
121         .vop_setattr = coda_setattr,            /* setattr */
122         .vop_read = coda_read,                  /* read */
123         .vop_write = coda_write,                /* write */
124         .vop_ioctl = coda_ioctl,                /* ioctl */
125         .vop_fsync = coda_fsync,                /* fsync */
126         .vop_remove = coda_remove,              /* remove */
127         .vop_link = coda_link,                  /* link */
128         .vop_rename = coda_rename,              /* rename */
129         .vop_mkdir = coda_mkdir,                /* mkdir */
130         .vop_rmdir = coda_rmdir,                /* rmdir */
131         .vop_symlink = coda_symlink,            /* symlink */
132         .vop_readdir = coda_readdir,            /* readdir */
133         .vop_readlink = coda_readlink,          /* readlink */
134         .vop_inactive = coda_inactive,          /* inactive */
135         .vop_reclaim = coda_reclaim,            /* reclaim */
136         .vop_lock1 = coda_lock,                 /* lock */
137         .vop_unlock = coda_unlock,              /* unlock */
138         .vop_bmap = VOP_EOPNOTSUPP,             /* bmap */
139         .vop_print = VOP_NULL,                  /* print */
140         .vop_islocked = coda_islocked,          /* islocked */
141         .vop_pathconf = coda_pathconf,          /* pathconf */
142         .vop_poll = vop_stdpoll,
143         .vop_getpages = vop_stdgetpages,        /* pager intf.*/
144         .vop_putpages = vop_stdputpages,        /* pager intf.*/
145         .vop_getwritemount = vop_stdgetwritemount,
146 #if 0
147         /* missing */
148         .vop_cachedlookup = ufs_lookup,
149         .vop_whiteout = ufs_whiteout,
150 #endif
151
152 };
153
154 static void     coda_print_vattr(struct vattr *attr);
155
156 int
157 coda_vnodeopstats_init(void)
158 {
159         int i;
160
161         for(i=0; i<CODA_VNODEOPS_SIZE; i++) {
162                 coda_vnodeopstats[i].opcode = i;
163                 coda_vnodeopstats[i].entries = 0;
164                 coda_vnodeopstats[i].sat_intrn = 0;
165                 coda_vnodeopstats[i].unsat_intrn = 0;
166                 coda_vnodeopstats[i].gen_intrn = 0;
167         }
168         return (0);
169 }
170
171 /*
172  * coda_open calls Venus which returns an open file descriptor the cache file
173  * holding the data.  We get the vnode while we are still in the context of
174  * the venus process in coda_psdev.c.  This vnode is then passed back to the
175  * caller and opened.
176  */
177 int
178 coda_open(struct vop_open_args *ap)
179 {
180
181         /*
182          * FreeBSD can pass the O_EXCL flag in mode, even though the check
183          * has already happened.  Venus defensively assumes that if open is
184          * passed the EXCL, it must be a bug.  We strip the flag here.
185          */
186         /* true args */
187         struct vnode **vpp = &(ap->a_vp);
188         struct cnode *cp = VTOC(*vpp);
189         int flag = ap->a_mode & (~O_EXCL);
190         struct ucred *cred = ap->a_cred;
191         struct thread *td = ap->a_td;
192         /* locals */
193         int error;
194         struct vnode *vp;
195
196         MARK_ENTRY(CODA_OPEN_STATS);
197
198         /*
199          * Check for open of control file.
200          */
201         if (IS_CTL_VP(*vpp)) {
202                 /* XXX */
203                 /* if (WRITEABLE(flag)) */
204                 if (flag & (FWRITE | O_TRUNC | O_CREAT | O_EXCL)) {
205                         MARK_INT_FAIL(CODA_OPEN_STATS);
206                         return (EACCES);
207                 }
208                 MARK_INT_SAT(CODA_OPEN_STATS);
209                 return (0);
210         }
211         error = venus_open(vtomi((*vpp)), &cp->c_fid, flag, cred,
212             td->td_proc, &vp);
213         if (error)
214                 return (error);
215         CODADEBUG(CODA_OPEN, myprintf(("open: vp %p result %d\n", vp,
216             error)););
217
218         /*
219          * Save the vnode pointer for the cache file.
220          */
221         if (cp->c_ovp == NULL) {
222                 cp->c_ovp = vp;
223         } else {
224                 if (cp->c_ovp != vp)
225                         panic("coda_open: cp->c_ovp != ITOV(ip)");
226         }
227         cp->c_ocount++;
228
229         /*
230          * Flush the attribute cached if writing the file.
231          */
232         if (flag & FWRITE) {
233                 cp->c_owrite++;
234                 cp->c_flags &= ~C_VATTR;
235         }
236
237         /*
238          * Open the cache file.
239          */
240         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
241         error = VOP_OPEN(vp, flag, cred, td, NULL);
242         if (error) {
243                 VOP_UNLOCK(vp, 0);
244                 printf("coda_open: VOP_OPEN on container failed %d\n", error);
245                 return (error);
246         }
247         (*vpp)->v_object = vp->v_object;
248         VOP_UNLOCK(vp, 0);
249         return (0);
250 }
251
252 /*
253  * Close the cache file used for I/O and notify Venus.
254  */
255 int
256 coda_close(struct vop_close_args *ap)
257 {
258         /* true args */
259         struct vnode *vp = ap->a_vp;
260         struct cnode *cp = VTOC(vp);
261         int flag = ap->a_fflag;
262         struct ucred *cred = ap->a_cred;
263         struct thread *td = ap->a_td;
264         /* locals */
265         int error;
266
267         MARK_ENTRY(CODA_CLOSE_STATS);
268
269         /*
270          * Check for close of control file.
271          */
272         if (IS_CTL_VP(vp)) {
273                 MARK_INT_SAT(CODA_CLOSE_STATS);
274                 return (0);
275         }
276         if (cp->c_ovp) {
277                 vn_lock(cp->c_ovp, LK_EXCLUSIVE | LK_RETRY);
278                 /* Do errors matter here? */
279                 VOP_CLOSE(cp->c_ovp, flag, cred, td);
280                 vput(cp->c_ovp);
281         }
282 #ifdef CODA_VERBOSE
283         else
284                 printf("coda_close: NO container vp %p/cp %p\n", vp, cp);
285 #endif
286         if (--cp->c_ocount == 0)
287                 cp->c_ovp = NULL;
288
289         /*
290          * File was opened for write.
291          */
292         if (flag & FWRITE)
293                 --cp->c_owrite;
294         if (!IS_UNMOUNTING(cp))
295                 error = venus_close(vtomi(vp), &cp->c_fid, flag, cred,
296                     td->td_proc);
297         else
298                 error = ENODEV;
299         CODADEBUG(CODA_CLOSE, myprintf(("close: result %d\n",error)););
300         return (error);
301 }
302
303 int
304 coda_read(struct vop_read_args *ap)
305 {
306
307         ENTRY;
308         return (coda_rdwr(ap->a_vp, ap->a_uio, UIO_READ, ap->a_ioflag,
309             ap->a_cred, ap->a_uio->uio_td));
310 }
311
312 int
313 coda_write(struct vop_write_args *ap)
314 {
315
316         ENTRY;
317         return (coda_rdwr(ap->a_vp, ap->a_uio, UIO_WRITE, ap->a_ioflag,
318             ap->a_cred, ap->a_uio->uio_td));
319 }
320
321 int
322 coda_rdwr(struct vnode *vp, struct uio *uiop, enum uio_rw rw, int ioflag,
323     struct ucred *cred, struct thread *td)
324 {
325         /* upcall decl */
326         /* NOTE: container file operation!!! */
327         /* locals */
328         struct cnode *cp = VTOC(vp);
329         struct vnode *cfvp = cp->c_ovp;
330         int opened_internally = 0;
331         int error = 0;
332
333         MARK_ENTRY(CODA_RDWR_STATS);
334         CODADEBUG(CODA_RDWR, myprintf(("coda_rdwr(%d, %p, %zd, %lld, %d)\n",
335             rw, (void *)uiop->uio_iov->iov_base, uiop->uio_resid,
336             (long long)uiop->uio_offset, uiop->uio_segflg)););
337
338         /*
339          * Check for rdwr of control object.
340          */
341         if (IS_CTL_VP(vp)) {
342                 MARK_INT_FAIL(CODA_RDWR_STATS);
343                 return (EINVAL);
344         }
345
346         /*
347          * If file is not already open this must be a page {read,write}
348          * request and we should open it internally.
349          */
350         if (cfvp == NULL) {
351                 opened_internally = 1;
352                 MARK_INT_GEN(CODA_OPEN_STATS);
353                 error = VOP_OPEN(vp, (rw == UIO_READ ? FREAD : FWRITE), cred,
354                     td, NULL);
355 #ifdef CODA_VERBOSE
356                 printf("coda_rdwr: Internally Opening %p\n", vp);
357 #endif
358                 if (error) {
359                         printf("coda_rdwr: VOP_OPEN on container failed "
360                             "%d\n", error);
361                         return (error);
362                 }
363                 cfvp = cp->c_ovp;
364         }
365
366         /*
367          * Have UFS handle the call.
368          */
369         CODADEBUG(CODA_RDWR, myprintf(("indirect rdwr: fid = %s, refcnt = "
370             "%d\n", coda_f2s(&cp->c_fid), CTOV(cp)->v_usecount)););
371         vn_lock(cfvp, LK_EXCLUSIVE | LK_RETRY);
372         if (rw == UIO_READ) {
373                 error = VOP_READ(cfvp, uiop, ioflag, cred);
374         } else {
375                 error = VOP_WRITE(cfvp, uiop, ioflag, cred);
376                 /*
377                  * ufs_write updates the vnode_pager_setsize for the
378                  * vnode/object.
379                  *
380                  * XXX: Since we now share vm objects between layers, this is
381                  * probably unnecessary.
382                  */
383                 {
384                         struct vattr attr;
385                         if (VOP_GETATTR(cfvp, &attr, cred) == 0)
386                                 vnode_pager_setsize(vp, attr.va_size);
387                 }
388         }
389         VOP_UNLOCK(cfvp, 0);
390         if (error)
391                 MARK_INT_FAIL(CODA_RDWR_STATS);
392         else
393                 MARK_INT_SAT(CODA_RDWR_STATS);
394
395         /*
396          * Do an internal close if necessary.
397          */
398         if (opened_internally) {
399                 MARK_INT_GEN(CODA_CLOSE_STATS);
400                 (void)VOP_CLOSE(vp, (rw == UIO_READ ? FREAD : FWRITE), cred,
401                     td);
402         }
403
404         /*
405          * Invalidate cached attributes if writing.
406          */
407         if (rw == UIO_WRITE)
408                 cp->c_flags &= ~C_VATTR;
409         return (error);
410 }
411
412 int
413 coda_ioctl(struct vop_ioctl_args *ap)
414 {
415         /* true args */
416         struct vnode *vp = ap->a_vp;
417         int com = ap->a_command;
418         caddr_t data = ap->a_data;
419         int flag = ap->a_fflag;
420         struct ucred *cred = ap->a_cred;
421         struct thread *td = ap->a_td;
422         /* locals */
423         int error;
424         struct vnode *tvp;
425         struct nameidata ndp;
426         struct PioctlData *iap = (struct PioctlData *)data;
427
428         MARK_ENTRY(CODA_IOCTL_STATS);
429         CODADEBUG(CODA_IOCTL, myprintf(("in coda_ioctl on %s\n", iap->path)););
430
431         /*
432          * Don't check for operation on a dying object, for ctlvp it
433          * shouldn't matter.
434          *
435          * Must be control object to succeed.
436          */
437         if (!IS_CTL_VP(vp)) {
438                 MARK_INT_FAIL(CODA_IOCTL_STATS);
439                 CODADEBUG(CODA_IOCTL, myprintf(("coda_ioctl error: vp != "
440                     "ctlvp")););
441                 return (EOPNOTSUPP);
442         }
443
444         /*
445          * Look up the pathname.
446          *
447          * Should we use the name cache here? It would get it from lookupname
448          * sooner or later anyway, right?
449          */
450         NDINIT(&ndp, LOOKUP, (iap->follow ? FOLLOW : NOFOLLOW),
451             UIO_USERSPACE, iap->path, td);
452         error = namei(&ndp);
453         tvp = ndp.ni_vp;
454         if (error) {
455                 MARK_INT_FAIL(CODA_IOCTL_STATS);
456                 CODADEBUG(CODA_IOCTL, myprintf(("coda_ioctl error: lookup "
457                     "returns %d\n", error)););
458                 return (error);
459         }
460
461         /*
462          * Make sure this is a coda style cnode, but it may be a different
463          * vfsp.
464          */
465         if (tvp->v_op != &coda_vnodeops) {
466                 vrele(tvp);
467                 NDFREE(&ndp, NDF_ONLY_PNBUF);
468                 MARK_INT_FAIL(CODA_IOCTL_STATS);
469                 CODADEBUG(CODA_IOCTL,
470                 myprintf(("coda_ioctl error: %s not a coda object\n",
471                     iap->path)););
472                 return (EINVAL);
473         }
474         if (iap->vi.in_size > VC_MAXDATASIZE) {
475                 NDFREE(&ndp, 0);
476                 return (EINVAL);
477         }
478         error = venus_ioctl(vtomi(tvp), &((VTOC(tvp))->c_fid), com, flag,
479             data, cred, td->td_proc);
480         if (error)
481                 MARK_INT_FAIL(CODA_IOCTL_STATS);
482         else
483                 CODADEBUG(CODA_IOCTL, myprintf(("Ioctl returns %d \n",
484                     error)););
485         vrele(tvp);
486         NDFREE(&ndp, NDF_ONLY_PNBUF);
487         return (error);
488 }
489
490 /*
491  * To reduce the cost of a user-level venus;we cache attributes in the
492  * kernel.  Each cnode has storage allocated for an attribute.  If c_vattr is
493  * valid, return a reference to it.  Otherwise, get the attributes from venus
494  * and store them in the cnode.  There is some question if this method is a
495  * security leak.  But I think that in order to make this call, the user must
496  * have done a lookup and opened the file, and therefore should already have
497  * access.
498  */
499 int
500 coda_getattr(struct vop_getattr_args *ap)
501 {
502         /* true args */
503         struct vnode *vp = ap->a_vp;
504         struct cnode *cp = VTOC(vp);
505         struct vattr *vap = ap->a_vap;
506         struct ucred *cred = ap->a_cred;
507         /* locals */
508         struct vnode *convp;
509         int error, size;
510
511         MARK_ENTRY(CODA_GETATTR_STATS);
512         if (IS_UNMOUNTING(cp))
513                 return (ENODEV);
514
515         /*
516          * Check for getattr of control object.
517          */
518         if (IS_CTL_VP(vp)) {
519                 MARK_INT_FAIL(CODA_GETATTR_STATS);
520                 return (ENOENT);
521         }
522
523         /*
524          * Check to see if the attributes have already been cached.
525          */
526         if (VALID_VATTR(cp)) {
527                 CODADEBUG(CODA_GETATTR, myprintf(("attr cache hit: %s\n",
528                     coda_f2s(&cp->c_fid))););
529                 CODADEBUG(CODA_GETATTR, if (!(codadebug & ~CODA_GETATTR))
530                     coda_print_vattr(&cp->c_vattr););
531                 *vap = cp->c_vattr;
532                 MARK_INT_SAT(CODA_GETATTR_STATS);
533                 return (0);
534         }
535         error = venus_getattr(vtomi(vp), &cp->c_fid, cred, vap);
536         if (!error) {
537                 CODADEBUG(CODA_GETATTR, myprintf(("getattr miss %s: result "
538                     "%d\n", coda_f2s(&cp->c_fid), error)););
539                 CODADEBUG(CODA_GETATTR, if (!(codadebug & ~CODA_GETATTR))
540                     coda_print_vattr(vap););
541
542                 /*
543                  * XXX: Since we now share vm objects between layers, this is
544                  * probably unnecessary.
545                  */
546                 size = vap->va_size;
547                 convp = cp->c_ovp;
548                 if (convp != NULL)
549                         vnode_pager_setsize(convp, size);
550
551                 /*
552                  * If not open for write, store attributes in cnode.
553                  */
554                 if ((cp->c_owrite == 0) && (coda_attr_cache)) {
555                         cp->c_vattr = *vap;
556                         cp->c_flags |= C_VATTR;
557                 }
558         }
559         return (error);
560 }
561
562 int
563 coda_setattr(struct vop_setattr_args *ap)
564 {
565         /* true args */
566         struct vnode *vp = ap->a_vp;
567         struct cnode *cp = VTOC(vp);
568         struct vattr *vap = ap->a_vap;
569         struct ucred *cred = ap->a_cred;
570         /* locals */
571         struct vnode *convp;
572         int error, size;
573
574         MARK_ENTRY(CODA_SETATTR_STATS);
575
576         /*
577          * Check for setattr of control object.
578          */
579         if (IS_CTL_VP(vp)) {
580                 MARK_INT_FAIL(CODA_SETATTR_STATS);
581                 return (ENOENT);
582         }
583         if (codadebug & CODADBGMSK(CODA_SETATTR))
584                 coda_print_vattr(vap);
585         error = venus_setattr(vtomi(vp), &cp->c_fid, vap, cred);
586         if (!error)
587                 cp->c_flags &= ~(C_VATTR | C_ACCCACHE);
588
589         /*
590          * XXX: Since we now share vm objects between layers, this is
591          * probably unnecessary.
592          *
593          * XXX: Shouldn't we only be doing this "set" if C_VATTR remains
594          * valid after venus_setattr()?
595          */
596         size = vap->va_size;
597         convp = cp->c_ovp;
598         if (size != VNOVAL && convp != NULL)
599                 vnode_pager_setsize(convp, size);
600         CODADEBUG(CODA_SETATTR, myprintf(("setattr %d\n", error)););
601         return (error);
602 }
603
604 int
605 coda_access(struct vop_access_args *ap)
606 {
607         /* true args */
608         struct vnode *vp = ap->a_vp;
609         struct cnode *cp = VTOC(vp);
610         accmode_t accmode = ap->a_accmode;
611         struct ucred *cred = ap->a_cred;
612         struct thread *td = ap->a_td;
613         /* locals */
614         int error;
615
616         MARK_ENTRY(CODA_ACCESS_STATS);
617
618         /*
619          * Check for access of control object.  Only read access is allowed
620          * on it.
621          */
622         if (IS_CTL_VP(vp)) {
623                 /*
624                  * Bogus hack - all will be marked as successes.
625                  */
626                 MARK_INT_SAT(CODA_ACCESS_STATS);
627                 return (((accmode & VREAD) && !(accmode & (VWRITE | VEXEC)))
628                     ? 0 : EACCES);
629         }
630
631         /*
632          * We maintain a one-entry LRU positive access cache with each cnode.
633          * In principle we could also track negative results, and for more
634          * than one uid, but we don't yet.  Venus is responsible for
635          * invalidating this cache as required.
636          */
637         if (coda_access_cache && VALID_ACCCACHE(cp) &&
638             (cred->cr_uid == cp->c_cached_uid) &&
639             (accmode & cp->c_cached_mode) == accmode) {
640                 MARK_INT_SAT(CODA_ACCESS_STATS);
641                 return (0);
642         }
643         error = venus_access(vtomi(vp), &cp->c_fid, accmode, cred, td->td_proc);
644         if (error == 0 && coda_access_cache) {
645                 /*-
646                  * When we have a new successful request, we consider three
647                  * cases:
648                  *
649                  * - No initialized access cache, in which case cache the
650                  *   result.
651                  * - Cached result for a different user, in which case we
652                  *   replace the entry.
653                  * - Cached result for the same user, in which case we add
654                  *   any newly granted rights to the cached mode.
655                  *
656                  * XXXRW: If we ever move to something more interesting than
657                  * uid-based token lookup, we'll need to change this.
658                  */
659                 cp->c_flags |= C_ACCCACHE;
660                 if (cp->c_cached_uid != cred->cr_uid) {
661                         cp->c_cached_mode = accmode;
662                         cp->c_cached_uid = cred->cr_uid;
663                 } else
664                         cp->c_cached_mode |= accmode;
665         }
666         return (error);
667 }
668
669 int
670 coda_readlink(struct vop_readlink_args *ap)
671 {
672         /* true args */
673         struct vnode *vp = ap->a_vp;
674         struct cnode *cp = VTOC(vp);
675         struct uio *uiop = ap->a_uio;
676         struct ucred *cred = ap->a_cred;
677         struct thread *td = ap->a_uio->uio_td;
678         /* locals */
679         int error;
680         char *str;
681         int len;
682
683         MARK_ENTRY(CODA_READLINK_STATS);
684
685         /*
686          * Check for readlink of control object.
687          */
688         if (IS_CTL_VP(vp)) {
689                 MARK_INT_FAIL(CODA_READLINK_STATS);
690                 return (ENOENT);
691         }
692         if ((coda_symlink_cache) && (VALID_SYMLINK(cp))) {
693                 /*
694                  * Symlink was cached.
695                  */
696                 uiop->uio_rw = UIO_READ;
697                 error = uiomove(cp->c_symlink, (int)cp->c_symlen, uiop);
698                 if (error)
699                         MARK_INT_FAIL(CODA_READLINK_STATS);
700                 else
701                         MARK_INT_SAT(CODA_READLINK_STATS);
702                 return (error);
703         }
704         error = venus_readlink(vtomi(vp), &cp->c_fid, cred, td != NULL ?
705             td->td_proc : NULL, &str, &len);
706         if (!error) {
707                 uiop->uio_rw = UIO_READ;
708                 error = uiomove(str, len, uiop);
709                 if (coda_symlink_cache) {
710                         cp->c_symlink = str;
711                         cp->c_symlen = len;
712                         cp->c_flags |= C_SYMLINK;
713                 } else
714                         CODA_FREE(str, len);
715         }
716         CODADEBUG(CODA_READLINK, myprintf(("in readlink result %d\n",
717             error)););
718         return (error);
719 }
720
721 int
722 coda_fsync(struct vop_fsync_args *ap)
723 {
724         /* true args */
725         struct vnode *vp = ap->a_vp;
726         struct cnode *cp = VTOC(vp);
727         struct thread *td = ap->a_td;
728         /* locals */
729         struct vnode *convp = cp->c_ovp;
730         int error;
731
732         MARK_ENTRY(CODA_FSYNC_STATS);
733
734         /*
735          * Check for fsync on an unmounting object.
736          *
737          * XXX: Is this comment true on FreeBSD?  It seems likely, since
738          * unmounting is fairly non-atomic.
739          *
740          * The NetBSD kernel, in it's infinite wisdom, can try to fsync after
741          * an unmount has been initiated.  This is a Bad Thing, which we have
742          * to avoid.  Not a legitimate failure for stats.
743          */
744         if (IS_UNMOUNTING(cp))
745                 return (ENODEV);
746
747         /*
748          * Check for fsync of control object.
749          */
750         if (IS_CTL_VP(vp)) {
751                 MARK_INT_SAT(CODA_FSYNC_STATS);
752                 return (0);
753         }
754         if (convp != NULL) {
755                 vn_lock(convp, LK_EXCLUSIVE | LK_RETRY);
756                 VOP_FSYNC(convp, MNT_WAIT, td);
757                 VOP_UNLOCK(convp, 0);
758         }
759
760         /*
761          * We see fsyncs with usecount == 1 then usecount == 0.  For now we
762          * ignore them.
763          */
764 #if 0
765         VI_LOCK(vp);
766         if (!vp->v_usecount) {
767                 printf("coda_fsync on vnode %p with %d usecount.  "
768                     "c_flags = %x (%x)\n", vp, vp->v_usecount, cp->c_flags,
769                     cp->c_flags&C_PURGING);
770         }
771         VI_UNLOCK(vp);
772 #endif
773
774         /*
775          * We can expect fsync on any vnode at all if venus is purging it.
776          * Venus can't very well answer the fsync request, now can it?
777          * Hopefully, it won't have to, because hopefully, venus preserves
778          * the (possibly untrue) invariant that it never purges an open
779          * vnode.  Hopefully.
780          */
781         if (cp->c_flags & C_PURGING)
782                 return (0);
783
784         /* XXX: needs research */
785         return (0);
786         error = venus_fsync(vtomi(vp), &cp->c_fid, td->td_proc);
787         CODADEBUG(CODA_FSYNC, myprintf(("in fsync result %d\n", error)););
788         return (error);
789 }
790
791 int
792 coda_inactive(struct vop_inactive_args *ap)
793 {
794         /*
795          * XXX - at the moment, inactive doesn't look at cred, and doesn't
796          * have a proc pointer.  Oops.
797          */
798         /* true args */
799         struct vnode *vp = ap->a_vp;
800         struct cnode *cp = VTOC(vp);
801         struct ucred *cred __attribute__((unused)) = NULL;
802         struct thread *td __attribute__((unused)) = curthread;
803         /* upcall decl */
804         /* locals */
805
806         /*
807          * We don't need to send inactive to venus - DCS.
808          */
809         MARK_ENTRY(CODA_INACTIVE_STATS);
810         CODADEBUG(CODA_INACTIVE, myprintf(("in inactive, %s, vfsp %p\n",
811             coda_f2s(&cp->c_fid), vp->v_mount)););
812         vp->v_object = NULL;
813
814         /*
815          * If an array has been allocated to hold the symlink, deallocate it.
816          */
817         if ((coda_symlink_cache) && (VALID_SYMLINK(cp))) {
818                 if (cp->c_symlink == NULL)
819                         panic("coda_inactive: null symlink pointer in cnode");
820                 CODA_FREE(cp->c_symlink, cp->c_symlen);
821                 cp->c_flags &= ~C_SYMLINK;
822                 cp->c_symlen = 0;
823         }
824
825         /*
826          * Remove it from the table so it can't be found.
827          */
828         coda_unsave(cp);
829         if ((struct coda_mntinfo *)(vp->v_mount->mnt_data) == NULL) {
830                 myprintf(("Help! vfsp->vfs_data was NULL, but vnode %p "
831                     "wasn't dying\n", vp));
832                 panic("badness in coda_inactive\n");
833         }
834         if (IS_UNMOUNTING(cp)) {
835 #ifdef  DEBUG
836                 printf("coda_inactive: IS_UNMOUNTING use %d: vp %p, cp %p\n",
837                     vrefcnt(vp), vp, cp);
838                 if (cp->c_ovp != NULL)
839                         printf("coda_inactive: cp->ovp != NULL use %d: vp "
840                             "%p, cp %p\n", vrefcnt(vp), vp, cp);
841 #endif
842         } else
843                 vgone(vp);
844         MARK_INT_SAT(CODA_INACTIVE_STATS);
845         return (0);
846 }
847
848 /*
849  * Remote filesystem operations having to do with directory manipulation.
850  */
851
852 /*
853  * In FreeBSD, lookup returns the vnode locked.
854  */
855 int
856 coda_lookup(struct vop_cachedlookup_args *ap)
857 {
858         /* true args */
859         struct vnode *dvp = ap->a_dvp;
860         struct cnode *dcp = VTOC(dvp);
861         struct vnode **vpp = ap->a_vpp;
862         /*
863          * It looks as though ap->a_cnp->ni_cnd->cn_nameptr holds the rest of
864          * the string to xlate, and that we must try to get at least
865          * ap->a_cnp->ni_cnd->cn_namelen of those characters to macth.  I
866          * could be wrong.
867          */
868         struct componentname  *cnp = ap->a_cnp;
869         struct ucred *cred = cnp->cn_cred;
870         struct thread *td = cnp->cn_thread;
871         /* locals */
872         struct cnode *cp;
873         const char *nm = cnp->cn_nameptr;
874         int len = cnp->cn_namelen;
875         struct CodaFid VFid;
876         int vtype;
877         int error = 0;
878
879         MARK_ENTRY(CODA_LOOKUP_STATS);
880         CODADEBUG(CODA_LOOKUP, myprintf(("lookup: %s in %s\n", nm,
881             coda_f2s(&dcp->c_fid))););
882
883         /*
884          * Check for lookup of control object.
885          */
886         if (IS_CTL_NAME(dvp, nm, len)) {
887                 *vpp = coda_ctlvp;
888                 vref(*vpp);
889                 MARK_INT_SAT(CODA_LOOKUP_STATS);
890                 goto exit;
891         }
892         if (len+1 > CODA_MAXNAMLEN) {
893                 MARK_INT_FAIL(CODA_LOOKUP_STATS);
894                 CODADEBUG(CODA_LOOKUP, myprintf(("name too long: lookup, "
895                     "%s (%s)\n", coda_f2s(&dcp->c_fid), nm)););
896                 *vpp = NULL;
897                 error = EINVAL;
898                 goto exit;
899         }
900
901         error = venus_lookup(vtomi(dvp), &dcp->c_fid, nm, len, cred,
902             td->td_proc, &VFid, &vtype);
903         if (error) {
904                 MARK_INT_FAIL(CODA_LOOKUP_STATS);
905                 CODADEBUG(CODA_LOOKUP, myprintf(("lookup error on %s "
906                     "(%s)%d\n", coda_f2s(&dcp->c_fid), nm, error)););
907                 *vpp = NULL;
908         } else {
909                 MARK_INT_SAT(CODA_LOOKUP_STATS);
910                 CODADEBUG(CODA_LOOKUP, myprintf(("lookup: %s type %o "
911                     "result %d\n", coda_f2s(&VFid), vtype, error)););
912                 cp = make_coda_node(&VFid, dvp->v_mount, vtype);
913                 *vpp = CTOV(cp);
914
915                 /*
916                  * Enter the new vnode in the namecache only if the top bit
917                  * isn't set.
918                  *
919                  * And don't enter a new vnode for an invalid one!
920                  */
921                 if (!(vtype & CODA_NOCACHE) && (cnp->cn_flags & MAKEENTRY))
922                         cache_enter(dvp, *vpp, cnp);
923         }
924 exit:
925         /*
926          * If we are creating, and this was the last name to be looked up,
927          * and the error was ENOENT, then there really shouldn't be an error
928          * and we can make the leaf NULL and return success.  Since this is
929          * supposed to work under Mach as well as FreeBSD, we're leaving this
930          * fn wrapped.  We also must tell lookup/namei that we need to save
931          * the last component of the name.  (Create will have to free the
932          * name buffer later...lucky us...).
933          */
934         if (((cnp->cn_nameiop == CREATE) || (cnp->cn_nameiop == RENAME))
935             && (cnp->cn_flags & ISLASTCN) && (error == ENOENT)) {
936                 error = EJUSTRETURN;
937                 cnp->cn_flags |= SAVENAME;
938                 *ap->a_vpp = NULL;
939         }
940
941         /*
942          * If we are removing, and we are at the last element, and we found
943          * it, then we need to keep the name around so that the removal will
944          * go ahead as planned.  Unfortunately, this will probably also lock
945          * the to-be-removed vnode, which may or may not be a good idea.
946          * I'll have to look at the bits of coda_remove to make sure.  We'll
947          * only save the name if we did in fact find the name, otherwise
948          * coda_remove won't have a chance to free the pathname.
949          */
950         if ((cnp->cn_nameiop == DELETE) && (cnp->cn_flags & ISLASTCN)
951             && !error)
952                 cnp->cn_flags |= SAVENAME;
953
954         /*
955          * If the lookup went well, we need to (potentially?) unlock the
956          * parent, and lock the child.  We are only responsible for checking
957          * to see if the parent is supposed to be unlocked before we return.
958          * We must always lock the child (provided there is one, and (the
959          * parent isn't locked or it isn't the same as the parent.)  Simple,
960          * huh?  We can never leave the parent locked unless we are ISLASTCN.
961          */
962         if (!error || (error == EJUSTRETURN)) {
963                 if (cnp->cn_flags & ISDOTDOT) {
964                         VOP_UNLOCK(dvp, 0);
965                         /*
966                          * The parent is unlocked.  As long as there is a
967                          * child, lock it without bothering to check anything
968                          * else.
969                          */
970                         if (*ap->a_vpp)
971                                 vn_lock(*ap->a_vpp, LK_EXCLUSIVE | LK_RETRY);
972                         vn_lock(dvp, LK_RETRY|LK_EXCLUSIVE);
973                 } else {
974                         /*
975                          * The parent is locked, and may be the same as the
976                          * child.  If different, go ahead and lock it.
977                          */
978                         if (*ap->a_vpp && (*ap->a_vpp != dvp))
979                                 vn_lock(*ap->a_vpp, LK_EXCLUSIVE | LK_RETRY);
980                 }
981         } else {
982                 /*
983                  * If the lookup failed, we need to ensure that the leaf is
984                  * NULL.
985                  *
986                  * Don't change any locking?
987                  */
988                 *ap->a_vpp = NULL;
989         }
990         return (error);
991 }
992
993 /*ARGSUSED*/
994 int
995 coda_create(struct vop_create_args *ap)
996 {
997         /* true args */
998         struct vnode *dvp = ap->a_dvp;
999         struct cnode *dcp = VTOC(dvp);
1000         struct vattr *va = ap->a_vap;
1001         int exclusive = 1;
1002         int mode = ap->a_vap->va_mode;
1003         struct vnode **vpp = ap->a_vpp;
1004         struct componentname  *cnp = ap->a_cnp;
1005         struct ucred *cred = cnp->cn_cred;
1006         struct thread *td = cnp->cn_thread;
1007         /* locals */
1008         int error;
1009         struct cnode *cp;
1010         const char *nm = cnp->cn_nameptr;
1011         int len = cnp->cn_namelen;
1012         struct CodaFid VFid;
1013         struct vattr attr;
1014
1015         MARK_ENTRY(CODA_CREATE_STATS);
1016
1017         /*
1018          * All creates are exclusive XXX.
1019          *
1020          * I'm assuming the 'mode' argument is the file mode bits XXX.
1021          *
1022          * Check for create of control object.
1023          */
1024         if (IS_CTL_NAME(dvp, nm, len)) {
1025                 *vpp = (struct vnode *)0;
1026                 MARK_INT_FAIL(CODA_CREATE_STATS);
1027                 return (EACCES);
1028         }
1029         error = venus_create(vtomi(dvp), &dcp->c_fid, nm, len, exclusive,
1030             mode, va, cred, td->td_proc, &VFid, &attr);
1031         if (!error) {
1032                 /*
1033                  * If this is an exclusive create, panic if the file already
1034                  * exists.
1035                  *
1036                  * Venus should have detected the file and reported EEXIST.
1037                  */
1038                 if ((exclusive == 1) && (coda_find(&VFid) != NULL))
1039                         panic("cnode existed for newly created file!");
1040                 cp = make_coda_node(&VFid, dvp->v_mount, attr.va_type);
1041                 *vpp = CTOV(cp);
1042
1043                 /*
1044                  * Update va to reflect the new attributes.
1045                  */
1046                 (*va) = attr;
1047
1048                 /*
1049                  * Update the attribute cache and mark it as valid.
1050                  */
1051                 if (coda_attr_cache) {
1052                         VTOC(*vpp)->c_vattr = attr;
1053                         VTOC(*vpp)->c_flags |= C_VATTR;
1054                 }
1055
1056                 /*
1057                  * Invalidate the parent's attr cache, the modification time
1058                  * has changed.
1059                  */
1060                 VTOC(dvp)->c_flags &= ~C_VATTR;
1061                 cache_enter(dvp, *vpp, cnp);
1062                 CODADEBUG(CODA_CREATE, myprintf(("create: %s, result %d\n",
1063                     coda_f2s(&VFid), error)););
1064         } else {
1065                 *vpp = (struct vnode *)0;
1066                 CODADEBUG(CODA_CREATE, myprintf(("create error %d\n",
1067                     error)););
1068         }
1069         if (!error) {
1070                 if (cnp->cn_flags & MAKEENTRY)
1071                         cache_enter(dvp, *vpp, cnp);
1072                 if (cnp->cn_flags & LOCKLEAF)
1073                         vn_lock(*ap->a_vpp, LK_EXCLUSIVE | LK_RETRY);
1074         } else if (error == ENOENT) {
1075                 /*
1076                  * XXXRW: We only enter a negative entry if ENOENT is
1077                  * returned, not other errors.  But will Venus invalidate dvp
1078                  * properly in all cases when new files appear via the
1079                  * network rather than a local operation?
1080                  */
1081                 if (cnp->cn_flags & MAKEENTRY)
1082                         cache_enter(dvp, NULL, cnp);
1083         }
1084         return (error);
1085 }
1086
1087 int
1088 coda_remove(struct vop_remove_args *ap)
1089 {
1090         /* true args */
1091         struct vnode *vp = ap->a_vp;
1092         struct vnode *dvp = ap->a_dvp;
1093         struct cnode *cp = VTOC(dvp);
1094         struct componentname  *cnp = ap->a_cnp;
1095         struct ucred *cred = cnp->cn_cred;
1096         struct thread *td = cnp->cn_thread;
1097         /* locals */
1098         int error;
1099         const char *nm = cnp->cn_nameptr;
1100         int len = cnp->cn_namelen;
1101 #if 0
1102         struct cnode *tp;
1103 #endif
1104
1105         MARK_ENTRY(CODA_REMOVE_STATS);
1106         CODADEBUG(CODA_REMOVE, myprintf(("remove: %s in %s\n", nm,
1107             coda_f2s(&cp->c_fid))););
1108
1109         /*
1110          * Check for remove of control object.
1111          */
1112         if (IS_CTL_NAME(dvp, nm, len)) {
1113                 MARK_INT_FAIL(CODA_REMOVE_STATS);
1114                 return (ENOENT);
1115         }
1116
1117         /*
1118          * Invalidate the parent's attr cache, the modification time has
1119          * changed.  We don't yet know if the last reference to the file is
1120          * being removed, but we do know the reference count on the child has
1121          * changed, so invalidate its attr cache also.
1122          */
1123         VTOC(dvp)->c_flags &= ~C_VATTR;
1124         VTOC(vp)->c_flags &= ~(C_VATTR | C_ACCCACHE);
1125         error = venus_remove(vtomi(dvp), &cp->c_fid, nm, len, cred,
1126             td->td_proc);
1127         cache_purge(vp);
1128         CODADEBUG(CODA_REMOVE, myprintf(("in remove result %d\n",error)););
1129         return (error);
1130 }
1131
1132 int
1133 coda_link(struct vop_link_args *ap)
1134 {
1135         /* true args */
1136         struct vnode *vp = ap->a_vp;
1137         struct cnode *cp = VTOC(vp);
1138         struct vnode *tdvp = ap->a_tdvp;
1139         struct cnode *tdcp = VTOC(tdvp);
1140         struct componentname *cnp = ap->a_cnp;
1141         struct ucred *cred = cnp->cn_cred;
1142         struct thread *td = cnp->cn_thread;
1143         /* locals */
1144         int error;
1145         const char *nm = cnp->cn_nameptr;
1146         int len = cnp->cn_namelen;
1147
1148         MARK_ENTRY(CODA_LINK_STATS);
1149
1150         if (codadebug & CODADBGMSK(CODA_LINK)) {
1151                 myprintf(("nb_link:   vp fid: %s\n", coda_f2s(&cp->c_fid)));
1152                 myprintf(("nb_link: tdvp fid: %s)\n",
1153                     coda_f2s(&tdcp->c_fid)));
1154         }
1155         if (codadebug & CODADBGMSK(CODA_LINK)) {
1156                 myprintf(("link:   vp fid: %s\n", coda_f2s(&cp->c_fid)));
1157                 myprintf(("link: tdvp fid: %s\n", coda_f2s(&tdcp->c_fid)));
1158         }
1159
1160         /*
1161          * Check for link to/from control object.
1162          */
1163         if (IS_CTL_NAME(tdvp, nm, len) || IS_CTL_VP(vp)) {
1164                 MARK_INT_FAIL(CODA_LINK_STATS);
1165                 return (EACCES);
1166         }
1167         error = venus_link(vtomi(vp), &cp->c_fid, &tdcp->c_fid, nm, len,
1168             cred, td->td_proc);
1169
1170         /*
1171          * Invalidate the parent's attr cache, the modification time has
1172          * changed.
1173          */
1174         VTOC(tdvp)->c_flags &= ~C_VATTR;
1175         VTOC(vp)->c_flags &= ~C_VATTR;
1176         CODADEBUG(CODA_LINK, myprintf(("in link result %d\n",error)););
1177         return (error);
1178 }
1179
1180 int
1181 coda_rename(struct vop_rename_args *ap)
1182 {
1183         /* true args */
1184         struct vnode *fvp = ap->a_fvp;
1185         struct vnode *tvp = ap->a_tvp;
1186         struct vnode *odvp = ap->a_fdvp;
1187         struct cnode *odcp = VTOC(odvp);
1188         struct componentname  *fcnp = ap->a_fcnp;
1189         struct vnode *ndvp = ap->a_tdvp;
1190         struct cnode *ndcp = VTOC(ndvp);
1191         struct componentname  *tcnp = ap->a_tcnp;
1192         struct ucred *cred = fcnp->cn_cred;
1193         struct thread *td = fcnp->cn_thread;
1194         /* true args */
1195         int error;
1196         const char *fnm = fcnp->cn_nameptr;
1197         int flen = fcnp->cn_namelen;
1198         const char *tnm = tcnp->cn_nameptr;
1199         int tlen = tcnp->cn_namelen;
1200
1201         MARK_ENTRY(CODA_RENAME_STATS);
1202
1203         /*
1204          * Check for rename involving control object.
1205          */
1206         if (IS_CTL_NAME(odvp, fnm, flen) || IS_CTL_NAME(ndvp, tnm, tlen)) {
1207                 MARK_INT_FAIL(CODA_RENAME_STATS);
1208                 return (EACCES);
1209         }
1210
1211         /*
1212          * Remove the entries for both source and target directories, which
1213          * should catch references to the children.  Perhaps we could purge
1214          * less?
1215          */
1216         cache_purge(odvp);
1217         cache_purge(ndvp);
1218
1219         /*
1220          * Invalidate parent directories as modification times have changed.
1221          * Invalidate access cache on renamed file as rights may have
1222          * changed.
1223          */
1224         VTOC(odvp)->c_flags &= ~C_VATTR;
1225         VTOC(ndvp)->c_flags &= ~C_VATTR;
1226         VTOC(fvp)->c_flags &= ~C_ACCCACHE;
1227         if (flen+1 > CODA_MAXNAMLEN) {
1228                 MARK_INT_FAIL(CODA_RENAME_STATS);
1229                 error = EINVAL;
1230                 goto exit;
1231         }
1232         if (tlen+1 > CODA_MAXNAMLEN) {
1233                 MARK_INT_FAIL(CODA_RENAME_STATS);
1234                 error = EINVAL;
1235                 goto exit;
1236         }
1237         error = venus_rename(vtomi(odvp), &odcp->c_fid, &ndcp->c_fid, fnm,
1238             flen, tnm, tlen, cred, td->td_proc);
1239 exit:
1240         CODADEBUG(CODA_RENAME, myprintf(("in rename result %d\n",error)););
1241
1242         /*
1243          * Update namecache to reflect that the names of various objects may
1244          * have changed (or gone away entirely).
1245          */
1246         cache_purge(fvp);
1247         cache_purge(tvp);
1248
1249         /*
1250          * Release parents first, then children.
1251          */
1252         vrele(odvp);
1253         if (tvp) {
1254                 if (tvp == ndvp)
1255                         vrele(ndvp);
1256                 else
1257                         vput(ndvp);
1258                 vput(tvp);
1259         } else
1260                 vput(ndvp);
1261         vrele(fvp);
1262         return (error);
1263 }
1264
1265 int
1266 coda_mkdir(struct vop_mkdir_args *ap)
1267 {
1268         /* true args */
1269         struct vnode *dvp = ap->a_dvp;
1270         struct cnode *dcp = VTOC(dvp);
1271         struct componentname  *cnp = ap->a_cnp;
1272         struct vattr *va = ap->a_vap;
1273         struct vnode **vpp = ap->a_vpp;
1274         struct ucred *cred = cnp->cn_cred;
1275         struct thread *td = cnp->cn_thread;
1276         /* locals */
1277         int error;
1278         const char *nm = cnp->cn_nameptr;
1279         int len = cnp->cn_namelen;
1280         struct cnode *cp;
1281         struct CodaFid VFid;
1282         struct vattr ova;
1283
1284         MARK_ENTRY(CODA_MKDIR_STATS);
1285
1286         /*
1287          * Check for mkdir of target object.
1288          */
1289         if (IS_CTL_NAME(dvp, nm, len)) {
1290                 *vpp = (struct vnode *)0;
1291                 MARK_INT_FAIL(CODA_MKDIR_STATS);
1292                 return (EACCES);
1293         }
1294         if (len+1 > CODA_MAXNAMLEN) {
1295                 *vpp = (struct vnode *)0;
1296                 MARK_INT_FAIL(CODA_MKDIR_STATS);
1297                 return (EACCES);
1298         }
1299         error = venus_mkdir(vtomi(dvp), &dcp->c_fid, nm, len, va, cred,
1300             td->td_proc, &VFid, &ova);
1301         if (!error) {
1302                 if (coda_find(&VFid) != NULL)
1303                         panic("cnode existed for newly created directory!");
1304                 cp =  make_coda_node(&VFid, dvp->v_mount, va->va_type);
1305                 *vpp = CTOV(cp);
1306
1307                 /*
1308                  * Enter the new vnode in the Name Cache.
1309                  */
1310                 if (cnp->cn_flags & MAKEENTRY)
1311                         cache_enter(dvp, *vpp, cnp);
1312
1313                 /*
1314                  * Update the attr cache and mark as valid.
1315                  */
1316                 if (coda_attr_cache) {
1317                         VTOC(*vpp)->c_vattr = ova;
1318                         VTOC(*vpp)->c_flags |= C_VATTR;
1319                 }
1320
1321                 /*
1322                  * Invalidate the parent's attr cache, the modification time
1323                  * has changed.
1324                  */
1325                 VTOC(dvp)->c_flags &= ~C_VATTR;
1326                 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
1327                 CODADEBUG( CODA_MKDIR, myprintf(("mkdir: %s result %d\n",
1328                     coda_f2s(&VFid), error)););
1329         } else {
1330                 *vpp = NULL;
1331                 CODADEBUG(CODA_MKDIR, myprintf(("mkdir error %d\n",error)););
1332         }
1333         return (error);
1334 }
1335
1336 int
1337 coda_rmdir(struct vop_rmdir_args *ap)
1338 {
1339         /* true args */
1340         struct vnode *vp = ap->a_vp;
1341         struct vnode *dvp = ap->a_dvp;
1342         struct cnode *dcp = VTOC(dvp);
1343         struct componentname  *cnp = ap->a_cnp;
1344         struct ucred *cred = cnp->cn_cred;
1345         struct thread *td = cnp->cn_thread;
1346         /* true args */
1347         int error;
1348         const char *nm = cnp->cn_nameptr;
1349         int len = cnp->cn_namelen;
1350 #if 0
1351         struct cnode *cp;
1352 #endif
1353
1354         MARK_ENTRY(CODA_RMDIR_STATS);
1355
1356         /*
1357          * Check for rmdir of control object.
1358          */
1359         if (IS_CTL_NAME(dvp, nm, len)) {
1360                 MARK_INT_FAIL(CODA_RMDIR_STATS);
1361                 return (ENOENT);
1362         }
1363
1364         /*
1365          * Possibly somewhat conservative purging, perhaps we just need to
1366          * purge vp?
1367          */
1368         cache_purge(dvp);
1369         cache_purge(vp);
1370
1371         /*
1372          * Invalidate the parent's attr cache, the modification time has
1373          * changed.
1374          */
1375         dcp->c_flags &= ~C_VATTR;
1376         error = venus_rmdir(vtomi(dvp), &dcp->c_fid, nm, len, cred,
1377             td->td_proc);
1378         CODADEBUG(CODA_RMDIR, myprintf(("in rmdir result %d\n", error)););
1379         return (error);
1380 }
1381
1382 int
1383 coda_symlink(struct vop_symlink_args *ap)
1384 {
1385         /* true args */
1386         struct vnode *tdvp = ap->a_dvp;
1387         struct cnode *tdcp = VTOC(tdvp);
1388         struct componentname *cnp = ap->a_cnp;
1389         struct vattr *tva = ap->a_vap;
1390         char *path = ap->a_target;
1391         struct ucred *cred = cnp->cn_cred;
1392         struct thread *td = cnp->cn_thread;
1393         struct vnode **vpp = ap->a_vpp;
1394         /* locals */
1395         int error;
1396
1397         /*-
1398          * XXX I'm assuming the following things about coda_symlink's
1399          * arguments:
1400          *       t(foo) is the new name/parent/etc being created.
1401          *       lname is the contents of the new symlink.
1402          */
1403         char *nm = cnp->cn_nameptr;
1404         int len = cnp->cn_namelen;
1405         int plen = strlen(path);
1406
1407         /*
1408          * Here's the strategy for the moment: perform the symlink, then do a
1409          * lookup to grab the resulting vnode.  I know this requires two
1410          * communications with Venus for a new sybolic link, but that's the
1411          * way the ball bounces.  I don't yet want to change the way the Mach
1412          * symlink works.  When Mach support is deprecated, we should change
1413          * symlink so that the common case returns the resultant vnode in a
1414          * vpp argument.
1415          */
1416         MARK_ENTRY(CODA_SYMLINK_STATS);
1417
1418         /*
1419          * Check for symlink of control object.
1420          */
1421         if (IS_CTL_NAME(tdvp, nm, len)) {
1422                 MARK_INT_FAIL(CODA_SYMLINK_STATS);
1423                 return (EACCES);
1424         }
1425         if (plen+1 > CODA_MAXPATHLEN) {
1426                 MARK_INT_FAIL(CODA_SYMLINK_STATS);
1427                 return (EINVAL);
1428         }
1429         if (len+1 > CODA_MAXNAMLEN) {
1430                 MARK_INT_FAIL(CODA_SYMLINK_STATS);
1431                 error = EINVAL;
1432                 goto exit;
1433         }
1434         error = venus_symlink(vtomi(tdvp), &tdcp->c_fid, path, plen, nm, len,
1435             tva, cred, td->td_proc);
1436
1437         /*
1438          * Invalidate the parent's attr cache, the modification time has
1439          * changed.
1440          */
1441         tdcp->c_flags &= ~C_VATTR;
1442         if (error == 0)
1443                 error = VOP_LOOKUP(tdvp, vpp, cnp);
1444 exit:
1445         CODADEBUG(CODA_SYMLINK, myprintf(("in symlink result %d\n",error)););
1446         return (error);
1447 }
1448
1449 /*
1450  * Read directory entries.
1451  *
1452  * XXX: This forwards the operator straight to the cache vnode using
1453  * VOP_READDIR(), rather than calling venus_readdir().  Why?
1454  */
1455 int
1456 coda_readdir(struct vop_readdir_args *ap)
1457 {
1458         /* true args */
1459         struct vnode *vp = ap->a_vp;
1460         struct cnode *cp = VTOC(vp);
1461         struct uio *uiop = ap->a_uio;
1462         struct ucred *cred = ap->a_cred;
1463         int *eofflag = ap->a_eofflag;
1464         u_long **cookies = ap->a_cookies;
1465         int *ncookies = ap->a_ncookies;
1466         struct thread *td = ap->a_uio->uio_td;
1467         /* upcall decl */
1468         /* locals */
1469         int error = 0;
1470         int opened_internally = 0;
1471
1472         MARK_ENTRY(CODA_READDIR_STATS);
1473         CODADEBUG(CODA_READDIR, myprintf(("coda_readdir(%p, %zd, %lld, %d)\n",
1474             (void *)uiop->uio_iov->iov_base, uiop->uio_resid,
1475             (long long)uiop->uio_offset, uiop->uio_segflg)););
1476
1477         /*
1478          * Check for readdir of control object.
1479          */
1480         if (IS_CTL_VP(vp)) {
1481                 MARK_INT_FAIL(CODA_READDIR_STATS);
1482                 return (ENOENT);
1483         }
1484
1485         /*
1486          * If directory is not already open do an "internal open" on it.
1487          *
1488          * XXX: Why would this happen?  For files, there's memory mapping,
1489          * execution, and other kernel access paths such as ktrace.  For
1490          * directories, it is less clear.
1491          */
1492         if (cp->c_ovp == NULL) {
1493                 opened_internally = 1;
1494                 MARK_INT_GEN(CODA_OPEN_STATS);
1495                 error = VOP_OPEN(vp, FREAD, cred, td, NULL);
1496                 printf("coda_readdir: Internally Opening %p\n", vp);
1497                 if (error) {
1498                         printf("coda_readdir: VOP_OPEN on container failed "
1499                            "%d\n", error);
1500                         return (error);
1501                 }
1502         }
1503
1504         /*
1505          * Have UFS handle the call.
1506          */
1507         CODADEBUG(CODA_READDIR, myprintf(("indirect readdir: fid = %s, "
1508             "refcnt = %d\n", coda_f2s(&cp->c_fid), vp->v_usecount)););
1509         vn_lock(cp->c_ovp, LK_SHARED | LK_RETRY);
1510         error = VOP_READDIR(cp->c_ovp, uiop, cred, eofflag, ncookies,
1511             cookies);
1512         VOP_UNLOCK(cp->c_ovp, 0);
1513         if (error)
1514                 MARK_INT_FAIL(CODA_READDIR_STATS);
1515         else
1516                 MARK_INT_SAT(CODA_READDIR_STATS);
1517
1518         /*
1519          * Do an "internal close" if necessary.
1520          */
1521         if (opened_internally) {
1522                 MARK_INT_GEN(CODA_CLOSE_STATS);
1523                 (void)VOP_CLOSE(vp, FREAD, cred, td);
1524         }
1525         return (error);
1526 }
1527
1528 int
1529 coda_reclaim(struct vop_reclaim_args *ap)
1530 {
1531         /* true args */
1532         struct vnode *vp = ap->a_vp;
1533         struct cnode *cp = VTOC(vp);
1534         /* upcall decl */
1535         /* locals */
1536
1537         /*
1538          * Forced unmount/flush will let vnodes with non-zero use be
1539          * destroyed!
1540          */
1541         ENTRY;
1542
1543         if (IS_UNMOUNTING(cp)) {
1544 #ifdef  DEBUG
1545                 if (VTOC(vp)->c_ovp) {
1546                         if (IS_UNMOUNTING(cp))
1547                                 printf("coda_reclaim: c_ovp not void: vp "
1548                                     "%p, cp %p\n", vp, cp);
1549                 }
1550 #endif
1551         } else {
1552                 if (prtactive && vp->v_usecount != 0)
1553                         vprint("coda_reclaim: pushing active", vp);
1554         }
1555         cache_purge(vp);
1556         coda_free(VTOC(vp));
1557         vp->v_data = NULL;
1558         vp->v_object = NULL;
1559         return (0);
1560 }
1561
1562 int
1563 coda_lock(struct vop_lock1_args *ap)
1564 {
1565         /* true args */
1566         struct vnode *vp = ap->a_vp;
1567         struct cnode *cp = VTOC(vp);
1568         /* upcall decl */
1569         /* locals */
1570
1571         ENTRY;
1572         if ((ap->a_flags & LK_INTERLOCK) == 0) {
1573                 VI_LOCK(vp);
1574                 ap->a_flags |= LK_INTERLOCK;
1575         }
1576         if (coda_lockdebug)
1577                 myprintf(("Attempting lock on %s\n", coda_f2s(&cp->c_fid)));
1578         return (vop_stdlock(ap));
1579 }
1580
1581 int
1582 coda_unlock(struct vop_unlock_args *ap)
1583 {
1584         /* true args */
1585         struct vnode *vp = ap->a_vp;
1586         struct cnode *cp = VTOC(vp);
1587         /* upcall decl */
1588         /* locals */
1589
1590         ENTRY;
1591         if (coda_lockdebug)
1592                 myprintf(("Attempting unlock on %s\n",
1593                     coda_f2s(&cp->c_fid)));
1594         return (vop_stdunlock(ap));
1595 }
1596
1597 int
1598 coda_islocked(struct vop_islocked_args *ap)
1599 {
1600         /* true args */
1601
1602         ENTRY;
1603         return (vop_stdislocked(ap));
1604 }
1605
1606 static void
1607 coda_print_vattr(struct vattr *attr)
1608 {
1609         char *typestr;
1610
1611         switch (attr->va_type) {
1612         case VNON:
1613                 typestr = "VNON";
1614                 break;
1615
1616         case VREG:
1617                 typestr = "VREG";
1618                 break;
1619
1620         case VDIR:
1621                 typestr = "VDIR";
1622                 break;
1623
1624         case VBLK:
1625                 typestr = "VBLK";
1626                 break;
1627
1628         case VCHR:
1629                 typestr = "VCHR";
1630                 break;
1631
1632         case VLNK:
1633                 typestr = "VLNK";
1634                 break;
1635
1636         case VSOCK:
1637                 typestr = "VSCK";
1638                 break;
1639
1640         case VFIFO:
1641                 typestr = "VFFO";
1642                 break;
1643
1644         case VBAD:
1645                 typestr = "VBAD";
1646                 break;
1647
1648         default:
1649                 typestr = "????";
1650                 break;
1651         }
1652         myprintf(("attr: type %s mode %d uid %d gid %d fsid %d rdev %d\n",
1653             typestr, (int)attr->va_mode, (int)attr->va_uid,
1654             (int)attr->va_gid, (int)attr->va_fsid, (int)attr->va_rdev));
1655         myprintf(("      fileid %d nlink %d size %d blocksize %d bytes %d\n",
1656             (int)attr->va_fileid, (int)attr->va_nlink, (int)attr->va_size,
1657             (int)attr->va_blocksize,(int)attr->va_bytes));
1658         myprintf(("      gen %ld flags %ld vaflags %d\n", attr->va_gen,
1659             attr->va_flags, attr->va_vaflags));
1660         myprintf(("      atime sec %d nsec %d\n", (int)attr->va_atime.tv_sec,
1661             (int)attr->va_atime.tv_nsec));
1662         myprintf(("      mtime sec %d nsec %d\n", (int)attr->va_mtime.tv_sec,
1663             (int)attr->va_mtime.tv_nsec));
1664         myprintf(("      ctime sec %d nsec %d\n", (int)attr->va_ctime.tv_sec,
1665             (int)attr->va_ctime.tv_nsec));
1666 }
1667
1668 /*
1669  * How to print a ucred.
1670  */
1671 void
1672 coda_print_cred(struct ucred *cred)
1673 {
1674         int i;
1675
1676         myprintf(("ref %d\tuid %d\n",cred->cr_ref,cred->cr_uid));
1677         for (i=0; i < cred->cr_ngroups; i++)
1678                 myprintf(("\tgroup %d: (%d)\n",i,cred->cr_groups[i]));
1679         myprintf(("\n"));
1680 }
1681
1682 /*
1683  * Return a vnode for the given fid.  If no cnode exists for this fid create
1684  * one and put it in a table hashed by coda_f2i().  If the cnode for this fid
1685  * is already in the table return it (ref count is incremented by coda_find.
1686  * The cnode will be flushed from the table when coda_inactive calls
1687  * coda_unsave.
1688  */
1689 struct cnode *
1690 make_coda_node(struct CodaFid *fid, struct mount *vfsp, short type)
1691 {
1692         struct cnode *cp;
1693         struct vnode *vp;
1694         int err;
1695
1696         /*
1697          * XXXRW: This really needs a moderate amount of reworking.  We need
1698          * to properly tolerate failures of getnewvnode() and insmntque(),
1699          * and callers need to be able to accept an error back from
1700          * make_coda_node.  There may also be more general issues in how we
1701          * handle forced unmount.  Finally, if/when Coda loses its dependency
1702          * on Giant, the ordering of this needs rethinking.
1703          */
1704         cp = coda_find(fid);
1705         if (cp != NULL) {
1706                 vref(CTOV(cp));
1707                 return (cp);
1708         }
1709         cp = coda_alloc();
1710         cp->c_fid = *fid;
1711         err = getnewvnode("coda", vfsp, &coda_vnodeops, &vp);
1712         if (err)
1713                 panic("coda: getnewvnode returned error %d\n", err);
1714         vp->v_data = cp;
1715         vp->v_type = type;
1716         cp->c_vnode = vp;
1717         coda_save(cp);
1718         err = insmntque(vp, vfsp);
1719         if (err != 0)
1720                 printf("coda: insmntque failed: error %d", err);
1721         return (cp);
1722 }
1723
1724 int
1725 coda_pathconf(struct vop_pathconf_args *ap)
1726 {
1727
1728         switch (ap->a_name) {
1729         case _PC_NAME_MAX:
1730                 *ap->a_retval = CODA_MAXNAMLEN;
1731                 return (0);
1732
1733         case _PC_PATH_MAX:
1734                 *ap->a_retval = CODA_MAXPATHLEN;
1735                 return (0);
1736
1737         default:
1738                 return (vop_stdpathconf(ap));
1739         }
1740 }