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_namecache.c,v 1.1.1.1 1998/08/29 21:14:52 rvb Exp $
31 * Mach Operating System
32 * Copyright (c) 1990 Carnegie-Mellon University
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 University.
40 * Contributers include David Steere, James Kistler, and M. Satyanarayanan.
44 * This module contains the routines to implement the CODA name cache. The
45 * purpose of this cache is to reduce the cost of translating pathnames
46 * into Vice FIDs. Each entry in the cache contains the name of the file,
47 * the vnode (FID) of the parent directory, and the cred structure of the
48 * user accessing the file.
50 * The first time a file is accessed, it is looked up by the local Venus
51 * which first insures that the user has access to the file. In addition
52 * we are guaranteed that Venus will invalidate any name cache entries in
53 * case the user no longer should be able to access the file. For these
54 * reasons we do not need to keep access list information as well as a
55 * cred structure for each entry.
57 * The table can be accessed through the routines cnc_init(), cnc_enter(),
58 * cnc_lookup(), cnc_rmfidcred(), cnc_rmfid(), cnc_rmcred(), and cnc_purge().
59 * There are several other routines which aid in the implementation of the
65 * 1. The name cache holds a reference to every vnode in it. Hence files can not be
66 * closed or made inactive until they are released.
67 * 2. coda_nc_name(cp) was added to get a name for a cnode pointer for debugging.
68 * 3. coda_nc_find() has debug code to detect when entries are stored with different
69 * credentials. We don't understand yet, if/how entries are NOT EQ but still
71 * 4. I wonder if this name cache could be replace by the vnode name cache.
72 * The latter has no zapping functions, so probably not.
75 #include <sys/cdefs.h>
76 __FBSDID("$FreeBSD$");
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/errno.h>
82 #include <sys/malloc.h>
83 #include <sys/mutex.h>
84 #include <sys/ucred.h>
87 #include <vm/vm_object.h>
89 #include <coda/coda.h>
90 #include <coda/cnode.h>
91 #include <coda/coda_namecache.h>
94 #include <coda/coda_vnops.h>
98 * Declaration of the name cache data structure.
101 int coda_nc_use = 1; /* Indicate use of CODA Name Cache */
102 int coda_nc_size = CODA_NC_CACHESIZE; /* size of the cache */
103 int coda_nc_hashsize = CODA_NC_HASHSIZE; /* size of the primary hash */
105 struct coda_cache *coda_nc_heap; /* pointer to the cache entries */
106 struct coda_hash *coda_nc_hash; /* hash table of coda_cache pointers */
107 struct coda_lru coda_nc_lru; /* head of lru chain */
109 struct coda_nc_statistics coda_nc_stat; /* Keep various stats */
112 * for testing purposes
114 int coda_nc_debug = 0;
117 * Entry points for the CODA Name Cache
119 static struct coda_cache *coda_nc_find(struct cnode *dcp, const char *name, int namelen,
120 struct ucred *cred, int hash);
121 static void coda_nc_remove(struct coda_cache *cncp, enum dc_status dcstat);
124 * Initialize the cache, the LRU structure and the Hash structure(s)
127 #define TOTAL_CACHE_SIZE (sizeof(struct coda_cache) * coda_nc_size)
128 #define TOTAL_HASH_SIZE (sizeof(struct coda_hash) * coda_nc_hashsize)
130 int coda_nc_initialized = 0; /* Initially the cache has not been initialized */
137 /* zero the statistics structure */
139 bzero(&coda_nc_stat, (sizeof(struct coda_nc_statistics)));
142 printf("CODA NAME CACHE: CACHE %d, HASH TBL %d\n", CODA_NC_CACHESIZE, CODA_NC_HASHSIZE);
144 CODA_ALLOC(coda_nc_heap, struct coda_cache *, TOTAL_CACHE_SIZE);
145 CODA_ALLOC(coda_nc_hash, struct coda_hash *, TOTAL_HASH_SIZE);
147 coda_nc_lru.lru_next =
148 coda_nc_lru.lru_prev = (struct coda_cache *)LRU_PART(&coda_nc_lru);
151 for (i=0; i < coda_nc_size; i++) { /* initialize the heap */
152 CODA_NC_LRUINS(&coda_nc_heap[i], &coda_nc_lru);
153 CODA_NC_HSHNUL(&coda_nc_heap[i]);
154 coda_nc_heap[i].cp = coda_nc_heap[i].dcp = (struct cnode *)0;
157 for (i=0; i < coda_nc_hashsize; i++) { /* initialize the hashtable */
158 CODA_NC_HSHNUL((struct coda_cache *)&coda_nc_hash[i]);
161 coda_nc_initialized++;
165 * Auxillary routines -- shouldn't be entry points
168 static struct coda_cache *
169 coda_nc_find(dcp, name, namelen, cred, hash)
177 * hash to find the appropriate bucket, look through the chain
178 * for the right entry (especially right cred, unless cred == 0)
180 struct coda_cache *cncp;
183 CODA_NC_DEBUG(CODA_NC_FIND,
184 myprintf(("coda_nc_find(dcp %p, name %s, len %d, cred %p, hash %d\n",
185 dcp, name, namelen, cred, hash));)
187 for (cncp = coda_nc_hash[hash].hash_next;
188 cncp != (struct coda_cache *)&coda_nc_hash[hash];
189 cncp = cncp->hash_next, count++)
192 if ((CODA_NAMEMATCH(cncp, name, namelen, dcp)) &&
193 ((cred == 0) || (cncp->cred == cred)))
195 /* compare cr_uid instead */
196 coda_nc_stat.Search_len += count;
200 else if (CODA_NAMEMATCH(cncp, name, namelen, dcp)) {
201 printf("coda_nc_find: name %s, new cred = %p, cred = %p\n",
202 name, cred, cncp->cred);
203 printf("nref %d, nuid %d, ngid %d // oref %d, ocred %d, ogid %d\n",
204 cred->cr_ref, cred->cr_uid, cred->cr_gid,
205 cncp->cred->cr_ref, cncp->cred->cr_uid, cncp->cred->cr_gid);
207 print_cred(cncp->cred);
212 return((struct coda_cache *)0);
216 * Enter a new (dir cnode, name) pair into the cache, updating the
217 * LRU and Hash as needed.
220 coda_nc_enter(dcp, name, namelen, cred, cp)
227 struct coda_cache *cncp;
230 if (coda_nc_use == 0) /* Cache is off */
233 CODA_NC_DEBUG(CODA_NC_ENTER,
234 myprintf(("Enter: dcp %p cp %p name %s cred %p \n",
235 dcp, cp, name, cred)); )
237 if (namelen > CODA_NC_NAMELEN) {
238 CODA_NC_DEBUG(CODA_NC_ENTER,
239 myprintf(("long name enter %s\n",name));)
240 coda_nc_stat.long_name_enters++; /* record stats */
244 hash = CODA_NC_HASH(name, namelen, dcp);
245 cncp = coda_nc_find(dcp, name, namelen, cred, hash);
246 if (cncp != (struct coda_cache *) 0) {
247 coda_nc_stat.dbl_enters++; /* duplicate entry */
251 coda_nc_stat.enters++; /* record the enters statistic */
253 /* Grab the next element in the lru chain */
254 cncp = CODA_NC_LRUGET(coda_nc_lru);
256 CODA_NC_LRUREM(cncp); /* remove it from the lists */
258 if (CODA_NC_VALID(cncp)) {
259 /* Seems really ugly, but we have to decrement the appropriate
260 hash bucket length here, so we have to find the hash bucket
262 coda_nc_hash[CODA_NC_HASH(cncp->name, cncp->namelen, cncp->dcp)].length--;
264 coda_nc_stat.lru_rm++; /* zapped a valid entry */
265 CODA_NC_HSHREM(cncp);
266 vrele(CTOV(cncp->dcp));
267 vrele(CTOV(cncp->cp));
272 * Put a hold on the current vnodes and fill in the cache entry.
278 cncp->namelen = namelen;
279 cncp->cred = crhold(cred);
281 bcopy(name, cncp->name, (unsigned)namelen);
283 /* Insert into the lru and hash chains. */
285 CODA_NC_LRUINS(cncp, &coda_nc_lru);
286 CODA_NC_HSHINS(cncp, &coda_nc_hash[hash]);
287 coda_nc_hash[hash].length++; /* Used for tuning */
289 CODA_NC_DEBUG(CODA_NC_PRINTCODA_NC, print_coda_nc(); )
293 * Find the (dir cnode, name) pair in the cache, if it's cred
294 * matches the input, return it, otherwise return 0
297 coda_nc_lookup(dcp, name, namelen, cred)
304 struct coda_cache *cncp;
306 if (coda_nc_use == 0) /* Cache is off */
307 return((struct cnode *) 0);
309 if (namelen > CODA_NC_NAMELEN) {
310 CODA_NC_DEBUG(CODA_NC_LOOKUP,
311 myprintf(("long name lookup %s\n",name));)
312 coda_nc_stat.long_name_lookups++; /* record stats */
313 return((struct cnode *) 0);
316 /* Use the hash function to locate the starting point,
317 then the search routine to go down the list looking for
321 hash = CODA_NC_HASH(name, namelen, dcp);
322 cncp = coda_nc_find(dcp, name, namelen, cred, hash);
323 if (cncp == (struct coda_cache *) 0) {
324 coda_nc_stat.misses++; /* record miss */
325 return((struct cnode *) 0);
330 /* put this entry at the end of the LRU */
331 CODA_NC_LRUREM(cncp);
332 CODA_NC_LRUINS(cncp, &coda_nc_lru);
334 /* move it to the front of the hash chain */
335 /* don't need to change the hash bucket length */
336 CODA_NC_HSHREM(cncp);
337 CODA_NC_HSHINS(cncp, &coda_nc_hash[hash]);
339 CODA_NC_DEBUG(CODA_NC_LOOKUP,
340 printf("lookup: dcp %p, name %s, cred %p = cp %p\n",
341 dcp, name, cred, cncp->cp); )
347 coda_nc_remove(cncp, dcstat)
348 struct coda_cache *cncp;
349 enum dc_status dcstat;
352 * remove an entry -- vrele(cncp->dcp, cp), crfree(cred),
353 * remove it from it's hash chain, and
354 * place it at the head of the lru list.
356 CODA_NC_DEBUG(CODA_NC_REMOVE,
357 myprintf(("coda_nc_remove %s from parent %s\n",
358 cncp->name, coda_f2s(&cncp->dcp->c_fid))); )
359 CODA_NC_HSHREM(cncp);
361 CODA_NC_HSHNUL(cncp); /* have it be a null chain */
362 if ((dcstat == IS_DOWNCALL) && (vrefcnt(CTOV(cncp->dcp)) == 1)) {
363 cncp->dcp->c_flags |= C_PURGING;
365 vrele(CTOV(cncp->dcp));
367 if ((dcstat == IS_DOWNCALL) && (vrefcnt(CTOV(cncp->cp)) == 1)) {
368 cncp->cp->c_flags |= C_PURGING;
370 vrele(CTOV(cncp->cp));
373 bzero(DATA_PART(cncp),DATA_SIZE);
375 /* Put the null entry just after the least-recently-used entry */
376 /* LRU_TOP adjusts the pointer to point to the top of the structure. */
377 CODA_NC_LRUREM(cncp);
378 CODA_NC_LRUINS(cncp, LRU_TOP(coda_nc_lru.lru_prev));
382 * Remove all entries with a parent which has the input fid.
385 coda_nc_zapParentfid(fid, dcstat)
387 enum dc_status dcstat;
389 /* To get to a specific fid, we might either have another hashing
390 function or do a sequential search through the cache for the
391 appropriate entries. The later may be acceptable since I don't
392 think callbacks or whatever Case 1 covers are frequent occurences.
394 struct coda_cache *cncp, *ncncp;
397 if (coda_nc_use == 0) /* Cache is off */
400 CODA_NC_DEBUG(CODA_NC_ZAPPFID,
401 myprintf(("ZapParent: fid %s\n", coda_f2s(fid))); )
403 coda_nc_stat.zapPfids++;
405 for (i = 0; i < coda_nc_hashsize; i++) {
408 * Need to save the hash_next pointer in case we remove the
409 * entry. remove causes hash_next to point to itself.
412 for (cncp = coda_nc_hash[i].hash_next;
413 cncp != (struct coda_cache *)&coda_nc_hash[i];
415 ncncp = cncp->hash_next;
416 if (coda_fid_eq(&(cncp->dcp->c_fid), fid)) {
417 coda_nc_hash[i].length--; /* Used for tuning */
418 coda_nc_remove(cncp, dcstat);
426 * Remove all entries which have the same fid as the input
429 coda_nc_zapfid(fid, dcstat)
431 enum dc_status dcstat;
433 /* See comment for zapParentfid. This routine will be used
434 if attributes are being cached.
436 struct coda_cache *cncp, *ncncp;
439 if (coda_nc_use == 0) /* Cache is off */
442 CODA_NC_DEBUG(CODA_NC_ZAPFID,
443 myprintf(("Zapfid: fid %s\n", coda_f2s(fid))); )
445 coda_nc_stat.zapFids++;
447 for (i = 0; i < coda_nc_hashsize; i++) {
448 for (cncp = coda_nc_hash[i].hash_next;
449 cncp != (struct coda_cache *)&coda_nc_hash[i];
451 ncncp = cncp->hash_next;
452 if (coda_fid_eq(&cncp->cp->c_fid, fid)) {
453 coda_nc_hash[i].length--; /* Used for tuning */
454 coda_nc_remove(cncp, dcstat);
461 * Remove all entries which match the fid and the cred
464 coda_nc_zapvnode(fid, cred, dcstat)
467 enum dc_status dcstat;
469 /* See comment for zapfid. I don't think that one would ever
470 want to zap a file with a specific cred from the kernel.
471 We'll leave this one unimplemented.
474 if (coda_nc_use == 0) /* Cache is off */
477 CODA_NC_DEBUG(CODA_NC_ZAPVNODE,
478 myprintf(("Zapvnode: fid %s cred %p\n",
479 coda_f2s(fid), cred)); )
486 * Remove all entries which have the (dir vnode, name) pair
489 coda_nc_zapfile(dcp, name, namelen)
494 /* use the hash function to locate the file, then zap all
495 entries of it regardless of the cred.
497 struct coda_cache *cncp;
500 if (coda_nc_use == 0) /* Cache is off */
503 CODA_NC_DEBUG(CODA_NC_ZAPFILE,
504 myprintf(("Zapfile: dcp %p name %s \n",
507 if (namelen > CODA_NC_NAMELEN) {
508 coda_nc_stat.long_remove++; /* record stats */
512 coda_nc_stat.zapFile++;
514 hash = CODA_NC_HASH(name, namelen, dcp);
515 cncp = coda_nc_find(dcp, name, namelen, 0, hash);
518 coda_nc_hash[hash].length--; /* Used for tuning */
520 coda_nc_remove(cncp, NOT_DOWNCALL);
521 cncp = coda_nc_find(dcp, name, namelen, 0, hash);
526 * Remove all the entries for a particular user. Used when tokens expire.
527 * A user is determined by his/her effective user id (id_uid).
530 coda_nc_purge_user(uid, dcstat)
532 enum dc_status dcstat;
535 * I think the best approach is to go through the entire cache
536 * via HASH or whatever and zap all entries which match the
537 * input cred. Or just flush the whole cache. It might be
538 * best to go through on basis of LRU since cache will almost
539 * always be full and LRU is more straightforward.
542 struct coda_cache *cncp, *ncncp;
545 if (coda_nc_use == 0) /* Cache is off */
548 CODA_NC_DEBUG(CODA_NC_PURGEUSER,
549 myprintf(("ZapDude: uid %x\n", uid)); )
550 coda_nc_stat.zapUsers++;
552 for (cncp = CODA_NC_LRUGET(coda_nc_lru);
553 cncp != (struct coda_cache *)(&coda_nc_lru);
555 ncncp = CODA_NC_LRUGET(*cncp);
557 if ((CODA_NC_VALID(cncp)) &&
558 ((cncp->cred)->cr_uid == uid)) {
559 /* Seems really ugly, but we have to decrement the appropriate
560 hash bucket length here, so we have to find the hash bucket
562 hash = CODA_NC_HASH(cncp->name, cncp->namelen, cncp->dcp);
563 coda_nc_hash[hash].length--; /* For performance tuning */
565 coda_nc_remove(cncp, dcstat);
571 * Flush the entire name cache. In response to a flush of the Venus cache.
574 coda_nc_flush(dcstat)
575 enum dc_status dcstat;
577 /* One option is to deallocate the current name cache and
578 call init to start again. Or just deallocate, then rebuild.
579 Or again, we could just go through the array and zero the
584 * Go through the whole lru chain and kill everything as we go.
585 * I don't use remove since that would rebuild the lru chain
586 * as it went and that seemed unneccesary.
588 struct coda_cache *cncp;
591 if (coda_nc_use == 0) /* Cache is off */
594 coda_nc_stat.Flushes++;
596 for (cncp = CODA_NC_LRUGET(coda_nc_lru);
597 cncp != (struct coda_cache *)&coda_nc_lru;
598 cncp = CODA_NC_LRUGET(*cncp)) {
599 if (CODA_NC_VALID(cncp)) {
601 CODA_NC_HSHREM(cncp); /* only zero valid nodes */
602 CODA_NC_HSHNUL(cncp);
603 if ((dcstat == IS_DOWNCALL)
604 && (vrefcnt(CTOV(cncp->dcp)) == 1))
606 cncp->dcp->c_flags |= C_PURGING;
608 vrele(CTOV(cncp->dcp));
610 ASSERT_VOP_LOCKED(CTOV(cncp->cp), "coda_nc_flush");
611 if (CTOV(cncp->cp)->v_vflag & VV_TEXT) {
612 if (coda_vmflush(cncp->cp))
613 CODADEBUG(CODA_FLUSH,
614 myprintf(("coda_nc_flush: %s busy\n",
615 coda_f2s(&cncp->cp->c_fid))); )
618 if ((dcstat == IS_DOWNCALL)
619 && (vrefcnt(CTOV(cncp->cp)) == 1))
621 cncp->cp->c_flags |= C_PURGING;
623 vrele(CTOV(cncp->cp));
626 bzero(DATA_PART(cncp),DATA_SIZE);
630 for (i = 0; i < coda_nc_hashsize; i++)
631 coda_nc_hash[i].length = 0;
639 * This routine should print out all the hash chains to the console.
645 struct coda_cache *cncp;
647 for (hash = 0; hash < coda_nc_hashsize; hash++) {
648 myprintf(("\nhash %d\n",hash));
650 for (cncp = coda_nc_hash[hash].hash_next;
651 cncp != (struct coda_cache *)&coda_nc_hash[hash];
652 cncp = cncp->hash_next) {
653 myprintf(("cp %p dcp %p cred %p name %s\n",
655 cncp->cred, cncp->name));
661 coda_nc_gather_stats(void)
663 int i, max = 0, sum = 0, temp, zeros = 0, ave, n;
665 for (i = 0; i < coda_nc_hashsize; i++) {
666 if (coda_nc_hash[i].length) {
667 sum += coda_nc_hash[i].length;
672 if (coda_nc_hash[i].length > max)
673 max = coda_nc_hash[i].length;
677 * When computing the Arithmetic mean, only count slots which
678 * are not empty in the distribution.
680 coda_nc_stat.Sum_bucket_len = sum;
681 coda_nc_stat.Num_zero_len = zeros;
682 coda_nc_stat.Max_bucket_len = max;
684 if ((n = coda_nc_hashsize - zeros) > 0)
690 for (i = 0; i < coda_nc_hashsize; i++) {
691 if (coda_nc_hash[i].length) {
692 temp = coda_nc_hash[i].length - ave;
696 coda_nc_stat.Sum2_bucket_len = sum;
700 * The purpose of this routine is to allow the hash and cache sizes to be
701 * changed dynamically. This should only be used in controlled environments,
702 * it makes no effort to lock other users from accessing the cache while it
703 * is in an improper state (except by turning the cache off).
706 coda_nc_resize(hashsize, heapsize, dcstat)
707 int hashsize, heapsize;
708 enum dc_status dcstat;
710 if ((hashsize % 2) || (heapsize % 2)) { /* Illegal hash or cache sizes */
714 coda_nc_use = 0; /* Turn the cache off */
716 coda_nc_flush(dcstat); /* free any cnodes in the cache */
718 /* WARNING: free must happen *before* size is reset */
719 CODA_FREE(coda_nc_heap,TOTAL_CACHE_SIZE);
720 CODA_FREE(coda_nc_hash,TOTAL_HASH_SIZE);
722 coda_nc_hashsize = hashsize;
723 coda_nc_size = heapsize;
725 coda_nc_init(); /* Set up a cache with the new size */
727 coda_nc_use = 1; /* Turn the cache back on */
732 char coda_nc_name_buf[CODA_MAXNAMLEN+1];
735 coda_nc_name(struct cnode *cp)
737 struct coda_cache *cncp, *ncncp;
740 if (coda_nc_use == 0) /* Cache is off */
743 for (i = 0; i < coda_nc_hashsize; i++) {
744 for (cncp = coda_nc_hash[i].hash_next;
745 cncp != (struct coda_cache *)&coda_nc_hash[i];
747 ncncp = cncp->hash_next;
748 if (cncp->cp == cp) {
749 bcopy(cncp->name, coda_nc_name_buf, cncp->namelen);
750 coda_nc_name_buf[cncp->namelen] = 0;
751 printf(" is %s (%p,%p)@%p",
752 coda_nc_name_buf, cncp->cp, cncp->dcp, cncp);