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 $
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 filesystem at Carnegie Mellon
43 * University. Contributers include David Steere, James Kistler, and
48 * 1. Added coda_unmounting to mark all cnodes as being UNMOUNTING. This has to
49 * be done before dounmount is called. Because some of the routines that
50 * dounmount calls before coda_unmounted might try to force flushes to venus.
51 * The vnode pager does this.
52 * 2. coda_unmounting marks all cnodes scanning coda_cache.
53 * 3. cfs_checkunmounting (under DEBUG) checks all cnodes by chasing the vnodes
54 * under the /coda mount point.
55 * 4. coda_cacheprint (under DEBUG) prints names with vnode/cnode address
60 #include <sys/param.h>
61 #include <sys/systm.h>
63 #include <sys/malloc.h>
64 #include <sys/mount.h>
66 #include <coda/coda.h>
67 #include <coda/cnode.h>
68 #include <coda/coda_subr.h>
69 #include <coda/coda_namecache.h>
75 struct cnode *coda_freelist = NULL;
76 struct cnode *coda_cache[CODA_CACHESIZE];
78 #define coda_hash(fid) (((fid)->Volume + (fid)->Vnode) & (CODA_CACHESIZE-1))
79 #define CNODE_NEXT(cp) ((cp)->c_next)
80 #define ODD(vnode) ((vnode) & 0x1)
92 coda_freelist = CNODE_NEXT(cp);
96 CODA_ALLOC(cp, struct cnode *, sizeof(struct cnode));
97 /* NetBSD vnodes don't have any Pager info in them ('cause there are
98 no external pagers, duh!) */
99 #define VNODE_VM_INFO_INIT(vp) /* MT */
100 VNODE_VM_INFO_INIT(CTOV(cp));
103 bzero(cp, sizeof (struct cnode));
109 * Deallocate a cnode.
113 register struct cnode *cp;
116 CNODE_NEXT(cp) = coda_freelist;
121 * Put a cnode in the hash table
127 CNODE_NEXT(cp) = coda_cache[coda_hash(&cp->c_fid)];
128 coda_cache[coda_hash(&cp->c_fid)] = cp;
132 * Remove a cnode from the hash table
139 struct cnode *ptrprev = NULL;
141 ptr = coda_cache[coda_hash(&cp->c_fid)];
142 while (ptr != NULL) {
144 if (ptrprev == NULL) {
145 coda_cache[coda_hash(&cp->c_fid)]
148 CNODE_NEXT(ptrprev) = CNODE_NEXT(ptr);
150 CNODE_NEXT(cp) = (struct cnode *)NULL;
155 ptr = CNODE_NEXT(ptr);
160 * Lookup a cnode by fid. If the cnode is dying, it is bogus so skip it.
161 * NOTE: this allows multiple cnodes with same fid -- dcs 1/25/95
169 cp = coda_cache[coda_hash(fid)];
171 if ((cp->c_fid.Vnode == fid->Vnode) &&
172 (cp->c_fid.Volume == fid->Volume) &&
173 (cp->c_fid.Unique == fid->Unique) &&
174 (!IS_UNMOUNTING(cp)))
185 * coda_kill is called as a side effect to vcopen. To prevent any
186 * cnodes left around from an earlier run of a venus or warden from
187 * causing problems with the new instance, mark any outstanding cnodes
188 * as dying. Future operations on these cnodes should fail (excepting
189 * coda_inactive of course!). Since multiple venii/wardens can be
190 * running, only kill the cnodes for a particular entry in the
191 * coda_mnttbl. -- DCS 12/1/94 */
194 coda_kill(whoIam, dcstat)
195 struct mount *whoIam;
196 enum dc_status dcstat;
202 * Algorithm is as follows:
203 * Second, flush whatever vnodes we can from the name cache.
205 * Finally, step through whatever is left and mark them dying.
206 * This prevents any operation at all.
209 /* This is slightly overkill, but should work. Eventually it'd be
210 * nice to only flush those entries from the namecache that
211 * reference a vnode in this vfs. */
212 coda_nc_flush(dcstat);
214 for (hash = 0; hash < CODA_CACHESIZE; hash++) {
215 for (cp = coda_cache[hash]; cp != NULL; cp = CNODE_NEXT(cp)) {
216 if (CTOV(cp)->v_mount == whoIam) {
218 printf("coda_kill: vp %p, cp %p\n", CTOV(cp), cp);
221 CODADEBUG(CODA_FLUSH,
222 myprintf(("Live cnode fid %lx.%lx.%lx flags %d count %d\n",
227 vrefcnt(CTOV(cp)))); );
235 * There are two reasons why a cnode may be in use, it may be in the
236 * name cache or it may be executing.
240 enum dc_status dcstat;
245 coda_clstat.ncalls++;
246 coda_clstat.reqs[CODA_FLUSH]++;
248 coda_nc_flush(dcstat); /* flush files from the name cache */
250 for (hash = 0; hash < CODA_CACHESIZE; hash++) {
251 for (cp = coda_cache[hash]; cp != NULL; cp = CNODE_NEXT(cp)) {
252 if (!ODD(cp->c_fid.Vnode)) /* only files can be executed */
259 * As a debugging measure, print out any cnodes that lived through a
268 for (hash = 0; hash < CODA_CACHESIZE; hash++) {
269 for (cp = coda_cache[hash];
271 cp = CNODE_NEXT(cp)) {
272 myprintf(("Live cnode fid %lx.%lx.%lx count %d\n",
273 (cp->c_fid).Volume,(cp->c_fid).Vnode,
274 (cp->c_fid).Unique, vrefcnt(CTOV(cp))));
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;
315 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp; vp = nvp) {
316 if (vp->v_mount != mp)
318 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
321 if (!(cp->c_flags & C_UNMOUNTING)) {
323 printf("vp %p, cp %p missed\n", vp, cp);
324 cp->c_flags |= C_UNMOUNTING;
330 coda_cacheprint(whoIam)
331 struct mount *whoIam;
337 printf("coda_cacheprint: coda_ctlvp %p, cp %p", coda_ctlvp, VTOC(coda_ctlvp));
338 coda_nc_name(VTOC(coda_ctlvp));
341 for (hash = 0; hash < CODA_CACHESIZE; hash++) {
342 for (cp = coda_cache[hash]; cp != NULL; cp = CNODE_NEXT(cp)) {
343 if (CTOV(cp)->v_mount == whoIam) {
344 printf("coda_cacheprint: vp %p, cp %p", CTOV(cp), cp);
351 printf("coda_cacheprint: count %d\n", count);
356 * There are 6 cases where invalidations occur. The semantics of each
359 * CODA_FLUSH -- flush all entries from the name cache and the cnode cache.
360 * CODA_PURGEUSER -- flush all entries from the name cache for a specific user
361 * This call is a result of token expiration.
363 * The next two are the result of callbacks on a file or directory.
364 * CODA_ZAPDIR -- flush the attributes for the dir from its cnode.
365 * Zap all children of this directory from the namecache.
366 * CODA_ZAPFILE -- flush the attributes for a file.
368 * The fifth is a result of Venus detecting an inconsistent file.
369 * CODA_PURGEFID -- flush the attribute for the file
370 * If it is a dir (odd vnode), purge its
371 * children from the namecache
372 * remove the file from the namecache.
374 * The sixth allows Venus to replace local fids with global ones
375 * during reintegration.
377 * CODA_REPLACE -- replace one ViceFid with another throughout the name cache
380 int handleDownCall(opcode, out)
381 int opcode; union outputArgs *out;
385 /* Handle invalidate requests. */
389 coda_flush(IS_DOWNCALL);
391 CODADEBUG(CODA_FLUSH,coda_testflush();) /* print remaining cnodes */
395 case CODA_PURGEUSER : {
396 coda_clstat.ncalls++;
397 coda_clstat.reqs[CODA_PURGEUSER]++;
399 /* XXX - need to prevent fsync's */
400 coda_nc_purge_user(out->coda_purgeuser.cred.cr_uid, IS_DOWNCALL);
404 case CODA_ZAPFILE : {
408 coda_clstat.ncalls++;
409 coda_clstat.reqs[CODA_ZAPFILE]++;
411 cp = coda_find(&out->coda_zapfile.CodaFid);
415 cp->c_flags &= ~C_VATTR;
416 ASSERT_VOP_LOCKED(CTOV(cp), "coda HandleDownCall");
417 if (CTOV(cp)->v_vflag & VV_TEXT)
418 error = coda_vmflush(cp);
419 CODADEBUG(CODA_ZAPFILE, myprintf((
420 "zapfile: fid = (%lx.%lx.%lx), refcnt = %d, error = %d\n",
421 cp->c_fid.Volume, cp->c_fid.Vnode, cp->c_fid.Unique,
422 vrefcnt(CTOV(cp)) - 1, error)););
423 if (vrefcnt(CTOV(cp)) == 1) {
424 cp->c_flags |= C_PURGING;
435 coda_clstat.ncalls++;
436 coda_clstat.reqs[CODA_ZAPDIR]++;
438 cp = coda_find(&out->coda_zapdir.CodaFid);
442 cp->c_flags &= ~C_VATTR;
443 coda_nc_zapParentfid(&out->coda_zapdir.CodaFid, IS_DOWNCALL);
445 CODADEBUG(CODA_ZAPDIR, myprintf((
446 "zapdir: fid = (%lx.%lx.%lx), refcnt = %d\n",
447 cp->c_fid.Volume, cp->c_fid.Vnode, cp->c_fid.Unique,
448 vrefcnt(CTOV(cp)) - 1)););
449 if (vrefcnt(CTOV(cp)) == 1) {
450 cp->c_flags |= C_PURGING;
458 case CODA_PURGEFID : {
462 coda_clstat.ncalls++;
463 coda_clstat.reqs[CODA_PURGEFID]++;
465 cp = coda_find(&out->coda_purgefid.CodaFid);
468 if (ODD(out->coda_purgefid.CodaFid.Vnode)) { /* Vnode is a directory */
469 coda_nc_zapParentfid(&out->coda_purgefid.CodaFid,
472 cp->c_flags &= ~C_VATTR;
473 coda_nc_zapfid(&out->coda_purgefid.CodaFid, IS_DOWNCALL);
474 ASSERT_VOP_LOCKED(CTOV(cp), "coda HandleDownCall");
475 if (!(ODD(out->coda_purgefid.CodaFid.Vnode))
476 && (CTOV(cp)->v_vflag & VV_TEXT)) {
478 error = coda_vmflush(cp);
480 CODADEBUG(CODA_PURGEFID, myprintf(("purgefid: fid = (%lx.%lx.%lx), refcnt = %d, error = %d\n",
481 cp->c_fid.Volume, cp->c_fid.Vnode,
483 vrefcnt(CTOV(cp)) - 1, error)););
484 if (vrefcnt(CTOV(cp)) == 1) {
485 cp->c_flags |= C_PURGING;
492 case CODA_REPLACE : {
493 struct cnode *cp = NULL;
495 coda_clstat.ncalls++;
496 coda_clstat.reqs[CODA_REPLACE]++;
498 cp = coda_find(&out->coda_replace.OldFid);
500 /* remove the cnode from the hash table, replace the fid, and reinsert */
503 cp->c_fid = out->coda_replace.NewFid;
506 CODADEBUG(CODA_REPLACE, myprintf(("replace: oldfid = (%lx.%lx.%lx), newfid = (%lx.%lx.%lx), cp = %p\n",
507 out->coda_replace.OldFid.Volume,
508 out->coda_replace.OldFid.Vnode,
509 out->coda_replace.OldFid.Unique,
510 cp->c_fid.Volume, cp->c_fid.Vnode,
511 cp->c_fid.Unique, cp));)
517 myprintf(("handleDownCall: unknown opcode %d\n", opcode));
522 /* coda_grab_vnode: lives in either cfs_mach.c or cfs_nbsd.c */
533 * kernel-internal debugging switches
535 void coda_debugon(void)
539 coda_vnop_print_entry = 1;
540 coda_psdev_print_entry = 1;
541 coda_vfsop_print_entry = 1;
544 void coda_debugoff(void)
548 coda_vnop_print_entry = 0;
549 coda_psdev_print_entry = 0;
550 coda_vfsop_print_entry = 0;
554 * Utilities used by both client and server
559 * 3) current test software
560 * 4) main procedure entry points
561 * 5) main procedure exit points
562 * 6) utility procedure entry points
563 * 7) utility procedure exit points
564 * 8) obscure procedure entry points
565 * 9) obscure procedure exit points