coda_vnops.c revision 154647
1/*-
2 *             Coda: an Experimental Distributed File System
3 *                              Release 3.1
4 *
5 *           Copyright (c) 1987-1998 Carnegie Mellon University
6 *                          All Rights Reserved
7 *
8 * Permission  to  use, copy, modify and distribute this software and its
9 * documentation is hereby granted,  provided  that  both  the  copyright
10 * notice  and  this  permission  notice  appear  in  all  copies  of the
11 * software, derivative works or  modified  versions,  and  any  portions
12 * thereof, and that both notices appear in supporting documentation, and
13 * that credit is given to Carnegie Mellon University  in  all  documents
14 * and publicity pertaining to direct or indirect use of this code or its
15 * derivatives.
16 *
17 * CODA IS AN EXPERIMENTAL SOFTWARE SYSTEM AND IS  KNOWN  TO  HAVE  BUGS,
18 * SOME  OF  WHICH MAY HAVE SERIOUS CONSEQUENCES.  CARNEGIE MELLON ALLOWS
19 * FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.   CARNEGIE  MELLON
20 * DISCLAIMS  ANY  LIABILITY  OF  ANY  KIND  FOR  ANY  DAMAGES WHATSOEVER
21 * RESULTING DIRECTLY OR INDIRECTLY FROM THE USE OF THIS SOFTWARE  OR  OF
22 * ANY DERIVATIVE WORK.
23 *
24 * Carnegie  Mellon  encourages  users  of  this  software  to return any
25 * improvements or extensions that  they  make,  and  to  grant  Carnegie
26 * Mellon the rights to redistribute these changes without encumbrance.
27 *
28 *  	@(#) src/sys/coda/coda_vnops.c,v 1.1.1.1 1998/08/29 21:14:52 rvb Exp $
29 */
30/*
31 * Mach Operating System
32 * Copyright (c) 1990 Carnegie-Mellon University
33 * Copyright (c) 1989 Carnegie-Mellon University
34 * All rights reserved.  The CMU software License Agreement specifies
35 * the terms and conditions for use and redistribution.
36 */
37
38/*
39 * This code was written for the Coda filesystem at Carnegie Mellon
40 * University.  Contributers include David Steere, James Kistler, and
41 * M. Satyanarayanan.
42 */
43
44#include <sys/cdefs.h>
45__FBSDID("$FreeBSD: head/sys/fs/coda/coda_vnops.c 154647 2006-01-21 19:51:47Z rwatson $");
46
47#include <sys/param.h>
48#include <sys/systm.h>
49#include <sys/acct.h>
50#include <sys/errno.h>
51#include <sys/fcntl.h>
52#include <sys/kernel.h>
53#include <sys/lock.h>
54#include <sys/malloc.h>
55#include <sys/file.h>		/* Must come after sys/malloc.h */
56#include <sys/mount.h>
57#include <sys/mutex.h>
58#include <sys/namei.h>
59#include <sys/proc.h>
60#include <sys/uio.h>
61#include <sys/unistd.h>
62
63#include <vm/vm.h>
64#include <vm/vm_object.h>
65#include <vm/vm_extern.h>
66
67#include <coda/coda.h>
68#include <coda/cnode.h>
69#include <coda/coda_vnops.h>
70#include <coda/coda_venus.h>
71#include <coda/coda_opstats.h>
72#include <coda/coda_subr.h>
73#include <coda/coda_namecache.h>
74#include <coda/coda_pioctl.h>
75
76/*
77 * These flags select various performance enhancements.
78 */
79int coda_attr_cache  = 1;       /* Set to cache attributes in the kernel */
80int coda_symlink_cache = 1;     /* Set to cache symbolic link information */
81int coda_access_cache = 1;      /* Set to handle some access checks directly */
82
83/* structure to keep track of vfs calls */
84
85struct coda_op_stats coda_vnodeopstats[CODA_VNODEOPS_SIZE];
86
87#define MARK_ENTRY(op) (coda_vnodeopstats[op].entries++)
88#define MARK_INT_SAT(op) (coda_vnodeopstats[op].sat_intrn++)
89#define MARK_INT_FAIL(op) (coda_vnodeopstats[op].unsat_intrn++)
90#define MARK_INT_GEN(op) (coda_vnodeopstats[op].gen_intrn++)
91
92/* What we are delaying for in printf */
93int coda_printf_delay = 0;  /* in microseconds */
94int coda_vnop_print_entry = 0;
95static int coda_lockdebug = 0;
96
97/*
98 * Some NetBSD details:
99 *
100 *   coda_start is called at the end of the mount syscall.
101 *   coda_init is called at boot time.
102 */
103
104#define ENTRY  if(coda_vnop_print_entry) myprintf(("Entered %s\n",__func__))
105
106/* Definition of the vnode operation vector */
107
108struct vop_vector coda_vnodeops = {
109    .vop_default = VOP_PANIC,
110    .vop_lookup = coda_lookup,		/* lookup */
111    .vop_create = coda_create,		/* create */
112    .vop_mknod = VOP_PANIC,	/* mknod */
113    .vop_open = coda_open,		/* open */
114    .vop_close = coda_close,		/* close */
115    .vop_access = coda_access,		/* access */
116    .vop_getattr = coda_getattr,	/* getattr */
117    .vop_setattr = coda_setattr,	/* setattr */
118    .vop_read = coda_read,		/* read */
119    .vop_write = coda_write,		/* write */
120    .vop_ioctl = coda_ioctl,		/* ioctl */
121    .vop_fsync = coda_fsync,		/* fsync */
122    .vop_remove = coda_remove,		/* remove */
123    .vop_link = coda_link,		/* link */
124    .vop_rename = coda_rename,		/* rename */
125    .vop_mkdir = coda_mkdir,		/* mkdir */
126    .vop_rmdir = coda_rmdir,		/* rmdir */
127    .vop_symlink = coda_symlink,	/* symlink */
128    .vop_readdir = coda_readdir,	/* readdir */
129    .vop_readlink = coda_readlink,	/* readlink */
130    .vop_inactive = coda_inactive,	/* inactive */
131    .vop_reclaim = coda_reclaim,	/* reclaim */
132    .vop_lock = coda_lock,		/* lock */
133    .vop_unlock = coda_unlock,		/* unlock */
134    .vop_bmap = coda_bmap,		/* bmap */
135    .vop_print = VOP_PANIC,	/* print */
136    .vop_islocked = coda_islocked,	/* islocked */
137    .vop_pathconf = coda_pathconf,	/* pathconf */
138    .vop_advlock = VOP_NULL,	/* advlock */
139    .vop_lease = VOP_NULL,		/* lease */
140    .vop_poll = vop_stdpoll,
141    .vop_getpages = vop_stdgetpages,	/* pager intf.*/
142    .vop_putpages = vop_stdputpages,	/* pager intf.*/
143    .vop_getwritemount =	vop_stdgetwritemount,
144
145#if 0
146    missing
147    .vop_cachedlookup =	ufs_lookup,
148    .vop_whiteout =	ufs_whiteout,
149#endif
150
151};
152
153/* A generic do-nothing.  For lease_check, advlock */
154int
155coda_vop_nop(void *anon) {
156    struct vnodeop_desc **desc = (struct vnodeop_desc **)anon;
157
158    if (codadebug) {
159	myprintf(("Vnode operation %s called, but unsupported\n",
160		  (*desc)->vdesc_name));
161    }
162   return (0);
163}
164
165int
166coda_vnodeopstats_init(void)
167{
168	register int i;
169
170	for(i=0;i<CODA_VNODEOPS_SIZE;i++) {
171		coda_vnodeopstats[i].opcode = i;
172		coda_vnodeopstats[i].entries = 0;
173		coda_vnodeopstats[i].sat_intrn = 0;
174		coda_vnodeopstats[i].unsat_intrn = 0;
175		coda_vnodeopstats[i].gen_intrn = 0;
176	}
177	return 0;
178}
179
180/*
181 * coda_open calls Venus to return the device, inode pair of the cache
182 * file holding the data. Using iget, coda_open finds the vnode of the
183 * cache file, and then opens it.
184 */
185int
186coda_open(struct vop_open_args *ap)
187{
188    /*
189     * NetBSD can pass the O_EXCL flag in mode, even though the check
190     * has already happened.  Venus defensively assumes that if open
191     * is passed the EXCL, it must be a bug.  We strip the flag here.
192     */
193/* true args */
194    register struct vnode **vpp = &(ap->a_vp);
195    struct cnode *cp = VTOC(*vpp);
196    int flag = ap->a_mode & (~O_EXCL);
197    struct ucred *cred = ap->a_cred;
198    struct thread *td = ap->a_td;
199/* locals */
200    int error;
201    struct vnode *vp;
202    struct cdev *dev;
203    ino_t inode;
204
205    MARK_ENTRY(CODA_OPEN_STATS);
206
207    /* Check for open of control file. */
208    if (IS_CTL_VP(*vpp)) {
209	/* XXX */
210	/* if (WRITEABLE(flag)) */
211	if (flag & (FWRITE | O_TRUNC | O_CREAT | O_EXCL)) {
212	    MARK_INT_FAIL(CODA_OPEN_STATS);
213	    return(EACCES);
214	}
215	MARK_INT_SAT(CODA_OPEN_STATS);
216	return(0);
217    }
218
219    error = venus_open(vtomi((*vpp)), &cp->c_fid, flag, cred, td->td_proc, &dev, &inode);
220    if (error)
221	return (error);
222    if (!error) {
223	CODADEBUG( CODA_OPEN,myprintf(("open: dev %#lx inode %lu result %d\n",
224				       (u_long)dev2udev(dev), (u_long)inode,
225				       error)); )
226    }
227
228    /* Translate the <device, inode> pair for the cache file into
229       an inode pointer. */
230    error = coda_grab_vnode(dev, inode, &vp);
231    if (error)
232	return (error);
233
234    /* We get the vnode back locked.  Needs unlocked */
235    VOP_UNLOCK(vp, 0, td);
236    /* Keep a reference until the close comes in. */
237    vref(*vpp);
238
239    /* Save the vnode pointer for the cache file. */
240    if (cp->c_ovp == NULL) {
241	cp->c_ovp = vp;
242    } else {
243	if (cp->c_ovp != vp)
244	    panic("coda_open:  cp->c_ovp != ITOV(ip)");
245    }
246    cp->c_ocount++;
247
248    /* Flush the attribute cached if writing the file. */
249    if (flag & FWRITE) {
250	cp->c_owrite++;
251	cp->c_flags &= ~C_VATTR;
252    }
253
254    /* Save the <device, inode> pair for the cache file to speed
255       up subsequent page_read's. */
256    cp->c_device = dev;
257    cp->c_inode = inode;
258
259    /* Open the cache file. */
260    error = VOP_OPEN(vp, flag, cred, td, -1);
261    if (error) {
262    	printf("coda_open: VOP_OPEN on container failed %d\n", error);
263	return (error);
264    }
265/* grab (above) does this when it calls newvnode unless it's in the cache*/
266
267    return(error);
268}
269
270/*
271 * Close the cache file used for I/O and notify Venus.
272 */
273int
274coda_close(struct vop_close_args *ap)
275{
276/* true args */
277    struct vnode *vp = ap->a_vp;
278    struct cnode *cp = VTOC(vp);
279    int flag = ap->a_fflag;
280    struct ucred *cred = ap->a_cred;
281    struct thread *td = ap->a_td;
282/* locals */
283    int error;
284
285    MARK_ENTRY(CODA_CLOSE_STATS);
286
287    /* Check for close of control file. */
288    if (IS_CTL_VP(vp)) {
289	MARK_INT_SAT(CODA_CLOSE_STATS);
290	return(0);
291    }
292
293    if (IS_UNMOUNTING(cp)) {
294	if (cp->c_ovp) {
295#ifdef	CODA_VERBOSE
296	    printf("coda_close: destroying container ref %d, ufs vp %p of vp %p/cp %p\n",
297		    vrefcnt(vp), cp->c_ovp, vp, cp);
298#endif
299#ifdef	hmm
300	    vgone(cp->c_ovp);
301#else
302	    VOP_CLOSE(cp->c_ovp, flag, cred, td); /* Do errors matter here? */
303	    vrele(cp->c_ovp);
304#endif
305	} else {
306#ifdef	CODA_VERBOSE
307	    printf("coda_close: NO container vp %p/cp %p\n", vp, cp);
308#endif
309	}
310	return ENODEV;
311    } else {
312	VOP_CLOSE(cp->c_ovp, flag, cred, td); /* Do errors matter here? */
313	vrele(cp->c_ovp);
314    }
315
316    if (--cp->c_ocount == 0)
317	cp->c_ovp = NULL;
318
319    if (flag & FWRITE)                    /* file was opened for write */
320	--cp->c_owrite;
321
322    error = venus_close(vtomi(vp), &cp->c_fid, flag, cred, td->td_proc);
323    vrele(CTOV(cp));
324
325    CODADEBUG(CODA_CLOSE, myprintf(("close: result %d\n",error)); )
326    return(error);
327}
328
329int
330coda_read(struct vop_read_args *ap)
331{
332
333    ENTRY;
334    return(coda_rdwr(ap->a_vp, ap->a_uio, UIO_READ,
335		    ap->a_ioflag, ap->a_cred, ap->a_uio->uio_td));
336}
337
338int
339coda_write(struct vop_write_args *ap)
340{
341
342    ENTRY;
343    return(coda_rdwr(ap->a_vp, ap->a_uio, UIO_WRITE,
344		    ap->a_ioflag, ap->a_cred, ap->a_uio->uio_td));
345}
346
347int
348coda_rdwr(struct vnode *vp, struct uio *uiop, enum uio_rw rw, int ioflag,
349    struct ucred *cred, struct thread *td)
350{
351/* upcall decl */
352  /* NOTE: container file operation!!! */
353/* locals */
354    struct cnode *cp = VTOC(vp);
355    struct vnode *cfvp = cp->c_ovp;
356    struct proc *p = td->td_proc;
357    struct thread *ltd = td;
358    int igot_internally = 0;
359    int opened_internally = 0;
360    int error = 0;
361    int iscore = 0;
362
363    MARK_ENTRY(CODA_RDWR_STATS);
364
365    CODADEBUG(CODA_RDWR, myprintf(("coda_rdwr(%d, %p, %d, %lld, %d)\n", rw,
366			      (void *)uiop->uio_iov->iov_base, uiop->uio_resid,
367			      (long long)uiop->uio_offset, uiop->uio_segflg)); )
368
369    /* Check for rdwr of control object. */
370    if (IS_CTL_VP(vp)) {
371	MARK_INT_FAIL(CODA_RDWR_STATS);
372	return(EINVAL);
373    }
374
375    /*
376     * If file is not already open this must be a page
377     * {read,write} request.  Iget the cache file's inode
378     * pointer if we still have its <device, inode> pair.
379     * Otherwise, we must do an internal open to derive the
380     * pair.
381     */
382    if (cfvp == NULL) {
383	/*
384	 * If we're dumping core, do the internal open. Otherwise
385	 * venus won't have the correct size of the core when
386	 * it's completely written.
387	 */
388	if (p) {
389	    PROC_LOCK(p);
390	    iscore = (p->p_acflag & ACORE);
391	    PROC_UNLOCK(p);
392	}
393	else
394	    ltd = curthread;
395
396	if (cp->c_inode != 0 && !iscore) {
397	    igot_internally = 1;
398	    error = coda_grab_vnode(cp->c_device, cp->c_inode, &cfvp);
399	    if (error) {
400		MARK_INT_FAIL(CODA_RDWR_STATS);
401		return(error);
402	    }
403	    /*
404	     * We get the vnode back locked by curthread in both Mach and
405	     * NetBSD.  Needs unlocked
406	     */
407	    VOP_UNLOCK(cfvp, 0, ltd);
408	}
409	else {
410	    opened_internally = 1;
411	    MARK_INT_GEN(CODA_OPEN_STATS);
412	    error = VOP_OPEN(vp, (rw == UIO_READ ? FREAD : FWRITE),
413			     cred, td, -1);
414printf("coda_rdwr: Internally Opening %p\n", vp);
415	    if (error) {
416		printf("coda_rdwr: VOP_OPEN on container failed %d\n", error);
417		return (error);
418	    }
419	    cfvp = cp->c_ovp;
420	}
421    }
422
423    /* Have UFS handle the call. */
424    CODADEBUG(CODA_RDWR, myprintf(("indirect rdwr: fid = %s, refcnt = %d\n",
425			     coda_f2s(&cp->c_fid), CTOV(cp)->v_usecount)); )
426    if (rw == UIO_READ) {
427	error = VOP_READ(cfvp, uiop, ioflag, cred);
428    } else {
429	error = VOP_WRITE(cfvp, uiop, ioflag, cred);
430	/* ufs_write updates the vnode_pager_setsize for the vnode/object */
431
432	{   struct vattr attr;
433
434	    if (VOP_GETATTR(cfvp, &attr, cred, td) == 0) {
435		vnode_pager_setsize(vp, attr.va_size);
436	    }
437	}
438    }
439
440    if (error)
441	MARK_INT_FAIL(CODA_RDWR_STATS);
442    else
443	MARK_INT_SAT(CODA_RDWR_STATS);
444
445    /* Do an internal close if necessary. */
446    if (opened_internally) {
447	MARK_INT_GEN(CODA_CLOSE_STATS);
448	(void)VOP_CLOSE(vp, (rw == UIO_READ ? FREAD : FWRITE), cred, td);
449    }
450
451    /* Invalidate cached attributes if writing. */
452    if (rw == UIO_WRITE)
453	cp->c_flags &= ~C_VATTR;
454    return(error);
455}
456
457
458
459int
460coda_ioctl(struct vop_ioctl_args *ap)
461{
462/* true args */
463    struct vnode *vp = ap->a_vp;
464    int com = ap->a_command;
465    caddr_t data = ap->a_data;
466    int flag = ap->a_fflag;
467    struct ucred *cred = ap->a_cred;
468    struct thread *td = ap->a_td;
469/* locals */
470    int error;
471    struct vnode *tvp;
472    struct nameidata ndp;
473    struct PioctlData *iap = (struct PioctlData *)data;
474
475    MARK_ENTRY(CODA_IOCTL_STATS);
476
477    CODADEBUG(CODA_IOCTL, myprintf(("in coda_ioctl on %s\n", iap->path));)
478
479    /* Don't check for operation on a dying object, for ctlvp it
480       shouldn't matter */
481
482    /* Must be control object to succeed. */
483    if (!IS_CTL_VP(vp)) {
484	MARK_INT_FAIL(CODA_IOCTL_STATS);
485	CODADEBUG(CODA_IOCTL, myprintf(("coda_ioctl error: vp != ctlvp"));)
486	    return (EOPNOTSUPP);
487    }
488    /* Look up the pathname. */
489
490    /* Should we use the name cache here? It would get it from
491       lookupname sooner or later anyway, right? */
492
493    NDINIT(&ndp, LOOKUP, (iap->follow ? FOLLOW : NOFOLLOW), UIO_USERSPACE, iap->path, td);
494    error = namei(&ndp);
495    tvp = ndp.ni_vp;
496
497    if (error) {
498	MARK_INT_FAIL(CODA_IOCTL_STATS);
499	CODADEBUG(CODA_IOCTL, myprintf(("coda_ioctl error: lookup returns %d\n",
500				   error));)
501	return(error);
502    }
503
504    /*
505     * Make sure this is a coda style cnode, but it may be a
506     * different vfsp
507     */
508    if (tvp->v_op != &coda_vnodeops) {
509	vrele(tvp);
510	NDFREE(&ndp, NDF_ONLY_PNBUF);
511	MARK_INT_FAIL(CODA_IOCTL_STATS);
512	CODADEBUG(CODA_IOCTL,
513		 myprintf(("coda_ioctl error: %s not a coda object\n",
514			iap->path));)
515	return(EINVAL);
516    }
517
518    if (iap->vi.in_size > VC_MAXDATASIZE) {
519	NDFREE(&ndp, 0);
520	return(EINVAL);
521    }
522    error = venus_ioctl(vtomi(tvp), &((VTOC(tvp))->c_fid), com, flag, data, cred, td->td_proc);
523
524    if (error)
525	MARK_INT_FAIL(CODA_IOCTL_STATS);
526    else
527	CODADEBUG(CODA_IOCTL, myprintf(("Ioctl returns %d \n", error)); )
528
529    vrele(tvp);
530    NDFREE(&ndp, NDF_ONLY_PNBUF);
531    return(error);
532}
533
534/*
535 * To reduce the cost of a user-level venus;we cache attributes in
536 * the kernel.  Each cnode has storage allocated for an attribute. If
537 * c_vattr is valid, return a reference to it. Otherwise, get the
538 * attributes from venus and store them in the cnode.  There is some
539 * question if this method is a security leak. But I think that in
540 * order to make this call, the user must have done a lookup and
541 * opened the file, and therefore should already have access.
542 */
543int
544coda_getattr(struct vop_getattr_args *ap)
545{
546/* true args */
547    struct vnode *vp = ap->a_vp;
548    struct cnode *cp = VTOC(vp);
549    struct vattr *vap = ap->a_vap;
550    struct ucred *cred = ap->a_cred;
551    struct thread *td = ap->a_td;
552/* locals */
553    int error;
554
555    MARK_ENTRY(CODA_GETATTR_STATS);
556
557    if (IS_UNMOUNTING(cp))
558	return ENODEV;
559
560    /* Check for getattr of control object. */
561    if (IS_CTL_VP(vp)) {
562	MARK_INT_FAIL(CODA_GETATTR_STATS);
563	return(ENOENT);
564    }
565
566    /* Check to see if the attributes have already been cached */
567    if (VALID_VATTR(cp)) {
568	CODADEBUG(CODA_GETATTR, { myprintf(("attr cache hit: %s\n",
569					coda_f2s(&cp->c_fid)));});
570	CODADEBUG(CODA_GETATTR, if (!(codadebug & ~CODA_GETATTR))
571		 print_vattr(&cp->c_vattr); );
572
573	*vap = cp->c_vattr;
574	MARK_INT_SAT(CODA_GETATTR_STATS);
575	return(0);
576    }
577
578    error = venus_getattr(vtomi(vp), &cp->c_fid, cred, td->td_proc, vap);
579
580    if (!error) {
581	CODADEBUG(CODA_GETATTR, myprintf(("getattr miss %s: result %d\n",
582				     coda_f2s(&cp->c_fid), error)); )
583
584	CODADEBUG(CODA_GETATTR, if (!(codadebug & ~CODA_GETATTR))
585		 print_vattr(vap);	);
586
587    {	int size = vap->va_size;
588    	struct vnode *convp = cp->c_ovp;
589	if (convp != (struct vnode *)0) {
590	    vnode_pager_setsize(convp, size);
591	}
592    }
593	/* If not open for write, store attributes in cnode */
594	if ((cp->c_owrite == 0) && (coda_attr_cache)) {
595	    cp->c_vattr = *vap;
596	    cp->c_flags |= C_VATTR;
597	}
598
599    }
600    return(error);
601}
602
603int
604coda_setattr(struct vop_setattr_args *ap)
605{
606/* true args */
607    register struct vnode *vp = ap->a_vp;
608    struct cnode *cp = VTOC(vp);
609    register struct vattr *vap = ap->a_vap;
610    struct ucred *cred = ap->a_cred;
611    struct thread *td = ap->a_td;
612/* locals */
613    int error;
614
615    MARK_ENTRY(CODA_SETATTR_STATS);
616
617    /* Check for setattr of control object. */
618    if (IS_CTL_VP(vp)) {
619	MARK_INT_FAIL(CODA_SETATTR_STATS);
620	return(ENOENT);
621    }
622
623    if (codadebug & CODADBGMSK(CODA_SETATTR)) {
624	print_vattr(vap);
625    }
626    error = venus_setattr(vtomi(vp), &cp->c_fid, vap, cred, td->td_proc);
627
628    if (!error)
629	cp->c_flags &= ~C_VATTR;
630
631    {	int size = vap->va_size;
632    	struct vnode *convp = cp->c_ovp;
633	if (size != VNOVAL && convp != (struct vnode *)0) {
634	    vnode_pager_setsize(convp, size);
635	}
636    }
637    CODADEBUG(CODA_SETATTR,	myprintf(("setattr %d\n", error)); )
638    return(error);
639}
640
641int
642coda_access(struct vop_access_args *ap)
643{
644/* true args */
645    struct vnode *vp = ap->a_vp;
646    struct cnode *cp = VTOC(vp);
647    int mode = ap->a_mode;
648    struct ucred *cred = ap->a_cred;
649    struct thread *td = ap->a_td;
650/* locals */
651    int error;
652
653    MARK_ENTRY(CODA_ACCESS_STATS);
654
655    /* Check for access of control object.  Only read access is
656       allowed on it. */
657    if (IS_CTL_VP(vp)) {
658	/* bogus hack - all will be marked as successes */
659	MARK_INT_SAT(CODA_ACCESS_STATS);
660	return(((mode & VREAD) && !(mode & (VWRITE | VEXEC)))
661	       ? 0 : EACCES);
662    }
663
664    /*
665     * if the file is a directory, and we are checking exec (eg lookup)
666     * access, and the file is in the namecache, then the user must have
667     * lookup access to it.
668     */
669    if (coda_access_cache) {
670	if ((vp->v_type == VDIR) && (mode & VEXEC)) {
671	    if (coda_nc_lookup(cp, ".", 1, cred)) {
672		MARK_INT_SAT(CODA_ACCESS_STATS);
673		return(0);                     /* it was in the cache */
674	    }
675	}
676    }
677
678    error = venus_access(vtomi(vp), &cp->c_fid, mode, cred, td->td_proc);
679
680    return(error);
681}
682
683int
684coda_readlink(struct vop_readlink_args *ap)
685{
686/* true args */
687    struct vnode *vp = ap->a_vp;
688    struct cnode *cp = VTOC(vp);
689    struct uio *uiop = ap->a_uio;
690    struct ucred *cred = ap->a_cred;
691    struct thread *td = ap->a_uio->uio_td;
692/* locals */
693    int error;
694    char *str;
695    int len;
696
697    MARK_ENTRY(CODA_READLINK_STATS);
698
699    /* Check for readlink of control object. */
700    if (IS_CTL_VP(vp)) {
701	MARK_INT_FAIL(CODA_READLINK_STATS);
702	return(ENOENT);
703    }
704
705    if ((coda_symlink_cache) && (VALID_SYMLINK(cp))) { /* symlink was cached */
706	uiop->uio_rw = UIO_READ;
707	error = uiomove(cp->c_symlink, (int)cp->c_symlen, uiop);
708	if (error)
709	    MARK_INT_FAIL(CODA_READLINK_STATS);
710	else
711	    MARK_INT_SAT(CODA_READLINK_STATS);
712	return(error);
713    }
714
715    error = venus_readlink(vtomi(vp), &cp->c_fid, cred,
716        td != NULL ? td->td_proc : NULL, &str, &len);
717
718    if (!error) {
719	uiop->uio_rw = UIO_READ;
720	error = uiomove(str, len, uiop);
721
722	if (coda_symlink_cache) {
723	    cp->c_symlink = str;
724	    cp->c_symlen = len;
725	    cp->c_flags |= C_SYMLINK;
726	} else
727	    CODA_FREE(str, len);
728    }
729
730    CODADEBUG(CODA_READLINK, myprintf(("in readlink result %d\n",error));)
731    return(error);
732}
733
734int
735coda_fsync(struct vop_fsync_args *ap)
736{
737/* true args */
738    struct vnode *vp = ap->a_vp;
739    struct cnode *cp = VTOC(vp);
740    struct thread *td = ap->a_td;
741/* locals */
742    struct vnode *convp = cp->c_ovp;
743    int error;
744
745    MARK_ENTRY(CODA_FSYNC_STATS);
746
747    /* Check for fsync on an unmounting object */
748    /* The NetBSD kernel, in it's infinite wisdom, can try to fsync
749     * after an unmount has been initiated.  This is a Bad Thing,
750     * which we have to avoid.  Not a legitimate failure for stats.
751     */
752    if (IS_UNMOUNTING(cp)) {
753	return(ENODEV);
754    }
755
756    /* Check for fsync of control object. */
757    if (IS_CTL_VP(vp)) {
758	MARK_INT_SAT(CODA_FSYNC_STATS);
759	return(0);
760    }
761
762    if (convp)
763    	VOP_FSYNC(convp, MNT_WAIT, td);
764
765    /*
766     * We see fsyncs with usecount == 1 then usecount == 0.
767     * For now we ignore them.
768     */
769    /*
770    VI_LOCK(vp);
771    if (!vp->v_usecount) {
772    	printf("coda_fsync on vnode %p with %d usecount.  c_flags = %x (%x)\n",
773		vp, vp->v_usecount, cp->c_flags, cp->c_flags&C_PURGING);
774    }
775    VI_UNLOCK(vp);
776    */
777
778    /*
779     * We can expect fsync on any vnode at all if venus is pruging it.
780     * Venus can't very well answer the fsync request, now can it?
781     * Hopefully, it won't have to, because hopefully, venus preserves
782     * the (possibly untrue) invariant that it never purges an open
783     * vnode.  Hopefully.
784     */
785    if (cp->c_flags & C_PURGING) {
786	return(0);
787    }
788
789    /* needs research */
790    return 0;
791    error = venus_fsync(vtomi(vp), &cp->c_fid, td->td_proc);
792
793    CODADEBUG(CODA_FSYNC, myprintf(("in fsync result %d\n",error)); );
794    return(error);
795}
796
797int
798coda_inactive(struct vop_inactive_args *ap)
799{
800    /* XXX - at the moment, inactive doesn't look at cred, and doesn't
801       have a proc pointer.  Oops. */
802/* true args */
803    struct vnode *vp = ap->a_vp;
804    struct cnode *cp = VTOC(vp);
805    struct ucred *cred __attribute__((unused)) = NULL;
806    struct thread *td __attribute__((unused)) = curthread;
807/* upcall decl */
808/* locals */
809
810    /* We don't need to send inactive to venus - DCS */
811    MARK_ENTRY(CODA_INACTIVE_STATS);
812
813    if (IS_CTL_VP(vp)) {
814	MARK_INT_SAT(CODA_INACTIVE_STATS);
815	return 0;
816    }
817
818    CODADEBUG(CODA_INACTIVE, myprintf(("in inactive, %s, vfsp %p\n",
819				  coda_f2s(&cp->c_fid), vp->v_mount));)
820
821    /* If an array has been allocated to hold the symlink, deallocate it */
822    if ((coda_symlink_cache) && (VALID_SYMLINK(cp))) {
823	if (cp->c_symlink == NULL)
824	    panic("coda_inactive: null symlink pointer in cnode");
825
826	CODA_FREE(cp->c_symlink, cp->c_symlen);
827	cp->c_flags &= ~C_SYMLINK;
828	cp->c_symlen = 0;
829    }
830
831    /* Remove it from the table so it can't be found. */
832    coda_unsave(cp);
833    if ((struct coda_mntinfo *)(vp->v_mount->mnt_data) == NULL) {
834	myprintf(("Help! vfsp->vfs_data was NULL, but vnode %p wasn't dying\n", vp));
835	panic("badness in coda_inactive\n");
836    }
837
838    if (IS_UNMOUNTING(cp)) {
839#ifdef	DEBUG
840	printf("coda_inactive: IS_UNMOUNTING use %d: vp %p, cp %p\n", vrefcnt(vp), vp, cp);
841	if (cp->c_ovp != NULL)
842	    printf("coda_inactive: cp->ovp != NULL use %d: vp %p, cp %p\n",
843	    	   vrefcnt(vp), vp, cp);
844#endif
845    } else {
846#ifdef OLD_DIAGNOSTIC
847	if (vrefcnt(CTOV(cp))) {
848	    panic("coda_inactive: nonzero reference count");
849	}
850	if (cp->c_ovp != NULL) {
851	    panic("coda_inactive:  cp->ovp != NULL");
852	}
853#endif
854	vgone(vp);
855    }
856
857    MARK_INT_SAT(CODA_INACTIVE_STATS);
858    return(0);
859}
860
861/*
862 * Remote filesystem operations having to do with directory manipulation.
863 */
864
865/*
866 * It appears that in NetBSD, lookup is supposed to return the vnode locked
867 */
868int
869coda_lookup(struct vop_lookup_args *ap)
870{
871/* true args */
872    struct vnode *dvp = ap->a_dvp;
873    struct cnode *dcp = VTOC(dvp);
874    struct vnode **vpp = ap->a_vpp;
875    /*
876     * It looks as though ap->a_cnp->ni_cnd->cn_nameptr holds the rest
877     * of the string to xlate, and that we must try to get at least
878     * ap->a_cnp->ni_cnd->cn_namelen of those characters to macth.  I
879     * could be wrong.
880     */
881    struct componentname  *cnp = ap->a_cnp;
882    struct ucred *cred = cnp->cn_cred;
883    struct thread *td = cnp->cn_thread;
884/* locals */
885    struct cnode *cp;
886    const char *nm = cnp->cn_nameptr;
887    int len = cnp->cn_namelen;
888    CodaFid VFid;
889    int	vtype;
890    int error = 0;
891
892    MARK_ENTRY(CODA_LOOKUP_STATS);
893
894    CODADEBUG(CODA_LOOKUP, myprintf(("lookup: %s in %s\n",
895				   nm, coda_f2s(&dcp->c_fid))););
896
897    /* Check for lookup of control object. */
898    if (IS_CTL_NAME(dvp, nm, len)) {
899	*vpp = coda_ctlvp;
900	vref(*vpp);
901	MARK_INT_SAT(CODA_LOOKUP_STATS);
902	goto exit;
903    }
904
905    if (len+1 > CODA_MAXNAMLEN) {
906	MARK_INT_FAIL(CODA_LOOKUP_STATS);
907
908	CODADEBUG(CODA_LOOKUP, myprintf(("name too long: lookup, %s (%s)\n",
909					 coda_f2s(&dcp->c_fid), nm)););
910	*vpp = (struct vnode *)0;
911	error = EINVAL;
912	goto exit;
913    }
914    /* First try to look the file up in the cfs name cache */
915    /* lock the parent vnode? */
916    cp = coda_nc_lookup(dcp, nm, len, cred);
917    if (cp) {
918	*vpp = CTOV(cp);
919	vref(*vpp);
920	CODADEBUG(CODA_LOOKUP,
921		 myprintf(("lookup result %d vpp %p\n",error,*vpp));)
922    } else {
923
924	/* The name wasn't cached, so we need to contact Venus */
925	error = venus_lookup(vtomi(dvp), &dcp->c_fid, nm, len, cred, td->td_proc, &VFid, &vtype);
926
927	if (error) {
928	    MARK_INT_FAIL(CODA_LOOKUP_STATS);
929
930	    CODADEBUG(CODA_LOOKUP, myprintf(("lookup error on %s (%s)%d\n",
931					     coda_f2s(&dcp->c_fid), nm, error));)
932	    *vpp = (struct vnode *)0;
933	} else {
934	    MARK_INT_SAT(CODA_LOOKUP_STATS);
935	    CODADEBUG(CODA_LOOKUP,
936		     myprintf(("lookup: %s type %o result %d\n",
937			       coda_f2s(&VFid), vtype, error)); )
938	    cp = make_coda_node(&VFid, dvp->v_mount, vtype);
939	    *vpp = CTOV(cp);
940
941	    /* enter the new vnode in the Name Cache only if the top bit isn't set */
942	    /* And don't enter a new vnode for an invalid one! */
943	    if (!(vtype & CODA_NOCACHE))
944		coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
945	}
946    }
947
948 exit:
949    /*
950     * If we are creating, and this was the last name to be looked up,
951     * and the error was ENOENT, then there really shouldn't be an
952     * error and we can make the leaf NULL and return success.  Since
953     * this is supposed to work under Mach as well as NetBSD, we're
954     * leaving this fn wrapped.  We also must tell lookup/namei that
955     * we need to save the last component of the name.  (Create will
956     * have to free the name buffer later...lucky us...)
957     */
958    if (((cnp->cn_nameiop == CREATE) || (cnp->cn_nameiop == RENAME))
959	&& (cnp->cn_flags & ISLASTCN)
960	&& (error == ENOENT))
961    {
962	error = EJUSTRETURN;
963	cnp->cn_flags |= SAVENAME;
964	*ap->a_vpp = NULL;
965    }
966
967    /*
968     * If we are removing, and we are at the last element, and we
969     * found it, then we need to keep the name around so that the
970     * removal will go ahead as planned.  Unfortunately, this will
971     * probably also lock the to-be-removed vnode, which may or may
972     * not be a good idea.  I'll have to look at the bits of
973     * coda_remove to make sure.  We'll only save the name if we did in
974     * fact find the name, otherwise coda_remove won't have a chance
975     * to free the pathname.
976     */
977    if ((cnp->cn_nameiop == DELETE)
978	&& (cnp->cn_flags & ISLASTCN)
979	&& !error)
980    {
981	cnp->cn_flags |= SAVENAME;
982    }
983
984    /*
985     * If the lookup went well, we need to (potentially?) unlock the
986     * parent, and lock the child.  We are only responsible for
987     * checking to see if the parent is supposed to be unlocked before
988     * we return.  We must always lock the child (provided there is
989     * one, and (the parent isn't locked or it isn't the same as the
990     * parent.)  Simple, huh?  We can never leave the parent locked unless
991     * we are ISLASTCN
992     */
993    if (!error || (error == EJUSTRETURN)) {
994	if (cnp->cn_flags & ISDOTDOT) {
995	    if ((error = VOP_UNLOCK(dvp, 0, td))) {
996		return error;
997	    }
998	    /*
999	     * The parent is unlocked.  As long as there is a child,
1000	     * lock it without bothering to check anything else.
1001	     */
1002	    if (*ap->a_vpp) {
1003		if ((error = VOP_LOCK(*ap->a_vpp, LK_EXCLUSIVE, td))) {
1004		    vn_lock(dvp, LK_RETRY|LK_EXCLUSIVE, td);
1005		    return (error);
1006		}
1007	    }
1008	    vn_lock(dvp, LK_RETRY|LK_EXCLUSIVE, td);
1009	} else {
1010	    /* The parent is locked, and may be the same as the child */
1011	    if (*ap->a_vpp && (*ap->a_vpp != dvp)) {
1012		/* Different, go ahead and lock it. */
1013		if ((error = VOP_LOCK(*ap->a_vpp, LK_EXCLUSIVE, td))) {
1014		    return (error);
1015		}
1016	    }
1017	}
1018    } else {
1019	/* If the lookup failed, we need to ensure that the leaf is NULL */
1020	/* Don't change any locking? */
1021	*ap->a_vpp = NULL;
1022    }
1023    return(error);
1024}
1025
1026/*ARGSUSED*/
1027int
1028coda_create(struct vop_create_args *ap)
1029{
1030/* true args */
1031    struct vnode *dvp = ap->a_dvp;
1032    struct cnode *dcp = VTOC(dvp);
1033    struct vattr *va = ap->a_vap;
1034    int exclusive = 1;
1035    int mode = ap->a_vap->va_mode;
1036    struct vnode **vpp = ap->a_vpp;
1037    struct componentname  *cnp = ap->a_cnp;
1038    struct ucred *cred = cnp->cn_cred;
1039    struct thread *td = cnp->cn_thread;
1040/* locals */
1041    int error;
1042    struct cnode *cp;
1043    const char *nm = cnp->cn_nameptr;
1044    int len = cnp->cn_namelen;
1045    CodaFid VFid;
1046    struct vattr attr;
1047
1048    MARK_ENTRY(CODA_CREATE_STATS);
1049
1050    /* All creates are exclusive XXX */
1051    /* I'm assuming the 'mode' argument is the file mode bits XXX */
1052
1053    /* Check for create of control object. */
1054    if (IS_CTL_NAME(dvp, nm, len)) {
1055	*vpp = (struct vnode *)0;
1056	MARK_INT_FAIL(CODA_CREATE_STATS);
1057	return(EACCES);
1058    }
1059
1060    error = venus_create(vtomi(dvp), &dcp->c_fid, nm, len, exclusive, mode, va, cred, td->td_proc, &VFid, &attr);
1061
1062    if (!error) {
1063
1064	/* If this is an exclusive create, panic if the file already exists. */
1065	/* Venus should have detected the file and reported EEXIST. */
1066
1067	if ((exclusive == 1) &&
1068	    (coda_find(&VFid) != NULL))
1069	    panic("cnode existed for newly created file!");
1070
1071	cp = make_coda_node(&VFid, dvp->v_mount, attr.va_type);
1072	*vpp = CTOV(cp);
1073
1074	/* Update va to reflect the new attributes. */
1075	(*va) = attr;
1076
1077	/* Update the attribute cache and mark it as valid */
1078	if (coda_attr_cache) {
1079	    VTOC(*vpp)->c_vattr = attr;
1080	    VTOC(*vpp)->c_flags |= C_VATTR;
1081	}
1082
1083	/* Invalidate the parent's attr cache, the modification time has changed */
1084	VTOC(dvp)->c_flags &= ~C_VATTR;
1085
1086	/* enter the new vnode in the Name Cache */
1087	coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
1088
1089	CODADEBUG(CODA_CREATE,
1090		  myprintf(("create: %s, result %d\n",
1091			   coda_f2s(&VFid), error)); )
1092    } else {
1093	*vpp = (struct vnode *)0;
1094	CODADEBUG(CODA_CREATE, myprintf(("create error %d\n", error));)
1095    }
1096
1097    if (!error) {
1098	if (cnp->cn_flags & LOCKLEAF) {
1099	    if ((error = VOP_LOCK(*ap->a_vpp, LK_EXCLUSIVE, td))) {
1100		printf("coda_create: ");
1101		panic("unlocked parent but couldn't lock child");
1102	    }
1103	}
1104#ifdef OLD_DIAGNOSTIC
1105	else {
1106	    printf("coda_create: LOCKLEAF not set!\n");
1107	}
1108#endif
1109    }
1110    return(error);
1111}
1112
1113int
1114coda_remove(struct vop_remove_args *ap)
1115{
1116/* true args */
1117    struct vnode *dvp = ap->a_dvp;
1118    struct cnode *cp = VTOC(dvp);
1119    struct componentname  *cnp = ap->a_cnp;
1120    struct ucred *cred = cnp->cn_cred;
1121    struct thread *td = cnp->cn_thread;
1122/* locals */
1123    int error;
1124    const char *nm = cnp->cn_nameptr;
1125    int len = cnp->cn_namelen;
1126    struct cnode *tp;
1127
1128    MARK_ENTRY(CODA_REMOVE_STATS);
1129
1130    CODADEBUG(CODA_REMOVE, myprintf(("remove: %s in %s\n",
1131				     nm, coda_f2s(&cp->c_fid))););
1132    /* Remove the file's entry from the CODA Name Cache */
1133    /* We're being conservative here, it might be that this person
1134     * doesn't really have sufficient access to delete the file
1135     * but we feel zapping the entry won't really hurt anyone -- dcs
1136     */
1137    /* I'm gonna go out on a limb here. If a file and a hardlink to it
1138     * exist, and one is removed, the link count on the other will be
1139     * off by 1. We could either invalidate the attrs if cached, or
1140     * fix them. I'll try to fix them. DCS 11/8/94
1141     */
1142    tp = coda_nc_lookup(VTOC(dvp), nm, len, cred);
1143    if (tp) {
1144	if (VALID_VATTR(tp)) {	/* If attrs are cached */
1145	    if (tp->c_vattr.va_nlink > 1) {	/* If it's a hard link */
1146		tp->c_vattr.va_nlink--;
1147	    }
1148	}
1149
1150	coda_nc_zapfile(VTOC(dvp), nm, len);
1151	/* No need to flush it if it doesn't exist! */
1152    }
1153    /* Invalidate the parent's attr cache, the modification time has changed */
1154    VTOC(dvp)->c_flags &= ~C_VATTR;
1155
1156    /* Check for remove of control object. */
1157    if (IS_CTL_NAME(dvp, nm, len)) {
1158	MARK_INT_FAIL(CODA_REMOVE_STATS);
1159	return(ENOENT);
1160    }
1161
1162    error = venus_remove(vtomi(dvp), &cp->c_fid, nm, len, cred, td->td_proc);
1163
1164    CODADEBUG(CODA_REMOVE, myprintf(("in remove result %d\n",error)); )
1165
1166    return(error);
1167}
1168
1169int
1170coda_link(struct vop_link_args *ap)
1171{
1172/* true args */
1173    struct vnode *vp = ap->a_vp;
1174    struct cnode *cp = VTOC(vp);
1175    struct vnode *tdvp = ap->a_tdvp;
1176    struct cnode *tdcp = VTOC(tdvp);
1177    struct componentname *cnp = ap->a_cnp;
1178    struct ucred *cred = cnp->cn_cred;
1179    struct thread *td = cnp->cn_thread;
1180/* locals */
1181    int error;
1182    const char *nm = cnp->cn_nameptr;
1183    int len = cnp->cn_namelen;
1184
1185    MARK_ENTRY(CODA_LINK_STATS);
1186
1187    if (codadebug & CODADBGMSK(CODA_LINK)) {
1188	myprintf(("nb_link:   vp fid: %s\n",
1189		  coda_f2s(&cp->c_fid)));
1190	myprintf(("nb_link: tdvp fid: %s)\n",
1191		  coda_f2s(&tdcp->c_fid)));
1192    }
1193    if (codadebug & CODADBGMSK(CODA_LINK)) {
1194	myprintf(("link:   vp fid: %s\n",
1195		  coda_f2s(&cp->c_fid)));
1196	myprintf(("link: tdvp fid: %s\n",
1197		  coda_f2s(&tdcp->c_fid)));
1198    }
1199
1200    /* Check for link to/from control object. */
1201    if (IS_CTL_NAME(tdvp, nm, len) || IS_CTL_VP(vp)) {
1202	MARK_INT_FAIL(CODA_LINK_STATS);
1203	return(EACCES);
1204    }
1205
1206    error = venus_link(vtomi(vp), &cp->c_fid, &tdcp->c_fid, nm, len, cred, td->td_proc);
1207
1208    /* Invalidate the parent's attr cache, the modification time has changed */
1209    VTOC(tdvp)->c_flags &= ~C_VATTR;
1210    VTOC(vp)->c_flags &= ~C_VATTR;
1211
1212    CODADEBUG(CODA_LINK,	myprintf(("in link result %d\n",error)); )
1213
1214    return(error);
1215}
1216
1217int
1218coda_rename(struct vop_rename_args *ap)
1219{
1220/* true args */
1221    struct vnode *odvp = ap->a_fdvp;
1222    struct cnode *odcp = VTOC(odvp);
1223    struct componentname  *fcnp = ap->a_fcnp;
1224    struct vnode *ndvp = ap->a_tdvp;
1225    struct cnode *ndcp = VTOC(ndvp);
1226    struct componentname  *tcnp = ap->a_tcnp;
1227    struct ucred *cred = fcnp->cn_cred;
1228    struct thread *td = fcnp->cn_thread;
1229/* true args */
1230    int error;
1231    const char *fnm = fcnp->cn_nameptr;
1232    int flen = fcnp->cn_namelen;
1233    const char *tnm = tcnp->cn_nameptr;
1234    int tlen = tcnp->cn_namelen;
1235
1236    MARK_ENTRY(CODA_RENAME_STATS);
1237
1238    /* Hmmm.  The vnodes are already looked up.  Perhaps they are locked?
1239       This could be Bad. XXX */
1240#ifdef OLD_DIAGNOSTIC
1241    if ((fcnp->cn_cred != tcnp->cn_cred)
1242	|| (fcnp->cn_thread != tcnp->cn_thread))
1243    {
1244	panic("coda_rename: component names don't agree");
1245    }
1246#endif
1247
1248    /* Check for rename involving control object. */
1249    if (IS_CTL_NAME(odvp, fnm, flen) || IS_CTL_NAME(ndvp, tnm, tlen)) {
1250	MARK_INT_FAIL(CODA_RENAME_STATS);
1251	return(EACCES);
1252    }
1253
1254    /* Problem with moving directories -- need to flush entry for .. */
1255    if (odvp != ndvp) {
1256	struct cnode *ovcp = coda_nc_lookup(VTOC(odvp), fnm, flen, cred);
1257	if (ovcp) {
1258	    struct vnode *ovp = CTOV(ovcp);
1259	    if ((ovp) &&
1260		(ovp->v_type == VDIR)) /* If it's a directory */
1261		coda_nc_zapfile(VTOC(ovp),"..", 2);
1262	}
1263    }
1264
1265    /* Remove the entries for both source and target files */
1266    coda_nc_zapfile(VTOC(odvp), fnm, flen);
1267    coda_nc_zapfile(VTOC(ndvp), tnm, tlen);
1268
1269    /* Invalidate the parent's attr cache, the modification time has changed */
1270    VTOC(odvp)->c_flags &= ~C_VATTR;
1271    VTOC(ndvp)->c_flags &= ~C_VATTR;
1272
1273    if (flen+1 > CODA_MAXNAMLEN) {
1274	MARK_INT_FAIL(CODA_RENAME_STATS);
1275	error = EINVAL;
1276	goto exit;
1277    }
1278
1279    if (tlen+1 > CODA_MAXNAMLEN) {
1280	MARK_INT_FAIL(CODA_RENAME_STATS);
1281	error = EINVAL;
1282	goto exit;
1283    }
1284
1285    error = venus_rename(vtomi(odvp), &odcp->c_fid, &ndcp->c_fid, fnm, flen, tnm, tlen, cred, td->td_proc);
1286
1287 exit:
1288    CODADEBUG(CODA_RENAME, myprintf(("in rename result %d\n",error));)
1289    /* XXX - do we need to call cache pureg on the moved vnode? */
1290    cache_purge(ap->a_fvp);
1291
1292    /* It seems to be incumbent on us to drop locks on all four vnodes */
1293    /* From-vnodes are not locked, only ref'd.  To-vnodes are locked. */
1294
1295    vrele(ap->a_fvp);
1296    vrele(odvp);
1297
1298    if (ap->a_tvp) {
1299	if (ap->a_tvp == ndvp) {
1300	    vrele(ap->a_tvp);
1301	} else {
1302	    vput(ap->a_tvp);
1303	}
1304    }
1305
1306    vput(ndvp);
1307    return(error);
1308}
1309
1310int
1311coda_mkdir(struct vop_mkdir_args *ap)
1312{
1313/* true args */
1314    struct vnode *dvp = ap->a_dvp;
1315    struct cnode *dcp = VTOC(dvp);
1316    struct componentname  *cnp = ap->a_cnp;
1317    register struct vattr *va = ap->a_vap;
1318    struct vnode **vpp = ap->a_vpp;
1319    struct ucred *cred = cnp->cn_cred;
1320    struct thread *td = cnp->cn_thread;
1321/* locals */
1322    int error;
1323    const char *nm = cnp->cn_nameptr;
1324    int len = cnp->cn_namelen;
1325    struct cnode *cp;
1326    CodaFid VFid;
1327    struct vattr ova;
1328
1329    MARK_ENTRY(CODA_MKDIR_STATS);
1330
1331    /* Check for mkdir of target object. */
1332    if (IS_CTL_NAME(dvp, nm, len)) {
1333	*vpp = (struct vnode *)0;
1334	MARK_INT_FAIL(CODA_MKDIR_STATS);
1335	return(EACCES);
1336    }
1337
1338    if (len+1 > CODA_MAXNAMLEN) {
1339	*vpp = (struct vnode *)0;
1340	MARK_INT_FAIL(CODA_MKDIR_STATS);
1341	return(EACCES);
1342    }
1343
1344    error = venus_mkdir(vtomi(dvp), &dcp->c_fid, nm, len, va, cred, td->td_proc, &VFid, &ova);
1345
1346    if (!error) {
1347	if (coda_find(&VFid) != NULL)
1348	    panic("cnode existed for newly created directory!");
1349
1350
1351	cp =  make_coda_node(&VFid, dvp->v_mount, va->va_type);
1352	*vpp = CTOV(cp);
1353
1354	/* enter the new vnode in the Name Cache */
1355	coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
1356
1357	/* as a side effect, enter "." and ".." for the directory */
1358	coda_nc_enter(VTOC(*vpp), ".", 1, cred, VTOC(*vpp));
1359	coda_nc_enter(VTOC(*vpp), "..", 2, cred, VTOC(dvp));
1360
1361	if (coda_attr_cache) {
1362	    VTOC(*vpp)->c_vattr = ova;		/* update the attr cache */
1363	    VTOC(*vpp)->c_flags |= C_VATTR;	/* Valid attributes in cnode */
1364	}
1365
1366	/* Invalidate the parent's attr cache, the modification time has changed */
1367	VTOC(dvp)->c_flags &= ~C_VATTR;
1368
1369	CODADEBUG( CODA_MKDIR, myprintf(("mkdir: %s result %d\n",
1370					 coda_f2s(&VFid), error)); )
1371	} else {
1372	*vpp = (struct vnode *)0;
1373	CODADEBUG(CODA_MKDIR, myprintf(("mkdir error %d\n",error));)
1374    }
1375
1376    return(error);
1377}
1378
1379int
1380coda_rmdir(struct vop_rmdir_args *ap)
1381{
1382/* true args */
1383    struct vnode *dvp = ap->a_dvp;
1384    struct cnode *dcp = VTOC(dvp);
1385    struct componentname  *cnp = ap->a_cnp;
1386    struct ucred *cred = cnp->cn_cred;
1387    struct thread *td = cnp->cn_thread;
1388/* true args */
1389    int error;
1390    const char *nm = cnp->cn_nameptr;
1391    int len = cnp->cn_namelen;
1392    struct cnode *cp;
1393
1394    MARK_ENTRY(CODA_RMDIR_STATS);
1395
1396    /* Check for rmdir of control object. */
1397    if (IS_CTL_NAME(dvp, nm, len)) {
1398	MARK_INT_FAIL(CODA_RMDIR_STATS);
1399	return(ENOENT);
1400    }
1401
1402    /* We're being conservative here, it might be that this person
1403     * doesn't really have sufficient access to delete the file
1404     * but we feel zapping the entry won't really hurt anyone -- dcs
1405     */
1406    /*
1407     * As a side effect of the rmdir, remove any entries for children of
1408     * the directory, especially "." and "..".
1409     */
1410    cp = coda_nc_lookup(dcp, nm, len, cred);
1411    if (cp) coda_nc_zapParentfid(&(cp->c_fid), NOT_DOWNCALL);
1412
1413    /* Remove the file's entry from the CODA Name Cache */
1414    coda_nc_zapfile(dcp, nm, len);
1415
1416    /* Invalidate the parent's attr cache, the modification time has changed */
1417    dcp->c_flags &= ~C_VATTR;
1418
1419    error = venus_rmdir(vtomi(dvp), &dcp->c_fid, nm, len, cred, td->td_proc);
1420
1421    CODADEBUG(CODA_RMDIR, myprintf(("in rmdir result %d\n", error)); )
1422
1423    return(error);
1424}
1425
1426int
1427coda_symlink(struct vop_symlink_args *ap)
1428{
1429/* true args */
1430    struct vnode *tdvp = ap->a_dvp;
1431    struct cnode *tdcp = VTOC(tdvp);
1432    struct componentname *cnp = ap->a_cnp;
1433    struct vattr *tva = ap->a_vap;
1434    char *path = ap->a_target;
1435    struct ucred *cred = cnp->cn_cred;
1436    struct thread *td = cnp->cn_thread;
1437    struct vnode **vpp = ap->a_vpp;
1438/* locals */
1439    int error;
1440    /*
1441     * XXX I'm assuming the following things about coda_symlink's
1442     * arguments:
1443     *       t(foo) is the new name/parent/etc being created.
1444     *       lname is the contents of the new symlink.
1445     */
1446    char *nm = cnp->cn_nameptr;
1447    int len = cnp->cn_namelen;
1448    int plen = strlen(path);
1449
1450    /*
1451     * Here's the strategy for the moment: perform the symlink, then
1452     * do a lookup to grab the resulting vnode.  I know this requires
1453     * two communications with Venus for a new sybolic link, but
1454     * that's the way the ball bounces.  I don't yet want to change
1455     * the way the Mach symlink works.  When Mach support is
1456     * deprecated, we should change symlink so that the common case
1457     * returns the resultant vnode in a vpp argument.
1458     */
1459
1460    MARK_ENTRY(CODA_SYMLINK_STATS);
1461
1462    /* Check for symlink of control object. */
1463    if (IS_CTL_NAME(tdvp, nm, len)) {
1464	MARK_INT_FAIL(CODA_SYMLINK_STATS);
1465	return(EACCES);
1466    }
1467
1468    if (plen+1 > CODA_MAXPATHLEN) {
1469	MARK_INT_FAIL(CODA_SYMLINK_STATS);
1470	return(EINVAL);
1471    }
1472
1473    if (len+1 > CODA_MAXNAMLEN) {
1474	MARK_INT_FAIL(CODA_SYMLINK_STATS);
1475	error = EINVAL;
1476	goto exit;
1477    }
1478
1479    error = venus_symlink(vtomi(tdvp), &tdcp->c_fid, path, plen, nm, len, tva, cred, td->td_proc);
1480
1481    /* Invalidate the parent's attr cache, the modification time has changed */
1482    tdcp->c_flags &= ~C_VATTR;
1483
1484    if (error == 0)
1485	error = VOP_LOOKUP(tdvp, vpp, cnp);
1486
1487 exit:
1488    CODADEBUG(CODA_SYMLINK, myprintf(("in symlink result %d\n",error)); )
1489    return(error);
1490}
1491
1492/*
1493 * Read directory entries.
1494 */
1495int
1496coda_readdir(struct vop_readdir_args *ap)
1497{
1498/* true args */
1499    struct vnode *vp = ap->a_vp;
1500    struct cnode *cp = VTOC(vp);
1501    register struct uio *uiop = ap->a_uio;
1502    struct ucred *cred = ap->a_cred;
1503    int *eofflag = ap->a_eofflag;
1504    u_long **cookies = ap->a_cookies;
1505    int *ncookies = ap->a_ncookies;
1506    struct thread *td = ap->a_uio->uio_td;
1507/* upcall decl */
1508/* locals */
1509    int error = 0;
1510
1511    MARK_ENTRY(CODA_READDIR_STATS);
1512
1513    CODADEBUG(CODA_READDIR, myprintf(("coda_readdir(%p, %d, %lld, %d)\n",
1514				      (void *)uiop->uio_iov->iov_base,
1515				      uiop->uio_resid,
1516				      (long long)uiop->uio_offset,
1517				      uiop->uio_segflg)); )
1518
1519    /* Check for readdir of control object. */
1520    if (IS_CTL_VP(vp)) {
1521	MARK_INT_FAIL(CODA_READDIR_STATS);
1522	return(ENOENT);
1523    }
1524
1525    {
1526	/* If directory is not already open do an "internal open" on it. */
1527	int opened_internally = 0;
1528	if (cp->c_ovp == NULL) {
1529	    opened_internally = 1;
1530	    MARK_INT_GEN(CODA_OPEN_STATS);
1531	    error = VOP_OPEN(vp, FREAD, cred, td, -1);
1532printf("coda_readdir: Internally Opening %p\n", vp);
1533	    if (error) {
1534		printf("coda_readdir: VOP_OPEN on container failed %d\n", error);
1535		return (error);
1536	    }
1537	}
1538
1539	/* Have UFS handle the call. */
1540	CODADEBUG(CODA_READDIR, myprintf(("indirect readdir: fid = %s, refcnt = %d\n", coda_f2s(&cp->c_fid), vp->v_usecount)); )
1541	error = VOP_READDIR(cp->c_ovp, uiop, cred, eofflag, ncookies,
1542			       cookies);
1543
1544	if (error)
1545	    MARK_INT_FAIL(CODA_READDIR_STATS);
1546	else
1547	    MARK_INT_SAT(CODA_READDIR_STATS);
1548
1549	/* Do an "internal close" if necessary. */
1550	if (opened_internally) {
1551	    MARK_INT_GEN(CODA_CLOSE_STATS);
1552	    (void)VOP_CLOSE(vp, FREAD, cred, td);
1553	}
1554    }
1555
1556    return(error);
1557}
1558
1559/*
1560 * Convert from filesystem blocks to device blocks
1561 */
1562int
1563coda_bmap(struct vop_bmap_args *ap)
1564{
1565    /* XXX on the global proc */
1566/* true args */
1567    struct vnode *vp __attribute__((unused)) = ap->a_vp;	/* file's vnode */
1568    daddr_t bn __attribute__((unused)) = ap->a_bn;	/* fs block number */
1569    struct bufobj **bop = ap->a_bop;			/* RETURN bufobj of device */
1570    daddr_t *bnp __attribute__((unused)) = ap->a_bnp;	/* RETURN device block number */
1571    struct thread *td __attribute__((unused)) = curthread;
1572/* upcall decl */
1573/* locals */
1574
1575	int ret = 0;
1576	struct cnode *cp;
1577
1578	cp = VTOC(vp);
1579	if (cp->c_ovp) {
1580		return EINVAL;
1581		ret =  VOP_BMAP(cp->c_ovp, bn, bop, bnp, ap->a_runp, ap->a_runb);
1582#if	0
1583		printf("VOP_BMAP(cp->c_ovp %p, bn %p, bop %p, bnp %lld, ap->a_runp %p, ap->a_runb %p) = %d\n",
1584			cp->c_ovp, bn, bop, bnp, ap->a_runp, ap->a_runb, ret);
1585#endif
1586		return ret;
1587	} else {
1588#if	0
1589		printf("coda_bmap: no container\n");
1590#endif
1591		return(EOPNOTSUPP);
1592	}
1593}
1594
1595int
1596coda_reclaim(struct vop_reclaim_args *ap)
1597{
1598/* true args */
1599    struct vnode *vp = ap->a_vp;
1600    struct cnode *cp = VTOC(vp);
1601/* upcall decl */
1602/* locals */
1603
1604/*
1605 * Forced unmount/flush will let vnodes with non zero use be destroyed!
1606 */
1607    ENTRY;
1608
1609    if (IS_UNMOUNTING(cp)) {
1610#ifdef	DEBUG
1611	if (VTOC(vp)->c_ovp) {
1612	    if (IS_UNMOUNTING(cp))
1613		printf("coda_reclaim: c_ovp not void: vp %p, cp %p\n", vp, cp);
1614	}
1615#endif
1616    } else {
1617#ifdef OLD_DIAGNOSTIC
1618	if (vrefcnt(vp) != 0)
1619	    print("coda_reclaim: pushing active %p\n", vp);
1620	if (VTOC(vp)->c_ovp) {
1621	    panic("coda_reclaim: c_ovp not void");
1622    }
1623#endif
1624    }
1625    cache_purge(vp);
1626    coda_free(VTOC(vp));
1627    vp->v_data = NULL;
1628    vnode_destroy_vobject(vp);
1629    return (0);
1630}
1631
1632int
1633coda_lock(struct vop_lock_args *ap)
1634{
1635/* true args */
1636    struct vnode *vp = ap->a_vp;
1637    struct cnode *cp = VTOC(vp);
1638/* upcall decl */
1639/* locals */
1640
1641    ENTRY;
1642
1643    if ((ap->a_flags & LK_INTERLOCK) == 0) {
1644	VI_LOCK(vp);
1645	ap->a_flags |= LK_INTERLOCK;
1646    }
1647
1648    if (coda_lockdebug) {
1649	myprintf(("Attempting lock on %s\n",
1650		  coda_f2s(&cp->c_fid)));
1651    }
1652
1653    return (vop_stdlock(ap));
1654}
1655
1656int
1657coda_unlock(struct vop_unlock_args *ap)
1658{
1659/* true args */
1660    struct vnode *vp = ap->a_vp;
1661    struct cnode *cp = VTOC(vp);
1662/* upcall decl */
1663/* locals */
1664
1665    ENTRY;
1666    if (coda_lockdebug) {
1667	myprintf(("Attempting unlock on %s\n",
1668		  coda_f2s(&cp->c_fid)));
1669    }
1670
1671    return (vop_stdunlock(ap));
1672}
1673
1674int
1675coda_islocked(struct vop_islocked_args *ap)
1676{
1677/* true args */
1678    ENTRY;
1679
1680    return (vop_stdislocked(ap));
1681}
1682
1683/* How one looks up a vnode given a device/inode pair: */
1684int
1685coda_grab_vnode(struct cdev *dev, ino_t ino, struct vnode **vpp)
1686{
1687    /* This is like VFS_VGET() or igetinode()! */
1688    int           error;
1689    struct mount *mp;
1690
1691    if (!(mp = devtomp(dev))) {
1692	myprintf(("coda_grab_vnode: devtomp(%#lx) returns NULL\n",
1693		  (u_long)dev2udev(dev)));
1694	return(ENXIO);
1695    }
1696
1697    /* XXX - ensure that nonzero-return means failure */
1698    error = VFS_VGET(mp,ino,LK_EXCLUSIVE,vpp);
1699    if (error) {
1700	myprintf(("coda_grab_vnode: iget/vget(%lx, %lu) returns %p, err %d\n",
1701		  (u_long)dev2udev(dev), (u_long)ino, (void *)*vpp, error));
1702	return(ENOENT);
1703    }
1704    return(0);
1705}
1706
1707void
1708print_vattr(struct vattr *attr)
1709{
1710    char *typestr;
1711
1712    switch (attr->va_type) {
1713    case VNON:
1714	typestr = "VNON";
1715	break;
1716    case VREG:
1717	typestr = "VREG";
1718	break;
1719    case VDIR:
1720	typestr = "VDIR";
1721	break;
1722    case VBLK:
1723	typestr = "VBLK";
1724	break;
1725    case VCHR:
1726	typestr = "VCHR";
1727	break;
1728    case VLNK:
1729	typestr = "VLNK";
1730	break;
1731    case VSOCK:
1732	typestr = "VSCK";
1733	break;
1734    case VFIFO:
1735	typestr = "VFFO";
1736	break;
1737    case VBAD:
1738	typestr = "VBAD";
1739	break;
1740    default:
1741	typestr = "????";
1742	break;
1743    }
1744
1745
1746    myprintf(("attr: type %s mode %d uid %d gid %d fsid %d rdev %d\n",
1747	      typestr, (int)attr->va_mode, (int)attr->va_uid,
1748	      (int)attr->va_gid, (int)attr->va_fsid, (int)attr->va_rdev));
1749
1750    myprintf(("      fileid %d nlink %d size %d blocksize %d bytes %d\n",
1751	      (int)attr->va_fileid, (int)attr->va_nlink,
1752	      (int)attr->va_size,
1753	      (int)attr->va_blocksize,(int)attr->va_bytes));
1754    myprintf(("      gen %ld flags %ld vaflags %d\n",
1755	      attr->va_gen, attr->va_flags, attr->va_vaflags));
1756    myprintf(("      atime sec %d nsec %d\n",
1757	      (int)attr->va_atime.tv_sec, (int)attr->va_atime.tv_nsec));
1758    myprintf(("      mtime sec %d nsec %d\n",
1759	      (int)attr->va_mtime.tv_sec, (int)attr->va_mtime.tv_nsec));
1760    myprintf(("      ctime sec %d nsec %d\n",
1761	      (int)attr->va_ctime.tv_sec, (int)attr->va_ctime.tv_nsec));
1762}
1763
1764/* How to print a ucred */
1765void
1766print_cred(struct ucred *cred)
1767{
1768
1769	int i;
1770
1771	myprintf(("ref %d\tuid %d\n",cred->cr_ref,cred->cr_uid));
1772
1773	for (i=0; i < cred->cr_ngroups; i++)
1774		myprintf(("\tgroup %d: (%d)\n",i,cred->cr_groups[i]));
1775	myprintf(("\n"));
1776
1777}
1778
1779/*
1780 * Return a vnode for the given fid.
1781 * If no cnode exists for this fid create one and put it
1782 * in a table hashed by coda_f2i().  If the cnode for
1783 * this fid is already in the table return it (ref count is
1784 * incremented by coda_find.  The cnode will be flushed from the
1785 * table when coda_inactive calls coda_unsave.
1786 */
1787struct cnode *
1788make_coda_node(CodaFid *fid, struct mount *vfsp, short type)
1789{
1790    struct cnode *cp;
1791    int          err;
1792
1793    if ((cp = coda_find(fid)) == NULL) {
1794	struct vnode *vp;
1795
1796	cp = coda_alloc();
1797	cp->c_fid = *fid;
1798
1799	err = getnewvnode("coda", vfsp, &coda_vnodeops, &vp);
1800	if (err) {
1801	    panic("coda: getnewvnode returned error %d\n", err);
1802	}
1803	vp->v_data = cp;
1804	vp->v_type = type;
1805	cp->c_vnode = vp;
1806	coda_save(cp);
1807
1808    } else {
1809	vref(CTOV(cp));
1810    }
1811
1812    return cp;
1813}
1814
1815int
1816coda_pathconf(struct vop_pathconf_args *ap)
1817{
1818	int error;
1819	register_t *retval;
1820
1821	retval = ap->a_retval;
1822	error = 0;
1823
1824	switch (ap->a_name) {
1825	case _PC_NAME_MAX:
1826		*retval = CODA_MAXNAMLEN;
1827		break;
1828	case _PC_PATH_MAX:
1829		*retval = CODA_MAXPATHLEN;
1830		break;
1831	default:
1832		error = vop_stdpathconf(ap);
1833		break;
1834	}
1835
1836	return (error);
1837}
1838