coda_vnops.c revision 54655
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 $
3050477Speter * $FreeBSD: head/sys/fs/coda/coda_vnops.c 54655 1999-12-15 23:02:35Z eivind $
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#include <sys/param.h>
4938625Srvb#include <sys/systm.h>
5038759Srvb#include <sys/kernel.h>
5138759Srvb#include <sys/proc.h>
5238625Srvb#include <sys/malloc.h>
5338759Srvb#include <sys/mount.h>
5438625Srvb#include <sys/errno.h>
5538625Srvb#include <sys/acct.h>
5638625Srvb#include <sys/file.h>
5738759Srvb#include <sys/fcntl.h>
5838625Srvb#include <sys/uio.h>
5938625Srvb#include <sys/namei.h>
6038625Srvb#include <sys/ioccom.h>
6138625Srvb#include <sys/select.h>
6238759Srvb
6338625Srvb#include <vm/vm.h>
6438625Srvb#include <vm/vm_object.h>
6538625Srvb#include <vm/vm_extern.h>
6638759Srvb#include <vm/vm_zone.h>
6738625Srvb
6839126Srvb#include <coda/coda.h>
6939126Srvb#include <coda/cnode.h>
7039126Srvb#include <coda/coda_vnops.h>
7139126Srvb#include <coda/coda_venus.h>
7239126Srvb#include <coda/coda_opstats.h>
7339126Srvb#include <coda/coda_subr.h>
7439126Srvb#include <coda/coda_namecache.h>
7539126Srvb#include <coda/coda_pioctl.h>
7638625Srvb
7738625Srvb/*
7838625Srvb * These flags select various performance enhancements.
7938625Srvb */
8039085Srvbint coda_attr_cache  = 1;       /* Set to cache attributes in the kernel */
8139085Srvbint coda_symlink_cache = 1;     /* Set to cache symbolic link information */
8239085Srvbint coda_access_cache = 1;      /* Set to handle some access checks directly */
8338625Srvb
8438625Srvb/* structure to keep track of vfs calls */
8538625Srvb
8639085Srvbstruct coda_op_stats coda_vnodeopstats[CODA_VNODEOPS_SIZE];
8738625Srvb
8839085Srvb#define MARK_ENTRY(op) (coda_vnodeopstats[op].entries++)
8939085Srvb#define MARK_INT_SAT(op) (coda_vnodeopstats[op].sat_intrn++)
9039085Srvb#define MARK_INT_FAIL(op) (coda_vnodeopstats[op].unsat_intrn++)
9139085Srvb#define MARK_INT_GEN(op) (coda_vnodeopstats[op].gen_intrn++)
9238625Srvb
9338625Srvb/* What we are delaying for in printf */
9439085Srvbint coda_printf_delay = 0;  /* in microseconds */
9539085Srvbint coda_vnop_print_entry = 0;
9639085Srvbstatic int coda_lockdebug = 0;
9738625Srvb
9838625Srvb/* Definition of the vfs operation vector */
9938625Srvb
10038625Srvb/*
10138625Srvb * Some NetBSD details:
10238625Srvb *
10339085Srvb *   coda_start is called at the end of the mount syscall.
10439085Srvb *   coda_init is called at boot time.
10538625Srvb */
10638625Srvb
10739085Srvb#define ENTRY  if(coda_vnop_print_entry) myprintf(("Entered %s\n",__FUNCTION__))
10838625Srvb
10938625Srvb/* Definition of the vnode operation vector */
11038625Srvb
11139085Srvbstruct vnodeopv_entry_desc coda_vnodeop_entries[] = {
11239085Srvb    { &vop_default_desc, coda_vop_error },
11339085Srvb    { &vop_lookup_desc, coda_lookup },		/* lookup */
11439085Srvb    { &vop_create_desc, coda_create },		/* create */
11539650Srvb    { &vop_mknod_desc, coda_vop_error },	/* mknod */
11639085Srvb    { &vop_open_desc, coda_open },		/* open */
11739085Srvb    { &vop_close_desc, coda_close },		/* close */
11839085Srvb    { &vop_access_desc, coda_access },		/* access */
11939650Srvb    { &vop_getattr_desc, coda_getattr },	/* getattr */
12039650Srvb    { &vop_setattr_desc, coda_setattr },	/* setattr */
12139085Srvb    { &vop_read_desc, coda_read },		/* read */
12239085Srvb    { &vop_write_desc, coda_write },		/* write */
12339085Srvb    { &vop_ioctl_desc, coda_ioctl },		/* ioctl */
12439650Srvb    { &vop_mmap_desc, coda_vop_error },		/* mmap */
12539085Srvb    { &vop_fsync_desc, coda_fsync },		/* fsync */
12639085Srvb    { &vop_remove_desc, coda_remove },		/* remove */
12739085Srvb    { &vop_link_desc, coda_link },		/* link */
12839085Srvb    { &vop_rename_desc, coda_rename },		/* rename */
12939085Srvb    { &vop_mkdir_desc, coda_mkdir },		/* mkdir */
13039085Srvb    { &vop_rmdir_desc, coda_rmdir },		/* rmdir */
13139650Srvb    { &vop_symlink_desc, coda_symlink },	/* symlink */
13239650Srvb    { &vop_readdir_desc, coda_readdir },	/* readdir */
13339085Srvb    { &vop_readlink_desc, coda_readlink },	/* readlink */
13439085Srvb    { &vop_inactive_desc, coda_inactive },	/* inactive */
13539650Srvb    { &vop_reclaim_desc, coda_reclaim },	/* reclaim */
13639085Srvb    { &vop_lock_desc, coda_lock },		/* lock */
13739085Srvb    { &vop_unlock_desc, coda_unlock },		/* unlock */
13839085Srvb    { &vop_bmap_desc, coda_bmap },		/* bmap */
13939085Srvb    { &vop_strategy_desc, coda_strategy },	/* strategy */
14039650Srvb    { &vop_print_desc, coda_vop_error },	/* print */
14139085Srvb    { &vop_islocked_desc, coda_islocked },	/* islocked */
14239650Srvb    { &vop_pathconf_desc, coda_vop_error },	/* pathconf */
14339650Srvb    { &vop_advlock_desc, coda_vop_nop },	/* advlock */
14439650Srvb    { &vop_bwrite_desc, coda_vop_error },	/* bwrite */
14539650Srvb    { &vop_lease_desc, coda_vop_nop },		/* lease */
14638759Srvb    { &vop_poll_desc, (vop_t *) vop_stdpoll },
14739085Srvb    { &vop_getpages_desc, coda_fbsd_getpages },	/* pager intf.*/
14839085Srvb    { &vop_putpages_desc, coda_fbsd_putpages },	/* pager intf.*/
14938625Srvb
15038759Srvb#if	0
15138625Srvb
15238759Srvb    we need to define these someday
15338625Srvb#define UFS_BLKATOFF(aa, bb, cc, dd) VFSTOUFS((aa)->v_mount)->um_blkatoff(aa, bb, cc, dd)
15438625Srvb#define UFS_VALLOC(aa, bb, cc, dd) VFSTOUFS((aa)->v_mount)->um_valloc(aa, bb, cc, dd)
15538625Srvb#define UFS_VFREE(aa, bb, cc) VFSTOUFS((aa)->v_mount)->um_vfree(aa, bb, cc)
15638625Srvb#define UFS_TRUNCATE(aa, bb, cc, dd, ee) VFSTOUFS((aa)->v_mount)->um_truncate(aa, bb, cc, dd, ee)
15742374Sbde#define UFS_UPDATE(aa, bb) VFSTOUFS((aa)->v_mount)->um_update(aa, bb)
15838625Srvb
15938625Srvb    missing
16038625Srvb    { &vop_reallocblks_desc,	(vop_t *) ufs_missingop },
16138625Srvb    { &vop_cachedlookup_desc,	(vop_t *) ufs_lookup },
16238625Srvb    { &vop_whiteout_desc,	(vop_t *) ufs_whiteout },
16338625Srvb#endif
16438625Srvb    { (struct vnodeop_desc*)NULL, (int(*)(void *))NULL }
16538625Srvb};
16638625Srvb
16739085Srvbstatic struct vnodeopv_desc coda_vnodeop_opv_desc =
16839085Srvb		{ &coda_vnodeop_p, coda_vnodeop_entries };
16938625Srvb
17039085SrvbVNODEOP_SET(coda_vnodeop_opv_desc);
17138625Srvb
17238625Srvb/* A generic panic: we were called with something we didn't define yet */
17338625Srvbint
17439085Srvbcoda_vop_error(void *anon) {
17538625Srvb    struct vnodeop_desc **desc = (struct vnodeop_desc **)anon;
17638625Srvb
17741202Srvb    myprintf(("coda_vop_error: Vnode operation %s called, but not defined.\n",
17838625Srvb	      (*desc)->vdesc_name));
17941202Srvb    /*
18039085Srvb    panic("coda_vop_error");
18141202Srvb    */
18241202Srvb    return EIO;
18338625Srvb}
18438625Srvb
18538625Srvb/* A generic do-nothing.  For lease_check, advlock */
18638625Srvbint
18739085Srvbcoda_vop_nop(void *anon) {
18838625Srvb    struct vnodeop_desc **desc = (struct vnodeop_desc **)anon;
18938625Srvb
19039085Srvb    if (codadebug) {
19138625Srvb	myprintf(("Vnode operation %s called, but unsupported\n",
19238625Srvb		  (*desc)->vdesc_name));
19338625Srvb    }
19438625Srvb   return (0);
19538625Srvb}
19638625Srvb
19738625Srvbint
19839085Srvbcoda_vnodeopstats_init(void)
19938625Srvb{
20038625Srvb	register int i;
20138625Srvb
20239085Srvb	for(i=0;i<CODA_VNODEOPS_SIZE;i++) {
20339085Srvb		coda_vnodeopstats[i].opcode = i;
20439085Srvb		coda_vnodeopstats[i].entries = 0;
20539085Srvb		coda_vnodeopstats[i].sat_intrn = 0;
20639085Srvb		coda_vnodeopstats[i].unsat_intrn = 0;
20739085Srvb		coda_vnodeopstats[i].gen_intrn = 0;
20838625Srvb	}
20938625Srvb	return 0;
21038625Srvb}
21138625Srvb
21238625Srvb/*
21339085Srvb * coda_open calls Venus to return the device, inode pair of the cache
21439085Srvb * file holding the data. Using iget, coda_open finds the vnode of the
21538625Srvb * cache file, and then opens it.
21638625Srvb */
21738625Srvbint
21839085Srvbcoda_open(v)
21938625Srvb    void *v;
22038625Srvb{
22138625Srvb    /*
22238625Srvb     * NetBSD can pass the O_EXCL flag in mode, even though the check
22338625Srvb     * has already happened.  Venus defensively assumes that if open
22438625Srvb     * is passed the EXCL, it must be a bug.  We strip the flag here.
22538625Srvb     */
22638625Srvb/* true args */
22738625Srvb    struct vop_open_args *ap = v;
22838625Srvb    register struct vnode **vpp = &(ap->a_vp);
22938625Srvb    struct cnode *cp = VTOC(*vpp);
23038625Srvb    int flag = ap->a_mode & (~O_EXCL);
23138625Srvb    struct ucred *cred = ap->a_cred;
23238625Srvb    struct proc *p = ap->a_p;
23338625Srvb/* locals */
23438625Srvb    int error;
23538625Srvb    struct vnode *vp;
23638625Srvb    dev_t dev;
23738625Srvb    ino_t inode;
23838625Srvb
23939085Srvb    MARK_ENTRY(CODA_OPEN_STATS);
24038625Srvb
24138625Srvb    /* Check for open of control file. */
24238625Srvb    if (IS_CTL_VP(*vpp)) {
24338625Srvb	/* XXX */
24438625Srvb	/* if (WRITEABLE(flag)) */
24538625Srvb	if (flag & (FWRITE | O_TRUNC | O_CREAT | O_EXCL)) {
24639085Srvb	    MARK_INT_FAIL(CODA_OPEN_STATS);
24738625Srvb	    return(EACCES);
24838625Srvb	}
24939085Srvb	MARK_INT_SAT(CODA_OPEN_STATS);
25038625Srvb	return(0);
25138625Srvb    }
25238625Srvb
25338625Srvb    error = venus_open(vtomi((*vpp)), &cp->c_fid, flag, cred, p, &dev, &inode);
25438625Srvb    if (error)
25538625Srvb	return (error);
25638625Srvb    if (!error) {
25749524Sbde	CODADEBUG( CODA_OPEN,myprintf(("open: dev %#lx inode %lu result %d\n",
25849524Sbde				       (u_long)dev2udev(dev), (u_long)inode,
25949524Sbde				       error)); )
26038625Srvb    }
26138625Srvb
26238625Srvb    /* Translate the <device, inode> pair for the cache file into
26338625Srvb       an inode pointer. */
26439085Srvb    error = coda_grab_vnode(dev, inode, &vp);
26538625Srvb    if (error)
26638625Srvb	return (error);
26738625Srvb
26838759Srvb    /* We get the vnode back locked.  Needs unlocked */
26938759Srvb    VOP_UNLOCK(vp, 0, p);
27038625Srvb    /* Keep a reference until the close comes in. */
27138625Srvb    vref(*vpp);
27238625Srvb
27338625Srvb    /* Save the vnode pointer for the cache file. */
27438625Srvb    if (cp->c_ovp == NULL) {
27538625Srvb	cp->c_ovp = vp;
27638625Srvb    } else {
27738625Srvb	if (cp->c_ovp != vp)
27839085Srvb	    panic("coda_open:  cp->c_ovp != ITOV(ip)");
27938625Srvb    }
28038625Srvb    cp->c_ocount++;
28138625Srvb
28238625Srvb    /* Flush the attribute cached if writing the file. */
28338625Srvb    if (flag & FWRITE) {
28438625Srvb	cp->c_owrite++;
28538625Srvb	cp->c_flags &= ~C_VATTR;
28638625Srvb    }
28738625Srvb
28838625Srvb    /* Save the <device, inode> pair for the cache file to speed
28938625Srvb       up subsequent page_read's. */
29038625Srvb    cp->c_device = dev;
29138625Srvb    cp->c_inode = inode;
29238625Srvb
29338625Srvb    /* Open the cache file. */
29438625Srvb    error = VOP_OPEN(vp, flag, cred, p);
29538625Srvb    if (error) {
29639085Srvb    	printf("coda_open: VOP_OPEN on container failed %d\n", error);
29738625Srvb	return (error);
29838625Srvb    }
29939650Srvb/* grab (above) does this when it calls newvnode unless it's in the cache*/
30038625Srvb    if (vp->v_type == VREG) {
30142315Seivind    	error = vfs_object_create(vp, p, cred);
30238625Srvb	if (error != 0) {
30339085Srvb	    printf("coda_open: vfs_object_create() returns %d\n", error);
30438625Srvb	    vput(vp);
30538625Srvb	}
30638625Srvb    }
30739650Srvb
30838625Srvb    return(error);
30938625Srvb}
31038625Srvb
31138625Srvb/*
31238625Srvb * Close the cache file used for I/O and notify Venus.
31338625Srvb */
31438625Srvbint
31539085Srvbcoda_close(v)
31638625Srvb    void *v;
31738625Srvb{
31838625Srvb/* true args */
31938625Srvb    struct vop_close_args *ap = v;
32038625Srvb    struct vnode *vp = ap->a_vp;
32138625Srvb    struct cnode *cp = VTOC(vp);
32238625Srvb    int flag = ap->a_fflag;
32338625Srvb    struct ucred *cred = ap->a_cred;
32438625Srvb    struct proc *p = ap->a_p;
32538625Srvb/* locals */
32638625Srvb    int error;
32738625Srvb
32839085Srvb    MARK_ENTRY(CODA_CLOSE_STATS);
32938625Srvb
33038625Srvb    /* Check for close of control file. */
33138625Srvb    if (IS_CTL_VP(vp)) {
33239085Srvb	MARK_INT_SAT(CODA_CLOSE_STATS);
33338625Srvb	return(0);
33438625Srvb    }
33538625Srvb
33638625Srvb    if (IS_UNMOUNTING(cp)) {
33738625Srvb	if (cp->c_ovp) {
33839728Srvb#ifdef	CODA_VERBOSE
33939085Srvb	    printf("coda_close: destroying container ref %d, ufs vp %p of vp %p/cp %p\n",
34038625Srvb		    vp->v_usecount, cp->c_ovp, vp, cp);
34139650Srvb#endif
34240708Srvb#ifdef	hmm
34338625Srvb	    vgone(cp->c_ovp);
34440708Srvb#else
34540708Srvb	    VOP_CLOSE(cp->c_ovp, flag, cred, p); /* Do errors matter here? */
34640708Srvb	    vrele(cp->c_ovp);
34740708Srvb#endif
34838625Srvb	} else {
34939728Srvb#ifdef	CODA_VERBOSE
35039085Srvb	    printf("coda_close: NO container vp %p/cp %p\n", vp, cp);
35139650Srvb#endif
35238625Srvb	}
35338625Srvb	return ENODEV;
35438625Srvb    } else {
35538625Srvb	VOP_CLOSE(cp->c_ovp, flag, cred, p); /* Do errors matter here? */
35638625Srvb	vrele(cp->c_ovp);
35738625Srvb    }
35838625Srvb
35938625Srvb    if (--cp->c_ocount == 0)
36038625Srvb	cp->c_ovp = NULL;
36138625Srvb
36238625Srvb    if (flag & FWRITE)                    /* file was opened for write */
36338625Srvb	--cp->c_owrite;
36438625Srvb
36538625Srvb    error = venus_close(vtomi(vp), &cp->c_fid, flag, cred, p);
36638625Srvb    vrele(CTOV(cp));
36738625Srvb
36839085Srvb    CODADEBUG(CODA_CLOSE, myprintf(("close: result %d\n",error)); )
36938625Srvb    return(error);
37038625Srvb}
37138625Srvb
37238625Srvbint
37339085Srvbcoda_read(v)
37438625Srvb    void *v;
37538625Srvb{
37638625Srvb    struct vop_read_args *ap = v;
37738625Srvb
37838625Srvb    ENTRY;
37939085Srvb    return(coda_rdwr(ap->a_vp, ap->a_uio, UIO_READ,
38038625Srvb		    ap->a_ioflag, ap->a_cred, ap->a_uio->uio_procp));
38138625Srvb}
38238625Srvb
38338625Srvbint
38439085Srvbcoda_write(v)
38538625Srvb    void *v;
38638625Srvb{
38738625Srvb    struct vop_write_args *ap = v;
38838625Srvb
38938625Srvb    ENTRY;
39039085Srvb    return(coda_rdwr(ap->a_vp, ap->a_uio, UIO_WRITE,
39138625Srvb		    ap->a_ioflag, ap->a_cred, ap->a_uio->uio_procp));
39238625Srvb}
39338625Srvb
39438625Srvbint
39539085Srvbcoda_rdwr(vp, uiop, rw, ioflag, cred, p)
39638625Srvb    struct vnode *vp;
39738625Srvb    struct uio *uiop;
39838625Srvb    enum uio_rw rw;
39938625Srvb    int ioflag;
40038625Srvb    struct ucred *cred;
40138625Srvb    struct proc *p;
40238625Srvb{
40338625Srvb/* upcall decl */
40438625Srvb  /* NOTE: container file operation!!! */
40538625Srvb/* locals */
40638625Srvb    struct cnode *cp = VTOC(vp);
40738625Srvb    struct vnode *cfvp = cp->c_ovp;
40838625Srvb    int igot_internally = 0;
40938625Srvb    int opened_internally = 0;
41038625Srvb    int error = 0;
41138625Srvb
41239085Srvb    MARK_ENTRY(CODA_RDWR_STATS);
41338625Srvb
41449524Sbde    CODADEBUG(CODA_RDWR, myprintf(("coda_rdwr(%d, %p, %d, %lld, %d)\n", rw,
41549524Sbde			      (void *)uiop->uio_iov->iov_base, uiop->uio_resid,
41649524Sbde			      (long long)uiop->uio_offset, uiop->uio_segflg)); )
41738625Srvb
41838625Srvb    /* Check for rdwr of control object. */
41938625Srvb    if (IS_CTL_VP(vp)) {
42039085Srvb	MARK_INT_FAIL(CODA_RDWR_STATS);
42138625Srvb	return(EINVAL);
42238625Srvb    }
42338625Srvb
42438625Srvb    /*
42538625Srvb     * If file is not already open this must be a page
42638625Srvb     * {read,write} request.  Iget the cache file's inode
42738625Srvb     * pointer if we still have its <device, inode> pair.
42838625Srvb     * Otherwise, we must do an internal open to derive the
42938625Srvb     * pair.
43038625Srvb     */
43138625Srvb    if (cfvp == NULL) {
43238625Srvb	/*
43338625Srvb	 * If we're dumping core, do the internal open. Otherwise
43438625Srvb	 * venus won't have the correct size of the core when
43538625Srvb	 * it's completely written.
43638625Srvb	 */
43738625Srvb	if (cp->c_inode != 0 && !(p && (p->p_acflag & ACORE))) {
43838625Srvb	    igot_internally = 1;
43939085Srvb	    error = coda_grab_vnode(cp->c_device, cp->c_inode, &cfvp);
44038625Srvb	    if (error) {
44139085Srvb		MARK_INT_FAIL(CODA_RDWR_STATS);
44238625Srvb		return(error);
44338625Srvb	    }
44438625Srvb	    /*
44538625Srvb	     * We get the vnode back locked in both Mach and
44638625Srvb	     * NetBSD.  Needs unlocked
44738625Srvb	     */
44838759Srvb	    VOP_UNLOCK(cfvp, 0, p);
44938625Srvb	}
45038625Srvb	else {
45138625Srvb	    opened_internally = 1;
45239085Srvb	    MARK_INT_GEN(CODA_OPEN_STATS);
45338625Srvb	    error = VOP_OPEN(vp, (rw == UIO_READ ? FREAD : FWRITE),
45438625Srvb			     cred, p);
45539085Srvbprintf("coda_rdwr: Internally Opening %p\n", vp);
45638625Srvb	    if (error) {
45739085Srvb		printf("coda_rdwr: VOP_OPEN on container failed %d\n", error);
45838625Srvb		return (error);
45938625Srvb	    }
46038625Srvb	    if (vp->v_type == VREG) {
46142315Seivind		error = vfs_object_create(vp, p, cred);
46238625Srvb		if (error != 0) {
46339085Srvb		    printf("coda_rdwr: vfs_object_create() returns %d\n", error);
46438625Srvb		    vput(vp);
46538625Srvb		}
46638625Srvb	    }
46738625Srvb	    if (error) {
46839085Srvb		MARK_INT_FAIL(CODA_RDWR_STATS);
46938625Srvb		return(error);
47038625Srvb	    }
47138625Srvb	    cfvp = cp->c_ovp;
47238625Srvb	}
47338625Srvb    }
47438625Srvb
47538625Srvb    /* Have UFS handle the call. */
47639085Srvb    CODADEBUG(CODA_RDWR, myprintf(("indirect rdwr: fid = (%lx.%lx.%lx), refcnt = %d\n",
47738625Srvb			      cp->c_fid.Volume, cp->c_fid.Vnode,
47838625Srvb			      cp->c_fid.Unique, CTOV(cp)->v_usecount)); )
47938625Srvb
48039650Srvb
48138625Srvb    if (rw == UIO_READ) {
48238625Srvb	error = VOP_READ(cfvp, uiop, ioflag, cred);
48338625Srvb    } else {
48438625Srvb	error = VOP_WRITE(cfvp, uiop, ioflag, cred);
48538625Srvb	/* ufs_write updates the vnode_pager_setsize for the vnode/object */
48639650Srvb
48738625Srvb	{   struct vattr attr;
48838625Srvb
48938625Srvb	    if (VOP_GETATTR(cfvp, &attr, cred, p) == 0) {
49038625Srvb		vnode_pager_setsize(vp, attr.va_size);
49138625Srvb	    }
49238625Srvb	}
49338625Srvb    }
49438625Srvb
49538625Srvb    if (error)
49639085Srvb	MARK_INT_FAIL(CODA_RDWR_STATS);
49738625Srvb    else
49839085Srvb	MARK_INT_SAT(CODA_RDWR_STATS);
49938625Srvb
50038625Srvb    /* Do an internal close if necessary. */
50138625Srvb    if (opened_internally) {
50239085Srvb	MARK_INT_GEN(CODA_CLOSE_STATS);
50338625Srvb	(void)VOP_CLOSE(vp, (rw == UIO_READ ? FREAD : FWRITE), cred, p);
50438625Srvb    }
50538625Srvb
50638625Srvb    /* Invalidate cached attributes if writing. */
50738625Srvb    if (rw == UIO_WRITE)
50838625Srvb	cp->c_flags &= ~C_VATTR;
50938625Srvb    return(error);
51038625Srvb}
51138625Srvb
51238625Srvbint
51339085Srvbcoda_ioctl(v)
51438625Srvb    void *v;
51538625Srvb{
51638625Srvb/* true args */
51738625Srvb    struct vop_ioctl_args *ap = v;
51838625Srvb    struct vnode *vp = ap->a_vp;
51938625Srvb    int com = ap->a_command;
52038625Srvb    caddr_t data = ap->a_data;
52138625Srvb    int flag = ap->a_fflag;
52238625Srvb    struct ucred *cred = ap->a_cred;
52338625Srvb    struct proc  *p = ap->a_p;
52438625Srvb/* locals */
52538625Srvb    int error;
52638625Srvb    struct vnode *tvp;
52738625Srvb    struct nameidata ndp;
52838625Srvb    struct PioctlData *iap = (struct PioctlData *)data;
52938625Srvb
53039085Srvb    MARK_ENTRY(CODA_IOCTL_STATS);
53138625Srvb
53239085Srvb    CODADEBUG(CODA_IOCTL, myprintf(("in coda_ioctl on %s\n", iap->path));)
53338625Srvb
53438625Srvb    /* Don't check for operation on a dying object, for ctlvp it
53538625Srvb       shouldn't matter */
53638625Srvb
53738625Srvb    /* Must be control object to succeed. */
53838625Srvb    if (!IS_CTL_VP(vp)) {
53939085Srvb	MARK_INT_FAIL(CODA_IOCTL_STATS);
54039085Srvb	CODADEBUG(CODA_IOCTL, myprintf(("coda_ioctl error: vp != ctlvp"));)
54138625Srvb	    return (EOPNOTSUPP);
54238625Srvb    }
54338625Srvb    /* Look up the pathname. */
54438625Srvb
54538625Srvb    /* Should we use the name cache here? It would get it from
54638625Srvb       lookupname sooner or later anyway, right? */
54738625Srvb
54843382Sbde    NDINIT(&ndp, LOOKUP, (iap->follow ? FOLLOW : NOFOLLOW), UIO_USERSPACE, iap->path, p);
54938625Srvb    error = namei(&ndp);
55038625Srvb    tvp = ndp.ni_vp;
55138625Srvb
55238625Srvb    if (error) {
55339085Srvb	MARK_INT_FAIL(CODA_IOCTL_STATS);
55439085Srvb	CODADEBUG(CODA_IOCTL, myprintf(("coda_ioctl error: lookup returns %d\n",
55538625Srvb				   error));)
55638625Srvb	return(error);
55738625Srvb    }
55838625Srvb
55938625Srvb    /*
56038625Srvb     * Make sure this is a coda style cnode, but it may be a
56138625Srvb     * different vfsp
56238625Srvb     */
56349687Sphk    if (tvp->v_op != coda_vnodeop_p) {
56438625Srvb	vrele(tvp);
56554655Seivind	NDFREE(&ndp, NDF_ONLY_PNBUF);
56639085Srvb	MARK_INT_FAIL(CODA_IOCTL_STATS);
56739085Srvb	CODADEBUG(CODA_IOCTL,
56839085Srvb		 myprintf(("coda_ioctl error: %s not a coda object\n",
56938625Srvb			iap->path));)
57038625Srvb	return(EINVAL);
57138625Srvb    }
57238625Srvb
57338625Srvb    if (iap->vi.in_size > VC_MAXDATASIZE) {
57454655Seivind	NDFREE(&ndp, 0);
57538625Srvb	return(EINVAL);
57638625Srvb    }
57738625Srvb    error = venus_ioctl(vtomi(tvp), &((VTOC(tvp))->c_fid), com, flag, data, cred, p);
57838625Srvb
57938625Srvb    if (error)
58039085Srvb	MARK_INT_FAIL(CODA_IOCTL_STATS);
58138625Srvb    else
58239085Srvb	CODADEBUG(CODA_IOCTL, myprintf(("Ioctl returns %d \n", error)); )
58338625Srvb
58438625Srvb    vrele(tvp);
58554655Seivind    NDFREE(&ndp, NDF_ONLY_PNBUF);
58638625Srvb    return(error);
58738625Srvb}
58838625Srvb
58938625Srvb/*
59038625Srvb * To reduce the cost of a user-level venus;we cache attributes in
59138625Srvb * the kernel.  Each cnode has storage allocated for an attribute. If
59238625Srvb * c_vattr is valid, return a reference to it. Otherwise, get the
59338625Srvb * attributes from venus and store them in the cnode.  There is some
59438625Srvb * question if this method is a security leak. But I think that in
59538625Srvb * order to make this call, the user must have done a lookup and
59638625Srvb * opened the file, and therefore should already have access.
59738625Srvb */
59838625Srvbint
59939085Srvbcoda_getattr(v)
60038625Srvb    void *v;
60138625Srvb{
60238625Srvb/* true args */
60338625Srvb    struct vop_getattr_args *ap = v;
60438625Srvb    struct vnode *vp = ap->a_vp;
60538625Srvb    struct cnode *cp = VTOC(vp);
60638625Srvb    struct vattr *vap = ap->a_vap;
60738625Srvb    struct ucred *cred = ap->a_cred;
60838625Srvb    struct proc *p = ap->a_p;
60938625Srvb/* locals */
61038625Srvb    int error;
61138625Srvb
61239085Srvb    MARK_ENTRY(CODA_GETATTR_STATS);
61338625Srvb
61438625Srvb    if (IS_UNMOUNTING(cp))
61538625Srvb	return ENODEV;
61638759Srvb
61738625Srvb    /* Check for getattr of control object. */
61838625Srvb    if (IS_CTL_VP(vp)) {
61939085Srvb	MARK_INT_FAIL(CODA_GETATTR_STATS);
62038625Srvb	return(ENOENT);
62138625Srvb    }
62238625Srvb
62338625Srvb    /* Check to see if the attributes have already been cached */
62438625Srvb    if (VALID_VATTR(cp)) {
62539085Srvb	CODADEBUG(CODA_GETATTR, { myprintf(("attr cache hit: (%lx.%lx.%lx)\n",
62638625Srvb				       cp->c_fid.Volume,
62738625Srvb				       cp->c_fid.Vnode,
62838625Srvb				       cp->c_fid.Unique));});
62939085Srvb	CODADEBUG(CODA_GETATTR, if (!(codadebug & ~CODA_GETATTR))
63038625Srvb		 print_vattr(&cp->c_vattr); );
63138625Srvb
63238625Srvb	*vap = cp->c_vattr;
63339085Srvb	MARK_INT_SAT(CODA_GETATTR_STATS);
63438625Srvb	return(0);
63538625Srvb    }
63638625Srvb
63738625Srvb    error = venus_getattr(vtomi(vp), &cp->c_fid, cred, p, vap);
63838625Srvb
63938625Srvb    if (!error) {
64039085Srvb	CODADEBUG(CODA_GETATTR, myprintf(("getattr miss (%lx.%lx.%lx): result %d\n",
64138625Srvb				     cp->c_fid.Volume,
64238625Srvb				     cp->c_fid.Vnode,
64338625Srvb				     cp->c_fid.Unique,
64438625Srvb				     error)); )
64538625Srvb
64639085Srvb	CODADEBUG(CODA_GETATTR, if (!(codadebug & ~CODA_GETATTR))
64738625Srvb		 print_vattr(vap);	);
64838625Srvb
64938625Srvb    {	int size = vap->va_size;
65038625Srvb    	struct vnode *convp = cp->c_ovp;
65138625Srvb	if (convp != (struct vnode *)0) {
65238625Srvb	    vnode_pager_setsize(convp, size);
65338625Srvb	}
65438625Srvb    }
65538625Srvb	/* If not open for write, store attributes in cnode */
65639085Srvb	if ((cp->c_owrite == 0) && (coda_attr_cache)) {
65738625Srvb	    cp->c_vattr = *vap;
65838625Srvb	    cp->c_flags |= C_VATTR;
65938625Srvb	}
66038625Srvb
66138625Srvb    }
66238625Srvb    return(error);
66338625Srvb}
66438625Srvb
66538625Srvbint
66639085Srvbcoda_setattr(v)
66738625Srvb    void *v;
66838625Srvb{
66938625Srvb/* true args */
67038625Srvb    struct vop_setattr_args *ap = v;
67138625Srvb    register struct vnode *vp = ap->a_vp;
67238625Srvb    struct cnode *cp = VTOC(vp);
67338625Srvb    register struct vattr *vap = ap->a_vap;
67438625Srvb    struct ucred *cred = ap->a_cred;
67538625Srvb    struct proc *p = ap->a_p;
67638625Srvb/* locals */
67738625Srvb    int error;
67838625Srvb
67939085Srvb    MARK_ENTRY(CODA_SETATTR_STATS);
68038625Srvb
68138625Srvb    /* Check for setattr of control object. */
68238625Srvb    if (IS_CTL_VP(vp)) {
68339085Srvb	MARK_INT_FAIL(CODA_SETATTR_STATS);
68438625Srvb	return(ENOENT);
68538625Srvb    }
68638625Srvb
68739085Srvb    if (codadebug & CODADBGMSK(CODA_SETATTR)) {
68838625Srvb	print_vattr(vap);
68938625Srvb    }
69038625Srvb    error = venus_setattr(vtomi(vp), &cp->c_fid, vap, cred, p);
69138625Srvb
69238625Srvb    if (!error)
69338625Srvb	cp->c_flags &= ~C_VATTR;
69438625Srvb
69538625Srvb    {	int size = vap->va_size;
69638625Srvb    	struct vnode *convp = cp->c_ovp;
69738625Srvb	if (size != VNOVAL && convp != (struct vnode *)0) {
69838625Srvb	    vnode_pager_setsize(convp, size);
69938625Srvb	}
70038625Srvb    }
70139085Srvb    CODADEBUG(CODA_SETATTR,	myprintf(("setattr %d\n", error)); )
70238625Srvb    return(error);
70338625Srvb}
70438625Srvb
70538625Srvbint
70639085Srvbcoda_access(v)
70738625Srvb    void *v;
70838625Srvb{
70938625Srvb/* true args */
71038625Srvb    struct vop_access_args *ap = v;
71138625Srvb    struct vnode *vp = ap->a_vp;
71238625Srvb    struct cnode *cp = VTOC(vp);
71338625Srvb    int mode = ap->a_mode;
71438625Srvb    struct ucred *cred = ap->a_cred;
71538625Srvb    struct proc *p = ap->a_p;
71638625Srvb/* locals */
71738625Srvb    int error;
71838625Srvb
71939085Srvb    MARK_ENTRY(CODA_ACCESS_STATS);
72038625Srvb
72138625Srvb    /* Check for access of control object.  Only read access is
72238625Srvb       allowed on it. */
72338625Srvb    if (IS_CTL_VP(vp)) {
72438625Srvb	/* bogus hack - all will be marked as successes */
72539085Srvb	MARK_INT_SAT(CODA_ACCESS_STATS);
72638625Srvb	return(((mode & VREAD) && !(mode & (VWRITE | VEXEC)))
72738625Srvb	       ? 0 : EACCES);
72838625Srvb    }
72938625Srvb
73038625Srvb    /*
73138625Srvb     * if the file is a directory, and we are checking exec (eg lookup)
73238625Srvb     * access, and the file is in the namecache, then the user must have
73338625Srvb     * lookup access to it.
73438625Srvb     */
73539085Srvb    if (coda_access_cache) {
73638625Srvb	if ((vp->v_type == VDIR) && (mode & VEXEC)) {
73739085Srvb	    if (coda_nc_lookup(cp, ".", 1, cred)) {
73839085Srvb		MARK_INT_SAT(CODA_ACCESS_STATS);
73938625Srvb		return(0);                     /* it was in the cache */
74038625Srvb	    }
74138625Srvb	}
74238625Srvb    }
74338625Srvb
74438625Srvb    error = venus_access(vtomi(vp), &cp->c_fid, mode, cred, p);
74538625Srvb
74638625Srvb    return(error);
74738625Srvb}
74838625Srvb
74938625Srvbint
75039085Srvbcoda_readlink(v)
75138625Srvb    void *v;
75238625Srvb{
75338625Srvb/* true args */
75438625Srvb    struct vop_readlink_args *ap = v;
75538625Srvb    struct vnode *vp = ap->a_vp;
75638625Srvb    struct cnode *cp = VTOC(vp);
75738625Srvb    struct uio *uiop = ap->a_uio;
75838625Srvb    struct ucred *cred = ap->a_cred;
75938625Srvb    struct proc *p = ap->a_uio->uio_procp;
76038625Srvb/* locals */
76138625Srvb    int error;
76238625Srvb    char *str;
76338625Srvb    int len;
76438625Srvb
76539085Srvb    MARK_ENTRY(CODA_READLINK_STATS);
76638625Srvb
76738625Srvb    /* Check for readlink of control object. */
76838625Srvb    if (IS_CTL_VP(vp)) {
76939085Srvb	MARK_INT_FAIL(CODA_READLINK_STATS);
77038625Srvb	return(ENOENT);
77138625Srvb    }
77238625Srvb
77339085Srvb    if ((coda_symlink_cache) && (VALID_SYMLINK(cp))) { /* symlink was cached */
77438625Srvb	uiop->uio_rw = UIO_READ;
77538625Srvb	error = uiomove(cp->c_symlink, (int)cp->c_symlen, uiop);
77638625Srvb	if (error)
77739085Srvb	    MARK_INT_FAIL(CODA_READLINK_STATS);
77838625Srvb	else
77939085Srvb	    MARK_INT_SAT(CODA_READLINK_STATS);
78038625Srvb	return(error);
78138625Srvb    }
78238625Srvb
78338625Srvb    error = venus_readlink(vtomi(vp), &cp->c_fid, cred, p, &str, &len);
78438625Srvb
78538625Srvb    if (!error) {
78638625Srvb	uiop->uio_rw = UIO_READ;
78738625Srvb	error = uiomove(str, len, uiop);
78838625Srvb
78939085Srvb	if (coda_symlink_cache) {
79038625Srvb	    cp->c_symlink = str;
79138625Srvb	    cp->c_symlen = len;
79238625Srvb	    cp->c_flags |= C_SYMLINK;
79338625Srvb	} else
79439085Srvb	    CODA_FREE(str, len);
79538625Srvb    }
79638625Srvb
79739085Srvb    CODADEBUG(CODA_READLINK, myprintf(("in readlink result %d\n",error));)
79838625Srvb    return(error);
79938625Srvb}
80038625Srvb
80138625Srvbint
80239085Srvbcoda_fsync(v)
80338625Srvb    void *v;
80438625Srvb{
80538625Srvb/* true args */
80638625Srvb    struct vop_fsync_args *ap = v;
80738625Srvb    struct vnode *vp = ap->a_vp;
80838625Srvb    struct cnode *cp = VTOC(vp);
80938625Srvb    struct ucred *cred = ap->a_cred;
81038625Srvb    struct proc *p = ap->a_p;
81138625Srvb/* locals */
81238625Srvb    struct vnode *convp = cp->c_ovp;
81338625Srvb    int error;
81438625Srvb
81539085Srvb    MARK_ENTRY(CODA_FSYNC_STATS);
81638625Srvb
81738625Srvb    /* Check for fsync on an unmounting object */
81838625Srvb    /* The NetBSD kernel, in it's infinite wisdom, can try to fsync
81938625Srvb     * after an unmount has been initiated.  This is a Bad Thing,
82038625Srvb     * which we have to avoid.  Not a legitimate failure for stats.
82138625Srvb     */
82238625Srvb    if (IS_UNMOUNTING(cp)) {
82338625Srvb	return(ENODEV);
82438625Srvb    }
82538625Srvb
82638625Srvb    /* Check for fsync of control object. */
82738625Srvb    if (IS_CTL_VP(vp)) {
82839085Srvb	MARK_INT_SAT(CODA_FSYNC_STATS);
82938625Srvb	return(0);
83038625Srvb    }
83138625Srvb
83238625Srvb    if (convp)
83338625Srvb    	VOP_FSYNC(convp, cred, MNT_WAIT, p);
83438625Srvb
83538625Srvb    /*
83638625Srvb     * We see fsyncs with usecount == 1 then usecount == 0.
83738625Srvb     * For now we ignore them.
83838625Srvb     */
83938625Srvb    /*
84038625Srvb    if (!vp->v_usecount) {
84139085Srvb    	printf("coda_fsync on vnode %p with %d usecount.  c_flags = %x (%x)\n",
84238625Srvb		vp, vp->v_usecount, cp->c_flags, cp->c_flags&C_PURGING);
84338625Srvb    }
84438625Srvb    */
84538625Srvb
84638625Srvb    /*
84738625Srvb     * We can expect fsync on any vnode at all if venus is pruging it.
84838625Srvb     * Venus can't very well answer the fsync request, now can it?
84938625Srvb     * Hopefully, it won't have to, because hopefully, venus preserves
85038625Srvb     * the (possibly untrue) invariant that it never purges an open
85138625Srvb     * vnode.  Hopefully.
85238625Srvb     */
85338625Srvb    if (cp->c_flags & C_PURGING) {
85438625Srvb	return(0);
85538625Srvb    }
85638625Srvb
85738625Srvb    /* needs research */
85838625Srvb    return 0;
85938625Srvb    error = venus_fsync(vtomi(vp), &cp->c_fid, cred, p);
86038625Srvb
86139085Srvb    CODADEBUG(CODA_FSYNC, myprintf(("in fsync result %d\n",error)); );
86238625Srvb    return(error);
86338625Srvb}
86438625Srvb
86538625Srvbint
86639085Srvbcoda_inactive(v)
86738625Srvb    void *v;
86838625Srvb{
86938625Srvb    /* XXX - at the moment, inactive doesn't look at cred, and doesn't
87038625Srvb       have a proc pointer.  Oops. */
87138625Srvb/* true args */
87238625Srvb    struct vop_inactive_args *ap = v;
87338625Srvb    struct vnode *vp = ap->a_vp;
87438625Srvb    struct cnode *cp = VTOC(vp);
87538625Srvb    struct ucred *cred __attribute__((unused)) = NULL;
87638625Srvb    struct proc *p __attribute__((unused)) = curproc;
87738625Srvb/* upcall decl */
87838625Srvb/* locals */
87938625Srvb
88038625Srvb    /* We don't need to send inactive to venus - DCS */
88139085Srvb    MARK_ENTRY(CODA_INACTIVE_STATS);
88238625Srvb
88338625Srvb    if (IS_CTL_VP(vp)) {
88439085Srvb	MARK_INT_SAT(CODA_INACTIVE_STATS);
88538625Srvb	return 0;
88638625Srvb    }
88738625Srvb
88839085Srvb    CODADEBUG(CODA_INACTIVE, myprintf(("in inactive, %lx.%lx.%lx. vfsp %p\n",
88938625Srvb				  cp->c_fid.Volume, cp->c_fid.Vnode,
89038625Srvb				  cp->c_fid.Unique, vp->v_mount));)
89138625Srvb
89238625Srvb    /* If an array has been allocated to hold the symlink, deallocate it */
89339085Srvb    if ((coda_symlink_cache) && (VALID_SYMLINK(cp))) {
89438625Srvb	if (cp->c_symlink == NULL)
89539085Srvb	    panic("coda_inactive: null symlink pointer in cnode");
89638625Srvb
89739085Srvb	CODA_FREE(cp->c_symlink, cp->c_symlen);
89838625Srvb	cp->c_flags &= ~C_SYMLINK;
89938625Srvb	cp->c_symlen = 0;
90038625Srvb    }
90138625Srvb
90238625Srvb    /* Remove it from the table so it can't be found. */
90339085Srvb    coda_unsave(cp);
90439085Srvb    if ((struct coda_mntinfo *)(vp->v_mount->mnt_data) == NULL) {
90538625Srvb	myprintf(("Help! vfsp->vfs_data was NULL, but vnode %p wasn't dying\n", vp));
90639085Srvb	panic("badness in coda_inactive\n");
90738625Srvb    }
90838625Srvb
90938625Srvb    if (IS_UNMOUNTING(cp)) {
91038625Srvb#ifdef	DEBUG
91139085Srvb	printf("coda_inactive: IS_UNMOUNTING use %d: vp %p, cp %p\n", vp->v_usecount, vp, cp);
91238625Srvb	if (cp->c_ovp != NULL)
91339085Srvb	    printf("coda_inactive: cp->ovp != NULL use %d: vp %p, cp %p\n",
91438625Srvb	    	   vp->v_usecount, vp, cp);
91538625Srvb#endif
91638759Srvb	lockmgr(&cp->c_lock, LK_RELEASE, &vp->v_interlock, p);
91738625Srvb    } else {
91839650Srvb#ifdef OLD_DIAGNOSTIC
91938625Srvb	if (CTOV(cp)->v_usecount) {
92039085Srvb	    panic("coda_inactive: nonzero reference count");
92138625Srvb	}
92238625Srvb	if (cp->c_ovp != NULL) {
92339085Srvb	    panic("coda_inactive:  cp->ovp != NULL");
92438625Srvb	}
92538625Srvb#endif
92638759Srvb	VOP_UNLOCK(vp, 0, p);
92738625Srvb	vgone(vp);
92838625Srvb    }
92938625Srvb
93039085Srvb    MARK_INT_SAT(CODA_INACTIVE_STATS);
93138625Srvb    return(0);
93238625Srvb}
93338625Srvb
93438625Srvb/*
93538625Srvb * Remote file system operations having to do with directory manipulation.
93638625Srvb */
93738625Srvb
93838625Srvb/*
93938625Srvb * It appears that in NetBSD, lookup is supposed to return the vnode locked
94038625Srvb */
94138625Srvbint
94239085Srvbcoda_lookup(v)
94338625Srvb    void *v;
94438625Srvb{
94538625Srvb/* true args */
94638625Srvb    struct vop_lookup_args *ap = v;
94738625Srvb    struct vnode *dvp = ap->a_dvp;
94838625Srvb    struct cnode *dcp = VTOC(dvp);
94938625Srvb    struct vnode **vpp = ap->a_vpp;
95038625Srvb    /*
95138625Srvb     * It looks as though ap->a_cnp->ni_cnd->cn_nameptr holds the rest
95238625Srvb     * of the string to xlate, and that we must try to get at least
95338625Srvb     * ap->a_cnp->ni_cnd->cn_namelen of those characters to macth.  I
95438625Srvb     * could be wrong.
95538625Srvb     */
95638625Srvb    struct componentname  *cnp = ap->a_cnp;
95738625Srvb    struct ucred *cred = cnp->cn_cred;
95838625Srvb    struct proc *p = cnp->cn_proc;
95938625Srvb/* locals */
96038625Srvb    struct cnode *cp;
96138625Srvb    const char *nm = cnp->cn_nameptr;
96238625Srvb    int len = cnp->cn_namelen;
96338625Srvb    ViceFid VFid;
96438625Srvb    int	vtype;
96538625Srvb    int error = 0;
96638625Srvb
96739085Srvb    MARK_ENTRY(CODA_LOOKUP_STATS);
96838625Srvb
96939085Srvb    CODADEBUG(CODA_LOOKUP, myprintf(("lookup: %s in %lx.%lx.%lx\n",
97038625Srvb				   nm, dcp->c_fid.Volume,
97138625Srvb				   dcp->c_fid.Vnode, dcp->c_fid.Unique)););
97238625Srvb
97338625Srvb    /* Check for lookup of control object. */
97438625Srvb    if (IS_CTL_NAME(dvp, nm, len)) {
97539085Srvb	*vpp = coda_ctlvp;
97638625Srvb	vref(*vpp);
97739085Srvb	MARK_INT_SAT(CODA_LOOKUP_STATS);
97838625Srvb	goto exit;
97938625Srvb    }
98038625Srvb
98139085Srvb    if (len+1 > CODA_MAXNAMLEN) {
98239085Srvb	MARK_INT_FAIL(CODA_LOOKUP_STATS);
98339085Srvb	CODADEBUG(CODA_LOOKUP, myprintf(("name too long: lookup, %lx.%lx.%lx(%s)\n",
98438625Srvb				    dcp->c_fid.Volume, dcp->c_fid.Vnode,
98538625Srvb				    dcp->c_fid.Unique, nm)););
98638625Srvb	*vpp = (struct vnode *)0;
98738625Srvb	error = EINVAL;
98838625Srvb	goto exit;
98938625Srvb    }
99038625Srvb    /* First try to look the file up in the cfs name cache */
99138625Srvb    /* lock the parent vnode? */
99239085Srvb    cp = coda_nc_lookup(dcp, nm, len, cred);
99338625Srvb    if (cp) {
99438625Srvb	*vpp = CTOV(cp);
99538625Srvb	vref(*vpp);
99639085Srvb	CODADEBUG(CODA_LOOKUP,
99738625Srvb		 myprintf(("lookup result %d vpp %p\n",error,*vpp));)
99838625Srvb    } else {
99938625Srvb
100038625Srvb	/* The name wasn't cached, so we need to contact Venus */
100138625Srvb	error = venus_lookup(vtomi(dvp), &dcp->c_fid, nm, len, cred, p, &VFid, &vtype);
100238625Srvb
100338625Srvb	if (error) {
100439085Srvb	    MARK_INT_FAIL(CODA_LOOKUP_STATS);
100539085Srvb	    CODADEBUG(CODA_LOOKUP, myprintf(("lookup error on %lx.%lx.%lx(%s)%d\n",
100638625Srvb					dcp->c_fid.Volume, dcp->c_fid.Vnode, dcp->c_fid.Unique, nm, error));)
100738625Srvb	    *vpp = (struct vnode *)0;
100838625Srvb	} else {
100939085Srvb	    MARK_INT_SAT(CODA_LOOKUP_STATS);
101039085Srvb	    CODADEBUG(CODA_LOOKUP,
101138625Srvb		     myprintf(("lookup: vol %lx vno %lx uni %lx type %o result %d\n",
101238625Srvb			    VFid.Volume, VFid.Vnode, VFid.Unique, vtype,
101338625Srvb			    error)); )
101438625Srvb
101539085Srvb	    cp = make_coda_node(&VFid, dvp->v_mount, vtype);
101638625Srvb	    *vpp = CTOV(cp);
101738625Srvb
101838625Srvb	    /* enter the new vnode in the Name Cache only if the top bit isn't set */
101938625Srvb	    /* And don't enter a new vnode for an invalid one! */
102039085Srvb	    if (!(vtype & CODA_NOCACHE))
102139085Srvb		coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
102238625Srvb	}
102338625Srvb    }
102438625Srvb
102538625Srvb exit:
102638625Srvb    /*
102738625Srvb     * If we are creating, and this was the last name to be looked up,
102838625Srvb     * and the error was ENOENT, then there really shouldn't be an
102938625Srvb     * error and we can make the leaf NULL and return success.  Since
103038625Srvb     * this is supposed to work under Mach as well as NetBSD, we're
103138625Srvb     * leaving this fn wrapped.  We also must tell lookup/namei that
103238625Srvb     * we need to save the last component of the name.  (Create will
103338625Srvb     * have to free the name buffer later...lucky us...)
103438625Srvb     */
103538625Srvb    if (((cnp->cn_nameiop == CREATE) || (cnp->cn_nameiop == RENAME))
103638625Srvb	&& (cnp->cn_flags & ISLASTCN)
103738625Srvb	&& (error == ENOENT))
103838625Srvb    {
103938625Srvb	error = EJUSTRETURN;
104038625Srvb	cnp->cn_flags |= SAVENAME;
104138625Srvb	*ap->a_vpp = NULL;
104238625Srvb    }
104338625Srvb
104438625Srvb    /*
104538625Srvb     * If we are removing, and we are at the last element, and we
104638625Srvb     * found it, then we need to keep the name around so that the
104738625Srvb     * removal will go ahead as planned.  Unfortunately, this will
104838625Srvb     * probably also lock the to-be-removed vnode, which may or may
104938625Srvb     * not be a good idea.  I'll have to look at the bits of
105039085Srvb     * coda_remove to make sure.  We'll only save the name if we did in
105139085Srvb     * fact find the name, otherwise coda_remove won't have a chance
105238625Srvb     * to free the pathname.
105338625Srvb     */
105438625Srvb    if ((cnp->cn_nameiop == DELETE)
105538625Srvb	&& (cnp->cn_flags & ISLASTCN)
105638625Srvb	&& !error)
105738625Srvb    {
105838625Srvb	cnp->cn_flags |= SAVENAME;
105938625Srvb    }
106038625Srvb
106138625Srvb    /*
106238625Srvb     * If the lookup went well, we need to (potentially?) unlock the
106338625Srvb     * parent, and lock the child.  We are only responsible for
106438625Srvb     * checking to see if the parent is supposed to be unlocked before
106538625Srvb     * we return.  We must always lock the child (provided there is
106638625Srvb     * one, and (the parent isn't locked or it isn't the same as the
106738625Srvb     * parent.)  Simple, huh?  We can never leave the parent locked unless
106838625Srvb     * we are ISLASTCN
106938625Srvb     */
107038625Srvb    if (!error || (error == EJUSTRETURN)) {
107138625Srvb	if (!(cnp->cn_flags & LOCKPARENT) || !(cnp->cn_flags & ISLASTCN)) {
107238759Srvb	    if ((error = VOP_UNLOCK(dvp, 0, p))) {
107338625Srvb		return error;
107438625Srvb	    }
107538625Srvb	    /*
107638625Srvb	     * The parent is unlocked.  As long as there is a child,
107738625Srvb	     * lock it without bothering to check anything else.
107838625Srvb	     */
107938625Srvb	    if (*ap->a_vpp) {
108038759Srvb		if ((error = VOP_LOCK(*ap->a_vpp, LK_EXCLUSIVE, p))) {
108139085Srvb		    printf("coda_lookup: ");
108238625Srvb		    panic("unlocked parent but couldn't lock child");
108338625Srvb		}
108438625Srvb	    }
108538625Srvb	} else {
108638625Srvb	    /* The parent is locked, and may be the same as the child */
108738625Srvb	    if (*ap->a_vpp && (*ap->a_vpp != dvp)) {
108838625Srvb		/* Different, go ahead and lock it. */
108938759Srvb		if ((error = VOP_LOCK(*ap->a_vpp, LK_EXCLUSIVE, p))) {
109039085Srvb		    printf("coda_lookup: ");
109138625Srvb		    panic("unlocked parent but couldn't lock child");
109238625Srvb		}
109338625Srvb	    }
109438625Srvb	}
109538625Srvb    } else {
109638625Srvb	/* If the lookup failed, we need to ensure that the leaf is NULL */
109738625Srvb	/* Don't change any locking? */
109838625Srvb	*ap->a_vpp = NULL;
109938625Srvb    }
110038625Srvb    return(error);
110138625Srvb}
110238625Srvb
110338625Srvb/*ARGSUSED*/
110438625Srvbint
110539085Srvbcoda_create(v)
110638625Srvb    void *v;
110738625Srvb{
110838625Srvb/* true args */
110938625Srvb    struct vop_create_args *ap = v;
111038625Srvb    struct vnode *dvp = ap->a_dvp;
111138625Srvb    struct cnode *dcp = VTOC(dvp);
111238625Srvb    struct vattr *va = ap->a_vap;
111338625Srvb    int exclusive = 1;
111438625Srvb    int mode = ap->a_vap->va_mode;
111538625Srvb    struct vnode **vpp = ap->a_vpp;
111638625Srvb    struct componentname  *cnp = ap->a_cnp;
111738625Srvb    struct ucred *cred = cnp->cn_cred;
111838625Srvb    struct proc *p = cnp->cn_proc;
111938625Srvb/* locals */
112038625Srvb    int error;
112138625Srvb    struct cnode *cp;
112238625Srvb    const char *nm = cnp->cn_nameptr;
112338625Srvb    int len = cnp->cn_namelen;
112438625Srvb    ViceFid VFid;
112538625Srvb    struct vattr attr;
112638625Srvb
112739085Srvb    MARK_ENTRY(CODA_CREATE_STATS);
112838625Srvb
112938625Srvb    /* All creates are exclusive XXX */
113038625Srvb    /* I'm assuming the 'mode' argument is the file mode bits XXX */
113138625Srvb
113238625Srvb    /* Check for create of control object. */
113338625Srvb    if (IS_CTL_NAME(dvp, nm, len)) {
113438625Srvb	*vpp = (struct vnode *)0;
113539085Srvb	MARK_INT_FAIL(CODA_CREATE_STATS);
113638625Srvb	return(EACCES);
113738625Srvb    }
113838625Srvb
113938625Srvb    error = venus_create(vtomi(dvp), &dcp->c_fid, nm, len, exclusive, mode, va, cred, p, &VFid, &attr);
114038625Srvb
114138625Srvb    if (!error) {
114238625Srvb
114338625Srvb	/* If this is an exclusive create, panic if the file already exists. */
114438625Srvb	/* Venus should have detected the file and reported EEXIST. */
114538625Srvb
114638625Srvb	if ((exclusive == 1) &&
114739085Srvb	    (coda_find(&VFid) != NULL))
114838625Srvb	    panic("cnode existed for newly created file!");
114938625Srvb
115039085Srvb	cp = make_coda_node(&VFid, dvp->v_mount, attr.va_type);
115138625Srvb	*vpp = CTOV(cp);
115238625Srvb
115338625Srvb	/* Update va to reflect the new attributes. */
115438625Srvb	(*va) = attr;
115538625Srvb
115638625Srvb	/* Update the attribute cache and mark it as valid */
115739085Srvb	if (coda_attr_cache) {
115838625Srvb	    VTOC(*vpp)->c_vattr = attr;
115938625Srvb	    VTOC(*vpp)->c_flags |= C_VATTR;
116038625Srvb	}
116138625Srvb
116238625Srvb	/* Invalidate the parent's attr cache, the modification time has changed */
116338625Srvb	VTOC(dvp)->c_flags &= ~C_VATTR;
116438625Srvb
116538625Srvb	/* enter the new vnode in the Name Cache */
116639085Srvb	coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
116738625Srvb
116839085Srvb	CODADEBUG(CODA_CREATE,
116938625Srvb		 myprintf(("create: (%lx.%lx.%lx), result %d\n",
117038625Srvb			VFid.Volume, VFid.Vnode, VFid.Unique, error)); )
117138625Srvb    } else {
117238625Srvb	*vpp = (struct vnode *)0;
117339085Srvb	CODADEBUG(CODA_CREATE, myprintf(("create error %d\n", error));)
117438625Srvb    }
117538625Srvb
117638625Srvb    if (!error) {
117738625Srvb	if (cnp->cn_flags & LOCKLEAF) {
117838759Srvb	    if ((error = VOP_LOCK(*ap->a_vpp, LK_EXCLUSIVE, p))) {
117939085Srvb		printf("coda_create: ");
118038625Srvb		panic("unlocked parent but couldn't lock child");
118138625Srvb	    }
118238625Srvb	}
118339650Srvb#ifdef OLD_DIAGNOSTIC
118438625Srvb	else {
118539085Srvb	    printf("coda_create: LOCKLEAF not set!\n");
118638625Srvb	}
118739650Srvb#endif
118838625Srvb    }
118938625Srvb    return(error);
119038625Srvb}
119138625Srvb
119238625Srvbint
119339085Srvbcoda_remove(v)
119438625Srvb    void *v;
119538625Srvb{
119638625Srvb/* true args */
119738625Srvb    struct vop_remove_args *ap = v;
119838625Srvb    struct vnode *dvp = ap->a_dvp;
119938625Srvb    struct cnode *cp = VTOC(dvp);
120038625Srvb    struct componentname  *cnp = ap->a_cnp;
120138625Srvb    struct ucred *cred = cnp->cn_cred;
120238625Srvb    struct proc *p = cnp->cn_proc;
120338625Srvb/* locals */
120438625Srvb    int error;
120538625Srvb    const char *nm = cnp->cn_nameptr;
120638625Srvb    int len = cnp->cn_namelen;
120738625Srvb    struct cnode *tp;
120838625Srvb
120939085Srvb    MARK_ENTRY(CODA_REMOVE_STATS);
121038625Srvb
121139085Srvb    CODADEBUG(CODA_REMOVE, myprintf(("remove: %s in %lx.%lx.%lx\n",
121238625Srvb				   nm, cp->c_fid.Volume, cp->c_fid.Vnode,
121338625Srvb				   cp->c_fid.Unique)););
121438625Srvb
121539085Srvb    /* Remove the file's entry from the CODA Name Cache */
121638625Srvb    /* We're being conservative here, it might be that this person
121738625Srvb     * doesn't really have sufficient access to delete the file
121838625Srvb     * but we feel zapping the entry won't really hurt anyone -- dcs
121938625Srvb     */
122038625Srvb    /* I'm gonna go out on a limb here. If a file and a hardlink to it
122138625Srvb     * exist, and one is removed, the link count on the other will be
122238625Srvb     * off by 1. We could either invalidate the attrs if cached, or
122338625Srvb     * fix them. I'll try to fix them. DCS 11/8/94
122438625Srvb     */
122539085Srvb    tp = coda_nc_lookup(VTOC(dvp), nm, len, cred);
122638625Srvb    if (tp) {
122738625Srvb	if (VALID_VATTR(tp)) {	/* If attrs are cached */
122838625Srvb	    if (tp->c_vattr.va_nlink > 1) {	/* If it's a hard link */
122938625Srvb		tp->c_vattr.va_nlink--;
123038625Srvb	    }
123138625Srvb	}
123238625Srvb
123339085Srvb	coda_nc_zapfile(VTOC(dvp), nm, len);
123438625Srvb	/* No need to flush it if it doesn't exist! */
123538625Srvb    }
123638625Srvb    /* Invalidate the parent's attr cache, the modification time has changed */
123738625Srvb    VTOC(dvp)->c_flags &= ~C_VATTR;
123838625Srvb
123938625Srvb    /* Check for remove of control object. */
124038625Srvb    if (IS_CTL_NAME(dvp, nm, len)) {
124139085Srvb	MARK_INT_FAIL(CODA_REMOVE_STATS);
124238625Srvb	return(ENOENT);
124338625Srvb    }
124438625Srvb
124538625Srvb    error = venus_remove(vtomi(dvp), &cp->c_fid, nm, len, cred, p);
124638625Srvb
124739085Srvb    CODADEBUG(CODA_REMOVE, myprintf(("in remove result %d\n",error)); )
124838625Srvb
124938625Srvb    return(error);
125038625Srvb}
125138625Srvb
125238625Srvbint
125339085Srvbcoda_link(v)
125438625Srvb    void *v;
125538625Srvb{
125638625Srvb/* true args */
125738625Srvb    struct vop_link_args *ap = v;
125838625Srvb    struct vnode *vp = ap->a_vp;
125938625Srvb    struct cnode *cp = VTOC(vp);
126038625Srvb    struct vnode *tdvp = ap->a_tdvp;
126138625Srvb    struct cnode *tdcp = VTOC(tdvp);
126238625Srvb    struct componentname *cnp = ap->a_cnp;
126338625Srvb    struct ucred *cred = cnp->cn_cred;
126438625Srvb    struct proc *p = cnp->cn_proc;
126538625Srvb/* locals */
126638625Srvb    int error;
126738625Srvb    const char *nm = cnp->cn_nameptr;
126838625Srvb    int len = cnp->cn_namelen;
126938625Srvb
127039085Srvb    MARK_ENTRY(CODA_LINK_STATS);
127138625Srvb
127239085Srvb    if (codadebug & CODADBGMSK(CODA_LINK)) {
127338625Srvb
127438625Srvb	myprintf(("nb_link:   vp fid: (%lx.%lx.%lx)\n",
127538625Srvb		  cp->c_fid.Volume, cp->c_fid.Vnode, cp->c_fid.Unique));
127638625Srvb	myprintf(("nb_link: tdvp fid: (%lx.%lx.%lx)\n",
127738625Srvb		  tdcp->c_fid.Volume, tdcp->c_fid.Vnode, tdcp->c_fid.Unique));
127838625Srvb
127938625Srvb    }
128039085Srvb    if (codadebug & CODADBGMSK(CODA_LINK)) {
128138625Srvb	myprintf(("link:   vp fid: (%lx.%lx.%lx)\n",
128238625Srvb		  cp->c_fid.Volume, cp->c_fid.Vnode, cp->c_fid.Unique));
128338625Srvb	myprintf(("link: tdvp fid: (%lx.%lx.%lx)\n",
128438625Srvb		  tdcp->c_fid.Volume, tdcp->c_fid.Vnode, tdcp->c_fid.Unique));
128538625Srvb
128638625Srvb    }
128738625Srvb
128838625Srvb    /* Check for link to/from control object. */
128938625Srvb    if (IS_CTL_NAME(tdvp, nm, len) || IS_CTL_VP(vp)) {
129039085Srvb	MARK_INT_FAIL(CODA_LINK_STATS);
129138625Srvb	return(EACCES);
129238625Srvb    }
129338625Srvb
129438625Srvb    error = venus_link(vtomi(vp), &cp->c_fid, &tdcp->c_fid, nm, len, cred, p);
129538625Srvb
129638625Srvb    /* Invalidate the parent's attr cache, the modification time has changed */
129738625Srvb    VTOC(tdvp)->c_flags &= ~C_VATTR;
129838625Srvb    VTOC(vp)->c_flags &= ~C_VATTR;
129938625Srvb
130039085Srvb    CODADEBUG(CODA_LINK,	myprintf(("in link result %d\n",error)); )
130138625Srvb
130238625Srvb    return(error);
130338625Srvb}
130438625Srvb
130538625Srvbint
130639085Srvbcoda_rename(v)
130738625Srvb    void *v;
130838625Srvb{
130938625Srvb/* true args */
131038625Srvb    struct vop_rename_args *ap = v;
131138625Srvb    struct vnode *odvp = ap->a_fdvp;
131238625Srvb    struct cnode *odcp = VTOC(odvp);
131338625Srvb    struct componentname  *fcnp = ap->a_fcnp;
131438625Srvb    struct vnode *ndvp = ap->a_tdvp;
131538625Srvb    struct cnode *ndcp = VTOC(ndvp);
131638625Srvb    struct componentname  *tcnp = ap->a_tcnp;
131738625Srvb    struct ucred *cred = fcnp->cn_cred;
131838625Srvb    struct proc *p = fcnp->cn_proc;
131938625Srvb/* true args */
132038625Srvb    int error;
132138625Srvb    const char *fnm = fcnp->cn_nameptr;
132238625Srvb    int flen = fcnp->cn_namelen;
132338625Srvb    const char *tnm = tcnp->cn_nameptr;
132438625Srvb    int tlen = tcnp->cn_namelen;
132538625Srvb
132639085Srvb    MARK_ENTRY(CODA_RENAME_STATS);
132738625Srvb
132838625Srvb    /* Hmmm.  The vnodes are already looked up.  Perhaps they are locked?
132938625Srvb       This could be Bad. XXX */
133039650Srvb#ifdef OLD_DIAGNOSTIC
133138625Srvb    if ((fcnp->cn_cred != tcnp->cn_cred)
133238625Srvb	|| (fcnp->cn_proc != tcnp->cn_proc))
133338625Srvb    {
133439085Srvb	panic("coda_rename: component names don't agree");
133538625Srvb    }
133639650Srvb#endif
133738625Srvb
133838625Srvb    /* Check for rename involving control object. */
133938625Srvb    if (IS_CTL_NAME(odvp, fnm, flen) || IS_CTL_NAME(ndvp, tnm, tlen)) {
134039085Srvb	MARK_INT_FAIL(CODA_RENAME_STATS);
134138625Srvb	return(EACCES);
134238625Srvb    }
134338625Srvb
134438625Srvb    /* Problem with moving directories -- need to flush entry for .. */
134538625Srvb    if (odvp != ndvp) {
134639085Srvb	struct cnode *ovcp = coda_nc_lookup(VTOC(odvp), fnm, flen, cred);
134738625Srvb	if (ovcp) {
134838625Srvb	    struct vnode *ovp = CTOV(ovcp);
134938625Srvb	    if ((ovp) &&
135038625Srvb		(ovp->v_type == VDIR)) /* If it's a directory */
135139085Srvb		coda_nc_zapfile(VTOC(ovp),"..", 2);
135238625Srvb	}
135338625Srvb    }
135438625Srvb
135538625Srvb    /* Remove the entries for both source and target files */
135639085Srvb    coda_nc_zapfile(VTOC(odvp), fnm, flen);
135739085Srvb    coda_nc_zapfile(VTOC(ndvp), tnm, tlen);
135838625Srvb
135938625Srvb    /* Invalidate the parent's attr cache, the modification time has changed */
136038625Srvb    VTOC(odvp)->c_flags &= ~C_VATTR;
136138625Srvb    VTOC(ndvp)->c_flags &= ~C_VATTR;
136238625Srvb
136339085Srvb    if (flen+1 > CODA_MAXNAMLEN) {
136439085Srvb	MARK_INT_FAIL(CODA_RENAME_STATS);
136538625Srvb	error = EINVAL;
136638625Srvb	goto exit;
136738625Srvb    }
136838625Srvb
136939085Srvb    if (tlen+1 > CODA_MAXNAMLEN) {
137039085Srvb	MARK_INT_FAIL(CODA_RENAME_STATS);
137138625Srvb	error = EINVAL;
137238625Srvb	goto exit;
137338625Srvb    }
137438625Srvb
137538625Srvb    error = venus_rename(vtomi(odvp), &odcp->c_fid, &ndcp->c_fid, fnm, flen, tnm, tlen, cred, p);
137638625Srvb
137738625Srvb exit:
137839085Srvb    CODADEBUG(CODA_RENAME, myprintf(("in rename result %d\n",error));)
137938625Srvb    /* XXX - do we need to call cache pureg on the moved vnode? */
138038625Srvb    cache_purge(ap->a_fvp);
138138625Srvb
138238625Srvb    /* It seems to be incumbent on us to drop locks on all four vnodes */
138338625Srvb    /* From-vnodes are not locked, only ref'd.  To-vnodes are locked. */
138438625Srvb
138538625Srvb    vrele(ap->a_fvp);
138638625Srvb    vrele(odvp);
138738625Srvb
138838625Srvb    if (ap->a_tvp) {
138938625Srvb	if (ap->a_tvp == ndvp) {
139038625Srvb	    vrele(ap->a_tvp);
139138625Srvb	} else {
139238625Srvb	    vput(ap->a_tvp);
139338625Srvb	}
139438625Srvb    }
139538625Srvb
139638625Srvb    vput(ndvp);
139738625Srvb    return(error);
139838625Srvb}
139938625Srvb
140038625Srvbint
140139085Srvbcoda_mkdir(v)
140238625Srvb    void *v;
140338625Srvb{
140438625Srvb/* true args */
140538625Srvb    struct vop_mkdir_args *ap = v;
140638625Srvb    struct vnode *dvp = ap->a_dvp;
140738625Srvb    struct cnode *dcp = VTOC(dvp);
140838625Srvb    struct componentname  *cnp = ap->a_cnp;
140938625Srvb    register struct vattr *va = ap->a_vap;
141038625Srvb    struct vnode **vpp = ap->a_vpp;
141138625Srvb    struct ucred *cred = cnp->cn_cred;
141238625Srvb    struct proc *p = cnp->cn_proc;
141338625Srvb/* locals */
141438625Srvb    int error;
141538625Srvb    const char *nm = cnp->cn_nameptr;
141638625Srvb    int len = cnp->cn_namelen;
141738625Srvb    struct cnode *cp;
141838625Srvb    ViceFid VFid;
141938625Srvb    struct vattr ova;
142038625Srvb
142139085Srvb    MARK_ENTRY(CODA_MKDIR_STATS);
142238625Srvb
142338625Srvb    /* Check for mkdir of target object. */
142438625Srvb    if (IS_CTL_NAME(dvp, nm, len)) {
142538625Srvb	*vpp = (struct vnode *)0;
142639085Srvb	MARK_INT_FAIL(CODA_MKDIR_STATS);
142738625Srvb	return(EACCES);
142838625Srvb    }
142938625Srvb
143039085Srvb    if (len+1 > CODA_MAXNAMLEN) {
143138625Srvb	*vpp = (struct vnode *)0;
143239085Srvb	MARK_INT_FAIL(CODA_MKDIR_STATS);
143338625Srvb	return(EACCES);
143438625Srvb    }
143538625Srvb
143638625Srvb    error = venus_mkdir(vtomi(dvp), &dcp->c_fid, nm, len, va, cred, p, &VFid, &ova);
143738625Srvb
143838625Srvb    if (!error) {
143939085Srvb	if (coda_find(&VFid) != NULL)
144038625Srvb	    panic("cnode existed for newly created directory!");
144138625Srvb
144238625Srvb
144339085Srvb	cp =  make_coda_node(&VFid, dvp->v_mount, va->va_type);
144438625Srvb	*vpp = CTOV(cp);
144538625Srvb
144638625Srvb	/* enter the new vnode in the Name Cache */
144739085Srvb	coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
144838625Srvb
144938625Srvb	/* as a side effect, enter "." and ".." for the directory */
145039085Srvb	coda_nc_enter(VTOC(*vpp), ".", 1, cred, VTOC(*vpp));
145139085Srvb	coda_nc_enter(VTOC(*vpp), "..", 2, cred, VTOC(dvp));
145238625Srvb
145339085Srvb	if (coda_attr_cache) {
145438625Srvb	    VTOC(*vpp)->c_vattr = ova;		/* update the attr cache */
145538625Srvb	    VTOC(*vpp)->c_flags |= C_VATTR;	/* Valid attributes in cnode */
145638625Srvb	}
145738625Srvb
145838625Srvb	/* Invalidate the parent's attr cache, the modification time has changed */
145938625Srvb	VTOC(dvp)->c_flags &= ~C_VATTR;
146038625Srvb
146139085Srvb	CODADEBUG( CODA_MKDIR, myprintf(("mkdir: (%lx.%lx.%lx) result %d\n",
146238625Srvb				    VFid.Volume, VFid.Vnode, VFid.Unique, error)); )
146338625Srvb    } else {
146438625Srvb	*vpp = (struct vnode *)0;
146539085Srvb	CODADEBUG(CODA_MKDIR, myprintf(("mkdir error %d\n",error));)
146638625Srvb    }
146738625Srvb
146838625Srvb    return(error);
146938625Srvb}
147038625Srvb
147138625Srvbint
147239085Srvbcoda_rmdir(v)
147338625Srvb    void *v;
147438625Srvb{
147538625Srvb/* true args */
147638625Srvb    struct vop_rmdir_args *ap = v;
147738625Srvb    struct vnode *dvp = ap->a_dvp;
147838625Srvb    struct cnode *dcp = VTOC(dvp);
147938625Srvb    struct componentname  *cnp = ap->a_cnp;
148038625Srvb    struct ucred *cred = cnp->cn_cred;
148138625Srvb    struct proc *p = cnp->cn_proc;
148238625Srvb/* true args */
148338625Srvb    int error;
148438625Srvb    const char *nm = cnp->cn_nameptr;
148538625Srvb    int len = cnp->cn_namelen;
148638625Srvb    struct cnode *cp;
148738625Srvb
148839085Srvb    MARK_ENTRY(CODA_RMDIR_STATS);
148938625Srvb
149038625Srvb    /* Check for rmdir of control object. */
149138625Srvb    if (IS_CTL_NAME(dvp, nm, len)) {
149239085Srvb	MARK_INT_FAIL(CODA_RMDIR_STATS);
149338625Srvb	return(ENOENT);
149438625Srvb    }
149538625Srvb
149638625Srvb    /* We're being conservative here, it might be that this person
149738625Srvb     * doesn't really have sufficient access to delete the file
149838625Srvb     * but we feel zapping the entry won't really hurt anyone -- dcs
149938625Srvb     */
150038625Srvb    /*
150138625Srvb     * As a side effect of the rmdir, remove any entries for children of
150238625Srvb     * the directory, especially "." and "..".
150338625Srvb     */
150439085Srvb    cp = coda_nc_lookup(dcp, nm, len, cred);
150539085Srvb    if (cp) coda_nc_zapParentfid(&(cp->c_fid), NOT_DOWNCALL);
150638625Srvb
150739085Srvb    /* Remove the file's entry from the CODA Name Cache */
150839085Srvb    coda_nc_zapfile(dcp, nm, len);
150938625Srvb
151038625Srvb    /* Invalidate the parent's attr cache, the modification time has changed */
151138625Srvb    dcp->c_flags &= ~C_VATTR;
151238625Srvb
151338625Srvb    error = venus_rmdir(vtomi(dvp), &dcp->c_fid, nm, len, cred, p);
151438625Srvb
151539085Srvb    CODADEBUG(CODA_RMDIR, myprintf(("in rmdir result %d\n", error)); )
151638625Srvb
151738625Srvb    return(error);
151838625Srvb}
151938625Srvb
152038625Srvbint
152139085Srvbcoda_symlink(v)
152238625Srvb    void *v;
152338625Srvb{
152438625Srvb/* true args */
152538625Srvb    struct vop_symlink_args *ap = v;
152638625Srvb    struct vnode *tdvp = ap->a_dvp;
152738625Srvb    struct cnode *tdcp = VTOC(tdvp);
152838625Srvb    struct componentname *cnp = ap->a_cnp;
152938625Srvb    struct vattr *tva = ap->a_vap;
153038625Srvb    char *path = ap->a_target;
153138625Srvb    struct ucred *cred = cnp->cn_cred;
153238625Srvb    struct proc *p = cnp->cn_proc;
153353131Seivind    struct vnode **vpp = ap->a_vpp;
153438625Srvb/* locals */
153538625Srvb    int error;
153638625Srvb    /*
153739085Srvb     * XXX I'm assuming the following things about coda_symlink's
153838625Srvb     * arguments:
153938625Srvb     *       t(foo) is the new name/parent/etc being created.
154038625Srvb     *       lname is the contents of the new symlink.
154138625Srvb     */
154238759Srvb    char *nm = cnp->cn_nameptr;
154338625Srvb    int len = cnp->cn_namelen;
154438625Srvb    int plen = strlen(path);
154538625Srvb
154638625Srvb    /*
154738625Srvb     * Here's the strategy for the moment: perform the symlink, then
154838625Srvb     * do a lookup to grab the resulting vnode.  I know this requires
154938625Srvb     * two communications with Venus for a new sybolic link, but
155038625Srvb     * that's the way the ball bounces.  I don't yet want to change
155138625Srvb     * the way the Mach symlink works.  When Mach support is
155238625Srvb     * deprecated, we should change symlink so that the common case
155338625Srvb     * returns the resultant vnode in a vpp argument.
155438625Srvb     */
155538625Srvb
155639085Srvb    MARK_ENTRY(CODA_SYMLINK_STATS);
155738625Srvb
155838625Srvb    /* Check for symlink of control object. */
155938625Srvb    if (IS_CTL_NAME(tdvp, nm, len)) {
156039085Srvb	MARK_INT_FAIL(CODA_SYMLINK_STATS);
156138625Srvb	return(EACCES);
156238625Srvb    }
156338625Srvb
156439085Srvb    if (plen+1 > CODA_MAXPATHLEN) {
156539085Srvb	MARK_INT_FAIL(CODA_SYMLINK_STATS);
156638625Srvb	return(EINVAL);
156738625Srvb    }
156838625Srvb
156939085Srvb    if (len+1 > CODA_MAXNAMLEN) {
157039085Srvb	MARK_INT_FAIL(CODA_SYMLINK_STATS);
157138625Srvb	error = EINVAL;
157238625Srvb	goto exit;
157338625Srvb    }
157438625Srvb
157538625Srvb    error = venus_symlink(vtomi(tdvp), &tdcp->c_fid, path, plen, nm, len, tva, cred, p);
157638625Srvb
157738625Srvb    /* Invalidate the parent's attr cache, the modification time has changed */
157838625Srvb    tdcp->c_flags &= ~C_VATTR;
157938625Srvb
158053131Seivind    if (error == 0)
158153131Seivind	error = VOP_LOOKUP(tdvp, vpp, cnp);
158238625Srvb
158338625Srvb exit:
158439085Srvb    CODADEBUG(CODA_SYMLINK, myprintf(("in symlink result %d\n",error)); )
158538625Srvb    return(error);
158638625Srvb}
158738625Srvb
158838625Srvb/*
158938625Srvb * Read directory entries.
159038625Srvb */
159138625Srvbint
159239085Srvbcoda_readdir(v)
159338625Srvb    void *v;
159438625Srvb{
159538625Srvb/* true args */
159638625Srvb    struct vop_readdir_args *ap = v;
159738625Srvb    struct vnode *vp = ap->a_vp;
159838625Srvb    struct cnode *cp = VTOC(vp);
159938625Srvb    register struct uio *uiop = ap->a_uio;
160038625Srvb    struct ucred *cred = ap->a_cred;
160138625Srvb    int *eofflag = ap->a_eofflag;
160238625Srvb    u_long **cookies = ap->a_cookies;
160338625Srvb    int *ncookies = ap->a_ncookies;
160438625Srvb    struct proc *p = ap->a_uio->uio_procp;
160538625Srvb/* upcall decl */
160638625Srvb/* locals */
160738625Srvb    int error = 0;
160838625Srvb
160939085Srvb    MARK_ENTRY(CODA_READDIR_STATS);
161038625Srvb
161149524Sbde    CODADEBUG(CODA_READDIR, myprintf(("coda_readdir(%p, %d, %lld, %d)\n",
161249524Sbde				      (void *)uiop->uio_iov->iov_base,
161349524Sbde				      uiop->uio_resid,
161449524Sbde				      (long long)uiop->uio_offset,
161549524Sbde				      uiop->uio_segflg)); )
161638625Srvb
161738625Srvb    /* Check for readdir of control object. */
161838625Srvb    if (IS_CTL_VP(vp)) {
161939085Srvb	MARK_INT_FAIL(CODA_READDIR_STATS);
162038625Srvb	return(ENOENT);
162138625Srvb    }
162238625Srvb
162338759Srvb    {
162438625Srvb	/* If directory is not already open do an "internal open" on it. */
162538625Srvb	int opened_internally = 0;
162638625Srvb	if (cp->c_ovp == NULL) {
162738625Srvb	    opened_internally = 1;
162839085Srvb	    MARK_INT_GEN(CODA_OPEN_STATS);
162938625Srvb	    error = VOP_OPEN(vp, FREAD, cred, p);
163039085Srvbprintf("coda_readdir: Internally Opening %p\n", vp);
163138625Srvb	    if (error) {
163239085Srvb		printf("coda_readdir: VOP_OPEN on container failed %d\n", error);
163338625Srvb		return (error);
163438625Srvb	    }
163538625Srvb	    if (vp->v_type == VREG) {
163642315Seivind		error = vfs_object_create(vp, p, cred);
163738625Srvb		if (error != 0) {
163839085Srvb		    printf("coda_readdir: vfs_object_create() returns %d\n", error);
163938625Srvb		    vput(vp);
164038625Srvb		}
164138625Srvb	    }
164238625Srvb	    if (error) return(error);
164338625Srvb	}
164438625Srvb
164538625Srvb	/* Have UFS handle the call. */
164639085Srvb	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)); )
164738625Srvb	error = VOP_READDIR(cp->c_ovp, uiop, cred, eofflag, ncookies,
164838625Srvb			       cookies);
164938625Srvb
165038625Srvb	if (error)
165139085Srvb	    MARK_INT_FAIL(CODA_READDIR_STATS);
165238625Srvb	else
165339085Srvb	    MARK_INT_SAT(CODA_READDIR_STATS);
165438625Srvb
165538625Srvb	/* Do an "internal close" if necessary. */
165638625Srvb	if (opened_internally) {
165739085Srvb	    MARK_INT_GEN(CODA_CLOSE_STATS);
165838625Srvb	    (void)VOP_CLOSE(vp, FREAD, cred, p);
165938625Srvb	}
166038625Srvb    }
166138625Srvb
166238625Srvb    return(error);
166338625Srvb}
166438625Srvb
166538625Srvb/*
166638625Srvb * Convert from file system blocks to device blocks
166738625Srvb */
166838625Srvbint
166939085Srvbcoda_bmap(v)
167038625Srvb    void *v;
167138625Srvb{
167238625Srvb    /* XXX on the global proc */
167338625Srvb/* true args */
167438625Srvb    struct vop_bmap_args *ap = v;
167538625Srvb    struct vnode *vp __attribute__((unused)) = ap->a_vp;	/* file's vnode */
167638625Srvb    daddr_t bn __attribute__((unused)) = ap->a_bn;	/* fs block number */
167738625Srvb    struct vnode **vpp = ap->a_vpp;			/* RETURN vp of device */
167838625Srvb    daddr_t *bnp __attribute__((unused)) = ap->a_bnp;	/* RETURN device block number */
167938625Srvb    struct proc *p __attribute__((unused)) = curproc;
168038625Srvb/* upcall decl */
168138625Srvb/* locals */
168238625Srvb
168338625Srvb	int ret = 0;
168438625Srvb	struct cnode *cp;
168538625Srvb
168638625Srvb	cp = VTOC(vp);
168738625Srvb	if (cp->c_ovp) {
168839728Srvb		return EINVAL;
168938625Srvb		ret =  VOP_BMAP(cp->c_ovp, bn, vpp, bnp, ap->a_runp, ap->a_runb);
169039650Srvb#if	0
169138625Srvb		printf("VOP_BMAP(cp->c_ovp %p, bn %p, vpp %p, bnp %p, ap->a_runp %p, ap->a_runb %p) = %d\n",
169238625Srvb			cp->c_ovp, bn, vpp, bnp, ap->a_runp, ap->a_runb, ret);
169339650Srvb#endif
169438625Srvb		return ret;
169538625Srvb	} else {
169641504Srvb#if	0
169739085Srvb		printf("coda_bmap: no container\n");
169841504Srvb#endif
169938625Srvb		return(EOPNOTSUPP);
170038625Srvb	}
170138625Srvb}
170238625Srvb
170338625Srvb/*
170438625Srvb * I don't think the following two things are used anywhere, so I've
170538625Srvb * commented them out
170638625Srvb *
170738625Srvb * struct buf *async_bufhead;
170838625Srvb * int async_daemon_count;
170938625Srvb */
171038625Srvbint
171139085Srvbcoda_strategy(v)
171238625Srvb    void *v;
171338625Srvb{
171438625Srvb/* true args */
171538625Srvb    struct vop_strategy_args *ap = v;
171638625Srvb    register struct buf *bp __attribute__((unused)) = ap->a_bp;
171738625Srvb    struct proc *p __attribute__((unused)) = curproc;
171838625Srvb/* upcall decl */
171938625Srvb/* locals */
172038625Srvb
172139085Srvb	printf("coda_strategy: called ???\n");
172238625Srvb	return(EOPNOTSUPP);
172338625Srvb}
172438625Srvb
172538625Srvbint
172639085Srvbcoda_reclaim(v)
172738625Srvb    void *v;
172838625Srvb{
172938625Srvb/* true args */
173038625Srvb    struct vop_reclaim_args *ap = v;
173138625Srvb    struct vnode *vp = ap->a_vp;
173238625Srvb    struct cnode *cp = VTOC(vp);
173338625Srvb/* upcall decl */
173438625Srvb/* locals */
173538625Srvb
173638625Srvb/*
173738625Srvb * Forced unmount/flush will let vnodes with non zero use be destroyed!
173838625Srvb */
173938625Srvb    ENTRY;
174038625Srvb
174138625Srvb    if (IS_UNMOUNTING(cp)) {
174238625Srvb#ifdef	DEBUG
174338625Srvb	if (VTOC(vp)->c_ovp) {
174438625Srvb	    if (IS_UNMOUNTING(cp))
174539085Srvb		printf("coda_reclaim: c_ovp not void: vp %p, cp %p\n", vp, cp);
174638625Srvb	}
174738625Srvb#endif
174838625Srvb    } else {
174939650Srvb#ifdef OLD_DIAGNOSTIC
175038625Srvb	if (vp->v_usecount != 0)
175139650Srvb	    print("coda_reclaim: pushing active %p\n", vp);
175238625Srvb	if (VTOC(vp)->c_ovp) {
175339085Srvb	    panic("coda_reclaim: c_ovp not void");
175438625Srvb    }
175539650Srvb#endif
175638625Srvb    }
175738625Srvb    cache_purge(vp);
175839085Srvb    coda_free(VTOC(vp));
175938625Srvb    VTOC(vp) = NULL;
176038625Srvb    return (0);
176138625Srvb}
176238625Srvb
176338625Srvbint
176439085Srvbcoda_lock(v)
176538625Srvb    void *v;
176638625Srvb{
176738625Srvb/* true args */
176838625Srvb    struct vop_lock_args *ap = v;
176938625Srvb    struct vnode *vp = ap->a_vp;
177038625Srvb    struct cnode *cp = VTOC(vp);
177138625Srvb    struct proc  *p = ap->a_p;
177238625Srvb/* upcall decl */
177338625Srvb/* locals */
177438625Srvb
177538625Srvb    ENTRY;
177638625Srvb
177739085Srvb    if (coda_lockdebug) {
177838625Srvb	myprintf(("Attempting lock on %lx.%lx.%lx\n",
177938625Srvb		  cp->c_fid.Volume, cp->c_fid.Vnode, cp->c_fid.Unique));
178038625Srvb    }
178138625Srvb
178242900Seivind#ifndef	DEBUG_LOCKS
178338759Srvb    return (lockmgr(&cp->c_lock, ap->a_flags, &vp->v_interlock, p));
178442900Seivind#else
178542900Seivind    return (debuglockmgr(&cp->c_lock, ap->a_flags, &vp->v_interlock, p,
178642900Seivind			 "coda_lock", vp->filename, vp->line));
178742900Seivind#endif
178838625Srvb}
178938625Srvb
179038625Srvbint
179139085Srvbcoda_unlock(v)
179238625Srvb    void *v;
179338625Srvb{
179438625Srvb/* true args */
179538625Srvb    struct vop_unlock_args *ap = v;
179638625Srvb    struct vnode *vp = ap->a_vp;
179738625Srvb    struct cnode *cp = VTOC(vp);
179838625Srvb    struct proc  *p = ap->a_p;
179938625Srvb/* upcall decl */
180038625Srvb/* locals */
180138625Srvb
180238625Srvb    ENTRY;
180339085Srvb    if (coda_lockdebug) {
180438625Srvb	myprintf(("Attempting unlock on %lx.%lx.%lx\n",
180538625Srvb		  cp->c_fid.Volume, cp->c_fid.Vnode, cp->c_fid.Unique));
180638625Srvb    }
180738625Srvb
180838759Srvb    return (lockmgr(&cp->c_lock, ap->a_flags | LK_RELEASE, &vp->v_interlock, p));
180938625Srvb}
181038625Srvb
181138625Srvbint
181239085Srvbcoda_islocked(v)
181338625Srvb    void *v;
181438625Srvb{
181538625Srvb/* true args */
181638625Srvb    struct vop_islocked_args *ap = v;
181738625Srvb    struct cnode *cp = VTOC(ap->a_vp);
181838625Srvb    ENTRY;
181938625Srvb
182054444Seivind    return (lockstatus(&cp->c_lock, ap->a_p));
182138625Srvb}
182238625Srvb
182338625Srvb/* How one looks up a vnode given a device/inode pair: */
182438625Srvbint
182539085Srvbcoda_grab_vnode(dev_t dev, ino_t ino, struct vnode **vpp)
182638625Srvb{
182738625Srvb    /* This is like VFS_VGET() or igetinode()! */
182838625Srvb    int           error;
182938625Srvb    struct mount *mp;
183038625Srvb
183138625Srvb    if (!(mp = devtomp(dev))) {
183249524Sbde	myprintf(("coda_grab_vnode: devtomp(%#lx) returns NULL\n",
183349524Sbde		  (u_long)dev2udev(dev)));
183438625Srvb	return(ENXIO);
183538625Srvb    }
183638625Srvb
183738625Srvb    /* XXX - ensure that nonzero-return means failure */
183838625Srvb    error = VFS_VGET(mp,ino,vpp);
183938625Srvb    if (error) {
184049524Sbde	myprintf(("coda_grab_vnode: iget/vget(%lx, %lu) returns %p, err %d\n",
184149524Sbde		  (u_long)dev2udev(dev), (u_long)ino, (void *)*vpp, error));
184238625Srvb	return(ENOENT);
184338625Srvb    }
184438625Srvb    return(0);
184538625Srvb}
184638625Srvb
184738625Srvbvoid
184838625Srvbprint_vattr( attr )
184938625Srvb	struct vattr *attr;
185038625Srvb{
185138625Srvb    char *typestr;
185238625Srvb
185338625Srvb    switch (attr->va_type) {
185438625Srvb    case VNON:
185538625Srvb	typestr = "VNON";
185638625Srvb	break;
185738625Srvb    case VREG:
185838625Srvb	typestr = "VREG";
185938625Srvb	break;
186038625Srvb    case VDIR:
186138625Srvb	typestr = "VDIR";
186238625Srvb	break;
186338625Srvb    case VBLK:
186438625Srvb	typestr = "VBLK";
186538625Srvb	break;
186638625Srvb    case VCHR:
186738625Srvb	typestr = "VCHR";
186838625Srvb	break;
186938625Srvb    case VLNK:
187038625Srvb	typestr = "VLNK";
187138625Srvb	break;
187238625Srvb    case VSOCK:
187338625Srvb	typestr = "VSCK";
187438625Srvb	break;
187538625Srvb    case VFIFO:
187638625Srvb	typestr = "VFFO";
187738625Srvb	break;
187838625Srvb    case VBAD:
187938625Srvb	typestr = "VBAD";
188038625Srvb	break;
188138625Srvb    default:
188238625Srvb	typestr = "????";
188338625Srvb	break;
188438625Srvb    }
188538625Srvb
188638625Srvb
188738625Srvb    myprintf(("attr: type %s mode %d uid %d gid %d fsid %d rdev %d\n",
188838625Srvb	      typestr, (int)attr->va_mode, (int)attr->va_uid,
188938625Srvb	      (int)attr->va_gid, (int)attr->va_fsid, (int)attr->va_rdev));
189038625Srvb
189138625Srvb    myprintf(("      fileid %d nlink %d size %d blocksize %d bytes %d\n",
189238625Srvb	      (int)attr->va_fileid, (int)attr->va_nlink,
189338625Srvb	      (int)attr->va_size,
189438625Srvb	      (int)attr->va_blocksize,(int)attr->va_bytes));
189538625Srvb    myprintf(("      gen %ld flags %ld vaflags %d\n",
189638625Srvb	      attr->va_gen, attr->va_flags, attr->va_vaflags));
189738625Srvb    myprintf(("      atime sec %d nsec %d\n",
189838625Srvb	      (int)attr->va_atime.tv_sec, (int)attr->va_atime.tv_nsec));
189938625Srvb    myprintf(("      mtime sec %d nsec %d\n",
190038625Srvb	      (int)attr->va_mtime.tv_sec, (int)attr->va_mtime.tv_nsec));
190138625Srvb    myprintf(("      ctime sec %d nsec %d\n",
190238625Srvb	      (int)attr->va_ctime.tv_sec, (int)attr->va_ctime.tv_nsec));
190338625Srvb}
190438625Srvb
190538625Srvb/* How to print a ucred */
190638625Srvbvoid
190738625Srvbprint_cred(cred)
190838625Srvb	struct ucred *cred;
190938625Srvb{
191038625Srvb
191138625Srvb	int i;
191238625Srvb
191338625Srvb	myprintf(("ref %d\tuid %d\n",cred->cr_ref,cred->cr_uid));
191438625Srvb
191538625Srvb	for (i=0; i < cred->cr_ngroups; i++)
191638625Srvb		myprintf(("\tgroup %d: (%d)\n",i,cred->cr_groups[i]));
191738625Srvb	myprintf(("\n"));
191838625Srvb
191938625Srvb}
192038625Srvb
192138625Srvb/*
192238625Srvb * Return a vnode for the given fid.
192338625Srvb * If no cnode exists for this fid create one and put it
192438625Srvb * in a table hashed by fid.Volume and fid.Vnode.  If the cnode for
192538625Srvb * this fid is already in the table return it (ref count is
192639085Srvb * incremented by coda_find.  The cnode will be flushed from the
192739085Srvb * table when coda_inactive calls coda_unsave.
192838625Srvb */
192938625Srvbstruct cnode *
193039085Srvbmake_coda_node(fid, vfsp, type)
193138625Srvb     ViceFid *fid; struct mount *vfsp; short type;
193238625Srvb{
193338625Srvb    struct cnode *cp;
193438625Srvb    int          err;
193538625Srvb
193639085Srvb    if ((cp = coda_find(fid)) == NULL) {
193738625Srvb	struct vnode *vp;
193838625Srvb
193939085Srvb	cp = coda_alloc();
194038625Srvb	lockinit(&cp->c_lock, PINOD, "cnode", 0, 0);
194138625Srvb	cp->c_fid = *fid;
194238625Srvb
194339085Srvb	err = getnewvnode(VT_CODA, vfsp, coda_vnodeop_p, &vp);
194438625Srvb	if (err) {
194539085Srvb	    panic("coda: getnewvnode returned error %d\n", err);
194638625Srvb	}
194738625Srvb	vp->v_data = cp;
194838625Srvb	vp->v_type = type;
194938625Srvb	cp->c_vnode = vp;
195039085Srvb	coda_save(cp);
195138625Srvb
195238625Srvb    } else {
195338625Srvb	vref(CTOV(cp));
195438625Srvb    }
195538625Srvb
195638625Srvb    return cp;
195738625Srvb}
1958