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>
54 #include <sys/mount.h>
55 #include <sys/namei.h>
57 #include <sys/rwlock.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_DEFINE1(vfs, namecache, fullpath, entry, "struct vnode *");
88 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, hit, "struct vnode *",
89 "char *", "struct vnode *");
90 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, miss, "struct vnode *");
91 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, return, "int",
92 "struct vnode *", "char *");
93 SDT_PROBE_DEFINE3(vfs, namecache, lookup, hit, "struct vnode *", "char *",
95 SDT_PROBE_DEFINE2(vfs, namecache, lookup, hit__negative,
96 "struct vnode *", "char *");
97 SDT_PROBE_DEFINE2(vfs, namecache, lookup, miss, "struct vnode *",
99 SDT_PROBE_DEFINE1(vfs, namecache, purge, done, "struct vnode *");
100 SDT_PROBE_DEFINE1(vfs, namecache, purge_negative, done, "struct vnode *");
101 SDT_PROBE_DEFINE1(vfs, namecache, purgevfs, done, "struct mount *");
102 SDT_PROBE_DEFINE3(vfs, namecache, zap, done, "struct vnode *", "char *",
104 SDT_PROBE_DEFINE2(vfs, namecache, zap_negative, done, "struct vnode *",
106 SDT_PROBE_DEFINE2(vfs, namecache, shrink_negative, done, "struct vnode *",
109 SDT_PROBE_DEFINE3(vfs, fplookup, lookup, done, "struct nameidata", "int", "bool");
110 SDT_PROBE_DECLARE(vfs, namei, lookup, entry);
111 SDT_PROBE_DECLARE(vfs, namei, lookup, return);
114 * This structure describes the elements in the cache of recent
115 * names looked up by namei.
120 _Static_assert(sizeof(struct negstate) <= sizeof(struct vnode *),
121 "the state must fit in a union with a pointer without growing it");
124 CK_LIST_ENTRY(namecache) nc_hash;/* hash chain */
125 LIST_ENTRY(namecache) nc_src; /* source vnode list */
126 TAILQ_ENTRY(namecache) nc_dst; /* destination vnode list */
127 struct vnode *nc_dvp; /* vnode of parent of name */
129 struct vnode *nu_vp; /* vnode the name refers to */
130 struct negstate nu_neg;/* negative entry state */
132 u_char nc_flag; /* flag bits */
133 u_char nc_nlen; /* length of name */
134 char nc_name[0]; /* segment name + nul */
138 * struct namecache_ts repeats struct namecache layout up to the
140 * struct namecache_ts is used in place of struct namecache when time(s) need
141 * to be stored. The nc_dotdottime field is used when a cache entry is mapping
142 * both a non-dotdot directory name plus dotdot for the directory's
145 struct namecache_ts {
146 struct timespec nc_time; /* timespec provided by fs */
147 struct timespec nc_dotdottime; /* dotdot timespec provided by fs */
148 int nc_ticks; /* ticks value when entry was added */
149 struct namecache nc_nc;
152 #define nc_vp n_un.nu_vp
153 #define nc_neg n_un.nu_neg
156 * Flags in namecache.nc_flag
158 #define NCF_WHITE 0x01
159 #define NCF_ISDOTDOT 0x02
162 #define NCF_DVDROP 0x10
163 #define NCF_NEGATIVE 0x20
164 #define NCF_INVALID 0x40
167 * Flags in negstate.neg_flag
172 * Mark an entry as invalid.
174 * This is called before it starts getting deconstructed.
177 cache_ncp_invalidate(struct namecache *ncp)
180 KASSERT((ncp->nc_flag & NCF_INVALID) == 0,
181 ("%s: entry %p already invalid", __func__, ncp));
182 ncp->nc_flag |= NCF_INVALID;
183 atomic_thread_fence_rel();
187 * Verify validity of an entry.
189 * All places which elide locks are supposed to call this after they are
190 * done with reading from an entry.
193 cache_ncp_invalid(struct namecache *ncp)
196 atomic_thread_fence_acq();
197 return ((ncp->nc_flag & NCF_INVALID) != 0);
201 * Name caching works as follows:
203 * Names found by directory scans are retained in a cache
204 * for future reference. It is managed LRU, so frequently
205 * used names will hang around. Cache is indexed by hash value
206 * obtained from (dvp, name) where dvp refers to the directory
209 * If it is a "negative" entry, (i.e. for a name that is known NOT to
210 * exist) the vnode pointer will be NULL.
212 * Upon reaching the last segment of a path, if the reference
213 * is for DELETE, or NOCACHE is set (rewrite), and the
214 * name is located in the cache, it will be dropped.
216 * These locks are used (in the order in which they can be taken):
218 * vnodelock mtx vnode lists and v_cache_dd field protection
219 * bucketlock rwlock for access to given set of hash buckets
220 * neglist mtx negative entry LRU management
222 * Additionally, ncneg_shrink_lock mtx is used to have at most one thread
223 * shrinking the LRU list.
225 * It is legal to take multiple vnodelock and bucketlock locks. The locking
226 * order is lower address first. Both are recursive.
228 * "." lookups are lockless.
230 * ".." and vnode -> name lookups require vnodelock.
232 * name -> vnode lookup requires the relevant bucketlock to be held for reading.
234 * Insertions and removals of entries require involved vnodes and bucketlocks
235 * to be write-locked to prevent other threads from seeing the entry.
237 * Some lookups result in removal of the found entry (e.g. getting rid of a
238 * negative entry with the intent to create a positive one), which poses a
239 * problem when multiple threads reach the state. Similarly, two different
240 * threads can purge two different vnodes and try to remove the same name.
242 * If the already held vnode lock is lower than the second required lock, we
243 * can just take the other lock. However, in the opposite case, this could
244 * deadlock. As such, this is resolved by trylocking and if that fails unlocking
245 * the first node, locking everything in order and revalidating the state.
251 * Structures associated with name caching.
253 #define NCHHASH(hash) \
254 (&nchashtbl[(hash) & nchash])
255 static __read_mostly CK_LIST_HEAD(nchashhead, namecache) *nchashtbl;/* Hash Table */
256 static u_long __read_mostly nchash; /* size of hash table */
257 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0,
258 "Size of namecache hash table");
259 static u_long __read_mostly ncnegfactor = 5; /* ratio of negative entries */
260 SYSCTL_ULONG(_vfs, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0,
261 "Ratio of negative namecache entries");
262 static u_long __exclusive_cache_line numneg; /* number of negative entries allocated */
263 static u_long __exclusive_cache_line numcache;/* number of cache entries allocated */
264 u_int ncsizefactor = 2;
265 SYSCTL_UINT(_vfs, OID_AUTO, ncsizefactor, CTLFLAG_RW, &ncsizefactor, 0,
266 "Size factor for namecache");
267 static u_int __read_mostly ncpurgeminvnodes;
268 SYSCTL_UINT(_vfs, OID_AUTO, ncpurgeminvnodes, CTLFLAG_RW, &ncpurgeminvnodes, 0,
269 "Number of vnodes below which purgevfs ignores the request");
270 static u_int __read_mostly ncsize; /* the size as computed on creation or resizing */
272 struct nchstats nchstats; /* cache effectiveness statistics */
274 static struct mtx __exclusive_cache_line ncneg_shrink_lock;
278 TAILQ_HEAD(, namecache) nl_list;
279 } __aligned(CACHE_LINE_SIZE);
281 static struct neglist __read_mostly *neglists;
282 static struct neglist ncneg_hot;
283 static u_long numhotneg;
285 #define numneglists (ncneghash + 1)
286 static u_int __read_mostly ncneghash;
287 static inline struct neglist *
288 NCP2NEGLIST(struct namecache *ncp)
291 return (&neglists[(((uintptr_t)(ncp) >> 8) & ncneghash)]);
294 static inline struct negstate *
295 NCP2NEGSTATE(struct namecache *ncp)
298 MPASS(ncp->nc_flag & NCF_NEGATIVE);
299 return (&ncp->nc_neg);
302 #define numbucketlocks (ncbuckethash + 1)
303 static u_int __read_mostly ncbuckethash;
304 static struct rwlock_padalign __read_mostly *bucketlocks;
305 #define HASH2BUCKETLOCK(hash) \
306 ((struct rwlock *)(&bucketlocks[((hash) & ncbuckethash)]))
308 #define numvnodelocks (ncvnodehash + 1)
309 static u_int __read_mostly ncvnodehash;
310 static struct mtx __read_mostly *vnodelocks;
311 static inline struct mtx *
312 VP2VNODELOCK(struct vnode *vp)
315 return (&vnodelocks[(((uintptr_t)(vp) >> 8) & ncvnodehash)]);
319 * UMA zones for the VFS cache.
321 * The small cache is used for entries with short names, which are the
322 * most common. The large cache is used for entries which are too big to
323 * fit in the small cache.
325 static uma_zone_t __read_mostly cache_zone_small;
326 static uma_zone_t __read_mostly cache_zone_small_ts;
327 static uma_zone_t __read_mostly cache_zone_large;
328 static uma_zone_t __read_mostly cache_zone_large_ts;
330 #define CACHE_PATH_CUTOFF 35
332 static struct namecache *
333 cache_alloc(int len, int ts)
335 struct namecache_ts *ncp_ts;
336 struct namecache *ncp;
338 if (__predict_false(ts)) {
339 if (len <= CACHE_PATH_CUTOFF)
340 ncp_ts = uma_zalloc_smr(cache_zone_small_ts, M_WAITOK);
342 ncp_ts = uma_zalloc_smr(cache_zone_large_ts, M_WAITOK);
343 ncp = &ncp_ts->nc_nc;
345 if (len <= CACHE_PATH_CUTOFF)
346 ncp = uma_zalloc_smr(cache_zone_small, M_WAITOK);
348 ncp = uma_zalloc_smr(cache_zone_large, M_WAITOK);
354 cache_free(struct namecache *ncp)
356 struct namecache_ts *ncp_ts;
360 if ((ncp->nc_flag & NCF_DVDROP) != 0)
362 if (__predict_false(ncp->nc_flag & NCF_TS)) {
363 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
364 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
365 uma_zfree_smr(cache_zone_small_ts, ncp_ts);
367 uma_zfree_smr(cache_zone_large_ts, ncp_ts);
369 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
370 uma_zfree_smr(cache_zone_small, ncp);
372 uma_zfree_smr(cache_zone_large, ncp);
377 cache_out_ts(struct namecache *ncp, struct timespec *tsp, int *ticksp)
379 struct namecache_ts *ncp_ts;
381 KASSERT((ncp->nc_flag & NCF_TS) != 0 ||
382 (tsp == NULL && ticksp == NULL),
385 if (tsp == NULL && ticksp == NULL)
388 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
390 *tsp = ncp_ts->nc_time;
392 *ticksp = ncp_ts->nc_ticks;
396 static int __read_mostly doingcache = 1; /* 1 => enable the cache */
397 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0,
398 "VFS namecache enabled");
401 /* Export size information to userland */
402 SYSCTL_INT(_debug_sizeof, OID_AUTO, namecache, CTLFLAG_RD, SYSCTL_NULL_INT_PTR,
403 sizeof(struct namecache), "sizeof(struct namecache)");
406 * The new name cache statistics
408 static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
409 "Name cache statistics");
410 #define STATNODE_ULONG(name, descr) \
411 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, descr);
412 #define STATNODE_COUNTER(name, descr) \
413 static COUNTER_U64_DEFINE_EARLY(name); \
414 SYSCTL_COUNTER_U64(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, \
416 STATNODE_ULONG(numneg, "Number of negative cache entries");
417 STATNODE_ULONG(numcache, "Number of cache entries");
418 STATNODE_COUNTER(numcachehv, "Number of namecache entries with vnodes held");
419 STATNODE_COUNTER(numdrops, "Number of dropped entries due to reaching the limit");
420 STATNODE_COUNTER(dothits, "Number of '.' hits");
421 STATNODE_COUNTER(dotdothits, "Number of '..' hits");
422 STATNODE_COUNTER(numchecks, "Number of checks in lookup");
423 STATNODE_COUNTER(nummiss, "Number of cache misses");
424 STATNODE_COUNTER(nummisszap, "Number of cache misses we do not want to cache");
425 STATNODE_COUNTER(numposzaps,
426 "Number of cache hits (positive) we do not want to cache");
427 STATNODE_COUNTER(numposhits, "Number of cache hits (positive)");
428 STATNODE_COUNTER(numnegzaps,
429 "Number of cache hits (negative) we do not want to cache");
430 STATNODE_COUNTER(numneghits, "Number of cache hits (negative)");
431 /* These count for vn_getcwd(), too. */
432 STATNODE_COUNTER(numfullpathcalls, "Number of fullpath search calls");
433 STATNODE_COUNTER(numfullpathfail1, "Number of fullpath search errors (ENOTDIR)");
434 STATNODE_COUNTER(numfullpathfail2,
435 "Number of fullpath search errors (VOP_VPTOCNP failures)");
436 STATNODE_COUNTER(numfullpathfail4, "Number of fullpath search errors (ENOMEM)");
437 STATNODE_COUNTER(numfullpathfound, "Number of successful fullpath calls");
438 STATNODE_COUNTER(zap_and_exit_bucket_relock_success,
439 "Number of successful removals after relocking");
440 static long zap_and_exit_bucket_fail; STATNODE_ULONG(zap_and_exit_bucket_fail,
441 "Number of times zap_and_exit failed to lock");
442 static long zap_and_exit_bucket_fail2; STATNODE_ULONG(zap_and_exit_bucket_fail2,
443 "Number of times zap_and_exit failed to lock");
444 static long cache_lock_vnodes_cel_3_failures;
445 STATNODE_ULONG(cache_lock_vnodes_cel_3_failures,
446 "Number of times 3-way vnode locking failed");
447 STATNODE_ULONG(numhotneg, "Number of hot negative entries");
448 STATNODE_COUNTER(numneg_evicted,
449 "Number of negative entries evicted when adding a new entry");
450 STATNODE_COUNTER(shrinking_skipped,
451 "Number of times shrinking was already in progress");
453 static void cache_zap_locked(struct namecache *ncp);
454 static int vn_fullpath_hardlink(struct thread *td, struct nameidata *ndp, char **retbuf,
455 char **freebuf, size_t *buflen);
456 static int vn_fullpath_any(struct thread *td, struct vnode *vp, struct vnode *rdir,
457 char *buf, char **retbuf, size_t *buflen);
458 static int vn_fullpath_dir(struct thread *td, struct vnode *vp, struct vnode *rdir,
459 char *buf, char **retbuf, size_t *len, bool slash_prefixed, size_t addend);
461 static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
463 static int cache_yield;
464 SYSCTL_INT(_vfs_cache, OID_AUTO, yield, CTLFLAG_RD, &cache_yield, 0,
465 "Number of times cache called yield");
467 static void __noinline
468 cache_maybe_yield(void)
471 if (should_yield()) {
473 kern_yield(PRI_USER);
478 cache_assert_vlp_locked(struct mtx *vlp)
482 mtx_assert(vlp, MA_OWNED);
486 cache_assert_vnode_locked(struct vnode *vp)
490 vlp = VP2VNODELOCK(vp);
491 cache_assert_vlp_locked(vlp);
495 cache_get_hash(char *name, u_char len, struct vnode *dvp)
499 hash = fnv_32_buf(name, len, FNV1_32_INIT);
500 hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
504 static inline struct rwlock *
505 NCP2BUCKETLOCK(struct namecache *ncp)
509 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen, ncp->nc_dvp);
510 return (HASH2BUCKETLOCK(hash));
515 cache_assert_bucket_locked(struct namecache *ncp, int mode)
519 blp = NCP2BUCKETLOCK(ncp);
520 rw_assert(blp, mode);
523 #define cache_assert_bucket_locked(x, y) do { } while (0)
526 #define cache_sort_vnodes(x, y) _cache_sort_vnodes((void **)(x), (void **)(y))
528 _cache_sort_vnodes(void **p1, void **p2)
532 MPASS(*p1 != NULL || *p2 != NULL);
542 cache_lock_all_buckets(void)
546 for (i = 0; i < numbucketlocks; i++)
547 rw_wlock(&bucketlocks[i]);
551 cache_unlock_all_buckets(void)
555 for (i = 0; i < numbucketlocks; i++)
556 rw_wunlock(&bucketlocks[i]);
560 cache_lock_all_vnodes(void)
564 for (i = 0; i < numvnodelocks; i++)
565 mtx_lock(&vnodelocks[i]);
569 cache_unlock_all_vnodes(void)
573 for (i = 0; i < numvnodelocks; i++)
574 mtx_unlock(&vnodelocks[i]);
578 cache_trylock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
581 cache_sort_vnodes(&vlp1, &vlp2);
584 if (!mtx_trylock(vlp1))
587 if (!mtx_trylock(vlp2)) {
597 cache_lock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
600 MPASS(vlp1 != NULL || vlp2 != NULL);
610 cache_unlock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
613 MPASS(vlp1 != NULL || vlp2 != NULL);
622 sysctl_nchstats(SYSCTL_HANDLER_ARGS)
624 struct nchstats snap;
626 if (req->oldptr == NULL)
627 return (SYSCTL_OUT(req, 0, sizeof(snap)));
630 snap.ncs_goodhits = counter_u64_fetch(numposhits);
631 snap.ncs_neghits = counter_u64_fetch(numneghits);
632 snap.ncs_badhits = counter_u64_fetch(numposzaps) +
633 counter_u64_fetch(numnegzaps);
634 snap.ncs_miss = counter_u64_fetch(nummisszap) +
635 counter_u64_fetch(nummiss);
637 return (SYSCTL_OUT(req, &snap, sizeof(snap)));
639 SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE | CTLFLAG_RD |
640 CTLFLAG_MPSAFE, 0, 0, sysctl_nchstats, "LU",
641 "VFS cache effectiveness statistics");
645 * Grab an atomic snapshot of the name cache hash chain lengths
647 static SYSCTL_NODE(_debug, OID_AUTO, hashstat,
648 CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
652 sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS)
654 struct nchashhead *ncpp;
655 struct namecache *ncp;
656 int i, error, n_nchash, *cntbuf;
659 n_nchash = nchash + 1; /* nchash is max index, not count */
660 if (req->oldptr == NULL)
661 return SYSCTL_OUT(req, 0, n_nchash * sizeof(int));
662 cntbuf = malloc(n_nchash * sizeof(int), M_TEMP, M_ZERO | M_WAITOK);
663 cache_lock_all_buckets();
664 if (n_nchash != nchash + 1) {
665 cache_unlock_all_buckets();
666 free(cntbuf, M_TEMP);
669 /* Scan hash tables counting entries */
670 for (ncpp = nchashtbl, i = 0; i < n_nchash; ncpp++, i++)
671 CK_LIST_FOREACH(ncp, ncpp, nc_hash)
673 cache_unlock_all_buckets();
674 for (error = 0, i = 0; i < n_nchash; i++)
675 if ((error = SYSCTL_OUT(req, &cntbuf[i], sizeof(int))) != 0)
677 free(cntbuf, M_TEMP);
680 SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD|
681 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_rawnchash, "S,int",
682 "nchash chain lengths");
685 sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS)
688 struct nchashhead *ncpp;
689 struct namecache *ncp;
691 int count, maxlength, used, pct;
694 return SYSCTL_OUT(req, 0, 4 * sizeof(int));
696 cache_lock_all_buckets();
697 n_nchash = nchash + 1; /* nchash is max index, not count */
701 /* Scan hash tables for applicable entries */
702 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
704 CK_LIST_FOREACH(ncp, ncpp, nc_hash) {
709 if (maxlength < count)
712 n_nchash = nchash + 1;
713 cache_unlock_all_buckets();
714 pct = (used * 100) / (n_nchash / 100);
715 error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash));
718 error = SYSCTL_OUT(req, &used, sizeof(used));
721 error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength));
724 error = SYSCTL_OUT(req, &pct, sizeof(pct));
729 SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD|
730 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_nchash, "I",
731 "nchash statistics (number of total/used buckets, maximum chain length, usage percentage)");
735 * Negative entries management
737 * A variation of LRU scheme is used. New entries are hashed into one of
738 * numneglists cold lists. Entries get promoted to the hot list on first hit.
740 * The shrinker will demote hot list head and evict from the cold list in a
741 * round-robin manner.
744 cache_negative_init(struct namecache *ncp)
746 struct negstate *negstate;
748 ncp->nc_flag |= NCF_NEGATIVE;
749 negstate = NCP2NEGSTATE(ncp);
750 negstate->neg_flag = 0;
754 cache_negative_hit(struct namecache *ncp)
756 struct neglist *neglist;
757 struct negstate *negstate;
759 negstate = NCP2NEGSTATE(ncp);
760 if ((negstate->neg_flag & NEG_HOT) != 0)
762 neglist = NCP2NEGLIST(ncp);
763 mtx_lock(&ncneg_hot.nl_lock);
764 mtx_lock(&neglist->nl_lock);
765 if ((negstate->neg_flag & NEG_HOT) == 0) {
767 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
768 TAILQ_INSERT_TAIL(&ncneg_hot.nl_list, ncp, nc_dst);
769 negstate->neg_flag |= NEG_HOT;
771 mtx_unlock(&neglist->nl_lock);
772 mtx_unlock(&ncneg_hot.nl_lock);
776 cache_negative_insert(struct namecache *ncp)
778 struct neglist *neglist;
780 MPASS(ncp->nc_flag & NCF_NEGATIVE);
781 cache_assert_bucket_locked(ncp, RA_WLOCKED);
782 neglist = NCP2NEGLIST(ncp);
783 mtx_lock(&neglist->nl_lock);
784 TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst);
785 mtx_unlock(&neglist->nl_lock);
786 atomic_add_rel_long(&numneg, 1);
790 cache_negative_remove(struct namecache *ncp)
792 struct neglist *neglist;
793 struct negstate *negstate;
794 bool hot_locked = false;
795 bool list_locked = false;
797 cache_assert_bucket_locked(ncp, RA_WLOCKED);
798 neglist = NCP2NEGLIST(ncp);
799 negstate = NCP2NEGSTATE(ncp);
800 if ((negstate->neg_flag & NEG_HOT) != 0) {
802 mtx_lock(&ncneg_hot.nl_lock);
803 if ((negstate->neg_flag & NEG_HOT) == 0) {
805 mtx_lock(&neglist->nl_lock);
809 mtx_lock(&neglist->nl_lock);
811 if ((negstate->neg_flag & NEG_HOT) != 0) {
812 mtx_assert(&ncneg_hot.nl_lock, MA_OWNED);
813 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
816 mtx_assert(&neglist->nl_lock, MA_OWNED);
817 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
820 mtx_unlock(&neglist->nl_lock);
822 mtx_unlock(&ncneg_hot.nl_lock);
823 atomic_subtract_rel_long(&numneg, 1);
827 cache_negative_shrink_select(struct namecache **ncpp,
828 struct neglist **neglistpp)
830 struct neglist *neglist;
831 struct namecache *ncp;
837 for (i = 0; i < numneglists; i++) {
838 neglist = &neglists[(cycle + i) % numneglists];
839 if (TAILQ_FIRST(&neglist->nl_list) == NULL)
841 mtx_lock(&neglist->nl_lock);
842 ncp = TAILQ_FIRST(&neglist->nl_list);
845 mtx_unlock(&neglist->nl_lock);
848 *neglistpp = neglist;
854 cache_negative_zap_one(void)
856 struct namecache *ncp, *ncp2;
857 struct neglist *neglist;
858 struct negstate *negstate;
862 if (mtx_owner(&ncneg_shrink_lock) != NULL ||
863 !mtx_trylock(&ncneg_shrink_lock)) {
864 counter_u64_add(shrinking_skipped, 1);
868 mtx_lock(&ncneg_hot.nl_lock);
869 ncp = TAILQ_FIRST(&ncneg_hot.nl_list);
871 neglist = NCP2NEGLIST(ncp);
872 negstate = NCP2NEGSTATE(ncp);
873 mtx_lock(&neglist->nl_lock);
874 MPASS((negstate->neg_flag & NEG_HOT) != 0);
875 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
876 TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst);
877 negstate->neg_flag &= ~NEG_HOT;
879 mtx_unlock(&neglist->nl_lock);
881 mtx_unlock(&ncneg_hot.nl_lock);
883 cache_negative_shrink_select(&ncp, &neglist);
885 mtx_unlock(&ncneg_shrink_lock);
889 MPASS(ncp->nc_flag & NCF_NEGATIVE);
890 dvlp = VP2VNODELOCK(ncp->nc_dvp);
891 blp = NCP2BUCKETLOCK(ncp);
892 mtx_unlock(&neglist->nl_lock);
896 * Enter SMR to safely check the negative list.
897 * Even if the found pointer matches, the entry may now be reallocated
898 * and used by a different vnode.
901 ncp2 = TAILQ_FIRST(&neglist->nl_list);
902 if (ncp != ncp2 || dvlp != VP2VNODELOCK(ncp2->nc_dvp) ||
903 blp != NCP2BUCKETLOCK(ncp2)) {
908 SDT_PROBE2(vfs, namecache, shrink_negative, done, ncp->nc_dvp,
910 cache_zap_locked(ncp);
911 counter_u64_add(numneg_evicted, 1);
919 * cache_zap_locked():
921 * Removes a namecache entry from cache, whether it contains an actual
922 * pointer to a vnode or if it is just a negative cache entry.
925 cache_zap_locked(struct namecache *ncp)
928 if (!(ncp->nc_flag & NCF_NEGATIVE))
929 cache_assert_vnode_locked(ncp->nc_vp);
930 cache_assert_vnode_locked(ncp->nc_dvp);
931 cache_assert_bucket_locked(ncp, RA_WLOCKED);
933 CTR2(KTR_VFS, "cache_zap(%p) vp %p", ncp,
934 (ncp->nc_flag & NCF_NEGATIVE) ? NULL : ncp->nc_vp);
936 cache_ncp_invalidate(ncp);
938 CK_LIST_REMOVE(ncp, nc_hash);
939 if (!(ncp->nc_flag & NCF_NEGATIVE)) {
940 SDT_PROBE3(vfs, namecache, zap, done, ncp->nc_dvp,
941 ncp->nc_name, ncp->nc_vp);
942 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
943 if (ncp == ncp->nc_vp->v_cache_dd)
944 ncp->nc_vp->v_cache_dd = NULL;
946 SDT_PROBE2(vfs, namecache, zap_negative, done, ncp->nc_dvp,
948 cache_negative_remove(ncp);
950 if (ncp->nc_flag & NCF_ISDOTDOT) {
951 if (ncp == ncp->nc_dvp->v_cache_dd)
952 ncp->nc_dvp->v_cache_dd = NULL;
954 LIST_REMOVE(ncp, nc_src);
955 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) {
956 ncp->nc_flag |= NCF_DVDROP;
957 counter_u64_add(numcachehv, -1);
960 atomic_subtract_rel_long(&numcache, 1);
964 cache_zap_negative_locked_vnode_kl(struct namecache *ncp, struct vnode *vp)
968 MPASS(ncp->nc_dvp == vp);
969 MPASS(ncp->nc_flag & NCF_NEGATIVE);
970 cache_assert_vnode_locked(vp);
972 blp = NCP2BUCKETLOCK(ncp);
974 cache_zap_locked(ncp);
979 cache_zap_locked_vnode_kl2(struct namecache *ncp, struct vnode *vp,
982 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
985 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
986 cache_assert_vnode_locked(vp);
988 if (ncp->nc_flag & NCF_NEGATIVE) {
993 cache_zap_negative_locked_vnode_kl(ncp, vp);
997 pvlp = VP2VNODELOCK(vp);
998 blp = NCP2BUCKETLOCK(ncp);
999 vlp1 = VP2VNODELOCK(ncp->nc_dvp);
1000 vlp2 = VP2VNODELOCK(ncp->nc_vp);
1002 if (*vlpp == vlp1 || *vlpp == vlp2) {
1006 if (*vlpp != NULL) {
1010 cache_sort_vnodes(&vlp1, &vlp2);
1015 if (!mtx_trylock(vlp1))
1021 cache_zap_locked(ncp);
1023 if (to_unlock != NULL)
1024 mtx_unlock(to_unlock);
1031 MPASS(*vlpp == NULL);
1036 static int __noinline
1037 cache_zap_locked_vnode(struct namecache *ncp, struct vnode *vp)
1039 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
1043 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
1044 cache_assert_vnode_locked(vp);
1046 pvlp = VP2VNODELOCK(vp);
1047 if (ncp->nc_flag & NCF_NEGATIVE) {
1048 cache_zap_negative_locked_vnode_kl(ncp, vp);
1052 blp = NCP2BUCKETLOCK(ncp);
1053 vlp1 = VP2VNODELOCK(ncp->nc_dvp);
1054 vlp2 = VP2VNODELOCK(ncp->nc_vp);
1055 cache_sort_vnodes(&vlp1, &vlp2);
1060 if (!mtx_trylock(vlp1)) {
1067 cache_zap_locked(ncp);
1069 mtx_unlock(to_unlock);
1076 * If trylocking failed we can get here. We know enough to take all needed locks
1077 * in the right order and re-lookup the entry.
1080 cache_zap_unlocked_bucket(struct namecache *ncp, struct componentname *cnp,
1081 struct vnode *dvp, struct mtx *dvlp, struct mtx *vlp, uint32_t hash,
1084 struct namecache *rncp;
1086 cache_assert_bucket_locked(ncp, RA_UNLOCKED);
1088 cache_sort_vnodes(&dvlp, &vlp);
1089 cache_lock_vnodes(dvlp, vlp);
1091 CK_LIST_FOREACH(rncp, (NCHHASH(hash)), nc_hash) {
1092 if (rncp == ncp && rncp->nc_dvp == dvp &&
1093 rncp->nc_nlen == cnp->cn_namelen &&
1094 !bcmp(rncp->nc_name, cnp->cn_nameptr, rncp->nc_nlen))
1098 cache_zap_locked(rncp);
1100 cache_unlock_vnodes(dvlp, vlp);
1101 counter_u64_add(zap_and_exit_bucket_relock_success, 1);
1106 cache_unlock_vnodes(dvlp, vlp);
1110 static int __noinline
1111 cache_zap_wlocked_bucket(struct namecache *ncp, struct componentname *cnp,
1112 uint32_t hash, struct rwlock *blp)
1114 struct mtx *dvlp, *vlp;
1117 cache_assert_bucket_locked(ncp, RA_WLOCKED);
1119 dvlp = VP2VNODELOCK(ncp->nc_dvp);
1121 if (!(ncp->nc_flag & NCF_NEGATIVE))
1122 vlp = VP2VNODELOCK(ncp->nc_vp);
1123 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1124 cache_zap_locked(ncp);
1126 cache_unlock_vnodes(dvlp, vlp);
1132 return (cache_zap_unlocked_bucket(ncp, cnp, dvp, dvlp, vlp, hash, blp));
1135 static int __noinline
1136 cache_zap_rlocked_bucket(struct namecache *ncp, struct componentname *cnp,
1137 uint32_t hash, struct rwlock *blp)
1139 struct mtx *dvlp, *vlp;
1142 cache_assert_bucket_locked(ncp, RA_RLOCKED);
1144 dvlp = VP2VNODELOCK(ncp->nc_dvp);
1146 if (!(ncp->nc_flag & NCF_NEGATIVE))
1147 vlp = VP2VNODELOCK(ncp->nc_vp);
1148 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1151 cache_zap_locked(ncp);
1153 cache_unlock_vnodes(dvlp, vlp);
1159 return (cache_zap_unlocked_bucket(ncp, cnp, dvp, dvlp, vlp, hash, blp));
1163 cache_zap_wlocked_bucket_kl(struct namecache *ncp, struct rwlock *blp,
1164 struct mtx **vlpp1, struct mtx **vlpp2)
1166 struct mtx *dvlp, *vlp;
1168 cache_assert_bucket_locked(ncp, RA_WLOCKED);
1170 dvlp = VP2VNODELOCK(ncp->nc_dvp);
1172 if (!(ncp->nc_flag & NCF_NEGATIVE))
1173 vlp = VP2VNODELOCK(ncp->nc_vp);
1174 cache_sort_vnodes(&dvlp, &vlp);
1176 if (*vlpp1 == dvlp && *vlpp2 == vlp) {
1177 cache_zap_locked(ncp);
1178 cache_unlock_vnodes(dvlp, vlp);
1191 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1192 cache_zap_locked(ncp);
1193 cache_unlock_vnodes(dvlp, vlp);
1208 cache_lookup_unlock(struct rwlock *blp, struct mtx *vlp)
1218 static int __noinline
1219 cache_lookup_dot(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1220 struct timespec *tsp, int *ticksp)
1225 CTR2(KTR_VFS, "cache_lookup(%p, %s) found via .",
1226 dvp, cnp->cn_nameptr);
1227 counter_u64_add(dothits, 1);
1228 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", *vpp);
1235 * When we lookup "." we still can be asked to lock it
1238 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
1239 if (ltype != VOP_ISLOCKED(*vpp)) {
1240 if (ltype == LK_EXCLUSIVE) {
1241 vn_lock(*vpp, LK_UPGRADE | LK_RETRY);
1242 if (VN_IS_DOOMED((*vpp))) {
1243 /* forced unmount */
1249 vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY);
1254 static __noinline int
1255 cache_lookup_nomakeentry(struct vnode *dvp, struct vnode **vpp,
1256 struct componentname *cnp, struct timespec *tsp, int *ticksp)
1258 struct namecache *ncp;
1260 struct mtx *dvlp, *dvlp2;
1264 if (cnp->cn_namelen == 2 &&
1265 cnp->cn_nameptr[0] == '.' && cnp->cn_nameptr[1] == '.') {
1266 counter_u64_add(dotdothits, 1);
1267 dvlp = VP2VNODELOCK(dvp);
1271 ncp = dvp->v_cache_dd;
1273 SDT_PROBE3(vfs, namecache, lookup, miss, dvp,
1280 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1281 if (ncp->nc_dvp != dvp)
1282 panic("dvp %p v_cache_dd %p\n", dvp, ncp);
1283 if (!cache_zap_locked_vnode_kl2(ncp,
1286 MPASS(dvp->v_cache_dd == NULL);
1292 dvp->v_cache_dd = NULL;
1300 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1301 blp = HASH2BUCKETLOCK(hash);
1303 if (CK_LIST_EMPTY(NCHHASH(hash)))
1308 CK_LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1309 counter_u64_add(numchecks, 1);
1310 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1311 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1315 /* We failed to find an entry */
1321 error = cache_zap_wlocked_bucket(ncp, cnp, hash, blp);
1322 if (__predict_false(error != 0)) {
1323 zap_and_exit_bucket_fail++;
1324 cache_maybe_yield();
1327 counter_u64_add(numposzaps, 1);
1331 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr, NULL);
1332 counter_u64_add(nummisszap, 1);
1337 * Lookup a name in the name cache
1341 * - dvp: Parent directory in which to search.
1342 * - vpp: Return argument. Will contain desired vnode on cache hit.
1343 * - cnp: Parameters of the name search. The most interesting bits of
1344 * the cn_flags field have the following meanings:
1345 * - MAKEENTRY: If clear, free an entry from the cache rather than look
1347 * - ISDOTDOT: Must be set if and only if cn_nameptr == ".."
1348 * - tsp: Return storage for cache timestamp. On a successful (positive
1349 * or negative) lookup, tsp will be filled with any timespec that
1350 * was stored when this cache entry was created. However, it will
1351 * be clear for "." entries.
1352 * - ticks: Return storage for alternate cache timestamp. On a successful
1353 * (positive or negative) lookup, it will contain the ticks value
1354 * that was current when the cache entry was created, unless cnp
1359 * - -1: A positive cache hit. vpp will contain the desired vnode.
1360 * - ENOENT: A negative cache hit, or dvp was recycled out from under us due
1361 * to a forced unmount. vpp will not be modified. If the entry
1362 * is a whiteout, then the ISWHITEOUT flag will be set in
1364 * - 0: A cache miss. vpp will not be modified.
1368 * On a cache hit, vpp will be returned locked and ref'd. If we're looking up
1369 * .., dvp is unlocked. If we're looking up . an extra ref is taken, but the
1370 * lock is not recursively acquired.
1373 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1374 struct timespec *tsp, int *ticksp)
1376 struct namecache_ts *ncp_ts;
1377 struct namecache *ncp;
1378 struct negstate *negstate;
1384 bool try_smr, doing_smr, whiteout;
1387 if (__predict_false(!doingcache)) {
1388 cnp->cn_flags &= ~MAKEENTRY;
1393 if (__predict_false(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.'))
1394 return (cache_lookup_dot(dvp, vpp, cnp, tsp, ticksp));
1396 if ((cnp->cn_flags & MAKEENTRY) == 0)
1397 return (cache_lookup_nomakeentry(dvp, vpp, cnp, tsp, ticksp));
1400 if (cnp->cn_nameiop == CREATE)
1407 if (cnp->cn_namelen == 2 &&
1408 cnp->cn_nameptr[0] == '.' && cnp->cn_nameptr[1] == '.') {
1409 counter_u64_add(dotdothits, 1);
1410 dvlp = VP2VNODELOCK(dvp);
1412 ncp = dvp->v_cache_dd;
1414 SDT_PROBE3(vfs, namecache, lookup, miss, dvp,
1419 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1420 if (ncp->nc_flag & NCF_NEGATIVE)
1426 /* Return failure if negative entry was found. */
1428 goto negative_success;
1429 CTR3(KTR_VFS, "cache_lookup(%p, %s) found %p via ..",
1430 dvp, cnp->cn_nameptr, *vpp);
1431 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, "..",
1433 cache_out_ts(ncp, tsp, ticksp);
1434 if ((ncp->nc_flag & (NCF_ISDOTDOT | NCF_DTS)) ==
1435 NCF_DTS && tsp != NULL) {
1436 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1437 *tsp = ncp_ts->nc_dotdottime;
1442 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1449 blp = HASH2BUCKETLOCK(hash);
1453 CK_LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1454 counter_u64_add(numchecks, 1);
1455 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1456 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1460 /* We failed to find an entry */
1461 if (__predict_false(ncp == NULL)) {
1466 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
1468 counter_u64_add(nummiss, 1);
1472 if (ncp->nc_flag & NCF_NEGATIVE)
1473 goto negative_success;
1475 /* We found a "positive" match, return the vnode */
1476 counter_u64_add(numposhits, 1);
1478 CTR4(KTR_VFS, "cache_lookup(%p, %s) found %p via ncp %p",
1479 dvp, cnp->cn_nameptr, *vpp, ncp);
1480 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name,
1482 cache_out_ts(ncp, tsp, ticksp);
1485 * On success we return a locked and ref'd vnode as per the lookup
1489 ltype = 0; /* silence gcc warning */
1490 if (cnp->cn_flags & ISDOTDOT) {
1491 ltype = VOP_ISLOCKED(dvp);
1495 if (cache_ncp_invalid(ncp)) {
1500 vs = vget_prep_smr(*vpp);
1502 if (vs == VGET_NONE) {
1507 vs = vget_prep(*vpp);
1508 cache_lookup_unlock(blp, dvlp);
1510 error = vget_finish(*vpp, cnp->cn_lkflags, vs);
1511 if (cnp->cn_flags & ISDOTDOT) {
1512 vn_lock(dvp, ltype | LK_RETRY);
1513 if (VN_IS_DOOMED(dvp)) {
1524 if ((cnp->cn_flags & ISLASTCN) &&
1525 (cnp->cn_lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE) {
1526 ASSERT_VOP_ELOCKED(*vpp, "cache_lookup");
1531 /* We found a negative match, and want to create it, so purge */
1532 if (cnp->cn_nameiop == CREATE) {
1534 counter_u64_add(numnegzaps, 1);
1538 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp, ncp->nc_name);
1539 cache_out_ts(ncp, tsp, ticksp);
1540 counter_u64_add(numneghits, 1);
1541 whiteout = (ncp->nc_flag & NCF_WHITE);
1545 * We need to take locks to promote an entry.
1547 negstate = NCP2NEGSTATE(ncp);
1548 if ((negstate->neg_flag & NEG_HOT) == 0 ||
1549 cache_ncp_invalid(ncp)) {
1556 cache_negative_hit(ncp);
1557 cache_lookup_unlock(blp, dvlp);
1560 cnp->cn_flags |= ISWHITEOUT;
1566 error = cache_zap_rlocked_bucket(ncp, cnp, hash, blp);
1568 error = cache_zap_locked_vnode(ncp, dvp);
1569 if (__predict_false(error != 0)) {
1570 zap_and_exit_bucket_fail2++;
1571 cache_maybe_yield();
1578 struct celockstate {
1580 struct rwlock *blp[2];
1582 CTASSERT((nitems(((struct celockstate *)0)->vlp) == 3));
1583 CTASSERT((nitems(((struct celockstate *)0)->blp) == 2));
1586 cache_celockstate_init(struct celockstate *cel)
1589 bzero(cel, sizeof(*cel));
1593 cache_lock_vnodes_cel(struct celockstate *cel, struct vnode *vp,
1596 struct mtx *vlp1, *vlp2;
1598 MPASS(cel->vlp[0] == NULL);
1599 MPASS(cel->vlp[1] == NULL);
1600 MPASS(cel->vlp[2] == NULL);
1602 MPASS(vp != NULL || dvp != NULL);
1604 vlp1 = VP2VNODELOCK(vp);
1605 vlp2 = VP2VNODELOCK(dvp);
1606 cache_sort_vnodes(&vlp1, &vlp2);
1617 cache_unlock_vnodes_cel(struct celockstate *cel)
1620 MPASS(cel->vlp[0] != NULL || cel->vlp[1] != NULL);
1622 if (cel->vlp[0] != NULL)
1623 mtx_unlock(cel->vlp[0]);
1624 if (cel->vlp[1] != NULL)
1625 mtx_unlock(cel->vlp[1]);
1626 if (cel->vlp[2] != NULL)
1627 mtx_unlock(cel->vlp[2]);
1631 cache_lock_vnodes_cel_3(struct celockstate *cel, struct vnode *vp)
1636 cache_assert_vlp_locked(cel->vlp[0]);
1637 cache_assert_vlp_locked(cel->vlp[1]);
1638 MPASS(cel->vlp[2] == NULL);
1641 vlp = VP2VNODELOCK(vp);
1644 if (vlp >= cel->vlp[1]) {
1647 if (mtx_trylock(vlp))
1649 cache_lock_vnodes_cel_3_failures++;
1650 cache_unlock_vnodes_cel(cel);
1651 if (vlp < cel->vlp[0]) {
1653 mtx_lock(cel->vlp[0]);
1654 mtx_lock(cel->vlp[1]);
1656 if (cel->vlp[0] != NULL)
1657 mtx_lock(cel->vlp[0]);
1659 mtx_lock(cel->vlp[1]);
1669 cache_lock_buckets_cel(struct celockstate *cel, struct rwlock *blp1,
1670 struct rwlock *blp2)
1673 MPASS(cel->blp[0] == NULL);
1674 MPASS(cel->blp[1] == NULL);
1676 cache_sort_vnodes(&blp1, &blp2);
1687 cache_unlock_buckets_cel(struct celockstate *cel)
1690 if (cel->blp[0] != NULL)
1691 rw_wunlock(cel->blp[0]);
1692 rw_wunlock(cel->blp[1]);
1696 * Lock part of the cache affected by the insertion.
1698 * This means vnodelocks for dvp, vp and the relevant bucketlock.
1699 * However, insertion can result in removal of an old entry. In this
1700 * case we have an additional vnode and bucketlock pair to lock. If the
1701 * entry is negative, ncelock is locked instead of the vnode.
1703 * That is, in the worst case we have to lock 3 vnodes and 2 bucketlocks, while
1704 * preserving the locking order (smaller address first).
1707 cache_enter_lock(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1710 struct namecache *ncp;
1711 struct rwlock *blps[2];
1713 blps[0] = HASH2BUCKETLOCK(hash);
1716 cache_lock_vnodes_cel(cel, dvp, vp);
1717 if (vp == NULL || vp->v_type != VDIR)
1719 ncp = vp->v_cache_dd;
1722 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1724 MPASS(ncp->nc_dvp == vp);
1725 blps[1] = NCP2BUCKETLOCK(ncp);
1726 if (ncp->nc_flag & NCF_NEGATIVE)
1728 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1731 * All vnodes got re-locked. Re-validate the state and if
1732 * nothing changed we are done. Otherwise restart.
1734 if (ncp == vp->v_cache_dd &&
1735 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1736 blps[1] == NCP2BUCKETLOCK(ncp) &&
1737 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1739 cache_unlock_vnodes_cel(cel);
1744 cache_lock_buckets_cel(cel, blps[0], blps[1]);
1748 cache_enter_lock_dd(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1751 struct namecache *ncp;
1752 struct rwlock *blps[2];
1754 blps[0] = HASH2BUCKETLOCK(hash);
1757 cache_lock_vnodes_cel(cel, dvp, vp);
1758 ncp = dvp->v_cache_dd;
1761 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1763 MPASS(ncp->nc_dvp == dvp);
1764 blps[1] = NCP2BUCKETLOCK(ncp);
1765 if (ncp->nc_flag & NCF_NEGATIVE)
1767 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1769 if (ncp == dvp->v_cache_dd &&
1770 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1771 blps[1] == NCP2BUCKETLOCK(ncp) &&
1772 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1774 cache_unlock_vnodes_cel(cel);
1779 cache_lock_buckets_cel(cel, blps[0], blps[1]);
1783 cache_enter_unlock(struct celockstate *cel)
1786 cache_unlock_buckets_cel(cel);
1787 cache_unlock_vnodes_cel(cel);
1790 static void __noinline
1791 cache_enter_dotdot_prep(struct vnode *dvp, struct vnode *vp,
1792 struct componentname *cnp)
1794 struct celockstate cel;
1795 struct namecache *ncp;
1799 if (dvp->v_cache_dd == NULL)
1801 len = cnp->cn_namelen;
1802 cache_celockstate_init(&cel);
1803 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1804 cache_enter_lock_dd(&cel, dvp, vp, hash);
1805 ncp = dvp->v_cache_dd;
1806 if (ncp != NULL && (ncp->nc_flag & NCF_ISDOTDOT)) {
1807 KASSERT(ncp->nc_dvp == dvp, ("wrong isdotdot parent"));
1808 cache_zap_locked(ncp);
1812 dvp->v_cache_dd = NULL;
1813 cache_enter_unlock(&cel);
1818 * Add an entry to the cache.
1821 cache_enter_time(struct vnode *dvp, struct vnode *vp, struct componentname *cnp,
1822 struct timespec *tsp, struct timespec *dtsp)
1824 struct celockstate cel;
1825 struct namecache *ncp, *n2, *ndd;
1826 struct namecache_ts *ncp_ts, *n2_ts;
1827 struct nchashhead *ncpp;
1833 CTR3(KTR_VFS, "cache_enter(%p, %p, %s)", dvp, vp, cnp->cn_nameptr);
1834 VNASSERT(vp == NULL || !VN_IS_DOOMED(vp), vp,
1835 ("cache_enter: Adding a doomed vnode"));
1836 VNASSERT(dvp == NULL || !VN_IS_DOOMED(dvp), dvp,
1837 ("cache_enter: Doomed vnode used as src"));
1840 if (__predict_false(!doingcache))
1845 if (__predict_false(cnp->cn_nameptr[0] == '.')) {
1846 if (cnp->cn_namelen == 1)
1848 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
1849 cache_enter_dotdot_prep(dvp, vp, cnp);
1850 flag = NCF_ISDOTDOT;
1855 * Avoid blowout in namecache entries.
1857 lnumcache = atomic_fetchadd_long(&numcache, 1) + 1;
1858 if (__predict_false(lnumcache >= ncsize)) {
1859 atomic_add_long(&numcache, -1);
1860 counter_u64_add(numdrops, 1);
1864 cache_celockstate_init(&cel);
1869 * Calculate the hash key and setup as much of the new
1870 * namecache entry as possible before acquiring the lock.
1872 ncp = cache_alloc(cnp->cn_namelen, tsp != NULL);
1873 ncp->nc_flag = flag;
1876 cache_negative_init(ncp);
1879 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1880 ncp_ts->nc_time = *tsp;
1881 ncp_ts->nc_ticks = ticks;
1882 ncp_ts->nc_nc.nc_flag |= NCF_TS;
1884 ncp_ts->nc_dotdottime = *dtsp;
1885 ncp_ts->nc_nc.nc_flag |= NCF_DTS;
1888 len = ncp->nc_nlen = cnp->cn_namelen;
1889 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1890 strlcpy(ncp->nc_name, cnp->cn_nameptr, len + 1);
1891 cache_enter_lock(&cel, dvp, vp, hash);
1894 * See if this vnode or negative entry is already in the cache
1895 * with this name. This can happen with concurrent lookups of
1896 * the same path name.
1898 ncpp = NCHHASH(hash);
1899 CK_LIST_FOREACH(n2, ncpp, nc_hash) {
1900 if (n2->nc_dvp == dvp &&
1901 n2->nc_nlen == cnp->cn_namelen &&
1902 !bcmp(n2->nc_name, cnp->cn_nameptr, n2->nc_nlen)) {
1904 KASSERT((n2->nc_flag & NCF_TS) != 0,
1906 n2_ts = __containerof(n2, struct namecache_ts, nc_nc);
1907 n2_ts->nc_time = ncp_ts->nc_time;
1908 n2_ts->nc_ticks = ncp_ts->nc_ticks;
1910 n2_ts->nc_dotdottime = ncp_ts->nc_dotdottime;
1911 n2_ts->nc_nc.nc_flag |= NCF_DTS;
1914 goto out_unlock_free;
1918 if (flag == NCF_ISDOTDOT) {
1920 * See if we are trying to add .. entry, but some other lookup
1921 * has populated v_cache_dd pointer already.
1923 if (dvp->v_cache_dd != NULL)
1924 goto out_unlock_free;
1925 KASSERT(vp == NULL || vp->v_type == VDIR,
1926 ("wrong vnode type %p", vp));
1927 dvp->v_cache_dd = ncp;
1931 if (vp->v_type == VDIR) {
1932 if (flag != NCF_ISDOTDOT) {
1934 * For this case, the cache entry maps both the
1935 * directory name in it and the name ".." for the
1936 * directory's parent.
1938 if ((ndd = vp->v_cache_dd) != NULL) {
1939 if ((ndd->nc_flag & NCF_ISDOTDOT) != 0)
1940 cache_zap_locked(ndd);
1944 vp->v_cache_dd = ncp;
1947 vp->v_cache_dd = NULL;
1951 if (flag != NCF_ISDOTDOT) {
1952 if (LIST_EMPTY(&dvp->v_cache_src)) {
1954 counter_u64_add(numcachehv, 1);
1956 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
1960 * If the entry is "negative", we place it into the
1961 * "negative" cache queue, otherwise, we place it into the
1962 * destination vnode's cache entries queue.
1965 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
1966 SDT_PROBE3(vfs, namecache, enter, done, dvp, ncp->nc_name,
1969 if (cnp->cn_flags & ISWHITEOUT)
1970 ncp->nc_flag |= NCF_WHITE;
1971 cache_negative_insert(ncp);
1972 SDT_PROBE2(vfs, namecache, enter_negative, done, dvp,
1976 atomic_thread_fence_rel();
1978 * Insert the new namecache entry into the appropriate chain
1979 * within the cache entries table.
1981 CK_LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
1983 cache_enter_unlock(&cel);
1984 if (numneg * ncnegfactor > lnumcache)
1985 cache_negative_zap_one();
1989 cache_enter_unlock(&cel);
1995 cache_roundup_2(u_int val)
1999 for (res = 1; res <= val; res <<= 1)
2006 * Name cache initialization, from vfs_init() when we are booting
2009 nchinit(void *dummy __unused)
2013 cache_zone_small = uma_zcreate("S VFS Cache",
2014 sizeof(struct namecache) + CACHE_PATH_CUTOFF + 1,
2015 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache),
2017 cache_zone_small_ts = uma_zcreate("STS VFS Cache",
2018 sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1,
2019 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache_ts),
2021 cache_zone_large = uma_zcreate("L VFS Cache",
2022 sizeof(struct namecache) + NAME_MAX + 1,
2023 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache),
2025 cache_zone_large_ts = uma_zcreate("LTS VFS Cache",
2026 sizeof(struct namecache_ts) + NAME_MAX + 1,
2027 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache_ts),
2030 VFS_SMR_ZONE_SET(cache_zone_small);
2031 VFS_SMR_ZONE_SET(cache_zone_small_ts);
2032 VFS_SMR_ZONE_SET(cache_zone_large);
2033 VFS_SMR_ZONE_SET(cache_zone_large_ts);
2035 ncsize = desiredvnodes * ncsizefactor;
2036 nchashtbl = hashinit(desiredvnodes * 2, M_VFSCACHE, &nchash);
2037 ncbuckethash = cache_roundup_2(mp_ncpus * mp_ncpus) - 1;
2038 if (ncbuckethash < 7) /* arbitrarily chosen to avoid having one lock */
2040 if (ncbuckethash > nchash)
2041 ncbuckethash = nchash;
2042 bucketlocks = malloc(sizeof(*bucketlocks) * numbucketlocks, M_VFSCACHE,
2044 for (i = 0; i < numbucketlocks; i++)
2045 rw_init_flags(&bucketlocks[i], "ncbuc", RW_DUPOK | RW_RECURSE);
2046 ncvnodehash = ncbuckethash;
2047 vnodelocks = malloc(sizeof(*vnodelocks) * numvnodelocks, M_VFSCACHE,
2049 for (i = 0; i < numvnodelocks; i++)
2050 mtx_init(&vnodelocks[i], "ncvn", NULL, MTX_DUPOK | MTX_RECURSE);
2051 ncpurgeminvnodes = numbucketlocks * 2;
2054 neglists = malloc(sizeof(*neglists) * numneglists, M_VFSCACHE,
2056 for (i = 0; i < numneglists; i++) {
2057 mtx_init(&neglists[i].nl_lock, "ncnegl", NULL, MTX_DEF);
2058 TAILQ_INIT(&neglists[i].nl_list);
2060 mtx_init(&ncneg_hot.nl_lock, "ncneglh", NULL, MTX_DEF);
2061 TAILQ_INIT(&ncneg_hot.nl_list);
2063 mtx_init(&ncneg_shrink_lock, "ncnegs", NULL, MTX_DEF);
2065 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL);
2068 cache_changesize(u_long newmaxvnodes)
2070 struct nchashhead *new_nchashtbl, *old_nchashtbl;
2071 u_long new_nchash, old_nchash;
2072 struct namecache *ncp;
2077 newncsize = newmaxvnodes * ncsizefactor;
2078 newmaxvnodes = cache_roundup_2(newmaxvnodes * 2);
2079 if (newmaxvnodes < numbucketlocks)
2080 newmaxvnodes = numbucketlocks;
2082 new_nchashtbl = hashinit(newmaxvnodes, M_VFSCACHE, &new_nchash);
2083 /* If same hash table size, nothing to do */
2084 if (nchash == new_nchash) {
2085 free(new_nchashtbl, M_VFSCACHE);
2089 * Move everything from the old hash table to the new table.
2090 * None of the namecache entries in the table can be removed
2091 * because to do so, they have to be removed from the hash table.
2093 cache_lock_all_vnodes();
2094 cache_lock_all_buckets();
2095 old_nchashtbl = nchashtbl;
2096 old_nchash = nchash;
2097 nchashtbl = new_nchashtbl;
2098 nchash = new_nchash;
2099 for (i = 0; i <= old_nchash; i++) {
2100 while ((ncp = CK_LIST_FIRST(&old_nchashtbl[i])) != NULL) {
2101 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen,
2103 CK_LIST_REMOVE(ncp, nc_hash);
2104 CK_LIST_INSERT_HEAD(NCHHASH(hash), ncp, nc_hash);
2108 cache_unlock_all_buckets();
2109 cache_unlock_all_vnodes();
2110 free(old_nchashtbl, M_VFSCACHE);
2114 * Invalidate all entries from and to a particular vnode.
2117 cache_purge(struct vnode *vp)
2119 TAILQ_HEAD(, namecache) ncps;
2120 struct namecache *ncp, *nnp;
2121 struct mtx *vlp, *vlp2;
2123 CTR1(KTR_VFS, "cache_purge(%p)", vp);
2124 SDT_PROBE1(vfs, namecache, purge, done, vp);
2125 if (LIST_EMPTY(&vp->v_cache_src) && TAILQ_EMPTY(&vp->v_cache_dst) &&
2126 vp->v_cache_dd == NULL)
2129 vlp = VP2VNODELOCK(vp);
2133 while (!LIST_EMPTY(&vp->v_cache_src)) {
2134 ncp = LIST_FIRST(&vp->v_cache_src);
2135 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2137 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2139 while (!TAILQ_EMPTY(&vp->v_cache_dst)) {
2140 ncp = TAILQ_FIRST(&vp->v_cache_dst);
2141 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2143 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2145 ncp = vp->v_cache_dd;
2147 KASSERT(ncp->nc_flag & NCF_ISDOTDOT,
2148 ("lost dotdot link"));
2149 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2151 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2153 KASSERT(vp->v_cache_dd == NULL, ("incomplete purge"));
2157 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2163 * Invalidate all negative entries for a particular directory vnode.
2166 cache_purge_negative(struct vnode *vp)
2168 TAILQ_HEAD(, namecache) ncps;
2169 struct namecache *ncp, *nnp;
2172 CTR1(KTR_VFS, "cache_purge_negative(%p)", vp);
2173 SDT_PROBE1(vfs, namecache, purge_negative, done, vp);
2174 if (LIST_EMPTY(&vp->v_cache_src))
2177 vlp = VP2VNODELOCK(vp);
2179 LIST_FOREACH_SAFE(ncp, &vp->v_cache_src, nc_src, nnp) {
2180 if (!(ncp->nc_flag & NCF_NEGATIVE))
2182 cache_zap_negative_locked_vnode_kl(ncp, vp);
2183 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2186 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2192 * Flush all entries referencing a particular filesystem.
2195 cache_purgevfs(struct mount *mp, bool force)
2197 TAILQ_HEAD(, namecache) ncps;
2198 struct mtx *vlp1, *vlp2;
2200 struct nchashhead *bucket;
2201 struct namecache *ncp, *nnp;
2202 u_long i, j, n_nchash;
2205 /* Scan hash tables for applicable entries */
2206 SDT_PROBE1(vfs, namecache, purgevfs, done, mp);
2207 if (!force && mp->mnt_nvnodelistsize <= ncpurgeminvnodes)
2210 n_nchash = nchash + 1;
2212 for (i = 0; i < numbucketlocks; i++) {
2213 blp = (struct rwlock *)&bucketlocks[i];
2215 for (j = i; j < n_nchash; j += numbucketlocks) {
2217 bucket = &nchashtbl[j];
2218 CK_LIST_FOREACH_SAFE(ncp, bucket, nc_hash, nnp) {
2219 cache_assert_bucket_locked(ncp, RA_WLOCKED);
2220 if (ncp->nc_dvp->v_mount != mp)
2222 error = cache_zap_wlocked_bucket_kl(ncp, blp,
2226 TAILQ_INSERT_HEAD(&ncps, ncp, nc_dst);
2230 if (vlp1 == NULL && vlp2 == NULL)
2231 cache_maybe_yield();
2238 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2244 * Perform canonical checks and cache lookup and pass on to filesystem
2245 * through the vop_cachedlookup only if needed.
2249 vfs_cache_lookup(struct vop_lookup_args *ap)
2253 struct vnode **vpp = ap->a_vpp;
2254 struct componentname *cnp = ap->a_cnp;
2255 int flags = cnp->cn_flags;
2260 if (dvp->v_type != VDIR)
2263 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
2264 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
2267 error = vn_dir_check_exec(dvp, cnp);
2271 error = cache_lookup(dvp, vpp, cnp, NULL, NULL);
2273 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
2279 /* Implementation of the getcwd syscall. */
2281 sys___getcwd(struct thread *td, struct __getcwd_args *uap)
2287 buflen = uap->buflen;
2288 if (__predict_false(buflen < 2))
2290 if (buflen > MAXPATHLEN)
2291 buflen = MAXPATHLEN;
2293 buf = malloc(buflen, M_TEMP, M_WAITOK);
2294 error = vn_getcwd(td, buf, &retbuf, &buflen);
2296 error = copyout(retbuf, uap->buf, buflen);
2302 vn_getcwd(struct thread *td, char *buf, char **retbuf, size_t *buflen)
2308 error = vn_fullpath_any(td, pwd->pwd_cdir, pwd->pwd_rdir, buf, retbuf, buflen);
2312 if (KTRPOINT(curthread, KTR_NAMEI) && error == 0)
2319 kern___realpathat(struct thread *td, int fd, const char *path, char *buf,
2320 size_t size, int flags, enum uio_seg pathseg)
2322 struct nameidata nd;
2323 char *retbuf, *freebuf;
2328 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | SAVENAME | WANTPARENT | AUDITVNODE1,
2329 pathseg, path, fd, &cap_fstat_rights, td);
2330 if ((error = namei(&nd)) != 0)
2332 error = vn_fullpath_hardlink(td, &nd, &retbuf, &freebuf, &size);
2334 error = copyout(retbuf, buf, size);
2335 free(freebuf, M_TEMP);
2342 sys___realpathat(struct thread *td, struct __realpathat_args *uap)
2345 return (kern___realpathat(td, uap->fd, uap->path, uap->buf, uap->size,
2346 uap->flags, UIO_USERSPACE));
2350 * Retrieve the full filesystem path that correspond to a vnode from the name
2351 * cache (if available)
2354 vn_fullpath(struct thread *td, struct vnode *vn, char **retbuf, char **freebuf)
2361 if (__predict_false(vn == NULL))
2364 buflen = MAXPATHLEN;
2365 buf = malloc(buflen, M_TEMP, M_WAITOK);
2367 error = vn_fullpath_any(td, vn, pwd->pwd_rdir, buf, retbuf, &buflen);
2378 * This function is similar to vn_fullpath, but it attempts to lookup the
2379 * pathname relative to the global root mount point. This is required for the
2380 * auditing sub-system, as audited pathnames must be absolute, relative to the
2381 * global root mount point.
2384 vn_fullpath_global(struct thread *td, struct vnode *vn,
2385 char **retbuf, char **freebuf)
2391 if (__predict_false(vn == NULL))
2393 buflen = MAXPATHLEN;
2394 buf = malloc(buflen, M_TEMP, M_WAITOK);
2395 error = vn_fullpath_any(td, vn, rootvnode, buf, retbuf, &buflen);
2404 vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, size_t *buflen)
2407 struct namecache *ncp;
2411 vlp = VP2VNODELOCK(*vp);
2413 TAILQ_FOREACH(ncp, &((*vp)->v_cache_dst), nc_dst) {
2414 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2418 if (*buflen < ncp->nc_nlen) {
2421 counter_u64_add(numfullpathfail4, 1);
2423 SDT_PROBE3(vfs, namecache, fullpath, return, error,
2427 *buflen -= ncp->nc_nlen;
2428 memcpy(buf + *buflen, ncp->nc_name, ncp->nc_nlen);
2429 SDT_PROBE3(vfs, namecache, fullpath, hit, ncp->nc_dvp,
2438 SDT_PROBE1(vfs, namecache, fullpath, miss, vp);
2441 vn_lock(*vp, LK_SHARED | LK_RETRY);
2442 error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen);
2445 counter_u64_add(numfullpathfail2, 1);
2446 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2451 if (VN_IS_DOOMED(dvp)) {
2452 /* forced unmount */
2455 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2459 * *vp has its use count incremented still.
2466 * Resolve a directory to a pathname.
2468 * The name of the directory can always be found in the namecache or fetched
2469 * from the filesystem. There is also guaranteed to be only one parent, meaning
2470 * we can just follow vnodes up until we find the root.
2472 * The vnode must be referenced.
2475 vn_fullpath_dir(struct thread *td, struct vnode *vp, struct vnode *rdir,
2476 char *buf, char **retbuf, size_t *len, bool slash_prefixed, size_t addend)
2478 #ifdef KDTRACE_HOOKS
2479 struct vnode *startvp = vp;
2485 VNPASS(vp->v_type == VDIR || VN_IS_DOOMED(vp), vp);
2486 VNPASS(vp->v_usecount > 0, vp);
2490 if (!slash_prefixed) {
2498 SDT_PROBE1(vfs, namecache, fullpath, entry, vp);
2499 counter_u64_add(numfullpathcalls, 1);
2500 while (vp != rdir && vp != rootvnode) {
2502 * The vp vnode must be already fully constructed,
2503 * since it is either found in namecache or obtained
2504 * from VOP_VPTOCNP(). We may test for VV_ROOT safely
2505 * without obtaining the vnode lock.
2507 if ((vp->v_vflag & VV_ROOT) != 0) {
2508 vn_lock(vp, LK_RETRY | LK_SHARED);
2511 * With the vnode locked, check for races with
2512 * unmount, forced or not. Note that we
2513 * already verified that vp is not equal to
2514 * the root vnode, which means that
2515 * mnt_vnodecovered can be NULL only for the
2518 if (VN_IS_DOOMED(vp) ||
2519 (vp1 = vp->v_mount->mnt_vnodecovered) == NULL ||
2520 vp1->v_mountedhere != vp->v_mount) {
2523 SDT_PROBE3(vfs, namecache, fullpath, return,
2533 if (vp->v_type != VDIR) {
2535 counter_u64_add(numfullpathfail1, 1);
2537 SDT_PROBE3(vfs, namecache, fullpath, return,
2541 error = vn_vptocnp(&vp, td->td_ucred, buf, &buflen);
2547 SDT_PROBE3(vfs, namecache, fullpath, return, error,
2551 buf[--buflen] = '/';
2552 slash_prefixed = true;
2556 if (!slash_prefixed) {
2559 counter_u64_add(numfullpathfail4, 1);
2560 SDT_PROBE3(vfs, namecache, fullpath, return, ENOMEM,
2564 buf[--buflen] = '/';
2566 counter_u64_add(numfullpathfound, 1);
2569 *retbuf = buf + buflen;
2570 SDT_PROBE3(vfs, namecache, fullpath, return, 0, startvp, *retbuf);
2577 * Resolve an arbitrary vnode to a pathname.
2580 * - hardlinks are not tracked, thus if the vnode is not a directory this can
2581 * resolve to a different path than the one used to find it
2582 * - namecache is not mandatory, meaning names are not guaranteed to be added
2583 * (in which case resolving fails)
2586 vn_fullpath_any(struct thread *td, struct vnode *vp, struct vnode *rdir,
2587 char *buf, char **retbuf, size_t *buflen)
2590 bool slash_prefixed;
2596 orig_buflen = *buflen;
2599 slash_prefixed = false;
2600 if (vp->v_type != VDIR) {
2602 buf[*buflen] = '\0';
2603 error = vn_vptocnp(&vp, td->td_ucred, buf, buflen);
2612 slash_prefixed = true;
2615 return (vn_fullpath_dir(td, vp, rdir, buf, retbuf, buflen, slash_prefixed,
2616 orig_buflen - *buflen));
2620 * Resolve an arbitrary vnode to a pathname (taking care of hardlinks).
2622 * Since the namecache does not track handlings, the caller is expected to first
2623 * look up the target vnode with SAVENAME | WANTPARENT flags passed to namei.
2625 * Then we have 2 cases:
2626 * - if the found vnode is a directory, the path can be constructed just by
2627 * fullowing names up the chain
2628 * - otherwise we populate the buffer with the saved name and start resolving
2632 vn_fullpath_hardlink(struct thread *td, struct nameidata *ndp, char **retbuf,
2633 char **freebuf, size_t *buflen)
2637 struct componentname *cnp;
2641 bool slash_prefixed;
2645 if (*buflen > MAXPATHLEN)
2646 *buflen = MAXPATHLEN;
2648 slash_prefixed = false;
2650 buf = malloc(*buflen, M_TEMP, M_WAITOK);
2655 if (vp->v_type != VDIR) {
2657 addend = cnp->cn_namelen + 2;
2658 if (*buflen < addend) {
2663 tmpbuf = buf + *buflen;
2665 memcpy(&tmpbuf[1], cnp->cn_nameptr, cnp->cn_namelen);
2666 tmpbuf[addend - 1] = '\0';
2667 slash_prefixed = true;
2672 error = vn_fullpath_dir(td, vp, pwd->pwd_rdir, buf, retbuf, buflen,
2673 slash_prefixed, addend);
2688 vn_dir_dd_ino(struct vnode *vp)
2690 struct namecache *ncp;
2695 ASSERT_VOP_LOCKED(vp, "vn_dir_dd_ino");
2696 vlp = VP2VNODELOCK(vp);
2698 TAILQ_FOREACH(ncp, &(vp->v_cache_dst), nc_dst) {
2699 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0)
2702 vs = vget_prep(ddvp);
2704 if (vget_finish(ddvp, LK_SHARED | LK_NOWAIT, vs))
2713 vn_commname(struct vnode *vp, char *buf, u_int buflen)
2715 struct namecache *ncp;
2719 vlp = VP2VNODELOCK(vp);
2721 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst)
2722 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2728 l = min(ncp->nc_nlen, buflen - 1);
2729 memcpy(buf, ncp->nc_name, l);
2736 * This function updates path string to vnode's full global path
2737 * and checks the size of the new path string against the pathlen argument.
2739 * Requires a locked, referenced vnode.
2740 * Vnode is re-locked on success or ENODEV, otherwise unlocked.
2742 * If vp is a directory, the call to vn_fullpath_global() always succeeds
2743 * because it falls back to the ".." lookup if the namecache lookup fails.
2746 vn_path_to_global_path(struct thread *td, struct vnode *vp, char *path,
2749 struct nameidata nd;
2754 ASSERT_VOP_ELOCKED(vp, __func__);
2756 /* Construct global filesystem path from vp. */
2758 error = vn_fullpath_global(td, vp, &rpath, &fbuf);
2765 if (strlen(rpath) >= pathlen) {
2767 error = ENAMETOOLONG;
2772 * Re-lookup the vnode by path to detect a possible rename.
2773 * As a side effect, the vnode is relocked.
2774 * If vnode was renamed, return ENOENT.
2776 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
2777 UIO_SYSSPACE, path, td);
2783 NDFREE(&nd, NDF_ONLY_PNBUF);
2787 strcpy(path, rpath);
2800 db_print_vpath(struct vnode *vp)
2803 while (vp != NULL) {
2804 db_printf("%p: ", vp);
2805 if (vp == rootvnode) {
2809 if (vp->v_vflag & VV_ROOT) {
2810 db_printf("<mount point>");
2811 vp = vp->v_mount->mnt_vnodecovered;
2813 struct namecache *ncp;
2817 ncp = TAILQ_FIRST(&vp->v_cache_dst);
2820 for (i = 0; i < ncp->nc_nlen; i++)
2821 db_printf("%c", *ncn++);
2834 DB_SHOW_COMMAND(vpath, db_show_vpath)
2839 db_printf("usage: show vpath <struct vnode *>\n");
2843 vp = (struct vnode *)addr;
2849 extern uma_zone_t namei_zone;
2851 static bool __read_frequently cache_fast_lookup = true;
2852 SYSCTL_BOOL(_vfs, OID_AUTO, cache_fast_lookup, CTLFLAG_RW,
2853 &cache_fast_lookup, 0, "");
2855 #define CACHE_FPL_FAILED -2020
2858 cache_fpl_cleanup_cnp(struct componentname *cnp)
2861 uma_zfree(namei_zone, cnp->cn_pnbuf);
2863 cnp->cn_pnbuf = NULL;
2864 cnp->cn_nameptr = NULL;
2869 cache_fpl_handle_root(struct nameidata *ndp, struct vnode **dpp)
2871 struct componentname *cnp;
2874 while (*(cnp->cn_nameptr) == '/') {
2879 *dpp = ndp->ni_rootdir;
2883 * Components of nameidata (or objects it can point to) which may
2884 * need restoring in case fast path lookup fails.
2886 struct nameidata_saved {
2895 enum cache_fpl_status status;
2897 struct nameidata *ndp;
2898 struct nameidata_saved snd;
2899 struct componentname *cnp;
2908 cache_fpl_checkpoint(struct cache_fpl *fpl, struct nameidata_saved *snd)
2911 snd->cn_flags = fpl->ndp->ni_cnd.cn_flags;
2912 snd->cn_namelen = fpl->ndp->ni_cnd.cn_namelen;
2913 snd->cn_nameptr = fpl->ndp->ni_cnd.cn_nameptr;
2914 snd->ni_pathlen = fpl->ndp->ni_pathlen;
2918 cache_fpl_restore(struct cache_fpl *fpl, struct nameidata_saved *snd)
2921 fpl->ndp->ni_cnd.cn_flags = snd->cn_flags;
2922 fpl->ndp->ni_cnd.cn_namelen = snd->cn_namelen;
2923 fpl->ndp->ni_cnd.cn_nameptr = snd->cn_nameptr;
2924 fpl->ndp->ni_pathlen = snd->ni_pathlen;
2928 #define cache_fpl_smr_assert_entered(fpl) ({ \
2929 struct cache_fpl *_fpl = (fpl); \
2930 MPASS(_fpl->in_smr == true); \
2931 VFS_SMR_ASSERT_ENTERED(); \
2933 #define cache_fpl_smr_assert_not_entered(fpl) ({ \
2934 struct cache_fpl *_fpl = (fpl); \
2935 MPASS(_fpl->in_smr == false); \
2936 VFS_SMR_ASSERT_NOT_ENTERED(); \
2939 #define cache_fpl_smr_assert_entered(fpl) do { } while (0)
2940 #define cache_fpl_smr_assert_not_entered(fpl) do { } while (0)
2943 #define cache_fpl_smr_enter(fpl) ({ \
2944 struct cache_fpl *_fpl = (fpl); \
2945 MPASS(_fpl->in_smr == false); \
2947 _fpl->in_smr = true; \
2950 #define cache_fpl_smr_exit(fpl) ({ \
2951 struct cache_fpl *_fpl = (fpl); \
2952 MPASS(_fpl->in_smr == true); \
2954 _fpl->in_smr = false; \
2958 cache_fpl_aborted_impl(struct cache_fpl *fpl, int line)
2961 if (fpl->status != CACHE_FPL_STATUS_UNSET) {
2962 KASSERT(fpl->status == CACHE_FPL_STATUS_PARTIAL,
2963 ("%s: converting to abort from %d at %d, set at %d\n",
2964 __func__, fpl->status, line, fpl->line));
2966 fpl->status = CACHE_FPL_STATUS_ABORTED;
2968 return (CACHE_FPL_FAILED);
2971 #define cache_fpl_aborted(x) cache_fpl_aborted_impl((x), __LINE__)
2974 cache_fpl_partial_impl(struct cache_fpl *fpl, int line)
2977 KASSERT(fpl->status == CACHE_FPL_STATUS_UNSET,
2978 ("%s: setting to partial at %d, but already set to %d at %d\n",
2979 __func__, line, fpl->status, fpl->line));
2980 cache_fpl_smr_assert_entered(fpl);
2981 fpl->status = CACHE_FPL_STATUS_PARTIAL;
2983 return (CACHE_FPL_FAILED);
2986 #define cache_fpl_partial(x) cache_fpl_partial_impl((x), __LINE__)
2989 cache_fpl_handled_impl(struct cache_fpl *fpl, int error, int line)
2992 KASSERT(fpl->status == CACHE_FPL_STATUS_UNSET,
2993 ("%s: setting to handled at %d, but already set to %d at %d\n",
2994 __func__, line, fpl->status, fpl->line));
2995 cache_fpl_smr_assert_not_entered(fpl);
2996 MPASS(error != CACHE_FPL_FAILED);
2997 fpl->status = CACHE_FPL_STATUS_HANDLED;
3002 #define cache_fpl_handled(x, e) cache_fpl_handled_impl((x), (e), __LINE__)
3004 #define CACHE_FPL_SUPPORTED_CN_FLAGS \
3005 (LOCKLEAF | FOLLOW | LOCKSHARED | SAVENAME | ISOPEN | AUDITVNODE1)
3008 cache_can_fplookup(struct cache_fpl *fpl)
3010 struct nameidata *ndp;
3011 struct componentname *cnp;
3016 td = cnp->cn_thread;
3018 if (!cache_fast_lookup) {
3019 cache_fpl_aborted(fpl);
3023 if (mac_vnode_check_lookup_enabled()) {
3024 cache_fpl_aborted(fpl);
3028 if ((cnp->cn_flags & ~CACHE_FPL_SUPPORTED_CN_FLAGS) != 0) {
3029 cache_fpl_aborted(fpl);
3032 if (cnp->cn_nameiop != LOOKUP) {
3033 cache_fpl_aborted(fpl);
3036 if (ndp->ni_dirfd != AT_FDCWD) {
3037 cache_fpl_aborted(fpl);
3040 if (IN_CAPABILITY_MODE(td)) {
3041 cache_fpl_aborted(fpl);
3044 if (AUDITING_TD(td)) {
3045 cache_fpl_aborted(fpl);
3048 if (ndp->ni_startdir != NULL) {
3049 cache_fpl_aborted(fpl);
3056 cache_fplookup_vnode_supported(struct vnode *vp)
3059 return (vp->v_type != VLNK);
3063 * The target vnode is not supported, prepare for the slow path to take over.
3066 cache_fplookup_partial_setup(struct cache_fpl *fpl)
3068 struct componentname *cnp;
3076 dvp_seqc = fpl->dvp_seqc;
3078 dvs = vget_prep_smr(dvp);
3079 if (dvs == VGET_NONE) {
3080 cache_fpl_smr_exit(fpl);
3081 return (cache_fpl_aborted(fpl));
3084 cache_fpl_smr_exit(fpl);
3086 vget_finish_ref(dvp, dvs);
3087 if (!vn_seqc_consistent(dvp, dvp_seqc)) {
3089 return (cache_fpl_aborted(fpl));
3092 pwd = pwd_hold(curthread);
3093 if (fpl->pwd != pwd) {
3096 return (cache_fpl_aborted(fpl));
3099 fpl->ndp->ni_startdir = dvp;
3104 cache_fplookup_final(struct cache_fpl *fpl)
3106 struct componentname *cnp;
3108 struct vnode *dvp, *tvp;
3109 seqc_t dvp_seqc, tvp_seqc;
3114 dvp_seqc = fpl->dvp_seqc;
3116 tvp_seqc = fpl->tvp_seqc;
3118 VNPASS(cache_fplookup_vnode_supported(dvp), dvp);
3120 tvs = vget_prep_smr(tvp);
3121 if (tvs == VGET_NONE) {
3122 return (cache_fpl_partial(fpl));
3125 if (!vn_seqc_consistent(dvp, dvp_seqc)) {
3126 cache_fpl_smr_exit(fpl);
3127 vget_abort(tvp, tvs);
3128 return (cache_fpl_aborted(fpl));
3131 cache_fpl_smr_exit(fpl);
3133 if ((cnp->cn_flags & LOCKLEAF) != 0) {
3134 error = vget_finish(tvp, cnp->cn_lkflags, tvs);
3136 return (cache_fpl_aborted(fpl));
3139 vget_finish_ref(tvp, tvs);
3142 if (!vn_seqc_consistent(tvp, tvp_seqc)) {
3143 if ((cnp->cn_flags & LOCKLEAF) != 0)
3147 return (cache_fpl_aborted(fpl));
3150 return (cache_fpl_handled(fpl, 0));
3154 cache_fplookup_next(struct cache_fpl *fpl)
3156 struct componentname *cnp;
3157 struct namecache *ncp;
3158 struct negstate *negstate;
3159 struct vnode *dvp, *tvp;
3167 if (__predict_false(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.')) {
3169 fpl->tvp_seqc = vn_seqc_read_any(dvp);
3170 if (seqc_in_modify(fpl->tvp_seqc)) {
3171 return (cache_fpl_aborted(fpl));
3176 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
3178 CK_LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
3179 counter_u64_add(numchecks, 1);
3180 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
3181 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
3186 * If there is no entry we have to punt to the slow path to perform
3187 * actual lookup. Should there be nothing with this name a negative
3188 * entry will be created.
3190 if (__predict_false(ncp == NULL)) {
3191 return (cache_fpl_partial(fpl));
3194 tvp = atomic_load_ptr(&ncp->nc_vp);
3195 nc_flag = atomic_load_char(&ncp->nc_flag);
3196 if ((nc_flag & NCF_NEGATIVE) != 0) {
3197 negstate = NCP2NEGSTATE(ncp);
3198 neg_hot = ((negstate->neg_flag & NEG_HOT) != 0);
3199 if (__predict_false(cache_ncp_invalid(ncp))) {
3200 return (cache_fpl_partial(fpl));
3202 if (__predict_false((nc_flag & NCF_WHITE) != 0)) {
3203 return (cache_fpl_partial(fpl));
3208 * Promoting to hot negative requires locks, thus is
3209 * left not yet supported for simplicity.
3211 return (cache_fpl_partial(fpl));
3213 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp,
3215 counter_u64_add(numneghits, 1);
3216 cache_fpl_smr_exit(fpl);
3217 return (cache_fpl_handled(fpl, ENOENT));
3220 if (__predict_false(cache_ncp_invalid(ncp))) {
3221 return (cache_fpl_partial(fpl));
3225 fpl->tvp_seqc = vn_seqc_read_any(tvp);
3226 if (seqc_in_modify(fpl->tvp_seqc)) {
3227 return (cache_fpl_partial(fpl));
3230 if (!cache_fplookup_vnode_supported(tvp)) {
3231 return (cache_fpl_partial(fpl));
3234 counter_u64_add(numposhits, 1);
3235 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name, tvp);
3240 cache_fplookup_mp_supported(struct mount *mp)
3245 if ((mp->mnt_kern_flag & MNTK_FPLOOKUP) == 0)
3247 if ((mp->mnt_flag & MNT_UNION) != 0)
3253 * Walk up the mount stack (if any).
3255 * Correctness is provided in the following ways:
3256 * - all vnodes are protected from freeing with SMR
3257 * - struct mount objects are type stable making them always safe to access
3258 * - stability of the particular mount is provided by busying it
3259 * - relationship between the vnode which is mounted on and the mount is
3260 * verified with the vnode sequence counter after busying
3261 * - association between root vnode of the mount and the mount is protected
3264 * From that point on we can read the sequence counter of the root vnode
3265 * and get the next mount on the stack (if any) using the same protection.
3267 * By the end of successful walk we are guaranteed the reached state was
3268 * indeed present at least at some point which matches the regular lookup.
3271 cache_fplookup_climb_mount(struct cache_fpl *fpl)
3273 struct mount *mp, *prev_mp;
3278 vp_seqc = fpl->tvp_seqc;
3279 if (vp->v_type != VDIR)
3282 mp = atomic_load_ptr(&vp->v_mountedhere);
3288 if (!vfs_op_thread_enter(mp)) {
3289 if (prev_mp != NULL)
3290 vfs_op_thread_exit(prev_mp);
3291 return (cache_fpl_partial(fpl));
3293 if (prev_mp != NULL)
3294 vfs_op_thread_exit(prev_mp);
3295 if (!vn_seqc_consistent(vp, vp_seqc)) {
3296 vfs_op_thread_exit(mp);
3297 return (cache_fpl_partial(fpl));
3299 if (!cache_fplookup_mp_supported(mp)) {
3300 vfs_op_thread_exit(mp);
3301 return (cache_fpl_partial(fpl));
3303 vp = atomic_load_ptr(&mp->mnt_rootvnode);
3304 if (vp == NULL || VN_IS_DOOMED(vp)) {
3305 vfs_op_thread_exit(mp);
3306 return (cache_fpl_partial(fpl));
3308 vp_seqc = vn_seqc_read_any(vp);
3309 if (seqc_in_modify(vp_seqc)) {
3310 vfs_op_thread_exit(mp);
3311 return (cache_fpl_partial(fpl));
3314 mp = atomic_load_ptr(&vp->v_mountedhere);
3319 vfs_op_thread_exit(prev_mp);
3321 fpl->tvp_seqc = vp_seqc;
3328 * The code is mostly copy-pasted from regular lookup, see lookup().
3329 * The structure is maintained along with comments for easier maintenance.
3330 * Deduplicating the code will become feasible after fast path lookup
3331 * becomes more feature-complete.
3334 cache_fplookup_parse(struct cache_fpl *fpl)
3336 struct nameidata *ndp;
3337 struct componentname *cnp;
3339 char *prev_ni_next; /* saved ndp->ni_next */
3340 size_t prev_ni_pathlen; /* saved ndp->ni_pathlen */
3346 * Search a new directory.
3348 * The last component of the filename is left accessible via
3349 * cnp->cn_nameptr for callers that need the name. Callers needing
3350 * the name set the SAVENAME flag. When done, they assume
3351 * responsibility for freeing the pathname buffer.
3353 for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
3355 cnp->cn_namelen = cp - cnp->cn_nameptr;
3356 if (cnp->cn_namelen > NAME_MAX) {
3357 cache_fpl_smr_exit(fpl);
3358 return (cache_fpl_handled(fpl, ENAMETOOLONG));
3360 prev_ni_pathlen = ndp->ni_pathlen;
3361 ndp->ni_pathlen -= cnp->cn_namelen;
3362 KASSERT(ndp->ni_pathlen <= PATH_MAX,
3363 ("%s: ni_pathlen underflow to %zd\n", __func__, ndp->ni_pathlen));
3364 prev_ni_next = ndp->ni_next;
3368 * Replace multiple slashes by a single slash and trailing slashes
3369 * by a null. This must be done before VOP_LOOKUP() because some
3370 * fs's don't know about trailing slashes. Remember if there were
3371 * trailing slashes to handle symlinks, existing non-directories
3372 * and non-existing files that won't be directories specially later.
3374 while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
3380 * Regular lookup performs the following:
3381 * *ndp->ni_next = '\0';
3382 * cnp->cn_flags |= TRAILINGSLASH;
3384 * Which is problematic since it modifies data read
3385 * from userspace. Then if fast path lookup was to
3386 * abort we would have to either restore it or convey
3387 * the flag. Since this is a corner case just ignore
3388 * it for simplicity.
3390 return (cache_fpl_partial(fpl));
3395 cnp->cn_flags |= MAKEENTRY;
3397 if (cnp->cn_namelen == 2 &&
3398 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
3399 cnp->cn_flags |= ISDOTDOT;
3401 cnp->cn_flags &= ~ISDOTDOT;
3402 if (*ndp->ni_next == 0)
3403 cnp->cn_flags |= ISLASTCN;
3405 cnp->cn_flags &= ~ISLASTCN;
3408 * Check for degenerate name (e.g. / or "")
3409 * which is a way of talking about a directory,
3410 * e.g. like "/." or ".".
3413 * Another corner case handled by the regular lookup
3415 if (__predict_false(cnp->cn_nameptr[0] == '\0')) {
3416 return (cache_fpl_partial(fpl));
3422 cache_fplookup_parse_advance(struct cache_fpl *fpl)
3424 struct nameidata *ndp;
3425 struct componentname *cnp;
3430 cnp->cn_nameptr = ndp->ni_next;
3431 while (*cnp->cn_nameptr == '/') {
3438 cache_fplookup_impl(struct vnode *dvp, struct cache_fpl *fpl)
3440 struct nameidata *ndp;
3441 struct componentname *cnp;
3445 error = CACHE_FPL_FAILED;
3449 cnp->cn_lkflags = LK_SHARED;
3450 if ((cnp->cn_flags & LOCKSHARED) == 0)
3451 cnp->cn_lkflags = LK_EXCLUSIVE;
3453 cache_fpl_checkpoint(fpl, &fpl->snd);
3456 fpl->dvp_seqc = vn_seqc_read_any(fpl->dvp);
3457 if (seqc_in_modify(fpl->dvp_seqc)) {
3458 cache_fpl_aborted(fpl);
3461 mp = atomic_load_ptr(&fpl->dvp->v_mount);
3462 if (!cache_fplookup_mp_supported(mp)) {
3463 cache_fpl_aborted(fpl);
3467 VNPASS(cache_fplookup_vnode_supported(fpl->dvp), fpl->dvp);
3470 error = cache_fplookup_parse(fpl);
3471 if (__predict_false(error != 0)) {
3475 if (cnp->cn_flags & ISDOTDOT) {
3476 error = cache_fpl_partial(fpl);
3480 VNPASS(cache_fplookup_vnode_supported(fpl->dvp), fpl->dvp);
3482 error = VOP_FPLOOKUP_VEXEC(fpl->dvp, cnp->cn_cred, cnp->cn_thread);
3483 if (__predict_false(error != 0)) {
3486 case EOPNOTSUPP: /* can happen when racing against vgone */
3487 cache_fpl_partial(fpl);
3491 * See the API contract for VOP_FPLOOKUP_VEXEC.
3493 if (!vn_seqc_consistent(fpl->dvp, fpl->dvp_seqc)) {
3494 error = cache_fpl_aborted(fpl);
3496 cache_fpl_smr_exit(fpl);
3497 cache_fpl_handled(fpl, error);
3504 error = cache_fplookup_next(fpl);
3505 if (__predict_false(error != 0)) {
3509 VNPASS(!seqc_in_modify(fpl->tvp_seqc), fpl->tvp);
3511 error = cache_fplookup_climb_mount(fpl);
3512 if (__predict_false(error != 0)) {
3516 VNPASS(!seqc_in_modify(fpl->tvp_seqc), fpl->tvp);
3518 if (cnp->cn_flags & ISLASTCN) {
3519 error = cache_fplookup_final(fpl);
3523 if (!vn_seqc_consistent(fpl->dvp, fpl->dvp_seqc)) {
3524 error = cache_fpl_aborted(fpl);
3528 fpl->dvp = fpl->tvp;
3529 fpl->dvp_seqc = fpl->tvp_seqc;
3531 cache_fplookup_parse_advance(fpl);
3532 cache_fpl_checkpoint(fpl, &fpl->snd);
3535 switch (fpl->status) {
3536 case CACHE_FPL_STATUS_UNSET:
3537 __assert_unreachable();
3539 case CACHE_FPL_STATUS_PARTIAL:
3540 cache_fpl_smr_assert_entered(fpl);
3541 return (cache_fplookup_partial_setup(fpl));
3542 case CACHE_FPL_STATUS_ABORTED:
3544 cache_fpl_smr_exit(fpl);
3545 return (CACHE_FPL_FAILED);
3546 case CACHE_FPL_STATUS_HANDLED:
3547 cache_fpl_smr_assert_not_entered(fpl);
3548 if (__predict_false(error != 0)) {
3551 cache_fpl_cleanup_cnp(cnp);
3554 ndp->ni_dvp = fpl->dvp;
3555 ndp->ni_vp = fpl->tvp;
3556 if (cnp->cn_flags & SAVENAME)
3557 cnp->cn_flags |= HASBUF;
3559 cache_fpl_cleanup_cnp(cnp);
3565 * Fast path lookup protected with SMR and sequence counters.
3567 * Note: all VOP_FPLOOKUP_VEXEC routines have a comment referencing this one.
3569 * Filesystems can opt in by setting the MNTK_FPLOOKUP flag and meeting criteria
3572 * Traditional vnode lookup conceptually looks like this:
3578 * vn_unlock(current);
3585 * Each jump to the next vnode is safe memory-wise and atomic with respect to
3586 * any modifications thanks to holding respective locks.
3588 * The same guarantee can be provided with a combination of safe memory
3589 * reclamation and sequence counters instead. If all operations which affect
3590 * the relationship between the current vnode and the one we are looking for
3591 * also modify the counter, we can verify whether all the conditions held as
3592 * we made the jump. This includes things like permissions, mount points etc.
3593 * Counter modification is provided by enclosing relevant places in
3594 * vn_seqc_write_begin()/end() calls.
3596 * Thus this translates to:
3599 * dvp_seqc = seqc_read_any(dvp);
3600 * if (seqc_in_modify(dvp_seqc)) // someone is altering the vnode
3604 * tvp_seqc = seqc_read_any(tvp);
3605 * if (seqc_in_modify(tvp_seqc)) // someone is altering the target vnode
3607 * if (!seqc_consistent(dvp, dvp_seqc) // someone is altering the vnode
3609 * dvp = tvp; // we know nothing of importance has changed
3610 * dvp_seqc = tvp_seqc; // store the counter for the tvp iteration
3614 * vget(); // secure the vnode
3615 * if (!seqc_consistent(tvp, tvp_seqc) // final check
3617 * // at this point we know nothing has changed for any parent<->child pair
3618 * // as they were crossed during the lookup, meaning we matched the guarantee
3619 * // of the locked variant
3622 * The API contract for VOP_FPLOOKUP_VEXEC routines is as follows:
3623 * - they are called while within vfs_smr protection which they must never exit
3624 * - EAGAIN can be returned to denote checking could not be performed, it is
3625 * always valid to return it
3626 * - if the sequence counter has not changed the result must be valid
3627 * - if the sequence counter has changed both false positives and false negatives
3628 * are permitted (since the result will be rejected later)
3629 * - for simple cases of unix permission checks vaccess_vexec_smr can be used
3631 * Caveats to watch out for:
3632 * - vnodes are passed unlocked and unreferenced with nothing stopping
3633 * VOP_RECLAIM, in turn meaning that ->v_data can become NULL. It is advised
3634 * to use atomic_load_ptr to fetch it.
3635 * - the aforementioned object can also get freed, meaning absent other means it
3636 * should be protected with vfs_smr
3637 * - either safely checking permissions as they are modified or guaranteeing
3638 * their stability is left to the routine
3641 cache_fplookup(struct nameidata *ndp, enum cache_fpl_status *status,
3644 struct cache_fpl fpl;
3647 struct componentname *cnp;
3648 struct nameidata_saved orig;
3651 *status = CACHE_FPL_STATUS_UNSET;
3652 bzero(&fpl, sizeof(fpl));
3653 fpl.status = CACHE_FPL_STATUS_UNSET;
3655 fpl.cnp = &ndp->ni_cnd;
3656 MPASS(curthread == fpl.cnp->cn_thread);
3658 if (!cache_can_fplookup(&fpl)) {
3659 SDT_PROBE3(vfs, fplookup, lookup, done, ndp, fpl.line, fpl.status);
3660 *status = fpl.status;
3661 return (EOPNOTSUPP);
3664 cache_fpl_checkpoint(&fpl, &orig);
3666 cache_fpl_smr_enter(&fpl);
3667 pwd = pwd_get_smr();
3669 ndp->ni_rootdir = pwd->pwd_rdir;
3670 ndp->ni_topdir = pwd->pwd_jdir;
3673 cnp->cn_nameptr = cnp->cn_pnbuf;
3674 if (cnp->cn_pnbuf[0] == '/') {
3675 cache_fpl_handle_root(ndp, &dvp);
3677 MPASS(ndp->ni_dirfd == AT_FDCWD);
3678 dvp = pwd->pwd_cdir;
3681 SDT_PROBE4(vfs, namei, lookup, entry, dvp, cnp->cn_pnbuf, cnp->cn_flags, true);
3683 error = cache_fplookup_impl(dvp, &fpl);
3684 cache_fpl_smr_assert_not_entered(&fpl);
3685 SDT_PROBE3(vfs, fplookup, lookup, done, ndp, fpl.line, fpl.status);
3687 *status = fpl.status;
3688 switch (fpl.status) {
3689 case CACHE_FPL_STATUS_UNSET:
3690 __assert_unreachable();
3692 case CACHE_FPL_STATUS_HANDLED:
3693 SDT_PROBE3(vfs, namei, lookup, return, error,
3694 (error == 0 ? ndp->ni_vp : NULL), true);
3696 case CACHE_FPL_STATUS_PARTIAL:
3698 cache_fpl_restore(&fpl, &fpl.snd);
3700 case CACHE_FPL_STATUS_ABORTED:
3701 cache_fpl_restore(&fpl, &orig);