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 TAILQ_HEAD(, namecache) nl_hotlist;
316 } __aligned(CACHE_LINE_SIZE);
318 static struct neglist neglists[numneglists];
320 static inline struct neglist *
321 NCP2NEGLIST(struct namecache *ncp)
324 return (&neglists[(((uintptr_t)(ncp) >> 8) & ncneghash)]);
327 static inline struct negstate *
328 NCP2NEGSTATE(struct namecache *ncp)
331 MPASS(ncp->nc_flag & NCF_NEGATIVE);
332 return (&ncp->nc_neg);
335 #define numbucketlocks (ncbuckethash + 1)
336 static u_int __read_mostly ncbuckethash;
337 static struct mtx_padalign __read_mostly *bucketlocks;
338 #define HASH2BUCKETLOCK(hash) \
339 ((struct mtx *)(&bucketlocks[((hash) & ncbuckethash)]))
341 #define numvnodelocks (ncvnodehash + 1)
342 static u_int __read_mostly ncvnodehash;
343 static struct mtx __read_mostly *vnodelocks;
344 static inline struct mtx *
345 VP2VNODELOCK(struct vnode *vp)
348 return (&vnodelocks[(((uintptr_t)(vp) >> 8) & ncvnodehash)]);
352 * UMA zones for the VFS cache.
354 * The small cache is used for entries with short names, which are the
355 * most common. The large cache is used for entries which are too big to
356 * fit in the small cache.
358 static uma_zone_t __read_mostly cache_zone_small;
359 static uma_zone_t __read_mostly cache_zone_small_ts;
360 static uma_zone_t __read_mostly cache_zone_large;
361 static uma_zone_t __read_mostly cache_zone_large_ts;
363 static struct namecache *
364 cache_alloc(int len, int ts)
366 struct namecache_ts *ncp_ts;
367 struct namecache *ncp;
369 if (__predict_false(ts)) {
370 if (len <= CACHE_PATH_CUTOFF)
371 ncp_ts = uma_zalloc_smr(cache_zone_small_ts, M_WAITOK);
373 ncp_ts = uma_zalloc_smr(cache_zone_large_ts, M_WAITOK);
374 ncp = &ncp_ts->nc_nc;
376 if (len <= CACHE_PATH_CUTOFF)
377 ncp = uma_zalloc_smr(cache_zone_small, M_WAITOK);
379 ncp = uma_zalloc_smr(cache_zone_large, M_WAITOK);
385 cache_free(struct namecache *ncp)
387 struct namecache_ts *ncp_ts;
390 if ((ncp->nc_flag & NCF_DVDROP) != 0)
392 if (__predict_false(ncp->nc_flag & NCF_TS)) {
393 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
394 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
395 uma_zfree_smr(cache_zone_small_ts, ncp_ts);
397 uma_zfree_smr(cache_zone_large_ts, ncp_ts);
399 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
400 uma_zfree_smr(cache_zone_small, ncp);
402 uma_zfree_smr(cache_zone_large, ncp);
407 cache_out_ts(struct namecache *ncp, struct timespec *tsp, int *ticksp)
409 struct namecache_ts *ncp_ts;
411 KASSERT((ncp->nc_flag & NCF_TS) != 0 ||
412 (tsp == NULL && ticksp == NULL),
418 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
419 *tsp = ncp_ts->nc_time;
420 *ticksp = ncp_ts->nc_ticks;
424 static int __read_mostly doingcache = 1; /* 1 => enable the cache */
425 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0,
426 "VFS namecache enabled");
429 /* Export size information to userland */
430 SYSCTL_INT(_debug_sizeof, OID_AUTO, namecache, CTLFLAG_RD, SYSCTL_NULL_INT_PTR,
431 sizeof(struct namecache), "sizeof(struct namecache)");
434 * The new name cache statistics
436 static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
437 "Name cache statistics");
438 #define STATNODE_ULONG(name, descr) \
439 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, descr);
440 #define STATNODE_COUNTER(name, descr) \
441 static COUNTER_U64_DEFINE_EARLY(name); \
442 SYSCTL_COUNTER_U64(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, \
444 STATNODE_ULONG(numneg, "Number of negative cache entries");
445 STATNODE_ULONG(numcache, "Number of cache entries");
446 STATNODE_COUNTER(numcachehv, "Number of namecache entries with vnodes held");
447 STATNODE_COUNTER(numdrops, "Number of dropped entries due to reaching the limit");
448 STATNODE_COUNTER(dothits, "Number of '.' hits");
449 STATNODE_COUNTER(dotdothits, "Number of '..' hits");
450 STATNODE_COUNTER(nummiss, "Number of cache misses");
451 STATNODE_COUNTER(nummisszap, "Number of cache misses we do not want to cache");
452 STATNODE_COUNTER(numposzaps,
453 "Number of cache hits (positive) we do not want to cache");
454 STATNODE_COUNTER(numposhits, "Number of cache hits (positive)");
455 STATNODE_COUNTER(numnegzaps,
456 "Number of cache hits (negative) we do not want to cache");
457 STATNODE_COUNTER(numneghits, "Number of cache hits (negative)");
458 /* These count for vn_getcwd(), too. */
459 STATNODE_COUNTER(numfullpathcalls, "Number of fullpath search calls");
460 STATNODE_COUNTER(numfullpathfail1, "Number of fullpath search errors (ENOTDIR)");
461 STATNODE_COUNTER(numfullpathfail2,
462 "Number of fullpath search errors (VOP_VPTOCNP failures)");
463 STATNODE_COUNTER(numfullpathfail4, "Number of fullpath search errors (ENOMEM)");
464 STATNODE_COUNTER(numfullpathfound, "Number of successful fullpath calls");
465 STATNODE_COUNTER(zap_and_exit_bucket_relock_success,
466 "Number of successful removals after relocking");
467 static long zap_and_exit_bucket_fail; STATNODE_ULONG(zap_and_exit_bucket_fail,
468 "Number of times zap_and_exit failed to lock");
469 static long zap_and_exit_bucket_fail2; STATNODE_ULONG(zap_and_exit_bucket_fail2,
470 "Number of times zap_and_exit failed to lock");
471 static long cache_lock_vnodes_cel_3_failures;
472 STATNODE_ULONG(cache_lock_vnodes_cel_3_failures,
473 "Number of times 3-way vnode locking failed");
474 STATNODE_COUNTER(numneg_evicted,
475 "Number of negative entries evicted when adding a new entry");
476 STATNODE_COUNTER(shrinking_skipped,
477 "Number of times shrinking was already in progress");
479 static void cache_zap_locked(struct namecache *ncp);
480 static int vn_fullpath_hardlink(struct nameidata *ndp, char **retbuf,
481 char **freebuf, size_t *buflen);
482 static int vn_fullpath_any_smr(struct vnode *vp, struct vnode *rdir, char *buf,
483 char **retbuf, size_t *buflen, bool slash_prefixed, size_t addend);
484 static int vn_fullpath_any(struct vnode *vp, struct vnode *rdir, char *buf,
485 char **retbuf, size_t *buflen);
486 static int vn_fullpath_dir(struct vnode *vp, struct vnode *rdir, char *buf,
487 char **retbuf, size_t *len, bool slash_prefixed, size_t addend);
489 static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
492 cache_assert_vlp_locked(struct mtx *vlp)
496 mtx_assert(vlp, MA_OWNED);
500 cache_assert_vnode_locked(struct vnode *vp)
504 vlp = VP2VNODELOCK(vp);
505 cache_assert_vlp_locked(vlp);
509 * TODO: With the value stored we can do better than computing the hash based
510 * on the address. The choice of FNV should also be revisited.
513 cache_prehash(struct vnode *vp)
516 vp->v_nchash = fnv_32_buf(&vp, sizeof(vp), FNV1_32_INIT);
520 cache_get_hash(char *name, u_char len, struct vnode *dvp)
523 return (fnv_32_buf(name, len, dvp->v_nchash));
526 static inline struct nchashhead *
527 NCP2BUCKET(struct namecache *ncp)
531 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen, ncp->nc_dvp);
532 return (NCHHASH(hash));
535 static inline struct mtx *
536 NCP2BUCKETLOCK(struct namecache *ncp)
540 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen, ncp->nc_dvp);
541 return (HASH2BUCKETLOCK(hash));
546 cache_assert_bucket_locked(struct namecache *ncp)
550 blp = NCP2BUCKETLOCK(ncp);
551 mtx_assert(blp, MA_OWNED);
555 cache_assert_bucket_unlocked(struct namecache *ncp)
559 blp = NCP2BUCKETLOCK(ncp);
560 mtx_assert(blp, MA_NOTOWNED);
563 #define cache_assert_bucket_locked(x) do { } while (0)
564 #define cache_assert_bucket_unlocked(x) do { } while (0)
567 #define cache_sort_vnodes(x, y) _cache_sort_vnodes((void **)(x), (void **)(y))
569 _cache_sort_vnodes(void **p1, void **p2)
573 MPASS(*p1 != NULL || *p2 != NULL);
583 cache_lock_all_buckets(void)
587 for (i = 0; i < numbucketlocks; i++)
588 mtx_lock(&bucketlocks[i]);
592 cache_unlock_all_buckets(void)
596 for (i = 0; i < numbucketlocks; i++)
597 mtx_unlock(&bucketlocks[i]);
601 cache_lock_all_vnodes(void)
605 for (i = 0; i < numvnodelocks; i++)
606 mtx_lock(&vnodelocks[i]);
610 cache_unlock_all_vnodes(void)
614 for (i = 0; i < numvnodelocks; i++)
615 mtx_unlock(&vnodelocks[i]);
619 cache_trylock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
622 cache_sort_vnodes(&vlp1, &vlp2);
625 if (!mtx_trylock(vlp1))
628 if (!mtx_trylock(vlp2)) {
638 cache_lock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
641 MPASS(vlp1 != NULL || vlp2 != NULL);
651 cache_unlock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
654 MPASS(vlp1 != NULL || vlp2 != NULL);
663 sysctl_nchstats(SYSCTL_HANDLER_ARGS)
665 struct nchstats snap;
667 if (req->oldptr == NULL)
668 return (SYSCTL_OUT(req, 0, sizeof(snap)));
671 snap.ncs_goodhits = counter_u64_fetch(numposhits);
672 snap.ncs_neghits = counter_u64_fetch(numneghits);
673 snap.ncs_badhits = counter_u64_fetch(numposzaps) +
674 counter_u64_fetch(numnegzaps);
675 snap.ncs_miss = counter_u64_fetch(nummisszap) +
676 counter_u64_fetch(nummiss);
678 return (SYSCTL_OUT(req, &snap, sizeof(snap)));
680 SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE | CTLFLAG_RD |
681 CTLFLAG_MPSAFE, 0, 0, sysctl_nchstats, "LU",
682 "VFS cache effectiveness statistics");
685 sysctl_hotnum(SYSCTL_HANDLER_ARGS)
690 for (i = 0; i < numneglists; i++)
691 out += neglists[i].nl_hotnum;
693 return (SYSCTL_OUT(req, &out, sizeof(out)));
695 SYSCTL_PROC(_vfs_cache, OID_AUTO, hotnum, CTLTYPE_INT | CTLFLAG_RD |
696 CTLFLAG_MPSAFE, 0, 0, sysctl_hotnum, "I",
697 "Number of hot negative entries");
701 * Grab an atomic snapshot of the name cache hash chain lengths
703 static SYSCTL_NODE(_debug, OID_AUTO, hashstat,
704 CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
708 sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS)
710 struct nchashhead *ncpp;
711 struct namecache *ncp;
712 int i, error, n_nchash, *cntbuf;
715 n_nchash = nchash + 1; /* nchash is max index, not count */
716 if (req->oldptr == NULL)
717 return SYSCTL_OUT(req, 0, n_nchash * sizeof(int));
718 cntbuf = malloc(n_nchash * sizeof(int), M_TEMP, M_ZERO | M_WAITOK);
719 cache_lock_all_buckets();
720 if (n_nchash != nchash + 1) {
721 cache_unlock_all_buckets();
722 free(cntbuf, M_TEMP);
725 /* Scan hash tables counting entries */
726 for (ncpp = nchashtbl, i = 0; i < n_nchash; ncpp++, i++)
727 CK_SLIST_FOREACH(ncp, ncpp, nc_hash)
729 cache_unlock_all_buckets();
730 for (error = 0, i = 0; i < n_nchash; i++)
731 if ((error = SYSCTL_OUT(req, &cntbuf[i], sizeof(int))) != 0)
733 free(cntbuf, M_TEMP);
736 SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD|
737 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_rawnchash, "S,int",
738 "nchash chain lengths");
741 sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS)
744 struct nchashhead *ncpp;
745 struct namecache *ncp;
747 int count, maxlength, used, pct;
750 return SYSCTL_OUT(req, 0, 4 * sizeof(int));
752 cache_lock_all_buckets();
753 n_nchash = nchash + 1; /* nchash is max index, not count */
757 /* Scan hash tables for applicable entries */
758 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
760 CK_SLIST_FOREACH(ncp, ncpp, nc_hash) {
765 if (maxlength < count)
768 n_nchash = nchash + 1;
769 cache_unlock_all_buckets();
770 pct = (used * 100) / (n_nchash / 100);
771 error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash));
774 error = SYSCTL_OUT(req, &used, sizeof(used));
777 error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength));
780 error = SYSCTL_OUT(req, &pct, sizeof(pct));
785 SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD|
786 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_nchash, "I",
787 "nchash statistics (number of total/used buckets, maximum chain length, usage percentage)");
791 * Negative entries management
793 * A variation of LRU scheme is used. New entries are hashed into one of
794 * numneglists cold lists. Entries get promoted to the hot list on first hit.
796 * The shrinker will demote hot list head and evict from the cold list in a
797 * round-robin manner.
800 cache_negative_init(struct namecache *ncp)
802 struct negstate *negstate;
804 ncp->nc_flag |= NCF_NEGATIVE;
805 negstate = NCP2NEGSTATE(ncp);
806 negstate->neg_flag = 0;
810 cache_negative_hit(struct namecache *ncp)
812 struct neglist *neglist;
813 struct negstate *negstate;
815 negstate = NCP2NEGSTATE(ncp);
816 if ((negstate->neg_flag & NEG_HOT) != 0)
818 neglist = NCP2NEGLIST(ncp);
819 mtx_lock(&neglist->nl_lock);
820 if ((negstate->neg_flag & NEG_HOT) == 0) {
821 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
822 TAILQ_INSERT_TAIL(&neglist->nl_hotlist, ncp, nc_dst);
823 neglist->nl_hotnum++;
824 negstate->neg_flag |= NEG_HOT;
826 mtx_unlock(&neglist->nl_lock);
830 cache_negative_insert(struct namecache *ncp)
832 struct neglist *neglist;
834 MPASS(ncp->nc_flag & NCF_NEGATIVE);
835 cache_assert_bucket_locked(ncp);
836 neglist = NCP2NEGLIST(ncp);
837 mtx_lock(&neglist->nl_lock);
838 TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst);
839 mtx_unlock(&neglist->nl_lock);
840 atomic_add_long(&numneg, 1);
844 cache_negative_remove(struct namecache *ncp)
846 struct neglist *neglist;
847 struct negstate *negstate;
849 cache_assert_bucket_locked(ncp);
850 neglist = NCP2NEGLIST(ncp);
851 negstate = NCP2NEGSTATE(ncp);
852 mtx_lock(&neglist->nl_lock);
853 if ((negstate->neg_flag & NEG_HOT) != 0) {
854 TAILQ_REMOVE(&neglist->nl_hotlist, ncp, nc_dst);
855 neglist->nl_hotnum--;
857 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
859 mtx_unlock(&neglist->nl_lock);
860 atomic_subtract_long(&numneg, 1);
863 static struct neglist *
864 cache_negative_shrink_select(void)
866 struct neglist *neglist;
871 for (i = 0; i < numneglists; i++) {
872 neglist = &neglists[(cycle + i) % numneglists];
873 if (TAILQ_FIRST(&neglist->nl_list) == NULL &&
874 TAILQ_FIRST(&neglist->nl_hotlist) == NULL)
876 mtx_lock(&neglist->nl_lock);
877 if (TAILQ_FIRST(&neglist->nl_list) != NULL ||
878 TAILQ_FIRST(&neglist->nl_hotlist) != NULL)
880 mtx_unlock(&neglist->nl_lock);
887 cache_negative_zap_one(void)
889 struct namecache *ncp, *ncp2;
890 struct neglist *neglist;
891 struct negstate *negstate;
895 if (mtx_owner(&ncneg_shrink_lock) != NULL ||
896 !mtx_trylock(&ncneg_shrink_lock)) {
897 counter_u64_add(shrinking_skipped, 1);
901 neglist = cache_negative_shrink_select();
902 mtx_unlock(&ncneg_shrink_lock);
903 if (neglist == NULL) {
907 ncp = TAILQ_FIRST(&neglist->nl_hotlist);
909 negstate = NCP2NEGSTATE(ncp);
910 TAILQ_REMOVE(&neglist->nl_hotlist, ncp, nc_dst);
911 TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst);
912 neglist->nl_hotnum--;
913 negstate->neg_flag &= ~NEG_HOT;
915 ncp = TAILQ_FIRST(&neglist->nl_list);
917 negstate = NCP2NEGSTATE(ncp);
918 dvlp = VP2VNODELOCK(ncp->nc_dvp);
919 blp = NCP2BUCKETLOCK(ncp);
920 mtx_unlock(&neglist->nl_lock);
924 * Enter SMR to safely check the negative list.
925 * Even if the found pointer matches, the entry may now be reallocated
926 * and used by a different vnode.
929 ncp2 = TAILQ_FIRST(&neglist->nl_list);
930 if (ncp != ncp2 || dvlp != VP2VNODELOCK(ncp2->nc_dvp) ||
931 blp != NCP2BUCKETLOCK(ncp2)) {
936 SDT_PROBE2(vfs, namecache, shrink_negative, done, ncp->nc_dvp,
938 cache_zap_locked(ncp);
939 counter_u64_add(numneg_evicted, 1);
948 * cache_zap_locked():
950 * Removes a namecache entry from cache, whether it contains an actual
951 * pointer to a vnode or if it is just a negative cache entry.
954 cache_zap_locked(struct namecache *ncp)
956 struct nchashhead *ncpp;
958 if (!(ncp->nc_flag & NCF_NEGATIVE))
959 cache_assert_vnode_locked(ncp->nc_vp);
960 cache_assert_vnode_locked(ncp->nc_dvp);
961 cache_assert_bucket_locked(ncp);
963 cache_ncp_invalidate(ncp);
965 ncpp = NCP2BUCKET(ncp);
966 CK_SLIST_REMOVE(ncpp, ncp, namecache, nc_hash);
967 if (!(ncp->nc_flag & NCF_NEGATIVE)) {
968 SDT_PROBE3(vfs, namecache, zap, done, ncp->nc_dvp,
969 ncp->nc_name, ncp->nc_vp);
970 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
971 if (ncp == ncp->nc_vp->v_cache_dd) {
972 vn_seqc_write_begin_unheld(ncp->nc_vp);
973 ncp->nc_vp->v_cache_dd = NULL;
974 vn_seqc_write_end(ncp->nc_vp);
977 SDT_PROBE2(vfs, namecache, zap_negative, done, ncp->nc_dvp,
979 cache_negative_remove(ncp);
981 if (ncp->nc_flag & NCF_ISDOTDOT) {
982 if (ncp == ncp->nc_dvp->v_cache_dd) {
983 vn_seqc_write_begin_unheld(ncp->nc_dvp);
984 ncp->nc_dvp->v_cache_dd = NULL;
985 vn_seqc_write_end(ncp->nc_dvp);
988 LIST_REMOVE(ncp, nc_src);
989 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) {
990 ncp->nc_flag |= NCF_DVDROP;
991 counter_u64_add(numcachehv, -1);
994 atomic_subtract_long(&numcache, 1);
998 cache_zap_negative_locked_vnode_kl(struct namecache *ncp, struct vnode *vp)
1002 MPASS(ncp->nc_dvp == vp);
1003 MPASS(ncp->nc_flag & NCF_NEGATIVE);
1004 cache_assert_vnode_locked(vp);
1006 blp = NCP2BUCKETLOCK(ncp);
1008 cache_zap_locked(ncp);
1013 cache_zap_locked_vnode_kl2(struct namecache *ncp, struct vnode *vp,
1016 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
1019 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
1020 cache_assert_vnode_locked(vp);
1022 if (ncp->nc_flag & NCF_NEGATIVE) {
1023 if (*vlpp != NULL) {
1027 cache_zap_negative_locked_vnode_kl(ncp, vp);
1031 pvlp = VP2VNODELOCK(vp);
1032 blp = NCP2BUCKETLOCK(ncp);
1033 vlp1 = VP2VNODELOCK(ncp->nc_dvp);
1034 vlp2 = VP2VNODELOCK(ncp->nc_vp);
1036 if (*vlpp == vlp1 || *vlpp == vlp2) {
1040 if (*vlpp != NULL) {
1044 cache_sort_vnodes(&vlp1, &vlp2);
1049 if (!mtx_trylock(vlp1))
1055 cache_zap_locked(ncp);
1057 if (to_unlock != NULL)
1058 mtx_unlock(to_unlock);
1065 MPASS(*vlpp == NULL);
1071 * If trylocking failed we can get here. We know enough to take all needed locks
1072 * in the right order and re-lookup the entry.
1075 cache_zap_unlocked_bucket(struct namecache *ncp, struct componentname *cnp,
1076 struct vnode *dvp, struct mtx *dvlp, struct mtx *vlp, uint32_t hash,
1079 struct namecache *rncp;
1081 cache_assert_bucket_unlocked(ncp);
1083 cache_sort_vnodes(&dvlp, &vlp);
1084 cache_lock_vnodes(dvlp, vlp);
1086 CK_SLIST_FOREACH(rncp, (NCHHASH(hash)), nc_hash) {
1087 if (rncp == ncp && rncp->nc_dvp == dvp &&
1088 rncp->nc_nlen == cnp->cn_namelen &&
1089 !bcmp(rncp->nc_name, cnp->cn_nameptr, rncp->nc_nlen))
1093 cache_zap_locked(rncp);
1095 cache_unlock_vnodes(dvlp, vlp);
1096 counter_u64_add(zap_and_exit_bucket_relock_success, 1);
1101 cache_unlock_vnodes(dvlp, vlp);
1105 static int __noinline
1106 cache_zap_locked_bucket(struct namecache *ncp, struct componentname *cnp,
1107 uint32_t hash, struct mtx *blp)
1109 struct mtx *dvlp, *vlp;
1112 cache_assert_bucket_locked(ncp);
1114 dvlp = VP2VNODELOCK(ncp->nc_dvp);
1116 if (!(ncp->nc_flag & NCF_NEGATIVE))
1117 vlp = VP2VNODELOCK(ncp->nc_vp);
1118 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1119 cache_zap_locked(ncp);
1121 cache_unlock_vnodes(dvlp, vlp);
1127 return (cache_zap_unlocked_bucket(ncp, cnp, dvp, dvlp, vlp, hash, blp));
1130 static __noinline int
1131 cache_remove_cnp(struct vnode *dvp, struct componentname *cnp)
1133 struct namecache *ncp;
1135 struct mtx *dvlp, *dvlp2;
1139 if (cnp->cn_namelen == 2 &&
1140 cnp->cn_nameptr[0] == '.' && cnp->cn_nameptr[1] == '.') {
1141 dvlp = VP2VNODELOCK(dvp);
1145 ncp = dvp->v_cache_dd;
1150 SDT_PROBE2(vfs, namecache, removecnp, miss, dvp, cnp);
1153 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1154 if (!cache_zap_locked_vnode_kl2(ncp, dvp, &dvlp2))
1156 MPASS(dvp->v_cache_dd == NULL);
1162 vn_seqc_write_begin(dvp);
1163 dvp->v_cache_dd = NULL;
1164 vn_seqc_write_end(dvp);
1169 SDT_PROBE2(vfs, namecache, removecnp, hit, dvp, cnp);
1173 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1174 blp = HASH2BUCKETLOCK(hash);
1176 if (CK_SLIST_EMPTY(NCHHASH(hash)))
1181 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1182 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1183 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1192 error = cache_zap_locked_bucket(ncp, cnp, hash, blp);
1193 if (__predict_false(error != 0)) {
1194 zap_and_exit_bucket_fail++;
1197 counter_u64_add(numposzaps, 1);
1198 SDT_PROBE2(vfs, namecache, removecnp, hit, dvp, cnp);
1202 counter_u64_add(nummisszap, 1);
1203 SDT_PROBE2(vfs, namecache, removecnp, miss, dvp, cnp);
1207 static int __noinline
1208 cache_lookup_dot(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1209 struct timespec *tsp, int *ticksp)
1214 counter_u64_add(dothits, 1);
1215 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", *vpp);
1222 * When we lookup "." we still can be asked to lock it
1225 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
1226 if (ltype != VOP_ISLOCKED(*vpp)) {
1227 if (ltype == LK_EXCLUSIVE) {
1228 vn_lock(*vpp, LK_UPGRADE | LK_RETRY);
1229 if (VN_IS_DOOMED((*vpp))) {
1230 /* forced unmount */
1236 vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY);
1241 static int __noinline
1242 cache_lookup_dotdot(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1243 struct timespec *tsp, int *ticksp)
1245 struct namecache_ts *ncp_ts;
1246 struct namecache *ncp;
1252 MPASS((cnp->cn_flags & ISDOTDOT) != 0);
1254 if ((cnp->cn_flags & MAKEENTRY) == 0) {
1255 cache_remove_cnp(dvp, cnp);
1259 counter_u64_add(dotdothits, 1);
1261 dvlp = VP2VNODELOCK(dvp);
1263 ncp = dvp->v_cache_dd;
1265 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, "..", NULL);
1269 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1270 if (ncp->nc_flag & NCF_NEGATIVE)
1277 goto negative_success;
1278 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, "..", *vpp);
1279 cache_out_ts(ncp, tsp, ticksp);
1280 if ((ncp->nc_flag & (NCF_ISDOTDOT | NCF_DTS)) ==
1281 NCF_DTS && tsp != NULL) {
1282 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1283 *tsp = ncp_ts->nc_dotdottime;
1287 ltype = VOP_ISLOCKED(dvp);
1289 vs = vget_prep(*vpp);
1291 error = vget_finish(*vpp, cnp->cn_lkflags, vs);
1292 vn_lock(dvp, ltype | LK_RETRY);
1293 if (VN_IS_DOOMED(dvp)) {
1305 if (__predict_false(cnp->cn_nameiop == CREATE)) {
1306 if (cnp->cn_flags & ISLASTCN) {
1307 counter_u64_add(numnegzaps, 1);
1308 cache_zap_negative_locked_vnode_kl(ncp, dvp);
1315 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp, ncp->nc_name);
1316 cache_out_ts(ncp, tsp, ticksp);
1317 counter_u64_add(numneghits, 1);
1318 whiteout = (ncp->nc_flag & NCF_WHITE);
1319 cache_negative_hit(ncp);
1322 cnp->cn_flags |= ISWHITEOUT;
1327 * Lookup a name in the name cache
1331 * - dvp: Parent directory in which to search.
1332 * - vpp: Return argument. Will contain desired vnode on cache hit.
1333 * - cnp: Parameters of the name search. The most interesting bits of
1334 * the cn_flags field have the following meanings:
1335 * - MAKEENTRY: If clear, free an entry from the cache rather than look
1337 * - ISDOTDOT: Must be set if and only if cn_nameptr == ".."
1338 * - tsp: Return storage for cache timestamp. On a successful (positive
1339 * or negative) lookup, tsp will be filled with any timespec that
1340 * was stored when this cache entry was created. However, it will
1341 * be clear for "." entries.
1342 * - ticks: Return storage for alternate cache timestamp. On a successful
1343 * (positive or negative) lookup, it will contain the ticks value
1344 * that was current when the cache entry was created, unless cnp
1347 * Either both tsp and ticks have to be provided or neither of them.
1351 * - -1: A positive cache hit. vpp will contain the desired vnode.
1352 * - ENOENT: A negative cache hit, or dvp was recycled out from under us due
1353 * to a forced unmount. vpp will not be modified. If the entry
1354 * is a whiteout, then the ISWHITEOUT flag will be set in
1356 * - 0: A cache miss. vpp will not be modified.
1360 * On a cache hit, vpp will be returned locked and ref'd. If we're looking up
1361 * .., dvp is unlocked. If we're looking up . an extra ref is taken, but the
1362 * lock is not recursively acquired.
1364 static int __noinline
1365 cache_lookup_fallback(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1366 struct timespec *tsp, int *ticksp)
1368 struct namecache *ncp;
1375 MPASS((cnp->cn_flags & (MAKEENTRY | ISDOTDOT)) == MAKEENTRY);
1378 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1379 blp = HASH2BUCKETLOCK(hash);
1382 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1383 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1384 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1388 if (__predict_false(ncp == NULL)) {
1390 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
1392 counter_u64_add(nummiss, 1);
1396 if (ncp->nc_flag & NCF_NEGATIVE)
1397 goto negative_success;
1399 counter_u64_add(numposhits, 1);
1401 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name, *vpp);
1402 cache_out_ts(ncp, tsp, ticksp);
1404 vs = vget_prep(*vpp);
1406 error = vget_finish(*vpp, cnp->cn_lkflags, vs);
1413 if (__predict_false(cnp->cn_nameiop == CREATE)) {
1414 if (cnp->cn_flags & ISLASTCN) {
1415 counter_u64_add(numnegzaps, 1);
1416 error = cache_zap_locked_bucket(ncp, cnp, hash, blp);
1417 if (__predict_false(error != 0)) {
1418 zap_and_exit_bucket_fail2++;
1426 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp, ncp->nc_name);
1427 cache_out_ts(ncp, tsp, ticksp);
1428 counter_u64_add(numneghits, 1);
1429 whiteout = (ncp->nc_flag & NCF_WHITE);
1430 cache_negative_hit(ncp);
1433 cnp->cn_flags |= ISWHITEOUT;
1438 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1439 struct timespec *tsp, int *ticksp)
1441 struct namecache *ncp;
1442 struct negstate *negstate;
1449 MPASS((tsp == NULL && ticksp == NULL) || (tsp != NULL && ticksp != NULL));
1452 if (__predict_false(!doingcache)) {
1453 cnp->cn_flags &= ~MAKEENTRY;
1458 if (__predict_false(cnp->cn_nameptr[0] == '.')) {
1459 if (cnp->cn_namelen == 1)
1460 return (cache_lookup_dot(dvp, vpp, cnp, tsp, ticksp));
1461 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.')
1462 return (cache_lookup_dotdot(dvp, vpp, cnp, tsp, ticksp));
1465 MPASS((cnp->cn_flags & ISDOTDOT) == 0);
1467 if ((cnp->cn_flags & MAKEENTRY) == 0) {
1468 cache_remove_cnp(dvp, cnp);
1472 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1475 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1476 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1477 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1481 if (__predict_false(ncp == NULL)) {
1483 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
1485 counter_u64_add(nummiss, 1);
1489 nc_flag = atomic_load_char(&ncp->nc_flag);
1490 if (nc_flag & NCF_NEGATIVE)
1491 goto negative_success;
1493 counter_u64_add(numposhits, 1);
1495 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name, *vpp);
1496 cache_out_ts(ncp, tsp, ticksp);
1498 if (!cache_ncp_canuse(ncp)) {
1503 vs = vget_prep_smr(*vpp);
1505 if (__predict_false(vs == VGET_NONE)) {
1509 error = vget_finish(*vpp, cnp->cn_lkflags, vs);
1516 if (__predict_false(cnp->cn_nameiop == CREATE)) {
1517 if (cnp->cn_flags & ISLASTCN) {
1523 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp, ncp->nc_name);
1524 cache_out_ts(ncp, tsp, ticksp);
1525 counter_u64_add(numneghits, 1);
1526 whiteout = (ncp->nc_flag & NCF_WHITE);
1528 * TODO: We need to take locks to promote an entry. Code doing it
1529 * in SMR lookup can be modified to be shared.
1531 negstate = NCP2NEGSTATE(ncp);
1532 if ((negstate->neg_flag & NEG_HOT) == 0 ||
1533 !cache_ncp_canuse(ncp)) {
1539 cnp->cn_flags |= ISWHITEOUT;
1542 return (cache_lookup_fallback(dvp, vpp, cnp, tsp, ticksp));
1545 struct celockstate {
1549 CTASSERT((nitems(((struct celockstate *)0)->vlp) == 3));
1550 CTASSERT((nitems(((struct celockstate *)0)->blp) == 2));
1553 cache_celockstate_init(struct celockstate *cel)
1556 bzero(cel, sizeof(*cel));
1560 cache_lock_vnodes_cel(struct celockstate *cel, struct vnode *vp,
1563 struct mtx *vlp1, *vlp2;
1565 MPASS(cel->vlp[0] == NULL);
1566 MPASS(cel->vlp[1] == NULL);
1567 MPASS(cel->vlp[2] == NULL);
1569 MPASS(vp != NULL || dvp != NULL);
1571 vlp1 = VP2VNODELOCK(vp);
1572 vlp2 = VP2VNODELOCK(dvp);
1573 cache_sort_vnodes(&vlp1, &vlp2);
1584 cache_unlock_vnodes_cel(struct celockstate *cel)
1587 MPASS(cel->vlp[0] != NULL || cel->vlp[1] != NULL);
1589 if (cel->vlp[0] != NULL)
1590 mtx_unlock(cel->vlp[0]);
1591 if (cel->vlp[1] != NULL)
1592 mtx_unlock(cel->vlp[1]);
1593 if (cel->vlp[2] != NULL)
1594 mtx_unlock(cel->vlp[2]);
1598 cache_lock_vnodes_cel_3(struct celockstate *cel, struct vnode *vp)
1603 cache_assert_vlp_locked(cel->vlp[0]);
1604 cache_assert_vlp_locked(cel->vlp[1]);
1605 MPASS(cel->vlp[2] == NULL);
1608 vlp = VP2VNODELOCK(vp);
1611 if (vlp >= cel->vlp[1]) {
1614 if (mtx_trylock(vlp))
1616 cache_lock_vnodes_cel_3_failures++;
1617 cache_unlock_vnodes_cel(cel);
1618 if (vlp < cel->vlp[0]) {
1620 mtx_lock(cel->vlp[0]);
1621 mtx_lock(cel->vlp[1]);
1623 if (cel->vlp[0] != NULL)
1624 mtx_lock(cel->vlp[0]);
1626 mtx_lock(cel->vlp[1]);
1636 cache_lock_buckets_cel(struct celockstate *cel, struct mtx *blp1,
1640 MPASS(cel->blp[0] == NULL);
1641 MPASS(cel->blp[1] == NULL);
1643 cache_sort_vnodes(&blp1, &blp2);
1654 cache_unlock_buckets_cel(struct celockstate *cel)
1657 if (cel->blp[0] != NULL)
1658 mtx_unlock(cel->blp[0]);
1659 mtx_unlock(cel->blp[1]);
1663 * Lock part of the cache affected by the insertion.
1665 * This means vnodelocks for dvp, vp and the relevant bucketlock.
1666 * However, insertion can result in removal of an old entry. In this
1667 * case we have an additional vnode and bucketlock pair to lock.
1669 * That is, in the worst case we have to lock 3 vnodes and 2 bucketlocks, while
1670 * preserving the locking order (smaller address first).
1673 cache_enter_lock(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1676 struct namecache *ncp;
1677 struct mtx *blps[2];
1679 blps[0] = HASH2BUCKETLOCK(hash);
1682 cache_lock_vnodes_cel(cel, dvp, vp);
1683 if (vp == NULL || vp->v_type != VDIR)
1685 ncp = vp->v_cache_dd;
1688 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1690 MPASS(ncp->nc_dvp == vp);
1691 blps[1] = NCP2BUCKETLOCK(ncp);
1692 if (ncp->nc_flag & NCF_NEGATIVE)
1694 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1697 * All vnodes got re-locked. Re-validate the state and if
1698 * nothing changed we are done. Otherwise restart.
1700 if (ncp == vp->v_cache_dd &&
1701 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1702 blps[1] == NCP2BUCKETLOCK(ncp) &&
1703 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1705 cache_unlock_vnodes_cel(cel);
1710 cache_lock_buckets_cel(cel, blps[0], blps[1]);
1714 cache_enter_lock_dd(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1717 struct namecache *ncp;
1718 struct mtx *blps[2];
1720 blps[0] = HASH2BUCKETLOCK(hash);
1723 cache_lock_vnodes_cel(cel, dvp, vp);
1724 ncp = dvp->v_cache_dd;
1727 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1729 MPASS(ncp->nc_dvp == dvp);
1730 blps[1] = NCP2BUCKETLOCK(ncp);
1731 if (ncp->nc_flag & NCF_NEGATIVE)
1733 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1735 if (ncp == dvp->v_cache_dd &&
1736 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1737 blps[1] == NCP2BUCKETLOCK(ncp) &&
1738 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1740 cache_unlock_vnodes_cel(cel);
1745 cache_lock_buckets_cel(cel, blps[0], blps[1]);
1749 cache_enter_unlock(struct celockstate *cel)
1752 cache_unlock_buckets_cel(cel);
1753 cache_unlock_vnodes_cel(cel);
1756 static void __noinline
1757 cache_enter_dotdot_prep(struct vnode *dvp, struct vnode *vp,
1758 struct componentname *cnp)
1760 struct celockstate cel;
1761 struct namecache *ncp;
1765 if (dvp->v_cache_dd == NULL)
1767 len = cnp->cn_namelen;
1768 cache_celockstate_init(&cel);
1769 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1770 cache_enter_lock_dd(&cel, dvp, vp, hash);
1771 vn_seqc_write_begin(dvp);
1772 ncp = dvp->v_cache_dd;
1773 if (ncp != NULL && (ncp->nc_flag & NCF_ISDOTDOT)) {
1774 KASSERT(ncp->nc_dvp == dvp, ("wrong isdotdot parent"));
1775 cache_zap_locked(ncp);
1779 dvp->v_cache_dd = NULL;
1780 vn_seqc_write_end(dvp);
1781 cache_enter_unlock(&cel);
1787 * Add an entry to the cache.
1790 cache_enter_time(struct vnode *dvp, struct vnode *vp, struct componentname *cnp,
1791 struct timespec *tsp, struct timespec *dtsp)
1793 struct celockstate cel;
1794 struct namecache *ncp, *n2, *ndd;
1795 struct namecache_ts *ncp_ts;
1796 struct nchashhead *ncpp;
1802 VNPASS(!VN_IS_DOOMED(dvp), dvp);
1803 VNPASS(dvp->v_type != VNON, dvp);
1805 VNPASS(!VN_IS_DOOMED(vp), vp);
1806 VNPASS(vp->v_type != VNON, vp);
1810 if (__predict_false(!doingcache))
1815 if (__predict_false(cnp->cn_nameptr[0] == '.')) {
1816 if (cnp->cn_namelen == 1)
1818 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
1819 cache_enter_dotdot_prep(dvp, vp, cnp);
1820 flag = NCF_ISDOTDOT;
1825 * Avoid blowout in namecache entries.
1827 lnumcache = atomic_fetchadd_long(&numcache, 1) + 1;
1828 if (__predict_false(lnumcache >= ncsize)) {
1829 atomic_subtract_long(&numcache, 1);
1830 counter_u64_add(numdrops, 1);
1834 cache_celockstate_init(&cel);
1839 * Calculate the hash key and setup as much of the new
1840 * namecache entry as possible before acquiring the lock.
1842 ncp = cache_alloc(cnp->cn_namelen, tsp != NULL);
1843 ncp->nc_flag = flag | NCF_WIP;
1846 cache_negative_init(ncp);
1849 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1850 ncp_ts->nc_time = *tsp;
1851 ncp_ts->nc_ticks = ticks;
1852 ncp_ts->nc_nc.nc_flag |= NCF_TS;
1854 ncp_ts->nc_dotdottime = *dtsp;
1855 ncp_ts->nc_nc.nc_flag |= NCF_DTS;
1858 len = ncp->nc_nlen = cnp->cn_namelen;
1859 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1860 memcpy(ncp->nc_name, cnp->cn_nameptr, len);
1861 ncp->nc_name[len] = '\0';
1862 cache_enter_lock(&cel, dvp, vp, hash);
1865 * See if this vnode or negative entry is already in the cache
1866 * with this name. This can happen with concurrent lookups of
1867 * the same path name.
1869 ncpp = NCHHASH(hash);
1870 CK_SLIST_FOREACH(n2, ncpp, nc_hash) {
1871 if (n2->nc_dvp == dvp &&
1872 n2->nc_nlen == cnp->cn_namelen &&
1873 !bcmp(n2->nc_name, cnp->cn_nameptr, n2->nc_nlen)) {
1874 MPASS(cache_ncp_canuse(n2));
1875 if ((n2->nc_flag & NCF_NEGATIVE) != 0)
1877 ("%s: found entry pointing to a different vnode (%p != %p)",
1878 __func__, NULL, vp));
1880 KASSERT(n2->nc_vp == vp,
1881 ("%s: found entry pointing to a different vnode (%p != %p)",
1882 __func__, n2->nc_vp, vp));
1884 * Entries are supposed to be immutable unless in the
1885 * process of getting destroyed. Accommodating for
1886 * changing timestamps is possible but not worth it.
1887 * This should be harmless in terms of correctness, in
1888 * the worst case resulting in an earlier expiration.
1889 * Alternatively, the found entry can be replaced
1892 MPASS((n2->nc_flag & (NCF_TS | NCF_DTS)) == (ncp->nc_flag & (NCF_TS | NCF_DTS)));
1895 KASSERT((n2->nc_flag & NCF_TS) != 0,
1897 n2_ts = __containerof(n2, struct namecache_ts, nc_nc);
1898 n2_ts->nc_time = ncp_ts->nc_time;
1899 n2_ts->nc_ticks = ncp_ts->nc_ticks;
1901 n2_ts->nc_dotdottime = ncp_ts->nc_dotdottime;
1902 n2_ts->nc_nc.nc_flag |= NCF_DTS;
1906 goto out_unlock_free;
1910 if (flag == NCF_ISDOTDOT) {
1912 * See if we are trying to add .. entry, but some other lookup
1913 * has populated v_cache_dd pointer already.
1915 if (dvp->v_cache_dd != NULL)
1916 goto out_unlock_free;
1917 KASSERT(vp == NULL || vp->v_type == VDIR,
1918 ("wrong vnode type %p", vp));
1919 vn_seqc_write_begin(dvp);
1920 dvp->v_cache_dd = ncp;
1921 vn_seqc_write_end(dvp);
1925 if (flag != NCF_ISDOTDOT) {
1927 * For this case, the cache entry maps both the
1928 * directory name in it and the name ".." for the
1929 * directory's parent.
1931 vn_seqc_write_begin(vp);
1932 if ((ndd = vp->v_cache_dd) != NULL) {
1933 if ((ndd->nc_flag & NCF_ISDOTDOT) != 0)
1934 cache_zap_locked(ndd);
1938 vp->v_cache_dd = ncp;
1939 vn_seqc_write_end(vp);
1940 } else if (vp->v_type != VDIR) {
1941 if (vp->v_cache_dd != NULL) {
1942 vn_seqc_write_begin(vp);
1943 vp->v_cache_dd = NULL;
1944 vn_seqc_write_end(vp);
1949 if (flag != NCF_ISDOTDOT) {
1950 if (LIST_EMPTY(&dvp->v_cache_src)) {
1952 counter_u64_add(numcachehv, 1);
1954 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
1958 * If the entry is "negative", we place it into the
1959 * "negative" cache queue, otherwise, we place it into the
1960 * destination vnode's cache entries queue.
1963 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
1964 SDT_PROBE3(vfs, namecache, enter, done, dvp, ncp->nc_name,
1967 if (cnp->cn_flags & ISWHITEOUT)
1968 ncp->nc_flag |= NCF_WHITE;
1969 cache_negative_insert(ncp);
1970 SDT_PROBE2(vfs, namecache, enter_negative, done, dvp,
1975 * Insert the new namecache entry into the appropriate chain
1976 * within the cache entries table.
1978 CK_SLIST_INSERT_HEAD(ncpp, ncp, nc_hash);
1980 atomic_thread_fence_rel();
1982 * Mark the entry as fully constructed.
1983 * It is immutable past this point until its removal.
1985 atomic_store_char(&ncp->nc_flag, ncp->nc_flag & ~NCF_WIP);
1987 cache_enter_unlock(&cel);
1988 if (numneg * ncnegfactor > lnumcache)
1989 cache_negative_zap_one();
1994 cache_enter_unlock(&cel);
1995 atomic_subtract_long(&numcache, 1);
2001 cache_roundup_2(u_int val)
2005 for (res = 1; res <= val; res <<= 1)
2011 static struct nchashhead *
2012 nchinittbl(u_long elements, u_long *hashmask)
2014 struct nchashhead *hashtbl;
2017 hashsize = cache_roundup_2(elements) / 2;
2019 hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), M_VFSCACHE, M_WAITOK);
2020 for (i = 0; i < hashsize; i++)
2021 CK_SLIST_INIT(&hashtbl[i]);
2022 *hashmask = hashsize - 1;
2027 ncfreetbl(struct nchashhead *hashtbl)
2030 free(hashtbl, M_VFSCACHE);
2034 * Name cache initialization, from vfs_init() when we are booting
2037 nchinit(void *dummy __unused)
2041 cache_zone_small = uma_zcreate("S VFS Cache", CACHE_ZONE_SMALL_SIZE,
2042 NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT, UMA_ZONE_ZINIT);
2043 cache_zone_small_ts = uma_zcreate("STS VFS Cache", CACHE_ZONE_SMALL_TS_SIZE,
2044 NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT, UMA_ZONE_ZINIT);
2045 cache_zone_large = uma_zcreate("L VFS Cache", CACHE_ZONE_LARGE_SIZE,
2046 NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT, UMA_ZONE_ZINIT);
2047 cache_zone_large_ts = uma_zcreate("LTS VFS Cache", CACHE_ZONE_LARGE_TS_SIZE,
2048 NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT, UMA_ZONE_ZINIT);
2050 VFS_SMR_ZONE_SET(cache_zone_small);
2051 VFS_SMR_ZONE_SET(cache_zone_small_ts);
2052 VFS_SMR_ZONE_SET(cache_zone_large);
2053 VFS_SMR_ZONE_SET(cache_zone_large_ts);
2055 ncsize = desiredvnodes * ncsizefactor;
2056 nchashtbl = nchinittbl(desiredvnodes * 2, &nchash);
2057 ncbuckethash = cache_roundup_2(mp_ncpus * mp_ncpus) - 1;
2058 if (ncbuckethash < 7) /* arbitrarily chosen to avoid having one lock */
2060 if (ncbuckethash > nchash)
2061 ncbuckethash = nchash;
2062 bucketlocks = malloc(sizeof(*bucketlocks) * numbucketlocks, M_VFSCACHE,
2064 for (i = 0; i < numbucketlocks; i++)
2065 mtx_init(&bucketlocks[i], "ncbuc", NULL, MTX_DUPOK | MTX_RECURSE);
2066 ncvnodehash = ncbuckethash;
2067 vnodelocks = malloc(sizeof(*vnodelocks) * numvnodelocks, M_VFSCACHE,
2069 for (i = 0; i < numvnodelocks; i++)
2070 mtx_init(&vnodelocks[i], "ncvn", NULL, MTX_DUPOK | MTX_RECURSE);
2072 for (i = 0; i < numneglists; i++) {
2073 mtx_init(&neglists[i].nl_lock, "ncnegl", NULL, MTX_DEF);
2074 TAILQ_INIT(&neglists[i].nl_list);
2075 TAILQ_INIT(&neglists[i].nl_hotlist);
2078 mtx_init(&ncneg_shrink_lock, "ncnegs", NULL, MTX_DEF);
2080 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL);
2083 cache_vnode_init(struct vnode *vp)
2086 LIST_INIT(&vp->v_cache_src);
2087 TAILQ_INIT(&vp->v_cache_dst);
2088 vp->v_cache_dd = NULL;
2093 cache_changesize(u_long newmaxvnodes)
2095 struct nchashhead *new_nchashtbl, *old_nchashtbl;
2096 u_long new_nchash, old_nchash;
2097 struct namecache *ncp;
2102 newncsize = newmaxvnodes * ncsizefactor;
2103 newmaxvnodes = cache_roundup_2(newmaxvnodes * 2);
2104 if (newmaxvnodes < numbucketlocks)
2105 newmaxvnodes = numbucketlocks;
2107 new_nchashtbl = nchinittbl(newmaxvnodes, &new_nchash);
2108 /* If same hash table size, nothing to do */
2109 if (nchash == new_nchash) {
2110 ncfreetbl(new_nchashtbl);
2114 * Move everything from the old hash table to the new table.
2115 * None of the namecache entries in the table can be removed
2116 * because to do so, they have to be removed from the hash table.
2118 cache_lock_all_vnodes();
2119 cache_lock_all_buckets();
2120 old_nchashtbl = nchashtbl;
2121 old_nchash = nchash;
2122 nchashtbl = new_nchashtbl;
2123 nchash = new_nchash;
2124 for (i = 0; i <= old_nchash; i++) {
2125 while ((ncp = CK_SLIST_FIRST(&old_nchashtbl[i])) != NULL) {
2126 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen,
2128 CK_SLIST_REMOVE(&old_nchashtbl[i], ncp, namecache, nc_hash);
2129 CK_SLIST_INSERT_HEAD(NCHHASH(hash), ncp, nc_hash);
2133 cache_unlock_all_buckets();
2134 cache_unlock_all_vnodes();
2135 ncfreetbl(old_nchashtbl);
2139 * Invalidate all entries from and to a particular vnode.
2142 cache_purge_impl(struct vnode *vp)
2144 TAILQ_HEAD(, namecache) ncps;
2145 struct namecache *ncp, *nnp;
2146 struct mtx *vlp, *vlp2;
2149 vlp = VP2VNODELOCK(vp);
2153 while (!LIST_EMPTY(&vp->v_cache_src)) {
2154 ncp = LIST_FIRST(&vp->v_cache_src);
2155 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2157 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2159 while (!TAILQ_EMPTY(&vp->v_cache_dst)) {
2160 ncp = TAILQ_FIRST(&vp->v_cache_dst);
2161 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2163 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2165 ncp = vp->v_cache_dd;
2167 KASSERT(ncp->nc_flag & NCF_ISDOTDOT,
2168 ("lost dotdot link"));
2169 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2171 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2173 KASSERT(vp->v_cache_dd == NULL, ("incomplete purge"));
2177 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2183 * Opportunistic check to see if there is anything to do.
2186 cache_has_entries(struct vnode *vp)
2189 if (LIST_EMPTY(&vp->v_cache_src) && TAILQ_EMPTY(&vp->v_cache_dst) &&
2190 vp->v_cache_dd == NULL)
2196 cache_purge(struct vnode *vp)
2199 SDT_PROBE1(vfs, namecache, purge, done, vp);
2200 if (!cache_has_entries(vp))
2202 cache_purge_impl(vp);
2206 * Only to be used by vgone.
2209 cache_purge_vgone(struct vnode *vp)
2213 VNPASS(VN_IS_DOOMED(vp), vp);
2214 if (cache_has_entries(vp)) {
2215 cache_purge_impl(vp);
2220 * Serialize against a potential thread doing cache_purge.
2222 vlp = VP2VNODELOCK(vp);
2223 mtx_wait_unlocked(vlp);
2224 if (cache_has_entries(vp)) {
2225 cache_purge_impl(vp);
2232 * Invalidate all negative entries for a particular directory vnode.
2235 cache_purge_negative(struct vnode *vp)
2237 TAILQ_HEAD(, namecache) ncps;
2238 struct namecache *ncp, *nnp;
2241 SDT_PROBE1(vfs, namecache, purge_negative, done, vp);
2242 if (LIST_EMPTY(&vp->v_cache_src))
2245 vlp = VP2VNODELOCK(vp);
2247 LIST_FOREACH_SAFE(ncp, &vp->v_cache_src, nc_src, nnp) {
2248 if (!(ncp->nc_flag & NCF_NEGATIVE))
2250 cache_zap_negative_locked_vnode_kl(ncp, vp);
2251 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2254 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2260 cache_rename(struct vnode *fdvp, struct vnode *fvp, struct vnode *tdvp,
2261 struct vnode *tvp, struct componentname *fcnp, struct componentname *tcnp)
2264 ASSERT_VOP_IN_SEQC(fdvp);
2265 ASSERT_VOP_IN_SEQC(fvp);
2266 ASSERT_VOP_IN_SEQC(tdvp);
2268 ASSERT_VOP_IN_SEQC(tvp);
2273 KASSERT(!cache_remove_cnp(tdvp, tcnp),
2274 ("%s: lingering negative entry", __func__));
2276 cache_remove_cnp(tdvp, tcnp);
2281 * Flush all entries referencing a particular filesystem.
2284 cache_purgevfs(struct mount *mp)
2286 struct vnode *vp, *mvp;
2288 SDT_PROBE1(vfs, namecache, purgevfs, done, mp);
2290 * Somewhat wasteful iteration over all vnodes. Would be better to
2291 * support filtering and avoid the interlock to begin with.
2293 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
2294 if (!cache_has_entries(vp)) {
2306 * Perform canonical checks and cache lookup and pass on to filesystem
2307 * through the vop_cachedlookup only if needed.
2311 vfs_cache_lookup(struct vop_lookup_args *ap)
2315 struct vnode **vpp = ap->a_vpp;
2316 struct componentname *cnp = ap->a_cnp;
2317 int flags = cnp->cn_flags;
2322 if (dvp->v_type != VDIR)
2325 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
2326 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
2329 error = vn_dir_check_exec(dvp, cnp);
2333 error = cache_lookup(dvp, vpp, cnp, NULL, NULL);
2335 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
2341 /* Implementation of the getcwd syscall. */
2343 sys___getcwd(struct thread *td, struct __getcwd_args *uap)
2349 buflen = uap->buflen;
2350 if (__predict_false(buflen < 2))
2352 if (buflen > MAXPATHLEN)
2353 buflen = MAXPATHLEN;
2355 buf = uma_zalloc(namei_zone, M_WAITOK);
2356 error = vn_getcwd(buf, &retbuf, &buflen);
2358 error = copyout(retbuf, uap->buf, buflen);
2359 uma_zfree(namei_zone, buf);
2364 vn_getcwd(char *buf, char **retbuf, size_t *buflen)
2370 pwd = pwd_get_smr();
2371 error = vn_fullpath_any_smr(pwd->pwd_cdir, pwd->pwd_rdir, buf, retbuf,
2373 VFS_SMR_ASSERT_NOT_ENTERED();
2375 pwd = pwd_hold(curthread);
2376 error = vn_fullpath_any(pwd->pwd_cdir, pwd->pwd_rdir, buf,
2382 if (KTRPOINT(curthread, KTR_NAMEI) && error == 0)
2389 kern___realpathat(struct thread *td, int fd, const char *path, char *buf,
2390 size_t size, int flags, enum uio_seg pathseg)
2392 struct nameidata nd;
2393 char *retbuf, *freebuf;
2398 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | SAVENAME | WANTPARENT | AUDITVNODE1,
2399 pathseg, path, fd, &cap_fstat_rights, td);
2400 if ((error = namei(&nd)) != 0)
2402 error = vn_fullpath_hardlink(&nd, &retbuf, &freebuf, &size);
2404 error = copyout(retbuf, buf, size);
2405 free(freebuf, M_TEMP);
2412 sys___realpathat(struct thread *td, struct __realpathat_args *uap)
2415 return (kern___realpathat(td, uap->fd, uap->path, uap->buf, uap->size,
2416 uap->flags, UIO_USERSPACE));
2420 * Retrieve the full filesystem path that correspond to a vnode from the name
2421 * cache (if available)
2424 vn_fullpath(struct vnode *vp, char **retbuf, char **freebuf)
2431 if (__predict_false(vp == NULL))
2434 buflen = MAXPATHLEN;
2435 buf = malloc(buflen, M_TEMP, M_WAITOK);
2437 pwd = pwd_get_smr();
2438 error = vn_fullpath_any_smr(vp, pwd->pwd_rdir, buf, retbuf, &buflen, false, 0);
2439 VFS_SMR_ASSERT_NOT_ENTERED();
2441 pwd = pwd_hold(curthread);
2442 error = vn_fullpath_any(vp, pwd->pwd_rdir, buf, retbuf, &buflen);
2453 * This function is similar to vn_fullpath, but it attempts to lookup the
2454 * pathname relative to the global root mount point. This is required for the
2455 * auditing sub-system, as audited pathnames must be absolute, relative to the
2456 * global root mount point.
2459 vn_fullpath_global(struct vnode *vp, char **retbuf, char **freebuf)
2465 if (__predict_false(vp == NULL))
2467 buflen = MAXPATHLEN;
2468 buf = malloc(buflen, M_TEMP, M_WAITOK);
2470 error = vn_fullpath_any_smr(vp, rootvnode, buf, retbuf, &buflen, false, 0);
2471 VFS_SMR_ASSERT_NOT_ENTERED();
2473 error = vn_fullpath_any(vp, rootvnode, buf, retbuf, &buflen);
2482 static struct namecache *
2483 vn_dd_from_dst(struct vnode *vp)
2485 struct namecache *ncp;
2487 cache_assert_vnode_locked(vp);
2488 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst) {
2489 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2496 vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, size_t *buflen)
2499 struct namecache *ncp;
2503 vlp = VP2VNODELOCK(*vp);
2505 ncp = (*vp)->v_cache_dd;
2506 if (ncp != NULL && (ncp->nc_flag & NCF_ISDOTDOT) == 0) {
2507 KASSERT(ncp == vn_dd_from_dst(*vp),
2508 ("%s: mismatch for dd entry (%p != %p)", __func__,
2509 ncp, vn_dd_from_dst(*vp)));
2511 ncp = vn_dd_from_dst(*vp);
2514 if (*buflen < ncp->nc_nlen) {
2517 counter_u64_add(numfullpathfail4, 1);
2519 SDT_PROBE3(vfs, namecache, fullpath, return, error,
2523 *buflen -= ncp->nc_nlen;
2524 memcpy(buf + *buflen, ncp->nc_name, ncp->nc_nlen);
2525 SDT_PROBE3(vfs, namecache, fullpath, hit, ncp->nc_dvp,
2534 SDT_PROBE1(vfs, namecache, fullpath, miss, vp);
2537 vn_lock(*vp, LK_SHARED | LK_RETRY);
2538 error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen);
2541 counter_u64_add(numfullpathfail2, 1);
2542 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2547 if (VN_IS_DOOMED(dvp)) {
2548 /* forced unmount */
2551 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2555 * *vp has its use count incremented still.
2562 * Resolve a directory to a pathname.
2564 * The name of the directory can always be found in the namecache or fetched
2565 * from the filesystem. There is also guaranteed to be only one parent, meaning
2566 * we can just follow vnodes up until we find the root.
2568 * The vnode must be referenced.
2571 vn_fullpath_dir(struct vnode *vp, struct vnode *rdir, char *buf, char **retbuf,
2572 size_t *len, bool slash_prefixed, size_t addend)
2574 #ifdef KDTRACE_HOOKS
2575 struct vnode *startvp = vp;
2581 VNPASS(vp->v_type == VDIR || VN_IS_DOOMED(vp), vp);
2582 VNPASS(vp->v_usecount > 0, vp);
2586 if (!slash_prefixed) {
2594 SDT_PROBE1(vfs, namecache, fullpath, entry, vp);
2595 counter_u64_add(numfullpathcalls, 1);
2596 while (vp != rdir && vp != rootvnode) {
2598 * The vp vnode must be already fully constructed,
2599 * since it is either found in namecache or obtained
2600 * from VOP_VPTOCNP(). We may test for VV_ROOT safely
2601 * without obtaining the vnode lock.
2603 if ((vp->v_vflag & VV_ROOT) != 0) {
2604 vn_lock(vp, LK_RETRY | LK_SHARED);
2607 * With the vnode locked, check for races with
2608 * unmount, forced or not. Note that we
2609 * already verified that vp is not equal to
2610 * the root vnode, which means that
2611 * mnt_vnodecovered can be NULL only for the
2614 if (VN_IS_DOOMED(vp) ||
2615 (vp1 = vp->v_mount->mnt_vnodecovered) == NULL ||
2616 vp1->v_mountedhere != vp->v_mount) {
2619 SDT_PROBE3(vfs, namecache, fullpath, return,
2629 if (vp->v_type != VDIR) {
2631 counter_u64_add(numfullpathfail1, 1);
2633 SDT_PROBE3(vfs, namecache, fullpath, return,
2637 error = vn_vptocnp(&vp, curthread->td_ucred, buf, &buflen);
2643 SDT_PROBE3(vfs, namecache, fullpath, return, error,
2647 buf[--buflen] = '/';
2648 slash_prefixed = true;
2652 if (!slash_prefixed) {
2655 counter_u64_add(numfullpathfail4, 1);
2656 SDT_PROBE3(vfs, namecache, fullpath, return, ENOMEM,
2660 buf[--buflen] = '/';
2662 counter_u64_add(numfullpathfound, 1);
2665 *retbuf = buf + buflen;
2666 SDT_PROBE3(vfs, namecache, fullpath, return, 0, startvp, *retbuf);
2673 * Resolve an arbitrary vnode to a pathname.
2676 * - hardlinks are not tracked, thus if the vnode is not a directory this can
2677 * resolve to a different path than the one used to find it
2678 * - namecache is not mandatory, meaning names are not guaranteed to be added
2679 * (in which case resolving fails)
2681 static void __inline
2682 cache_rev_failed_impl(int *reason, int line)
2687 #define cache_rev_failed(var) cache_rev_failed_impl((var), __LINE__)
2690 vn_fullpath_any_smr(struct vnode *vp, struct vnode *rdir, char *buf,
2691 char **retbuf, size_t *buflen, bool slash_prefixed, size_t addend)
2693 #ifdef KDTRACE_HOOKS
2694 struct vnode *startvp = vp;
2698 struct namecache *ncp;
2702 #ifdef KDTRACE_HOOKS
2705 seqc_t vp_seqc, tvp_seqc;
2708 VFS_SMR_ASSERT_ENTERED();
2710 if (!cache_fast_revlookup) {
2715 orig_buflen = *buflen;
2717 if (!slash_prefixed) {
2718 MPASS(*buflen >= 2);
2720 buf[*buflen] = '\0';
2723 if (vp == rdir || vp == rootvnode) {
2724 if (!slash_prefixed) {
2731 #ifdef KDTRACE_HOOKS
2735 ncp = NULL; /* for sdt probe down below */
2736 vp_seqc = vn_seqc_read_any(vp);
2737 if (seqc_in_modify(vp_seqc)) {
2738 cache_rev_failed(&reason);
2743 #ifdef KDTRACE_HOOKS
2746 if ((vp->v_vflag & VV_ROOT) != 0) {
2747 mp = atomic_load_ptr(&vp->v_mount);
2749 cache_rev_failed(&reason);
2752 tvp = atomic_load_ptr(&mp->mnt_vnodecovered);
2753 tvp_seqc = vn_seqc_read_any(tvp);
2754 if (seqc_in_modify(tvp_seqc)) {
2755 cache_rev_failed(&reason);
2758 if (!vn_seqc_consistent(vp, vp_seqc)) {
2759 cache_rev_failed(&reason);
2766 ncp = atomic_load_ptr(&vp->v_cache_dd);
2768 cache_rev_failed(&reason);
2771 nc_flag = atomic_load_char(&ncp->nc_flag);
2772 if ((nc_flag & NCF_ISDOTDOT) != 0) {
2773 cache_rev_failed(&reason);
2776 if (!cache_ncp_canuse(ncp)) {
2777 cache_rev_failed(&reason);
2780 if (ncp->nc_nlen >= *buflen) {
2781 cache_rev_failed(&reason);
2785 *buflen -= ncp->nc_nlen;
2786 memcpy(buf + *buflen, ncp->nc_name, ncp->nc_nlen);
2790 tvp_seqc = vn_seqc_read_any(tvp);
2791 if (seqc_in_modify(tvp_seqc)) {
2792 cache_rev_failed(&reason);
2795 if (!vn_seqc_consistent(vp, vp_seqc)) {
2796 cache_rev_failed(&reason);
2801 if (vp == rdir || vp == rootvnode)
2806 *retbuf = buf + *buflen;
2807 *buflen = orig_buflen - *buflen + addend;
2808 SDT_PROBE2(vfs, namecache, fullpath_smr, hit, startvp, *retbuf);
2812 *buflen = orig_buflen;
2813 SDT_PROBE4(vfs, namecache, fullpath_smr, miss, startvp, ncp, reason, i);
2819 vn_fullpath_any(struct vnode *vp, struct vnode *rdir, char *buf, char **retbuf,
2823 bool slash_prefixed;
2829 orig_buflen = *buflen;
2832 slash_prefixed = false;
2833 if (vp->v_type != VDIR) {
2835 buf[*buflen] = '\0';
2836 error = vn_vptocnp(&vp, curthread->td_ucred, buf, buflen);
2845 slash_prefixed = true;
2848 return (vn_fullpath_dir(vp, rdir, buf, retbuf, buflen, slash_prefixed,
2849 orig_buflen - *buflen));
2853 * Resolve an arbitrary vnode to a pathname (taking care of hardlinks).
2855 * Since the namecache does not track handlings, the caller is expected to first
2856 * look up the target vnode with SAVENAME | WANTPARENT flags passed to namei.
2858 * Then we have 2 cases:
2859 * - if the found vnode is a directory, the path can be constructed just by
2860 * fullowing names up the chain
2861 * - otherwise we populate the buffer with the saved name and start resolving
2865 vn_fullpath_hardlink(struct nameidata *ndp, char **retbuf, char **freebuf,
2870 struct componentname *cnp;
2874 bool slash_prefixed;
2879 if (*buflen > MAXPATHLEN)
2880 *buflen = MAXPATHLEN;
2882 slash_prefixed = false;
2884 buf = malloc(*buflen, M_TEMP, M_WAITOK);
2889 * Check for VBAD to work around the vp_crossmp bug in lookup().
2891 * For example consider tmpfs on /tmp and realpath /tmp. ni_vp will be
2892 * set to mount point's root vnode while ni_dvp will be vp_crossmp.
2893 * If the type is VDIR (like in this very case) we can skip looking
2894 * at ni_dvp in the first place. However, since vnodes get passed here
2895 * unlocked the target may transition to doomed state (type == VBAD)
2896 * before we get to evaluate the condition. If this happens, we will
2897 * populate part of the buffer and descend to vn_fullpath_dir with
2898 * vp == vp_crossmp. Prevent the problem by checking for VBAD.
2900 * This should be atomic_load(&vp->v_type) but it is ilegal to take
2901 * an address of a bit field, even if said field is sized to char.
2902 * Work around the problem by reading the value into a full-sized enum
2903 * and then re-reading it with atomic_load which will still prevent
2904 * the compiler from re-reading down the road.
2907 type = atomic_load_int(&type);
2914 addend = cnp->cn_namelen + 2;
2915 if (*buflen < addend) {
2920 tmpbuf = buf + *buflen;
2922 memcpy(&tmpbuf[1], cnp->cn_nameptr, cnp->cn_namelen);
2923 tmpbuf[addend - 1] = '\0';
2924 slash_prefixed = true;
2929 pwd = pwd_get_smr();
2930 error = vn_fullpath_any_smr(vp, pwd->pwd_rdir, buf, retbuf, buflen,
2931 slash_prefixed, addend);
2932 VFS_SMR_ASSERT_NOT_ENTERED();
2934 pwd = pwd_hold(curthread);
2936 error = vn_fullpath_dir(vp, pwd->pwd_rdir, buf, retbuf, buflen,
2937 slash_prefixed, addend);
2952 vn_dir_dd_ino(struct vnode *vp)
2954 struct namecache *ncp;
2959 ASSERT_VOP_LOCKED(vp, "vn_dir_dd_ino");
2960 vlp = VP2VNODELOCK(vp);
2962 TAILQ_FOREACH(ncp, &(vp->v_cache_dst), nc_dst) {
2963 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0)
2966 vs = vget_prep(ddvp);
2968 if (vget_finish(ddvp, LK_SHARED | LK_NOWAIT, vs))
2977 vn_commname(struct vnode *vp, char *buf, u_int buflen)
2979 struct namecache *ncp;
2983 vlp = VP2VNODELOCK(vp);
2985 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst)
2986 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2992 l = min(ncp->nc_nlen, buflen - 1);
2993 memcpy(buf, ncp->nc_name, l);
3000 * This function updates path string to vnode's full global path
3001 * and checks the size of the new path string against the pathlen argument.
3003 * Requires a locked, referenced vnode.
3004 * Vnode is re-locked on success or ENODEV, otherwise unlocked.
3006 * If vp is a directory, the call to vn_fullpath_global() always succeeds
3007 * because it falls back to the ".." lookup if the namecache lookup fails.
3010 vn_path_to_global_path(struct thread *td, struct vnode *vp, char *path,
3013 struct nameidata nd;
3018 ASSERT_VOP_ELOCKED(vp, __func__);
3020 /* Construct global filesystem path from vp. */
3022 error = vn_fullpath_global(vp, &rpath, &fbuf);
3029 if (strlen(rpath) >= pathlen) {
3031 error = ENAMETOOLONG;
3036 * Re-lookup the vnode by path to detect a possible rename.
3037 * As a side effect, the vnode is relocked.
3038 * If vnode was renamed, return ENOENT.
3040 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
3041 UIO_SYSSPACE, path, td);
3047 NDFREE(&nd, NDF_ONLY_PNBUF);
3051 strcpy(path, rpath);
3064 db_print_vpath(struct vnode *vp)
3067 while (vp != NULL) {
3068 db_printf("%p: ", vp);
3069 if (vp == rootvnode) {
3073 if (vp->v_vflag & VV_ROOT) {
3074 db_printf("<mount point>");
3075 vp = vp->v_mount->mnt_vnodecovered;
3077 struct namecache *ncp;
3081 ncp = TAILQ_FIRST(&vp->v_cache_dst);
3084 for (i = 0; i < ncp->nc_nlen; i++)
3085 db_printf("%c", *ncn++);
3098 DB_SHOW_COMMAND(vpath, db_show_vpath)
3103 db_printf("usage: show vpath <struct vnode *>\n");
3107 vp = (struct vnode *)addr;
3113 static bool __read_frequently cache_fast_lookup = true;
3114 SYSCTL_BOOL(_vfs, OID_AUTO, cache_fast_lookup, CTLFLAG_RW,
3115 &cache_fast_lookup, 0, "");
3117 #define CACHE_FPL_FAILED -2020
3120 cache_fpl_cleanup_cnp(struct componentname *cnp)
3123 uma_zfree(namei_zone, cnp->cn_pnbuf);
3125 cnp->cn_pnbuf = NULL;
3126 cnp->cn_nameptr = NULL;
3131 cache_fpl_handle_root(struct nameidata *ndp, struct vnode **dpp)
3133 struct componentname *cnp;
3136 while (*(cnp->cn_nameptr) == '/') {
3141 *dpp = ndp->ni_rootdir;
3145 * Components of nameidata (or objects it can point to) which may
3146 * need restoring in case fast path lookup fails.
3148 struct nameidata_saved {
3156 struct nameidata *ndp;
3157 struct componentname *cnp;
3163 struct nameidata_saved snd;
3165 enum cache_fpl_status status:8;
3171 cache_fpl_checkpoint(struct cache_fpl *fpl, struct nameidata_saved *snd)
3174 snd->cn_flags = fpl->ndp->ni_cnd.cn_flags;
3175 snd->cn_namelen = fpl->ndp->ni_cnd.cn_namelen;
3176 snd->cn_nameptr = fpl->ndp->ni_cnd.cn_nameptr;
3177 snd->ni_pathlen = fpl->ndp->ni_pathlen;
3181 cache_fpl_restore(struct cache_fpl *fpl, struct nameidata_saved *snd)
3184 fpl->ndp->ni_cnd.cn_flags = snd->cn_flags;
3185 fpl->ndp->ni_cnd.cn_namelen = snd->cn_namelen;
3186 fpl->ndp->ni_cnd.cn_nameptr = snd->cn_nameptr;
3187 fpl->ndp->ni_pathlen = snd->ni_pathlen;
3191 #define cache_fpl_smr_assert_entered(fpl) ({ \
3192 struct cache_fpl *_fpl = (fpl); \
3193 MPASS(_fpl->in_smr == true); \
3194 VFS_SMR_ASSERT_ENTERED(); \
3196 #define cache_fpl_smr_assert_not_entered(fpl) ({ \
3197 struct cache_fpl *_fpl = (fpl); \
3198 MPASS(_fpl->in_smr == false); \
3199 VFS_SMR_ASSERT_NOT_ENTERED(); \
3202 #define cache_fpl_smr_assert_entered(fpl) do { } while (0)
3203 #define cache_fpl_smr_assert_not_entered(fpl) do { } while (0)
3206 #define cache_fpl_smr_enter_initial(fpl) ({ \
3207 struct cache_fpl *_fpl = (fpl); \
3209 _fpl->in_smr = true; \
3212 #define cache_fpl_smr_enter(fpl) ({ \
3213 struct cache_fpl *_fpl = (fpl); \
3214 MPASS(_fpl->in_smr == false); \
3216 _fpl->in_smr = true; \
3219 #define cache_fpl_smr_exit(fpl) ({ \
3220 struct cache_fpl *_fpl = (fpl); \
3221 MPASS(_fpl->in_smr == true); \
3223 _fpl->in_smr = false; \
3227 cache_fpl_aborted_impl(struct cache_fpl *fpl, int line)
3230 if (fpl->status != CACHE_FPL_STATUS_UNSET) {
3231 KASSERT(fpl->status == CACHE_FPL_STATUS_PARTIAL,
3232 ("%s: converting to abort from %d at %d, set at %d\n",
3233 __func__, fpl->status, line, fpl->line));
3235 fpl->status = CACHE_FPL_STATUS_ABORTED;
3237 return (CACHE_FPL_FAILED);
3240 #define cache_fpl_aborted(x) cache_fpl_aborted_impl((x), __LINE__)
3243 cache_fpl_partial_impl(struct cache_fpl *fpl, int line)
3246 KASSERT(fpl->status == CACHE_FPL_STATUS_UNSET,
3247 ("%s: setting to partial at %d, but already set to %d at %d\n",
3248 __func__, line, fpl->status, fpl->line));
3249 cache_fpl_smr_assert_entered(fpl);
3250 fpl->status = CACHE_FPL_STATUS_PARTIAL;
3252 return (CACHE_FPL_FAILED);
3255 #define cache_fpl_partial(x) cache_fpl_partial_impl((x), __LINE__)
3258 cache_fpl_handled_impl(struct cache_fpl *fpl, int error, int line)
3261 KASSERT(fpl->status == CACHE_FPL_STATUS_UNSET,
3262 ("%s: setting to handled at %d, but already set to %d at %d\n",
3263 __func__, line, fpl->status, fpl->line));
3264 cache_fpl_smr_assert_not_entered(fpl);
3265 MPASS(error != CACHE_FPL_FAILED);
3266 fpl->status = CACHE_FPL_STATUS_HANDLED;
3271 #define cache_fpl_handled(x, e) cache_fpl_handled_impl((x), (e), __LINE__)
3273 #define CACHE_FPL_SUPPORTED_CN_FLAGS \
3274 (LOCKLEAF | LOCKPARENT | WANTPARENT | NOCACHE | FOLLOW | LOCKSHARED | SAVENAME | \
3275 SAVESTART | WILLBEDIR | ISOPEN | NOMACCHECK | AUDITVNODE1 | AUDITVNODE2 | NOCAPCHECK)
3277 #define CACHE_FPL_INTERNAL_CN_FLAGS \
3278 (ISDOTDOT | MAKEENTRY | ISLASTCN)
3280 _Static_assert((CACHE_FPL_SUPPORTED_CN_FLAGS & CACHE_FPL_INTERNAL_CN_FLAGS) == 0,
3281 "supported and internal flags overlap");
3284 cache_fpl_islastcn(struct nameidata *ndp)
3287 return (*ndp->ni_next == 0);
3291 cache_fpl_isdotdot(struct componentname *cnp)
3294 if (cnp->cn_namelen == 2 &&
3295 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
3301 cache_can_fplookup(struct cache_fpl *fpl)
3303 struct nameidata *ndp;
3304 struct componentname *cnp;
3309 td = cnp->cn_thread;
3311 if (!cache_fast_lookup) {
3312 cache_fpl_aborted(fpl);
3316 if (mac_vnode_check_lookup_enabled()) {
3317 cache_fpl_aborted(fpl);
3321 if ((cnp->cn_flags & ~CACHE_FPL_SUPPORTED_CN_FLAGS) != 0) {
3322 cache_fpl_aborted(fpl);
3325 if (IN_CAPABILITY_MODE(td)) {
3326 cache_fpl_aborted(fpl);
3329 if (AUDITING_TD(td)) {
3330 cache_fpl_aborted(fpl);
3333 if (ndp->ni_startdir != NULL) {
3334 cache_fpl_aborted(fpl);
3341 cache_fplookup_dirfd(struct cache_fpl *fpl, struct vnode **vpp)
3343 struct nameidata *ndp;
3348 error = fgetvp_lookup_smr(ndp->ni_dirfd, ndp, vpp, &fsearch);
3349 if (__predict_false(error != 0)) {
3350 cache_fpl_smr_exit(fpl);
3351 return (cache_fpl_aborted(fpl));
3353 fpl->fsearch = fsearch;
3358 cache_fplookup_vnode_supported(struct vnode *vp)
3361 return (vp->v_type != VLNK);
3365 * Move a negative entry to the hot list.
3367 * We have to take locks, but they may be contended and in the worst
3368 * case we may need to go off CPU. We don't want to spin within the
3369 * smr section and we can't block with it. Instead we are going to
3370 * look up the entry again.
3372 static int __noinline
3373 cache_fplookup_negative_promote(struct cache_fpl *fpl, struct namecache *oncp,
3376 struct componentname *cnp;
3377 struct namecache *ncp;
3378 struct neglist *neglist;
3379 struct negstate *negstate;
3386 if (!vhold_smr(dvp))
3387 return (cache_fpl_aborted(fpl));
3389 neglist = NCP2NEGLIST(oncp);
3390 cache_fpl_smr_exit(fpl);
3392 mtx_lock(&neglist->nl_lock);
3394 * For hash iteration.
3396 cache_fpl_smr_enter(fpl);
3399 * Avoid all surprises by only succeeding if we got the same entry and
3400 * bailing completely otherwise.
3402 * In particular at this point there can be a new ncp which matches the
3403 * search but hashes to a different neglist.
3405 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
3411 * No match to begin with.
3413 if (__predict_false(ncp == NULL)) {
3418 * The newly found entry may be something different...
3420 if (!(ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
3421 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))) {
3426 * ... and not even negative.
3428 nc_flag = atomic_load_char(&ncp->nc_flag);
3429 if ((nc_flag & NCF_NEGATIVE) == 0) {
3433 if (__predict_false(!cache_ncp_canuse(ncp))) {
3437 negstate = NCP2NEGSTATE(ncp);
3438 if ((negstate->neg_flag & NEG_HOT) == 0) {
3439 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
3440 TAILQ_INSERT_TAIL(&neglist->nl_hotlist, ncp, nc_dst);
3441 neglist->nl_hotnum++;
3442 negstate->neg_flag |= NEG_HOT;
3445 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp, ncp->nc_name);
3446 counter_u64_add(numneghits, 1);
3447 cache_fpl_smr_exit(fpl);
3448 mtx_unlock(&neglist->nl_lock);
3450 return (cache_fpl_handled(fpl, ENOENT));
3452 cache_fpl_smr_exit(fpl);
3453 mtx_unlock(&neglist->nl_lock);
3455 return (cache_fpl_aborted(fpl));
3459 * The target vnode is not supported, prepare for the slow path to take over.
3461 static int __noinline
3462 cache_fplookup_partial_setup(struct cache_fpl *fpl)
3464 struct nameidata *ndp;
3465 struct componentname *cnp;
3475 dvp_seqc = fpl->dvp_seqc;
3477 if (!pwd_hold_smr(pwd)) {
3478 cache_fpl_smr_exit(fpl);
3479 return (cache_fpl_aborted(fpl));
3482 dvs = vget_prep_smr(dvp);
3483 cache_fpl_smr_exit(fpl);
3484 if (__predict_false(dvs == VGET_NONE)) {
3486 return (cache_fpl_aborted(fpl));
3489 vget_finish_ref(dvp, dvs);
3490 if (!vn_seqc_consistent(dvp, dvp_seqc)) {
3493 return (cache_fpl_aborted(fpl));
3496 cache_fpl_restore(fpl, &fpl->snd);
3498 ndp->ni_startdir = dvp;
3499 cnp->cn_flags |= MAKEENTRY;
3500 if (cache_fpl_islastcn(ndp))
3501 cnp->cn_flags |= ISLASTCN;
3502 if (cache_fpl_isdotdot(cnp))
3503 cnp->cn_flags |= ISDOTDOT;
3509 cache_fplookup_final_child(struct cache_fpl *fpl, enum vgetstate tvs)
3511 struct componentname *cnp;
3518 tvp_seqc = fpl->tvp_seqc;
3520 if ((cnp->cn_flags & LOCKLEAF) != 0) {
3521 lkflags = LK_SHARED;
3522 if ((cnp->cn_flags & LOCKSHARED) == 0)
3523 lkflags = LK_EXCLUSIVE;
3524 error = vget_finish(tvp, lkflags, tvs);
3525 if (__predict_false(error != 0)) {
3526 return (cache_fpl_aborted(fpl));
3529 vget_finish_ref(tvp, tvs);
3532 if (!vn_seqc_consistent(tvp, tvp_seqc)) {
3533 if ((cnp->cn_flags & LOCKLEAF) != 0)
3537 return (cache_fpl_aborted(fpl));
3540 return (cache_fpl_handled(fpl, 0));
3544 * They want to possibly modify the state of the namecache.
3546 * Don't try to match the API contract, just leave.
3547 * TODO: this leaves scalability on the table
3550 cache_fplookup_final_modifying(struct cache_fpl *fpl)
3552 struct componentname *cnp;
3555 MPASS(cnp->cn_nameiop != LOOKUP);
3556 return (cache_fpl_partial(fpl));
3559 static int __noinline
3560 cache_fplookup_final_withparent(struct cache_fpl *fpl)
3562 struct componentname *cnp;
3563 enum vgetstate dvs, tvs;
3564 struct vnode *dvp, *tvp;
3570 dvp_seqc = fpl->dvp_seqc;
3573 MPASS((cnp->cn_flags & (LOCKPARENT|WANTPARENT)) != 0);
3576 * This is less efficient than it can be for simplicity.
3578 dvs = vget_prep_smr(dvp);
3579 if (__predict_false(dvs == VGET_NONE)) {
3580 return (cache_fpl_aborted(fpl));
3582 tvs = vget_prep_smr(tvp);
3583 if (__predict_false(tvs == VGET_NONE)) {
3584 cache_fpl_smr_exit(fpl);
3585 vget_abort(dvp, dvs);
3586 return (cache_fpl_aborted(fpl));
3589 cache_fpl_smr_exit(fpl);
3591 if ((cnp->cn_flags & LOCKPARENT) != 0) {
3592 error = vget_finish(dvp, LK_EXCLUSIVE, dvs);
3593 if (__predict_false(error != 0)) {
3594 vget_abort(tvp, tvs);
3595 return (cache_fpl_aborted(fpl));
3598 vget_finish_ref(dvp, dvs);
3601 if (!vn_seqc_consistent(dvp, dvp_seqc)) {
3602 vget_abort(tvp, tvs);
3603 if ((cnp->cn_flags & LOCKPARENT) != 0)
3607 return (cache_fpl_aborted(fpl));
3610 error = cache_fplookup_final_child(fpl, tvs);
3611 if (__predict_false(error != 0)) {
3612 MPASS(fpl->status == CACHE_FPL_STATUS_ABORTED);
3613 if ((cnp->cn_flags & LOCKPARENT) != 0)
3620 MPASS(fpl->status == CACHE_FPL_STATUS_HANDLED);
3625 cache_fplookup_final(struct cache_fpl *fpl)
3627 struct componentname *cnp;
3629 struct vnode *dvp, *tvp;
3634 dvp_seqc = fpl->dvp_seqc;
3637 VNPASS(cache_fplookup_vnode_supported(dvp), dvp);
3639 if (cnp->cn_nameiop != LOOKUP) {
3640 return (cache_fplookup_final_modifying(fpl));
3643 if ((cnp->cn_flags & (LOCKPARENT|WANTPARENT)) != 0)
3644 return (cache_fplookup_final_withparent(fpl));
3646 tvs = vget_prep_smr(tvp);
3647 if (__predict_false(tvs == VGET_NONE)) {
3648 return (cache_fpl_partial(fpl));
3651 if (!vn_seqc_consistent(dvp, dvp_seqc)) {
3652 cache_fpl_smr_exit(fpl);
3653 vget_abort(tvp, tvs);
3654 return (cache_fpl_aborted(fpl));
3657 cache_fpl_smr_exit(fpl);
3658 return (cache_fplookup_final_child(fpl, tvs));
3661 static int __noinline
3662 cache_fplookup_dot(struct cache_fpl *fpl)
3669 fpl->tvp_seqc = vn_seqc_read_any(dvp);
3670 if (seqc_in_modify(fpl->tvp_seqc)) {
3671 return (cache_fpl_aborted(fpl));
3674 counter_u64_add(dothits, 1);
3675 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", dvp);
3680 static int __noinline
3681 cache_fplookup_dotdot(struct cache_fpl *fpl)
3683 struct nameidata *ndp;
3684 struct componentname *cnp;
3685 struct namecache *ncp;
3695 * XXX this is racy the same way regular lookup is
3697 for (pr = cnp->cn_cred->cr_prison; pr != NULL;
3699 if (dvp == pr->pr_root)
3702 if (dvp == ndp->ni_rootdir ||
3703 dvp == ndp->ni_topdir ||
3707 fpl->tvp_seqc = vn_seqc_read_any(dvp);
3708 if (seqc_in_modify(fpl->tvp_seqc)) {
3709 return (cache_fpl_aborted(fpl));
3714 if ((dvp->v_vflag & VV_ROOT) != 0) {
3717 * The opposite of climb mount is needed here.
3719 return (cache_fpl_aborted(fpl));
3722 ncp = atomic_load_ptr(&dvp->v_cache_dd);
3724 return (cache_fpl_aborted(fpl));
3727 nc_flag = atomic_load_char(&ncp->nc_flag);
3728 if ((nc_flag & NCF_ISDOTDOT) != 0) {
3729 if ((nc_flag & NCF_NEGATIVE) != 0)
3730 return (cache_fpl_aborted(fpl));
3731 fpl->tvp = ncp->nc_vp;
3733 fpl->tvp = ncp->nc_dvp;
3736 if (__predict_false(!cache_ncp_canuse(ncp))) {
3737 return (cache_fpl_aborted(fpl));
3740 fpl->tvp_seqc = vn_seqc_read_any(fpl->tvp);
3741 if (seqc_in_modify(fpl->tvp_seqc)) {
3742 return (cache_fpl_partial(fpl));
3745 counter_u64_add(dotdothits, 1);
3750 cache_fplookup_next(struct cache_fpl *fpl)
3752 struct componentname *cnp;
3753 struct namecache *ncp;
3754 struct negstate *negstate;
3755 struct vnode *dvp, *tvp;
3763 if (__predict_false(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.')) {
3764 return (cache_fplookup_dot(fpl));
3767 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
3769 CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
3770 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
3771 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
3776 * If there is no entry we have to punt to the slow path to perform
3777 * actual lookup. Should there be nothing with this name a negative
3778 * entry will be created.
3780 if (__predict_false(ncp == NULL)) {
3781 return (cache_fpl_partial(fpl));
3784 tvp = atomic_load_ptr(&ncp->nc_vp);
3785 nc_flag = atomic_load_char(&ncp->nc_flag);
3786 if ((nc_flag & NCF_NEGATIVE) != 0) {
3788 * If they want to create an entry we need to replace this one.
3790 if (__predict_false(fpl->cnp->cn_nameiop != LOOKUP)) {
3791 return (cache_fpl_partial(fpl));
3793 negstate = NCP2NEGSTATE(ncp);
3794 neg_hot = ((negstate->neg_flag & NEG_HOT) != 0);
3795 if (__predict_false(!cache_ncp_canuse(ncp))) {
3796 return (cache_fpl_partial(fpl));
3798 if (__predict_false((nc_flag & NCF_WHITE) != 0)) {
3799 return (cache_fpl_partial(fpl));
3802 return (cache_fplookup_negative_promote(fpl, ncp, hash));
3804 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp,
3806 counter_u64_add(numneghits, 1);
3807 cache_fpl_smr_exit(fpl);
3808 return (cache_fpl_handled(fpl, ENOENT));
3811 if (__predict_false(!cache_ncp_canuse(ncp))) {
3812 return (cache_fpl_partial(fpl));
3816 fpl->tvp_seqc = vn_seqc_read_any(tvp);
3817 if (seqc_in_modify(fpl->tvp_seqc)) {
3818 return (cache_fpl_partial(fpl));
3821 if (!cache_fplookup_vnode_supported(tvp)) {
3822 return (cache_fpl_partial(fpl));
3825 counter_u64_add(numposhits, 1);
3826 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name, tvp);
3831 cache_fplookup_mp_supported(struct mount *mp)
3836 if ((mp->mnt_kern_flag & MNTK_FPLOOKUP) == 0)
3842 * Walk up the mount stack (if any).
3844 * Correctness is provided in the following ways:
3845 * - all vnodes are protected from freeing with SMR
3846 * - struct mount objects are type stable making them always safe to access
3847 * - stability of the particular mount is provided by busying it
3848 * - relationship between the vnode which is mounted on and the mount is
3849 * verified with the vnode sequence counter after busying
3850 * - association between root vnode of the mount and the mount is protected
3853 * From that point on we can read the sequence counter of the root vnode
3854 * and get the next mount on the stack (if any) using the same protection.
3856 * By the end of successful walk we are guaranteed the reached state was
3857 * indeed present at least at some point which matches the regular lookup.
3859 static int __noinline
3860 cache_fplookup_climb_mount(struct cache_fpl *fpl)
3862 struct mount *mp, *prev_mp;
3867 vp_seqc = fpl->tvp_seqc;
3869 VNPASS(vp->v_type == VDIR || vp->v_type == VBAD, vp);
3870 mp = atomic_load_ptr(&vp->v_mountedhere);
3876 if (!vfs_op_thread_enter_crit(mp)) {
3877 if (prev_mp != NULL)
3878 vfs_op_thread_exit_crit(prev_mp);
3879 return (cache_fpl_partial(fpl));
3881 if (prev_mp != NULL)
3882 vfs_op_thread_exit_crit(prev_mp);
3883 if (!vn_seqc_consistent(vp, vp_seqc)) {
3884 vfs_op_thread_exit_crit(mp);
3885 return (cache_fpl_partial(fpl));
3887 if (!cache_fplookup_mp_supported(mp)) {
3888 vfs_op_thread_exit_crit(mp);
3889 return (cache_fpl_partial(fpl));
3891 vp = atomic_load_ptr(&mp->mnt_rootvnode);
3892 if (vp == NULL || VN_IS_DOOMED(vp)) {
3893 vfs_op_thread_exit_crit(mp);
3894 return (cache_fpl_partial(fpl));
3896 vp_seqc = vn_seqc_read_any(vp);
3897 if (seqc_in_modify(vp_seqc)) {
3898 vfs_op_thread_exit_crit(mp);
3899 return (cache_fpl_partial(fpl));
3902 mp = atomic_load_ptr(&vp->v_mountedhere);
3907 vfs_op_thread_exit_crit(prev_mp);
3909 fpl->tvp_seqc = vp_seqc;
3914 cache_fplookup_need_climb_mount(struct cache_fpl *fpl)
3922 * Hack: while this is a union, the pointer tends to be NULL so save on
3925 mp = atomic_load_ptr(&vp->v_mountedhere);
3928 if (vp->v_type == VDIR)
3936 * The code was originally copy-pasted from regular lookup and despite
3937 * clean ups leaves performance on the table. Any modifications here
3938 * must take into account that in case off fallback the resulting
3939 * nameidata state has to be compatible with the original.
3942 cache_fplookup_parse(struct cache_fpl *fpl)
3944 struct nameidata *ndp;
3945 struct componentname *cnp;
3952 * Search a new directory.
3954 * The last component of the filename is left accessible via
3955 * cnp->cn_nameptr for callers that need the name. Callers needing
3956 * the name set the SAVENAME flag. When done, they assume
3957 * responsibility for freeing the pathname buffer.
3959 for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
3961 cnp->cn_namelen = cp - cnp->cn_nameptr;
3962 if (__predict_false(cnp->cn_namelen > NAME_MAX)) {
3963 cache_fpl_smr_exit(fpl);
3964 return (cache_fpl_handled(fpl, ENAMETOOLONG));
3966 ndp->ni_pathlen -= cnp->cn_namelen;
3967 KASSERT(ndp->ni_pathlen <= PATH_MAX,
3968 ("%s: ni_pathlen underflow to %zd\n", __func__, ndp->ni_pathlen));
3972 * Replace multiple slashes by a single slash and trailing slashes
3973 * by a null. This must be done before VOP_LOOKUP() because some
3974 * fs's don't know about trailing slashes. Remember if there were
3975 * trailing slashes to handle symlinks, existing non-directories
3976 * and non-existing files that won't be directories specially later.
3978 while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
3984 * Regular lookup performs the following:
3985 * *ndp->ni_next = '\0';
3986 * cnp->cn_flags |= TRAILINGSLASH;
3988 * Which is problematic since it modifies data read
3989 * from userspace. Then if fast path lookup was to
3990 * abort we would have to either restore it or convey
3991 * the flag. Since this is a corner case just ignore
3992 * it for simplicity.
3994 return (cache_fpl_partial(fpl));
4000 * Check for degenerate name (e.g. / or "")
4001 * which is a way of talking about a directory,
4002 * e.g. like "/." or ".".
4005 * Another corner case handled by the regular lookup
4007 if (__predict_false(cnp->cn_nameptr[0] == '\0')) {
4008 return (cache_fpl_partial(fpl));
4014 cache_fplookup_parse_advance(struct cache_fpl *fpl)
4016 struct nameidata *ndp;
4017 struct componentname *cnp;
4022 cnp->cn_nameptr = ndp->ni_next;
4023 while (*cnp->cn_nameptr == '/') {
4030 * See the API contract for VOP_FPLOOKUP_VEXEC.
4032 static int __noinline
4033 cache_fplookup_failed_vexec(struct cache_fpl *fpl, int error)
4035 struct componentname *cnp;
4041 dvp_seqc = fpl->dvp_seqc;
4044 * Hack: they may be looking up foo/bar, where foo is a
4045 * regular file. In such a case we need to turn ENOTDIR,
4046 * but we may happen to get here with a different error.
4048 if (dvp->v_type != VDIR) {
4050 * The check here is predominantly to catch
4051 * EOPNOTSUPP from dead_vnodeops. If the vnode
4052 * gets doomed past this point it is going to
4053 * fail seqc verification.
4055 if (VN_IS_DOOMED(dvp)) {
4056 return (cache_fpl_aborted(fpl));
4062 * Hack: handle O_SEARCH.
4064 * Open Group Base Specifications Issue 7, 2018 edition states:
4065 * If the access mode of the open file description associated with the
4066 * file descriptor is not O_SEARCH, the function shall check whether
4067 * directory searches are permitted using the current permissions of
4068 * the directory underlying the file descriptor. If the access mode is
4069 * O_SEARCH, the function shall not perform the check.
4071 * Regular lookup tests for the NOEXECCHECK flag for every path
4072 * component to decide whether to do the permission check. However,
4073 * since most lookups never have the flag (and when they do it is only
4074 * present for the first path component), lockless lookup only acts on
4075 * it if there is a permission problem. Here the flag is represented
4076 * with a boolean so that we don't have to clear it on the way out.
4078 * For simplicity this always aborts.
4079 * TODO: check if this is the first lookup and ignore the permission
4080 * problem. Note the flag has to survive fallback (if it happens to be
4084 return (cache_fpl_aborted(fpl));
4089 if (!vn_seqc_consistent(dvp, dvp_seqc)) {
4090 error = cache_fpl_aborted(fpl);
4092 cache_fpl_partial(fpl);
4096 if (!vn_seqc_consistent(dvp, dvp_seqc)) {
4097 error = cache_fpl_aborted(fpl);
4099 cache_fpl_smr_exit(fpl);
4100 cache_fpl_handled(fpl, error);
4108 cache_fplookup_impl(struct vnode *dvp, struct cache_fpl *fpl)
4110 struct nameidata *ndp;
4111 struct componentname *cnp;
4115 error = CACHE_FPL_FAILED;
4119 cache_fpl_checkpoint(fpl, &fpl->snd);
4122 fpl->dvp_seqc = vn_seqc_read_any(fpl->dvp);
4123 if (seqc_in_modify(fpl->dvp_seqc)) {
4124 cache_fpl_aborted(fpl);
4127 mp = atomic_load_ptr(&fpl->dvp->v_mount);
4128 if (!cache_fplookup_mp_supported(mp)) {
4129 cache_fpl_aborted(fpl);
4133 VNPASS(cache_fplookup_vnode_supported(fpl->dvp), fpl->dvp);
4136 error = cache_fplookup_parse(fpl);
4137 if (__predict_false(error != 0)) {
4141 VNPASS(cache_fplookup_vnode_supported(fpl->dvp), fpl->dvp);
4143 error = VOP_FPLOOKUP_VEXEC(fpl->dvp, cnp->cn_cred);
4144 if (__predict_false(error != 0)) {
4145 error = cache_fplookup_failed_vexec(fpl, error);
4149 if (__predict_false(cache_fpl_isdotdot(cnp))) {
4150 error = cache_fplookup_dotdot(fpl);
4151 if (__predict_false(error != 0)) {
4155 error = cache_fplookup_next(fpl);
4156 if (__predict_false(error != 0)) {
4160 VNPASS(!seqc_in_modify(fpl->tvp_seqc), fpl->tvp);
4162 if (cache_fplookup_need_climb_mount(fpl)) {
4163 error = cache_fplookup_climb_mount(fpl);
4164 if (__predict_false(error != 0)) {
4170 VNPASS(!seqc_in_modify(fpl->tvp_seqc), fpl->tvp);
4172 if (cache_fpl_islastcn(ndp)) {
4173 error = cache_fplookup_final(fpl);
4177 if (!vn_seqc_consistent(fpl->dvp, fpl->dvp_seqc)) {
4178 error = cache_fpl_aborted(fpl);
4182 fpl->dvp = fpl->tvp;
4183 fpl->dvp_seqc = fpl->tvp_seqc;
4185 cache_fplookup_parse_advance(fpl);
4186 cache_fpl_checkpoint(fpl, &fpl->snd);
4189 switch (fpl->status) {
4190 case CACHE_FPL_STATUS_UNSET:
4191 __assert_unreachable();
4193 case CACHE_FPL_STATUS_PARTIAL:
4194 cache_fpl_smr_assert_entered(fpl);
4195 return (cache_fplookup_partial_setup(fpl));
4196 case CACHE_FPL_STATUS_ABORTED:
4198 cache_fpl_smr_exit(fpl);
4199 return (CACHE_FPL_FAILED);
4200 case CACHE_FPL_STATUS_HANDLED:
4201 MPASS(error != CACHE_FPL_FAILED);
4202 cache_fpl_smr_assert_not_entered(fpl);
4203 if (__predict_false(error != 0)) {
4206 cache_fpl_cleanup_cnp(cnp);
4209 ndp->ni_dvp = fpl->dvp;
4210 ndp->ni_vp = fpl->tvp;
4211 if (cnp->cn_flags & SAVENAME)
4212 cnp->cn_flags |= HASBUF;
4214 cache_fpl_cleanup_cnp(cnp);
4220 * Fast path lookup protected with SMR and sequence counters.
4222 * Note: all VOP_FPLOOKUP_VEXEC routines have a comment referencing this one.
4224 * Filesystems can opt in by setting the MNTK_FPLOOKUP flag and meeting criteria
4227 * Traditional vnode lookup conceptually looks like this:
4233 * vn_unlock(current);
4240 * Each jump to the next vnode is safe memory-wise and atomic with respect to
4241 * any modifications thanks to holding respective locks.
4243 * The same guarantee can be provided with a combination of safe memory
4244 * reclamation and sequence counters instead. If all operations which affect
4245 * the relationship between the current vnode and the one we are looking for
4246 * also modify the counter, we can verify whether all the conditions held as
4247 * we made the jump. This includes things like permissions, mount points etc.
4248 * Counter modification is provided by enclosing relevant places in
4249 * vn_seqc_write_begin()/end() calls.
4251 * Thus this translates to:
4254 * dvp_seqc = seqc_read_any(dvp);
4255 * if (seqc_in_modify(dvp_seqc)) // someone is altering the vnode
4259 * tvp_seqc = seqc_read_any(tvp);
4260 * if (seqc_in_modify(tvp_seqc)) // someone is altering the target vnode
4262 * if (!seqc_consistent(dvp, dvp_seqc) // someone is altering the vnode
4264 * dvp = tvp; // we know nothing of importance has changed
4265 * dvp_seqc = tvp_seqc; // store the counter for the tvp iteration
4269 * vget(); // secure the vnode
4270 * if (!seqc_consistent(tvp, tvp_seqc) // final check
4272 * // at this point we know nothing has changed for any parent<->child pair
4273 * // as they were crossed during the lookup, meaning we matched the guarantee
4274 * // of the locked variant
4277 * The API contract for VOP_FPLOOKUP_VEXEC routines is as follows:
4278 * - they are called while within vfs_smr protection which they must never exit
4279 * - EAGAIN can be returned to denote checking could not be performed, it is
4280 * always valid to return it
4281 * - if the sequence counter has not changed the result must be valid
4282 * - if the sequence counter has changed both false positives and false negatives
4283 * are permitted (since the result will be rejected later)
4284 * - for simple cases of unix permission checks vaccess_vexec_smr can be used
4286 * Caveats to watch out for:
4287 * - vnodes are passed unlocked and unreferenced with nothing stopping
4288 * VOP_RECLAIM, in turn meaning that ->v_data can become NULL. It is advised
4289 * to use atomic_load_ptr to fetch it.
4290 * - the aforementioned object can also get freed, meaning absent other means it
4291 * should be protected with vfs_smr
4292 * - either safely checking permissions as they are modified or guaranteeing
4293 * their stability is left to the routine
4296 cache_fplookup(struct nameidata *ndp, enum cache_fpl_status *status,
4299 struct cache_fpl fpl;
4302 struct componentname *cnp;
4303 struct nameidata_saved orig;
4306 MPASS(ndp->ni_lcf == 0);
4308 fpl.status = CACHE_FPL_STATUS_UNSET;
4310 fpl.cnp = &ndp->ni_cnd;
4311 MPASS(curthread == fpl.cnp->cn_thread);
4313 if ((fpl.cnp->cn_flags & SAVESTART) != 0)
4314 MPASS(fpl.cnp->cn_nameiop != LOOKUP);
4316 if (!cache_can_fplookup(&fpl)) {
4317 SDT_PROBE3(vfs, fplookup, lookup, done, ndp, fpl.line, fpl.status);
4318 *status = fpl.status;
4319 return (EOPNOTSUPP);
4322 cache_fpl_checkpoint(&fpl, &orig);
4324 cache_fpl_smr_enter_initial(&fpl);
4325 fpl.fsearch = false;
4326 pwd = pwd_get_smr();
4328 ndp->ni_rootdir = pwd->pwd_rdir;
4329 ndp->ni_topdir = pwd->pwd_jdir;
4332 cnp->cn_nameptr = cnp->cn_pnbuf;
4333 if (cnp->cn_pnbuf[0] == '/') {
4334 cache_fpl_handle_root(ndp, &dvp);
4336 if (ndp->ni_dirfd == AT_FDCWD) {
4337 dvp = pwd->pwd_cdir;
4339 error = cache_fplookup_dirfd(&fpl, &dvp);
4340 if (__predict_false(error != 0)) {
4346 SDT_PROBE4(vfs, namei, lookup, entry, dvp, cnp->cn_pnbuf, cnp->cn_flags, true);
4348 error = cache_fplookup_impl(dvp, &fpl);
4350 cache_fpl_smr_assert_not_entered(&fpl);
4351 SDT_PROBE3(vfs, fplookup, lookup, done, ndp, fpl.line, fpl.status);
4353 *status = fpl.status;
4354 switch (fpl.status) {
4355 case CACHE_FPL_STATUS_UNSET:
4356 __assert_unreachable();
4358 case CACHE_FPL_STATUS_HANDLED:
4359 SDT_PROBE3(vfs, namei, lookup, return, error,
4360 (error == 0 ? ndp->ni_vp : NULL), true);
4362 case CACHE_FPL_STATUS_PARTIAL:
4365 * Status restored by cache_fplookup_partial_setup.
4368 case CACHE_FPL_STATUS_ABORTED:
4369 cache_fpl_restore(&fpl, &orig);