2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993, 1995
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Poul-Henning Kamp of the FreeBSD Project.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)vfs_cache.c 8.5 (Berkeley) 3/22/95
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
41 #include "opt_ktrace.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/capsicum.h>
46 #include <sys/counter.h>
47 #include <sys/filedesc.h>
48 #include <sys/fnv_hash.h>
49 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/fcntl.h>
55 #include <sys/mount.h>
56 #include <sys/namei.h>
62 #include <sys/syscallsubr.h>
63 #include <sys/sysctl.h>
64 #include <sys/sysproto.h>
65 #include <sys/vnode.h>
68 #include <sys/ktrace.h>
71 #include <sys/capsicum.h>
73 #include <security/audit/audit.h>
74 #include <security/mac/mac_framework.h>
82 static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
85 SDT_PROVIDER_DECLARE(vfs);
86 SDT_PROBE_DEFINE3(vfs, namecache, enter, done, "struct vnode *", "char *",
88 SDT_PROBE_DEFINE3(vfs, namecache, enter, duplicate, "struct vnode *", "char *",
90 SDT_PROBE_DEFINE2(vfs, namecache, enter_negative, done, "struct vnode *",
92 SDT_PROBE_DEFINE2(vfs, namecache, fullpath_smr, hit, "struct vnode *",
94 SDT_PROBE_DEFINE4(vfs, namecache, fullpath_smr, miss, "struct vnode *",
95 "struct namecache *", "int", "int");
96 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, entry, "struct vnode *");
97 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, hit, "struct vnode *",
98 "char *", "struct vnode *");
99 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, miss, "struct vnode *");
100 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, return, "int",
101 "struct vnode *", "char *");
102 SDT_PROBE_DEFINE3(vfs, namecache, lookup, hit, "struct vnode *", "char *",
104 SDT_PROBE_DEFINE2(vfs, namecache, lookup, hit__negative,
105 "struct vnode *", "char *");
106 SDT_PROBE_DEFINE2(vfs, namecache, lookup, miss, "struct vnode *",
108 SDT_PROBE_DEFINE2(vfs, namecache, removecnp, hit, "struct vnode *",
109 "struct componentname *");
110 SDT_PROBE_DEFINE2(vfs, namecache, removecnp, miss, "struct vnode *",
111 "struct componentname *");
112 SDT_PROBE_DEFINE1(vfs, namecache, purge, done, "struct vnode *");
113 SDT_PROBE_DEFINE1(vfs, namecache, purge_negative, done, "struct vnode *");
114 SDT_PROBE_DEFINE1(vfs, namecache, purgevfs, done, "struct mount *");
115 SDT_PROBE_DEFINE3(vfs, namecache, zap, done, "struct vnode *", "char *",
117 SDT_PROBE_DEFINE2(vfs, namecache, zap_negative, done, "struct vnode *",
119 SDT_PROBE_DEFINE2(vfs, namecache, evict_negative, done, "struct vnode *",
122 SDT_PROBE_DEFINE3(vfs, fplookup, lookup, done, "struct nameidata", "int", "bool");
123 SDT_PROBE_DECLARE(vfs, namei, lookup, entry);
124 SDT_PROBE_DECLARE(vfs, namei, lookup, return);
127 * This structure describes the elements in the cache of recent
128 * names looked up by namei.
134 _Static_assert(sizeof(struct negstate) <= sizeof(struct vnode *),
135 "the state must fit in a union with a pointer without growing it");
138 LIST_ENTRY(namecache) nc_src; /* source vnode list */
139 TAILQ_ENTRY(namecache) nc_dst; /* destination vnode list */
140 CK_SLIST_ENTRY(namecache) nc_hash;/* hash chain */
141 struct vnode *nc_dvp; /* vnode of parent of name */
143 struct vnode *nu_vp; /* vnode the name refers to */
144 struct negstate nu_neg;/* negative entry state */
146 u_char nc_flag; /* flag bits */
147 u_char nc_nlen; /* length of name */
148 char nc_name[0]; /* segment name + nul */
152 * struct namecache_ts repeats struct namecache layout up to the
154 * struct namecache_ts is used in place of struct namecache when time(s) need
155 * to be stored. The nc_dotdottime field is used when a cache entry is mapping
156 * both a non-dotdot directory name plus dotdot for the directory's
159 * See below for alignment requirement.
161 struct namecache_ts {
162 struct timespec nc_time; /* timespec provided by fs */
163 struct timespec nc_dotdottime; /* dotdot timespec provided by fs */
164 int nc_ticks; /* ticks value when entry was added */
165 struct namecache nc_nc;
169 * At least mips n32 performs 64-bit accesses to timespec as found
170 * in namecache_ts and requires them to be aligned. Since others
171 * may be in the same spot suffer a little bit and enforce the
172 * alignment for everyone. Note this is a nop for 64-bit platforms.
174 #define CACHE_ZONE_ALIGNMENT UMA_ALIGNOF(time_t)
175 #define CACHE_PATH_CUTOFF 39
177 #define CACHE_ZONE_SMALL_SIZE (sizeof(struct namecache) + CACHE_PATH_CUTOFF + 1)
178 #define CACHE_ZONE_SMALL_TS_SIZE (sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1)
179 #define CACHE_ZONE_LARGE_SIZE (sizeof(struct namecache) + NAME_MAX + 1)
180 #define CACHE_ZONE_LARGE_TS_SIZE (sizeof(struct namecache_ts) + NAME_MAX + 1)
182 _Static_assert((CACHE_ZONE_SMALL_SIZE % (CACHE_ZONE_ALIGNMENT + 1)) == 0, "bad zone size");
183 _Static_assert((CACHE_ZONE_SMALL_TS_SIZE % (CACHE_ZONE_ALIGNMENT + 1)) == 0, "bad zone size");
184 _Static_assert((CACHE_ZONE_LARGE_SIZE % (CACHE_ZONE_ALIGNMENT + 1)) == 0, "bad zone size");
185 _Static_assert((CACHE_ZONE_LARGE_TS_SIZE % (CACHE_ZONE_ALIGNMENT + 1)) == 0, "bad zone size");
187 #define nc_vp n_un.nu_vp
188 #define nc_neg n_un.nu_neg
191 * Flags in namecache.nc_flag
193 #define NCF_WHITE 0x01
194 #define NCF_ISDOTDOT 0x02
197 #define NCF_DVDROP 0x10
198 #define NCF_NEGATIVE 0x20
199 #define NCF_INVALID 0x40
203 * Flags in negstate.neg_flag
208 * Mark an entry as invalid.
210 * This is called before it starts getting deconstructed.
213 cache_ncp_invalidate(struct namecache *ncp)
216 KASSERT((ncp->nc_flag & NCF_INVALID) == 0,
217 ("%s: entry %p already invalid", __func__, ncp));
218 atomic_store_char(&ncp->nc_flag, ncp->nc_flag | NCF_INVALID);
219 atomic_thread_fence_rel();
223 * Check whether the entry can be safely used.
225 * All places which elide locks are supposed to call this after they are
226 * done with reading from an entry.
229 cache_ncp_canuse(struct namecache *ncp)
232 atomic_thread_fence_acq();
233 return ((atomic_load_char(&ncp->nc_flag) & (NCF_INVALID | NCF_WIP)) == 0);
237 * Name caching works as follows:
239 * Names found by directory scans are retained in a cache
240 * for future reference. It is managed LRU, so frequently
241 * used names will hang around. Cache is indexed by hash value
242 * obtained from (dvp, name) where dvp refers to the directory
245 * If it is a "negative" entry, (i.e. for a name that is known NOT to
246 * exist) the vnode pointer will be NULL.
248 * Upon reaching the last segment of a path, if the reference
249 * is for DELETE, or NOCACHE is set (rewrite), and the
250 * name is located in the cache, it will be dropped.
252 * These locks are used (in the order in which they can be taken):
254 * vnodelock mtx vnode lists and v_cache_dd field protection
255 * bucketlock mtx for access to given set of hash buckets
256 * neglist mtx negative entry LRU management
258 * It is legal to take multiple vnodelock and bucketlock locks. The locking
259 * order is lower address first. Both are recursive.
261 * "." lookups are lockless.
263 * ".." and vnode -> name lookups require vnodelock.
265 * name -> vnode lookup requires the relevant bucketlock to be held for reading.
267 * Insertions and removals of entries require involved vnodes and bucketlocks
268 * to be locked to provide safe operation against other threads modifying the
271 * Some lookups result in removal of the found entry (e.g. getting rid of a
272 * negative entry with the intent to create a positive one), which poses a
273 * problem when multiple threads reach the state. Similarly, two different
274 * threads can purge two different vnodes and try to remove the same name.
276 * If the already held vnode lock is lower than the second required lock, we
277 * can just take the other lock. However, in the opposite case, this could
278 * deadlock. As such, this is resolved by trylocking and if that fails unlocking
279 * the first node, locking everything in order and revalidating the state.
284 static SYSCTL_NODE(_vfs_cache, OID_AUTO, param, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
285 "Name cache parameters");
287 static u_int __read_mostly ncsize; /* the size as computed on creation or resizing */
288 SYSCTL_UINT(_vfs_cache_param, OID_AUTO, size, CTLFLAG_RW, &ncsize, 0,
289 "Total namecache capacity");
291 u_int ncsizefactor = 2;
292 SYSCTL_UINT(_vfs_cache_param, OID_AUTO, sizefactor, CTLFLAG_RW, &ncsizefactor, 0,
293 "Size factor for namecache");
295 static u_long __read_mostly ncnegfactor = 5; /* ratio of negative entries */
296 SYSCTL_ULONG(_vfs_cache_param, OID_AUTO, negfactor, CTLFLAG_RW, &ncnegfactor, 0,
297 "Ratio of negative namecache entries");
300 * Negative entry % of namecahe capacity above which automatic eviction is allowed.
302 * Check cache_neg_evict_cond for details.
304 static u_int ncnegminpct = 3;
306 static u_int __read_mostly neg_min; /* the above recomputed against ncsize */
307 SYSCTL_UINT(_vfs_cache_param, OID_AUTO, negmin, CTLFLAG_RD, &neg_min, 0,
308 "Negative entry count above which automatic eviction is allowed");
311 * Structures associated with name caching.
313 #define NCHHASH(hash) \
314 (&nchashtbl[(hash) & nchash])
315 static __read_mostly CK_SLIST_HEAD(nchashhead, namecache) *nchashtbl;/* Hash Table */
316 static u_long __read_mostly nchash; /* size of hash table */
317 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0,
318 "Size of namecache hash table");
319 static u_long __exclusive_cache_line numneg; /* number of negative entries allocated */
320 static u_long __exclusive_cache_line numcache;/* number of cache entries allocated */
322 struct nchstats nchstats; /* cache effectiveness statistics */
324 static bool __read_frequently cache_fast_revlookup = true;
325 SYSCTL_BOOL(_vfs, OID_AUTO, cache_fast_revlookup, CTLFLAG_RW,
326 &cache_fast_revlookup, 0, "");
328 static u_int __exclusive_cache_line neg_cycle;
331 #define numneglists (ncneghash + 1)
334 struct mtx nl_evict_lock;
335 struct mtx nl_lock __aligned(CACHE_LINE_SIZE);
336 TAILQ_HEAD(, namecache) nl_list;
337 TAILQ_HEAD(, namecache) nl_hotlist;
339 } __aligned(CACHE_LINE_SIZE);
341 static struct neglist neglists[numneglists];
343 static inline struct neglist *
344 NCP2NEGLIST(struct namecache *ncp)
347 return (&neglists[(((uintptr_t)(ncp) >> 8) & ncneghash)]);
350 static inline struct negstate *
351 NCP2NEGSTATE(struct namecache *ncp)
354 MPASS(ncp->nc_flag & NCF_NEGATIVE);
355 return (&ncp->nc_neg);
358 #define numbucketlocks (ncbuckethash + 1)
359 static u_int __read_mostly ncbuckethash;
360 static struct mtx_padalign __read_mostly *bucketlocks;
361 #define HASH2BUCKETLOCK(hash) \
362 ((struct mtx *)(&bucketlocks[((hash) & ncbuckethash)]))
364 #define numvnodelocks (ncvnodehash + 1)
365 static u_int __read_mostly ncvnodehash;
366 static struct mtx __read_mostly *vnodelocks;
367 static inline struct mtx *
368 VP2VNODELOCK(struct vnode *vp)
371 return (&vnodelocks[(((uintptr_t)(vp) >> 8) & ncvnodehash)]);
375 * UMA zones for the VFS cache.
377 * The small cache is used for entries with short names, which are the
378 * most common. The large cache is used for entries which are too big to
379 * fit in the small cache.
381 static uma_zone_t __read_mostly cache_zone_small;
382 static uma_zone_t __read_mostly cache_zone_small_ts;
383 static uma_zone_t __read_mostly cache_zone_large;
384 static uma_zone_t __read_mostly cache_zone_large_ts;
386 static struct namecache *
387 cache_alloc(int len, int ts)
389 struct namecache_ts *ncp_ts;
390 struct namecache *ncp;
392 if (__predict_false(ts)) {
393 if (len <= CACHE_PATH_CUTOFF)
394 ncp_ts = uma_zalloc_smr(cache_zone_small_ts, M_WAITOK);
396 ncp_ts = uma_zalloc_smr(cache_zone_large_ts, M_WAITOK);
397 ncp = &ncp_ts->nc_nc;
399 if (len <= CACHE_PATH_CUTOFF)
400 ncp = uma_zalloc_smr(cache_zone_small, M_WAITOK);
402 ncp = uma_zalloc_smr(cache_zone_large, M_WAITOK);
408 cache_free(struct namecache *ncp)
410 struct namecache_ts *ncp_ts;
413 if ((ncp->nc_flag & NCF_DVDROP) != 0)
415 if (__predict_false(ncp->nc_flag & NCF_TS)) {
416 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
417 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
418 uma_zfree_smr(cache_zone_small_ts, ncp_ts);
420 uma_zfree_smr(cache_zone_large_ts, ncp_ts);
422 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
423 uma_zfree_smr(cache_zone_small, ncp);
425 uma_zfree_smr(cache_zone_large, ncp);
430 cache_out_ts(struct namecache *ncp, struct timespec *tsp, int *ticksp)
432 struct namecache_ts *ncp_ts;
434 KASSERT((ncp->nc_flag & NCF_TS) != 0 ||
435 (tsp == NULL && ticksp == NULL),
441 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
442 *tsp = ncp_ts->nc_time;
443 *ticksp = ncp_ts->nc_ticks;
447 static int __read_mostly doingcache = 1; /* 1 => enable the cache */
448 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0,
449 "VFS namecache enabled");
452 /* Export size information to userland */
453 SYSCTL_INT(_debug_sizeof, OID_AUTO, namecache, CTLFLAG_RD, SYSCTL_NULL_INT_PTR,
454 sizeof(struct namecache), "sizeof(struct namecache)");
457 * The new name cache statistics
459 static SYSCTL_NODE(_vfs_cache, OID_AUTO, stats, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
460 "Name cache statistics");
462 #define STATNODE_ULONG(name, varname, descr) \
463 SYSCTL_ULONG(_vfs_cache_stats, OID_AUTO, name, CTLFLAG_RD, &varname, 0, descr);
464 #define STATNODE_COUNTER(name, varname, descr) \
465 static COUNTER_U64_DEFINE_EARLY(varname); \
466 SYSCTL_COUNTER_U64(_vfs_cache_stats, OID_AUTO, name, CTLFLAG_RD, &varname, \
468 STATNODE_ULONG(neg, numneg, "Number of negative cache entries");
469 STATNODE_ULONG(count, numcache, "Number of cache entries");
470 STATNODE_COUNTER(heldvnodes, numcachehv, "Number of namecache entries with vnodes held");
471 STATNODE_COUNTER(drops, numdrops, "Number of dropped entries due to reaching the limit");
472 STATNODE_COUNTER(dothits, dothits, "Number of '.' hits");
473 STATNODE_COUNTER(dotdothis, dotdothits, "Number of '..' hits");
474 STATNODE_COUNTER(miss, nummiss, "Number of cache misses");
475 STATNODE_COUNTER(misszap, nummisszap, "Number of cache misses we do not want to cache");
476 STATNODE_COUNTER(posszaps, numposzaps,
477 "Number of cache hits (positive) we do not want to cache");
478 STATNODE_COUNTER(poshits, numposhits, "Number of cache hits (positive)");
479 STATNODE_COUNTER(negzaps, numnegzaps,
480 "Number of cache hits (negative) we do not want to cache");
481 STATNODE_COUNTER(neghits, numneghits, "Number of cache hits (negative)");
482 /* These count for vn_getcwd(), too. */
483 STATNODE_COUNTER(fullpathcalls, numfullpathcalls, "Number of fullpath search calls");
484 STATNODE_COUNTER(fullpathfail1, numfullpathfail1, "Number of fullpath search errors (ENOTDIR)");
485 STATNODE_COUNTER(fullpathfail2, numfullpathfail2,
486 "Number of fullpath search errors (VOP_VPTOCNP failures)");
487 STATNODE_COUNTER(fullpathfail4, numfullpathfail4, "Number of fullpath search errors (ENOMEM)");
488 STATNODE_COUNTER(fullpathfound, numfullpathfound, "Number of successful fullpath calls");
491 * Debug or developer statistics.
493 static SYSCTL_NODE(_vfs_cache, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
494 "Name cache debugging");
495 #define DEBUGNODE_ULONG(name, varname, descr) \
496 SYSCTL_ULONG(_vfs_cache_debug, OID_AUTO, name, CTLFLAG_RD, &varname, 0, descr);
497 #define DEBUGNODE_COUNTER(name, varname, descr) \
498 static COUNTER_U64_DEFINE_EARLY(varname); \
499 SYSCTL_COUNTER_U64(_vfs_cache_debug, OID_AUTO, name, CTLFLAG_RD, &varname, \
501 DEBUGNODE_COUNTER(zap_bucket_relock_success, zap_bucket_relock_success,
502 "Number of successful removals after relocking");
503 static long zap_bucket_fail;
504 DEBUGNODE_ULONG(zap_bucket_fail, zap_bucket_fail, "");
505 static long zap_bucket_fail2;
506 DEBUGNODE_ULONG(zap_bucket_fail2, zap_bucket_fail2, "");
507 static long cache_lock_vnodes_cel_3_failures;
508 DEBUGNODE_ULONG(vnodes_cel_3_failures, cache_lock_vnodes_cel_3_failures,
509 "Number of times 3-way vnode locking failed");
511 static void cache_zap_locked(struct namecache *ncp);
512 static int vn_fullpath_hardlink(struct nameidata *ndp, char **retbuf,
513 char **freebuf, size_t *buflen);
514 static int vn_fullpath_any_smr(struct vnode *vp, struct vnode *rdir, char *buf,
515 char **retbuf, size_t *buflen, bool slash_prefixed, size_t addend);
516 static int vn_fullpath_any(struct vnode *vp, struct vnode *rdir, char *buf,
517 char **retbuf, size_t *buflen);
518 static int vn_fullpath_dir(struct vnode *vp, struct vnode *rdir, char *buf,
519 char **retbuf, size_t *len, bool slash_prefixed, size_t addend);
521 static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
524 cache_assert_vlp_locked(struct mtx *vlp)
528 mtx_assert(vlp, MA_OWNED);
532 cache_assert_vnode_locked(struct vnode *vp)
536 vlp = VP2VNODELOCK(vp);
537 cache_assert_vlp_locked(vlp);
541 * TODO: With the value stored we can do better than computing the hash based
542 * on the address. The choice of FNV should also be revisited.
545 cache_prehash(struct vnode *vp)
548 vp->v_nchash = fnv_32_buf(&vp, sizeof(vp), FNV1_32_INIT);
552 cache_get_hash(char *name, u_char len, struct vnode *dvp)
555 return (fnv_32_buf(name, len, dvp->v_nchash));
558 static inline struct nchashhead *
559 NCP2BUCKET(struct namecache *ncp)
563 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen, ncp->nc_dvp);
564 return (NCHHASH(hash));
567 static inline struct mtx *
568 NCP2BUCKETLOCK(struct namecache *ncp)
572 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen, ncp->nc_dvp);
573 return (HASH2BUCKETLOCK(hash));
578 cache_assert_bucket_locked(struct namecache *ncp)
582 blp = NCP2BUCKETLOCK(ncp);
583 mtx_assert(blp, MA_OWNED);
587 cache_assert_bucket_unlocked(struct namecache *ncp)
591 blp = NCP2BUCKETLOCK(ncp);
592 mtx_assert(blp, MA_NOTOWNED);
595 #define cache_assert_bucket_locked(x) do { } while (0)
596 #define cache_assert_bucket_unlocked(x) do { } while (0)
599 #define cache_sort_vnodes(x, y) _cache_sort_vnodes((void **)(x), (void **)(y))
601 _cache_sort_vnodes(void **p1, void **p2)
605 MPASS(*p1 != NULL || *p2 != NULL);
615 cache_lock_all_buckets(void)
619 for (i = 0; i < numbucketlocks; i++)
620 mtx_lock(&bucketlocks[i]);
624 cache_unlock_all_buckets(void)
628 for (i = 0; i < numbucketlocks; i++)
629 mtx_unlock(&bucketlocks[i]);
633 cache_lock_all_vnodes(void)
637 for (i = 0; i < numvnodelocks; i++)
638 mtx_lock(&vnodelocks[i]);
642 cache_unlock_all_vnodes(void)
646 for (i = 0; i < numvnodelocks; i++)
647 mtx_unlock(&vnodelocks[i]);
651 cache_trylock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
654 cache_sort_vnodes(&vlp1, &vlp2);
657 if (!mtx_trylock(vlp1))
660 if (!mtx_trylock(vlp2)) {
670 cache_lock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
673 MPASS(vlp1 != NULL || vlp2 != NULL);
683 cache_unlock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
686 MPASS(vlp1 != NULL || vlp2 != NULL);
695 sysctl_nchstats(SYSCTL_HANDLER_ARGS)
697 struct nchstats snap;
699 if (req->oldptr == NULL)
700 return (SYSCTL_OUT(req, 0, sizeof(snap)));
703 snap.ncs_goodhits = counter_u64_fetch(numposhits);
704 snap.ncs_neghits = counter_u64_fetch(numneghits);
705 snap.ncs_badhits = counter_u64_fetch(numposzaps) +
706 counter_u64_fetch(numnegzaps);
707 snap.ncs_miss = counter_u64_fetch(nummisszap) +
708 counter_u64_fetch(nummiss);
710 return (SYSCTL_OUT(req, &snap, sizeof(snap)));
712 SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE | CTLFLAG_RD |
713 CTLFLAG_MPSAFE, 0, 0, sysctl_nchstats, "LU",
714 "VFS cache effectiveness statistics");
717 cache_recalc_neg_min(u_int val)
720 neg_min = (ncsize * val) / 100;
724 sysctl_negminpct(SYSCTL_HANDLER_ARGS)
730 error = sysctl_handle_int(oidp, &val, 0, req);
731 if (error != 0 || req->newptr == NULL)
734 if (val == ncnegminpct)
736 if (val < 0 || val > 99)
739 cache_recalc_neg_min(val);
743 SYSCTL_PROC(_vfs_cache_param, OID_AUTO, negminpct,
744 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, NULL, 0, sysctl_negminpct,
745 "I", "Negative entry \% of namecahe capacity above which automatic eviction is allowed");
749 * Grab an atomic snapshot of the name cache hash chain lengths
751 static SYSCTL_NODE(_debug, OID_AUTO, hashstat,
752 CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
756 sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS)
758 struct nchashhead *ncpp;
759 struct namecache *ncp;
760 int i, error, n_nchash, *cntbuf;
763 n_nchash = nchash + 1; /* nchash is max index, not count */
764 if (req->oldptr == NULL)
765 return SYSCTL_OUT(req, 0, n_nchash * sizeof(int));
766 cntbuf = malloc(n_nchash * sizeof(int), M_TEMP, M_ZERO | M_WAITOK);
767 cache_lock_all_buckets();
768 if (n_nchash != nchash + 1) {
769 cache_unlock_all_buckets();
770 free(cntbuf, M_TEMP);
773 /* Scan hash tables counting entries */
774 for (ncpp = nchashtbl, i = 0; i < n_nchash; ncpp++, i++)
775 CK_SLIST_FOREACH(ncp, ncpp, nc_hash)
777 cache_unlock_all_buckets();
778 for (error = 0, i = 0; i < n_nchash; i++)
779 if ((error = SYSCTL_OUT(req, &cntbuf[i], sizeof(int))) != 0)
781 free(cntbuf, M_TEMP);
784 SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD|
785 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_rawnchash, "S,int",
786 "nchash chain lengths");
789 sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS)
792 struct nchashhead *ncpp;
793 struct namecache *ncp;
795 int count, maxlength, used, pct;
798 return SYSCTL_OUT(req, 0, 4 * sizeof(int));
800 cache_lock_all_buckets();
801 n_nchash = nchash + 1; /* nchash is max index, not count */
805 /* Scan hash tables for applicable entries */
806 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
808 CK_SLIST_FOREACH(ncp, ncpp, nc_hash) {
813 if (maxlength < count)
816 n_nchash = nchash + 1;
817 cache_unlock_all_buckets();
818 pct = (used * 100) / (n_nchash / 100);
819 error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash));
822 error = SYSCTL_OUT(req, &used, sizeof(used));
825 error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength));
828 error = SYSCTL_OUT(req, &pct, sizeof(pct));
833 SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD|
834 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_nchash, "I",
835 "nchash statistics (number of total/used buckets, maximum chain length, usage percentage)");
839 * Negative entries management
841 * Various workloads create plenty of negative entries and barely use them
842 * afterwards. Moreover malicious users can keep performing bogus lookups
843 * adding even more entries. For example "make tinderbox" as of writing this
844 * comment ends up with 2.6M namecache entries in total, 1.2M of which are
847 * As such, a rather aggressive eviction method is needed. The currently
848 * employed method is a placeholder.
850 * Entries are split over numneglists separate lists, each of which is further
851 * split into hot and cold entries. Entries get promoted after getting a hit.
852 * Eviction happens on addition of new entry.
854 static SYSCTL_NODE(_vfs_cache, OID_AUTO, neg, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
855 "Name cache negative entry statistics");
857 SYSCTL_ULONG(_vfs_cache_neg, OID_AUTO, count, CTLFLAG_RD, &numneg, 0,
858 "Number of negative cache entries");
860 static COUNTER_U64_DEFINE_EARLY(neg_created);
861 SYSCTL_COUNTER_U64(_vfs_cache_neg, OID_AUTO, created, CTLFLAG_RD, &neg_created,
862 "Number of created negative entries");
864 static COUNTER_U64_DEFINE_EARLY(neg_evicted);
865 SYSCTL_COUNTER_U64(_vfs_cache_neg, OID_AUTO, evicted, CTLFLAG_RD, &neg_evicted,
866 "Number of evicted negative entries");
868 static COUNTER_U64_DEFINE_EARLY(neg_evict_skipped_empty);
869 SYSCTL_COUNTER_U64(_vfs_cache_neg, OID_AUTO, evict_skipped_empty, CTLFLAG_RD,
870 &neg_evict_skipped_empty,
871 "Number of times evicting failed due to lack of entries");
873 static COUNTER_U64_DEFINE_EARLY(neg_evict_skipped_missed);
874 SYSCTL_COUNTER_U64(_vfs_cache_neg, OID_AUTO, evict_skipped_missed, CTLFLAG_RD,
875 &neg_evict_skipped_missed,
876 "Number of times evicting failed due to target entry disappearing");
878 static COUNTER_U64_DEFINE_EARLY(neg_evict_skipped_contended);
879 SYSCTL_COUNTER_U64(_vfs_cache_neg, OID_AUTO, evict_skipped_contended, CTLFLAG_RD,
880 &neg_evict_skipped_contended,
881 "Number of times evicting failed due to contention");
883 SYSCTL_COUNTER_U64(_vfs_cache_neg, OID_AUTO, hits, CTLFLAG_RD, &numneghits,
884 "Number of cache hits (negative)");
887 sysctl_neg_hot(SYSCTL_HANDLER_ARGS)
892 for (i = 0; i < numneglists; i++)
893 out += neglists[i].nl_hotnum;
895 return (SYSCTL_OUT(req, &out, sizeof(out)));
897 SYSCTL_PROC(_vfs_cache_neg, OID_AUTO, hot, CTLTYPE_INT | CTLFLAG_RD |
898 CTLFLAG_MPSAFE, 0, 0, sysctl_neg_hot, "I",
899 "Number of hot negative entries");
902 cache_neg_init(struct namecache *ncp)
906 ncp->nc_flag |= NCF_NEGATIVE;
907 ns = NCP2NEGSTATE(ncp);
910 counter_u64_add(neg_created, 1);
913 #define CACHE_NEG_PROMOTION_THRESH 2
916 cache_neg_hit_prep(struct namecache *ncp)
921 ns = NCP2NEGSTATE(ncp);
922 n = atomic_load_char(&ns->neg_hit);
924 if (n >= CACHE_NEG_PROMOTION_THRESH)
926 if (atomic_fcmpset_8(&ns->neg_hit, &n, n + 1))
929 return (n + 1 == CACHE_NEG_PROMOTION_THRESH);
933 * Nothing to do here but it is provided for completeness as some
934 * cache_neg_hit_prep callers may end up returning without even
937 #define cache_neg_hit_abort(ncp) do { } while (0)
940 cache_neg_hit_finish(struct namecache *ncp)
943 SDT_PROBE2(vfs, namecache, lookup, hit__negative, ncp->nc_dvp, ncp->nc_name);
944 counter_u64_add(numneghits, 1);
948 * Move a negative entry to the hot list.
951 cache_neg_promote_locked(struct namecache *ncp)
956 ns = NCP2NEGSTATE(ncp);
957 nl = NCP2NEGLIST(ncp);
958 mtx_assert(&nl->nl_lock, MA_OWNED);
959 if ((ns->neg_flag & NEG_HOT) == 0) {
960 TAILQ_REMOVE(&nl->nl_list, ncp, nc_dst);
961 TAILQ_INSERT_TAIL(&nl->nl_hotlist, ncp, nc_dst);
963 ns->neg_flag |= NEG_HOT;
968 * Move a hot negative entry to the cold list.
971 cache_neg_demote_locked(struct namecache *ncp)
976 ns = NCP2NEGSTATE(ncp);
977 nl = NCP2NEGLIST(ncp);
978 mtx_assert(&nl->nl_lock, MA_OWNED);
979 MPASS(ns->neg_flag & NEG_HOT);
980 TAILQ_REMOVE(&nl->nl_hotlist, ncp, nc_dst);
981 TAILQ_INSERT_TAIL(&nl->nl_list, ncp, nc_dst);
983 ns->neg_flag &= ~NEG_HOT;
984 atomic_store_char(&ns->neg_hit, 0);
988 * Move a negative entry to the hot list if it matches the lookup.
990 * We have to take locks, but they may be contended and in the worst
991 * case we may need to go off CPU. We don't want to spin within the
992 * smr section and we can't block with it. Exiting the section means
993 * the found entry could have been evicted. We are going to look it
997 cache_neg_promote_cond(struct vnode *dvp, struct componentname *cnp,
998 struct namecache *oncp, uint32_t hash)
1000 struct namecache *ncp;
1004 nl = NCP2NEGLIST(oncp);
1006 mtx_lock(&nl->nl_lock);
1008 * For hash iteration.
1013 * Avoid all surprises by only succeeding if we got the same entry and
1014 * bailing completely otherwise.
1015 * XXX There are no provisions to keep the vnode around, meaning we may
1016 * end up promoting a negative entry for a *new* vnode and returning
1017 * ENOENT on its account. This is the error we want to return anyway
1018 * and promotion is harmless.
1020 * In particular at this point there can be a new ncp which matches the
1021 * search but hashes to a different neglist.
1023 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1029 * No match to begin with.
1031 if (__predict_false(ncp == NULL)) {
1036 * The newly found entry may be something different...
1038 if (!(ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1039 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))) {
1044 * ... and not even negative.
1046 nc_flag = atomic_load_char(&ncp->nc_flag);
1047 if ((nc_flag & NCF_NEGATIVE) == 0) {
1051 if (__predict_false(!cache_ncp_canuse(ncp))) {
1055 cache_neg_promote_locked(ncp);
1056 cache_neg_hit_finish(ncp);
1058 mtx_unlock(&nl->nl_lock);
1062 mtx_unlock(&nl->nl_lock);
1067 cache_neg_promote(struct namecache *ncp)
1071 nl = NCP2NEGLIST(ncp);
1072 mtx_lock(&nl->nl_lock);
1073 cache_neg_promote_locked(ncp);
1074 mtx_unlock(&nl->nl_lock);
1078 cache_neg_insert(struct namecache *ncp)
1082 MPASS(ncp->nc_flag & NCF_NEGATIVE);
1083 cache_assert_bucket_locked(ncp);
1084 nl = NCP2NEGLIST(ncp);
1085 mtx_lock(&nl->nl_lock);
1086 TAILQ_INSERT_TAIL(&nl->nl_list, ncp, nc_dst);
1087 mtx_unlock(&nl->nl_lock);
1088 atomic_add_long(&numneg, 1);
1092 cache_neg_remove(struct namecache *ncp)
1095 struct negstate *ns;
1097 cache_assert_bucket_locked(ncp);
1098 nl = NCP2NEGLIST(ncp);
1099 ns = NCP2NEGSTATE(ncp);
1100 mtx_lock(&nl->nl_lock);
1101 if ((ns->neg_flag & NEG_HOT) != 0) {
1102 TAILQ_REMOVE(&nl->nl_hotlist, ncp, nc_dst);
1105 TAILQ_REMOVE(&nl->nl_list, ncp, nc_dst);
1107 mtx_unlock(&nl->nl_lock);
1108 atomic_subtract_long(&numneg, 1);
1111 static struct neglist *
1112 cache_neg_evict_select_list(void)
1117 c = atomic_fetchadd_int(&neg_cycle, 1) + 1;
1118 nl = &neglists[c % numneglists];
1119 if (!mtx_trylock(&nl->nl_evict_lock)) {
1120 counter_u64_add(neg_evict_skipped_contended, 1);
1126 static struct namecache *
1127 cache_neg_evict_select_entry(struct neglist *nl)
1129 struct namecache *ncp, *lncp;
1130 struct negstate *ns, *lns;
1133 mtx_assert(&nl->nl_evict_lock, MA_OWNED);
1134 mtx_assert(&nl->nl_lock, MA_OWNED);
1135 ncp = TAILQ_FIRST(&nl->nl_list);
1139 lns = NCP2NEGSTATE(lncp);
1140 for (i = 1; i < 4; i++) {
1141 ncp = TAILQ_NEXT(ncp, nc_dst);
1144 ns = NCP2NEGSTATE(ncp);
1145 if (ns->neg_hit < lns->neg_hit) {
1154 cache_neg_evict(void)
1156 struct namecache *ncp, *ncp2;
1158 struct negstate *ns;
1166 nl = cache_neg_evict_select_list();
1171 mtx_lock(&nl->nl_lock);
1172 ncp = TAILQ_FIRST(&nl->nl_hotlist);
1174 cache_neg_demote_locked(ncp);
1176 ncp = cache_neg_evict_select_entry(nl);
1178 counter_u64_add(neg_evict_skipped_empty, 1);
1179 mtx_unlock(&nl->nl_lock);
1180 mtx_unlock(&nl->nl_evict_lock);
1183 ns = NCP2NEGSTATE(ncp);
1184 nlen = ncp->nc_nlen;
1186 hash = cache_get_hash(ncp->nc_name, nlen, dvp);
1187 dvlp = VP2VNODELOCK(dvp);
1188 blp = HASH2BUCKETLOCK(hash);
1189 mtx_unlock(&nl->nl_lock);
1190 mtx_unlock(&nl->nl_evict_lock);
1194 * Note that since all locks were dropped above, the entry may be
1195 * gone or reallocated to be something else.
1197 CK_SLIST_FOREACH(ncp2, (NCHHASH(hash)), nc_hash) {
1198 if (ncp2 == ncp && ncp2->nc_dvp == dvp &&
1199 ncp2->nc_nlen == nlen && (ncp2->nc_flag & NCF_NEGATIVE) != 0)
1203 counter_u64_add(neg_evict_skipped_missed, 1);
1207 MPASS(dvlp == VP2VNODELOCK(ncp->nc_dvp));
1208 MPASS(blp == NCP2BUCKETLOCK(ncp));
1209 SDT_PROBE2(vfs, namecache, evict_negative, done, ncp->nc_dvp,
1211 cache_zap_locked(ncp);
1212 counter_u64_add(neg_evicted, 1);
1223 * Maybe evict a negative entry to create more room.
1225 * The ncnegfactor parameter limits what fraction of the total count
1226 * can comprise of negative entries. However, if the cache is just
1227 * warming up this leads to excessive evictions. As such, ncnegminpct
1228 * (recomputed to neg_min) dictates whether the above should be
1231 * Try evicting if the cache is close to full capacity regardless of
1232 * other considerations.
1235 cache_neg_evict_cond(u_long lnumcache)
1239 if (ncsize - 1000 < lnumcache)
1241 lnumneg = atomic_load_long(&numneg);
1242 if (lnumneg < neg_min)
1244 if (lnumneg * ncnegfactor < lnumcache)
1247 return (cache_neg_evict());
1251 * cache_zap_locked():
1253 * Removes a namecache entry from cache, whether it contains an actual
1254 * pointer to a vnode or if it is just a negative cache entry.
1257 cache_zap_locked(struct namecache *ncp)
1259 struct nchashhead *ncpp;
1261 if (!(ncp->nc_flag & NCF_NEGATIVE))
1262 cache_assert_vnode_locked(ncp->nc_vp);
1263 cache_assert_vnode_locked(ncp->nc_dvp);
1264 cache_assert_bucket_locked(ncp);
1266 cache_ncp_invalidate(ncp);
1268 ncpp = NCP2BUCKET(ncp);
1269 CK_SLIST_REMOVE(ncpp, ncp, namecache, nc_hash);
1270 if (!(ncp->nc_flag & NCF_NEGATIVE)) {
1271 SDT_PROBE3(vfs, namecache, zap, done, ncp->nc_dvp,
1272 ncp->nc_name, ncp->nc_vp);
1273 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
1274 if (ncp == ncp->nc_vp->v_cache_dd) {
1275 vn_seqc_write_begin_unheld(ncp->nc_vp);
1276 ncp->nc_vp->v_cache_dd = NULL;
1277 vn_seqc_write_end(ncp->nc_vp);
1280 SDT_PROBE2(vfs, namecache, zap_negative, done, ncp->nc_dvp,
1282 cache_neg_remove(ncp);
1284 if (ncp->nc_flag & NCF_ISDOTDOT) {
1285 if (ncp == ncp->nc_dvp->v_cache_dd) {
1286 vn_seqc_write_begin_unheld(ncp->nc_dvp);
1287 ncp->nc_dvp->v_cache_dd = NULL;
1288 vn_seqc_write_end(ncp->nc_dvp);
1291 LIST_REMOVE(ncp, nc_src);
1292 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) {
1293 ncp->nc_flag |= NCF_DVDROP;
1294 counter_u64_add(numcachehv, -1);
1297 atomic_subtract_long(&numcache, 1);
1301 cache_zap_negative_locked_vnode_kl(struct namecache *ncp, struct vnode *vp)
1305 MPASS(ncp->nc_dvp == vp);
1306 MPASS(ncp->nc_flag & NCF_NEGATIVE);
1307 cache_assert_vnode_locked(vp);
1309 blp = NCP2BUCKETLOCK(ncp);
1311 cache_zap_locked(ncp);
1316 cache_zap_locked_vnode_kl2(struct namecache *ncp, struct vnode *vp,
1319 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
1322 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
1323 cache_assert_vnode_locked(vp);
1325 if (ncp->nc_flag & NCF_NEGATIVE) {
1326 if (*vlpp != NULL) {
1330 cache_zap_negative_locked_vnode_kl(ncp, vp);
1334 pvlp = VP2VNODELOCK(vp);
1335 blp = NCP2BUCKETLOCK(ncp);
1336 vlp1 = VP2VNODELOCK(ncp->nc_dvp);
1337 vlp2 = VP2VNODELOCK(ncp->nc_vp);
1339 if (*vlpp == vlp1 || *vlpp == vlp2) {
1343 if (*vlpp != NULL) {
1347 cache_sort_vnodes(&vlp1, &vlp2);
1352 if (!mtx_trylock(vlp1))
1358 cache_zap_locked(ncp);
1360 if (to_unlock != NULL)
1361 mtx_unlock(to_unlock);
1368 MPASS(*vlpp == NULL);
1374 * If trylocking failed we can get here. We know enough to take all needed locks
1375 * in the right order and re-lookup the entry.
1378 cache_zap_unlocked_bucket(struct namecache *ncp, struct componentname *cnp,
1379 struct vnode *dvp, struct mtx *dvlp, struct mtx *vlp, uint32_t hash,
1382 struct namecache *rncp;
1384 cache_assert_bucket_unlocked(ncp);
1386 cache_sort_vnodes(&dvlp, &vlp);
1387 cache_lock_vnodes(dvlp, vlp);
1389 CK_SLIST_FOREACH(rncp, (NCHHASH(hash)), nc_hash) {
1390 if (rncp == ncp && rncp->nc_dvp == dvp &&
1391 rncp->nc_nlen == cnp->cn_namelen &&
1392 !bcmp(rncp->nc_name, cnp->cn_nameptr, rncp->nc_nlen))
1396 cache_zap_locked(rncp);
1398 cache_unlock_vnodes(dvlp, vlp);
1399 counter_u64_add(zap_bucket_relock_success, 1);
1404 cache_unlock_vnodes(dvlp, vlp);
1408 static int __noinline
1409 cache_zap_locked_bucket(struct namecache *ncp, struct componentname *cnp,
1410 uint32_t hash, struct mtx *blp)
1412 struct mtx *dvlp, *vlp;
1415 cache_assert_bucket_locked(ncp);
1417 dvlp = VP2VNODELOCK(ncp->nc_dvp);
1419 if (!(ncp->nc_flag & NCF_NEGATIVE))
1420 vlp = VP2VNODELOCK(ncp->nc_vp);
1421 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1422 cache_zap_locked(ncp);
1424 cache_unlock_vnodes(dvlp, vlp);
1430 return (cache_zap_unlocked_bucket(ncp, cnp, dvp, dvlp, vlp, hash, blp));
1433 static __noinline int
1434 cache_remove_cnp(struct vnode *dvp, struct componentname *cnp)
1436 struct namecache *ncp;
1438 struct mtx *dvlp, *dvlp2;
1442 if (cnp->cn_namelen == 2 &&
1443 cnp->cn_nameptr[0] == '.' && cnp->cn_nameptr[1] == '.') {
1444 dvlp = VP2VNODELOCK(dvp);
1448 ncp = dvp->v_cache_dd;
1453 SDT_PROBE2(vfs, namecache, removecnp, miss, dvp, cnp);
1456 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1457 if (!cache_zap_locked_vnode_kl2(ncp, dvp, &dvlp2))
1459 MPASS(dvp->v_cache_dd == NULL);
1465 vn_seqc_write_begin(dvp);
1466 dvp->v_cache_dd = NULL;
1467 vn_seqc_write_end(dvp);
1472 SDT_PROBE2(vfs, namecache, removecnp, hit, dvp, cnp);
1476 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1477 blp = HASH2BUCKETLOCK(hash);
1479 if (CK_SLIST_EMPTY(NCHHASH(hash)))
1484 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1485 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1486 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1495 error = cache_zap_locked_bucket(ncp, cnp, hash, blp);
1496 if (__predict_false(error != 0)) {
1500 counter_u64_add(numposzaps, 1);
1501 SDT_PROBE2(vfs, namecache, removecnp, hit, dvp, cnp);
1505 counter_u64_add(nummisszap, 1);
1506 SDT_PROBE2(vfs, namecache, removecnp, miss, dvp, cnp);
1510 static int __noinline
1511 cache_lookup_dot(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1512 struct timespec *tsp, int *ticksp)
1517 counter_u64_add(dothits, 1);
1518 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", *vpp);
1525 * When we lookup "." we still can be asked to lock it
1528 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
1529 if (ltype != VOP_ISLOCKED(*vpp)) {
1530 if (ltype == LK_EXCLUSIVE) {
1531 vn_lock(*vpp, LK_UPGRADE | LK_RETRY);
1532 if (VN_IS_DOOMED((*vpp))) {
1533 /* forced unmount */
1539 vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY);
1544 static int __noinline
1545 cache_lookup_dotdot(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1546 struct timespec *tsp, int *ticksp)
1548 struct namecache_ts *ncp_ts;
1549 struct namecache *ncp;
1555 MPASS((cnp->cn_flags & ISDOTDOT) != 0);
1557 if ((cnp->cn_flags & MAKEENTRY) == 0) {
1558 cache_remove_cnp(dvp, cnp);
1562 counter_u64_add(dotdothits, 1);
1564 dvlp = VP2VNODELOCK(dvp);
1566 ncp = dvp->v_cache_dd;
1568 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, "..", NULL);
1572 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1573 if (ncp->nc_flag & NCF_NEGATIVE)
1580 goto negative_success;
1581 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, "..", *vpp);
1582 cache_out_ts(ncp, tsp, ticksp);
1583 if ((ncp->nc_flag & (NCF_ISDOTDOT | NCF_DTS)) ==
1584 NCF_DTS && tsp != NULL) {
1585 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1586 *tsp = ncp_ts->nc_dotdottime;
1590 ltype = VOP_ISLOCKED(dvp);
1592 vs = vget_prep(*vpp);
1594 error = vget_finish(*vpp, cnp->cn_lkflags, vs);
1595 vn_lock(dvp, ltype | LK_RETRY);
1596 if (VN_IS_DOOMED(dvp)) {
1608 if (__predict_false(cnp->cn_nameiop == CREATE)) {
1609 if (cnp->cn_flags & ISLASTCN) {
1610 counter_u64_add(numnegzaps, 1);
1611 cache_zap_negative_locked_vnode_kl(ncp, dvp);
1618 whiteout = (ncp->nc_flag & NCF_WHITE);
1619 cache_out_ts(ncp, tsp, ticksp);
1620 if (cache_neg_hit_prep(ncp))
1621 cache_neg_promote(ncp);
1623 cache_neg_hit_finish(ncp);
1626 cnp->cn_flags |= ISWHITEOUT;
1631 * Lookup a name in the name cache
1635 * - dvp: Parent directory in which to search.
1636 * - vpp: Return argument. Will contain desired vnode on cache hit.
1637 * - cnp: Parameters of the name search. The most interesting bits of
1638 * the cn_flags field have the following meanings:
1639 * - MAKEENTRY: If clear, free an entry from the cache rather than look
1641 * - ISDOTDOT: Must be set if and only if cn_nameptr == ".."
1642 * - tsp: Return storage for cache timestamp. On a successful (positive
1643 * or negative) lookup, tsp will be filled with any timespec that
1644 * was stored when this cache entry was created. However, it will
1645 * be clear for "." entries.
1646 * - ticks: Return storage for alternate cache timestamp. On a successful
1647 * (positive or negative) lookup, it will contain the ticks value
1648 * that was current when the cache entry was created, unless cnp
1651 * Either both tsp and ticks have to be provided or neither of them.
1655 * - -1: A positive cache hit. vpp will contain the desired vnode.
1656 * - ENOENT: A negative cache hit, or dvp was recycled out from under us due
1657 * to a forced unmount. vpp will not be modified. If the entry
1658 * is a whiteout, then the ISWHITEOUT flag will be set in
1660 * - 0: A cache miss. vpp will not be modified.
1664 * On a cache hit, vpp will be returned locked and ref'd. If we're looking up
1665 * .., dvp is unlocked. If we're looking up . an extra ref is taken, but the
1666 * lock is not recursively acquired.
1668 static int __noinline
1669 cache_lookup_fallback(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1670 struct timespec *tsp, int *ticksp)
1672 struct namecache *ncp;
1679 MPASS((cnp->cn_flags & (MAKEENTRY | ISDOTDOT)) == MAKEENTRY);
1682 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1683 blp = HASH2BUCKETLOCK(hash);
1686 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1687 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1688 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1692 if (__predict_false(ncp == NULL)) {
1694 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
1696 counter_u64_add(nummiss, 1);
1700 if (ncp->nc_flag & NCF_NEGATIVE)
1701 goto negative_success;
1703 counter_u64_add(numposhits, 1);
1705 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name, *vpp);
1706 cache_out_ts(ncp, tsp, ticksp);
1708 vs = vget_prep(*vpp);
1710 error = vget_finish(*vpp, cnp->cn_lkflags, vs);
1717 if (__predict_false(cnp->cn_nameiop == CREATE)) {
1718 if (cnp->cn_flags & ISLASTCN) {
1719 counter_u64_add(numnegzaps, 1);
1720 error = cache_zap_locked_bucket(ncp, cnp, hash, blp);
1721 if (__predict_false(error != 0)) {
1730 whiteout = (ncp->nc_flag & NCF_WHITE);
1731 cache_out_ts(ncp, tsp, ticksp);
1732 if (cache_neg_hit_prep(ncp))
1733 cache_neg_promote(ncp);
1735 cache_neg_hit_finish(ncp);
1738 cnp->cn_flags |= ISWHITEOUT;
1743 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1744 struct timespec *tsp, int *ticksp)
1746 struct namecache *ncp;
1750 bool whiteout, neg_promote;
1753 MPASS((tsp == NULL && ticksp == NULL) || (tsp != NULL && ticksp != NULL));
1756 if (__predict_false(!doingcache)) {
1757 cnp->cn_flags &= ~MAKEENTRY;
1762 if (__predict_false(cnp->cn_nameptr[0] == '.')) {
1763 if (cnp->cn_namelen == 1)
1764 return (cache_lookup_dot(dvp, vpp, cnp, tsp, ticksp));
1765 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.')
1766 return (cache_lookup_dotdot(dvp, vpp, cnp, tsp, ticksp));
1769 MPASS((cnp->cn_flags & ISDOTDOT) == 0);
1771 if ((cnp->cn_flags & MAKEENTRY) == 0) {
1772 cache_remove_cnp(dvp, cnp);
1776 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1779 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1780 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1781 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1785 if (__predict_false(ncp == NULL)) {
1787 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
1789 counter_u64_add(nummiss, 1);
1793 nc_flag = atomic_load_char(&ncp->nc_flag);
1794 if (nc_flag & NCF_NEGATIVE)
1795 goto negative_success;
1797 counter_u64_add(numposhits, 1);
1799 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name, *vpp);
1800 cache_out_ts(ncp, tsp, ticksp);
1802 if (!cache_ncp_canuse(ncp)) {
1807 vs = vget_prep_smr(*vpp);
1809 if (__predict_false(vs == VGET_NONE)) {
1813 error = vget_finish(*vpp, cnp->cn_lkflags, vs);
1820 if (__predict_false(cnp->cn_nameiop == CREATE)) {
1821 if (cnp->cn_flags & ISLASTCN) {
1827 cache_out_ts(ncp, tsp, ticksp);
1828 whiteout = (ncp->nc_flag & NCF_WHITE);
1829 neg_promote = cache_neg_hit_prep(ncp);
1830 if (__predict_false(!cache_ncp_canuse(ncp))) {
1831 cache_neg_hit_abort(ncp);
1837 if (!cache_neg_promote_cond(dvp, cnp, ncp, hash))
1840 cache_neg_hit_finish(ncp);
1844 cnp->cn_flags |= ISWHITEOUT;
1847 return (cache_lookup_fallback(dvp, vpp, cnp, tsp, ticksp));
1850 struct celockstate {
1854 CTASSERT((nitems(((struct celockstate *)0)->vlp) == 3));
1855 CTASSERT((nitems(((struct celockstate *)0)->blp) == 2));
1858 cache_celockstate_init(struct celockstate *cel)
1861 bzero(cel, sizeof(*cel));
1865 cache_lock_vnodes_cel(struct celockstate *cel, struct vnode *vp,
1868 struct mtx *vlp1, *vlp2;
1870 MPASS(cel->vlp[0] == NULL);
1871 MPASS(cel->vlp[1] == NULL);
1872 MPASS(cel->vlp[2] == NULL);
1874 MPASS(vp != NULL || dvp != NULL);
1876 vlp1 = VP2VNODELOCK(vp);
1877 vlp2 = VP2VNODELOCK(dvp);
1878 cache_sort_vnodes(&vlp1, &vlp2);
1889 cache_unlock_vnodes_cel(struct celockstate *cel)
1892 MPASS(cel->vlp[0] != NULL || cel->vlp[1] != NULL);
1894 if (cel->vlp[0] != NULL)
1895 mtx_unlock(cel->vlp[0]);
1896 if (cel->vlp[1] != NULL)
1897 mtx_unlock(cel->vlp[1]);
1898 if (cel->vlp[2] != NULL)
1899 mtx_unlock(cel->vlp[2]);
1903 cache_lock_vnodes_cel_3(struct celockstate *cel, struct vnode *vp)
1908 cache_assert_vlp_locked(cel->vlp[0]);
1909 cache_assert_vlp_locked(cel->vlp[1]);
1910 MPASS(cel->vlp[2] == NULL);
1913 vlp = VP2VNODELOCK(vp);
1916 if (vlp >= cel->vlp[1]) {
1919 if (mtx_trylock(vlp))
1921 cache_lock_vnodes_cel_3_failures++;
1922 cache_unlock_vnodes_cel(cel);
1923 if (vlp < cel->vlp[0]) {
1925 mtx_lock(cel->vlp[0]);
1926 mtx_lock(cel->vlp[1]);
1928 if (cel->vlp[0] != NULL)
1929 mtx_lock(cel->vlp[0]);
1931 mtx_lock(cel->vlp[1]);
1941 cache_lock_buckets_cel(struct celockstate *cel, struct mtx *blp1,
1945 MPASS(cel->blp[0] == NULL);
1946 MPASS(cel->blp[1] == NULL);
1948 cache_sort_vnodes(&blp1, &blp2);
1959 cache_unlock_buckets_cel(struct celockstate *cel)
1962 if (cel->blp[0] != NULL)
1963 mtx_unlock(cel->blp[0]);
1964 mtx_unlock(cel->blp[1]);
1968 * Lock part of the cache affected by the insertion.
1970 * This means vnodelocks for dvp, vp and the relevant bucketlock.
1971 * However, insertion can result in removal of an old entry. In this
1972 * case we have an additional vnode and bucketlock pair to lock.
1974 * That is, in the worst case we have to lock 3 vnodes and 2 bucketlocks, while
1975 * preserving the locking order (smaller address first).
1978 cache_enter_lock(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1981 struct namecache *ncp;
1982 struct mtx *blps[2];
1984 blps[0] = HASH2BUCKETLOCK(hash);
1987 cache_lock_vnodes_cel(cel, dvp, vp);
1988 if (vp == NULL || vp->v_type != VDIR)
1990 ncp = vp->v_cache_dd;
1993 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1995 MPASS(ncp->nc_dvp == vp);
1996 blps[1] = NCP2BUCKETLOCK(ncp);
1997 if (ncp->nc_flag & NCF_NEGATIVE)
1999 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
2002 * All vnodes got re-locked. Re-validate the state and if
2003 * nothing changed we are done. Otherwise restart.
2005 if (ncp == vp->v_cache_dd &&
2006 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
2007 blps[1] == NCP2BUCKETLOCK(ncp) &&
2008 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
2010 cache_unlock_vnodes_cel(cel);
2015 cache_lock_buckets_cel(cel, blps[0], blps[1]);
2019 cache_enter_lock_dd(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
2022 struct namecache *ncp;
2023 struct mtx *blps[2];
2025 blps[0] = HASH2BUCKETLOCK(hash);
2028 cache_lock_vnodes_cel(cel, dvp, vp);
2029 ncp = dvp->v_cache_dd;
2032 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2034 MPASS(ncp->nc_dvp == dvp);
2035 blps[1] = NCP2BUCKETLOCK(ncp);
2036 if (ncp->nc_flag & NCF_NEGATIVE)
2038 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
2040 if (ncp == dvp->v_cache_dd &&
2041 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
2042 blps[1] == NCP2BUCKETLOCK(ncp) &&
2043 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
2045 cache_unlock_vnodes_cel(cel);
2050 cache_lock_buckets_cel(cel, blps[0], blps[1]);
2054 cache_enter_unlock(struct celockstate *cel)
2057 cache_unlock_buckets_cel(cel);
2058 cache_unlock_vnodes_cel(cel);
2061 static void __noinline
2062 cache_enter_dotdot_prep(struct vnode *dvp, struct vnode *vp,
2063 struct componentname *cnp)
2065 struct celockstate cel;
2066 struct namecache *ncp;
2070 if (dvp->v_cache_dd == NULL)
2072 len = cnp->cn_namelen;
2073 cache_celockstate_init(&cel);
2074 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
2075 cache_enter_lock_dd(&cel, dvp, vp, hash);
2076 vn_seqc_write_begin(dvp);
2077 ncp = dvp->v_cache_dd;
2078 if (ncp != NULL && (ncp->nc_flag & NCF_ISDOTDOT)) {
2079 KASSERT(ncp->nc_dvp == dvp, ("wrong isdotdot parent"));
2080 cache_zap_locked(ncp);
2084 dvp->v_cache_dd = NULL;
2085 vn_seqc_write_end(dvp);
2086 cache_enter_unlock(&cel);
2092 * Add an entry to the cache.
2095 cache_enter_time(struct vnode *dvp, struct vnode *vp, struct componentname *cnp,
2096 struct timespec *tsp, struct timespec *dtsp)
2098 struct celockstate cel;
2099 struct namecache *ncp, *n2, *ndd;
2100 struct namecache_ts *ncp_ts;
2101 struct nchashhead *ncpp;
2107 VNPASS(!VN_IS_DOOMED(dvp), dvp);
2108 VNPASS(dvp->v_type != VNON, dvp);
2110 VNPASS(!VN_IS_DOOMED(vp), vp);
2111 VNPASS(vp->v_type != VNON, vp);
2115 if (__predict_false(!doingcache))
2120 if (__predict_false(cnp->cn_nameptr[0] == '.')) {
2121 if (cnp->cn_namelen == 1)
2123 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
2124 cache_enter_dotdot_prep(dvp, vp, cnp);
2125 flag = NCF_ISDOTDOT;
2130 * Avoid blowout in namecache entries.
2133 * 1. filesystems may end up tryng to add an already existing entry
2134 * (for example this can happen after a cache miss during concurrent
2135 * lookup), in which case we will call cache_neg_evict despite not
2137 * 2. the routine may fail to free anything and no provisions are made
2138 * to make it try harder (see the inside for failure modes)
2139 * 3. it only ever looks at negative entries.
2141 lnumcache = atomic_fetchadd_long(&numcache, 1) + 1;
2142 if (cache_neg_evict_cond(lnumcache)) {
2143 lnumcache = atomic_load_long(&numcache);
2145 if (__predict_false(lnumcache >= ncsize)) {
2146 atomic_subtract_long(&numcache, 1);
2147 counter_u64_add(numdrops, 1);
2151 cache_celockstate_init(&cel);
2156 * Calculate the hash key and setup as much of the new
2157 * namecache entry as possible before acquiring the lock.
2159 ncp = cache_alloc(cnp->cn_namelen, tsp != NULL);
2160 ncp->nc_flag = flag | NCF_WIP;
2163 cache_neg_init(ncp);
2166 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
2167 ncp_ts->nc_time = *tsp;
2168 ncp_ts->nc_ticks = ticks;
2169 ncp_ts->nc_nc.nc_flag |= NCF_TS;
2171 ncp_ts->nc_dotdottime = *dtsp;
2172 ncp_ts->nc_nc.nc_flag |= NCF_DTS;
2175 len = ncp->nc_nlen = cnp->cn_namelen;
2176 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
2177 memcpy(ncp->nc_name, cnp->cn_nameptr, len);
2178 ncp->nc_name[len] = '\0';
2179 cache_enter_lock(&cel, dvp, vp, hash);
2182 * See if this vnode or negative entry is already in the cache
2183 * with this name. This can happen with concurrent lookups of
2184 * the same path name.
2186 ncpp = NCHHASH(hash);
2187 CK_SLIST_FOREACH(n2, ncpp, nc_hash) {
2188 if (n2->nc_dvp == dvp &&
2189 n2->nc_nlen == cnp->cn_namelen &&
2190 !bcmp(n2->nc_name, cnp->cn_nameptr, n2->nc_nlen)) {
2191 MPASS(cache_ncp_canuse(n2));
2192 if ((n2->nc_flag & NCF_NEGATIVE) != 0)
2194 ("%s: found entry pointing to a different vnode (%p != %p)",
2195 __func__, NULL, vp));
2197 KASSERT(n2->nc_vp == vp,
2198 ("%s: found entry pointing to a different vnode (%p != %p)",
2199 __func__, n2->nc_vp, vp));
2201 * Entries are supposed to be immutable unless in the
2202 * process of getting destroyed. Accommodating for
2203 * changing timestamps is possible but not worth it.
2204 * This should be harmless in terms of correctness, in
2205 * the worst case resulting in an earlier expiration.
2206 * Alternatively, the found entry can be replaced
2209 MPASS((n2->nc_flag & (NCF_TS | NCF_DTS)) == (ncp->nc_flag & (NCF_TS | NCF_DTS)));
2212 KASSERT((n2->nc_flag & NCF_TS) != 0,
2214 n2_ts = __containerof(n2, struct namecache_ts, nc_nc);
2215 n2_ts->nc_time = ncp_ts->nc_time;
2216 n2_ts->nc_ticks = ncp_ts->nc_ticks;
2218 n2_ts->nc_dotdottime = ncp_ts->nc_dotdottime;
2219 n2_ts->nc_nc.nc_flag |= NCF_DTS;
2223 SDT_PROBE3(vfs, namecache, enter, duplicate, dvp, ncp->nc_name,
2225 goto out_unlock_free;
2229 if (flag == NCF_ISDOTDOT) {
2231 * See if we are trying to add .. entry, but some other lookup
2232 * has populated v_cache_dd pointer already.
2234 if (dvp->v_cache_dd != NULL)
2235 goto out_unlock_free;
2236 KASSERT(vp == NULL || vp->v_type == VDIR,
2237 ("wrong vnode type %p", vp));
2238 vn_seqc_write_begin(dvp);
2239 dvp->v_cache_dd = ncp;
2240 vn_seqc_write_end(dvp);
2244 if (flag != NCF_ISDOTDOT) {
2246 * For this case, the cache entry maps both the
2247 * directory name in it and the name ".." for the
2248 * directory's parent.
2250 vn_seqc_write_begin(vp);
2251 if ((ndd = vp->v_cache_dd) != NULL) {
2252 if ((ndd->nc_flag & NCF_ISDOTDOT) != 0)
2253 cache_zap_locked(ndd);
2257 vp->v_cache_dd = ncp;
2258 vn_seqc_write_end(vp);
2259 } else if (vp->v_type != VDIR) {
2260 if (vp->v_cache_dd != NULL) {
2261 vn_seqc_write_begin(vp);
2262 vp->v_cache_dd = NULL;
2263 vn_seqc_write_end(vp);
2268 if (flag != NCF_ISDOTDOT) {
2269 if (LIST_EMPTY(&dvp->v_cache_src)) {
2271 counter_u64_add(numcachehv, 1);
2273 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
2277 * If the entry is "negative", we place it into the
2278 * "negative" cache queue, otherwise, we place it into the
2279 * destination vnode's cache entries queue.
2282 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
2283 SDT_PROBE3(vfs, namecache, enter, done, dvp, ncp->nc_name,
2286 if (cnp->cn_flags & ISWHITEOUT)
2287 ncp->nc_flag |= NCF_WHITE;
2288 cache_neg_insert(ncp);
2289 SDT_PROBE2(vfs, namecache, enter_negative, done, dvp,
2294 * Insert the new namecache entry into the appropriate chain
2295 * within the cache entries table.
2297 CK_SLIST_INSERT_HEAD(ncpp, ncp, nc_hash);
2299 atomic_thread_fence_rel();
2301 * Mark the entry as fully constructed.
2302 * It is immutable past this point until its removal.
2304 atomic_store_char(&ncp->nc_flag, ncp->nc_flag & ~NCF_WIP);
2306 cache_enter_unlock(&cel);
2311 cache_enter_unlock(&cel);
2312 atomic_subtract_long(&numcache, 1);
2318 cache_roundup_2(u_int val)
2322 for (res = 1; res <= val; res <<= 1)
2328 static struct nchashhead *
2329 nchinittbl(u_long elements, u_long *hashmask)
2331 struct nchashhead *hashtbl;
2334 hashsize = cache_roundup_2(elements) / 2;
2336 hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), M_VFSCACHE, M_WAITOK);
2337 for (i = 0; i < hashsize; i++)
2338 CK_SLIST_INIT(&hashtbl[i]);
2339 *hashmask = hashsize - 1;
2344 ncfreetbl(struct nchashhead *hashtbl)
2347 free(hashtbl, M_VFSCACHE);
2351 * Name cache initialization, from vfs_init() when we are booting
2354 nchinit(void *dummy __unused)
2358 cache_zone_small = uma_zcreate("S VFS Cache", CACHE_ZONE_SMALL_SIZE,
2359 NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT, UMA_ZONE_ZINIT);
2360 cache_zone_small_ts = uma_zcreate("STS VFS Cache", CACHE_ZONE_SMALL_TS_SIZE,
2361 NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT, UMA_ZONE_ZINIT);
2362 cache_zone_large = uma_zcreate("L VFS Cache", CACHE_ZONE_LARGE_SIZE,
2363 NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT, UMA_ZONE_ZINIT);
2364 cache_zone_large_ts = uma_zcreate("LTS VFS Cache", CACHE_ZONE_LARGE_TS_SIZE,
2365 NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT, UMA_ZONE_ZINIT);
2367 VFS_SMR_ZONE_SET(cache_zone_small);
2368 VFS_SMR_ZONE_SET(cache_zone_small_ts);
2369 VFS_SMR_ZONE_SET(cache_zone_large);
2370 VFS_SMR_ZONE_SET(cache_zone_large_ts);
2372 ncsize = desiredvnodes * ncsizefactor;
2373 cache_recalc_neg_min(ncnegminpct);
2374 nchashtbl = nchinittbl(desiredvnodes * 2, &nchash);
2375 ncbuckethash = cache_roundup_2(mp_ncpus * mp_ncpus) - 1;
2376 if (ncbuckethash < 7) /* arbitrarily chosen to avoid having one lock */
2378 if (ncbuckethash > nchash)
2379 ncbuckethash = nchash;
2380 bucketlocks = malloc(sizeof(*bucketlocks) * numbucketlocks, M_VFSCACHE,
2382 for (i = 0; i < numbucketlocks; i++)
2383 mtx_init(&bucketlocks[i], "ncbuc", NULL, MTX_DUPOK | MTX_RECURSE);
2384 ncvnodehash = ncbuckethash;
2385 vnodelocks = malloc(sizeof(*vnodelocks) * numvnodelocks, M_VFSCACHE,
2387 for (i = 0; i < numvnodelocks; i++)
2388 mtx_init(&vnodelocks[i], "ncvn", NULL, MTX_DUPOK | MTX_RECURSE);
2390 for (i = 0; i < numneglists; i++) {
2391 mtx_init(&neglists[i].nl_evict_lock, "ncnege", NULL, MTX_DEF);
2392 mtx_init(&neglists[i].nl_lock, "ncnegl", NULL, MTX_DEF);
2393 TAILQ_INIT(&neglists[i].nl_list);
2394 TAILQ_INIT(&neglists[i].nl_hotlist);
2397 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL);
2400 cache_vnode_init(struct vnode *vp)
2403 LIST_INIT(&vp->v_cache_src);
2404 TAILQ_INIT(&vp->v_cache_dst);
2405 vp->v_cache_dd = NULL;
2410 cache_changesize(u_long newmaxvnodes)
2412 struct nchashhead *new_nchashtbl, *old_nchashtbl;
2413 u_long new_nchash, old_nchash;
2414 struct namecache *ncp;
2419 newncsize = newmaxvnodes * ncsizefactor;
2420 newmaxvnodes = cache_roundup_2(newmaxvnodes * 2);
2421 if (newmaxvnodes < numbucketlocks)
2422 newmaxvnodes = numbucketlocks;
2424 new_nchashtbl = nchinittbl(newmaxvnodes, &new_nchash);
2425 /* If same hash table size, nothing to do */
2426 if (nchash == new_nchash) {
2427 ncfreetbl(new_nchashtbl);
2431 * Move everything from the old hash table to the new table.
2432 * None of the namecache entries in the table can be removed
2433 * because to do so, they have to be removed from the hash table.
2435 cache_lock_all_vnodes();
2436 cache_lock_all_buckets();
2437 old_nchashtbl = nchashtbl;
2438 old_nchash = nchash;
2439 nchashtbl = new_nchashtbl;
2440 nchash = new_nchash;
2441 for (i = 0; i <= old_nchash; i++) {
2442 while ((ncp = CK_SLIST_FIRST(&old_nchashtbl[i])) != NULL) {
2443 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen,
2445 CK_SLIST_REMOVE(&old_nchashtbl[i], ncp, namecache, nc_hash);
2446 CK_SLIST_INSERT_HEAD(NCHHASH(hash), ncp, nc_hash);
2450 cache_recalc_neg_min(ncnegminpct);
2451 cache_unlock_all_buckets();
2452 cache_unlock_all_vnodes();
2453 ncfreetbl(old_nchashtbl);
2457 * Invalidate all entries from and to a particular vnode.
2460 cache_purge_impl(struct vnode *vp)
2462 TAILQ_HEAD(, namecache) ncps;
2463 struct namecache *ncp, *nnp;
2464 struct mtx *vlp, *vlp2;
2467 vlp = VP2VNODELOCK(vp);
2471 while (!LIST_EMPTY(&vp->v_cache_src)) {
2472 ncp = LIST_FIRST(&vp->v_cache_src);
2473 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2475 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2477 while (!TAILQ_EMPTY(&vp->v_cache_dst)) {
2478 ncp = TAILQ_FIRST(&vp->v_cache_dst);
2479 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2481 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2483 ncp = vp->v_cache_dd;
2485 KASSERT(ncp->nc_flag & NCF_ISDOTDOT,
2486 ("lost dotdot link"));
2487 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2489 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2491 KASSERT(vp->v_cache_dd == NULL, ("incomplete purge"));
2495 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2501 * Opportunistic check to see if there is anything to do.
2504 cache_has_entries(struct vnode *vp)
2507 if (LIST_EMPTY(&vp->v_cache_src) && TAILQ_EMPTY(&vp->v_cache_dst) &&
2508 vp->v_cache_dd == NULL)
2514 cache_purge(struct vnode *vp)
2517 SDT_PROBE1(vfs, namecache, purge, done, vp);
2518 if (!cache_has_entries(vp))
2520 cache_purge_impl(vp);
2524 * Only to be used by vgone.
2527 cache_purge_vgone(struct vnode *vp)
2531 VNPASS(VN_IS_DOOMED(vp), vp);
2532 if (cache_has_entries(vp)) {
2533 cache_purge_impl(vp);
2538 * Serialize against a potential thread doing cache_purge.
2540 vlp = VP2VNODELOCK(vp);
2541 mtx_wait_unlocked(vlp);
2542 if (cache_has_entries(vp)) {
2543 cache_purge_impl(vp);
2550 * Invalidate all negative entries for a particular directory vnode.
2553 cache_purge_negative(struct vnode *vp)
2555 TAILQ_HEAD(, namecache) ncps;
2556 struct namecache *ncp, *nnp;
2559 SDT_PROBE1(vfs, namecache, purge_negative, done, vp);
2560 if (LIST_EMPTY(&vp->v_cache_src))
2563 vlp = VP2VNODELOCK(vp);
2565 LIST_FOREACH_SAFE(ncp, &vp->v_cache_src, nc_src, nnp) {
2566 if (!(ncp->nc_flag & NCF_NEGATIVE))
2568 cache_zap_negative_locked_vnode_kl(ncp, vp);
2569 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2572 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2578 cache_rename(struct vnode *fdvp, struct vnode *fvp, struct vnode *tdvp,
2579 struct vnode *tvp, struct componentname *fcnp, struct componentname *tcnp)
2582 ASSERT_VOP_IN_SEQC(fdvp);
2583 ASSERT_VOP_IN_SEQC(fvp);
2584 ASSERT_VOP_IN_SEQC(tdvp);
2586 ASSERT_VOP_IN_SEQC(tvp);
2591 KASSERT(!cache_remove_cnp(tdvp, tcnp),
2592 ("%s: lingering negative entry", __func__));
2594 cache_remove_cnp(tdvp, tcnp);
2599 * Flush all entries referencing a particular filesystem.
2602 cache_purgevfs(struct mount *mp)
2604 struct vnode *vp, *mvp;
2606 SDT_PROBE1(vfs, namecache, purgevfs, done, mp);
2608 * Somewhat wasteful iteration over all vnodes. Would be better to
2609 * support filtering and avoid the interlock to begin with.
2611 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
2612 if (!cache_has_entries(vp)) {
2624 * Perform canonical checks and cache lookup and pass on to filesystem
2625 * through the vop_cachedlookup only if needed.
2629 vfs_cache_lookup(struct vop_lookup_args *ap)
2633 struct vnode **vpp = ap->a_vpp;
2634 struct componentname *cnp = ap->a_cnp;
2635 int flags = cnp->cn_flags;
2640 if (dvp->v_type != VDIR)
2643 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
2644 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
2647 error = vn_dir_check_exec(dvp, cnp);
2651 error = cache_lookup(dvp, vpp, cnp, NULL, NULL);
2653 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
2659 /* Implementation of the getcwd syscall. */
2661 sys___getcwd(struct thread *td, struct __getcwd_args *uap)
2667 buflen = uap->buflen;
2668 if (__predict_false(buflen < 2))
2670 if (buflen > MAXPATHLEN)
2671 buflen = MAXPATHLEN;
2673 buf = uma_zalloc(namei_zone, M_WAITOK);
2674 error = vn_getcwd(buf, &retbuf, &buflen);
2676 error = copyout(retbuf, uap->buf, buflen);
2677 uma_zfree(namei_zone, buf);
2682 vn_getcwd(char *buf, char **retbuf, size_t *buflen)
2688 pwd = pwd_get_smr();
2689 error = vn_fullpath_any_smr(pwd->pwd_cdir, pwd->pwd_rdir, buf, retbuf,
2691 VFS_SMR_ASSERT_NOT_ENTERED();
2693 pwd = pwd_hold(curthread);
2694 error = vn_fullpath_any(pwd->pwd_cdir, pwd->pwd_rdir, buf,
2700 if (KTRPOINT(curthread, KTR_NAMEI) && error == 0)
2707 kern___realpathat(struct thread *td, int fd, const char *path, char *buf,
2708 size_t size, int flags, enum uio_seg pathseg)
2710 struct nameidata nd;
2711 char *retbuf, *freebuf;
2716 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | SAVENAME | WANTPARENT | AUDITVNODE1,
2717 pathseg, path, fd, &cap_fstat_rights, td);
2718 if ((error = namei(&nd)) != 0)
2720 error = vn_fullpath_hardlink(&nd, &retbuf, &freebuf, &size);
2722 error = copyout(retbuf, buf, size);
2723 free(freebuf, M_TEMP);
2730 sys___realpathat(struct thread *td, struct __realpathat_args *uap)
2733 return (kern___realpathat(td, uap->fd, uap->path, uap->buf, uap->size,
2734 uap->flags, UIO_USERSPACE));
2738 * Retrieve the full filesystem path that correspond to a vnode from the name
2739 * cache (if available)
2742 vn_fullpath(struct vnode *vp, char **retbuf, char **freebuf)
2749 if (__predict_false(vp == NULL))
2752 buflen = MAXPATHLEN;
2753 buf = malloc(buflen, M_TEMP, M_WAITOK);
2755 pwd = pwd_get_smr();
2756 error = vn_fullpath_any_smr(vp, pwd->pwd_rdir, buf, retbuf, &buflen, false, 0);
2757 VFS_SMR_ASSERT_NOT_ENTERED();
2759 pwd = pwd_hold(curthread);
2760 error = vn_fullpath_any(vp, pwd->pwd_rdir, buf, retbuf, &buflen);
2771 * This function is similar to vn_fullpath, but it attempts to lookup the
2772 * pathname relative to the global root mount point. This is required for the
2773 * auditing sub-system, as audited pathnames must be absolute, relative to the
2774 * global root mount point.
2777 vn_fullpath_global(struct vnode *vp, char **retbuf, char **freebuf)
2783 if (__predict_false(vp == NULL))
2785 buflen = MAXPATHLEN;
2786 buf = malloc(buflen, M_TEMP, M_WAITOK);
2788 error = vn_fullpath_any_smr(vp, rootvnode, buf, retbuf, &buflen, false, 0);
2789 VFS_SMR_ASSERT_NOT_ENTERED();
2791 error = vn_fullpath_any(vp, rootvnode, buf, retbuf, &buflen);
2800 static struct namecache *
2801 vn_dd_from_dst(struct vnode *vp)
2803 struct namecache *ncp;
2805 cache_assert_vnode_locked(vp);
2806 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst) {
2807 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2814 vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, size_t *buflen)
2817 struct namecache *ncp;
2821 vlp = VP2VNODELOCK(*vp);
2823 ncp = (*vp)->v_cache_dd;
2824 if (ncp != NULL && (ncp->nc_flag & NCF_ISDOTDOT) == 0) {
2825 KASSERT(ncp == vn_dd_from_dst(*vp),
2826 ("%s: mismatch for dd entry (%p != %p)", __func__,
2827 ncp, vn_dd_from_dst(*vp)));
2829 ncp = vn_dd_from_dst(*vp);
2832 if (*buflen < ncp->nc_nlen) {
2835 counter_u64_add(numfullpathfail4, 1);
2837 SDT_PROBE3(vfs, namecache, fullpath, return, error,
2841 *buflen -= ncp->nc_nlen;
2842 memcpy(buf + *buflen, ncp->nc_name, ncp->nc_nlen);
2843 SDT_PROBE3(vfs, namecache, fullpath, hit, ncp->nc_dvp,
2852 SDT_PROBE1(vfs, namecache, fullpath, miss, vp);
2855 vn_lock(*vp, LK_SHARED | LK_RETRY);
2856 error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen);
2859 counter_u64_add(numfullpathfail2, 1);
2860 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2865 if (VN_IS_DOOMED(dvp)) {
2866 /* forced unmount */
2869 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2873 * *vp has its use count incremented still.
2880 * Resolve a directory to a pathname.
2882 * The name of the directory can always be found in the namecache or fetched
2883 * from the filesystem. There is also guaranteed to be only one parent, meaning
2884 * we can just follow vnodes up until we find the root.
2886 * The vnode must be referenced.
2889 vn_fullpath_dir(struct vnode *vp, struct vnode *rdir, char *buf, char **retbuf,
2890 size_t *len, bool slash_prefixed, size_t addend)
2892 #ifdef KDTRACE_HOOKS
2893 struct vnode *startvp = vp;
2899 VNPASS(vp->v_type == VDIR || VN_IS_DOOMED(vp), vp);
2900 VNPASS(vp->v_usecount > 0, vp);
2904 if (!slash_prefixed) {
2912 SDT_PROBE1(vfs, namecache, fullpath, entry, vp);
2913 counter_u64_add(numfullpathcalls, 1);
2914 while (vp != rdir && vp != rootvnode) {
2916 * The vp vnode must be already fully constructed,
2917 * since it is either found in namecache or obtained
2918 * from VOP_VPTOCNP(). We may test for VV_ROOT safely
2919 * without obtaining the vnode lock.
2921 if ((vp->v_vflag & VV_ROOT) != 0) {
2922 vn_lock(vp, LK_RETRY | LK_SHARED);
2925 * With the vnode locked, check for races with
2926 * unmount, forced or not. Note that we
2927 * already verified that vp is not equal to
2928 * the root vnode, which means that
2929 * mnt_vnodecovered can be NULL only for the
2932 if (VN_IS_DOOMED(vp) ||
2933 (vp1 = vp->v_mount->mnt_vnodecovered) == NULL ||
2934 vp1->v_mountedhere != vp->v_mount) {
2937 SDT_PROBE3(vfs, namecache, fullpath, return,
2947 if (vp->v_type != VDIR) {
2949 counter_u64_add(numfullpathfail1, 1);
2951 SDT_PROBE3(vfs, namecache, fullpath, return,
2955 error = vn_vptocnp(&vp, curthread->td_ucred, buf, &buflen);
2961 SDT_PROBE3(vfs, namecache, fullpath, return, error,
2965 buf[--buflen] = '/';
2966 slash_prefixed = true;
2970 if (!slash_prefixed) {
2973 counter_u64_add(numfullpathfail4, 1);
2974 SDT_PROBE3(vfs, namecache, fullpath, return, ENOMEM,
2978 buf[--buflen] = '/';
2980 counter_u64_add(numfullpathfound, 1);
2983 *retbuf = buf + buflen;
2984 SDT_PROBE3(vfs, namecache, fullpath, return, 0, startvp, *retbuf);
2991 * Resolve an arbitrary vnode to a pathname.
2994 * - hardlinks are not tracked, thus if the vnode is not a directory this can
2995 * resolve to a different path than the one used to find it
2996 * - namecache is not mandatory, meaning names are not guaranteed to be added
2997 * (in which case resolving fails)
2999 static void __inline
3000 cache_rev_failed_impl(int *reason, int line)
3005 #define cache_rev_failed(var) cache_rev_failed_impl((var), __LINE__)
3008 vn_fullpath_any_smr(struct vnode *vp, struct vnode *rdir, char *buf,
3009 char **retbuf, size_t *buflen, bool slash_prefixed, size_t addend)
3011 #ifdef KDTRACE_HOOKS
3012 struct vnode *startvp = vp;
3016 struct namecache *ncp;
3020 #ifdef KDTRACE_HOOKS
3023 seqc_t vp_seqc, tvp_seqc;
3026 VFS_SMR_ASSERT_ENTERED();
3028 if (!cache_fast_revlookup) {
3033 orig_buflen = *buflen;
3035 if (!slash_prefixed) {
3036 MPASS(*buflen >= 2);
3038 buf[*buflen] = '\0';
3041 if (vp == rdir || vp == rootvnode) {
3042 if (!slash_prefixed) {
3049 #ifdef KDTRACE_HOOKS
3053 ncp = NULL; /* for sdt probe down below */
3054 vp_seqc = vn_seqc_read_any(vp);
3055 if (seqc_in_modify(vp_seqc)) {
3056 cache_rev_failed(&reason);
3061 #ifdef KDTRACE_HOOKS
3064 if ((vp->v_vflag & VV_ROOT) != 0) {
3065 mp = atomic_load_ptr(&vp->v_mount);
3067 cache_rev_failed(&reason);
3070 tvp = atomic_load_ptr(&mp->mnt_vnodecovered);
3071 tvp_seqc = vn_seqc_read_any(tvp);
3072 if (seqc_in_modify(tvp_seqc)) {
3073 cache_rev_failed(&reason);
3076 if (!vn_seqc_consistent(vp, vp_seqc)) {
3077 cache_rev_failed(&reason);
3084 ncp = atomic_load_ptr(&vp->v_cache_dd);
3086 cache_rev_failed(&reason);
3089 nc_flag = atomic_load_char(&ncp->nc_flag);
3090 if ((nc_flag & NCF_ISDOTDOT) != 0) {
3091 cache_rev_failed(&reason);
3094 if (!cache_ncp_canuse(ncp)) {
3095 cache_rev_failed(&reason);
3098 if (ncp->nc_nlen >= *buflen) {
3099 cache_rev_failed(&reason);
3103 *buflen -= ncp->nc_nlen;
3104 memcpy(buf + *buflen, ncp->nc_name, ncp->nc_nlen);
3108 tvp_seqc = vn_seqc_read_any(tvp);
3109 if (seqc_in_modify(tvp_seqc)) {
3110 cache_rev_failed(&reason);
3113 if (!vn_seqc_consistent(vp, vp_seqc)) {
3114 cache_rev_failed(&reason);
3119 if (vp == rdir || vp == rootvnode)
3124 *retbuf = buf + *buflen;
3125 *buflen = orig_buflen - *buflen + addend;
3126 SDT_PROBE2(vfs, namecache, fullpath_smr, hit, startvp, *retbuf);
3130 *buflen = orig_buflen;
3131 SDT_PROBE4(vfs, namecache, fullpath_smr, miss, startvp, ncp, reason, i);
3137 vn_fullpath_any(struct vnode *vp, struct vnode *rdir, char *buf, char **retbuf,
3141 bool slash_prefixed;
3147 orig_buflen = *buflen;
3150 slash_prefixed = false;
3151 if (vp->v_type != VDIR) {
3153 buf[*buflen] = '\0';
3154 error = vn_vptocnp(&vp, curthread->td_ucred, buf, buflen);
3163 slash_prefixed = true;
3166 return (vn_fullpath_dir(vp, rdir, buf, retbuf, buflen, slash_prefixed,
3167 orig_buflen - *buflen));
3171 * Resolve an arbitrary vnode to a pathname (taking care of hardlinks).
3173 * Since the namecache does not track handlings, the caller is expected to first
3174 * look up the target vnode with SAVENAME | WANTPARENT flags passed to namei.
3176 * Then we have 2 cases:
3177 * - if the found vnode is a directory, the path can be constructed just by
3178 * fullowing names up the chain
3179 * - otherwise we populate the buffer with the saved name and start resolving
3183 vn_fullpath_hardlink(struct nameidata *ndp, char **retbuf, char **freebuf,
3188 struct componentname *cnp;
3192 bool slash_prefixed;
3197 if (*buflen > MAXPATHLEN)
3198 *buflen = MAXPATHLEN;
3200 slash_prefixed = false;
3202 buf = malloc(*buflen, M_TEMP, M_WAITOK);
3207 * Check for VBAD to work around the vp_crossmp bug in lookup().
3209 * For example consider tmpfs on /tmp and realpath /tmp. ni_vp will be
3210 * set to mount point's root vnode while ni_dvp will be vp_crossmp.
3211 * If the type is VDIR (like in this very case) we can skip looking
3212 * at ni_dvp in the first place. However, since vnodes get passed here
3213 * unlocked the target may transition to doomed state (type == VBAD)
3214 * before we get to evaluate the condition. If this happens, we will
3215 * populate part of the buffer and descend to vn_fullpath_dir with
3216 * vp == vp_crossmp. Prevent the problem by checking for VBAD.
3218 * This should be atomic_load(&vp->v_type) but it is ilegal to take
3219 * an address of a bit field, even if said field is sized to char.
3220 * Work around the problem by reading the value into a full-sized enum
3221 * and then re-reading it with atomic_load which will still prevent
3222 * the compiler from re-reading down the road.
3225 type = atomic_load_int(&type);
3232 addend = cnp->cn_namelen + 2;
3233 if (*buflen < addend) {
3238 tmpbuf = buf + *buflen;
3240 memcpy(&tmpbuf[1], cnp->cn_nameptr, cnp->cn_namelen);
3241 tmpbuf[addend - 1] = '\0';
3242 slash_prefixed = true;
3247 pwd = pwd_get_smr();
3248 error = vn_fullpath_any_smr(vp, pwd->pwd_rdir, buf, retbuf, buflen,
3249 slash_prefixed, addend);
3250 VFS_SMR_ASSERT_NOT_ENTERED();
3252 pwd = pwd_hold(curthread);
3254 error = vn_fullpath_dir(vp, pwd->pwd_rdir, buf, retbuf, buflen,
3255 slash_prefixed, addend);
3270 vn_dir_dd_ino(struct vnode *vp)
3272 struct namecache *ncp;
3277 ASSERT_VOP_LOCKED(vp, "vn_dir_dd_ino");
3278 vlp = VP2VNODELOCK(vp);
3280 TAILQ_FOREACH(ncp, &(vp->v_cache_dst), nc_dst) {
3281 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0)
3284 vs = vget_prep(ddvp);
3286 if (vget_finish(ddvp, LK_SHARED | LK_NOWAIT, vs))
3295 vn_commname(struct vnode *vp, char *buf, u_int buflen)
3297 struct namecache *ncp;
3301 vlp = VP2VNODELOCK(vp);
3303 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst)
3304 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
3310 l = min(ncp->nc_nlen, buflen - 1);
3311 memcpy(buf, ncp->nc_name, l);
3318 * This function updates path string to vnode's full global path
3319 * and checks the size of the new path string against the pathlen argument.
3321 * Requires a locked, referenced vnode.
3322 * Vnode is re-locked on success or ENODEV, otherwise unlocked.
3324 * If vp is a directory, the call to vn_fullpath_global() always succeeds
3325 * because it falls back to the ".." lookup if the namecache lookup fails.
3328 vn_path_to_global_path(struct thread *td, struct vnode *vp, char *path,
3331 struct nameidata nd;
3336 ASSERT_VOP_ELOCKED(vp, __func__);
3338 /* Construct global filesystem path from vp. */
3340 error = vn_fullpath_global(vp, &rpath, &fbuf);
3347 if (strlen(rpath) >= pathlen) {
3349 error = ENAMETOOLONG;
3354 * Re-lookup the vnode by path to detect a possible rename.
3355 * As a side effect, the vnode is relocked.
3356 * If vnode was renamed, return ENOENT.
3358 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
3359 UIO_SYSSPACE, path, td);
3365 NDFREE(&nd, NDF_ONLY_PNBUF);
3369 strcpy(path, rpath);
3382 db_print_vpath(struct vnode *vp)
3385 while (vp != NULL) {
3386 db_printf("%p: ", vp);
3387 if (vp == rootvnode) {
3391 if (vp->v_vflag & VV_ROOT) {
3392 db_printf("<mount point>");
3393 vp = vp->v_mount->mnt_vnodecovered;
3395 struct namecache *ncp;
3399 ncp = TAILQ_FIRST(&vp->v_cache_dst);
3402 for (i = 0; i < ncp->nc_nlen; i++)
3403 db_printf("%c", *ncn++);
3416 DB_SHOW_COMMAND(vpath, db_show_vpath)
3421 db_printf("usage: show vpath <struct vnode *>\n");
3425 vp = (struct vnode *)addr;
3431 static bool __read_frequently cache_fast_lookup = true;
3432 SYSCTL_BOOL(_vfs, OID_AUTO, cache_fast_lookup, CTLFLAG_RW,
3433 &cache_fast_lookup, 0, "");
3435 #define CACHE_FPL_FAILED -2020
3438 cache_fpl_cleanup_cnp(struct componentname *cnp)
3441 uma_zfree(namei_zone, cnp->cn_pnbuf);
3443 cnp->cn_pnbuf = NULL;
3444 cnp->cn_nameptr = NULL;
3449 cache_fpl_handle_root(struct nameidata *ndp, struct vnode **dpp)
3451 struct componentname *cnp;
3454 while (*(cnp->cn_nameptr) == '/') {
3459 *dpp = ndp->ni_rootdir;
3463 * Components of nameidata (or objects it can point to) which may
3464 * need restoring in case fast path lookup fails.
3466 struct nameidata_saved {
3474 struct nameidata *ndp;
3475 struct componentname *cnp;
3481 struct nameidata_saved snd;
3483 enum cache_fpl_status status:8;
3489 cache_fpl_checkpoint(struct cache_fpl *fpl, struct nameidata_saved *snd)
3492 snd->cn_flags = fpl->ndp->ni_cnd.cn_flags;
3493 snd->cn_namelen = fpl->ndp->ni_cnd.cn_namelen;
3494 snd->cn_nameptr = fpl->ndp->ni_cnd.cn_nameptr;
3495 snd->ni_pathlen = fpl->ndp->ni_pathlen;
3499 cache_fpl_restore(struct cache_fpl *fpl, struct nameidata_saved *snd)
3502 fpl->ndp->ni_cnd.cn_flags = snd->cn_flags;
3503 fpl->ndp->ni_cnd.cn_namelen = snd->cn_namelen;
3504 fpl->ndp->ni_cnd.cn_nameptr = snd->cn_nameptr;
3505 fpl->ndp->ni_pathlen = snd->ni_pathlen;
3509 #define cache_fpl_smr_assert_entered(fpl) ({ \
3510 struct cache_fpl *_fpl = (fpl); \
3511 MPASS(_fpl->in_smr == true); \
3512 VFS_SMR_ASSERT_ENTERED(); \
3514 #define cache_fpl_smr_assert_not_entered(fpl) ({ \
3515 struct cache_fpl *_fpl = (fpl); \
3516 MPASS(_fpl->in_smr == false); \
3517 VFS_SMR_ASSERT_NOT_ENTERED(); \
3520 #define cache_fpl_smr_assert_entered(fpl) do { } while (0)
3521 #define cache_fpl_smr_assert_not_entered(fpl) do { } while (0)
3524 #define cache_fpl_smr_enter_initial(fpl) ({ \
3525 struct cache_fpl *_fpl = (fpl); \
3527 _fpl->in_smr = true; \
3530 #define cache_fpl_smr_enter(fpl) ({ \
3531 struct cache_fpl *_fpl = (fpl); \
3532 MPASS(_fpl->in_smr == false); \
3534 _fpl->in_smr = true; \
3537 #define cache_fpl_smr_exit(fpl) ({ \
3538 struct cache_fpl *_fpl = (fpl); \
3539 MPASS(_fpl->in_smr == true); \
3541 _fpl->in_smr = false; \
3545 cache_fpl_aborted_impl(struct cache_fpl *fpl, int line)
3548 if (fpl->status != CACHE_FPL_STATUS_UNSET) {
3549 KASSERT(fpl->status == CACHE_FPL_STATUS_PARTIAL,
3550 ("%s: converting to abort from %d at %d, set at %d\n",
3551 __func__, fpl->status, line, fpl->line));
3553 fpl->status = CACHE_FPL_STATUS_ABORTED;
3555 return (CACHE_FPL_FAILED);
3558 #define cache_fpl_aborted(x) cache_fpl_aborted_impl((x), __LINE__)
3561 cache_fpl_partial_impl(struct cache_fpl *fpl, int line)
3564 KASSERT(fpl->status == CACHE_FPL_STATUS_UNSET,
3565 ("%s: setting to partial at %d, but already set to %d at %d\n",
3566 __func__, line, fpl->status, fpl->line));
3567 cache_fpl_smr_assert_entered(fpl);
3568 fpl->status = CACHE_FPL_STATUS_PARTIAL;
3570 return (CACHE_FPL_FAILED);
3573 #define cache_fpl_partial(x) cache_fpl_partial_impl((x), __LINE__)
3576 cache_fpl_handled_impl(struct cache_fpl *fpl, int error, int line)
3579 KASSERT(fpl->status == CACHE_FPL_STATUS_UNSET,
3580 ("%s: setting to handled at %d, but already set to %d at %d\n",
3581 __func__, line, fpl->status, fpl->line));
3582 cache_fpl_smr_assert_not_entered(fpl);
3583 MPASS(error != CACHE_FPL_FAILED);
3584 fpl->status = CACHE_FPL_STATUS_HANDLED;
3589 #define cache_fpl_handled(x, e) cache_fpl_handled_impl((x), (e), __LINE__)
3591 #define CACHE_FPL_SUPPORTED_CN_FLAGS \
3592 (LOCKLEAF | LOCKPARENT | WANTPARENT | NOCACHE | FOLLOW | LOCKSHARED | SAVENAME | \
3593 SAVESTART | WILLBEDIR | ISOPEN | NOMACCHECK | AUDITVNODE1 | AUDITVNODE2 | NOCAPCHECK)
3595 #define CACHE_FPL_INTERNAL_CN_FLAGS \
3596 (ISDOTDOT | MAKEENTRY | ISLASTCN)
3598 _Static_assert((CACHE_FPL_SUPPORTED_CN_FLAGS & CACHE_FPL_INTERNAL_CN_FLAGS) == 0,
3599 "supported and internal flags overlap");
3602 cache_fpl_islastcn(struct nameidata *ndp)
3605 return (*ndp->ni_next == 0);
3609 cache_fpl_isdotdot(struct componentname *cnp)
3612 if (cnp->cn_namelen == 2 &&
3613 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
3619 cache_can_fplookup(struct cache_fpl *fpl)
3621 struct nameidata *ndp;
3622 struct componentname *cnp;
3627 td = cnp->cn_thread;
3629 if (!cache_fast_lookup) {
3630 cache_fpl_aborted(fpl);
3634 if (mac_vnode_check_lookup_enabled()) {
3635 cache_fpl_aborted(fpl);
3639 if ((cnp->cn_flags & ~CACHE_FPL_SUPPORTED_CN_FLAGS) != 0) {
3640 cache_fpl_aborted(fpl);
3643 if (IN_CAPABILITY_MODE(td)) {
3644 cache_fpl_aborted(fpl);
3647 if (AUDITING_TD(td)) {
3648 cache_fpl_aborted(fpl);
3651 if (ndp->ni_startdir != NULL) {
3652 cache_fpl_aborted(fpl);
3659 cache_fplookup_dirfd(struct cache_fpl *fpl, struct vnode **vpp)
3661 struct nameidata *ndp;
3666 error = fgetvp_lookup_smr(ndp->ni_dirfd, ndp, vpp, &fsearch);
3667 if (__predict_false(error != 0)) {
3668 cache_fpl_smr_exit(fpl);
3669 return (cache_fpl_aborted(fpl));
3671 fpl->fsearch = fsearch;
3676 cache_fplookup_vnode_supported(struct vnode *vp)
3679 return (vp->v_type != VLNK);
3682 static int __noinline
3683 cache_fplookup_negative_promote(struct cache_fpl *fpl, struct namecache *oncp,
3686 struct componentname *cnp;
3692 cache_fpl_smr_exit(fpl);
3693 if (cache_neg_promote_cond(dvp, cnp, oncp, hash))
3694 return (cache_fpl_handled(fpl, ENOENT));
3696 return (cache_fpl_aborted(fpl));
3700 * The target vnode is not supported, prepare for the slow path to take over.
3702 static int __noinline
3703 cache_fplookup_partial_setup(struct cache_fpl *fpl)
3705 struct nameidata *ndp;
3706 struct componentname *cnp;
3716 dvp_seqc = fpl->dvp_seqc;
3718 if (!pwd_hold_smr(pwd)) {
3719 cache_fpl_smr_exit(fpl);
3720 return (cache_fpl_aborted(fpl));
3723 dvs = vget_prep_smr(dvp);
3724 cache_fpl_smr_exit(fpl);
3725 if (__predict_false(dvs == VGET_NONE)) {
3727 return (cache_fpl_aborted(fpl));
3730 vget_finish_ref(dvp, dvs);
3731 if (!vn_seqc_consistent(dvp, dvp_seqc)) {
3734 return (cache_fpl_aborted(fpl));
3737 cache_fpl_restore(fpl, &fpl->snd);
3739 ndp->ni_startdir = dvp;
3740 cnp->cn_flags |= MAKEENTRY;
3741 if (cache_fpl_islastcn(ndp))
3742 cnp->cn_flags |= ISLASTCN;
3743 if (cache_fpl_isdotdot(cnp))
3744 cnp->cn_flags |= ISDOTDOT;
3750 cache_fplookup_final_child(struct cache_fpl *fpl, enum vgetstate tvs)
3752 struct componentname *cnp;
3759 tvp_seqc = fpl->tvp_seqc;
3761 if ((cnp->cn_flags & LOCKLEAF) != 0) {
3762 lkflags = LK_SHARED;
3763 if ((cnp->cn_flags & LOCKSHARED) == 0)
3764 lkflags = LK_EXCLUSIVE;
3765 error = vget_finish(tvp, lkflags, tvs);
3766 if (__predict_false(error != 0)) {
3767 return (cache_fpl_aborted(fpl));
3770 vget_finish_ref(tvp, tvs);
3773 if (!vn_seqc_consistent(tvp, tvp_seqc)) {
3774 if ((cnp->cn_flags & LOCKLEAF) != 0)
3778 return (cache_fpl_aborted(fpl));
3781 return (cache_fpl_handled(fpl, 0));
3785 * They want to possibly modify the state of the namecache.
3787 * Don't try to match the API contract, just leave.
3788 * TODO: this leaves scalability on the table
3791 cache_fplookup_final_modifying(struct cache_fpl *fpl)
3793 struct componentname *cnp;
3796 MPASS(cnp->cn_nameiop != LOOKUP);
3797 return (cache_fpl_partial(fpl));
3800 static int __noinline
3801 cache_fplookup_final_withparent(struct cache_fpl *fpl)
3803 struct componentname *cnp;
3804 enum vgetstate dvs, tvs;
3805 struct vnode *dvp, *tvp;
3811 dvp_seqc = fpl->dvp_seqc;
3814 MPASS((cnp->cn_flags & (LOCKPARENT|WANTPARENT)) != 0);
3817 * This is less efficient than it can be for simplicity.
3819 dvs = vget_prep_smr(dvp);
3820 if (__predict_false(dvs == VGET_NONE)) {
3821 return (cache_fpl_aborted(fpl));
3823 tvs = vget_prep_smr(tvp);
3824 if (__predict_false(tvs == VGET_NONE)) {
3825 cache_fpl_smr_exit(fpl);
3826 vget_abort(dvp, dvs);
3827 return (cache_fpl_aborted(fpl));
3830 cache_fpl_smr_exit(fpl);
3832 if ((cnp->cn_flags & LOCKPARENT) != 0) {
3833 error = vget_finish(dvp, LK_EXCLUSIVE, dvs);
3834 if (__predict_false(error != 0)) {
3835 vget_abort(tvp, tvs);
3836 return (cache_fpl_aborted(fpl));
3839 vget_finish_ref(dvp, dvs);
3842 if (!vn_seqc_consistent(dvp, dvp_seqc)) {
3843 vget_abort(tvp, tvs);
3844 if ((cnp->cn_flags & LOCKPARENT) != 0)
3848 return (cache_fpl_aborted(fpl));
3851 error = cache_fplookup_final_child(fpl, tvs);
3852 if (__predict_false(error != 0)) {
3853 MPASS(fpl->status == CACHE_FPL_STATUS_ABORTED);
3854 if ((cnp->cn_flags & LOCKPARENT) != 0)
3861 MPASS(fpl->status == CACHE_FPL_STATUS_HANDLED);
3866 cache_fplookup_final(struct cache_fpl *fpl)
3868 struct componentname *cnp;
3870 struct vnode *dvp, *tvp;
3875 dvp_seqc = fpl->dvp_seqc;
3878 VNPASS(cache_fplookup_vnode_supported(dvp), dvp);
3880 if (cnp->cn_nameiop != LOOKUP) {
3881 return (cache_fplookup_final_modifying(fpl));
3884 if ((cnp->cn_flags & (LOCKPARENT|WANTPARENT)) != 0)
3885 return (cache_fplookup_final_withparent(fpl));
3887 tvs = vget_prep_smr(tvp);
3888 if (__predict_false(tvs == VGET_NONE)) {
3889 return (cache_fpl_partial(fpl));
3892 if (!vn_seqc_consistent(dvp, dvp_seqc)) {
3893 cache_fpl_smr_exit(fpl);
3894 vget_abort(tvp, tvs);
3895 return (cache_fpl_aborted(fpl));
3898 cache_fpl_smr_exit(fpl);
3899 return (cache_fplookup_final_child(fpl, tvs));
3902 static int __noinline
3903 cache_fplookup_dot(struct cache_fpl *fpl)
3910 fpl->tvp_seqc = vn_seqc_read_any(dvp);
3911 if (seqc_in_modify(fpl->tvp_seqc)) {
3912 return (cache_fpl_aborted(fpl));
3915 counter_u64_add(dothits, 1);
3916 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", dvp);
3921 static int __noinline
3922 cache_fplookup_dotdot(struct cache_fpl *fpl)
3924 struct nameidata *ndp;
3925 struct componentname *cnp;
3926 struct namecache *ncp;
3936 * XXX this is racy the same way regular lookup is
3938 for (pr = cnp->cn_cred->cr_prison; pr != NULL;
3940 if (dvp == pr->pr_root)
3943 if (dvp == ndp->ni_rootdir ||
3944 dvp == ndp->ni_topdir ||
3948 fpl->tvp_seqc = vn_seqc_read_any(dvp);
3949 if (seqc_in_modify(fpl->tvp_seqc)) {
3950 return (cache_fpl_aborted(fpl));
3955 if ((dvp->v_vflag & VV_ROOT) != 0) {
3958 * The opposite of climb mount is needed here.
3960 return (cache_fpl_aborted(fpl));
3963 ncp = atomic_load_ptr(&dvp->v_cache_dd);
3965 return (cache_fpl_aborted(fpl));
3968 nc_flag = atomic_load_char(&ncp->nc_flag);
3969 if ((nc_flag & NCF_ISDOTDOT) != 0) {
3970 if ((nc_flag & NCF_NEGATIVE) != 0)
3971 return (cache_fpl_aborted(fpl));
3972 fpl->tvp = ncp->nc_vp;
3974 fpl->tvp = ncp->nc_dvp;
3977 if (__predict_false(!cache_ncp_canuse(ncp))) {
3978 return (cache_fpl_aborted(fpl));
3981 fpl->tvp_seqc = vn_seqc_read_any(fpl->tvp);
3982 if (seqc_in_modify(fpl->tvp_seqc)) {
3983 return (cache_fpl_partial(fpl));
3986 counter_u64_add(dotdothits, 1);
3990 static int __noinline
3991 cache_fplookup_neg(struct cache_fpl *fpl, struct namecache *ncp, uint32_t hash)
3996 nc_flag = atomic_load_char(&ncp->nc_flag);
3997 MPASS((nc_flag & NCF_NEGATIVE) != 0);
3999 * If they want to create an entry we need to replace this one.
4001 if (__predict_false(fpl->cnp->cn_nameiop != LOOKUP)) {
4004 * This should call something similar to
4005 * cache_fplookup_final_modifying.
4007 return (cache_fpl_partial(fpl));
4009 neg_promote = cache_neg_hit_prep(ncp);
4010 if (__predict_false(!cache_ncp_canuse(ncp))) {
4011 cache_neg_hit_abort(ncp);
4012 return (cache_fpl_partial(fpl));
4014 if (__predict_false((nc_flag & NCF_WHITE) != 0)) {
4015 cache_neg_hit_abort(ncp);
4016 return (cache_fpl_partial(fpl));
4019 return (cache_fplookup_negative_promote(fpl, ncp, hash));
4021 cache_neg_hit_finish(ncp);
4022 cache_fpl_smr_exit(fpl);
4023 return (cache_fpl_handled(fpl, ENOENT));
4027 cache_fplookup_next(struct cache_fpl *fpl)
4029 struct componentname *cnp;
4030 struct namecache *ncp;
4031 struct vnode *dvp, *tvp;
4038 if (__predict_false(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.')) {
4039 return (cache_fplookup_dot(fpl));
4042 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
4044 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
4045 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
4046 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
4051 * If there is no entry we have to punt to the slow path to perform
4052 * actual lookup. Should there be nothing with this name a negative
4053 * entry will be created.
4055 if (__predict_false(ncp == NULL)) {
4056 return (cache_fpl_partial(fpl));
4059 tvp = atomic_load_ptr(&ncp->nc_vp);
4060 nc_flag = atomic_load_char(&ncp->nc_flag);
4061 if ((nc_flag & NCF_NEGATIVE) != 0) {
4062 return (cache_fplookup_neg(fpl, ncp, hash));
4065 if (__predict_false(!cache_ncp_canuse(ncp))) {
4066 return (cache_fpl_partial(fpl));
4070 fpl->tvp_seqc = vn_seqc_read_any(tvp);
4071 if (seqc_in_modify(fpl->tvp_seqc)) {
4072 return (cache_fpl_partial(fpl));
4075 if (!cache_fplookup_vnode_supported(tvp)) {
4076 return (cache_fpl_partial(fpl));
4079 counter_u64_add(numposhits, 1);
4080 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name, tvp);
4085 cache_fplookup_mp_supported(struct mount *mp)
4090 if ((mp->mnt_kern_flag & MNTK_FPLOOKUP) == 0)
4096 * Walk up the mount stack (if any).
4098 * Correctness is provided in the following ways:
4099 * - all vnodes are protected from freeing with SMR
4100 * - struct mount objects are type stable making them always safe to access
4101 * - stability of the particular mount is provided by busying it
4102 * - relationship between the vnode which is mounted on and the mount is
4103 * verified with the vnode sequence counter after busying
4104 * - association between root vnode of the mount and the mount is protected
4107 * From that point on we can read the sequence counter of the root vnode
4108 * and get the next mount on the stack (if any) using the same protection.
4110 * By the end of successful walk we are guaranteed the reached state was
4111 * indeed present at least at some point which matches the regular lookup.
4113 static int __noinline
4114 cache_fplookup_climb_mount(struct cache_fpl *fpl)
4116 struct mount *mp, *prev_mp;
4121 vp_seqc = fpl->tvp_seqc;
4123 VNPASS(vp->v_type == VDIR || vp->v_type == VBAD, vp);
4124 mp = atomic_load_ptr(&vp->v_mountedhere);
4130 if (!vfs_op_thread_enter_crit(mp)) {
4131 if (prev_mp != NULL)
4132 vfs_op_thread_exit_crit(prev_mp);
4133 return (cache_fpl_partial(fpl));
4135 if (prev_mp != NULL)
4136 vfs_op_thread_exit_crit(prev_mp);
4137 if (!vn_seqc_consistent(vp, vp_seqc)) {
4138 vfs_op_thread_exit_crit(mp);
4139 return (cache_fpl_partial(fpl));
4141 if (!cache_fplookup_mp_supported(mp)) {
4142 vfs_op_thread_exit_crit(mp);
4143 return (cache_fpl_partial(fpl));
4145 vp = atomic_load_ptr(&mp->mnt_rootvnode);
4146 if (vp == NULL || VN_IS_DOOMED(vp)) {
4147 vfs_op_thread_exit_crit(mp);
4148 return (cache_fpl_partial(fpl));
4150 vp_seqc = vn_seqc_read_any(vp);
4151 if (seqc_in_modify(vp_seqc)) {
4152 vfs_op_thread_exit_crit(mp);
4153 return (cache_fpl_partial(fpl));
4156 mp = atomic_load_ptr(&vp->v_mountedhere);
4161 vfs_op_thread_exit_crit(prev_mp);
4163 fpl->tvp_seqc = vp_seqc;
4168 cache_fplookup_need_climb_mount(struct cache_fpl *fpl)
4176 * Hack: while this is a union, the pointer tends to be NULL so save on
4179 mp = atomic_load_ptr(&vp->v_mountedhere);
4182 if (vp->v_type == VDIR)
4190 * The code was originally copy-pasted from regular lookup and despite
4191 * clean ups leaves performance on the table. Any modifications here
4192 * must take into account that in case off fallback the resulting
4193 * nameidata state has to be compatible with the original.
4196 cache_fplookup_parse(struct cache_fpl *fpl)
4198 struct nameidata *ndp;
4199 struct componentname *cnp;
4206 * Search a new directory.
4208 * The last component of the filename is left accessible via
4209 * cnp->cn_nameptr for callers that need the name. Callers needing
4210 * the name set the SAVENAME flag. When done, they assume
4211 * responsibility for freeing the pathname buffer.
4213 for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
4215 cnp->cn_namelen = cp - cnp->cn_nameptr;
4216 if (__predict_false(cnp->cn_namelen > NAME_MAX)) {
4217 cache_fpl_smr_exit(fpl);
4218 return (cache_fpl_handled(fpl, ENAMETOOLONG));
4220 ndp->ni_pathlen -= cnp->cn_namelen;
4221 KASSERT(ndp->ni_pathlen <= PATH_MAX,
4222 ("%s: ni_pathlen underflow to %zd\n", __func__, ndp->ni_pathlen));
4226 * Replace multiple slashes by a single slash and trailing slashes
4227 * by a null. This must be done before VOP_LOOKUP() because some
4228 * fs's don't know about trailing slashes. Remember if there were
4229 * trailing slashes to handle symlinks, existing non-directories
4230 * and non-existing files that won't be directories specially later.
4232 while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
4238 * Regular lookup performs the following:
4239 * *ndp->ni_next = '\0';
4240 * cnp->cn_flags |= TRAILINGSLASH;
4242 * Which is problematic since it modifies data read
4243 * from userspace. Then if fast path lookup was to
4244 * abort we would have to either restore it or convey
4245 * the flag. Since this is a corner case just ignore
4246 * it for simplicity.
4248 return (cache_fpl_partial(fpl));
4254 * Check for degenerate name (e.g. / or "")
4255 * which is a way of talking about a directory,
4256 * e.g. like "/." or ".".
4259 * Another corner case handled by the regular lookup
4261 if (__predict_false(cnp->cn_nameptr[0] == '\0')) {
4262 return (cache_fpl_partial(fpl));
4268 cache_fplookup_parse_advance(struct cache_fpl *fpl)
4270 struct nameidata *ndp;
4271 struct componentname *cnp;
4276 cnp->cn_nameptr = ndp->ni_next;
4277 while (*cnp->cn_nameptr == '/') {
4284 * See the API contract for VOP_FPLOOKUP_VEXEC.
4286 static int __noinline
4287 cache_fplookup_failed_vexec(struct cache_fpl *fpl, int error)
4289 struct componentname *cnp;
4295 dvp_seqc = fpl->dvp_seqc;
4298 * Hack: they may be looking up foo/bar, where foo is a
4299 * regular file. In such a case we need to turn ENOTDIR,
4300 * but we may happen to get here with a different error.
4302 if (dvp->v_type != VDIR) {
4304 * The check here is predominantly to catch
4305 * EOPNOTSUPP from dead_vnodeops. If the vnode
4306 * gets doomed past this point it is going to
4307 * fail seqc verification.
4309 if (VN_IS_DOOMED(dvp)) {
4310 return (cache_fpl_aborted(fpl));
4316 * Hack: handle O_SEARCH.
4318 * Open Group Base Specifications Issue 7, 2018 edition states:
4319 * If the access mode of the open file description associated with the
4320 * file descriptor is not O_SEARCH, the function shall check whether
4321 * directory searches are permitted using the current permissions of
4322 * the directory underlying the file descriptor. If the access mode is
4323 * O_SEARCH, the function shall not perform the check.
4325 * Regular lookup tests for the NOEXECCHECK flag for every path
4326 * component to decide whether to do the permission check. However,
4327 * since most lookups never have the flag (and when they do it is only
4328 * present for the first path component), lockless lookup only acts on
4329 * it if there is a permission problem. Here the flag is represented
4330 * with a boolean so that we don't have to clear it on the way out.
4332 * For simplicity this always aborts.
4333 * TODO: check if this is the first lookup and ignore the permission
4334 * problem. Note the flag has to survive fallback (if it happens to be
4338 return (cache_fpl_aborted(fpl));
4343 if (!vn_seqc_consistent(dvp, dvp_seqc)) {
4344 error = cache_fpl_aborted(fpl);
4346 cache_fpl_partial(fpl);
4350 if (!vn_seqc_consistent(dvp, dvp_seqc)) {
4351 error = cache_fpl_aborted(fpl);
4353 cache_fpl_smr_exit(fpl);
4354 cache_fpl_handled(fpl, error);
4362 cache_fplookup_impl(struct vnode *dvp, struct cache_fpl *fpl)
4364 struct nameidata *ndp;
4365 struct componentname *cnp;
4369 error = CACHE_FPL_FAILED;
4373 cache_fpl_checkpoint(fpl, &fpl->snd);
4376 fpl->dvp_seqc = vn_seqc_read_any(fpl->dvp);
4377 if (seqc_in_modify(fpl->dvp_seqc)) {
4378 cache_fpl_aborted(fpl);
4381 mp = atomic_load_ptr(&fpl->dvp->v_mount);
4382 if (!cache_fplookup_mp_supported(mp)) {
4383 cache_fpl_aborted(fpl);
4387 VNPASS(cache_fplookup_vnode_supported(fpl->dvp), fpl->dvp);
4390 error = cache_fplookup_parse(fpl);
4391 if (__predict_false(error != 0)) {
4395 VNPASS(cache_fplookup_vnode_supported(fpl->dvp), fpl->dvp);
4397 error = VOP_FPLOOKUP_VEXEC(fpl->dvp, cnp->cn_cred);
4398 if (__predict_false(error != 0)) {
4399 error = cache_fplookup_failed_vexec(fpl, error);
4403 if (__predict_false(cache_fpl_isdotdot(cnp))) {
4404 error = cache_fplookup_dotdot(fpl);
4405 if (__predict_false(error != 0)) {
4409 error = cache_fplookup_next(fpl);
4410 if (__predict_false(error != 0)) {
4414 VNPASS(!seqc_in_modify(fpl->tvp_seqc), fpl->tvp);
4416 if (cache_fplookup_need_climb_mount(fpl)) {
4417 error = cache_fplookup_climb_mount(fpl);
4418 if (__predict_false(error != 0)) {
4424 VNPASS(!seqc_in_modify(fpl->tvp_seqc), fpl->tvp);
4426 if (cache_fpl_islastcn(ndp)) {
4427 error = cache_fplookup_final(fpl);
4431 if (!vn_seqc_consistent(fpl->dvp, fpl->dvp_seqc)) {
4432 error = cache_fpl_aborted(fpl);
4436 fpl->dvp = fpl->tvp;
4437 fpl->dvp_seqc = fpl->tvp_seqc;
4439 cache_fplookup_parse_advance(fpl);
4440 cache_fpl_checkpoint(fpl, &fpl->snd);
4443 switch (fpl->status) {
4444 case CACHE_FPL_STATUS_UNSET:
4445 __assert_unreachable();
4447 case CACHE_FPL_STATUS_PARTIAL:
4448 cache_fpl_smr_assert_entered(fpl);
4449 return (cache_fplookup_partial_setup(fpl));
4450 case CACHE_FPL_STATUS_ABORTED:
4452 cache_fpl_smr_exit(fpl);
4453 return (CACHE_FPL_FAILED);
4454 case CACHE_FPL_STATUS_HANDLED:
4455 MPASS(error != CACHE_FPL_FAILED);
4456 cache_fpl_smr_assert_not_entered(fpl);
4457 if (__predict_false(error != 0)) {
4460 cache_fpl_cleanup_cnp(cnp);
4463 ndp->ni_dvp = fpl->dvp;
4464 ndp->ni_vp = fpl->tvp;
4465 if (cnp->cn_flags & SAVENAME)
4466 cnp->cn_flags |= HASBUF;
4468 cache_fpl_cleanup_cnp(cnp);
4474 * Fast path lookup protected with SMR and sequence counters.
4476 * Note: all VOP_FPLOOKUP_VEXEC routines have a comment referencing this one.
4478 * Filesystems can opt in by setting the MNTK_FPLOOKUP flag and meeting criteria
4481 * Traditional vnode lookup conceptually looks like this:
4487 * vn_unlock(current);
4494 * Each jump to the next vnode is safe memory-wise and atomic with respect to
4495 * any modifications thanks to holding respective locks.
4497 * The same guarantee can be provided with a combination of safe memory
4498 * reclamation and sequence counters instead. If all operations which affect
4499 * the relationship between the current vnode and the one we are looking for
4500 * also modify the counter, we can verify whether all the conditions held as
4501 * we made the jump. This includes things like permissions, mount points etc.
4502 * Counter modification is provided by enclosing relevant places in
4503 * vn_seqc_write_begin()/end() calls.
4505 * Thus this translates to:
4508 * dvp_seqc = seqc_read_any(dvp);
4509 * if (seqc_in_modify(dvp_seqc)) // someone is altering the vnode
4513 * tvp_seqc = seqc_read_any(tvp);
4514 * if (seqc_in_modify(tvp_seqc)) // someone is altering the target vnode
4516 * if (!seqc_consistent(dvp, dvp_seqc) // someone is altering the vnode
4518 * dvp = tvp; // we know nothing of importance has changed
4519 * dvp_seqc = tvp_seqc; // store the counter for the tvp iteration
4523 * vget(); // secure the vnode
4524 * if (!seqc_consistent(tvp, tvp_seqc) // final check
4526 * // at this point we know nothing has changed for any parent<->child pair
4527 * // as they were crossed during the lookup, meaning we matched the guarantee
4528 * // of the locked variant
4531 * The API contract for VOP_FPLOOKUP_VEXEC routines is as follows:
4532 * - they are called while within vfs_smr protection which they must never exit
4533 * - EAGAIN can be returned to denote checking could not be performed, it is
4534 * always valid to return it
4535 * - if the sequence counter has not changed the result must be valid
4536 * - if the sequence counter has changed both false positives and false negatives
4537 * are permitted (since the result will be rejected later)
4538 * - for simple cases of unix permission checks vaccess_vexec_smr can be used
4540 * Caveats to watch out for:
4541 * - vnodes are passed unlocked and unreferenced with nothing stopping
4542 * VOP_RECLAIM, in turn meaning that ->v_data can become NULL. It is advised
4543 * to use atomic_load_ptr to fetch it.
4544 * - the aforementioned object can also get freed, meaning absent other means it
4545 * should be protected with vfs_smr
4546 * - either safely checking permissions as they are modified or guaranteeing
4547 * their stability is left to the routine
4550 cache_fplookup(struct nameidata *ndp, enum cache_fpl_status *status,
4553 struct cache_fpl fpl;
4556 struct componentname *cnp;
4557 struct nameidata_saved orig;
4560 MPASS(ndp->ni_lcf == 0);
4562 fpl.status = CACHE_FPL_STATUS_UNSET;
4564 fpl.cnp = &ndp->ni_cnd;
4565 MPASS(curthread == fpl.cnp->cn_thread);
4567 if ((fpl.cnp->cn_flags & SAVESTART) != 0)
4568 MPASS(fpl.cnp->cn_nameiop != LOOKUP);
4570 if (!cache_can_fplookup(&fpl)) {
4571 SDT_PROBE3(vfs, fplookup, lookup, done, ndp, fpl.line, fpl.status);
4572 *status = fpl.status;
4573 return (EOPNOTSUPP);
4576 cache_fpl_checkpoint(&fpl, &orig);
4578 cache_fpl_smr_enter_initial(&fpl);
4579 fpl.fsearch = false;
4580 pwd = pwd_get_smr();
4582 ndp->ni_rootdir = pwd->pwd_rdir;
4583 ndp->ni_topdir = pwd->pwd_jdir;
4586 cnp->cn_nameptr = cnp->cn_pnbuf;
4587 if (cnp->cn_pnbuf[0] == '/') {
4588 cache_fpl_handle_root(ndp, &dvp);
4590 if (ndp->ni_dirfd == AT_FDCWD) {
4591 dvp = pwd->pwd_cdir;
4593 error = cache_fplookup_dirfd(&fpl, &dvp);
4594 if (__predict_false(error != 0)) {
4600 SDT_PROBE4(vfs, namei, lookup, entry, dvp, cnp->cn_pnbuf, cnp->cn_flags, true);
4602 error = cache_fplookup_impl(dvp, &fpl);
4604 cache_fpl_smr_assert_not_entered(&fpl);
4605 SDT_PROBE3(vfs, fplookup, lookup, done, ndp, fpl.line, fpl.status);
4607 *status = fpl.status;
4608 switch (fpl.status) {
4609 case CACHE_FPL_STATUS_UNSET:
4610 __assert_unreachable();
4612 case CACHE_FPL_STATUS_HANDLED:
4613 SDT_PROBE3(vfs, namei, lookup, return, error,
4614 (error == 0 ? ndp->ni_vp : NULL), true);
4616 case CACHE_FPL_STATUS_PARTIAL:
4619 * Status restored by cache_fplookup_partial_setup.
4622 case CACHE_FPL_STATUS_ABORTED:
4623 cache_fpl_restore(&fpl, &orig);