coda_subr.c revision 1.29 1 /* $NetBSD: coda_subr.c,v 1.29 2014/12/13 15:58:39 hannken Exp $ */
2
3 /*
4 *
5 * Coda: an Experimental Distributed File System
6 * Release 3.1
7 *
8 * Copyright (c) 1987-1998 Carnegie Mellon University
9 * All Rights Reserved
10 *
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.
19 *
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.
26 *
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.
30 *
31 * @(#) coda/coda_subr.c,v 1.1.1.1 1998/08/29 21:26:45 rvb Exp $
32 */
33
34 /*
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.
39 */
40
41 /*
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. */
45
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
55 */
56
57 #include <sys/cdefs.h>
58 __KERNEL_RCSID(0, "$NetBSD: coda_subr.c,v 1.29 2014/12/13 15:58:39 hannken Exp $");
59
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>
66 #include <sys/kauth.h>
67
68 #include <coda/coda.h>
69 #include <coda/cnode.h>
70 #include <coda/coda_subr.h>
71 #include <coda/coda_namecache.h>
72
73
74 int coda_active = 0;
75 int coda_reuse = 0;
76 int coda_new = 0;
77
78 struct cnode *coda_freelist = NULL;
79 struct cnode *coda_cache[CODA_CACHESIZE];
80 MALLOC_DEFINE(M_CODA, "coda", "Coda file system structures and tables");
81
82 int codadebug = 0;
83 int coda_printf_delay = 0; /* in microseconds */
84 int coda_vnop_print_entry = 0;
85 int coda_vfsop_print_entry = 0;
86
87 #define CNODE_NEXT(cp) ((cp)->c_next)
88
89 #ifdef CODA_COMPAT_5
90 #define coda_hash(fid) \
91 (((fid)->Volume + (fid)->Vnode) & (CODA_CACHESIZE-1))
92 #define IS_DIR(cnode) (cnode.Vnode & 0x1)
93 #else
94 #define coda_hash(fid) \
95 (coda_f2i(fid) & (CODA_CACHESIZE-1))
96 #define IS_DIR(cnode) (cnode.opaque[2] & 0x1)
97 #endif
98
99 struct vnode *coda_ctlvp;
100
101 /*
102 * Allocate a cnode.
103 */
104 struct cnode *
105 coda_alloc(void)
106 {
107 struct cnode *cp;
108
109 if (coda_freelist) {
110 cp = coda_freelist;
111 coda_freelist = CNODE_NEXT(cp);
112 coda_reuse++;
113 }
114 else {
115 CODA_ALLOC(cp, struct cnode *, sizeof(struct cnode));
116 /* NetBSD vnodes don't have any Pager info in them ('cause there are
117 no external pagers, duh!) */
118 #define VNODE_VM_INFO_INIT(vp) /* MT */
119 VNODE_VM_INFO_INIT(CTOV(cp));
120 coda_new++;
121 }
122 memset(cp, 0, sizeof (struct cnode));
123
124 return(cp);
125 }
126
127 /*
128 * Deallocate a cnode.
129 */
130 void
131 coda_free(struct cnode *cp)
132 {
133
134 CNODE_NEXT(cp) = coda_freelist;
135 coda_freelist = cp;
136 }
137
138 /*
139 * Put a cnode in the hash table
140 */
141 void
142 coda_save(struct cnode *cp)
143 {
144 CNODE_NEXT(cp) = coda_cache[coda_hash(&cp->c_fid)];
145 coda_cache[coda_hash(&cp->c_fid)] = cp;
146 }
147
148 /*
149 * Remove a cnode from the hash table
150 */
151 void
152 coda_unsave(struct cnode *cp)
153 {
154 struct cnode *ptr;
155 struct cnode *ptrprev = NULL;
156
157 ptr = coda_cache[coda_hash(&cp->c_fid)];
158 while (ptr != NULL) {
159 if (ptr == cp) {
160 if (ptrprev == NULL) {
161 coda_cache[coda_hash(&cp->c_fid)]
162 = CNODE_NEXT(ptr);
163 } else {
164 CNODE_NEXT(ptrprev) = CNODE_NEXT(ptr);
165 }
166 CNODE_NEXT(cp) = NULL;
167
168 return;
169 }
170 ptrprev = ptr;
171 ptr = CNODE_NEXT(ptr);
172 }
173 }
174
175 /*
176 * Lookup a cnode by fid. If the cnode is dying, it is bogus so skip it.
177 * NOTE: this allows multiple cnodes with same fid -- dcs 1/25/95
178 */
179 struct cnode *
180 coda_find(CodaFid *fid)
181 {
182 struct cnode *cp;
183
184 cp = coda_cache[coda_hash(fid)];
185 while (cp) {
186 if (coda_fid_eq(&(cp->c_fid), fid) &&
187 (!IS_UNMOUNTING(cp)))
188 {
189 coda_active++;
190 return(cp);
191 }
192 cp = CNODE_NEXT(cp);
193 }
194 return(NULL);
195 }
196
197 /*
198 * coda_kill is called as a side effect to vcopen. To prevent any
199 * cnodes left around from an earlier run of a venus or warden from
200 * causing problems with the new instance, mark any outstanding cnodes
201 * as dying. Future operations on these cnodes should fail (excepting
202 * coda_inactive of course!). Since multiple venii/wardens can be
203 * running, only kill the cnodes for a particular entry in the
204 * coda_mnttbl. -- DCS 12/1/94 */
205
206 int
207 coda_kill(struct mount *whoIam, enum dc_status dcstat)
208 {
209 int hash, count = 0;
210 struct cnode *cp;
211
212 /*
213 * Algorithm is as follows:
214 * Second, flush whatever vnodes we can from the name cache.
215 *
216 * Finally, step through whatever is left and mark them dying.
217 * This prevents any operation at all.
218
219 */
220
221 /* This is slightly overkill, but should work. Eventually it'd be
222 * nice to only flush those entries from the namecache that
223 * reference a vnode in this vfs. */
224 coda_nc_flush(dcstat);
225
226 for (hash = 0; hash < CODA_CACHESIZE; hash++) {
227 for (cp = coda_cache[hash]; cp != NULL; cp = CNODE_NEXT(cp)) {
228 if (IS_CTL_VP(CTOV(cp)))
229 continue;
230 if (CTOV(cp)->v_mount == whoIam) {
231 #ifdef DEBUG
232 printf("coda_kill: vp %p, cp %p\n", CTOV(cp), cp);
233 #endif
234 count++;
235 CODADEBUG(CODA_FLUSH,
236 myprintf(("Live cnode fid %s flags %d count %d\n",
237 coda_f2s(&cp->c_fid),
238 cp->c_flags,
239 CTOV(cp)->v_usecount)); );
240 }
241 }
242 }
243 return count;
244 }
245
246 /*
247 * There are two reasons why a cnode may be in use, it may be in the
248 * name cache or it may be executing.
249 */
250 void
251 coda_flush(enum dc_status dcstat)
252 {
253 int hash;
254 struct cnode *cp;
255
256 coda_clstat.ncalls++;
257 coda_clstat.reqs[CODA_FLUSH]++;
258
259 coda_nc_flush(dcstat); /* flush files from the name cache */
260
261 for (hash = 0; hash < CODA_CACHESIZE; hash++) {
262 for (cp = coda_cache[hash]; cp != NULL; cp = CNODE_NEXT(cp)) {
263 if (!IS_DIR(cp->c_fid)) /* only files can be executed */
264 coda_vmflush(cp);
265 }
266 }
267 }
268
269 /*
270 * As a debugging measure, print out any cnodes that lived through a
271 * name cache flush.
272 */
273 void
274 coda_testflush(void)
275 {
276 int hash;
277 struct cnode *cp;
278
279 for (hash = 0; hash < CODA_CACHESIZE; hash++) {
280 for (cp = coda_cache[hash];
281 cp != NULL;
282 cp = CNODE_NEXT(cp)) {
283 myprintf(("Live cnode fid %s count %d\n",
284 coda_f2s(&cp->c_fid), CTOV(cp)->v_usecount));
285 }
286 }
287 }
288
289 /*
290 * First, step through all cnodes and mark them unmounting.
291 * NetBSD kernels may try to fsync them now that venus
292 * is dead, which would be a bad thing.
293 *
294 */
295 void
296 coda_unmounting(struct mount *whoIam)
297 {
298 int hash;
299 struct cnode *cp;
300
301 for (hash = 0; hash < CODA_CACHESIZE; hash++) {
302 for (cp = coda_cache[hash]; cp != NULL; cp = CNODE_NEXT(cp)) {
303 if (CTOV(cp)->v_mount == whoIam) {
304 cp->c_flags |= C_UNMOUNTING;
305 }
306 }
307 }
308 }
309
310 #ifdef DEBUG
311 void
312 coda_checkunmounting(struct mount *mp)
313 {
314 struct vnode *vp;
315 struct cnode *cp;
316 int count = 0, bad = 0;
317 loop:
318 TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
319 if (vp->v_mount != mp)
320 goto loop;
321 cp = VTOC(vp);
322 count++;
323 if (!(cp->c_flags & C_UNMOUNTING)) {
324 bad++;
325 printf("vp %p, cp %p missed\n", vp, cp);
326 cp->c_flags |= C_UNMOUNTING;
327 }
328 }
329 }
330
331 void
332 coda_cacheprint(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
381 int handleDownCall(int opcode, union outputArgs *out)
382 {
383 int error;
384
385 /* Handle invalidate requests. */
386 switch (opcode) {
387 case CODA_FLUSH : {
388
389 coda_flush(IS_DOWNCALL);
390
391 CODADEBUG(CODA_FLUSH,coda_testflush();) /* print remaining cnodes */
392 return(0);
393 }
394
395 case CODA_PURGEUSER : {
396 coda_clstat.ncalls++;
397 coda_clstat.reqs[CODA_PURGEUSER]++;
398
399 /* XXX - need to prevent fsync's */
400 #ifdef CODA_COMPAT_5
401 coda_nc_purge_user(out->coda_purgeuser.cred.cr_uid, IS_DOWNCALL);
402 #else
403 coda_nc_purge_user(out->coda_purgeuser.uid, IS_DOWNCALL);
404 #endif
405 return(0);
406 }
407
408 case CODA_ZAPFILE : {
409 struct cnode *cp;
410
411 error = 0;
412 coda_clstat.ncalls++;
413 coda_clstat.reqs[CODA_ZAPFILE]++;
414
415 cp = coda_find(&out->coda_zapfile.Fid);
416 if (cp != NULL) {
417 vref(CTOV(cp));
418
419 cp->c_flags &= ~C_VATTR;
420 if (CTOV(cp)->v_iflag & VI_TEXT)
421 error = coda_vmflush(cp);
422 CODADEBUG(CODA_ZAPFILE, myprintf((
423 "zapfile: fid = %s, refcnt = %d, error = %d\n",
424 coda_f2s(&cp->c_fid), CTOV(cp)->v_usecount - 1, error)););
425 if (CTOV(cp)->v_usecount == 1) {
426 cp->c_flags |= C_PURGING;
427 }
428 vrele(CTOV(cp));
429 }
430
431 return(error);
432 }
433
434 case CODA_ZAPDIR : {
435 struct cnode *cp;
436
437 coda_clstat.ncalls++;
438 coda_clstat.reqs[CODA_ZAPDIR]++;
439
440 cp = coda_find(&out->coda_zapdir.Fid);
441 if (cp != NULL) {
442 vref(CTOV(cp));
443
444 cp->c_flags &= ~C_VATTR;
445 coda_nc_zapParentfid(&out->coda_zapdir.Fid, IS_DOWNCALL);
446
447 CODADEBUG(CODA_ZAPDIR, myprintf((
448 "zapdir: fid = %s, refcnt = %d\n",
449 coda_f2s(&cp->c_fid), CTOV(cp)->v_usecount - 1)););
450 if (CTOV(cp)->v_usecount == 1) {
451 cp->c_flags |= C_PURGING;
452 }
453 vrele(CTOV(cp));
454 }
455
456 return(0);
457 }
458
459 case CODA_PURGEFID : {
460 struct cnode *cp;
461
462 error = 0;
463 coda_clstat.ncalls++;
464 coda_clstat.reqs[CODA_PURGEFID]++;
465
466 cp = coda_find(&out->coda_purgefid.Fid);
467 if (cp != NULL) {
468 vref(CTOV(cp));
469 if (IS_DIR(out->coda_purgefid.Fid)) { /* Vnode is a directory */
470 coda_nc_zapParentfid(&out->coda_purgefid.Fid,
471 IS_DOWNCALL);
472 }
473 cp->c_flags &= ~C_VATTR;
474 coda_nc_zapfid(&out->coda_purgefid.Fid, IS_DOWNCALL);
475 if (!(IS_DIR(out->coda_purgefid.Fid))
476 && (CTOV(cp)->v_iflag & VI_TEXT)) {
477
478 error = coda_vmflush(cp);
479 }
480 CODADEBUG(CODA_PURGEFID, myprintf((
481 "purgefid: fid = %s, refcnt = %d, error = %d\n",
482 coda_f2s(&cp->c_fid), CTOV(cp)->v_usecount - 1, error)););
483 if (CTOV(cp)->v_usecount == 1) {
484 cp->c_flags |= C_PURGING;
485 }
486 vrele(CTOV(cp));
487 }
488 return(error);
489 }
490
491 case CODA_REPLACE : {
492 struct cnode *cp = NULL;
493
494 coda_clstat.ncalls++;
495 coda_clstat.reqs[CODA_REPLACE]++;
496
497 cp = coda_find(&out->coda_replace.OldFid);
498 if (cp != NULL) {
499 /* remove the cnode from the hash table, replace the fid, and reinsert */
500 vref(CTOV(cp));
501 coda_unsave(cp);
502 cp->c_fid = out->coda_replace.NewFid;
503 coda_save(cp);
504
505 CODADEBUG(CODA_REPLACE, myprintf((
506 "replace: oldfid = %s, newfid = %s, cp = %p\n",
507 coda_f2s(&out->coda_replace.OldFid),
508 coda_f2s(&cp->c_fid), cp));)
509 vrele(CTOV(cp));
510 }
511 return (0);
512 }
513 default:
514 myprintf(("handleDownCall: unknown opcode %d\n", opcode));
515 return (EINVAL);
516 }
517 }
518
519 /* coda_grab_vnode: lives in either cfs_mach.c or cfs_nbsd.c */
520
521 int
522 coda_vmflush(struct cnode *cp)
523 {
524 return 0;
525 }
526
527
528 /*
529 * kernel-internal debugging switches
530 */
531
532 void coda_debugon(void)
533 {
534 codadebug = -1;
535 coda_nc_debug = -1;
536 coda_vnop_print_entry = 1;
537 coda_psdev_print_entry = 1;
538 coda_vfsop_print_entry = 1;
539 }
540
541 void coda_debugoff(void)
542 {
543 codadebug = 0;
544 coda_nc_debug = 0;
545 coda_vnop_print_entry = 0;
546 coda_psdev_print_entry = 0;
547 coda_vfsop_print_entry = 0;
548 }
549
550 /* How to print a ucred */
551 void
552 coda_print_cred(kauth_cred_t cred)
553 {
554
555 uint16_t ngroups;
556 int i;
557
558 myprintf(("ref %d\tuid %d\n", kauth_cred_getrefcnt(cred),
559 kauth_cred_geteuid(cred)));
560
561 ngroups = kauth_cred_ngroups(cred);
562 for (i=0; i < ngroups; i++)
563 myprintf(("\tgroup %d: (%d)\n", i, kauth_cred_group(cred, i)));
564 myprintf(("\n"));
565
566 }
567
568 /*
569 * Utilities used by both client and server
570 * Standard levels:
571 * 0) no debugging
572 * 1) hard failures
573 * 2) soft failures
574 * 3) current test software
575 * 4) main procedure entry points
576 * 5) main procedure exit points
577 * 6) utility procedure entry points
578 * 7) utility procedure exit points
579 * 8) obscure procedure entry points
580 * 9) obscure procedure exit points
581 * 10) random stuff
582 * 11) all <= 1
583 * 12) all <= 2
584 * 13) all <= 3
585 * ...
586 */
587