coda_vnops.c revision 76166
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 76166 2001-05-01 08:13:21Z markm $
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>
5076166Smarkm#include <sys/acct.h>
5176166Smarkm#include <sys/errno.h>
5276166Smarkm#include <sys/fcntl.h>
5338759Srvb#include <sys/kernel.h>
5476166Smarkm#include <sys/lock.h>
5576166Smarkm#include <sys/malloc.h>
5676166Smarkm#include <sys/file.h>		/* Must come after sys/malloc.h */
5776166Smarkm#include <sys/mount.h>
5876166Smarkm#include <sys/mutex.h>
5976166Smarkm#include <sys/namei.h>
6038759Srvb#include <sys/proc.h>
6175877Smjacob#include <sys/socket.h>
6238625Srvb#include <sys/uio.h>
6338759Srvb
6438625Srvb#include <vm/vm.h>
6538625Srvb#include <vm/vm_object.h>
6638625Srvb#include <vm/vm_extern.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 */
12439085Srvb    { &vop_fsync_desc, coda_fsync },		/* fsync */
12539085Srvb    { &vop_remove_desc, coda_remove },		/* remove */
12639085Srvb    { &vop_link_desc, coda_link },		/* link */
12739085Srvb    { &vop_rename_desc, coda_rename },		/* rename */
12839085Srvb    { &vop_mkdir_desc, coda_mkdir },		/* mkdir */
12939085Srvb    { &vop_rmdir_desc, coda_rmdir },		/* rmdir */
13039650Srvb    { &vop_symlink_desc, coda_symlink },	/* symlink */
13139650Srvb    { &vop_readdir_desc, coda_readdir },	/* readdir */
13239085Srvb    { &vop_readlink_desc, coda_readlink },	/* readlink */
13339085Srvb    { &vop_inactive_desc, coda_inactive },	/* inactive */
13439650Srvb    { &vop_reclaim_desc, coda_reclaim },	/* reclaim */
13539085Srvb    { &vop_lock_desc, coda_lock },		/* lock */
13639085Srvb    { &vop_unlock_desc, coda_unlock },		/* unlock */
13739085Srvb    { &vop_bmap_desc, coda_bmap },		/* bmap */
13839085Srvb    { &vop_strategy_desc, coda_strategy },	/* strategy */
13939650Srvb    { &vop_print_desc, coda_vop_error },	/* print */
14039085Srvb    { &vop_islocked_desc, coda_islocked },	/* islocked */
14139650Srvb    { &vop_pathconf_desc, coda_vop_error },	/* pathconf */
14239650Srvb    { &vop_advlock_desc, coda_vop_nop },	/* advlock */
14339650Srvb    { &vop_lease_desc, coda_vop_nop },		/* lease */
14438759Srvb    { &vop_poll_desc, (vop_t *) vop_stdpoll },
14539085Srvb    { &vop_getpages_desc, coda_fbsd_getpages },	/* pager intf.*/
14639085Srvb    { &vop_putpages_desc, coda_fbsd_putpages },	/* pager intf.*/
14738625Srvb
14838759Srvb#if	0
14938625Srvb
15038759Srvb    we need to define these someday
15138625Srvb#define UFS_BLKATOFF(aa, bb, cc, dd) VFSTOUFS((aa)->v_mount)->um_blkatoff(aa, bb, cc, dd)
15238625Srvb#define UFS_VALLOC(aa, bb, cc, dd) VFSTOUFS((aa)->v_mount)->um_valloc(aa, bb, cc, dd)
15338625Srvb#define UFS_VFREE(aa, bb, cc) VFSTOUFS((aa)->v_mount)->um_vfree(aa, bb, cc)
15438625Srvb#define UFS_TRUNCATE(aa, bb, cc, dd, ee) VFSTOUFS((aa)->v_mount)->um_truncate(aa, bb, cc, dd, ee)
15542374Sbde#define UFS_UPDATE(aa, bb) VFSTOUFS((aa)->v_mount)->um_update(aa, bb)
15638625Srvb
15738625Srvb    missing
15838625Srvb    { &vop_reallocblks_desc,	(vop_t *) ufs_missingop },
15938625Srvb    { &vop_cachedlookup_desc,	(vop_t *) ufs_lookup },
16038625Srvb    { &vop_whiteout_desc,	(vop_t *) ufs_whiteout },
16138625Srvb#endif
16238625Srvb    { (struct vnodeop_desc*)NULL, (int(*)(void *))NULL }
16338625Srvb};
16438625Srvb
16539085Srvbstatic struct vnodeopv_desc coda_vnodeop_opv_desc =
16639085Srvb		{ &coda_vnodeop_p, coda_vnodeop_entries };
16738625Srvb
16839085SrvbVNODEOP_SET(coda_vnodeop_opv_desc);
16938625Srvb
17038625Srvb/* A generic panic: we were called with something we didn't define yet */
17138625Srvbint
17239085Srvbcoda_vop_error(void *anon) {
17338625Srvb    struct vnodeop_desc **desc = (struct vnodeop_desc **)anon;
17438625Srvb
17541202Srvb    myprintf(("coda_vop_error: Vnode operation %s called, but not defined.\n",
17638625Srvb	      (*desc)->vdesc_name));
17741202Srvb    /*
17839085Srvb    panic("coda_vop_error");
17941202Srvb    */
18041202Srvb    return EIO;
18138625Srvb}
18238625Srvb
18338625Srvb/* A generic do-nothing.  For lease_check, advlock */
18438625Srvbint
18539085Srvbcoda_vop_nop(void *anon) {
18638625Srvb    struct vnodeop_desc **desc = (struct vnodeop_desc **)anon;
18738625Srvb
18839085Srvb    if (codadebug) {
18938625Srvb	myprintf(("Vnode operation %s called, but unsupported\n",
19038625Srvb		  (*desc)->vdesc_name));
19138625Srvb    }
19238625Srvb   return (0);
19338625Srvb}
19438625Srvb
19538625Srvbint
19639085Srvbcoda_vnodeopstats_init(void)
19738625Srvb{
19838625Srvb	register int i;
19938625Srvb
20039085Srvb	for(i=0;i<CODA_VNODEOPS_SIZE;i++) {
20139085Srvb		coda_vnodeopstats[i].opcode = i;
20239085Srvb		coda_vnodeopstats[i].entries = 0;
20339085Srvb		coda_vnodeopstats[i].sat_intrn = 0;
20439085Srvb		coda_vnodeopstats[i].unsat_intrn = 0;
20539085Srvb		coda_vnodeopstats[i].gen_intrn = 0;
20638625Srvb	}
20738625Srvb	return 0;
20838625Srvb}
20938625Srvb
21038625Srvb/*
21139085Srvb * coda_open calls Venus to return the device, inode pair of the cache
21239085Srvb * file holding the data. Using iget, coda_open finds the vnode of the
21338625Srvb * cache file, and then opens it.
21438625Srvb */
21538625Srvbint
21639085Srvbcoda_open(v)
21738625Srvb    void *v;
21838625Srvb{
21938625Srvb    /*
22038625Srvb     * NetBSD can pass the O_EXCL flag in mode, even though the check
22138625Srvb     * has already happened.  Venus defensively assumes that if open
22238625Srvb     * is passed the EXCL, it must be a bug.  We strip the flag here.
22338625Srvb     */
22438625Srvb/* true args */
22538625Srvb    struct vop_open_args *ap = v;
22638625Srvb    register struct vnode **vpp = &(ap->a_vp);
22738625Srvb    struct cnode *cp = VTOC(*vpp);
22838625Srvb    int flag = ap->a_mode & (~O_EXCL);
22938625Srvb    struct ucred *cred = ap->a_cred;
23038625Srvb    struct proc *p = ap->a_p;
23138625Srvb/* locals */
23238625Srvb    int error;
23338625Srvb    struct vnode *vp;
23438625Srvb    dev_t dev;
23538625Srvb    ino_t inode;
23638625Srvb
23739085Srvb    MARK_ENTRY(CODA_OPEN_STATS);
23838625Srvb
23938625Srvb    /* Check for open of control file. */
24038625Srvb    if (IS_CTL_VP(*vpp)) {
24138625Srvb	/* XXX */
24238625Srvb	/* if (WRITEABLE(flag)) */
24338625Srvb	if (flag & (FWRITE | O_TRUNC | O_CREAT | O_EXCL)) {
24439085Srvb	    MARK_INT_FAIL(CODA_OPEN_STATS);
24538625Srvb	    return(EACCES);
24638625Srvb	}
24739085Srvb	MARK_INT_SAT(CODA_OPEN_STATS);
24838625Srvb	return(0);
24938625Srvb    }
25038625Srvb
25138625Srvb    error = venus_open(vtomi((*vpp)), &cp->c_fid, flag, cred, p, &dev, &inode);
25238625Srvb    if (error)
25338625Srvb	return (error);
25438625Srvb    if (!error) {
25549524Sbde	CODADEBUG( CODA_OPEN,myprintf(("open: dev %#lx inode %lu result %d\n",
25649524Sbde				       (u_long)dev2udev(dev), (u_long)inode,
25749524Sbde				       error)); )
25838625Srvb    }
25938625Srvb
26038625Srvb    /* Translate the <device, inode> pair for the cache file into
26138625Srvb       an inode pointer. */
26239085Srvb    error = coda_grab_vnode(dev, inode, &vp);
26338625Srvb    if (error)
26438625Srvb	return (error);
26538625Srvb
26638759Srvb    /* We get the vnode back locked.  Needs unlocked */
26738759Srvb    VOP_UNLOCK(vp, 0, p);
26838625Srvb    /* Keep a reference until the close comes in. */
26938625Srvb    vref(*vpp);
27038625Srvb
27138625Srvb    /* Save the vnode pointer for the cache file. */
27238625Srvb    if (cp->c_ovp == NULL) {
27338625Srvb	cp->c_ovp = vp;
27438625Srvb    } else {
27538625Srvb	if (cp->c_ovp != vp)
27639085Srvb	    panic("coda_open:  cp->c_ovp != ITOV(ip)");
27738625Srvb    }
27838625Srvb    cp->c_ocount++;
27938625Srvb
28038625Srvb    /* Flush the attribute cached if writing the file. */
28138625Srvb    if (flag & FWRITE) {
28238625Srvb	cp->c_owrite++;
28338625Srvb	cp->c_flags &= ~C_VATTR;
28438625Srvb    }
28538625Srvb
28638625Srvb    /* Save the <device, inode> pair for the cache file to speed
28738625Srvb       up subsequent page_read's. */
28838625Srvb    cp->c_device = dev;
28938625Srvb    cp->c_inode = inode;
29038625Srvb
29138625Srvb    /* Open the cache file. */
29238625Srvb    error = VOP_OPEN(vp, flag, cred, p);
29338625Srvb    if (error) {
29439085Srvb    	printf("coda_open: VOP_OPEN on container failed %d\n", error);
29538625Srvb	return (error);
29638625Srvb    }
29739650Srvb/* grab (above) does this when it calls newvnode unless it's in the cache*/
29838625Srvb    if (vp->v_type == VREG) {
29942315Seivind    	error = vfs_object_create(vp, p, cred);
30038625Srvb	if (error != 0) {
30139085Srvb	    printf("coda_open: vfs_object_create() returns %d\n", error);
30238625Srvb	    vput(vp);
30338625Srvb	}
30438625Srvb    }
30539650Srvb
30638625Srvb    return(error);
30738625Srvb}
30838625Srvb
30938625Srvb/*
31038625Srvb * Close the cache file used for I/O and notify Venus.
31138625Srvb */
31238625Srvbint
31339085Srvbcoda_close(v)
31438625Srvb    void *v;
31538625Srvb{
31638625Srvb/* true args */
31738625Srvb    struct vop_close_args *ap = v;
31838625Srvb    struct vnode *vp = ap->a_vp;
31938625Srvb    struct cnode *cp = VTOC(vp);
32038625Srvb    int flag = ap->a_fflag;
32138625Srvb    struct ucred *cred = ap->a_cred;
32238625Srvb    struct proc *p = ap->a_p;
32338625Srvb/* locals */
32438625Srvb    int error;
32538625Srvb
32639085Srvb    MARK_ENTRY(CODA_CLOSE_STATS);
32738625Srvb
32838625Srvb    /* Check for close of control file. */
32938625Srvb    if (IS_CTL_VP(vp)) {
33039085Srvb	MARK_INT_SAT(CODA_CLOSE_STATS);
33138625Srvb	return(0);
33238625Srvb    }
33338625Srvb
33438625Srvb    if (IS_UNMOUNTING(cp)) {
33538625Srvb	if (cp->c_ovp) {
33639728Srvb#ifdef	CODA_VERBOSE
33739085Srvb	    printf("coda_close: destroying container ref %d, ufs vp %p of vp %p/cp %p\n",
33838625Srvb		    vp->v_usecount, cp->c_ovp, vp, cp);
33939650Srvb#endif
34040708Srvb#ifdef	hmm
34138625Srvb	    vgone(cp->c_ovp);
34240708Srvb#else
34340708Srvb	    VOP_CLOSE(cp->c_ovp, flag, cred, p); /* Do errors matter here? */
34440708Srvb	    vrele(cp->c_ovp);
34540708Srvb#endif
34638625Srvb	} else {
34739728Srvb#ifdef	CODA_VERBOSE
34839085Srvb	    printf("coda_close: NO container vp %p/cp %p\n", vp, cp);
34939650Srvb#endif
35038625Srvb	}
35138625Srvb	return ENODEV;
35238625Srvb    } else {
35338625Srvb	VOP_CLOSE(cp->c_ovp, flag, cred, p); /* Do errors matter here? */
35438625Srvb	vrele(cp->c_ovp);
35538625Srvb    }
35638625Srvb
35738625Srvb    if (--cp->c_ocount == 0)
35838625Srvb	cp->c_ovp = NULL;
35938625Srvb
36038625Srvb    if (flag & FWRITE)                    /* file was opened for write */
36138625Srvb	--cp->c_owrite;
36238625Srvb
36338625Srvb    error = venus_close(vtomi(vp), &cp->c_fid, flag, cred, p);
36438625Srvb    vrele(CTOV(cp));
36538625Srvb
36639085Srvb    CODADEBUG(CODA_CLOSE, myprintf(("close: result %d\n",error)); )
36738625Srvb    return(error);
36838625Srvb}
36938625Srvb
37038625Srvbint
37139085Srvbcoda_read(v)
37238625Srvb    void *v;
37338625Srvb{
37438625Srvb    struct vop_read_args *ap = v;
37538625Srvb
37638625Srvb    ENTRY;
37739085Srvb    return(coda_rdwr(ap->a_vp, ap->a_uio, UIO_READ,
37838625Srvb		    ap->a_ioflag, ap->a_cred, ap->a_uio->uio_procp));
37938625Srvb}
38038625Srvb
38138625Srvbint
38239085Srvbcoda_write(v)
38338625Srvb    void *v;
38438625Srvb{
38538625Srvb    struct vop_write_args *ap = v;
38638625Srvb
38738625Srvb    ENTRY;
38839085Srvb    return(coda_rdwr(ap->a_vp, ap->a_uio, UIO_WRITE,
38938625Srvb		    ap->a_ioflag, ap->a_cred, ap->a_uio->uio_procp));
39038625Srvb}
39138625Srvb
39238625Srvbint
39339085Srvbcoda_rdwr(vp, uiop, rw, ioflag, cred, p)
39438625Srvb    struct vnode *vp;
39538625Srvb    struct uio *uiop;
39638625Srvb    enum uio_rw rw;
39738625Srvb    int ioflag;
39838625Srvb    struct ucred *cred;
39938625Srvb    struct proc *p;
40038625Srvb{
40138625Srvb/* upcall decl */
40238625Srvb  /* NOTE: container file operation!!! */
40338625Srvb/* locals */
40438625Srvb    struct cnode *cp = VTOC(vp);
40538625Srvb    struct vnode *cfvp = cp->c_ovp;
40638625Srvb    int igot_internally = 0;
40738625Srvb    int opened_internally = 0;
40838625Srvb    int error = 0;
40938625Srvb
41039085Srvb    MARK_ENTRY(CODA_RDWR_STATS);
41138625Srvb
41249524Sbde    CODADEBUG(CODA_RDWR, myprintf(("coda_rdwr(%d, %p, %d, %lld, %d)\n", rw,
41349524Sbde			      (void *)uiop->uio_iov->iov_base, uiop->uio_resid,
41449524Sbde			      (long long)uiop->uio_offset, uiop->uio_segflg)); )
41538625Srvb
41638625Srvb    /* Check for rdwr of control object. */
41738625Srvb    if (IS_CTL_VP(vp)) {
41839085Srvb	MARK_INT_FAIL(CODA_RDWR_STATS);
41938625Srvb	return(EINVAL);
42038625Srvb    }
42138625Srvb
42238625Srvb    /*
42338625Srvb     * If file is not already open this must be a page
42438625Srvb     * {read,write} request.  Iget the cache file's inode
42538625Srvb     * pointer if we still have its <device, inode> pair.
42638625Srvb     * Otherwise, we must do an internal open to derive the
42738625Srvb     * pair.
42838625Srvb     */
42938625Srvb    if (cfvp == NULL) {
43038625Srvb	/*
43138625Srvb	 * If we're dumping core, do the internal open. Otherwise
43238625Srvb	 * venus won't have the correct size of the core when
43338625Srvb	 * it's completely written.
43438625Srvb	 */
43569652Sjhb	PROC_LOCK(p);
43638625Srvb	if (cp->c_inode != 0 && !(p && (p->p_acflag & ACORE))) {
43769652Sjhb	    PROC_UNLOCK(p);
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 {
45169652Sjhb	    PROC_UNLOCK(p);
45238625Srvb	    opened_internally = 1;
45339085Srvb	    MARK_INT_GEN(CODA_OPEN_STATS);
45438625Srvb	    error = VOP_OPEN(vp, (rw == UIO_READ ? FREAD : FWRITE),
45538625Srvb			     cred, p);
45639085Srvbprintf("coda_rdwr: Internally Opening %p\n", vp);
45738625Srvb	    if (error) {
45839085Srvb		printf("coda_rdwr: VOP_OPEN on container failed %d\n", error);
45938625Srvb		return (error);
46038625Srvb	    }
46138625Srvb	    if (vp->v_type == VREG) {
46242315Seivind		error = vfs_object_create(vp, p, cred);
46338625Srvb		if (error != 0) {
46439085Srvb		    printf("coda_rdwr: vfs_object_create() returns %d\n", error);
46538625Srvb		    vput(vp);
46638625Srvb		}
46738625Srvb	    }
46838625Srvb	    if (error) {
46939085Srvb		MARK_INT_FAIL(CODA_RDWR_STATS);
47038625Srvb		return(error);
47138625Srvb	    }
47238625Srvb	    cfvp = cp->c_ovp;
47338625Srvb	}
47438625Srvb    }
47538625Srvb
47638625Srvb    /* Have UFS handle the call. */
47739085Srvb    CODADEBUG(CODA_RDWR, myprintf(("indirect rdwr: fid = (%lx.%lx.%lx), refcnt = %d\n",
47838625Srvb			      cp->c_fid.Volume, cp->c_fid.Vnode,
47938625Srvb			      cp->c_fid.Unique, CTOV(cp)->v_usecount)); )
48038625Srvb
48139650Srvb
48238625Srvb    if (rw == UIO_READ) {
48338625Srvb	error = VOP_READ(cfvp, uiop, ioflag, cred);
48438625Srvb    } else {
48538625Srvb	error = VOP_WRITE(cfvp, uiop, ioflag, cred);
48638625Srvb	/* ufs_write updates the vnode_pager_setsize for the vnode/object */
48739650Srvb
48838625Srvb	{   struct vattr attr;
48938625Srvb
49038625Srvb	    if (VOP_GETATTR(cfvp, &attr, cred, p) == 0) {
49138625Srvb		vnode_pager_setsize(vp, attr.va_size);
49238625Srvb	    }
49338625Srvb	}
49438625Srvb    }
49538625Srvb
49638625Srvb    if (error)
49739085Srvb	MARK_INT_FAIL(CODA_RDWR_STATS);
49838625Srvb    else
49939085Srvb	MARK_INT_SAT(CODA_RDWR_STATS);
50038625Srvb
50138625Srvb    /* Do an internal close if necessary. */
50238625Srvb    if (opened_internally) {
50339085Srvb	MARK_INT_GEN(CODA_CLOSE_STATS);
50438625Srvb	(void)VOP_CLOSE(vp, (rw == UIO_READ ? FREAD : FWRITE), cred, p);
50538625Srvb    }
50638625Srvb
50738625Srvb    /* Invalidate cached attributes if writing. */
50838625Srvb    if (rw == UIO_WRITE)
50938625Srvb	cp->c_flags &= ~C_VATTR;
51038625Srvb    return(error);
51138625Srvb}
51238625Srvb
51338625Srvbint
51439085Srvbcoda_ioctl(v)
51538625Srvb    void *v;
51638625Srvb{
51738625Srvb/* true args */
51838625Srvb    struct vop_ioctl_args *ap = v;
51938625Srvb    struct vnode *vp = ap->a_vp;
52038625Srvb    int com = ap->a_command;
52138625Srvb    caddr_t data = ap->a_data;
52238625Srvb    int flag = ap->a_fflag;
52338625Srvb    struct ucred *cred = ap->a_cred;
52438625Srvb    struct proc  *p = ap->a_p;
52538625Srvb/* locals */
52638625Srvb    int error;
52738625Srvb    struct vnode *tvp;
52838625Srvb    struct nameidata ndp;
52938625Srvb    struct PioctlData *iap = (struct PioctlData *)data;
53038625Srvb
53139085Srvb    MARK_ENTRY(CODA_IOCTL_STATS);
53238625Srvb
53339085Srvb    CODADEBUG(CODA_IOCTL, myprintf(("in coda_ioctl on %s\n", iap->path));)
53438625Srvb
53538625Srvb    /* Don't check for operation on a dying object, for ctlvp it
53638625Srvb       shouldn't matter */
53738625Srvb
53838625Srvb    /* Must be control object to succeed. */
53938625Srvb    if (!IS_CTL_VP(vp)) {
54039085Srvb	MARK_INT_FAIL(CODA_IOCTL_STATS);
54139085Srvb	CODADEBUG(CODA_IOCTL, myprintf(("coda_ioctl error: vp != ctlvp"));)
54238625Srvb	    return (EOPNOTSUPP);
54338625Srvb    }
54438625Srvb    /* Look up the pathname. */
54538625Srvb
54638625Srvb    /* Should we use the name cache here? It would get it from
54738625Srvb       lookupname sooner or later anyway, right? */
54838625Srvb
54943382Sbde    NDINIT(&ndp, LOOKUP, (iap->follow ? FOLLOW : NOFOLLOW), UIO_USERSPACE, iap->path, p);
55038625Srvb    error = namei(&ndp);
55138625Srvb    tvp = ndp.ni_vp;
55238625Srvb
55338625Srvb    if (error) {
55439085Srvb	MARK_INT_FAIL(CODA_IOCTL_STATS);
55539085Srvb	CODADEBUG(CODA_IOCTL, myprintf(("coda_ioctl error: lookup returns %d\n",
55638625Srvb				   error));)
55738625Srvb	return(error);
55838625Srvb    }
55938625Srvb
56038625Srvb    /*
56138625Srvb     * Make sure this is a coda style cnode, but it may be a
56238625Srvb     * different vfsp
56338625Srvb     */
56449687Sphk    if (tvp->v_op != coda_vnodeop_p) {
56538625Srvb	vrele(tvp);
56654655Seivind	NDFREE(&ndp, NDF_ONLY_PNBUF);
56739085Srvb	MARK_INT_FAIL(CODA_IOCTL_STATS);
56839085Srvb	CODADEBUG(CODA_IOCTL,
56939085Srvb		 myprintf(("coda_ioctl error: %s not a coda object\n",
57038625Srvb			iap->path));)
57138625Srvb	return(EINVAL);
57238625Srvb    }
57338625Srvb
57438625Srvb    if (iap->vi.in_size > VC_MAXDATASIZE) {
57554655Seivind	NDFREE(&ndp, 0);
57638625Srvb	return(EINVAL);
57738625Srvb    }
57838625Srvb    error = venus_ioctl(vtomi(tvp), &((VTOC(tvp))->c_fid), com, flag, data, cred, p);
57938625Srvb
58038625Srvb    if (error)
58139085Srvb	MARK_INT_FAIL(CODA_IOCTL_STATS);
58238625Srvb    else
58339085Srvb	CODADEBUG(CODA_IOCTL, myprintf(("Ioctl returns %d \n", error)); )
58438625Srvb
58538625Srvb    vrele(tvp);
58654655Seivind    NDFREE(&ndp, NDF_ONLY_PNBUF);
58738625Srvb    return(error);
58838625Srvb}
58938625Srvb
59038625Srvb/*
59138625Srvb * To reduce the cost of a user-level venus;we cache attributes in
59238625Srvb * the kernel.  Each cnode has storage allocated for an attribute. If
59338625Srvb * c_vattr is valid, return a reference to it. Otherwise, get the
59438625Srvb * attributes from venus and store them in the cnode.  There is some
59538625Srvb * question if this method is a security leak. But I think that in
59638625Srvb * order to make this call, the user must have done a lookup and
59738625Srvb * opened the file, and therefore should already have access.
59838625Srvb */
59938625Srvbint
60039085Srvbcoda_getattr(v)
60138625Srvb    void *v;
60238625Srvb{
60338625Srvb/* true args */
60438625Srvb    struct vop_getattr_args *ap = v;
60538625Srvb    struct vnode *vp = ap->a_vp;
60638625Srvb    struct cnode *cp = VTOC(vp);
60738625Srvb    struct vattr *vap = ap->a_vap;
60838625Srvb    struct ucred *cred = ap->a_cred;
60938625Srvb    struct proc *p = ap->a_p;
61038625Srvb/* locals */
61138625Srvb    int error;
61238625Srvb
61339085Srvb    MARK_ENTRY(CODA_GETATTR_STATS);
61438625Srvb
61538625Srvb    if (IS_UNMOUNTING(cp))
61638625Srvb	return ENODEV;
61738759Srvb
61838625Srvb    /* Check for getattr of control object. */
61938625Srvb    if (IS_CTL_VP(vp)) {
62039085Srvb	MARK_INT_FAIL(CODA_GETATTR_STATS);
62138625Srvb	return(ENOENT);
62238625Srvb    }
62338625Srvb
62438625Srvb    /* Check to see if the attributes have already been cached */
62538625Srvb    if (VALID_VATTR(cp)) {
62639085Srvb	CODADEBUG(CODA_GETATTR, { myprintf(("attr cache hit: (%lx.%lx.%lx)\n",
62738625Srvb				       cp->c_fid.Volume,
62838625Srvb				       cp->c_fid.Vnode,
62938625Srvb				       cp->c_fid.Unique));});
63039085Srvb	CODADEBUG(CODA_GETATTR, if (!(codadebug & ~CODA_GETATTR))
63138625Srvb		 print_vattr(&cp->c_vattr); );
63238625Srvb
63338625Srvb	*vap = cp->c_vattr;
63439085Srvb	MARK_INT_SAT(CODA_GETATTR_STATS);
63538625Srvb	return(0);
63638625Srvb    }
63738625Srvb
63838625Srvb    error = venus_getattr(vtomi(vp), &cp->c_fid, cred, p, vap);
63938625Srvb
64038625Srvb    if (!error) {
64139085Srvb	CODADEBUG(CODA_GETATTR, myprintf(("getattr miss (%lx.%lx.%lx): result %d\n",
64238625Srvb				     cp->c_fid.Volume,
64338625Srvb				     cp->c_fid.Vnode,
64438625Srvb				     cp->c_fid.Unique,
64538625Srvb				     error)); )
64638625Srvb
64739085Srvb	CODADEBUG(CODA_GETATTR, if (!(codadebug & ~CODA_GETATTR))
64838625Srvb		 print_vattr(vap);	);
64938625Srvb
65038625Srvb    {	int size = vap->va_size;
65138625Srvb    	struct vnode *convp = cp->c_ovp;
65238625Srvb	if (convp != (struct vnode *)0) {
65338625Srvb	    vnode_pager_setsize(convp, size);
65438625Srvb	}
65538625Srvb    }
65638625Srvb	/* If not open for write, store attributes in cnode */
65739085Srvb	if ((cp->c_owrite == 0) && (coda_attr_cache)) {
65838625Srvb	    cp->c_vattr = *vap;
65938625Srvb	    cp->c_flags |= C_VATTR;
66038625Srvb	}
66138625Srvb
66238625Srvb    }
66338625Srvb    return(error);
66438625Srvb}
66538625Srvb
66638625Srvbint
66739085Srvbcoda_setattr(v)
66838625Srvb    void *v;
66938625Srvb{
67038625Srvb/* true args */
67138625Srvb    struct vop_setattr_args *ap = v;
67238625Srvb    register struct vnode *vp = ap->a_vp;
67338625Srvb    struct cnode *cp = VTOC(vp);
67438625Srvb    register struct vattr *vap = ap->a_vap;
67538625Srvb    struct ucred *cred = ap->a_cred;
67638625Srvb    struct proc *p = ap->a_p;
67738625Srvb/* locals */
67838625Srvb    int error;
67938625Srvb
68039085Srvb    MARK_ENTRY(CODA_SETATTR_STATS);
68138625Srvb
68238625Srvb    /* Check for setattr of control object. */
68338625Srvb    if (IS_CTL_VP(vp)) {
68439085Srvb	MARK_INT_FAIL(CODA_SETATTR_STATS);
68538625Srvb	return(ENOENT);
68638625Srvb    }
68738625Srvb
68839085Srvb    if (codadebug & CODADBGMSK(CODA_SETATTR)) {
68938625Srvb	print_vattr(vap);
69038625Srvb    }
69138625Srvb    error = venus_setattr(vtomi(vp), &cp->c_fid, vap, cred, p);
69238625Srvb
69338625Srvb    if (!error)
69438625Srvb	cp->c_flags &= ~C_VATTR;
69538625Srvb
69638625Srvb    {	int size = vap->va_size;
69738625Srvb    	struct vnode *convp = cp->c_ovp;
69838625Srvb	if (size != VNOVAL && convp != (struct vnode *)0) {
69938625Srvb	    vnode_pager_setsize(convp, size);
70038625Srvb	}
70138625Srvb    }
70239085Srvb    CODADEBUG(CODA_SETATTR,	myprintf(("setattr %d\n", error)); )
70338625Srvb    return(error);
70438625Srvb}
70538625Srvb
70638625Srvbint
70739085Srvbcoda_access(v)
70838625Srvb    void *v;
70938625Srvb{
71038625Srvb/* true args */
71138625Srvb    struct vop_access_args *ap = v;
71238625Srvb    struct vnode *vp = ap->a_vp;
71338625Srvb    struct cnode *cp = VTOC(vp);
71438625Srvb    int mode = ap->a_mode;
71538625Srvb    struct ucred *cred = ap->a_cred;
71638625Srvb    struct proc *p = ap->a_p;
71738625Srvb/* locals */
71838625Srvb    int error;
71938625Srvb
72039085Srvb    MARK_ENTRY(CODA_ACCESS_STATS);
72138625Srvb
72238625Srvb    /* Check for access of control object.  Only read access is
72338625Srvb       allowed on it. */
72438625Srvb    if (IS_CTL_VP(vp)) {
72538625Srvb	/* bogus hack - all will be marked as successes */
72639085Srvb	MARK_INT_SAT(CODA_ACCESS_STATS);
72738625Srvb	return(((mode & VREAD) && !(mode & (VWRITE | VEXEC)))
72838625Srvb	       ? 0 : EACCES);
72938625Srvb    }
73038625Srvb
73138625Srvb    /*
73238625Srvb     * if the file is a directory, and we are checking exec (eg lookup)
73338625Srvb     * access, and the file is in the namecache, then the user must have
73438625Srvb     * lookup access to it.
73538625Srvb     */
73639085Srvb    if (coda_access_cache) {
73738625Srvb	if ((vp->v_type == VDIR) && (mode & VEXEC)) {
73839085Srvb	    if (coda_nc_lookup(cp, ".", 1, cred)) {
73939085Srvb		MARK_INT_SAT(CODA_ACCESS_STATS);
74038625Srvb		return(0);                     /* it was in the cache */
74138625Srvb	    }
74238625Srvb	}
74338625Srvb    }
74438625Srvb
74538625Srvb    error = venus_access(vtomi(vp), &cp->c_fid, mode, cred, p);
74638625Srvb
74738625Srvb    return(error);
74838625Srvb}
74938625Srvb
75038625Srvbint
75139085Srvbcoda_readlink(v)
75238625Srvb    void *v;
75338625Srvb{
75438625Srvb/* true args */
75538625Srvb    struct vop_readlink_args *ap = v;
75638625Srvb    struct vnode *vp = ap->a_vp;
75738625Srvb    struct cnode *cp = VTOC(vp);
75838625Srvb    struct uio *uiop = ap->a_uio;
75938625Srvb    struct ucred *cred = ap->a_cred;
76038625Srvb    struct proc *p = ap->a_uio->uio_procp;
76138625Srvb/* locals */
76238625Srvb    int error;
76338625Srvb    char *str;
76438625Srvb    int len;
76538625Srvb
76639085Srvb    MARK_ENTRY(CODA_READLINK_STATS);
76738625Srvb
76838625Srvb    /* Check for readlink of control object. */
76938625Srvb    if (IS_CTL_VP(vp)) {
77039085Srvb	MARK_INT_FAIL(CODA_READLINK_STATS);
77138625Srvb	return(ENOENT);
77238625Srvb    }
77338625Srvb
77439085Srvb    if ((coda_symlink_cache) && (VALID_SYMLINK(cp))) { /* symlink was cached */
77538625Srvb	uiop->uio_rw = UIO_READ;
77638625Srvb	error = uiomove(cp->c_symlink, (int)cp->c_symlen, uiop);
77738625Srvb	if (error)
77839085Srvb	    MARK_INT_FAIL(CODA_READLINK_STATS);
77938625Srvb	else
78039085Srvb	    MARK_INT_SAT(CODA_READLINK_STATS);
78138625Srvb	return(error);
78238625Srvb    }
78338625Srvb
78438625Srvb    error = venus_readlink(vtomi(vp), &cp->c_fid, cred, p, &str, &len);
78538625Srvb
78638625Srvb    if (!error) {
78738625Srvb	uiop->uio_rw = UIO_READ;
78838625Srvb	error = uiomove(str, len, uiop);
78938625Srvb
79039085Srvb	if (coda_symlink_cache) {
79138625Srvb	    cp->c_symlink = str;
79238625Srvb	    cp->c_symlen = len;
79338625Srvb	    cp->c_flags |= C_SYMLINK;
79438625Srvb	} else
79539085Srvb	    CODA_FREE(str, len);
79638625Srvb    }
79738625Srvb
79839085Srvb    CODADEBUG(CODA_READLINK, myprintf(("in readlink result %d\n",error));)
79938625Srvb    return(error);
80038625Srvb}
80138625Srvb
80238625Srvbint
80339085Srvbcoda_fsync(v)
80438625Srvb    void *v;
80538625Srvb{
80638625Srvb/* true args */
80738625Srvb    struct vop_fsync_args *ap = v;
80838625Srvb    struct vnode *vp = ap->a_vp;
80938625Srvb    struct cnode *cp = VTOC(vp);
81038625Srvb    struct ucred *cred = ap->a_cred;
81138625Srvb    struct proc *p = ap->a_p;
81238625Srvb/* locals */
81338625Srvb    struct vnode *convp = cp->c_ovp;
81438625Srvb    int error;
81538625Srvb
81639085Srvb    MARK_ENTRY(CODA_FSYNC_STATS);
81738625Srvb
81838625Srvb    /* Check for fsync on an unmounting object */
81938625Srvb    /* The NetBSD kernel, in it's infinite wisdom, can try to fsync
82038625Srvb     * after an unmount has been initiated.  This is a Bad Thing,
82138625Srvb     * which we have to avoid.  Not a legitimate failure for stats.
82238625Srvb     */
82338625Srvb    if (IS_UNMOUNTING(cp)) {
82438625Srvb	return(ENODEV);
82538625Srvb    }
82638625Srvb
82738625Srvb    /* Check for fsync of control object. */
82838625Srvb    if (IS_CTL_VP(vp)) {
82939085Srvb	MARK_INT_SAT(CODA_FSYNC_STATS);
83038625Srvb	return(0);
83138625Srvb    }
83238625Srvb
83338625Srvb    if (convp)
83438625Srvb    	VOP_FSYNC(convp, cred, MNT_WAIT, p);
83538625Srvb
83638625Srvb    /*
83738625Srvb     * We see fsyncs with usecount == 1 then usecount == 0.
83838625Srvb     * For now we ignore them.
83938625Srvb     */
84038625Srvb    /*
84138625Srvb    if (!vp->v_usecount) {
84239085Srvb    	printf("coda_fsync on vnode %p with %d usecount.  c_flags = %x (%x)\n",
84338625Srvb		vp, vp->v_usecount, cp->c_flags, cp->c_flags&C_PURGING);
84438625Srvb    }
84538625Srvb    */
84638625Srvb
84738625Srvb    /*
84838625Srvb     * We can expect fsync on any vnode at all if venus is pruging it.
84938625Srvb     * Venus can't very well answer the fsync request, now can it?
85038625Srvb     * Hopefully, it won't have to, because hopefully, venus preserves
85138625Srvb     * the (possibly untrue) invariant that it never purges an open
85238625Srvb     * vnode.  Hopefully.
85338625Srvb     */
85438625Srvb    if (cp->c_flags & C_PURGING) {
85538625Srvb	return(0);
85638625Srvb    }
85738625Srvb
85838625Srvb    /* needs research */
85938625Srvb    return 0;
86038625Srvb    error = venus_fsync(vtomi(vp), &cp->c_fid, cred, p);
86138625Srvb
86239085Srvb    CODADEBUG(CODA_FSYNC, myprintf(("in fsync result %d\n",error)); );
86338625Srvb    return(error);
86438625Srvb}
86538625Srvb
86638625Srvbint
86739085Srvbcoda_inactive(v)
86838625Srvb    void *v;
86938625Srvb{
87038625Srvb    /* XXX - at the moment, inactive doesn't look at cred, and doesn't
87138625Srvb       have a proc pointer.  Oops. */
87238625Srvb/* true args */
87338625Srvb    struct vop_inactive_args *ap = v;
87438625Srvb    struct vnode *vp = ap->a_vp;
87538625Srvb    struct cnode *cp = VTOC(vp);
87638625Srvb    struct ucred *cred __attribute__((unused)) = NULL;
87738625Srvb    struct proc *p __attribute__((unused)) = curproc;
87838625Srvb/* upcall decl */
87938625Srvb/* locals */
88038625Srvb
88138625Srvb    /* We don't need to send inactive to venus - DCS */
88239085Srvb    MARK_ENTRY(CODA_INACTIVE_STATS);
88338625Srvb
88438625Srvb    if (IS_CTL_VP(vp)) {
88539085Srvb	MARK_INT_SAT(CODA_INACTIVE_STATS);
88638625Srvb	return 0;
88738625Srvb    }
88838625Srvb
88939085Srvb    CODADEBUG(CODA_INACTIVE, myprintf(("in inactive, %lx.%lx.%lx. vfsp %p\n",
89038625Srvb				  cp->c_fid.Volume, cp->c_fid.Vnode,
89138625Srvb				  cp->c_fid.Unique, vp->v_mount));)
89238625Srvb
89338625Srvb    /* If an array has been allocated to hold the symlink, deallocate it */
89439085Srvb    if ((coda_symlink_cache) && (VALID_SYMLINK(cp))) {
89538625Srvb	if (cp->c_symlink == NULL)
89639085Srvb	    panic("coda_inactive: null symlink pointer in cnode");
89738625Srvb
89839085Srvb	CODA_FREE(cp->c_symlink, cp->c_symlen);
89938625Srvb	cp->c_flags &= ~C_SYMLINK;
90038625Srvb	cp->c_symlen = 0;
90138625Srvb    }
90238625Srvb
90338625Srvb    /* Remove it from the table so it can't be found. */
90439085Srvb    coda_unsave(cp);
90539085Srvb    if ((struct coda_mntinfo *)(vp->v_mount->mnt_data) == NULL) {
90638625Srvb	myprintf(("Help! vfsp->vfs_data was NULL, but vnode %p wasn't dying\n", vp));
90739085Srvb	panic("badness in coda_inactive\n");
90838625Srvb    }
90938625Srvb
91038625Srvb    if (IS_UNMOUNTING(cp)) {
91138625Srvb#ifdef	DEBUG
91239085Srvb	printf("coda_inactive: IS_UNMOUNTING use %d: vp %p, cp %p\n", vp->v_usecount, vp, cp);
91338625Srvb	if (cp->c_ovp != NULL)
91439085Srvb	    printf("coda_inactive: cp->ovp != NULL use %d: vp %p, cp %p\n",
91538625Srvb	    	   vp->v_usecount, vp, cp);
91638625Srvb#endif
91738759Srvb	lockmgr(&cp->c_lock, LK_RELEASE, &vp->v_interlock, p);
91838625Srvb    } else {
91939650Srvb#ifdef OLD_DIAGNOSTIC
92038625Srvb	if (CTOV(cp)->v_usecount) {
92139085Srvb	    panic("coda_inactive: nonzero reference count");
92238625Srvb	}
92338625Srvb	if (cp->c_ovp != NULL) {
92439085Srvb	    panic("coda_inactive:  cp->ovp != NULL");
92538625Srvb	}
92638625Srvb#endif
92738759Srvb	VOP_UNLOCK(vp, 0, p);
92838625Srvb	vgone(vp);
92938625Srvb    }
93038625Srvb
93139085Srvb    MARK_INT_SAT(CODA_INACTIVE_STATS);
93238625Srvb    return(0);
93338625Srvb}
93438625Srvb
93538625Srvb/*
93638625Srvb * Remote file system operations having to do with directory manipulation.
93738625Srvb */
93838625Srvb
93938625Srvb/*
94038625Srvb * It appears that in NetBSD, lookup is supposed to return the vnode locked
94138625Srvb */
94238625Srvbint
94339085Srvbcoda_lookup(v)
94438625Srvb    void *v;
94538625Srvb{
94638625Srvb/* true args */
94738625Srvb    struct vop_lookup_args *ap = v;
94838625Srvb    struct vnode *dvp = ap->a_dvp;
94938625Srvb    struct cnode *dcp = VTOC(dvp);
95038625Srvb    struct vnode **vpp = ap->a_vpp;
95138625Srvb    /*
95238625Srvb     * It looks as though ap->a_cnp->ni_cnd->cn_nameptr holds the rest
95338625Srvb     * of the string to xlate, and that we must try to get at least
95438625Srvb     * ap->a_cnp->ni_cnd->cn_namelen of those characters to macth.  I
95538625Srvb     * could be wrong.
95638625Srvb     */
95738625Srvb    struct componentname  *cnp = ap->a_cnp;
95838625Srvb    struct ucred *cred = cnp->cn_cred;
95938625Srvb    struct proc *p = cnp->cn_proc;
96038625Srvb/* locals */
96138625Srvb    struct cnode *cp;
96238625Srvb    const char *nm = cnp->cn_nameptr;
96338625Srvb    int len = cnp->cn_namelen;
96438625Srvb    ViceFid VFid;
96538625Srvb    int	vtype;
96638625Srvb    int error = 0;
96738625Srvb
96839085Srvb    MARK_ENTRY(CODA_LOOKUP_STATS);
96938625Srvb
97039085Srvb    CODADEBUG(CODA_LOOKUP, myprintf(("lookup: %s in %lx.%lx.%lx\n",
97138625Srvb				   nm, dcp->c_fid.Volume,
97238625Srvb				   dcp->c_fid.Vnode, dcp->c_fid.Unique)););
97338625Srvb
97438625Srvb    /* Check for lookup of control object. */
97538625Srvb    if (IS_CTL_NAME(dvp, nm, len)) {
97639085Srvb	*vpp = coda_ctlvp;
97738625Srvb	vref(*vpp);
97839085Srvb	MARK_INT_SAT(CODA_LOOKUP_STATS);
97938625Srvb	goto exit;
98038625Srvb    }
98138625Srvb
98239085Srvb    if (len+1 > CODA_MAXNAMLEN) {
98339085Srvb	MARK_INT_FAIL(CODA_LOOKUP_STATS);
98439085Srvb	CODADEBUG(CODA_LOOKUP, myprintf(("name too long: lookup, %lx.%lx.%lx(%s)\n",
98538625Srvb				    dcp->c_fid.Volume, dcp->c_fid.Vnode,
98638625Srvb				    dcp->c_fid.Unique, nm)););
98738625Srvb	*vpp = (struct vnode *)0;
98838625Srvb	error = EINVAL;
98938625Srvb	goto exit;
99038625Srvb    }
99138625Srvb    /* First try to look the file up in the cfs name cache */
99238625Srvb    /* lock the parent vnode? */
99339085Srvb    cp = coda_nc_lookup(dcp, nm, len, cred);
99438625Srvb    if (cp) {
99538625Srvb	*vpp = CTOV(cp);
99638625Srvb	vref(*vpp);
99739085Srvb	CODADEBUG(CODA_LOOKUP,
99838625Srvb		 myprintf(("lookup result %d vpp %p\n",error,*vpp));)
99938625Srvb    } else {
100038625Srvb
100138625Srvb	/* The name wasn't cached, so we need to contact Venus */
100238625Srvb	error = venus_lookup(vtomi(dvp), &dcp->c_fid, nm, len, cred, p, &VFid, &vtype);
100338625Srvb
100438625Srvb	if (error) {
100539085Srvb	    MARK_INT_FAIL(CODA_LOOKUP_STATS);
100639085Srvb	    CODADEBUG(CODA_LOOKUP, myprintf(("lookup error on %lx.%lx.%lx(%s)%d\n",
100738625Srvb					dcp->c_fid.Volume, dcp->c_fid.Vnode, dcp->c_fid.Unique, nm, error));)
100838625Srvb	    *vpp = (struct vnode *)0;
100938625Srvb	} else {
101039085Srvb	    MARK_INT_SAT(CODA_LOOKUP_STATS);
101139085Srvb	    CODADEBUG(CODA_LOOKUP,
101238625Srvb		     myprintf(("lookup: vol %lx vno %lx uni %lx type %o result %d\n",
101338625Srvb			    VFid.Volume, VFid.Vnode, VFid.Unique, vtype,
101438625Srvb			    error)); )
101538625Srvb
101639085Srvb	    cp = make_coda_node(&VFid, dvp->v_mount, vtype);
101738625Srvb	    *vpp = CTOV(cp);
101838625Srvb
101938625Srvb	    /* enter the new vnode in the Name Cache only if the top bit isn't set */
102038625Srvb	    /* And don't enter a new vnode for an invalid one! */
102139085Srvb	    if (!(vtype & CODA_NOCACHE))
102239085Srvb		coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
102338625Srvb	}
102438625Srvb    }
102538625Srvb
102638625Srvb exit:
102738625Srvb    /*
102838625Srvb     * If we are creating, and this was the last name to be looked up,
102938625Srvb     * and the error was ENOENT, then there really shouldn't be an
103038625Srvb     * error and we can make the leaf NULL and return success.  Since
103138625Srvb     * this is supposed to work under Mach as well as NetBSD, we're
103238625Srvb     * leaving this fn wrapped.  We also must tell lookup/namei that
103338625Srvb     * we need to save the last component of the name.  (Create will
103438625Srvb     * have to free the name buffer later...lucky us...)
103538625Srvb     */
103638625Srvb    if (((cnp->cn_nameiop == CREATE) || (cnp->cn_nameiop == RENAME))
103738625Srvb	&& (cnp->cn_flags & ISLASTCN)
103838625Srvb	&& (error == ENOENT))
103938625Srvb    {
104038625Srvb	error = EJUSTRETURN;
104138625Srvb	cnp->cn_flags |= SAVENAME;
104238625Srvb	*ap->a_vpp = NULL;
104338625Srvb    }
104438625Srvb
104538625Srvb    /*
104638625Srvb     * If we are removing, and we are at the last element, and we
104738625Srvb     * found it, then we need to keep the name around so that the
104838625Srvb     * removal will go ahead as planned.  Unfortunately, this will
104938625Srvb     * probably also lock the to-be-removed vnode, which may or may
105038625Srvb     * not be a good idea.  I'll have to look at the bits of
105139085Srvb     * coda_remove to make sure.  We'll only save the name if we did in
105239085Srvb     * fact find the name, otherwise coda_remove won't have a chance
105338625Srvb     * to free the pathname.
105438625Srvb     */
105538625Srvb    if ((cnp->cn_nameiop == DELETE)
105638625Srvb	&& (cnp->cn_flags & ISLASTCN)
105738625Srvb	&& !error)
105838625Srvb    {
105938625Srvb	cnp->cn_flags |= SAVENAME;
106038625Srvb    }
106138625Srvb
106238625Srvb    /*
106338625Srvb     * If the lookup went well, we need to (potentially?) unlock the
106438625Srvb     * parent, and lock the child.  We are only responsible for
106538625Srvb     * checking to see if the parent is supposed to be unlocked before
106638625Srvb     * we return.  We must always lock the child (provided there is
106738625Srvb     * one, and (the parent isn't locked or it isn't the same as the
106838625Srvb     * parent.)  Simple, huh?  We can never leave the parent locked unless
106938625Srvb     * we are ISLASTCN
107038625Srvb     */
107138625Srvb    if (!error || (error == EJUSTRETURN)) {
107238625Srvb	if (!(cnp->cn_flags & LOCKPARENT) || !(cnp->cn_flags & ISLASTCN)) {
107338759Srvb	    if ((error = VOP_UNLOCK(dvp, 0, p))) {
107438625Srvb		return error;
107538625Srvb	    }
107638625Srvb	    /*
107738625Srvb	     * The parent is unlocked.  As long as there is a child,
107838625Srvb	     * lock it without bothering to check anything else.
107938625Srvb	     */
108038625Srvb	    if (*ap->a_vpp) {
108138759Srvb		if ((error = VOP_LOCK(*ap->a_vpp, LK_EXCLUSIVE, p))) {
108239085Srvb		    printf("coda_lookup: ");
108338625Srvb		    panic("unlocked parent but couldn't lock child");
108438625Srvb		}
108538625Srvb	    }
108638625Srvb	} else {
108738625Srvb	    /* The parent is locked, and may be the same as the child */
108838625Srvb	    if (*ap->a_vpp && (*ap->a_vpp != dvp)) {
108938625Srvb		/* Different, go ahead and lock it. */
109038759Srvb		if ((error = VOP_LOCK(*ap->a_vpp, LK_EXCLUSIVE, p))) {
109139085Srvb		    printf("coda_lookup: ");
109238625Srvb		    panic("unlocked parent but couldn't lock child");
109338625Srvb		}
109438625Srvb	    }
109538625Srvb	}
109638625Srvb    } else {
109738625Srvb	/* If the lookup failed, we need to ensure that the leaf is NULL */
109838625Srvb	/* Don't change any locking? */
109938625Srvb	*ap->a_vpp = NULL;
110038625Srvb    }
110138625Srvb    return(error);
110238625Srvb}
110338625Srvb
110438625Srvb/*ARGSUSED*/
110538625Srvbint
110639085Srvbcoda_create(v)
110738625Srvb    void *v;
110838625Srvb{
110938625Srvb/* true args */
111038625Srvb    struct vop_create_args *ap = v;
111138625Srvb    struct vnode *dvp = ap->a_dvp;
111238625Srvb    struct cnode *dcp = VTOC(dvp);
111338625Srvb    struct vattr *va = ap->a_vap;
111438625Srvb    int exclusive = 1;
111538625Srvb    int mode = ap->a_vap->va_mode;
111638625Srvb    struct vnode **vpp = ap->a_vpp;
111738625Srvb    struct componentname  *cnp = ap->a_cnp;
111838625Srvb    struct ucred *cred = cnp->cn_cred;
111938625Srvb    struct proc *p = cnp->cn_proc;
112038625Srvb/* locals */
112138625Srvb    int error;
112238625Srvb    struct cnode *cp;
112338625Srvb    const char *nm = cnp->cn_nameptr;
112438625Srvb    int len = cnp->cn_namelen;
112538625Srvb    ViceFid VFid;
112638625Srvb    struct vattr attr;
112738625Srvb
112839085Srvb    MARK_ENTRY(CODA_CREATE_STATS);
112938625Srvb
113038625Srvb    /* All creates are exclusive XXX */
113138625Srvb    /* I'm assuming the 'mode' argument is the file mode bits XXX */
113238625Srvb
113338625Srvb    /* Check for create of control object. */
113438625Srvb    if (IS_CTL_NAME(dvp, nm, len)) {
113538625Srvb	*vpp = (struct vnode *)0;
113639085Srvb	MARK_INT_FAIL(CODA_CREATE_STATS);
113738625Srvb	return(EACCES);
113838625Srvb    }
113938625Srvb
114038625Srvb    error = venus_create(vtomi(dvp), &dcp->c_fid, nm, len, exclusive, mode, va, cred, p, &VFid, &attr);
114138625Srvb
114238625Srvb    if (!error) {
114338625Srvb
114438625Srvb	/* If this is an exclusive create, panic if the file already exists. */
114538625Srvb	/* Venus should have detected the file and reported EEXIST. */
114638625Srvb
114738625Srvb	if ((exclusive == 1) &&
114839085Srvb	    (coda_find(&VFid) != NULL))
114938625Srvb	    panic("cnode existed for newly created file!");
115038625Srvb
115139085Srvb	cp = make_coda_node(&VFid, dvp->v_mount, attr.va_type);
115238625Srvb	*vpp = CTOV(cp);
115338625Srvb
115438625Srvb	/* Update va to reflect the new attributes. */
115538625Srvb	(*va) = attr;
115638625Srvb
115738625Srvb	/* Update the attribute cache and mark it as valid */
115839085Srvb	if (coda_attr_cache) {
115938625Srvb	    VTOC(*vpp)->c_vattr = attr;
116038625Srvb	    VTOC(*vpp)->c_flags |= C_VATTR;
116138625Srvb	}
116238625Srvb
116338625Srvb	/* Invalidate the parent's attr cache, the modification time has changed */
116438625Srvb	VTOC(dvp)->c_flags &= ~C_VATTR;
116538625Srvb
116638625Srvb	/* enter the new vnode in the Name Cache */
116739085Srvb	coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
116838625Srvb
116939085Srvb	CODADEBUG(CODA_CREATE,
117038625Srvb		 myprintf(("create: (%lx.%lx.%lx), result %d\n",
117138625Srvb			VFid.Volume, VFid.Vnode, VFid.Unique, error)); )
117238625Srvb    } else {
117338625Srvb	*vpp = (struct vnode *)0;
117439085Srvb	CODADEBUG(CODA_CREATE, myprintf(("create error %d\n", error));)
117538625Srvb    }
117638625Srvb
117738625Srvb    if (!error) {
117838625Srvb	if (cnp->cn_flags & LOCKLEAF) {
117938759Srvb	    if ((error = VOP_LOCK(*ap->a_vpp, LK_EXCLUSIVE, p))) {
118039085Srvb		printf("coda_create: ");
118138625Srvb		panic("unlocked parent but couldn't lock child");
118238625Srvb	    }
118338625Srvb	}
118439650Srvb#ifdef OLD_DIAGNOSTIC
118538625Srvb	else {
118639085Srvb	    printf("coda_create: LOCKLEAF not set!\n");
118738625Srvb	}
118839650Srvb#endif
118938625Srvb    }
119038625Srvb    return(error);
119138625Srvb}
119238625Srvb
119338625Srvbint
119439085Srvbcoda_remove(v)
119538625Srvb    void *v;
119638625Srvb{
119738625Srvb/* true args */
119838625Srvb    struct vop_remove_args *ap = v;
119938625Srvb    struct vnode *dvp = ap->a_dvp;
120038625Srvb    struct cnode *cp = VTOC(dvp);
120138625Srvb    struct componentname  *cnp = ap->a_cnp;
120238625Srvb    struct ucred *cred = cnp->cn_cred;
120338625Srvb    struct proc *p = cnp->cn_proc;
120438625Srvb/* locals */
120538625Srvb    int error;
120638625Srvb    const char *nm = cnp->cn_nameptr;
120738625Srvb    int len = cnp->cn_namelen;
120838625Srvb    struct cnode *tp;
120938625Srvb
121039085Srvb    MARK_ENTRY(CODA_REMOVE_STATS);
121138625Srvb
121239085Srvb    CODADEBUG(CODA_REMOVE, myprintf(("remove: %s in %lx.%lx.%lx\n",
121338625Srvb				   nm, cp->c_fid.Volume, cp->c_fid.Vnode,
121438625Srvb				   cp->c_fid.Unique)););
121538625Srvb
121639085Srvb    /* Remove the file's entry from the CODA Name Cache */
121738625Srvb    /* We're being conservative here, it might be that this person
121838625Srvb     * doesn't really have sufficient access to delete the file
121938625Srvb     * but we feel zapping the entry won't really hurt anyone -- dcs
122038625Srvb     */
122138625Srvb    /* I'm gonna go out on a limb here. If a file and a hardlink to it
122238625Srvb     * exist, and one is removed, the link count on the other will be
122338625Srvb     * off by 1. We could either invalidate the attrs if cached, or
122438625Srvb     * fix them. I'll try to fix them. DCS 11/8/94
122538625Srvb     */
122639085Srvb    tp = coda_nc_lookup(VTOC(dvp), nm, len, cred);
122738625Srvb    if (tp) {
122838625Srvb	if (VALID_VATTR(tp)) {	/* If attrs are cached */
122938625Srvb	    if (tp->c_vattr.va_nlink > 1) {	/* If it's a hard link */
123038625Srvb		tp->c_vattr.va_nlink--;
123138625Srvb	    }
123238625Srvb	}
123338625Srvb
123439085Srvb	coda_nc_zapfile(VTOC(dvp), nm, len);
123538625Srvb	/* No need to flush it if it doesn't exist! */
123638625Srvb    }
123738625Srvb    /* Invalidate the parent's attr cache, the modification time has changed */
123838625Srvb    VTOC(dvp)->c_flags &= ~C_VATTR;
123938625Srvb
124038625Srvb    /* Check for remove of control object. */
124138625Srvb    if (IS_CTL_NAME(dvp, nm, len)) {
124239085Srvb	MARK_INT_FAIL(CODA_REMOVE_STATS);
124338625Srvb	return(ENOENT);
124438625Srvb    }
124538625Srvb
124638625Srvb    error = venus_remove(vtomi(dvp), &cp->c_fid, nm, len, cred, p);
124738625Srvb
124839085Srvb    CODADEBUG(CODA_REMOVE, myprintf(("in remove result %d\n",error)); )
124938625Srvb
125038625Srvb    return(error);
125138625Srvb}
125238625Srvb
125338625Srvbint
125439085Srvbcoda_link(v)
125538625Srvb    void *v;
125638625Srvb{
125738625Srvb/* true args */
125838625Srvb    struct vop_link_args *ap = v;
125938625Srvb    struct vnode *vp = ap->a_vp;
126038625Srvb    struct cnode *cp = VTOC(vp);
126138625Srvb    struct vnode *tdvp = ap->a_tdvp;
126238625Srvb    struct cnode *tdcp = VTOC(tdvp);
126338625Srvb    struct componentname *cnp = ap->a_cnp;
126438625Srvb    struct ucred *cred = cnp->cn_cred;
126538625Srvb    struct proc *p = cnp->cn_proc;
126638625Srvb/* locals */
126738625Srvb    int error;
126838625Srvb    const char *nm = cnp->cn_nameptr;
126938625Srvb    int len = cnp->cn_namelen;
127038625Srvb
127139085Srvb    MARK_ENTRY(CODA_LINK_STATS);
127238625Srvb
127339085Srvb    if (codadebug & CODADBGMSK(CODA_LINK)) {
127438625Srvb
127538625Srvb	myprintf(("nb_link:   vp fid: (%lx.%lx.%lx)\n",
127638625Srvb		  cp->c_fid.Volume, cp->c_fid.Vnode, cp->c_fid.Unique));
127738625Srvb	myprintf(("nb_link: tdvp fid: (%lx.%lx.%lx)\n",
127838625Srvb		  tdcp->c_fid.Volume, tdcp->c_fid.Vnode, tdcp->c_fid.Unique));
127938625Srvb
128038625Srvb    }
128139085Srvb    if (codadebug & CODADBGMSK(CODA_LINK)) {
128238625Srvb	myprintf(("link:   vp fid: (%lx.%lx.%lx)\n",
128338625Srvb		  cp->c_fid.Volume, cp->c_fid.Vnode, cp->c_fid.Unique));
128438625Srvb	myprintf(("link: tdvp fid: (%lx.%lx.%lx)\n",
128538625Srvb		  tdcp->c_fid.Volume, tdcp->c_fid.Vnode, tdcp->c_fid.Unique));
128638625Srvb
128738625Srvb    }
128838625Srvb
128938625Srvb    /* Check for link to/from control object. */
129038625Srvb    if (IS_CTL_NAME(tdvp, nm, len) || IS_CTL_VP(vp)) {
129139085Srvb	MARK_INT_FAIL(CODA_LINK_STATS);
129238625Srvb	return(EACCES);
129338625Srvb    }
129438625Srvb
129538625Srvb    error = venus_link(vtomi(vp), &cp->c_fid, &tdcp->c_fid, nm, len, cred, p);
129638625Srvb
129738625Srvb    /* Invalidate the parent's attr cache, the modification time has changed */
129838625Srvb    VTOC(tdvp)->c_flags &= ~C_VATTR;
129938625Srvb    VTOC(vp)->c_flags &= ~C_VATTR;
130038625Srvb
130139085Srvb    CODADEBUG(CODA_LINK,	myprintf(("in link result %d\n",error)); )
130238625Srvb
130338625Srvb    return(error);
130438625Srvb}
130538625Srvb
130638625Srvbint
130739085Srvbcoda_rename(v)
130838625Srvb    void *v;
130938625Srvb{
131038625Srvb/* true args */
131138625Srvb    struct vop_rename_args *ap = v;
131238625Srvb    struct vnode *odvp = ap->a_fdvp;
131338625Srvb    struct cnode *odcp = VTOC(odvp);
131438625Srvb    struct componentname  *fcnp = ap->a_fcnp;
131538625Srvb    struct vnode *ndvp = ap->a_tdvp;
131638625Srvb    struct cnode *ndcp = VTOC(ndvp);
131738625Srvb    struct componentname  *tcnp = ap->a_tcnp;
131838625Srvb    struct ucred *cred = fcnp->cn_cred;
131938625Srvb    struct proc *p = fcnp->cn_proc;
132038625Srvb/* true args */
132138625Srvb    int error;
132238625Srvb    const char *fnm = fcnp->cn_nameptr;
132338625Srvb    int flen = fcnp->cn_namelen;
132438625Srvb    const char *tnm = tcnp->cn_nameptr;
132538625Srvb    int tlen = tcnp->cn_namelen;
132638625Srvb
132739085Srvb    MARK_ENTRY(CODA_RENAME_STATS);
132838625Srvb
132938625Srvb    /* Hmmm.  The vnodes are already looked up.  Perhaps they are locked?
133038625Srvb       This could be Bad. XXX */
133139650Srvb#ifdef OLD_DIAGNOSTIC
133238625Srvb    if ((fcnp->cn_cred != tcnp->cn_cred)
133338625Srvb	|| (fcnp->cn_proc != tcnp->cn_proc))
133438625Srvb    {
133539085Srvb	panic("coda_rename: component names don't agree");
133638625Srvb    }
133739650Srvb#endif
133838625Srvb
133938625Srvb    /* Check for rename involving control object. */
134038625Srvb    if (IS_CTL_NAME(odvp, fnm, flen) || IS_CTL_NAME(ndvp, tnm, tlen)) {
134139085Srvb	MARK_INT_FAIL(CODA_RENAME_STATS);
134238625Srvb	return(EACCES);
134338625Srvb    }
134438625Srvb
134538625Srvb    /* Problem with moving directories -- need to flush entry for .. */
134638625Srvb    if (odvp != ndvp) {
134739085Srvb	struct cnode *ovcp = coda_nc_lookup(VTOC(odvp), fnm, flen, cred);
134838625Srvb	if (ovcp) {
134938625Srvb	    struct vnode *ovp = CTOV(ovcp);
135038625Srvb	    if ((ovp) &&
135138625Srvb		(ovp->v_type == VDIR)) /* If it's a directory */
135239085Srvb		coda_nc_zapfile(VTOC(ovp),"..", 2);
135338625Srvb	}
135438625Srvb    }
135538625Srvb
135638625Srvb    /* Remove the entries for both source and target files */
135739085Srvb    coda_nc_zapfile(VTOC(odvp), fnm, flen);
135839085Srvb    coda_nc_zapfile(VTOC(ndvp), tnm, tlen);
135938625Srvb
136038625Srvb    /* Invalidate the parent's attr cache, the modification time has changed */
136138625Srvb    VTOC(odvp)->c_flags &= ~C_VATTR;
136238625Srvb    VTOC(ndvp)->c_flags &= ~C_VATTR;
136338625Srvb
136439085Srvb    if (flen+1 > CODA_MAXNAMLEN) {
136539085Srvb	MARK_INT_FAIL(CODA_RENAME_STATS);
136638625Srvb	error = EINVAL;
136738625Srvb	goto exit;
136838625Srvb    }
136938625Srvb
137039085Srvb    if (tlen+1 > CODA_MAXNAMLEN) {
137139085Srvb	MARK_INT_FAIL(CODA_RENAME_STATS);
137238625Srvb	error = EINVAL;
137338625Srvb	goto exit;
137438625Srvb    }
137538625Srvb
137638625Srvb    error = venus_rename(vtomi(odvp), &odcp->c_fid, &ndcp->c_fid, fnm, flen, tnm, tlen, cred, p);
137738625Srvb
137838625Srvb exit:
137939085Srvb    CODADEBUG(CODA_RENAME, myprintf(("in rename result %d\n",error));)
138038625Srvb    /* XXX - do we need to call cache pureg on the moved vnode? */
138138625Srvb    cache_purge(ap->a_fvp);
138238625Srvb
138338625Srvb    /* It seems to be incumbent on us to drop locks on all four vnodes */
138438625Srvb    /* From-vnodes are not locked, only ref'd.  To-vnodes are locked. */
138538625Srvb
138638625Srvb    vrele(ap->a_fvp);
138738625Srvb    vrele(odvp);
138838625Srvb
138938625Srvb    if (ap->a_tvp) {
139038625Srvb	if (ap->a_tvp == ndvp) {
139138625Srvb	    vrele(ap->a_tvp);
139238625Srvb	} else {
139338625Srvb	    vput(ap->a_tvp);
139438625Srvb	}
139538625Srvb    }
139638625Srvb
139738625Srvb    vput(ndvp);
139838625Srvb    return(error);
139938625Srvb}
140038625Srvb
140138625Srvbint
140239085Srvbcoda_mkdir(v)
140338625Srvb    void *v;
140438625Srvb{
140538625Srvb/* true args */
140638625Srvb    struct vop_mkdir_args *ap = v;
140738625Srvb    struct vnode *dvp = ap->a_dvp;
140838625Srvb    struct cnode *dcp = VTOC(dvp);
140938625Srvb    struct componentname  *cnp = ap->a_cnp;
141038625Srvb    register struct vattr *va = ap->a_vap;
141138625Srvb    struct vnode **vpp = ap->a_vpp;
141238625Srvb    struct ucred *cred = cnp->cn_cred;
141338625Srvb    struct proc *p = cnp->cn_proc;
141438625Srvb/* locals */
141538625Srvb    int error;
141638625Srvb    const char *nm = cnp->cn_nameptr;
141738625Srvb    int len = cnp->cn_namelen;
141838625Srvb    struct cnode *cp;
141938625Srvb    ViceFid VFid;
142038625Srvb    struct vattr ova;
142138625Srvb
142239085Srvb    MARK_ENTRY(CODA_MKDIR_STATS);
142338625Srvb
142438625Srvb    /* Check for mkdir of target object. */
142538625Srvb    if (IS_CTL_NAME(dvp, nm, len)) {
142638625Srvb	*vpp = (struct vnode *)0;
142739085Srvb	MARK_INT_FAIL(CODA_MKDIR_STATS);
142838625Srvb	return(EACCES);
142938625Srvb    }
143038625Srvb
143139085Srvb    if (len+1 > CODA_MAXNAMLEN) {
143238625Srvb	*vpp = (struct vnode *)0;
143339085Srvb	MARK_INT_FAIL(CODA_MKDIR_STATS);
143438625Srvb	return(EACCES);
143538625Srvb    }
143638625Srvb
143738625Srvb    error = venus_mkdir(vtomi(dvp), &dcp->c_fid, nm, len, va, cred, p, &VFid, &ova);
143838625Srvb
143938625Srvb    if (!error) {
144039085Srvb	if (coda_find(&VFid) != NULL)
144138625Srvb	    panic("cnode existed for newly created directory!");
144238625Srvb
144338625Srvb
144439085Srvb	cp =  make_coda_node(&VFid, dvp->v_mount, va->va_type);
144538625Srvb	*vpp = CTOV(cp);
144638625Srvb
144738625Srvb	/* enter the new vnode in the Name Cache */
144839085Srvb	coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
144938625Srvb
145038625Srvb	/* as a side effect, enter "." and ".." for the directory */
145139085Srvb	coda_nc_enter(VTOC(*vpp), ".", 1, cred, VTOC(*vpp));
145239085Srvb	coda_nc_enter(VTOC(*vpp), "..", 2, cred, VTOC(dvp));
145338625Srvb
145439085Srvb	if (coda_attr_cache) {
145538625Srvb	    VTOC(*vpp)->c_vattr = ova;		/* update the attr cache */
145638625Srvb	    VTOC(*vpp)->c_flags |= C_VATTR;	/* Valid attributes in cnode */
145738625Srvb	}
145838625Srvb
145938625Srvb	/* Invalidate the parent's attr cache, the modification time has changed */
146038625Srvb	VTOC(dvp)->c_flags &= ~C_VATTR;
146138625Srvb
146239085Srvb	CODADEBUG( CODA_MKDIR, myprintf(("mkdir: (%lx.%lx.%lx) result %d\n",
146338625Srvb				    VFid.Volume, VFid.Vnode, VFid.Unique, error)); )
146438625Srvb    } else {
146538625Srvb	*vpp = (struct vnode *)0;
146639085Srvb	CODADEBUG(CODA_MKDIR, myprintf(("mkdir error %d\n",error));)
146738625Srvb    }
146838625Srvb
146938625Srvb    return(error);
147038625Srvb}
147138625Srvb
147238625Srvbint
147339085Srvbcoda_rmdir(v)
147438625Srvb    void *v;
147538625Srvb{
147638625Srvb/* true args */
147738625Srvb    struct vop_rmdir_args *ap = v;
147838625Srvb    struct vnode *dvp = ap->a_dvp;
147938625Srvb    struct cnode *dcp = VTOC(dvp);
148038625Srvb    struct componentname  *cnp = ap->a_cnp;
148138625Srvb    struct ucred *cred = cnp->cn_cred;
148238625Srvb    struct proc *p = cnp->cn_proc;
148338625Srvb/* true args */
148438625Srvb    int error;
148538625Srvb    const char *nm = cnp->cn_nameptr;
148638625Srvb    int len = cnp->cn_namelen;
148738625Srvb    struct cnode *cp;
148838625Srvb
148939085Srvb    MARK_ENTRY(CODA_RMDIR_STATS);
149038625Srvb
149138625Srvb    /* Check for rmdir of control object. */
149238625Srvb    if (IS_CTL_NAME(dvp, nm, len)) {
149339085Srvb	MARK_INT_FAIL(CODA_RMDIR_STATS);
149438625Srvb	return(ENOENT);
149538625Srvb    }
149638625Srvb
149738625Srvb    /* We're being conservative here, it might be that this person
149838625Srvb     * doesn't really have sufficient access to delete the file
149938625Srvb     * but we feel zapping the entry won't really hurt anyone -- dcs
150038625Srvb     */
150138625Srvb    /*
150238625Srvb     * As a side effect of the rmdir, remove any entries for children of
150338625Srvb     * the directory, especially "." and "..".
150438625Srvb     */
150539085Srvb    cp = coda_nc_lookup(dcp, nm, len, cred);
150639085Srvb    if (cp) coda_nc_zapParentfid(&(cp->c_fid), NOT_DOWNCALL);
150738625Srvb
150839085Srvb    /* Remove the file's entry from the CODA Name Cache */
150939085Srvb    coda_nc_zapfile(dcp, nm, len);
151038625Srvb
151138625Srvb    /* Invalidate the parent's attr cache, the modification time has changed */
151238625Srvb    dcp->c_flags &= ~C_VATTR;
151338625Srvb
151438625Srvb    error = venus_rmdir(vtomi(dvp), &dcp->c_fid, nm, len, cred, p);
151538625Srvb
151639085Srvb    CODADEBUG(CODA_RMDIR, myprintf(("in rmdir result %d\n", error)); )
151738625Srvb
151838625Srvb    return(error);
151938625Srvb}
152038625Srvb
152138625Srvbint
152239085Srvbcoda_symlink(v)
152338625Srvb    void *v;
152438625Srvb{
152538625Srvb/* true args */
152638625Srvb    struct vop_symlink_args *ap = v;
152738625Srvb    struct vnode *tdvp = ap->a_dvp;
152838625Srvb    struct cnode *tdcp = VTOC(tdvp);
152938625Srvb    struct componentname *cnp = ap->a_cnp;
153038625Srvb    struct vattr *tva = ap->a_vap;
153138625Srvb    char *path = ap->a_target;
153238625Srvb    struct ucred *cred = cnp->cn_cred;
153338625Srvb    struct proc *p = cnp->cn_proc;
153453131Seivind    struct vnode **vpp = ap->a_vpp;
153538625Srvb/* locals */
153638625Srvb    int error;
153738625Srvb    /*
153839085Srvb     * XXX I'm assuming the following things about coda_symlink's
153938625Srvb     * arguments:
154038625Srvb     *       t(foo) is the new name/parent/etc being created.
154138625Srvb     *       lname is the contents of the new symlink.
154238625Srvb     */
154338759Srvb    char *nm = cnp->cn_nameptr;
154438625Srvb    int len = cnp->cn_namelen;
154538625Srvb    int plen = strlen(path);
154638625Srvb
154738625Srvb    /*
154838625Srvb     * Here's the strategy for the moment: perform the symlink, then
154938625Srvb     * do a lookup to grab the resulting vnode.  I know this requires
155038625Srvb     * two communications with Venus for a new sybolic link, but
155138625Srvb     * that's the way the ball bounces.  I don't yet want to change
155238625Srvb     * the way the Mach symlink works.  When Mach support is
155338625Srvb     * deprecated, we should change symlink so that the common case
155438625Srvb     * returns the resultant vnode in a vpp argument.
155538625Srvb     */
155638625Srvb
155739085Srvb    MARK_ENTRY(CODA_SYMLINK_STATS);
155838625Srvb
155938625Srvb    /* Check for symlink of control object. */
156038625Srvb    if (IS_CTL_NAME(tdvp, nm, len)) {
156139085Srvb	MARK_INT_FAIL(CODA_SYMLINK_STATS);
156238625Srvb	return(EACCES);
156338625Srvb    }
156438625Srvb
156539085Srvb    if (plen+1 > CODA_MAXPATHLEN) {
156639085Srvb	MARK_INT_FAIL(CODA_SYMLINK_STATS);
156738625Srvb	return(EINVAL);
156838625Srvb    }
156938625Srvb
157039085Srvb    if (len+1 > CODA_MAXNAMLEN) {
157139085Srvb	MARK_INT_FAIL(CODA_SYMLINK_STATS);
157238625Srvb	error = EINVAL;
157338625Srvb	goto exit;
157438625Srvb    }
157538625Srvb
157638625Srvb    error = venus_symlink(vtomi(tdvp), &tdcp->c_fid, path, plen, nm, len, tva, cred, p);
157738625Srvb
157838625Srvb    /* Invalidate the parent's attr cache, the modification time has changed */
157938625Srvb    tdcp->c_flags &= ~C_VATTR;
158038625Srvb
158153131Seivind    if (error == 0)
158253131Seivind	error = VOP_LOOKUP(tdvp, vpp, cnp);
158338625Srvb
158438625Srvb exit:
158539085Srvb    CODADEBUG(CODA_SYMLINK, myprintf(("in symlink result %d\n",error)); )
158638625Srvb    return(error);
158738625Srvb}
158838625Srvb
158938625Srvb/*
159038625Srvb * Read directory entries.
159138625Srvb */
159238625Srvbint
159339085Srvbcoda_readdir(v)
159438625Srvb    void *v;
159538625Srvb{
159638625Srvb/* true args */
159738625Srvb    struct vop_readdir_args *ap = v;
159838625Srvb    struct vnode *vp = ap->a_vp;
159938625Srvb    struct cnode *cp = VTOC(vp);
160038625Srvb    register struct uio *uiop = ap->a_uio;
160138625Srvb    struct ucred *cred = ap->a_cred;
160238625Srvb    int *eofflag = ap->a_eofflag;
160338625Srvb    u_long **cookies = ap->a_cookies;
160438625Srvb    int *ncookies = ap->a_ncookies;
160538625Srvb    struct proc *p = ap->a_uio->uio_procp;
160638625Srvb/* upcall decl */
160738625Srvb/* locals */
160838625Srvb    int error = 0;
160938625Srvb
161039085Srvb    MARK_ENTRY(CODA_READDIR_STATS);
161138625Srvb
161249524Sbde    CODADEBUG(CODA_READDIR, myprintf(("coda_readdir(%p, %d, %lld, %d)\n",
161349524Sbde				      (void *)uiop->uio_iov->iov_base,
161449524Sbde				      uiop->uio_resid,
161549524Sbde				      (long long)uiop->uio_offset,
161649524Sbde				      uiop->uio_segflg)); )
161738625Srvb
161838625Srvb    /* Check for readdir of control object. */
161938625Srvb    if (IS_CTL_VP(vp)) {
162039085Srvb	MARK_INT_FAIL(CODA_READDIR_STATS);
162138625Srvb	return(ENOENT);
162238625Srvb    }
162338625Srvb
162438759Srvb    {
162538625Srvb	/* If directory is not already open do an "internal open" on it. */
162638625Srvb	int opened_internally = 0;
162738625Srvb	if (cp->c_ovp == NULL) {
162838625Srvb	    opened_internally = 1;
162939085Srvb	    MARK_INT_GEN(CODA_OPEN_STATS);
163038625Srvb	    error = VOP_OPEN(vp, FREAD, cred, p);
163139085Srvbprintf("coda_readdir: Internally Opening %p\n", vp);
163238625Srvb	    if (error) {
163339085Srvb		printf("coda_readdir: VOP_OPEN on container failed %d\n", error);
163438625Srvb		return (error);
163538625Srvb	    }
163638625Srvb	    if (vp->v_type == VREG) {
163742315Seivind		error = vfs_object_create(vp, p, cred);
163838625Srvb		if (error != 0) {
163939085Srvb		    printf("coda_readdir: vfs_object_create() returns %d\n", error);
164038625Srvb		    vput(vp);
164138625Srvb		}
164238625Srvb	    }
164338625Srvb	    if (error) return(error);
164438625Srvb	}
164538625Srvb
164638625Srvb	/* Have UFS handle the call. */
164739085Srvb	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)); )
164838625Srvb	error = VOP_READDIR(cp->c_ovp, uiop, cred, eofflag, ncookies,
164938625Srvb			       cookies);
165038625Srvb
165138625Srvb	if (error)
165239085Srvb	    MARK_INT_FAIL(CODA_READDIR_STATS);
165338625Srvb	else
165439085Srvb	    MARK_INT_SAT(CODA_READDIR_STATS);
165538625Srvb
165638625Srvb	/* Do an "internal close" if necessary. */
165738625Srvb	if (opened_internally) {
165839085Srvb	    MARK_INT_GEN(CODA_CLOSE_STATS);
165938625Srvb	    (void)VOP_CLOSE(vp, FREAD, cred, p);
166038625Srvb	}
166138625Srvb    }
166238625Srvb
166338625Srvb    return(error);
166438625Srvb}
166538625Srvb
166638625Srvb/*
166738625Srvb * Convert from file system blocks to device blocks
166838625Srvb */
166938625Srvbint
167039085Srvbcoda_bmap(v)
167138625Srvb    void *v;
167238625Srvb{
167338625Srvb    /* XXX on the global proc */
167438625Srvb/* true args */
167538625Srvb    struct vop_bmap_args *ap = v;
167638625Srvb    struct vnode *vp __attribute__((unused)) = ap->a_vp;	/* file's vnode */
167738625Srvb    daddr_t bn __attribute__((unused)) = ap->a_bn;	/* fs block number */
167838625Srvb    struct vnode **vpp = ap->a_vpp;			/* RETURN vp of device */
167938625Srvb    daddr_t *bnp __attribute__((unused)) = ap->a_bnp;	/* RETURN device block number */
168038625Srvb    struct proc *p __attribute__((unused)) = curproc;
168138625Srvb/* upcall decl */
168238625Srvb/* locals */
168338625Srvb
168438625Srvb	int ret = 0;
168538625Srvb	struct cnode *cp;
168638625Srvb
168738625Srvb	cp = VTOC(vp);
168838625Srvb	if (cp->c_ovp) {
168939728Srvb		return EINVAL;
169038625Srvb		ret =  VOP_BMAP(cp->c_ovp, bn, vpp, bnp, ap->a_runp, ap->a_runb);
169139650Srvb#if	0
169238625Srvb		printf("VOP_BMAP(cp->c_ovp %p, bn %p, vpp %p, bnp %p, ap->a_runp %p, ap->a_runb %p) = %d\n",
169338625Srvb			cp->c_ovp, bn, vpp, bnp, ap->a_runp, ap->a_runb, ret);
169439650Srvb#endif
169538625Srvb		return ret;
169638625Srvb	} else {
169741504Srvb#if	0
169839085Srvb		printf("coda_bmap: no container\n");
169941504Srvb#endif
170038625Srvb		return(EOPNOTSUPP);
170138625Srvb	}
170238625Srvb}
170338625Srvb
170438625Srvb/*
170538625Srvb * I don't think the following two things are used anywhere, so I've
170638625Srvb * commented them out
170738625Srvb *
170838625Srvb * struct buf *async_bufhead;
170938625Srvb * int async_daemon_count;
171038625Srvb */
171138625Srvbint
171239085Srvbcoda_strategy(v)
171338625Srvb    void *v;
171438625Srvb{
171538625Srvb/* true args */
171638625Srvb    struct vop_strategy_args *ap = v;
171738625Srvb    register struct buf *bp __attribute__((unused)) = ap->a_bp;
171838625Srvb    struct proc *p __attribute__((unused)) = curproc;
171938625Srvb/* upcall decl */
172038625Srvb/* locals */
172138625Srvb
172239085Srvb	printf("coda_strategy: called ???\n");
172338625Srvb	return(EOPNOTSUPP);
172438625Srvb}
172538625Srvb
172638625Srvbint
172739085Srvbcoda_reclaim(v)
172838625Srvb    void *v;
172938625Srvb{
173038625Srvb/* true args */
173138625Srvb    struct vop_reclaim_args *ap = v;
173238625Srvb    struct vnode *vp = ap->a_vp;
173338625Srvb    struct cnode *cp = VTOC(vp);
173438625Srvb/* upcall decl */
173538625Srvb/* locals */
173638625Srvb
173738625Srvb/*
173838625Srvb * Forced unmount/flush will let vnodes with non zero use be destroyed!
173938625Srvb */
174038625Srvb    ENTRY;
174138625Srvb
174238625Srvb    if (IS_UNMOUNTING(cp)) {
174338625Srvb#ifdef	DEBUG
174438625Srvb	if (VTOC(vp)->c_ovp) {
174538625Srvb	    if (IS_UNMOUNTING(cp))
174639085Srvb		printf("coda_reclaim: c_ovp not void: vp %p, cp %p\n", vp, cp);
174738625Srvb	}
174838625Srvb#endif
174938625Srvb    } else {
175039650Srvb#ifdef OLD_DIAGNOSTIC
175138625Srvb	if (vp->v_usecount != 0)
175239650Srvb	    print("coda_reclaim: pushing active %p\n", vp);
175338625Srvb	if (VTOC(vp)->c_ovp) {
175439085Srvb	    panic("coda_reclaim: c_ovp not void");
175538625Srvb    }
175639650Srvb#endif
175738625Srvb    }
175838625Srvb    cache_purge(vp);
175966615Sjasone    lockdestroy(&(VTOC(vp)->c_lock));
176039085Srvb    coda_free(VTOC(vp));
176138625Srvb    VTOC(vp) = NULL;
176238625Srvb    return (0);
176338625Srvb}
176438625Srvb
176538625Srvbint
176639085Srvbcoda_lock(v)
176738625Srvb    void *v;
176838625Srvb{
176938625Srvb/* true args */
177038625Srvb    struct vop_lock_args *ap = v;
177138625Srvb    struct vnode *vp = ap->a_vp;
177238625Srvb    struct cnode *cp = VTOC(vp);
177338625Srvb    struct proc  *p = ap->a_p;
177438625Srvb/* upcall decl */
177538625Srvb/* locals */
177638625Srvb
177738625Srvb    ENTRY;
177838625Srvb
177939085Srvb    if (coda_lockdebug) {
178038625Srvb	myprintf(("Attempting lock on %lx.%lx.%lx\n",
178138625Srvb		  cp->c_fid.Volume, cp->c_fid.Vnode, cp->c_fid.Unique));
178238625Srvb    }
178338625Srvb
178442900Seivind#ifndef	DEBUG_LOCKS
178538759Srvb    return (lockmgr(&cp->c_lock, ap->a_flags, &vp->v_interlock, p));
178642900Seivind#else
178742900Seivind    return (debuglockmgr(&cp->c_lock, ap->a_flags, &vp->v_interlock, p,
178842900Seivind			 "coda_lock", vp->filename, vp->line));
178942900Seivind#endif
179038625Srvb}
179138625Srvb
179238625Srvbint
179339085Srvbcoda_unlock(v)
179438625Srvb    void *v;
179538625Srvb{
179638625Srvb/* true args */
179738625Srvb    struct vop_unlock_args *ap = v;
179838625Srvb    struct vnode *vp = ap->a_vp;
179938625Srvb    struct cnode *cp = VTOC(vp);
180038625Srvb    struct proc  *p = ap->a_p;
180138625Srvb/* upcall decl */
180238625Srvb/* locals */
180338625Srvb
180438625Srvb    ENTRY;
180539085Srvb    if (coda_lockdebug) {
180638625Srvb	myprintf(("Attempting unlock on %lx.%lx.%lx\n",
180738625Srvb		  cp->c_fid.Volume, cp->c_fid.Vnode, cp->c_fid.Unique));
180838625Srvb    }
180938625Srvb
181038759Srvb    return (lockmgr(&cp->c_lock, ap->a_flags | LK_RELEASE, &vp->v_interlock, p));
181138625Srvb}
181238625Srvb
181338625Srvbint
181439085Srvbcoda_islocked(v)
181538625Srvb    void *v;
181638625Srvb{
181738625Srvb/* true args */
181838625Srvb    struct vop_islocked_args *ap = v;
181938625Srvb    struct cnode *cp = VTOC(ap->a_vp);
182038625Srvb    ENTRY;
182138625Srvb
182254444Seivind    return (lockstatus(&cp->c_lock, ap->a_p));
182338625Srvb}
182438625Srvb
182538625Srvb/* How one looks up a vnode given a device/inode pair: */
182638625Srvbint
182739085Srvbcoda_grab_vnode(dev_t dev, ino_t ino, struct vnode **vpp)
182838625Srvb{
182938625Srvb    /* This is like VFS_VGET() or igetinode()! */
183038625Srvb    int           error;
183138625Srvb    struct mount *mp;
183238625Srvb
183338625Srvb    if (!(mp = devtomp(dev))) {
183449524Sbde	myprintf(("coda_grab_vnode: devtomp(%#lx) returns NULL\n",
183549524Sbde		  (u_long)dev2udev(dev)));
183638625Srvb	return(ENXIO);
183738625Srvb    }
183838625Srvb
183938625Srvb    /* XXX - ensure that nonzero-return means failure */
184038625Srvb    error = VFS_VGET(mp,ino,vpp);
184138625Srvb    if (error) {
184249524Sbde	myprintf(("coda_grab_vnode: iget/vget(%lx, %lu) returns %p, err %d\n",
184349524Sbde		  (u_long)dev2udev(dev), (u_long)ino, (void *)*vpp, error));
184438625Srvb	return(ENOENT);
184538625Srvb    }
184638625Srvb    return(0);
184738625Srvb}
184838625Srvb
184938625Srvbvoid
185038625Srvbprint_vattr( attr )
185138625Srvb	struct vattr *attr;
185238625Srvb{
185338625Srvb    char *typestr;
185438625Srvb
185538625Srvb    switch (attr->va_type) {
185638625Srvb    case VNON:
185738625Srvb	typestr = "VNON";
185838625Srvb	break;
185938625Srvb    case VREG:
186038625Srvb	typestr = "VREG";
186138625Srvb	break;
186238625Srvb    case VDIR:
186338625Srvb	typestr = "VDIR";
186438625Srvb	break;
186538625Srvb    case VBLK:
186638625Srvb	typestr = "VBLK";
186738625Srvb	break;
186838625Srvb    case VCHR:
186938625Srvb	typestr = "VCHR";
187038625Srvb	break;
187138625Srvb    case VLNK:
187238625Srvb	typestr = "VLNK";
187338625Srvb	break;
187438625Srvb    case VSOCK:
187538625Srvb	typestr = "VSCK";
187638625Srvb	break;
187738625Srvb    case VFIFO:
187838625Srvb	typestr = "VFFO";
187938625Srvb	break;
188038625Srvb    case VBAD:
188138625Srvb	typestr = "VBAD";
188238625Srvb	break;
188338625Srvb    default:
188438625Srvb	typestr = "????";
188538625Srvb	break;
188638625Srvb    }
188738625Srvb
188838625Srvb
188938625Srvb    myprintf(("attr: type %s mode %d uid %d gid %d fsid %d rdev %d\n",
189038625Srvb	      typestr, (int)attr->va_mode, (int)attr->va_uid,
189138625Srvb	      (int)attr->va_gid, (int)attr->va_fsid, (int)attr->va_rdev));
189238625Srvb
189338625Srvb    myprintf(("      fileid %d nlink %d size %d blocksize %d bytes %d\n",
189438625Srvb	      (int)attr->va_fileid, (int)attr->va_nlink,
189538625Srvb	      (int)attr->va_size,
189638625Srvb	      (int)attr->va_blocksize,(int)attr->va_bytes));
189738625Srvb    myprintf(("      gen %ld flags %ld vaflags %d\n",
189838625Srvb	      attr->va_gen, attr->va_flags, attr->va_vaflags));
189938625Srvb    myprintf(("      atime sec %d nsec %d\n",
190038625Srvb	      (int)attr->va_atime.tv_sec, (int)attr->va_atime.tv_nsec));
190138625Srvb    myprintf(("      mtime sec %d nsec %d\n",
190238625Srvb	      (int)attr->va_mtime.tv_sec, (int)attr->va_mtime.tv_nsec));
190338625Srvb    myprintf(("      ctime sec %d nsec %d\n",
190438625Srvb	      (int)attr->va_ctime.tv_sec, (int)attr->va_ctime.tv_nsec));
190538625Srvb}
190638625Srvb
190738625Srvb/* How to print a ucred */
190838625Srvbvoid
190938625Srvbprint_cred(cred)
191038625Srvb	struct ucred *cred;
191138625Srvb{
191238625Srvb
191338625Srvb	int i;
191438625Srvb
191538625Srvb	myprintf(("ref %d\tuid %d\n",cred->cr_ref,cred->cr_uid));
191638625Srvb
191738625Srvb	for (i=0; i < cred->cr_ngroups; i++)
191838625Srvb		myprintf(("\tgroup %d: (%d)\n",i,cred->cr_groups[i]));
191938625Srvb	myprintf(("\n"));
192038625Srvb
192138625Srvb}
192238625Srvb
192338625Srvb/*
192438625Srvb * Return a vnode for the given fid.
192538625Srvb * If no cnode exists for this fid create one and put it
192638625Srvb * in a table hashed by fid.Volume and fid.Vnode.  If the cnode for
192738625Srvb * this fid is already in the table return it (ref count is
192839085Srvb * incremented by coda_find.  The cnode will be flushed from the
192939085Srvb * table when coda_inactive calls coda_unsave.
193038625Srvb */
193138625Srvbstruct cnode *
193239085Srvbmake_coda_node(fid, vfsp, type)
193338625Srvb     ViceFid *fid; struct mount *vfsp; short type;
193438625Srvb{
193538625Srvb    struct cnode *cp;
193638625Srvb    int          err;
193738625Srvb
193839085Srvb    if ((cp = coda_find(fid)) == NULL) {
193938625Srvb	struct vnode *vp;
194038625Srvb
194139085Srvb	cp = coda_alloc();
194238625Srvb	lockinit(&cp->c_lock, PINOD, "cnode", 0, 0);
194338625Srvb	cp->c_fid = *fid;
194438625Srvb
194539085Srvb	err = getnewvnode(VT_CODA, vfsp, coda_vnodeop_p, &vp);
194638625Srvb	if (err) {
194739085Srvb	    panic("coda: getnewvnode returned error %d\n", err);
194838625Srvb	}
194938625Srvb	vp->v_data = cp;
195038625Srvb	vp->v_type = type;
195138625Srvb	cp->c_vnode = vp;
195239085Srvb	coda_save(cp);
195338625Srvb
195438625Srvb    } else {
195538625Srvb	vref(CTOV(cp));
195638625Srvb    }
195738625Srvb
195838625Srvb    return cp;
195938625Srvb}
1960