Home | History | Annotate | Line # | Download | only in kern
vfs_cache.c revision 1.71
      1 /*	$NetBSD: vfs_cache.c,v 1.71 2007/11/07 00:23:25 ad 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. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  *	@(#)vfs_cache.c	8.3 (Berkeley) 8/22/94
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: vfs_cache.c,v 1.71 2007/11/07 00:23:25 ad Exp $");
     36 
     37 #include "opt_ddb.h"
     38 #include "opt_revcache.h"
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/time.h>
     43 #include <sys/mount.h>
     44 #include <sys/vnode.h>
     45 #include <sys/namei.h>
     46 #include <sys/errno.h>
     47 #include <sys/malloc.h>
     48 #include <sys/pool.h>
     49 #include <sys/mutex.h>
     50 
     51 #define NAMECACHE_ENTER_REVERSE
     52 /*
     53  * Name caching works as follows:
     54  *
     55  * Names found by directory scans are retained in a cache
     56  * for future reference.  It is managed LRU, so frequently
     57  * used names will hang around.  Cache is indexed by hash value
     58  * obtained from (dvp, name) where dvp refers to the directory
     59  * containing name.
     60  *
     61  * For simplicity (and economy of storage), names longer than
     62  * a maximum length of NCHNAMLEN are not cached; they occur
     63  * infrequently in any case, and are almost never of interest.
     64  *
     65  * Upon reaching the last segment of a path, if the reference
     66  * is for DELETE, or NOCACHE is set (rewrite), and the
     67  * name is located in the cache, it will be dropped.
     68  * The entry is dropped also when it was not possible to lock
     69  * the cached vnode, either because vget() failed or the generation
     70  * number has changed while waiting for the lock.
     71  */
     72 
     73 /*
     74  * Structures associated with name cacheing.
     75  */
     76 LIST_HEAD(nchashhead, namecache) *nchashtbl;
     77 u_long	nchash;				/* size of hash table - 1 */
     78 long	numcache;			/* number of cache entries allocated */
     79 #define	NCHASH(cnp, dvp)	\
     80 	(((cnp)->cn_hash ^ ((uintptr_t)(dvp) >> 3)) & nchash)
     81 
     82 LIST_HEAD(ncvhashhead, namecache) *ncvhashtbl;
     83 u_long	ncvhash;			/* size of hash table - 1 */
     84 #define	NCVHASH(vp)		(((uintptr_t)(vp) >> 3) & ncvhash)
     85 
     86 TAILQ_HEAD(, namecache) nclruhead;		/* LRU chain */
     87 struct	nchstats nchstats;		/* cache effectiveness statistics */
     88 
     89 static pool_cache_t namecache_cache;
     90 
     91 MALLOC_DEFINE(M_CACHE, "namecache", "Dynamically allocated cache entries");
     92 
     93 int doingcache = 1;			/* 1 => enable the cache */
     94 
     95 /* A single lock to protect cache insertion, removal and lookup */
     96 static kmutex_t namecache_lock;
     97 
     98 static void cache_remove(struct namecache *);
     99 static void cache_free(struct namecache *);
    100 static inline struct namecache *cache_lookup_entry(
    101     const struct vnode *, const struct componentname *);
    102 
    103 static void
    104 cache_remove(struct namecache *ncp)
    105 {
    106 
    107 	KASSERT(mutex_owned(&namecache_lock));
    108 
    109 	ncp->nc_dvp = NULL;
    110 	ncp->nc_vp = NULL;
    111 
    112 	TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
    113 	if (ncp->nc_hash.le_prev != NULL) {
    114 		LIST_REMOVE(ncp, nc_hash);
    115 		ncp->nc_hash.le_prev = NULL;
    116 	}
    117 	if (ncp->nc_vhash.le_prev != NULL) {
    118 		LIST_REMOVE(ncp, nc_vhash);
    119 		ncp->nc_vhash.le_prev = NULL;
    120 	}
    121 	if (ncp->nc_vlist.le_prev != NULL) {
    122 		LIST_REMOVE(ncp, nc_vlist);
    123 		ncp->nc_vlist.le_prev = NULL;
    124 	}
    125 	if (ncp->nc_dvlist.le_prev != NULL) {
    126 		LIST_REMOVE(ncp, nc_dvlist);
    127 		ncp->nc_dvlist.le_prev = NULL;
    128 	}
    129 }
    130 
    131 static void
    132 cache_free(struct namecache *ncp)
    133 {
    134 
    135 	pool_cache_put(namecache_cache, ncp);
    136 	numcache--;
    137 }
    138 
    139 static inline struct namecache *
    140 cache_lookup_entry(const struct vnode *dvp, const struct componentname *cnp)
    141 {
    142 	struct nchashhead *ncpp;
    143 	struct namecache *ncp;
    144 
    145 	KASSERT(mutex_owned(&namecache_lock));
    146 
    147 	ncpp = &nchashtbl[NCHASH(cnp, dvp)];
    148 
    149 	LIST_FOREACH(ncp, ncpp, nc_hash) {
    150 		if (ncp->nc_dvp == dvp &&
    151 		    ncp->nc_nlen == cnp->cn_namelen &&
    152 		    !memcmp(ncp->nc_name, cnp->cn_nameptr, (u_int)ncp->nc_nlen))
    153 			break;
    154 	}
    155 
    156 	return ncp;
    157 }
    158 
    159 /*
    160  * Look for a the name in the cache. We don't do this
    161  * if the segment name is long, simply so the cache can avoid
    162  * holding long names (which would either waste space, or
    163  * add greatly to the complexity).
    164  *
    165  * Lookup is called with ni_dvp pointing to the directory to search,
    166  * ni_ptr pointing to the name of the entry being sought, ni_namelen
    167  * tells the length of the name, and ni_hash contains a hash of
    168  * the name. If the lookup succeeds, the vnode is locked, stored in ni_vp
    169  * and a status of zero is returned. If the locking fails for whatever
    170  * reason, the vnode is unlocked and the error is returned to caller.
    171  * If the lookup determines that the name does not exist (negative cacheing),
    172  * a status of ENOENT is returned. If the lookup fails, a status of -1
    173  * is returned.
    174  */
    175 int
    176 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
    177 {
    178 	struct namecache *ncp;
    179 	struct vnode *vp;
    180 	int error;
    181 
    182 	if (!doingcache) {
    183 		cnp->cn_flags &= ~MAKEENTRY;
    184 		*vpp = NULL;
    185 		return (-1);
    186 	}
    187 
    188 	if (cnp->cn_namelen > NCHNAMLEN) {
    189 		/* Unlocked, but only for stats. */
    190 		nchstats.ncs_long++;
    191 		cnp->cn_flags &= ~MAKEENTRY;
    192 		goto fail;
    193 	}
    194 	mutex_enter(&namecache_lock);
    195 	ncp = cache_lookup_entry(dvp, cnp);
    196 	if (ncp == NULL) {
    197 		nchstats.ncs_miss++;
    198 		goto fail_wlock;
    199 	}
    200 	if ((cnp->cn_flags & MAKEENTRY) == 0) {
    201 		nchstats.ncs_badhits++;
    202 		goto remove;
    203 	} else if (ncp->nc_vp == NULL) {
    204 		/*
    205 		 * Restore the ISWHITEOUT flag saved earlier.
    206 		 */
    207 		cnp->cn_flags |= ncp->nc_flags;
    208 		if (cnp->cn_nameiop != CREATE ||
    209 		    (cnp->cn_flags & ISLASTCN) == 0) {
    210 			nchstats.ncs_neghits++;
    211 			/*
    212 			 * Move this slot to end of LRU chain,
    213 			 * if not already there.
    214 			 */
    215 			if (TAILQ_NEXT(ncp, nc_lru) != 0) {
    216 				TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
    217 				TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
    218 			}
    219 			mutex_exit(&namecache_lock);
    220 			return (ENOENT);
    221 		} else {
    222 			nchstats.ncs_badhits++;
    223 			goto remove;
    224 		}
    225 	}
    226 
    227 	vp = ncp->nc_vp;
    228 
    229 	/*
    230 	 * Move this slot to end of LRU chain, if not already there.
    231 	 */
    232 	if (TAILQ_NEXT(ncp, nc_lru) != 0) {
    233 		TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
    234 		TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
    235 	}
    236 
    237 	error = vget(vp, LK_NOWAIT);
    238 
    239 	/* Release the name cache mutex while we get reference to the vnode */
    240 	mutex_exit(&namecache_lock);
    241 
    242 #ifdef DEBUG
    243 	/*
    244 	 * since we released namecache_lock,
    245 	 * we can't use this pointer any more.
    246 	 */
    247 	ncp = NULL;
    248 #endif /* DEBUG */
    249 
    250 	if (error) {
    251 		KASSERT(error == EBUSY);
    252 		/*
    253 		 * this vnode is being cleaned out.
    254 		 */
    255 		nchstats.ncs_falsehits++; /* XXX badhits? */
    256 		goto fail;
    257 	}
    258 
    259 	if (vp == dvp) {	/* lookup on "." */
    260 		error = 0;
    261 	} else if (cnp->cn_flags & ISDOTDOT) {
    262 		VOP_UNLOCK(dvp, 0);
    263 		error = vn_lock(vp, LK_EXCLUSIVE);
    264 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
    265 	} else {
    266 		error = vn_lock(vp, LK_EXCLUSIVE);
    267 	}
    268 
    269 	/*
    270 	 * Check that the lock succeeded.
    271 	 */
    272 	if (error) {
    273 		/* Unlocked, but only for stats. */
    274 		nchstats.ncs_badhits++;
    275 		*vpp = NULL;
    276 		return (-1);
    277 	}
    278 
    279 	/* Unlocked, but only for stats. */
    280 	nchstats.ncs_goodhits++;
    281 	*vpp = vp;
    282 	return (0);
    283 
    284 remove:
    285 	/*
    286 	 * Last component and we are renaming or deleting,
    287 	 * the cache entry is invalid, or otherwise don't
    288 	 * want cache entry to exist.
    289 	 */
    290 	cache_remove(ncp);
    291 	cache_free(ncp);
    292 
    293 fail_wlock:
    294 	mutex_exit(&namecache_lock);
    295 fail:
    296 	*vpp = NULL;
    297 	return (-1);
    298 }
    299 
    300 int
    301 cache_lookup_raw(struct vnode *dvp, struct vnode **vpp,
    302     struct componentname *cnp)
    303 {
    304 	struct namecache *ncp;
    305 	struct vnode *vp;
    306 	int error;
    307 
    308 	if (!doingcache) {
    309 		cnp->cn_flags &= ~MAKEENTRY;
    310 		*vpp = NULL;
    311 		return (-1);
    312 	}
    313 
    314 	if (cnp->cn_namelen > NCHNAMLEN) {
    315 		/* Unlocked, but only for stats. */
    316 		nchstats.ncs_long++;
    317 		cnp->cn_flags &= ~MAKEENTRY;
    318 		goto fail;
    319 	}
    320 	mutex_enter(&namecache_lock);
    321 	ncp = cache_lookup_entry(dvp, cnp);
    322 	if (ncp == NULL) {
    323 		nchstats.ncs_miss++;
    324 		goto fail_wlock;
    325 	}
    326 	/*
    327 	 * Move this slot to end of LRU chain,
    328 	 * if not already there.
    329 	 */
    330 	if (TAILQ_NEXT(ncp, nc_lru) != 0) {
    331 		TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
    332 		TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
    333 	}
    334 
    335 	vp = ncp->nc_vp;
    336 	if (vp == NULL) {
    337 		/*
    338 		 * Restore the ISWHITEOUT flag saved earlier.
    339 		 */
    340 		cnp->cn_flags |= ncp->nc_flags;
    341 		nchstats.ncs_neghits++;
    342 		mutex_exit(&namecache_lock);
    343 		return (ENOENT);
    344 	}
    345 
    346 	error = vget(vp, LK_NOWAIT);
    347 
    348 	/* Release the name cache mutex while we get reference to the vnode */
    349 	mutex_exit(&namecache_lock);
    350 
    351 	if (error) {
    352 		KASSERT(error == EBUSY);
    353 		/*
    354 		 * this vnode is being cleaned out.
    355 		 */
    356 		nchstats.ncs_falsehits++; /* XXX badhits? */
    357 		goto fail;
    358 	}
    359 
    360 	*vpp = vp;
    361 
    362 	return 0;
    363 
    364 fail_wlock:
    365 	mutex_exit(&namecache_lock);
    366 fail:
    367 	*vpp = NULL;
    368 	return -1;
    369 }
    370 
    371 /*
    372  * Scan cache looking for name of directory entry pointing at vp.
    373  *
    374  * Fill in dvpp.
    375  *
    376  * If bufp is non-NULL, also place the name in the buffer which starts
    377  * at bufp, immediately before *bpp, and move bpp backwards to point
    378  * at the start of it.  (Yes, this is a little baroque, but it's done
    379  * this way to cater to the whims of getcwd).
    380  *
    381  * Returns 0 on success, -1 on cache miss, positive errno on failure.
    382  */
    383 int
    384 cache_revlookup(struct vnode *vp, struct vnode **dvpp, char **bpp, char *bufp)
    385 {
    386 	struct namecache *ncp;
    387 	struct vnode *dvp;
    388 	struct ncvhashhead *nvcpp;
    389 	char *bp;
    390 
    391 	if (!doingcache)
    392 		goto out;
    393 
    394 	nvcpp = &ncvhashtbl[NCVHASH(vp)];
    395 
    396 	mutex_enter(&namecache_lock);
    397 	LIST_FOREACH(ncp, nvcpp, nc_vhash) {
    398 		if (ncp->nc_vp == vp &&
    399 		    (dvp = ncp->nc_dvp) != NULL &&
    400 		    dvp != vp) { 		/* avoid pesky . entries.. */
    401 
    402 #ifdef DIAGNOSTIC
    403 			if (ncp->nc_nlen == 1 &&
    404 			    ncp->nc_name[0] == '.')
    405 				panic("cache_revlookup: found entry for .");
    406 
    407 			if (ncp->nc_nlen == 2 &&
    408 			    ncp->nc_name[0] == '.' &&
    409 			    ncp->nc_name[1] == '.')
    410 				panic("cache_revlookup: found entry for ..");
    411 #endif
    412 			nchstats.ncs_revhits++;
    413 
    414 			if (bufp) {
    415 				bp = *bpp;
    416 				bp -= ncp->nc_nlen;
    417 				if (bp <= bufp) {
    418 					*dvpp = NULL;
    419 					mutex_exit(&namecache_lock);
    420 					return (ERANGE);
    421 				}
    422 				memcpy(bp, ncp->nc_name, ncp->nc_nlen);
    423 				*bpp = bp;
    424 			}
    425 
    426 			/* XXX MP: how do we know dvp won't evaporate? */
    427 			*dvpp = dvp;
    428 			mutex_exit(&namecache_lock);
    429 			return (0);
    430 		}
    431 	}
    432 	nchstats.ncs_revmiss++;
    433 	mutex_exit(&namecache_lock);
    434  out:
    435 	*dvpp = NULL;
    436 	return (-1);
    437 }
    438 
    439 /*
    440  * Add an entry to the cache
    441  */
    442 void
    443 cache_enter(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
    444 {
    445 	struct namecache *ncp;
    446 	struct namecache *oncp;
    447 	struct nchashhead *ncpp;
    448 	struct ncvhashhead *nvcpp;
    449 
    450 #ifdef DIAGNOSTIC
    451 	if (cnp->cn_namelen > NCHNAMLEN)
    452 		panic("cache_enter: name too long");
    453 #endif
    454 	if (!doingcache)
    455 		return;
    456 	/*
    457 	 * Free the cache slot at head of lru chain.
    458 	 */
    459 	mutex_enter(&namecache_lock);
    460 
    461 	if (numcache < numvnodes) {
    462 		numcache++;
    463 		mutex_exit(&namecache_lock);
    464 		ncp = pool_cache_get(namecache_cache, PR_WAITOK);
    465 		memset(ncp, 0, sizeof(*ncp));
    466 		mutex_enter(&namecache_lock);
    467 	} else if ((ncp = TAILQ_FIRST(&nclruhead)) != NULL) {
    468 		cache_remove(ncp);
    469 	} else {
    470 		mutex_exit(&namecache_lock);
    471 		return;
    472 	}
    473 
    474 	/*
    475 	 * Concurrent lookups in the same directory may race for a
    476 	 * cache entry.  if there's a duplicated entry, free it.
    477 	 */
    478 	oncp = cache_lookup_entry(dvp, cnp);
    479 	if (oncp) {
    480 		cache_remove(oncp);
    481 		cache_free(oncp);
    482 	}
    483 	KASSERT(cache_lookup_entry(dvp, cnp) == NULL);
    484 
    485 	/* Grab the vnode we just found. */
    486 	ncp->nc_vp = vp;
    487 	if (vp == NULL) {
    488 		/*
    489 		 * For negative hits, save the ISWHITEOUT flag so we can
    490 		 * restore it later when the cache entry is used again.
    491 		 */
    492 		ncp->nc_flags = cnp->cn_flags & ISWHITEOUT;
    493 	}
    494 	/* Fill in cache info. */
    495 	ncp->nc_dvp = dvp;
    496 	LIST_INSERT_HEAD(&dvp->v_dnclist, ncp, nc_dvlist);
    497 	if (vp)
    498 		LIST_INSERT_HEAD(&vp->v_nclist, ncp, nc_vlist);
    499 	ncp->nc_nlen = cnp->cn_namelen;
    500 	memcpy(ncp->nc_name, cnp->cn_nameptr, (unsigned)ncp->nc_nlen);
    501 	TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
    502 	ncpp = &nchashtbl[NCHASH(cnp, dvp)];
    503 	LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
    504 
    505 	ncp->nc_vhash.le_prev = NULL;
    506 	ncp->nc_vhash.le_next = NULL;
    507 
    508 	/*
    509 	 * Create reverse-cache entries (used in getcwd) for directories.
    510 	 * (and in linux procfs exe node)
    511 	 */
    512 	if (vp != NULL &&
    513 	    vp != dvp &&
    514 #ifndef NAMECACHE_ENTER_REVERSE
    515 	    vp->v_type == VDIR &&
    516 #endif
    517 	    (ncp->nc_nlen > 2 ||
    518 	    (ncp->nc_nlen > 1 && ncp->nc_name[1] != '.') ||
    519 	    (/* ncp->nc_nlen > 0 && */ ncp->nc_name[0] != '.'))) {
    520 		nvcpp = &ncvhashtbl[NCVHASH(vp)];
    521 		LIST_INSERT_HEAD(nvcpp, ncp, nc_vhash);
    522 	}
    523 	mutex_exit(&namecache_lock);
    524 }
    525 
    526 /*
    527  * Name cache initialization, from vfs_init() when we are booting
    528  */
    529 void
    530 nchinit(void)
    531 {
    532 
    533 	namecache_cache = pool_cache_init(sizeof(struct namecache), 0, 0, 0,
    534 	    "ncachepl", NULL, IPL_NONE, NULL, NULL, NULL);
    535 	KASSERT(namecache_cache != NULL);
    536 
    537 	mutex_init(&namecache_lock, MUTEX_DEFAULT, IPL_NONE);
    538 	TAILQ_INIT(&nclruhead);
    539 	nchashtbl =
    540 	    hashinit(desiredvnodes, HASH_LIST, M_CACHE, M_WAITOK, &nchash);
    541 	ncvhashtbl =
    542 #ifdef NAMECACHE_ENTER_REVERSE
    543 	    hashinit(desiredvnodes, HASH_LIST, M_CACHE, M_WAITOK, &ncvhash);
    544 #else
    545 	    hashinit(desiredvnodes/8, HASH_LIST, M_CACHE, M_WAITOK, &ncvhash);
    546 #endif
    547 }
    548 
    549 /*
    550  * Name cache reinitialization, for when the maximum number of vnodes increases.
    551  */
    552 void
    553 nchreinit(void)
    554 {
    555 	struct namecache *ncp;
    556 	struct nchashhead *oldhash1, *hash1;
    557 	struct ncvhashhead *oldhash2, *hash2;
    558 	u_long i, oldmask1, oldmask2, mask1, mask2;
    559 
    560 	hash1 = hashinit(desiredvnodes, HASH_LIST, M_CACHE, M_WAITOK, &mask1);
    561 	hash2 =
    562 #ifdef NAMECACHE_ENTER_REVERSE
    563 	    hashinit(desiredvnodes, HASH_LIST, M_CACHE, M_WAITOK, &mask2);
    564 #else
    565 	    hashinit(desiredvnodes/8, HASH_LIST, M_CACHE, M_WAITOK, &mask2);
    566 #endif
    567 	mutex_enter(&namecache_lock);
    568 	oldhash1 = nchashtbl;
    569 	oldmask1 = nchash;
    570 	nchashtbl = hash1;
    571 	nchash = mask1;
    572 	oldhash2 = ncvhashtbl;
    573 	oldmask2 = ncvhash;
    574 	ncvhashtbl = hash2;
    575 	ncvhash = mask2;
    576 	for (i = 0; i <= oldmask1; i++) {
    577 		while ((ncp = LIST_FIRST(&oldhash1[i])) != NULL) {
    578 			LIST_REMOVE(ncp, nc_hash);
    579 			ncp->nc_hash.le_prev = NULL;
    580 		}
    581 	}
    582 	for (i = 0; i <= oldmask2; i++) {
    583 		while ((ncp = LIST_FIRST(&oldhash2[i])) != NULL) {
    584 			LIST_REMOVE(ncp, nc_vhash);
    585 			ncp->nc_vhash.le_prev = NULL;
    586 		}
    587 	}
    588 	mutex_exit(&namecache_lock);
    589 	hashdone(oldhash1, M_CACHE);
    590 	hashdone(oldhash2, M_CACHE);
    591 }
    592 
    593 /*
    594  * Cache flush, a particular vnode; called when a vnode is renamed to
    595  * hide entries that would now be invalid
    596  */
    597 void
    598 cache_purge1(struct vnode *vp, const struct componentname *cnp, int flags)
    599 {
    600 	struct namecache *ncp, *ncnext;
    601 
    602 	mutex_enter(&namecache_lock);
    603 	if (flags & PURGE_PARENTS) {
    604 		for (ncp = LIST_FIRST(&vp->v_nclist); ncp != NULL;
    605 		    ncp = ncnext) {
    606 			ncnext = LIST_NEXT(ncp, nc_vlist);
    607 			cache_remove(ncp);
    608 			cache_free(ncp);
    609 		}
    610 	}
    611 	if (flags & PURGE_CHILDREN) {
    612 		for (ncp = LIST_FIRST(&vp->v_dnclist); ncp != NULL;
    613 		    ncp = ncnext) {
    614 			ncnext = LIST_NEXT(ncp, nc_dvlist);
    615 			cache_remove(ncp);
    616 			cache_free(ncp);
    617 		}
    618 	}
    619 	if (cnp != NULL) {
    620 		ncp = cache_lookup_entry(vp, cnp);
    621 		if (ncp) {
    622 			cache_remove(ncp);
    623 			cache_free(ncp);
    624 		}
    625 	}
    626 	mutex_exit(&namecache_lock);
    627 }
    628 
    629 /*
    630  * Cache flush, a whole filesystem; called when filesys is umounted to
    631  * remove entries that would now be invalid.
    632  */
    633 void
    634 cache_purgevfs(struct mount *mp)
    635 {
    636 	struct namecache *ncp, *nxtcp;
    637 
    638 	mutex_enter(&namecache_lock);
    639 	for (ncp = TAILQ_FIRST(&nclruhead); ncp != NULL; ncp = nxtcp) {
    640 		nxtcp = TAILQ_NEXT(ncp, nc_lru);
    641 		if (ncp->nc_dvp == NULL || ncp->nc_dvp->v_mount != mp) {
    642 			continue;
    643 		}
    644 		/* Free the resources we had. */
    645 		cache_remove(ncp);
    646 		cache_free(ncp);
    647 	}
    648 	mutex_exit(&namecache_lock);
    649 }
    650 
    651 #ifdef DDB
    652 void
    653 namecache_print(struct vnode *vp, void (*pr)(const char *, ...))
    654 {
    655 	struct vnode *dvp = NULL;
    656 	struct namecache *ncp;
    657 
    658 	TAILQ_FOREACH(ncp, &nclruhead, nc_lru) {
    659 		if (ncp->nc_vp == vp) {
    660 			(*pr)("name %.*s\n", ncp->nc_nlen, ncp->nc_name);
    661 			dvp = ncp->nc_dvp;
    662 		}
    663 	}
    664 	if (dvp == NULL) {
    665 		(*pr)("name not found\n");
    666 		return;
    667 	}
    668 	vp = dvp;
    669 	TAILQ_FOREACH(ncp, &nclruhead, nc_lru) {
    670 		if (ncp->nc_vp == vp) {
    671 			(*pr)("parent %.*s\n", ncp->nc_nlen, ncp->nc_name);
    672 		}
    673 	}
    674 }
    675 #endif
    676