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