Home | History | Annotate | Line # | Download | only in kern
vfs_cache.c revision 1.73
      1  1.73        ad /*	$NetBSD: vfs_cache.c,v 1.73 2008/04/11 15:25:24 ad Exp $	*/
      2  1.73        ad 
      3  1.73        ad /*-
      4  1.73        ad  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  1.73        ad  * All rights reserved.
      6  1.73        ad  *
      7  1.73        ad  * Redistribution and use in source and binary forms, with or without
      8  1.73        ad  * modification, are permitted provided that the following conditions
      9  1.73        ad  * are met:
     10  1.73        ad  * 1. Redistributions of source code must retain the above copyright
     11  1.73        ad  *    notice, this list of conditions and the following disclaimer.
     12  1.73        ad  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.73        ad  *    notice, this list of conditions and the following disclaimer in the
     14  1.73        ad  *    documentation and/or other materials provided with the distribution.
     15  1.73        ad  * 3. All advertising materials mentioning features or use of this software
     16  1.73        ad  *    must display the following acknowledgement:
     17  1.73        ad  *	This product includes software developed by the NetBSD
     18  1.73        ad  *	Foundation, Inc. and its contributors.
     19  1.73        ad  * 4. Neither the name of The NetBSD Foundation nor the names of its
     20  1.73        ad  *    contributors may be used to endorse or promote products derived
     21  1.73        ad  *    from this software without specific prior written permission.
     22  1.73        ad  *
     23  1.73        ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     24  1.73        ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  1.73        ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  1.73        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     27  1.73        ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  1.73        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  1.73        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  1.73        ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  1.73        ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  1.73        ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  1.73        ad  * POSSIBILITY OF SUCH DAMAGE.
     34  1.73        ad  */
     35   1.6       cgd 
     36   1.1       cgd /*
     37   1.5   mycroft  * Copyright (c) 1989, 1993
     38   1.5   mycroft  *	The Regents of the University of California.  All rights reserved.
     39   1.1       cgd  *
     40   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     41   1.1       cgd  * modification, are permitted provided that the following conditions
     42   1.1       cgd  * are met:
     43   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     44   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     45   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     46   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     47   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     48  1.51       agc  * 3. Neither the name of the University nor the names of its contributors
     49   1.1       cgd  *    may be used to endorse or promote products derived from this software
     50   1.1       cgd  *    without specific prior written permission.
     51   1.1       cgd  *
     52   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     53   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     56   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62   1.1       cgd  * SUCH DAMAGE.
     63   1.1       cgd  *
     64  1.10   mycroft  *	@(#)vfs_cache.c	8.3 (Berkeley) 8/22/94
     65   1.1       cgd  */
     66  1.32     lukem 
     67  1.32     lukem #include <sys/cdefs.h>
     68  1.73        ad __KERNEL_RCSID(0, "$NetBSD: vfs_cache.c,v 1.73 2008/04/11 15:25:24 ad Exp $");
     69   1.1       cgd 
     70  1.28       chs #include "opt_ddb.h"
     71  1.29      fvdl #include "opt_revcache.h"
     72  1.28       chs 
     73   1.4   mycroft #include <sys/param.h>
     74   1.4   mycroft #include <sys/systm.h>
     75   1.4   mycroft #include <sys/time.h>
     76   1.4   mycroft #include <sys/mount.h>
     77   1.4   mycroft #include <sys/vnode.h>
     78   1.4   mycroft #include <sys/namei.h>
     79   1.4   mycroft #include <sys/errno.h>
     80   1.4   mycroft #include <sys/malloc.h>
     81  1.18   thorpej #include <sys/pool.h>
     82  1.68        ad #include <sys/mutex.h>
     83  1.73        ad #include <sys/atomic.h>
     84  1.73        ad #include <sys/kthread.h>
     85  1.73        ad #include <sys/kernel.h>
     86  1.73        ad #include <sys/cpu.h>
     87  1.73        ad #include <sys/evcnt.h>
     88   1.1       cgd 
     89  1.66  christos #define NAMECACHE_ENTER_REVERSE
     90   1.1       cgd /*
     91   1.1       cgd  * Name caching works as follows:
     92   1.1       cgd  *
     93   1.1       cgd  * Names found by directory scans are retained in a cache
     94   1.1       cgd  * for future reference.  It is managed LRU, so frequently
     95   1.1       cgd  * used names will hang around.  Cache is indexed by hash value
     96  1.20  jdolecek  * obtained from (dvp, name) where dvp refers to the directory
     97   1.1       cgd  * containing name.
     98   1.1       cgd  *
     99   1.1       cgd  * For simplicity (and economy of storage), names longer than
    100   1.1       cgd  * a maximum length of NCHNAMLEN are not cached; they occur
    101   1.1       cgd  * infrequently in any case, and are almost never of interest.
    102   1.1       cgd  *
    103   1.1       cgd  * Upon reaching the last segment of a path, if the reference
    104   1.1       cgd  * is for DELETE, or NOCACHE is set (rewrite), and the
    105   1.1       cgd  * name is located in the cache, it will be dropped.
    106  1.20  jdolecek  * The entry is dropped also when it was not possible to lock
    107  1.20  jdolecek  * the cached vnode, either because vget() failed or the generation
    108  1.20  jdolecek  * number has changed while waiting for the lock.
    109   1.1       cgd  */
    110   1.1       cgd 
    111   1.1       cgd /*
    112   1.1       cgd  * Structures associated with name cacheing.
    113   1.1       cgd  */
    114   1.9   mycroft LIST_HEAD(nchashhead, namecache) *nchashtbl;
    115   1.1       cgd u_long	nchash;				/* size of hash table - 1 */
    116  1.49      yamt #define	NCHASH(cnp, dvp)	\
    117  1.49      yamt 	(((cnp)->cn_hash ^ ((uintptr_t)(dvp) >> 3)) & nchash)
    118  1.19  sommerfe 
    119  1.19  sommerfe LIST_HEAD(ncvhashhead, namecache) *ncvhashtbl;
    120  1.19  sommerfe u_long	ncvhash;			/* size of hash table - 1 */
    121  1.48      yamt #define	NCVHASH(vp)		(((uintptr_t)(vp) >> 3) & ncvhash)
    122  1.19  sommerfe 
    123  1.73        ad long	numcache;			/* number of cache entries allocated */
    124  1.73        ad static u_int	cache_gcpend;		/* number of entries pending GC */
    125  1.73        ad static void	*cache_gcqueue;		/* garbage collection queue */
    126  1.73        ad 
    127  1.72      matt TAILQ_HEAD(, namecache) nclruhead =		/* LRU chain */
    128  1.72      matt 	TAILQ_HEAD_INITIALIZER(nclruhead);
    129  1.73        ad #define	COUNT(x)	nchstats.x++
    130   1.1       cgd struct	nchstats nchstats;		/* cache effectiveness statistics */
    131   1.1       cgd 
    132  1.71        ad static pool_cache_t namecache_cache;
    133  1.38   thorpej 
    134  1.38   thorpej MALLOC_DEFINE(M_CACHE, "namecache", "Dynamically allocated cache entries");
    135  1.18   thorpej 
    136  1.73        ad int cache_lowat = 95;
    137  1.73        ad int cache_hiwat = 98;
    138  1.73        ad int cache_hottime = 5;			/* number of seconds */
    139   1.7    chopps int doingcache = 1;			/* 1 => enable the cache */
    140   1.1       cgd 
    141  1.73        ad static struct evcnt cache_ev_scan;
    142  1.73        ad static struct evcnt cache_ev_gc;
    143  1.73        ad static struct evcnt cache_ev_over;
    144  1.73        ad static struct evcnt cache_ev_under;
    145  1.73        ad static struct evcnt cache_ev_forced;
    146  1.73        ad 
    147  1.73        ad /* A single lock to serialize modifications. */
    148  1.73        ad static kmutex_t *namecache_lock;
    149  1.39        pk 
    150  1.73        ad static void cache_invalidate(struct namecache *);
    151  1.63     perry static inline struct namecache *cache_lookup_entry(
    152  1.55      yamt     const struct vnode *, const struct componentname *);
    153  1.73        ad static void cache_thread(void *);
    154  1.73        ad static void cache_invalidate(struct namecache *);
    155  1.73        ad static void cache_disassociate(struct namecache *);
    156  1.73        ad static void cache_reclaim(void);
    157  1.73        ad static int cache_ctor(void *, void *, int);
    158  1.73        ad static void cache_dtor(void *, void *);
    159  1.46      yamt 
    160  1.73        ad /*
    161  1.73        ad  * Invalidate a cache entry and enqueue it for garbage collection.
    162  1.73        ad  */
    163  1.46      yamt static void
    164  1.73        ad cache_invalidate(struct namecache *ncp)
    165  1.46      yamt {
    166  1.73        ad 	void *head;
    167  1.46      yamt 
    168  1.73        ad 	KASSERT(mutex_owned(&ncp->nc_lock));
    169  1.46      yamt 
    170  1.73        ad 	if (ncp->nc_dvp != NULL) {
    171  1.73        ad 		ncp->nc_vp = NULL;
    172  1.73        ad 		ncp->nc_dvp = NULL;
    173  1.73        ad 		do {
    174  1.73        ad 			head = cache_gcqueue;
    175  1.73        ad 			ncp->nc_gcqueue = head;
    176  1.73        ad 		} while (atomic_cas_ptr(&cache_gcqueue, head, ncp) != head);
    177  1.73        ad 		atomic_inc_uint(&cache_gcpend);
    178  1.73        ad 	}
    179  1.73        ad }
    180  1.46      yamt 
    181  1.73        ad /*
    182  1.73        ad  * Disassociate a namecache entry from any vnodes it is attached to,
    183  1.73        ad  * and remove from the global LRU list.
    184  1.73        ad  */
    185  1.73        ad static void
    186  1.73        ad cache_disassociate(struct namecache *ncp)
    187  1.73        ad {
    188  1.73        ad 
    189  1.73        ad 	KASSERT(mutex_owned(namecache_lock));
    190  1.73        ad 	KASSERT(ncp->nc_dvp == NULL);
    191  1.73        ad 
    192  1.73        ad 	if (ncp->nc_lru.tqe_prev != NULL) {
    193  1.73        ad 		TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
    194  1.73        ad 		ncp->nc_lru.tqe_prev = NULL;
    195  1.46      yamt 	}
    196  1.46      yamt 	if (ncp->nc_vhash.le_prev != NULL) {
    197  1.46      yamt 		LIST_REMOVE(ncp, nc_vhash);
    198  1.46      yamt 		ncp->nc_vhash.le_prev = NULL;
    199  1.46      yamt 	}
    200  1.46      yamt 	if (ncp->nc_vlist.le_prev != NULL) {
    201  1.46      yamt 		LIST_REMOVE(ncp, nc_vlist);
    202  1.46      yamt 		ncp->nc_vlist.le_prev = NULL;
    203  1.46      yamt 	}
    204  1.46      yamt 	if (ncp->nc_dvlist.le_prev != NULL) {
    205  1.46      yamt 		LIST_REMOVE(ncp, nc_dvlist);
    206  1.46      yamt 		ncp->nc_dvlist.le_prev = NULL;
    207  1.46      yamt 	}
    208  1.46      yamt }
    209  1.46      yamt 
    210  1.73        ad /*
    211  1.73        ad  * Lock all CPUs to prevent any cache lookup activity.  Conceptually,
    212  1.73        ad  * this locks out all "readers".
    213  1.73        ad  */
    214  1.46      yamt static void
    215  1.73        ad cache_lock_cpus(void)
    216  1.46      yamt {
    217  1.73        ad 	CPU_INFO_ITERATOR cii;
    218  1.73        ad 	struct cpu_info *ci;
    219  1.46      yamt 
    220  1.73        ad 	for (CPU_INFO_FOREACH(cii, ci)) {
    221  1.73        ad 		mutex_enter(ci->ci_data.cpu_cachelock);
    222  1.73        ad 	}
    223  1.46      yamt }
    224  1.46      yamt 
    225  1.73        ad /*
    226  1.73        ad  * Release all CPU locks.
    227  1.73        ad  */
    228  1.73        ad static void
    229  1.73        ad cache_unlock_cpus(void)
    230  1.73        ad {
    231  1.73        ad 	CPU_INFO_ITERATOR cii;
    232  1.73        ad 	struct cpu_info *ci;
    233  1.73        ad 
    234  1.73        ad 	for (CPU_INFO_FOREACH(cii, ci)) {
    235  1.73        ad 		mutex_exit(ci->ci_data.cpu_cachelock);
    236  1.73        ad 	}
    237  1.73        ad }
    238  1.73        ad 
    239  1.73        ad /*
    240  1.73        ad  * Find a single cache entry and return it locked.  'namecache_lock' or
    241  1.73        ad  * at least one of the per-CPU locks must be held.
    242  1.73        ad  */
    243  1.73        ad static struct namecache *
    244  1.55      yamt cache_lookup_entry(const struct vnode *dvp, const struct componentname *cnp)
    245  1.55      yamt {
    246  1.55      yamt 	struct nchashhead *ncpp;
    247  1.55      yamt 	struct namecache *ncp;
    248  1.55      yamt 
    249  1.55      yamt 	ncpp = &nchashtbl[NCHASH(cnp, dvp)];
    250  1.55      yamt 
    251  1.55      yamt 	LIST_FOREACH(ncp, ncpp, nc_hash) {
    252  1.73        ad 		if (ncp->nc_dvp != dvp ||
    253  1.73        ad 		    ncp->nc_nlen != cnp->cn_namelen ||
    254  1.73        ad 		    memcmp(ncp->nc_name, cnp->cn_nameptr, (u_int)ncp->nc_nlen))
    255  1.73        ad 		    	continue;
    256  1.73        ad 	    	mutex_enter(&ncp->nc_lock);
    257  1.73        ad 		if (ncp->nc_dvp == dvp) {
    258  1.73        ad 			ncp->nc_hittime = hardclock_ticks;
    259  1.73        ad 			return ncp;
    260  1.73        ad 		}
    261  1.73        ad 		/* Raced: entry has been nullified. */
    262  1.73        ad 		mutex_exit(&ncp->nc_lock);
    263  1.55      yamt 	}
    264  1.55      yamt 
    265  1.73        ad 	return NULL;
    266  1.55      yamt }
    267  1.55      yamt 
    268   1.1       cgd /*
    269   1.1       cgd  * Look for a the name in the cache. We don't do this
    270   1.1       cgd  * if the segment name is long, simply so the cache can avoid
    271   1.1       cgd  * holding long names (which would either waste space, or
    272   1.1       cgd  * add greatly to the complexity).
    273   1.1       cgd  *
    274   1.1       cgd  * Lookup is called with ni_dvp pointing to the directory to search,
    275   1.1       cgd  * ni_ptr pointing to the name of the entry being sought, ni_namelen
    276   1.1       cgd  * tells the length of the name, and ni_hash contains a hash of
    277  1.20  jdolecek  * the name. If the lookup succeeds, the vnode is locked, stored in ni_vp
    278  1.20  jdolecek  * and a status of zero is returned. If the locking fails for whatever
    279  1.20  jdolecek  * reason, the vnode is unlocked and the error is returned to caller.
    280  1.20  jdolecek  * If the lookup determines that the name does not exist (negative cacheing),
    281  1.20  jdolecek  * a status of ENOENT is returned. If the lookup fails, a status of -1
    282  1.20  jdolecek  * is returned.
    283   1.1       cgd  */
    284   1.5   mycroft int
    285  1.34     enami cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
    286   1.1       cgd {
    287  1.23  augustss 	struct namecache *ncp;
    288  1.20  jdolecek 	struct vnode *vp;
    289  1.73        ad 	kmutex_t *cpulock;
    290  1.36   thorpej 	int error;
    291   1.1       cgd 
    292   1.8       cgd 	if (!doingcache) {
    293   1.8       cgd 		cnp->cn_flags &= ~MAKEENTRY;
    294  1.34     enami 		*vpp = NULL;
    295  1.20  jdolecek 		return (-1);
    296   1.8       cgd 	}
    297  1.39        pk 
    298   1.5   mycroft 	if (cnp->cn_namelen > NCHNAMLEN) {
    299  1.70        ad 		/* Unlocked, but only for stats. */
    300  1.73        ad 		COUNT(ncs_long);
    301   1.5   mycroft 		cnp->cn_flags &= ~MAKEENTRY;
    302  1.53      yamt 		goto fail;
    303   1.1       cgd 	}
    304  1.73        ad 	cpulock = curcpu()->ci_data.cpu_cachelock;
    305  1.73        ad 	mutex_enter(cpulock);
    306  1.55      yamt 	ncp = cache_lookup_entry(dvp, cnp);
    307  1.55      yamt 	if (ncp == NULL) {
    308  1.73        ad 		COUNT(ncs_miss);
    309  1.39        pk 		goto fail_wlock;
    310   1.1       cgd 	}
    311   1.9   mycroft 	if ((cnp->cn_flags & MAKEENTRY) == 0) {
    312  1.73        ad 		COUNT(ncs_badhits);
    313  1.20  jdolecek 		goto remove;
    314   1.1       cgd 	} else if (ncp->nc_vp == NULL) {
    315  1.11   mycroft 		/*
    316  1.11   mycroft 		 * Restore the ISWHITEOUT flag saved earlier.
    317  1.11   mycroft 		 */
    318  1.50      yamt 		cnp->cn_flags |= ncp->nc_flags;
    319  1.21   mycroft 		if (cnp->cn_nameiop != CREATE ||
    320  1.21   mycroft 		    (cnp->cn_flags & ISLASTCN) == 0) {
    321  1.73        ad 			COUNT(ncs_neghits);
    322  1.73        ad 			mutex_exit(&ncp->nc_lock);
    323  1.73        ad 			mutex_exit(cpulock);
    324   1.1       cgd 			return (ENOENT);
    325  1.20  jdolecek 		} else {
    326  1.73        ad 			COUNT(ncs_badhits);
    327  1.20  jdolecek 			goto remove;
    328  1.20  jdolecek 		}
    329  1.20  jdolecek 	}
    330  1.20  jdolecek 
    331  1.20  jdolecek 	vp = ncp->nc_vp;
    332  1.73        ad 	mutex_enter(&vp->v_interlock);
    333  1.73        ad 	mutex_exit(&ncp->nc_lock);
    334  1.73        ad 	mutex_exit(cpulock);
    335  1.73        ad 	error = vget(vp, LK_NOWAIT | LK_INTERLOCK);
    336  1.39        pk 
    337  1.52      yamt #ifdef DEBUG
    338  1.52      yamt 	/*
    339  1.73        ad 	 * since we released nb->nb_lock,
    340  1.52      yamt 	 * we can't use this pointer any more.
    341  1.52      yamt 	 */
    342  1.52      yamt 	ncp = NULL;
    343  1.52      yamt #endif /* DEBUG */
    344  1.52      yamt 
    345  1.60      yamt 	if (error) {
    346  1.60      yamt 		KASSERT(error == EBUSY);
    347  1.53      yamt 		/*
    348  1.53      yamt 		 * this vnode is being cleaned out.
    349  1.53      yamt 		 */
    350  1.73        ad 		COUNT(ncs_falsehits); /* XXX badhits? */
    351  1.53      yamt 		goto fail;
    352  1.53      yamt 	}
    353  1.53      yamt 
    354  1.20  jdolecek 	if (vp == dvp) {	/* lookup on "." */
    355  1.20  jdolecek 		error = 0;
    356  1.20  jdolecek 	} else if (cnp->cn_flags & ISDOTDOT) {
    357  1.20  jdolecek 		VOP_UNLOCK(dvp, 0);
    358  1.60      yamt 		error = vn_lock(vp, LK_EXCLUSIVE);
    359  1.67       chs 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
    360   1.1       cgd 	} else {
    361  1.60      yamt 		error = vn_lock(vp, LK_EXCLUSIVE);
    362  1.20  jdolecek 	}
    363  1.20  jdolecek 
    364  1.20  jdolecek 	/*
    365  1.54      yamt 	 * Check that the lock succeeded.
    366  1.20  jdolecek 	 */
    367  1.47      yamt 	if (error) {
    368  1.70        ad 		/* Unlocked, but only for stats. */
    369  1.73        ad 		COUNT(ncs_badhits);
    370  1.31       chs 		*vpp = NULL;
    371  1.31       chs 		return (-1);
    372  1.20  jdolecek 	}
    373  1.20  jdolecek 
    374  1.70        ad 	/* Unlocked, but only for stats. */
    375  1.73        ad 	COUNT(ncs_goodhits);
    376  1.20  jdolecek 	*vpp = vp;
    377  1.20  jdolecek 	return (0);
    378   1.1       cgd 
    379  1.20  jdolecek remove:
    380   1.1       cgd 	/*
    381   1.1       cgd 	 * Last component and we are renaming or deleting,
    382   1.1       cgd 	 * the cache entry is invalid, or otherwise don't
    383   1.1       cgd 	 * want cache entry to exist.
    384   1.1       cgd 	 */
    385  1.73        ad 	cache_invalidate(ncp);
    386  1.73        ad 	mutex_exit(&ncp->nc_lock);
    387  1.39        pk fail_wlock:
    388  1.73        ad 	mutex_exit(cpulock);
    389  1.53      yamt fail:
    390  1.34     enami 	*vpp = NULL;
    391  1.20  jdolecek 	return (-1);
    392   1.1       cgd }
    393   1.1       cgd 
    394  1.61      yamt int
    395  1.61      yamt cache_lookup_raw(struct vnode *dvp, struct vnode **vpp,
    396  1.61      yamt     struct componentname *cnp)
    397  1.61      yamt {
    398  1.61      yamt 	struct namecache *ncp;
    399  1.61      yamt 	struct vnode *vp;
    400  1.73        ad 	kmutex_t *cpulock;
    401  1.61      yamt 	int error;
    402  1.61      yamt 
    403  1.61      yamt 	if (!doingcache) {
    404  1.61      yamt 		cnp->cn_flags &= ~MAKEENTRY;
    405  1.61      yamt 		*vpp = NULL;
    406  1.61      yamt 		return (-1);
    407  1.61      yamt 	}
    408  1.61      yamt 
    409  1.61      yamt 	if (cnp->cn_namelen > NCHNAMLEN) {
    410  1.70        ad 		/* Unlocked, but only for stats. */
    411  1.73        ad 		COUNT(ncs_long);
    412  1.61      yamt 		cnp->cn_flags &= ~MAKEENTRY;
    413  1.61      yamt 		goto fail;
    414  1.61      yamt 	}
    415  1.73        ad 	cpulock = curcpu()->ci_data.cpu_cachelock;
    416  1.73        ad 	mutex_enter(cpulock);
    417  1.61      yamt 	ncp = cache_lookup_entry(dvp, cnp);
    418  1.61      yamt 	if (ncp == NULL) {
    419  1.73        ad 		COUNT(ncs_miss);
    420  1.61      yamt 		goto fail_wlock;
    421  1.61      yamt 	}
    422  1.61      yamt 	vp = ncp->nc_vp;
    423  1.61      yamt 	if (vp == NULL) {
    424  1.61      yamt 		/*
    425  1.61      yamt 		 * Restore the ISWHITEOUT flag saved earlier.
    426  1.61      yamt 		 */
    427  1.61      yamt 		cnp->cn_flags |= ncp->nc_flags;
    428  1.73        ad 		COUNT(ncs_neghits);
    429  1.73        ad 		mutex_exit(&ncp->nc_lock);
    430  1.73        ad 		mutex_exit(cpulock);
    431  1.61      yamt 		return (ENOENT);
    432  1.61      yamt 	}
    433  1.73        ad 	mutex_enter(&vp->v_interlock);
    434  1.73        ad 	mutex_exit(&ncp->nc_lock);
    435  1.73        ad 	mutex_exit(cpulock);
    436  1.73        ad 	error = vget(vp, LK_NOWAIT | LK_INTERLOCK);
    437  1.61      yamt 
    438  1.61      yamt 	if (error) {
    439  1.61      yamt 		KASSERT(error == EBUSY);
    440  1.61      yamt 		/*
    441  1.61      yamt 		 * this vnode is being cleaned out.
    442  1.61      yamt 		 */
    443  1.73        ad 		COUNT(ncs_falsehits); /* XXX badhits? */
    444  1.61      yamt 		goto fail;
    445  1.61      yamt 	}
    446  1.61      yamt 
    447  1.61      yamt 	*vpp = vp;
    448  1.61      yamt 
    449  1.61      yamt 	return 0;
    450  1.61      yamt 
    451  1.61      yamt fail_wlock:
    452  1.73        ad 	mutex_exit(cpulock);
    453  1.61      yamt fail:
    454  1.61      yamt 	*vpp = NULL;
    455  1.61      yamt 	return -1;
    456  1.61      yamt }
    457  1.61      yamt 
    458   1.1       cgd /*
    459  1.19  sommerfe  * Scan cache looking for name of directory entry pointing at vp.
    460  1.19  sommerfe  *
    461  1.19  sommerfe  * Fill in dvpp.
    462  1.19  sommerfe  *
    463  1.19  sommerfe  * If bufp is non-NULL, also place the name in the buffer which starts
    464  1.19  sommerfe  * at bufp, immediately before *bpp, and move bpp backwards to point
    465  1.19  sommerfe  * at the start of it.  (Yes, this is a little baroque, but it's done
    466  1.19  sommerfe  * this way to cater to the whims of getcwd).
    467  1.19  sommerfe  *
    468  1.19  sommerfe  * Returns 0 on success, -1 on cache miss, positive errno on failure.
    469  1.19  sommerfe  */
    470  1.19  sommerfe int
    471  1.34     enami cache_revlookup(struct vnode *vp, struct vnode **dvpp, char **bpp, char *bufp)
    472  1.19  sommerfe {
    473  1.19  sommerfe 	struct namecache *ncp;
    474  1.19  sommerfe 	struct vnode *dvp;
    475  1.19  sommerfe 	struct ncvhashhead *nvcpp;
    476  1.34     enami 	char *bp;
    477  1.34     enami 
    478  1.19  sommerfe 	if (!doingcache)
    479  1.19  sommerfe 		goto out;
    480  1.19  sommerfe 
    481  1.30       chs 	nvcpp = &ncvhashtbl[NCVHASH(vp)];
    482  1.19  sommerfe 
    483  1.73        ad 	mutex_enter(namecache_lock);
    484  1.27       chs 	LIST_FOREACH(ncp, nvcpp, nc_vhash) {
    485  1.73        ad 		mutex_enter(&ncp->nc_lock);
    486  1.34     enami 		if (ncp->nc_vp == vp &&
    487  1.34     enami 		    (dvp = ncp->nc_dvp) != NULL &&
    488  1.47      yamt 		    dvp != vp) { 		/* avoid pesky . entries.. */
    489  1.34     enami 
    490  1.19  sommerfe #ifdef DIAGNOSTIC
    491  1.34     enami 			if (ncp->nc_nlen == 1 &&
    492  1.34     enami 			    ncp->nc_name[0] == '.')
    493  1.19  sommerfe 				panic("cache_revlookup: found entry for .");
    494  1.19  sommerfe 
    495  1.34     enami 			if (ncp->nc_nlen == 2 &&
    496  1.34     enami 			    ncp->nc_name[0] == '.' &&
    497  1.34     enami 			    ncp->nc_name[1] == '.')
    498  1.19  sommerfe 				panic("cache_revlookup: found entry for ..");
    499  1.19  sommerfe #endif
    500  1.73        ad 			COUNT(ncs_revhits);
    501  1.19  sommerfe 
    502  1.19  sommerfe 			if (bufp) {
    503  1.19  sommerfe 				bp = *bpp;
    504  1.19  sommerfe 				bp -= ncp->nc_nlen;
    505  1.19  sommerfe 				if (bp <= bufp) {
    506  1.34     enami 					*dvpp = NULL;
    507  1.73        ad 					mutex_exit(&ncp->nc_lock);
    508  1.73        ad 					mutex_exit(namecache_lock);
    509  1.34     enami 					return (ERANGE);
    510  1.19  sommerfe 				}
    511  1.19  sommerfe 				memcpy(bp, ncp->nc_name, ncp->nc_nlen);
    512  1.19  sommerfe 				*bpp = bp;
    513  1.19  sommerfe 			}
    514  1.34     enami 
    515  1.19  sommerfe 			/* XXX MP: how do we know dvp won't evaporate? */
    516  1.19  sommerfe 			*dvpp = dvp;
    517  1.73        ad 			mutex_exit(&ncp->nc_lock);
    518  1.73        ad 			mutex_exit(namecache_lock);
    519  1.34     enami 			return (0);
    520  1.19  sommerfe 		}
    521  1.73        ad 		mutex_exit(&ncp->nc_lock);
    522  1.19  sommerfe 	}
    523  1.73        ad 	COUNT(ncs_revmiss);
    524  1.73        ad 	mutex_exit(namecache_lock);
    525  1.19  sommerfe  out:
    526  1.34     enami 	*dvpp = NULL;
    527  1.34     enami 	return (-1);
    528  1.19  sommerfe }
    529  1.19  sommerfe 
    530  1.19  sommerfe /*
    531   1.1       cgd  * Add an entry to the cache
    532   1.1       cgd  */
    533  1.13  christos void
    534  1.34     enami cache_enter(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
    535   1.1       cgd {
    536  1.23  augustss 	struct namecache *ncp;
    537  1.59      yamt 	struct namecache *oncp;
    538  1.23  augustss 	struct nchashhead *ncpp;
    539  1.23  augustss 	struct ncvhashhead *nvcpp;
    540   1.1       cgd 
    541   1.5   mycroft #ifdef DIAGNOSTIC
    542   1.5   mycroft 	if (cnp->cn_namelen > NCHNAMLEN)
    543   1.5   mycroft 		panic("cache_enter: name too long");
    544   1.5   mycroft #endif
    545   1.1       cgd 	if (!doingcache)
    546   1.1       cgd 		return;
    547  1.58      yamt 
    548  1.73        ad 	if (numcache > desiredvnodes) {
    549  1.73        ad 		mutex_enter(namecache_lock);
    550  1.73        ad 		cache_ev_forced.ev_count++;
    551  1.73        ad 		cache_reclaim();
    552  1.73        ad 		mutex_exit(namecache_lock);
    553  1.39        pk 	}
    554  1.57        pk 
    555  1.73        ad 	ncp = pool_cache_get(namecache_cache, PR_WAITOK);
    556  1.73        ad 	mutex_enter(namecache_lock);
    557  1.73        ad 	numcache++;
    558  1.73        ad 
    559  1.59      yamt 	/*
    560  1.59      yamt 	 * Concurrent lookups in the same directory may race for a
    561  1.59      yamt 	 * cache entry.  if there's a duplicated entry, free it.
    562  1.59      yamt 	 */
    563  1.59      yamt 	oncp = cache_lookup_entry(dvp, cnp);
    564  1.59      yamt 	if (oncp) {
    565  1.73        ad 		cache_invalidate(oncp);
    566  1.73        ad 		mutex_exit(&oncp->nc_lock);
    567  1.59      yamt 	}
    568  1.59      yamt 
    569  1.34     enami 	/* Grab the vnode we just found. */
    570  1.73        ad 	mutex_enter(&ncp->nc_lock);
    571   1.5   mycroft 	ncp->nc_vp = vp;
    572  1.73        ad 	ncp->nc_flags = 0;
    573  1.73        ad 	ncp->nc_hittime = 0;
    574  1.73        ad 	ncp->nc_gcqueue = NULL;
    575  1.47      yamt 	if (vp == NULL) {
    576  1.11   mycroft 		/*
    577  1.11   mycroft 		 * For negative hits, save the ISWHITEOUT flag so we can
    578  1.11   mycroft 		 * restore it later when the cache entry is used again.
    579  1.11   mycroft 		 */
    580  1.50      yamt 		ncp->nc_flags = cnp->cn_flags & ISWHITEOUT;
    581  1.11   mycroft 	}
    582  1.34     enami 	/* Fill in cache info. */
    583   1.5   mycroft 	ncp->nc_dvp = dvp;
    584  1.46      yamt 	LIST_INSERT_HEAD(&dvp->v_dnclist, ncp, nc_dvlist);
    585  1.46      yamt 	if (vp)
    586  1.46      yamt 		LIST_INSERT_HEAD(&vp->v_nclist, ncp, nc_vlist);
    587  1.73        ad 	else {
    588  1.73        ad 		ncp->nc_vlist.le_prev = NULL;
    589  1.73        ad 		ncp->nc_vlist.le_next = NULL;
    590  1.73        ad 	}
    591   1.5   mycroft 	ncp->nc_nlen = cnp->cn_namelen;
    592  1.73        ad 	TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
    593  1.17     perry 	memcpy(ncp->nc_name, cnp->cn_nameptr, (unsigned)ncp->nc_nlen);
    594  1.30       chs 	ncpp = &nchashtbl[NCHASH(cnp, dvp)];
    595  1.73        ad 
    596  1.73        ad 	/*
    597  1.73        ad 	 * Flush updates before making visible in table.  No need for a
    598  1.73        ad 	 * memory barrier on the other side: to see modifications the
    599  1.73        ad 	 * list must be followed, meaning a dependent pointer load.
    600  1.73        ad 	 */
    601  1.73        ad 	membar_producer();
    602   1.9   mycroft 	LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
    603  1.19  sommerfe 
    604  1.34     enami 	ncp->nc_vhash.le_prev = NULL;
    605  1.34     enami 	ncp->nc_vhash.le_next = NULL;
    606  1.34     enami 
    607  1.19  sommerfe 	/*
    608  1.19  sommerfe 	 * Create reverse-cache entries (used in getcwd) for directories.
    609  1.66  christos 	 * (and in linux procfs exe node)
    610  1.19  sommerfe 	 */
    611  1.33     enami 	if (vp != NULL &&
    612  1.33     enami 	    vp != dvp &&
    613  1.29      fvdl #ifndef NAMECACHE_ENTER_REVERSE
    614  1.33     enami 	    vp->v_type == VDIR &&
    615  1.29      fvdl #endif
    616  1.33     enami 	    (ncp->nc_nlen > 2 ||
    617  1.33     enami 	    (ncp->nc_nlen > 1 && ncp->nc_name[1] != '.') ||
    618  1.33     enami 	    (/* ncp->nc_nlen > 0 && */ ncp->nc_name[0] != '.'))) {
    619  1.30       chs 		nvcpp = &ncvhashtbl[NCVHASH(vp)];
    620  1.19  sommerfe 		LIST_INSERT_HEAD(nvcpp, ncp, nc_vhash);
    621  1.19  sommerfe 	}
    622  1.73        ad 	mutex_exit(&ncp->nc_lock);
    623  1.73        ad 	mutex_exit(namecache_lock);
    624   1.1       cgd }
    625   1.1       cgd 
    626   1.1       cgd /*
    627   1.1       cgd  * Name cache initialization, from vfs_init() when we are booting
    628   1.1       cgd  */
    629  1.13  christos void
    630  1.34     enami nchinit(void)
    631   1.1       cgd {
    632  1.73        ad 	int error;
    633   1.1       cgd 
    634  1.73        ad 	namecache_cache = pool_cache_init(sizeof(struct namecache),
    635  1.73        ad 	    coherency_unit, 0, 0, "ncache", NULL, IPL_NONE, cache_ctor,
    636  1.73        ad 	    cache_dtor, NULL);
    637  1.71        ad 	KASSERT(namecache_cache != NULL);
    638  1.71        ad 
    639  1.73        ad 	namecache_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    640  1.73        ad 
    641  1.26        ad 	nchashtbl =
    642  1.26        ad 	    hashinit(desiredvnodes, HASH_LIST, M_CACHE, M_WAITOK, &nchash);
    643  1.26        ad 	ncvhashtbl =
    644  1.29      fvdl #ifdef NAMECACHE_ENTER_REVERSE
    645  1.29      fvdl 	    hashinit(desiredvnodes, HASH_LIST, M_CACHE, M_WAITOK, &ncvhash);
    646  1.29      fvdl #else
    647  1.26        ad 	    hashinit(desiredvnodes/8, HASH_LIST, M_CACHE, M_WAITOK, &ncvhash);
    648  1.29      fvdl #endif
    649  1.73        ad 
    650  1.73        ad 	error = kthread_create(PRI_VM, KTHREAD_MPSAFE, NULL, cache_thread,
    651  1.73        ad 	    NULL, NULL, "cachegc");
    652  1.73        ad 	if (error != 0)
    653  1.73        ad 		panic("nchinit %d", error);
    654  1.73        ad 
    655  1.73        ad 	evcnt_attach_dynamic(&cache_ev_scan, EVCNT_TYPE_MISC, NULL,
    656  1.73        ad 	   "namecache", "entries scanned");
    657  1.73        ad 	evcnt_attach_dynamic(&cache_ev_gc, EVCNT_TYPE_MISC, NULL,
    658  1.73        ad 	   "namecache", "entries collected");
    659  1.73        ad 	evcnt_attach_dynamic(&cache_ev_over, EVCNT_TYPE_MISC, NULL,
    660  1.73        ad 	   "namecache", "over scan target");
    661  1.73        ad 	evcnt_attach_dynamic(&cache_ev_under, EVCNT_TYPE_MISC, NULL,
    662  1.73        ad 	   "namecache", "under scan target");
    663  1.73        ad 	evcnt_attach_dynamic(&cache_ev_forced, EVCNT_TYPE_MISC, NULL,
    664  1.73        ad 	   "namecache", "forced reclaims");
    665  1.73        ad }
    666  1.73        ad 
    667  1.73        ad static int
    668  1.73        ad cache_ctor(void *arg, void *obj, int flag)
    669  1.73        ad {
    670  1.73        ad 	struct namecache *ncp;
    671  1.73        ad 
    672  1.73        ad 	ncp = obj;
    673  1.73        ad 	mutex_init(&ncp->nc_lock, MUTEX_DEFAULT, IPL_NONE);
    674  1.73        ad 
    675  1.73        ad 	return 0;
    676  1.73        ad }
    677  1.73        ad 
    678  1.73        ad static void
    679  1.73        ad cache_dtor(void *arg, void *obj)
    680  1.73        ad {
    681  1.73        ad 	struct namecache *ncp;
    682  1.73        ad 
    683  1.73        ad 	ncp = obj;
    684  1.73        ad 	mutex_destroy(&ncp->nc_lock);
    685  1.73        ad }
    686  1.73        ad 
    687  1.73        ad /*
    688  1.73        ad  * Called once for each CPU in the system as attached.
    689  1.73        ad  */
    690  1.73        ad void
    691  1.73        ad cache_cpu_init(struct cpu_info *ci)
    692  1.73        ad {
    693  1.73        ad 
    694  1.73        ad 	ci->ci_data.cpu_cachelock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    695  1.30       chs }
    696  1.30       chs 
    697  1.30       chs /*
    698  1.30       chs  * Name cache reinitialization, for when the maximum number of vnodes increases.
    699  1.30       chs  */
    700  1.30       chs void
    701  1.34     enami nchreinit(void)
    702  1.30       chs {
    703  1.30       chs 	struct namecache *ncp;
    704  1.30       chs 	struct nchashhead *oldhash1, *hash1;
    705  1.30       chs 	struct ncvhashhead *oldhash2, *hash2;
    706  1.36   thorpej 	u_long i, oldmask1, oldmask2, mask1, mask2;
    707  1.30       chs 
    708  1.30       chs 	hash1 = hashinit(desiredvnodes, HASH_LIST, M_CACHE, M_WAITOK, &mask1);
    709  1.30       chs 	hash2 =
    710  1.30       chs #ifdef NAMECACHE_ENTER_REVERSE
    711  1.30       chs 	    hashinit(desiredvnodes, HASH_LIST, M_CACHE, M_WAITOK, &mask2);
    712  1.30       chs #else
    713  1.30       chs 	    hashinit(desiredvnodes/8, HASH_LIST, M_CACHE, M_WAITOK, &mask2);
    714  1.30       chs #endif
    715  1.73        ad 	mutex_enter(namecache_lock);
    716  1.73        ad 	cache_lock_cpus();
    717  1.30       chs 	oldhash1 = nchashtbl;
    718  1.30       chs 	oldmask1 = nchash;
    719  1.30       chs 	nchashtbl = hash1;
    720  1.30       chs 	nchash = mask1;
    721  1.30       chs 	oldhash2 = ncvhashtbl;
    722  1.30       chs 	oldmask2 = ncvhash;
    723  1.30       chs 	ncvhashtbl = hash2;
    724  1.30       chs 	ncvhash = mask2;
    725  1.30       chs 	for (i = 0; i <= oldmask1; i++) {
    726  1.30       chs 		while ((ncp = LIST_FIRST(&oldhash1[i])) != NULL) {
    727  1.30       chs 			LIST_REMOVE(ncp, nc_hash);
    728  1.30       chs 			ncp->nc_hash.le_prev = NULL;
    729  1.30       chs 		}
    730  1.30       chs 	}
    731  1.30       chs 	for (i = 0; i <= oldmask2; i++) {
    732  1.30       chs 		while ((ncp = LIST_FIRST(&oldhash2[i])) != NULL) {
    733  1.30       chs 			LIST_REMOVE(ncp, nc_vhash);
    734  1.30       chs 			ncp->nc_vhash.le_prev = NULL;
    735  1.30       chs 		}
    736  1.30       chs 	}
    737  1.73        ad 	cache_unlock_cpus();
    738  1.73        ad 	mutex_exit(namecache_lock);
    739  1.30       chs 	hashdone(oldhash1, M_CACHE);
    740  1.30       chs 	hashdone(oldhash2, M_CACHE);
    741   1.1       cgd }
    742   1.1       cgd 
    743   1.1       cgd /*
    744   1.1       cgd  * Cache flush, a particular vnode; called when a vnode is renamed to
    745   1.1       cgd  * hide entries that would now be invalid
    746   1.1       cgd  */
    747  1.13  christos void
    748  1.55      yamt cache_purge1(struct vnode *vp, const struct componentname *cnp, int flags)
    749   1.1       cgd {
    750  1.46      yamt 	struct namecache *ncp, *ncnext;
    751   1.1       cgd 
    752  1.73        ad 	mutex_enter(namecache_lock);
    753  1.55      yamt 	if (flags & PURGE_PARENTS) {
    754  1.55      yamt 		for (ncp = LIST_FIRST(&vp->v_nclist); ncp != NULL;
    755  1.55      yamt 		    ncp = ncnext) {
    756  1.55      yamt 			ncnext = LIST_NEXT(ncp, nc_vlist);
    757  1.73        ad 			mutex_enter(&ncp->nc_lock);
    758  1.73        ad 			cache_invalidate(ncp);
    759  1.73        ad 			mutex_exit(&ncp->nc_lock);
    760  1.73        ad 			cache_disassociate(ncp);
    761  1.55      yamt 		}
    762  1.55      yamt 	}
    763  1.55      yamt 	if (flags & PURGE_CHILDREN) {
    764  1.55      yamt 		for (ncp = LIST_FIRST(&vp->v_dnclist); ncp != NULL;
    765  1.55      yamt 		    ncp = ncnext) {
    766  1.55      yamt 			ncnext = LIST_NEXT(ncp, nc_dvlist);
    767  1.73        ad 			mutex_enter(&ncp->nc_lock);
    768  1.73        ad 			cache_invalidate(ncp);
    769  1.73        ad 			mutex_exit(&ncp->nc_lock);
    770  1.73        ad 			cache_disassociate(ncp);
    771  1.55      yamt 		}
    772  1.46      yamt 	}
    773  1.55      yamt 	if (cnp != NULL) {
    774  1.55      yamt 		ncp = cache_lookup_entry(vp, cnp);
    775  1.55      yamt 		if (ncp) {
    776  1.73        ad 			cache_invalidate(ncp);
    777  1.73        ad 			cache_disassociate(ncp);
    778  1.73        ad 			mutex_exit(&ncp->nc_lock);
    779  1.55      yamt 		}
    780  1.46      yamt 	}
    781  1.73        ad 	mutex_exit(namecache_lock);
    782   1.1       cgd }
    783   1.1       cgd 
    784   1.1       cgd /*
    785   1.1       cgd  * Cache flush, a whole filesystem; called when filesys is umounted to
    786  1.27       chs  * remove entries that would now be invalid.
    787   1.1       cgd  */
    788  1.13  christos void
    789  1.34     enami cache_purgevfs(struct mount *mp)
    790   1.1       cgd {
    791  1.23  augustss 	struct namecache *ncp, *nxtcp;
    792   1.1       cgd 
    793  1.73        ad 	mutex_enter(namecache_lock);
    794  1.73        ad 	for (ncp = TAILQ_FIRST(&nclruhead); ncp != NULL; ncp = nxtcp) {
    795  1.73        ad 		nxtcp = TAILQ_NEXT(ncp, nc_lru);
    796  1.73        ad 		mutex_enter(&ncp->nc_lock);
    797  1.73        ad 		if (ncp->nc_dvp != NULL && ncp->nc_dvp->v_mount == mp) {
    798  1.73        ad 			/* Free the resources we had. */
    799  1.73        ad 			cache_invalidate(ncp);
    800  1.73        ad 			cache_disassociate(ncp);
    801  1.73        ad 		}
    802  1.73        ad 		mutex_exit(&ncp->nc_lock);
    803  1.73        ad 	}
    804  1.73        ad 	cache_reclaim();
    805  1.73        ad 	mutex_exit(namecache_lock);
    806  1.73        ad }
    807  1.73        ad 
    808  1.73        ad /*
    809  1.73        ad  * Scan global list invalidating entries until we meet a preset target.
    810  1.73        ad  * Prefer to invalidate entries that have not scored a hit within
    811  1.73        ad  * cache_hottime seconds.  We sort the LRU list only for this routine's
    812  1.73        ad  * benefit.
    813  1.73        ad  */
    814  1.73        ad static void
    815  1.73        ad cache_prune(int incache, int target)
    816  1.73        ad {
    817  1.73        ad 	struct namecache *ncp, *nxtcp, *sentinel;
    818  1.73        ad 	int items, recent, tryharder;
    819  1.73        ad 
    820  1.73        ad 	KASSERT(mutex_owned(namecache_lock));
    821  1.73        ad 
    822  1.73        ad 	items = 0;
    823  1.73        ad 	tryharder = 0;
    824  1.73        ad 	recent = hardclock_ticks - hz * cache_hottime;
    825  1.73        ad 	sentinel = NULL;
    826  1.27       chs 	for (ncp = TAILQ_FIRST(&nclruhead); ncp != NULL; ncp = nxtcp) {
    827  1.73        ad 		if (incache <= target)
    828  1.73        ad 			break;
    829  1.73        ad 		items++;
    830  1.27       chs 		nxtcp = TAILQ_NEXT(ncp, nc_lru);
    831  1.73        ad 		if (ncp->nc_dvp == NULL)
    832   1.1       cgd 			continue;
    833  1.73        ad 		if (ncp == sentinel) {
    834  1.73        ad 			/*
    835  1.73        ad 			 * If we looped back on ourself, then ignore
    836  1.73        ad 			 * recent entries and purge whatever we find.
    837  1.73        ad 			 */
    838  1.73        ad 			tryharder = 1;
    839   1.5   mycroft 		}
    840  1.73        ad 		if (!tryharder && ncp->nc_hittime > recent) {
    841  1.73        ad 			if (sentinel == NULL)
    842  1.73        ad 				sentinel = ncp;
    843  1.73        ad 			TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
    844  1.73        ad 			TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
    845  1.73        ad 			continue;
    846  1.73        ad 		}
    847  1.73        ad 		mutex_enter(&ncp->nc_lock);
    848  1.73        ad 		if (ncp->nc_dvp != NULL) {
    849  1.73        ad 			cache_invalidate(ncp);
    850  1.73        ad 			cache_disassociate(ncp);
    851  1.73        ad 			incache--;
    852  1.73        ad 		}
    853  1.73        ad 		mutex_exit(&ncp->nc_lock);
    854  1.73        ad 	}
    855  1.73        ad 	cache_ev_scan.ev_count += items;
    856  1.73        ad }
    857  1.73        ad 
    858  1.73        ad /*
    859  1.73        ad  * Collect dead cache entries from all CPUs and garbage collect.
    860  1.73        ad  */
    861  1.73        ad static void
    862  1.73        ad cache_reclaim(void)
    863  1.73        ad {
    864  1.73        ad 	struct namecache *ncp, *next;
    865  1.73        ad 	int items;
    866  1.73        ad 
    867  1.73        ad 	KASSERT(mutex_owned(namecache_lock));
    868  1.73        ad 
    869  1.73        ad 	/*
    870  1.73        ad 	 * If the number of extant entries not awaiting garbage collection
    871  1.73        ad 	 * exceeds the high water mark, then reclaim stale entries until we
    872  1.73        ad 	 * reach our low water mark.
    873  1.73        ad 	 */
    874  1.73        ad 	items = numcache - cache_gcpend;
    875  1.73        ad 	if (items > (uint64_t)desiredvnodes * cache_hiwat / 100) {
    876  1.73        ad 		cache_prune(items, (int)((uint64_t)desiredvnodes *
    877  1.73        ad 		    cache_lowat / 100));
    878  1.73        ad 		cache_ev_over.ev_count++;
    879  1.73        ad 	} else
    880  1.73        ad 		cache_ev_under.ev_count++;
    881  1.73        ad 
    882  1.73        ad 	/*
    883  1.73        ad 	 * Stop forward lookup activity on all CPUs and garbage collect dead
    884  1.73        ad 	 * entries.
    885  1.73        ad 	 */
    886  1.73        ad 	cache_lock_cpus();
    887  1.73        ad 	ncp = cache_gcqueue;
    888  1.73        ad 	cache_gcqueue = NULL;
    889  1.73        ad 	items = cache_gcpend;
    890  1.73        ad 	cache_gcpend = 0;
    891  1.73        ad 	while (ncp != NULL) {
    892  1.73        ad 		next = ncp->nc_gcqueue;
    893  1.73        ad 		cache_disassociate(ncp);
    894  1.73        ad 		KASSERT(ncp->nc_dvp == NULL);
    895  1.73        ad 		if (ncp->nc_hash.le_prev != NULL) {
    896  1.73        ad 			LIST_REMOVE(ncp, nc_hash);
    897  1.73        ad 			ncp->nc_hash.le_prev = NULL;
    898  1.73        ad 		}
    899  1.73        ad 		pool_cache_put(namecache_cache, ncp);
    900  1.73        ad 		ncp = next;
    901  1.73        ad 	}
    902  1.73        ad 	cache_unlock_cpus();
    903  1.73        ad 	numcache -= items;
    904  1.73        ad 	cache_ev_gc.ev_count += items;
    905  1.73        ad }
    906  1.73        ad 
    907  1.73        ad /*
    908  1.73        ad  * Cache maintainence thread, awakening once per second to:
    909  1.73        ad  *
    910  1.73        ad  * => keep number of entries below the high water mark
    911  1.73        ad  * => sort pseudo-LRU list
    912  1.73        ad  * => garbage collect dead entries
    913  1.73        ad  */
    914  1.73        ad static void
    915  1.73        ad cache_thread(void *arg)
    916  1.73        ad {
    917  1.73        ad 
    918  1.73        ad 	mutex_enter(namecache_lock);
    919  1.73        ad 	for (;;) {
    920  1.73        ad 		cache_reclaim();
    921  1.73        ad 		kpause("cachegc", false, hz, namecache_lock);
    922   1.1       cgd 	}
    923   1.1       cgd }
    924  1.19  sommerfe 
    925  1.28       chs #ifdef DDB
    926  1.28       chs void
    927  1.28       chs namecache_print(struct vnode *vp, void (*pr)(const char *, ...))
    928  1.28       chs {
    929  1.28       chs 	struct vnode *dvp = NULL;
    930  1.28       chs 	struct namecache *ncp;
    931  1.28       chs 
    932  1.28       chs 	TAILQ_FOREACH(ncp, &nclruhead, nc_lru) {
    933  1.73        ad 		if (ncp->nc_vp == vp && ncp->nc_dvp != NULL) {
    934  1.28       chs 			(*pr)("name %.*s\n", ncp->nc_nlen, ncp->nc_name);
    935  1.28       chs 			dvp = ncp->nc_dvp;
    936  1.28       chs 		}
    937  1.28       chs 	}
    938  1.28       chs 	if (dvp == NULL) {
    939  1.28       chs 		(*pr)("name not found\n");
    940  1.28       chs 		return;
    941  1.28       chs 	}
    942  1.28       chs 	vp = dvp;
    943  1.28       chs 	TAILQ_FOREACH(ncp, &nclruhead, nc_lru) {
    944  1.47      yamt 		if (ncp->nc_vp == vp) {
    945  1.28       chs 			(*pr)("parent %.*s\n", ncp->nc_nlen, ncp->nc_name);
    946  1.28       chs 		}
    947  1.28       chs 	}
    948  1.28       chs }
    949  1.28       chs #endif
    950