Home | History | Annotate | Line # | Download | only in coda
coda_namecache.c revision 1.13
      1 /*	$NetBSD: coda_namecache.c,v 1.13 2003/08/27 17:49:49 drochner 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.13 2003/08/27 17:49:49 drochner Exp $");
     81 
     82 #include <sys/param.h>
     83 #include <sys/errno.h>
     84 #include <sys/malloc.h>
     85 #include <sys/select.h>
     86 
     87 #include <coda/coda.h>
     88 #include <coda/cnode.h>
     89 #include <coda/coda_namecache.h>
     90 
     91 #ifdef	DEBUG
     92 #include <coda/coda_vnops.h>
     93 #endif
     94 
     95 #ifndef insque
     96 #include <sys/systm.h>
     97 #endif /* insque */
     98 
     99 /*
    100  * Declaration of the name cache data structure.
    101  */
    102 
    103 int 	coda_nc_use = 1;			 /* Indicate use of CODA Name Cache */
    104 
    105 int	coda_nc_size = CODA_NC_CACHESIZE;	 /* size of the cache */
    106 int	coda_nc_hashsize = CODA_NC_HASHSIZE; /* size of the primary hash */
    107 
    108 struct 	coda_cache *coda_nc_heap;	/* pointer to the cache entries */
    109 struct	coda_hash  *coda_nc_hash;	/* hash table of cfscache pointers */
    110 struct	coda_lru   coda_nc_lru;		/* head of lru chain */
    111 
    112 struct coda_nc_statistics coda_nc_stat;	/* Keep various stats */
    113 
    114 /*
    115  * for testing purposes
    116  */
    117 int coda_nc_debug = 0;
    118 
    119 /*
    120  * Entry points for the CODA Name Cache
    121  */
    122 static struct coda_cache *
    123 coda_nc_find(struct cnode *dcp, const char *name, int namelen,
    124 	struct ucred *cred, int hash);
    125 static void
    126 coda_nc_remove(struct coda_cache *cncp, enum dc_status dcstat);
    127 
    128 /*
    129  * Initialize the cache, the LRU structure and the Hash structure(s)
    130  */
    131 
    132 #define TOTAL_CACHE_SIZE 	(sizeof(struct coda_cache) * coda_nc_size)
    133 #define TOTAL_HASH_SIZE 	(sizeof(struct coda_hash)  * coda_nc_hashsize)
    134 
    135 int coda_nc_initialized = 0;      /* Initially the cache has not been initialized */
    136 
    137 void
    138 coda_nc_init(void)
    139 {
    140     int i;
    141 
    142     /* zero the statistics structure */
    143 
    144     memset(&coda_nc_stat, 0, (sizeof(struct coda_nc_statistics)));
    145 
    146 #ifdef	CODA_VERBOSE
    147     printf("CODA NAME CACHE: CACHE %d, HASH TBL %d\n", CODA_NC_CACHESIZE, CODA_NC_HASHSIZE);
    148 #endif
    149     CODA_ALLOC(coda_nc_heap, struct coda_cache *, TOTAL_CACHE_SIZE);
    150     CODA_ALLOC(coda_nc_hash, struct coda_hash *, TOTAL_HASH_SIZE);
    151 
    152     coda_nc_lru.lru_next =
    153 	coda_nc_lru.lru_prev = (struct coda_cache *)LRU_PART(&coda_nc_lru);
    154 
    155 
    156     for (i=0; i < coda_nc_size; i++) {	/* initialize the heap */
    157 	CODA_NC_LRUINS(&coda_nc_heap[i], &coda_nc_lru);
    158 	CODA_NC_HSHNUL(&coda_nc_heap[i]);
    159 	coda_nc_heap[i].cp = coda_nc_heap[i].dcp = (struct cnode *)0;
    160     }
    161 
    162     for (i=0; i < coda_nc_hashsize; i++) {	/* initialize the hashtable */
    163 	CODA_NC_HSHNUL((struct coda_cache *)&coda_nc_hash[i]);
    164     }
    165 
    166     coda_nc_initialized++;
    167 }
    168 
    169 /*
    170  * Auxillary routines -- shouldn't be entry points
    171  */
    172 
    173 static struct coda_cache *
    174 coda_nc_find(dcp, name, namelen, cred, hash)
    175 	struct cnode *dcp;
    176 	const char *name;
    177 	int namelen;
    178 	struct ucred *cred;
    179 	int hash;
    180 {
    181 	/*
    182 	 * hash to find the appropriate bucket, look through the chain
    183 	 * for the right entry (especially right cred, unless cred == 0)
    184 	 */
    185 	struct coda_cache *cncp;
    186 	int count = 1;
    187 
    188 	CODA_NC_DEBUG(CODA_NC_FIND,
    189 		myprintf(("coda_nc_find(dcp %p, name %s, len %d, cred %p, hash %d\n",
    190 			dcp, name, namelen, cred, hash));)
    191 
    192 	for (cncp = coda_nc_hash[hash].hash_next;
    193 	     cncp != (struct coda_cache *)&coda_nc_hash[hash];
    194 	     cncp = cncp->hash_next, count++)
    195 	{
    196 
    197 	    if ((CODA_NAMEMATCH(cncp, name, namelen, dcp)) &&
    198 		((cred == 0) || (cncp->cred == cred)))
    199 	    {
    200 		/* compare cr_uid instead */
    201 		coda_nc_stat.Search_len += count;
    202 		return(cncp);
    203 	    }
    204 #ifdef	DEBUG
    205 	    else if (CODA_NAMEMATCH(cncp, name, namelen, dcp)) {
    206 	    	printf("coda_nc_find: name %s, new cred = %p, cred = %p\n",
    207 			name, cred, cncp->cred);
    208 		printf("nref %d, nuid %d, ngid %d // oref %d, ocred %d, ogid %d\n",
    209 			cred->cr_ref, cred->cr_uid, cred->cr_gid,
    210 			cncp->cred->cr_ref, cncp->cred->cr_uid, cncp->cred->cr_gid);
    211 		print_cred(cred);
    212 		print_cred(cncp->cred);
    213 	    }
    214 #endif
    215 	}
    216 
    217 	return((struct coda_cache *)0);
    218 }
    219 
    220 /*
    221  * Enter a new (dir cnode, name) pair into the cache, updating the
    222  * LRU and Hash as needed.
    223  */
    224 void
    225 coda_nc_enter(dcp, name, namelen, cred, cp)
    226     struct cnode *dcp;
    227     const char *name;
    228     int namelen;
    229     struct ucred *cred;
    230     struct cnode *cp;
    231 {
    232     struct coda_cache *cncp;
    233     int hash;
    234 
    235     if (coda_nc_use == 0)			/* Cache is off */
    236 	return;
    237 
    238     CODA_NC_DEBUG(CODA_NC_ENTER,
    239 		myprintf(("Enter: dcp %p cp %p name %s cred %p \n",
    240 		       dcp, cp, name, cred)); )
    241 
    242     if (namelen > CODA_NC_NAMELEN) {
    243 	CODA_NC_DEBUG(CODA_NC_ENTER,
    244 		    myprintf(("long name enter %s\n",name));)
    245 	    coda_nc_stat.long_name_enters++;	/* record stats */
    246 	return;
    247     }
    248 
    249     hash = CODA_NC_HASH(name, namelen, dcp);
    250     cncp = coda_nc_find(dcp, name, namelen, cred, hash);
    251     if (cncp != (struct coda_cache *) 0) {
    252 	coda_nc_stat.dbl_enters++;		/* duplicate entry */
    253 	return;
    254     }
    255 
    256     coda_nc_stat.enters++;		/* record the enters statistic */
    257 
    258     /* Grab the next element in the lru chain */
    259     cncp = CODA_NC_LRUGET(coda_nc_lru);
    260 
    261     CODA_NC_LRUREM(cncp);	/* remove it from the lists */
    262 
    263     if (CODA_NC_VALID(cncp)) {
    264 	/* Seems really ugly, but we have to decrement the appropriate
    265 	   hash bucket length here, so we have to find the hash bucket
    266 	   */
    267 	coda_nc_hash[CODA_NC_HASH(cncp->name, cncp->namelen, cncp->dcp)].length--;
    268 
    269 	coda_nc_stat.lru_rm++;	/* zapped a valid entry */
    270 	CODA_NC_HSHREM(cncp);
    271 	vrele(CTOV(cncp->dcp));
    272 	vrele(CTOV(cncp->cp));
    273 	crfree(cncp->cred);
    274     }
    275 
    276     /*
    277      * Put a hold on the current vnodes and fill in the cache entry.
    278      */
    279     vref(CTOV(cp));
    280     vref(CTOV(dcp));
    281     crhold(cred);
    282     cncp->dcp = dcp;
    283     cncp->cp = cp;
    284     cncp->namelen = namelen;
    285     cncp->cred = cred;
    286 
    287     bcopy(name, cncp->name, (unsigned)namelen);
    288 
    289     /* Insert into the lru and hash chains. */
    290 
    291     CODA_NC_LRUINS(cncp, &coda_nc_lru);
    292     CODA_NC_HSHINS(cncp, &coda_nc_hash[hash]);
    293     coda_nc_hash[hash].length++;                      /* Used for tuning */
    294 
    295     CODA_NC_DEBUG(CODA_NC_PRINTCODA_NC, print_coda_nc(); )
    296 }
    297 
    298 /*
    299  * Find the (dir cnode, name) pair in the cache, if it's cred
    300  * matches the input, return it, otherwise return 0
    301  */
    302 struct cnode *
    303 coda_nc_lookup(dcp, name, namelen, cred)
    304 	struct cnode *dcp;
    305 	const char *name;
    306 	int namelen;
    307 	struct ucred *cred;
    308 {
    309 	int hash;
    310 	struct coda_cache *cncp;
    311 
    312 	if (coda_nc_use == 0)			/* Cache is off */
    313 		return((struct cnode *) 0);
    314 
    315 	if (namelen > CODA_NC_NAMELEN) {
    316 	        CODA_NC_DEBUG(CODA_NC_LOOKUP,
    317 			    myprintf(("long name lookup %s\n",name));)
    318 		coda_nc_stat.long_name_lookups++;		/* record stats */
    319 		return((struct cnode *) 0);
    320 	}
    321 
    322 	/* Use the hash function to locate the starting point,
    323 	   then the search routine to go down the list looking for
    324 	   the correct cred.
    325  	 */
    326 
    327 	hash = CODA_NC_HASH(name, namelen, dcp);
    328 	cncp = coda_nc_find(dcp, name, namelen, cred, hash);
    329 	if (cncp == (struct coda_cache *) 0) {
    330 		coda_nc_stat.misses++;			/* record miss */
    331 		return((struct cnode *) 0);
    332 	}
    333 
    334 	coda_nc_stat.hits++;
    335 
    336 	/* put this entry at the end of the LRU */
    337 	CODA_NC_LRUREM(cncp);
    338 	CODA_NC_LRUINS(cncp, &coda_nc_lru);
    339 
    340 	/* move it to the front of the hash chain */
    341 	/* don't need to change the hash bucket length */
    342 	CODA_NC_HSHREM(cncp);
    343 	CODA_NC_HSHINS(cncp, &coda_nc_hash[hash]);
    344 
    345 	CODA_NC_DEBUG(CODA_NC_LOOKUP,
    346 		printf("lookup: dcp %p, name %s, cred %p = cp %p\n",
    347 			dcp, name, cred, cncp->cp); )
    348 
    349 	return(cncp->cp);
    350 }
    351 
    352 static void
    353 coda_nc_remove(cncp, dcstat)
    354 	struct coda_cache *cncp;
    355 	enum dc_status dcstat;
    356 {
    357 	/*
    358 	 * remove an entry -- vrele(cncp->dcp, cp), crfree(cred),
    359 	 * remove it from it's hash chain, and
    360 	 * place it at the head of the lru list.
    361 	 */
    362         CODA_NC_DEBUG(CODA_NC_REMOVE,
    363 		    myprintf(("coda_nc_remove %s from parent %s\n",
    364 			      cncp->name, coda_f2s(&cncp->dcp->c_fid))); )
    365 
    366 
    367   	CODA_NC_HSHREM(cncp);
    368 
    369 	CODA_NC_HSHNUL(cncp);		/* have it be a null chain */
    370 	if ((dcstat == IS_DOWNCALL) && (CTOV(cncp->dcp)->v_usecount == 1)) {
    371 		cncp->dcp->c_flags |= C_PURGING;
    372 	}
    373 	vrele(CTOV(cncp->dcp));
    374 
    375 	if ((dcstat == IS_DOWNCALL) && (CTOV(cncp->cp)->v_usecount == 1)) {
    376 		cncp->cp->c_flags |= C_PURGING;
    377 	}
    378 	vrele(CTOV(cncp->cp));
    379 
    380 	crfree(cncp->cred);
    381 	memset(DATA_PART(cncp), 0, DATA_SIZE);
    382 
    383 	/* Put the null entry just after the least-recently-used entry */
    384 	/* LRU_TOP adjusts the pointer to point to the top of the structure. */
    385 	CODA_NC_LRUREM(cncp);
    386 	CODA_NC_LRUINS(cncp, LRU_TOP(coda_nc_lru.lru_prev));
    387 }
    388 
    389 /*
    390  * Remove all entries with a parent which has the input fid.
    391  */
    392 void
    393 coda_nc_zapParentfid(fid, dcstat)
    394 	CodaFid *fid;
    395 	enum dc_status dcstat;
    396 {
    397 	/* To get to a specific fid, we might either have another hashing
    398 	   function or do a sequential search through the cache for the
    399 	   appropriate entries. The later may be acceptable since I don't
    400 	   think callbacks or whatever Case 1 covers are frequent occurrences.
    401 	 */
    402 	struct coda_cache *cncp, *ncncp;
    403 	int i;
    404 
    405 	if (coda_nc_use == 0)			/* Cache is off */
    406 		return;
    407 
    408 	CODA_NC_DEBUG(CODA_NC_ZAPPFID,
    409 		myprintf(("ZapParent: fid %s\n", coda_f2s(fid))); )
    410 
    411 	coda_nc_stat.zapPfids++;
    412 
    413 	for (i = 0; i < coda_nc_hashsize; i++) {
    414 
    415 		/*
    416 		 * Need to save the hash_next pointer in case we remove the
    417 		 * entry. remove causes hash_next to point to itself.
    418 		 */
    419 
    420 		for (cncp = coda_nc_hash[i].hash_next;
    421 		     cncp != (struct coda_cache *)&coda_nc_hash[i];
    422 		     cncp = ncncp) {
    423 			ncncp = cncp->hash_next;
    424 			if (coda_fid_eq(&(cncp->dcp->c_fid), fid)) {
    425 			        coda_nc_hash[i].length--;      /* Used for tuning */
    426 				coda_nc_remove(cncp, dcstat);
    427 			}
    428 		}
    429 	}
    430 }
    431 
    432 /*
    433  * Remove all entries which have the same fid as the input
    434  */
    435 void
    436 coda_nc_zapfid(fid, dcstat)
    437 	CodaFid *fid;
    438 	enum dc_status dcstat;
    439 {
    440 	/* See comment for zapParentfid. This routine will be used
    441 	   if attributes are being cached.
    442 	 */
    443 	struct coda_cache *cncp, *ncncp;
    444 	int i;
    445 
    446 	if (coda_nc_use == 0)			/* Cache is off */
    447 		return;
    448 
    449 	CODA_NC_DEBUG(CODA_NC_ZAPFID,
    450 		myprintf(("Zapfid: fid %s\n", coda_f2s(fid))); )
    451 
    452 	coda_nc_stat.zapFids++;
    453 
    454 	for (i = 0; i < coda_nc_hashsize; i++) {
    455 		for (cncp = coda_nc_hash[i].hash_next;
    456 		     cncp != (struct coda_cache *)&coda_nc_hash[i];
    457 		     cncp = ncncp) {
    458 			ncncp = cncp->hash_next;
    459 			if (coda_fid_eq(&cncp->cp->c_fid, fid)) {
    460 			        coda_nc_hash[i].length--;     /* Used for tuning */
    461 				coda_nc_remove(cncp, dcstat);
    462 			}
    463 		}
    464 	}
    465 }
    466 
    467 /*
    468  * Remove all entries which match the fid and the cred
    469  */
    470 void
    471 coda_nc_zapvnode(fid, cred, dcstat)
    472 	CodaFid *fid;
    473 	struct ucred *cred;
    474 	enum dc_status dcstat;
    475 {
    476 	/* See comment for zapfid. I don't think that one would ever
    477 	   want to zap a file with a specific cred from the kernel.
    478 	   We'll leave this one unimplemented.
    479 	 */
    480 	if (coda_nc_use == 0)			/* Cache is off */
    481 		return;
    482 
    483 	CODA_NC_DEBUG(CODA_NC_ZAPVNODE,
    484 		myprintf(("Zapvnode: fid %s cred %p\n",
    485 			  coda_f2s(fid), cred)); )
    486 }
    487 
    488 /*
    489  * Remove all entries which have the (dir vnode, name) pair
    490  */
    491 void
    492 coda_nc_zapfile(dcp, name, namelen)
    493 	struct cnode *dcp;
    494 	const char *name;
    495 	int namelen;
    496 {
    497 	/* use the hash function to locate the file, then zap all
    498  	   entries of it regardless of the cred.
    499 	 */
    500 	struct coda_cache *cncp;
    501 	int hash;
    502 
    503 	if (coda_nc_use == 0)			/* Cache is off */
    504 		return;
    505 
    506 	CODA_NC_DEBUG(CODA_NC_ZAPFILE,
    507 		myprintf(("Zapfile: dcp %p name %s \n",
    508 			  dcp, name)); )
    509 
    510 	if (namelen > CODA_NC_NAMELEN) {
    511 		coda_nc_stat.long_remove++;		/* record stats */
    512 		return;
    513 	}
    514 
    515 	coda_nc_stat.zapFile++;
    516 
    517 	hash = CODA_NC_HASH(name, namelen, dcp);
    518 	cncp = coda_nc_find(dcp, name, namelen, 0, hash);
    519 
    520 	while (cncp) {
    521 	  coda_nc_hash[hash].length--;                 /* Used for tuning */
    522 /* 1.3 */
    523 	  coda_nc_remove(cncp, NOT_DOWNCALL);
    524 	  cncp = coda_nc_find(dcp, name, namelen, 0, hash);
    525 	}
    526 }
    527 
    528 /*
    529  * Remove all the entries for a particular user. Used when tokens expire.
    530  * A user is determined by his/her effective user id (id_uid).
    531  */
    532 void
    533 coda_nc_purge_user(uid, dcstat)
    534 	uid_t	uid;
    535 	enum dc_status  dcstat;
    536 {
    537 	/*
    538 	 * I think the best approach is to go through the entire cache
    539 	 * via HASH or whatever and zap all entries which match the
    540 	 * input cred. Or just flush the whole cache.  It might be
    541 	 * best to go through on basis of LRU since cache will almost
    542 	 * always be full and LRU is more straightforward.
    543 	 */
    544 
    545 	struct coda_cache *cncp, *ncncp;
    546 	int hash;
    547 
    548 	if (coda_nc_use == 0)			/* Cache is off */
    549 		return;
    550 
    551 	CODA_NC_DEBUG(CODA_NC_PURGEUSER,
    552 		myprintf(("ZapDude: uid %x\n", uid)); )
    553 	coda_nc_stat.zapUsers++;
    554 
    555 	for (cncp = CODA_NC_LRUGET(coda_nc_lru);
    556 	     cncp != (struct coda_cache *)(&coda_nc_lru);
    557 	     cncp = ncncp) {
    558 		ncncp = CODA_NC_LRUGET(*cncp);
    559 
    560 		if ((CODA_NC_VALID(cncp)) &&
    561 		   ((cncp->cred)->cr_uid == uid)) {
    562 		        /* Seems really ugly, but we have to decrement the appropriate
    563 			   hash bucket length here, so we have to find the hash bucket
    564 			   */
    565 		        hash = CODA_NC_HASH(cncp->name, cncp->namelen, cncp->dcp);
    566 			coda_nc_hash[hash].length--;     /* For performance tuning */
    567 
    568 			coda_nc_remove(cncp, dcstat);
    569 		}
    570 	}
    571 }
    572 
    573 /*
    574  * Flush the entire name cache. In response to a flush of the Venus cache.
    575  */
    576 void
    577 coda_nc_flush(dcstat)
    578 	enum dc_status dcstat;
    579 {
    580 	/* One option is to deallocate the current name cache and
    581 	   call init to start again. Or just deallocate, then rebuild.
    582 	   Or again, we could just go through the array and zero the
    583 	   appropriate fields.
    584 	 */
    585 
    586 	/*
    587 	 * Go through the whole lru chain and kill everything as we go.
    588 	 * I don't use remove since that would rebuild the lru chain
    589 	 * as it went and that seemed unneccesary.
    590 	 */
    591 	struct coda_cache *cncp;
    592 	int i;
    593 
    594 	if (coda_nc_use == 0)			/* Cache is off */
    595 		return;
    596 
    597 	coda_nc_stat.Flushes++;
    598 
    599 	for (cncp = CODA_NC_LRUGET(coda_nc_lru);
    600 	     cncp != (struct coda_cache *)&coda_nc_lru;
    601 	     cncp = CODA_NC_LRUGET(*cncp)) {
    602 		if (CODA_NC_VALID(cncp)) {
    603 
    604 			CODA_NC_HSHREM(cncp);	/* only zero valid nodes */
    605 			CODA_NC_HSHNUL(cncp);
    606 			if ((dcstat == IS_DOWNCALL)
    607 			    && (CTOV(cncp->dcp)->v_usecount == 1))
    608 			{
    609 				cncp->dcp->c_flags |= C_PURGING;
    610 			}
    611 			vrele(CTOV(cncp->dcp));
    612 
    613 			if (CTOV(cncp->cp)->v_flag & VTEXT) {
    614 			    if (coda_vmflush(cncp->cp))
    615 				CODADEBUG(CODA_FLUSH,
    616 					myprintf(("coda_nc_flush: %s busy\n",
    617 						coda_f2s(&cncp->cp->c_fid))); )
    618 			}
    619 
    620 			if ((dcstat == IS_DOWNCALL)
    621 			    && (CTOV(cncp->cp)->v_usecount == 1))
    622 			{
    623 				cncp->cp->c_flags |= C_PURGING;
    624 			}
    625 			vrele(CTOV(cncp->cp));
    626 
    627 			crfree(cncp->cred);
    628 			memset(DATA_PART(cncp), 0, DATA_SIZE);
    629 		}
    630 	}
    631 
    632 	for (i = 0; i < coda_nc_hashsize; i++)
    633 	  coda_nc_hash[i].length = 0;
    634 }
    635 
    636 /*
    637  * Debugging routines
    638  */
    639 
    640 /*
    641  * This routine should print out all the hash chains to the console.
    642  */
    643 void
    644 print_coda_nc(void)
    645 {
    646 	int hash;
    647 	struct coda_cache *cncp;
    648 
    649 	for (hash = 0; hash < coda_nc_hashsize; hash++) {
    650 		myprintf(("\nhash %d\n",hash));
    651 
    652 		for (cncp = coda_nc_hash[hash].hash_next;
    653 		     cncp != (struct coda_cache *)&coda_nc_hash[hash];
    654 		     cncp = cncp->hash_next) {
    655 			myprintf(("cp %p dcp %p cred %p name %s\n",
    656 				  cncp->cp, cncp->dcp,
    657 				  cncp->cred, cncp->name));
    658 		     }
    659 	}
    660 }
    661 
    662 void
    663 coda_nc_gather_stats(void)
    664 {
    665     int i, max = 0, sum = 0, temp, zeros = 0, ave, n;
    666 
    667 	for (i = 0; i < coda_nc_hashsize; i++) {
    668 	  if (coda_nc_hash[i].length) {
    669 	    sum += coda_nc_hash[i].length;
    670 	  } else {
    671 	    zeros++;
    672 	  }
    673 
    674 	  if (coda_nc_hash[i].length > max)
    675 	    max = coda_nc_hash[i].length;
    676 	}
    677 
    678 	/*
    679 	 * When computing the Arithmetic mean, only count slots which
    680 	 * are not empty in the distribution.
    681 	 */
    682         coda_nc_stat.Sum_bucket_len = sum;
    683         coda_nc_stat.Num_zero_len = zeros;
    684         coda_nc_stat.Max_bucket_len = max;
    685 
    686 	if ((n = coda_nc_hashsize - zeros) > 0)
    687 	  ave = sum / n;
    688 	else
    689 	  ave = 0;
    690 
    691 	sum = 0;
    692 	for (i = 0; i < coda_nc_hashsize; i++) {
    693 	  if (coda_nc_hash[i].length) {
    694 	    temp = coda_nc_hash[i].length - ave;
    695 	    sum += temp * temp;
    696 	  }
    697 	}
    698         coda_nc_stat.Sum2_bucket_len = sum;
    699 }
    700 
    701 /*
    702  * The purpose of this routine is to allow the hash and cache sizes to be
    703  * changed dynamically. This should only be used in controlled environments,
    704  * it makes no effort to lock other users from accessing the cache while it
    705  * is in an improper state (except by turning the cache off).
    706  */
    707 int
    708 coda_nc_resize(hashsize, heapsize, dcstat)
    709      int hashsize, heapsize;
    710      enum dc_status dcstat;
    711 {
    712     if ((hashsize % 2) || (heapsize % 2)) { /* Illegal hash or cache sizes */
    713 	return(EINVAL);
    714     }
    715 
    716     coda_nc_use = 0;                       /* Turn the cache off */
    717 
    718     coda_nc_flush(dcstat);                 /* free any cnodes in the cache */
    719 
    720     /* WARNING: free must happen *before* size is reset */
    721     CODA_FREE(coda_nc_heap,TOTAL_CACHE_SIZE);
    722     CODA_FREE(coda_nc_hash,TOTAL_HASH_SIZE);
    723 
    724     coda_nc_hashsize = hashsize;
    725     coda_nc_size = heapsize;
    726 
    727     coda_nc_init();                        /* Set up a cache with the new size */
    728 
    729     coda_nc_use = 1;                       /* Turn the cache back on */
    730     return(0);
    731 }
    732 
    733 char coda_nc_name_buf[CODA_MAXNAMLEN+1];
    734 
    735 void
    736 coda_nc_name(struct cnode *cp)
    737 {
    738 	struct coda_cache *cncp, *ncncp;
    739 	int i;
    740 
    741 	if (coda_nc_use == 0)			/* Cache is off */
    742 		return;
    743 
    744 	for (i = 0; i < coda_nc_hashsize; i++) {
    745 		for (cncp = coda_nc_hash[i].hash_next;
    746 		     cncp != (struct coda_cache *)&coda_nc_hash[i];
    747 		     cncp = ncncp) {
    748 			ncncp = cncp->hash_next;
    749 			if (cncp->cp == cp) {
    750 				bcopy(cncp->name, coda_nc_name_buf, cncp->namelen);
    751 				coda_nc_name_buf[cncp->namelen] = 0;
    752 				printf(" is %s (%p,%p)@%p",
    753 					coda_nc_name_buf, cncp->cp, cncp->dcp, cncp);
    754 			}
    755 
    756 		}
    757 	}
    758 }
    759