Home | History | Annotate | Line # | Download | only in kern
vfs_cache.c revision 1.19
      1 /*	$NetBSD: vfs_cache.c,v 1.19 1999/03/22 17:01:55 sommerfe Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	@(#)vfs_cache.c	8.3 (Berkeley) 8/22/94
     36  */
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/time.h>
     41 #include <sys/mount.h>
     42 #include <sys/vnode.h>
     43 #include <sys/namei.h>
     44 #include <sys/errno.h>
     45 #include <sys/malloc.h>
     46 #include <sys/pool.h>
     47 
     48 /*
     49  * Name caching works as follows:
     50  *
     51  * Names found by directory scans are retained in a cache
     52  * for future reference.  It is managed LRU, so frequently
     53  * used names will hang around.  Cache is indexed by hash value
     54  * obtained from (vp, name) where vp refers to the directory
     55  * containing name.
     56  *
     57  * For simplicity (and economy of storage), names longer than
     58  * a maximum length of NCHNAMLEN are not cached; they occur
     59  * infrequently in any case, and are almost never of interest.
     60  *
     61  * Upon reaching the last segment of a path, if the reference
     62  * is for DELETE, or NOCACHE is set (rewrite), and the
     63  * name is located in the cache, it will be dropped.
     64  */
     65 
     66 /*
     67  * Structures associated with name cacheing.
     68  */
     69 LIST_HEAD(nchashhead, namecache) *nchashtbl;
     70 u_long	nchash;				/* size of hash table - 1 */
     71 long	numcache;			/* number of cache entries allocated */
     72 
     73 LIST_HEAD(ncvhashhead, namecache) *ncvhashtbl;
     74 u_long	ncvhash;			/* size of hash table - 1 */
     75 
     76 TAILQ_HEAD(, namecache) nclruhead;		/* LRU chain */
     77 struct	nchstats nchstats;		/* cache effectiveness statistics */
     78 
     79 struct pool namecache_pool;
     80 
     81 int doingcache = 1;			/* 1 => enable the cache */
     82 
     83 /*
     84  * Look for a the name in the cache. We don't do this
     85  * if the segment name is long, simply so the cache can avoid
     86  * holding long names (which would either waste space, or
     87  * add greatly to the complexity).
     88  *
     89  * Lookup is called with ni_dvp pointing to the directory to search,
     90  * ni_ptr pointing to the name of the entry being sought, ni_namelen
     91  * tells the length of the name, and ni_hash contains a hash of
     92  * the name. If the lookup succeeds, the vnode is returned in ni_vp
     93  * and a status of -1 is returned. If the lookup determines that
     94  * the name does not exist (negative cacheing), a status of ENOENT
     95  * is returned. If the lookup fails, a status of zero is returned.
     96  */
     97 int
     98 cache_lookup(dvp, vpp, cnp)
     99 	struct vnode *dvp;
    100 	struct vnode **vpp;
    101 	struct componentname *cnp;
    102 {
    103 	register struct namecache *ncp;
    104 	register struct nchashhead *ncpp;
    105 
    106 	if (!doingcache) {
    107 		cnp->cn_flags &= ~MAKEENTRY;
    108 		return (0);
    109 	}
    110 	if (cnp->cn_namelen > NCHNAMLEN) {
    111 		nchstats.ncs_long++;
    112 		cnp->cn_flags &= ~MAKEENTRY;
    113 		return (0);
    114 	}
    115 	ncpp = &nchashtbl[(cnp->cn_hash ^ dvp->v_id) & nchash];
    116 	for (ncp = ncpp->lh_first; ncp != 0; ncp = ncp->nc_hash.le_next) {
    117 		if (ncp->nc_dvp == dvp &&
    118 		    ncp->nc_dvpid == dvp->v_id &&
    119 		    ncp->nc_nlen == cnp->cn_namelen &&
    120 		    !memcmp(ncp->nc_name, cnp->cn_nameptr, (u_int)ncp->nc_nlen))
    121 			break;
    122 	}
    123 	if (ncp == 0) {
    124 		nchstats.ncs_miss++;
    125 		return (0);
    126 	}
    127 	if ((cnp->cn_flags & MAKEENTRY) == 0) {
    128 		nchstats.ncs_badhits++;
    129 	} else if (ncp->nc_vp == NULL) {
    130 		/*
    131 		 * Restore the ISWHITEOUT flag saved earlier.
    132 		 */
    133 		cnp->cn_flags |= ncp->nc_vpid;
    134 		if (cnp->cn_nameiop != CREATE) {
    135 			nchstats.ncs_neghits++;
    136 			/*
    137 			 * Move this slot to end of LRU chain,
    138 			 * if not already there.
    139 			 */
    140 			if (ncp->nc_lru.tqe_next != 0) {
    141 				TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
    142 				TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
    143 			}
    144 			return (ENOENT);
    145 		} else
    146 			nchstats.ncs_badhits++;
    147 	} else if (ncp->nc_vpid != ncp->nc_vp->v_id) {
    148 		nchstats.ncs_falsehits++;
    149 	} else {
    150 		nchstats.ncs_goodhits++;
    151 		/*
    152 		 * move this slot to end of LRU chain, if not already there
    153 		 */
    154 		if (ncp->nc_lru.tqe_next != 0) {
    155 			TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
    156 			TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
    157 		}
    158 		*vpp = ncp->nc_vp;
    159 		return (-1);
    160 	}
    161 
    162 	/*
    163 	 * Last component and we are renaming or deleting,
    164 	 * the cache entry is invalid, or otherwise don't
    165 	 * want cache entry to exist.
    166 	 */
    167 	TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
    168 	LIST_REMOVE(ncp, nc_hash);
    169 	ncp->nc_hash.le_prev = 0;
    170 	if (ncp->nc_vhash.le_prev != NULL) {
    171 		LIST_REMOVE(ncp, nc_vhash);
    172 		ncp->nc_vhash.le_prev = 0;
    173 	}
    174 	TAILQ_INSERT_HEAD(&nclruhead, ncp, nc_lru);
    175 	return (0);
    176 }
    177 
    178 /*
    179  * Scan cache looking for name of directory entry pointing at vp.
    180  *
    181  * Fill in dvpp.
    182  *
    183  * If bufp is non-NULL, also place the name in the buffer which starts
    184  * at bufp, immediately before *bpp, and move bpp backwards to point
    185  * at the start of it.  (Yes, this is a little baroque, but it's done
    186  * this way to cater to the whims of getcwd).
    187  *
    188  * Returns 0 on success, -1 on cache miss, positive errno on failure.
    189  */
    190 int
    191 cache_revlookup (vp, dvpp, bpp, bufp)
    192 	struct vnode *vp, **dvpp;
    193 	char **bpp;
    194 	char *bufp;
    195 {
    196 	struct namecache *ncp;
    197 	struct vnode *dvp;
    198 	struct ncvhashhead *nvcpp;
    199 
    200 	if (!doingcache)
    201 		goto out;
    202 
    203 	nvcpp = &ncvhashtbl[(vp->v_id & ncvhash)];
    204 
    205 	for (ncp = nvcpp->lh_first; ncp != 0; ncp = ncp->nc_vhash.le_next) {
    206 		if ((ncp->nc_vp == vp) &&
    207 		    (ncp->nc_vpid == vp->v_id) &&
    208 		    ((dvp = ncp->nc_dvp) != 0) &&
    209 		    (dvp != vp) && 		/* avoid pesky . entries.. */
    210 		    (dvp->v_id == ncp->nc_dvpid))
    211 		{
    212 			char *bp;
    213 
    214 #ifdef DIAGNOSTIC
    215 			if ((ncp->nc_nlen == 1) &&
    216 			    (ncp->nc_name[0] == '.'))
    217 				panic("cache_revlookup: found entry for .");
    218 
    219 			if ((ncp->nc_nlen == 2) &&
    220 			    (ncp->nc_name[0] == '.') &&
    221 			    (ncp->nc_name[1] == '.'))
    222 				panic("cache_revlookup: found entry for ..");
    223 #endif
    224 			nchstats.ncs_revhits++;
    225 
    226 			if (bufp) {
    227 				bp = *bpp;
    228 				bp -= ncp->nc_nlen;
    229 				if (bp <= bufp) {
    230 					*dvpp = 0;
    231 					return ERANGE;
    232 				}
    233 				memcpy(bp, ncp->nc_name, ncp->nc_nlen);
    234 				*bpp = bp;
    235 			}
    236 
    237 			/* XXX MP: how do we know dvp won't evaporate? */
    238 			*dvpp = dvp;
    239 			return 0;
    240 		}
    241 	}
    242 	nchstats.ncs_revmiss++;
    243  out:
    244 	*dvpp = 0;
    245 	return -1;
    246 }
    247 
    248 /*
    249  * Add an entry to the cache
    250  */
    251 void
    252 cache_enter(dvp, vp, cnp)
    253 	struct vnode *dvp;
    254 	struct vnode *vp;
    255 	struct componentname *cnp;
    256 {
    257 	register struct namecache *ncp;
    258 	register struct nchashhead *ncpp;
    259 	register struct ncvhashhead *nvcpp;
    260 
    261 #ifdef DIAGNOSTIC
    262 	if (cnp->cn_namelen > NCHNAMLEN)
    263 		panic("cache_enter: name too long");
    264 #endif
    265 	if (!doingcache)
    266 		return;
    267 	/*
    268 	 * Free the cache slot at head of lru chain.
    269 	 */
    270 	if (numcache < desiredvnodes) {
    271 		ncp = pool_get(&namecache_pool, PR_WAITOK);
    272 		memset((char *)ncp, 0, sizeof(*ncp));
    273 		numcache++;
    274 	} else if ((ncp = nclruhead.tqh_first) != NULL) {
    275 		TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
    276 		if (ncp->nc_hash.le_prev != 0) {
    277 			LIST_REMOVE(ncp, nc_hash);
    278 			ncp->nc_hash.le_prev = 0;
    279 		}
    280 		if (ncp->nc_vhash.le_prev != 0) {
    281 			LIST_REMOVE(ncp, nc_vhash);
    282 			ncp->nc_vhash.le_prev = 0;
    283 		}
    284 	} else
    285 		return;
    286 	/* grab the vnode we just found */
    287 	ncp->nc_vp = vp;
    288 	if (vp)
    289 		ncp->nc_vpid = vp->v_id;
    290 	else {
    291 		/*
    292 		 * For negative hits, save the ISWHITEOUT flag so we can
    293 		 * restore it later when the cache entry is used again.
    294 		 */
    295 		ncp->nc_vpid = cnp->cn_flags & ISWHITEOUT;
    296 	}
    297 	/* fill in cache info */
    298 	ncp->nc_dvp = dvp;
    299 	ncp->nc_dvpid = dvp->v_id;
    300 	ncp->nc_nlen = cnp->cn_namelen;
    301 	memcpy(ncp->nc_name, cnp->cn_nameptr, (unsigned)ncp->nc_nlen);
    302 	TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
    303 	ncpp = &nchashtbl[(cnp->cn_hash ^ dvp->v_id) & nchash];
    304 	LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
    305 
    306 	ncp->nc_vhash.le_prev = 0;
    307 	ncp->nc_vhash.le_next = 0;
    308 
    309 	/*
    310 	 * Create reverse-cache entries (used in getcwd) for directories.
    311 	 */
    312 	if (vp &&
    313 	    (vp != dvp) &&
    314 	    (vp->v_type == VDIR) &&
    315 	    ((ncp->nc_nlen > 2) ||
    316 	     ((ncp->nc_nlen == 2) && (ncp->nc_name[0] != '.') && (ncp->nc_name[1] != '.')) ||
    317 	     ((ncp->nc_nlen == 1) && (ncp->nc_name[0] != '.'))))
    318 	{
    319 		nvcpp = &ncvhashtbl[(vp->v_id & ncvhash)];
    320 		LIST_INSERT_HEAD(nvcpp, ncp, nc_vhash);
    321 	}
    322 
    323 }
    324 
    325 /*
    326  * Name cache initialization, from vfs_init() when we are booting
    327  */
    328 void
    329 nchinit()
    330 {
    331 
    332 	TAILQ_INIT(&nclruhead);
    333 	nchashtbl = hashinit(desiredvnodes, M_CACHE, M_WAITOK, &nchash);
    334 	ncvhashtbl = hashinit(desiredvnodes/8, M_CACHE, M_WAITOK, &ncvhash);
    335 	pool_init(&namecache_pool, sizeof(struct namecache), 0, 0, 0,
    336 	    "ncachepl", 0, pool_page_alloc_nointr, pool_page_free_nointr,
    337 	    M_CACHE);
    338 }
    339 
    340 /*
    341  * Cache flush, a particular vnode; called when a vnode is renamed to
    342  * hide entries that would now be invalid
    343  */
    344 void
    345 cache_purge(vp)
    346 	struct vnode *vp;
    347 {
    348 	struct namecache *ncp;
    349 	struct nchashhead *ncpp;
    350 
    351 	vp->v_id = ++nextvnodeid;
    352 	if (nextvnodeid != 0)
    353 		return;
    354 	for (ncpp = &nchashtbl[nchash]; ncpp >= nchashtbl; ncpp--) {
    355 		for (ncp = ncpp->lh_first; ncp != 0; ncp = ncp->nc_hash.le_next) {
    356 			ncp->nc_vpid = 0;
    357 			ncp->nc_dvpid = 0;
    358 		}
    359 	}
    360 	vp->v_id = ++nextvnodeid;
    361 }
    362 
    363 /*
    364  * Cache flush, a whole filesystem; called when filesys is umounted to
    365  * remove entries that would now be invalid
    366  *
    367  * The line "nxtcp = nchhead" near the end is to avoid potential problems
    368  * if the cache lru chain is modified while we are dumping the
    369  * inode.  This makes the algorithm O(n^2), but do you think I care?
    370  */
    371 void
    372 cache_purgevfs(mp)
    373 	struct mount *mp;
    374 {
    375 	register struct namecache *ncp, *nxtcp;
    376 
    377 	for (ncp = nclruhead.tqh_first; ncp != 0; ncp = nxtcp) {
    378 		if (ncp->nc_dvp == NULL || ncp->nc_dvp->v_mount != mp) {
    379 			nxtcp = ncp->nc_lru.tqe_next;
    380 			continue;
    381 		}
    382 		/* free the resources we had */
    383 		ncp->nc_vp = NULL;
    384 		ncp->nc_dvp = NULL;
    385 		TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
    386 		if (ncp->nc_hash.le_prev != 0) {
    387 			LIST_REMOVE(ncp, nc_hash);
    388 			ncp->nc_hash.le_prev = 0;
    389 		}
    390 		if (ncp->nc_vhash.le_prev != 0) {
    391 			LIST_REMOVE(ncp, nc_vhash);
    392 			ncp->nc_vhash.le_prev = 0;
    393 		}
    394 		/* cause rescan of list, it may have altered */
    395 		nxtcp = nclruhead.tqh_first;
    396 		TAILQ_INSERT_HEAD(&nclruhead, ncp, nc_lru);
    397 	}
    398 }
    399 
    400