2 * Copyright (c) 1989, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Poul-Henning Kamp of the FreeBSD Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)vfs_cache.c 8.5 (Berkeley) 3/22/95
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include "opt_ktrace.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/counter.h>
43 #include <sys/filedesc.h>
44 #include <sys/fnv_hash.h>
45 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/fcntl.h>
49 #include <sys/mount.h>
50 #include <sys/namei.h>
52 #include <sys/rwlock.h>
55 #include <sys/syscallsubr.h>
56 #include <sys/sysctl.h>
57 #include <sys/sysproto.h>
58 #include <sys/vnode.h>
60 #include <sys/ktrace.h>
65 SDT_PROVIDER_DECLARE(vfs);
66 SDT_PROBE_DEFINE3(vfs, namecache, enter, done, "struct vnode *", "char *",
68 SDT_PROBE_DEFINE2(vfs, namecache, enter_negative, done, "struct vnode *",
70 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, entry, "struct vnode *");
71 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, hit, "struct vnode *",
72 "char *", "struct vnode *");
73 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, miss, "struct vnode *");
74 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, return, "int",
75 "struct vnode *", "char *");
76 SDT_PROBE_DEFINE3(vfs, namecache, lookup, hit, "struct vnode *", "char *",
78 SDT_PROBE_DEFINE2(vfs, namecache, lookup, hit__negative,
79 "struct vnode *", "char *");
80 SDT_PROBE_DEFINE2(vfs, namecache, lookup, miss, "struct vnode *",
82 SDT_PROBE_DEFINE1(vfs, namecache, purge, done, "struct vnode *");
83 SDT_PROBE_DEFINE1(vfs, namecache, purge_negative, done, "struct vnode *");
84 SDT_PROBE_DEFINE1(vfs, namecache, purgevfs, done, "struct mount *");
85 SDT_PROBE_DEFINE3(vfs, namecache, zap, done, "struct vnode *", "char *",
87 SDT_PROBE_DEFINE2(vfs, namecache, zap_negative, done, "struct vnode *",
91 * This structure describes the elements in the cache of recent
92 * names looked up by namei.
96 LIST_ENTRY(namecache) nc_hash; /* hash chain */
97 LIST_ENTRY(namecache) nc_src; /* source vnode list */
98 TAILQ_ENTRY(namecache) nc_dst; /* destination vnode list */
99 struct vnode *nc_dvp; /* vnode of parent of name */
100 struct vnode *nc_vp; /* vnode the name refers to */
101 u_char nc_flag; /* flag bits */
102 u_char nc_nlen; /* length of name */
103 char nc_name[0]; /* segment name + nul */
107 * struct namecache_ts repeats struct namecache layout up to the
109 * struct namecache_ts is used in place of struct namecache when time(s) need
110 * to be stored. The nc_dotdottime field is used when a cache entry is mapping
111 * both a non-dotdot directory name plus dotdot for the directory's
114 struct namecache_ts {
115 LIST_ENTRY(namecache) nc_hash; /* hash chain */
116 LIST_ENTRY(namecache) nc_src; /* source vnode list */
117 TAILQ_ENTRY(namecache) nc_dst; /* destination vnode list */
118 struct vnode *nc_dvp; /* vnode of parent of name */
119 struct vnode *nc_vp; /* vnode the name refers to */
120 u_char nc_flag; /* flag bits */
121 u_char nc_nlen; /* length of name */
122 struct timespec nc_time; /* timespec provided by fs */
123 struct timespec nc_dotdottime; /* dotdot timespec provided by fs */
124 int nc_ticks; /* ticks value when entry was added */
125 char nc_name[0]; /* segment name + nul */
129 * Flags in namecache.nc_flag
131 #define NCF_WHITE 0x01
132 #define NCF_ISDOTDOT 0x02
135 #define NCF_DVDROP 0x10
138 * Name caching works as follows:
140 * Names found by directory scans are retained in a cache
141 * for future reference. It is managed LRU, so frequently
142 * used names will hang around. Cache is indexed by hash value
143 * obtained from (vp, name) where vp refers to the directory
146 * If it is a "negative" entry, (i.e. for a name that is known NOT to
147 * exist) the vnode pointer will be NULL.
149 * Upon reaching the last segment of a path, if the reference
150 * is for DELETE, or NOCACHE is set (rewrite), and the
151 * name is located in the cache, it will be dropped.
153 * These locks are used (in the order in which they can be taken):
155 * vnodelock mtx vnode lists and v_cache_dd field protection
156 * bucketlock rwlock for access to given set of hash buckets
157 * ncneg_mtx mtx negative entry LRU management
159 * Additionally, ncneg_shrink_lock mtx is used to have at most one thread
160 * shrinking the LRU list.
162 * It is legal to take multiple vnodelock and bucketlock locks. The locking
163 * order is lower address first. Both are recursive.
165 * "." lookups are lockless.
167 * ".." and vnode -> name lookups require vnodelock.
169 * name -> vnode lookup requires the relevant bucketlock to be held for reading.
171 * Insertions and removals of entries require involved vnodes and bucketlocks
172 * to be write-locked to prevent other threads from seeing the entry.
174 * Some lookups result in removal of the found entry (e.g. getting rid of a
175 * negative entry with the intent to create a positive one), which poses a
176 * problem when multiple threads reach the state. Similarly, two different
177 * threads can purge two different vnodes and try to remove the same name.
179 * If the already held vnode lock is lower than the second required lock, we
180 * can just take the other lock. However, in the opposite case, this could
181 * deadlock. As such, this is resolved by trylocking and if that fails unlocking
182 * the first node, locking everything in order and revalidating the state.
186 * Structures associated with name caching.
188 #define NCHHASH(hash) \
189 (&nchashtbl[(hash) & nchash])
190 static LIST_HEAD(nchashhead, namecache) *nchashtbl; /* Hash Table */
191 static TAILQ_HEAD(, namecache) ncneg; /* Hash Table */
192 static u_long nchash; /* size of hash table */
193 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0,
194 "Size of namecache hash table");
195 static u_long ncnegfactor = 16; /* ratio of negative entries */
196 SYSCTL_ULONG(_vfs, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0,
197 "Ratio of negative namecache entries");
198 static u_long numneg; /* number of negative entries allocated */
199 SYSCTL_ULONG(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0,
200 "Number of negative entries in namecache");
201 static u_long numcache; /* number of cache entries allocated */
202 SYSCTL_ULONG(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0,
203 "Number of namecache entries");
204 static u_long numcachehv; /* number of cache entries with vnodes held */
205 SYSCTL_ULONG(_debug, OID_AUTO, numcachehv, CTLFLAG_RD, &numcachehv, 0,
206 "Number of namecache entries with vnodes held");
207 u_int ncsizefactor = 2;
208 SYSCTL_UINT(_vfs, OID_AUTO, ncsizefactor, CTLFLAG_RW, &ncsizefactor, 0,
209 "Size factor for namecache");
211 struct nchstats nchstats; /* cache effectiveness statistics */
213 static struct mtx ncneg_shrink_lock;
214 MTX_SYSINIT(vfscache_shrink_neg, &ncneg_shrink_lock, "Name Cache shrink neg",
217 static struct mtx_padalign ncneg_mtx;
218 MTX_SYSINIT(vfscache_neg, &ncneg_mtx, "ncneg", MTX_DEF);
220 static u_int numbucketlocks;
221 static struct rwlock_padalign *bucketlocks;
222 #define HASH2BUCKETLOCK(hash) \
223 ((struct rwlock *)(&bucketlocks[((hash) % numbucketlocks)]))
225 static u_int numvnodelocks;
226 static struct mtx *vnodelocks;
227 static inline struct mtx *
228 VP2VNODELOCK(struct vnode *vp)
234 vlp = &vnodelocks[(((uintptr_t)(vp) >> 8) % numvnodelocks)];
239 * UMA zones for the VFS cache.
241 * The small cache is used for entries with short names, which are the
242 * most common. The large cache is used for entries which are too big to
243 * fit in the small cache.
245 static uma_zone_t cache_zone_small;
246 static uma_zone_t cache_zone_small_ts;
247 static uma_zone_t cache_zone_large;
248 static uma_zone_t cache_zone_large_ts;
250 #define CACHE_PATH_CUTOFF 35
252 static struct namecache *
253 cache_alloc(int len, int ts)
256 if (len > CACHE_PATH_CUTOFF) {
258 return (uma_zalloc(cache_zone_large_ts, M_WAITOK));
260 return (uma_zalloc(cache_zone_large, M_WAITOK));
263 return (uma_zalloc(cache_zone_small_ts, M_WAITOK));
265 return (uma_zalloc(cache_zone_small, M_WAITOK));
269 cache_free(struct namecache *ncp)
275 ts = ncp->nc_flag & NCF_TS;
276 if ((ncp->nc_flag & NCF_DVDROP) != 0)
278 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF) {
280 uma_zfree(cache_zone_small_ts, ncp);
282 uma_zfree(cache_zone_small, ncp);
284 uma_zfree(cache_zone_large_ts, ncp);
286 uma_zfree(cache_zone_large, ncp);
290 nc_get_name(struct namecache *ncp)
292 struct namecache_ts *ncp_ts;
294 if ((ncp->nc_flag & NCF_TS) == 0)
295 return (ncp->nc_name);
296 ncp_ts = (struct namecache_ts *)ncp;
297 return (ncp_ts->nc_name);
301 cache_out_ts(struct namecache *ncp, struct timespec *tsp, int *ticksp)
304 KASSERT((ncp->nc_flag & NCF_TS) != 0 ||
305 (tsp == NULL && ticksp == NULL),
309 *tsp = ((struct namecache_ts *)ncp)->nc_time;
311 *ticksp = ((struct namecache_ts *)ncp)->nc_ticks;
314 static int doingcache = 1; /* 1 => enable the cache */
315 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0,
316 "VFS namecache enabled");
318 /* Export size information to userland */
319 SYSCTL_INT(_debug_sizeof, OID_AUTO, namecache, CTLFLAG_RD, SYSCTL_NULL_INT_PTR,
320 sizeof(struct namecache), "sizeof(struct namecache)");
323 * The new name cache statistics
325 static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0,
326 "Name cache statistics");
327 #define STATNODE_ULONG(name, descr) \
328 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, descr);
329 #define STATNODE_COUNTER(name, descr) \
330 static counter_u64_t name; \
331 SYSCTL_COUNTER_U64(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, descr);
332 STATNODE_ULONG(numneg, "Number of negative cache entries");
333 STATNODE_ULONG(numcache, "Number of cache entries");
334 STATNODE_COUNTER(numcalls, "Number of cache lookups");
335 STATNODE_COUNTER(dothits, "Number of '.' hits");
336 STATNODE_COUNTER(dotdothits, "Number of '..' hits");
337 STATNODE_COUNTER(numchecks, "Number of checks in lookup");
338 STATNODE_COUNTER(nummiss, "Number of cache misses");
339 STATNODE_COUNTER(nummisszap, "Number of cache misses we do not want to cache");
340 STATNODE_COUNTER(numposzaps,
341 "Number of cache hits (positive) we do not want to cache");
342 STATNODE_COUNTER(numposhits, "Number of cache hits (positive)");
343 STATNODE_COUNTER(numnegzaps,
344 "Number of cache hits (negative) we do not want to cache");
345 STATNODE_COUNTER(numneghits, "Number of cache hits (negative)");
346 /* These count for kern___getcwd(), too. */
347 STATNODE_COUNTER(numfullpathcalls, "Number of fullpath search calls");
348 STATNODE_COUNTER(numfullpathfail1, "Number of fullpath search errors (ENOTDIR)");
349 STATNODE_COUNTER(numfullpathfail2,
350 "Number of fullpath search errors (VOP_VPTOCNP failures)");
351 STATNODE_COUNTER(numfullpathfail4, "Number of fullpath search errors (ENOMEM)");
352 STATNODE_COUNTER(numfullpathfound, "Number of successful fullpath calls");
353 static long zap_and_exit_bucket_fail; STATNODE_ULONG(zap_and_exit_bucket_fail,
354 "Number of times zap_and_exit failed to lock");
355 static long cache_lock_vnodes_cel_3_failures;
356 STATNODE_ULONG(cache_lock_vnodes_cel_3_failures,
357 "Number of times 3-way vnode locking failed");
359 static void cache_zap_locked(struct namecache *ncp, bool neg_locked);
360 static int vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
361 char *buf, char **retbuf, u_int buflen);
363 static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
365 static int cache_yield;
366 SYSCTL_INT(_vfs_cache, OID_AUTO, yield, CTLFLAG_RD, &cache_yield, 0,
367 "Number of times cache called yield");
370 cache_maybe_yield(void)
373 if (should_yield()) {
375 kern_yield(PRI_USER);
380 cache_assert_vlp_locked(struct mtx *vlp)
384 mtx_assert(vlp, MA_OWNED);
388 cache_assert_vnode_locked(struct vnode *vp)
392 vlp = VP2VNODELOCK(vp);
393 cache_assert_vlp_locked(vlp);
397 cache_get_hash(char *name, u_char len, struct vnode *dvp)
401 hash = fnv_32_buf(name, len, FNV1_32_INIT);
402 hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
406 static inline struct rwlock *
407 NCP2BUCKETLOCK(struct namecache *ncp)
411 hash = cache_get_hash(nc_get_name(ncp), ncp->nc_nlen, ncp->nc_dvp);
412 return (HASH2BUCKETLOCK(hash));
417 cache_assert_bucket_locked(struct namecache *ncp, int mode)
421 blp = NCP2BUCKETLOCK(ncp);
422 rw_assert(blp, mode);
425 #define cache_assert_bucket_locked(x, y) do { } while (0)
428 #define cache_sort(x, y) _cache_sort((void **)(x), (void **)(y))
430 _cache_sort(void **p1, void **p2)
442 cache_lock_all_buckets(void)
446 for (i = 0; i < numbucketlocks; i++)
447 rw_wlock(&bucketlocks[i]);
451 cache_unlock_all_buckets(void)
455 for (i = 0; i < numbucketlocks; i++)
456 rw_wunlock(&bucketlocks[i]);
460 cache_lock_all_vnodes(void)
464 for (i = 0; i < numvnodelocks; i++)
465 mtx_lock(&vnodelocks[i]);
469 cache_unlock_all_vnodes(void)
473 for (i = 0; i < numvnodelocks; i++)
474 mtx_unlock(&vnodelocks[i]);
478 cache_trylock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
481 cache_sort(&vlp1, &vlp2);
485 if (!mtx_trylock(vlp1))
488 if (!mtx_trylock(vlp2)) {
498 cache_unlock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
501 MPASS(vlp1 != NULL || vlp2 != NULL);
510 sysctl_nchstats(SYSCTL_HANDLER_ARGS)
512 struct nchstats snap;
514 if (req->oldptr == NULL)
515 return (SYSCTL_OUT(req, 0, sizeof(snap)));
518 snap.ncs_goodhits = counter_u64_fetch(numposhits);
519 snap.ncs_neghits = counter_u64_fetch(numneghits);
520 snap.ncs_badhits = counter_u64_fetch(numposzaps) +
521 counter_u64_fetch(numnegzaps);
522 snap.ncs_miss = counter_u64_fetch(nummisszap) +
523 counter_u64_fetch(nummiss);
525 return (SYSCTL_OUT(req, &snap, sizeof(snap)));
527 SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE | CTLFLAG_RD |
528 CTLFLAG_MPSAFE, 0, 0, sysctl_nchstats, "LU",
529 "VFS cache effectiveness statistics");
533 * Grab an atomic snapshot of the name cache hash chain lengths
535 static SYSCTL_NODE(_debug, OID_AUTO, hashstat, CTLFLAG_RW, NULL,
539 sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS)
541 struct nchashhead *ncpp;
542 struct namecache *ncp;
543 int i, error, n_nchash, *cntbuf;
546 n_nchash = nchash + 1; /* nchash is max index, not count */
547 if (req->oldptr == NULL)
548 return SYSCTL_OUT(req, 0, n_nchash * sizeof(int));
549 cntbuf = malloc(n_nchash * sizeof(int), M_TEMP, M_ZERO | M_WAITOK);
550 cache_lock_all_buckets();
551 if (n_nchash != nchash + 1) {
552 cache_unlock_all_buckets();
553 free(cntbuf, M_TEMP);
556 /* Scan hash tables counting entries */
557 for (ncpp = nchashtbl, i = 0; i < n_nchash; ncpp++, i++)
558 LIST_FOREACH(ncp, ncpp, nc_hash)
560 cache_unlock_all_buckets();
561 for (error = 0, i = 0; i < n_nchash; i++)
562 if ((error = SYSCTL_OUT(req, &cntbuf[i], sizeof(int))) != 0)
564 free(cntbuf, M_TEMP);
567 SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD|
568 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_rawnchash, "S,int",
569 "nchash chain lengths");
572 sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS)
575 struct nchashhead *ncpp;
576 struct namecache *ncp;
578 int count, maxlength, used, pct;
581 return SYSCTL_OUT(req, 0, 4 * sizeof(int));
583 cache_lock_all_buckets();
584 n_nchash = nchash + 1; /* nchash is max index, not count */
588 /* Scan hash tables for applicable entries */
589 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
591 LIST_FOREACH(ncp, ncpp, nc_hash) {
596 if (maxlength < count)
599 n_nchash = nchash + 1;
600 cache_unlock_all_buckets();
601 pct = (used * 100) / (n_nchash / 100);
602 error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash));
605 error = SYSCTL_OUT(req, &used, sizeof(used));
608 error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength));
611 error = SYSCTL_OUT(req, &pct, sizeof(pct));
616 SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD|
617 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_nchash, "I",
618 "nchash statistics (number of total/used buckets, maximum chain length, usage percentage)");
622 * Negative entries management
625 cache_negative_hit(struct namecache *ncp)
628 MPASS(ncp->nc_vp == NULL);
629 mtx_lock(&ncneg_mtx);
630 TAILQ_REMOVE(&ncneg, ncp, nc_dst);
631 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
632 mtx_unlock(&ncneg_mtx);
636 cache_negative_insert(struct namecache *ncp)
639 MPASS(ncp->nc_vp == NULL);
640 cache_assert_bucket_locked(ncp, RA_WLOCKED);
641 mtx_lock(&ncneg_mtx);
642 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
644 mtx_unlock(&ncneg_mtx);
648 cache_negative_remove(struct namecache *ncp, bool neg_locked)
651 MPASS(ncp->nc_vp == NULL);
652 cache_assert_bucket_locked(ncp, RA_WLOCKED);
654 mtx_lock(&ncneg_mtx);
656 mtx_assert(&ncneg_mtx, MA_OWNED);
657 TAILQ_REMOVE(&ncneg, ncp, nc_dst);
660 mtx_unlock(&ncneg_mtx);
664 cache_negative_zap_one(void)
666 struct namecache *ncp, *ncp2;
670 if (!mtx_trylock(&ncneg_shrink_lock))
673 mtx_lock(&ncneg_mtx);
674 ncp = TAILQ_FIRST(&ncneg);
676 mtx_unlock(&ncneg_mtx);
679 MPASS(ncp->nc_vp == NULL);
680 dvlp = VP2VNODELOCK(ncp->nc_dvp);
681 blp = NCP2BUCKETLOCK(ncp);
682 mtx_unlock(&ncneg_mtx);
685 mtx_lock(&ncneg_mtx);
686 ncp2 = TAILQ_FIRST(&ncneg);
687 if (ncp != ncp2 || dvlp != VP2VNODELOCK(ncp2->nc_dvp) ||
688 blp != NCP2BUCKETLOCK(ncp2) || ncp2->nc_vp != NULL) {
692 cache_zap_locked(ncp, true);
694 mtx_unlock(&ncneg_mtx);
698 mtx_unlock(&ncneg_shrink_lock);
703 * cache_zap_locked():
705 * Removes a namecache entry from cache, whether it contains an actual
706 * pointer to a vnode or if it is just a negative cache entry.
709 cache_zap_locked(struct namecache *ncp, bool neg_locked)
712 cache_assert_vnode_locked(ncp->nc_vp);
713 cache_assert_vnode_locked(ncp->nc_dvp);
714 cache_assert_bucket_locked(ncp, RA_WLOCKED);
716 CTR2(KTR_VFS, "cache_zap(%p) vp %p", ncp, ncp->nc_vp);
717 if (ncp->nc_vp != NULL) {
718 SDT_PROBE3(vfs, namecache, zap, done, ncp->nc_dvp,
719 nc_get_name(ncp), ncp->nc_vp);
721 SDT_PROBE2(vfs, namecache, zap_negative, done, ncp->nc_dvp,
724 LIST_REMOVE(ncp, nc_hash);
725 if (ncp->nc_flag & NCF_ISDOTDOT) {
726 if (ncp == ncp->nc_dvp->v_cache_dd)
727 ncp->nc_dvp->v_cache_dd = NULL;
729 LIST_REMOVE(ncp, nc_src);
730 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) {
731 ncp->nc_flag |= NCF_DVDROP;
732 atomic_subtract_rel_long(&numcachehv, 1);
736 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
737 if (ncp == ncp->nc_vp->v_cache_dd)
738 ncp->nc_vp->v_cache_dd = NULL;
740 cache_negative_remove(ncp, neg_locked);
742 atomic_subtract_rel_long(&numcache, 1);
746 cache_zap_negative_locked_vnode_kl(struct namecache *ncp, struct vnode *vp)
750 MPASS(ncp->nc_dvp == vp);
751 MPASS(ncp->nc_vp == NULL);
752 cache_assert_vnode_locked(vp);
754 blp = NCP2BUCKETLOCK(ncp);
756 cache_zap_locked(ncp, false);
761 cache_zap_locked_vnode_kl2(struct namecache *ncp, struct vnode *vp,
764 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
767 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
768 cache_assert_vnode_locked(vp);
770 if (ncp->nc_vp == NULL) {
775 cache_zap_negative_locked_vnode_kl(ncp, vp);
779 pvlp = VP2VNODELOCK(vp);
780 blp = NCP2BUCKETLOCK(ncp);
781 vlp1 = VP2VNODELOCK(ncp->nc_dvp);
782 vlp2 = VP2VNODELOCK(ncp->nc_vp);
784 if (*vlpp == vlp1 || *vlpp == vlp2) {
792 cache_sort(&vlp1, &vlp2);
797 if (!mtx_trylock(vlp1))
803 cache_zap_locked(ncp, false);
805 if (to_unlock != NULL)
806 mtx_unlock(to_unlock);
813 MPASS(*vlpp == NULL);
819 cache_zap_locked_vnode(struct namecache *ncp, struct vnode *vp)
821 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
825 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
826 cache_assert_vnode_locked(vp);
828 pvlp = VP2VNODELOCK(vp);
829 if (ncp->nc_vp == NULL) {
830 cache_zap_negative_locked_vnode_kl(ncp, vp);
834 blp = NCP2BUCKETLOCK(ncp);
835 vlp1 = VP2VNODELOCK(ncp->nc_dvp);
836 vlp2 = VP2VNODELOCK(ncp->nc_vp);
837 cache_sort(&vlp1, &vlp2);
842 if (!mtx_trylock(vlp1)) {
849 cache_zap_locked(ncp, false);
851 mtx_unlock(to_unlock);
858 cache_zap_rlocked_bucket(struct namecache *ncp, struct rwlock *blp)
860 struct mtx *dvlp, *vlp;
862 cache_assert_bucket_locked(ncp, RA_RLOCKED);
864 dvlp = VP2VNODELOCK(ncp->nc_dvp);
865 vlp = VP2VNODELOCK(ncp->nc_vp);
866 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
869 cache_zap_locked(ncp, false);
871 cache_unlock_vnodes(dvlp, vlp);
880 cache_zap_wlocked_bucket_kl(struct namecache *ncp, struct rwlock *blp,
881 struct mtx **vlpp1, struct mtx **vlpp2)
883 struct mtx *dvlp, *vlp;
885 cache_assert_bucket_locked(ncp, RA_WLOCKED);
887 dvlp = VP2VNODELOCK(ncp->nc_dvp);
888 vlp = VP2VNODELOCK(ncp->nc_vp);
889 cache_sort(&dvlp, &vlp);
891 if (*vlpp1 == dvlp && *vlpp2 == vlp) {
892 cache_zap_locked(ncp, false);
893 cache_unlock_vnodes(dvlp, vlp);
906 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
907 cache_zap_locked(ncp, false);
908 cache_unlock_vnodes(dvlp, vlp);
923 cache_lookup_unlock(struct rwlock *blp, struct mtx *vlp)
928 mtx_assert(vlp, MA_NOTOWNED);
935 * Lookup an entry in the cache
937 * Lookup is called with dvp pointing to the directory to search,
938 * cnp pointing to the name of the entry being sought. If the lookup
939 * succeeds, the vnode is returned in *vpp, and a status of -1 is
940 * returned. If the lookup determines that the name does not exist
941 * (negative caching), a status of ENOENT is returned. If the lookup
942 * fails, a status of zero is returned. If the directory vnode is
943 * recycled out from under us due to a forced unmount, a status of
944 * ENOENT is returned.
946 * vpp is locked and ref'd on return. If we're looking up DOTDOT, dvp is
947 * unlocked. If we're looking up . an extra ref is taken, but the lock is
948 * not recursively acquired.
952 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
953 struct timespec *tsp, int *ticksp)
955 struct namecache *ncp;
957 struct mtx *dvlp, *dvlp2;
962 cnp->cn_flags &= ~MAKEENTRY;
967 dvlp = VP2VNODELOCK(dvp);
969 counter_u64_add(numcalls, 1);
971 if (cnp->cn_nameptr[0] == '.') {
972 if (cnp->cn_namelen == 1) {
974 CTR2(KTR_VFS, "cache_lookup(%p, %s) found via .",
975 dvp, cnp->cn_nameptr);
976 counter_u64_add(dothits, 1);
977 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", *vpp);
984 * When we lookup "." we still can be asked to lock it
987 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
988 if (ltype != VOP_ISLOCKED(*vpp)) {
989 if (ltype == LK_EXCLUSIVE) {
990 vn_lock(*vpp, LK_UPGRADE | LK_RETRY);
991 if ((*vpp)->v_iflag & VI_DOOMED) {
998 vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY);
1002 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
1003 counter_u64_add(dotdothits, 1);
1007 ncp = dvp->v_cache_dd;
1009 SDT_PROBE3(vfs, namecache, lookup, miss, dvp,
1014 if ((cnp->cn_flags & MAKEENTRY) == 0) {
1015 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1016 if (ncp->nc_dvp != dvp)
1017 panic("dvp %p v_cache_dd %p\n", dvp, ncp);
1018 if (!cache_zap_locked_vnode_kl2(ncp,
1021 MPASS(dvp->v_cache_dd == NULL);
1027 dvp->v_cache_dd = NULL;
1034 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0)
1038 /* Return failure if negative entry was found. */
1040 goto negative_success;
1041 CTR3(KTR_VFS, "cache_lookup(%p, %s) found %p via ..",
1042 dvp, cnp->cn_nameptr, *vpp);
1043 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, "..",
1045 cache_out_ts(ncp, tsp, ticksp);
1046 if ((ncp->nc_flag & (NCF_ISDOTDOT | NCF_DTS)) ==
1047 NCF_DTS && tsp != NULL)
1048 *tsp = ((struct namecache_ts *)ncp)->
1054 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1055 blp = HASH2BUCKETLOCK(hash);
1058 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1059 counter_u64_add(numchecks, 1);
1060 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1061 !bcmp(nc_get_name(ncp), cnp->cn_nameptr, ncp->nc_nlen))
1065 /* We failed to find an entry */
1067 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
1069 if ((cnp->cn_flags & MAKEENTRY) == 0) {
1070 counter_u64_add(nummisszap, 1);
1072 counter_u64_add(nummiss, 1);
1077 /* We don't want to have an entry, so dump it */
1078 if ((cnp->cn_flags & MAKEENTRY) == 0) {
1079 counter_u64_add(numposzaps, 1);
1083 /* We found a "positive" match, return the vnode */
1085 counter_u64_add(numposhits, 1);
1087 CTR4(KTR_VFS, "cache_lookup(%p, %s) found %p via ncp %p",
1088 dvp, cnp->cn_nameptr, *vpp, ncp);
1089 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, nc_get_name(ncp),
1091 cache_out_ts(ncp, tsp, ticksp);
1096 /* We found a negative match, and want to create it, so purge */
1097 if (cnp->cn_nameiop == CREATE) {
1098 counter_u64_add(numnegzaps, 1);
1102 counter_u64_add(numneghits, 1);
1103 cache_negative_hit(ncp);
1104 if (ncp->nc_flag & NCF_WHITE)
1105 cnp->cn_flags |= ISWHITEOUT;
1106 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp,
1108 cache_out_ts(ncp, tsp, ticksp);
1109 cache_lookup_unlock(blp, dvlp);
1114 * On success we return a locked and ref'd vnode as per the lookup
1118 ltype = 0; /* silence gcc warning */
1119 if (cnp->cn_flags & ISDOTDOT) {
1120 ltype = VOP_ISLOCKED(dvp);
1124 cache_lookup_unlock(blp, dvlp);
1125 error = vget(*vpp, cnp->cn_lkflags | LK_VNHELD, cnp->cn_thread);
1126 if (cnp->cn_flags & ISDOTDOT) {
1127 vn_lock(dvp, ltype | LK_RETRY);
1128 if (dvp->v_iflag & VI_DOOMED) {
1139 if ((cnp->cn_flags & ISLASTCN) &&
1140 (cnp->cn_lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE) {
1141 ASSERT_VOP_ELOCKED(*vpp, "cache_lookup");
1146 cache_lookup_unlock(blp, dvlp);
1151 error = cache_zap_rlocked_bucket(ncp, blp);
1153 error = cache_zap_locked_vnode(ncp, dvp);
1155 zap_and_exit_bucket_fail++;
1156 cache_maybe_yield();
1163 struct celockstate {
1165 struct rwlock *blp[2];
1167 CTASSERT((nitems(((struct celockstate *)0)->vlp) == 3));
1168 CTASSERT((nitems(((struct celockstate *)0)->blp) == 2));
1171 cache_celockstate_init(struct celockstate *cel)
1174 bzero(cel, sizeof(*cel));
1178 cache_lock_vnodes_cel(struct celockstate *cel, struct vnode *vp,
1181 struct mtx *vlp1, *vlp2;
1183 MPASS(cel->vlp[0] == NULL);
1184 MPASS(cel->vlp[1] == NULL);
1185 MPASS(cel->vlp[2] == NULL);
1187 MPASS(vp != NULL || dvp != NULL);
1189 vlp1 = VP2VNODELOCK(vp);
1190 vlp2 = VP2VNODELOCK(dvp);
1191 cache_sort(&vlp1, &vlp2);
1202 cache_unlock_vnodes_cel(struct celockstate *cel)
1205 MPASS(cel->vlp[0] != NULL || cel->vlp[1] != NULL);
1207 if (cel->vlp[0] != NULL)
1208 mtx_unlock(cel->vlp[0]);
1209 if (cel->vlp[1] != NULL)
1210 mtx_unlock(cel->vlp[1]);
1211 if (cel->vlp[2] != NULL)
1212 mtx_unlock(cel->vlp[2]);
1216 cache_lock_vnodes_cel_3(struct celockstate *cel, struct vnode *vp)
1221 cache_assert_vlp_locked(cel->vlp[0]);
1222 cache_assert_vlp_locked(cel->vlp[1]);
1223 MPASS(cel->vlp[2] == NULL);
1225 vlp = VP2VNODELOCK(vp);
1230 if (vlp >= cel->vlp[1]) {
1233 if (mtx_trylock(vlp))
1235 cache_lock_vnodes_cel_3_failures++;
1236 cache_unlock_vnodes_cel(cel);
1237 if (vlp < cel->vlp[0]) {
1239 mtx_lock(cel->vlp[0]);
1240 mtx_lock(cel->vlp[1]);
1242 if (cel->vlp[0] != NULL)
1243 mtx_lock(cel->vlp[0]);
1245 mtx_lock(cel->vlp[1]);
1255 cache_lock_buckets_cel(struct celockstate *cel, struct rwlock *blp1,
1256 struct rwlock *blp2)
1259 MPASS(cel->blp[0] == NULL);
1260 MPASS(cel->blp[1] == NULL);
1262 cache_sort(&blp1, &blp2);
1273 cache_unlock_buckets_cel(struct celockstate *cel)
1276 if (cel->blp[0] != NULL)
1277 rw_wunlock(cel->blp[0]);
1278 rw_wunlock(cel->blp[1]);
1282 * Lock part of the cache affected by the insertion.
1284 * This means vnodelocks for dvp, vp and the relevant bucketlock.
1285 * However, insertion can result in removal of an old entry. In this
1286 * case we have an additional vnode and bucketlock pair to lock. If the
1287 * entry is negative, ncelock is locked instead of the vnode.
1289 * That is, in the worst case we have to lock 3 vnodes and 2 bucketlocks, while
1290 * preserving the locking order (smaller address first).
1293 cache_enter_lock(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1296 struct namecache *ncp;
1297 struct rwlock *blps[2];
1299 blps[0] = HASH2BUCKETLOCK(hash);
1302 cache_lock_vnodes_cel(cel, dvp, vp);
1303 if (vp == NULL || vp->v_type != VDIR)
1305 ncp = vp->v_cache_dd;
1308 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1310 MPASS(ncp->nc_dvp == vp);
1311 blps[1] = NCP2BUCKETLOCK(ncp);
1312 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1315 * All vnodes got re-locked. Re-validate the state and if
1316 * nothing changed we are done. Otherwise restart.
1318 if (ncp == vp->v_cache_dd &&
1319 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1320 blps[1] == NCP2BUCKETLOCK(ncp) &&
1321 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1323 cache_unlock_vnodes_cel(cel);
1328 cache_lock_buckets_cel(cel, blps[0], blps[1]);
1332 cache_enter_lock_dd(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1335 struct namecache *ncp;
1336 struct rwlock *blps[2];
1338 blps[0] = HASH2BUCKETLOCK(hash);
1341 cache_lock_vnodes_cel(cel, dvp, vp);
1342 ncp = dvp->v_cache_dd;
1345 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1347 MPASS(ncp->nc_dvp == dvp);
1348 blps[1] = NCP2BUCKETLOCK(ncp);
1349 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1351 if (ncp == dvp->v_cache_dd &&
1352 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1353 blps[1] == NCP2BUCKETLOCK(ncp) &&
1354 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1356 cache_unlock_vnodes_cel(cel);
1361 cache_lock_buckets_cel(cel, blps[0], blps[1]);
1365 cache_enter_unlock(struct celockstate *cel)
1368 cache_unlock_buckets_cel(cel);
1369 cache_unlock_vnodes_cel(cel);
1373 * Add an entry to the cache.
1376 cache_enter_time(struct vnode *dvp, struct vnode *vp, struct componentname *cnp,
1377 struct timespec *tsp, struct timespec *dtsp)
1379 struct celockstate cel;
1380 struct namecache *ncp, *n2, *ndd;
1381 struct namecache_ts *n3;
1382 struct nchashhead *ncpp;
1387 CTR3(KTR_VFS, "cache_enter(%p, %p, %s)", dvp, vp, cnp->cn_nameptr);
1388 VNASSERT(vp == NULL || (vp->v_iflag & VI_DOOMED) == 0, vp,
1389 ("cache_enter: Adding a doomed vnode"));
1390 VNASSERT(dvp == NULL || (dvp->v_iflag & VI_DOOMED) == 0, dvp,
1391 ("cache_enter: Doomed vnode used as src"));
1397 * Avoid blowout in namecache entries.
1399 if (numcache >= desiredvnodes * ncsizefactor)
1402 cache_celockstate_init(&cel);
1405 if (cnp->cn_nameptr[0] == '.') {
1406 if (cnp->cn_namelen == 1)
1408 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
1409 len = cnp->cn_namelen;
1410 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1411 cache_enter_lock_dd(&cel, dvp, vp, hash);
1413 * If dotdot entry already exists, just retarget it
1414 * to new parent vnode, otherwise continue with new
1415 * namecache entry allocation.
1417 if ((ncp = dvp->v_cache_dd) != NULL &&
1418 ncp->nc_flag & NCF_ISDOTDOT) {
1419 KASSERT(ncp->nc_dvp == dvp,
1420 ("wrong isdotdot parent"));
1421 if (ncp->nc_vp != NULL) {
1422 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst,
1425 cache_negative_remove(ncp, false);
1428 TAILQ_INSERT_HEAD(&vp->v_cache_dst,
1431 cache_negative_insert(ncp);
1434 cache_enter_unlock(&cel);
1437 dvp->v_cache_dd = NULL;
1438 cache_enter_unlock(&cel);
1439 cache_celockstate_init(&cel);
1440 SDT_PROBE3(vfs, namecache, enter, done, dvp, "..", vp);
1441 flag = NCF_ISDOTDOT;
1446 * Calculate the hash key and setup as much of the new
1447 * namecache entry as possible before acquiring the lock.
1449 ncp = cache_alloc(cnp->cn_namelen, tsp != NULL);
1452 ncp->nc_flag = flag;
1454 n3 = (struct namecache_ts *)ncp;
1456 n3->nc_ticks = ticks;
1457 n3->nc_flag |= NCF_TS;
1459 n3->nc_dotdottime = *dtsp;
1460 n3->nc_flag |= NCF_DTS;
1463 len = ncp->nc_nlen = cnp->cn_namelen;
1464 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1465 strlcpy(nc_get_name(ncp), cnp->cn_nameptr, len + 1);
1466 cache_enter_lock(&cel, dvp, vp, hash);
1469 * See if this vnode or negative entry is already in the cache
1470 * with this name. This can happen with concurrent lookups of
1471 * the same path name.
1473 ncpp = NCHHASH(hash);
1474 LIST_FOREACH(n2, ncpp, nc_hash) {
1475 if (n2->nc_dvp == dvp &&
1476 n2->nc_nlen == cnp->cn_namelen &&
1477 !bcmp(nc_get_name(n2), cnp->cn_nameptr, n2->nc_nlen)) {
1479 KASSERT((n2->nc_flag & NCF_TS) != 0,
1481 n3 = (struct namecache_ts *)n2;
1483 ((struct namecache_ts *)ncp)->nc_time;
1485 ((struct namecache_ts *)ncp)->nc_ticks;
1488 ((struct namecache_ts *)ncp)->
1490 n3->nc_flag |= NCF_DTS;
1493 goto out_unlock_free;
1497 if (flag == NCF_ISDOTDOT) {
1499 * See if we are trying to add .. entry, but some other lookup
1500 * has populated v_cache_dd pointer already.
1502 if (dvp->v_cache_dd != NULL)
1503 goto out_unlock_free;
1504 KASSERT(vp == NULL || vp->v_type == VDIR,
1505 ("wrong vnode type %p", vp));
1506 dvp->v_cache_dd = ncp;
1509 atomic_add_rel_long(&numcache, 1);
1511 if (vp->v_type == VDIR) {
1512 if (flag != NCF_ISDOTDOT) {
1514 * For this case, the cache entry maps both the
1515 * directory name in it and the name ".." for the
1516 * directory's parent.
1518 if ((ndd = vp->v_cache_dd) != NULL) {
1519 if ((ndd->nc_flag & NCF_ISDOTDOT) != 0)
1520 cache_zap_locked(ndd, false);
1524 vp->v_cache_dd = ncp;
1527 vp->v_cache_dd = NULL;
1531 if (flag != NCF_ISDOTDOT) {
1532 if (LIST_EMPTY(&dvp->v_cache_src)) {
1534 atomic_add_rel_long(&numcachehv, 1);
1536 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
1540 * Insert the new namecache entry into the appropriate chain
1541 * within the cache entries table.
1543 LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
1546 * If the entry is "negative", we place it into the
1547 * "negative" cache queue, otherwise, we place it into the
1548 * destination vnode's cache entries queue.
1551 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
1552 SDT_PROBE3(vfs, namecache, enter, done, dvp, nc_get_name(ncp),
1555 if (cnp->cn_flags & ISWHITEOUT)
1556 ncp->nc_flag |= NCF_WHITE;
1557 cache_negative_insert(ncp);
1558 SDT_PROBE2(vfs, namecache, enter_negative, done, dvp,
1561 cache_enter_unlock(&cel);
1562 if (numneg * ncnegfactor > numcache)
1563 cache_negative_zap_one();
1567 cache_enter_unlock(&cel);
1573 cache_roundup_2(u_int val)
1577 for (res = 1; res <= val; res <<= 1)
1584 * Name cache initialization, from vfs_init() when we are booting
1587 nchinit(void *dummy __unused)
1593 cache_zone_small = uma_zcreate("S VFS Cache",
1594 sizeof(struct namecache) + CACHE_PATH_CUTOFF + 1,
1595 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
1596 cache_zone_small_ts = uma_zcreate("STS VFS Cache",
1597 sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1,
1598 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
1599 cache_zone_large = uma_zcreate("L VFS Cache",
1600 sizeof(struct namecache) + NAME_MAX + 1,
1601 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
1602 cache_zone_large_ts = uma_zcreate("LTS VFS Cache",
1603 sizeof(struct namecache_ts) + NAME_MAX + 1,
1604 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
1606 nchashtbl = hashinit(desiredvnodes * 2, M_VFSCACHE, &nchash);
1607 numbucketlocks = cache_roundup_2(mp_ncpus * 64);
1608 bucketlocks = malloc(sizeof(*bucketlocks) * numbucketlocks, M_VFSCACHE,
1610 for (i = 0; i < numbucketlocks; i++)
1611 rw_init_flags(&bucketlocks[i], "ncbuc", RW_DUPOK | RW_RECURSE);
1612 numvnodelocks = cache_roundup_2(mp_ncpus * 64);
1613 vnodelocks = malloc(sizeof(*vnodelocks) * numvnodelocks, M_VFSCACHE,
1615 for (i = 0; i < numvnodelocks; i++)
1616 mtx_init(&vnodelocks[i], "ncvn", NULL, MTX_DUPOK | MTX_RECURSE);
1618 numcalls = counter_u64_alloc(M_WAITOK);
1619 dothits = counter_u64_alloc(M_WAITOK);
1620 dotdothits = counter_u64_alloc(M_WAITOK);
1621 numchecks = counter_u64_alloc(M_WAITOK);
1622 nummiss = counter_u64_alloc(M_WAITOK);
1623 nummisszap = counter_u64_alloc(M_WAITOK);
1624 numposzaps = counter_u64_alloc(M_WAITOK);
1625 numposhits = counter_u64_alloc(M_WAITOK);
1626 numnegzaps = counter_u64_alloc(M_WAITOK);
1627 numneghits = counter_u64_alloc(M_WAITOK);
1628 numfullpathcalls = counter_u64_alloc(M_WAITOK);
1629 numfullpathfail1 = counter_u64_alloc(M_WAITOK);
1630 numfullpathfail2 = counter_u64_alloc(M_WAITOK);
1631 numfullpathfail4 = counter_u64_alloc(M_WAITOK);
1632 numfullpathfound = counter_u64_alloc(M_WAITOK);
1634 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL);
1637 cache_changesize(int newmaxvnodes)
1639 struct nchashhead *new_nchashtbl, *old_nchashtbl;
1640 u_long new_nchash, old_nchash;
1641 struct namecache *ncp;
1645 new_nchashtbl = hashinit(newmaxvnodes * 2, M_VFSCACHE, &new_nchash);
1646 /* If same hash table size, nothing to do */
1647 if (nchash == new_nchash) {
1648 free(new_nchashtbl, M_VFSCACHE);
1652 * Move everything from the old hash table to the new table.
1653 * None of the namecache entries in the table can be removed
1654 * because to do so, they have to be removed from the hash table.
1656 cache_lock_all_vnodes();
1657 cache_lock_all_buckets();
1658 old_nchashtbl = nchashtbl;
1659 old_nchash = nchash;
1660 nchashtbl = new_nchashtbl;
1661 nchash = new_nchash;
1662 for (i = 0; i <= old_nchash; i++) {
1663 while ((ncp = LIST_FIRST(&old_nchashtbl[i])) != NULL) {
1664 hash = cache_get_hash(nc_get_name(ncp), ncp->nc_nlen,
1666 LIST_REMOVE(ncp, nc_hash);
1667 LIST_INSERT_HEAD(NCHHASH(hash), ncp, nc_hash);
1670 cache_unlock_all_buckets();
1671 cache_unlock_all_vnodes();
1672 free(old_nchashtbl, M_VFSCACHE);
1676 * Invalidate all entries to a particular vnode.
1679 cache_purge(struct vnode *vp)
1681 TAILQ_HEAD(, namecache) ncps;
1682 struct namecache *ncp, *nnp;
1683 struct mtx *vlp, *vlp2;
1685 CTR1(KTR_VFS, "cache_purge(%p)", vp);
1686 SDT_PROBE1(vfs, namecache, purge, done, vp);
1687 if (LIST_EMPTY(&vp->v_cache_src) && TAILQ_EMPTY(&vp->v_cache_dst) &&
1688 vp->v_cache_dd == NULL)
1691 vlp = VP2VNODELOCK(vp);
1695 while (!LIST_EMPTY(&vp->v_cache_src)) {
1696 ncp = LIST_FIRST(&vp->v_cache_src);
1697 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
1699 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
1701 while (!TAILQ_EMPTY(&vp->v_cache_dst)) {
1702 ncp = TAILQ_FIRST(&vp->v_cache_dst);
1703 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
1705 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
1707 ncp = vp->v_cache_dd;
1709 KASSERT(ncp->nc_flag & NCF_ISDOTDOT,
1710 ("lost dotdot link"));
1711 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
1713 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
1715 KASSERT(vp->v_cache_dd == NULL, ("incomplete purge"));
1719 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
1725 * Invalidate all negative entries for a particular directory vnode.
1728 cache_purge_negative(struct vnode *vp)
1730 TAILQ_HEAD(, namecache) ncps;
1731 struct namecache *ncp, *nnp;
1734 CTR1(KTR_VFS, "cache_purge_negative(%p)", vp);
1735 SDT_PROBE1(vfs, namecache, purge_negative, done, vp);
1737 vlp = VP2VNODELOCK(vp);
1739 LIST_FOREACH_SAFE(ncp, &vp->v_cache_src, nc_src, nnp) {
1740 if (ncp->nc_vp != NULL)
1742 cache_zap_negative_locked_vnode_kl(ncp, vp);
1743 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
1746 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
1752 * Flush all entries referencing a particular filesystem.
1755 cache_purgevfs(struct mount *mp)
1757 TAILQ_HEAD(, namecache) ncps;
1758 struct mtx *vlp1, *vlp2;
1760 struct nchashhead *bucket;
1761 struct namecache *ncp, *nnp;
1762 u_long i, j, n_nchash;
1765 /* Scan hash tables for applicable entries */
1766 SDT_PROBE1(vfs, namecache, purgevfs, done, mp);
1768 n_nchash = nchash + 1;
1770 for (i = 0; i < numbucketlocks; i++) {
1771 blp = (struct rwlock *)&bucketlocks[i];
1773 for (j = i; j < n_nchash; j += numbucketlocks) {
1775 bucket = &nchashtbl[j];
1776 LIST_FOREACH_SAFE(ncp, bucket, nc_hash, nnp) {
1777 cache_assert_bucket_locked(ncp, RA_WLOCKED);
1778 if (ncp->nc_dvp->v_mount != mp)
1780 error = cache_zap_wlocked_bucket_kl(ncp, blp,
1784 TAILQ_INSERT_HEAD(&ncps, ncp, nc_dst);
1788 if (vlp1 == NULL && vlp2 == NULL)
1789 cache_maybe_yield();
1796 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
1802 * Perform canonical checks and cache lookup and pass on to filesystem
1803 * through the vop_cachedlookup only if needed.
1807 vfs_cache_lookup(struct vop_lookup_args *ap)
1811 struct vnode **vpp = ap->a_vpp;
1812 struct componentname *cnp = ap->a_cnp;
1813 struct ucred *cred = cnp->cn_cred;
1814 int flags = cnp->cn_flags;
1815 struct thread *td = cnp->cn_thread;
1820 if (dvp->v_type != VDIR)
1823 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
1824 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
1827 error = VOP_ACCESS(dvp, VEXEC, cred, td);
1831 error = cache_lookup(dvp, vpp, cnp, NULL, NULL);
1833 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
1840 * XXX All of these sysctls would probably be more productive dead.
1842 static int disablecwd;
1843 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0,
1844 "Disable the getcwd syscall");
1846 /* Implementation of the getcwd syscall. */
1848 sys___getcwd(struct thread *td, struct __getcwd_args *uap)
1851 return (kern___getcwd(td, uap->buf, UIO_USERSPACE, uap->buflen,
1856 kern___getcwd(struct thread *td, char *buf, enum uio_seg bufseg, u_int buflen,
1860 struct filedesc *fdp;
1861 struct vnode *cdir, *rdir;
1868 if (buflen > path_max)
1871 tmpbuf = malloc(buflen, M_TEMP, M_WAITOK);
1872 fdp = td->td_proc->p_fd;
1873 FILEDESC_SLOCK(fdp);
1874 cdir = fdp->fd_cdir;
1876 rdir = fdp->fd_rdir;
1878 FILEDESC_SUNLOCK(fdp);
1879 error = vn_fullpath1(td, cdir, rdir, tmpbuf, &bp, buflen);
1884 if (bufseg == UIO_SYSSPACE)
1885 bcopy(bp, buf, strlen(bp) + 1);
1887 error = copyout(bp, buf, strlen(bp) + 1);
1889 if (KTRPOINT(curthread, KTR_NAMEI))
1893 free(tmpbuf, M_TEMP);
1898 * Thus begins the fullpath magic.
1901 static int disablefullpath;
1902 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW, &disablefullpath, 0,
1903 "Disable the vn_fullpath function");
1906 * Retrieve the full filesystem path that correspond to a vnode from the name
1907 * cache (if available)
1910 vn_fullpath(struct thread *td, struct vnode *vn, char **retbuf, char **freebuf)
1913 struct filedesc *fdp;
1917 if (disablefullpath)
1922 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1923 fdp = td->td_proc->p_fd;
1924 FILEDESC_SLOCK(fdp);
1925 rdir = fdp->fd_rdir;
1927 FILEDESC_SUNLOCK(fdp);
1928 error = vn_fullpath1(td, vn, rdir, buf, retbuf, MAXPATHLEN);
1939 * This function is similar to vn_fullpath, but it attempts to lookup the
1940 * pathname relative to the global root mount point. This is required for the
1941 * auditing sub-system, as audited pathnames must be absolute, relative to the
1942 * global root mount point.
1945 vn_fullpath_global(struct thread *td, struct vnode *vn,
1946 char **retbuf, char **freebuf)
1951 if (disablefullpath)
1955 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1956 error = vn_fullpath1(td, vn, rootvnode, buf, retbuf, MAXPATHLEN);
1965 vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, u_int *buflen)
1968 struct namecache *ncp;
1972 vlp = VP2VNODELOCK(*vp);
1974 TAILQ_FOREACH(ncp, &((*vp)->v_cache_dst), nc_dst) {
1975 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1979 if (*buflen < ncp->nc_nlen) {
1982 counter_u64_add(numfullpathfail4, 1);
1984 SDT_PROBE3(vfs, namecache, fullpath, return, error,
1988 *buflen -= ncp->nc_nlen;
1989 memcpy(buf + *buflen, nc_get_name(ncp), ncp->nc_nlen);
1990 SDT_PROBE3(vfs, namecache, fullpath, hit, ncp->nc_dvp,
1991 nc_get_name(ncp), vp);
1999 SDT_PROBE1(vfs, namecache, fullpath, miss, vp);
2002 vn_lock(*vp, LK_SHARED | LK_RETRY);
2003 error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen);
2006 counter_u64_add(numfullpathfail2, 1);
2007 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2012 if (dvp->v_iflag & VI_DOOMED) {
2013 /* forced unmount */
2016 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2020 * *vp has its use count incremented still.
2027 * The magic behind kern___getcwd() and vn_fullpath().
2030 vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
2031 char *buf, char **retbuf, u_int buflen)
2033 int error, slash_prefixed;
2034 #ifdef KDTRACE_HOOKS
2035 struct vnode *startvp = vp;
2044 SDT_PROBE1(vfs, namecache, fullpath, entry, vp);
2045 counter_u64_add(numfullpathcalls, 1);
2047 if (vp->v_type != VDIR) {
2048 error = vn_vptocnp(&vp, td->td_ucred, buf, &buflen);
2055 buf[--buflen] = '/';
2058 while (vp != rdir && vp != rootvnode) {
2059 if (vp->v_vflag & VV_ROOT) {
2060 if (vp->v_iflag & VI_DOOMED) { /* forced unmount */
2063 SDT_PROBE3(vfs, namecache, fullpath, return,
2067 vp1 = vp->v_mount->mnt_vnodecovered;
2073 if (vp->v_type != VDIR) {
2075 counter_u64_add(numfullpathfail1, 1);
2077 SDT_PROBE3(vfs, namecache, fullpath, return,
2081 error = vn_vptocnp(&vp, td->td_ucred, buf, &buflen);
2087 SDT_PROBE3(vfs, namecache, fullpath, return, error,
2091 buf[--buflen] = '/';
2096 if (!slash_prefixed) {
2099 counter_u64_add(numfullpathfail4, 1);
2100 SDT_PROBE3(vfs, namecache, fullpath, return, ENOMEM,
2104 buf[--buflen] = '/';
2106 counter_u64_add(numfullpathfound, 1);
2109 SDT_PROBE3(vfs, namecache, fullpath, return, 0, startvp, buf + buflen);
2110 *retbuf = buf + buflen;
2115 vn_dir_dd_ino(struct vnode *vp)
2117 struct namecache *ncp;
2121 ASSERT_VOP_LOCKED(vp, "vn_dir_dd_ino");
2122 vlp = VP2VNODELOCK(vp);
2124 TAILQ_FOREACH(ncp, &(vp->v_cache_dst), nc_dst) {
2125 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0)
2130 if (vget(ddvp, LK_SHARED | LK_NOWAIT | LK_VNHELD, curthread))
2139 vn_commname(struct vnode *vp, char *buf, u_int buflen)
2141 struct namecache *ncp;
2145 vlp = VP2VNODELOCK(vp);
2147 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst)
2148 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2154 l = min(ncp->nc_nlen, buflen - 1);
2155 memcpy(buf, nc_get_name(ncp), l);
2161 /* ABI compat shims for old kernel modules. */
2164 void cache_enter(struct vnode *dvp, struct vnode *vp,
2165 struct componentname *cnp);
2168 cache_enter(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2171 cache_enter_time(dvp, vp, cnp, NULL, NULL);
2175 * This function updates path string to vnode's full global path
2176 * and checks the size of the new path string against the pathlen argument.
2178 * Requires a locked, referenced vnode.
2179 * Vnode is re-locked on success or ENODEV, otherwise unlocked.
2181 * If sysctl debug.disablefullpath is set, ENODEV is returned,
2182 * vnode is left locked and path remain untouched.
2184 * If vp is a directory, the call to vn_fullpath_global() always succeeds
2185 * because it falls back to the ".." lookup if the namecache lookup fails.
2188 vn_path_to_global_path(struct thread *td, struct vnode *vp, char *path,
2191 struct nameidata nd;
2196 ASSERT_VOP_ELOCKED(vp, __func__);
2198 /* Return ENODEV if sysctl debug.disablefullpath==1 */
2199 if (disablefullpath)
2202 /* Construct global filesystem path from vp. */
2204 error = vn_fullpath_global(td, vp, &rpath, &fbuf);
2211 if (strlen(rpath) >= pathlen) {
2213 error = ENAMETOOLONG;
2218 * Re-lookup the vnode by path to detect a possible rename.
2219 * As a side effect, the vnode is relocked.
2220 * If vnode was renamed, return ENOENT.
2222 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
2223 UIO_SYSSPACE, path, td);
2229 NDFREE(&nd, NDF_ONLY_PNBUF);
2233 strcpy(path, rpath);