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