coda_vnops.c revision 38759
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 *
2938759Srvb *  	@(#) src/sys/cfs/cfs_vnodeops.c,v 1.1.1.1 1998/08/29 21:14:52 rvb Exp $
3038759Srvb *  $Id: $
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
5038625Srvb * $Log: cfs_vnodeops.c,v $
5138759Srvb * Revision 1.1.1.1  1998/08/29 21:14:52  rvb
5238759Srvb * Very Preliminary Coda
5338759Srvb *
5438625Srvb * Revision 1.12  1998/08/28 18:28:00  rvb
5538625Srvb * NetBSD -current is stricter!
5638625Srvb *
5738625Srvb * Revision 1.11  1998/08/28 18:12:23  rvb
5838625Srvb * Now it also works on FreeBSD -current.  This code will be
5938625Srvb * committed to the FreeBSD -current and NetBSD -current
6038625Srvb * trees.  It will then be tailored to the particular platform
6138625Srvb * by flushing conditional code.
6238625Srvb *
6338625Srvb * Revision 1.10  1998/08/18 17:05:21  rvb
6438625Srvb * Don't use __RCSID now
6538625Srvb *
6638625Srvb * Revision 1.9  1998/08/18 16:31:46  rvb
6738625Srvb * Sync the code for NetBSD -current; test on 1.3 later
6838625Srvb *
6938625Srvb * Revision 1.8  98/02/24  22:22:50  rvb
7038625Srvb * Fixes up mainly to flush iopen and friends
7138625Srvb *
7238625Srvb * Revision 1.7  98/01/31  20:53:15  rvb
7338625Srvb * First version that works on FreeBSD 2.2.5
7438625Srvb *
7538625Srvb * Revision 1.6  98/01/23  11:53:47  rvb
7638625Srvb * Bring RVB_CFS1_1 to HEAD
7738625Srvb *
7838625Srvb * Revision 1.5.2.8  98/01/23  11:21:11  rvb
7938625Srvb * Sync with 2.2.5
8038625Srvb *
8138625Srvb * Revision 1.5.2.7  97/12/19  14:26:08  rvb
8238625Srvb * session id
8338625Srvb *
8438625Srvb * Revision 1.5.2.6  97/12/16  22:01:34  rvb
8538625Srvb * Oops add cfs_subr.h cfs_venus.h; sync with peter
8638625Srvb *
8738625Srvb * Revision 1.5.2.5  97/12/16  12:40:14  rvb
8838625Srvb * Sync with 1.3
8938625Srvb *
9038625Srvb * Revision 1.5.2.4  97/12/10  14:08:31  rvb
9138625Srvb * Fix O_ flags; check result in cfscall
9238625Srvb *
9338625Srvb * Revision 1.5.2.3  97/12/10  11:40:27  rvb
9438625Srvb * No more ody
9538625Srvb *
9638625Srvb * Revision 1.5.2.2  97/12/09  16:07:15  rvb
9738625Srvb * Sync with vfs/include/coda.h
9838625Srvb *
9938625Srvb * Revision 1.5.2.1  97/12/06  17:41:25  rvb
10038625Srvb * Sync with peters coda.h
10138625Srvb *
10238625Srvb * Revision 1.5  97/12/05  10:39:23  rvb
10338625Srvb * Read CHANGES
10438625Srvb *
10538625Srvb * Revision 1.4.14.10  97/11/25  08:08:48  rvb
10638625Srvb * cfs_venus ... done; until cred/vattr change
10738625Srvb *
10838625Srvb * Revision 1.4.14.9  97/11/24  15:44:48  rvb
10938625Srvb * Final cfs_venus.c w/o macros, but one locking bug
11038625Srvb *
11138625Srvb * Revision 1.4.14.8  97/11/21  11:28:04  rvb
11238625Srvb * cfs_venus.c is done: first pass
11338625Srvb *
11438625Srvb * Revision 1.4.14.7  97/11/20  11:46:51  rvb
11538625Srvb * Capture current cfs_venus
11638625Srvb *
11738625Srvb * Revision 1.4.14.6  97/11/18  10:27:19  rvb
11838625Srvb * cfs_nbsd.c is DEAD!!!; integrated into cfs_vf/vnops.c
11938625Srvb * cfs_nb_foo and cfs_foo are joined
12038625Srvb *
12138625Srvb * Revision 1.4.14.5  97/11/13  22:03:03  rvb
12238625Srvb * pass2 cfs_NetBSD.h mt
12338625Srvb *
12438625Srvb * Revision 1.4.14.4  97/11/12  12:09:42  rvb
12538625Srvb * reorg pass1
12638625Srvb *
12738625Srvb * Revision 1.4.14.3  97/11/06  21:03:28  rvb
12838625Srvb * don't include headers in headers
12938625Srvb *
13038625Srvb * Revision 1.4.14.2  97/10/29  16:06:30  rvb
13138625Srvb * Kill DYING
13238625Srvb *
13338625Srvb * Revision 1.4.14.1  1997/10/28 23:10:18  rvb
13438625Srvb * >64Meg; venus can be killed!
13538625Srvb *
13638625Srvb * Revision 1.4  1997/02/20 13:54:50  lily
13738625Srvb * check for NULL return from cfsnc_lookup before CTOV
13838625Srvb *
13938625Srvb * Revision 1.3  1996/12/12 22:11:02  bnoble
14038625Srvb * Fixed the "downcall invokes venus operation" deadlock in all known cases.
14138625Srvb * There may be more
14238625Srvb *
14338625Srvb * Revision 1.2  1996/01/02 16:57:07  bnoble
14438625Srvb * Added support for Coda MiniCache and raw inode calls (final commit)
14538625Srvb *
14638625Srvb * Revision 1.1.2.1  1995/12/20 01:57:34  bnoble
14738625Srvb * Added CFS-specific files
14838625Srvb *
14938625Srvb * Revision 3.1.1.1  1995/03/04  19:08:06  bnoble
15038625Srvb * Branch for NetBSD port revisions
15138625Srvb *
15238625Srvb * Revision 3.1  1995/03/04  19:08:04  bnoble
15338625Srvb * Bump to major revision 3 to prepare for NetBSD port
15438625Srvb *
15538625Srvb * Revision 2.6  1995/02/17  16:25:26  dcs
15638625Srvb * These versions represent several changes:
15738625Srvb * 1. Allow venus to restart even if outstanding references exist.
15838625Srvb * 2. Have only one ctlvp per client, as opposed to one per mounted cfs device.d
15938625Srvb * 3. Allow ody_expand to return many members, not just one.
16038625Srvb *
16138625Srvb * Revision 2.5  94/11/09  20:29:27  dcs
16238625Srvb * Small bug in remove dealing with hard links and link counts was fixed.
16338625Srvb *
16438625Srvb * Revision 2.4  94/10/14  09:58:42  dcs
16538625Srvb * Made changes 'cause sun4s have braindead compilers
16638625Srvb *
16738625Srvb * Revision 2.3  94/10/12  16:46:37  dcs
16838625Srvb * Cleaned kernel/venus interface by removing XDR junk, plus
16938625Srvb * so cleanup to allow this code to be more easily ported.
17038625Srvb *
17138625Srvb * Revision 2.2  94/09/20  14:12:41  dcs
17238625Srvb * Fixed bug in rename when moving a directory.
17338625Srvb *
17438625Srvb * Revision 2.1  94/07/21  16:25:22  satya
17538625Srvb * Conversion to C++ 3.0; start of Coda Release 2.0
17638625Srvb *
17738625Srvb * Revision 1.4  93/12/17  01:38:01  luqi
17838625Srvb * Changes made for kernel to pass process info to Venus:
17938625Srvb *
18038625Srvb * (1) in file cfs.h
18138625Srvb * add process id and process group id in most of the cfs argument types.
18238625Srvb *
18338625Srvb * (2) in file cfs_vnodeops.c
18438625Srvb * add process info passing in most of the cfs vnode operations.
18538625Srvb *
18638625Srvb * (3) in file cfs_xdr.c
18738625Srvb * expand xdr routines according changes in (1).
18838625Srvb * add variable pass_process_info to allow venus for kernel version checking.
18938625Srvb *
19038625Srvb * Revision 1.3  93/05/28  16:24:33  bnoble
19138625Srvb * *** empty log message ***
19238625Srvb *
19338625Srvb * Revision 1.2  92/10/27  17:58:25  lily
19438625Srvb * merge kernel/latest and alpha/src/cfs
19538625Srvb *
19638625Srvb * Revision 2.4  92/09/30  14:16:37  mja
19738625Srvb * 	Redid buffer allocation so that it does kmem_{alloc,free} for all
19838625Srvb * 	architectures.  Zone allocation, previously used on the 386, caused
19938625Srvb * 	panics if it was invoked repeatedly.  Stack allocation, previously
20038625Srvb * 	used on all other architectures, tickled some Mach bug that appeared
20138625Srvb * 	with large stack frames.
20238625Srvb * 	[91/02/09            jjk]
20338625Srvb *
20438625Srvb * 	Added contributors blurb.
20538625Srvb * 	[90/12/13            jjk]
20638625Srvb *
20738625Srvb * Revision 2.3  90/07/26  15:50:09  mrt
20838625Srvb * 	    Fixed fix to rename to remove .. from moved directories.
20938625Srvb * 	[90/06/28            dcs]
21038625Srvb *
21138625Srvb * Revision 1.7  90/06/28  16:24:25  dcs
21238625Srvb * Fixed bug with moving directories, we weren't flushing .. for the moved directory.
21338625Srvb *
21438625Srvb * Revision 1.6  90/05/31  17:01:47  dcs
21538625Srvb * Prepare for merge with facilities kernel.
21638625Srvb *
21738625Srvb *
21838625Srvb */
21938625Srvb
22038625Srvb#include <sys/param.h>
22138625Srvb#include <sys/systm.h>
22238759Srvb#include <sys/kernel.h>
22338759Srvb#include <sys/proc.h>
22438625Srvb#include <sys/malloc.h>
22538759Srvb#include <sys/mount.h>
22638625Srvb#include <sys/errno.h>
22738625Srvb#include <sys/acct.h>
22838625Srvb#include <sys/file.h>
22938759Srvb#include <sys/fcntl.h>
23038625Srvb#include <sys/uio.h>
23138625Srvb#include <sys/namei.h>
23238625Srvb#include <sys/ioccom.h>
23338625Srvb#include <sys/select.h>
23438759Srvb
23538625Srvb#include <vm/vm.h>
23638625Srvb#include <vm/vm_object.h>
23738625Srvb#include <vm/vm_extern.h>
23838759Srvb#include <vm/vm_zone.h>
23938625Srvb
24038625Srvb#include <cfs/coda.h>
24138625Srvb#include <cfs/cnode.h>
24238625Srvb#include <cfs/cfs_vnodeops.h>
24338625Srvb#include <cfs/cfs_venus.h>
24438625Srvb#include <cfs/coda_opstats.h>
24538625Srvb#include <cfs/cfs_subr.h>
24638625Srvb#include <cfs/cfsnc.h>
24738625Srvb#include <cfs/pioctl.h>
24838625Srvb
24938625Srvb/*
25038625Srvb * These flags select various performance enhancements.
25138625Srvb */
25238625Srvbint cfs_attr_cache  = 1;       /* Set to cache attributes in the kernel */
25338625Srvbint cfs_symlink_cache = 1;     /* Set to cache symbolic link information */
25438625Srvbint cfs_access_cache = 1;      /* Set to handle some access checks directly */
25538625Srvb
25638625Srvb/* structure to keep track of vfs calls */
25738625Srvb
25838625Srvbstruct cfs_op_stats cfs_vnodeopstats[CFS_VNODEOPS_SIZE];
25938625Srvb
26038625Srvb#define MARK_ENTRY(op) (cfs_vnodeopstats[op].entries++)
26138625Srvb#define MARK_INT_SAT(op) (cfs_vnodeopstats[op].sat_intrn++)
26238625Srvb#define MARK_INT_FAIL(op) (cfs_vnodeopstats[op].unsat_intrn++)
26338625Srvb#define MARK_INT_GEN(op) (cfs_vnodeopstats[op].gen_intrn++)
26438625Srvb
26538625Srvb/* What we are delaying for in printf */
26638625Srvbint cfs_printf_delay = 0;  /* in microseconds */
26738625Srvbint cfs_vnop_print_entry = 0;
26838625Srvbstatic int cfs_lockdebug = 0;
26938625Srvb
27038625Srvb/* Definition of the vfs operation vector */
27138625Srvb
27238625Srvb/*
27338625Srvb * Some NetBSD details:
27438625Srvb *
27538625Srvb *   cfs_start is called at the end of the mount syscall.
27638625Srvb *   cfs_init is called at boot time.
27738625Srvb */
27838625Srvb
27938759Srvb#define ENTRY  if(cfs_vnop_print_entry) myprintf(("Entered %s\n",__FUNCTION__))
28038625Srvb
28138625Srvb/* Definition of the vnode operation vector */
28238625Srvb
28338625Srvbstruct vnodeopv_entry_desc cfs_vnodeop_entries[] = {
28438759Srvb    { &vop_default_desc, cfs_vop_error },
28538625Srvb    { &vop_lookup_desc, cfs_lookup },		/* lookup */
28638625Srvb    { &vop_create_desc, cfs_create },		/* create */
28738759Srvb    { &vop_mknod_desc, cfs_vop_error },				/* mknod */
28838625Srvb    { &vop_open_desc, cfs_open },		/* open */
28938625Srvb    { &vop_close_desc, cfs_close },		/* close */
29038625Srvb    { &vop_access_desc, cfs_access },		/* access */
29138625Srvb    { &vop_getattr_desc, cfs_getattr },		/* getattr */
29238625Srvb    { &vop_setattr_desc, cfs_setattr },		/* setattr */
29338625Srvb    { &vop_read_desc, cfs_read },		/* read */
29438625Srvb    { &vop_write_desc, cfs_write },		/* write */
29538625Srvb    { &vop_ioctl_desc, cfs_ioctl },		/* ioctl */
29638759Srvb    { &vop_mmap_desc, cfs_vop_error },				/* mmap */
29738625Srvb    { &vop_fsync_desc, cfs_fsync },		/* fsync */
29838625Srvb    { &vop_remove_desc, cfs_remove },		/* remove */
29938625Srvb    { &vop_link_desc, cfs_link },		/* link */
30038625Srvb    { &vop_rename_desc, cfs_rename },		/* rename */
30138625Srvb    { &vop_mkdir_desc, cfs_mkdir },		/* mkdir */
30238625Srvb    { &vop_rmdir_desc, cfs_rmdir },		/* rmdir */
30338625Srvb    { &vop_symlink_desc, cfs_symlink },		/* symlink */
30438625Srvb    { &vop_readdir_desc, cfs_readdir },		/* readdir */
30538625Srvb    { &vop_readlink_desc, cfs_readlink },	/* readlink */
30638625Srvb    { &vop_abortop_desc, cfs_abortop },		/* abortop */
30738625Srvb    { &vop_inactive_desc, cfs_inactive },	/* inactive */
30838625Srvb    { &vop_reclaim_desc, cfs_reclaim },		/* reclaim */
30938625Srvb    { &vop_lock_desc, cfs_lock },		/* lock */
31038625Srvb    { &vop_unlock_desc, cfs_unlock },		/* unlock */
31138625Srvb    { &vop_bmap_desc, cfs_bmap },		/* bmap */
31238625Srvb    { &vop_strategy_desc, cfs_strategy },	/* strategy */
31338759Srvb    { &vop_print_desc, cfs_vop_error },				/* print */
31438625Srvb    { &vop_islocked_desc, cfs_islocked },	/* islocked */
31538759Srvb    { &vop_pathconf_desc, cfs_vop_error },			/* pathconf */
31638759Srvb    { &vop_advlock_desc, cfs_vop_nop },				/* advlock */
31738759Srvb    { &vop_bwrite_desc, cfs_vop_error },			/* bwrite */
31838759Srvb    { &vop_lease_desc, cfs_vop_nop },				/* lease */
31938759Srvb    { &vop_poll_desc, (vop_t *) vop_stdpoll },
32038759Srvb    { &vop_getpages_desc, cfs_fbsd_getpages },	/* pager intf.*/
32138759Srvb    { &vop_putpages_desc, cfs_fbsd_putpages },	/* pager intf.*/
32238625Srvb
32338759Srvb#if	0
32438625Srvb
32538759Srvb    we need to define these someday
32638625Srvb#define UFS_BLKATOFF(aa, bb, cc, dd) VFSTOUFS((aa)->v_mount)->um_blkatoff(aa, bb, cc, dd)
32738625Srvb#define UFS_VALLOC(aa, bb, cc, dd) VFSTOUFS((aa)->v_mount)->um_valloc(aa, bb, cc, dd)
32838625Srvb#define UFS_VFREE(aa, bb, cc) VFSTOUFS((aa)->v_mount)->um_vfree(aa, bb, cc)
32938625Srvb#define UFS_TRUNCATE(aa, bb, cc, dd, ee) VFSTOUFS((aa)->v_mount)->um_truncate(aa, bb, cc, dd, ee)
33038625Srvb#define UFS_UPDATE(aa, bb, cc, dd) VFSTOUFS((aa)->v_mount)->um_update(aa, bb, cc, dd)
33138625Srvb
33238625Srvb    missing
33338625Srvb    { &vop_reallocblks_desc,	(vop_t *) ufs_missingop },
33438625Srvb    { &vop_cachedlookup_desc,	(vop_t *) ufs_lookup },
33538625Srvb    { &vop_whiteout_desc,	(vop_t *) ufs_whiteout },
33638625Srvb#endif
33738625Srvb    { (struct vnodeop_desc*)NULL, (int(*)(void *))NULL }
33838625Srvb};
33938625Srvb
34038625Srvbstatic struct vnodeopv_desc cfs_vnodeop_opv_desc =
34138625Srvb		{ &cfs_vnodeop_p, cfs_vnodeop_entries };
34238625Srvb
34338625SrvbVNODEOP_SET(cfs_vnodeop_opv_desc);
34438625Srvb
34538625Srvb/* A generic panic: we were called with something we didn't define yet */
34638625Srvbint
34738759Srvbcfs_vop_error(void *anon) {
34838625Srvb    struct vnodeop_desc **desc = (struct vnodeop_desc **)anon;
34938625Srvb
35038625Srvb    myprintf(("Vnode operation %s called, but not defined\n",
35138625Srvb	      (*desc)->vdesc_name));
35238759Srvb    panic("cfs_vop_error");
35338625Srvb    return 0;
35438625Srvb}
35538625Srvb
35638625Srvb/* A generic do-nothing.  For lease_check, advlock */
35738625Srvbint
35838759Srvbcfs_vop_nop(void *anon) {
35938625Srvb    struct vnodeop_desc **desc = (struct vnodeop_desc **)anon;
36038625Srvb
36138625Srvb    if (cfsdebug) {
36238625Srvb	myprintf(("Vnode operation %s called, but unsupported\n",
36338625Srvb		  (*desc)->vdesc_name));
36438625Srvb    }
36538625Srvb   return (0);
36638625Srvb}
36738625Srvb
36838625Srvbint
36938625Srvbcfs_vnodeopstats_init(void)
37038625Srvb{
37138625Srvb	register int i;
37238625Srvb
37338625Srvb	for(i=0;i<CFS_VNODEOPS_SIZE;i++) {
37438625Srvb		cfs_vnodeopstats[i].opcode = i;
37538625Srvb		cfs_vnodeopstats[i].entries = 0;
37638625Srvb		cfs_vnodeopstats[i].sat_intrn = 0;
37738625Srvb		cfs_vnodeopstats[i].unsat_intrn = 0;
37838625Srvb		cfs_vnodeopstats[i].gen_intrn = 0;
37938625Srvb	}
38038625Srvb	return 0;
38138625Srvb}
38238625Srvb
38338625Srvb/*
38438625Srvb * cfs_open calls Venus to return the device, inode pair of the cache
38538625Srvb * file holding the data. Using iget, cfs_open finds the vnode of the
38638625Srvb * cache file, and then opens it.
38738625Srvb */
38838625Srvbint
38938625Srvbcfs_open(v)
39038625Srvb    void *v;
39138625Srvb{
39238625Srvb    /*
39338625Srvb     * NetBSD can pass the O_EXCL flag in mode, even though the check
39438625Srvb     * has already happened.  Venus defensively assumes that if open
39538625Srvb     * is passed the EXCL, it must be a bug.  We strip the flag here.
39638625Srvb     */
39738625Srvb/* true args */
39838625Srvb    struct vop_open_args *ap = v;
39938625Srvb    register struct vnode **vpp = &(ap->a_vp);
40038625Srvb    struct cnode *cp = VTOC(*vpp);
40138625Srvb    int flag = ap->a_mode & (~O_EXCL);
40238625Srvb    struct ucred *cred = ap->a_cred;
40338625Srvb    struct proc *p = ap->a_p;
40438625Srvb/* locals */
40538625Srvb    int error;
40638625Srvb    struct vnode *vp;
40738625Srvb    dev_t dev;
40838625Srvb    ino_t inode;
40938625Srvb
41038625Srvb    MARK_ENTRY(CFS_OPEN_STATS);
41138625Srvb
41238625Srvb    /* Check for open of control file. */
41338625Srvb    if (IS_CTL_VP(*vpp)) {
41438625Srvb	/* XXX */
41538625Srvb	/* if (WRITEABLE(flag)) */
41638625Srvb	if (flag & (FWRITE | O_TRUNC | O_CREAT | O_EXCL)) {
41738625Srvb	    MARK_INT_FAIL(CFS_OPEN_STATS);
41838625Srvb	    return(EACCES);
41938625Srvb	}
42038625Srvb	MARK_INT_SAT(CFS_OPEN_STATS);
42138625Srvb	return(0);
42238625Srvb    }
42338625Srvb
42438625Srvb    error = venus_open(vtomi((*vpp)), &cp->c_fid, flag, cred, p, &dev, &inode);
42538625Srvb    if (error)
42638625Srvb	return (error);
42738625Srvb    if (!error) {
42838625Srvb	CFSDEBUG( CFS_OPEN,myprintf(("open: dev %d inode %d result %d\n",
42938625Srvb				  dev, inode, error)); )
43038625Srvb    }
43138625Srvb
43238625Srvb    /* Translate the <device, inode> pair for the cache file into
43338625Srvb       an inode pointer. */
43438625Srvb    error = cfs_grab_vnode(dev, inode, &vp);
43538625Srvb    if (error)
43638625Srvb	return (error);
43738625Srvb
43838759Srvb    /* We get the vnode back locked.  Needs unlocked */
43938759Srvb    VOP_UNLOCK(vp, 0, p);
44038625Srvb    /* Keep a reference until the close comes in. */
44138625Srvb    vref(*vpp);
44238625Srvb
44338625Srvb    /* Save the vnode pointer for the cache file. */
44438625Srvb    if (cp->c_ovp == NULL) {
44538625Srvb	cp->c_ovp = vp;
44638625Srvb    } else {
44738625Srvb	if (cp->c_ovp != vp)
44838625Srvb	    panic("cfs_open:  cp->c_ovp != ITOV(ip)");
44938625Srvb    }
45038625Srvb    cp->c_ocount++;
45138625Srvb
45238625Srvb    /* Flush the attribute cached if writing the file. */
45338625Srvb    if (flag & FWRITE) {
45438625Srvb	cp->c_owrite++;
45538625Srvb	cp->c_flags &= ~C_VATTR;
45638625Srvb    }
45738625Srvb
45838625Srvb    /* Save the <device, inode> pair for the cache file to speed
45938625Srvb       up subsequent page_read's. */
46038625Srvb    cp->c_device = dev;
46138625Srvb    cp->c_inode = inode;
46238625Srvb
46338625Srvb    /* Open the cache file. */
46438625Srvb    error = VOP_OPEN(vp, flag, cred, p);
46538625Srvb    if (error) {
46638625Srvb    	printf("cfs_open: VOP_OPEN on container failed %d\n", error);
46738625Srvb	return (error);
46838625Srvb    }
46938625Srvb    if (vp->v_type == VREG) {
47038625Srvb    	error = vfs_object_create(vp, p, cred, 1);
47138625Srvb	if (error != 0) {
47238625Srvb	    printf("cfs_open: vfs_object_create() returns %d\n", error);
47338625Srvb	    vput(vp);
47438625Srvb	}
47538625Srvb    }
47638625Srvb    return(error);
47738625Srvb}
47838625Srvb
47938625Srvb/*
48038625Srvb * Close the cache file used for I/O and notify Venus.
48138625Srvb */
48238625Srvbint
48338625Srvbcfs_close(v)
48438625Srvb    void *v;
48538625Srvb{
48638625Srvb/* true args */
48738625Srvb    struct vop_close_args *ap = v;
48838625Srvb    struct vnode *vp = ap->a_vp;
48938625Srvb    struct cnode *cp = VTOC(vp);
49038625Srvb    int flag = ap->a_fflag;
49138625Srvb    struct ucred *cred = ap->a_cred;
49238625Srvb    struct proc *p = ap->a_p;
49338625Srvb/* locals */
49438625Srvb    int error;
49538625Srvb
49638625Srvb    MARK_ENTRY(CFS_CLOSE_STATS);
49738625Srvb
49838625Srvb    /* Check for close of control file. */
49938625Srvb    if (IS_CTL_VP(vp)) {
50038625Srvb	MARK_INT_SAT(CFS_CLOSE_STATS);
50138625Srvb	return(0);
50238625Srvb    }
50338625Srvb
50438625Srvb    if (IS_UNMOUNTING(cp)) {
50538625Srvb	if (cp->c_ovp) {
50638625Srvb	    printf("cfs_close: destroying container ref %d, ufs vp %p of vp %p/cp %p\n",
50738625Srvb		    vp->v_usecount, cp->c_ovp, vp, cp);
50838625Srvb	    vgone(cp->c_ovp);
50938625Srvb	} else {
51038625Srvb	    printf("cfs_close: NO container vp %p/cp %p\n", vp, cp);
51138625Srvb	}
51238625Srvb	return ENODEV;
51338625Srvb    } else {
51438625Srvb	VOP_CLOSE(cp->c_ovp, flag, cred, p); /* Do errors matter here? */
51538625Srvb	vrele(cp->c_ovp);
51638625Srvb    }
51738625Srvb
51838625Srvb    if (--cp->c_ocount == 0)
51938625Srvb	cp->c_ovp = NULL;
52038625Srvb
52138625Srvb    if (flag & FWRITE)                    /* file was opened for write */
52238625Srvb	--cp->c_owrite;
52338625Srvb
52438625Srvb    error = venus_close(vtomi(vp), &cp->c_fid, flag, cred, p);
52538625Srvb    vrele(CTOV(cp));
52638625Srvb
52738625Srvb    CFSDEBUG(CFS_CLOSE, myprintf(("close: result %d\n",error)); )
52838625Srvb    return(error);
52938625Srvb}
53038625Srvb
53138625Srvbint
53238625Srvbcfs_read(v)
53338625Srvb    void *v;
53438625Srvb{
53538625Srvb    struct vop_read_args *ap = v;
53638625Srvb
53738625Srvb    ENTRY;
53838625Srvb    return(cfs_rdwr(ap->a_vp, ap->a_uio, UIO_READ,
53938625Srvb		    ap->a_ioflag, ap->a_cred, ap->a_uio->uio_procp));
54038625Srvb}
54138625Srvb
54238625Srvbint
54338625Srvbcfs_write(v)
54438625Srvb    void *v;
54538625Srvb{
54638625Srvb    struct vop_write_args *ap = v;
54738625Srvb
54838625Srvb    ENTRY;
54938625Srvb    return(cfs_rdwr(ap->a_vp, ap->a_uio, UIO_WRITE,
55038625Srvb		    ap->a_ioflag, ap->a_cred, ap->a_uio->uio_procp));
55138625Srvb}
55238625Srvb
55338625Srvbint
55438625Srvbcfs_rdwr(vp, uiop, rw, ioflag, cred, p)
55538625Srvb    struct vnode *vp;
55638625Srvb    struct uio *uiop;
55738625Srvb    enum uio_rw rw;
55838625Srvb    int ioflag;
55938625Srvb    struct ucred *cred;
56038625Srvb    struct proc *p;
56138625Srvb{
56238625Srvb/* upcall decl */
56338625Srvb  /* NOTE: container file operation!!! */
56438625Srvb/* locals */
56538625Srvb    struct cnode *cp = VTOC(vp);
56638625Srvb    struct vnode *cfvp = cp->c_ovp;
56738625Srvb    int igot_internally = 0;
56838625Srvb    int opened_internally = 0;
56938625Srvb    int error = 0;
57038625Srvb
57138625Srvb    MARK_ENTRY(CFS_RDWR_STATS);
57238625Srvb
57338625Srvb    CFSDEBUG(CFS_RDWR, myprintf(("cfs_rdwr(%d, %p, %d, %qd, %d)\n", rw,
57438625Srvb			      uiop->uio_iov->iov_base, uiop->uio_resid,
57538625Srvb			      uiop->uio_offset, uiop->uio_segflg)); )
57638625Srvb
57738625Srvb    /* Check for rdwr of control object. */
57838625Srvb    if (IS_CTL_VP(vp)) {
57938625Srvb	MARK_INT_FAIL(CFS_RDWR_STATS);
58038625Srvb	return(EINVAL);
58138625Srvb    }
58238625Srvb
58338625Srvb    /*
58438625Srvb     * If file is not already open this must be a page
58538625Srvb     * {read,write} request.  Iget the cache file's inode
58638625Srvb     * pointer if we still have its <device, inode> pair.
58738625Srvb     * Otherwise, we must do an internal open to derive the
58838625Srvb     * pair.
58938625Srvb     */
59038625Srvb    if (cfvp == NULL) {
59138625Srvb	/*
59238625Srvb	 * If we're dumping core, do the internal open. Otherwise
59338625Srvb	 * venus won't have the correct size of the core when
59438625Srvb	 * it's completely written.
59538625Srvb	 */
59638625Srvb	if (cp->c_inode != 0 && !(p && (p->p_acflag & ACORE))) {
59738625Srvb	    igot_internally = 1;
59838625Srvb	    error = cfs_grab_vnode(cp->c_device, cp->c_inode, &cfvp);
59938625Srvb	    if (error) {
60038625Srvb		MARK_INT_FAIL(CFS_RDWR_STATS);
60138625Srvb		return(error);
60238625Srvb	    }
60338625Srvb	    /*
60438625Srvb	     * We get the vnode back locked in both Mach and
60538625Srvb	     * NetBSD.  Needs unlocked
60638625Srvb	     */
60738759Srvb	    VOP_UNLOCK(cfvp, 0, p);
60838625Srvb	}
60938625Srvb	else {
61038625Srvb	    opened_internally = 1;
61138625Srvb	    MARK_INT_GEN(CFS_OPEN_STATS);
61238625Srvb	    error = VOP_OPEN(vp, (rw == UIO_READ ? FREAD : FWRITE),
61338625Srvb			     cred, p);
61438625Srvbprintf("cfs_rdwr: Internally Opening %p\n", vp);
61538625Srvb	    if (error) {
61638625Srvb		printf("cfs_rdwr: VOP_OPEN on container failed %d\n", error);
61738625Srvb		return (error);
61838625Srvb	    }
61938625Srvb	    if (vp->v_type == VREG) {
62038625Srvb		error = vfs_object_create(vp, p, cred, 1);
62138625Srvb		if (error != 0) {
62238625Srvb		    printf("cfs_rdwr: vfs_object_create() returns %d\n", error);
62338625Srvb		    vput(vp);
62438625Srvb		}
62538625Srvb	    }
62638625Srvb	    if (error) {
62738625Srvb		MARK_INT_FAIL(CFS_RDWR_STATS);
62838625Srvb		return(error);
62938625Srvb	    }
63038625Srvb	    cfvp = cp->c_ovp;
63138625Srvb	}
63238625Srvb    }
63338625Srvb
63438625Srvb    /* Have UFS handle the call. */
63538625Srvb    CFSDEBUG(CFS_RDWR, myprintf(("indirect rdwr: fid = (%lx.%lx.%lx), refcnt = %d\n",
63638625Srvb			      cp->c_fid.Volume, cp->c_fid.Vnode,
63738625Srvb			      cp->c_fid.Unique, CTOV(cp)->v_usecount)); )
63838625Srvb
63938625Srvb    if (rw == UIO_READ) {
64038625Srvb	error = VOP_READ(cfvp, uiop, ioflag, cred);
64138625Srvb    } else {
64238625Srvb	error = VOP_WRITE(cfvp, uiop, ioflag, cred);
64338625Srvb	/* ufs_write updates the vnode_pager_setsize for the vnode/object */
64438625Srvb	{   struct vattr attr;
64538625Srvb
64638625Srvb	    if (VOP_GETATTR(cfvp, &attr, cred, p) == 0) {
64738625Srvb		vnode_pager_setsize(vp, attr.va_size);
64838625Srvb	    }
64938625Srvb	}
65038625Srvb    }
65138625Srvb
65238625Srvb    if (error)
65338625Srvb	MARK_INT_FAIL(CFS_RDWR_STATS);
65438625Srvb    else
65538625Srvb	MARK_INT_SAT(CFS_RDWR_STATS);
65638625Srvb
65738625Srvb    /* Do an internal close if necessary. */
65838625Srvb    if (opened_internally) {
65938625Srvb	MARK_INT_GEN(CFS_CLOSE_STATS);
66038625Srvb	(void)VOP_CLOSE(vp, (rw == UIO_READ ? FREAD : FWRITE), cred, p);
66138625Srvb    }
66238625Srvb
66338625Srvb    /* Invalidate cached attributes if writing. */
66438625Srvb    if (rw == UIO_WRITE)
66538625Srvb	cp->c_flags &= ~C_VATTR;
66638625Srvb    return(error);
66738625Srvb}
66838625Srvb
66938625Srvbint
67038625Srvbcfs_ioctl(v)
67138625Srvb    void *v;
67238625Srvb{
67338625Srvb/* true args */
67438625Srvb    struct vop_ioctl_args *ap = v;
67538625Srvb    struct vnode *vp = ap->a_vp;
67638625Srvb    int com = ap->a_command;
67738625Srvb    caddr_t data = ap->a_data;
67838625Srvb    int flag = ap->a_fflag;
67938625Srvb    struct ucred *cred = ap->a_cred;
68038625Srvb    struct proc  *p = ap->a_p;
68138625Srvb/* locals */
68238625Srvb    int error;
68338625Srvb    struct vnode *tvp;
68438625Srvb    struct nameidata ndp;
68538625Srvb    struct PioctlData *iap = (struct PioctlData *)data;
68638625Srvb
68738625Srvb    MARK_ENTRY(CFS_IOCTL_STATS);
68838625Srvb
68938625Srvb    CFSDEBUG(CFS_IOCTL, myprintf(("in cfs_ioctl on %s\n", iap->path));)
69038625Srvb
69138625Srvb    /* Don't check for operation on a dying object, for ctlvp it
69238625Srvb       shouldn't matter */
69338625Srvb
69438625Srvb    /* Must be control object to succeed. */
69538625Srvb    if (!IS_CTL_VP(vp)) {
69638625Srvb	MARK_INT_FAIL(CFS_IOCTL_STATS);
69738625Srvb	CFSDEBUG(CFS_IOCTL, myprintf(("cfs_ioctl error: vp != ctlvp"));)
69838625Srvb	    return (EOPNOTSUPP);
69938625Srvb    }
70038625Srvb    /* Look up the pathname. */
70138625Srvb
70238625Srvb    /* Should we use the name cache here? It would get it from
70338625Srvb       lookupname sooner or later anyway, right? */
70438625Srvb
70538625Srvb    NDINIT(&ndp, LOOKUP, (iap->follow ? FOLLOW : NOFOLLOW), UIO_USERSPACE, ((caddr_t)iap->path), p);
70638625Srvb    error = namei(&ndp);
70738625Srvb    tvp = ndp.ni_vp;
70838625Srvb
70938625Srvb    if (error) {
71038625Srvb	MARK_INT_FAIL(CFS_IOCTL_STATS);
71138625Srvb	CFSDEBUG(CFS_IOCTL, myprintf(("cfs_ioctl error: lookup returns %d\n",
71238625Srvb				   error));)
71338625Srvb	return(error);
71438625Srvb    }
71538625Srvb
71638625Srvb    /*
71738625Srvb     * Make sure this is a coda style cnode, but it may be a
71838625Srvb     * different vfsp
71938625Srvb     */
72038625Srvb    /* XXX: this totally violates the comment about vtagtype in vnode.h */
72138625Srvb    if (tvp->v_tag != VT_CFS) {
72238625Srvb	vrele(tvp);
72338625Srvb	MARK_INT_FAIL(CFS_IOCTL_STATS);
72438625Srvb	CFSDEBUG(CFS_IOCTL,
72538625Srvb		 myprintf(("cfs_ioctl error: %s not a coda object\n",
72638625Srvb			iap->path));)
72738625Srvb	return(EINVAL);
72838625Srvb    }
72938625Srvb
73038625Srvb    if (iap->vi.in_size > VC_MAXDATASIZE) {
73138625Srvb	vrele(tvp);
73238625Srvb	return(EINVAL);
73338625Srvb    }
73438625Srvb    error = venus_ioctl(vtomi(tvp), &((VTOC(tvp))->c_fid), com, flag, data, cred, p);
73538625Srvb
73638625Srvb    if (error)
73738625Srvb	MARK_INT_FAIL(CFS_IOCTL_STATS);
73838625Srvb    else
73938625Srvb	CFSDEBUG(CFS_IOCTL, myprintf(("Ioctl returns %d \n", error)); )
74038625Srvb
74138625Srvb    vrele(tvp);
74238625Srvb    return(error);
74338625Srvb}
74438625Srvb
74538625Srvb/*
74638625Srvb * To reduce the cost of a user-level venus;we cache attributes in
74738625Srvb * the kernel.  Each cnode has storage allocated for an attribute. If
74838625Srvb * c_vattr is valid, return a reference to it. Otherwise, get the
74938625Srvb * attributes from venus and store them in the cnode.  There is some
75038625Srvb * question if this method is a security leak. But I think that in
75138625Srvb * order to make this call, the user must have done a lookup and
75238625Srvb * opened the file, and therefore should already have access.
75338625Srvb */
75438625Srvbint
75538625Srvbcfs_getattr(v)
75638625Srvb    void *v;
75738625Srvb{
75838625Srvb/* true args */
75938625Srvb    struct vop_getattr_args *ap = v;
76038625Srvb    struct vnode *vp = ap->a_vp;
76138625Srvb    struct cnode *cp = VTOC(vp);
76238625Srvb    struct vattr *vap = ap->a_vap;
76338625Srvb    struct ucred *cred = ap->a_cred;
76438625Srvb    struct proc *p = ap->a_p;
76538625Srvb/* locals */
76638625Srvb    int error;
76738625Srvb
76838625Srvb    MARK_ENTRY(CFS_GETATTR_STATS);
76938625Srvb
77038625Srvb    if (IS_UNMOUNTING(cp))
77138625Srvb	return ENODEV;
77238759Srvb
77338625Srvb    /* Check for getattr of control object. */
77438625Srvb    if (IS_CTL_VP(vp)) {
77538625Srvb	MARK_INT_FAIL(CFS_GETATTR_STATS);
77638625Srvb	return(ENOENT);
77738625Srvb    }
77838625Srvb
77938625Srvb    /* Check to see if the attributes have already been cached */
78038625Srvb    if (VALID_VATTR(cp)) {
78138625Srvb	CFSDEBUG(CFS_GETATTR, { myprintf(("attr cache hit: (%lx.%lx.%lx)\n",
78238625Srvb				       cp->c_fid.Volume,
78338625Srvb				       cp->c_fid.Vnode,
78438625Srvb				       cp->c_fid.Unique));});
78538625Srvb	CFSDEBUG(CFS_GETATTR, if (!(cfsdebug & ~CFS_GETATTR))
78638625Srvb		 print_vattr(&cp->c_vattr); );
78738625Srvb
78838625Srvb	*vap = cp->c_vattr;
78938625Srvb	MARK_INT_SAT(CFS_GETATTR_STATS);
79038625Srvb	return(0);
79138625Srvb    }
79238625Srvb
79338625Srvb    error = venus_getattr(vtomi(vp), &cp->c_fid, cred, p, vap);
79438625Srvb
79538625Srvb    if (!error) {
79638625Srvb	CFSDEBUG(CFS_GETATTR, myprintf(("getattr miss (%lx.%lx.%lx): result %d\n",
79738625Srvb				     cp->c_fid.Volume,
79838625Srvb				     cp->c_fid.Vnode,
79938625Srvb				     cp->c_fid.Unique,
80038625Srvb				     error)); )
80138625Srvb
80238625Srvb	CFSDEBUG(CFS_GETATTR, if (!(cfsdebug & ~CFS_GETATTR))
80338625Srvb		 print_vattr(vap);	);
80438625Srvb
80538625Srvb    {	int size = vap->va_size;
80638625Srvb    	struct vnode *convp = cp->c_ovp;
80738625Srvb	if (convp != (struct vnode *)0) {
80838625Srvb	    vnode_pager_setsize(convp, size);
80938625Srvb	}
81038625Srvb    }
81138625Srvb	/* If not open for write, store attributes in cnode */
81238625Srvb	if ((cp->c_owrite == 0) && (cfs_attr_cache)) {
81338625Srvb	    cp->c_vattr = *vap;
81438625Srvb	    cp->c_flags |= C_VATTR;
81538625Srvb	}
81638625Srvb
81738625Srvb    }
81838625Srvb    return(error);
81938625Srvb}
82038625Srvb
82138625Srvbint
82238625Srvbcfs_setattr(v)
82338625Srvb    void *v;
82438625Srvb{
82538625Srvb/* true args */
82638625Srvb    struct vop_setattr_args *ap = v;
82738625Srvb    register struct vnode *vp = ap->a_vp;
82838625Srvb    struct cnode *cp = VTOC(vp);
82938625Srvb    register struct vattr *vap = ap->a_vap;
83038625Srvb    struct ucred *cred = ap->a_cred;
83138625Srvb    struct proc *p = ap->a_p;
83238625Srvb/* locals */
83338625Srvb    int error;
83438625Srvb
83538625Srvb    MARK_ENTRY(CFS_SETATTR_STATS);
83638625Srvb
83738625Srvb    /* Check for setattr of control object. */
83838625Srvb    if (IS_CTL_VP(vp)) {
83938625Srvb	MARK_INT_FAIL(CFS_SETATTR_STATS);
84038625Srvb	return(ENOENT);
84138625Srvb    }
84238625Srvb
84338625Srvb    if (cfsdebug & CFSDBGMSK(CFS_SETATTR)) {
84438625Srvb	print_vattr(vap);
84538625Srvb    }
84638625Srvb    error = venus_setattr(vtomi(vp), &cp->c_fid, vap, cred, p);
84738625Srvb
84838625Srvb    if (!error)
84938625Srvb	cp->c_flags &= ~C_VATTR;
85038625Srvb
85138625Srvb    {	int size = vap->va_size;
85238625Srvb    	struct vnode *convp = cp->c_ovp;
85338625Srvb	if (size != VNOVAL && convp != (struct vnode *)0) {
85438625Srvb	    vnode_pager_setsize(convp, size);
85538625Srvb	}
85638625Srvb    }
85738625Srvb    CFSDEBUG(CFS_SETATTR,	myprintf(("setattr %d\n", error)); )
85838625Srvb    return(error);
85938625Srvb}
86038625Srvb
86138625Srvbint
86238625Srvbcfs_access(v)
86338625Srvb    void *v;
86438625Srvb{
86538625Srvb/* true args */
86638625Srvb    struct vop_access_args *ap = v;
86738625Srvb    struct vnode *vp = ap->a_vp;
86838625Srvb    struct cnode *cp = VTOC(vp);
86938625Srvb    int mode = ap->a_mode;
87038625Srvb    struct ucred *cred = ap->a_cred;
87138625Srvb    struct proc *p = ap->a_p;
87238625Srvb/* locals */
87338625Srvb    int error;
87438625Srvb
87538625Srvb    MARK_ENTRY(CFS_ACCESS_STATS);
87638625Srvb
87738625Srvb    /* Check for access of control object.  Only read access is
87838625Srvb       allowed on it. */
87938625Srvb    if (IS_CTL_VP(vp)) {
88038625Srvb	/* bogus hack - all will be marked as successes */
88138625Srvb	MARK_INT_SAT(CFS_ACCESS_STATS);
88238625Srvb	return(((mode & VREAD) && !(mode & (VWRITE | VEXEC)))
88338625Srvb	       ? 0 : EACCES);
88438625Srvb    }
88538625Srvb
88638625Srvb    /*
88738625Srvb     * if the file is a directory, and we are checking exec (eg lookup)
88838625Srvb     * access, and the file is in the namecache, then the user must have
88938625Srvb     * lookup access to it.
89038625Srvb     */
89138625Srvb    if (cfs_access_cache) {
89238625Srvb	if ((vp->v_type == VDIR) && (mode & VEXEC)) {
89338625Srvb	    if (cfsnc_lookup(cp, ".", 1, cred)) {
89438625Srvb		MARK_INT_SAT(CFS_ACCESS_STATS);
89538625Srvb		return(0);                     /* it was in the cache */
89638625Srvb	    }
89738625Srvb	}
89838625Srvb    }
89938625Srvb
90038625Srvb    error = venus_access(vtomi(vp), &cp->c_fid, mode, cred, p);
90138625Srvb
90238625Srvb    return(error);
90338625Srvb}
90438625Srvb
90538625Srvb/*
90638625Srvb * CFS abort op, called after namei() when a CREATE/DELETE isn't actually
90738625Srvb * done. If a buffer has been saved in anticipation of a cfs_create or
90838625Srvb * a cfs_remove, delete it.
90938625Srvb */
91038625Srvb/* ARGSUSED */
91138625Srvbint
91238625Srvbcfs_abortop(v)
91338625Srvb    void *v;
91438625Srvb{
91538625Srvb/* true args */
91638625Srvb    struct vop_abortop_args /* {
91738625Srvb	struct vnode *a_dvp;
91838625Srvb	struct componentname *a_cnp;
91938625Srvb    } */ *ap = v;
92038625Srvb/* upcall decl */
92138625Srvb/* locals */
92238625Srvb
92338625Srvb    if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
92438759Srvb	zfree(namei_zone, ap->a_cnp->cn_pnbuf);
92538625Srvb    return (0);
92638625Srvb}
92738625Srvb
92838625Srvbint
92938625Srvbcfs_readlink(v)
93038625Srvb    void *v;
93138625Srvb{
93238625Srvb/* true args */
93338625Srvb    struct vop_readlink_args *ap = v;
93438625Srvb    struct vnode *vp = ap->a_vp;
93538625Srvb    struct cnode *cp = VTOC(vp);
93638625Srvb    struct uio *uiop = ap->a_uio;
93738625Srvb    struct ucred *cred = ap->a_cred;
93838625Srvb    struct proc *p = ap->a_uio->uio_procp;
93938625Srvb/* locals */
94038625Srvb    int error;
94138625Srvb    char *str;
94238625Srvb    int len;
94338625Srvb
94438625Srvb    MARK_ENTRY(CFS_READLINK_STATS);
94538625Srvb
94638625Srvb    /* Check for readlink of control object. */
94738625Srvb    if (IS_CTL_VP(vp)) {
94838625Srvb	MARK_INT_FAIL(CFS_READLINK_STATS);
94938625Srvb	return(ENOENT);
95038625Srvb    }
95138625Srvb
95238625Srvb    if ((cfs_symlink_cache) && (VALID_SYMLINK(cp))) { /* symlink was cached */
95338625Srvb	uiop->uio_rw = UIO_READ;
95438625Srvb	error = uiomove(cp->c_symlink, (int)cp->c_symlen, uiop);
95538625Srvb	if (error)
95638625Srvb	    MARK_INT_FAIL(CFS_READLINK_STATS);
95738625Srvb	else
95838625Srvb	    MARK_INT_SAT(CFS_READLINK_STATS);
95938625Srvb	return(error);
96038625Srvb    }
96138625Srvb
96238625Srvb    error = venus_readlink(vtomi(vp), &cp->c_fid, cred, p, &str, &len);
96338625Srvb
96438625Srvb    if (!error) {
96538625Srvb	uiop->uio_rw = UIO_READ;
96638625Srvb	error = uiomove(str, len, uiop);
96738625Srvb
96838625Srvb	if (cfs_symlink_cache) {
96938625Srvb	    cp->c_symlink = str;
97038625Srvb	    cp->c_symlen = len;
97138625Srvb	    cp->c_flags |= C_SYMLINK;
97238625Srvb	} else
97338625Srvb	    CFS_FREE(str, len);
97438625Srvb    }
97538625Srvb
97638625Srvb    CFSDEBUG(CFS_READLINK, myprintf(("in readlink result %d\n",error));)
97738625Srvb    return(error);
97838625Srvb}
97938625Srvb
98038625Srvbint
98138625Srvbcfs_fsync(v)
98238625Srvb    void *v;
98338625Srvb{
98438625Srvb/* true args */
98538625Srvb    struct vop_fsync_args *ap = v;
98638625Srvb    struct vnode *vp = ap->a_vp;
98738625Srvb    struct cnode *cp = VTOC(vp);
98838625Srvb    struct ucred *cred = ap->a_cred;
98938625Srvb    struct proc *p = ap->a_p;
99038625Srvb/* locals */
99138625Srvb    struct vnode *convp = cp->c_ovp;
99238625Srvb    int error;
99338625Srvb
99438625Srvb    MARK_ENTRY(CFS_FSYNC_STATS);
99538625Srvb
99638625Srvb    /* Check for fsync on an unmounting object */
99738625Srvb    /* The NetBSD kernel, in it's infinite wisdom, can try to fsync
99838625Srvb     * after an unmount has been initiated.  This is a Bad Thing,
99938625Srvb     * which we have to avoid.  Not a legitimate failure for stats.
100038625Srvb     */
100138625Srvb    if (IS_UNMOUNTING(cp)) {
100238625Srvb	return(ENODEV);
100338625Srvb    }
100438625Srvb
100538625Srvb    /* Check for fsync of control object. */
100638625Srvb    if (IS_CTL_VP(vp)) {
100738625Srvb	MARK_INT_SAT(CFS_FSYNC_STATS);
100838625Srvb	return(0);
100938625Srvb    }
101038625Srvb
101138625Srvb    if (convp)
101238625Srvb    	VOP_FSYNC(convp, cred, MNT_WAIT, p);
101338625Srvb
101438625Srvb    /*
101538625Srvb     * We see fsyncs with usecount == 1 then usecount == 0.
101638625Srvb     * For now we ignore them.
101738625Srvb     */
101838625Srvb    /*
101938625Srvb    if (!vp->v_usecount) {
102038625Srvb    	printf("cfs_fsync on vnode %p with %d usecount.  c_flags = %x (%x)\n",
102138625Srvb		vp, vp->v_usecount, cp->c_flags, cp->c_flags&C_PURGING);
102238625Srvb    }
102338625Srvb    */
102438625Srvb
102538625Srvb    /*
102638625Srvb     * We can expect fsync on any vnode at all if venus is pruging it.
102738625Srvb     * Venus can't very well answer the fsync request, now can it?
102838625Srvb     * Hopefully, it won't have to, because hopefully, venus preserves
102938625Srvb     * the (possibly untrue) invariant that it never purges an open
103038625Srvb     * vnode.  Hopefully.
103138625Srvb     */
103238625Srvb    if (cp->c_flags & C_PURGING) {
103338625Srvb	return(0);
103438625Srvb    }
103538625Srvb
103638625Srvb    /* needs research */
103738625Srvb    return 0;
103838625Srvb    error = venus_fsync(vtomi(vp), &cp->c_fid, cred, p);
103938625Srvb
104038625Srvb    CFSDEBUG(CFS_FSYNC, myprintf(("in fsync result %d\n",error)); );
104138625Srvb    return(error);
104238625Srvb}
104338625Srvb
104438625Srvbint
104538625Srvbcfs_inactive(v)
104638625Srvb    void *v;
104738625Srvb{
104838625Srvb    /* XXX - at the moment, inactive doesn't look at cred, and doesn't
104938625Srvb       have a proc pointer.  Oops. */
105038625Srvb/* true args */
105138625Srvb    struct vop_inactive_args *ap = v;
105238625Srvb    struct vnode *vp = ap->a_vp;
105338625Srvb    struct cnode *cp = VTOC(vp);
105438625Srvb    struct ucred *cred __attribute__((unused)) = NULL;
105538625Srvb    struct proc *p __attribute__((unused)) = curproc;
105638625Srvb/* upcall decl */
105738625Srvb/* locals */
105838625Srvb
105938625Srvb    /* We don't need to send inactive to venus - DCS */
106038625Srvb    MARK_ENTRY(CFS_INACTIVE_STATS);
106138625Srvb
106238625Srvb    if (IS_CTL_VP(vp)) {
106338625Srvb	MARK_INT_SAT(CFS_INACTIVE_STATS);
106438625Srvb	return 0;
106538625Srvb    }
106638625Srvb
106738625Srvb    CFSDEBUG(CFS_INACTIVE, myprintf(("in inactive, %lx.%lx.%lx. vfsp %p\n",
106838625Srvb				  cp->c_fid.Volume, cp->c_fid.Vnode,
106938625Srvb				  cp->c_fid.Unique, vp->v_mount));)
107038625Srvb
107138625Srvb    /* If an array has been allocated to hold the symlink, deallocate it */
107238625Srvb    if ((cfs_symlink_cache) && (VALID_SYMLINK(cp))) {
107338625Srvb	if (cp->c_symlink == NULL)
107438625Srvb	    panic("cfs_inactive: null symlink pointer in cnode");
107538625Srvb
107638625Srvb	CFS_FREE(cp->c_symlink, cp->c_symlen);
107738625Srvb	cp->c_flags &= ~C_SYMLINK;
107838625Srvb	cp->c_symlen = 0;
107938625Srvb    }
108038625Srvb
108138625Srvb    /* Remove it from the table so it can't be found. */
108238625Srvb    cfs_unsave(cp);
108338625Srvb    if ((struct cfs_mntinfo *)(vp->v_mount->mnt_data) == NULL) {
108438625Srvb	myprintf(("Help! vfsp->vfs_data was NULL, but vnode %p wasn't dying\n", vp));
108538625Srvb	panic("badness in cfs_inactive\n");
108638625Srvb    }
108738625Srvb
108838625Srvb    if (IS_UNMOUNTING(cp)) {
108938625Srvb#ifdef	DEBUG
109038625Srvb	printf("cfs_inactive: IS_UNMOUNTING use %d: vp %p, cp %p\n", vp->v_usecount, vp, cp);
109138625Srvb	if (cp->c_ovp != NULL)
109238625Srvb	    printf("cfs_inactive: cp->ovp != NULL use %d: vp %p, cp %p\n",
109338625Srvb	    	   vp->v_usecount, vp, cp);
109438625Srvb#endif
109538759Srvb	lockmgr(&cp->c_lock, LK_RELEASE, &vp->v_interlock, p);
109638625Srvb    } else {
109738625Srvb#ifdef DIAGNOSTIC
109838625Srvb	if (CTOV(cp)->v_usecount) {
109938625Srvb	    panic("cfs_inactive: nonzero reference count");
110038625Srvb	}
110138625Srvb	if (cp->c_ovp != NULL) {
110238625Srvb	    panic("cfs_inactive:  cp->ovp != NULL");
110338625Srvb	}
110438625Srvb#endif
110538759Srvb	VOP_UNLOCK(vp, 0, p);
110638625Srvb	vgone(vp);
110738625Srvb    }
110838625Srvb
110938625Srvb    MARK_INT_SAT(CFS_INACTIVE_STATS);
111038625Srvb    return(0);
111138625Srvb}
111238625Srvb
111338625Srvb/*
111438625Srvb * Remote file system operations having to do with directory manipulation.
111538625Srvb */
111638625Srvb
111738625Srvb/*
111838625Srvb * It appears that in NetBSD, lookup is supposed to return the vnode locked
111938625Srvb */
112038625Srvbint
112138625Srvbcfs_lookup(v)
112238625Srvb    void *v;
112338625Srvb{
112438625Srvb/* true args */
112538625Srvb    struct vop_lookup_args *ap = v;
112638625Srvb    struct vnode *dvp = ap->a_dvp;
112738625Srvb    struct cnode *dcp = VTOC(dvp);
112838625Srvb    struct vnode **vpp = ap->a_vpp;
112938625Srvb    /*
113038625Srvb     * It looks as though ap->a_cnp->ni_cnd->cn_nameptr holds the rest
113138625Srvb     * of the string to xlate, and that we must try to get at least
113238625Srvb     * ap->a_cnp->ni_cnd->cn_namelen of those characters to macth.  I
113338625Srvb     * could be wrong.
113438625Srvb     */
113538625Srvb    struct componentname  *cnp = ap->a_cnp;
113638625Srvb    struct ucred *cred = cnp->cn_cred;
113738625Srvb    struct proc *p = cnp->cn_proc;
113838625Srvb/* locals */
113938625Srvb    struct cnode *cp;
114038625Srvb    const char *nm = cnp->cn_nameptr;
114138625Srvb    int len = cnp->cn_namelen;
114238625Srvb    ViceFid VFid;
114338625Srvb    int	vtype;
114438625Srvb    int error = 0;
114538625Srvb
114638625Srvb    MARK_ENTRY(CFS_LOOKUP_STATS);
114738625Srvb
114838625Srvb    CFSDEBUG(CFS_LOOKUP, myprintf(("lookup: %s in %lx.%lx.%lx\n",
114938625Srvb				   nm, dcp->c_fid.Volume,
115038625Srvb				   dcp->c_fid.Vnode, dcp->c_fid.Unique)););
115138625Srvb
115238625Srvb    /* Check for lookup of control object. */
115338625Srvb    if (IS_CTL_NAME(dvp, nm, len)) {
115438625Srvb	*vpp = cfs_ctlvp;
115538625Srvb	vref(*vpp);
115638625Srvb	MARK_INT_SAT(CFS_LOOKUP_STATS);
115738625Srvb	goto exit;
115838625Srvb    }
115938625Srvb
116038625Srvb    if (len+1 > CFS_MAXNAMLEN) {
116138625Srvb	MARK_INT_FAIL(CFS_LOOKUP_STATS);
116238625Srvb	CFSDEBUG(CFS_LOOKUP, myprintf(("name too long: lookup, %lx.%lx.%lx(%s)\n",
116338625Srvb				    dcp->c_fid.Volume, dcp->c_fid.Vnode,
116438625Srvb				    dcp->c_fid.Unique, nm)););
116538625Srvb	*vpp = (struct vnode *)0;
116638625Srvb	error = EINVAL;
116738625Srvb	goto exit;
116838625Srvb    }
116938625Srvb    /* First try to look the file up in the cfs name cache */
117038625Srvb    /* lock the parent vnode? */
117138625Srvb    cp = cfsnc_lookup(dcp, nm, len, cred);
117238625Srvb    if (cp) {
117338625Srvb	*vpp = CTOV(cp);
117438625Srvb	vref(*vpp);
117538625Srvb	CFSDEBUG(CFS_LOOKUP,
117638625Srvb		 myprintf(("lookup result %d vpp %p\n",error,*vpp));)
117738625Srvb    } else {
117838625Srvb
117938625Srvb	/* The name wasn't cached, so we need to contact Venus */
118038625Srvb	error = venus_lookup(vtomi(dvp), &dcp->c_fid, nm, len, cred, p, &VFid, &vtype);
118138625Srvb
118238625Srvb	if (error) {
118338625Srvb	    MARK_INT_FAIL(CFS_LOOKUP_STATS);
118438625Srvb	    CFSDEBUG(CFS_LOOKUP, myprintf(("lookup error on %lx.%lx.%lx(%s)%d\n",
118538625Srvb					dcp->c_fid.Volume, dcp->c_fid.Vnode, dcp->c_fid.Unique, nm, error));)
118638625Srvb	    *vpp = (struct vnode *)0;
118738625Srvb	} else {
118838625Srvb	    MARK_INT_SAT(CFS_LOOKUP_STATS);
118938625Srvb	    CFSDEBUG(CFS_LOOKUP,
119038625Srvb		     myprintf(("lookup: vol %lx vno %lx uni %lx type %o result %d\n",
119138625Srvb			    VFid.Volume, VFid.Vnode, VFid.Unique, vtype,
119238625Srvb			    error)); )
119338625Srvb
119438625Srvb	    cp = makecfsnode(&VFid, dvp->v_mount, vtype);
119538625Srvb	    *vpp = CTOV(cp);
119638625Srvb
119738625Srvb	    /* enter the new vnode in the Name Cache only if the top bit isn't set */
119838625Srvb	    /* And don't enter a new vnode for an invalid one! */
119938759Srvb	    if (!(vtype & CFS_NOCACHE))
120038625Srvb		cfsnc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
120138625Srvb	}
120238625Srvb    }
120338625Srvb
120438625Srvb exit:
120538625Srvb    /*
120638625Srvb     * If we are creating, and this was the last name to be looked up,
120738625Srvb     * and the error was ENOENT, then there really shouldn't be an
120838625Srvb     * error and we can make the leaf NULL and return success.  Since
120938625Srvb     * this is supposed to work under Mach as well as NetBSD, we're
121038625Srvb     * leaving this fn wrapped.  We also must tell lookup/namei that
121138625Srvb     * we need to save the last component of the name.  (Create will
121238625Srvb     * have to free the name buffer later...lucky us...)
121338625Srvb     */
121438625Srvb    if (((cnp->cn_nameiop == CREATE) || (cnp->cn_nameiop == RENAME))
121538625Srvb	&& (cnp->cn_flags & ISLASTCN)
121638625Srvb	&& (error == ENOENT))
121738625Srvb    {
121838625Srvb	error = EJUSTRETURN;
121938625Srvb	cnp->cn_flags |= SAVENAME;
122038625Srvb	*ap->a_vpp = NULL;
122138625Srvb    }
122238625Srvb
122338625Srvb    /*
122438625Srvb     * If we are removing, and we are at the last element, and we
122538625Srvb     * found it, then we need to keep the name around so that the
122638625Srvb     * removal will go ahead as planned.  Unfortunately, this will
122738625Srvb     * probably also lock the to-be-removed vnode, which may or may
122838625Srvb     * not be a good idea.  I'll have to look at the bits of
122938625Srvb     * cfs_remove to make sure.  We'll only save the name if we did in
123038625Srvb     * fact find the name, otherwise cfs_remove won't have a chance
123138625Srvb     * to free the pathname.
123238625Srvb     */
123338625Srvb    if ((cnp->cn_nameiop == DELETE)
123438625Srvb	&& (cnp->cn_flags & ISLASTCN)
123538625Srvb	&& !error)
123638625Srvb    {
123738625Srvb	cnp->cn_flags |= SAVENAME;
123838625Srvb    }
123938625Srvb
124038625Srvb    /*
124138625Srvb     * If the lookup went well, we need to (potentially?) unlock the
124238625Srvb     * parent, and lock the child.  We are only responsible for
124338625Srvb     * checking to see if the parent is supposed to be unlocked before
124438625Srvb     * we return.  We must always lock the child (provided there is
124538625Srvb     * one, and (the parent isn't locked or it isn't the same as the
124638625Srvb     * parent.)  Simple, huh?  We can never leave the parent locked unless
124738625Srvb     * we are ISLASTCN
124838625Srvb     */
124938625Srvb    if (!error || (error == EJUSTRETURN)) {
125038625Srvb	if (!(cnp->cn_flags & LOCKPARENT) || !(cnp->cn_flags & ISLASTCN)) {
125138759Srvb	    if ((error = VOP_UNLOCK(dvp, 0, p))) {
125238625Srvb		return error;
125338625Srvb	    }
125438625Srvb	    /*
125538625Srvb	     * The parent is unlocked.  As long as there is a child,
125638625Srvb	     * lock it without bothering to check anything else.
125738625Srvb	     */
125838625Srvb	    if (*ap->a_vpp) {
125938759Srvb		if ((error = VOP_LOCK(*ap->a_vpp, LK_EXCLUSIVE, p))) {
126038625Srvb		    printf("cfs_lookup: ");
126138625Srvb		    panic("unlocked parent but couldn't lock child");
126238625Srvb		}
126338625Srvb	    }
126438625Srvb	} else {
126538625Srvb	    /* The parent is locked, and may be the same as the child */
126638625Srvb	    if (*ap->a_vpp && (*ap->a_vpp != dvp)) {
126738625Srvb		/* Different, go ahead and lock it. */
126838759Srvb		if ((error = VOP_LOCK(*ap->a_vpp, LK_EXCLUSIVE, p))) {
126938625Srvb		    printf("cfs_lookup: ");
127038625Srvb		    panic("unlocked parent but couldn't lock child");
127138625Srvb		}
127238625Srvb	    }
127338625Srvb	}
127438625Srvb    } else {
127538625Srvb	/* If the lookup failed, we need to ensure that the leaf is NULL */
127638625Srvb	/* Don't change any locking? */
127738625Srvb	*ap->a_vpp = NULL;
127838625Srvb    }
127938625Srvb    return(error);
128038625Srvb}
128138625Srvb
128238625Srvb/*ARGSUSED*/
128338625Srvbint
128438625Srvbcfs_create(v)
128538625Srvb    void *v;
128638625Srvb{
128738625Srvb/* true args */
128838625Srvb    struct vop_create_args *ap = v;
128938625Srvb    struct vnode *dvp = ap->a_dvp;
129038625Srvb    struct cnode *dcp = VTOC(dvp);
129138625Srvb    struct vattr *va = ap->a_vap;
129238625Srvb    int exclusive = 1;
129338625Srvb    int mode = ap->a_vap->va_mode;
129438625Srvb    struct vnode **vpp = ap->a_vpp;
129538625Srvb    struct componentname  *cnp = ap->a_cnp;
129638625Srvb    struct ucred *cred = cnp->cn_cred;
129738625Srvb    struct proc *p = cnp->cn_proc;
129838625Srvb/* locals */
129938625Srvb    int error;
130038625Srvb    struct cnode *cp;
130138625Srvb    const char *nm = cnp->cn_nameptr;
130238625Srvb    int len = cnp->cn_namelen;
130338625Srvb    ViceFid VFid;
130438625Srvb    struct vattr attr;
130538625Srvb
130638625Srvb    MARK_ENTRY(CFS_CREATE_STATS);
130738625Srvb
130838625Srvb    /* All creates are exclusive XXX */
130938625Srvb    /* I'm assuming the 'mode' argument is the file mode bits XXX */
131038625Srvb
131138625Srvb    /* Check for create of control object. */
131238625Srvb    if (IS_CTL_NAME(dvp, nm, len)) {
131338625Srvb	*vpp = (struct vnode *)0;
131438625Srvb	MARK_INT_FAIL(CFS_CREATE_STATS);
131538625Srvb	return(EACCES);
131638625Srvb    }
131738625Srvb
131838625Srvb    error = venus_create(vtomi(dvp), &dcp->c_fid, nm, len, exclusive, mode, va, cred, p, &VFid, &attr);
131938625Srvb
132038625Srvb    if (!error) {
132138625Srvb
132238625Srvb	/* If this is an exclusive create, panic if the file already exists. */
132338625Srvb	/* Venus should have detected the file and reported EEXIST. */
132438625Srvb
132538625Srvb	if ((exclusive == 1) &&
132638625Srvb	    (cfs_find(&VFid) != NULL))
132738625Srvb	    panic("cnode existed for newly created file!");
132838625Srvb
132938625Srvb	cp = makecfsnode(&VFid, dvp->v_mount, attr.va_type);
133038625Srvb	*vpp = CTOV(cp);
133138625Srvb
133238625Srvb	/* Update va to reflect the new attributes. */
133338625Srvb	(*va) = attr;
133438625Srvb
133538625Srvb	/* Update the attribute cache and mark it as valid */
133638625Srvb	if (cfs_attr_cache) {
133738625Srvb	    VTOC(*vpp)->c_vattr = attr;
133838625Srvb	    VTOC(*vpp)->c_flags |= C_VATTR;
133938625Srvb	}
134038625Srvb
134138625Srvb	/* Invalidate the parent's attr cache, the modification time has changed */
134238625Srvb	VTOC(dvp)->c_flags &= ~C_VATTR;
134338625Srvb
134438625Srvb	/* enter the new vnode in the Name Cache */
134538625Srvb	cfsnc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
134638625Srvb
134738625Srvb	CFSDEBUG(CFS_CREATE,
134838625Srvb		 myprintf(("create: (%lx.%lx.%lx), result %d\n",
134938625Srvb			VFid.Volume, VFid.Vnode, VFid.Unique, error)); )
135038625Srvb    } else {
135138625Srvb	*vpp = (struct vnode *)0;
135238625Srvb	CFSDEBUG(CFS_CREATE, myprintf(("create error %d\n", error));)
135338625Srvb    }
135438625Srvb
135538625Srvb    if (!error) {
135638625Srvb	if (cnp->cn_flags & LOCKLEAF) {
135738759Srvb	    if ((error = VOP_LOCK(*ap->a_vpp, LK_EXCLUSIVE, p))) {
135838625Srvb		printf("cfs_create: ");
135938625Srvb		panic("unlocked parent but couldn't lock child");
136038625Srvb	    }
136138625Srvb	}
136238625Srvb#ifdef DIAGNOSTIC
136338625Srvb	else {
136438625Srvb	    printf("cfs_create: LOCKLEAF not set!\n");
136538625Srvb	}
136638625Srvb#endif /* DIAGNOSTIC */
136738625Srvb    }
136838625Srvb    /* Have to free the previously saved name */
136938625Srvb    /*
137038625Srvb     * This condition is stolen from ufs_makeinode.  I have no idea
137138625Srvb     * why it's here, but what the hey...
137238625Srvb     */
137338625Srvb    if ((cnp->cn_flags & SAVESTART) == 0) {
137438759Srvb	zfree(namei_zone, cnp->cn_pnbuf);
137538625Srvb    }
137638625Srvb    return(error);
137738625Srvb}
137838625Srvb
137938625Srvbint
138038625Srvbcfs_remove(v)
138138625Srvb    void *v;
138238625Srvb{
138338625Srvb/* true args */
138438625Srvb    struct vop_remove_args *ap = v;
138538625Srvb    struct vnode *dvp = ap->a_dvp;
138638625Srvb    struct cnode *cp = VTOC(dvp);
138738625Srvb    struct componentname  *cnp = ap->a_cnp;
138838625Srvb    struct ucred *cred = cnp->cn_cred;
138938625Srvb    struct proc *p = cnp->cn_proc;
139038625Srvb/* locals */
139138625Srvb    int error;
139238625Srvb    const char *nm = cnp->cn_nameptr;
139338625Srvb    int len = cnp->cn_namelen;
139438625Srvb    struct cnode *tp;
139538625Srvb
139638625Srvb    MARK_ENTRY(CFS_REMOVE_STATS);
139738625Srvb
139838625Srvb    CFSDEBUG(CFS_REMOVE, myprintf(("remove: %s in %lx.%lx.%lx\n",
139938625Srvb				   nm, cp->c_fid.Volume, cp->c_fid.Vnode,
140038625Srvb				   cp->c_fid.Unique)););
140138625Srvb
140238625Srvb    /* Remove the file's entry from the CFS Name Cache */
140338625Srvb    /* We're being conservative here, it might be that this person
140438625Srvb     * doesn't really have sufficient access to delete the file
140538625Srvb     * but we feel zapping the entry won't really hurt anyone -- dcs
140638625Srvb     */
140738625Srvb    /* I'm gonna go out on a limb here. If a file and a hardlink to it
140838625Srvb     * exist, and one is removed, the link count on the other will be
140938625Srvb     * off by 1. We could either invalidate the attrs if cached, or
141038625Srvb     * fix them. I'll try to fix them. DCS 11/8/94
141138625Srvb     */
141238625Srvb    tp = cfsnc_lookup(VTOC(dvp), nm, len, cred);
141338625Srvb    if (tp) {
141438625Srvb	if (VALID_VATTR(tp)) {	/* If attrs are cached */
141538625Srvb	    if (tp->c_vattr.va_nlink > 1) {	/* If it's a hard link */
141638625Srvb		tp->c_vattr.va_nlink--;
141738625Srvb	    }
141838625Srvb	}
141938625Srvb
142038625Srvb	cfsnc_zapfile(VTOC(dvp), nm, len);
142138625Srvb	/* No need to flush it if it doesn't exist! */
142238625Srvb    }
142338625Srvb    /* Invalidate the parent's attr cache, the modification time has changed */
142438625Srvb    VTOC(dvp)->c_flags &= ~C_VATTR;
142538625Srvb
142638625Srvb    /* Check for remove of control object. */
142738625Srvb    if (IS_CTL_NAME(dvp, nm, len)) {
142838625Srvb	MARK_INT_FAIL(CFS_REMOVE_STATS);
142938625Srvb	return(ENOENT);
143038625Srvb    }
143138625Srvb
143238625Srvb    error = venus_remove(vtomi(dvp), &cp->c_fid, nm, len, cred, p);
143338625Srvb
143438625Srvb    CFSDEBUG(CFS_REMOVE, myprintf(("in remove result %d\n",error)); )
143538625Srvb
143638625Srvb    if ((cnp->cn_flags & SAVESTART) == 0) {
143738759Srvb	zfree(namei_zone, cnp->cn_pnbuf);
143838625Srvb    }
143938625Srvb    return(error);
144038625Srvb}
144138625Srvb
144238625Srvbint
144338625Srvbcfs_link(v)
144438625Srvb    void *v;
144538625Srvb{
144638625Srvb/* true args */
144738625Srvb    struct vop_link_args *ap = v;
144838625Srvb    struct vnode *vp = ap->a_vp;
144938625Srvb    struct cnode *cp = VTOC(vp);
145038625Srvb    struct vnode *tdvp = ap->a_tdvp;
145138625Srvb    struct cnode *tdcp = VTOC(tdvp);
145238625Srvb    struct componentname *cnp = ap->a_cnp;
145338625Srvb    struct ucred *cred = cnp->cn_cred;
145438625Srvb    struct proc *p = cnp->cn_proc;
145538625Srvb/* locals */
145638625Srvb    int error;
145738625Srvb    const char *nm = cnp->cn_nameptr;
145838625Srvb    int len = cnp->cn_namelen;
145938625Srvb
146038625Srvb    MARK_ENTRY(CFS_LINK_STATS);
146138625Srvb
146238625Srvb    if (cfsdebug & CFSDBGMSK(CFS_LINK)) {
146338625Srvb
146438625Srvb	myprintf(("nb_link:   vp fid: (%lx.%lx.%lx)\n",
146538625Srvb		  cp->c_fid.Volume, cp->c_fid.Vnode, cp->c_fid.Unique));
146638625Srvb	myprintf(("nb_link: tdvp fid: (%lx.%lx.%lx)\n",
146738625Srvb		  tdcp->c_fid.Volume, tdcp->c_fid.Vnode, tdcp->c_fid.Unique));
146838625Srvb
146938625Srvb    }
147038625Srvb    if (cfsdebug & CFSDBGMSK(CFS_LINK)) {
147138625Srvb	myprintf(("link:   vp fid: (%lx.%lx.%lx)\n",
147238625Srvb		  cp->c_fid.Volume, cp->c_fid.Vnode, cp->c_fid.Unique));
147338625Srvb	myprintf(("link: tdvp fid: (%lx.%lx.%lx)\n",
147438625Srvb		  tdcp->c_fid.Volume, tdcp->c_fid.Vnode, tdcp->c_fid.Unique));
147538625Srvb
147638625Srvb    }
147738625Srvb
147838625Srvb    /* Check for link to/from control object. */
147938625Srvb    if (IS_CTL_NAME(tdvp, nm, len) || IS_CTL_VP(vp)) {
148038625Srvb	MARK_INT_FAIL(CFS_LINK_STATS);
148138625Srvb	return(EACCES);
148238625Srvb    }
148338625Srvb
148438625Srvb    error = venus_link(vtomi(vp), &cp->c_fid, &tdcp->c_fid, nm, len, cred, p);
148538625Srvb
148638625Srvb    /* Invalidate the parent's attr cache, the modification time has changed */
148738625Srvb    VTOC(tdvp)->c_flags &= ~C_VATTR;
148838625Srvb    VTOC(vp)->c_flags &= ~C_VATTR;
148938625Srvb
149038625Srvb    CFSDEBUG(CFS_LINK,	myprintf(("in link result %d\n",error)); )
149138625Srvb
149238625Srvbexit:
149338625Srvb
149438625Srvb    /* Drop the name buffer if we don't need to SAVESTART */
149538625Srvb    if ((cnp->cn_flags & SAVESTART) == 0) {
149638759Srvb	zfree(namei_zone, cnp->cn_pnbuf);
149738625Srvb    }
149838625Srvb    return(error);
149938625Srvb}
150038625Srvb
150138625Srvbint
150238625Srvbcfs_rename(v)
150338625Srvb    void *v;
150438625Srvb{
150538625Srvb/* true args */
150638625Srvb    struct vop_rename_args *ap = v;
150738625Srvb    struct vnode *odvp = ap->a_fdvp;
150838625Srvb    struct cnode *odcp = VTOC(odvp);
150938625Srvb    struct componentname  *fcnp = ap->a_fcnp;
151038625Srvb    struct vnode *ndvp = ap->a_tdvp;
151138625Srvb    struct cnode *ndcp = VTOC(ndvp);
151238625Srvb    struct componentname  *tcnp = ap->a_tcnp;
151338625Srvb    struct ucred *cred = fcnp->cn_cred;
151438625Srvb    struct proc *p = fcnp->cn_proc;
151538625Srvb/* true args */
151638625Srvb    int error;
151738625Srvb    const char *fnm = fcnp->cn_nameptr;
151838625Srvb    int flen = fcnp->cn_namelen;
151938625Srvb    const char *tnm = tcnp->cn_nameptr;
152038625Srvb    int tlen = tcnp->cn_namelen;
152138625Srvb
152238625Srvb    MARK_ENTRY(CFS_RENAME_STATS);
152338625Srvb
152438625Srvb    /* Hmmm.  The vnodes are already looked up.  Perhaps they are locked?
152538625Srvb       This could be Bad. XXX */
152638625Srvb#ifdef DIAGNOSTIC
152738625Srvb    if ((fcnp->cn_cred != tcnp->cn_cred)
152838625Srvb	|| (fcnp->cn_proc != tcnp->cn_proc))
152938625Srvb    {
153038625Srvb	panic("cfs_rename: component names don't agree");
153138625Srvb    }
153238625Srvb#endif DIAGNOSTIC
153338625Srvb
153438625Srvb    /* Check for rename involving control object. */
153538625Srvb    if (IS_CTL_NAME(odvp, fnm, flen) || IS_CTL_NAME(ndvp, tnm, tlen)) {
153638625Srvb	MARK_INT_FAIL(CFS_RENAME_STATS);
153738625Srvb	return(EACCES);
153838625Srvb    }
153938625Srvb
154038625Srvb    /* Problem with moving directories -- need to flush entry for .. */
154138625Srvb    if (odvp != ndvp) {
154238625Srvb	struct cnode *ovcp = cfsnc_lookup(VTOC(odvp), fnm, flen, cred);
154338625Srvb	if (ovcp) {
154438625Srvb	    struct vnode *ovp = CTOV(ovcp);
154538625Srvb	    if ((ovp) &&
154638625Srvb		(ovp->v_type == VDIR)) /* If it's a directory */
154738625Srvb		cfsnc_zapfile(VTOC(ovp),"..", 2);
154838625Srvb	}
154938625Srvb    }
155038625Srvb
155138625Srvb    /* Remove the entries for both source and target files */
155238625Srvb    cfsnc_zapfile(VTOC(odvp), fnm, flen);
155338625Srvb    cfsnc_zapfile(VTOC(ndvp), tnm, tlen);
155438625Srvb
155538625Srvb    /* Invalidate the parent's attr cache, the modification time has changed */
155638625Srvb    VTOC(odvp)->c_flags &= ~C_VATTR;
155738625Srvb    VTOC(ndvp)->c_flags &= ~C_VATTR;
155838625Srvb
155938625Srvb    if (flen+1 > CFS_MAXNAMLEN) {
156038625Srvb	MARK_INT_FAIL(CFS_RENAME_STATS);
156138625Srvb	error = EINVAL;
156238625Srvb	goto exit;
156338625Srvb    }
156438625Srvb
156538625Srvb    if (tlen+1 > CFS_MAXNAMLEN) {
156638625Srvb	MARK_INT_FAIL(CFS_RENAME_STATS);
156738625Srvb	error = EINVAL;
156838625Srvb	goto exit;
156938625Srvb    }
157038625Srvb
157138625Srvb    error = venus_rename(vtomi(odvp), &odcp->c_fid, &ndcp->c_fid, fnm, flen, tnm, tlen, cred, p);
157238625Srvb
157338625Srvb exit:
157438625Srvb    CFSDEBUG(CFS_RENAME, myprintf(("in rename result %d\n",error));)
157538625Srvb    /* XXX - do we need to call cache pureg on the moved vnode? */
157638625Srvb    cache_purge(ap->a_fvp);
157738625Srvb
157838625Srvb    /* It seems to be incumbent on us to drop locks on all four vnodes */
157938625Srvb    /* From-vnodes are not locked, only ref'd.  To-vnodes are locked. */
158038625Srvb
158138625Srvb    vrele(ap->a_fvp);
158238625Srvb    vrele(odvp);
158338625Srvb
158438625Srvb    if (ap->a_tvp) {
158538625Srvb	if (ap->a_tvp == ndvp) {
158638625Srvb	    vrele(ap->a_tvp);
158738625Srvb	} else {
158838625Srvb	    vput(ap->a_tvp);
158938625Srvb	}
159038625Srvb    }
159138625Srvb
159238625Srvb    vput(ndvp);
159338625Srvb    return(error);
159438625Srvb}
159538625Srvb
159638625Srvbint
159738625Srvbcfs_mkdir(v)
159838625Srvb    void *v;
159938625Srvb{
160038625Srvb/* true args */
160138625Srvb    struct vop_mkdir_args *ap = v;
160238625Srvb    struct vnode *dvp = ap->a_dvp;
160338625Srvb    struct cnode *dcp = VTOC(dvp);
160438625Srvb    struct componentname  *cnp = ap->a_cnp;
160538625Srvb    register struct vattr *va = ap->a_vap;
160638625Srvb    struct vnode **vpp = ap->a_vpp;
160738625Srvb    struct ucred *cred = cnp->cn_cred;
160838625Srvb    struct proc *p = cnp->cn_proc;
160938625Srvb/* locals */
161038625Srvb    int error;
161138625Srvb    const char *nm = cnp->cn_nameptr;
161238625Srvb    int len = cnp->cn_namelen;
161338625Srvb    struct cnode *cp;
161438625Srvb    ViceFid VFid;
161538625Srvb    struct vattr ova;
161638625Srvb
161738625Srvb    MARK_ENTRY(CFS_MKDIR_STATS);
161838625Srvb
161938625Srvb    /* Check for mkdir of target object. */
162038625Srvb    if (IS_CTL_NAME(dvp, nm, len)) {
162138625Srvb	*vpp = (struct vnode *)0;
162238625Srvb	MARK_INT_FAIL(CFS_MKDIR_STATS);
162338625Srvb	return(EACCES);
162438625Srvb    }
162538625Srvb
162638625Srvb    if (len+1 > CFS_MAXNAMLEN) {
162738625Srvb	*vpp = (struct vnode *)0;
162838625Srvb	MARK_INT_FAIL(CFS_MKDIR_STATS);
162938625Srvb	return(EACCES);
163038625Srvb    }
163138625Srvb
163238625Srvb    error = venus_mkdir(vtomi(dvp), &dcp->c_fid, nm, len, va, cred, p, &VFid, &ova);
163338625Srvb
163438625Srvb    if (!error) {
163538625Srvb	if (cfs_find(&VFid) != NULL)
163638625Srvb	    panic("cnode existed for newly created directory!");
163738625Srvb
163838625Srvb
163938625Srvb	cp =  makecfsnode(&VFid, dvp->v_mount, va->va_type);
164038625Srvb	*vpp = CTOV(cp);
164138625Srvb
164238625Srvb	/* enter the new vnode in the Name Cache */
164338625Srvb	cfsnc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
164438625Srvb
164538625Srvb	/* as a side effect, enter "." and ".." for the directory */
164638625Srvb	cfsnc_enter(VTOC(*vpp), ".", 1, cred, VTOC(*vpp));
164738625Srvb	cfsnc_enter(VTOC(*vpp), "..", 2, cred, VTOC(dvp));
164838625Srvb
164938625Srvb	if (cfs_attr_cache) {
165038625Srvb	    VTOC(*vpp)->c_vattr = ova;		/* update the attr cache */
165138625Srvb	    VTOC(*vpp)->c_flags |= C_VATTR;	/* Valid attributes in cnode */
165238625Srvb	}
165338625Srvb
165438625Srvb	/* Invalidate the parent's attr cache, the modification time has changed */
165538625Srvb	VTOC(dvp)->c_flags &= ~C_VATTR;
165638625Srvb
165738625Srvb	CFSDEBUG( CFS_MKDIR, myprintf(("mkdir: (%lx.%lx.%lx) result %d\n",
165838625Srvb				    VFid.Volume, VFid.Vnode, VFid.Unique, error)); )
165938625Srvb    } else {
166038625Srvb	*vpp = (struct vnode *)0;
166138625Srvb	CFSDEBUG(CFS_MKDIR, myprintf(("mkdir error %d\n",error));)
166238625Srvb    }
166338625Srvb
166438625Srvb    /* Have to free the previously saved name */
166538625Srvb    /*
166638625Srvb     * ufs_mkdir doesn't check for SAVESTART before freeing the
166738625Srvb     * pathname buffer, but ufs_create does.  For the moment, I'll
166838625Srvb     * follow their lead, but this seems like it is probably
166938625Srvb     * incorrect.
167038625Srvb     */
167138759Srvb    zfree(namei_zone, cnp->cn_pnbuf);
167238625Srvb    return(error);
167338625Srvb}
167438625Srvb
167538625Srvbint
167638625Srvbcfs_rmdir(v)
167738625Srvb    void *v;
167838625Srvb{
167938625Srvb/* true args */
168038625Srvb    struct vop_rmdir_args *ap = v;
168138625Srvb    struct vnode *dvp = ap->a_dvp;
168238625Srvb    struct cnode *dcp = VTOC(dvp);
168338625Srvb    struct componentname  *cnp = ap->a_cnp;
168438625Srvb    struct ucred *cred = cnp->cn_cred;
168538625Srvb    struct proc *p = cnp->cn_proc;
168638625Srvb/* true args */
168738625Srvb    int error;
168838625Srvb    const char *nm = cnp->cn_nameptr;
168938625Srvb    int len = cnp->cn_namelen;
169038625Srvb    struct cnode *cp;
169138625Srvb
169238625Srvb    MARK_ENTRY(CFS_RMDIR_STATS);
169338625Srvb
169438625Srvb    /* Check for rmdir of control object. */
169538625Srvb    if (IS_CTL_NAME(dvp, nm, len)) {
169638625Srvb	MARK_INT_FAIL(CFS_RMDIR_STATS);
169738625Srvb	return(ENOENT);
169838625Srvb    }
169938625Srvb
170038625Srvb    /* We're being conservative here, it might be that this person
170138625Srvb     * doesn't really have sufficient access to delete the file
170238625Srvb     * but we feel zapping the entry won't really hurt anyone -- dcs
170338625Srvb     */
170438625Srvb    /*
170538625Srvb     * As a side effect of the rmdir, remove any entries for children of
170638625Srvb     * the directory, especially "." and "..".
170738625Srvb     */
170838625Srvb    cp = cfsnc_lookup(dcp, nm, len, cred);
170938625Srvb    if (cp) cfsnc_zapParentfid(&(cp->c_fid), NOT_DOWNCALL);
171038625Srvb
171138625Srvb    /* Remove the file's entry from the CFS Name Cache */
171238625Srvb    cfsnc_zapfile(dcp, nm, len);
171338625Srvb
171438625Srvb    /* Invalidate the parent's attr cache, the modification time has changed */
171538625Srvb    dcp->c_flags &= ~C_VATTR;
171638625Srvb
171738625Srvb    error = venus_rmdir(vtomi(dvp), &dcp->c_fid, nm, len, cred, p);
171838625Srvb
171938625Srvb    CFSDEBUG(CFS_RMDIR, myprintf(("in rmdir result %d\n", error)); )
172038625Srvb
172138625Srvb    if ((cnp->cn_flags & SAVESTART) == 0) {
172238759Srvb	zfree(namei_zone, cnp->cn_pnbuf);
172338625Srvb    }
172438625Srvb    return(error);
172538625Srvb}
172638625Srvb
172738625Srvbint
172838625Srvbcfs_symlink(v)
172938625Srvb    void *v;
173038625Srvb{
173138625Srvb/* true args */
173238625Srvb    struct vop_symlink_args *ap = v;
173338625Srvb    struct vnode *tdvp = ap->a_dvp;
173438625Srvb    struct cnode *tdcp = VTOC(tdvp);
173538625Srvb    struct componentname *cnp = ap->a_cnp;
173638625Srvb    struct vattr *tva = ap->a_vap;
173738625Srvb    char *path = ap->a_target;
173838625Srvb    struct ucred *cred = cnp->cn_cred;
173938625Srvb    struct proc *p = cnp->cn_proc;
174038625Srvb/* locals */
174138625Srvb    int error;
174238625Srvb    /*
174338625Srvb     * XXX I'm assuming the following things about cfs_symlink's
174438625Srvb     * arguments:
174538625Srvb     *       t(foo) is the new name/parent/etc being created.
174638625Srvb     *       lname is the contents of the new symlink.
174738625Srvb     */
174838759Srvb    char *nm = cnp->cn_nameptr;
174938625Srvb    int len = cnp->cn_namelen;
175038625Srvb    int plen = strlen(path);
175138625Srvb
175238625Srvb    /* XXX What about the vpp argument?  Do we need it? */
175338625Srvb    /*
175438625Srvb     * Here's the strategy for the moment: perform the symlink, then
175538625Srvb     * do a lookup to grab the resulting vnode.  I know this requires
175638625Srvb     * two communications with Venus for a new sybolic link, but
175738625Srvb     * that's the way the ball bounces.  I don't yet want to change
175838625Srvb     * the way the Mach symlink works.  When Mach support is
175938625Srvb     * deprecated, we should change symlink so that the common case
176038625Srvb     * returns the resultant vnode in a vpp argument.
176138625Srvb     */
176238625Srvb
176338625Srvb    MARK_ENTRY(CFS_SYMLINK_STATS);
176438625Srvb
176538625Srvb    /* Check for symlink of control object. */
176638625Srvb    if (IS_CTL_NAME(tdvp, nm, len)) {
176738625Srvb	MARK_INT_FAIL(CFS_SYMLINK_STATS);
176838625Srvb	return(EACCES);
176938625Srvb    }
177038625Srvb
177138625Srvb    if (plen+1 > CFS_MAXPATHLEN) {
177238625Srvb	MARK_INT_FAIL(CFS_SYMLINK_STATS);
177338625Srvb	return(EINVAL);
177438625Srvb    }
177538625Srvb
177638625Srvb    if (len+1 > CFS_MAXNAMLEN) {
177738625Srvb	MARK_INT_FAIL(CFS_SYMLINK_STATS);
177838625Srvb	error = EINVAL;
177938625Srvb	goto exit;
178038625Srvb    }
178138625Srvb
178238625Srvb    error = venus_symlink(vtomi(tdvp), &tdcp->c_fid, path, plen, nm, len, tva, cred, p);
178338625Srvb
178438625Srvb    /* Invalidate the parent's attr cache, the modification time has changed */
178538625Srvb    tdcp->c_flags &= ~C_VATTR;
178638625Srvb
178738625Srvb    /*
178838625Srvb     * Free the name buffer
178938625Srvb     */
179038625Srvb    if ((cnp->cn_flags & SAVESTART) == 0) {
179138759Srvb	zfree(namei_zone, cnp->cn_pnbuf);
179238625Srvb    }
179338625Srvb
179438625Srvb exit:
179538625Srvb    CFSDEBUG(CFS_SYMLINK, myprintf(("in symlink result %d\n",error)); )
179638625Srvb    return(error);
179738625Srvb}
179838625Srvb
179938625Srvb/*
180038625Srvb * Read directory entries.
180138625Srvb */
180238625Srvbint
180338625Srvbcfs_readdir(v)
180438625Srvb    void *v;
180538625Srvb{
180638625Srvb/* true args */
180738625Srvb    struct vop_readdir_args *ap = v;
180838625Srvb    struct vnode *vp = ap->a_vp;
180938625Srvb    struct cnode *cp = VTOC(vp);
181038625Srvb    register struct uio *uiop = ap->a_uio;
181138625Srvb    struct ucred *cred = ap->a_cred;
181238625Srvb    int *eofflag = ap->a_eofflag;
181338625Srvb    u_long **cookies = ap->a_cookies;
181438625Srvb    int *ncookies = ap->a_ncookies;
181538625Srvb    struct proc *p = ap->a_uio->uio_procp;
181638625Srvb/* upcall decl */
181738625Srvb/* locals */
181838625Srvb    int error = 0;
181938625Srvb
182038625Srvb    MARK_ENTRY(CFS_READDIR_STATS);
182138625Srvb
182238625Srvb    CFSDEBUG(CFS_READDIR, myprintf(("cfs_readdir(%p, %d, %qd, %d)\n", uiop->uio_iov->iov_base, uiop->uio_resid, uiop->uio_offset, uiop->uio_segflg)); )
182338625Srvb
182438625Srvb    /* Check for readdir of control object. */
182538625Srvb    if (IS_CTL_VP(vp)) {
182638625Srvb	MARK_INT_FAIL(CFS_READDIR_STATS);
182738625Srvb	return(ENOENT);
182838625Srvb    }
182938625Srvb
183038759Srvb    {
183138625Srvb	/* If directory is not already open do an "internal open" on it. */
183238625Srvb	int opened_internally = 0;
183338625Srvb	if (cp->c_ovp == NULL) {
183438625Srvb	    opened_internally = 1;
183538625Srvb	    MARK_INT_GEN(CFS_OPEN_STATS);
183638625Srvb	    error = VOP_OPEN(vp, FREAD, cred, p);
183738625Srvbprintf("cfs_readdir: Internally Opening %p\n", vp);
183838625Srvb	    if (error) {
183938625Srvb		printf("cfs_readdir: VOP_OPEN on container failed %d\n", error);
184038625Srvb		return (error);
184138625Srvb	    }
184238625Srvb	    if (vp->v_type == VREG) {
184338625Srvb		error = vfs_object_create(vp, p, cred, 1);
184438625Srvb		if (error != 0) {
184538625Srvb		    printf("cfs_readdir: vfs_object_create() returns %d\n", error);
184638625Srvb		    vput(vp);
184738625Srvb		}
184838625Srvb	    }
184938625Srvb	    if (error) return(error);
185038625Srvb	}
185138625Srvb
185238625Srvb	/* Have UFS handle the call. */
185338625Srvb	CFSDEBUG(CFS_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)); )
185438625Srvb	error = VOP_READDIR(cp->c_ovp, uiop, cred, eofflag, ncookies,
185538625Srvb			       cookies);
185638625Srvb
185738625Srvb	if (error)
185838625Srvb	    MARK_INT_FAIL(CFS_READDIR_STATS);
185938625Srvb	else
186038625Srvb	    MARK_INT_SAT(CFS_READDIR_STATS);
186138625Srvb
186238625Srvb	/* Do an "internal close" if necessary. */
186338625Srvb	if (opened_internally) {
186438625Srvb	    MARK_INT_GEN(CFS_CLOSE_STATS);
186538625Srvb	    (void)VOP_CLOSE(vp, FREAD, cred, p);
186638625Srvb	}
186738625Srvb    }
186838625Srvb
186938625Srvb    return(error);
187038625Srvb}
187138625Srvb
187238625Srvb/*
187338625Srvb * Convert from file system blocks to device blocks
187438625Srvb */
187538625Srvbint
187638625Srvbcfs_bmap(v)
187738625Srvb    void *v;
187838625Srvb{
187938625Srvb    /* XXX on the global proc */
188038625Srvb/* true args */
188138625Srvb    struct vop_bmap_args *ap = v;
188238625Srvb    struct vnode *vp __attribute__((unused)) = ap->a_vp;	/* file's vnode */
188338625Srvb    daddr_t bn __attribute__((unused)) = ap->a_bn;	/* fs block number */
188438625Srvb    struct vnode **vpp = ap->a_vpp;			/* RETURN vp of device */
188538625Srvb    daddr_t *bnp __attribute__((unused)) = ap->a_bnp;	/* RETURN device block number */
188638625Srvb    struct proc *p __attribute__((unused)) = curproc;
188738625Srvb/* upcall decl */
188838625Srvb/* locals */
188938625Srvb
189038625Srvb	int ret = 0;
189138625Srvb	struct cnode *cp;
189238625Srvb
189338625Srvb	cp = VTOC(vp);
189438625Srvb	if (cp->c_ovp) {
189538625Srvb		printf("cfs_bmap: container .. ");
189638625Srvb		ret =  VOP_BMAP(cp->c_ovp, bn, vpp, bnp, ap->a_runp, ap->a_runb);
189738625Srvb		printf("VOP_BMAP(cp->c_ovp %p, bn %p, vpp %p, bnp %p, ap->a_runp %p, ap->a_runb %p) = %d\n",
189838625Srvb			cp->c_ovp, bn, vpp, bnp, ap->a_runp, ap->a_runb, ret);
189938625Srvb		return ret;
190038625Srvb	} else {
190138625Srvb		printf("cfs_bmap: no container\n");
190238625Srvb		return(EOPNOTSUPP);
190338625Srvb	}
190438625Srvb}
190538625Srvb
190638625Srvb/*
190738625Srvb * I don't think the following two things are used anywhere, so I've
190838625Srvb * commented them out
190938625Srvb *
191038625Srvb * struct buf *async_bufhead;
191138625Srvb * int async_daemon_count;
191238625Srvb */
191338625Srvbint
191438625Srvbcfs_strategy(v)
191538625Srvb    void *v;
191638625Srvb{
191738625Srvb/* true args */
191838625Srvb    struct vop_strategy_args *ap = v;
191938625Srvb    register struct buf *bp __attribute__((unused)) = ap->a_bp;
192038625Srvb    struct proc *p __attribute__((unused)) = curproc;
192138625Srvb/* upcall decl */
192238625Srvb/* locals */
192338625Srvb
192438625Srvb	printf("cfs_strategy: called ???\n");
192538625Srvb	return(EOPNOTSUPP);
192638625Srvb}
192738625Srvb
192838625Srvbint
192938625Srvbcfs_reclaim(v)
193038625Srvb    void *v;
193138625Srvb{
193238625Srvb/* true args */
193338625Srvb    struct vop_reclaim_args *ap = v;
193438625Srvb    struct vnode *vp = ap->a_vp;
193538625Srvb    struct cnode *cp = VTOC(vp);
193638625Srvb/* upcall decl */
193738625Srvb/* locals */
193838625Srvb
193938625Srvb/*
194038625Srvb * Forced unmount/flush will let vnodes with non zero use be destroyed!
194138625Srvb */
194238625Srvb    ENTRY;
194338625Srvb
194438625Srvb    if (IS_UNMOUNTING(cp)) {
194538625Srvb#ifdef	DEBUG
194638625Srvb	if (VTOC(vp)->c_ovp) {
194738625Srvb	    if (IS_UNMOUNTING(cp))
194838625Srvb		printf("cfs_reclaim: c_ovp not void: vp %p, cp %p\n", vp, cp);
194938625Srvb	}
195038625Srvb#endif
195138625Srvb    } else {
195238625Srvb#ifdef DIAGNOSTIC
195338625Srvb	if (vp->v_usecount != 0)
195438625Srvb	    vprint("cfs_reclaim: pushing active", vp);
195538625Srvb	if (VTOC(vp)->c_ovp) {
195638625Srvb	    panic("cfs_reclaim: c_ovp not void");
195738625Srvb    }
195838625Srvb#endif DIAGNOSTIC
195938625Srvb    }
196038625Srvb    cache_purge(vp);
196138625Srvb    cfs_free(VTOC(vp));
196238625Srvb    VTOC(vp) = NULL;
196338625Srvb    return (0);
196438625Srvb}
196538625Srvb
196638625Srvbint
196738625Srvbcfs_lock(v)
196838625Srvb    void *v;
196938625Srvb{
197038625Srvb/* true args */
197138625Srvb    struct vop_lock_args *ap = v;
197238625Srvb    struct vnode *vp = ap->a_vp;
197338625Srvb    struct cnode *cp = VTOC(vp);
197438625Srvb    struct proc  *p = ap->a_p;
197538625Srvb/* upcall decl */
197638625Srvb/* locals */
197738625Srvb
197838625Srvb    ENTRY;
197938625Srvb
198038625Srvb    if (cfs_lockdebug) {
198138625Srvb	myprintf(("Attempting lock on %lx.%lx.%lx\n",
198238625Srvb		  cp->c_fid.Volume, cp->c_fid.Vnode, cp->c_fid.Unique));
198338625Srvb    }
198438625Srvb
198538759Srvb    return (lockmgr(&cp->c_lock, ap->a_flags, &vp->v_interlock, p));
198638625Srvb}
198738625Srvb
198838625Srvbint
198938625Srvbcfs_unlock(v)
199038625Srvb    void *v;
199138625Srvb{
199238625Srvb/* true args */
199338625Srvb    struct vop_unlock_args *ap = v;
199438625Srvb    struct vnode *vp = ap->a_vp;
199538625Srvb    struct cnode *cp = VTOC(vp);
199638625Srvb    struct proc  *p = ap->a_p;
199738625Srvb/* upcall decl */
199838625Srvb/* locals */
199938625Srvb
200038625Srvb    ENTRY;
200138625Srvb    if (cfs_lockdebug) {
200238625Srvb	myprintf(("Attempting unlock on %lx.%lx.%lx\n",
200338625Srvb		  cp->c_fid.Volume, cp->c_fid.Vnode, cp->c_fid.Unique));
200438625Srvb    }
200538625Srvb
200638759Srvb    return (lockmgr(&cp->c_lock, ap->a_flags | LK_RELEASE, &vp->v_interlock, p));
200738625Srvb}
200838625Srvb
200938625Srvbint
201038625Srvbcfs_islocked(v)
201138625Srvb    void *v;
201238625Srvb{
201338625Srvb/* true args */
201438625Srvb    struct vop_islocked_args *ap = v;
201538625Srvb    struct cnode *cp = VTOC(ap->a_vp);
201638625Srvb    ENTRY;
201738625Srvb
201838625Srvb    return (lockstatus(&cp->c_lock));
201938625Srvb}
202038625Srvb
202138625Srvb/* How one looks up a vnode given a device/inode pair: */
202238625Srvbint
202338625Srvbcfs_grab_vnode(dev_t dev, ino_t ino, struct vnode **vpp)
202438625Srvb{
202538625Srvb    /* This is like VFS_VGET() or igetinode()! */
202638625Srvb    int           error;
202738625Srvb    struct mount *mp;
202838625Srvb
202938625Srvb    if (!(mp = devtomp(dev))) {
203038625Srvb	myprintf(("cfs_grab_vnode: devtomp(%d) returns NULL\n", dev));
203138625Srvb	return(ENXIO);
203238625Srvb    }
203338625Srvb
203438625Srvb    /* XXX - ensure that nonzero-return means failure */
203538625Srvb    error = VFS_VGET(mp,ino,vpp);
203638625Srvb    if (error) {
203738625Srvb	myprintf(("cfs_grab_vnode: iget/vget(%d, %d) returns %p, err %d\n",
203838625Srvb		  dev, ino, *vpp, error));
203938625Srvb	return(ENOENT);
204038625Srvb    }
204138625Srvb    return(0);
204238625Srvb}
204338625Srvb
204438625Srvbvoid
204538625Srvbprint_vattr( attr )
204638625Srvb	struct vattr *attr;
204738625Srvb{
204838625Srvb    char *typestr;
204938625Srvb
205038625Srvb    switch (attr->va_type) {
205138625Srvb    case VNON:
205238625Srvb	typestr = "VNON";
205338625Srvb	break;
205438625Srvb    case VREG:
205538625Srvb	typestr = "VREG";
205638625Srvb	break;
205738625Srvb    case VDIR:
205838625Srvb	typestr = "VDIR";
205938625Srvb	break;
206038625Srvb    case VBLK:
206138625Srvb	typestr = "VBLK";
206238625Srvb	break;
206338625Srvb    case VCHR:
206438625Srvb	typestr = "VCHR";
206538625Srvb	break;
206638625Srvb    case VLNK:
206738625Srvb	typestr = "VLNK";
206838625Srvb	break;
206938625Srvb    case VSOCK:
207038625Srvb	typestr = "VSCK";
207138625Srvb	break;
207238625Srvb    case VFIFO:
207338625Srvb	typestr = "VFFO";
207438625Srvb	break;
207538625Srvb    case VBAD:
207638625Srvb	typestr = "VBAD";
207738625Srvb	break;
207838625Srvb    default:
207938625Srvb	typestr = "????";
208038625Srvb	break;
208138625Srvb    }
208238625Srvb
208338625Srvb
208438625Srvb    myprintf(("attr: type %s mode %d uid %d gid %d fsid %d rdev %d\n",
208538625Srvb	      typestr, (int)attr->va_mode, (int)attr->va_uid,
208638625Srvb	      (int)attr->va_gid, (int)attr->va_fsid, (int)attr->va_rdev));
208738625Srvb
208838625Srvb    myprintf(("      fileid %d nlink %d size %d blocksize %d bytes %d\n",
208938625Srvb	      (int)attr->va_fileid, (int)attr->va_nlink,
209038625Srvb	      (int)attr->va_size,
209138625Srvb	      (int)attr->va_blocksize,(int)attr->va_bytes));
209238625Srvb    myprintf(("      gen %ld flags %ld vaflags %d\n",
209338625Srvb	      attr->va_gen, attr->va_flags, attr->va_vaflags));
209438625Srvb    myprintf(("      atime sec %d nsec %d\n",
209538625Srvb	      (int)attr->va_atime.tv_sec, (int)attr->va_atime.tv_nsec));
209638625Srvb    myprintf(("      mtime sec %d nsec %d\n",
209738625Srvb	      (int)attr->va_mtime.tv_sec, (int)attr->va_mtime.tv_nsec));
209838625Srvb    myprintf(("      ctime sec %d nsec %d\n",
209938625Srvb	      (int)attr->va_ctime.tv_sec, (int)attr->va_ctime.tv_nsec));
210038625Srvb}
210138625Srvb
210238625Srvb/* How to print a ucred */
210338625Srvbvoid
210438625Srvbprint_cred(cred)
210538625Srvb	struct ucred *cred;
210638625Srvb{
210738625Srvb
210838625Srvb	int i;
210938625Srvb
211038625Srvb	myprintf(("ref %d\tuid %d\n",cred->cr_ref,cred->cr_uid));
211138625Srvb
211238625Srvb	for (i=0; i < cred->cr_ngroups; i++)
211338625Srvb		myprintf(("\tgroup %d: (%d)\n",i,cred->cr_groups[i]));
211438625Srvb	myprintf(("\n"));
211538625Srvb
211638625Srvb}
211738625Srvb
211838625Srvb/*
211938625Srvb * Return a vnode for the given fid.
212038625Srvb * If no cnode exists for this fid create one and put it
212138625Srvb * in a table hashed by fid.Volume and fid.Vnode.  If the cnode for
212238625Srvb * this fid is already in the table return it (ref count is
212338625Srvb * incremented by cfs_find.  The cnode will be flushed from the
212438625Srvb * table when cfs_inactive calls cfs_unsave.
212538625Srvb */
212638625Srvbstruct cnode *
212738625Srvbmakecfsnode(fid, vfsp, type)
212838625Srvb     ViceFid *fid; struct mount *vfsp; short type;
212938625Srvb{
213038625Srvb    struct cnode *cp;
213138625Srvb    int          err;
213238625Srvb
213338625Srvb    if ((cp = cfs_find(fid)) == NULL) {
213438625Srvb	struct vnode *vp;
213538625Srvb
213638625Srvb	cp = cfs_alloc();
213738625Srvb	lockinit(&cp->c_lock, PINOD, "cnode", 0, 0);
213838625Srvb	cp->c_fid = *fid;
213938625Srvb
214038625Srvb	err = getnewvnode(VT_CFS, vfsp, cfs_vnodeop_p, &vp);
214138625Srvb	if (err) {
214238625Srvb	    panic("cfs: getnewvnode returned error %d\n", err);
214338625Srvb	}
214438625Srvb	vp->v_data = cp;
214538625Srvb	vp->v_type = type;
214638625Srvb	cp->c_vnode = vp;
214738625Srvb	cfs_save(cp);
214838625Srvb
214938625Srvb    } else {
215038625Srvb	vref(CTOV(cp));
215138625Srvb    }
215238625Srvb
215338625Srvb    return cp;
215438625Srvb}
2155