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