xref: /netbsd-src/sys/coda/coda_vnops.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: coda_vnops.c,v 1.91 2013/10/17 20:55:30 christos Exp $	*/
2 
3 /*
4  *
5  *             Coda: an Experimental Distributed File System
6  *                              Release 3.1
7  *
8  *           Copyright (c) 1987-1998 Carnegie Mellon University
9  *                          All Rights Reserved
10  *
11  * Permission  to  use, copy, modify and distribute this software and its
12  * documentation is hereby granted,  provided  that  both  the  copyright
13  * notice  and  this  permission  notice  appear  in  all  copies  of the
14  * software, derivative works or  modified  versions,  and  any  portions
15  * thereof, and that both notices appear in supporting documentation, and
16  * that credit is given to Carnegie Mellon University  in  all  documents
17  * and publicity pertaining to direct or indirect use of this code or its
18  * derivatives.
19  *
20  * CODA IS AN EXPERIMENTAL SOFTWARE SYSTEM AND IS  KNOWN  TO  HAVE  BUGS,
21  * SOME  OF  WHICH MAY HAVE SERIOUS CONSEQUENCES.  CARNEGIE MELLON ALLOWS
22  * FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.   CARNEGIE  MELLON
23  * DISCLAIMS  ANY  LIABILITY  OF  ANY  KIND  FOR  ANY  DAMAGES WHATSOEVER
24  * RESULTING DIRECTLY OR INDIRECTLY FROM THE USE OF THIS SOFTWARE  OR  OF
25  * ANY DERIVATIVE WORK.
26  *
27  * Carnegie  Mellon  encourages  users  of  this  software  to return any
28  * improvements or extensions that  they  make,  and  to  grant  Carnegie
29  * Mellon the rights to redistribute these changes without encumbrance.
30  *
31  * 	@(#) coda/coda_vnops.c,v 1.1.1.1 1998/08/29 21:26:46 rvb Exp $
32  */
33 
34 /*
35  * Mach Operating System
36  * Copyright (c) 1990 Carnegie-Mellon University
37  * Copyright (c) 1989 Carnegie-Mellon University
38  * All rights reserved.  The CMU software License Agreement specifies
39  * the terms and conditions for use and redistribution.
40  */
41 
42 /*
43  * This code was written for the Coda file system at Carnegie Mellon
44  * University.  Contributers include David Steere, James Kistler, and
45  * M. Satyanarayanan.
46  */
47 
48 #include <sys/cdefs.h>
49 __KERNEL_RCSID(0, "$NetBSD: coda_vnops.c,v 1.91 2013/10/17 20:55:30 christos Exp $");
50 
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/malloc.h>
54 #include <sys/errno.h>
55 #include <sys/acct.h>
56 #include <sys/file.h>
57 #include <sys/uio.h>
58 #include <sys/namei.h>
59 #include <sys/ioctl.h>
60 #include <sys/mount.h>
61 #include <sys/proc.h>
62 #include <sys/select.h>
63 #include <sys/vnode.h>
64 #include <sys/kauth.h>
65 
66 #include <miscfs/genfs/genfs.h>
67 
68 #include <coda/coda.h>
69 #include <coda/cnode.h>
70 #include <coda/coda_vnops.h>
71 #include <coda/coda_venus.h>
72 #include <coda/coda_opstats.h>
73 #include <coda/coda_subr.h>
74 #include <coda/coda_namecache.h>
75 #include <coda/coda_pioctl.h>
76 
77 /*
78  * These flags select various performance enhancements.
79  */
80 int coda_attr_cache  = 1;       /* Set to cache attributes in the kernel */
81 int coda_symlink_cache = 1;     /* Set to cache symbolic link information */
82 int coda_access_cache = 1;      /* Set to handle some access checks directly */
83 
84 /* structure to keep track of vfs calls */
85 
86 struct coda_op_stats coda_vnodeopstats[CODA_VNODEOPS_SIZE];
87 
88 #define MARK_ENTRY(op) (coda_vnodeopstats[op].entries++)
89 #define MARK_INT_SAT(op) (coda_vnodeopstats[op].sat_intrn++)
90 #define MARK_INT_FAIL(op) (coda_vnodeopstats[op].unsat_intrn++)
91 #define MARK_INT_GEN(op) (coda_vnodeopstats[op].gen_intrn++)
92 
93 /* What we are delaying for in printf */
94 static int coda_lockdebug = 0;
95 
96 #define ENTRY if(coda_vnop_print_entry) myprintf(("Entered %s\n",__func__))
97 
98 /* Definition of the vnode operation vector */
99 
100 const struct vnodeopv_entry_desc coda_vnodeop_entries[] = {
101     { &vop_default_desc, coda_vop_error },
102     { &vop_lookup_desc, coda_lookup },		/* lookup */
103     { &vop_create_desc, coda_create },		/* create */
104     { &vop_mknod_desc, coda_vop_error },	/* mknod */
105     { &vop_open_desc, coda_open },		/* open */
106     { &vop_close_desc, coda_close },		/* close */
107     { &vop_access_desc, coda_access },		/* access */
108     { &vop_getattr_desc, coda_getattr },	/* getattr */
109     { &vop_setattr_desc, coda_setattr },	/* setattr */
110     { &vop_read_desc, coda_read },		/* read */
111     { &vop_write_desc, coda_write },		/* write */
112     { &vop_fcntl_desc, genfs_fcntl },		/* fcntl */
113     { &vop_ioctl_desc, coda_ioctl },		/* ioctl */
114     { &vop_mmap_desc, genfs_mmap },		/* mmap */
115     { &vop_fsync_desc, coda_fsync },		/* fsync */
116     { &vop_remove_desc, coda_remove },		/* remove */
117     { &vop_link_desc, coda_link },		/* link */
118     { &vop_rename_desc, coda_rename },		/* rename */
119     { &vop_mkdir_desc, coda_mkdir },		/* mkdir */
120     { &vop_rmdir_desc, coda_rmdir },		/* rmdir */
121     { &vop_symlink_desc, coda_symlink },	/* symlink */
122     { &vop_readdir_desc, coda_readdir },	/* readdir */
123     { &vop_readlink_desc, coda_readlink },	/* readlink */
124     { &vop_abortop_desc, coda_abortop },	/* abortop */
125     { &vop_inactive_desc, coda_inactive },	/* inactive */
126     { &vop_reclaim_desc, coda_reclaim },	/* reclaim */
127     { &vop_lock_desc, coda_lock },		/* lock */
128     { &vop_unlock_desc, coda_unlock },		/* unlock */
129     { &vop_bmap_desc, coda_bmap },		/* bmap */
130     { &vop_strategy_desc, coda_strategy },	/* strategy */
131     { &vop_print_desc, coda_vop_error },	/* print */
132     { &vop_islocked_desc, coda_islocked },	/* islocked */
133     { &vop_pathconf_desc, coda_vop_error },	/* pathconf */
134     { &vop_advlock_desc, coda_vop_nop },	/* advlock */
135     { &vop_bwrite_desc, coda_vop_error },	/* bwrite */
136     { &vop_seek_desc, genfs_seek },		/* seek */
137     { &vop_poll_desc, genfs_poll },		/* poll */
138     { &vop_getpages_desc, coda_getpages },	/* getpages */
139     { &vop_putpages_desc, coda_putpages },	/* putpages */
140     { NULL, NULL }
141 };
142 
143 static void coda_print_vattr(struct vattr *);
144 
145 int (**coda_vnodeop_p)(void *);
146 const struct vnodeopv_desc coda_vnodeop_opv_desc =
147         { &coda_vnodeop_p, coda_vnodeop_entries };
148 
149 /* Definitions of NetBSD vnodeop interfaces */
150 
151 /*
152  * A generic error routine.  Return EIO without looking at arguments.
153  */
154 int
155 coda_vop_error(void *anon) {
156     struct vnodeop_desc **desc = (struct vnodeop_desc **)anon;
157 
158     if (codadebug) {
159 	myprintf(("%s: Vnode operation %s called (error).\n",
160 	    __func__, (*desc)->vdesc_name));
161     }
162 
163     return EIO;
164 }
165 
166 /* A generic do-nothing. */
167 int
168 coda_vop_nop(void *anon) {
169     struct vnodeop_desc **desc = (struct vnodeop_desc **)anon;
170 
171     if (codadebug) {
172 	myprintf(("Vnode operation %s called, but unsupported\n",
173 		  (*desc)->vdesc_name));
174     }
175    return (0);
176 }
177 
178 int
179 coda_vnodeopstats_init(void)
180 {
181 	int i;
182 
183 	for(i=0;i<CODA_VNODEOPS_SIZE;i++) {
184 		coda_vnodeopstats[i].opcode = i;
185 		coda_vnodeopstats[i].entries = 0;
186 		coda_vnodeopstats[i].sat_intrn = 0;
187 		coda_vnodeopstats[i].unsat_intrn = 0;
188 		coda_vnodeopstats[i].gen_intrn = 0;
189 	}
190 
191 	return 0;
192 }
193 
194 /*
195  * XXX The entire relationship between VOP_OPEN and having a container
196  * file (via venus_open) needs to be reexamined.  In particular, it's
197  * valid to open/mmap/close and then reference.  Instead of doing
198  * VOP_OPEN when getpages needs a container, we should do the
199  * venus_open part, and record that the vnode has opened the container
200  * for getpages, and do the matching logical close on coda_inactive.
201  * Further, coda_rdwr needs a container file, and sometimes needs to
202  * do the equivalent of open (core dumps).
203  */
204 /*
205  * coda_open calls Venus to return the device and inode of the
206  * container file, and then obtains a vnode for that file.  The
207  * container vnode is stored in the coda vnode, and a reference is
208  * added for each open file.
209  */
210 int
211 coda_open(void *v)
212 {
213     /*
214      * NetBSD can pass the O_EXCL flag in mode, even though the check
215      * has already happened.  Venus defensively assumes that if open
216      * is passed the EXCL, it must be a bug.  We strip the flag here.
217      */
218 /* true args */
219     struct vop_open_args *ap = v;
220     vnode_t *vp = ap->a_vp;
221     struct cnode *cp = VTOC(vp);
222     int flag = ap->a_mode & (~O_EXCL);
223     kauth_cred_t cred = ap->a_cred;
224 /* locals */
225     int error;
226     dev_t dev;			/* container file device, inode, vnode */
227     ino_t inode;
228     vnode_t *container_vp;
229 
230     MARK_ENTRY(CODA_OPEN_STATS);
231 
232     if (!VOP_ISLOCKED(vp))
233 	VOP_LOCK(vp, LK_EXCLUSIVE);
234     /* Check for open of control file. */
235     if (IS_CTL_VP(vp)) {
236 	/* if (WRITABLE(flag)) */
237 	if (flag & (FWRITE | O_TRUNC | O_CREAT | O_EXCL)) {
238 	    MARK_INT_FAIL(CODA_OPEN_STATS);
239 	    return(EACCES);
240 	}
241 	MARK_INT_SAT(CODA_OPEN_STATS);
242 	return(0);
243     }
244 
245     error = venus_open(vtomi(vp), &cp->c_fid, flag, cred, curlwp, &dev, &inode);
246     if (error)
247 	return (error);
248     if (!error) {
249 	    CODADEBUG(CODA_OPEN, myprintf((
250 		"%s: dev 0x%llx inode %llu result %d\n", __func__,
251 		(unsigned long long)dev, (unsigned long long)inode, error));)
252     }
253 
254     /*
255      * Obtain locked and referenced container vnode from container
256      * device/inode.
257      */
258     error = coda_grab_vnode(vp, dev, inode, &container_vp);
259     if (error)
260 	return (error);
261 
262     /* Save the vnode pointer for the container file. */
263     if (cp->c_ovp == NULL) {
264 	cp->c_ovp = container_vp;
265     } else {
266 	if (cp->c_ovp != container_vp)
267 	    /*
268 	     * Perhaps venus returned a different container, or
269 	     * something else went wrong.
270 	     */
271 	    panic("%s: cp->c_ovp != container_vp", __func__);
272     }
273     cp->c_ocount++;
274 
275     /* Flush the attribute cache if writing the file. */
276     if (flag & FWRITE) {
277 	cp->c_owrite++;
278 	cp->c_flags &= ~C_VATTR;
279     }
280 
281     /*
282      * Save the <device, inode> pair for the container file to speed
283      * up subsequent reads while closed (mmap, program execution).
284      * This is perhaps safe because venus will invalidate the node
285      * before changing the container file mapping.
286      */
287     cp->c_device = dev;
288     cp->c_inode = inode;
289 
290     /* Open the container file. */
291     error = VOP_OPEN(container_vp, flag, cred);
292     /*
293      * Drop the lock on the container, after we have done VOP_OPEN
294      * (which requires a locked vnode).
295      */
296     VOP_UNLOCK(container_vp);
297     return(error);
298 }
299 
300 /*
301  * Close the cache file used for I/O and notify Venus.
302  */
303 int
304 coda_close(void *v)
305 {
306 /* true args */
307     struct vop_close_args *ap = v;
308     vnode_t *vp = ap->a_vp;
309     struct cnode *cp = VTOC(vp);
310     int flag = ap->a_fflag;
311     kauth_cred_t cred = ap->a_cred;
312 /* locals */
313     int error;
314 
315     MARK_ENTRY(CODA_CLOSE_STATS);
316 
317     /* Check for close of control file. */
318     if (IS_CTL_VP(vp)) {
319 	MARK_INT_SAT(CODA_CLOSE_STATS);
320 	return(0);
321     }
322 
323     /*
324      * XXX The IS_UNMOUNTING part of this is very suspect.
325      */
326     if (IS_UNMOUNTING(cp)) {
327 	if (cp->c_ovp) {
328 #ifdef	CODA_VERBOSE
329 	    printf("%s: destroying container %d, ufs vp %p of vp %p/cp %p\n",
330 		__func__, vp->v_usecount, cp->c_ovp, vp, cp);
331 #endif
332 #ifdef	hmm
333 	    vgone(cp->c_ovp);
334 #else
335 	    vn_lock(cp->c_ovp, LK_EXCLUSIVE | LK_RETRY);
336 	    VOP_CLOSE(cp->c_ovp, flag, cred); /* Do errors matter here? */
337 	    vput(cp->c_ovp);
338 #endif
339 	} else {
340 #ifdef	CODA_VERBOSE
341 	    printf("%s: NO container vp %p/cp %p\n", __func__, vp, cp);
342 #endif
343 	}
344 	return ENODEV;
345     }
346 
347     /* Lock the container node, and VOP_CLOSE it. */
348     vn_lock(cp->c_ovp, LK_EXCLUSIVE | LK_RETRY);
349     VOP_CLOSE(cp->c_ovp, flag, cred); /* Do errors matter here? */
350     /*
351      * Drop the lock we just obtained, and vrele the container vnode.
352      * Decrement reference counts, and clear container vnode pointer on
353      * last close.
354      */
355     vput(cp->c_ovp);
356     if (flag & FWRITE)
357 	--cp->c_owrite;
358     if (--cp->c_ocount == 0)
359 	cp->c_ovp = NULL;
360 
361     error = venus_close(vtomi(vp), &cp->c_fid, flag, cred, curlwp);
362 
363     CODADEBUG(CODA_CLOSE, myprintf(("%s: result %d\n", __func__, error)); )
364     return(error);
365 }
366 
367 int
368 coda_read(void *v)
369 {
370     struct vop_read_args *ap = v;
371 
372     ENTRY;
373     return(coda_rdwr(ap->a_vp, ap->a_uio, UIO_READ,
374 		    ap->a_ioflag, ap->a_cred, curlwp));
375 }
376 
377 int
378 coda_write(void *v)
379 {
380     struct vop_write_args *ap = v;
381 
382     ENTRY;
383     return(coda_rdwr(ap->a_vp, ap->a_uio, UIO_WRITE,
384 		    ap->a_ioflag, ap->a_cred, curlwp));
385 }
386 
387 int
388 coda_rdwr(vnode_t *vp, struct uio *uiop, enum uio_rw rw, int ioflag,
389 	kauth_cred_t cred, struct lwp *l)
390 {
391 /* upcall decl */
392   /* NOTE: container file operation!!! */
393 /* locals */
394     struct cnode *cp = VTOC(vp);
395     vnode_t *cfvp = cp->c_ovp;
396     struct proc *p = l->l_proc;
397     int opened_internally = 0;
398     int error = 0;
399 
400     MARK_ENTRY(CODA_RDWR_STATS);
401 
402     CODADEBUG(CODA_RDWR, myprintf(("coda_rdwr(%d, %p, %lu, %lld)\n", rw,
403 	uiop->uio_iov->iov_base, (unsigned long) uiop->uio_resid,
404 	(long long) uiop->uio_offset)); )
405 
406     /* Check for rdwr of control object. */
407     if (IS_CTL_VP(vp)) {
408 	MARK_INT_FAIL(CODA_RDWR_STATS);
409 	return(EINVAL);
410     }
411 
412     /* Redirect the request to UFS. */
413 
414     /*
415      * If file is not already open this must be a page
416      * {read,write} request.  Iget the cache file's inode
417      * pointer if we still have its <device, inode> pair.
418      * Otherwise, we must do an internal open to derive the
419      * pair.
420      * XXX Integrate this into a coherent strategy for container
421      * file acquisition.
422      */
423     if (cfvp == NULL) {
424 	/*
425 	 * If we're dumping core, do the internal open. Otherwise
426 	 * venus won't have the correct size of the core when
427 	 * it's completely written.
428 	 */
429 	if (cp->c_inode != 0 && !(p && (p->p_acflag & ACORE))) {
430 #ifdef CODA_VERBOSE
431 	    printf("%s: grabbing container vnode, losing reference\n",
432 		__func__);
433 #endif
434 	    /* Get locked and refed vnode. */
435 	    error = coda_grab_vnode(vp, cp->c_device, cp->c_inode, &cfvp);
436 	    if (error) {
437 		MARK_INT_FAIL(CODA_RDWR_STATS);
438 		return(error);
439 	    }
440 	    /*
441 	     * Drop lock.
442 	     * XXX Where is reference released.
443 	     */
444 	    VOP_UNLOCK(cfvp);
445 	}
446 	else {
447 #ifdef CODA_VERBOSE
448 	    printf("%s: internal VOP_OPEN\n", __func__);
449 #endif
450 	    opened_internally = 1;
451 	    MARK_INT_GEN(CODA_OPEN_STATS);
452 	    error = VOP_OPEN(vp, (rw == UIO_READ ? FREAD : FWRITE), cred);
453 #ifdef	CODA_VERBOSE
454 	    printf("%s: Internally Opening %p\n", __func__, vp);
455 #endif
456 	    if (error) {
457 		MARK_INT_FAIL(CODA_RDWR_STATS);
458 		return(error);
459 	    }
460 	    cfvp = cp->c_ovp;
461 	}
462     }
463 
464     /* Have UFS handle the call. */
465     CODADEBUG(CODA_RDWR, myprintf(("%s: fid = %s, refcnt = %d\n", __func__,
466 	coda_f2s(&cp->c_fid), CTOV(cp)->v_usecount)); )
467 
468     if (rw == UIO_READ) {
469 	error = VOP_READ(cfvp, uiop, ioflag, cred);
470     } else {
471 	error = VOP_WRITE(cfvp, uiop, ioflag, cred);
472     }
473 
474     if (error)
475 	MARK_INT_FAIL(CODA_RDWR_STATS);
476     else
477 	MARK_INT_SAT(CODA_RDWR_STATS);
478 
479     /* Do an internal close if necessary. */
480     if (opened_internally) {
481 	MARK_INT_GEN(CODA_CLOSE_STATS);
482 	(void)VOP_CLOSE(vp, (rw == UIO_READ ? FREAD : FWRITE), cred);
483     }
484 
485     /* Invalidate cached attributes if writing. */
486     if (rw == UIO_WRITE)
487 	cp->c_flags &= ~C_VATTR;
488     return(error);
489 }
490 
491 int
492 coda_ioctl(void *v)
493 {
494 /* true args */
495     struct vop_ioctl_args *ap = v;
496     vnode_t *vp = ap->a_vp;
497     int com = ap->a_command;
498     void *data = ap->a_data;
499     int flag = ap->a_fflag;
500     kauth_cred_t cred = ap->a_cred;
501 /* locals */
502     int error;
503     vnode_t *tvp;
504     struct PioctlData *iap = (struct PioctlData *)data;
505     namei_simple_flags_t sflags;
506 
507     MARK_ENTRY(CODA_IOCTL_STATS);
508 
509     CODADEBUG(CODA_IOCTL, myprintf(("in coda_ioctl on %s\n", iap->path));)
510 
511     /* Don't check for operation on a dying object, for ctlvp it
512        shouldn't matter */
513 
514     /* Must be control object to succeed. */
515     if (!IS_CTL_VP(vp)) {
516 	MARK_INT_FAIL(CODA_IOCTL_STATS);
517 	CODADEBUG(CODA_IOCTL, myprintf(("%s error: vp != ctlvp", __func__));)
518 	return (EOPNOTSUPP);
519     }
520     /* Look up the pathname. */
521 
522     /* Should we use the name cache here? It would get it from
523        lookupname sooner or later anyway, right? */
524 
525     sflags = iap->follow ? NSM_FOLLOW_NOEMULROOT : NSM_NOFOLLOW_NOEMULROOT;
526     error = namei_simple_user(iap->path, sflags, &tvp);
527 
528     if (error) {
529 	MARK_INT_FAIL(CODA_IOCTL_STATS);
530 	CODADEBUG(CODA_IOCTL, myprintf(("%s error: lookup returns %d\n",
531 	    __func__, error));)
532 	return(error);
533     }
534 
535     /*
536      * Make sure this is a coda style cnode, but it may be a
537      * different vfsp
538      */
539     /* XXX: this totally violates the comment about vtagtype in vnode.h */
540     if (tvp->v_tag != VT_CODA) {
541 	vrele(tvp);
542 	MARK_INT_FAIL(CODA_IOCTL_STATS);
543 	CODADEBUG(CODA_IOCTL, myprintf(("%s error: %s not a coda object\n",
544 	    __func__, iap->path));)
545 	return(EINVAL);
546     }
547 
548     if (iap->vi.in_size > VC_MAXDATASIZE || iap->vi.out_size > VC_MAXDATASIZE) {
549 	vrele(tvp);
550 	return(EINVAL);
551     }
552     error = venus_ioctl(vtomi(tvp), &((VTOC(tvp))->c_fid), com, flag, data,
553 	cred, curlwp);
554 
555     if (error)
556 	MARK_INT_FAIL(CODA_IOCTL_STATS);
557     else
558 	CODADEBUG(CODA_IOCTL, myprintf(("Ioctl returns %d \n", error)); )
559 
560     vrele(tvp);
561     return(error);
562 }
563 
564 /*
565  * To reduce the cost of a user-level venus;we cache attributes in
566  * the kernel.  Each cnode has storage allocated for an attribute. If
567  * c_vattr is valid, return a reference to it. Otherwise, get the
568  * attributes from venus and store them in the cnode.  There is some
569  * question if this method is a security leak. But I think that in
570  * order to make this call, the user must have done a lookup and
571  * opened the file, and therefore should already have access.
572  */
573 int
574 coda_getattr(void *v)
575 {
576 /* true args */
577     struct vop_getattr_args *ap = v;
578     vnode_t *vp = ap->a_vp;
579     struct cnode *cp = VTOC(vp);
580     struct vattr *vap = ap->a_vap;
581     kauth_cred_t cred = ap->a_cred;
582 /* locals */
583     int error;
584 
585     MARK_ENTRY(CODA_GETATTR_STATS);
586 
587     /* Check for getattr of control object. */
588     if (IS_CTL_VP(vp)) {
589 	MARK_INT_FAIL(CODA_GETATTR_STATS);
590 	return(ENOENT);
591     }
592 
593     /* Check to see if the attributes have already been cached */
594     if (VALID_VATTR(cp)) {
595 	CODADEBUG(CODA_GETATTR, { myprintf(("%s: attr cache hit: %s\n",
596 	    __func__, coda_f2s(&cp->c_fid)));})
597 	CODADEBUG(CODA_GETATTR, if (!(codadebug & ~CODA_GETATTR))
598 	    coda_print_vattr(&cp->c_vattr); )
599 
600 	*vap = cp->c_vattr;
601 	MARK_INT_SAT(CODA_GETATTR_STATS);
602 	return(0);
603     }
604 
605     error = venus_getattr(vtomi(vp), &cp->c_fid, cred, curlwp, vap);
606 
607     if (!error) {
608 	CODADEBUG(CODA_GETATTR, myprintf(("%s miss %s: result %d\n",
609 	    __func__, coda_f2s(&cp->c_fid), error)); )
610 
611 	CODADEBUG(CODA_GETATTR, if (!(codadebug & ~CODA_GETATTR))
612 	    coda_print_vattr(vap);	)
613 
614 	/* If not open for write, store attributes in cnode */
615 	if ((cp->c_owrite == 0) && (coda_attr_cache)) {
616 	    cp->c_vattr = *vap;
617 	    cp->c_flags |= C_VATTR;
618 	}
619 
620     }
621     return(error);
622 }
623 
624 int
625 coda_setattr(void *v)
626 {
627 /* true args */
628     struct vop_setattr_args *ap = v;
629     vnode_t *vp = ap->a_vp;
630     struct cnode *cp = VTOC(vp);
631     struct vattr *vap = ap->a_vap;
632     kauth_cred_t cred = ap->a_cred;
633 /* locals */
634     int error;
635 
636     MARK_ENTRY(CODA_SETATTR_STATS);
637 
638     /* Check for setattr of control object. */
639     if (IS_CTL_VP(vp)) {
640 	MARK_INT_FAIL(CODA_SETATTR_STATS);
641 	return(ENOENT);
642     }
643 
644     if (codadebug & CODADBGMSK(CODA_SETATTR)) {
645 	coda_print_vattr(vap);
646     }
647     error = venus_setattr(vtomi(vp), &cp->c_fid, vap, cred, curlwp);
648 
649     if (!error)
650 	cp->c_flags &= ~C_VATTR;
651 
652     CODADEBUG(CODA_SETATTR,	myprintf(("setattr %d\n", error)); )
653     return(error);
654 }
655 
656 int
657 coda_access(void *v)
658 {
659 /* true args */
660     struct vop_access_args *ap = v;
661     vnode_t *vp = ap->a_vp;
662     struct cnode *cp = VTOC(vp);
663     int mode = ap->a_mode;
664     kauth_cred_t cred = ap->a_cred;
665 /* locals */
666     int error;
667 
668     MARK_ENTRY(CODA_ACCESS_STATS);
669 
670     /* Check for access of control object.  Only read access is
671        allowed on it. */
672     if (IS_CTL_VP(vp)) {
673 	/* bogus hack - all will be marked as successes */
674 	MARK_INT_SAT(CODA_ACCESS_STATS);
675 	return(((mode & VREAD) && !(mode & (VWRITE | VEXEC)))
676 	       ? 0 : EACCES);
677     }
678 
679     /*
680      * if the file is a directory, and we are checking exec (eg lookup)
681      * access, and the file is in the namecache, then the user must have
682      * lookup access to it.
683      */
684     if (coda_access_cache) {
685 	if ((vp->v_type == VDIR) && (mode & VEXEC)) {
686 	    if (coda_nc_lookup(cp, ".", 1, cred)) {
687 		MARK_INT_SAT(CODA_ACCESS_STATS);
688 		return(0);                     /* it was in the cache */
689 	    }
690 	}
691     }
692 
693     error = venus_access(vtomi(vp), &cp->c_fid, mode, cred, curlwp);
694 
695     return(error);
696 }
697 
698 /*
699  * CODA abort op, called after namei() when a CREATE/DELETE isn't actually
700  * done. If a buffer has been saved in anticipation of a coda_create or
701  * a coda_remove, delete it.
702  */
703 /* ARGSUSED */
704 int
705 coda_abortop(void *v)
706 {
707 /* true args */
708     struct vop_abortop_args /* {
709 	vnode_t *a_dvp;
710 	struct componentname *a_cnp;
711     } */ *ap = v;
712 
713     (void)ap;
714 /* upcall decl */
715 /* locals */
716 
717     return (0);
718 }
719 
720 int
721 coda_readlink(void *v)
722 {
723 /* true args */
724     struct vop_readlink_args *ap = v;
725     vnode_t *vp = ap->a_vp;
726     struct cnode *cp = VTOC(vp);
727     struct uio *uiop = ap->a_uio;
728     kauth_cred_t cred = ap->a_cred;
729 /* locals */
730     struct lwp *l = curlwp;
731     int error;
732     char *str;
733     int len;
734 
735     MARK_ENTRY(CODA_READLINK_STATS);
736 
737     /* Check for readlink of control object. */
738     if (IS_CTL_VP(vp)) {
739 	MARK_INT_FAIL(CODA_READLINK_STATS);
740 	return(ENOENT);
741     }
742 
743     if ((coda_symlink_cache) && (VALID_SYMLINK(cp))) { /* symlink was cached */
744 	uiop->uio_rw = UIO_READ;
745 	error = uiomove(cp->c_symlink, (int)cp->c_symlen, uiop);
746 	if (error)
747 	    MARK_INT_FAIL(CODA_READLINK_STATS);
748 	else
749 	    MARK_INT_SAT(CODA_READLINK_STATS);
750 	return(error);
751     }
752 
753     error = venus_readlink(vtomi(vp), &cp->c_fid, cred, l, &str, &len);
754 
755     if (!error) {
756 	uiop->uio_rw = UIO_READ;
757 	error = uiomove(str, len, uiop);
758 
759 	if (coda_symlink_cache) {
760 	    cp->c_symlink = str;
761 	    cp->c_symlen = len;
762 	    cp->c_flags |= C_SYMLINK;
763 	} else
764 	    CODA_FREE(str, len);
765     }
766 
767     CODADEBUG(CODA_READLINK, myprintf(("in readlink result %d\n",error));)
768     return(error);
769 }
770 
771 int
772 coda_fsync(void *v)
773 {
774 /* true args */
775     struct vop_fsync_args *ap = v;
776     vnode_t *vp = ap->a_vp;
777     struct cnode *cp = VTOC(vp);
778     kauth_cred_t cred = ap->a_cred;
779 /* locals */
780     vnode_t *convp = cp->c_ovp;
781     int error;
782 
783     MARK_ENTRY(CODA_FSYNC_STATS);
784 
785     /* Check for fsync on an unmounting object */
786     /* The NetBSD kernel, in it's infinite wisdom, can try to fsync
787      * after an unmount has been initiated.  This is a Bad Thing,
788      * which we have to avoid.  Not a legitimate failure for stats.
789      */
790     if (IS_UNMOUNTING(cp)) {
791 	return(ENODEV);
792     }
793 
794     /* Check for fsync of control object. */
795     if (IS_CTL_VP(vp)) {
796 	MARK_INT_SAT(CODA_FSYNC_STATS);
797 	return(0);
798     }
799 
800     if (convp)
801     	VOP_FSYNC(convp, cred, MNT_WAIT, 0, 0);
802 
803     /*
804      * We can expect fsync on any vnode at all if venus is pruging it.
805      * Venus can't very well answer the fsync request, now can it?
806      * Hopefully, it won't have to, because hopefully, venus preserves
807      * the (possibly untrue) invariant that it never purges an open
808      * vnode.  Hopefully.
809      */
810     if (cp->c_flags & C_PURGING) {
811 	return(0);
812     }
813 
814     error = venus_fsync(vtomi(vp), &cp->c_fid, cred, curlwp);
815 
816     CODADEBUG(CODA_FSYNC, myprintf(("in fsync result %d\n",error)); )
817     return(error);
818 }
819 
820 /*
821  * vp is locked on entry, and we must unlock it.
822  * XXX This routine is suspect and probably needs rewriting.
823  */
824 int
825 coda_inactive(void *v)
826 {
827 /* true args */
828     struct vop_inactive_args *ap = v;
829     vnode_t *vp = ap->a_vp;
830     struct cnode *cp = VTOC(vp);
831     kauth_cred_t cred __unused = NULL;
832 
833     /* We don't need to send inactive to venus - DCS */
834     MARK_ENTRY(CODA_INACTIVE_STATS);
835 
836     if (IS_CTL_VP(vp)) {
837 	MARK_INT_SAT(CODA_INACTIVE_STATS);
838 	return 0;
839     }
840 
841     CODADEBUG(CODA_INACTIVE, myprintf(("in inactive, %s, vfsp %p\n",
842 				  coda_f2s(&cp->c_fid), vp->v_mount));)
843 
844     /* If an array has been allocated to hold the symlink, deallocate it */
845     if ((coda_symlink_cache) && (VALID_SYMLINK(cp))) {
846 	if (cp->c_symlink == NULL)
847 	    panic("%s: null symlink pointer in cnode", __func__);
848 
849 	CODA_FREE(cp->c_symlink, cp->c_symlen);
850 	cp->c_flags &= ~C_SYMLINK;
851 	cp->c_symlen = 0;
852     }
853 
854     /* Remove it from the table so it can't be found. */
855     coda_unsave(cp);
856     if (vp->v_mount->mnt_data == NULL) {
857 	myprintf(("Help! vfsp->vfs_data was NULL, but vnode %p wasn't dying\n", vp));
858 	panic("badness in coda_inactive");
859     }
860 
861 #ifdef CODA_VERBOSE
862     /* Sanity checks that perhaps should be panic. */
863     if (vp->v_usecount > 1)
864 	printf("%s: %p usecount %d\n", __func__, vp, vp->v_usecount);
865     if (cp->c_ovp != NULL)
866 	printf("%s: %p ovp != NULL\n", __func__, vp);
867 #endif
868     /* XXX Do we need to VOP_CLOSE container vnodes? */
869     VOP_UNLOCK(vp);
870     if (!IS_UNMOUNTING(cp))
871 	*ap->a_recycle = true;
872 
873     MARK_INT_SAT(CODA_INACTIVE_STATS);
874     return(0);
875 }
876 
877 /*
878  * Coda does not use the normal namecache, but a private version.
879  * Consider how to use the standard facility instead.
880  */
881 int
882 coda_lookup(void *v)
883 {
884 /* true args */
885     struct vop_lookup_args *ap = v;
886     /* (locked) vnode of dir in which to do lookup */
887     vnode_t *dvp = ap->a_dvp;
888     struct cnode *dcp = VTOC(dvp);
889     /* output variable for result */
890     vnode_t **vpp = ap->a_vpp;
891     /* name to lookup */
892     struct componentname *cnp = ap->a_cnp;
893     kauth_cred_t cred = cnp->cn_cred;
894     struct lwp *l = curlwp;
895 /* locals */
896     struct cnode *cp;
897     const char *nm = cnp->cn_nameptr;
898     int len = cnp->cn_namelen;
899     int flags = cnp->cn_flags;
900     CodaFid VFid;
901     int	vtype;
902     int error = 0;
903 
904     MARK_ENTRY(CODA_LOOKUP_STATS);
905 
906     CODADEBUG(CODA_LOOKUP, myprintf(("%s: %s in %s\n", __func__,
907 	nm, coda_f2s(&dcp->c_fid)));)
908 
909     /*
910      * XXX componentname flags in MODMASK are not handled at all
911      */
912 
913     /*
914      * The overall strategy is to switch on the lookup type and get a
915      * result vnode that is vref'd but not locked.  Then, the code at
916      * exit: switches on ., .., and regular lookups and does the right
917      * locking.
918      */
919 
920     /* Check for lookup of control object. */
921     if (IS_CTL_NAME(dvp, nm, len)) {
922 	*vpp = coda_ctlvp;
923 	vref(*vpp);
924 	MARK_INT_SAT(CODA_LOOKUP_STATS);
925 	goto exit;
926     }
927 
928     /* Avoid trying to hand venus an unreasonably long name. */
929     if (len+1 > CODA_MAXNAMLEN) {
930 	MARK_INT_FAIL(CODA_LOOKUP_STATS);
931 	CODADEBUG(CODA_LOOKUP, myprintf(("%s: name too long:, %s (%s)\n",
932 	    __func__, coda_f2s(&dcp->c_fid), nm));)
933 	*vpp = (vnode_t *)0;
934 	error = EINVAL;
935 	goto exit;
936     }
937 
938     /*
939      * Try to resolve the lookup in the minicache.  If that fails, ask
940      * venus to do the lookup.  XXX The interaction between vnode
941      * locking and any locking that coda does is not clear.
942      */
943     cp = coda_nc_lookup(dcp, nm, len, cred);
944     if (cp) {
945 	*vpp = CTOV(cp);
946 	vref(*vpp);
947 	CODADEBUG(CODA_LOOKUP,
948 		 myprintf(("lookup result %d vpp %p\n",error,*vpp));)
949     } else {
950 	/* The name wasn't cached, so ask Venus. */
951 	error = venus_lookup(vtomi(dvp), &dcp->c_fid, nm, len, cred, l, &VFid,
952 	    &vtype);
953 
954 	if (error) {
955 	    MARK_INT_FAIL(CODA_LOOKUP_STATS);
956 	    CODADEBUG(CODA_LOOKUP, myprintf(("%s: lookup error on %s (%s)%d\n",
957 		__func__, coda_f2s(&dcp->c_fid), nm, error));)
958 	    *vpp = (vnode_t *)0;
959 	} else {
960 	    MARK_INT_SAT(CODA_LOOKUP_STATS);
961 	    CODADEBUG(CODA_LOOKUP, myprintf(("%s: %s type %o result %d\n",
962 		__func__, coda_f2s(&VFid), vtype, error)); )
963 
964 	    cp = make_coda_node(&VFid, dvp->v_mount, vtype);
965 	    *vpp = CTOV(cp);
966 	    /* vpp is now vrefed. */
967 
968 	    /*
969 	     * Unless this vnode is marked CODA_NOCACHE, enter it into
970 	     * the coda name cache to avoid a future venus round-trip.
971 	     * XXX Interaction with componentname NOCACHE is unclear.
972 	     */
973 	    if (!(vtype & CODA_NOCACHE))
974 		coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
975 	}
976     }
977 
978  exit:
979     /*
980      * If we are creating, and this was the last name to be looked up,
981      * and the error was ENOENT, then make the leaf NULL and return
982      * success.
983      * XXX Check against new lookup rules.
984      */
985     if (((cnp->cn_nameiop == CREATE) || (cnp->cn_nameiop == RENAME))
986 	&& (cnp->cn_flags & ISLASTCN)
987 	&& (error == ENOENT))
988     {
989 	error = EJUSTRETURN;
990 	*ap->a_vpp = NULL;
991     }
992 
993     /*
994      * If the lookup succeeded, we must generally lock the returned
995      * vnode.  This could be a ., .., or normal lookup.  See
996      * vnodeops(9) for the details.
997      */
998     /*
999      * XXX LK_RETRY is likely incorrect.  Handle vn_lock failure
1000      * somehow, and remove LK_RETRY.
1001      */
1002     if (!error || (error == EJUSTRETURN)) {
1003 	/* Lookup has a value and it isn't "."? */
1004 	if (*ap->a_vpp && (*ap->a_vpp != dvp)) {
1005 	    if (flags & ISDOTDOT)
1006 		/* ..: unlock parent */
1007 		VOP_UNLOCK(dvp);
1008 	    /* all but .: lock child */
1009 	    vn_lock(*ap->a_vpp, LK_EXCLUSIVE | LK_RETRY);
1010 	    if (flags & ISDOTDOT)
1011 		/* ..: relock parent */
1012 	        vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1013 	}
1014 	/* else .: leave dvp locked */
1015     } else {
1016 	/* The lookup failed, so return NULL.  Leave dvp locked. */
1017 	*ap->a_vpp = NULL;
1018     }
1019     return(error);
1020 }
1021 
1022 /*ARGSUSED*/
1023 int
1024 coda_create(void *v)
1025 {
1026 /* true args */
1027     struct vop_create_args *ap = v;
1028     vnode_t *dvp = ap->a_dvp;
1029     struct cnode *dcp = VTOC(dvp);
1030     struct vattr *va = ap->a_vap;
1031     int exclusive = 1;
1032     int mode = ap->a_vap->va_mode;
1033     vnode_t **vpp = ap->a_vpp;
1034     struct componentname  *cnp = ap->a_cnp;
1035     kauth_cred_t cred = cnp->cn_cred;
1036     struct lwp *l = curlwp;
1037 /* locals */
1038     int error;
1039     struct cnode *cp;
1040     const char *nm = cnp->cn_nameptr;
1041     int len = cnp->cn_namelen;
1042     CodaFid VFid;
1043     struct vattr attr;
1044 
1045     MARK_ENTRY(CODA_CREATE_STATS);
1046 
1047     /* All creates are exclusive XXX */
1048     /* I'm assuming the 'mode' argument is the file mode bits XXX */
1049 
1050     /* Check for create of control object. */
1051     if (IS_CTL_NAME(dvp, nm, len)) {
1052 	*vpp = (vnode_t *)0;
1053 	MARK_INT_FAIL(CODA_CREATE_STATS);
1054 	return(EACCES);
1055     }
1056 
1057     error = venus_create(vtomi(dvp), &dcp->c_fid, nm, len, exclusive, mode, va, cred, l, &VFid, &attr);
1058 
1059     if (!error) {
1060 
1061         /*
1062 	 * XXX Violation of venus/kernel invariants is a difficult case,
1063 	 * but venus should not be able to cause a panic.
1064 	 */
1065 	/* If this is an exclusive create, panic if the file already exists. */
1066 	/* Venus should have detected the file and reported EEXIST. */
1067 
1068 	if ((exclusive == 1) &&
1069 	    (coda_find(&VFid) != NULL))
1070 	    panic("cnode existed for newly created file!");
1071 
1072 	cp = make_coda_node(&VFid, dvp->v_mount, attr.va_type);
1073 	*vpp = CTOV(cp);
1074 
1075 	/* XXX vnodeops doesn't say this argument can be changed. */
1076 	/* Update va to reflect the new attributes. */
1077 	(*va) = attr;
1078 
1079 	/* Update the attribute cache and mark it as valid */
1080 	if (coda_attr_cache) {
1081 	    VTOC(*vpp)->c_vattr = attr;
1082 	    VTOC(*vpp)->c_flags |= C_VATTR;
1083 	}
1084 
1085 	/* Invalidate parent's attr cache (modification time has changed). */
1086 	VTOC(dvp)->c_flags &= ~C_VATTR;
1087 
1088 	/* enter the new vnode in the Name Cache */
1089 	coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
1090 
1091 	CODADEBUG(CODA_CREATE, myprintf(("%s: %s, result %d\n", __func__,
1092 	    coda_f2s(&VFid), error)); )
1093     } else {
1094 	*vpp = (vnode_t *)0;
1095 	CODADEBUG(CODA_CREATE, myprintf(("%s: create error %d\n", __func__,
1096 	    error));)
1097     }
1098 
1099     /*
1100      * vnodeops(9) says that we must unlock the parent and lock the child.
1101      * XXX Should we lock the child first?
1102      */
1103     vput(dvp);
1104     if (!error) {
1105 #ifdef CODA_VERBOSE
1106 	if ((cnp->cn_flags & LOCKLEAF) == 0)
1107 	    /* This should not happen; flags are for lookup only. */
1108 	    printf("%s: LOCKLEAF not set!\n", __func__);
1109 
1110 	if ((error = vn_lock(*ap->a_vpp, LK_EXCLUSIVE)))
1111 	    /* XXX Perhaps avoid this panic. */
1112 	    panic("%s: couldn't lock child", __func__);
1113 #endif
1114     }
1115 
1116     return(error);
1117 }
1118 
1119 int
1120 coda_remove(void *v)
1121 {
1122 /* true args */
1123     struct vop_remove_args *ap = v;
1124     vnode_t *dvp = ap->a_dvp;
1125     struct cnode *cp = VTOC(dvp);
1126     vnode_t *vp = ap->a_vp;
1127     struct componentname  *cnp = ap->a_cnp;
1128     kauth_cred_t cred = cnp->cn_cred;
1129     struct lwp *l = curlwp;
1130 /* locals */
1131     int error;
1132     const char *nm = cnp->cn_nameptr;
1133     int len = cnp->cn_namelen;
1134     struct cnode *tp;
1135 
1136     MARK_ENTRY(CODA_REMOVE_STATS);
1137 
1138     CODADEBUG(CODA_REMOVE, myprintf(("%s: %s in %s\n", __func__,
1139 	nm, coda_f2s(&cp->c_fid)));)
1140 
1141     /* Remove the file's entry from the CODA Name Cache */
1142     /* We're being conservative here, it might be that this person
1143      * doesn't really have sufficient access to delete the file
1144      * but we feel zapping the entry won't really hurt anyone -- dcs
1145      */
1146     /* I'm gonna go out on a limb here. If a file and a hardlink to it
1147      * exist, and one is removed, the link count on the other will be
1148      * off by 1. We could either invalidate the attrs if cached, or
1149      * fix them. I'll try to fix them. DCS 11/8/94
1150      */
1151     tp = coda_nc_lookup(VTOC(dvp), nm, len, cred);
1152     if (tp) {
1153 	if (VALID_VATTR(tp)) {	/* If attrs are cached */
1154 	    if (tp->c_vattr.va_nlink > 1) {	/* If it's a hard link */
1155 		tp->c_vattr.va_nlink--;
1156 	    }
1157 	}
1158 
1159 	coda_nc_zapfile(VTOC(dvp), nm, len);
1160 	/* No need to flush it if it doesn't exist! */
1161     }
1162     /* Invalidate the parent's attr cache, the modification time has changed */
1163     VTOC(dvp)->c_flags &= ~C_VATTR;
1164 
1165     /* Check for remove of control object. */
1166     if (IS_CTL_NAME(dvp, nm, len)) {
1167 	MARK_INT_FAIL(CODA_REMOVE_STATS);
1168 	return(ENOENT);
1169     }
1170 
1171     error = venus_remove(vtomi(dvp), &cp->c_fid, nm, len, cred, l);
1172 
1173     CODADEBUG(CODA_REMOVE, myprintf(("in remove result %d\n",error)); )
1174 
1175     /*
1176      * Unlock parent and child (avoiding double if ".").
1177      */
1178     if (dvp == vp) {
1179 	vrele(vp);
1180     } else {
1181 	vput(vp);
1182     }
1183     vput(dvp);
1184 
1185     return(error);
1186 }
1187 
1188 /*
1189  * dvp is the directory where the link is to go, and is locked.
1190  * vp is the object to be linked to, and is unlocked.
1191  * At exit, we must unlock dvp, and vput dvp.
1192  */
1193 int
1194 coda_link(void *v)
1195 {
1196 /* true args */
1197     struct vop_link_args *ap = v;
1198     vnode_t *vp = ap->a_vp;
1199     struct cnode *cp = VTOC(vp);
1200     vnode_t *dvp = ap->a_dvp;
1201     struct cnode *dcp = VTOC(dvp);
1202     struct componentname *cnp = ap->a_cnp;
1203     kauth_cred_t cred = cnp->cn_cred;
1204     struct lwp *l = curlwp;
1205 /* locals */
1206     int error;
1207     const char *nm = cnp->cn_nameptr;
1208     int len = cnp->cn_namelen;
1209 
1210     MARK_ENTRY(CODA_LINK_STATS);
1211 
1212     if (codadebug & CODADBGMSK(CODA_LINK)) {
1213 
1214 	myprintf(("%s: vp fid: %s\n", __func__, coda_f2s(&cp->c_fid)));
1215 	myprintf(("%s: dvp fid: %s)\n", __func__, coda_f2s(&dcp->c_fid)));
1216 
1217     }
1218     if (codadebug & CODADBGMSK(CODA_LINK)) {
1219 	myprintf(("%s: vp fid: %s\n", __func__, coda_f2s(&cp->c_fid)));
1220 	myprintf(("%s: dvp fid: %s\n", __func__, coda_f2s(&dcp->c_fid)));
1221 
1222     }
1223 
1224     /* Check for link to/from control object. */
1225     if (IS_CTL_NAME(dvp, nm, len) || IS_CTL_VP(vp)) {
1226 	MARK_INT_FAIL(CODA_LINK_STATS);
1227 	return(EACCES);
1228     }
1229 
1230     /* If linking . to a name, error out earlier. */
1231     if (vp == dvp) {
1232 #ifdef CODA_VERBOSE
1233         printf("%s coda_link vp==dvp\n", __func__);
1234 #endif
1235 	error = EISDIR;
1236 	goto exit;
1237     }
1238 
1239     /* XXX Why does venus_link need the vnode to be locked?*/
1240     if ((error = vn_lock(vp, LK_EXCLUSIVE)) != 0) {
1241 #ifdef CODA_VERBOSE
1242 	printf("%s: couldn't lock vnode %p\n", __func__, vp);
1243 #endif
1244 	error = EFAULT;		/* XXX better value */
1245 	goto exit;
1246     }
1247     error = venus_link(vtomi(vp), &cp->c_fid, &dcp->c_fid, nm, len, cred, l);
1248     VOP_UNLOCK(vp);
1249 
1250     /* Invalidate parent's attr cache (the modification time has changed). */
1251     VTOC(dvp)->c_flags &= ~C_VATTR;
1252     /* Invalidate child's attr cache (XXX why). */
1253     VTOC(vp)->c_flags &= ~C_VATTR;
1254 
1255     CODADEBUG(CODA_LINK,	myprintf(("in link result %d\n",error)); )
1256 
1257 exit:
1258     vput(dvp);
1259     return(error);
1260 }
1261 
1262 int
1263 coda_rename(void *v)
1264 {
1265 /* true args */
1266     struct vop_rename_args *ap = v;
1267     vnode_t *odvp = ap->a_fdvp;
1268     struct cnode *odcp = VTOC(odvp);
1269     struct componentname  *fcnp = ap->a_fcnp;
1270     vnode_t *ndvp = ap->a_tdvp;
1271     struct cnode *ndcp = VTOC(ndvp);
1272     struct componentname  *tcnp = ap->a_tcnp;
1273     kauth_cred_t cred = fcnp->cn_cred;
1274     struct lwp *l = curlwp;
1275 /* true args */
1276     int error;
1277     const char *fnm = fcnp->cn_nameptr;
1278     int flen = fcnp->cn_namelen;
1279     const char *tnm = tcnp->cn_nameptr;
1280     int tlen = tcnp->cn_namelen;
1281 
1282     MARK_ENTRY(CODA_RENAME_STATS);
1283 
1284     /* Hmmm.  The vnodes are already looked up.  Perhaps they are locked?
1285        This could be Bad. XXX */
1286 #ifdef OLD_DIAGNOSTIC
1287     if ((fcnp->cn_cred != tcnp->cn_cred)
1288 	|| (fcnp->cn_lwp != tcnp->cn_lwp))
1289     {
1290 	panic("%s: component names don't agree", __func__);
1291     }
1292 #endif
1293 
1294     /* Check for rename involving control object. */
1295     if (IS_CTL_NAME(odvp, fnm, flen) || IS_CTL_NAME(ndvp, tnm, tlen)) {
1296 	MARK_INT_FAIL(CODA_RENAME_STATS);
1297 	return(EACCES);
1298     }
1299 
1300     /* Problem with moving directories -- need to flush entry for .. */
1301     if (odvp != ndvp) {
1302 	struct cnode *ovcp = coda_nc_lookup(VTOC(odvp), fnm, flen, cred);
1303 	if (ovcp) {
1304 	    vnode_t *ovp = CTOV(ovcp);
1305 	    if ((ovp) &&
1306 		(ovp->v_type == VDIR)) /* If it's a directory */
1307 		coda_nc_zapfile(VTOC(ovp),"..", 2);
1308 	}
1309     }
1310 
1311     /* Remove the entries for both source and target files */
1312     coda_nc_zapfile(VTOC(odvp), fnm, flen);
1313     coda_nc_zapfile(VTOC(ndvp), tnm, tlen);
1314 
1315     /* Invalidate the parent's attr cache, the modification time has changed */
1316     VTOC(odvp)->c_flags &= ~C_VATTR;
1317     VTOC(ndvp)->c_flags &= ~C_VATTR;
1318 
1319     if (flen+1 > CODA_MAXNAMLEN) {
1320 	MARK_INT_FAIL(CODA_RENAME_STATS);
1321 	error = EINVAL;
1322 	goto exit;
1323     }
1324 
1325     if (tlen+1 > CODA_MAXNAMLEN) {
1326 	MARK_INT_FAIL(CODA_RENAME_STATS);
1327 	error = EINVAL;
1328 	goto exit;
1329     }
1330 
1331     error = venus_rename(vtomi(odvp), &odcp->c_fid, &ndcp->c_fid, fnm, flen, tnm, tlen, cred, l);
1332 
1333  exit:
1334     CODADEBUG(CODA_RENAME, myprintf(("in rename result %d\n",error));)
1335     /* XXX - do we need to call cache pureg on the moved vnode? */
1336     cache_purge(ap->a_fvp);
1337 
1338     /* It seems to be incumbent on us to drop locks on all four vnodes */
1339     /* From-vnodes are not locked, only ref'd.  To-vnodes are locked. */
1340 
1341     vrele(ap->a_fvp);
1342     vrele(odvp);
1343 
1344     if (ap->a_tvp) {
1345 	if (ap->a_tvp == ndvp) {
1346 	    vrele(ap->a_tvp);
1347 	} else {
1348 	    vput(ap->a_tvp);
1349 	}
1350     }
1351 
1352     vput(ndvp);
1353     return(error);
1354 }
1355 
1356 int
1357 coda_mkdir(void *v)
1358 {
1359 /* true args */
1360     struct vop_mkdir_args *ap = v;
1361     vnode_t *dvp = ap->a_dvp;
1362     struct cnode *dcp = VTOC(dvp);
1363     struct componentname  *cnp = ap->a_cnp;
1364     struct vattr *va = ap->a_vap;
1365     vnode_t **vpp = ap->a_vpp;
1366     kauth_cred_t cred = cnp->cn_cred;
1367     struct lwp *l = curlwp;
1368 /* locals */
1369     int error;
1370     const char *nm = cnp->cn_nameptr;
1371     int len = cnp->cn_namelen;
1372     struct cnode *cp;
1373     CodaFid VFid;
1374     struct vattr ova;
1375 
1376     MARK_ENTRY(CODA_MKDIR_STATS);
1377 
1378     /* Check for mkdir of target object. */
1379     if (IS_CTL_NAME(dvp, nm, len)) {
1380 	*vpp = (vnode_t *)0;
1381 	MARK_INT_FAIL(CODA_MKDIR_STATS);
1382 	return(EACCES);
1383     }
1384 
1385     if (len+1 > CODA_MAXNAMLEN) {
1386 	*vpp = (vnode_t *)0;
1387 	MARK_INT_FAIL(CODA_MKDIR_STATS);
1388 	return(EACCES);
1389     }
1390 
1391     error = venus_mkdir(vtomi(dvp), &dcp->c_fid, nm, len, va, cred, l, &VFid, &ova);
1392 
1393     if (!error) {
1394 	if (coda_find(&VFid) != NULL)
1395 	    panic("cnode existed for newly created directory!");
1396 
1397 
1398 	cp =  make_coda_node(&VFid, dvp->v_mount, va->va_type);
1399 	*vpp = CTOV(cp);
1400 
1401 	/* enter the new vnode in the Name Cache */
1402 	coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
1403 
1404 	/* as a side effect, enter "." and ".." for the directory */
1405 	coda_nc_enter(VTOC(*vpp), ".", 1, cred, VTOC(*vpp));
1406 	coda_nc_enter(VTOC(*vpp), "..", 2, cred, VTOC(dvp));
1407 
1408 	if (coda_attr_cache) {
1409 	    VTOC(*vpp)->c_vattr = ova;		/* update the attr cache */
1410 	    VTOC(*vpp)->c_flags |= C_VATTR;	/* Valid attributes in cnode */
1411 	}
1412 
1413 	/* Invalidate the parent's attr cache, the modification time has changed */
1414 	VTOC(dvp)->c_flags &= ~C_VATTR;
1415 
1416 	CODADEBUG( CODA_MKDIR, myprintf(("%s: %s result %d\n", __func__,
1417 	    coda_f2s(&VFid), error)); )
1418     } else {
1419 	*vpp = (vnode_t *)0;
1420 	CODADEBUG(CODA_MKDIR, myprintf(("%s error %d\n", __func__, error));)
1421     }
1422 
1423     /*
1424      * Currently, all mkdirs explicitly vput their dvp's.
1425      * It also appears that we *must* lock the vpp, since
1426      * lockleaf isn't set, but someone down the road is going
1427      * to try to unlock the new directory.
1428      */
1429     vput(dvp);
1430     if (!error) {
1431 	if ((error = vn_lock(*ap->a_vpp, LK_EXCLUSIVE))) {
1432 	    panic("%s: couldn't lock child", __func__);
1433 	}
1434     }
1435 
1436     return(error);
1437 }
1438 
1439 int
1440 coda_rmdir(void *v)
1441 {
1442 /* true args */
1443     struct vop_rmdir_args *ap = v;
1444     vnode_t *dvp = ap->a_dvp;
1445     struct cnode *dcp = VTOC(dvp);
1446     vnode_t *vp = ap->a_vp;
1447     struct componentname  *cnp = ap->a_cnp;
1448     kauth_cred_t cred = cnp->cn_cred;
1449     struct lwp *l = curlwp;
1450 /* true args */
1451     int error;
1452     const char *nm = cnp->cn_nameptr;
1453     int len = cnp->cn_namelen;
1454     struct cnode *cp;
1455 
1456     MARK_ENTRY(CODA_RMDIR_STATS);
1457 
1458     /* Check for rmdir of control object. */
1459     if (IS_CTL_NAME(dvp, nm, len)) {
1460 	MARK_INT_FAIL(CODA_RMDIR_STATS);
1461 	return(ENOENT);
1462     }
1463 
1464     /* Can't remove . in self. */
1465     if (dvp == vp) {
1466 #ifdef CODA_VERBOSE
1467 	printf("%s: dvp == vp\n", __func__);
1468 #endif
1469 	error = EINVAL;
1470 	goto exit;
1471     }
1472 
1473     /*
1474      * The caller may not have adequate permissions, and the venus
1475      * operation may fail, but it doesn't hurt from a correctness
1476      * viewpoint to invalidate cache entries.
1477      * XXX Why isn't this done after the venus_rmdir call?
1478      */
1479     /* Look up child in name cache (by name, from parent). */
1480     cp = coda_nc_lookup(dcp, nm, len, cred);
1481     /* If found, remove all children of the child (., ..). */
1482     if (cp) coda_nc_zapParentfid(&(cp->c_fid), NOT_DOWNCALL);
1483 
1484     /* Remove child's own entry. */
1485     coda_nc_zapfile(dcp, nm, len);
1486 
1487     /* Invalidate parent's attr cache (the modification time has changed). */
1488     dcp->c_flags &= ~C_VATTR;
1489 
1490     error = venus_rmdir(vtomi(dvp), &dcp->c_fid, nm, len, cred, l);
1491 
1492     CODADEBUG(CODA_RMDIR, myprintf(("in rmdir result %d\n", error)); )
1493 
1494 exit:
1495     /* vput both vnodes */
1496     vput(dvp);
1497     if (dvp == vp) {
1498 	vrele(vp);
1499     } else {
1500 	vput(vp);
1501     }
1502 
1503     return(error);
1504 }
1505 
1506 int
1507 coda_symlink(void *v)
1508 {
1509 /* true args */
1510     struct vop_symlink_args *ap = v;
1511     vnode_t *dvp = ap->a_dvp;
1512     struct cnode *dcp = VTOC(dvp);
1513     /* a_vpp is used in place below */
1514     struct componentname *cnp = ap->a_cnp;
1515     struct vattr *tva = ap->a_vap;
1516     char *path = ap->a_target;
1517     kauth_cred_t cred = cnp->cn_cred;
1518     struct lwp *l = curlwp;
1519 /* locals */
1520     int error;
1521     u_long saved_cn_flags;
1522     const char *nm = cnp->cn_nameptr;
1523     int len = cnp->cn_namelen;
1524     int plen = strlen(path);
1525 
1526     /*
1527      * Here's the strategy for the moment: perform the symlink, then
1528      * do a lookup to grab the resulting vnode.  I know this requires
1529      * two communications with Venus for a new sybolic link, but
1530      * that's the way the ball bounces.  I don't yet want to change
1531      * the way the Mach symlink works.  When Mach support is
1532      * deprecated, we should change symlink so that the common case
1533      * returns the resultant vnode in a vpp argument.
1534      */
1535 
1536     MARK_ENTRY(CODA_SYMLINK_STATS);
1537 
1538     /* Check for symlink of control object. */
1539     if (IS_CTL_NAME(dvp, nm, len)) {
1540 	MARK_INT_FAIL(CODA_SYMLINK_STATS);
1541 	error = EACCES;
1542 	goto exit;
1543     }
1544 
1545     if (plen+1 > CODA_MAXPATHLEN) {
1546 	MARK_INT_FAIL(CODA_SYMLINK_STATS);
1547 	error = EINVAL;
1548 	goto exit;
1549     }
1550 
1551     if (len+1 > CODA_MAXNAMLEN) {
1552 	MARK_INT_FAIL(CODA_SYMLINK_STATS);
1553 	error = EINVAL;
1554 	goto exit;
1555     }
1556 
1557     error = venus_symlink(vtomi(dvp), &dcp->c_fid, path, plen, nm, len, tva, cred, l);
1558 
1559     /* Invalidate the parent's attr cache (modification time has changed). */
1560     dcp->c_flags &= ~C_VATTR;
1561 
1562     if (!error) {
1563 	/*
1564 	 * VOP_SYMLINK is not defined to pay attention to cnp->cn_flags;
1565 	 * these are defined only for VOP_LOOKUP.   We desire to reuse
1566 	 * cnp for a VOP_LOOKUP operation, and must be sure to not pass
1567 	 * stray flags passed to us.  Such stray flags can occur because
1568 	 * sys_symlink makes a namei call and then reuses the
1569 	 * componentname structure.
1570 	 */
1571 	/*
1572 	 * XXX Arguably we should create our own componentname structure
1573 	 * and not reuse the one that was passed in.
1574 	 */
1575 	saved_cn_flags = cnp->cn_flags;
1576 	cnp->cn_flags &= ~(MODMASK | OPMASK);
1577 	cnp->cn_flags |= LOOKUP;
1578 	error = VOP_LOOKUP(dvp, ap->a_vpp, cnp);
1579 	cnp->cn_flags = saved_cn_flags;
1580 	/* Either an error occurs, or ap->a_vpp is locked. */
1581     }
1582 
1583  exit:
1584     /* unlock and deference parent */
1585     vput(dvp);
1586 
1587     CODADEBUG(CODA_SYMLINK, myprintf(("in symlink result %d\n",error)); )
1588     return(error);
1589 }
1590 
1591 /*
1592  * Read directory entries.
1593  */
1594 int
1595 coda_readdir(void *v)
1596 {
1597 /* true args */
1598     struct vop_readdir_args *ap = v;
1599     vnode_t *vp = ap->a_vp;
1600     struct cnode *cp = VTOC(vp);
1601     struct uio *uiop = ap->a_uio;
1602     kauth_cred_t cred = ap->a_cred;
1603     int *eofflag = ap->a_eofflag;
1604     off_t **cookies = ap->a_cookies;
1605     int *ncookies = ap->a_ncookies;
1606 /* upcall decl */
1607 /* locals */
1608     int error = 0;
1609 
1610     MARK_ENTRY(CODA_READDIR_STATS);
1611 
1612     CODADEBUG(CODA_READDIR, myprintf(("%s: (%p, %lu, %lld)\n", __func__,
1613 	uiop->uio_iov->iov_base, (unsigned long) uiop->uio_resid,
1614 	(long long) uiop->uio_offset)); )
1615 
1616     /* Check for readdir of control object. */
1617     if (IS_CTL_VP(vp)) {
1618 	MARK_INT_FAIL(CODA_READDIR_STATS);
1619 	return(ENOENT);
1620     }
1621 
1622     {
1623 	/* Redirect the request to UFS. */
1624 
1625 	/* If directory is not already open do an "internal open" on it. */
1626 	int opened_internally = 0;
1627 	if (cp->c_ovp == NULL) {
1628 	    opened_internally = 1;
1629 	    MARK_INT_GEN(CODA_OPEN_STATS);
1630 	    error = VOP_OPEN(vp, FREAD, cred);
1631 #ifdef	CODA_VERBOSE
1632 	    printf("%s: Internally Opening %p\n", __func__, vp);
1633 #endif
1634 	    if (error) return(error);
1635 	} else
1636 	    vp = cp->c_ovp;
1637 
1638 	/* Have UFS handle the call. */
1639 	CODADEBUG(CODA_READDIR, myprintf(("%s: fid = %s, refcnt = %d\n",
1640 	    __func__, coda_f2s(&cp->c_fid), vp->v_usecount)); )
1641 	error = VOP_READDIR(vp, uiop, cred, eofflag, cookies, ncookies);
1642 	if (error)
1643 	    MARK_INT_FAIL(CODA_READDIR_STATS);
1644 	else
1645 	    MARK_INT_SAT(CODA_READDIR_STATS);
1646 
1647 	/* Do an "internal close" if necessary. */
1648 	if (opened_internally) {
1649 	    MARK_INT_GEN(CODA_CLOSE_STATS);
1650 	    (void)VOP_CLOSE(vp, FREAD, cred);
1651 	}
1652     }
1653 
1654     return(error);
1655 }
1656 
1657 /*
1658  * Convert from file system blocks to device blocks
1659  */
1660 int
1661 coda_bmap(void *v)
1662 {
1663     /* XXX on the global proc */
1664 /* true args */
1665     struct vop_bmap_args *ap = v;
1666     vnode_t *vp __unused = ap->a_vp;	/* file's vnode */
1667     daddr_t bn __unused = ap->a_bn;	/* fs block number */
1668     vnode_t **vpp = ap->a_vpp;			/* RETURN vp of device */
1669     daddr_t *bnp __unused = ap->a_bnp;	/* RETURN device block number */
1670     struct lwp *l __unused = curlwp;
1671 /* upcall decl */
1672 /* locals */
1673 
1674 	*vpp = (vnode_t *)0;
1675 	myprintf(("coda_bmap called!\n"));
1676 	return(EINVAL);
1677 }
1678 
1679 /*
1680  * I don't think the following two things are used anywhere, so I've
1681  * commented them out
1682  *
1683  * struct buf *async_bufhead;
1684  * int async_daemon_count;
1685  */
1686 int
1687 coda_strategy(void *v)
1688 {
1689 /* true args */
1690     struct vop_strategy_args *ap = v;
1691     struct buf *bp __unused = ap->a_bp;
1692     struct lwp *l __unused = curlwp;
1693 /* upcall decl */
1694 /* locals */
1695 
1696 	myprintf(("coda_strategy called!  "));
1697 	return(EINVAL);
1698 }
1699 
1700 int
1701 coda_reclaim(void *v)
1702 {
1703 /* true args */
1704     struct vop_reclaim_args *ap = v;
1705     vnode_t *vp = ap->a_vp;
1706     struct cnode *cp = VTOC(vp);
1707 /* upcall decl */
1708 /* locals */
1709 
1710 /*
1711  * Forced unmount/flush will let vnodes with non zero use be destroyed!
1712  */
1713     ENTRY;
1714 
1715     if (IS_UNMOUNTING(cp)) {
1716 #ifdef	DEBUG
1717 	if (VTOC(vp)->c_ovp) {
1718 	    if (IS_UNMOUNTING(cp))
1719 		printf("%s: c_ovp not void: vp %p, cp %p\n", __func__, vp, cp);
1720 	}
1721 #endif
1722     } else {
1723 #ifdef OLD_DIAGNOSTIC
1724 	if (vp->v_usecount != 0)
1725 	    print("%s: pushing active %p\n", __func__, vp);
1726 	if (VTOC(vp)->c_ovp) {
1727 	    panic("%s: c_ovp not void", __func__);
1728 	}
1729 #endif
1730     }
1731     coda_free(VTOC(vp));
1732     SET_VTOC(vp) = NULL;
1733     return (0);
1734 }
1735 
1736 int
1737 coda_lock(void *v)
1738 {
1739 /* true args */
1740     struct vop_lock_args *ap = v;
1741     vnode_t *vp = ap->a_vp;
1742     struct cnode *cp = VTOC(vp);
1743 /* upcall decl */
1744 /* locals */
1745 
1746     ENTRY;
1747 
1748     if (coda_lockdebug) {
1749 	myprintf(("Attempting lock on %s\n",
1750 		  coda_f2s(&cp->c_fid)));
1751     }
1752 
1753     return genfs_lock(v);
1754 }
1755 
1756 int
1757 coda_unlock(void *v)
1758 {
1759 /* true args */
1760     struct vop_unlock_args *ap = v;
1761     vnode_t *vp = ap->a_vp;
1762     struct cnode *cp = VTOC(vp);
1763 /* upcall decl */
1764 /* locals */
1765 
1766     ENTRY;
1767     if (coda_lockdebug) {
1768 	myprintf(("Attempting unlock on %s\n",
1769 		  coda_f2s(&cp->c_fid)));
1770     }
1771 
1772     return genfs_unlock(v);
1773 }
1774 
1775 int
1776 coda_islocked(void *v)
1777 {
1778 /* true args */
1779     ENTRY;
1780 
1781     return genfs_islocked(v);
1782 }
1783 
1784 /*
1785  * Given a device and inode, obtain a locked vnode.  One reference is
1786  * obtained and passed back to the caller.
1787  */
1788 int
1789 coda_grab_vnode(vnode_t *uvp, dev_t dev, ino_t ino, vnode_t **vpp)
1790 {
1791     int           error;
1792     struct mount *mp;
1793 
1794     /* Obtain mount point structure from device. */
1795     if (!(mp = devtomp(dev))) {
1796 	myprintf(("%s: devtomp(0x%llx) returns NULL\n", __func__,
1797 	    (unsigned long long)dev));
1798 	return(ENXIO);
1799     }
1800 
1801     /*
1802      * Obtain vnode from mount point and inode.
1803      * XXX VFS_VGET does not clearly define locked/referenced state of
1804      * returned vnode.
1805      */
1806     error = VFS_VGET(mp, ino, vpp);
1807     if (error) {
1808 	myprintf(("%s: iget/vget(0x%llx, %llu) returns %p, err %d\n", __func__,
1809 	    (unsigned long long)dev, (unsigned long long)ino, *vpp, error));
1810 	return(ENOENT);
1811     }
1812     /* share the underlying vnode lock with the coda vnode */
1813     mutex_obj_hold((*vpp)->v_interlock);
1814     uvm_obj_setlock(&uvp->v_uobj, (*vpp)->v_interlock);
1815     if (!VOP_ISLOCKED(*vpp))
1816 	VOP_LOCK(*vpp, LK_EXCLUSIVE);
1817     return(0);
1818 }
1819 
1820 static void
1821 coda_print_vattr(struct vattr *attr)
1822 {
1823     const char *typestr;
1824 
1825     switch (attr->va_type) {
1826     case VNON:
1827 	typestr = "VNON";
1828 	break;
1829     case VREG:
1830 	typestr = "VREG";
1831 	break;
1832     case VDIR:
1833 	typestr = "VDIR";
1834 	break;
1835     case VBLK:
1836 	typestr = "VBLK";
1837 	break;
1838     case VCHR:
1839 	typestr = "VCHR";
1840 	break;
1841     case VLNK:
1842 	typestr = "VLNK";
1843 	break;
1844     case VSOCK:
1845 	typestr = "VSCK";
1846 	break;
1847     case VFIFO:
1848 	typestr = "VFFO";
1849 	break;
1850     case VBAD:
1851 	typestr = "VBAD";
1852 	break;
1853     default:
1854 	typestr = "????";
1855 	break;
1856     }
1857 
1858 
1859     myprintf(("attr: type %s mode %d uid %d gid %d fsid %d rdev %d\n",
1860 	      typestr, (int)attr->va_mode, (int)attr->va_uid,
1861 	      (int)attr->va_gid, (int)attr->va_fsid, (int)attr->va_rdev));
1862 
1863     myprintf(("      fileid %d nlink %d size %d blocksize %d bytes %d\n",
1864 	      (int)attr->va_fileid, (int)attr->va_nlink,
1865 	      (int)attr->va_size,
1866 	      (int)attr->va_blocksize,(int)attr->va_bytes));
1867     myprintf(("      gen %ld flags %ld vaflags %d\n",
1868 	      attr->va_gen, attr->va_flags, attr->va_vaflags));
1869     myprintf(("      atime sec %d nsec %d\n",
1870 	      (int)attr->va_atime.tv_sec, (int)attr->va_atime.tv_nsec));
1871     myprintf(("      mtime sec %d nsec %d\n",
1872 	      (int)attr->va_mtime.tv_sec, (int)attr->va_mtime.tv_nsec));
1873     myprintf(("      ctime sec %d nsec %d\n",
1874 	      (int)attr->va_ctime.tv_sec, (int)attr->va_ctime.tv_nsec));
1875 }
1876 
1877 /*
1878  * Return a vnode for the given fid.
1879  * If no cnode exists for this fid create one and put it
1880  * in a table hashed by coda_f2i().  If the cnode for
1881  * this fid is already in the table return it (ref count is
1882  * incremented by coda_find.  The cnode will be flushed from the
1883  * table when coda_inactive calls coda_unsave.
1884  */
1885 struct cnode *
1886 make_coda_node(CodaFid *fid, struct mount *fvsp, short type)
1887 {
1888     struct cnode *cp;
1889     int          error;
1890 
1891     if ((cp = coda_find(fid)) == NULL) {
1892 	vnode_t *vp;
1893 
1894 	cp = coda_alloc();
1895 	cp->c_fid = *fid;
1896 
1897 	error = getnewvnode(VT_CODA, fvsp, coda_vnodeop_p, NULL, &vp);
1898 	if (error) {
1899 	    panic("%s: getnewvnode returned error %d", __func__, error);
1900 	}
1901 	vp->v_data = cp;
1902 	vp->v_type = type;
1903 	cp->c_vnode = vp;
1904 	uvm_vnp_setsize(vp, 0);
1905 	coda_save(cp);
1906 
1907     } else {
1908 	vref(CTOV(cp));
1909     }
1910 
1911     return cp;
1912 }
1913 
1914 /*
1915  * coda_getpages may be called on a vnode which has not been opened,
1916  * e.g. to fault in pages to execute a program.  In that case, we must
1917  * open the file to get the container.  The vnode may or may not be
1918  * locked, and we must leave it in the same state.
1919  */
1920 int
1921 coda_getpages(void *v)
1922 {
1923 	struct vop_getpages_args /* {
1924 		vnode_t *a_vp;
1925 		voff_t a_offset;
1926 		struct vm_page **a_m;
1927 		int *a_count;
1928 		int a_centeridx;
1929 		vm_prot_t a_access_type;
1930 		int a_advice;
1931 		int a_flags;
1932 	} */ *ap = v;
1933 	vnode_t *vp = ap->a_vp, *cvp;
1934 	struct cnode *cp = VTOC(vp);
1935 	struct lwp *l = curlwp;
1936 	kauth_cred_t cred = l->l_cred;
1937 	int error, cerror;
1938 	int waslocked;	       /* 1 if vnode lock was held on entry */
1939 	int didopen = 0;	/* 1 if we opened container file */
1940 
1941 	/*
1942 	 * Handle a case that uvm_fault doesn't quite use yet.
1943 	 * See layer_vnops.c. for inspiration.
1944 	 */
1945 	if (ap->a_flags & PGO_LOCKED) {
1946 		return EBUSY;
1947 	}
1948 
1949 	KASSERT(mutex_owned(vp->v_interlock));
1950 
1951 	/* Check for control object. */
1952 	if (IS_CTL_VP(vp)) {
1953 #ifdef CODA_VERBOSE
1954 		printf("%s: control object %p\n", __func__, vp);
1955 #endif
1956 		return(EINVAL);
1957 	}
1958 
1959 	/*
1960 	 * XXX It's really not ok to be releasing the lock we get,
1961 	 * because we could be overlapping with another call to
1962 	 * getpages and drop a lock they are relying on.  We need to
1963 	 * figure out whether getpages ever is called holding the
1964 	 * lock, and if we should serialize getpages calls by some
1965 	 * mechanism.
1966 	 */
1967 	/* XXX VOP_ISLOCKED() may not be used for lock decisions. */
1968 	waslocked = VOP_ISLOCKED(vp);
1969 
1970 	/* Get container file if not already present. */
1971 	cvp = cp->c_ovp;
1972 	if (cvp == NULL) {
1973 		/*
1974 		 * VOP_OPEN requires a locked vnode.  We must avoid
1975 		 * locking the vnode if it is already locked, and
1976 		 * leave it in the same state on exit.
1977 		 */
1978 		if (waslocked == 0) {
1979 			mutex_exit(vp->v_interlock);
1980 			cerror = vn_lock(vp, LK_EXCLUSIVE);
1981 			if (cerror) {
1982 #ifdef CODA_VERBOSE
1983 				printf("%s: can't lock vnode %p\n",
1984 				    __func__, vp);
1985 #endif
1986 				return cerror;
1987 			}
1988 #ifdef CODA_VERBOSE
1989 			printf("%s: locked vnode %p\n", __func__, vp);
1990 #endif
1991 		}
1992 
1993 		/*
1994 		 * Open file (causes upcall to venus).
1995 		 * XXX Perhaps we should not fully open the file, but
1996 		 * simply obtain a container file.
1997 		 */
1998 		/* XXX Is it ok to do this while holding the simplelock? */
1999 		cerror = VOP_OPEN(vp, FREAD, cred);
2000 
2001 		if (cerror) {
2002 #ifdef CODA_VERBOSE
2003 			printf("%s: cannot open vnode %p => %d\n", __func__,
2004 			    vp, cerror);
2005 #endif
2006 			if (waslocked == 0)
2007 				VOP_UNLOCK(vp);
2008 			return cerror;
2009 		}
2010 
2011 #ifdef CODA_VERBOSE
2012 		printf("%s: opened vnode %p\n", __func__, vp);
2013 #endif
2014 		cvp = cp->c_ovp;
2015 		didopen = 1;
2016 		if (waslocked == 0)
2017 			mutex_enter(vp->v_interlock);
2018 	}
2019 	KASSERT(cvp != NULL);
2020 
2021 	/* Munge the arg structure to refer to the container vnode. */
2022 	KASSERT(cvp->v_interlock == vp->v_interlock);
2023 	ap->a_vp = cp->c_ovp;
2024 
2025 	/* Finally, call getpages on it. */
2026 	error = VCALL(ap->a_vp, VOFFSET(vop_getpages), ap);
2027 
2028 	/* If we opened the vnode, we must close it. */
2029 	if (didopen) {
2030 		/*
2031 		 * VOP_CLOSE requires a locked vnode, but we are still
2032 		 * holding the lock (or riding a caller's lock).
2033 		 */
2034 		cerror = VOP_CLOSE(vp, FREAD, cred);
2035 #ifdef CODA_VERBOSE
2036 		if (cerror != 0)
2037 			/* XXX How should we handle this? */
2038 			printf("%s: closed vnode %p -> %d\n", __func__,
2039 			    vp, cerror);
2040 #endif
2041 
2042 		/* If we obtained a lock, drop it. */
2043 		if (waslocked == 0)
2044 			VOP_UNLOCK(vp);
2045 	}
2046 
2047 	return error;
2048 }
2049 
2050 /*
2051  * The protocol requires v_interlock to be held by the caller.
2052  */
2053 int
2054 coda_putpages(void *v)
2055 {
2056 	struct vop_putpages_args /* {
2057 		vnode_t *a_vp;
2058 		voff_t a_offlo;
2059 		voff_t a_offhi;
2060 		int a_flags;
2061 	} */ *ap = v;
2062 	vnode_t *vp = ap->a_vp, *cvp;
2063 	struct cnode *cp = VTOC(vp);
2064 	int error;
2065 
2066 	KASSERT(mutex_owned(vp->v_interlock));
2067 
2068 	/* Check for control object. */
2069 	if (IS_CTL_VP(vp)) {
2070 		mutex_exit(vp->v_interlock);
2071 #ifdef CODA_VERBOSE
2072 		printf("%s: control object %p\n", __func__, vp);
2073 #endif
2074 		return(EINVAL);
2075 	}
2076 
2077 	/*
2078 	 * If container object is not present, then there are no pages
2079 	 * to put; just return without error.  This happens all the
2080 	 * time, apparently during discard of a closed vnode (which
2081 	 * trivially can't have dirty pages).
2082 	 */
2083 	cvp = cp->c_ovp;
2084 	if (cvp == NULL) {
2085 		mutex_exit(vp->v_interlock);
2086 		return 0;
2087 	}
2088 
2089 	/* Munge the arg structure to refer to the container vnode. */
2090 	KASSERT(cvp->v_interlock == vp->v_interlock);
2091 	ap->a_vp = cvp;
2092 
2093 	/* Finally, call putpages on it. */
2094 	error = VCALL(ap->a_vp, VOFFSET(vop_putpages), ap);
2095 
2096 	return error;
2097 }
2098