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