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 * $FreeBSD: src/sys/coda/coda_subr.c,v 1.11.2.1 2001/10/25 19:18:51 dillon 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/select.h>
65 #include <sys/mount.h>
67 #include <coda/coda.h>
68 #include <coda/cnode.h>
69 #include <coda/coda_subr.h>
70 #include <coda/coda_namecache.h>
76 struct cnode *coda_freelist = NULL;
77 struct cnode *coda_cache[CODA_CACHESIZE];
79 #define coda_hash(fid) (((fid)->Volume + (fid)->Vnode) & (CODA_CACHESIZE-1))
80 #define CNODE_NEXT(cp) ((cp)->c_next)
81 #define ODD(vnode) ((vnode) & 0x1)
93 coda_freelist = CNODE_NEXT(cp);
97 CODA_ALLOC(cp, struct cnode *, sizeof(struct cnode));
98 /* NetBSD vnodes don't have any Pager info in them ('cause there are
99 no external pagers, duh!) */
100 #define VNODE_VM_INFO_INIT(vp) /* MT */
101 VNODE_VM_INFO_INIT(CTOV(cp));
104 bzero(cp, sizeof (struct cnode));
110 * Deallocate a cnode.
114 register struct cnode *cp;
117 CNODE_NEXT(cp) = coda_freelist;
122 * Put a cnode in the hash table
128 CNODE_NEXT(cp) = coda_cache[coda_hash(&cp->c_fid)];
129 coda_cache[coda_hash(&cp->c_fid)] = cp;
133 * Remove a cnode from the hash table
140 struct cnode *ptrprev = NULL;
142 ptr = coda_cache[coda_hash(&cp->c_fid)];
143 while (ptr != NULL) {
145 if (ptrprev == NULL) {
146 coda_cache[coda_hash(&cp->c_fid)]
149 CNODE_NEXT(ptrprev) = CNODE_NEXT(ptr);
151 CNODE_NEXT(cp) = (struct cnode *)NULL;
156 ptr = CNODE_NEXT(ptr);
161 * Lookup a cnode by fid. If the cnode is dying, it is bogus so skip it.
162 * NOTE: this allows multiple cnodes with same fid -- dcs 1/25/95
170 cp = coda_cache[coda_hash(fid)];
172 if ((cp->c_fid.Vnode == fid->Vnode) &&
173 (cp->c_fid.Volume == fid->Volume) &&
174 (cp->c_fid.Unique == fid->Unique) &&
175 (!IS_UNMOUNTING(cp)))
186 * coda_kill is called as a side effect to vcopen. To prevent any
187 * cnodes left around from an earlier run of a venus or warden from
188 * causing problems with the new instance, mark any outstanding cnodes
189 * as dying. Future operations on these cnodes should fail (excepting
190 * coda_inactive of course!). Since multiple venii/wardens can be
191 * running, only kill the cnodes for a particular entry in the
192 * coda_mnttbl. -- DCS 12/1/94 */
195 coda_kill(whoIam, dcstat)
196 struct mount *whoIam;
197 enum dc_status dcstat;
203 * Algorithm is as follows:
204 * Second, flush whatever vnodes we can from the name cache.
206 * Finally, step through whatever is left and mark them dying.
207 * This prevents any operation at all.
210 /* This is slightly overkill, but should work. Eventually it'd be
211 * nice to only flush those entries from the namecache that
212 * reference a vnode in this vfs. */
213 coda_nc_flush(dcstat);
215 for (hash = 0; hash < CODA_CACHESIZE; hash++) {
216 for (cp = coda_cache[hash]; cp != NULL; cp = CNODE_NEXT(cp)) {
217 if (CTOV(cp)->v_mount == whoIam) {
219 printf("coda_kill: vp %p, cp %p\n", CTOV(cp), cp);
222 CODADEBUG(CODA_FLUSH,
223 myprintf(("Live cnode fid %lx.%lx.%lx flags %d count %d\n",
228 CTOV(cp)->v_usecount)); );
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 (!ODD(cp->c_fid.Vnode)) /* 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 %lx.%lx.%lx count %d\n",
274 (cp->c_fid).Volume,(cp->c_fid).Vnode,
275 (cp->c_fid).Unique, CTOV(cp)->v_usecount));
281 * First, step through all cnodes and mark them unmounting.
282 * NetBSD kernels may try to fsync them now that venus
283 * is dead, which would be a bad thing.
287 coda_unmounting(whoIam)
288 struct mount *whoIam;
293 for (hash = 0; hash < CODA_CACHESIZE; hash++) {
294 for (cp = coda_cache[hash]; cp != NULL; cp = CNODE_NEXT(cp)) {
295 if (CTOV(cp)->v_mount == whoIam) {
296 if (cp->c_flags & (C_LOCKED|C_WANTED)) {
297 printf("coda_unmounting: Unlocking %p\n", cp);
298 cp->c_flags &= ~(C_LOCKED|C_WANTED);
299 wakeup((caddr_t) cp);
301 cp->c_flags |= C_UNMOUNTING;
309 coda_checkunmounting(mp)
312 register struct vnode *vp, *nvp;
314 int count = 0, bad = 0;
316 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp; vp = nvp) {
317 if (vp->v_mount != mp)
319 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
322 if (!(cp->c_flags & C_UNMOUNTING)) {
324 printf("vp %p, cp %p missed\n", vp, cp);
325 cp->c_flags |= C_UNMOUNTING;
331 coda_cacheprint(whoIam)
332 struct mount *whoIam;
338 printf("coda_cacheprint: coda_ctlvp %p, cp %p", coda_ctlvp, VTOC(coda_ctlvp));
339 coda_nc_name(VTOC(coda_ctlvp));
342 for (hash = 0; hash < CODA_CACHESIZE; hash++) {
343 for (cp = coda_cache[hash]; cp != NULL; cp = CNODE_NEXT(cp)) {
344 if (CTOV(cp)->v_mount == whoIam) {
345 printf("coda_cacheprint: vp %p, cp %p", CTOV(cp), cp);
352 printf("coda_cacheprint: count %d\n", count);
357 * There are 6 cases where invalidations occur. The semantics of each
360 * CODA_FLUSH -- flush all entries from the name cache and the cnode cache.
361 * CODA_PURGEUSER -- flush all entries from the name cache for a specific user
362 * This call is a result of token expiration.
364 * The next two are the result of callbacks on a file or directory.
365 * CODA_ZAPDIR -- flush the attributes for the dir from its cnode.
366 * Zap all children of this directory from the namecache.
367 * CODA_ZAPFILE -- flush the attributes for a file.
369 * The fifth is a result of Venus detecting an inconsistent file.
370 * CODA_PURGEFID -- flush the attribute for the file
371 * If it is a dir (odd vnode), purge its
372 * children from the namecache
373 * remove the file from the namecache.
375 * The sixth allows Venus to replace local fids with global ones
376 * during reintegration.
378 * CODA_REPLACE -- replace one ViceFid with another throughout the name cache
381 int handleDownCall(opcode, out)
382 int opcode; union outputArgs *out;
386 /* Handle invalidate requests. */
390 coda_flush(IS_DOWNCALL);
392 CODADEBUG(CODA_FLUSH,coda_testflush();) /* print remaining cnodes */
396 case CODA_PURGEUSER : {
397 coda_clstat.ncalls++;
398 coda_clstat.reqs[CODA_PURGEUSER]++;
400 /* XXX - need to prevent fsync's */
401 coda_nc_purge_user(out->coda_purgeuser.cred.cr_uid, IS_DOWNCALL);
405 case CODA_ZAPFILE : {
409 coda_clstat.ncalls++;
410 coda_clstat.reqs[CODA_ZAPFILE]++;
412 cp = coda_find(&out->coda_zapfile.CodaFid);
416 cp->c_flags &= ~C_VATTR;
417 if (CTOV(cp)->v_flag & VTEXT)
418 error = coda_vmflush(cp);
419 CODADEBUG(CODA_ZAPFILE, myprintf(("zapfile: fid = (%lx.%lx.%lx),
420 refcnt = %d, error = %d\n",
424 CTOV(cp)->v_usecount - 1, error)););
425 if (CTOV(cp)->v_usecount == 1) {
426 cp->c_flags |= C_PURGING;
437 coda_clstat.ncalls++;
438 coda_clstat.reqs[CODA_ZAPDIR]++;
440 cp = coda_find(&out->coda_zapdir.CodaFid);
444 cp->c_flags &= ~C_VATTR;
445 coda_nc_zapParentfid(&out->coda_zapdir.CodaFid, IS_DOWNCALL);
447 CODADEBUG(CODA_ZAPDIR, myprintf(("zapdir: fid = (%lx.%lx.%lx),
448 refcnt = %d\n",cp->c_fid.Volume,
451 CTOV(cp)->v_usecount - 1)););
452 if (CTOV(cp)->v_usecount == 1) {
453 cp->c_flags |= C_PURGING;
461 case CODA_PURGEFID : {
465 coda_clstat.ncalls++;
466 coda_clstat.reqs[CODA_PURGEFID]++;
468 cp = coda_find(&out->coda_purgefid.CodaFid);
471 if (ODD(out->coda_purgefid.CodaFid.Vnode)) { /* Vnode is a directory */
472 coda_nc_zapParentfid(&out->coda_purgefid.CodaFid,
475 cp->c_flags &= ~C_VATTR;
476 coda_nc_zapfid(&out->coda_purgefid.CodaFid, IS_DOWNCALL);
477 if (!(ODD(out->coda_purgefid.CodaFid.Vnode))
478 && (CTOV(cp)->v_flag & VTEXT)) {
480 error = coda_vmflush(cp);
482 CODADEBUG(CODA_PURGEFID, myprintf(("purgefid: fid = (%lx.%lx.%lx), refcnt = %d, error = %d\n",
483 cp->c_fid.Volume, cp->c_fid.Vnode,
485 CTOV(cp)->v_usecount - 1, error)););
486 if (CTOV(cp)->v_usecount == 1) {
487 cp->c_flags |= C_PURGING;
494 case CODA_REPLACE : {
495 struct cnode *cp = NULL;
497 coda_clstat.ncalls++;
498 coda_clstat.reqs[CODA_REPLACE]++;
500 cp = coda_find(&out->coda_replace.OldFid);
502 /* remove the cnode from the hash table, replace the fid, and reinsert */
505 cp->c_fid = out->coda_replace.NewFid;
508 CODADEBUG(CODA_REPLACE, myprintf(("replace: oldfid = (%lx.%lx.%lx), newfid = (%lx.%lx.%lx), cp = %p\n",
509 out->coda_replace.OldFid.Volume,
510 out->coda_replace.OldFid.Vnode,
511 out->coda_replace.OldFid.Unique,
512 cp->c_fid.Volume, cp->c_fid.Vnode,
513 cp->c_fid.Unique, cp));)
519 myprintf(("handleDownCall: unknown opcode %d\n", opcode));
524 /* coda_grab_vnode: lives in either cfs_mach.c or cfs_nbsd.c */
535 * kernel-internal debugging switches
537 void coda_debugon(void)
541 coda_vnop_print_entry = 1;
542 coda_psdev_print_entry = 1;
543 coda_vfsop_print_entry = 1;
546 void coda_debugoff(void)
550 coda_vnop_print_entry = 0;
551 coda_psdev_print_entry = 0;
552 coda_vfsop_print_entry = 0;
556 * Utilities used by both client and server
561 * 3) current test software
562 * 4) main procedure entry points
563 * 5) main procedure exit points
564 * 6) utility procedure entry points
565 * 7) utility procedure exit points
566 * 8) obscure procedure entry points
567 * 9) obscure procedure exit points