xref: /netbsd-src/sys/kern/vfs_cache.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: vfs_cache.c,v 1.61 2004/06/27 08:50:44 yamt 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.61 2004/06/27 08:50:44 yamt 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 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 (vp == dvp ||
301 		    error || (~cnp->cn_flags & (LOCKPARENT|ISLASTCN)) != 0) {
302 			if ((error = vn_lock(dvp, LK_EXCLUSIVE)) != 0)
303 				return (error);
304 			cnp->cn_flags &= ~PDIRUNLOCK;
305 		}
306 		*vpp = NULL;
307 		return (-1);
308 	}
309 
310 	/* XXXSMP - updating stats without lock; do we care? */
311 	nchstats.ncs_goodhits++;
312 	*vpp = vp;
313 	return (0);
314 
315 remove:
316 	/*
317 	 * Last component and we are renaming or deleting,
318 	 * the cache entry is invalid, or otherwise don't
319 	 * want cache entry to exist.
320 	 */
321 	cache_remove(ncp);
322 	cache_free(ncp);
323 
324 fail_wlock:
325 	simple_unlock(&namecache_slock);
326 fail:
327 	*vpp = NULL;
328 	return (-1);
329 }
330 
331 int
332 cache_lookup_raw(struct vnode *dvp, struct vnode **vpp,
333     struct componentname *cnp)
334 {
335 	struct namecache *ncp;
336 	struct vnode *vp;
337 	int error;
338 
339 	if (!doingcache) {
340 		cnp->cn_flags &= ~MAKEENTRY;
341 		*vpp = NULL;
342 		return (-1);
343 	}
344 
345 	if (cnp->cn_namelen > NCHNAMLEN) {
346 		/* XXXSMP - updating stats without lock; do we care? */
347 		nchstats.ncs_long++;
348 		cnp->cn_flags &= ~MAKEENTRY;
349 		goto fail;
350 	}
351 	simple_lock(&namecache_slock);
352 	ncp = cache_lookup_entry(dvp, cnp);
353 	if (ncp == NULL) {
354 		nchstats.ncs_miss++;
355 		goto fail_wlock;
356 	}
357 	/*
358 	 * Move this slot to end of LRU chain,
359 	 * if not already there.
360 	 */
361 	if (TAILQ_NEXT(ncp, nc_lru) != 0) {
362 		TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
363 		TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
364 	}
365 
366 	vp = ncp->nc_vp;
367 	if (vp == NULL) {
368 		/*
369 		 * Restore the ISWHITEOUT flag saved earlier.
370 		 */
371 		cnp->cn_flags |= ncp->nc_flags;
372 		nchstats.ncs_neghits++;
373 		simple_unlock(&namecache_slock);
374 		return (ENOENT);
375 	}
376 
377 	error = vget(vp, LK_NOWAIT);
378 
379 	/* Release the name cache mutex while we get reference to the vnode */
380 	simple_unlock(&namecache_slock);
381 
382 	if (error) {
383 		KASSERT(error == EBUSY);
384 		/*
385 		 * this vnode is being cleaned out.
386 		 */
387 		nchstats.ncs_falsehits++; /* XXX badhits? */
388 		goto fail;
389 	}
390 
391 	*vpp = vp;
392 
393 	return 0;
394 
395 fail_wlock:
396 	simple_unlock(&namecache_slock);
397 fail:
398 	*vpp = NULL;
399 	return -1;
400 }
401 
402 /*
403  * Scan cache looking for name of directory entry pointing at vp.
404  *
405  * Fill in dvpp.
406  *
407  * If bufp is non-NULL, also place the name in the buffer which starts
408  * at bufp, immediately before *bpp, and move bpp backwards to point
409  * at the start of it.  (Yes, this is a little baroque, but it's done
410  * this way to cater to the whims of getcwd).
411  *
412  * Returns 0 on success, -1 on cache miss, positive errno on failure.
413  */
414 int
415 cache_revlookup(struct vnode *vp, struct vnode **dvpp, char **bpp, char *bufp)
416 {
417 	struct namecache *ncp;
418 	struct vnode *dvp;
419 	struct ncvhashhead *nvcpp;
420 	char *bp;
421 
422 	if (!doingcache)
423 		goto out;
424 
425 	nvcpp = &ncvhashtbl[NCVHASH(vp)];
426 
427 	simple_lock(&namecache_slock);
428 	LIST_FOREACH(ncp, nvcpp, nc_vhash) {
429 		if (ncp->nc_vp == vp &&
430 		    (dvp = ncp->nc_dvp) != NULL &&
431 		    dvp != vp) { 		/* avoid pesky . entries.. */
432 
433 #ifdef DIAGNOSTIC
434 			if (ncp->nc_nlen == 1 &&
435 			    ncp->nc_name[0] == '.')
436 				panic("cache_revlookup: found entry for .");
437 
438 			if (ncp->nc_nlen == 2 &&
439 			    ncp->nc_name[0] == '.' &&
440 			    ncp->nc_name[1] == '.')
441 				panic("cache_revlookup: found entry for ..");
442 #endif
443 			nchstats.ncs_revhits++;
444 
445 			if (bufp) {
446 				bp = *bpp;
447 				bp -= ncp->nc_nlen;
448 				if (bp <= bufp) {
449 					*dvpp = NULL;
450 					simple_unlock(&namecache_slock);
451 					return (ERANGE);
452 				}
453 				memcpy(bp, ncp->nc_name, ncp->nc_nlen);
454 				*bpp = bp;
455 			}
456 
457 			/* XXX MP: how do we know dvp won't evaporate? */
458 			*dvpp = dvp;
459 			simple_unlock(&namecache_slock);
460 			return (0);
461 		}
462 	}
463 	nchstats.ncs_revmiss++;
464 	simple_unlock(&namecache_slock);
465  out:
466 	*dvpp = NULL;
467 	return (-1);
468 }
469 
470 /*
471  * Add an entry to the cache
472  */
473 void
474 cache_enter(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
475 {
476 	struct namecache *ncp;
477 	struct namecache *oncp;
478 	struct nchashhead *ncpp;
479 	struct ncvhashhead *nvcpp;
480 
481 #ifdef DIAGNOSTIC
482 	if (cnp->cn_namelen > NCHNAMLEN)
483 		panic("cache_enter: name too long");
484 #endif
485 	if (!doingcache)
486 		return;
487 	/*
488 	 * Free the cache slot at head of lru chain.
489 	 */
490 	simple_lock(&namecache_slock);
491 
492 	if (numcache < numvnodes) {
493 		numcache++;
494 		simple_unlock(&namecache_slock);
495 		ncp = pool_get(&namecache_pool, PR_WAITOK);
496 		memset(ncp, 0, sizeof(*ncp));
497 		simple_lock(&namecache_slock);
498 	} else if ((ncp = TAILQ_FIRST(&nclruhead)) != NULL) {
499 		cache_remove(ncp);
500 	} else {
501 		simple_unlock(&namecache_slock);
502 		return;
503 	}
504 
505 	/*
506 	 * Concurrent lookups in the same directory may race for a
507 	 * cache entry.  if there's a duplicated entry, free it.
508 	 */
509 	oncp = cache_lookup_entry(dvp, cnp);
510 	if (oncp) {
511 		cache_remove(oncp);
512 		cache_free(oncp);
513 	}
514 	KASSERT(cache_lookup_entry(dvp, cnp) == NULL);
515 
516 	/* Grab the vnode we just found. */
517 	ncp->nc_vp = vp;
518 	if (vp == NULL) {
519 		/*
520 		 * For negative hits, save the ISWHITEOUT flag so we can
521 		 * restore it later when the cache entry is used again.
522 		 */
523 		ncp->nc_flags = cnp->cn_flags & ISWHITEOUT;
524 	}
525 	/* Fill in cache info. */
526 	ncp->nc_dvp = dvp;
527 	LIST_INSERT_HEAD(&dvp->v_dnclist, ncp, nc_dvlist);
528 	if (vp)
529 		LIST_INSERT_HEAD(&vp->v_nclist, ncp, nc_vlist);
530 	ncp->nc_nlen = cnp->cn_namelen;
531 	memcpy(ncp->nc_name, cnp->cn_nameptr, (unsigned)ncp->nc_nlen);
532 	TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
533 	ncpp = &nchashtbl[NCHASH(cnp, dvp)];
534 	LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
535 
536 	ncp->nc_vhash.le_prev = NULL;
537 	ncp->nc_vhash.le_next = NULL;
538 
539 	/*
540 	 * Create reverse-cache entries (used in getcwd) for directories.
541 	 */
542 	if (vp != NULL &&
543 	    vp != dvp &&
544 #ifndef NAMECACHE_ENTER_REVERSE
545 	    vp->v_type == VDIR &&
546 #endif
547 	    (ncp->nc_nlen > 2 ||
548 	    (ncp->nc_nlen > 1 && ncp->nc_name[1] != '.') ||
549 	    (/* ncp->nc_nlen > 0 && */ ncp->nc_name[0] != '.'))) {
550 		nvcpp = &ncvhashtbl[NCVHASH(vp)];
551 		LIST_INSERT_HEAD(nvcpp, ncp, nc_vhash);
552 	}
553 	simple_unlock(&namecache_slock);
554 }
555 
556 /*
557  * Name cache initialization, from vfs_init() when we are booting
558  */
559 void
560 nchinit(void)
561 {
562 
563 	TAILQ_INIT(&nclruhead);
564 	nchashtbl =
565 	    hashinit(desiredvnodes, HASH_LIST, M_CACHE, M_WAITOK, &nchash);
566 	ncvhashtbl =
567 #ifdef NAMECACHE_ENTER_REVERSE
568 	    hashinit(desiredvnodes, HASH_LIST, M_CACHE, M_WAITOK, &ncvhash);
569 #else
570 	    hashinit(desiredvnodes/8, HASH_LIST, M_CACHE, M_WAITOK, &ncvhash);
571 #endif
572 }
573 
574 /*
575  * Name cache reinitialization, for when the maximum number of vnodes increases.
576  */
577 void
578 nchreinit(void)
579 {
580 	struct namecache *ncp;
581 	struct nchashhead *oldhash1, *hash1;
582 	struct ncvhashhead *oldhash2, *hash2;
583 	u_long i, oldmask1, oldmask2, mask1, mask2;
584 
585 	hash1 = hashinit(desiredvnodes, HASH_LIST, M_CACHE, M_WAITOK, &mask1);
586 	hash2 =
587 #ifdef NAMECACHE_ENTER_REVERSE
588 	    hashinit(desiredvnodes, HASH_LIST, M_CACHE, M_WAITOK, &mask2);
589 #else
590 	    hashinit(desiredvnodes/8, HASH_LIST, M_CACHE, M_WAITOK, &mask2);
591 #endif
592 	simple_lock(&namecache_slock);
593 	oldhash1 = nchashtbl;
594 	oldmask1 = nchash;
595 	nchashtbl = hash1;
596 	nchash = mask1;
597 	oldhash2 = ncvhashtbl;
598 	oldmask2 = ncvhash;
599 	ncvhashtbl = hash2;
600 	ncvhash = mask2;
601 	for (i = 0; i <= oldmask1; i++) {
602 		while ((ncp = LIST_FIRST(&oldhash1[i])) != NULL) {
603 			LIST_REMOVE(ncp, nc_hash);
604 			ncp->nc_hash.le_prev = NULL;
605 		}
606 	}
607 	for (i = 0; i <= oldmask2; i++) {
608 		while ((ncp = LIST_FIRST(&oldhash2[i])) != NULL) {
609 			LIST_REMOVE(ncp, nc_vhash);
610 			ncp->nc_vhash.le_prev = NULL;
611 		}
612 	}
613 	simple_unlock(&namecache_slock);
614 	hashdone(oldhash1, M_CACHE);
615 	hashdone(oldhash2, M_CACHE);
616 }
617 
618 /*
619  * Cache flush, a particular vnode; called when a vnode is renamed to
620  * hide entries that would now be invalid
621  */
622 void
623 cache_purge1(struct vnode *vp, const struct componentname *cnp, int flags)
624 {
625 	struct namecache *ncp, *ncnext;
626 
627 	simple_lock(&namecache_slock);
628 	if (flags & PURGE_PARENTS) {
629 		for (ncp = LIST_FIRST(&vp->v_nclist); ncp != NULL;
630 		    ncp = ncnext) {
631 			ncnext = LIST_NEXT(ncp, nc_vlist);
632 			cache_remove(ncp);
633 			cache_free(ncp);
634 		}
635 	}
636 	if (flags & PURGE_CHILDREN) {
637 		for (ncp = LIST_FIRST(&vp->v_dnclist); ncp != NULL;
638 		    ncp = ncnext) {
639 			ncnext = LIST_NEXT(ncp, nc_dvlist);
640 			cache_remove(ncp);
641 			cache_free(ncp);
642 		}
643 	}
644 	if (cnp != NULL) {
645 		ncp = cache_lookup_entry(vp, cnp);
646 		if (ncp) {
647 			cache_remove(ncp);
648 			cache_free(ncp);
649 		}
650 	}
651 	simple_unlock(&namecache_slock);
652 }
653 
654 /*
655  * Cache flush, a whole filesystem; called when filesys is umounted to
656  * remove entries that would now be invalid.
657  */
658 void
659 cache_purgevfs(struct mount *mp)
660 {
661 	struct namecache *ncp, *nxtcp;
662 
663 	simple_lock(&namecache_slock);
664 	for (ncp = TAILQ_FIRST(&nclruhead); ncp != NULL; ncp = nxtcp) {
665 		nxtcp = TAILQ_NEXT(ncp, nc_lru);
666 		if (ncp->nc_dvp == NULL || ncp->nc_dvp->v_mount != mp) {
667 			continue;
668 		}
669 		/* Free the resources we had. */
670 		cache_remove(ncp);
671 		cache_free(ncp);
672 	}
673 	simple_unlock(&namecache_slock);
674 }
675 
676 #ifdef DDB
677 void
678 namecache_print(struct vnode *vp, void (*pr)(const char *, ...))
679 {
680 	struct vnode *dvp = NULL;
681 	struct namecache *ncp;
682 
683 	TAILQ_FOREACH(ncp, &nclruhead, nc_lru) {
684 		if (ncp->nc_vp == vp) {
685 			(*pr)("name %.*s\n", ncp->nc_nlen, ncp->nc_name);
686 			dvp = ncp->nc_dvp;
687 		}
688 	}
689 	if (dvp == NULL) {
690 		(*pr)("name not found\n");
691 		return;
692 	}
693 	vp = dvp;
694 	TAILQ_FOREACH(ncp, &nclruhead, nc_lru) {
695 		if (ncp->nc_vp == vp) {
696 			(*pr)("parent %.*s\n", ncp->nc_nlen, ncp->nc_name);
697 		}
698 	}
699 }
700 #endif
701