coda_vnops.c revision 43382
138625Srvb/*
238759Srvb *
338759Srvb *             Coda: an Experimental Distributed File System
438759Srvb *                              Release 3.1
538759Srvb *
638759Srvb *           Copyright (c) 1987-1998 Carnegie Mellon University
738759Srvb *                          All Rights Reserved
838759Srvb *
938759Srvb * Permission  to  use, copy, modify and distribute this software and its
1038759Srvb * documentation is hereby granted,  provided  that  both  the  copyright
1138759Srvb * notice  and  this  permission  notice  appear  in  all  copies  of the
1238759Srvb * software, derivative works or  modified  versions,  and  any  portions
1338759Srvb * thereof, and that both notices appear in supporting documentation, and
1438759Srvb * that credit is given to Carnegie Mellon University  in  all  documents
1538759Srvb * and publicity pertaining to direct or indirect use of this code or its
1638759Srvb * derivatives.
1738759Srvb *
1838759Srvb * CODA IS AN EXPERIMENTAL SOFTWARE SYSTEM AND IS  KNOWN  TO  HAVE  BUGS,
1938759Srvb * SOME  OF  WHICH MAY HAVE SERIOUS CONSEQUENCES.  CARNEGIE MELLON ALLOWS
2038759Srvb * FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.   CARNEGIE  MELLON
2138759Srvb * DISCLAIMS  ANY  LIABILITY  OF  ANY  KIND  FOR  ANY  DAMAGES WHATSOEVER
2238759Srvb * RESULTING DIRECTLY OR INDIRECTLY FROM THE USE OF THIS SOFTWARE  OR  OF
2338759Srvb * ANY DERIVATIVE WORK.
2438759Srvb *
2538759Srvb * Carnegie  Mellon  encourages  users  of  this  software  to return any
2638759Srvb * improvements or extensions that  they  make,  and  to  grant  Carnegie
2738759Srvb * Mellon the rights to redistribute these changes without encumbrance.
2838759Srvb *
2939126Srvb *  	@(#) src/sys/coda/coda_vnops.c,v 1.1.1.1 1998/08/29 21:14:52 rvb Exp $
3043382Sbde *  $Id: coda_vnops.c,v 1.14 1999/01/27 20:09:17 dillon Exp $
3138759Srvb *
3238759Srvb */
3338625Srvb
3438625Srvb/*
3538625Srvb * Mach Operating System
3638625Srvb * Copyright (c) 1990 Carnegie-Mellon University
3738625Srvb * Copyright (c) 1989 Carnegie-Mellon University
3838625Srvb * All rights reserved.  The CMU software License Agreement specifies
3938625Srvb * the terms and conditions for use and redistribution.
4038625Srvb */
4138625Srvb
4238625Srvb/*
4338625Srvb * This code was written for the Coda file system at Carnegie Mellon
4438625Srvb * University.  Contributers include David Steere, James Kistler, and
4538625Srvb * M. Satyanarayanan.
4638625Srvb */
4738625Srvb
4838625Srvb/*
4938625Srvb * HISTORY
5039126Srvb * $Log: coda_vnops.c,v $
5143382Sbde * Revision 1.14  1999/01/27 20:09:17  dillon
5243382Sbde *     Fix warnings preparing for -Wall -Wcast-qual
5343382Sbde *
5443382Sbde *     Also disable one usb module in LINT due to fatal compilation errors,
5543382Sbde *     temporary.
5643382Sbde *
5743295Sdillon * Revision 1.13  1999/01/20 14:49:05  eivind
5843295Sdillon * Add 'options DEBUG_LOCKS', which stores extra information in struct
5943295Sdillon * lock, and add some macros and function parameters to make sure that
6043295Sdillon * the information get to the point where it can be put in the lock
6143295Sdillon * structure.
6243295Sdillon *
6343295Sdillon * While I'm here, add DEBUG_VFS_LOCKS to LINT.
6443295Sdillon *
6542900Seivind * Revision 1.12  1999/01/07 16:14:12  bde
6642900Seivind * Don't pass unused unused timestamp args to UFS_UPDATE() or waste
6742900Seivind * time initializing them.  This almost finishes centralizing (in-core)
6842900Seivind * timestamp updates in ufs_itimes().
6942900Seivind *
7042374Sbde * Revision 1.11  1999/01/05 18:49:51  eivind
7142374Sbde * Remove the 'waslocked' parameter to vfs_object_create().
7242374Sbde *
7342315Seivind * Revision 1.10  1998/12/04 18:44:21  rvb
7442315Seivind * Don't print diagnostic anymore
7542315Seivind *
7641504Srvb * Revision 1.9  1998/11/16 19:48:26  rvb
7741504Srvb * A few bug fixes for Robert Watson
7841504Srvb *
7941202Srvb * Revision 1.8  1998/10/28 20:31:13  rvb
8041202Srvb * Change the way unmounting happens to guarantee that the
8141202Srvb * client programs are allowed to finish up (coda_call is
8241202Srvb * forced to complete) and release their locks.  Thus there
8341202Srvb * is a reasonable chance that the vflush implicit in the
8441202Srvb * unmount will not get hung on held locks.
8541202Srvb *
8640708Srvb * Revision 1.7  1998/10/25 17:44:41  phk
8740708Srvb * Nitpicking and dusting performed on a train.  Removes trivial warnings
8840708Srvb * about unused variables, labels and other lint.
8940708Srvb *
9040648Sphk * Revision 1.6  1998/09/28 20:52:58  rvb
9140648Sphk * Cleanup and fix THE bug
9240648Sphk *
9339728Srvb * Revision 1.5  1998/09/25 17:38:32  rvb
9439728Srvb * Put "stray" printouts under DIAGNOSTIC.  Make everything build
9539728Srvb * with DEBUG on.  Add support for lkm.  (The macro's don't work
9639728Srvb * for me; for a good chuckle look at the end of coda_fbsd.c.)
9739728Srvb *
9839650Srvb * Revision 1.4  1998/09/13 13:57:59  rvb
9939650Srvb * Finish conversion of cfs -> coda
10039650Srvb *
10139126Srvb * Revision 1.3  1998/09/11 18:50:17  rvb
10239126Srvb * All the references to cfs, in symbols, structs, and strings
10339126Srvb * have been changed to coda.  (Same for CFS.)
10439126Srvb *
10539085Srvb * Revision 1.2  1998/09/02 19:09:53  rvb
10639085Srvb * Pass2 complete
10739085Srvb *
10838759Srvb * Revision 1.1.1.1  1998/08/29 21:14:52  rvb
10938759Srvb * Very Preliminary Coda
11038759Srvb *
11138625Srvb * Revision 1.12  1998/08/28 18:28:00  rvb
11238625Srvb * NetBSD -current is stricter!
11338625Srvb *
11438625Srvb * Revision 1.11  1998/08/28 18:12:23  rvb
11538625Srvb * Now it also works on FreeBSD -current.  This code will be
11638625Srvb * committed to the FreeBSD -current and NetBSD -current
11738625Srvb * trees.  It will then be tailored to the particular platform
11838625Srvb * by flushing conditional code.
11938625Srvb *
12038625Srvb * Revision 1.10  1998/08/18 17:05:21  rvb
12138625Srvb * Don't use __RCSID now
12238625Srvb *
12338625Srvb * Revision 1.9  1998/08/18 16:31:46  rvb
12438625Srvb * Sync the code for NetBSD -current; test on 1.3 later
12538625Srvb *
12638625Srvb * Revision 1.8  98/02/24  22:22:50  rvb
12738625Srvb * Fixes up mainly to flush iopen and friends
12838625Srvb *
12938625Srvb * Revision 1.7  98/01/31  20:53:15  rvb
13038625Srvb * First version that works on FreeBSD 2.2.5
13138625Srvb *
13238625Srvb * Revision 1.6  98/01/23  11:53:47  rvb
13339085Srvb * Bring RVB_CODA1_1 to HEAD
13438625Srvb *
13538625Srvb * Revision 1.5.2.8  98/01/23  11:21:11  rvb
13638625Srvb * Sync with 2.2.5
13738625Srvb *
13838625Srvb * Revision 1.5.2.7  97/12/19  14:26:08  rvb
13938625Srvb * session id
14038625Srvb *
14138625Srvb * Revision 1.5.2.6  97/12/16  22:01:34  rvb
14238625Srvb * Oops add cfs_subr.h cfs_venus.h; sync with peter
14338625Srvb *
14438625Srvb * Revision 1.5.2.5  97/12/16  12:40:14  rvb
14538625Srvb * Sync with 1.3
14638625Srvb *
14738625Srvb * Revision 1.5.2.4  97/12/10  14:08:31  rvb
14839085Srvb * Fix O_ flags; check result in coda_call
14938625Srvb *
15038625Srvb * Revision 1.5.2.3  97/12/10  11:40:27  rvb
15138625Srvb * No more ody
15238625Srvb *
15338625Srvb * Revision 1.5.2.2  97/12/09  16:07:15  rvb
15438625Srvb * Sync with vfs/include/coda.h
15538625Srvb *
15638625Srvb * Revision 1.5.2.1  97/12/06  17:41:25  rvb
15738625Srvb * Sync with peters coda.h
15838625Srvb *
15938625Srvb * Revision 1.5  97/12/05  10:39:23  rvb
16038625Srvb * Read CHANGES
16138625Srvb *
16238625Srvb * Revision 1.4.14.10  97/11/25  08:08:48  rvb
16338625Srvb * cfs_venus ... done; until cred/vattr change
16438625Srvb *
16538625Srvb * Revision 1.4.14.9  97/11/24  15:44:48  rvb
16638625Srvb * Final cfs_venus.c w/o macros, but one locking bug
16738625Srvb *
16838625Srvb * Revision 1.4.14.8  97/11/21  11:28:04  rvb
16938625Srvb * cfs_venus.c is done: first pass
17038625Srvb *
17138625Srvb * Revision 1.4.14.7  97/11/20  11:46:51  rvb
17238625Srvb * Capture current cfs_venus
17338625Srvb *
17438625Srvb * Revision 1.4.14.6  97/11/18  10:27:19  rvb
17538625Srvb * cfs_nbsd.c is DEAD!!!; integrated into cfs_vf/vnops.c
17638625Srvb * cfs_nb_foo and cfs_foo are joined
17738625Srvb *
17838625Srvb * Revision 1.4.14.5  97/11/13  22:03:03  rvb
17938625Srvb * pass2 cfs_NetBSD.h mt
18038625Srvb *
18138625Srvb * Revision 1.4.14.4  97/11/12  12:09:42  rvb
18238625Srvb * reorg pass1
18338625Srvb *
18438625Srvb * Revision 1.4.14.3  97/11/06  21:03:28  rvb
18538625Srvb * don't include headers in headers
18638625Srvb *
18738625Srvb * Revision 1.4.14.2  97/10/29  16:06:30  rvb
18838625Srvb * Kill DYING
18938625Srvb *
19038625Srvb * Revision 1.4.14.1  1997/10/28 23:10:18  rvb
19138625Srvb * >64Meg; venus can be killed!
19238625Srvb *
19338625Srvb * Revision 1.4  1997/02/20 13:54:50  lily
19439085Srvb * check for NULL return from coda_nc_lookup before CTOV
19538625Srvb *
19638625Srvb * Revision 1.3  1996/12/12 22:11:02  bnoble
19738625Srvb * Fixed the "downcall invokes venus operation" deadlock in all known cases.
19838625Srvb * There may be more
19938625Srvb *
20038625Srvb * Revision 1.2  1996/01/02 16:57:07  bnoble
20138625Srvb * Added support for Coda MiniCache and raw inode calls (final commit)
20238625Srvb *
20338625Srvb * Revision 1.1.2.1  1995/12/20 01:57:34  bnoble
20439085Srvb * Added CODA-specific files
20538625Srvb *
20638625Srvb * Revision 3.1.1.1  1995/03/04  19:08:06  bnoble
20738625Srvb * Branch for NetBSD port revisions
20838625Srvb *
20938625Srvb * Revision 3.1  1995/03/04  19:08:04  bnoble
21038625Srvb * Bump to major revision 3 to prepare for NetBSD port
21138625Srvb *
21238625Srvb * Revision 2.6  1995/02/17  16:25:26  dcs
21338625Srvb * These versions represent several changes:
21438625Srvb * 1. Allow venus to restart even if outstanding references exist.
21538625Srvb * 2. Have only one ctlvp per client, as opposed to one per mounted cfs device.d
21638625Srvb * 3. Allow ody_expand to return many members, not just one.
21738625Srvb *
21838625Srvb * Revision 2.5  94/11/09  20:29:27  dcs
21938625Srvb * Small bug in remove dealing with hard links and link counts was fixed.
22038625Srvb *
22138625Srvb * Revision 2.4  94/10/14  09:58:42  dcs
22238625Srvb * Made changes 'cause sun4s have braindead compilers
22338625Srvb *
22438625Srvb * Revision 2.3  94/10/12  16:46:37  dcs
22538625Srvb * Cleaned kernel/venus interface by removing XDR junk, plus
22638625Srvb * so cleanup to allow this code to be more easily ported.
22738625Srvb *
22838625Srvb * Revision 2.2  94/09/20  14:12:41  dcs
22938625Srvb * Fixed bug in rename when moving a directory.
23038625Srvb *
23138625Srvb * Revision 2.1  94/07/21  16:25:22  satya
23238625Srvb * Conversion to C++ 3.0; start of Coda Release 2.0
23338625Srvb *
23438625Srvb * Revision 1.4  93/12/17  01:38:01  luqi
23538625Srvb * Changes made for kernel to pass process info to Venus:
23638625Srvb *
23738625Srvb * (1) in file cfs.h
23838625Srvb * add process id and process group id in most of the cfs argument types.
23938625Srvb *
24038625Srvb * (2) in file cfs_vnodeops.c
24138625Srvb * add process info passing in most of the cfs vnode operations.
24238625Srvb *
24338625Srvb * (3) in file cfs_xdr.c
24438625Srvb * expand xdr routines according changes in (1).
24538625Srvb * add variable pass_process_info to allow venus for kernel version checking.
24638625Srvb *
24738625Srvb * Revision 1.3  93/05/28  16:24:33  bnoble
24838625Srvb * *** empty log message ***
24938625Srvb *
25038625Srvb * Revision 1.2  92/10/27  17:58:25  lily
25138625Srvb * merge kernel/latest and alpha/src/cfs
25238625Srvb *
25338625Srvb * Revision 2.4  92/09/30  14:16:37  mja
25438625Srvb * 	Redid buffer allocation so that it does kmem_{alloc,free} for all
25538625Srvb * 	architectures.  Zone allocation, previously used on the 386, caused
25638625Srvb * 	panics if it was invoked repeatedly.  Stack allocation, previously
25738625Srvb * 	used on all other architectures, tickled some Mach bug that appeared
25838625Srvb * 	with large stack frames.
25938625Srvb * 	[91/02/09            jjk]
26038625Srvb *
26138625Srvb * 	Added contributors blurb.
26238625Srvb * 	[90/12/13            jjk]
26338625Srvb *
26438625Srvb * Revision 2.3  90/07/26  15:50:09  mrt
26538625Srvb * 	    Fixed fix to rename to remove .. from moved directories.
26638625Srvb * 	[90/06/28            dcs]
26738625Srvb *
26838625Srvb * Revision 1.7  90/06/28  16:24:25  dcs
26938625Srvb * Fixed bug with moving directories, we weren't flushing .. for the moved directory.
27038625Srvb *
27138625Srvb * Revision 1.6  90/05/31  17:01:47  dcs
27238625Srvb * Prepare for merge with facilities kernel.
27338625Srvb *
27438625Srvb *
27538625Srvb */
27638625Srvb
27738625Srvb#include <sys/param.h>
27838625Srvb#include <sys/systm.h>
27938759Srvb#include <sys/kernel.h>
28038759Srvb#include <sys/proc.h>
28138625Srvb#include <sys/malloc.h>
28238759Srvb#include <sys/mount.h>
28338625Srvb#include <sys/errno.h>
28438625Srvb#include <sys/acct.h>
28538625Srvb#include <sys/file.h>
28638759Srvb#include <sys/fcntl.h>
28738625Srvb#include <sys/uio.h>
28838625Srvb#include <sys/namei.h>
28938625Srvb#include <sys/ioccom.h>
29038625Srvb#include <sys/select.h>
29138759Srvb
29238625Srvb#include <vm/vm.h>
29338625Srvb#include <vm/vm_object.h>
29438625Srvb#include <vm/vm_extern.h>
29538759Srvb#include <vm/vm_zone.h>
29638625Srvb
29739126Srvb#include <coda/coda.h>
29839126Srvb#include <coda/cnode.h>
29939126Srvb#include <coda/coda_vnops.h>
30039126Srvb#include <coda/coda_venus.h>
30139126Srvb#include <coda/coda_opstats.h>
30239126Srvb#include <coda/coda_subr.h>
30339126Srvb#include <coda/coda_namecache.h>
30439126Srvb#include <coda/coda_pioctl.h>
30538625Srvb
30638625Srvb/*
30738625Srvb * These flags select various performance enhancements.
30838625Srvb */
30939085Srvbint coda_attr_cache  = 1;       /* Set to cache attributes in the kernel */
31039085Srvbint coda_symlink_cache = 1;     /* Set to cache symbolic link information */
31139085Srvbint coda_access_cache = 1;      /* Set to handle some access checks directly */
31238625Srvb
31338625Srvb/* structure to keep track of vfs calls */
31438625Srvb
31539085Srvbstruct coda_op_stats coda_vnodeopstats[CODA_VNODEOPS_SIZE];
31638625Srvb
31739085Srvb#define MARK_ENTRY(op) (coda_vnodeopstats[op].entries++)
31839085Srvb#define MARK_INT_SAT(op) (coda_vnodeopstats[op].sat_intrn++)
31939085Srvb#define MARK_INT_FAIL(op) (coda_vnodeopstats[op].unsat_intrn++)
32039085Srvb#define MARK_INT_GEN(op) (coda_vnodeopstats[op].gen_intrn++)
32138625Srvb
32238625Srvb/* What we are delaying for in printf */
32339085Srvbint coda_printf_delay = 0;  /* in microseconds */
32439085Srvbint coda_vnop_print_entry = 0;
32539085Srvbstatic int coda_lockdebug = 0;
32638625Srvb
32738625Srvb/* Definition of the vfs operation vector */
32838625Srvb
32938625Srvb/*
33038625Srvb * Some NetBSD details:
33138625Srvb *
33239085Srvb *   coda_start is called at the end of the mount syscall.
33339085Srvb *   coda_init is called at boot time.
33438625Srvb */
33538625Srvb
33639085Srvb#define ENTRY  if(coda_vnop_print_entry) myprintf(("Entered %s\n",__FUNCTION__))
33738625Srvb
33838625Srvb/* Definition of the vnode operation vector */
33938625Srvb
34039085Srvbstruct vnodeopv_entry_desc coda_vnodeop_entries[] = {
34139085Srvb    { &vop_default_desc, coda_vop_error },
34239085Srvb    { &vop_lookup_desc, coda_lookup },		/* lookup */
34339085Srvb    { &vop_create_desc, coda_create },		/* create */
34439650Srvb    { &vop_mknod_desc, coda_vop_error },	/* mknod */
34539085Srvb    { &vop_open_desc, coda_open },		/* open */
34639085Srvb    { &vop_close_desc, coda_close },		/* close */
34739085Srvb    { &vop_access_desc, coda_access },		/* access */
34839650Srvb    { &vop_getattr_desc, coda_getattr },	/* getattr */
34939650Srvb    { &vop_setattr_desc, coda_setattr },	/* setattr */
35039085Srvb    { &vop_read_desc, coda_read },		/* read */
35139085Srvb    { &vop_write_desc, coda_write },		/* write */
35239085Srvb    { &vop_ioctl_desc, coda_ioctl },		/* ioctl */
35339650Srvb    { &vop_mmap_desc, coda_vop_error },		/* mmap */
35439085Srvb    { &vop_fsync_desc, coda_fsync },		/* fsync */
35539085Srvb    { &vop_remove_desc, coda_remove },		/* remove */
35639085Srvb    { &vop_link_desc, coda_link },		/* link */
35739085Srvb    { &vop_rename_desc, coda_rename },		/* rename */
35839085Srvb    { &vop_mkdir_desc, coda_mkdir },		/* mkdir */
35939085Srvb    { &vop_rmdir_desc, coda_rmdir },		/* rmdir */
36039650Srvb    { &vop_symlink_desc, coda_symlink },	/* symlink */
36139650Srvb    { &vop_readdir_desc, coda_readdir },	/* readdir */
36239085Srvb    { &vop_readlink_desc, coda_readlink },	/* readlink */
36339650Srvb    { &vop_abortop_desc, coda_abortop },	/* abortop */
36439085Srvb    { &vop_inactive_desc, coda_inactive },	/* inactive */
36539650Srvb    { &vop_reclaim_desc, coda_reclaim },	/* reclaim */
36639085Srvb    { &vop_lock_desc, coda_lock },		/* lock */
36739085Srvb    { &vop_unlock_desc, coda_unlock },		/* unlock */
36839085Srvb    { &vop_bmap_desc, coda_bmap },		/* bmap */
36939085Srvb    { &vop_strategy_desc, coda_strategy },	/* strategy */
37039650Srvb    { &vop_print_desc, coda_vop_error },	/* print */
37139085Srvb    { &vop_islocked_desc, coda_islocked },	/* islocked */
37239650Srvb    { &vop_pathconf_desc, coda_vop_error },	/* pathconf */
37339650Srvb    { &vop_advlock_desc, coda_vop_nop },	/* advlock */
37439650Srvb    { &vop_bwrite_desc, coda_vop_error },	/* bwrite */
37539650Srvb    { &vop_lease_desc, coda_vop_nop },		/* lease */
37638759Srvb    { &vop_poll_desc, (vop_t *) vop_stdpoll },
37739085Srvb    { &vop_getpages_desc, coda_fbsd_getpages },	/* pager intf.*/
37839085Srvb    { &vop_putpages_desc, coda_fbsd_putpages },	/* pager intf.*/
37938625Srvb
38038759Srvb#if	0
38138625Srvb
38238759Srvb    we need to define these someday
38338625Srvb#define UFS_BLKATOFF(aa, bb, cc, dd) VFSTOUFS((aa)->v_mount)->um_blkatoff(aa, bb, cc, dd)
38438625Srvb#define UFS_VALLOC(aa, bb, cc, dd) VFSTOUFS((aa)->v_mount)->um_valloc(aa, bb, cc, dd)
38538625Srvb#define UFS_VFREE(aa, bb, cc) VFSTOUFS((aa)->v_mount)->um_vfree(aa, bb, cc)
38638625Srvb#define UFS_TRUNCATE(aa, bb, cc, dd, ee) VFSTOUFS((aa)->v_mount)->um_truncate(aa, bb, cc, dd, ee)
38742374Sbde#define UFS_UPDATE(aa, bb) VFSTOUFS((aa)->v_mount)->um_update(aa, bb)
38838625Srvb
38938625Srvb    missing
39038625Srvb    { &vop_reallocblks_desc,	(vop_t *) ufs_missingop },
39138625Srvb    { &vop_cachedlookup_desc,	(vop_t *) ufs_lookup },
39238625Srvb    { &vop_whiteout_desc,	(vop_t *) ufs_whiteout },
39338625Srvb#endif
39438625Srvb    { (struct vnodeop_desc*)NULL, (int(*)(void *))NULL }
39538625Srvb};
39638625Srvb
39739085Srvbstatic struct vnodeopv_desc coda_vnodeop_opv_desc =
39839085Srvb		{ &coda_vnodeop_p, coda_vnodeop_entries };
39938625Srvb
40039085SrvbVNODEOP_SET(coda_vnodeop_opv_desc);
40138625Srvb
40238625Srvb/* A generic panic: we were called with something we didn't define yet */
40338625Srvbint
40439085Srvbcoda_vop_error(void *anon) {
40538625Srvb    struct vnodeop_desc **desc = (struct vnodeop_desc **)anon;
40638625Srvb
40741202Srvb    myprintf(("coda_vop_error: Vnode operation %s called, but not defined.\n",
40838625Srvb	      (*desc)->vdesc_name));
40941202Srvb    /*
41039085Srvb    panic("coda_vop_error");
41141202Srvb    */
41241202Srvb    return EIO;
41338625Srvb}
41438625Srvb
41538625Srvb/* A generic do-nothing.  For lease_check, advlock */
41638625Srvbint
41739085Srvbcoda_vop_nop(void *anon) {
41838625Srvb    struct vnodeop_desc **desc = (struct vnodeop_desc **)anon;
41938625Srvb
42039085Srvb    if (codadebug) {
42138625Srvb	myprintf(("Vnode operation %s called, but unsupported\n",
42238625Srvb		  (*desc)->vdesc_name));
42338625Srvb    }
42438625Srvb   return (0);
42538625Srvb}
42638625Srvb
42738625Srvbint
42839085Srvbcoda_vnodeopstats_init(void)
42938625Srvb{
43038625Srvb	register int i;
43138625Srvb
43239085Srvb	for(i=0;i<CODA_VNODEOPS_SIZE;i++) {
43339085Srvb		coda_vnodeopstats[i].opcode = i;
43439085Srvb		coda_vnodeopstats[i].entries = 0;
43539085Srvb		coda_vnodeopstats[i].sat_intrn = 0;
43639085Srvb		coda_vnodeopstats[i].unsat_intrn = 0;
43739085Srvb		coda_vnodeopstats[i].gen_intrn = 0;
43838625Srvb	}
43938625Srvb	return 0;
44038625Srvb}
44138625Srvb
44238625Srvb/*
44339085Srvb * coda_open calls Venus to return the device, inode pair of the cache
44439085Srvb * file holding the data. Using iget, coda_open finds the vnode of the
44538625Srvb * cache file, and then opens it.
44638625Srvb */
44738625Srvbint
44839085Srvbcoda_open(v)
44938625Srvb    void *v;
45038625Srvb{
45138625Srvb    /*
45238625Srvb     * NetBSD can pass the O_EXCL flag in mode, even though the check
45338625Srvb     * has already happened.  Venus defensively assumes that if open
45438625Srvb     * is passed the EXCL, it must be a bug.  We strip the flag here.
45538625Srvb     */
45638625Srvb/* true args */
45738625Srvb    struct vop_open_args *ap = v;
45838625Srvb    register struct vnode **vpp = &(ap->a_vp);
45938625Srvb    struct cnode *cp = VTOC(*vpp);
46038625Srvb    int flag = ap->a_mode & (~O_EXCL);
46138625Srvb    struct ucred *cred = ap->a_cred;
46238625Srvb    struct proc *p = ap->a_p;
46338625Srvb/* locals */
46438625Srvb    int error;
46538625Srvb    struct vnode *vp;
46638625Srvb    dev_t dev;
46738625Srvb    ino_t inode;
46838625Srvb
46939085Srvb    MARK_ENTRY(CODA_OPEN_STATS);
47038625Srvb
47138625Srvb    /* Check for open of control file. */
47238625Srvb    if (IS_CTL_VP(*vpp)) {
47338625Srvb	/* XXX */
47438625Srvb	/* if (WRITEABLE(flag)) */
47538625Srvb	if (flag & (FWRITE | O_TRUNC | O_CREAT | O_EXCL)) {
47639085Srvb	    MARK_INT_FAIL(CODA_OPEN_STATS);
47738625Srvb	    return(EACCES);
47838625Srvb	}
47939085Srvb	MARK_INT_SAT(CODA_OPEN_STATS);
48038625Srvb	return(0);
48138625Srvb    }
48238625Srvb
48338625Srvb    error = venus_open(vtomi((*vpp)), &cp->c_fid, flag, cred, p, &dev, &inode);
48438625Srvb    if (error)
48538625Srvb	return (error);
48638625Srvb    if (!error) {
48739085Srvb	CODADEBUG( CODA_OPEN,myprintf(("open: dev %d inode %d result %d\n",
48838625Srvb				  dev, inode, error)); )
48938625Srvb    }
49038625Srvb
49138625Srvb    /* Translate the <device, inode> pair for the cache file into
49238625Srvb       an inode pointer. */
49339085Srvb    error = coda_grab_vnode(dev, inode, &vp);
49438625Srvb    if (error)
49538625Srvb	return (error);
49638625Srvb
49738759Srvb    /* We get the vnode back locked.  Needs unlocked */
49838759Srvb    VOP_UNLOCK(vp, 0, p);
49938625Srvb    /* Keep a reference until the close comes in. */
50038625Srvb    vref(*vpp);
50138625Srvb
50238625Srvb    /* Save the vnode pointer for the cache file. */
50338625Srvb    if (cp->c_ovp == NULL) {
50438625Srvb	cp->c_ovp = vp;
50538625Srvb    } else {
50638625Srvb	if (cp->c_ovp != vp)
50739085Srvb	    panic("coda_open:  cp->c_ovp != ITOV(ip)");
50838625Srvb    }
50938625Srvb    cp->c_ocount++;
51038625Srvb
51138625Srvb    /* Flush the attribute cached if writing the file. */
51238625Srvb    if (flag & FWRITE) {
51338625Srvb	cp->c_owrite++;
51438625Srvb	cp->c_flags &= ~C_VATTR;
51538625Srvb    }
51638625Srvb
51738625Srvb    /* Save the <device, inode> pair for the cache file to speed
51838625Srvb       up subsequent page_read's. */
51938625Srvb    cp->c_device = dev;
52038625Srvb    cp->c_inode = inode;
52138625Srvb
52238625Srvb    /* Open the cache file. */
52338625Srvb    error = VOP_OPEN(vp, flag, cred, p);
52438625Srvb    if (error) {
52539085Srvb    	printf("coda_open: VOP_OPEN on container failed %d\n", error);
52638625Srvb	return (error);
52738625Srvb    }
52839650Srvb/* grab (above) does this when it calls newvnode unless it's in the cache*/
52938625Srvb    if (vp->v_type == VREG) {
53042315Seivind    	error = vfs_object_create(vp, p, cred);
53138625Srvb	if (error != 0) {
53239085Srvb	    printf("coda_open: vfs_object_create() returns %d\n", error);
53338625Srvb	    vput(vp);
53438625Srvb	}
53538625Srvb    }
53639650Srvb
53738625Srvb    return(error);
53838625Srvb}
53938625Srvb
54038625Srvb/*
54138625Srvb * Close the cache file used for I/O and notify Venus.
54238625Srvb */
54338625Srvbint
54439085Srvbcoda_close(v)
54538625Srvb    void *v;
54638625Srvb{
54738625Srvb/* true args */
54838625Srvb    struct vop_close_args *ap = v;
54938625Srvb    struct vnode *vp = ap->a_vp;
55038625Srvb    struct cnode *cp = VTOC(vp);
55138625Srvb    int flag = ap->a_fflag;
55238625Srvb    struct ucred *cred = ap->a_cred;
55338625Srvb    struct proc *p = ap->a_p;
55438625Srvb/* locals */
55538625Srvb    int error;
55638625Srvb
55739085Srvb    MARK_ENTRY(CODA_CLOSE_STATS);
55838625Srvb
55938625Srvb    /* Check for close of control file. */
56038625Srvb    if (IS_CTL_VP(vp)) {
56139085Srvb	MARK_INT_SAT(CODA_CLOSE_STATS);
56238625Srvb	return(0);
56338625Srvb    }
56438625Srvb
56538625Srvb    if (IS_UNMOUNTING(cp)) {
56638625Srvb	if (cp->c_ovp) {
56739728Srvb#ifdef	CODA_VERBOSE
56839085Srvb	    printf("coda_close: destroying container ref %d, ufs vp %p of vp %p/cp %p\n",
56938625Srvb		    vp->v_usecount, cp->c_ovp, vp, cp);
57039650Srvb#endif
57140708Srvb#ifdef	hmm
57238625Srvb	    vgone(cp->c_ovp);
57340708Srvb#else
57440708Srvb	    VOP_CLOSE(cp->c_ovp, flag, cred, p); /* Do errors matter here? */
57540708Srvb	    vrele(cp->c_ovp);
57640708Srvb#endif
57738625Srvb	} else {
57839728Srvb#ifdef	CODA_VERBOSE
57939085Srvb	    printf("coda_close: NO container vp %p/cp %p\n", vp, cp);
58039650Srvb#endif
58138625Srvb	}
58238625Srvb	return ENODEV;
58338625Srvb    } else {
58438625Srvb	VOP_CLOSE(cp->c_ovp, flag, cred, p); /* Do errors matter here? */
58538625Srvb	vrele(cp->c_ovp);
58638625Srvb    }
58738625Srvb
58838625Srvb    if (--cp->c_ocount == 0)
58938625Srvb	cp->c_ovp = NULL;
59038625Srvb
59138625Srvb    if (flag & FWRITE)                    /* file was opened for write */
59238625Srvb	--cp->c_owrite;
59338625Srvb
59438625Srvb    error = venus_close(vtomi(vp), &cp->c_fid, flag, cred, p);
59538625Srvb    vrele(CTOV(cp));
59638625Srvb
59739085Srvb    CODADEBUG(CODA_CLOSE, myprintf(("close: result %d\n",error)); )
59838625Srvb    return(error);
59938625Srvb}
60038625Srvb
60138625Srvbint
60239085Srvbcoda_read(v)
60338625Srvb    void *v;
60438625Srvb{
60538625Srvb    struct vop_read_args *ap = v;
60638625Srvb
60738625Srvb    ENTRY;
60839085Srvb    return(coda_rdwr(ap->a_vp, ap->a_uio, UIO_READ,
60938625Srvb		    ap->a_ioflag, ap->a_cred, ap->a_uio->uio_procp));
61038625Srvb}
61138625Srvb
61238625Srvbint
61339085Srvbcoda_write(v)
61438625Srvb    void *v;
61538625Srvb{
61638625Srvb    struct vop_write_args *ap = v;
61738625Srvb
61838625Srvb    ENTRY;
61939085Srvb    return(coda_rdwr(ap->a_vp, ap->a_uio, UIO_WRITE,
62038625Srvb		    ap->a_ioflag, ap->a_cred, ap->a_uio->uio_procp));
62138625Srvb}
62238625Srvb
62338625Srvbint
62439085Srvbcoda_rdwr(vp, uiop, rw, ioflag, cred, p)
62538625Srvb    struct vnode *vp;
62638625Srvb    struct uio *uiop;
62738625Srvb    enum uio_rw rw;
62838625Srvb    int ioflag;
62938625Srvb    struct ucred *cred;
63038625Srvb    struct proc *p;
63138625Srvb{
63238625Srvb/* upcall decl */
63338625Srvb  /* NOTE: container file operation!!! */
63438625Srvb/* locals */
63538625Srvb    struct cnode *cp = VTOC(vp);
63638625Srvb    struct vnode *cfvp = cp->c_ovp;
63738625Srvb    int igot_internally = 0;
63838625Srvb    int opened_internally = 0;
63938625Srvb    int error = 0;
64038625Srvb
64139085Srvb    MARK_ENTRY(CODA_RDWR_STATS);
64238625Srvb
64339085Srvb    CODADEBUG(CODA_RDWR, myprintf(("coda_rdwr(%d, %p, %d, %qd, %d)\n", rw,
64438625Srvb			      uiop->uio_iov->iov_base, uiop->uio_resid,
64538625Srvb			      uiop->uio_offset, uiop->uio_segflg)); )
64638625Srvb
64738625Srvb    /* Check for rdwr of control object. */
64838625Srvb    if (IS_CTL_VP(vp)) {
64939085Srvb	MARK_INT_FAIL(CODA_RDWR_STATS);
65038625Srvb	return(EINVAL);
65138625Srvb    }
65238625Srvb
65338625Srvb    /*
65438625Srvb     * If file is not already open this must be a page
65538625Srvb     * {read,write} request.  Iget the cache file's inode
65638625Srvb     * pointer if we still have its <device, inode> pair.
65738625Srvb     * Otherwise, we must do an internal open to derive the
65838625Srvb     * pair.
65938625Srvb     */
66038625Srvb    if (cfvp == NULL) {
66138625Srvb	/*
66238625Srvb	 * If we're dumping core, do the internal open. Otherwise
66338625Srvb	 * venus won't have the correct size of the core when
66438625Srvb	 * it's completely written.
66538625Srvb	 */
66638625Srvb	if (cp->c_inode != 0 && !(p && (p->p_acflag & ACORE))) {
66738625Srvb	    igot_internally = 1;
66839085Srvb	    error = coda_grab_vnode(cp->c_device, cp->c_inode, &cfvp);
66938625Srvb	    if (error) {
67039085Srvb		MARK_INT_FAIL(CODA_RDWR_STATS);
67138625Srvb		return(error);
67238625Srvb	    }
67338625Srvb	    /*
67438625Srvb	     * We get the vnode back locked in both Mach and
67538625Srvb	     * NetBSD.  Needs unlocked
67638625Srvb	     */
67738759Srvb	    VOP_UNLOCK(cfvp, 0, p);
67838625Srvb	}
67938625Srvb	else {
68038625Srvb	    opened_internally = 1;
68139085Srvb	    MARK_INT_GEN(CODA_OPEN_STATS);
68238625Srvb	    error = VOP_OPEN(vp, (rw == UIO_READ ? FREAD : FWRITE),
68338625Srvb			     cred, p);
68439085Srvbprintf("coda_rdwr: Internally Opening %p\n", vp);
68538625Srvb	    if (error) {
68639085Srvb		printf("coda_rdwr: VOP_OPEN on container failed %d\n", error);
68738625Srvb		return (error);
68838625Srvb	    }
68938625Srvb	    if (vp->v_type == VREG) {
69042315Seivind		error = vfs_object_create(vp, p, cred);
69138625Srvb		if (error != 0) {
69239085Srvb		    printf("coda_rdwr: vfs_object_create() returns %d\n", error);
69338625Srvb		    vput(vp);
69438625Srvb		}
69538625Srvb	    }
69638625Srvb	    if (error) {
69739085Srvb		MARK_INT_FAIL(CODA_RDWR_STATS);
69838625Srvb		return(error);
69938625Srvb	    }
70038625Srvb	    cfvp = cp->c_ovp;
70138625Srvb	}
70238625Srvb    }
70338625Srvb
70438625Srvb    /* Have UFS handle the call. */
70539085Srvb    CODADEBUG(CODA_RDWR, myprintf(("indirect rdwr: fid = (%lx.%lx.%lx), refcnt = %d\n",
70638625Srvb			      cp->c_fid.Volume, cp->c_fid.Vnode,
70738625Srvb			      cp->c_fid.Unique, CTOV(cp)->v_usecount)); )
70838625Srvb
70939650Srvb
71038625Srvb    if (rw == UIO_READ) {
71138625Srvb	error = VOP_READ(cfvp, uiop, ioflag, cred);
71238625Srvb    } else {
71338625Srvb	error = VOP_WRITE(cfvp, uiop, ioflag, cred);
71438625Srvb	/* ufs_write updates the vnode_pager_setsize for the vnode/object */
71539650Srvb
71638625Srvb	{   struct vattr attr;
71738625Srvb
71838625Srvb	    if (VOP_GETATTR(cfvp, &attr, cred, p) == 0) {
71938625Srvb		vnode_pager_setsize(vp, attr.va_size);
72038625Srvb	    }
72138625Srvb	}
72238625Srvb    }
72338625Srvb
72438625Srvb    if (error)
72539085Srvb	MARK_INT_FAIL(CODA_RDWR_STATS);
72638625Srvb    else
72739085Srvb	MARK_INT_SAT(CODA_RDWR_STATS);
72838625Srvb
72938625Srvb    /* Do an internal close if necessary. */
73038625Srvb    if (opened_internally) {
73139085Srvb	MARK_INT_GEN(CODA_CLOSE_STATS);
73238625Srvb	(void)VOP_CLOSE(vp, (rw == UIO_READ ? FREAD : FWRITE), cred, p);
73338625Srvb    }
73438625Srvb
73538625Srvb    /* Invalidate cached attributes if writing. */
73638625Srvb    if (rw == UIO_WRITE)
73738625Srvb	cp->c_flags &= ~C_VATTR;
73838625Srvb    return(error);
73938625Srvb}
74038625Srvb
74138625Srvbint
74239085Srvbcoda_ioctl(v)
74338625Srvb    void *v;
74438625Srvb{
74538625Srvb/* true args */
74638625Srvb    struct vop_ioctl_args *ap = v;
74738625Srvb    struct vnode *vp = ap->a_vp;
74838625Srvb    int com = ap->a_command;
74938625Srvb    caddr_t data = ap->a_data;
75038625Srvb    int flag = ap->a_fflag;
75138625Srvb    struct ucred *cred = ap->a_cred;
75238625Srvb    struct proc  *p = ap->a_p;
75338625Srvb/* locals */
75438625Srvb    int error;
75538625Srvb    struct vnode *tvp;
75638625Srvb    struct nameidata ndp;
75738625Srvb    struct PioctlData *iap = (struct PioctlData *)data;
75838625Srvb
75939085Srvb    MARK_ENTRY(CODA_IOCTL_STATS);
76038625Srvb
76139085Srvb    CODADEBUG(CODA_IOCTL, myprintf(("in coda_ioctl on %s\n", iap->path));)
76238625Srvb
76338625Srvb    /* Don't check for operation on a dying object, for ctlvp it
76438625Srvb       shouldn't matter */
76538625Srvb
76638625Srvb    /* Must be control object to succeed. */
76738625Srvb    if (!IS_CTL_VP(vp)) {
76839085Srvb	MARK_INT_FAIL(CODA_IOCTL_STATS);
76939085Srvb	CODADEBUG(CODA_IOCTL, myprintf(("coda_ioctl error: vp != ctlvp"));)
77038625Srvb	    return (EOPNOTSUPP);
77138625Srvb    }
77238625Srvb    /* Look up the pathname. */
77338625Srvb
77438625Srvb    /* Should we use the name cache here? It would get it from
77538625Srvb       lookupname sooner or later anyway, right? */
77638625Srvb
77743382Sbde    NDINIT(&ndp, LOOKUP, (iap->follow ? FOLLOW : NOFOLLOW), UIO_USERSPACE, iap->path, p);
77838625Srvb    error = namei(&ndp);
77938625Srvb    tvp = ndp.ni_vp;
78038625Srvb
78138625Srvb    if (error) {
78239085Srvb	MARK_INT_FAIL(CODA_IOCTL_STATS);
78339085Srvb	CODADEBUG(CODA_IOCTL, myprintf(("coda_ioctl error: lookup returns %d\n",
78438625Srvb				   error));)
78538625Srvb	return(error);
78638625Srvb    }
78738625Srvb
78838625Srvb    /*
78938625Srvb     * Make sure this is a coda style cnode, but it may be a
79038625Srvb     * different vfsp
79138625Srvb     */
79238625Srvb    /* XXX: this totally violates the comment about vtagtype in vnode.h */
79339085Srvb    if (tvp->v_tag != VT_CODA) {
79438625Srvb	vrele(tvp);
79539085Srvb	MARK_INT_FAIL(CODA_IOCTL_STATS);
79639085Srvb	CODADEBUG(CODA_IOCTL,
79739085Srvb		 myprintf(("coda_ioctl error: %s not a coda object\n",
79838625Srvb			iap->path));)
79938625Srvb	return(EINVAL);
80038625Srvb    }
80138625Srvb
80238625Srvb    if (iap->vi.in_size > VC_MAXDATASIZE) {
80338625Srvb	vrele(tvp);
80438625Srvb	return(EINVAL);
80538625Srvb    }
80638625Srvb    error = venus_ioctl(vtomi(tvp), &((VTOC(tvp))->c_fid), com, flag, data, cred, p);
80738625Srvb
80838625Srvb    if (error)
80939085Srvb	MARK_INT_FAIL(CODA_IOCTL_STATS);
81038625Srvb    else
81139085Srvb	CODADEBUG(CODA_IOCTL, myprintf(("Ioctl returns %d \n", error)); )
81238625Srvb
81338625Srvb    vrele(tvp);
81438625Srvb    return(error);
81538625Srvb}
81638625Srvb
81738625Srvb/*
81838625Srvb * To reduce the cost of a user-level venus;we cache attributes in
81938625Srvb * the kernel.  Each cnode has storage allocated for an attribute. If
82038625Srvb * c_vattr is valid, return a reference to it. Otherwise, get the
82138625Srvb * attributes from venus and store them in the cnode.  There is some
82238625Srvb * question if this method is a security leak. But I think that in
82338625Srvb * order to make this call, the user must have done a lookup and
82438625Srvb * opened the file, and therefore should already have access.
82538625Srvb */
82638625Srvbint
82739085Srvbcoda_getattr(v)
82838625Srvb    void *v;
82938625Srvb{
83038625Srvb/* true args */
83138625Srvb    struct vop_getattr_args *ap = v;
83238625Srvb    struct vnode *vp = ap->a_vp;
83338625Srvb    struct cnode *cp = VTOC(vp);
83438625Srvb    struct vattr *vap = ap->a_vap;
83538625Srvb    struct ucred *cred = ap->a_cred;
83638625Srvb    struct proc *p = ap->a_p;
83738625Srvb/* locals */
83838625Srvb    int error;
83938625Srvb
84039085Srvb    MARK_ENTRY(CODA_GETATTR_STATS);
84138625Srvb
84238625Srvb    if (IS_UNMOUNTING(cp))
84338625Srvb	return ENODEV;
84438759Srvb
84538625Srvb    /* Check for getattr of control object. */
84638625Srvb    if (IS_CTL_VP(vp)) {
84739085Srvb	MARK_INT_FAIL(CODA_GETATTR_STATS);
84838625Srvb	return(ENOENT);
84938625Srvb    }
85038625Srvb
85138625Srvb    /* Check to see if the attributes have already been cached */
85238625Srvb    if (VALID_VATTR(cp)) {
85339085Srvb	CODADEBUG(CODA_GETATTR, { myprintf(("attr cache hit: (%lx.%lx.%lx)\n",
85438625Srvb				       cp->c_fid.Volume,
85538625Srvb				       cp->c_fid.Vnode,
85638625Srvb				       cp->c_fid.Unique));});
85739085Srvb	CODADEBUG(CODA_GETATTR, if (!(codadebug & ~CODA_GETATTR))
85838625Srvb		 print_vattr(&cp->c_vattr); );
85938625Srvb
86038625Srvb	*vap = cp->c_vattr;
86139085Srvb	MARK_INT_SAT(CODA_GETATTR_STATS);
86238625Srvb	return(0);
86338625Srvb    }
86438625Srvb
86538625Srvb    error = venus_getattr(vtomi(vp), &cp->c_fid, cred, p, vap);
86638625Srvb
86738625Srvb    if (!error) {
86839085Srvb	CODADEBUG(CODA_GETATTR, myprintf(("getattr miss (%lx.%lx.%lx): result %d\n",
86938625Srvb				     cp->c_fid.Volume,
87038625Srvb				     cp->c_fid.Vnode,
87138625Srvb				     cp->c_fid.Unique,
87238625Srvb				     error)); )
87338625Srvb
87439085Srvb	CODADEBUG(CODA_GETATTR, if (!(codadebug & ~CODA_GETATTR))
87538625Srvb		 print_vattr(vap);	);
87638625Srvb
87738625Srvb    {	int size = vap->va_size;
87838625Srvb    	struct vnode *convp = cp->c_ovp;
87938625Srvb	if (convp != (struct vnode *)0) {
88038625Srvb	    vnode_pager_setsize(convp, size);
88138625Srvb	}
88238625Srvb    }
88338625Srvb	/* If not open for write, store attributes in cnode */
88439085Srvb	if ((cp->c_owrite == 0) && (coda_attr_cache)) {
88538625Srvb	    cp->c_vattr = *vap;
88638625Srvb	    cp->c_flags |= C_VATTR;
88738625Srvb	}
88838625Srvb
88938625Srvb    }
89038625Srvb    return(error);
89138625Srvb}
89238625Srvb
89338625Srvbint
89439085Srvbcoda_setattr(v)
89538625Srvb    void *v;
89638625Srvb{
89738625Srvb/* true args */
89838625Srvb    struct vop_setattr_args *ap = v;
89938625Srvb    register struct vnode *vp = ap->a_vp;
90038625Srvb    struct cnode *cp = VTOC(vp);
90138625Srvb    register struct vattr *vap = ap->a_vap;
90238625Srvb    struct ucred *cred = ap->a_cred;
90338625Srvb    struct proc *p = ap->a_p;
90438625Srvb/* locals */
90538625Srvb    int error;
90638625Srvb
90739085Srvb    MARK_ENTRY(CODA_SETATTR_STATS);
90838625Srvb
90938625Srvb    /* Check for setattr of control object. */
91038625Srvb    if (IS_CTL_VP(vp)) {
91139085Srvb	MARK_INT_FAIL(CODA_SETATTR_STATS);
91238625Srvb	return(ENOENT);
91338625Srvb    }
91438625Srvb
91539085Srvb    if (codadebug & CODADBGMSK(CODA_SETATTR)) {
91638625Srvb	print_vattr(vap);
91738625Srvb    }
91838625Srvb    error = venus_setattr(vtomi(vp), &cp->c_fid, vap, cred, p);
91938625Srvb
92038625Srvb    if (!error)
92138625Srvb	cp->c_flags &= ~C_VATTR;
92238625Srvb
92338625Srvb    {	int size = vap->va_size;
92438625Srvb    	struct vnode *convp = cp->c_ovp;
92538625Srvb	if (size != VNOVAL && convp != (struct vnode *)0) {
92638625Srvb	    vnode_pager_setsize(convp, size);
92738625Srvb	}
92838625Srvb    }
92939085Srvb    CODADEBUG(CODA_SETATTR,	myprintf(("setattr %d\n", error)); )
93038625Srvb    return(error);
93138625Srvb}
93238625Srvb
93338625Srvbint
93439085Srvbcoda_access(v)
93538625Srvb    void *v;
93638625Srvb{
93738625Srvb/* true args */
93838625Srvb    struct vop_access_args *ap = v;
93938625Srvb    struct vnode *vp = ap->a_vp;
94038625Srvb    struct cnode *cp = VTOC(vp);
94138625Srvb    int mode = ap->a_mode;
94238625Srvb    struct ucred *cred = ap->a_cred;
94338625Srvb    struct proc *p = ap->a_p;
94438625Srvb/* locals */
94538625Srvb    int error;
94638625Srvb
94739085Srvb    MARK_ENTRY(CODA_ACCESS_STATS);
94838625Srvb
94938625Srvb    /* Check for access of control object.  Only read access is
95038625Srvb       allowed on it. */
95138625Srvb    if (IS_CTL_VP(vp)) {
95238625Srvb	/* bogus hack - all will be marked as successes */
95339085Srvb	MARK_INT_SAT(CODA_ACCESS_STATS);
95438625Srvb	return(((mode & VREAD) && !(mode & (VWRITE | VEXEC)))
95538625Srvb	       ? 0 : EACCES);
95638625Srvb    }
95738625Srvb
95838625Srvb    /*
95938625Srvb     * if the file is a directory, and we are checking exec (eg lookup)
96038625Srvb     * access, and the file is in the namecache, then the user must have
96138625Srvb     * lookup access to it.
96238625Srvb     */
96339085Srvb    if (coda_access_cache) {
96438625Srvb	if ((vp->v_type == VDIR) && (mode & VEXEC)) {
96539085Srvb	    if (coda_nc_lookup(cp, ".", 1, cred)) {
96639085Srvb		MARK_INT_SAT(CODA_ACCESS_STATS);
96738625Srvb		return(0);                     /* it was in the cache */
96838625Srvb	    }
96938625Srvb	}
97038625Srvb    }
97138625Srvb
97238625Srvb    error = venus_access(vtomi(vp), &cp->c_fid, mode, cred, p);
97338625Srvb
97438625Srvb    return(error);
97538625Srvb}
97638625Srvb
97738625Srvb/*
97839085Srvb * CODA abort op, called after namei() when a CREATE/DELETE isn't actually
97939085Srvb * done. If a buffer has been saved in anticipation of a coda_create or
98039085Srvb * a coda_remove, delete it.
98138625Srvb */
98238625Srvb/* ARGSUSED */
98338625Srvbint
98439085Srvbcoda_abortop(v)
98538625Srvb    void *v;
98638625Srvb{
98738625Srvb/* true args */
98838625Srvb    struct vop_abortop_args /* {
98938625Srvb	struct vnode *a_dvp;
99038625Srvb	struct componentname *a_cnp;
99138625Srvb    } */ *ap = v;
99238625Srvb/* upcall decl */
99338625Srvb/* locals */
99438625Srvb
99538625Srvb    if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
99638759Srvb	zfree(namei_zone, ap->a_cnp->cn_pnbuf);
99738625Srvb    return (0);
99838625Srvb}
99938625Srvb
100038625Srvbint
100139085Srvbcoda_readlink(v)
100238625Srvb    void *v;
100338625Srvb{
100438625Srvb/* true args */
100538625Srvb    struct vop_readlink_args *ap = v;
100638625Srvb    struct vnode *vp = ap->a_vp;
100738625Srvb    struct cnode *cp = VTOC(vp);
100838625Srvb    struct uio *uiop = ap->a_uio;
100938625Srvb    struct ucred *cred = ap->a_cred;
101038625Srvb    struct proc *p = ap->a_uio->uio_procp;
101138625Srvb/* locals */
101238625Srvb    int error;
101338625Srvb    char *str;
101438625Srvb    int len;
101538625Srvb
101639085Srvb    MARK_ENTRY(CODA_READLINK_STATS);
101738625Srvb
101838625Srvb    /* Check for readlink of control object. */
101938625Srvb    if (IS_CTL_VP(vp)) {
102039085Srvb	MARK_INT_FAIL(CODA_READLINK_STATS);
102138625Srvb	return(ENOENT);
102238625Srvb    }
102338625Srvb
102439085Srvb    if ((coda_symlink_cache) && (VALID_SYMLINK(cp))) { /* symlink was cached */
102538625Srvb	uiop->uio_rw = UIO_READ;
102638625Srvb	error = uiomove(cp->c_symlink, (int)cp->c_symlen, uiop);
102738625Srvb	if (error)
102839085Srvb	    MARK_INT_FAIL(CODA_READLINK_STATS);
102938625Srvb	else
103039085Srvb	    MARK_INT_SAT(CODA_READLINK_STATS);
103138625Srvb	return(error);
103238625Srvb    }
103338625Srvb
103438625Srvb    error = venus_readlink(vtomi(vp), &cp->c_fid, cred, p, &str, &len);
103538625Srvb
103638625Srvb    if (!error) {
103738625Srvb	uiop->uio_rw = UIO_READ;
103838625Srvb	error = uiomove(str, len, uiop);
103938625Srvb
104039085Srvb	if (coda_symlink_cache) {
104138625Srvb	    cp->c_symlink = str;
104238625Srvb	    cp->c_symlen = len;
104338625Srvb	    cp->c_flags |= C_SYMLINK;
104438625Srvb	} else
104539085Srvb	    CODA_FREE(str, len);
104638625Srvb    }
104738625Srvb
104839085Srvb    CODADEBUG(CODA_READLINK, myprintf(("in readlink result %d\n",error));)
104938625Srvb    return(error);
105038625Srvb}
105138625Srvb
105238625Srvbint
105339085Srvbcoda_fsync(v)
105438625Srvb    void *v;
105538625Srvb{
105638625Srvb/* true args */
105738625Srvb    struct vop_fsync_args *ap = v;
105838625Srvb    struct vnode *vp = ap->a_vp;
105938625Srvb    struct cnode *cp = VTOC(vp);
106038625Srvb    struct ucred *cred = ap->a_cred;
106138625Srvb    struct proc *p = ap->a_p;
106238625Srvb/* locals */
106338625Srvb    struct vnode *convp = cp->c_ovp;
106438625Srvb    int error;
106538625Srvb
106639085Srvb    MARK_ENTRY(CODA_FSYNC_STATS);
106738625Srvb
106838625Srvb    /* Check for fsync on an unmounting object */
106938625Srvb    /* The NetBSD kernel, in it's infinite wisdom, can try to fsync
107038625Srvb     * after an unmount has been initiated.  This is a Bad Thing,
107138625Srvb     * which we have to avoid.  Not a legitimate failure for stats.
107238625Srvb     */
107338625Srvb    if (IS_UNMOUNTING(cp)) {
107438625Srvb	return(ENODEV);
107538625Srvb    }
107638625Srvb
107738625Srvb    /* Check for fsync of control object. */
107838625Srvb    if (IS_CTL_VP(vp)) {
107939085Srvb	MARK_INT_SAT(CODA_FSYNC_STATS);
108038625Srvb	return(0);
108138625Srvb    }
108238625Srvb
108338625Srvb    if (convp)
108438625Srvb    	VOP_FSYNC(convp, cred, MNT_WAIT, p);
108538625Srvb
108638625Srvb    /*
108738625Srvb     * We see fsyncs with usecount == 1 then usecount == 0.
108838625Srvb     * For now we ignore them.
108938625Srvb     */
109038625Srvb    /*
109138625Srvb    if (!vp->v_usecount) {
109239085Srvb    	printf("coda_fsync on vnode %p with %d usecount.  c_flags = %x (%x)\n",
109338625Srvb		vp, vp->v_usecount, cp->c_flags, cp->c_flags&C_PURGING);
109438625Srvb    }
109538625Srvb    */
109638625Srvb
109738625Srvb    /*
109838625Srvb     * We can expect fsync on any vnode at all if venus is pruging it.
109938625Srvb     * Venus can't very well answer the fsync request, now can it?
110038625Srvb     * Hopefully, it won't have to, because hopefully, venus preserves
110138625Srvb     * the (possibly untrue) invariant that it never purges an open
110238625Srvb     * vnode.  Hopefully.
110338625Srvb     */
110438625Srvb    if (cp->c_flags & C_PURGING) {
110538625Srvb	return(0);
110638625Srvb    }
110738625Srvb
110838625Srvb    /* needs research */
110938625Srvb    return 0;
111038625Srvb    error = venus_fsync(vtomi(vp), &cp->c_fid, cred, p);
111138625Srvb
111239085Srvb    CODADEBUG(CODA_FSYNC, myprintf(("in fsync result %d\n",error)); );
111338625Srvb    return(error);
111438625Srvb}
111538625Srvb
111638625Srvbint
111739085Srvbcoda_inactive(v)
111838625Srvb    void *v;
111938625Srvb{
112038625Srvb    /* XXX - at the moment, inactive doesn't look at cred, and doesn't
112138625Srvb       have a proc pointer.  Oops. */
112238625Srvb/* true args */
112338625Srvb    struct vop_inactive_args *ap = v;
112438625Srvb    struct vnode *vp = ap->a_vp;
112538625Srvb    struct cnode *cp = VTOC(vp);
112638625Srvb    struct ucred *cred __attribute__((unused)) = NULL;
112738625Srvb    struct proc *p __attribute__((unused)) = curproc;
112838625Srvb/* upcall decl */
112938625Srvb/* locals */
113038625Srvb
113138625Srvb    /* We don't need to send inactive to venus - DCS */
113239085Srvb    MARK_ENTRY(CODA_INACTIVE_STATS);
113338625Srvb
113438625Srvb    if (IS_CTL_VP(vp)) {
113539085Srvb	MARK_INT_SAT(CODA_INACTIVE_STATS);
113638625Srvb	return 0;
113738625Srvb    }
113838625Srvb
113939085Srvb    CODADEBUG(CODA_INACTIVE, myprintf(("in inactive, %lx.%lx.%lx. vfsp %p\n",
114038625Srvb				  cp->c_fid.Volume, cp->c_fid.Vnode,
114138625Srvb				  cp->c_fid.Unique, vp->v_mount));)
114238625Srvb
114338625Srvb    /* If an array has been allocated to hold the symlink, deallocate it */
114439085Srvb    if ((coda_symlink_cache) && (VALID_SYMLINK(cp))) {
114538625Srvb	if (cp->c_symlink == NULL)
114639085Srvb	    panic("coda_inactive: null symlink pointer in cnode");
114738625Srvb
114839085Srvb	CODA_FREE(cp->c_symlink, cp->c_symlen);
114938625Srvb	cp->c_flags &= ~C_SYMLINK;
115038625Srvb	cp->c_symlen = 0;
115138625Srvb    }
115238625Srvb
115338625Srvb    /* Remove it from the table so it can't be found. */
115439085Srvb    coda_unsave(cp);
115539085Srvb    if ((struct coda_mntinfo *)(vp->v_mount->mnt_data) == NULL) {
115638625Srvb	myprintf(("Help! vfsp->vfs_data was NULL, but vnode %p wasn't dying\n", vp));
115739085Srvb	panic("badness in coda_inactive\n");
115838625Srvb    }
115938625Srvb
116038625Srvb    if (IS_UNMOUNTING(cp)) {
116138625Srvb#ifdef	DEBUG
116239085Srvb	printf("coda_inactive: IS_UNMOUNTING use %d: vp %p, cp %p\n", vp->v_usecount, vp, cp);
116338625Srvb	if (cp->c_ovp != NULL)
116439085Srvb	    printf("coda_inactive: cp->ovp != NULL use %d: vp %p, cp %p\n",
116538625Srvb	    	   vp->v_usecount, vp, cp);
116638625Srvb#endif
116738759Srvb	lockmgr(&cp->c_lock, LK_RELEASE, &vp->v_interlock, p);
116838625Srvb    } else {
116939650Srvb#ifdef OLD_DIAGNOSTIC
117038625Srvb	if (CTOV(cp)->v_usecount) {
117139085Srvb	    panic("coda_inactive: nonzero reference count");
117238625Srvb	}
117338625Srvb	if (cp->c_ovp != NULL) {
117439085Srvb	    panic("coda_inactive:  cp->ovp != NULL");
117538625Srvb	}
117638625Srvb#endif
117738759Srvb	VOP_UNLOCK(vp, 0, p);
117838625Srvb	vgone(vp);
117938625Srvb    }
118038625Srvb
118139085Srvb    MARK_INT_SAT(CODA_INACTIVE_STATS);
118238625Srvb    return(0);
118338625Srvb}
118438625Srvb
118538625Srvb/*
118638625Srvb * Remote file system operations having to do with directory manipulation.
118738625Srvb */
118838625Srvb
118938625Srvb/*
119038625Srvb * It appears that in NetBSD, lookup is supposed to return the vnode locked
119138625Srvb */
119238625Srvbint
119339085Srvbcoda_lookup(v)
119438625Srvb    void *v;
119538625Srvb{
119638625Srvb/* true args */
119738625Srvb    struct vop_lookup_args *ap = v;
119838625Srvb    struct vnode *dvp = ap->a_dvp;
119938625Srvb    struct cnode *dcp = VTOC(dvp);
120038625Srvb    struct vnode **vpp = ap->a_vpp;
120138625Srvb    /*
120238625Srvb     * It looks as though ap->a_cnp->ni_cnd->cn_nameptr holds the rest
120338625Srvb     * of the string to xlate, and that we must try to get at least
120438625Srvb     * ap->a_cnp->ni_cnd->cn_namelen of those characters to macth.  I
120538625Srvb     * could be wrong.
120638625Srvb     */
120738625Srvb    struct componentname  *cnp = ap->a_cnp;
120838625Srvb    struct ucred *cred = cnp->cn_cred;
120938625Srvb    struct proc *p = cnp->cn_proc;
121038625Srvb/* locals */
121138625Srvb    struct cnode *cp;
121238625Srvb    const char *nm = cnp->cn_nameptr;
121338625Srvb    int len = cnp->cn_namelen;
121438625Srvb    ViceFid VFid;
121538625Srvb    int	vtype;
121638625Srvb    int error = 0;
121738625Srvb
121839085Srvb    MARK_ENTRY(CODA_LOOKUP_STATS);
121938625Srvb
122039085Srvb    CODADEBUG(CODA_LOOKUP, myprintf(("lookup: %s in %lx.%lx.%lx\n",
122138625Srvb				   nm, dcp->c_fid.Volume,
122238625Srvb				   dcp->c_fid.Vnode, dcp->c_fid.Unique)););
122338625Srvb
122438625Srvb    /* Check for lookup of control object. */
122538625Srvb    if (IS_CTL_NAME(dvp, nm, len)) {
122639085Srvb	*vpp = coda_ctlvp;
122738625Srvb	vref(*vpp);
122839085Srvb	MARK_INT_SAT(CODA_LOOKUP_STATS);
122938625Srvb	goto exit;
123038625Srvb    }
123138625Srvb
123239085Srvb    if (len+1 > CODA_MAXNAMLEN) {
123339085Srvb	MARK_INT_FAIL(CODA_LOOKUP_STATS);
123439085Srvb	CODADEBUG(CODA_LOOKUP, myprintf(("name too long: lookup, %lx.%lx.%lx(%s)\n",
123538625Srvb				    dcp->c_fid.Volume, dcp->c_fid.Vnode,
123638625Srvb				    dcp->c_fid.Unique, nm)););
123738625Srvb	*vpp = (struct vnode *)0;
123838625Srvb	error = EINVAL;
123938625Srvb	goto exit;
124038625Srvb    }
124138625Srvb    /* First try to look the file up in the cfs name cache */
124238625Srvb    /* lock the parent vnode? */
124339085Srvb    cp = coda_nc_lookup(dcp, nm, len, cred);
124438625Srvb    if (cp) {
124538625Srvb	*vpp = CTOV(cp);
124638625Srvb	vref(*vpp);
124739085Srvb	CODADEBUG(CODA_LOOKUP,
124838625Srvb		 myprintf(("lookup result %d vpp %p\n",error,*vpp));)
124938625Srvb    } else {
125038625Srvb
125138625Srvb	/* The name wasn't cached, so we need to contact Venus */
125238625Srvb	error = venus_lookup(vtomi(dvp), &dcp->c_fid, nm, len, cred, p, &VFid, &vtype);
125338625Srvb
125438625Srvb	if (error) {
125539085Srvb	    MARK_INT_FAIL(CODA_LOOKUP_STATS);
125639085Srvb	    CODADEBUG(CODA_LOOKUP, myprintf(("lookup error on %lx.%lx.%lx(%s)%d\n",
125738625Srvb					dcp->c_fid.Volume, dcp->c_fid.Vnode, dcp->c_fid.Unique, nm, error));)
125838625Srvb	    *vpp = (struct vnode *)0;
125938625Srvb	} else {
126039085Srvb	    MARK_INT_SAT(CODA_LOOKUP_STATS);
126139085Srvb	    CODADEBUG(CODA_LOOKUP,
126238625Srvb		     myprintf(("lookup: vol %lx vno %lx uni %lx type %o result %d\n",
126338625Srvb			    VFid.Volume, VFid.Vnode, VFid.Unique, vtype,
126438625Srvb			    error)); )
126538625Srvb
126639085Srvb	    cp = make_coda_node(&VFid, dvp->v_mount, vtype);
126738625Srvb	    *vpp = CTOV(cp);
126838625Srvb
126938625Srvb	    /* enter the new vnode in the Name Cache only if the top bit isn't set */
127038625Srvb	    /* And don't enter a new vnode for an invalid one! */
127139085Srvb	    if (!(vtype & CODA_NOCACHE))
127239085Srvb		coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
127338625Srvb	}
127438625Srvb    }
127538625Srvb
127638625Srvb exit:
127738625Srvb    /*
127838625Srvb     * If we are creating, and this was the last name to be looked up,
127938625Srvb     * and the error was ENOENT, then there really shouldn't be an
128038625Srvb     * error and we can make the leaf NULL and return success.  Since
128138625Srvb     * this is supposed to work under Mach as well as NetBSD, we're
128238625Srvb     * leaving this fn wrapped.  We also must tell lookup/namei that
128338625Srvb     * we need to save the last component of the name.  (Create will
128438625Srvb     * have to free the name buffer later...lucky us...)
128538625Srvb     */
128638625Srvb    if (((cnp->cn_nameiop == CREATE) || (cnp->cn_nameiop == RENAME))
128738625Srvb	&& (cnp->cn_flags & ISLASTCN)
128838625Srvb	&& (error == ENOENT))
128938625Srvb    {
129038625Srvb	error = EJUSTRETURN;
129138625Srvb	cnp->cn_flags |= SAVENAME;
129238625Srvb	*ap->a_vpp = NULL;
129338625Srvb    }
129438625Srvb
129538625Srvb    /*
129638625Srvb     * If we are removing, and we are at the last element, and we
129738625Srvb     * found it, then we need to keep the name around so that the
129838625Srvb     * removal will go ahead as planned.  Unfortunately, this will
129938625Srvb     * probably also lock the to-be-removed vnode, which may or may
130038625Srvb     * not be a good idea.  I'll have to look at the bits of
130139085Srvb     * coda_remove to make sure.  We'll only save the name if we did in
130239085Srvb     * fact find the name, otherwise coda_remove won't have a chance
130338625Srvb     * to free the pathname.
130438625Srvb     */
130538625Srvb    if ((cnp->cn_nameiop == DELETE)
130638625Srvb	&& (cnp->cn_flags & ISLASTCN)
130738625Srvb	&& !error)
130838625Srvb    {
130938625Srvb	cnp->cn_flags |= SAVENAME;
131038625Srvb    }
131138625Srvb
131238625Srvb    /*
131338625Srvb     * If the lookup went well, we need to (potentially?) unlock the
131438625Srvb     * parent, and lock the child.  We are only responsible for
131538625Srvb     * checking to see if the parent is supposed to be unlocked before
131638625Srvb     * we return.  We must always lock the child (provided there is
131738625Srvb     * one, and (the parent isn't locked or it isn't the same as the
131838625Srvb     * parent.)  Simple, huh?  We can never leave the parent locked unless
131938625Srvb     * we are ISLASTCN
132038625Srvb     */
132138625Srvb    if (!error || (error == EJUSTRETURN)) {
132238625Srvb	if (!(cnp->cn_flags & LOCKPARENT) || !(cnp->cn_flags & ISLASTCN)) {
132338759Srvb	    if ((error = VOP_UNLOCK(dvp, 0, p))) {
132438625Srvb		return error;
132538625Srvb	    }
132638625Srvb	    /*
132738625Srvb	     * The parent is unlocked.  As long as there is a child,
132838625Srvb	     * lock it without bothering to check anything else.
132938625Srvb	     */
133038625Srvb	    if (*ap->a_vpp) {
133138759Srvb		if ((error = VOP_LOCK(*ap->a_vpp, LK_EXCLUSIVE, p))) {
133239085Srvb		    printf("coda_lookup: ");
133338625Srvb		    panic("unlocked parent but couldn't lock child");
133438625Srvb		}
133538625Srvb	    }
133638625Srvb	} else {
133738625Srvb	    /* The parent is locked, and may be the same as the child */
133838625Srvb	    if (*ap->a_vpp && (*ap->a_vpp != dvp)) {
133938625Srvb		/* Different, go ahead and lock it. */
134038759Srvb		if ((error = VOP_LOCK(*ap->a_vpp, LK_EXCLUSIVE, p))) {
134139085Srvb		    printf("coda_lookup: ");
134238625Srvb		    panic("unlocked parent but couldn't lock child");
134338625Srvb		}
134438625Srvb	    }
134538625Srvb	}
134638625Srvb    } else {
134738625Srvb	/* If the lookup failed, we need to ensure that the leaf is NULL */
134838625Srvb	/* Don't change any locking? */
134938625Srvb	*ap->a_vpp = NULL;
135038625Srvb    }
135138625Srvb    return(error);
135238625Srvb}
135338625Srvb
135438625Srvb/*ARGSUSED*/
135538625Srvbint
135639085Srvbcoda_create(v)
135738625Srvb    void *v;
135838625Srvb{
135938625Srvb/* true args */
136038625Srvb    struct vop_create_args *ap = v;
136138625Srvb    struct vnode *dvp = ap->a_dvp;
136238625Srvb    struct cnode *dcp = VTOC(dvp);
136338625Srvb    struct vattr *va = ap->a_vap;
136438625Srvb    int exclusive = 1;
136538625Srvb    int mode = ap->a_vap->va_mode;
136638625Srvb    struct vnode **vpp = ap->a_vpp;
136738625Srvb    struct componentname  *cnp = ap->a_cnp;
136838625Srvb    struct ucred *cred = cnp->cn_cred;
136938625Srvb    struct proc *p = cnp->cn_proc;
137038625Srvb/* locals */
137138625Srvb    int error;
137238625Srvb    struct cnode *cp;
137338625Srvb    const char *nm = cnp->cn_nameptr;
137438625Srvb    int len = cnp->cn_namelen;
137538625Srvb    ViceFid VFid;
137638625Srvb    struct vattr attr;
137738625Srvb
137839085Srvb    MARK_ENTRY(CODA_CREATE_STATS);
137938625Srvb
138038625Srvb    /* All creates are exclusive XXX */
138138625Srvb    /* I'm assuming the 'mode' argument is the file mode bits XXX */
138238625Srvb
138338625Srvb    /* Check for create of control object. */
138438625Srvb    if (IS_CTL_NAME(dvp, nm, len)) {
138538625Srvb	*vpp = (struct vnode *)0;
138639085Srvb	MARK_INT_FAIL(CODA_CREATE_STATS);
138738625Srvb	return(EACCES);
138838625Srvb    }
138938625Srvb
139038625Srvb    error = venus_create(vtomi(dvp), &dcp->c_fid, nm, len, exclusive, mode, va, cred, p, &VFid, &attr);
139138625Srvb
139238625Srvb    if (!error) {
139338625Srvb
139438625Srvb	/* If this is an exclusive create, panic if the file already exists. */
139538625Srvb	/* Venus should have detected the file and reported EEXIST. */
139638625Srvb
139738625Srvb	if ((exclusive == 1) &&
139839085Srvb	    (coda_find(&VFid) != NULL))
139938625Srvb	    panic("cnode existed for newly created file!");
140038625Srvb
140139085Srvb	cp = make_coda_node(&VFid, dvp->v_mount, attr.va_type);
140238625Srvb	*vpp = CTOV(cp);
140338625Srvb
140438625Srvb	/* Update va to reflect the new attributes. */
140538625Srvb	(*va) = attr;
140638625Srvb
140738625Srvb	/* Update the attribute cache and mark it as valid */
140839085Srvb	if (coda_attr_cache) {
140938625Srvb	    VTOC(*vpp)->c_vattr = attr;
141038625Srvb	    VTOC(*vpp)->c_flags |= C_VATTR;
141138625Srvb	}
141238625Srvb
141338625Srvb	/* Invalidate the parent's attr cache, the modification time has changed */
141438625Srvb	VTOC(dvp)->c_flags &= ~C_VATTR;
141538625Srvb
141638625Srvb	/* enter the new vnode in the Name Cache */
141739085Srvb	coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
141838625Srvb
141939085Srvb	CODADEBUG(CODA_CREATE,
142038625Srvb		 myprintf(("create: (%lx.%lx.%lx), result %d\n",
142138625Srvb			VFid.Volume, VFid.Vnode, VFid.Unique, error)); )
142238625Srvb    } else {
142338625Srvb	*vpp = (struct vnode *)0;
142439085Srvb	CODADEBUG(CODA_CREATE, myprintf(("create error %d\n", error));)
142538625Srvb    }
142638625Srvb
142738625Srvb    if (!error) {
142838625Srvb	if (cnp->cn_flags & LOCKLEAF) {
142938759Srvb	    if ((error = VOP_LOCK(*ap->a_vpp, LK_EXCLUSIVE, p))) {
143039085Srvb		printf("coda_create: ");
143138625Srvb		panic("unlocked parent but couldn't lock child");
143238625Srvb	    }
143338625Srvb	}
143439650Srvb#ifdef OLD_DIAGNOSTIC
143538625Srvb	else {
143639085Srvb	    printf("coda_create: LOCKLEAF not set!\n");
143738625Srvb	}
143839650Srvb#endif
143938625Srvb    }
144038625Srvb    /* Have to free the previously saved name */
144138625Srvb    /*
144238625Srvb     * This condition is stolen from ufs_makeinode.  I have no idea
144338625Srvb     * why it's here, but what the hey...
144438625Srvb     */
144538625Srvb    if ((cnp->cn_flags & SAVESTART) == 0) {
144638759Srvb	zfree(namei_zone, cnp->cn_pnbuf);
144738625Srvb    }
144838625Srvb    return(error);
144938625Srvb}
145038625Srvb
145138625Srvbint
145239085Srvbcoda_remove(v)
145338625Srvb    void *v;
145438625Srvb{
145538625Srvb/* true args */
145638625Srvb    struct vop_remove_args *ap = v;
145738625Srvb    struct vnode *dvp = ap->a_dvp;
145838625Srvb    struct cnode *cp = VTOC(dvp);
145938625Srvb    struct componentname  *cnp = ap->a_cnp;
146038625Srvb    struct ucred *cred = cnp->cn_cred;
146138625Srvb    struct proc *p = cnp->cn_proc;
146238625Srvb/* locals */
146338625Srvb    int error;
146438625Srvb    const char *nm = cnp->cn_nameptr;
146538625Srvb    int len = cnp->cn_namelen;
146638625Srvb    struct cnode *tp;
146738625Srvb
146839085Srvb    MARK_ENTRY(CODA_REMOVE_STATS);
146938625Srvb
147039085Srvb    CODADEBUG(CODA_REMOVE, myprintf(("remove: %s in %lx.%lx.%lx\n",
147138625Srvb				   nm, cp->c_fid.Volume, cp->c_fid.Vnode,
147238625Srvb				   cp->c_fid.Unique)););
147338625Srvb
147439085Srvb    /* Remove the file's entry from the CODA Name Cache */
147538625Srvb    /* We're being conservative here, it might be that this person
147638625Srvb     * doesn't really have sufficient access to delete the file
147738625Srvb     * but we feel zapping the entry won't really hurt anyone -- dcs
147838625Srvb     */
147938625Srvb    /* I'm gonna go out on a limb here. If a file and a hardlink to it
148038625Srvb     * exist, and one is removed, the link count on the other will be
148138625Srvb     * off by 1. We could either invalidate the attrs if cached, or
148238625Srvb     * fix them. I'll try to fix them. DCS 11/8/94
148338625Srvb     */
148439085Srvb    tp = coda_nc_lookup(VTOC(dvp), nm, len, cred);
148538625Srvb    if (tp) {
148638625Srvb	if (VALID_VATTR(tp)) {	/* If attrs are cached */
148738625Srvb	    if (tp->c_vattr.va_nlink > 1) {	/* If it's a hard link */
148838625Srvb		tp->c_vattr.va_nlink--;
148938625Srvb	    }
149038625Srvb	}
149138625Srvb
149239085Srvb	coda_nc_zapfile(VTOC(dvp), nm, len);
149338625Srvb	/* No need to flush it if it doesn't exist! */
149438625Srvb    }
149538625Srvb    /* Invalidate the parent's attr cache, the modification time has changed */
149638625Srvb    VTOC(dvp)->c_flags &= ~C_VATTR;
149738625Srvb
149838625Srvb    /* Check for remove of control object. */
149938625Srvb    if (IS_CTL_NAME(dvp, nm, len)) {
150039085Srvb	MARK_INT_FAIL(CODA_REMOVE_STATS);
150138625Srvb	return(ENOENT);
150238625Srvb    }
150338625Srvb
150438625Srvb    error = venus_remove(vtomi(dvp), &cp->c_fid, nm, len, cred, p);
150538625Srvb
150639085Srvb    CODADEBUG(CODA_REMOVE, myprintf(("in remove result %d\n",error)); )
150738625Srvb
150838625Srvb    if ((cnp->cn_flags & SAVESTART) == 0) {
150938759Srvb	zfree(namei_zone, cnp->cn_pnbuf);
151038625Srvb    }
151138625Srvb    return(error);
151238625Srvb}
151338625Srvb
151438625Srvbint
151539085Srvbcoda_link(v)
151638625Srvb    void *v;
151738625Srvb{
151838625Srvb/* true args */
151938625Srvb    struct vop_link_args *ap = v;
152038625Srvb    struct vnode *vp = ap->a_vp;
152138625Srvb    struct cnode *cp = VTOC(vp);
152238625Srvb    struct vnode *tdvp = ap->a_tdvp;
152338625Srvb    struct cnode *tdcp = VTOC(tdvp);
152438625Srvb    struct componentname *cnp = ap->a_cnp;
152538625Srvb    struct ucred *cred = cnp->cn_cred;
152638625Srvb    struct proc *p = cnp->cn_proc;
152738625Srvb/* locals */
152838625Srvb    int error;
152938625Srvb    const char *nm = cnp->cn_nameptr;
153038625Srvb    int len = cnp->cn_namelen;
153138625Srvb
153239085Srvb    MARK_ENTRY(CODA_LINK_STATS);
153338625Srvb
153439085Srvb    if (codadebug & CODADBGMSK(CODA_LINK)) {
153538625Srvb
153638625Srvb	myprintf(("nb_link:   vp fid: (%lx.%lx.%lx)\n",
153738625Srvb		  cp->c_fid.Volume, cp->c_fid.Vnode, cp->c_fid.Unique));
153838625Srvb	myprintf(("nb_link: tdvp fid: (%lx.%lx.%lx)\n",
153938625Srvb		  tdcp->c_fid.Volume, tdcp->c_fid.Vnode, tdcp->c_fid.Unique));
154038625Srvb
154138625Srvb    }
154239085Srvb    if (codadebug & CODADBGMSK(CODA_LINK)) {
154338625Srvb	myprintf(("link:   vp fid: (%lx.%lx.%lx)\n",
154438625Srvb		  cp->c_fid.Volume, cp->c_fid.Vnode, cp->c_fid.Unique));
154538625Srvb	myprintf(("link: tdvp fid: (%lx.%lx.%lx)\n",
154638625Srvb		  tdcp->c_fid.Volume, tdcp->c_fid.Vnode, tdcp->c_fid.Unique));
154738625Srvb
154838625Srvb    }
154938625Srvb
155038625Srvb    /* Check for link to/from control object. */
155138625Srvb    if (IS_CTL_NAME(tdvp, nm, len) || IS_CTL_VP(vp)) {
155239085Srvb	MARK_INT_FAIL(CODA_LINK_STATS);
155338625Srvb	return(EACCES);
155438625Srvb    }
155538625Srvb
155638625Srvb    error = venus_link(vtomi(vp), &cp->c_fid, &tdcp->c_fid, nm, len, cred, p);
155738625Srvb
155838625Srvb    /* Invalidate the parent's attr cache, the modification time has changed */
155938625Srvb    VTOC(tdvp)->c_flags &= ~C_VATTR;
156038625Srvb    VTOC(vp)->c_flags &= ~C_VATTR;
156138625Srvb
156239085Srvb    CODADEBUG(CODA_LINK,	myprintf(("in link result %d\n",error)); )
156338625Srvb
156438625Srvb    /* Drop the name buffer if we don't need to SAVESTART */
156538625Srvb    if ((cnp->cn_flags & SAVESTART) == 0) {
156638759Srvb	zfree(namei_zone, cnp->cn_pnbuf);
156738625Srvb    }
156838625Srvb    return(error);
156938625Srvb}
157038625Srvb
157138625Srvbint
157239085Srvbcoda_rename(v)
157338625Srvb    void *v;
157438625Srvb{
157538625Srvb/* true args */
157638625Srvb    struct vop_rename_args *ap = v;
157738625Srvb    struct vnode *odvp = ap->a_fdvp;
157838625Srvb    struct cnode *odcp = VTOC(odvp);
157938625Srvb    struct componentname  *fcnp = ap->a_fcnp;
158038625Srvb    struct vnode *ndvp = ap->a_tdvp;
158138625Srvb    struct cnode *ndcp = VTOC(ndvp);
158238625Srvb    struct componentname  *tcnp = ap->a_tcnp;
158338625Srvb    struct ucred *cred = fcnp->cn_cred;
158438625Srvb    struct proc *p = fcnp->cn_proc;
158538625Srvb/* true args */
158638625Srvb    int error;
158738625Srvb    const char *fnm = fcnp->cn_nameptr;
158838625Srvb    int flen = fcnp->cn_namelen;
158938625Srvb    const char *tnm = tcnp->cn_nameptr;
159038625Srvb    int tlen = tcnp->cn_namelen;
159138625Srvb
159239085Srvb    MARK_ENTRY(CODA_RENAME_STATS);
159338625Srvb
159438625Srvb    /* Hmmm.  The vnodes are already looked up.  Perhaps they are locked?
159538625Srvb       This could be Bad. XXX */
159639650Srvb#ifdef OLD_DIAGNOSTIC
159738625Srvb    if ((fcnp->cn_cred != tcnp->cn_cred)
159838625Srvb	|| (fcnp->cn_proc != tcnp->cn_proc))
159938625Srvb    {
160039085Srvb	panic("coda_rename: component names don't agree");
160138625Srvb    }
160239650Srvb#endif
160338625Srvb
160438625Srvb    /* Check for rename involving control object. */
160538625Srvb    if (IS_CTL_NAME(odvp, fnm, flen) || IS_CTL_NAME(ndvp, tnm, tlen)) {
160639085Srvb	MARK_INT_FAIL(CODA_RENAME_STATS);
160738625Srvb	return(EACCES);
160838625Srvb    }
160938625Srvb
161038625Srvb    /* Problem with moving directories -- need to flush entry for .. */
161138625Srvb    if (odvp != ndvp) {
161239085Srvb	struct cnode *ovcp = coda_nc_lookup(VTOC(odvp), fnm, flen, cred);
161338625Srvb	if (ovcp) {
161438625Srvb	    struct vnode *ovp = CTOV(ovcp);
161538625Srvb	    if ((ovp) &&
161638625Srvb		(ovp->v_type == VDIR)) /* If it's a directory */
161739085Srvb		coda_nc_zapfile(VTOC(ovp),"..", 2);
161838625Srvb	}
161938625Srvb    }
162038625Srvb
162138625Srvb    /* Remove the entries for both source and target files */
162239085Srvb    coda_nc_zapfile(VTOC(odvp), fnm, flen);
162339085Srvb    coda_nc_zapfile(VTOC(ndvp), tnm, tlen);
162438625Srvb
162538625Srvb    /* Invalidate the parent's attr cache, the modification time has changed */
162638625Srvb    VTOC(odvp)->c_flags &= ~C_VATTR;
162738625Srvb    VTOC(ndvp)->c_flags &= ~C_VATTR;
162838625Srvb
162939085Srvb    if (flen+1 > CODA_MAXNAMLEN) {
163039085Srvb	MARK_INT_FAIL(CODA_RENAME_STATS);
163138625Srvb	error = EINVAL;
163238625Srvb	goto exit;
163338625Srvb    }
163438625Srvb
163539085Srvb    if (tlen+1 > CODA_MAXNAMLEN) {
163639085Srvb	MARK_INT_FAIL(CODA_RENAME_STATS);
163738625Srvb	error = EINVAL;
163838625Srvb	goto exit;
163938625Srvb    }
164038625Srvb
164138625Srvb    error = venus_rename(vtomi(odvp), &odcp->c_fid, &ndcp->c_fid, fnm, flen, tnm, tlen, cred, p);
164238625Srvb
164338625Srvb exit:
164439085Srvb    CODADEBUG(CODA_RENAME, myprintf(("in rename result %d\n",error));)
164538625Srvb    /* XXX - do we need to call cache pureg on the moved vnode? */
164638625Srvb    cache_purge(ap->a_fvp);
164738625Srvb
164838625Srvb    /* It seems to be incumbent on us to drop locks on all four vnodes */
164938625Srvb    /* From-vnodes are not locked, only ref'd.  To-vnodes are locked. */
165038625Srvb
165138625Srvb    vrele(ap->a_fvp);
165238625Srvb    vrele(odvp);
165338625Srvb
165438625Srvb    if (ap->a_tvp) {
165538625Srvb	if (ap->a_tvp == ndvp) {
165638625Srvb	    vrele(ap->a_tvp);
165738625Srvb	} else {
165838625Srvb	    vput(ap->a_tvp);
165938625Srvb	}
166038625Srvb    }
166138625Srvb
166238625Srvb    vput(ndvp);
166338625Srvb    return(error);
166438625Srvb}
166538625Srvb
166638625Srvbint
166739085Srvbcoda_mkdir(v)
166838625Srvb    void *v;
166938625Srvb{
167038625Srvb/* true args */
167138625Srvb    struct vop_mkdir_args *ap = v;
167238625Srvb    struct vnode *dvp = ap->a_dvp;
167338625Srvb    struct cnode *dcp = VTOC(dvp);
167438625Srvb    struct componentname  *cnp = ap->a_cnp;
167538625Srvb    register struct vattr *va = ap->a_vap;
167638625Srvb    struct vnode **vpp = ap->a_vpp;
167738625Srvb    struct ucred *cred = cnp->cn_cred;
167838625Srvb    struct proc *p = cnp->cn_proc;
167938625Srvb/* locals */
168038625Srvb    int error;
168138625Srvb    const char *nm = cnp->cn_nameptr;
168238625Srvb    int len = cnp->cn_namelen;
168338625Srvb    struct cnode *cp;
168438625Srvb    ViceFid VFid;
168538625Srvb    struct vattr ova;
168638625Srvb
168739085Srvb    MARK_ENTRY(CODA_MKDIR_STATS);
168838625Srvb
168938625Srvb    /* Check for mkdir of target object. */
169038625Srvb    if (IS_CTL_NAME(dvp, nm, len)) {
169138625Srvb	*vpp = (struct vnode *)0;
169239085Srvb	MARK_INT_FAIL(CODA_MKDIR_STATS);
169338625Srvb	return(EACCES);
169438625Srvb    }
169538625Srvb
169639085Srvb    if (len+1 > CODA_MAXNAMLEN) {
169738625Srvb	*vpp = (struct vnode *)0;
169839085Srvb	MARK_INT_FAIL(CODA_MKDIR_STATS);
169938625Srvb	return(EACCES);
170038625Srvb    }
170138625Srvb
170238625Srvb    error = venus_mkdir(vtomi(dvp), &dcp->c_fid, nm, len, va, cred, p, &VFid, &ova);
170338625Srvb
170438625Srvb    if (!error) {
170539085Srvb	if (coda_find(&VFid) != NULL)
170638625Srvb	    panic("cnode existed for newly created directory!");
170738625Srvb
170838625Srvb
170939085Srvb	cp =  make_coda_node(&VFid, dvp->v_mount, va->va_type);
171038625Srvb	*vpp = CTOV(cp);
171138625Srvb
171238625Srvb	/* enter the new vnode in the Name Cache */
171339085Srvb	coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
171438625Srvb
171538625Srvb	/* as a side effect, enter "." and ".." for the directory */
171639085Srvb	coda_nc_enter(VTOC(*vpp), ".", 1, cred, VTOC(*vpp));
171739085Srvb	coda_nc_enter(VTOC(*vpp), "..", 2, cred, VTOC(dvp));
171838625Srvb
171939085Srvb	if (coda_attr_cache) {
172038625Srvb	    VTOC(*vpp)->c_vattr = ova;		/* update the attr cache */
172138625Srvb	    VTOC(*vpp)->c_flags |= C_VATTR;	/* Valid attributes in cnode */
172238625Srvb	}
172338625Srvb
172438625Srvb	/* Invalidate the parent's attr cache, the modification time has changed */
172538625Srvb	VTOC(dvp)->c_flags &= ~C_VATTR;
172638625Srvb
172739085Srvb	CODADEBUG( CODA_MKDIR, myprintf(("mkdir: (%lx.%lx.%lx) result %d\n",
172838625Srvb				    VFid.Volume, VFid.Vnode, VFid.Unique, error)); )
172938625Srvb    } else {
173038625Srvb	*vpp = (struct vnode *)0;
173139085Srvb	CODADEBUG(CODA_MKDIR, myprintf(("mkdir error %d\n",error));)
173238625Srvb    }
173338625Srvb
173438625Srvb    /* Have to free the previously saved name */
173538625Srvb    /*
173638625Srvb     * ufs_mkdir doesn't check for SAVESTART before freeing the
173738625Srvb     * pathname buffer, but ufs_create does.  For the moment, I'll
173838625Srvb     * follow their lead, but this seems like it is probably
173938625Srvb     * incorrect.
174038625Srvb     */
174138759Srvb    zfree(namei_zone, cnp->cn_pnbuf);
174238625Srvb    return(error);
174338625Srvb}
174438625Srvb
174538625Srvbint
174639085Srvbcoda_rmdir(v)
174738625Srvb    void *v;
174838625Srvb{
174938625Srvb/* true args */
175038625Srvb    struct vop_rmdir_args *ap = v;
175138625Srvb    struct vnode *dvp = ap->a_dvp;
175238625Srvb    struct cnode *dcp = VTOC(dvp);
175338625Srvb    struct componentname  *cnp = ap->a_cnp;
175438625Srvb    struct ucred *cred = cnp->cn_cred;
175538625Srvb    struct proc *p = cnp->cn_proc;
175638625Srvb/* true args */
175738625Srvb    int error;
175838625Srvb    const char *nm = cnp->cn_nameptr;
175938625Srvb    int len = cnp->cn_namelen;
176038625Srvb    struct cnode *cp;
176138625Srvb
176239085Srvb    MARK_ENTRY(CODA_RMDIR_STATS);
176338625Srvb
176438625Srvb    /* Check for rmdir of control object. */
176538625Srvb    if (IS_CTL_NAME(dvp, nm, len)) {
176639085Srvb	MARK_INT_FAIL(CODA_RMDIR_STATS);
176738625Srvb	return(ENOENT);
176838625Srvb    }
176938625Srvb
177038625Srvb    /* We're being conservative here, it might be that this person
177138625Srvb     * doesn't really have sufficient access to delete the file
177238625Srvb     * but we feel zapping the entry won't really hurt anyone -- dcs
177338625Srvb     */
177438625Srvb    /*
177538625Srvb     * As a side effect of the rmdir, remove any entries for children of
177638625Srvb     * the directory, especially "." and "..".
177738625Srvb     */
177839085Srvb    cp = coda_nc_lookup(dcp, nm, len, cred);
177939085Srvb    if (cp) coda_nc_zapParentfid(&(cp->c_fid), NOT_DOWNCALL);
178038625Srvb
178139085Srvb    /* Remove the file's entry from the CODA Name Cache */
178239085Srvb    coda_nc_zapfile(dcp, nm, len);
178338625Srvb
178438625Srvb    /* Invalidate the parent's attr cache, the modification time has changed */
178538625Srvb    dcp->c_flags &= ~C_VATTR;
178638625Srvb
178738625Srvb    error = venus_rmdir(vtomi(dvp), &dcp->c_fid, nm, len, cred, p);
178838625Srvb
178939085Srvb    CODADEBUG(CODA_RMDIR, myprintf(("in rmdir result %d\n", error)); )
179038625Srvb
179138625Srvb    if ((cnp->cn_flags & SAVESTART) == 0) {
179238759Srvb	zfree(namei_zone, cnp->cn_pnbuf);
179338625Srvb    }
179438625Srvb    return(error);
179538625Srvb}
179638625Srvb
179738625Srvbint
179839085Srvbcoda_symlink(v)
179938625Srvb    void *v;
180038625Srvb{
180138625Srvb/* true args */
180238625Srvb    struct vop_symlink_args *ap = v;
180338625Srvb    struct vnode *tdvp = ap->a_dvp;
180438625Srvb    struct cnode *tdcp = VTOC(tdvp);
180538625Srvb    struct componentname *cnp = ap->a_cnp;
180638625Srvb    struct vattr *tva = ap->a_vap;
180738625Srvb    char *path = ap->a_target;
180838625Srvb    struct ucred *cred = cnp->cn_cred;
180938625Srvb    struct proc *p = cnp->cn_proc;
181038625Srvb/* locals */
181138625Srvb    int error;
181238625Srvb    /*
181339085Srvb     * XXX I'm assuming the following things about coda_symlink's
181438625Srvb     * arguments:
181538625Srvb     *       t(foo) is the new name/parent/etc being created.
181638625Srvb     *       lname is the contents of the new symlink.
181738625Srvb     */
181838759Srvb    char *nm = cnp->cn_nameptr;
181938625Srvb    int len = cnp->cn_namelen;
182038625Srvb    int plen = strlen(path);
182138625Srvb
182238625Srvb    /* XXX What about the vpp argument?  Do we need it? */
182338625Srvb    /*
182438625Srvb     * Here's the strategy for the moment: perform the symlink, then
182538625Srvb     * do a lookup to grab the resulting vnode.  I know this requires
182638625Srvb     * two communications with Venus for a new sybolic link, but
182738625Srvb     * that's the way the ball bounces.  I don't yet want to change
182838625Srvb     * the way the Mach symlink works.  When Mach support is
182938625Srvb     * deprecated, we should change symlink so that the common case
183038625Srvb     * returns the resultant vnode in a vpp argument.
183138625Srvb     */
183238625Srvb
183339085Srvb    MARK_ENTRY(CODA_SYMLINK_STATS);
183438625Srvb
183538625Srvb    /* Check for symlink of control object. */
183638625Srvb    if (IS_CTL_NAME(tdvp, nm, len)) {
183739085Srvb	MARK_INT_FAIL(CODA_SYMLINK_STATS);
183838625Srvb	return(EACCES);
183938625Srvb    }
184038625Srvb
184139085Srvb    if (plen+1 > CODA_MAXPATHLEN) {
184239085Srvb	MARK_INT_FAIL(CODA_SYMLINK_STATS);
184338625Srvb	return(EINVAL);
184438625Srvb    }
184538625Srvb
184639085Srvb    if (len+1 > CODA_MAXNAMLEN) {
184739085Srvb	MARK_INT_FAIL(CODA_SYMLINK_STATS);
184838625Srvb	error = EINVAL;
184938625Srvb	goto exit;
185038625Srvb    }
185138625Srvb
185238625Srvb    error = venus_symlink(vtomi(tdvp), &tdcp->c_fid, path, plen, nm, len, tva, cred, p);
185338625Srvb
185438625Srvb    /* Invalidate the parent's attr cache, the modification time has changed */
185538625Srvb    tdcp->c_flags &= ~C_VATTR;
185638625Srvb
185738625Srvb    /*
185838625Srvb     * Free the name buffer
185938625Srvb     */
186038625Srvb    if ((cnp->cn_flags & SAVESTART) == 0) {
186138759Srvb	zfree(namei_zone, cnp->cn_pnbuf);
186238625Srvb    }
186338625Srvb
186438625Srvb exit:
186539085Srvb    CODADEBUG(CODA_SYMLINK, myprintf(("in symlink result %d\n",error)); )
186638625Srvb    return(error);
186738625Srvb}
186838625Srvb
186938625Srvb/*
187038625Srvb * Read directory entries.
187138625Srvb */
187238625Srvbint
187339085Srvbcoda_readdir(v)
187438625Srvb    void *v;
187538625Srvb{
187638625Srvb/* true args */
187738625Srvb    struct vop_readdir_args *ap = v;
187838625Srvb    struct vnode *vp = ap->a_vp;
187938625Srvb    struct cnode *cp = VTOC(vp);
188038625Srvb    register struct uio *uiop = ap->a_uio;
188138625Srvb    struct ucred *cred = ap->a_cred;
188238625Srvb    int *eofflag = ap->a_eofflag;
188338625Srvb    u_long **cookies = ap->a_cookies;
188438625Srvb    int *ncookies = ap->a_ncookies;
188538625Srvb    struct proc *p = ap->a_uio->uio_procp;
188638625Srvb/* upcall decl */
188738625Srvb/* locals */
188838625Srvb    int error = 0;
188938625Srvb
189039085Srvb    MARK_ENTRY(CODA_READDIR_STATS);
189138625Srvb
189239085Srvb    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)); )
189338625Srvb
189438625Srvb    /* Check for readdir of control object. */
189538625Srvb    if (IS_CTL_VP(vp)) {
189639085Srvb	MARK_INT_FAIL(CODA_READDIR_STATS);
189738625Srvb	return(ENOENT);
189838625Srvb    }
189938625Srvb
190038759Srvb    {
190138625Srvb	/* If directory is not already open do an "internal open" on it. */
190238625Srvb	int opened_internally = 0;
190338625Srvb	if (cp->c_ovp == NULL) {
190438625Srvb	    opened_internally = 1;
190539085Srvb	    MARK_INT_GEN(CODA_OPEN_STATS);
190638625Srvb	    error = VOP_OPEN(vp, FREAD, cred, p);
190739085Srvbprintf("coda_readdir: Internally Opening %p\n", vp);
190838625Srvb	    if (error) {
190939085Srvb		printf("coda_readdir: VOP_OPEN on container failed %d\n", error);
191038625Srvb		return (error);
191138625Srvb	    }
191238625Srvb	    if (vp->v_type == VREG) {
191342315Seivind		error = vfs_object_create(vp, p, cred);
191438625Srvb		if (error != 0) {
191539085Srvb		    printf("coda_readdir: vfs_object_create() returns %d\n", error);
191638625Srvb		    vput(vp);
191738625Srvb		}
191838625Srvb	    }
191938625Srvb	    if (error) return(error);
192038625Srvb	}
192138625Srvb
192238625Srvb	/* Have UFS handle the call. */
192339085Srvb	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)); )
192438625Srvb	error = VOP_READDIR(cp->c_ovp, uiop, cred, eofflag, ncookies,
192538625Srvb			       cookies);
192638625Srvb
192738625Srvb	if (error)
192839085Srvb	    MARK_INT_FAIL(CODA_READDIR_STATS);
192938625Srvb	else
193039085Srvb	    MARK_INT_SAT(CODA_READDIR_STATS);
193138625Srvb
193238625Srvb	/* Do an "internal close" if necessary. */
193338625Srvb	if (opened_internally) {
193439085Srvb	    MARK_INT_GEN(CODA_CLOSE_STATS);
193538625Srvb	    (void)VOP_CLOSE(vp, FREAD, cred, p);
193638625Srvb	}
193738625Srvb    }
193838625Srvb
193938625Srvb    return(error);
194038625Srvb}
194138625Srvb
194238625Srvb/*
194338625Srvb * Convert from file system blocks to device blocks
194438625Srvb */
194538625Srvbint
194639085Srvbcoda_bmap(v)
194738625Srvb    void *v;
194838625Srvb{
194938625Srvb    /* XXX on the global proc */
195038625Srvb/* true args */
195138625Srvb    struct vop_bmap_args *ap = v;
195238625Srvb    struct vnode *vp __attribute__((unused)) = ap->a_vp;	/* file's vnode */
195338625Srvb    daddr_t bn __attribute__((unused)) = ap->a_bn;	/* fs block number */
195438625Srvb    struct vnode **vpp = ap->a_vpp;			/* RETURN vp of device */
195538625Srvb    daddr_t *bnp __attribute__((unused)) = ap->a_bnp;	/* RETURN device block number */
195638625Srvb    struct proc *p __attribute__((unused)) = curproc;
195738625Srvb/* upcall decl */
195838625Srvb/* locals */
195938625Srvb
196038625Srvb	int ret = 0;
196138625Srvb	struct cnode *cp;
196238625Srvb
196338625Srvb	cp = VTOC(vp);
196438625Srvb	if (cp->c_ovp) {
196539728Srvb		return EINVAL;
196638625Srvb		ret =  VOP_BMAP(cp->c_ovp, bn, vpp, bnp, ap->a_runp, ap->a_runb);
196739650Srvb#if	0
196838625Srvb		printf("VOP_BMAP(cp->c_ovp %p, bn %p, vpp %p, bnp %p, ap->a_runp %p, ap->a_runb %p) = %d\n",
196938625Srvb			cp->c_ovp, bn, vpp, bnp, ap->a_runp, ap->a_runb, ret);
197039650Srvb#endif
197138625Srvb		return ret;
197238625Srvb	} else {
197341504Srvb#if	0
197439085Srvb		printf("coda_bmap: no container\n");
197541504Srvb#endif
197638625Srvb		return(EOPNOTSUPP);
197738625Srvb	}
197838625Srvb}
197938625Srvb
198038625Srvb/*
198138625Srvb * I don't think the following two things are used anywhere, so I've
198238625Srvb * commented them out
198338625Srvb *
198438625Srvb * struct buf *async_bufhead;
198538625Srvb * int async_daemon_count;
198638625Srvb */
198738625Srvbint
198839085Srvbcoda_strategy(v)
198938625Srvb    void *v;
199038625Srvb{
199138625Srvb/* true args */
199238625Srvb    struct vop_strategy_args *ap = v;
199338625Srvb    register struct buf *bp __attribute__((unused)) = ap->a_bp;
199438625Srvb    struct proc *p __attribute__((unused)) = curproc;
199538625Srvb/* upcall decl */
199638625Srvb/* locals */
199738625Srvb
199839085Srvb	printf("coda_strategy: called ???\n");
199938625Srvb	return(EOPNOTSUPP);
200038625Srvb}
200138625Srvb
200238625Srvbint
200339085Srvbcoda_reclaim(v)
200438625Srvb    void *v;
200538625Srvb{
200638625Srvb/* true args */
200738625Srvb    struct vop_reclaim_args *ap = v;
200838625Srvb    struct vnode *vp = ap->a_vp;
200938625Srvb    struct cnode *cp = VTOC(vp);
201038625Srvb/* upcall decl */
201138625Srvb/* locals */
201238625Srvb
201338625Srvb/*
201438625Srvb * Forced unmount/flush will let vnodes with non zero use be destroyed!
201538625Srvb */
201638625Srvb    ENTRY;
201738625Srvb
201838625Srvb    if (IS_UNMOUNTING(cp)) {
201938625Srvb#ifdef	DEBUG
202038625Srvb	if (VTOC(vp)->c_ovp) {
202138625Srvb	    if (IS_UNMOUNTING(cp))
202239085Srvb		printf("coda_reclaim: c_ovp not void: vp %p, cp %p\n", vp, cp);
202338625Srvb	}
202438625Srvb#endif
202538625Srvb    } else {
202639650Srvb#ifdef OLD_DIAGNOSTIC
202738625Srvb	if (vp->v_usecount != 0)
202839650Srvb	    print("coda_reclaim: pushing active %p\n", vp);
202938625Srvb	if (VTOC(vp)->c_ovp) {
203039085Srvb	    panic("coda_reclaim: c_ovp not void");
203138625Srvb    }
203239650Srvb#endif
203338625Srvb    }
203438625Srvb    cache_purge(vp);
203539085Srvb    coda_free(VTOC(vp));
203638625Srvb    VTOC(vp) = NULL;
203738625Srvb    return (0);
203838625Srvb}
203938625Srvb
204038625Srvbint
204139085Srvbcoda_lock(v)
204238625Srvb    void *v;
204338625Srvb{
204438625Srvb/* true args */
204538625Srvb    struct vop_lock_args *ap = v;
204638625Srvb    struct vnode *vp = ap->a_vp;
204738625Srvb    struct cnode *cp = VTOC(vp);
204838625Srvb    struct proc  *p = ap->a_p;
204938625Srvb/* upcall decl */
205038625Srvb/* locals */
205138625Srvb
205238625Srvb    ENTRY;
205338625Srvb
205439085Srvb    if (coda_lockdebug) {
205538625Srvb	myprintf(("Attempting lock on %lx.%lx.%lx\n",
205638625Srvb		  cp->c_fid.Volume, cp->c_fid.Vnode, cp->c_fid.Unique));
205738625Srvb    }
205838625Srvb
205942900Seivind#ifndef	DEBUG_LOCKS
206038759Srvb    return (lockmgr(&cp->c_lock, ap->a_flags, &vp->v_interlock, p));
206142900Seivind#else
206242900Seivind    return (debuglockmgr(&cp->c_lock, ap->a_flags, &vp->v_interlock, p,
206342900Seivind			 "coda_lock", vp->filename, vp->line));
206442900Seivind#endif
206538625Srvb}
206638625Srvb
206738625Srvbint
206839085Srvbcoda_unlock(v)
206938625Srvb    void *v;
207038625Srvb{
207138625Srvb/* true args */
207238625Srvb    struct vop_unlock_args *ap = v;
207338625Srvb    struct vnode *vp = ap->a_vp;
207438625Srvb    struct cnode *cp = VTOC(vp);
207538625Srvb    struct proc  *p = ap->a_p;
207638625Srvb/* upcall decl */
207738625Srvb/* locals */
207838625Srvb
207938625Srvb    ENTRY;
208039085Srvb    if (coda_lockdebug) {
208138625Srvb	myprintf(("Attempting unlock on %lx.%lx.%lx\n",
208238625Srvb		  cp->c_fid.Volume, cp->c_fid.Vnode, cp->c_fid.Unique));
208338625Srvb    }
208438625Srvb
208538759Srvb    return (lockmgr(&cp->c_lock, ap->a_flags | LK_RELEASE, &vp->v_interlock, p));
208638625Srvb}
208738625Srvb
208838625Srvbint
208939085Srvbcoda_islocked(v)
209038625Srvb    void *v;
209138625Srvb{
209238625Srvb/* true args */
209338625Srvb    struct vop_islocked_args *ap = v;
209438625Srvb    struct cnode *cp = VTOC(ap->a_vp);
209538625Srvb    ENTRY;
209638625Srvb
209738625Srvb    return (lockstatus(&cp->c_lock));
209838625Srvb}
209938625Srvb
210038625Srvb/* How one looks up a vnode given a device/inode pair: */
210138625Srvbint
210239085Srvbcoda_grab_vnode(dev_t dev, ino_t ino, struct vnode **vpp)
210338625Srvb{
210438625Srvb    /* This is like VFS_VGET() or igetinode()! */
210538625Srvb    int           error;
210638625Srvb    struct mount *mp;
210738625Srvb
210838625Srvb    if (!(mp = devtomp(dev))) {
210939085Srvb	myprintf(("coda_grab_vnode: devtomp(%d) returns NULL\n", dev));
211038625Srvb	return(ENXIO);
211138625Srvb    }
211238625Srvb
211338625Srvb    /* XXX - ensure that nonzero-return means failure */
211438625Srvb    error = VFS_VGET(mp,ino,vpp);
211538625Srvb    if (error) {
211639085Srvb	myprintf(("coda_grab_vnode: iget/vget(%d, %d) returns %p, err %d\n",
211738625Srvb		  dev, ino, *vpp, error));
211838625Srvb	return(ENOENT);
211938625Srvb    }
212038625Srvb    return(0);
212138625Srvb}
212238625Srvb
212338625Srvbvoid
212438625Srvbprint_vattr( attr )
212538625Srvb	struct vattr *attr;
212638625Srvb{
212738625Srvb    char *typestr;
212838625Srvb
212938625Srvb    switch (attr->va_type) {
213038625Srvb    case VNON:
213138625Srvb	typestr = "VNON";
213238625Srvb	break;
213338625Srvb    case VREG:
213438625Srvb	typestr = "VREG";
213538625Srvb	break;
213638625Srvb    case VDIR:
213738625Srvb	typestr = "VDIR";
213838625Srvb	break;
213938625Srvb    case VBLK:
214038625Srvb	typestr = "VBLK";
214138625Srvb	break;
214238625Srvb    case VCHR:
214338625Srvb	typestr = "VCHR";
214438625Srvb	break;
214538625Srvb    case VLNK:
214638625Srvb	typestr = "VLNK";
214738625Srvb	break;
214838625Srvb    case VSOCK:
214938625Srvb	typestr = "VSCK";
215038625Srvb	break;
215138625Srvb    case VFIFO:
215238625Srvb	typestr = "VFFO";
215338625Srvb	break;
215438625Srvb    case VBAD:
215538625Srvb	typestr = "VBAD";
215638625Srvb	break;
215738625Srvb    default:
215838625Srvb	typestr = "????";
215938625Srvb	break;
216038625Srvb    }
216138625Srvb
216238625Srvb
216338625Srvb    myprintf(("attr: type %s mode %d uid %d gid %d fsid %d rdev %d\n",
216438625Srvb	      typestr, (int)attr->va_mode, (int)attr->va_uid,
216538625Srvb	      (int)attr->va_gid, (int)attr->va_fsid, (int)attr->va_rdev));
216638625Srvb
216738625Srvb    myprintf(("      fileid %d nlink %d size %d blocksize %d bytes %d\n",
216838625Srvb	      (int)attr->va_fileid, (int)attr->va_nlink,
216938625Srvb	      (int)attr->va_size,
217038625Srvb	      (int)attr->va_blocksize,(int)attr->va_bytes));
217138625Srvb    myprintf(("      gen %ld flags %ld vaflags %d\n",
217238625Srvb	      attr->va_gen, attr->va_flags, attr->va_vaflags));
217338625Srvb    myprintf(("      atime sec %d nsec %d\n",
217438625Srvb	      (int)attr->va_atime.tv_sec, (int)attr->va_atime.tv_nsec));
217538625Srvb    myprintf(("      mtime sec %d nsec %d\n",
217638625Srvb	      (int)attr->va_mtime.tv_sec, (int)attr->va_mtime.tv_nsec));
217738625Srvb    myprintf(("      ctime sec %d nsec %d\n",
217838625Srvb	      (int)attr->va_ctime.tv_sec, (int)attr->va_ctime.tv_nsec));
217938625Srvb}
218038625Srvb
218138625Srvb/* How to print a ucred */
218238625Srvbvoid
218338625Srvbprint_cred(cred)
218438625Srvb	struct ucred *cred;
218538625Srvb{
218638625Srvb
218738625Srvb	int i;
218838625Srvb
218938625Srvb	myprintf(("ref %d\tuid %d\n",cred->cr_ref,cred->cr_uid));
219038625Srvb
219138625Srvb	for (i=0; i < cred->cr_ngroups; i++)
219238625Srvb		myprintf(("\tgroup %d: (%d)\n",i,cred->cr_groups[i]));
219338625Srvb	myprintf(("\n"));
219438625Srvb
219538625Srvb}
219638625Srvb
219738625Srvb/*
219838625Srvb * Return a vnode for the given fid.
219938625Srvb * If no cnode exists for this fid create one and put it
220038625Srvb * in a table hashed by fid.Volume and fid.Vnode.  If the cnode for
220138625Srvb * this fid is already in the table return it (ref count is
220239085Srvb * incremented by coda_find.  The cnode will be flushed from the
220339085Srvb * table when coda_inactive calls coda_unsave.
220438625Srvb */
220538625Srvbstruct cnode *
220639085Srvbmake_coda_node(fid, vfsp, type)
220738625Srvb     ViceFid *fid; struct mount *vfsp; short type;
220838625Srvb{
220938625Srvb    struct cnode *cp;
221038625Srvb    int          err;
221138625Srvb
221239085Srvb    if ((cp = coda_find(fid)) == NULL) {
221338625Srvb	struct vnode *vp;
221438625Srvb
221539085Srvb	cp = coda_alloc();
221638625Srvb	lockinit(&cp->c_lock, PINOD, "cnode", 0, 0);
221738625Srvb	cp->c_fid = *fid;
221838625Srvb
221939085Srvb	err = getnewvnode(VT_CODA, vfsp, coda_vnodeop_p, &vp);
222038625Srvb	if (err) {
222139085Srvb	    panic("coda: getnewvnode returned error %d\n", err);
222238625Srvb	}
222338625Srvb	vp->v_data = cp;
222438625Srvb	vp->v_type = type;
222538625Srvb	cp->c_vnode = vp;
222639085Srvb	coda_save(cp);
222738625Srvb
222838625Srvb    } else {
222938625Srvb	vref(CTOV(cp));
223038625Srvb    }
223138625Srvb
223238625Srvb    return cp;
223338625Srvb}
2234