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 $
32 * Mach Operating System
33 * Copyright (c) 1989 Carnegie-Mellon University
34 * All rights reserved. The CMU software License Agreement specifies
35 * the terms and conditions for use and redistribution.
39 * This code was written for the Coda filesystem at Carnegie Mellon
40 * University. Contributers include David Steere, James Kistler, and
46 * 1. Added coda_unmounting to mark all cnodes as being UNMOUNTING. This
47 * has to be done before dounmount is called. Because some of the
48 * routines that dounmount calls before coda_unmounted might try to
49 * force flushes to venus. The vnode pager does this.
50 * 2. coda_unmounting marks all cnodes scanning coda_cache.
51 * 3. cfs_checkunmounting (under DEBUG) checks all cnodes by chasing the
52 * vnodes under the /coda mount point.
53 * 4. coda_cacheprint (under DEBUG) prints names with vnode/cnode address.
56 #include <sys/cdefs.h>
57 __FBSDID("$FreeBSD$");
59 #include <sys/param.h>
60 #include <sys/systm.h>
62 #include <sys/malloc.h>
63 #include <sys/mutex.h>
64 #include <sys/mount.h>
66 #include <fs/coda/coda.h>
67 #include <fs/coda/cnode.h>
68 #include <fs/coda/coda_subr.h>
70 static int coda_active = 0;
71 static int coda_reuse = 0;
72 static int coda_new = 0;
74 static struct cnode *coda_freelist = NULL;
75 static struct cnode *coda_cache[CODA_CACHESIZE];
77 #define CNODE_NEXT(cp) ((cp)->c_next)
80 #define coda_hash(fid) (((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)
95 if (coda_freelist != NULL) {
97 coda_freelist = CNODE_NEXT(cp);
100 CODA_ALLOC(cp, struct cnode *, sizeof(struct cnode));
103 * FreeBSD vnodes don't have any Pager info in them ('cause
104 * there are no external pagers, duh!).
106 #define VNODE_VM_INFO_INIT(vp) /* MT */
107 VNODE_VM_INFO_INIT(CTOV(cp));
110 bzero(cp, sizeof (struct cnode));
115 * Deallocate a cnode.
118 coda_free(struct cnode *cp)
121 CNODE_NEXT(cp) = coda_freelist;
126 * Put a cnode in the hash table.
129 coda_save(struct cnode *cp)
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.
140 coda_unsave(struct cnode *cp)
143 struct cnode *ptrprev = NULL;
145 ptr = coda_cache[coda_hash(&cp->c_fid)];
146 while (ptr != NULL) {
149 coda_cache[coda_hash(&cp->c_fid)] =
152 CNODE_NEXT(ptrprev) = CNODE_NEXT(ptr);
153 CNODE_NEXT(cp) = (struct cnode *)NULL;
157 ptr = CNODE_NEXT(ptr);
162 * Lookup a cnode by fid. If the cnode is dying, it is bogus so skip it.
164 * NOTE: this allows multiple cnodes with same fid -- dcs 1/25/95
167 coda_find(struct CodaFid *fid)
171 cp = coda_cache[coda_hash(fid)];
173 if (coda_fid_eq(&(cp->c_fid), fid) && (!IS_UNMOUNTING(cp))) {
183 * Clear all cached access control decisions from Coda.
186 coda_acccache_purge(struct mount *mnt)
191 for (hash = 0; hash < CODA_CACHESIZE; hash++) {
192 for (cp = coda_cache[hash]; cp != NULL;
193 cp = CNODE_NEXT(cp)) {
194 if (CTOV(cp)->v_mount == mnt && VALID_ACCCACHE(cp)) {
195 CODADEBUG(CODA_FLUSH, myprintf(("acccache "
196 "purge fid %s uid %d mode 0x%x\n",
197 coda_f2s(&cp->c_fid), cp->c_cached_uid,
198 (int)cp->c_cached_mode)););
199 cp->c_flags &= ~C_ACCCACHE;
206 * When a user loses their tokens (or other related events), we invalidate
207 * any cached access rights in the access cache. In the Linux version of
208 * Coda, we maintain a global epoch and simply bump it to invalidate all
209 * cached results generated in the epoch. For now, we walk all cnodes and
210 * manually invalidate just that uid in FreeBSD.
213 coda_acccache_purgeuser(struct mount *mnt, uid_t uid)
218 for (hash = 0; hash < CODA_CACHESIZE; hash++) {
219 for (cp = coda_cache[hash]; cp != NULL;
220 cp = CNODE_NEXT(cp)) {
221 if (CTOV(cp)->v_mount == mnt &&
222 VALID_ACCCACHE(cp) && (cp->c_cached_uid == uid)) {
223 CODADEBUG(CODA_PURGEUSER, myprintf((
224 "acccache purgeuser fid %s uid %d mode "
225 "0x%x\n", coda_f2s(&cp->c_fid),
226 cp->c_cached_uid, (int)cp->c_cached_mode)););
227 cp->c_flags &= ~C_ACCCACHE;
234 * coda_kill is called as a side effect to vcopen. To prevent any cnodes
235 * left around from an earlier run of a venus or warden from causing problems
236 * with the new instance, mark any outstanding cnodes as dying. Future
237 * operations on these cnodes should fail (excepting coda_inactive of
238 * course!). Since multiple venii/wardens can be running, only kill the
239 * cnodes for a particular entry in the coda_mnttbl. -- DCS 12/1/94
241 * XXX: I don't believe any special behavior is required with respect to the
242 * global namecache here, as /coda will have unmounted and hence cache_flush
246 coda_kill(struct mount *whoIam, enum dc_status dcstat)
252 * Algorithm is as follows:
253 * Second, flush whatever vnodes we can from the name cache.
255 * Finally, step through whatever is left and mark them dying.
256 * This prevents any operation at all.
258 * This is slightly overkill, but should work. Eventually it'd be
259 * nice to only flush those entries from the namecache that reference
260 * a vnode in this vfs.
262 * XXXRW: Perhaps we no longer need to purge the name cache when
263 * using the VFS name cache, as unmount will do that.
265 cache_purgevfs(whoIam);
266 for (hash = 0; hash < CODA_CACHESIZE; hash++) {
267 for (cp = coda_cache[hash];cp != NULL;
268 cp = CNODE_NEXT(cp)) {
269 if (CTOV(cp)->v_mount == whoIam) {
271 printf("coda_kill: vp %p, cp %p\n", CTOV(cp),
275 CODADEBUG(CODA_FLUSH, myprintf(("Live cnode "
276 "fid %s flags %d count %d\n",
277 coda_f2s(&cp->c_fid), cp->c_flags,
278 vrefcnt(CTOV(cp)))););
286 * There are two reasons why a cnode may be in use, it may be in the name
287 * cache or it may be executing.
290 coda_flush(struct coda_mntinfo *mnt, enum dc_status dcstat)
295 coda_clstat.ncalls++;
296 coda_clstat.reqs[CODA_FLUSH]++;
298 coda_acccache_purge(mnt->mi_vfsp);
299 cache_purgevfs(mnt->mi_vfsp);
300 for (hash = 0; hash < CODA_CACHESIZE; hash++) {
301 for (cp = coda_cache[hash]; cp != NULL;
302 cp = CNODE_NEXT(cp)) {
304 * Only files that can be executed need to be flushed
307 * NOTE: Currently this doesn't do anything, but
310 if (!IS_DIR(cp->c_fid))
317 * As a debugging measure, print out any cnodes that lived through a name
326 for (hash = 0; hash < CODA_CACHESIZE; hash++) {
327 for (cp = coda_cache[hash]; cp != NULL;
329 myprintf(("Live cnode fid %s count %d\n",
330 coda_f2s(&cp->c_fid), CTOV(cp)->v_usecount));
335 * First, step through all cnodes and mark them unmounting. FreeBSD kernels
336 * may try to fsync them now that venus is dead, which would be a bad thing.
339 coda_unmounting(struct mount *whoIam)
344 for (hash = 0; hash < CODA_CACHESIZE; hash++) {
345 for (cp = coda_cache[hash]; cp != NULL;
346 cp = CNODE_NEXT(cp)) {
347 if (CTOV(cp)->v_mount == whoIam) {
348 if (cp->c_flags & (C_LOCKED|C_WANTED)) {
349 printf("coda_unmounting: Unlocking "
351 cp->c_flags &= ~(C_LOCKED|C_WANTED);
352 wakeup((caddr_t) cp);
354 cp->c_flags |= C_UNMOUNTING;
362 coda_checkunmounting(struct mount *mp)
364 struct vnode *vp, *nvp;
366 int count = 0, bad = 0;
368 MNT_VNODE_FOREACH_ALL(vp, mp, nvp) {
371 if (!(cp->c_flags & C_UNMOUNTING)) {
373 printf("vp %p, cp %p missed\n", vp, cp);
374 cp->c_flags |= C_UNMOUNTING;
381 coda_cacheprint(struct mount *whoIam)
387 printf("coda_cacheprint: coda_ctlvp %p, cp %p", coda_ctlvp,
391 coda_nc_name(VTOC(coda_ctlvp));
394 for (hash = 0; hash < CODA_CACHESIZE; hash++) {
395 for (cp = coda_cache[hash]; cp != NULL;
396 cp = CNODE_NEXT(cp)) {
397 if (CTOV(cp)->v_mount == whoIam) {
398 printf("coda_cacheprint: vp %p, cp %p",
408 printf("coda_cacheprint: count %d\n", count);
413 * There are 6 cases where invalidations occur. The semantics of each is
416 * CODA_FLUSH -- Flush all entries from the name cache and the cnode
419 * CODA_PURGEUSER -- Flush all entries from the name cache for a specific
420 * user. This call is a result of token expiration.
422 * The next two are the result of callbacks on a file or directory:
424 * CODA_ZAPDIR -- Flush the attributes for the dir from its cnode. Zap
425 * all children of this directory from the namecache.
427 * CODA_ZAPFILE -- Flush the attributes for a file.
429 * The fifth is a result of Venus detecting an inconsistent file:
431 * CODA_PURGEFID -- Flush the attribute for the file; if it is a dir (odd
432 * vnode), purge its children from the namecache; remove
433 * the file from the namecache.
435 * The sixth allows Venus to replace local fids with global ones during
438 * CODA_REPLACE -- Replace one CodaFid with another throughout the name
442 handleDownCall(struct coda_mntinfo *mnt, int opcode, union outputArgs *out)
447 * Handle invalidate requests.
451 coda_flush(mnt, IS_DOWNCALL);
453 /* Print any remaining cnodes. */
454 CODADEBUG(CODA_FLUSH, coda_testflush(););
458 case CODA_PURGEUSER: {
459 coda_clstat.ncalls++;
460 coda_clstat.reqs[CODA_PURGEUSER]++;
462 /* XXX - need to prevent fsync's. */
465 * Purge any access cache entries for the uid.
468 coda_acccache_purgeuser(mnt->mi_vfsp,
469 out->coda_purgeuser.cred.cr_uid);
471 coda_acccache_purgeuser(mnt->mi_vfsp,
472 out->coda_purgeuser.uid);
481 coda_clstat.ncalls++;
482 coda_clstat.reqs[CODA_ZAPFILE]++;
483 cp = coda_find(&out->coda_zapfile.Fid);
486 cache_purge(CTOV(cp));
487 cp->c_flags &= ~(C_VATTR | C_ACCCACHE);
488 ASSERT_VOP_LOCKED(CTOV(cp), "coda HandleDownCall");
489 if (VOP_IS_TEXT(CTOV(cp)))
490 error = coda_vmflush(cp);
491 CODADEBUG(CODA_ZAPFILE,
492 myprintf(("zapfile: fid = %s, refcnt = %d, error = "
493 "%d\n", coda_f2s(&cp->c_fid),
494 CTOV(cp)->v_usecount - 1, error)););
495 if (vrefcnt(CTOV(cp)) == 1)
496 cp->c_flags |= C_PURGING;
505 coda_clstat.ncalls++;
506 coda_clstat.reqs[CODA_ZAPDIR]++;
507 cp = coda_find(&out->coda_zapdir.Fid);
510 cache_purge(CTOV(cp));
511 cp->c_flags &= ~(C_VATTR | C_ACCCACHE);
512 CODADEBUG(CODA_ZAPDIR, myprintf(("zapdir: fid = %s, "
513 "refcnt = %d\n", coda_f2s(&cp->c_fid),
514 CTOV(cp)->v_usecount - 1)););
515 if (vrefcnt(CTOV(cp)) == 1)
516 cp->c_flags |= C_PURGING;
522 case CODA_PURGEFID: {
526 coda_clstat.ncalls++;
527 coda_clstat.reqs[CODA_PURGEFID]++;
528 cp = coda_find(&out->coda_purgefid.Fid);
531 cache_purge(CTOV(cp));
532 cp->c_flags &= ~(C_VATTR | C_ACCCACHE);
533 ASSERT_VOP_LOCKED(CTOV(cp), "coda HandleDownCall");
534 if (!(IS_DIR(out->coda_purgefid.Fid))
535 && VOP_IS_TEXT(CTOV(cp)))
536 error = coda_vmflush(cp);
537 CODADEBUG(CODA_PURGEFID, myprintf(("purgefid: fid "
538 "= %s, refcnt = %d, error = %d\n",
539 coda_f2s(&cp->c_fid),
540 CTOV(cp)->v_usecount - 1, error)););
541 if (vrefcnt(CTOV(cp)) == 1)
542 cp->c_flags |= C_PURGING;
549 struct cnode *cp = NULL;
551 coda_clstat.ncalls++;
552 coda_clstat.reqs[CODA_REPLACE]++;
553 cp = coda_find(&out->coda_replace.OldFid);
556 * Remove the cnode from the hash table, replace the
557 * fid, and reinsert. Clear the attribute cache as
558 * the "inode number" may have changed (it's just a
559 * hash of the fid, and the fid is changing).
563 cp->c_fid = out->coda_replace.NewFid;
564 cp->c_flags &= ~C_VATTR;
566 CODADEBUG(CODA_REPLACE, myprintf(("replace: oldfid "
567 "= %s, newfid = %s, cp = %p\n",
568 coda_f2s(&out->coda_replace.OldFid),
569 coda_f2s(&cp->c_fid), cp)););
575 myprintf(("handleDownCall: unknown opcode %d\n", opcode));
581 coda_vmflush(struct cnode *cp)
588 * Kernel-internal debugging switches.
595 coda_vnop_print_entry = 1;
596 coda_psdev_print_entry = 1;
597 coda_vfsop_print_entry = 1;
605 coda_vnop_print_entry = 0;
606 coda_psdev_print_entry = 0;
607 coda_vfsop_print_entry = 0;
611 * Utilities used by both client and server
616 * 3) current test software
617 * 4) main procedure entry points
618 * 5) main procedure exit points
619 * 6) utility procedure entry points
620 * 7) utility procedure exit points
621 * 8) obscure procedure entry points
622 * 9) obscure procedure exit points