]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/coda/coda_vnops.c
Sync with sys/i386/isa/isa.c revision 1.114.
[FreeBSD/FreeBSD.git] / sys / coda / coda_vnops.c
1 /*
2  * 
3  *             Coda: an Experimental Distributed File System
4  *                              Release 3.1
5  * 
6  *           Copyright (c) 1987-1998 Carnegie Mellon University
7  *                          All Rights Reserved
8  * 
9  * Permission  to  use, copy, modify and distribute this software and its
10  * documentation is hereby granted,  provided  that  both  the  copyright
11  * notice  and  this  permission  notice  appear  in  all  copies  of the
12  * software, derivative works or  modified  versions,  and  any  portions
13  * thereof, and that both notices appear in supporting documentation, and
14  * that credit is given to Carnegie Mellon University  in  all  documents
15  * and publicity pertaining to direct or indirect use of this code or its
16  * derivatives.
17  * 
18  * CODA IS AN EXPERIMENTAL SOFTWARE SYSTEM AND IS  KNOWN  TO  HAVE  BUGS,
19  * SOME  OF  WHICH MAY HAVE SERIOUS CONSEQUENCES.  CARNEGIE MELLON ALLOWS
20  * FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.   CARNEGIE  MELLON
21  * DISCLAIMS  ANY  LIABILITY  OF  ANY  KIND  FOR  ANY  DAMAGES WHATSOEVER
22  * RESULTING DIRECTLY OR INDIRECTLY FROM THE USE OF THIS SOFTWARE  OR  OF
23  * ANY DERIVATIVE WORK.
24  * 
25  * Carnegie  Mellon  encourages  users  of  this  software  to return any
26  * improvements or extensions that  they  make,  and  to  grant  Carnegie
27  * Mellon the rights to redistribute these changes without encumbrance.
28  * 
29  *      @(#) src/sys/coda/coda_vnops.c,v 1.1.1.1 1998/08/29 21:14:52 rvb Exp $
30  *  $Id: coda_vnops.c,v 1.5 1998/09/25 17:38:32 rvb Exp $
31  * 
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 /*
49  * HISTORY
50  * $Log: coda_vnops.c,v $
51  * Revision 1.5  1998/09/25 17:38:32  rvb
52  * Put "stray" printouts under DIAGNOSTIC.  Make everything build
53  * with DEBUG on.  Add support for lkm.  (The macro's don't work
54  * for me; for a good chuckle look at the end of coda_fbsd.c.)
55  *
56  * Revision 1.4  1998/09/13 13:57:59  rvb
57  * Finish conversion of cfs -> coda
58  *
59  * Revision 1.3  1998/09/11 18:50:17  rvb
60  * All the references to cfs, in symbols, structs, and strings
61  * have been changed to coda.  (Same for CFS.)
62  *
63  * Revision 1.2  1998/09/02 19:09:53  rvb
64  * Pass2 complete
65  *
66  * Revision 1.1.1.1  1998/08/29 21:14:52  rvb
67  * Very Preliminary Coda
68  *
69  * Revision 1.12  1998/08/28 18:28:00  rvb
70  * NetBSD -current is stricter!
71  *
72  * Revision 1.11  1998/08/28 18:12:23  rvb
73  * Now it also works on FreeBSD -current.  This code will be
74  * committed to the FreeBSD -current and NetBSD -current
75  * trees.  It will then be tailored to the particular platform
76  * by flushing conditional code.
77  *
78  * Revision 1.10  1998/08/18 17:05:21  rvb
79  * Don't use __RCSID now
80  *
81  * Revision 1.9  1998/08/18 16:31:46  rvb
82  * Sync the code for NetBSD -current; test on 1.3 later
83  *
84  * Revision 1.8  98/02/24  22:22:50  rvb
85  * Fixes up mainly to flush iopen and friends
86  * 
87  * Revision 1.7  98/01/31  20:53:15  rvb
88  * First version that works on FreeBSD 2.2.5
89  * 
90  * Revision 1.6  98/01/23  11:53:47  rvb
91  * Bring RVB_CODA1_1 to HEAD
92  * 
93  * Revision 1.5.2.8  98/01/23  11:21:11  rvb
94  * Sync with 2.2.5
95  * 
96  * Revision 1.5.2.7  97/12/19  14:26:08  rvb
97  * session id
98  * 
99  * Revision 1.5.2.6  97/12/16  22:01:34  rvb
100  * Oops add cfs_subr.h cfs_venus.h; sync with peter
101  * 
102  * Revision 1.5.2.5  97/12/16  12:40:14  rvb
103  * Sync with 1.3
104  * 
105  * Revision 1.5.2.4  97/12/10  14:08:31  rvb
106  * Fix O_ flags; check result in coda_call
107  * 
108  * Revision 1.5.2.3  97/12/10  11:40:27  rvb
109  * No more ody
110  * 
111  * Revision 1.5.2.2  97/12/09  16:07:15  rvb
112  * Sync with vfs/include/coda.h
113  * 
114  * Revision 1.5.2.1  97/12/06  17:41:25  rvb
115  * Sync with peters coda.h
116  * 
117  * Revision 1.5  97/12/05  10:39:23  rvb
118  * Read CHANGES
119  * 
120  * Revision 1.4.14.10  97/11/25  08:08:48  rvb
121  * cfs_venus ... done; until cred/vattr change
122  * 
123  * Revision 1.4.14.9  97/11/24  15:44:48  rvb
124  * Final cfs_venus.c w/o macros, but one locking bug
125  * 
126  * Revision 1.4.14.8  97/11/21  11:28:04  rvb
127  * cfs_venus.c is done: first pass
128  * 
129  * Revision 1.4.14.7  97/11/20  11:46:51  rvb
130  * Capture current cfs_venus
131  * 
132  * Revision 1.4.14.6  97/11/18  10:27:19  rvb
133  * cfs_nbsd.c is DEAD!!!; integrated into cfs_vf/vnops.c
134  * cfs_nb_foo and cfs_foo are joined
135  * 
136  * Revision 1.4.14.5  97/11/13  22:03:03  rvb
137  * pass2 cfs_NetBSD.h mt
138  * 
139  * Revision 1.4.14.4  97/11/12  12:09:42  rvb
140  * reorg pass1
141  * 
142  * Revision 1.4.14.3  97/11/06  21:03:28  rvb
143  * don't include headers in headers
144  * 
145  * Revision 1.4.14.2  97/10/29  16:06:30  rvb
146  * Kill DYING
147  * 
148  * Revision 1.4.14.1  1997/10/28 23:10:18  rvb
149  * >64Meg; venus can be killed!
150  *
151  * Revision 1.4  1997/02/20 13:54:50  lily
152  * check for NULL return from coda_nc_lookup before CTOV
153  *
154  * Revision 1.3  1996/12/12 22:11:02  bnoble
155  * Fixed the "downcall invokes venus operation" deadlock in all known cases.
156  * There may be more
157  *
158  * Revision 1.2  1996/01/02 16:57:07  bnoble
159  * Added support for Coda MiniCache and raw inode calls (final commit)
160  *
161  * Revision 1.1.2.1  1995/12/20 01:57:34  bnoble
162  * Added CODA-specific files
163  *
164  * Revision 3.1.1.1  1995/03/04  19:08:06  bnoble
165  * Branch for NetBSD port revisions
166  *
167  * Revision 3.1  1995/03/04  19:08:04  bnoble
168  * Bump to major revision 3 to prepare for NetBSD port
169  *
170  * Revision 2.6  1995/02/17  16:25:26  dcs
171  * These versions represent several changes:
172  * 1. Allow venus to restart even if outstanding references exist.
173  * 2. Have only one ctlvp per client, as opposed to one per mounted cfs device.d
174  * 3. Allow ody_expand to return many members, not just one.
175  *
176  * Revision 2.5  94/11/09  20:29:27  dcs
177  * Small bug in remove dealing with hard links and link counts was fixed.
178  * 
179  * Revision 2.4  94/10/14  09:58:42  dcs
180  * Made changes 'cause sun4s have braindead compilers
181  * 
182  * Revision 2.3  94/10/12  16:46:37  dcs
183  * Cleaned kernel/venus interface by removing XDR junk, plus
184  * so cleanup to allow this code to be more easily ported.
185  * 
186  * Revision 2.2  94/09/20  14:12:41  dcs
187  * Fixed bug in rename when moving a directory.
188  * 
189  * Revision 2.1  94/07/21  16:25:22  satya
190  * Conversion to C++ 3.0; start of Coda Release 2.0
191  * 
192  * Revision 1.4  93/12/17  01:38:01  luqi
193  * Changes made for kernel to pass process info to Venus:
194  * 
195  * (1) in file cfs.h
196  * add process id and process group id in most of the cfs argument types.
197  * 
198  * (2) in file cfs_vnodeops.c
199  * add process info passing in most of the cfs vnode operations.
200  * 
201  * (3) in file cfs_xdr.c
202  * expand xdr routines according changes in (1). 
203  * add variable pass_process_info to allow venus for kernel version checking.
204  * 
205  * Revision 1.3  93/05/28  16:24:33  bnoble
206  * *** empty log message ***
207  * 
208  * Revision 1.2  92/10/27  17:58:25  lily
209  * merge kernel/latest and alpha/src/cfs
210  * 
211  * Revision 2.4  92/09/30  14:16:37  mja
212  *      Redid buffer allocation so that it does kmem_{alloc,free} for all
213  *      architectures.  Zone allocation, previously used on the 386, caused
214  *      panics if it was invoked repeatedly.  Stack allocation, previously
215  *      used on all other architectures, tickled some Mach bug that appeared
216  *      with large stack frames.
217  *      [91/02/09            jjk]
218  * 
219  *      Added contributors blurb.
220  *      [90/12/13            jjk]
221  * 
222  * Revision 2.3  90/07/26  15:50:09  mrt
223  *          Fixed fix to rename to remove .. from moved directories.
224  *      [90/06/28            dcs]
225  * 
226  * Revision 1.7  90/06/28  16:24:25  dcs
227  * Fixed bug with moving directories, we weren't flushing .. for the moved directory.
228  * 
229  * Revision 1.6  90/05/31  17:01:47  dcs
230  * Prepare for merge with facilities kernel.
231  * 
232  * 
233  */
234
235 #include <sys/param.h>
236 #include <sys/systm.h>
237 #include <sys/kernel.h>
238 #include <sys/proc.h>
239 #include <sys/malloc.h>
240 #include <sys/mount.h>
241 #include <sys/errno.h>
242 #include <sys/acct.h>
243 #include <sys/file.h>
244 #include <sys/fcntl.h>
245 #include <sys/uio.h>
246 #include <sys/namei.h>
247 #include <sys/ioccom.h>
248 #include <sys/select.h>
249
250 #include <vm/vm.h>
251 #include <vm/vm_object.h>
252 #include <vm/vm_extern.h>
253 #include <vm/vm_zone.h>
254
255 #include <coda/coda.h>
256 #include <coda/cnode.h>
257 #include <coda/coda_vnops.h>
258 #include <coda/coda_venus.h>
259 #include <coda/coda_opstats.h>
260 #include <coda/coda_subr.h>
261 #include <coda/coda_namecache.h>
262 #include <coda/coda_pioctl.h>
263
264 /* 
265  * These flags select various performance enhancements.
266  */
267 int coda_attr_cache  = 1;       /* Set to cache attributes in the kernel */
268 int coda_symlink_cache = 1;     /* Set to cache symbolic link information */
269 int coda_access_cache = 1;      /* Set to handle some access checks directly */
270
271 /* structure to keep track of vfs calls */
272
273 struct coda_op_stats coda_vnodeopstats[CODA_VNODEOPS_SIZE];
274
275 #define MARK_ENTRY(op) (coda_vnodeopstats[op].entries++)
276 #define MARK_INT_SAT(op) (coda_vnodeopstats[op].sat_intrn++)
277 #define MARK_INT_FAIL(op) (coda_vnodeopstats[op].unsat_intrn++)
278 #define MARK_INT_GEN(op) (coda_vnodeopstats[op].gen_intrn++)
279
280 /* What we are delaying for in printf */
281 int coda_printf_delay = 0;  /* in microseconds */
282 int coda_vnop_print_entry = 0;
283 static int coda_lockdebug = 0;
284
285 /* Definition of the vfs operation vector */
286
287 /*
288  * Some NetBSD details:
289  * 
290  *   coda_start is called at the end of the mount syscall.
291  *   coda_init is called at boot time.
292  */
293
294 #define ENTRY  if(coda_vnop_print_entry) myprintf(("Entered %s\n",__FUNCTION__))
295
296 /* Definition of the vnode operation vector */
297
298 struct vnodeopv_entry_desc coda_vnodeop_entries[] = {
299     { &vop_default_desc, coda_vop_error },
300     { &vop_lookup_desc, coda_lookup },          /* lookup */
301     { &vop_create_desc, coda_create },          /* create */
302     { &vop_mknod_desc, coda_vop_error },        /* mknod */
303     { &vop_open_desc, coda_open },              /* open */
304     { &vop_close_desc, coda_close },            /* close */
305     { &vop_access_desc, coda_access },          /* access */
306     { &vop_getattr_desc, coda_getattr },        /* getattr */
307     { &vop_setattr_desc, coda_setattr },        /* setattr */
308     { &vop_read_desc, coda_read },              /* read */
309     { &vop_write_desc, coda_write },            /* write */
310     { &vop_ioctl_desc, coda_ioctl },            /* ioctl */
311     { &vop_mmap_desc, coda_vop_error },         /* mmap */
312     { &vop_fsync_desc, coda_fsync },            /* fsync */
313     { &vop_remove_desc, coda_remove },          /* remove */
314     { &vop_link_desc, coda_link },              /* link */
315     { &vop_rename_desc, coda_rename },          /* rename */
316     { &vop_mkdir_desc, coda_mkdir },            /* mkdir */
317     { &vop_rmdir_desc, coda_rmdir },            /* rmdir */
318     { &vop_symlink_desc, coda_symlink },        /* symlink */
319     { &vop_readdir_desc, coda_readdir },        /* readdir */
320     { &vop_readlink_desc, coda_readlink },      /* readlink */
321     { &vop_abortop_desc, coda_abortop },        /* abortop */
322     { &vop_inactive_desc, coda_inactive },      /* inactive */
323     { &vop_reclaim_desc, coda_reclaim },        /* reclaim */
324     { &vop_lock_desc, coda_lock },              /* lock */
325     { &vop_unlock_desc, coda_unlock },          /* unlock */
326     { &vop_bmap_desc, coda_bmap },              /* bmap */
327     { &vop_strategy_desc, coda_strategy },      /* strategy */
328     { &vop_print_desc, coda_vop_error },        /* print */
329     { &vop_islocked_desc, coda_islocked },      /* islocked */
330     { &vop_pathconf_desc, coda_vop_error },     /* pathconf */
331     { &vop_advlock_desc, coda_vop_nop },        /* advlock */
332     { &vop_bwrite_desc, coda_vop_error },       /* bwrite */
333     { &vop_lease_desc, coda_vop_nop },          /* lease */
334     { &vop_poll_desc, (vop_t *) vop_stdpoll },
335     { &vop_getpages_desc, coda_fbsd_getpages }, /* pager intf.*/
336     { &vop_putpages_desc, coda_fbsd_putpages }, /* pager intf.*/
337
338 #if     0
339
340     we need to define these someday
341 #define UFS_BLKATOFF(aa, bb, cc, dd) VFSTOUFS((aa)->v_mount)->um_blkatoff(aa, bb, cc, dd)
342 #define UFS_VALLOC(aa, bb, cc, dd) VFSTOUFS((aa)->v_mount)->um_valloc(aa, bb, cc, dd)
343 #define UFS_VFREE(aa, bb, cc) VFSTOUFS((aa)->v_mount)->um_vfree(aa, bb, cc)
344 #define UFS_TRUNCATE(aa, bb, cc, dd, ee) VFSTOUFS((aa)->v_mount)->um_truncate(aa, bb, cc, dd, ee)
345 #define UFS_UPDATE(aa, bb, cc, dd) VFSTOUFS((aa)->v_mount)->um_update(aa, bb, cc, dd)
346
347     missing
348     { &vop_reallocblks_desc,    (vop_t *) ufs_missingop },
349     { &vop_cachedlookup_desc,   (vop_t *) ufs_lookup },
350     { &vop_whiteout_desc,       (vop_t *) ufs_whiteout },
351 #endif
352     { (struct vnodeop_desc*)NULL, (int(*)(void *))NULL }
353 };
354
355 static struct vnodeopv_desc coda_vnodeop_opv_desc =
356                 { &coda_vnodeop_p, coda_vnodeop_entries };
357
358 VNODEOP_SET(coda_vnodeop_opv_desc);
359
360 /* A generic panic: we were called with something we didn't define yet */
361 int
362 coda_vop_error(void *anon) {
363     struct vnodeop_desc **desc = (struct vnodeop_desc **)anon;
364
365     myprintf(("Vnode operation %s called, but not defined\n",
366               (*desc)->vdesc_name));
367     panic("coda_vop_error");
368     return 0;
369 }
370
371 /* A generic do-nothing.  For lease_check, advlock */
372 int
373 coda_vop_nop(void *anon) {
374     struct vnodeop_desc **desc = (struct vnodeop_desc **)anon;
375
376     if (codadebug) {
377         myprintf(("Vnode operation %s called, but unsupported\n",
378                   (*desc)->vdesc_name));
379     } 
380    return (0);
381 }
382
383 int
384 coda_vnodeopstats_init(void)
385 {
386         register int i;
387         
388         for(i=0;i<CODA_VNODEOPS_SIZE;i++) {
389                 coda_vnodeopstats[i].opcode = i;
390                 coda_vnodeopstats[i].entries = 0;
391                 coda_vnodeopstats[i].sat_intrn = 0;
392                 coda_vnodeopstats[i].unsat_intrn = 0;
393                 coda_vnodeopstats[i].gen_intrn = 0;
394         }
395         return 0;
396 }
397                 
398 /* 
399  * coda_open calls Venus to return the device, inode pair of the cache
400  * file holding the data. Using iget, coda_open finds the vnode of the
401  * cache file, and then opens it.
402  */
403 int
404 coda_open(v)
405     void *v;
406 {
407     /* 
408      * NetBSD can pass the O_EXCL flag in mode, even though the check
409      * has already happened.  Venus defensively assumes that if open
410      * is passed the EXCL, it must be a bug.  We strip the flag here.
411      */
412 /* true args */
413     struct vop_open_args *ap = v;
414     register struct vnode **vpp = &(ap->a_vp);
415     struct cnode *cp = VTOC(*vpp);
416     int flag = ap->a_mode & (~O_EXCL);
417     struct ucred *cred = ap->a_cred;
418     struct proc *p = ap->a_p;
419 /* locals */
420     int error;
421     struct vnode *vp;
422     dev_t dev;
423     ino_t inode;
424
425     MARK_ENTRY(CODA_OPEN_STATS);
426
427     /* Check for open of control file. */
428     if (IS_CTL_VP(*vpp)) {
429         /* XXX */
430         /* if (WRITEABLE(flag)) */ 
431         if (flag & (FWRITE | O_TRUNC | O_CREAT | O_EXCL)) {
432             MARK_INT_FAIL(CODA_OPEN_STATS);
433             return(EACCES);
434         }
435         MARK_INT_SAT(CODA_OPEN_STATS);
436         return(0);
437     }
438
439     error = venus_open(vtomi((*vpp)), &cp->c_fid, flag, cred, p, &dev, &inode);
440     if (error)
441         return (error);
442     if (!error) {
443         CODADEBUG( CODA_OPEN,myprintf(("open: dev %d inode %d result %d\n",
444                                   dev, inode, error)); )
445     }
446
447     /* Translate the <device, inode> pair for the cache file into
448        an inode pointer. */
449     error = coda_grab_vnode(dev, inode, &vp);
450     if (error)
451         return (error);
452
453     /* We get the vnode back locked.  Needs unlocked */
454     VOP_UNLOCK(vp, 0, p);
455     /* Keep a reference until the close comes in. */
456     vref(*vpp);                
457
458     /* Save the vnode pointer for the cache file. */
459     if (cp->c_ovp == NULL) {
460         cp->c_ovp = vp;
461     } else {
462         if (cp->c_ovp != vp)
463             panic("coda_open:  cp->c_ovp != ITOV(ip)");
464     }
465     cp->c_ocount++;
466
467     /* Flush the attribute cached if writing the file. */
468     if (flag & FWRITE) {
469         cp->c_owrite++;
470         cp->c_flags &= ~C_VATTR;
471     }
472
473     /* Save the <device, inode> pair for the cache file to speed
474        up subsequent page_read's. */
475     cp->c_device = dev;
476     cp->c_inode = inode;
477
478     /* Open the cache file. */
479     error = VOP_OPEN(vp, flag, cred, p); 
480     if (error) {
481         printf("coda_open: VOP_OPEN on container failed %d\n", error);
482         return (error);
483     }
484 /* grab (above) does this when it calls newvnode unless it's in the cache*/
485     if (vp->v_type == VREG) {
486         error = vfs_object_create(vp, p, cred, 1);
487         if (error != 0) {
488             printf("coda_open: vfs_object_create() returns %d\n", error);
489             vput(vp);
490         }
491     }
492
493     return(error);
494 }
495
496 /*
497  * Close the cache file used for I/O and notify Venus.
498  */
499 int
500 coda_close(v)
501     void *v;
502 {
503 /* true args */
504     struct vop_close_args *ap = v;
505     struct vnode *vp = ap->a_vp;
506     struct cnode *cp = VTOC(vp);
507     int flag = ap->a_fflag;
508     struct ucred *cred = ap->a_cred;
509     struct proc *p = ap->a_p;
510 /* locals */
511     int error;
512
513     MARK_ENTRY(CODA_CLOSE_STATS);
514
515     /* Check for close of control file. */
516     if (IS_CTL_VP(vp)) {
517         MARK_INT_SAT(CODA_CLOSE_STATS);
518         return(0);
519     }
520
521     if (IS_UNMOUNTING(cp)) {
522         if (cp->c_ovp) {
523 #ifdef  CODA_VERBOSE
524             printf("coda_close: destroying container ref %d, ufs vp %p of vp %p/cp %p\n",
525                     vp->v_usecount, cp->c_ovp, vp, cp);
526 #endif
527             vgone(cp->c_ovp);
528         } else {
529 #ifdef  CODA_VERBOSE
530             printf("coda_close: NO container vp %p/cp %p\n", vp, cp);
531 #endif
532         }
533         return ENODEV;
534     } else {
535         VOP_CLOSE(cp->c_ovp, flag, cred, p); /* Do errors matter here? */
536         vrele(cp->c_ovp);
537     }
538
539     if (--cp->c_ocount == 0)
540         cp->c_ovp = NULL;
541
542     if (flag & FWRITE)                    /* file was opened for write */
543         --cp->c_owrite;
544
545     error = venus_close(vtomi(vp), &cp->c_fid, flag, cred, p);
546     vrele(CTOV(cp));
547
548     CODADEBUG(CODA_CLOSE, myprintf(("close: result %d\n",error)); )
549     return(error);
550 }
551
552 int
553 coda_read(v)
554     void *v;
555 {
556     struct vop_read_args *ap = v;
557
558     ENTRY;
559     return(coda_rdwr(ap->a_vp, ap->a_uio, UIO_READ,
560                     ap->a_ioflag, ap->a_cred, ap->a_uio->uio_procp));
561 }
562
563 int
564 coda_write(v)
565     void *v;
566 {
567     struct vop_write_args *ap = v;
568
569     ENTRY;
570     return(coda_rdwr(ap->a_vp, ap->a_uio, UIO_WRITE,
571                     ap->a_ioflag, ap->a_cred, ap->a_uio->uio_procp));
572 }
573
574 int
575 coda_rdwr(vp, uiop, rw, ioflag, cred, p)
576     struct vnode *vp;
577     struct uio *uiop;
578     enum uio_rw rw;
579     int ioflag;
580     struct ucred *cred;
581     struct proc *p;
582
583 /* upcall decl */
584   /* NOTE: container file operation!!! */
585 /* locals */
586     struct cnode *cp = VTOC(vp);
587     struct vnode *cfvp = cp->c_ovp;
588     int igot_internally = 0;
589     int opened_internally = 0;
590     int error = 0;
591
592     MARK_ENTRY(CODA_RDWR_STATS);
593
594     CODADEBUG(CODA_RDWR, myprintf(("coda_rdwr(%d, %p, %d, %qd, %d)\n", rw, 
595                               uiop->uio_iov->iov_base, uiop->uio_resid, 
596                               uiop->uio_offset, uiop->uio_segflg)); )
597         
598     /* Check for rdwr of control object. */
599     if (IS_CTL_VP(vp)) {
600         MARK_INT_FAIL(CODA_RDWR_STATS);
601         return(EINVAL);
602     }
603
604     /* 
605      * If file is not already open this must be a page
606      * {read,write} request.  Iget the cache file's inode
607      * pointer if we still have its <device, inode> pair.
608      * Otherwise, we must do an internal open to derive the
609      * pair. 
610      */
611     if (cfvp == NULL) {
612         /* 
613          * If we're dumping core, do the internal open. Otherwise
614          * venus won't have the correct size of the core when
615          * it's completely written.
616          */
617         if (cp->c_inode != 0 && !(p && (p->p_acflag & ACORE))) { 
618             igot_internally = 1;
619             error = coda_grab_vnode(cp->c_device, cp->c_inode, &cfvp);
620             if (error) {
621                 MARK_INT_FAIL(CODA_RDWR_STATS);
622                 return(error);
623             }
624             /* 
625              * We get the vnode back locked in both Mach and
626              * NetBSD.  Needs unlocked 
627              */
628             VOP_UNLOCK(cfvp, 0, p);
629         }
630         else {
631             opened_internally = 1;
632             MARK_INT_GEN(CODA_OPEN_STATS);
633             error = VOP_OPEN(vp, (rw == UIO_READ ? FREAD : FWRITE), 
634                              cred, p);
635 printf("coda_rdwr: Internally Opening %p\n", vp);
636             if (error) {
637                 printf("coda_rdwr: VOP_OPEN on container failed %d\n", error);
638                 return (error);
639             }
640             if (vp->v_type == VREG) {
641                 error = vfs_object_create(vp, p, cred, 1);
642                 if (error != 0) {
643                     printf("coda_rdwr: vfs_object_create() returns %d\n", error);
644                     vput(vp);
645                 }
646             }
647             if (error) {
648                 MARK_INT_FAIL(CODA_RDWR_STATS);
649                 return(error);
650             }
651             cfvp = cp->c_ovp;
652         }
653     }
654
655     /* Have UFS handle the call. */
656     CODADEBUG(CODA_RDWR, myprintf(("indirect rdwr: fid = (%lx.%lx.%lx), refcnt = %d\n",
657                               cp->c_fid.Volume, cp->c_fid.Vnode, 
658                               cp->c_fid.Unique, CTOV(cp)->v_usecount)); )
659
660
661     if (rw == UIO_READ) {
662         error = VOP_READ(cfvp, uiop, ioflag, cred);
663     } else {
664         error = VOP_WRITE(cfvp, uiop, ioflag, cred);
665         /* ufs_write updates the vnode_pager_setsize for the vnode/object */
666
667         {   struct vattr attr;
668
669             if (VOP_GETATTR(cfvp, &attr, cred, p) == 0) {
670                 vnode_pager_setsize(vp, attr.va_size);
671             }
672         }
673     }
674
675     if (error)
676         MARK_INT_FAIL(CODA_RDWR_STATS);
677     else
678         MARK_INT_SAT(CODA_RDWR_STATS);
679
680     /* Do an internal close if necessary. */
681     if (opened_internally) {
682         MARK_INT_GEN(CODA_CLOSE_STATS);
683         (void)VOP_CLOSE(vp, (rw == UIO_READ ? FREAD : FWRITE), cred, p);
684     }
685
686     /* Invalidate cached attributes if writing. */
687     if (rw == UIO_WRITE)
688         cp->c_flags &= ~C_VATTR;
689     return(error);
690 }
691
692 int
693 coda_ioctl(v)
694     void *v;
695 {
696 /* true args */
697     struct vop_ioctl_args *ap = v;
698     struct vnode *vp = ap->a_vp;
699     int com = ap->a_command;
700     caddr_t data = ap->a_data;
701     int flag = ap->a_fflag;
702     struct ucred *cred = ap->a_cred;
703     struct proc  *p = ap->a_p;
704 /* locals */
705     int error;
706     struct vnode *tvp;
707     struct nameidata ndp;
708     struct PioctlData *iap = (struct PioctlData *)data;
709
710     MARK_ENTRY(CODA_IOCTL_STATS);
711
712     CODADEBUG(CODA_IOCTL, myprintf(("in coda_ioctl on %s\n", iap->path));)
713         
714     /* Don't check for operation on a dying object, for ctlvp it
715        shouldn't matter */
716         
717     /* Must be control object to succeed. */
718     if (!IS_CTL_VP(vp)) {
719         MARK_INT_FAIL(CODA_IOCTL_STATS);
720         CODADEBUG(CODA_IOCTL, myprintf(("coda_ioctl error: vp != ctlvp"));)
721             return (EOPNOTSUPP);
722     }
723     /* Look up the pathname. */
724
725     /* Should we use the name cache here? It would get it from
726        lookupname sooner or later anyway, right? */
727
728     NDINIT(&ndp, LOOKUP, (iap->follow ? FOLLOW : NOFOLLOW), UIO_USERSPACE, ((caddr_t)iap->path), p);
729     error = namei(&ndp);
730     tvp = ndp.ni_vp;
731
732     if (error) {
733         MARK_INT_FAIL(CODA_IOCTL_STATS);
734         CODADEBUG(CODA_IOCTL, myprintf(("coda_ioctl error: lookup returns %d\n",
735                                    error));)
736         return(error);
737     }
738
739     /* 
740      * Make sure this is a coda style cnode, but it may be a
741      * different vfsp 
742      */
743     /* XXX: this totally violates the comment about vtagtype in vnode.h */
744     if (tvp->v_tag != VT_CODA) {
745         vrele(tvp);
746         MARK_INT_FAIL(CODA_IOCTL_STATS);
747         CODADEBUG(CODA_IOCTL, 
748                  myprintf(("coda_ioctl error: %s not a coda object\n", 
749                         iap->path));)
750         return(EINVAL);
751     }
752
753     if (iap->vi.in_size > VC_MAXDATASIZE) {
754         vrele(tvp);
755         return(EINVAL);
756     }
757     error = venus_ioctl(vtomi(tvp), &((VTOC(tvp))->c_fid), com, flag, data, cred, p);
758
759     if (error)
760         MARK_INT_FAIL(CODA_IOCTL_STATS);
761     else
762         CODADEBUG(CODA_IOCTL, myprintf(("Ioctl returns %d \n", error)); )
763
764     vrele(tvp);
765     return(error);
766 }
767
768 /*
769  * To reduce the cost of a user-level venus;we cache attributes in
770  * the kernel.  Each cnode has storage allocated for an attribute. If
771  * c_vattr is valid, return a reference to it. Otherwise, get the
772  * attributes from venus and store them in the cnode.  There is some
773  * question if this method is a security leak. But I think that in
774  * order to make this call, the user must have done a lookup and
775  * opened the file, and therefore should already have access.  
776  */
777 int
778 coda_getattr(v)
779     void *v;
780 {
781 /* true args */
782     struct vop_getattr_args *ap = v;
783     struct vnode *vp = ap->a_vp;
784     struct cnode *cp = VTOC(vp);
785     struct vattr *vap = ap->a_vap;
786     struct ucred *cred = ap->a_cred;
787     struct proc *p = ap->a_p;
788 /* locals */
789     int error;
790
791     MARK_ENTRY(CODA_GETATTR_STATS);
792
793     if (IS_UNMOUNTING(cp))
794         return ENODEV;
795
796     /* Check for getattr of control object. */
797     if (IS_CTL_VP(vp)) {
798         MARK_INT_FAIL(CODA_GETATTR_STATS);
799         return(ENOENT);
800     }
801
802     /* Check to see if the attributes have already been cached */
803     if (VALID_VATTR(cp)) { 
804         CODADEBUG(CODA_GETATTR, { myprintf(("attr cache hit: (%lx.%lx.%lx)\n",
805                                        cp->c_fid.Volume,
806                                        cp->c_fid.Vnode,
807                                        cp->c_fid.Unique));});
808         CODADEBUG(CODA_GETATTR, if (!(codadebug & ~CODA_GETATTR))
809                  print_vattr(&cp->c_vattr); );
810         
811         *vap = cp->c_vattr;
812         MARK_INT_SAT(CODA_GETATTR_STATS);
813         return(0);
814     }
815
816     error = venus_getattr(vtomi(vp), &cp->c_fid, cred, p, vap);
817
818     if (!error) {
819         CODADEBUG(CODA_GETATTR, myprintf(("getattr miss (%lx.%lx.%lx): result %d\n",
820                                      cp->c_fid.Volume,
821                                      cp->c_fid.Vnode,
822                                      cp->c_fid.Unique,
823                                      error)); )
824             
825         CODADEBUG(CODA_GETATTR, if (!(codadebug & ~CODA_GETATTR))
826                  print_vattr(vap);      );
827         
828     {   int size = vap->va_size;
829         struct vnode *convp = cp->c_ovp;
830         if (convp != (struct vnode *)0) {
831             vnode_pager_setsize(convp, size);
832         }
833     }
834         /* If not open for write, store attributes in cnode */   
835         if ((cp->c_owrite == 0) && (coda_attr_cache)) {  
836             cp->c_vattr = *vap;
837             cp->c_flags |= C_VATTR; 
838         }
839         
840     }
841     return(error);
842 }
843
844 int
845 coda_setattr(v)
846     void *v;
847 {
848 /* true args */
849     struct vop_setattr_args *ap = v;
850     register struct vnode *vp = ap->a_vp;
851     struct cnode *cp = VTOC(vp);
852     register struct vattr *vap = ap->a_vap;
853     struct ucred *cred = ap->a_cred;
854     struct proc *p = ap->a_p;
855 /* locals */
856     int error;
857
858     MARK_ENTRY(CODA_SETATTR_STATS);
859
860     /* Check for setattr of control object. */
861     if (IS_CTL_VP(vp)) {
862         MARK_INT_FAIL(CODA_SETATTR_STATS);
863         return(ENOENT);
864     }
865
866     if (codadebug & CODADBGMSK(CODA_SETATTR)) {
867         print_vattr(vap);
868     }
869     error = venus_setattr(vtomi(vp), &cp->c_fid, vap, cred, p);
870
871     if (!error)
872         cp->c_flags &= ~C_VATTR;
873
874     {   int size = vap->va_size;
875         struct vnode *convp = cp->c_ovp;
876         if (size != VNOVAL && convp != (struct vnode *)0) {
877             vnode_pager_setsize(convp, size);
878         }
879     }
880     CODADEBUG(CODA_SETATTR,     myprintf(("setattr %d\n", error)); )
881     return(error);
882 }
883
884 int
885 coda_access(v)
886     void *v;
887 {
888 /* true args */
889     struct vop_access_args *ap = v;
890     struct vnode *vp = ap->a_vp;
891     struct cnode *cp = VTOC(vp);
892     int mode = ap->a_mode;
893     struct ucred *cred = ap->a_cred;
894     struct proc *p = ap->a_p;
895 /* locals */
896     int error;
897
898     MARK_ENTRY(CODA_ACCESS_STATS);
899
900     /* Check for access of control object.  Only read access is
901        allowed on it. */
902     if (IS_CTL_VP(vp)) {
903         /* bogus hack - all will be marked as successes */
904         MARK_INT_SAT(CODA_ACCESS_STATS);
905         return(((mode & VREAD) && !(mode & (VWRITE | VEXEC))) 
906                ? 0 : EACCES);
907     }
908
909     /*
910      * if the file is a directory, and we are checking exec (eg lookup) 
911      * access, and the file is in the namecache, then the user must have 
912      * lookup access to it.
913      */
914     if (coda_access_cache) {
915         if ((vp->v_type == VDIR) && (mode & VEXEC)) {
916             if (coda_nc_lookup(cp, ".", 1, cred)) {
917                 MARK_INT_SAT(CODA_ACCESS_STATS);
918                 return(0);                     /* it was in the cache */
919             }
920         }
921     }
922
923     error = venus_access(vtomi(vp), &cp->c_fid, mode, cred, p);
924
925     return(error);
926 }
927
928 /*
929  * CODA abort op, called after namei() when a CREATE/DELETE isn't actually
930  * done. If a buffer has been saved in anticipation of a coda_create or
931  * a coda_remove, delete it.
932  */
933 /* ARGSUSED */
934 int
935 coda_abortop(v)
936     void *v;
937 {
938 /* true args */
939     struct vop_abortop_args /* {
940         struct vnode *a_dvp;
941         struct componentname *a_cnp;
942     } */ *ap = v;
943 /* upcall decl */
944 /* locals */
945
946     if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
947         zfree(namei_zone, ap->a_cnp->cn_pnbuf);
948     return (0);
949 }
950
951 int
952 coda_readlink(v)
953     void *v;
954 {
955 /* true args */
956     struct vop_readlink_args *ap = v;
957     struct vnode *vp = ap->a_vp;
958     struct cnode *cp = VTOC(vp);
959     struct uio *uiop = ap->a_uio;
960     struct ucred *cred = ap->a_cred;
961     struct proc *p = ap->a_uio->uio_procp;
962 /* locals */
963     int error;
964     char *str;
965     int len;
966
967     MARK_ENTRY(CODA_READLINK_STATS);
968
969     /* Check for readlink of control object. */
970     if (IS_CTL_VP(vp)) {
971         MARK_INT_FAIL(CODA_READLINK_STATS);
972         return(ENOENT);
973     }
974
975     if ((coda_symlink_cache) && (VALID_SYMLINK(cp))) { /* symlink was cached */
976         uiop->uio_rw = UIO_READ;
977         error = uiomove(cp->c_symlink, (int)cp->c_symlen, uiop);
978         if (error)
979             MARK_INT_FAIL(CODA_READLINK_STATS);
980         else
981             MARK_INT_SAT(CODA_READLINK_STATS);
982         return(error);
983     }
984
985     error = venus_readlink(vtomi(vp), &cp->c_fid, cred, p, &str, &len);
986
987     if (!error) {
988         uiop->uio_rw = UIO_READ;
989         error = uiomove(str, len, uiop);
990
991         if (coda_symlink_cache) {
992             cp->c_symlink = str;
993             cp->c_symlen = len;
994             cp->c_flags |= C_SYMLINK;
995         } else
996             CODA_FREE(str, len);
997     }
998
999     CODADEBUG(CODA_READLINK, myprintf(("in readlink result %d\n",error));)
1000     return(error);
1001 }
1002
1003 int
1004 coda_fsync(v)
1005     void *v;
1006 {
1007 /* true args */
1008     struct vop_fsync_args *ap = v;
1009     struct vnode *vp = ap->a_vp;
1010     struct cnode *cp = VTOC(vp);
1011     struct ucred *cred = ap->a_cred;
1012     struct proc *p = ap->a_p;
1013 /* locals */
1014     struct vnode *convp = cp->c_ovp;
1015     int error;
1016    
1017     MARK_ENTRY(CODA_FSYNC_STATS);
1018
1019     /* Check for fsync on an unmounting object */
1020     /* The NetBSD kernel, in it's infinite wisdom, can try to fsync
1021      * after an unmount has been initiated.  This is a Bad Thing,
1022      * which we have to avoid.  Not a legitimate failure for stats.
1023      */
1024     if (IS_UNMOUNTING(cp)) {
1025         return(ENODEV);
1026     }
1027
1028     /* Check for fsync of control object. */
1029     if (IS_CTL_VP(vp)) {
1030         MARK_INT_SAT(CODA_FSYNC_STATS);
1031         return(0);
1032     }
1033
1034     if (convp)
1035         VOP_FSYNC(convp, cred, MNT_WAIT, p);
1036
1037     /*
1038      * We see fsyncs with usecount == 1 then usecount == 0.
1039      * For now we ignore them.
1040      */
1041     /*
1042     if (!vp->v_usecount) {
1043         printf("coda_fsync on vnode %p with %d usecount.  c_flags = %x (%x)\n",
1044                 vp, vp->v_usecount, cp->c_flags, cp->c_flags&C_PURGING);
1045     }
1046     */
1047
1048     /*
1049      * We can expect fsync on any vnode at all if venus is pruging it.
1050      * Venus can't very well answer the fsync request, now can it?
1051      * Hopefully, it won't have to, because hopefully, venus preserves
1052      * the (possibly untrue) invariant that it never purges an open
1053      * vnode.  Hopefully.
1054      */
1055     if (cp->c_flags & C_PURGING) {
1056         return(0);
1057     }
1058
1059     /* needs research */
1060     return 0;
1061     error = venus_fsync(vtomi(vp), &cp->c_fid, cred, p);
1062
1063     CODADEBUG(CODA_FSYNC, myprintf(("in fsync result %d\n",error)); );
1064     return(error);
1065 }
1066
1067 int
1068 coda_inactive(v)
1069     void *v;
1070 {
1071     /* XXX - at the moment, inactive doesn't look at cred, and doesn't
1072        have a proc pointer.  Oops. */
1073 /* true args */
1074     struct vop_inactive_args *ap = v;
1075     struct vnode *vp = ap->a_vp;
1076     struct cnode *cp = VTOC(vp);
1077     struct ucred *cred __attribute__((unused)) = NULL;
1078     struct proc *p __attribute__((unused)) = curproc;
1079 /* upcall decl */
1080 /* locals */
1081
1082     /* We don't need to send inactive to venus - DCS */
1083     MARK_ENTRY(CODA_INACTIVE_STATS);
1084
1085     if (IS_CTL_VP(vp)) {
1086         MARK_INT_SAT(CODA_INACTIVE_STATS);
1087         return 0;
1088     }
1089
1090     CODADEBUG(CODA_INACTIVE, myprintf(("in inactive, %lx.%lx.%lx. vfsp %p\n",
1091                                   cp->c_fid.Volume, cp->c_fid.Vnode, 
1092                                   cp->c_fid.Unique, vp->v_mount));)
1093
1094     /* If an array has been allocated to hold the symlink, deallocate it */
1095     if ((coda_symlink_cache) && (VALID_SYMLINK(cp))) {
1096         if (cp->c_symlink == NULL)
1097             panic("coda_inactive: null symlink pointer in cnode");
1098         
1099         CODA_FREE(cp->c_symlink, cp->c_symlen);
1100         cp->c_flags &= ~C_SYMLINK;
1101         cp->c_symlen = 0;
1102     }
1103
1104     /* Remove it from the table so it can't be found. */
1105     coda_unsave(cp);
1106     if ((struct coda_mntinfo *)(vp->v_mount->mnt_data) == NULL) {
1107         myprintf(("Help! vfsp->vfs_data was NULL, but vnode %p wasn't dying\n", vp));
1108         panic("badness in coda_inactive\n");
1109     }
1110
1111     if (IS_UNMOUNTING(cp)) {
1112 #ifdef  DEBUG
1113         printf("coda_inactive: IS_UNMOUNTING use %d: vp %p, cp %p\n", vp->v_usecount, vp, cp);
1114         if (cp->c_ovp != NULL)
1115             printf("coda_inactive: cp->ovp != NULL use %d: vp %p, cp %p\n",
1116                    vp->v_usecount, vp, cp);
1117 #endif
1118         lockmgr(&cp->c_lock, LK_RELEASE, &vp->v_interlock, p);
1119     } else {
1120 #ifdef OLD_DIAGNOSTIC
1121         if (CTOV(cp)->v_usecount) {
1122             panic("coda_inactive: nonzero reference count");
1123         }
1124         if (cp->c_ovp != NULL) {
1125             panic("coda_inactive:  cp->ovp != NULL");
1126         }
1127 #endif
1128         VOP_UNLOCK(vp, 0, p);
1129         vgone(vp);
1130     }
1131
1132     MARK_INT_SAT(CODA_INACTIVE_STATS);
1133     return(0);
1134 }
1135
1136 /*
1137  * Remote file system operations having to do with directory manipulation.
1138  */
1139
1140 /* 
1141  * It appears that in NetBSD, lookup is supposed to return the vnode locked
1142  */
1143 int
1144 coda_lookup(v)
1145     void *v;
1146 {
1147 /* true args */
1148     struct vop_lookup_args *ap = v;
1149     struct vnode *dvp = ap->a_dvp;
1150     struct cnode *dcp = VTOC(dvp);
1151     struct vnode **vpp = ap->a_vpp;
1152     /* 
1153      * It looks as though ap->a_cnp->ni_cnd->cn_nameptr holds the rest
1154      * of the string to xlate, and that we must try to get at least
1155      * ap->a_cnp->ni_cnd->cn_namelen of those characters to macth.  I
1156      * could be wrong. 
1157      */
1158     struct componentname  *cnp = ap->a_cnp;
1159     struct ucred *cred = cnp->cn_cred;
1160     struct proc *p = cnp->cn_proc;
1161 /* locals */
1162     struct cnode *cp;
1163     const char *nm = cnp->cn_nameptr;
1164     int len = cnp->cn_namelen;
1165     ViceFid VFid;
1166     int vtype;
1167     int error = 0;
1168
1169     MARK_ENTRY(CODA_LOOKUP_STATS);
1170
1171     CODADEBUG(CODA_LOOKUP, myprintf(("lookup: %s in %lx.%lx.%lx\n",
1172                                    nm, dcp->c_fid.Volume,
1173                                    dcp->c_fid.Vnode, dcp->c_fid.Unique)););
1174
1175     /* Check for lookup of control object. */
1176     if (IS_CTL_NAME(dvp, nm, len)) {
1177         *vpp = coda_ctlvp;
1178         vref(*vpp);
1179         MARK_INT_SAT(CODA_LOOKUP_STATS);
1180         goto exit;
1181     }
1182
1183     if (len+1 > CODA_MAXNAMLEN) {
1184         MARK_INT_FAIL(CODA_LOOKUP_STATS);
1185         CODADEBUG(CODA_LOOKUP, myprintf(("name too long: lookup, %lx.%lx.%lx(%s)\n",
1186                                     dcp->c_fid.Volume, dcp->c_fid.Vnode,
1187                                     dcp->c_fid.Unique, nm)););
1188         *vpp = (struct vnode *)0;
1189         error = EINVAL;
1190         goto exit;
1191     }
1192     /* First try to look the file up in the cfs name cache */
1193     /* lock the parent vnode? */
1194     cp = coda_nc_lookup(dcp, nm, len, cred);
1195     if (cp) {
1196         *vpp = CTOV(cp);
1197         vref(*vpp);
1198         CODADEBUG(CODA_LOOKUP, 
1199                  myprintf(("lookup result %d vpp %p\n",error,*vpp));)
1200     } else {
1201         
1202         /* The name wasn't cached, so we need to contact Venus */
1203         error = venus_lookup(vtomi(dvp), &dcp->c_fid, nm, len, cred, p, &VFid, &vtype);
1204         
1205         if (error) {
1206             MARK_INT_FAIL(CODA_LOOKUP_STATS);
1207             CODADEBUG(CODA_LOOKUP, myprintf(("lookup error on %lx.%lx.%lx(%s)%d\n",
1208                                         dcp->c_fid.Volume, dcp->c_fid.Vnode, dcp->c_fid.Unique, nm, error));)
1209             *vpp = (struct vnode *)0;
1210         } else {
1211             MARK_INT_SAT(CODA_LOOKUP_STATS);
1212             CODADEBUG(CODA_LOOKUP, 
1213                      myprintf(("lookup: vol %lx vno %lx uni %lx type %o result %d\n",
1214                             VFid.Volume, VFid.Vnode, VFid.Unique, vtype,
1215                             error)); )
1216                 
1217             cp = make_coda_node(&VFid, dvp->v_mount, vtype);
1218             *vpp = CTOV(cp);
1219             
1220             /* enter the new vnode in the Name Cache only if the top bit isn't set */
1221             /* And don't enter a new vnode for an invalid one! */
1222             if (!(vtype & CODA_NOCACHE))
1223                 coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
1224         }
1225     }
1226
1227  exit:
1228     /* 
1229      * If we are creating, and this was the last name to be looked up,
1230      * and the error was ENOENT, then there really shouldn't be an
1231      * error and we can make the leaf NULL and return success.  Since
1232      * this is supposed to work under Mach as well as NetBSD, we're
1233      * leaving this fn wrapped.  We also must tell lookup/namei that
1234      * we need to save the last component of the name.  (Create will
1235      * have to free the name buffer later...lucky us...)
1236      */
1237     if (((cnp->cn_nameiop == CREATE) || (cnp->cn_nameiop == RENAME))
1238         && (cnp->cn_flags & ISLASTCN)
1239         && (error == ENOENT))
1240     {
1241         error = EJUSTRETURN;
1242         cnp->cn_flags |= SAVENAME;
1243         *ap->a_vpp = NULL;
1244     }
1245
1246     /* 
1247      * If we are removing, and we are at the last element, and we
1248      * found it, then we need to keep the name around so that the
1249      * removal will go ahead as planned.  Unfortunately, this will
1250      * probably also lock the to-be-removed vnode, which may or may
1251      * not be a good idea.  I'll have to look at the bits of
1252      * coda_remove to make sure.  We'll only save the name if we did in
1253      * fact find the name, otherwise coda_remove won't have a chance
1254      * to free the pathname.  
1255      */
1256     if ((cnp->cn_nameiop == DELETE)
1257         && (cnp->cn_flags & ISLASTCN)
1258         && !error)
1259     {
1260         cnp->cn_flags |= SAVENAME;
1261     }
1262
1263     /* 
1264      * If the lookup went well, we need to (potentially?) unlock the
1265      * parent, and lock the child.  We are only responsible for
1266      * checking to see if the parent is supposed to be unlocked before
1267      * we return.  We must always lock the child (provided there is
1268      * one, and (the parent isn't locked or it isn't the same as the
1269      * parent.)  Simple, huh?  We can never leave the parent locked unless
1270      * we are ISLASTCN
1271      */
1272     if (!error || (error == EJUSTRETURN)) {
1273         if (!(cnp->cn_flags & LOCKPARENT) || !(cnp->cn_flags & ISLASTCN)) {
1274             if ((error = VOP_UNLOCK(dvp, 0, p))) {
1275                 return error; 
1276             }       
1277             /* 
1278              * The parent is unlocked.  As long as there is a child,
1279              * lock it without bothering to check anything else. 
1280              */
1281             if (*ap->a_vpp) {
1282                 if ((error = VOP_LOCK(*ap->a_vpp, LK_EXCLUSIVE, p))) {
1283                     printf("coda_lookup: ");
1284                     panic("unlocked parent but couldn't lock child");
1285                 }
1286             }
1287         } else {
1288             /* The parent is locked, and may be the same as the child */
1289             if (*ap->a_vpp && (*ap->a_vpp != dvp)) {
1290                 /* Different, go ahead and lock it. */
1291                 if ((error = VOP_LOCK(*ap->a_vpp, LK_EXCLUSIVE, p))) {
1292                     printf("coda_lookup: ");
1293                     panic("unlocked parent but couldn't lock child");
1294                 }
1295             }
1296         }
1297     } else {
1298         /* If the lookup failed, we need to ensure that the leaf is NULL */
1299         /* Don't change any locking? */
1300         *ap->a_vpp = NULL;
1301     }
1302     return(error);
1303 }
1304
1305 /*ARGSUSED*/
1306 int
1307 coda_create(v)
1308     void *v;
1309 {
1310 /* true args */
1311     struct vop_create_args *ap = v;
1312     struct vnode *dvp = ap->a_dvp;
1313     struct cnode *dcp = VTOC(dvp);
1314     struct vattr *va = ap->a_vap;
1315     int exclusive = 1;
1316     int mode = ap->a_vap->va_mode;
1317     struct vnode **vpp = ap->a_vpp;
1318     struct componentname  *cnp = ap->a_cnp;
1319     struct ucred *cred = cnp->cn_cred;
1320     struct proc *p = cnp->cn_proc;
1321 /* locals */
1322     int error;
1323     struct cnode *cp;
1324     const char *nm = cnp->cn_nameptr;
1325     int len = cnp->cn_namelen;
1326     ViceFid VFid;
1327     struct vattr attr;
1328
1329     MARK_ENTRY(CODA_CREATE_STATS);
1330
1331     /* All creates are exclusive XXX */
1332     /* I'm assuming the 'mode' argument is the file mode bits XXX */
1333
1334     /* Check for create of control object. */
1335     if (IS_CTL_NAME(dvp, nm, len)) {
1336         *vpp = (struct vnode *)0;
1337         MARK_INT_FAIL(CODA_CREATE_STATS);
1338         return(EACCES);
1339     }
1340
1341     error = venus_create(vtomi(dvp), &dcp->c_fid, nm, len, exclusive, mode, va, cred, p, &VFid, &attr);
1342
1343     if (!error) {
1344         
1345         /* If this is an exclusive create, panic if the file already exists. */
1346         /* Venus should have detected the file and reported EEXIST. */
1347
1348         if ((exclusive == 1) &&
1349             (coda_find(&VFid) != NULL))
1350             panic("cnode existed for newly created file!");
1351         
1352         cp = make_coda_node(&VFid, dvp->v_mount, attr.va_type);
1353         *vpp = CTOV(cp);
1354         
1355         /* Update va to reflect the new attributes. */
1356         (*va) = attr;
1357         
1358         /* Update the attribute cache and mark it as valid */
1359         if (coda_attr_cache) {
1360             VTOC(*vpp)->c_vattr = attr;
1361             VTOC(*vpp)->c_flags |= C_VATTR;       
1362         }
1363
1364         /* Invalidate the parent's attr cache, the modification time has changed */
1365         VTOC(dvp)->c_flags &= ~C_VATTR;
1366         
1367         /* enter the new vnode in the Name Cache */
1368         coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
1369         
1370         CODADEBUG(CODA_CREATE, 
1371                  myprintf(("create: (%lx.%lx.%lx), result %d\n",
1372                         VFid.Volume, VFid.Vnode, VFid.Unique, error)); )
1373     } else {
1374         *vpp = (struct vnode *)0;
1375         CODADEBUG(CODA_CREATE, myprintf(("create error %d\n", error));)
1376     }
1377
1378     if (!error) {
1379         if (cnp->cn_flags & LOCKLEAF) {
1380             if ((error = VOP_LOCK(*ap->a_vpp, LK_EXCLUSIVE, p))) {
1381                 printf("coda_create: ");
1382                 panic("unlocked parent but couldn't lock child");
1383             }
1384         }
1385 #ifdef OLD_DIAGNOSTIC
1386         else {
1387             printf("coda_create: LOCKLEAF not set!\n");
1388         }
1389 #endif
1390     }
1391     /* Have to free the previously saved name */
1392     /* 
1393      * This condition is stolen from ufs_makeinode.  I have no idea
1394      * why it's here, but what the hey...
1395      */
1396     if ((cnp->cn_flags & SAVESTART) == 0) {
1397         zfree(namei_zone, cnp->cn_pnbuf);
1398     }
1399     return(error);
1400 }
1401
1402 int
1403 coda_remove(v)
1404     void *v;
1405 {
1406 /* true args */
1407     struct vop_remove_args *ap = v;
1408     struct vnode *dvp = ap->a_dvp;
1409     struct cnode *cp = VTOC(dvp);
1410     struct componentname  *cnp = ap->a_cnp;
1411     struct ucred *cred = cnp->cn_cred;
1412     struct proc *p = cnp->cn_proc;
1413 /* locals */
1414     int error;
1415     const char *nm = cnp->cn_nameptr;
1416     int len = cnp->cn_namelen;
1417     struct cnode *tp;
1418
1419     MARK_ENTRY(CODA_REMOVE_STATS);
1420
1421     CODADEBUG(CODA_REMOVE, myprintf(("remove: %s in %lx.%lx.%lx\n",
1422                                    nm, cp->c_fid.Volume, cp->c_fid.Vnode,
1423                                    cp->c_fid.Unique)););
1424
1425     /* Remove the file's entry from the CODA Name Cache */
1426     /* We're being conservative here, it might be that this person
1427      * doesn't really have sufficient access to delete the file
1428      * but we feel zapping the entry won't really hurt anyone -- dcs
1429      */
1430     /* I'm gonna go out on a limb here. If a file and a hardlink to it
1431      * exist, and one is removed, the link count on the other will be
1432      * off by 1. We could either invalidate the attrs if cached, or
1433      * fix them. I'll try to fix them. DCS 11/8/94
1434      */
1435     tp = coda_nc_lookup(VTOC(dvp), nm, len, cred);
1436     if (tp) {
1437         if (VALID_VATTR(tp)) {  /* If attrs are cached */
1438             if (tp->c_vattr.va_nlink > 1) {     /* If it's a hard link */
1439                 tp->c_vattr.va_nlink--;
1440             }
1441         }
1442         
1443         coda_nc_zapfile(VTOC(dvp), nm, len); 
1444         /* No need to flush it if it doesn't exist! */
1445     }
1446     /* Invalidate the parent's attr cache, the modification time has changed */
1447     VTOC(dvp)->c_flags &= ~C_VATTR;
1448
1449     /* Check for remove of control object. */
1450     if (IS_CTL_NAME(dvp, nm, len)) {
1451         MARK_INT_FAIL(CODA_REMOVE_STATS);
1452         return(ENOENT);
1453     }
1454
1455     error = venus_remove(vtomi(dvp), &cp->c_fid, nm, len, cred, p);
1456
1457     CODADEBUG(CODA_REMOVE, myprintf(("in remove result %d\n",error)); )
1458
1459     if ((cnp->cn_flags & SAVESTART) == 0) {
1460         zfree(namei_zone, cnp->cn_pnbuf);
1461     }
1462     return(error);
1463 }
1464
1465 int
1466 coda_link(v)
1467     void *v;
1468 {
1469 /* true args */
1470     struct vop_link_args *ap = v;
1471     struct vnode *vp = ap->a_vp;
1472     struct cnode *cp = VTOC(vp);
1473     struct vnode *tdvp = ap->a_tdvp;
1474     struct cnode *tdcp = VTOC(tdvp);
1475     struct componentname *cnp = ap->a_cnp;
1476     struct ucred *cred = cnp->cn_cred;
1477     struct proc *p = cnp->cn_proc;
1478 /* locals */
1479     int error;
1480     const char *nm = cnp->cn_nameptr;
1481     int len = cnp->cn_namelen;
1482
1483     MARK_ENTRY(CODA_LINK_STATS);
1484
1485     if (codadebug & CODADBGMSK(CODA_LINK)) {
1486
1487         myprintf(("nb_link:   vp fid: (%lx.%lx.%lx)\n",
1488                   cp->c_fid.Volume, cp->c_fid.Vnode, cp->c_fid.Unique));
1489         myprintf(("nb_link: tdvp fid: (%lx.%lx.%lx)\n",
1490                   tdcp->c_fid.Volume, tdcp->c_fid.Vnode, tdcp->c_fid.Unique));
1491         
1492     }
1493     if (codadebug & CODADBGMSK(CODA_LINK)) {
1494         myprintf(("link:   vp fid: (%lx.%lx.%lx)\n",
1495                   cp->c_fid.Volume, cp->c_fid.Vnode, cp->c_fid.Unique));
1496         myprintf(("link: tdvp fid: (%lx.%lx.%lx)\n",
1497                   tdcp->c_fid.Volume, tdcp->c_fid.Vnode, tdcp->c_fid.Unique));
1498
1499     }
1500
1501     /* Check for link to/from control object. */
1502     if (IS_CTL_NAME(tdvp, nm, len) || IS_CTL_VP(vp)) {
1503         MARK_INT_FAIL(CODA_LINK_STATS);
1504         return(EACCES);
1505     }
1506
1507     error = venus_link(vtomi(vp), &cp->c_fid, &tdcp->c_fid, nm, len, cred, p);
1508
1509     /* Invalidate the parent's attr cache, the modification time has changed */
1510     VTOC(tdvp)->c_flags &= ~C_VATTR;
1511     VTOC(vp)->c_flags &= ~C_VATTR;
1512
1513     CODADEBUG(CODA_LINK,        myprintf(("in link result %d\n",error)); )
1514
1515 exit:
1516
1517     /* Drop the name buffer if we don't need to SAVESTART */
1518     if ((cnp->cn_flags & SAVESTART) == 0) {
1519         zfree(namei_zone, cnp->cn_pnbuf);
1520     }
1521     return(error);
1522 }
1523
1524 int
1525 coda_rename(v)
1526     void *v;
1527 {
1528 /* true args */
1529     struct vop_rename_args *ap = v;
1530     struct vnode *odvp = ap->a_fdvp;
1531     struct cnode *odcp = VTOC(odvp);
1532     struct componentname  *fcnp = ap->a_fcnp;
1533     struct vnode *ndvp = ap->a_tdvp;
1534     struct cnode *ndcp = VTOC(ndvp);
1535     struct componentname  *tcnp = ap->a_tcnp;
1536     struct ucred *cred = fcnp->cn_cred;
1537     struct proc *p = fcnp->cn_proc;
1538 /* true args */
1539     int error;
1540     const char *fnm = fcnp->cn_nameptr;
1541     int flen = fcnp->cn_namelen;
1542     const char *tnm = tcnp->cn_nameptr;
1543     int tlen = tcnp->cn_namelen;
1544
1545     MARK_ENTRY(CODA_RENAME_STATS);
1546
1547     /* Hmmm.  The vnodes are already looked up.  Perhaps they are locked?
1548        This could be Bad. XXX */
1549 #ifdef OLD_DIAGNOSTIC
1550     if ((fcnp->cn_cred != tcnp->cn_cred)
1551         || (fcnp->cn_proc != tcnp->cn_proc))
1552     {
1553         panic("coda_rename: component names don't agree");
1554     }
1555 #endif
1556
1557     /* Check for rename involving control object. */ 
1558     if (IS_CTL_NAME(odvp, fnm, flen) || IS_CTL_NAME(ndvp, tnm, tlen)) {
1559         MARK_INT_FAIL(CODA_RENAME_STATS);
1560         return(EACCES);
1561     }
1562
1563     /* Problem with moving directories -- need to flush entry for .. */
1564     if (odvp != ndvp) {
1565         struct cnode *ovcp = coda_nc_lookup(VTOC(odvp), fnm, flen, cred);
1566         if (ovcp) {
1567             struct vnode *ovp = CTOV(ovcp);
1568             if ((ovp) &&
1569                 (ovp->v_type == VDIR)) /* If it's a directory */
1570                 coda_nc_zapfile(VTOC(ovp),"..", 2);
1571         }
1572     }
1573
1574     /* Remove the entries for both source and target files */
1575     coda_nc_zapfile(VTOC(odvp), fnm, flen);
1576     coda_nc_zapfile(VTOC(ndvp), tnm, tlen);
1577
1578     /* Invalidate the parent's attr cache, the modification time has changed */
1579     VTOC(odvp)->c_flags &= ~C_VATTR;
1580     VTOC(ndvp)->c_flags &= ~C_VATTR;
1581
1582     if (flen+1 > CODA_MAXNAMLEN) {
1583         MARK_INT_FAIL(CODA_RENAME_STATS);
1584         error = EINVAL;
1585         goto exit;
1586     }
1587
1588     if (tlen+1 > CODA_MAXNAMLEN) {
1589         MARK_INT_FAIL(CODA_RENAME_STATS);
1590         error = EINVAL;
1591         goto exit;
1592     }
1593
1594     error = venus_rename(vtomi(odvp), &odcp->c_fid, &ndcp->c_fid, fnm, flen, tnm, tlen, cred, p);
1595
1596  exit:
1597     CODADEBUG(CODA_RENAME, myprintf(("in rename result %d\n",error));)
1598     /* XXX - do we need to call cache pureg on the moved vnode? */
1599     cache_purge(ap->a_fvp);
1600
1601     /* It seems to be incumbent on us to drop locks on all four vnodes */
1602     /* From-vnodes are not locked, only ref'd.  To-vnodes are locked. */
1603
1604     vrele(ap->a_fvp);
1605     vrele(odvp);
1606
1607     if (ap->a_tvp) {
1608         if (ap->a_tvp == ndvp) {
1609             vrele(ap->a_tvp);
1610         } else {
1611             vput(ap->a_tvp);
1612         }
1613     }
1614
1615     vput(ndvp);
1616     return(error);
1617 }
1618
1619 int
1620 coda_mkdir(v)
1621     void *v;
1622 {
1623 /* true args */
1624     struct vop_mkdir_args *ap = v;
1625     struct vnode *dvp = ap->a_dvp;
1626     struct cnode *dcp = VTOC(dvp);      
1627     struct componentname  *cnp = ap->a_cnp;
1628     register struct vattr *va = ap->a_vap;
1629     struct vnode **vpp = ap->a_vpp;
1630     struct ucred *cred = cnp->cn_cred;
1631     struct proc *p = cnp->cn_proc;
1632 /* locals */
1633     int error;
1634     const char *nm = cnp->cn_nameptr;
1635     int len = cnp->cn_namelen;
1636     struct cnode *cp;
1637     ViceFid VFid;
1638     struct vattr ova;
1639
1640     MARK_ENTRY(CODA_MKDIR_STATS);
1641
1642     /* Check for mkdir of target object. */
1643     if (IS_CTL_NAME(dvp, nm, len)) {
1644         *vpp = (struct vnode *)0;
1645         MARK_INT_FAIL(CODA_MKDIR_STATS);
1646         return(EACCES);
1647     }
1648
1649     if (len+1 > CODA_MAXNAMLEN) {
1650         *vpp = (struct vnode *)0;
1651         MARK_INT_FAIL(CODA_MKDIR_STATS);
1652         return(EACCES);
1653     }
1654
1655     error = venus_mkdir(vtomi(dvp), &dcp->c_fid, nm, len, va, cred, p, &VFid, &ova);
1656
1657     if (!error) {
1658         if (coda_find(&VFid) != NULL)
1659             panic("cnode existed for newly created directory!");
1660         
1661         
1662         cp =  make_coda_node(&VFid, dvp->v_mount, va->va_type);
1663         *vpp = CTOV(cp);
1664         
1665         /* enter the new vnode in the Name Cache */
1666         coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
1667
1668         /* as a side effect, enter "." and ".." for the directory */
1669         coda_nc_enter(VTOC(*vpp), ".", 1, cred, VTOC(*vpp));
1670         coda_nc_enter(VTOC(*vpp), "..", 2, cred, VTOC(dvp));
1671
1672         if (coda_attr_cache) {
1673             VTOC(*vpp)->c_vattr = ova;          /* update the attr cache */
1674             VTOC(*vpp)->c_flags |= C_VATTR;     /* Valid attributes in cnode */
1675         }
1676
1677         /* Invalidate the parent's attr cache, the modification time has changed */
1678         VTOC(dvp)->c_flags &= ~C_VATTR;
1679         
1680         CODADEBUG( CODA_MKDIR, myprintf(("mkdir: (%lx.%lx.%lx) result %d\n",
1681                                     VFid.Volume, VFid.Vnode, VFid.Unique, error)); )
1682     } else {
1683         *vpp = (struct vnode *)0;
1684         CODADEBUG(CODA_MKDIR, myprintf(("mkdir error %d\n",error));)
1685     }
1686
1687     /* Have to free the previously saved name */
1688     /* 
1689      * ufs_mkdir doesn't check for SAVESTART before freeing the
1690      * pathname buffer, but ufs_create does.  For the moment, I'll
1691      * follow their lead, but this seems like it is probably
1692      * incorrect.  
1693      */
1694     zfree(namei_zone, cnp->cn_pnbuf);
1695     return(error);
1696 }
1697
1698 int
1699 coda_rmdir(v)
1700     void *v;
1701 {
1702 /* true args */
1703     struct vop_rmdir_args *ap = v;
1704     struct vnode *dvp = ap->a_dvp;
1705     struct cnode *dcp = VTOC(dvp);
1706     struct componentname  *cnp = ap->a_cnp;
1707     struct ucred *cred = cnp->cn_cred;
1708     struct proc *p = cnp->cn_proc;
1709 /* true args */
1710     int error;
1711     const char *nm = cnp->cn_nameptr;
1712     int len = cnp->cn_namelen;
1713     struct cnode *cp;
1714    
1715     MARK_ENTRY(CODA_RMDIR_STATS);
1716
1717     /* Check for rmdir of control object. */
1718     if (IS_CTL_NAME(dvp, nm, len)) {
1719         MARK_INT_FAIL(CODA_RMDIR_STATS);
1720         return(ENOENT);
1721     }
1722
1723     /* We're being conservative here, it might be that this person
1724      * doesn't really have sufficient access to delete the file
1725      * but we feel zapping the entry won't really hurt anyone -- dcs
1726      */
1727     /*
1728      * As a side effect of the rmdir, remove any entries for children of
1729      * the directory, especially "." and "..".
1730      */
1731     cp = coda_nc_lookup(dcp, nm, len, cred);
1732     if (cp) coda_nc_zapParentfid(&(cp->c_fid), NOT_DOWNCALL);
1733
1734     /* Remove the file's entry from the CODA Name Cache */
1735     coda_nc_zapfile(dcp, nm, len);
1736
1737     /* Invalidate the parent's attr cache, the modification time has changed */
1738     dcp->c_flags &= ~C_VATTR;
1739
1740     error = venus_rmdir(vtomi(dvp), &dcp->c_fid, nm, len, cred, p);
1741
1742     CODADEBUG(CODA_RMDIR, myprintf(("in rmdir result %d\n", error)); )
1743
1744     if ((cnp->cn_flags & SAVESTART) == 0) {
1745         zfree(namei_zone, cnp->cn_pnbuf);
1746     }
1747     return(error);
1748 }
1749
1750 int
1751 coda_symlink(v)
1752     void *v;
1753 {
1754 /* true args */
1755     struct vop_symlink_args *ap = v;
1756     struct vnode *tdvp = ap->a_dvp;
1757     struct cnode *tdcp = VTOC(tdvp);    
1758     struct componentname *cnp = ap->a_cnp;
1759     struct vattr *tva = ap->a_vap;
1760     char *path = ap->a_target;
1761     struct ucred *cred = cnp->cn_cred;
1762     struct proc *p = cnp->cn_proc;
1763 /* locals */
1764     int error;
1765     /* 
1766      * XXX I'm assuming the following things about coda_symlink's
1767      * arguments: 
1768      *       t(foo) is the new name/parent/etc being created.
1769      *       lname is the contents of the new symlink. 
1770      */
1771     char *nm = cnp->cn_nameptr;
1772     int len = cnp->cn_namelen;
1773     int plen = strlen(path);
1774
1775     /* XXX What about the vpp argument?  Do we need it? */
1776     /* 
1777      * Here's the strategy for the moment: perform the symlink, then
1778      * do a lookup to grab the resulting vnode.  I know this requires
1779      * two communications with Venus for a new sybolic link, but
1780      * that's the way the ball bounces.  I don't yet want to change
1781      * the way the Mach symlink works.  When Mach support is
1782      * deprecated, we should change symlink so that the common case
1783      * returns the resultant vnode in a vpp argument.
1784      */
1785
1786     MARK_ENTRY(CODA_SYMLINK_STATS);
1787
1788     /* Check for symlink of control object. */
1789     if (IS_CTL_NAME(tdvp, nm, len)) {
1790         MARK_INT_FAIL(CODA_SYMLINK_STATS);
1791         return(EACCES);
1792     }
1793
1794     if (plen+1 > CODA_MAXPATHLEN) {
1795         MARK_INT_FAIL(CODA_SYMLINK_STATS);
1796         return(EINVAL);
1797     }
1798
1799     if (len+1 > CODA_MAXNAMLEN) {
1800         MARK_INT_FAIL(CODA_SYMLINK_STATS);
1801         error = EINVAL;
1802         goto exit;
1803     }
1804
1805     error = venus_symlink(vtomi(tdvp), &tdcp->c_fid, path, plen, nm, len, tva, cred, p);
1806
1807     /* Invalidate the parent's attr cache, the modification time has changed */
1808     tdcp->c_flags &= ~C_VATTR;
1809
1810     /* 
1811      * Free the name buffer 
1812      */
1813     if ((cnp->cn_flags & SAVESTART) == 0) {
1814         zfree(namei_zone, cnp->cn_pnbuf);
1815     }
1816
1817  exit:    
1818     CODADEBUG(CODA_SYMLINK, myprintf(("in symlink result %d\n",error)); )
1819     return(error);
1820 }
1821
1822 /*
1823  * Read directory entries.
1824  */
1825 int
1826 coda_readdir(v)
1827     void *v;
1828 {
1829 /* true args */
1830     struct vop_readdir_args *ap = v;
1831     struct vnode *vp = ap->a_vp;
1832     struct cnode *cp = VTOC(vp);
1833     register struct uio *uiop = ap->a_uio;
1834     struct ucred *cred = ap->a_cred;
1835     int *eofflag = ap->a_eofflag;
1836     u_long **cookies = ap->a_cookies;
1837     int *ncookies = ap->a_ncookies;
1838     struct proc *p = ap->a_uio->uio_procp;
1839 /* upcall decl */
1840 /* locals */
1841     int error = 0;
1842
1843     MARK_ENTRY(CODA_READDIR_STATS);
1844
1845     CODADEBUG(CODA_READDIR, myprintf(("coda_readdir(%p, %d, %qd, %d)\n", uiop->uio_iov->iov_base, uiop->uio_resid, uiop->uio_offset, uiop->uio_segflg)); )
1846         
1847     /* Check for readdir of control object. */
1848     if (IS_CTL_VP(vp)) {
1849         MARK_INT_FAIL(CODA_READDIR_STATS);
1850         return(ENOENT);
1851     }
1852
1853     {
1854         /* If directory is not already open do an "internal open" on it. */
1855         int opened_internally = 0;
1856         if (cp->c_ovp == NULL) {
1857             opened_internally = 1;
1858             MARK_INT_GEN(CODA_OPEN_STATS);
1859             error = VOP_OPEN(vp, FREAD, cred, p);
1860 printf("coda_readdir: Internally Opening %p\n", vp);
1861             if (error) {
1862                 printf("coda_readdir: VOP_OPEN on container failed %d\n", error);
1863                 return (error);
1864             }
1865             if (vp->v_type == VREG) {
1866                 error = vfs_object_create(vp, p, cred, 1);
1867                 if (error != 0) {
1868                     printf("coda_readdir: vfs_object_create() returns %d\n", error);
1869                     vput(vp);
1870                 }
1871             }
1872             if (error) return(error);
1873         }
1874         
1875         /* Have UFS handle the call. */
1876         CODADEBUG(CODA_READDIR, myprintf(("indirect readdir: fid = (%lx.%lx.%lx), refcnt = %d\n",cp->c_fid.Volume, cp->c_fid.Vnode, cp->c_fid.Unique, vp->v_usecount)); )
1877         error = VOP_READDIR(cp->c_ovp, uiop, cred, eofflag, ncookies,
1878                                cookies);
1879         
1880         if (error)
1881             MARK_INT_FAIL(CODA_READDIR_STATS);
1882         else
1883             MARK_INT_SAT(CODA_READDIR_STATS);
1884         
1885         /* Do an "internal close" if necessary. */ 
1886         if (opened_internally) {
1887             MARK_INT_GEN(CODA_CLOSE_STATS);
1888             (void)VOP_CLOSE(vp, FREAD, cred, p);
1889         }
1890     }
1891
1892     return(error);
1893 }
1894
1895 /*
1896  * Convert from file system blocks to device blocks
1897  */
1898 int
1899 coda_bmap(v)
1900     void *v;
1901 {
1902     /* XXX on the global proc */
1903 /* true args */
1904     struct vop_bmap_args *ap = v;
1905     struct vnode *vp __attribute__((unused)) = ap->a_vp;        /* file's vnode */
1906     daddr_t bn __attribute__((unused)) = ap->a_bn;      /* fs block number */
1907     struct vnode **vpp = ap->a_vpp;                     /* RETURN vp of device */
1908     daddr_t *bnp __attribute__((unused)) = ap->a_bnp;   /* RETURN device block number */
1909     struct proc *p __attribute__((unused)) = curproc;
1910 /* upcall decl */
1911 /* locals */
1912
1913         int ret = 0;
1914         struct cnode *cp;
1915
1916         cp = VTOC(vp);
1917         if (cp->c_ovp) {
1918                 return EINVAL;
1919                 ret =  VOP_BMAP(cp->c_ovp, bn, vpp, bnp, ap->a_runp, ap->a_runb);
1920 #if     0
1921                 printf("VOP_BMAP(cp->c_ovp %p, bn %p, vpp %p, bnp %p, ap->a_runp %p, ap->a_runb %p) = %d\n",
1922                         cp->c_ovp, bn, vpp, bnp, ap->a_runp, ap->a_runb, ret);
1923 #endif
1924                 return ret;
1925         } else {
1926                 printf("coda_bmap: no container\n");
1927                 return(EOPNOTSUPP);
1928         }
1929 }
1930
1931 /*
1932  * I don't think the following two things are used anywhere, so I've
1933  * commented them out 
1934  * 
1935  * struct buf *async_bufhead; 
1936  * int async_daemon_count;
1937  */
1938 int
1939 coda_strategy(v)
1940     void *v;
1941 {
1942 /* true args */
1943     struct vop_strategy_args *ap = v;
1944     register struct buf *bp __attribute__((unused)) = ap->a_bp;
1945     struct proc *p __attribute__((unused)) = curproc;
1946 /* upcall decl */
1947 /* locals */
1948
1949         printf("coda_strategy: called ???\n");
1950         return(EOPNOTSUPP);
1951 }
1952
1953 int
1954 coda_reclaim(v) 
1955     void *v;
1956 {
1957 /* true args */
1958     struct vop_reclaim_args *ap = v;
1959     struct vnode *vp = ap->a_vp;
1960     struct cnode *cp = VTOC(vp);
1961 /* upcall decl */
1962 /* locals */
1963
1964 /*
1965  * Forced unmount/flush will let vnodes with non zero use be destroyed!
1966  */
1967     ENTRY;
1968
1969     if (IS_UNMOUNTING(cp)) {
1970 #ifdef  DEBUG
1971         if (VTOC(vp)->c_ovp) {
1972             if (IS_UNMOUNTING(cp))
1973                 printf("coda_reclaim: c_ovp not void: vp %p, cp %p\n", vp, cp);
1974         }
1975 #endif
1976     } else {
1977 #ifdef OLD_DIAGNOSTIC
1978         if (vp->v_usecount != 0) 
1979             print("coda_reclaim: pushing active %p\n", vp);
1980         if (VTOC(vp)->c_ovp) {
1981             panic("coda_reclaim: c_ovp not void");
1982     }
1983 #endif
1984     }   
1985     cache_purge(vp);
1986     coda_free(VTOC(vp));
1987     VTOC(vp) = NULL;
1988     return (0);
1989 }
1990
1991 int
1992 coda_lock(v)
1993     void *v;
1994 {
1995 /* true args */
1996     struct vop_lock_args *ap = v;
1997     struct vnode *vp = ap->a_vp;
1998     struct cnode *cp = VTOC(vp);
1999     struct proc  *p = ap->a_p;
2000 /* upcall decl */
2001 /* locals */
2002
2003     ENTRY;
2004
2005     if (coda_lockdebug) {
2006         myprintf(("Attempting lock on %lx.%lx.%lx\n",
2007                   cp->c_fid.Volume, cp->c_fid.Vnode, cp->c_fid.Unique));
2008     }
2009
2010     return (lockmgr(&cp->c_lock, ap->a_flags, &vp->v_interlock, p));
2011 }
2012
2013 int
2014 coda_unlock(v)
2015     void *v;
2016 {
2017 /* true args */
2018     struct vop_unlock_args *ap = v;
2019     struct vnode *vp = ap->a_vp;
2020     struct cnode *cp = VTOC(vp);
2021     struct proc  *p = ap->a_p;
2022 /* upcall decl */
2023 /* locals */
2024
2025     ENTRY;
2026     if (coda_lockdebug) {
2027         myprintf(("Attempting unlock on %lx.%lx.%lx\n",
2028                   cp->c_fid.Volume, cp->c_fid.Vnode, cp->c_fid.Unique));
2029     }
2030
2031     return (lockmgr(&cp->c_lock, ap->a_flags | LK_RELEASE, &vp->v_interlock, p));
2032 }
2033
2034 int
2035 coda_islocked(v)
2036     void *v;
2037 {
2038 /* true args */
2039     struct vop_islocked_args *ap = v;
2040     struct cnode *cp = VTOC(ap->a_vp);
2041     ENTRY;
2042
2043     return (lockstatus(&cp->c_lock));
2044 }
2045
2046 /* How one looks up a vnode given a device/inode pair: */
2047 int
2048 coda_grab_vnode(dev_t dev, ino_t ino, struct vnode **vpp)
2049 {
2050     /* This is like VFS_VGET() or igetinode()! */
2051     int           error;
2052     struct mount *mp;
2053
2054     if (!(mp = devtomp(dev))) {
2055         myprintf(("coda_grab_vnode: devtomp(%d) returns NULL\n", dev));
2056         return(ENXIO);
2057     }
2058
2059     /* XXX - ensure that nonzero-return means failure */
2060     error = VFS_VGET(mp,ino,vpp);
2061     if (error) {
2062         myprintf(("coda_grab_vnode: iget/vget(%d, %d) returns %p, err %d\n", 
2063                   dev, ino, *vpp, error));
2064         return(ENOENT);
2065     }
2066     return(0);
2067 }
2068
2069 void
2070 print_vattr( attr )
2071         struct vattr *attr;
2072 {
2073     char *typestr;
2074
2075     switch (attr->va_type) {
2076     case VNON:
2077         typestr = "VNON";
2078         break;
2079     case VREG:
2080         typestr = "VREG";
2081         break;
2082     case VDIR:
2083         typestr = "VDIR";
2084         break;
2085     case VBLK:
2086         typestr = "VBLK";
2087         break;
2088     case VCHR:
2089         typestr = "VCHR";
2090         break;
2091     case VLNK:
2092         typestr = "VLNK";
2093         break;
2094     case VSOCK:
2095         typestr = "VSCK";
2096         break;
2097     case VFIFO:
2098         typestr = "VFFO";
2099         break;
2100     case VBAD:
2101         typestr = "VBAD";
2102         break;
2103     default:
2104         typestr = "????";
2105         break;
2106     }
2107
2108
2109     myprintf(("attr: type %s mode %d uid %d gid %d fsid %d rdev %d\n",
2110               typestr, (int)attr->va_mode, (int)attr->va_uid,
2111               (int)attr->va_gid, (int)attr->va_fsid, (int)attr->va_rdev));
2112
2113     myprintf(("      fileid %d nlink %d size %d blocksize %d bytes %d\n",
2114               (int)attr->va_fileid, (int)attr->va_nlink, 
2115               (int)attr->va_size,
2116               (int)attr->va_blocksize,(int)attr->va_bytes));
2117     myprintf(("      gen %ld flags %ld vaflags %d\n",
2118               attr->va_gen, attr->va_flags, attr->va_vaflags));
2119     myprintf(("      atime sec %d nsec %d\n",
2120               (int)attr->va_atime.tv_sec, (int)attr->va_atime.tv_nsec));
2121     myprintf(("      mtime sec %d nsec %d\n",
2122               (int)attr->va_mtime.tv_sec, (int)attr->va_mtime.tv_nsec));
2123     myprintf(("      ctime sec %d nsec %d\n",
2124               (int)attr->va_ctime.tv_sec, (int)attr->va_ctime.tv_nsec));
2125 }
2126
2127 /* How to print a ucred */
2128 void
2129 print_cred(cred)
2130         struct ucred *cred;
2131 {
2132
2133         int i;
2134
2135         myprintf(("ref %d\tuid %d\n",cred->cr_ref,cred->cr_uid));
2136
2137         for (i=0; i < cred->cr_ngroups; i++)
2138                 myprintf(("\tgroup %d: (%d)\n",i,cred->cr_groups[i]));
2139         myprintf(("\n"));
2140
2141 }
2142
2143 /*
2144  * Return a vnode for the given fid.
2145  * If no cnode exists for this fid create one and put it
2146  * in a table hashed by fid.Volume and fid.Vnode.  If the cnode for
2147  * this fid is already in the table return it (ref count is
2148  * incremented by coda_find.  The cnode will be flushed from the
2149  * table when coda_inactive calls coda_unsave.
2150  */
2151 struct cnode *
2152 make_coda_node(fid, vfsp, type)
2153      ViceFid *fid; struct mount *vfsp; short type;
2154 {
2155     struct cnode *cp;
2156     int          err;
2157
2158     if ((cp = coda_find(fid)) == NULL) {
2159         struct vnode *vp;
2160         
2161         cp = coda_alloc();
2162         lockinit(&cp->c_lock, PINOD, "cnode", 0, 0);
2163         cp->c_fid = *fid;
2164         
2165         err = getnewvnode(VT_CODA, vfsp, coda_vnodeop_p, &vp);  
2166         if (err) {                                                
2167             panic("coda: getnewvnode returned error %d\n", err);   
2168         }                                                         
2169         vp->v_data = cp;                                          
2170         vp->v_type = type;                                      
2171         cp->c_vnode = vp;                                         
2172         coda_save(cp);
2173         
2174     } else {
2175         vref(CTOV(cp));
2176     }
2177
2178     return cp;
2179 }