coda_vnops.c revision 188588
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 188588 2009-02-13 18:18:14Z jhb $");
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. */
80176139Srwatsonstatic int coda_access_cache = 1;	/* Set to cache some access checks. */
8138625Srvb
82176139Srwatson/*
83176139Srwatson * Structure to keep track of vfs calls.
84176139Srwatson */
85176139Srwatsonstatic struct coda_op_stats coda_vnodeopstats[CODA_VNODEOPS_SIZE];
8638625Srvb
87176139Srwatson#define	MARK_ENTRY(op)		(coda_vnodeopstats[op].entries++)
88176139Srwatson#define	MARK_INT_SAT(op)	(coda_vnodeopstats[op].sat_intrn++)
89176139Srwatson#define	MARK_INT_FAIL(op)	(coda_vnodeopstats[op].unsat_intrn++)
90176139Srwatson#define	MARK_INT_GEN(op)	(coda_vnodeopstats[op].gen_intrn++)
9138625Srvb
92176139Srwatson/*
93176139Srwatson * What we are delaying for in printf.
94176139Srwatson */
95176139Srwatsonint coda_printf_delay = 0;	/* In microseconds */
9639085Srvbint coda_vnop_print_entry = 0;
9739085Srvbstatic int coda_lockdebug = 0;
9838625Srvb
9938625Srvb/*
100176120Srwatson * Some FreeBSD details:
101176139Srwatson *
102176139Srwatson * codadev_modevent is called at boot time or module load time.
10338625Srvb */
104176139Srwatson#define	ENTRY do {							\
105176139Srwatson	if (coda_vnop_print_entry)					\
106176139Srwatson		myprintf(("Entered %s\n", __func__));			\
107176139Srwatson} while (0)
10838625Srvb
109176139Srwatson/*
110176139Srwatson * Definition of the vnode operation vector.
111176139Srwatson */
112138290Sphkstruct vop_vector coda_vnodeops = {
113176139Srwatson	.vop_default = &default_vnodeops,
114176233Srwatson	.vop_cachedlookup = coda_lookup,	/* uncached lookup */
115176233Srwatson	.vop_lookup = vfs_cache_lookup,		/* namecache lookup */
116176139Srwatson	.vop_create = coda_create,		/* create */
117176139Srwatson	.vop_open = coda_open,			/* open */
118176139Srwatson	.vop_close = coda_close,		/* close */
119176139Srwatson	.vop_access = coda_access,		/* access */
120176139Srwatson	.vop_getattr = coda_getattr,		/* getattr */
121176139Srwatson	.vop_setattr = coda_setattr,		/* setattr */
122176139Srwatson	.vop_read = coda_read,			/* read */
123176139Srwatson	.vop_write = coda_write,		/* write */
124176139Srwatson	.vop_ioctl = coda_ioctl,		/* ioctl */
125176139Srwatson	.vop_fsync = coda_fsync,		/* fsync */
126176139Srwatson	.vop_remove = coda_remove,		/* remove */
127176139Srwatson	.vop_link = coda_link,			/* link */
128176139Srwatson	.vop_rename = coda_rename,		/* rename */
129176139Srwatson	.vop_mkdir = coda_mkdir,		/* mkdir */
130176139Srwatson	.vop_rmdir = coda_rmdir,		/* rmdir */
131176139Srwatson	.vop_symlink = coda_symlink,		/* symlink */
132176139Srwatson	.vop_readdir = coda_readdir,		/* readdir */
133176139Srwatson	.vop_readlink = coda_readlink,		/* readlink */
134176139Srwatson	.vop_inactive = coda_inactive,		/* inactive */
135176139Srwatson	.vop_reclaim = coda_reclaim,		/* reclaim */
136176139Srwatson	.vop_lock1 = coda_lock,			/* lock */
137176139Srwatson	.vop_unlock = coda_unlock,		/* unlock */
138176139Srwatson	.vop_bmap = VOP_EOPNOTSUPP,		/* bmap */
139176139Srwatson	.vop_print = VOP_NULL,			/* print */
140176139Srwatson	.vop_islocked = coda_islocked,		/* islocked */
141176139Srwatson	.vop_pathconf = coda_pathconf,		/* pathconf */
142176139Srwatson	.vop_poll = vop_stdpoll,
143176139Srwatson	.vop_getpages = vop_stdgetpages,	/* pager intf.*/
144176139Srwatson	.vop_putpages = vop_stdputpages,	/* pager intf.*/
145176139Srwatson	.vop_getwritemount = vop_stdgetwritemount,
146138290Sphk#if 0
147176139Srwatson	/* missing */
148176139Srwatson	.vop_cachedlookup = ufs_lookup,
149176139Srwatson	.vop_whiteout =	ufs_whiteout,
15038625Srvb#endif
15177784Sshafeeq
15238625Srvb};
15338625Srvb
154176131Srwatsonstatic void	coda_print_vattr(struct vattr *attr);
15538625Srvb
15638625Srvbint
15739085Srvbcoda_vnodeopstats_init(void)
15838625Srvb{
159176139Srwatson	int i;
160176139Srwatson
161176139Srwatson	for(i=0; i<CODA_VNODEOPS_SIZE; i++) {
16239085Srvb		coda_vnodeopstats[i].opcode = i;
16339085Srvb		coda_vnodeopstats[i].entries = 0;
16439085Srvb		coda_vnodeopstats[i].sat_intrn = 0;
16539085Srvb		coda_vnodeopstats[i].unsat_intrn = 0;
16639085Srvb		coda_vnodeopstats[i].gen_intrn = 0;
16738625Srvb	}
168176139Srwatson	return (0);
16938625Srvb}
170176139Srwatson
171176139Srwatson/*
172176139Srwatson * coda_open calls Venus which returns an open file descriptor the cache file
173176139Srwatson * holding the data.  We get the vnode while we are still in the context of
174176139Srwatson * the venus process in coda_psdev.c.  This vnode is then passed back to the
175176139Srwatson * caller and opened.
17638625Srvb */
17738625Srvbint
178138290Sphkcoda_open(struct vop_open_args *ap)
17938625Srvb{
18038625Srvb
181176139Srwatson	/*
182176139Srwatson	 * FreeBSD can pass the O_EXCL flag in mode, even though the check
183176139Srwatson	 * has already happened.  Venus defensively assumes that if open is
184176139Srwatson	 * passed the EXCL, it must be a bug.  We strip the flag here.
185176139Srwatson	 */
186176139Srwatson	/* true args */
187176139Srwatson	struct vnode **vpp = &(ap->a_vp);
188176139Srwatson	struct cnode *cp = VTOC(*vpp);
189176139Srwatson	int flag = ap->a_mode & (~O_EXCL);
190176139Srwatson	struct ucred *cred = ap->a_cred;
191176139Srwatson	struct thread *td = ap->a_td;
192176139Srwatson	/* locals */
193176139Srwatson	int error;
194176139Srwatson	struct vnode *vp;
19538625Srvb
196176139Srwatson	MARK_ENTRY(CODA_OPEN_STATS);
197176139Srwatson
198176139Srwatson	/*
199176139Srwatson	 * Check for open of control file.
200176139Srwatson	 */
201176139Srwatson	if (IS_CTL_VP(*vpp)) {
202176139Srwatson		/* XXX */
203176139Srwatson		/* if (WRITEABLE(flag)) */
204176139Srwatson		if (flag & (FWRITE | O_TRUNC | O_CREAT | O_EXCL)) {
205176139Srwatson			MARK_INT_FAIL(CODA_OPEN_STATS);
206176139Srwatson			return (EACCES);
207176139Srwatson		}
208176139Srwatson		MARK_INT_SAT(CODA_OPEN_STATS);
209176139Srwatson		return (0);
21038625Srvb	}
211176139Srwatson	error = venus_open(vtomi((*vpp)), &cp->c_fid, flag, cred,
212176139Srwatson	    td->td_proc, &vp);
213176139Srwatson	if (error)
214176139Srwatson		return (error);
215176139Srwatson	CODADEBUG(CODA_OPEN, myprintf(("open: vp %p result %d\n", vp,
216176139Srwatson	    error)););
21738625Srvb
218176139Srwatson	/*
219176139Srwatson	 * Save the vnode pointer for the cache file.
220176139Srwatson	 */
221176139Srwatson	if (cp->c_ovp == NULL) {
222176139Srwatson		cp->c_ovp = vp;
223176139Srwatson	} else {
224176139Srwatson		if (cp->c_ovp != vp)
225176139Srwatson			panic("coda_open: cp->c_ovp != ITOV(ip)");
226176139Srwatson	}
227176139Srwatson	cp->c_ocount++;
22838625Srvb
229176139Srwatson	/*
230176139Srwatson	 * Flush the attribute cached if writing the file.
231176139Srwatson	 */
232176139Srwatson	if (flag & FWRITE) {
233176139Srwatson		cp->c_owrite++;
234176139Srwatson		cp->c_flags &= ~C_VATTR;
235176139Srwatson	}
23638625Srvb
237176139Srwatson	/*
238176139Srwatson	 * Open the cache file.
239176139Srwatson	 */
240176139Srwatson	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
241176139Srwatson	error = VOP_OPEN(vp, flag, cred, td, NULL);
242176139Srwatson	if (error) {
243176139Srwatson		VOP_UNLOCK(vp, 0);
244176139Srwatson    		printf("coda_open: VOP_OPEN on container failed %d\n", error);
245176139Srwatson		return (error);
246176139Srwatson	}
247176139Srwatson	(*vpp)->v_object = vp->v_object;
248176118Srwatson	VOP_UNLOCK(vp, 0);
249176139Srwatson	return (0);
25038625Srvb}
25138625Srvb
25238625Srvb/*
25338625Srvb * Close the cache file used for I/O and notify Venus.
25438625Srvb */
25538625Srvbint
256138290Sphkcoda_close(struct vop_close_args *ap)
25738625Srvb{
258176139Srwatson	/* true args */
259176139Srwatson	struct vnode *vp = ap->a_vp;
260176139Srwatson	struct cnode *cp = VTOC(vp);
261176139Srwatson	int flag = ap->a_fflag;
262176139Srwatson	struct ucred *cred = ap->a_cred;
263176139Srwatson	struct thread *td = ap->a_td;
264176139Srwatson	/* locals */
265176139Srwatson	int error;
26638625Srvb
267176139Srwatson	MARK_ENTRY(CODA_CLOSE_STATS);
26838625Srvb
269176139Srwatson	/*
270176139Srwatson	 * Check for close of control file.
271176139Srwatson	 */
272176139Srwatson	if (IS_CTL_VP(vp)) {
273176139Srwatson		MARK_INT_SAT(CODA_CLOSE_STATS);
274176139Srwatson		return (0);
275176139Srwatson	}
276176139Srwatson	if (cp->c_ovp) {
277176139Srwatson		vn_lock(cp->c_ovp, LK_EXCLUSIVE | LK_RETRY);
278176139Srwatson		/* Do errors matter here? */
279176139Srwatson		VOP_CLOSE(cp->c_ovp, flag, cred, td);
280176139Srwatson		vput(cp->c_ovp);
281176139Srwatson	}
282171377Srwatson#ifdef CODA_VERBOSE
283176139Srwatson	else
284176139Srwatson		printf("coda_close: NO container vp %p/cp %p\n", vp, cp);
285171377Srwatson#endif
286176139Srwatson	if (--cp->c_ocount == 0)
287176139Srwatson		cp->c_ovp = NULL;
28838625Srvb
289176139Srwatson	/*
290176139Srwatson	 * File was opened for write.
291176139Srwatson	 */
292176139Srwatson	if (flag & FWRITE)
293176139Srwatson		--cp->c_owrite;
294176139Srwatson	if (!IS_UNMOUNTING(cp))
295176139Srwatson		error = venus_close(vtomi(vp), &cp->c_fid, flag, cred,
296176139Srwatson		    td->td_proc);
297176139Srwatson	else
298176139Srwatson		error = ENODEV;
299176139Srwatson	CODADEBUG(CODA_CLOSE, myprintf(("close: result %d\n",error)););
300176139Srwatson	return (error);
30138625Srvb}
30238625Srvb
30338625Srvbint
304138290Sphkcoda_read(struct vop_read_args *ap)
30538625Srvb{
30638625Srvb
307176139Srwatson	ENTRY;
308176139Srwatson	return (coda_rdwr(ap->a_vp, ap->a_uio, UIO_READ, ap->a_ioflag,
309176139Srwatson	    ap->a_cred, ap->a_uio->uio_td));
31038625Srvb}
31138625Srvb
31238625Srvbint
313138290Sphkcoda_write(struct vop_write_args *ap)
31438625Srvb{
31538625Srvb
316176139Srwatson	ENTRY;
317176139Srwatson	return (coda_rdwr(ap->a_vp, ap->a_uio, UIO_WRITE, ap->a_ioflag,
318176139Srwatson	    ap->a_cred, ap->a_uio->uio_td));
31938625Srvb}
32038625Srvb
32138625Srvbint
322154647Srwatsoncoda_rdwr(struct vnode *vp, struct uio *uiop, enum uio_rw rw, int ioflag,
323154647Srwatson    struct ucred *cred, struct thread *td)
324176139Srwatson{
325176139Srwatson	/* upcall decl */
326176139Srwatson	/* NOTE: container file operation!!! */
327176139Srwatson	/* locals */
328176139Srwatson	struct cnode *cp = VTOC(vp);
329176139Srwatson	struct vnode *cfvp = cp->c_ovp;
330176139Srwatson	int opened_internally = 0;
331176139Srwatson	int error = 0;
33238625Srvb
333176139Srwatson	MARK_ENTRY(CODA_RDWR_STATS);
334176139Srwatson	CODADEBUG(CODA_RDWR, myprintf(("coda_rdwr(%d, %p, %d, %lld, %d)\n",
335176139Srwatson	    rw, (void *)uiop->uio_iov->iov_base, uiop->uio_resid,
336176139Srwatson	    (long long)uiop->uio_offset, uiop->uio_segflg)););
33738625Srvb
338176139Srwatson	/*
339176139Srwatson	 * Check for rdwr of control object.
340176139Srwatson	 */
341176139Srwatson	if (IS_CTL_VP(vp)) {
342176139Srwatson		MARK_INT_FAIL(CODA_RDWR_STATS);
343176139Srwatson		return (EINVAL);
344176139Srwatson	}
34538625Srvb
346176139Srwatson	/*
347176139Srwatson	 * If file is not already open this must be a page {read,write}
348176139Srwatson	 * request and we should open it internally.
349176139Srwatson	 */
350176139Srwatson	if (cfvp == NULL) {
351176139Srwatson		opened_internally = 1;
352176139Srwatson		MARK_INT_GEN(CODA_OPEN_STATS);
353176139Srwatson		error = VOP_OPEN(vp, (rw == UIO_READ ? FREAD : FWRITE), cred,
354176139Srwatson		    td, NULL);
355175545Srwatson#ifdef CODA_VERBOSE
356176139Srwatson		printf("coda_rdwr: Internally Opening %p\n", vp);
357175545Srwatson#endif
358176139Srwatson		if (error) {
359176139Srwatson			printf("coda_rdwr: VOP_OPEN on container failed "
360176139Srwatson			    "%d\n", error);
361176139Srwatson			return (error);
362176139Srwatson		}
363176139Srwatson		cfvp = cp->c_ovp;
36438625Srvb	}
36538625Srvb
366176139Srwatson	/*
367176139Srwatson	 * Have UFS handle the call.
368176139Srwatson	 */
369176139Srwatson	CODADEBUG(CODA_RDWR, myprintf(("indirect rdwr: fid = %s, refcnt = "
370176139Srwatson	    "%d\n", coda_f2s(&cp->c_fid), CTOV(cp)->v_usecount)););
371176139Srwatson	vn_lock(cfvp, LK_EXCLUSIVE | LK_RETRY);
372176139Srwatson	if (rw == UIO_READ) {
373176139Srwatson		error = VOP_READ(cfvp, uiop, ioflag, cred);
374176139Srwatson	} else {
375176139Srwatson		error = VOP_WRITE(cfvp, uiop, ioflag, cred);
376176139Srwatson		/*
377176139Srwatson		 * ufs_write updates the vnode_pager_setsize for the
378176139Srwatson		 * vnode/object.
379176139Srwatson		 *
380176139Srwatson		 * XXX: Since we now share vm objects between layers, this is
381176139Srwatson		 * probably unnecessary.
382176139Srwatson		 */
383176139Srwatson		{
384176139Srwatson			struct vattr attr;
385182371Sattilio			if (VOP_GETATTR(cfvp, &attr, cred) == 0)
386176139Srwatson				vnode_pager_setsize(vp, attr.va_size);
387176139Srwatson		}
388176139Srwatson	}
389176139Srwatson	VOP_UNLOCK(cfvp, 0);
390176139Srwatson	if (error)
391176139Srwatson		MARK_INT_FAIL(CODA_RDWR_STATS);
392176139Srwatson	else
393176139Srwatson		MARK_INT_SAT(CODA_RDWR_STATS);
39439650Srvb
395176139Srwatson	/*
396176139Srwatson	 * Do an internal close if necessary.
397176139Srwatson	 */
398176139Srwatson	if (opened_internally) {
399176139Srwatson		MARK_INT_GEN(CODA_CLOSE_STATS);
400176139Srwatson		(void)VOP_CLOSE(vp, (rw == UIO_READ ? FREAD : FWRITE), cred,
401176139Srwatson		    td);
40238625Srvb	}
40338625Srvb
404176139Srwatson	/*
405176139Srwatson	 * Invalidate cached attributes if writing.
406176139Srwatson	 */
407176139Srwatson	if (rw == UIO_WRITE)
408176139Srwatson		cp->c_flags &= ~C_VATTR;
409176139Srwatson	return (error);
41038625Srvb}
41138625Srvb
41238625Srvbint
413138290Sphkcoda_ioctl(struct vop_ioctl_args *ap)
41438625Srvb{
415176139Srwatson	/* true args */
416176139Srwatson	struct vnode *vp = ap->a_vp;
417176139Srwatson	int com = ap->a_command;
418176139Srwatson	caddr_t data = ap->a_data;
419176139Srwatson	int flag = ap->a_fflag;
420176139Srwatson	struct ucred *cred = ap->a_cred;
421176139Srwatson	struct thread *td = ap->a_td;
422176139Srwatson	/* locals */
423176139Srwatson	int error;
424176139Srwatson	struct vnode *tvp;
425176139Srwatson	struct nameidata ndp;
426176139Srwatson	struct PioctlData *iap = (struct PioctlData *)data;
42738625Srvb
428176139Srwatson	MARK_ENTRY(CODA_IOCTL_STATS);
429176139Srwatson	CODADEBUG(CODA_IOCTL, myprintf(("in coda_ioctl on %s\n", iap->path)););
43038625Srvb
431176139Srwatson	/*
432176139Srwatson	 * Don't check for operation on a dying object, for ctlvp it
433176139Srwatson	 * shouldn't matter.
434176139Srwatson	 *
435176139Srwatson	 * Must be control object to succeed.
436176139Srwatson	 */
437176139Srwatson	if (!IS_CTL_VP(vp)) {
438176139Srwatson		MARK_INT_FAIL(CODA_IOCTL_STATS);
439176139Srwatson		CODADEBUG(CODA_IOCTL, myprintf(("coda_ioctl error: vp != "
440176139Srwatson		    "ctlvp")););
441176139Srwatson		return (EOPNOTSUPP);
442176139Srwatson	}
44338625Srvb
444176139Srwatson	/*
445176139Srwatson	 * Look up the pathname.
446176139Srwatson	 *
447176139Srwatson	 * Should we use the name cache here? It would get it from lookupname
448176139Srwatson	 * sooner or later anyway, right?
449176139Srwatson	 */
450176139Srwatson	NDINIT(&ndp, LOOKUP, (iap->follow ? FOLLOW : NOFOLLOW),
451176139Srwatson	    UIO_USERSPACE, iap->path, td);
452176139Srwatson	error = namei(&ndp);
453176139Srwatson	tvp = ndp.ni_vp;
454176139Srwatson	if (error) {
455176139Srwatson		MARK_INT_FAIL(CODA_IOCTL_STATS);
456176139Srwatson		CODADEBUG(CODA_IOCTL, myprintf(("coda_ioctl error: lookup "
457176139Srwatson		    "returns %d\n", error)););
458176139Srwatson		return (error);
459176139Srwatson	}
46038625Srvb
461176139Srwatson	/*
462176139Srwatson	 * Make sure this is a coda style cnode, but it may be a different
463176139Srwatson	 * vfsp.
464176139Srwatson	 */
465176139Srwatson	if (tvp->v_op != &coda_vnodeops) {
466176139Srwatson		vrele(tvp);
467176139Srwatson		NDFREE(&ndp, NDF_ONLY_PNBUF);
468176139Srwatson		MARK_INT_FAIL(CODA_IOCTL_STATS);
469176139Srwatson		CODADEBUG(CODA_IOCTL,
470176139Srwatson		myprintf(("coda_ioctl error: %s not a coda object\n",
471176139Srwatson		    iap->path)););
472176139Srwatson		return (EINVAL);
473176139Srwatson	}
474176139Srwatson	if (iap->vi.in_size > VC_MAXDATASIZE) {
475176139Srwatson		NDFREE(&ndp, 0);
476176139Srwatson		return (EINVAL);
477176139Srwatson	}
478176139Srwatson	error = venus_ioctl(vtomi(tvp), &((VTOC(tvp))->c_fid), com, flag,
479176139Srwatson	    data, cred, td->td_proc);
480176139Srwatson	if (error)
481176139Srwatson		MARK_INT_FAIL(CODA_IOCTL_STATS);
482176139Srwatson	else
483176139Srwatson		CODADEBUG(CODA_IOCTL, myprintf(("Ioctl returns %d \n",
484176139Srwatson		    error)););
48538625Srvb	vrele(tvp);
48654655Seivind	NDFREE(&ndp, NDF_ONLY_PNBUF);
487176139Srwatson	return (error);
48838625Srvb}
48938625Srvb
49038625Srvb/*
491176139Srwatson * To reduce the cost of a user-level venus;we cache attributes in the
492176139Srwatson * kernel.  Each cnode has storage allocated for an attribute.  If c_vattr is
493176139Srwatson * valid, return a reference to it.  Otherwise, get the attributes from venus
494176139Srwatson * and store them in the cnode.  There is some question if this method is a
495176139Srwatson * security leak.  But I think that in order to make this call, the user must
496176139Srwatson * have done a lookup and opened the file, and therefore should already have
497176139Srwatson * access.
49838625Srvb */
49938625Srvbint
500138290Sphkcoda_getattr(struct vop_getattr_args *ap)
50138625Srvb{
502176139Srwatson	/* true args */
503176139Srwatson	struct vnode *vp = ap->a_vp;
504176139Srwatson	struct cnode *cp = VTOC(vp);
505176139Srwatson	struct vattr *vap = ap->a_vap;
506176139Srwatson	struct ucred *cred = ap->a_cred;
507176139Srwatson	/* locals */
508176139Srwatson    	struct vnode *convp;
509176139Srwatson	int error, size;
51038625Srvb
511176139Srwatson	MARK_ENTRY(CODA_GETATTR_STATS);
512176139Srwatson	if (IS_UNMOUNTING(cp))
513176139Srwatson		return (ENODEV);
51438625Srvb
515176139Srwatson	/*
516176139Srwatson	 * Check for getattr of control object.
517176139Srwatson	 */
518176139Srwatson	if (IS_CTL_VP(vp)) {
519176139Srwatson		MARK_INT_FAIL(CODA_GETATTR_STATS);
520176139Srwatson		return (ENOENT);
521176139Srwatson	}
52238759Srvb
523176139Srwatson	/*
524176139Srwatson	 * Check to see if the attributes have already been cached.
525176139Srwatson	 */
526176139Srwatson	if (VALID_VATTR(cp)) {
527176139Srwatson		CODADEBUG(CODA_GETATTR, myprintf(("attr cache hit: %s\n",
528176139Srwatson		    coda_f2s(&cp->c_fid))););
529176139Srwatson		CODADEBUG(CODA_GETATTR, if (!(codadebug & ~CODA_GETATTR))
530176139Srwatson		    coda_print_vattr(&cp->c_vattr););
531176139Srwatson		*vap = cp->c_vattr;
532176139Srwatson		MARK_INT_SAT(CODA_GETATTR_STATS);
533176139Srwatson		return (0);
534176139Srwatson	}
535182371Sattilio    	error = venus_getattr(vtomi(vp), &cp->c_fid, cred, vap);
536176139Srwatson	if (!error) {
537176139Srwatson		CODADEBUG(CODA_GETATTR, myprintf(("getattr miss %s: result "
538176139Srwatson		    "%d\n", coda_f2s(&cp->c_fid), error)););
539176139Srwatson		CODADEBUG(CODA_GETATTR, if (!(codadebug & ~CODA_GETATTR))
540176139Srwatson		    coda_print_vattr(vap););
54138625Srvb
542176139Srwatson		/*
543176139Srwatson		 * XXX: Since we now share vm objects between layers, this is
544176139Srwatson		 * probably unnecessary.
545176139Srwatson		 */
546176139Srwatson		size = vap->va_size;
547176139Srwatson    		convp = cp->c_ovp;
548176139Srwatson		if (convp != NULL)
549176139Srwatson			vnode_pager_setsize(convp, size);
55038625Srvb
551176139Srwatson		/*
552176139Srwatson		 * If not open for write, store attributes in cnode.
553176139Srwatson		 */
554176139Srwatson		if ((cp->c_owrite == 0) && (coda_attr_cache)) {
555176139Srwatson			cp->c_vattr = *vap;
556176139Srwatson			cp->c_flags |= C_VATTR;
557176139Srwatson		}
55838625Srvb	}
559176139Srwatson	return (error);
56038625Srvb}
56138625Srvb
56238625Srvbint
563138290Sphkcoda_setattr(struct vop_setattr_args *ap)
56438625Srvb{
565176139Srwatson	/* true args */
566176139Srwatson	struct vnode *vp = ap->a_vp;
567176139Srwatson	struct cnode *cp = VTOC(vp);
568176139Srwatson	struct vattr *vap = ap->a_vap;
569176139Srwatson	struct ucred *cred = ap->a_cred;
570176139Srwatson	/* locals */
571176139Srwatson    	struct vnode *convp;
572176139Srwatson	int error, size;
57338625Srvb
574176139Srwatson	MARK_ENTRY(CODA_SETATTR_STATS);
57538625Srvb
576176139Srwatson	/*
577176139Srwatson	 * Check for setattr of control object.
578176139Srwatson	 */
579176139Srwatson	if (IS_CTL_VP(vp)) {
580176139Srwatson		MARK_INT_FAIL(CODA_SETATTR_STATS);
581176139Srwatson		return (ENOENT);
582176139Srwatson	}
583176139Srwatson	if (codadebug & CODADBGMSK(CODA_SETATTR))
584176139Srwatson		coda_print_vattr(vap);
585182371Sattilio	error = venus_setattr(vtomi(vp), &cp->c_fid, vap, cred);
586176139Srwatson	if (!error)
587176238Srwatson		cp->c_flags &= ~(C_VATTR | C_ACCCACHE);
58838625Srvb
589176139Srwatson	/*
590176139Srwatson	 * XXX: Since we now share vm objects between layers, this is
591176139Srwatson	 * probably unnecessary.
592176139Srwatson	 *
593176139Srwatson	 * XXX: Shouldn't we only be doing this "set" if C_VATTR remains
594176139Srwatson	 * valid after venus_setattr()?
595176139Srwatson	 */
596176139Srwatson	size = vap->va_size;
597176139Srwatson    	convp = cp->c_ovp;
598176139Srwatson	if (size != VNOVAL && convp != NULL)
599176139Srwatson		vnode_pager_setsize(convp, size);
600176139Srwatson	CODADEBUG(CODA_SETATTR,	myprintf(("setattr %d\n", error)););
601176139Srwatson	return (error);
60238625Srvb}
60338625Srvb
60438625Srvbint
605138290Sphkcoda_access(struct vop_access_args *ap)
60638625Srvb{
607176139Srwatson	/* true args */
608176139Srwatson	struct vnode *vp = ap->a_vp;
609176139Srwatson	struct cnode *cp = VTOC(vp);
610184413Strasz	accmode_t accmode = ap->a_accmode;
611176139Srwatson	struct ucred *cred = ap->a_cred;
612176139Srwatson	struct thread *td = ap->a_td;
613176139Srwatson	/* locals */
614176238Srwatson	int error;
61538625Srvb
616176139Srwatson	MARK_ENTRY(CODA_ACCESS_STATS);
61738625Srvb
618176139Srwatson	/*
619176139Srwatson	 * Check for access of control object.  Only read access is allowed
620176139Srwatson	 * on it.
621176139Srwatson	 */
622176139Srwatson	if (IS_CTL_VP(vp)) {
623176139Srwatson		/*
624176139Srwatson		 * Bogus hack - all will be marked as successes.
625176139Srwatson		 */
62639085Srvb		MARK_INT_SAT(CODA_ACCESS_STATS);
627184413Strasz		return (((accmode & VREAD) && !(accmode & (VWRITE | VEXEC)))
628176139Srwatson		    ? 0 : EACCES);
62938625Srvb	}
63038625Srvb
631176139Srwatson	/*
632176238Srwatson	 * We maintain a one-entry LRU positive access cache with each cnode.
633176238Srwatson	 * In principle we could also track negative results, and for more
634176238Srwatson	 * than one uid, but we don't yet.  Venus is responsible for
635176238Srwatson	 * invalidating this cache as required.
636176139Srwatson	 */
637176238Srwatson	if (coda_access_cache && VALID_ACCCACHE(cp) &&
638176238Srwatson	    (cred->cr_uid == cp->c_cached_uid) &&
639184413Strasz	    (accmode & cp->c_cached_mode) == accmode) {
640176238Srwatson		MARK_INT_SAT(CODA_ACCESS_STATS);
641176238Srwatson		return (0);
642176238Srwatson	}
643184413Strasz	error = venus_access(vtomi(vp), &cp->c_fid, accmode, cred, td->td_proc);
644176238Srwatson	if (error == 0 && coda_access_cache) {
645176238Srwatson		/*-
646176238Srwatson		 * When we have a new successful request, we consider three
647176238Srwatson		 * cases:
648176238Srwatson		 *
649176238Srwatson		 * - No initialized access cache, in which case cache the
650176238Srwatson		 *   result.
651176238Srwatson		 * - Cached result for a different user, in which case we
652176238Srwatson		 *   replace the entry.
653176238Srwatson		 * - Cached result for the same user, in which case we add
654176238Srwatson		 *   any newly granted rights to the cached mode.
655176238Srwatson		 *
656176238Srwatson		 * XXXRW: If we ever move to something more interesting than
657176238Srwatson		 * uid-based token lookup, we'll need to change this.
658176238Srwatson		 */
659176238Srwatson		cp->c_flags |= C_ACCCACHE;
660176238Srwatson		if (cp->c_cached_uid != cred->cr_uid) {
661184413Strasz			cp->c_cached_mode = accmode;
662176238Srwatson			cp->c_cached_uid = cred->cr_uid;
663176238Srwatson		} else
664184413Strasz			cp->c_cached_mode |= accmode;
665176238Srwatson	}
666176238Srwatson	return (error);
66738625Srvb}
66838625Srvb
66938625Srvbint
670138290Sphkcoda_readlink(struct vop_readlink_args *ap)
67138625Srvb{
672176139Srwatson	/* true args */
673176139Srwatson	struct vnode *vp = ap->a_vp;
674176139Srwatson	struct cnode *cp = VTOC(vp);
675176139Srwatson	struct uio *uiop = ap->a_uio;
676176139Srwatson	struct ucred *cred = ap->a_cred;
677176139Srwatson	struct thread *td = ap->a_uio->uio_td;
678176139Srwatson	/* locals */
679176139Srwatson	int error;
680176139Srwatson	char *str;
681176139Srwatson	int len;
68238625Srvb
683176139Srwatson	MARK_ENTRY(CODA_READLINK_STATS);
68438625Srvb
685176139Srwatson	/*
686176139Srwatson	 * Check for readlink of control object.
687176139Srwatson	 */
688176139Srwatson	if (IS_CTL_VP(vp)) {
689176139Srwatson		MARK_INT_FAIL(CODA_READLINK_STATS);
690176139Srwatson		return (ENOENT);
691176139Srwatson	}
692176139Srwatson	if ((coda_symlink_cache) && (VALID_SYMLINK(cp))) {
693176139Srwatson		/*
694176139Srwatson		 * Symlink was cached.
695176139Srwatson		 */
696176139Srwatson		uiop->uio_rw = UIO_READ;
697176139Srwatson		error = uiomove(cp->c_symlink, (int)cp->c_symlen, uiop);
698176139Srwatson		if (error)
699176139Srwatson			MARK_INT_FAIL(CODA_READLINK_STATS);
700176139Srwatson		else
701176139Srwatson			MARK_INT_SAT(CODA_READLINK_STATS);
702176139Srwatson		return (error);
703176139Srwatson	}
704176139Srwatson	error = venus_readlink(vtomi(vp), &cp->c_fid, cred, td != NULL ?
705176139Srwatson	    td->td_proc : NULL, &str, &len);
706176139Srwatson	if (!error) {
707176139Srwatson		uiop->uio_rw = UIO_READ;
708176139Srwatson		error = uiomove(str, len, uiop);
709176139Srwatson		if (coda_symlink_cache) {
710176139Srwatson			cp->c_symlink = str;
711176139Srwatson			cp->c_symlen = len;
712176139Srwatson			cp->c_flags |= C_SYMLINK;
713176139Srwatson		} else
714176139Srwatson			CODA_FREE(str, len);
715176139Srwatson	}
716176139Srwatson	CODADEBUG(CODA_READLINK, myprintf(("in readlink result %d\n",
717176139Srwatson	    error)););
718176139Srwatson	return (error);
71938625Srvb}
72038625Srvb
72138625Srvbint
722138290Sphkcoda_fsync(struct vop_fsync_args *ap)
72338625Srvb{
724176139Srwatson	/* true args */
725176139Srwatson	struct vnode *vp = ap->a_vp;
726176139Srwatson	struct cnode *cp = VTOC(vp);
727176139Srwatson	struct thread *td = ap->a_td;
728176139Srwatson	/* locals */
729176139Srwatson	struct vnode *convp = cp->c_ovp;
730176139Srwatson	int error;
73138625Srvb
732176139Srwatson	MARK_ENTRY(CODA_FSYNC_STATS);
73338625Srvb
734176139Srwatson	/*
735176139Srwatson	 * Check for fsync on an unmounting object.
736176139Srwatson	 *
737176139Srwatson	 * XXX: Is this comment true on FreeBSD?  It seems likely, since
738176139Srwatson	 * unmounting is fairly non-atomic.
739176139Srwatson	 *
740176139Srwatson	 * The NetBSD kernel, in it's infinite wisdom, can try to fsync after
741176139Srwatson	 * an unmount has been initiated.  This is a Bad Thing, which we have
742176139Srwatson	 * to avoid.  Not a legitimate failure for stats.
743176139Srwatson	 */
744176139Srwatson	if (IS_UNMOUNTING(cp))
745176139Srwatson		return (ENODEV);
74638625Srvb
747176139Srwatson	/*
748176139Srwatson	 * Check for fsync of control object.
749176139Srwatson	 */
750176139Srwatson	if (IS_CTL_VP(vp)) {
751176139Srwatson		MARK_INT_SAT(CODA_FSYNC_STATS);
752176139Srwatson		return (0);
753176139Srwatson	}
754176139Srwatson	if (convp != NULL) {
755176139Srwatson		vn_lock(convp, LK_EXCLUSIVE | LK_RETRY);
756176139Srwatson		VOP_FSYNC(convp, MNT_WAIT, td);
757176139Srwatson		VOP_UNLOCK(convp, 0);
758176139Srwatson	}
75938625Srvb
760176139Srwatson	/*
761176139Srwatson	 * We see fsyncs with usecount == 1 then usecount == 0.  For now we
762176139Srwatson	 * ignore them.
763176139Srwatson	 */
764176139Srwatson#if 0
765176139Srwatson	VI_LOCK(vp);
766176139Srwatson	if (!vp->v_usecount) {
767176139Srwatson		printf("coda_fsync on vnode %p with %d usecount.  "
768176139Srwatson		    "c_flags = %x (%x)\n", vp, vp->v_usecount, cp->c_flags,
769176139Srwatson		    cp->c_flags&C_PURGING);
770176139Srwatson	}
771176139Srwatson	VI_UNLOCK(vp);
772176139Srwatson#endif
77338625Srvb
774176139Srwatson	/*
775176139Srwatson	 * We can expect fsync on any vnode at all if venus is purging it.
776176139Srwatson	 * Venus can't very well answer the fsync request, now can it?
777176139Srwatson	 * Hopefully, it won't have to, because hopefully, venus preserves
778176139Srwatson	 * the (possibly untrue) invariant that it never purges an open
779176139Srwatson	 * vnode.  Hopefully.
780176139Srwatson	 */
781176139Srwatson	if (cp->c_flags & C_PURGING)
782176139Srwatson		return (0);
78338625Srvb
784176139Srwatson	/* XXX: needs research */
785176139Srwatson	return (0);
786176139Srwatson	error = venus_fsync(vtomi(vp), &cp->c_fid, td->td_proc);
787176139Srwatson	CODADEBUG(CODA_FSYNC, myprintf(("in fsync result %d\n", error)););
788176139Srwatson	return (error);
78938625Srvb}
79038625Srvb
79138625Srvbint
792138290Sphkcoda_inactive(struct vop_inactive_args *ap)
79338625Srvb{
794176139Srwatson	/*
795176139Srwatson	 * XXX - at the moment, inactive doesn't look at cred, and doesn't
796176139Srwatson	 * have a proc pointer.  Oops.
797176139Srwatson	 */
798176139Srwatson	/* true args */
799176139Srwatson	struct vnode *vp = ap->a_vp;
800176139Srwatson	struct cnode *cp = VTOC(vp);
801176139Srwatson	struct ucred *cred __attribute__((unused)) = NULL;
802176139Srwatson	struct thread *td __attribute__((unused)) = curthread;
803176139Srwatson	/* upcall decl */
804176139Srwatson	/* locals */
80538625Srvb
806176139Srwatson	/*
807176139Srwatson	 * We don't need to send inactive to venus - DCS.
808176139Srwatson	 */
809176139Srwatson	MARK_ENTRY(CODA_INACTIVE_STATS);
810176139Srwatson	CODADEBUG(CODA_INACTIVE, myprintf(("in inactive, %s, vfsp %p\n",
811176139Srwatson	    coda_f2s(&cp->c_fid), vp->v_mount)););
812176139Srwatson	vp->v_object = NULL;
81338625Srvb
814176139Srwatson	/*
815176139Srwatson	 * If an array has been allocated to hold the symlink, deallocate it.
816176139Srwatson	 */
817176139Srwatson	if ((coda_symlink_cache) && (VALID_SYMLINK(cp))) {
818176139Srwatson		if (cp->c_symlink == NULL)
819176139Srwatson			panic("coda_inactive: null symlink pointer in cnode");
820176139Srwatson		CODA_FREE(cp->c_symlink, cp->c_symlen);
821176139Srwatson		cp->c_flags &= ~C_SYMLINK;
822176139Srwatson		cp->c_symlen = 0;
823176139Srwatson	}
824175473Srwatson
825176139Srwatson	/*
826176139Srwatson	 * Remove it from the table so it can't be found.
827176139Srwatson	 */
828176139Srwatson	coda_unsave(cp);
829176139Srwatson	if ((struct coda_mntinfo *)(vp->v_mount->mnt_data) == NULL) {
830176139Srwatson		myprintf(("Help! vfsp->vfs_data was NULL, but vnode %p "
831176139Srwatson		    "wasn't dying\n", vp));
832176139Srwatson		panic("badness in coda_inactive\n");
833176139Srwatson	}
834176139Srwatson	if (IS_UNMOUNTING(cp)) {
83538625Srvb#ifdef	DEBUG
836176139Srwatson		printf("coda_inactive: IS_UNMOUNTING use %d: vp %p, cp %p\n",
837176139Srwatson		    vrefcnt(vp), vp, cp);
838176139Srwatson		if (cp->c_ovp != NULL)
839176139Srwatson			printf("coda_inactive: cp->ovp != NULL use %d: vp "
840176139Srwatson			    "%p, cp %p\n", vrefcnt(vp), vp, cp);
84138625Srvb#endif
842176263Srwatson	} else
843176139Srwatson		vgone(vp);
844176139Srwatson	MARK_INT_SAT(CODA_INACTIVE_STATS);
845176139Srwatson	return (0);
84638625Srvb}
84738625Srvb
84838625Srvb/*
84996755Strhodes * Remote filesystem operations having to do with directory manipulation.
85038625Srvb */
85138625Srvb
852176139Srwatson/*
853176120Srwatson * In FreeBSD, lookup returns the vnode locked.
85438625Srvb */
85538625Srvbint
856176233Srwatsoncoda_lookup(struct vop_cachedlookup_args *ap)
85738625Srvb{
858176139Srwatson	/* true args */
859176139Srwatson	struct vnode *dvp = ap->a_dvp;
860176139Srwatson	struct cnode *dcp = VTOC(dvp);
861176139Srwatson	struct vnode **vpp = ap->a_vpp;
862176139Srwatson	/*
863176139Srwatson	 * It looks as though ap->a_cnp->ni_cnd->cn_nameptr holds the rest of
864176139Srwatson	 * the string to xlate, and that we must try to get at least
865176139Srwatson	 * ap->a_cnp->ni_cnd->cn_namelen of those characters to macth.  I
866176139Srwatson	 * could be wrong.
867176139Srwatson	 */
868176139Srwatson	struct componentname  *cnp = ap->a_cnp;
869176139Srwatson	struct ucred *cred = cnp->cn_cred;
870176139Srwatson	struct thread *td = cnp->cn_thread;
871176139Srwatson	/* locals */
872176139Srwatson	struct cnode *cp;
873176139Srwatson	const char *nm = cnp->cn_nameptr;
874176139Srwatson	int len = cnp->cn_namelen;
875176139Srwatson	CodaFid VFid;
876176139Srwatson	int vtype;
877176139Srwatson	int error = 0;
87838625Srvb
879176139Srwatson	MARK_ENTRY(CODA_LOOKUP_STATS);
880176139Srwatson	CODADEBUG(CODA_LOOKUP, myprintf(("lookup: %s in %s\n", nm,
881176139Srwatson	    coda_f2s(&dcp->c_fid))););
88238625Srvb
883176139Srwatson	/*
884176139Srwatson	 * Check for lookup of control object.
885176139Srwatson	 */
886176139Srwatson	if (IS_CTL_NAME(dvp, nm, len)) {
887176139Srwatson		*vpp = coda_ctlvp;
888176139Srwatson		vref(*vpp);
889176139Srwatson		MARK_INT_SAT(CODA_LOOKUP_STATS);
890176139Srwatson		goto exit;
891176139Srwatson	}
892176139Srwatson	if (len+1 > CODA_MAXNAMLEN) {
893176139Srwatson		MARK_INT_FAIL(CODA_LOOKUP_STATS);
894176139Srwatson		CODADEBUG(CODA_LOOKUP, myprintf(("name too long: lookup, "
895176139Srwatson		    "%s (%s)\n", coda_f2s(&dcp->c_fid), nm)););
896176139Srwatson		*vpp = NULL;
897176139Srwatson		error = EINVAL;
898176139Srwatson		goto exit;
899176139Srwatson	}
90038625Srvb
901176233Srwatson	error = venus_lookup(vtomi(dvp), &dcp->c_fid, nm, len, cred,
902176233Srwatson	    td->td_proc, &VFid, &vtype);
903176233Srwatson	if (error) {
904176233Srwatson		MARK_INT_FAIL(CODA_LOOKUP_STATS);
905176233Srwatson		CODADEBUG(CODA_LOOKUP, myprintf(("lookup error on %s "
906176233Srwatson		    "(%s)%d\n", coda_f2s(&dcp->c_fid), nm, error)););
907176233Srwatson		*vpp = NULL;
908176139Srwatson	} else {
909176233Srwatson		MARK_INT_SAT(CODA_LOOKUP_STATS);
910176233Srwatson		CODADEBUG(CODA_LOOKUP, myprintf(("lookup: %s type %o "
911176233Srwatson		    "result %d\n", coda_f2s(&VFid), vtype, error)););
912176233Srwatson		cp = make_coda_node(&VFid, dvp->v_mount, vtype);
913176233Srwatson    		*vpp = CTOV(cp);
914176233Srwatson
915176233Srwatson    		/*
916176233Srwatson		 * Enter the new vnode in the namecache only if the top bit
917176233Srwatson		 * isn't set.
918176233Srwatson		 *
919176233Srwatson		 * And don't enter a new vnode for an invalid one!
920176139Srwatson		 */
921176233Srwatson		if (!(vtype & CODA_NOCACHE) && (cnp->cn_flags & MAKEENTRY))
922176233Srwatson			cache_enter(dvp, *vpp, cnp);
92338625Srvb	}
924176139Srwatsonexit:
925176139Srwatson	/*
926176139Srwatson	 * If we are creating, and this was the last name to be looked up,
927176139Srwatson	 * and the error was ENOENT, then there really shouldn't be an error
928176139Srwatson	 * and we can make the leaf NULL and return success.  Since this is
929176139Srwatson	 * supposed to work under Mach as well as FreeBSD, we're leaving this
930176139Srwatson	 * fn wrapped.  We also must tell lookup/namei that we need to save
931176139Srwatson	 * the last component of the name.  (Create will have to free the
932176139Srwatson	 * name buffer later...lucky us...).
933176139Srwatson	 */
934176139Srwatson	if (((cnp->cn_nameiop == CREATE) || (cnp->cn_nameiop == RENAME))
935176139Srwatson	    && (cnp->cn_flags & ISLASTCN) && (error == ENOENT)) {
936176139Srwatson		error = EJUSTRETURN;
937176139Srwatson		cnp->cn_flags |= SAVENAME;
938176139Srwatson		*ap->a_vpp = NULL;
939176139Srwatson	}
94038625Srvb
941176139Srwatson	/*
942176139Srwatson	 * If we are removing, and we are at the last element, and we found
943176139Srwatson	 * it, then we need to keep the name around so that the removal will
944176139Srwatson	 * go ahead as planned.  Unfortunately, this will probably also lock
945176139Srwatson	 * the to-be-removed vnode, which may or may not be a good idea.
946176139Srwatson	 * I'll have to look at the bits of coda_remove to make sure.  We'll
947176139Srwatson	 * only save the name if we did in fact find the name, otherwise
948176139Srwatson	 * coda_remove won't have a chance to free the pathname.
949176139Srwatson	 */
950176139Srwatson	if ((cnp->cn_nameiop == DELETE) && (cnp->cn_flags & ISLASTCN)
951176139Srwatson	    && !error)
952176139Srwatson		cnp->cn_flags |= SAVENAME;
95338625Srvb
954176139Srwatson	/*
955176139Srwatson	 * If the lookup went well, we need to (potentially?) unlock the
956176139Srwatson	 * parent, and lock the child.  We are only responsible for checking
957176139Srwatson	 * to see if the parent is supposed to be unlocked before we return.
958176139Srwatson	 * We must always lock the child (provided there is one, and (the
959176139Srwatson	 * parent isn't locked or it isn't the same as the parent.)  Simple,
960176139Srwatson	 * huh?  We can never leave the parent locked unless we are ISLASTCN.
961176139Srwatson	 */
962176139Srwatson	if (!error || (error == EJUSTRETURN)) {
963176139Srwatson		if (cnp->cn_flags & ISDOTDOT) {
964176139Srwatson			VOP_UNLOCK(dvp, 0);
965176139Srwatson			/*
966176139Srwatson			 * The parent is unlocked.  As long as there is a
967176139Srwatson			 * child, lock it without bothering to check anything
968176139Srwatson			 * else.
969176139Srwatson			 */
970176139Srwatson			if (*ap->a_vpp)
971176139Srwatson				vn_lock(*ap->a_vpp, LK_EXCLUSIVE | LK_RETRY);
972176139Srwatson			vn_lock(dvp, LK_RETRY|LK_EXCLUSIVE);
973176139Srwatson		} else {
974176139Srwatson			/*
975176139Srwatson			 * The parent is locked, and may be the same as the
976176139Srwatson			 * child.  If different, go ahead and lock it.
977176139Srwatson			 */
978176139Srwatson			if (*ap->a_vpp && (*ap->a_vpp != dvp))
979176139Srwatson				vn_lock(*ap->a_vpp, LK_EXCLUSIVE | LK_RETRY);
980176139Srwatson		}
98138625Srvb	} else {
982176139Srwatson		/*
983176139Srwatson		 * If the lookup failed, we need to ensure that the leaf is
984176139Srwatson		 * NULL.
985176139Srwatson		 *
986176139Srwatson		 * Don't change any locking?
987176139Srwatson		 */
988176139Srwatson		*ap->a_vpp = NULL;
98938625Srvb	}
990176139Srwatson	return (error);
99138625Srvb}
99238625Srvb
99338625Srvb/*ARGSUSED*/
99438625Srvbint
995138290Sphkcoda_create(struct vop_create_args *ap)
99638625Srvb{
997176139Srwatson	/* true args */
998176139Srwatson	struct vnode *dvp = ap->a_dvp;
999176139Srwatson	struct cnode *dcp = VTOC(dvp);
1000176139Srwatson	struct vattr *va = ap->a_vap;
1001176139Srwatson	int exclusive = 1;
1002176139Srwatson	int mode = ap->a_vap->va_mode;
1003176139Srwatson	struct vnode **vpp = ap->a_vpp;
1004176139Srwatson	struct componentname  *cnp = ap->a_cnp;
1005176139Srwatson	struct ucred *cred = cnp->cn_cred;
1006176139Srwatson	struct thread *td = cnp->cn_thread;
1007176139Srwatson	/* locals */
1008176139Srwatson	int error;
1009176139Srwatson	struct cnode *cp;
1010176139Srwatson	const char *nm = cnp->cn_nameptr;
1011176139Srwatson	int len = cnp->cn_namelen;
1012176139Srwatson	CodaFid VFid;
1013176139Srwatson	struct vattr attr;
101438625Srvb
1015176139Srwatson	MARK_ENTRY(CODA_CREATE_STATS);
101638625Srvb
1017176139Srwatson	/*
1018176139Srwatson	 * All creates are exclusive XXX.
1019176139Srwatson	 *
1020176139Srwatson	 * I'm assuming the 'mode' argument is the file mode bits XXX.
1021176139Srwatson	 *
1022176139Srwatson	 * Check for create of control object.
1023176139Srwatson	 */
1024176139Srwatson	if (IS_CTL_NAME(dvp, nm, len)) {
1025176139Srwatson		*vpp = (struct vnode *)0;
1026176139Srwatson		MARK_INT_FAIL(CODA_CREATE_STATS);
1027176139Srwatson		return (EACCES);
1028176139Srwatson	}
1029176139Srwatson	error = venus_create(vtomi(dvp), &dcp->c_fid, nm, len, exclusive,
1030176139Srwatson	    mode, va, cred, td->td_proc, &VFid, &attr);
1031176139Srwatson	if (!error) {
1032176139Srwatson		/*
1033176139Srwatson		 * If this is an exclusive create, panic if the file already
1034176139Srwatson		 * exists.
1035176139Srwatson		 *
1036176139Srwatson		 * Venus should have detected the file and reported EEXIST.
1037176139Srwatson		 */
1038176139Srwatson		if ((exclusive == 1) && (coda_find(&VFid) != NULL))
1039176139Srwatson	  	  	panic("cnode existed for newly created file!");
1040176139Srwatson		cp = make_coda_node(&VFid, dvp->v_mount, attr.va_type);
1041176139Srwatson		*vpp = CTOV(cp);
104238625Srvb
1043176139Srwatson		/*
1044176139Srwatson		 * Update va to reflect the new attributes.
1045176139Srwatson		 */
1046176139Srwatson		(*va) = attr;
104738625Srvb
1048176139Srwatson		/*
1049176139Srwatson		 * Update the attribute cache and mark it as valid.
1050176139Srwatson		 */
1051176139Srwatson		if (coda_attr_cache) {
1052176139Srwatson			VTOC(*vpp)->c_vattr = attr;
1053176139Srwatson			VTOC(*vpp)->c_flags |= C_VATTR;
1054176139Srwatson		}
105538625Srvb
1056176139Srwatson		/*
1057176139Srwatson		 * Invalidate the parent's attr cache, the modification time
1058176139Srwatson		 * has changed.
1059176139Srwatson		 */
1060176139Srwatson		VTOC(dvp)->c_flags &= ~C_VATTR;
1061176233Srwatson		cache_enter(dvp, *vpp, cnp);
1062176139Srwatson		CODADEBUG(CODA_CREATE, myprintf(("create: %s, result %d\n",
1063176139Srwatson		    coda_f2s(&VFid), error)););
1064176139Srwatson	} else {
1065176139Srwatson		*vpp = (struct vnode *)0;
1066176139Srwatson		CODADEBUG(CODA_CREATE, myprintf(("create error %d\n",
1067176139Srwatson		    error)););
106838625Srvb	}
1069176139Srwatson	if (!error) {
1070176233Srwatson		if (cnp->cn_flags & MAKEENTRY)
1071176233Srwatson			cache_enter(dvp, *vpp, cnp);
1072176139Srwatson		if (cnp->cn_flags & LOCKLEAF)
1073176139Srwatson			vn_lock(*ap->a_vpp, LK_EXCLUSIVE | LK_RETRY);
1074176233Srwatson	} else if (error == ENOENT) {
1075176233Srwatson		/*
1076176233Srwatson		 * XXXRW: We only enter a negative entry if ENOENT is
1077176233Srwatson		 * returned, not other errors.  But will Venus invalidate dvp
1078176233Srwatson		 * properly in all cases when new files appear via the
1079176233Srwatson		 * network rather than a local operation?
1080176233Srwatson		 */
1081176233Srwatson		if (cnp->cn_flags & MAKEENTRY)
1082176233Srwatson			cache_enter(dvp, NULL, cnp);
108338625Srvb	}
1084176139Srwatson	return (error);
108538625Srvb}
108638625Srvb
108738625Srvbint
1088138290Sphkcoda_remove(struct vop_remove_args *ap)
108938625Srvb{
1090176139Srwatson	/* true args */
1091176233Srwatson	struct vnode *vp = ap->a_vp;
1092176139Srwatson	struct vnode *dvp = ap->a_dvp;
1093176139Srwatson	struct cnode *cp = VTOC(dvp);
1094176139Srwatson	struct componentname  *cnp = ap->a_cnp;
1095176139Srwatson	struct ucred *cred = cnp->cn_cred;
1096176139Srwatson	struct thread *td = cnp->cn_thread;
1097176139Srwatson	/* locals */
1098176139Srwatson	int error;
1099176139Srwatson	const char *nm = cnp->cn_nameptr;
1100176139Srwatson	int len = cnp->cn_namelen;
1101176233Srwatson#if 0
1102176139Srwatson	struct cnode *tp;
1103176233Srwatson#endif
110438625Srvb
1105176139Srwatson	MARK_ENTRY(CODA_REMOVE_STATS);
1106176139Srwatson	CODADEBUG(CODA_REMOVE, myprintf(("remove: %s in %s\n", nm,
1107176139Srwatson	    coda_f2s(&cp->c_fid))););
110838625Srvb
1109176139Srwatson	/*
1110176233Srwatson	 * Check for remove of control object.
1111176139Srwatson	 */
1112176233Srwatson	if (IS_CTL_NAME(dvp, nm, len)) {
1113176233Srwatson		MARK_INT_FAIL(CODA_REMOVE_STATS);
1114176233Srwatson		return (ENOENT);
111538625Srvb	}
111638625Srvb
1117176139Srwatson	/*
1118176139Srwatson	 * Invalidate the parent's attr cache, the modification time has
1119176233Srwatson	 * changed.  We don't yet know if the last reference to the file is
1120176233Srwatson	 * being removed, but we do know the reference count on the child has
1121176233Srwatson	 * changed, so invalidate its attr cache also.
1122176139Srwatson	 */
1123176139Srwatson	VTOC(dvp)->c_flags &= ~C_VATTR;
1124176238Srwatson	VTOC(vp)->c_flags &= ~(C_VATTR | C_ACCCACHE);
1125176139Srwatson	error = venus_remove(vtomi(dvp), &cp->c_fid, nm, len, cred,
1126176139Srwatson	    td->td_proc);
1127176233Srwatson	cache_purge(vp);
1128176139Srwatson	CODADEBUG(CODA_REMOVE, myprintf(("in remove result %d\n",error)););
1129176139Srwatson	return (error);
113038625Srvb}
113138625Srvb
113238625Srvbint
1133138290Sphkcoda_link(struct vop_link_args *ap)
113438625Srvb{
1135176139Srwatson	/* true args */
1136176139Srwatson	struct vnode *vp = ap->a_vp;
1137176139Srwatson	struct cnode *cp = VTOC(vp);
1138176139Srwatson	struct vnode *tdvp = ap->a_tdvp;
1139176139Srwatson	struct cnode *tdcp = VTOC(tdvp);
1140176139Srwatson	struct componentname *cnp = ap->a_cnp;
1141176139Srwatson	struct ucred *cred = cnp->cn_cred;
1142176139Srwatson	struct thread *td = cnp->cn_thread;
1143176139Srwatson	/* locals */
1144176139Srwatson	int error;
1145176139Srwatson	const char *nm = cnp->cn_nameptr;
1146176139Srwatson	int len = cnp->cn_namelen;
114738625Srvb
1148176139Srwatson	MARK_ENTRY(CODA_LINK_STATS);
114938625Srvb
1150176139Srwatson	if (codadebug & CODADBGMSK(CODA_LINK)) {
1151176139Srwatson		myprintf(("nb_link:   vp fid: %s\n", coda_f2s(&cp->c_fid)));
1152176139Srwatson		myprintf(("nb_link: tdvp fid: %s)\n",
1153176139Srwatson		    coda_f2s(&tdcp->c_fid)));
1154176139Srwatson	}
1155176139Srwatson	if (codadebug & CODADBGMSK(CODA_LINK)) {
1156176139Srwatson		myprintf(("link:   vp fid: %s\n", coda_f2s(&cp->c_fid)));
1157176139Srwatson		myprintf(("link: tdvp fid: %s\n", coda_f2s(&tdcp->c_fid)));
1158176139Srwatson	}
115938625Srvb
1160176139Srwatson	/*
1161176139Srwatson	 * Check for link to/from control object.
1162176139Srwatson	 */
1163176139Srwatson	if (IS_CTL_NAME(tdvp, nm, len) || IS_CTL_VP(vp)) {
1164176139Srwatson		MARK_INT_FAIL(CODA_LINK_STATS);
1165176139Srwatson		return (EACCES);
1166176139Srwatson	}
1167176139Srwatson	error = venus_link(vtomi(vp), &cp->c_fid, &tdcp->c_fid, nm, len,
1168176139Srwatson	    cred, td->td_proc);
116938625Srvb
1170176139Srwatson	/*
1171176139Srwatson	 * Invalidate the parent's attr cache, the modification time has
1172176139Srwatson	 * changed.
1173176139Srwatson	 */
1174176139Srwatson	VTOC(tdvp)->c_flags &= ~C_VATTR;
1175176139Srwatson	VTOC(vp)->c_flags &= ~C_VATTR;
1176176139Srwatson	CODADEBUG(CODA_LINK, myprintf(("in link result %d\n",error)););
1177176139Srwatson	return (error);
117838625Srvb}
117938625Srvb
118038625Srvbint
1181138290Sphkcoda_rename(struct vop_rename_args *ap)
118238625Srvb{
1183176139Srwatson	/* true args */
1184176238Srwatson	struct vnode *fvp = ap->a_fvp;
1185176238Srwatson	struct vnode *tvp = ap->a_tvp;
1186176139Srwatson	struct vnode *odvp = ap->a_fdvp;
1187176139Srwatson	struct cnode *odcp = VTOC(odvp);
1188176139Srwatson	struct componentname  *fcnp = ap->a_fcnp;
1189176139Srwatson	struct vnode *ndvp = ap->a_tdvp;
1190176139Srwatson	struct cnode *ndcp = VTOC(ndvp);
1191176139Srwatson	struct componentname  *tcnp = ap->a_tcnp;
1192176139Srwatson	struct ucred *cred = fcnp->cn_cred;
1193176139Srwatson	struct thread *td = fcnp->cn_thread;
1194176139Srwatson	/* true args */
1195176139Srwatson	int error;
1196176139Srwatson	const char *fnm = fcnp->cn_nameptr;
1197176139Srwatson	int flen = fcnp->cn_namelen;
1198176139Srwatson	const char *tnm = tcnp->cn_nameptr;
1199176139Srwatson	int tlen = tcnp->cn_namelen;
120038625Srvb
1201176139Srwatson	MARK_ENTRY(CODA_RENAME_STATS);
120238625Srvb
1203176139Srwatson	/*
1204176139Srwatson	 * Check for rename involving control object.
1205176139Srwatson	 */
1206176139Srwatson	if (IS_CTL_NAME(odvp, fnm, flen) || IS_CTL_NAME(ndvp, tnm, tlen)) {
1207176139Srwatson		MARK_INT_FAIL(CODA_RENAME_STATS);
1208176139Srwatson		return (EACCES);
120938625Srvb	}
121038625Srvb
1211176139Srwatson	/*
1212176233Srwatson	 * Remove the entries for both source and target directories, which
1213176233Srwatson	 * should catch references to the children.  Perhaps we could purge
1214176233Srwatson	 * less?
1215176139Srwatson	 */
1216176233Srwatson	cache_purge(odvp);
1217176233Srwatson	cache_purge(ndvp);
121838625Srvb
1219176139Srwatson	/*
1220176238Srwatson	 * Invalidate parent directories as modification times have changed.
1221176238Srwatson	 * Invalidate access cache on renamed file as rights may have
1222176139Srwatson	 * changed.
1223176139Srwatson	 */
1224176139Srwatson	VTOC(odvp)->c_flags &= ~C_VATTR;
1225176139Srwatson	VTOC(ndvp)->c_flags &= ~C_VATTR;
1226176238Srwatson	VTOC(fvp)->c_flags &= ~C_ACCCACHE;
1227176139Srwatson	if (flen+1 > CODA_MAXNAMLEN) {
1228176139Srwatson		MARK_INT_FAIL(CODA_RENAME_STATS);
1229176139Srwatson		error = EINVAL;
1230176139Srwatson		goto exit;
1231176139Srwatson	}
1232176139Srwatson	if (tlen+1 > CODA_MAXNAMLEN) {
1233176139Srwatson		MARK_INT_FAIL(CODA_RENAME_STATS);
1234176139Srwatson		error = EINVAL;
1235176139Srwatson		goto exit;
1236176139Srwatson	}
1237176139Srwatson	error = venus_rename(vtomi(odvp), &odcp->c_fid, &ndcp->c_fid, fnm,
1238176139Srwatson	    flen, tnm, tlen, cred, td->td_proc);
1239176139Srwatsonexit:
1240176139Srwatson	CODADEBUG(CODA_RENAME, myprintf(("in rename result %d\n",error)););
124138625Srvb
1242176139Srwatson	/*
1243176238Srwatson	 * Update namecache to reflect that the names of various objects may
1244176238Srwatson	 * have changed (or gone away entirely).
1245176139Srwatson	 */
1246176238Srwatson	cache_purge(fvp);
1247176238Srwatson	cache_purge(tvp);
124838625Srvb
1249176139Srwatson	/*
1250176139Srwatson	 * Release parents first, then children.
1251176139Srwatson	 */
1252176139Srwatson	vrele(odvp);
1253176238Srwatson	if (tvp) {
1254176238Srwatson		if (tvp == ndvp)
1255176139Srwatson			vrele(ndvp);
1256176139Srwatson		else
1257176139Srwatson			vput(ndvp);
1258176238Srwatson		vput(tvp);
1259176139Srwatson	} else
1260176139Srwatson		vput(ndvp);
1261176238Srwatson	vrele(fvp);
1262176139Srwatson	return (error);
126338625Srvb}
126438625Srvb
126538625Srvbint
1266138290Sphkcoda_mkdir(struct vop_mkdir_args *ap)
126738625Srvb{
1268176139Srwatson	/* true args */
1269176139Srwatson	struct vnode *dvp = ap->a_dvp;
1270176139Srwatson	struct cnode *dcp = VTOC(dvp);
1271176139Srwatson	struct componentname  *cnp = ap->a_cnp;
1272176139Srwatson	struct vattr *va = ap->a_vap;
1273176139Srwatson	struct vnode **vpp = ap->a_vpp;
1274176139Srwatson	struct ucred *cred = cnp->cn_cred;
1275176139Srwatson	struct thread *td = cnp->cn_thread;
1276176139Srwatson	/* locals */
1277176139Srwatson	int error;
1278176139Srwatson	const char *nm = cnp->cn_nameptr;
1279176139Srwatson	int len = cnp->cn_namelen;
1280176139Srwatson	struct cnode *cp;
1281176139Srwatson	CodaFid VFid;
1282176139Srwatson	struct vattr ova;
128338625Srvb
1284176139Srwatson	MARK_ENTRY(CODA_MKDIR_STATS);
128538625Srvb
1286176139Srwatson	/*
1287176139Srwatson	 * Check for mkdir of target object.
1288176139Srwatson	 */
1289176139Srwatson	if (IS_CTL_NAME(dvp, nm, len)) {
1290176139Srwatson		*vpp = (struct vnode *)0;
1291176139Srwatson		MARK_INT_FAIL(CODA_MKDIR_STATS);
1292176139Srwatson		return (EACCES);
129338625Srvb	}
1294176139Srwatson	if (len+1 > CODA_MAXNAMLEN) {
1295176139Srwatson		*vpp = (struct vnode *)0;
1296176139Srwatson		MARK_INT_FAIL(CODA_MKDIR_STATS);
1297176139Srwatson		return (EACCES);
1298176139Srwatson	}
1299176139Srwatson	error = venus_mkdir(vtomi(dvp), &dcp->c_fid, nm, len, va, cred,
1300176139Srwatson	    td->td_proc, &VFid, &ova);
1301176139Srwatson	if (!error) {
1302176139Srwatson		if (coda_find(&VFid) != NULL)
1303176139Srwatson			panic("cnode existed for newly created directory!");
1304176139Srwatson		cp =  make_coda_node(&VFid, dvp->v_mount, va->va_type);
1305176139Srwatson		*vpp = CTOV(cp);
130638625Srvb
1307176139Srwatson		/*
1308176139Srwatson		 * Enter the new vnode in the Name Cache.
1309176139Srwatson		 */
1310176233Srwatson		if (cnp->cn_flags & MAKEENTRY)
1311176233Srwatson			cache_enter(dvp, *vpp, cnp);
1312175474Srwatson
1313176139Srwatson		/*
1314176233Srwatson		 * Update the attr cache and mark as valid.
1315176139Srwatson		 */
1316176139Srwatson		if (coda_attr_cache) {
1317176139Srwatson			VTOC(*vpp)->c_vattr = ova;
1318176139Srwatson			VTOC(*vpp)->c_flags |= C_VATTR;
1319176139Srwatson		}
1320175474Srwatson
1321176139Srwatson		/*
1322176139Srwatson		 * Invalidate the parent's attr cache, the modification time
1323176139Srwatson		 * has changed.
1324176139Srwatson		 */
1325176139Srwatson		VTOC(dvp)->c_flags &= ~C_VATTR;
1326176139Srwatson		vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
1327176139Srwatson		CODADEBUG( CODA_MKDIR, myprintf(("mkdir: %s result %d\n",
1328176139Srwatson		    coda_f2s(&VFid), error)););
1329119832Stjr	} else {
1330176139Srwatson		*vpp = NULL;
1331176139Srwatson		CODADEBUG(CODA_MKDIR, myprintf(("mkdir error %d\n",error)););
1332176139Srwatson	}
1333176139Srwatson	return (error);
133438625Srvb}
133538625Srvb
133638625Srvbint
1337138290Sphkcoda_rmdir(struct vop_rmdir_args *ap)
133838625Srvb{
1339176139Srwatson	/* true args */
1340176233Srwatson	struct vnode *vp = ap->a_vp;
1341176139Srwatson	struct vnode *dvp = ap->a_dvp;
1342176139Srwatson	struct cnode *dcp = VTOC(dvp);
1343176139Srwatson	struct componentname  *cnp = ap->a_cnp;
1344176139Srwatson	struct ucred *cred = cnp->cn_cred;
1345176139Srwatson	struct thread *td = cnp->cn_thread;
1346176139Srwatson	/* true args */
1347176139Srwatson	int error;
1348176139Srwatson	const char *nm = cnp->cn_nameptr;
1349176139Srwatson	int len = cnp->cn_namelen;
1350176233Srwatson#if 0
1351176139Srwatson	struct cnode *cp;
1352176233Srwatson#endif
135338625Srvb
1354176139Srwatson	MARK_ENTRY(CODA_RMDIR_STATS);
135538625Srvb
1356176139Srwatson	/*
1357176139Srwatson	 * Check for rmdir of control object.
1358176139Srwatson	 */
1359176139Srwatson	if (IS_CTL_NAME(dvp, nm, len)) {
1360176139Srwatson		MARK_INT_FAIL(CODA_RMDIR_STATS);
1361176139Srwatson		return (ENOENT);
1362176139Srwatson	}
136338625Srvb
1364176139Srwatson	/*
1365176233Srwatson	 * Possibly somewhat conservative purging, perhaps we just need to
1366176233Srwatson	 * purge vp?
1367176139Srwatson	 */
1368176233Srwatson	cache_purge(dvp);
1369176233Srwatson	cache_purge(vp);
137038625Srvb
1371176139Srwatson	/*
1372176139Srwatson	 * Invalidate the parent's attr cache, the modification time has
1373176139Srwatson	 * changed.
1374176139Srwatson	 */
1375176139Srwatson	dcp->c_flags &= ~C_VATTR;
1376176139Srwatson	error = venus_rmdir(vtomi(dvp), &dcp->c_fid, nm, len, cred,
1377176139Srwatson	    td->td_proc);
1378176139Srwatson	CODADEBUG(CODA_RMDIR, myprintf(("in rmdir result %d\n", error)););
1379176139Srwatson	return (error);
138038625Srvb}
138138625Srvb
138238625Srvbint
1383138290Sphkcoda_symlink(struct vop_symlink_args *ap)
138438625Srvb{
1385176139Srwatson	/* true args */
1386176139Srwatson	struct vnode *tdvp = ap->a_dvp;
1387176139Srwatson	struct cnode *tdcp = VTOC(tdvp);
1388176139Srwatson	struct componentname *cnp = ap->a_cnp;
1389176139Srwatson	struct vattr *tva = ap->a_vap;
1390176139Srwatson	char *path = ap->a_target;
1391176139Srwatson	struct ucred *cred = cnp->cn_cred;
1392176139Srwatson	struct thread *td = cnp->cn_thread;
1393176139Srwatson	struct vnode **vpp = ap->a_vpp;
1394176139Srwatson	/* locals */
1395176139Srwatson	int error;
139638625Srvb
1397176139Srwatson	/*-
1398176139Srwatson	 * XXX I'm assuming the following things about coda_symlink's
1399176139Srwatson	 * arguments:
1400176139Srwatson	 *       t(foo) is the new name/parent/etc being created.
1401176139Srwatson	 *       lname is the contents of the new symlink.
1402176139Srwatson	 */
1403176139Srwatson	char *nm = cnp->cn_nameptr;
1404176139Srwatson	int len = cnp->cn_namelen;
1405176139Srwatson	int plen = strlen(path);
140638625Srvb
1407176139Srwatson	/*
1408176139Srwatson	 * Here's the strategy for the moment: perform the symlink, then do a
1409176139Srwatson	 * lookup to grab the resulting vnode.  I know this requires two
1410176139Srwatson	 * communications with Venus for a new sybolic link, but that's the
1411176139Srwatson	 * way the ball bounces.  I don't yet want to change the way the Mach
1412176139Srwatson	 * symlink works.  When Mach support is deprecated, we should change
1413176139Srwatson	 * symlink so that the common case returns the resultant vnode in a
1414176139Srwatson	 * vpp argument.
1415176139Srwatson	 */
1416176139Srwatson	MARK_ENTRY(CODA_SYMLINK_STATS);
141738625Srvb
1418176139Srwatson	/*
1419176139Srwatson	 * Check for symlink of control object.
1420176139Srwatson	 */
1421176139Srwatson	if (IS_CTL_NAME(tdvp, nm, len)) {
1422176139Srwatson		MARK_INT_FAIL(CODA_SYMLINK_STATS);
1423176139Srwatson		return (EACCES);
1424176139Srwatson	}
1425176139Srwatson	if (plen+1 > CODA_MAXPATHLEN) {
1426176139Srwatson		MARK_INT_FAIL(CODA_SYMLINK_STATS);
1427176139Srwatson		return (EINVAL);
1428176139Srwatson	}
1429176139Srwatson	if (len+1 > CODA_MAXNAMLEN) {
1430176139Srwatson		MARK_INT_FAIL(CODA_SYMLINK_STATS);
1431176139Srwatson		error = EINVAL;
1432176139Srwatson		goto exit;
1433176139Srwatson	}
1434176139Srwatson	error = venus_symlink(vtomi(tdvp), &tdcp->c_fid, path, plen, nm, len,
1435176139Srwatson	    tva, cred, td->td_proc);
143638625Srvb
1437176139Srwatson	/*
1438176139Srwatson	 * Invalidate the parent's attr cache, the modification time has
1439176139Srwatson	 * changed.
1440176139Srwatson	 */
1441176139Srwatson	tdcp->c_flags &= ~C_VATTR;
1442176139Srwatson	if (error == 0)
1443176139Srwatson		error = VOP_LOOKUP(tdvp, vpp, cnp);
1444176139Srwatsonexit:
1445176139Srwatson	CODADEBUG(CODA_SYMLINK, myprintf(("in symlink result %d\n",error)););
1446176139Srwatson	return (error);
144738625Srvb}
144838625Srvb
144938625Srvb/*
145038625Srvb * Read directory entries.
1451176131Srwatson *
1452176139Srwatson * XXX: This forwards the operator straight to the cache vnode using
1453176131Srwatson * VOP_READDIR(), rather than calling venus_readdir().  Why?
145438625Srvb */
145538625Srvbint
1456138290Sphkcoda_readdir(struct vop_readdir_args *ap)
145738625Srvb{
1458176139Srwatson	/* true args */
1459176139Srwatson	struct vnode *vp = ap->a_vp;
1460176139Srwatson	struct cnode *cp = VTOC(vp);
1461176139Srwatson	struct uio *uiop = ap->a_uio;
1462176139Srwatson	struct ucred *cred = ap->a_cred;
1463176139Srwatson	int *eofflag = ap->a_eofflag;
1464176139Srwatson	u_long **cookies = ap->a_cookies;
1465176139Srwatson	int *ncookies = ap->a_ncookies;
1466176139Srwatson	struct thread *td = ap->a_uio->uio_td;
1467176139Srwatson	/* upcall decl */
1468176139Srwatson	/* locals */
1469176139Srwatson	int error = 0;
1470176139Srwatson	int opened_internally = 0;
147138625Srvb
1472176139Srwatson	MARK_ENTRY(CODA_READDIR_STATS);
1473176139Srwatson	CODADEBUG(CODA_READDIR, myprintf(("coda_readdir(%p, %d, %lld, %d)\n",
1474176139Srwatson	    (void *)uiop->uio_iov->iov_base, uiop->uio_resid,
1475176139Srwatson	    (long long)uiop->uio_offset, uiop->uio_segflg)););
147638625Srvb
1477176139Srwatson	/*
1478176139Srwatson	 * Check for readdir of control object.
1479176139Srwatson	 */
1480176139Srwatson	if (IS_CTL_VP(vp)) {
1481176139Srwatson		MARK_INT_FAIL(CODA_READDIR_STATS);
1482176139Srwatson		return (ENOENT);
1483176139Srwatson	}
148438625Srvb
1485176139Srwatson	/*
1486176139Srwatson	 * If directory is not already open do an "internal open" on it.
1487176139Srwatson	 *
1488176139Srwatson	 * XXX: Why would this happen?  For files, there's memory mapping,
1489176139Srwatson	 * execution, and other kernel access paths such as ktrace.  For
1490176139Srwatson	 * directories, it is less clear.
1491176139Srwatson	 */
149238625Srvb	if (cp->c_ovp == NULL) {
1493176139Srwatson		opened_internally = 1;
1494176139Srwatson		MARK_INT_GEN(CODA_OPEN_STATS);
1495176139Srwatson		error = VOP_OPEN(vp, FREAD, cred, td, NULL);
1496176139Srwatson		printf("coda_readdir: Internally Opening %p\n", vp);
1497176139Srwatson		if (error) {
1498176139Srwatson			printf("coda_readdir: VOP_OPEN on container failed "
1499176139Srwatson			   "%d\n", error);
1500176139Srwatson			return (error);
1501176139Srwatson		}
150238625Srvb	}
1503176139Srwatson
1504176139Srwatson	/*
1505176139Srwatson	 * Have UFS handle the call.
1506176139Srwatson	 */
1507176139Srwatson	CODADEBUG(CODA_READDIR, myprintf(("indirect readdir: fid = %s, "
1508176139Srwatson	    "refcnt = %d\n", coda_f2s(&cp->c_fid), vp->v_usecount)););
1509188588Sjhb	vn_lock(cp->c_ovp, LK_SHARED | LK_RETRY);
151038625Srvb	error = VOP_READDIR(cp->c_ovp, uiop, cred, eofflag, ncookies,
1511176139Srwatson	    cookies);
1512176118Srwatson	VOP_UNLOCK(cp->c_ovp, 0);
151338625Srvb	if (error)
1514176139Srwatson		MARK_INT_FAIL(CODA_READDIR_STATS);
151538625Srvb	else
1516176139Srwatson		MARK_INT_SAT(CODA_READDIR_STATS);
1517176139Srwatson
1518176139Srwatson	/*
1519176139Srwatson	 * Do an "internal close" if necessary.
1520176139Srwatson	 */
152138625Srvb	if (opened_internally) {
1522176139Srwatson		MARK_INT_GEN(CODA_CLOSE_STATS);
1523176139Srwatson		(void)VOP_CLOSE(vp, FREAD, cred, td);
152438625Srvb	}
1525176139Srwatson	return (error);
152638625Srvb}
152738625Srvb
152838625Srvbint
1529138290Sphkcoda_reclaim(struct vop_reclaim_args *ap)
153038625Srvb{
1531176139Srwatson	/* true args */
1532176139Srwatson	struct vnode *vp = ap->a_vp;
1533176139Srwatson	struct cnode *cp = VTOC(vp);
1534176139Srwatson	/* upcall decl */
1535176139Srwatson	/* locals */
153638625Srvb
1537176139Srwatson	/*
1538176139Srwatson	 * Forced unmount/flush will let vnodes with non-zero use be
1539176139Srwatson	 * destroyed!
1540176139Srwatson	 */
1541176139Srwatson	ENTRY;
154238625Srvb
1543176139Srwatson	if (IS_UNMOUNTING(cp)) {
154438625Srvb#ifdef	DEBUG
1545176139Srwatson		if (VTOC(vp)->c_ovp) {
1546176139Srwatson			if (IS_UNMOUNTING(cp))
1547176139Srwatson				printf("coda_reclaim: c_ovp not void: vp "
1548176139Srwatson				    "%p, cp %p\n", vp, cp);
1549176139Srwatson		}
155038625Srvb#endif
1551176139Srwatson	} else {
1552176263Srwatson		if (prtactive && vp->v_usecount != 0)
1553176263Srwatson			vprint("coda_reclaim: pushing active", vp);
1554176139Srwatson	}
1555176139Srwatson	cache_purge(vp);
1556176139Srwatson	coda_free(VTOC(vp));
1557176139Srwatson	vp->v_data = NULL;
1558176139Srwatson	vp->v_object = NULL;
1559176139Srwatson	return (0);
156038625Srvb}
156138625Srvb
156238625Srvbint
1563169671Skibcoda_lock(struct vop_lock1_args *ap)
156438625Srvb{
1565176139Srwatson	/* true args */
1566176139Srwatson	struct vnode *vp = ap->a_vp;
1567176139Srwatson	struct cnode *cp = VTOC(vp);
1568176139Srwatson	/* upcall decl */
1569176139Srwatson	/* locals */
157038625Srvb
1571176139Srwatson	ENTRY;
1572176139Srwatson	if ((ap->a_flags & LK_INTERLOCK) == 0) {
1573176139Srwatson		VI_LOCK(vp);
1574176139Srwatson		ap->a_flags |= LK_INTERLOCK;
1575176139Srwatson	}
1576176139Srwatson	if (coda_lockdebug)
1577176139Srwatson		myprintf(("Attempting lock on %s\n", coda_f2s(&cp->c_fid)));
1578176139Srwatson	return (vop_stdlock(ap));
157938625Srvb}
158038625Srvb
158138625Srvbint
1582138290Sphkcoda_unlock(struct vop_unlock_args *ap)
158338625Srvb{
1584176139Srwatson	/* true args */
1585176139Srwatson	struct vnode *vp = ap->a_vp;
1586176139Srwatson	struct cnode *cp = VTOC(vp);
1587176139Srwatson	/* upcall decl */
1588176139Srwatson	/* locals */
158938625Srvb
1590176139Srwatson	ENTRY;
1591176139Srwatson	if (coda_lockdebug)
1592176139Srwatson		myprintf(("Attempting unlock on %s\n",
1593176139Srwatson		    coda_f2s(&cp->c_fid)));
1594176139Srwatson	return (vop_stdunlock(ap));
159538625Srvb}
159638625Srvb
159738625Srvbint
1598138290Sphkcoda_islocked(struct vop_islocked_args *ap)
159938625Srvb{
1600176139Srwatson	/* true args */
160138625Srvb
1602176139Srwatson	ENTRY;
1603176139Srwatson	return (vop_stdislocked(ap));
160438625Srvb}
160538625Srvb
1606176131Srwatsonstatic void
1607176131Srwatsoncoda_print_vattr(struct vattr *attr)
160838625Srvb{
1609176139Srwatson	char *typestr;
161038625Srvb
1611176139Srwatson	switch (attr->va_type) {
1612176139Srwatson	case VNON:
1613176139Srwatson		typestr = "VNON";
1614176139Srwatson		break;
161538625Srvb
1616176139Srwatson	case VREG:
1617176139Srwatson		typestr = "VREG";
1618176139Srwatson		break;
161938625Srvb
1620176139Srwatson	case VDIR:
1621176139Srwatson		typestr = "VDIR";
1622176139Srwatson		break;
162338625Srvb
1624176139Srwatson	case VBLK:
1625176139Srwatson		typestr = "VBLK";
1626176139Srwatson		break;
1627176139Srwatson
1628176139Srwatson	case VCHR:
1629176139Srwatson		typestr = "VCHR";
1630176139Srwatson		break;
1631176139Srwatson
1632176139Srwatson	case VLNK:
1633176139Srwatson		typestr = "VLNK";
1634176139Srwatson		break;
1635176139Srwatson
1636176139Srwatson	case VSOCK:
1637176139Srwatson		typestr = "VSCK";
1638176139Srwatson		break;
1639176139Srwatson
1640176139Srwatson	case VFIFO:
1641176139Srwatson		typestr = "VFFO";
1642176139Srwatson		break;
1643176139Srwatson
1644176139Srwatson	case VBAD:
1645176139Srwatson		typestr = "VBAD";
1646176139Srwatson		break;
1647176139Srwatson
1648176139Srwatson	default:
1649176139Srwatson		typestr = "????";
1650176139Srwatson		break;
1651176139Srwatson	}
1652176139Srwatson	myprintf(("attr: type %s mode %d uid %d gid %d fsid %d rdev %d\n",
1653176139Srwatson	    typestr, (int)attr->va_mode, (int)attr->va_uid,
1654176139Srwatson	    (int)attr->va_gid, (int)attr->va_fsid, (int)attr->va_rdev));
1655176139Srwatson	myprintf(("      fileid %d nlink %d size %d blocksize %d bytes %d\n",
1656176139Srwatson	    (int)attr->va_fileid, (int)attr->va_nlink, (int)attr->va_size,
1657176139Srwatson	    (int)attr->va_blocksize,(int)attr->va_bytes));
1658176139Srwatson	myprintf(("      gen %ld flags %ld vaflags %d\n", attr->va_gen,
1659176139Srwatson	    attr->va_flags, attr->va_vaflags));
1660176139Srwatson	myprintf(("      atime sec %d nsec %d\n", (int)attr->va_atime.tv_sec,
1661176139Srwatson	    (int)attr->va_atime.tv_nsec));
1662176139Srwatson	myprintf(("      mtime sec %d nsec %d\n", (int)attr->va_mtime.tv_sec,
1663176139Srwatson	    (int)attr->va_mtime.tv_nsec));
1664176139Srwatson	myprintf(("      ctime sec %d nsec %d\n", (int)attr->va_ctime.tv_sec,
1665176139Srwatson	    (int)attr->va_ctime.tv_nsec));
166638625Srvb}
166738625Srvb
1668176139Srwatson/*
1669176139Srwatson * How to print a ucred.
1670176139Srwatson */
167138625Srvbvoid
1672176131Srwatsoncoda_print_cred(struct ucred *cred)
167338625Srvb{
167438625Srvb	int i;
167538625Srvb
167638625Srvb	myprintf(("ref %d\tuid %d\n",cred->cr_ref,cred->cr_uid));
167738625Srvb	for (i=0; i < cred->cr_ngroups; i++)
167838625Srvb		myprintf(("\tgroup %d: (%d)\n",i,cred->cr_groups[i]));
167938625Srvb	myprintf(("\n"));
168038625Srvb}
168138625Srvb
168238625Srvb/*
1683176139Srwatson * Return a vnode for the given fid.  If no cnode exists for this fid create
1684176139Srwatson * one and put it in a table hashed by coda_f2i().  If the cnode for this fid
1685176139Srwatson * is already in the table return it (ref count is incremented by coda_find.
1686176139Srwatson * The cnode will be flushed from the table when coda_inactive calls
1687176139Srwatson * coda_unsave.
168838625Srvb */
168938625Srvbstruct cnode *
1690154647Srwatsonmake_coda_node(CodaFid *fid, struct mount *vfsp, short type)
169138625Srvb{
1692176139Srwatson	struct cnode *cp;
1693176307Srwatson	struct vnode *vp;
1694176139Srwatson	int err;
169538625Srvb
1696176307Srwatson	/*
1697176307Srwatson	 * XXXRW: This really needs a moderate amount of reworking.  We need
1698176307Srwatson	 * to properly tolerate failures of getnewvnode() and insmntque(),
1699176307Srwatson	 * and callers need to be able to accept an error back from
1700176307Srwatson	 * make_coda_node.  There may also be more general issues in how we
1701176307Srwatson	 * handle forced unmount.  Finally, if/when Coda loses its dependency
1702176307Srwatson	 * on Giant, the ordering of this needs rethinking.
1703176307Srwatson	 */
1704176307Srwatson	cp = coda_find(fid);
1705176307Srwatson	if (cp != NULL) {
1706176139Srwatson		vref(CTOV(cp));
1707176307Srwatson		return (cp);
1708176307Srwatson	}
1709176307Srwatson	cp = coda_alloc();
1710176307Srwatson	cp->c_fid = *fid;
1711176307Srwatson	err = getnewvnode("coda", vfsp, &coda_vnodeops, &vp);
1712176307Srwatson	if (err)
1713176307Srwatson		panic("coda: getnewvnode returned error %d\n", err);
1714176307Srwatson	vp->v_data = cp;
1715176307Srwatson	vp->v_type = type;
1716176307Srwatson	cp->c_vnode = vp;
1717176307Srwatson	coda_save(cp);
1718176307Srwatson	err = insmntque(vp, vfsp);
1719176307Srwatson	if (err != 0)
1720176307Srwatson		printf("coda: insmntque failed: error %d", err);
1721176139Srwatson	return (cp);
172238625Srvb}
1723111903Stjr
1724111903Stjrint
1725154647Srwatsoncoda_pathconf(struct vop_pathconf_args *ap)
1726111903Stjr{
1727111903Stjr
1728111903Stjr	switch (ap->a_name) {
1729111903Stjr	case _PC_NAME_MAX:
1730176156Srwatson		*ap->a_retval = CODA_MAXNAMLEN;
1731176156Srwatson		return (0);
1732176156Srwatson
1733111903Stjr	case _PC_PATH_MAX:
1734176156Srwatson		*ap->a_retval = CODA_MAXPATHLEN;
1735176156Srwatson		return (0);
1736176156Srwatson
1737111903Stjr	default:
1738176156Srwatson		return (vop_stdpathconf(ap));
1739111903Stjr	}
1740111903Stjr}
1741