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 file system 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 CTOV(cp)->v_usecount)); );
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, 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;
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 if (CTOV(cp)->v_flag & VTEXT)
417 error = coda_vmflush(cp);
418 CODADEBUG(CODA_ZAPFILE, myprintf(("zapfile: fid = (%lx.%lx.%lx),
419 refcnt = %d, error = %d\n",
423 CTOV(cp)->v_usecount - 1, error)););
424 if (CTOV(cp)->v_usecount == 1) {
425 cp->c_flags |= C_PURGING;
436 coda_clstat.ncalls++;
437 coda_clstat.reqs[CODA_ZAPDIR]++;
439 cp = coda_find(&out->coda_zapdir.CodaFid);
443 cp->c_flags &= ~C_VATTR;
444 coda_nc_zapParentfid(&out->coda_zapdir.CodaFid, IS_DOWNCALL);
446 CODADEBUG(CODA_ZAPDIR, myprintf(("zapdir: fid = (%lx.%lx.%lx),
447 refcnt = %d\n",cp->c_fid.Volume,
450 CTOV(cp)->v_usecount - 1)););
451 if (CTOV(cp)->v_usecount == 1) {
452 cp->c_flags |= C_PURGING;
460 case CODA_PURGEFID : {
464 coda_clstat.ncalls++;
465 coda_clstat.reqs[CODA_PURGEFID]++;
467 cp = coda_find(&out->coda_purgefid.CodaFid);
470 if (ODD(out->coda_purgefid.CodaFid.Vnode)) { /* Vnode is a directory */
471 coda_nc_zapParentfid(&out->coda_purgefid.CodaFid,
474 cp->c_flags &= ~C_VATTR;
475 coda_nc_zapfid(&out->coda_purgefid.CodaFid, IS_DOWNCALL);
476 if (!(ODD(out->coda_purgefid.CodaFid.Vnode))
477 && (CTOV(cp)->v_flag & VTEXT)) {
479 error = coda_vmflush(cp);
481 CODADEBUG(CODA_PURGEFID, myprintf(("purgefid: fid = (%lx.%lx.%lx), refcnt = %d, error = %d\n",
482 cp->c_fid.Volume, cp->c_fid.Vnode,
484 CTOV(cp)->v_usecount - 1, error)););
485 if (CTOV(cp)->v_usecount == 1) {
486 cp->c_flags |= C_PURGING;
493 case CODA_REPLACE : {
494 struct cnode *cp = NULL;
496 coda_clstat.ncalls++;
497 coda_clstat.reqs[CODA_REPLACE]++;
499 cp = coda_find(&out->coda_replace.OldFid);
501 /* remove the cnode from the hash table, replace the fid, and reinsert */
504 cp->c_fid = out->coda_replace.NewFid;
507 CODADEBUG(CODA_REPLACE, myprintf(("replace: oldfid = (%lx.%lx.%lx), newfid = (%lx.%lx.%lx), cp = %p\n",
508 out->coda_replace.OldFid.Volume,
509 out->coda_replace.OldFid.Vnode,
510 out->coda_replace.OldFid.Unique,
511 cp->c_fid.Volume, cp->c_fid.Vnode,
512 cp->c_fid.Unique, cp));)
518 myprintf(("handleDownCall: unknown opcode %d\n", opcode));
523 /* coda_grab_vnode: lives in either cfs_mach.c or cfs_nbsd.c */
534 * kernel-internal debugging switches
536 void coda_debugon(void)
540 coda_vnop_print_entry = 1;
541 coda_psdev_print_entry = 1;
542 coda_vfsop_print_entry = 1;
545 void coda_debugoff(void)
549 coda_vnop_print_entry = 0;
550 coda_psdev_print_entry = 0;
551 coda_vfsop_print_entry = 0;
555 * Utilities used by both client and server
560 * 3) current test software
561 * 4) main procedure entry points
562 * 5) main procedure exit points
563 * 6) utility procedure entry points
564 * 7) utility procedure exit points
565 * 8) obscure procedure entry points
566 * 9) obscure procedure exit points