Deleted Added
sdiff udiff text old ( 116173 ) new ( 119832 )
full compact
1/*
2 * Coda: an Experimental Distributed File System
3 * Release 3.1
4 *
5 * Copyright (c) 1987-1998 Carnegie Mellon University
6 * All Rights Reserved
7 *
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
15 * derivatives.
16 *
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.
23 *
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.
27 *
28 * @(#) src/sys/coda/coda_subr.c,v 1.1.1.1 1998/08/29 21:14:52 rvb Exp $
29 */
30/*
31 * Mach Operating System
32 * Copyright (c) 1989 Carnegie-Mellon University
33 * All rights reserved. The CMU software License Agreement specifies
34 * the terms and conditions for use and redistribution.
35 */
36
37/*
38 * This code was written for the Coda filesystem at Carnegie Mellon
39 * University. Contributers include David Steere, James Kistler, and
40 * M. Satyanarayanan.
41 */
42
43/* NOTES: rvb
44 * 1. Added coda_unmounting to mark all cnodes as being UNMOUNTING. This has to
45 * be done before dounmount is called. Because some of the routines that
46 * dounmount calls before coda_unmounted might try to force flushes to venus.
47 * The vnode pager does this.
48 * 2. coda_unmounting marks all cnodes scanning coda_cache.
49 * 3. cfs_checkunmounting (under DEBUG) checks all cnodes by chasing the vnodes
50 * under the /coda mount point.
51 * 4. coda_cacheprint (under DEBUG) prints names with vnode/cnode address
52 */
53
54#include <sys/cdefs.h>
55__FBSDID("$FreeBSD: head/sys/fs/coda/coda_subr.c 119832 2003-09-07 07:43:10Z tjr $");
56
57#include <vcoda.h>
58
59#include <sys/param.h>
60#include <sys/systm.h>
61#include <sys/lock.h>
62#include <sys/malloc.h>
63#include <sys/mount.h>
64
65#include <coda/coda.h>
66#include <coda/cnode.h>
67#include <coda/coda_subr.h>
68#include <coda/coda_namecache.h>
69
70int coda_active = 0;
71int coda_reuse = 0;
72int coda_new = 0;
73
74struct cnode *coda_freelist = NULL;
75struct cnode *coda_cache[CODA_CACHESIZE];
76
77#define CNODE_NEXT(cp) ((cp)->c_next)
78
79#ifdef CODA_COMPAT_5
80#define coda_hash(fid) \
81 (((fid)->Volume + (fid)->Vnode) & (CODA_CACHESIZE-1))
82#define IS_DIR(cnode) (cnode.Vnode & 0x1)
83#else
84#define coda_hash(fid) (coda_f2i(fid) & (CODA_CACHESIZE-1))
85#define IS_DIR(cnode) (cnode.opaque[2] & 0x1)
86#endif
87
88/*
89 * Allocate a cnode.
90 */
91struct cnode *
92coda_alloc(void)
93{
94 struct cnode *cp;
95
96 if (coda_freelist) {
97 cp = coda_freelist;
98 coda_freelist = CNODE_NEXT(cp);
99 coda_reuse++;
100 }
101 else {
102 CODA_ALLOC(cp, struct cnode *, sizeof(struct cnode));
103 /* NetBSD vnodes don't have any Pager info in them ('cause there are
104 no external pagers, duh!) */
105#define VNODE_VM_INFO_INIT(vp) /* MT */
106 VNODE_VM_INFO_INIT(CTOV(cp));
107 coda_new++;
108 }
109 bzero(cp, sizeof (struct cnode));
110
111 return(cp);
112}
113
114/*
115 * Deallocate a cnode.
116 */
117void
118coda_free(cp)
119 register struct cnode *cp;
120{
121
122 CNODE_NEXT(cp) = coda_freelist;
123 coda_freelist = cp;
124}
125
126/*
127 * Put a cnode in the hash table
128 */
129void
130coda_save(cp)
131 struct cnode *cp;
132{
133 CNODE_NEXT(cp) = coda_cache[coda_hash(&cp->c_fid)];
134 coda_cache[coda_hash(&cp->c_fid)] = cp;
135}
136
137/*
138 * Remove a cnode from the hash table
139 */
140void
141coda_unsave(cp)
142 struct cnode *cp;
143{
144 struct cnode *ptr;
145 struct cnode *ptrprev = NULL;
146
147 ptr = coda_cache[coda_hash(&cp->c_fid)];
148 while (ptr != NULL) {
149 if (ptr == cp) {
150 if (ptrprev == NULL) {
151 coda_cache[coda_hash(&cp->c_fid)]
152 = CNODE_NEXT(ptr);
153 } else {
154 CNODE_NEXT(ptrprev) = CNODE_NEXT(ptr);
155 }
156 CNODE_NEXT(cp) = (struct cnode *)NULL;
157
158 return;
159 }
160 ptrprev = ptr;
161 ptr = CNODE_NEXT(ptr);
162 }
163}
164
165/*
166 * Lookup a cnode by fid. If the cnode is dying, it is bogus so skip it.
167 * NOTE: this allows multiple cnodes with same fid -- dcs 1/25/95
168 */
169struct cnode *
170coda_find(fid)
171 CodaFid *fid;
172{
173 struct cnode *cp;
174
175 cp = coda_cache[coda_hash(fid)];
176 while (cp) {
177 if (coda_fid_eq(&(cp->c_fid), fid) &&
178 (!IS_UNMOUNTING(cp)))
179 {
180 coda_active++;
181 return(cp);
182 }
183 cp = CNODE_NEXT(cp);
184 }
185 return(NULL);
186}
187
188/*
189 * coda_kill is called as a side effect to vcopen. To prevent any
190 * cnodes left around from an earlier run of a venus or warden from
191 * causing problems with the new instance, mark any outstanding cnodes
192 * as dying. Future operations on these cnodes should fail (excepting
193 * coda_inactive of course!). Since multiple venii/wardens can be
194 * running, only kill the cnodes for a particular entry in the
195 * coda_mnttbl. -- DCS 12/1/94 */
196
197int
198coda_kill(whoIam, dcstat)
199 struct mount *whoIam;
200 enum dc_status dcstat;
201{
202 int hash, count = 0;
203 struct cnode *cp;
204
205 /*
206 * Algorithm is as follows:
207 * Second, flush whatever vnodes we can from the name cache.
208 *
209 * Finally, step through whatever is left and mark them dying.
210 * This prevents any operation at all.
211 */
212
213 /* This is slightly overkill, but should work. Eventually it'd be
214 * nice to only flush those entries from the namecache that
215 * reference a vnode in this vfs. */
216 coda_nc_flush(dcstat);
217
218 for (hash = 0; hash < CODA_CACHESIZE; hash++) {
219 for (cp = coda_cache[hash]; cp != NULL; cp = CNODE_NEXT(cp)) {
220 if (CTOV(cp)->v_mount == whoIam) {
221#ifdef DEBUG
222 printf("coda_kill: vp %p, cp %p\n", CTOV(cp), cp);
223#endif
224 count++;
225 CODADEBUG(CODA_FLUSH,
226 myprintf(("Live cnode fid %s flags %d count %d\n",
227 coda_f2s(&cp->c_fid),
228 cp->c_flags,
229 vrefcnt(CTOV(cp)))); );
230 }
231 }
232 }
233 return count;
234}
235
236/*
237 * There are two reasons why a cnode may be in use, it may be in the
238 * name cache or it may be executing.
239 */
240void
241coda_flush(dcstat)
242 enum dc_status dcstat;
243{
244 int hash;
245 struct cnode *cp;
246
247 coda_clstat.ncalls++;
248 coda_clstat.reqs[CODA_FLUSH]++;
249
250 coda_nc_flush(dcstat); /* flush files from the name cache */
251
252 for (hash = 0; hash < CODA_CACHESIZE; hash++) {
253 for (cp = coda_cache[hash]; cp != NULL; cp = CNODE_NEXT(cp)) {
254 if (!IS_DIR(cp->c_fid)) /* only files can be executed */
255 coda_vmflush(cp);
256 }
257 }
258}
259
260/*
261 * As a debugging measure, print out any cnodes that lived through a
262 * name cache flush.
263 */
264void
265coda_testflush(void)
266{
267 int hash;
268 struct cnode *cp;
269
270 for (hash = 0; hash < CODA_CACHESIZE; hash++) {
271 for (cp = coda_cache[hash];
272 cp != NULL;
273 cp = CNODE_NEXT(cp)) {
274 myprintf(("Live cnode fid %s count %d\n",
275 coda_f2s(&cp->c_fid), CTOV(cp)->v_usecount));
276 }
277 }
278}
279
280/*
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.
284 *
285 */
286void
287coda_unmounting(whoIam)
288 struct mount *whoIam;
289{
290 int hash;
291 struct cnode *cp;
292
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);
300 }
301 cp->c_flags |= C_UNMOUNTING;
302 }
303 }
304 }
305}
306
307#ifdef DEBUG
308void
309coda_checkunmounting(mp)
310 struct mount *mp;
311{
312 register struct vnode *vp, *nvp;
313 struct cnode *cp;
314 int count = 0, bad = 0;
315loop:
316 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp; vp = nvp) {
317 if (vp->v_mount != mp)
318 goto loop;
319 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
320 cp = VTOC(vp);
321 count++;
322 if (!(cp->c_flags & C_UNMOUNTING)) {
323 bad++;
324 printf("vp %p, cp %p missed\n", vp, cp);
325 cp->c_flags |= C_UNMOUNTING;
326 }
327 }
328}
329
330void
331coda_cacheprint(whoIam)
332 struct mount *whoIam;
333{
334 int hash;
335 struct cnode *cp;
336 int count = 0;
337
338 printf("coda_cacheprint: coda_ctlvp %p, cp %p", coda_ctlvp, VTOC(coda_ctlvp));
339 coda_nc_name(VTOC(coda_ctlvp));
340 printf("\n");
341
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);
346 coda_nc_name(cp);
347 printf("\n");
348 count++;
349 }
350 }
351 }
352 printf("coda_cacheprint: count %d\n", count);
353}
354#endif
355
356/*
357 * There are 6 cases where invalidations occur. The semantics of each
358 * is listed here.
359 *
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.
363 *
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.
368 *
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.
374 *
375 * The sixth allows Venus to replace local fids with global ones
376 * during reintegration.
377 *
378 * CODA_REPLACE -- replace one CodaFid with another throughout the name cache
379 */
380
381int handleDownCall(opcode, out)
382 int opcode; union outputArgs *out;
383{
384 int error;
385
386 /* Handle invalidate requests. */
387 switch (opcode) {
388 case CODA_FLUSH : {
389
390 coda_flush(IS_DOWNCALL);
391
392 CODADEBUG(CODA_FLUSH,coda_testflush();) /* print remaining cnodes */
393 return(0);
394 }
395
396 case CODA_PURGEUSER : {
397 coda_clstat.ncalls++;
398 coda_clstat.reqs[CODA_PURGEUSER]++;
399
400 /* XXX - need to prevent fsync's */
401#ifdef CODA_COMPAT_5
402 coda_nc_purge_user(out->coda_purgeuser.cred.cr_uid, IS_DOWNCALL);
403#else
404 coda_nc_purge_user(out->coda_purgeuser.uid, IS_DOWNCALL);
405#endif
406 return(0);
407 }
408
409 case CODA_ZAPFILE : {
410 struct cnode *cp;
411
412 error = 0;
413 coda_clstat.ncalls++;
414 coda_clstat.reqs[CODA_ZAPFILE]++;
415
416 cp = coda_find(&out->coda_zapfile.Fid);
417 if (cp != NULL) {
418 vref(CTOV(cp));
419
420 cp->c_flags &= ~C_VATTR;
421 ASSERT_VOP_LOCKED(CTOV(cp), "coda HandleDownCall");
422 if (CTOV(cp)->v_vflag & VV_TEXT)
423 error = coda_vmflush(cp);
424 CODADEBUG(CODA_ZAPFILE,
425 myprintf(("zapfile: fid = %s, refcnt = %d, error = %d\n",
426 coda_f2s(&cp->c_fid), CTOV(cp)->v_usecount - 1, error)););
427 if (vrefcnt(CTOV(cp)) == 1) {
428 cp->c_flags |= C_PURGING;
429 }
430 vrele(CTOV(cp));
431 }
432
433 return(error);
434 }
435
436 case CODA_ZAPDIR : {
437 struct cnode *cp;
438
439 coda_clstat.ncalls++;
440 coda_clstat.reqs[CODA_ZAPDIR]++;
441
442 cp = coda_find(&out->coda_zapdir.Fid);
443 if (cp != NULL) {
444 vref(CTOV(cp));
445
446 cp->c_flags &= ~C_VATTR;
447 coda_nc_zapParentfid(&out->coda_zapdir.Fid, IS_DOWNCALL);
448
449 CODADEBUG(CODA_ZAPDIR, myprintf((
450 "zapdir: fid = %s, refcnt = %d\n",
451 coda_f2s(&cp->c_fid), CTOV(cp)->v_usecount - 1)););
452 if (vrefcnt(CTOV(cp)) == 1) {
453 cp->c_flags |= C_PURGING;
454 }
455 vrele(CTOV(cp));
456 }
457
458 return(0);
459 }
460
461 case CODA_PURGEFID : {
462 struct cnode *cp;
463
464 error = 0;
465 coda_clstat.ncalls++;
466 coda_clstat.reqs[CODA_PURGEFID]++;
467
468 cp = coda_find(&out->coda_purgefid.Fid);
469 if (cp != NULL) {
470 vref(CTOV(cp));
471 if (IS_DIR(out->coda_purgefid.Fid)) { /* Vnode is a directory */
472 coda_nc_zapParentfid(&out->coda_purgefid.Fid,IS_DOWNCALL);
473 }
474 cp->c_flags &= ~C_VATTR;
475 coda_nc_zapfid(&out->coda_purgefid.Fid, IS_DOWNCALL);
476 ASSERT_VOP_LOCKED(CTOV(cp), "coda HandleDownCall");
477 if (!(IS_DIR(out->coda_purgefid.Fid))
478 && (CTOV(cp)->v_vflag & VV_TEXT)) {
479
480 error = coda_vmflush(cp);
481 }
482 CODADEBUG(CODA_PURGEFID, myprintf((
483 "purgefid: fid = %s, refcnt = %d, error = %d\n",
484 coda_f2s(&cp->c_fid), CTOV(cp)->v_usecount - 1, error)););
485 if (vrefcnt(CTOV(cp)) == 1) {
486 cp->c_flags |= C_PURGING;
487 }
488 vrele(CTOV(cp));
489 }
490 return(error);
491 }
492
493 case CODA_REPLACE : {
494 struct cnode *cp = NULL;
495
496 coda_clstat.ncalls++;
497 coda_clstat.reqs[CODA_REPLACE]++;
498
499 cp = coda_find(&out->coda_replace.OldFid);
500 if (cp != NULL) {
501 /* remove the cnode from the hash table, replace the fid, and reinsert */
502 vref(CTOV(cp));
503 coda_unsave(cp);
504 cp->c_fid = out->coda_replace.NewFid;
505 coda_save(cp);
506
507 CODADEBUG(CODA_REPLACE, myprintf((
508 "replace: oldfid = %s, newfid = %s, cp = %p\n",
509 coda_f2s(&out->coda_replace.OldFid),
510 coda_f2s(&cp->c_fid), cp));) vrele(CTOV(cp));
511 }
512 return (0);
513 }
514 default:
515 myprintf(("handleDownCall: unknown opcode %d\n", opcode));
516 return (EINVAL);
517 }
518}
519
520/* coda_grab_vnode: lives in either cfs_mach.c or cfs_nbsd.c */
521
522int
523coda_vmflush(cp)
524 struct cnode *cp;
525{
526 return 0;
527}
528
529
530/*
531 * kernel-internal debugging switches
532 */
533void coda_debugon(void)
534{
535 codadebug = -1;
536 coda_nc_debug = -1;
537 coda_vnop_print_entry = 1;
538 coda_psdev_print_entry = 1;
539 coda_vfsop_print_entry = 1;
540}
541
542void coda_debugoff(void)
543{
544 codadebug = 0;
545 coda_nc_debug = 0;
546 coda_vnop_print_entry = 0;
547 coda_psdev_print_entry = 0;
548 coda_vfsop_print_entry = 0;
549}
550
551/*
552 * Utilities used by both client and server
553 * Standard levels:
554 * 0) no debugging
555 * 1) hard failures
556 * 2) soft failures
557 * 3) current test software
558 * 4) main procedure entry points
559 * 5) main procedure exit points
560 * 6) utility procedure entry points
561 * 7) utility procedure exit points
562 * 8) obscure procedure entry points
563 * 9) obscure procedure exit points
564 * 10) random stuff
565 * 11) all <= 1
566 * 12) all <= 2
567 * 13) all <= 3
568 * ...
569 */