2 * Coda: an Experimental Distributed File System
5 * Copyright (c) 1987-1998 Carnegie Mellon University
8 * Permission to use, copy, modify and distribute this software and its
9 * documentation is hereby granted, provided that both the copyright
10 * notice and this permission notice appear in all copies of the
11 * software, derivative works or modified versions, and any portions
12 * thereof, and that both notices appear in supporting documentation, and
13 * that credit is given to Carnegie Mellon University in all documents
14 * and publicity pertaining to direct or indirect use of this code or its
17 * CODA IS AN EXPERIMENTAL SOFTWARE SYSTEM AND IS KNOWN TO HAVE BUGS,
18 * SOME OF WHICH MAY HAVE SERIOUS CONSEQUENCES. CARNEGIE MELLON ALLOWS
19 * FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION. CARNEGIE MELLON
20 * DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER
21 * RESULTING DIRECTLY OR INDIRECTLY FROM THE USE OF THIS SOFTWARE OR OF
22 * ANY DERIVATIVE WORK.
24 * Carnegie Mellon encourages users of this software to return any
25 * improvements or extensions that they make, and to grant Carnegie
26 * Mellon the rights to redistribute these changes without encumbrance.
28 * @(#) src/sys/coda/coda_subr.c,v 1.1.1.1 1998/08/29 21:14:52 rvb Exp $
31 * Mach Operating System
32 * Copyright (c) 1989 Carnegie-Mellon University
33 * All rights reserved. The CMU software License Agreement specifies
34 * the terms and conditions for use and redistribution.
38 * This code was written for the Coda filesystem at Carnegie Mellon
39 * University. Contributers include David Steere, James Kistler, and
44 * 1. Added coda_unmounting to mark all cnodes as being UNMOUNTING. This has to
45 * be done before dounmount is called. Because some of the routines that
46 * dounmount calls before coda_unmounted might try to force flushes to venus.
47 * The vnode pager does this.
48 * 2. coda_unmounting marks all cnodes scanning coda_cache.
49 * 3. cfs_checkunmounting (under DEBUG) checks all cnodes by chasing the vnodes
50 * under the /coda mount point.
51 * 4. coda_cacheprint (under DEBUG) prints names with vnode/cnode address
54 #include <sys/cdefs.h>
55 __FBSDID("$FreeBSD$");
57 #include <sys/param.h>
58 #include <sys/systm.h>
60 #include <sys/malloc.h>
61 #include <sys/mutex.h>
62 #include <sys/mount.h>
64 #include <coda/coda.h>
65 #include <coda/cnode.h>
66 #include <coda/coda_subr.h>
67 #include <coda/coda_namecache.h>
73 struct cnode *coda_freelist = NULL;
74 struct cnode *coda_cache[CODA_CACHESIZE];
76 #define CNODE_NEXT(cp) ((cp)->c_next)
79 #define coda_hash(fid) \
80 (((fid)->Volume + (fid)->Vnode) & (CODA_CACHESIZE-1))
81 #define IS_DIR(cnode) (cnode.Vnode & 0x1)
83 #define coda_hash(fid) (coda_f2i(fid) & (CODA_CACHESIZE-1))
84 #define IS_DIR(cnode) (cnode.opaque[2] & 0x1)
97 coda_freelist = CNODE_NEXT(cp);
101 CODA_ALLOC(cp, struct cnode *, sizeof(struct cnode));
102 /* NetBSD vnodes don't have any Pager info in them ('cause there are
103 no external pagers, duh!) */
104 #define VNODE_VM_INFO_INIT(vp) /* MT */
105 VNODE_VM_INFO_INIT(CTOV(cp));
108 bzero(cp, sizeof (struct cnode));
114 * Deallocate a cnode.
118 register struct cnode *cp;
121 CNODE_NEXT(cp) = coda_freelist;
126 * Put a cnode in the hash table
132 CNODE_NEXT(cp) = coda_cache[coda_hash(&cp->c_fid)];
133 coda_cache[coda_hash(&cp->c_fid)] = cp;
137 * Remove a cnode from the hash table
144 struct cnode *ptrprev = NULL;
146 ptr = coda_cache[coda_hash(&cp->c_fid)];
147 while (ptr != NULL) {
149 if (ptrprev == NULL) {
150 coda_cache[coda_hash(&cp->c_fid)]
153 CNODE_NEXT(ptrprev) = CNODE_NEXT(ptr);
155 CNODE_NEXT(cp) = (struct cnode *)NULL;
160 ptr = CNODE_NEXT(ptr);
165 * Lookup a cnode by fid. If the cnode is dying, it is bogus so skip it.
166 * NOTE: this allows multiple cnodes with same fid -- dcs 1/25/95
174 cp = coda_cache[coda_hash(fid)];
176 if (coda_fid_eq(&(cp->c_fid), fid) &&
177 (!IS_UNMOUNTING(cp)))
188 * coda_kill is called as a side effect to vcopen. To prevent any
189 * cnodes left around from an earlier run of a venus or warden from
190 * causing problems with the new instance, mark any outstanding cnodes
191 * as dying. Future operations on these cnodes should fail (excepting
192 * coda_inactive of course!). Since multiple venii/wardens can be
193 * running, only kill the cnodes for a particular entry in the
194 * coda_mnttbl. -- DCS 12/1/94 */
197 coda_kill(whoIam, dcstat)
198 struct mount *whoIam;
199 enum dc_status dcstat;
205 * Algorithm is as follows:
206 * Second, flush whatever vnodes we can from the name cache.
208 * Finally, step through whatever is left and mark them dying.
209 * This prevents any operation at all.
212 /* This is slightly overkill, but should work. Eventually it'd be
213 * nice to only flush those entries from the namecache that
214 * reference a vnode in this vfs. */
215 coda_nc_flush(dcstat);
217 for (hash = 0; hash < CODA_CACHESIZE; hash++) {
218 for (cp = coda_cache[hash]; cp != NULL; cp = CNODE_NEXT(cp)) {
219 if (CTOV(cp)->v_mount == whoIam) {
221 printf("coda_kill: vp %p, cp %p\n", CTOV(cp), cp);
224 CODADEBUG(CODA_FLUSH,
225 myprintf(("Live cnode fid %s flags %d count %d\n",
226 coda_f2s(&cp->c_fid),
228 vrefcnt(CTOV(cp)))); );
236 * There are two reasons why a cnode may be in use, it may be in the
237 * name cache or it may be executing.
241 enum dc_status dcstat;
246 coda_clstat.ncalls++;
247 coda_clstat.reqs[CODA_FLUSH]++;
249 coda_nc_flush(dcstat); /* flush files from the name cache */
251 for (hash = 0; hash < CODA_CACHESIZE; hash++) {
252 for (cp = coda_cache[hash]; cp != NULL; cp = CNODE_NEXT(cp)) {
253 if (!IS_DIR(cp->c_fid)) /* only files can be executed */
260 * As a debugging measure, print out any cnodes that lived through a
269 for (hash = 0; hash < CODA_CACHESIZE; hash++) {
270 for (cp = coda_cache[hash];
272 cp = CNODE_NEXT(cp)) {
273 myprintf(("Live cnode fid %s count %d\n",
274 coda_f2s(&cp->c_fid), CTOV(cp)->v_usecount));
280 * First, step through all cnodes and mark them unmounting.
281 * NetBSD kernels may try to fsync them now that venus
282 * is dead, which would be a bad thing.
286 coda_unmounting(whoIam)
287 struct mount *whoIam;
292 for (hash = 0; hash < CODA_CACHESIZE; hash++) {
293 for (cp = coda_cache[hash]; cp != NULL; cp = CNODE_NEXT(cp)) {
294 if (CTOV(cp)->v_mount == whoIam) {
295 if (cp->c_flags & (C_LOCKED|C_WANTED)) {
296 printf("coda_unmounting: Unlocking %p\n", cp);
297 cp->c_flags &= ~(C_LOCKED|C_WANTED);
298 wakeup((caddr_t) cp);
300 cp->c_flags |= C_UNMOUNTING;
308 coda_checkunmounting(mp)
311 register struct vnode *vp, *nvp;
313 int count = 0, bad = 0;
316 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp; vp = nvp) {
317 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
318 if (vp->v_mount != mp)
321 if (vp->v_iflag & VI_XLOCK) {
327 if (!(cp->c_flags & C_UNMOUNTING)) {
329 printf("vp %p, cp %p missed\n", vp, cp);
330 cp->c_flags |= C_UNMOUNTING;
338 coda_cacheprint(whoIam)
339 struct mount *whoIam;
345 printf("coda_cacheprint: coda_ctlvp %p, cp %p", coda_ctlvp, VTOC(coda_ctlvp));
346 coda_nc_name(VTOC(coda_ctlvp));
349 for (hash = 0; hash < CODA_CACHESIZE; hash++) {
350 for (cp = coda_cache[hash]; cp != NULL; cp = CNODE_NEXT(cp)) {
351 if (CTOV(cp)->v_mount == whoIam) {
352 printf("coda_cacheprint: vp %p, cp %p", CTOV(cp), cp);
359 printf("coda_cacheprint: count %d\n", count);
364 * There are 6 cases where invalidations occur. The semantics of each
367 * CODA_FLUSH -- flush all entries from the name cache and the cnode cache.
368 * CODA_PURGEUSER -- flush all entries from the name cache for a specific user
369 * This call is a result of token expiration.
371 * The next two are the result of callbacks on a file or directory.
372 * CODA_ZAPDIR -- flush the attributes for the dir from its cnode.
373 * Zap all children of this directory from the namecache.
374 * CODA_ZAPFILE -- flush the attributes for a file.
376 * The fifth is a result of Venus detecting an inconsistent file.
377 * CODA_PURGEFID -- flush the attribute for the file
378 * If it is a dir (odd vnode), purge its
379 * children from the namecache
380 * remove the file from the namecache.
382 * The sixth allows Venus to replace local fids with global ones
383 * during reintegration.
385 * CODA_REPLACE -- replace one CodaFid with another throughout the name cache
388 int handleDownCall(opcode, out)
389 int opcode; union outputArgs *out;
393 /* Handle invalidate requests. */
397 coda_flush(IS_DOWNCALL);
399 CODADEBUG(CODA_FLUSH,coda_testflush();) /* print remaining cnodes */
403 case CODA_PURGEUSER : {
404 coda_clstat.ncalls++;
405 coda_clstat.reqs[CODA_PURGEUSER]++;
407 /* XXX - need to prevent fsync's */
409 coda_nc_purge_user(out->coda_purgeuser.cred.cr_uid, IS_DOWNCALL);
411 coda_nc_purge_user(out->coda_purgeuser.uid, IS_DOWNCALL);
416 case CODA_ZAPFILE : {
420 coda_clstat.ncalls++;
421 coda_clstat.reqs[CODA_ZAPFILE]++;
423 cp = coda_find(&out->coda_zapfile.Fid);
427 cp->c_flags &= ~C_VATTR;
428 ASSERT_VOP_LOCKED(CTOV(cp), "coda HandleDownCall");
429 if (CTOV(cp)->v_vflag & VV_TEXT)
430 error = coda_vmflush(cp);
431 CODADEBUG(CODA_ZAPFILE,
432 myprintf(("zapfile: fid = %s, refcnt = %d, error = %d\n",
433 coda_f2s(&cp->c_fid), CTOV(cp)->v_usecount - 1, error)););
434 if (vrefcnt(CTOV(cp)) == 1) {
435 cp->c_flags |= C_PURGING;
446 coda_clstat.ncalls++;
447 coda_clstat.reqs[CODA_ZAPDIR]++;
449 cp = coda_find(&out->coda_zapdir.Fid);
453 cp->c_flags &= ~C_VATTR;
454 coda_nc_zapParentfid(&out->coda_zapdir.Fid, IS_DOWNCALL);
456 CODADEBUG(CODA_ZAPDIR, myprintf((
457 "zapdir: fid = %s, refcnt = %d\n",
458 coda_f2s(&cp->c_fid), CTOV(cp)->v_usecount - 1)););
459 if (vrefcnt(CTOV(cp)) == 1) {
460 cp->c_flags |= C_PURGING;
468 case CODA_PURGEFID : {
472 coda_clstat.ncalls++;
473 coda_clstat.reqs[CODA_PURGEFID]++;
475 cp = coda_find(&out->coda_purgefid.Fid);
478 if (IS_DIR(out->coda_purgefid.Fid)) { /* Vnode is a directory */
479 coda_nc_zapParentfid(&out->coda_purgefid.Fid,IS_DOWNCALL);
481 cp->c_flags &= ~C_VATTR;
482 coda_nc_zapfid(&out->coda_purgefid.Fid, IS_DOWNCALL);
483 ASSERT_VOP_LOCKED(CTOV(cp), "coda HandleDownCall");
484 if (!(IS_DIR(out->coda_purgefid.Fid))
485 && (CTOV(cp)->v_vflag & VV_TEXT)) {
487 error = coda_vmflush(cp);
489 CODADEBUG(CODA_PURGEFID, myprintf((
490 "purgefid: fid = %s, refcnt = %d, error = %d\n",
491 coda_f2s(&cp->c_fid), CTOV(cp)->v_usecount - 1, error)););
492 if (vrefcnt(CTOV(cp)) == 1) {
493 cp->c_flags |= C_PURGING;
500 case CODA_REPLACE : {
501 struct cnode *cp = NULL;
503 coda_clstat.ncalls++;
504 coda_clstat.reqs[CODA_REPLACE]++;
506 cp = coda_find(&out->coda_replace.OldFid);
508 /* remove the cnode from the hash table, replace the fid, and reinsert */
511 cp->c_fid = out->coda_replace.NewFid;
514 CODADEBUG(CODA_REPLACE, myprintf((
515 "replace: oldfid = %s, newfid = %s, cp = %p\n",
516 coda_f2s(&out->coda_replace.OldFid),
517 coda_f2s(&cp->c_fid), cp));) vrele(CTOV(cp));
522 myprintf(("handleDownCall: unknown opcode %d\n", opcode));
527 /* coda_grab_vnode: lives in either cfs_mach.c or cfs_nbsd.c */
538 * kernel-internal debugging switches
540 void coda_debugon(void)
544 coda_vnop_print_entry = 1;
545 coda_psdev_print_entry = 1;
546 coda_vfsop_print_entry = 1;
549 void coda_debugoff(void)
553 coda_vnop_print_entry = 0;
554 coda_psdev_print_entry = 0;
555 coda_vfsop_print_entry = 0;
559 * Utilities used by both client and server
564 * 3) current test software
565 * 4) main procedure entry points
566 * 5) main procedure exit points
567 * 6) utility procedure entry points
568 * 7) utility procedure exit points
569 * 8) obscure procedure entry points
570 * 9) obscure procedure exit points