3 * Coda: an Experimental Distributed File System
6 * Copyright (c) 1987-1998 Carnegie Mellon University
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
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.
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.
29 * @(#) src/sys/coda/coda_subr.c,v 1.1.1.1 1998/08/29 21:14:52 rvb Exp $
30 * $Id: coda_subr.c,v 1.6 1998/09/25 17:38:31 rvb Exp $
35 * Mach Operating System
36 * Copyright (c) 1989 Carnegie-Mellon University
37 * All rights reserved. The CMU software License Agreement specifies
38 * the terms and conditions for use and redistribution.
42 * This code was written for the Coda file system at Carnegie Mellon
43 * University. Contributers include David Steere, James Kistler, and
44 * M. Satyanarayanan. */
48 * $Log: coda_subr.c,v $
49 * Revision 1.6 1998/09/25 17:38:31 rvb
50 * Put "stray" printouts under DIAGNOSTIC. Make everything build
51 * with DEBUG on. Add support for lkm. (The macro's don't work
52 * for me; for a good chuckle look at the end of coda_fbsd.c.)
54 * Revision 1.5 1998/09/13 13:57:59 rvb
55 * Finish conversion of cfs -> coda
57 * Revision 1.4 1998/09/11 18:50:17 rvb
58 * All the references to cfs, in symbols, structs, and strings
59 * have been changed to coda. (Same for CFS.)
61 * Revision 1.2 1998/09/02 19:09:53 rvb
64 * Revision 1.1.1.1 1998/08/29 21:14:52 rvb
65 * Very Preliminary Coda
67 * Revision 1.11 1998/08/28 18:12:18 rvb
68 * Now it also works on FreeBSD -current. This code will be
69 * committed to the FreeBSD -current and NetBSD -current
70 * trees. It will then be tailored to the particular platform
71 * by flushing conditional code.
73 * Revision 1.10 1998/08/18 17:05:16 rvb
74 * Don't use __RCSID now
76 * Revision 1.9 1998/08/18 16:31:41 rvb
77 * Sync the code for NetBSD -current; test on 1.3 later
79 * Revision 1.8 98/01/31 20:53:12 rvb
80 * First version that works on FreeBSD 2.2.5
82 * Revision 1.7 98/01/23 11:53:42 rvb
83 * Bring RVB_CODA1_1 to HEAD
85 * Revision 1.6.2.3 98/01/23 11:21:05 rvb
88 * Revision 1.6.2.2 97/12/16 12:40:06 rvb
91 * Revision 1.6.2.1 97/12/06 17:41:21 rvb
92 * Sync with peters coda.h
94 * Revision 1.6 97/12/05 10:39:17 rvb
97 * Revision 1.5.4.8 97/11/26 15:28:58 rvb
98 * Cant make downcall pbuf == union cfs_downcalls yet
100 * Revision 1.5.4.7 97/11/20 11:46:42 rvb
101 * Capture current cfs_venus
103 * Revision 1.5.4.6 97/11/18 10:27:16 rvb
104 * cfs_nbsd.c is DEAD!!!; integrated into cfs_vf/vnops.c
105 * cfs_nb_foo and cfs_foo are joined
107 * Revision 1.5.4.5 97/11/13 22:03:00 rvb
108 * pass2 cfs_NetBSD.h mt
110 * Revision 1.5.4.4 97/11/12 12:09:39 rvb
113 * Revision 1.5.4.3 97/11/06 21:02:38 rvb
114 * first pass at ^c ^z
116 * Revision 1.5.4.2 97/10/29 16:06:27 rvb
119 * Revision 1.5.4.1 97/10/28 23:10:16 rvb
120 * >64Meg; venus can be killed!
122 * Revision 1.5 97/08/05 11:08:17 lily
123 * Removed cfsnc_replace, replaced it with a coda_find, unhash, and
124 * rehash. This fixes a cnode leak and a bug in which the fid is
125 * not actually replaced. (cfs_namecache.c, cfsnc.h, cfs_subr.c)
127 * Revision 1.4 96/12/12 22:10:59 bnoble
128 * Fixed the "downcall invokes venus operation" deadlock in all known cases.
131 * Revision 1.3 1996/12/05 16:20:15 bnoble
132 * Minor debugging aids
134 * Revision 1.2 1996/01/02 16:57:01 bnoble
135 * Added support for Coda MiniCache and raw inode calls (final commit)
137 * Revision 1.1.2.1 1995/12/20 01:57:27 bnoble
138 * Added CODA-specific files
140 * Revision 3.1.1.1 1995/03/04 19:07:59 bnoble
141 * Branch for NetBSD port revisions
143 * Revision 3.1 1995/03/04 19:07:58 bnoble
144 * Bump to major revision 3 to prepare for NetBSD port
146 * Revision 2.8 1995/03/03 17:00:04 dcs
147 * Fixed kernel bug involving sleep and upcalls. Basically if you killed
148 * a job waiting on venus, the venus upcall queues got trashed. Depending
149 * on luck, you could kill the kernel or not.
150 * (mods to cfs_subr.c and cfs_mach.d)
152 * Revision 2.7 95/03/02 22:45:21 dcs
155 * Revision 2.6 95/02/17 16:25:17 dcs
156 * These versions represent several changes:
157 * 1. Allow venus to restart even if outstanding references exist.
158 * 2. Have only one ctlvp per client, as opposed to one per mounted cfs device.d
159 * 3. Allow ody_expand to return many members, not just one.
161 * Revision 2.5 94/11/09 15:56:26 dcs
162 * Had the thread sleeping on the wrong thing!
164 * Revision 2.4 94/10/14 09:57:57 dcs
165 * Made changes 'cause sun4s have braindead compilers
167 * Revision 2.3 94/10/12 16:46:26 dcs
168 * Cleaned kernel/venus interface by removing XDR junk, plus
169 * so cleanup to allow this code to be more easily ported.
171 * Revision 1.2 92/10/27 17:58:22 lily
172 * merge kernel/latest and alpha/src/cfs
174 * Revision 2.4 92/09/30 14:16:26 mja
175 * Incorporated Dave Steere's fix for the GNU-Emacs bug.
176 * Also, included his coda_flush routine in place of the former coda_nc_flush.
179 * Added contributors blurb.
182 * Hack to allow users to keep coda venus calls uninterruptible. THis
183 * basically prevents the Gnu-emacs bug from appearing, in which a call
184 * was being interrupted, and return EINTR, but gnu didn't check for the
185 * error and figured the file was buggered.
188 * Revision 2.3 90/08/10 10:23:20 mrt
189 * Removed include of vm/vm_page.h as it no longer exists.
192 * Revision 2.2 90/07/05 11:26:35 mrt
193 * Initialize name cache on first call to vcopen.
196 * Created for the Coda File System.
199 * Revision 1.5 90/05/31 17:01:35 dcs
200 * Prepare for merge with facilities kernel.
202 * Revision 1.2 90/03/19 15:56:25 dcs
203 * Initialize name cache on first call to vcopen.
205 * Revision 1.1 90/03/15 10:43:26 jjk
211 * 1. Added coda_unmounting to mark all cnodes as being UNMOUNTING. This has to
212 * be done before dounmount is called. Because some of the routines that
213 * dounmount calls before coda_unmounted might try to force flushes to venus.
214 * The vnode pager does this.
215 * 2. coda_unmounting marks all cnodes scanning coda_cache.
216 * 3. cfs_checkunmounting (under DEBUG) checks all cnodes by chasing the vnodes
217 * under the /coda mount point.
218 * 4. coda_cacheprint (under DEBUG) prints names with vnode/cnode address
227 #include <sys/param.h>
228 #include <sys/systm.h>
229 #include <sys/proc.h>
230 #include <sys/malloc.h>
231 #include <sys/select.h>
232 #include <sys/mount.h>
234 #include <coda/coda.h>
235 #include <coda/cnode.h>
236 #include <coda/coda_subr.h>
237 #include <coda/coda_namecache.h>
243 struct cnode *coda_freelist = NULL;
244 struct cnode *coda_cache[CODA_CACHESIZE];
246 #define coda_hash(fid) (((fid)->Volume + (fid)->Vnode) & (CODA_CACHESIZE-1))
247 #define CNODE_NEXT(cp) ((cp)->c_next)
248 #define ODD(vnode) ((vnode) & 0x1)
260 coda_freelist = CNODE_NEXT(cp);
264 CODA_ALLOC(cp, struct cnode *, sizeof(struct cnode));
265 /* NetBSD vnodes don't have any Pager info in them ('cause there are
266 no external pagers, duh!) */
267 #define VNODE_VM_INFO_INIT(vp) /* MT */
268 VNODE_VM_INFO_INIT(CTOV(cp));
271 bzero(cp, sizeof (struct cnode));
277 * Deallocate a cnode.
281 register struct cnode *cp;
284 CNODE_NEXT(cp) = coda_freelist;
289 * Put a cnode in the hash table
295 CNODE_NEXT(cp) = coda_cache[coda_hash(&cp->c_fid)];
296 coda_cache[coda_hash(&cp->c_fid)] = cp;
300 * Remove a cnode from the hash table
307 struct cnode *ptrprev = NULL;
309 ptr = coda_cache[coda_hash(&cp->c_fid)];
310 while (ptr != NULL) {
312 if (ptrprev == NULL) {
313 coda_cache[coda_hash(&cp->c_fid)]
316 CNODE_NEXT(ptrprev) = CNODE_NEXT(ptr);
318 CNODE_NEXT(cp) = (struct cnode *)NULL;
323 ptr = CNODE_NEXT(ptr);
328 * Lookup a cnode by fid. If the cnode is dying, it is bogus so skip it.
329 * NOTE: this allows multiple cnodes with same fid -- dcs 1/25/95
337 cp = coda_cache[coda_hash(fid)];
339 if ((cp->c_fid.Vnode == fid->Vnode) &&
340 (cp->c_fid.Volume == fid->Volume) &&
341 (cp->c_fid.Unique == fid->Unique) &&
342 (!IS_UNMOUNTING(cp)))
353 * coda_kill is called as a side effect to vcopen. To prevent any
354 * cnodes left around from an earlier run of a venus or warden from
355 * causing problems with the new instance, mark any outstanding cnodes
356 * as dying. Future operations on these cnodes should fail (excepting
357 * coda_inactive of course!). Since multiple venii/wardens can be
358 * running, only kill the cnodes for a particular entry in the
359 * coda_mnttbl. -- DCS 12/1/94 */
362 coda_kill(whoIam, dcstat)
363 struct mount *whoIam;
364 enum dc_status dcstat;
370 * Algorithm is as follows:
371 * Second, flush whatever vnodes we can from the name cache.
373 * Finally, step through whatever is left and mark them dying.
374 * This prevents any operation at all.
377 /* This is slightly overkill, but should work. Eventually it'd be
378 * nice to only flush those entries from the namecache that
379 * reference a vnode in this vfs. */
380 coda_nc_flush(dcstat);
382 for (hash = 0; hash < CODA_CACHESIZE; hash++) {
383 for (cp = coda_cache[hash]; cp != NULL; cp = CNODE_NEXT(cp)) {
384 if (CTOV(cp)->v_mount == whoIam) {
386 printf("coda_kill: vp %p, cp %p\n", CTOV(cp), cp);
389 CODADEBUG(CODA_FLUSH,
390 myprintf(("Live cnode fid %lx.%lx.%lx flags %d count %d\n",
395 CTOV(cp)->v_usecount)); );
403 * There are two reasons why a cnode may be in use, it may be in the
404 * name cache or it may be executing.
408 enum dc_status dcstat;
413 coda_clstat.ncalls++;
414 coda_clstat.reqs[CODA_FLUSH]++;
416 coda_nc_flush(dcstat); /* flush files from the name cache */
418 for (hash = 0; hash < CODA_CACHESIZE; hash++) {
419 for (cp = coda_cache[hash]; cp != NULL; cp = CNODE_NEXT(cp)) {
420 if (!ODD(cp->c_fid.Vnode)) /* only files can be executed */
427 * As a debugging measure, print out any cnodes that lived through a
436 for (hash = 0; hash < CODA_CACHESIZE; hash++) {
437 for (cp = coda_cache[hash];
439 cp = CNODE_NEXT(cp)) {
440 myprintf(("Live cnode fid %lx.%lx.%lx count %d\n",
441 (cp->c_fid).Volume,(cp->c_fid).Vnode,
442 (cp->c_fid).Unique, CTOV(cp)->v_usecount));
448 * First, step through all cnodes and mark them unmounting.
449 * NetBSD kernels may try to fsync them now that venus
450 * is dead, which would be a bad thing.
454 coda_unmounting(whoIam)
455 struct mount *whoIam;
460 for (hash = 0; hash < CODA_CACHESIZE; hash++) {
461 for (cp = coda_cache[hash]; cp != NULL; cp = CNODE_NEXT(cp)) {
462 if (CTOV(cp)->v_mount == whoIam) {
463 if (cp->c_flags & (C_LOCKED|C_WANTED)) {
464 printf("coda_unmounting: Unlocking %p\n", cp);
465 cp->c_flags &= ~(C_LOCKED|C_WANTED);
466 wakeup((caddr_t) cp);
468 cp->c_flags |= C_UNMOUNTING;
476 coda_checkunmounting(mp)
479 register struct vnode *vp, *nvp;
481 int count = 0, bad = 0;
483 for (vp = mp->mnt_vnodelist.lh_first; vp; vp = nvp) {
484 if (vp->v_mount != mp)
486 nvp = vp->v_mntvnodes.le_next;
489 if (!(cp->c_flags & C_UNMOUNTING)) {
491 printf("vp %p, cp %p missed\n", vp, cp);
492 cp->c_flags |= C_UNMOUNTING;
498 coda_cacheprint(whoIam)
499 struct mount *whoIam;
505 printf("coda_cacheprint: coda_ctlvp %p, cp %p", coda_ctlvp, VTOC(coda_ctlvp));
506 coda_nc_name(VTOC(coda_ctlvp));
509 for (hash = 0; hash < CODA_CACHESIZE; hash++) {
510 for (cp = coda_cache[hash]; cp != NULL; cp = CNODE_NEXT(cp)) {
511 if (CTOV(cp)->v_mount == whoIam) {
512 printf("coda_cacheprint: vp %p, cp %p", CTOV(cp), cp);
519 printf("coda_cacheprint: count %d\n", count);
524 * There are 6 cases where invalidations occur. The semantics of each
527 * CODA_FLUSH -- flush all entries from the name cache and the cnode cache.
528 * CODA_PURGEUSER -- flush all entries from the name cache for a specific user
529 * This call is a result of token expiration.
531 * The next two are the result of callbacks on a file or directory.
532 * CODA_ZAPDIR -- flush the attributes for the dir from its cnode.
533 * Zap all children of this directory from the namecache.
534 * CODA_ZAPFILE -- flush the attributes for a file.
536 * The fifth is a result of Venus detecting an inconsistent file.
537 * CODA_PURGEFID -- flush the attribute for the file
538 * If it is a dir (odd vnode), purge its
539 * children from the namecache
540 * remove the file from the namecache.
542 * The sixth allows Venus to replace local fids with global ones
543 * during reintegration.
545 * CODA_REPLACE -- replace one ViceFid with another throughout the name cache
548 int handleDownCall(opcode, out)
549 int opcode; union outputArgs *out;
553 /* Handle invalidate requests. */
557 coda_flush(IS_DOWNCALL);
559 CODADEBUG(CODA_FLUSH,coda_testflush();) /* print remaining cnodes */
563 case CODA_PURGEUSER : {
564 coda_clstat.ncalls++;
565 coda_clstat.reqs[CODA_PURGEUSER]++;
567 /* XXX - need to prevent fsync's */
568 coda_nc_purge_user(out->coda_purgeuser.cred.cr_uid, IS_DOWNCALL);
572 case CODA_ZAPFILE : {
576 coda_clstat.ncalls++;
577 coda_clstat.reqs[CODA_ZAPFILE]++;
579 cp = coda_find(&out->coda_zapfile.CodaFid);
583 cp->c_flags &= ~C_VATTR;
584 if (CTOV(cp)->v_flag & VTEXT)
585 error = coda_vmflush(cp);
586 CODADEBUG(CODA_ZAPFILE, myprintf(("zapfile: fid = (%lx.%lx.%lx),
587 refcnt = %d, error = %d\n",
591 CTOV(cp)->v_usecount - 1, error)););
592 if (CTOV(cp)->v_usecount == 1) {
593 cp->c_flags |= C_PURGING;
604 coda_clstat.ncalls++;
605 coda_clstat.reqs[CODA_ZAPDIR]++;
607 cp = coda_find(&out->coda_zapdir.CodaFid);
611 cp->c_flags &= ~C_VATTR;
612 coda_nc_zapParentfid(&out->coda_zapdir.CodaFid, IS_DOWNCALL);
614 CODADEBUG(CODA_ZAPDIR, myprintf(("zapdir: fid = (%lx.%lx.%lx),
615 refcnt = %d\n",cp->c_fid.Volume,
618 CTOV(cp)->v_usecount - 1)););
619 if (CTOV(cp)->v_usecount == 1) {
620 cp->c_flags |= C_PURGING;
628 case CODA_ZAPVNODE : {
629 coda_clstat.ncalls++;
630 coda_clstat.reqs[CODA_ZAPVNODE]++;
632 myprintf(("CODA_ZAPVNODE: Called, but uniplemented\n"));
634 * Not that below we must really translate the returned coda_cred to
635 * a netbsd cred. This is a bit muddled at present and the cfsnc_zapnode
636 * is further unimplemented, so punt!
637 * I suppose we could use just the uid.
639 /* coda_nc_zapvnode(&out->coda_zapvnode.VFid, &out->coda_zapvnode.cred,
644 case CODA_PURGEFID : {
648 coda_clstat.ncalls++;
649 coda_clstat.reqs[CODA_PURGEFID]++;
651 cp = coda_find(&out->coda_purgefid.CodaFid);
654 if (ODD(out->coda_purgefid.CodaFid.Vnode)) { /* Vnode is a directory */
655 coda_nc_zapParentfid(&out->coda_purgefid.CodaFid,
658 cp->c_flags &= ~C_VATTR;
659 coda_nc_zapfid(&out->coda_purgefid.CodaFid, IS_DOWNCALL);
660 if (!(ODD(out->coda_purgefid.CodaFid.Vnode))
661 && (CTOV(cp)->v_flag & VTEXT)) {
663 error = coda_vmflush(cp);
665 CODADEBUG(CODA_PURGEFID, myprintf(("purgefid: fid = (%lx.%lx.%lx), refcnt = %d, error = %d\n",
666 cp->c_fid.Volume, cp->c_fid.Vnode,
668 CTOV(cp)->v_usecount - 1, error)););
669 if (CTOV(cp)->v_usecount == 1) {
670 cp->c_flags |= C_PURGING;
677 case CODA_REPLACE : {
678 struct cnode *cp = NULL;
680 coda_clstat.ncalls++;
681 coda_clstat.reqs[CODA_REPLACE]++;
683 cp = coda_find(&out->coda_replace.OldFid);
685 /* remove the cnode from the hash table, replace the fid, and reinsert */
688 cp->c_fid = out->coda_replace.NewFid;
691 CODADEBUG(CODA_REPLACE, myprintf(("replace: oldfid = (%lx.%lx.%lx), newfid = (%lx.%lx.%lx), cp = %p\n",
692 out->coda_replace.OldFid.Volume,
693 out->coda_replace.OldFid.Vnode,
694 out->coda_replace.OldFid.Unique,
695 cp->c_fid.Volume, cp->c_fid.Vnode,
696 cp->c_fid.Unique, cp));)
702 myprintf(("handleDownCall: unknown opcode %d\n", opcode));
707 /* coda_grab_vnode: lives in either cfs_mach.c or cfs_nbsd.c */
718 * kernel-internal debugging switches
720 void coda_debugon(void)
724 coda_vnop_print_entry = 1;
725 coda_psdev_print_entry = 1;
726 coda_vfsop_print_entry = 1;
729 void coda_debugoff(void)
733 coda_vnop_print_entry = 0;
734 coda_psdev_print_entry = 0;
735 coda_vfsop_print_entry = 0;
739 * Utilities used by both client and server
744 * 3) current test software
745 * 4) main procedure entry points
746 * 5) main procedure exit points
747 * 6) utility procedure entry points
748 * 7) utility procedure exit points
749 * 8) obscure procedure entry points
750 * 9) obscure procedure exit points