proc->thread stage 3.5: Add an IO_CORE flag so coda doesn't have to dig
[dragonfly.git] / sys / vfs / coda / coda_vnops.c
1 /*
2  * 
3  *             Coda: an Experimental Distributed File System
4  *                              Release 3.1
5  * 
6  *           Copyright (c) 1987-1998 Carnegie Mellon University
7  *                          All Rights Reserved
8  * 
9  * Permission  to  use, copy, modify and distribute this software and its
10  * documentation is hereby granted,  provided  that  both  the  copyright
11  * notice  and  this  permission  notice  appear  in  all  copies  of the
12  * software, derivative works or  modified  versions,  and  any  portions
13  * thereof, and that both notices appear in supporting documentation, and
14  * that credit is given to Carnegie Mellon University  in  all  documents
15  * and publicity pertaining to direct or indirect use of this code or its
16  * derivatives.
17  * 
18  * CODA IS AN EXPERIMENTAL SOFTWARE SYSTEM AND IS  KNOWN  TO  HAVE  BUGS,
19  * SOME  OF  WHICH MAY HAVE SERIOUS CONSEQUENCES.  CARNEGIE MELLON ALLOWS
20  * FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.   CARNEGIE  MELLON
21  * DISCLAIMS  ANY  LIABILITY  OF  ANY  KIND  FOR  ANY  DAMAGES WHATSOEVER
22  * RESULTING DIRECTLY OR INDIRECTLY FROM THE USE OF THIS SOFTWARE  OR  OF
23  * ANY DERIVATIVE WORK.
24  * 
25  * Carnegie  Mellon  encourages  users  of  this  software  to return any
26  * improvements or extensions that  they  make,  and  to  grant  Carnegie
27  * Mellon the rights to redistribute these changes without encumbrance.
28  * 
29  *      @(#) src/sys/coda/coda_vnops.c,v 1.1.1.1 1998/08/29 21:14:52 rvb Exp $
30  * $FreeBSD: src/sys/coda/coda_vnops.c,v 1.22.2.1 2001/06/29 16:26:22 shafeeq Exp $
31  * $DragonFly: src/sys/vfs/coda/Attic/coda_vnops.c,v 1.3 2003/06/24 02:11:53 dillon Exp $
32  * 
33  */
34
35 /* 
36  * Mach Operating System
37  * Copyright (c) 1990 Carnegie-Mellon University
38  * Copyright (c) 1989 Carnegie-Mellon University
39  * All rights reserved.  The CMU software License Agreement specifies
40  * the terms and conditions for use and redistribution.
41  */
42
43 /*
44  * This code was written for the Coda file system at Carnegie Mellon
45  * University.  Contributers include David Steere, James Kistler, and
46  * M. Satyanarayanan.  
47  */
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/proc.h>
53 #include <sys/malloc.h>
54 #include <sys/mount.h>
55 #include <sys/errno.h>
56 #include <sys/acct.h>
57 #include <sys/file.h>
58 #include <sys/fcntl.h>
59 #include <sys/uio.h>
60 #include <sys/namei.h>
61 #include <sys/ioccom.h>
62 #include <sys/select.h>
63
64 #include <vm/vm.h>
65 #include <vm/vm_object.h>
66 #include <vm/vm_extern.h>
67 #include <vm/vm_zone.h>
68
69 #include <coda/coda.h>
70 #include <coda/cnode.h>
71 #include <coda/coda_vnops.h>
72 #include <coda/coda_venus.h>
73 #include <coda/coda_opstats.h>
74 #include <coda/coda_subr.h>
75 #include <coda/coda_namecache.h>
76 #include <coda/coda_pioctl.h>
77
78 /* 
79  * These flags select various performance enhancements.
80  */
81 int coda_attr_cache  = 1;       /* Set to cache attributes in the kernel */
82 int coda_symlink_cache = 1;     /* Set to cache symbolic link information */
83 int coda_access_cache = 1;      /* Set to handle some access checks directly */
84
85 /* structure to keep track of vfs calls */
86
87 struct coda_op_stats coda_vnodeopstats[CODA_VNODEOPS_SIZE];
88
89 #define MARK_ENTRY(op) (coda_vnodeopstats[op].entries++)
90 #define MARK_INT_SAT(op) (coda_vnodeopstats[op].sat_intrn++)
91 #define MARK_INT_FAIL(op) (coda_vnodeopstats[op].unsat_intrn++)
92 #define MARK_INT_GEN(op) (coda_vnodeopstats[op].gen_intrn++)
93
94 /* What we are delaying for in printf */
95 int coda_printf_delay = 0;  /* in microseconds */
96 int coda_vnop_print_entry = 0;
97 static int coda_lockdebug = 0;
98
99 /* Definition of the vfs operation vector */
100
101 /*
102  * Some NetBSD details:
103  * 
104  *   coda_start is called at the end of the mount syscall.
105  *   coda_init is called at boot time.
106  */
107
108 #define ENTRY  if(coda_vnop_print_entry) myprintf(("Entered %s\n",__FUNCTION__))
109
110 /* Definition of the vnode operation vector */
111
112 struct vnodeopv_entry_desc coda_vnodeop_entries[] = {
113     { &vop_default_desc, coda_vop_error },
114     { &vop_lookup_desc, coda_lookup },          /* lookup */
115     { &vop_create_desc, coda_create },          /* create */
116     { &vop_mknod_desc, coda_vop_error },        /* mknod */
117     { &vop_open_desc, coda_open },              /* open */
118     { &vop_close_desc, coda_close },            /* close */
119     { &vop_access_desc, coda_access },          /* access */
120     { &vop_getattr_desc, coda_getattr },        /* getattr */
121     { &vop_setattr_desc, coda_setattr },        /* setattr */
122     { &vop_read_desc, coda_read },              /* read */
123     { &vop_write_desc, coda_write },            /* write */
124     { &vop_ioctl_desc, coda_ioctl },            /* ioctl */
125     { &vop_mmap_desc, coda_vop_error },         /* mmap */
126     { &vop_fsync_desc, coda_fsync },            /* fsync */
127     { &vop_remove_desc, coda_remove },          /* remove */
128     { &vop_link_desc, coda_link },              /* link */
129     { &vop_rename_desc, coda_rename },          /* rename */
130     { &vop_mkdir_desc, coda_mkdir },            /* mkdir */
131     { &vop_rmdir_desc, coda_rmdir },            /* rmdir */
132     { &vop_symlink_desc, coda_symlink },        /* symlink */
133     { &vop_readdir_desc, coda_readdir },        /* readdir */
134     { &vop_readlink_desc, coda_readlink },      /* readlink */
135     { &vop_inactive_desc, coda_inactive },      /* inactive */
136     { &vop_reclaim_desc, coda_reclaim },        /* reclaim */
137     { &vop_lock_desc, coda_lock },              /* lock */
138     { &vop_unlock_desc, coda_unlock },          /* unlock */
139     { &vop_bmap_desc, coda_bmap },              /* bmap */
140     { &vop_strategy_desc, coda_strategy },      /* strategy */
141     { &vop_print_desc, coda_vop_error },        /* print */
142     { &vop_islocked_desc, coda_islocked },      /* islocked */
143     { &vop_pathconf_desc, coda_vop_error },     /* pathconf */
144     { &vop_advlock_desc, coda_vop_nop },        /* advlock */
145     { &vop_bwrite_desc, coda_vop_error },       /* bwrite */
146     { &vop_lease_desc, coda_vop_nop },          /* lease */
147     { &vop_poll_desc, (vop_t *) vop_stdpoll },
148     { &vop_getpages_desc, coda_fbsd_getpages }, /* pager intf.*/
149     { &vop_putpages_desc, coda_fbsd_putpages }, /* pager intf.*/
150     { &vop_createvobject_desc,      (vop_t *) vop_stdcreatevobject },
151     { &vop_destroyvobject_desc,     (vop_t *) vop_stddestroyvobject },
152     { &vop_getvobject_desc,         (vop_t *) vop_stdgetvobject },
153
154 #if     0
155
156     we need to define these someday
157 #define UFS_BLKATOFF(aa, bb, cc, dd) VFSTOUFS((aa)->v_mount)->um_blkatoff(aa, bb, cc, dd)
158 #define UFS_VALLOC(aa, bb, cc, dd) VFSTOUFS((aa)->v_mount)->um_valloc(aa, bb, cc, dd)
159 #define UFS_VFREE(aa, bb, cc) VFSTOUFS((aa)->v_mount)->um_vfree(aa, bb, cc)
160 #define UFS_TRUNCATE(aa, bb, cc, dd, ee) VFSTOUFS((aa)->v_mount)->um_truncate(aa, bb, cc, dd, ee)
161 #define UFS_UPDATE(aa, bb) VFSTOUFS((aa)->v_mount)->um_update(aa, bb)
162
163     missing
164     { &vop_reallocblks_desc,    (vop_t *) ufs_missingop },
165     { &vop_cachedlookup_desc,   (vop_t *) ufs_lookup },
166     { &vop_whiteout_desc,       (vop_t *) ufs_whiteout },
167 #endif
168     { (struct vnodeop_desc*)NULL, (int(*)(void *))NULL }
169 };
170
171 static struct vnodeopv_desc coda_vnodeop_opv_desc =
172                 { &coda_vnodeop_p, coda_vnodeop_entries };
173
174 VNODEOP_SET(coda_vnodeop_opv_desc);
175
176 /* A generic panic: we were called with something we didn't define yet */
177 int
178 coda_vop_error(void *anon) {
179     struct vnodeop_desc **desc = (struct vnodeop_desc **)anon;
180
181     myprintf(("coda_vop_error: Vnode operation %s called, but not defined.\n",
182               (*desc)->vdesc_name));
183     /*
184     panic("coda_vop_error");
185     */
186     return EIO;
187 }
188
189 /* A generic do-nothing.  For lease_check, advlock */
190 int
191 coda_vop_nop(void *anon) {
192     struct vnodeop_desc **desc = (struct vnodeop_desc **)anon;
193
194     if (codadebug) {
195         myprintf(("Vnode operation %s called, but unsupported\n",
196                   (*desc)->vdesc_name));
197     } 
198    return (0);
199 }
200
201 int
202 coda_vnodeopstats_init(void)
203 {
204         register int i;
205         
206         for(i=0;i<CODA_VNODEOPS_SIZE;i++) {
207                 coda_vnodeopstats[i].opcode = i;
208                 coda_vnodeopstats[i].entries = 0;
209                 coda_vnodeopstats[i].sat_intrn = 0;
210                 coda_vnodeopstats[i].unsat_intrn = 0;
211                 coda_vnodeopstats[i].gen_intrn = 0;
212         }
213         return 0;
214 }
215                 
216 /* 
217  * coda_open calls Venus to return the device, inode pair of the cache
218  * file holding the data. Using iget, coda_open finds the vnode of the
219  * cache file, and then opens it.
220  */
221 int
222 coda_open(v)
223     void *v;
224 {
225     /* 
226      * NetBSD can pass the O_EXCL flag in mode, even though the check
227      * has already happened.  Venus defensively assumes that if open
228      * is passed the EXCL, it must be a bug.  We strip the flag here.
229      */
230 /* true args */
231     struct vop_open_args *ap = v;
232     register struct vnode **vpp = &(ap->a_vp);
233     struct cnode *cp = VTOC(*vpp);
234     int flag = ap->a_mode & (~O_EXCL);
235     struct ucred *cred = ap->a_cred;
236     struct proc *p = ap->a_p;
237 /* locals */
238     int error;
239     struct vnode *vp;
240     dev_t dev;
241     ino_t inode;
242
243     MARK_ENTRY(CODA_OPEN_STATS);
244
245     /* Check for open of control file. */
246     if (IS_CTL_VP(*vpp)) {
247         /* XXX */
248         /* if (WRITEABLE(flag)) */ 
249         if (flag & (FWRITE | O_TRUNC | O_CREAT | O_EXCL)) {
250             MARK_INT_FAIL(CODA_OPEN_STATS);
251             return(EACCES);
252         }
253         MARK_INT_SAT(CODA_OPEN_STATS);
254         return(0);
255     }
256
257     error = venus_open(vtomi((*vpp)), &cp->c_fid, flag, cred, p, &dev, &inode);
258     if (error)
259         return (error);
260     if (!error) {
261         CODADEBUG( CODA_OPEN,myprintf(("open: dev %#lx inode %lu result %d\n",
262                                        (u_long)dev2udev(dev), (u_long)inode,
263                                        error)); )
264     }
265
266     /* Translate the <device, inode> pair for the cache file into
267        an inode pointer. */
268     error = coda_grab_vnode(dev, inode, &vp);
269     if (error)
270         return (error);
271
272     /* We get the vnode back locked.  Needs unlocked */
273     VOP_UNLOCK(vp, 0, p);
274     /* Keep a reference until the close comes in. */
275     vref(*vpp);                
276
277     /* Save the vnode pointer for the cache file. */
278     if (cp->c_ovp == NULL) {
279         cp->c_ovp = vp;
280     } else {
281         if (cp->c_ovp != vp)
282             panic("coda_open:  cp->c_ovp != ITOV(ip)");
283     }
284     cp->c_ocount++;
285
286     /* Flush the attribute cached if writing the file. */
287     if (flag & FWRITE) {
288         cp->c_owrite++;
289         cp->c_flags &= ~C_VATTR;
290     }
291
292     /* Save the <device, inode> pair for the cache file to speed
293        up subsequent page_read's. */
294     cp->c_device = dev;
295     cp->c_inode = inode;
296
297     /* Open the cache file. */
298     error = VOP_OPEN(vp, flag, cred, p); 
299     if (error) {
300         printf("coda_open: VOP_OPEN on container failed %d\n", error);
301         return (error);
302     }
303 /* grab (above) does this when it calls newvnode unless it's in the cache*/
304     if (vp->v_type == VREG) {
305         error = vfs_object_create(vp, p, cred);
306         if (error != 0) {
307             printf("coda_open: vfs_object_create() returns %d\n", error);
308             vput(vp);
309         }
310     }
311
312     return(error);
313 }
314
315 /*
316  * Close the cache file used for I/O and notify Venus.
317  */
318 int
319 coda_close(v)
320     void *v;
321 {
322 /* true args */
323     struct vop_close_args *ap = v;
324     struct vnode *vp = ap->a_vp;
325     struct cnode *cp = VTOC(vp);
326     int flag = ap->a_fflag;
327     struct ucred *cred = ap->a_cred;
328     struct proc *p = ap->a_p;
329 /* locals */
330     int error;
331
332     MARK_ENTRY(CODA_CLOSE_STATS);
333
334     /* Check for close of control file. */
335     if (IS_CTL_VP(vp)) {
336         MARK_INT_SAT(CODA_CLOSE_STATS);
337         return(0);
338     }
339
340     if (IS_UNMOUNTING(cp)) {
341         if (cp->c_ovp) {
342 #ifdef  CODA_VERBOSE
343             printf("coda_close: destroying container ref %d, ufs vp %p of vp %p/cp %p\n",
344                     vp->v_usecount, cp->c_ovp, vp, cp);
345 #endif
346 #ifdef  hmm
347             vgone(cp->c_ovp);
348 #else
349             VOP_CLOSE(cp->c_ovp, flag, cred, p); /* Do errors matter here? */
350             vrele(cp->c_ovp);
351 #endif
352         } else {
353 #ifdef  CODA_VERBOSE
354             printf("coda_close: NO container vp %p/cp %p\n", vp, cp);
355 #endif
356         }
357         return ENODEV;
358     } else {
359         VOP_CLOSE(cp->c_ovp, flag, cred, p); /* Do errors matter here? */
360         vrele(cp->c_ovp);
361     }
362
363     if (--cp->c_ocount == 0)
364         cp->c_ovp = NULL;
365
366     if (flag & FWRITE)                    /* file was opened for write */
367         --cp->c_owrite;
368
369     error = venus_close(vtomi(vp), &cp->c_fid, flag, cred, p);
370     vrele(CTOV(cp));
371
372     CODADEBUG(CODA_CLOSE, myprintf(("close: result %d\n",error)); )
373     return(error);
374 }
375
376 int
377 coda_read(v)
378     void *v;
379 {
380     struct vop_read_args *ap = v;
381
382     ENTRY;
383     return(coda_rdwr(ap->a_vp, ap->a_uio, UIO_READ,
384                     ap->a_ioflag, ap->a_cred, ap->a_uio->uio_procp));
385 }
386
387 int
388 coda_write(v)
389     void *v;
390 {
391     struct vop_write_args *ap = v;
392
393     ENTRY;
394     return(coda_rdwr(ap->a_vp, ap->a_uio, UIO_WRITE,
395                     ap->a_ioflag, ap->a_cred, ap->a_uio->uio_procp));
396 }
397
398 int
399 coda_rdwr(vp, uiop, rw, ioflag, cred, p)
400     struct vnode *vp;
401     struct uio *uiop;
402     enum uio_rw rw;
403     int ioflag;
404     struct ucred *cred;
405     struct proc *p;
406
407 /* upcall decl */
408   /* NOTE: container file operation!!! */
409 /* locals */
410     struct cnode *cp = VTOC(vp);
411     struct vnode *cfvp = cp->c_ovp;
412     int igot_internally = 0;
413     int opened_internally = 0;
414     int error = 0;
415
416     MARK_ENTRY(CODA_RDWR_STATS);
417
418     CODADEBUG(CODA_RDWR, myprintf(("coda_rdwr(%d, %p, %d, %lld, %d)\n", rw, 
419                               (void *)uiop->uio_iov->iov_base, uiop->uio_resid, 
420                               (long long)uiop->uio_offset, uiop->uio_segflg)); )
421         
422     /* Check for rdwr of control object. */
423     if (IS_CTL_VP(vp)) {
424         MARK_INT_FAIL(CODA_RDWR_STATS);
425         return(EINVAL);
426     }
427
428     /* 
429      * If file is not already open this must be a page
430      * {read,write} request.  Iget the cache file's inode
431      * pointer if we still have its <device, inode> pair.
432      * Otherwise, we must do an internal open to derive the
433      * pair. 
434      */
435     if (cfvp == NULL) {
436         /* 
437          * If we're dumping core, do the internal open. Otherwise
438          * venus won't have the correct size of the core when
439          * it's completely written.
440          */
441         if (cp->c_inode != 0 && (ioflag & IO_CORE) == 0) { 
442             igot_internally = 1;
443             error = coda_grab_vnode(cp->c_device, cp->c_inode, &cfvp);
444             if (error) {
445                 MARK_INT_FAIL(CODA_RDWR_STATS);
446                 return(error);
447             }
448             /* 
449              * We get the vnode back locked in both Mach and
450              * NetBSD.  Needs unlocked 
451              */
452             VOP_UNLOCK(cfvp, 0, p);
453         }
454         else {
455             opened_internally = 1;
456             MARK_INT_GEN(CODA_OPEN_STATS);
457             error = VOP_OPEN(vp, (rw == UIO_READ ? FREAD : FWRITE), 
458                              cred, p);
459 printf("coda_rdwr: Internally Opening %p\n", vp);
460             if (error) {
461                 printf("coda_rdwr: VOP_OPEN on container failed %d\n", error);
462                 return (error);
463             }
464             if (vp->v_type == VREG) {
465                 error = vfs_object_create(vp, p, cred);
466                 if (error != 0) {
467                     printf("coda_rdwr: vfs_object_create() returns %d\n", error);
468                     vput(vp);
469                 }
470             }
471             if (error) {
472                 MARK_INT_FAIL(CODA_RDWR_STATS);
473                 return(error);
474             }
475             cfvp = cp->c_ovp;
476         }
477     }
478
479     /* Have UFS handle the call. */
480     CODADEBUG(CODA_RDWR, myprintf(("indirect rdwr: fid = (%lx.%lx.%lx), refcnt = %d\n",
481                               cp->c_fid.Volume, cp->c_fid.Vnode, 
482                               cp->c_fid.Unique, CTOV(cp)->v_usecount)); )
483
484
485     if (rw == UIO_READ) {
486         error = VOP_READ(cfvp, uiop, ioflag, cred);
487     } else {
488         error = VOP_WRITE(cfvp, uiop, ioflag, cred);
489         /* ufs_write updates the vnode_pager_setsize for the vnode/object */
490
491         {   struct vattr attr;
492
493             if (VOP_GETATTR(cfvp, &attr, cred, p) == 0) {
494                 vnode_pager_setsize(vp, attr.va_size);
495             }
496         }
497     }
498
499     if (error)
500         MARK_INT_FAIL(CODA_RDWR_STATS);
501     else
502         MARK_INT_SAT(CODA_RDWR_STATS);
503
504     /* Do an internal close if necessary. */
505     if (opened_internally) {
506         MARK_INT_GEN(CODA_CLOSE_STATS);
507         (void)VOP_CLOSE(vp, (rw == UIO_READ ? FREAD : FWRITE), cred, p);
508     }
509
510     /* Invalidate cached attributes if writing. */
511     if (rw == UIO_WRITE)
512         cp->c_flags &= ~C_VATTR;
513     return(error);
514 }
515
516 int
517 coda_ioctl(v)
518     void *v;
519 {
520 /* true args */
521     struct vop_ioctl_args *ap = v;
522     struct vnode *vp = ap->a_vp;
523     int com = ap->a_command;
524     caddr_t data = ap->a_data;
525     int flag = ap->a_fflag;
526     struct ucred *cred = ap->a_cred;
527     struct proc  *p = ap->a_p;
528 /* locals */
529     int error;
530     struct vnode *tvp;
531     struct nameidata ndp;
532     struct PioctlData *iap = (struct PioctlData *)data;
533
534     MARK_ENTRY(CODA_IOCTL_STATS);
535
536     CODADEBUG(CODA_IOCTL, myprintf(("in coda_ioctl on %s\n", iap->path));)
537         
538     /* Don't check for operation on a dying object, for ctlvp it
539        shouldn't matter */
540         
541     /* Must be control object to succeed. */
542     if (!IS_CTL_VP(vp)) {
543         MARK_INT_FAIL(CODA_IOCTL_STATS);
544         CODADEBUG(CODA_IOCTL, myprintf(("coda_ioctl error: vp != ctlvp"));)
545             return (EOPNOTSUPP);
546     }
547     /* Look up the pathname. */
548
549     /* Should we use the name cache here? It would get it from
550        lookupname sooner or later anyway, right? */
551
552     NDINIT(&ndp, LOOKUP, (iap->follow ? FOLLOW : NOFOLLOW), UIO_USERSPACE, iap->path, p);
553     error = namei(&ndp);
554     tvp = ndp.ni_vp;
555
556     if (error) {
557         MARK_INT_FAIL(CODA_IOCTL_STATS);
558         CODADEBUG(CODA_IOCTL, myprintf(("coda_ioctl error: lookup returns %d\n",
559                                    error));)
560         return(error);
561     }
562
563     /* 
564      * Make sure this is a coda style cnode, but it may be a
565      * different vfsp 
566      */
567     if (tvp->v_op != coda_vnodeop_p) {
568         vrele(tvp);
569         NDFREE(&ndp, NDF_ONLY_PNBUF);
570         MARK_INT_FAIL(CODA_IOCTL_STATS);
571         CODADEBUG(CODA_IOCTL, 
572                  myprintf(("coda_ioctl error: %s not a coda object\n", 
573                         iap->path));)
574         return(EINVAL);
575     }
576
577     if (iap->vi.in_size > VC_MAXDATASIZE) {
578         NDFREE(&ndp, 0);
579         return(EINVAL);
580     }
581     error = venus_ioctl(vtomi(tvp), &((VTOC(tvp))->c_fid), com, flag, data, cred, p);
582
583     if (error)
584         MARK_INT_FAIL(CODA_IOCTL_STATS);
585     else
586         CODADEBUG(CODA_IOCTL, myprintf(("Ioctl returns %d \n", error)); )
587
588     vrele(tvp);
589     NDFREE(&ndp, NDF_ONLY_PNBUF);
590     return(error);
591 }
592
593 /*
594  * To reduce the cost of a user-level venus;we cache attributes in
595  * the kernel.  Each cnode has storage allocated for an attribute. If
596  * c_vattr is valid, return a reference to it. Otherwise, get the
597  * attributes from venus and store them in the cnode.  There is some
598  * question if this method is a security leak. But I think that in
599  * order to make this call, the user must have done a lookup and
600  * opened the file, and therefore should already have access.  
601  */
602 int
603 coda_getattr(v)
604     void *v;
605 {
606 /* true args */
607     struct vop_getattr_args *ap = v;
608     struct vnode *vp = ap->a_vp;
609     struct cnode *cp = VTOC(vp);
610     struct vattr *vap = ap->a_vap;
611     struct ucred *cred = ap->a_cred;
612     struct proc *p = ap->a_p;
613 /* locals */
614     int error;
615
616     MARK_ENTRY(CODA_GETATTR_STATS);
617
618     if (IS_UNMOUNTING(cp))
619         return ENODEV;
620
621     /* Check for getattr of control object. */
622     if (IS_CTL_VP(vp)) {
623         MARK_INT_FAIL(CODA_GETATTR_STATS);
624         return(ENOENT);
625     }
626
627     /* Check to see if the attributes have already been cached */
628     if (VALID_VATTR(cp)) { 
629         CODADEBUG(CODA_GETATTR, { myprintf(("attr cache hit: (%lx.%lx.%lx)\n",
630                                        cp->c_fid.Volume,
631                                        cp->c_fid.Vnode,
632                                        cp->c_fid.Unique));});
633         CODADEBUG(CODA_GETATTR, if (!(codadebug & ~CODA_GETATTR))
634                  print_vattr(&cp->c_vattr); );
635         
636         *vap = cp->c_vattr;
637         MARK_INT_SAT(CODA_GETATTR_STATS);
638         return(0);
639     }
640
641     error = venus_getattr(vtomi(vp), &cp->c_fid, cred, p, vap);
642
643     if (!error) {
644         CODADEBUG(CODA_GETATTR, myprintf(("getattr miss (%lx.%lx.%lx): result %d\n",
645                                      cp->c_fid.Volume,
646                                      cp->c_fid.Vnode,
647                                      cp->c_fid.Unique,
648                                      error)); )
649             
650         CODADEBUG(CODA_GETATTR, if (!(codadebug & ~CODA_GETATTR))
651                  print_vattr(vap);      );
652         
653     {   int size = vap->va_size;
654         struct vnode *convp = cp->c_ovp;
655         if (convp != (struct vnode *)0) {
656             vnode_pager_setsize(convp, size);
657         }
658     }
659         /* If not open for write, store attributes in cnode */   
660         if ((cp->c_owrite == 0) && (coda_attr_cache)) {  
661             cp->c_vattr = *vap;
662             cp->c_flags |= C_VATTR; 
663         }
664         
665     }
666     return(error);
667 }
668
669 int
670 coda_setattr(v)
671     void *v;
672 {
673 /* true args */
674     struct vop_setattr_args *ap = v;
675     register struct vnode *vp = ap->a_vp;
676     struct cnode *cp = VTOC(vp);
677     register struct vattr *vap = ap->a_vap;
678     struct ucred *cred = ap->a_cred;
679     struct proc *p = ap->a_p;
680 /* locals */
681     int error;
682
683     MARK_ENTRY(CODA_SETATTR_STATS);
684
685     /* Check for setattr of control object. */
686     if (IS_CTL_VP(vp)) {
687         MARK_INT_FAIL(CODA_SETATTR_STATS);
688         return(ENOENT);
689     }
690
691     if (codadebug & CODADBGMSK(CODA_SETATTR)) {
692         print_vattr(vap);
693     }
694     error = venus_setattr(vtomi(vp), &cp->c_fid, vap, cred, p);
695
696     if (!error)
697         cp->c_flags &= ~C_VATTR;
698
699     {   int size = vap->va_size;
700         struct vnode *convp = cp->c_ovp;
701         if (size != VNOVAL && convp != (struct vnode *)0) {
702             vnode_pager_setsize(convp, size);
703         }
704     }
705     CODADEBUG(CODA_SETATTR,     myprintf(("setattr %d\n", error)); )
706     return(error);
707 }
708
709 int
710 coda_access(v)
711     void *v;
712 {
713 /* true args */
714     struct vop_access_args *ap = v;
715     struct vnode *vp = ap->a_vp;
716     struct cnode *cp = VTOC(vp);
717     int mode = ap->a_mode;
718     struct ucred *cred = ap->a_cred;
719     struct proc *p = ap->a_p;
720 /* locals */
721     int error;
722
723     MARK_ENTRY(CODA_ACCESS_STATS);
724
725     /* Check for access of control object.  Only read access is
726        allowed on it. */
727     if (IS_CTL_VP(vp)) {
728         /* bogus hack - all will be marked as successes */
729         MARK_INT_SAT(CODA_ACCESS_STATS);
730         return(((mode & VREAD) && !(mode & (VWRITE | VEXEC))) 
731                ? 0 : EACCES);
732     }
733
734     /*
735      * if the file is a directory, and we are checking exec (eg lookup) 
736      * access, and the file is in the namecache, then the user must have 
737      * lookup access to it.
738      */
739     if (coda_access_cache) {
740         if ((vp->v_type == VDIR) && (mode & VEXEC)) {
741             if (coda_nc_lookup(cp, ".", 1, cred)) {
742                 MARK_INT_SAT(CODA_ACCESS_STATS);
743                 return(0);                     /* it was in the cache */
744             }
745         }
746     }
747
748     error = venus_access(vtomi(vp), &cp->c_fid, mode, cred, p);
749
750     return(error);
751 }
752
753 int
754 coda_readlink(v)
755     void *v;
756 {
757 /* true args */
758     struct vop_readlink_args *ap = v;
759     struct vnode *vp = ap->a_vp;
760     struct cnode *cp = VTOC(vp);
761     struct uio *uiop = ap->a_uio;
762     struct ucred *cred = ap->a_cred;
763     struct proc *p = ap->a_uio->uio_procp;
764 /* locals */
765     int error;
766     char *str;
767     int len;
768
769     MARK_ENTRY(CODA_READLINK_STATS);
770
771     /* Check for readlink of control object. */
772     if (IS_CTL_VP(vp)) {
773         MARK_INT_FAIL(CODA_READLINK_STATS);
774         return(ENOENT);
775     }
776
777     if ((coda_symlink_cache) && (VALID_SYMLINK(cp))) { /* symlink was cached */
778         uiop->uio_rw = UIO_READ;
779         error = uiomove(cp->c_symlink, (int)cp->c_symlen, uiop);
780         if (error)
781             MARK_INT_FAIL(CODA_READLINK_STATS);
782         else
783             MARK_INT_SAT(CODA_READLINK_STATS);
784         return(error);
785     }
786
787     error = venus_readlink(vtomi(vp), &cp->c_fid, cred, p, &str, &len);
788
789     if (!error) {
790         uiop->uio_rw = UIO_READ;
791         error = uiomove(str, len, uiop);
792
793         if (coda_symlink_cache) {
794             cp->c_symlink = str;
795             cp->c_symlen = len;
796             cp->c_flags |= C_SYMLINK;
797         } else
798             CODA_FREE(str, len);
799     }
800
801     CODADEBUG(CODA_READLINK, myprintf(("in readlink result %d\n",error));)
802     return(error);
803 }
804
805 int
806 coda_fsync(v)
807     void *v;
808 {
809 /* true args */
810     struct vop_fsync_args *ap = v;
811     struct vnode *vp = ap->a_vp;
812     struct cnode *cp = VTOC(vp);
813     struct ucred *cred = ap->a_cred;
814     struct proc *p = ap->a_p;
815 /* locals */
816     struct vnode *convp = cp->c_ovp;
817     int error;
818    
819     MARK_ENTRY(CODA_FSYNC_STATS);
820
821     /* Check for fsync on an unmounting object */
822     /* The NetBSD kernel, in it's infinite wisdom, can try to fsync
823      * after an unmount has been initiated.  This is a Bad Thing,
824      * which we have to avoid.  Not a legitimate failure for stats.
825      */
826     if (IS_UNMOUNTING(cp)) {
827         return(ENODEV);
828     }
829
830     /* Check for fsync of control object. */
831     if (IS_CTL_VP(vp)) {
832         MARK_INT_SAT(CODA_FSYNC_STATS);
833         return(0);
834     }
835
836     if (convp)
837         VOP_FSYNC(convp, cred, MNT_WAIT, p);
838
839     /*
840      * We see fsyncs with usecount == 1 then usecount == 0.
841      * For now we ignore them.
842      */
843     /*
844     if (!vp->v_usecount) {
845         printf("coda_fsync on vnode %p with %d usecount.  c_flags = %x (%x)\n",
846                 vp, vp->v_usecount, cp->c_flags, cp->c_flags&C_PURGING);
847     }
848     */
849
850     /*
851      * We can expect fsync on any vnode at all if venus is pruging it.
852      * Venus can't very well answer the fsync request, now can it?
853      * Hopefully, it won't have to, because hopefully, venus preserves
854      * the (possibly untrue) invariant that it never purges an open
855      * vnode.  Hopefully.
856      */
857     if (cp->c_flags & C_PURGING) {
858         return(0);
859     }
860
861     /* needs research */
862     return 0;
863     error = venus_fsync(vtomi(vp), &cp->c_fid, cred, p);
864
865     CODADEBUG(CODA_FSYNC, myprintf(("in fsync result %d\n",error)); );
866     return(error);
867 }
868
869 int
870 coda_inactive(v)
871     void *v;
872 {
873     /* XXX - at the moment, inactive doesn't look at cred, and doesn't
874        have a proc pointer.  Oops. */
875 /* true args */
876     struct vop_inactive_args *ap = v;
877     struct vnode *vp = ap->a_vp;
878     struct cnode *cp = VTOC(vp);
879     struct ucred *cred __attribute__((unused)) = NULL;
880     struct proc *p __attribute__((unused)) = curproc;
881 /* upcall decl */
882 /* locals */
883
884     /* We don't need to send inactive to venus - DCS */
885     MARK_ENTRY(CODA_INACTIVE_STATS);
886
887     if (IS_CTL_VP(vp)) {
888         MARK_INT_SAT(CODA_INACTIVE_STATS);
889         return 0;
890     }
891
892     CODADEBUG(CODA_INACTIVE, myprintf(("in inactive, %lx.%lx.%lx. vfsp %p\n",
893                                   cp->c_fid.Volume, cp->c_fid.Vnode, 
894                                   cp->c_fid.Unique, vp->v_mount));)
895
896     /* If an array has been allocated to hold the symlink, deallocate it */
897     if ((coda_symlink_cache) && (VALID_SYMLINK(cp))) {
898         if (cp->c_symlink == NULL)
899             panic("coda_inactive: null symlink pointer in cnode");
900         
901         CODA_FREE(cp->c_symlink, cp->c_symlen);
902         cp->c_flags &= ~C_SYMLINK;
903         cp->c_symlen = 0;
904     }
905
906     /* Remove it from the table so it can't be found. */
907     coda_unsave(cp);
908     if ((struct coda_mntinfo *)(vp->v_mount->mnt_data) == NULL) {
909         myprintf(("Help! vfsp->vfs_data was NULL, but vnode %p wasn't dying\n", vp));
910         panic("badness in coda_inactive\n");
911     }
912
913     if (IS_UNMOUNTING(cp)) {
914 #ifdef  DEBUG
915         printf("coda_inactive: IS_UNMOUNTING use %d: vp %p, cp %p\n", vp->v_usecount, vp, cp);
916         if (cp->c_ovp != NULL)
917             printf("coda_inactive: cp->ovp != NULL use %d: vp %p, cp %p\n",
918                    vp->v_usecount, vp, cp);
919 #endif
920         lockmgr(&cp->c_lock, LK_RELEASE, &vp->v_interlock, p);
921     } else {
922 #ifdef OLD_DIAGNOSTIC
923         if (CTOV(cp)->v_usecount) {
924             panic("coda_inactive: nonzero reference count");
925         }
926         if (cp->c_ovp != NULL) {
927             panic("coda_inactive:  cp->ovp != NULL");
928         }
929 #endif
930         VOP_UNLOCK(vp, 0, p);
931         vgone(vp);
932     }
933
934     MARK_INT_SAT(CODA_INACTIVE_STATS);
935     return(0);
936 }
937
938 /*
939  * Remote file system operations having to do with directory manipulation.
940  */
941
942 /* 
943  * It appears that in NetBSD, lookup is supposed to return the vnode locked
944  */
945 int
946 coda_lookup(v)
947     void *v;
948 {
949 /* true args */
950     struct vop_lookup_args *ap = v;
951     struct vnode *dvp = ap->a_dvp;
952     struct cnode *dcp = VTOC(dvp);
953     struct vnode **vpp = ap->a_vpp;
954     /* 
955      * It looks as though ap->a_cnp->ni_cnd->cn_nameptr holds the rest
956      * of the string to xlate, and that we must try to get at least
957      * ap->a_cnp->ni_cnd->cn_namelen of those characters to macth.  I
958      * could be wrong. 
959      */
960     struct componentname  *cnp = ap->a_cnp;
961     struct ucred *cred = cnp->cn_cred;
962     struct proc *p = cnp->cn_proc;
963 /* locals */
964     struct cnode *cp;
965     const char *nm = cnp->cn_nameptr;
966     int len = cnp->cn_namelen;
967     ViceFid VFid;
968     int vtype;
969     int error = 0;
970
971     MARK_ENTRY(CODA_LOOKUP_STATS);
972
973     CODADEBUG(CODA_LOOKUP, myprintf(("lookup: %s in %lx.%lx.%lx\n",
974                                    nm, dcp->c_fid.Volume,
975                                    dcp->c_fid.Vnode, dcp->c_fid.Unique)););
976
977     /* Check for lookup of control object. */
978     if (IS_CTL_NAME(dvp, nm, len)) {
979         *vpp = coda_ctlvp;
980         vref(*vpp);
981         MARK_INT_SAT(CODA_LOOKUP_STATS);
982         goto exit;
983     }
984
985     if (len+1 > CODA_MAXNAMLEN) {
986         MARK_INT_FAIL(CODA_LOOKUP_STATS);
987         CODADEBUG(CODA_LOOKUP, myprintf(("name too long: lookup, %lx.%lx.%lx(%s)\n",
988                                     dcp->c_fid.Volume, dcp->c_fid.Vnode,
989                                     dcp->c_fid.Unique, nm)););
990         *vpp = (struct vnode *)0;
991         error = EINVAL;
992         goto exit;
993     }
994     /* First try to look the file up in the cfs name cache */
995     /* lock the parent vnode? */
996     cp = coda_nc_lookup(dcp, nm, len, cred);
997     if (cp) {
998         *vpp = CTOV(cp);
999         vref(*vpp);
1000         CODADEBUG(CODA_LOOKUP, 
1001                  myprintf(("lookup result %d vpp %p\n",error,*vpp));)
1002     } else {
1003         
1004         /* The name wasn't cached, so we need to contact Venus */
1005         error = venus_lookup(vtomi(dvp), &dcp->c_fid, nm, len, cred, p, &VFid, &vtype);
1006         
1007         if (error) {
1008             MARK_INT_FAIL(CODA_LOOKUP_STATS);
1009             CODADEBUG(CODA_LOOKUP, myprintf(("lookup error on %lx.%lx.%lx(%s)%d\n",
1010                                         dcp->c_fid.Volume, dcp->c_fid.Vnode, dcp->c_fid.Unique, nm, error));)
1011             *vpp = (struct vnode *)0;
1012         } else {
1013             MARK_INT_SAT(CODA_LOOKUP_STATS);
1014             CODADEBUG(CODA_LOOKUP, 
1015                      myprintf(("lookup: vol %lx vno %lx uni %lx type %o result %d\n",
1016                             VFid.Volume, VFid.Vnode, VFid.Unique, vtype,
1017                             error)); )
1018                 
1019             cp = make_coda_node(&VFid, dvp->v_mount, vtype);
1020             *vpp = CTOV(cp);
1021             
1022             /* enter the new vnode in the Name Cache only if the top bit isn't set */
1023             /* And don't enter a new vnode for an invalid one! */
1024             if (!(vtype & CODA_NOCACHE))
1025                 coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
1026         }
1027     }
1028
1029  exit:
1030     /* 
1031      * If we are creating, and this was the last name to be looked up,
1032      * and the error was ENOENT, then there really shouldn't be an
1033      * error and we can make the leaf NULL and return success.  Since
1034      * this is supposed to work under Mach as well as NetBSD, we're
1035      * leaving this fn wrapped.  We also must tell lookup/namei that
1036      * we need to save the last component of the name.  (Create will
1037      * have to free the name buffer later...lucky us...)
1038      */
1039     if (((cnp->cn_nameiop == CREATE) || (cnp->cn_nameiop == RENAME))
1040         && (cnp->cn_flags & ISLASTCN)
1041         && (error == ENOENT))
1042     {
1043         error = EJUSTRETURN;
1044         cnp->cn_flags |= SAVENAME;
1045         *ap->a_vpp = NULL;
1046     }
1047
1048     /* 
1049      * If we are removing, and we are at the last element, and we
1050      * found it, then we need to keep the name around so that the
1051      * removal will go ahead as planned.  Unfortunately, this will
1052      * probably also lock the to-be-removed vnode, which may or may
1053      * not be a good idea.  I'll have to look at the bits of
1054      * coda_remove to make sure.  We'll only save the name if we did in
1055      * fact find the name, otherwise coda_remove won't have a chance
1056      * to free the pathname.  
1057      */
1058     if ((cnp->cn_nameiop == DELETE)
1059         && (cnp->cn_flags & ISLASTCN)
1060         && !error)
1061     {
1062         cnp->cn_flags |= SAVENAME;
1063     }
1064
1065     /* 
1066      * If the lookup went well, we need to (potentially?) unlock the
1067      * parent, and lock the child.  We are only responsible for
1068      * checking to see if the parent is supposed to be unlocked before
1069      * we return.  We must always lock the child (provided there is
1070      * one, and (the parent isn't locked or it isn't the same as the
1071      * parent.)  Simple, huh?  We can never leave the parent locked unless
1072      * we are ISLASTCN
1073      */
1074     if (!error || (error == EJUSTRETURN)) {
1075         if (!(cnp->cn_flags & LOCKPARENT) || !(cnp->cn_flags & ISLASTCN)) {
1076             if ((error = VOP_UNLOCK(dvp, 0, p))) {
1077                 return error; 
1078             }       
1079             /* 
1080              * The parent is unlocked.  As long as there is a child,
1081              * lock it without bothering to check anything else. 
1082              */
1083             if (*ap->a_vpp) {
1084                 if ((error = VOP_LOCK(*ap->a_vpp, LK_EXCLUSIVE, p))) {
1085                     printf("coda_lookup: ");
1086                     panic("unlocked parent but couldn't lock child");
1087                 }
1088             }
1089         } else {
1090             /* The parent is locked, and may be the same as the child */
1091             if (*ap->a_vpp && (*ap->a_vpp != dvp)) {
1092                 /* Different, go ahead and lock it. */
1093                 if ((error = VOP_LOCK(*ap->a_vpp, LK_EXCLUSIVE, p))) {
1094                     printf("coda_lookup: ");
1095                     panic("unlocked parent but couldn't lock child");
1096                 }
1097             }
1098         }
1099     } else {
1100         /* If the lookup failed, we need to ensure that the leaf is NULL */
1101         /* Don't change any locking? */
1102         *ap->a_vpp = NULL;
1103     }
1104     return(error);
1105 }
1106
1107 /*ARGSUSED*/
1108 int
1109 coda_create(v)
1110     void *v;
1111 {
1112 /* true args */
1113     struct vop_create_args *ap = v;
1114     struct vnode *dvp = ap->a_dvp;
1115     struct cnode *dcp = VTOC(dvp);
1116     struct vattr *va = ap->a_vap;
1117     int exclusive = 1;
1118     int mode = ap->a_vap->va_mode;
1119     struct vnode **vpp = ap->a_vpp;
1120     struct componentname  *cnp = ap->a_cnp;
1121     struct ucred *cred = cnp->cn_cred;
1122     struct proc *p = cnp->cn_proc;
1123 /* locals */
1124     int error;
1125     struct cnode *cp;
1126     const char *nm = cnp->cn_nameptr;
1127     int len = cnp->cn_namelen;
1128     ViceFid VFid;
1129     struct vattr attr;
1130
1131     MARK_ENTRY(CODA_CREATE_STATS);
1132
1133     /* All creates are exclusive XXX */
1134     /* I'm assuming the 'mode' argument is the file mode bits XXX */
1135
1136     /* Check for create of control object. */
1137     if (IS_CTL_NAME(dvp, nm, len)) {
1138         *vpp = (struct vnode *)0;
1139         MARK_INT_FAIL(CODA_CREATE_STATS);
1140         return(EACCES);
1141     }
1142
1143     error = venus_create(vtomi(dvp), &dcp->c_fid, nm, len, exclusive, mode, va, cred, p, &VFid, &attr);
1144
1145     if (!error) {
1146         
1147         /* If this is an exclusive create, panic if the file already exists. */
1148         /* Venus should have detected the file and reported EEXIST. */
1149
1150         if ((exclusive == 1) &&
1151             (coda_find(&VFid) != NULL))
1152             panic("cnode existed for newly created file!");
1153         
1154         cp = make_coda_node(&VFid, dvp->v_mount, attr.va_type);
1155         *vpp = CTOV(cp);
1156         
1157         /* Update va to reflect the new attributes. */
1158         (*va) = attr;
1159         
1160         /* Update the attribute cache and mark it as valid */
1161         if (coda_attr_cache) {
1162             VTOC(*vpp)->c_vattr = attr;
1163             VTOC(*vpp)->c_flags |= C_VATTR;       
1164         }
1165
1166         /* Invalidate the parent's attr cache, the modification time has changed */
1167         VTOC(dvp)->c_flags &= ~C_VATTR;
1168         
1169         /* enter the new vnode in the Name Cache */
1170         coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
1171         
1172         CODADEBUG(CODA_CREATE, 
1173                  myprintf(("create: (%lx.%lx.%lx), result %d\n",
1174                         VFid.Volume, VFid.Vnode, VFid.Unique, error)); )
1175     } else {
1176         *vpp = (struct vnode *)0;
1177         CODADEBUG(CODA_CREATE, myprintf(("create error %d\n", error));)
1178     }
1179
1180     if (!error) {
1181         if (cnp->cn_flags & LOCKLEAF) {
1182             if ((error = VOP_LOCK(*ap->a_vpp, LK_EXCLUSIVE, p))) {
1183                 printf("coda_create: ");
1184                 panic("unlocked parent but couldn't lock child");
1185             }
1186         }
1187 #ifdef OLD_DIAGNOSTIC
1188         else {
1189             printf("coda_create: LOCKLEAF not set!\n");
1190         }
1191 #endif
1192     }
1193     return(error);
1194 }
1195
1196 int
1197 coda_remove(v)
1198     void *v;
1199 {
1200 /* true args */
1201     struct vop_remove_args *ap = v;
1202     struct vnode *dvp = ap->a_dvp;
1203     struct cnode *cp = VTOC(dvp);
1204     struct componentname  *cnp = ap->a_cnp;
1205     struct ucred *cred = cnp->cn_cred;
1206     struct proc *p = cnp->cn_proc;
1207 /* locals */
1208     int error;
1209     const char *nm = cnp->cn_nameptr;
1210     int len = cnp->cn_namelen;
1211     struct cnode *tp;
1212
1213     MARK_ENTRY(CODA_REMOVE_STATS);
1214
1215     CODADEBUG(CODA_REMOVE, myprintf(("remove: %s in %lx.%lx.%lx\n",
1216                                    nm, cp->c_fid.Volume, cp->c_fid.Vnode,
1217                                    cp->c_fid.Unique)););
1218
1219     /* Remove the file's entry from the CODA Name Cache */
1220     /* We're being conservative here, it might be that this person
1221      * doesn't really have sufficient access to delete the file
1222      * but we feel zapping the entry won't really hurt anyone -- dcs
1223      */
1224     /* I'm gonna go out on a limb here. If a file and a hardlink to it
1225      * exist, and one is removed, the link count on the other will be
1226      * off by 1. We could either invalidate the attrs if cached, or
1227      * fix them. I'll try to fix them. DCS 11/8/94
1228      */
1229     tp = coda_nc_lookup(VTOC(dvp), nm, len, cred);
1230     if (tp) {
1231         if (VALID_VATTR(tp)) {  /* If attrs are cached */
1232             if (tp->c_vattr.va_nlink > 1) {     /* If it's a hard link */
1233                 tp->c_vattr.va_nlink--;
1234             }
1235         }
1236         
1237         coda_nc_zapfile(VTOC(dvp), nm, len); 
1238         /* No need to flush it if it doesn't exist! */
1239     }
1240     /* Invalidate the parent's attr cache, the modification time has changed */
1241     VTOC(dvp)->c_flags &= ~C_VATTR;
1242
1243     /* Check for remove of control object. */
1244     if (IS_CTL_NAME(dvp, nm, len)) {
1245         MARK_INT_FAIL(CODA_REMOVE_STATS);
1246         return(ENOENT);
1247     }
1248
1249     error = venus_remove(vtomi(dvp), &cp->c_fid, nm, len, cred, p);
1250
1251     CODADEBUG(CODA_REMOVE, myprintf(("in remove result %d\n",error)); )
1252
1253     return(error);
1254 }
1255
1256 int
1257 coda_link(v)
1258     void *v;
1259 {
1260 /* true args */
1261     struct vop_link_args *ap = v;
1262     struct vnode *vp = ap->a_vp;
1263     struct cnode *cp = VTOC(vp);
1264     struct vnode *tdvp = ap->a_tdvp;
1265     struct cnode *tdcp = VTOC(tdvp);
1266     struct componentname *cnp = ap->a_cnp;
1267     struct ucred *cred = cnp->cn_cred;
1268     struct proc *p = cnp->cn_proc;
1269 /* locals */
1270     int error;
1271     const char *nm = cnp->cn_nameptr;
1272     int len = cnp->cn_namelen;
1273
1274     MARK_ENTRY(CODA_LINK_STATS);
1275
1276     if (codadebug & CODADBGMSK(CODA_LINK)) {
1277
1278         myprintf(("nb_link:   vp fid: (%lx.%lx.%lx)\n",
1279                   cp->c_fid.Volume, cp->c_fid.Vnode, cp->c_fid.Unique));
1280         myprintf(("nb_link: tdvp fid: (%lx.%lx.%lx)\n",
1281                   tdcp->c_fid.Volume, tdcp->c_fid.Vnode, tdcp->c_fid.Unique));
1282         
1283     }
1284     if (codadebug & CODADBGMSK(CODA_LINK)) {
1285         myprintf(("link:   vp fid: (%lx.%lx.%lx)\n",
1286                   cp->c_fid.Volume, cp->c_fid.Vnode, cp->c_fid.Unique));
1287         myprintf(("link: tdvp fid: (%lx.%lx.%lx)\n",
1288                   tdcp->c_fid.Volume, tdcp->c_fid.Vnode, tdcp->c_fid.Unique));
1289
1290     }
1291
1292     /* Check for link to/from control object. */
1293     if (IS_CTL_NAME(tdvp, nm, len) || IS_CTL_VP(vp)) {
1294         MARK_INT_FAIL(CODA_LINK_STATS);
1295         return(EACCES);
1296     }
1297
1298     error = venus_link(vtomi(vp), &cp->c_fid, &tdcp->c_fid, nm, len, cred, p);
1299
1300     /* Invalidate the parent's attr cache, the modification time has changed */
1301     VTOC(tdvp)->c_flags &= ~C_VATTR;
1302     VTOC(vp)->c_flags &= ~C_VATTR;
1303
1304     CODADEBUG(CODA_LINK,        myprintf(("in link result %d\n",error)); )
1305
1306     return(error);
1307 }
1308
1309 int
1310 coda_rename(v)
1311     void *v;
1312 {
1313 /* true args */
1314     struct vop_rename_args *ap = v;
1315     struct vnode *odvp = ap->a_fdvp;
1316     struct cnode *odcp = VTOC(odvp);
1317     struct componentname  *fcnp = ap->a_fcnp;
1318     struct vnode *ndvp = ap->a_tdvp;
1319     struct cnode *ndcp = VTOC(ndvp);
1320     struct componentname  *tcnp = ap->a_tcnp;
1321     struct ucred *cred = fcnp->cn_cred;
1322     struct proc *p = fcnp->cn_proc;
1323 /* true args */
1324     int error;
1325     const char *fnm = fcnp->cn_nameptr;
1326     int flen = fcnp->cn_namelen;
1327     const char *tnm = tcnp->cn_nameptr;
1328     int tlen = tcnp->cn_namelen;
1329
1330     MARK_ENTRY(CODA_RENAME_STATS);
1331
1332     /* Hmmm.  The vnodes are already looked up.  Perhaps they are locked?
1333        This could be Bad. XXX */
1334 #ifdef OLD_DIAGNOSTIC
1335     if ((fcnp->cn_cred != tcnp->cn_cred)
1336         || (fcnp->cn_proc != tcnp->cn_proc))
1337     {
1338         panic("coda_rename: component names don't agree");
1339     }
1340 #endif
1341
1342     /* Check for rename involving control object. */ 
1343     if (IS_CTL_NAME(odvp, fnm, flen) || IS_CTL_NAME(ndvp, tnm, tlen)) {
1344         MARK_INT_FAIL(CODA_RENAME_STATS);
1345         return(EACCES);
1346     }
1347
1348     /* Problem with moving directories -- need to flush entry for .. */
1349     if (odvp != ndvp) {
1350         struct cnode *ovcp = coda_nc_lookup(VTOC(odvp), fnm, flen, cred);
1351         if (ovcp) {
1352             struct vnode *ovp = CTOV(ovcp);
1353             if ((ovp) &&
1354                 (ovp->v_type == VDIR)) /* If it's a directory */
1355                 coda_nc_zapfile(VTOC(ovp),"..", 2);
1356         }
1357     }
1358
1359     /* Remove the entries for both source and target files */
1360     coda_nc_zapfile(VTOC(odvp), fnm, flen);
1361     coda_nc_zapfile(VTOC(ndvp), tnm, tlen);
1362
1363     /* Invalidate the parent's attr cache, the modification time has changed */
1364     VTOC(odvp)->c_flags &= ~C_VATTR;
1365     VTOC(ndvp)->c_flags &= ~C_VATTR;
1366
1367     if (flen+1 > CODA_MAXNAMLEN) {
1368         MARK_INT_FAIL(CODA_RENAME_STATS);
1369         error = EINVAL;
1370         goto exit;
1371     }
1372
1373     if (tlen+1 > CODA_MAXNAMLEN) {
1374         MARK_INT_FAIL(CODA_RENAME_STATS);
1375         error = EINVAL;
1376         goto exit;
1377     }
1378
1379     error = venus_rename(vtomi(odvp), &odcp->c_fid, &ndcp->c_fid, fnm, flen, tnm, tlen, cred, p);
1380
1381  exit:
1382     CODADEBUG(CODA_RENAME, myprintf(("in rename result %d\n",error));)
1383     /* XXX - do we need to call cache pureg on the moved vnode? */
1384     cache_purge(ap->a_fvp);
1385
1386     /* It seems to be incumbent on us to drop locks on all four vnodes */
1387     /* From-vnodes are not locked, only ref'd.  To-vnodes are locked. */
1388
1389     vrele(ap->a_fvp);
1390     vrele(odvp);
1391
1392     if (ap->a_tvp) {
1393         if (ap->a_tvp == ndvp) {
1394             vrele(ap->a_tvp);
1395         } else {
1396             vput(ap->a_tvp);
1397         }
1398     }
1399
1400     vput(ndvp);
1401     return(error);
1402 }
1403
1404 int
1405 coda_mkdir(v)
1406     void *v;
1407 {
1408 /* true args */
1409     struct vop_mkdir_args *ap = v;
1410     struct vnode *dvp = ap->a_dvp;
1411     struct cnode *dcp = VTOC(dvp);      
1412     struct componentname  *cnp = ap->a_cnp;
1413     register struct vattr *va = ap->a_vap;
1414     struct vnode **vpp = ap->a_vpp;
1415     struct ucred *cred = cnp->cn_cred;
1416     struct proc *p = cnp->cn_proc;
1417 /* locals */
1418     int error;
1419     const char *nm = cnp->cn_nameptr;
1420     int len = cnp->cn_namelen;
1421     struct cnode *cp;
1422     ViceFid VFid;
1423     struct vattr ova;
1424
1425     MARK_ENTRY(CODA_MKDIR_STATS);
1426
1427     /* Check for mkdir of target object. */
1428     if (IS_CTL_NAME(dvp, nm, len)) {
1429         *vpp = (struct vnode *)0;
1430         MARK_INT_FAIL(CODA_MKDIR_STATS);
1431         return(EACCES);
1432     }
1433
1434     if (len+1 > CODA_MAXNAMLEN) {
1435         *vpp = (struct vnode *)0;
1436         MARK_INT_FAIL(CODA_MKDIR_STATS);
1437         return(EACCES);
1438     }
1439
1440     error = venus_mkdir(vtomi(dvp), &dcp->c_fid, nm, len, va, cred, p, &VFid, &ova);
1441
1442     if (!error) {
1443         if (coda_find(&VFid) != NULL)
1444             panic("cnode existed for newly created directory!");
1445         
1446         
1447         cp =  make_coda_node(&VFid, dvp->v_mount, va->va_type);
1448         *vpp = CTOV(cp);
1449         
1450         /* enter the new vnode in the Name Cache */
1451         coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
1452
1453         /* as a side effect, enter "." and ".." for the directory */
1454         coda_nc_enter(VTOC(*vpp), ".", 1, cred, VTOC(*vpp));
1455         coda_nc_enter(VTOC(*vpp), "..", 2, cred, VTOC(dvp));
1456
1457         if (coda_attr_cache) {
1458             VTOC(*vpp)->c_vattr = ova;          /* update the attr cache */
1459             VTOC(*vpp)->c_flags |= C_VATTR;     /* Valid attributes in cnode */
1460         }
1461
1462         /* Invalidate the parent's attr cache, the modification time has changed */
1463         VTOC(dvp)->c_flags &= ~C_VATTR;
1464         
1465         CODADEBUG( CODA_MKDIR, myprintf(("mkdir: (%lx.%lx.%lx) result %d\n",
1466                                     VFid.Volume, VFid.Vnode, VFid.Unique, error)); )
1467     } else {
1468         *vpp = (struct vnode *)0;
1469         CODADEBUG(CODA_MKDIR, myprintf(("mkdir error %d\n",error));)
1470     }
1471
1472     return(error);
1473 }
1474
1475 int
1476 coda_rmdir(v)
1477     void *v;
1478 {
1479 /* true args */
1480     struct vop_rmdir_args *ap = v;
1481     struct vnode *dvp = ap->a_dvp;
1482     struct cnode *dcp = VTOC(dvp);
1483     struct componentname  *cnp = ap->a_cnp;
1484     struct ucred *cred = cnp->cn_cred;
1485     struct proc *p = cnp->cn_proc;
1486 /* true args */
1487     int error;
1488     const char *nm = cnp->cn_nameptr;
1489     int len = cnp->cn_namelen;
1490     struct cnode *cp;
1491    
1492     MARK_ENTRY(CODA_RMDIR_STATS);
1493
1494     /* Check for rmdir of control object. */
1495     if (IS_CTL_NAME(dvp, nm, len)) {
1496         MARK_INT_FAIL(CODA_RMDIR_STATS);
1497         return(ENOENT);
1498     }
1499
1500     /* We're being conservative here, it might be that this person
1501      * doesn't really have sufficient access to delete the file
1502      * but we feel zapping the entry won't really hurt anyone -- dcs
1503      */
1504     /*
1505      * As a side effect of the rmdir, remove any entries for children of
1506      * the directory, especially "." and "..".
1507      */
1508     cp = coda_nc_lookup(dcp, nm, len, cred);
1509     if (cp) coda_nc_zapParentfid(&(cp->c_fid), NOT_DOWNCALL);
1510
1511     /* Remove the file's entry from the CODA Name Cache */
1512     coda_nc_zapfile(dcp, nm, len);
1513
1514     /* Invalidate the parent's attr cache, the modification time has changed */
1515     dcp->c_flags &= ~C_VATTR;
1516
1517     error = venus_rmdir(vtomi(dvp), &dcp->c_fid, nm, len, cred, p);
1518
1519     CODADEBUG(CODA_RMDIR, myprintf(("in rmdir result %d\n", error)); )
1520
1521     return(error);
1522 }
1523
1524 int
1525 coda_symlink(v)
1526     void *v;
1527 {
1528 /* true args */
1529     struct vop_symlink_args *ap = v;
1530     struct vnode *tdvp = ap->a_dvp;
1531     struct cnode *tdcp = VTOC(tdvp);    
1532     struct componentname *cnp = ap->a_cnp;
1533     struct vattr *tva = ap->a_vap;
1534     char *path = ap->a_target;
1535     struct ucred *cred = cnp->cn_cred;
1536     struct proc *p = cnp->cn_proc;
1537     struct vnode **vpp = ap->a_vpp;
1538 /* locals */
1539     int error;
1540     /* 
1541      * XXX I'm assuming the following things about coda_symlink's
1542      * arguments: 
1543      *       t(foo) is the new name/parent/etc being created.
1544      *       lname is the contents of the new symlink. 
1545      */
1546     char *nm = cnp->cn_nameptr;
1547     int len = cnp->cn_namelen;
1548     int plen = strlen(path);
1549
1550     /* 
1551      * Here's the strategy for the moment: perform the symlink, then
1552      * do a lookup to grab the resulting vnode.  I know this requires
1553      * two communications with Venus for a new sybolic link, but
1554      * that's the way the ball bounces.  I don't yet want to change
1555      * the way the Mach symlink works.  When Mach support is
1556      * deprecated, we should change symlink so that the common case
1557      * returns the resultant vnode in a vpp argument.
1558      */
1559
1560     MARK_ENTRY(CODA_SYMLINK_STATS);
1561
1562     /* Check for symlink of control object. */
1563     if (IS_CTL_NAME(tdvp, nm, len)) {
1564         MARK_INT_FAIL(CODA_SYMLINK_STATS);
1565         return(EACCES);
1566     }
1567
1568     if (plen+1 > CODA_MAXPATHLEN) {
1569         MARK_INT_FAIL(CODA_SYMLINK_STATS);
1570         return(EINVAL);
1571     }
1572
1573     if (len+1 > CODA_MAXNAMLEN) {
1574         MARK_INT_FAIL(CODA_SYMLINK_STATS);
1575         error = EINVAL;
1576         goto exit;
1577     }
1578
1579     error = venus_symlink(vtomi(tdvp), &tdcp->c_fid, path, plen, nm, len, tva, cred, p);
1580
1581     /* Invalidate the parent's attr cache, the modification time has changed */
1582     tdcp->c_flags &= ~C_VATTR;
1583
1584     if (error == 0)
1585         error = VOP_LOOKUP(tdvp, vpp, cnp);
1586
1587  exit:    
1588     CODADEBUG(CODA_SYMLINK, myprintf(("in symlink result %d\n",error)); )
1589     return(error);
1590 }
1591
1592 /*
1593  * Read directory entries.
1594  */
1595 int
1596 coda_readdir(v)
1597     void *v;
1598 {
1599 /* true args */
1600     struct vop_readdir_args *ap = v;
1601     struct vnode *vp = ap->a_vp;
1602     struct cnode *cp = VTOC(vp);
1603     register struct uio *uiop = ap->a_uio;
1604     struct ucred *cred = ap->a_cred;
1605     int *eofflag = ap->a_eofflag;
1606     u_long **cookies = ap->a_cookies;
1607     int *ncookies = ap->a_ncookies;
1608     struct proc *p = ap->a_uio->uio_procp;
1609 /* upcall decl */
1610 /* locals */
1611     int error = 0;
1612
1613     MARK_ENTRY(CODA_READDIR_STATS);
1614
1615     CODADEBUG(CODA_READDIR, myprintf(("coda_readdir(%p, %d, %lld, %d)\n",
1616                                       (void *)uiop->uio_iov->iov_base,
1617                                       uiop->uio_resid,
1618                                       (long long)uiop->uio_offset,
1619                                       uiop->uio_segflg)); )
1620         
1621     /* Check for readdir of control object. */
1622     if (IS_CTL_VP(vp)) {
1623         MARK_INT_FAIL(CODA_READDIR_STATS);
1624         return(ENOENT);
1625     }
1626
1627     {
1628         /* If directory is not already open do an "internal open" on it. */
1629         int opened_internally = 0;
1630         if (cp->c_ovp == NULL) {
1631             opened_internally = 1;
1632             MARK_INT_GEN(CODA_OPEN_STATS);
1633             error = VOP_OPEN(vp, FREAD, cred, p);
1634 printf("coda_readdir: Internally Opening %p\n", vp);
1635             if (error) {
1636                 printf("coda_readdir: VOP_OPEN on container failed %d\n", error);
1637                 return (error);
1638             }
1639             if (vp->v_type == VREG) {
1640                 error = vfs_object_create(vp, p, cred);
1641                 if (error != 0) {
1642                     printf("coda_readdir: vfs_object_create() returns %d\n", error);
1643                     vput(vp);
1644                 }
1645             }
1646             if (error) return(error);
1647         }
1648         
1649         /* Have UFS handle the call. */
1650         CODADEBUG(CODA_READDIR, myprintf(("indirect readdir: fid = (%lx.%lx.%lx), refcnt = %d\n",cp->c_fid.Volume, cp->c_fid.Vnode, cp->c_fid.Unique, vp->v_usecount)); )
1651         error = VOP_READDIR(cp->c_ovp, uiop, cred, eofflag, ncookies,
1652                                cookies);
1653         
1654         if (error)
1655             MARK_INT_FAIL(CODA_READDIR_STATS);
1656         else
1657             MARK_INT_SAT(CODA_READDIR_STATS);
1658         
1659         /* Do an "internal close" if necessary. */ 
1660         if (opened_internally) {
1661             MARK_INT_GEN(CODA_CLOSE_STATS);
1662             (void)VOP_CLOSE(vp, FREAD, cred, p);
1663         }
1664     }
1665
1666     return(error);
1667 }
1668
1669 /*
1670  * Convert from file system blocks to device blocks
1671  */
1672 int
1673 coda_bmap(v)
1674     void *v;
1675 {
1676     /* XXX on the global proc */
1677 /* true args */
1678     struct vop_bmap_args *ap = v;
1679     struct vnode *vp __attribute__((unused)) = ap->a_vp;        /* file's vnode */
1680     daddr_t bn __attribute__((unused)) = ap->a_bn;      /* fs block number */
1681     struct vnode **vpp = ap->a_vpp;                     /* RETURN vp of device */
1682     daddr_t *bnp __attribute__((unused)) = ap->a_bnp;   /* RETURN device block number */
1683     struct proc *p __attribute__((unused)) = curproc;
1684 /* upcall decl */
1685 /* locals */
1686
1687         int ret = 0;
1688         struct cnode *cp;
1689
1690         cp = VTOC(vp);
1691         if (cp->c_ovp) {
1692                 return EINVAL;
1693                 ret =  VOP_BMAP(cp->c_ovp, bn, vpp, bnp, ap->a_runp, ap->a_runb);
1694 #if     0
1695                 printf("VOP_BMAP(cp->c_ovp %p, bn %p, vpp %p, bnp %p, ap->a_runp %p, ap->a_runb %p) = %d\n",
1696                         cp->c_ovp, bn, vpp, bnp, ap->a_runp, ap->a_runb, ret);
1697 #endif
1698                 return ret;
1699         } else {
1700 #if     0
1701                 printf("coda_bmap: no container\n");
1702 #endif
1703                 return(EOPNOTSUPP);
1704         }
1705 }
1706
1707 /*
1708  * I don't think the following two things are used anywhere, so I've
1709  * commented them out 
1710  * 
1711  * struct buf *async_bufhead; 
1712  * int async_daemon_count;
1713  */
1714 int
1715 coda_strategy(v)
1716     void *v;
1717 {
1718 /* true args */
1719     struct vop_strategy_args *ap = v;
1720     register struct buf *bp __attribute__((unused)) = ap->a_bp;
1721     struct proc *p __attribute__((unused)) = curproc;
1722 /* upcall decl */
1723 /* locals */
1724
1725         printf("coda_strategy: called ???\n");
1726         return(EOPNOTSUPP);
1727 }
1728
1729 int
1730 coda_reclaim(v) 
1731     void *v;
1732 {
1733 /* true args */
1734     struct vop_reclaim_args *ap = v;
1735     struct vnode *vp = ap->a_vp;
1736     struct cnode *cp = VTOC(vp);
1737 /* upcall decl */
1738 /* locals */
1739
1740 /*
1741  * Forced unmount/flush will let vnodes with non zero use be destroyed!
1742  */
1743     ENTRY;
1744
1745     if (IS_UNMOUNTING(cp)) {
1746 #ifdef  DEBUG
1747         if (VTOC(vp)->c_ovp) {
1748             if (IS_UNMOUNTING(cp))
1749                 printf("coda_reclaim: c_ovp not void: vp %p, cp %p\n", vp, cp);
1750         }
1751 #endif
1752     } else {
1753 #ifdef OLD_DIAGNOSTIC
1754         if (vp->v_usecount != 0) 
1755             print("coda_reclaim: pushing active %p\n", vp);
1756         if (VTOC(vp)->c_ovp) {
1757             panic("coda_reclaim: c_ovp not void");
1758     }
1759 #endif
1760     }   
1761     cache_purge(vp);
1762     coda_free(VTOC(vp));
1763     VTOC(vp) = NULL;
1764     return (0);
1765 }
1766
1767 int
1768 coda_lock(v)
1769     void *v;
1770 {
1771 /* true args */
1772     struct vop_lock_args *ap = v;
1773     struct vnode *vp = ap->a_vp;
1774     struct cnode *cp = VTOC(vp);
1775     struct proc  *p = ap->a_p;
1776 /* upcall decl */
1777 /* locals */
1778
1779     ENTRY;
1780
1781     if (coda_lockdebug) {
1782         myprintf(("Attempting lock on %lx.%lx.%lx\n",
1783                   cp->c_fid.Volume, cp->c_fid.Vnode, cp->c_fid.Unique));
1784     }
1785
1786 #ifndef DEBUG_LOCKS
1787     return (lockmgr(&cp->c_lock, ap->a_flags, &vp->v_interlock, p));
1788 #else
1789     return (debuglockmgr(&cp->c_lock, ap->a_flags, &vp->v_interlock, p,
1790                          "coda_lock", vp->filename, vp->line));
1791 #endif
1792 }
1793
1794 int
1795 coda_unlock(v)
1796     void *v;
1797 {
1798 /* true args */
1799     struct vop_unlock_args *ap = v;
1800     struct vnode *vp = ap->a_vp;
1801     struct cnode *cp = VTOC(vp);
1802     struct proc  *p = ap->a_p;
1803 /* upcall decl */
1804 /* locals */
1805
1806     ENTRY;
1807     if (coda_lockdebug) {
1808         myprintf(("Attempting unlock on %lx.%lx.%lx\n",
1809                   cp->c_fid.Volume, cp->c_fid.Vnode, cp->c_fid.Unique));
1810     }
1811
1812     return (lockmgr(&cp->c_lock, ap->a_flags | LK_RELEASE, &vp->v_interlock, p));
1813 }
1814
1815 int
1816 coda_islocked(v)
1817     void *v;
1818 {
1819 /* true args */
1820     struct vop_islocked_args *ap = v;
1821     struct cnode *cp = VTOC(ap->a_vp);
1822     ENTRY;
1823
1824     return (lockstatus(&cp->c_lock, ap->a_p));
1825 }
1826
1827 /* How one looks up a vnode given a device/inode pair: */
1828 int
1829 coda_grab_vnode(dev_t dev, ino_t ino, struct vnode **vpp)
1830 {
1831     /* This is like VFS_VGET() or igetinode()! */
1832     int           error;
1833     struct mount *mp;
1834
1835     if (!(mp = devtomp(dev))) {
1836         myprintf(("coda_grab_vnode: devtomp(%#lx) returns NULL\n",
1837                   (u_long)dev2udev(dev)));
1838         return(ENXIO);
1839     }
1840
1841     /* XXX - ensure that nonzero-return means failure */
1842     error = VFS_VGET(mp,ino,vpp);
1843     if (error) {
1844         myprintf(("coda_grab_vnode: iget/vget(%lx, %lu) returns %p, err %d\n", 
1845                   (u_long)dev2udev(dev), (u_long)ino, (void *)*vpp, error));
1846         return(ENOENT);
1847     }
1848     return(0);
1849 }
1850
1851 void
1852 print_vattr( attr )
1853         struct vattr *attr;
1854 {
1855     char *typestr;
1856
1857     switch (attr->va_type) {
1858     case VNON:
1859         typestr = "VNON";
1860         break;
1861     case VREG:
1862         typestr = "VREG";
1863         break;
1864     case VDIR:
1865         typestr = "VDIR";
1866         break;
1867     case VBLK:
1868         typestr = "VBLK";
1869         break;
1870     case VCHR:
1871         typestr = "VCHR";
1872         break;
1873     case VLNK:
1874         typestr = "VLNK";
1875         break;
1876     case VSOCK:
1877         typestr = "VSCK";
1878         break;
1879     case VFIFO:
1880         typestr = "VFFO";
1881         break;
1882     case VBAD:
1883         typestr = "VBAD";
1884         break;
1885     default:
1886         typestr = "????";
1887         break;
1888     }
1889
1890
1891     myprintf(("attr: type %s mode %d uid %d gid %d fsid %d rdev %d\n",
1892               typestr, (int)attr->va_mode, (int)attr->va_uid,
1893               (int)attr->va_gid, (int)attr->va_fsid, (int)attr->va_rdev));
1894
1895     myprintf(("      fileid %d nlink %d size %d blocksize %d bytes %d\n",
1896               (int)attr->va_fileid, (int)attr->va_nlink, 
1897               (int)attr->va_size,
1898               (int)attr->va_blocksize,(int)attr->va_bytes));
1899     myprintf(("      gen %ld flags %ld vaflags %d\n",
1900               attr->va_gen, attr->va_flags, attr->va_vaflags));
1901     myprintf(("      atime sec %d nsec %d\n",
1902               (int)attr->va_atime.tv_sec, (int)attr->va_atime.tv_nsec));
1903     myprintf(("      mtime sec %d nsec %d\n",
1904               (int)attr->va_mtime.tv_sec, (int)attr->va_mtime.tv_nsec));
1905     myprintf(("      ctime sec %d nsec %d\n",
1906               (int)attr->va_ctime.tv_sec, (int)attr->va_ctime.tv_nsec));
1907 }
1908
1909 /* How to print a ucred */
1910 void
1911 print_cred(cred)
1912         struct ucred *cred;
1913 {
1914
1915         int i;
1916
1917         myprintf(("ref %d\tuid %d\n",cred->cr_ref,cred->cr_uid));
1918
1919         for (i=0; i < cred->cr_ngroups; i++)
1920                 myprintf(("\tgroup %d: (%d)\n",i,cred->cr_groups[i]));
1921         myprintf(("\n"));
1922
1923 }
1924
1925 /*
1926  * Return a vnode for the given fid.
1927  * If no cnode exists for this fid create one and put it
1928  * in a table hashed by fid.Volume and fid.Vnode.  If the cnode for
1929  * this fid is already in the table return it (ref count is
1930  * incremented by coda_find.  The cnode will be flushed from the
1931  * table when coda_inactive calls coda_unsave.
1932  */
1933 struct cnode *
1934 make_coda_node(fid, vfsp, type)
1935      ViceFid *fid; struct mount *vfsp; short type;
1936 {
1937     struct cnode *cp;
1938     int          err;
1939
1940     if ((cp = coda_find(fid)) == NULL) {
1941         struct vnode *vp;
1942         
1943         cp = coda_alloc();
1944         lockinit(&cp->c_lock, PINOD, "cnode", 0, 0);
1945         cp->c_fid = *fid;
1946         
1947         err = getnewvnode(VT_CODA, vfsp, coda_vnodeop_p, &vp);  
1948         if (err) {                                                
1949             panic("coda: getnewvnode returned error %d\n", err);   
1950         }                                                         
1951         vp->v_data = cp;                                          
1952         vp->v_type = type;                                      
1953         cp->c_vnode = vp;                                         
1954         coda_save(cp);
1955         
1956     } else {
1957         vref(CTOV(cp));
1958     }
1959
1960     return cp;
1961 }