2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993, 1995
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Poul-Henning Kamp of the FreeBSD Project.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)vfs_cache.c 8.5 (Berkeley) 3/22/95
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
41 #include "opt_ktrace.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/capsicum.h>
46 #include <sys/counter.h>
47 #include <sys/filedesc.h>
48 #include <sys/fnv_hash.h>
49 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/fcntl.h>
55 #include <sys/mount.h>
56 #include <sys/namei.h>
62 #include <sys/syscallsubr.h>
63 #include <sys/sysctl.h>
64 #include <sys/sysproto.h>
65 #include <sys/vnode.h>
68 #include <sys/ktrace.h>
71 #include <sys/capsicum.h>
73 #include <security/audit/audit.h>
74 #include <security/mac/mac_framework.h>
82 SDT_PROVIDER_DECLARE(vfs);
83 SDT_PROBE_DEFINE3(vfs, namecache, enter, done, "struct vnode *", "char *",
85 SDT_PROBE_DEFINE2(vfs, namecache, enter_negative, done, "struct vnode *",
87 SDT_PROBE_DEFINE2(vfs, namecache, fullpath_smr, hit, "struct vnode *",
89 SDT_PROBE_DEFINE4(vfs, namecache, fullpath_smr, miss, "struct vnode *",
90 "struct namecache *", "int", "int");
91 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, entry, "struct vnode *");
92 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, hit, "struct vnode *",
93 "char *", "struct vnode *");
94 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, miss, "struct vnode *");
95 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, return, "int",
96 "struct vnode *", "char *");
97 SDT_PROBE_DEFINE3(vfs, namecache, lookup, hit, "struct vnode *", "char *",
99 SDT_PROBE_DEFINE2(vfs, namecache, lookup, hit__negative,
100 "struct vnode *", "char *");
101 SDT_PROBE_DEFINE2(vfs, namecache, lookup, miss, "struct vnode *",
103 SDT_PROBE_DEFINE2(vfs, namecache, removecnp, hit, "struct vnode *",
104 "struct componentname *");
105 SDT_PROBE_DEFINE2(vfs, namecache, removecnp, miss, "struct vnode *",
106 "struct componentname *");
107 SDT_PROBE_DEFINE1(vfs, namecache, purge, done, "struct vnode *");
108 SDT_PROBE_DEFINE1(vfs, namecache, purge_negative, done, "struct vnode *");
109 SDT_PROBE_DEFINE1(vfs, namecache, purgevfs, done, "struct mount *");
110 SDT_PROBE_DEFINE3(vfs, namecache, zap, done, "struct vnode *", "char *",
112 SDT_PROBE_DEFINE2(vfs, namecache, zap_negative, done, "struct vnode *",
114 SDT_PROBE_DEFINE2(vfs, namecache, shrink_negative, done, "struct vnode *",
117 SDT_PROBE_DEFINE3(vfs, fplookup, lookup, done, "struct nameidata", "int", "bool");
118 SDT_PROBE_DECLARE(vfs, namei, lookup, entry);
119 SDT_PROBE_DECLARE(vfs, namei, lookup, return);
122 * This structure describes the elements in the cache of recent
123 * names looked up by namei.
128 _Static_assert(sizeof(struct negstate) <= sizeof(struct vnode *),
129 "the state must fit in a union with a pointer without growing it");
132 LIST_ENTRY(namecache) nc_src; /* source vnode list */
133 TAILQ_ENTRY(namecache) nc_dst; /* destination vnode list */
134 CK_SLIST_ENTRY(namecache) nc_hash;/* hash chain */
135 struct vnode *nc_dvp; /* vnode of parent of name */
137 struct vnode *nu_vp; /* vnode the name refers to */
138 struct negstate nu_neg;/* negative entry state */
140 u_char nc_flag; /* flag bits */
141 u_char nc_nlen; /* length of name */
142 char nc_name[0]; /* segment name + nul */
146 * struct namecache_ts repeats struct namecache layout up to the
148 * struct namecache_ts is used in place of struct namecache when time(s) need
149 * to be stored. The nc_dotdottime field is used when a cache entry is mapping
150 * both a non-dotdot directory name plus dotdot for the directory's
153 * See below for alignment requirement.
155 struct namecache_ts {
156 struct timespec nc_time; /* timespec provided by fs */
157 struct timespec nc_dotdottime; /* dotdot timespec provided by fs */
158 int nc_ticks; /* ticks value when entry was added */
159 struct namecache nc_nc;
163 * At least mips n32 performs 64-bit accesses to timespec as found
164 * in namecache_ts and requires them to be aligned. Since others
165 * may be in the same spot suffer a little bit and enforce the
166 * alignment for everyone. Note this is a nop for 64-bit platforms.
168 #define CACHE_ZONE_ALIGNMENT UMA_ALIGNOF(time_t)
169 #define CACHE_PATH_CUTOFF 39
171 #define CACHE_ZONE_SMALL_SIZE (sizeof(struct namecache) + CACHE_PATH_CUTOFF + 1)
172 #define CACHE_ZONE_SMALL_TS_SIZE (sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1)
173 #define CACHE_ZONE_LARGE_SIZE (sizeof(struct namecache) + NAME_MAX + 1)
174 #define CACHE_ZONE_LARGE_TS_SIZE (sizeof(struct namecache_ts) + NAME_MAX + 1)
176 _Static_assert((CACHE_ZONE_SMALL_SIZE % (CACHE_ZONE_ALIGNMENT + 1)) == 0, "bad zone size");
177 _Static_assert((CACHE_ZONE_SMALL_TS_SIZE % (CACHE_ZONE_ALIGNMENT + 1)) == 0, "bad zone size");
178 _Static_assert((CACHE_ZONE_LARGE_SIZE % (CACHE_ZONE_ALIGNMENT + 1)) == 0, "bad zone size");
179 _Static_assert((CACHE_ZONE_LARGE_TS_SIZE % (CACHE_ZONE_ALIGNMENT + 1)) == 0, "bad zone size");
181 #define nc_vp n_un.nu_vp
182 #define nc_neg n_un.nu_neg
185 * Flags in namecache.nc_flag
187 #define NCF_WHITE 0x01
188 #define NCF_ISDOTDOT 0x02
191 #define NCF_DVDROP 0x10
192 #define NCF_NEGATIVE 0x20
193 #define NCF_INVALID 0x40
197 * Flags in negstate.neg_flag
202 * Mark an entry as invalid.
204 * This is called before it starts getting deconstructed.
207 cache_ncp_invalidate(struct namecache *ncp)
210 KASSERT((ncp->nc_flag & NCF_INVALID) == 0,
211 ("%s: entry %p already invalid", __func__, ncp));
212 atomic_store_char(&ncp->nc_flag, ncp->nc_flag | NCF_INVALID);
213 atomic_thread_fence_rel();
217 * Check whether the entry can be safely used.
219 * All places which elide locks are supposed to call this after they are
220 * done with reading from an entry.
223 cache_ncp_canuse(struct namecache *ncp)
226 atomic_thread_fence_acq();
227 return ((atomic_load_char(&ncp->nc_flag) & (NCF_INVALID | NCF_WIP)) == 0);
231 * Name caching works as follows:
233 * Names found by directory scans are retained in a cache
234 * for future reference. It is managed LRU, so frequently
235 * used names will hang around. Cache is indexed by hash value
236 * obtained from (dvp, name) where dvp refers to the directory
239 * If it is a "negative" entry, (i.e. for a name that is known NOT to
240 * exist) the vnode pointer will be NULL.
242 * Upon reaching the last segment of a path, if the reference
243 * is for DELETE, or NOCACHE is set (rewrite), and the
244 * name is located in the cache, it will be dropped.
246 * These locks are used (in the order in which they can be taken):
248 * vnodelock mtx vnode lists and v_cache_dd field protection
249 * bucketlock mtx for access to given set of hash buckets
250 * neglist mtx negative entry LRU management
252 * Additionally, ncneg_shrink_lock mtx is used to have at most one thread
253 * shrinking the LRU list.
255 * It is legal to take multiple vnodelock and bucketlock locks. The locking
256 * order is lower address first. Both are recursive.
258 * "." lookups are lockless.
260 * ".." and vnode -> name lookups require vnodelock.
262 * name -> vnode lookup requires the relevant bucketlock to be held for reading.
264 * Insertions and removals of entries require involved vnodes and bucketlocks
265 * to be locked to provide safe operation against other threads modifying the
268 * Some lookups result in removal of the found entry (e.g. getting rid of a
269 * negative entry with the intent to create a positive one), which poses a
270 * problem when multiple threads reach the state. Similarly, two different
271 * threads can purge two different vnodes and try to remove the same name.
273 * If the already held vnode lock is lower than the second required lock, we
274 * can just take the other lock. However, in the opposite case, this could
275 * deadlock. As such, this is resolved by trylocking and if that fails unlocking
276 * the first node, locking everything in order and revalidating the state.
282 * Structures associated with name caching.
284 #define NCHHASH(hash) \
285 (&nchashtbl[(hash) & nchash])
286 static __read_mostly CK_SLIST_HEAD(nchashhead, namecache) *nchashtbl;/* Hash Table */
287 static u_long __read_mostly nchash; /* size of hash table */
288 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0,
289 "Size of namecache hash table");
290 static u_long __read_mostly ncnegfactor = 5; /* ratio of negative entries */
291 SYSCTL_ULONG(_vfs, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0,
292 "Ratio of negative namecache entries");
293 static u_long __exclusive_cache_line numneg; /* number of negative entries allocated */
294 static u_long __exclusive_cache_line numcache;/* number of cache entries allocated */
295 u_int ncsizefactor = 2;
296 SYSCTL_UINT(_vfs, OID_AUTO, ncsizefactor, CTLFLAG_RW, &ncsizefactor, 0,
297 "Size factor for namecache");
298 static u_int __read_mostly ncsize; /* the size as computed on creation or resizing */
300 struct nchstats nchstats; /* cache effectiveness statistics */
302 static bool __read_frequently cache_fast_revlookup = true;
303 SYSCTL_BOOL(_vfs, OID_AUTO, cache_fast_revlookup, CTLFLAG_RW,
304 &cache_fast_revlookup, 0, "");
306 static struct mtx __exclusive_cache_line ncneg_shrink_lock;
309 #define numneglists (ncneghash + 1)
313 TAILQ_HEAD(, namecache) nl_list;
314 } __aligned(CACHE_LINE_SIZE);
316 static struct neglist neglists[numneglists];
317 static struct neglist ncneg_hot;
318 static u_long numhotneg;
320 static inline struct neglist *
321 NCP2NEGLIST(struct namecache *ncp)
324 return (&neglists[(((uintptr_t)(ncp) >> 8) & ncneghash)]);
327 static inline struct negstate *
328 NCP2NEGSTATE(struct namecache *ncp)
331 MPASS(ncp->nc_flag & NCF_NEGATIVE);
332 return (&ncp->nc_neg);
335 #define numbucketlocks (ncbuckethash + 1)
336 static u_int __read_mostly ncbuckethash;
337 static struct mtx_padalign __read_mostly *bucketlocks;
338 #define HASH2BUCKETLOCK(hash) \
339 ((struct mtx *)(&bucketlocks[((hash) & ncbuckethash)]))
341 #define numvnodelocks (ncvnodehash + 1)
342 static u_int __read_mostly ncvnodehash;
343 static struct mtx __read_mostly *vnodelocks;
344 static inline struct mtx *
345 VP2VNODELOCK(struct vnode *vp)
348 return (&vnodelocks[(((uintptr_t)(vp) >> 8) & ncvnodehash)]);
352 * UMA zones for the VFS cache.
354 * The small cache is used for entries with short names, which are the
355 * most common. The large cache is used for entries which are too big to
356 * fit in the small cache.
358 static uma_zone_t __read_mostly cache_zone_small;
359 static uma_zone_t __read_mostly cache_zone_small_ts;
360 static uma_zone_t __read_mostly cache_zone_large;
361 static uma_zone_t __read_mostly cache_zone_large_ts;
363 static struct namecache *
364 cache_alloc(int len, int ts)
366 struct namecache_ts *ncp_ts;
367 struct namecache *ncp;
369 if (__predict_false(ts)) {
370 if (len <= CACHE_PATH_CUTOFF)
371 ncp_ts = uma_zalloc_smr(cache_zone_small_ts, M_WAITOK);
373 ncp_ts = uma_zalloc_smr(cache_zone_large_ts, M_WAITOK);
374 ncp = &ncp_ts->nc_nc;
376 if (len <= CACHE_PATH_CUTOFF)
377 ncp = uma_zalloc_smr(cache_zone_small, M_WAITOK);
379 ncp = uma_zalloc_smr(cache_zone_large, M_WAITOK);
385 cache_free(struct namecache *ncp)
387 struct namecache_ts *ncp_ts;
390 if ((ncp->nc_flag & NCF_DVDROP) != 0)
392 if (__predict_false(ncp->nc_flag & NCF_TS)) {
393 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
394 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
395 uma_zfree_smr(cache_zone_small_ts, ncp_ts);
397 uma_zfree_smr(cache_zone_large_ts, ncp_ts);
399 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
400 uma_zfree_smr(cache_zone_small, ncp);
402 uma_zfree_smr(cache_zone_large, ncp);
407 cache_out_ts(struct namecache *ncp, struct timespec *tsp, int *ticksp)
409 struct namecache_ts *ncp_ts;
411 KASSERT((ncp->nc_flag & NCF_TS) != 0 ||
412 (tsp == NULL && ticksp == NULL),
418 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
419 *tsp = ncp_ts->nc_time;
420 *ticksp = ncp_ts->nc_ticks;
424 static int __read_mostly doingcache = 1; /* 1 => enable the cache */
425 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0,
426 "VFS namecache enabled");
429 /* Export size information to userland */
430 SYSCTL_INT(_debug_sizeof, OID_AUTO, namecache, CTLFLAG_RD, SYSCTL_NULL_INT_PTR,
431 sizeof(struct namecache), "sizeof(struct namecache)");
434 * The new name cache statistics
436 static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
437 "Name cache statistics");
438 #define STATNODE_ULONG(name, descr) \
439 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, descr);
440 #define STATNODE_COUNTER(name, descr) \
441 static COUNTER_U64_DEFINE_EARLY(name); \
442 SYSCTL_COUNTER_U64(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, \
444 STATNODE_ULONG(numneg, "Number of negative cache entries");
445 STATNODE_ULONG(numcache, "Number of cache entries");
446 STATNODE_COUNTER(numcachehv, "Number of namecache entries with vnodes held");
447 STATNODE_COUNTER(numdrops, "Number of dropped entries due to reaching the limit");
448 STATNODE_COUNTER(dothits, "Number of '.' hits");
449 STATNODE_COUNTER(dotdothits, "Number of '..' hits");
450 STATNODE_COUNTER(nummiss, "Number of cache misses");
451 STATNODE_COUNTER(nummisszap, "Number of cache misses we do not want to cache");
452 STATNODE_COUNTER(numposzaps,
453 "Number of cache hits (positive) we do not want to cache");
454 STATNODE_COUNTER(numposhits, "Number of cache hits (positive)");
455 STATNODE_COUNTER(numnegzaps,
456 "Number of cache hits (negative) we do not want to cache");
457 STATNODE_COUNTER(numneghits, "Number of cache hits (negative)");
458 /* These count for vn_getcwd(), too. */
459 STATNODE_COUNTER(numfullpathcalls, "Number of fullpath search calls");
460 STATNODE_COUNTER(numfullpathfail1, "Number of fullpath search errors (ENOTDIR)");
461 STATNODE_COUNTER(numfullpathfail2,
462 "Number of fullpath search errors (VOP_VPTOCNP failures)");
463 STATNODE_COUNTER(numfullpathfail4, "Number of fullpath search errors (ENOMEM)");
464 STATNODE_COUNTER(numfullpathfound, "Number of successful fullpath calls");
465 STATNODE_COUNTER(zap_and_exit_bucket_relock_success,
466 "Number of successful removals after relocking");
467 static long zap_and_exit_bucket_fail; STATNODE_ULONG(zap_and_exit_bucket_fail,
468 "Number of times zap_and_exit failed to lock");
469 static long zap_and_exit_bucket_fail2; STATNODE_ULONG(zap_and_exit_bucket_fail2,
470 "Number of times zap_and_exit failed to lock");
471 static long cache_lock_vnodes_cel_3_failures;
472 STATNODE_ULONG(cache_lock_vnodes_cel_3_failures,
473 "Number of times 3-way vnode locking failed");
474 STATNODE_ULONG(numhotneg, "Number of hot negative entries");
475 STATNODE_COUNTER(numneg_evicted,
476 "Number of negative entries evicted when adding a new entry");
477 STATNODE_COUNTER(shrinking_skipped,
478 "Number of times shrinking was already in progress");
480 static void cache_zap_locked(struct namecache *ncp);
481 static int vn_fullpath_hardlink(struct nameidata *ndp, char **retbuf,
482 char **freebuf, size_t *buflen);
483 static int vn_fullpath_any_smr(struct vnode *vp, struct vnode *rdir, char *buf,
484 char **retbuf, size_t *buflen, bool slash_prefixed, size_t addend);
485 static int vn_fullpath_any(struct vnode *vp, struct vnode *rdir, char *buf,
486 char **retbuf, size_t *buflen);
487 static int vn_fullpath_dir(struct vnode *vp, struct vnode *rdir, char *buf,
488 char **retbuf, size_t *len, bool slash_prefixed, size_t addend);
490 static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
493 cache_assert_vlp_locked(struct mtx *vlp)
497 mtx_assert(vlp, MA_OWNED);
501 cache_assert_vnode_locked(struct vnode *vp)
505 vlp = VP2VNODELOCK(vp);
506 cache_assert_vlp_locked(vlp);
510 * TODO: With the value stored we can do better than computing the hash based
511 * on the address. The choice of FNV should also be revisited.
514 cache_prehash(struct vnode *vp)
517 vp->v_nchash = fnv_32_buf(&vp, sizeof(vp), FNV1_32_INIT);
521 cache_get_hash(char *name, u_char len, struct vnode *dvp)
524 return (fnv_32_buf(name, len, dvp->v_nchash));
527 static inline struct nchashhead *
528 NCP2BUCKET(struct namecache *ncp)
532 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen, ncp->nc_dvp);
533 return (NCHHASH(hash));
536 static inline struct mtx *
537 NCP2BUCKETLOCK(struct namecache *ncp)
541 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen, ncp->nc_dvp);
542 return (HASH2BUCKETLOCK(hash));
547 cache_assert_bucket_locked(struct namecache *ncp)
551 blp = NCP2BUCKETLOCK(ncp);
552 mtx_assert(blp, MA_OWNED);
556 cache_assert_bucket_unlocked(struct namecache *ncp)
560 blp = NCP2BUCKETLOCK(ncp);
561 mtx_assert(blp, MA_NOTOWNED);
564 #define cache_assert_bucket_locked(x) do { } while (0)
565 #define cache_assert_bucket_unlocked(x) do { } while (0)
568 #define cache_sort_vnodes(x, y) _cache_sort_vnodes((void **)(x), (void **)(y))
570 _cache_sort_vnodes(void **p1, void **p2)
574 MPASS(*p1 != NULL || *p2 != NULL);
584 cache_lock_all_buckets(void)
588 for (i = 0; i < numbucketlocks; i++)
589 mtx_lock(&bucketlocks[i]);
593 cache_unlock_all_buckets(void)
597 for (i = 0; i < numbucketlocks; i++)
598 mtx_unlock(&bucketlocks[i]);
602 cache_lock_all_vnodes(void)
606 for (i = 0; i < numvnodelocks; i++)
607 mtx_lock(&vnodelocks[i]);
611 cache_unlock_all_vnodes(void)
615 for (i = 0; i < numvnodelocks; i++)
616 mtx_unlock(&vnodelocks[i]);
620 cache_trylock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
623 cache_sort_vnodes(&vlp1, &vlp2);
626 if (!mtx_trylock(vlp1))
629 if (!mtx_trylock(vlp2)) {
639 cache_lock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
642 MPASS(vlp1 != NULL || vlp2 != NULL);
652 cache_unlock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
655 MPASS(vlp1 != NULL || vlp2 != NULL);
664 sysctl_nchstats(SYSCTL_HANDLER_ARGS)
666 struct nchstats snap;
668 if (req->oldptr == NULL)
669 return (SYSCTL_OUT(req, 0, sizeof(snap)));
672 snap.ncs_goodhits = counter_u64_fetch(numposhits);
673 snap.ncs_neghits = counter_u64_fetch(numneghits);
674 snap.ncs_badhits = counter_u64_fetch(numposzaps) +
675 counter_u64_fetch(numnegzaps);
676 snap.ncs_miss = counter_u64_fetch(nummisszap) +
677 counter_u64_fetch(nummiss);
679 return (SYSCTL_OUT(req, &snap, sizeof(snap)));
681 SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE | CTLFLAG_RD |
682 CTLFLAG_MPSAFE, 0, 0, sysctl_nchstats, "LU",
683 "VFS cache effectiveness statistics");
687 * Grab an atomic snapshot of the name cache hash chain lengths
689 static SYSCTL_NODE(_debug, OID_AUTO, hashstat,
690 CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
694 sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS)
696 struct nchashhead *ncpp;
697 struct namecache *ncp;
698 int i, error, n_nchash, *cntbuf;
701 n_nchash = nchash + 1; /* nchash is max index, not count */
702 if (req->oldptr == NULL)
703 return SYSCTL_OUT(req, 0, n_nchash * sizeof(int));
704 cntbuf = malloc(n_nchash * sizeof(int), M_TEMP, M_ZERO | M_WAITOK);
705 cache_lock_all_buckets();
706 if (n_nchash != nchash + 1) {
707 cache_unlock_all_buckets();
708 free(cntbuf, M_TEMP);
711 /* Scan hash tables counting entries */
712 for (ncpp = nchashtbl, i = 0; i < n_nchash; ncpp++, i++)
713 CK_SLIST_FOREACH(ncp, ncpp, nc_hash)
715 cache_unlock_all_buckets();
716 for (error = 0, i = 0; i < n_nchash; i++)
717 if ((error = SYSCTL_OUT(req, &cntbuf[i], sizeof(int))) != 0)
719 free(cntbuf, M_TEMP);
722 SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD|
723 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_rawnchash, "S,int",
724 "nchash chain lengths");
727 sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS)
730 struct nchashhead *ncpp;
731 struct namecache *ncp;
733 int count, maxlength, used, pct;
736 return SYSCTL_OUT(req, 0, 4 * sizeof(int));
738 cache_lock_all_buckets();
739 n_nchash = nchash + 1; /* nchash is max index, not count */
743 /* Scan hash tables for applicable entries */
744 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
746 CK_SLIST_FOREACH(ncp, ncpp, nc_hash) {
751 if (maxlength < count)
754 n_nchash = nchash + 1;
755 cache_unlock_all_buckets();
756 pct = (used * 100) / (n_nchash / 100);
757 error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash));
760 error = SYSCTL_OUT(req, &used, sizeof(used));
763 error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength));
766 error = SYSCTL_OUT(req, &pct, sizeof(pct));
771 SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD|
772 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_nchash, "I",
773 "nchash statistics (number of total/used buckets, maximum chain length, usage percentage)");
777 * Negative entries management
779 * A variation of LRU scheme is used. New entries are hashed into one of
780 * numneglists cold lists. Entries get promoted to the hot list on first hit.
782 * The shrinker will demote hot list head and evict from the cold list in a
783 * round-robin manner.
786 cache_negative_init(struct namecache *ncp)
788 struct negstate *negstate;
790 ncp->nc_flag |= NCF_NEGATIVE;
791 negstate = NCP2NEGSTATE(ncp);
792 negstate->neg_flag = 0;
796 cache_negative_hit(struct namecache *ncp)
798 struct neglist *neglist;
799 struct negstate *negstate;
801 negstate = NCP2NEGSTATE(ncp);
802 if ((negstate->neg_flag & NEG_HOT) != 0)
804 neglist = NCP2NEGLIST(ncp);
805 mtx_lock(&ncneg_hot.nl_lock);
806 mtx_lock(&neglist->nl_lock);
807 if ((negstate->neg_flag & NEG_HOT) == 0) {
809 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
810 TAILQ_INSERT_TAIL(&ncneg_hot.nl_list, ncp, nc_dst);
811 negstate->neg_flag |= NEG_HOT;
813 mtx_unlock(&neglist->nl_lock);
814 mtx_unlock(&ncneg_hot.nl_lock);
818 cache_negative_insert(struct namecache *ncp)
820 struct neglist *neglist;
822 MPASS(ncp->nc_flag & NCF_NEGATIVE);
823 cache_assert_bucket_locked(ncp);
824 neglist = NCP2NEGLIST(ncp);
825 mtx_lock(&neglist->nl_lock);
826 TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst);
827 mtx_unlock(&neglist->nl_lock);
828 atomic_add_long(&numneg, 1);
832 cache_negative_remove(struct namecache *ncp)
834 struct neglist *neglist;
835 struct negstate *negstate;
836 bool hot_locked = false;
837 bool list_locked = false;
839 cache_assert_bucket_locked(ncp);
840 neglist = NCP2NEGLIST(ncp);
841 negstate = NCP2NEGSTATE(ncp);
842 if ((negstate->neg_flag & NEG_HOT) != 0) {
844 mtx_lock(&ncneg_hot.nl_lock);
845 if ((negstate->neg_flag & NEG_HOT) == 0) {
847 mtx_lock(&neglist->nl_lock);
851 mtx_lock(&neglist->nl_lock);
853 * We may be racing against promotion in lockless lookup.
855 if ((negstate->neg_flag & NEG_HOT) != 0) {
856 mtx_unlock(&neglist->nl_lock);
858 mtx_lock(&ncneg_hot.nl_lock);
859 mtx_lock(&neglist->nl_lock);
862 if ((negstate->neg_flag & NEG_HOT) != 0) {
863 mtx_assert(&ncneg_hot.nl_lock, MA_OWNED);
864 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
867 mtx_assert(&neglist->nl_lock, MA_OWNED);
868 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
871 mtx_unlock(&neglist->nl_lock);
873 mtx_unlock(&ncneg_hot.nl_lock);
874 atomic_subtract_long(&numneg, 1);
878 cache_negative_shrink_select(struct namecache **ncpp,
879 struct neglist **neglistpp)
881 struct neglist *neglist;
882 struct namecache *ncp;
888 for (i = 0; i < numneglists; i++) {
889 neglist = &neglists[(cycle + i) % numneglists];
890 if (TAILQ_FIRST(&neglist->nl_list) == NULL)
892 mtx_lock(&neglist->nl_lock);
893 ncp = TAILQ_FIRST(&neglist->nl_list);
896 mtx_unlock(&neglist->nl_lock);
899 *neglistpp = neglist;
905 cache_negative_zap_one(void)
907 struct namecache *ncp, *ncp2;
908 struct neglist *neglist;
909 struct negstate *negstate;
913 if (mtx_owner(&ncneg_shrink_lock) != NULL ||
914 !mtx_trylock(&ncneg_shrink_lock)) {
915 counter_u64_add(shrinking_skipped, 1);
919 mtx_lock(&ncneg_hot.nl_lock);
920 ncp = TAILQ_FIRST(&ncneg_hot.nl_list);
922 neglist = NCP2NEGLIST(ncp);
923 negstate = NCP2NEGSTATE(ncp);
924 mtx_lock(&neglist->nl_lock);
925 MPASS((negstate->neg_flag & NEG_HOT) != 0);
926 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
927 TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst);
928 negstate->neg_flag &= ~NEG_HOT;
930 mtx_unlock(&neglist->nl_lock);
932 mtx_unlock(&ncneg_hot.nl_lock);
934 cache_negative_shrink_select(&ncp, &neglist);
936 mtx_unlock(&ncneg_shrink_lock);
940 MPASS(ncp->nc_flag & NCF_NEGATIVE);
941 dvlp = VP2VNODELOCK(ncp->nc_dvp);
942 blp = NCP2BUCKETLOCK(ncp);
943 mtx_unlock(&neglist->nl_lock);
947 * Enter SMR to safely check the negative list.
948 * Even if the found pointer matches, the entry may now be reallocated
949 * and used by a different vnode.
952 ncp2 = TAILQ_FIRST(&neglist->nl_list);
953 if (ncp != ncp2 || dvlp != VP2VNODELOCK(ncp2->nc_dvp) ||
954 blp != NCP2BUCKETLOCK(ncp2)) {
959 SDT_PROBE2(vfs, namecache, shrink_negative, done, ncp->nc_dvp,
961 cache_zap_locked(ncp);
962 counter_u64_add(numneg_evicted, 1);
971 * cache_zap_locked():
973 * Removes a namecache entry from cache, whether it contains an actual
974 * pointer to a vnode or if it is just a negative cache entry.
977 cache_zap_locked(struct namecache *ncp)
979 struct nchashhead *ncpp;
981 if (!(ncp->nc_flag & NCF_NEGATIVE))
982 cache_assert_vnode_locked(ncp->nc_vp);
983 cache_assert_vnode_locked(ncp->nc_dvp);
984 cache_assert_bucket_locked(ncp);
986 cache_ncp_invalidate(ncp);
988 ncpp = NCP2BUCKET(ncp);
989 CK_SLIST_REMOVE(ncpp, ncp, namecache, nc_hash);
990 if (!(ncp->nc_flag & NCF_NEGATIVE)) {
991 SDT_PROBE3(vfs, namecache, zap, done, ncp->nc_dvp,
992 ncp->nc_name, ncp->nc_vp);
993 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
994 if (ncp == ncp->nc_vp->v_cache_dd) {
995 vn_seqc_write_begin_unheld(ncp->nc_vp);
996 ncp->nc_vp->v_cache_dd = NULL;
997 vn_seqc_write_end(ncp->nc_vp);
1000 SDT_PROBE2(vfs, namecache, zap_negative, done, ncp->nc_dvp,
1002 cache_negative_remove(ncp);
1004 if (ncp->nc_flag & NCF_ISDOTDOT) {
1005 if (ncp == ncp->nc_dvp->v_cache_dd) {
1006 vn_seqc_write_begin_unheld(ncp->nc_dvp);
1007 ncp->nc_dvp->v_cache_dd = NULL;
1008 vn_seqc_write_end(ncp->nc_dvp);
1011 LIST_REMOVE(ncp, nc_src);
1012 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) {
1013 ncp->nc_flag |= NCF_DVDROP;
1014 counter_u64_add(numcachehv, -1);
1017 atomic_subtract_long(&numcache, 1);
1021 cache_zap_negative_locked_vnode_kl(struct namecache *ncp, struct vnode *vp)
1025 MPASS(ncp->nc_dvp == vp);
1026 MPASS(ncp->nc_flag & NCF_NEGATIVE);
1027 cache_assert_vnode_locked(vp);
1029 blp = NCP2BUCKETLOCK(ncp);
1031 cache_zap_locked(ncp);
1036 cache_zap_locked_vnode_kl2(struct namecache *ncp, struct vnode *vp,
1039 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
1042 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
1043 cache_assert_vnode_locked(vp);
1045 if (ncp->nc_flag & NCF_NEGATIVE) {
1046 if (*vlpp != NULL) {
1050 cache_zap_negative_locked_vnode_kl(ncp, vp);
1054 pvlp = VP2VNODELOCK(vp);
1055 blp = NCP2BUCKETLOCK(ncp);
1056 vlp1 = VP2VNODELOCK(ncp->nc_dvp);
1057 vlp2 = VP2VNODELOCK(ncp->nc_vp);
1059 if (*vlpp == vlp1 || *vlpp == vlp2) {
1063 if (*vlpp != NULL) {
1067 cache_sort_vnodes(&vlp1, &vlp2);
1072 if (!mtx_trylock(vlp1))
1078 cache_zap_locked(ncp);
1080 if (to_unlock != NULL)
1081 mtx_unlock(to_unlock);
1088 MPASS(*vlpp == NULL);
1094 * If trylocking failed we can get here. We know enough to take all needed locks
1095 * in the right order and re-lookup the entry.
1098 cache_zap_unlocked_bucket(struct namecache *ncp, struct componentname *cnp,
1099 struct vnode *dvp, struct mtx *dvlp, struct mtx *vlp, uint32_t hash,
1102 struct namecache *rncp;
1104 cache_assert_bucket_unlocked(ncp);
1106 cache_sort_vnodes(&dvlp, &vlp);
1107 cache_lock_vnodes(dvlp, vlp);
1109 CK_SLIST_FOREACH(rncp, (NCHHASH(hash)), nc_hash) {
1110 if (rncp == ncp && rncp->nc_dvp == dvp &&
1111 rncp->nc_nlen == cnp->cn_namelen &&
1112 !bcmp(rncp->nc_name, cnp->cn_nameptr, rncp->nc_nlen))
1116 cache_zap_locked(rncp);
1118 cache_unlock_vnodes(dvlp, vlp);
1119 counter_u64_add(zap_and_exit_bucket_relock_success, 1);
1124 cache_unlock_vnodes(dvlp, vlp);
1128 static int __noinline
1129 cache_zap_locked_bucket(struct namecache *ncp, struct componentname *cnp,
1130 uint32_t hash, struct mtx *blp)
1132 struct mtx *dvlp, *vlp;
1135 cache_assert_bucket_locked(ncp);
1137 dvlp = VP2VNODELOCK(ncp->nc_dvp);
1139 if (!(ncp->nc_flag & NCF_NEGATIVE))
1140 vlp = VP2VNODELOCK(ncp->nc_vp);
1141 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1142 cache_zap_locked(ncp);
1144 cache_unlock_vnodes(dvlp, vlp);
1150 return (cache_zap_unlocked_bucket(ncp, cnp, dvp, dvlp, vlp, hash, blp));
1153 static __noinline int
1154 cache_remove_cnp(struct vnode *dvp, struct componentname *cnp)
1156 struct namecache *ncp;
1158 struct mtx *dvlp, *dvlp2;
1162 if (cnp->cn_namelen == 2 &&
1163 cnp->cn_nameptr[0] == '.' && cnp->cn_nameptr[1] == '.') {
1164 dvlp = VP2VNODELOCK(dvp);
1168 ncp = dvp->v_cache_dd;
1173 SDT_PROBE2(vfs, namecache, removecnp, miss, dvp, cnp);
1176 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1177 if (!cache_zap_locked_vnode_kl2(ncp, dvp, &dvlp2))
1179 MPASS(dvp->v_cache_dd == NULL);
1185 vn_seqc_write_begin(dvp);
1186 dvp->v_cache_dd = NULL;
1187 vn_seqc_write_end(dvp);
1192 SDT_PROBE2(vfs, namecache, removecnp, hit, dvp, cnp);
1196 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1197 blp = HASH2BUCKETLOCK(hash);
1199 if (CK_SLIST_EMPTY(NCHHASH(hash)))
1204 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1205 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1206 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1215 error = cache_zap_locked_bucket(ncp, cnp, hash, blp);
1216 if (__predict_false(error != 0)) {
1217 zap_and_exit_bucket_fail++;
1220 counter_u64_add(numposzaps, 1);
1221 SDT_PROBE2(vfs, namecache, removecnp, hit, dvp, cnp);
1225 counter_u64_add(nummisszap, 1);
1226 SDT_PROBE2(vfs, namecache, removecnp, miss, dvp, cnp);
1230 static int __noinline
1231 cache_lookup_dot(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1232 struct timespec *tsp, int *ticksp)
1237 counter_u64_add(dothits, 1);
1238 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", *vpp);
1245 * When we lookup "." we still can be asked to lock it
1248 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
1249 if (ltype != VOP_ISLOCKED(*vpp)) {
1250 if (ltype == LK_EXCLUSIVE) {
1251 vn_lock(*vpp, LK_UPGRADE | LK_RETRY);
1252 if (VN_IS_DOOMED((*vpp))) {
1253 /* forced unmount */
1259 vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY);
1264 static int __noinline
1265 cache_lookup_dotdot(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1266 struct timespec *tsp, int *ticksp)
1268 struct namecache_ts *ncp_ts;
1269 struct namecache *ncp;
1275 MPASS((cnp->cn_flags & ISDOTDOT) != 0);
1277 if ((cnp->cn_flags & MAKEENTRY) == 0) {
1278 cache_remove_cnp(dvp, cnp);
1282 counter_u64_add(dotdothits, 1);
1284 dvlp = VP2VNODELOCK(dvp);
1286 ncp = dvp->v_cache_dd;
1288 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, "..", NULL);
1292 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1293 if (ncp->nc_flag & NCF_NEGATIVE)
1300 goto negative_success;
1301 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, "..", *vpp);
1302 cache_out_ts(ncp, tsp, ticksp);
1303 if ((ncp->nc_flag & (NCF_ISDOTDOT | NCF_DTS)) ==
1304 NCF_DTS && tsp != NULL) {
1305 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1306 *tsp = ncp_ts->nc_dotdottime;
1310 ltype = VOP_ISLOCKED(dvp);
1312 vs = vget_prep(*vpp);
1314 error = vget_finish(*vpp, cnp->cn_lkflags, vs);
1315 vn_lock(dvp, ltype | LK_RETRY);
1316 if (VN_IS_DOOMED(dvp)) {
1328 if (__predict_false(cnp->cn_nameiop == CREATE)) {
1329 if (cnp->cn_flags & ISLASTCN) {
1330 counter_u64_add(numnegzaps, 1);
1331 cache_zap_negative_locked_vnode_kl(ncp, dvp);
1338 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp, ncp->nc_name);
1339 cache_out_ts(ncp, tsp, ticksp);
1340 counter_u64_add(numneghits, 1);
1341 whiteout = (ncp->nc_flag & NCF_WHITE);
1342 cache_negative_hit(ncp);
1345 cnp->cn_flags |= ISWHITEOUT;
1350 * Lookup a name in the name cache
1354 * - dvp: Parent directory in which to search.
1355 * - vpp: Return argument. Will contain desired vnode on cache hit.
1356 * - cnp: Parameters of the name search. The most interesting bits of
1357 * the cn_flags field have the following meanings:
1358 * - MAKEENTRY: If clear, free an entry from the cache rather than look
1360 * - ISDOTDOT: Must be set if and only if cn_nameptr == ".."
1361 * - tsp: Return storage for cache timestamp. On a successful (positive
1362 * or negative) lookup, tsp will be filled with any timespec that
1363 * was stored when this cache entry was created. However, it will
1364 * be clear for "." entries.
1365 * - ticks: Return storage for alternate cache timestamp. On a successful
1366 * (positive or negative) lookup, it will contain the ticks value
1367 * that was current when the cache entry was created, unless cnp
1370 * Either both tsp and ticks have to be provided or neither of them.
1374 * - -1: A positive cache hit. vpp will contain the desired vnode.
1375 * - ENOENT: A negative cache hit, or dvp was recycled out from under us due
1376 * to a forced unmount. vpp will not be modified. If the entry
1377 * is a whiteout, then the ISWHITEOUT flag will be set in
1379 * - 0: A cache miss. vpp will not be modified.
1383 * On a cache hit, vpp will be returned locked and ref'd. If we're looking up
1384 * .., dvp is unlocked. If we're looking up . an extra ref is taken, but the
1385 * lock is not recursively acquired.
1387 static int __noinline
1388 cache_lookup_fallback(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1389 struct timespec *tsp, int *ticksp)
1391 struct namecache *ncp;
1398 MPASS((cnp->cn_flags & (MAKEENTRY | ISDOTDOT)) == MAKEENTRY);
1401 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1402 blp = HASH2BUCKETLOCK(hash);
1405 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1406 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1407 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1411 if (__predict_false(ncp == NULL)) {
1413 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
1415 counter_u64_add(nummiss, 1);
1419 if (ncp->nc_flag & NCF_NEGATIVE)
1420 goto negative_success;
1422 counter_u64_add(numposhits, 1);
1424 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name, *vpp);
1425 cache_out_ts(ncp, tsp, ticksp);
1427 vs = vget_prep(*vpp);
1429 error = vget_finish(*vpp, cnp->cn_lkflags, vs);
1436 if (__predict_false(cnp->cn_nameiop == CREATE)) {
1437 if (cnp->cn_flags & ISLASTCN) {
1438 counter_u64_add(numnegzaps, 1);
1439 error = cache_zap_locked_bucket(ncp, cnp, hash, blp);
1440 if (__predict_false(error != 0)) {
1441 zap_and_exit_bucket_fail2++;
1449 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp, ncp->nc_name);
1450 cache_out_ts(ncp, tsp, ticksp);
1451 counter_u64_add(numneghits, 1);
1452 whiteout = (ncp->nc_flag & NCF_WHITE);
1453 cache_negative_hit(ncp);
1456 cnp->cn_flags |= ISWHITEOUT;
1461 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1462 struct timespec *tsp, int *ticksp)
1464 struct namecache *ncp;
1465 struct negstate *negstate;
1472 MPASS((tsp == NULL && ticksp == NULL) || (tsp != NULL && ticksp != NULL));
1475 if (__predict_false(!doingcache)) {
1476 cnp->cn_flags &= ~MAKEENTRY;
1481 if (__predict_false(cnp->cn_nameptr[0] == '.')) {
1482 if (cnp->cn_namelen == 1)
1483 return (cache_lookup_dot(dvp, vpp, cnp, tsp, ticksp));
1484 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.')
1485 return (cache_lookup_dotdot(dvp, vpp, cnp, tsp, ticksp));
1488 MPASS((cnp->cn_flags & ISDOTDOT) == 0);
1490 if ((cnp->cn_flags & MAKEENTRY) == 0) {
1491 cache_remove_cnp(dvp, cnp);
1495 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1498 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1499 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1500 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1504 if (__predict_false(ncp == NULL)) {
1506 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
1508 counter_u64_add(nummiss, 1);
1512 nc_flag = atomic_load_char(&ncp->nc_flag);
1513 if (nc_flag & NCF_NEGATIVE)
1514 goto negative_success;
1516 counter_u64_add(numposhits, 1);
1518 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name, *vpp);
1519 cache_out_ts(ncp, tsp, ticksp);
1521 if (!cache_ncp_canuse(ncp)) {
1526 vs = vget_prep_smr(*vpp);
1528 if (__predict_false(vs == VGET_NONE)) {
1532 error = vget_finish(*vpp, cnp->cn_lkflags, vs);
1539 if (__predict_false(cnp->cn_nameiop == CREATE)) {
1540 if (cnp->cn_flags & ISLASTCN) {
1546 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp, ncp->nc_name);
1547 cache_out_ts(ncp, tsp, ticksp);
1548 counter_u64_add(numneghits, 1);
1549 whiteout = (ncp->nc_flag & NCF_WHITE);
1551 * TODO: We need to take locks to promote an entry. Code doing it
1552 * in SMR lookup can be modified to be shared.
1554 negstate = NCP2NEGSTATE(ncp);
1555 if ((negstate->neg_flag & NEG_HOT) == 0 ||
1556 !cache_ncp_canuse(ncp)) {
1562 cnp->cn_flags |= ISWHITEOUT;
1565 return (cache_lookup_fallback(dvp, vpp, cnp, tsp, ticksp));
1568 struct celockstate {
1572 CTASSERT((nitems(((struct celockstate *)0)->vlp) == 3));
1573 CTASSERT((nitems(((struct celockstate *)0)->blp) == 2));
1576 cache_celockstate_init(struct celockstate *cel)
1579 bzero(cel, sizeof(*cel));
1583 cache_lock_vnodes_cel(struct celockstate *cel, struct vnode *vp,
1586 struct mtx *vlp1, *vlp2;
1588 MPASS(cel->vlp[0] == NULL);
1589 MPASS(cel->vlp[1] == NULL);
1590 MPASS(cel->vlp[2] == NULL);
1592 MPASS(vp != NULL || dvp != NULL);
1594 vlp1 = VP2VNODELOCK(vp);
1595 vlp2 = VP2VNODELOCK(dvp);
1596 cache_sort_vnodes(&vlp1, &vlp2);
1607 cache_unlock_vnodes_cel(struct celockstate *cel)
1610 MPASS(cel->vlp[0] != NULL || cel->vlp[1] != NULL);
1612 if (cel->vlp[0] != NULL)
1613 mtx_unlock(cel->vlp[0]);
1614 if (cel->vlp[1] != NULL)
1615 mtx_unlock(cel->vlp[1]);
1616 if (cel->vlp[2] != NULL)
1617 mtx_unlock(cel->vlp[2]);
1621 cache_lock_vnodes_cel_3(struct celockstate *cel, struct vnode *vp)
1626 cache_assert_vlp_locked(cel->vlp[0]);
1627 cache_assert_vlp_locked(cel->vlp[1]);
1628 MPASS(cel->vlp[2] == NULL);
1631 vlp = VP2VNODELOCK(vp);
1634 if (vlp >= cel->vlp[1]) {
1637 if (mtx_trylock(vlp))
1639 cache_lock_vnodes_cel_3_failures++;
1640 cache_unlock_vnodes_cel(cel);
1641 if (vlp < cel->vlp[0]) {
1643 mtx_lock(cel->vlp[0]);
1644 mtx_lock(cel->vlp[1]);
1646 if (cel->vlp[0] != NULL)
1647 mtx_lock(cel->vlp[0]);
1649 mtx_lock(cel->vlp[1]);
1659 cache_lock_buckets_cel(struct celockstate *cel, struct mtx *blp1,
1663 MPASS(cel->blp[0] == NULL);
1664 MPASS(cel->blp[1] == NULL);
1666 cache_sort_vnodes(&blp1, &blp2);
1677 cache_unlock_buckets_cel(struct celockstate *cel)
1680 if (cel->blp[0] != NULL)
1681 mtx_unlock(cel->blp[0]);
1682 mtx_unlock(cel->blp[1]);
1686 * Lock part of the cache affected by the insertion.
1688 * This means vnodelocks for dvp, vp and the relevant bucketlock.
1689 * However, insertion can result in removal of an old entry. In this
1690 * case we have an additional vnode and bucketlock pair to lock.
1692 * That is, in the worst case we have to lock 3 vnodes and 2 bucketlocks, while
1693 * preserving the locking order (smaller address first).
1696 cache_enter_lock(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1699 struct namecache *ncp;
1700 struct mtx *blps[2];
1702 blps[0] = HASH2BUCKETLOCK(hash);
1705 cache_lock_vnodes_cel(cel, dvp, vp);
1706 if (vp == NULL || vp->v_type != VDIR)
1708 ncp = vp->v_cache_dd;
1711 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1713 MPASS(ncp->nc_dvp == vp);
1714 blps[1] = NCP2BUCKETLOCK(ncp);
1715 if (ncp->nc_flag & NCF_NEGATIVE)
1717 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1720 * All vnodes got re-locked. Re-validate the state and if
1721 * nothing changed we are done. Otherwise restart.
1723 if (ncp == vp->v_cache_dd &&
1724 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1725 blps[1] == NCP2BUCKETLOCK(ncp) &&
1726 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1728 cache_unlock_vnodes_cel(cel);
1733 cache_lock_buckets_cel(cel, blps[0], blps[1]);
1737 cache_enter_lock_dd(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1740 struct namecache *ncp;
1741 struct mtx *blps[2];
1743 blps[0] = HASH2BUCKETLOCK(hash);
1746 cache_lock_vnodes_cel(cel, dvp, vp);
1747 ncp = dvp->v_cache_dd;
1750 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1752 MPASS(ncp->nc_dvp == dvp);
1753 blps[1] = NCP2BUCKETLOCK(ncp);
1754 if (ncp->nc_flag & NCF_NEGATIVE)
1756 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1758 if (ncp == dvp->v_cache_dd &&
1759 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1760 blps[1] == NCP2BUCKETLOCK(ncp) &&
1761 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1763 cache_unlock_vnodes_cel(cel);
1768 cache_lock_buckets_cel(cel, blps[0], blps[1]);
1772 cache_enter_unlock(struct celockstate *cel)
1775 cache_unlock_buckets_cel(cel);
1776 cache_unlock_vnodes_cel(cel);
1779 static void __noinline
1780 cache_enter_dotdot_prep(struct vnode *dvp, struct vnode *vp,
1781 struct componentname *cnp)
1783 struct celockstate cel;
1784 struct namecache *ncp;
1788 if (dvp->v_cache_dd == NULL)
1790 len = cnp->cn_namelen;
1791 cache_celockstate_init(&cel);
1792 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1793 cache_enter_lock_dd(&cel, dvp, vp, hash);
1794 vn_seqc_write_begin(dvp);
1795 ncp = dvp->v_cache_dd;
1796 if (ncp != NULL && (ncp->nc_flag & NCF_ISDOTDOT)) {
1797 KASSERT(ncp->nc_dvp == dvp, ("wrong isdotdot parent"));
1798 cache_zap_locked(ncp);
1802 dvp->v_cache_dd = NULL;
1803 vn_seqc_write_end(dvp);
1804 cache_enter_unlock(&cel);
1810 * Add an entry to the cache.
1813 cache_enter_time(struct vnode *dvp, struct vnode *vp, struct componentname *cnp,
1814 struct timespec *tsp, struct timespec *dtsp)
1816 struct celockstate cel;
1817 struct namecache *ncp, *n2, *ndd;
1818 struct namecache_ts *ncp_ts;
1819 struct nchashhead *ncpp;
1825 VNPASS(!VN_IS_DOOMED(dvp), dvp);
1826 VNPASS(dvp->v_type != VNON, dvp);
1828 VNPASS(!VN_IS_DOOMED(vp), vp);
1829 VNPASS(vp->v_type != VNON, vp);
1833 if (__predict_false(!doingcache))
1838 if (__predict_false(cnp->cn_nameptr[0] == '.')) {
1839 if (cnp->cn_namelen == 1)
1841 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
1842 cache_enter_dotdot_prep(dvp, vp, cnp);
1843 flag = NCF_ISDOTDOT;
1848 * Avoid blowout in namecache entries.
1850 lnumcache = atomic_fetchadd_long(&numcache, 1) + 1;
1851 if (__predict_false(lnumcache >= ncsize)) {
1852 atomic_subtract_long(&numcache, 1);
1853 counter_u64_add(numdrops, 1);
1857 cache_celockstate_init(&cel);
1862 * Calculate the hash key and setup as much of the new
1863 * namecache entry as possible before acquiring the lock.
1865 ncp = cache_alloc(cnp->cn_namelen, tsp != NULL);
1866 ncp->nc_flag = flag | NCF_WIP;
1869 cache_negative_init(ncp);
1872 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1873 ncp_ts->nc_time = *tsp;
1874 ncp_ts->nc_ticks = ticks;
1875 ncp_ts->nc_nc.nc_flag |= NCF_TS;
1877 ncp_ts->nc_dotdottime = *dtsp;
1878 ncp_ts->nc_nc.nc_flag |= NCF_DTS;
1881 len = ncp->nc_nlen = cnp->cn_namelen;
1882 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1883 memcpy(ncp->nc_name, cnp->cn_nameptr, len);
1884 ncp->nc_name[len] = '\0';
1885 cache_enter_lock(&cel, dvp, vp, hash);
1888 * See if this vnode or negative entry is already in the cache
1889 * with this name. This can happen with concurrent lookups of
1890 * the same path name.
1892 ncpp = NCHHASH(hash);
1893 CK_SLIST_FOREACH(n2, ncpp, nc_hash) {
1894 if (n2->nc_dvp == dvp &&
1895 n2->nc_nlen == cnp->cn_namelen &&
1896 !bcmp(n2->nc_name, cnp->cn_nameptr, n2->nc_nlen)) {
1897 MPASS(cache_ncp_canuse(n2));
1898 if ((n2->nc_flag & NCF_NEGATIVE) != 0)
1900 ("%s: found entry pointing to a different vnode (%p != %p)",
1901 __func__, NULL, vp));
1903 KASSERT(n2->nc_vp == vp,
1904 ("%s: found entry pointing to a different vnode (%p != %p)",
1905 __func__, n2->nc_vp, vp));
1907 * Entries are supposed to be immutable unless in the
1908 * process of getting destroyed. Accommodating for
1909 * changing timestamps is possible but not worth it.
1910 * This should be harmless in terms of correctness, in
1911 * the worst case resulting in an earlier expiration.
1912 * Alternatively, the found entry can be replaced
1915 MPASS((n2->nc_flag & (NCF_TS | NCF_DTS)) == (ncp->nc_flag & (NCF_TS | NCF_DTS)));
1918 KASSERT((n2->nc_flag & NCF_TS) != 0,
1920 n2_ts = __containerof(n2, struct namecache_ts, nc_nc);
1921 n2_ts->nc_time = ncp_ts->nc_time;
1922 n2_ts->nc_ticks = ncp_ts->nc_ticks;
1924 n2_ts->nc_dotdottime = ncp_ts->nc_dotdottime;
1925 n2_ts->nc_nc.nc_flag |= NCF_DTS;
1929 goto out_unlock_free;
1933 if (flag == NCF_ISDOTDOT) {
1935 * See if we are trying to add .. entry, but some other lookup
1936 * has populated v_cache_dd pointer already.
1938 if (dvp->v_cache_dd != NULL)
1939 goto out_unlock_free;
1940 KASSERT(vp == NULL || vp->v_type == VDIR,
1941 ("wrong vnode type %p", vp));
1942 vn_seqc_write_begin(dvp);
1943 dvp->v_cache_dd = ncp;
1944 vn_seqc_write_end(dvp);
1948 if (flag != NCF_ISDOTDOT) {
1950 * For this case, the cache entry maps both the
1951 * directory name in it and the name ".." for the
1952 * directory's parent.
1954 vn_seqc_write_begin(vp);
1955 if ((ndd = vp->v_cache_dd) != NULL) {
1956 if ((ndd->nc_flag & NCF_ISDOTDOT) != 0)
1957 cache_zap_locked(ndd);
1961 vp->v_cache_dd = ncp;
1962 vn_seqc_write_end(vp);
1963 } else if (vp->v_type != VDIR) {
1964 if (vp->v_cache_dd != NULL) {
1965 vn_seqc_write_begin(vp);
1966 vp->v_cache_dd = NULL;
1967 vn_seqc_write_end(vp);
1972 if (flag != NCF_ISDOTDOT) {
1973 if (LIST_EMPTY(&dvp->v_cache_src)) {
1975 counter_u64_add(numcachehv, 1);
1977 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
1981 * If the entry is "negative", we place it into the
1982 * "negative" cache queue, otherwise, we place it into the
1983 * destination vnode's cache entries queue.
1986 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
1987 SDT_PROBE3(vfs, namecache, enter, done, dvp, ncp->nc_name,
1990 if (cnp->cn_flags & ISWHITEOUT)
1991 ncp->nc_flag |= NCF_WHITE;
1992 cache_negative_insert(ncp);
1993 SDT_PROBE2(vfs, namecache, enter_negative, done, dvp,
1998 * Insert the new namecache entry into the appropriate chain
1999 * within the cache entries table.
2001 CK_SLIST_INSERT_HEAD(ncpp, ncp, nc_hash);
2003 atomic_thread_fence_rel();
2005 * Mark the entry as fully constructed.
2006 * It is immutable past this point until its removal.
2008 atomic_store_char(&ncp->nc_flag, ncp->nc_flag & ~NCF_WIP);
2010 cache_enter_unlock(&cel);
2011 if (numneg * ncnegfactor > lnumcache)
2012 cache_negative_zap_one();
2017 cache_enter_unlock(&cel);
2018 atomic_subtract_long(&numcache, 1);
2024 cache_roundup_2(u_int val)
2028 for (res = 1; res <= val; res <<= 1)
2034 static struct nchashhead *
2035 nchinittbl(u_long elements, u_long *hashmask)
2037 struct nchashhead *hashtbl;
2040 hashsize = cache_roundup_2(elements) / 2;
2042 hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), M_VFSCACHE, M_WAITOK);
2043 for (i = 0; i < hashsize; i++)
2044 CK_SLIST_INIT(&hashtbl[i]);
2045 *hashmask = hashsize - 1;
2050 ncfreetbl(struct nchashhead *hashtbl)
2053 free(hashtbl, M_VFSCACHE);
2057 * Name cache initialization, from vfs_init() when we are booting
2060 nchinit(void *dummy __unused)
2064 cache_zone_small = uma_zcreate("S VFS Cache", CACHE_ZONE_SMALL_SIZE,
2065 NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT, UMA_ZONE_ZINIT);
2066 cache_zone_small_ts = uma_zcreate("STS VFS Cache", CACHE_ZONE_SMALL_TS_SIZE,
2067 NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT, UMA_ZONE_ZINIT);
2068 cache_zone_large = uma_zcreate("L VFS Cache", CACHE_ZONE_LARGE_SIZE,
2069 NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT, UMA_ZONE_ZINIT);
2070 cache_zone_large_ts = uma_zcreate("LTS VFS Cache", CACHE_ZONE_LARGE_TS_SIZE,
2071 NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT, UMA_ZONE_ZINIT);
2073 VFS_SMR_ZONE_SET(cache_zone_small);
2074 VFS_SMR_ZONE_SET(cache_zone_small_ts);
2075 VFS_SMR_ZONE_SET(cache_zone_large);
2076 VFS_SMR_ZONE_SET(cache_zone_large_ts);
2078 ncsize = desiredvnodes * ncsizefactor;
2079 nchashtbl = nchinittbl(desiredvnodes * 2, &nchash);
2080 ncbuckethash = cache_roundup_2(mp_ncpus * mp_ncpus) - 1;
2081 if (ncbuckethash < 7) /* arbitrarily chosen to avoid having one lock */
2083 if (ncbuckethash > nchash)
2084 ncbuckethash = nchash;
2085 bucketlocks = malloc(sizeof(*bucketlocks) * numbucketlocks, M_VFSCACHE,
2087 for (i = 0; i < numbucketlocks; i++)
2088 mtx_init(&bucketlocks[i], "ncbuc", NULL, MTX_DUPOK | MTX_RECURSE);
2089 ncvnodehash = ncbuckethash;
2090 vnodelocks = malloc(sizeof(*vnodelocks) * numvnodelocks, M_VFSCACHE,
2092 for (i = 0; i < numvnodelocks; i++)
2093 mtx_init(&vnodelocks[i], "ncvn", NULL, MTX_DUPOK | MTX_RECURSE);
2095 for (i = 0; i < numneglists; i++) {
2096 mtx_init(&neglists[i].nl_lock, "ncnegl", NULL, MTX_DEF);
2097 TAILQ_INIT(&neglists[i].nl_list);
2099 mtx_init(&ncneg_hot.nl_lock, "ncneglh", NULL, MTX_DEF);
2100 TAILQ_INIT(&ncneg_hot.nl_list);
2102 mtx_init(&ncneg_shrink_lock, "ncnegs", NULL, MTX_DEF);
2104 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL);
2107 cache_vnode_init(struct vnode *vp)
2110 LIST_INIT(&vp->v_cache_src);
2111 TAILQ_INIT(&vp->v_cache_dst);
2112 vp->v_cache_dd = NULL;
2117 cache_changesize(u_long newmaxvnodes)
2119 struct nchashhead *new_nchashtbl, *old_nchashtbl;
2120 u_long new_nchash, old_nchash;
2121 struct namecache *ncp;
2126 newncsize = newmaxvnodes * ncsizefactor;
2127 newmaxvnodes = cache_roundup_2(newmaxvnodes * 2);
2128 if (newmaxvnodes < numbucketlocks)
2129 newmaxvnodes = numbucketlocks;
2131 new_nchashtbl = nchinittbl(newmaxvnodes, &new_nchash);
2132 /* If same hash table size, nothing to do */
2133 if (nchash == new_nchash) {
2134 ncfreetbl(new_nchashtbl);
2138 * Move everything from the old hash table to the new table.
2139 * None of the namecache entries in the table can be removed
2140 * because to do so, they have to be removed from the hash table.
2142 cache_lock_all_vnodes();
2143 cache_lock_all_buckets();
2144 old_nchashtbl = nchashtbl;
2145 old_nchash = nchash;
2146 nchashtbl = new_nchashtbl;
2147 nchash = new_nchash;
2148 for (i = 0; i <= old_nchash; i++) {
2149 while ((ncp = CK_SLIST_FIRST(&old_nchashtbl[i])) != NULL) {
2150 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen,
2152 CK_SLIST_REMOVE(&old_nchashtbl[i], ncp, namecache, nc_hash);
2153 CK_SLIST_INSERT_HEAD(NCHHASH(hash), ncp, nc_hash);
2157 cache_unlock_all_buckets();
2158 cache_unlock_all_vnodes();
2159 ncfreetbl(old_nchashtbl);
2163 * Invalidate all entries from and to a particular vnode.
2166 cache_purge_impl(struct vnode *vp)
2168 TAILQ_HEAD(, namecache) ncps;
2169 struct namecache *ncp, *nnp;
2170 struct mtx *vlp, *vlp2;
2173 vlp = VP2VNODELOCK(vp);
2177 while (!LIST_EMPTY(&vp->v_cache_src)) {
2178 ncp = LIST_FIRST(&vp->v_cache_src);
2179 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2181 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2183 while (!TAILQ_EMPTY(&vp->v_cache_dst)) {
2184 ncp = TAILQ_FIRST(&vp->v_cache_dst);
2185 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2187 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2189 ncp = vp->v_cache_dd;
2191 KASSERT(ncp->nc_flag & NCF_ISDOTDOT,
2192 ("lost dotdot link"));
2193 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2195 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2197 KASSERT(vp->v_cache_dd == NULL, ("incomplete purge"));
2201 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2207 * Opportunistic check to see if there is anything to do.
2210 cache_has_entries(struct vnode *vp)
2213 if (LIST_EMPTY(&vp->v_cache_src) && TAILQ_EMPTY(&vp->v_cache_dst) &&
2214 vp->v_cache_dd == NULL)
2220 cache_purge(struct vnode *vp)
2223 SDT_PROBE1(vfs, namecache, purge, done, vp);
2224 if (!cache_has_entries(vp))
2226 cache_purge_impl(vp);
2230 * Only to be used by vgone.
2233 cache_purge_vgone(struct vnode *vp)
2237 VNPASS(VN_IS_DOOMED(vp), vp);
2238 if (cache_has_entries(vp)) {
2239 cache_purge_impl(vp);
2244 * Serialize against a potential thread doing cache_purge.
2246 vlp = VP2VNODELOCK(vp);
2247 mtx_wait_unlocked(vlp);
2248 if (cache_has_entries(vp)) {
2249 cache_purge_impl(vp);
2256 * Invalidate all negative entries for a particular directory vnode.
2259 cache_purge_negative(struct vnode *vp)
2261 TAILQ_HEAD(, namecache) ncps;
2262 struct namecache *ncp, *nnp;
2265 SDT_PROBE1(vfs, namecache, purge_negative, done, vp);
2266 if (LIST_EMPTY(&vp->v_cache_src))
2269 vlp = VP2VNODELOCK(vp);
2271 LIST_FOREACH_SAFE(ncp, &vp->v_cache_src, nc_src, nnp) {
2272 if (!(ncp->nc_flag & NCF_NEGATIVE))
2274 cache_zap_negative_locked_vnode_kl(ncp, vp);
2275 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2278 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2284 cache_rename(struct vnode *fdvp, struct vnode *fvp, struct vnode *tdvp,
2285 struct vnode *tvp, struct componentname *fcnp, struct componentname *tcnp)
2288 ASSERT_VOP_IN_SEQC(fdvp);
2289 ASSERT_VOP_IN_SEQC(fvp);
2290 ASSERT_VOP_IN_SEQC(tdvp);
2292 ASSERT_VOP_IN_SEQC(tvp);
2297 KASSERT(!cache_remove_cnp(tdvp, tcnp),
2298 ("%s: lingering negative entry", __func__));
2300 cache_remove_cnp(tdvp, tcnp);
2305 * Flush all entries referencing a particular filesystem.
2308 cache_purgevfs(struct mount *mp)
2310 struct vnode *vp, *mvp;
2312 SDT_PROBE1(vfs, namecache, purgevfs, done, mp);
2314 * Somewhat wasteful iteration over all vnodes. Would be better to
2315 * support filtering and avoid the interlock to begin with.
2317 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
2318 if (!cache_has_entries(vp)) {
2330 * Perform canonical checks and cache lookup and pass on to filesystem
2331 * through the vop_cachedlookup only if needed.
2335 vfs_cache_lookup(struct vop_lookup_args *ap)
2339 struct vnode **vpp = ap->a_vpp;
2340 struct componentname *cnp = ap->a_cnp;
2341 int flags = cnp->cn_flags;
2346 if (dvp->v_type != VDIR)
2349 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
2350 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
2353 error = vn_dir_check_exec(dvp, cnp);
2357 error = cache_lookup(dvp, vpp, cnp, NULL, NULL);
2359 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
2365 /* Implementation of the getcwd syscall. */
2367 sys___getcwd(struct thread *td, struct __getcwd_args *uap)
2373 buflen = uap->buflen;
2374 if (__predict_false(buflen < 2))
2376 if (buflen > MAXPATHLEN)
2377 buflen = MAXPATHLEN;
2379 buf = uma_zalloc(namei_zone, M_WAITOK);
2380 error = vn_getcwd(buf, &retbuf, &buflen);
2382 error = copyout(retbuf, uap->buf, buflen);
2383 uma_zfree(namei_zone, buf);
2388 vn_getcwd(char *buf, char **retbuf, size_t *buflen)
2394 pwd = pwd_get_smr();
2395 error = vn_fullpath_any_smr(pwd->pwd_cdir, pwd->pwd_rdir, buf, retbuf,
2397 VFS_SMR_ASSERT_NOT_ENTERED();
2399 pwd = pwd_hold(curthread);
2400 error = vn_fullpath_any(pwd->pwd_cdir, pwd->pwd_rdir, buf,
2406 if (KTRPOINT(curthread, KTR_NAMEI) && error == 0)
2413 kern___realpathat(struct thread *td, int fd, const char *path, char *buf,
2414 size_t size, int flags, enum uio_seg pathseg)
2416 struct nameidata nd;
2417 char *retbuf, *freebuf;
2422 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | SAVENAME | WANTPARENT | AUDITVNODE1,
2423 pathseg, path, fd, &cap_fstat_rights, td);
2424 if ((error = namei(&nd)) != 0)
2426 error = vn_fullpath_hardlink(&nd, &retbuf, &freebuf, &size);
2428 error = copyout(retbuf, buf, size);
2429 free(freebuf, M_TEMP);
2436 sys___realpathat(struct thread *td, struct __realpathat_args *uap)
2439 return (kern___realpathat(td, uap->fd, uap->path, uap->buf, uap->size,
2440 uap->flags, UIO_USERSPACE));
2444 * Retrieve the full filesystem path that correspond to a vnode from the name
2445 * cache (if available)
2448 vn_fullpath(struct vnode *vp, char **retbuf, char **freebuf)
2455 if (__predict_false(vp == NULL))
2458 buflen = MAXPATHLEN;
2459 buf = malloc(buflen, M_TEMP, M_WAITOK);
2461 pwd = pwd_get_smr();
2462 error = vn_fullpath_any_smr(vp, pwd->pwd_rdir, buf, retbuf, &buflen, false, 0);
2463 VFS_SMR_ASSERT_NOT_ENTERED();
2465 pwd = pwd_hold(curthread);
2466 error = vn_fullpath_any(vp, pwd->pwd_rdir, buf, retbuf, &buflen);
2477 * This function is similar to vn_fullpath, but it attempts to lookup the
2478 * pathname relative to the global root mount point. This is required for the
2479 * auditing sub-system, as audited pathnames must be absolute, relative to the
2480 * global root mount point.
2483 vn_fullpath_global(struct vnode *vp, char **retbuf, char **freebuf)
2489 if (__predict_false(vp == NULL))
2491 buflen = MAXPATHLEN;
2492 buf = malloc(buflen, M_TEMP, M_WAITOK);
2494 error = vn_fullpath_any_smr(vp, rootvnode, buf, retbuf, &buflen, false, 0);
2495 VFS_SMR_ASSERT_NOT_ENTERED();
2497 error = vn_fullpath_any(vp, rootvnode, buf, retbuf, &buflen);
2506 static struct namecache *
2507 vn_dd_from_dst(struct vnode *vp)
2509 struct namecache *ncp;
2511 cache_assert_vnode_locked(vp);
2512 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst) {
2513 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2520 vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, size_t *buflen)
2523 struct namecache *ncp;
2527 vlp = VP2VNODELOCK(*vp);
2529 ncp = (*vp)->v_cache_dd;
2530 if (ncp != NULL && (ncp->nc_flag & NCF_ISDOTDOT) == 0) {
2531 KASSERT(ncp == vn_dd_from_dst(*vp),
2532 ("%s: mismatch for dd entry (%p != %p)", __func__,
2533 ncp, vn_dd_from_dst(*vp)));
2535 ncp = vn_dd_from_dst(*vp);
2538 if (*buflen < ncp->nc_nlen) {
2541 counter_u64_add(numfullpathfail4, 1);
2543 SDT_PROBE3(vfs, namecache, fullpath, return, error,
2547 *buflen -= ncp->nc_nlen;
2548 memcpy(buf + *buflen, ncp->nc_name, ncp->nc_nlen);
2549 SDT_PROBE3(vfs, namecache, fullpath, hit, ncp->nc_dvp,
2558 SDT_PROBE1(vfs, namecache, fullpath, miss, vp);
2561 vn_lock(*vp, LK_SHARED | LK_RETRY);
2562 error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen);
2565 counter_u64_add(numfullpathfail2, 1);
2566 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2571 if (VN_IS_DOOMED(dvp)) {
2572 /* forced unmount */
2575 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2579 * *vp has its use count incremented still.
2586 * Resolve a directory to a pathname.
2588 * The name of the directory can always be found in the namecache or fetched
2589 * from the filesystem. There is also guaranteed to be only one parent, meaning
2590 * we can just follow vnodes up until we find the root.
2592 * The vnode must be referenced.
2595 vn_fullpath_dir(struct vnode *vp, struct vnode *rdir, char *buf, char **retbuf,
2596 size_t *len, bool slash_prefixed, size_t addend)
2598 #ifdef KDTRACE_HOOKS
2599 struct vnode *startvp = vp;
2605 VNPASS(vp->v_type == VDIR || VN_IS_DOOMED(vp), vp);
2606 VNPASS(vp->v_usecount > 0, vp);
2610 if (!slash_prefixed) {
2618 SDT_PROBE1(vfs, namecache, fullpath, entry, vp);
2619 counter_u64_add(numfullpathcalls, 1);
2620 while (vp != rdir && vp != rootvnode) {
2622 * The vp vnode must be already fully constructed,
2623 * since it is either found in namecache or obtained
2624 * from VOP_VPTOCNP(). We may test for VV_ROOT safely
2625 * without obtaining the vnode lock.
2627 if ((vp->v_vflag & VV_ROOT) != 0) {
2628 vn_lock(vp, LK_RETRY | LK_SHARED);
2631 * With the vnode locked, check for races with
2632 * unmount, forced or not. Note that we
2633 * already verified that vp is not equal to
2634 * the root vnode, which means that
2635 * mnt_vnodecovered can be NULL only for the
2638 if (VN_IS_DOOMED(vp) ||
2639 (vp1 = vp->v_mount->mnt_vnodecovered) == NULL ||
2640 vp1->v_mountedhere != vp->v_mount) {
2643 SDT_PROBE3(vfs, namecache, fullpath, return,
2653 if (vp->v_type != VDIR) {
2655 counter_u64_add(numfullpathfail1, 1);
2657 SDT_PROBE3(vfs, namecache, fullpath, return,
2661 error = vn_vptocnp(&vp, curthread->td_ucred, buf, &buflen);
2667 SDT_PROBE3(vfs, namecache, fullpath, return, error,
2671 buf[--buflen] = '/';
2672 slash_prefixed = true;
2676 if (!slash_prefixed) {
2679 counter_u64_add(numfullpathfail4, 1);
2680 SDT_PROBE3(vfs, namecache, fullpath, return, ENOMEM,
2684 buf[--buflen] = '/';
2686 counter_u64_add(numfullpathfound, 1);
2689 *retbuf = buf + buflen;
2690 SDT_PROBE3(vfs, namecache, fullpath, return, 0, startvp, *retbuf);
2697 * Resolve an arbitrary vnode to a pathname.
2700 * - hardlinks are not tracked, thus if the vnode is not a directory this can
2701 * resolve to a different path than the one used to find it
2702 * - namecache is not mandatory, meaning names are not guaranteed to be added
2703 * (in which case resolving fails)
2705 static void __inline
2706 cache_rev_failed_impl(int *reason, int line)
2711 #define cache_rev_failed(var) cache_rev_failed_impl((var), __LINE__)
2714 vn_fullpath_any_smr(struct vnode *vp, struct vnode *rdir, char *buf,
2715 char **retbuf, size_t *buflen, bool slash_prefixed, size_t addend)
2717 #ifdef KDTRACE_HOOKS
2718 struct vnode *startvp = vp;
2722 struct namecache *ncp;
2726 #ifdef KDTRACE_HOOKS
2729 seqc_t vp_seqc, tvp_seqc;
2732 VFS_SMR_ASSERT_ENTERED();
2734 if (!cache_fast_revlookup) {
2739 orig_buflen = *buflen;
2741 if (!slash_prefixed) {
2742 MPASS(*buflen >= 2);
2744 buf[*buflen] = '\0';
2747 if (vp == rdir || vp == rootvnode) {
2748 if (!slash_prefixed) {
2755 #ifdef KDTRACE_HOOKS
2759 ncp = NULL; /* for sdt probe down below */
2760 vp_seqc = vn_seqc_read_any(vp);
2761 if (seqc_in_modify(vp_seqc)) {
2762 cache_rev_failed(&reason);
2767 #ifdef KDTRACE_HOOKS
2770 if ((vp->v_vflag & VV_ROOT) != 0) {
2771 mp = atomic_load_ptr(&vp->v_mount);
2773 cache_rev_failed(&reason);
2776 tvp = atomic_load_ptr(&mp->mnt_vnodecovered);
2777 tvp_seqc = vn_seqc_read_any(tvp);
2778 if (seqc_in_modify(tvp_seqc)) {
2779 cache_rev_failed(&reason);
2782 if (!vn_seqc_consistent(vp, vp_seqc)) {
2783 cache_rev_failed(&reason);
2790 ncp = atomic_load_ptr(&vp->v_cache_dd);
2792 cache_rev_failed(&reason);
2795 nc_flag = atomic_load_char(&ncp->nc_flag);
2796 if ((nc_flag & NCF_ISDOTDOT) != 0) {
2797 cache_rev_failed(&reason);
2800 if (!cache_ncp_canuse(ncp)) {
2801 cache_rev_failed(&reason);
2804 if (ncp->nc_nlen >= *buflen) {
2805 cache_rev_failed(&reason);
2809 *buflen -= ncp->nc_nlen;
2810 memcpy(buf + *buflen, ncp->nc_name, ncp->nc_nlen);
2814 tvp_seqc = vn_seqc_read_any(tvp);
2815 if (seqc_in_modify(tvp_seqc)) {
2816 cache_rev_failed(&reason);
2819 if (!vn_seqc_consistent(vp, vp_seqc)) {
2820 cache_rev_failed(&reason);
2825 if (vp == rdir || vp == rootvnode)
2830 *retbuf = buf + *buflen;
2831 *buflen = orig_buflen - *buflen + addend;
2832 SDT_PROBE2(vfs, namecache, fullpath_smr, hit, startvp, *retbuf);
2836 *buflen = orig_buflen;
2837 SDT_PROBE4(vfs, namecache, fullpath_smr, miss, startvp, ncp, reason, i);
2843 vn_fullpath_any(struct vnode *vp, struct vnode *rdir, char *buf, char **retbuf,
2847 bool slash_prefixed;
2853 orig_buflen = *buflen;
2856 slash_prefixed = false;
2857 if (vp->v_type != VDIR) {
2859 buf[*buflen] = '\0';
2860 error = vn_vptocnp(&vp, curthread->td_ucred, buf, buflen);
2869 slash_prefixed = true;
2872 return (vn_fullpath_dir(vp, rdir, buf, retbuf, buflen, slash_prefixed,
2873 orig_buflen - *buflen));
2877 * Resolve an arbitrary vnode to a pathname (taking care of hardlinks).
2879 * Since the namecache does not track handlings, the caller is expected to first
2880 * look up the target vnode with SAVENAME | WANTPARENT flags passed to namei.
2882 * Then we have 2 cases:
2883 * - if the found vnode is a directory, the path can be constructed just by
2884 * fullowing names up the chain
2885 * - otherwise we populate the buffer with the saved name and start resolving
2889 vn_fullpath_hardlink(struct nameidata *ndp, char **retbuf, char **freebuf,
2894 struct componentname *cnp;
2898 bool slash_prefixed;
2903 if (*buflen > MAXPATHLEN)
2904 *buflen = MAXPATHLEN;
2906 slash_prefixed = false;
2908 buf = malloc(*buflen, M_TEMP, M_WAITOK);
2913 * Check for VBAD to work around the vp_crossmp bug in lookup().
2915 * For example consider tmpfs on /tmp and realpath /tmp. ni_vp will be
2916 * set to mount point's root vnode while ni_dvp will be vp_crossmp.
2917 * If the type is VDIR (like in this very case) we can skip looking
2918 * at ni_dvp in the first place. However, since vnodes get passed here
2919 * unlocked the target may transition to doomed state (type == VBAD)
2920 * before we get to evaluate the condition. If this happens, we will
2921 * populate part of the buffer and descend to vn_fullpath_dir with
2922 * vp == vp_crossmp. Prevent the problem by checking for VBAD.
2924 * This should be atomic_load(&vp->v_type) but it is ilegal to take
2925 * an address of a bit field, even if said field is sized to char.
2926 * Work around the problem by reading the value into a full-sized enum
2927 * and then re-reading it with atomic_load which will still prevent
2928 * the compiler from re-reading down the road.
2931 type = atomic_load_int(&type);
2938 addend = cnp->cn_namelen + 2;
2939 if (*buflen < addend) {
2944 tmpbuf = buf + *buflen;
2946 memcpy(&tmpbuf[1], cnp->cn_nameptr, cnp->cn_namelen);
2947 tmpbuf[addend - 1] = '\0';
2948 slash_prefixed = true;
2953 pwd = pwd_get_smr();
2954 error = vn_fullpath_any_smr(vp, pwd->pwd_rdir, buf, retbuf, buflen,
2955 slash_prefixed, addend);
2956 VFS_SMR_ASSERT_NOT_ENTERED();
2958 pwd = pwd_hold(curthread);
2960 error = vn_fullpath_dir(vp, pwd->pwd_rdir, buf, retbuf, buflen,
2961 slash_prefixed, addend);
2976 vn_dir_dd_ino(struct vnode *vp)
2978 struct namecache *ncp;
2983 ASSERT_VOP_LOCKED(vp, "vn_dir_dd_ino");
2984 vlp = VP2VNODELOCK(vp);
2986 TAILQ_FOREACH(ncp, &(vp->v_cache_dst), nc_dst) {
2987 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0)
2990 vs = vget_prep(ddvp);
2992 if (vget_finish(ddvp, LK_SHARED | LK_NOWAIT, vs))
3001 vn_commname(struct vnode *vp, char *buf, u_int buflen)
3003 struct namecache *ncp;
3007 vlp = VP2VNODELOCK(vp);
3009 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst)
3010 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
3016 l = min(ncp->nc_nlen, buflen - 1);
3017 memcpy(buf, ncp->nc_name, l);
3024 * This function updates path string to vnode's full global path
3025 * and checks the size of the new path string against the pathlen argument.
3027 * Requires a locked, referenced vnode.
3028 * Vnode is re-locked on success or ENODEV, otherwise unlocked.
3030 * If vp is a directory, the call to vn_fullpath_global() always succeeds
3031 * because it falls back to the ".." lookup if the namecache lookup fails.
3034 vn_path_to_global_path(struct thread *td, struct vnode *vp, char *path,
3037 struct nameidata nd;
3042 ASSERT_VOP_ELOCKED(vp, __func__);
3044 /* Construct global filesystem path from vp. */
3046 error = vn_fullpath_global(vp, &rpath, &fbuf);
3053 if (strlen(rpath) >= pathlen) {
3055 error = ENAMETOOLONG;
3060 * Re-lookup the vnode by path to detect a possible rename.
3061 * As a side effect, the vnode is relocked.
3062 * If vnode was renamed, return ENOENT.
3064 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
3065 UIO_SYSSPACE, path, td);
3071 NDFREE(&nd, NDF_ONLY_PNBUF);
3075 strcpy(path, rpath);
3088 db_print_vpath(struct vnode *vp)
3091 while (vp != NULL) {
3092 db_printf("%p: ", vp);
3093 if (vp == rootvnode) {
3097 if (vp->v_vflag & VV_ROOT) {
3098 db_printf("<mount point>");
3099 vp = vp->v_mount->mnt_vnodecovered;
3101 struct namecache *ncp;
3105 ncp = TAILQ_FIRST(&vp->v_cache_dst);
3108 for (i = 0; i < ncp->nc_nlen; i++)
3109 db_printf("%c", *ncn++);
3122 DB_SHOW_COMMAND(vpath, db_show_vpath)
3127 db_printf("usage: show vpath <struct vnode *>\n");
3131 vp = (struct vnode *)addr;
3137 static bool __read_frequently cache_fast_lookup = true;
3138 SYSCTL_BOOL(_vfs, OID_AUTO, cache_fast_lookup, CTLFLAG_RW,
3139 &cache_fast_lookup, 0, "");
3141 #define CACHE_FPL_FAILED -2020
3144 cache_fpl_cleanup_cnp(struct componentname *cnp)
3147 uma_zfree(namei_zone, cnp->cn_pnbuf);
3149 cnp->cn_pnbuf = NULL;
3150 cnp->cn_nameptr = NULL;
3155 cache_fpl_handle_root(struct nameidata *ndp, struct vnode **dpp)
3157 struct componentname *cnp;
3160 while (*(cnp->cn_nameptr) == '/') {
3165 *dpp = ndp->ni_rootdir;
3169 * Components of nameidata (or objects it can point to) which may
3170 * need restoring in case fast path lookup fails.
3172 struct nameidata_saved {
3180 struct nameidata *ndp;
3181 struct componentname *cnp;
3187 struct nameidata_saved snd;
3189 enum cache_fpl_status status:8;
3195 cache_fpl_checkpoint(struct cache_fpl *fpl, struct nameidata_saved *snd)
3198 snd->cn_flags = fpl->ndp->ni_cnd.cn_flags;
3199 snd->cn_namelen = fpl->ndp->ni_cnd.cn_namelen;
3200 snd->cn_nameptr = fpl->ndp->ni_cnd.cn_nameptr;
3201 snd->ni_pathlen = fpl->ndp->ni_pathlen;
3205 cache_fpl_restore(struct cache_fpl *fpl, struct nameidata_saved *snd)
3208 fpl->ndp->ni_cnd.cn_flags = snd->cn_flags;
3209 fpl->ndp->ni_cnd.cn_namelen = snd->cn_namelen;
3210 fpl->ndp->ni_cnd.cn_nameptr = snd->cn_nameptr;
3211 fpl->ndp->ni_pathlen = snd->ni_pathlen;
3215 #define cache_fpl_smr_assert_entered(fpl) ({ \
3216 struct cache_fpl *_fpl = (fpl); \
3217 MPASS(_fpl->in_smr == true); \
3218 VFS_SMR_ASSERT_ENTERED(); \
3220 #define cache_fpl_smr_assert_not_entered(fpl) ({ \
3221 struct cache_fpl *_fpl = (fpl); \
3222 MPASS(_fpl->in_smr == false); \
3223 VFS_SMR_ASSERT_NOT_ENTERED(); \
3226 #define cache_fpl_smr_assert_entered(fpl) do { } while (0)
3227 #define cache_fpl_smr_assert_not_entered(fpl) do { } while (0)
3230 #define cache_fpl_smr_enter_initial(fpl) ({ \
3231 struct cache_fpl *_fpl = (fpl); \
3233 _fpl->in_smr = true; \
3236 #define cache_fpl_smr_enter(fpl) ({ \
3237 struct cache_fpl *_fpl = (fpl); \
3238 MPASS(_fpl->in_smr == false); \
3240 _fpl->in_smr = true; \
3243 #define cache_fpl_smr_exit(fpl) ({ \
3244 struct cache_fpl *_fpl = (fpl); \
3245 MPASS(_fpl->in_smr == true); \
3247 _fpl->in_smr = false; \
3251 cache_fpl_aborted_impl(struct cache_fpl *fpl, int line)
3254 if (fpl->status != CACHE_FPL_STATUS_UNSET) {
3255 KASSERT(fpl->status == CACHE_FPL_STATUS_PARTIAL,
3256 ("%s: converting to abort from %d at %d, set at %d\n",
3257 __func__, fpl->status, line, fpl->line));
3259 fpl->status = CACHE_FPL_STATUS_ABORTED;
3261 return (CACHE_FPL_FAILED);
3264 #define cache_fpl_aborted(x) cache_fpl_aborted_impl((x), __LINE__)
3267 cache_fpl_partial_impl(struct cache_fpl *fpl, int line)
3270 KASSERT(fpl->status == CACHE_FPL_STATUS_UNSET,
3271 ("%s: setting to partial at %d, but already set to %d at %d\n",
3272 __func__, line, fpl->status, fpl->line));
3273 cache_fpl_smr_assert_entered(fpl);
3274 fpl->status = CACHE_FPL_STATUS_PARTIAL;
3276 return (CACHE_FPL_FAILED);
3279 #define cache_fpl_partial(x) cache_fpl_partial_impl((x), __LINE__)
3282 cache_fpl_handled_impl(struct cache_fpl *fpl, int error, int line)
3285 KASSERT(fpl->status == CACHE_FPL_STATUS_UNSET,
3286 ("%s: setting to handled at %d, but already set to %d at %d\n",
3287 __func__, line, fpl->status, fpl->line));
3288 cache_fpl_smr_assert_not_entered(fpl);
3289 MPASS(error != CACHE_FPL_FAILED);
3290 fpl->status = CACHE_FPL_STATUS_HANDLED;
3295 #define cache_fpl_handled(x, e) cache_fpl_handled_impl((x), (e), __LINE__)
3297 #define CACHE_FPL_SUPPORTED_CN_FLAGS \
3298 (LOCKLEAF | LOCKPARENT | WANTPARENT | NOCACHE | FOLLOW | LOCKSHARED | SAVENAME | \
3299 SAVESTART | WILLBEDIR | ISOPEN | NOMACCHECK | AUDITVNODE1 | AUDITVNODE2 | NOCAPCHECK)
3301 #define CACHE_FPL_INTERNAL_CN_FLAGS \
3302 (ISDOTDOT | MAKEENTRY | ISLASTCN)
3304 _Static_assert((CACHE_FPL_SUPPORTED_CN_FLAGS & CACHE_FPL_INTERNAL_CN_FLAGS) == 0,
3305 "supported and internal flags overlap");
3308 cache_fpl_islastcn(struct nameidata *ndp)
3311 return (*ndp->ni_next == 0);
3315 cache_fpl_isdotdot(struct componentname *cnp)
3318 if (cnp->cn_namelen == 2 &&
3319 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
3325 cache_can_fplookup(struct cache_fpl *fpl)
3327 struct nameidata *ndp;
3328 struct componentname *cnp;
3333 td = cnp->cn_thread;
3335 if (!cache_fast_lookup) {
3336 cache_fpl_aborted(fpl);
3340 if (mac_vnode_check_lookup_enabled()) {
3341 cache_fpl_aborted(fpl);
3345 if ((cnp->cn_flags & ~CACHE_FPL_SUPPORTED_CN_FLAGS) != 0) {
3346 cache_fpl_aborted(fpl);
3349 if (IN_CAPABILITY_MODE(td)) {
3350 cache_fpl_aborted(fpl);
3353 if (AUDITING_TD(td)) {
3354 cache_fpl_aborted(fpl);
3357 if (ndp->ni_startdir != NULL) {
3358 cache_fpl_aborted(fpl);
3365 cache_fplookup_dirfd(struct cache_fpl *fpl, struct vnode **vpp)
3367 struct nameidata *ndp;
3372 error = fgetvp_lookup_smr(ndp->ni_dirfd, ndp, vpp, &fsearch);
3373 if (__predict_false(error != 0)) {
3374 cache_fpl_smr_exit(fpl);
3375 return (cache_fpl_aborted(fpl));
3377 fpl->fsearch = fsearch;
3382 cache_fplookup_vnode_supported(struct vnode *vp)
3385 return (vp->v_type != VLNK);
3389 * Move a negative entry to the hot list.
3391 * We have to take locks, but they may be contended and in the worst
3392 * case we may need to go off CPU. We don't want to spin within the
3393 * smr section and we can't block with it. Instead we are going to
3394 * look up the entry again.
3396 static int __noinline
3397 cache_fplookup_negative_promote(struct cache_fpl *fpl, struct namecache *oncp,
3400 struct componentname *cnp;
3401 struct namecache *ncp;
3402 struct neglist *neglist;
3403 struct negstate *negstate;
3410 if (!vhold_smr(dvp))
3411 return (cache_fpl_aborted(fpl));
3413 neglist = NCP2NEGLIST(oncp);
3414 cache_fpl_smr_exit(fpl);
3416 mtx_lock(&ncneg_hot.nl_lock);
3417 mtx_lock(&neglist->nl_lock);
3419 * For hash iteration.
3421 cache_fpl_smr_enter(fpl);
3424 * Avoid all surprises by only succeeding if we got the same entry and
3425 * bailing completely otherwise.
3427 * In particular at this point there can be a new ncp which matches the
3428 * search but hashes to a different neglist.
3430 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
3436 * No match to begin with.
3438 if (__predict_false(ncp == NULL)) {
3443 * The newly found entry may be something different...
3445 if (!(ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
3446 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))) {
3451 * ... and not even negative.
3453 nc_flag = atomic_load_char(&ncp->nc_flag);
3454 if ((nc_flag & NCF_NEGATIVE) == 0) {
3458 if (__predict_false(!cache_ncp_canuse(ncp))) {
3462 negstate = NCP2NEGSTATE(ncp);
3463 if ((negstate->neg_flag & NEG_HOT) == 0) {
3465 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
3466 TAILQ_INSERT_TAIL(&ncneg_hot.nl_list, ncp, nc_dst);
3467 negstate->neg_flag |= NEG_HOT;
3470 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp, ncp->nc_name);
3471 counter_u64_add(numneghits, 1);
3472 cache_fpl_smr_exit(fpl);
3473 mtx_unlock(&neglist->nl_lock);
3474 mtx_unlock(&ncneg_hot.nl_lock);
3476 return (cache_fpl_handled(fpl, ENOENT));
3478 cache_fpl_smr_exit(fpl);
3479 mtx_unlock(&neglist->nl_lock);
3480 mtx_unlock(&ncneg_hot.nl_lock);
3482 return (cache_fpl_aborted(fpl));
3486 * The target vnode is not supported, prepare for the slow path to take over.
3488 static int __noinline
3489 cache_fplookup_partial_setup(struct cache_fpl *fpl)
3491 struct nameidata *ndp;
3492 struct componentname *cnp;
3502 dvp_seqc = fpl->dvp_seqc;
3504 if (!pwd_hold_smr(pwd)) {
3505 cache_fpl_smr_exit(fpl);
3506 return (cache_fpl_aborted(fpl));
3509 dvs = vget_prep_smr(dvp);
3510 cache_fpl_smr_exit(fpl);
3511 if (__predict_false(dvs == VGET_NONE)) {
3513 return (cache_fpl_aborted(fpl));
3516 vget_finish_ref(dvp, dvs);
3517 if (!vn_seqc_consistent(dvp, dvp_seqc)) {
3520 return (cache_fpl_aborted(fpl));
3523 cache_fpl_restore(fpl, &fpl->snd);
3525 ndp->ni_startdir = dvp;
3526 cnp->cn_flags |= MAKEENTRY;
3527 if (cache_fpl_islastcn(ndp))
3528 cnp->cn_flags |= ISLASTCN;
3529 if (cache_fpl_isdotdot(cnp))
3530 cnp->cn_flags |= ISDOTDOT;
3536 cache_fplookup_final_child(struct cache_fpl *fpl, enum vgetstate tvs)
3538 struct componentname *cnp;
3545 tvp_seqc = fpl->tvp_seqc;
3547 if ((cnp->cn_flags & LOCKLEAF) != 0) {
3548 lkflags = LK_SHARED;
3549 if ((cnp->cn_flags & LOCKSHARED) == 0)
3550 lkflags = LK_EXCLUSIVE;
3551 error = vget_finish(tvp, lkflags, tvs);
3552 if (__predict_false(error != 0)) {
3553 return (cache_fpl_aborted(fpl));
3556 vget_finish_ref(tvp, tvs);
3559 if (!vn_seqc_consistent(tvp, tvp_seqc)) {
3560 if ((cnp->cn_flags & LOCKLEAF) != 0)
3564 return (cache_fpl_aborted(fpl));
3567 return (cache_fpl_handled(fpl, 0));
3571 * They want to possibly modify the state of the namecache.
3573 * Don't try to match the API contract, just leave.
3574 * TODO: this leaves scalability on the table
3577 cache_fplookup_final_modifying(struct cache_fpl *fpl)
3579 struct componentname *cnp;
3582 MPASS(cnp->cn_nameiop != LOOKUP);
3583 return (cache_fpl_partial(fpl));
3586 static int __noinline
3587 cache_fplookup_final_withparent(struct cache_fpl *fpl)
3589 struct componentname *cnp;
3590 enum vgetstate dvs, tvs;
3591 struct vnode *dvp, *tvp;
3597 dvp_seqc = fpl->dvp_seqc;
3600 MPASS((cnp->cn_flags & (LOCKPARENT|WANTPARENT)) != 0);
3603 * This is less efficient than it can be for simplicity.
3605 dvs = vget_prep_smr(dvp);
3606 if (__predict_false(dvs == VGET_NONE)) {
3607 return (cache_fpl_aborted(fpl));
3609 tvs = vget_prep_smr(tvp);
3610 if (__predict_false(tvs == VGET_NONE)) {
3611 cache_fpl_smr_exit(fpl);
3612 vget_abort(dvp, dvs);
3613 return (cache_fpl_aborted(fpl));
3616 cache_fpl_smr_exit(fpl);
3618 if ((cnp->cn_flags & LOCKPARENT) != 0) {
3619 error = vget_finish(dvp, LK_EXCLUSIVE, dvs);
3620 if (__predict_false(error != 0)) {
3621 vget_abort(tvp, tvs);
3622 return (cache_fpl_aborted(fpl));
3625 vget_finish_ref(dvp, dvs);
3628 if (!vn_seqc_consistent(dvp, dvp_seqc)) {
3629 vget_abort(tvp, tvs);
3630 if ((cnp->cn_flags & LOCKPARENT) != 0)
3634 return (cache_fpl_aborted(fpl));
3637 error = cache_fplookup_final_child(fpl, tvs);
3638 if (__predict_false(error != 0)) {
3639 MPASS(fpl->status == CACHE_FPL_STATUS_ABORTED);
3640 if ((cnp->cn_flags & LOCKPARENT) != 0)
3647 MPASS(fpl->status == CACHE_FPL_STATUS_HANDLED);
3652 cache_fplookup_final(struct cache_fpl *fpl)
3654 struct componentname *cnp;
3656 struct vnode *dvp, *tvp;
3661 dvp_seqc = fpl->dvp_seqc;
3664 VNPASS(cache_fplookup_vnode_supported(dvp), dvp);
3666 if (cnp->cn_nameiop != LOOKUP) {
3667 return (cache_fplookup_final_modifying(fpl));
3670 if ((cnp->cn_flags & (LOCKPARENT|WANTPARENT)) != 0)
3671 return (cache_fplookup_final_withparent(fpl));
3673 tvs = vget_prep_smr(tvp);
3674 if (__predict_false(tvs == VGET_NONE)) {
3675 return (cache_fpl_partial(fpl));
3678 if (!vn_seqc_consistent(dvp, dvp_seqc)) {
3679 cache_fpl_smr_exit(fpl);
3680 vget_abort(tvp, tvs);
3681 return (cache_fpl_aborted(fpl));
3684 cache_fpl_smr_exit(fpl);
3685 return (cache_fplookup_final_child(fpl, tvs));
3688 static int __noinline
3689 cache_fplookup_dot(struct cache_fpl *fpl)
3696 fpl->tvp_seqc = vn_seqc_read_any(dvp);
3697 if (seqc_in_modify(fpl->tvp_seqc)) {
3698 return (cache_fpl_aborted(fpl));
3701 counter_u64_add(dothits, 1);
3702 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", dvp);
3707 static int __noinline
3708 cache_fplookup_dotdot(struct cache_fpl *fpl)
3710 struct nameidata *ndp;
3711 struct componentname *cnp;
3712 struct namecache *ncp;
3722 * XXX this is racy the same way regular lookup is
3724 for (pr = cnp->cn_cred->cr_prison; pr != NULL;
3726 if (dvp == pr->pr_root)
3729 if (dvp == ndp->ni_rootdir ||
3730 dvp == ndp->ni_topdir ||
3734 fpl->tvp_seqc = vn_seqc_read_any(dvp);
3735 if (seqc_in_modify(fpl->tvp_seqc)) {
3736 return (cache_fpl_aborted(fpl));
3741 if ((dvp->v_vflag & VV_ROOT) != 0) {
3744 * The opposite of climb mount is needed here.
3746 return (cache_fpl_aborted(fpl));
3749 ncp = atomic_load_ptr(&dvp->v_cache_dd);
3751 return (cache_fpl_aborted(fpl));
3754 nc_flag = atomic_load_char(&ncp->nc_flag);
3755 if ((nc_flag & NCF_ISDOTDOT) != 0) {
3756 if ((nc_flag & NCF_NEGATIVE) != 0)
3757 return (cache_fpl_aborted(fpl));
3758 fpl->tvp = ncp->nc_vp;
3760 fpl->tvp = ncp->nc_dvp;
3763 if (__predict_false(!cache_ncp_canuse(ncp))) {
3764 return (cache_fpl_aborted(fpl));
3767 fpl->tvp_seqc = vn_seqc_read_any(fpl->tvp);
3768 if (seqc_in_modify(fpl->tvp_seqc)) {
3769 return (cache_fpl_partial(fpl));
3772 counter_u64_add(dotdothits, 1);
3777 cache_fplookup_next(struct cache_fpl *fpl)
3779 struct componentname *cnp;
3780 struct namecache *ncp;
3781 struct negstate *negstate;
3782 struct vnode *dvp, *tvp;
3790 if (__predict_false(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.')) {
3791 return (cache_fplookup_dot(fpl));
3794 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
3796 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
3797 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
3798 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
3803 * If there is no entry we have to punt to the slow path to perform
3804 * actual lookup. Should there be nothing with this name a negative
3805 * entry will be created.
3807 if (__predict_false(ncp == NULL)) {
3808 return (cache_fpl_partial(fpl));
3811 tvp = atomic_load_ptr(&ncp->nc_vp);
3812 nc_flag = atomic_load_char(&ncp->nc_flag);
3813 if ((nc_flag & NCF_NEGATIVE) != 0) {
3815 * If they want to create an entry we need to replace this one.
3817 if (__predict_false(fpl->cnp->cn_nameiop != LOOKUP)) {
3818 return (cache_fpl_partial(fpl));
3820 negstate = NCP2NEGSTATE(ncp);
3821 neg_hot = ((negstate->neg_flag & NEG_HOT) != 0);
3822 if (__predict_false(!cache_ncp_canuse(ncp))) {
3823 return (cache_fpl_partial(fpl));
3825 if (__predict_false((nc_flag & NCF_WHITE) != 0)) {
3826 return (cache_fpl_partial(fpl));
3829 return (cache_fplookup_negative_promote(fpl, ncp, hash));
3831 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp,
3833 counter_u64_add(numneghits, 1);
3834 cache_fpl_smr_exit(fpl);
3835 return (cache_fpl_handled(fpl, ENOENT));
3838 if (__predict_false(!cache_ncp_canuse(ncp))) {
3839 return (cache_fpl_partial(fpl));
3843 fpl->tvp_seqc = vn_seqc_read_any(tvp);
3844 if (seqc_in_modify(fpl->tvp_seqc)) {
3845 return (cache_fpl_partial(fpl));
3848 if (!cache_fplookup_vnode_supported(tvp)) {
3849 return (cache_fpl_partial(fpl));
3852 counter_u64_add(numposhits, 1);
3853 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name, tvp);
3858 cache_fplookup_mp_supported(struct mount *mp)
3863 if ((mp->mnt_kern_flag & MNTK_FPLOOKUP) == 0)
3869 * Walk up the mount stack (if any).
3871 * Correctness is provided in the following ways:
3872 * - all vnodes are protected from freeing with SMR
3873 * - struct mount objects are type stable making them always safe to access
3874 * - stability of the particular mount is provided by busying it
3875 * - relationship between the vnode which is mounted on and the mount is
3876 * verified with the vnode sequence counter after busying
3877 * - association between root vnode of the mount and the mount is protected
3880 * From that point on we can read the sequence counter of the root vnode
3881 * and get the next mount on the stack (if any) using the same protection.
3883 * By the end of successful walk we are guaranteed the reached state was
3884 * indeed present at least at some point which matches the regular lookup.
3886 static int __noinline
3887 cache_fplookup_climb_mount(struct cache_fpl *fpl)
3889 struct mount *mp, *prev_mp;
3894 vp_seqc = fpl->tvp_seqc;
3896 VNPASS(vp->v_type == VDIR || vp->v_type == VBAD, vp);
3897 mp = atomic_load_ptr(&vp->v_mountedhere);
3903 if (!vfs_op_thread_enter_crit(mp)) {
3904 if (prev_mp != NULL)
3905 vfs_op_thread_exit_crit(prev_mp);
3906 return (cache_fpl_partial(fpl));
3908 if (prev_mp != NULL)
3909 vfs_op_thread_exit_crit(prev_mp);
3910 if (!vn_seqc_consistent(vp, vp_seqc)) {
3911 vfs_op_thread_exit_crit(mp);
3912 return (cache_fpl_partial(fpl));
3914 if (!cache_fplookup_mp_supported(mp)) {
3915 vfs_op_thread_exit_crit(mp);
3916 return (cache_fpl_partial(fpl));
3918 vp = atomic_load_ptr(&mp->mnt_rootvnode);
3919 if (vp == NULL || VN_IS_DOOMED(vp)) {
3920 vfs_op_thread_exit_crit(mp);
3921 return (cache_fpl_partial(fpl));
3923 vp_seqc = vn_seqc_read_any(vp);
3924 if (seqc_in_modify(vp_seqc)) {
3925 vfs_op_thread_exit_crit(mp);
3926 return (cache_fpl_partial(fpl));
3929 mp = atomic_load_ptr(&vp->v_mountedhere);
3934 vfs_op_thread_exit_crit(prev_mp);
3936 fpl->tvp_seqc = vp_seqc;
3941 cache_fplookup_need_climb_mount(struct cache_fpl *fpl)
3949 * Hack: while this is a union, the pointer tends to be NULL so save on
3952 mp = atomic_load_ptr(&vp->v_mountedhere);
3955 if (vp->v_type == VDIR)
3963 * The code was originally copy-pasted from regular lookup and despite
3964 * clean ups leaves performance on the table. Any modifications here
3965 * must take into account that in case off fallback the resulting
3966 * nameidata state has to be compatible with the original.
3969 cache_fplookup_parse(struct cache_fpl *fpl)
3971 struct nameidata *ndp;
3972 struct componentname *cnp;
3979 * Search a new directory.
3981 * The last component of the filename is left accessible via
3982 * cnp->cn_nameptr for callers that need the name. Callers needing
3983 * the name set the SAVENAME flag. When done, they assume
3984 * responsibility for freeing the pathname buffer.
3986 for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
3988 cnp->cn_namelen = cp - cnp->cn_nameptr;
3989 if (__predict_false(cnp->cn_namelen > NAME_MAX)) {
3990 cache_fpl_smr_exit(fpl);
3991 return (cache_fpl_handled(fpl, ENAMETOOLONG));
3993 ndp->ni_pathlen -= cnp->cn_namelen;
3994 KASSERT(ndp->ni_pathlen <= PATH_MAX,
3995 ("%s: ni_pathlen underflow to %zd\n", __func__, ndp->ni_pathlen));
3999 * Replace multiple slashes by a single slash and trailing slashes
4000 * by a null. This must be done before VOP_LOOKUP() because some
4001 * fs's don't know about trailing slashes. Remember if there were
4002 * trailing slashes to handle symlinks, existing non-directories
4003 * and non-existing files that won't be directories specially later.
4005 while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
4011 * Regular lookup performs the following:
4012 * *ndp->ni_next = '\0';
4013 * cnp->cn_flags |= TRAILINGSLASH;
4015 * Which is problematic since it modifies data read
4016 * from userspace. Then if fast path lookup was to
4017 * abort we would have to either restore it or convey
4018 * the flag. Since this is a corner case just ignore
4019 * it for simplicity.
4021 return (cache_fpl_partial(fpl));
4027 * Check for degenerate name (e.g. / or "")
4028 * which is a way of talking about a directory,
4029 * e.g. like "/." or ".".
4032 * Another corner case handled by the regular lookup
4034 if (__predict_false(cnp->cn_nameptr[0] == '\0')) {
4035 return (cache_fpl_partial(fpl));
4041 cache_fplookup_parse_advance(struct cache_fpl *fpl)
4043 struct nameidata *ndp;
4044 struct componentname *cnp;
4049 cnp->cn_nameptr = ndp->ni_next;
4050 while (*cnp->cn_nameptr == '/') {
4057 * See the API contract for VOP_FPLOOKUP_VEXEC.
4059 static int __noinline
4060 cache_fplookup_failed_vexec(struct cache_fpl *fpl, int error)
4062 struct componentname *cnp;
4068 dvp_seqc = fpl->dvp_seqc;
4071 * Hack: they may be looking up foo/bar, where foo is a
4072 * regular file. In such a case we need to turn ENOTDIR,
4073 * but we may happen to get here with a different error.
4075 if (dvp->v_type != VDIR) {
4077 * The check here is predominantly to catch
4078 * EOPNOTSUPP from dead_vnodeops. If the vnode
4079 * gets doomed past this point it is going to
4080 * fail seqc verification.
4082 if (VN_IS_DOOMED(dvp)) {
4083 return (cache_fpl_aborted(fpl));
4089 * Hack: handle O_SEARCH.
4091 * Open Group Base Specifications Issue 7, 2018 edition states:
4092 * If the access mode of the open file description associated with the
4093 * file descriptor is not O_SEARCH, the function shall check whether
4094 * directory searches are permitted using the current permissions of
4095 * the directory underlying the file descriptor. If the access mode is
4096 * O_SEARCH, the function shall not perform the check.
4098 * Regular lookup tests for the NOEXECCHECK flag for every path
4099 * component to decide whether to do the permission check. However,
4100 * since most lookups never have the flag (and when they do it is only
4101 * present for the first path component), lockless lookup only acts on
4102 * it if there is a permission problem. Here the flag is represented
4103 * with a boolean so that we don't have to clear it on the way out.
4105 * For simplicity this always aborts.
4106 * TODO: check if this is the first lookup and ignore the permission
4107 * problem. Note the flag has to survive fallback (if it happens to be
4111 return (cache_fpl_aborted(fpl));
4116 if (!vn_seqc_consistent(dvp, dvp_seqc)) {
4117 error = cache_fpl_aborted(fpl);
4119 cache_fpl_partial(fpl);
4123 if (!vn_seqc_consistent(dvp, dvp_seqc)) {
4124 error = cache_fpl_aborted(fpl);
4126 cache_fpl_smr_exit(fpl);
4127 cache_fpl_handled(fpl, error);
4135 cache_fplookup_impl(struct vnode *dvp, struct cache_fpl *fpl)
4137 struct nameidata *ndp;
4138 struct componentname *cnp;
4142 error = CACHE_FPL_FAILED;
4146 cache_fpl_checkpoint(fpl, &fpl->snd);
4149 fpl->dvp_seqc = vn_seqc_read_any(fpl->dvp);
4150 if (seqc_in_modify(fpl->dvp_seqc)) {
4151 cache_fpl_aborted(fpl);
4154 mp = atomic_load_ptr(&fpl->dvp->v_mount);
4155 if (!cache_fplookup_mp_supported(mp)) {
4156 cache_fpl_aborted(fpl);
4160 VNPASS(cache_fplookup_vnode_supported(fpl->dvp), fpl->dvp);
4163 error = cache_fplookup_parse(fpl);
4164 if (__predict_false(error != 0)) {
4168 VNPASS(cache_fplookup_vnode_supported(fpl->dvp), fpl->dvp);
4170 error = VOP_FPLOOKUP_VEXEC(fpl->dvp, cnp->cn_cred);
4171 if (__predict_false(error != 0)) {
4172 error = cache_fplookup_failed_vexec(fpl, error);
4176 if (__predict_false(cache_fpl_isdotdot(cnp))) {
4177 error = cache_fplookup_dotdot(fpl);
4178 if (__predict_false(error != 0)) {
4182 error = cache_fplookup_next(fpl);
4183 if (__predict_false(error != 0)) {
4187 VNPASS(!seqc_in_modify(fpl->tvp_seqc), fpl->tvp);
4189 if (cache_fplookup_need_climb_mount(fpl)) {
4190 error = cache_fplookup_climb_mount(fpl);
4191 if (__predict_false(error != 0)) {
4197 VNPASS(!seqc_in_modify(fpl->tvp_seqc), fpl->tvp);
4199 if (cache_fpl_islastcn(ndp)) {
4200 error = cache_fplookup_final(fpl);
4204 if (!vn_seqc_consistent(fpl->dvp, fpl->dvp_seqc)) {
4205 error = cache_fpl_aborted(fpl);
4209 fpl->dvp = fpl->tvp;
4210 fpl->dvp_seqc = fpl->tvp_seqc;
4212 cache_fplookup_parse_advance(fpl);
4213 cache_fpl_checkpoint(fpl, &fpl->snd);
4216 switch (fpl->status) {
4217 case CACHE_FPL_STATUS_UNSET:
4218 __assert_unreachable();
4220 case CACHE_FPL_STATUS_PARTIAL:
4221 cache_fpl_smr_assert_entered(fpl);
4222 return (cache_fplookup_partial_setup(fpl));
4223 case CACHE_FPL_STATUS_ABORTED:
4225 cache_fpl_smr_exit(fpl);
4226 return (CACHE_FPL_FAILED);
4227 case CACHE_FPL_STATUS_HANDLED:
4228 MPASS(error != CACHE_FPL_FAILED);
4229 cache_fpl_smr_assert_not_entered(fpl);
4230 if (__predict_false(error != 0)) {
4233 cache_fpl_cleanup_cnp(cnp);
4236 ndp->ni_dvp = fpl->dvp;
4237 ndp->ni_vp = fpl->tvp;
4238 if (cnp->cn_flags & SAVENAME)
4239 cnp->cn_flags |= HASBUF;
4241 cache_fpl_cleanup_cnp(cnp);
4247 * Fast path lookup protected with SMR and sequence counters.
4249 * Note: all VOP_FPLOOKUP_VEXEC routines have a comment referencing this one.
4251 * Filesystems can opt in by setting the MNTK_FPLOOKUP flag and meeting criteria
4254 * Traditional vnode lookup conceptually looks like this:
4260 * vn_unlock(current);
4267 * Each jump to the next vnode is safe memory-wise and atomic with respect to
4268 * any modifications thanks to holding respective locks.
4270 * The same guarantee can be provided with a combination of safe memory
4271 * reclamation and sequence counters instead. If all operations which affect
4272 * the relationship between the current vnode and the one we are looking for
4273 * also modify the counter, we can verify whether all the conditions held as
4274 * we made the jump. This includes things like permissions, mount points etc.
4275 * Counter modification is provided by enclosing relevant places in
4276 * vn_seqc_write_begin()/end() calls.
4278 * Thus this translates to:
4281 * dvp_seqc = seqc_read_any(dvp);
4282 * if (seqc_in_modify(dvp_seqc)) // someone is altering the vnode
4286 * tvp_seqc = seqc_read_any(tvp);
4287 * if (seqc_in_modify(tvp_seqc)) // someone is altering the target vnode
4289 * if (!seqc_consistent(dvp, dvp_seqc) // someone is altering the vnode
4291 * dvp = tvp; // we know nothing of importance has changed
4292 * dvp_seqc = tvp_seqc; // store the counter for the tvp iteration
4296 * vget(); // secure the vnode
4297 * if (!seqc_consistent(tvp, tvp_seqc) // final check
4299 * // at this point we know nothing has changed for any parent<->child pair
4300 * // as they were crossed during the lookup, meaning we matched the guarantee
4301 * // of the locked variant
4304 * The API contract for VOP_FPLOOKUP_VEXEC routines is as follows:
4305 * - they are called while within vfs_smr protection which they must never exit
4306 * - EAGAIN can be returned to denote checking could not be performed, it is
4307 * always valid to return it
4308 * - if the sequence counter has not changed the result must be valid
4309 * - if the sequence counter has changed both false positives and false negatives
4310 * are permitted (since the result will be rejected later)
4311 * - for simple cases of unix permission checks vaccess_vexec_smr can be used
4313 * Caveats to watch out for:
4314 * - vnodes are passed unlocked and unreferenced with nothing stopping
4315 * VOP_RECLAIM, in turn meaning that ->v_data can become NULL. It is advised
4316 * to use atomic_load_ptr to fetch it.
4317 * - the aforementioned object can also get freed, meaning absent other means it
4318 * should be protected with vfs_smr
4319 * - either safely checking permissions as they are modified or guaranteeing
4320 * their stability is left to the routine
4323 cache_fplookup(struct nameidata *ndp, enum cache_fpl_status *status,
4326 struct cache_fpl fpl;
4329 struct componentname *cnp;
4330 struct nameidata_saved orig;
4333 MPASS(ndp->ni_lcf == 0);
4335 fpl.status = CACHE_FPL_STATUS_UNSET;
4337 fpl.cnp = &ndp->ni_cnd;
4338 MPASS(curthread == fpl.cnp->cn_thread);
4340 if ((fpl.cnp->cn_flags & SAVESTART) != 0)
4341 MPASS(fpl.cnp->cn_nameiop != LOOKUP);
4343 if (!cache_can_fplookup(&fpl)) {
4344 SDT_PROBE3(vfs, fplookup, lookup, done, ndp, fpl.line, fpl.status);
4345 *status = fpl.status;
4346 return (EOPNOTSUPP);
4349 cache_fpl_checkpoint(&fpl, &orig);
4351 cache_fpl_smr_enter_initial(&fpl);
4352 fpl.fsearch = false;
4353 pwd = pwd_get_smr();
4355 ndp->ni_rootdir = pwd->pwd_rdir;
4356 ndp->ni_topdir = pwd->pwd_jdir;
4359 cnp->cn_nameptr = cnp->cn_pnbuf;
4360 if (cnp->cn_pnbuf[0] == '/') {
4361 cache_fpl_handle_root(ndp, &dvp);
4363 if (ndp->ni_dirfd == AT_FDCWD) {
4364 dvp = pwd->pwd_cdir;
4366 error = cache_fplookup_dirfd(&fpl, &dvp);
4367 if (__predict_false(error != 0)) {
4373 SDT_PROBE4(vfs, namei, lookup, entry, dvp, cnp->cn_pnbuf, cnp->cn_flags, true);
4375 error = cache_fplookup_impl(dvp, &fpl);
4377 cache_fpl_smr_assert_not_entered(&fpl);
4378 SDT_PROBE3(vfs, fplookup, lookup, done, ndp, fpl.line, fpl.status);
4380 *status = fpl.status;
4381 switch (fpl.status) {
4382 case CACHE_FPL_STATUS_UNSET:
4383 __assert_unreachable();
4385 case CACHE_FPL_STATUS_HANDLED:
4386 SDT_PROBE3(vfs, namei, lookup, return, error,
4387 (error == 0 ? ndp->ni_vp : NULL), true);
4389 case CACHE_FPL_STATUS_PARTIAL:
4392 * Status restored by cache_fplookup_partial_setup.
4395 case CACHE_FPL_STATUS_ABORTED:
4396 cache_fpl_restore(&fpl, &orig);