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