Home | History | Annotate | Line # | Download | only in kern
vfs_cache.c revision 1.34
      1 /*	$NetBSD: vfs_cache.c,v 1.34 2001/12/10 01:49:26 enami 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.34 2001/12/10 01:49:26 enami 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(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
    112 {
    113 	struct namecache *ncp;
    114 	struct nchashhead *ncpp;
    115 	struct vnode *vp;
    116 	int vpid, error;
    117 
    118 	if (!doingcache) {
    119 		cnp->cn_flags &= ~MAKEENTRY;
    120 		*vpp = NULL;
    121 		return (-1);
    122 	}
    123 	if (cnp->cn_namelen > NCHNAMLEN) {
    124 		nchstats.ncs_long++;
    125 		cnp->cn_flags &= ~MAKEENTRY;
    126 		*vpp = NULL;
    127 		return (-1);
    128 	}
    129 	ncpp = &nchashtbl[NCHASH(cnp, dvp)];
    130 	LIST_FOREACH(ncp, ncpp, nc_hash) {
    131 		if (ncp->nc_dvp == dvp &&
    132 		    ncp->nc_dvpid == dvp->v_id &&
    133 		    ncp->nc_nlen == cnp->cn_namelen &&
    134 		    !memcmp(ncp->nc_name, cnp->cn_nameptr, (u_int)ncp->nc_nlen))
    135 			break;
    136 	}
    137 	if (ncp == 0) {
    138 		nchstats.ncs_miss++;
    139 		*vpp = NULL;
    140 		return (-1);
    141 	}
    142 	if ((cnp->cn_flags & MAKEENTRY) == 0) {
    143 		nchstats.ncs_badhits++;
    144 		goto remove;
    145 	} else if (ncp->nc_vp == NULL) {
    146 		/*
    147 		 * Restore the ISWHITEOUT flag saved earlier.
    148 		 */
    149 		cnp->cn_flags |= ncp->nc_vpid;
    150 		if (cnp->cn_nameiop != CREATE ||
    151 		    (cnp->cn_flags & ISLASTCN) == 0) {
    152 			nchstats.ncs_neghits++;
    153 			/*
    154 			 * Move this slot to end of LRU chain,
    155 			 * if not already there.
    156 			 */
    157 			if (ncp->nc_lru.tqe_next != 0) {
    158 				TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
    159 				TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
    160 			}
    161 			return (ENOENT);
    162 		} else {
    163 			nchstats.ncs_badhits++;
    164 			goto remove;
    165 		}
    166 	} else if (ncp->nc_vpid != ncp->nc_vp->v_id) {
    167 		nchstats.ncs_falsehits++;
    168 		goto remove;
    169 	}
    170 
    171 	vp = ncp->nc_vp;
    172 	vpid = vp->v_id;
    173 	if (vp == dvp) {	/* lookup on "." */
    174 		VREF(dvp);
    175 		error = 0;
    176 	} else if (cnp->cn_flags & ISDOTDOT) {
    177 		VOP_UNLOCK(dvp, 0);
    178 		cnp->cn_flags |= PDIRUNLOCK;
    179 		error = vget(vp, LK_EXCLUSIVE);
    180 		/*
    181 		 * If the above vget() succeeded and both LOCKPARENT and
    182 		 * ISLASTCN is set, lock the directory vnode as well.
    183 		 */
    184 		if (!error && (~cnp->cn_flags & (LOCKPARENT|ISLASTCN)) == 0) {
    185 			if ((error = vn_lock(dvp, LK_EXCLUSIVE)) != 0) {
    186 				vput(vp);
    187 				return (error);
    188 			}
    189 			cnp->cn_flags &= ~PDIRUNLOCK;
    190 		}
    191 	} else {
    192 		error = vget(vp, LK_EXCLUSIVE);
    193 		/*
    194 		 * If the above vget() failed or either of LOCKPARENT or
    195 		 * ISLASTCN is set, unlock the directory vnode.
    196 		 */
    197 		if (error || (~cnp->cn_flags & (LOCKPARENT|ISLASTCN)) != 0) {
    198 			VOP_UNLOCK(dvp, 0);
    199 			cnp->cn_flags |= PDIRUNLOCK;
    200 		}
    201 	}
    202 
    203 	/*
    204 	 * Check that the lock succeeded, and that the capability number did
    205 	 * not change while we were waiting for the lock.
    206 	 */
    207 	if (error || vpid != vp->v_id) {
    208 		if (!error) {
    209 			vput(vp);
    210 			nchstats.ncs_falsehits++;
    211 		} else
    212 			nchstats.ncs_badhits++;
    213 		/*
    214 		 * The parent needs to be locked when we return to VOP_LOOKUP().
    215 		 * The `.' case here should be extremely rare (if it can happen
    216 		 * at all), so we don't bother optimizing out the unlock/relock.
    217 		 */
    218 		if (vp == dvp ||
    219 		    error || (~cnp->cn_flags & (LOCKPARENT|ISLASTCN)) != 0) {
    220 			if ((error = vn_lock(dvp, LK_EXCLUSIVE)) != 0)
    221 				return (error);
    222 			cnp->cn_flags &= ~PDIRUNLOCK;
    223 		}
    224 		*vpp = NULL;
    225 		return (-1);
    226 	}
    227 
    228 	nchstats.ncs_goodhits++;
    229 	/*
    230 	 * Move this slot to end of LRU chain, if not already there.
    231 	 */
    232 	if (ncp->nc_lru.tqe_next != 0) {
    233 		TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
    234 		TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
    235 	}
    236 	*vpp = vp;
    237 	return (0);
    238 
    239 remove:
    240 	/*
    241 	 * Last component and we are renaming or deleting,
    242 	 * the cache entry is invalid, or otherwise don't
    243 	 * want cache entry to exist.
    244 	 */
    245 	TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
    246 	LIST_REMOVE(ncp, nc_hash);
    247 	ncp->nc_hash.le_prev = NULL;
    248 	if (ncp->nc_vhash.le_prev != NULL) {
    249 		LIST_REMOVE(ncp, nc_vhash);
    250 		ncp->nc_vhash.le_prev = NULL;
    251 	}
    252 	TAILQ_INSERT_HEAD(&nclruhead, ncp, nc_lru);
    253 	*vpp = NULL;
    254 	return (-1);
    255 }
    256 
    257 /*
    258  * Scan cache looking for name of directory entry pointing at vp.
    259  *
    260  * Fill in dvpp.
    261  *
    262  * If bufp is non-NULL, also place the name in the buffer which starts
    263  * at bufp, immediately before *bpp, and move bpp backwards to point
    264  * at the start of it.  (Yes, this is a little baroque, but it's done
    265  * this way to cater to the whims of getcwd).
    266  *
    267  * Returns 0 on success, -1 on cache miss, positive errno on failure.
    268  */
    269 int
    270 cache_revlookup(struct vnode *vp, struct vnode **dvpp, char **bpp, char *bufp)
    271 {
    272 	struct namecache *ncp;
    273 	struct vnode *dvp;
    274 	struct ncvhashhead *nvcpp;
    275 	char *bp;
    276 
    277 	if (!doingcache)
    278 		goto out;
    279 
    280 	nvcpp = &ncvhashtbl[NCVHASH(vp)];
    281 
    282 	LIST_FOREACH(ncp, nvcpp, nc_vhash) {
    283 		if (ncp->nc_vp == vp &&
    284 		    ncp->nc_vpid == vp->v_id &&
    285 		    (dvp = ncp->nc_dvp) != NULL &&
    286 		    dvp != vp && 		/* avoid pesky . entries.. */
    287 		    dvp->v_id == ncp->nc_dvpid) {
    288 
    289 #ifdef DIAGNOSTIC
    290 			if (ncp->nc_nlen == 1 &&
    291 			    ncp->nc_name[0] == '.')
    292 				panic("cache_revlookup: found entry for .");
    293 
    294 			if (ncp->nc_nlen == 2 &&
    295 			    ncp->nc_name[0] == '.' &&
    296 			    ncp->nc_name[1] == '.')
    297 				panic("cache_revlookup: found entry for ..");
    298 #endif
    299 			nchstats.ncs_revhits++;
    300 
    301 			if (bufp) {
    302 				bp = *bpp;
    303 				bp -= ncp->nc_nlen;
    304 				if (bp <= bufp) {
    305 					*dvpp = NULL;
    306 					return (ERANGE);
    307 				}
    308 				memcpy(bp, ncp->nc_name, ncp->nc_nlen);
    309 				*bpp = bp;
    310 			}
    311 
    312 			/* XXX MP: how do we know dvp won't evaporate? */
    313 			*dvpp = dvp;
    314 			return (0);
    315 		}
    316 	}
    317 	nchstats.ncs_revmiss++;
    318  out:
    319 	*dvpp = NULL;
    320 	return (-1);
    321 }
    322 
    323 /*
    324  * Add an entry to the cache
    325  */
    326 void
    327 cache_enter(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
    328 {
    329 	struct namecache *ncp;
    330 	struct nchashhead *ncpp;
    331 	struct ncvhashhead *nvcpp;
    332 
    333 #ifdef DIAGNOSTIC
    334 	if (cnp->cn_namelen > NCHNAMLEN)
    335 		panic("cache_enter: name too long");
    336 #endif
    337 	if (!doingcache)
    338 		return;
    339 	/*
    340 	 * Free the cache slot at head of lru chain.
    341 	 */
    342 	if (numcache < numvnodes) {
    343 		ncp = pool_get(&namecache_pool, PR_WAITOK);
    344 		memset(ncp, 0, sizeof(*ncp));
    345 		numcache++;
    346 	} else if ((ncp = TAILQ_FIRST(&nclruhead)) != NULL) {
    347 		TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
    348 		if (ncp->nc_hash.le_prev != NULL) {
    349 			LIST_REMOVE(ncp, nc_hash);
    350 			ncp->nc_hash.le_prev = NULL;
    351 		}
    352 		if (ncp->nc_vhash.le_prev != NULL) {
    353 			LIST_REMOVE(ncp, nc_vhash);
    354 			ncp->nc_vhash.le_prev = NULL;
    355 		}
    356 	} else
    357 		return;
    358 	/* Grab the vnode we just found. */
    359 	ncp->nc_vp = vp;
    360 	if (vp)
    361 		ncp->nc_vpid = vp->v_id;
    362 	else {
    363 		/*
    364 		 * For negative hits, save the ISWHITEOUT flag so we can
    365 		 * restore it later when the cache entry is used again.
    366 		 */
    367 		ncp->nc_vpid = cnp->cn_flags & ISWHITEOUT;
    368 	}
    369 	/* Fill in cache info. */
    370 	ncp->nc_dvp = dvp;
    371 	ncp->nc_dvpid = dvp->v_id;
    372 	ncp->nc_nlen = cnp->cn_namelen;
    373 	memcpy(ncp->nc_name, cnp->cn_nameptr, (unsigned)ncp->nc_nlen);
    374 	TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
    375 	ncpp = &nchashtbl[NCHASH(cnp, dvp)];
    376 	LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
    377 
    378 	ncp->nc_vhash.le_prev = NULL;
    379 	ncp->nc_vhash.le_next = NULL;
    380 
    381 	/*
    382 	 * Create reverse-cache entries (used in getcwd) for directories.
    383 	 */
    384 	if (vp != NULL &&
    385 	    vp != dvp &&
    386 #ifndef NAMECACHE_ENTER_REVERSE
    387 	    vp->v_type == VDIR &&
    388 #endif
    389 	    (ncp->nc_nlen > 2 ||
    390 	    (ncp->nc_nlen > 1 && ncp->nc_name[1] != '.') ||
    391 	    (/* ncp->nc_nlen > 0 && */ ncp->nc_name[0] != '.'))) {
    392 		nvcpp = &ncvhashtbl[NCVHASH(vp)];
    393 		LIST_INSERT_HEAD(nvcpp, ncp, nc_vhash);
    394 	}
    395 }
    396 
    397 /*
    398  * Name cache initialization, from vfs_init() when we are booting
    399  */
    400 void
    401 nchinit(void)
    402 {
    403 
    404 	TAILQ_INIT(&nclruhead);
    405 	nchashtbl =
    406 	    hashinit(desiredvnodes, HASH_LIST, M_CACHE, M_WAITOK, &nchash);
    407 	ncvhashtbl =
    408 #ifdef NAMECACHE_ENTER_REVERSE
    409 	    hashinit(desiredvnodes, HASH_LIST, M_CACHE, M_WAITOK, &ncvhash);
    410 #else
    411 	    hashinit(desiredvnodes/8, HASH_LIST, M_CACHE, M_WAITOK, &ncvhash);
    412 #endif
    413 	pool_init(&namecache_pool, sizeof(struct namecache), 0, 0, 0,
    414 	    "ncachepl", 0, pool_page_alloc_nointr, pool_page_free_nointr,
    415 	    M_CACHE);
    416 }
    417 
    418 /*
    419  * Name cache reinitialization, for when the maximum number of vnodes increases.
    420  */
    421 void
    422 nchreinit(void)
    423 {
    424 	struct namecache *ncp;
    425 	struct nchashhead *oldhash1, *hash1;
    426 	struct ncvhashhead *oldhash2, *hash2;
    427 	u_long oldmask1, oldmask2, mask1, mask2;
    428 	int i;
    429 
    430 	hash1 = hashinit(desiredvnodes, HASH_LIST, M_CACHE, M_WAITOK, &mask1);
    431 	hash2 =
    432 #ifdef NAMECACHE_ENTER_REVERSE
    433 	    hashinit(desiredvnodes, HASH_LIST, M_CACHE, M_WAITOK, &mask2);
    434 #else
    435 	    hashinit(desiredvnodes/8, HASH_LIST, M_CACHE, M_WAITOK, &mask2);
    436 #endif
    437 	oldhash1 = nchashtbl;
    438 	oldmask1 = nchash;
    439 	nchashtbl = hash1;
    440 	nchash = mask1;
    441 	oldhash2 = ncvhashtbl;
    442 	oldmask2 = ncvhash;
    443 	ncvhashtbl = hash2;
    444 	ncvhash = mask2;
    445 	for (i = 0; i <= oldmask1; i++) {
    446 		while ((ncp = LIST_FIRST(&oldhash1[i])) != NULL) {
    447 			LIST_REMOVE(ncp, nc_hash);
    448 			ncp->nc_hash.le_prev = NULL;
    449 		}
    450 	}
    451 	for (i = 0; i <= oldmask2; i++) {
    452 		while ((ncp = LIST_FIRST(&oldhash2[i])) != NULL) {
    453 			LIST_REMOVE(ncp, nc_vhash);
    454 			ncp->nc_vhash.le_prev = NULL;
    455 		}
    456 	}
    457 	hashdone(oldhash1, M_CACHE);
    458 	hashdone(oldhash2, M_CACHE);
    459 }
    460 
    461 /*
    462  * Cache flush, a particular vnode; called when a vnode is renamed to
    463  * hide entries that would now be invalid
    464  */
    465 void
    466 cache_purge(struct vnode *vp)
    467 {
    468 	struct namecache *ncp;
    469 	struct nchashhead *ncpp;
    470 	static u_long nextvnodeid;
    471 
    472 	vp->v_id = ++nextvnodeid;
    473 	if (nextvnodeid != 0)
    474 		return;
    475 	for (ncpp = &nchashtbl[nchash]; ncpp >= nchashtbl; ncpp--) {
    476 		LIST_FOREACH(ncp, ncpp, nc_hash) {
    477 			ncp->nc_vpid = 0;
    478 			ncp->nc_dvpid = 0;
    479 		}
    480 	}
    481 	vp->v_id = ++nextvnodeid;
    482 }
    483 
    484 /*
    485  * Cache flush, a whole filesystem; called when filesys is umounted to
    486  * remove entries that would now be invalid.
    487  */
    488 void
    489 cache_purgevfs(struct mount *mp)
    490 {
    491 	struct namecache *ncp, *nxtcp;
    492 
    493 	for (ncp = TAILQ_FIRST(&nclruhead); ncp != NULL; ncp = nxtcp) {
    494 		nxtcp = TAILQ_NEXT(ncp, nc_lru);
    495 		if (ncp->nc_dvp == NULL || ncp->nc_dvp->v_mount != mp) {
    496 			continue;
    497 		}
    498 		/* Free the resources we had. */
    499 		ncp->nc_vp = NULL;
    500 		ncp->nc_dvp = NULL;
    501 		TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
    502 		if (ncp->nc_hash.le_prev != NULL) {
    503 			LIST_REMOVE(ncp, nc_hash);
    504 			ncp->nc_hash.le_prev = NULL;
    505 		}
    506 		if (ncp->nc_vhash.le_prev != NULL) {
    507 			LIST_REMOVE(ncp, nc_vhash);
    508 			ncp->nc_vhash.le_prev = NULL;
    509 		}
    510 		TAILQ_INSERT_HEAD(&nclruhead, ncp, nc_lru);
    511 	}
    512 }
    513 
    514 #ifdef DDB
    515 void
    516 namecache_print(struct vnode *vp, void (*pr)(const char *, ...))
    517 {
    518 	struct vnode *dvp = NULL;
    519 	struct namecache *ncp;
    520 
    521 	TAILQ_FOREACH(ncp, &nclruhead, nc_lru) {
    522 		if (ncp->nc_vp == vp && ncp->nc_vpid == vp->v_id) {
    523 			(*pr)("name %.*s\n", ncp->nc_nlen, ncp->nc_name);
    524 			dvp = ncp->nc_dvp;
    525 		}
    526 	}
    527 	if (dvp == NULL) {
    528 		(*pr)("name not found\n");
    529 		return;
    530 	}
    531 	vp = dvp;
    532 	TAILQ_FOREACH(ncp, &nclruhead, nc_lru) {
    533 		if (ncp->nc_vp == vp && ncp->nc_vpid == vp->v_id) {
    534 			(*pr)("parent %.*s\n", ncp->nc_nlen, ncp->nc_name);
    535 		}
    536 	}
    537 }
    538 #endif
    539