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