1 1.159 riastrad /* $NetBSD: vfs_cache.c,v 1.159 2024/12/07 02:27:38 riastradh Exp $ */ 2 1.73 ad 3 1.73 ad /*- 4 1.155 ad * Copyright (c) 2008, 2019, 2020, 2023 The NetBSD Foundation, Inc. 5 1.73 ad * All rights reserved. 6 1.73 ad * 7 1.128 ad * This code is derived from software contributed to The NetBSD Foundation 8 1.128 ad * by Andrew Doran. 9 1.128 ad * 10 1.73 ad * Redistribution and use in source and binary forms, with or without 11 1.73 ad * modification, are permitted provided that the following conditions 12 1.73 ad * are met: 13 1.73 ad * 1. Redistributions of source code must retain the above copyright 14 1.73 ad * notice, this list of conditions and the following disclaimer. 15 1.73 ad * 2. Redistributions in binary form must reproduce the above copyright 16 1.73 ad * notice, this list of conditions and the following disclaimer in the 17 1.73 ad * documentation and/or other materials provided with the distribution. 18 1.73 ad * 19 1.73 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.73 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.73 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.73 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.73 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.73 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.73 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.73 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.73 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.73 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.73 ad * POSSIBILITY OF SUCH DAMAGE. 30 1.73 ad */ 31 1.6 cgd 32 1.1 cgd /* 33 1.5 mycroft * Copyright (c) 1989, 1993 34 1.5 mycroft * The Regents of the University of California. All rights reserved. 35 1.1 cgd * 36 1.1 cgd * Redistribution and use in source and binary forms, with or without 37 1.1 cgd * modification, are permitted provided that the following conditions 38 1.1 cgd * are met: 39 1.1 cgd * 1. Redistributions of source code must retain the above copyright 40 1.1 cgd * notice, this list of conditions and the following disclaimer. 41 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright 42 1.1 cgd * notice, this list of conditions and the following disclaimer in the 43 1.1 cgd * documentation and/or other materials provided with the distribution. 44 1.51 agc * 3. Neither the name of the University nor the names of its contributors 45 1.1 cgd * may be used to endorse or promote products derived from this software 46 1.1 cgd * without specific prior written permission. 47 1.1 cgd * 48 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 49 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 50 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 51 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 52 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 53 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 54 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 55 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 56 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 57 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 58 1.1 cgd * SUCH DAMAGE. 59 1.1 cgd * 60 1.10 mycroft * @(#)vfs_cache.c 8.3 (Berkeley) 8/22/94 61 1.1 cgd */ 62 1.32 lukem 63 1.128 ad /* 64 1.128 ad * Name caching: 65 1.128 ad * 66 1.128 ad * Names found by directory scans are retained in a cache for future 67 1.128 ad * reference. It is managed LRU, so frequently used names will hang 68 1.128 ad * around. The cache is indexed by hash value obtained from the name. 69 1.128 ad * 70 1.128 ad * The name cache is the brainchild of Robert Elz and was introduced in 71 1.128 ad * 4.3BSD. See "Using gprof to Tune the 4.2BSD Kernel", Marshall Kirk 72 1.128 ad * McKusick, May 21 1984. 73 1.128 ad * 74 1.128 ad * Data structures: 75 1.128 ad * 76 1.128 ad * Most Unix namecaches very sensibly use a global hash table to index 77 1.128 ad * names. The global hash table works well, but can cause concurrency 78 1.128 ad * headaches for the kernel hacker. In the NetBSD 10.0 implementation 79 1.128 ad * we are not sensible, and use a per-directory data structure to index 80 1.128 ad * names, but the cache otherwise functions the same. 81 1.128 ad * 82 1.155 ad * The index is a red-black tree. It should not be difficult to 83 1.155 ad * experiment with other types of index, however note that a tree 84 1.155 ad * can trivially be made to support lockless lookup. 85 1.128 ad * 86 1.128 ad * Each cached name is stored in a struct namecache, along with a 87 1.128 ad * pointer to the associated vnode (nc_vp). Names longer than a 88 1.128 ad * maximum length of NCHNAMLEN are allocated with kmem_alloc(); they 89 1.128 ad * occur infrequently, and names shorter than this are stored directly 90 1.128 ad * in struct namecache. If it is a "negative" entry, (i.e. for a name 91 1.128 ad * that is known NOT to exist) the vnode pointer will be NULL. 92 1.128 ad * 93 1.155 ad * In practice this implementation is not any slower than the hash 94 1.155 ad * table that preceeded it and in some cases it significantly 95 1.155 ad * outperforms the hash table. Some reasons why this might be: 96 1.155 ad * 97 1.155 ad * - natural partitioning provided by the file system structure, which 98 1.155 ad * the prior implementation discarded (global hash table). 99 1.155 ad * - worst case tree traversal of O(log n), the hash table could have 100 1.155 ad * many collisions. 101 1.155 ad * - minimized cache misses & total L2/L3 CPU cache footprint; struct 102 1.155 ad * namecache and vnode_impl_t are laid out to keep cache footprint 103 1.155 ad * minimal in the lookup path; no hash table buckets to cache. 104 1.155 ad * - minimized number of conditionals & string comparisons. 105 1.155 ad * 106 1.128 ad * For a directory with 3 cached names for 3 distinct vnodes, the 107 1.128 ad * various vnodes and namecache structs would be connected like this 108 1.128 ad * (the root is at the bottom of the diagram): 109 1.128 ad * 110 1.128 ad * ... 111 1.128 ad * ^ 112 1.128 ad * |- vi_nc_tree 113 1.147 riastrad * | 114 1.128 ad * +----o----+ +---------+ +---------+ 115 1.128 ad * | VDIR | | VCHR | | VREG | 116 1.128 ad * | vnode o-----+ | vnode o-----+ | vnode o------+ 117 1.128 ad * +---------+ | +---------+ | +---------+ | 118 1.128 ad * ^ | ^ | ^ | 119 1.128 ad * |- nc_vp |- vi_nc_list |- nc_vp |- vi_nc_list |- nc_vp | 120 1.128 ad * | | | | | | 121 1.128 ad * +----o----+ | +----o----+ | +----o----+ | 122 1.128 ad * +---onamecache|<----+ +---onamecache|<----+ +---onamecache|<-----+ 123 1.128 ad * | +---------+ | +---------+ | +---------+ 124 1.128 ad * | ^ | ^ | ^ 125 1.128 ad * | | | | | | 126 1.128 ad * | | +----------------------+ | | 127 1.128 ad * |-nc_dvp | +-------------------------------------------------+ 128 1.128 ad * | |/- vi_nc_tree | | 129 1.128 ad * | | |- nc_dvp |- nc_dvp 130 1.128 ad * | +----o----+ | | 131 1.128 ad * +-->| VDIR |<----------+ | 132 1.128 ad * | vnode |<------------------------------------+ 133 1.128 ad * +---------+ 134 1.128 ad * 135 1.128 ad * START HERE 136 1.128 ad * 137 1.128 ad * Replacement: 138 1.128 ad * 139 1.128 ad * As the cache becomes full, old and unused entries are purged as new 140 1.128 ad * entries are added. The synchronization overhead in maintaining a 141 1.128 ad * strict ordering would be prohibitive, so the VM system's "clock" or 142 1.128 ad * "second chance" page replacement algorithm is aped here. New 143 1.128 ad * entries go to the tail of the active list. After they age out and 144 1.128 ad * reach the head of the list, they are moved to the tail of the 145 1.128 ad * inactive list. Any use of the deactivated cache entry reactivates 146 1.128 ad * it, saving it from impending doom; if not reactivated, the entry 147 1.128 ad * eventually reaches the head of the inactive list and is purged. 148 1.128 ad * 149 1.128 ad * Concurrency: 150 1.128 ad * 151 1.128 ad * From a performance perspective, cache_lookup(nameiop == LOOKUP) is 152 1.128 ad * what really matters; insertion of new entries with cache_enter() is 153 1.128 ad * comparatively infrequent, and overshadowed by the cost of expensive 154 1.128 ad * file system metadata operations (which may involve disk I/O). We 155 1.128 ad * therefore want to make everything simplest in the lookup path. 156 1.128 ad * 157 1.128 ad * struct namecache is mostly stable except for list and tree related 158 1.157 riastrad * entries, changes to which don't affect the cached name or vnode. 159 1.128 ad * For changes to name+vnode, entries are purged in preference to 160 1.128 ad * modifying them. 161 1.128 ad * 162 1.128 ad * Read access to namecache entries is made via tree, list, or LRU 163 1.128 ad * list. A lock corresponding to the direction of access should be 164 1.128 ad * held. See definition of "struct namecache" in src/sys/namei.src, 165 1.128 ad * and the definition of "struct vnode" for the particulars. 166 1.128 ad * 167 1.156 ad * Per-CPU statistics, and LRU list totals are read unlocked, since an 168 1.156 ad * approximate value is OK. We maintain 32-bit sized per-CPU counters 169 1.156 ad * and 64-bit global counters since 32-bit sized counters can be 170 1.156 ad * observed locklessly while the global counters are protected by a 171 1.156 ad * mutex. 172 1.128 ad * 173 1.128 ad * The lock order is: 174 1.128 ad * 175 1.128 ad * 1) vi->vi_nc_lock (tree or parent -> child direction, 176 1.128 ad * used during forward lookup) 177 1.128 ad * 178 1.128 ad * 2) vi->vi_nc_listlock (list or child -> parent direction, 179 1.128 ad * used during reverse lookup) 180 1.128 ad * 181 1.128 ad * 3) cache_lru_lock (LRU list direction, used during reclaim) 182 1.128 ad */ 183 1.128 ad 184 1.157 riastrad #define __NAMECACHE_PRIVATE 185 1.157 riastrad 186 1.32 lukem #include <sys/cdefs.h> 187 1.159 riastrad __KERNEL_RCSID(0, "$NetBSD: vfs_cache.c,v 1.159 2024/12/07 02:27:38 riastradh Exp $"); 188 1.1 cgd 189 1.107 pooka #ifdef _KERNEL_OPT 190 1.28 chs #include "opt_ddb.h" 191 1.115 riastrad #include "opt_dtrace.h" 192 1.107 pooka #endif 193 1.28 chs 194 1.150 skrll #include <sys/param.h> 195 1.128 ad #include <sys/types.h> 196 1.157 riastrad 197 1.115 riastrad #include <sys/atomic.h> 198 1.128 ad #include <sys/callout.h> 199 1.115 riastrad #include <sys/cpu.h> 200 1.115 riastrad #include <sys/errno.h> 201 1.115 riastrad #include <sys/evcnt.h> 202 1.128 ad #include <sys/hash.h> 203 1.115 riastrad #include <sys/kernel.h> 204 1.4 mycroft #include <sys/mount.h> 205 1.115 riastrad #include <sys/mutex.h> 206 1.4 mycroft #include <sys/namei.h> 207 1.128 ad #include <sys/param.h> 208 1.18 thorpej #include <sys/pool.h> 209 1.108 christos #include <sys/sdt.h> 210 1.115 riastrad #include <sys/sysctl.h> 211 1.115 riastrad #include <sys/systm.h> 212 1.115 riastrad #include <sys/time.h> 213 1.115 riastrad #include <sys/vnode_impl.h> 214 1.1 cgd 215 1.128 ad #include <miscfs/genfs/genfs.h> 216 1.1 cgd 217 1.155 ad /* 218 1.155 ad * Assert that data structure layout hasn't changed unintentionally. 219 1.155 ad */ 220 1.155 ad #ifdef _LP64 221 1.155 ad CTASSERT(sizeof(struct namecache) == 128); 222 1.155 ad #else 223 1.155 ad CTASSERT(sizeof(struct namecache) == 64); 224 1.155 ad #endif 225 1.155 ad CTASSERT(NC_NLEN_MASK >= MAXPATHLEN); 226 1.155 ad 227 1.128 ad static void cache_activate(struct namecache *); 228 1.128 ad static void cache_update_stats(void *); 229 1.128 ad static int cache_compare_nodes(void *, const void *, const void *); 230 1.128 ad static void cache_deactivate(void); 231 1.128 ad static void cache_reclaim(void); 232 1.128 ad static int cache_stat_sysctl(SYSCTLFN_ARGS); 233 1.128 ad 234 1.131 ad /* 235 1.131 ad * Global pool cache. 236 1.131 ad */ 237 1.128 ad static pool_cache_t cache_pool __read_mostly; 238 1.128 ad 239 1.131 ad /* 240 1.131 ad * LRU replacement. 241 1.131 ad */ 242 1.128 ad enum cache_lru_id { 243 1.128 ad LRU_ACTIVE, 244 1.128 ad LRU_INACTIVE, 245 1.128 ad LRU_COUNT 246 1.128 ad }; 247 1.120 riastrad 248 1.128 ad static struct { 249 1.128 ad TAILQ_HEAD(, namecache) list[LRU_COUNT]; 250 1.128 ad u_int count[LRU_COUNT]; 251 1.128 ad } cache_lru __cacheline_aligned; 252 1.117 riastrad 253 1.128 ad static kmutex_t cache_lru_lock __cacheline_aligned; 254 1.117 riastrad 255 1.131 ad /* 256 1.131 ad * Cache effectiveness statistics. nchstats holds system-wide total. 257 1.131 ad */ 258 1.128 ad struct nchstats nchstats; 259 1.103 dennis struct nchstats_percpu _NAMEI_CACHE_STATS(uint32_t); 260 1.77 ad struct nchcpu { 261 1.128 ad struct nchstats_percpu cur; 262 1.128 ad struct nchstats_percpu last; 263 1.77 ad }; 264 1.128 ad static callout_t cache_stat_callout; 265 1.128 ad static kmutex_t cache_stat_lock __cacheline_aligned; 266 1.77 ad 267 1.138 ad #define COUNT(f) do { \ 268 1.128 ad lwp_t *l = curlwp; \ 269 1.128 ad KPREEMPT_DISABLE(l); \ 270 1.149 christos struct nchcpu *nchcpu = curcpu()->ci_data.cpu_nch; \ 271 1.149 christos nchcpu->cur.f++; \ 272 1.128 ad KPREEMPT_ENABLE(l); \ 273 1.128 ad } while (/* CONSTCOND */ 0); 274 1.128 ad 275 1.128 ad #define UPDATE(nchcpu, f) do { \ 276 1.128 ad uint32_t cur = atomic_load_relaxed(&nchcpu->cur.f); \ 277 1.135 ad nchstats.f += (uint32_t)(cur - nchcpu->last.f); \ 278 1.128 ad nchcpu->last.f = cur; \ 279 1.128 ad } while (/* CONSTCOND */ 0) 280 1.90 dholland 281 1.90 dholland /* 282 1.128 ad * Tunables. cache_maxlen replaces the historical doingcache: 283 1.128 ad * set it zero to disable caching for debugging purposes. 284 1.1 cgd */ 285 1.128 ad int cache_lru_maxdeact __read_mostly = 2; /* max # to deactivate */ 286 1.128 ad int cache_lru_maxscan __read_mostly = 64; /* max # to scan/reclaim */ 287 1.155 ad int cache_maxlen __read_mostly = NC_NLEN_MASK; /* max name length to cache */ 288 1.128 ad int cache_stat_interval __read_mostly = 300; /* in seconds */ 289 1.128 ad 290 1.131 ad /* 291 1.131 ad * sysctl stuff. 292 1.131 ad */ 293 1.128 ad static struct sysctllog *cache_sysctllog; 294 1.128 ad 295 1.131 ad /* 296 1.146 ad * This is a dummy name that cannot usually occur anywhere in the cache nor 297 1.146 ad * file system. It's used when caching the root vnode of mounted file 298 1.146 ad * systems. The name is attached to the directory that the file system is 299 1.146 ad * mounted on. 300 1.146 ad */ 301 1.146 ad static const char cache_mp_name[] = ""; 302 1.146 ad static const int cache_mp_nlen = sizeof(cache_mp_name) - 1; 303 1.146 ad 304 1.146 ad /* 305 1.131 ad * Red-black tree stuff. 306 1.131 ad */ 307 1.128 ad static const rb_tree_ops_t cache_rbtree_ops = { 308 1.128 ad .rbto_compare_nodes = cache_compare_nodes, 309 1.131 ad .rbto_compare_key = cache_compare_nodes, 310 1.128 ad .rbto_node_offset = offsetof(struct namecache, nc_tree), 311 1.128 ad .rbto_context = NULL 312 1.128 ad }; 313 1.89 rmind 314 1.131 ad /* 315 1.131 ad * dtrace probes. 316 1.131 ad */ 317 1.108 christos SDT_PROBE_DEFINE1(vfs, namecache, invalidate, done, "struct vnode *"); 318 1.108 christos SDT_PROBE_DEFINE1(vfs, namecache, purge, parents, "struct vnode *"); 319 1.108 christos SDT_PROBE_DEFINE1(vfs, namecache, purge, children, "struct vnode *"); 320 1.108 christos SDT_PROBE_DEFINE2(vfs, namecache, purge, name, "char *", "size_t"); 321 1.108 christos SDT_PROBE_DEFINE1(vfs, namecache, purge, vfs, "struct mount *"); 322 1.108 christos SDT_PROBE_DEFINE3(vfs, namecache, lookup, hit, "struct vnode *", 323 1.108 christos "char *", "size_t"); 324 1.108 christos SDT_PROBE_DEFINE3(vfs, namecache, lookup, miss, "struct vnode *", 325 1.108 christos "char *", "size_t"); 326 1.108 christos SDT_PROBE_DEFINE3(vfs, namecache, lookup, toolong, "struct vnode *", 327 1.108 christos "char *", "size_t"); 328 1.108 christos SDT_PROBE_DEFINE2(vfs, namecache, revlookup, success, "struct vnode *", 329 1.108 christos "struct vnode *"); 330 1.108 christos SDT_PROBE_DEFINE2(vfs, namecache, revlookup, fail, "struct vnode *", 331 1.108 christos "int"); 332 1.108 christos SDT_PROBE_DEFINE2(vfs, namecache, prune, done, "int", "int"); 333 1.108 christos SDT_PROBE_DEFINE3(vfs, namecache, enter, toolong, "struct vnode *", 334 1.108 christos "char *", "size_t"); 335 1.108 christos SDT_PROBE_DEFINE3(vfs, namecache, enter, done, "struct vnode *", 336 1.108 christos "char *", "size_t"); 337 1.108 christos 338 1.73 ad /* 339 1.128 ad * rbtree: compare two nodes. 340 1.90 dholland */ 341 1.128 ad static int 342 1.128 ad cache_compare_nodes(void *context, const void *n1, const void *n2) 343 1.90 dholland { 344 1.128 ad const struct namecache *nc1 = n1; 345 1.128 ad const struct namecache *nc2 = n2; 346 1.90 dholland 347 1.128 ad if (nc1->nc_key < nc2->nc_key) { 348 1.128 ad return -1; 349 1.128 ad } 350 1.128 ad if (nc1->nc_key > nc2->nc_key) { 351 1.128 ad return 1; 352 1.128 ad } 353 1.155 ad KASSERT(NC_NLEN(nc1) == NC_NLEN(nc2)); 354 1.155 ad return memcmp(nc1->nc_name, nc2->nc_name, NC_NLEN(nc1)); 355 1.73 ad } 356 1.46 yamt 357 1.73 ad /* 358 1.128 ad * Compute a key value for the given name. The name length is encoded in 359 1.128 ad * the key value to try and improve uniqueness, and so that length doesn't 360 1.128 ad * need to be compared separately for string comparisons. 361 1.73 ad */ 362 1.155 ad static uintptr_t 363 1.128 ad cache_key(const char *name, size_t nlen) 364 1.73 ad { 365 1.155 ad uintptr_t key; 366 1.73 ad 367 1.155 ad KASSERT((nlen & ~NC_NLEN_MASK) == 0); 368 1.73 ad 369 1.128 ad key = hash32_buf(name, nlen, HASH32_STR_INIT); 370 1.155 ad return (key << NC_NLEN_BITS) | (uintptr_t)nlen; 371 1.46 yamt } 372 1.46 yamt 373 1.73 ad /* 374 1.128 ad * Remove an entry from the cache. vi_nc_lock must be held, and if dir2node 375 1.128 ad * is true, then we're locking in the conventional direction and the list 376 1.128 ad * lock will be acquired when removing the entry from the vnode list. 377 1.73 ad */ 378 1.73 ad static void 379 1.128 ad cache_remove(struct namecache *ncp, const bool dir2node) 380 1.73 ad { 381 1.128 ad struct vnode *vp, *dvp = ncp->nc_dvp; 382 1.128 ad vnode_impl_t *dvi = VNODE_TO_VIMPL(dvp); 383 1.155 ad size_t namelen = NC_NLEN(ncp); 384 1.128 ad 385 1.128 ad KASSERT(rw_write_held(&dvi->vi_nc_lock)); 386 1.155 ad KASSERT(cache_key(ncp->nc_name, namelen) == ncp->nc_key); 387 1.132 ad KASSERT(rb_tree_find_node(&dvi->vi_nc_tree, ncp) == ncp); 388 1.128 ad 389 1.155 ad SDT_PROBE(vfs, namecache, invalidate, done, ncp, 0, 0, 0, 0); 390 1.128 ad 391 1.134 ad /* 392 1.134 ad * Remove from the vnode's list. This excludes cache_revlookup(), 393 1.134 ad * and then it's safe to remove from the LRU lists. 394 1.134 ad */ 395 1.128 ad if ((vp = ncp->nc_vp) != NULL) { 396 1.128 ad vnode_impl_t *vi = VNODE_TO_VIMPL(vp); 397 1.128 ad if (__predict_true(dir2node)) { 398 1.128 ad rw_enter(&vi->vi_nc_listlock, RW_WRITER); 399 1.128 ad TAILQ_REMOVE(&vi->vi_nc_list, ncp, nc_list); 400 1.128 ad rw_exit(&vi->vi_nc_listlock); 401 1.128 ad } else { 402 1.128 ad TAILQ_REMOVE(&vi->vi_nc_list, ncp, nc_list); 403 1.128 ad } 404 1.128 ad } 405 1.73 ad 406 1.134 ad /* Remove from the directory's rbtree. */ 407 1.134 ad rb_tree_remove_node(&dvi->vi_nc_tree, ncp); 408 1.134 ad 409 1.134 ad /* Remove from the LRU lists. */ 410 1.134 ad mutex_enter(&cache_lru_lock); 411 1.134 ad TAILQ_REMOVE(&cache_lru.list[ncp->nc_lrulist], ncp, nc_lru); 412 1.134 ad cache_lru.count[ncp->nc_lrulist]--; 413 1.134 ad mutex_exit(&cache_lru_lock); 414 1.134 ad 415 1.128 ad /* Finally, free it. */ 416 1.155 ad if (namelen > NCHNAMLEN) { 417 1.155 ad size_t sz = offsetof(struct namecache, nc_name[namelen]); 418 1.128 ad kmem_free(ncp, sz); 419 1.128 ad } else { 420 1.128 ad pool_cache_put(cache_pool, ncp); 421 1.73 ad } 422 1.73 ad } 423 1.73 ad 424 1.73 ad /* 425 1.128 ad * Find a single cache entry and return it. vi_nc_lock must be held. 426 1.73 ad */ 427 1.128 ad static struct namecache * __noinline 428 1.128 ad cache_lookup_entry(struct vnode *dvp, const char *name, size_t namelen, 429 1.155 ad uintptr_t key) 430 1.55 yamt { 431 1.128 ad vnode_impl_t *dvi = VNODE_TO_VIMPL(dvp); 432 1.128 ad struct rb_node *node = dvi->vi_nc_tree.rbt_root; 433 1.55 yamt struct namecache *ncp; 434 1.155 ad enum cache_lru_id lrulist; 435 1.155 ad int diff; 436 1.128 ad 437 1.155 ad KASSERT(namelen <= MAXPATHLEN); 438 1.128 ad KASSERT(rw_lock_held(&dvi->vi_nc_lock)); 439 1.128 ad 440 1.128 ad /* 441 1.128 ad * Search the RB tree for the key. This is an inlined lookup 442 1.156 ad * tailored for exactly what's needed here that turns out to be 443 1.156 ad * quite a bit faster than using rb_tree_find_node(). 444 1.131 ad * 445 1.135 ad * For a matching key memcmp() needs to be called once to confirm 446 1.135 ad * that the correct name has been found. Very rarely there will be 447 1.135 ad * a key value collision and the search will continue. 448 1.128 ad */ 449 1.128 ad for (;;) { 450 1.128 ad if (__predict_false(RB_SENTINEL_P(node))) { 451 1.128 ad return NULL; 452 1.128 ad } 453 1.138 ad ncp = (struct namecache *)node; 454 1.128 ad KASSERT((void *)&ncp->nc_tree == (void *)ncp); 455 1.128 ad KASSERT(ncp->nc_dvp == dvp); 456 1.138 ad if (ncp->nc_key == key) { 457 1.155 ad KASSERT(NC_NLEN(ncp) == namelen); 458 1.131 ad diff = memcmp(ncp->nc_name, name, namelen); 459 1.131 ad if (__predict_true(diff == 0)) { 460 1.131 ad break; 461 1.131 ad } 462 1.147 riastrad node = node->rb_nodes[diff < 0]; 463 1.131 ad } else { 464 1.131 ad node = node->rb_nodes[ncp->nc_key < key]; 465 1.128 ad } 466 1.128 ad } 467 1.55 yamt 468 1.128 ad /* 469 1.128 ad * If the entry is on the wrong LRU list, requeue it. This is an 470 1.128 ad * unlocked check, but it will rarely be wrong and even then there 471 1.128 ad * will be no harm caused. 472 1.128 ad */ 473 1.128 ad lrulist = atomic_load_relaxed(&ncp->nc_lrulist); 474 1.128 ad if (__predict_false(lrulist != LRU_ACTIVE)) { 475 1.128 ad cache_activate(ncp); 476 1.128 ad } 477 1.128 ad return ncp; 478 1.55 yamt } 479 1.55 yamt 480 1.1 cgd /* 481 1.1 cgd * Look for a the name in the cache. We don't do this 482 1.1 cgd * if the segment name is long, simply so the cache can avoid 483 1.1 cgd * holding long names (which would either waste space, or 484 1.1 cgd * add greatly to the complexity). 485 1.1 cgd * 486 1.90 dholland * Lookup is called with DVP pointing to the directory to search, 487 1.90 dholland * and CNP providing the name of the entry being sought: cn_nameptr 488 1.90 dholland * is the name, cn_namelen is its length, and cn_flags is the flags 489 1.90 dholland * word from the namei operation. 490 1.90 dholland * 491 1.90 dholland * DVP must be locked. 492 1.90 dholland * 493 1.90 dholland * There are three possible non-error return states: 494 1.90 dholland * 1. Nothing was found in the cache. Nothing is known about 495 1.90 dholland * the requested name. 496 1.90 dholland * 2. A negative entry was found in the cache, meaning that the 497 1.90 dholland * requested name definitely does not exist. 498 1.90 dholland * 3. A positive entry was found in the cache, meaning that the 499 1.90 dholland * requested name does exist and that we are providing the 500 1.90 dholland * vnode. 501 1.90 dholland * In these cases the results are: 502 1.90 dholland * 1. 0 returned; VN is set to NULL. 503 1.90 dholland * 2. 1 returned; VN is set to NULL. 504 1.90 dholland * 3. 1 returned; VN is set to the vnode found. 505 1.90 dholland * 506 1.90 dholland * The additional result argument ISWHT is set to zero, unless a 507 1.90 dholland * negative entry is found that was entered as a whiteout, in which 508 1.90 dholland * case ISWHT is set to one. 509 1.90 dholland * 510 1.90 dholland * The ISWHT_RET argument pointer may be null. In this case an 511 1.90 dholland * assertion is made that the whiteout flag is not set. File systems 512 1.90 dholland * that do not support whiteouts can/should do this. 513 1.90 dholland * 514 1.90 dholland * Filesystems that do support whiteouts should add ISWHITEOUT to 515 1.90 dholland * cnp->cn_flags if ISWHT comes back nonzero. 516 1.90 dholland * 517 1.90 dholland * When a vnode is returned, it is locked, as per the vnode lookup 518 1.90 dholland * locking protocol. 519 1.90 dholland * 520 1.90 dholland * There is no way for this function to fail, in the sense of 521 1.90 dholland * generating an error that requires aborting the namei operation. 522 1.90 dholland * 523 1.90 dholland * (Prior to October 2012, this function returned an integer status, 524 1.90 dholland * and a vnode, and mucked with the flags word in CNP for whiteouts. 525 1.90 dholland * The integer status was -1 for "nothing found", ENOENT for "a 526 1.90 dholland * negative entry found", 0 for "a positive entry found", and possibly 527 1.90 dholland * other errors, and the value of VN might or might not have been set 528 1.90 dholland * depending on what error occurred.) 529 1.1 cgd */ 530 1.113 riastrad bool 531 1.91 dholland cache_lookup(struct vnode *dvp, const char *name, size_t namelen, 532 1.91 dholland uint32_t nameiop, uint32_t cnflags, 533 1.90 dholland int *iswht_ret, struct vnode **vn_ret) 534 1.1 cgd { 535 1.128 ad vnode_impl_t *dvi = VNODE_TO_VIMPL(dvp); 536 1.23 augustss struct namecache *ncp; 537 1.20 jdolecek struct vnode *vp; 538 1.155 ad uintptr_t key; 539 1.113 riastrad int error; 540 1.113 riastrad bool hit; 541 1.128 ad krw_t op; 542 1.125 ad 543 1.146 ad KASSERT(namelen != cache_mp_nlen || name == cache_mp_name); 544 1.146 ad 545 1.90 dholland /* Establish default result values */ 546 1.90 dholland if (iswht_ret != NULL) { 547 1.90 dholland *iswht_ret = 0; 548 1.90 dholland } 549 1.90 dholland *vn_ret = NULL; 550 1.90 dholland 551 1.128 ad if (__predict_false(namelen > cache_maxlen)) { 552 1.128 ad SDT_PROBE(vfs, namecache, lookup, toolong, dvp, 553 1.128 ad name, namelen, 0, 0); 554 1.128 ad COUNT(ncs_long); 555 1.113 riastrad return false; 556 1.8 cgd } 557 1.39 pk 558 1.128 ad /* Compute the key up front - don't need the lock. */ 559 1.128 ad key = cache_key(name, namelen); 560 1.128 ad 561 1.128 ad /* Could the entry be purged below? */ 562 1.128 ad if ((cnflags & ISLASTCN) != 0 && 563 1.128 ad ((cnflags & MAKEENTRY) == 0 || nameiop == CREATE)) { 564 1.158 riastrad op = RW_WRITER; 565 1.128 ad } else { 566 1.128 ad op = RW_READER; 567 1.1 cgd } 568 1.103 dennis 569 1.128 ad /* Now look for the name. */ 570 1.128 ad rw_enter(&dvi->vi_nc_lock, op); 571 1.128 ad ncp = cache_lookup_entry(dvp, name, namelen, key); 572 1.77 ad if (__predict_false(ncp == NULL)) { 573 1.128 ad rw_exit(&dvi->vi_nc_lock); 574 1.128 ad COUNT(ncs_miss); 575 1.128 ad SDT_PROBE(vfs, namecache, lookup, miss, dvp, 576 1.128 ad name, namelen, 0, 0); 577 1.113 riastrad return false; 578 1.1 cgd } 579 1.128 ad if (__predict_false((cnflags & MAKEENTRY) == 0)) { 580 1.77 ad /* 581 1.77 ad * Last component and we are renaming or deleting, 582 1.77 ad * the cache entry is invalid, or otherwise don't 583 1.77 ad * want cache entry to exist. 584 1.77 ad */ 585 1.128 ad KASSERT((cnflags & ISLASTCN) != 0); 586 1.128 ad cache_remove(ncp, true); 587 1.128 ad rw_exit(&dvi->vi_nc_lock); 588 1.128 ad COUNT(ncs_badhits); 589 1.113 riastrad return false; 590 1.90 dholland } 591 1.155 ad if ((vp = ncp->nc_vp) == NULL) { 592 1.136 ad if (iswht_ret != NULL) { 593 1.136 ad /* 594 1.136 ad * Restore the ISWHITEOUT flag saved earlier. 595 1.136 ad */ 596 1.136 ad *iswht_ret = ncp->nc_whiteout; 597 1.136 ad } else { 598 1.136 ad KASSERT(!ncp->nc_whiteout); 599 1.136 ad } 600 1.128 ad if (nameiop == CREATE && (cnflags & ISLASTCN) != 0) { 601 1.90 dholland /* 602 1.128 ad * Last component and we are preparing to create 603 1.128 ad * the named object, so flush the negative cache 604 1.128 ad * entry. 605 1.90 dholland */ 606 1.128 ad COUNT(ncs_badhits); 607 1.128 ad cache_remove(ncp, true); 608 1.128 ad hit = false; 609 1.90 dholland } else { 610 1.128 ad COUNT(ncs_neghits); 611 1.128 ad SDT_PROBE(vfs, namecache, lookup, hit, dvp, name, 612 1.128 ad namelen, 0, 0); 613 1.90 dholland /* found neg entry; vn is already null from above */ 614 1.113 riastrad hit = true; 615 1.128 ad } 616 1.128 ad rw_exit(&dvi->vi_nc_lock); 617 1.113 riastrad return hit; 618 1.20 jdolecek } 619 1.144 ad error = vcache_tryvget(vp); 620 1.128 ad rw_exit(&dvi->vi_nc_lock); 621 1.92 hannken if (error) { 622 1.92 hannken KASSERT(error == EBUSY); 623 1.92 hannken /* 624 1.92 hannken * This vnode is being cleaned out. 625 1.92 hannken * XXX badhits? 626 1.92 hannken */ 627 1.128 ad COUNT(ncs_falsehits); 628 1.113 riastrad return false; 629 1.77 ad } 630 1.101 christos 631 1.128 ad COUNT(ncs_goodhits); 632 1.128 ad SDT_PROBE(vfs, namecache, lookup, hit, dvp, name, namelen, 0, 0); 633 1.101 christos /* found it */ 634 1.101 christos *vn_ret = vp; 635 1.113 riastrad return true; 636 1.1 cgd } 637 1.1 cgd 638 1.103 dennis /* 639 1.128 ad * Version of the above without the nameiop argument, for NFS. 640 1.103 dennis */ 641 1.113 riastrad bool 642 1.91 dholland cache_lookup_raw(struct vnode *dvp, const char *name, size_t namelen, 643 1.91 dholland uint32_t cnflags, 644 1.90 dholland int *iswht_ret, struct vnode **vn_ret) 645 1.61 yamt { 646 1.128 ad 647 1.128 ad return cache_lookup(dvp, name, namelen, LOOKUP, cnflags | MAKEENTRY, 648 1.128 ad iswht_ret, vn_ret); 649 1.128 ad } 650 1.128 ad 651 1.128 ad /* 652 1.128 ad * Used by namei() to walk down a path, component by component by looking up 653 1.128 ad * names in the cache. The node locks are chained along the way: a parent's 654 1.128 ad * lock is not dropped until the child's is acquired. 655 1.128 ad */ 656 1.128 ad bool 657 1.128 ad cache_lookup_linked(struct vnode *dvp, const char *name, size_t namelen, 658 1.128 ad struct vnode **vn_ret, krwlock_t **plock, 659 1.128 ad kauth_cred_t cred) 660 1.128 ad { 661 1.128 ad vnode_impl_t *dvi = VNODE_TO_VIMPL(dvp); 662 1.61 yamt struct namecache *ncp; 663 1.145 ad krwlock_t *oldlock, *newlock; 664 1.155 ad struct vnode *vp; 665 1.155 ad uintptr_t key; 666 1.101 christos int error; 667 1.61 yamt 668 1.146 ad KASSERT(namelen != cache_mp_nlen || name == cache_mp_name); 669 1.146 ad 670 1.128 ad /* If disabled, or file system doesn't support this, bail out. */ 671 1.131 ad if (__predict_false((dvp->v_mount->mnt_iflag & IMNT_NCLOOKUP) == 0)) { 672 1.113 riastrad return false; 673 1.61 yamt } 674 1.61 yamt 675 1.131 ad if (__predict_false(namelen > cache_maxlen)) { 676 1.128 ad COUNT(ncs_long); 677 1.128 ad return false; 678 1.128 ad } 679 1.128 ad 680 1.128 ad /* Compute the key up front - don't need the lock. */ 681 1.128 ad key = cache_key(name, namelen); 682 1.128 ad 683 1.128 ad /* 684 1.128 ad * Acquire the directory lock. Once we have that, we can drop the 685 1.128 ad * previous one (if any). 686 1.128 ad * 687 1.128 ad * The two lock holds mean that the directory can't go away while 688 1.128 ad * here: the directory must be purged with cache_purge() before 689 1.128 ad * being freed, and both parent & child's vi_nc_lock must be taken 690 1.128 ad * before that point is passed. 691 1.128 ad * 692 1.128 ad * However if there's no previous lock, like at the root of the 693 1.128 ad * chain, then "dvp" must be referenced to prevent dvp going away 694 1.128 ad * before we get its lock. 695 1.128 ad * 696 1.128 ad * Note that the two locks can be the same if looking up a dot, for 697 1.140 ad * example: /usr/bin/. If looking up the parent (..) we can't wait 698 1.140 ad * on the lock as child -> parent is the wrong direction. 699 1.128 ad */ 700 1.128 ad if (*plock != &dvi->vi_nc_lock) { 701 1.145 ad oldlock = *plock; 702 1.145 ad newlock = &dvi->vi_nc_lock; 703 1.141 ad if (!rw_tryenter(&dvi->vi_nc_lock, RW_READER)) { 704 1.141 ad return false; 705 1.140 ad } 706 1.145 ad } else { 707 1.145 ad oldlock = NULL; 708 1.145 ad newlock = NULL; 709 1.145 ad if (*plock == NULL) { 710 1.145 ad KASSERT(vrefcnt(dvp) > 0); 711 1.128 ad } 712 1.128 ad } 713 1.128 ad 714 1.128 ad /* 715 1.128 ad * First up check if the user is allowed to look up files in this 716 1.128 ad * directory. 717 1.128 ad */ 718 1.145 ad if (cred != FSCRED) { 719 1.145 ad if (dvi->vi_nc_mode == VNOVAL) { 720 1.145 ad if (newlock != NULL) { 721 1.145 ad rw_exit(newlock); 722 1.145 ad } 723 1.145 ad return false; 724 1.145 ad } 725 1.153 riastrad KASSERT(dvi->vi_nc_uid != VNOVAL); 726 1.153 riastrad KASSERT(dvi->vi_nc_gid != VNOVAL); 727 1.151 christos error = kauth_authorize_vnode(cred, 728 1.151 christos KAUTH_ACCESS_ACTION(VEXEC, 729 1.158 riastrad dvp->v_type, dvi->vi_nc_mode & ALLPERMS), 730 1.158 riastrad dvp, NULL, 731 1.145 ad genfs_can_access(dvp, cred, dvi->vi_nc_uid, dvi->vi_nc_gid, 732 1.158 riastrad dvi->vi_nc_mode & ALLPERMS, NULL, VEXEC)); 733 1.145 ad if (error != 0) { 734 1.145 ad if (newlock != NULL) { 735 1.145 ad rw_exit(newlock); 736 1.145 ad } 737 1.145 ad COUNT(ncs_denied); 738 1.145 ad return false; 739 1.145 ad } 740 1.61 yamt } 741 1.128 ad 742 1.128 ad /* 743 1.128 ad * Now look for a matching cache entry. 744 1.128 ad */ 745 1.128 ad ncp = cache_lookup_entry(dvp, name, namelen, key); 746 1.77 ad if (__predict_false(ncp == NULL)) { 747 1.145 ad if (newlock != NULL) { 748 1.145 ad rw_exit(newlock); 749 1.145 ad } 750 1.128 ad COUNT(ncs_miss); 751 1.128 ad SDT_PROBE(vfs, namecache, lookup, miss, dvp, 752 1.128 ad name, namelen, 0, 0); 753 1.113 riastrad return false; 754 1.61 yamt } 755 1.155 ad if ((vp = ncp->nc_vp) == NULL) { 756 1.90 dholland /* found negative entry; vn is already null from above */ 757 1.153 riastrad KASSERT(namelen != cache_mp_nlen); 758 1.153 riastrad KASSERT(name != cache_mp_name); 759 1.128 ad COUNT(ncs_neghits); 760 1.145 ad } else { 761 1.145 ad COUNT(ncs_goodhits); /* XXX can be "badhits" */ 762 1.61 yamt } 763 1.128 ad SDT_PROBE(vfs, namecache, lookup, hit, dvp, name, namelen, 0, 0); 764 1.103 dennis 765 1.103 dennis /* 766 1.128 ad * Return with the directory lock still held. It will either be 767 1.128 ad * returned to us with another call to cache_lookup_linked() when 768 1.128 ad * looking up the next component, or the caller will release it 769 1.128 ad * manually when finished. 770 1.103 dennis */ 771 1.145 ad if (oldlock) { 772 1.145 ad rw_exit(oldlock); 773 1.145 ad } 774 1.145 ad if (newlock) { 775 1.145 ad *plock = newlock; 776 1.147 riastrad } 777 1.155 ad *vn_ret = vp; 778 1.113 riastrad return true; 779 1.61 yamt } 780 1.61 yamt 781 1.1 cgd /* 782 1.19 sommerfe * Scan cache looking for name of directory entry pointing at vp. 783 1.128 ad * Will not search for "." or "..". 784 1.19 sommerfe * 785 1.86 hannken * If the lookup succeeds the vnode is referenced and stored in dvpp. 786 1.19 sommerfe * 787 1.19 sommerfe * If bufp is non-NULL, also place the name in the buffer which starts 788 1.19 sommerfe * at bufp, immediately before *bpp, and move bpp backwards to point 789 1.19 sommerfe * at the start of it. (Yes, this is a little baroque, but it's done 790 1.19 sommerfe * this way to cater to the whims of getcwd). 791 1.19 sommerfe * 792 1.19 sommerfe * Returns 0 on success, -1 on cache miss, positive errno on failure. 793 1.19 sommerfe */ 794 1.19 sommerfe int 795 1.128 ad cache_revlookup(struct vnode *vp, struct vnode **dvpp, char **bpp, char *bufp, 796 1.143 christos bool checkaccess, accmode_t accmode) 797 1.19 sommerfe { 798 1.128 ad vnode_impl_t *vi = VNODE_TO_VIMPL(vp); 799 1.19 sommerfe struct namecache *ncp; 800 1.155 ad enum cache_lru_id lrulist; 801 1.19 sommerfe struct vnode *dvp; 802 1.155 ad int error, nlen; 803 1.34 enami char *bp; 804 1.34 enami 805 1.126 ad KASSERT(vp != NULL); 806 1.126 ad 807 1.128 ad if (cache_maxlen == 0) 808 1.19 sommerfe goto out; 809 1.19 sommerfe 810 1.128 ad rw_enter(&vi->vi_nc_listlock, RW_READER); 811 1.128 ad if (checkaccess) { 812 1.128 ad /* 813 1.128 ad * Check if the user is allowed to see. NOTE: this is 814 1.128 ad * checking for access on the "wrong" directory. getcwd() 815 1.128 ad * wants to see that there is access on every component 816 1.128 ad * along the way, not that there is access to any individual 817 1.128 ad * component. Don't use this to check you can look in vp. 818 1.128 ad * 819 1.128 ad * I don't like it, I didn't come up with it, don't blame me! 820 1.128 ad */ 821 1.142 ad if (vi->vi_nc_mode == VNOVAL) { 822 1.142 ad rw_exit(&vi->vi_nc_listlock); 823 1.142 ad return -1; 824 1.142 ad } 825 1.153 riastrad KASSERT(vi->vi_nc_uid != VNOVAL); 826 1.153 riastrad KASSERT(vi->vi_nc_gid != VNOVAL); 827 1.151 christos error = kauth_authorize_vnode(kauth_cred_get(), 828 1.128 ad KAUTH_ACCESS_ACTION(VEXEC, vp->v_type, vi->vi_nc_mode & 829 1.158 riastrad ALLPERMS), 830 1.158 riastrad vp, NULL, genfs_can_access(vp, curlwp->l_cred, 831 1.158 riastrad vi->vi_nc_uid, vi->vi_nc_gid, 832 1.158 riastrad vi->vi_nc_mode & ALLPERMS, 833 1.158 riastrad NULL, accmode)); 834 1.158 riastrad if (error != 0) { 835 1.158 riastrad rw_exit(&vi->vi_nc_listlock); 836 1.128 ad COUNT(ncs_denied); 837 1.159 riastrad return SET_ERROR(EACCES); 838 1.127 ad } 839 1.128 ad } 840 1.128 ad TAILQ_FOREACH(ncp, &vi->vi_nc_list, nc_list) { 841 1.128 ad KASSERT(ncp->nc_vp == vp); 842 1.128 ad KASSERT(ncp->nc_dvp != NULL); 843 1.155 ad nlen = NC_NLEN(ncp); 844 1.128 ad 845 1.127 ad /* 846 1.146 ad * Ignore mountpoint entries. 847 1.146 ad */ 848 1.155 ad if (nlen == cache_mp_nlen) { 849 1.146 ad continue; 850 1.146 ad } 851 1.146 ad 852 1.146 ad /* 853 1.128 ad * The queue is partially sorted. Once we hit dots, nothing 854 1.128 ad * else remains but dots and dotdots, so bail out. 855 1.127 ad */ 856 1.127 ad if (ncp->nc_name[0] == '.') { 857 1.127 ad if (nlen == 1 || 858 1.127 ad (nlen == 2 && ncp->nc_name[1] == '.')) { 859 1.158 riastrad break; 860 1.19 sommerfe } 861 1.127 ad } 862 1.128 ad 863 1.135 ad /* 864 1.135 ad * Record a hit on the entry. This is an unlocked read but 865 1.135 ad * even if wrong it doesn't matter too much. 866 1.135 ad */ 867 1.128 ad lrulist = atomic_load_relaxed(&ncp->nc_lrulist); 868 1.128 ad if (lrulist != LRU_ACTIVE) { 869 1.128 ad cache_activate(ncp); 870 1.128 ad } 871 1.34 enami 872 1.127 ad if (bufp) { 873 1.127 ad bp = *bpp; 874 1.127 ad bp -= nlen; 875 1.127 ad if (bp <= bufp) { 876 1.92 hannken *dvpp = NULL; 877 1.128 ad rw_exit(&vi->vi_nc_listlock); 878 1.127 ad SDT_PROBE(vfs, namecache, revlookup, 879 1.127 ad fail, vp, ERANGE, 0, 0, 0); 880 1.159 riastrad return SET_ERROR(ERANGE); 881 1.86 hannken } 882 1.127 ad memcpy(bp, ncp->nc_name, nlen); 883 1.127 ad *bpp = bp; 884 1.19 sommerfe } 885 1.127 ad 886 1.128 ad dvp = ncp->nc_dvp; 887 1.144 ad error = vcache_tryvget(dvp); 888 1.128 ad rw_exit(&vi->vi_nc_listlock); 889 1.127 ad if (error) { 890 1.127 ad KASSERT(error == EBUSY); 891 1.127 ad if (bufp) 892 1.127 ad (*bpp) += nlen; 893 1.127 ad *dvpp = NULL; 894 1.127 ad SDT_PROBE(vfs, namecache, revlookup, fail, vp, 895 1.127 ad error, 0, 0, 0); 896 1.127 ad return -1; 897 1.127 ad } 898 1.127 ad *dvpp = dvp; 899 1.127 ad SDT_PROBE(vfs, namecache, revlookup, success, vp, dvp, 900 1.127 ad 0, 0, 0); 901 1.128 ad COUNT(ncs_revhits); 902 1.159 riastrad return 0; 903 1.19 sommerfe } 904 1.128 ad rw_exit(&vi->vi_nc_listlock); 905 1.128 ad COUNT(ncs_revmiss); 906 1.157 riastrad out: 907 1.34 enami *dvpp = NULL; 908 1.159 riastrad return -1; 909 1.19 sommerfe } 910 1.19 sommerfe 911 1.19 sommerfe /* 912 1.128 ad * Add an entry to the cache. 913 1.1 cgd */ 914 1.13 christos void 915 1.91 dholland cache_enter(struct vnode *dvp, struct vnode *vp, 916 1.91 dholland const char *name, size_t namelen, uint32_t cnflags) 917 1.1 cgd { 918 1.128 ad vnode_impl_t *dvi = VNODE_TO_VIMPL(dvp); 919 1.128 ad struct namecache *ncp, *oncp; 920 1.128 ad int total; 921 1.1 cgd 922 1.146 ad KASSERT(namelen != cache_mp_nlen || name == cache_mp_name); 923 1.146 ad 924 1.89 rmind /* First, check whether we can/should add a cache entry. */ 925 1.91 dholland if ((cnflags & MAKEENTRY) == 0 || 926 1.128 ad __predict_false(namelen > cache_maxlen)) { 927 1.108 christos SDT_PROBE(vfs, namecache, enter, toolong, vp, name, namelen, 928 1.108 christos 0, 0); 929 1.1 cgd return; 930 1.89 rmind } 931 1.58 yamt 932 1.108 christos SDT_PROBE(vfs, namecache, enter, done, vp, name, namelen, 0, 0); 933 1.128 ad 934 1.128 ad /* 935 1.128 ad * Reclaim some entries if over budget. This is an unlocked check, 936 1.128 ad * but it doesn't matter. Just need to catch up with things 937 1.128 ad * eventually: it doesn't matter if we go over temporarily. 938 1.128 ad */ 939 1.128 ad total = atomic_load_relaxed(&cache_lru.count[LRU_ACTIVE]); 940 1.128 ad total += atomic_load_relaxed(&cache_lru.count[LRU_INACTIVE]); 941 1.128 ad if (__predict_false(total > desiredvnodes)) { 942 1.73 ad cache_reclaim(); 943 1.39 pk } 944 1.57 pk 945 1.128 ad /* Now allocate a fresh entry. */ 946 1.128 ad if (__predict_true(namelen <= NCHNAMLEN)) { 947 1.128 ad ncp = pool_cache_get(cache_pool, PR_WAITOK); 948 1.128 ad } else { 949 1.128 ad size_t sz = offsetof(struct namecache, nc_name[namelen]); 950 1.128 ad ncp = kmem_alloc(sz, KM_SLEEP); 951 1.128 ad } 952 1.122 maya 953 1.130 ad /* 954 1.130 ad * Fill in cache info. For negative hits, save the ISWHITEOUT flag 955 1.130 ad * so we can restore it later when the cache entry is used again. 956 1.130 ad */ 957 1.130 ad ncp->nc_vp = vp; 958 1.128 ad ncp->nc_dvp = dvp; 959 1.128 ad ncp->nc_key = cache_key(name, namelen); 960 1.130 ad ncp->nc_whiteout = ((cnflags & ISWHITEOUT) != 0); 961 1.128 ad memcpy(ncp->nc_name, name, namelen); 962 1.73 ad 963 1.59 yamt /* 964 1.130 ad * Insert to the directory. Concurrent lookups may race for a cache 965 1.130 ad * entry. If there's a entry there already, purge it. 966 1.59 yamt */ 967 1.128 ad rw_enter(&dvi->vi_nc_lock, RW_WRITER); 968 1.128 ad oncp = rb_tree_insert_node(&dvi->vi_nc_tree, ncp); 969 1.128 ad if (oncp != ncp) { 970 1.128 ad KASSERT(oncp->nc_key == ncp->nc_key); 971 1.155 ad KASSERT(NC_NLEN(oncp) == NC_NLEN(ncp)); 972 1.131 ad KASSERT(memcmp(oncp->nc_name, name, namelen) == 0); 973 1.128 ad cache_remove(oncp, true); 974 1.128 ad oncp = rb_tree_insert_node(&dvi->vi_nc_tree, ncp); 975 1.128 ad KASSERT(oncp == ncp); 976 1.59 yamt } 977 1.59 yamt 978 1.130 ad /* 979 1.130 ad * With the directory lock still held, insert to the tail of the 980 1.135 ad * ACTIVE LRU list (new) and take the opportunity to incrementally 981 1.135 ad * balance the lists. 982 1.130 ad */ 983 1.130 ad mutex_enter(&cache_lru_lock); 984 1.130 ad ncp->nc_lrulist = LRU_ACTIVE; 985 1.130 ad cache_lru.count[LRU_ACTIVE]++; 986 1.130 ad TAILQ_INSERT_TAIL(&cache_lru.list[LRU_ACTIVE], ncp, nc_lru); 987 1.130 ad cache_deactivate(); 988 1.130 ad mutex_exit(&cache_lru_lock); 989 1.130 ad 990 1.130 ad /* 991 1.135 ad * Finally, insert to the vnode and unlock. With everything set up 992 1.135 ad * it's safe to let cache_revlookup() see the entry. Partially sort 993 1.135 ad * the per-vnode list: dots go to back so cache_revlookup() doesn't 994 1.135 ad * have to consider them. 995 1.130 ad */ 996 1.130 ad if (vp != NULL) { 997 1.128 ad vnode_impl_t *vi = VNODE_TO_VIMPL(vp); 998 1.128 ad rw_enter(&vi->vi_nc_listlock, RW_WRITER); 999 1.127 ad if ((namelen == 1 && name[0] == '.') || 1000 1.127 ad (namelen == 2 && name[0] == '.' && name[1] == '.')) { 1001 1.128 ad TAILQ_INSERT_TAIL(&vi->vi_nc_list, ncp, nc_list); 1002 1.127 ad } else { 1003 1.128 ad TAILQ_INSERT_HEAD(&vi->vi_nc_list, ncp, nc_list); 1004 1.127 ad } 1005 1.128 ad rw_exit(&vi->vi_nc_listlock); 1006 1.73 ad } 1007 1.128 ad rw_exit(&dvi->vi_nc_lock); 1008 1.128 ad } 1009 1.128 ad 1010 1.128 ad /* 1011 1.128 ad * Set identity info in cache for a vnode. We only care about directories 1012 1.142 ad * so ignore other updates. The cached info may be marked invalid if the 1013 1.142 ad * inode has an ACL. 1014 1.128 ad */ 1015 1.128 ad void 1016 1.142 ad cache_enter_id(struct vnode *vp, mode_t mode, uid_t uid, gid_t gid, bool valid) 1017 1.128 ad { 1018 1.128 ad vnode_impl_t *vi = VNODE_TO_VIMPL(vp); 1019 1.128 ad 1020 1.128 ad if (vp->v_type == VDIR) { 1021 1.128 ad /* Grab both locks, for forward & reverse lookup. */ 1022 1.128 ad rw_enter(&vi->vi_nc_lock, RW_WRITER); 1023 1.128 ad rw_enter(&vi->vi_nc_listlock, RW_WRITER); 1024 1.142 ad if (valid) { 1025 1.142 ad vi->vi_nc_mode = mode; 1026 1.142 ad vi->vi_nc_uid = uid; 1027 1.142 ad vi->vi_nc_gid = gid; 1028 1.142 ad } else { 1029 1.142 ad vi->vi_nc_mode = VNOVAL; 1030 1.142 ad vi->vi_nc_uid = VNOVAL; 1031 1.142 ad vi->vi_nc_gid = VNOVAL; 1032 1.142 ad } 1033 1.128 ad rw_exit(&vi->vi_nc_listlock); 1034 1.128 ad rw_exit(&vi->vi_nc_lock); 1035 1.128 ad } 1036 1.128 ad } 1037 1.128 ad 1038 1.128 ad /* 1039 1.128 ad * Return true if we have identity for the given vnode, and use as an 1040 1.128 ad * opportunity to confirm that everything squares up. 1041 1.128 ad * 1042 1.128 ad * Because of shared code, some file systems could provide partial 1043 1.142 ad * information, missing some updates, so check the mount flag too. 1044 1.128 ad */ 1045 1.128 ad bool 1046 1.128 ad cache_have_id(struct vnode *vp) 1047 1.128 ad { 1048 1.128 ad 1049 1.128 ad if (vp->v_type == VDIR && 1050 1.142 ad (vp->v_mount->mnt_iflag & IMNT_NCLOOKUP) != 0 && 1051 1.142 ad atomic_load_relaxed(&VNODE_TO_VIMPL(vp)->vi_nc_mode) != VNOVAL) { 1052 1.128 ad return true; 1053 1.128 ad } else { 1054 1.128 ad return false; 1055 1.128 ad } 1056 1.1 cgd } 1057 1.1 cgd 1058 1.1 cgd /* 1059 1.146 ad * Enter a mount point. cvp is the covered vnode, and rvp is the root of 1060 1.146 ad * the mounted file system. 1061 1.146 ad */ 1062 1.146 ad void 1063 1.146 ad cache_enter_mount(struct vnode *cvp, struct vnode *rvp) 1064 1.146 ad { 1065 1.146 ad 1066 1.146 ad KASSERT(vrefcnt(cvp) > 0); 1067 1.146 ad KASSERT(vrefcnt(rvp) > 0); 1068 1.146 ad KASSERT(cvp->v_type == VDIR); 1069 1.146 ad KASSERT((rvp->v_vflag & VV_ROOT) != 0); 1070 1.146 ad 1071 1.146 ad if (rvp->v_type == VDIR) { 1072 1.146 ad cache_enter(cvp, rvp, cache_mp_name, cache_mp_nlen, MAKEENTRY); 1073 1.146 ad } 1074 1.146 ad } 1075 1.146 ad 1076 1.146 ad /* 1077 1.146 ad * Look up a cached mount point. Used in the strongly locked path. 1078 1.146 ad */ 1079 1.146 ad bool 1080 1.146 ad cache_lookup_mount(struct vnode *dvp, struct vnode **vn_ret) 1081 1.146 ad { 1082 1.146 ad bool ret; 1083 1.146 ad 1084 1.146 ad ret = cache_lookup(dvp, cache_mp_name, cache_mp_nlen, LOOKUP, 1085 1.146 ad MAKEENTRY, NULL, vn_ret); 1086 1.146 ad KASSERT((*vn_ret != NULL) == ret); 1087 1.146 ad return ret; 1088 1.146 ad } 1089 1.146 ad 1090 1.146 ad /* 1091 1.146 ad * Try to cross a mount point. For use with cache_lookup_linked(). 1092 1.146 ad */ 1093 1.146 ad bool 1094 1.146 ad cache_cross_mount(struct vnode **dvp, krwlock_t **plock) 1095 1.146 ad { 1096 1.146 ad 1097 1.146 ad return cache_lookup_linked(*dvp, cache_mp_name, cache_mp_nlen, 1098 1.146 ad dvp, plock, FSCRED); 1099 1.146 ad } 1100 1.146 ad 1101 1.146 ad /* 1102 1.128 ad * Name cache initialization, from vfs_init() when the system is booting. 1103 1.1 cgd */ 1104 1.13 christos void 1105 1.34 enami nchinit(void) 1106 1.1 cgd { 1107 1.1 cgd 1108 1.128 ad cache_pool = pool_cache_init(sizeof(struct namecache), 1109 1.135 ad coherency_unit, 0, 0, "namecache", NULL, IPL_NONE, NULL, 1110 1.128 ad NULL, NULL); 1111 1.128 ad KASSERT(cache_pool != NULL); 1112 1.128 ad 1113 1.128 ad mutex_init(&cache_lru_lock, MUTEX_DEFAULT, IPL_NONE); 1114 1.128 ad TAILQ_INIT(&cache_lru.list[LRU_ACTIVE]); 1115 1.128 ad TAILQ_INIT(&cache_lru.list[LRU_INACTIVE]); 1116 1.128 ad 1117 1.128 ad mutex_init(&cache_stat_lock, MUTEX_DEFAULT, IPL_NONE); 1118 1.128 ad callout_init(&cache_stat_callout, CALLOUT_MPSAFE); 1119 1.128 ad callout_setfunc(&cache_stat_callout, cache_update_stats, NULL); 1120 1.128 ad callout_schedule(&cache_stat_callout, cache_stat_interval * hz); 1121 1.128 ad 1122 1.128 ad KASSERT(cache_sysctllog == NULL); 1123 1.128 ad sysctl_createv(&cache_sysctllog, 0, NULL, NULL, 1124 1.128 ad CTLFLAG_PERMANENT, 1125 1.128 ad CTLTYPE_STRUCT, "namecache_stats", 1126 1.128 ad SYSCTL_DESCR("namecache statistics"), 1127 1.128 ad cache_stat_sysctl, 0, NULL, 0, 1128 1.128 ad CTL_VFS, CTL_CREATE, CTL_EOL); 1129 1.128 ad } 1130 1.128 ad 1131 1.128 ad /* 1132 1.128 ad * Called once for each CPU in the system as attached. 1133 1.128 ad */ 1134 1.128 ad void 1135 1.128 ad cache_cpu_init(struct cpu_info *ci) 1136 1.128 ad { 1137 1.128 ad size_t sz; 1138 1.104 pooka 1139 1.155 ad sz = roundup2(sizeof(struct nchcpu), coherency_unit); 1140 1.155 ad ci->ci_data.cpu_nch = kmem_zalloc(sz, KM_SLEEP); 1141 1.155 ad KASSERT(((uintptr_t)ci->ci_data.cpu_nch & (coherency_unit - 1)) == 0); 1142 1.73 ad } 1143 1.73 ad 1144 1.128 ad /* 1145 1.128 ad * A vnode is being allocated: set up cache structures. 1146 1.128 ad */ 1147 1.128 ad void 1148 1.128 ad cache_vnode_init(struct vnode *vp) 1149 1.73 ad { 1150 1.128 ad vnode_impl_t *vi = VNODE_TO_VIMPL(vp); 1151 1.128 ad 1152 1.128 ad rw_init(&vi->vi_nc_lock); 1153 1.128 ad rw_init(&vi->vi_nc_listlock); 1154 1.128 ad rb_tree_init(&vi->vi_nc_tree, &cache_rbtree_ops); 1155 1.128 ad TAILQ_INIT(&vi->vi_nc_list); 1156 1.128 ad vi->vi_nc_mode = VNOVAL; 1157 1.128 ad vi->vi_nc_uid = VNOVAL; 1158 1.128 ad vi->vi_nc_gid = VNOVAL; 1159 1.128 ad } 1160 1.125 ad 1161 1.128 ad /* 1162 1.128 ad * A vnode is being freed: finish cache structures. 1163 1.128 ad */ 1164 1.128 ad void 1165 1.128 ad cache_vnode_fini(struct vnode *vp) 1166 1.128 ad { 1167 1.128 ad vnode_impl_t *vi = VNODE_TO_VIMPL(vp); 1168 1.73 ad 1169 1.128 ad KASSERT(RB_TREE_MIN(&vi->vi_nc_tree) == NULL); 1170 1.128 ad KASSERT(TAILQ_EMPTY(&vi->vi_nc_list)); 1171 1.128 ad rw_destroy(&vi->vi_nc_lock); 1172 1.128 ad rw_destroy(&vi->vi_nc_listlock); 1173 1.73 ad } 1174 1.73 ad 1175 1.128 ad /* 1176 1.128 ad * Helper for cache_purge1(): purge cache entries for the given vnode from 1177 1.128 ad * all directories that the vnode is cached in. 1178 1.128 ad */ 1179 1.73 ad static void 1180 1.128 ad cache_purge_parents(struct vnode *vp) 1181 1.73 ad { 1182 1.128 ad vnode_impl_t *dvi, *vi = VNODE_TO_VIMPL(vp); 1183 1.128 ad struct vnode *dvp, *blocked; 1184 1.125 ad struct namecache *ncp; 1185 1.73 ad 1186 1.128 ad SDT_PROBE(vfs, namecache, purge, parents, vp, 0, 0, 0, 0); 1187 1.128 ad 1188 1.128 ad blocked = NULL; 1189 1.128 ad 1190 1.128 ad rw_enter(&vi->vi_nc_listlock, RW_WRITER); 1191 1.128 ad while ((ncp = TAILQ_FIRST(&vi->vi_nc_list)) != NULL) { 1192 1.128 ad /* 1193 1.128 ad * Locking in the wrong direction. Try for a hold on the 1194 1.128 ad * directory node's lock, and if we get it then all good, 1195 1.128 ad * nuke the entry and move on to the next. 1196 1.128 ad */ 1197 1.128 ad dvp = ncp->nc_dvp; 1198 1.128 ad dvi = VNODE_TO_VIMPL(dvp); 1199 1.128 ad if (rw_tryenter(&dvi->vi_nc_lock, RW_WRITER)) { 1200 1.128 ad cache_remove(ncp, false); 1201 1.128 ad rw_exit(&dvi->vi_nc_lock); 1202 1.128 ad blocked = NULL; 1203 1.128 ad continue; 1204 1.128 ad } 1205 1.128 ad 1206 1.128 ad /* 1207 1.128 ad * We can't wait on the directory node's lock with our list 1208 1.128 ad * lock held or the system could deadlock. 1209 1.128 ad * 1210 1.128 ad * Take a hold on the directory vnode to prevent it from 1211 1.128 ad * being freed (taking the vnode & lock with it). Then 1212 1.128 ad * wait for the lock to become available with no other locks 1213 1.128 ad * held, and retry. 1214 1.128 ad * 1215 1.128 ad * If this happens twice in a row, give the other side a 1216 1.128 ad * breather; we can do nothing until it lets go. 1217 1.128 ad */ 1218 1.128 ad vhold(dvp); 1219 1.128 ad rw_exit(&vi->vi_nc_listlock); 1220 1.128 ad rw_enter(&dvi->vi_nc_lock, RW_WRITER); 1221 1.128 ad /* Do nothing. */ 1222 1.128 ad rw_exit(&dvi->vi_nc_lock); 1223 1.128 ad holdrele(dvp); 1224 1.128 ad if (blocked == dvp) { 1225 1.128 ad kpause("ncpurge", false, 1, NULL); 1226 1.128 ad } 1227 1.128 ad rw_enter(&vi->vi_nc_listlock, RW_WRITER); 1228 1.128 ad blocked = dvp; 1229 1.128 ad } 1230 1.128 ad rw_exit(&vi->vi_nc_listlock); 1231 1.73 ad } 1232 1.73 ad 1233 1.73 ad /* 1234 1.128 ad * Helper for cache_purge1(): purge all cache entries hanging off the given 1235 1.128 ad * directory vnode. 1236 1.73 ad */ 1237 1.128 ad static void 1238 1.128 ad cache_purge_children(struct vnode *dvp) 1239 1.73 ad { 1240 1.128 ad vnode_impl_t *dvi = VNODE_TO_VIMPL(dvp); 1241 1.128 ad struct namecache *ncp; 1242 1.128 ad 1243 1.128 ad SDT_PROBE(vfs, namecache, purge, children, dvp, 0, 0, 0, 0); 1244 1.73 ad 1245 1.128 ad rw_enter(&dvi->vi_nc_lock, RW_WRITER); 1246 1.135 ad while ((ncp = RB_TREE_MIN(&dvi->vi_nc_tree)) != NULL) { 1247 1.128 ad cache_remove(ncp, true); 1248 1.128 ad } 1249 1.128 ad rw_exit(&dvi->vi_nc_lock); 1250 1.30 chs } 1251 1.30 chs 1252 1.30 chs /* 1253 1.128 ad * Helper for cache_purge1(): purge cache entry from the given vnode, 1254 1.128 ad * finding it by name. 1255 1.30 chs */ 1256 1.128 ad static void 1257 1.128 ad cache_purge_name(struct vnode *dvp, const char *name, size_t namelen) 1258 1.30 chs { 1259 1.128 ad vnode_impl_t *dvi = VNODE_TO_VIMPL(dvp); 1260 1.30 chs struct namecache *ncp; 1261 1.155 ad uintptr_t key; 1262 1.126 ad 1263 1.128 ad SDT_PROBE(vfs, namecache, purge, name, name, namelen, 0, 0, 0); 1264 1.128 ad 1265 1.128 ad key = cache_key(name, namelen); 1266 1.128 ad rw_enter(&dvi->vi_nc_lock, RW_WRITER); 1267 1.128 ad ncp = cache_lookup_entry(dvp, name, namelen, key); 1268 1.128 ad if (ncp) { 1269 1.128 ad cache_remove(ncp, true); 1270 1.128 ad } 1271 1.128 ad rw_exit(&dvi->vi_nc_lock); 1272 1.1 cgd } 1273 1.1 cgd 1274 1.1 cgd /* 1275 1.1 cgd * Cache flush, a particular vnode; called when a vnode is renamed to 1276 1.128 ad * hide entries that would now be invalid. 1277 1.1 cgd */ 1278 1.13 christos void 1279 1.91 dholland cache_purge1(struct vnode *vp, const char *name, size_t namelen, int flags) 1280 1.1 cgd { 1281 1.1 cgd 1282 1.55 yamt if (flags & PURGE_PARENTS) { 1283 1.128 ad cache_purge_parents(vp); 1284 1.55 yamt } 1285 1.55 yamt if (flags & PURGE_CHILDREN) { 1286 1.128 ad cache_purge_children(vp); 1287 1.46 yamt } 1288 1.91 dholland if (name != NULL) { 1289 1.128 ad cache_purge_name(vp, name, namelen); 1290 1.46 yamt } 1291 1.128 ad } 1292 1.128 ad 1293 1.128 ad /* 1294 1.128 ad * vnode filter for cache_purgevfs(). 1295 1.128 ad */ 1296 1.128 ad static bool 1297 1.128 ad cache_vdir_filter(void *cookie, vnode_t *vp) 1298 1.128 ad { 1299 1.128 ad 1300 1.128 ad return vp->v_type == VDIR; 1301 1.1 cgd } 1302 1.1 cgd 1303 1.1 cgd /* 1304 1.1 cgd * Cache flush, a whole filesystem; called when filesys is umounted to 1305 1.27 chs * remove entries that would now be invalid. 1306 1.1 cgd */ 1307 1.13 christos void 1308 1.34 enami cache_purgevfs(struct mount *mp) 1309 1.1 cgd { 1310 1.128 ad struct vnode_iterator *iter; 1311 1.128 ad vnode_t *dvp; 1312 1.1 cgd 1313 1.128 ad vfs_vnode_iterator_init(mp, &iter); 1314 1.128 ad for (;;) { 1315 1.128 ad dvp = vfs_vnode_iterator_next(iter, cache_vdir_filter, NULL); 1316 1.128 ad if (dvp == NULL) { 1317 1.128 ad break; 1318 1.73 ad } 1319 1.128 ad cache_purge_children(dvp); 1320 1.128 ad vrele(dvp); 1321 1.73 ad } 1322 1.128 ad vfs_vnode_iterator_destroy(iter); 1323 1.73 ad } 1324 1.73 ad 1325 1.73 ad /* 1326 1.135 ad * Re-queue an entry onto the tail of the active LRU list, after it has 1327 1.135 ad * scored a hit. 1328 1.73 ad */ 1329 1.73 ad static void 1330 1.128 ad cache_activate(struct namecache *ncp) 1331 1.73 ad { 1332 1.73 ad 1333 1.128 ad mutex_enter(&cache_lru_lock); 1334 1.128 ad TAILQ_REMOVE(&cache_lru.list[ncp->nc_lrulist], ncp, nc_lru); 1335 1.128 ad TAILQ_INSERT_TAIL(&cache_lru.list[LRU_ACTIVE], ncp, nc_lru); 1336 1.128 ad cache_lru.count[ncp->nc_lrulist]--; 1337 1.128 ad cache_lru.count[LRU_ACTIVE]++; 1338 1.128 ad ncp->nc_lrulist = LRU_ACTIVE; 1339 1.128 ad mutex_exit(&cache_lru_lock); 1340 1.73 ad } 1341 1.73 ad 1342 1.73 ad /* 1343 1.128 ad * Try to balance the LRU lists. Pick some victim entries, and re-queue 1344 1.147 riastrad * them from the head of the active list to the tail of the inactive list. 1345 1.73 ad */ 1346 1.73 ad static void 1347 1.128 ad cache_deactivate(void) 1348 1.73 ad { 1349 1.128 ad struct namecache *ncp; 1350 1.128 ad int total, i; 1351 1.128 ad 1352 1.128 ad KASSERT(mutex_owned(&cache_lru_lock)); 1353 1.73 ad 1354 1.128 ad /* If we're nowhere near budget yet, don't bother. */ 1355 1.128 ad total = cache_lru.count[LRU_ACTIVE] + cache_lru.count[LRU_INACTIVE]; 1356 1.128 ad if (total < (desiredvnodes >> 1)) { 1357 1.158 riastrad return; 1358 1.128 ad } 1359 1.73 ad 1360 1.73 ad /* 1361 1.128 ad * Aim for a 1:1 ratio of active to inactive. This is to allow each 1362 1.128 ad * potential victim a reasonable amount of time to cycle through the 1363 1.128 ad * inactive list in order to score a hit and be reactivated, while 1364 1.128 ad * trying not to cause reactivations too frequently. 1365 1.73 ad */ 1366 1.128 ad if (cache_lru.count[LRU_ACTIVE] < cache_lru.count[LRU_INACTIVE]) { 1367 1.128 ad return; 1368 1.128 ad } 1369 1.73 ad 1370 1.128 ad /* Move only a few at a time; will catch up eventually. */ 1371 1.128 ad for (i = 0; i < cache_lru_maxdeact; i++) { 1372 1.128 ad ncp = TAILQ_FIRST(&cache_lru.list[LRU_ACTIVE]); 1373 1.128 ad if (ncp == NULL) { 1374 1.128 ad break; 1375 1.128 ad } 1376 1.128 ad KASSERT(ncp->nc_lrulist == LRU_ACTIVE); 1377 1.128 ad ncp->nc_lrulist = LRU_INACTIVE; 1378 1.128 ad TAILQ_REMOVE(&cache_lru.list[LRU_ACTIVE], ncp, nc_lru); 1379 1.128 ad TAILQ_INSERT_TAIL(&cache_lru.list[LRU_INACTIVE], ncp, nc_lru); 1380 1.128 ad cache_lru.count[LRU_ACTIVE]--; 1381 1.128 ad cache_lru.count[LRU_INACTIVE]++; 1382 1.128 ad } 1383 1.73 ad } 1384 1.73 ad 1385 1.73 ad /* 1386 1.128 ad * Free some entries from the cache, when we have gone over budget. 1387 1.128 ad * 1388 1.128 ad * We don't want to cause too much work for any individual caller, and it 1389 1.128 ad * doesn't matter if we temporarily go over budget. This is also "just a 1390 1.128 ad * cache" so it's not a big deal if we screw up and throw out something we 1391 1.128 ad * shouldn't. So we take a relaxed attitude to this process to reduce its 1392 1.128 ad * impact. 1393 1.73 ad */ 1394 1.73 ad static void 1395 1.128 ad cache_reclaim(void) 1396 1.28 chs { 1397 1.28 chs struct namecache *ncp; 1398 1.128 ad vnode_impl_t *dvi; 1399 1.128 ad int toscan; 1400 1.28 chs 1401 1.128 ad /* 1402 1.152 andvar * Scan up to a preset maximum number of entries, but no more than 1403 1.128 ad * 0.8% of the total at once (to allow for very small systems). 1404 1.128 ad * 1405 1.128 ad * On bigger systems, do a larger chunk of work to reduce the number 1406 1.128 ad * of times that cache_lru_lock is held for any length of time. 1407 1.128 ad */ 1408 1.128 ad mutex_enter(&cache_lru_lock); 1409 1.128 ad toscan = MIN(cache_lru_maxscan, desiredvnodes >> 7); 1410 1.128 ad toscan = MAX(toscan, 1); 1411 1.128 ad SDT_PROBE(vfs, namecache, prune, done, cache_lru.count[LRU_ACTIVE] + 1412 1.128 ad cache_lru.count[LRU_INACTIVE], toscan, 0, 0, 0); 1413 1.128 ad while (toscan-- != 0) { 1414 1.128 ad /* First try to balance the lists. */ 1415 1.128 ad cache_deactivate(); 1416 1.128 ad 1417 1.128 ad /* Now look for a victim on head of inactive list (old). */ 1418 1.128 ad ncp = TAILQ_FIRST(&cache_lru.list[LRU_INACTIVE]); 1419 1.128 ad if (ncp == NULL) { 1420 1.128 ad break; 1421 1.28 chs } 1422 1.128 ad dvi = VNODE_TO_VIMPL(ncp->nc_dvp); 1423 1.128 ad KASSERT(ncp->nc_lrulist == LRU_INACTIVE); 1424 1.128 ad KASSERT(dvi != NULL); 1425 1.128 ad 1426 1.128 ad /* 1427 1.128 ad * Locking in the wrong direction. If we can't get the 1428 1.128 ad * lock, the directory is actively busy, and it could also 1429 1.128 ad * cause problems for the next guy in here, so send the 1430 1.128 ad * entry to the back of the list. 1431 1.128 ad */ 1432 1.128 ad if (!rw_tryenter(&dvi->vi_nc_lock, RW_WRITER)) { 1433 1.128 ad TAILQ_REMOVE(&cache_lru.list[LRU_INACTIVE], 1434 1.128 ad ncp, nc_lru); 1435 1.128 ad TAILQ_INSERT_TAIL(&cache_lru.list[LRU_INACTIVE], 1436 1.128 ad ncp, nc_lru); 1437 1.128 ad continue; 1438 1.28 chs } 1439 1.128 ad 1440 1.128 ad /* 1441 1.128 ad * Now have the victim entry locked. Drop the LRU list 1442 1.128 ad * lock, purge the entry, and start over. The hold on 1443 1.128 ad * vi_nc_lock will prevent the vnode from vanishing until 1444 1.128 ad * finished (cache_purge() will be called on dvp before it 1445 1.128 ad * disappears, and that will wait on vi_nc_lock). 1446 1.128 ad */ 1447 1.128 ad mutex_exit(&cache_lru_lock); 1448 1.128 ad cache_remove(ncp, true); 1449 1.128 ad rw_exit(&dvi->vi_nc_lock); 1450 1.128 ad mutex_enter(&cache_lru_lock); 1451 1.28 chs } 1452 1.128 ad mutex_exit(&cache_lru_lock); 1453 1.28 chs } 1454 1.95 joerg 1455 1.128 ad /* 1456 1.128 ad * For file system code: count a lookup that required a full re-scan of 1457 1.128 ad * directory metadata. 1458 1.128 ad */ 1459 1.95 joerg void 1460 1.95 joerg namecache_count_pass2(void) 1461 1.95 joerg { 1462 1.95 joerg 1463 1.128 ad COUNT(ncs_pass2); 1464 1.95 joerg } 1465 1.95 joerg 1466 1.128 ad /* 1467 1.128 ad * For file system code: count a lookup that scored a hit in the directory 1468 1.128 ad * metadata near the location of the last lookup. 1469 1.128 ad */ 1470 1.95 joerg void 1471 1.95 joerg namecache_count_2passes(void) 1472 1.95 joerg { 1473 1.95 joerg 1474 1.128 ad COUNT(ncs_2passes); 1475 1.128 ad } 1476 1.128 ad 1477 1.128 ad /* 1478 1.128 ad * Sum the stats from all CPUs into nchstats. This needs to run at least 1479 1.128 ad * once within every window where a 32-bit counter could roll over. It's 1480 1.128 ad * called regularly by timer to ensure this. 1481 1.128 ad */ 1482 1.128 ad static void 1483 1.128 ad cache_update_stats(void *cookie) 1484 1.128 ad { 1485 1.128 ad CPU_INFO_ITERATOR cii; 1486 1.128 ad struct cpu_info *ci; 1487 1.128 ad 1488 1.128 ad mutex_enter(&cache_stat_lock); 1489 1.128 ad for (CPU_INFO_FOREACH(cii, ci)) { 1490 1.128 ad struct nchcpu *nchcpu = ci->ci_data.cpu_nch; 1491 1.128 ad UPDATE(nchcpu, ncs_goodhits); 1492 1.128 ad UPDATE(nchcpu, ncs_neghits); 1493 1.128 ad UPDATE(nchcpu, ncs_badhits); 1494 1.128 ad UPDATE(nchcpu, ncs_falsehits); 1495 1.128 ad UPDATE(nchcpu, ncs_miss); 1496 1.128 ad UPDATE(nchcpu, ncs_long); 1497 1.128 ad UPDATE(nchcpu, ncs_pass2); 1498 1.128 ad UPDATE(nchcpu, ncs_2passes); 1499 1.128 ad UPDATE(nchcpu, ncs_revhits); 1500 1.128 ad UPDATE(nchcpu, ncs_revmiss); 1501 1.128 ad UPDATE(nchcpu, ncs_denied); 1502 1.128 ad } 1503 1.128 ad if (cookie != NULL) { 1504 1.128 ad memcpy(cookie, &nchstats, sizeof(nchstats)); 1505 1.128 ad } 1506 1.128 ad /* Reset the timer; arrive back here in N minutes at latest. */ 1507 1.128 ad callout_schedule(&cache_stat_callout, cache_stat_interval * hz); 1508 1.128 ad mutex_exit(&cache_stat_lock); 1509 1.95 joerg } 1510 1.97 joerg 1511 1.103 dennis /* 1512 1.131 ad * Fetch the current values of the stats for sysctl. 1513 1.103 dennis */ 1514 1.97 joerg static int 1515 1.97 joerg cache_stat_sysctl(SYSCTLFN_ARGS) 1516 1.97 joerg { 1517 1.125 ad struct nchstats stats; 1518 1.97 joerg 1519 1.97 joerg if (oldp == NULL) { 1520 1.128 ad *oldlenp = sizeof(nchstats); 1521 1.97 joerg return 0; 1522 1.97 joerg } 1523 1.97 joerg 1524 1.128 ad if (*oldlenp <= 0) { 1525 1.97 joerg *oldlenp = 0; 1526 1.97 joerg return 0; 1527 1.97 joerg } 1528 1.97 joerg 1529 1.128 ad /* Refresh the global stats. */ 1530 1.103 dennis sysctl_unlock(); 1531 1.128 ad cache_update_stats(&stats); 1532 1.97 joerg sysctl_relock(); 1533 1.97 joerg 1534 1.128 ad *oldlenp = MIN(sizeof(stats), *oldlenp); 1535 1.128 ad return sysctl_copyout(l, &stats, oldp, *oldlenp); 1536 1.97 joerg } 1537 1.97 joerg 1538 1.128 ad /* 1539 1.128 ad * For the debugger, given the address of a vnode, print all associated 1540 1.128 ad * names in the cache. 1541 1.128 ad */ 1542 1.128 ad #ifdef DDB 1543 1.128 ad void 1544 1.128 ad namecache_print(struct vnode *vp, void (*pr)(const char *, ...)) 1545 1.97 joerg { 1546 1.128 ad struct vnode *dvp = NULL; 1547 1.128 ad struct namecache *ncp; 1548 1.128 ad enum cache_lru_id id; 1549 1.104 pooka 1550 1.128 ad for (id = 0; id < LRU_COUNT; id++) { 1551 1.128 ad TAILQ_FOREACH(ncp, &cache_lru.list[id], nc_lru) { 1552 1.128 ad if (ncp->nc_vp == vp) { 1553 1.155 ad (*pr)("name %.*s\n", NC_NLEN(ncp), 1554 1.128 ad ncp->nc_name); 1555 1.128 ad dvp = ncp->nc_dvp; 1556 1.128 ad } 1557 1.128 ad } 1558 1.128 ad } 1559 1.128 ad if (dvp == NULL) { 1560 1.128 ad (*pr)("name not found\n"); 1561 1.128 ad return; 1562 1.128 ad } 1563 1.128 ad for (id = 0; id < LRU_COUNT; id++) { 1564 1.128 ad TAILQ_FOREACH(ncp, &cache_lru.list[id], nc_lru) { 1565 1.128 ad if (ncp->nc_vp == dvp) { 1566 1.155 ad (*pr)("parent %.*s\n", NC_NLEN(ncp), 1567 1.128 ad ncp->nc_name); 1568 1.128 ad } 1569 1.128 ad } 1570 1.128 ad } 1571 1.97 joerg } 1572 1.128 ad #endif 1573