coda_vnops.c revision 176233
1139745Simp/*-
238759Srvb *             Coda: an Experimental Distributed File System
338759Srvb *                              Release 3.1
4176139Srwatson *
538759Srvb *           Copyright (c) 1987-1998 Carnegie Mellon University
638759Srvb *                          All Rights Reserved
7176139Srwatson *
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.
16176139Srwatson *
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.
23176139Srwatson *
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.
27176139Srwatson *
2839126Srvb *  	@(#) src/sys/coda/coda_vnops.c,v 1.1.1.1 1998/08/29 21:14:52 rvb Exp $
2938759Srvb */
30176139Srwatson/*
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
41176139Srwatson * M. Satyanarayanan.
4238625Srvb */
4338625Srvb
44116173Sobrien#include <sys/cdefs.h>
45116173Sobrien__FBSDID("$FreeBSD: head/sys/fs/coda/coda_vnops.c 176233 2008-02-13 13:06:22Z 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_pioctl.h>
7438625Srvb
75176139Srwatson/*
7638625Srvb * These flags select various performance enhancements.
7738625Srvb */
78176139Srwatsonstatic int coda_attr_cache = 1;		/* Set to cache attributes. */
79176139Srwatsonstatic int coda_symlink_cache = 1;	/* Set to cache symbolic links. */
80176233Srwatson#if 0
81176139Srwatsonstatic int coda_access_cache = 1;	/* Set to cache some access checks. */
82176233Srwatson#endif
8338625Srvb
84176139Srwatson/*
85176139Srwatson * Structure to keep track of vfs calls.
86176139Srwatson */
87176139Srwatsonstatic struct coda_op_stats coda_vnodeopstats[CODA_VNODEOPS_SIZE];
8838625Srvb
89176139Srwatson#define	MARK_ENTRY(op)		(coda_vnodeopstats[op].entries++)
90176139Srwatson#define	MARK_INT_SAT(op)	(coda_vnodeopstats[op].sat_intrn++)
91176139Srwatson#define	MARK_INT_FAIL(op)	(coda_vnodeopstats[op].unsat_intrn++)
92176139Srwatson#define	MARK_INT_GEN(op)	(coda_vnodeopstats[op].gen_intrn++)
9338625Srvb
94176139Srwatson/*
95176139Srwatson * What we are delaying for in printf.
96176139Srwatson */
97176139Srwatsonint coda_printf_delay = 0;	/* In microseconds */
9839085Srvbint coda_vnop_print_entry = 0;
9939085Srvbstatic int coda_lockdebug = 0;
10038625Srvb
10138625Srvb/*
102176120Srwatson * Some FreeBSD details:
103176139Srwatson *
104176139Srwatson * codadev_modevent is called at boot time or module load time.
10538625Srvb */
106176139Srwatson#define	ENTRY do {							\
107176139Srwatson	if (coda_vnop_print_entry)					\
108176139Srwatson		myprintf(("Entered %s\n", __func__));			\
109176139Srwatson} while (0)
11038625Srvb
111176139Srwatson/*
112176139Srwatson * Definition of the vnode operation vector.
113176139Srwatson */
114138290Sphkstruct vop_vector coda_vnodeops = {
115176139Srwatson	.vop_default = &default_vnodeops,
116176233Srwatson	.vop_cachedlookup = coda_lookup,	/* uncached lookup */
117176233Srwatson	.vop_lookup = vfs_cache_lookup,		/* namecache lookup */
118176139Srwatson	.vop_create = coda_create,		/* create */
119176139Srwatson	.vop_open = coda_open,			/* open */
120176139Srwatson	.vop_close = coda_close,		/* close */
121176139Srwatson	.vop_access = coda_access,		/* access */
122176139Srwatson	.vop_getattr = coda_getattr,		/* getattr */
123176139Srwatson	.vop_setattr = coda_setattr,		/* setattr */
124176139Srwatson	.vop_read = coda_read,			/* read */
125176139Srwatson	.vop_write = coda_write,		/* write */
126176139Srwatson	.vop_ioctl = coda_ioctl,		/* ioctl */
127176139Srwatson	.vop_fsync = coda_fsync,		/* fsync */
128176139Srwatson	.vop_remove = coda_remove,		/* remove */
129176139Srwatson	.vop_link = coda_link,			/* link */
130176139Srwatson	.vop_rename = coda_rename,		/* rename */
131176139Srwatson	.vop_mkdir = coda_mkdir,		/* mkdir */
132176139Srwatson	.vop_rmdir = coda_rmdir,		/* rmdir */
133176139Srwatson	.vop_symlink = coda_symlink,		/* symlink */
134176139Srwatson	.vop_readdir = coda_readdir,		/* readdir */
135176139Srwatson	.vop_readlink = coda_readlink,		/* readlink */
136176139Srwatson	.vop_inactive = coda_inactive,		/* inactive */
137176139Srwatson	.vop_reclaim = coda_reclaim,		/* reclaim */
138176139Srwatson	.vop_lock1 = coda_lock,			/* lock */
139176139Srwatson	.vop_unlock = coda_unlock,		/* unlock */
140176139Srwatson	.vop_bmap = VOP_EOPNOTSUPP,		/* bmap */
141176139Srwatson	.vop_print = VOP_NULL,			/* print */
142176139Srwatson	.vop_islocked = coda_islocked,		/* islocked */
143176139Srwatson	.vop_pathconf = coda_pathconf,		/* pathconf */
144176139Srwatson	.vop_poll = vop_stdpoll,
145176139Srwatson	.vop_getpages = vop_stdgetpages,	/* pager intf.*/
146176139Srwatson	.vop_putpages = vop_stdputpages,	/* pager intf.*/
147176139Srwatson	.vop_getwritemount = vop_stdgetwritemount,
148138290Sphk#if 0
149176139Srwatson	/* missing */
150176139Srwatson	.vop_cachedlookup = ufs_lookup,
151176139Srwatson	.vop_whiteout =	ufs_whiteout,
15238625Srvb#endif
15377784Sshafeeq
15438625Srvb};
15538625Srvb
156176131Srwatsonstatic void	coda_print_vattr(struct vattr *attr);
15738625Srvb
15838625Srvbint
15939085Srvbcoda_vnodeopstats_init(void)
16038625Srvb{
161176139Srwatson	int i;
162176139Srwatson
163176139Srwatson	for(i=0; i<CODA_VNODEOPS_SIZE; i++) {
16439085Srvb		coda_vnodeopstats[i].opcode = i;
16539085Srvb		coda_vnodeopstats[i].entries = 0;
16639085Srvb		coda_vnodeopstats[i].sat_intrn = 0;
16739085Srvb		coda_vnodeopstats[i].unsat_intrn = 0;
16839085Srvb		coda_vnodeopstats[i].gen_intrn = 0;
16938625Srvb	}
170176139Srwatson	return (0);
17138625Srvb}
172176139Srwatson
173176139Srwatson/*
174176139Srwatson * coda_open calls Venus which returns an open file descriptor the cache file
175176139Srwatson * holding the data.  We get the vnode while we are still in the context of
176176139Srwatson * the venus process in coda_psdev.c.  This vnode is then passed back to the
177176139Srwatson * caller and opened.
17838625Srvb */
17938625Srvbint
180138290Sphkcoda_open(struct vop_open_args *ap)
18138625Srvb{
18238625Srvb
183176139Srwatson	/*
184176139Srwatson	 * FreeBSD can pass the O_EXCL flag in mode, even though the check
185176139Srwatson	 * has already happened.  Venus defensively assumes that if open is
186176139Srwatson	 * passed the EXCL, it must be a bug.  We strip the flag here.
187176139Srwatson	 */
188176139Srwatson	/* true args */
189176139Srwatson	struct vnode **vpp = &(ap->a_vp);
190176139Srwatson	struct cnode *cp = VTOC(*vpp);
191176139Srwatson	int flag = ap->a_mode & (~O_EXCL);
192176139Srwatson	struct ucred *cred = ap->a_cred;
193176139Srwatson	struct thread *td = ap->a_td;
194176139Srwatson	/* locals */
195176139Srwatson	int error;
196176139Srwatson	struct vnode *vp;
19738625Srvb
198176139Srwatson	MARK_ENTRY(CODA_OPEN_STATS);
199176139Srwatson
200176139Srwatson	/*
201176139Srwatson	 * Check for open of control file.
202176139Srwatson	 */
203176139Srwatson	if (IS_CTL_VP(*vpp)) {
204176139Srwatson		/* XXX */
205176139Srwatson		/* if (WRITEABLE(flag)) */
206176139Srwatson		if (flag & (FWRITE | O_TRUNC | O_CREAT | O_EXCL)) {
207176139Srwatson			MARK_INT_FAIL(CODA_OPEN_STATS);
208176139Srwatson			return (EACCES);
209176139Srwatson		}
210176139Srwatson		MARK_INT_SAT(CODA_OPEN_STATS);
211176139Srwatson		return (0);
21238625Srvb	}
213176139Srwatson	error = venus_open(vtomi((*vpp)), &cp->c_fid, flag, cred,
214176139Srwatson	    td->td_proc, &vp);
215176139Srwatson	if (error)
216176139Srwatson		return (error);
217176139Srwatson	CODADEBUG(CODA_OPEN, myprintf(("open: vp %p result %d\n", vp,
218176139Srwatson	    error)););
21938625Srvb
220176139Srwatson	/*
221176139Srwatson	 * Save the vnode pointer for the cache file.
222176139Srwatson	 */
223176139Srwatson	if (cp->c_ovp == NULL) {
224176139Srwatson		cp->c_ovp = vp;
225176139Srwatson	} else {
226176139Srwatson		if (cp->c_ovp != vp)
227176139Srwatson			panic("coda_open: cp->c_ovp != ITOV(ip)");
228176139Srwatson	}
229176139Srwatson	cp->c_ocount++;
23038625Srvb
231176139Srwatson	/*
232176139Srwatson	 * Flush the attribute cached if writing the file.
233176139Srwatson	 */
234176139Srwatson	if (flag & FWRITE) {
235176139Srwatson		cp->c_owrite++;
236176139Srwatson		cp->c_flags &= ~C_VATTR;
237176139Srwatson	}
23838625Srvb
239176139Srwatson	/*
240176139Srwatson	 * Open the cache file.
241176139Srwatson	 */
242176139Srwatson	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
243176139Srwatson	error = VOP_OPEN(vp, flag, cred, td, NULL);
244176139Srwatson	if (error) {
245176139Srwatson		VOP_UNLOCK(vp, 0);
246176139Srwatson    		printf("coda_open: VOP_OPEN on container failed %d\n", error);
247176139Srwatson		return (error);
248176139Srwatson	}
249176139Srwatson	(*vpp)->v_object = vp->v_object;
250176118Srwatson	VOP_UNLOCK(vp, 0);
251176139Srwatson	return (0);
25238625Srvb}
25338625Srvb
25438625Srvb/*
25538625Srvb * Close the cache file used for I/O and notify Venus.
25638625Srvb */
25738625Srvbint
258138290Sphkcoda_close(struct vop_close_args *ap)
25938625Srvb{
260176139Srwatson	/* true args */
261176139Srwatson	struct vnode *vp = ap->a_vp;
262176139Srwatson	struct cnode *cp = VTOC(vp);
263176139Srwatson	int flag = ap->a_fflag;
264176139Srwatson	struct ucred *cred = ap->a_cred;
265176139Srwatson	struct thread *td = ap->a_td;
266176139Srwatson	/* locals */
267176139Srwatson	int error;
26838625Srvb
269176139Srwatson	MARK_ENTRY(CODA_CLOSE_STATS);
27038625Srvb
271176139Srwatson	/*
272176139Srwatson	 * Check for close of control file.
273176139Srwatson	 */
274176139Srwatson	if (IS_CTL_VP(vp)) {
275176139Srwatson		MARK_INT_SAT(CODA_CLOSE_STATS);
276176139Srwatson		return (0);
277176139Srwatson	}
278176139Srwatson	if (cp->c_ovp) {
279176139Srwatson		vn_lock(cp->c_ovp, LK_EXCLUSIVE | LK_RETRY);
280176139Srwatson		/* Do errors matter here? */
281176139Srwatson		VOP_CLOSE(cp->c_ovp, flag, cred, td);
282176139Srwatson		vput(cp->c_ovp);
283176139Srwatson	}
284171377Srwatson#ifdef CODA_VERBOSE
285176139Srwatson	else
286176139Srwatson		printf("coda_close: NO container vp %p/cp %p\n", vp, cp);
287171377Srwatson#endif
288176139Srwatson	if (--cp->c_ocount == 0)
289176139Srwatson		cp->c_ovp = NULL;
29038625Srvb
291176139Srwatson	/*
292176139Srwatson	 * File was opened for write.
293176139Srwatson	 */
294176139Srwatson	if (flag & FWRITE)
295176139Srwatson		--cp->c_owrite;
296176139Srwatson	if (!IS_UNMOUNTING(cp))
297176139Srwatson		error = venus_close(vtomi(vp), &cp->c_fid, flag, cred,
298176139Srwatson		    td->td_proc);
299176139Srwatson	else
300176139Srwatson		error = ENODEV;
301176139Srwatson	CODADEBUG(CODA_CLOSE, myprintf(("close: result %d\n",error)););
302176139Srwatson	return (error);
30338625Srvb}
30438625Srvb
30538625Srvbint
306138290Sphkcoda_read(struct vop_read_args *ap)
30738625Srvb{
30838625Srvb
309176139Srwatson	ENTRY;
310176139Srwatson	return (coda_rdwr(ap->a_vp, ap->a_uio, UIO_READ, ap->a_ioflag,
311176139Srwatson	    ap->a_cred, ap->a_uio->uio_td));
31238625Srvb}
31338625Srvb
31438625Srvbint
315138290Sphkcoda_write(struct vop_write_args *ap)
31638625Srvb{
31738625Srvb
318176139Srwatson	ENTRY;
319176139Srwatson	return (coda_rdwr(ap->a_vp, ap->a_uio, UIO_WRITE, ap->a_ioflag,
320176139Srwatson	    ap->a_cred, ap->a_uio->uio_td));
32138625Srvb}
32238625Srvb
32338625Srvbint
324154647Srwatsoncoda_rdwr(struct vnode *vp, struct uio *uiop, enum uio_rw rw, int ioflag,
325154647Srwatson    struct ucred *cred, struct thread *td)
326176139Srwatson{
327176139Srwatson	/* upcall decl */
328176139Srwatson	/* NOTE: container file operation!!! */
329176139Srwatson	/* locals */
330176139Srwatson	struct cnode *cp = VTOC(vp);
331176139Srwatson	struct vnode *cfvp = cp->c_ovp;
332176139Srwatson	int opened_internally = 0;
333176139Srwatson	int error = 0;
33438625Srvb
335176139Srwatson	MARK_ENTRY(CODA_RDWR_STATS);
336176139Srwatson	CODADEBUG(CODA_RDWR, myprintf(("coda_rdwr(%d, %p, %d, %lld, %d)\n",
337176139Srwatson	    rw, (void *)uiop->uio_iov->iov_base, uiop->uio_resid,
338176139Srwatson	    (long long)uiop->uio_offset, uiop->uio_segflg)););
33938625Srvb
340176139Srwatson	/*
341176139Srwatson	 * Check for rdwr of control object.
342176139Srwatson	 */
343176139Srwatson	if (IS_CTL_VP(vp)) {
344176139Srwatson		MARK_INT_FAIL(CODA_RDWR_STATS);
345176139Srwatson		return (EINVAL);
346176139Srwatson	}
34738625Srvb
348176139Srwatson	/*
349176139Srwatson	 * If file is not already open this must be a page {read,write}
350176139Srwatson	 * request and we should open it internally.
351176139Srwatson	 */
352176139Srwatson	if (cfvp == NULL) {
353176139Srwatson		opened_internally = 1;
354176139Srwatson		MARK_INT_GEN(CODA_OPEN_STATS);
355176139Srwatson		error = VOP_OPEN(vp, (rw == UIO_READ ? FREAD : FWRITE), cred,
356176139Srwatson		    td, NULL);
357175545Srwatson#ifdef CODA_VERBOSE
358176139Srwatson		printf("coda_rdwr: Internally Opening %p\n", vp);
359175545Srwatson#endif
360176139Srwatson		if (error) {
361176139Srwatson			printf("coda_rdwr: VOP_OPEN on container failed "
362176139Srwatson			    "%d\n", error);
363176139Srwatson			return (error);
364176139Srwatson		}
365176139Srwatson		cfvp = cp->c_ovp;
36638625Srvb	}
36738625Srvb
368176139Srwatson	/*
369176139Srwatson	 * Have UFS handle the call.
370176139Srwatson	 */
371176139Srwatson	CODADEBUG(CODA_RDWR, myprintf(("indirect rdwr: fid = %s, refcnt = "
372176139Srwatson	    "%d\n", coda_f2s(&cp->c_fid), CTOV(cp)->v_usecount)););
373176139Srwatson	vn_lock(cfvp, LK_EXCLUSIVE | LK_RETRY);
374176139Srwatson	if (rw == UIO_READ) {
375176139Srwatson		error = VOP_READ(cfvp, uiop, ioflag, cred);
376176139Srwatson	} else {
377176139Srwatson		error = VOP_WRITE(cfvp, uiop, ioflag, cred);
378176139Srwatson		/*
379176139Srwatson		 * ufs_write updates the vnode_pager_setsize for the
380176139Srwatson		 * vnode/object.
381176139Srwatson		 *
382176139Srwatson		 * XXX: Since we now share vm objects between layers, this is
383176139Srwatson		 * probably unnecessary.
384176139Srwatson		 */
385176139Srwatson		{
386176139Srwatson			struct vattr attr;
387176139Srwatson			if (VOP_GETATTR(cfvp, &attr, cred, td) == 0)
388176139Srwatson				vnode_pager_setsize(vp, attr.va_size);
389176139Srwatson		}
390176139Srwatson	}
391176139Srwatson	VOP_UNLOCK(cfvp, 0);
392176139Srwatson	if (error)
393176139Srwatson		MARK_INT_FAIL(CODA_RDWR_STATS);
394176139Srwatson	else
395176139Srwatson		MARK_INT_SAT(CODA_RDWR_STATS);
39639650Srvb
397176139Srwatson	/*
398176139Srwatson	 * Do an internal close if necessary.
399176139Srwatson	 */
400176139Srwatson	if (opened_internally) {
401176139Srwatson		MARK_INT_GEN(CODA_CLOSE_STATS);
402176139Srwatson		(void)VOP_CLOSE(vp, (rw == UIO_READ ? FREAD : FWRITE), cred,
403176139Srwatson		    td);
40438625Srvb	}
40538625Srvb
406176139Srwatson	/*
407176139Srwatson	 * Invalidate cached attributes if writing.
408176139Srwatson	 */
409176139Srwatson	if (rw == UIO_WRITE)
410176139Srwatson		cp->c_flags &= ~C_VATTR;
411176139Srwatson	return (error);
41238625Srvb}
41338625Srvb
41438625Srvbint
415138290Sphkcoda_ioctl(struct vop_ioctl_args *ap)
41638625Srvb{
417176139Srwatson	/* true args */
418176139Srwatson	struct vnode *vp = ap->a_vp;
419176139Srwatson	int com = ap->a_command;
420176139Srwatson	caddr_t data = ap->a_data;
421176139Srwatson	int flag = ap->a_fflag;
422176139Srwatson	struct ucred *cred = ap->a_cred;
423176139Srwatson	struct thread *td = ap->a_td;
424176139Srwatson	/* locals */
425176139Srwatson	int error;
426176139Srwatson	struct vnode *tvp;
427176139Srwatson	struct nameidata ndp;
428176139Srwatson	struct PioctlData *iap = (struct PioctlData *)data;
42938625Srvb
430176139Srwatson	MARK_ENTRY(CODA_IOCTL_STATS);
431176139Srwatson	CODADEBUG(CODA_IOCTL, myprintf(("in coda_ioctl on %s\n", iap->path)););
43238625Srvb
433176139Srwatson	/*
434176139Srwatson	 * Don't check for operation on a dying object, for ctlvp it
435176139Srwatson	 * shouldn't matter.
436176139Srwatson	 *
437176139Srwatson	 * Must be control object to succeed.
438176139Srwatson	 */
439176139Srwatson	if (!IS_CTL_VP(vp)) {
440176139Srwatson		MARK_INT_FAIL(CODA_IOCTL_STATS);
441176139Srwatson		CODADEBUG(CODA_IOCTL, myprintf(("coda_ioctl error: vp != "
442176139Srwatson		    "ctlvp")););
443176139Srwatson		return (EOPNOTSUPP);
444176139Srwatson	}
44538625Srvb
446176139Srwatson	/*
447176139Srwatson	 * Look up the pathname.
448176139Srwatson	 *
449176139Srwatson	 * Should we use the name cache here? It would get it from lookupname
450176139Srwatson	 * sooner or later anyway, right?
451176139Srwatson	 */
452176139Srwatson	NDINIT(&ndp, LOOKUP, (iap->follow ? FOLLOW : NOFOLLOW),
453176139Srwatson	    UIO_USERSPACE, iap->path, td);
454176139Srwatson	error = namei(&ndp);
455176139Srwatson	tvp = ndp.ni_vp;
456176139Srwatson	if (error) {
457176139Srwatson		MARK_INT_FAIL(CODA_IOCTL_STATS);
458176139Srwatson		CODADEBUG(CODA_IOCTL, myprintf(("coda_ioctl error: lookup "
459176139Srwatson		    "returns %d\n", error)););
460176139Srwatson		return (error);
461176139Srwatson	}
46238625Srvb
463176139Srwatson	/*
464176139Srwatson	 * Make sure this is a coda style cnode, but it may be a different
465176139Srwatson	 * vfsp.
466176139Srwatson	 */
467176139Srwatson	if (tvp->v_op != &coda_vnodeops) {
468176139Srwatson		vrele(tvp);
469176139Srwatson		NDFREE(&ndp, NDF_ONLY_PNBUF);
470176139Srwatson		MARK_INT_FAIL(CODA_IOCTL_STATS);
471176139Srwatson		CODADEBUG(CODA_IOCTL,
472176139Srwatson		myprintf(("coda_ioctl error: %s not a coda object\n",
473176139Srwatson		    iap->path)););
474176139Srwatson		return (EINVAL);
475176139Srwatson	}
476176139Srwatson	if (iap->vi.in_size > VC_MAXDATASIZE) {
477176139Srwatson		NDFREE(&ndp, 0);
478176139Srwatson		return (EINVAL);
479176139Srwatson	}
480176139Srwatson	error = venus_ioctl(vtomi(tvp), &((VTOC(tvp))->c_fid), com, flag,
481176139Srwatson	    data, cred, td->td_proc);
482176139Srwatson	if (error)
483176139Srwatson		MARK_INT_FAIL(CODA_IOCTL_STATS);
484176139Srwatson	else
485176139Srwatson		CODADEBUG(CODA_IOCTL, myprintf(("Ioctl returns %d \n",
486176139Srwatson		    error)););
48738625Srvb	vrele(tvp);
48854655Seivind	NDFREE(&ndp, NDF_ONLY_PNBUF);
489176139Srwatson	return (error);
49038625Srvb}
49138625Srvb
49238625Srvb/*
493176139Srwatson * To reduce the cost of a user-level venus;we cache attributes in the
494176139Srwatson * kernel.  Each cnode has storage allocated for an attribute.  If c_vattr is
495176139Srwatson * valid, return a reference to it.  Otherwise, get the attributes from venus
496176139Srwatson * and store them in the cnode.  There is some question if this method is a
497176139Srwatson * security leak.  But I think that in order to make this call, the user must
498176139Srwatson * have done a lookup and opened the file, and therefore should already have
499176139Srwatson * access.
50038625Srvb */
50138625Srvbint
502138290Sphkcoda_getattr(struct vop_getattr_args *ap)
50338625Srvb{
504176139Srwatson	/* true args */
505176139Srwatson	struct vnode *vp = ap->a_vp;
506176139Srwatson	struct cnode *cp = VTOC(vp);
507176139Srwatson	struct vattr *vap = ap->a_vap;
508176139Srwatson	struct ucred *cred = ap->a_cred;
509176139Srwatson	struct thread *td = ap->a_td;
510176139Srwatson	/* locals */
511176139Srwatson    	struct vnode *convp;
512176139Srwatson	int error, size;
51338625Srvb
514176139Srwatson	MARK_ENTRY(CODA_GETATTR_STATS);
515176139Srwatson	if (IS_UNMOUNTING(cp))
516176139Srwatson		return (ENODEV);
51738625Srvb
518176139Srwatson	/*
519176139Srwatson	 * Check for getattr of control object.
520176139Srwatson	 */
521176139Srwatson	if (IS_CTL_VP(vp)) {
522176139Srwatson		MARK_INT_FAIL(CODA_GETATTR_STATS);
523176139Srwatson		return (ENOENT);
524176139Srwatson	}
52538759Srvb
526176139Srwatson	/*
527176139Srwatson	 * Check to see if the attributes have already been cached.
528176139Srwatson	 */
529176139Srwatson	if (VALID_VATTR(cp)) {
530176139Srwatson		CODADEBUG(CODA_GETATTR, myprintf(("attr cache hit: %s\n",
531176139Srwatson		    coda_f2s(&cp->c_fid))););
532176139Srwatson		CODADEBUG(CODA_GETATTR, if (!(codadebug & ~CODA_GETATTR))
533176139Srwatson		    coda_print_vattr(&cp->c_vattr););
534176139Srwatson		*vap = cp->c_vattr;
535176139Srwatson		MARK_INT_SAT(CODA_GETATTR_STATS);
536176139Srwatson		return (0);
537176139Srwatson	}
538176139Srwatson    	error = venus_getattr(vtomi(vp), &cp->c_fid, cred, td->td_proc, vap);
539176139Srwatson	if (!error) {
540176139Srwatson		CODADEBUG(CODA_GETATTR, myprintf(("getattr miss %s: result "
541176139Srwatson		    "%d\n", coda_f2s(&cp->c_fid), error)););
542176139Srwatson		CODADEBUG(CODA_GETATTR, if (!(codadebug & ~CODA_GETATTR))
543176139Srwatson		    coda_print_vattr(vap););
54438625Srvb
545176139Srwatson		/*
546176139Srwatson		 * XXX: Since we now share vm objects between layers, this is
547176139Srwatson		 * probably unnecessary.
548176139Srwatson		 */
549176139Srwatson		size = vap->va_size;
550176139Srwatson    		convp = cp->c_ovp;
551176139Srwatson		if (convp != NULL)
552176139Srwatson			vnode_pager_setsize(convp, size);
55338625Srvb
554176139Srwatson		/*
555176139Srwatson		 * If not open for write, store attributes in cnode.
556176139Srwatson		 */
557176139Srwatson		if ((cp->c_owrite == 0) && (coda_attr_cache)) {
558176139Srwatson			cp->c_vattr = *vap;
559176139Srwatson			cp->c_flags |= C_VATTR;
560176139Srwatson		}
56138625Srvb	}
562176139Srwatson	return (error);
56338625Srvb}
56438625Srvb
56538625Srvbint
566138290Sphkcoda_setattr(struct vop_setattr_args *ap)
56738625Srvb{
568176139Srwatson	/* true args */
569176139Srwatson	struct vnode *vp = ap->a_vp;
570176139Srwatson	struct cnode *cp = VTOC(vp);
571176139Srwatson	struct vattr *vap = ap->a_vap;
572176139Srwatson	struct ucred *cred = ap->a_cred;
573176139Srwatson	struct thread *td = ap->a_td;
574176139Srwatson	/* locals */
575176139Srwatson    	struct vnode *convp;
576176139Srwatson	int error, size;
57738625Srvb
578176139Srwatson	MARK_ENTRY(CODA_SETATTR_STATS);
57938625Srvb
580176139Srwatson	/*
581176139Srwatson	 * Check for setattr of control object.
582176139Srwatson	 */
583176139Srwatson	if (IS_CTL_VP(vp)) {
584176139Srwatson		MARK_INT_FAIL(CODA_SETATTR_STATS);
585176139Srwatson		return (ENOENT);
586176139Srwatson	}
587176139Srwatson	if (codadebug & CODADBGMSK(CODA_SETATTR))
588176139Srwatson		coda_print_vattr(vap);
589176139Srwatson	error = venus_setattr(vtomi(vp), &cp->c_fid, vap, cred, td->td_proc);
590176139Srwatson	if (!error)
591176139Srwatson		cp->c_flags &= ~C_VATTR;
59238625Srvb
593176139Srwatson	/*
594176139Srwatson	 * XXX: Since we now share vm objects between layers, this is
595176139Srwatson	 * probably unnecessary.
596176139Srwatson	 *
597176139Srwatson	 * XXX: Shouldn't we only be doing this "set" if C_VATTR remains
598176139Srwatson	 * valid after venus_setattr()?
599176139Srwatson	 */
600176139Srwatson	size = vap->va_size;
601176139Srwatson    	convp = cp->c_ovp;
602176139Srwatson	if (size != VNOVAL && convp != NULL)
603176139Srwatson		vnode_pager_setsize(convp, size);
604176139Srwatson	CODADEBUG(CODA_SETATTR,	myprintf(("setattr %d\n", error)););
605176139Srwatson	return (error);
60638625Srvb}
60738625Srvb
60838625Srvbint
609138290Sphkcoda_access(struct vop_access_args *ap)
61038625Srvb{
611176139Srwatson	/* true args */
612176139Srwatson	struct vnode *vp = ap->a_vp;
613176139Srwatson	struct cnode *cp = VTOC(vp);
614176139Srwatson	int mode = ap->a_mode;
615176139Srwatson	struct ucred *cred = ap->a_cred;
616176139Srwatson	struct thread *td = ap->a_td;
617176139Srwatson	/* locals */
61838625Srvb
619176139Srwatson	MARK_ENTRY(CODA_ACCESS_STATS);
62038625Srvb
621176139Srwatson	/*
622176139Srwatson	 * Check for access of control object.  Only read access is allowed
623176139Srwatson	 * on it.
624176139Srwatson	 */
625176139Srwatson	if (IS_CTL_VP(vp)) {
626176139Srwatson		/*
627176139Srwatson		 * Bogus hack - all will be marked as successes.
628176139Srwatson		 */
62939085Srvb		MARK_INT_SAT(CODA_ACCESS_STATS);
630176139Srwatson		return (((mode & VREAD) && !(mode & (VWRITE | VEXEC)))
631176139Srwatson		    ? 0 : EACCES);
63238625Srvb	}
63338625Srvb
634176139Srwatson	/*
635176233Srwatson	 * XXXRW: We should add an actual access cache here, similar to the
636176233Srwatson	 * one found in NFS, the Linux Coda module, etc.
637176233Srwatson	 *
638176233Srwatson	 * In principle it could be as simple as caching the uid and granted
639176233Srwatson	 * access mode (as in NFS), but we also need invalidation.  The Coda
640176233Srwatson	 * module on Linux does this using a global generation number which
641176233Srwatson	 * is bumped on an access control cache flush, whereas NFS does it
642176233Srwatson	 * with a timeout.
643176139Srwatson	 */
644176233Srwatson	return (venus_access(vtomi(vp), &cp->c_fid, mode, cred, td->td_proc));
64538625Srvb}
64638625Srvb
64738625Srvbint
648138290Sphkcoda_readlink(struct vop_readlink_args *ap)
64938625Srvb{
650176139Srwatson	/* true args */
651176139Srwatson	struct vnode *vp = ap->a_vp;
652176139Srwatson	struct cnode *cp = VTOC(vp);
653176139Srwatson	struct uio *uiop = ap->a_uio;
654176139Srwatson	struct ucred *cred = ap->a_cred;
655176139Srwatson	struct thread *td = ap->a_uio->uio_td;
656176139Srwatson	/* locals */
657176139Srwatson	int error;
658176139Srwatson	char *str;
659176139Srwatson	int len;
66038625Srvb
661176139Srwatson	MARK_ENTRY(CODA_READLINK_STATS);
66238625Srvb
663176139Srwatson	/*
664176139Srwatson	 * Check for readlink of control object.
665176139Srwatson	 */
666176139Srwatson	if (IS_CTL_VP(vp)) {
667176139Srwatson		MARK_INT_FAIL(CODA_READLINK_STATS);
668176139Srwatson		return (ENOENT);
669176139Srwatson	}
670176139Srwatson	if ((coda_symlink_cache) && (VALID_SYMLINK(cp))) {
671176139Srwatson		/*
672176139Srwatson		 * Symlink was cached.
673176139Srwatson		 */
674176139Srwatson		uiop->uio_rw = UIO_READ;
675176139Srwatson		error = uiomove(cp->c_symlink, (int)cp->c_symlen, uiop);
676176139Srwatson		if (error)
677176139Srwatson			MARK_INT_FAIL(CODA_READLINK_STATS);
678176139Srwatson		else
679176139Srwatson			MARK_INT_SAT(CODA_READLINK_STATS);
680176139Srwatson		return (error);
681176139Srwatson	}
682176139Srwatson	error = venus_readlink(vtomi(vp), &cp->c_fid, cred, td != NULL ?
683176139Srwatson	    td->td_proc : NULL, &str, &len);
684176139Srwatson	if (!error) {
685176139Srwatson		uiop->uio_rw = UIO_READ;
686176139Srwatson		error = uiomove(str, len, uiop);
687176139Srwatson		if (coda_symlink_cache) {
688176139Srwatson			cp->c_symlink = str;
689176139Srwatson			cp->c_symlen = len;
690176139Srwatson			cp->c_flags |= C_SYMLINK;
691176139Srwatson		} else
692176139Srwatson			CODA_FREE(str, len);
693176139Srwatson	}
694176139Srwatson	CODADEBUG(CODA_READLINK, myprintf(("in readlink result %d\n",
695176139Srwatson	    error)););
696176139Srwatson	return (error);
69738625Srvb}
69838625Srvb
69938625Srvbint
700138290Sphkcoda_fsync(struct vop_fsync_args *ap)
70138625Srvb{
702176139Srwatson	/* true args */
703176139Srwatson	struct vnode *vp = ap->a_vp;
704176139Srwatson	struct cnode *cp = VTOC(vp);
705176139Srwatson	struct thread *td = ap->a_td;
706176139Srwatson	/* locals */
707176139Srwatson	struct vnode *convp = cp->c_ovp;
708176139Srwatson	int error;
70938625Srvb
710176139Srwatson	MARK_ENTRY(CODA_FSYNC_STATS);
71138625Srvb
712176139Srwatson	/*
713176139Srwatson	 * Check for fsync on an unmounting object.
714176139Srwatson	 *
715176139Srwatson	 * XXX: Is this comment true on FreeBSD?  It seems likely, since
716176139Srwatson	 * unmounting is fairly non-atomic.
717176139Srwatson	 *
718176139Srwatson	 * The NetBSD kernel, in it's infinite wisdom, can try to fsync after
719176139Srwatson	 * an unmount has been initiated.  This is a Bad Thing, which we have
720176139Srwatson	 * to avoid.  Not a legitimate failure for stats.
721176139Srwatson	 */
722176139Srwatson	if (IS_UNMOUNTING(cp))
723176139Srwatson		return (ENODEV);
72438625Srvb
725176139Srwatson	/*
726176139Srwatson	 * Check for fsync of control object.
727176139Srwatson	 */
728176139Srwatson	if (IS_CTL_VP(vp)) {
729176139Srwatson		MARK_INT_SAT(CODA_FSYNC_STATS);
730176139Srwatson		return (0);
731176139Srwatson	}
732176139Srwatson	if (convp != NULL) {
733176139Srwatson		vn_lock(convp, LK_EXCLUSIVE | LK_RETRY);
734176139Srwatson		VOP_FSYNC(convp, MNT_WAIT, td);
735176139Srwatson		VOP_UNLOCK(convp, 0);
736176139Srwatson	}
73738625Srvb
738176139Srwatson	/*
739176139Srwatson	 * We see fsyncs with usecount == 1 then usecount == 0.  For now we
740176139Srwatson	 * ignore them.
741176139Srwatson	 */
742176139Srwatson#if 0
743176139Srwatson	VI_LOCK(vp);
744176139Srwatson	if (!vp->v_usecount) {
745176139Srwatson		printf("coda_fsync on vnode %p with %d usecount.  "
746176139Srwatson		    "c_flags = %x (%x)\n", vp, vp->v_usecount, cp->c_flags,
747176139Srwatson		    cp->c_flags&C_PURGING);
748176139Srwatson	}
749176139Srwatson	VI_UNLOCK(vp);
750176139Srwatson#endif
75138625Srvb
752176139Srwatson	/*
753176139Srwatson	 * We can expect fsync on any vnode at all if venus is purging it.
754176139Srwatson	 * Venus can't very well answer the fsync request, now can it?
755176139Srwatson	 * Hopefully, it won't have to, because hopefully, venus preserves
756176139Srwatson	 * the (possibly untrue) invariant that it never purges an open
757176139Srwatson	 * vnode.  Hopefully.
758176139Srwatson	 */
759176139Srwatson	if (cp->c_flags & C_PURGING)
760176139Srwatson		return (0);
76138625Srvb
762176139Srwatson	/* XXX: needs research */
763176139Srwatson	return (0);
764176139Srwatson	error = venus_fsync(vtomi(vp), &cp->c_fid, td->td_proc);
765176139Srwatson	CODADEBUG(CODA_FSYNC, myprintf(("in fsync result %d\n", error)););
766176139Srwatson	return (error);
76738625Srvb}
76838625Srvb
76938625Srvbint
770138290Sphkcoda_inactive(struct vop_inactive_args *ap)
77138625Srvb{
772176139Srwatson	/*
773176139Srwatson	 * XXX - at the moment, inactive doesn't look at cred, and doesn't
774176139Srwatson	 * have a proc pointer.  Oops.
775176139Srwatson	 */
776176139Srwatson	/* true args */
777176139Srwatson	struct vnode *vp = ap->a_vp;
778176139Srwatson	struct cnode *cp = VTOC(vp);
779176139Srwatson	struct ucred *cred __attribute__((unused)) = NULL;
780176139Srwatson	struct thread *td __attribute__((unused)) = curthread;
781176139Srwatson	/* upcall decl */
782176139Srwatson	/* locals */
78338625Srvb
784176139Srwatson	/*
785176139Srwatson	 * We don't need to send inactive to venus - DCS.
786176139Srwatson	 */
787176139Srwatson	MARK_ENTRY(CODA_INACTIVE_STATS);
788176139Srwatson	CODADEBUG(CODA_INACTIVE, myprintf(("in inactive, %s, vfsp %p\n",
789176139Srwatson	    coda_f2s(&cp->c_fid), vp->v_mount)););
790176139Srwatson	vp->v_object = NULL;
79138625Srvb
792176139Srwatson	/*
793176139Srwatson	 * If an array has been allocated to hold the symlink, deallocate it.
794176139Srwatson	 */
795176139Srwatson	if ((coda_symlink_cache) && (VALID_SYMLINK(cp))) {
796176139Srwatson		if (cp->c_symlink == NULL)
797176139Srwatson			panic("coda_inactive: null symlink pointer in cnode");
798176139Srwatson		CODA_FREE(cp->c_symlink, cp->c_symlen);
799176139Srwatson		cp->c_flags &= ~C_SYMLINK;
800176139Srwatson		cp->c_symlen = 0;
801176139Srwatson	}
802175473Srwatson
803176139Srwatson	/*
804176139Srwatson	 * Remove it from the table so it can't be found.
805176139Srwatson	 */
806176139Srwatson	coda_unsave(cp);
807176139Srwatson	if ((struct coda_mntinfo *)(vp->v_mount->mnt_data) == NULL) {
808176139Srwatson		myprintf(("Help! vfsp->vfs_data was NULL, but vnode %p "
809176139Srwatson		    "wasn't dying\n", vp));
810176139Srwatson		panic("badness in coda_inactive\n");
811176139Srwatson	}
812176139Srwatson	if (IS_UNMOUNTING(cp)) {
81338625Srvb#ifdef	DEBUG
814176139Srwatson		printf("coda_inactive: IS_UNMOUNTING use %d: vp %p, cp %p\n",
815176139Srwatson		    vrefcnt(vp), vp, cp);
816176139Srwatson		if (cp->c_ovp != NULL)
817176139Srwatson			printf("coda_inactive: cp->ovp != NULL use %d: vp "
818176139Srwatson			    "%p, cp %p\n", vrefcnt(vp), vp, cp);
81938625Srvb#endif
820176139Srwatson	} else {
82139650Srvb#ifdef OLD_DIAGNOSTIC
822176139Srwatson		if (vrefcnt(CTOV(cp)))
823176139Srwatson			panic("coda_inactive: nonzero reference count");
824176139Srwatson		if (cp->c_ovp != NULL)
825176139Srwatson			panic("coda_inactive:  cp->ovp != NULL");
826176139Srwatson#endif
827176139Srwatson		vgone(vp);
82838625Srvb	}
829176139Srwatson	MARK_INT_SAT(CODA_INACTIVE_STATS);
830176139Srwatson	return (0);
83138625Srvb}
83238625Srvb
83338625Srvb/*
83496755Strhodes * Remote filesystem operations having to do with directory manipulation.
83538625Srvb */
83638625Srvb
837176139Srwatson/*
838176120Srwatson * In FreeBSD, lookup returns the vnode locked.
83938625Srvb */
84038625Srvbint
841176233Srwatsoncoda_lookup(struct vop_cachedlookup_args *ap)
84238625Srvb{
843176139Srwatson	/* true args */
844176139Srwatson	struct vnode *dvp = ap->a_dvp;
845176139Srwatson	struct cnode *dcp = VTOC(dvp);
846176139Srwatson	struct vnode **vpp = ap->a_vpp;
847176139Srwatson	/*
848176139Srwatson	 * It looks as though ap->a_cnp->ni_cnd->cn_nameptr holds the rest of
849176139Srwatson	 * the string to xlate, and that we must try to get at least
850176139Srwatson	 * ap->a_cnp->ni_cnd->cn_namelen of those characters to macth.  I
851176139Srwatson	 * could be wrong.
852176139Srwatson	 */
853176139Srwatson	struct componentname  *cnp = ap->a_cnp;
854176139Srwatson	struct ucred *cred = cnp->cn_cred;
855176139Srwatson	struct thread *td = cnp->cn_thread;
856176139Srwatson	/* locals */
857176139Srwatson	struct cnode *cp;
858176139Srwatson	const char *nm = cnp->cn_nameptr;
859176139Srwatson	int len = cnp->cn_namelen;
860176139Srwatson	CodaFid VFid;
861176139Srwatson	int vtype;
862176139Srwatson	int error = 0;
86338625Srvb
864176139Srwatson	MARK_ENTRY(CODA_LOOKUP_STATS);
865176139Srwatson	CODADEBUG(CODA_LOOKUP, myprintf(("lookup: %s in %s\n", nm,
866176139Srwatson	    coda_f2s(&dcp->c_fid))););
86738625Srvb
868176139Srwatson	/*
869176139Srwatson	 * Check for lookup of control object.
870176139Srwatson	 */
871176139Srwatson	if (IS_CTL_NAME(dvp, nm, len)) {
872176139Srwatson		*vpp = coda_ctlvp;
873176139Srwatson		vref(*vpp);
874176139Srwatson		MARK_INT_SAT(CODA_LOOKUP_STATS);
875176139Srwatson		goto exit;
876176139Srwatson	}
877176139Srwatson	if (len+1 > CODA_MAXNAMLEN) {
878176139Srwatson		MARK_INT_FAIL(CODA_LOOKUP_STATS);
879176139Srwatson		CODADEBUG(CODA_LOOKUP, myprintf(("name too long: lookup, "
880176139Srwatson		    "%s (%s)\n", coda_f2s(&dcp->c_fid), nm)););
881176139Srwatson		*vpp = NULL;
882176139Srwatson		error = EINVAL;
883176139Srwatson		goto exit;
884176139Srwatson	}
88538625Srvb
886176233Srwatson	error = venus_lookup(vtomi(dvp), &dcp->c_fid, nm, len, cred,
887176233Srwatson	    td->td_proc, &VFid, &vtype);
888176233Srwatson	if (error) {
889176233Srwatson		MARK_INT_FAIL(CODA_LOOKUP_STATS);
890176233Srwatson		CODADEBUG(CODA_LOOKUP, myprintf(("lookup error on %s "
891176233Srwatson		    "(%s)%d\n", coda_f2s(&dcp->c_fid), nm, error)););
892176233Srwatson		*vpp = NULL;
893176139Srwatson	} else {
894176233Srwatson		MARK_INT_SAT(CODA_LOOKUP_STATS);
895176233Srwatson		CODADEBUG(CODA_LOOKUP, myprintf(("lookup: %s type %o "
896176233Srwatson		    "result %d\n", coda_f2s(&VFid), vtype, error)););
897176233Srwatson		cp = make_coda_node(&VFid, dvp->v_mount, vtype);
898176233Srwatson    		*vpp = CTOV(cp);
899176233Srwatson
900176233Srwatson    		/*
901176233Srwatson		 * Enter the new vnode in the namecache only if the top bit
902176233Srwatson		 * isn't set.
903176233Srwatson		 *
904176233Srwatson		 * And don't enter a new vnode for an invalid one!
905176139Srwatson		 */
906176233Srwatson		if (!(vtype & CODA_NOCACHE) && (cnp->cn_flags & MAKEENTRY))
907176233Srwatson			cache_enter(dvp, *vpp, cnp);
90838625Srvb	}
909176139Srwatsonexit:
910176139Srwatson	/*
911176139Srwatson	 * If we are creating, and this was the last name to be looked up,
912176139Srwatson	 * and the error was ENOENT, then there really shouldn't be an error
913176139Srwatson	 * and we can make the leaf NULL and return success.  Since this is
914176139Srwatson	 * supposed to work under Mach as well as FreeBSD, we're leaving this
915176139Srwatson	 * fn wrapped.  We also must tell lookup/namei that we need to save
916176139Srwatson	 * the last component of the name.  (Create will have to free the
917176139Srwatson	 * name buffer later...lucky us...).
918176139Srwatson	 */
919176139Srwatson	if (((cnp->cn_nameiop == CREATE) || (cnp->cn_nameiop == RENAME))
920176139Srwatson	    && (cnp->cn_flags & ISLASTCN) && (error == ENOENT)) {
921176139Srwatson		error = EJUSTRETURN;
922176139Srwatson		cnp->cn_flags |= SAVENAME;
923176139Srwatson		*ap->a_vpp = NULL;
924176139Srwatson	}
92538625Srvb
926176139Srwatson	/*
927176139Srwatson	 * If we are removing, and we are at the last element, and we found
928176139Srwatson	 * it, then we need to keep the name around so that the removal will
929176139Srwatson	 * go ahead as planned.  Unfortunately, this will probably also lock
930176139Srwatson	 * the to-be-removed vnode, which may or may not be a good idea.
931176139Srwatson	 * I'll have to look at the bits of coda_remove to make sure.  We'll
932176139Srwatson	 * only save the name if we did in fact find the name, otherwise
933176139Srwatson	 * coda_remove won't have a chance to free the pathname.
934176139Srwatson	 */
935176139Srwatson	if ((cnp->cn_nameiop == DELETE) && (cnp->cn_flags & ISLASTCN)
936176139Srwatson	    && !error)
937176139Srwatson		cnp->cn_flags |= SAVENAME;
93838625Srvb
939176139Srwatson	/*
940176139Srwatson	 * If the lookup went well, we need to (potentially?) unlock the
941176139Srwatson	 * parent, and lock the child.  We are only responsible for checking
942176139Srwatson	 * to see if the parent is supposed to be unlocked before we return.
943176139Srwatson	 * We must always lock the child (provided there is one, and (the
944176139Srwatson	 * parent isn't locked or it isn't the same as the parent.)  Simple,
945176139Srwatson	 * huh?  We can never leave the parent locked unless we are ISLASTCN.
946176139Srwatson	 */
947176139Srwatson	if (!error || (error == EJUSTRETURN)) {
948176139Srwatson		if (cnp->cn_flags & ISDOTDOT) {
949176139Srwatson			VOP_UNLOCK(dvp, 0);
950176139Srwatson			/*
951176139Srwatson			 * The parent is unlocked.  As long as there is a
952176139Srwatson			 * child, lock it without bothering to check anything
953176139Srwatson			 * else.
954176139Srwatson			 */
955176139Srwatson			if (*ap->a_vpp)
956176139Srwatson				vn_lock(*ap->a_vpp, LK_EXCLUSIVE | LK_RETRY);
957176139Srwatson			vn_lock(dvp, LK_RETRY|LK_EXCLUSIVE);
958176139Srwatson		} else {
959176139Srwatson			/*
960176139Srwatson			 * The parent is locked, and may be the same as the
961176139Srwatson			 * child.  If different, go ahead and lock it.
962176139Srwatson			 */
963176139Srwatson			if (*ap->a_vpp && (*ap->a_vpp != dvp))
964176139Srwatson				vn_lock(*ap->a_vpp, LK_EXCLUSIVE | LK_RETRY);
965176139Srwatson		}
96638625Srvb	} else {
967176139Srwatson		/*
968176139Srwatson		 * If the lookup failed, we need to ensure that the leaf is
969176139Srwatson		 * NULL.
970176139Srwatson		 *
971176139Srwatson		 * Don't change any locking?
972176139Srwatson		 */
973176139Srwatson		*ap->a_vpp = NULL;
97438625Srvb	}
975176139Srwatson	return (error);
97638625Srvb}
97738625Srvb
97838625Srvb/*ARGSUSED*/
97938625Srvbint
980138290Sphkcoda_create(struct vop_create_args *ap)
98138625Srvb{
982176139Srwatson	/* true args */
983176139Srwatson	struct vnode *dvp = ap->a_dvp;
984176139Srwatson	struct cnode *dcp = VTOC(dvp);
985176139Srwatson	struct vattr *va = ap->a_vap;
986176139Srwatson	int exclusive = 1;
987176139Srwatson	int mode = ap->a_vap->va_mode;
988176139Srwatson	struct vnode **vpp = ap->a_vpp;
989176139Srwatson	struct componentname  *cnp = ap->a_cnp;
990176139Srwatson	struct ucred *cred = cnp->cn_cred;
991176139Srwatson	struct thread *td = cnp->cn_thread;
992176139Srwatson	/* locals */
993176139Srwatson	int error;
994176139Srwatson	struct cnode *cp;
995176139Srwatson	const char *nm = cnp->cn_nameptr;
996176139Srwatson	int len = cnp->cn_namelen;
997176139Srwatson	CodaFid VFid;
998176139Srwatson	struct vattr attr;
99938625Srvb
1000176139Srwatson	MARK_ENTRY(CODA_CREATE_STATS);
100138625Srvb
1002176139Srwatson	/*
1003176139Srwatson	 * All creates are exclusive XXX.
1004176139Srwatson	 *
1005176139Srwatson	 * I'm assuming the 'mode' argument is the file mode bits XXX.
1006176139Srwatson	 *
1007176139Srwatson	 * Check for create of control object.
1008176139Srwatson	 */
1009176139Srwatson	if (IS_CTL_NAME(dvp, nm, len)) {
1010176139Srwatson		*vpp = (struct vnode *)0;
1011176139Srwatson		MARK_INT_FAIL(CODA_CREATE_STATS);
1012176139Srwatson		return (EACCES);
1013176139Srwatson	}
1014176139Srwatson	error = venus_create(vtomi(dvp), &dcp->c_fid, nm, len, exclusive,
1015176139Srwatson	    mode, va, cred, td->td_proc, &VFid, &attr);
1016176139Srwatson	if (!error) {
1017176139Srwatson		/*
1018176139Srwatson		 * If this is an exclusive create, panic if the file already
1019176139Srwatson		 * exists.
1020176139Srwatson		 *
1021176139Srwatson		 * Venus should have detected the file and reported EEXIST.
1022176139Srwatson		 */
1023176139Srwatson		if ((exclusive == 1) && (coda_find(&VFid) != NULL))
1024176139Srwatson	  	  	panic("cnode existed for newly created file!");
1025176139Srwatson		cp = make_coda_node(&VFid, dvp->v_mount, attr.va_type);
1026176139Srwatson		*vpp = CTOV(cp);
102738625Srvb
1028176139Srwatson		/*
1029176139Srwatson		 * Update va to reflect the new attributes.
1030176139Srwatson		 */
1031176139Srwatson		(*va) = attr;
103238625Srvb
1033176139Srwatson		/*
1034176139Srwatson		 * Update the attribute cache and mark it as valid.
1035176139Srwatson		 */
1036176139Srwatson		if (coda_attr_cache) {
1037176139Srwatson			VTOC(*vpp)->c_vattr = attr;
1038176139Srwatson			VTOC(*vpp)->c_flags |= C_VATTR;
1039176139Srwatson		}
104038625Srvb
1041176139Srwatson		/*
1042176139Srwatson		 * Invalidate the parent's attr cache, the modification time
1043176139Srwatson		 * has changed.
1044176139Srwatson		 */
1045176139Srwatson		VTOC(dvp)->c_flags &= ~C_VATTR;
1046176233Srwatson		cache_enter(dvp, *vpp, cnp);
1047176139Srwatson		CODADEBUG(CODA_CREATE, myprintf(("create: %s, result %d\n",
1048176139Srwatson		    coda_f2s(&VFid), error)););
1049176139Srwatson	} else {
1050176139Srwatson		*vpp = (struct vnode *)0;
1051176139Srwatson		CODADEBUG(CODA_CREATE, myprintf(("create error %d\n",
1052176139Srwatson		    error)););
105338625Srvb	}
1054176139Srwatson	if (!error) {
1055176233Srwatson		if (cnp->cn_flags & MAKEENTRY)
1056176233Srwatson			cache_enter(dvp, *vpp, cnp);
1057176139Srwatson		if (cnp->cn_flags & LOCKLEAF)
1058176139Srwatson			vn_lock(*ap->a_vpp, LK_EXCLUSIVE | LK_RETRY);
105939650Srvb#ifdef OLD_DIAGNOSTIC
1060176139Srwatson		else
1061176139Srwatson			printf("coda_create: LOCKLEAF not set!\n");
1062176139Srwatson#endif
1063176233Srwatson	} else if (error == ENOENT) {
1064176233Srwatson		/*
1065176233Srwatson		 * XXXRW: We only enter a negative entry if ENOENT is
1066176233Srwatson		 * returned, not other errors.  But will Venus invalidate dvp
1067176233Srwatson		 * properly in all cases when new files appear via the
1068176233Srwatson		 * network rather than a local operation?
1069176233Srwatson		 */
1070176233Srwatson		if (cnp->cn_flags & MAKEENTRY)
1071176233Srwatson			cache_enter(dvp, NULL, cnp);
107238625Srvb	}
1073176139Srwatson	return (error);
107438625Srvb}
107538625Srvb
107638625Srvbint
1077138290Sphkcoda_remove(struct vop_remove_args *ap)
107838625Srvb{
1079176139Srwatson	/* true args */
1080176233Srwatson	struct vnode *vp = ap->a_vp;
1081176139Srwatson	struct vnode *dvp = ap->a_dvp;
1082176139Srwatson	struct cnode *cp = VTOC(dvp);
1083176139Srwatson	struct componentname  *cnp = ap->a_cnp;
1084176139Srwatson	struct ucred *cred = cnp->cn_cred;
1085176139Srwatson	struct thread *td = cnp->cn_thread;
1086176139Srwatson	/* locals */
1087176139Srwatson	int error;
1088176139Srwatson	const char *nm = cnp->cn_nameptr;
1089176139Srwatson	int len = cnp->cn_namelen;
1090176233Srwatson#if 0
1091176139Srwatson	struct cnode *tp;
1092176233Srwatson#endif
109338625Srvb
1094176139Srwatson	MARK_ENTRY(CODA_REMOVE_STATS);
1095176139Srwatson	CODADEBUG(CODA_REMOVE, myprintf(("remove: %s in %s\n", nm,
1096176139Srwatson	    coda_f2s(&cp->c_fid))););
109738625Srvb
1098176139Srwatson	/*
1099176233Srwatson	 * Check for remove of control object.
1100176139Srwatson	 */
1101176233Srwatson	if (IS_CTL_NAME(dvp, nm, len)) {
1102176233Srwatson		MARK_INT_FAIL(CODA_REMOVE_STATS);
1103176233Srwatson		return (ENOENT);
110438625Srvb	}
110538625Srvb
1106176139Srwatson	/*
1107176139Srwatson	 * Invalidate the parent's attr cache, the modification time has
1108176233Srwatson	 * changed.  We don't yet know if the last reference to the file is
1109176233Srwatson	 * being removed, but we do know the reference count on the child has
1110176233Srwatson	 * changed, so invalidate its attr cache also.
1111176139Srwatson	 */
1112176139Srwatson	VTOC(dvp)->c_flags &= ~C_VATTR;
1113176233Srwatson	VTOC(vp)->c_flags &= ~C_VATTR;
1114176139Srwatson	error = venus_remove(vtomi(dvp), &cp->c_fid, nm, len, cred,
1115176139Srwatson	    td->td_proc);
1116176233Srwatson	cache_purge(vp);
1117176139Srwatson	CODADEBUG(CODA_REMOVE, myprintf(("in remove result %d\n",error)););
1118176139Srwatson	return (error);
111938625Srvb}
112038625Srvb
112138625Srvbint
1122138290Sphkcoda_link(struct vop_link_args *ap)
112338625Srvb{
1124176139Srwatson	/* true args */
1125176139Srwatson	struct vnode *vp = ap->a_vp;
1126176139Srwatson	struct cnode *cp = VTOC(vp);
1127176139Srwatson	struct vnode *tdvp = ap->a_tdvp;
1128176139Srwatson	struct cnode *tdcp = VTOC(tdvp);
1129176139Srwatson	struct componentname *cnp = ap->a_cnp;
1130176139Srwatson	struct ucred *cred = cnp->cn_cred;
1131176139Srwatson	struct thread *td = cnp->cn_thread;
1132176139Srwatson	/* locals */
1133176139Srwatson	int error;
1134176139Srwatson	const char *nm = cnp->cn_nameptr;
1135176139Srwatson	int len = cnp->cn_namelen;
113638625Srvb
1137176139Srwatson	MARK_ENTRY(CODA_LINK_STATS);
113838625Srvb
1139176139Srwatson	if (codadebug & CODADBGMSK(CODA_LINK)) {
1140176139Srwatson		myprintf(("nb_link:   vp fid: %s\n", coda_f2s(&cp->c_fid)));
1141176139Srwatson		myprintf(("nb_link: tdvp fid: %s)\n",
1142176139Srwatson		    coda_f2s(&tdcp->c_fid)));
1143176139Srwatson	}
1144176139Srwatson	if (codadebug & CODADBGMSK(CODA_LINK)) {
1145176139Srwatson		myprintf(("link:   vp fid: %s\n", coda_f2s(&cp->c_fid)));
1146176139Srwatson		myprintf(("link: tdvp fid: %s\n", coda_f2s(&tdcp->c_fid)));
1147176139Srwatson	}
114838625Srvb
1149176139Srwatson	/*
1150176139Srwatson	 * Check for link to/from control object.
1151176139Srwatson	 */
1152176139Srwatson	if (IS_CTL_NAME(tdvp, nm, len) || IS_CTL_VP(vp)) {
1153176139Srwatson		MARK_INT_FAIL(CODA_LINK_STATS);
1154176139Srwatson		return (EACCES);
1155176139Srwatson	}
1156176139Srwatson	error = venus_link(vtomi(vp), &cp->c_fid, &tdcp->c_fid, nm, len,
1157176139Srwatson	    cred, td->td_proc);
115838625Srvb
1159176139Srwatson	/*
1160176139Srwatson	 * Invalidate the parent's attr cache, the modification time has
1161176139Srwatson	 * changed.
1162176139Srwatson	 */
1163176139Srwatson	VTOC(tdvp)->c_flags &= ~C_VATTR;
1164176139Srwatson	VTOC(vp)->c_flags &= ~C_VATTR;
1165176139Srwatson	CODADEBUG(CODA_LINK, myprintf(("in link result %d\n",error)););
1166176139Srwatson	return (error);
116738625Srvb}
116838625Srvb
116938625Srvbint
1170138290Sphkcoda_rename(struct vop_rename_args *ap)
117138625Srvb{
1172176139Srwatson	/* true args */
1173176139Srwatson	struct vnode *odvp = ap->a_fdvp;
1174176139Srwatson	struct cnode *odcp = VTOC(odvp);
1175176139Srwatson	struct componentname  *fcnp = ap->a_fcnp;
1176176139Srwatson	struct vnode *ndvp = ap->a_tdvp;
1177176139Srwatson	struct cnode *ndcp = VTOC(ndvp);
1178176139Srwatson	struct componentname  *tcnp = ap->a_tcnp;
1179176139Srwatson	struct ucred *cred = fcnp->cn_cred;
1180176139Srwatson	struct thread *td = fcnp->cn_thread;
1181176139Srwatson	/* true args */
1182176139Srwatson	int error;
1183176139Srwatson	const char *fnm = fcnp->cn_nameptr;
1184176139Srwatson	int flen = fcnp->cn_namelen;
1185176139Srwatson	const char *tnm = tcnp->cn_nameptr;
1186176139Srwatson	int tlen = tcnp->cn_namelen;
118738625Srvb
1188176139Srwatson	MARK_ENTRY(CODA_RENAME_STATS);
118938625Srvb
1190176139Srwatson	/*
1191176139Srwatson	 * Hmmm.  The vnodes are already looked up.  Perhaps they are locked?
1192176139Srwatson	 * This could be Bad. XXX
1193176139Srwatson	 */
119439650Srvb#ifdef OLD_DIAGNOSTIC
1195176139Srwatson	if ((fcnp->cn_cred != tcnp->cn_cred) ||
1196176139Srwatson	    (fcnp->cn_thread != tcnp->cn_thread))
1197176139Srwatson		panic("coda_rename: component names don't agree");
119839650Srvb#endif
119938625Srvb
1200176139Srwatson	/*
1201176139Srwatson	 * Check for rename involving control object.
1202176139Srwatson	 */
1203176139Srwatson	if (IS_CTL_NAME(odvp, fnm, flen) || IS_CTL_NAME(ndvp, tnm, tlen)) {
1204176139Srwatson		MARK_INT_FAIL(CODA_RENAME_STATS);
1205176139Srwatson		return (EACCES);
120638625Srvb	}
120738625Srvb
1208176139Srwatson	/*
1209176233Srwatson	 * Remove the entries for both source and target directories, which
1210176233Srwatson	 * should catch references to the children.  Perhaps we could purge
1211176233Srwatson	 * less?
1212176139Srwatson	 */
1213176233Srwatson	cache_purge(odvp);
1214176233Srwatson	cache_purge(ndvp);
121538625Srvb
1216176139Srwatson	/*
1217176139Srwatson	 * Invalidate the parent's attr cache, the modification time has
1218176139Srwatson	 * changed.
1219176139Srwatson	 */
1220176139Srwatson	VTOC(odvp)->c_flags &= ~C_VATTR;
1221176139Srwatson	VTOC(ndvp)->c_flags &= ~C_VATTR;
1222176139Srwatson	if (flen+1 > CODA_MAXNAMLEN) {
1223176139Srwatson		MARK_INT_FAIL(CODA_RENAME_STATS);
1224176139Srwatson		error = EINVAL;
1225176139Srwatson		goto exit;
1226176139Srwatson	}
1227176139Srwatson	if (tlen+1 > CODA_MAXNAMLEN) {
1228176139Srwatson		MARK_INT_FAIL(CODA_RENAME_STATS);
1229176139Srwatson		error = EINVAL;
1230176139Srwatson		goto exit;
1231176139Srwatson	}
1232176139Srwatson	error = venus_rename(vtomi(odvp), &odcp->c_fid, &ndcp->c_fid, fnm,
1233176139Srwatson	    flen, tnm, tlen, cred, td->td_proc);
1234176139Srwatsonexit:
1235176139Srwatson	CODADEBUG(CODA_RENAME, myprintf(("in rename result %d\n",error)););
123638625Srvb
1237176139Srwatson	/*
1238176139Srwatson	 * XXX - do we need to call cache pureg on the moved vnode?
1239176139Srwatson	 */
1240176139Srwatson	cache_purge(ap->a_fvp);
124138625Srvb
1242176139Srwatson	/*
1243176139Srwatson	 * Release parents first, then children.
1244176139Srwatson	 */
1245176139Srwatson	vrele(odvp);
1246176139Srwatson	if (ap->a_tvp) {
1247176139Srwatson		if (ap->a_tvp == ndvp)
1248176139Srwatson			vrele(ndvp);
1249176139Srwatson		else
1250176139Srwatson			vput(ndvp);
1251176139Srwatson		vput(ap->a_tvp);
1252176139Srwatson	} else
1253176139Srwatson		vput(ndvp);
1254176139Srwatson	vrele(ap->a_fvp);
1255176139Srwatson	return (error);
125638625Srvb}
125738625Srvb
125838625Srvbint
1259138290Sphkcoda_mkdir(struct vop_mkdir_args *ap)
126038625Srvb{
1261176139Srwatson	/* true args */
1262176139Srwatson	struct vnode *dvp = ap->a_dvp;
1263176139Srwatson	struct cnode *dcp = VTOC(dvp);
1264176139Srwatson	struct componentname  *cnp = ap->a_cnp;
1265176139Srwatson	struct vattr *va = ap->a_vap;
1266176139Srwatson	struct vnode **vpp = ap->a_vpp;
1267176139Srwatson	struct ucred *cred = cnp->cn_cred;
1268176139Srwatson	struct thread *td = cnp->cn_thread;
1269176139Srwatson	/* locals */
1270176139Srwatson	int error;
1271176139Srwatson	const char *nm = cnp->cn_nameptr;
1272176139Srwatson	int len = cnp->cn_namelen;
1273176139Srwatson	struct cnode *cp;
1274176139Srwatson	CodaFid VFid;
1275176139Srwatson	struct vattr ova;
127638625Srvb
1277176139Srwatson	MARK_ENTRY(CODA_MKDIR_STATS);
127838625Srvb
1279176139Srwatson	/*
1280176139Srwatson	 * Check for mkdir of target object.
1281176139Srwatson	 */
1282176139Srwatson	if (IS_CTL_NAME(dvp, nm, len)) {
1283176139Srwatson		*vpp = (struct vnode *)0;
1284176139Srwatson		MARK_INT_FAIL(CODA_MKDIR_STATS);
1285176139Srwatson		return (EACCES);
128638625Srvb	}
1287176139Srwatson	if (len+1 > CODA_MAXNAMLEN) {
1288176139Srwatson		*vpp = (struct vnode *)0;
1289176139Srwatson		MARK_INT_FAIL(CODA_MKDIR_STATS);
1290176139Srwatson		return (EACCES);
1291176139Srwatson	}
1292176139Srwatson	error = venus_mkdir(vtomi(dvp), &dcp->c_fid, nm, len, va, cred,
1293176139Srwatson	    td->td_proc, &VFid, &ova);
1294176139Srwatson	if (!error) {
1295176139Srwatson		if (coda_find(&VFid) != NULL)
1296176139Srwatson			panic("cnode existed for newly created directory!");
1297176139Srwatson		cp =  make_coda_node(&VFid, dvp->v_mount, va->va_type);
1298176139Srwatson		*vpp = CTOV(cp);
129938625Srvb
1300176139Srwatson		/*
1301176139Srwatson		 * Enter the new vnode in the Name Cache.
1302176139Srwatson		 */
1303176233Srwatson		if (cnp->cn_flags & MAKEENTRY)
1304176233Srwatson			cache_enter(dvp, *vpp, cnp);
1305175474Srwatson
1306176139Srwatson		/*
1307176233Srwatson		 * Update the attr cache and mark as valid.
1308176139Srwatson		 */
1309176139Srwatson		if (coda_attr_cache) {
1310176139Srwatson			VTOC(*vpp)->c_vattr = ova;
1311176139Srwatson			VTOC(*vpp)->c_flags |= C_VATTR;
1312176139Srwatson		}
1313175474Srwatson
1314176139Srwatson		/*
1315176139Srwatson		 * Invalidate the parent's attr cache, the modification time
1316176139Srwatson		 * has changed.
1317176139Srwatson		 */
1318176139Srwatson		VTOC(dvp)->c_flags &= ~C_VATTR;
1319176139Srwatson		vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
1320176139Srwatson		CODADEBUG( CODA_MKDIR, myprintf(("mkdir: %s result %d\n",
1321176139Srwatson		    coda_f2s(&VFid), error)););
1322119832Stjr	} else {
1323176139Srwatson		*vpp = NULL;
1324176139Srwatson		CODADEBUG(CODA_MKDIR, myprintf(("mkdir error %d\n",error)););
1325176139Srwatson	}
1326176139Srwatson	return (error);
132738625Srvb}
132838625Srvb
132938625Srvbint
1330138290Sphkcoda_rmdir(struct vop_rmdir_args *ap)
133138625Srvb{
1332176139Srwatson	/* true args */
1333176233Srwatson	struct vnode *vp = ap->a_vp;
1334176139Srwatson	struct vnode *dvp = ap->a_dvp;
1335176139Srwatson	struct cnode *dcp = VTOC(dvp);
1336176139Srwatson	struct componentname  *cnp = ap->a_cnp;
1337176139Srwatson	struct ucred *cred = cnp->cn_cred;
1338176139Srwatson	struct thread *td = cnp->cn_thread;
1339176139Srwatson	/* true args */
1340176139Srwatson	int error;
1341176139Srwatson	const char *nm = cnp->cn_nameptr;
1342176139Srwatson	int len = cnp->cn_namelen;
1343176233Srwatson#if 0
1344176139Srwatson	struct cnode *cp;
1345176233Srwatson#endif
134638625Srvb
1347176139Srwatson	MARK_ENTRY(CODA_RMDIR_STATS);
134838625Srvb
1349176139Srwatson	/*
1350176139Srwatson	 * Check for rmdir of control object.
1351176139Srwatson	 */
1352176139Srwatson	if (IS_CTL_NAME(dvp, nm, len)) {
1353176139Srwatson		MARK_INT_FAIL(CODA_RMDIR_STATS);
1354176139Srwatson		return (ENOENT);
1355176139Srwatson	}
135638625Srvb
1357176139Srwatson	/*
1358176233Srwatson	 * Possibly somewhat conservative purging, perhaps we just need to
1359176233Srwatson	 * purge vp?
1360176139Srwatson	 */
1361176233Srwatson	cache_purge(dvp);
1362176233Srwatson	cache_purge(vp);
136338625Srvb
1364176139Srwatson	/*
1365176139Srwatson	 * Invalidate the parent's attr cache, the modification time has
1366176139Srwatson	 * changed.
1367176139Srwatson	 */
1368176139Srwatson	dcp->c_flags &= ~C_VATTR;
1369176139Srwatson	error = venus_rmdir(vtomi(dvp), &dcp->c_fid, nm, len, cred,
1370176139Srwatson	    td->td_proc);
1371176139Srwatson	CODADEBUG(CODA_RMDIR, myprintf(("in rmdir result %d\n", error)););
1372176139Srwatson	return (error);
137338625Srvb}
137438625Srvb
137538625Srvbint
1376138290Sphkcoda_symlink(struct vop_symlink_args *ap)
137738625Srvb{
1378176139Srwatson	/* true args */
1379176139Srwatson	struct vnode *tdvp = ap->a_dvp;
1380176139Srwatson	struct cnode *tdcp = VTOC(tdvp);
1381176139Srwatson	struct componentname *cnp = ap->a_cnp;
1382176139Srwatson	struct vattr *tva = ap->a_vap;
1383176139Srwatson	char *path = ap->a_target;
1384176139Srwatson	struct ucred *cred = cnp->cn_cred;
1385176139Srwatson	struct thread *td = cnp->cn_thread;
1386176139Srwatson	struct vnode **vpp = ap->a_vpp;
1387176139Srwatson	/* locals */
1388176139Srwatson	int error;
138938625Srvb
1390176139Srwatson	/*-
1391176139Srwatson	 * XXX I'm assuming the following things about coda_symlink's
1392176139Srwatson	 * arguments:
1393176139Srwatson	 *       t(foo) is the new name/parent/etc being created.
1394176139Srwatson	 *       lname is the contents of the new symlink.
1395176139Srwatson	 */
1396176139Srwatson	char *nm = cnp->cn_nameptr;
1397176139Srwatson	int len = cnp->cn_namelen;
1398176139Srwatson	int plen = strlen(path);
139938625Srvb
1400176139Srwatson	/*
1401176139Srwatson	 * Here's the strategy for the moment: perform the symlink, then do a
1402176139Srwatson	 * lookup to grab the resulting vnode.  I know this requires two
1403176139Srwatson	 * communications with Venus for a new sybolic link, but that's the
1404176139Srwatson	 * way the ball bounces.  I don't yet want to change the way the Mach
1405176139Srwatson	 * symlink works.  When Mach support is deprecated, we should change
1406176139Srwatson	 * symlink so that the common case returns the resultant vnode in a
1407176139Srwatson	 * vpp argument.
1408176139Srwatson	 */
1409176139Srwatson	MARK_ENTRY(CODA_SYMLINK_STATS);
141038625Srvb
1411176139Srwatson	/*
1412176139Srwatson	 * Check for symlink of control object.
1413176139Srwatson	 */
1414176139Srwatson	if (IS_CTL_NAME(tdvp, nm, len)) {
1415176139Srwatson		MARK_INT_FAIL(CODA_SYMLINK_STATS);
1416176139Srwatson		return (EACCES);
1417176139Srwatson	}
1418176139Srwatson	if (plen+1 > CODA_MAXPATHLEN) {
1419176139Srwatson		MARK_INT_FAIL(CODA_SYMLINK_STATS);
1420176139Srwatson		return (EINVAL);
1421176139Srwatson	}
1422176139Srwatson	if (len+1 > CODA_MAXNAMLEN) {
1423176139Srwatson		MARK_INT_FAIL(CODA_SYMLINK_STATS);
1424176139Srwatson		error = EINVAL;
1425176139Srwatson		goto exit;
1426176139Srwatson	}
1427176139Srwatson	error = venus_symlink(vtomi(tdvp), &tdcp->c_fid, path, plen, nm, len,
1428176139Srwatson	    tva, cred, td->td_proc);
142938625Srvb
1430176139Srwatson	/*
1431176139Srwatson	 * Invalidate the parent's attr cache, the modification time has
1432176139Srwatson	 * changed.
1433176139Srwatson	 */
1434176139Srwatson	tdcp->c_flags &= ~C_VATTR;
1435176139Srwatson	if (error == 0)
1436176139Srwatson		error = VOP_LOOKUP(tdvp, vpp, cnp);
1437176139Srwatsonexit:
1438176139Srwatson	CODADEBUG(CODA_SYMLINK, myprintf(("in symlink result %d\n",error)););
1439176139Srwatson	return (error);
144038625Srvb}
144138625Srvb
144238625Srvb/*
144338625Srvb * Read directory entries.
1444176131Srwatson *
1445176139Srwatson * XXX: This forwards the operator straight to the cache vnode using
1446176131Srwatson * VOP_READDIR(), rather than calling venus_readdir().  Why?
144738625Srvb */
144838625Srvbint
1449138290Sphkcoda_readdir(struct vop_readdir_args *ap)
145038625Srvb{
1451176139Srwatson	/* true args */
1452176139Srwatson	struct vnode *vp = ap->a_vp;
1453176139Srwatson	struct cnode *cp = VTOC(vp);
1454176139Srwatson	struct uio *uiop = ap->a_uio;
1455176139Srwatson	struct ucred *cred = ap->a_cred;
1456176139Srwatson	int *eofflag = ap->a_eofflag;
1457176139Srwatson	u_long **cookies = ap->a_cookies;
1458176139Srwatson	int *ncookies = ap->a_ncookies;
1459176139Srwatson	struct thread *td = ap->a_uio->uio_td;
1460176139Srwatson	/* upcall decl */
1461176139Srwatson	/* locals */
1462176139Srwatson	int error = 0;
1463176139Srwatson	int opened_internally = 0;
146438625Srvb
1465176139Srwatson	MARK_ENTRY(CODA_READDIR_STATS);
1466176139Srwatson	CODADEBUG(CODA_READDIR, myprintf(("coda_readdir(%p, %d, %lld, %d)\n",
1467176139Srwatson	    (void *)uiop->uio_iov->iov_base, uiop->uio_resid,
1468176139Srwatson	    (long long)uiop->uio_offset, uiop->uio_segflg)););
146938625Srvb
1470176139Srwatson	/*
1471176139Srwatson	 * Check for readdir of control object.
1472176139Srwatson	 */
1473176139Srwatson	if (IS_CTL_VP(vp)) {
1474176139Srwatson		MARK_INT_FAIL(CODA_READDIR_STATS);
1475176139Srwatson		return (ENOENT);
1476176139Srwatson	}
147738625Srvb
1478176139Srwatson	/*
1479176139Srwatson	 * If directory is not already open do an "internal open" on it.
1480176139Srwatson	 *
1481176139Srwatson	 * XXX: Why would this happen?  For files, there's memory mapping,
1482176139Srwatson	 * execution, and other kernel access paths such as ktrace.  For
1483176139Srwatson	 * directories, it is less clear.
1484176139Srwatson	 */
148538625Srvb	if (cp->c_ovp == NULL) {
1486176139Srwatson		opened_internally = 1;
1487176139Srwatson		MARK_INT_GEN(CODA_OPEN_STATS);
1488176139Srwatson		error = VOP_OPEN(vp, FREAD, cred, td, NULL);
1489176139Srwatson		printf("coda_readdir: Internally Opening %p\n", vp);
1490176139Srwatson		if (error) {
1491176139Srwatson			printf("coda_readdir: VOP_OPEN on container failed "
1492176139Srwatson			   "%d\n", error);
1493176139Srwatson			return (error);
1494176139Srwatson		}
149538625Srvb	}
1496176139Srwatson
1497176139Srwatson	/*
1498176139Srwatson	 * Have UFS handle the call.
1499176139Srwatson	 */
1500176139Srwatson	CODADEBUG(CODA_READDIR, myprintf(("indirect readdir: fid = %s, "
1501176139Srwatson	    "refcnt = %d\n", coda_f2s(&cp->c_fid), vp->v_usecount)););
1502176121Srwatson	vn_lock(cp->c_ovp, LK_EXCLUSIVE | LK_RETRY);
150338625Srvb	error = VOP_READDIR(cp->c_ovp, uiop, cred, eofflag, ncookies,
1504176139Srwatson	    cookies);
1505176118Srwatson	VOP_UNLOCK(cp->c_ovp, 0);
150638625Srvb	if (error)
1507176139Srwatson		MARK_INT_FAIL(CODA_READDIR_STATS);
150838625Srvb	else
1509176139Srwatson		MARK_INT_SAT(CODA_READDIR_STATS);
1510176139Srwatson
1511176139Srwatson	/*
1512176139Srwatson	 * Do an "internal close" if necessary.
1513176139Srwatson	 */
151438625Srvb	if (opened_internally) {
1515176139Srwatson		MARK_INT_GEN(CODA_CLOSE_STATS);
1516176139Srwatson		(void)VOP_CLOSE(vp, FREAD, cred, td);
151738625Srvb	}
1518176139Srwatson	return (error);
151938625Srvb}
152038625Srvb
152138625Srvbint
1522138290Sphkcoda_reclaim(struct vop_reclaim_args *ap)
152338625Srvb{
1524176139Srwatson	/* true args */
1525176139Srwatson	struct vnode *vp = ap->a_vp;
1526176139Srwatson	struct cnode *cp = VTOC(vp);
1527176139Srwatson	/* upcall decl */
1528176139Srwatson	/* locals */
152938625Srvb
1530176139Srwatson	/*
1531176139Srwatson	 * Forced unmount/flush will let vnodes with non-zero use be
1532176139Srwatson	 * destroyed!
1533176139Srwatson	 */
1534176139Srwatson	ENTRY;
153538625Srvb
1536176139Srwatson	if (IS_UNMOUNTING(cp)) {
153738625Srvb#ifdef	DEBUG
1538176139Srwatson		if (VTOC(vp)->c_ovp) {
1539176139Srwatson			if (IS_UNMOUNTING(cp))
1540176139Srwatson				printf("coda_reclaim: c_ovp not void: vp "
1541176139Srwatson				    "%p, cp %p\n", vp, cp);
1542176139Srwatson		}
154338625Srvb#endif
1544176139Srwatson	} else {
154539650Srvb#ifdef OLD_DIAGNOSTIC
1546176139Srwatson		if (vrefcnt(vp) != 0)
1547176139Srwatson			print("coda_reclaim: pushing active %p\n", vp);
1548176139Srwatson		if (VTOC(vp)->c_ovp)
1549176139Srwatson			panic("coda_reclaim: c_ovp not void");
155039650Srvb#endif
1551176139Srwatson	}
1552176139Srwatson	cache_purge(vp);
1553176139Srwatson	coda_free(VTOC(vp));
1554176139Srwatson	vp->v_data = NULL;
1555176139Srwatson	vp->v_object = NULL;
1556176139Srwatson	return (0);
155738625Srvb}
155838625Srvb
155938625Srvbint
1560169671Skibcoda_lock(struct vop_lock1_args *ap)
156138625Srvb{
1562176139Srwatson	/* true args */
1563176139Srwatson	struct vnode *vp = ap->a_vp;
1564176139Srwatson	struct cnode *cp = VTOC(vp);
1565176139Srwatson	/* upcall decl */
1566176139Srwatson	/* locals */
156738625Srvb
1568176139Srwatson	ENTRY;
1569176139Srwatson	if ((ap->a_flags & LK_INTERLOCK) == 0) {
1570176139Srwatson		VI_LOCK(vp);
1571176139Srwatson		ap->a_flags |= LK_INTERLOCK;
1572176139Srwatson	}
1573176139Srwatson	if (coda_lockdebug)
1574176139Srwatson		myprintf(("Attempting lock on %s\n", coda_f2s(&cp->c_fid)));
1575176139Srwatson	return (vop_stdlock(ap));
157638625Srvb}
157738625Srvb
157838625Srvbint
1579138290Sphkcoda_unlock(struct vop_unlock_args *ap)
158038625Srvb{
1581176139Srwatson	/* true args */
1582176139Srwatson	struct vnode *vp = ap->a_vp;
1583176139Srwatson	struct cnode *cp = VTOC(vp);
1584176139Srwatson	/* upcall decl */
1585176139Srwatson	/* locals */
158638625Srvb
1587176139Srwatson	ENTRY;
1588176139Srwatson	if (coda_lockdebug)
1589176139Srwatson		myprintf(("Attempting unlock on %s\n",
1590176139Srwatson		    coda_f2s(&cp->c_fid)));
1591176139Srwatson	return (vop_stdunlock(ap));
159238625Srvb}
159338625Srvb
159438625Srvbint
1595138290Sphkcoda_islocked(struct vop_islocked_args *ap)
159638625Srvb{
1597176139Srwatson	/* true args */
159838625Srvb
1599176139Srwatson	ENTRY;
1600176139Srwatson	return (vop_stdislocked(ap));
160138625Srvb}
160238625Srvb
1603176131Srwatsonstatic void
1604176131Srwatsoncoda_print_vattr(struct vattr *attr)
160538625Srvb{
1606176139Srwatson	char *typestr;
160738625Srvb
1608176139Srwatson	switch (attr->va_type) {
1609176139Srwatson	case VNON:
1610176139Srwatson		typestr = "VNON";
1611176139Srwatson		break;
161238625Srvb
1613176139Srwatson	case VREG:
1614176139Srwatson		typestr = "VREG";
1615176139Srwatson		break;
161638625Srvb
1617176139Srwatson	case VDIR:
1618176139Srwatson		typestr = "VDIR";
1619176139Srwatson		break;
162038625Srvb
1621176139Srwatson	case VBLK:
1622176139Srwatson		typestr = "VBLK";
1623176139Srwatson		break;
1624176139Srwatson
1625176139Srwatson	case VCHR:
1626176139Srwatson		typestr = "VCHR";
1627176139Srwatson		break;
1628176139Srwatson
1629176139Srwatson	case VLNK:
1630176139Srwatson		typestr = "VLNK";
1631176139Srwatson		break;
1632176139Srwatson
1633176139Srwatson	case VSOCK:
1634176139Srwatson		typestr = "VSCK";
1635176139Srwatson		break;
1636176139Srwatson
1637176139Srwatson	case VFIFO:
1638176139Srwatson		typestr = "VFFO";
1639176139Srwatson		break;
1640176139Srwatson
1641176139Srwatson	case VBAD:
1642176139Srwatson		typestr = "VBAD";
1643176139Srwatson		break;
1644176139Srwatson
1645176139Srwatson	default:
1646176139Srwatson		typestr = "????";
1647176139Srwatson		break;
1648176139Srwatson	}
1649176139Srwatson	myprintf(("attr: type %s mode %d uid %d gid %d fsid %d rdev %d\n",
1650176139Srwatson	    typestr, (int)attr->va_mode, (int)attr->va_uid,
1651176139Srwatson	    (int)attr->va_gid, (int)attr->va_fsid, (int)attr->va_rdev));
1652176139Srwatson	myprintf(("      fileid %d nlink %d size %d blocksize %d bytes %d\n",
1653176139Srwatson	    (int)attr->va_fileid, (int)attr->va_nlink, (int)attr->va_size,
1654176139Srwatson	    (int)attr->va_blocksize,(int)attr->va_bytes));
1655176139Srwatson	myprintf(("      gen %ld flags %ld vaflags %d\n", attr->va_gen,
1656176139Srwatson	    attr->va_flags, attr->va_vaflags));
1657176139Srwatson	myprintf(("      atime sec %d nsec %d\n", (int)attr->va_atime.tv_sec,
1658176139Srwatson	    (int)attr->va_atime.tv_nsec));
1659176139Srwatson	myprintf(("      mtime sec %d nsec %d\n", (int)attr->va_mtime.tv_sec,
1660176139Srwatson	    (int)attr->va_mtime.tv_nsec));
1661176139Srwatson	myprintf(("      ctime sec %d nsec %d\n", (int)attr->va_ctime.tv_sec,
1662176139Srwatson	    (int)attr->va_ctime.tv_nsec));
166338625Srvb}
166438625Srvb
1665176139Srwatson/*
1666176139Srwatson * How to print a ucred.
1667176139Srwatson */
166838625Srvbvoid
1669176131Srwatsoncoda_print_cred(struct ucred *cred)
167038625Srvb{
167138625Srvb	int i;
167238625Srvb
167338625Srvb	myprintf(("ref %d\tuid %d\n",cred->cr_ref,cred->cr_uid));
167438625Srvb	for (i=0; i < cred->cr_ngroups; i++)
167538625Srvb		myprintf(("\tgroup %d: (%d)\n",i,cred->cr_groups[i]));
167638625Srvb	myprintf(("\n"));
167738625Srvb}
167838625Srvb
167938625Srvb/*
1680176139Srwatson * Return a vnode for the given fid.  If no cnode exists for this fid create
1681176139Srwatson * one and put it in a table hashed by coda_f2i().  If the cnode for this fid
1682176139Srwatson * is already in the table return it (ref count is incremented by coda_find.
1683176139Srwatson * The cnode will be flushed from the table when coda_inactive calls
1684176139Srwatson * coda_unsave.
168538625Srvb */
168638625Srvbstruct cnode *
1687154647Srwatsonmake_coda_node(CodaFid *fid, struct mount *vfsp, short type)
168838625Srvb{
1689176139Srwatson	struct cnode *cp;
1690176139Srwatson	int err;
169138625Srvb
1692176139Srwatson	if ((cp = coda_find(fid)) == NULL) {
1693176139Srwatson		struct vnode *vp;
169438625Srvb
1695176139Srwatson		cp = coda_alloc();
1696176139Srwatson		cp->c_fid = *fid;
1697176139Srwatson		err = getnewvnode("coda", vfsp, &coda_vnodeops, &vp);
1698176139Srwatson		if (err)
1699176139Srwatson			panic("coda: getnewvnode returned error %d\n", err);
1700176139Srwatson
1701176139Srwatson		/*
1702176139Srwatson		 * XXX: Too early for mpsafe fs.
1703176139Srwatson		 */
1704176139Srwatson		err = insmntque1(vp, vfsp, NULL, NULL);
1705176139Srwatson		if (err != 0)
1706176139Srwatson			panic("coda: insmntque failed: error %d", err);
1707176139Srwatson		vp->v_data = cp;
1708176139Srwatson		vp->v_type = type;
1709176139Srwatson		cp->c_vnode = vp;
1710176139Srwatson		coda_save(cp);
1711176139Srwatson	} else
1712176139Srwatson		vref(CTOV(cp));
1713176139Srwatson	return (cp);
171438625Srvb}
1715111903Stjr
1716111903Stjrint
1717154647Srwatsoncoda_pathconf(struct vop_pathconf_args *ap)
1718111903Stjr{
1719111903Stjr
1720111903Stjr	switch (ap->a_name) {
1721111903Stjr	case _PC_NAME_MAX:
1722176156Srwatson		*ap->a_retval = CODA_MAXNAMLEN;
1723176156Srwatson		return (0);
1724176156Srwatson
1725111903Stjr	case _PC_PATH_MAX:
1726176156Srwatson		*ap->a_retval = CODA_MAXPATHLEN;
1727176156Srwatson		return (0);
1728176156Srwatson
1729111903Stjr	default:
1730176156Srwatson		return (vop_stdpathconf(ap));
1731111903Stjr	}
1732111903Stjr}
1733