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_DEFINE2(vfs, namecache, enter_negative, done, "struct vnode *",
87 SDT_PROBE_DEFINE2(vfs, namecache, fullpath_smr, hit, "struct vnode *",
89 SDT_PROBE_DEFINE4(vfs, namecache, fullpath_smr, miss, "struct vnode *",
90 "struct namecache *", "int", "int");
91 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, entry, "struct vnode *");
92 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, hit, "struct vnode *",
93 "char *", "struct vnode *");
94 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, miss, "struct vnode *");
95 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, return, "int",
96 "struct vnode *", "char *");
97 SDT_PROBE_DEFINE3(vfs, namecache, lookup, hit, "struct vnode *", "char *",
99 SDT_PROBE_DEFINE2(vfs, namecache, lookup, hit__negative,
100 "struct vnode *", "char *");
101 SDT_PROBE_DEFINE2(vfs, namecache, lookup, miss, "struct vnode *",
103 SDT_PROBE_DEFINE2(vfs, namecache, removecnp, hit, "struct vnode *",
104 "struct componentname *");
105 SDT_PROBE_DEFINE2(vfs, namecache, removecnp, miss, "struct vnode *",
106 "struct componentname *");
107 SDT_PROBE_DEFINE1(vfs, namecache, purge, done, "struct vnode *");
108 SDT_PROBE_DEFINE1(vfs, namecache, purge_negative, done, "struct vnode *");
109 SDT_PROBE_DEFINE1(vfs, namecache, purgevfs, done, "struct mount *");
110 SDT_PROBE_DEFINE3(vfs, namecache, zap, done, "struct vnode *", "char *",
112 SDT_PROBE_DEFINE2(vfs, namecache, zap_negative, done, "struct vnode *",
114 SDT_PROBE_DEFINE2(vfs, namecache, shrink_negative, done, "struct vnode *",
117 SDT_PROBE_DEFINE3(vfs, fplookup, lookup, done, "struct nameidata", "int", "bool");
118 SDT_PROBE_DECLARE(vfs, namei, lookup, entry);
119 SDT_PROBE_DECLARE(vfs, namei, lookup, return);
122 * This structure describes the elements in the cache of recent
123 * names looked up by namei.
128 _Static_assert(sizeof(struct negstate) <= sizeof(struct vnode *),
129 "the state must fit in a union with a pointer without growing it");
132 LIST_ENTRY(namecache) nc_src; /* source vnode list */
133 TAILQ_ENTRY(namecache) nc_dst; /* destination vnode list */
134 CK_SLIST_ENTRY(namecache) nc_hash;/* hash chain */
135 struct vnode *nc_dvp; /* vnode of parent of name */
137 struct vnode *nu_vp; /* vnode the name refers to */
138 struct negstate nu_neg;/* negative entry state */
140 u_char nc_flag; /* flag bits */
141 u_char nc_nlen; /* length of name */
142 char nc_name[0]; /* segment name + nul */
146 * struct namecache_ts repeats struct namecache layout up to the
148 * struct namecache_ts is used in place of struct namecache when time(s) need
149 * to be stored. The nc_dotdottime field is used when a cache entry is mapping
150 * both a non-dotdot directory name plus dotdot for the directory's
153 * See below for alignment requirement.
155 struct namecache_ts {
156 struct timespec nc_time; /* timespec provided by fs */
157 struct timespec nc_dotdottime; /* dotdot timespec provided by fs */
158 int nc_ticks; /* ticks value when entry was added */
159 struct namecache nc_nc;
163 * At least mips n32 performs 64-bit accesses to timespec as found
164 * in namecache_ts and requires them to be aligned. Since others
165 * may be in the same spot suffer a little bit and enforce the
166 * alignment for everyone. Note this is a nop for 64-bit platforms.
168 #define CACHE_ZONE_ALIGNMENT UMA_ALIGNOF(time_t)
169 #define CACHE_PATH_CUTOFF 39
171 #define CACHE_ZONE_SMALL_SIZE (sizeof(struct namecache) + CACHE_PATH_CUTOFF + 1)
172 #define CACHE_ZONE_SMALL_TS_SIZE (sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1)
173 #define CACHE_ZONE_LARGE_SIZE (sizeof(struct namecache) + NAME_MAX + 1)
174 #define CACHE_ZONE_LARGE_TS_SIZE (sizeof(struct namecache_ts) + NAME_MAX + 1)
176 _Static_assert((CACHE_ZONE_SMALL_SIZE % (CACHE_ZONE_ALIGNMENT + 1)) == 0, "bad zone size");
177 _Static_assert((CACHE_ZONE_SMALL_TS_SIZE % (CACHE_ZONE_ALIGNMENT + 1)) == 0, "bad zone size");
178 _Static_assert((CACHE_ZONE_LARGE_SIZE % (CACHE_ZONE_ALIGNMENT + 1)) == 0, "bad zone size");
179 _Static_assert((CACHE_ZONE_LARGE_TS_SIZE % (CACHE_ZONE_ALIGNMENT + 1)) == 0, "bad zone size");
181 #define nc_vp n_un.nu_vp
182 #define nc_neg n_un.nu_neg
185 * Flags in namecache.nc_flag
187 #define NCF_WHITE 0x01
188 #define NCF_ISDOTDOT 0x02
191 #define NCF_DVDROP 0x10
192 #define NCF_NEGATIVE 0x20
193 #define NCF_INVALID 0x40
197 * Flags in negstate.neg_flag
202 * Mark an entry as invalid.
204 * This is called before it starts getting deconstructed.
207 cache_ncp_invalidate(struct namecache *ncp)
210 KASSERT((ncp->nc_flag & NCF_INVALID) == 0,
211 ("%s: entry %p already invalid", __func__, ncp));
212 atomic_store_char(&ncp->nc_flag, ncp->nc_flag | NCF_INVALID);
213 atomic_thread_fence_rel();
217 * Check whether the entry can be safely used.
219 * All places which elide locks are supposed to call this after they are
220 * done with reading from an entry.
223 cache_ncp_canuse(struct namecache *ncp)
226 atomic_thread_fence_acq();
227 return ((atomic_load_char(&ncp->nc_flag) & (NCF_INVALID | NCF_WIP)) == 0);
231 * Name caching works as follows:
233 * Names found by directory scans are retained in a cache
234 * for future reference. It is managed LRU, so frequently
235 * used names will hang around. Cache is indexed by hash value
236 * obtained from (dvp, name) where dvp refers to the directory
239 * If it is a "negative" entry, (i.e. for a name that is known NOT to
240 * exist) the vnode pointer will be NULL.
242 * Upon reaching the last segment of a path, if the reference
243 * is for DELETE, or NOCACHE is set (rewrite), and the
244 * name is located in the cache, it will be dropped.
246 * These locks are used (in the order in which they can be taken):
248 * vnodelock mtx vnode lists and v_cache_dd field protection
249 * bucketlock mtx for access to given set of hash buckets
250 * neglist mtx negative entry LRU management
252 * Additionally, ncneg_shrink_lock mtx is used to have at most one thread
253 * shrinking the LRU list.
255 * It is legal to take multiple vnodelock and bucketlock locks. The locking
256 * order is lower address first. Both are recursive.
258 * "." lookups are lockless.
260 * ".." and vnode -> name lookups require vnodelock.
262 * name -> vnode lookup requires the relevant bucketlock to be held for reading.
264 * Insertions and removals of entries require involved vnodes and bucketlocks
265 * to be locked to provide safe operation against other threads modifying the
268 * Some lookups result in removal of the found entry (e.g. getting rid of a
269 * negative entry with the intent to create a positive one), which poses a
270 * problem when multiple threads reach the state. Similarly, two different
271 * threads can purge two different vnodes and try to remove the same name.
273 * If the already held vnode lock is lower than the second required lock, we
274 * can just take the other lock. However, in the opposite case, this could
275 * deadlock. As such, this is resolved by trylocking and if that fails unlocking
276 * the first node, locking everything in order and revalidating the state.
282 * Structures associated with name caching.
284 #define NCHHASH(hash) \
285 (&nchashtbl[(hash) & nchash])
286 static __read_mostly CK_SLIST_HEAD(nchashhead, namecache) *nchashtbl;/* Hash Table */
287 static u_long __read_mostly nchash; /* size of hash table */
288 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0,
289 "Size of namecache hash table");
290 static u_long __read_mostly ncnegfactor = 5; /* ratio of negative entries */
291 SYSCTL_ULONG(_vfs, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0,
292 "Ratio of negative namecache entries");
293 static u_long __exclusive_cache_line numneg; /* number of negative entries allocated */
294 static u_long __exclusive_cache_line numcache;/* number of cache entries allocated */
295 u_int ncsizefactor = 2;
296 SYSCTL_UINT(_vfs, OID_AUTO, ncsizefactor, CTLFLAG_RW, &ncsizefactor, 0,
297 "Size factor for namecache");
298 static u_int __read_mostly ncpurgeminvnodes;
299 SYSCTL_UINT(_vfs, OID_AUTO, ncpurgeminvnodes, CTLFLAG_RW, &ncpurgeminvnodes, 0,
300 "Number of vnodes below which purgevfs ignores the request");
301 static u_int __read_mostly ncsize; /* the size as computed on creation or resizing */
303 struct nchstats nchstats; /* cache effectiveness statistics */
305 static bool __read_frequently cache_fast_revlookup = true;
306 SYSCTL_BOOL(_vfs, OID_AUTO, cache_fast_revlookup, CTLFLAG_RW,
307 &cache_fast_revlookup, 0, "");
309 static struct mtx __exclusive_cache_line ncneg_shrink_lock;
313 TAILQ_HEAD(, namecache) nl_list;
314 } __aligned(CACHE_LINE_SIZE);
316 static struct neglist __read_mostly *neglists;
317 static struct neglist ncneg_hot;
318 static u_long numhotneg;
321 #define numneglists (ncneghash + 1)
322 static inline struct neglist *
323 NCP2NEGLIST(struct namecache *ncp)
326 return (&neglists[(((uintptr_t)(ncp) >> 8) & ncneghash)]);
329 static inline struct negstate *
330 NCP2NEGSTATE(struct namecache *ncp)
333 MPASS(ncp->nc_flag & NCF_NEGATIVE);
334 return (&ncp->nc_neg);
337 #define numbucketlocks (ncbuckethash + 1)
338 static u_int __read_mostly ncbuckethash;
339 static struct mtx_padalign __read_mostly *bucketlocks;
340 #define HASH2BUCKETLOCK(hash) \
341 ((struct mtx *)(&bucketlocks[((hash) & ncbuckethash)]))
343 #define numvnodelocks (ncvnodehash + 1)
344 static u_int __read_mostly ncvnodehash;
345 static struct mtx __read_mostly *vnodelocks;
346 static inline struct mtx *
347 VP2VNODELOCK(struct vnode *vp)
350 return (&vnodelocks[(((uintptr_t)(vp) >> 8) & ncvnodehash)]);
354 * UMA zones for the VFS cache.
356 * The small cache is used for entries with short names, which are the
357 * most common. The large cache is used for entries which are too big to
358 * fit in the small cache.
360 static uma_zone_t __read_mostly cache_zone_small;
361 static uma_zone_t __read_mostly cache_zone_small_ts;
362 static uma_zone_t __read_mostly cache_zone_large;
363 static uma_zone_t __read_mostly cache_zone_large_ts;
365 static struct namecache *
366 cache_alloc(int len, int ts)
368 struct namecache_ts *ncp_ts;
369 struct namecache *ncp;
371 if (__predict_false(ts)) {
372 if (len <= CACHE_PATH_CUTOFF)
373 ncp_ts = uma_zalloc_smr(cache_zone_small_ts, M_WAITOK);
375 ncp_ts = uma_zalloc_smr(cache_zone_large_ts, M_WAITOK);
376 ncp = &ncp_ts->nc_nc;
378 if (len <= CACHE_PATH_CUTOFF)
379 ncp = uma_zalloc_smr(cache_zone_small, M_WAITOK);
381 ncp = uma_zalloc_smr(cache_zone_large, M_WAITOK);
387 cache_free(struct namecache *ncp)
389 struct namecache_ts *ncp_ts;
392 if ((ncp->nc_flag & NCF_DVDROP) != 0)
394 if (__predict_false(ncp->nc_flag & NCF_TS)) {
395 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
396 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
397 uma_zfree_smr(cache_zone_small_ts, ncp_ts);
399 uma_zfree_smr(cache_zone_large_ts, ncp_ts);
401 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
402 uma_zfree_smr(cache_zone_small, ncp);
404 uma_zfree_smr(cache_zone_large, ncp);
409 cache_out_ts(struct namecache *ncp, struct timespec *tsp, int *ticksp)
411 struct namecache_ts *ncp_ts;
413 KASSERT((ncp->nc_flag & NCF_TS) != 0 ||
414 (tsp == NULL && ticksp == NULL),
420 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
421 *tsp = ncp_ts->nc_time;
422 *ticksp = ncp_ts->nc_ticks;
426 static int __read_mostly doingcache = 1; /* 1 => enable the cache */
427 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0,
428 "VFS namecache enabled");
431 /* Export size information to userland */
432 SYSCTL_INT(_debug_sizeof, OID_AUTO, namecache, CTLFLAG_RD, SYSCTL_NULL_INT_PTR,
433 sizeof(struct namecache), "sizeof(struct namecache)");
436 * The new name cache statistics
438 static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
439 "Name cache statistics");
440 #define STATNODE_ULONG(name, descr) \
441 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, descr);
442 #define STATNODE_COUNTER(name, descr) \
443 static COUNTER_U64_DEFINE_EARLY(name); \
444 SYSCTL_COUNTER_U64(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, \
446 STATNODE_ULONG(numneg, "Number of negative cache entries");
447 STATNODE_ULONG(numcache, "Number of cache entries");
448 STATNODE_COUNTER(numcachehv, "Number of namecache entries with vnodes held");
449 STATNODE_COUNTER(numdrops, "Number of dropped entries due to reaching the limit");
450 STATNODE_COUNTER(dothits, "Number of '.' hits");
451 STATNODE_COUNTER(dotdothits, "Number of '..' hits");
452 STATNODE_COUNTER(nummiss, "Number of cache misses");
453 STATNODE_COUNTER(nummisszap, "Number of cache misses we do not want to cache");
454 STATNODE_COUNTER(numposzaps,
455 "Number of cache hits (positive) we do not want to cache");
456 STATNODE_COUNTER(numposhits, "Number of cache hits (positive)");
457 STATNODE_COUNTER(numnegzaps,
458 "Number of cache hits (negative) we do not want to cache");
459 STATNODE_COUNTER(numneghits, "Number of cache hits (negative)");
460 /* These count for vn_getcwd(), too. */
461 STATNODE_COUNTER(numfullpathcalls, "Number of fullpath search calls");
462 STATNODE_COUNTER(numfullpathfail1, "Number of fullpath search errors (ENOTDIR)");
463 STATNODE_COUNTER(numfullpathfail2,
464 "Number of fullpath search errors (VOP_VPTOCNP failures)");
465 STATNODE_COUNTER(numfullpathfail4, "Number of fullpath search errors (ENOMEM)");
466 STATNODE_COUNTER(numfullpathfound, "Number of successful fullpath calls");
467 STATNODE_COUNTER(zap_and_exit_bucket_relock_success,
468 "Number of successful removals after relocking");
469 static long zap_and_exit_bucket_fail; STATNODE_ULONG(zap_and_exit_bucket_fail,
470 "Number of times zap_and_exit failed to lock");
471 static long zap_and_exit_bucket_fail2; STATNODE_ULONG(zap_and_exit_bucket_fail2,
472 "Number of times zap_and_exit failed to lock");
473 static long cache_lock_vnodes_cel_3_failures;
474 STATNODE_ULONG(cache_lock_vnodes_cel_3_failures,
475 "Number of times 3-way vnode locking failed");
476 STATNODE_ULONG(numhotneg, "Number of hot negative entries");
477 STATNODE_COUNTER(numneg_evicted,
478 "Number of negative entries evicted when adding a new entry");
479 STATNODE_COUNTER(shrinking_skipped,
480 "Number of times shrinking was already in progress");
482 static void cache_zap_locked(struct namecache *ncp);
483 static int vn_fullpath_hardlink(struct nameidata *ndp, char **retbuf,
484 char **freebuf, size_t *buflen);
485 static int vn_fullpath_any_smr(struct vnode *vp, struct vnode *rdir, char *buf,
486 char **retbuf, size_t *buflen, bool slash_prefixed, size_t addend);
487 static int vn_fullpath_any(struct vnode *vp, struct vnode *rdir, char *buf,
488 char **retbuf, size_t *buflen);
489 static int vn_fullpath_dir(struct vnode *vp, struct vnode *rdir, char *buf,
490 char **retbuf, size_t *len, bool slash_prefixed, size_t addend);
492 static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
494 static int cache_yield;
495 SYSCTL_INT(_vfs_cache, OID_AUTO, yield, CTLFLAG_RD, &cache_yield, 0,
496 "Number of times cache called yield");
498 static void __noinline
499 cache_maybe_yield(void)
502 if (should_yield()) {
504 kern_yield(PRI_USER);
509 cache_assert_vlp_locked(struct mtx *vlp)
513 mtx_assert(vlp, MA_OWNED);
517 cache_assert_vnode_locked(struct vnode *vp)
521 vlp = VP2VNODELOCK(vp);
522 cache_assert_vlp_locked(vlp);
526 * TODO: With the value stored we can do better than computing the hash based
527 * on the address. The choice of FNV should also be revisited.
530 cache_prehash(struct vnode *vp)
533 vp->v_nchash = fnv_32_buf(&vp, sizeof(vp), FNV1_32_INIT);
537 cache_get_hash(char *name, u_char len, struct vnode *dvp)
540 return (fnv_32_buf(name, len, dvp->v_nchash));
543 static inline struct nchashhead *
544 NCP2BUCKET(struct namecache *ncp)
548 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen, ncp->nc_dvp);
549 return (NCHHASH(hash));
552 static inline struct mtx *
553 NCP2BUCKETLOCK(struct namecache *ncp)
557 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen, ncp->nc_dvp);
558 return (HASH2BUCKETLOCK(hash));
563 cache_assert_bucket_locked(struct namecache *ncp)
567 blp = NCP2BUCKETLOCK(ncp);
568 mtx_assert(blp, MA_OWNED);
572 cache_assert_bucket_unlocked(struct namecache *ncp)
576 blp = NCP2BUCKETLOCK(ncp);
577 mtx_assert(blp, MA_NOTOWNED);
580 #define cache_assert_bucket_locked(x) do { } while (0)
581 #define cache_assert_bucket_unlocked(x) do { } while (0)
584 #define cache_sort_vnodes(x, y) _cache_sort_vnodes((void **)(x), (void **)(y))
586 _cache_sort_vnodes(void **p1, void **p2)
590 MPASS(*p1 != NULL || *p2 != NULL);
600 cache_lock_all_buckets(void)
604 for (i = 0; i < numbucketlocks; i++)
605 mtx_lock(&bucketlocks[i]);
609 cache_unlock_all_buckets(void)
613 for (i = 0; i < numbucketlocks; i++)
614 mtx_unlock(&bucketlocks[i]);
618 cache_lock_all_vnodes(void)
622 for (i = 0; i < numvnodelocks; i++)
623 mtx_lock(&vnodelocks[i]);
627 cache_unlock_all_vnodes(void)
631 for (i = 0; i < numvnodelocks; i++)
632 mtx_unlock(&vnodelocks[i]);
636 cache_trylock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
639 cache_sort_vnodes(&vlp1, &vlp2);
642 if (!mtx_trylock(vlp1))
645 if (!mtx_trylock(vlp2)) {
655 cache_lock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
658 MPASS(vlp1 != NULL || vlp2 != NULL);
668 cache_unlock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
671 MPASS(vlp1 != NULL || vlp2 != NULL);
680 sysctl_nchstats(SYSCTL_HANDLER_ARGS)
682 struct nchstats snap;
684 if (req->oldptr == NULL)
685 return (SYSCTL_OUT(req, 0, sizeof(snap)));
688 snap.ncs_goodhits = counter_u64_fetch(numposhits);
689 snap.ncs_neghits = counter_u64_fetch(numneghits);
690 snap.ncs_badhits = counter_u64_fetch(numposzaps) +
691 counter_u64_fetch(numnegzaps);
692 snap.ncs_miss = counter_u64_fetch(nummisszap) +
693 counter_u64_fetch(nummiss);
695 return (SYSCTL_OUT(req, &snap, sizeof(snap)));
697 SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE | CTLFLAG_RD |
698 CTLFLAG_MPSAFE, 0, 0, sysctl_nchstats, "LU",
699 "VFS cache effectiveness statistics");
703 * Grab an atomic snapshot of the name cache hash chain lengths
705 static SYSCTL_NODE(_debug, OID_AUTO, hashstat,
706 CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
710 sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS)
712 struct nchashhead *ncpp;
713 struct namecache *ncp;
714 int i, error, n_nchash, *cntbuf;
717 n_nchash = nchash + 1; /* nchash is max index, not count */
718 if (req->oldptr == NULL)
719 return SYSCTL_OUT(req, 0, n_nchash * sizeof(int));
720 cntbuf = malloc(n_nchash * sizeof(int), M_TEMP, M_ZERO | M_WAITOK);
721 cache_lock_all_buckets();
722 if (n_nchash != nchash + 1) {
723 cache_unlock_all_buckets();
724 free(cntbuf, M_TEMP);
727 /* Scan hash tables counting entries */
728 for (ncpp = nchashtbl, i = 0; i < n_nchash; ncpp++, i++)
729 CK_SLIST_FOREACH(ncp, ncpp, nc_hash)
731 cache_unlock_all_buckets();
732 for (error = 0, i = 0; i < n_nchash; i++)
733 if ((error = SYSCTL_OUT(req, &cntbuf[i], sizeof(int))) != 0)
735 free(cntbuf, M_TEMP);
738 SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD|
739 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_rawnchash, "S,int",
740 "nchash chain lengths");
743 sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS)
746 struct nchashhead *ncpp;
747 struct namecache *ncp;
749 int count, maxlength, used, pct;
752 return SYSCTL_OUT(req, 0, 4 * sizeof(int));
754 cache_lock_all_buckets();
755 n_nchash = nchash + 1; /* nchash is max index, not count */
759 /* Scan hash tables for applicable entries */
760 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
762 CK_SLIST_FOREACH(ncp, ncpp, nc_hash) {
767 if (maxlength < count)
770 n_nchash = nchash + 1;
771 cache_unlock_all_buckets();
772 pct = (used * 100) / (n_nchash / 100);
773 error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash));
776 error = SYSCTL_OUT(req, &used, sizeof(used));
779 error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength));
782 error = SYSCTL_OUT(req, &pct, sizeof(pct));
787 SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD|
788 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_nchash, "I",
789 "nchash statistics (number of total/used buckets, maximum chain length, usage percentage)");
793 * Negative entries management
795 * A variation of LRU scheme is used. New entries are hashed into one of
796 * numneglists cold lists. Entries get promoted to the hot list on first hit.
798 * The shrinker will demote hot list head and evict from the cold list in a
799 * round-robin manner.
802 cache_negative_init(struct namecache *ncp)
804 struct negstate *negstate;
806 ncp->nc_flag |= NCF_NEGATIVE;
807 negstate = NCP2NEGSTATE(ncp);
808 negstate->neg_flag = 0;
812 cache_negative_hit(struct namecache *ncp)
814 struct neglist *neglist;
815 struct negstate *negstate;
817 negstate = NCP2NEGSTATE(ncp);
818 if ((negstate->neg_flag & NEG_HOT) != 0)
820 neglist = NCP2NEGLIST(ncp);
821 mtx_lock(&ncneg_hot.nl_lock);
822 mtx_lock(&neglist->nl_lock);
823 if ((negstate->neg_flag & NEG_HOT) == 0) {
825 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
826 TAILQ_INSERT_TAIL(&ncneg_hot.nl_list, ncp, nc_dst);
827 negstate->neg_flag |= NEG_HOT;
829 mtx_unlock(&neglist->nl_lock);
830 mtx_unlock(&ncneg_hot.nl_lock);
834 cache_negative_insert(struct namecache *ncp)
836 struct neglist *neglist;
838 MPASS(ncp->nc_flag & NCF_NEGATIVE);
839 cache_assert_bucket_locked(ncp);
840 neglist = NCP2NEGLIST(ncp);
841 mtx_lock(&neglist->nl_lock);
842 TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst);
843 mtx_unlock(&neglist->nl_lock);
844 atomic_add_rel_long(&numneg, 1);
848 cache_negative_remove(struct namecache *ncp)
850 struct neglist *neglist;
851 struct negstate *negstate;
852 bool hot_locked = false;
853 bool list_locked = false;
855 cache_assert_bucket_locked(ncp);
856 neglist = NCP2NEGLIST(ncp);
857 negstate = NCP2NEGSTATE(ncp);
858 if ((negstate->neg_flag & NEG_HOT) != 0) {
860 mtx_lock(&ncneg_hot.nl_lock);
861 if ((negstate->neg_flag & NEG_HOT) == 0) {
863 mtx_lock(&neglist->nl_lock);
867 mtx_lock(&neglist->nl_lock);
869 * We may be racing against promotion in lockless lookup.
871 if ((negstate->neg_flag & NEG_HOT) != 0) {
872 mtx_unlock(&neglist->nl_lock);
874 mtx_lock(&ncneg_hot.nl_lock);
875 mtx_lock(&neglist->nl_lock);
878 if ((negstate->neg_flag & NEG_HOT) != 0) {
879 mtx_assert(&ncneg_hot.nl_lock, MA_OWNED);
880 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
883 mtx_assert(&neglist->nl_lock, MA_OWNED);
884 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
887 mtx_unlock(&neglist->nl_lock);
889 mtx_unlock(&ncneg_hot.nl_lock);
890 atomic_subtract_rel_long(&numneg, 1);
894 cache_negative_shrink_select(struct namecache **ncpp,
895 struct neglist **neglistpp)
897 struct neglist *neglist;
898 struct namecache *ncp;
904 for (i = 0; i < numneglists; i++) {
905 neglist = &neglists[(cycle + i) % numneglists];
906 if (TAILQ_FIRST(&neglist->nl_list) == NULL)
908 mtx_lock(&neglist->nl_lock);
909 ncp = TAILQ_FIRST(&neglist->nl_list);
912 mtx_unlock(&neglist->nl_lock);
915 *neglistpp = neglist;
921 cache_negative_zap_one(void)
923 struct namecache *ncp, *ncp2;
924 struct neglist *neglist;
925 struct negstate *negstate;
929 if (mtx_owner(&ncneg_shrink_lock) != NULL ||
930 !mtx_trylock(&ncneg_shrink_lock)) {
931 counter_u64_add(shrinking_skipped, 1);
935 mtx_lock(&ncneg_hot.nl_lock);
936 ncp = TAILQ_FIRST(&ncneg_hot.nl_list);
938 neglist = NCP2NEGLIST(ncp);
939 negstate = NCP2NEGSTATE(ncp);
940 mtx_lock(&neglist->nl_lock);
941 MPASS((negstate->neg_flag & NEG_HOT) != 0);
942 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
943 TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst);
944 negstate->neg_flag &= ~NEG_HOT;
946 mtx_unlock(&neglist->nl_lock);
948 mtx_unlock(&ncneg_hot.nl_lock);
950 cache_negative_shrink_select(&ncp, &neglist);
952 mtx_unlock(&ncneg_shrink_lock);
956 MPASS(ncp->nc_flag & NCF_NEGATIVE);
957 dvlp = VP2VNODELOCK(ncp->nc_dvp);
958 blp = NCP2BUCKETLOCK(ncp);
959 mtx_unlock(&neglist->nl_lock);
963 * Enter SMR to safely check the negative list.
964 * Even if the found pointer matches, the entry may now be reallocated
965 * and used by a different vnode.
968 ncp2 = TAILQ_FIRST(&neglist->nl_list);
969 if (ncp != ncp2 || dvlp != VP2VNODELOCK(ncp2->nc_dvp) ||
970 blp != NCP2BUCKETLOCK(ncp2)) {
975 SDT_PROBE2(vfs, namecache, shrink_negative, done, ncp->nc_dvp,
977 cache_zap_locked(ncp);
978 counter_u64_add(numneg_evicted, 1);
987 * cache_zap_locked():
989 * Removes a namecache entry from cache, whether it contains an actual
990 * pointer to a vnode or if it is just a negative cache entry.
993 cache_zap_locked(struct namecache *ncp)
995 struct nchashhead *ncpp;
997 if (!(ncp->nc_flag & NCF_NEGATIVE))
998 cache_assert_vnode_locked(ncp->nc_vp);
999 cache_assert_vnode_locked(ncp->nc_dvp);
1000 cache_assert_bucket_locked(ncp);
1002 cache_ncp_invalidate(ncp);
1004 ncpp = NCP2BUCKET(ncp);
1005 CK_SLIST_REMOVE(ncpp, ncp, namecache, nc_hash);
1006 if (!(ncp->nc_flag & NCF_NEGATIVE)) {
1007 SDT_PROBE3(vfs, namecache, zap, done, ncp->nc_dvp,
1008 ncp->nc_name, ncp->nc_vp);
1009 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
1010 if (ncp == ncp->nc_vp->v_cache_dd) {
1011 vn_seqc_write_begin_unheld(ncp->nc_vp);
1012 ncp->nc_vp->v_cache_dd = NULL;
1013 vn_seqc_write_end(ncp->nc_vp);
1016 SDT_PROBE2(vfs, namecache, zap_negative, done, ncp->nc_dvp,
1018 cache_negative_remove(ncp);
1020 if (ncp->nc_flag & NCF_ISDOTDOT) {
1021 if (ncp == ncp->nc_dvp->v_cache_dd) {
1022 vn_seqc_write_begin_unheld(ncp->nc_dvp);
1023 ncp->nc_dvp->v_cache_dd = NULL;
1024 vn_seqc_write_end(ncp->nc_dvp);
1027 LIST_REMOVE(ncp, nc_src);
1028 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) {
1029 ncp->nc_flag |= NCF_DVDROP;
1030 counter_u64_add(numcachehv, -1);
1033 atomic_subtract_rel_long(&numcache, 1);
1037 cache_zap_negative_locked_vnode_kl(struct namecache *ncp, struct vnode *vp)
1041 MPASS(ncp->nc_dvp == vp);
1042 MPASS(ncp->nc_flag & NCF_NEGATIVE);
1043 cache_assert_vnode_locked(vp);
1045 blp = NCP2BUCKETLOCK(ncp);
1047 cache_zap_locked(ncp);
1052 cache_zap_locked_vnode_kl2(struct namecache *ncp, struct vnode *vp,
1055 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
1058 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
1059 cache_assert_vnode_locked(vp);
1061 if (ncp->nc_flag & NCF_NEGATIVE) {
1062 if (*vlpp != NULL) {
1066 cache_zap_negative_locked_vnode_kl(ncp, vp);
1070 pvlp = VP2VNODELOCK(vp);
1071 blp = NCP2BUCKETLOCK(ncp);
1072 vlp1 = VP2VNODELOCK(ncp->nc_dvp);
1073 vlp2 = VP2VNODELOCK(ncp->nc_vp);
1075 if (*vlpp == vlp1 || *vlpp == vlp2) {
1079 if (*vlpp != NULL) {
1083 cache_sort_vnodes(&vlp1, &vlp2);
1088 if (!mtx_trylock(vlp1))
1094 cache_zap_locked(ncp);
1096 if (to_unlock != NULL)
1097 mtx_unlock(to_unlock);
1104 MPASS(*vlpp == NULL);
1109 static int __noinline
1110 cache_zap_locked_vnode(struct namecache *ncp, struct vnode *vp)
1112 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
1116 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
1117 cache_assert_vnode_locked(vp);
1119 pvlp = VP2VNODELOCK(vp);
1120 if (ncp->nc_flag & NCF_NEGATIVE) {
1121 cache_zap_negative_locked_vnode_kl(ncp, vp);
1125 blp = NCP2BUCKETLOCK(ncp);
1126 vlp1 = VP2VNODELOCK(ncp->nc_dvp);
1127 vlp2 = VP2VNODELOCK(ncp->nc_vp);
1128 cache_sort_vnodes(&vlp1, &vlp2);
1133 if (!mtx_trylock(vlp1)) {
1135 * TODO: Very wasteful but rare.
1147 cache_zap_locked(ncp);
1149 mtx_unlock(to_unlock);
1156 * If trylocking failed we can get here. We know enough to take all needed locks
1157 * in the right order and re-lookup the entry.
1160 cache_zap_unlocked_bucket(struct namecache *ncp, struct componentname *cnp,
1161 struct vnode *dvp, struct mtx *dvlp, struct mtx *vlp, uint32_t hash,
1164 struct namecache *rncp;
1166 cache_assert_bucket_unlocked(ncp);
1168 cache_sort_vnodes(&dvlp, &vlp);
1169 cache_lock_vnodes(dvlp, vlp);
1171 CK_SLIST_FOREACH(rncp, (NCHHASH(hash)), nc_hash) {
1172 if (rncp == ncp && rncp->nc_dvp == dvp &&
1173 rncp->nc_nlen == cnp->cn_namelen &&
1174 !bcmp(rncp->nc_name, cnp->cn_nameptr, rncp->nc_nlen))
1178 cache_zap_locked(rncp);
1180 cache_unlock_vnodes(dvlp, vlp);
1181 counter_u64_add(zap_and_exit_bucket_relock_success, 1);
1186 cache_unlock_vnodes(dvlp, vlp);
1190 static int __noinline
1191 cache_zap_locked_bucket(struct namecache *ncp, struct componentname *cnp,
1192 uint32_t hash, struct mtx *blp)
1194 struct mtx *dvlp, *vlp;
1197 cache_assert_bucket_locked(ncp);
1199 dvlp = VP2VNODELOCK(ncp->nc_dvp);
1201 if (!(ncp->nc_flag & NCF_NEGATIVE))
1202 vlp = VP2VNODELOCK(ncp->nc_vp);
1203 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1204 cache_zap_locked(ncp);
1206 cache_unlock_vnodes(dvlp, vlp);
1212 return (cache_zap_unlocked_bucket(ncp, cnp, dvp, dvlp, vlp, hash, blp));
1216 cache_zap_locked_bucket_kl(struct namecache *ncp, struct mtx *blp,
1217 struct mtx **vlpp1, struct mtx **vlpp2)
1219 struct mtx *dvlp, *vlp;
1221 cache_assert_bucket_locked(ncp);
1223 dvlp = VP2VNODELOCK(ncp->nc_dvp);
1225 if (!(ncp->nc_flag & NCF_NEGATIVE))
1226 vlp = VP2VNODELOCK(ncp->nc_vp);
1227 cache_sort_vnodes(&dvlp, &vlp);
1229 if (*vlpp1 == dvlp && *vlpp2 == vlp) {
1230 cache_zap_locked(ncp);
1231 cache_unlock_vnodes(dvlp, vlp);
1244 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1245 cache_zap_locked(ncp);
1246 cache_unlock_vnodes(dvlp, vlp);
1260 static __noinline int
1261 cache_remove_cnp(struct vnode *dvp, struct componentname *cnp)
1263 struct namecache *ncp;
1265 struct mtx *dvlp, *dvlp2;
1269 if (cnp->cn_namelen == 2 &&
1270 cnp->cn_nameptr[0] == '.' && cnp->cn_nameptr[1] == '.') {
1271 dvlp = VP2VNODELOCK(dvp);
1275 ncp = dvp->v_cache_dd;
1280 SDT_PROBE2(vfs, namecache, removecnp, miss, dvp, cnp);
1283 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1284 if (!cache_zap_locked_vnode_kl2(ncp, dvp, &dvlp2))
1286 MPASS(dvp->v_cache_dd == NULL);
1292 vn_seqc_write_begin(dvp);
1293 dvp->v_cache_dd = NULL;
1294 vn_seqc_write_end(dvp);
1299 SDT_PROBE2(vfs, namecache, removecnp, hit, dvp, cnp);
1303 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1304 blp = HASH2BUCKETLOCK(hash);
1306 if (CK_SLIST_EMPTY(NCHHASH(hash)))
1311 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1312 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1313 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1322 error = cache_zap_locked_bucket(ncp, cnp, hash, blp);
1323 if (__predict_false(error != 0)) {
1324 zap_and_exit_bucket_fail++;
1327 counter_u64_add(numposzaps, 1);
1328 SDT_PROBE2(vfs, namecache, removecnp, hit, dvp, cnp);
1332 counter_u64_add(nummisszap, 1);
1333 SDT_PROBE2(vfs, namecache, removecnp, miss, dvp, cnp);
1337 static int __noinline
1338 cache_lookup_dot(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1339 struct timespec *tsp, int *ticksp)
1344 counter_u64_add(dothits, 1);
1345 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", *vpp);
1352 * When we lookup "." we still can be asked to lock it
1355 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
1356 if (ltype != VOP_ISLOCKED(*vpp)) {
1357 if (ltype == LK_EXCLUSIVE) {
1358 vn_lock(*vpp, LK_UPGRADE | LK_RETRY);
1359 if (VN_IS_DOOMED((*vpp))) {
1360 /* forced unmount */
1366 vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY);
1371 static int __noinline
1372 cache_lookup_dotdot(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1373 struct timespec *tsp, int *ticksp)
1375 struct namecache_ts *ncp_ts;
1376 struct namecache *ncp;
1382 MPASS((cnp->cn_flags & ISDOTDOT) != 0);
1384 if ((cnp->cn_flags & MAKEENTRY) == 0) {
1385 cache_remove_cnp(dvp, cnp);
1389 counter_u64_add(dotdothits, 1);
1391 dvlp = VP2VNODELOCK(dvp);
1393 ncp = dvp->v_cache_dd;
1395 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, "..", NULL);
1399 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1400 if (ncp->nc_flag & NCF_NEGATIVE)
1407 goto negative_success;
1408 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, "..", *vpp);
1409 cache_out_ts(ncp, tsp, ticksp);
1410 if ((ncp->nc_flag & (NCF_ISDOTDOT | NCF_DTS)) ==
1411 NCF_DTS && tsp != NULL) {
1412 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1413 *tsp = ncp_ts->nc_dotdottime;
1417 ltype = VOP_ISLOCKED(dvp);
1419 vs = vget_prep(*vpp);
1421 error = vget_finish(*vpp, cnp->cn_lkflags, vs);
1422 vn_lock(dvp, ltype | LK_RETRY);
1423 if (VN_IS_DOOMED(dvp)) {
1435 if (__predict_false(cnp->cn_nameiop == CREATE)) {
1436 if (cnp->cn_flags & ISLASTCN) {
1437 counter_u64_add(numnegzaps, 1);
1438 error = cache_zap_locked_vnode(ncp, dvp);
1439 if (__predict_false(error != 0)) {
1440 zap_and_exit_bucket_fail2++;
1448 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp, ncp->nc_name);
1449 cache_out_ts(ncp, tsp, ticksp);
1450 counter_u64_add(numneghits, 1);
1451 whiteout = (ncp->nc_flag & NCF_WHITE);
1452 cache_negative_hit(ncp);
1455 cnp->cn_flags |= ISWHITEOUT;
1460 * Lookup a name in the name cache
1464 * - dvp: Parent directory in which to search.
1465 * - vpp: Return argument. Will contain desired vnode on cache hit.
1466 * - cnp: Parameters of the name search. The most interesting bits of
1467 * the cn_flags field have the following meanings:
1468 * - MAKEENTRY: If clear, free an entry from the cache rather than look
1470 * - ISDOTDOT: Must be set if and only if cn_nameptr == ".."
1471 * - tsp: Return storage for cache timestamp. On a successful (positive
1472 * or negative) lookup, tsp will be filled with any timespec that
1473 * was stored when this cache entry was created. However, it will
1474 * be clear for "." entries.
1475 * - ticks: Return storage for alternate cache timestamp. On a successful
1476 * (positive or negative) lookup, it will contain the ticks value
1477 * that was current when the cache entry was created, unless cnp
1480 * Either both tsp and ticks have to be provided or neither of them.
1484 * - -1: A positive cache hit. vpp will contain the desired vnode.
1485 * - ENOENT: A negative cache hit, or dvp was recycled out from under us due
1486 * to a forced unmount. vpp will not be modified. If the entry
1487 * is a whiteout, then the ISWHITEOUT flag will be set in
1489 * - 0: A cache miss. vpp will not be modified.
1493 * On a cache hit, vpp will be returned locked and ref'd. If we're looking up
1494 * .., dvp is unlocked. If we're looking up . an extra ref is taken, but the
1495 * lock is not recursively acquired.
1497 static int __noinline
1498 cache_lookup_fallback(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1499 struct timespec *tsp, int *ticksp)
1501 struct namecache *ncp;
1508 MPASS((cnp->cn_flags & (MAKEENTRY | ISDOTDOT)) == MAKEENTRY);
1511 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1512 blp = HASH2BUCKETLOCK(hash);
1515 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1516 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1517 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1521 if (__predict_false(ncp == NULL)) {
1523 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
1525 counter_u64_add(nummiss, 1);
1529 if (ncp->nc_flag & NCF_NEGATIVE)
1530 goto negative_success;
1532 counter_u64_add(numposhits, 1);
1534 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name, *vpp);
1535 cache_out_ts(ncp, tsp, ticksp);
1537 vs = vget_prep(*vpp);
1539 error = vget_finish(*vpp, cnp->cn_lkflags, vs);
1546 if (__predict_false(cnp->cn_nameiop == CREATE)) {
1547 if (cnp->cn_flags & ISLASTCN) {
1548 counter_u64_add(numnegzaps, 1);
1549 error = cache_zap_locked_vnode(ncp, dvp);
1550 if (__predict_false(error != 0)) {
1551 zap_and_exit_bucket_fail2++;
1559 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp, ncp->nc_name);
1560 cache_out_ts(ncp, tsp, ticksp);
1561 counter_u64_add(numneghits, 1);
1562 whiteout = (ncp->nc_flag & NCF_WHITE);
1563 cache_negative_hit(ncp);
1566 cnp->cn_flags |= ISWHITEOUT;
1571 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1572 struct timespec *tsp, int *ticksp)
1574 struct namecache *ncp;
1575 struct negstate *negstate;
1582 MPASS((tsp == NULL && ticksp == NULL) || (tsp != NULL && ticksp != NULL));
1585 if (__predict_false(!doingcache)) {
1586 cnp->cn_flags &= ~MAKEENTRY;
1591 if (__predict_false(cnp->cn_nameptr[0] == '.')) {
1592 if (cnp->cn_namelen == 1)
1593 return (cache_lookup_dot(dvp, vpp, cnp, tsp, ticksp));
1594 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.')
1595 return (cache_lookup_dotdot(dvp, vpp, cnp, tsp, ticksp));
1598 MPASS((cnp->cn_flags & ISDOTDOT) == 0);
1600 if ((cnp->cn_flags & MAKEENTRY) == 0) {
1601 cache_remove_cnp(dvp, cnp);
1605 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1608 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1609 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1610 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1614 if (__predict_false(ncp == NULL)) {
1616 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
1618 counter_u64_add(nummiss, 1);
1622 nc_flag = atomic_load_char(&ncp->nc_flag);
1623 if (nc_flag & NCF_NEGATIVE)
1624 goto negative_success;
1626 counter_u64_add(numposhits, 1);
1628 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name, *vpp);
1629 cache_out_ts(ncp, tsp, ticksp);
1631 if (!cache_ncp_canuse(ncp)) {
1636 vs = vget_prep_smr(*vpp);
1638 if (__predict_false(vs == VGET_NONE)) {
1642 error = vget_finish(*vpp, cnp->cn_lkflags, vs);
1649 if (__predict_false(cnp->cn_nameiop == CREATE)) {
1650 if (cnp->cn_flags & ISLASTCN) {
1656 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp, ncp->nc_name);
1657 cache_out_ts(ncp, tsp, ticksp);
1658 counter_u64_add(numneghits, 1);
1659 whiteout = (ncp->nc_flag & NCF_WHITE);
1661 * TODO: We need to take locks to promote an entry. Code doing it
1662 * in SMR lookup can be modified to be shared.
1664 negstate = NCP2NEGSTATE(ncp);
1665 if ((negstate->neg_flag & NEG_HOT) == 0 ||
1666 !cache_ncp_canuse(ncp)) {
1672 cnp->cn_flags |= ISWHITEOUT;
1675 return (cache_lookup_fallback(dvp, vpp, cnp, tsp, ticksp));
1678 struct celockstate {
1682 CTASSERT((nitems(((struct celockstate *)0)->vlp) == 3));
1683 CTASSERT((nitems(((struct celockstate *)0)->blp) == 2));
1686 cache_celockstate_init(struct celockstate *cel)
1689 bzero(cel, sizeof(*cel));
1693 cache_lock_vnodes_cel(struct celockstate *cel, struct vnode *vp,
1696 struct mtx *vlp1, *vlp2;
1698 MPASS(cel->vlp[0] == NULL);
1699 MPASS(cel->vlp[1] == NULL);
1700 MPASS(cel->vlp[2] == NULL);
1702 MPASS(vp != NULL || dvp != NULL);
1704 vlp1 = VP2VNODELOCK(vp);
1705 vlp2 = VP2VNODELOCK(dvp);
1706 cache_sort_vnodes(&vlp1, &vlp2);
1717 cache_unlock_vnodes_cel(struct celockstate *cel)
1720 MPASS(cel->vlp[0] != NULL || cel->vlp[1] != NULL);
1722 if (cel->vlp[0] != NULL)
1723 mtx_unlock(cel->vlp[0]);
1724 if (cel->vlp[1] != NULL)
1725 mtx_unlock(cel->vlp[1]);
1726 if (cel->vlp[2] != NULL)
1727 mtx_unlock(cel->vlp[2]);
1731 cache_lock_vnodes_cel_3(struct celockstate *cel, struct vnode *vp)
1736 cache_assert_vlp_locked(cel->vlp[0]);
1737 cache_assert_vlp_locked(cel->vlp[1]);
1738 MPASS(cel->vlp[2] == NULL);
1741 vlp = VP2VNODELOCK(vp);
1744 if (vlp >= cel->vlp[1]) {
1747 if (mtx_trylock(vlp))
1749 cache_lock_vnodes_cel_3_failures++;
1750 cache_unlock_vnodes_cel(cel);
1751 if (vlp < cel->vlp[0]) {
1753 mtx_lock(cel->vlp[0]);
1754 mtx_lock(cel->vlp[1]);
1756 if (cel->vlp[0] != NULL)
1757 mtx_lock(cel->vlp[0]);
1759 mtx_lock(cel->vlp[1]);
1769 cache_lock_buckets_cel(struct celockstate *cel, struct mtx *blp1,
1773 MPASS(cel->blp[0] == NULL);
1774 MPASS(cel->blp[1] == NULL);
1776 cache_sort_vnodes(&blp1, &blp2);
1787 cache_unlock_buckets_cel(struct celockstate *cel)
1790 if (cel->blp[0] != NULL)
1791 mtx_unlock(cel->blp[0]);
1792 mtx_unlock(cel->blp[1]);
1796 * Lock part of the cache affected by the insertion.
1798 * This means vnodelocks for dvp, vp and the relevant bucketlock.
1799 * However, insertion can result in removal of an old entry. In this
1800 * case we have an additional vnode and bucketlock pair to lock.
1802 * That is, in the worst case we have to lock 3 vnodes and 2 bucketlocks, while
1803 * preserving the locking order (smaller address first).
1806 cache_enter_lock(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1809 struct namecache *ncp;
1810 struct mtx *blps[2];
1812 blps[0] = HASH2BUCKETLOCK(hash);
1815 cache_lock_vnodes_cel(cel, dvp, vp);
1816 if (vp == NULL || vp->v_type != VDIR)
1818 ncp = vp->v_cache_dd;
1821 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1823 MPASS(ncp->nc_dvp == vp);
1824 blps[1] = NCP2BUCKETLOCK(ncp);
1825 if (ncp->nc_flag & NCF_NEGATIVE)
1827 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1830 * All vnodes got re-locked. Re-validate the state and if
1831 * nothing changed we are done. Otherwise restart.
1833 if (ncp == vp->v_cache_dd &&
1834 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1835 blps[1] == NCP2BUCKETLOCK(ncp) &&
1836 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1838 cache_unlock_vnodes_cel(cel);
1843 cache_lock_buckets_cel(cel, blps[0], blps[1]);
1847 cache_enter_lock_dd(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1850 struct namecache *ncp;
1851 struct mtx *blps[2];
1853 blps[0] = HASH2BUCKETLOCK(hash);
1856 cache_lock_vnodes_cel(cel, dvp, vp);
1857 ncp = dvp->v_cache_dd;
1860 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1862 MPASS(ncp->nc_dvp == dvp);
1863 blps[1] = NCP2BUCKETLOCK(ncp);
1864 if (ncp->nc_flag & NCF_NEGATIVE)
1866 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1868 if (ncp == dvp->v_cache_dd &&
1869 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1870 blps[1] == NCP2BUCKETLOCK(ncp) &&
1871 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1873 cache_unlock_vnodes_cel(cel);
1878 cache_lock_buckets_cel(cel, blps[0], blps[1]);
1882 cache_enter_unlock(struct celockstate *cel)
1885 cache_unlock_buckets_cel(cel);
1886 cache_unlock_vnodes_cel(cel);
1889 static void __noinline
1890 cache_enter_dotdot_prep(struct vnode *dvp, struct vnode *vp,
1891 struct componentname *cnp)
1893 struct celockstate cel;
1894 struct namecache *ncp;
1898 if (dvp->v_cache_dd == NULL)
1900 len = cnp->cn_namelen;
1901 cache_celockstate_init(&cel);
1902 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1903 cache_enter_lock_dd(&cel, dvp, vp, hash);
1904 vn_seqc_write_begin(dvp);
1905 ncp = dvp->v_cache_dd;
1906 if (ncp != NULL && (ncp->nc_flag & NCF_ISDOTDOT)) {
1907 KASSERT(ncp->nc_dvp == dvp, ("wrong isdotdot parent"));
1908 cache_zap_locked(ncp);
1912 dvp->v_cache_dd = NULL;
1913 vn_seqc_write_end(dvp);
1914 cache_enter_unlock(&cel);
1920 * Add an entry to the cache.
1923 cache_enter_time(struct vnode *dvp, struct vnode *vp, struct componentname *cnp,
1924 struct timespec *tsp, struct timespec *dtsp)
1926 struct celockstate cel;
1927 struct namecache *ncp, *n2, *ndd;
1928 struct namecache_ts *ncp_ts, *n2_ts;
1929 struct nchashhead *ncpp;
1935 VNPASS(!VN_IS_DOOMED(dvp), dvp);
1936 VNPASS(dvp->v_type != VNON, dvp);
1938 VNPASS(!VN_IS_DOOMED(vp), vp);
1939 VNPASS(vp->v_type != VNON, vp);
1943 if (__predict_false(!doingcache))
1948 if (__predict_false(cnp->cn_nameptr[0] == '.')) {
1949 if (cnp->cn_namelen == 1)
1951 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
1952 cache_enter_dotdot_prep(dvp, vp, cnp);
1953 flag = NCF_ISDOTDOT;
1958 * Avoid blowout in namecache entries.
1960 lnumcache = atomic_fetchadd_long(&numcache, 1) + 1;
1961 if (__predict_false(lnumcache >= ncsize)) {
1962 atomic_add_long(&numcache, -1);
1963 counter_u64_add(numdrops, 1);
1967 cache_celockstate_init(&cel);
1972 * Calculate the hash key and setup as much of the new
1973 * namecache entry as possible before acquiring the lock.
1975 ncp = cache_alloc(cnp->cn_namelen, tsp != NULL);
1976 ncp->nc_flag = flag | NCF_WIP;
1979 cache_negative_init(ncp);
1982 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1983 ncp_ts->nc_time = *tsp;
1984 ncp_ts->nc_ticks = ticks;
1985 ncp_ts->nc_nc.nc_flag |= NCF_TS;
1987 ncp_ts->nc_dotdottime = *dtsp;
1988 ncp_ts->nc_nc.nc_flag |= NCF_DTS;
1991 len = ncp->nc_nlen = cnp->cn_namelen;
1992 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1993 memcpy(ncp->nc_name, cnp->cn_nameptr, len);
1994 ncp->nc_name[len] = '\0';
1995 cache_enter_lock(&cel, dvp, vp, hash);
1998 * See if this vnode or negative entry is already in the cache
1999 * with this name. This can happen with concurrent lookups of
2000 * the same path name.
2002 ncpp = NCHHASH(hash);
2003 CK_SLIST_FOREACH(n2, ncpp, nc_hash) {
2004 if (n2->nc_dvp == dvp &&
2005 n2->nc_nlen == cnp->cn_namelen &&
2006 !bcmp(n2->nc_name, cnp->cn_nameptr, n2->nc_nlen)) {
2007 MPASS(cache_ncp_canuse(n2));
2008 if ((n2->nc_flag & NCF_NEGATIVE) != 0)
2010 ("%s: found entry pointing to a different vnode (%p != %p)",
2011 __func__, NULL, vp));
2013 KASSERT(n2->nc_vp == vp,
2014 ("%s: found entry pointing to a different vnode (%p != %p)",
2015 __func__, n2->nc_vp, vp));
2017 KASSERT((n2->nc_flag & NCF_TS) != 0,
2019 n2_ts = __containerof(n2, struct namecache_ts, nc_nc);
2020 n2_ts->nc_time = ncp_ts->nc_time;
2021 n2_ts->nc_ticks = ncp_ts->nc_ticks;
2023 n2_ts->nc_dotdottime = ncp_ts->nc_dotdottime;
2024 n2_ts->nc_nc.nc_flag |= NCF_DTS;
2027 goto out_unlock_free;
2031 if (flag == NCF_ISDOTDOT) {
2033 * See if we are trying to add .. entry, but some other lookup
2034 * has populated v_cache_dd pointer already.
2036 if (dvp->v_cache_dd != NULL)
2037 goto out_unlock_free;
2038 KASSERT(vp == NULL || vp->v_type == VDIR,
2039 ("wrong vnode type %p", vp));
2040 vn_seqc_write_begin(dvp);
2041 dvp->v_cache_dd = ncp;
2042 vn_seqc_write_end(dvp);
2046 if (flag != NCF_ISDOTDOT) {
2048 * For this case, the cache entry maps both the
2049 * directory name in it and the name ".." for the
2050 * directory's parent.
2052 vn_seqc_write_begin(vp);
2053 if ((ndd = vp->v_cache_dd) != NULL) {
2054 if ((ndd->nc_flag & NCF_ISDOTDOT) != 0)
2055 cache_zap_locked(ndd);
2059 vp->v_cache_dd = ncp;
2060 vn_seqc_write_end(vp);
2061 } else if (vp->v_type != VDIR) {
2062 if (vp->v_cache_dd != NULL) {
2063 vn_seqc_write_begin(vp);
2064 vp->v_cache_dd = NULL;
2065 vn_seqc_write_end(vp);
2070 if (flag != NCF_ISDOTDOT) {
2071 if (LIST_EMPTY(&dvp->v_cache_src)) {
2073 counter_u64_add(numcachehv, 1);
2075 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
2079 * If the entry is "negative", we place it into the
2080 * "negative" cache queue, otherwise, we place it into the
2081 * destination vnode's cache entries queue.
2084 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
2085 SDT_PROBE3(vfs, namecache, enter, done, dvp, ncp->nc_name,
2088 if (cnp->cn_flags & ISWHITEOUT)
2089 ncp->nc_flag |= NCF_WHITE;
2090 cache_negative_insert(ncp);
2091 SDT_PROBE2(vfs, namecache, enter_negative, done, dvp,
2096 * Insert the new namecache entry into the appropriate chain
2097 * within the cache entries table.
2099 CK_SLIST_INSERT_HEAD(ncpp, ncp, nc_hash);
2101 atomic_thread_fence_rel();
2103 * Mark the entry as fully constructed.
2104 * It is immutable past this point until its removal.
2106 atomic_store_char(&ncp->nc_flag, ncp->nc_flag & ~NCF_WIP);
2108 cache_enter_unlock(&cel);
2109 if (numneg * ncnegfactor > lnumcache)
2110 cache_negative_zap_one();
2115 cache_enter_unlock(&cel);
2116 atomic_add_long(&numcache, -1);
2122 cache_roundup_2(u_int val)
2126 for (res = 1; res <= val; res <<= 1)
2132 static struct nchashhead *
2133 nchinittbl(u_long elements, u_long *hashmask)
2135 struct nchashhead *hashtbl;
2138 hashsize = cache_roundup_2(elements) / 2;
2140 hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), M_VFSCACHE, M_WAITOK);
2141 for (i = 0; i < hashsize; i++)
2142 CK_SLIST_INIT(&hashtbl[i]);
2143 *hashmask = hashsize - 1;
2148 ncfreetbl(struct nchashhead *hashtbl)
2151 free(hashtbl, M_VFSCACHE);
2155 * Name cache initialization, from vfs_init() when we are booting
2158 nchinit(void *dummy __unused)
2162 cache_zone_small = uma_zcreate("S VFS Cache", CACHE_ZONE_SMALL_SIZE,
2163 NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT, UMA_ZONE_ZINIT);
2164 cache_zone_small_ts = uma_zcreate("STS VFS Cache", CACHE_ZONE_SMALL_TS_SIZE,
2165 NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT, UMA_ZONE_ZINIT);
2166 cache_zone_large = uma_zcreate("L VFS Cache", CACHE_ZONE_LARGE_SIZE,
2167 NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT, UMA_ZONE_ZINIT);
2168 cache_zone_large_ts = uma_zcreate("LTS VFS Cache", CACHE_ZONE_LARGE_TS_SIZE,
2169 NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT, UMA_ZONE_ZINIT);
2171 VFS_SMR_ZONE_SET(cache_zone_small);
2172 VFS_SMR_ZONE_SET(cache_zone_small_ts);
2173 VFS_SMR_ZONE_SET(cache_zone_large);
2174 VFS_SMR_ZONE_SET(cache_zone_large_ts);
2176 ncsize = desiredvnodes * ncsizefactor;
2177 nchashtbl = nchinittbl(desiredvnodes * 2, &nchash);
2178 ncbuckethash = cache_roundup_2(mp_ncpus * mp_ncpus) - 1;
2179 if (ncbuckethash < 7) /* arbitrarily chosen to avoid having one lock */
2181 if (ncbuckethash > nchash)
2182 ncbuckethash = nchash;
2183 bucketlocks = malloc(sizeof(*bucketlocks) * numbucketlocks, M_VFSCACHE,
2185 for (i = 0; i < numbucketlocks; i++)
2186 mtx_init(&bucketlocks[i], "ncbuc", NULL, MTX_DUPOK | MTX_RECURSE);
2187 ncvnodehash = ncbuckethash;
2188 vnodelocks = malloc(sizeof(*vnodelocks) * numvnodelocks, M_VFSCACHE,
2190 for (i = 0; i < numvnodelocks; i++)
2191 mtx_init(&vnodelocks[i], "ncvn", NULL, MTX_DUPOK | MTX_RECURSE);
2192 ncpurgeminvnodes = numbucketlocks * 2;
2194 neglists = malloc(sizeof(*neglists) * numneglists, M_VFSCACHE,
2196 for (i = 0; i < numneglists; i++) {
2197 mtx_init(&neglists[i].nl_lock, "ncnegl", NULL, MTX_DEF);
2198 TAILQ_INIT(&neglists[i].nl_list);
2200 mtx_init(&ncneg_hot.nl_lock, "ncneglh", NULL, MTX_DEF);
2201 TAILQ_INIT(&ncneg_hot.nl_list);
2203 mtx_init(&ncneg_shrink_lock, "ncnegs", NULL, MTX_DEF);
2205 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL);
2208 cache_vnode_init(struct vnode *vp)
2211 LIST_INIT(&vp->v_cache_src);
2212 TAILQ_INIT(&vp->v_cache_dst);
2213 vp->v_cache_dd = NULL;
2218 cache_changesize(u_long newmaxvnodes)
2220 struct nchashhead *new_nchashtbl, *old_nchashtbl;
2221 u_long new_nchash, old_nchash;
2222 struct namecache *ncp;
2227 newncsize = newmaxvnodes * ncsizefactor;
2228 newmaxvnodes = cache_roundup_2(newmaxvnodes * 2);
2229 if (newmaxvnodes < numbucketlocks)
2230 newmaxvnodes = numbucketlocks;
2232 new_nchashtbl = nchinittbl(newmaxvnodes, &new_nchash);
2233 /* If same hash table size, nothing to do */
2234 if (nchash == new_nchash) {
2235 ncfreetbl(new_nchashtbl);
2239 * Move everything from the old hash table to the new table.
2240 * None of the namecache entries in the table can be removed
2241 * because to do so, they have to be removed from the hash table.
2243 cache_lock_all_vnodes();
2244 cache_lock_all_buckets();
2245 old_nchashtbl = nchashtbl;
2246 old_nchash = nchash;
2247 nchashtbl = new_nchashtbl;
2248 nchash = new_nchash;
2249 for (i = 0; i <= old_nchash; i++) {
2250 while ((ncp = CK_SLIST_FIRST(&old_nchashtbl[i])) != NULL) {
2251 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen,
2253 CK_SLIST_REMOVE(&old_nchashtbl[i], ncp, namecache, nc_hash);
2254 CK_SLIST_INSERT_HEAD(NCHHASH(hash), ncp, nc_hash);
2258 cache_unlock_all_buckets();
2259 cache_unlock_all_vnodes();
2260 ncfreetbl(old_nchashtbl);
2264 * Invalidate all entries from and to a particular vnode.
2267 cache_purge_impl(struct vnode *vp)
2269 TAILQ_HEAD(, namecache) ncps;
2270 struct namecache *ncp, *nnp;
2271 struct mtx *vlp, *vlp2;
2274 vlp = VP2VNODELOCK(vp);
2276 mtx_assert(vlp, MA_OWNED);
2278 while (!LIST_EMPTY(&vp->v_cache_src)) {
2279 ncp = LIST_FIRST(&vp->v_cache_src);
2280 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2282 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2284 while (!TAILQ_EMPTY(&vp->v_cache_dst)) {
2285 ncp = TAILQ_FIRST(&vp->v_cache_dst);
2286 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2288 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2290 ncp = vp->v_cache_dd;
2292 KASSERT(ncp->nc_flag & NCF_ISDOTDOT,
2293 ("lost dotdot link"));
2294 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2296 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2298 KASSERT(vp->v_cache_dd == NULL, ("incomplete purge"));
2302 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2308 cache_purge(struct vnode *vp)
2312 SDT_PROBE1(vfs, namecache, purge, done, vp);
2313 if (LIST_EMPTY(&vp->v_cache_src) && TAILQ_EMPTY(&vp->v_cache_dst) &&
2314 vp->v_cache_dd == NULL)
2316 vlp = VP2VNODELOCK(vp);
2318 cache_purge_impl(vp);
2322 * Only to be used by vgone.
2325 cache_purge_vgone(struct vnode *vp)
2329 VNPASS(VN_IS_DOOMED(vp), vp);
2330 vlp = VP2VNODELOCK(vp);
2331 if (!(LIST_EMPTY(&vp->v_cache_src) && TAILQ_EMPTY(&vp->v_cache_dst) &&
2332 vp->v_cache_dd == NULL)) {
2334 cache_purge_impl(vp);
2335 mtx_assert(vlp, MA_NOTOWNED);
2340 * All the NULL pointer state we found above may be transient.
2341 * Serialize against a possible thread doing cache_purge.
2343 mtx_wait_unlocked(vlp);
2344 if (!(LIST_EMPTY(&vp->v_cache_src) && TAILQ_EMPTY(&vp->v_cache_dst) &&
2345 vp->v_cache_dd == NULL)) {
2347 cache_purge_impl(vp);
2348 mtx_assert(vlp, MA_NOTOWNED);
2355 * Invalidate all negative entries for a particular directory vnode.
2358 cache_purge_negative(struct vnode *vp)
2360 TAILQ_HEAD(, namecache) ncps;
2361 struct namecache *ncp, *nnp;
2364 SDT_PROBE1(vfs, namecache, purge_negative, done, vp);
2365 if (LIST_EMPTY(&vp->v_cache_src))
2368 vlp = VP2VNODELOCK(vp);
2370 LIST_FOREACH_SAFE(ncp, &vp->v_cache_src, nc_src, nnp) {
2371 if (!(ncp->nc_flag & NCF_NEGATIVE))
2373 cache_zap_negative_locked_vnode_kl(ncp, vp);
2374 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2377 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2383 cache_rename(struct vnode *fdvp, struct vnode *fvp, struct vnode *tdvp,
2384 struct vnode *tvp, struct componentname *fcnp, struct componentname *tcnp)
2387 ASSERT_VOP_IN_SEQC(fdvp);
2388 ASSERT_VOP_IN_SEQC(fvp);
2389 ASSERT_VOP_IN_SEQC(tdvp);
2391 ASSERT_VOP_IN_SEQC(tvp);
2396 KASSERT(!cache_remove_cnp(tdvp, tcnp),
2397 ("%s: lingering negative entry", __func__));
2399 cache_remove_cnp(tdvp, tcnp);
2404 * Flush all entries referencing a particular filesystem.
2407 cache_purgevfs(struct mount *mp, bool force)
2409 TAILQ_HEAD(, namecache) ncps;
2410 struct mtx *vlp1, *vlp2;
2412 struct nchashhead *bucket;
2413 struct namecache *ncp, *nnp;
2414 u_long i, j, n_nchash;
2417 /* Scan hash tables for applicable entries */
2418 SDT_PROBE1(vfs, namecache, purgevfs, done, mp);
2419 if (!force && mp->mnt_nvnodelistsize <= ncpurgeminvnodes)
2422 n_nchash = nchash + 1;
2424 for (i = 0; i < numbucketlocks; i++) {
2425 blp = (struct mtx *)&bucketlocks[i];
2427 for (j = i; j < n_nchash; j += numbucketlocks) {
2429 bucket = &nchashtbl[j];
2430 CK_SLIST_FOREACH_SAFE(ncp, bucket, nc_hash, nnp) {
2431 cache_assert_bucket_locked(ncp);
2432 if (ncp->nc_dvp->v_mount != mp)
2434 error = cache_zap_locked_bucket_kl(ncp, blp,
2438 TAILQ_INSERT_HEAD(&ncps, ncp, nc_dst);
2442 if (vlp1 == NULL && vlp2 == NULL)
2443 cache_maybe_yield();
2450 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2456 * Perform canonical checks and cache lookup and pass on to filesystem
2457 * through the vop_cachedlookup only if needed.
2461 vfs_cache_lookup(struct vop_lookup_args *ap)
2465 struct vnode **vpp = ap->a_vpp;
2466 struct componentname *cnp = ap->a_cnp;
2467 int flags = cnp->cn_flags;
2472 if (dvp->v_type != VDIR)
2475 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
2476 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
2479 error = vn_dir_check_exec(dvp, cnp);
2483 error = cache_lookup(dvp, vpp, cnp, NULL, NULL);
2485 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
2491 /* Implementation of the getcwd syscall. */
2493 sys___getcwd(struct thread *td, struct __getcwd_args *uap)
2499 buflen = uap->buflen;
2500 if (__predict_false(buflen < 2))
2502 if (buflen > MAXPATHLEN)
2503 buflen = MAXPATHLEN;
2505 buf = uma_zalloc(namei_zone, M_WAITOK);
2506 error = vn_getcwd(buf, &retbuf, &buflen);
2508 error = copyout(retbuf, uap->buf, buflen);
2509 uma_zfree(namei_zone, buf);
2514 vn_getcwd(char *buf, char **retbuf, size_t *buflen)
2520 pwd = pwd_get_smr();
2521 error = vn_fullpath_any_smr(pwd->pwd_cdir, pwd->pwd_rdir, buf, retbuf,
2523 VFS_SMR_ASSERT_NOT_ENTERED();
2525 pwd = pwd_hold(curthread);
2526 error = vn_fullpath_any(pwd->pwd_cdir, pwd->pwd_rdir, buf,
2532 if (KTRPOINT(curthread, KTR_NAMEI) && error == 0)
2539 kern___realpathat(struct thread *td, int fd, const char *path, char *buf,
2540 size_t size, int flags, enum uio_seg pathseg)
2542 struct nameidata nd;
2543 char *retbuf, *freebuf;
2548 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | SAVENAME | WANTPARENT | AUDITVNODE1,
2549 pathseg, path, fd, &cap_fstat_rights, td);
2550 if ((error = namei(&nd)) != 0)
2552 error = vn_fullpath_hardlink(&nd, &retbuf, &freebuf, &size);
2554 error = copyout(retbuf, buf, size);
2555 free(freebuf, M_TEMP);
2562 sys___realpathat(struct thread *td, struct __realpathat_args *uap)
2565 return (kern___realpathat(td, uap->fd, uap->path, uap->buf, uap->size,
2566 uap->flags, UIO_USERSPACE));
2570 * Retrieve the full filesystem path that correspond to a vnode from the name
2571 * cache (if available)
2574 vn_fullpath(struct vnode *vp, char **retbuf, char **freebuf)
2581 if (__predict_false(vp == NULL))
2584 buflen = MAXPATHLEN;
2585 buf = malloc(buflen, M_TEMP, M_WAITOK);
2587 pwd = pwd_get_smr();
2588 error = vn_fullpath_any_smr(vp, pwd->pwd_rdir, buf, retbuf, &buflen, false, 0);
2589 VFS_SMR_ASSERT_NOT_ENTERED();
2591 pwd = pwd_hold(curthread);
2592 error = vn_fullpath_any(vp, pwd->pwd_rdir, buf, retbuf, &buflen);
2603 * This function is similar to vn_fullpath, but it attempts to lookup the
2604 * pathname relative to the global root mount point. This is required for the
2605 * auditing sub-system, as audited pathnames must be absolute, relative to the
2606 * global root mount point.
2609 vn_fullpath_global(struct vnode *vp, char **retbuf, char **freebuf)
2615 if (__predict_false(vp == NULL))
2617 buflen = MAXPATHLEN;
2618 buf = malloc(buflen, M_TEMP, M_WAITOK);
2620 error = vn_fullpath_any_smr(vp, rootvnode, buf, retbuf, &buflen, false, 0);
2621 VFS_SMR_ASSERT_NOT_ENTERED();
2623 error = vn_fullpath_any(vp, rootvnode, buf, retbuf, &buflen);
2632 static struct namecache *
2633 vn_dd_from_dst(struct vnode *vp)
2635 struct namecache *ncp;
2637 cache_assert_vnode_locked(vp);
2638 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst) {
2639 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2646 vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, size_t *buflen)
2649 struct namecache *ncp;
2653 vlp = VP2VNODELOCK(*vp);
2655 ncp = (*vp)->v_cache_dd;
2656 if (ncp != NULL && (ncp->nc_flag & NCF_ISDOTDOT) == 0) {
2657 KASSERT(ncp == vn_dd_from_dst(*vp),
2658 ("%s: mismatch for dd entry (%p != %p)", __func__,
2659 ncp, vn_dd_from_dst(*vp)));
2661 ncp = vn_dd_from_dst(*vp);
2664 if (*buflen < ncp->nc_nlen) {
2667 counter_u64_add(numfullpathfail4, 1);
2669 SDT_PROBE3(vfs, namecache, fullpath, return, error,
2673 *buflen -= ncp->nc_nlen;
2674 memcpy(buf + *buflen, ncp->nc_name, ncp->nc_nlen);
2675 SDT_PROBE3(vfs, namecache, fullpath, hit, ncp->nc_dvp,
2684 SDT_PROBE1(vfs, namecache, fullpath, miss, vp);
2687 vn_lock(*vp, LK_SHARED | LK_RETRY);
2688 error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen);
2691 counter_u64_add(numfullpathfail2, 1);
2692 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2697 if (VN_IS_DOOMED(dvp)) {
2698 /* forced unmount */
2701 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2705 * *vp has its use count incremented still.
2712 * Resolve a directory to a pathname.
2714 * The name of the directory can always be found in the namecache or fetched
2715 * from the filesystem. There is also guaranteed to be only one parent, meaning
2716 * we can just follow vnodes up until we find the root.
2718 * The vnode must be referenced.
2721 vn_fullpath_dir(struct vnode *vp, struct vnode *rdir, char *buf, char **retbuf,
2722 size_t *len, bool slash_prefixed, size_t addend)
2724 #ifdef KDTRACE_HOOKS
2725 struct vnode *startvp = vp;
2731 VNPASS(vp->v_type == VDIR || VN_IS_DOOMED(vp), vp);
2732 VNPASS(vp->v_usecount > 0, vp);
2736 if (!slash_prefixed) {
2744 SDT_PROBE1(vfs, namecache, fullpath, entry, vp);
2745 counter_u64_add(numfullpathcalls, 1);
2746 while (vp != rdir && vp != rootvnode) {
2748 * The vp vnode must be already fully constructed,
2749 * since it is either found in namecache or obtained
2750 * from VOP_VPTOCNP(). We may test for VV_ROOT safely
2751 * without obtaining the vnode lock.
2753 if ((vp->v_vflag & VV_ROOT) != 0) {
2754 vn_lock(vp, LK_RETRY | LK_SHARED);
2757 * With the vnode locked, check for races with
2758 * unmount, forced or not. Note that we
2759 * already verified that vp is not equal to
2760 * the root vnode, which means that
2761 * mnt_vnodecovered can be NULL only for the
2764 if (VN_IS_DOOMED(vp) ||
2765 (vp1 = vp->v_mount->mnt_vnodecovered) == NULL ||
2766 vp1->v_mountedhere != vp->v_mount) {
2769 SDT_PROBE3(vfs, namecache, fullpath, return,
2779 if (vp->v_type != VDIR) {
2781 counter_u64_add(numfullpathfail1, 1);
2783 SDT_PROBE3(vfs, namecache, fullpath, return,
2787 error = vn_vptocnp(&vp, curthread->td_ucred, buf, &buflen);
2793 SDT_PROBE3(vfs, namecache, fullpath, return, error,
2797 buf[--buflen] = '/';
2798 slash_prefixed = true;
2802 if (!slash_prefixed) {
2805 counter_u64_add(numfullpathfail4, 1);
2806 SDT_PROBE3(vfs, namecache, fullpath, return, ENOMEM,
2810 buf[--buflen] = '/';
2812 counter_u64_add(numfullpathfound, 1);
2815 *retbuf = buf + buflen;
2816 SDT_PROBE3(vfs, namecache, fullpath, return, 0, startvp, *retbuf);
2823 * Resolve an arbitrary vnode to a pathname.
2826 * - hardlinks are not tracked, thus if the vnode is not a directory this can
2827 * resolve to a different path than the one used to find it
2828 * - namecache is not mandatory, meaning names are not guaranteed to be added
2829 * (in which case resolving fails)
2831 static void __inline
2832 cache_rev_failed_impl(int *reason, int line)
2837 #define cache_rev_failed(var) cache_rev_failed_impl((var), __LINE__)
2840 vn_fullpath_any_smr(struct vnode *vp, struct vnode *rdir, char *buf,
2841 char **retbuf, size_t *buflen, bool slash_prefixed, size_t addend)
2843 #ifdef KDTRACE_HOOKS
2844 struct vnode *startvp = vp;
2848 struct namecache *ncp;
2852 #ifdef KDTRACE_HOOKS
2855 seqc_t vp_seqc, tvp_seqc;
2858 VFS_SMR_ASSERT_ENTERED();
2860 if (!cache_fast_revlookup) {
2865 orig_buflen = *buflen;
2867 if (!slash_prefixed) {
2868 MPASS(*buflen >= 2);
2870 buf[*buflen] = '\0';
2873 if (vp == rdir || vp == rootvnode) {
2874 if (!slash_prefixed) {
2881 #ifdef KDTRACE_HOOKS
2885 ncp = NULL; /* for sdt probe down below */
2886 vp_seqc = vn_seqc_read_any(vp);
2887 if (seqc_in_modify(vp_seqc)) {
2888 cache_rev_failed(&reason);
2893 #ifdef KDTRACE_HOOKS
2896 if ((vp->v_vflag & VV_ROOT) != 0) {
2897 mp = atomic_load_ptr(&vp->v_mount);
2899 cache_rev_failed(&reason);
2902 tvp = atomic_load_ptr(&mp->mnt_vnodecovered);
2903 tvp_seqc = vn_seqc_read_any(tvp);
2904 if (seqc_in_modify(tvp_seqc)) {
2905 cache_rev_failed(&reason);
2908 if (!vn_seqc_consistent(vp, vp_seqc)) {
2909 cache_rev_failed(&reason);
2916 ncp = atomic_load_ptr(&vp->v_cache_dd);
2918 cache_rev_failed(&reason);
2921 nc_flag = atomic_load_char(&ncp->nc_flag);
2922 if ((nc_flag & NCF_ISDOTDOT) != 0) {
2923 cache_rev_failed(&reason);
2926 if (!cache_ncp_canuse(ncp)) {
2927 cache_rev_failed(&reason);
2930 if (ncp->nc_nlen >= *buflen) {
2931 cache_rev_failed(&reason);
2935 *buflen -= ncp->nc_nlen;
2936 memcpy(buf + *buflen, ncp->nc_name, ncp->nc_nlen);
2940 tvp_seqc = vn_seqc_read_any(tvp);
2941 if (seqc_in_modify(tvp_seqc)) {
2942 cache_rev_failed(&reason);
2945 if (!vn_seqc_consistent(vp, vp_seqc)) {
2946 cache_rev_failed(&reason);
2951 if (vp == rdir || vp == rootvnode)
2956 *retbuf = buf + *buflen;
2957 *buflen = orig_buflen - *buflen + addend;
2958 SDT_PROBE2(vfs, namecache, fullpath_smr, hit, startvp, *retbuf);
2962 *buflen = orig_buflen;
2963 SDT_PROBE4(vfs, namecache, fullpath_smr, miss, startvp, ncp, reason, i);
2969 vn_fullpath_any(struct vnode *vp, struct vnode *rdir, char *buf, char **retbuf,
2973 bool slash_prefixed;
2979 orig_buflen = *buflen;
2982 slash_prefixed = false;
2983 if (vp->v_type != VDIR) {
2985 buf[*buflen] = '\0';
2986 error = vn_vptocnp(&vp, curthread->td_ucred, buf, buflen);
2995 slash_prefixed = true;
2998 return (vn_fullpath_dir(vp, rdir, buf, retbuf, buflen, slash_prefixed,
2999 orig_buflen - *buflen));
3003 * Resolve an arbitrary vnode to a pathname (taking care of hardlinks).
3005 * Since the namecache does not track handlings, the caller is expected to first
3006 * look up the target vnode with SAVENAME | WANTPARENT flags passed to namei.
3008 * Then we have 2 cases:
3009 * - if the found vnode is a directory, the path can be constructed just by
3010 * fullowing names up the chain
3011 * - otherwise we populate the buffer with the saved name and start resolving
3015 vn_fullpath_hardlink(struct nameidata *ndp, char **retbuf, char **freebuf,
3020 struct componentname *cnp;
3024 bool slash_prefixed;
3029 if (*buflen > MAXPATHLEN)
3030 *buflen = MAXPATHLEN;
3032 slash_prefixed = false;
3034 buf = malloc(*buflen, M_TEMP, M_WAITOK);
3039 * Check for VBAD to work around the vp_crossmp bug in lookup().
3041 * For example consider tmpfs on /tmp and realpath /tmp. ni_vp will be
3042 * set to mount point's root vnode while ni_dvp will be vp_crossmp.
3043 * If the type is VDIR (like in this very case) we can skip looking
3044 * at ni_dvp in the first place. However, since vnodes get passed here
3045 * unlocked the target may transition to doomed state (type == VBAD)
3046 * before we get to evaluate the condition. If this happens, we will
3047 * populate part of the buffer and descend to vn_fullpath_dir with
3048 * vp == vp_crossmp. Prevent the problem by checking for VBAD.
3050 * This should be atomic_load(&vp->v_type) but it is ilegal to take
3051 * an address of a bit field, even if said field is sized to char.
3052 * Work around the problem by reading the value into a full-sized enum
3053 * and then re-reading it with atomic_load which will still prevent
3054 * the compiler from re-reading down the road.
3057 type = atomic_load_int(&type);
3064 addend = cnp->cn_namelen + 2;
3065 if (*buflen < addend) {
3070 tmpbuf = buf + *buflen;
3072 memcpy(&tmpbuf[1], cnp->cn_nameptr, cnp->cn_namelen);
3073 tmpbuf[addend - 1] = '\0';
3074 slash_prefixed = true;
3079 pwd = pwd_get_smr();
3080 error = vn_fullpath_any_smr(vp, pwd->pwd_rdir, buf, retbuf, buflen,
3081 slash_prefixed, addend);
3082 VFS_SMR_ASSERT_NOT_ENTERED();
3084 pwd = pwd_hold(curthread);
3086 error = vn_fullpath_dir(vp, pwd->pwd_rdir, buf, retbuf, buflen,
3087 slash_prefixed, addend);
3102 vn_dir_dd_ino(struct vnode *vp)
3104 struct namecache *ncp;
3109 ASSERT_VOP_LOCKED(vp, "vn_dir_dd_ino");
3110 vlp = VP2VNODELOCK(vp);
3112 TAILQ_FOREACH(ncp, &(vp->v_cache_dst), nc_dst) {
3113 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0)
3116 vs = vget_prep(ddvp);
3118 if (vget_finish(ddvp, LK_SHARED | LK_NOWAIT, vs))
3127 vn_commname(struct vnode *vp, char *buf, u_int buflen)
3129 struct namecache *ncp;
3133 vlp = VP2VNODELOCK(vp);
3135 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst)
3136 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
3142 l = min(ncp->nc_nlen, buflen - 1);
3143 memcpy(buf, ncp->nc_name, l);
3150 * This function updates path string to vnode's full global path
3151 * and checks the size of the new path string against the pathlen argument.
3153 * Requires a locked, referenced vnode.
3154 * Vnode is re-locked on success or ENODEV, otherwise unlocked.
3156 * If vp is a directory, the call to vn_fullpath_global() always succeeds
3157 * because it falls back to the ".." lookup if the namecache lookup fails.
3160 vn_path_to_global_path(struct thread *td, struct vnode *vp, char *path,
3163 struct nameidata nd;
3168 ASSERT_VOP_ELOCKED(vp, __func__);
3170 /* Construct global filesystem path from vp. */
3172 error = vn_fullpath_global(vp, &rpath, &fbuf);
3179 if (strlen(rpath) >= pathlen) {
3181 error = ENAMETOOLONG;
3186 * Re-lookup the vnode by path to detect a possible rename.
3187 * As a side effect, the vnode is relocked.
3188 * If vnode was renamed, return ENOENT.
3190 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
3191 UIO_SYSSPACE, path, td);
3197 NDFREE(&nd, NDF_ONLY_PNBUF);
3201 strcpy(path, rpath);
3214 db_print_vpath(struct vnode *vp)
3217 while (vp != NULL) {
3218 db_printf("%p: ", vp);
3219 if (vp == rootvnode) {
3223 if (vp->v_vflag & VV_ROOT) {
3224 db_printf("<mount point>");
3225 vp = vp->v_mount->mnt_vnodecovered;
3227 struct namecache *ncp;
3231 ncp = TAILQ_FIRST(&vp->v_cache_dst);
3234 for (i = 0; i < ncp->nc_nlen; i++)
3235 db_printf("%c", *ncn++);
3248 DB_SHOW_COMMAND(vpath, db_show_vpath)
3253 db_printf("usage: show vpath <struct vnode *>\n");
3257 vp = (struct vnode *)addr;
3263 static bool __read_frequently cache_fast_lookup = true;
3264 SYSCTL_BOOL(_vfs, OID_AUTO, cache_fast_lookup, CTLFLAG_RW,
3265 &cache_fast_lookup, 0, "");
3267 #define CACHE_FPL_FAILED -2020
3270 cache_fpl_cleanup_cnp(struct componentname *cnp)
3273 uma_zfree(namei_zone, cnp->cn_pnbuf);
3275 cnp->cn_pnbuf = NULL;
3276 cnp->cn_nameptr = NULL;
3281 cache_fpl_handle_root(struct nameidata *ndp, struct vnode **dpp)
3283 struct componentname *cnp;
3286 while (*(cnp->cn_nameptr) == '/') {
3291 *dpp = ndp->ni_rootdir;
3295 * Components of nameidata (or objects it can point to) which may
3296 * need restoring in case fast path lookup fails.
3298 struct nameidata_saved {
3306 struct nameidata *ndp;
3307 struct componentname *cnp;
3313 struct nameidata_saved snd;
3315 enum cache_fpl_status status:8;
3320 cache_fpl_checkpoint(struct cache_fpl *fpl, struct nameidata_saved *snd)
3323 snd->cn_flags = fpl->ndp->ni_cnd.cn_flags;
3324 snd->cn_namelen = fpl->ndp->ni_cnd.cn_namelen;
3325 snd->cn_nameptr = fpl->ndp->ni_cnd.cn_nameptr;
3326 snd->ni_pathlen = fpl->ndp->ni_pathlen;
3330 cache_fpl_restore(struct cache_fpl *fpl, struct nameidata_saved *snd)
3333 fpl->ndp->ni_cnd.cn_flags = snd->cn_flags;
3334 fpl->ndp->ni_cnd.cn_namelen = snd->cn_namelen;
3335 fpl->ndp->ni_cnd.cn_nameptr = snd->cn_nameptr;
3336 fpl->ndp->ni_pathlen = snd->ni_pathlen;
3340 #define cache_fpl_smr_assert_entered(fpl) ({ \
3341 struct cache_fpl *_fpl = (fpl); \
3342 MPASS(_fpl->in_smr == true); \
3343 VFS_SMR_ASSERT_ENTERED(); \
3345 #define cache_fpl_smr_assert_not_entered(fpl) ({ \
3346 struct cache_fpl *_fpl = (fpl); \
3347 MPASS(_fpl->in_smr == false); \
3348 VFS_SMR_ASSERT_NOT_ENTERED(); \
3351 #define cache_fpl_smr_assert_entered(fpl) do { } while (0)
3352 #define cache_fpl_smr_assert_not_entered(fpl) do { } while (0)
3355 #define cache_fpl_smr_enter_initial(fpl) ({ \
3356 struct cache_fpl *_fpl = (fpl); \
3358 _fpl->in_smr = true; \
3361 #define cache_fpl_smr_enter(fpl) ({ \
3362 struct cache_fpl *_fpl = (fpl); \
3363 MPASS(_fpl->in_smr == false); \
3365 _fpl->in_smr = true; \
3368 #define cache_fpl_smr_exit(fpl) ({ \
3369 struct cache_fpl *_fpl = (fpl); \
3370 MPASS(_fpl->in_smr == true); \
3372 _fpl->in_smr = false; \
3376 cache_fpl_aborted_impl(struct cache_fpl *fpl, int line)
3379 if (fpl->status != CACHE_FPL_STATUS_UNSET) {
3380 KASSERT(fpl->status == CACHE_FPL_STATUS_PARTIAL,
3381 ("%s: converting to abort from %d at %d, set at %d\n",
3382 __func__, fpl->status, line, fpl->line));
3384 fpl->status = CACHE_FPL_STATUS_ABORTED;
3386 return (CACHE_FPL_FAILED);
3389 #define cache_fpl_aborted(x) cache_fpl_aborted_impl((x), __LINE__)
3392 cache_fpl_partial_impl(struct cache_fpl *fpl, int line)
3395 KASSERT(fpl->status == CACHE_FPL_STATUS_UNSET,
3396 ("%s: setting to partial at %d, but already set to %d at %d\n",
3397 __func__, line, fpl->status, fpl->line));
3398 cache_fpl_smr_assert_entered(fpl);
3399 fpl->status = CACHE_FPL_STATUS_PARTIAL;
3401 return (CACHE_FPL_FAILED);
3404 #define cache_fpl_partial(x) cache_fpl_partial_impl((x), __LINE__)
3407 cache_fpl_handled_impl(struct cache_fpl *fpl, int error, int line)
3410 KASSERT(fpl->status == CACHE_FPL_STATUS_UNSET,
3411 ("%s: setting to handled at %d, but already set to %d at %d\n",
3412 __func__, line, fpl->status, fpl->line));
3413 cache_fpl_smr_assert_not_entered(fpl);
3414 MPASS(error != CACHE_FPL_FAILED);
3415 fpl->status = CACHE_FPL_STATUS_HANDLED;
3420 #define cache_fpl_handled(x, e) cache_fpl_handled_impl((x), (e), __LINE__)
3422 #define CACHE_FPL_SUPPORTED_CN_FLAGS \
3423 (LOCKLEAF | LOCKPARENT | WANTPARENT | NOCACHE | FOLLOW | LOCKSHARED | SAVENAME | \
3424 SAVESTART | WILLBEDIR | ISOPEN | NOMACCHECK | AUDITVNODE1 | AUDITVNODE2 | NOCAPCHECK)
3426 #define CACHE_FPL_INTERNAL_CN_FLAGS \
3427 (ISDOTDOT | MAKEENTRY | ISLASTCN)
3429 _Static_assert((CACHE_FPL_SUPPORTED_CN_FLAGS & CACHE_FPL_INTERNAL_CN_FLAGS) == 0,
3430 "supported and internal flags overlap");
3433 cache_fpl_islastcn(struct nameidata *ndp)
3436 return (*ndp->ni_next == 0);
3440 cache_fpl_isdotdot(struct componentname *cnp)
3443 if (cnp->cn_namelen == 2 &&
3444 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
3450 cache_can_fplookup(struct cache_fpl *fpl)
3452 struct nameidata *ndp;
3453 struct componentname *cnp;
3458 td = cnp->cn_thread;
3460 if (!cache_fast_lookup) {
3461 cache_fpl_aborted(fpl);
3465 if (mac_vnode_check_lookup_enabled()) {
3466 cache_fpl_aborted(fpl);
3470 if ((cnp->cn_flags & ~CACHE_FPL_SUPPORTED_CN_FLAGS) != 0) {
3471 cache_fpl_aborted(fpl);
3474 if (ndp->ni_dirfd != AT_FDCWD) {
3475 cache_fpl_aborted(fpl);
3478 if (IN_CAPABILITY_MODE(td)) {
3479 cache_fpl_aborted(fpl);
3482 if (AUDITING_TD(td)) {
3483 cache_fpl_aborted(fpl);
3486 if (ndp->ni_startdir != NULL) {
3487 cache_fpl_aborted(fpl);
3494 cache_fplookup_vnode_supported(struct vnode *vp)
3497 return (vp->v_type != VLNK);
3501 * Move a negative entry to the hot list.
3503 * We have to take locks, but they may be contended and in the worst
3504 * case we may need to go off CPU. We don't want to spin within the
3505 * smr section and we can't block with it. Instead we are going to
3506 * look up the entry again.
3508 static int __noinline
3509 cache_fplookup_negative_promote(struct cache_fpl *fpl, struct namecache *oncp,
3512 struct componentname *cnp;
3513 struct namecache *ncp;
3514 struct neglist *neglist;
3515 struct negstate *negstate;
3522 if (!vhold_smr(dvp))
3523 return (cache_fpl_aborted(fpl));
3525 neglist = NCP2NEGLIST(oncp);
3526 cache_fpl_smr_exit(fpl);
3528 mtx_lock(&ncneg_hot.nl_lock);
3529 mtx_lock(&neglist->nl_lock);
3531 * For hash iteration.
3533 cache_fpl_smr_enter(fpl);
3536 * Avoid all surprises by only succeeding if we got the same entry and
3537 * bailing completely otherwise.
3539 * In particular at this point there can be a new ncp which matches the
3540 * search but hashes to a different neglist.
3542 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
3548 * No match to begin with.
3550 if (__predict_false(ncp == NULL)) {
3555 * The newly found entry may be something different...
3557 if (!(ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
3558 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))) {
3563 * ... and not even negative.
3565 nc_flag = atomic_load_char(&ncp->nc_flag);
3566 if ((nc_flag & NCF_NEGATIVE) == 0) {
3570 if (__predict_false(!cache_ncp_canuse(ncp))) {
3574 negstate = NCP2NEGSTATE(ncp);
3575 if ((negstate->neg_flag & NEG_HOT) == 0) {
3577 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
3578 TAILQ_INSERT_TAIL(&ncneg_hot.nl_list, ncp, nc_dst);
3579 negstate->neg_flag |= NEG_HOT;
3582 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp, ncp->nc_name);
3583 counter_u64_add(numneghits, 1);
3584 cache_fpl_smr_exit(fpl);
3585 mtx_unlock(&neglist->nl_lock);
3586 mtx_unlock(&ncneg_hot.nl_lock);
3588 return (cache_fpl_handled(fpl, ENOENT));
3590 cache_fpl_smr_exit(fpl);
3591 mtx_unlock(&neglist->nl_lock);
3592 mtx_unlock(&ncneg_hot.nl_lock);
3594 return (cache_fpl_aborted(fpl));
3598 * The target vnode is not supported, prepare for the slow path to take over.
3600 static int __noinline
3601 cache_fplookup_partial_setup(struct cache_fpl *fpl)
3603 struct nameidata *ndp;
3604 struct componentname *cnp;
3613 dvp_seqc = fpl->dvp_seqc;
3615 dvs = vget_prep_smr(dvp);
3616 if (__predict_false(dvs == VGET_NONE)) {
3617 cache_fpl_smr_exit(fpl);
3618 return (cache_fpl_aborted(fpl));
3621 cache_fpl_smr_exit(fpl);
3623 vget_finish_ref(dvp, dvs);
3624 if (!vn_seqc_consistent(dvp, dvp_seqc)) {
3626 return (cache_fpl_aborted(fpl));
3629 pwd = pwd_hold(curthread);
3630 if (fpl->pwd != pwd) {
3633 return (cache_fpl_aborted(fpl));
3636 cache_fpl_restore(fpl, &fpl->snd);
3638 ndp->ni_startdir = dvp;
3639 cnp->cn_flags |= MAKEENTRY;
3640 if (cache_fpl_islastcn(ndp))
3641 cnp->cn_flags |= ISLASTCN;
3642 if (cache_fpl_isdotdot(cnp))
3643 cnp->cn_flags |= ISDOTDOT;
3649 cache_fplookup_final_child(struct cache_fpl *fpl, enum vgetstate tvs)
3651 struct componentname *cnp;
3658 tvp_seqc = fpl->tvp_seqc;
3660 if ((cnp->cn_flags & LOCKLEAF) != 0) {
3661 lkflags = LK_SHARED;
3662 if ((cnp->cn_flags & LOCKSHARED) == 0)
3663 lkflags = LK_EXCLUSIVE;
3664 error = vget_finish(tvp, lkflags, tvs);
3665 if (__predict_false(error != 0)) {
3666 return (cache_fpl_aborted(fpl));
3669 vget_finish_ref(tvp, tvs);
3672 if (!vn_seqc_consistent(tvp, tvp_seqc)) {
3673 if ((cnp->cn_flags & LOCKLEAF) != 0)
3677 return (cache_fpl_aborted(fpl));
3680 return (cache_fpl_handled(fpl, 0));
3684 * They want to possibly modify the state of the namecache.
3686 * Don't try to match the API contract, just leave.
3687 * TODO: this leaves scalability on the table
3690 cache_fplookup_final_modifying(struct cache_fpl *fpl)
3692 struct componentname *cnp;
3695 MPASS(cnp->cn_nameiop != LOOKUP);
3696 return (cache_fpl_partial(fpl));
3699 static int __noinline
3700 cache_fplookup_final_withparent(struct cache_fpl *fpl)
3702 struct componentname *cnp;
3703 enum vgetstate dvs, tvs;
3704 struct vnode *dvp, *tvp;
3705 seqc_t dvp_seqc, tvp_seqc;
3710 dvp_seqc = fpl->dvp_seqc;
3712 tvp_seqc = fpl->tvp_seqc;
3714 MPASS((cnp->cn_flags & (LOCKPARENT|WANTPARENT)) != 0);
3717 * This is less efficient than it can be for simplicity.
3719 dvs = vget_prep_smr(dvp);
3720 if (__predict_false(dvs == VGET_NONE)) {
3721 return (cache_fpl_aborted(fpl));
3723 tvs = vget_prep_smr(tvp);
3724 if (__predict_false(tvs == VGET_NONE)) {
3725 cache_fpl_smr_exit(fpl);
3726 vget_abort(dvp, dvs);
3727 return (cache_fpl_aborted(fpl));
3730 cache_fpl_smr_exit(fpl);
3732 if ((cnp->cn_flags & LOCKPARENT) != 0) {
3733 error = vget_finish(dvp, LK_EXCLUSIVE, dvs);
3734 if (__predict_false(error != 0)) {
3735 vget_abort(tvp, tvs);
3736 return (cache_fpl_aborted(fpl));
3739 vget_finish_ref(dvp, dvs);
3742 if (!vn_seqc_consistent(dvp, dvp_seqc)) {
3743 vget_abort(tvp, tvs);
3744 if ((cnp->cn_flags & LOCKPARENT) != 0)
3748 return (cache_fpl_aborted(fpl));
3751 error = cache_fplookup_final_child(fpl, tvs);
3752 if (__predict_false(error != 0)) {
3753 MPASS(fpl->status == CACHE_FPL_STATUS_ABORTED);
3754 if ((cnp->cn_flags & LOCKPARENT) != 0)
3761 MPASS(fpl->status == CACHE_FPL_STATUS_HANDLED);
3766 cache_fplookup_final(struct cache_fpl *fpl)
3768 struct componentname *cnp;
3770 struct vnode *dvp, *tvp;
3771 seqc_t dvp_seqc, tvp_seqc;
3775 dvp_seqc = fpl->dvp_seqc;
3777 tvp_seqc = fpl->tvp_seqc;
3779 VNPASS(cache_fplookup_vnode_supported(dvp), dvp);
3781 if (cnp->cn_nameiop != LOOKUP) {
3782 return (cache_fplookup_final_modifying(fpl));
3785 if ((cnp->cn_flags & (LOCKPARENT|WANTPARENT)) != 0)
3786 return (cache_fplookup_final_withparent(fpl));
3788 tvs = vget_prep_smr(tvp);
3789 if (__predict_false(tvs == VGET_NONE)) {
3790 return (cache_fpl_partial(fpl));
3793 if (!vn_seqc_consistent(dvp, dvp_seqc)) {
3794 cache_fpl_smr_exit(fpl);
3795 vget_abort(tvp, tvs);
3796 return (cache_fpl_aborted(fpl));
3799 cache_fpl_smr_exit(fpl);
3800 return (cache_fplookup_final_child(fpl, tvs));
3803 static int __noinline
3804 cache_fplookup_dot(struct cache_fpl *fpl)
3811 fpl->tvp_seqc = vn_seqc_read_any(dvp);
3812 if (seqc_in_modify(fpl->tvp_seqc)) {
3813 return (cache_fpl_aborted(fpl));
3816 counter_u64_add(dothits, 1);
3817 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", dvp);
3822 static int __noinline
3823 cache_fplookup_dotdot(struct cache_fpl *fpl)
3825 struct nameidata *ndp;
3826 struct componentname *cnp;
3827 struct namecache *ncp;
3837 * XXX this is racy the same way regular lookup is
3839 for (pr = cnp->cn_cred->cr_prison; pr != NULL;
3841 if (dvp == pr->pr_root)
3844 if (dvp == ndp->ni_rootdir ||
3845 dvp == ndp->ni_topdir ||
3849 fpl->tvp_seqc = vn_seqc_read_any(dvp);
3850 if (seqc_in_modify(fpl->tvp_seqc)) {
3851 return (cache_fpl_aborted(fpl));
3856 if ((dvp->v_vflag & VV_ROOT) != 0) {
3859 * The opposite of climb mount is needed here.
3861 return (cache_fpl_aborted(fpl));
3864 ncp = atomic_load_ptr(&dvp->v_cache_dd);
3866 return (cache_fpl_aborted(fpl));
3869 nc_flag = atomic_load_char(&ncp->nc_flag);
3870 if ((nc_flag & NCF_ISDOTDOT) != 0) {
3871 if ((nc_flag & NCF_NEGATIVE) != 0)
3872 return (cache_fpl_aborted(fpl));
3873 fpl->tvp = ncp->nc_vp;
3875 fpl->tvp = ncp->nc_dvp;
3878 if (__predict_false(!cache_ncp_canuse(ncp))) {
3879 return (cache_fpl_aborted(fpl));
3882 fpl->tvp_seqc = vn_seqc_read_any(fpl->tvp);
3883 if (seqc_in_modify(fpl->tvp_seqc)) {
3884 return (cache_fpl_partial(fpl));
3887 counter_u64_add(dotdothits, 1);
3892 cache_fplookup_next(struct cache_fpl *fpl)
3894 struct componentname *cnp;
3895 struct namecache *ncp;
3896 struct negstate *negstate;
3897 struct vnode *dvp, *tvp;
3905 if (__predict_false(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.')) {
3906 return (cache_fplookup_dot(fpl));
3909 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
3911 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
3912 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
3913 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
3918 * If there is no entry we have to punt to the slow path to perform
3919 * actual lookup. Should there be nothing with this name a negative
3920 * entry will be created.
3922 if (__predict_false(ncp == NULL)) {
3923 return (cache_fpl_partial(fpl));
3926 tvp = atomic_load_ptr(&ncp->nc_vp);
3927 nc_flag = atomic_load_char(&ncp->nc_flag);
3928 if ((nc_flag & NCF_NEGATIVE) != 0) {
3930 * If they want to create an entry we need to replace this one.
3932 if (__predict_false(fpl->cnp->cn_nameiop != LOOKUP)) {
3933 return (cache_fpl_partial(fpl));
3935 negstate = NCP2NEGSTATE(ncp);
3936 neg_hot = ((negstate->neg_flag & NEG_HOT) != 0);
3937 if (__predict_false(!cache_ncp_canuse(ncp))) {
3938 return (cache_fpl_partial(fpl));
3940 if (__predict_false((nc_flag & NCF_WHITE) != 0)) {
3941 return (cache_fpl_partial(fpl));
3944 return (cache_fplookup_negative_promote(fpl, ncp, hash));
3946 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp,
3948 counter_u64_add(numneghits, 1);
3949 cache_fpl_smr_exit(fpl);
3950 return (cache_fpl_handled(fpl, ENOENT));
3953 if (__predict_false(!cache_ncp_canuse(ncp))) {
3954 return (cache_fpl_partial(fpl));
3958 fpl->tvp_seqc = vn_seqc_read_any(tvp);
3959 if (seqc_in_modify(fpl->tvp_seqc)) {
3960 return (cache_fpl_partial(fpl));
3963 if (!cache_fplookup_vnode_supported(tvp)) {
3964 return (cache_fpl_partial(fpl));
3967 counter_u64_add(numposhits, 1);
3968 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name, tvp);
3973 cache_fplookup_mp_supported(struct mount *mp)
3978 if ((mp->mnt_kern_flag & MNTK_FPLOOKUP) == 0)
3984 * Walk up the mount stack (if any).
3986 * Correctness is provided in the following ways:
3987 * - all vnodes are protected from freeing with SMR
3988 * - struct mount objects are type stable making them always safe to access
3989 * - stability of the particular mount is provided by busying it
3990 * - relationship between the vnode which is mounted on and the mount is
3991 * verified with the vnode sequence counter after busying
3992 * - association between root vnode of the mount and the mount is protected
3995 * From that point on we can read the sequence counter of the root vnode
3996 * and get the next mount on the stack (if any) using the same protection.
3998 * By the end of successful walk we are guaranteed the reached state was
3999 * indeed present at least at some point which matches the regular lookup.
4001 static int __noinline
4002 cache_fplookup_climb_mount(struct cache_fpl *fpl)
4004 struct mount *mp, *prev_mp;
4009 vp_seqc = fpl->tvp_seqc;
4011 VNPASS(vp->v_type == VDIR || vp->v_type == VBAD, vp);
4012 mp = atomic_load_ptr(&vp->v_mountedhere);
4018 if (!vfs_op_thread_enter_crit(mp)) {
4019 if (prev_mp != NULL)
4020 vfs_op_thread_exit_crit(prev_mp);
4021 return (cache_fpl_partial(fpl));
4023 if (prev_mp != NULL)
4024 vfs_op_thread_exit_crit(prev_mp);
4025 if (!vn_seqc_consistent(vp, vp_seqc)) {
4026 vfs_op_thread_exit_crit(mp);
4027 return (cache_fpl_partial(fpl));
4029 if (!cache_fplookup_mp_supported(mp)) {
4030 vfs_op_thread_exit_crit(mp);
4031 return (cache_fpl_partial(fpl));
4033 vp = atomic_load_ptr(&mp->mnt_rootvnode);
4034 if (vp == NULL || VN_IS_DOOMED(vp)) {
4035 vfs_op_thread_exit_crit(mp);
4036 return (cache_fpl_partial(fpl));
4038 vp_seqc = vn_seqc_read_any(vp);
4039 if (seqc_in_modify(vp_seqc)) {
4040 vfs_op_thread_exit_crit(mp);
4041 return (cache_fpl_partial(fpl));
4044 mp = atomic_load_ptr(&vp->v_mountedhere);
4049 vfs_op_thread_exit_crit(prev_mp);
4051 fpl->tvp_seqc = vp_seqc;
4056 cache_fplookup_need_climb_mount(struct cache_fpl *fpl)
4064 * Hack: while this is a union, the pointer tends to be NULL so save on
4067 mp = atomic_load_ptr(&vp->v_mountedhere);
4070 if (vp->v_type == VDIR)
4078 * The code is mostly copy-pasted from regular lookup, see lookup().
4079 * The structure is maintained along with comments for easier maintenance.
4080 * Deduplicating the code will become feasible after fast path lookup
4081 * becomes more feature-complete.
4084 cache_fplookup_parse(struct cache_fpl *fpl)
4086 struct nameidata *ndp;
4087 struct componentname *cnp;
4094 * Search a new directory.
4096 * The last component of the filename is left accessible via
4097 * cnp->cn_nameptr for callers that need the name. Callers needing
4098 * the name set the SAVENAME flag. When done, they assume
4099 * responsibility for freeing the pathname buffer.
4101 for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
4103 cnp->cn_namelen = cp - cnp->cn_nameptr;
4104 if (__predict_false(cnp->cn_namelen > NAME_MAX)) {
4105 cache_fpl_smr_exit(fpl);
4106 return (cache_fpl_handled(fpl, ENAMETOOLONG));
4108 ndp->ni_pathlen -= cnp->cn_namelen;
4109 KASSERT(ndp->ni_pathlen <= PATH_MAX,
4110 ("%s: ni_pathlen underflow to %zd\n", __func__, ndp->ni_pathlen));
4114 * Replace multiple slashes by a single slash and trailing slashes
4115 * by a null. This must be done before VOP_LOOKUP() because some
4116 * fs's don't know about trailing slashes. Remember if there were
4117 * trailing slashes to handle symlinks, existing non-directories
4118 * and non-existing files that won't be directories specially later.
4120 while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
4126 * Regular lookup performs the following:
4127 * *ndp->ni_next = '\0';
4128 * cnp->cn_flags |= TRAILINGSLASH;
4130 * Which is problematic since it modifies data read
4131 * from userspace. Then if fast path lookup was to
4132 * abort we would have to either restore it or convey
4133 * the flag. Since this is a corner case just ignore
4134 * it for simplicity.
4136 return (cache_fpl_partial(fpl));
4142 * Check for degenerate name (e.g. / or "")
4143 * which is a way of talking about a directory,
4144 * e.g. like "/." or ".".
4147 * Another corner case handled by the regular lookup
4149 if (__predict_false(cnp->cn_nameptr[0] == '\0')) {
4150 return (cache_fpl_partial(fpl));
4156 cache_fplookup_parse_advance(struct cache_fpl *fpl)
4158 struct nameidata *ndp;
4159 struct componentname *cnp;
4164 cnp->cn_nameptr = ndp->ni_next;
4165 while (*cnp->cn_nameptr == '/') {
4171 static int __noinline
4172 cache_fplookup_failed_vexec(struct cache_fpl *fpl, int error)
4178 * Can happen when racing against vgone.
4181 cache_fpl_partial(fpl);
4185 * See the API contract for VOP_FPLOOKUP_VEXEC.
4187 if (!vn_seqc_consistent(fpl->dvp, fpl->dvp_seqc)) {
4188 error = cache_fpl_aborted(fpl);
4190 cache_fpl_smr_exit(fpl);
4191 cache_fpl_handled(fpl, error);
4199 cache_fplookup_impl(struct vnode *dvp, struct cache_fpl *fpl)
4201 struct nameidata *ndp;
4202 struct componentname *cnp;
4206 error = CACHE_FPL_FAILED;
4210 cache_fpl_checkpoint(fpl, &fpl->snd);
4213 fpl->dvp_seqc = vn_seqc_read_any(fpl->dvp);
4214 if (seqc_in_modify(fpl->dvp_seqc)) {
4215 cache_fpl_aborted(fpl);
4218 mp = atomic_load_ptr(&fpl->dvp->v_mount);
4219 if (!cache_fplookup_mp_supported(mp)) {
4220 cache_fpl_aborted(fpl);
4224 VNPASS(cache_fplookup_vnode_supported(fpl->dvp), fpl->dvp);
4227 error = cache_fplookup_parse(fpl);
4228 if (__predict_false(error != 0)) {
4232 VNPASS(cache_fplookup_vnode_supported(fpl->dvp), fpl->dvp);
4234 error = VOP_FPLOOKUP_VEXEC(fpl->dvp, cnp->cn_cred);
4235 if (__predict_false(error != 0)) {
4236 error = cache_fplookup_failed_vexec(fpl, error);
4240 if (__predict_false(cache_fpl_isdotdot(cnp))) {
4241 error = cache_fplookup_dotdot(fpl);
4242 if (__predict_false(error != 0)) {
4246 error = cache_fplookup_next(fpl);
4247 if (__predict_false(error != 0)) {
4251 VNPASS(!seqc_in_modify(fpl->tvp_seqc), fpl->tvp);
4253 if (cache_fplookup_need_climb_mount(fpl)) {
4254 error = cache_fplookup_climb_mount(fpl);
4255 if (__predict_false(error != 0)) {
4261 VNPASS(!seqc_in_modify(fpl->tvp_seqc), fpl->tvp);
4263 if (cache_fpl_islastcn(ndp)) {
4264 error = cache_fplookup_final(fpl);
4268 if (!vn_seqc_consistent(fpl->dvp, fpl->dvp_seqc)) {
4269 error = cache_fpl_aborted(fpl);
4273 fpl->dvp = fpl->tvp;
4274 fpl->dvp_seqc = fpl->tvp_seqc;
4276 cache_fplookup_parse_advance(fpl);
4277 cache_fpl_checkpoint(fpl, &fpl->snd);
4280 switch (fpl->status) {
4281 case CACHE_FPL_STATUS_UNSET:
4282 __assert_unreachable();
4284 case CACHE_FPL_STATUS_PARTIAL:
4285 cache_fpl_smr_assert_entered(fpl);
4286 return (cache_fplookup_partial_setup(fpl));
4287 case CACHE_FPL_STATUS_ABORTED:
4289 cache_fpl_smr_exit(fpl);
4290 return (CACHE_FPL_FAILED);
4291 case CACHE_FPL_STATUS_HANDLED:
4292 MPASS(error != CACHE_FPL_FAILED);
4293 cache_fpl_smr_assert_not_entered(fpl);
4294 if (__predict_false(error != 0)) {
4297 cache_fpl_cleanup_cnp(cnp);
4300 ndp->ni_dvp = fpl->dvp;
4301 ndp->ni_vp = fpl->tvp;
4302 if (cnp->cn_flags & SAVENAME)
4303 cnp->cn_flags |= HASBUF;
4305 cache_fpl_cleanup_cnp(cnp);
4311 * Fast path lookup protected with SMR and sequence counters.
4313 * Note: all VOP_FPLOOKUP_VEXEC routines have a comment referencing this one.
4315 * Filesystems can opt in by setting the MNTK_FPLOOKUP flag and meeting criteria
4318 * Traditional vnode lookup conceptually looks like this:
4324 * vn_unlock(current);
4331 * Each jump to the next vnode is safe memory-wise and atomic with respect to
4332 * any modifications thanks to holding respective locks.
4334 * The same guarantee can be provided with a combination of safe memory
4335 * reclamation and sequence counters instead. If all operations which affect
4336 * the relationship between the current vnode and the one we are looking for
4337 * also modify the counter, we can verify whether all the conditions held as
4338 * we made the jump. This includes things like permissions, mount points etc.
4339 * Counter modification is provided by enclosing relevant places in
4340 * vn_seqc_write_begin()/end() calls.
4342 * Thus this translates to:
4345 * dvp_seqc = seqc_read_any(dvp);
4346 * if (seqc_in_modify(dvp_seqc)) // someone is altering the vnode
4350 * tvp_seqc = seqc_read_any(tvp);
4351 * if (seqc_in_modify(tvp_seqc)) // someone is altering the target vnode
4353 * if (!seqc_consistent(dvp, dvp_seqc) // someone is altering the vnode
4355 * dvp = tvp; // we know nothing of importance has changed
4356 * dvp_seqc = tvp_seqc; // store the counter for the tvp iteration
4360 * vget(); // secure the vnode
4361 * if (!seqc_consistent(tvp, tvp_seqc) // final check
4363 * // at this point we know nothing has changed for any parent<->child pair
4364 * // as they were crossed during the lookup, meaning we matched the guarantee
4365 * // of the locked variant
4368 * The API contract for VOP_FPLOOKUP_VEXEC routines is as follows:
4369 * - they are called while within vfs_smr protection which they must never exit
4370 * - EAGAIN can be returned to denote checking could not be performed, it is
4371 * always valid to return it
4372 * - if the sequence counter has not changed the result must be valid
4373 * - if the sequence counter has changed both false positives and false negatives
4374 * are permitted (since the result will be rejected later)
4375 * - for simple cases of unix permission checks vaccess_vexec_smr can be used
4377 * Caveats to watch out for:
4378 * - vnodes are passed unlocked and unreferenced with nothing stopping
4379 * VOP_RECLAIM, in turn meaning that ->v_data can become NULL. It is advised
4380 * to use atomic_load_ptr to fetch it.
4381 * - the aforementioned object can also get freed, meaning absent other means it
4382 * should be protected with vfs_smr
4383 * - either safely checking permissions as they are modified or guaranteeing
4384 * their stability is left to the routine
4387 cache_fplookup(struct nameidata *ndp, enum cache_fpl_status *status,
4390 struct cache_fpl fpl;
4393 struct componentname *cnp;
4394 struct nameidata_saved orig;
4397 MPASS(ndp->ni_lcf == 0);
4399 fpl.status = CACHE_FPL_STATUS_UNSET;
4401 fpl.cnp = &ndp->ni_cnd;
4402 MPASS(curthread == fpl.cnp->cn_thread);
4404 if ((fpl.cnp->cn_flags & SAVESTART) != 0)
4405 MPASS(fpl.cnp->cn_nameiop != LOOKUP);
4407 if (!cache_can_fplookup(&fpl)) {
4408 SDT_PROBE3(vfs, fplookup, lookup, done, ndp, fpl.line, fpl.status);
4409 *status = fpl.status;
4410 return (EOPNOTSUPP);
4413 cache_fpl_checkpoint(&fpl, &orig);
4415 cache_fpl_smr_enter_initial(&fpl);
4416 pwd = pwd_get_smr();
4418 ndp->ni_rootdir = pwd->pwd_rdir;
4419 ndp->ni_topdir = pwd->pwd_jdir;
4422 cnp->cn_nameptr = cnp->cn_pnbuf;
4423 if (cnp->cn_pnbuf[0] == '/') {
4424 cache_fpl_handle_root(ndp, &dvp);
4426 MPASS(ndp->ni_dirfd == AT_FDCWD);
4427 dvp = pwd->pwd_cdir;
4430 SDT_PROBE4(vfs, namei, lookup, entry, dvp, cnp->cn_pnbuf, cnp->cn_flags, true);
4432 error = cache_fplookup_impl(dvp, &fpl);
4433 cache_fpl_smr_assert_not_entered(&fpl);
4434 SDT_PROBE3(vfs, fplookup, lookup, done, ndp, fpl.line, fpl.status);
4436 *status = fpl.status;
4437 switch (fpl.status) {
4438 case CACHE_FPL_STATUS_UNSET:
4439 __assert_unreachable();
4441 case CACHE_FPL_STATUS_HANDLED:
4442 SDT_PROBE3(vfs, namei, lookup, return, error,
4443 (error == 0 ? ndp->ni_vp : NULL), true);
4445 case CACHE_FPL_STATUS_PARTIAL:
4448 * Status restored by cache_fplookup_partial_setup.
4451 case CACHE_FPL_STATUS_ABORTED:
4452 cache_fpl_restore(&fpl, &orig);