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