xref: /netbsd-src/sys/kern/vfs_cache.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: vfs_cache.c,v 1.72 2007/11/11 23:22:25 matt 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.72 2007/11/11 23:22:25 matt 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 	TAILQ_HEAD_INITIALIZER(nclruhead);
88 struct	nchstats nchstats;		/* cache effectiveness statistics */
89 
90 static pool_cache_t namecache_cache;
91 
92 MALLOC_DEFINE(M_CACHE, "namecache", "Dynamically allocated cache entries");
93 
94 int doingcache = 1;			/* 1 => enable the cache */
95 
96 /* A single lock to protect cache insertion, removal and lookup */
97 static kmutex_t namecache_lock;
98 
99 static void cache_remove(struct namecache *);
100 static void cache_free(struct namecache *);
101 static inline struct namecache *cache_lookup_entry(
102     const struct vnode *, const struct componentname *);
103 
104 static void
105 cache_remove(struct namecache *ncp)
106 {
107 
108 	KASSERT(mutex_owned(&namecache_lock));
109 
110 	ncp->nc_dvp = NULL;
111 	ncp->nc_vp = NULL;
112 
113 	TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
114 	if (ncp->nc_hash.le_prev != NULL) {
115 		LIST_REMOVE(ncp, nc_hash);
116 		ncp->nc_hash.le_prev = NULL;
117 	}
118 	if (ncp->nc_vhash.le_prev != NULL) {
119 		LIST_REMOVE(ncp, nc_vhash);
120 		ncp->nc_vhash.le_prev = NULL;
121 	}
122 	if (ncp->nc_vlist.le_prev != NULL) {
123 		LIST_REMOVE(ncp, nc_vlist);
124 		ncp->nc_vlist.le_prev = NULL;
125 	}
126 	if (ncp->nc_dvlist.le_prev != NULL) {
127 		LIST_REMOVE(ncp, nc_dvlist);
128 		ncp->nc_dvlist.le_prev = NULL;
129 	}
130 }
131 
132 static void
133 cache_free(struct namecache *ncp)
134 {
135 
136 	pool_cache_put(namecache_cache, ncp);
137 	numcache--;
138 }
139 
140 static inline struct namecache *
141 cache_lookup_entry(const struct vnode *dvp, const struct componentname *cnp)
142 {
143 	struct nchashhead *ncpp;
144 	struct namecache *ncp;
145 
146 	KASSERT(mutex_owned(&namecache_lock));
147 
148 	ncpp = &nchashtbl[NCHASH(cnp, dvp)];
149 
150 	LIST_FOREACH(ncp, ncpp, nc_hash) {
151 		if (ncp->nc_dvp == dvp &&
152 		    ncp->nc_nlen == cnp->cn_namelen &&
153 		    !memcmp(ncp->nc_name, cnp->cn_nameptr, (u_int)ncp->nc_nlen))
154 			break;
155 	}
156 
157 	return ncp;
158 }
159 
160 /*
161  * Look for a the name in the cache. We don't do this
162  * if the segment name is long, simply so the cache can avoid
163  * holding long names (which would either waste space, or
164  * add greatly to the complexity).
165  *
166  * Lookup is called with ni_dvp pointing to the directory to search,
167  * ni_ptr pointing to the name of the entry being sought, ni_namelen
168  * tells the length of the name, and ni_hash contains a hash of
169  * the name. If the lookup succeeds, the vnode is locked, stored in ni_vp
170  * and a status of zero is returned. If the locking fails for whatever
171  * reason, the vnode is unlocked and the error is returned to caller.
172  * If the lookup determines that the name does not exist (negative cacheing),
173  * a status of ENOENT is returned. If the lookup fails, a status of -1
174  * is returned.
175  */
176 int
177 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
178 {
179 	struct namecache *ncp;
180 	struct vnode *vp;
181 	int error;
182 
183 	if (!doingcache) {
184 		cnp->cn_flags &= ~MAKEENTRY;
185 		*vpp = NULL;
186 		return (-1);
187 	}
188 
189 	if (cnp->cn_namelen > NCHNAMLEN) {
190 		/* Unlocked, but only for stats. */
191 		nchstats.ncs_long++;
192 		cnp->cn_flags &= ~MAKEENTRY;
193 		goto fail;
194 	}
195 	mutex_enter(&namecache_lock);
196 	ncp = cache_lookup_entry(dvp, cnp);
197 	if (ncp == NULL) {
198 		nchstats.ncs_miss++;
199 		goto fail_wlock;
200 	}
201 	if ((cnp->cn_flags & MAKEENTRY) == 0) {
202 		nchstats.ncs_badhits++;
203 		goto remove;
204 	} else if (ncp->nc_vp == NULL) {
205 		/*
206 		 * Restore the ISWHITEOUT flag saved earlier.
207 		 */
208 		cnp->cn_flags |= ncp->nc_flags;
209 		if (cnp->cn_nameiop != CREATE ||
210 		    (cnp->cn_flags & ISLASTCN) == 0) {
211 			nchstats.ncs_neghits++;
212 			/*
213 			 * Move this slot to end of LRU chain,
214 			 * if not already there.
215 			 */
216 			if (TAILQ_NEXT(ncp, nc_lru) != 0) {
217 				TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
218 				TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
219 			}
220 			mutex_exit(&namecache_lock);
221 			return (ENOENT);
222 		} else {
223 			nchstats.ncs_badhits++;
224 			goto remove;
225 		}
226 	}
227 
228 	vp = ncp->nc_vp;
229 
230 	/*
231 	 * Move this slot to end of LRU chain, if not already there.
232 	 */
233 	if (TAILQ_NEXT(ncp, nc_lru) != 0) {
234 		TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
235 		TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
236 	}
237 
238 	error = vget(vp, LK_NOWAIT);
239 
240 	/* Release the name cache mutex while we get reference to the vnode */
241 	mutex_exit(&namecache_lock);
242 
243 #ifdef DEBUG
244 	/*
245 	 * since we released namecache_lock,
246 	 * we can't use this pointer any more.
247 	 */
248 	ncp = NULL;
249 #endif /* DEBUG */
250 
251 	if (error) {
252 		KASSERT(error == EBUSY);
253 		/*
254 		 * this vnode is being cleaned out.
255 		 */
256 		nchstats.ncs_falsehits++; /* XXX badhits? */
257 		goto fail;
258 	}
259 
260 	if (vp == dvp) {	/* lookup on "." */
261 		error = 0;
262 	} else if (cnp->cn_flags & ISDOTDOT) {
263 		VOP_UNLOCK(dvp, 0);
264 		error = vn_lock(vp, LK_EXCLUSIVE);
265 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
266 	} else {
267 		error = vn_lock(vp, LK_EXCLUSIVE);
268 	}
269 
270 	/*
271 	 * Check that the lock succeeded.
272 	 */
273 	if (error) {
274 		/* Unlocked, but only for stats. */
275 		nchstats.ncs_badhits++;
276 		*vpp = NULL;
277 		return (-1);
278 	}
279 
280 	/* Unlocked, but only for stats. */
281 	nchstats.ncs_goodhits++;
282 	*vpp = vp;
283 	return (0);
284 
285 remove:
286 	/*
287 	 * Last component and we are renaming or deleting,
288 	 * the cache entry is invalid, or otherwise don't
289 	 * want cache entry to exist.
290 	 */
291 	cache_remove(ncp);
292 	cache_free(ncp);
293 
294 fail_wlock:
295 	mutex_exit(&namecache_lock);
296 fail:
297 	*vpp = NULL;
298 	return (-1);
299 }
300 
301 int
302 cache_lookup_raw(struct vnode *dvp, struct vnode **vpp,
303     struct componentname *cnp)
304 {
305 	struct namecache *ncp;
306 	struct vnode *vp;
307 	int error;
308 
309 	if (!doingcache) {
310 		cnp->cn_flags &= ~MAKEENTRY;
311 		*vpp = NULL;
312 		return (-1);
313 	}
314 
315 	if (cnp->cn_namelen > NCHNAMLEN) {
316 		/* Unlocked, but only for stats. */
317 		nchstats.ncs_long++;
318 		cnp->cn_flags &= ~MAKEENTRY;
319 		goto fail;
320 	}
321 	mutex_enter(&namecache_lock);
322 	ncp = cache_lookup_entry(dvp, cnp);
323 	if (ncp == NULL) {
324 		nchstats.ncs_miss++;
325 		goto fail_wlock;
326 	}
327 	/*
328 	 * Move this slot to end of LRU chain,
329 	 * if not already there.
330 	 */
331 	if (TAILQ_NEXT(ncp, nc_lru) != 0) {
332 		TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
333 		TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
334 	}
335 
336 	vp = ncp->nc_vp;
337 	if (vp == NULL) {
338 		/*
339 		 * Restore the ISWHITEOUT flag saved earlier.
340 		 */
341 		cnp->cn_flags |= ncp->nc_flags;
342 		nchstats.ncs_neghits++;
343 		mutex_exit(&namecache_lock);
344 		return (ENOENT);
345 	}
346 
347 	error = vget(vp, LK_NOWAIT);
348 
349 	/* Release the name cache mutex while we get reference to the vnode */
350 	mutex_exit(&namecache_lock);
351 
352 	if (error) {
353 		KASSERT(error == EBUSY);
354 		/*
355 		 * this vnode is being cleaned out.
356 		 */
357 		nchstats.ncs_falsehits++; /* XXX badhits? */
358 		goto fail;
359 	}
360 
361 	*vpp = vp;
362 
363 	return 0;
364 
365 fail_wlock:
366 	mutex_exit(&namecache_lock);
367 fail:
368 	*vpp = NULL;
369 	return -1;
370 }
371 
372 /*
373  * Scan cache looking for name of directory entry pointing at vp.
374  *
375  * Fill in dvpp.
376  *
377  * If bufp is non-NULL, also place the name in the buffer which starts
378  * at bufp, immediately before *bpp, and move bpp backwards to point
379  * at the start of it.  (Yes, this is a little baroque, but it's done
380  * this way to cater to the whims of getcwd).
381  *
382  * Returns 0 on success, -1 on cache miss, positive errno on failure.
383  */
384 int
385 cache_revlookup(struct vnode *vp, struct vnode **dvpp, char **bpp, char *bufp)
386 {
387 	struct namecache *ncp;
388 	struct vnode *dvp;
389 	struct ncvhashhead *nvcpp;
390 	char *bp;
391 
392 	if (!doingcache)
393 		goto out;
394 
395 	nvcpp = &ncvhashtbl[NCVHASH(vp)];
396 
397 	mutex_enter(&namecache_lock);
398 	LIST_FOREACH(ncp, nvcpp, nc_vhash) {
399 		if (ncp->nc_vp == vp &&
400 		    (dvp = ncp->nc_dvp) != NULL &&
401 		    dvp != vp) { 		/* avoid pesky . entries.. */
402 
403 #ifdef DIAGNOSTIC
404 			if (ncp->nc_nlen == 1 &&
405 			    ncp->nc_name[0] == '.')
406 				panic("cache_revlookup: found entry for .");
407 
408 			if (ncp->nc_nlen == 2 &&
409 			    ncp->nc_name[0] == '.' &&
410 			    ncp->nc_name[1] == '.')
411 				panic("cache_revlookup: found entry for ..");
412 #endif
413 			nchstats.ncs_revhits++;
414 
415 			if (bufp) {
416 				bp = *bpp;
417 				bp -= ncp->nc_nlen;
418 				if (bp <= bufp) {
419 					*dvpp = NULL;
420 					mutex_exit(&namecache_lock);
421 					return (ERANGE);
422 				}
423 				memcpy(bp, ncp->nc_name, ncp->nc_nlen);
424 				*bpp = bp;
425 			}
426 
427 			/* XXX MP: how do we know dvp won't evaporate? */
428 			*dvpp = dvp;
429 			mutex_exit(&namecache_lock);
430 			return (0);
431 		}
432 	}
433 	nchstats.ncs_revmiss++;
434 	mutex_exit(&namecache_lock);
435  out:
436 	*dvpp = NULL;
437 	return (-1);
438 }
439 
440 /*
441  * Add an entry to the cache
442  */
443 void
444 cache_enter(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
445 {
446 	struct namecache *ncp;
447 	struct namecache *oncp;
448 	struct nchashhead *ncpp;
449 	struct ncvhashhead *nvcpp;
450 
451 #ifdef DIAGNOSTIC
452 	if (cnp->cn_namelen > NCHNAMLEN)
453 		panic("cache_enter: name too long");
454 #endif
455 	if (!doingcache)
456 		return;
457 	/*
458 	 * Free the cache slot at head of lru chain.
459 	 */
460 	mutex_enter(&namecache_lock);
461 
462 	if (numcache < numvnodes) {
463 		numcache++;
464 		mutex_exit(&namecache_lock);
465 		ncp = pool_cache_get(namecache_cache, PR_WAITOK);
466 		memset(ncp, 0, sizeof(*ncp));
467 		mutex_enter(&namecache_lock);
468 	} else if ((ncp = TAILQ_FIRST(&nclruhead)) != NULL) {
469 		cache_remove(ncp);
470 	} else {
471 		mutex_exit(&namecache_lock);
472 		return;
473 	}
474 
475 	/*
476 	 * Concurrent lookups in the same directory may race for a
477 	 * cache entry.  if there's a duplicated entry, free it.
478 	 */
479 	oncp = cache_lookup_entry(dvp, cnp);
480 	if (oncp) {
481 		cache_remove(oncp);
482 		cache_free(oncp);
483 	}
484 	KASSERT(cache_lookup_entry(dvp, cnp) == NULL);
485 
486 	/* Grab the vnode we just found. */
487 	ncp->nc_vp = vp;
488 	if (vp == NULL) {
489 		/*
490 		 * For negative hits, save the ISWHITEOUT flag so we can
491 		 * restore it later when the cache entry is used again.
492 		 */
493 		ncp->nc_flags = cnp->cn_flags & ISWHITEOUT;
494 	}
495 	/* Fill in cache info. */
496 	ncp->nc_dvp = dvp;
497 	LIST_INSERT_HEAD(&dvp->v_dnclist, ncp, nc_dvlist);
498 	if (vp)
499 		LIST_INSERT_HEAD(&vp->v_nclist, ncp, nc_vlist);
500 	ncp->nc_nlen = cnp->cn_namelen;
501 	memcpy(ncp->nc_name, cnp->cn_nameptr, (unsigned)ncp->nc_nlen);
502 	TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
503 	ncpp = &nchashtbl[NCHASH(cnp, dvp)];
504 	LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
505 
506 	ncp->nc_vhash.le_prev = NULL;
507 	ncp->nc_vhash.le_next = NULL;
508 
509 	/*
510 	 * Create reverse-cache entries (used in getcwd) for directories.
511 	 * (and in linux procfs exe node)
512 	 */
513 	if (vp != NULL &&
514 	    vp != dvp &&
515 #ifndef NAMECACHE_ENTER_REVERSE
516 	    vp->v_type == VDIR &&
517 #endif
518 	    (ncp->nc_nlen > 2 ||
519 	    (ncp->nc_nlen > 1 && ncp->nc_name[1] != '.') ||
520 	    (/* ncp->nc_nlen > 0 && */ ncp->nc_name[0] != '.'))) {
521 		nvcpp = &ncvhashtbl[NCVHASH(vp)];
522 		LIST_INSERT_HEAD(nvcpp, ncp, nc_vhash);
523 	}
524 	mutex_exit(&namecache_lock);
525 }
526 
527 /*
528  * Name cache initialization, from vfs_init() when we are booting
529  */
530 void
531 nchinit(void)
532 {
533 
534 	namecache_cache = pool_cache_init(sizeof(struct namecache), 0, 0, 0,
535 	    "ncachepl", NULL, IPL_NONE, NULL, NULL, NULL);
536 	KASSERT(namecache_cache != NULL);
537 
538 	mutex_init(&namecache_lock, MUTEX_DEFAULT, IPL_NONE);
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