No empty .Rs/.Re
[netbsd-mini2440.git] / sys / coda / coda_subr.c
blob34ba0dd984826edb9d92af87a7b537a256b984eb
1 /* $NetBSD: coda_subr.c,v 1.23 2007/03/04 06:01:12 christos Exp $ */
3 /*
5 * Coda: an Experimental Distributed File System
6 * Release 3.1
8 * Copyright (c) 1987-1998 Carnegie Mellon University
9 * All Rights Reserved
11 * Permission to use, copy, modify and distribute this software and its
12 * documentation is hereby granted, provided that both the copyright
13 * notice and this permission notice appear in all copies of the
14 * software, derivative works or modified versions, and any portions
15 * thereof, and that both notices appear in supporting documentation, and
16 * that credit is given to Carnegie Mellon University in all documents
17 * and publicity pertaining to direct or indirect use of this code or its
18 * derivatives.
20 * CODA IS AN EXPERIMENTAL SOFTWARE SYSTEM AND IS KNOWN TO HAVE BUGS,
21 * SOME OF WHICH MAY HAVE SERIOUS CONSEQUENCES. CARNEGIE MELLON ALLOWS
22 * FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION. CARNEGIE MELLON
23 * DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER
24 * RESULTING DIRECTLY OR INDIRECTLY FROM THE USE OF THIS SOFTWARE OR OF
25 * ANY DERIVATIVE WORK.
27 * Carnegie Mellon encourages users of this software to return any
28 * improvements or extensions that they make, and to grant Carnegie
29 * Mellon the rights to redistribute these changes without encumbrance.
31 * @(#) coda/coda_subr.c,v 1.1.1.1 1998/08/29 21:26:45 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
44 * M. Satyanarayanan. */
46 /* NOTES: rvb
47 * 1. Added coda_unmounting to mark all cnodes as being UNMOUNTING. This has to
48 * be done before dounmount is called. Because some of the routines that
49 * dounmount calls before coda_unmounted might try to force flushes to venus.
50 * The vnode pager does this.
51 * 2. coda_unmounting marks all cnodes scanning coda_cache.
52 * 3. cfs_checkunmounting (under DEBUG) checks all cnodes by chasing the vnodes
53 * under the /coda mount point.
54 * 4. coda_cacheprint (under DEBUG) prints names with vnode/cnode address
57 #include <sys/cdefs.h>
58 __KERNEL_RCSID(0, "$NetBSD: coda_subr.c,v 1.23 2007/03/04 06:01:12 christos Exp $");
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/malloc.h>
63 #include <sys/proc.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>
72 #ifdef _KERNEL_OPT
73 #include "opt_coda_compat.h"
74 #endif
76 int coda_active = 0;
77 int coda_reuse = 0;
78 int coda_new = 0;
80 struct cnode *coda_freelist = NULL;
81 struct cnode *coda_cache[CODA_CACHESIZE];
83 #define CNODE_NEXT(cp) ((cp)->c_next)
85 #ifdef CODA_COMPAT_5
86 #define coda_hash(fid) \
87 (((fid)->Volume + (fid)->Vnode) & (CODA_CACHESIZE-1))
88 #define IS_DIR(cnode) (cnode.Vnode & 0x1)
89 #else
90 #define coda_hash(fid) \
91 (coda_f2i(fid) & (CODA_CACHESIZE-1))
92 #define IS_DIR(cnode) (cnode.opaque[2] & 0x1)
93 #endif
96 * Allocate a cnode.
98 struct cnode *
99 coda_alloc(void)
101 struct cnode *cp;
103 if (coda_freelist) {
104 cp = coda_freelist;
105 coda_freelist = CNODE_NEXT(cp);
106 coda_reuse++;
108 else {
109 CODA_ALLOC(cp, struct cnode *, sizeof(struct cnode));
110 /* NetBSD vnodes don't have any Pager info in them ('cause there are
111 no external pagers, duh!) */
112 #define VNODE_VM_INFO_INIT(vp) /* MT */
113 VNODE_VM_INFO_INIT(CTOV(cp));
114 coda_new++;
116 memset(cp, 0, sizeof (struct cnode));
118 return(cp);
122 * Deallocate a cnode.
124 void
125 coda_free(struct cnode *cp)
128 CNODE_NEXT(cp) = coda_freelist;
129 coda_freelist = cp;
133 * Put a cnode in the hash table
135 void
136 coda_save(struct cnode *cp)
138 CNODE_NEXT(cp) = coda_cache[coda_hash(&cp->c_fid)];
139 coda_cache[coda_hash(&cp->c_fid)] = cp;
143 * Remove a cnode from the hash table
145 void
146 coda_unsave(struct cnode *cp)
148 struct cnode *ptr;
149 struct cnode *ptrprev = NULL;
151 ptr = coda_cache[coda_hash(&cp->c_fid)];
152 while (ptr != NULL) {
153 if (ptr == cp) {
154 if (ptrprev == NULL) {
155 coda_cache[coda_hash(&cp->c_fid)]
156 = CNODE_NEXT(ptr);
157 } else {
158 CNODE_NEXT(ptrprev) = CNODE_NEXT(ptr);
160 CNODE_NEXT(cp) = (struct cnode *)NULL;
162 return;
164 ptrprev = ptr;
165 ptr = CNODE_NEXT(ptr);
170 * Lookup a cnode by fid. If the cnode is dying, it is bogus so skip it.
171 * NOTE: this allows multiple cnodes with same fid -- dcs 1/25/95
173 struct cnode *
174 coda_find(CodaFid *fid)
176 struct cnode *cp;
178 cp = coda_cache[coda_hash(fid)];
179 while (cp) {
180 if (coda_fid_eq(&(cp->c_fid), fid) &&
181 (!IS_UNMOUNTING(cp)))
183 coda_active++;
184 return(cp);
186 cp = CNODE_NEXT(cp);
188 return(NULL);
192 * coda_kill is called as a side effect to vcopen. To prevent any
193 * cnodes left around from an earlier run of a venus or warden from
194 * causing problems with the new instance, mark any outstanding cnodes
195 * as dying. Future operations on these cnodes should fail (excepting
196 * coda_inactive of course!). Since multiple venii/wardens can be
197 * running, only kill the cnodes for a particular entry in the
198 * coda_mnttbl. -- DCS 12/1/94 */
201 coda_kill(struct mount *whoIam, enum dc_status dcstat)
203 int hash, count = 0;
204 struct cnode *cp;
207 * Algorithm is as follows:
208 * Second, flush whatever vnodes we can from the name cache.
210 * Finally, step through whatever is left and mark them dying.
211 * This prevents any operation at all.
215 /* This is slightly overkill, but should work. Eventually it'd be
216 * nice to only flush those entries from the namecache that
217 * reference a vnode in this vfs. */
218 coda_nc_flush(dcstat);
220 for (hash = 0; hash < CODA_CACHESIZE; hash++) {
221 for (cp = coda_cache[hash]; cp != NULL; cp = CNODE_NEXT(cp)) {
222 if (CTOV(cp)->v_mount == whoIam) {
223 #ifdef DEBUG
224 printf("coda_kill: vp %p, cp %p\n", CTOV(cp), cp);
225 #endif
226 count++;
227 CODADEBUG(CODA_FLUSH,
228 myprintf(("Live cnode fid %s flags %d count %d\n",
229 coda_f2s(&cp->c_fid),
230 cp->c_flags,
231 CTOV(cp)->v_usecount)); );
235 return count;
239 * There are two reasons why a cnode may be in use, it may be in the
240 * name cache or it may be executing.
242 void
243 coda_flush(enum dc_status dcstat)
245 int hash;
246 struct cnode *cp;
248 coda_clstat.ncalls++;
249 coda_clstat.reqs[CODA_FLUSH]++;
251 coda_nc_flush(dcstat); /* flush files from the name cache */
253 for (hash = 0; hash < CODA_CACHESIZE; hash++) {
254 for (cp = coda_cache[hash]; cp != NULL; cp = CNODE_NEXT(cp)) {
255 if (!IS_DIR(cp->c_fid)) /* only files can be executed */
256 coda_vmflush(cp);
262 * As a debugging measure, print out any cnodes that lived through a
263 * name cache flush.
265 void
266 coda_testflush(void)
268 int hash;
269 struct cnode *cp;
271 for (hash = 0; hash < CODA_CACHESIZE; hash++) {
272 for (cp = coda_cache[hash];
273 cp != NULL;
274 cp = CNODE_NEXT(cp)) {
275 myprintf(("Live cnode fid %s count %d\n",
276 coda_f2s(&cp->c_fid), CTOV(cp)->v_usecount));
282 * First, step through all cnodes and mark them unmounting.
283 * NetBSD kernels may try to fsync them now that venus
284 * is dead, which would be a bad thing.
287 void
288 coda_unmounting(struct mount *whoIam)
290 int hash;
291 struct cnode *cp;
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((void *) cp);
301 cp->c_flags |= C_UNMOUNTING;
307 #ifdef DEBUG
308 void
309 coda_checkunmounting(struct mount *mp)
311 struct vnode *vp;
312 struct cnode *cp;
313 int count = 0, bad = 0;
314 loop:
315 TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
316 if (vp->v_mount != mp)
317 goto loop;
318 cp = VTOC(vp);
319 count++;
320 if (!(cp->c_flags & C_UNMOUNTING)) {
321 bad++;
322 printf("vp %p, cp %p missed\n", vp, cp);
323 cp->c_flags |= C_UNMOUNTING;
328 void
329 coda_cacheprint(struct mount *whoIam)
331 int hash;
332 struct cnode *cp;
333 int count = 0;
335 printf("coda_cacheprint: coda_ctlvp %p, cp %p", coda_ctlvp, VTOC(coda_ctlvp));
336 coda_nc_name(VTOC(coda_ctlvp));
337 printf("\n");
339 for (hash = 0; hash < CODA_CACHESIZE; hash++) {
340 for (cp = coda_cache[hash]; cp != NULL; cp = CNODE_NEXT(cp)) {
341 if (CTOV(cp)->v_mount == whoIam) {
342 printf("coda_cacheprint: vp %p, cp %p", CTOV(cp), cp);
343 coda_nc_name(cp);
344 printf("\n");
345 count++;
349 printf("coda_cacheprint: count %d\n", count);
351 #endif
354 * There are 6 cases where invalidations occur. The semantics of each
355 * is listed here.
357 * CODA_FLUSH -- flush all entries from the name cache and the cnode cache.
358 * CODA_PURGEUSER -- flush all entries from the name cache for a specific user
359 * This call is a result of token expiration.
361 * The next two are the result of callbacks on a file or directory.
362 * CODA_ZAPDIR -- flush the attributes for the dir from its cnode.
363 * Zap all children of this directory from the namecache.
364 * CODA_ZAPFILE -- flush the attributes for a file.
366 * The fifth is a result of Venus detecting an inconsistent file.
367 * CODA_PURGEFID -- flush the attribute for the file
368 * If it is a dir (odd vnode), purge its
369 * children from the namecache
370 * remove the file from the namecache.
372 * The sixth allows Venus to replace local fids with global ones
373 * during reintegration.
375 * CODA_REPLACE -- replace one CodaFid with another throughout the name cache
378 int handleDownCall(int opcode, union outputArgs *out)
380 int error;
382 /* Handle invalidate requests. */
383 switch (opcode) {
384 case CODA_FLUSH : {
386 coda_flush(IS_DOWNCALL);
388 CODADEBUG(CODA_FLUSH,coda_testflush();) /* print remaining cnodes */
389 return(0);
392 case CODA_PURGEUSER : {
393 coda_clstat.ncalls++;
394 coda_clstat.reqs[CODA_PURGEUSER]++;
396 /* XXX - need to prevent fsync's */
397 #ifdef CODA_COMPAT_5
398 coda_nc_purge_user(out->coda_purgeuser.cred.cr_uid, IS_DOWNCALL);
399 #else
400 coda_nc_purge_user(out->coda_purgeuser.uid, IS_DOWNCALL);
401 #endif
402 return(0);
405 case CODA_ZAPFILE : {
406 struct cnode *cp;
408 error = 0;
409 coda_clstat.ncalls++;
410 coda_clstat.reqs[CODA_ZAPFILE]++;
412 cp = coda_find(&out->coda_zapfile.Fid);
413 if (cp != NULL) {
414 vref(CTOV(cp));
416 cp->c_flags &= ~C_VATTR;
417 if (CTOV(cp)->v_iflag & VI_TEXT)
418 error = coda_vmflush(cp);
419 CODADEBUG(CODA_ZAPFILE, myprintf((
420 "zapfile: fid = %s, refcnt = %d, error = %d\n",
421 coda_f2s(&cp->c_fid), CTOV(cp)->v_usecount - 1, error)););
422 if (CTOV(cp)->v_usecount == 1) {
423 cp->c_flags |= C_PURGING;
425 vrele(CTOV(cp));
428 return(error);
431 case CODA_ZAPDIR : {
432 struct cnode *cp;
434 coda_clstat.ncalls++;
435 coda_clstat.reqs[CODA_ZAPDIR]++;
437 cp = coda_find(&out->coda_zapdir.Fid);
438 if (cp != NULL) {
439 vref(CTOV(cp));
441 cp->c_flags &= ~C_VATTR;
442 coda_nc_zapParentfid(&out->coda_zapdir.Fid, IS_DOWNCALL);
444 CODADEBUG(CODA_ZAPDIR, myprintf((
445 "zapdir: fid = %s, refcnt = %d\n",
446 coda_f2s(&cp->c_fid), CTOV(cp)->v_usecount - 1)););
447 if (CTOV(cp)->v_usecount == 1) {
448 cp->c_flags |= C_PURGING;
450 vrele(CTOV(cp));
453 return(0);
456 case CODA_PURGEFID : {
457 struct cnode *cp;
459 error = 0;
460 coda_clstat.ncalls++;
461 coda_clstat.reqs[CODA_PURGEFID]++;
463 cp = coda_find(&out->coda_purgefid.Fid);
464 if (cp != NULL) {
465 vref(CTOV(cp));
466 if (IS_DIR(out->coda_purgefid.Fid)) { /* Vnode is a directory */
467 coda_nc_zapParentfid(&out->coda_purgefid.Fid,
468 IS_DOWNCALL);
470 cp->c_flags &= ~C_VATTR;
471 coda_nc_zapfid(&out->coda_purgefid.Fid, IS_DOWNCALL);
472 if (!(IS_DIR(out->coda_purgefid.Fid))
473 && (CTOV(cp)->v_iflag & VI_TEXT)) {
475 error = coda_vmflush(cp);
477 CODADEBUG(CODA_PURGEFID, myprintf((
478 "purgefid: fid = %s, refcnt = %d, error = %d\n",
479 coda_f2s(&cp->c_fid), CTOV(cp)->v_usecount - 1, error)););
480 if (CTOV(cp)->v_usecount == 1) {
481 cp->c_flags |= C_PURGING;
483 vrele(CTOV(cp));
485 return(error);
488 case CODA_REPLACE : {
489 struct cnode *cp = NULL;
491 coda_clstat.ncalls++;
492 coda_clstat.reqs[CODA_REPLACE]++;
494 cp = coda_find(&out->coda_replace.OldFid);
495 if (cp != NULL) {
496 /* remove the cnode from the hash table, replace the fid, and reinsert */
497 vref(CTOV(cp));
498 coda_unsave(cp);
499 cp->c_fid = out->coda_replace.NewFid;
500 coda_save(cp);
502 CODADEBUG(CODA_REPLACE, myprintf((
503 "replace: oldfid = %s, newfid = %s, cp = %p\n",
504 coda_f2s(&out->coda_replace.OldFid),
505 coda_f2s(&cp->c_fid), cp));)
506 vrele(CTOV(cp));
508 return (0);
510 default:
511 myprintf(("handleDownCall: unknown opcode %d\n", opcode));
512 return (EINVAL);
516 /* coda_grab_vnode: lives in either cfs_mach.c or cfs_nbsd.c */
519 coda_vmflush(struct cnode *cp)
521 return 0;
526 * kernel-internal debugging switches
529 void coda_debugon(void)
531 codadebug = -1;
532 coda_nc_debug = -1;
533 coda_vnop_print_entry = 1;
534 coda_psdev_print_entry = 1;
535 coda_vfsop_print_entry = 1;
538 void coda_debugoff(void)
540 codadebug = 0;
541 coda_nc_debug = 0;
542 coda_vnop_print_entry = 0;
543 coda_psdev_print_entry = 0;
544 coda_vfsop_print_entry = 0;
548 * Utilities used by both client and server
549 * Standard levels:
550 * 0) no debugging
551 * 1) hard failures
552 * 2) soft failures
553 * 3) current test software
554 * 4) main procedure entry points
555 * 5) main procedure exit points
556 * 6) utility procedure entry points
557 * 7) utility procedure exit points
558 * 8) obscure procedure entry points
559 * 9) obscure procedure exit points
560 * 10) random stuff
561 * 11) all <= 1
562 * 12) all <= 2
563 * 13) all <= 3
564 * ...