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$");
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);
334194990Skib	CODADEBUG(CODA_RDWR, myprintf(("coda_rdwr(%d, %p, %zd, %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	}
474210997Srwatson	if (iap->vi.in_size > VC_MAXDATASIZE ||
475210997Srwatson	    iap->vi.out_size > VC_MAXDATASIZE) {
476176139Srwatson		NDFREE(&ndp, 0);
477176139Srwatson		return (EINVAL);
478176139Srwatson	}
479176139Srwatson	error = venus_ioctl(vtomi(tvp), &((VTOC(tvp))->c_fid), com, flag,
480176139Srwatson	    data, cred, td->td_proc);
481176139Srwatson	if (error)
482176139Srwatson		MARK_INT_FAIL(CODA_IOCTL_STATS);
483176139Srwatson	else
484176139Srwatson		CODADEBUG(CODA_IOCTL, myprintf(("Ioctl returns %d \n",
485176139Srwatson		    error)););
48638625Srvb	vrele(tvp);
48754655Seivind	NDFREE(&ndp, NDF_ONLY_PNBUF);
488176139Srwatson	return (error);
48938625Srvb}
49038625Srvb
49138625Srvb/*
492176139Srwatson * To reduce the cost of a user-level venus;we cache attributes in the
493176139Srwatson * kernel.  Each cnode has storage allocated for an attribute.  If c_vattr is
494176139Srwatson * valid, return a reference to it.  Otherwise, get the attributes from venus
495176139Srwatson * and store them in the cnode.  There is some question if this method is a
496176139Srwatson * security leak.  But I think that in order to make this call, the user must
497176139Srwatson * have done a lookup and opened the file, and therefore should already have
498176139Srwatson * access.
49938625Srvb */
50038625Srvbint
501138290Sphkcoda_getattr(struct vop_getattr_args *ap)
50238625Srvb{
503176139Srwatson	/* true args */
504176139Srwatson	struct vnode *vp = ap->a_vp;
505176139Srwatson	struct cnode *cp = VTOC(vp);
506176139Srwatson	struct vattr *vap = ap->a_vap;
507176139Srwatson	struct ucred *cred = ap->a_cred;
508176139Srwatson	/* locals */
509176139Srwatson    	struct vnode *convp;
510176139Srwatson	int error, size;
51138625Srvb
512176139Srwatson	MARK_ENTRY(CODA_GETATTR_STATS);
513176139Srwatson	if (IS_UNMOUNTING(cp))
514176139Srwatson		return (ENODEV);
51538625Srvb
516176139Srwatson	/*
517176139Srwatson	 * Check for getattr of control object.
518176139Srwatson	 */
519176139Srwatson	if (IS_CTL_VP(vp)) {
520176139Srwatson		MARK_INT_FAIL(CODA_GETATTR_STATS);
521176139Srwatson		return (ENOENT);
522176139Srwatson	}
52338759Srvb
524176139Srwatson	/*
525176139Srwatson	 * Check to see if the attributes have already been cached.
526176139Srwatson	 */
527176139Srwatson	if (VALID_VATTR(cp)) {
528176139Srwatson		CODADEBUG(CODA_GETATTR, myprintf(("attr cache hit: %s\n",
529176139Srwatson		    coda_f2s(&cp->c_fid))););
530176139Srwatson		CODADEBUG(CODA_GETATTR, if (!(codadebug & ~CODA_GETATTR))
531176139Srwatson		    coda_print_vattr(&cp->c_vattr););
532176139Srwatson		*vap = cp->c_vattr;
533176139Srwatson		MARK_INT_SAT(CODA_GETATTR_STATS);
534176139Srwatson		return (0);
535176139Srwatson	}
536182371Sattilio    	error = venus_getattr(vtomi(vp), &cp->c_fid, cred, vap);
537176139Srwatson	if (!error) {
538176139Srwatson		CODADEBUG(CODA_GETATTR, myprintf(("getattr miss %s: result "
539176139Srwatson		    "%d\n", coda_f2s(&cp->c_fid), error)););
540176139Srwatson		CODADEBUG(CODA_GETATTR, if (!(codadebug & ~CODA_GETATTR))
541176139Srwatson		    coda_print_vattr(vap););
54238625Srvb
543176139Srwatson		/*
544176139Srwatson		 * XXX: Since we now share vm objects between layers, this is
545176139Srwatson		 * probably unnecessary.
546176139Srwatson		 */
547176139Srwatson		size = vap->va_size;
548176139Srwatson    		convp = cp->c_ovp;
549176139Srwatson		if (convp != NULL)
550176139Srwatson			vnode_pager_setsize(convp, size);
55138625Srvb
552176139Srwatson		/*
553176139Srwatson		 * If not open for write, store attributes in cnode.
554176139Srwatson		 */
555176139Srwatson		if ((cp->c_owrite == 0) && (coda_attr_cache)) {
556176139Srwatson			cp->c_vattr = *vap;
557176139Srwatson			cp->c_flags |= C_VATTR;
558176139Srwatson		}
55938625Srvb	}
560176139Srwatson	return (error);
56138625Srvb}
56238625Srvb
56338625Srvbint
564138290Sphkcoda_setattr(struct vop_setattr_args *ap)
56538625Srvb{
566176139Srwatson	/* true args */
567176139Srwatson	struct vnode *vp = ap->a_vp;
568176139Srwatson	struct cnode *cp = VTOC(vp);
569176139Srwatson	struct vattr *vap = ap->a_vap;
570176139Srwatson	struct ucred *cred = ap->a_cred;
571176139Srwatson	/* locals */
572176139Srwatson    	struct vnode *convp;
573176139Srwatson	int error, size;
57438625Srvb
575176139Srwatson	MARK_ENTRY(CODA_SETATTR_STATS);
57638625Srvb
577176139Srwatson	/*
578176139Srwatson	 * Check for setattr of control object.
579176139Srwatson	 */
580176139Srwatson	if (IS_CTL_VP(vp)) {
581176139Srwatson		MARK_INT_FAIL(CODA_SETATTR_STATS);
582176139Srwatson		return (ENOENT);
583176139Srwatson	}
584176139Srwatson	if (codadebug & CODADBGMSK(CODA_SETATTR))
585176139Srwatson		coda_print_vattr(vap);
586182371Sattilio	error = venus_setattr(vtomi(vp), &cp->c_fid, vap, cred);
587176139Srwatson	if (!error)
588176238Srwatson		cp->c_flags &= ~(C_VATTR | C_ACCCACHE);
58938625Srvb
590176139Srwatson	/*
591176139Srwatson	 * XXX: Since we now share vm objects between layers, this is
592176139Srwatson	 * probably unnecessary.
593176139Srwatson	 *
594176139Srwatson	 * XXX: Shouldn't we only be doing this "set" if C_VATTR remains
595176139Srwatson	 * valid after venus_setattr()?
596176139Srwatson	 */
597176139Srwatson	size = vap->va_size;
598176139Srwatson    	convp = cp->c_ovp;
599176139Srwatson	if (size != VNOVAL && convp != NULL)
600176139Srwatson		vnode_pager_setsize(convp, size);
601176139Srwatson	CODADEBUG(CODA_SETATTR,	myprintf(("setattr %d\n", error)););
602176139Srwatson	return (error);
60338625Srvb}
60438625Srvb
60538625Srvbint
606138290Sphkcoda_access(struct vop_access_args *ap)
60738625Srvb{
608176139Srwatson	/* true args */
609176139Srwatson	struct vnode *vp = ap->a_vp;
610176139Srwatson	struct cnode *cp = VTOC(vp);
611184413Strasz	accmode_t accmode = ap->a_accmode;
612176139Srwatson	struct ucred *cred = ap->a_cred;
613176139Srwatson	struct thread *td = ap->a_td;
614176139Srwatson	/* locals */
615176238Srwatson	int error;
61638625Srvb
617176139Srwatson	MARK_ENTRY(CODA_ACCESS_STATS);
61838625Srvb
619176139Srwatson	/*
620176139Srwatson	 * Check for access of control object.  Only read access is allowed
621176139Srwatson	 * on it.
622176139Srwatson	 */
623176139Srwatson	if (IS_CTL_VP(vp)) {
624176139Srwatson		/*
625176139Srwatson		 * Bogus hack - all will be marked as successes.
626176139Srwatson		 */
62739085Srvb		MARK_INT_SAT(CODA_ACCESS_STATS);
628184413Strasz		return (((accmode & VREAD) && !(accmode & (VWRITE | VEXEC)))
629176139Srwatson		    ? 0 : EACCES);
63038625Srvb	}
63138625Srvb
632176139Srwatson	/*
633176238Srwatson	 * We maintain a one-entry LRU positive access cache with each cnode.
634176238Srwatson	 * In principle we could also track negative results, and for more
635176238Srwatson	 * than one uid, but we don't yet.  Venus is responsible for
636176238Srwatson	 * invalidating this cache as required.
637176139Srwatson	 */
638176238Srwatson	if (coda_access_cache && VALID_ACCCACHE(cp) &&
639176238Srwatson	    (cred->cr_uid == cp->c_cached_uid) &&
640184413Strasz	    (accmode & cp->c_cached_mode) == accmode) {
641176238Srwatson		MARK_INT_SAT(CODA_ACCESS_STATS);
642176238Srwatson		return (0);
643176238Srwatson	}
644184413Strasz	error = venus_access(vtomi(vp), &cp->c_fid, accmode, cred, td->td_proc);
645176238Srwatson	if (error == 0 && coda_access_cache) {
646176238Srwatson		/*-
647176238Srwatson		 * When we have a new successful request, we consider three
648176238Srwatson		 * cases:
649176238Srwatson		 *
650176238Srwatson		 * - No initialized access cache, in which case cache the
651176238Srwatson		 *   result.
652176238Srwatson		 * - Cached result for a different user, in which case we
653176238Srwatson		 *   replace the entry.
654176238Srwatson		 * - Cached result for the same user, in which case we add
655176238Srwatson		 *   any newly granted rights to the cached mode.
656176238Srwatson		 *
657176238Srwatson		 * XXXRW: If we ever move to something more interesting than
658176238Srwatson		 * uid-based token lookup, we'll need to change this.
659176238Srwatson		 */
660176238Srwatson		cp->c_flags |= C_ACCCACHE;
661176238Srwatson		if (cp->c_cached_uid != cred->cr_uid) {
662184413Strasz			cp->c_cached_mode = accmode;
663176238Srwatson			cp->c_cached_uid = cred->cr_uid;
664176238Srwatson		} else
665184413Strasz			cp->c_cached_mode |= accmode;
666176238Srwatson	}
667176238Srwatson	return (error);
66838625Srvb}
66938625Srvb
67038625Srvbint
671138290Sphkcoda_readlink(struct vop_readlink_args *ap)
67238625Srvb{
673176139Srwatson	/* true args */
674176139Srwatson	struct vnode *vp = ap->a_vp;
675176139Srwatson	struct cnode *cp = VTOC(vp);
676176139Srwatson	struct uio *uiop = ap->a_uio;
677176139Srwatson	struct ucred *cred = ap->a_cred;
678176139Srwatson	struct thread *td = ap->a_uio->uio_td;
679176139Srwatson	/* locals */
680176139Srwatson	int error;
681176139Srwatson	char *str;
682176139Srwatson	int len;
68338625Srvb
684176139Srwatson	MARK_ENTRY(CODA_READLINK_STATS);
68538625Srvb
686176139Srwatson	/*
687176139Srwatson	 * Check for readlink of control object.
688176139Srwatson	 */
689176139Srwatson	if (IS_CTL_VP(vp)) {
690176139Srwatson		MARK_INT_FAIL(CODA_READLINK_STATS);
691176139Srwatson		return (ENOENT);
692176139Srwatson	}
693176139Srwatson	if ((coda_symlink_cache) && (VALID_SYMLINK(cp))) {
694176139Srwatson		/*
695176139Srwatson		 * Symlink was cached.
696176139Srwatson		 */
697176139Srwatson		uiop->uio_rw = UIO_READ;
698176139Srwatson		error = uiomove(cp->c_symlink, (int)cp->c_symlen, uiop);
699176139Srwatson		if (error)
700176139Srwatson			MARK_INT_FAIL(CODA_READLINK_STATS);
701176139Srwatson		else
702176139Srwatson			MARK_INT_SAT(CODA_READLINK_STATS);
703176139Srwatson		return (error);
704176139Srwatson	}
705176139Srwatson	error = venus_readlink(vtomi(vp), &cp->c_fid, cred, td != NULL ?
706176139Srwatson	    td->td_proc : NULL, &str, &len);
707176139Srwatson	if (!error) {
708176139Srwatson		uiop->uio_rw = UIO_READ;
709176139Srwatson		error = uiomove(str, len, uiop);
710176139Srwatson		if (coda_symlink_cache) {
711176139Srwatson			cp->c_symlink = str;
712176139Srwatson			cp->c_symlen = len;
713176139Srwatson			cp->c_flags |= C_SYMLINK;
714176139Srwatson		} else
715176139Srwatson			CODA_FREE(str, len);
716176139Srwatson	}
717176139Srwatson	CODADEBUG(CODA_READLINK, myprintf(("in readlink result %d\n",
718176139Srwatson	    error)););
719176139Srwatson	return (error);
72038625Srvb}
72138625Srvb
72238625Srvbint
723138290Sphkcoda_fsync(struct vop_fsync_args *ap)
72438625Srvb{
725176139Srwatson	/* true args */
726176139Srwatson	struct vnode *vp = ap->a_vp;
727176139Srwatson	struct cnode *cp = VTOC(vp);
728176139Srwatson	struct thread *td = ap->a_td;
729176139Srwatson	/* locals */
730176139Srwatson	struct vnode *convp = cp->c_ovp;
731176139Srwatson	int error;
73238625Srvb
733176139Srwatson	MARK_ENTRY(CODA_FSYNC_STATS);
73438625Srvb
735176139Srwatson	/*
736176139Srwatson	 * Check for fsync on an unmounting object.
737176139Srwatson	 *
738176139Srwatson	 * XXX: Is this comment true on FreeBSD?  It seems likely, since
739176139Srwatson	 * unmounting is fairly non-atomic.
740176139Srwatson	 *
741176139Srwatson	 * The NetBSD kernel, in it's infinite wisdom, can try to fsync after
742176139Srwatson	 * an unmount has been initiated.  This is a Bad Thing, which we have
743176139Srwatson	 * to avoid.  Not a legitimate failure for stats.
744176139Srwatson	 */
745176139Srwatson	if (IS_UNMOUNTING(cp))
746176139Srwatson		return (ENODEV);
74738625Srvb
748176139Srwatson	/*
749176139Srwatson	 * Check for fsync of control object.
750176139Srwatson	 */
751176139Srwatson	if (IS_CTL_VP(vp)) {
752176139Srwatson		MARK_INT_SAT(CODA_FSYNC_STATS);
753176139Srwatson		return (0);
754176139Srwatson	}
755176139Srwatson	if (convp != NULL) {
756176139Srwatson		vn_lock(convp, LK_EXCLUSIVE | LK_RETRY);
757176139Srwatson		VOP_FSYNC(convp, MNT_WAIT, td);
758176139Srwatson		VOP_UNLOCK(convp, 0);
759176139Srwatson	}
76038625Srvb
761176139Srwatson	/*
762176139Srwatson	 * We see fsyncs with usecount == 1 then usecount == 0.  For now we
763176139Srwatson	 * ignore them.
764176139Srwatson	 */
765176139Srwatson#if 0
766176139Srwatson	VI_LOCK(vp);
767176139Srwatson	if (!vp->v_usecount) {
768176139Srwatson		printf("coda_fsync on vnode %p with %d usecount.  "
769176139Srwatson		    "c_flags = %x (%x)\n", vp, vp->v_usecount, cp->c_flags,
770176139Srwatson		    cp->c_flags&C_PURGING);
771176139Srwatson	}
772176139Srwatson	VI_UNLOCK(vp);
773176139Srwatson#endif
77438625Srvb
775176139Srwatson	/*
776176139Srwatson	 * We can expect fsync on any vnode at all if venus is purging it.
777176139Srwatson	 * Venus can't very well answer the fsync request, now can it?
778176139Srwatson	 * Hopefully, it won't have to, because hopefully, venus preserves
779176139Srwatson	 * the (possibly untrue) invariant that it never purges an open
780176139Srwatson	 * vnode.  Hopefully.
781176139Srwatson	 */
782176139Srwatson	if (cp->c_flags & C_PURGING)
783176139Srwatson		return (0);
78438625Srvb
785176139Srwatson	/* XXX: needs research */
786176139Srwatson	return (0);
787176139Srwatson	error = venus_fsync(vtomi(vp), &cp->c_fid, td->td_proc);
788176139Srwatson	CODADEBUG(CODA_FSYNC, myprintf(("in fsync result %d\n", error)););
789176139Srwatson	return (error);
79038625Srvb}
79138625Srvb
79238625Srvbint
793138290Sphkcoda_inactive(struct vop_inactive_args *ap)
79438625Srvb{
795176139Srwatson	/*
796176139Srwatson	 * XXX - at the moment, inactive doesn't look at cred, and doesn't
797176139Srwatson	 * have a proc pointer.  Oops.
798176139Srwatson	 */
799176139Srwatson	/* true args */
800176139Srwatson	struct vnode *vp = ap->a_vp;
801176139Srwatson	struct cnode *cp = VTOC(vp);
802176139Srwatson	struct ucred *cred __attribute__((unused)) = NULL;
803176139Srwatson	struct thread *td __attribute__((unused)) = curthread;
804176139Srwatson	/* upcall decl */
805176139Srwatson	/* locals */
80638625Srvb
807176139Srwatson	/*
808176139Srwatson	 * We don't need to send inactive to venus - DCS.
809176139Srwatson	 */
810176139Srwatson	MARK_ENTRY(CODA_INACTIVE_STATS);
811176139Srwatson	CODADEBUG(CODA_INACTIVE, myprintf(("in inactive, %s, vfsp %p\n",
812176139Srwatson	    coda_f2s(&cp->c_fid), vp->v_mount)););
813176139Srwatson	vp->v_object = NULL;
81438625Srvb
815176139Srwatson	/*
816176139Srwatson	 * If an array has been allocated to hold the symlink, deallocate it.
817176139Srwatson	 */
818176139Srwatson	if ((coda_symlink_cache) && (VALID_SYMLINK(cp))) {
819176139Srwatson		if (cp->c_symlink == NULL)
820176139Srwatson			panic("coda_inactive: null symlink pointer in cnode");
821176139Srwatson		CODA_FREE(cp->c_symlink, cp->c_symlen);
822176139Srwatson		cp->c_flags &= ~C_SYMLINK;
823176139Srwatson		cp->c_symlen = 0;
824176139Srwatson	}
825175473Srwatson
826176139Srwatson	/*
827176139Srwatson	 * Remove it from the table so it can't be found.
828176139Srwatson	 */
829176139Srwatson	coda_unsave(cp);
830176139Srwatson	if ((struct coda_mntinfo *)(vp->v_mount->mnt_data) == NULL) {
831176139Srwatson		myprintf(("Help! vfsp->vfs_data was NULL, but vnode %p "
832176139Srwatson		    "wasn't dying\n", vp));
833176139Srwatson		panic("badness in coda_inactive\n");
834176139Srwatson	}
835176139Srwatson	if (IS_UNMOUNTING(cp)) {
83638625Srvb#ifdef	DEBUG
837176139Srwatson		printf("coda_inactive: IS_UNMOUNTING use %d: vp %p, cp %p\n",
838176139Srwatson		    vrefcnt(vp), vp, cp);
839176139Srwatson		if (cp->c_ovp != NULL)
840176139Srwatson			printf("coda_inactive: cp->ovp != NULL use %d: vp "
841176139Srwatson			    "%p, cp %p\n", vrefcnt(vp), vp, cp);
84238625Srvb#endif
843176263Srwatson	} else
844176139Srwatson		vgone(vp);
845176139Srwatson	MARK_INT_SAT(CODA_INACTIVE_STATS);
846176139Srwatson	return (0);
84738625Srvb}
84838625Srvb
84938625Srvb/*
85096755Strhodes * Remote filesystem operations having to do with directory manipulation.
85138625Srvb */
85238625Srvb
853176139Srwatson/*
854176120Srwatson * In FreeBSD, lookup returns the vnode locked.
85538625Srvb */
85638625Srvbint
857176233Srwatsoncoda_lookup(struct vop_cachedlookup_args *ap)
85838625Srvb{
859176139Srwatson	/* true args */
860176139Srwatson	struct vnode *dvp = ap->a_dvp;
861176139Srwatson	struct cnode *dcp = VTOC(dvp);
862176139Srwatson	struct vnode **vpp = ap->a_vpp;
863176139Srwatson	/*
864176139Srwatson	 * It looks as though ap->a_cnp->ni_cnd->cn_nameptr holds the rest of
865176139Srwatson	 * the string to xlate, and that we must try to get at least
866176139Srwatson	 * ap->a_cnp->ni_cnd->cn_namelen of those characters to macth.  I
867176139Srwatson	 * could be wrong.
868176139Srwatson	 */
869176139Srwatson	struct componentname  *cnp = ap->a_cnp;
870176139Srwatson	struct ucred *cred = cnp->cn_cred;
871176139Srwatson	struct thread *td = cnp->cn_thread;
872176139Srwatson	/* locals */
873176139Srwatson	struct cnode *cp;
874176139Srwatson	const char *nm = cnp->cn_nameptr;
875176139Srwatson	int len = cnp->cn_namelen;
876206210Srwatson	struct CodaFid VFid;
877176139Srwatson	int vtype;
878176139Srwatson	int error = 0;
87938625Srvb
880176139Srwatson	MARK_ENTRY(CODA_LOOKUP_STATS);
881176139Srwatson	CODADEBUG(CODA_LOOKUP, myprintf(("lookup: %s in %s\n", nm,
882176139Srwatson	    coda_f2s(&dcp->c_fid))););
88338625Srvb
884176139Srwatson	/*
885176139Srwatson	 * Check for lookup of control object.
886176139Srwatson	 */
887176139Srwatson	if (IS_CTL_NAME(dvp, nm, len)) {
888176139Srwatson		*vpp = coda_ctlvp;
889176139Srwatson		vref(*vpp);
890176139Srwatson		MARK_INT_SAT(CODA_LOOKUP_STATS);
891176139Srwatson		goto exit;
892176139Srwatson	}
893176139Srwatson	if (len+1 > CODA_MAXNAMLEN) {
894176139Srwatson		MARK_INT_FAIL(CODA_LOOKUP_STATS);
895176139Srwatson		CODADEBUG(CODA_LOOKUP, myprintf(("name too long: lookup, "
896176139Srwatson		    "%s (%s)\n", coda_f2s(&dcp->c_fid), nm)););
897176139Srwatson		*vpp = NULL;
898176139Srwatson		error = EINVAL;
899176139Srwatson		goto exit;
900176139Srwatson	}
90138625Srvb
902176233Srwatson	error = venus_lookup(vtomi(dvp), &dcp->c_fid, nm, len, cred,
903176233Srwatson	    td->td_proc, &VFid, &vtype);
904176233Srwatson	if (error) {
905176233Srwatson		MARK_INT_FAIL(CODA_LOOKUP_STATS);
906176233Srwatson		CODADEBUG(CODA_LOOKUP, myprintf(("lookup error on %s "
907176233Srwatson		    "(%s)%d\n", coda_f2s(&dcp->c_fid), nm, error)););
908176233Srwatson		*vpp = NULL;
909176139Srwatson	} else {
910176233Srwatson		MARK_INT_SAT(CODA_LOOKUP_STATS);
911176233Srwatson		CODADEBUG(CODA_LOOKUP, myprintf(("lookup: %s type %o "
912176233Srwatson		    "result %d\n", coda_f2s(&VFid), vtype, error)););
913176233Srwatson		cp = make_coda_node(&VFid, dvp->v_mount, vtype);
914176233Srwatson    		*vpp = CTOV(cp);
915176233Srwatson
916176233Srwatson    		/*
917176233Srwatson		 * Enter the new vnode in the namecache only if the top bit
918176233Srwatson		 * isn't set.
919176233Srwatson		 *
920176233Srwatson		 * And don't enter a new vnode for an invalid one!
921176139Srwatson		 */
922176233Srwatson		if (!(vtype & CODA_NOCACHE) && (cnp->cn_flags & MAKEENTRY))
923176233Srwatson			cache_enter(dvp, *vpp, cnp);
92438625Srvb	}
925176139Srwatsonexit:
926176139Srwatson	/*
927176139Srwatson	 * If we are creating, and this was the last name to be looked up,
928176139Srwatson	 * and the error was ENOENT, then there really shouldn't be an error
929176139Srwatson	 * and we can make the leaf NULL and return success.  Since this is
930176139Srwatson	 * supposed to work under Mach as well as FreeBSD, we're leaving this
931176139Srwatson	 * fn wrapped.  We also must tell lookup/namei that we need to save
932176139Srwatson	 * the last component of the name.  (Create will have to free the
933176139Srwatson	 * name buffer later...lucky us...).
934176139Srwatson	 */
935176139Srwatson	if (((cnp->cn_nameiop == CREATE) || (cnp->cn_nameiop == RENAME))
936176139Srwatson	    && (cnp->cn_flags & ISLASTCN) && (error == ENOENT)) {
937176139Srwatson		error = EJUSTRETURN;
938176139Srwatson		cnp->cn_flags |= SAVENAME;
939176139Srwatson		*ap->a_vpp = NULL;
940176139Srwatson	}
94138625Srvb
942176139Srwatson	/*
943176139Srwatson	 * If we are removing, and we are at the last element, and we found
944176139Srwatson	 * it, then we need to keep the name around so that the removal will
945176139Srwatson	 * go ahead as planned.  Unfortunately, this will probably also lock
946176139Srwatson	 * the to-be-removed vnode, which may or may not be a good idea.
947176139Srwatson	 * I'll have to look at the bits of coda_remove to make sure.  We'll
948176139Srwatson	 * only save the name if we did in fact find the name, otherwise
949176139Srwatson	 * coda_remove won't have a chance to free the pathname.
950176139Srwatson	 */
951176139Srwatson	if ((cnp->cn_nameiop == DELETE) && (cnp->cn_flags & ISLASTCN)
952176139Srwatson	    && !error)
953176139Srwatson		cnp->cn_flags |= SAVENAME;
95438625Srvb
955176139Srwatson	/*
956176139Srwatson	 * If the lookup went well, we need to (potentially?) unlock the
957176139Srwatson	 * parent, and lock the child.  We are only responsible for checking
958176139Srwatson	 * to see if the parent is supposed to be unlocked before we return.
959176139Srwatson	 * We must always lock the child (provided there is one, and (the
960176139Srwatson	 * parent isn't locked or it isn't the same as the parent.)  Simple,
961176139Srwatson	 * huh?  We can never leave the parent locked unless we are ISLASTCN.
962176139Srwatson	 */
963176139Srwatson	if (!error || (error == EJUSTRETURN)) {
964176139Srwatson		if (cnp->cn_flags & ISDOTDOT) {
965176139Srwatson			VOP_UNLOCK(dvp, 0);
966176139Srwatson			/*
967176139Srwatson			 * The parent is unlocked.  As long as there is a
968176139Srwatson			 * child, lock it without bothering to check anything
969176139Srwatson			 * else.
970176139Srwatson			 */
971176139Srwatson			if (*ap->a_vpp)
972176139Srwatson				vn_lock(*ap->a_vpp, LK_EXCLUSIVE | LK_RETRY);
973176139Srwatson			vn_lock(dvp, LK_RETRY|LK_EXCLUSIVE);
974176139Srwatson		} else {
975176139Srwatson			/*
976176139Srwatson			 * The parent is locked, and may be the same as the
977176139Srwatson			 * child.  If different, go ahead and lock it.
978176139Srwatson			 */
979176139Srwatson			if (*ap->a_vpp && (*ap->a_vpp != dvp))
980176139Srwatson				vn_lock(*ap->a_vpp, LK_EXCLUSIVE | LK_RETRY);
981176139Srwatson		}
98238625Srvb	} else {
983176139Srwatson		/*
984176139Srwatson		 * If the lookup failed, we need to ensure that the leaf is
985176139Srwatson		 * NULL.
986176139Srwatson		 *
987176139Srwatson		 * Don't change any locking?
988176139Srwatson		 */
989176139Srwatson		*ap->a_vpp = NULL;
99038625Srvb	}
991176139Srwatson	return (error);
99238625Srvb}
99338625Srvb
99438625Srvb/*ARGSUSED*/
99538625Srvbint
996138290Sphkcoda_create(struct vop_create_args *ap)
99738625Srvb{
998176139Srwatson	/* true args */
999176139Srwatson	struct vnode *dvp = ap->a_dvp;
1000176139Srwatson	struct cnode *dcp = VTOC(dvp);
1001176139Srwatson	struct vattr *va = ap->a_vap;
1002176139Srwatson	int exclusive = 1;
1003176139Srwatson	int mode = ap->a_vap->va_mode;
1004176139Srwatson	struct vnode **vpp = ap->a_vpp;
1005176139Srwatson	struct componentname  *cnp = ap->a_cnp;
1006176139Srwatson	struct ucred *cred = cnp->cn_cred;
1007176139Srwatson	struct thread *td = cnp->cn_thread;
1008176139Srwatson	/* locals */
1009176139Srwatson	int error;
1010176139Srwatson	struct cnode *cp;
1011176139Srwatson	const char *nm = cnp->cn_nameptr;
1012176139Srwatson	int len = cnp->cn_namelen;
1013206210Srwatson	struct CodaFid VFid;
1014176139Srwatson	struct vattr attr;
101538625Srvb
1016176139Srwatson	MARK_ENTRY(CODA_CREATE_STATS);
101738625Srvb
1018176139Srwatson	/*
1019176139Srwatson	 * All creates are exclusive XXX.
1020176139Srwatson	 *
1021176139Srwatson	 * I'm assuming the 'mode' argument is the file mode bits XXX.
1022176139Srwatson	 *
1023176139Srwatson	 * Check for create of control object.
1024176139Srwatson	 */
1025176139Srwatson	if (IS_CTL_NAME(dvp, nm, len)) {
1026176139Srwatson		*vpp = (struct vnode *)0;
1027176139Srwatson		MARK_INT_FAIL(CODA_CREATE_STATS);
1028176139Srwatson		return (EACCES);
1029176139Srwatson	}
1030176139Srwatson	error = venus_create(vtomi(dvp), &dcp->c_fid, nm, len, exclusive,
1031176139Srwatson	    mode, va, cred, td->td_proc, &VFid, &attr);
1032176139Srwatson	if (!error) {
1033176139Srwatson		/*
1034176139Srwatson		 * If this is an exclusive create, panic if the file already
1035176139Srwatson		 * exists.
1036176139Srwatson		 *
1037176139Srwatson		 * Venus should have detected the file and reported EEXIST.
1038176139Srwatson		 */
1039176139Srwatson		if ((exclusive == 1) && (coda_find(&VFid) != NULL))
1040176139Srwatson	  	  	panic("cnode existed for newly created file!");
1041176139Srwatson		cp = make_coda_node(&VFid, dvp->v_mount, attr.va_type);
1042176139Srwatson		*vpp = CTOV(cp);
104338625Srvb
1044176139Srwatson		/*
1045176139Srwatson		 * Update va to reflect the new attributes.
1046176139Srwatson		 */
1047176139Srwatson		(*va) = attr;
104838625Srvb
1049176139Srwatson		/*
1050176139Srwatson		 * Update the attribute cache and mark it as valid.
1051176139Srwatson		 */
1052176139Srwatson		if (coda_attr_cache) {
1053176139Srwatson			VTOC(*vpp)->c_vattr = attr;
1054176139Srwatson			VTOC(*vpp)->c_flags |= C_VATTR;
1055176139Srwatson		}
105638625Srvb
1057176139Srwatson		/*
1058176139Srwatson		 * Invalidate the parent's attr cache, the modification time
1059176139Srwatson		 * has changed.
1060176139Srwatson		 */
1061176139Srwatson		VTOC(dvp)->c_flags &= ~C_VATTR;
1062176233Srwatson		cache_enter(dvp, *vpp, cnp);
1063176139Srwatson		CODADEBUG(CODA_CREATE, myprintf(("create: %s, result %d\n",
1064176139Srwatson		    coda_f2s(&VFid), error)););
1065176139Srwatson	} else {
1066176139Srwatson		*vpp = (struct vnode *)0;
1067176139Srwatson		CODADEBUG(CODA_CREATE, myprintf(("create error %d\n",
1068176139Srwatson		    error)););
106938625Srvb	}
1070176139Srwatson	if (!error) {
1071176233Srwatson		if (cnp->cn_flags & MAKEENTRY)
1072176233Srwatson			cache_enter(dvp, *vpp, cnp);
1073176139Srwatson		if (cnp->cn_flags & LOCKLEAF)
1074176139Srwatson			vn_lock(*ap->a_vpp, LK_EXCLUSIVE | LK_RETRY);
1075176233Srwatson	} else if (error == ENOENT) {
1076176233Srwatson		/*
1077176233Srwatson		 * XXXRW: We only enter a negative entry if ENOENT is
1078176233Srwatson		 * returned, not other errors.  But will Venus invalidate dvp
1079176233Srwatson		 * properly in all cases when new files appear via the
1080176233Srwatson		 * network rather than a local operation?
1081176233Srwatson		 */
1082176233Srwatson		if (cnp->cn_flags & MAKEENTRY)
1083176233Srwatson			cache_enter(dvp, NULL, cnp);
108438625Srvb	}
1085176139Srwatson	return (error);
108638625Srvb}
108738625Srvb
108838625Srvbint
1089138290Sphkcoda_remove(struct vop_remove_args *ap)
109038625Srvb{
1091176139Srwatson	/* true args */
1092176233Srwatson	struct vnode *vp = ap->a_vp;
1093176139Srwatson	struct vnode *dvp = ap->a_dvp;
1094176139Srwatson	struct cnode *cp = VTOC(dvp);
1095176139Srwatson	struct componentname  *cnp = ap->a_cnp;
1096176139Srwatson	struct ucred *cred = cnp->cn_cred;
1097176139Srwatson	struct thread *td = cnp->cn_thread;
1098176139Srwatson	/* locals */
1099176139Srwatson	int error;
1100176139Srwatson	const char *nm = cnp->cn_nameptr;
1101176139Srwatson	int len = cnp->cn_namelen;
1102176233Srwatson#if 0
1103176139Srwatson	struct cnode *tp;
1104176233Srwatson#endif
110538625Srvb
1106176139Srwatson	MARK_ENTRY(CODA_REMOVE_STATS);
1107176139Srwatson	CODADEBUG(CODA_REMOVE, myprintf(("remove: %s in %s\n", nm,
1108176139Srwatson	    coda_f2s(&cp->c_fid))););
110938625Srvb
1110176139Srwatson	/*
1111176233Srwatson	 * Check for remove of control object.
1112176139Srwatson	 */
1113176233Srwatson	if (IS_CTL_NAME(dvp, nm, len)) {
1114176233Srwatson		MARK_INT_FAIL(CODA_REMOVE_STATS);
1115176233Srwatson		return (ENOENT);
111638625Srvb	}
111738625Srvb
1118176139Srwatson	/*
1119176139Srwatson	 * Invalidate the parent's attr cache, the modification time has
1120176233Srwatson	 * changed.  We don't yet know if the last reference to the file is
1121176233Srwatson	 * being removed, but we do know the reference count on the child has
1122176233Srwatson	 * changed, so invalidate its attr cache also.
1123176139Srwatson	 */
1124176139Srwatson	VTOC(dvp)->c_flags &= ~C_VATTR;
1125176238Srwatson	VTOC(vp)->c_flags &= ~(C_VATTR | C_ACCCACHE);
1126176139Srwatson	error = venus_remove(vtomi(dvp), &cp->c_fid, nm, len, cred,
1127176139Srwatson	    td->td_proc);
1128176233Srwatson	cache_purge(vp);
1129176139Srwatson	CODADEBUG(CODA_REMOVE, myprintf(("in remove result %d\n",error)););
1130176139Srwatson	return (error);
113138625Srvb}
113238625Srvb
113338625Srvbint
1134138290Sphkcoda_link(struct vop_link_args *ap)
113538625Srvb{
1136176139Srwatson	/* true args */
1137176139Srwatson	struct vnode *vp = ap->a_vp;
1138176139Srwatson	struct cnode *cp = VTOC(vp);
1139176139Srwatson	struct vnode *tdvp = ap->a_tdvp;
1140176139Srwatson	struct cnode *tdcp = VTOC(tdvp);
1141176139Srwatson	struct componentname *cnp = ap->a_cnp;
1142176139Srwatson	struct ucred *cred = cnp->cn_cred;
1143176139Srwatson	struct thread *td = cnp->cn_thread;
1144176139Srwatson	/* locals */
1145176139Srwatson	int error;
1146176139Srwatson	const char *nm = cnp->cn_nameptr;
1147176139Srwatson	int len = cnp->cn_namelen;
114838625Srvb
1149176139Srwatson	MARK_ENTRY(CODA_LINK_STATS);
115038625Srvb
1151176139Srwatson	if (codadebug & CODADBGMSK(CODA_LINK)) {
1152176139Srwatson		myprintf(("nb_link:   vp fid: %s\n", coda_f2s(&cp->c_fid)));
1153176139Srwatson		myprintf(("nb_link: tdvp fid: %s)\n",
1154176139Srwatson		    coda_f2s(&tdcp->c_fid)));
1155176139Srwatson	}
1156176139Srwatson	if (codadebug & CODADBGMSK(CODA_LINK)) {
1157176139Srwatson		myprintf(("link:   vp fid: %s\n", coda_f2s(&cp->c_fid)));
1158176139Srwatson		myprintf(("link: tdvp fid: %s\n", coda_f2s(&tdcp->c_fid)));
1159176139Srwatson	}
116038625Srvb
1161176139Srwatson	/*
1162176139Srwatson	 * Check for link to/from control object.
1163176139Srwatson	 */
1164176139Srwatson	if (IS_CTL_NAME(tdvp, nm, len) || IS_CTL_VP(vp)) {
1165176139Srwatson		MARK_INT_FAIL(CODA_LINK_STATS);
1166176139Srwatson		return (EACCES);
1167176139Srwatson	}
1168176139Srwatson	error = venus_link(vtomi(vp), &cp->c_fid, &tdcp->c_fid, nm, len,
1169176139Srwatson	    cred, td->td_proc);
117038625Srvb
1171176139Srwatson	/*
1172176139Srwatson	 * Invalidate the parent's attr cache, the modification time has
1173176139Srwatson	 * changed.
1174176139Srwatson	 */
1175176139Srwatson	VTOC(tdvp)->c_flags &= ~C_VATTR;
1176176139Srwatson	VTOC(vp)->c_flags &= ~C_VATTR;
1177176139Srwatson	CODADEBUG(CODA_LINK, myprintf(("in link result %d\n",error)););
1178176139Srwatson	return (error);
117938625Srvb}
118038625Srvb
118138625Srvbint
1182138290Sphkcoda_rename(struct vop_rename_args *ap)
118338625Srvb{
1184176139Srwatson	/* true args */
1185176238Srwatson	struct vnode *fvp = ap->a_fvp;
1186176238Srwatson	struct vnode *tvp = ap->a_tvp;
1187176139Srwatson	struct vnode *odvp = ap->a_fdvp;
1188176139Srwatson	struct cnode *odcp = VTOC(odvp);
1189176139Srwatson	struct componentname  *fcnp = ap->a_fcnp;
1190176139Srwatson	struct vnode *ndvp = ap->a_tdvp;
1191176139Srwatson	struct cnode *ndcp = VTOC(ndvp);
1192176139Srwatson	struct componentname  *tcnp = ap->a_tcnp;
1193176139Srwatson	struct ucred *cred = fcnp->cn_cred;
1194176139Srwatson	struct thread *td = fcnp->cn_thread;
1195176139Srwatson	/* true args */
1196176139Srwatson	int error;
1197176139Srwatson	const char *fnm = fcnp->cn_nameptr;
1198176139Srwatson	int flen = fcnp->cn_namelen;
1199176139Srwatson	const char *tnm = tcnp->cn_nameptr;
1200176139Srwatson	int tlen = tcnp->cn_namelen;
120138625Srvb
1202176139Srwatson	MARK_ENTRY(CODA_RENAME_STATS);
120338625Srvb
1204176139Srwatson	/*
1205176139Srwatson	 * Check for rename involving control object.
1206176139Srwatson	 */
1207176139Srwatson	if (IS_CTL_NAME(odvp, fnm, flen) || IS_CTL_NAME(ndvp, tnm, tlen)) {
1208176139Srwatson		MARK_INT_FAIL(CODA_RENAME_STATS);
1209176139Srwatson		return (EACCES);
121038625Srvb	}
121138625Srvb
1212176139Srwatson	/*
1213176233Srwatson	 * Remove the entries for both source and target directories, which
1214176233Srwatson	 * should catch references to the children.  Perhaps we could purge
1215176233Srwatson	 * less?
1216176139Srwatson	 */
1217176233Srwatson	cache_purge(odvp);
1218176233Srwatson	cache_purge(ndvp);
121938625Srvb
1220176139Srwatson	/*
1221176238Srwatson	 * Invalidate parent directories as modification times have changed.
1222176238Srwatson	 * Invalidate access cache on renamed file as rights may have
1223176139Srwatson	 * changed.
1224176139Srwatson	 */
1225176139Srwatson	VTOC(odvp)->c_flags &= ~C_VATTR;
1226176139Srwatson	VTOC(ndvp)->c_flags &= ~C_VATTR;
1227176238Srwatson	VTOC(fvp)->c_flags &= ~C_ACCCACHE;
1228176139Srwatson	if (flen+1 > CODA_MAXNAMLEN) {
1229176139Srwatson		MARK_INT_FAIL(CODA_RENAME_STATS);
1230176139Srwatson		error = EINVAL;
1231176139Srwatson		goto exit;
1232176139Srwatson	}
1233176139Srwatson	if (tlen+1 > CODA_MAXNAMLEN) {
1234176139Srwatson		MARK_INT_FAIL(CODA_RENAME_STATS);
1235176139Srwatson		error = EINVAL;
1236176139Srwatson		goto exit;
1237176139Srwatson	}
1238176139Srwatson	error = venus_rename(vtomi(odvp), &odcp->c_fid, &ndcp->c_fid, fnm,
1239176139Srwatson	    flen, tnm, tlen, cred, td->td_proc);
1240176139Srwatsonexit:
1241176139Srwatson	CODADEBUG(CODA_RENAME, myprintf(("in rename result %d\n",error)););
124238625Srvb
1243176139Srwatson	/*
1244176238Srwatson	 * Update namecache to reflect that the names of various objects may
1245176238Srwatson	 * have changed (or gone away entirely).
1246176139Srwatson	 */
1247176238Srwatson	cache_purge(fvp);
1248176238Srwatson	cache_purge(tvp);
124938625Srvb
1250176139Srwatson	/*
1251176139Srwatson	 * Release parents first, then children.
1252176139Srwatson	 */
1253176139Srwatson	vrele(odvp);
1254176238Srwatson	if (tvp) {
1255176238Srwatson		if (tvp == ndvp)
1256176139Srwatson			vrele(ndvp);
1257176139Srwatson		else
1258176139Srwatson			vput(ndvp);
1259176238Srwatson		vput(tvp);
1260176139Srwatson	} else
1261176139Srwatson		vput(ndvp);
1262176238Srwatson	vrele(fvp);
1263176139Srwatson	return (error);
126438625Srvb}
126538625Srvb
126638625Srvbint
1267138290Sphkcoda_mkdir(struct vop_mkdir_args *ap)
126838625Srvb{
1269176139Srwatson	/* true args */
1270176139Srwatson	struct vnode *dvp = ap->a_dvp;
1271176139Srwatson	struct cnode *dcp = VTOC(dvp);
1272176139Srwatson	struct componentname  *cnp = ap->a_cnp;
1273176139Srwatson	struct vattr *va = ap->a_vap;
1274176139Srwatson	struct vnode **vpp = ap->a_vpp;
1275176139Srwatson	struct ucred *cred = cnp->cn_cred;
1276176139Srwatson	struct thread *td = cnp->cn_thread;
1277176139Srwatson	/* locals */
1278176139Srwatson	int error;
1279176139Srwatson	const char *nm = cnp->cn_nameptr;
1280176139Srwatson	int len = cnp->cn_namelen;
1281176139Srwatson	struct cnode *cp;
1282206210Srwatson	struct CodaFid VFid;
1283176139Srwatson	struct vattr ova;
128438625Srvb
1285176139Srwatson	MARK_ENTRY(CODA_MKDIR_STATS);
128638625Srvb
1287176139Srwatson	/*
1288176139Srwatson	 * Check for mkdir of target object.
1289176139Srwatson	 */
1290176139Srwatson	if (IS_CTL_NAME(dvp, nm, len)) {
1291176139Srwatson		*vpp = (struct vnode *)0;
1292176139Srwatson		MARK_INT_FAIL(CODA_MKDIR_STATS);
1293176139Srwatson		return (EACCES);
129438625Srvb	}
1295176139Srwatson	if (len+1 > CODA_MAXNAMLEN) {
1296176139Srwatson		*vpp = (struct vnode *)0;
1297176139Srwatson		MARK_INT_FAIL(CODA_MKDIR_STATS);
1298176139Srwatson		return (EACCES);
1299176139Srwatson	}
1300176139Srwatson	error = venus_mkdir(vtomi(dvp), &dcp->c_fid, nm, len, va, cred,
1301176139Srwatson	    td->td_proc, &VFid, &ova);
1302176139Srwatson	if (!error) {
1303176139Srwatson		if (coda_find(&VFid) != NULL)
1304176139Srwatson			panic("cnode existed for newly created directory!");
1305176139Srwatson		cp =  make_coda_node(&VFid, dvp->v_mount, va->va_type);
1306176139Srwatson		*vpp = CTOV(cp);
130738625Srvb
1308176139Srwatson		/*
1309176139Srwatson		 * Enter the new vnode in the Name Cache.
1310176139Srwatson		 */
1311176233Srwatson		if (cnp->cn_flags & MAKEENTRY)
1312176233Srwatson			cache_enter(dvp, *vpp, cnp);
1313175474Srwatson
1314176139Srwatson		/*
1315176233Srwatson		 * Update the attr cache and mark as valid.
1316176139Srwatson		 */
1317176139Srwatson		if (coda_attr_cache) {
1318176139Srwatson			VTOC(*vpp)->c_vattr = ova;
1319176139Srwatson			VTOC(*vpp)->c_flags |= C_VATTR;
1320176139Srwatson		}
1321175474Srwatson
1322176139Srwatson		/*
1323176139Srwatson		 * Invalidate the parent's attr cache, the modification time
1324176139Srwatson		 * has changed.
1325176139Srwatson		 */
1326176139Srwatson		VTOC(dvp)->c_flags &= ~C_VATTR;
1327176139Srwatson		vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
1328176139Srwatson		CODADEBUG( CODA_MKDIR, myprintf(("mkdir: %s result %d\n",
1329176139Srwatson		    coda_f2s(&VFid), error)););
1330119832Stjr	} else {
1331176139Srwatson		*vpp = NULL;
1332176139Srwatson		CODADEBUG(CODA_MKDIR, myprintf(("mkdir error %d\n",error)););
1333176139Srwatson	}
1334176139Srwatson	return (error);
133538625Srvb}
133638625Srvb
133738625Srvbint
1338138290Sphkcoda_rmdir(struct vop_rmdir_args *ap)
133938625Srvb{
1340176139Srwatson	/* true args */
1341176233Srwatson	struct vnode *vp = ap->a_vp;
1342176139Srwatson	struct vnode *dvp = ap->a_dvp;
1343176139Srwatson	struct cnode *dcp = VTOC(dvp);
1344176139Srwatson	struct componentname  *cnp = ap->a_cnp;
1345176139Srwatson	struct ucred *cred = cnp->cn_cred;
1346176139Srwatson	struct thread *td = cnp->cn_thread;
1347176139Srwatson	/* true args */
1348176139Srwatson	int error;
1349176139Srwatson	const char *nm = cnp->cn_nameptr;
1350176139Srwatson	int len = cnp->cn_namelen;
1351176233Srwatson#if 0
1352176139Srwatson	struct cnode *cp;
1353176233Srwatson#endif
135438625Srvb
1355176139Srwatson	MARK_ENTRY(CODA_RMDIR_STATS);
135638625Srvb
1357176139Srwatson	/*
1358176139Srwatson	 * Check for rmdir of control object.
1359176139Srwatson	 */
1360176139Srwatson	if (IS_CTL_NAME(dvp, nm, len)) {
1361176139Srwatson		MARK_INT_FAIL(CODA_RMDIR_STATS);
1362176139Srwatson		return (ENOENT);
1363176139Srwatson	}
136438625Srvb
1365176139Srwatson	/*
1366176233Srwatson	 * Possibly somewhat conservative purging, perhaps we just need to
1367176233Srwatson	 * purge vp?
1368176139Srwatson	 */
1369176233Srwatson	cache_purge(dvp);
1370176233Srwatson	cache_purge(vp);
137138625Srvb
1372176139Srwatson	/*
1373176139Srwatson	 * Invalidate the parent's attr cache, the modification time has
1374176139Srwatson	 * changed.
1375176139Srwatson	 */
1376176139Srwatson	dcp->c_flags &= ~C_VATTR;
1377176139Srwatson	error = venus_rmdir(vtomi(dvp), &dcp->c_fid, nm, len, cred,
1378176139Srwatson	    td->td_proc);
1379176139Srwatson	CODADEBUG(CODA_RMDIR, myprintf(("in rmdir result %d\n", error)););
1380176139Srwatson	return (error);
138138625Srvb}
138238625Srvb
138338625Srvbint
1384138290Sphkcoda_symlink(struct vop_symlink_args *ap)
138538625Srvb{
1386176139Srwatson	/* true args */
1387176139Srwatson	struct vnode *tdvp = ap->a_dvp;
1388176139Srwatson	struct cnode *tdcp = VTOC(tdvp);
1389176139Srwatson	struct componentname *cnp = ap->a_cnp;
1390176139Srwatson	struct vattr *tva = ap->a_vap;
1391176139Srwatson	char *path = ap->a_target;
1392176139Srwatson	struct ucred *cred = cnp->cn_cred;
1393176139Srwatson	struct thread *td = cnp->cn_thread;
1394176139Srwatson	struct vnode **vpp = ap->a_vpp;
1395176139Srwatson	/* locals */
1396176139Srwatson	int error;
139738625Srvb
1398176139Srwatson	/*-
1399176139Srwatson	 * XXX I'm assuming the following things about coda_symlink's
1400176139Srwatson	 * arguments:
1401176139Srwatson	 *       t(foo) is the new name/parent/etc being created.
1402176139Srwatson	 *       lname is the contents of the new symlink.
1403176139Srwatson	 */
1404176139Srwatson	char *nm = cnp->cn_nameptr;
1405176139Srwatson	int len = cnp->cn_namelen;
1406176139Srwatson	int plen = strlen(path);
140738625Srvb
1408176139Srwatson	/*
1409176139Srwatson	 * Here's the strategy for the moment: perform the symlink, then do a
1410176139Srwatson	 * lookup to grab the resulting vnode.  I know this requires two
1411176139Srwatson	 * communications with Venus for a new sybolic link, but that's the
1412176139Srwatson	 * way the ball bounces.  I don't yet want to change the way the Mach
1413176139Srwatson	 * symlink works.  When Mach support is deprecated, we should change
1414176139Srwatson	 * symlink so that the common case returns the resultant vnode in a
1415176139Srwatson	 * vpp argument.
1416176139Srwatson	 */
1417176139Srwatson	MARK_ENTRY(CODA_SYMLINK_STATS);
141838625Srvb
1419176139Srwatson	/*
1420176139Srwatson	 * Check for symlink of control object.
1421176139Srwatson	 */
1422176139Srwatson	if (IS_CTL_NAME(tdvp, nm, len)) {
1423176139Srwatson		MARK_INT_FAIL(CODA_SYMLINK_STATS);
1424176139Srwatson		return (EACCES);
1425176139Srwatson	}
1426176139Srwatson	if (plen+1 > CODA_MAXPATHLEN) {
1427176139Srwatson		MARK_INT_FAIL(CODA_SYMLINK_STATS);
1428176139Srwatson		return (EINVAL);
1429176139Srwatson	}
1430176139Srwatson	if (len+1 > CODA_MAXNAMLEN) {
1431176139Srwatson		MARK_INT_FAIL(CODA_SYMLINK_STATS);
1432176139Srwatson		error = EINVAL;
1433176139Srwatson		goto exit;
1434176139Srwatson	}
1435176139Srwatson	error = venus_symlink(vtomi(tdvp), &tdcp->c_fid, path, plen, nm, len,
1436176139Srwatson	    tva, cred, td->td_proc);
143738625Srvb
1438176139Srwatson	/*
1439176139Srwatson	 * Invalidate the parent's attr cache, the modification time has
1440176139Srwatson	 * changed.
1441176139Srwatson	 */
1442176139Srwatson	tdcp->c_flags &= ~C_VATTR;
1443176139Srwatson	if (error == 0)
1444176139Srwatson		error = VOP_LOOKUP(tdvp, vpp, cnp);
1445176139Srwatsonexit:
1446176139Srwatson	CODADEBUG(CODA_SYMLINK, myprintf(("in symlink result %d\n",error)););
1447176139Srwatson	return (error);
144838625Srvb}
144938625Srvb
145038625Srvb/*
145138625Srvb * Read directory entries.
1452176131Srwatson *
1453176139Srwatson * XXX: This forwards the operator straight to the cache vnode using
1454176131Srwatson * VOP_READDIR(), rather than calling venus_readdir().  Why?
145538625Srvb */
145638625Srvbint
1457138290Sphkcoda_readdir(struct vop_readdir_args *ap)
145838625Srvb{
1459176139Srwatson	/* true args */
1460176139Srwatson	struct vnode *vp = ap->a_vp;
1461176139Srwatson	struct cnode *cp = VTOC(vp);
1462176139Srwatson	struct uio *uiop = ap->a_uio;
1463176139Srwatson	struct ucred *cred = ap->a_cred;
1464176139Srwatson	int *eofflag = ap->a_eofflag;
1465176139Srwatson	u_long **cookies = ap->a_cookies;
1466176139Srwatson	int *ncookies = ap->a_ncookies;
1467176139Srwatson	struct thread *td = ap->a_uio->uio_td;
1468176139Srwatson	/* upcall decl */
1469176139Srwatson	/* locals */
1470176139Srwatson	int error = 0;
1471176139Srwatson	int opened_internally = 0;
147238625Srvb
1473176139Srwatson	MARK_ENTRY(CODA_READDIR_STATS);
1474194990Skib	CODADEBUG(CODA_READDIR, myprintf(("coda_readdir(%p, %zd, %lld, %d)\n",
1475176139Srwatson	    (void *)uiop->uio_iov->iov_base, uiop->uio_resid,
1476176139Srwatson	    (long long)uiop->uio_offset, uiop->uio_segflg)););
147738625Srvb
1478176139Srwatson	/*
1479176139Srwatson	 * Check for readdir of control object.
1480176139Srwatson	 */
1481176139Srwatson	if (IS_CTL_VP(vp)) {
1482176139Srwatson		MARK_INT_FAIL(CODA_READDIR_STATS);
1483176139Srwatson		return (ENOENT);
1484176139Srwatson	}
148538625Srvb
1486176139Srwatson	/*
1487176139Srwatson	 * If directory is not already open do an "internal open" on it.
1488176139Srwatson	 *
1489176139Srwatson	 * XXX: Why would this happen?  For files, there's memory mapping,
1490176139Srwatson	 * execution, and other kernel access paths such as ktrace.  For
1491176139Srwatson	 * directories, it is less clear.
1492176139Srwatson	 */
149338625Srvb	if (cp->c_ovp == NULL) {
1494176139Srwatson		opened_internally = 1;
1495176139Srwatson		MARK_INT_GEN(CODA_OPEN_STATS);
1496176139Srwatson		error = VOP_OPEN(vp, FREAD, cred, td, NULL);
1497176139Srwatson		printf("coda_readdir: Internally Opening %p\n", vp);
1498176139Srwatson		if (error) {
1499176139Srwatson			printf("coda_readdir: VOP_OPEN on container failed "
1500176139Srwatson			   "%d\n", error);
1501176139Srwatson			return (error);
1502176139Srwatson		}
150338625Srvb	}
1504176139Srwatson
1505176139Srwatson	/*
1506176139Srwatson	 * Have UFS handle the call.
1507176139Srwatson	 */
1508176139Srwatson	CODADEBUG(CODA_READDIR, myprintf(("indirect readdir: fid = %s, "
1509176139Srwatson	    "refcnt = %d\n", coda_f2s(&cp->c_fid), vp->v_usecount)););
1510188588Sjhb	vn_lock(cp->c_ovp, LK_SHARED | LK_RETRY);
151138625Srvb	error = VOP_READDIR(cp->c_ovp, uiop, cred, eofflag, ncookies,
1512176139Srwatson	    cookies);
1513176118Srwatson	VOP_UNLOCK(cp->c_ovp, 0);
151438625Srvb	if (error)
1515176139Srwatson		MARK_INT_FAIL(CODA_READDIR_STATS);
151638625Srvb	else
1517176139Srwatson		MARK_INT_SAT(CODA_READDIR_STATS);
1518176139Srwatson
1519176139Srwatson	/*
1520176139Srwatson	 * Do an "internal close" if necessary.
1521176139Srwatson	 */
152238625Srvb	if (opened_internally) {
1523176139Srwatson		MARK_INT_GEN(CODA_CLOSE_STATS);
1524176139Srwatson		(void)VOP_CLOSE(vp, FREAD, cred, td);
152538625Srvb	}
1526176139Srwatson	return (error);
152738625Srvb}
152838625Srvb
152938625Srvbint
1530138290Sphkcoda_reclaim(struct vop_reclaim_args *ap)
153138625Srvb{
1532176139Srwatson	/* true args */
1533176139Srwatson	struct vnode *vp = ap->a_vp;
1534176139Srwatson	struct cnode *cp = VTOC(vp);
1535176139Srwatson	/* upcall decl */
1536176139Srwatson	/* locals */
153738625Srvb
1538176139Srwatson	/*
1539176139Srwatson	 * Forced unmount/flush will let vnodes with non-zero use be
1540176139Srwatson	 * destroyed!
1541176139Srwatson	 */
1542176139Srwatson	ENTRY;
154338625Srvb
1544176139Srwatson	if (IS_UNMOUNTING(cp)) {
154538625Srvb#ifdef	DEBUG
1546176139Srwatson		if (VTOC(vp)->c_ovp) {
1547176139Srwatson			if (IS_UNMOUNTING(cp))
1548176139Srwatson				printf("coda_reclaim: c_ovp not void: vp "
1549176139Srwatson				    "%p, cp %p\n", vp, cp);
1550176139Srwatson		}
155138625Srvb#endif
1552176139Srwatson	}
1553176139Srwatson	cache_purge(vp);
1554176139Srwatson	coda_free(VTOC(vp));
1555176139Srwatson	vp->v_data = NULL;
1556176139Srwatson	vp->v_object = NULL;
1557176139Srwatson	return (0);
155838625Srvb}
155938625Srvb
156038625Srvbint
1561169671Skibcoda_lock(struct vop_lock1_args *ap)
156238625Srvb{
1563176139Srwatson	/* true args */
1564176139Srwatson	struct vnode *vp = ap->a_vp;
1565176139Srwatson	struct cnode *cp = VTOC(vp);
1566176139Srwatson	/* upcall decl */
1567176139Srwatson	/* locals */
156838625Srvb
1569176139Srwatson	ENTRY;
1570176139Srwatson	if ((ap->a_flags & LK_INTERLOCK) == 0) {
1571176139Srwatson		VI_LOCK(vp);
1572176139Srwatson		ap->a_flags |= LK_INTERLOCK;
1573176139Srwatson	}
1574176139Srwatson	if (coda_lockdebug)
1575176139Srwatson		myprintf(("Attempting lock on %s\n", coda_f2s(&cp->c_fid)));
1576176139Srwatson	return (vop_stdlock(ap));
157738625Srvb}
157838625Srvb
157938625Srvbint
1580138290Sphkcoda_unlock(struct vop_unlock_args *ap)
158138625Srvb{
1582176139Srwatson	/* true args */
1583176139Srwatson	struct vnode *vp = ap->a_vp;
1584176139Srwatson	struct cnode *cp = VTOC(vp);
1585176139Srwatson	/* upcall decl */
1586176139Srwatson	/* locals */
158738625Srvb
1588176139Srwatson	ENTRY;
1589176139Srwatson	if (coda_lockdebug)
1590176139Srwatson		myprintf(("Attempting unlock on %s\n",
1591176139Srwatson		    coda_f2s(&cp->c_fid)));
1592176139Srwatson	return (vop_stdunlock(ap));
159338625Srvb}
159438625Srvb
159538625Srvbint
1596138290Sphkcoda_islocked(struct vop_islocked_args *ap)
159738625Srvb{
1598176139Srwatson	/* true args */
159938625Srvb
1600176139Srwatson	ENTRY;
1601176139Srwatson	return (vop_stdislocked(ap));
160238625Srvb}
160338625Srvb
1604176131Srwatsonstatic void
1605176131Srwatsoncoda_print_vattr(struct vattr *attr)
160638625Srvb{
1607176139Srwatson	char *typestr;
160838625Srvb
1609176139Srwatson	switch (attr->va_type) {
1610176139Srwatson	case VNON:
1611176139Srwatson		typestr = "VNON";
1612176139Srwatson		break;
161338625Srvb
1614176139Srwatson	case VREG:
1615176139Srwatson		typestr = "VREG";
1616176139Srwatson		break;
161738625Srvb
1618176139Srwatson	case VDIR:
1619176139Srwatson		typestr = "VDIR";
1620176139Srwatson		break;
162138625Srvb
1622176139Srwatson	case VBLK:
1623176139Srwatson		typestr = "VBLK";
1624176139Srwatson		break;
1625176139Srwatson
1626176139Srwatson	case VCHR:
1627176139Srwatson		typestr = "VCHR";
1628176139Srwatson		break;
1629176139Srwatson
1630176139Srwatson	case VLNK:
1631176139Srwatson		typestr = "VLNK";
1632176139Srwatson		break;
1633176139Srwatson
1634176139Srwatson	case VSOCK:
1635176139Srwatson		typestr = "VSCK";
1636176139Srwatson		break;
1637176139Srwatson
1638176139Srwatson	case VFIFO:
1639176139Srwatson		typestr = "VFFO";
1640176139Srwatson		break;
1641176139Srwatson
1642176139Srwatson	case VBAD:
1643176139Srwatson		typestr = "VBAD";
1644176139Srwatson		break;
1645176139Srwatson
1646176139Srwatson	default:
1647176139Srwatson		typestr = "????";
1648176139Srwatson		break;
1649176139Srwatson	}
1650176139Srwatson	myprintf(("attr: type %s mode %d uid %d gid %d fsid %d rdev %d\n",
1651176139Srwatson	    typestr, (int)attr->va_mode, (int)attr->va_uid,
1652176139Srwatson	    (int)attr->va_gid, (int)attr->va_fsid, (int)attr->va_rdev));
1653176139Srwatson	myprintf(("      fileid %d nlink %d size %d blocksize %d bytes %d\n",
1654176139Srwatson	    (int)attr->va_fileid, (int)attr->va_nlink, (int)attr->va_size,
1655176139Srwatson	    (int)attr->va_blocksize,(int)attr->va_bytes));
1656176139Srwatson	myprintf(("      gen %ld flags %ld vaflags %d\n", attr->va_gen,
1657176139Srwatson	    attr->va_flags, attr->va_vaflags));
1658176139Srwatson	myprintf(("      atime sec %d nsec %d\n", (int)attr->va_atime.tv_sec,
1659176139Srwatson	    (int)attr->va_atime.tv_nsec));
1660176139Srwatson	myprintf(("      mtime sec %d nsec %d\n", (int)attr->va_mtime.tv_sec,
1661176139Srwatson	    (int)attr->va_mtime.tv_nsec));
1662176139Srwatson	myprintf(("      ctime sec %d nsec %d\n", (int)attr->va_ctime.tv_sec,
1663176139Srwatson	    (int)attr->va_ctime.tv_nsec));
166438625Srvb}
166538625Srvb
1666176139Srwatson/*
1667176139Srwatson * How to print a ucred.
1668176139Srwatson */
166938625Srvbvoid
1670176131Srwatsoncoda_print_cred(struct ucred *cred)
167138625Srvb{
167238625Srvb	int i;
167338625Srvb
167438625Srvb	myprintf(("ref %d\tuid %d\n",cred->cr_ref,cred->cr_uid));
167538625Srvb	for (i=0; i < cred->cr_ngroups; i++)
167638625Srvb		myprintf(("\tgroup %d: (%d)\n",i,cred->cr_groups[i]));
167738625Srvb	myprintf(("\n"));
167838625Srvb}
167938625Srvb
168038625Srvb/*
1681176139Srwatson * Return a vnode for the given fid.  If no cnode exists for this fid create
1682176139Srwatson * one and put it in a table hashed by coda_f2i().  If the cnode for this fid
1683176139Srwatson * is already in the table return it (ref count is incremented by coda_find.
1684176139Srwatson * The cnode will be flushed from the table when coda_inactive calls
1685176139Srwatson * coda_unsave.
168638625Srvb */
168738625Srvbstruct cnode *
1688206210Srwatsonmake_coda_node(struct CodaFid *fid, struct mount *vfsp, short type)
168938625Srvb{
1690176139Srwatson	struct cnode *cp;
1691176307Srwatson	struct vnode *vp;
1692176139Srwatson	int err;
169338625Srvb
1694176307Srwatson	/*
1695176307Srwatson	 * XXXRW: This really needs a moderate amount of reworking.  We need
1696176307Srwatson	 * to properly tolerate failures of getnewvnode() and insmntque(),
1697176307Srwatson	 * and callers need to be able to accept an error back from
1698176307Srwatson	 * make_coda_node.  There may also be more general issues in how we
1699176307Srwatson	 * handle forced unmount.  Finally, if/when Coda loses its dependency
1700176307Srwatson	 * on Giant, the ordering of this needs rethinking.
1701176307Srwatson	 */
1702176307Srwatson	cp = coda_find(fid);
1703176307Srwatson	if (cp != NULL) {
1704176139Srwatson		vref(CTOV(cp));
1705176307Srwatson		return (cp);
1706176307Srwatson	}
1707176307Srwatson	cp = coda_alloc();
1708176307Srwatson	cp->c_fid = *fid;
1709176307Srwatson	err = getnewvnode("coda", vfsp, &coda_vnodeops, &vp);
1710176307Srwatson	if (err)
1711176307Srwatson		panic("coda: getnewvnode returned error %d\n", err);
1712176307Srwatson	vp->v_data = cp;
1713176307Srwatson	vp->v_type = type;
1714176307Srwatson	cp->c_vnode = vp;
1715176307Srwatson	coda_save(cp);
1716176307Srwatson	err = insmntque(vp, vfsp);
1717176307Srwatson	if (err != 0)
1718176307Srwatson		printf("coda: insmntque failed: error %d", err);
1719176139Srwatson	return (cp);
172038625Srvb}
1721111903Stjr
1722111903Stjrint
1723154647Srwatsoncoda_pathconf(struct vop_pathconf_args *ap)
1724111903Stjr{
1725111903Stjr
1726111903Stjr	switch (ap->a_name) {
1727111903Stjr	case _PC_NAME_MAX:
1728176156Srwatson		*ap->a_retval = CODA_MAXNAMLEN;
1729176156Srwatson		return (0);
1730176156Srwatson
1731111903Stjr	case _PC_PATH_MAX:
1732176156Srwatson		*ap->a_retval = CODA_MAXPATHLEN;
1733176156Srwatson		return (0);
1734176156Srwatson
1735111903Stjr	default:
1736176156Srwatson		return (vop_stdpathconf(ap));
1737111903Stjr	}
1738111903Stjr}
1739