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(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 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, 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;
369 MNT_VNODE_FOREACH(vp, mp, nvp) {
371 if (vp->v_iflag & VI_DOOMED) {
377 if (!(cp->c_flags & C_UNMOUNTING)) {
379 printf("vp %p, cp %p missed\n", vp, cp);
380 cp->c_flags |= C_UNMOUNTING;
388 coda_cacheprint(struct mount *whoIam)
394 printf("coda_cacheprint: coda_ctlvp %p, cp %p", coda_ctlvp,
398 coda_nc_name(VTOC(coda_ctlvp));
401 for (hash = 0; hash < CODA_CACHESIZE; hash++) {
402 for (cp = coda_cache[hash]; cp != NULL;
403 cp = CNODE_NEXT(cp)) {
404 if (CTOV(cp)->v_mount == whoIam) {
405 printf("coda_cacheprint: vp %p, cp %p",
415 printf("coda_cacheprint: count %d\n", count);
420 * There are 6 cases where invalidations occur. The semantics of each is
423 * CODA_FLUSH -- Flush all entries from the name cache and the cnode
426 * CODA_PURGEUSER -- Flush all entries from the name cache for a specific
427 * user. This call is a result of token expiration.
429 * The next two are the result of callbacks on a file or directory:
431 * CODA_ZAPDIR -- Flush the attributes for the dir from its cnode. Zap
432 * all children of this directory from the namecache.
434 * CODA_ZAPFILE -- Flush the attributes for a file.
436 * The fifth is a result of Venus detecting an inconsistent file:
438 * CODA_PURGEFID -- Flush the attribute for the file; if it is a dir (odd
439 * vnode), purge its children from the namecache; remove
440 * the file from the namecache.
442 * The sixth allows Venus to replace local fids with global ones during
445 * CODA_REPLACE -- Replace one CodaFid with another throughout the name
449 handleDownCall(struct coda_mntinfo *mnt, int opcode, union outputArgs *out)
454 * Handle invalidate requests.
458 coda_flush(mnt, IS_DOWNCALL);
460 /* Print any remaining cnodes. */
461 CODADEBUG(CODA_FLUSH, coda_testflush(););
465 case CODA_PURGEUSER: {
466 coda_clstat.ncalls++;
467 coda_clstat.reqs[CODA_PURGEUSER]++;
469 /* XXX - need to prevent fsync's. */
472 * Purge any access cache entries for the uid.
475 coda_acccache_purgeuser(mnt->mi_vfsp,
476 out->coda_purgeuser.cred.cr_uid);
478 coda_acccache_purgeuser(mnt->mi_vfsp,
479 out->coda_purgeuser.uid);
488 coda_clstat.ncalls++;
489 coda_clstat.reqs[CODA_ZAPFILE]++;
490 cp = coda_find(&out->coda_zapfile.Fid);
493 cache_purge(CTOV(cp));
494 cp->c_flags &= ~(C_VATTR | C_ACCCACHE);
495 ASSERT_VOP_LOCKED(CTOV(cp), "coda HandleDownCall");
496 if (CTOV(cp)->v_vflag & VV_TEXT)
497 error = coda_vmflush(cp);
498 CODADEBUG(CODA_ZAPFILE,
499 myprintf(("zapfile: fid = %s, refcnt = %d, error = "
500 "%d\n", coda_f2s(&cp->c_fid),
501 CTOV(cp)->v_usecount - 1, error)););
502 if (vrefcnt(CTOV(cp)) == 1)
503 cp->c_flags |= C_PURGING;
512 coda_clstat.ncalls++;
513 coda_clstat.reqs[CODA_ZAPDIR]++;
514 cp = coda_find(&out->coda_zapdir.Fid);
517 cache_purge(CTOV(cp));
518 cp->c_flags &= ~(C_VATTR | C_ACCCACHE);
519 CODADEBUG(CODA_ZAPDIR, myprintf(("zapdir: fid = %s, "
520 "refcnt = %d\n", coda_f2s(&cp->c_fid),
521 CTOV(cp)->v_usecount - 1)););
522 if (vrefcnt(CTOV(cp)) == 1)
523 cp->c_flags |= C_PURGING;
529 case CODA_PURGEFID: {
533 coda_clstat.ncalls++;
534 coda_clstat.reqs[CODA_PURGEFID]++;
535 cp = coda_find(&out->coda_purgefid.Fid);
538 cache_purge(CTOV(cp));
539 cp->c_flags &= ~(C_VATTR | C_ACCCACHE);
540 ASSERT_VOP_LOCKED(CTOV(cp), "coda HandleDownCall");
541 if (!(IS_DIR(out->coda_purgefid.Fid))
542 && (CTOV(cp)->v_vflag & VV_TEXT))
543 error = coda_vmflush(cp);
544 CODADEBUG(CODA_PURGEFID, myprintf(("purgefid: fid "
545 "= %s, refcnt = %d, error = %d\n",
546 coda_f2s(&cp->c_fid),
547 CTOV(cp)->v_usecount - 1, error)););
548 if (vrefcnt(CTOV(cp)) == 1)
549 cp->c_flags |= C_PURGING;
556 struct cnode *cp = NULL;
558 coda_clstat.ncalls++;
559 coda_clstat.reqs[CODA_REPLACE]++;
560 cp = coda_find(&out->coda_replace.OldFid);
563 * Remove the cnode from the hash table, replace the
564 * fid, and reinsert. Clear the attribute cache as
565 * the "inode number" may have changed (it's just a
566 * hash of the fid, and the fid is changing).
570 cp->c_fid = out->coda_replace.NewFid;
571 cp->c_flags &= ~C_VATTR;
573 CODADEBUG(CODA_REPLACE, myprintf(("replace: oldfid "
574 "= %s, newfid = %s, cp = %p\n",
575 coda_f2s(&out->coda_replace.OldFid),
576 coda_f2s(&cp->c_fid), cp)););
582 myprintf(("handleDownCall: unknown opcode %d\n", opcode));
588 coda_vmflush(struct cnode *cp)
595 * Kernel-internal debugging switches.
602 coda_vnop_print_entry = 1;
603 coda_psdev_print_entry = 1;
604 coda_vfsop_print_entry = 1;
612 coda_vnop_print_entry = 0;
613 coda_psdev_print_entry = 0;
614 coda_vfsop_print_entry = 0;
618 * Utilities used by both client and server
623 * 3) current test software
624 * 4) main procedure entry points
625 * 5) main procedure exit points
626 * 6) utility procedure entry points
627 * 7) utility procedure exit points
628 * 8) obscure procedure entry points
629 * 9) obscure procedure exit points