coda_vnops.c revision 175473
1139745Simp/*-
238759Srvb *             Coda: an Experimental Distributed File System
338759Srvb *                              Release 3.1
438759Srvb *
538759Srvb *           Copyright (c) 1987-1998 Carnegie Mellon University
638759Srvb *                          All Rights Reserved
738759Srvb *
838759Srvb * Permission  to  use, copy, modify and distribute this software and its
938759Srvb * documentation is hereby granted,  provided  that  both  the  copyright
1038759Srvb * notice  and  this  permission  notice  appear  in  all  copies  of the
1138759Srvb * software, derivative works or  modified  versions,  and  any  portions
1238759Srvb * thereof, and that both notices appear in supporting documentation, and
1338759Srvb * that credit is given to Carnegie Mellon University  in  all  documents
1438759Srvb * and publicity pertaining to direct or indirect use of this code or its
1538759Srvb * derivatives.
1638759Srvb *
1738759Srvb * CODA IS AN EXPERIMENTAL SOFTWARE SYSTEM AND IS  KNOWN  TO  HAVE  BUGS,
1838759Srvb * SOME  OF  WHICH MAY HAVE SERIOUS CONSEQUENCES.  CARNEGIE MELLON ALLOWS
1938759Srvb * FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.   CARNEGIE  MELLON
2038759Srvb * DISCLAIMS  ANY  LIABILITY  OF  ANY  KIND  FOR  ANY  DAMAGES WHATSOEVER
2138759Srvb * RESULTING DIRECTLY OR INDIRECTLY FROM THE USE OF THIS SOFTWARE  OR  OF
2238759Srvb * ANY DERIVATIVE WORK.
2338759Srvb *
2438759Srvb * Carnegie  Mellon  encourages  users  of  this  software  to return any
2538759Srvb * improvements or extensions that  they  make,  and  to  grant  Carnegie
2638759Srvb * Mellon the rights to redistribute these changes without encumbrance.
2738759Srvb *
2839126Srvb *  	@(#) src/sys/coda/coda_vnops.c,v 1.1.1.1 1998/08/29 21:14:52 rvb Exp $
2938759Srvb */
3038625Srvb/*
3138625Srvb * Mach Operating System
3238625Srvb * Copyright (c) 1990 Carnegie-Mellon University
3338625Srvb * Copyright (c) 1989 Carnegie-Mellon University
3438625Srvb * All rights reserved.  The CMU software License Agreement specifies
3538625Srvb * the terms and conditions for use and redistribution.
3638625Srvb */
3738625Srvb
3838625Srvb/*
3996755Strhodes * This code was written for the Coda filesystem at Carnegie Mellon
4038625Srvb * University.  Contributers include David Steere, James Kistler, and
4138625Srvb * M. Satyanarayanan.
4238625Srvb */
4338625Srvb
44116173Sobrien#include <sys/cdefs.h>
45116173Sobrien__FBSDID("$FreeBSD: head/sys/fs/coda/coda_vnops.c 175473 2008-01-19 13:27:14Z rwatson $");
46116173Sobrien
4738625Srvb#include <sys/param.h>
4838625Srvb#include <sys/systm.h>
4976166Smarkm#include <sys/acct.h>
5076166Smarkm#include <sys/errno.h>
5176166Smarkm#include <sys/fcntl.h>
5238759Srvb#include <sys/kernel.h>
5376166Smarkm#include <sys/lock.h>
5476166Smarkm#include <sys/malloc.h>
5576166Smarkm#include <sys/file.h>		/* Must come after sys/malloc.h */
5676166Smarkm#include <sys/mount.h>
5776166Smarkm#include <sys/mutex.h>
5876166Smarkm#include <sys/namei.h>
5938759Srvb#include <sys/proc.h>
6038625Srvb#include <sys/uio.h>
61111903Stjr#include <sys/unistd.h>
6238759Srvb
6338625Srvb#include <vm/vm.h>
6438625Srvb#include <vm/vm_object.h>
6538625Srvb#include <vm/vm_extern.h>
6638625Srvb
67171416Srwatson#include <fs/coda/coda.h>
68171416Srwatson#include <fs/coda/cnode.h>
69171416Srwatson#include <fs/coda/coda_vnops.h>
70171416Srwatson#include <fs/coda/coda_venus.h>
71171416Srwatson#include <fs/coda/coda_opstats.h>
72171416Srwatson#include <fs/coda/coda_subr.h>
73171416Srwatson#include <fs/coda/coda_namecache.h>
74171416Srwatson#include <fs/coda/coda_pioctl.h>
7538625Srvb
7638625Srvb/*
7738625Srvb * These flags select various performance enhancements.
7838625Srvb */
7939085Srvbint coda_attr_cache  = 1;       /* Set to cache attributes in the kernel */
8039085Srvbint coda_symlink_cache = 1;     /* Set to cache symbolic link information */
8139085Srvbint coda_access_cache = 1;      /* Set to handle some access checks directly */
8238625Srvb
8338625Srvb/* structure to keep track of vfs calls */
8438625Srvb
8539085Srvbstruct coda_op_stats coda_vnodeopstats[CODA_VNODEOPS_SIZE];
8638625Srvb
8739085Srvb#define MARK_ENTRY(op) (coda_vnodeopstats[op].entries++)
8839085Srvb#define MARK_INT_SAT(op) (coda_vnodeopstats[op].sat_intrn++)
8939085Srvb#define MARK_INT_FAIL(op) (coda_vnodeopstats[op].unsat_intrn++)
9039085Srvb#define MARK_INT_GEN(op) (coda_vnodeopstats[op].gen_intrn++)
9138625Srvb
9238625Srvb/* What we are delaying for in printf */
9339085Srvbint coda_printf_delay = 0;  /* in microseconds */
9439085Srvbint coda_vnop_print_entry = 0;
9539085Srvbstatic int coda_lockdebug = 0;
9638625Srvb
9738625Srvb/*
9838625Srvb * Some NetBSD details:
9938625Srvb *
10039085Srvb *   coda_start is called at the end of the mount syscall.
10139085Srvb *   coda_init is called at boot time.
10238625Srvb */
10338625Srvb
10487599Sobrien#define ENTRY  if(coda_vnop_print_entry) myprintf(("Entered %s\n",__func__))
10538625Srvb
10638625Srvb/* Definition of the vnode operation vector */
10738625Srvb
108138290Sphkstruct vop_vector coda_vnodeops = {
109138290Sphk    .vop_default = VOP_PANIC,
110138290Sphk    .vop_lookup = coda_lookup,		/* lookup */
111138290Sphk    .vop_create = coda_create,		/* create */
112138290Sphk    .vop_mknod = VOP_PANIC,	/* mknod */
113138290Sphk    .vop_open = coda_open,		/* open */
114138290Sphk    .vop_close = coda_close,		/* close */
115138290Sphk    .vop_access = coda_access,		/* access */
116138290Sphk    .vop_getattr = coda_getattr,	/* getattr */
117138290Sphk    .vop_setattr = coda_setattr,	/* setattr */
118138290Sphk    .vop_read = coda_read,		/* read */
119138290Sphk    .vop_write = coda_write,		/* write */
120138290Sphk    .vop_ioctl = coda_ioctl,		/* ioctl */
121138290Sphk    .vop_fsync = coda_fsync,		/* fsync */
122138290Sphk    .vop_remove = coda_remove,		/* remove */
123138290Sphk    .vop_link = coda_link,		/* link */
124138290Sphk    .vop_rename = coda_rename,		/* rename */
125138290Sphk    .vop_mkdir = coda_mkdir,		/* mkdir */
126138290Sphk    .vop_rmdir = coda_rmdir,		/* rmdir */
127138290Sphk    .vop_symlink = coda_symlink,	/* symlink */
128138290Sphk    .vop_readdir = coda_readdir,	/* readdir */
129138290Sphk    .vop_readlink = coda_readlink,	/* readlink */
130138290Sphk    .vop_inactive = coda_inactive,	/* inactive */
131138290Sphk    .vop_reclaim = coda_reclaim,	/* reclaim */
132169671Skib    .vop_lock1 = coda_lock,		/* lock */
133138290Sphk    .vop_unlock = coda_unlock,		/* unlock */
134138290Sphk    .vop_bmap = coda_bmap,		/* bmap */
135138290Sphk    .vop_print = VOP_PANIC,	/* print */
136138290Sphk    .vop_islocked = coda_islocked,	/* islocked */
137138290Sphk    .vop_pathconf = coda_pathconf,	/* pathconf */
138138290Sphk    .vop_advlock = VOP_NULL,	/* advlock */
139138290Sphk    .vop_lease = VOP_NULL,		/* lease */
140138290Sphk    .vop_poll = vop_stdpoll,
141138290Sphk    .vop_getpages = vop_stdgetpages,	/* pager intf.*/
142138290Sphk    .vop_putpages = vop_stdputpages,	/* pager intf.*/
143138290Sphk    .vop_getwritemount =	vop_stdgetwritemount,
14438625Srvb
145138290Sphk#if 0
14638625Srvb    missing
147138290Sphk    .vop_cachedlookup =	ufs_lookup,
148138290Sphk    .vop_whiteout =	ufs_whiteout,
14938625Srvb#endif
15077784Sshafeeq
15138625Srvb};
15238625Srvb
15338625Srvb/* A generic do-nothing.  For lease_check, advlock */
15438625Srvbint
15539085Srvbcoda_vop_nop(void *anon) {
15638625Srvb    struct vnodeop_desc **desc = (struct vnodeop_desc **)anon;
15738625Srvb
15839085Srvb    if (codadebug) {
15938625Srvb	myprintf(("Vnode operation %s called, but unsupported\n",
16038625Srvb		  (*desc)->vdesc_name));
16138625Srvb    }
16238625Srvb   return (0);
16338625Srvb}
16438625Srvb
16538625Srvbint
16639085Srvbcoda_vnodeopstats_init(void)
16738625Srvb{
16838625Srvb	register int i;
16938625Srvb
17039085Srvb	for(i=0;i<CODA_VNODEOPS_SIZE;i++) {
17139085Srvb		coda_vnodeopstats[i].opcode = i;
17239085Srvb		coda_vnodeopstats[i].entries = 0;
17339085Srvb		coda_vnodeopstats[i].sat_intrn = 0;
17439085Srvb		coda_vnodeopstats[i].unsat_intrn = 0;
17539085Srvb		coda_vnodeopstats[i].gen_intrn = 0;
17638625Srvb	}
17738625Srvb	return 0;
17838625Srvb}
17938625Srvb
18038625Srvb/*
181171377Srwatson * coda_open calls Venus which returns an open file descriptor the cache
182171377Srwatson * file holding the data. We get the vnode while we are still in the
183171377Srwatson * context of the venus process in coda_psdev.c. This vnode is then
184171377Srwatson * passed back to the caller and opened.
18538625Srvb */
18638625Srvbint
187138290Sphkcoda_open(struct vop_open_args *ap)
18838625Srvb{
18938625Srvb    /*
19038625Srvb     * NetBSD can pass the O_EXCL flag in mode, even though the check
19138625Srvb     * has already happened.  Venus defensively assumes that if open
19238625Srvb     * is passed the EXCL, it must be a bug.  We strip the flag here.
19338625Srvb     */
19438625Srvb/* true args */
19538625Srvb    register struct vnode **vpp = &(ap->a_vp);
19638625Srvb    struct cnode *cp = VTOC(*vpp);
19738625Srvb    int flag = ap->a_mode & (~O_EXCL);
19838625Srvb    struct ucred *cred = ap->a_cred;
19983366Sjulian    struct thread *td = ap->a_td;
20038625Srvb/* locals */
20138625Srvb    int error;
20238625Srvb    struct vnode *vp;
20338625Srvb
20439085Srvb    MARK_ENTRY(CODA_OPEN_STATS);
20538625Srvb
20638625Srvb    /* Check for open of control file. */
20738625Srvb    if (IS_CTL_VP(*vpp)) {
20838625Srvb	/* XXX */
20938625Srvb	/* if (WRITEABLE(flag)) */
21038625Srvb	if (flag & (FWRITE | O_TRUNC | O_CREAT | O_EXCL)) {
21139085Srvb	    MARK_INT_FAIL(CODA_OPEN_STATS);
21238625Srvb	    return(EACCES);
21338625Srvb	}
21439085Srvb	MARK_INT_SAT(CODA_OPEN_STATS);
21538625Srvb	return(0);
21638625Srvb    }
21738625Srvb
218171377Srwatson    error = venus_open(vtomi((*vpp)), &cp->c_fid, flag, cred, td->td_proc, &vp);
21938625Srvb    if (error)
22038625Srvb	return (error);
22138625Srvb
222171377Srwatson    CODADEBUG( CODA_OPEN,myprintf(("open: vp %p result %d\n", vp, error));)
22338625Srvb
22438625Srvb    /* Keep a reference until the close comes in. */
22538625Srvb    vref(*vpp);
22638625Srvb
22738625Srvb    /* Save the vnode pointer for the cache file. */
22838625Srvb    if (cp->c_ovp == NULL) {
22938625Srvb	cp->c_ovp = vp;
23038625Srvb    } else {
23138625Srvb	if (cp->c_ovp != vp)
23239085Srvb	    panic("coda_open:  cp->c_ovp != ITOV(ip)");
23338625Srvb    }
23438625Srvb    cp->c_ocount++;
23538625Srvb
23638625Srvb    /* Flush the attribute cached if writing the file. */
23738625Srvb    if (flag & FWRITE) {
23838625Srvb	cp->c_owrite++;
23938625Srvb	cp->c_flags &= ~C_VATTR;
24038625Srvb    }
24138625Srvb
24238625Srvb    /* Open the cache file. */
243170152Skib    error = VOP_OPEN(vp, flag, cred, td, NULL);
24438625Srvb    if (error) {
24539085Srvb    	printf("coda_open: VOP_OPEN on container failed %d\n", error);
24638625Srvb	return (error);
247175473Srwatson    } else {
248175473Srwatson	(*vpp)->v_object = vp->v_object;
24938625Srvb    }
25039650Srvb/* grab (above) does this when it calls newvnode unless it's in the cache*/
25139650Srvb
25238625Srvb    return(error);
25338625Srvb}
25438625Srvb
25538625Srvb/*
25638625Srvb * Close the cache file used for I/O and notify Venus.
25738625Srvb */
25838625Srvbint
259138290Sphkcoda_close(struct vop_close_args *ap)
26038625Srvb{
26138625Srvb/* true args */
26238625Srvb    struct vnode *vp = ap->a_vp;
26338625Srvb    struct cnode *cp = VTOC(vp);
26438625Srvb    int flag = ap->a_fflag;
26538625Srvb    struct ucred *cred = ap->a_cred;
26683366Sjulian    struct thread *td = ap->a_td;
26738625Srvb/* locals */
26838625Srvb    int error;
26938625Srvb
27039085Srvb    MARK_ENTRY(CODA_CLOSE_STATS);
27138625Srvb
27238625Srvb    /* Check for close of control file. */
27338625Srvb    if (IS_CTL_VP(vp)) {
27439085Srvb	MARK_INT_SAT(CODA_CLOSE_STATS);
27538625Srvb	return(0);
27638625Srvb    }
27738625Srvb
278171377Srwatson    if (cp->c_ovp) {
27983366Sjulian	VOP_CLOSE(cp->c_ovp, flag, cred, td); /* Do errors matter here? */
28038625Srvb	vrele(cp->c_ovp);
28138625Srvb    }
282171377Srwatson#ifdef CODA_VERBOSE
283171377Srwatson    else printf("coda_close: NO container vp %p/cp %p\n", vp, cp);
284171377Srwatson#endif
28538625Srvb
28638625Srvb    if (--cp->c_ocount == 0)
28738625Srvb	cp->c_ovp = NULL;
28838625Srvb
28938625Srvb    if (flag & FWRITE)                    /* file was opened for write */
29038625Srvb	--cp->c_owrite;
29138625Srvb
292171377Srwatson    if (!IS_UNMOUNTING(cp))
293171377Srwatson         error = venus_close(vtomi(vp), &cp->c_fid, flag, cred, td->td_proc);
294171377Srwatson    else error = ENODEV;
29538625Srvb
296171377Srwatson    vrele(vp);
297171377Srwatson
29839085Srvb    CODADEBUG(CODA_CLOSE, myprintf(("close: result %d\n",error)); )
29938625Srvb    return(error);
30038625Srvb}
30138625Srvb
30238625Srvbint
303138290Sphkcoda_read(struct vop_read_args *ap)
30438625Srvb{
30538625Srvb
30638625Srvb    ENTRY;
30739085Srvb    return(coda_rdwr(ap->a_vp, ap->a_uio, UIO_READ,
30883366Sjulian		    ap->a_ioflag, ap->a_cred, ap->a_uio->uio_td));
30938625Srvb}
31038625Srvb
31138625Srvbint
312138290Sphkcoda_write(struct vop_write_args *ap)
31338625Srvb{
31438625Srvb
31538625Srvb    ENTRY;
31639085Srvb    return(coda_rdwr(ap->a_vp, ap->a_uio, UIO_WRITE,
31783366Sjulian		    ap->a_ioflag, ap->a_cred, ap->a_uio->uio_td));
31838625Srvb}
31938625Srvb
32038625Srvbint
321154647Srwatsoncoda_rdwr(struct vnode *vp, struct uio *uiop, enum uio_rw rw, int ioflag,
322154647Srwatson    struct ucred *cred, struct thread *td)
32338625Srvb{
32438625Srvb/* upcall decl */
32538625Srvb  /* NOTE: container file operation!!! */
32638625Srvb/* locals */
32738625Srvb    struct cnode *cp = VTOC(vp);
32838625Srvb    struct vnode *cfvp = cp->c_ovp;
32938625Srvb    int opened_internally = 0;
33038625Srvb    int error = 0;
33138625Srvb
33239085Srvb    MARK_ENTRY(CODA_RDWR_STATS);
33338625Srvb
33449524Sbde    CODADEBUG(CODA_RDWR, myprintf(("coda_rdwr(%d, %p, %d, %lld, %d)\n", rw,
33549524Sbde			      (void *)uiop->uio_iov->iov_base, uiop->uio_resid,
33649524Sbde			      (long long)uiop->uio_offset, uiop->uio_segflg)); )
33738625Srvb
33838625Srvb    /* Check for rdwr of control object. */
33938625Srvb    if (IS_CTL_VP(vp)) {
34039085Srvb	MARK_INT_FAIL(CODA_RDWR_STATS);
34138625Srvb	return(EINVAL);
34238625Srvb    }
34338625Srvb
34438625Srvb    /*
345171377Srwatson     * If file is not already open this must be a page {read,write} request
346171377Srwatson     * and we should open it internally.
34738625Srvb     */
34838625Srvb    if (cfvp == NULL) {
349171377Srwatson	opened_internally = 1;
350171377Srwatson	MARK_INT_GEN(CODA_OPEN_STATS);
351171377Srwatson	error = VOP_OPEN(vp, (rw == UIO_READ ? FREAD : FWRITE), cred, td, NULL);
352171377Srwatson	printf("coda_rdwr: Internally Opening %p\n", vp);
353171377Srwatson	if (error) {
35439085Srvb		printf("coda_rdwr: VOP_OPEN on container failed %d\n", error);
35538625Srvb		return (error);
35638625Srvb	}
357171377Srwatson	cfvp = cp->c_ovp;
35838625Srvb    }
35938625Srvb
36038625Srvb    /* Have UFS handle the call. */
361119832Stjr    CODADEBUG(CODA_RDWR, myprintf(("indirect rdwr: fid = %s, refcnt = %d\n",
362119832Stjr			     coda_f2s(&cp->c_fid), CTOV(cp)->v_usecount)); )
36338625Srvb    if (rw == UIO_READ) {
36438625Srvb	error = VOP_READ(cfvp, uiop, ioflag, cred);
36538625Srvb    } else {
36638625Srvb	error = VOP_WRITE(cfvp, uiop, ioflag, cred);
36738625Srvb	/* ufs_write updates the vnode_pager_setsize for the vnode/object */
36839650Srvb
36938625Srvb	{   struct vattr attr;
37038625Srvb
37183366Sjulian	    if (VOP_GETATTR(cfvp, &attr, cred, td) == 0) {
37238625Srvb		vnode_pager_setsize(vp, attr.va_size);
37338625Srvb	    }
37438625Srvb	}
37538625Srvb    }
37638625Srvb
37738625Srvb    if (error)
37839085Srvb	MARK_INT_FAIL(CODA_RDWR_STATS);
37938625Srvb    else
38039085Srvb	MARK_INT_SAT(CODA_RDWR_STATS);
38138625Srvb
38238625Srvb    /* Do an internal close if necessary. */
38338625Srvb    if (opened_internally) {
38439085Srvb	MARK_INT_GEN(CODA_CLOSE_STATS);
38583366Sjulian	(void)VOP_CLOSE(vp, (rw == UIO_READ ? FREAD : FWRITE), cred, td);
38638625Srvb    }
38738625Srvb
38838625Srvb    /* Invalidate cached attributes if writing. */
38938625Srvb    if (rw == UIO_WRITE)
39038625Srvb	cp->c_flags &= ~C_VATTR;
39138625Srvb    return(error);
39238625Srvb}
39338625Srvb
39477784Sshafeeq
39577784Sshafeeq
39638625Srvbint
397138290Sphkcoda_ioctl(struct vop_ioctl_args *ap)
39838625Srvb{
39938625Srvb/* true args */
40038625Srvb    struct vnode *vp = ap->a_vp;
40138625Srvb    int com = ap->a_command;
40238625Srvb    caddr_t data = ap->a_data;
40338625Srvb    int flag = ap->a_fflag;
40438625Srvb    struct ucred *cred = ap->a_cred;
40583366Sjulian    struct thread *td = ap->a_td;
40638625Srvb/* locals */
40738625Srvb    int error;
40838625Srvb    struct vnode *tvp;
40938625Srvb    struct nameidata ndp;
41038625Srvb    struct PioctlData *iap = (struct PioctlData *)data;
41138625Srvb
41239085Srvb    MARK_ENTRY(CODA_IOCTL_STATS);
41338625Srvb
41439085Srvb    CODADEBUG(CODA_IOCTL, myprintf(("in coda_ioctl on %s\n", iap->path));)
41538625Srvb
41638625Srvb    /* Don't check for operation on a dying object, for ctlvp it
41738625Srvb       shouldn't matter */
41838625Srvb
41938625Srvb    /* Must be control object to succeed. */
42038625Srvb    if (!IS_CTL_VP(vp)) {
42139085Srvb	MARK_INT_FAIL(CODA_IOCTL_STATS);
42239085Srvb	CODADEBUG(CODA_IOCTL, myprintf(("coda_ioctl error: vp != ctlvp"));)
42338625Srvb	    return (EOPNOTSUPP);
42438625Srvb    }
42538625Srvb    /* Look up the pathname. */
42638625Srvb
42738625Srvb    /* Should we use the name cache here? It would get it from
42838625Srvb       lookupname sooner or later anyway, right? */
42938625Srvb
43083366Sjulian    NDINIT(&ndp, LOOKUP, (iap->follow ? FOLLOW : NOFOLLOW), UIO_USERSPACE, iap->path, td);
43138625Srvb    error = namei(&ndp);
43238625Srvb    tvp = ndp.ni_vp;
43338625Srvb
43438625Srvb    if (error) {
43539085Srvb	MARK_INT_FAIL(CODA_IOCTL_STATS);
43639085Srvb	CODADEBUG(CODA_IOCTL, myprintf(("coda_ioctl error: lookup returns %d\n",
43738625Srvb				   error));)
43838625Srvb	return(error);
43938625Srvb    }
44038625Srvb
44138625Srvb    /*
44238625Srvb     * Make sure this is a coda style cnode, but it may be a
44338625Srvb     * different vfsp
44438625Srvb     */
445138290Sphk    if (tvp->v_op != &coda_vnodeops) {
44638625Srvb	vrele(tvp);
44754655Seivind	NDFREE(&ndp, NDF_ONLY_PNBUF);
44839085Srvb	MARK_INT_FAIL(CODA_IOCTL_STATS);
44939085Srvb	CODADEBUG(CODA_IOCTL,
45039085Srvb		 myprintf(("coda_ioctl error: %s not a coda object\n",
45138625Srvb			iap->path));)
45238625Srvb	return(EINVAL);
45338625Srvb    }
45438625Srvb
45538625Srvb    if (iap->vi.in_size > VC_MAXDATASIZE) {
45654655Seivind	NDFREE(&ndp, 0);
45738625Srvb	return(EINVAL);
45838625Srvb    }
45983366Sjulian    error = venus_ioctl(vtomi(tvp), &((VTOC(tvp))->c_fid), com, flag, data, cred, td->td_proc);
46038625Srvb
46138625Srvb    if (error)
46239085Srvb	MARK_INT_FAIL(CODA_IOCTL_STATS);
46338625Srvb    else
46439085Srvb	CODADEBUG(CODA_IOCTL, myprintf(("Ioctl returns %d \n", error)); )
46538625Srvb
46638625Srvb    vrele(tvp);
46754655Seivind    NDFREE(&ndp, NDF_ONLY_PNBUF);
46838625Srvb    return(error);
46938625Srvb}
47038625Srvb
47138625Srvb/*
47238625Srvb * To reduce the cost of a user-level venus;we cache attributes in
47338625Srvb * the kernel.  Each cnode has storage allocated for an attribute. If
47438625Srvb * c_vattr is valid, return a reference to it. Otherwise, get the
47538625Srvb * attributes from venus and store them in the cnode.  There is some
47638625Srvb * question if this method is a security leak. But I think that in
47738625Srvb * order to make this call, the user must have done a lookup and
47838625Srvb * opened the file, and therefore should already have access.
47938625Srvb */
48038625Srvbint
481138290Sphkcoda_getattr(struct vop_getattr_args *ap)
48238625Srvb{
48338625Srvb/* true args */
48438625Srvb    struct vnode *vp = ap->a_vp;
48538625Srvb    struct cnode *cp = VTOC(vp);
48638625Srvb    struct vattr *vap = ap->a_vap;
48738625Srvb    struct ucred *cred = ap->a_cred;
48883366Sjulian    struct thread *td = ap->a_td;
48938625Srvb/* locals */
49038625Srvb    int error;
49138625Srvb
49239085Srvb    MARK_ENTRY(CODA_GETATTR_STATS);
49338625Srvb
49438625Srvb    if (IS_UNMOUNTING(cp))
49538625Srvb	return ENODEV;
49638759Srvb
49738625Srvb    /* Check for getattr of control object. */
49838625Srvb    if (IS_CTL_VP(vp)) {
49939085Srvb	MARK_INT_FAIL(CODA_GETATTR_STATS);
50038625Srvb	return(ENOENT);
50138625Srvb    }
50238625Srvb
50338625Srvb    /* Check to see if the attributes have already been cached */
50438625Srvb    if (VALID_VATTR(cp)) {
505119832Stjr	CODADEBUG(CODA_GETATTR, { myprintf(("attr cache hit: %s\n",
506119832Stjr					coda_f2s(&cp->c_fid)));});
50739085Srvb	CODADEBUG(CODA_GETATTR, if (!(codadebug & ~CODA_GETATTR))
50838625Srvb		 print_vattr(&cp->c_vattr); );
50938625Srvb
51038625Srvb	*vap = cp->c_vattr;
51139085Srvb	MARK_INT_SAT(CODA_GETATTR_STATS);
51238625Srvb	return(0);
51338625Srvb    }
51438625Srvb
51583366Sjulian    error = venus_getattr(vtomi(vp), &cp->c_fid, cred, td->td_proc, vap);
51638625Srvb
51738625Srvb    if (!error) {
518119832Stjr	CODADEBUG(CODA_GETATTR, myprintf(("getattr miss %s: result %d\n",
519119832Stjr				     coda_f2s(&cp->c_fid), error)); )
52038625Srvb
52139085Srvb	CODADEBUG(CODA_GETATTR, if (!(codadebug & ~CODA_GETATTR))
52238625Srvb		 print_vattr(vap);	);
52338625Srvb
52438625Srvb    {	int size = vap->va_size;
52538625Srvb    	struct vnode *convp = cp->c_ovp;
52638625Srvb	if (convp != (struct vnode *)0) {
52738625Srvb	    vnode_pager_setsize(convp, size);
52838625Srvb	}
52938625Srvb    }
53038625Srvb	/* If not open for write, store attributes in cnode */
53139085Srvb	if ((cp->c_owrite == 0) && (coda_attr_cache)) {
53238625Srvb	    cp->c_vattr = *vap;
53338625Srvb	    cp->c_flags |= C_VATTR;
53438625Srvb	}
53538625Srvb
53638625Srvb    }
53738625Srvb    return(error);
53838625Srvb}
53938625Srvb
54038625Srvbint
541138290Sphkcoda_setattr(struct vop_setattr_args *ap)
54238625Srvb{
54338625Srvb/* true args */
54438625Srvb    register struct vnode *vp = ap->a_vp;
54538625Srvb    struct cnode *cp = VTOC(vp);
54638625Srvb    register struct vattr *vap = ap->a_vap;
54738625Srvb    struct ucred *cred = ap->a_cred;
54883366Sjulian    struct thread *td = ap->a_td;
54938625Srvb/* locals */
55038625Srvb    int error;
55138625Srvb
55239085Srvb    MARK_ENTRY(CODA_SETATTR_STATS);
55338625Srvb
55438625Srvb    /* Check for setattr of control object. */
55538625Srvb    if (IS_CTL_VP(vp)) {
55639085Srvb	MARK_INT_FAIL(CODA_SETATTR_STATS);
55738625Srvb	return(ENOENT);
55838625Srvb    }
55938625Srvb
56039085Srvb    if (codadebug & CODADBGMSK(CODA_SETATTR)) {
56138625Srvb	print_vattr(vap);
56238625Srvb    }
56383366Sjulian    error = venus_setattr(vtomi(vp), &cp->c_fid, vap, cred, td->td_proc);
56438625Srvb
56538625Srvb    if (!error)
56638625Srvb	cp->c_flags &= ~C_VATTR;
56738625Srvb
56838625Srvb    {	int size = vap->va_size;
56938625Srvb    	struct vnode *convp = cp->c_ovp;
57038625Srvb	if (size != VNOVAL && convp != (struct vnode *)0) {
57138625Srvb	    vnode_pager_setsize(convp, size);
57238625Srvb	}
57338625Srvb    }
57439085Srvb    CODADEBUG(CODA_SETATTR,	myprintf(("setattr %d\n", error)); )
57538625Srvb    return(error);
57638625Srvb}
57738625Srvb
57838625Srvbint
579138290Sphkcoda_access(struct vop_access_args *ap)
58038625Srvb{
58138625Srvb/* true args */
58238625Srvb    struct vnode *vp = ap->a_vp;
58338625Srvb    struct cnode *cp = VTOC(vp);
58438625Srvb    int mode = ap->a_mode;
58538625Srvb    struct ucred *cred = ap->a_cred;
58683366Sjulian    struct thread *td = ap->a_td;
58738625Srvb/* locals */
58838625Srvb    int error;
58938625Srvb
59039085Srvb    MARK_ENTRY(CODA_ACCESS_STATS);
59138625Srvb
59238625Srvb    /* Check for access of control object.  Only read access is
59338625Srvb       allowed on it. */
59438625Srvb    if (IS_CTL_VP(vp)) {
59538625Srvb	/* bogus hack - all will be marked as successes */
59639085Srvb	MARK_INT_SAT(CODA_ACCESS_STATS);
59738625Srvb	return(((mode & VREAD) && !(mode & (VWRITE | VEXEC)))
59838625Srvb	       ? 0 : EACCES);
59938625Srvb    }
60038625Srvb
60138625Srvb    /*
60238625Srvb     * if the file is a directory, and we are checking exec (eg lookup)
60338625Srvb     * access, and the file is in the namecache, then the user must have
60438625Srvb     * lookup access to it.
60538625Srvb     */
60639085Srvb    if (coda_access_cache) {
60738625Srvb	if ((vp->v_type == VDIR) && (mode & VEXEC)) {
60839085Srvb	    if (coda_nc_lookup(cp, ".", 1, cred)) {
60939085Srvb		MARK_INT_SAT(CODA_ACCESS_STATS);
61038625Srvb		return(0);                     /* it was in the cache */
61138625Srvb	    }
61238625Srvb	}
61338625Srvb    }
61438625Srvb
61583366Sjulian    error = venus_access(vtomi(vp), &cp->c_fid, mode, cred, td->td_proc);
61638625Srvb
61738625Srvb    return(error);
61838625Srvb}
61938625Srvb
62038625Srvbint
621138290Sphkcoda_readlink(struct vop_readlink_args *ap)
62238625Srvb{
62338625Srvb/* true args */
62438625Srvb    struct vnode *vp = ap->a_vp;
62538625Srvb    struct cnode *cp = VTOC(vp);
62638625Srvb    struct uio *uiop = ap->a_uio;
62738625Srvb    struct ucred *cred = ap->a_cred;
62883366Sjulian    struct thread *td = ap->a_uio->uio_td;
62938625Srvb/* locals */
63038625Srvb    int error;
63138625Srvb    char *str;
63238625Srvb    int len;
63338625Srvb
63439085Srvb    MARK_ENTRY(CODA_READLINK_STATS);
63538625Srvb
63638625Srvb    /* Check for readlink of control object. */
63738625Srvb    if (IS_CTL_VP(vp)) {
63839085Srvb	MARK_INT_FAIL(CODA_READLINK_STATS);
63938625Srvb	return(ENOENT);
64038625Srvb    }
64138625Srvb
64239085Srvb    if ((coda_symlink_cache) && (VALID_SYMLINK(cp))) { /* symlink was cached */
64338625Srvb	uiop->uio_rw = UIO_READ;
64438625Srvb	error = uiomove(cp->c_symlink, (int)cp->c_symlen, uiop);
64538625Srvb	if (error)
64639085Srvb	    MARK_INT_FAIL(CODA_READLINK_STATS);
64738625Srvb	else
64839085Srvb	    MARK_INT_SAT(CODA_READLINK_STATS);
64938625Srvb	return(error);
65038625Srvb    }
65138625Srvb
652111902Stjr    error = venus_readlink(vtomi(vp), &cp->c_fid, cred,
653111902Stjr        td != NULL ? td->td_proc : NULL, &str, &len);
65438625Srvb
65538625Srvb    if (!error) {
65638625Srvb	uiop->uio_rw = UIO_READ;
65738625Srvb	error = uiomove(str, len, uiop);
65838625Srvb
65939085Srvb	if (coda_symlink_cache) {
66038625Srvb	    cp->c_symlink = str;
66138625Srvb	    cp->c_symlen = len;
66238625Srvb	    cp->c_flags |= C_SYMLINK;
66338625Srvb	} else
66439085Srvb	    CODA_FREE(str, len);
66538625Srvb    }
66638625Srvb
66739085Srvb    CODADEBUG(CODA_READLINK, myprintf(("in readlink result %d\n",error));)
66838625Srvb    return(error);
66938625Srvb}
67038625Srvb
67138625Srvbint
672138290Sphkcoda_fsync(struct vop_fsync_args *ap)
67338625Srvb{
67438625Srvb/* true args */
67538625Srvb    struct vnode *vp = ap->a_vp;
67638625Srvb    struct cnode *cp = VTOC(vp);
67783366Sjulian    struct thread *td = ap->a_td;
67838625Srvb/* locals */
67938625Srvb    struct vnode *convp = cp->c_ovp;
68038625Srvb    int error;
68138625Srvb
68239085Srvb    MARK_ENTRY(CODA_FSYNC_STATS);
68338625Srvb
68438625Srvb    /* Check for fsync on an unmounting object */
68538625Srvb    /* The NetBSD kernel, in it's infinite wisdom, can try to fsync
68638625Srvb     * after an unmount has been initiated.  This is a Bad Thing,
68738625Srvb     * which we have to avoid.  Not a legitimate failure for stats.
68838625Srvb     */
68938625Srvb    if (IS_UNMOUNTING(cp)) {
69038625Srvb	return(ENODEV);
69138625Srvb    }
69238625Srvb
69338625Srvb    /* Check for fsync of control object. */
69438625Srvb    if (IS_CTL_VP(vp)) {
69539085Srvb	MARK_INT_SAT(CODA_FSYNC_STATS);
69638625Srvb	return(0);
69738625Srvb    }
69838625Srvb
69938625Srvb    if (convp)
700140048Sphk    	VOP_FSYNC(convp, MNT_WAIT, td);
70138625Srvb
70238625Srvb    /*
70338625Srvb     * We see fsyncs with usecount == 1 then usecount == 0.
70438625Srvb     * For now we ignore them.
70538625Srvb     */
70638625Srvb    /*
707103937Sjeff    VI_LOCK(vp);
70838625Srvb    if (!vp->v_usecount) {
70939085Srvb    	printf("coda_fsync on vnode %p with %d usecount.  c_flags = %x (%x)\n",
71038625Srvb		vp, vp->v_usecount, cp->c_flags, cp->c_flags&C_PURGING);
71138625Srvb    }
712103937Sjeff    VI_UNLOCK(vp);
71338625Srvb    */
71438625Srvb
71538625Srvb    /*
71638625Srvb     * We can expect fsync on any vnode at all if venus is pruging it.
71738625Srvb     * Venus can't very well answer the fsync request, now can it?
71838625Srvb     * Hopefully, it won't have to, because hopefully, venus preserves
71938625Srvb     * the (possibly untrue) invariant that it never purges an open
72038625Srvb     * vnode.  Hopefully.
72138625Srvb     */
72238625Srvb    if (cp->c_flags & C_PURGING) {
72338625Srvb	return(0);
72438625Srvb    }
72538625Srvb
72638625Srvb    /* needs research */
72738625Srvb    return 0;
728140048Sphk    error = venus_fsync(vtomi(vp), &cp->c_fid, td->td_proc);
72938625Srvb
73039085Srvb    CODADEBUG(CODA_FSYNC, myprintf(("in fsync result %d\n",error)); );
73138625Srvb    return(error);
73238625Srvb}
73338625Srvb
73438625Srvbint
735138290Sphkcoda_inactive(struct vop_inactive_args *ap)
73638625Srvb{
73738625Srvb    /* XXX - at the moment, inactive doesn't look at cred, and doesn't
73838625Srvb       have a proc pointer.  Oops. */
73938625Srvb/* true args */
74038625Srvb    struct vnode *vp = ap->a_vp;
74138625Srvb    struct cnode *cp = VTOC(vp);
74238625Srvb    struct ucred *cred __attribute__((unused)) = NULL;
74383366Sjulian    struct thread *td __attribute__((unused)) = curthread;
74438625Srvb/* upcall decl */
74538625Srvb/* locals */
74638625Srvb
74738625Srvb    /* We don't need to send inactive to venus - DCS */
74839085Srvb    MARK_ENTRY(CODA_INACTIVE_STATS);
74938625Srvb
750119832Stjr    CODADEBUG(CODA_INACTIVE, myprintf(("in inactive, %s, vfsp %p\n",
751119832Stjr				  coda_f2s(&cp->c_fid), vp->v_mount));)
752175473Srwatson
753175473Srwatson    vp->v_object = NULL;
754119832Stjr
75538625Srvb    /* If an array has been allocated to hold the symlink, deallocate it */
75639085Srvb    if ((coda_symlink_cache) && (VALID_SYMLINK(cp))) {
75738625Srvb	if (cp->c_symlink == NULL)
75839085Srvb	    panic("coda_inactive: null symlink pointer in cnode");
75938625Srvb
76039085Srvb	CODA_FREE(cp->c_symlink, cp->c_symlen);
76138625Srvb	cp->c_flags &= ~C_SYMLINK;
76238625Srvb	cp->c_symlen = 0;
76338625Srvb    }
76438625Srvb
76538625Srvb    /* Remove it from the table so it can't be found. */
76639085Srvb    coda_unsave(cp);
76739085Srvb    if ((struct coda_mntinfo *)(vp->v_mount->mnt_data) == NULL) {
76838625Srvb	myprintf(("Help! vfsp->vfs_data was NULL, but vnode %p wasn't dying\n", vp));
76939085Srvb	panic("badness in coda_inactive\n");
77038625Srvb    }
77138625Srvb
77238625Srvb    if (IS_UNMOUNTING(cp)) {
77338625Srvb#ifdef	DEBUG
774103937Sjeff	printf("coda_inactive: IS_UNMOUNTING use %d: vp %p, cp %p\n", vrefcnt(vp), vp, cp);
77538625Srvb	if (cp->c_ovp != NULL)
77639085Srvb	    printf("coda_inactive: cp->ovp != NULL use %d: vp %p, cp %p\n",
777103937Sjeff	    	   vrefcnt(vp), vp, cp);
77838625Srvb#endif
77938625Srvb    } else {
78039650Srvb#ifdef OLD_DIAGNOSTIC
781103937Sjeff	if (vrefcnt(CTOV(cp))) {
78239085Srvb	    panic("coda_inactive: nonzero reference count");
78338625Srvb	}
78438625Srvb	if (cp->c_ovp != NULL) {
78539085Srvb	    panic("coda_inactive:  cp->ovp != NULL");
78638625Srvb	}
78738625Srvb#endif
78838625Srvb	vgone(vp);
78938625Srvb    }
79038625Srvb
79139085Srvb    MARK_INT_SAT(CODA_INACTIVE_STATS);
79238625Srvb    return(0);
79338625Srvb}
79438625Srvb
79538625Srvb/*
79696755Strhodes * Remote filesystem operations having to do with directory manipulation.
79738625Srvb */
79838625Srvb
79938625Srvb/*
80038625Srvb * It appears that in NetBSD, lookup is supposed to return the vnode locked
80138625Srvb */
80238625Srvbint
803138290Sphkcoda_lookup(struct vop_lookup_args *ap)
80438625Srvb{
80538625Srvb/* true args */
80638625Srvb    struct vnode *dvp = ap->a_dvp;
80738625Srvb    struct cnode *dcp = VTOC(dvp);
80838625Srvb    struct vnode **vpp = ap->a_vpp;
80938625Srvb    /*
81038625Srvb     * It looks as though ap->a_cnp->ni_cnd->cn_nameptr holds the rest
81138625Srvb     * of the string to xlate, and that we must try to get at least
81238625Srvb     * ap->a_cnp->ni_cnd->cn_namelen of those characters to macth.  I
81338625Srvb     * could be wrong.
81438625Srvb     */
81538625Srvb    struct componentname  *cnp = ap->a_cnp;
81638625Srvb    struct ucred *cred = cnp->cn_cred;
81783366Sjulian    struct thread *td = cnp->cn_thread;
81838625Srvb/* locals */
81938625Srvb    struct cnode *cp;
82038625Srvb    const char *nm = cnp->cn_nameptr;
82138625Srvb    int len = cnp->cn_namelen;
822119832Stjr    CodaFid VFid;
82338625Srvb    int	vtype;
82438625Srvb    int error = 0;
82538625Srvb
82639085Srvb    MARK_ENTRY(CODA_LOOKUP_STATS);
82738625Srvb
828119832Stjr    CODADEBUG(CODA_LOOKUP, myprintf(("lookup: %s in %s\n",
829119832Stjr				   nm, coda_f2s(&dcp->c_fid))););
83038625Srvb
83138625Srvb    /* Check for lookup of control object. */
83238625Srvb    if (IS_CTL_NAME(dvp, nm, len)) {
83339085Srvb	*vpp = coda_ctlvp;
83438625Srvb	vref(*vpp);
83539085Srvb	MARK_INT_SAT(CODA_LOOKUP_STATS);
83638625Srvb	goto exit;
83738625Srvb    }
83838625Srvb
83939085Srvb    if (len+1 > CODA_MAXNAMLEN) {
84039085Srvb	MARK_INT_FAIL(CODA_LOOKUP_STATS);
841119832Stjr
842119832Stjr	CODADEBUG(CODA_LOOKUP, myprintf(("name too long: lookup, %s (%s)\n",
843119832Stjr					 coda_f2s(&dcp->c_fid), nm)););
84438625Srvb	*vpp = (struct vnode *)0;
84538625Srvb	error = EINVAL;
84638625Srvb	goto exit;
84738625Srvb    }
84838625Srvb    /* First try to look the file up in the cfs name cache */
84938625Srvb    /* lock the parent vnode? */
85039085Srvb    cp = coda_nc_lookup(dcp, nm, len, cred);
85138625Srvb    if (cp) {
85238625Srvb	*vpp = CTOV(cp);
85338625Srvb	vref(*vpp);
85439085Srvb	CODADEBUG(CODA_LOOKUP,
85538625Srvb		 myprintf(("lookup result %d vpp %p\n",error,*vpp));)
85638625Srvb    } else {
85738625Srvb
85838625Srvb	/* The name wasn't cached, so we need to contact Venus */
85983366Sjulian	error = venus_lookup(vtomi(dvp), &dcp->c_fid, nm, len, cred, td->td_proc, &VFid, &vtype);
86038625Srvb
86138625Srvb	if (error) {
86239085Srvb	    MARK_INT_FAIL(CODA_LOOKUP_STATS);
863119832Stjr
864119832Stjr	    CODADEBUG(CODA_LOOKUP, myprintf(("lookup error on %s (%s)%d\n",
865119832Stjr					     coda_f2s(&dcp->c_fid), nm, error));)
86638625Srvb	    *vpp = (struct vnode *)0;
86738625Srvb	} else {
86839085Srvb	    MARK_INT_SAT(CODA_LOOKUP_STATS);
86939085Srvb	    CODADEBUG(CODA_LOOKUP,
870119832Stjr		     myprintf(("lookup: %s type %o result %d\n",
871119832Stjr			       coda_f2s(&VFid), vtype, error)); )
87239085Srvb	    cp = make_coda_node(&VFid, dvp->v_mount, vtype);
87338625Srvb	    *vpp = CTOV(cp);
87438625Srvb
87538625Srvb	    /* enter the new vnode in the Name Cache only if the top bit isn't set */
87638625Srvb	    /* And don't enter a new vnode for an invalid one! */
87739085Srvb	    if (!(vtype & CODA_NOCACHE))
87839085Srvb		coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
87938625Srvb	}
88038625Srvb    }
88138625Srvb
88238625Srvb exit:
88338625Srvb    /*
88438625Srvb     * If we are creating, and this was the last name to be looked up,
88538625Srvb     * and the error was ENOENT, then there really shouldn't be an
88638625Srvb     * error and we can make the leaf NULL and return success.  Since
88738625Srvb     * this is supposed to work under Mach as well as NetBSD, we're
88838625Srvb     * leaving this fn wrapped.  We also must tell lookup/namei that
88938625Srvb     * we need to save the last component of the name.  (Create will
89038625Srvb     * have to free the name buffer later...lucky us...)
89138625Srvb     */
89238625Srvb    if (((cnp->cn_nameiop == CREATE) || (cnp->cn_nameiop == RENAME))
89338625Srvb	&& (cnp->cn_flags & ISLASTCN)
89438625Srvb	&& (error == ENOENT))
89538625Srvb    {
89638625Srvb	error = EJUSTRETURN;
89738625Srvb	cnp->cn_flags |= SAVENAME;
89838625Srvb	*ap->a_vpp = NULL;
89938625Srvb    }
90038625Srvb
90138625Srvb    /*
90238625Srvb     * If we are removing, and we are at the last element, and we
90338625Srvb     * found it, then we need to keep the name around so that the
90438625Srvb     * removal will go ahead as planned.  Unfortunately, this will
90538625Srvb     * probably also lock the to-be-removed vnode, which may or may
90638625Srvb     * not be a good idea.  I'll have to look at the bits of
90739085Srvb     * coda_remove to make sure.  We'll only save the name if we did in
90839085Srvb     * fact find the name, otherwise coda_remove won't have a chance
90938625Srvb     * to free the pathname.
91038625Srvb     */
91138625Srvb    if ((cnp->cn_nameiop == DELETE)
91238625Srvb	&& (cnp->cn_flags & ISLASTCN)
91338625Srvb	&& !error)
91438625Srvb    {
91538625Srvb	cnp->cn_flags |= SAVENAME;
91638625Srvb    }
91738625Srvb
91838625Srvb    /*
91938625Srvb     * If the lookup went well, we need to (potentially?) unlock the
92038625Srvb     * parent, and lock the child.  We are only responsible for
92138625Srvb     * checking to see if the parent is supposed to be unlocked before
92238625Srvb     * we return.  We must always lock the child (provided there is
92338625Srvb     * one, and (the parent isn't locked or it isn't the same as the
92438625Srvb     * parent.)  Simple, huh?  We can never leave the parent locked unless
92538625Srvb     * we are ISLASTCN
92638625Srvb     */
92738625Srvb    if (!error || (error == EJUSTRETURN)) {
928144227Sjeff	if (cnp->cn_flags & ISDOTDOT) {
929175294Sattilio	    if ((error = VOP_UNLOCK(dvp, 0))) {
93038625Srvb		return error;
93138625Srvb	    }
93238625Srvb	    /*
93338625Srvb	     * The parent is unlocked.  As long as there is a child,
93438625Srvb	     * lock it without bothering to check anything else.
93538625Srvb	     */
93638625Srvb	    if (*ap->a_vpp) {
937175294Sattilio		if ((error = VOP_LOCK(*ap->a_vpp, LK_EXCLUSIVE))) {
938175202Sattilio		    vn_lock(dvp, LK_RETRY|LK_EXCLUSIVE);
939144227Sjeff		    return (error);
94038625Srvb		}
94138625Srvb	    }
942175202Sattilio	    vn_lock(dvp, LK_RETRY|LK_EXCLUSIVE);
94338625Srvb	} else {
94438625Srvb	    /* The parent is locked, and may be the same as the child */
94538625Srvb	    if (*ap->a_vpp && (*ap->a_vpp != dvp)) {
94638625Srvb		/* Different, go ahead and lock it. */
947175294Sattilio		if ((error = VOP_LOCK(*ap->a_vpp, LK_EXCLUSIVE))) {
948144227Sjeff		    return (error);
94938625Srvb		}
95038625Srvb	    }
95138625Srvb	}
95238625Srvb    } else {
95338625Srvb	/* If the lookup failed, we need to ensure that the leaf is NULL */
95438625Srvb	/* Don't change any locking? */
95538625Srvb	*ap->a_vpp = NULL;
95638625Srvb    }
95738625Srvb    return(error);
95838625Srvb}
95938625Srvb
96038625Srvb/*ARGSUSED*/
96138625Srvbint
962138290Sphkcoda_create(struct vop_create_args *ap)
96338625Srvb{
96438625Srvb/* true args */
96538625Srvb    struct vnode *dvp = ap->a_dvp;
96638625Srvb    struct cnode *dcp = VTOC(dvp);
96738625Srvb    struct vattr *va = ap->a_vap;
96838625Srvb    int exclusive = 1;
96938625Srvb    int mode = ap->a_vap->va_mode;
97038625Srvb    struct vnode **vpp = ap->a_vpp;
97138625Srvb    struct componentname  *cnp = ap->a_cnp;
97238625Srvb    struct ucred *cred = cnp->cn_cred;
97383366Sjulian    struct thread *td = cnp->cn_thread;
97438625Srvb/* locals */
97538625Srvb    int error;
97638625Srvb    struct cnode *cp;
97738625Srvb    const char *nm = cnp->cn_nameptr;
97838625Srvb    int len = cnp->cn_namelen;
979119832Stjr    CodaFid VFid;
98038625Srvb    struct vattr attr;
98138625Srvb
98239085Srvb    MARK_ENTRY(CODA_CREATE_STATS);
98338625Srvb
98438625Srvb    /* All creates are exclusive XXX */
98538625Srvb    /* I'm assuming the 'mode' argument is the file mode bits XXX */
98638625Srvb
98738625Srvb    /* Check for create of control object. */
98838625Srvb    if (IS_CTL_NAME(dvp, nm, len)) {
98938625Srvb	*vpp = (struct vnode *)0;
99039085Srvb	MARK_INT_FAIL(CODA_CREATE_STATS);
99138625Srvb	return(EACCES);
99238625Srvb    }
99338625Srvb
99483366Sjulian    error = venus_create(vtomi(dvp), &dcp->c_fid, nm, len, exclusive, mode, va, cred, td->td_proc, &VFid, &attr);
99538625Srvb
99638625Srvb    if (!error) {
99738625Srvb
99838625Srvb	/* If this is an exclusive create, panic if the file already exists. */
99938625Srvb	/* Venus should have detected the file and reported EEXIST. */
100038625Srvb
100138625Srvb	if ((exclusive == 1) &&
100239085Srvb	    (coda_find(&VFid) != NULL))
100338625Srvb	    panic("cnode existed for newly created file!");
100438625Srvb
100539085Srvb	cp = make_coda_node(&VFid, dvp->v_mount, attr.va_type);
100638625Srvb	*vpp = CTOV(cp);
100738625Srvb
100838625Srvb	/* Update va to reflect the new attributes. */
100938625Srvb	(*va) = attr;
101038625Srvb
101138625Srvb	/* Update the attribute cache and mark it as valid */
101239085Srvb	if (coda_attr_cache) {
101338625Srvb	    VTOC(*vpp)->c_vattr = attr;
101438625Srvb	    VTOC(*vpp)->c_flags |= C_VATTR;
101538625Srvb	}
101638625Srvb
101738625Srvb	/* Invalidate the parent's attr cache, the modification time has changed */
101838625Srvb	VTOC(dvp)->c_flags &= ~C_VATTR;
101938625Srvb
102038625Srvb	/* enter the new vnode in the Name Cache */
102139085Srvb	coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
102238625Srvb
102339085Srvb	CODADEBUG(CODA_CREATE,
1024119832Stjr		  myprintf(("create: %s, result %d\n",
1025119832Stjr			   coda_f2s(&VFid), error)); )
102638625Srvb    } else {
102738625Srvb	*vpp = (struct vnode *)0;
102839085Srvb	CODADEBUG(CODA_CREATE, myprintf(("create error %d\n", error));)
102938625Srvb    }
103038625Srvb
103138625Srvb    if (!error) {
103238625Srvb	if (cnp->cn_flags & LOCKLEAF) {
1033175294Sattilio	    if ((error = VOP_LOCK(*ap->a_vpp, LK_EXCLUSIVE))) {
103439085Srvb		printf("coda_create: ");
103538625Srvb		panic("unlocked parent but couldn't lock child");
103638625Srvb	    }
103738625Srvb	}
103839650Srvb#ifdef OLD_DIAGNOSTIC
103938625Srvb	else {
104039085Srvb	    printf("coda_create: LOCKLEAF not set!\n");
104138625Srvb	}
104239650Srvb#endif
104338625Srvb    }
104438625Srvb    return(error);
104538625Srvb}
104638625Srvb
104738625Srvbint
1048138290Sphkcoda_remove(struct vop_remove_args *ap)
104938625Srvb{
105038625Srvb/* true args */
105138625Srvb    struct vnode *dvp = ap->a_dvp;
105238625Srvb    struct cnode *cp = VTOC(dvp);
105338625Srvb    struct componentname  *cnp = ap->a_cnp;
105438625Srvb    struct ucred *cred = cnp->cn_cred;
105583366Sjulian    struct thread *td = cnp->cn_thread;
105638625Srvb/* locals */
105738625Srvb    int error;
105838625Srvb    const char *nm = cnp->cn_nameptr;
105938625Srvb    int len = cnp->cn_namelen;
106038625Srvb    struct cnode *tp;
106138625Srvb
106239085Srvb    MARK_ENTRY(CODA_REMOVE_STATS);
106338625Srvb
1064119832Stjr    CODADEBUG(CODA_REMOVE, myprintf(("remove: %s in %s\n",
1065119832Stjr				     nm, coda_f2s(&cp->c_fid))););
106639085Srvb    /* Remove the file's entry from the CODA Name Cache */
106738625Srvb    /* We're being conservative here, it might be that this person
106838625Srvb     * doesn't really have sufficient access to delete the file
106938625Srvb     * but we feel zapping the entry won't really hurt anyone -- dcs
107038625Srvb     */
107138625Srvb    /* I'm gonna go out on a limb here. If a file and a hardlink to it
107238625Srvb     * exist, and one is removed, the link count on the other will be
107338625Srvb     * off by 1. We could either invalidate the attrs if cached, or
107438625Srvb     * fix them. I'll try to fix them. DCS 11/8/94
107538625Srvb     */
107639085Srvb    tp = coda_nc_lookup(VTOC(dvp), nm, len, cred);
107738625Srvb    if (tp) {
107838625Srvb	if (VALID_VATTR(tp)) {	/* If attrs are cached */
107938625Srvb	    if (tp->c_vattr.va_nlink > 1) {	/* If it's a hard link */
108038625Srvb		tp->c_vattr.va_nlink--;
108138625Srvb	    }
108238625Srvb	}
108338625Srvb
108439085Srvb	coda_nc_zapfile(VTOC(dvp), nm, len);
108538625Srvb	/* No need to flush it if it doesn't exist! */
108638625Srvb    }
108738625Srvb    /* Invalidate the parent's attr cache, the modification time has changed */
108838625Srvb    VTOC(dvp)->c_flags &= ~C_VATTR;
108938625Srvb
109038625Srvb    /* Check for remove of control object. */
109138625Srvb    if (IS_CTL_NAME(dvp, nm, len)) {
109239085Srvb	MARK_INT_FAIL(CODA_REMOVE_STATS);
109338625Srvb	return(ENOENT);
109438625Srvb    }
109538625Srvb
109683366Sjulian    error = venus_remove(vtomi(dvp), &cp->c_fid, nm, len, cred, td->td_proc);
109738625Srvb
109839085Srvb    CODADEBUG(CODA_REMOVE, myprintf(("in remove result %d\n",error)); )
109938625Srvb
110038625Srvb    return(error);
110138625Srvb}
110238625Srvb
110338625Srvbint
1104138290Sphkcoda_link(struct vop_link_args *ap)
110538625Srvb{
110638625Srvb/* true args */
110738625Srvb    struct vnode *vp = ap->a_vp;
110838625Srvb    struct cnode *cp = VTOC(vp);
110938625Srvb    struct vnode *tdvp = ap->a_tdvp;
111038625Srvb    struct cnode *tdcp = VTOC(tdvp);
111138625Srvb    struct componentname *cnp = ap->a_cnp;
111238625Srvb    struct ucred *cred = cnp->cn_cred;
111383366Sjulian    struct thread *td = cnp->cn_thread;
111438625Srvb/* locals */
111538625Srvb    int error;
111638625Srvb    const char *nm = cnp->cn_nameptr;
111738625Srvb    int len = cnp->cn_namelen;
111838625Srvb
111939085Srvb    MARK_ENTRY(CODA_LINK_STATS);
112038625Srvb
112139085Srvb    if (codadebug & CODADBGMSK(CODA_LINK)) {
1122119832Stjr	myprintf(("nb_link:   vp fid: %s\n",
1123119832Stjr		  coda_f2s(&cp->c_fid)));
1124119832Stjr	myprintf(("nb_link: tdvp fid: %s)\n",
1125119832Stjr		  coda_f2s(&tdcp->c_fid)));
112638625Srvb    }
112739085Srvb    if (codadebug & CODADBGMSK(CODA_LINK)) {
1128119832Stjr	myprintf(("link:   vp fid: %s\n",
1129119832Stjr		  coda_f2s(&cp->c_fid)));
1130119832Stjr	myprintf(("link: tdvp fid: %s\n",
1131119832Stjr		  coda_f2s(&tdcp->c_fid)));
113238625Srvb    }
113338625Srvb
113438625Srvb    /* Check for link to/from control object. */
113538625Srvb    if (IS_CTL_NAME(tdvp, nm, len) || IS_CTL_VP(vp)) {
113639085Srvb	MARK_INT_FAIL(CODA_LINK_STATS);
113738625Srvb	return(EACCES);
113838625Srvb    }
113938625Srvb
114083366Sjulian    error = venus_link(vtomi(vp), &cp->c_fid, &tdcp->c_fid, nm, len, cred, td->td_proc);
114138625Srvb
114238625Srvb    /* Invalidate the parent's attr cache, the modification time has changed */
114338625Srvb    VTOC(tdvp)->c_flags &= ~C_VATTR;
114438625Srvb    VTOC(vp)->c_flags &= ~C_VATTR;
114538625Srvb
114639085Srvb    CODADEBUG(CODA_LINK,	myprintf(("in link result %d\n",error)); )
114738625Srvb
114838625Srvb    return(error);
114938625Srvb}
115038625Srvb
115138625Srvbint
1152138290Sphkcoda_rename(struct vop_rename_args *ap)
115338625Srvb{
115438625Srvb/* true args */
115538625Srvb    struct vnode *odvp = ap->a_fdvp;
115638625Srvb    struct cnode *odcp = VTOC(odvp);
115738625Srvb    struct componentname  *fcnp = ap->a_fcnp;
115838625Srvb    struct vnode *ndvp = ap->a_tdvp;
115938625Srvb    struct cnode *ndcp = VTOC(ndvp);
116038625Srvb    struct componentname  *tcnp = ap->a_tcnp;
116138625Srvb    struct ucred *cred = fcnp->cn_cred;
116283366Sjulian    struct thread *td = fcnp->cn_thread;
116338625Srvb/* true args */
116438625Srvb    int error;
116538625Srvb    const char *fnm = fcnp->cn_nameptr;
116638625Srvb    int flen = fcnp->cn_namelen;
116738625Srvb    const char *tnm = tcnp->cn_nameptr;
116838625Srvb    int tlen = tcnp->cn_namelen;
116938625Srvb
117039085Srvb    MARK_ENTRY(CODA_RENAME_STATS);
117138625Srvb
117238625Srvb    /* Hmmm.  The vnodes are already looked up.  Perhaps they are locked?
117338625Srvb       This could be Bad. XXX */
117439650Srvb#ifdef OLD_DIAGNOSTIC
117538625Srvb    if ((fcnp->cn_cred != tcnp->cn_cred)
117683366Sjulian	|| (fcnp->cn_thread != tcnp->cn_thread))
117738625Srvb    {
117839085Srvb	panic("coda_rename: component names don't agree");
117938625Srvb    }
118039650Srvb#endif
118138625Srvb
118238625Srvb    /* Check for rename involving control object. */
118338625Srvb    if (IS_CTL_NAME(odvp, fnm, flen) || IS_CTL_NAME(ndvp, tnm, tlen)) {
118439085Srvb	MARK_INT_FAIL(CODA_RENAME_STATS);
118538625Srvb	return(EACCES);
118638625Srvb    }
118738625Srvb
118838625Srvb    /* Problem with moving directories -- need to flush entry for .. */
118938625Srvb    if (odvp != ndvp) {
119039085Srvb	struct cnode *ovcp = coda_nc_lookup(VTOC(odvp), fnm, flen, cred);
119138625Srvb	if (ovcp) {
119238625Srvb	    struct vnode *ovp = CTOV(ovcp);
119338625Srvb	    if ((ovp) &&
119438625Srvb		(ovp->v_type == VDIR)) /* If it's a directory */
119539085Srvb		coda_nc_zapfile(VTOC(ovp),"..", 2);
119638625Srvb	}
119738625Srvb    }
119838625Srvb
119938625Srvb    /* Remove the entries for both source and target files */
120039085Srvb    coda_nc_zapfile(VTOC(odvp), fnm, flen);
120139085Srvb    coda_nc_zapfile(VTOC(ndvp), tnm, tlen);
120238625Srvb
120338625Srvb    /* Invalidate the parent's attr cache, the modification time has changed */
120438625Srvb    VTOC(odvp)->c_flags &= ~C_VATTR;
120538625Srvb    VTOC(ndvp)->c_flags &= ~C_VATTR;
120638625Srvb
120739085Srvb    if (flen+1 > CODA_MAXNAMLEN) {
120839085Srvb	MARK_INT_FAIL(CODA_RENAME_STATS);
120938625Srvb	error = EINVAL;
121038625Srvb	goto exit;
121138625Srvb    }
121238625Srvb
121339085Srvb    if (tlen+1 > CODA_MAXNAMLEN) {
121439085Srvb	MARK_INT_FAIL(CODA_RENAME_STATS);
121538625Srvb	error = EINVAL;
121638625Srvb	goto exit;
121738625Srvb    }
121838625Srvb
121983366Sjulian    error = venus_rename(vtomi(odvp), &odcp->c_fid, &ndcp->c_fid, fnm, flen, tnm, tlen, cred, td->td_proc);
122038625Srvb
122138625Srvb exit:
122239085Srvb    CODADEBUG(CODA_RENAME, myprintf(("in rename result %d\n",error));)
122338625Srvb    /* XXX - do we need to call cache pureg on the moved vnode? */
122438625Srvb    cache_purge(ap->a_fvp);
122538625Srvb
1226155160Sjeff    /* Release parents first, then children. */
122738625Srvb    vrele(odvp);
122838625Srvb    if (ap->a_tvp) {
1229155160Sjeff	if (ap->a_tvp == ndvp)
1230155160Sjeff	    vrele(ndvp);
1231155160Sjeff	else
1232155160Sjeff	    vput(ndvp);
1233155160Sjeff	vput(ap->a_tvp);
1234155160Sjeff    } else
1235155160Sjeff	vput(ndvp);
1236155160Sjeff    vrele(ap->a_fvp);
123738625Srvb
123838625Srvb    return(error);
123938625Srvb}
124038625Srvb
124138625Srvbint
1242138290Sphkcoda_mkdir(struct vop_mkdir_args *ap)
124338625Srvb{
124438625Srvb/* true args */
124538625Srvb    struct vnode *dvp = ap->a_dvp;
124638625Srvb    struct cnode *dcp = VTOC(dvp);
124738625Srvb    struct componentname  *cnp = ap->a_cnp;
124838625Srvb    register struct vattr *va = ap->a_vap;
124938625Srvb    struct vnode **vpp = ap->a_vpp;
125038625Srvb    struct ucred *cred = cnp->cn_cred;
125183366Sjulian    struct thread *td = cnp->cn_thread;
125238625Srvb/* locals */
125338625Srvb    int error;
125438625Srvb    const char *nm = cnp->cn_nameptr;
125538625Srvb    int len = cnp->cn_namelen;
125638625Srvb    struct cnode *cp;
1257119832Stjr    CodaFid VFid;
125838625Srvb    struct vattr ova;
125938625Srvb
126039085Srvb    MARK_ENTRY(CODA_MKDIR_STATS);
126138625Srvb
126238625Srvb    /* Check for mkdir of target object. */
126338625Srvb    if (IS_CTL_NAME(dvp, nm, len)) {
126438625Srvb	*vpp = (struct vnode *)0;
126539085Srvb	MARK_INT_FAIL(CODA_MKDIR_STATS);
126638625Srvb	return(EACCES);
126738625Srvb    }
126838625Srvb
126939085Srvb    if (len+1 > CODA_MAXNAMLEN) {
127038625Srvb	*vpp = (struct vnode *)0;
127139085Srvb	MARK_INT_FAIL(CODA_MKDIR_STATS);
127238625Srvb	return(EACCES);
127338625Srvb    }
127438625Srvb
127583366Sjulian    error = venus_mkdir(vtomi(dvp), &dcp->c_fid, nm, len, va, cred, td->td_proc, &VFid, &ova);
127638625Srvb
127738625Srvb    if (!error) {
127839085Srvb	if (coda_find(&VFid) != NULL)
127938625Srvb	    panic("cnode existed for newly created directory!");
128038625Srvb
128138625Srvb
128239085Srvb	cp =  make_coda_node(&VFid, dvp->v_mount, va->va_type);
128338625Srvb	*vpp = CTOV(cp);
128438625Srvb
128538625Srvb	/* enter the new vnode in the Name Cache */
128639085Srvb	coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
128738625Srvb
128838625Srvb	/* as a side effect, enter "." and ".." for the directory */
128939085Srvb	coda_nc_enter(VTOC(*vpp), ".", 1, cred, VTOC(*vpp));
129039085Srvb	coda_nc_enter(VTOC(*vpp), "..", 2, cred, VTOC(dvp));
129138625Srvb
129239085Srvb	if (coda_attr_cache) {
129338625Srvb	    VTOC(*vpp)->c_vattr = ova;		/* update the attr cache */
129438625Srvb	    VTOC(*vpp)->c_flags |= C_VATTR;	/* Valid attributes in cnode */
129538625Srvb	}
129638625Srvb
129738625Srvb	/* Invalidate the parent's attr cache, the modification time has changed */
129838625Srvb	VTOC(dvp)->c_flags &= ~C_VATTR;
129938625Srvb
1300119832Stjr	CODADEBUG( CODA_MKDIR, myprintf(("mkdir: %s result %d\n",
1301119832Stjr					 coda_f2s(&VFid), error)); )
1302119832Stjr	} else {
130338625Srvb	*vpp = (struct vnode *)0;
130439085Srvb	CODADEBUG(CODA_MKDIR, myprintf(("mkdir error %d\n",error));)
130538625Srvb    }
130638625Srvb
130738625Srvb    return(error);
130838625Srvb}
130938625Srvb
131038625Srvbint
1311138290Sphkcoda_rmdir(struct vop_rmdir_args *ap)
131238625Srvb{
131338625Srvb/* true args */
131438625Srvb    struct vnode *dvp = ap->a_dvp;
131538625Srvb    struct cnode *dcp = VTOC(dvp);
131638625Srvb    struct componentname  *cnp = ap->a_cnp;
131738625Srvb    struct ucred *cred = cnp->cn_cred;
131883366Sjulian    struct thread *td = cnp->cn_thread;
131938625Srvb/* true args */
132038625Srvb    int error;
132138625Srvb    const char *nm = cnp->cn_nameptr;
132238625Srvb    int len = cnp->cn_namelen;
132338625Srvb    struct cnode *cp;
132438625Srvb
132539085Srvb    MARK_ENTRY(CODA_RMDIR_STATS);
132638625Srvb
132738625Srvb    /* Check for rmdir of control object. */
132838625Srvb    if (IS_CTL_NAME(dvp, nm, len)) {
132939085Srvb	MARK_INT_FAIL(CODA_RMDIR_STATS);
133038625Srvb	return(ENOENT);
133138625Srvb    }
133238625Srvb
133338625Srvb    /* We're being conservative here, it might be that this person
133438625Srvb     * doesn't really have sufficient access to delete the file
133538625Srvb     * but we feel zapping the entry won't really hurt anyone -- dcs
133638625Srvb     */
133738625Srvb    /*
133838625Srvb     * As a side effect of the rmdir, remove any entries for children of
133938625Srvb     * the directory, especially "." and "..".
134038625Srvb     */
134139085Srvb    cp = coda_nc_lookup(dcp, nm, len, cred);
134239085Srvb    if (cp) coda_nc_zapParentfid(&(cp->c_fid), NOT_DOWNCALL);
134338625Srvb
134439085Srvb    /* Remove the file's entry from the CODA Name Cache */
134539085Srvb    coda_nc_zapfile(dcp, nm, len);
134638625Srvb
134738625Srvb    /* Invalidate the parent's attr cache, the modification time has changed */
134838625Srvb    dcp->c_flags &= ~C_VATTR;
134938625Srvb
135083366Sjulian    error = venus_rmdir(vtomi(dvp), &dcp->c_fid, nm, len, cred, td->td_proc);
135138625Srvb
135239085Srvb    CODADEBUG(CODA_RMDIR, myprintf(("in rmdir result %d\n", error)); )
135338625Srvb
135438625Srvb    return(error);
135538625Srvb}
135638625Srvb
135738625Srvbint
1358138290Sphkcoda_symlink(struct vop_symlink_args *ap)
135938625Srvb{
136038625Srvb/* true args */
136138625Srvb    struct vnode *tdvp = ap->a_dvp;
136238625Srvb    struct cnode *tdcp = VTOC(tdvp);
136338625Srvb    struct componentname *cnp = ap->a_cnp;
136438625Srvb    struct vattr *tva = ap->a_vap;
136538625Srvb    char *path = ap->a_target;
136638625Srvb    struct ucred *cred = cnp->cn_cred;
136783366Sjulian    struct thread *td = cnp->cn_thread;
136853131Seivind    struct vnode **vpp = ap->a_vpp;
136938625Srvb/* locals */
137038625Srvb    int error;
137138625Srvb    /*
137239085Srvb     * XXX I'm assuming the following things about coda_symlink's
137338625Srvb     * arguments:
137438625Srvb     *       t(foo) is the new name/parent/etc being created.
137538625Srvb     *       lname is the contents of the new symlink.
137638625Srvb     */
137738759Srvb    char *nm = cnp->cn_nameptr;
137838625Srvb    int len = cnp->cn_namelen;
137938625Srvb    int plen = strlen(path);
138038625Srvb
138138625Srvb    /*
138238625Srvb     * Here's the strategy for the moment: perform the symlink, then
138338625Srvb     * do a lookup to grab the resulting vnode.  I know this requires
138438625Srvb     * two communications with Venus for a new sybolic link, but
138538625Srvb     * that's the way the ball bounces.  I don't yet want to change
138638625Srvb     * the way the Mach symlink works.  When Mach support is
138738625Srvb     * deprecated, we should change symlink so that the common case
138838625Srvb     * returns the resultant vnode in a vpp argument.
138938625Srvb     */
139038625Srvb
139139085Srvb    MARK_ENTRY(CODA_SYMLINK_STATS);
139238625Srvb
139338625Srvb    /* Check for symlink of control object. */
139438625Srvb    if (IS_CTL_NAME(tdvp, nm, len)) {
139539085Srvb	MARK_INT_FAIL(CODA_SYMLINK_STATS);
139638625Srvb	return(EACCES);
139738625Srvb    }
139838625Srvb
139939085Srvb    if (plen+1 > CODA_MAXPATHLEN) {
140039085Srvb	MARK_INT_FAIL(CODA_SYMLINK_STATS);
140138625Srvb	return(EINVAL);
140238625Srvb    }
140338625Srvb
140439085Srvb    if (len+1 > CODA_MAXNAMLEN) {
140539085Srvb	MARK_INT_FAIL(CODA_SYMLINK_STATS);
140638625Srvb	error = EINVAL;
140738625Srvb	goto exit;
140838625Srvb    }
140938625Srvb
141083366Sjulian    error = venus_symlink(vtomi(tdvp), &tdcp->c_fid, path, plen, nm, len, tva, cred, td->td_proc);
141138625Srvb
141238625Srvb    /* Invalidate the parent's attr cache, the modification time has changed */
141338625Srvb    tdcp->c_flags &= ~C_VATTR;
141438625Srvb
141553131Seivind    if (error == 0)
141653131Seivind	error = VOP_LOOKUP(tdvp, vpp, cnp);
141738625Srvb
141838625Srvb exit:
141939085Srvb    CODADEBUG(CODA_SYMLINK, myprintf(("in symlink result %d\n",error)); )
142038625Srvb    return(error);
142138625Srvb}
142238625Srvb
142338625Srvb/*
142438625Srvb * Read directory entries.
142538625Srvb */
142638625Srvbint
1427138290Sphkcoda_readdir(struct vop_readdir_args *ap)
142838625Srvb{
142938625Srvb/* true args */
143038625Srvb    struct vnode *vp = ap->a_vp;
143138625Srvb    struct cnode *cp = VTOC(vp);
143238625Srvb    register struct uio *uiop = ap->a_uio;
143338625Srvb    struct ucred *cred = ap->a_cred;
143438625Srvb    int *eofflag = ap->a_eofflag;
143538625Srvb    u_long **cookies = ap->a_cookies;
143638625Srvb    int *ncookies = ap->a_ncookies;
143783366Sjulian    struct thread *td = ap->a_uio->uio_td;
143838625Srvb/* upcall decl */
143938625Srvb/* locals */
144038625Srvb    int error = 0;
144138625Srvb
144239085Srvb    MARK_ENTRY(CODA_READDIR_STATS);
144338625Srvb
144449524Sbde    CODADEBUG(CODA_READDIR, myprintf(("coda_readdir(%p, %d, %lld, %d)\n",
144549524Sbde				      (void *)uiop->uio_iov->iov_base,
144649524Sbde				      uiop->uio_resid,
144749524Sbde				      (long long)uiop->uio_offset,
144849524Sbde				      uiop->uio_segflg)); )
144938625Srvb
145038625Srvb    /* Check for readdir of control object. */
145138625Srvb    if (IS_CTL_VP(vp)) {
145239085Srvb	MARK_INT_FAIL(CODA_READDIR_STATS);
145338625Srvb	return(ENOENT);
145438625Srvb    }
145538625Srvb
145638759Srvb    {
145738625Srvb	/* If directory is not already open do an "internal open" on it. */
145838625Srvb	int opened_internally = 0;
145938625Srvb	if (cp->c_ovp == NULL) {
146038625Srvb	    opened_internally = 1;
146139085Srvb	    MARK_INT_GEN(CODA_OPEN_STATS);
1462170152Skib	    error = VOP_OPEN(vp, FREAD, cred, td, NULL);
1463171377Srwatson	    printf("coda_readdir: Internally Opening %p\n", vp);
146438625Srvb	    if (error) {
146539085Srvb		printf("coda_readdir: VOP_OPEN on container failed %d\n", error);
146638625Srvb		return (error);
146738625Srvb	    }
146838625Srvb	}
146938625Srvb
147038625Srvb	/* Have UFS handle the call. */
1471119832Stjr	CODADEBUG(CODA_READDIR, myprintf(("indirect readdir: fid = %s, refcnt = %d\n", coda_f2s(&cp->c_fid), vp->v_usecount)); )
147238625Srvb	error = VOP_READDIR(cp->c_ovp, uiop, cred, eofflag, ncookies,
147338625Srvb			       cookies);
147438625Srvb
147538625Srvb	if (error)
147639085Srvb	    MARK_INT_FAIL(CODA_READDIR_STATS);
147738625Srvb	else
147839085Srvb	    MARK_INT_SAT(CODA_READDIR_STATS);
147938625Srvb
148038625Srvb	/* Do an "internal close" if necessary. */
148138625Srvb	if (opened_internally) {
148239085Srvb	    MARK_INT_GEN(CODA_CLOSE_STATS);
148383366Sjulian	    (void)VOP_CLOSE(vp, FREAD, cred, td);
148438625Srvb	}
148538625Srvb    }
148638625Srvb
148738625Srvb    return(error);
148838625Srvb}
148938625Srvb
149038625Srvb/*
149196755Strhodes * Convert from filesystem blocks to device blocks
149238625Srvb */
149338625Srvbint
1494138290Sphkcoda_bmap(struct vop_bmap_args *ap)
149538625Srvb{
149638625Srvb    /* XXX on the global proc */
149738625Srvb/* true args */
149838625Srvb    struct vnode *vp __attribute__((unused)) = ap->a_vp;	/* file's vnode */
149938625Srvb    daddr_t bn __attribute__((unused)) = ap->a_bn;	/* fs block number */
1500137726Sphk    struct bufobj **bop = ap->a_bop;			/* RETURN bufobj of device */
150196572Sphk    daddr_t *bnp __attribute__((unused)) = ap->a_bnp;	/* RETURN device block number */
150283366Sjulian    struct thread *td __attribute__((unused)) = curthread;
150338625Srvb/* upcall decl */
150438625Srvb/* locals */
150538625Srvb
150638625Srvb	int ret = 0;
150738625Srvb	struct cnode *cp;
150838625Srvb
150938625Srvb	cp = VTOC(vp);
151038625Srvb	if (cp->c_ovp) {
151139728Srvb		return EINVAL;
1512137726Sphk		ret =  VOP_BMAP(cp->c_ovp, bn, bop, bnp, ap->a_runp, ap->a_runb);
151339650Srvb#if	0
1514137726Sphk		printf("VOP_BMAP(cp->c_ovp %p, bn %p, bop %p, bnp %lld, ap->a_runp %p, ap->a_runb %p) = %d\n",
1515137726Sphk			cp->c_ovp, bn, bop, bnp, ap->a_runp, ap->a_runb, ret);
151639650Srvb#endif
151738625Srvb		return ret;
151838625Srvb	} else {
151941504Srvb#if	0
152039085Srvb		printf("coda_bmap: no container\n");
152141504Srvb#endif
152238625Srvb		return(EOPNOTSUPP);
152338625Srvb	}
152438625Srvb}
152538625Srvb
152638625Srvbint
1527138290Sphkcoda_reclaim(struct vop_reclaim_args *ap)
152838625Srvb{
152938625Srvb/* true args */
153038625Srvb    struct vnode *vp = ap->a_vp;
153138625Srvb    struct cnode *cp = VTOC(vp);
153238625Srvb/* upcall decl */
153338625Srvb/* locals */
153438625Srvb
153538625Srvb/*
153638625Srvb * Forced unmount/flush will let vnodes with non zero use be destroyed!
153738625Srvb */
153838625Srvb    ENTRY;
153938625Srvb
154038625Srvb    if (IS_UNMOUNTING(cp)) {
154138625Srvb#ifdef	DEBUG
154238625Srvb	if (VTOC(vp)->c_ovp) {
154338625Srvb	    if (IS_UNMOUNTING(cp))
154439085Srvb		printf("coda_reclaim: c_ovp not void: vp %p, cp %p\n", vp, cp);
154538625Srvb	}
154638625Srvb#endif
154738625Srvb    } else {
154839650Srvb#ifdef OLD_DIAGNOSTIC
1549103937Sjeff	if (vrefcnt(vp) != 0)
155039650Srvb	    print("coda_reclaim: pushing active %p\n", vp);
155138625Srvb	if (VTOC(vp)->c_ovp) {
155239085Srvb	    panic("coda_reclaim: c_ovp not void");
155338625Srvb    }
155439650Srvb#endif
155538625Srvb    }
155638625Srvb    cache_purge(vp);
155739085Srvb    coda_free(VTOC(vp));
1558132765Skan    vp->v_data = NULL;
1559175473Srwatson    vp->v_object = NULL;
156038625Srvb    return (0);
156138625Srvb}
156238625Srvb
156338625Srvbint
1564169671Skibcoda_lock(struct vop_lock1_args *ap)
156538625Srvb{
156638625Srvb/* true args */
156738625Srvb    struct vnode *vp = ap->a_vp;
156838625Srvb    struct cnode *cp = VTOC(vp);
156938625Srvb/* upcall decl */
157038625Srvb/* locals */
157138625Srvb
157238625Srvb    ENTRY;
157338625Srvb
1574143507Sjeff    if ((ap->a_flags & LK_INTERLOCK) == 0) {
1575143507Sjeff	VI_LOCK(vp);
1576143507Sjeff	ap->a_flags |= LK_INTERLOCK;
1577143507Sjeff    }
1578143507Sjeff
157939085Srvb    if (coda_lockdebug) {
1580119832Stjr	myprintf(("Attempting lock on %s\n",
1581119832Stjr		  coda_f2s(&cp->c_fid)));
158238625Srvb    }
158338625Srvb
1584143507Sjeff    return (vop_stdlock(ap));
158538625Srvb}
158638625Srvb
158738625Srvbint
1588138290Sphkcoda_unlock(struct vop_unlock_args *ap)
158938625Srvb{
159038625Srvb/* true args */
159138625Srvb    struct vnode *vp = ap->a_vp;
159238625Srvb    struct cnode *cp = VTOC(vp);
159338625Srvb/* upcall decl */
159438625Srvb/* locals */
159538625Srvb
159638625Srvb    ENTRY;
159739085Srvb    if (coda_lockdebug) {
1598119832Stjr	myprintf(("Attempting unlock on %s\n",
1599119832Stjr		  coda_f2s(&cp->c_fid)));
160038625Srvb    }
160138625Srvb
1602143507Sjeff    return (vop_stdunlock(ap));
160338625Srvb}
160438625Srvb
160538625Srvbint
1606138290Sphkcoda_islocked(struct vop_islocked_args *ap)
160738625Srvb{
160838625Srvb/* true args */
160938625Srvb    ENTRY;
161038625Srvb
1611143507Sjeff    return (vop_stdislocked(ap));
161238625Srvb}
161338625Srvb
161438625Srvbvoid
1615154647Srwatsonprint_vattr(struct vattr *attr)
161638625Srvb{
161738625Srvb    char *typestr;
161838625Srvb
161938625Srvb    switch (attr->va_type) {
162038625Srvb    case VNON:
162138625Srvb	typestr = "VNON";
162238625Srvb	break;
162338625Srvb    case VREG:
162438625Srvb	typestr = "VREG";
162538625Srvb	break;
162638625Srvb    case VDIR:
162738625Srvb	typestr = "VDIR";
162838625Srvb	break;
162938625Srvb    case VBLK:
163038625Srvb	typestr = "VBLK";
163138625Srvb	break;
163238625Srvb    case VCHR:
163338625Srvb	typestr = "VCHR";
163438625Srvb	break;
163538625Srvb    case VLNK:
163638625Srvb	typestr = "VLNK";
163738625Srvb	break;
163838625Srvb    case VSOCK:
163938625Srvb	typestr = "VSCK";
164038625Srvb	break;
164138625Srvb    case VFIFO:
164238625Srvb	typestr = "VFFO";
164338625Srvb	break;
164438625Srvb    case VBAD:
164538625Srvb	typestr = "VBAD";
164638625Srvb	break;
164738625Srvb    default:
164838625Srvb	typestr = "????";
164938625Srvb	break;
165038625Srvb    }
165138625Srvb
165238625Srvb
165338625Srvb    myprintf(("attr: type %s mode %d uid %d gid %d fsid %d rdev %d\n",
165438625Srvb	      typestr, (int)attr->va_mode, (int)attr->va_uid,
165538625Srvb	      (int)attr->va_gid, (int)attr->va_fsid, (int)attr->va_rdev));
165638625Srvb
165738625Srvb    myprintf(("      fileid %d nlink %d size %d blocksize %d bytes %d\n",
165838625Srvb	      (int)attr->va_fileid, (int)attr->va_nlink,
165938625Srvb	      (int)attr->va_size,
166038625Srvb	      (int)attr->va_blocksize,(int)attr->va_bytes));
166138625Srvb    myprintf(("      gen %ld flags %ld vaflags %d\n",
166238625Srvb	      attr->va_gen, attr->va_flags, attr->va_vaflags));
166338625Srvb    myprintf(("      atime sec %d nsec %d\n",
166438625Srvb	      (int)attr->va_atime.tv_sec, (int)attr->va_atime.tv_nsec));
166538625Srvb    myprintf(("      mtime sec %d nsec %d\n",
166638625Srvb	      (int)attr->va_mtime.tv_sec, (int)attr->va_mtime.tv_nsec));
166738625Srvb    myprintf(("      ctime sec %d nsec %d\n",
166838625Srvb	      (int)attr->va_ctime.tv_sec, (int)attr->va_ctime.tv_nsec));
166938625Srvb}
167038625Srvb
167138625Srvb/* How to print a ucred */
167238625Srvbvoid
1673154647Srwatsonprint_cred(struct ucred *cred)
167438625Srvb{
167538625Srvb
167638625Srvb	int i;
167738625Srvb
167838625Srvb	myprintf(("ref %d\tuid %d\n",cred->cr_ref,cred->cr_uid));
167938625Srvb
168038625Srvb	for (i=0; i < cred->cr_ngroups; i++)
168138625Srvb		myprintf(("\tgroup %d: (%d)\n",i,cred->cr_groups[i]));
168238625Srvb	myprintf(("\n"));
168338625Srvb
168438625Srvb}
168538625Srvb
168638625Srvb/*
168738625Srvb * Return a vnode for the given fid.
168838625Srvb * If no cnode exists for this fid create one and put it
1689119832Stjr * in a table hashed by coda_f2i().  If the cnode for
169038625Srvb * this fid is already in the table return it (ref count is
169139085Srvb * incremented by coda_find.  The cnode will be flushed from the
169239085Srvb * table when coda_inactive calls coda_unsave.
169338625Srvb */
169438625Srvbstruct cnode *
1695154647Srwatsonmake_coda_node(CodaFid *fid, struct mount *vfsp, short type)
169638625Srvb{
169738625Srvb    struct cnode *cp;
169838625Srvb    int          err;
169938625Srvb
170039085Srvb    if ((cp = coda_find(fid)) == NULL) {
170138625Srvb	struct vnode *vp;
170238625Srvb
170339085Srvb	cp = coda_alloc();
170438625Srvb	cp->c_fid = *fid;
170538625Srvb
1706138290Sphk	err = getnewvnode("coda", vfsp, &coda_vnodeops, &vp);
170738625Srvb	if (err) {
170839085Srvb	    panic("coda: getnewvnode returned error %d\n", err);
170938625Srvb	}
1710167497Stegge	err = insmntque1(vp, vfsp, NULL, NULL);	/* XXX: Too early for mpsafe fs */
1711167497Stegge	if (err != 0)
1712167497Stegge		panic("coda: insmntque failed: error %d", err);
171338625Srvb	vp->v_data = cp;
171438625Srvb	vp->v_type = type;
171538625Srvb	cp->c_vnode = vp;
171639085Srvb	coda_save(cp);
171738625Srvb
171838625Srvb    } else {
171938625Srvb	vref(CTOV(cp));
172038625Srvb    }
172138625Srvb
172238625Srvb    return cp;
172338625Srvb}
1724111903Stjr
1725111903Stjrint
1726154647Srwatsoncoda_pathconf(struct vop_pathconf_args *ap)
1727111903Stjr{
1728111903Stjr	int error;
1729111931Stjr	register_t *retval;
1730111903Stjr
1731111903Stjr	retval = ap->a_retval;
1732111903Stjr	error = 0;
1733111903Stjr
1734111903Stjr	switch (ap->a_name) {
1735111903Stjr	case _PC_NAME_MAX:
1736111903Stjr		*retval = CODA_MAXNAMLEN;
1737111903Stjr		break;
1738111903Stjr	case _PC_PATH_MAX:
1739111903Stjr		*retval = CODA_MAXPATHLEN;
1740111903Stjr		break;
1741111903Stjr	default:
1742111903Stjr		error = vop_stdpathconf(ap);
1743111903Stjr		break;
1744111903Stjr	}
1745111903Stjr
1746111903Stjr	return (error);
1747111903Stjr}
1748