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 SDT_PROVIDER_DECLARE(vfs);
83 SDT_PROBE_DEFINE3(vfs, namecache, enter, done, "struct vnode *", "char *",
85 SDT_PROBE_DEFINE3(vfs, namecache, enter, duplicate, "struct vnode *", "char *",
87 SDT_PROBE_DEFINE2(vfs, namecache, enter_negative, done, "struct vnode *",
89 SDT_PROBE_DEFINE2(vfs, namecache, fullpath_smr, hit, "struct vnode *",
91 SDT_PROBE_DEFINE4(vfs, namecache, fullpath_smr, miss, "struct vnode *",
92 "struct namecache *", "int", "int");
93 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, entry, "struct vnode *");
94 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, hit, "struct vnode *",
95 "char *", "struct vnode *");
96 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, miss, "struct vnode *");
97 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, return, "int",
98 "struct vnode *", "char *");
99 SDT_PROBE_DEFINE3(vfs, namecache, lookup, hit, "struct vnode *", "char *",
101 SDT_PROBE_DEFINE2(vfs, namecache, lookup, hit__negative,
102 "struct vnode *", "char *");
103 SDT_PROBE_DEFINE2(vfs, namecache, lookup, miss, "struct vnode *",
105 SDT_PROBE_DEFINE2(vfs, namecache, removecnp, hit, "struct vnode *",
106 "struct componentname *");
107 SDT_PROBE_DEFINE2(vfs, namecache, removecnp, miss, "struct vnode *",
108 "struct componentname *");
109 SDT_PROBE_DEFINE1(vfs, namecache, purge, done, "struct vnode *");
110 SDT_PROBE_DEFINE1(vfs, namecache, purge_negative, done, "struct vnode *");
111 SDT_PROBE_DEFINE1(vfs, namecache, purgevfs, done, "struct mount *");
112 SDT_PROBE_DEFINE3(vfs, namecache, zap, done, "struct vnode *", "char *",
114 SDT_PROBE_DEFINE2(vfs, namecache, zap_negative, done, "struct vnode *",
116 SDT_PROBE_DEFINE2(vfs, namecache, evict_negative, done, "struct vnode *",
119 SDT_PROBE_DEFINE3(vfs, fplookup, lookup, done, "struct nameidata", "int", "bool");
120 SDT_PROBE_DECLARE(vfs, namei, lookup, entry);
121 SDT_PROBE_DECLARE(vfs, namei, lookup, return);
124 * This structure describes the elements in the cache of recent
125 * names looked up by namei.
130 _Static_assert(sizeof(struct negstate) <= sizeof(struct vnode *),
131 "the state must fit in a union with a pointer without growing it");
134 LIST_ENTRY(namecache) nc_src; /* source vnode list */
135 TAILQ_ENTRY(namecache) nc_dst; /* destination vnode list */
136 CK_SLIST_ENTRY(namecache) nc_hash;/* hash chain */
137 struct vnode *nc_dvp; /* vnode of parent of name */
139 struct vnode *nu_vp; /* vnode the name refers to */
140 struct negstate nu_neg;/* negative entry state */
142 u_char nc_flag; /* flag bits */
143 u_char nc_nlen; /* length of name */
144 char nc_name[0]; /* segment name + nul */
148 * struct namecache_ts repeats struct namecache layout up to the
150 * struct namecache_ts is used in place of struct namecache when time(s) need
151 * to be stored. The nc_dotdottime field is used when a cache entry is mapping
152 * both a non-dotdot directory name plus dotdot for the directory's
155 * See below for alignment requirement.
157 struct namecache_ts {
158 struct timespec nc_time; /* timespec provided by fs */
159 struct timespec nc_dotdottime; /* dotdot timespec provided by fs */
160 int nc_ticks; /* ticks value when entry was added */
161 struct namecache nc_nc;
165 * At least mips n32 performs 64-bit accesses to timespec as found
166 * in namecache_ts and requires them to be aligned. Since others
167 * may be in the same spot suffer a little bit and enforce the
168 * alignment for everyone. Note this is a nop for 64-bit platforms.
170 #define CACHE_ZONE_ALIGNMENT UMA_ALIGNOF(time_t)
171 #define CACHE_PATH_CUTOFF 39
173 #define CACHE_ZONE_SMALL_SIZE (sizeof(struct namecache) + CACHE_PATH_CUTOFF + 1)
174 #define CACHE_ZONE_SMALL_TS_SIZE (sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1)
175 #define CACHE_ZONE_LARGE_SIZE (sizeof(struct namecache) + NAME_MAX + 1)
176 #define CACHE_ZONE_LARGE_TS_SIZE (sizeof(struct namecache_ts) + NAME_MAX + 1)
178 _Static_assert((CACHE_ZONE_SMALL_SIZE % (CACHE_ZONE_ALIGNMENT + 1)) == 0, "bad zone size");
179 _Static_assert((CACHE_ZONE_SMALL_TS_SIZE % (CACHE_ZONE_ALIGNMENT + 1)) == 0, "bad zone size");
180 _Static_assert((CACHE_ZONE_LARGE_SIZE % (CACHE_ZONE_ALIGNMENT + 1)) == 0, "bad zone size");
181 _Static_assert((CACHE_ZONE_LARGE_TS_SIZE % (CACHE_ZONE_ALIGNMENT + 1)) == 0, "bad zone size");
183 #define nc_vp n_un.nu_vp
184 #define nc_neg n_un.nu_neg
187 * Flags in namecache.nc_flag
189 #define NCF_WHITE 0x01
190 #define NCF_ISDOTDOT 0x02
193 #define NCF_DVDROP 0x10
194 #define NCF_NEGATIVE 0x20
195 #define NCF_INVALID 0x40
199 * Flags in negstate.neg_flag
204 * Mark an entry as invalid.
206 * This is called before it starts getting deconstructed.
209 cache_ncp_invalidate(struct namecache *ncp)
212 KASSERT((ncp->nc_flag & NCF_INVALID) == 0,
213 ("%s: entry %p already invalid", __func__, ncp));
214 atomic_store_char(&ncp->nc_flag, ncp->nc_flag | NCF_INVALID);
215 atomic_thread_fence_rel();
219 * Check whether the entry can be safely used.
221 * All places which elide locks are supposed to call this after they are
222 * done with reading from an entry.
225 cache_ncp_canuse(struct namecache *ncp)
228 atomic_thread_fence_acq();
229 return ((atomic_load_char(&ncp->nc_flag) & (NCF_INVALID | NCF_WIP)) == 0);
233 * Name caching works as follows:
235 * Names found by directory scans are retained in a cache
236 * for future reference. It is managed LRU, so frequently
237 * used names will hang around. Cache is indexed by hash value
238 * obtained from (dvp, name) where dvp refers to the directory
241 * If it is a "negative" entry, (i.e. for a name that is known NOT to
242 * exist) the vnode pointer will be NULL.
244 * Upon reaching the last segment of a path, if the reference
245 * is for DELETE, or NOCACHE is set (rewrite), and the
246 * name is located in the cache, it will be dropped.
248 * These locks are used (in the order in which they can be taken):
250 * vnodelock mtx vnode lists and v_cache_dd field protection
251 * bucketlock mtx for access to given set of hash buckets
252 * neglist mtx negative entry LRU management
254 * It is legal to take multiple vnodelock and bucketlock locks. The locking
255 * order is lower address first. Both are recursive.
257 * "." lookups are lockless.
259 * ".." and vnode -> name lookups require vnodelock.
261 * name -> vnode lookup requires the relevant bucketlock to be held for reading.
263 * Insertions and removals of entries require involved vnodes and bucketlocks
264 * to be locked to provide safe operation against other threads modifying the
267 * Some lookups result in removal of the found entry (e.g. getting rid of a
268 * negative entry with the intent to create a positive one), which poses a
269 * problem when multiple threads reach the state. Similarly, two different
270 * threads can purge two different vnodes and try to remove the same name.
272 * If the already held vnode lock is lower than the second required lock, we
273 * can just take the other lock. However, in the opposite case, this could
274 * deadlock. As such, this is resolved by trylocking and if that fails unlocking
275 * the first node, locking everything in order and revalidating the state.
281 * Structures associated with name caching.
283 #define NCHHASH(hash) \
284 (&nchashtbl[(hash) & nchash])
285 static __read_mostly CK_SLIST_HEAD(nchashhead, namecache) *nchashtbl;/* Hash Table */
286 static u_long __read_mostly nchash; /* size of hash table */
287 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0,
288 "Size of namecache hash table");
289 static u_long __read_mostly ncnegfactor = 5; /* ratio of negative entries */
290 SYSCTL_ULONG(_vfs, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0,
291 "Ratio of negative namecache entries");
292 static u_long __exclusive_cache_line numneg; /* number of negative entries allocated */
293 static u_long __exclusive_cache_line numcache;/* number of cache entries allocated */
294 u_int ncsizefactor = 2;
295 SYSCTL_UINT(_vfs, OID_AUTO, ncsizefactor, CTLFLAG_RW, &ncsizefactor, 0,
296 "Size factor for namecache");
297 static u_int __read_mostly ncsize; /* the size as computed on creation or resizing */
299 struct nchstats nchstats; /* cache effectiveness statistics */
301 static bool __read_frequently cache_fast_revlookup = true;
302 SYSCTL_BOOL(_vfs, OID_AUTO, cache_fast_revlookup, CTLFLAG_RW,
303 &cache_fast_revlookup, 0, "");
305 static u_int __exclusive_cache_line neg_cycle;
308 #define numneglists (ncneghash + 1)
311 struct mtx nl_evict_lock;
312 struct mtx nl_lock __aligned(CACHE_LINE_SIZE);
313 TAILQ_HEAD(, namecache) nl_list;
314 TAILQ_HEAD(, namecache) nl_hotlist;
316 } __aligned(CACHE_LINE_SIZE);
318 static struct neglist neglists[numneglists];
320 static inline struct neglist *
321 NCP2NEGLIST(struct namecache *ncp)
324 return (&neglists[(((uintptr_t)(ncp) >> 8) & ncneghash)]);
327 static inline struct negstate *
328 NCP2NEGSTATE(struct namecache *ncp)
331 MPASS(ncp->nc_flag & NCF_NEGATIVE);
332 return (&ncp->nc_neg);
335 #define numbucketlocks (ncbuckethash + 1)
336 static u_int __read_mostly ncbuckethash;
337 static struct mtx_padalign __read_mostly *bucketlocks;
338 #define HASH2BUCKETLOCK(hash) \
339 ((struct mtx *)(&bucketlocks[((hash) & ncbuckethash)]))
341 #define numvnodelocks (ncvnodehash + 1)
342 static u_int __read_mostly ncvnodehash;
343 static struct mtx __read_mostly *vnodelocks;
344 static inline struct mtx *
345 VP2VNODELOCK(struct vnode *vp)
348 return (&vnodelocks[(((uintptr_t)(vp) >> 8) & ncvnodehash)]);
352 * UMA zones for the VFS cache.
354 * The small cache is used for entries with short names, which are the
355 * most common. The large cache is used for entries which are too big to
356 * fit in the small cache.
358 static uma_zone_t __read_mostly cache_zone_small;
359 static uma_zone_t __read_mostly cache_zone_small_ts;
360 static uma_zone_t __read_mostly cache_zone_large;
361 static uma_zone_t __read_mostly cache_zone_large_ts;
363 static struct namecache *
364 cache_alloc(int len, int ts)
366 struct namecache_ts *ncp_ts;
367 struct namecache *ncp;
369 if (__predict_false(ts)) {
370 if (len <= CACHE_PATH_CUTOFF)
371 ncp_ts = uma_zalloc_smr(cache_zone_small_ts, M_WAITOK);
373 ncp_ts = uma_zalloc_smr(cache_zone_large_ts, M_WAITOK);
374 ncp = &ncp_ts->nc_nc;
376 if (len <= CACHE_PATH_CUTOFF)
377 ncp = uma_zalloc_smr(cache_zone_small, M_WAITOK);
379 ncp = uma_zalloc_smr(cache_zone_large, M_WAITOK);
385 cache_free(struct namecache *ncp)
387 struct namecache_ts *ncp_ts;
390 if ((ncp->nc_flag & NCF_DVDROP) != 0)
392 if (__predict_false(ncp->nc_flag & NCF_TS)) {
393 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
394 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
395 uma_zfree_smr(cache_zone_small_ts, ncp_ts);
397 uma_zfree_smr(cache_zone_large_ts, ncp_ts);
399 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
400 uma_zfree_smr(cache_zone_small, ncp);
402 uma_zfree_smr(cache_zone_large, ncp);
407 cache_out_ts(struct namecache *ncp, struct timespec *tsp, int *ticksp)
409 struct namecache_ts *ncp_ts;
411 KASSERT((ncp->nc_flag & NCF_TS) != 0 ||
412 (tsp == NULL && ticksp == NULL),
418 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
419 *tsp = ncp_ts->nc_time;
420 *ticksp = ncp_ts->nc_ticks;
424 static int __read_mostly doingcache = 1; /* 1 => enable the cache */
425 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0,
426 "VFS namecache enabled");
429 /* Export size information to userland */
430 SYSCTL_INT(_debug_sizeof, OID_AUTO, namecache, CTLFLAG_RD, SYSCTL_NULL_INT_PTR,
431 sizeof(struct namecache), "sizeof(struct namecache)");
434 * The new name cache statistics
436 static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
437 "Name cache statistics");
438 #define STATNODE_ULONG(name, descr) \
439 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, descr);
440 #define STATNODE_COUNTER(name, descr) \
441 static COUNTER_U64_DEFINE_EARLY(name); \
442 SYSCTL_COUNTER_U64(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, \
444 STATNODE_ULONG(numneg, "Number of negative cache entries");
445 STATNODE_ULONG(numcache, "Number of cache entries");
446 STATNODE_COUNTER(numcachehv, "Number of namecache entries with vnodes held");
447 STATNODE_COUNTER(numdrops, "Number of dropped entries due to reaching the limit");
448 STATNODE_COUNTER(dothits, "Number of '.' hits");
449 STATNODE_COUNTER(dotdothits, "Number of '..' hits");
450 STATNODE_COUNTER(nummiss, "Number of cache misses");
451 STATNODE_COUNTER(nummisszap, "Number of cache misses we do not want to cache");
452 STATNODE_COUNTER(numposzaps,
453 "Number of cache hits (positive) we do not want to cache");
454 STATNODE_COUNTER(numposhits, "Number of cache hits (positive)");
455 STATNODE_COUNTER(numnegzaps,
456 "Number of cache hits (negative) we do not want to cache");
457 STATNODE_COUNTER(numneghits, "Number of cache hits (negative)");
458 /* These count for vn_getcwd(), too. */
459 STATNODE_COUNTER(numfullpathcalls, "Number of fullpath search calls");
460 STATNODE_COUNTER(numfullpathfail1, "Number of fullpath search errors (ENOTDIR)");
461 STATNODE_COUNTER(numfullpathfail2,
462 "Number of fullpath search errors (VOP_VPTOCNP failures)");
463 STATNODE_COUNTER(numfullpathfail4, "Number of fullpath search errors (ENOMEM)");
464 STATNODE_COUNTER(numfullpathfound, "Number of successful fullpath calls");
465 STATNODE_COUNTER(zap_and_exit_bucket_relock_success,
466 "Number of successful removals after relocking");
467 static long zap_and_exit_bucket_fail; STATNODE_ULONG(zap_and_exit_bucket_fail,
468 "Number of times zap_and_exit failed to lock");
469 static long zap_and_exit_bucket_fail2; STATNODE_ULONG(zap_and_exit_bucket_fail2,
470 "Number of times zap_and_exit failed to lock");
471 static long cache_lock_vnodes_cel_3_failures;
472 STATNODE_ULONG(cache_lock_vnodes_cel_3_failures,
473 "Number of times 3-way vnode locking failed");
475 static void cache_zap_locked(struct namecache *ncp);
476 static int vn_fullpath_hardlink(struct nameidata *ndp, char **retbuf,
477 char **freebuf, size_t *buflen);
478 static int vn_fullpath_any_smr(struct vnode *vp, struct vnode *rdir, char *buf,
479 char **retbuf, size_t *buflen, bool slash_prefixed, size_t addend);
480 static int vn_fullpath_any(struct vnode *vp, struct vnode *rdir, char *buf,
481 char **retbuf, size_t *buflen);
482 static int vn_fullpath_dir(struct vnode *vp, struct vnode *rdir, char *buf,
483 char **retbuf, size_t *len, bool slash_prefixed, size_t addend);
485 static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
488 cache_assert_vlp_locked(struct mtx *vlp)
492 mtx_assert(vlp, MA_OWNED);
496 cache_assert_vnode_locked(struct vnode *vp)
500 vlp = VP2VNODELOCK(vp);
501 cache_assert_vlp_locked(vlp);
505 * TODO: With the value stored we can do better than computing the hash based
506 * on the address. The choice of FNV should also be revisited.
509 cache_prehash(struct vnode *vp)
512 vp->v_nchash = fnv_32_buf(&vp, sizeof(vp), FNV1_32_INIT);
516 cache_get_hash(char *name, u_char len, struct vnode *dvp)
519 return (fnv_32_buf(name, len, dvp->v_nchash));
522 static inline struct nchashhead *
523 NCP2BUCKET(struct namecache *ncp)
527 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen, ncp->nc_dvp);
528 return (NCHHASH(hash));
531 static inline struct mtx *
532 NCP2BUCKETLOCK(struct namecache *ncp)
536 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen, ncp->nc_dvp);
537 return (HASH2BUCKETLOCK(hash));
542 cache_assert_bucket_locked(struct namecache *ncp)
546 blp = NCP2BUCKETLOCK(ncp);
547 mtx_assert(blp, MA_OWNED);
551 cache_assert_bucket_unlocked(struct namecache *ncp)
555 blp = NCP2BUCKETLOCK(ncp);
556 mtx_assert(blp, MA_NOTOWNED);
559 #define cache_assert_bucket_locked(x) do { } while (0)
560 #define cache_assert_bucket_unlocked(x) do { } while (0)
563 #define cache_sort_vnodes(x, y) _cache_sort_vnodes((void **)(x), (void **)(y))
565 _cache_sort_vnodes(void **p1, void **p2)
569 MPASS(*p1 != NULL || *p2 != NULL);
579 cache_lock_all_buckets(void)
583 for (i = 0; i < numbucketlocks; i++)
584 mtx_lock(&bucketlocks[i]);
588 cache_unlock_all_buckets(void)
592 for (i = 0; i < numbucketlocks; i++)
593 mtx_unlock(&bucketlocks[i]);
597 cache_lock_all_vnodes(void)
601 for (i = 0; i < numvnodelocks; i++)
602 mtx_lock(&vnodelocks[i]);
606 cache_unlock_all_vnodes(void)
610 for (i = 0; i < numvnodelocks; i++)
611 mtx_unlock(&vnodelocks[i]);
615 cache_trylock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
618 cache_sort_vnodes(&vlp1, &vlp2);
621 if (!mtx_trylock(vlp1))
624 if (!mtx_trylock(vlp2)) {
634 cache_lock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
637 MPASS(vlp1 != NULL || vlp2 != NULL);
647 cache_unlock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
650 MPASS(vlp1 != NULL || vlp2 != NULL);
659 sysctl_nchstats(SYSCTL_HANDLER_ARGS)
661 struct nchstats snap;
663 if (req->oldptr == NULL)
664 return (SYSCTL_OUT(req, 0, sizeof(snap)));
667 snap.ncs_goodhits = counter_u64_fetch(numposhits);
668 snap.ncs_neghits = counter_u64_fetch(numneghits);
669 snap.ncs_badhits = counter_u64_fetch(numposzaps) +
670 counter_u64_fetch(numnegzaps);
671 snap.ncs_miss = counter_u64_fetch(nummisszap) +
672 counter_u64_fetch(nummiss);
674 return (SYSCTL_OUT(req, &snap, sizeof(snap)));
676 SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE | CTLFLAG_RD |
677 CTLFLAG_MPSAFE, 0, 0, sysctl_nchstats, "LU",
678 "VFS cache effectiveness statistics");
682 * Grab an atomic snapshot of the name cache hash chain lengths
684 static SYSCTL_NODE(_debug, OID_AUTO, hashstat,
685 CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
689 sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS)
691 struct nchashhead *ncpp;
692 struct namecache *ncp;
693 int i, error, n_nchash, *cntbuf;
696 n_nchash = nchash + 1; /* nchash is max index, not count */
697 if (req->oldptr == NULL)
698 return SYSCTL_OUT(req, 0, n_nchash * sizeof(int));
699 cntbuf = malloc(n_nchash * sizeof(int), M_TEMP, M_ZERO | M_WAITOK);
700 cache_lock_all_buckets();
701 if (n_nchash != nchash + 1) {
702 cache_unlock_all_buckets();
703 free(cntbuf, M_TEMP);
706 /* Scan hash tables counting entries */
707 for (ncpp = nchashtbl, i = 0; i < n_nchash; ncpp++, i++)
708 CK_SLIST_FOREACH(ncp, ncpp, nc_hash)
710 cache_unlock_all_buckets();
711 for (error = 0, i = 0; i < n_nchash; i++)
712 if ((error = SYSCTL_OUT(req, &cntbuf[i], sizeof(int))) != 0)
714 free(cntbuf, M_TEMP);
717 SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD|
718 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_rawnchash, "S,int",
719 "nchash chain lengths");
722 sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS)
725 struct nchashhead *ncpp;
726 struct namecache *ncp;
728 int count, maxlength, used, pct;
731 return SYSCTL_OUT(req, 0, 4 * sizeof(int));
733 cache_lock_all_buckets();
734 n_nchash = nchash + 1; /* nchash is max index, not count */
738 /* Scan hash tables for applicable entries */
739 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
741 CK_SLIST_FOREACH(ncp, ncpp, nc_hash) {
746 if (maxlength < count)
749 n_nchash = nchash + 1;
750 cache_unlock_all_buckets();
751 pct = (used * 100) / (n_nchash / 100);
752 error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash));
755 error = SYSCTL_OUT(req, &used, sizeof(used));
758 error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength));
761 error = SYSCTL_OUT(req, &pct, sizeof(pct));
766 SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD|
767 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_nchash, "I",
768 "nchash statistics (number of total/used buckets, maximum chain length, usage percentage)");
772 * Negative entries management
774 * Various workloads create plenty of negative entries and barely use them
775 * afterwards. Moreover malicious users can keep performing bogus lookups
776 * adding even more entries. For example "make tinderbox" as of writing this
777 * comment ends up with 2.6M namecache entries in total, 1.2M of which are
780 * As such, a rather aggressive eviction method is needed. The currently
781 * employed method is a placeholder.
783 * Entries are split over numneglists separate lists, each of which is further
784 * split into hot and cold entries. Entries get promoted after getting a hit.
785 * Eviction happens on addition of new entry.
787 static SYSCTL_NODE(_vfs_cache, OID_AUTO, neg, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
788 "Name cache negative entry statistics");
790 SYSCTL_ULONG(_vfs_cache_neg, OID_AUTO, count, CTLFLAG_RD, &numneg, 0,
791 "Number of negative cache entries");
793 static COUNTER_U64_DEFINE_EARLY(neg_created);
794 SYSCTL_COUNTER_U64(_vfs_cache_neg, OID_AUTO, created, CTLFLAG_RD, &neg_created,
795 "Number of created negative entries");
797 static COUNTER_U64_DEFINE_EARLY(neg_evicted);
798 SYSCTL_COUNTER_U64(_vfs_cache_neg, OID_AUTO, evicted, CTLFLAG_RD, &neg_evicted,
799 "Number of evicted negative entries");
801 static COUNTER_U64_DEFINE_EARLY(neg_evict_skipped_empty);
802 SYSCTL_COUNTER_U64(_vfs_cache_neg, OID_AUTO, evict_skipped_empty, CTLFLAG_RD,
803 &neg_evict_skipped_empty,
804 "Number of times evicting failed due to lack of entries");
806 static COUNTER_U64_DEFINE_EARLY(neg_evict_skipped_missed);
807 SYSCTL_COUNTER_U64(_vfs_cache_neg, OID_AUTO, evict_skipped_missed, CTLFLAG_RD,
808 &neg_evict_skipped_missed,
809 "Number of times evicting failed due to target entry disappearing");
811 static COUNTER_U64_DEFINE_EARLY(neg_evict_skipped_contended);
812 SYSCTL_COUNTER_U64(_vfs_cache_neg, OID_AUTO, evict_skipped_contended, CTLFLAG_RD,
813 &neg_evict_skipped_contended,
814 "Number of times evicting failed due to contention");
816 SYSCTL_COUNTER_U64(_vfs_cache_neg, OID_AUTO, hits, CTLFLAG_RD, &numneghits,
817 "Number of cache hits (negative)");
820 sysctl_neg_hot(SYSCTL_HANDLER_ARGS)
825 for (i = 0; i < numneglists; i++)
826 out += neglists[i].nl_hotnum;
828 return (SYSCTL_OUT(req, &out, sizeof(out)));
830 SYSCTL_PROC(_vfs_cache_neg, OID_AUTO, hot, CTLTYPE_INT | CTLFLAG_RD |
831 CTLFLAG_MPSAFE, 0, 0, sysctl_neg_hot, "I",
832 "Number of hot negative entries");
835 cache_neg_init(struct namecache *ncp)
839 ncp->nc_flag |= NCF_NEGATIVE;
840 ns = NCP2NEGSTATE(ncp);
842 counter_u64_add(neg_created, 1);
846 * Move a negative entry to the hot list.
849 cache_neg_promote(struct namecache *ncp)
854 ns = NCP2NEGSTATE(ncp);
855 nl = NCP2NEGLIST(ncp);
856 mtx_assert(&nl->nl_lock, MA_OWNED);
857 if ((ns->neg_flag & NEG_HOT) == 0) {
858 TAILQ_REMOVE(&nl->nl_list, ncp, nc_dst);
859 TAILQ_INSERT_TAIL(&nl->nl_hotlist, ncp, nc_dst);
861 ns->neg_flag |= NEG_HOT;
866 * Move a negative entry to the hot list if it matches the lookup.
868 * We have to take locks, but they may be contended and in the worst
869 * case we may need to go off CPU. We don't want to spin within the
870 * smr section and we can't block with it. Exiting the section means
871 * the found entry could have been evicted. We are going to look it
875 cache_neg_promote_cond(struct vnode *dvp, struct componentname *cnp,
876 struct namecache *oncp, uint32_t hash)
878 struct namecache *ncp;
882 nl = NCP2NEGLIST(oncp);
884 mtx_lock(&nl->nl_lock);
886 * For hash iteration.
891 * Avoid all surprises by only succeeding if we got the same entry and
892 * bailing completely otherwise.
893 * XXX There are no provisions to keep the vnode around, meaning we may
894 * end up promoting a negative entry for a *new* vnode and returning
895 * ENOENT on its account. This is the error we want to return anyway
896 * and promotion is harmless.
898 * In particular at this point there can be a new ncp which matches the
899 * search but hashes to a different neglist.
901 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
907 * No match to begin with.
909 if (__predict_false(ncp == NULL)) {
914 * The newly found entry may be something different...
916 if (!(ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
917 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))) {
922 * ... and not even negative.
924 nc_flag = atomic_load_char(&ncp->nc_flag);
925 if ((nc_flag & NCF_NEGATIVE) == 0) {
929 if (__predict_false(!cache_ncp_canuse(ncp))) {
933 cache_neg_promote(ncp);
935 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp, ncp->nc_name);
936 counter_u64_add(numneghits, 1);
938 mtx_unlock(&nl->nl_lock);
942 mtx_unlock(&nl->nl_lock);
947 cache_neg_hit(struct namecache *ncp)
952 ns = NCP2NEGSTATE(ncp);
953 if ((ns->neg_flag & NEG_HOT) != 0)
955 nl = NCP2NEGLIST(ncp);
956 mtx_lock(&nl->nl_lock);
957 cache_neg_promote(ncp);
958 mtx_unlock(&nl->nl_lock);
962 cache_neg_insert(struct namecache *ncp)
966 MPASS(ncp->nc_flag & NCF_NEGATIVE);
967 cache_assert_bucket_locked(ncp);
968 nl = NCP2NEGLIST(ncp);
969 mtx_lock(&nl->nl_lock);
970 TAILQ_INSERT_TAIL(&nl->nl_list, ncp, nc_dst);
971 mtx_unlock(&nl->nl_lock);
972 atomic_add_long(&numneg, 1);
976 cache_neg_remove(struct namecache *ncp)
981 cache_assert_bucket_locked(ncp);
982 nl = NCP2NEGLIST(ncp);
983 ns = NCP2NEGSTATE(ncp);
984 mtx_lock(&nl->nl_lock);
985 if ((ns->neg_flag & NEG_HOT) != 0) {
986 TAILQ_REMOVE(&nl->nl_hotlist, ncp, nc_dst);
989 TAILQ_REMOVE(&nl->nl_list, ncp, nc_dst);
991 mtx_unlock(&nl->nl_lock);
992 atomic_subtract_long(&numneg, 1);
995 static struct neglist *
996 cache_neg_evict_select(void)
1001 c = atomic_fetchadd_int(&neg_cycle, 1) + 1;
1002 nl = &neglists[c % numneglists];
1003 if (!mtx_trylock(&nl->nl_evict_lock)) {
1004 counter_u64_add(neg_evict_skipped_contended, 1);
1011 cache_neg_evict(void)
1013 struct namecache *ncp, *ncp2;
1015 struct negstate *ns;
1022 nl = cache_neg_evict_select();
1027 mtx_lock(&nl->nl_lock);
1028 ncp = TAILQ_FIRST(&nl->nl_hotlist);
1030 ns = NCP2NEGSTATE(ncp);
1031 TAILQ_REMOVE(&nl->nl_hotlist, ncp, nc_dst);
1032 TAILQ_INSERT_TAIL(&nl->nl_list, ncp, nc_dst);
1034 ns->neg_flag &= ~NEG_HOT;
1036 ncp = TAILQ_FIRST(&nl->nl_list);
1038 counter_u64_add(neg_evict_skipped_empty, 1);
1039 mtx_unlock(&nl->nl_lock);
1040 mtx_unlock(&nl->nl_evict_lock);
1043 ns = NCP2NEGSTATE(ncp);
1044 nlen = ncp->nc_nlen;
1046 hash = cache_get_hash(ncp->nc_name, nlen, dvp);
1047 dvlp = VP2VNODELOCK(dvp);
1048 blp = HASH2BUCKETLOCK(hash);
1049 mtx_unlock(&nl->nl_lock);
1050 mtx_unlock(&nl->nl_evict_lock);
1054 * Note that since all locks were dropped above, the entry may be
1055 * gone or reallocated to be something else.
1057 CK_SLIST_FOREACH(ncp2, (NCHHASH(hash)), nc_hash) {
1058 if (ncp2 == ncp && ncp2->nc_dvp == dvp &&
1059 ncp2->nc_nlen == nlen && (ncp2->nc_flag & NCF_NEGATIVE) != 0)
1063 counter_u64_add(neg_evict_skipped_missed, 1);
1066 MPASS(dvlp == VP2VNODELOCK(ncp->nc_dvp));
1067 MPASS(blp == NCP2BUCKETLOCK(ncp));
1068 SDT_PROBE2(vfs, namecache, evict_negative, done, ncp->nc_dvp,
1070 cache_zap_locked(ncp);
1071 counter_u64_add(neg_evicted, 1);
1080 * cache_zap_locked():
1082 * Removes a namecache entry from cache, whether it contains an actual
1083 * pointer to a vnode or if it is just a negative cache entry.
1086 cache_zap_locked(struct namecache *ncp)
1088 struct nchashhead *ncpp;
1090 if (!(ncp->nc_flag & NCF_NEGATIVE))
1091 cache_assert_vnode_locked(ncp->nc_vp);
1092 cache_assert_vnode_locked(ncp->nc_dvp);
1093 cache_assert_bucket_locked(ncp);
1095 cache_ncp_invalidate(ncp);
1097 ncpp = NCP2BUCKET(ncp);
1098 CK_SLIST_REMOVE(ncpp, ncp, namecache, nc_hash);
1099 if (!(ncp->nc_flag & NCF_NEGATIVE)) {
1100 SDT_PROBE3(vfs, namecache, zap, done, ncp->nc_dvp,
1101 ncp->nc_name, ncp->nc_vp);
1102 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
1103 if (ncp == ncp->nc_vp->v_cache_dd) {
1104 vn_seqc_write_begin_unheld(ncp->nc_vp);
1105 ncp->nc_vp->v_cache_dd = NULL;
1106 vn_seqc_write_end(ncp->nc_vp);
1109 SDT_PROBE2(vfs, namecache, zap_negative, done, ncp->nc_dvp,
1111 cache_neg_remove(ncp);
1113 if (ncp->nc_flag & NCF_ISDOTDOT) {
1114 if (ncp == ncp->nc_dvp->v_cache_dd) {
1115 vn_seqc_write_begin_unheld(ncp->nc_dvp);
1116 ncp->nc_dvp->v_cache_dd = NULL;
1117 vn_seqc_write_end(ncp->nc_dvp);
1120 LIST_REMOVE(ncp, nc_src);
1121 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) {
1122 ncp->nc_flag |= NCF_DVDROP;
1123 counter_u64_add(numcachehv, -1);
1126 atomic_subtract_long(&numcache, 1);
1130 cache_zap_negative_locked_vnode_kl(struct namecache *ncp, struct vnode *vp)
1134 MPASS(ncp->nc_dvp == vp);
1135 MPASS(ncp->nc_flag & NCF_NEGATIVE);
1136 cache_assert_vnode_locked(vp);
1138 blp = NCP2BUCKETLOCK(ncp);
1140 cache_zap_locked(ncp);
1145 cache_zap_locked_vnode_kl2(struct namecache *ncp, struct vnode *vp,
1148 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
1151 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
1152 cache_assert_vnode_locked(vp);
1154 if (ncp->nc_flag & NCF_NEGATIVE) {
1155 if (*vlpp != NULL) {
1159 cache_zap_negative_locked_vnode_kl(ncp, vp);
1163 pvlp = VP2VNODELOCK(vp);
1164 blp = NCP2BUCKETLOCK(ncp);
1165 vlp1 = VP2VNODELOCK(ncp->nc_dvp);
1166 vlp2 = VP2VNODELOCK(ncp->nc_vp);
1168 if (*vlpp == vlp1 || *vlpp == vlp2) {
1172 if (*vlpp != NULL) {
1176 cache_sort_vnodes(&vlp1, &vlp2);
1181 if (!mtx_trylock(vlp1))
1187 cache_zap_locked(ncp);
1189 if (to_unlock != NULL)
1190 mtx_unlock(to_unlock);
1197 MPASS(*vlpp == NULL);
1203 * If trylocking failed we can get here. We know enough to take all needed locks
1204 * in the right order and re-lookup the entry.
1207 cache_zap_unlocked_bucket(struct namecache *ncp, struct componentname *cnp,
1208 struct vnode *dvp, struct mtx *dvlp, struct mtx *vlp, uint32_t hash,
1211 struct namecache *rncp;
1213 cache_assert_bucket_unlocked(ncp);
1215 cache_sort_vnodes(&dvlp, &vlp);
1216 cache_lock_vnodes(dvlp, vlp);
1218 CK_SLIST_FOREACH(rncp, (NCHHASH(hash)), nc_hash) {
1219 if (rncp == ncp && rncp->nc_dvp == dvp &&
1220 rncp->nc_nlen == cnp->cn_namelen &&
1221 !bcmp(rncp->nc_name, cnp->cn_nameptr, rncp->nc_nlen))
1225 cache_zap_locked(rncp);
1227 cache_unlock_vnodes(dvlp, vlp);
1228 counter_u64_add(zap_and_exit_bucket_relock_success, 1);
1233 cache_unlock_vnodes(dvlp, vlp);
1237 static int __noinline
1238 cache_zap_locked_bucket(struct namecache *ncp, struct componentname *cnp,
1239 uint32_t hash, struct mtx *blp)
1241 struct mtx *dvlp, *vlp;
1244 cache_assert_bucket_locked(ncp);
1246 dvlp = VP2VNODELOCK(ncp->nc_dvp);
1248 if (!(ncp->nc_flag & NCF_NEGATIVE))
1249 vlp = VP2VNODELOCK(ncp->nc_vp);
1250 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1251 cache_zap_locked(ncp);
1253 cache_unlock_vnodes(dvlp, vlp);
1259 return (cache_zap_unlocked_bucket(ncp, cnp, dvp, dvlp, vlp, hash, blp));
1262 static __noinline int
1263 cache_remove_cnp(struct vnode *dvp, struct componentname *cnp)
1265 struct namecache *ncp;
1267 struct mtx *dvlp, *dvlp2;
1271 if (cnp->cn_namelen == 2 &&
1272 cnp->cn_nameptr[0] == '.' && cnp->cn_nameptr[1] == '.') {
1273 dvlp = VP2VNODELOCK(dvp);
1277 ncp = dvp->v_cache_dd;
1282 SDT_PROBE2(vfs, namecache, removecnp, miss, dvp, cnp);
1285 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1286 if (!cache_zap_locked_vnode_kl2(ncp, dvp, &dvlp2))
1288 MPASS(dvp->v_cache_dd == NULL);
1294 vn_seqc_write_begin(dvp);
1295 dvp->v_cache_dd = NULL;
1296 vn_seqc_write_end(dvp);
1301 SDT_PROBE2(vfs, namecache, removecnp, hit, dvp, cnp);
1305 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1306 blp = HASH2BUCKETLOCK(hash);
1308 if (CK_SLIST_EMPTY(NCHHASH(hash)))
1313 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1314 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1315 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1324 error = cache_zap_locked_bucket(ncp, cnp, hash, blp);
1325 if (__predict_false(error != 0)) {
1326 zap_and_exit_bucket_fail++;
1329 counter_u64_add(numposzaps, 1);
1330 SDT_PROBE2(vfs, namecache, removecnp, hit, dvp, cnp);
1334 counter_u64_add(nummisszap, 1);
1335 SDT_PROBE2(vfs, namecache, removecnp, miss, dvp, cnp);
1339 static int __noinline
1340 cache_lookup_dot(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1341 struct timespec *tsp, int *ticksp)
1346 counter_u64_add(dothits, 1);
1347 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", *vpp);
1354 * When we lookup "." we still can be asked to lock it
1357 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
1358 if (ltype != VOP_ISLOCKED(*vpp)) {
1359 if (ltype == LK_EXCLUSIVE) {
1360 vn_lock(*vpp, LK_UPGRADE | LK_RETRY);
1361 if (VN_IS_DOOMED((*vpp))) {
1362 /* forced unmount */
1368 vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY);
1373 static int __noinline
1374 cache_lookup_dotdot(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1375 struct timespec *tsp, int *ticksp)
1377 struct namecache_ts *ncp_ts;
1378 struct namecache *ncp;
1384 MPASS((cnp->cn_flags & ISDOTDOT) != 0);
1386 if ((cnp->cn_flags & MAKEENTRY) == 0) {
1387 cache_remove_cnp(dvp, cnp);
1391 counter_u64_add(dotdothits, 1);
1393 dvlp = VP2VNODELOCK(dvp);
1395 ncp = dvp->v_cache_dd;
1397 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, "..", NULL);
1401 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1402 if (ncp->nc_flag & NCF_NEGATIVE)
1409 goto negative_success;
1410 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, "..", *vpp);
1411 cache_out_ts(ncp, tsp, ticksp);
1412 if ((ncp->nc_flag & (NCF_ISDOTDOT | NCF_DTS)) ==
1413 NCF_DTS && tsp != NULL) {
1414 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1415 *tsp = ncp_ts->nc_dotdottime;
1419 ltype = VOP_ISLOCKED(dvp);
1421 vs = vget_prep(*vpp);
1423 error = vget_finish(*vpp, cnp->cn_lkflags, vs);
1424 vn_lock(dvp, ltype | LK_RETRY);
1425 if (VN_IS_DOOMED(dvp)) {
1437 if (__predict_false(cnp->cn_nameiop == CREATE)) {
1438 if (cnp->cn_flags & ISLASTCN) {
1439 counter_u64_add(numnegzaps, 1);
1440 cache_zap_negative_locked_vnode_kl(ncp, dvp);
1447 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp, ncp->nc_name);
1448 cache_out_ts(ncp, tsp, ticksp);
1449 counter_u64_add(numneghits, 1);
1450 whiteout = (ncp->nc_flag & NCF_WHITE);
1454 cnp->cn_flags |= ISWHITEOUT;
1459 * Lookup a name in the name cache
1463 * - dvp: Parent directory in which to search.
1464 * - vpp: Return argument. Will contain desired vnode on cache hit.
1465 * - cnp: Parameters of the name search. The most interesting bits of
1466 * the cn_flags field have the following meanings:
1467 * - MAKEENTRY: If clear, free an entry from the cache rather than look
1469 * - ISDOTDOT: Must be set if and only if cn_nameptr == ".."
1470 * - tsp: Return storage for cache timestamp. On a successful (positive
1471 * or negative) lookup, tsp will be filled with any timespec that
1472 * was stored when this cache entry was created. However, it will
1473 * be clear for "." entries.
1474 * - ticks: Return storage for alternate cache timestamp. On a successful
1475 * (positive or negative) lookup, it will contain the ticks value
1476 * that was current when the cache entry was created, unless cnp
1479 * Either both tsp and ticks have to be provided or neither of them.
1483 * - -1: A positive cache hit. vpp will contain the desired vnode.
1484 * - ENOENT: A negative cache hit, or dvp was recycled out from under us due
1485 * to a forced unmount. vpp will not be modified. If the entry
1486 * is a whiteout, then the ISWHITEOUT flag will be set in
1488 * - 0: A cache miss. vpp will not be modified.
1492 * On a cache hit, vpp will be returned locked and ref'd. If we're looking up
1493 * .., dvp is unlocked. If we're looking up . an extra ref is taken, but the
1494 * lock is not recursively acquired.
1496 static int __noinline
1497 cache_lookup_fallback(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1498 struct timespec *tsp, int *ticksp)
1500 struct namecache *ncp;
1507 MPASS((cnp->cn_flags & (MAKEENTRY | ISDOTDOT)) == MAKEENTRY);
1510 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1511 blp = HASH2BUCKETLOCK(hash);
1514 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1515 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1516 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1520 if (__predict_false(ncp == NULL)) {
1522 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
1524 counter_u64_add(nummiss, 1);
1528 if (ncp->nc_flag & NCF_NEGATIVE)
1529 goto negative_success;
1531 counter_u64_add(numposhits, 1);
1533 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name, *vpp);
1534 cache_out_ts(ncp, tsp, ticksp);
1536 vs = vget_prep(*vpp);
1538 error = vget_finish(*vpp, cnp->cn_lkflags, vs);
1545 if (__predict_false(cnp->cn_nameiop == CREATE)) {
1546 if (cnp->cn_flags & ISLASTCN) {
1547 counter_u64_add(numnegzaps, 1);
1548 error = cache_zap_locked_bucket(ncp, cnp, hash, blp);
1549 if (__predict_false(error != 0)) {
1550 zap_and_exit_bucket_fail2++;
1558 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp, ncp->nc_name);
1559 cache_out_ts(ncp, tsp, ticksp);
1560 counter_u64_add(numneghits, 1);
1561 whiteout = (ncp->nc_flag & NCF_WHITE);
1565 cnp->cn_flags |= ISWHITEOUT;
1570 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1571 struct timespec *tsp, int *ticksp)
1573 struct namecache *ncp;
1574 struct negstate *ns;
1578 bool whiteout, neg_hot;
1581 MPASS((tsp == NULL && ticksp == NULL) || (tsp != NULL && ticksp != NULL));
1584 if (__predict_false(!doingcache)) {
1585 cnp->cn_flags &= ~MAKEENTRY;
1590 if (__predict_false(cnp->cn_nameptr[0] == '.')) {
1591 if (cnp->cn_namelen == 1)
1592 return (cache_lookup_dot(dvp, vpp, cnp, tsp, ticksp));
1593 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.')
1594 return (cache_lookup_dotdot(dvp, vpp, cnp, tsp, ticksp));
1597 MPASS((cnp->cn_flags & ISDOTDOT) == 0);
1599 if ((cnp->cn_flags & MAKEENTRY) == 0) {
1600 cache_remove_cnp(dvp, cnp);
1604 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1607 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1608 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1609 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1613 if (__predict_false(ncp == NULL)) {
1615 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
1617 counter_u64_add(nummiss, 1);
1621 nc_flag = atomic_load_char(&ncp->nc_flag);
1622 if (nc_flag & NCF_NEGATIVE)
1623 goto negative_success;
1625 counter_u64_add(numposhits, 1);
1627 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name, *vpp);
1628 cache_out_ts(ncp, tsp, ticksp);
1630 if (!cache_ncp_canuse(ncp)) {
1635 vs = vget_prep_smr(*vpp);
1637 if (__predict_false(vs == VGET_NONE)) {
1641 error = vget_finish(*vpp, cnp->cn_lkflags, vs);
1648 if (__predict_false(cnp->cn_nameiop == CREATE)) {
1649 if (cnp->cn_flags & ISLASTCN) {
1655 cache_out_ts(ncp, tsp, ticksp);
1656 whiteout = (ncp->nc_flag & NCF_WHITE);
1657 ns = NCP2NEGSTATE(ncp);
1658 neg_hot = ((ns->neg_flag & NEG_HOT) != 0);
1659 if (__predict_false(!cache_ncp_canuse(ncp))) {
1665 if (!cache_neg_promote_cond(dvp, cnp, ncp, hash))
1668 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp, ncp->nc_name);
1669 counter_u64_add(numneghits, 1);
1673 cnp->cn_flags |= ISWHITEOUT;
1676 return (cache_lookup_fallback(dvp, vpp, cnp, tsp, ticksp));
1679 struct celockstate {
1683 CTASSERT((nitems(((struct celockstate *)0)->vlp) == 3));
1684 CTASSERT((nitems(((struct celockstate *)0)->blp) == 2));
1687 cache_celockstate_init(struct celockstate *cel)
1690 bzero(cel, sizeof(*cel));
1694 cache_lock_vnodes_cel(struct celockstate *cel, struct vnode *vp,
1697 struct mtx *vlp1, *vlp2;
1699 MPASS(cel->vlp[0] == NULL);
1700 MPASS(cel->vlp[1] == NULL);
1701 MPASS(cel->vlp[2] == NULL);
1703 MPASS(vp != NULL || dvp != NULL);
1705 vlp1 = VP2VNODELOCK(vp);
1706 vlp2 = VP2VNODELOCK(dvp);
1707 cache_sort_vnodes(&vlp1, &vlp2);
1718 cache_unlock_vnodes_cel(struct celockstate *cel)
1721 MPASS(cel->vlp[0] != NULL || cel->vlp[1] != NULL);
1723 if (cel->vlp[0] != NULL)
1724 mtx_unlock(cel->vlp[0]);
1725 if (cel->vlp[1] != NULL)
1726 mtx_unlock(cel->vlp[1]);
1727 if (cel->vlp[2] != NULL)
1728 mtx_unlock(cel->vlp[2]);
1732 cache_lock_vnodes_cel_3(struct celockstate *cel, struct vnode *vp)
1737 cache_assert_vlp_locked(cel->vlp[0]);
1738 cache_assert_vlp_locked(cel->vlp[1]);
1739 MPASS(cel->vlp[2] == NULL);
1742 vlp = VP2VNODELOCK(vp);
1745 if (vlp >= cel->vlp[1]) {
1748 if (mtx_trylock(vlp))
1750 cache_lock_vnodes_cel_3_failures++;
1751 cache_unlock_vnodes_cel(cel);
1752 if (vlp < cel->vlp[0]) {
1754 mtx_lock(cel->vlp[0]);
1755 mtx_lock(cel->vlp[1]);
1757 if (cel->vlp[0] != NULL)
1758 mtx_lock(cel->vlp[0]);
1760 mtx_lock(cel->vlp[1]);
1770 cache_lock_buckets_cel(struct celockstate *cel, struct mtx *blp1,
1774 MPASS(cel->blp[0] == NULL);
1775 MPASS(cel->blp[1] == NULL);
1777 cache_sort_vnodes(&blp1, &blp2);
1788 cache_unlock_buckets_cel(struct celockstate *cel)
1791 if (cel->blp[0] != NULL)
1792 mtx_unlock(cel->blp[0]);
1793 mtx_unlock(cel->blp[1]);
1797 * Lock part of the cache affected by the insertion.
1799 * This means vnodelocks for dvp, vp and the relevant bucketlock.
1800 * However, insertion can result in removal of an old entry. In this
1801 * case we have an additional vnode and bucketlock pair to lock.
1803 * That is, in the worst case we have to lock 3 vnodes and 2 bucketlocks, while
1804 * preserving the locking order (smaller address first).
1807 cache_enter_lock(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1810 struct namecache *ncp;
1811 struct mtx *blps[2];
1813 blps[0] = HASH2BUCKETLOCK(hash);
1816 cache_lock_vnodes_cel(cel, dvp, vp);
1817 if (vp == NULL || vp->v_type != VDIR)
1819 ncp = vp->v_cache_dd;
1822 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1824 MPASS(ncp->nc_dvp == vp);
1825 blps[1] = NCP2BUCKETLOCK(ncp);
1826 if (ncp->nc_flag & NCF_NEGATIVE)
1828 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1831 * All vnodes got re-locked. Re-validate the state and if
1832 * nothing changed we are done. Otherwise restart.
1834 if (ncp == vp->v_cache_dd &&
1835 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1836 blps[1] == NCP2BUCKETLOCK(ncp) &&
1837 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1839 cache_unlock_vnodes_cel(cel);
1844 cache_lock_buckets_cel(cel, blps[0], blps[1]);
1848 cache_enter_lock_dd(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1851 struct namecache *ncp;
1852 struct mtx *blps[2];
1854 blps[0] = HASH2BUCKETLOCK(hash);
1857 cache_lock_vnodes_cel(cel, dvp, vp);
1858 ncp = dvp->v_cache_dd;
1861 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1863 MPASS(ncp->nc_dvp == dvp);
1864 blps[1] = NCP2BUCKETLOCK(ncp);
1865 if (ncp->nc_flag & NCF_NEGATIVE)
1867 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1869 if (ncp == dvp->v_cache_dd &&
1870 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1871 blps[1] == NCP2BUCKETLOCK(ncp) &&
1872 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1874 cache_unlock_vnodes_cel(cel);
1879 cache_lock_buckets_cel(cel, blps[0], blps[1]);
1883 cache_enter_unlock(struct celockstate *cel)
1886 cache_unlock_buckets_cel(cel);
1887 cache_unlock_vnodes_cel(cel);
1890 static void __noinline
1891 cache_enter_dotdot_prep(struct vnode *dvp, struct vnode *vp,
1892 struct componentname *cnp)
1894 struct celockstate cel;
1895 struct namecache *ncp;
1899 if (dvp->v_cache_dd == NULL)
1901 len = cnp->cn_namelen;
1902 cache_celockstate_init(&cel);
1903 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1904 cache_enter_lock_dd(&cel, dvp, vp, hash);
1905 vn_seqc_write_begin(dvp);
1906 ncp = dvp->v_cache_dd;
1907 if (ncp != NULL && (ncp->nc_flag & NCF_ISDOTDOT)) {
1908 KASSERT(ncp->nc_dvp == dvp, ("wrong isdotdot parent"));
1909 cache_zap_locked(ncp);
1913 dvp->v_cache_dd = NULL;
1914 vn_seqc_write_end(dvp);
1915 cache_enter_unlock(&cel);
1921 * Add an entry to the cache.
1924 cache_enter_time(struct vnode *dvp, struct vnode *vp, struct componentname *cnp,
1925 struct timespec *tsp, struct timespec *dtsp)
1927 struct celockstate cel;
1928 struct namecache *ncp, *n2, *ndd;
1929 struct namecache_ts *ncp_ts;
1930 struct nchashhead *ncpp;
1936 VNPASS(!VN_IS_DOOMED(dvp), dvp);
1937 VNPASS(dvp->v_type != VNON, dvp);
1939 VNPASS(!VN_IS_DOOMED(vp), vp);
1940 VNPASS(vp->v_type != VNON, vp);
1944 if (__predict_false(!doingcache))
1949 if (__predict_false(cnp->cn_nameptr[0] == '.')) {
1950 if (cnp->cn_namelen == 1)
1952 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
1953 cache_enter_dotdot_prep(dvp, vp, cnp);
1954 flag = NCF_ISDOTDOT;
1959 * Avoid blowout in namecache entries.
1962 * 1. filesystems may end up tryng to add an already existing entry
1963 * (for example this can happen after a cache miss during concurrent
1964 * lookup), in which case we will call cache_neg_evict despite not
1966 * 2. the routine may fail to free anything and no provisions are made
1967 * to make it try harder (see the inside for failure modes)
1968 * 3. it only ever looks at negative entries.
1970 lnumcache = atomic_fetchadd_long(&numcache, 1) + 1;
1971 if (numneg * ncnegfactor > lnumcache) {
1973 lnumcache = atomic_load_long(&numcache);
1975 if (__predict_false(lnumcache >= ncsize)) {
1976 atomic_subtract_long(&numcache, 1);
1977 counter_u64_add(numdrops, 1);
1981 cache_celockstate_init(&cel);
1986 * Calculate the hash key and setup as much of the new
1987 * namecache entry as possible before acquiring the lock.
1989 ncp = cache_alloc(cnp->cn_namelen, tsp != NULL);
1990 ncp->nc_flag = flag | NCF_WIP;
1993 cache_neg_init(ncp);
1996 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1997 ncp_ts->nc_time = *tsp;
1998 ncp_ts->nc_ticks = ticks;
1999 ncp_ts->nc_nc.nc_flag |= NCF_TS;
2001 ncp_ts->nc_dotdottime = *dtsp;
2002 ncp_ts->nc_nc.nc_flag |= NCF_DTS;
2005 len = ncp->nc_nlen = cnp->cn_namelen;
2006 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
2007 memcpy(ncp->nc_name, cnp->cn_nameptr, len);
2008 ncp->nc_name[len] = '\0';
2009 cache_enter_lock(&cel, dvp, vp, hash);
2012 * See if this vnode or negative entry is already in the cache
2013 * with this name. This can happen with concurrent lookups of
2014 * the same path name.
2016 ncpp = NCHHASH(hash);
2017 CK_SLIST_FOREACH(n2, ncpp, nc_hash) {
2018 if (n2->nc_dvp == dvp &&
2019 n2->nc_nlen == cnp->cn_namelen &&
2020 !bcmp(n2->nc_name, cnp->cn_nameptr, n2->nc_nlen)) {
2021 MPASS(cache_ncp_canuse(n2));
2022 if ((n2->nc_flag & NCF_NEGATIVE) != 0)
2024 ("%s: found entry pointing to a different vnode (%p != %p)",
2025 __func__, NULL, vp));
2027 KASSERT(n2->nc_vp == vp,
2028 ("%s: found entry pointing to a different vnode (%p != %p)",
2029 __func__, n2->nc_vp, vp));
2031 * Entries are supposed to be immutable unless in the
2032 * process of getting destroyed. Accommodating for
2033 * changing timestamps is possible but not worth it.
2034 * This should be harmless in terms of correctness, in
2035 * the worst case resulting in an earlier expiration.
2036 * Alternatively, the found entry can be replaced
2039 MPASS((n2->nc_flag & (NCF_TS | NCF_DTS)) == (ncp->nc_flag & (NCF_TS | NCF_DTS)));
2042 KASSERT((n2->nc_flag & NCF_TS) != 0,
2044 n2_ts = __containerof(n2, struct namecache_ts, nc_nc);
2045 n2_ts->nc_time = ncp_ts->nc_time;
2046 n2_ts->nc_ticks = ncp_ts->nc_ticks;
2048 n2_ts->nc_dotdottime = ncp_ts->nc_dotdottime;
2049 n2_ts->nc_nc.nc_flag |= NCF_DTS;
2053 SDT_PROBE3(vfs, namecache, enter, duplicate, dvp, ncp->nc_name,
2055 goto out_unlock_free;
2059 if (flag == NCF_ISDOTDOT) {
2061 * See if we are trying to add .. entry, but some other lookup
2062 * has populated v_cache_dd pointer already.
2064 if (dvp->v_cache_dd != NULL)
2065 goto out_unlock_free;
2066 KASSERT(vp == NULL || vp->v_type == VDIR,
2067 ("wrong vnode type %p", vp));
2068 vn_seqc_write_begin(dvp);
2069 dvp->v_cache_dd = ncp;
2070 vn_seqc_write_end(dvp);
2074 if (flag != NCF_ISDOTDOT) {
2076 * For this case, the cache entry maps both the
2077 * directory name in it and the name ".." for the
2078 * directory's parent.
2080 vn_seqc_write_begin(vp);
2081 if ((ndd = vp->v_cache_dd) != NULL) {
2082 if ((ndd->nc_flag & NCF_ISDOTDOT) != 0)
2083 cache_zap_locked(ndd);
2087 vp->v_cache_dd = ncp;
2088 vn_seqc_write_end(vp);
2089 } else if (vp->v_type != VDIR) {
2090 if (vp->v_cache_dd != NULL) {
2091 vn_seqc_write_begin(vp);
2092 vp->v_cache_dd = NULL;
2093 vn_seqc_write_end(vp);
2098 if (flag != NCF_ISDOTDOT) {
2099 if (LIST_EMPTY(&dvp->v_cache_src)) {
2101 counter_u64_add(numcachehv, 1);
2103 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
2107 * If the entry is "negative", we place it into the
2108 * "negative" cache queue, otherwise, we place it into the
2109 * destination vnode's cache entries queue.
2112 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
2113 SDT_PROBE3(vfs, namecache, enter, done, dvp, ncp->nc_name,
2116 if (cnp->cn_flags & ISWHITEOUT)
2117 ncp->nc_flag |= NCF_WHITE;
2118 cache_neg_insert(ncp);
2119 SDT_PROBE2(vfs, namecache, enter_negative, done, dvp,
2124 * Insert the new namecache entry into the appropriate chain
2125 * within the cache entries table.
2127 CK_SLIST_INSERT_HEAD(ncpp, ncp, nc_hash);
2129 atomic_thread_fence_rel();
2131 * Mark the entry as fully constructed.
2132 * It is immutable past this point until its removal.
2134 atomic_store_char(&ncp->nc_flag, ncp->nc_flag & ~NCF_WIP);
2136 cache_enter_unlock(&cel);
2141 cache_enter_unlock(&cel);
2142 atomic_subtract_long(&numcache, 1);
2148 cache_roundup_2(u_int val)
2152 for (res = 1; res <= val; res <<= 1)
2158 static struct nchashhead *
2159 nchinittbl(u_long elements, u_long *hashmask)
2161 struct nchashhead *hashtbl;
2164 hashsize = cache_roundup_2(elements) / 2;
2166 hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), M_VFSCACHE, M_WAITOK);
2167 for (i = 0; i < hashsize; i++)
2168 CK_SLIST_INIT(&hashtbl[i]);
2169 *hashmask = hashsize - 1;
2174 ncfreetbl(struct nchashhead *hashtbl)
2177 free(hashtbl, M_VFSCACHE);
2181 * Name cache initialization, from vfs_init() when we are booting
2184 nchinit(void *dummy __unused)
2188 cache_zone_small = uma_zcreate("S VFS Cache", CACHE_ZONE_SMALL_SIZE,
2189 NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT, UMA_ZONE_ZINIT);
2190 cache_zone_small_ts = uma_zcreate("STS VFS Cache", CACHE_ZONE_SMALL_TS_SIZE,
2191 NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT, UMA_ZONE_ZINIT);
2192 cache_zone_large = uma_zcreate("L VFS Cache", CACHE_ZONE_LARGE_SIZE,
2193 NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT, UMA_ZONE_ZINIT);
2194 cache_zone_large_ts = uma_zcreate("LTS VFS Cache", CACHE_ZONE_LARGE_TS_SIZE,
2195 NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT, UMA_ZONE_ZINIT);
2197 VFS_SMR_ZONE_SET(cache_zone_small);
2198 VFS_SMR_ZONE_SET(cache_zone_small_ts);
2199 VFS_SMR_ZONE_SET(cache_zone_large);
2200 VFS_SMR_ZONE_SET(cache_zone_large_ts);
2202 ncsize = desiredvnodes * ncsizefactor;
2203 nchashtbl = nchinittbl(desiredvnodes * 2, &nchash);
2204 ncbuckethash = cache_roundup_2(mp_ncpus * mp_ncpus) - 1;
2205 if (ncbuckethash < 7) /* arbitrarily chosen to avoid having one lock */
2207 if (ncbuckethash > nchash)
2208 ncbuckethash = nchash;
2209 bucketlocks = malloc(sizeof(*bucketlocks) * numbucketlocks, M_VFSCACHE,
2211 for (i = 0; i < numbucketlocks; i++)
2212 mtx_init(&bucketlocks[i], "ncbuc", NULL, MTX_DUPOK | MTX_RECURSE);
2213 ncvnodehash = ncbuckethash;
2214 vnodelocks = malloc(sizeof(*vnodelocks) * numvnodelocks, M_VFSCACHE,
2216 for (i = 0; i < numvnodelocks; i++)
2217 mtx_init(&vnodelocks[i], "ncvn", NULL, MTX_DUPOK | MTX_RECURSE);
2219 for (i = 0; i < numneglists; i++) {
2220 mtx_init(&neglists[i].nl_evict_lock, "ncnege", NULL, MTX_DEF);
2221 mtx_init(&neglists[i].nl_lock, "ncnegl", NULL, MTX_DEF);
2222 TAILQ_INIT(&neglists[i].nl_list);
2223 TAILQ_INIT(&neglists[i].nl_hotlist);
2226 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL);
2229 cache_vnode_init(struct vnode *vp)
2232 LIST_INIT(&vp->v_cache_src);
2233 TAILQ_INIT(&vp->v_cache_dst);
2234 vp->v_cache_dd = NULL;
2239 cache_changesize(u_long newmaxvnodes)
2241 struct nchashhead *new_nchashtbl, *old_nchashtbl;
2242 u_long new_nchash, old_nchash;
2243 struct namecache *ncp;
2248 newncsize = newmaxvnodes * ncsizefactor;
2249 newmaxvnodes = cache_roundup_2(newmaxvnodes * 2);
2250 if (newmaxvnodes < numbucketlocks)
2251 newmaxvnodes = numbucketlocks;
2253 new_nchashtbl = nchinittbl(newmaxvnodes, &new_nchash);
2254 /* If same hash table size, nothing to do */
2255 if (nchash == new_nchash) {
2256 ncfreetbl(new_nchashtbl);
2260 * Move everything from the old hash table to the new table.
2261 * None of the namecache entries in the table can be removed
2262 * because to do so, they have to be removed from the hash table.
2264 cache_lock_all_vnodes();
2265 cache_lock_all_buckets();
2266 old_nchashtbl = nchashtbl;
2267 old_nchash = nchash;
2268 nchashtbl = new_nchashtbl;
2269 nchash = new_nchash;
2270 for (i = 0; i <= old_nchash; i++) {
2271 while ((ncp = CK_SLIST_FIRST(&old_nchashtbl[i])) != NULL) {
2272 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen,
2274 CK_SLIST_REMOVE(&old_nchashtbl[i], ncp, namecache, nc_hash);
2275 CK_SLIST_INSERT_HEAD(NCHHASH(hash), ncp, nc_hash);
2279 cache_unlock_all_buckets();
2280 cache_unlock_all_vnodes();
2281 ncfreetbl(old_nchashtbl);
2285 * Invalidate all entries from and to a particular vnode.
2288 cache_purge_impl(struct vnode *vp)
2290 TAILQ_HEAD(, namecache) ncps;
2291 struct namecache *ncp, *nnp;
2292 struct mtx *vlp, *vlp2;
2295 vlp = VP2VNODELOCK(vp);
2299 while (!LIST_EMPTY(&vp->v_cache_src)) {
2300 ncp = LIST_FIRST(&vp->v_cache_src);
2301 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2303 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2305 while (!TAILQ_EMPTY(&vp->v_cache_dst)) {
2306 ncp = TAILQ_FIRST(&vp->v_cache_dst);
2307 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2309 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2311 ncp = vp->v_cache_dd;
2313 KASSERT(ncp->nc_flag & NCF_ISDOTDOT,
2314 ("lost dotdot link"));
2315 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2317 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2319 KASSERT(vp->v_cache_dd == NULL, ("incomplete purge"));
2323 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2329 * Opportunistic check to see if there is anything to do.
2332 cache_has_entries(struct vnode *vp)
2335 if (LIST_EMPTY(&vp->v_cache_src) && TAILQ_EMPTY(&vp->v_cache_dst) &&
2336 vp->v_cache_dd == NULL)
2342 cache_purge(struct vnode *vp)
2345 SDT_PROBE1(vfs, namecache, purge, done, vp);
2346 if (!cache_has_entries(vp))
2348 cache_purge_impl(vp);
2352 * Only to be used by vgone.
2355 cache_purge_vgone(struct vnode *vp)
2359 VNPASS(VN_IS_DOOMED(vp), vp);
2360 if (cache_has_entries(vp)) {
2361 cache_purge_impl(vp);
2366 * Serialize against a potential thread doing cache_purge.
2368 vlp = VP2VNODELOCK(vp);
2369 mtx_wait_unlocked(vlp);
2370 if (cache_has_entries(vp)) {
2371 cache_purge_impl(vp);
2378 * Invalidate all negative entries for a particular directory vnode.
2381 cache_purge_negative(struct vnode *vp)
2383 TAILQ_HEAD(, namecache) ncps;
2384 struct namecache *ncp, *nnp;
2387 SDT_PROBE1(vfs, namecache, purge_negative, done, vp);
2388 if (LIST_EMPTY(&vp->v_cache_src))
2391 vlp = VP2VNODELOCK(vp);
2393 LIST_FOREACH_SAFE(ncp, &vp->v_cache_src, nc_src, nnp) {
2394 if (!(ncp->nc_flag & NCF_NEGATIVE))
2396 cache_zap_negative_locked_vnode_kl(ncp, vp);
2397 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2400 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2406 cache_rename(struct vnode *fdvp, struct vnode *fvp, struct vnode *tdvp,
2407 struct vnode *tvp, struct componentname *fcnp, struct componentname *tcnp)
2410 ASSERT_VOP_IN_SEQC(fdvp);
2411 ASSERT_VOP_IN_SEQC(fvp);
2412 ASSERT_VOP_IN_SEQC(tdvp);
2414 ASSERT_VOP_IN_SEQC(tvp);
2419 KASSERT(!cache_remove_cnp(tdvp, tcnp),
2420 ("%s: lingering negative entry", __func__));
2422 cache_remove_cnp(tdvp, tcnp);
2427 * Flush all entries referencing a particular filesystem.
2430 cache_purgevfs(struct mount *mp)
2432 struct vnode *vp, *mvp;
2434 SDT_PROBE1(vfs, namecache, purgevfs, done, mp);
2436 * Somewhat wasteful iteration over all vnodes. Would be better to
2437 * support filtering and avoid the interlock to begin with.
2439 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
2440 if (!cache_has_entries(vp)) {
2452 * Perform canonical checks and cache lookup and pass on to filesystem
2453 * through the vop_cachedlookup only if needed.
2457 vfs_cache_lookup(struct vop_lookup_args *ap)
2461 struct vnode **vpp = ap->a_vpp;
2462 struct componentname *cnp = ap->a_cnp;
2463 int flags = cnp->cn_flags;
2468 if (dvp->v_type != VDIR)
2471 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
2472 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
2475 error = vn_dir_check_exec(dvp, cnp);
2479 error = cache_lookup(dvp, vpp, cnp, NULL, NULL);
2481 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
2487 /* Implementation of the getcwd syscall. */
2489 sys___getcwd(struct thread *td, struct __getcwd_args *uap)
2495 buflen = uap->buflen;
2496 if (__predict_false(buflen < 2))
2498 if (buflen > MAXPATHLEN)
2499 buflen = MAXPATHLEN;
2501 buf = uma_zalloc(namei_zone, M_WAITOK);
2502 error = vn_getcwd(buf, &retbuf, &buflen);
2504 error = copyout(retbuf, uap->buf, buflen);
2505 uma_zfree(namei_zone, buf);
2510 vn_getcwd(char *buf, char **retbuf, size_t *buflen)
2516 pwd = pwd_get_smr();
2517 error = vn_fullpath_any_smr(pwd->pwd_cdir, pwd->pwd_rdir, buf, retbuf,
2519 VFS_SMR_ASSERT_NOT_ENTERED();
2521 pwd = pwd_hold(curthread);
2522 error = vn_fullpath_any(pwd->pwd_cdir, pwd->pwd_rdir, buf,
2528 if (KTRPOINT(curthread, KTR_NAMEI) && error == 0)
2535 kern___realpathat(struct thread *td, int fd, const char *path, char *buf,
2536 size_t size, int flags, enum uio_seg pathseg)
2538 struct nameidata nd;
2539 char *retbuf, *freebuf;
2544 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | SAVENAME | WANTPARENT | AUDITVNODE1,
2545 pathseg, path, fd, &cap_fstat_rights, td);
2546 if ((error = namei(&nd)) != 0)
2548 error = vn_fullpath_hardlink(&nd, &retbuf, &freebuf, &size);
2550 error = copyout(retbuf, buf, size);
2551 free(freebuf, M_TEMP);
2558 sys___realpathat(struct thread *td, struct __realpathat_args *uap)
2561 return (kern___realpathat(td, uap->fd, uap->path, uap->buf, uap->size,
2562 uap->flags, UIO_USERSPACE));
2566 * Retrieve the full filesystem path that correspond to a vnode from the name
2567 * cache (if available)
2570 vn_fullpath(struct vnode *vp, char **retbuf, char **freebuf)
2577 if (__predict_false(vp == NULL))
2580 buflen = MAXPATHLEN;
2581 buf = malloc(buflen, M_TEMP, M_WAITOK);
2583 pwd = pwd_get_smr();
2584 error = vn_fullpath_any_smr(vp, pwd->pwd_rdir, buf, retbuf, &buflen, false, 0);
2585 VFS_SMR_ASSERT_NOT_ENTERED();
2587 pwd = pwd_hold(curthread);
2588 error = vn_fullpath_any(vp, pwd->pwd_rdir, buf, retbuf, &buflen);
2599 * This function is similar to vn_fullpath, but it attempts to lookup the
2600 * pathname relative to the global root mount point. This is required for the
2601 * auditing sub-system, as audited pathnames must be absolute, relative to the
2602 * global root mount point.
2605 vn_fullpath_global(struct vnode *vp, char **retbuf, char **freebuf)
2611 if (__predict_false(vp == NULL))
2613 buflen = MAXPATHLEN;
2614 buf = malloc(buflen, M_TEMP, M_WAITOK);
2616 error = vn_fullpath_any_smr(vp, rootvnode, buf, retbuf, &buflen, false, 0);
2617 VFS_SMR_ASSERT_NOT_ENTERED();
2619 error = vn_fullpath_any(vp, rootvnode, buf, retbuf, &buflen);
2628 static struct namecache *
2629 vn_dd_from_dst(struct vnode *vp)
2631 struct namecache *ncp;
2633 cache_assert_vnode_locked(vp);
2634 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst) {
2635 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2642 vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, size_t *buflen)
2645 struct namecache *ncp;
2649 vlp = VP2VNODELOCK(*vp);
2651 ncp = (*vp)->v_cache_dd;
2652 if (ncp != NULL && (ncp->nc_flag & NCF_ISDOTDOT) == 0) {
2653 KASSERT(ncp == vn_dd_from_dst(*vp),
2654 ("%s: mismatch for dd entry (%p != %p)", __func__,
2655 ncp, vn_dd_from_dst(*vp)));
2657 ncp = vn_dd_from_dst(*vp);
2660 if (*buflen < ncp->nc_nlen) {
2663 counter_u64_add(numfullpathfail4, 1);
2665 SDT_PROBE3(vfs, namecache, fullpath, return, error,
2669 *buflen -= ncp->nc_nlen;
2670 memcpy(buf + *buflen, ncp->nc_name, ncp->nc_nlen);
2671 SDT_PROBE3(vfs, namecache, fullpath, hit, ncp->nc_dvp,
2680 SDT_PROBE1(vfs, namecache, fullpath, miss, vp);
2683 vn_lock(*vp, LK_SHARED | LK_RETRY);
2684 error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen);
2687 counter_u64_add(numfullpathfail2, 1);
2688 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2693 if (VN_IS_DOOMED(dvp)) {
2694 /* forced unmount */
2697 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2701 * *vp has its use count incremented still.
2708 * Resolve a directory to a pathname.
2710 * The name of the directory can always be found in the namecache or fetched
2711 * from the filesystem. There is also guaranteed to be only one parent, meaning
2712 * we can just follow vnodes up until we find the root.
2714 * The vnode must be referenced.
2717 vn_fullpath_dir(struct vnode *vp, struct vnode *rdir, char *buf, char **retbuf,
2718 size_t *len, bool slash_prefixed, size_t addend)
2720 #ifdef KDTRACE_HOOKS
2721 struct vnode *startvp = vp;
2727 VNPASS(vp->v_type == VDIR || VN_IS_DOOMED(vp), vp);
2728 VNPASS(vp->v_usecount > 0, vp);
2732 if (!slash_prefixed) {
2740 SDT_PROBE1(vfs, namecache, fullpath, entry, vp);
2741 counter_u64_add(numfullpathcalls, 1);
2742 while (vp != rdir && vp != rootvnode) {
2744 * The vp vnode must be already fully constructed,
2745 * since it is either found in namecache or obtained
2746 * from VOP_VPTOCNP(). We may test for VV_ROOT safely
2747 * without obtaining the vnode lock.
2749 if ((vp->v_vflag & VV_ROOT) != 0) {
2750 vn_lock(vp, LK_RETRY | LK_SHARED);
2753 * With the vnode locked, check for races with
2754 * unmount, forced or not. Note that we
2755 * already verified that vp is not equal to
2756 * the root vnode, which means that
2757 * mnt_vnodecovered can be NULL only for the
2760 if (VN_IS_DOOMED(vp) ||
2761 (vp1 = vp->v_mount->mnt_vnodecovered) == NULL ||
2762 vp1->v_mountedhere != vp->v_mount) {
2765 SDT_PROBE3(vfs, namecache, fullpath, return,
2775 if (vp->v_type != VDIR) {
2777 counter_u64_add(numfullpathfail1, 1);
2779 SDT_PROBE3(vfs, namecache, fullpath, return,
2783 error = vn_vptocnp(&vp, curthread->td_ucred, buf, &buflen);
2789 SDT_PROBE3(vfs, namecache, fullpath, return, error,
2793 buf[--buflen] = '/';
2794 slash_prefixed = true;
2798 if (!slash_prefixed) {
2801 counter_u64_add(numfullpathfail4, 1);
2802 SDT_PROBE3(vfs, namecache, fullpath, return, ENOMEM,
2806 buf[--buflen] = '/';
2808 counter_u64_add(numfullpathfound, 1);
2811 *retbuf = buf + buflen;
2812 SDT_PROBE3(vfs, namecache, fullpath, return, 0, startvp, *retbuf);
2819 * Resolve an arbitrary vnode to a pathname.
2822 * - hardlinks are not tracked, thus if the vnode is not a directory this can
2823 * resolve to a different path than the one used to find it
2824 * - namecache is not mandatory, meaning names are not guaranteed to be added
2825 * (in which case resolving fails)
2827 static void __inline
2828 cache_rev_failed_impl(int *reason, int line)
2833 #define cache_rev_failed(var) cache_rev_failed_impl((var), __LINE__)
2836 vn_fullpath_any_smr(struct vnode *vp, struct vnode *rdir, char *buf,
2837 char **retbuf, size_t *buflen, bool slash_prefixed, size_t addend)
2839 #ifdef KDTRACE_HOOKS
2840 struct vnode *startvp = vp;
2844 struct namecache *ncp;
2848 #ifdef KDTRACE_HOOKS
2851 seqc_t vp_seqc, tvp_seqc;
2854 VFS_SMR_ASSERT_ENTERED();
2856 if (!cache_fast_revlookup) {
2861 orig_buflen = *buflen;
2863 if (!slash_prefixed) {
2864 MPASS(*buflen >= 2);
2866 buf[*buflen] = '\0';
2869 if (vp == rdir || vp == rootvnode) {
2870 if (!slash_prefixed) {
2877 #ifdef KDTRACE_HOOKS
2881 ncp = NULL; /* for sdt probe down below */
2882 vp_seqc = vn_seqc_read_any(vp);
2883 if (seqc_in_modify(vp_seqc)) {
2884 cache_rev_failed(&reason);
2889 #ifdef KDTRACE_HOOKS
2892 if ((vp->v_vflag & VV_ROOT) != 0) {
2893 mp = atomic_load_ptr(&vp->v_mount);
2895 cache_rev_failed(&reason);
2898 tvp = atomic_load_ptr(&mp->mnt_vnodecovered);
2899 tvp_seqc = vn_seqc_read_any(tvp);
2900 if (seqc_in_modify(tvp_seqc)) {
2901 cache_rev_failed(&reason);
2904 if (!vn_seqc_consistent(vp, vp_seqc)) {
2905 cache_rev_failed(&reason);
2912 ncp = atomic_load_ptr(&vp->v_cache_dd);
2914 cache_rev_failed(&reason);
2917 nc_flag = atomic_load_char(&ncp->nc_flag);
2918 if ((nc_flag & NCF_ISDOTDOT) != 0) {
2919 cache_rev_failed(&reason);
2922 if (!cache_ncp_canuse(ncp)) {
2923 cache_rev_failed(&reason);
2926 if (ncp->nc_nlen >= *buflen) {
2927 cache_rev_failed(&reason);
2931 *buflen -= ncp->nc_nlen;
2932 memcpy(buf + *buflen, ncp->nc_name, ncp->nc_nlen);
2936 tvp_seqc = vn_seqc_read_any(tvp);
2937 if (seqc_in_modify(tvp_seqc)) {
2938 cache_rev_failed(&reason);
2941 if (!vn_seqc_consistent(vp, vp_seqc)) {
2942 cache_rev_failed(&reason);
2947 if (vp == rdir || vp == rootvnode)
2952 *retbuf = buf + *buflen;
2953 *buflen = orig_buflen - *buflen + addend;
2954 SDT_PROBE2(vfs, namecache, fullpath_smr, hit, startvp, *retbuf);
2958 *buflen = orig_buflen;
2959 SDT_PROBE4(vfs, namecache, fullpath_smr, miss, startvp, ncp, reason, i);
2965 vn_fullpath_any(struct vnode *vp, struct vnode *rdir, char *buf, char **retbuf,
2969 bool slash_prefixed;
2975 orig_buflen = *buflen;
2978 slash_prefixed = false;
2979 if (vp->v_type != VDIR) {
2981 buf[*buflen] = '\0';
2982 error = vn_vptocnp(&vp, curthread->td_ucred, buf, buflen);
2991 slash_prefixed = true;
2994 return (vn_fullpath_dir(vp, rdir, buf, retbuf, buflen, slash_prefixed,
2995 orig_buflen - *buflen));
2999 * Resolve an arbitrary vnode to a pathname (taking care of hardlinks).
3001 * Since the namecache does not track handlings, the caller is expected to first
3002 * look up the target vnode with SAVENAME | WANTPARENT flags passed to namei.
3004 * Then we have 2 cases:
3005 * - if the found vnode is a directory, the path can be constructed just by
3006 * fullowing names up the chain
3007 * - otherwise we populate the buffer with the saved name and start resolving
3011 vn_fullpath_hardlink(struct nameidata *ndp, char **retbuf, char **freebuf,
3016 struct componentname *cnp;
3020 bool slash_prefixed;
3025 if (*buflen > MAXPATHLEN)
3026 *buflen = MAXPATHLEN;
3028 slash_prefixed = false;
3030 buf = malloc(*buflen, M_TEMP, M_WAITOK);
3035 * Check for VBAD to work around the vp_crossmp bug in lookup().
3037 * For example consider tmpfs on /tmp and realpath /tmp. ni_vp will be
3038 * set to mount point's root vnode while ni_dvp will be vp_crossmp.
3039 * If the type is VDIR (like in this very case) we can skip looking
3040 * at ni_dvp in the first place. However, since vnodes get passed here
3041 * unlocked the target may transition to doomed state (type == VBAD)
3042 * before we get to evaluate the condition. If this happens, we will
3043 * populate part of the buffer and descend to vn_fullpath_dir with
3044 * vp == vp_crossmp. Prevent the problem by checking for VBAD.
3046 * This should be atomic_load(&vp->v_type) but it is ilegal to take
3047 * an address of a bit field, even if said field is sized to char.
3048 * Work around the problem by reading the value into a full-sized enum
3049 * and then re-reading it with atomic_load which will still prevent
3050 * the compiler from re-reading down the road.
3053 type = atomic_load_int(&type);
3060 addend = cnp->cn_namelen + 2;
3061 if (*buflen < addend) {
3066 tmpbuf = buf + *buflen;
3068 memcpy(&tmpbuf[1], cnp->cn_nameptr, cnp->cn_namelen);
3069 tmpbuf[addend - 1] = '\0';
3070 slash_prefixed = true;
3075 pwd = pwd_get_smr();
3076 error = vn_fullpath_any_smr(vp, pwd->pwd_rdir, buf, retbuf, buflen,
3077 slash_prefixed, addend);
3078 VFS_SMR_ASSERT_NOT_ENTERED();
3080 pwd = pwd_hold(curthread);
3082 error = vn_fullpath_dir(vp, pwd->pwd_rdir, buf, retbuf, buflen,
3083 slash_prefixed, addend);
3098 vn_dir_dd_ino(struct vnode *vp)
3100 struct namecache *ncp;
3105 ASSERT_VOP_LOCKED(vp, "vn_dir_dd_ino");
3106 vlp = VP2VNODELOCK(vp);
3108 TAILQ_FOREACH(ncp, &(vp->v_cache_dst), nc_dst) {
3109 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0)
3112 vs = vget_prep(ddvp);
3114 if (vget_finish(ddvp, LK_SHARED | LK_NOWAIT, vs))
3123 vn_commname(struct vnode *vp, char *buf, u_int buflen)
3125 struct namecache *ncp;
3129 vlp = VP2VNODELOCK(vp);
3131 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst)
3132 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
3138 l = min(ncp->nc_nlen, buflen - 1);
3139 memcpy(buf, ncp->nc_name, l);
3146 * This function updates path string to vnode's full global path
3147 * and checks the size of the new path string against the pathlen argument.
3149 * Requires a locked, referenced vnode.
3150 * Vnode is re-locked on success or ENODEV, otherwise unlocked.
3152 * If vp is a directory, the call to vn_fullpath_global() always succeeds
3153 * because it falls back to the ".." lookup if the namecache lookup fails.
3156 vn_path_to_global_path(struct thread *td, struct vnode *vp, char *path,
3159 struct nameidata nd;
3164 ASSERT_VOP_ELOCKED(vp, __func__);
3166 /* Construct global filesystem path from vp. */
3168 error = vn_fullpath_global(vp, &rpath, &fbuf);
3175 if (strlen(rpath) >= pathlen) {
3177 error = ENAMETOOLONG;
3182 * Re-lookup the vnode by path to detect a possible rename.
3183 * As a side effect, the vnode is relocked.
3184 * If vnode was renamed, return ENOENT.
3186 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
3187 UIO_SYSSPACE, path, td);
3193 NDFREE(&nd, NDF_ONLY_PNBUF);
3197 strcpy(path, rpath);
3210 db_print_vpath(struct vnode *vp)
3213 while (vp != NULL) {
3214 db_printf("%p: ", vp);
3215 if (vp == rootvnode) {
3219 if (vp->v_vflag & VV_ROOT) {
3220 db_printf("<mount point>");
3221 vp = vp->v_mount->mnt_vnodecovered;
3223 struct namecache *ncp;
3227 ncp = TAILQ_FIRST(&vp->v_cache_dst);
3230 for (i = 0; i < ncp->nc_nlen; i++)
3231 db_printf("%c", *ncn++);
3244 DB_SHOW_COMMAND(vpath, db_show_vpath)
3249 db_printf("usage: show vpath <struct vnode *>\n");
3253 vp = (struct vnode *)addr;
3259 static bool __read_frequently cache_fast_lookup = true;
3260 SYSCTL_BOOL(_vfs, OID_AUTO, cache_fast_lookup, CTLFLAG_RW,
3261 &cache_fast_lookup, 0, "");
3263 #define CACHE_FPL_FAILED -2020
3266 cache_fpl_cleanup_cnp(struct componentname *cnp)
3269 uma_zfree(namei_zone, cnp->cn_pnbuf);
3271 cnp->cn_pnbuf = NULL;
3272 cnp->cn_nameptr = NULL;
3277 cache_fpl_handle_root(struct nameidata *ndp, struct vnode **dpp)
3279 struct componentname *cnp;
3282 while (*(cnp->cn_nameptr) == '/') {
3287 *dpp = ndp->ni_rootdir;
3291 * Components of nameidata (or objects it can point to) which may
3292 * need restoring in case fast path lookup fails.
3294 struct nameidata_saved {
3302 struct nameidata *ndp;
3303 struct componentname *cnp;
3309 struct nameidata_saved snd;
3311 enum cache_fpl_status status:8;
3317 cache_fpl_checkpoint(struct cache_fpl *fpl, struct nameidata_saved *snd)
3320 snd->cn_flags = fpl->ndp->ni_cnd.cn_flags;
3321 snd->cn_namelen = fpl->ndp->ni_cnd.cn_namelen;
3322 snd->cn_nameptr = fpl->ndp->ni_cnd.cn_nameptr;
3323 snd->ni_pathlen = fpl->ndp->ni_pathlen;
3327 cache_fpl_restore(struct cache_fpl *fpl, struct nameidata_saved *snd)
3330 fpl->ndp->ni_cnd.cn_flags = snd->cn_flags;
3331 fpl->ndp->ni_cnd.cn_namelen = snd->cn_namelen;
3332 fpl->ndp->ni_cnd.cn_nameptr = snd->cn_nameptr;
3333 fpl->ndp->ni_pathlen = snd->ni_pathlen;
3337 #define cache_fpl_smr_assert_entered(fpl) ({ \
3338 struct cache_fpl *_fpl = (fpl); \
3339 MPASS(_fpl->in_smr == true); \
3340 VFS_SMR_ASSERT_ENTERED(); \
3342 #define cache_fpl_smr_assert_not_entered(fpl) ({ \
3343 struct cache_fpl *_fpl = (fpl); \
3344 MPASS(_fpl->in_smr == false); \
3345 VFS_SMR_ASSERT_NOT_ENTERED(); \
3348 #define cache_fpl_smr_assert_entered(fpl) do { } while (0)
3349 #define cache_fpl_smr_assert_not_entered(fpl) do { } while (0)
3352 #define cache_fpl_smr_enter_initial(fpl) ({ \
3353 struct cache_fpl *_fpl = (fpl); \
3355 _fpl->in_smr = true; \
3358 #define cache_fpl_smr_enter(fpl) ({ \
3359 struct cache_fpl *_fpl = (fpl); \
3360 MPASS(_fpl->in_smr == false); \
3362 _fpl->in_smr = true; \
3365 #define cache_fpl_smr_exit(fpl) ({ \
3366 struct cache_fpl *_fpl = (fpl); \
3367 MPASS(_fpl->in_smr == true); \
3369 _fpl->in_smr = false; \
3373 cache_fpl_aborted_impl(struct cache_fpl *fpl, int line)
3376 if (fpl->status != CACHE_FPL_STATUS_UNSET) {
3377 KASSERT(fpl->status == CACHE_FPL_STATUS_PARTIAL,
3378 ("%s: converting to abort from %d at %d, set at %d\n",
3379 __func__, fpl->status, line, fpl->line));
3381 fpl->status = CACHE_FPL_STATUS_ABORTED;
3383 return (CACHE_FPL_FAILED);
3386 #define cache_fpl_aborted(x) cache_fpl_aborted_impl((x), __LINE__)
3389 cache_fpl_partial_impl(struct cache_fpl *fpl, int line)
3392 KASSERT(fpl->status == CACHE_FPL_STATUS_UNSET,
3393 ("%s: setting to partial at %d, but already set to %d at %d\n",
3394 __func__, line, fpl->status, fpl->line));
3395 cache_fpl_smr_assert_entered(fpl);
3396 fpl->status = CACHE_FPL_STATUS_PARTIAL;
3398 return (CACHE_FPL_FAILED);
3401 #define cache_fpl_partial(x) cache_fpl_partial_impl((x), __LINE__)
3404 cache_fpl_handled_impl(struct cache_fpl *fpl, int error, int line)
3407 KASSERT(fpl->status == CACHE_FPL_STATUS_UNSET,
3408 ("%s: setting to handled at %d, but already set to %d at %d\n",
3409 __func__, line, fpl->status, fpl->line));
3410 cache_fpl_smr_assert_not_entered(fpl);
3411 MPASS(error != CACHE_FPL_FAILED);
3412 fpl->status = CACHE_FPL_STATUS_HANDLED;
3417 #define cache_fpl_handled(x, e) cache_fpl_handled_impl((x), (e), __LINE__)
3419 #define CACHE_FPL_SUPPORTED_CN_FLAGS \
3420 (LOCKLEAF | LOCKPARENT | WANTPARENT | NOCACHE | FOLLOW | LOCKSHARED | SAVENAME | \
3421 SAVESTART | WILLBEDIR | ISOPEN | NOMACCHECK | AUDITVNODE1 | AUDITVNODE2 | NOCAPCHECK)
3423 #define CACHE_FPL_INTERNAL_CN_FLAGS \
3424 (ISDOTDOT | MAKEENTRY | ISLASTCN)
3426 _Static_assert((CACHE_FPL_SUPPORTED_CN_FLAGS & CACHE_FPL_INTERNAL_CN_FLAGS) == 0,
3427 "supported and internal flags overlap");
3430 cache_fpl_islastcn(struct nameidata *ndp)
3433 return (*ndp->ni_next == 0);
3437 cache_fpl_isdotdot(struct componentname *cnp)
3440 if (cnp->cn_namelen == 2 &&
3441 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
3447 cache_can_fplookup(struct cache_fpl *fpl)
3449 struct nameidata *ndp;
3450 struct componentname *cnp;
3455 td = cnp->cn_thread;
3457 if (!cache_fast_lookup) {
3458 cache_fpl_aborted(fpl);
3462 if (mac_vnode_check_lookup_enabled()) {
3463 cache_fpl_aborted(fpl);
3467 if ((cnp->cn_flags & ~CACHE_FPL_SUPPORTED_CN_FLAGS) != 0) {
3468 cache_fpl_aborted(fpl);
3471 if (IN_CAPABILITY_MODE(td)) {
3472 cache_fpl_aborted(fpl);
3475 if (AUDITING_TD(td)) {
3476 cache_fpl_aborted(fpl);
3479 if (ndp->ni_startdir != NULL) {
3480 cache_fpl_aborted(fpl);
3487 cache_fplookup_dirfd(struct cache_fpl *fpl, struct vnode **vpp)
3489 struct nameidata *ndp;
3494 error = fgetvp_lookup_smr(ndp->ni_dirfd, ndp, vpp, &fsearch);
3495 if (__predict_false(error != 0)) {
3496 cache_fpl_smr_exit(fpl);
3497 return (cache_fpl_aborted(fpl));
3499 fpl->fsearch = fsearch;
3504 cache_fplookup_vnode_supported(struct vnode *vp)
3507 return (vp->v_type != VLNK);
3510 static int __noinline
3511 cache_fplookup_negative_promote(struct cache_fpl *fpl, struct namecache *oncp,
3514 struct componentname *cnp;
3520 cache_fpl_smr_exit(fpl);
3521 if (cache_neg_promote_cond(dvp, cnp, oncp, hash))
3522 return (cache_fpl_handled(fpl, ENOENT));
3524 return (cache_fpl_aborted(fpl));
3528 * The target vnode is not supported, prepare for the slow path to take over.
3530 static int __noinline
3531 cache_fplookup_partial_setup(struct cache_fpl *fpl)
3533 struct nameidata *ndp;
3534 struct componentname *cnp;
3544 dvp_seqc = fpl->dvp_seqc;
3546 if (!pwd_hold_smr(pwd)) {
3547 cache_fpl_smr_exit(fpl);
3548 return (cache_fpl_aborted(fpl));
3551 dvs = vget_prep_smr(dvp);
3552 cache_fpl_smr_exit(fpl);
3553 if (__predict_false(dvs == VGET_NONE)) {
3555 return (cache_fpl_aborted(fpl));
3558 vget_finish_ref(dvp, dvs);
3559 if (!vn_seqc_consistent(dvp, dvp_seqc)) {
3562 return (cache_fpl_aborted(fpl));
3565 cache_fpl_restore(fpl, &fpl->snd);
3567 ndp->ni_startdir = dvp;
3568 cnp->cn_flags |= MAKEENTRY;
3569 if (cache_fpl_islastcn(ndp))
3570 cnp->cn_flags |= ISLASTCN;
3571 if (cache_fpl_isdotdot(cnp))
3572 cnp->cn_flags |= ISDOTDOT;
3578 cache_fplookup_final_child(struct cache_fpl *fpl, enum vgetstate tvs)
3580 struct componentname *cnp;
3587 tvp_seqc = fpl->tvp_seqc;
3589 if ((cnp->cn_flags & LOCKLEAF) != 0) {
3590 lkflags = LK_SHARED;
3591 if ((cnp->cn_flags & LOCKSHARED) == 0)
3592 lkflags = LK_EXCLUSIVE;
3593 error = vget_finish(tvp, lkflags, tvs);
3594 if (__predict_false(error != 0)) {
3595 return (cache_fpl_aborted(fpl));
3598 vget_finish_ref(tvp, tvs);
3601 if (!vn_seqc_consistent(tvp, tvp_seqc)) {
3602 if ((cnp->cn_flags & LOCKLEAF) != 0)
3606 return (cache_fpl_aborted(fpl));
3609 return (cache_fpl_handled(fpl, 0));
3613 * They want to possibly modify the state of the namecache.
3615 * Don't try to match the API contract, just leave.
3616 * TODO: this leaves scalability on the table
3619 cache_fplookup_final_modifying(struct cache_fpl *fpl)
3621 struct componentname *cnp;
3624 MPASS(cnp->cn_nameiop != LOOKUP);
3625 return (cache_fpl_partial(fpl));
3628 static int __noinline
3629 cache_fplookup_final_withparent(struct cache_fpl *fpl)
3631 struct componentname *cnp;
3632 enum vgetstate dvs, tvs;
3633 struct vnode *dvp, *tvp;
3639 dvp_seqc = fpl->dvp_seqc;
3642 MPASS((cnp->cn_flags & (LOCKPARENT|WANTPARENT)) != 0);
3645 * This is less efficient than it can be for simplicity.
3647 dvs = vget_prep_smr(dvp);
3648 if (__predict_false(dvs == VGET_NONE)) {
3649 return (cache_fpl_aborted(fpl));
3651 tvs = vget_prep_smr(tvp);
3652 if (__predict_false(tvs == VGET_NONE)) {
3653 cache_fpl_smr_exit(fpl);
3654 vget_abort(dvp, dvs);
3655 return (cache_fpl_aborted(fpl));
3658 cache_fpl_smr_exit(fpl);
3660 if ((cnp->cn_flags & LOCKPARENT) != 0) {
3661 error = vget_finish(dvp, LK_EXCLUSIVE, dvs);
3662 if (__predict_false(error != 0)) {
3663 vget_abort(tvp, tvs);
3664 return (cache_fpl_aborted(fpl));
3667 vget_finish_ref(dvp, dvs);
3670 if (!vn_seqc_consistent(dvp, dvp_seqc)) {
3671 vget_abort(tvp, tvs);
3672 if ((cnp->cn_flags & LOCKPARENT) != 0)
3676 return (cache_fpl_aborted(fpl));
3679 error = cache_fplookup_final_child(fpl, tvs);
3680 if (__predict_false(error != 0)) {
3681 MPASS(fpl->status == CACHE_FPL_STATUS_ABORTED);
3682 if ((cnp->cn_flags & LOCKPARENT) != 0)
3689 MPASS(fpl->status == CACHE_FPL_STATUS_HANDLED);
3694 cache_fplookup_final(struct cache_fpl *fpl)
3696 struct componentname *cnp;
3698 struct vnode *dvp, *tvp;
3703 dvp_seqc = fpl->dvp_seqc;
3706 VNPASS(cache_fplookup_vnode_supported(dvp), dvp);
3708 if (cnp->cn_nameiop != LOOKUP) {
3709 return (cache_fplookup_final_modifying(fpl));
3712 if ((cnp->cn_flags & (LOCKPARENT|WANTPARENT)) != 0)
3713 return (cache_fplookup_final_withparent(fpl));
3715 tvs = vget_prep_smr(tvp);
3716 if (__predict_false(tvs == VGET_NONE)) {
3717 return (cache_fpl_partial(fpl));
3720 if (!vn_seqc_consistent(dvp, dvp_seqc)) {
3721 cache_fpl_smr_exit(fpl);
3722 vget_abort(tvp, tvs);
3723 return (cache_fpl_aborted(fpl));
3726 cache_fpl_smr_exit(fpl);
3727 return (cache_fplookup_final_child(fpl, tvs));
3730 static int __noinline
3731 cache_fplookup_dot(struct cache_fpl *fpl)
3738 fpl->tvp_seqc = vn_seqc_read_any(dvp);
3739 if (seqc_in_modify(fpl->tvp_seqc)) {
3740 return (cache_fpl_aborted(fpl));
3743 counter_u64_add(dothits, 1);
3744 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", dvp);
3749 static int __noinline
3750 cache_fplookup_dotdot(struct cache_fpl *fpl)
3752 struct nameidata *ndp;
3753 struct componentname *cnp;
3754 struct namecache *ncp;
3764 * XXX this is racy the same way regular lookup is
3766 for (pr = cnp->cn_cred->cr_prison; pr != NULL;
3768 if (dvp == pr->pr_root)
3771 if (dvp == ndp->ni_rootdir ||
3772 dvp == ndp->ni_topdir ||
3776 fpl->tvp_seqc = vn_seqc_read_any(dvp);
3777 if (seqc_in_modify(fpl->tvp_seqc)) {
3778 return (cache_fpl_aborted(fpl));
3783 if ((dvp->v_vflag & VV_ROOT) != 0) {
3786 * The opposite of climb mount is needed here.
3788 return (cache_fpl_aborted(fpl));
3791 ncp = atomic_load_ptr(&dvp->v_cache_dd);
3793 return (cache_fpl_aborted(fpl));
3796 nc_flag = atomic_load_char(&ncp->nc_flag);
3797 if ((nc_flag & NCF_ISDOTDOT) != 0) {
3798 if ((nc_flag & NCF_NEGATIVE) != 0)
3799 return (cache_fpl_aborted(fpl));
3800 fpl->tvp = ncp->nc_vp;
3802 fpl->tvp = ncp->nc_dvp;
3805 if (__predict_false(!cache_ncp_canuse(ncp))) {
3806 return (cache_fpl_aborted(fpl));
3809 fpl->tvp_seqc = vn_seqc_read_any(fpl->tvp);
3810 if (seqc_in_modify(fpl->tvp_seqc)) {
3811 return (cache_fpl_partial(fpl));
3814 counter_u64_add(dotdothits, 1);
3819 cache_fplookup_next(struct cache_fpl *fpl)
3821 struct componentname *cnp;
3822 struct namecache *ncp;
3823 struct negstate *ns;
3824 struct vnode *dvp, *tvp;
3832 if (__predict_false(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.')) {
3833 return (cache_fplookup_dot(fpl));
3836 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
3838 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
3839 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
3840 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
3845 * If there is no entry we have to punt to the slow path to perform
3846 * actual lookup. Should there be nothing with this name a negative
3847 * entry will be created.
3849 if (__predict_false(ncp == NULL)) {
3850 return (cache_fpl_partial(fpl));
3853 tvp = atomic_load_ptr(&ncp->nc_vp);
3854 nc_flag = atomic_load_char(&ncp->nc_flag);
3855 if ((nc_flag & NCF_NEGATIVE) != 0) {
3857 * If they want to create an entry we need to replace this one.
3859 if (__predict_false(fpl->cnp->cn_nameiop != LOOKUP)) {
3860 return (cache_fpl_partial(fpl));
3862 ns = NCP2NEGSTATE(ncp);
3863 neg_hot = ((ns->neg_flag & NEG_HOT) != 0);
3864 if (__predict_false(!cache_ncp_canuse(ncp))) {
3865 return (cache_fpl_partial(fpl));
3867 if (__predict_false((nc_flag & NCF_WHITE) != 0)) {
3868 return (cache_fpl_partial(fpl));
3871 return (cache_fplookup_negative_promote(fpl, ncp, hash));
3873 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp,
3875 counter_u64_add(numneghits, 1);
3876 cache_fpl_smr_exit(fpl);
3877 return (cache_fpl_handled(fpl, ENOENT));
3880 if (__predict_false(!cache_ncp_canuse(ncp))) {
3881 return (cache_fpl_partial(fpl));
3885 fpl->tvp_seqc = vn_seqc_read_any(tvp);
3886 if (seqc_in_modify(fpl->tvp_seqc)) {
3887 return (cache_fpl_partial(fpl));
3890 if (!cache_fplookup_vnode_supported(tvp)) {
3891 return (cache_fpl_partial(fpl));
3894 counter_u64_add(numposhits, 1);
3895 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name, tvp);
3900 cache_fplookup_mp_supported(struct mount *mp)
3905 if ((mp->mnt_kern_flag & MNTK_FPLOOKUP) == 0)
3911 * Walk up the mount stack (if any).
3913 * Correctness is provided in the following ways:
3914 * - all vnodes are protected from freeing with SMR
3915 * - struct mount objects are type stable making them always safe to access
3916 * - stability of the particular mount is provided by busying it
3917 * - relationship between the vnode which is mounted on and the mount is
3918 * verified with the vnode sequence counter after busying
3919 * - association between root vnode of the mount and the mount is protected
3922 * From that point on we can read the sequence counter of the root vnode
3923 * and get the next mount on the stack (if any) using the same protection.
3925 * By the end of successful walk we are guaranteed the reached state was
3926 * indeed present at least at some point which matches the regular lookup.
3928 static int __noinline
3929 cache_fplookup_climb_mount(struct cache_fpl *fpl)
3931 struct mount *mp, *prev_mp;
3936 vp_seqc = fpl->tvp_seqc;
3938 VNPASS(vp->v_type == VDIR || vp->v_type == VBAD, vp);
3939 mp = atomic_load_ptr(&vp->v_mountedhere);
3945 if (!vfs_op_thread_enter_crit(mp)) {
3946 if (prev_mp != NULL)
3947 vfs_op_thread_exit_crit(prev_mp);
3948 return (cache_fpl_partial(fpl));
3950 if (prev_mp != NULL)
3951 vfs_op_thread_exit_crit(prev_mp);
3952 if (!vn_seqc_consistent(vp, vp_seqc)) {
3953 vfs_op_thread_exit_crit(mp);
3954 return (cache_fpl_partial(fpl));
3956 if (!cache_fplookup_mp_supported(mp)) {
3957 vfs_op_thread_exit_crit(mp);
3958 return (cache_fpl_partial(fpl));
3960 vp = atomic_load_ptr(&mp->mnt_rootvnode);
3961 if (vp == NULL || VN_IS_DOOMED(vp)) {
3962 vfs_op_thread_exit_crit(mp);
3963 return (cache_fpl_partial(fpl));
3965 vp_seqc = vn_seqc_read_any(vp);
3966 if (seqc_in_modify(vp_seqc)) {
3967 vfs_op_thread_exit_crit(mp);
3968 return (cache_fpl_partial(fpl));
3971 mp = atomic_load_ptr(&vp->v_mountedhere);
3976 vfs_op_thread_exit_crit(prev_mp);
3978 fpl->tvp_seqc = vp_seqc;
3983 cache_fplookup_need_climb_mount(struct cache_fpl *fpl)
3991 * Hack: while this is a union, the pointer tends to be NULL so save on
3994 mp = atomic_load_ptr(&vp->v_mountedhere);
3997 if (vp->v_type == VDIR)
4005 * The code was originally copy-pasted from regular lookup and despite
4006 * clean ups leaves performance on the table. Any modifications here
4007 * must take into account that in case off fallback the resulting
4008 * nameidata state has to be compatible with the original.
4011 cache_fplookup_parse(struct cache_fpl *fpl)
4013 struct nameidata *ndp;
4014 struct componentname *cnp;
4021 * Search a new directory.
4023 * The last component of the filename is left accessible via
4024 * cnp->cn_nameptr for callers that need the name. Callers needing
4025 * the name set the SAVENAME flag. When done, they assume
4026 * responsibility for freeing the pathname buffer.
4028 for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
4030 cnp->cn_namelen = cp - cnp->cn_nameptr;
4031 if (__predict_false(cnp->cn_namelen > NAME_MAX)) {
4032 cache_fpl_smr_exit(fpl);
4033 return (cache_fpl_handled(fpl, ENAMETOOLONG));
4035 ndp->ni_pathlen -= cnp->cn_namelen;
4036 KASSERT(ndp->ni_pathlen <= PATH_MAX,
4037 ("%s: ni_pathlen underflow to %zd\n", __func__, ndp->ni_pathlen));
4041 * Replace multiple slashes by a single slash and trailing slashes
4042 * by a null. This must be done before VOP_LOOKUP() because some
4043 * fs's don't know about trailing slashes. Remember if there were
4044 * trailing slashes to handle symlinks, existing non-directories
4045 * and non-existing files that won't be directories specially later.
4047 while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
4053 * Regular lookup performs the following:
4054 * *ndp->ni_next = '\0';
4055 * cnp->cn_flags |= TRAILINGSLASH;
4057 * Which is problematic since it modifies data read
4058 * from userspace. Then if fast path lookup was to
4059 * abort we would have to either restore it or convey
4060 * the flag. Since this is a corner case just ignore
4061 * it for simplicity.
4063 return (cache_fpl_partial(fpl));
4069 * Check for degenerate name (e.g. / or "")
4070 * which is a way of talking about a directory,
4071 * e.g. like "/." or ".".
4074 * Another corner case handled by the regular lookup
4076 if (__predict_false(cnp->cn_nameptr[0] == '\0')) {
4077 return (cache_fpl_partial(fpl));
4083 cache_fplookup_parse_advance(struct cache_fpl *fpl)
4085 struct nameidata *ndp;
4086 struct componentname *cnp;
4091 cnp->cn_nameptr = ndp->ni_next;
4092 while (*cnp->cn_nameptr == '/') {
4099 * See the API contract for VOP_FPLOOKUP_VEXEC.
4101 static int __noinline
4102 cache_fplookup_failed_vexec(struct cache_fpl *fpl, int error)
4104 struct componentname *cnp;
4110 dvp_seqc = fpl->dvp_seqc;
4113 * Hack: they may be looking up foo/bar, where foo is a
4114 * regular file. In such a case we need to turn ENOTDIR,
4115 * but we may happen to get here with a different error.
4117 if (dvp->v_type != VDIR) {
4119 * The check here is predominantly to catch
4120 * EOPNOTSUPP from dead_vnodeops. If the vnode
4121 * gets doomed past this point it is going to
4122 * fail seqc verification.
4124 if (VN_IS_DOOMED(dvp)) {
4125 return (cache_fpl_aborted(fpl));
4131 * Hack: handle O_SEARCH.
4133 * Open Group Base Specifications Issue 7, 2018 edition states:
4134 * If the access mode of the open file description associated with the
4135 * file descriptor is not O_SEARCH, the function shall check whether
4136 * directory searches are permitted using the current permissions of
4137 * the directory underlying the file descriptor. If the access mode is
4138 * O_SEARCH, the function shall not perform the check.
4140 * Regular lookup tests for the NOEXECCHECK flag for every path
4141 * component to decide whether to do the permission check. However,
4142 * since most lookups never have the flag (and when they do it is only
4143 * present for the first path component), lockless lookup only acts on
4144 * it if there is a permission problem. Here the flag is represented
4145 * with a boolean so that we don't have to clear it on the way out.
4147 * For simplicity this always aborts.
4148 * TODO: check if this is the first lookup and ignore the permission
4149 * problem. Note the flag has to survive fallback (if it happens to be
4153 return (cache_fpl_aborted(fpl));
4158 if (!vn_seqc_consistent(dvp, dvp_seqc)) {
4159 error = cache_fpl_aborted(fpl);
4161 cache_fpl_partial(fpl);
4165 if (!vn_seqc_consistent(dvp, dvp_seqc)) {
4166 error = cache_fpl_aborted(fpl);
4168 cache_fpl_smr_exit(fpl);
4169 cache_fpl_handled(fpl, error);
4177 cache_fplookup_impl(struct vnode *dvp, struct cache_fpl *fpl)
4179 struct nameidata *ndp;
4180 struct componentname *cnp;
4184 error = CACHE_FPL_FAILED;
4188 cache_fpl_checkpoint(fpl, &fpl->snd);
4191 fpl->dvp_seqc = vn_seqc_read_any(fpl->dvp);
4192 if (seqc_in_modify(fpl->dvp_seqc)) {
4193 cache_fpl_aborted(fpl);
4196 mp = atomic_load_ptr(&fpl->dvp->v_mount);
4197 if (!cache_fplookup_mp_supported(mp)) {
4198 cache_fpl_aborted(fpl);
4202 VNPASS(cache_fplookup_vnode_supported(fpl->dvp), fpl->dvp);
4205 error = cache_fplookup_parse(fpl);
4206 if (__predict_false(error != 0)) {
4210 VNPASS(cache_fplookup_vnode_supported(fpl->dvp), fpl->dvp);
4212 error = VOP_FPLOOKUP_VEXEC(fpl->dvp, cnp->cn_cred);
4213 if (__predict_false(error != 0)) {
4214 error = cache_fplookup_failed_vexec(fpl, error);
4218 if (__predict_false(cache_fpl_isdotdot(cnp))) {
4219 error = cache_fplookup_dotdot(fpl);
4220 if (__predict_false(error != 0)) {
4224 error = cache_fplookup_next(fpl);
4225 if (__predict_false(error != 0)) {
4229 VNPASS(!seqc_in_modify(fpl->tvp_seqc), fpl->tvp);
4231 if (cache_fplookup_need_climb_mount(fpl)) {
4232 error = cache_fplookup_climb_mount(fpl);
4233 if (__predict_false(error != 0)) {
4239 VNPASS(!seqc_in_modify(fpl->tvp_seqc), fpl->tvp);
4241 if (cache_fpl_islastcn(ndp)) {
4242 error = cache_fplookup_final(fpl);
4246 if (!vn_seqc_consistent(fpl->dvp, fpl->dvp_seqc)) {
4247 error = cache_fpl_aborted(fpl);
4251 fpl->dvp = fpl->tvp;
4252 fpl->dvp_seqc = fpl->tvp_seqc;
4254 cache_fplookup_parse_advance(fpl);
4255 cache_fpl_checkpoint(fpl, &fpl->snd);
4258 switch (fpl->status) {
4259 case CACHE_FPL_STATUS_UNSET:
4260 __assert_unreachable();
4262 case CACHE_FPL_STATUS_PARTIAL:
4263 cache_fpl_smr_assert_entered(fpl);
4264 return (cache_fplookup_partial_setup(fpl));
4265 case CACHE_FPL_STATUS_ABORTED:
4267 cache_fpl_smr_exit(fpl);
4268 return (CACHE_FPL_FAILED);
4269 case CACHE_FPL_STATUS_HANDLED:
4270 MPASS(error != CACHE_FPL_FAILED);
4271 cache_fpl_smr_assert_not_entered(fpl);
4272 if (__predict_false(error != 0)) {
4275 cache_fpl_cleanup_cnp(cnp);
4278 ndp->ni_dvp = fpl->dvp;
4279 ndp->ni_vp = fpl->tvp;
4280 if (cnp->cn_flags & SAVENAME)
4281 cnp->cn_flags |= HASBUF;
4283 cache_fpl_cleanup_cnp(cnp);
4289 * Fast path lookup protected with SMR and sequence counters.
4291 * Note: all VOP_FPLOOKUP_VEXEC routines have a comment referencing this one.
4293 * Filesystems can opt in by setting the MNTK_FPLOOKUP flag and meeting criteria
4296 * Traditional vnode lookup conceptually looks like this:
4302 * vn_unlock(current);
4309 * Each jump to the next vnode is safe memory-wise and atomic with respect to
4310 * any modifications thanks to holding respective locks.
4312 * The same guarantee can be provided with a combination of safe memory
4313 * reclamation and sequence counters instead. If all operations which affect
4314 * the relationship between the current vnode and the one we are looking for
4315 * also modify the counter, we can verify whether all the conditions held as
4316 * we made the jump. This includes things like permissions, mount points etc.
4317 * Counter modification is provided by enclosing relevant places in
4318 * vn_seqc_write_begin()/end() calls.
4320 * Thus this translates to:
4323 * dvp_seqc = seqc_read_any(dvp);
4324 * if (seqc_in_modify(dvp_seqc)) // someone is altering the vnode
4328 * tvp_seqc = seqc_read_any(tvp);
4329 * if (seqc_in_modify(tvp_seqc)) // someone is altering the target vnode
4331 * if (!seqc_consistent(dvp, dvp_seqc) // someone is altering the vnode
4333 * dvp = tvp; // we know nothing of importance has changed
4334 * dvp_seqc = tvp_seqc; // store the counter for the tvp iteration
4338 * vget(); // secure the vnode
4339 * if (!seqc_consistent(tvp, tvp_seqc) // final check
4341 * // at this point we know nothing has changed for any parent<->child pair
4342 * // as they were crossed during the lookup, meaning we matched the guarantee
4343 * // of the locked variant
4346 * The API contract for VOP_FPLOOKUP_VEXEC routines is as follows:
4347 * - they are called while within vfs_smr protection which they must never exit
4348 * - EAGAIN can be returned to denote checking could not be performed, it is
4349 * always valid to return it
4350 * - if the sequence counter has not changed the result must be valid
4351 * - if the sequence counter has changed both false positives and false negatives
4352 * are permitted (since the result will be rejected later)
4353 * - for simple cases of unix permission checks vaccess_vexec_smr can be used
4355 * Caveats to watch out for:
4356 * - vnodes are passed unlocked and unreferenced with nothing stopping
4357 * VOP_RECLAIM, in turn meaning that ->v_data can become NULL. It is advised
4358 * to use atomic_load_ptr to fetch it.
4359 * - the aforementioned object can also get freed, meaning absent other means it
4360 * should be protected with vfs_smr
4361 * - either safely checking permissions as they are modified or guaranteeing
4362 * their stability is left to the routine
4365 cache_fplookup(struct nameidata *ndp, enum cache_fpl_status *status,
4368 struct cache_fpl fpl;
4371 struct componentname *cnp;
4372 struct nameidata_saved orig;
4375 MPASS(ndp->ni_lcf == 0);
4377 fpl.status = CACHE_FPL_STATUS_UNSET;
4379 fpl.cnp = &ndp->ni_cnd;
4380 MPASS(curthread == fpl.cnp->cn_thread);
4382 if ((fpl.cnp->cn_flags & SAVESTART) != 0)
4383 MPASS(fpl.cnp->cn_nameiop != LOOKUP);
4385 if (!cache_can_fplookup(&fpl)) {
4386 SDT_PROBE3(vfs, fplookup, lookup, done, ndp, fpl.line, fpl.status);
4387 *status = fpl.status;
4388 return (EOPNOTSUPP);
4391 cache_fpl_checkpoint(&fpl, &orig);
4393 cache_fpl_smr_enter_initial(&fpl);
4394 fpl.fsearch = false;
4395 pwd = pwd_get_smr();
4397 ndp->ni_rootdir = pwd->pwd_rdir;
4398 ndp->ni_topdir = pwd->pwd_jdir;
4401 cnp->cn_nameptr = cnp->cn_pnbuf;
4402 if (cnp->cn_pnbuf[0] == '/') {
4403 cache_fpl_handle_root(ndp, &dvp);
4405 if (ndp->ni_dirfd == AT_FDCWD) {
4406 dvp = pwd->pwd_cdir;
4408 error = cache_fplookup_dirfd(&fpl, &dvp);
4409 if (__predict_false(error != 0)) {
4415 SDT_PROBE4(vfs, namei, lookup, entry, dvp, cnp->cn_pnbuf, cnp->cn_flags, true);
4417 error = cache_fplookup_impl(dvp, &fpl);
4419 cache_fpl_smr_assert_not_entered(&fpl);
4420 SDT_PROBE3(vfs, fplookup, lookup, done, ndp, fpl.line, fpl.status);
4422 *status = fpl.status;
4423 switch (fpl.status) {
4424 case CACHE_FPL_STATUS_UNSET:
4425 __assert_unreachable();
4427 case CACHE_FPL_STATUS_HANDLED:
4428 SDT_PROBE3(vfs, namei, lookup, return, error,
4429 (error == 0 ? ndp->ni_vp : NULL), true);
4431 case CACHE_FPL_STATUS_PARTIAL:
4434 * Status restored by cache_fplookup_partial_setup.
4437 case CACHE_FPL_STATUS_ABORTED:
4438 cache_fpl_restore(&fpl, &orig);