1 /* $NetBSD: coda_namecache.c,v 1.30 2024/05/17 23:57:46 thorpej 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_namecache.c,v 1.1.1.1 1998/08/29 21:26:45 rvb Exp $
32 */
33
34 /*
35 * Mach Operating System
36 * Copyright (c) 1990 Carnegie-Mellon University
37 * Copyright (c) 1989 Carnegie-Mellon University
38 * All rights reserved. The CMU software License Agreement specifies
39 * the terms and conditions for use and redistribution.
40 */
41
42 /*
43 * This code was written for the Coda file system at Carnegie Mellon University.
44 * Contributers include David Steere, James Kistler, and M. Satyanarayanan.
45 */
46
47 /*
48 * This module contains the routines to implement the CODA name cache. The
49 * purpose of this cache is to reduce the cost of translating pathnames
50 * into Vice FIDs. Each entry in the cache contains the name of the file,
51 * the vnode (FID) of the parent directory, and the cred structure of the
52 * user accessing the file.
53 *
54 * The first time a file is accessed, it is looked up by the local Venus
55 * which first insures that the user has access to the file. In addition
56 * we are guaranteed that Venus will invalidate any name cache entries in
57 * case the user no longer should be able to access the file. For these
58 * reasons we do not need to keep access list information as well as a
59 * cred structure for each entry.
60 *
61 * The table can be accessed through the routines cnc_init(), cnc_enter(),
62 * cnc_lookup(), cnc_rmfidcred(), cnc_rmfid(), cnc_rmcred(), and cnc_purge().
63 * There are several other routines which aid in the implementation of the
64 * hash table.
65 */
66
67 /*
68 * NOTES: rvb@cs
69 * 1. The name cache holds a reference to every vnode in it. Hence files can not be
70 * closed or made inactive until they are released.
71 * 2. coda_nc_name(cp) was added to get a name for a cnode pointer for debugging.
72 * 3. coda_nc_find() has debug code to detect when entries are stored with different
73 * credentials. We don't understand yet, if/how entries are NOT EQ but still
74 * EQUAL
75 * 4. I wonder if this name cache could be replace by the vnode name cache.
76 * The latter has no zapping functions, so probably not.
77 */
78
79 #include <sys/cdefs.h>
80 __KERNEL_RCSID(0, "$NetBSD: coda_namecache.c,v 1.30 2024/05/17 23:57:46 thorpej Exp $");
81
82 #include <sys/param.h>
83 #include <sys/errno.h>
84 #include <sys/select.h>
85 #include <sys/kauth.h>
86
87 #include <coda/coda.h>
88 #include <coda/cnode.h>
89 #include <coda/coda_namecache.h>
90 #include <coda/coda_subr.h>
91
92 /*
93 * Declaration of the name cache data structure.
94 */
95
96 int coda_nc_use = 1; /* Indicate use of CODA Name Cache */
97
98 int coda_nc_size = CODA_NC_CACHESIZE; /* size of the cache */
99 int coda_nc_hashsize = CODA_NC_HASHSIZE; /* size of the primary hash */
100
101 struct coda_cache *coda_nc_heap; /* pointer to the cache entries */
102 struct coda_hash *coda_nc_hash; /* hash table of cfscache pointers */
103 struct coda_lru coda_nc_lru; /* head of lru chain */
104
105 struct coda_nc_statistics coda_nc_stat; /* Keep various stats */
106
107 /*
108 * for testing purposes
109 */
110 int coda_nc_debug = 0;
111
112 /*
113 * Entry points for the CODA Name Cache
114 */
115 static struct coda_cache *
116 coda_nc_find(struct cnode *dcp, const char *name, int namelen,
117 kauth_cred_t cred, int hash);
118 static void
119 coda_nc_remove(struct coda_cache *cncp, enum dc_status dcstat);
120
121 /*
122 * Initialize the cache, the LRU structure and the Hash structure(s)
123 */
124
125 #define TOTAL_CACHE_SIZE (sizeof(struct coda_cache) * coda_nc_size)
126 #define TOTAL_HASH_SIZE (sizeof(struct coda_hash) * coda_nc_hashsize)
127
128 int coda_nc_initialized = 0; /* Initially the cache has not been initialized */
129
130 void
coda_nc_init(void)131 coda_nc_init(void)
132 {
133 int i;
134
135 /* zero the statistics structure */
136
137 memset(&coda_nc_stat, 0, (sizeof(struct coda_nc_statistics)));
138
139 #ifdef CODA_VERBOSE
140 printf("CODA NAME CACHE: CACHE %d, HASH TBL %d\n", CODA_NC_CACHESIZE, CODA_NC_HASHSIZE);
141 #endif
142 CODA_ALLOC(coda_nc_heap, struct coda_cache *, TOTAL_CACHE_SIZE);
143 CODA_ALLOC(coda_nc_hash, struct coda_hash *, TOTAL_HASH_SIZE);
144
145 memset(coda_nc_heap, 0, TOTAL_CACHE_SIZE);
146 memset(coda_nc_hash, 0, TOTAL_HASH_SIZE);
147
148 TAILQ_INIT(&coda_nc_lru.head);
149
150 for (i=0; i < coda_nc_size; i++) { /* initialize the heap */
151 TAILQ_INSERT_HEAD(&coda_nc_lru.head, &coda_nc_heap[i], lru);
152 }
153
154 for (i=0; i < coda_nc_hashsize; i++) { /* initialize the hashtable */
155 LIST_INIT(&coda_nc_hash[i].head);
156 }
157
158 coda_nc_initialized++;
159 }
160
161 /*
162 * Auxiliary routines -- shouldn't be entry points
163 */
164
165 static struct coda_cache *
coda_nc_find(struct cnode * dcp,const char * name,int namelen,kauth_cred_t cred,int hash)166 coda_nc_find(struct cnode *dcp, const char *name, int namelen,
167 kauth_cred_t cred, int hash)
168 {
169 /*
170 * hash to find the appropriate bucket, look through the chain
171 * for the right entry (especially right cred, unless cred == 0)
172 */
173 struct coda_cache *cncp;
174 int count = 1;
175
176 CODA_NC_DEBUG(CODA_NC_FIND,
177 myprintf(("coda_nc_find(dcp %p, name %s, len %d, cred %p, hash %d\n",
178 dcp, name, namelen, cred, hash));)
179
180 LIST_FOREACH(cncp, &coda_nc_hash[hash].head, hash)
181 {
182
183 if ((CODA_NAMEMATCH(cncp, name, namelen, dcp)) &&
184 ((cred == 0) || (cncp->cred == cred)))
185 {
186 /* compare cr_uid instead */
187 coda_nc_stat.Search_len += count;
188 return(cncp);
189 }
190 #ifdef DEBUG
191 else if (CODA_NAMEMATCH(cncp, name, namelen, dcp)) {
192 printf("coda_nc_find: name %s, new cred = %p, cred = %p\n",
193 name, cred, cncp->cred);
194 printf("nref %d, nuid %d, ngid %d // oref %d, ocred %d, ogid %d\n",
195 kauth_cred_getrefcnt(cred),
196 kauth_cred_geteuid(cred),
197 kauth_cred_getegid(cred),
198 kauth_cred_getrefcnt(cncp->cred),
199 kauth_cred_geteuid(cncp->cred),
200 kauth_cred_getegid(cncp->cred));
201 coda_print_cred(cred);
202 coda_print_cred(cncp->cred);
203 }
204 #endif
205 count++;
206 }
207
208 return((struct coda_cache *)0);
209 }
210
211 /*
212 * Enter a new (dir cnode, name) pair into the cache, updating the
213 * LRU and Hash as needed.
214 */
215 void
coda_nc_enter(struct cnode * dcp,const char * name,int namelen,kauth_cred_t cred,struct cnode * cp)216 coda_nc_enter(struct cnode *dcp, const char *name, int namelen,
217 kauth_cred_t cred, struct cnode *cp)
218 {
219 struct coda_cache *cncp;
220 int hash;
221
222 if (coda_nc_use == 0) /* Cache is off */
223 return;
224
225 CODA_NC_DEBUG(CODA_NC_ENTER,
226 myprintf(("Enter: dcp %p cp %p name %s cred %p \n",
227 dcp, cp, name, cred)); )
228
229 if (namelen > CODA_NC_NAMELEN) {
230 CODA_NC_DEBUG(CODA_NC_ENTER,
231 myprintf(("long name enter %s\n",name));)
232 coda_nc_stat.long_name_enters++; /* record stats */
233 return;
234 }
235
236 hash = CODA_NC_HASH(name, namelen, dcp);
237 cncp = coda_nc_find(dcp, name, namelen, cred, hash);
238 if (cncp != (struct coda_cache *) 0) {
239 coda_nc_stat.dbl_enters++; /* duplicate entry */
240 return;
241 }
242
243 coda_nc_stat.enters++; /* record the enters statistic */
244
245 /* Grab the next element in the lru chain */
246 cncp = TAILQ_FIRST(&coda_nc_lru.head);
247 TAILQ_REMOVE(&coda_nc_lru.head, cncp, lru);
248
249 if (CODA_NC_VALID(cncp)) {
250 /* Seems really ugly, but we have to decrement the appropriate
251 hash bucket length here, so we have to find the hash bucket
252 */
253 coda_nc_hash[CODA_NC_HASH(cncp->name, cncp->namelen, cncp->dcp)].length--;
254
255 coda_nc_stat.lru_rm++; /* zapped a valid entry */
256 LIST_REMOVE(cncp, hash);
257 vrele(CTOV(cncp->dcp));
258 vrele(CTOV(cncp->cp));
259 kauth_cred_free(cncp->cred);
260 }
261
262 /*
263 * Put a hold on the current vnodes and fill in the cache entry.
264 */
265 vref(CTOV(cp));
266 vref(CTOV(dcp));
267 kauth_cred_hold(cred);
268 cncp->dcp = dcp;
269 cncp->cp = cp;
270 cncp->namelen = namelen;
271 cncp->cred = cred;
272
273 memcpy(cncp->name, name, (unsigned)namelen);
274
275 /* Insert into the lru and hash chains. */
276 TAILQ_INSERT_TAIL(&coda_nc_lru.head, cncp, lru);
277 LIST_INSERT_HEAD(&coda_nc_hash[hash].head, cncp, hash);
278 coda_nc_hash[hash].length++; /* Used for tuning */
279
280 CODA_NC_DEBUG(CODA_NC_PRINTCODA_NC, print_coda_nc(); )
281 }
282
283 /*
284 * Find the (dir cnode, name) pair in the cache, if its cred
285 * matches the input, return it, otherwise return 0
286 */
287 struct cnode *
coda_nc_lookup(struct cnode * dcp,const char * name,int namelen,kauth_cred_t cred)288 coda_nc_lookup(struct cnode *dcp, const char *name, int namelen,
289 kauth_cred_t cred)
290 {
291 int hash;
292 struct coda_cache *cncp;
293
294 if (coda_nc_use == 0) /* Cache is off */
295 return((struct cnode *) 0);
296
297 if (namelen > CODA_NC_NAMELEN) {
298 CODA_NC_DEBUG(CODA_NC_LOOKUP,
299 myprintf(("long name lookup %s\n",name));)
300 coda_nc_stat.long_name_lookups++; /* record stats */
301 return((struct cnode *) 0);
302 }
303
304 /* Use the hash function to locate the starting point,
305 then the search routine to go down the list looking for
306 the correct cred.
307 */
308
309 hash = CODA_NC_HASH(name, namelen, dcp);
310 cncp = coda_nc_find(dcp, name, namelen, cred, hash);
311 if (cncp == (struct coda_cache *) 0) {
312 coda_nc_stat.misses++; /* record miss */
313 return((struct cnode *) 0);
314 }
315
316 coda_nc_stat.hits++;
317
318 /* put this entry at the end of the LRU */
319 TAILQ_REMOVE(&coda_nc_lru.head, cncp, lru);
320 TAILQ_INSERT_TAIL(&coda_nc_lru.head, cncp, lru);
321
322 /* move it to the front of the hash chain */
323 /* don't need to change the hash bucket length */
324 LIST_REMOVE(cncp, hash);
325 LIST_INSERT_HEAD(&coda_nc_hash[hash].head, cncp, hash);
326
327 CODA_NC_DEBUG(CODA_NC_LOOKUP,
328 printf("lookup: dcp %p, name %s, cred %p = cp %p\n",
329 dcp, name, cred, cncp->cp); )
330
331 return(cncp->cp);
332 }
333
334 static void
coda_nc_remove(struct coda_cache * cncp,enum dc_status dcstat)335 coda_nc_remove(struct coda_cache *cncp, enum dc_status dcstat)
336 {
337 /*
338 * remove an entry -- vrele(cncp->dcp, cp), crfree(cred),
339 * remove it from its hash chain, and
340 * place it at the head of the lru list.
341 */
342 CODA_NC_DEBUG(CODA_NC_REMOVE,
343 myprintf(("coda_nc_remove %s from parent %s\n",
344 cncp->name, coda_f2s(&cncp->dcp->c_fid))); )
345
346
347 LIST_REMOVE(cncp, hash);
348 memset(&cncp->hash, 0, sizeof(cncp->hash));
349
350 if ((dcstat == IS_DOWNCALL) && (vrefcnt(CTOV(cncp->dcp)) == 1)) {
351 cncp->dcp->c_flags |= C_PURGING;
352 }
353 vrele(CTOV(cncp->dcp));
354
355 if ((dcstat == IS_DOWNCALL) && (vrefcnt(CTOV(cncp->cp)) == 1)) {
356 cncp->cp->c_flags |= C_PURGING;
357 }
358 vrele(CTOV(cncp->cp));
359
360 kauth_cred_free(cncp->cred);
361 memset(DATA_PART(cncp), 0, DATA_SIZE);
362
363 /* move the null entry to the front for reuse */
364 TAILQ_REMOVE(&coda_nc_lru.head, cncp, lru);
365 TAILQ_INSERT_HEAD(&coda_nc_lru.head, cncp, lru);
366 }
367
368 /*
369 * Remove all entries with a parent which has the input fid.
370 */
371 void
coda_nc_zapParentfid(CodaFid * fid,enum dc_status dcstat)372 coda_nc_zapParentfid(CodaFid *fid, enum dc_status dcstat)
373 {
374 /* To get to a specific fid, we might either have another hashing
375 function or do a sequential search through the cache for the
376 appropriate entries. The later may be acceptable since I don't
377 think callbacks or whatever Case 1 covers are frequent occurrences.
378 */
379 struct coda_cache *cncp, *ncncp;
380 int i;
381
382 if (coda_nc_use == 0) /* Cache is off */
383 return;
384
385 CODA_NC_DEBUG(CODA_NC_ZAPPFID,
386 myprintf(("ZapParent: fid %s\n", coda_f2s(fid))); )
387
388 coda_nc_stat.zapPfids++;
389
390 for (i = 0; i < coda_nc_hashsize; i++) {
391
392 /*
393 * Need to save the hash_next pointer in case we remove the
394 * entry. remove causes hash_next to point to itself.
395 */
396
397 ncncp = LIST_FIRST(&coda_nc_hash[i].head);
398 while ((cncp = ncncp) != NULL) {
399 ncncp = LIST_NEXT(cncp, hash);
400
401 if (coda_fid_eq(&(cncp->dcp->c_fid), fid)) {
402 coda_nc_hash[i].length--; /* Used for tuning */
403 coda_nc_remove(cncp, dcstat);
404 }
405 }
406 }
407 }
408
409 /*
410 * Remove all entries which have the same fid as the input
411 */
412 void
coda_nc_zapfid(CodaFid * fid,enum dc_status dcstat)413 coda_nc_zapfid(CodaFid *fid, enum dc_status dcstat)
414 {
415 /* See comment for zapParentfid. This routine will be used
416 if attributes are being cached.
417 */
418 struct coda_cache *cncp, *ncncp;
419 int i;
420
421 if (coda_nc_use == 0) /* Cache is off */
422 return;
423
424 CODA_NC_DEBUG(CODA_NC_ZAPFID,
425 myprintf(("Zapfid: fid %s\n", coda_f2s(fid))); )
426
427 coda_nc_stat.zapFids++;
428
429 for (i = 0; i < coda_nc_hashsize; i++) {
430
431 ncncp = LIST_FIRST(&coda_nc_hash[i].head);
432 while ((cncp = ncncp) != NULL) {
433 ncncp = LIST_NEXT(cncp, hash);
434
435 if (coda_fid_eq(&cncp->cp->c_fid, fid)) {
436 coda_nc_hash[i].length--; /* Used for tuning */
437 coda_nc_remove(cncp, dcstat);
438 }
439 }
440 }
441 }
442
443 /*
444 * Remove all entries which match the fid and the cred
445 */
446 void
coda_nc_zapvnode(CodaFid * fid,kauth_cred_t cred,enum dc_status dcstat)447 coda_nc_zapvnode(CodaFid *fid, kauth_cred_t cred,
448 enum dc_status dcstat)
449 {
450 /* See comment for zapfid. I don't think that one would ever
451 want to zap a file with a specific cred from the kernel.
452 We'll leave this one unimplemented.
453 */
454 if (coda_nc_use == 0) /* Cache is off */
455 return;
456
457 CODA_NC_DEBUG(CODA_NC_ZAPVNODE,
458 myprintf(("Zapvnode: fid %s cred %p\n",
459 coda_f2s(fid), cred)); )
460 }
461
462 /*
463 * Remove all entries which have the (dir vnode, name) pair
464 */
465 void
coda_nc_zapfile(struct cnode * dcp,const char * name,int namelen)466 coda_nc_zapfile(struct cnode *dcp, const char *name, int namelen)
467 {
468 /* use the hash function to locate the file, then zap all
469 entries of it regardless of the cred.
470 */
471 struct coda_cache *cncp;
472 int hash;
473
474 if (coda_nc_use == 0) /* Cache is off */
475 return;
476
477 CODA_NC_DEBUG(CODA_NC_ZAPFILE,
478 myprintf(("Zapfile: dcp %p name %s \n",
479 dcp, name)); )
480
481 if (namelen > CODA_NC_NAMELEN) {
482 coda_nc_stat.long_remove++; /* record stats */
483 return;
484 }
485
486 coda_nc_stat.zapFile++;
487
488 hash = CODA_NC_HASH(name, namelen, dcp);
489 cncp = coda_nc_find(dcp, name, namelen, 0, hash);
490
491 while (cncp) {
492 coda_nc_hash[hash].length--; /* Used for tuning */
493 /* 1.3 */
494 coda_nc_remove(cncp, NOT_DOWNCALL);
495 cncp = coda_nc_find(dcp, name, namelen, 0, hash);
496 }
497 }
498
499 /*
500 * Remove all the entries for a particular user. Used when tokens expire.
501 * A user is determined by his/her effective user id (id_uid).
502 */
503 void
coda_nc_purge_user(uid_t uid,enum dc_status dcstat)504 coda_nc_purge_user(uid_t uid, enum dc_status dcstat)
505 {
506 /*
507 * I think the best approach is to go through the entire cache
508 * via HASH or whatever and zap all entries which match the
509 * input cred. Or just flush the whole cache. It might be
510 * best to go through on basis of LRU since cache will almost
511 * always be full and LRU is more straightforward.
512 */
513
514 struct coda_cache *cncp, *ncncp;
515 int hash;
516
517 if (coda_nc_use == 0) /* Cache is off */
518 return;
519
520 CODA_NC_DEBUG(CODA_NC_PURGEUSER,
521 myprintf(("ZapDude: uid %x\n", uid)); )
522 coda_nc_stat.zapUsers++;
523
524 ncncp = TAILQ_FIRST(&coda_nc_lru.head);
525 while ((cncp = ncncp) != NULL) {
526 ncncp = TAILQ_NEXT(cncp, lru);
527
528 if ((CODA_NC_VALID(cncp)) &&
529 (kauth_cred_geteuid(cncp->cred) == uid)) {
530 /* Seems really ugly, but we have to decrement the appropriate
531 hash bucket length here, so we have to find the hash bucket
532 */
533 hash = CODA_NC_HASH(cncp->name, cncp->namelen, cncp->dcp);
534 coda_nc_hash[hash].length--; /* For performance tuning */
535
536 coda_nc_remove(cncp, dcstat);
537 }
538 }
539 }
540
541 /*
542 * Flush the entire name cache. In response to a flush of the Venus cache.
543 */
544 void
coda_nc_flush(enum dc_status dcstat)545 coda_nc_flush(enum dc_status dcstat)
546 {
547 /* One option is to deallocate the current name cache and
548 call init to start again. Or just deallocate, then rebuild.
549 Or again, we could just go through the array and zero the
550 appropriate fields.
551 */
552
553 /*
554 * Go through the whole lru chain and kill everything as we go.
555 * I don't use remove since that would rebuild the lru chain
556 * as it went and that seemed unnecessary.
557 */
558 struct coda_cache *cncp;
559 int i;
560
561 if (coda_nc_use == 0) /* Cache is off */
562 return;
563
564 coda_nc_stat.Flushes++;
565
566 TAILQ_FOREACH(cncp, &coda_nc_lru.head, lru) {
567 if (CODA_NC_VALID(cncp)) { /* only zero valid nodes */
568 LIST_REMOVE(cncp, hash);
569 memset(&cncp->hash, 0, sizeof(cncp->hash));
570
571 if ((dcstat == IS_DOWNCALL)
572 && (vrefcnt(CTOV(cncp->dcp)) == 1))
573 {
574 cncp->dcp->c_flags |= C_PURGING;
575 }
576 vrele(CTOV(cncp->dcp));
577
578 if (CTOV(cncp->cp)->v_iflag & VI_TEXT) {
579 if (coda_vmflush(cncp->cp))
580 CODADEBUG(CODA_FLUSH,
581 myprintf(("coda_nc_flush: %s busy\n",
582 coda_f2s(&cncp->cp->c_fid))); )
583 }
584
585 if ((dcstat == IS_DOWNCALL)
586 && (vrefcnt(CTOV(cncp->cp)) == 1))
587 {
588 cncp->cp->c_flags |= C_PURGING;
589 }
590 vrele(CTOV(cncp->cp));
591
592 kauth_cred_free(cncp->cred);
593 memset(DATA_PART(cncp), 0, DATA_SIZE);
594 }
595 }
596
597 for (i = 0; i < coda_nc_hashsize; i++)
598 coda_nc_hash[i].length = 0;
599 }
600
601 /*
602 * Debugging routines
603 */
604
605 /*
606 * This routine should print out all the hash chains to the console.
607 */
608 void
print_coda_nc(void)609 print_coda_nc(void)
610 {
611 int hash;
612 struct coda_cache *cncp;
613
614 for (hash = 0; hash < coda_nc_hashsize; hash++) {
615 myprintf(("\nhash %d\n",hash));
616
617 LIST_FOREACH(cncp, &coda_nc_hash[hash].head, hash) {
618 myprintf(("cp %p dcp %p cred %p name %s\n",
619 cncp->cp, cncp->dcp,
620 cncp->cred, cncp->name));
621 }
622 }
623 }
624
625 void
coda_nc_gather_stats(void)626 coda_nc_gather_stats(void)
627 {
628 int i, xmax = 0, sum = 0, temp, zeros = 0, ave, n;
629
630 for (i = 0; i < coda_nc_hashsize; i++) {
631 if (coda_nc_hash[i].length) {
632 sum += coda_nc_hash[i].length;
633 } else {
634 zeros++;
635 }
636
637 if (coda_nc_hash[i].length > xmax)
638 xmax = coda_nc_hash[i].length;
639 }
640
641 /*
642 * When computing the Arithmetic mean, only count slots which
643 * are not empty in the distribution.
644 */
645 coda_nc_stat.Sum_bucket_len = sum;
646 coda_nc_stat.Num_zero_len = zeros;
647 coda_nc_stat.Max_bucket_len = xmax;
648
649 if ((n = coda_nc_hashsize - zeros) > 0)
650 ave = sum / n;
651 else
652 ave = 0;
653
654 sum = 0;
655 for (i = 0; i < coda_nc_hashsize; i++) {
656 if (coda_nc_hash[i].length) {
657 temp = coda_nc_hash[i].length - ave;
658 sum += temp * temp;
659 }
660 }
661 coda_nc_stat.Sum2_bucket_len = sum;
662 }
663
664 /*
665 * The purpose of this routine is to allow the hash and cache sizes to be
666 * changed dynamically. This should only be used in controlled environments,
667 * it makes no effort to lock other users from accessing the cache while it
668 * is in an improper state (except by turning the cache off).
669 */
670 int
coda_nc_resize(int hashsize,int heapsize,enum dc_status dcstat)671 coda_nc_resize(int hashsize, int heapsize, enum dc_status dcstat)
672 {
673 if ((hashsize % 2) || (heapsize % 2)) { /* Illegal hash or cache sizes */
674 return(EINVAL);
675 }
676
677 coda_nc_use = 0; /* Turn the cache off */
678
679 coda_nc_flush(dcstat); /* free any cnodes in the cache */
680
681 /* WARNING: free must happen *before* size is reset */
682 CODA_FREE(coda_nc_heap,TOTAL_CACHE_SIZE);
683 CODA_FREE(coda_nc_hash,TOTAL_HASH_SIZE);
684
685 coda_nc_hashsize = hashsize;
686 coda_nc_size = heapsize;
687
688 coda_nc_init(); /* Set up a cache with the new size */
689
690 coda_nc_use = 1; /* Turn the cache back on */
691 return(0);
692 }
693
694 char coda_nc_name_buf[CODA_MAXNAMLEN+1];
695
696 void
coda_nc_name(struct cnode * cp)697 coda_nc_name(struct cnode *cp)
698 {
699 struct coda_cache *cncp;
700 int i;
701
702 if (coda_nc_use == 0) /* Cache is off */
703 return;
704
705 for (i = 0; i < coda_nc_hashsize; i++) {
706
707 LIST_FOREACH(cncp, &coda_nc_hash[i].head, hash) {
708 if (cncp->cp == cp) {
709 memcpy(coda_nc_name_buf, cncp->name, cncp->namelen);
710 coda_nc_name_buf[cncp->namelen] = 0;
711 printf(" is %s (%p,%p)@%p",
712 coda_nc_name_buf, cncp->cp, cncp->dcp, cncp);
713 }
714
715 }
716 }
717 }
718