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