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