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