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