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