2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993, 1995
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Poul-Henning Kamp of the FreeBSD Project.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)vfs_cache.c 8.5 (Berkeley) 3/22/95
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
41 #include "opt_ktrace.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/capsicum.h>
46 #include <sys/counter.h>
47 #include <sys/filedesc.h>
48 #include <sys/fnv_hash.h>
49 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/fcntl.h>
55 #include <sys/mount.h>
56 #include <sys/namei.h>
62 #include <sys/syscallsubr.h>
63 #include <sys/sysctl.h>
64 #include <sys/sysproto.h>
65 #include <sys/vnode.h>
68 #include <sys/ktrace.h>
71 #include <sys/capsicum.h>
73 #include <security/audit/audit.h>
74 #include <security/mac/mac_framework.h>
82 SDT_PROVIDER_DECLARE(vfs);
83 SDT_PROBE_DEFINE3(vfs, namecache, enter, done, "struct vnode *", "char *",
85 SDT_PROBE_DEFINE2(vfs, namecache, enter_negative, done, "struct vnode *",
87 SDT_PROBE_DEFINE2(vfs, namecache, fullpath_smr, hit, "struct vnode *",
89 SDT_PROBE_DEFINE4(vfs, namecache, fullpath_smr, miss, "struct vnode *",
90 "struct namecache *", "int", "int");
91 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, entry, "struct vnode *");
92 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, hit, "struct vnode *",
93 "char *", "struct vnode *");
94 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, miss, "struct vnode *");
95 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, return, "int",
96 "struct vnode *", "char *");
97 SDT_PROBE_DEFINE3(vfs, namecache, lookup, hit, "struct vnode *", "char *",
99 SDT_PROBE_DEFINE2(vfs, namecache, lookup, hit__negative,
100 "struct vnode *", "char *");
101 SDT_PROBE_DEFINE2(vfs, namecache, lookup, miss, "struct vnode *",
103 SDT_PROBE_DEFINE2(vfs, namecache, removecnp, hit, "struct vnode *",
104 "struct componentname *");
105 SDT_PROBE_DEFINE2(vfs, namecache, removecnp, miss, "struct vnode *",
106 "struct componentname *");
107 SDT_PROBE_DEFINE1(vfs, namecache, purge, done, "struct vnode *");
108 SDT_PROBE_DEFINE1(vfs, namecache, purge_negative, done, "struct vnode *");
109 SDT_PROBE_DEFINE1(vfs, namecache, purgevfs, done, "struct mount *");
110 SDT_PROBE_DEFINE3(vfs, namecache, zap, done, "struct vnode *", "char *",
112 SDT_PROBE_DEFINE2(vfs, namecache, zap_negative, done, "struct vnode *",
114 SDT_PROBE_DEFINE2(vfs, namecache, shrink_negative, done, "struct vnode *",
117 SDT_PROBE_DEFINE3(vfs, fplookup, lookup, done, "struct nameidata", "int", "bool");
118 SDT_PROBE_DECLARE(vfs, namei, lookup, entry);
119 SDT_PROBE_DECLARE(vfs, namei, lookup, return);
122 * This structure describes the elements in the cache of recent
123 * names looked up by namei.
128 _Static_assert(sizeof(struct negstate) <= sizeof(struct vnode *),
129 "the state must fit in a union with a pointer without growing it");
132 LIST_ENTRY(namecache) nc_src; /* source vnode list */
133 TAILQ_ENTRY(namecache) nc_dst; /* destination vnode list */
134 CK_SLIST_ENTRY(namecache) nc_hash;/* hash chain */
135 struct vnode *nc_dvp; /* vnode of parent of name */
137 struct vnode *nu_vp; /* vnode the name refers to */
138 struct negstate nu_neg;/* negative entry state */
140 u_char nc_flag; /* flag bits */
141 u_char nc_nlen; /* length of name */
142 char nc_name[0]; /* segment name + nul */
146 * struct namecache_ts repeats struct namecache layout up to the
148 * struct namecache_ts is used in place of struct namecache when time(s) need
149 * to be stored. The nc_dotdottime field is used when a cache entry is mapping
150 * both a non-dotdot directory name plus dotdot for the directory's
153 * See below for alignment requirement.
155 struct namecache_ts {
156 struct timespec nc_time; /* timespec provided by fs */
157 struct timespec nc_dotdottime; /* dotdot timespec provided by fs */
158 int nc_ticks; /* ticks value when entry was added */
159 struct namecache nc_nc;
163 * At least mips n32 performs 64-bit accesses to timespec as found
164 * in namecache_ts and requires them to be aligned. Since others
165 * may be in the same spot suffer a little bit and enforce the
166 * alignment for everyone. Note this is a nop for 64-bit platforms.
168 #define CACHE_ZONE_ALIGNMENT UMA_ALIGNOF(time_t)
169 #define CACHE_PATH_CUTOFF 39
171 #define CACHE_ZONE_SMALL_SIZE (sizeof(struct namecache) + CACHE_PATH_CUTOFF + 1)
172 #define CACHE_ZONE_SMALL_TS_SIZE (sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1)
173 #define CACHE_ZONE_LARGE_SIZE (sizeof(struct namecache) + NAME_MAX + 1)
174 #define CACHE_ZONE_LARGE_TS_SIZE (sizeof(struct namecache_ts) + NAME_MAX + 1)
176 _Static_assert((CACHE_ZONE_SMALL_SIZE % (CACHE_ZONE_ALIGNMENT + 1)) == 0, "bad zone size");
177 _Static_assert((CACHE_ZONE_SMALL_TS_SIZE % (CACHE_ZONE_ALIGNMENT + 1)) == 0, "bad zone size");
178 _Static_assert((CACHE_ZONE_LARGE_SIZE % (CACHE_ZONE_ALIGNMENT + 1)) == 0, "bad zone size");
179 _Static_assert((CACHE_ZONE_LARGE_TS_SIZE % (CACHE_ZONE_ALIGNMENT + 1)) == 0, "bad zone size");
181 #define nc_vp n_un.nu_vp
182 #define nc_neg n_un.nu_neg
185 * Flags in namecache.nc_flag
187 #define NCF_WHITE 0x01
188 #define NCF_ISDOTDOT 0x02
191 #define NCF_DVDROP 0x10
192 #define NCF_NEGATIVE 0x20
193 #define NCF_INVALID 0x40
197 * Flags in negstate.neg_flag
202 * Mark an entry as invalid.
204 * This is called before it starts getting deconstructed.
207 cache_ncp_invalidate(struct namecache *ncp)
210 KASSERT((ncp->nc_flag & NCF_INVALID) == 0,
211 ("%s: entry %p already invalid", __func__, ncp));
212 atomic_store_char(&ncp->nc_flag, ncp->nc_flag | NCF_INVALID);
213 atomic_thread_fence_rel();
217 * Check whether the entry can be safely used.
219 * All places which elide locks are supposed to call this after they are
220 * done with reading from an entry.
223 cache_ncp_canuse(struct namecache *ncp)
226 atomic_thread_fence_acq();
227 return ((atomic_load_char(&ncp->nc_flag) & (NCF_INVALID | NCF_WIP)) == 0);
231 * Name caching works as follows:
233 * Names found by directory scans are retained in a cache
234 * for future reference. It is managed LRU, so frequently
235 * used names will hang around. Cache is indexed by hash value
236 * obtained from (dvp, name) where dvp refers to the directory
239 * If it is a "negative" entry, (i.e. for a name that is known NOT to
240 * exist) the vnode pointer will be NULL.
242 * Upon reaching the last segment of a path, if the reference
243 * is for DELETE, or NOCACHE is set (rewrite), and the
244 * name is located in the cache, it will be dropped.
246 * These locks are used (in the order in which they can be taken):
248 * vnodelock mtx vnode lists and v_cache_dd field protection
249 * bucketlock mtx for access to given set of hash buckets
250 * neglist mtx negative entry LRU management
252 * Additionally, ncneg_shrink_lock mtx is used to have at most one thread
253 * shrinking the LRU list.
255 * It is legal to take multiple vnodelock and bucketlock locks. The locking
256 * order is lower address first. Both are recursive.
258 * "." lookups are lockless.
260 * ".." and vnode -> name lookups require vnodelock.
262 * name -> vnode lookup requires the relevant bucketlock to be held for reading.
264 * Insertions and removals of entries require involved vnodes and bucketlocks
265 * to be locked to provide safe operation against other threads modifying the
268 * Some lookups result in removal of the found entry (e.g. getting rid of a
269 * negative entry with the intent to create a positive one), which poses a
270 * problem when multiple threads reach the state. Similarly, two different
271 * threads can purge two different vnodes and try to remove the same name.
273 * If the already held vnode lock is lower than the second required lock, we
274 * can just take the other lock. However, in the opposite case, this could
275 * deadlock. As such, this is resolved by trylocking and if that fails unlocking
276 * the first node, locking everything in order and revalidating the state.
282 * Structures associated with name caching.
284 #define NCHHASH(hash) \
285 (&nchashtbl[(hash) & nchash])
286 static __read_mostly CK_SLIST_HEAD(nchashhead, namecache) *nchashtbl;/* Hash Table */
287 static u_long __read_mostly nchash; /* size of hash table */
288 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0,
289 "Size of namecache hash table");
290 static u_long __read_mostly ncnegfactor = 5; /* ratio of negative entries */
291 SYSCTL_ULONG(_vfs, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0,
292 "Ratio of negative namecache entries");
293 static u_long __exclusive_cache_line numneg; /* number of negative entries allocated */
294 static u_long __exclusive_cache_line numcache;/* number of cache entries allocated */
295 u_int ncsizefactor = 2;
296 SYSCTL_UINT(_vfs, OID_AUTO, ncsizefactor, CTLFLAG_RW, &ncsizefactor, 0,
297 "Size factor for namecache");
298 static u_int __read_mostly ncpurgeminvnodes;
299 SYSCTL_UINT(_vfs, OID_AUTO, ncpurgeminvnodes, CTLFLAG_RW, &ncpurgeminvnodes, 0,
300 "Number of vnodes below which purgevfs ignores the request");
301 static u_int __read_mostly ncsize; /* the size as computed on creation or resizing */
303 struct nchstats nchstats; /* cache effectiveness statistics */
305 static bool __read_frequently cache_fast_revlookup = true;
306 SYSCTL_BOOL(_vfs, OID_AUTO, cache_fast_revlookup, CTLFLAG_RW,
307 &cache_fast_revlookup, 0, "");
309 static struct mtx __exclusive_cache_line ncneg_shrink_lock;
313 TAILQ_HEAD(, namecache) nl_list;
314 } __aligned(CACHE_LINE_SIZE);
316 static struct neglist __read_mostly *neglists;
317 static struct neglist ncneg_hot;
318 static u_long numhotneg;
321 #define numneglists (ncneghash + 1)
322 static inline struct neglist *
323 NCP2NEGLIST(struct namecache *ncp)
326 return (&neglists[(((uintptr_t)(ncp) >> 8) & ncneghash)]);
329 static inline struct negstate *
330 NCP2NEGSTATE(struct namecache *ncp)
333 MPASS(ncp->nc_flag & NCF_NEGATIVE);
334 return (&ncp->nc_neg);
337 #define numbucketlocks (ncbuckethash + 1)
338 static u_int __read_mostly ncbuckethash;
339 static struct mtx_padalign __read_mostly *bucketlocks;
340 #define HASH2BUCKETLOCK(hash) \
341 ((struct mtx *)(&bucketlocks[((hash) & ncbuckethash)]))
343 #define numvnodelocks (ncvnodehash + 1)
344 static u_int __read_mostly ncvnodehash;
345 static struct mtx __read_mostly *vnodelocks;
346 static inline struct mtx *
347 VP2VNODELOCK(struct vnode *vp)
350 return (&vnodelocks[(((uintptr_t)(vp) >> 8) & ncvnodehash)]);
354 * UMA zones for the VFS cache.
356 * The small cache is used for entries with short names, which are the
357 * most common. The large cache is used for entries which are too big to
358 * fit in the small cache.
360 static uma_zone_t __read_mostly cache_zone_small;
361 static uma_zone_t __read_mostly cache_zone_small_ts;
362 static uma_zone_t __read_mostly cache_zone_large;
363 static uma_zone_t __read_mostly cache_zone_large_ts;
365 static struct namecache *
366 cache_alloc(int len, int ts)
368 struct namecache_ts *ncp_ts;
369 struct namecache *ncp;
371 if (__predict_false(ts)) {
372 if (len <= CACHE_PATH_CUTOFF)
373 ncp_ts = uma_zalloc_smr(cache_zone_small_ts, M_WAITOK);
375 ncp_ts = uma_zalloc_smr(cache_zone_large_ts, M_WAITOK);
376 ncp = &ncp_ts->nc_nc;
378 if (len <= CACHE_PATH_CUTOFF)
379 ncp = uma_zalloc_smr(cache_zone_small, M_WAITOK);
381 ncp = uma_zalloc_smr(cache_zone_large, M_WAITOK);
387 cache_free(struct namecache *ncp)
389 struct namecache_ts *ncp_ts;
392 if ((ncp->nc_flag & NCF_DVDROP) != 0)
394 if (__predict_false(ncp->nc_flag & NCF_TS)) {
395 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
396 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
397 uma_zfree_smr(cache_zone_small_ts, ncp_ts);
399 uma_zfree_smr(cache_zone_large_ts, ncp_ts);
401 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
402 uma_zfree_smr(cache_zone_small, ncp);
404 uma_zfree_smr(cache_zone_large, ncp);
409 cache_out_ts(struct namecache *ncp, struct timespec *tsp, int *ticksp)
411 struct namecache_ts *ncp_ts;
413 KASSERT((ncp->nc_flag & NCF_TS) != 0 ||
414 (tsp == NULL && ticksp == NULL),
420 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
421 *tsp = ncp_ts->nc_time;
422 *ticksp = ncp_ts->nc_ticks;
426 static int __read_mostly doingcache = 1; /* 1 => enable the cache */
427 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0,
428 "VFS namecache enabled");
431 /* Export size information to userland */
432 SYSCTL_INT(_debug_sizeof, OID_AUTO, namecache, CTLFLAG_RD, SYSCTL_NULL_INT_PTR,
433 sizeof(struct namecache), "sizeof(struct namecache)");
436 * The new name cache statistics
438 static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
439 "Name cache statistics");
440 #define STATNODE_ULONG(name, descr) \
441 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, descr);
442 #define STATNODE_COUNTER(name, descr) \
443 static COUNTER_U64_DEFINE_EARLY(name); \
444 SYSCTL_COUNTER_U64(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, \
446 STATNODE_ULONG(numneg, "Number of negative cache entries");
447 STATNODE_ULONG(numcache, "Number of cache entries");
448 STATNODE_COUNTER(numcachehv, "Number of namecache entries with vnodes held");
449 STATNODE_COUNTER(numdrops, "Number of dropped entries due to reaching the limit");
450 STATNODE_COUNTER(dothits, "Number of '.' hits");
451 STATNODE_COUNTER(dotdothits, "Number of '..' hits");
452 STATNODE_COUNTER(nummiss, "Number of cache misses");
453 STATNODE_COUNTER(nummisszap, "Number of cache misses we do not want to cache");
454 STATNODE_COUNTER(numposzaps,
455 "Number of cache hits (positive) we do not want to cache");
456 STATNODE_COUNTER(numposhits, "Number of cache hits (positive)");
457 STATNODE_COUNTER(numnegzaps,
458 "Number of cache hits (negative) we do not want to cache");
459 STATNODE_COUNTER(numneghits, "Number of cache hits (negative)");
460 /* These count for vn_getcwd(), too. */
461 STATNODE_COUNTER(numfullpathcalls, "Number of fullpath search calls");
462 STATNODE_COUNTER(numfullpathfail1, "Number of fullpath search errors (ENOTDIR)");
463 STATNODE_COUNTER(numfullpathfail2,
464 "Number of fullpath search errors (VOP_VPTOCNP failures)");
465 STATNODE_COUNTER(numfullpathfail4, "Number of fullpath search errors (ENOMEM)");
466 STATNODE_COUNTER(numfullpathfound, "Number of successful fullpath calls");
467 STATNODE_COUNTER(zap_and_exit_bucket_relock_success,
468 "Number of successful removals after relocking");
469 static long zap_and_exit_bucket_fail; STATNODE_ULONG(zap_and_exit_bucket_fail,
470 "Number of times zap_and_exit failed to lock");
471 static long zap_and_exit_bucket_fail2; STATNODE_ULONG(zap_and_exit_bucket_fail2,
472 "Number of times zap_and_exit failed to lock");
473 static long cache_lock_vnodes_cel_3_failures;
474 STATNODE_ULONG(cache_lock_vnodes_cel_3_failures,
475 "Number of times 3-way vnode locking failed");
476 STATNODE_ULONG(numhotneg, "Number of hot negative entries");
477 STATNODE_COUNTER(numneg_evicted,
478 "Number of negative entries evicted when adding a new entry");
479 STATNODE_COUNTER(shrinking_skipped,
480 "Number of times shrinking was already in progress");
482 static void cache_zap_locked(struct namecache *ncp);
483 static int vn_fullpath_hardlink(struct nameidata *ndp, char **retbuf,
484 char **freebuf, size_t *buflen);
485 static int vn_fullpath_any_smr(struct vnode *vp, struct vnode *rdir, char *buf,
486 char **retbuf, size_t *buflen, bool slash_prefixed, size_t addend);
487 static int vn_fullpath_any(struct vnode *vp, struct vnode *rdir, char *buf,
488 char **retbuf, size_t *buflen);
489 static int vn_fullpath_dir(struct vnode *vp, struct vnode *rdir, char *buf,
490 char **retbuf, size_t *len, bool slash_prefixed, size_t addend);
492 static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
494 static int cache_yield;
495 SYSCTL_INT(_vfs_cache, OID_AUTO, yield, CTLFLAG_RD, &cache_yield, 0,
496 "Number of times cache called yield");
498 static void __noinline
499 cache_maybe_yield(void)
502 if (should_yield()) {
504 kern_yield(PRI_USER);
509 cache_assert_vlp_locked(struct mtx *vlp)
513 mtx_assert(vlp, MA_OWNED);
517 cache_assert_vnode_locked(struct vnode *vp)
521 vlp = VP2VNODELOCK(vp);
522 cache_assert_vlp_locked(vlp);
526 * TODO: With the value stored we can do better than computing the hash based
527 * on the address. The choice of FNV should also be revisited.
530 cache_prehash(struct vnode *vp)
533 vp->v_nchash = fnv_32_buf(&vp, sizeof(vp), FNV1_32_INIT);
537 cache_get_hash(char *name, u_char len, struct vnode *dvp)
540 return (fnv_32_buf(name, len, dvp->v_nchash));
543 static inline struct nchashhead *
544 NCP2BUCKET(struct namecache *ncp)
548 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen, ncp->nc_dvp);
549 return (NCHHASH(hash));
552 static inline struct mtx *
553 NCP2BUCKETLOCK(struct namecache *ncp)
557 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen, ncp->nc_dvp);
558 return (HASH2BUCKETLOCK(hash));
563 cache_assert_bucket_locked(struct namecache *ncp)
567 blp = NCP2BUCKETLOCK(ncp);
568 mtx_assert(blp, MA_OWNED);
572 cache_assert_bucket_unlocked(struct namecache *ncp)
576 blp = NCP2BUCKETLOCK(ncp);
577 mtx_assert(blp, MA_NOTOWNED);
580 #define cache_assert_bucket_locked(x) do { } while (0)
581 #define cache_assert_bucket_unlocked(x) do { } while (0)
584 #define cache_sort_vnodes(x, y) _cache_sort_vnodes((void **)(x), (void **)(y))
586 _cache_sort_vnodes(void **p1, void **p2)
590 MPASS(*p1 != NULL || *p2 != NULL);
600 cache_lock_all_buckets(void)
604 for (i = 0; i < numbucketlocks; i++)
605 mtx_lock(&bucketlocks[i]);
609 cache_unlock_all_buckets(void)
613 for (i = 0; i < numbucketlocks; i++)
614 mtx_unlock(&bucketlocks[i]);
618 cache_lock_all_vnodes(void)
622 for (i = 0; i < numvnodelocks; i++)
623 mtx_lock(&vnodelocks[i]);
627 cache_unlock_all_vnodes(void)
631 for (i = 0; i < numvnodelocks; i++)
632 mtx_unlock(&vnodelocks[i]);
636 cache_trylock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
639 cache_sort_vnodes(&vlp1, &vlp2);
642 if (!mtx_trylock(vlp1))
645 if (!mtx_trylock(vlp2)) {
655 cache_lock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
658 MPASS(vlp1 != NULL || vlp2 != NULL);
668 cache_unlock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
671 MPASS(vlp1 != NULL || vlp2 != NULL);
680 sysctl_nchstats(SYSCTL_HANDLER_ARGS)
682 struct nchstats snap;
684 if (req->oldptr == NULL)
685 return (SYSCTL_OUT(req, 0, sizeof(snap)));
688 snap.ncs_goodhits = counter_u64_fetch(numposhits);
689 snap.ncs_neghits = counter_u64_fetch(numneghits);
690 snap.ncs_badhits = counter_u64_fetch(numposzaps) +
691 counter_u64_fetch(numnegzaps);
692 snap.ncs_miss = counter_u64_fetch(nummisszap) +
693 counter_u64_fetch(nummiss);
695 return (SYSCTL_OUT(req, &snap, sizeof(snap)));
697 SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE | CTLFLAG_RD |
698 CTLFLAG_MPSAFE, 0, 0, sysctl_nchstats, "LU",
699 "VFS cache effectiveness statistics");
703 * Grab an atomic snapshot of the name cache hash chain lengths
705 static SYSCTL_NODE(_debug, OID_AUTO, hashstat,
706 CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
710 sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS)
712 struct nchashhead *ncpp;
713 struct namecache *ncp;
714 int i, error, n_nchash, *cntbuf;
717 n_nchash = nchash + 1; /* nchash is max index, not count */
718 if (req->oldptr == NULL)
719 return SYSCTL_OUT(req, 0, n_nchash * sizeof(int));
720 cntbuf = malloc(n_nchash * sizeof(int), M_TEMP, M_ZERO | M_WAITOK);
721 cache_lock_all_buckets();
722 if (n_nchash != nchash + 1) {
723 cache_unlock_all_buckets();
724 free(cntbuf, M_TEMP);
727 /* Scan hash tables counting entries */
728 for (ncpp = nchashtbl, i = 0; i < n_nchash; ncpp++, i++)
729 CK_SLIST_FOREACH(ncp, ncpp, nc_hash)
731 cache_unlock_all_buckets();
732 for (error = 0, i = 0; i < n_nchash; i++)
733 if ((error = SYSCTL_OUT(req, &cntbuf[i], sizeof(int))) != 0)
735 free(cntbuf, M_TEMP);
738 SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD|
739 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_rawnchash, "S,int",
740 "nchash chain lengths");
743 sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS)
746 struct nchashhead *ncpp;
747 struct namecache *ncp;
749 int count, maxlength, used, pct;
752 return SYSCTL_OUT(req, 0, 4 * sizeof(int));
754 cache_lock_all_buckets();
755 n_nchash = nchash + 1; /* nchash is max index, not count */
759 /* Scan hash tables for applicable entries */
760 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
762 CK_SLIST_FOREACH(ncp, ncpp, nc_hash) {
767 if (maxlength < count)
770 n_nchash = nchash + 1;
771 cache_unlock_all_buckets();
772 pct = (used * 100) / (n_nchash / 100);
773 error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash));
776 error = SYSCTL_OUT(req, &used, sizeof(used));
779 error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength));
782 error = SYSCTL_OUT(req, &pct, sizeof(pct));
787 SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD|
788 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_nchash, "I",
789 "nchash statistics (number of total/used buckets, maximum chain length, usage percentage)");
793 * Negative entries management
795 * A variation of LRU scheme is used. New entries are hashed into one of
796 * numneglists cold lists. Entries get promoted to the hot list on first hit.
798 * The shrinker will demote hot list head and evict from the cold list in a
799 * round-robin manner.
802 cache_negative_init(struct namecache *ncp)
804 struct negstate *negstate;
806 ncp->nc_flag |= NCF_NEGATIVE;
807 negstate = NCP2NEGSTATE(ncp);
808 negstate->neg_flag = 0;
812 cache_negative_hit(struct namecache *ncp)
814 struct neglist *neglist;
815 struct negstate *negstate;
817 negstate = NCP2NEGSTATE(ncp);
818 if ((negstate->neg_flag & NEG_HOT) != 0)
820 neglist = NCP2NEGLIST(ncp);
821 mtx_lock(&ncneg_hot.nl_lock);
822 mtx_lock(&neglist->nl_lock);
823 if ((negstate->neg_flag & NEG_HOT) == 0) {
825 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
826 TAILQ_INSERT_TAIL(&ncneg_hot.nl_list, ncp, nc_dst);
827 negstate->neg_flag |= NEG_HOT;
829 mtx_unlock(&neglist->nl_lock);
830 mtx_unlock(&ncneg_hot.nl_lock);
834 cache_negative_insert(struct namecache *ncp)
836 struct neglist *neglist;
838 MPASS(ncp->nc_flag & NCF_NEGATIVE);
839 cache_assert_bucket_locked(ncp);
840 neglist = NCP2NEGLIST(ncp);
841 mtx_lock(&neglist->nl_lock);
842 TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst);
843 mtx_unlock(&neglist->nl_lock);
844 atomic_add_rel_long(&numneg, 1);
848 cache_negative_remove(struct namecache *ncp)
850 struct neglist *neglist;
851 struct negstate *negstate;
852 bool hot_locked = false;
853 bool list_locked = false;
855 cache_assert_bucket_locked(ncp);
856 neglist = NCP2NEGLIST(ncp);
857 negstate = NCP2NEGSTATE(ncp);
858 if ((negstate->neg_flag & NEG_HOT) != 0) {
860 mtx_lock(&ncneg_hot.nl_lock);
861 if ((negstate->neg_flag & NEG_HOT) == 0) {
863 mtx_lock(&neglist->nl_lock);
867 mtx_lock(&neglist->nl_lock);
869 * We may be racing against promotion in lockless lookup.
871 if ((negstate->neg_flag & NEG_HOT) != 0) {
872 mtx_unlock(&neglist->nl_lock);
874 mtx_lock(&ncneg_hot.nl_lock);
875 mtx_lock(&neglist->nl_lock);
878 if ((negstate->neg_flag & NEG_HOT) != 0) {
879 mtx_assert(&ncneg_hot.nl_lock, MA_OWNED);
880 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
883 mtx_assert(&neglist->nl_lock, MA_OWNED);
884 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
887 mtx_unlock(&neglist->nl_lock);
889 mtx_unlock(&ncneg_hot.nl_lock);
890 atomic_subtract_rel_long(&numneg, 1);
894 cache_negative_shrink_select(struct namecache **ncpp,
895 struct neglist **neglistpp)
897 struct neglist *neglist;
898 struct namecache *ncp;
904 for (i = 0; i < numneglists; i++) {
905 neglist = &neglists[(cycle + i) % numneglists];
906 if (TAILQ_FIRST(&neglist->nl_list) == NULL)
908 mtx_lock(&neglist->nl_lock);
909 ncp = TAILQ_FIRST(&neglist->nl_list);
912 mtx_unlock(&neglist->nl_lock);
915 *neglistpp = neglist;
921 cache_negative_zap_one(void)
923 struct namecache *ncp, *ncp2;
924 struct neglist *neglist;
925 struct negstate *negstate;
929 if (mtx_owner(&ncneg_shrink_lock) != NULL ||
930 !mtx_trylock(&ncneg_shrink_lock)) {
931 counter_u64_add(shrinking_skipped, 1);
935 mtx_lock(&ncneg_hot.nl_lock);
936 ncp = TAILQ_FIRST(&ncneg_hot.nl_list);
938 neglist = NCP2NEGLIST(ncp);
939 negstate = NCP2NEGSTATE(ncp);
940 mtx_lock(&neglist->nl_lock);
941 MPASS((negstate->neg_flag & NEG_HOT) != 0);
942 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
943 TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst);
944 negstate->neg_flag &= ~NEG_HOT;
946 mtx_unlock(&neglist->nl_lock);
948 mtx_unlock(&ncneg_hot.nl_lock);
950 cache_negative_shrink_select(&ncp, &neglist);
952 mtx_unlock(&ncneg_shrink_lock);
956 MPASS(ncp->nc_flag & NCF_NEGATIVE);
957 dvlp = VP2VNODELOCK(ncp->nc_dvp);
958 blp = NCP2BUCKETLOCK(ncp);
959 mtx_unlock(&neglist->nl_lock);
963 * Enter SMR to safely check the negative list.
964 * Even if the found pointer matches, the entry may now be reallocated
965 * and used by a different vnode.
968 ncp2 = TAILQ_FIRST(&neglist->nl_list);
969 if (ncp != ncp2 || dvlp != VP2VNODELOCK(ncp2->nc_dvp) ||
970 blp != NCP2BUCKETLOCK(ncp2)) {
975 SDT_PROBE2(vfs, namecache, shrink_negative, done, ncp->nc_dvp,
977 cache_zap_locked(ncp);
978 counter_u64_add(numneg_evicted, 1);
987 * cache_zap_locked():
989 * Removes a namecache entry from cache, whether it contains an actual
990 * pointer to a vnode or if it is just a negative cache entry.
993 cache_zap_locked(struct namecache *ncp)
995 struct nchashhead *ncpp;
997 if (!(ncp->nc_flag & NCF_NEGATIVE))
998 cache_assert_vnode_locked(ncp->nc_vp);
999 cache_assert_vnode_locked(ncp->nc_dvp);
1000 cache_assert_bucket_locked(ncp);
1002 cache_ncp_invalidate(ncp);
1004 ncpp = NCP2BUCKET(ncp);
1005 CK_SLIST_REMOVE(ncpp, ncp, namecache, nc_hash);
1006 if (!(ncp->nc_flag & NCF_NEGATIVE)) {
1007 SDT_PROBE3(vfs, namecache, zap, done, ncp->nc_dvp,
1008 ncp->nc_name, ncp->nc_vp);
1009 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
1010 if (ncp == ncp->nc_vp->v_cache_dd) {
1011 vn_seqc_write_begin_unheld(ncp->nc_vp);
1012 ncp->nc_vp->v_cache_dd = NULL;
1013 vn_seqc_write_end(ncp->nc_vp);
1016 SDT_PROBE2(vfs, namecache, zap_negative, done, ncp->nc_dvp,
1018 cache_negative_remove(ncp);
1020 if (ncp->nc_flag & NCF_ISDOTDOT) {
1021 if (ncp == ncp->nc_dvp->v_cache_dd) {
1022 vn_seqc_write_begin_unheld(ncp->nc_dvp);
1023 ncp->nc_dvp->v_cache_dd = NULL;
1024 vn_seqc_write_end(ncp->nc_dvp);
1027 LIST_REMOVE(ncp, nc_src);
1028 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) {
1029 ncp->nc_flag |= NCF_DVDROP;
1030 counter_u64_add(numcachehv, -1);
1033 atomic_subtract_rel_long(&numcache, 1);
1037 cache_zap_negative_locked_vnode_kl(struct namecache *ncp, struct vnode *vp)
1041 MPASS(ncp->nc_dvp == vp);
1042 MPASS(ncp->nc_flag & NCF_NEGATIVE);
1043 cache_assert_vnode_locked(vp);
1045 blp = NCP2BUCKETLOCK(ncp);
1047 cache_zap_locked(ncp);
1052 cache_zap_locked_vnode_kl2(struct namecache *ncp, struct vnode *vp,
1055 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
1058 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
1059 cache_assert_vnode_locked(vp);
1061 if (ncp->nc_flag & NCF_NEGATIVE) {
1062 if (*vlpp != NULL) {
1066 cache_zap_negative_locked_vnode_kl(ncp, vp);
1070 pvlp = VP2VNODELOCK(vp);
1071 blp = NCP2BUCKETLOCK(ncp);
1072 vlp1 = VP2VNODELOCK(ncp->nc_dvp);
1073 vlp2 = VP2VNODELOCK(ncp->nc_vp);
1075 if (*vlpp == vlp1 || *vlpp == vlp2) {
1079 if (*vlpp != NULL) {
1083 cache_sort_vnodes(&vlp1, &vlp2);
1088 if (!mtx_trylock(vlp1))
1094 cache_zap_locked(ncp);
1096 if (to_unlock != NULL)
1097 mtx_unlock(to_unlock);
1104 MPASS(*vlpp == NULL);
1109 static int __noinline
1110 cache_zap_locked_vnode(struct namecache *ncp, struct vnode *vp)
1112 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
1116 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
1117 cache_assert_vnode_locked(vp);
1119 pvlp = VP2VNODELOCK(vp);
1120 if (ncp->nc_flag & NCF_NEGATIVE) {
1121 cache_zap_negative_locked_vnode_kl(ncp, vp);
1125 blp = NCP2BUCKETLOCK(ncp);
1126 vlp1 = VP2VNODELOCK(ncp->nc_dvp);
1127 vlp2 = VP2VNODELOCK(ncp->nc_vp);
1128 cache_sort_vnodes(&vlp1, &vlp2);
1133 if (!mtx_trylock(vlp1)) {
1135 * TODO: Very wasteful but rare.
1147 cache_zap_locked(ncp);
1149 mtx_unlock(to_unlock);
1156 * If trylocking failed we can get here. We know enough to take all needed locks
1157 * in the right order and re-lookup the entry.
1160 cache_zap_unlocked_bucket(struct namecache *ncp, struct componentname *cnp,
1161 struct vnode *dvp, struct mtx *dvlp, struct mtx *vlp, uint32_t hash,
1164 struct namecache *rncp;
1166 cache_assert_bucket_unlocked(ncp);
1168 cache_sort_vnodes(&dvlp, &vlp);
1169 cache_lock_vnodes(dvlp, vlp);
1171 CK_SLIST_FOREACH(rncp, (NCHHASH(hash)), nc_hash) {
1172 if (rncp == ncp && rncp->nc_dvp == dvp &&
1173 rncp->nc_nlen == cnp->cn_namelen &&
1174 !bcmp(rncp->nc_name, cnp->cn_nameptr, rncp->nc_nlen))
1178 cache_zap_locked(rncp);
1180 cache_unlock_vnodes(dvlp, vlp);
1181 counter_u64_add(zap_and_exit_bucket_relock_success, 1);
1186 cache_unlock_vnodes(dvlp, vlp);
1190 static int __noinline
1191 cache_zap_locked_bucket(struct namecache *ncp, struct componentname *cnp,
1192 uint32_t hash, struct mtx *blp)
1194 struct mtx *dvlp, *vlp;
1197 cache_assert_bucket_locked(ncp);
1199 dvlp = VP2VNODELOCK(ncp->nc_dvp);
1201 if (!(ncp->nc_flag & NCF_NEGATIVE))
1202 vlp = VP2VNODELOCK(ncp->nc_vp);
1203 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1204 cache_zap_locked(ncp);
1206 cache_unlock_vnodes(dvlp, vlp);
1212 return (cache_zap_unlocked_bucket(ncp, cnp, dvp, dvlp, vlp, hash, blp));
1216 cache_zap_locked_bucket_kl(struct namecache *ncp, struct mtx *blp,
1217 struct mtx **vlpp1, struct mtx **vlpp2)
1219 struct mtx *dvlp, *vlp;
1221 cache_assert_bucket_locked(ncp);
1223 dvlp = VP2VNODELOCK(ncp->nc_dvp);
1225 if (!(ncp->nc_flag & NCF_NEGATIVE))
1226 vlp = VP2VNODELOCK(ncp->nc_vp);
1227 cache_sort_vnodes(&dvlp, &vlp);
1229 if (*vlpp1 == dvlp && *vlpp2 == vlp) {
1230 cache_zap_locked(ncp);
1231 cache_unlock_vnodes(dvlp, vlp);
1244 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1245 cache_zap_locked(ncp);
1246 cache_unlock_vnodes(dvlp, vlp);
1260 static __noinline int
1261 cache_remove_cnp(struct vnode *dvp, struct componentname *cnp)
1263 struct namecache *ncp;
1265 struct mtx *dvlp, *dvlp2;
1269 if (cnp->cn_namelen == 2 &&
1270 cnp->cn_nameptr[0] == '.' && cnp->cn_nameptr[1] == '.') {
1271 dvlp = VP2VNODELOCK(dvp);
1275 ncp = dvp->v_cache_dd;
1280 SDT_PROBE2(vfs, namecache, removecnp, miss, dvp, cnp);
1283 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1284 if (!cache_zap_locked_vnode_kl2(ncp, dvp, &dvlp2))
1286 MPASS(dvp->v_cache_dd == NULL);
1292 vn_seqc_write_begin(dvp);
1293 dvp->v_cache_dd = NULL;
1294 vn_seqc_write_end(dvp);
1299 SDT_PROBE2(vfs, namecache, removecnp, hit, dvp, cnp);
1303 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1304 blp = HASH2BUCKETLOCK(hash);
1306 if (CK_SLIST_EMPTY(NCHHASH(hash)))
1311 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1312 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1313 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1322 error = cache_zap_locked_bucket(ncp, cnp, hash, blp);
1323 if (__predict_false(error != 0)) {
1324 zap_and_exit_bucket_fail++;
1327 counter_u64_add(numposzaps, 1);
1328 SDT_PROBE2(vfs, namecache, removecnp, hit, dvp, cnp);
1332 counter_u64_add(nummisszap, 1);
1333 SDT_PROBE2(vfs, namecache, removecnp, miss, dvp, cnp);
1337 static int __noinline
1338 cache_lookup_dot(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1339 struct timespec *tsp, int *ticksp)
1344 counter_u64_add(dothits, 1);
1345 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", *vpp);
1352 * When we lookup "." we still can be asked to lock it
1355 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
1356 if (ltype != VOP_ISLOCKED(*vpp)) {
1357 if (ltype == LK_EXCLUSIVE) {
1358 vn_lock(*vpp, LK_UPGRADE | LK_RETRY);
1359 if (VN_IS_DOOMED((*vpp))) {
1360 /* forced unmount */
1366 vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY);
1371 static int __noinline
1372 cache_lookup_dotdot(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1373 struct timespec *tsp, int *ticksp)
1375 struct namecache_ts *ncp_ts;
1376 struct namecache *ncp;
1382 MPASS((cnp->cn_flags & ISDOTDOT) != 0);
1384 if ((cnp->cn_flags & MAKEENTRY) == 0) {
1385 cache_remove_cnp(dvp, cnp);
1389 counter_u64_add(dotdothits, 1);
1391 dvlp = VP2VNODELOCK(dvp);
1393 ncp = dvp->v_cache_dd;
1395 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, "..", NULL);
1399 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1400 if (ncp->nc_flag & NCF_NEGATIVE)
1407 goto negative_success;
1408 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, "..", *vpp);
1409 cache_out_ts(ncp, tsp, ticksp);
1410 if ((ncp->nc_flag & (NCF_ISDOTDOT | NCF_DTS)) ==
1411 NCF_DTS && tsp != NULL) {
1412 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1413 *tsp = ncp_ts->nc_dotdottime;
1417 ltype = VOP_ISLOCKED(dvp);
1419 vs = vget_prep(*vpp);
1421 error = vget_finish(*vpp, cnp->cn_lkflags, vs);
1422 vn_lock(dvp, ltype | LK_RETRY);
1423 if (VN_IS_DOOMED(dvp)) {
1435 if (__predict_false(cnp->cn_nameiop == CREATE)) {
1436 if (cnp->cn_flags & ISLASTCN) {
1437 counter_u64_add(numnegzaps, 1);
1438 error = cache_zap_locked_vnode(ncp, dvp);
1439 if (__predict_false(error != 0)) {
1440 zap_and_exit_bucket_fail2++;
1448 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp, ncp->nc_name);
1449 cache_out_ts(ncp, tsp, ticksp);
1450 counter_u64_add(numneghits, 1);
1451 whiteout = (ncp->nc_flag & NCF_WHITE);
1452 cache_negative_hit(ncp);
1455 cnp->cn_flags |= ISWHITEOUT;
1460 * Lookup a name in the name cache
1464 * - dvp: Parent directory in which to search.
1465 * - vpp: Return argument. Will contain desired vnode on cache hit.
1466 * - cnp: Parameters of the name search. The most interesting bits of
1467 * the cn_flags field have the following meanings:
1468 * - MAKEENTRY: If clear, free an entry from the cache rather than look
1470 * - ISDOTDOT: Must be set if and only if cn_nameptr == ".."
1471 * - tsp: Return storage for cache timestamp. On a successful (positive
1472 * or negative) lookup, tsp will be filled with any timespec that
1473 * was stored when this cache entry was created. However, it will
1474 * be clear for "." entries.
1475 * - ticks: Return storage for alternate cache timestamp. On a successful
1476 * (positive or negative) lookup, it will contain the ticks value
1477 * that was current when the cache entry was created, unless cnp
1480 * Either both tsp and ticks have to be provided or neither of them.
1484 * - -1: A positive cache hit. vpp will contain the desired vnode.
1485 * - ENOENT: A negative cache hit, or dvp was recycled out from under us due
1486 * to a forced unmount. vpp will not be modified. If the entry
1487 * is a whiteout, then the ISWHITEOUT flag will be set in
1489 * - 0: A cache miss. vpp will not be modified.
1493 * On a cache hit, vpp will be returned locked and ref'd. If we're looking up
1494 * .., dvp is unlocked. If we're looking up . an extra ref is taken, but the
1495 * lock is not recursively acquired.
1497 static int __noinline
1498 cache_lookup_fallback(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1499 struct timespec *tsp, int *ticksp)
1501 struct namecache *ncp;
1508 MPASS((cnp->cn_flags & (MAKEENTRY | ISDOTDOT)) == MAKEENTRY);
1511 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1512 blp = HASH2BUCKETLOCK(hash);
1515 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1516 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1517 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1521 if (__predict_false(ncp == NULL)) {
1523 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
1525 counter_u64_add(nummiss, 1);
1529 if (ncp->nc_flag & NCF_NEGATIVE)
1530 goto negative_success;
1532 counter_u64_add(numposhits, 1);
1534 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name, *vpp);
1535 cache_out_ts(ncp, tsp, ticksp);
1537 vs = vget_prep(*vpp);
1539 error = vget_finish(*vpp, cnp->cn_lkflags, vs);
1546 if (__predict_false(cnp->cn_nameiop == CREATE)) {
1547 if (cnp->cn_flags & ISLASTCN) {
1548 counter_u64_add(numnegzaps, 1);
1549 error = cache_zap_locked_vnode(ncp, dvp);
1550 if (__predict_false(error != 0)) {
1551 zap_and_exit_bucket_fail2++;
1559 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp, ncp->nc_name);
1560 cache_out_ts(ncp, tsp, ticksp);
1561 counter_u64_add(numneghits, 1);
1562 whiteout = (ncp->nc_flag & NCF_WHITE);
1563 cache_negative_hit(ncp);
1566 cnp->cn_flags |= ISWHITEOUT;
1571 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1572 struct timespec *tsp, int *ticksp)
1574 struct namecache *ncp;
1575 struct negstate *negstate;
1582 MPASS((tsp == NULL && ticksp == NULL) || (tsp != NULL && ticksp != NULL));
1585 if (__predict_false(!doingcache)) {
1586 cnp->cn_flags &= ~MAKEENTRY;
1591 if (__predict_false(cnp->cn_nameptr[0] == '.')) {
1592 if (cnp->cn_namelen == 1)
1593 return (cache_lookup_dot(dvp, vpp, cnp, tsp, ticksp));
1594 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.')
1595 return (cache_lookup_dotdot(dvp, vpp, cnp, tsp, ticksp));
1598 MPASS((cnp->cn_flags & ISDOTDOT) == 0);
1600 if ((cnp->cn_flags & MAKEENTRY) == 0) {
1601 cache_remove_cnp(dvp, cnp);
1605 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1608 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1609 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1610 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1614 if (__predict_false(ncp == NULL)) {
1616 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
1618 counter_u64_add(nummiss, 1);
1622 nc_flag = atomic_load_char(&ncp->nc_flag);
1623 if (nc_flag & NCF_NEGATIVE)
1624 goto negative_success;
1626 counter_u64_add(numposhits, 1);
1628 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name, *vpp);
1629 cache_out_ts(ncp, tsp, ticksp);
1631 if (!cache_ncp_canuse(ncp)) {
1636 vs = vget_prep_smr(*vpp);
1638 if (__predict_false(vs == VGET_NONE)) {
1642 error = vget_finish(*vpp, cnp->cn_lkflags, vs);
1649 if (__predict_false(cnp->cn_nameiop == CREATE)) {
1650 if (cnp->cn_flags & ISLASTCN) {
1656 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp, ncp->nc_name);
1657 cache_out_ts(ncp, tsp, ticksp);
1658 counter_u64_add(numneghits, 1);
1659 whiteout = (ncp->nc_flag & NCF_WHITE);
1661 * TODO: We need to take locks to promote an entry. Code doing it
1662 * in SMR lookup can be modified to be shared.
1664 negstate = NCP2NEGSTATE(ncp);
1665 if ((negstate->neg_flag & NEG_HOT) == 0 ||
1666 !cache_ncp_canuse(ncp)) {
1672 cnp->cn_flags |= ISWHITEOUT;
1675 return (cache_lookup_fallback(dvp, vpp, cnp, tsp, ticksp));
1678 struct celockstate {
1682 CTASSERT((nitems(((struct celockstate *)0)->vlp) == 3));
1683 CTASSERT((nitems(((struct celockstate *)0)->blp) == 2));
1686 cache_celockstate_init(struct celockstate *cel)
1689 bzero(cel, sizeof(*cel));
1693 cache_lock_vnodes_cel(struct celockstate *cel, struct vnode *vp,
1696 struct mtx *vlp1, *vlp2;
1698 MPASS(cel->vlp[0] == NULL);
1699 MPASS(cel->vlp[1] == NULL);
1700 MPASS(cel->vlp[2] == NULL);
1702 MPASS(vp != NULL || dvp != NULL);
1704 vlp1 = VP2VNODELOCK(vp);
1705 vlp2 = VP2VNODELOCK(dvp);
1706 cache_sort_vnodes(&vlp1, &vlp2);
1717 cache_unlock_vnodes_cel(struct celockstate *cel)
1720 MPASS(cel->vlp[0] != NULL || cel->vlp[1] != NULL);
1722 if (cel->vlp[0] != NULL)
1723 mtx_unlock(cel->vlp[0]);
1724 if (cel->vlp[1] != NULL)
1725 mtx_unlock(cel->vlp[1]);
1726 if (cel->vlp[2] != NULL)
1727 mtx_unlock(cel->vlp[2]);
1731 cache_lock_vnodes_cel_3(struct celockstate *cel, struct vnode *vp)
1736 cache_assert_vlp_locked(cel->vlp[0]);
1737 cache_assert_vlp_locked(cel->vlp[1]);
1738 MPASS(cel->vlp[2] == NULL);
1741 vlp = VP2VNODELOCK(vp);
1744 if (vlp >= cel->vlp[1]) {
1747 if (mtx_trylock(vlp))
1749 cache_lock_vnodes_cel_3_failures++;
1750 cache_unlock_vnodes_cel(cel);
1751 if (vlp < cel->vlp[0]) {
1753 mtx_lock(cel->vlp[0]);
1754 mtx_lock(cel->vlp[1]);
1756 if (cel->vlp[0] != NULL)
1757 mtx_lock(cel->vlp[0]);
1759 mtx_lock(cel->vlp[1]);
1769 cache_lock_buckets_cel(struct celockstate *cel, struct mtx *blp1,
1773 MPASS(cel->blp[0] == NULL);
1774 MPASS(cel->blp[1] == NULL);
1776 cache_sort_vnodes(&blp1, &blp2);
1787 cache_unlock_buckets_cel(struct celockstate *cel)
1790 if (cel->blp[0] != NULL)
1791 mtx_unlock(cel->blp[0]);
1792 mtx_unlock(cel->blp[1]);
1796 * Lock part of the cache affected by the insertion.
1798 * This means vnodelocks for dvp, vp and the relevant bucketlock.
1799 * However, insertion can result in removal of an old entry. In this
1800 * case we have an additional vnode and bucketlock pair to lock.
1802 * That is, in the worst case we have to lock 3 vnodes and 2 bucketlocks, while
1803 * preserving the locking order (smaller address first).
1806 cache_enter_lock(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1809 struct namecache *ncp;
1810 struct mtx *blps[2];
1812 blps[0] = HASH2BUCKETLOCK(hash);
1815 cache_lock_vnodes_cel(cel, dvp, vp);
1816 if (vp == NULL || vp->v_type != VDIR)
1818 ncp = vp->v_cache_dd;
1821 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1823 MPASS(ncp->nc_dvp == vp);
1824 blps[1] = NCP2BUCKETLOCK(ncp);
1825 if (ncp->nc_flag & NCF_NEGATIVE)
1827 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1830 * All vnodes got re-locked. Re-validate the state and if
1831 * nothing changed we are done. Otherwise restart.
1833 if (ncp == vp->v_cache_dd &&
1834 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1835 blps[1] == NCP2BUCKETLOCK(ncp) &&
1836 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1838 cache_unlock_vnodes_cel(cel);
1843 cache_lock_buckets_cel(cel, blps[0], blps[1]);
1847 cache_enter_lock_dd(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1850 struct namecache *ncp;
1851 struct mtx *blps[2];
1853 blps[0] = HASH2BUCKETLOCK(hash);
1856 cache_lock_vnodes_cel(cel, dvp, vp);
1857 ncp = dvp->v_cache_dd;
1860 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1862 MPASS(ncp->nc_dvp == dvp);
1863 blps[1] = NCP2BUCKETLOCK(ncp);
1864 if (ncp->nc_flag & NCF_NEGATIVE)
1866 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1868 if (ncp == dvp->v_cache_dd &&
1869 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1870 blps[1] == NCP2BUCKETLOCK(ncp) &&
1871 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1873 cache_unlock_vnodes_cel(cel);
1878 cache_lock_buckets_cel(cel, blps[0], blps[1]);
1882 cache_enter_unlock(struct celockstate *cel)
1885 cache_unlock_buckets_cel(cel);
1886 cache_unlock_vnodes_cel(cel);
1889 static void __noinline
1890 cache_enter_dotdot_prep(struct vnode *dvp, struct vnode *vp,
1891 struct componentname *cnp)
1893 struct celockstate cel;
1894 struct namecache *ncp;
1898 if (dvp->v_cache_dd == NULL)
1900 len = cnp->cn_namelen;
1901 cache_celockstate_init(&cel);
1902 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1903 cache_enter_lock_dd(&cel, dvp, vp, hash);
1904 vn_seqc_write_begin(dvp);
1905 ncp = dvp->v_cache_dd;
1906 if (ncp != NULL && (ncp->nc_flag & NCF_ISDOTDOT)) {
1907 KASSERT(ncp->nc_dvp == dvp, ("wrong isdotdot parent"));
1908 cache_zap_locked(ncp);
1912 dvp->v_cache_dd = NULL;
1913 vn_seqc_write_end(dvp);
1914 cache_enter_unlock(&cel);
1920 * Add an entry to the cache.
1923 cache_enter_time(struct vnode *dvp, struct vnode *vp, struct componentname *cnp,
1924 struct timespec *tsp, struct timespec *dtsp)
1926 struct celockstate cel;
1927 struct namecache *ncp, *n2, *ndd;
1928 struct namecache_ts *ncp_ts;
1929 struct nchashhead *ncpp;
1935 VNPASS(!VN_IS_DOOMED(dvp), dvp);
1936 VNPASS(dvp->v_type != VNON, dvp);
1938 VNPASS(!VN_IS_DOOMED(vp), vp);
1939 VNPASS(vp->v_type != VNON, vp);
1943 if (__predict_false(!doingcache))
1948 if (__predict_false(cnp->cn_nameptr[0] == '.')) {
1949 if (cnp->cn_namelen == 1)
1951 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
1952 cache_enter_dotdot_prep(dvp, vp, cnp);
1953 flag = NCF_ISDOTDOT;
1958 * Avoid blowout in namecache entries.
1960 lnumcache = atomic_fetchadd_long(&numcache, 1) + 1;
1961 if (__predict_false(lnumcache >= ncsize)) {
1962 atomic_add_long(&numcache, -1);
1963 counter_u64_add(numdrops, 1);
1967 cache_celockstate_init(&cel);
1972 * Calculate the hash key and setup as much of the new
1973 * namecache entry as possible before acquiring the lock.
1975 ncp = cache_alloc(cnp->cn_namelen, tsp != NULL);
1976 ncp->nc_flag = flag | NCF_WIP;
1979 cache_negative_init(ncp);
1982 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1983 ncp_ts->nc_time = *tsp;
1984 ncp_ts->nc_ticks = ticks;
1985 ncp_ts->nc_nc.nc_flag |= NCF_TS;
1987 ncp_ts->nc_dotdottime = *dtsp;
1988 ncp_ts->nc_nc.nc_flag |= NCF_DTS;
1991 len = ncp->nc_nlen = cnp->cn_namelen;
1992 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1993 memcpy(ncp->nc_name, cnp->cn_nameptr, len);
1994 ncp->nc_name[len] = '\0';
1995 cache_enter_lock(&cel, dvp, vp, hash);
1998 * See if this vnode or negative entry is already in the cache
1999 * with this name. This can happen with concurrent lookups of
2000 * the same path name.
2002 ncpp = NCHHASH(hash);
2003 CK_SLIST_FOREACH(n2, ncpp, nc_hash) {
2004 if (n2->nc_dvp == dvp &&
2005 n2->nc_nlen == cnp->cn_namelen &&
2006 !bcmp(n2->nc_name, cnp->cn_nameptr, n2->nc_nlen)) {
2007 MPASS(cache_ncp_canuse(n2));
2008 if ((n2->nc_flag & NCF_NEGATIVE) != 0)
2010 ("%s: found entry pointing to a different vnode (%p != %p)",
2011 __func__, NULL, vp));
2013 KASSERT(n2->nc_vp == vp,
2014 ("%s: found entry pointing to a different vnode (%p != %p)",
2015 __func__, n2->nc_vp, vp));
2017 * Entries are supposed to be immutable unless in the
2018 * process of getting destroyed. Accommodating for
2019 * changing timestamps is possible but not worth it.
2020 * This should be harmless in terms of correctness, in
2021 * the worst case resulting in an earlier expiration.
2022 * Alternatively, the found entry can be replaced
2025 MPASS((n2->nc_flag & (NCF_TS | NCF_DTS)) == (ncp->nc_flag & (NCF_TS | NCF_DTS)));
2028 KASSERT((n2->nc_flag & NCF_TS) != 0,
2030 n2_ts = __containerof(n2, struct namecache_ts, nc_nc);
2031 n2_ts->nc_time = ncp_ts->nc_time;
2032 n2_ts->nc_ticks = ncp_ts->nc_ticks;
2034 n2_ts->nc_dotdottime = ncp_ts->nc_dotdottime;
2035 n2_ts->nc_nc.nc_flag |= NCF_DTS;
2039 goto out_unlock_free;
2043 if (flag == NCF_ISDOTDOT) {
2045 * See if we are trying to add .. entry, but some other lookup
2046 * has populated v_cache_dd pointer already.
2048 if (dvp->v_cache_dd != NULL)
2049 goto out_unlock_free;
2050 KASSERT(vp == NULL || vp->v_type == VDIR,
2051 ("wrong vnode type %p", vp));
2052 vn_seqc_write_begin(dvp);
2053 dvp->v_cache_dd = ncp;
2054 vn_seqc_write_end(dvp);
2058 if (flag != NCF_ISDOTDOT) {
2060 * For this case, the cache entry maps both the
2061 * directory name in it and the name ".." for the
2062 * directory's parent.
2064 vn_seqc_write_begin(vp);
2065 if ((ndd = vp->v_cache_dd) != NULL) {
2066 if ((ndd->nc_flag & NCF_ISDOTDOT) != 0)
2067 cache_zap_locked(ndd);
2071 vp->v_cache_dd = ncp;
2072 vn_seqc_write_end(vp);
2073 } else if (vp->v_type != VDIR) {
2074 if (vp->v_cache_dd != NULL) {
2075 vn_seqc_write_begin(vp);
2076 vp->v_cache_dd = NULL;
2077 vn_seqc_write_end(vp);
2082 if (flag != NCF_ISDOTDOT) {
2083 if (LIST_EMPTY(&dvp->v_cache_src)) {
2085 counter_u64_add(numcachehv, 1);
2087 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
2091 * If the entry is "negative", we place it into the
2092 * "negative" cache queue, otherwise, we place it into the
2093 * destination vnode's cache entries queue.
2096 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
2097 SDT_PROBE3(vfs, namecache, enter, done, dvp, ncp->nc_name,
2100 if (cnp->cn_flags & ISWHITEOUT)
2101 ncp->nc_flag |= NCF_WHITE;
2102 cache_negative_insert(ncp);
2103 SDT_PROBE2(vfs, namecache, enter_negative, done, dvp,
2108 * Insert the new namecache entry into the appropriate chain
2109 * within the cache entries table.
2111 CK_SLIST_INSERT_HEAD(ncpp, ncp, nc_hash);
2113 atomic_thread_fence_rel();
2115 * Mark the entry as fully constructed.
2116 * It is immutable past this point until its removal.
2118 atomic_store_char(&ncp->nc_flag, ncp->nc_flag & ~NCF_WIP);
2120 cache_enter_unlock(&cel);
2121 if (numneg * ncnegfactor > lnumcache)
2122 cache_negative_zap_one();
2127 cache_enter_unlock(&cel);
2128 atomic_add_long(&numcache, -1);
2134 cache_roundup_2(u_int val)
2138 for (res = 1; res <= val; res <<= 1)
2144 static struct nchashhead *
2145 nchinittbl(u_long elements, u_long *hashmask)
2147 struct nchashhead *hashtbl;
2150 hashsize = cache_roundup_2(elements) / 2;
2152 hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), M_VFSCACHE, M_WAITOK);
2153 for (i = 0; i < hashsize; i++)
2154 CK_SLIST_INIT(&hashtbl[i]);
2155 *hashmask = hashsize - 1;
2160 ncfreetbl(struct nchashhead *hashtbl)
2163 free(hashtbl, M_VFSCACHE);
2167 * Name cache initialization, from vfs_init() when we are booting
2170 nchinit(void *dummy __unused)
2174 cache_zone_small = uma_zcreate("S VFS Cache", CACHE_ZONE_SMALL_SIZE,
2175 NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT, UMA_ZONE_ZINIT);
2176 cache_zone_small_ts = uma_zcreate("STS VFS Cache", CACHE_ZONE_SMALL_TS_SIZE,
2177 NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT, UMA_ZONE_ZINIT);
2178 cache_zone_large = uma_zcreate("L VFS Cache", CACHE_ZONE_LARGE_SIZE,
2179 NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT, UMA_ZONE_ZINIT);
2180 cache_zone_large_ts = uma_zcreate("LTS VFS Cache", CACHE_ZONE_LARGE_TS_SIZE,
2181 NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT, UMA_ZONE_ZINIT);
2183 VFS_SMR_ZONE_SET(cache_zone_small);
2184 VFS_SMR_ZONE_SET(cache_zone_small_ts);
2185 VFS_SMR_ZONE_SET(cache_zone_large);
2186 VFS_SMR_ZONE_SET(cache_zone_large_ts);
2188 ncsize = desiredvnodes * ncsizefactor;
2189 nchashtbl = nchinittbl(desiredvnodes * 2, &nchash);
2190 ncbuckethash = cache_roundup_2(mp_ncpus * mp_ncpus) - 1;
2191 if (ncbuckethash < 7) /* arbitrarily chosen to avoid having one lock */
2193 if (ncbuckethash > nchash)
2194 ncbuckethash = nchash;
2195 bucketlocks = malloc(sizeof(*bucketlocks) * numbucketlocks, M_VFSCACHE,
2197 for (i = 0; i < numbucketlocks; i++)
2198 mtx_init(&bucketlocks[i], "ncbuc", NULL, MTX_DUPOK | MTX_RECURSE);
2199 ncvnodehash = ncbuckethash;
2200 vnodelocks = malloc(sizeof(*vnodelocks) * numvnodelocks, M_VFSCACHE,
2202 for (i = 0; i < numvnodelocks; i++)
2203 mtx_init(&vnodelocks[i], "ncvn", NULL, MTX_DUPOK | MTX_RECURSE);
2204 ncpurgeminvnodes = numbucketlocks * 2;
2206 neglists = malloc(sizeof(*neglists) * numneglists, M_VFSCACHE,
2208 for (i = 0; i < numneglists; i++) {
2209 mtx_init(&neglists[i].nl_lock, "ncnegl", NULL, MTX_DEF);
2210 TAILQ_INIT(&neglists[i].nl_list);
2212 mtx_init(&ncneg_hot.nl_lock, "ncneglh", NULL, MTX_DEF);
2213 TAILQ_INIT(&ncneg_hot.nl_list);
2215 mtx_init(&ncneg_shrink_lock, "ncnegs", NULL, MTX_DEF);
2217 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL);
2220 cache_vnode_init(struct vnode *vp)
2223 LIST_INIT(&vp->v_cache_src);
2224 TAILQ_INIT(&vp->v_cache_dst);
2225 vp->v_cache_dd = NULL;
2230 cache_changesize(u_long newmaxvnodes)
2232 struct nchashhead *new_nchashtbl, *old_nchashtbl;
2233 u_long new_nchash, old_nchash;
2234 struct namecache *ncp;
2239 newncsize = newmaxvnodes * ncsizefactor;
2240 newmaxvnodes = cache_roundup_2(newmaxvnodes * 2);
2241 if (newmaxvnodes < numbucketlocks)
2242 newmaxvnodes = numbucketlocks;
2244 new_nchashtbl = nchinittbl(newmaxvnodes, &new_nchash);
2245 /* If same hash table size, nothing to do */
2246 if (nchash == new_nchash) {
2247 ncfreetbl(new_nchashtbl);
2251 * Move everything from the old hash table to the new table.
2252 * None of the namecache entries in the table can be removed
2253 * because to do so, they have to be removed from the hash table.
2255 cache_lock_all_vnodes();
2256 cache_lock_all_buckets();
2257 old_nchashtbl = nchashtbl;
2258 old_nchash = nchash;
2259 nchashtbl = new_nchashtbl;
2260 nchash = new_nchash;
2261 for (i = 0; i <= old_nchash; i++) {
2262 while ((ncp = CK_SLIST_FIRST(&old_nchashtbl[i])) != NULL) {
2263 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen,
2265 CK_SLIST_REMOVE(&old_nchashtbl[i], ncp, namecache, nc_hash);
2266 CK_SLIST_INSERT_HEAD(NCHHASH(hash), ncp, nc_hash);
2270 cache_unlock_all_buckets();
2271 cache_unlock_all_vnodes();
2272 ncfreetbl(old_nchashtbl);
2276 * Invalidate all entries from and to a particular vnode.
2279 cache_purge_impl(struct vnode *vp)
2281 TAILQ_HEAD(, namecache) ncps;
2282 struct namecache *ncp, *nnp;
2283 struct mtx *vlp, *vlp2;
2286 vlp = VP2VNODELOCK(vp);
2288 mtx_assert(vlp, MA_OWNED);
2290 while (!LIST_EMPTY(&vp->v_cache_src)) {
2291 ncp = LIST_FIRST(&vp->v_cache_src);
2292 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2294 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2296 while (!TAILQ_EMPTY(&vp->v_cache_dst)) {
2297 ncp = TAILQ_FIRST(&vp->v_cache_dst);
2298 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2300 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2302 ncp = vp->v_cache_dd;
2304 KASSERT(ncp->nc_flag & NCF_ISDOTDOT,
2305 ("lost dotdot link"));
2306 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2308 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2310 KASSERT(vp->v_cache_dd == NULL, ("incomplete purge"));
2314 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2320 cache_purge(struct vnode *vp)
2324 SDT_PROBE1(vfs, namecache, purge, done, vp);
2325 if (LIST_EMPTY(&vp->v_cache_src) && TAILQ_EMPTY(&vp->v_cache_dst) &&
2326 vp->v_cache_dd == NULL)
2328 vlp = VP2VNODELOCK(vp);
2330 cache_purge_impl(vp);
2334 * Only to be used by vgone.
2337 cache_purge_vgone(struct vnode *vp)
2341 VNPASS(VN_IS_DOOMED(vp), vp);
2342 vlp = VP2VNODELOCK(vp);
2343 if (!(LIST_EMPTY(&vp->v_cache_src) && TAILQ_EMPTY(&vp->v_cache_dst) &&
2344 vp->v_cache_dd == NULL)) {
2346 cache_purge_impl(vp);
2347 mtx_assert(vlp, MA_NOTOWNED);
2352 * All the NULL pointer state we found above may be transient.
2353 * Serialize against a possible thread doing cache_purge.
2355 mtx_wait_unlocked(vlp);
2356 if (!(LIST_EMPTY(&vp->v_cache_src) && TAILQ_EMPTY(&vp->v_cache_dst) &&
2357 vp->v_cache_dd == NULL)) {
2359 cache_purge_impl(vp);
2360 mtx_assert(vlp, MA_NOTOWNED);
2367 * Invalidate all negative entries for a particular directory vnode.
2370 cache_purge_negative(struct vnode *vp)
2372 TAILQ_HEAD(, namecache) ncps;
2373 struct namecache *ncp, *nnp;
2376 SDT_PROBE1(vfs, namecache, purge_negative, done, vp);
2377 if (LIST_EMPTY(&vp->v_cache_src))
2380 vlp = VP2VNODELOCK(vp);
2382 LIST_FOREACH_SAFE(ncp, &vp->v_cache_src, nc_src, nnp) {
2383 if (!(ncp->nc_flag & NCF_NEGATIVE))
2385 cache_zap_negative_locked_vnode_kl(ncp, vp);
2386 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2389 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2395 cache_rename(struct vnode *fdvp, struct vnode *fvp, struct vnode *tdvp,
2396 struct vnode *tvp, struct componentname *fcnp, struct componentname *tcnp)
2399 ASSERT_VOP_IN_SEQC(fdvp);
2400 ASSERT_VOP_IN_SEQC(fvp);
2401 ASSERT_VOP_IN_SEQC(tdvp);
2403 ASSERT_VOP_IN_SEQC(tvp);
2408 KASSERT(!cache_remove_cnp(tdvp, tcnp),
2409 ("%s: lingering negative entry", __func__));
2411 cache_remove_cnp(tdvp, tcnp);
2416 * Flush all entries referencing a particular filesystem.
2419 cache_purgevfs(struct mount *mp, bool force)
2421 TAILQ_HEAD(, namecache) ncps;
2422 struct mtx *vlp1, *vlp2;
2424 struct nchashhead *bucket;
2425 struct namecache *ncp, *nnp;
2426 u_long i, j, n_nchash;
2429 /* Scan hash tables for applicable entries */
2430 SDT_PROBE1(vfs, namecache, purgevfs, done, mp);
2431 if (!force && mp->mnt_nvnodelistsize <= ncpurgeminvnodes)
2434 n_nchash = nchash + 1;
2436 for (i = 0; i < numbucketlocks; i++) {
2437 blp = (struct mtx *)&bucketlocks[i];
2439 for (j = i; j < n_nchash; j += numbucketlocks) {
2441 bucket = &nchashtbl[j];
2442 CK_SLIST_FOREACH_SAFE(ncp, bucket, nc_hash, nnp) {
2443 cache_assert_bucket_locked(ncp);
2444 if (ncp->nc_dvp->v_mount != mp)
2446 error = cache_zap_locked_bucket_kl(ncp, blp,
2450 TAILQ_INSERT_HEAD(&ncps, ncp, nc_dst);
2454 if (vlp1 == NULL && vlp2 == NULL)
2455 cache_maybe_yield();
2462 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2468 * Perform canonical checks and cache lookup and pass on to filesystem
2469 * through the vop_cachedlookup only if needed.
2473 vfs_cache_lookup(struct vop_lookup_args *ap)
2477 struct vnode **vpp = ap->a_vpp;
2478 struct componentname *cnp = ap->a_cnp;
2479 int flags = cnp->cn_flags;
2484 if (dvp->v_type != VDIR)
2487 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
2488 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
2491 error = vn_dir_check_exec(dvp, cnp);
2495 error = cache_lookup(dvp, vpp, cnp, NULL, NULL);
2497 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
2503 /* Implementation of the getcwd syscall. */
2505 sys___getcwd(struct thread *td, struct __getcwd_args *uap)
2511 buflen = uap->buflen;
2512 if (__predict_false(buflen < 2))
2514 if (buflen > MAXPATHLEN)
2515 buflen = MAXPATHLEN;
2517 buf = uma_zalloc(namei_zone, M_WAITOK);
2518 error = vn_getcwd(buf, &retbuf, &buflen);
2520 error = copyout(retbuf, uap->buf, buflen);
2521 uma_zfree(namei_zone, buf);
2526 vn_getcwd(char *buf, char **retbuf, size_t *buflen)
2532 pwd = pwd_get_smr();
2533 error = vn_fullpath_any_smr(pwd->pwd_cdir, pwd->pwd_rdir, buf, retbuf,
2535 VFS_SMR_ASSERT_NOT_ENTERED();
2537 pwd = pwd_hold(curthread);
2538 error = vn_fullpath_any(pwd->pwd_cdir, pwd->pwd_rdir, buf,
2544 if (KTRPOINT(curthread, KTR_NAMEI) && error == 0)
2551 kern___realpathat(struct thread *td, int fd, const char *path, char *buf,
2552 size_t size, int flags, enum uio_seg pathseg)
2554 struct nameidata nd;
2555 char *retbuf, *freebuf;
2560 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | SAVENAME | WANTPARENT | AUDITVNODE1,
2561 pathseg, path, fd, &cap_fstat_rights, td);
2562 if ((error = namei(&nd)) != 0)
2564 error = vn_fullpath_hardlink(&nd, &retbuf, &freebuf, &size);
2566 error = copyout(retbuf, buf, size);
2567 free(freebuf, M_TEMP);
2574 sys___realpathat(struct thread *td, struct __realpathat_args *uap)
2577 return (kern___realpathat(td, uap->fd, uap->path, uap->buf, uap->size,
2578 uap->flags, UIO_USERSPACE));
2582 * Retrieve the full filesystem path that correspond to a vnode from the name
2583 * cache (if available)
2586 vn_fullpath(struct vnode *vp, char **retbuf, char **freebuf)
2593 if (__predict_false(vp == NULL))
2596 buflen = MAXPATHLEN;
2597 buf = malloc(buflen, M_TEMP, M_WAITOK);
2599 pwd = pwd_get_smr();
2600 error = vn_fullpath_any_smr(vp, pwd->pwd_rdir, buf, retbuf, &buflen, false, 0);
2601 VFS_SMR_ASSERT_NOT_ENTERED();
2603 pwd = pwd_hold(curthread);
2604 error = vn_fullpath_any(vp, pwd->pwd_rdir, buf, retbuf, &buflen);
2615 * This function is similar to vn_fullpath, but it attempts to lookup the
2616 * pathname relative to the global root mount point. This is required for the
2617 * auditing sub-system, as audited pathnames must be absolute, relative to the
2618 * global root mount point.
2621 vn_fullpath_global(struct vnode *vp, char **retbuf, char **freebuf)
2627 if (__predict_false(vp == NULL))
2629 buflen = MAXPATHLEN;
2630 buf = malloc(buflen, M_TEMP, M_WAITOK);
2632 error = vn_fullpath_any_smr(vp, rootvnode, buf, retbuf, &buflen, false, 0);
2633 VFS_SMR_ASSERT_NOT_ENTERED();
2635 error = vn_fullpath_any(vp, rootvnode, buf, retbuf, &buflen);
2644 static struct namecache *
2645 vn_dd_from_dst(struct vnode *vp)
2647 struct namecache *ncp;
2649 cache_assert_vnode_locked(vp);
2650 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst) {
2651 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2658 vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, size_t *buflen)
2661 struct namecache *ncp;
2665 vlp = VP2VNODELOCK(*vp);
2667 ncp = (*vp)->v_cache_dd;
2668 if (ncp != NULL && (ncp->nc_flag & NCF_ISDOTDOT) == 0) {
2669 KASSERT(ncp == vn_dd_from_dst(*vp),
2670 ("%s: mismatch for dd entry (%p != %p)", __func__,
2671 ncp, vn_dd_from_dst(*vp)));
2673 ncp = vn_dd_from_dst(*vp);
2676 if (*buflen < ncp->nc_nlen) {
2679 counter_u64_add(numfullpathfail4, 1);
2681 SDT_PROBE3(vfs, namecache, fullpath, return, error,
2685 *buflen -= ncp->nc_nlen;
2686 memcpy(buf + *buflen, ncp->nc_name, ncp->nc_nlen);
2687 SDT_PROBE3(vfs, namecache, fullpath, hit, ncp->nc_dvp,
2696 SDT_PROBE1(vfs, namecache, fullpath, miss, vp);
2699 vn_lock(*vp, LK_SHARED | LK_RETRY);
2700 error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen);
2703 counter_u64_add(numfullpathfail2, 1);
2704 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2709 if (VN_IS_DOOMED(dvp)) {
2710 /* forced unmount */
2713 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2717 * *vp has its use count incremented still.
2724 * Resolve a directory to a pathname.
2726 * The name of the directory can always be found in the namecache or fetched
2727 * from the filesystem. There is also guaranteed to be only one parent, meaning
2728 * we can just follow vnodes up until we find the root.
2730 * The vnode must be referenced.
2733 vn_fullpath_dir(struct vnode *vp, struct vnode *rdir, char *buf, char **retbuf,
2734 size_t *len, bool slash_prefixed, size_t addend)
2736 #ifdef KDTRACE_HOOKS
2737 struct vnode *startvp = vp;
2743 VNPASS(vp->v_type == VDIR || VN_IS_DOOMED(vp), vp);
2744 VNPASS(vp->v_usecount > 0, vp);
2748 if (!slash_prefixed) {
2756 SDT_PROBE1(vfs, namecache, fullpath, entry, vp);
2757 counter_u64_add(numfullpathcalls, 1);
2758 while (vp != rdir && vp != rootvnode) {
2760 * The vp vnode must be already fully constructed,
2761 * since it is either found in namecache or obtained
2762 * from VOP_VPTOCNP(). We may test for VV_ROOT safely
2763 * without obtaining the vnode lock.
2765 if ((vp->v_vflag & VV_ROOT) != 0) {
2766 vn_lock(vp, LK_RETRY | LK_SHARED);
2769 * With the vnode locked, check for races with
2770 * unmount, forced or not. Note that we
2771 * already verified that vp is not equal to
2772 * the root vnode, which means that
2773 * mnt_vnodecovered can be NULL only for the
2776 if (VN_IS_DOOMED(vp) ||
2777 (vp1 = vp->v_mount->mnt_vnodecovered) == NULL ||
2778 vp1->v_mountedhere != vp->v_mount) {
2781 SDT_PROBE3(vfs, namecache, fullpath, return,
2791 if (vp->v_type != VDIR) {
2793 counter_u64_add(numfullpathfail1, 1);
2795 SDT_PROBE3(vfs, namecache, fullpath, return,
2799 error = vn_vptocnp(&vp, curthread->td_ucred, buf, &buflen);
2805 SDT_PROBE3(vfs, namecache, fullpath, return, error,
2809 buf[--buflen] = '/';
2810 slash_prefixed = true;
2814 if (!slash_prefixed) {
2817 counter_u64_add(numfullpathfail4, 1);
2818 SDT_PROBE3(vfs, namecache, fullpath, return, ENOMEM,
2822 buf[--buflen] = '/';
2824 counter_u64_add(numfullpathfound, 1);
2827 *retbuf = buf + buflen;
2828 SDT_PROBE3(vfs, namecache, fullpath, return, 0, startvp, *retbuf);
2835 * Resolve an arbitrary vnode to a pathname.
2838 * - hardlinks are not tracked, thus if the vnode is not a directory this can
2839 * resolve to a different path than the one used to find it
2840 * - namecache is not mandatory, meaning names are not guaranteed to be added
2841 * (in which case resolving fails)
2843 static void __inline
2844 cache_rev_failed_impl(int *reason, int line)
2849 #define cache_rev_failed(var) cache_rev_failed_impl((var), __LINE__)
2852 vn_fullpath_any_smr(struct vnode *vp, struct vnode *rdir, char *buf,
2853 char **retbuf, size_t *buflen, bool slash_prefixed, size_t addend)
2855 #ifdef KDTRACE_HOOKS
2856 struct vnode *startvp = vp;
2860 struct namecache *ncp;
2864 #ifdef KDTRACE_HOOKS
2867 seqc_t vp_seqc, tvp_seqc;
2870 VFS_SMR_ASSERT_ENTERED();
2872 if (!cache_fast_revlookup) {
2877 orig_buflen = *buflen;
2879 if (!slash_prefixed) {
2880 MPASS(*buflen >= 2);
2882 buf[*buflen] = '\0';
2885 if (vp == rdir || vp == rootvnode) {
2886 if (!slash_prefixed) {
2893 #ifdef KDTRACE_HOOKS
2897 ncp = NULL; /* for sdt probe down below */
2898 vp_seqc = vn_seqc_read_any(vp);
2899 if (seqc_in_modify(vp_seqc)) {
2900 cache_rev_failed(&reason);
2905 #ifdef KDTRACE_HOOKS
2908 if ((vp->v_vflag & VV_ROOT) != 0) {
2909 mp = atomic_load_ptr(&vp->v_mount);
2911 cache_rev_failed(&reason);
2914 tvp = atomic_load_ptr(&mp->mnt_vnodecovered);
2915 tvp_seqc = vn_seqc_read_any(tvp);
2916 if (seqc_in_modify(tvp_seqc)) {
2917 cache_rev_failed(&reason);
2920 if (!vn_seqc_consistent(vp, vp_seqc)) {
2921 cache_rev_failed(&reason);
2928 ncp = atomic_load_ptr(&vp->v_cache_dd);
2930 cache_rev_failed(&reason);
2933 nc_flag = atomic_load_char(&ncp->nc_flag);
2934 if ((nc_flag & NCF_ISDOTDOT) != 0) {
2935 cache_rev_failed(&reason);
2938 if (!cache_ncp_canuse(ncp)) {
2939 cache_rev_failed(&reason);
2942 if (ncp->nc_nlen >= *buflen) {
2943 cache_rev_failed(&reason);
2947 *buflen -= ncp->nc_nlen;
2948 memcpy(buf + *buflen, ncp->nc_name, ncp->nc_nlen);
2952 tvp_seqc = vn_seqc_read_any(tvp);
2953 if (seqc_in_modify(tvp_seqc)) {
2954 cache_rev_failed(&reason);
2957 if (!vn_seqc_consistent(vp, vp_seqc)) {
2958 cache_rev_failed(&reason);
2963 if (vp == rdir || vp == rootvnode)
2968 *retbuf = buf + *buflen;
2969 *buflen = orig_buflen - *buflen + addend;
2970 SDT_PROBE2(vfs, namecache, fullpath_smr, hit, startvp, *retbuf);
2974 *buflen = orig_buflen;
2975 SDT_PROBE4(vfs, namecache, fullpath_smr, miss, startvp, ncp, reason, i);
2981 vn_fullpath_any(struct vnode *vp, struct vnode *rdir, char *buf, char **retbuf,
2985 bool slash_prefixed;
2991 orig_buflen = *buflen;
2994 slash_prefixed = false;
2995 if (vp->v_type != VDIR) {
2997 buf[*buflen] = '\0';
2998 error = vn_vptocnp(&vp, curthread->td_ucred, buf, buflen);
3007 slash_prefixed = true;
3010 return (vn_fullpath_dir(vp, rdir, buf, retbuf, buflen, slash_prefixed,
3011 orig_buflen - *buflen));
3015 * Resolve an arbitrary vnode to a pathname (taking care of hardlinks).
3017 * Since the namecache does not track handlings, the caller is expected to first
3018 * look up the target vnode with SAVENAME | WANTPARENT flags passed to namei.
3020 * Then we have 2 cases:
3021 * - if the found vnode is a directory, the path can be constructed just by
3022 * fullowing names up the chain
3023 * - otherwise we populate the buffer with the saved name and start resolving
3027 vn_fullpath_hardlink(struct nameidata *ndp, char **retbuf, char **freebuf,
3032 struct componentname *cnp;
3036 bool slash_prefixed;
3041 if (*buflen > MAXPATHLEN)
3042 *buflen = MAXPATHLEN;
3044 slash_prefixed = false;
3046 buf = malloc(*buflen, M_TEMP, M_WAITOK);
3051 * Check for VBAD to work around the vp_crossmp bug in lookup().
3053 * For example consider tmpfs on /tmp and realpath /tmp. ni_vp will be
3054 * set to mount point's root vnode while ni_dvp will be vp_crossmp.
3055 * If the type is VDIR (like in this very case) we can skip looking
3056 * at ni_dvp in the first place. However, since vnodes get passed here
3057 * unlocked the target may transition to doomed state (type == VBAD)
3058 * before we get to evaluate the condition. If this happens, we will
3059 * populate part of the buffer and descend to vn_fullpath_dir with
3060 * vp == vp_crossmp. Prevent the problem by checking for VBAD.
3062 * This should be atomic_load(&vp->v_type) but it is ilegal to take
3063 * an address of a bit field, even if said field is sized to char.
3064 * Work around the problem by reading the value into a full-sized enum
3065 * and then re-reading it with atomic_load which will still prevent
3066 * the compiler from re-reading down the road.
3069 type = atomic_load_int(&type);
3076 addend = cnp->cn_namelen + 2;
3077 if (*buflen < addend) {
3082 tmpbuf = buf + *buflen;
3084 memcpy(&tmpbuf[1], cnp->cn_nameptr, cnp->cn_namelen);
3085 tmpbuf[addend - 1] = '\0';
3086 slash_prefixed = true;
3091 pwd = pwd_get_smr();
3092 error = vn_fullpath_any_smr(vp, pwd->pwd_rdir, buf, retbuf, buflen,
3093 slash_prefixed, addend);
3094 VFS_SMR_ASSERT_NOT_ENTERED();
3096 pwd = pwd_hold(curthread);
3098 error = vn_fullpath_dir(vp, pwd->pwd_rdir, buf, retbuf, buflen,
3099 slash_prefixed, addend);
3114 vn_dir_dd_ino(struct vnode *vp)
3116 struct namecache *ncp;
3121 ASSERT_VOP_LOCKED(vp, "vn_dir_dd_ino");
3122 vlp = VP2VNODELOCK(vp);
3124 TAILQ_FOREACH(ncp, &(vp->v_cache_dst), nc_dst) {
3125 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0)
3128 vs = vget_prep(ddvp);
3130 if (vget_finish(ddvp, LK_SHARED | LK_NOWAIT, vs))
3139 vn_commname(struct vnode *vp, char *buf, u_int buflen)
3141 struct namecache *ncp;
3145 vlp = VP2VNODELOCK(vp);
3147 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst)
3148 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
3154 l = min(ncp->nc_nlen, buflen - 1);
3155 memcpy(buf, ncp->nc_name, l);
3162 * This function updates path string to vnode's full global path
3163 * and checks the size of the new path string against the pathlen argument.
3165 * Requires a locked, referenced vnode.
3166 * Vnode is re-locked on success or ENODEV, otherwise unlocked.
3168 * If vp is a directory, the call to vn_fullpath_global() always succeeds
3169 * because it falls back to the ".." lookup if the namecache lookup fails.
3172 vn_path_to_global_path(struct thread *td, struct vnode *vp, char *path,
3175 struct nameidata nd;
3180 ASSERT_VOP_ELOCKED(vp, __func__);
3182 /* Construct global filesystem path from vp. */
3184 error = vn_fullpath_global(vp, &rpath, &fbuf);
3191 if (strlen(rpath) >= pathlen) {
3193 error = ENAMETOOLONG;
3198 * Re-lookup the vnode by path to detect a possible rename.
3199 * As a side effect, the vnode is relocked.
3200 * If vnode was renamed, return ENOENT.
3202 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
3203 UIO_SYSSPACE, path, td);
3209 NDFREE(&nd, NDF_ONLY_PNBUF);
3213 strcpy(path, rpath);
3226 db_print_vpath(struct vnode *vp)
3229 while (vp != NULL) {
3230 db_printf("%p: ", vp);
3231 if (vp == rootvnode) {
3235 if (vp->v_vflag & VV_ROOT) {
3236 db_printf("<mount point>");
3237 vp = vp->v_mount->mnt_vnodecovered;
3239 struct namecache *ncp;
3243 ncp = TAILQ_FIRST(&vp->v_cache_dst);
3246 for (i = 0; i < ncp->nc_nlen; i++)
3247 db_printf("%c", *ncn++);
3260 DB_SHOW_COMMAND(vpath, db_show_vpath)
3265 db_printf("usage: show vpath <struct vnode *>\n");
3269 vp = (struct vnode *)addr;
3275 static bool __read_frequently cache_fast_lookup = true;
3276 SYSCTL_BOOL(_vfs, OID_AUTO, cache_fast_lookup, CTLFLAG_RW,
3277 &cache_fast_lookup, 0, "");
3279 #define CACHE_FPL_FAILED -2020
3282 cache_fpl_cleanup_cnp(struct componentname *cnp)
3285 uma_zfree(namei_zone, cnp->cn_pnbuf);
3287 cnp->cn_pnbuf = NULL;
3288 cnp->cn_nameptr = NULL;
3293 cache_fpl_handle_root(struct nameidata *ndp, struct vnode **dpp)
3295 struct componentname *cnp;
3298 while (*(cnp->cn_nameptr) == '/') {
3303 *dpp = ndp->ni_rootdir;
3307 * Components of nameidata (or objects it can point to) which may
3308 * need restoring in case fast path lookup fails.
3310 struct nameidata_saved {
3318 struct nameidata *ndp;
3319 struct componentname *cnp;
3325 struct nameidata_saved snd;
3327 enum cache_fpl_status status:8;
3332 cache_fpl_checkpoint(struct cache_fpl *fpl, struct nameidata_saved *snd)
3335 snd->cn_flags = fpl->ndp->ni_cnd.cn_flags;
3336 snd->cn_namelen = fpl->ndp->ni_cnd.cn_namelen;
3337 snd->cn_nameptr = fpl->ndp->ni_cnd.cn_nameptr;
3338 snd->ni_pathlen = fpl->ndp->ni_pathlen;
3342 cache_fpl_restore(struct cache_fpl *fpl, struct nameidata_saved *snd)
3345 fpl->ndp->ni_cnd.cn_flags = snd->cn_flags;
3346 fpl->ndp->ni_cnd.cn_namelen = snd->cn_namelen;
3347 fpl->ndp->ni_cnd.cn_nameptr = snd->cn_nameptr;
3348 fpl->ndp->ni_pathlen = snd->ni_pathlen;
3352 #define cache_fpl_smr_assert_entered(fpl) ({ \
3353 struct cache_fpl *_fpl = (fpl); \
3354 MPASS(_fpl->in_smr == true); \
3355 VFS_SMR_ASSERT_ENTERED(); \
3357 #define cache_fpl_smr_assert_not_entered(fpl) ({ \
3358 struct cache_fpl *_fpl = (fpl); \
3359 MPASS(_fpl->in_smr == false); \
3360 VFS_SMR_ASSERT_NOT_ENTERED(); \
3363 #define cache_fpl_smr_assert_entered(fpl) do { } while (0)
3364 #define cache_fpl_smr_assert_not_entered(fpl) do { } while (0)
3367 #define cache_fpl_smr_enter_initial(fpl) ({ \
3368 struct cache_fpl *_fpl = (fpl); \
3370 _fpl->in_smr = true; \
3373 #define cache_fpl_smr_enter(fpl) ({ \
3374 struct cache_fpl *_fpl = (fpl); \
3375 MPASS(_fpl->in_smr == false); \
3377 _fpl->in_smr = true; \
3380 #define cache_fpl_smr_exit(fpl) ({ \
3381 struct cache_fpl *_fpl = (fpl); \
3382 MPASS(_fpl->in_smr == true); \
3384 _fpl->in_smr = false; \
3388 cache_fpl_aborted_impl(struct cache_fpl *fpl, int line)
3391 if (fpl->status != CACHE_FPL_STATUS_UNSET) {
3392 KASSERT(fpl->status == CACHE_FPL_STATUS_PARTIAL,
3393 ("%s: converting to abort from %d at %d, set at %d\n",
3394 __func__, fpl->status, line, fpl->line));
3396 fpl->status = CACHE_FPL_STATUS_ABORTED;
3398 return (CACHE_FPL_FAILED);
3401 #define cache_fpl_aborted(x) cache_fpl_aborted_impl((x), __LINE__)
3404 cache_fpl_partial_impl(struct cache_fpl *fpl, int line)
3407 KASSERT(fpl->status == CACHE_FPL_STATUS_UNSET,
3408 ("%s: setting to partial at %d, but already set to %d at %d\n",
3409 __func__, line, fpl->status, fpl->line));
3410 cache_fpl_smr_assert_entered(fpl);
3411 fpl->status = CACHE_FPL_STATUS_PARTIAL;
3413 return (CACHE_FPL_FAILED);
3416 #define cache_fpl_partial(x) cache_fpl_partial_impl((x), __LINE__)
3419 cache_fpl_handled_impl(struct cache_fpl *fpl, int error, int line)
3422 KASSERT(fpl->status == CACHE_FPL_STATUS_UNSET,
3423 ("%s: setting to handled at %d, but already set to %d at %d\n",
3424 __func__, line, fpl->status, fpl->line));
3425 cache_fpl_smr_assert_not_entered(fpl);
3426 MPASS(error != CACHE_FPL_FAILED);
3427 fpl->status = CACHE_FPL_STATUS_HANDLED;
3432 #define cache_fpl_handled(x, e) cache_fpl_handled_impl((x), (e), __LINE__)
3434 #define CACHE_FPL_SUPPORTED_CN_FLAGS \
3435 (LOCKLEAF | LOCKPARENT | WANTPARENT | NOCACHE | FOLLOW | LOCKSHARED | SAVENAME | \
3436 SAVESTART | WILLBEDIR | ISOPEN | NOMACCHECK | AUDITVNODE1 | AUDITVNODE2 | NOCAPCHECK)
3438 #define CACHE_FPL_INTERNAL_CN_FLAGS \
3439 (ISDOTDOT | MAKEENTRY | ISLASTCN)
3441 _Static_assert((CACHE_FPL_SUPPORTED_CN_FLAGS & CACHE_FPL_INTERNAL_CN_FLAGS) == 0,
3442 "supported and internal flags overlap");
3445 cache_fpl_islastcn(struct nameidata *ndp)
3448 return (*ndp->ni_next == 0);
3452 cache_fpl_isdotdot(struct componentname *cnp)
3455 if (cnp->cn_namelen == 2 &&
3456 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
3462 cache_can_fplookup(struct cache_fpl *fpl)
3464 struct nameidata *ndp;
3465 struct componentname *cnp;
3470 td = cnp->cn_thread;
3472 if (!cache_fast_lookup) {
3473 cache_fpl_aborted(fpl);
3477 if (mac_vnode_check_lookup_enabled()) {
3478 cache_fpl_aborted(fpl);
3482 if ((cnp->cn_flags & ~CACHE_FPL_SUPPORTED_CN_FLAGS) != 0) {
3483 cache_fpl_aborted(fpl);
3486 if (ndp->ni_dirfd != AT_FDCWD) {
3487 cache_fpl_aborted(fpl);
3490 if (IN_CAPABILITY_MODE(td)) {
3491 cache_fpl_aborted(fpl);
3494 if (AUDITING_TD(td)) {
3495 cache_fpl_aborted(fpl);
3498 if (ndp->ni_startdir != NULL) {
3499 cache_fpl_aborted(fpl);
3506 cache_fplookup_vnode_supported(struct vnode *vp)
3509 return (vp->v_type != VLNK);
3513 * Move a negative entry to the hot list.
3515 * We have to take locks, but they may be contended and in the worst
3516 * case we may need to go off CPU. We don't want to spin within the
3517 * smr section and we can't block with it. Instead we are going to
3518 * look up the entry again.
3520 static int __noinline
3521 cache_fplookup_negative_promote(struct cache_fpl *fpl, struct namecache *oncp,
3524 struct componentname *cnp;
3525 struct namecache *ncp;
3526 struct neglist *neglist;
3527 struct negstate *negstate;
3534 if (!vhold_smr(dvp))
3535 return (cache_fpl_aborted(fpl));
3537 neglist = NCP2NEGLIST(oncp);
3538 cache_fpl_smr_exit(fpl);
3540 mtx_lock(&ncneg_hot.nl_lock);
3541 mtx_lock(&neglist->nl_lock);
3543 * For hash iteration.
3545 cache_fpl_smr_enter(fpl);
3548 * Avoid all surprises by only succeeding if we got the same entry and
3549 * bailing completely otherwise.
3551 * In particular at this point there can be a new ncp which matches the
3552 * search but hashes to a different neglist.
3554 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
3560 * No match to begin with.
3562 if (__predict_false(ncp == NULL)) {
3567 * The newly found entry may be something different...
3569 if (!(ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
3570 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))) {
3575 * ... and not even negative.
3577 nc_flag = atomic_load_char(&ncp->nc_flag);
3578 if ((nc_flag & NCF_NEGATIVE) == 0) {
3582 if (__predict_false(!cache_ncp_canuse(ncp))) {
3586 negstate = NCP2NEGSTATE(ncp);
3587 if ((negstate->neg_flag & NEG_HOT) == 0) {
3589 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
3590 TAILQ_INSERT_TAIL(&ncneg_hot.nl_list, ncp, nc_dst);
3591 negstate->neg_flag |= NEG_HOT;
3594 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp, ncp->nc_name);
3595 counter_u64_add(numneghits, 1);
3596 cache_fpl_smr_exit(fpl);
3597 mtx_unlock(&neglist->nl_lock);
3598 mtx_unlock(&ncneg_hot.nl_lock);
3600 return (cache_fpl_handled(fpl, ENOENT));
3602 cache_fpl_smr_exit(fpl);
3603 mtx_unlock(&neglist->nl_lock);
3604 mtx_unlock(&ncneg_hot.nl_lock);
3606 return (cache_fpl_aborted(fpl));
3610 * The target vnode is not supported, prepare for the slow path to take over.
3612 static int __noinline
3613 cache_fplookup_partial_setup(struct cache_fpl *fpl)
3615 struct nameidata *ndp;
3616 struct componentname *cnp;
3625 dvp_seqc = fpl->dvp_seqc;
3627 dvs = vget_prep_smr(dvp);
3628 if (__predict_false(dvs == VGET_NONE)) {
3629 cache_fpl_smr_exit(fpl);
3630 return (cache_fpl_aborted(fpl));
3633 cache_fpl_smr_exit(fpl);
3635 vget_finish_ref(dvp, dvs);
3636 if (!vn_seqc_consistent(dvp, dvp_seqc)) {
3638 return (cache_fpl_aborted(fpl));
3641 pwd = pwd_hold(curthread);
3642 if (fpl->pwd != pwd) {
3645 return (cache_fpl_aborted(fpl));
3648 cache_fpl_restore(fpl, &fpl->snd);
3650 ndp->ni_startdir = dvp;
3651 cnp->cn_flags |= MAKEENTRY;
3652 if (cache_fpl_islastcn(ndp))
3653 cnp->cn_flags |= ISLASTCN;
3654 if (cache_fpl_isdotdot(cnp))
3655 cnp->cn_flags |= ISDOTDOT;
3661 cache_fplookup_final_child(struct cache_fpl *fpl, enum vgetstate tvs)
3663 struct componentname *cnp;
3670 tvp_seqc = fpl->tvp_seqc;
3672 if ((cnp->cn_flags & LOCKLEAF) != 0) {
3673 lkflags = LK_SHARED;
3674 if ((cnp->cn_flags & LOCKSHARED) == 0)
3675 lkflags = LK_EXCLUSIVE;
3676 error = vget_finish(tvp, lkflags, tvs);
3677 if (__predict_false(error != 0)) {
3678 return (cache_fpl_aborted(fpl));
3681 vget_finish_ref(tvp, tvs);
3684 if (!vn_seqc_consistent(tvp, tvp_seqc)) {
3685 if ((cnp->cn_flags & LOCKLEAF) != 0)
3689 return (cache_fpl_aborted(fpl));
3692 return (cache_fpl_handled(fpl, 0));
3696 * They want to possibly modify the state of the namecache.
3698 * Don't try to match the API contract, just leave.
3699 * TODO: this leaves scalability on the table
3702 cache_fplookup_final_modifying(struct cache_fpl *fpl)
3704 struct componentname *cnp;
3707 MPASS(cnp->cn_nameiop != LOOKUP);
3708 return (cache_fpl_partial(fpl));
3711 static int __noinline
3712 cache_fplookup_final_withparent(struct cache_fpl *fpl)
3714 struct componentname *cnp;
3715 enum vgetstate dvs, tvs;
3716 struct vnode *dvp, *tvp;
3717 seqc_t dvp_seqc, tvp_seqc;
3722 dvp_seqc = fpl->dvp_seqc;
3724 tvp_seqc = fpl->tvp_seqc;
3726 MPASS((cnp->cn_flags & (LOCKPARENT|WANTPARENT)) != 0);
3729 * This is less efficient than it can be for simplicity.
3731 dvs = vget_prep_smr(dvp);
3732 if (__predict_false(dvs == VGET_NONE)) {
3733 return (cache_fpl_aborted(fpl));
3735 tvs = vget_prep_smr(tvp);
3736 if (__predict_false(tvs == VGET_NONE)) {
3737 cache_fpl_smr_exit(fpl);
3738 vget_abort(dvp, dvs);
3739 return (cache_fpl_aborted(fpl));
3742 cache_fpl_smr_exit(fpl);
3744 if ((cnp->cn_flags & LOCKPARENT) != 0) {
3745 error = vget_finish(dvp, LK_EXCLUSIVE, dvs);
3746 if (__predict_false(error != 0)) {
3747 vget_abort(tvp, tvs);
3748 return (cache_fpl_aborted(fpl));
3751 vget_finish_ref(dvp, dvs);
3754 if (!vn_seqc_consistent(dvp, dvp_seqc)) {
3755 vget_abort(tvp, tvs);
3756 if ((cnp->cn_flags & LOCKPARENT) != 0)
3760 return (cache_fpl_aborted(fpl));
3763 error = cache_fplookup_final_child(fpl, tvs);
3764 if (__predict_false(error != 0)) {
3765 MPASS(fpl->status == CACHE_FPL_STATUS_ABORTED);
3766 if ((cnp->cn_flags & LOCKPARENT) != 0)
3773 MPASS(fpl->status == CACHE_FPL_STATUS_HANDLED);
3778 cache_fplookup_final(struct cache_fpl *fpl)
3780 struct componentname *cnp;
3782 struct vnode *dvp, *tvp;
3783 seqc_t dvp_seqc, tvp_seqc;
3787 dvp_seqc = fpl->dvp_seqc;
3789 tvp_seqc = fpl->tvp_seqc;
3791 VNPASS(cache_fplookup_vnode_supported(dvp), dvp);
3793 if (cnp->cn_nameiop != LOOKUP) {
3794 return (cache_fplookup_final_modifying(fpl));
3797 if ((cnp->cn_flags & (LOCKPARENT|WANTPARENT)) != 0)
3798 return (cache_fplookup_final_withparent(fpl));
3800 tvs = vget_prep_smr(tvp);
3801 if (__predict_false(tvs == VGET_NONE)) {
3802 return (cache_fpl_partial(fpl));
3805 if (!vn_seqc_consistent(dvp, dvp_seqc)) {
3806 cache_fpl_smr_exit(fpl);
3807 vget_abort(tvp, tvs);
3808 return (cache_fpl_aborted(fpl));
3811 cache_fpl_smr_exit(fpl);
3812 return (cache_fplookup_final_child(fpl, tvs));
3815 static int __noinline
3816 cache_fplookup_dot(struct cache_fpl *fpl)
3823 fpl->tvp_seqc = vn_seqc_read_any(dvp);
3824 if (seqc_in_modify(fpl->tvp_seqc)) {
3825 return (cache_fpl_aborted(fpl));
3828 counter_u64_add(dothits, 1);
3829 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", dvp);
3834 static int __noinline
3835 cache_fplookup_dotdot(struct cache_fpl *fpl)
3837 struct nameidata *ndp;
3838 struct componentname *cnp;
3839 struct namecache *ncp;
3849 * XXX this is racy the same way regular lookup is
3851 for (pr = cnp->cn_cred->cr_prison; pr != NULL;
3853 if (dvp == pr->pr_root)
3856 if (dvp == ndp->ni_rootdir ||
3857 dvp == ndp->ni_topdir ||
3861 fpl->tvp_seqc = vn_seqc_read_any(dvp);
3862 if (seqc_in_modify(fpl->tvp_seqc)) {
3863 return (cache_fpl_aborted(fpl));
3868 if ((dvp->v_vflag & VV_ROOT) != 0) {
3871 * The opposite of climb mount is needed here.
3873 return (cache_fpl_aborted(fpl));
3876 ncp = atomic_load_ptr(&dvp->v_cache_dd);
3878 return (cache_fpl_aborted(fpl));
3881 nc_flag = atomic_load_char(&ncp->nc_flag);
3882 if ((nc_flag & NCF_ISDOTDOT) != 0) {
3883 if ((nc_flag & NCF_NEGATIVE) != 0)
3884 return (cache_fpl_aborted(fpl));
3885 fpl->tvp = ncp->nc_vp;
3887 fpl->tvp = ncp->nc_dvp;
3890 if (__predict_false(!cache_ncp_canuse(ncp))) {
3891 return (cache_fpl_aborted(fpl));
3894 fpl->tvp_seqc = vn_seqc_read_any(fpl->tvp);
3895 if (seqc_in_modify(fpl->tvp_seqc)) {
3896 return (cache_fpl_partial(fpl));
3899 counter_u64_add(dotdothits, 1);
3904 cache_fplookup_next(struct cache_fpl *fpl)
3906 struct componentname *cnp;
3907 struct namecache *ncp;
3908 struct negstate *negstate;
3909 struct vnode *dvp, *tvp;
3917 if (__predict_false(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.')) {
3918 return (cache_fplookup_dot(fpl));
3921 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
3923 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
3924 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
3925 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
3930 * If there is no entry we have to punt to the slow path to perform
3931 * actual lookup. Should there be nothing with this name a negative
3932 * entry will be created.
3934 if (__predict_false(ncp == NULL)) {
3935 return (cache_fpl_partial(fpl));
3938 tvp = atomic_load_ptr(&ncp->nc_vp);
3939 nc_flag = atomic_load_char(&ncp->nc_flag);
3940 if ((nc_flag & NCF_NEGATIVE) != 0) {
3942 * If they want to create an entry we need to replace this one.
3944 if (__predict_false(fpl->cnp->cn_nameiop != LOOKUP)) {
3945 return (cache_fpl_partial(fpl));
3947 negstate = NCP2NEGSTATE(ncp);
3948 neg_hot = ((negstate->neg_flag & NEG_HOT) != 0);
3949 if (__predict_false(!cache_ncp_canuse(ncp))) {
3950 return (cache_fpl_partial(fpl));
3952 if (__predict_false((nc_flag & NCF_WHITE) != 0)) {
3953 return (cache_fpl_partial(fpl));
3956 return (cache_fplookup_negative_promote(fpl, ncp, hash));
3958 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp,
3960 counter_u64_add(numneghits, 1);
3961 cache_fpl_smr_exit(fpl);
3962 return (cache_fpl_handled(fpl, ENOENT));
3965 if (__predict_false(!cache_ncp_canuse(ncp))) {
3966 return (cache_fpl_partial(fpl));
3970 fpl->tvp_seqc = vn_seqc_read_any(tvp);
3971 if (seqc_in_modify(fpl->tvp_seqc)) {
3972 return (cache_fpl_partial(fpl));
3975 if (!cache_fplookup_vnode_supported(tvp)) {
3976 return (cache_fpl_partial(fpl));
3979 counter_u64_add(numposhits, 1);
3980 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name, tvp);
3985 cache_fplookup_mp_supported(struct mount *mp)
3990 if ((mp->mnt_kern_flag & MNTK_FPLOOKUP) == 0)
3996 * Walk up the mount stack (if any).
3998 * Correctness is provided in the following ways:
3999 * - all vnodes are protected from freeing with SMR
4000 * - struct mount objects are type stable making them always safe to access
4001 * - stability of the particular mount is provided by busying it
4002 * - relationship between the vnode which is mounted on and the mount is
4003 * verified with the vnode sequence counter after busying
4004 * - association between root vnode of the mount and the mount is protected
4007 * From that point on we can read the sequence counter of the root vnode
4008 * and get the next mount on the stack (if any) using the same protection.
4010 * By the end of successful walk we are guaranteed the reached state was
4011 * indeed present at least at some point which matches the regular lookup.
4013 static int __noinline
4014 cache_fplookup_climb_mount(struct cache_fpl *fpl)
4016 struct mount *mp, *prev_mp;
4021 vp_seqc = fpl->tvp_seqc;
4023 VNPASS(vp->v_type == VDIR || vp->v_type == VBAD, vp);
4024 mp = atomic_load_ptr(&vp->v_mountedhere);
4030 if (!vfs_op_thread_enter_crit(mp)) {
4031 if (prev_mp != NULL)
4032 vfs_op_thread_exit_crit(prev_mp);
4033 return (cache_fpl_partial(fpl));
4035 if (prev_mp != NULL)
4036 vfs_op_thread_exit_crit(prev_mp);
4037 if (!vn_seqc_consistent(vp, vp_seqc)) {
4038 vfs_op_thread_exit_crit(mp);
4039 return (cache_fpl_partial(fpl));
4041 if (!cache_fplookup_mp_supported(mp)) {
4042 vfs_op_thread_exit_crit(mp);
4043 return (cache_fpl_partial(fpl));
4045 vp = atomic_load_ptr(&mp->mnt_rootvnode);
4046 if (vp == NULL || VN_IS_DOOMED(vp)) {
4047 vfs_op_thread_exit_crit(mp);
4048 return (cache_fpl_partial(fpl));
4050 vp_seqc = vn_seqc_read_any(vp);
4051 if (seqc_in_modify(vp_seqc)) {
4052 vfs_op_thread_exit_crit(mp);
4053 return (cache_fpl_partial(fpl));
4056 mp = atomic_load_ptr(&vp->v_mountedhere);
4061 vfs_op_thread_exit_crit(prev_mp);
4063 fpl->tvp_seqc = vp_seqc;
4068 cache_fplookup_need_climb_mount(struct cache_fpl *fpl)
4076 * Hack: while this is a union, the pointer tends to be NULL so save on
4079 mp = atomic_load_ptr(&vp->v_mountedhere);
4082 if (vp->v_type == VDIR)
4090 * The code is mostly copy-pasted from regular lookup, see lookup().
4091 * The structure is maintained along with comments for easier maintenance.
4092 * Deduplicating the code will become feasible after fast path lookup
4093 * becomes more feature-complete.
4096 cache_fplookup_parse(struct cache_fpl *fpl)
4098 struct nameidata *ndp;
4099 struct componentname *cnp;
4106 * Search a new directory.
4108 * The last component of the filename is left accessible via
4109 * cnp->cn_nameptr for callers that need the name. Callers needing
4110 * the name set the SAVENAME flag. When done, they assume
4111 * responsibility for freeing the pathname buffer.
4113 for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
4115 cnp->cn_namelen = cp - cnp->cn_nameptr;
4116 if (__predict_false(cnp->cn_namelen > NAME_MAX)) {
4117 cache_fpl_smr_exit(fpl);
4118 return (cache_fpl_handled(fpl, ENAMETOOLONG));
4120 ndp->ni_pathlen -= cnp->cn_namelen;
4121 KASSERT(ndp->ni_pathlen <= PATH_MAX,
4122 ("%s: ni_pathlen underflow to %zd\n", __func__, ndp->ni_pathlen));
4126 * Replace multiple slashes by a single slash and trailing slashes
4127 * by a null. This must be done before VOP_LOOKUP() because some
4128 * fs's don't know about trailing slashes. Remember if there were
4129 * trailing slashes to handle symlinks, existing non-directories
4130 * and non-existing files that won't be directories specially later.
4132 while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
4138 * Regular lookup performs the following:
4139 * *ndp->ni_next = '\0';
4140 * cnp->cn_flags |= TRAILINGSLASH;
4142 * Which is problematic since it modifies data read
4143 * from userspace. Then if fast path lookup was to
4144 * abort we would have to either restore it or convey
4145 * the flag. Since this is a corner case just ignore
4146 * it for simplicity.
4148 return (cache_fpl_partial(fpl));
4154 * Check for degenerate name (e.g. / or "")
4155 * which is a way of talking about a directory,
4156 * e.g. like "/." or ".".
4159 * Another corner case handled by the regular lookup
4161 if (__predict_false(cnp->cn_nameptr[0] == '\0')) {
4162 return (cache_fpl_partial(fpl));
4168 cache_fplookup_parse_advance(struct cache_fpl *fpl)
4170 struct nameidata *ndp;
4171 struct componentname *cnp;
4176 cnp->cn_nameptr = ndp->ni_next;
4177 while (*cnp->cn_nameptr == '/') {
4183 static int __noinline
4184 cache_fplookup_failed_vexec(struct cache_fpl *fpl, int error)
4190 * Can happen when racing against vgone.
4193 cache_fpl_partial(fpl);
4197 * See the API contract for VOP_FPLOOKUP_VEXEC.
4199 if (!vn_seqc_consistent(fpl->dvp, fpl->dvp_seqc)) {
4200 error = cache_fpl_aborted(fpl);
4202 cache_fpl_smr_exit(fpl);
4203 cache_fpl_handled(fpl, error);
4211 cache_fplookup_impl(struct vnode *dvp, struct cache_fpl *fpl)
4213 struct nameidata *ndp;
4214 struct componentname *cnp;
4218 error = CACHE_FPL_FAILED;
4222 cache_fpl_checkpoint(fpl, &fpl->snd);
4225 fpl->dvp_seqc = vn_seqc_read_any(fpl->dvp);
4226 if (seqc_in_modify(fpl->dvp_seqc)) {
4227 cache_fpl_aborted(fpl);
4230 mp = atomic_load_ptr(&fpl->dvp->v_mount);
4231 if (!cache_fplookup_mp_supported(mp)) {
4232 cache_fpl_aborted(fpl);
4236 VNPASS(cache_fplookup_vnode_supported(fpl->dvp), fpl->dvp);
4239 error = cache_fplookup_parse(fpl);
4240 if (__predict_false(error != 0)) {
4244 VNPASS(cache_fplookup_vnode_supported(fpl->dvp), fpl->dvp);
4246 error = VOP_FPLOOKUP_VEXEC(fpl->dvp, cnp->cn_cred);
4247 if (__predict_false(error != 0)) {
4248 error = cache_fplookup_failed_vexec(fpl, error);
4252 if (__predict_false(cache_fpl_isdotdot(cnp))) {
4253 error = cache_fplookup_dotdot(fpl);
4254 if (__predict_false(error != 0)) {
4258 error = cache_fplookup_next(fpl);
4259 if (__predict_false(error != 0)) {
4263 VNPASS(!seqc_in_modify(fpl->tvp_seqc), fpl->tvp);
4265 if (cache_fplookup_need_climb_mount(fpl)) {
4266 error = cache_fplookup_climb_mount(fpl);
4267 if (__predict_false(error != 0)) {
4273 VNPASS(!seqc_in_modify(fpl->tvp_seqc), fpl->tvp);
4275 if (cache_fpl_islastcn(ndp)) {
4276 error = cache_fplookup_final(fpl);
4280 if (!vn_seqc_consistent(fpl->dvp, fpl->dvp_seqc)) {
4281 error = cache_fpl_aborted(fpl);
4285 fpl->dvp = fpl->tvp;
4286 fpl->dvp_seqc = fpl->tvp_seqc;
4288 cache_fplookup_parse_advance(fpl);
4289 cache_fpl_checkpoint(fpl, &fpl->snd);
4292 switch (fpl->status) {
4293 case CACHE_FPL_STATUS_UNSET:
4294 __assert_unreachable();
4296 case CACHE_FPL_STATUS_PARTIAL:
4297 cache_fpl_smr_assert_entered(fpl);
4298 return (cache_fplookup_partial_setup(fpl));
4299 case CACHE_FPL_STATUS_ABORTED:
4301 cache_fpl_smr_exit(fpl);
4302 return (CACHE_FPL_FAILED);
4303 case CACHE_FPL_STATUS_HANDLED:
4304 MPASS(error != CACHE_FPL_FAILED);
4305 cache_fpl_smr_assert_not_entered(fpl);
4306 if (__predict_false(error != 0)) {
4309 cache_fpl_cleanup_cnp(cnp);
4312 ndp->ni_dvp = fpl->dvp;
4313 ndp->ni_vp = fpl->tvp;
4314 if (cnp->cn_flags & SAVENAME)
4315 cnp->cn_flags |= HASBUF;
4317 cache_fpl_cleanup_cnp(cnp);
4323 * Fast path lookup protected with SMR and sequence counters.
4325 * Note: all VOP_FPLOOKUP_VEXEC routines have a comment referencing this one.
4327 * Filesystems can opt in by setting the MNTK_FPLOOKUP flag and meeting criteria
4330 * Traditional vnode lookup conceptually looks like this:
4336 * vn_unlock(current);
4343 * Each jump to the next vnode is safe memory-wise and atomic with respect to
4344 * any modifications thanks to holding respective locks.
4346 * The same guarantee can be provided with a combination of safe memory
4347 * reclamation and sequence counters instead. If all operations which affect
4348 * the relationship between the current vnode and the one we are looking for
4349 * also modify the counter, we can verify whether all the conditions held as
4350 * we made the jump. This includes things like permissions, mount points etc.
4351 * Counter modification is provided by enclosing relevant places in
4352 * vn_seqc_write_begin()/end() calls.
4354 * Thus this translates to:
4357 * dvp_seqc = seqc_read_any(dvp);
4358 * if (seqc_in_modify(dvp_seqc)) // someone is altering the vnode
4362 * tvp_seqc = seqc_read_any(tvp);
4363 * if (seqc_in_modify(tvp_seqc)) // someone is altering the target vnode
4365 * if (!seqc_consistent(dvp, dvp_seqc) // someone is altering the vnode
4367 * dvp = tvp; // we know nothing of importance has changed
4368 * dvp_seqc = tvp_seqc; // store the counter for the tvp iteration
4372 * vget(); // secure the vnode
4373 * if (!seqc_consistent(tvp, tvp_seqc) // final check
4375 * // at this point we know nothing has changed for any parent<->child pair
4376 * // as they were crossed during the lookup, meaning we matched the guarantee
4377 * // of the locked variant
4380 * The API contract for VOP_FPLOOKUP_VEXEC routines is as follows:
4381 * - they are called while within vfs_smr protection which they must never exit
4382 * - EAGAIN can be returned to denote checking could not be performed, it is
4383 * always valid to return it
4384 * - if the sequence counter has not changed the result must be valid
4385 * - if the sequence counter has changed both false positives and false negatives
4386 * are permitted (since the result will be rejected later)
4387 * - for simple cases of unix permission checks vaccess_vexec_smr can be used
4389 * Caveats to watch out for:
4390 * - vnodes are passed unlocked and unreferenced with nothing stopping
4391 * VOP_RECLAIM, in turn meaning that ->v_data can become NULL. It is advised
4392 * to use atomic_load_ptr to fetch it.
4393 * - the aforementioned object can also get freed, meaning absent other means it
4394 * should be protected with vfs_smr
4395 * - either safely checking permissions as they are modified or guaranteeing
4396 * their stability is left to the routine
4399 cache_fplookup(struct nameidata *ndp, enum cache_fpl_status *status,
4402 struct cache_fpl fpl;
4405 struct componentname *cnp;
4406 struct nameidata_saved orig;
4409 MPASS(ndp->ni_lcf == 0);
4411 fpl.status = CACHE_FPL_STATUS_UNSET;
4413 fpl.cnp = &ndp->ni_cnd;
4414 MPASS(curthread == fpl.cnp->cn_thread);
4416 if ((fpl.cnp->cn_flags & SAVESTART) != 0)
4417 MPASS(fpl.cnp->cn_nameiop != LOOKUP);
4419 if (!cache_can_fplookup(&fpl)) {
4420 SDT_PROBE3(vfs, fplookup, lookup, done, ndp, fpl.line, fpl.status);
4421 *status = fpl.status;
4422 return (EOPNOTSUPP);
4425 cache_fpl_checkpoint(&fpl, &orig);
4427 cache_fpl_smr_enter_initial(&fpl);
4428 pwd = pwd_get_smr();
4430 ndp->ni_rootdir = pwd->pwd_rdir;
4431 ndp->ni_topdir = pwd->pwd_jdir;
4434 cnp->cn_nameptr = cnp->cn_pnbuf;
4435 if (cnp->cn_pnbuf[0] == '/') {
4436 cache_fpl_handle_root(ndp, &dvp);
4438 MPASS(ndp->ni_dirfd == AT_FDCWD);
4439 dvp = pwd->pwd_cdir;
4442 SDT_PROBE4(vfs, namei, lookup, entry, dvp, cnp->cn_pnbuf, cnp->cn_flags, true);
4444 error = cache_fplookup_impl(dvp, &fpl);
4445 cache_fpl_smr_assert_not_entered(&fpl);
4446 SDT_PROBE3(vfs, fplookup, lookup, done, ndp, fpl.line, fpl.status);
4448 *status = fpl.status;
4449 switch (fpl.status) {
4450 case CACHE_FPL_STATUS_UNSET:
4451 __assert_unreachable();
4453 case CACHE_FPL_STATUS_HANDLED:
4454 SDT_PROBE3(vfs, namei, lookup, return, error,
4455 (error == 0 ? ndp->ni_vp : NULL), true);
4457 case CACHE_FPL_STATUS_PARTIAL:
4460 * Status restored by cache_fplookup_partial_setup.
4463 case CACHE_FPL_STATUS_ABORTED:
4464 cache_fpl_restore(&fpl, &orig);