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