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