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