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 ncsize; /* the size as computed on creation or resizing */
300 struct nchstats nchstats; /* cache effectiveness statistics */
302 static bool __read_frequently cache_fast_revlookup = true;
303 SYSCTL_BOOL(_vfs, OID_AUTO, cache_fast_revlookup, CTLFLAG_RW,
304 &cache_fast_revlookup, 0, "");
306 static struct mtx __exclusive_cache_line ncneg_shrink_lock;
310 TAILQ_HEAD(, namecache) nl_list;
311 } __aligned(CACHE_LINE_SIZE);
313 static struct neglist __read_mostly *neglists;
314 static struct neglist ncneg_hot;
315 static u_long numhotneg;
318 #define numneglists (ncneghash + 1)
319 static inline struct neglist *
320 NCP2NEGLIST(struct namecache *ncp)
323 return (&neglists[(((uintptr_t)(ncp) >> 8) & ncneghash)]);
326 static inline struct negstate *
327 NCP2NEGSTATE(struct namecache *ncp)
330 MPASS(ncp->nc_flag & NCF_NEGATIVE);
331 return (&ncp->nc_neg);
334 #define numbucketlocks (ncbuckethash + 1)
335 static u_int __read_mostly ncbuckethash;
336 static struct mtx_padalign __read_mostly *bucketlocks;
337 #define HASH2BUCKETLOCK(hash) \
338 ((struct mtx *)(&bucketlocks[((hash) & ncbuckethash)]))
340 #define numvnodelocks (ncvnodehash + 1)
341 static u_int __read_mostly ncvnodehash;
342 static struct mtx __read_mostly *vnodelocks;
343 static inline struct mtx *
344 VP2VNODELOCK(struct vnode *vp)
347 return (&vnodelocks[(((uintptr_t)(vp) >> 8) & ncvnodehash)]);
351 * UMA zones for the VFS cache.
353 * The small cache is used for entries with short names, which are the
354 * most common. The large cache is used for entries which are too big to
355 * fit in the small cache.
357 static uma_zone_t __read_mostly cache_zone_small;
358 static uma_zone_t __read_mostly cache_zone_small_ts;
359 static uma_zone_t __read_mostly cache_zone_large;
360 static uma_zone_t __read_mostly cache_zone_large_ts;
362 static struct namecache *
363 cache_alloc(int len, int ts)
365 struct namecache_ts *ncp_ts;
366 struct namecache *ncp;
368 if (__predict_false(ts)) {
369 if (len <= CACHE_PATH_CUTOFF)
370 ncp_ts = uma_zalloc_smr(cache_zone_small_ts, M_WAITOK);
372 ncp_ts = uma_zalloc_smr(cache_zone_large_ts, M_WAITOK);
373 ncp = &ncp_ts->nc_nc;
375 if (len <= CACHE_PATH_CUTOFF)
376 ncp = uma_zalloc_smr(cache_zone_small, M_WAITOK);
378 ncp = uma_zalloc_smr(cache_zone_large, M_WAITOK);
384 cache_free(struct namecache *ncp)
386 struct namecache_ts *ncp_ts;
389 if ((ncp->nc_flag & NCF_DVDROP) != 0)
391 if (__predict_false(ncp->nc_flag & NCF_TS)) {
392 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
393 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
394 uma_zfree_smr(cache_zone_small_ts, ncp_ts);
396 uma_zfree_smr(cache_zone_large_ts, ncp_ts);
398 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
399 uma_zfree_smr(cache_zone_small, ncp);
401 uma_zfree_smr(cache_zone_large, ncp);
406 cache_out_ts(struct namecache *ncp, struct timespec *tsp, int *ticksp)
408 struct namecache_ts *ncp_ts;
410 KASSERT((ncp->nc_flag & NCF_TS) != 0 ||
411 (tsp == NULL && ticksp == NULL),
417 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
418 *tsp = ncp_ts->nc_time;
419 *ticksp = ncp_ts->nc_ticks;
423 static int __read_mostly doingcache = 1; /* 1 => enable the cache */
424 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0,
425 "VFS namecache enabled");
428 /* Export size information to userland */
429 SYSCTL_INT(_debug_sizeof, OID_AUTO, namecache, CTLFLAG_RD, SYSCTL_NULL_INT_PTR,
430 sizeof(struct namecache), "sizeof(struct namecache)");
433 * The new name cache statistics
435 static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
436 "Name cache statistics");
437 #define STATNODE_ULONG(name, descr) \
438 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, descr);
439 #define STATNODE_COUNTER(name, descr) \
440 static COUNTER_U64_DEFINE_EARLY(name); \
441 SYSCTL_COUNTER_U64(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, \
443 STATNODE_ULONG(numneg, "Number of negative cache entries");
444 STATNODE_ULONG(numcache, "Number of cache entries");
445 STATNODE_COUNTER(numcachehv, "Number of namecache entries with vnodes held");
446 STATNODE_COUNTER(numdrops, "Number of dropped entries due to reaching the limit");
447 STATNODE_COUNTER(dothits, "Number of '.' hits");
448 STATNODE_COUNTER(dotdothits, "Number of '..' hits");
449 STATNODE_COUNTER(nummiss, "Number of cache misses");
450 STATNODE_COUNTER(nummisszap, "Number of cache misses we do not want to cache");
451 STATNODE_COUNTER(numposzaps,
452 "Number of cache hits (positive) we do not want to cache");
453 STATNODE_COUNTER(numposhits, "Number of cache hits (positive)");
454 STATNODE_COUNTER(numnegzaps,
455 "Number of cache hits (negative) we do not want to cache");
456 STATNODE_COUNTER(numneghits, "Number of cache hits (negative)");
457 /* These count for vn_getcwd(), too. */
458 STATNODE_COUNTER(numfullpathcalls, "Number of fullpath search calls");
459 STATNODE_COUNTER(numfullpathfail1, "Number of fullpath search errors (ENOTDIR)");
460 STATNODE_COUNTER(numfullpathfail2,
461 "Number of fullpath search errors (VOP_VPTOCNP failures)");
462 STATNODE_COUNTER(numfullpathfail4, "Number of fullpath search errors (ENOMEM)");
463 STATNODE_COUNTER(numfullpathfound, "Number of successful fullpath calls");
464 STATNODE_COUNTER(zap_and_exit_bucket_relock_success,
465 "Number of successful removals after relocking");
466 static long zap_and_exit_bucket_fail; STATNODE_ULONG(zap_and_exit_bucket_fail,
467 "Number of times zap_and_exit failed to lock");
468 static long zap_and_exit_bucket_fail2; STATNODE_ULONG(zap_and_exit_bucket_fail2,
469 "Number of times zap_and_exit failed to lock");
470 static long cache_lock_vnodes_cel_3_failures;
471 STATNODE_ULONG(cache_lock_vnodes_cel_3_failures,
472 "Number of times 3-way vnode locking failed");
473 STATNODE_ULONG(numhotneg, "Number of hot negative entries");
474 STATNODE_COUNTER(numneg_evicted,
475 "Number of negative entries evicted when adding a new entry");
476 STATNODE_COUNTER(shrinking_skipped,
477 "Number of times shrinking was already in progress");
479 static void cache_zap_locked(struct namecache *ncp);
480 static int vn_fullpath_hardlink(struct nameidata *ndp, char **retbuf,
481 char **freebuf, size_t *buflen);
482 static int vn_fullpath_any_smr(struct vnode *vp, struct vnode *rdir, char *buf,
483 char **retbuf, size_t *buflen, bool slash_prefixed, size_t addend);
484 static int vn_fullpath_any(struct vnode *vp, struct vnode *rdir, char *buf,
485 char **retbuf, size_t *buflen);
486 static int vn_fullpath_dir(struct vnode *vp, struct vnode *rdir, char *buf,
487 char **retbuf, size_t *len, bool slash_prefixed, size_t addend);
489 static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
492 cache_assert_vlp_locked(struct mtx *vlp)
496 mtx_assert(vlp, MA_OWNED);
500 cache_assert_vnode_locked(struct vnode *vp)
504 vlp = VP2VNODELOCK(vp);
505 cache_assert_vlp_locked(vlp);
509 * TODO: With the value stored we can do better than computing the hash based
510 * on the address. The choice of FNV should also be revisited.
513 cache_prehash(struct vnode *vp)
516 vp->v_nchash = fnv_32_buf(&vp, sizeof(vp), FNV1_32_INIT);
520 cache_get_hash(char *name, u_char len, struct vnode *dvp)
523 return (fnv_32_buf(name, len, dvp->v_nchash));
526 static inline struct nchashhead *
527 NCP2BUCKET(struct namecache *ncp)
531 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen, ncp->nc_dvp);
532 return (NCHHASH(hash));
535 static inline struct mtx *
536 NCP2BUCKETLOCK(struct namecache *ncp)
540 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen, ncp->nc_dvp);
541 return (HASH2BUCKETLOCK(hash));
546 cache_assert_bucket_locked(struct namecache *ncp)
550 blp = NCP2BUCKETLOCK(ncp);
551 mtx_assert(blp, MA_OWNED);
555 cache_assert_bucket_unlocked(struct namecache *ncp)
559 blp = NCP2BUCKETLOCK(ncp);
560 mtx_assert(blp, MA_NOTOWNED);
563 #define cache_assert_bucket_locked(x) do { } while (0)
564 #define cache_assert_bucket_unlocked(x) do { } while (0)
567 #define cache_sort_vnodes(x, y) _cache_sort_vnodes((void **)(x), (void **)(y))
569 _cache_sort_vnodes(void **p1, void **p2)
573 MPASS(*p1 != NULL || *p2 != NULL);
583 cache_lock_all_buckets(void)
587 for (i = 0; i < numbucketlocks; i++)
588 mtx_lock(&bucketlocks[i]);
592 cache_unlock_all_buckets(void)
596 for (i = 0; i < numbucketlocks; i++)
597 mtx_unlock(&bucketlocks[i]);
601 cache_lock_all_vnodes(void)
605 for (i = 0; i < numvnodelocks; i++)
606 mtx_lock(&vnodelocks[i]);
610 cache_unlock_all_vnodes(void)
614 for (i = 0; i < numvnodelocks; i++)
615 mtx_unlock(&vnodelocks[i]);
619 cache_trylock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
622 cache_sort_vnodes(&vlp1, &vlp2);
625 if (!mtx_trylock(vlp1))
628 if (!mtx_trylock(vlp2)) {
638 cache_lock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
641 MPASS(vlp1 != NULL || vlp2 != NULL);
651 cache_unlock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
654 MPASS(vlp1 != NULL || vlp2 != NULL);
663 sysctl_nchstats(SYSCTL_HANDLER_ARGS)
665 struct nchstats snap;
667 if (req->oldptr == NULL)
668 return (SYSCTL_OUT(req, 0, sizeof(snap)));
671 snap.ncs_goodhits = counter_u64_fetch(numposhits);
672 snap.ncs_neghits = counter_u64_fetch(numneghits);
673 snap.ncs_badhits = counter_u64_fetch(numposzaps) +
674 counter_u64_fetch(numnegzaps);
675 snap.ncs_miss = counter_u64_fetch(nummisszap) +
676 counter_u64_fetch(nummiss);
678 return (SYSCTL_OUT(req, &snap, sizeof(snap)));
680 SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE | CTLFLAG_RD |
681 CTLFLAG_MPSAFE, 0, 0, sysctl_nchstats, "LU",
682 "VFS cache effectiveness statistics");
686 * Grab an atomic snapshot of the name cache hash chain lengths
688 static SYSCTL_NODE(_debug, OID_AUTO, hashstat,
689 CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
693 sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS)
695 struct nchashhead *ncpp;
696 struct namecache *ncp;
697 int i, error, n_nchash, *cntbuf;
700 n_nchash = nchash + 1; /* nchash is max index, not count */
701 if (req->oldptr == NULL)
702 return SYSCTL_OUT(req, 0, n_nchash * sizeof(int));
703 cntbuf = malloc(n_nchash * sizeof(int), M_TEMP, M_ZERO | M_WAITOK);
704 cache_lock_all_buckets();
705 if (n_nchash != nchash + 1) {
706 cache_unlock_all_buckets();
707 free(cntbuf, M_TEMP);
710 /* Scan hash tables counting entries */
711 for (ncpp = nchashtbl, i = 0; i < n_nchash; ncpp++, i++)
712 CK_SLIST_FOREACH(ncp, ncpp, nc_hash)
714 cache_unlock_all_buckets();
715 for (error = 0, i = 0; i < n_nchash; i++)
716 if ((error = SYSCTL_OUT(req, &cntbuf[i], sizeof(int))) != 0)
718 free(cntbuf, M_TEMP);
721 SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD|
722 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_rawnchash, "S,int",
723 "nchash chain lengths");
726 sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS)
729 struct nchashhead *ncpp;
730 struct namecache *ncp;
732 int count, maxlength, used, pct;
735 return SYSCTL_OUT(req, 0, 4 * sizeof(int));
737 cache_lock_all_buckets();
738 n_nchash = nchash + 1; /* nchash is max index, not count */
742 /* Scan hash tables for applicable entries */
743 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
745 CK_SLIST_FOREACH(ncp, ncpp, nc_hash) {
750 if (maxlength < count)
753 n_nchash = nchash + 1;
754 cache_unlock_all_buckets();
755 pct = (used * 100) / (n_nchash / 100);
756 error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash));
759 error = SYSCTL_OUT(req, &used, sizeof(used));
762 error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength));
765 error = SYSCTL_OUT(req, &pct, sizeof(pct));
770 SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD|
771 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_nchash, "I",
772 "nchash statistics (number of total/used buckets, maximum chain length, usage percentage)");
776 * Negative entries management
778 * A variation of LRU scheme is used. New entries are hashed into one of
779 * numneglists cold lists. Entries get promoted to the hot list on first hit.
781 * The shrinker will demote hot list head and evict from the cold list in a
782 * round-robin manner.
785 cache_negative_init(struct namecache *ncp)
787 struct negstate *negstate;
789 ncp->nc_flag |= NCF_NEGATIVE;
790 negstate = NCP2NEGSTATE(ncp);
791 negstate->neg_flag = 0;
795 cache_negative_hit(struct namecache *ncp)
797 struct neglist *neglist;
798 struct negstate *negstate;
800 negstate = NCP2NEGSTATE(ncp);
801 if ((negstate->neg_flag & NEG_HOT) != 0)
803 neglist = NCP2NEGLIST(ncp);
804 mtx_lock(&ncneg_hot.nl_lock);
805 mtx_lock(&neglist->nl_lock);
806 if ((negstate->neg_flag & NEG_HOT) == 0) {
808 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
809 TAILQ_INSERT_TAIL(&ncneg_hot.nl_list, ncp, nc_dst);
810 negstate->neg_flag |= NEG_HOT;
812 mtx_unlock(&neglist->nl_lock);
813 mtx_unlock(&ncneg_hot.nl_lock);
817 cache_negative_insert(struct namecache *ncp)
819 struct neglist *neglist;
821 MPASS(ncp->nc_flag & NCF_NEGATIVE);
822 cache_assert_bucket_locked(ncp);
823 neglist = NCP2NEGLIST(ncp);
824 mtx_lock(&neglist->nl_lock);
825 TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst);
826 mtx_unlock(&neglist->nl_lock);
827 atomic_add_long(&numneg, 1);
831 cache_negative_remove(struct namecache *ncp)
833 struct neglist *neglist;
834 struct negstate *negstate;
835 bool hot_locked = false;
836 bool list_locked = false;
838 cache_assert_bucket_locked(ncp);
839 neglist = NCP2NEGLIST(ncp);
840 negstate = NCP2NEGSTATE(ncp);
841 if ((negstate->neg_flag & NEG_HOT) != 0) {
843 mtx_lock(&ncneg_hot.nl_lock);
844 if ((negstate->neg_flag & NEG_HOT) == 0) {
846 mtx_lock(&neglist->nl_lock);
850 mtx_lock(&neglist->nl_lock);
852 * We may be racing against promotion in lockless lookup.
854 if ((negstate->neg_flag & NEG_HOT) != 0) {
855 mtx_unlock(&neglist->nl_lock);
857 mtx_lock(&ncneg_hot.nl_lock);
858 mtx_lock(&neglist->nl_lock);
861 if ((negstate->neg_flag & NEG_HOT) != 0) {
862 mtx_assert(&ncneg_hot.nl_lock, MA_OWNED);
863 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
866 mtx_assert(&neglist->nl_lock, MA_OWNED);
867 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
870 mtx_unlock(&neglist->nl_lock);
872 mtx_unlock(&ncneg_hot.nl_lock);
873 atomic_subtract_long(&numneg, 1);
877 cache_negative_shrink_select(struct namecache **ncpp,
878 struct neglist **neglistpp)
880 struct neglist *neglist;
881 struct namecache *ncp;
887 for (i = 0; i < numneglists; i++) {
888 neglist = &neglists[(cycle + i) % numneglists];
889 if (TAILQ_FIRST(&neglist->nl_list) == NULL)
891 mtx_lock(&neglist->nl_lock);
892 ncp = TAILQ_FIRST(&neglist->nl_list);
895 mtx_unlock(&neglist->nl_lock);
898 *neglistpp = neglist;
904 cache_negative_zap_one(void)
906 struct namecache *ncp, *ncp2;
907 struct neglist *neglist;
908 struct negstate *negstate;
912 if (mtx_owner(&ncneg_shrink_lock) != NULL ||
913 !mtx_trylock(&ncneg_shrink_lock)) {
914 counter_u64_add(shrinking_skipped, 1);
918 mtx_lock(&ncneg_hot.nl_lock);
919 ncp = TAILQ_FIRST(&ncneg_hot.nl_list);
921 neglist = NCP2NEGLIST(ncp);
922 negstate = NCP2NEGSTATE(ncp);
923 mtx_lock(&neglist->nl_lock);
924 MPASS((negstate->neg_flag & NEG_HOT) != 0);
925 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
926 TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst);
927 negstate->neg_flag &= ~NEG_HOT;
929 mtx_unlock(&neglist->nl_lock);
931 mtx_unlock(&ncneg_hot.nl_lock);
933 cache_negative_shrink_select(&ncp, &neglist);
935 mtx_unlock(&ncneg_shrink_lock);
939 MPASS(ncp->nc_flag & NCF_NEGATIVE);
940 dvlp = VP2VNODELOCK(ncp->nc_dvp);
941 blp = NCP2BUCKETLOCK(ncp);
942 mtx_unlock(&neglist->nl_lock);
946 * Enter SMR to safely check the negative list.
947 * Even if the found pointer matches, the entry may now be reallocated
948 * and used by a different vnode.
951 ncp2 = TAILQ_FIRST(&neglist->nl_list);
952 if (ncp != ncp2 || dvlp != VP2VNODELOCK(ncp2->nc_dvp) ||
953 blp != NCP2BUCKETLOCK(ncp2)) {
958 SDT_PROBE2(vfs, namecache, shrink_negative, done, ncp->nc_dvp,
960 cache_zap_locked(ncp);
961 counter_u64_add(numneg_evicted, 1);
970 * cache_zap_locked():
972 * Removes a namecache entry from cache, whether it contains an actual
973 * pointer to a vnode or if it is just a negative cache entry.
976 cache_zap_locked(struct namecache *ncp)
978 struct nchashhead *ncpp;
980 if (!(ncp->nc_flag & NCF_NEGATIVE))
981 cache_assert_vnode_locked(ncp->nc_vp);
982 cache_assert_vnode_locked(ncp->nc_dvp);
983 cache_assert_bucket_locked(ncp);
985 cache_ncp_invalidate(ncp);
987 ncpp = NCP2BUCKET(ncp);
988 CK_SLIST_REMOVE(ncpp, ncp, namecache, nc_hash);
989 if (!(ncp->nc_flag & NCF_NEGATIVE)) {
990 SDT_PROBE3(vfs, namecache, zap, done, ncp->nc_dvp,
991 ncp->nc_name, ncp->nc_vp);
992 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
993 if (ncp == ncp->nc_vp->v_cache_dd) {
994 vn_seqc_write_begin_unheld(ncp->nc_vp);
995 ncp->nc_vp->v_cache_dd = NULL;
996 vn_seqc_write_end(ncp->nc_vp);
999 SDT_PROBE2(vfs, namecache, zap_negative, done, ncp->nc_dvp,
1001 cache_negative_remove(ncp);
1003 if (ncp->nc_flag & NCF_ISDOTDOT) {
1004 if (ncp == ncp->nc_dvp->v_cache_dd) {
1005 vn_seqc_write_begin_unheld(ncp->nc_dvp);
1006 ncp->nc_dvp->v_cache_dd = NULL;
1007 vn_seqc_write_end(ncp->nc_dvp);
1010 LIST_REMOVE(ncp, nc_src);
1011 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) {
1012 ncp->nc_flag |= NCF_DVDROP;
1013 counter_u64_add(numcachehv, -1);
1016 atomic_subtract_long(&numcache, 1);
1020 cache_zap_negative_locked_vnode_kl(struct namecache *ncp, struct vnode *vp)
1024 MPASS(ncp->nc_dvp == vp);
1025 MPASS(ncp->nc_flag & NCF_NEGATIVE);
1026 cache_assert_vnode_locked(vp);
1028 blp = NCP2BUCKETLOCK(ncp);
1030 cache_zap_locked(ncp);
1035 cache_zap_locked_vnode_kl2(struct namecache *ncp, struct vnode *vp,
1038 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
1041 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
1042 cache_assert_vnode_locked(vp);
1044 if (ncp->nc_flag & NCF_NEGATIVE) {
1045 if (*vlpp != NULL) {
1049 cache_zap_negative_locked_vnode_kl(ncp, vp);
1053 pvlp = VP2VNODELOCK(vp);
1054 blp = NCP2BUCKETLOCK(ncp);
1055 vlp1 = VP2VNODELOCK(ncp->nc_dvp);
1056 vlp2 = VP2VNODELOCK(ncp->nc_vp);
1058 if (*vlpp == vlp1 || *vlpp == vlp2) {
1062 if (*vlpp != NULL) {
1066 cache_sort_vnodes(&vlp1, &vlp2);
1071 if (!mtx_trylock(vlp1))
1077 cache_zap_locked(ncp);
1079 if (to_unlock != NULL)
1080 mtx_unlock(to_unlock);
1087 MPASS(*vlpp == NULL);
1093 * If trylocking failed we can get here. We know enough to take all needed locks
1094 * in the right order and re-lookup the entry.
1097 cache_zap_unlocked_bucket(struct namecache *ncp, struct componentname *cnp,
1098 struct vnode *dvp, struct mtx *dvlp, struct mtx *vlp, uint32_t hash,
1101 struct namecache *rncp;
1103 cache_assert_bucket_unlocked(ncp);
1105 cache_sort_vnodes(&dvlp, &vlp);
1106 cache_lock_vnodes(dvlp, vlp);
1108 CK_SLIST_FOREACH(rncp, (NCHHASH(hash)), nc_hash) {
1109 if (rncp == ncp && rncp->nc_dvp == dvp &&
1110 rncp->nc_nlen == cnp->cn_namelen &&
1111 !bcmp(rncp->nc_name, cnp->cn_nameptr, rncp->nc_nlen))
1115 cache_zap_locked(rncp);
1117 cache_unlock_vnodes(dvlp, vlp);
1118 counter_u64_add(zap_and_exit_bucket_relock_success, 1);
1123 cache_unlock_vnodes(dvlp, vlp);
1127 static int __noinline
1128 cache_zap_locked_bucket(struct namecache *ncp, struct componentname *cnp,
1129 uint32_t hash, struct mtx *blp)
1131 struct mtx *dvlp, *vlp;
1134 cache_assert_bucket_locked(ncp);
1136 dvlp = VP2VNODELOCK(ncp->nc_dvp);
1138 if (!(ncp->nc_flag & NCF_NEGATIVE))
1139 vlp = VP2VNODELOCK(ncp->nc_vp);
1140 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1141 cache_zap_locked(ncp);
1143 cache_unlock_vnodes(dvlp, vlp);
1149 return (cache_zap_unlocked_bucket(ncp, cnp, dvp, dvlp, vlp, hash, blp));
1152 static __noinline int
1153 cache_remove_cnp(struct vnode *dvp, struct componentname *cnp)
1155 struct namecache *ncp;
1157 struct mtx *dvlp, *dvlp2;
1161 if (cnp->cn_namelen == 2 &&
1162 cnp->cn_nameptr[0] == '.' && cnp->cn_nameptr[1] == '.') {
1163 dvlp = VP2VNODELOCK(dvp);
1167 ncp = dvp->v_cache_dd;
1172 SDT_PROBE2(vfs, namecache, removecnp, miss, dvp, cnp);
1175 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1176 if (!cache_zap_locked_vnode_kl2(ncp, dvp, &dvlp2))
1178 MPASS(dvp->v_cache_dd == NULL);
1184 vn_seqc_write_begin(dvp);
1185 dvp->v_cache_dd = NULL;
1186 vn_seqc_write_end(dvp);
1191 SDT_PROBE2(vfs, namecache, removecnp, hit, dvp, cnp);
1195 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1196 blp = HASH2BUCKETLOCK(hash);
1198 if (CK_SLIST_EMPTY(NCHHASH(hash)))
1203 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1204 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1205 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1214 error = cache_zap_locked_bucket(ncp, cnp, hash, blp);
1215 if (__predict_false(error != 0)) {
1216 zap_and_exit_bucket_fail++;
1219 counter_u64_add(numposzaps, 1);
1220 SDT_PROBE2(vfs, namecache, removecnp, hit, dvp, cnp);
1224 counter_u64_add(nummisszap, 1);
1225 SDT_PROBE2(vfs, namecache, removecnp, miss, dvp, cnp);
1229 static int __noinline
1230 cache_lookup_dot(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1231 struct timespec *tsp, int *ticksp)
1236 counter_u64_add(dothits, 1);
1237 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", *vpp);
1244 * When we lookup "." we still can be asked to lock it
1247 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
1248 if (ltype != VOP_ISLOCKED(*vpp)) {
1249 if (ltype == LK_EXCLUSIVE) {
1250 vn_lock(*vpp, LK_UPGRADE | LK_RETRY);
1251 if (VN_IS_DOOMED((*vpp))) {
1252 /* forced unmount */
1258 vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY);
1263 static int __noinline
1264 cache_lookup_dotdot(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1265 struct timespec *tsp, int *ticksp)
1267 struct namecache_ts *ncp_ts;
1268 struct namecache *ncp;
1274 MPASS((cnp->cn_flags & ISDOTDOT) != 0);
1276 if ((cnp->cn_flags & MAKEENTRY) == 0) {
1277 cache_remove_cnp(dvp, cnp);
1281 counter_u64_add(dotdothits, 1);
1283 dvlp = VP2VNODELOCK(dvp);
1285 ncp = dvp->v_cache_dd;
1287 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, "..", NULL);
1291 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1292 if (ncp->nc_flag & NCF_NEGATIVE)
1299 goto negative_success;
1300 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, "..", *vpp);
1301 cache_out_ts(ncp, tsp, ticksp);
1302 if ((ncp->nc_flag & (NCF_ISDOTDOT | NCF_DTS)) ==
1303 NCF_DTS && tsp != NULL) {
1304 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1305 *tsp = ncp_ts->nc_dotdottime;
1309 ltype = VOP_ISLOCKED(dvp);
1311 vs = vget_prep(*vpp);
1313 error = vget_finish(*vpp, cnp->cn_lkflags, vs);
1314 vn_lock(dvp, ltype | LK_RETRY);
1315 if (VN_IS_DOOMED(dvp)) {
1327 if (__predict_false(cnp->cn_nameiop == CREATE)) {
1328 if (cnp->cn_flags & ISLASTCN) {
1329 counter_u64_add(numnegzaps, 1);
1330 cache_zap_negative_locked_vnode_kl(ncp, dvp);
1337 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp, ncp->nc_name);
1338 cache_out_ts(ncp, tsp, ticksp);
1339 counter_u64_add(numneghits, 1);
1340 whiteout = (ncp->nc_flag & NCF_WHITE);
1341 cache_negative_hit(ncp);
1344 cnp->cn_flags |= ISWHITEOUT;
1349 * Lookup a name in the name cache
1353 * - dvp: Parent directory in which to search.
1354 * - vpp: Return argument. Will contain desired vnode on cache hit.
1355 * - cnp: Parameters of the name search. The most interesting bits of
1356 * the cn_flags field have the following meanings:
1357 * - MAKEENTRY: If clear, free an entry from the cache rather than look
1359 * - ISDOTDOT: Must be set if and only if cn_nameptr == ".."
1360 * - tsp: Return storage for cache timestamp. On a successful (positive
1361 * or negative) lookup, tsp will be filled with any timespec that
1362 * was stored when this cache entry was created. However, it will
1363 * be clear for "." entries.
1364 * - ticks: Return storage for alternate cache timestamp. On a successful
1365 * (positive or negative) lookup, it will contain the ticks value
1366 * that was current when the cache entry was created, unless cnp
1369 * Either both tsp and ticks have to be provided or neither of them.
1373 * - -1: A positive cache hit. vpp will contain the desired vnode.
1374 * - ENOENT: A negative cache hit, or dvp was recycled out from under us due
1375 * to a forced unmount. vpp will not be modified. If the entry
1376 * is a whiteout, then the ISWHITEOUT flag will be set in
1378 * - 0: A cache miss. vpp will not be modified.
1382 * On a cache hit, vpp will be returned locked and ref'd. If we're looking up
1383 * .., dvp is unlocked. If we're looking up . an extra ref is taken, but the
1384 * lock is not recursively acquired.
1386 static int __noinline
1387 cache_lookup_fallback(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1388 struct timespec *tsp, int *ticksp)
1390 struct namecache *ncp;
1397 MPASS((cnp->cn_flags & (MAKEENTRY | ISDOTDOT)) == MAKEENTRY);
1400 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1401 blp = HASH2BUCKETLOCK(hash);
1404 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1405 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1406 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1410 if (__predict_false(ncp == NULL)) {
1412 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
1414 counter_u64_add(nummiss, 1);
1418 if (ncp->nc_flag & NCF_NEGATIVE)
1419 goto negative_success;
1421 counter_u64_add(numposhits, 1);
1423 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name, *vpp);
1424 cache_out_ts(ncp, tsp, ticksp);
1426 vs = vget_prep(*vpp);
1428 error = vget_finish(*vpp, cnp->cn_lkflags, vs);
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_bucket(ncp, cnp, hash, blp);
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 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1461 struct timespec *tsp, int *ticksp)
1463 struct namecache *ncp;
1464 struct negstate *negstate;
1471 MPASS((tsp == NULL && ticksp == NULL) || (tsp != NULL && ticksp != NULL));
1474 if (__predict_false(!doingcache)) {
1475 cnp->cn_flags &= ~MAKEENTRY;
1480 if (__predict_false(cnp->cn_nameptr[0] == '.')) {
1481 if (cnp->cn_namelen == 1)
1482 return (cache_lookup_dot(dvp, vpp, cnp, tsp, ticksp));
1483 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.')
1484 return (cache_lookup_dotdot(dvp, vpp, cnp, tsp, ticksp));
1487 MPASS((cnp->cn_flags & ISDOTDOT) == 0);
1489 if ((cnp->cn_flags & MAKEENTRY) == 0) {
1490 cache_remove_cnp(dvp, cnp);
1494 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1497 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1498 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1499 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1503 if (__predict_false(ncp == NULL)) {
1505 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
1507 counter_u64_add(nummiss, 1);
1511 nc_flag = atomic_load_char(&ncp->nc_flag);
1512 if (nc_flag & NCF_NEGATIVE)
1513 goto negative_success;
1515 counter_u64_add(numposhits, 1);
1517 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name, *vpp);
1518 cache_out_ts(ncp, tsp, ticksp);
1520 if (!cache_ncp_canuse(ncp)) {
1525 vs = vget_prep_smr(*vpp);
1527 if (__predict_false(vs == VGET_NONE)) {
1531 error = vget_finish(*vpp, cnp->cn_lkflags, vs);
1538 if (__predict_false(cnp->cn_nameiop == CREATE)) {
1539 if (cnp->cn_flags & ISLASTCN) {
1545 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp, ncp->nc_name);
1546 cache_out_ts(ncp, tsp, ticksp);
1547 counter_u64_add(numneghits, 1);
1548 whiteout = (ncp->nc_flag & NCF_WHITE);
1550 * TODO: We need to take locks to promote an entry. Code doing it
1551 * in SMR lookup can be modified to be shared.
1553 negstate = NCP2NEGSTATE(ncp);
1554 if ((negstate->neg_flag & NEG_HOT) == 0 ||
1555 !cache_ncp_canuse(ncp)) {
1561 cnp->cn_flags |= ISWHITEOUT;
1564 return (cache_lookup_fallback(dvp, vpp, cnp, tsp, ticksp));
1567 struct celockstate {
1571 CTASSERT((nitems(((struct celockstate *)0)->vlp) == 3));
1572 CTASSERT((nitems(((struct celockstate *)0)->blp) == 2));
1575 cache_celockstate_init(struct celockstate *cel)
1578 bzero(cel, sizeof(*cel));
1582 cache_lock_vnodes_cel(struct celockstate *cel, struct vnode *vp,
1585 struct mtx *vlp1, *vlp2;
1587 MPASS(cel->vlp[0] == NULL);
1588 MPASS(cel->vlp[1] == NULL);
1589 MPASS(cel->vlp[2] == NULL);
1591 MPASS(vp != NULL || dvp != NULL);
1593 vlp1 = VP2VNODELOCK(vp);
1594 vlp2 = VP2VNODELOCK(dvp);
1595 cache_sort_vnodes(&vlp1, &vlp2);
1606 cache_unlock_vnodes_cel(struct celockstate *cel)
1609 MPASS(cel->vlp[0] != NULL || cel->vlp[1] != NULL);
1611 if (cel->vlp[0] != NULL)
1612 mtx_unlock(cel->vlp[0]);
1613 if (cel->vlp[1] != NULL)
1614 mtx_unlock(cel->vlp[1]);
1615 if (cel->vlp[2] != NULL)
1616 mtx_unlock(cel->vlp[2]);
1620 cache_lock_vnodes_cel_3(struct celockstate *cel, struct vnode *vp)
1625 cache_assert_vlp_locked(cel->vlp[0]);
1626 cache_assert_vlp_locked(cel->vlp[1]);
1627 MPASS(cel->vlp[2] == NULL);
1630 vlp = VP2VNODELOCK(vp);
1633 if (vlp >= cel->vlp[1]) {
1636 if (mtx_trylock(vlp))
1638 cache_lock_vnodes_cel_3_failures++;
1639 cache_unlock_vnodes_cel(cel);
1640 if (vlp < cel->vlp[0]) {
1642 mtx_lock(cel->vlp[0]);
1643 mtx_lock(cel->vlp[1]);
1645 if (cel->vlp[0] != NULL)
1646 mtx_lock(cel->vlp[0]);
1648 mtx_lock(cel->vlp[1]);
1658 cache_lock_buckets_cel(struct celockstate *cel, struct mtx *blp1,
1662 MPASS(cel->blp[0] == NULL);
1663 MPASS(cel->blp[1] == NULL);
1665 cache_sort_vnodes(&blp1, &blp2);
1676 cache_unlock_buckets_cel(struct celockstate *cel)
1679 if (cel->blp[0] != NULL)
1680 mtx_unlock(cel->blp[0]);
1681 mtx_unlock(cel->blp[1]);
1685 * Lock part of the cache affected by the insertion.
1687 * This means vnodelocks for dvp, vp and the relevant bucketlock.
1688 * However, insertion can result in removal of an old entry. In this
1689 * case we have an additional vnode and bucketlock pair to lock.
1691 * That is, in the worst case we have to lock 3 vnodes and 2 bucketlocks, while
1692 * preserving the locking order (smaller address first).
1695 cache_enter_lock(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1698 struct namecache *ncp;
1699 struct mtx *blps[2];
1701 blps[0] = HASH2BUCKETLOCK(hash);
1704 cache_lock_vnodes_cel(cel, dvp, vp);
1705 if (vp == NULL || vp->v_type != VDIR)
1707 ncp = vp->v_cache_dd;
1710 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1712 MPASS(ncp->nc_dvp == vp);
1713 blps[1] = NCP2BUCKETLOCK(ncp);
1714 if (ncp->nc_flag & NCF_NEGATIVE)
1716 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1719 * All vnodes got re-locked. Re-validate the state and if
1720 * nothing changed we are done. Otherwise restart.
1722 if (ncp == vp->v_cache_dd &&
1723 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1724 blps[1] == NCP2BUCKETLOCK(ncp) &&
1725 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1727 cache_unlock_vnodes_cel(cel);
1732 cache_lock_buckets_cel(cel, blps[0], blps[1]);
1736 cache_enter_lock_dd(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1739 struct namecache *ncp;
1740 struct mtx *blps[2];
1742 blps[0] = HASH2BUCKETLOCK(hash);
1745 cache_lock_vnodes_cel(cel, dvp, vp);
1746 ncp = dvp->v_cache_dd;
1749 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1751 MPASS(ncp->nc_dvp == dvp);
1752 blps[1] = NCP2BUCKETLOCK(ncp);
1753 if (ncp->nc_flag & NCF_NEGATIVE)
1755 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1757 if (ncp == dvp->v_cache_dd &&
1758 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1759 blps[1] == NCP2BUCKETLOCK(ncp) &&
1760 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1762 cache_unlock_vnodes_cel(cel);
1767 cache_lock_buckets_cel(cel, blps[0], blps[1]);
1771 cache_enter_unlock(struct celockstate *cel)
1774 cache_unlock_buckets_cel(cel);
1775 cache_unlock_vnodes_cel(cel);
1778 static void __noinline
1779 cache_enter_dotdot_prep(struct vnode *dvp, struct vnode *vp,
1780 struct componentname *cnp)
1782 struct celockstate cel;
1783 struct namecache *ncp;
1787 if (dvp->v_cache_dd == NULL)
1789 len = cnp->cn_namelen;
1790 cache_celockstate_init(&cel);
1791 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1792 cache_enter_lock_dd(&cel, dvp, vp, hash);
1793 vn_seqc_write_begin(dvp);
1794 ncp = dvp->v_cache_dd;
1795 if (ncp != NULL && (ncp->nc_flag & NCF_ISDOTDOT)) {
1796 KASSERT(ncp->nc_dvp == dvp, ("wrong isdotdot parent"));
1797 cache_zap_locked(ncp);
1801 dvp->v_cache_dd = NULL;
1802 vn_seqc_write_end(dvp);
1803 cache_enter_unlock(&cel);
1809 * Add an entry to the cache.
1812 cache_enter_time(struct vnode *dvp, struct vnode *vp, struct componentname *cnp,
1813 struct timespec *tsp, struct timespec *dtsp)
1815 struct celockstate cel;
1816 struct namecache *ncp, *n2, *ndd;
1817 struct namecache_ts *ncp_ts;
1818 struct nchashhead *ncpp;
1824 VNPASS(!VN_IS_DOOMED(dvp), dvp);
1825 VNPASS(dvp->v_type != VNON, dvp);
1827 VNPASS(!VN_IS_DOOMED(vp), vp);
1828 VNPASS(vp->v_type != VNON, vp);
1832 if (__predict_false(!doingcache))
1837 if (__predict_false(cnp->cn_nameptr[0] == '.')) {
1838 if (cnp->cn_namelen == 1)
1840 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
1841 cache_enter_dotdot_prep(dvp, vp, cnp);
1842 flag = NCF_ISDOTDOT;
1847 * Avoid blowout in namecache entries.
1849 lnumcache = atomic_fetchadd_long(&numcache, 1) + 1;
1850 if (__predict_false(lnumcache >= ncsize)) {
1851 atomic_subtract_long(&numcache, 1);
1852 counter_u64_add(numdrops, 1);
1856 cache_celockstate_init(&cel);
1861 * Calculate the hash key and setup as much of the new
1862 * namecache entry as possible before acquiring the lock.
1864 ncp = cache_alloc(cnp->cn_namelen, tsp != NULL);
1865 ncp->nc_flag = flag | NCF_WIP;
1868 cache_negative_init(ncp);
1871 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1872 ncp_ts->nc_time = *tsp;
1873 ncp_ts->nc_ticks = ticks;
1874 ncp_ts->nc_nc.nc_flag |= NCF_TS;
1876 ncp_ts->nc_dotdottime = *dtsp;
1877 ncp_ts->nc_nc.nc_flag |= NCF_DTS;
1880 len = ncp->nc_nlen = cnp->cn_namelen;
1881 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1882 memcpy(ncp->nc_name, cnp->cn_nameptr, len);
1883 ncp->nc_name[len] = '\0';
1884 cache_enter_lock(&cel, dvp, vp, hash);
1887 * See if this vnode or negative entry is already in the cache
1888 * with this name. This can happen with concurrent lookups of
1889 * the same path name.
1891 ncpp = NCHHASH(hash);
1892 CK_SLIST_FOREACH(n2, ncpp, nc_hash) {
1893 if (n2->nc_dvp == dvp &&
1894 n2->nc_nlen == cnp->cn_namelen &&
1895 !bcmp(n2->nc_name, cnp->cn_nameptr, n2->nc_nlen)) {
1896 MPASS(cache_ncp_canuse(n2));
1897 if ((n2->nc_flag & NCF_NEGATIVE) != 0)
1899 ("%s: found entry pointing to a different vnode (%p != %p)",
1900 __func__, NULL, vp));
1902 KASSERT(n2->nc_vp == vp,
1903 ("%s: found entry pointing to a different vnode (%p != %p)",
1904 __func__, n2->nc_vp, vp));
1906 * Entries are supposed to be immutable unless in the
1907 * process of getting destroyed. Accommodating for
1908 * changing timestamps is possible but not worth it.
1909 * This should be harmless in terms of correctness, in
1910 * the worst case resulting in an earlier expiration.
1911 * Alternatively, the found entry can be replaced
1914 MPASS((n2->nc_flag & (NCF_TS | NCF_DTS)) == (ncp->nc_flag & (NCF_TS | NCF_DTS)));
1917 KASSERT((n2->nc_flag & NCF_TS) != 0,
1919 n2_ts = __containerof(n2, struct namecache_ts, nc_nc);
1920 n2_ts->nc_time = ncp_ts->nc_time;
1921 n2_ts->nc_ticks = ncp_ts->nc_ticks;
1923 n2_ts->nc_dotdottime = ncp_ts->nc_dotdottime;
1924 n2_ts->nc_nc.nc_flag |= NCF_DTS;
1928 goto out_unlock_free;
1932 if (flag == NCF_ISDOTDOT) {
1934 * See if we are trying to add .. entry, but some other lookup
1935 * has populated v_cache_dd pointer already.
1937 if (dvp->v_cache_dd != NULL)
1938 goto out_unlock_free;
1939 KASSERT(vp == NULL || vp->v_type == VDIR,
1940 ("wrong vnode type %p", vp));
1941 vn_seqc_write_begin(dvp);
1942 dvp->v_cache_dd = ncp;
1943 vn_seqc_write_end(dvp);
1947 if (flag != NCF_ISDOTDOT) {
1949 * For this case, the cache entry maps both the
1950 * directory name in it and the name ".." for the
1951 * directory's parent.
1953 vn_seqc_write_begin(vp);
1954 if ((ndd = vp->v_cache_dd) != NULL) {
1955 if ((ndd->nc_flag & NCF_ISDOTDOT) != 0)
1956 cache_zap_locked(ndd);
1960 vp->v_cache_dd = ncp;
1961 vn_seqc_write_end(vp);
1962 } else if (vp->v_type != VDIR) {
1963 if (vp->v_cache_dd != NULL) {
1964 vn_seqc_write_begin(vp);
1965 vp->v_cache_dd = NULL;
1966 vn_seqc_write_end(vp);
1971 if (flag != NCF_ISDOTDOT) {
1972 if (LIST_EMPTY(&dvp->v_cache_src)) {
1974 counter_u64_add(numcachehv, 1);
1976 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
1980 * If the entry is "negative", we place it into the
1981 * "negative" cache queue, otherwise, we place it into the
1982 * destination vnode's cache entries queue.
1985 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
1986 SDT_PROBE3(vfs, namecache, enter, done, dvp, ncp->nc_name,
1989 if (cnp->cn_flags & ISWHITEOUT)
1990 ncp->nc_flag |= NCF_WHITE;
1991 cache_negative_insert(ncp);
1992 SDT_PROBE2(vfs, namecache, enter_negative, done, dvp,
1997 * Insert the new namecache entry into the appropriate chain
1998 * within the cache entries table.
2000 CK_SLIST_INSERT_HEAD(ncpp, ncp, nc_hash);
2002 atomic_thread_fence_rel();
2004 * Mark the entry as fully constructed.
2005 * It is immutable past this point until its removal.
2007 atomic_store_char(&ncp->nc_flag, ncp->nc_flag & ~NCF_WIP);
2009 cache_enter_unlock(&cel);
2010 if (numneg * ncnegfactor > lnumcache)
2011 cache_negative_zap_one();
2016 cache_enter_unlock(&cel);
2017 atomic_subtract_long(&numcache, 1);
2023 cache_roundup_2(u_int val)
2027 for (res = 1; res <= val; res <<= 1)
2033 static struct nchashhead *
2034 nchinittbl(u_long elements, u_long *hashmask)
2036 struct nchashhead *hashtbl;
2039 hashsize = cache_roundup_2(elements) / 2;
2041 hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), M_VFSCACHE, M_WAITOK);
2042 for (i = 0; i < hashsize; i++)
2043 CK_SLIST_INIT(&hashtbl[i]);
2044 *hashmask = hashsize - 1;
2049 ncfreetbl(struct nchashhead *hashtbl)
2052 free(hashtbl, M_VFSCACHE);
2056 * Name cache initialization, from vfs_init() when we are booting
2059 nchinit(void *dummy __unused)
2063 cache_zone_small = uma_zcreate("S VFS Cache", CACHE_ZONE_SMALL_SIZE,
2064 NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT, UMA_ZONE_ZINIT);
2065 cache_zone_small_ts = uma_zcreate("STS VFS Cache", CACHE_ZONE_SMALL_TS_SIZE,
2066 NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT, UMA_ZONE_ZINIT);
2067 cache_zone_large = uma_zcreate("L VFS Cache", CACHE_ZONE_LARGE_SIZE,
2068 NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT, UMA_ZONE_ZINIT);
2069 cache_zone_large_ts = uma_zcreate("LTS VFS Cache", CACHE_ZONE_LARGE_TS_SIZE,
2070 NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT, UMA_ZONE_ZINIT);
2072 VFS_SMR_ZONE_SET(cache_zone_small);
2073 VFS_SMR_ZONE_SET(cache_zone_small_ts);
2074 VFS_SMR_ZONE_SET(cache_zone_large);
2075 VFS_SMR_ZONE_SET(cache_zone_large_ts);
2077 ncsize = desiredvnodes * ncsizefactor;
2078 nchashtbl = nchinittbl(desiredvnodes * 2, &nchash);
2079 ncbuckethash = cache_roundup_2(mp_ncpus * mp_ncpus) - 1;
2080 if (ncbuckethash < 7) /* arbitrarily chosen to avoid having one lock */
2082 if (ncbuckethash > nchash)
2083 ncbuckethash = nchash;
2084 bucketlocks = malloc(sizeof(*bucketlocks) * numbucketlocks, M_VFSCACHE,
2086 for (i = 0; i < numbucketlocks; i++)
2087 mtx_init(&bucketlocks[i], "ncbuc", NULL, MTX_DUPOK | MTX_RECURSE);
2088 ncvnodehash = ncbuckethash;
2089 vnodelocks = malloc(sizeof(*vnodelocks) * numvnodelocks, M_VFSCACHE,
2091 for (i = 0; i < numvnodelocks; i++)
2092 mtx_init(&vnodelocks[i], "ncvn", NULL, MTX_DUPOK | MTX_RECURSE);
2094 neglists = malloc(sizeof(*neglists) * numneglists, M_VFSCACHE,
2096 for (i = 0; i < numneglists; i++) {
2097 mtx_init(&neglists[i].nl_lock, "ncnegl", NULL, MTX_DEF);
2098 TAILQ_INIT(&neglists[i].nl_list);
2100 mtx_init(&ncneg_hot.nl_lock, "ncneglh", NULL, MTX_DEF);
2101 TAILQ_INIT(&ncneg_hot.nl_list);
2103 mtx_init(&ncneg_shrink_lock, "ncnegs", NULL, MTX_DEF);
2105 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL);
2108 cache_vnode_init(struct vnode *vp)
2111 LIST_INIT(&vp->v_cache_src);
2112 TAILQ_INIT(&vp->v_cache_dst);
2113 vp->v_cache_dd = NULL;
2118 cache_changesize(u_long newmaxvnodes)
2120 struct nchashhead *new_nchashtbl, *old_nchashtbl;
2121 u_long new_nchash, old_nchash;
2122 struct namecache *ncp;
2127 newncsize = newmaxvnodes * ncsizefactor;
2128 newmaxvnodes = cache_roundup_2(newmaxvnodes * 2);
2129 if (newmaxvnodes < numbucketlocks)
2130 newmaxvnodes = numbucketlocks;
2132 new_nchashtbl = nchinittbl(newmaxvnodes, &new_nchash);
2133 /* If same hash table size, nothing to do */
2134 if (nchash == new_nchash) {
2135 ncfreetbl(new_nchashtbl);
2139 * Move everything from the old hash table to the new table.
2140 * None of the namecache entries in the table can be removed
2141 * because to do so, they have to be removed from the hash table.
2143 cache_lock_all_vnodes();
2144 cache_lock_all_buckets();
2145 old_nchashtbl = nchashtbl;
2146 old_nchash = nchash;
2147 nchashtbl = new_nchashtbl;
2148 nchash = new_nchash;
2149 for (i = 0; i <= old_nchash; i++) {
2150 while ((ncp = CK_SLIST_FIRST(&old_nchashtbl[i])) != NULL) {
2151 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen,
2153 CK_SLIST_REMOVE(&old_nchashtbl[i], ncp, namecache, nc_hash);
2154 CK_SLIST_INSERT_HEAD(NCHHASH(hash), ncp, nc_hash);
2158 cache_unlock_all_buckets();
2159 cache_unlock_all_vnodes();
2160 ncfreetbl(old_nchashtbl);
2164 * Invalidate all entries from and to a particular vnode.
2167 cache_purge_impl(struct vnode *vp)
2169 TAILQ_HEAD(, namecache) ncps;
2170 struct namecache *ncp, *nnp;
2171 struct mtx *vlp, *vlp2;
2174 vlp = VP2VNODELOCK(vp);
2178 while (!LIST_EMPTY(&vp->v_cache_src)) {
2179 ncp = LIST_FIRST(&vp->v_cache_src);
2180 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2182 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2184 while (!TAILQ_EMPTY(&vp->v_cache_dst)) {
2185 ncp = TAILQ_FIRST(&vp->v_cache_dst);
2186 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2188 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2190 ncp = vp->v_cache_dd;
2192 KASSERT(ncp->nc_flag & NCF_ISDOTDOT,
2193 ("lost dotdot link"));
2194 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2196 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2198 KASSERT(vp->v_cache_dd == NULL, ("incomplete purge"));
2202 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2208 * Opportunistic check to see if there is anything to do.
2211 cache_has_entries(struct vnode *vp)
2214 if (LIST_EMPTY(&vp->v_cache_src) && TAILQ_EMPTY(&vp->v_cache_dst) &&
2215 vp->v_cache_dd == NULL)
2221 cache_purge(struct vnode *vp)
2224 SDT_PROBE1(vfs, namecache, purge, done, vp);
2225 if (!cache_has_entries(vp))
2227 cache_purge_impl(vp);
2231 * Only to be used by vgone.
2234 cache_purge_vgone(struct vnode *vp)
2238 VNPASS(VN_IS_DOOMED(vp), vp);
2239 if (cache_has_entries(vp)) {
2240 cache_purge_impl(vp);
2245 * Serialize against a potential thread doing cache_purge.
2247 vlp = VP2VNODELOCK(vp);
2248 mtx_wait_unlocked(vlp);
2249 if (cache_has_entries(vp)) {
2250 cache_purge_impl(vp);
2257 * Invalidate all negative entries for a particular directory vnode.
2260 cache_purge_negative(struct vnode *vp)
2262 TAILQ_HEAD(, namecache) ncps;
2263 struct namecache *ncp, *nnp;
2266 SDT_PROBE1(vfs, namecache, purge_negative, done, vp);
2267 if (LIST_EMPTY(&vp->v_cache_src))
2270 vlp = VP2VNODELOCK(vp);
2272 LIST_FOREACH_SAFE(ncp, &vp->v_cache_src, nc_src, nnp) {
2273 if (!(ncp->nc_flag & NCF_NEGATIVE))
2275 cache_zap_negative_locked_vnode_kl(ncp, vp);
2276 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2279 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2285 cache_rename(struct vnode *fdvp, struct vnode *fvp, struct vnode *tdvp,
2286 struct vnode *tvp, struct componentname *fcnp, struct componentname *tcnp)
2289 ASSERT_VOP_IN_SEQC(fdvp);
2290 ASSERT_VOP_IN_SEQC(fvp);
2291 ASSERT_VOP_IN_SEQC(tdvp);
2293 ASSERT_VOP_IN_SEQC(tvp);
2298 KASSERT(!cache_remove_cnp(tdvp, tcnp),
2299 ("%s: lingering negative entry", __func__));
2301 cache_remove_cnp(tdvp, tcnp);
2306 * Flush all entries referencing a particular filesystem.
2309 cache_purgevfs(struct mount *mp)
2311 struct vnode *vp, *mvp;
2313 SDT_PROBE1(vfs, namecache, purgevfs, done, mp);
2315 * Somewhat wasteful iteration over all vnodes. Would be better to
2316 * support filtering and avoid the interlock to begin with.
2318 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
2319 if (!cache_has_entries(vp)) {
2331 * Perform canonical checks and cache lookup and pass on to filesystem
2332 * through the vop_cachedlookup only if needed.
2336 vfs_cache_lookup(struct vop_lookup_args *ap)
2340 struct vnode **vpp = ap->a_vpp;
2341 struct componentname *cnp = ap->a_cnp;
2342 int flags = cnp->cn_flags;
2347 if (dvp->v_type != VDIR)
2350 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
2351 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
2354 error = vn_dir_check_exec(dvp, cnp);
2358 error = cache_lookup(dvp, vpp, cnp, NULL, NULL);
2360 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
2366 /* Implementation of the getcwd syscall. */
2368 sys___getcwd(struct thread *td, struct __getcwd_args *uap)
2374 buflen = uap->buflen;
2375 if (__predict_false(buflen < 2))
2377 if (buflen > MAXPATHLEN)
2378 buflen = MAXPATHLEN;
2380 buf = uma_zalloc(namei_zone, M_WAITOK);
2381 error = vn_getcwd(buf, &retbuf, &buflen);
2383 error = copyout(retbuf, uap->buf, buflen);
2384 uma_zfree(namei_zone, buf);
2389 vn_getcwd(char *buf, char **retbuf, size_t *buflen)
2395 pwd = pwd_get_smr();
2396 error = vn_fullpath_any_smr(pwd->pwd_cdir, pwd->pwd_rdir, buf, retbuf,
2398 VFS_SMR_ASSERT_NOT_ENTERED();
2400 pwd = pwd_hold(curthread);
2401 error = vn_fullpath_any(pwd->pwd_cdir, pwd->pwd_rdir, buf,
2407 if (KTRPOINT(curthread, KTR_NAMEI) && error == 0)
2414 kern___realpathat(struct thread *td, int fd, const char *path, char *buf,
2415 size_t size, int flags, enum uio_seg pathseg)
2417 struct nameidata nd;
2418 char *retbuf, *freebuf;
2423 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | SAVENAME | WANTPARENT | AUDITVNODE1,
2424 pathseg, path, fd, &cap_fstat_rights, td);
2425 if ((error = namei(&nd)) != 0)
2427 error = vn_fullpath_hardlink(&nd, &retbuf, &freebuf, &size);
2429 error = copyout(retbuf, buf, size);
2430 free(freebuf, M_TEMP);
2437 sys___realpathat(struct thread *td, struct __realpathat_args *uap)
2440 return (kern___realpathat(td, uap->fd, uap->path, uap->buf, uap->size,
2441 uap->flags, UIO_USERSPACE));
2445 * Retrieve the full filesystem path that correspond to a vnode from the name
2446 * cache (if available)
2449 vn_fullpath(struct vnode *vp, char **retbuf, char **freebuf)
2456 if (__predict_false(vp == NULL))
2459 buflen = MAXPATHLEN;
2460 buf = malloc(buflen, M_TEMP, M_WAITOK);
2462 pwd = pwd_get_smr();
2463 error = vn_fullpath_any_smr(vp, pwd->pwd_rdir, buf, retbuf, &buflen, false, 0);
2464 VFS_SMR_ASSERT_NOT_ENTERED();
2466 pwd = pwd_hold(curthread);
2467 error = vn_fullpath_any(vp, pwd->pwd_rdir, buf, retbuf, &buflen);
2478 * This function is similar to vn_fullpath, but it attempts to lookup the
2479 * pathname relative to the global root mount point. This is required for the
2480 * auditing sub-system, as audited pathnames must be absolute, relative to the
2481 * global root mount point.
2484 vn_fullpath_global(struct vnode *vp, char **retbuf, char **freebuf)
2490 if (__predict_false(vp == NULL))
2492 buflen = MAXPATHLEN;
2493 buf = malloc(buflen, M_TEMP, M_WAITOK);
2495 error = vn_fullpath_any_smr(vp, rootvnode, buf, retbuf, &buflen, false, 0);
2496 VFS_SMR_ASSERT_NOT_ENTERED();
2498 error = vn_fullpath_any(vp, rootvnode, buf, retbuf, &buflen);
2507 static struct namecache *
2508 vn_dd_from_dst(struct vnode *vp)
2510 struct namecache *ncp;
2512 cache_assert_vnode_locked(vp);
2513 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst) {
2514 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2521 vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, size_t *buflen)
2524 struct namecache *ncp;
2528 vlp = VP2VNODELOCK(*vp);
2530 ncp = (*vp)->v_cache_dd;
2531 if (ncp != NULL && (ncp->nc_flag & NCF_ISDOTDOT) == 0) {
2532 KASSERT(ncp == vn_dd_from_dst(*vp),
2533 ("%s: mismatch for dd entry (%p != %p)", __func__,
2534 ncp, vn_dd_from_dst(*vp)));
2536 ncp = vn_dd_from_dst(*vp);
2539 if (*buflen < ncp->nc_nlen) {
2542 counter_u64_add(numfullpathfail4, 1);
2544 SDT_PROBE3(vfs, namecache, fullpath, return, error,
2548 *buflen -= ncp->nc_nlen;
2549 memcpy(buf + *buflen, ncp->nc_name, ncp->nc_nlen);
2550 SDT_PROBE3(vfs, namecache, fullpath, hit, ncp->nc_dvp,
2559 SDT_PROBE1(vfs, namecache, fullpath, miss, vp);
2562 vn_lock(*vp, LK_SHARED | LK_RETRY);
2563 error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen);
2566 counter_u64_add(numfullpathfail2, 1);
2567 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2572 if (VN_IS_DOOMED(dvp)) {
2573 /* forced unmount */
2576 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2580 * *vp has its use count incremented still.
2587 * Resolve a directory to a pathname.
2589 * The name of the directory can always be found in the namecache or fetched
2590 * from the filesystem. There is also guaranteed to be only one parent, meaning
2591 * we can just follow vnodes up until we find the root.
2593 * The vnode must be referenced.
2596 vn_fullpath_dir(struct vnode *vp, struct vnode *rdir, char *buf, char **retbuf,
2597 size_t *len, bool slash_prefixed, size_t addend)
2599 #ifdef KDTRACE_HOOKS
2600 struct vnode *startvp = vp;
2606 VNPASS(vp->v_type == VDIR || VN_IS_DOOMED(vp), vp);
2607 VNPASS(vp->v_usecount > 0, vp);
2611 if (!slash_prefixed) {
2619 SDT_PROBE1(vfs, namecache, fullpath, entry, vp);
2620 counter_u64_add(numfullpathcalls, 1);
2621 while (vp != rdir && vp != rootvnode) {
2623 * The vp vnode must be already fully constructed,
2624 * since it is either found in namecache or obtained
2625 * from VOP_VPTOCNP(). We may test for VV_ROOT safely
2626 * without obtaining the vnode lock.
2628 if ((vp->v_vflag & VV_ROOT) != 0) {
2629 vn_lock(vp, LK_RETRY | LK_SHARED);
2632 * With the vnode locked, check for races with
2633 * unmount, forced or not. Note that we
2634 * already verified that vp is not equal to
2635 * the root vnode, which means that
2636 * mnt_vnodecovered can be NULL only for the
2639 if (VN_IS_DOOMED(vp) ||
2640 (vp1 = vp->v_mount->mnt_vnodecovered) == NULL ||
2641 vp1->v_mountedhere != vp->v_mount) {
2644 SDT_PROBE3(vfs, namecache, fullpath, return,
2654 if (vp->v_type != VDIR) {
2656 counter_u64_add(numfullpathfail1, 1);
2658 SDT_PROBE3(vfs, namecache, fullpath, return,
2662 error = vn_vptocnp(&vp, curthread->td_ucred, buf, &buflen);
2668 SDT_PROBE3(vfs, namecache, fullpath, return, error,
2672 buf[--buflen] = '/';
2673 slash_prefixed = true;
2677 if (!slash_prefixed) {
2680 counter_u64_add(numfullpathfail4, 1);
2681 SDT_PROBE3(vfs, namecache, fullpath, return, ENOMEM,
2685 buf[--buflen] = '/';
2687 counter_u64_add(numfullpathfound, 1);
2690 *retbuf = buf + buflen;
2691 SDT_PROBE3(vfs, namecache, fullpath, return, 0, startvp, *retbuf);
2698 * Resolve an arbitrary vnode to a pathname.
2701 * - hardlinks are not tracked, thus if the vnode is not a directory this can
2702 * resolve to a different path than the one used to find it
2703 * - namecache is not mandatory, meaning names are not guaranteed to be added
2704 * (in which case resolving fails)
2706 static void __inline
2707 cache_rev_failed_impl(int *reason, int line)
2712 #define cache_rev_failed(var) cache_rev_failed_impl((var), __LINE__)
2715 vn_fullpath_any_smr(struct vnode *vp, struct vnode *rdir, char *buf,
2716 char **retbuf, size_t *buflen, bool slash_prefixed, size_t addend)
2718 #ifdef KDTRACE_HOOKS
2719 struct vnode *startvp = vp;
2723 struct namecache *ncp;
2727 #ifdef KDTRACE_HOOKS
2730 seqc_t vp_seqc, tvp_seqc;
2733 VFS_SMR_ASSERT_ENTERED();
2735 if (!cache_fast_revlookup) {
2740 orig_buflen = *buflen;
2742 if (!slash_prefixed) {
2743 MPASS(*buflen >= 2);
2745 buf[*buflen] = '\0';
2748 if (vp == rdir || vp == rootvnode) {
2749 if (!slash_prefixed) {
2756 #ifdef KDTRACE_HOOKS
2760 ncp = NULL; /* for sdt probe down below */
2761 vp_seqc = vn_seqc_read_any(vp);
2762 if (seqc_in_modify(vp_seqc)) {
2763 cache_rev_failed(&reason);
2768 #ifdef KDTRACE_HOOKS
2771 if ((vp->v_vflag & VV_ROOT) != 0) {
2772 mp = atomic_load_ptr(&vp->v_mount);
2774 cache_rev_failed(&reason);
2777 tvp = atomic_load_ptr(&mp->mnt_vnodecovered);
2778 tvp_seqc = vn_seqc_read_any(tvp);
2779 if (seqc_in_modify(tvp_seqc)) {
2780 cache_rev_failed(&reason);
2783 if (!vn_seqc_consistent(vp, vp_seqc)) {
2784 cache_rev_failed(&reason);
2791 ncp = atomic_load_ptr(&vp->v_cache_dd);
2793 cache_rev_failed(&reason);
2796 nc_flag = atomic_load_char(&ncp->nc_flag);
2797 if ((nc_flag & NCF_ISDOTDOT) != 0) {
2798 cache_rev_failed(&reason);
2801 if (!cache_ncp_canuse(ncp)) {
2802 cache_rev_failed(&reason);
2805 if (ncp->nc_nlen >= *buflen) {
2806 cache_rev_failed(&reason);
2810 *buflen -= ncp->nc_nlen;
2811 memcpy(buf + *buflen, ncp->nc_name, ncp->nc_nlen);
2815 tvp_seqc = vn_seqc_read_any(tvp);
2816 if (seqc_in_modify(tvp_seqc)) {
2817 cache_rev_failed(&reason);
2820 if (!vn_seqc_consistent(vp, vp_seqc)) {
2821 cache_rev_failed(&reason);
2826 if (vp == rdir || vp == rootvnode)
2831 *retbuf = buf + *buflen;
2832 *buflen = orig_buflen - *buflen + addend;
2833 SDT_PROBE2(vfs, namecache, fullpath_smr, hit, startvp, *retbuf);
2837 *buflen = orig_buflen;
2838 SDT_PROBE4(vfs, namecache, fullpath_smr, miss, startvp, ncp, reason, i);
2844 vn_fullpath_any(struct vnode *vp, struct vnode *rdir, char *buf, char **retbuf,
2848 bool slash_prefixed;
2854 orig_buflen = *buflen;
2857 slash_prefixed = false;
2858 if (vp->v_type != VDIR) {
2860 buf[*buflen] = '\0';
2861 error = vn_vptocnp(&vp, curthread->td_ucred, buf, buflen);
2870 slash_prefixed = true;
2873 return (vn_fullpath_dir(vp, rdir, buf, retbuf, buflen, slash_prefixed,
2874 orig_buflen - *buflen));
2878 * Resolve an arbitrary vnode to a pathname (taking care of hardlinks).
2880 * Since the namecache does not track handlings, the caller is expected to first
2881 * look up the target vnode with SAVENAME | WANTPARENT flags passed to namei.
2883 * Then we have 2 cases:
2884 * - if the found vnode is a directory, the path can be constructed just by
2885 * fullowing names up the chain
2886 * - otherwise we populate the buffer with the saved name and start resolving
2890 vn_fullpath_hardlink(struct nameidata *ndp, char **retbuf, char **freebuf,
2895 struct componentname *cnp;
2899 bool slash_prefixed;
2904 if (*buflen > MAXPATHLEN)
2905 *buflen = MAXPATHLEN;
2907 slash_prefixed = false;
2909 buf = malloc(*buflen, M_TEMP, M_WAITOK);
2914 * Check for VBAD to work around the vp_crossmp bug in lookup().
2916 * For example consider tmpfs on /tmp and realpath /tmp. ni_vp will be
2917 * set to mount point's root vnode while ni_dvp will be vp_crossmp.
2918 * If the type is VDIR (like in this very case) we can skip looking
2919 * at ni_dvp in the first place. However, since vnodes get passed here
2920 * unlocked the target may transition to doomed state (type == VBAD)
2921 * before we get to evaluate the condition. If this happens, we will
2922 * populate part of the buffer and descend to vn_fullpath_dir with
2923 * vp == vp_crossmp. Prevent the problem by checking for VBAD.
2925 * This should be atomic_load(&vp->v_type) but it is ilegal to take
2926 * an address of a bit field, even if said field is sized to char.
2927 * Work around the problem by reading the value into a full-sized enum
2928 * and then re-reading it with atomic_load which will still prevent
2929 * the compiler from re-reading down the road.
2932 type = atomic_load_int(&type);
2939 addend = cnp->cn_namelen + 2;
2940 if (*buflen < addend) {
2945 tmpbuf = buf + *buflen;
2947 memcpy(&tmpbuf[1], cnp->cn_nameptr, cnp->cn_namelen);
2948 tmpbuf[addend - 1] = '\0';
2949 slash_prefixed = true;
2954 pwd = pwd_get_smr();
2955 error = vn_fullpath_any_smr(vp, pwd->pwd_rdir, buf, retbuf, buflen,
2956 slash_prefixed, addend);
2957 VFS_SMR_ASSERT_NOT_ENTERED();
2959 pwd = pwd_hold(curthread);
2961 error = vn_fullpath_dir(vp, pwd->pwd_rdir, buf, retbuf, buflen,
2962 slash_prefixed, addend);
2977 vn_dir_dd_ino(struct vnode *vp)
2979 struct namecache *ncp;
2984 ASSERT_VOP_LOCKED(vp, "vn_dir_dd_ino");
2985 vlp = VP2VNODELOCK(vp);
2987 TAILQ_FOREACH(ncp, &(vp->v_cache_dst), nc_dst) {
2988 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0)
2991 vs = vget_prep(ddvp);
2993 if (vget_finish(ddvp, LK_SHARED | LK_NOWAIT, vs))
3002 vn_commname(struct vnode *vp, char *buf, u_int buflen)
3004 struct namecache *ncp;
3008 vlp = VP2VNODELOCK(vp);
3010 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst)
3011 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
3017 l = min(ncp->nc_nlen, buflen - 1);
3018 memcpy(buf, ncp->nc_name, l);
3025 * This function updates path string to vnode's full global path
3026 * and checks the size of the new path string against the pathlen argument.
3028 * Requires a locked, referenced vnode.
3029 * Vnode is re-locked on success or ENODEV, otherwise unlocked.
3031 * If vp is a directory, the call to vn_fullpath_global() always succeeds
3032 * because it falls back to the ".." lookup if the namecache lookup fails.
3035 vn_path_to_global_path(struct thread *td, struct vnode *vp, char *path,
3038 struct nameidata nd;
3043 ASSERT_VOP_ELOCKED(vp, __func__);
3045 /* Construct global filesystem path from vp. */
3047 error = vn_fullpath_global(vp, &rpath, &fbuf);
3054 if (strlen(rpath) >= pathlen) {
3056 error = ENAMETOOLONG;
3061 * Re-lookup the vnode by path to detect a possible rename.
3062 * As a side effect, the vnode is relocked.
3063 * If vnode was renamed, return ENOENT.
3065 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
3066 UIO_SYSSPACE, path, td);
3072 NDFREE(&nd, NDF_ONLY_PNBUF);
3076 strcpy(path, rpath);
3089 db_print_vpath(struct vnode *vp)
3092 while (vp != NULL) {
3093 db_printf("%p: ", vp);
3094 if (vp == rootvnode) {
3098 if (vp->v_vflag & VV_ROOT) {
3099 db_printf("<mount point>");
3100 vp = vp->v_mount->mnt_vnodecovered;
3102 struct namecache *ncp;
3106 ncp = TAILQ_FIRST(&vp->v_cache_dst);
3109 for (i = 0; i < ncp->nc_nlen; i++)
3110 db_printf("%c", *ncn++);
3123 DB_SHOW_COMMAND(vpath, db_show_vpath)
3128 db_printf("usage: show vpath <struct vnode *>\n");
3132 vp = (struct vnode *)addr;
3138 static bool __read_frequently cache_fast_lookup = true;
3139 SYSCTL_BOOL(_vfs, OID_AUTO, cache_fast_lookup, CTLFLAG_RW,
3140 &cache_fast_lookup, 0, "");
3142 #define CACHE_FPL_FAILED -2020
3145 cache_fpl_cleanup_cnp(struct componentname *cnp)
3148 uma_zfree(namei_zone, cnp->cn_pnbuf);
3150 cnp->cn_pnbuf = NULL;
3151 cnp->cn_nameptr = NULL;
3156 cache_fpl_handle_root(struct nameidata *ndp, struct vnode **dpp)
3158 struct componentname *cnp;
3161 while (*(cnp->cn_nameptr) == '/') {
3166 *dpp = ndp->ni_rootdir;
3170 * Components of nameidata (or objects it can point to) which may
3171 * need restoring in case fast path lookup fails.
3173 struct nameidata_saved {
3181 struct nameidata *ndp;
3182 struct componentname *cnp;
3188 struct nameidata_saved snd;
3190 enum cache_fpl_status status:8;
3195 cache_fpl_checkpoint(struct cache_fpl *fpl, struct nameidata_saved *snd)
3198 snd->cn_flags = fpl->ndp->ni_cnd.cn_flags;
3199 snd->cn_namelen = fpl->ndp->ni_cnd.cn_namelen;
3200 snd->cn_nameptr = fpl->ndp->ni_cnd.cn_nameptr;
3201 snd->ni_pathlen = fpl->ndp->ni_pathlen;
3205 cache_fpl_restore(struct cache_fpl *fpl, struct nameidata_saved *snd)
3208 fpl->ndp->ni_cnd.cn_flags = snd->cn_flags;
3209 fpl->ndp->ni_cnd.cn_namelen = snd->cn_namelen;
3210 fpl->ndp->ni_cnd.cn_nameptr = snd->cn_nameptr;
3211 fpl->ndp->ni_pathlen = snd->ni_pathlen;
3215 #define cache_fpl_smr_assert_entered(fpl) ({ \
3216 struct cache_fpl *_fpl = (fpl); \
3217 MPASS(_fpl->in_smr == true); \
3218 VFS_SMR_ASSERT_ENTERED(); \
3220 #define cache_fpl_smr_assert_not_entered(fpl) ({ \
3221 struct cache_fpl *_fpl = (fpl); \
3222 MPASS(_fpl->in_smr == false); \
3223 VFS_SMR_ASSERT_NOT_ENTERED(); \
3226 #define cache_fpl_smr_assert_entered(fpl) do { } while (0)
3227 #define cache_fpl_smr_assert_not_entered(fpl) do { } while (0)
3230 #define cache_fpl_smr_enter_initial(fpl) ({ \
3231 struct cache_fpl *_fpl = (fpl); \
3233 _fpl->in_smr = true; \
3236 #define cache_fpl_smr_enter(fpl) ({ \
3237 struct cache_fpl *_fpl = (fpl); \
3238 MPASS(_fpl->in_smr == false); \
3240 _fpl->in_smr = true; \
3243 #define cache_fpl_smr_exit(fpl) ({ \
3244 struct cache_fpl *_fpl = (fpl); \
3245 MPASS(_fpl->in_smr == true); \
3247 _fpl->in_smr = false; \
3251 cache_fpl_aborted_impl(struct cache_fpl *fpl, int line)
3254 if (fpl->status != CACHE_FPL_STATUS_UNSET) {
3255 KASSERT(fpl->status == CACHE_FPL_STATUS_PARTIAL,
3256 ("%s: converting to abort from %d at %d, set at %d\n",
3257 __func__, fpl->status, line, fpl->line));
3259 fpl->status = CACHE_FPL_STATUS_ABORTED;
3261 return (CACHE_FPL_FAILED);
3264 #define cache_fpl_aborted(x) cache_fpl_aborted_impl((x), __LINE__)
3267 cache_fpl_partial_impl(struct cache_fpl *fpl, int line)
3270 KASSERT(fpl->status == CACHE_FPL_STATUS_UNSET,
3271 ("%s: setting to partial at %d, but already set to %d at %d\n",
3272 __func__, line, fpl->status, fpl->line));
3273 cache_fpl_smr_assert_entered(fpl);
3274 fpl->status = CACHE_FPL_STATUS_PARTIAL;
3276 return (CACHE_FPL_FAILED);
3279 #define cache_fpl_partial(x) cache_fpl_partial_impl((x), __LINE__)
3282 cache_fpl_handled_impl(struct cache_fpl *fpl, int error, int line)
3285 KASSERT(fpl->status == CACHE_FPL_STATUS_UNSET,
3286 ("%s: setting to handled at %d, but already set to %d at %d\n",
3287 __func__, line, fpl->status, fpl->line));
3288 cache_fpl_smr_assert_not_entered(fpl);
3289 MPASS(error != CACHE_FPL_FAILED);
3290 fpl->status = CACHE_FPL_STATUS_HANDLED;
3295 #define cache_fpl_handled(x, e) cache_fpl_handled_impl((x), (e), __LINE__)
3297 #define CACHE_FPL_SUPPORTED_CN_FLAGS \
3298 (LOCKLEAF | LOCKPARENT | WANTPARENT | NOCACHE | FOLLOW | LOCKSHARED | SAVENAME | \
3299 SAVESTART | WILLBEDIR | ISOPEN | NOMACCHECK | AUDITVNODE1 | AUDITVNODE2 | NOCAPCHECK)
3301 #define CACHE_FPL_INTERNAL_CN_FLAGS \
3302 (ISDOTDOT | MAKEENTRY | ISLASTCN)
3304 _Static_assert((CACHE_FPL_SUPPORTED_CN_FLAGS & CACHE_FPL_INTERNAL_CN_FLAGS) == 0,
3305 "supported and internal flags overlap");
3308 cache_fpl_islastcn(struct nameidata *ndp)
3311 return (*ndp->ni_next == 0);
3315 cache_fpl_isdotdot(struct componentname *cnp)
3318 if (cnp->cn_namelen == 2 &&
3319 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
3325 cache_can_fplookup(struct cache_fpl *fpl)
3327 struct nameidata *ndp;
3328 struct componentname *cnp;
3333 td = cnp->cn_thread;
3335 if (!cache_fast_lookup) {
3336 cache_fpl_aborted(fpl);
3340 if (mac_vnode_check_lookup_enabled()) {
3341 cache_fpl_aborted(fpl);
3345 if ((cnp->cn_flags & ~CACHE_FPL_SUPPORTED_CN_FLAGS) != 0) {
3346 cache_fpl_aborted(fpl);
3349 if (ndp->ni_dirfd != AT_FDCWD) {
3350 cache_fpl_aborted(fpl);
3353 if (IN_CAPABILITY_MODE(td)) {
3354 cache_fpl_aborted(fpl);
3357 if (AUDITING_TD(td)) {
3358 cache_fpl_aborted(fpl);
3361 if (ndp->ni_startdir != NULL) {
3362 cache_fpl_aborted(fpl);
3369 cache_fplookup_vnode_supported(struct vnode *vp)
3372 return (vp->v_type != VLNK);
3376 * Move a negative entry to the hot list.
3378 * We have to take locks, but they may be contended and in the worst
3379 * case we may need to go off CPU. We don't want to spin within the
3380 * smr section and we can't block with it. Instead we are going to
3381 * look up the entry again.
3383 static int __noinline
3384 cache_fplookup_negative_promote(struct cache_fpl *fpl, struct namecache *oncp,
3387 struct componentname *cnp;
3388 struct namecache *ncp;
3389 struct neglist *neglist;
3390 struct negstate *negstate;
3397 if (!vhold_smr(dvp))
3398 return (cache_fpl_aborted(fpl));
3400 neglist = NCP2NEGLIST(oncp);
3401 cache_fpl_smr_exit(fpl);
3403 mtx_lock(&ncneg_hot.nl_lock);
3404 mtx_lock(&neglist->nl_lock);
3406 * For hash iteration.
3408 cache_fpl_smr_enter(fpl);
3411 * Avoid all surprises by only succeeding if we got the same entry and
3412 * bailing completely otherwise.
3414 * In particular at this point there can be a new ncp which matches the
3415 * search but hashes to a different neglist.
3417 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
3423 * No match to begin with.
3425 if (__predict_false(ncp == NULL)) {
3430 * The newly found entry may be something different...
3432 if (!(ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
3433 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))) {
3438 * ... and not even negative.
3440 nc_flag = atomic_load_char(&ncp->nc_flag);
3441 if ((nc_flag & NCF_NEGATIVE) == 0) {
3445 if (__predict_false(!cache_ncp_canuse(ncp))) {
3449 negstate = NCP2NEGSTATE(ncp);
3450 if ((negstate->neg_flag & NEG_HOT) == 0) {
3452 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
3453 TAILQ_INSERT_TAIL(&ncneg_hot.nl_list, ncp, nc_dst);
3454 negstate->neg_flag |= NEG_HOT;
3457 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp, ncp->nc_name);
3458 counter_u64_add(numneghits, 1);
3459 cache_fpl_smr_exit(fpl);
3460 mtx_unlock(&neglist->nl_lock);
3461 mtx_unlock(&ncneg_hot.nl_lock);
3463 return (cache_fpl_handled(fpl, ENOENT));
3465 cache_fpl_smr_exit(fpl);
3466 mtx_unlock(&neglist->nl_lock);
3467 mtx_unlock(&ncneg_hot.nl_lock);
3469 return (cache_fpl_aborted(fpl));
3473 * The target vnode is not supported, prepare for the slow path to take over.
3475 static int __noinline
3476 cache_fplookup_partial_setup(struct cache_fpl *fpl)
3478 struct nameidata *ndp;
3479 struct componentname *cnp;
3489 dvp_seqc = fpl->dvp_seqc;
3491 if (!pwd_hold_smr(pwd)) {
3492 cache_fpl_smr_exit(fpl);
3493 return (cache_fpl_aborted(fpl));
3496 dvs = vget_prep_smr(dvp);
3497 cache_fpl_smr_exit(fpl);
3498 if (__predict_false(dvs == VGET_NONE)) {
3500 return (cache_fpl_aborted(fpl));
3503 vget_finish_ref(dvp, dvs);
3504 if (!vn_seqc_consistent(dvp, dvp_seqc)) {
3507 return (cache_fpl_aborted(fpl));
3510 cache_fpl_restore(fpl, &fpl->snd);
3512 ndp->ni_startdir = dvp;
3513 cnp->cn_flags |= MAKEENTRY;
3514 if (cache_fpl_islastcn(ndp))
3515 cnp->cn_flags |= ISLASTCN;
3516 if (cache_fpl_isdotdot(cnp))
3517 cnp->cn_flags |= ISDOTDOT;
3523 cache_fplookup_final_child(struct cache_fpl *fpl, enum vgetstate tvs)
3525 struct componentname *cnp;
3532 tvp_seqc = fpl->tvp_seqc;
3534 if ((cnp->cn_flags & LOCKLEAF) != 0) {
3535 lkflags = LK_SHARED;
3536 if ((cnp->cn_flags & LOCKSHARED) == 0)
3537 lkflags = LK_EXCLUSIVE;
3538 error = vget_finish(tvp, lkflags, tvs);
3539 if (__predict_false(error != 0)) {
3540 return (cache_fpl_aborted(fpl));
3543 vget_finish_ref(tvp, tvs);
3546 if (!vn_seqc_consistent(tvp, tvp_seqc)) {
3547 if ((cnp->cn_flags & LOCKLEAF) != 0)
3551 return (cache_fpl_aborted(fpl));
3554 return (cache_fpl_handled(fpl, 0));
3558 * They want to possibly modify the state of the namecache.
3560 * Don't try to match the API contract, just leave.
3561 * TODO: this leaves scalability on the table
3564 cache_fplookup_final_modifying(struct cache_fpl *fpl)
3566 struct componentname *cnp;
3569 MPASS(cnp->cn_nameiop != LOOKUP);
3570 return (cache_fpl_partial(fpl));
3573 static int __noinline
3574 cache_fplookup_final_withparent(struct cache_fpl *fpl)
3576 struct componentname *cnp;
3577 enum vgetstate dvs, tvs;
3578 struct vnode *dvp, *tvp;
3584 dvp_seqc = fpl->dvp_seqc;
3587 MPASS((cnp->cn_flags & (LOCKPARENT|WANTPARENT)) != 0);
3590 * This is less efficient than it can be for simplicity.
3592 dvs = vget_prep_smr(dvp);
3593 if (__predict_false(dvs == VGET_NONE)) {
3594 return (cache_fpl_aborted(fpl));
3596 tvs = vget_prep_smr(tvp);
3597 if (__predict_false(tvs == VGET_NONE)) {
3598 cache_fpl_smr_exit(fpl);
3599 vget_abort(dvp, dvs);
3600 return (cache_fpl_aborted(fpl));
3603 cache_fpl_smr_exit(fpl);
3605 if ((cnp->cn_flags & LOCKPARENT) != 0) {
3606 error = vget_finish(dvp, LK_EXCLUSIVE, dvs);
3607 if (__predict_false(error != 0)) {
3608 vget_abort(tvp, tvs);
3609 return (cache_fpl_aborted(fpl));
3612 vget_finish_ref(dvp, dvs);
3615 if (!vn_seqc_consistent(dvp, dvp_seqc)) {
3616 vget_abort(tvp, tvs);
3617 if ((cnp->cn_flags & LOCKPARENT) != 0)
3621 return (cache_fpl_aborted(fpl));
3624 error = cache_fplookup_final_child(fpl, tvs);
3625 if (__predict_false(error != 0)) {
3626 MPASS(fpl->status == CACHE_FPL_STATUS_ABORTED);
3627 if ((cnp->cn_flags & LOCKPARENT) != 0)
3634 MPASS(fpl->status == CACHE_FPL_STATUS_HANDLED);
3639 cache_fplookup_final(struct cache_fpl *fpl)
3641 struct componentname *cnp;
3643 struct vnode *dvp, *tvp;
3648 dvp_seqc = fpl->dvp_seqc;
3651 VNPASS(cache_fplookup_vnode_supported(dvp), dvp);
3653 if (cnp->cn_nameiop != LOOKUP) {
3654 return (cache_fplookup_final_modifying(fpl));
3657 if ((cnp->cn_flags & (LOCKPARENT|WANTPARENT)) != 0)
3658 return (cache_fplookup_final_withparent(fpl));
3660 tvs = vget_prep_smr(tvp);
3661 if (__predict_false(tvs == VGET_NONE)) {
3662 return (cache_fpl_partial(fpl));
3665 if (!vn_seqc_consistent(dvp, dvp_seqc)) {
3666 cache_fpl_smr_exit(fpl);
3667 vget_abort(tvp, tvs);
3668 return (cache_fpl_aborted(fpl));
3671 cache_fpl_smr_exit(fpl);
3672 return (cache_fplookup_final_child(fpl, tvs));
3675 static int __noinline
3676 cache_fplookup_dot(struct cache_fpl *fpl)
3683 fpl->tvp_seqc = vn_seqc_read_any(dvp);
3684 if (seqc_in_modify(fpl->tvp_seqc)) {
3685 return (cache_fpl_aborted(fpl));
3688 counter_u64_add(dothits, 1);
3689 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", dvp);
3694 static int __noinline
3695 cache_fplookup_dotdot(struct cache_fpl *fpl)
3697 struct nameidata *ndp;
3698 struct componentname *cnp;
3699 struct namecache *ncp;
3709 * XXX this is racy the same way regular lookup is
3711 for (pr = cnp->cn_cred->cr_prison; pr != NULL;
3713 if (dvp == pr->pr_root)
3716 if (dvp == ndp->ni_rootdir ||
3717 dvp == ndp->ni_topdir ||
3721 fpl->tvp_seqc = vn_seqc_read_any(dvp);
3722 if (seqc_in_modify(fpl->tvp_seqc)) {
3723 return (cache_fpl_aborted(fpl));
3728 if ((dvp->v_vflag & VV_ROOT) != 0) {
3731 * The opposite of climb mount is needed here.
3733 return (cache_fpl_aborted(fpl));
3736 ncp = atomic_load_ptr(&dvp->v_cache_dd);
3738 return (cache_fpl_aborted(fpl));
3741 nc_flag = atomic_load_char(&ncp->nc_flag);
3742 if ((nc_flag & NCF_ISDOTDOT) != 0) {
3743 if ((nc_flag & NCF_NEGATIVE) != 0)
3744 return (cache_fpl_aborted(fpl));
3745 fpl->tvp = ncp->nc_vp;
3747 fpl->tvp = ncp->nc_dvp;
3750 if (__predict_false(!cache_ncp_canuse(ncp))) {
3751 return (cache_fpl_aborted(fpl));
3754 fpl->tvp_seqc = vn_seqc_read_any(fpl->tvp);
3755 if (seqc_in_modify(fpl->tvp_seqc)) {
3756 return (cache_fpl_partial(fpl));
3759 counter_u64_add(dotdothits, 1);
3764 cache_fplookup_next(struct cache_fpl *fpl)
3766 struct componentname *cnp;
3767 struct namecache *ncp;
3768 struct negstate *negstate;
3769 struct vnode *dvp, *tvp;
3777 if (__predict_false(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.')) {
3778 return (cache_fplookup_dot(fpl));
3781 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
3783 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
3784 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
3785 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
3790 * If there is no entry we have to punt to the slow path to perform
3791 * actual lookup. Should there be nothing with this name a negative
3792 * entry will be created.
3794 if (__predict_false(ncp == NULL)) {
3795 return (cache_fpl_partial(fpl));
3798 tvp = atomic_load_ptr(&ncp->nc_vp);
3799 nc_flag = atomic_load_char(&ncp->nc_flag);
3800 if ((nc_flag & NCF_NEGATIVE) != 0) {
3802 * If they want to create an entry we need to replace this one.
3804 if (__predict_false(fpl->cnp->cn_nameiop != LOOKUP)) {
3805 return (cache_fpl_partial(fpl));
3807 negstate = NCP2NEGSTATE(ncp);
3808 neg_hot = ((negstate->neg_flag & NEG_HOT) != 0);
3809 if (__predict_false(!cache_ncp_canuse(ncp))) {
3810 return (cache_fpl_partial(fpl));
3812 if (__predict_false((nc_flag & NCF_WHITE) != 0)) {
3813 return (cache_fpl_partial(fpl));
3816 return (cache_fplookup_negative_promote(fpl, ncp, hash));
3818 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp,
3820 counter_u64_add(numneghits, 1);
3821 cache_fpl_smr_exit(fpl);
3822 return (cache_fpl_handled(fpl, ENOENT));
3825 if (__predict_false(!cache_ncp_canuse(ncp))) {
3826 return (cache_fpl_partial(fpl));
3830 fpl->tvp_seqc = vn_seqc_read_any(tvp);
3831 if (seqc_in_modify(fpl->tvp_seqc)) {
3832 return (cache_fpl_partial(fpl));
3835 if (!cache_fplookup_vnode_supported(tvp)) {
3836 return (cache_fpl_partial(fpl));
3839 counter_u64_add(numposhits, 1);
3840 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name, tvp);
3845 cache_fplookup_mp_supported(struct mount *mp)
3850 if ((mp->mnt_kern_flag & MNTK_FPLOOKUP) == 0)
3856 * Walk up the mount stack (if any).
3858 * Correctness is provided in the following ways:
3859 * - all vnodes are protected from freeing with SMR
3860 * - struct mount objects are type stable making them always safe to access
3861 * - stability of the particular mount is provided by busying it
3862 * - relationship between the vnode which is mounted on and the mount is
3863 * verified with the vnode sequence counter after busying
3864 * - association between root vnode of the mount and the mount is protected
3867 * From that point on we can read the sequence counter of the root vnode
3868 * and get the next mount on the stack (if any) using the same protection.
3870 * By the end of successful walk we are guaranteed the reached state was
3871 * indeed present at least at some point which matches the regular lookup.
3873 static int __noinline
3874 cache_fplookup_climb_mount(struct cache_fpl *fpl)
3876 struct mount *mp, *prev_mp;
3881 vp_seqc = fpl->tvp_seqc;
3883 VNPASS(vp->v_type == VDIR || vp->v_type == VBAD, vp);
3884 mp = atomic_load_ptr(&vp->v_mountedhere);
3890 if (!vfs_op_thread_enter_crit(mp)) {
3891 if (prev_mp != NULL)
3892 vfs_op_thread_exit_crit(prev_mp);
3893 return (cache_fpl_partial(fpl));
3895 if (prev_mp != NULL)
3896 vfs_op_thread_exit_crit(prev_mp);
3897 if (!vn_seqc_consistent(vp, vp_seqc)) {
3898 vfs_op_thread_exit_crit(mp);
3899 return (cache_fpl_partial(fpl));
3901 if (!cache_fplookup_mp_supported(mp)) {
3902 vfs_op_thread_exit_crit(mp);
3903 return (cache_fpl_partial(fpl));
3905 vp = atomic_load_ptr(&mp->mnt_rootvnode);
3906 if (vp == NULL || VN_IS_DOOMED(vp)) {
3907 vfs_op_thread_exit_crit(mp);
3908 return (cache_fpl_partial(fpl));
3910 vp_seqc = vn_seqc_read_any(vp);
3911 if (seqc_in_modify(vp_seqc)) {
3912 vfs_op_thread_exit_crit(mp);
3913 return (cache_fpl_partial(fpl));
3916 mp = atomic_load_ptr(&vp->v_mountedhere);
3921 vfs_op_thread_exit_crit(prev_mp);
3923 fpl->tvp_seqc = vp_seqc;
3928 cache_fplookup_need_climb_mount(struct cache_fpl *fpl)
3936 * Hack: while this is a union, the pointer tends to be NULL so save on
3939 mp = atomic_load_ptr(&vp->v_mountedhere);
3942 if (vp->v_type == VDIR)
3950 * The code was originally copy-pasted from regular lookup and despite
3951 * clean ups leaves performance on the table. Any modifications here
3952 * must take into account that in case off fallback the resulting
3953 * nameidata state has to be compatible with the original.
3956 cache_fplookup_parse(struct cache_fpl *fpl)
3958 struct nameidata *ndp;
3959 struct componentname *cnp;
3966 * Search a new directory.
3968 * The last component of the filename is left accessible via
3969 * cnp->cn_nameptr for callers that need the name. Callers needing
3970 * the name set the SAVENAME flag. When done, they assume
3971 * responsibility for freeing the pathname buffer.
3973 for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
3975 cnp->cn_namelen = cp - cnp->cn_nameptr;
3976 if (__predict_false(cnp->cn_namelen > NAME_MAX)) {
3977 cache_fpl_smr_exit(fpl);
3978 return (cache_fpl_handled(fpl, ENAMETOOLONG));
3980 ndp->ni_pathlen -= cnp->cn_namelen;
3981 KASSERT(ndp->ni_pathlen <= PATH_MAX,
3982 ("%s: ni_pathlen underflow to %zd\n", __func__, ndp->ni_pathlen));
3986 * Replace multiple slashes by a single slash and trailing slashes
3987 * by a null. This must be done before VOP_LOOKUP() because some
3988 * fs's don't know about trailing slashes. Remember if there were
3989 * trailing slashes to handle symlinks, existing non-directories
3990 * and non-existing files that won't be directories specially later.
3992 while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
3998 * Regular lookup performs the following:
3999 * *ndp->ni_next = '\0';
4000 * cnp->cn_flags |= TRAILINGSLASH;
4002 * Which is problematic since it modifies data read
4003 * from userspace. Then if fast path lookup was to
4004 * abort we would have to either restore it or convey
4005 * the flag. Since this is a corner case just ignore
4006 * it for simplicity.
4008 return (cache_fpl_partial(fpl));
4014 * Check for degenerate name (e.g. / or "")
4015 * which is a way of talking about a directory,
4016 * e.g. like "/." or ".".
4019 * Another corner case handled by the regular lookup
4021 if (__predict_false(cnp->cn_nameptr[0] == '\0')) {
4022 return (cache_fpl_partial(fpl));
4028 cache_fplookup_parse_advance(struct cache_fpl *fpl)
4030 struct nameidata *ndp;
4031 struct componentname *cnp;
4036 cnp->cn_nameptr = ndp->ni_next;
4037 while (*cnp->cn_nameptr == '/') {
4043 static int __noinline
4044 cache_fplookup_failed_vexec(struct cache_fpl *fpl, int error)
4048 * Hack: they may be looking up foo/bar, where foo is a
4049 * regular file. In such a case we need to turn ENOTDIR,
4050 * but we may happen to get here with a different error.
4052 if (fpl->dvp->v_type != VDIR) {
4059 * Can happen when racing against vgone.
4062 cache_fpl_partial(fpl);
4066 * See the API contract for VOP_FPLOOKUP_VEXEC.
4068 if (!vn_seqc_consistent(fpl->dvp, fpl->dvp_seqc)) {
4069 error = cache_fpl_aborted(fpl);
4071 cache_fpl_smr_exit(fpl);
4072 cache_fpl_handled(fpl, error);
4080 cache_fplookup_impl(struct vnode *dvp, struct cache_fpl *fpl)
4082 struct nameidata *ndp;
4083 struct componentname *cnp;
4087 error = CACHE_FPL_FAILED;
4091 cache_fpl_checkpoint(fpl, &fpl->snd);
4094 fpl->dvp_seqc = vn_seqc_read_any(fpl->dvp);
4095 if (seqc_in_modify(fpl->dvp_seqc)) {
4096 cache_fpl_aborted(fpl);
4099 mp = atomic_load_ptr(&fpl->dvp->v_mount);
4100 if (!cache_fplookup_mp_supported(mp)) {
4101 cache_fpl_aborted(fpl);
4105 VNPASS(cache_fplookup_vnode_supported(fpl->dvp), fpl->dvp);
4108 error = cache_fplookup_parse(fpl);
4109 if (__predict_false(error != 0)) {
4113 VNPASS(cache_fplookup_vnode_supported(fpl->dvp), fpl->dvp);
4115 error = VOP_FPLOOKUP_VEXEC(fpl->dvp, cnp->cn_cred);
4116 if (__predict_false(error != 0)) {
4117 error = cache_fplookup_failed_vexec(fpl, error);
4121 if (__predict_false(cache_fpl_isdotdot(cnp))) {
4122 error = cache_fplookup_dotdot(fpl);
4123 if (__predict_false(error != 0)) {
4127 error = cache_fplookup_next(fpl);
4128 if (__predict_false(error != 0)) {
4132 VNPASS(!seqc_in_modify(fpl->tvp_seqc), fpl->tvp);
4134 if (cache_fplookup_need_climb_mount(fpl)) {
4135 error = cache_fplookup_climb_mount(fpl);
4136 if (__predict_false(error != 0)) {
4142 VNPASS(!seqc_in_modify(fpl->tvp_seqc), fpl->tvp);
4144 if (cache_fpl_islastcn(ndp)) {
4145 error = cache_fplookup_final(fpl);
4149 if (!vn_seqc_consistent(fpl->dvp, fpl->dvp_seqc)) {
4150 error = cache_fpl_aborted(fpl);
4154 fpl->dvp = fpl->tvp;
4155 fpl->dvp_seqc = fpl->tvp_seqc;
4157 cache_fplookup_parse_advance(fpl);
4158 cache_fpl_checkpoint(fpl, &fpl->snd);
4161 switch (fpl->status) {
4162 case CACHE_FPL_STATUS_UNSET:
4163 __assert_unreachable();
4165 case CACHE_FPL_STATUS_PARTIAL:
4166 cache_fpl_smr_assert_entered(fpl);
4167 return (cache_fplookup_partial_setup(fpl));
4168 case CACHE_FPL_STATUS_ABORTED:
4170 cache_fpl_smr_exit(fpl);
4171 return (CACHE_FPL_FAILED);
4172 case CACHE_FPL_STATUS_HANDLED:
4173 MPASS(error != CACHE_FPL_FAILED);
4174 cache_fpl_smr_assert_not_entered(fpl);
4175 if (__predict_false(error != 0)) {
4178 cache_fpl_cleanup_cnp(cnp);
4181 ndp->ni_dvp = fpl->dvp;
4182 ndp->ni_vp = fpl->tvp;
4183 if (cnp->cn_flags & SAVENAME)
4184 cnp->cn_flags |= HASBUF;
4186 cache_fpl_cleanup_cnp(cnp);
4192 * Fast path lookup protected with SMR and sequence counters.
4194 * Note: all VOP_FPLOOKUP_VEXEC routines have a comment referencing this one.
4196 * Filesystems can opt in by setting the MNTK_FPLOOKUP flag and meeting criteria
4199 * Traditional vnode lookup conceptually looks like this:
4205 * vn_unlock(current);
4212 * Each jump to the next vnode is safe memory-wise and atomic with respect to
4213 * any modifications thanks to holding respective locks.
4215 * The same guarantee can be provided with a combination of safe memory
4216 * reclamation and sequence counters instead. If all operations which affect
4217 * the relationship between the current vnode and the one we are looking for
4218 * also modify the counter, we can verify whether all the conditions held as
4219 * we made the jump. This includes things like permissions, mount points etc.
4220 * Counter modification is provided by enclosing relevant places in
4221 * vn_seqc_write_begin()/end() calls.
4223 * Thus this translates to:
4226 * dvp_seqc = seqc_read_any(dvp);
4227 * if (seqc_in_modify(dvp_seqc)) // someone is altering the vnode
4231 * tvp_seqc = seqc_read_any(tvp);
4232 * if (seqc_in_modify(tvp_seqc)) // someone is altering the target vnode
4234 * if (!seqc_consistent(dvp, dvp_seqc) // someone is altering the vnode
4236 * dvp = tvp; // we know nothing of importance has changed
4237 * dvp_seqc = tvp_seqc; // store the counter for the tvp iteration
4241 * vget(); // secure the vnode
4242 * if (!seqc_consistent(tvp, tvp_seqc) // final check
4244 * // at this point we know nothing has changed for any parent<->child pair
4245 * // as they were crossed during the lookup, meaning we matched the guarantee
4246 * // of the locked variant
4249 * The API contract for VOP_FPLOOKUP_VEXEC routines is as follows:
4250 * - they are called while within vfs_smr protection which they must never exit
4251 * - EAGAIN can be returned to denote checking could not be performed, it is
4252 * always valid to return it
4253 * - if the sequence counter has not changed the result must be valid
4254 * - if the sequence counter has changed both false positives and false negatives
4255 * are permitted (since the result will be rejected later)
4256 * - for simple cases of unix permission checks vaccess_vexec_smr can be used
4258 * Caveats to watch out for:
4259 * - vnodes are passed unlocked and unreferenced with nothing stopping
4260 * VOP_RECLAIM, in turn meaning that ->v_data can become NULL. It is advised
4261 * to use atomic_load_ptr to fetch it.
4262 * - the aforementioned object can also get freed, meaning absent other means it
4263 * should be protected with vfs_smr
4264 * - either safely checking permissions as they are modified or guaranteeing
4265 * their stability is left to the routine
4268 cache_fplookup(struct nameidata *ndp, enum cache_fpl_status *status,
4271 struct cache_fpl fpl;
4274 struct componentname *cnp;
4275 struct nameidata_saved orig;
4278 MPASS(ndp->ni_lcf == 0);
4280 fpl.status = CACHE_FPL_STATUS_UNSET;
4282 fpl.cnp = &ndp->ni_cnd;
4283 MPASS(curthread == fpl.cnp->cn_thread);
4285 if ((fpl.cnp->cn_flags & SAVESTART) != 0)
4286 MPASS(fpl.cnp->cn_nameiop != LOOKUP);
4288 if (!cache_can_fplookup(&fpl)) {
4289 SDT_PROBE3(vfs, fplookup, lookup, done, ndp, fpl.line, fpl.status);
4290 *status = fpl.status;
4291 return (EOPNOTSUPP);
4294 cache_fpl_checkpoint(&fpl, &orig);
4296 cache_fpl_smr_enter_initial(&fpl);
4297 pwd = pwd_get_smr();
4299 ndp->ni_rootdir = pwd->pwd_rdir;
4300 ndp->ni_topdir = pwd->pwd_jdir;
4303 cnp->cn_nameptr = cnp->cn_pnbuf;
4304 if (cnp->cn_pnbuf[0] == '/') {
4305 cache_fpl_handle_root(ndp, &dvp);
4307 MPASS(ndp->ni_dirfd == AT_FDCWD);
4308 dvp = pwd->pwd_cdir;
4311 SDT_PROBE4(vfs, namei, lookup, entry, dvp, cnp->cn_pnbuf, cnp->cn_flags, true);
4313 error = cache_fplookup_impl(dvp, &fpl);
4314 cache_fpl_smr_assert_not_entered(&fpl);
4315 SDT_PROBE3(vfs, fplookup, lookup, done, ndp, fpl.line, fpl.status);
4317 *status = fpl.status;
4318 switch (fpl.status) {
4319 case CACHE_FPL_STATUS_UNSET:
4320 __assert_unreachable();
4322 case CACHE_FPL_STATUS_HANDLED:
4323 SDT_PROBE3(vfs, namei, lookup, return, error,
4324 (error == 0 ? ndp->ni_vp : NULL), true);
4326 case CACHE_FPL_STATUS_PARTIAL:
4329 * Status restored by cache_fplookup_partial_setup.
4332 case CACHE_FPL_STATUS_ABORTED:
4333 cache_fpl_restore(&fpl, &orig);