Home | History | Annotate | Line # | Download | only in kern
vfs_cache.c revision 1.32
      1 /*	$NetBSD: vfs_cache.c,v 1.32 2001/11/12 15:25:36 lukem 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/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: vfs_cache.c,v 1.32 2001/11/12 15:25:36 lukem Exp $");
     40 
     41 #include "opt_ddb.h"
     42 #include "opt_revcache.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/time.h>
     47 #include <sys/mount.h>
     48 #include <sys/vnode.h>
     49 #include <sys/namei.h>
     50 #include <sys/errno.h>
     51 #include <sys/malloc.h>
     52 #include <sys/pool.h>
     53 
     54 /*
     55  * Name caching works as follows:
     56  *
     57  * Names found by directory scans are retained in a cache
     58  * for future reference.  It is managed LRU, so frequently
     59  * used names will hang around.  Cache is indexed by hash value
     60  * obtained from (dvp, name) where dvp refers to the directory
     61  * containing name.
     62  *
     63  * For simplicity (and economy of storage), names longer than
     64  * a maximum length of NCHNAMLEN are not cached; they occur
     65  * infrequently in any case, and are almost never of interest.
     66  *
     67  * Upon reaching the last segment of a path, if the reference
     68  * is for DELETE, or NOCACHE is set (rewrite), and the
     69  * name is located in the cache, it will be dropped.
     70  * The entry is dropped also when it was not possible to lock
     71  * the cached vnode, either because vget() failed or the generation
     72  * number has changed while waiting for the lock.
     73  */
     74 
     75 /*
     76  * Structures associated with name cacheing.
     77  */
     78 LIST_HEAD(nchashhead, namecache) *nchashtbl;
     79 u_long	nchash;				/* size of hash table - 1 */
     80 long	numcache;			/* number of cache entries allocated */
     81 #define NCHASH(cnp, dvp)	(((cnp)->cn_hash ^ (dvp)->v_id) & nchash)
     82 
     83 LIST_HEAD(ncvhashhead, namecache) *ncvhashtbl;
     84 u_long	ncvhash;			/* size of hash table - 1 */
     85 #define NCVHASH(vp)		((vp)->v_id & ncvhash)
     86 
     87 TAILQ_HEAD(, namecache) nclruhead;		/* LRU chain */
     88 struct	nchstats nchstats;		/* cache effectiveness statistics */
     89 
     90 struct pool namecache_pool;
     91 
     92 int doingcache = 1;			/* 1 => enable the cache */
     93 
     94 /*
     95  * Look for a the name in the cache. We don't do this
     96  * if the segment name is long, simply so the cache can avoid
     97  * holding long names (which would either waste space, or
     98  * add greatly to the complexity).
     99  *
    100  * Lookup is called with ni_dvp pointing to the directory to search,
    101  * ni_ptr pointing to the name of the entry being sought, ni_namelen
    102  * tells the length of the name, and ni_hash contains a hash of
    103  * the name. If the lookup succeeds, the vnode is locked, stored in ni_vp
    104  * and a status of zero is returned. If the locking fails for whatever
    105  * reason, the vnode is unlocked and the error is returned to caller.
    106  * If the lookup determines that the name does not exist (negative cacheing),
    107  * a status of ENOENT is returned. If the lookup fails, a status of -1
    108  * is returned.
    109  */
    110 int
    111 cache_lookup(dvp, vpp, cnp)
    112 	struct vnode *dvp;
    113 	struct vnode **vpp;
    114 	struct componentname *cnp;
    115 {
    116 	struct namecache *ncp;
    117 	struct nchashhead *ncpp;
    118 	struct vnode *vp;
    119 	int vpid, error;
    120 
    121 	if (!doingcache) {
    122 		cnp->cn_flags &= ~MAKEENTRY;
    123 		*vpp = 0;
    124 		return (-1);
    125 	}
    126 	if (cnp->cn_namelen > NCHNAMLEN) {
    127 		nchstats.ncs_long++;
    128 		cnp->cn_flags &= ~MAKEENTRY;
    129 		*vpp = 0;
    130 		return (-1);
    131 	}
    132 	ncpp = &nchashtbl[NCHASH(cnp, dvp)];
    133 	LIST_FOREACH(ncp, ncpp, nc_hash) {
    134 		if (ncp->nc_dvp == dvp &&
    135 		    ncp->nc_dvpid == dvp->v_id &&
    136 		    ncp->nc_nlen == cnp->cn_namelen &&
    137 		    !memcmp(ncp->nc_name, cnp->cn_nameptr, (u_int)ncp->nc_nlen))
    138 			break;
    139 	}
    140 	if (ncp == 0) {
    141 		nchstats.ncs_miss++;
    142 		*vpp = 0;
    143 		return (-1);
    144 	}
    145 	if ((cnp->cn_flags & MAKEENTRY) == 0) {
    146 		nchstats.ncs_badhits++;
    147 		goto remove;
    148 	} else if (ncp->nc_vp == NULL) {
    149 		/*
    150 		 * Restore the ISWHITEOUT flag saved earlier.
    151 		 */
    152 		cnp->cn_flags |= ncp->nc_vpid;
    153 		if (cnp->cn_nameiop != CREATE ||
    154 		    (cnp->cn_flags & ISLASTCN) == 0) {
    155 			nchstats.ncs_neghits++;
    156 			/*
    157 			 * Move this slot to end of LRU chain,
    158 			 * if not already there.
    159 			 */
    160 			if (ncp->nc_lru.tqe_next != 0) {
    161 				TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
    162 				TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
    163 			}
    164 			return (ENOENT);
    165 		} else {
    166 			nchstats.ncs_badhits++;
    167 			goto remove;
    168 		}
    169 	} else if (ncp->nc_vpid != ncp->nc_vp->v_id) {
    170 		nchstats.ncs_falsehits++;
    171 		goto remove;
    172 	}
    173 
    174 	vp = ncp->nc_vp;
    175 	vpid = vp->v_id;
    176 	if (vp == dvp) {	/* lookup on "." */
    177 		VREF(dvp);
    178 		error = 0;
    179 	} else if (cnp->cn_flags & ISDOTDOT) {
    180 		VOP_UNLOCK(dvp, 0);
    181 		cnp->cn_flags |= PDIRUNLOCK;
    182 		error = vget(vp, LK_EXCLUSIVE);
    183 		/* if the above vget() succeeded and both LOCKPARENT and
    184 		 * ISLASTCN is set, lock the directory vnode as well */
    185 		if (!error && (~cnp->cn_flags & (LOCKPARENT|ISLASTCN)) == 0) {
    186 			if ((error = vn_lock(dvp, LK_EXCLUSIVE)) != 0) {
    187 				vput(vp);
    188 				return (error);
    189 			}
    190 			cnp->cn_flags &= ~PDIRUNLOCK;
    191 		}
    192 	} else {
    193 		error = vget(vp, LK_EXCLUSIVE);
    194 		/* if the above vget() failed or either of LOCKPARENT or
    195 		 * ISLASTCN is set, unlock the directory vnode */
    196 		if (error || (~cnp->cn_flags & (LOCKPARENT|ISLASTCN)) != 0) {
    197 			VOP_UNLOCK(dvp, 0);
    198 			cnp->cn_flags |= PDIRUNLOCK;
    199 		}
    200 	}
    201 
    202 	/*
    203 	 * Check that the lock succeeded, and that the capability number did
    204 	 * not change while we were waiting for the lock.
    205 	 */
    206 	if (error || vpid != vp->v_id) {
    207 		if (!error) {
    208 			vput(vp);
    209 			nchstats.ncs_falsehits++;
    210 		} else
    211 			nchstats.ncs_badhits++;
    212 		/*
    213 		 * The parent needs to be locked when we return to VOP_LOOKUP().
    214 		 * The `.' case here should be extremely rare (if it can happen
    215 		 * at all), so we don't bother optimizing out the unlock/relock.
    216 		 */
    217 		if (vp == dvp ||
    218 		    error || (~cnp->cn_flags & (LOCKPARENT|ISLASTCN)) != 0) {
    219 			if ((error = vn_lock(dvp, LK_EXCLUSIVE)) != 0)
    220 				return (error);
    221 			cnp->cn_flags &= ~PDIRUNLOCK;
    222 		}
    223 		*vpp = NULL;
    224 		return (-1);
    225 	}
    226 
    227 	nchstats.ncs_goodhits++;
    228 	/*
    229 	 * move this slot to end of LRU chain, if not already there
    230 	 */
    231 	if (ncp->nc_lru.tqe_next != 0) {
    232 		TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
    233 		TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
    234 	}
    235 	*vpp = vp;
    236 	return (0);
    237 
    238 remove:
    239 	/*
    240 	 * Last component and we are renaming or deleting,
    241 	 * the cache entry is invalid, or otherwise don't
    242 	 * want cache entry to exist.
    243 	 */
    244 	TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
    245 	LIST_REMOVE(ncp, nc_hash);
    246 	ncp->nc_hash.le_prev = 0;
    247 	if (ncp->nc_vhash.le_prev != NULL) {
    248 		LIST_REMOVE(ncp, nc_vhash);
    249 		ncp->nc_vhash.le_prev = 0;
    250 	}
    251 	TAILQ_INSERT_HEAD(&nclruhead, ncp, nc_lru);
    252 	*vpp = 0;
    253 	return (-1);
    254 }
    255 
    256 /*
    257  * Scan cache looking for name of directory entry pointing at vp.
    258  *
    259  * Fill in dvpp.
    260  *
    261  * If bufp is non-NULL, also place the name in the buffer which starts
    262  * at bufp, immediately before *bpp, and move bpp backwards to point
    263  * at the start of it.  (Yes, this is a little baroque, but it's done
    264  * this way to cater to the whims of getcwd).
    265  *
    266  * Returns 0 on success, -1 on cache miss, positive errno on failure.
    267  */
    268 int
    269 cache_revlookup (vp, dvpp, bpp, bufp)
    270 	struct vnode *vp, **dvpp;
    271 	char **bpp;
    272 	char *bufp;
    273 {
    274 	struct namecache *ncp;
    275 	struct vnode *dvp;
    276 	struct ncvhashhead *nvcpp;
    277 
    278 	if (!doingcache)
    279 		goto out;
    280 
    281 	nvcpp = &ncvhashtbl[NCVHASH(vp)];
    282 
    283 	LIST_FOREACH(ncp, nvcpp, nc_vhash) {
    284 		if ((ncp->nc_vp == vp) &&
    285 		    (ncp->nc_vpid == vp->v_id) &&
    286 		    ((dvp = ncp->nc_dvp) != 0) &&
    287 		    (dvp != vp) && 		/* avoid pesky . entries.. */
    288 		    (dvp->v_id == ncp->nc_dvpid))
    289 		{
    290 			char *bp;
    291 
    292 #ifdef DIAGNOSTIC
    293 			if ((ncp->nc_nlen == 1) &&
    294 			    (ncp->nc_name[0] == '.'))
    295 				panic("cache_revlookup: found entry for .");
    296 
    297 			if ((ncp->nc_nlen == 2) &&
    298 			    (ncp->nc_name[0] == '.') &&
    299 			    (ncp->nc_name[1] == '.'))
    300 				panic("cache_revlookup: found entry for ..");
    301 #endif
    302 			nchstats.ncs_revhits++;
    303 
    304 			if (bufp) {
    305 				bp = *bpp;
    306 				bp -= ncp->nc_nlen;
    307 				if (bp <= bufp) {
    308 					*dvpp = 0;
    309 					return ERANGE;
    310 				}
    311 				memcpy(bp, ncp->nc_name, ncp->nc_nlen);
    312 				*bpp = bp;
    313 			}
    314 
    315 			/* XXX MP: how do we know dvp won't evaporate? */
    316 			*dvpp = dvp;
    317 			return 0;
    318 		}
    319 	}
    320 	nchstats.ncs_revmiss++;
    321  out:
    322 	*dvpp = 0;
    323 	return -1;
    324 }
    325 
    326 /*
    327  * Add an entry to the cache
    328  */
    329 void
    330 cache_enter(dvp, vp, cnp)
    331 	struct vnode *dvp;
    332 	struct vnode *vp;
    333 	struct componentname *cnp;
    334 {
    335 	struct namecache *ncp;
    336 	struct nchashhead *ncpp;
    337 	struct ncvhashhead *nvcpp;
    338 
    339 #ifdef DIAGNOSTIC
    340 	if (cnp->cn_namelen > NCHNAMLEN)
    341 		panic("cache_enter: name too long");
    342 #endif
    343 	if (!doingcache)
    344 		return;
    345 	/*
    346 	 * Free the cache slot at head of lru chain.
    347 	 */
    348 	if (numcache < numvnodes) {
    349 		ncp = pool_get(&namecache_pool, PR_WAITOK);
    350 		memset(ncp, 0, sizeof(*ncp));
    351 		numcache++;
    352 	} else if ((ncp = TAILQ_FIRST(&nclruhead)) != NULL) {
    353 		TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
    354 		if (ncp->nc_hash.le_prev != 0) {
    355 			LIST_REMOVE(ncp, nc_hash);
    356 			ncp->nc_hash.le_prev = 0;
    357 		}
    358 		if (ncp->nc_vhash.le_prev != 0) {
    359 			LIST_REMOVE(ncp, nc_vhash);
    360 			ncp->nc_vhash.le_prev = 0;
    361 		}
    362 	} else
    363 		return;
    364 	/* grab the vnode we just found */
    365 	ncp->nc_vp = vp;
    366 	if (vp)
    367 		ncp->nc_vpid = vp->v_id;
    368 	else {
    369 		/*
    370 		 * For negative hits, save the ISWHITEOUT flag so we can
    371 		 * restore it later when the cache entry is used again.
    372 		 */
    373 		ncp->nc_vpid = cnp->cn_flags & ISWHITEOUT;
    374 	}
    375 	/* fill in cache info */
    376 	ncp->nc_dvp = dvp;
    377 	ncp->nc_dvpid = dvp->v_id;
    378 	ncp->nc_nlen = cnp->cn_namelen;
    379 	memcpy(ncp->nc_name, cnp->cn_nameptr, (unsigned)ncp->nc_nlen);
    380 	TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
    381 	ncpp = &nchashtbl[NCHASH(cnp, dvp)];
    382 	LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
    383 
    384 	ncp->nc_vhash.le_prev = 0;
    385 	ncp->nc_vhash.le_next = 0;
    386 
    387 	/*
    388 	 * Create reverse-cache entries (used in getcwd) for directories.
    389 	 */
    390 	if (vp &&
    391 	    (vp != dvp) &&
    392 #ifndef NAMECACHE_ENTER_REVERSE
    393 	    (vp->v_type == VDIR) &&
    394 #endif
    395 	    ((ncp->nc_nlen > 2) ||
    396 	     ((ncp->nc_nlen == 2) && (ncp->nc_name[0] != '.') && (ncp->nc_name[1] != '.')) ||
    397 	     ((ncp->nc_nlen == 1) && (ncp->nc_name[0] != '.'))))
    398 	{
    399 		nvcpp = &ncvhashtbl[NCVHASH(vp)];
    400 		LIST_INSERT_HEAD(nvcpp, ncp, nc_vhash);
    401 	}
    402 
    403 }
    404 
    405 /*
    406  * Name cache initialization, from vfs_init() when we are booting
    407  */
    408 void
    409 nchinit()
    410 {
    411 
    412 	TAILQ_INIT(&nclruhead);
    413 	nchashtbl =
    414 	    hashinit(desiredvnodes, HASH_LIST, M_CACHE, M_WAITOK, &nchash);
    415 	ncvhashtbl =
    416 #ifdef NAMECACHE_ENTER_REVERSE
    417 	    hashinit(desiredvnodes, HASH_LIST, M_CACHE, M_WAITOK, &ncvhash);
    418 #else
    419 	    hashinit(desiredvnodes/8, HASH_LIST, M_CACHE, M_WAITOK, &ncvhash);
    420 #endif
    421 	pool_init(&namecache_pool, sizeof(struct namecache), 0, 0, 0,
    422 	    "ncachepl", 0, pool_page_alloc_nointr, pool_page_free_nointr,
    423 	    M_CACHE);
    424 }
    425 
    426 /*
    427  * Name cache reinitialization, for when the maximum number of vnodes increases.
    428  */
    429 void
    430 nchreinit()
    431 {
    432 	struct namecache *ncp;
    433 	struct nchashhead *oldhash1, *hash1;
    434 	struct ncvhashhead *oldhash2, *hash2;
    435 	u_long oldmask1, oldmask2, mask1, mask2;
    436 	int i;
    437 
    438 	hash1 = hashinit(desiredvnodes, HASH_LIST, M_CACHE, M_WAITOK, &mask1);
    439 	hash2 =
    440 #ifdef NAMECACHE_ENTER_REVERSE
    441 	    hashinit(desiredvnodes, HASH_LIST, M_CACHE, M_WAITOK, &mask2);
    442 #else
    443 	    hashinit(desiredvnodes/8, HASH_LIST, M_CACHE, M_WAITOK, &mask2);
    444 #endif
    445 	oldhash1 = nchashtbl;
    446 	oldmask1 = nchash;
    447 	nchashtbl = hash1;
    448 	nchash = mask1;
    449 	oldhash2 = ncvhashtbl;
    450 	oldmask2 = ncvhash;
    451 	ncvhashtbl = hash2;
    452 	ncvhash = mask2;
    453 	for (i = 0; i <= oldmask1; i++) {
    454 		while ((ncp = LIST_FIRST(&oldhash1[i])) != NULL) {
    455 			LIST_REMOVE(ncp, nc_hash);
    456 			ncp->nc_hash.le_prev = NULL;
    457 		}
    458 	}
    459 	for (i = 0; i <= oldmask2; i++) {
    460 		while ((ncp = LIST_FIRST(&oldhash2[i])) != NULL) {
    461 			LIST_REMOVE(ncp, nc_vhash);
    462 			ncp->nc_vhash.le_prev = NULL;
    463 		}
    464 	}
    465 	hashdone(oldhash1, M_CACHE);
    466 	hashdone(oldhash2, M_CACHE);
    467 }
    468 
    469 /*
    470  * Cache flush, a particular vnode; called when a vnode is renamed to
    471  * hide entries that would now be invalid
    472  */
    473 void
    474 cache_purge(vp)
    475 	struct vnode *vp;
    476 {
    477 	struct namecache *ncp;
    478 	struct nchashhead *ncpp;
    479 	static u_long nextvnodeid;
    480 
    481 	vp->v_id = ++nextvnodeid;
    482 	if (nextvnodeid != 0)
    483 		return;
    484 	for (ncpp = &nchashtbl[nchash]; ncpp >= nchashtbl; ncpp--) {
    485 		LIST_FOREACH(ncp, ncpp, nc_hash) {
    486 			ncp->nc_vpid = 0;
    487 			ncp->nc_dvpid = 0;
    488 		}
    489 	}
    490 	vp->v_id = ++nextvnodeid;
    491 }
    492 
    493 /*
    494  * Cache flush, a whole filesystem; called when filesys is umounted to
    495  * remove entries that would now be invalid.
    496  */
    497 void
    498 cache_purgevfs(mp)
    499 	struct mount *mp;
    500 {
    501 	struct namecache *ncp, *nxtcp;
    502 
    503 	for (ncp = TAILQ_FIRST(&nclruhead); ncp != NULL; ncp = nxtcp) {
    504 		nxtcp = TAILQ_NEXT(ncp, nc_lru);
    505 		if (ncp->nc_dvp == NULL || ncp->nc_dvp->v_mount != mp) {
    506 			continue;
    507 		}
    508 		/* free the resources we had */
    509 		ncp->nc_vp = NULL;
    510 		ncp->nc_dvp = NULL;
    511 		TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
    512 		if (ncp->nc_hash.le_prev != 0) {
    513 			LIST_REMOVE(ncp, nc_hash);
    514 			ncp->nc_hash.le_prev = 0;
    515 		}
    516 		if (ncp->nc_vhash.le_prev != 0) {
    517 			LIST_REMOVE(ncp, nc_vhash);
    518 			ncp->nc_vhash.le_prev = 0;
    519 		}
    520 		TAILQ_INSERT_HEAD(&nclruhead, ncp, nc_lru);
    521 	}
    522 }
    523 
    524 #ifdef DDB
    525 void
    526 namecache_print(struct vnode *vp, void (*pr)(const char *, ...))
    527 {
    528 	struct vnode *dvp = NULL;
    529 	struct namecache *ncp;
    530 
    531 	TAILQ_FOREACH(ncp, &nclruhead, nc_lru) {
    532 		if (ncp->nc_vp == vp && ncp->nc_vpid == vp->v_id) {
    533 			(*pr)("name %.*s\n", ncp->nc_nlen, ncp->nc_name);
    534 			dvp = ncp->nc_dvp;
    535 		}
    536 	}
    537 	if (dvp == NULL) {
    538 		(*pr)("name not found\n");
    539 		return;
    540 	}
    541 	vp = dvp;
    542 	TAILQ_FOREACH(ncp, &nclruhead, nc_lru) {
    543 		if (ncp->nc_vp == vp && ncp->nc_vpid == vp->v_id) {
    544 			(*pr)("parent %.*s\n", ncp->nc_nlen, ncp->nc_name);
    545 		}
    546 	}
    547 }
    548 #endif
    549