Home | History | Annotate | Line # | Download | only in kern
vfs_cache.c revision 1.65
      1 /*	$NetBSD: vfs_cache.c,v 1.65 2006/09/10 05:51:33 dbj 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.65 2006/09/10 05:51:33 dbj 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/lock.h>
     50 
     51 /*
     52  * Name caching works as follows:
     53  *
     54  * Names found by directory scans are retained in a cache
     55  * for future reference.  It is managed LRU, so frequently
     56  * used names will hang around.  Cache is indexed by hash value
     57  * obtained from (dvp, name) where dvp refers to the directory
     58  * containing name.
     59  *
     60  * For simplicity (and economy of storage), names longer than
     61  * a maximum length of NCHNAMLEN are not cached; they occur
     62  * infrequently in any case, and are almost never of interest.
     63  *
     64  * Upon reaching the last segment of a path, if the reference
     65  * is for DELETE, or NOCACHE is set (rewrite), and the
     66  * name is located in the cache, it will be dropped.
     67  * The entry is dropped also when it was not possible to lock
     68  * the cached vnode, either because vget() failed or the generation
     69  * number has changed while waiting for the lock.
     70  */
     71 
     72 /*
     73  * Structures associated with name cacheing.
     74  */
     75 LIST_HEAD(nchashhead, namecache) *nchashtbl;
     76 u_long	nchash;				/* size of hash table - 1 */
     77 long	numcache;			/* number of cache entries allocated */
     78 #define	NCHASH(cnp, dvp)	\
     79 	(((cnp)->cn_hash ^ ((uintptr_t)(dvp) >> 3)) & nchash)
     80 
     81 LIST_HEAD(ncvhashhead, namecache) *ncvhashtbl;
     82 u_long	ncvhash;			/* size of hash table - 1 */
     83 #define	NCVHASH(vp)		(((uintptr_t)(vp) >> 3) & ncvhash)
     84 
     85 TAILQ_HEAD(, namecache) nclruhead;		/* LRU chain */
     86 struct	nchstats nchstats;		/* cache effectiveness statistics */
     87 
     88 POOL_INIT(namecache_pool, sizeof(struct namecache), 0, 0, 0, "ncachepl",
     89     &pool_allocator_nointr);
     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 struct simplelock namecache_slock = SIMPLELOCK_INITIALIZER;
     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 	LOCK_ASSERT(simple_lock_held(&namecache_slock));
    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_put(&namecache_pool, 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 	LOCK_ASSERT(simple_lock_held(&namecache_slock));
    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 		/* XXXSMP - updating stats without lock; do we care? */
    190 		nchstats.ncs_long++;
    191 		cnp->cn_flags &= ~MAKEENTRY;
    192 		goto fail;
    193 	}
    194 	simple_lock(&namecache_slock);
    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 			simple_unlock(&namecache_slock);
    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 	simple_unlock(&namecache_slock);
    241 
    242 #ifdef DEBUG
    243 	/*
    244 	 * since we released namecache_slock,
    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 		cnp->cn_flags |= PDIRUNLOCK;
    264 		error = vn_lock(vp, LK_EXCLUSIVE);
    265 		/*
    266 		 * If the above vn_lock() succeeded and both LOCKPARENT and
    267 		 * ISLASTCN is set, lock the directory vnode as well.
    268 		 */
    269 		if (!error && (~cnp->cn_flags & (LOCKPARENT|ISLASTCN)) == 0) {
    270 			if ((error = vn_lock(dvp, LK_EXCLUSIVE)) != 0) {
    271 				vput(vp);
    272 				return (error);
    273 			}
    274 			cnp->cn_flags &= ~PDIRUNLOCK;
    275 		}
    276 	} else {
    277 		error = vn_lock(vp, LK_EXCLUSIVE);
    278 		/*
    279 		 * If the above vn_lock() failed or either of LOCKPARENT or
    280 		 * ISLASTCN is not set, unlock the directory vnode.
    281 		 */
    282 		if (error || (~cnp->cn_flags & (LOCKPARENT|ISLASTCN)) != 0) {
    283 			VOP_UNLOCK(dvp, 0);
    284 			cnp->cn_flags |= PDIRUNLOCK;
    285 		}
    286 	}
    287 
    288 	/*
    289 	 * Check that the lock succeeded.
    290 	 */
    291 	if (error) {
    292 		/* XXXSMP - updating stats without lock; do we care? */
    293 		nchstats.ncs_badhits++;
    294 
    295 		/*
    296 		 * The parent needs to be locked when we return to VOP_LOOKUP().
    297 		 * The `.' case here should be extremely rare (if it can happen
    298 		 * at all), so we don't bother optimizing out the unlock/relock.
    299 		 */
    300 		if ((error = vn_lock(dvp, LK_EXCLUSIVE)) != 0)
    301 			return (error);
    302 		cnp->cn_flags &= ~PDIRUNLOCK;
    303 		*vpp = NULL;
    304 		return (-1);
    305 	}
    306 
    307 	/* XXXSMP - updating stats without lock; do we care? */
    308 	nchstats.ncs_goodhits++;
    309 	*vpp = vp;
    310 	return (0);
    311 
    312 remove:
    313 	/*
    314 	 * Last component and we are renaming or deleting,
    315 	 * the cache entry is invalid, or otherwise don't
    316 	 * want cache entry to exist.
    317 	 */
    318 	cache_remove(ncp);
    319 	cache_free(ncp);
    320 
    321 fail_wlock:
    322 	simple_unlock(&namecache_slock);
    323 fail:
    324 	*vpp = NULL;
    325 	return (-1);
    326 }
    327 
    328 int
    329 cache_lookup_raw(struct vnode *dvp, struct vnode **vpp,
    330     struct componentname *cnp)
    331 {
    332 	struct namecache *ncp;
    333 	struct vnode *vp;
    334 	int error;
    335 
    336 	if (!doingcache) {
    337 		cnp->cn_flags &= ~MAKEENTRY;
    338 		*vpp = NULL;
    339 		return (-1);
    340 	}
    341 
    342 	if (cnp->cn_namelen > NCHNAMLEN) {
    343 		/* XXXSMP - updating stats without lock; do we care? */
    344 		nchstats.ncs_long++;
    345 		cnp->cn_flags &= ~MAKEENTRY;
    346 		goto fail;
    347 	}
    348 	simple_lock(&namecache_slock);
    349 	ncp = cache_lookup_entry(dvp, cnp);
    350 	if (ncp == NULL) {
    351 		nchstats.ncs_miss++;
    352 		goto fail_wlock;
    353 	}
    354 	/*
    355 	 * Move this slot to end of LRU chain,
    356 	 * if not already there.
    357 	 */
    358 	if (TAILQ_NEXT(ncp, nc_lru) != 0) {
    359 		TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
    360 		TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
    361 	}
    362 
    363 	vp = ncp->nc_vp;
    364 	if (vp == NULL) {
    365 		/*
    366 		 * Restore the ISWHITEOUT flag saved earlier.
    367 		 */
    368 		cnp->cn_flags |= ncp->nc_flags;
    369 		nchstats.ncs_neghits++;
    370 		simple_unlock(&namecache_slock);
    371 		return (ENOENT);
    372 	}
    373 
    374 	error = vget(vp, LK_NOWAIT);
    375 
    376 	/* Release the name cache mutex while we get reference to the vnode */
    377 	simple_unlock(&namecache_slock);
    378 
    379 	if (error) {
    380 		KASSERT(error == EBUSY);
    381 		/*
    382 		 * this vnode is being cleaned out.
    383 		 */
    384 		nchstats.ncs_falsehits++; /* XXX badhits? */
    385 		goto fail;
    386 	}
    387 
    388 	*vpp = vp;
    389 
    390 	return 0;
    391 
    392 fail_wlock:
    393 	simple_unlock(&namecache_slock);
    394 fail:
    395 	*vpp = NULL;
    396 	return -1;
    397 }
    398 
    399 /*
    400  * Scan cache looking for name of directory entry pointing at vp.
    401  *
    402  * Fill in dvpp.
    403  *
    404  * If bufp is non-NULL, also place the name in the buffer which starts
    405  * at bufp, immediately before *bpp, and move bpp backwards to point
    406  * at the start of it.  (Yes, this is a little baroque, but it's done
    407  * this way to cater to the whims of getcwd).
    408  *
    409  * Returns 0 on success, -1 on cache miss, positive errno on failure.
    410  */
    411 int
    412 cache_revlookup(struct vnode *vp, struct vnode **dvpp, char **bpp, char *bufp)
    413 {
    414 	struct namecache *ncp;
    415 	struct vnode *dvp;
    416 	struct ncvhashhead *nvcpp;
    417 	char *bp;
    418 
    419 	if (!doingcache)
    420 		goto out;
    421 
    422 	nvcpp = &ncvhashtbl[NCVHASH(vp)];
    423 
    424 	simple_lock(&namecache_slock);
    425 	LIST_FOREACH(ncp, nvcpp, nc_vhash) {
    426 		if (ncp->nc_vp == vp &&
    427 		    (dvp = ncp->nc_dvp) != NULL &&
    428 		    dvp != vp) { 		/* avoid pesky . entries.. */
    429 
    430 #ifdef DIAGNOSTIC
    431 			if (ncp->nc_nlen == 1 &&
    432 			    ncp->nc_name[0] == '.')
    433 				panic("cache_revlookup: found entry for .");
    434 
    435 			if (ncp->nc_nlen == 2 &&
    436 			    ncp->nc_name[0] == '.' &&
    437 			    ncp->nc_name[1] == '.')
    438 				panic("cache_revlookup: found entry for ..");
    439 #endif
    440 			nchstats.ncs_revhits++;
    441 
    442 			if (bufp) {
    443 				bp = *bpp;
    444 				bp -= ncp->nc_nlen;
    445 				if (bp <= bufp) {
    446 					*dvpp = NULL;
    447 					simple_unlock(&namecache_slock);
    448 					return (ERANGE);
    449 				}
    450 				memcpy(bp, ncp->nc_name, ncp->nc_nlen);
    451 				*bpp = bp;
    452 			}
    453 
    454 			/* XXX MP: how do we know dvp won't evaporate? */
    455 			*dvpp = dvp;
    456 			simple_unlock(&namecache_slock);
    457 			return (0);
    458 		}
    459 	}
    460 	nchstats.ncs_revmiss++;
    461 	simple_unlock(&namecache_slock);
    462  out:
    463 	*dvpp = NULL;
    464 	return (-1);
    465 }
    466 
    467 /*
    468  * Add an entry to the cache
    469  */
    470 void
    471 cache_enter(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
    472 {
    473 	struct namecache *ncp;
    474 	struct namecache *oncp;
    475 	struct nchashhead *ncpp;
    476 	struct ncvhashhead *nvcpp;
    477 
    478 #ifdef DIAGNOSTIC
    479 	if (cnp->cn_namelen > NCHNAMLEN)
    480 		panic("cache_enter: name too long");
    481 #endif
    482 	if (!doingcache)
    483 		return;
    484 	/*
    485 	 * Free the cache slot at head of lru chain.
    486 	 */
    487 	simple_lock(&namecache_slock);
    488 
    489 	if (numcache < numvnodes) {
    490 		numcache++;
    491 		simple_unlock(&namecache_slock);
    492 		ncp = pool_get(&namecache_pool, PR_WAITOK);
    493 		memset(ncp, 0, sizeof(*ncp));
    494 		simple_lock(&namecache_slock);
    495 	} else if ((ncp = TAILQ_FIRST(&nclruhead)) != NULL) {
    496 		cache_remove(ncp);
    497 	} else {
    498 		simple_unlock(&namecache_slock);
    499 		return;
    500 	}
    501 
    502 	/*
    503 	 * Concurrent lookups in the same directory may race for a
    504 	 * cache entry.  if there's a duplicated entry, free it.
    505 	 */
    506 	oncp = cache_lookup_entry(dvp, cnp);
    507 	if (oncp) {
    508 		cache_remove(oncp);
    509 		cache_free(oncp);
    510 	}
    511 	KASSERT(cache_lookup_entry(dvp, cnp) == NULL);
    512 
    513 	/* Grab the vnode we just found. */
    514 	ncp->nc_vp = vp;
    515 	if (vp == NULL) {
    516 		/*
    517 		 * For negative hits, save the ISWHITEOUT flag so we can
    518 		 * restore it later when the cache entry is used again.
    519 		 */
    520 		ncp->nc_flags = cnp->cn_flags & ISWHITEOUT;
    521 	}
    522 	/* Fill in cache info. */
    523 	ncp->nc_dvp = dvp;
    524 	LIST_INSERT_HEAD(&dvp->v_dnclist, ncp, nc_dvlist);
    525 	if (vp)
    526 		LIST_INSERT_HEAD(&vp->v_nclist, ncp, nc_vlist);
    527 	ncp->nc_nlen = cnp->cn_namelen;
    528 	memcpy(ncp->nc_name, cnp->cn_nameptr, (unsigned)ncp->nc_nlen);
    529 	TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
    530 	ncpp = &nchashtbl[NCHASH(cnp, dvp)];
    531 	LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
    532 
    533 	ncp->nc_vhash.le_prev = NULL;
    534 	ncp->nc_vhash.le_next = NULL;
    535 
    536 	/*
    537 	 * Create reverse-cache entries (used in getcwd) for directories.
    538 	 */
    539 	if (vp != NULL &&
    540 	    vp != dvp &&
    541 #ifndef NAMECACHE_ENTER_REVERSE
    542 	    vp->v_type == VDIR &&
    543 #endif
    544 	    (ncp->nc_nlen > 2 ||
    545 	    (ncp->nc_nlen > 1 && ncp->nc_name[1] != '.') ||
    546 	    (/* ncp->nc_nlen > 0 && */ ncp->nc_name[0] != '.'))) {
    547 		nvcpp = &ncvhashtbl[NCVHASH(vp)];
    548 		LIST_INSERT_HEAD(nvcpp, ncp, nc_vhash);
    549 	}
    550 	simple_unlock(&namecache_slock);
    551 }
    552 
    553 /*
    554  * Name cache initialization, from vfs_init() when we are booting
    555  */
    556 void
    557 nchinit(void)
    558 {
    559 
    560 	TAILQ_INIT(&nclruhead);
    561 	nchashtbl =
    562 	    hashinit(desiredvnodes, HASH_LIST, M_CACHE, M_WAITOK, &nchash);
    563 	ncvhashtbl =
    564 #ifdef NAMECACHE_ENTER_REVERSE
    565 	    hashinit(desiredvnodes, HASH_LIST, M_CACHE, M_WAITOK, &ncvhash);
    566 #else
    567 	    hashinit(desiredvnodes/8, HASH_LIST, M_CACHE, M_WAITOK, &ncvhash);
    568 #endif
    569 }
    570 
    571 /*
    572  * Name cache reinitialization, for when the maximum number of vnodes increases.
    573  */
    574 void
    575 nchreinit(void)
    576 {
    577 	struct namecache *ncp;
    578 	struct nchashhead *oldhash1, *hash1;
    579 	struct ncvhashhead *oldhash2, *hash2;
    580 	u_long i, oldmask1, oldmask2, mask1, mask2;
    581 
    582 	hash1 = hashinit(desiredvnodes, HASH_LIST, M_CACHE, M_WAITOK, &mask1);
    583 	hash2 =
    584 #ifdef NAMECACHE_ENTER_REVERSE
    585 	    hashinit(desiredvnodes, HASH_LIST, M_CACHE, M_WAITOK, &mask2);
    586 #else
    587 	    hashinit(desiredvnodes/8, HASH_LIST, M_CACHE, M_WAITOK, &mask2);
    588 #endif
    589 	simple_lock(&namecache_slock);
    590 	oldhash1 = nchashtbl;
    591 	oldmask1 = nchash;
    592 	nchashtbl = hash1;
    593 	nchash = mask1;
    594 	oldhash2 = ncvhashtbl;
    595 	oldmask2 = ncvhash;
    596 	ncvhashtbl = hash2;
    597 	ncvhash = mask2;
    598 	for (i = 0; i <= oldmask1; i++) {
    599 		while ((ncp = LIST_FIRST(&oldhash1[i])) != NULL) {
    600 			LIST_REMOVE(ncp, nc_hash);
    601 			ncp->nc_hash.le_prev = NULL;
    602 		}
    603 	}
    604 	for (i = 0; i <= oldmask2; i++) {
    605 		while ((ncp = LIST_FIRST(&oldhash2[i])) != NULL) {
    606 			LIST_REMOVE(ncp, nc_vhash);
    607 			ncp->nc_vhash.le_prev = NULL;
    608 		}
    609 	}
    610 	simple_unlock(&namecache_slock);
    611 	hashdone(oldhash1, M_CACHE);
    612 	hashdone(oldhash2, M_CACHE);
    613 }
    614 
    615 /*
    616  * Cache flush, a particular vnode; called when a vnode is renamed to
    617  * hide entries that would now be invalid
    618  */
    619 void
    620 cache_purge1(struct vnode *vp, const struct componentname *cnp, int flags)
    621 {
    622 	struct namecache *ncp, *ncnext;
    623 
    624 	simple_lock(&namecache_slock);
    625 	if (flags & PURGE_PARENTS) {
    626 		for (ncp = LIST_FIRST(&vp->v_nclist); ncp != NULL;
    627 		    ncp = ncnext) {
    628 			ncnext = LIST_NEXT(ncp, nc_vlist);
    629 			cache_remove(ncp);
    630 			cache_free(ncp);
    631 		}
    632 	}
    633 	if (flags & PURGE_CHILDREN) {
    634 		for (ncp = LIST_FIRST(&vp->v_dnclist); ncp != NULL;
    635 		    ncp = ncnext) {
    636 			ncnext = LIST_NEXT(ncp, nc_dvlist);
    637 			cache_remove(ncp);
    638 			cache_free(ncp);
    639 		}
    640 	}
    641 	if (cnp != NULL) {
    642 		ncp = cache_lookup_entry(vp, cnp);
    643 		if (ncp) {
    644 			cache_remove(ncp);
    645 			cache_free(ncp);
    646 		}
    647 	}
    648 	simple_unlock(&namecache_slock);
    649 }
    650 
    651 /*
    652  * Cache flush, a whole filesystem; called when filesys is umounted to
    653  * remove entries that would now be invalid.
    654  */
    655 void
    656 cache_purgevfs(struct mount *mp)
    657 {
    658 	struct namecache *ncp, *nxtcp;
    659 
    660 	simple_lock(&namecache_slock);
    661 	for (ncp = TAILQ_FIRST(&nclruhead); ncp != NULL; ncp = nxtcp) {
    662 		nxtcp = TAILQ_NEXT(ncp, nc_lru);
    663 		if (ncp->nc_dvp == NULL || ncp->nc_dvp->v_mount != mp) {
    664 			continue;
    665 		}
    666 		/* Free the resources we had. */
    667 		cache_remove(ncp);
    668 		cache_free(ncp);
    669 	}
    670 	simple_unlock(&namecache_slock);
    671 }
    672 
    673 #ifdef DDB
    674 void
    675 namecache_print(struct vnode *vp, void (*pr)(const char *, ...))
    676 {
    677 	struct vnode *dvp = NULL;
    678 	struct namecache *ncp;
    679 
    680 	TAILQ_FOREACH(ncp, &nclruhead, nc_lru) {
    681 		if (ncp->nc_vp == vp) {
    682 			(*pr)("name %.*s\n", ncp->nc_nlen, ncp->nc_name);
    683 			dvp = ncp->nc_dvp;
    684 		}
    685 	}
    686 	if (dvp == NULL) {
    687 		(*pr)("name not found\n");
    688 		return;
    689 	}
    690 	vp = dvp;
    691 	TAILQ_FOREACH(ncp, &nclruhead, nc_lru) {
    692 		if (ncp->nc_vp == vp) {
    693 			(*pr)("parent %.*s\n", ncp->nc_nlen, ncp->nc_name);
    694 		}
    695 	}
    696 }
    697 #endif
    698