Home | History | Annotate | Line # | Download | only in kern
vfs_cache.c revision 1.124
      1  1.124        ad /*	$NetBSD: vfs_cache.c,v 1.124 2019/12/01 13:39:53 ad Exp $	*/
      2   1.73        ad 
      3   1.73        ad /*-
      4  1.124        ad  * Copyright (c) 2008, 2019 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  *
     16   1.73        ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17   1.73        ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18   1.73        ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19   1.73        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20   1.73        ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21   1.73        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22   1.73        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23   1.73        ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24   1.73        ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25   1.73        ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26   1.73        ad  * POSSIBILITY OF SUCH DAMAGE.
     27   1.73        ad  */
     28    1.6       cgd 
     29    1.1       cgd /*
     30    1.5   mycroft  * Copyright (c) 1989, 1993
     31    1.5   mycroft  *	The Regents of the University of California.  All rights reserved.
     32    1.1       cgd  *
     33    1.1       cgd  * Redistribution and use in source and binary forms, with or without
     34    1.1       cgd  * modification, are permitted provided that the following conditions
     35    1.1       cgd  * are met:
     36    1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     37    1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     38    1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     39    1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     40    1.1       cgd  *    documentation and/or other materials provided with the distribution.
     41   1.51       agc  * 3. Neither the name of the University nor the names of its contributors
     42    1.1       cgd  *    may be used to endorse or promote products derived from this software
     43    1.1       cgd  *    without specific prior written permission.
     44    1.1       cgd  *
     45    1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     46    1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     47    1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     48    1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     49    1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     50    1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     51    1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     52    1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     53    1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     54    1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     55    1.1       cgd  * SUCH DAMAGE.
     56    1.1       cgd  *
     57   1.10   mycroft  *	@(#)vfs_cache.c	8.3 (Berkeley) 8/22/94
     58    1.1       cgd  */
     59   1.32     lukem 
     60   1.32     lukem #include <sys/cdefs.h>
     61  1.124        ad __KERNEL_RCSID(0, "$NetBSD: vfs_cache.c,v 1.124 2019/12/01 13:39:53 ad Exp $");
     62    1.1       cgd 
     63  1.121  christos #define __NAMECACHE_PRIVATE
     64  1.107     pooka #ifdef _KERNEL_OPT
     65   1.28       chs #include "opt_ddb.h"
     66  1.115  riastrad #include "opt_dtrace.h"
     67   1.29      fvdl #include "opt_revcache.h"
     68  1.107     pooka #endif
     69   1.28       chs 
     70    1.4   mycroft #include <sys/param.h>
     71  1.115  riastrad #include <sys/atomic.h>
     72  1.115  riastrad #include <sys/cpu.h>
     73  1.115  riastrad #include <sys/errno.h>
     74  1.115  riastrad #include <sys/evcnt.h>
     75  1.115  riastrad #include <sys/kernel.h>
     76  1.115  riastrad #include <sys/kthread.h>
     77    1.4   mycroft #include <sys/mount.h>
     78  1.115  riastrad #include <sys/mutex.h>
     79    1.4   mycroft #include <sys/namei.h>
     80   1.18   thorpej #include <sys/pool.h>
     81  1.108  christos #include <sys/sdt.h>
     82  1.115  riastrad #include <sys/sysctl.h>
     83  1.115  riastrad #include <sys/systm.h>
     84  1.115  riastrad #include <sys/time.h>
     85  1.115  riastrad #include <sys/vnode_impl.h>
     86    1.1       cgd 
     87   1.66  christos #define NAMECACHE_ENTER_REVERSE
     88    1.1       cgd /*
     89    1.1       cgd  * Name caching works as follows:
     90    1.1       cgd  *
     91    1.1       cgd  * Names found by directory scans are retained in a cache
     92    1.1       cgd  * for future reference.  It is managed LRU, so frequently
     93    1.1       cgd  * used names will hang around.  Cache is indexed by hash value
     94   1.20  jdolecek  * obtained from (dvp, name) where dvp refers to the directory
     95    1.1       cgd  * containing name.
     96    1.1       cgd  *
     97    1.1       cgd  * Upon reaching the last segment of a path, if the reference
     98    1.1       cgd  * is for DELETE, or NOCACHE is set (rewrite), and the
     99    1.1       cgd  * name is located in the cache, it will be dropped.
    100    1.1       cgd  */
    101    1.1       cgd 
    102    1.1       cgd /*
    103  1.120  riastrad  * Cache entry lifetime:
    104  1.120  riastrad  *
    105  1.120  riastrad  *	nonexistent
    106  1.120  riastrad  *	---create---> active
    107  1.120  riastrad  *	---invalidate---> queued
    108  1.120  riastrad  *	---reclaim---> nonexistent.
    109  1.120  riastrad  *
    110  1.120  riastrad  * States:
    111  1.120  riastrad  * - Nonexistent.  Cache entry does not exist.
    112  1.120  riastrad  *
    113  1.120  riastrad  * - Active.  cache_lookup, cache_lookup_raw, cache_revlookup can look
    114  1.120  riastrad  *   up, acquire references, and hand off references to vnodes,
    115  1.120  riastrad  *   e.g. via v_interlock.  Marked by nonnull ncp->nc_dvp.
    116  1.120  riastrad  *
    117  1.120  riastrad  * - Queued.  Pending desstruction by cache_reclaim.  Cannot be used by
    118  1.120  riastrad  *   cache_lookup, cache_lookup_raw, or cache_revlookup.  May still be
    119  1.120  riastrad  *   on lists.  Marked by null ncp->nc_dvp.
    120  1.120  riastrad  *
    121  1.120  riastrad  * Transitions:
    122  1.120  riastrad  *
    123  1.120  riastrad  * - Create: nonexistent--->active
    124  1.120  riastrad  *
    125  1.120  riastrad  *   Done by cache_enter(dvp, vp, name, namelen, cnflags), called by
    126  1.120  riastrad  *   VOP_LOOKUP after the answer is found.  Allocates a struct
    127  1.120  riastrad  *   namecache object, initializes it with the above fields, and
    128  1.120  riastrad  *   activates it by inserting it into the forward and reverse tables.
    129  1.120  riastrad  *
    130  1.120  riastrad  * - Invalidate: active--->queued
    131  1.120  riastrad  *
    132  1.120  riastrad  *   Done by cache_invalidate.  If not already invalidated, nullify
    133  1.124        ad  *   ncp->nc_dvp and ncp->nc_vp, and add to namecache_gc_queue.  Called,
    134  1.120  riastrad  *   among various other places, in cache_lookup(dvp, name, namelen,
    135  1.120  riastrad  *   nameiop, cnflags, &iswht, &vp) when MAKEENTRY is missing from
    136  1.120  riastrad  *   cnflags.
    137  1.120  riastrad  *
    138  1.120  riastrad  * - Reclaim: queued--->nonexistent
    139  1.120  riastrad  *
    140  1.120  riastrad  *   Done by cache_reclaim.  Disassociate ncp from any lists it is on
    141  1.120  riastrad  *   and free memory.
    142  1.120  riastrad  */
    143  1.120  riastrad 
    144  1.120  riastrad /*
    145  1.117  riastrad  * Locking.
    146  1.102    dennis  *
    147  1.117  riastrad  * L namecache_lock		Global lock for namecache table and queues.
    148  1.124        ad  * G namecache_gc_lock		Global lock for garbage collection.
    149  1.117  riastrad  * C struct nchcpu::cpu_lock	Per-CPU lock to reduce read contention.
    150  1.124        ad  * N struct namecache::nc_lock	Per-entry lock, matching nc_vp->v_interlock.
    151  1.124        ad  *				If nc_vp==NULL, lock is private / not shared.
    152  1.117  riastrad  *
    153  1.124        ad  * Lock order: L -> C -> N
    154  1.118  riastrad  *
    155  1.118  riastrad  *	Examples:
    156  1.118  riastrad  *	. L->C: cache_reclaim
    157  1.124        ad  *	. C->N: cache_lookup
    158  1.124        ad  *	. L->N: cache_purge1, cache_revlookup
    159  1.117  riastrad  *
    160  1.117  riastrad  * All use serialized by namecache_lock:
    161  1.117  riastrad  *
    162  1.117  riastrad  *	nclruhead / struct namecache::nc_lru
    163  1.117  riastrad  *	ncvhashtbl / struct namecache::nc_vhash
    164  1.117  riastrad  *	struct vnode_impl::vi_dnclist / struct namecache::nc_dvlist
    165  1.117  riastrad  *	struct vnode_impl::vi_nclist / struct namecache::nc_vlist
    166  1.117  riastrad  *	nchstats
    167  1.117  riastrad  *
    168  1.117  riastrad  * - Insertion serialized by namecache_lock,
    169  1.117  riastrad  * - read protected by per-CPU lock,
    170  1.117  riastrad  * - insert/read ordering guaranteed by memory barriers, and
    171  1.124        ad  * - deletion allowed only under namecache_lock, with namecache_gc_lock
    172  1.124        ad  *   taken to chop out the garbage collection list, and *all* per-CPU locks
    173  1.124        ad  *   observed as "unowned" at least once:
    174  1.117  riastrad  *
    175  1.117  riastrad  *	nchashtbl / struct namecache::nc_hash
    176  1.117  riastrad  *
    177  1.117  riastrad  *   The per-CPU locks exist only to reduce the probability of
    178  1.117  riastrad  *   contention between readers.  We do not bind to a CPU, so
    179  1.117  riastrad  *   contention is still possible.
    180  1.117  riastrad  *
    181  1.117  riastrad  * All use serialized by struct namecache::nc_lock:
    182  1.117  riastrad  *
    183  1.117  riastrad  *	struct namecache::nc_dvp
    184  1.117  riastrad  *	struct namecache::nc_vp
    185  1.124        ad  *	struct namecache::nc_hittime (*)
    186  1.117  riastrad  *
    187  1.124        ad  * All use serialized by struct namecache_gc_lock:
    188  1.124        ad  *
    189  1.124        ad  *	struct namecache::nc_gclist
    190  1.124        ad  *
    191  1.124        ad  * (*) cache_prune reads nc_hittime unlocked, since approximate is OK.
    192  1.117  riastrad  *
    193  1.117  riastrad  * Unlocked because stable after initialization:
    194  1.117  riastrad  *
    195  1.117  riastrad  *	struct namecache::nc_dvp
    196  1.117  riastrad  *	struct namecache::nc_vp
    197  1.117  riastrad  *	struct namecache::nc_flags
    198  1.117  riastrad  *	struct namecache::nc_nlen
    199  1.117  riastrad  *	struct namecache::nc_name
    200  1.117  riastrad  *
    201  1.117  riastrad  * Unlocked because approximation is OK:
    202  1.117  riastrad  *
    203  1.117  riastrad  *	struct nchcpu::cpu_stats
    204  1.117  riastrad  *	struct nchcpu::cpu_stats_last
    205  1.117  riastrad  *
    206  1.117  riastrad  * Updates under namecache_lock or any per-CPU lock are marked with
    207  1.117  riastrad  * COUNT, while updates outside those locks are marked with COUNT_UNL.
    208  1.117  riastrad  *
    209  1.117  riastrad  * - The theory seems to have been that you could replace COUNT_UNL by
    210  1.117  riastrad  *   atomic operations -- except that doesn't help unless you also
    211  1.117  riastrad  *   replace COUNT by atomic operations, because mixing atomics and
    212  1.117  riastrad  *   nonatomics is a recipe for failure.
    213  1.117  riastrad  * - We use 32-bit per-CPU counters and 64-bit global counters under
    214  1.117  riastrad  *   the theory that 32-bit counters are less likely to be hosed by
    215  1.117  riastrad  *   nonatomic increment.
    216  1.117  riastrad  */
    217  1.117  riastrad 
    218  1.117  riastrad /*
    219  1.117  riastrad  * The comment below is preserved for posterity in case it is
    220  1.117  riastrad  * important, but it is clear that everywhere the namecache_count_*()
    221  1.117  riastrad  * functions are called, other cache_*() functions that take the same
    222  1.117  riastrad  * locks are also called, so I can't imagine how this could be a
    223  1.117  riastrad  * problem:
    224  1.103    dennis  *
    225  1.103    dennis  * N.B.: Attempting to protect COUNT_UNL() increments by taking
    226  1.103    dennis  * a per-cpu lock in the namecache_count_*() functions causes
    227  1.103    dennis  * a deadlock.  Don't do that, use atomic increments instead if
    228  1.103    dennis  * the imperfections here bug you.
    229  1.117  riastrad  */
    230  1.117  riastrad 
    231  1.117  riastrad /*
    232  1.117  riastrad  * struct nchstats_percpu:
    233  1.103    dennis  *
    234  1.117  riastrad  *	Per-CPU counters.
    235   1.77        ad  */
    236  1.103    dennis struct nchstats_percpu _NAMEI_CACHE_STATS(uint32_t);
    237  1.103    dennis 
    238  1.117  riastrad /*
    239  1.117  riastrad  * struct nchcpu:
    240  1.117  riastrad  *
    241  1.117  riastrad  *	Per-CPU namecache state: lock and per-CPU counters.
    242  1.117  riastrad  */
    243   1.77        ad struct nchcpu {
    244  1.103    dennis 	kmutex_t		cpu_lock;
    245  1.103    dennis 	struct nchstats_percpu	cpu_stats;
    246  1.103    dennis 	/* XXX maybe __cacheline_aligned would improve this? */
    247  1.103    dennis 	struct nchstats_percpu	cpu_stats_last;	/* from last sample */
    248   1.77        ad };
    249   1.77        ad 
    250   1.77        ad /*
    251   1.90  dholland  * The type for the hash code. While the hash function generates a
    252   1.90  dholland  * u32, the hash code has historically been passed around as a u_long,
    253   1.90  dholland  * and the value is modified by xor'ing a uintptr_t, so it's not
    254   1.90  dholland  * entirely clear what the best type is. For now I'll leave it
    255   1.90  dholland  * unchanged as u_long.
    256   1.90  dholland  */
    257   1.90  dholland 
    258   1.90  dholland typedef u_long nchash_t;
    259   1.90  dholland 
    260   1.90  dholland /*
    261    1.1       cgd  * Structures associated with name cacheing.
    262    1.1       cgd  */
    263   1.89     rmind 
    264  1.124        ad static kmutex_t namecache_lock __cacheline_aligned;
    265   1.89     rmind static pool_cache_t namecache_cache __read_mostly;
    266   1.89     rmind static TAILQ_HEAD(, namecache) nclruhead __cacheline_aligned;
    267   1.89     rmind 
    268   1.89     rmind static LIST_HEAD(nchashhead, namecache) *nchashtbl __read_mostly;
    269   1.89     rmind static u_long	nchash __read_mostly;
    270   1.89     rmind 
    271   1.90  dholland #define	NCHASH2(hash, dvp)	\
    272   1.90  dholland 	(((hash) ^ ((uintptr_t)(dvp) >> 3)) & nchash)
    273   1.19  sommerfe 
    274   1.89     rmind static LIST_HEAD(ncvhashhead, namecache) *ncvhashtbl __read_mostly;
    275   1.89     rmind static u_long	ncvhash __read_mostly;
    276   1.89     rmind 
    277   1.48      yamt #define	NCVHASH(vp)		(((uintptr_t)(vp) >> 3) & ncvhash)
    278   1.19  sommerfe 
    279   1.89     rmind /* Number of cache entries allocated. */
    280   1.89     rmind static long	numcache __cacheline_aligned;
    281   1.73        ad 
    282   1.89     rmind /* Garbage collection queue and number of entries pending in it. */
    283  1.124        ad static kmutex_t namecache_gc_lock __cacheline_aligned;
    284  1.124        ad static SLIST_HEAD(namecache_gc_queue, namecache) namecache_gc_queue;
    285  1.124        ad static u_int	namecache_gc_pend;
    286   1.89     rmind 
    287  1.103    dennis /* Cache effectiveness statistics.  This holds total from per-cpu stats */
    288   1.89     rmind struct nchstats	nchstats __cacheline_aligned;
    289  1.103    dennis 
    290  1.103    dennis /*
    291  1.103    dennis  * Macros to count an event, update the central stats with per-cpu
    292  1.103    dennis  * values and add current per-cpu increments to the subsystem total
    293  1.103    dennis  * last collected by cache_reclaim().
    294  1.103    dennis  */
    295  1.103    dennis #define	COUNT(cpup, f)	((cpup)->cpu_stats.f++)
    296  1.103    dennis 
    297  1.103    dennis #define	UPDATE(cpup, f) do { \
    298  1.103    dennis 	struct nchcpu *Xcpup = (cpup); \
    299  1.103    dennis 	uint32_t Xcnt = (volatile uint32_t) Xcpup->cpu_stats.f; \
    300  1.103    dennis 	nchstats.f += Xcnt - Xcpup->cpu_stats_last.f; \
    301  1.103    dennis 	Xcpup->cpu_stats_last.f = Xcnt; \
    302  1.103    dennis } while (/* CONSTCOND */ 0)
    303  1.103    dennis 
    304  1.103    dennis /* Do unlocked stats the same way. Use a different name to allow mind changes */
    305  1.103    dennis #define	COUNT_UNL(cpup, f)	COUNT((cpup), f)
    306   1.38   thorpej 
    307  1.124        ad static const int cache_lowat = 97;
    308   1.89     rmind static const int cache_hiwat = 98;
    309   1.89     rmind static const int cache_hottime = 5;	/* number of seconds */
    310   1.89     rmind static int doingcache = 1;		/* 1 => enable the cache */
    311    1.1       cgd 
    312   1.73        ad static struct evcnt cache_ev_scan;
    313   1.73        ad static struct evcnt cache_ev_gc;
    314   1.73        ad static struct evcnt cache_ev_over;
    315   1.73        ad static struct evcnt cache_ev_under;
    316   1.73        ad static struct evcnt cache_ev_forced;
    317   1.73        ad 
    318   1.89     rmind static struct namecache *cache_lookup_entry(
    319   1.91  dholland     const struct vnode *, const char *, size_t);
    320   1.73        ad static void cache_thread(void *);
    321   1.73        ad static void cache_invalidate(struct namecache *);
    322   1.73        ad static void cache_disassociate(struct namecache *);
    323   1.73        ad static void cache_reclaim(void);
    324   1.73        ad static int cache_ctor(void *, void *, int);
    325   1.73        ad static void cache_dtor(void *, void *);
    326   1.46      yamt 
    327  1.104     pooka static struct sysctllog *sysctllog;
    328  1.104     pooka static void sysctl_cache_stat_setup(void);
    329  1.104     pooka 
    330  1.108  christos SDT_PROVIDER_DEFINE(vfs);
    331  1.108  christos 
    332  1.108  christos SDT_PROBE_DEFINE1(vfs, namecache, invalidate, done, "struct vnode *");
    333  1.108  christos SDT_PROBE_DEFINE1(vfs, namecache, purge, parents, "struct vnode *");
    334  1.108  christos SDT_PROBE_DEFINE1(vfs, namecache, purge, children, "struct vnode *");
    335  1.108  christos SDT_PROBE_DEFINE2(vfs, namecache, purge, name, "char *", "size_t");
    336  1.108  christos SDT_PROBE_DEFINE1(vfs, namecache, purge, vfs, "struct mount *");
    337  1.108  christos SDT_PROBE_DEFINE3(vfs, namecache, lookup, hit, "struct vnode *",
    338  1.108  christos     "char *", "size_t");
    339  1.108  christos SDT_PROBE_DEFINE3(vfs, namecache, lookup, miss, "struct vnode *",
    340  1.108  christos     "char *", "size_t");
    341  1.108  christos SDT_PROBE_DEFINE3(vfs, namecache, lookup, toolong, "struct vnode *",
    342  1.108  christos     "char *", "size_t");
    343  1.108  christos SDT_PROBE_DEFINE2(vfs, namecache, revlookup, success, "struct vnode *",
    344  1.108  christos      "struct vnode *");
    345  1.108  christos SDT_PROBE_DEFINE2(vfs, namecache, revlookup, fail, "struct vnode *",
    346  1.108  christos      "int");
    347  1.108  christos SDT_PROBE_DEFINE2(vfs, namecache, prune, done, "int", "int");
    348  1.108  christos SDT_PROBE_DEFINE3(vfs, namecache, enter, toolong, "struct vnode *",
    349  1.108  christos     "char *", "size_t");
    350  1.108  christos SDT_PROBE_DEFINE3(vfs, namecache, enter, done, "struct vnode *",
    351  1.108  christos     "char *", "size_t");
    352  1.108  christos 
    353   1.73        ad /*
    354   1.90  dholland  * Compute the hash for an entry.
    355   1.90  dholland  *
    356   1.90  dholland  * (This is for now a wrapper around namei_hash, whose interface is
    357   1.90  dholland  * for the time being slightly inconvenient.)
    358   1.90  dholland  */
    359   1.90  dholland static nchash_t
    360   1.91  dholland cache_hash(const char *name, size_t namelen)
    361   1.90  dholland {
    362   1.90  dholland 	const char *endptr;
    363   1.90  dholland 
    364   1.91  dholland 	endptr = name + namelen;
    365   1.91  dholland 	return namei_hash(name, &endptr);
    366   1.90  dholland }
    367   1.90  dholland 
    368   1.90  dholland /*
    369   1.73        ad  * Invalidate a cache entry and enqueue it for garbage collection.
    370   1.73        ad  */
    371   1.46      yamt static void
    372   1.73        ad cache_invalidate(struct namecache *ncp)
    373   1.46      yamt {
    374   1.46      yamt 
    375  1.124        ad 	KASSERT(mutex_owned(ncp->nc_lock));
    376   1.46      yamt 
    377   1.73        ad 	if (ncp->nc_dvp != NULL) {
    378  1.108  christos 		SDT_PROBE(vfs, namecache, invalidate, done, ncp->nc_dvp,
    379  1.108  christos 		    0, 0, 0, 0);
    380  1.108  christos 
    381   1.73        ad 		ncp->nc_vp = NULL;
    382   1.73        ad 		ncp->nc_dvp = NULL;
    383  1.124        ad 		mutex_enter(&namecache_gc_lock);
    384  1.124        ad 		SLIST_INSERT_HEAD(&namecache_gc_queue, ncp, nc_gclist);
    385  1.124        ad 		namecache_gc_pend++;
    386  1.124        ad 		mutex_exit(&namecache_gc_lock);
    387   1.73        ad 	}
    388   1.73        ad }
    389   1.46      yamt 
    390   1.73        ad /*
    391   1.73        ad  * Disassociate a namecache entry from any vnodes it is attached to,
    392   1.73        ad  * and remove from the global LRU list.
    393   1.73        ad  */
    394   1.73        ad static void
    395   1.73        ad cache_disassociate(struct namecache *ncp)
    396   1.73        ad {
    397   1.73        ad 
    398  1.124        ad 	KASSERT(mutex_owned(&namecache_lock));
    399   1.73        ad 	KASSERT(ncp->nc_dvp == NULL);
    400   1.73        ad 
    401   1.73        ad 	if (ncp->nc_lru.tqe_prev != NULL) {
    402   1.73        ad 		TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
    403   1.73        ad 		ncp->nc_lru.tqe_prev = NULL;
    404   1.46      yamt 	}
    405   1.46      yamt 	if (ncp->nc_vhash.le_prev != NULL) {
    406   1.46      yamt 		LIST_REMOVE(ncp, nc_vhash);
    407   1.46      yamt 		ncp->nc_vhash.le_prev = NULL;
    408   1.46      yamt 	}
    409   1.46      yamt 	if (ncp->nc_vlist.le_prev != NULL) {
    410   1.46      yamt 		LIST_REMOVE(ncp, nc_vlist);
    411   1.46      yamt 		ncp->nc_vlist.le_prev = NULL;
    412   1.46      yamt 	}
    413   1.46      yamt 	if (ncp->nc_dvlist.le_prev != NULL) {
    414   1.46      yamt 		LIST_REMOVE(ncp, nc_dvlist);
    415   1.46      yamt 		ncp->nc_dvlist.le_prev = NULL;
    416   1.46      yamt 	}
    417   1.46      yamt }
    418   1.46      yamt 
    419   1.73        ad /*
    420   1.73        ad  * Lock all CPUs to prevent any cache lookup activity.  Conceptually,
    421  1.124        ad  * this locks out all "readers".  This is a very heavyweight operation
    422  1.124        ad  * that we only use for nchreinit().
    423   1.73        ad  */
    424   1.46      yamt static void
    425   1.73        ad cache_lock_cpus(void)
    426   1.46      yamt {
    427   1.73        ad 	CPU_INFO_ITERATOR cii;
    428   1.73        ad 	struct cpu_info *ci;
    429   1.77        ad 	struct nchcpu *cpup;
    430   1.46      yamt 
    431  1.124        ad 	/* Not necessary but don't want more than one LWP trying this. */
    432  1.124        ad 	KASSERT(mutex_owned(&namecache_lock));
    433  1.124        ad 
    434  1.103    dennis 	/*
    435  1.103    dennis 	 * Lock out all CPUs first, then harvest per-cpu stats.  This
    436  1.103    dennis 	 * is probably not quite as cache-efficient as doing the lock
    437  1.103    dennis 	 * and harvest at the same time, but allows cache_stat_sysctl()
    438  1.103    dennis 	 * to make do with a per-cpu lock.
    439  1.103    dennis 	 */
    440   1.73        ad 	for (CPU_INFO_FOREACH(cii, ci)) {
    441   1.77        ad 		cpup = ci->ci_data.cpu_nch;
    442   1.77        ad 		mutex_enter(&cpup->cpu_lock);
    443  1.103    dennis 	}
    444  1.103    dennis 	for (CPU_INFO_FOREACH(cii, ci)) {
    445  1.103    dennis 		cpup = ci->ci_data.cpu_nch;
    446  1.103    dennis 		UPDATE(cpup, ncs_goodhits);
    447  1.103    dennis 		UPDATE(cpup, ncs_neghits);
    448  1.103    dennis 		UPDATE(cpup, ncs_badhits);
    449  1.103    dennis 		UPDATE(cpup, ncs_falsehits);
    450  1.103    dennis 		UPDATE(cpup, ncs_miss);
    451  1.103    dennis 		UPDATE(cpup, ncs_long);
    452  1.103    dennis 		UPDATE(cpup, ncs_pass2);
    453  1.103    dennis 		UPDATE(cpup, ncs_2passes);
    454  1.103    dennis 		UPDATE(cpup, ncs_revhits);
    455  1.103    dennis 		UPDATE(cpup, ncs_revmiss);
    456   1.73        ad 	}
    457   1.46      yamt }
    458   1.46      yamt 
    459   1.73        ad /*
    460   1.73        ad  * Release all CPU locks.
    461   1.73        ad  */
    462   1.73        ad static void
    463   1.73        ad cache_unlock_cpus(void)
    464   1.73        ad {
    465   1.73        ad 	CPU_INFO_ITERATOR cii;
    466   1.73        ad 	struct cpu_info *ci;
    467   1.77        ad 	struct nchcpu *cpup;
    468   1.73        ad 
    469   1.73        ad 	for (CPU_INFO_FOREACH(cii, ci)) {
    470   1.77        ad 		cpup = ci->ci_data.cpu_nch;
    471   1.77        ad 		mutex_exit(&cpup->cpu_lock);
    472   1.73        ad 	}
    473   1.73        ad }
    474   1.73        ad 
    475   1.73        ad /*
    476  1.103    dennis  * Find a single cache entry and return it locked.
    477  1.103    dennis  * The caller needs to hold namecache_lock or a per-cpu lock to hold
    478  1.103    dennis  * off cache_reclaim().
    479   1.73        ad  */
    480   1.73        ad static struct namecache *
    481   1.91  dholland cache_lookup_entry(const struct vnode *dvp, const char *name, size_t namelen)
    482   1.55      yamt {
    483   1.55      yamt 	struct nchashhead *ncpp;
    484   1.55      yamt 	struct namecache *ncp;
    485   1.90  dholland 	nchash_t hash;
    486  1.124        ad 	int ticks;
    487   1.55      yamt 
    488   1.84      yamt 	KASSERT(dvp != NULL);
    489   1.91  dholland 	hash = cache_hash(name, namelen);
    490   1.90  dholland 	ncpp = &nchashtbl[NCHASH2(hash, dvp)];
    491   1.55      yamt 
    492   1.55      yamt 	LIST_FOREACH(ncp, ncpp, nc_hash) {
    493  1.105    dennis 		membar_datadep_consumer();	/* for Alpha... */
    494   1.73        ad 		if (ncp->nc_dvp != dvp ||
    495   1.91  dholland 		    ncp->nc_nlen != namelen ||
    496   1.91  dholland 		    memcmp(ncp->nc_name, name, (u_int)ncp->nc_nlen))
    497   1.73        ad 		    	continue;
    498  1.124        ad 	    	mutex_enter(ncp->nc_lock);
    499   1.77        ad 		if (__predict_true(ncp->nc_dvp == dvp)) {
    500  1.124        ad 			ticks = hardclock_ticks;
    501  1.124        ad 			if (ncp->nc_hittime != ticks) {
    502  1.124        ad 				/*
    503  1.124        ad 				 * Avoid false sharing on MP: do not store
    504  1.124        ad 				 * to *ncp unless the value changed.
    505  1.124        ad 				 */
    506  1.124        ad 				ncp->nc_hittime = ticks;
    507  1.124        ad 			}
    508  1.108  christos 			SDT_PROBE(vfs, namecache, lookup, hit, dvp,
    509  1.108  christos 			    name, namelen, 0, 0);
    510   1.73        ad 			return ncp;
    511   1.73        ad 		}
    512   1.73        ad 		/* Raced: entry has been nullified. */
    513  1.124        ad 		mutex_exit(ncp->nc_lock);
    514   1.55      yamt 	}
    515   1.55      yamt 
    516  1.108  christos 	SDT_PROBE(vfs, namecache, lookup, miss, dvp,
    517  1.108  christos 	    name, namelen, 0, 0);
    518   1.73        ad 	return NULL;
    519   1.55      yamt }
    520   1.55      yamt 
    521    1.1       cgd /*
    522    1.1       cgd  * Look for a the name in the cache. We don't do this
    523    1.1       cgd  * if the segment name is long, simply so the cache can avoid
    524    1.1       cgd  * holding long names (which would either waste space, or
    525    1.1       cgd  * add greatly to the complexity).
    526    1.1       cgd  *
    527   1.90  dholland  * Lookup is called with DVP pointing to the directory to search,
    528   1.90  dholland  * and CNP providing the name of the entry being sought: cn_nameptr
    529   1.90  dholland  * is the name, cn_namelen is its length, and cn_flags is the flags
    530   1.90  dholland  * word from the namei operation.
    531   1.90  dholland  *
    532   1.90  dholland  * DVP must be locked.
    533   1.90  dholland  *
    534   1.90  dholland  * There are three possible non-error return states:
    535   1.90  dholland  *    1. Nothing was found in the cache. Nothing is known about
    536   1.90  dholland  *       the requested name.
    537   1.90  dholland  *    2. A negative entry was found in the cache, meaning that the
    538   1.90  dholland  *       requested name definitely does not exist.
    539   1.90  dholland  *    3. A positive entry was found in the cache, meaning that the
    540   1.90  dholland  *       requested name does exist and that we are providing the
    541   1.90  dholland  *       vnode.
    542   1.90  dholland  * In these cases the results are:
    543   1.90  dholland  *    1. 0 returned; VN is set to NULL.
    544   1.90  dholland  *    2. 1 returned; VN is set to NULL.
    545   1.90  dholland  *    3. 1 returned; VN is set to the vnode found.
    546   1.90  dholland  *
    547   1.90  dholland  * The additional result argument ISWHT is set to zero, unless a
    548   1.90  dholland  * negative entry is found that was entered as a whiteout, in which
    549   1.90  dholland  * case ISWHT is set to one.
    550   1.90  dholland  *
    551   1.90  dholland  * The ISWHT_RET argument pointer may be null. In this case an
    552   1.90  dholland  * assertion is made that the whiteout flag is not set. File systems
    553   1.90  dholland  * that do not support whiteouts can/should do this.
    554   1.90  dholland  *
    555   1.90  dholland  * Filesystems that do support whiteouts should add ISWHITEOUT to
    556   1.90  dholland  * cnp->cn_flags if ISWHT comes back nonzero.
    557   1.90  dholland  *
    558   1.90  dholland  * When a vnode is returned, it is locked, as per the vnode lookup
    559   1.90  dholland  * locking protocol.
    560   1.90  dholland  *
    561   1.90  dholland  * There is no way for this function to fail, in the sense of
    562   1.90  dholland  * generating an error that requires aborting the namei operation.
    563   1.90  dholland  *
    564   1.90  dholland  * (Prior to October 2012, this function returned an integer status,
    565   1.90  dholland  * and a vnode, and mucked with the flags word in CNP for whiteouts.
    566   1.90  dholland  * The integer status was -1 for "nothing found", ENOENT for "a
    567   1.90  dholland  * negative entry found", 0 for "a positive entry found", and possibly
    568   1.90  dholland  * other errors, and the value of VN might or might not have been set
    569   1.90  dholland  * depending on what error occurred.)
    570    1.1       cgd  */
    571  1.113  riastrad bool
    572   1.91  dholland cache_lookup(struct vnode *dvp, const char *name, size_t namelen,
    573   1.91  dholland 	     uint32_t nameiop, uint32_t cnflags,
    574   1.90  dholland 	     int *iswht_ret, struct vnode **vn_ret)
    575    1.1       cgd {
    576   1.23  augustss 	struct namecache *ncp;
    577   1.20  jdolecek 	struct vnode *vp;
    578   1.77        ad 	struct nchcpu *cpup;
    579  1.113  riastrad 	int error;
    580  1.113  riastrad 	bool hit;
    581  1.103    dennis 
    582   1.90  dholland 	/* Establish default result values */
    583   1.90  dholland 	if (iswht_ret != NULL) {
    584   1.90  dholland 		*iswht_ret = 0;
    585   1.90  dholland 	}
    586   1.90  dholland 	*vn_ret = NULL;
    587   1.90  dholland 
    588   1.77        ad 	if (__predict_false(!doingcache)) {
    589  1.113  riastrad 		return false;
    590    1.8       cgd 	}
    591   1.39        pk 
    592   1.77        ad 	cpup = curcpu()->ci_data.cpu_nch;
    593  1.102    dennis 	mutex_enter(&cpup->cpu_lock);
    594  1.121  christos 	if (__predict_false(namelen > USHRT_MAX)) {
    595  1.108  christos 		SDT_PROBE(vfs, namecache, lookup, toolong, dvp,
    596  1.108  christos 		    name, namelen, 0, 0);
    597  1.103    dennis 		COUNT(cpup, ncs_long);
    598   1.77        ad 		mutex_exit(&cpup->cpu_lock);
    599   1.90  dholland 		/* found nothing */
    600  1.113  riastrad 		return false;
    601    1.1       cgd 	}
    602  1.103    dennis 
    603   1.91  dholland 	ncp = cache_lookup_entry(dvp, name, namelen);
    604   1.77        ad 	if (__predict_false(ncp == NULL)) {
    605  1.103    dennis 		COUNT(cpup, ncs_miss);
    606   1.77        ad 		mutex_exit(&cpup->cpu_lock);
    607   1.90  dholland 		/* found nothing */
    608  1.113  riastrad 		return false;
    609    1.1       cgd 	}
    610   1.91  dholland 	if ((cnflags & MAKEENTRY) == 0) {
    611  1.103    dennis 		COUNT(cpup, ncs_badhits);
    612   1.77        ad 		/*
    613   1.77        ad 		 * Last component and we are renaming or deleting,
    614   1.77        ad 		 * the cache entry is invalid, or otherwise don't
    615   1.77        ad 		 * want cache entry to exist.
    616   1.77        ad 		 */
    617   1.77        ad 		cache_invalidate(ncp);
    618  1.124        ad 		mutex_exit(ncp->nc_lock);
    619  1.102    dennis 		mutex_exit(&cpup->cpu_lock);
    620   1.90  dholland 		/* found nothing */
    621  1.113  riastrad 		return false;
    622   1.90  dholland 	}
    623  1.124        ad 	vp = ncp->nc_vp;
    624  1.124        ad 	if (__predict_false(vp == NULL)) {
    625   1.90  dholland 		if (iswht_ret != NULL) {
    626   1.90  dholland 			/*
    627   1.90  dholland 			 * Restore the ISWHITEOUT flag saved earlier.
    628   1.90  dholland 			 */
    629   1.90  dholland 			KASSERT((ncp->nc_flags & ~ISWHITEOUT) == 0);
    630   1.90  dholland 			*iswht_ret = (ncp->nc_flags & ISWHITEOUT) != 0;
    631   1.90  dholland 		} else {
    632   1.90  dholland 			KASSERT(ncp->nc_flags == 0);
    633   1.90  dholland 		}
    634   1.90  dholland 
    635   1.91  dholland 		if (__predict_true(nameiop != CREATE ||
    636   1.91  dholland 		    (cnflags & ISLASTCN) == 0)) {
    637  1.103    dennis 			COUNT(cpup, ncs_neghits);
    638   1.90  dholland 			/* found neg entry; vn is already null from above */
    639  1.113  riastrad 			hit = true;
    640   1.20  jdolecek 		} else {
    641  1.103    dennis 			COUNT(cpup, ncs_badhits);
    642   1.77        ad 			/*
    643  1.109  dholland 			 * Last component and we are preparing to create
    644  1.109  dholland 			 * the named object, so flush the negative cache
    645  1.109  dholland 			 * entry.
    646   1.77        ad 			 */
    647   1.77        ad 			cache_invalidate(ncp);
    648   1.90  dholland 			/* found nothing */
    649  1.113  riastrad 			hit = false;
    650   1.20  jdolecek 		}
    651  1.124        ad 		mutex_exit(ncp->nc_lock);
    652  1.103    dennis 		mutex_exit(&cpup->cpu_lock);
    653  1.113  riastrad 		return hit;
    654   1.20  jdolecek 	}
    655  1.124        ad 	KASSERT(vp->v_interlock == ncp->nc_lock);
    656  1.102    dennis 	mutex_exit(&cpup->cpu_lock);
    657  1.103    dennis 
    658  1.103    dennis 	/*
    659  1.111   hannken 	 * Unlocked except for the vnode interlock.  Call vcache_tryvget().
    660  1.103    dennis 	 */
    661  1.111   hannken 	error = vcache_tryvget(vp);
    662   1.92   hannken 	if (error) {
    663   1.92   hannken 		KASSERT(error == EBUSY);
    664   1.92   hannken 		/*
    665   1.92   hannken 		 * This vnode is being cleaned out.
    666   1.92   hannken 		 * XXX badhits?
    667   1.92   hannken 		 */
    668  1.103    dennis 		COUNT_UNL(cpup, ncs_falsehits);
    669   1.92   hannken 		/* found nothing */
    670  1.113  riastrad 		return false;
    671   1.77        ad 	}
    672  1.101  christos 
    673  1.103    dennis 	COUNT_UNL(cpup, ncs_goodhits);
    674  1.101  christos 	/* found it */
    675  1.101  christos 	*vn_ret = vp;
    676  1.113  riastrad 	return true;
    677    1.1       cgd }
    678    1.1       cgd 
    679  1.103    dennis 
    680  1.103    dennis /*
    681  1.103    dennis  * Cut-'n-pasted version of the above without the nameiop argument.
    682  1.103    dennis  */
    683  1.113  riastrad bool
    684   1.91  dholland cache_lookup_raw(struct vnode *dvp, const char *name, size_t namelen,
    685   1.91  dholland 		 uint32_t cnflags,
    686   1.90  dholland 		 int *iswht_ret, struct vnode **vn_ret)
    687   1.61      yamt {
    688   1.61      yamt 	struct namecache *ncp;
    689   1.61      yamt 	struct vnode *vp;
    690   1.77        ad 	struct nchcpu *cpup;
    691  1.101  christos 	int error;
    692   1.61      yamt 
    693   1.90  dholland 	/* Establish default results. */
    694   1.90  dholland 	if (iswht_ret != NULL) {
    695   1.90  dholland 		*iswht_ret = 0;
    696   1.90  dholland 	}
    697   1.90  dholland 	*vn_ret = NULL;
    698   1.90  dholland 
    699   1.77        ad 	if (__predict_false(!doingcache)) {
    700   1.90  dholland 		/* found nothing */
    701  1.113  riastrad 		return false;
    702   1.61      yamt 	}
    703   1.61      yamt 
    704   1.77        ad 	cpup = curcpu()->ci_data.cpu_nch;
    705  1.102    dennis 	mutex_enter(&cpup->cpu_lock);
    706  1.121  christos 	if (__predict_false(namelen > USHRT_MAX)) {
    707  1.103    dennis 		COUNT(cpup, ncs_long);
    708   1.77        ad 		mutex_exit(&cpup->cpu_lock);
    709   1.90  dholland 		/* found nothing */
    710  1.113  riastrad 		return false;
    711   1.61      yamt 	}
    712   1.91  dholland 	ncp = cache_lookup_entry(dvp, name, namelen);
    713   1.77        ad 	if (__predict_false(ncp == NULL)) {
    714  1.103    dennis 		COUNT(cpup, ncs_miss);
    715   1.77        ad 		mutex_exit(&cpup->cpu_lock);
    716   1.90  dholland 		/* found nothing */
    717  1.113  riastrad 		return false;
    718   1.61      yamt 	}
    719   1.61      yamt 	vp = ncp->nc_vp;
    720   1.61      yamt 	if (vp == NULL) {
    721   1.61      yamt 		/*
    722   1.61      yamt 		 * Restore the ISWHITEOUT flag saved earlier.
    723   1.61      yamt 		 */
    724   1.90  dholland 		if (iswht_ret != NULL) {
    725   1.90  dholland 			KASSERT((ncp->nc_flags & ~ISWHITEOUT) == 0);
    726   1.90  dholland 			/*cnp->cn_flags |= ncp->nc_flags;*/
    727   1.90  dholland 			*iswht_ret = (ncp->nc_flags & ISWHITEOUT) != 0;
    728   1.90  dholland 		}
    729  1.103    dennis 		COUNT(cpup, ncs_neghits);
    730  1.124        ad 		mutex_exit(ncp->nc_lock);
    731  1.101  christos 		mutex_exit(&cpup->cpu_lock);
    732   1.90  dholland 		/* found negative entry; vn is already null from above */
    733  1.113  riastrad 		return true;
    734   1.61      yamt 	}
    735  1.124        ad 	KASSERT(vp->v_interlock == ncp->nc_lock);
    736  1.102    dennis 	mutex_exit(&cpup->cpu_lock);
    737  1.103    dennis 
    738  1.103    dennis 	/*
    739  1.111   hannken 	 * Unlocked except for the vnode interlock.  Call vcache_tryvget().
    740  1.103    dennis 	 */
    741  1.111   hannken 	error = vcache_tryvget(vp);
    742   1.92   hannken 	if (error) {
    743   1.92   hannken 		KASSERT(error == EBUSY);
    744   1.92   hannken 		/*
    745   1.92   hannken 		 * This vnode is being cleaned out.
    746   1.92   hannken 		 * XXX badhits?
    747   1.92   hannken 		 */
    748  1.103    dennis 		COUNT_UNL(cpup, ncs_falsehits);
    749   1.92   hannken 		/* found nothing */
    750  1.113  riastrad 		return false;
    751   1.61      yamt 	}
    752  1.101  christos 
    753  1.103    dennis 	COUNT_UNL(cpup, ncs_goodhits); /* XXX can be "badhits" */
    754  1.101  christos 	/* found it */
    755  1.101  christos 	*vn_ret = vp;
    756  1.113  riastrad 	return true;
    757   1.61      yamt }
    758   1.61      yamt 
    759    1.1       cgd /*
    760   1.19  sommerfe  * Scan cache looking for name of directory entry pointing at vp.
    761   1.19  sommerfe  *
    762   1.86   hannken  * If the lookup succeeds the vnode is referenced and stored in dvpp.
    763   1.19  sommerfe  *
    764   1.19  sommerfe  * If bufp is non-NULL, also place the name in the buffer which starts
    765   1.19  sommerfe  * at bufp, immediately before *bpp, and move bpp backwards to point
    766   1.19  sommerfe  * at the start of it.  (Yes, this is a little baroque, but it's done
    767   1.19  sommerfe  * this way to cater to the whims of getcwd).
    768   1.19  sommerfe  *
    769   1.19  sommerfe  * Returns 0 on success, -1 on cache miss, positive errno on failure.
    770   1.19  sommerfe  */
    771   1.19  sommerfe int
    772   1.34     enami cache_revlookup(struct vnode *vp, struct vnode **dvpp, char **bpp, char *bufp)
    773   1.19  sommerfe {
    774   1.19  sommerfe 	struct namecache *ncp;
    775   1.19  sommerfe 	struct vnode *dvp;
    776  1.103    dennis 	struct ncvhashhead *nvcpp;
    777   1.95     joerg 	struct nchcpu *cpup;
    778   1.34     enami 	char *bp;
    779   1.86   hannken 	int error, nlen;
    780  1.124        ad 	bool locked, again;
    781   1.34     enami 
    782   1.19  sommerfe 	if (!doingcache)
    783   1.19  sommerfe 		goto out;
    784   1.19  sommerfe 
    785   1.30       chs 	nvcpp = &ncvhashtbl[NCVHASH(vp)];
    786  1.103    dennis 
    787  1.103    dennis 	/*
    788  1.103    dennis 	 * We increment counters in the local CPU's per-cpu stats.
    789  1.103    dennis 	 * We don't take the per-cpu lock, however, since this function
    790  1.103    dennis 	 * is the only place these counters are incremented so no one
    791  1.103    dennis 	 * will be racing with us to increment them.
    792  1.103    dennis 	 */
    793  1.124        ad 	again = false;
    794  1.124        ad  retry:
    795   1.95     joerg 	cpup = curcpu()->ci_data.cpu_nch;
    796  1.124        ad 	mutex_enter(&namecache_lock);
    797   1.27       chs 	LIST_FOREACH(ncp, nvcpp, nc_vhash) {
    798  1.124        ad 		mutex_enter(ncp->nc_lock);
    799   1.34     enami 		if (ncp->nc_vp == vp &&
    800   1.34     enami 		    (dvp = ncp->nc_dvp) != NULL &&
    801   1.47      yamt 		    dvp != vp) { 		/* avoid pesky . entries.. */
    802   1.34     enami 
    803   1.19  sommerfe #ifdef DIAGNOSTIC
    804   1.34     enami 			if (ncp->nc_nlen == 1 &&
    805   1.34     enami 			    ncp->nc_name[0] == '.')
    806   1.19  sommerfe 				panic("cache_revlookup: found entry for .");
    807   1.19  sommerfe 
    808   1.34     enami 			if (ncp->nc_nlen == 2 &&
    809   1.34     enami 			    ncp->nc_name[0] == '.' &&
    810   1.34     enami 			    ncp->nc_name[1] == '.')
    811   1.19  sommerfe 				panic("cache_revlookup: found entry for ..");
    812   1.19  sommerfe #endif
    813  1.103    dennis 			COUNT(cpup, ncs_revhits);
    814   1.86   hannken 			nlen = ncp->nc_nlen;
    815   1.19  sommerfe 
    816   1.19  sommerfe 			if (bufp) {
    817   1.19  sommerfe 				bp = *bpp;
    818   1.86   hannken 				bp -= nlen;
    819   1.19  sommerfe 				if (bp <= bufp) {
    820   1.34     enami 					*dvpp = NULL;
    821  1.124        ad 					mutex_exit(ncp->nc_lock);
    822  1.124        ad 					mutex_exit(&namecache_lock);
    823  1.108  christos 					SDT_PROBE(vfs, namecache, revlookup,
    824  1.108  christos 					    fail, vp, ERANGE, 0, 0, 0);
    825   1.34     enami 					return (ERANGE);
    826   1.19  sommerfe 				}
    827   1.86   hannken 				memcpy(bp, ncp->nc_name, nlen);
    828   1.19  sommerfe 				*bpp = bp;
    829   1.19  sommerfe 			}
    830   1.34     enami 
    831  1.124        ad 
    832  1.124        ad 			KASSERT(ncp->nc_lock != dvp->v_interlock);
    833  1.124        ad 			locked = mutex_tryenter(dvp->v_interlock);
    834  1.124        ad 			mutex_exit(ncp->nc_lock);
    835  1.124        ad 			mutex_exit(&namecache_lock);
    836  1.124        ad 			if (!locked) {
    837  1.124        ad 				if (again) {
    838  1.124        ad 					kpause("nchrace", false, 1, NULL);
    839  1.124        ad 				}
    840  1.124        ad 				again = true;
    841  1.124        ad 				goto retry;
    842  1.124        ad 			}
    843  1.111   hannken 			error = vcache_tryvget(dvp);
    844   1.92   hannken 			if (error) {
    845   1.92   hannken 				KASSERT(error == EBUSY);
    846   1.92   hannken 				if (bufp)
    847   1.92   hannken 					(*bpp) += nlen;
    848   1.92   hannken 				*dvpp = NULL;
    849  1.108  christos 				SDT_PROBE(vfs, namecache, revlookup, fail, vp,
    850  1.108  christos 				    error, 0, 0, 0);
    851   1.92   hannken 				return -1;
    852   1.86   hannken 			}
    853   1.19  sommerfe 			*dvpp = dvp;
    854  1.108  christos 			SDT_PROBE(vfs, namecache, revlookup, success, vp, dvp,
    855  1.108  christos 			    0, 0, 0);
    856   1.34     enami 			return (0);
    857   1.19  sommerfe 		}
    858  1.124        ad 		mutex_exit(ncp->nc_lock);
    859   1.19  sommerfe 	}
    860  1.103    dennis 	COUNT(cpup, ncs_revmiss);
    861  1.124        ad 	mutex_exit(&namecache_lock);
    862   1.19  sommerfe  out:
    863   1.34     enami 	*dvpp = NULL;
    864   1.34     enami 	return (-1);
    865   1.19  sommerfe }
    866   1.19  sommerfe 
    867   1.19  sommerfe /*
    868    1.1       cgd  * Add an entry to the cache
    869    1.1       cgd  */
    870   1.13  christos void
    871   1.91  dholland cache_enter(struct vnode *dvp, struct vnode *vp,
    872   1.91  dholland 	    const char *name, size_t namelen, uint32_t cnflags)
    873    1.1       cgd {
    874   1.23  augustss 	struct namecache *ncp;
    875   1.59      yamt 	struct namecache *oncp;
    876   1.23  augustss 	struct nchashhead *ncpp;
    877   1.23  augustss 	struct ncvhashhead *nvcpp;
    878   1.90  dholland 	nchash_t hash;
    879    1.1       cgd 
    880   1.89     rmind 	/* First, check whether we can/should add a cache entry. */
    881   1.91  dholland 	if ((cnflags & MAKEENTRY) == 0 ||
    882  1.121  christos 	    __predict_false(namelen > USHRT_MAX || !doingcache)) {
    883  1.108  christos 		SDT_PROBE(vfs, namecache, enter, toolong, vp, name, namelen,
    884  1.108  christos 		    0, 0);
    885    1.1       cgd 		return;
    886   1.89     rmind 	}
    887   1.58      yamt 
    888  1.108  christos 	SDT_PROBE(vfs, namecache, enter, done, vp, name, namelen, 0, 0);
    889   1.73        ad 	if (numcache > desiredvnodes) {
    890  1.124        ad 		mutex_enter(&namecache_lock);
    891   1.73        ad 		cache_ev_forced.ev_count++;
    892   1.73        ad 		cache_reclaim();
    893  1.124        ad 		mutex_exit(&namecache_lock);
    894   1.39        pk 	}
    895   1.57        pk 
    896  1.121  christos 	if (namelen > NCHNAMLEN) {
    897  1.121  christos 		ncp = kmem_alloc(sizeof(*ncp) + namelen, KM_SLEEP);
    898  1.121  christos 		cache_ctor(NULL, ncp, 0);
    899  1.121  christos 	} else
    900  1.122      maya 		ncp = pool_cache_get(namecache_cache, PR_WAITOK);
    901  1.122      maya 
    902  1.124        ad 	mutex_enter(&namecache_lock);
    903   1.73        ad 	numcache++;
    904   1.73        ad 
    905   1.59      yamt 	/*
    906   1.59      yamt 	 * Concurrent lookups in the same directory may race for a
    907   1.59      yamt 	 * cache entry.  if there's a duplicated entry, free it.
    908   1.59      yamt 	 */
    909   1.91  dholland 	oncp = cache_lookup_entry(dvp, name, namelen);
    910   1.59      yamt 	if (oncp) {
    911   1.73        ad 		cache_invalidate(oncp);
    912  1.124        ad 		mutex_exit(oncp->nc_lock);
    913   1.59      yamt 	}
    914   1.59      yamt 
    915   1.34     enami 	/* Grab the vnode we just found. */
    916    1.5   mycroft 	ncp->nc_vp = vp;
    917   1.73        ad 	ncp->nc_flags = 0;
    918   1.73        ad 	ncp->nc_hittime = 0;
    919   1.47      yamt 	if (vp == NULL) {
    920   1.11   mycroft 		/*
    921   1.11   mycroft 		 * For negative hits, save the ISWHITEOUT flag so we can
    922   1.11   mycroft 		 * restore it later when the cache entry is used again.
    923   1.11   mycroft 		 */
    924   1.91  dholland 		ncp->nc_flags = cnflags & ISWHITEOUT;
    925  1.124        ad 		ncp->nc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    926  1.124        ad 	} else {
    927  1.124        ad 		ncp->nc_lock = vp->v_interlock;
    928  1.124        ad 		mutex_obj_hold(ncp->nc_lock);
    929   1.11   mycroft 	}
    930   1.89     rmind 
    931   1.34     enami 	/* Fill in cache info. */
    932  1.124        ad 	mutex_enter(ncp->nc_lock);
    933    1.5   mycroft 	ncp->nc_dvp = dvp;
    934  1.112   hannken 	LIST_INSERT_HEAD(&VNODE_TO_VIMPL(dvp)->vi_dnclist, ncp, nc_dvlist);
    935   1.46      yamt 	if (vp)
    936  1.112   hannken 		LIST_INSERT_HEAD(&VNODE_TO_VIMPL(vp)->vi_nclist, ncp, nc_vlist);
    937   1.73        ad 	else {
    938   1.73        ad 		ncp->nc_vlist.le_prev = NULL;
    939   1.73        ad 		ncp->nc_vlist.le_next = NULL;
    940   1.73        ad 	}
    941  1.121  christos 	KASSERT(namelen <= USHRT_MAX);
    942   1.91  dholland 	ncp->nc_nlen = namelen;
    943   1.91  dholland 	memcpy(ncp->nc_name, name, (unsigned)ncp->nc_nlen);
    944   1.73        ad 	TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
    945   1.91  dholland 	hash = cache_hash(name, namelen);
    946   1.90  dholland 	ncpp = &nchashtbl[NCHASH2(hash, dvp)];
    947   1.73        ad 
    948   1.73        ad 	/*
    949   1.73        ad 	 * Flush updates before making visible in table.  No need for a
    950   1.73        ad 	 * memory barrier on the other side: to see modifications the
    951   1.73        ad 	 * list must be followed, meaning a dependent pointer load.
    952   1.74        ad 	 * The below is LIST_INSERT_HEAD() inlined, with the memory
    953   1.74        ad 	 * barrier included in the correct place.
    954   1.73        ad 	 */
    955   1.74        ad 	if ((ncp->nc_hash.le_next = ncpp->lh_first) != NULL)
    956   1.74        ad 		ncpp->lh_first->nc_hash.le_prev = &ncp->nc_hash.le_next;
    957   1.74        ad 	ncp->nc_hash.le_prev = &ncpp->lh_first;
    958   1.73        ad 	membar_producer();
    959   1.74        ad 	ncpp->lh_first = ncp;
    960   1.19  sommerfe 
    961   1.34     enami 	ncp->nc_vhash.le_prev = NULL;
    962   1.34     enami 	ncp->nc_vhash.le_next = NULL;
    963   1.34     enami 
    964   1.19  sommerfe 	/*
    965   1.19  sommerfe 	 * Create reverse-cache entries (used in getcwd) for directories.
    966   1.66  christos 	 * (and in linux procfs exe node)
    967   1.19  sommerfe 	 */
    968   1.33     enami 	if (vp != NULL &&
    969   1.33     enami 	    vp != dvp &&
    970   1.29      fvdl #ifndef NAMECACHE_ENTER_REVERSE
    971   1.33     enami 	    vp->v_type == VDIR &&
    972   1.29      fvdl #endif
    973   1.33     enami 	    (ncp->nc_nlen > 2 ||
    974   1.33     enami 	    (ncp->nc_nlen > 1 && ncp->nc_name[1] != '.') ||
    975   1.33     enami 	    (/* ncp->nc_nlen > 0 && */ ncp->nc_name[0] != '.'))) {
    976   1.30       chs 		nvcpp = &ncvhashtbl[NCVHASH(vp)];
    977   1.19  sommerfe 		LIST_INSERT_HEAD(nvcpp, ncp, nc_vhash);
    978   1.19  sommerfe 	}
    979  1.124        ad 	mutex_exit(ncp->nc_lock);
    980  1.124        ad 	mutex_exit(&namecache_lock);
    981    1.1       cgd }
    982    1.1       cgd 
    983    1.1       cgd /*
    984    1.1       cgd  * Name cache initialization, from vfs_init() when we are booting
    985    1.1       cgd  */
    986   1.13  christos void
    987   1.34     enami nchinit(void)
    988    1.1       cgd {
    989   1.73        ad 	int error;
    990    1.1       cgd 
    991   1.89     rmind 	TAILQ_INIT(&nclruhead);
    992  1.121  christos 	namecache_cache = pool_cache_init(sizeof(struct namecache) + NCHNAMLEN,
    993   1.73        ad 	    coherency_unit, 0, 0, "ncache", NULL, IPL_NONE, cache_ctor,
    994   1.73        ad 	    cache_dtor, NULL);
    995   1.71        ad 	KASSERT(namecache_cache != NULL);
    996   1.71        ad 
    997  1.124        ad 	mutex_init(&namecache_lock, MUTEX_DEFAULT, IPL_NONE);
    998  1.124        ad 	mutex_init(&namecache_gc_lock, MUTEX_DEFAULT, IPL_NONE);
    999   1.76        ad 	nchashtbl = hashinit(desiredvnodes, HASH_LIST, true, &nchash);
   1000   1.26        ad 	ncvhashtbl =
   1001   1.29      fvdl #ifdef NAMECACHE_ENTER_REVERSE
   1002   1.76        ad 	    hashinit(desiredvnodes, HASH_LIST, true, &ncvhash);
   1003   1.29      fvdl #else
   1004   1.76        ad 	    hashinit(desiredvnodes/8, HASH_LIST, true, &ncvhash);
   1005   1.29      fvdl #endif
   1006   1.73        ad 
   1007   1.73        ad 	error = kthread_create(PRI_VM, KTHREAD_MPSAFE, NULL, cache_thread,
   1008   1.73        ad 	    NULL, NULL, "cachegc");
   1009   1.73        ad 	if (error != 0)
   1010   1.73        ad 		panic("nchinit %d", error);
   1011   1.73        ad 
   1012   1.73        ad 	evcnt_attach_dynamic(&cache_ev_scan, EVCNT_TYPE_MISC, NULL,
   1013   1.73        ad 	   "namecache", "entries scanned");
   1014   1.73        ad 	evcnt_attach_dynamic(&cache_ev_gc, EVCNT_TYPE_MISC, NULL,
   1015   1.73        ad 	   "namecache", "entries collected");
   1016   1.73        ad 	evcnt_attach_dynamic(&cache_ev_over, EVCNT_TYPE_MISC, NULL,
   1017   1.73        ad 	   "namecache", "over scan target");
   1018   1.73        ad 	evcnt_attach_dynamic(&cache_ev_under, EVCNT_TYPE_MISC, NULL,
   1019   1.73        ad 	   "namecache", "under scan target");
   1020   1.73        ad 	evcnt_attach_dynamic(&cache_ev_forced, EVCNT_TYPE_MISC, NULL,
   1021   1.73        ad 	   "namecache", "forced reclaims");
   1022  1.104     pooka 
   1023  1.104     pooka 	sysctl_cache_stat_setup();
   1024   1.73        ad }
   1025   1.73        ad 
   1026   1.73        ad static int
   1027   1.73        ad cache_ctor(void *arg, void *obj, int flag)
   1028   1.73        ad {
   1029   1.73        ad 
   1030   1.73        ad 	return 0;
   1031   1.73        ad }
   1032   1.73        ad 
   1033   1.73        ad static void
   1034   1.73        ad cache_dtor(void *arg, void *obj)
   1035   1.73        ad {
   1036   1.73        ad 
   1037   1.73        ad }
   1038   1.73        ad 
   1039   1.73        ad /*
   1040   1.73        ad  * Called once for each CPU in the system as attached.
   1041   1.73        ad  */
   1042   1.73        ad void
   1043   1.73        ad cache_cpu_init(struct cpu_info *ci)
   1044   1.73        ad {
   1045   1.77        ad 	struct nchcpu *cpup;
   1046   1.77        ad 	size_t sz;
   1047   1.73        ad 
   1048   1.77        ad 	sz = roundup2(sizeof(*cpup), coherency_unit) + coherency_unit;
   1049   1.77        ad 	cpup = kmem_zalloc(sz, KM_SLEEP);
   1050   1.77        ad 	cpup = (void *)roundup2((uintptr_t)cpup, coherency_unit);
   1051   1.77        ad 	mutex_init(&cpup->cpu_lock, MUTEX_DEFAULT, IPL_NONE);
   1052   1.77        ad 	ci->ci_data.cpu_nch = cpup;
   1053   1.30       chs }
   1054   1.30       chs 
   1055   1.30       chs /*
   1056   1.30       chs  * Name cache reinitialization, for when the maximum number of vnodes increases.
   1057   1.30       chs  */
   1058   1.30       chs void
   1059   1.34     enami nchreinit(void)
   1060   1.30       chs {
   1061   1.30       chs 	struct namecache *ncp;
   1062   1.30       chs 	struct nchashhead *oldhash1, *hash1;
   1063   1.30       chs 	struct ncvhashhead *oldhash2, *hash2;
   1064   1.36   thorpej 	u_long i, oldmask1, oldmask2, mask1, mask2;
   1065   1.30       chs 
   1066   1.76        ad 	hash1 = hashinit(desiredvnodes, HASH_LIST, true, &mask1);
   1067   1.30       chs 	hash2 =
   1068   1.30       chs #ifdef NAMECACHE_ENTER_REVERSE
   1069   1.76        ad 	    hashinit(desiredvnodes, HASH_LIST, true, &mask2);
   1070   1.30       chs #else
   1071   1.76        ad 	    hashinit(desiredvnodes/8, HASH_LIST, true, &mask2);
   1072   1.30       chs #endif
   1073  1.124        ad 	mutex_enter(&namecache_lock);
   1074   1.73        ad 	cache_lock_cpus();
   1075   1.30       chs 	oldhash1 = nchashtbl;
   1076   1.30       chs 	oldmask1 = nchash;
   1077   1.30       chs 	nchashtbl = hash1;
   1078   1.30       chs 	nchash = mask1;
   1079   1.30       chs 	oldhash2 = ncvhashtbl;
   1080   1.30       chs 	oldmask2 = ncvhash;
   1081   1.30       chs 	ncvhashtbl = hash2;
   1082   1.30       chs 	ncvhash = mask2;
   1083   1.30       chs 	for (i = 0; i <= oldmask1; i++) {
   1084   1.30       chs 		while ((ncp = LIST_FIRST(&oldhash1[i])) != NULL) {
   1085   1.30       chs 			LIST_REMOVE(ncp, nc_hash);
   1086   1.30       chs 			ncp->nc_hash.le_prev = NULL;
   1087   1.30       chs 		}
   1088   1.30       chs 	}
   1089   1.30       chs 	for (i = 0; i <= oldmask2; i++) {
   1090   1.30       chs 		while ((ncp = LIST_FIRST(&oldhash2[i])) != NULL) {
   1091   1.30       chs 			LIST_REMOVE(ncp, nc_vhash);
   1092   1.30       chs 			ncp->nc_vhash.le_prev = NULL;
   1093   1.30       chs 		}
   1094   1.30       chs 	}
   1095   1.73        ad 	cache_unlock_cpus();
   1096  1.124        ad 	mutex_exit(&namecache_lock);
   1097   1.76        ad 	hashdone(oldhash1, HASH_LIST, oldmask1);
   1098   1.76        ad 	hashdone(oldhash2, HASH_LIST, oldmask2);
   1099    1.1       cgd }
   1100    1.1       cgd 
   1101    1.1       cgd /*
   1102    1.1       cgd  * Cache flush, a particular vnode; called when a vnode is renamed to
   1103    1.1       cgd  * hide entries that would now be invalid
   1104    1.1       cgd  */
   1105   1.13  christos void
   1106   1.91  dholland cache_purge1(struct vnode *vp, const char *name, size_t namelen, int flags)
   1107    1.1       cgd {
   1108   1.46      yamt 	struct namecache *ncp, *ncnext;
   1109    1.1       cgd 
   1110  1.124        ad 	mutex_enter(&namecache_lock);
   1111   1.55      yamt 	if (flags & PURGE_PARENTS) {
   1112  1.108  christos 		SDT_PROBE(vfs, namecache, purge, parents, vp, 0, 0, 0, 0);
   1113  1.108  christos 
   1114  1.112   hannken 		for (ncp = LIST_FIRST(&VNODE_TO_VIMPL(vp)->vi_nclist);
   1115  1.112   hannken 		    ncp != NULL; ncp = ncnext) {
   1116   1.55      yamt 			ncnext = LIST_NEXT(ncp, nc_vlist);
   1117  1.124        ad 			mutex_enter(ncp->nc_lock);
   1118   1.73        ad 			cache_invalidate(ncp);
   1119  1.124        ad 			mutex_exit(ncp->nc_lock);
   1120   1.73        ad 			cache_disassociate(ncp);
   1121   1.55      yamt 		}
   1122   1.55      yamt 	}
   1123   1.55      yamt 	if (flags & PURGE_CHILDREN) {
   1124  1.108  christos 		SDT_PROBE(vfs, namecache, purge, children, vp, 0, 0, 0, 0);
   1125  1.112   hannken 		for (ncp = LIST_FIRST(&VNODE_TO_VIMPL(vp)->vi_dnclist);
   1126  1.112   hannken 		    ncp != NULL; ncp = ncnext) {
   1127   1.55      yamt 			ncnext = LIST_NEXT(ncp, nc_dvlist);
   1128  1.124        ad 			mutex_enter(ncp->nc_lock);
   1129   1.73        ad 			cache_invalidate(ncp);
   1130  1.124        ad 			mutex_exit(ncp->nc_lock);
   1131   1.73        ad 			cache_disassociate(ncp);
   1132   1.55      yamt 		}
   1133   1.46      yamt 	}
   1134   1.91  dholland 	if (name != NULL) {
   1135  1.108  christos 		SDT_PROBE(vfs, namecache, purge, name, name, namelen, 0, 0, 0);
   1136   1.91  dholland 		ncp = cache_lookup_entry(vp, name, namelen);
   1137   1.55      yamt 		if (ncp) {
   1138   1.73        ad 			cache_invalidate(ncp);
   1139  1.124        ad 			mutex_exit(ncp->nc_lock);
   1140   1.73        ad 			cache_disassociate(ncp);
   1141   1.55      yamt 		}
   1142   1.46      yamt 	}
   1143  1.124        ad 	mutex_exit(&namecache_lock);
   1144    1.1       cgd }
   1145    1.1       cgd 
   1146    1.1       cgd /*
   1147    1.1       cgd  * Cache flush, a whole filesystem; called when filesys is umounted to
   1148   1.27       chs  * remove entries that would now be invalid.
   1149    1.1       cgd  */
   1150   1.13  christos void
   1151   1.34     enami cache_purgevfs(struct mount *mp)
   1152    1.1       cgd {
   1153   1.23  augustss 	struct namecache *ncp, *nxtcp;
   1154    1.1       cgd 
   1155  1.108  christos 	SDT_PROBE(vfs, namecache, purge, vfs, mp, 0, 0, 0, 0);
   1156  1.124        ad 	mutex_enter(&namecache_lock);
   1157   1.73        ad 	for (ncp = TAILQ_FIRST(&nclruhead); ncp != NULL; ncp = nxtcp) {
   1158   1.73        ad 		nxtcp = TAILQ_NEXT(ncp, nc_lru);
   1159  1.124        ad 		mutex_enter(ncp->nc_lock);
   1160   1.73        ad 		if (ncp->nc_dvp != NULL && ncp->nc_dvp->v_mount == mp) {
   1161   1.73        ad 			/* Free the resources we had. */
   1162   1.73        ad 			cache_invalidate(ncp);
   1163   1.73        ad 			cache_disassociate(ncp);
   1164   1.73        ad 		}
   1165  1.124        ad 		mutex_exit(ncp->nc_lock);
   1166   1.73        ad 	}
   1167   1.73        ad 	cache_reclaim();
   1168  1.124        ad 	mutex_exit(&namecache_lock);
   1169   1.73        ad }
   1170   1.73        ad 
   1171   1.73        ad /*
   1172  1.116  riastrad  * Scan global list invalidating entries until we meet a preset target.
   1173   1.73        ad  * Prefer to invalidate entries that have not scored a hit within
   1174   1.73        ad  * cache_hottime seconds.  We sort the LRU list only for this routine's
   1175   1.73        ad  * benefit.
   1176   1.73        ad  */
   1177   1.73        ad static void
   1178   1.73        ad cache_prune(int incache, int target)
   1179   1.73        ad {
   1180   1.73        ad 	struct namecache *ncp, *nxtcp, *sentinel;
   1181   1.73        ad 	int items, recent, tryharder;
   1182   1.73        ad 
   1183  1.124        ad 	KASSERT(mutex_owned(&namecache_lock));
   1184   1.73        ad 
   1185  1.108  christos 	SDT_PROBE(vfs, namecache, prune, done, incache, target, 0, 0, 0);
   1186   1.73        ad 	items = 0;
   1187   1.73        ad 	tryharder = 0;
   1188   1.73        ad 	recent = hardclock_ticks - hz * cache_hottime;
   1189   1.73        ad 	sentinel = NULL;
   1190   1.27       chs 	for (ncp = TAILQ_FIRST(&nclruhead); ncp != NULL; ncp = nxtcp) {
   1191   1.73        ad 		if (incache <= target)
   1192   1.73        ad 			break;
   1193   1.73        ad 		items++;
   1194   1.27       chs 		nxtcp = TAILQ_NEXT(ncp, nc_lru);
   1195   1.73        ad 		if (ncp == sentinel) {
   1196   1.73        ad 			/*
   1197   1.73        ad 			 * If we looped back on ourself, then ignore
   1198   1.73        ad 			 * recent entries and purge whatever we find.
   1199   1.73        ad 			 */
   1200   1.73        ad 			tryharder = 1;
   1201    1.5   mycroft 		}
   1202   1.93   hannken 		if (ncp->nc_dvp == NULL)
   1203   1.93   hannken 			continue;
   1204   1.81      yamt 		if (!tryharder && (ncp->nc_hittime - recent) > 0) {
   1205   1.73        ad 			if (sentinel == NULL)
   1206   1.73        ad 				sentinel = ncp;
   1207   1.73        ad 			TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
   1208   1.73        ad 			TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
   1209   1.73        ad 			continue;
   1210   1.73        ad 		}
   1211  1.124        ad 		mutex_enter(ncp->nc_lock);
   1212   1.73        ad 		if (ncp->nc_dvp != NULL) {
   1213   1.73        ad 			cache_invalidate(ncp);
   1214   1.73        ad 			cache_disassociate(ncp);
   1215   1.73        ad 			incache--;
   1216   1.73        ad 		}
   1217  1.124        ad 		mutex_exit(ncp->nc_lock);
   1218   1.73        ad 	}
   1219   1.73        ad 	cache_ev_scan.ev_count += items;
   1220   1.73        ad }
   1221   1.73        ad 
   1222   1.73        ad /*
   1223   1.73        ad  * Collect dead cache entries from all CPUs and garbage collect.
   1224   1.73        ad  */
   1225   1.73        ad static void
   1226   1.73        ad cache_reclaim(void)
   1227   1.73        ad {
   1228  1.124        ad 	CPU_INFO_ITERATOR cii;
   1229  1.124        ad 	struct cpu_info *ci;
   1230  1.124        ad 	struct nchcpu *cpup;
   1231  1.124        ad 	struct namecache_gc_queue queue;
   1232   1.73        ad 	struct namecache *ncp, *next;
   1233   1.73        ad 	int items;
   1234   1.73        ad 
   1235  1.124        ad 	KASSERT(mutex_owned(&namecache_lock));
   1236   1.73        ad 
   1237   1.73        ad 	/*
   1238   1.73        ad 	 * If the number of extant entries not awaiting garbage collection
   1239   1.73        ad 	 * exceeds the high water mark, then reclaim stale entries until we
   1240   1.73        ad 	 * reach our low water mark.
   1241   1.73        ad 	 */
   1242  1.124        ad 	items = numcache - namecache_gc_pend;
   1243   1.73        ad 	if (items > (uint64_t)desiredvnodes * cache_hiwat / 100) {
   1244   1.73        ad 		cache_prune(items, (int)((uint64_t)desiredvnodes *
   1245   1.73        ad 		    cache_lowat / 100));
   1246   1.73        ad 		cache_ev_over.ev_count++;
   1247   1.73        ad 	} else
   1248   1.73        ad 		cache_ev_under.ev_count++;
   1249   1.73        ad 
   1250  1.124        ad 	/* Chop the existing garbage collection list out. */
   1251  1.124        ad 	mutex_enter(&namecache_gc_lock);
   1252  1.124        ad 	queue = namecache_gc_queue;
   1253  1.124        ad 	items = namecache_gc_pend;
   1254  1.124        ad 	SLIST_INIT(&namecache_gc_queue);
   1255  1.124        ad 	namecache_gc_pend = 0;
   1256  1.124        ad 	mutex_exit(&namecache_gc_lock);
   1257  1.124        ad 
   1258   1.73        ad 	/*
   1259  1.124        ad 	 * Now disassociate all entries.  We haven't locked out the reader
   1260  1.124        ad 	 * side (cache_lookup_entry()) but the "next" pointers in the hash
   1261  1.124        ad 	 * list will remain sufficiently consitent regardless of which
   1262  1.124        ad 	 * version of the list the reader sees (see defs of LIST_FOREACH,
   1263  1.124        ad 	 * LIST_NEXT);
   1264   1.73        ad 	 */
   1265  1.124        ad 	SLIST_FOREACH(ncp, &queue, nc_gclist) {
   1266   1.73        ad 		cache_disassociate(ncp);
   1267   1.73        ad 		KASSERT(ncp->nc_dvp == NULL);
   1268   1.73        ad 		if (ncp->nc_hash.le_prev != NULL) {
   1269   1.73        ad 			LIST_REMOVE(ncp, nc_hash);
   1270   1.73        ad 			ncp->nc_hash.le_prev = NULL;
   1271   1.73        ad 		}
   1272  1.124        ad 	}
   1273  1.124        ad 
   1274  1.124        ad 	/*
   1275  1.124        ad 	 * With that done, make sure our updates are visible on the bus, and
   1276  1.124        ad 	 * make a pass to observe the status of all of the CPU locks.  If we
   1277  1.124        ad 	 * see a lock is unheld, we know the garbage collected entries can
   1278  1.124        ad 	 * no longer visible to that CPU.  If we see a lock IS held, we need
   1279  1.124        ad 	 * to acquire and release it once to make sure that CPU is out of
   1280  1.124        ad 	 * cache_lookup_entry().  Take the opportunity to refresh stats.
   1281  1.124        ad 	 */
   1282  1.124        ad 	membar_sync();	/* stores above vs. reads below */
   1283  1.124        ad 	for (CPU_INFO_FOREACH(cii, ci)) {
   1284  1.124        ad 		cpup = ci->ci_data.cpu_nch;
   1285  1.124        ad 		if (__predict_false(mutex_owner(&cpup->cpu_lock) != NULL)) {
   1286  1.124        ad 			mutex_enter(&cpup->cpu_lock);
   1287  1.124        ad 			/* nothing */
   1288  1.124        ad 			mutex_exit(&cpup->cpu_lock);
   1289  1.124        ad 		}
   1290  1.124        ad 		UPDATE(cpup, ncs_goodhits);
   1291  1.124        ad 		UPDATE(cpup, ncs_neghits);
   1292  1.124        ad 		UPDATE(cpup, ncs_badhits);
   1293  1.124        ad 		UPDATE(cpup, ncs_falsehits);
   1294  1.124        ad 		UPDATE(cpup, ncs_miss);
   1295  1.124        ad 		UPDATE(cpup, ncs_long);
   1296  1.124        ad 		UPDATE(cpup, ncs_pass2);
   1297  1.124        ad 		UPDATE(cpup, ncs_2passes);
   1298  1.124        ad 		UPDATE(cpup, ncs_revhits);
   1299  1.124        ad 		UPDATE(cpup, ncs_revmiss);
   1300  1.124        ad 	}
   1301  1.124        ad 	membar_sync();	/* reads above vs. stores below */
   1302  1.124        ad 
   1303  1.124        ad 	/*
   1304  1.124        ad 	 * Nobody else can see the cache entries any more.  Make a final
   1305  1.124        ad 	 * pass over the list and toss the contents.
   1306  1.124        ad 	 */
   1307  1.124        ad 	SLIST_FOREACH_SAFE(ncp, &queue, nc_gclist, next) {
   1308  1.124        ad 		mutex_obj_free(ncp->nc_lock);
   1309  1.124        ad 		ncp->nc_lock = NULL;
   1310  1.121  christos 		if (ncp->nc_nlen > NCHNAMLEN) {
   1311  1.121  christos 			cache_dtor(NULL, ncp);
   1312  1.121  christos 			kmem_free(ncp, sizeof(*ncp) + ncp->nc_nlen);
   1313  1.121  christos 		} else
   1314  1.123      maya 			pool_cache_put(namecache_cache, ncp);
   1315   1.73        ad 	}
   1316  1.124        ad 
   1317   1.73        ad 	numcache -= items;
   1318   1.73        ad 	cache_ev_gc.ev_count += items;
   1319   1.73        ad }
   1320   1.73        ad 
   1321   1.73        ad /*
   1322   1.73        ad  * Cache maintainence thread, awakening once per second to:
   1323   1.73        ad  *
   1324   1.73        ad  * => keep number of entries below the high water mark
   1325   1.73        ad  * => sort pseudo-LRU list
   1326   1.73        ad  * => garbage collect dead entries
   1327   1.73        ad  */
   1328   1.73        ad static void
   1329   1.73        ad cache_thread(void *arg)
   1330   1.73        ad {
   1331   1.73        ad 
   1332  1.124        ad 	mutex_enter(&namecache_lock);
   1333   1.73        ad 	for (;;) {
   1334   1.73        ad 		cache_reclaim();
   1335  1.124        ad 		kpause("cachegc", false, hz, &namecache_lock);
   1336    1.1       cgd 	}
   1337    1.1       cgd }
   1338   1.19  sommerfe 
   1339   1.28       chs #ifdef DDB
   1340   1.28       chs void
   1341   1.28       chs namecache_print(struct vnode *vp, void (*pr)(const char *, ...))
   1342   1.28       chs {
   1343   1.28       chs 	struct vnode *dvp = NULL;
   1344   1.28       chs 	struct namecache *ncp;
   1345   1.28       chs 
   1346   1.28       chs 	TAILQ_FOREACH(ncp, &nclruhead, nc_lru) {
   1347   1.73        ad 		if (ncp->nc_vp == vp && ncp->nc_dvp != NULL) {
   1348   1.28       chs 			(*pr)("name %.*s\n", ncp->nc_nlen, ncp->nc_name);
   1349   1.28       chs 			dvp = ncp->nc_dvp;
   1350   1.28       chs 		}
   1351   1.28       chs 	}
   1352   1.28       chs 	if (dvp == NULL) {
   1353   1.28       chs 		(*pr)("name not found\n");
   1354   1.28       chs 		return;
   1355   1.28       chs 	}
   1356   1.28       chs 	vp = dvp;
   1357   1.28       chs 	TAILQ_FOREACH(ncp, &nclruhead, nc_lru) {
   1358   1.47      yamt 		if (ncp->nc_vp == vp) {
   1359   1.28       chs 			(*pr)("parent %.*s\n", ncp->nc_nlen, ncp->nc_name);
   1360   1.28       chs 		}
   1361   1.28       chs 	}
   1362   1.28       chs }
   1363   1.28       chs #endif
   1364   1.95     joerg 
   1365   1.95     joerg void
   1366   1.95     joerg namecache_count_pass2(void)
   1367   1.95     joerg {
   1368   1.95     joerg 	struct nchcpu *cpup = curcpu()->ci_data.cpu_nch;
   1369   1.95     joerg 
   1370  1.103    dennis 	COUNT_UNL(cpup, ncs_pass2);
   1371   1.95     joerg }
   1372   1.95     joerg 
   1373   1.95     joerg void
   1374   1.95     joerg namecache_count_2passes(void)
   1375   1.95     joerg {
   1376   1.95     joerg 	struct nchcpu *cpup = curcpu()->ci_data.cpu_nch;
   1377   1.95     joerg 
   1378  1.103    dennis 	COUNT_UNL(cpup, ncs_2passes);
   1379   1.95     joerg }
   1380   1.97     joerg 
   1381  1.103    dennis /*
   1382  1.103    dennis  * Fetch the current values of the stats.  We return the most
   1383  1.103    dennis  * recent values harvested into nchstats by cache_reclaim(), which
   1384  1.103    dennis  * will be less than a second old.
   1385  1.103    dennis  */
   1386   1.97     joerg static int
   1387   1.97     joerg cache_stat_sysctl(SYSCTLFN_ARGS)
   1388   1.97     joerg {
   1389  1.103    dennis 	CPU_INFO_ITERATOR cii;
   1390  1.103    dennis 	struct cpu_info *ci;
   1391   1.97     joerg 
   1392   1.97     joerg 	if (oldp == NULL) {
   1393  1.124        ad 		*oldlenp = sizeof(nchstats);
   1394   1.97     joerg 		return 0;
   1395   1.97     joerg 	}
   1396   1.97     joerg 
   1397  1.124        ad 	if (*oldlenp < sizeof(nchstats)) {
   1398   1.97     joerg 		*oldlenp = 0;
   1399   1.97     joerg 		return 0;
   1400   1.97     joerg 	}
   1401   1.97     joerg 
   1402  1.103    dennis 	sysctl_unlock();
   1403  1.124        ad 	mutex_enter(&namecache_lock);
   1404  1.103    dennis 	for (CPU_INFO_FOREACH(cii, ci)) {
   1405  1.103    dennis 		struct nchcpu *cpup = ci->ci_data.cpu_nch;
   1406   1.97     joerg 
   1407  1.124        ad 		UPDATE(cpup, ncs_goodhits);
   1408  1.124        ad 		UPDATE(cpup, ncs_neghits);
   1409  1.124        ad 		UPDATE(cpup, ncs_badhits);
   1410  1.124        ad 		UPDATE(cpup, ncs_falsehits);
   1411  1.124        ad 		UPDATE(cpup, ncs_miss);
   1412  1.124        ad 
   1413  1.124        ad 		UPDATE(cpup, ncs_pass2);
   1414  1.124        ad 		UPDATE(cpup, ncs_2passes);
   1415  1.124        ad 		UPDATE(cpup, ncs_revhits);
   1416  1.124        ad 		UPDATE(cpup, ncs_revmiss);
   1417  1.103    dennis 	}
   1418  1.124        ad 	mutex_exit(&namecache_lock);
   1419   1.97     joerg 	sysctl_relock();
   1420   1.97     joerg 
   1421  1.124        ad 	*oldlenp = sizeof(nchstats);
   1422  1.124        ad 	return sysctl_copyout(l, &nchstats, oldp, sizeof(nchstats));
   1423   1.97     joerg }
   1424   1.97     joerg 
   1425  1.104     pooka static void
   1426  1.104     pooka sysctl_cache_stat_setup(void)
   1427   1.97     joerg {
   1428  1.104     pooka 
   1429  1.104     pooka 	KASSERT(sysctllog == NULL);
   1430  1.104     pooka 	sysctl_createv(&sysctllog, 0, NULL, NULL,
   1431   1.97     joerg 		       CTLFLAG_PERMANENT,
   1432   1.97     joerg 		       CTLTYPE_STRUCT, "namecache_stats",
   1433   1.97     joerg 		       SYSCTL_DESCR("namecache statistics"),
   1434   1.97     joerg 		       cache_stat_sysctl, 0, NULL, 0,
   1435   1.97     joerg 		       CTL_VFS, CTL_CREATE, CTL_EOL);
   1436   1.97     joerg }
   1437