xref: /freebsd/sys/kern/vfs_cache.c (revision 10328f8b112381e25e324688c8603caf4cee94ac)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993, 1995
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Poul-Henning Kamp of the FreeBSD Project.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
21  *
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
32  * SUCH DAMAGE.
33  *
34  *	@(#)vfs_cache.c	8.5 (Berkeley) 3/22/95
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include "opt_ddb.h"
41 #include "opt_ktrace.h"
42 
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>
50 #include <sys/ktr.h>
51 #include <sys/lock.h>
52 #include <sys/malloc.h>
53 #include <sys/fcntl.h>
54 #include <sys/jail.h>
55 #include <sys/mount.h>
56 #include <sys/namei.h>
57 #include <sys/proc.h>
58 #include <sys/seqc.h>
59 #include <sys/sdt.h>
60 #include <sys/smr.h>
61 #include <sys/smp.h>
62 #include <sys/syscallsubr.h>
63 #include <sys/sysctl.h>
64 #include <sys/sysproto.h>
65 #include <sys/vnode.h>
66 #include <ck_queue.h>
67 #ifdef KTRACE
68 #include <sys/ktrace.h>
69 #endif
70 #ifdef INVARIANTS
71 #include <machine/_inttypes.h>
72 #endif
73 
74 #include <sys/capsicum.h>
75 
76 #include <security/audit/audit.h>
77 #include <security/mac/mac_framework.h>
78 
79 #ifdef DDB
80 #include <ddb/ddb.h>
81 #endif
82 
83 #include <vm/uma.h>
84 
85 static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
86     "Name cache");
87 
88 SDT_PROVIDER_DECLARE(vfs);
89 SDT_PROBE_DEFINE3(vfs, namecache, enter, done, "struct vnode *", "char *",
90     "struct vnode *");
91 SDT_PROBE_DEFINE3(vfs, namecache, enter, duplicate, "struct vnode *", "char *",
92     "struct vnode *");
93 SDT_PROBE_DEFINE2(vfs, namecache, enter_negative, done, "struct vnode *",
94     "char *");
95 SDT_PROBE_DEFINE2(vfs, namecache, fullpath_smr, hit, "struct vnode *",
96     "const char *");
97 SDT_PROBE_DEFINE4(vfs, namecache, fullpath_smr, miss, "struct vnode *",
98     "struct namecache *", "int", "int");
99 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, entry, "struct vnode *");
100 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, hit, "struct vnode *",
101     "char *", "struct vnode *");
102 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, miss, "struct vnode *");
103 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, return, "int",
104     "struct vnode *", "char *");
105 SDT_PROBE_DEFINE3(vfs, namecache, lookup, hit, "struct vnode *", "char *",
106     "struct vnode *");
107 SDT_PROBE_DEFINE2(vfs, namecache, lookup, hit__negative,
108     "struct vnode *", "char *");
109 SDT_PROBE_DEFINE2(vfs, namecache, lookup, miss, "struct vnode *",
110     "char *");
111 SDT_PROBE_DEFINE2(vfs, namecache, removecnp, hit, "struct vnode *",
112     "struct componentname *");
113 SDT_PROBE_DEFINE2(vfs, namecache, removecnp, miss, "struct vnode *",
114     "struct componentname *");
115 SDT_PROBE_DEFINE1(vfs, namecache, purge, done, "struct vnode *");
116 SDT_PROBE_DEFINE1(vfs, namecache, purge, batch, "int");
117 SDT_PROBE_DEFINE1(vfs, namecache, purge_negative, done, "struct vnode *");
118 SDT_PROBE_DEFINE1(vfs, namecache, purgevfs, done, "struct mount *");
119 SDT_PROBE_DEFINE3(vfs, namecache, zap, done, "struct vnode *", "char *",
120     "struct vnode *");
121 SDT_PROBE_DEFINE2(vfs, namecache, zap_negative, done, "struct vnode *",
122     "char *");
123 SDT_PROBE_DEFINE2(vfs, namecache, evict_negative, done, "struct vnode *",
124     "char *");
125 SDT_PROBE_DEFINE1(vfs, namecache, symlink, alloc__fail, "size_t");
126 
127 SDT_PROBE_DEFINE3(vfs, fplookup, lookup, done, "struct nameidata", "int", "bool");
128 SDT_PROBE_DECLARE(vfs, namei, lookup, entry);
129 SDT_PROBE_DECLARE(vfs, namei, lookup, return);
130 
131 /*
132  * This structure describes the elements in the cache of recent
133  * names looked up by namei.
134  */
135 struct negstate {
136 	u_char neg_flag;
137 	u_char neg_hit;
138 };
139 _Static_assert(sizeof(struct negstate) <= sizeof(struct vnode *),
140     "the state must fit in a union with a pointer without growing it");
141 
142 struct	namecache {
143 	LIST_ENTRY(namecache) nc_src;	/* source vnode list */
144 	TAILQ_ENTRY(namecache) nc_dst;	/* destination vnode list */
145 	CK_SLIST_ENTRY(namecache) nc_hash;/* hash chain */
146 	struct	vnode *nc_dvp;		/* vnode of parent of name */
147 	union {
148 		struct	vnode *nu_vp;	/* vnode the name refers to */
149 		struct	negstate nu_neg;/* negative entry state */
150 	} n_un;
151 	u_char	nc_flag;		/* flag bits */
152 	u_char	nc_nlen;		/* length of name */
153 	char	nc_name[0];		/* segment name + nul */
154 };
155 
156 /*
157  * struct namecache_ts repeats struct namecache layout up to the
158  * nc_nlen member.
159  * struct namecache_ts is used in place of struct namecache when time(s) need
160  * to be stored.  The nc_dotdottime field is used when a cache entry is mapping
161  * both a non-dotdot directory name plus dotdot for the directory's
162  * parent.
163  *
164  * See below for alignment requirement.
165  */
166 struct	namecache_ts {
167 	struct	timespec nc_time;	/* timespec provided by fs */
168 	struct	timespec nc_dotdottime;	/* dotdot timespec provided by fs */
169 	int	nc_ticks;		/* ticks value when entry was added */
170 	int	nc_pad;
171 	struct namecache nc_nc;
172 };
173 
174 TAILQ_HEAD(cache_freebatch, namecache);
175 
176 /*
177  * At least mips n32 performs 64-bit accesses to timespec as found
178  * in namecache_ts and requires them to be aligned. Since others
179  * may be in the same spot suffer a little bit and enforce the
180  * alignment for everyone. Note this is a nop for 64-bit platforms.
181  */
182 #define CACHE_ZONE_ALIGNMENT	UMA_ALIGNOF(time_t)
183 
184 /*
185  * TODO: the initial value of CACHE_PATH_CUTOFF was inherited from the
186  * 4.4 BSD codebase. Later on struct namecache was tweaked to become
187  * smaller and the value was bumped to retain the total size, but it
188  * was never re-evaluated for suitability. A simple test counting
189  * lengths during package building shows that the value of 45 covers
190  * about 86% of all added entries, reaching 99% at 65.
191  *
192  * Regardless of the above, use of dedicated zones instead of malloc may be
193  * inducing additional waste. This may be hard to address as said zones are
194  * tied to VFS SMR. Even if retaining them, the current split should be
195  * re-evaluated.
196  */
197 #ifdef __LP64__
198 #define	CACHE_PATH_CUTOFF	45
199 #define	CACHE_LARGE_PAD		6
200 #else
201 #define	CACHE_PATH_CUTOFF	41
202 #define	CACHE_LARGE_PAD		2
203 #endif
204 
205 #define CACHE_ZONE_SMALL_SIZE		(offsetof(struct namecache, nc_name) + CACHE_PATH_CUTOFF + 1)
206 #define CACHE_ZONE_SMALL_TS_SIZE	(offsetof(struct namecache_ts, nc_nc) + CACHE_ZONE_SMALL_SIZE)
207 #define CACHE_ZONE_LARGE_SIZE		(offsetof(struct namecache, nc_name) + NAME_MAX + 1 + CACHE_LARGE_PAD)
208 #define CACHE_ZONE_LARGE_TS_SIZE	(offsetof(struct namecache_ts, nc_nc) + CACHE_ZONE_LARGE_SIZE)
209 
210 _Static_assert((CACHE_ZONE_SMALL_SIZE % (CACHE_ZONE_ALIGNMENT + 1)) == 0, "bad zone size");
211 _Static_assert((CACHE_ZONE_SMALL_TS_SIZE % (CACHE_ZONE_ALIGNMENT + 1)) == 0, "bad zone size");
212 _Static_assert((CACHE_ZONE_LARGE_SIZE % (CACHE_ZONE_ALIGNMENT + 1)) == 0, "bad zone size");
213 _Static_assert((CACHE_ZONE_LARGE_TS_SIZE % (CACHE_ZONE_ALIGNMENT + 1)) == 0, "bad zone size");
214 
215 #define	nc_vp		n_un.nu_vp
216 #define	nc_neg		n_un.nu_neg
217 
218 /*
219  * Flags in namecache.nc_flag
220  */
221 #define NCF_WHITE	0x01
222 #define NCF_ISDOTDOT	0x02
223 #define	NCF_TS		0x04
224 #define	NCF_DTS		0x08
225 #define	NCF_DVDROP	0x10
226 #define	NCF_NEGATIVE	0x20
227 #define	NCF_INVALID	0x40
228 #define	NCF_WIP		0x80
229 
230 /*
231  * Flags in negstate.neg_flag
232  */
233 #define NEG_HOT		0x01
234 
235 static bool	cache_neg_evict_cond(u_long lnumcache);
236 
237 /*
238  * Mark an entry as invalid.
239  *
240  * This is called before it starts getting deconstructed.
241  */
242 static void
243 cache_ncp_invalidate(struct namecache *ncp)
244 {
245 
246 	KASSERT((ncp->nc_flag & NCF_INVALID) == 0,
247 	    ("%s: entry %p already invalid", __func__, ncp));
248 	atomic_store_char(&ncp->nc_flag, ncp->nc_flag | NCF_INVALID);
249 	atomic_thread_fence_rel();
250 }
251 
252 /*
253  * Check whether the entry can be safely used.
254  *
255  * All places which elide locks are supposed to call this after they are
256  * done with reading from an entry.
257  */
258 #define cache_ncp_canuse(ncp)	({					\
259 	struct namecache *_ncp = (ncp);					\
260 	u_char _nc_flag;						\
261 									\
262 	atomic_thread_fence_acq();					\
263 	_nc_flag = atomic_load_char(&_ncp->nc_flag);			\
264 	__predict_true((_nc_flag & (NCF_INVALID | NCF_WIP)) == 0);	\
265 })
266 
267 /*
268  * Like the above but also checks NCF_WHITE.
269  */
270 #define cache_fpl_neg_ncp_canuse(ncp)	({				\
271 	struct namecache *_ncp = (ncp);					\
272 	u_char _nc_flag;						\
273 									\
274 	atomic_thread_fence_acq();					\
275 	_nc_flag = atomic_load_char(&_ncp->nc_flag);			\
276 	__predict_true((_nc_flag & (NCF_INVALID | NCF_WIP | NCF_WHITE)) == 0);	\
277 })
278 
279 /*
280  * Name caching works as follows:
281  *
282  * Names found by directory scans are retained in a cache
283  * for future reference.  It is managed LRU, so frequently
284  * used names will hang around.  Cache is indexed by hash value
285  * obtained from (dvp, name) where dvp refers to the directory
286  * containing name.
287  *
288  * If it is a "negative" entry, (i.e. for a name that is known NOT to
289  * exist) the vnode pointer will be NULL.
290  *
291  * Upon reaching the last segment of a path, if the reference
292  * is for DELETE, or NOCACHE is set (rewrite), and the
293  * name is located in the cache, it will be dropped.
294  *
295  * These locks are used (in the order in which they can be taken):
296  * NAME		TYPE	ROLE
297  * vnodelock	mtx	vnode lists and v_cache_dd field protection
298  * bucketlock	mtx	for access to given set of hash buckets
299  * neglist	mtx	negative entry LRU management
300  *
301  * It is legal to take multiple vnodelock and bucketlock locks. The locking
302  * order is lower address first. Both are recursive.
303  *
304  * "." lookups are lockless.
305  *
306  * ".." and vnode -> name lookups require vnodelock.
307  *
308  * name -> vnode lookup requires the relevant bucketlock to be held for reading.
309  *
310  * Insertions and removals of entries require involved vnodes and bucketlocks
311  * to be locked to provide safe operation against other threads modifying the
312  * cache.
313  *
314  * Some lookups result in removal of the found entry (e.g. getting rid of a
315  * negative entry with the intent to create a positive one), which poses a
316  * problem when multiple threads reach the state. Similarly, two different
317  * threads can purge two different vnodes and try to remove the same name.
318  *
319  * If the already held vnode lock is lower than the second required lock, we
320  * can just take the other lock. However, in the opposite case, this could
321  * deadlock. As such, this is resolved by trylocking and if that fails unlocking
322  * the first node, locking everything in order and revalidating the state.
323  */
324 
325 VFS_SMR_DECLARE;
326 
327 static SYSCTL_NODE(_vfs_cache, OID_AUTO, param, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
328     "Name cache parameters");
329 
330 static u_int __read_mostly	ncsize; /* the size as computed on creation or resizing */
331 SYSCTL_UINT(_vfs_cache_param, OID_AUTO, size, CTLFLAG_RW, &ncsize, 0,
332     "Total namecache capacity");
333 
334 u_int ncsizefactor = 2;
335 SYSCTL_UINT(_vfs_cache_param, OID_AUTO, sizefactor, CTLFLAG_RW, &ncsizefactor, 0,
336     "Size factor for namecache");
337 
338 static u_long __read_mostly	ncnegfactor = 5; /* ratio of negative entries */
339 SYSCTL_ULONG(_vfs_cache_param, OID_AUTO, negfactor, CTLFLAG_RW, &ncnegfactor, 0,
340     "Ratio of negative namecache entries");
341 
342 /*
343  * Negative entry % of namecache capacity above which automatic eviction is allowed.
344  *
345  * Check cache_neg_evict_cond for details.
346  */
347 static u_int ncnegminpct = 3;
348 
349 static u_int __read_mostly     neg_min; /* the above recomputed against ncsize */
350 SYSCTL_UINT(_vfs_cache_param, OID_AUTO, negmin, CTLFLAG_RD, &neg_min, 0,
351     "Negative entry count above which automatic eviction is allowed");
352 
353 /*
354  * Structures associated with name caching.
355  */
356 #define NCHHASH(hash) \
357 	(&nchashtbl[(hash) & nchash])
358 static __read_mostly CK_SLIST_HEAD(nchashhead, namecache) *nchashtbl;/* Hash Table */
359 static u_long __read_mostly	nchash;			/* size of hash table */
360 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0,
361     "Size of namecache hash table");
362 static u_long __exclusive_cache_line	numneg;	/* number of negative entries allocated */
363 static u_long __exclusive_cache_line	numcache;/* number of cache entries allocated */
364 
365 struct nchstats	nchstats;		/* cache effectiveness statistics */
366 
367 static bool __read_frequently cache_fast_revlookup = true;
368 SYSCTL_BOOL(_vfs, OID_AUTO, cache_fast_revlookup, CTLFLAG_RW,
369     &cache_fast_revlookup, 0, "");
370 
371 static bool __read_mostly cache_rename_add = true;
372 SYSCTL_BOOL(_vfs, OID_AUTO, cache_rename_add, CTLFLAG_RW,
373     &cache_rename_add, 0, "");
374 
375 static u_int __exclusive_cache_line neg_cycle;
376 
377 #define ncneghash	3
378 #define	numneglists	(ncneghash + 1)
379 
380 struct neglist {
381 	struct mtx		nl_evict_lock;
382 	struct mtx		nl_lock __aligned(CACHE_LINE_SIZE);
383 	TAILQ_HEAD(, namecache) nl_list;
384 	TAILQ_HEAD(, namecache) nl_hotlist;
385 	u_long			nl_hotnum;
386 } __aligned(CACHE_LINE_SIZE);
387 
388 static struct neglist neglists[numneglists];
389 
390 static inline struct neglist *
391 NCP2NEGLIST(struct namecache *ncp)
392 {
393 
394 	return (&neglists[(((uintptr_t)(ncp) >> 8) & ncneghash)]);
395 }
396 
397 static inline struct negstate *
398 NCP2NEGSTATE(struct namecache *ncp)
399 {
400 
401 	MPASS(atomic_load_char(&ncp->nc_flag) & NCF_NEGATIVE);
402 	return (&ncp->nc_neg);
403 }
404 
405 #define	numbucketlocks (ncbuckethash + 1)
406 static u_int __read_mostly  ncbuckethash;
407 static struct mtx_padalign __read_mostly  *bucketlocks;
408 #define	HASH2BUCKETLOCK(hash) \
409 	((struct mtx *)(&bucketlocks[((hash) & ncbuckethash)]))
410 
411 #define	numvnodelocks (ncvnodehash + 1)
412 static u_int __read_mostly  ncvnodehash;
413 static struct mtx __read_mostly *vnodelocks;
414 static inline struct mtx *
415 VP2VNODELOCK(struct vnode *vp)
416 {
417 
418 	return (&vnodelocks[(((uintptr_t)(vp) >> 8) & ncvnodehash)]);
419 }
420 
421 static void
422 cache_out_ts(struct namecache *ncp, struct timespec *tsp, int *ticksp)
423 {
424 	struct namecache_ts *ncp_ts;
425 
426 	KASSERT((ncp->nc_flag & NCF_TS) != 0 ||
427 	    (tsp == NULL && ticksp == NULL),
428 	    ("No NCF_TS"));
429 
430 	if (tsp == NULL)
431 		return;
432 
433 	ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
434 	*tsp = ncp_ts->nc_time;
435 	*ticksp = ncp_ts->nc_ticks;
436 }
437 
438 #ifdef DEBUG_CACHE
439 static int __read_mostly	doingcache = 1;	/* 1 => enable the cache */
440 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0,
441     "VFS namecache enabled");
442 #endif
443 
444 /* Export size information to userland */
445 SYSCTL_INT(_debug_sizeof, OID_AUTO, namecache, CTLFLAG_RD, SYSCTL_NULL_INT_PTR,
446     sizeof(struct namecache), "sizeof(struct namecache)");
447 
448 /*
449  * The new name cache statistics
450  */
451 static SYSCTL_NODE(_vfs_cache, OID_AUTO, stats, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
452     "Name cache statistics");
453 
454 #define STATNODE_ULONG(name, varname, descr)					\
455 	SYSCTL_ULONG(_vfs_cache_stats, OID_AUTO, name, CTLFLAG_RD, &varname, 0, descr);
456 #define STATNODE_COUNTER(name, varname, descr)					\
457 	static COUNTER_U64_DEFINE_EARLY(varname);				\
458 	SYSCTL_COUNTER_U64(_vfs_cache_stats, OID_AUTO, name, CTLFLAG_RD, &varname, \
459 	    descr);
460 STATNODE_ULONG(neg, numneg, "Number of negative cache entries");
461 STATNODE_ULONG(count, numcache, "Number of cache entries");
462 STATNODE_COUNTER(heldvnodes, numcachehv, "Number of namecache entries with vnodes held");
463 STATNODE_COUNTER(drops, numdrops, "Number of dropped entries due to reaching the limit");
464 STATNODE_COUNTER(dothits, dothits, "Number of '.' hits");
465 STATNODE_COUNTER(dotdothis, dotdothits, "Number of '..' hits");
466 STATNODE_COUNTER(miss, nummiss, "Number of cache misses");
467 STATNODE_COUNTER(misszap, nummisszap, "Number of cache misses we do not want to cache");
468 STATNODE_COUNTER(posszaps, numposzaps,
469     "Number of cache hits (positive) we do not want to cache");
470 STATNODE_COUNTER(poshits, numposhits, "Number of cache hits (positive)");
471 STATNODE_COUNTER(negzaps, numnegzaps,
472     "Number of cache hits (negative) we do not want to cache");
473 STATNODE_COUNTER(neghits, numneghits, "Number of cache hits (negative)");
474 /* These count for vn_getcwd(), too. */
475 STATNODE_COUNTER(fullpathcalls, numfullpathcalls, "Number of fullpath search calls");
476 STATNODE_COUNTER(fullpathfail1, numfullpathfail1, "Number of fullpath search errors (ENOTDIR)");
477 STATNODE_COUNTER(fullpathfail2, numfullpathfail2,
478     "Number of fullpath search errors (VOP_VPTOCNP failures)");
479 STATNODE_COUNTER(fullpathfail4, numfullpathfail4, "Number of fullpath search errors (ENOMEM)");
480 STATNODE_COUNTER(fullpathfound, numfullpathfound, "Number of successful fullpath calls");
481 STATNODE_COUNTER(symlinktoobig, symlinktoobig, "Number of times symlink did not fit the cache");
482 
483 /*
484  * Debug or developer statistics.
485  */
486 static SYSCTL_NODE(_vfs_cache, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
487     "Name cache debugging");
488 #define DEBUGNODE_ULONG(name, varname, descr)					\
489 	SYSCTL_ULONG(_vfs_cache_debug, OID_AUTO, name, CTLFLAG_RD, &varname, 0, descr);
490 #define DEBUGNODE_COUNTER(name, varname, descr)					\
491 	static COUNTER_U64_DEFINE_EARLY(varname);				\
492 	SYSCTL_COUNTER_U64(_vfs_cache_debug, OID_AUTO, name, CTLFLAG_RD, &varname, \
493 	    descr);
494 DEBUGNODE_COUNTER(zap_bucket_relock_success, zap_bucket_relock_success,
495     "Number of successful removals after relocking");
496 static long zap_bucket_fail;
497 DEBUGNODE_ULONG(zap_bucket_fail, zap_bucket_fail, "");
498 static long zap_bucket_fail2;
499 DEBUGNODE_ULONG(zap_bucket_fail2, zap_bucket_fail2, "");
500 static long cache_lock_vnodes_cel_3_failures;
501 DEBUGNODE_ULONG(vnodes_cel_3_failures, cache_lock_vnodes_cel_3_failures,
502     "Number of times 3-way vnode locking failed");
503 
504 static void cache_zap_locked(struct namecache *ncp);
505 static int vn_fullpath_hardlink(struct nameidata *ndp, char **retbuf,
506     char **freebuf, size_t *buflen);
507 static int vn_fullpath_any_smr(struct vnode *vp, struct vnode *rdir, char *buf,
508     char **retbuf, size_t *buflen, size_t addend);
509 static int vn_fullpath_any(struct vnode *vp, struct vnode *rdir, char *buf,
510     char **retbuf, size_t *buflen);
511 static int vn_fullpath_dir(struct vnode *vp, struct vnode *rdir, char *buf,
512     char **retbuf, size_t *len, size_t addend);
513 
514 static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
515 
516 static inline void
517 cache_assert_vlp_locked(struct mtx *vlp)
518 {
519 
520 	if (vlp != NULL)
521 		mtx_assert(vlp, MA_OWNED);
522 }
523 
524 static inline void
525 cache_assert_vnode_locked(struct vnode *vp)
526 {
527 	struct mtx *vlp;
528 
529 	vlp = VP2VNODELOCK(vp);
530 	cache_assert_vlp_locked(vlp);
531 }
532 
533 /*
534  * Directory vnodes with entries are held for two reasons:
535  * 1. make them less of a target for reclamation in vnlru
536  * 2. suffer smaller performance penalty in locked lookup as requeieing is avoided
537  *
538  * It will be feasible to stop doing it altogether if all filesystems start
539  * supporting lockless lookup.
540  */
541 static void
542 cache_hold_vnode(struct vnode *vp)
543 {
544 
545 	cache_assert_vnode_locked(vp);
546 	VNPASS(LIST_EMPTY(&vp->v_cache_src), vp);
547 	vhold(vp);
548 	counter_u64_add(numcachehv, 1);
549 }
550 
551 static void
552 cache_drop_vnode(struct vnode *vp)
553 {
554 
555 	/*
556 	 * Called after all locks are dropped, meaning we can't assert
557 	 * on the state of v_cache_src.
558 	 */
559 	vdrop(vp);
560 	counter_u64_add(numcachehv, -1);
561 }
562 
563 /*
564  * UMA zones.
565  */
566 static uma_zone_t __read_mostly cache_zone_small;
567 static uma_zone_t __read_mostly cache_zone_small_ts;
568 static uma_zone_t __read_mostly cache_zone_large;
569 static uma_zone_t __read_mostly cache_zone_large_ts;
570 
571 char *
572 cache_symlink_alloc(size_t size, int flags)
573 {
574 
575 	if (size < CACHE_ZONE_SMALL_SIZE) {
576 		return (uma_zalloc_smr(cache_zone_small, flags));
577 	}
578 	if (size < CACHE_ZONE_LARGE_SIZE) {
579 		return (uma_zalloc_smr(cache_zone_large, flags));
580 	}
581 	counter_u64_add(symlinktoobig, 1);
582 	SDT_PROBE1(vfs, namecache, symlink, alloc__fail, size);
583 	return (NULL);
584 }
585 
586 void
587 cache_symlink_free(char *string, size_t size)
588 {
589 
590 	MPASS(string != NULL);
591 	KASSERT(size < CACHE_ZONE_LARGE_SIZE,
592 	    ("%s: size %zu too big", __func__, size));
593 
594 	if (size < CACHE_ZONE_SMALL_SIZE) {
595 		uma_zfree_smr(cache_zone_small, string);
596 		return;
597 	}
598 	if (size < CACHE_ZONE_LARGE_SIZE) {
599 		uma_zfree_smr(cache_zone_large, string);
600 		return;
601 	}
602 	__assert_unreachable();
603 }
604 
605 static struct namecache *
606 cache_alloc_uma(int len, bool ts)
607 {
608 	struct namecache_ts *ncp_ts;
609 	struct namecache *ncp;
610 
611 	if (__predict_false(ts)) {
612 		if (len <= CACHE_PATH_CUTOFF)
613 			ncp_ts = uma_zalloc_smr(cache_zone_small_ts, M_WAITOK);
614 		else
615 			ncp_ts = uma_zalloc_smr(cache_zone_large_ts, M_WAITOK);
616 		ncp = &ncp_ts->nc_nc;
617 	} else {
618 		if (len <= CACHE_PATH_CUTOFF)
619 			ncp = uma_zalloc_smr(cache_zone_small, M_WAITOK);
620 		else
621 			ncp = uma_zalloc_smr(cache_zone_large, M_WAITOK);
622 	}
623 	return (ncp);
624 }
625 
626 static void
627 cache_free_uma(struct namecache *ncp)
628 {
629 	struct namecache_ts *ncp_ts;
630 
631 	if (__predict_false(ncp->nc_flag & NCF_TS)) {
632 		ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
633 		if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
634 			uma_zfree_smr(cache_zone_small_ts, ncp_ts);
635 		else
636 			uma_zfree_smr(cache_zone_large_ts, ncp_ts);
637 	} else {
638 		if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
639 			uma_zfree_smr(cache_zone_small, ncp);
640 		else
641 			uma_zfree_smr(cache_zone_large, ncp);
642 	}
643 }
644 
645 static struct namecache *
646 cache_alloc(int len, bool ts)
647 {
648 	u_long lnumcache;
649 
650 	/*
651 	 * Avoid blowout in namecache entries.
652 	 *
653 	 * Bugs:
654 	 * 1. filesystems may end up trying to add an already existing entry
655 	 * (for example this can happen after a cache miss during concurrent
656 	 * lookup), in which case we will call cache_neg_evict despite not
657 	 * adding anything.
658 	 * 2. the routine may fail to free anything and no provisions are made
659 	 * to make it try harder (see the inside for failure modes)
660 	 * 3. it only ever looks at negative entries.
661 	 */
662 	lnumcache = atomic_fetchadd_long(&numcache, 1) + 1;
663 	if (cache_neg_evict_cond(lnumcache)) {
664 		lnumcache = atomic_load_long(&numcache);
665 	}
666 	if (__predict_false(lnumcache >= ncsize)) {
667 		atomic_subtract_long(&numcache, 1);
668 		counter_u64_add(numdrops, 1);
669 		return (NULL);
670 	}
671 	return (cache_alloc_uma(len, ts));
672 }
673 
674 static void
675 cache_free(struct namecache *ncp)
676 {
677 
678 	MPASS(ncp != NULL);
679 	if ((ncp->nc_flag & NCF_DVDROP) != 0) {
680 		cache_drop_vnode(ncp->nc_dvp);
681 	}
682 	cache_free_uma(ncp);
683 	atomic_subtract_long(&numcache, 1);
684 }
685 
686 static void
687 cache_free_batch(struct cache_freebatch *batch)
688 {
689 	struct namecache *ncp, *nnp;
690 	int i;
691 
692 	i = 0;
693 	if (TAILQ_EMPTY(batch))
694 		goto out;
695 	TAILQ_FOREACH_SAFE(ncp, batch, nc_dst, nnp) {
696 		if ((ncp->nc_flag & NCF_DVDROP) != 0) {
697 			cache_drop_vnode(ncp->nc_dvp);
698 		}
699 		cache_free_uma(ncp);
700 		i++;
701 	}
702 	atomic_subtract_long(&numcache, i);
703 out:
704 	SDT_PROBE1(vfs, namecache, purge, batch, i);
705 }
706 
707 /*
708  * TODO: With the value stored we can do better than computing the hash based
709  * on the address. The choice of FNV should also be revisited.
710  */
711 static void
712 cache_prehash(struct vnode *vp)
713 {
714 
715 	vp->v_nchash = fnv_32_buf(&vp, sizeof(vp), FNV1_32_INIT);
716 }
717 
718 static uint32_t
719 cache_get_hash(char *name, u_char len, struct vnode *dvp)
720 {
721 
722 	return (fnv_32_buf(name, len, dvp->v_nchash));
723 }
724 
725 static inline struct nchashhead *
726 NCP2BUCKET(struct namecache *ncp)
727 {
728 	uint32_t hash;
729 
730 	hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen, ncp->nc_dvp);
731 	return (NCHHASH(hash));
732 }
733 
734 static inline struct mtx *
735 NCP2BUCKETLOCK(struct namecache *ncp)
736 {
737 	uint32_t hash;
738 
739 	hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen, ncp->nc_dvp);
740 	return (HASH2BUCKETLOCK(hash));
741 }
742 
743 #ifdef INVARIANTS
744 static void
745 cache_assert_bucket_locked(struct namecache *ncp)
746 {
747 	struct mtx *blp;
748 
749 	blp = NCP2BUCKETLOCK(ncp);
750 	mtx_assert(blp, MA_OWNED);
751 }
752 
753 static void
754 cache_assert_bucket_unlocked(struct namecache *ncp)
755 {
756 	struct mtx *blp;
757 
758 	blp = NCP2BUCKETLOCK(ncp);
759 	mtx_assert(blp, MA_NOTOWNED);
760 }
761 #else
762 #define cache_assert_bucket_locked(x) do { } while (0)
763 #define cache_assert_bucket_unlocked(x) do { } while (0)
764 #endif
765 
766 #define cache_sort_vnodes(x, y)	_cache_sort_vnodes((void **)(x), (void **)(y))
767 static void
768 _cache_sort_vnodes(void **p1, void **p2)
769 {
770 	void *tmp;
771 
772 	MPASS(*p1 != NULL || *p2 != NULL);
773 
774 	if (*p1 > *p2) {
775 		tmp = *p2;
776 		*p2 = *p1;
777 		*p1 = tmp;
778 	}
779 }
780 
781 static void
782 cache_lock_all_buckets(void)
783 {
784 	u_int i;
785 
786 	for (i = 0; i < numbucketlocks; i++)
787 		mtx_lock(&bucketlocks[i]);
788 }
789 
790 static void
791 cache_unlock_all_buckets(void)
792 {
793 	u_int i;
794 
795 	for (i = 0; i < numbucketlocks; i++)
796 		mtx_unlock(&bucketlocks[i]);
797 }
798 
799 static void
800 cache_lock_all_vnodes(void)
801 {
802 	u_int i;
803 
804 	for (i = 0; i < numvnodelocks; i++)
805 		mtx_lock(&vnodelocks[i]);
806 }
807 
808 static void
809 cache_unlock_all_vnodes(void)
810 {
811 	u_int i;
812 
813 	for (i = 0; i < numvnodelocks; i++)
814 		mtx_unlock(&vnodelocks[i]);
815 }
816 
817 static int
818 cache_trylock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
819 {
820 
821 	cache_sort_vnodes(&vlp1, &vlp2);
822 
823 	if (vlp1 != NULL) {
824 		if (!mtx_trylock(vlp1))
825 			return (EAGAIN);
826 	}
827 	if (!mtx_trylock(vlp2)) {
828 		if (vlp1 != NULL)
829 			mtx_unlock(vlp1);
830 		return (EAGAIN);
831 	}
832 
833 	return (0);
834 }
835 
836 static void
837 cache_lock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
838 {
839 
840 	MPASS(vlp1 != NULL || vlp2 != NULL);
841 	MPASS(vlp1 <= vlp2);
842 
843 	if (vlp1 != NULL)
844 		mtx_lock(vlp1);
845 	if (vlp2 != NULL)
846 		mtx_lock(vlp2);
847 }
848 
849 static void
850 cache_unlock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
851 {
852 
853 	MPASS(vlp1 != NULL || vlp2 != NULL);
854 
855 	if (vlp1 != NULL)
856 		mtx_unlock(vlp1);
857 	if (vlp2 != NULL)
858 		mtx_unlock(vlp2);
859 }
860 
861 static int
862 sysctl_nchstats(SYSCTL_HANDLER_ARGS)
863 {
864 	struct nchstats snap;
865 
866 	if (req->oldptr == NULL)
867 		return (SYSCTL_OUT(req, 0, sizeof(snap)));
868 
869 	snap = nchstats;
870 	snap.ncs_goodhits = counter_u64_fetch(numposhits);
871 	snap.ncs_neghits = counter_u64_fetch(numneghits);
872 	snap.ncs_badhits = counter_u64_fetch(numposzaps) +
873 	    counter_u64_fetch(numnegzaps);
874 	snap.ncs_miss = counter_u64_fetch(nummisszap) +
875 	    counter_u64_fetch(nummiss);
876 
877 	return (SYSCTL_OUT(req, &snap, sizeof(snap)));
878 }
879 SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE | CTLFLAG_RD |
880     CTLFLAG_MPSAFE, 0, 0, sysctl_nchstats, "LU",
881     "VFS cache effectiveness statistics");
882 
883 static void
884 cache_recalc_neg_min(u_int val)
885 {
886 
887 	neg_min = (ncsize * val) / 100;
888 }
889 
890 static int
891 sysctl_negminpct(SYSCTL_HANDLER_ARGS)
892 {
893 	u_int val;
894 	int error;
895 
896 	val = ncnegminpct;
897 	error = sysctl_handle_int(oidp, &val, 0, req);
898 	if (error != 0 || req->newptr == NULL)
899 		return (error);
900 
901 	if (val == ncnegminpct)
902 		return (0);
903 	if (val < 0 || val > 99)
904 		return (EINVAL);
905 	ncnegminpct = val;
906 	cache_recalc_neg_min(val);
907 	return (0);
908 }
909 
910 SYSCTL_PROC(_vfs_cache_param, OID_AUTO, negminpct,
911     CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, NULL, 0, sysctl_negminpct,
912     "I", "Negative entry \% of namecache capacity above which automatic eviction is allowed");
913 
914 #ifdef DIAGNOSTIC
915 /*
916  * Grab an atomic snapshot of the name cache hash chain lengths
917  */
918 static SYSCTL_NODE(_debug, OID_AUTO, hashstat,
919     CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
920     "hash table stats");
921 
922 static int
923 sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS)
924 {
925 	struct nchashhead *ncpp;
926 	struct namecache *ncp;
927 	int i, error, n_nchash, *cntbuf;
928 
929 retry:
930 	n_nchash = nchash + 1;	/* nchash is max index, not count */
931 	if (req->oldptr == NULL)
932 		return SYSCTL_OUT(req, 0, n_nchash * sizeof(int));
933 	cntbuf = malloc(n_nchash * sizeof(int), M_TEMP, M_ZERO | M_WAITOK);
934 	cache_lock_all_buckets();
935 	if (n_nchash != nchash + 1) {
936 		cache_unlock_all_buckets();
937 		free(cntbuf, M_TEMP);
938 		goto retry;
939 	}
940 	/* Scan hash tables counting entries */
941 	for (ncpp = nchashtbl, i = 0; i < n_nchash; ncpp++, i++)
942 		CK_SLIST_FOREACH(ncp, ncpp, nc_hash)
943 			cntbuf[i]++;
944 	cache_unlock_all_buckets();
945 	for (error = 0, i = 0; i < n_nchash; i++)
946 		if ((error = SYSCTL_OUT(req, &cntbuf[i], sizeof(int))) != 0)
947 			break;
948 	free(cntbuf, M_TEMP);
949 	return (error);
950 }
951 SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD|
952     CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_rawnchash, "S,int",
953     "nchash chain lengths");
954 
955 static int
956 sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS)
957 {
958 	int error;
959 	struct nchashhead *ncpp;
960 	struct namecache *ncp;
961 	int n_nchash;
962 	int count, maxlength, used, pct;
963 
964 	if (!req->oldptr)
965 		return SYSCTL_OUT(req, 0, 4 * sizeof(int));
966 
967 	cache_lock_all_buckets();
968 	n_nchash = nchash + 1;	/* nchash is max index, not count */
969 	used = 0;
970 	maxlength = 0;
971 
972 	/* Scan hash tables for applicable entries */
973 	for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
974 		count = 0;
975 		CK_SLIST_FOREACH(ncp, ncpp, nc_hash) {
976 			count++;
977 		}
978 		if (count)
979 			used++;
980 		if (maxlength < count)
981 			maxlength = count;
982 	}
983 	n_nchash = nchash + 1;
984 	cache_unlock_all_buckets();
985 	pct = (used * 100) / (n_nchash / 100);
986 	error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash));
987 	if (error)
988 		return (error);
989 	error = SYSCTL_OUT(req, &used, sizeof(used));
990 	if (error)
991 		return (error);
992 	error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength));
993 	if (error)
994 		return (error);
995 	error = SYSCTL_OUT(req, &pct, sizeof(pct));
996 	if (error)
997 		return (error);
998 	return (0);
999 }
1000 SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD|
1001     CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_nchash, "I",
1002     "nchash statistics (number of total/used buckets, maximum chain length, usage percentage)");
1003 #endif
1004 
1005 /*
1006  * Negative entries management
1007  *
1008  * Various workloads create plenty of negative entries and barely use them
1009  * afterwards. Moreover malicious users can keep performing bogus lookups
1010  * adding even more entries. For example "make tinderbox" as of writing this
1011  * comment ends up with 2.6M namecache entries in total, 1.2M of which are
1012  * negative.
1013  *
1014  * As such, a rather aggressive eviction method is needed. The currently
1015  * employed method is a placeholder.
1016  *
1017  * Entries are split over numneglists separate lists, each of which is further
1018  * split into hot and cold entries. Entries get promoted after getting a hit.
1019  * Eviction happens on addition of new entry.
1020  */
1021 static SYSCTL_NODE(_vfs_cache, OID_AUTO, neg, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1022     "Name cache negative entry statistics");
1023 
1024 SYSCTL_ULONG(_vfs_cache_neg, OID_AUTO, count, CTLFLAG_RD, &numneg, 0,
1025     "Number of negative cache entries");
1026 
1027 static COUNTER_U64_DEFINE_EARLY(neg_created);
1028 SYSCTL_COUNTER_U64(_vfs_cache_neg, OID_AUTO, created, CTLFLAG_RD, &neg_created,
1029     "Number of created negative entries");
1030 
1031 static COUNTER_U64_DEFINE_EARLY(neg_evicted);
1032 SYSCTL_COUNTER_U64(_vfs_cache_neg, OID_AUTO, evicted, CTLFLAG_RD, &neg_evicted,
1033     "Number of evicted negative entries");
1034 
1035 static COUNTER_U64_DEFINE_EARLY(neg_evict_skipped_empty);
1036 SYSCTL_COUNTER_U64(_vfs_cache_neg, OID_AUTO, evict_skipped_empty, CTLFLAG_RD,
1037     &neg_evict_skipped_empty,
1038     "Number of times evicting failed due to lack of entries");
1039 
1040 static COUNTER_U64_DEFINE_EARLY(neg_evict_skipped_missed);
1041 SYSCTL_COUNTER_U64(_vfs_cache_neg, OID_AUTO, evict_skipped_missed, CTLFLAG_RD,
1042     &neg_evict_skipped_missed,
1043     "Number of times evicting failed due to target entry disappearing");
1044 
1045 static COUNTER_U64_DEFINE_EARLY(neg_evict_skipped_contended);
1046 SYSCTL_COUNTER_U64(_vfs_cache_neg, OID_AUTO, evict_skipped_contended, CTLFLAG_RD,
1047     &neg_evict_skipped_contended,
1048     "Number of times evicting failed due to contention");
1049 
1050 SYSCTL_COUNTER_U64(_vfs_cache_neg, OID_AUTO, hits, CTLFLAG_RD, &numneghits,
1051     "Number of cache hits (negative)");
1052 
1053 static int
1054 sysctl_neg_hot(SYSCTL_HANDLER_ARGS)
1055 {
1056 	int i, out;
1057 
1058 	out = 0;
1059 	for (i = 0; i < numneglists; i++)
1060 		out += neglists[i].nl_hotnum;
1061 
1062 	return (SYSCTL_OUT(req, &out, sizeof(out)));
1063 }
1064 SYSCTL_PROC(_vfs_cache_neg, OID_AUTO, hot, CTLTYPE_INT | CTLFLAG_RD |
1065     CTLFLAG_MPSAFE, 0, 0, sysctl_neg_hot, "I",
1066     "Number of hot negative entries");
1067 
1068 static void
1069 cache_neg_init(struct namecache *ncp)
1070 {
1071 	struct negstate *ns;
1072 
1073 	ncp->nc_flag |= NCF_NEGATIVE;
1074 	ns = NCP2NEGSTATE(ncp);
1075 	ns->neg_flag = 0;
1076 	ns->neg_hit = 0;
1077 	counter_u64_add(neg_created, 1);
1078 }
1079 
1080 #define CACHE_NEG_PROMOTION_THRESH 2
1081 
1082 static bool
1083 cache_neg_hit_prep(struct namecache *ncp)
1084 {
1085 	struct negstate *ns;
1086 	u_char n;
1087 
1088 	ns = NCP2NEGSTATE(ncp);
1089 	n = atomic_load_char(&ns->neg_hit);
1090 	for (;;) {
1091 		if (n >= CACHE_NEG_PROMOTION_THRESH)
1092 			return (false);
1093 		if (atomic_fcmpset_8(&ns->neg_hit, &n, n + 1))
1094 			break;
1095 	}
1096 	return (n + 1 == CACHE_NEG_PROMOTION_THRESH);
1097 }
1098 
1099 /*
1100  * Nothing to do here but it is provided for completeness as some
1101  * cache_neg_hit_prep callers may end up returning without even
1102  * trying to promote.
1103  */
1104 #define cache_neg_hit_abort(ncp)	do { } while (0)
1105 
1106 static void
1107 cache_neg_hit_finish(struct namecache *ncp)
1108 {
1109 
1110 	SDT_PROBE2(vfs, namecache, lookup, hit__negative, ncp->nc_dvp, ncp->nc_name);
1111 	counter_u64_add(numneghits, 1);
1112 }
1113 
1114 /*
1115  * Move a negative entry to the hot list.
1116  */
1117 static void
1118 cache_neg_promote_locked(struct namecache *ncp)
1119 {
1120 	struct neglist *nl;
1121 	struct negstate *ns;
1122 
1123 	ns = NCP2NEGSTATE(ncp);
1124 	nl = NCP2NEGLIST(ncp);
1125 	mtx_assert(&nl->nl_lock, MA_OWNED);
1126 	if ((ns->neg_flag & NEG_HOT) == 0) {
1127 		TAILQ_REMOVE(&nl->nl_list, ncp, nc_dst);
1128 		TAILQ_INSERT_TAIL(&nl->nl_hotlist, ncp, nc_dst);
1129 		nl->nl_hotnum++;
1130 		ns->neg_flag |= NEG_HOT;
1131 	}
1132 }
1133 
1134 /*
1135  * Move a hot negative entry to the cold list.
1136  */
1137 static void
1138 cache_neg_demote_locked(struct namecache *ncp)
1139 {
1140 	struct neglist *nl;
1141 	struct negstate *ns;
1142 
1143 	ns = NCP2NEGSTATE(ncp);
1144 	nl = NCP2NEGLIST(ncp);
1145 	mtx_assert(&nl->nl_lock, MA_OWNED);
1146 	MPASS(ns->neg_flag & NEG_HOT);
1147 	TAILQ_REMOVE(&nl->nl_hotlist, ncp, nc_dst);
1148 	TAILQ_INSERT_TAIL(&nl->nl_list, ncp, nc_dst);
1149 	nl->nl_hotnum--;
1150 	ns->neg_flag &= ~NEG_HOT;
1151 	atomic_store_char(&ns->neg_hit, 0);
1152 }
1153 
1154 /*
1155  * Move a negative entry to the hot list if it matches the lookup.
1156  *
1157  * We have to take locks, but they may be contended and in the worst
1158  * case we may need to go off CPU. We don't want to spin within the
1159  * smr section and we can't block with it. Exiting the section means
1160  * the found entry could have been evicted. We are going to look it
1161  * up again.
1162  */
1163 static bool
1164 cache_neg_promote_cond(struct vnode *dvp, struct componentname *cnp,
1165     struct namecache *oncp, uint32_t hash)
1166 {
1167 	struct namecache *ncp;
1168 	struct neglist *nl;
1169 	u_char nc_flag;
1170 
1171 	nl = NCP2NEGLIST(oncp);
1172 
1173 	mtx_lock(&nl->nl_lock);
1174 	/*
1175 	 * For hash iteration.
1176 	 */
1177 	vfs_smr_enter();
1178 
1179 	/*
1180 	 * Avoid all surprises by only succeeding if we got the same entry and
1181 	 * bailing completely otherwise.
1182 	 * XXX There are no provisions to keep the vnode around, meaning we may
1183 	 * end up promoting a negative entry for a *new* vnode and returning
1184 	 * ENOENT on its account. This is the error we want to return anyway
1185 	 * and promotion is harmless.
1186 	 *
1187 	 * In particular at this point there can be a new ncp which matches the
1188 	 * search but hashes to a different neglist.
1189 	 */
1190 	CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1191 		if (ncp == oncp)
1192 			break;
1193 	}
1194 
1195 	/*
1196 	 * No match to begin with.
1197 	 */
1198 	if (__predict_false(ncp == NULL)) {
1199 		goto out_abort;
1200 	}
1201 
1202 	/*
1203 	 * The newly found entry may be something different...
1204 	 */
1205 	if (!(ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1206 	    !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))) {
1207 		goto out_abort;
1208 	}
1209 
1210 	/*
1211 	 * ... and not even negative.
1212 	 */
1213 	nc_flag = atomic_load_char(&ncp->nc_flag);
1214 	if ((nc_flag & NCF_NEGATIVE) == 0) {
1215 		goto out_abort;
1216 	}
1217 
1218 	if (!cache_ncp_canuse(ncp)) {
1219 		goto out_abort;
1220 	}
1221 
1222 	cache_neg_promote_locked(ncp);
1223 	cache_neg_hit_finish(ncp);
1224 	vfs_smr_exit();
1225 	mtx_unlock(&nl->nl_lock);
1226 	return (true);
1227 out_abort:
1228 	vfs_smr_exit();
1229 	mtx_unlock(&nl->nl_lock);
1230 	return (false);
1231 }
1232 
1233 static void
1234 cache_neg_promote(struct namecache *ncp)
1235 {
1236 	struct neglist *nl;
1237 
1238 	nl = NCP2NEGLIST(ncp);
1239 	mtx_lock(&nl->nl_lock);
1240 	cache_neg_promote_locked(ncp);
1241 	mtx_unlock(&nl->nl_lock);
1242 }
1243 
1244 static void
1245 cache_neg_insert(struct namecache *ncp)
1246 {
1247 	struct neglist *nl;
1248 
1249 	MPASS(ncp->nc_flag & NCF_NEGATIVE);
1250 	cache_assert_bucket_locked(ncp);
1251 	nl = NCP2NEGLIST(ncp);
1252 	mtx_lock(&nl->nl_lock);
1253 	TAILQ_INSERT_TAIL(&nl->nl_list, ncp, nc_dst);
1254 	mtx_unlock(&nl->nl_lock);
1255 	atomic_add_long(&numneg, 1);
1256 }
1257 
1258 static void
1259 cache_neg_remove(struct namecache *ncp)
1260 {
1261 	struct neglist *nl;
1262 	struct negstate *ns;
1263 
1264 	cache_assert_bucket_locked(ncp);
1265 	nl = NCP2NEGLIST(ncp);
1266 	ns = NCP2NEGSTATE(ncp);
1267 	mtx_lock(&nl->nl_lock);
1268 	if ((ns->neg_flag & NEG_HOT) != 0) {
1269 		TAILQ_REMOVE(&nl->nl_hotlist, ncp, nc_dst);
1270 		nl->nl_hotnum--;
1271 	} else {
1272 		TAILQ_REMOVE(&nl->nl_list, ncp, nc_dst);
1273 	}
1274 	mtx_unlock(&nl->nl_lock);
1275 	atomic_subtract_long(&numneg, 1);
1276 }
1277 
1278 static struct neglist *
1279 cache_neg_evict_select_list(void)
1280 {
1281 	struct neglist *nl;
1282 	u_int c;
1283 
1284 	c = atomic_fetchadd_int(&neg_cycle, 1) + 1;
1285 	nl = &neglists[c % numneglists];
1286 	if (!mtx_trylock(&nl->nl_evict_lock)) {
1287 		counter_u64_add(neg_evict_skipped_contended, 1);
1288 		return (NULL);
1289 	}
1290 	return (nl);
1291 }
1292 
1293 static struct namecache *
1294 cache_neg_evict_select_entry(struct neglist *nl)
1295 {
1296 	struct namecache *ncp, *lncp;
1297 	struct negstate *ns, *lns;
1298 	int i;
1299 
1300 	mtx_assert(&nl->nl_evict_lock, MA_OWNED);
1301 	mtx_assert(&nl->nl_lock, MA_OWNED);
1302 	ncp = TAILQ_FIRST(&nl->nl_list);
1303 	if (ncp == NULL)
1304 		return (NULL);
1305 	lncp = ncp;
1306 	lns = NCP2NEGSTATE(lncp);
1307 	for (i = 1; i < 4; i++) {
1308 		ncp = TAILQ_NEXT(ncp, nc_dst);
1309 		if (ncp == NULL)
1310 			break;
1311 		ns = NCP2NEGSTATE(ncp);
1312 		if (ns->neg_hit < lns->neg_hit) {
1313 			lncp = ncp;
1314 			lns = ns;
1315 		}
1316 	}
1317 	return (lncp);
1318 }
1319 
1320 static bool
1321 cache_neg_evict(void)
1322 {
1323 	struct namecache *ncp, *ncp2;
1324 	struct neglist *nl;
1325 	struct vnode *dvp;
1326 	struct mtx *dvlp;
1327 	struct mtx *blp;
1328 	uint32_t hash;
1329 	u_char nlen;
1330 	bool evicted;
1331 
1332 	nl = cache_neg_evict_select_list();
1333 	if (nl == NULL) {
1334 		return (false);
1335 	}
1336 
1337 	mtx_lock(&nl->nl_lock);
1338 	ncp = TAILQ_FIRST(&nl->nl_hotlist);
1339 	if (ncp != NULL) {
1340 		cache_neg_demote_locked(ncp);
1341 	}
1342 	ncp = cache_neg_evict_select_entry(nl);
1343 	if (ncp == NULL) {
1344 		counter_u64_add(neg_evict_skipped_empty, 1);
1345 		mtx_unlock(&nl->nl_lock);
1346 		mtx_unlock(&nl->nl_evict_lock);
1347 		return (false);
1348 	}
1349 	nlen = ncp->nc_nlen;
1350 	dvp = ncp->nc_dvp;
1351 	hash = cache_get_hash(ncp->nc_name, nlen, dvp);
1352 	dvlp = VP2VNODELOCK(dvp);
1353 	blp = HASH2BUCKETLOCK(hash);
1354 	mtx_unlock(&nl->nl_lock);
1355 	mtx_unlock(&nl->nl_evict_lock);
1356 	mtx_lock(dvlp);
1357 	mtx_lock(blp);
1358 	/*
1359 	 * Note that since all locks were dropped above, the entry may be
1360 	 * gone or reallocated to be something else.
1361 	 */
1362 	CK_SLIST_FOREACH(ncp2, (NCHHASH(hash)), nc_hash) {
1363 		if (ncp2 == ncp && ncp2->nc_dvp == dvp &&
1364 		    ncp2->nc_nlen == nlen && (ncp2->nc_flag & NCF_NEGATIVE) != 0)
1365 			break;
1366 	}
1367 	if (ncp2 == NULL) {
1368 		counter_u64_add(neg_evict_skipped_missed, 1);
1369 		ncp = NULL;
1370 		evicted = false;
1371 	} else {
1372 		MPASS(dvlp == VP2VNODELOCK(ncp->nc_dvp));
1373 		MPASS(blp == NCP2BUCKETLOCK(ncp));
1374 		SDT_PROBE2(vfs, namecache, evict_negative, done, ncp->nc_dvp,
1375 		    ncp->nc_name);
1376 		cache_zap_locked(ncp);
1377 		counter_u64_add(neg_evicted, 1);
1378 		evicted = true;
1379 	}
1380 	mtx_unlock(blp);
1381 	mtx_unlock(dvlp);
1382 	if (ncp != NULL)
1383 		cache_free(ncp);
1384 	return (evicted);
1385 }
1386 
1387 /*
1388  * Maybe evict a negative entry to create more room.
1389  *
1390  * The ncnegfactor parameter limits what fraction of the total count
1391  * can comprise of negative entries. However, if the cache is just
1392  * warming up this leads to excessive evictions.  As such, ncnegminpct
1393  * (recomputed to neg_min) dictates whether the above should be
1394  * applied.
1395  *
1396  * Try evicting if the cache is close to full capacity regardless of
1397  * other considerations.
1398  */
1399 static bool
1400 cache_neg_evict_cond(u_long lnumcache)
1401 {
1402 	u_long lnumneg;
1403 
1404 	if (ncsize - 1000 < lnumcache)
1405 		goto out_evict;
1406 	lnumneg = atomic_load_long(&numneg);
1407 	if (lnumneg < neg_min)
1408 		return (false);
1409 	if (lnumneg * ncnegfactor < lnumcache)
1410 		return (false);
1411 out_evict:
1412 	return (cache_neg_evict());
1413 }
1414 
1415 /*
1416  * cache_zap_locked():
1417  *
1418  *   Removes a namecache entry from cache, whether it contains an actual
1419  *   pointer to a vnode or if it is just a negative cache entry.
1420  */
1421 static void
1422 cache_zap_locked(struct namecache *ncp)
1423 {
1424 	struct nchashhead *ncpp;
1425 	struct vnode *dvp, *vp;
1426 
1427 	dvp = ncp->nc_dvp;
1428 	vp = ncp->nc_vp;
1429 
1430 	if (!(ncp->nc_flag & NCF_NEGATIVE))
1431 		cache_assert_vnode_locked(vp);
1432 	cache_assert_vnode_locked(dvp);
1433 	cache_assert_bucket_locked(ncp);
1434 
1435 	cache_ncp_invalidate(ncp);
1436 
1437 	ncpp = NCP2BUCKET(ncp);
1438 	CK_SLIST_REMOVE(ncpp, ncp, namecache, nc_hash);
1439 	if (!(ncp->nc_flag & NCF_NEGATIVE)) {
1440 		SDT_PROBE3(vfs, namecache, zap, done, dvp, ncp->nc_name, vp);
1441 		TAILQ_REMOVE(&vp->v_cache_dst, ncp, nc_dst);
1442 		if (ncp == vp->v_cache_dd) {
1443 			atomic_store_ptr(&vp->v_cache_dd, NULL);
1444 		}
1445 	} else {
1446 		SDT_PROBE2(vfs, namecache, zap_negative, done, dvp, ncp->nc_name);
1447 		cache_neg_remove(ncp);
1448 	}
1449 	if (ncp->nc_flag & NCF_ISDOTDOT) {
1450 		if (ncp == dvp->v_cache_dd) {
1451 			atomic_store_ptr(&dvp->v_cache_dd, NULL);
1452 		}
1453 	} else {
1454 		LIST_REMOVE(ncp, nc_src);
1455 		if (LIST_EMPTY(&dvp->v_cache_src)) {
1456 			ncp->nc_flag |= NCF_DVDROP;
1457 		}
1458 	}
1459 }
1460 
1461 static void
1462 cache_zap_negative_locked_vnode_kl(struct namecache *ncp, struct vnode *vp)
1463 {
1464 	struct mtx *blp;
1465 
1466 	MPASS(ncp->nc_dvp == vp);
1467 	MPASS(ncp->nc_flag & NCF_NEGATIVE);
1468 	cache_assert_vnode_locked(vp);
1469 
1470 	blp = NCP2BUCKETLOCK(ncp);
1471 	mtx_lock(blp);
1472 	cache_zap_locked(ncp);
1473 	mtx_unlock(blp);
1474 }
1475 
1476 static bool
1477 cache_zap_locked_vnode_kl2(struct namecache *ncp, struct vnode *vp,
1478     struct mtx **vlpp)
1479 {
1480 	struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
1481 	struct mtx *blp;
1482 
1483 	MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
1484 	cache_assert_vnode_locked(vp);
1485 
1486 	if (ncp->nc_flag & NCF_NEGATIVE) {
1487 		if (*vlpp != NULL) {
1488 			mtx_unlock(*vlpp);
1489 			*vlpp = NULL;
1490 		}
1491 		cache_zap_negative_locked_vnode_kl(ncp, vp);
1492 		return (true);
1493 	}
1494 
1495 	pvlp = VP2VNODELOCK(vp);
1496 	blp = NCP2BUCKETLOCK(ncp);
1497 	vlp1 = VP2VNODELOCK(ncp->nc_dvp);
1498 	vlp2 = VP2VNODELOCK(ncp->nc_vp);
1499 
1500 	if (*vlpp == vlp1 || *vlpp == vlp2) {
1501 		to_unlock = *vlpp;
1502 		*vlpp = NULL;
1503 	} else {
1504 		if (*vlpp != NULL) {
1505 			mtx_unlock(*vlpp);
1506 			*vlpp = NULL;
1507 		}
1508 		cache_sort_vnodes(&vlp1, &vlp2);
1509 		if (vlp1 == pvlp) {
1510 			mtx_lock(vlp2);
1511 			to_unlock = vlp2;
1512 		} else {
1513 			if (!mtx_trylock(vlp1))
1514 				goto out_relock;
1515 			to_unlock = vlp1;
1516 		}
1517 	}
1518 	mtx_lock(blp);
1519 	cache_zap_locked(ncp);
1520 	mtx_unlock(blp);
1521 	if (to_unlock != NULL)
1522 		mtx_unlock(to_unlock);
1523 	return (true);
1524 
1525 out_relock:
1526 	mtx_unlock(vlp2);
1527 	mtx_lock(vlp1);
1528 	mtx_lock(vlp2);
1529 	MPASS(*vlpp == NULL);
1530 	*vlpp = vlp1;
1531 	return (false);
1532 }
1533 
1534 /*
1535  * If trylocking failed we can get here. We know enough to take all needed locks
1536  * in the right order and re-lookup the entry.
1537  */
1538 static int
1539 cache_zap_unlocked_bucket(struct namecache *ncp, struct componentname *cnp,
1540     struct vnode *dvp, struct mtx *dvlp, struct mtx *vlp, uint32_t hash,
1541     struct mtx *blp)
1542 {
1543 	struct namecache *rncp;
1544 
1545 	cache_assert_bucket_unlocked(ncp);
1546 
1547 	cache_sort_vnodes(&dvlp, &vlp);
1548 	cache_lock_vnodes(dvlp, vlp);
1549 	mtx_lock(blp);
1550 	CK_SLIST_FOREACH(rncp, (NCHHASH(hash)), nc_hash) {
1551 		if (rncp == ncp && rncp->nc_dvp == dvp &&
1552 		    rncp->nc_nlen == cnp->cn_namelen &&
1553 		    !bcmp(rncp->nc_name, cnp->cn_nameptr, rncp->nc_nlen))
1554 			break;
1555 	}
1556 	if (rncp != NULL) {
1557 		cache_zap_locked(rncp);
1558 		mtx_unlock(blp);
1559 		cache_unlock_vnodes(dvlp, vlp);
1560 		counter_u64_add(zap_bucket_relock_success, 1);
1561 		return (0);
1562 	}
1563 
1564 	mtx_unlock(blp);
1565 	cache_unlock_vnodes(dvlp, vlp);
1566 	return (EAGAIN);
1567 }
1568 
1569 static int __noinline
1570 cache_zap_locked_bucket(struct namecache *ncp, struct componentname *cnp,
1571     uint32_t hash, struct mtx *blp)
1572 {
1573 	struct mtx *dvlp, *vlp;
1574 	struct vnode *dvp;
1575 
1576 	cache_assert_bucket_locked(ncp);
1577 
1578 	dvlp = VP2VNODELOCK(ncp->nc_dvp);
1579 	vlp = NULL;
1580 	if (!(ncp->nc_flag & NCF_NEGATIVE))
1581 		vlp = VP2VNODELOCK(ncp->nc_vp);
1582 	if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1583 		cache_zap_locked(ncp);
1584 		mtx_unlock(blp);
1585 		cache_unlock_vnodes(dvlp, vlp);
1586 		return (0);
1587 	}
1588 
1589 	dvp = ncp->nc_dvp;
1590 	mtx_unlock(blp);
1591 	return (cache_zap_unlocked_bucket(ncp, cnp, dvp, dvlp, vlp, hash, blp));
1592 }
1593 
1594 static __noinline int
1595 cache_remove_cnp(struct vnode *dvp, struct componentname *cnp)
1596 {
1597 	struct namecache *ncp;
1598 	struct mtx *blp;
1599 	struct mtx *dvlp, *dvlp2;
1600 	uint32_t hash;
1601 	int error;
1602 
1603 	if (cnp->cn_namelen == 2 &&
1604 	    cnp->cn_nameptr[0] == '.' && cnp->cn_nameptr[1] == '.') {
1605 		dvlp = VP2VNODELOCK(dvp);
1606 		dvlp2 = NULL;
1607 		mtx_lock(dvlp);
1608 retry_dotdot:
1609 		ncp = dvp->v_cache_dd;
1610 		if (ncp == NULL) {
1611 			mtx_unlock(dvlp);
1612 			if (dvlp2 != NULL)
1613 				mtx_unlock(dvlp2);
1614 			SDT_PROBE2(vfs, namecache, removecnp, miss, dvp, cnp);
1615 			return (0);
1616 		}
1617 		if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1618 			if (!cache_zap_locked_vnode_kl2(ncp, dvp, &dvlp2))
1619 				goto retry_dotdot;
1620 			MPASS(dvp->v_cache_dd == NULL);
1621 			mtx_unlock(dvlp);
1622 			if (dvlp2 != NULL)
1623 				mtx_unlock(dvlp2);
1624 			cache_free(ncp);
1625 		} else {
1626 			atomic_store_ptr(&dvp->v_cache_dd, NULL);
1627 			mtx_unlock(dvlp);
1628 			if (dvlp2 != NULL)
1629 				mtx_unlock(dvlp2);
1630 		}
1631 		SDT_PROBE2(vfs, namecache, removecnp, hit, dvp, cnp);
1632 		return (1);
1633 	}
1634 
1635 	hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1636 	blp = HASH2BUCKETLOCK(hash);
1637 retry:
1638 	if (CK_SLIST_EMPTY(NCHHASH(hash)))
1639 		goto out_no_entry;
1640 
1641 	mtx_lock(blp);
1642 
1643 	CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1644 		if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1645 		    !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1646 			break;
1647 	}
1648 
1649 	if (ncp == NULL) {
1650 		mtx_unlock(blp);
1651 		goto out_no_entry;
1652 	}
1653 
1654 	error = cache_zap_locked_bucket(ncp, cnp, hash, blp);
1655 	if (__predict_false(error != 0)) {
1656 		zap_bucket_fail++;
1657 		goto retry;
1658 	}
1659 	counter_u64_add(numposzaps, 1);
1660 	SDT_PROBE2(vfs, namecache, removecnp, hit, dvp, cnp);
1661 	cache_free(ncp);
1662 	return (1);
1663 out_no_entry:
1664 	counter_u64_add(nummisszap, 1);
1665 	SDT_PROBE2(vfs, namecache, removecnp, miss, dvp, cnp);
1666 	return (0);
1667 }
1668 
1669 static int __noinline
1670 cache_lookup_dot(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1671     struct timespec *tsp, int *ticksp)
1672 {
1673 	int ltype;
1674 
1675 	*vpp = dvp;
1676 	counter_u64_add(dothits, 1);
1677 	SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", *vpp);
1678 	if (tsp != NULL)
1679 		timespecclear(tsp);
1680 	if (ticksp != NULL)
1681 		*ticksp = ticks;
1682 	vrefact(*vpp);
1683 	/*
1684 	 * When we lookup "." we still can be asked to lock it
1685 	 * differently...
1686 	 */
1687 	ltype = cnp->cn_lkflags & LK_TYPE_MASK;
1688 	if (ltype != VOP_ISLOCKED(*vpp)) {
1689 		if (ltype == LK_EXCLUSIVE) {
1690 			vn_lock(*vpp, LK_UPGRADE | LK_RETRY);
1691 			if (VN_IS_DOOMED((*vpp))) {
1692 				/* forced unmount */
1693 				vrele(*vpp);
1694 				*vpp = NULL;
1695 				return (ENOENT);
1696 			}
1697 		} else
1698 			vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY);
1699 	}
1700 	return (-1);
1701 }
1702 
1703 static int __noinline
1704 cache_lookup_dotdot(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1705     struct timespec *tsp, int *ticksp)
1706 {
1707 	struct namecache_ts *ncp_ts;
1708 	struct namecache *ncp;
1709 	struct mtx *dvlp;
1710 	enum vgetstate vs;
1711 	int error, ltype;
1712 	bool whiteout;
1713 
1714 	MPASS((cnp->cn_flags & ISDOTDOT) != 0);
1715 
1716 	if ((cnp->cn_flags & MAKEENTRY) == 0) {
1717 		cache_remove_cnp(dvp, cnp);
1718 		return (0);
1719 	}
1720 
1721 	counter_u64_add(dotdothits, 1);
1722 retry:
1723 	dvlp = VP2VNODELOCK(dvp);
1724 	mtx_lock(dvlp);
1725 	ncp = dvp->v_cache_dd;
1726 	if (ncp == NULL) {
1727 		SDT_PROBE3(vfs, namecache, lookup, miss, dvp, "..", NULL);
1728 		mtx_unlock(dvlp);
1729 		return (0);
1730 	}
1731 	if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1732 		if (ncp->nc_flag & NCF_NEGATIVE)
1733 			*vpp = NULL;
1734 		else
1735 			*vpp = ncp->nc_vp;
1736 	} else
1737 		*vpp = ncp->nc_dvp;
1738 	if (*vpp == NULL)
1739 		goto negative_success;
1740 	SDT_PROBE3(vfs, namecache, lookup, hit, dvp, "..", *vpp);
1741 	cache_out_ts(ncp, tsp, ticksp);
1742 	if ((ncp->nc_flag & (NCF_ISDOTDOT | NCF_DTS)) ==
1743 	    NCF_DTS && tsp != NULL) {
1744 		ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1745 		*tsp = ncp_ts->nc_dotdottime;
1746 	}
1747 
1748 	MPASS(dvp != *vpp);
1749 	ltype = VOP_ISLOCKED(dvp);
1750 	VOP_UNLOCK(dvp);
1751 	vs = vget_prep(*vpp);
1752 	mtx_unlock(dvlp);
1753 	error = vget_finish(*vpp, cnp->cn_lkflags, vs);
1754 	vn_lock(dvp, ltype | LK_RETRY);
1755 	if (VN_IS_DOOMED(dvp)) {
1756 		if (error == 0)
1757 			vput(*vpp);
1758 		*vpp = NULL;
1759 		return (ENOENT);
1760 	}
1761 	if (error) {
1762 		*vpp = NULL;
1763 		goto retry;
1764 	}
1765 	return (-1);
1766 negative_success:
1767 	if (__predict_false(cnp->cn_nameiop == CREATE)) {
1768 		if (cnp->cn_flags & ISLASTCN) {
1769 			counter_u64_add(numnegzaps, 1);
1770 			cache_zap_negative_locked_vnode_kl(ncp, dvp);
1771 			mtx_unlock(dvlp);
1772 			cache_free(ncp);
1773 			return (0);
1774 		}
1775 	}
1776 
1777 	whiteout = (ncp->nc_flag & NCF_WHITE);
1778 	cache_out_ts(ncp, tsp, ticksp);
1779 	if (cache_neg_hit_prep(ncp))
1780 		cache_neg_promote(ncp);
1781 	else
1782 		cache_neg_hit_finish(ncp);
1783 	mtx_unlock(dvlp);
1784 	if (whiteout)
1785 		cnp->cn_flags |= ISWHITEOUT;
1786 	return (ENOENT);
1787 }
1788 
1789 /**
1790  * Lookup a name in the name cache
1791  *
1792  * # Arguments
1793  *
1794  * - dvp:	Parent directory in which to search.
1795  * - vpp:	Return argument.  Will contain desired vnode on cache hit.
1796  * - cnp:	Parameters of the name search.  The most interesting bits of
1797  *   		the cn_flags field have the following meanings:
1798  *   	- MAKEENTRY:	If clear, free an entry from the cache rather than look
1799  *   			it up.
1800  *   	- ISDOTDOT:	Must be set if and only if cn_nameptr == ".."
1801  * - tsp:	Return storage for cache timestamp.  On a successful (positive
1802  *   		or negative) lookup, tsp will be filled with any timespec that
1803  *   		was stored when this cache entry was created.  However, it will
1804  *   		be clear for "." entries.
1805  * - ticks:	Return storage for alternate cache timestamp.  On a successful
1806  *   		(positive or negative) lookup, it will contain the ticks value
1807  *   		that was current when the cache entry was created, unless cnp
1808  *   		was ".".
1809  *
1810  * Either both tsp and ticks have to be provided or neither of them.
1811  *
1812  * # Returns
1813  *
1814  * - -1:	A positive cache hit.  vpp will contain the desired vnode.
1815  * - ENOENT:	A negative cache hit, or dvp was recycled out from under us due
1816  *		to a forced unmount.  vpp will not be modified.  If the entry
1817  *		is a whiteout, then the ISWHITEOUT flag will be set in
1818  *		cnp->cn_flags.
1819  * - 0:		A cache miss.  vpp will not be modified.
1820  *
1821  * # Locking
1822  *
1823  * On a cache hit, vpp will be returned locked and ref'd.  If we're looking up
1824  * .., dvp is unlocked.  If we're looking up . an extra ref is taken, but the
1825  * lock is not recursively acquired.
1826  */
1827 static int __noinline
1828 cache_lookup_fallback(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1829     struct timespec *tsp, int *ticksp)
1830 {
1831 	struct namecache *ncp;
1832 	struct mtx *blp;
1833 	uint32_t hash;
1834 	enum vgetstate vs;
1835 	int error;
1836 	bool whiteout;
1837 
1838 	MPASS((cnp->cn_flags & ISDOTDOT) == 0);
1839 	MPASS((cnp->cn_flags & (MAKEENTRY | NC_KEEPPOSENTRY)) != 0);
1840 
1841 retry:
1842 	hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1843 	blp = HASH2BUCKETLOCK(hash);
1844 	mtx_lock(blp);
1845 
1846 	CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1847 		if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1848 		    !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1849 			break;
1850 	}
1851 
1852 	if (__predict_false(ncp == NULL)) {
1853 		mtx_unlock(blp);
1854 		SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
1855 		    NULL);
1856 		counter_u64_add(nummiss, 1);
1857 		return (0);
1858 	}
1859 
1860 	if (ncp->nc_flag & NCF_NEGATIVE)
1861 		goto negative_success;
1862 
1863 	counter_u64_add(numposhits, 1);
1864 	*vpp = ncp->nc_vp;
1865 	SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name, *vpp);
1866 	cache_out_ts(ncp, tsp, ticksp);
1867 	MPASS(dvp != *vpp);
1868 	vs = vget_prep(*vpp);
1869 	mtx_unlock(blp);
1870 	error = vget_finish(*vpp, cnp->cn_lkflags, vs);
1871 	if (error) {
1872 		*vpp = NULL;
1873 		goto retry;
1874 	}
1875 	return (-1);
1876 negative_success:
1877 	/*
1878 	 * We don't get here with regular lookup apart from corner cases.
1879 	 */
1880 	if (__predict_true(cnp->cn_nameiop == CREATE)) {
1881 		if (cnp->cn_flags & ISLASTCN) {
1882 			counter_u64_add(numnegzaps, 1);
1883 			error = cache_zap_locked_bucket(ncp, cnp, hash, blp);
1884 			if (__predict_false(error != 0)) {
1885 				zap_bucket_fail2++;
1886 				goto retry;
1887 			}
1888 			cache_free(ncp);
1889 			return (0);
1890 		}
1891 	}
1892 
1893 	whiteout = (ncp->nc_flag & NCF_WHITE);
1894 	cache_out_ts(ncp, tsp, ticksp);
1895 	if (cache_neg_hit_prep(ncp))
1896 		cache_neg_promote(ncp);
1897 	else
1898 		cache_neg_hit_finish(ncp);
1899 	mtx_unlock(blp);
1900 	if (whiteout)
1901 		cnp->cn_flags |= ISWHITEOUT;
1902 	return (ENOENT);
1903 }
1904 
1905 int
1906 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1907     struct timespec *tsp, int *ticksp)
1908 {
1909 	struct namecache *ncp;
1910 	uint32_t hash;
1911 	enum vgetstate vs;
1912 	int error;
1913 	bool whiteout, neg_promote;
1914 	u_short nc_flag;
1915 
1916 	MPASS((tsp == NULL && ticksp == NULL) || (tsp != NULL && ticksp != NULL));
1917 
1918 #ifdef DEBUG_CACHE
1919 	if (__predict_false(!doingcache)) {
1920 		cnp->cn_flags &= ~MAKEENTRY;
1921 		return (0);
1922 	}
1923 #endif
1924 
1925 	if (__predict_false(cnp->cn_nameptr[0] == '.')) {
1926 		if (cnp->cn_namelen == 1)
1927 			return (cache_lookup_dot(dvp, vpp, cnp, tsp, ticksp));
1928 		if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.')
1929 			return (cache_lookup_dotdot(dvp, vpp, cnp, tsp, ticksp));
1930 	}
1931 
1932 	MPASS((cnp->cn_flags & ISDOTDOT) == 0);
1933 
1934 	if ((cnp->cn_flags & (MAKEENTRY | NC_KEEPPOSENTRY)) == 0) {
1935 		cache_remove_cnp(dvp, cnp);
1936 		return (0);
1937 	}
1938 
1939 	hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1940 	vfs_smr_enter();
1941 
1942 	CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1943 		if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1944 		    !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1945 			break;
1946 	}
1947 
1948 	if (__predict_false(ncp == NULL)) {
1949 		vfs_smr_exit();
1950 		SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
1951 		    NULL);
1952 		counter_u64_add(nummiss, 1);
1953 		return (0);
1954 	}
1955 
1956 	nc_flag = atomic_load_char(&ncp->nc_flag);
1957 	if (nc_flag & NCF_NEGATIVE)
1958 		goto negative_success;
1959 
1960 	counter_u64_add(numposhits, 1);
1961 	*vpp = ncp->nc_vp;
1962 	SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name, *vpp);
1963 	cache_out_ts(ncp, tsp, ticksp);
1964 	MPASS(dvp != *vpp);
1965 	if (!cache_ncp_canuse(ncp)) {
1966 		vfs_smr_exit();
1967 		*vpp = NULL;
1968 		goto out_fallback;
1969 	}
1970 	vs = vget_prep_smr(*vpp);
1971 	vfs_smr_exit();
1972 	if (__predict_false(vs == VGET_NONE)) {
1973 		*vpp = NULL;
1974 		goto out_fallback;
1975 	}
1976 	error = vget_finish(*vpp, cnp->cn_lkflags, vs);
1977 	if (error) {
1978 		*vpp = NULL;
1979 		goto out_fallback;
1980 	}
1981 	return (-1);
1982 negative_success:
1983 	if (cnp->cn_nameiop == CREATE) {
1984 		if (cnp->cn_flags & ISLASTCN) {
1985 			vfs_smr_exit();
1986 			goto out_fallback;
1987 		}
1988 	}
1989 
1990 	cache_out_ts(ncp, tsp, ticksp);
1991 	whiteout = (atomic_load_char(&ncp->nc_flag) & NCF_WHITE);
1992 	neg_promote = cache_neg_hit_prep(ncp);
1993 	if (!cache_ncp_canuse(ncp)) {
1994 		cache_neg_hit_abort(ncp);
1995 		vfs_smr_exit();
1996 		goto out_fallback;
1997 	}
1998 	if (neg_promote) {
1999 		vfs_smr_exit();
2000 		if (!cache_neg_promote_cond(dvp, cnp, ncp, hash))
2001 			goto out_fallback;
2002 	} else {
2003 		cache_neg_hit_finish(ncp);
2004 		vfs_smr_exit();
2005 	}
2006 	if (whiteout)
2007 		cnp->cn_flags |= ISWHITEOUT;
2008 	return (ENOENT);
2009 out_fallback:
2010 	return (cache_lookup_fallback(dvp, vpp, cnp, tsp, ticksp));
2011 }
2012 
2013 struct celockstate {
2014 	struct mtx *vlp[3];
2015 	struct mtx *blp[2];
2016 };
2017 CTASSERT((nitems(((struct celockstate *)0)->vlp) == 3));
2018 CTASSERT((nitems(((struct celockstate *)0)->blp) == 2));
2019 
2020 static inline void
2021 cache_celockstate_init(struct celockstate *cel)
2022 {
2023 
2024 	bzero(cel, sizeof(*cel));
2025 }
2026 
2027 static void
2028 cache_lock_vnodes_cel(struct celockstate *cel, struct vnode *vp,
2029     struct vnode *dvp)
2030 {
2031 	struct mtx *vlp1, *vlp2;
2032 
2033 	MPASS(cel->vlp[0] == NULL);
2034 	MPASS(cel->vlp[1] == NULL);
2035 	MPASS(cel->vlp[2] == NULL);
2036 
2037 	MPASS(vp != NULL || dvp != NULL);
2038 
2039 	vlp1 = VP2VNODELOCK(vp);
2040 	vlp2 = VP2VNODELOCK(dvp);
2041 	cache_sort_vnodes(&vlp1, &vlp2);
2042 
2043 	if (vlp1 != NULL) {
2044 		mtx_lock(vlp1);
2045 		cel->vlp[0] = vlp1;
2046 	}
2047 	mtx_lock(vlp2);
2048 	cel->vlp[1] = vlp2;
2049 }
2050 
2051 static void
2052 cache_unlock_vnodes_cel(struct celockstate *cel)
2053 {
2054 
2055 	MPASS(cel->vlp[0] != NULL || cel->vlp[1] != NULL);
2056 
2057 	if (cel->vlp[0] != NULL)
2058 		mtx_unlock(cel->vlp[0]);
2059 	if (cel->vlp[1] != NULL)
2060 		mtx_unlock(cel->vlp[1]);
2061 	if (cel->vlp[2] != NULL)
2062 		mtx_unlock(cel->vlp[2]);
2063 }
2064 
2065 static bool
2066 cache_lock_vnodes_cel_3(struct celockstate *cel, struct vnode *vp)
2067 {
2068 	struct mtx *vlp;
2069 	bool ret;
2070 
2071 	cache_assert_vlp_locked(cel->vlp[0]);
2072 	cache_assert_vlp_locked(cel->vlp[1]);
2073 	MPASS(cel->vlp[2] == NULL);
2074 
2075 	MPASS(vp != NULL);
2076 	vlp = VP2VNODELOCK(vp);
2077 
2078 	ret = true;
2079 	if (vlp >= cel->vlp[1]) {
2080 		mtx_lock(vlp);
2081 	} else {
2082 		if (mtx_trylock(vlp))
2083 			goto out;
2084 		cache_lock_vnodes_cel_3_failures++;
2085 		cache_unlock_vnodes_cel(cel);
2086 		if (vlp < cel->vlp[0]) {
2087 			mtx_lock(vlp);
2088 			mtx_lock(cel->vlp[0]);
2089 			mtx_lock(cel->vlp[1]);
2090 		} else {
2091 			if (cel->vlp[0] != NULL)
2092 				mtx_lock(cel->vlp[0]);
2093 			mtx_lock(vlp);
2094 			mtx_lock(cel->vlp[1]);
2095 		}
2096 		ret = false;
2097 	}
2098 out:
2099 	cel->vlp[2] = vlp;
2100 	return (ret);
2101 }
2102 
2103 static void
2104 cache_lock_buckets_cel(struct celockstate *cel, struct mtx *blp1,
2105     struct mtx *blp2)
2106 {
2107 
2108 	MPASS(cel->blp[0] == NULL);
2109 	MPASS(cel->blp[1] == NULL);
2110 
2111 	cache_sort_vnodes(&blp1, &blp2);
2112 
2113 	if (blp1 != NULL) {
2114 		mtx_lock(blp1);
2115 		cel->blp[0] = blp1;
2116 	}
2117 	mtx_lock(blp2);
2118 	cel->blp[1] = blp2;
2119 }
2120 
2121 static void
2122 cache_unlock_buckets_cel(struct celockstate *cel)
2123 {
2124 
2125 	if (cel->blp[0] != NULL)
2126 		mtx_unlock(cel->blp[0]);
2127 	mtx_unlock(cel->blp[1]);
2128 }
2129 
2130 /*
2131  * Lock part of the cache affected by the insertion.
2132  *
2133  * This means vnodelocks for dvp, vp and the relevant bucketlock.
2134  * However, insertion can result in removal of an old entry. In this
2135  * case we have an additional vnode and bucketlock pair to lock.
2136  *
2137  * That is, in the worst case we have to lock 3 vnodes and 2 bucketlocks, while
2138  * preserving the locking order (smaller address first).
2139  */
2140 static void
2141 cache_enter_lock(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
2142     uint32_t hash)
2143 {
2144 	struct namecache *ncp;
2145 	struct mtx *blps[2];
2146 	u_char nc_flag;
2147 
2148 	blps[0] = HASH2BUCKETLOCK(hash);
2149 	for (;;) {
2150 		blps[1] = NULL;
2151 		cache_lock_vnodes_cel(cel, dvp, vp);
2152 		if (vp == NULL || vp->v_type != VDIR)
2153 			break;
2154 		ncp = atomic_load_consume_ptr(&vp->v_cache_dd);
2155 		if (ncp == NULL)
2156 			break;
2157 		nc_flag = atomic_load_char(&ncp->nc_flag);
2158 		if ((nc_flag & NCF_ISDOTDOT) == 0)
2159 			break;
2160 		MPASS(ncp->nc_dvp == vp);
2161 		blps[1] = NCP2BUCKETLOCK(ncp);
2162 		if ((nc_flag & NCF_NEGATIVE) != 0)
2163 			break;
2164 		if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
2165 			break;
2166 		/*
2167 		 * All vnodes got re-locked. Re-validate the state and if
2168 		 * nothing changed we are done. Otherwise restart.
2169 		 */
2170 		if (ncp == vp->v_cache_dd &&
2171 		    (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
2172 		    blps[1] == NCP2BUCKETLOCK(ncp) &&
2173 		    VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
2174 			break;
2175 		cache_unlock_vnodes_cel(cel);
2176 		cel->vlp[0] = NULL;
2177 		cel->vlp[1] = NULL;
2178 		cel->vlp[2] = NULL;
2179 	}
2180 	cache_lock_buckets_cel(cel, blps[0], blps[1]);
2181 }
2182 
2183 static void
2184 cache_enter_lock_dd(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
2185     uint32_t hash)
2186 {
2187 	struct namecache *ncp;
2188 	struct mtx *blps[2];
2189 	u_char nc_flag;
2190 
2191 	blps[0] = HASH2BUCKETLOCK(hash);
2192 	for (;;) {
2193 		blps[1] = NULL;
2194 		cache_lock_vnodes_cel(cel, dvp, vp);
2195 		ncp = atomic_load_consume_ptr(&dvp->v_cache_dd);
2196 		if (ncp == NULL)
2197 			break;
2198 		nc_flag = atomic_load_char(&ncp->nc_flag);
2199 		if ((nc_flag & NCF_ISDOTDOT) == 0)
2200 			break;
2201 		MPASS(ncp->nc_dvp == dvp);
2202 		blps[1] = NCP2BUCKETLOCK(ncp);
2203 		if ((nc_flag & NCF_NEGATIVE) != 0)
2204 			break;
2205 		if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
2206 			break;
2207 		if (ncp == dvp->v_cache_dd &&
2208 		    (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
2209 		    blps[1] == NCP2BUCKETLOCK(ncp) &&
2210 		    VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
2211 			break;
2212 		cache_unlock_vnodes_cel(cel);
2213 		cel->vlp[0] = NULL;
2214 		cel->vlp[1] = NULL;
2215 		cel->vlp[2] = NULL;
2216 	}
2217 	cache_lock_buckets_cel(cel, blps[0], blps[1]);
2218 }
2219 
2220 static void
2221 cache_enter_unlock(struct celockstate *cel)
2222 {
2223 
2224 	cache_unlock_buckets_cel(cel);
2225 	cache_unlock_vnodes_cel(cel);
2226 }
2227 
2228 static void __noinline
2229 cache_enter_dotdot_prep(struct vnode *dvp, struct vnode *vp,
2230     struct componentname *cnp)
2231 {
2232 	struct celockstate cel;
2233 	struct namecache *ncp;
2234 	uint32_t hash;
2235 	int len;
2236 
2237 	if (atomic_load_ptr(&dvp->v_cache_dd) == NULL)
2238 		return;
2239 	len = cnp->cn_namelen;
2240 	cache_celockstate_init(&cel);
2241 	hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
2242 	cache_enter_lock_dd(&cel, dvp, vp, hash);
2243 	ncp = dvp->v_cache_dd;
2244 	if (ncp != NULL && (ncp->nc_flag & NCF_ISDOTDOT)) {
2245 		KASSERT(ncp->nc_dvp == dvp, ("wrong isdotdot parent"));
2246 		cache_zap_locked(ncp);
2247 	} else {
2248 		ncp = NULL;
2249 	}
2250 	atomic_store_ptr(&dvp->v_cache_dd, NULL);
2251 	cache_enter_unlock(&cel);
2252 	if (ncp != NULL)
2253 		cache_free(ncp);
2254 }
2255 
2256 /*
2257  * Add an entry to the cache.
2258  */
2259 void
2260 cache_enter_time(struct vnode *dvp, struct vnode *vp, struct componentname *cnp,
2261     struct timespec *tsp, struct timespec *dtsp)
2262 {
2263 	struct celockstate cel;
2264 	struct namecache *ncp, *n2, *ndd;
2265 	struct namecache_ts *ncp_ts;
2266 	struct nchashhead *ncpp;
2267 	uint32_t hash;
2268 	int flag;
2269 	int len;
2270 
2271 	KASSERT(cnp->cn_namelen <= NAME_MAX,
2272 	    ("%s: passed len %ld exceeds NAME_MAX (%d)", __func__, cnp->cn_namelen,
2273 	    NAME_MAX));
2274 	VNPASS(dvp != vp, dvp);
2275 	VNPASS(!VN_IS_DOOMED(dvp), dvp);
2276 	VNPASS(dvp->v_type != VNON, dvp);
2277 	if (vp != NULL) {
2278 		VNPASS(!VN_IS_DOOMED(vp), vp);
2279 		VNPASS(vp->v_type != VNON, vp);
2280 	}
2281 
2282 #ifdef DEBUG_CACHE
2283 	if (__predict_false(!doingcache))
2284 		return;
2285 #endif
2286 
2287 	flag = 0;
2288 	if (__predict_false(cnp->cn_nameptr[0] == '.')) {
2289 		if (cnp->cn_namelen == 1)
2290 			return;
2291 		if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
2292 			cache_enter_dotdot_prep(dvp, vp, cnp);
2293 			flag = NCF_ISDOTDOT;
2294 		}
2295 	}
2296 
2297 	ncp = cache_alloc(cnp->cn_namelen, tsp != NULL);
2298 	if (ncp == NULL)
2299 		return;
2300 
2301 	cache_celockstate_init(&cel);
2302 	ndd = NULL;
2303 	ncp_ts = NULL;
2304 
2305 	/*
2306 	 * Calculate the hash key and setup as much of the new
2307 	 * namecache entry as possible before acquiring the lock.
2308 	 */
2309 	ncp->nc_flag = flag | NCF_WIP;
2310 	ncp->nc_vp = vp;
2311 	if (vp == NULL)
2312 		cache_neg_init(ncp);
2313 	ncp->nc_dvp = dvp;
2314 	if (tsp != NULL) {
2315 		ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
2316 		ncp_ts->nc_time = *tsp;
2317 		ncp_ts->nc_ticks = ticks;
2318 		ncp_ts->nc_nc.nc_flag |= NCF_TS;
2319 		if (dtsp != NULL) {
2320 			ncp_ts->nc_dotdottime = *dtsp;
2321 			ncp_ts->nc_nc.nc_flag |= NCF_DTS;
2322 		}
2323 	}
2324 	len = ncp->nc_nlen = cnp->cn_namelen;
2325 	hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
2326 	memcpy(ncp->nc_name, cnp->cn_nameptr, len);
2327 	ncp->nc_name[len] = '\0';
2328 	cache_enter_lock(&cel, dvp, vp, hash);
2329 
2330 	/*
2331 	 * See if this vnode or negative entry is already in the cache
2332 	 * with this name.  This can happen with concurrent lookups of
2333 	 * the same path name.
2334 	 */
2335 	ncpp = NCHHASH(hash);
2336 	CK_SLIST_FOREACH(n2, ncpp, nc_hash) {
2337 		if (n2->nc_dvp == dvp &&
2338 		    n2->nc_nlen == cnp->cn_namelen &&
2339 		    !bcmp(n2->nc_name, cnp->cn_nameptr, n2->nc_nlen)) {
2340 			MPASS(cache_ncp_canuse(n2));
2341 			if ((n2->nc_flag & NCF_NEGATIVE) != 0)
2342 				KASSERT(vp == NULL,
2343 				    ("%s: found entry pointing to a different vnode (%p != %p)",
2344 				    __func__, NULL, vp));
2345 			else
2346 				KASSERT(n2->nc_vp == vp,
2347 				    ("%s: found entry pointing to a different vnode (%p != %p)",
2348 				    __func__, n2->nc_vp, vp));
2349 			/*
2350 			 * Entries are supposed to be immutable unless in the
2351 			 * process of getting destroyed. Accommodating for
2352 			 * changing timestamps is possible but not worth it.
2353 			 * This should be harmless in terms of correctness, in
2354 			 * the worst case resulting in an earlier expiration.
2355 			 * Alternatively, the found entry can be replaced
2356 			 * altogether.
2357 			 */
2358 			MPASS((n2->nc_flag & (NCF_TS | NCF_DTS)) == (ncp->nc_flag & (NCF_TS | NCF_DTS)));
2359 #if 0
2360 			if (tsp != NULL) {
2361 				KASSERT((n2->nc_flag & NCF_TS) != 0,
2362 				    ("no NCF_TS"));
2363 				n2_ts = __containerof(n2, struct namecache_ts, nc_nc);
2364 				n2_ts->nc_time = ncp_ts->nc_time;
2365 				n2_ts->nc_ticks = ncp_ts->nc_ticks;
2366 				if (dtsp != NULL) {
2367 					n2_ts->nc_dotdottime = ncp_ts->nc_dotdottime;
2368 					n2_ts->nc_nc.nc_flag |= NCF_DTS;
2369 				}
2370 			}
2371 #endif
2372 			SDT_PROBE3(vfs, namecache, enter, duplicate, dvp, ncp->nc_name,
2373 			    vp);
2374 			goto out_unlock_free;
2375 		}
2376 	}
2377 
2378 	if (flag == NCF_ISDOTDOT) {
2379 		/*
2380 		 * See if we are trying to add .. entry, but some other lookup
2381 		 * has populated v_cache_dd pointer already.
2382 		 */
2383 		if (dvp->v_cache_dd != NULL)
2384 			goto out_unlock_free;
2385 		KASSERT(vp == NULL || vp->v_type == VDIR,
2386 		    ("wrong vnode type %p", vp));
2387 		atomic_thread_fence_rel();
2388 		atomic_store_ptr(&dvp->v_cache_dd, ncp);
2389 	}
2390 
2391 	if (vp != NULL) {
2392 		if (flag != NCF_ISDOTDOT) {
2393 			/*
2394 			 * For this case, the cache entry maps both the
2395 			 * directory name in it and the name ".." for the
2396 			 * directory's parent.
2397 			 */
2398 			if ((ndd = vp->v_cache_dd) != NULL) {
2399 				if ((ndd->nc_flag & NCF_ISDOTDOT) != 0)
2400 					cache_zap_locked(ndd);
2401 				else
2402 					ndd = NULL;
2403 			}
2404 			atomic_thread_fence_rel();
2405 			atomic_store_ptr(&vp->v_cache_dd, ncp);
2406 		} else if (vp->v_type != VDIR) {
2407 			if (vp->v_cache_dd != NULL) {
2408 				atomic_store_ptr(&vp->v_cache_dd, NULL);
2409 			}
2410 		}
2411 	}
2412 
2413 	if (flag != NCF_ISDOTDOT) {
2414 		if (LIST_EMPTY(&dvp->v_cache_src)) {
2415 			cache_hold_vnode(dvp);
2416 		}
2417 		LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
2418 	}
2419 
2420 	/*
2421 	 * If the entry is "negative", we place it into the
2422 	 * "negative" cache queue, otherwise, we place it into the
2423 	 * destination vnode's cache entries queue.
2424 	 */
2425 	if (vp != NULL) {
2426 		TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
2427 		SDT_PROBE3(vfs, namecache, enter, done, dvp, ncp->nc_name,
2428 		    vp);
2429 	} else {
2430 		if (cnp->cn_flags & ISWHITEOUT)
2431 			atomic_store_char(&ncp->nc_flag, ncp->nc_flag | NCF_WHITE);
2432 		cache_neg_insert(ncp);
2433 		SDT_PROBE2(vfs, namecache, enter_negative, done, dvp,
2434 		    ncp->nc_name);
2435 	}
2436 
2437 	/*
2438 	 * Insert the new namecache entry into the appropriate chain
2439 	 * within the cache entries table.
2440 	 */
2441 	CK_SLIST_INSERT_HEAD(ncpp, ncp, nc_hash);
2442 
2443 	atomic_thread_fence_rel();
2444 	/*
2445 	 * Mark the entry as fully constructed.
2446 	 * It is immutable past this point until its removal.
2447 	 */
2448 	atomic_store_char(&ncp->nc_flag, ncp->nc_flag & ~NCF_WIP);
2449 
2450 	cache_enter_unlock(&cel);
2451 	if (ndd != NULL)
2452 		cache_free(ndd);
2453 	return;
2454 out_unlock_free:
2455 	cache_enter_unlock(&cel);
2456 	cache_free(ncp);
2457 	return;
2458 }
2459 
2460 static u_int
2461 cache_roundup_2(u_int val)
2462 {
2463 	u_int res;
2464 
2465 	for (res = 1; res <= val; res <<= 1)
2466 		continue;
2467 
2468 	return (res);
2469 }
2470 
2471 static struct nchashhead *
2472 nchinittbl(u_long elements, u_long *hashmask)
2473 {
2474 	struct nchashhead *hashtbl;
2475 	u_long hashsize, i;
2476 
2477 	hashsize = cache_roundup_2(elements) / 2;
2478 
2479 	hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), M_VFSCACHE, M_WAITOK);
2480 	for (i = 0; i < hashsize; i++)
2481 		CK_SLIST_INIT(&hashtbl[i]);
2482 	*hashmask = hashsize - 1;
2483 	return (hashtbl);
2484 }
2485 
2486 static void
2487 ncfreetbl(struct nchashhead *hashtbl)
2488 {
2489 
2490 	free(hashtbl, M_VFSCACHE);
2491 }
2492 
2493 /*
2494  * Name cache initialization, from vfs_init() when we are booting
2495  */
2496 static void
2497 nchinit(void *dummy __unused)
2498 {
2499 	u_int i;
2500 
2501 	cache_zone_small = uma_zcreate("S VFS Cache", CACHE_ZONE_SMALL_SIZE,
2502 	    NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT, UMA_ZONE_ZINIT);
2503 	cache_zone_small_ts = uma_zcreate("STS VFS Cache", CACHE_ZONE_SMALL_TS_SIZE,
2504 	    NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT, UMA_ZONE_ZINIT);
2505 	cache_zone_large = uma_zcreate("L VFS Cache", CACHE_ZONE_LARGE_SIZE,
2506 	    NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT, UMA_ZONE_ZINIT);
2507 	cache_zone_large_ts = uma_zcreate("LTS VFS Cache", CACHE_ZONE_LARGE_TS_SIZE,
2508 	    NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT, UMA_ZONE_ZINIT);
2509 
2510 	VFS_SMR_ZONE_SET(cache_zone_small);
2511 	VFS_SMR_ZONE_SET(cache_zone_small_ts);
2512 	VFS_SMR_ZONE_SET(cache_zone_large);
2513 	VFS_SMR_ZONE_SET(cache_zone_large_ts);
2514 
2515 	ncsize = desiredvnodes * ncsizefactor;
2516 	cache_recalc_neg_min(ncnegminpct);
2517 	nchashtbl = nchinittbl(desiredvnodes * 2, &nchash);
2518 	ncbuckethash = cache_roundup_2(mp_ncpus * mp_ncpus) - 1;
2519 	if (ncbuckethash < 7) /* arbitrarily chosen to avoid having one lock */
2520 		ncbuckethash = 7;
2521 	if (ncbuckethash > nchash)
2522 		ncbuckethash = nchash;
2523 	bucketlocks = malloc(sizeof(*bucketlocks) * numbucketlocks, M_VFSCACHE,
2524 	    M_WAITOK | M_ZERO);
2525 	for (i = 0; i < numbucketlocks; i++)
2526 		mtx_init(&bucketlocks[i], "ncbuc", NULL, MTX_DUPOK | MTX_RECURSE);
2527 	ncvnodehash = ncbuckethash;
2528 	vnodelocks = malloc(sizeof(*vnodelocks) * numvnodelocks, M_VFSCACHE,
2529 	    M_WAITOK | M_ZERO);
2530 	for (i = 0; i < numvnodelocks; i++)
2531 		mtx_init(&vnodelocks[i], "ncvn", NULL, MTX_DUPOK | MTX_RECURSE);
2532 
2533 	for (i = 0; i < numneglists; i++) {
2534 		mtx_init(&neglists[i].nl_evict_lock, "ncnege", NULL, MTX_DEF);
2535 		mtx_init(&neglists[i].nl_lock, "ncnegl", NULL, MTX_DEF);
2536 		TAILQ_INIT(&neglists[i].nl_list);
2537 		TAILQ_INIT(&neglists[i].nl_hotlist);
2538 	}
2539 }
2540 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL);
2541 
2542 void
2543 cache_vnode_init(struct vnode *vp)
2544 {
2545 
2546 	LIST_INIT(&vp->v_cache_src);
2547 	TAILQ_INIT(&vp->v_cache_dst);
2548 	vp->v_cache_dd = NULL;
2549 	cache_prehash(vp);
2550 }
2551 
2552 void
2553 cache_changesize(u_long newmaxvnodes)
2554 {
2555 	struct nchashhead *new_nchashtbl, *old_nchashtbl;
2556 	u_long new_nchash, old_nchash;
2557 	struct namecache *ncp;
2558 	uint32_t hash;
2559 	u_long newncsize;
2560 	int i;
2561 
2562 	newncsize = newmaxvnodes * ncsizefactor;
2563 	newmaxvnodes = cache_roundup_2(newmaxvnodes * 2);
2564 	if (newmaxvnodes < numbucketlocks)
2565 		newmaxvnodes = numbucketlocks;
2566 
2567 	new_nchashtbl = nchinittbl(newmaxvnodes, &new_nchash);
2568 	/* If same hash table size, nothing to do */
2569 	if (nchash == new_nchash) {
2570 		ncfreetbl(new_nchashtbl);
2571 		return;
2572 	}
2573 	/*
2574 	 * Move everything from the old hash table to the new table.
2575 	 * None of the namecache entries in the table can be removed
2576 	 * because to do so, they have to be removed from the hash table.
2577 	 */
2578 	cache_lock_all_vnodes();
2579 	cache_lock_all_buckets();
2580 	old_nchashtbl = nchashtbl;
2581 	old_nchash = nchash;
2582 	nchashtbl = new_nchashtbl;
2583 	nchash = new_nchash;
2584 	for (i = 0; i <= old_nchash; i++) {
2585 		while ((ncp = CK_SLIST_FIRST(&old_nchashtbl[i])) != NULL) {
2586 			hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen,
2587 			    ncp->nc_dvp);
2588 			CK_SLIST_REMOVE(&old_nchashtbl[i], ncp, namecache, nc_hash);
2589 			CK_SLIST_INSERT_HEAD(NCHHASH(hash), ncp, nc_hash);
2590 		}
2591 	}
2592 	ncsize = newncsize;
2593 	cache_recalc_neg_min(ncnegminpct);
2594 	cache_unlock_all_buckets();
2595 	cache_unlock_all_vnodes();
2596 	ncfreetbl(old_nchashtbl);
2597 }
2598 
2599 /*
2600  * Remove all entries from and to a particular vnode.
2601  */
2602 static void
2603 cache_purge_impl(struct vnode *vp)
2604 {
2605 	struct cache_freebatch batch;
2606 	struct namecache *ncp;
2607 	struct mtx *vlp, *vlp2;
2608 
2609 	TAILQ_INIT(&batch);
2610 	vlp = VP2VNODELOCK(vp);
2611 	vlp2 = NULL;
2612 	mtx_lock(vlp);
2613 retry:
2614 	while (!LIST_EMPTY(&vp->v_cache_src)) {
2615 		ncp = LIST_FIRST(&vp->v_cache_src);
2616 		if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2617 			goto retry;
2618 		TAILQ_INSERT_TAIL(&batch, ncp, nc_dst);
2619 	}
2620 	while (!TAILQ_EMPTY(&vp->v_cache_dst)) {
2621 		ncp = TAILQ_FIRST(&vp->v_cache_dst);
2622 		if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2623 			goto retry;
2624 		TAILQ_INSERT_TAIL(&batch, ncp, nc_dst);
2625 	}
2626 	ncp = vp->v_cache_dd;
2627 	if (ncp != NULL) {
2628 		KASSERT(ncp->nc_flag & NCF_ISDOTDOT,
2629 		   ("lost dotdot link"));
2630 		if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2631 			goto retry;
2632 		TAILQ_INSERT_TAIL(&batch, ncp, nc_dst);
2633 	}
2634 	KASSERT(vp->v_cache_dd == NULL, ("incomplete purge"));
2635 	mtx_unlock(vlp);
2636 	if (vlp2 != NULL)
2637 		mtx_unlock(vlp2);
2638 	cache_free_batch(&batch);
2639 }
2640 
2641 /*
2642  * Opportunistic check to see if there is anything to do.
2643  */
2644 static bool
2645 cache_has_entries(struct vnode *vp)
2646 {
2647 
2648 	if (LIST_EMPTY(&vp->v_cache_src) && TAILQ_EMPTY(&vp->v_cache_dst) &&
2649 	    atomic_load_ptr(&vp->v_cache_dd) == NULL)
2650 		return (false);
2651 	return (true);
2652 }
2653 
2654 void
2655 cache_purge(struct vnode *vp)
2656 {
2657 
2658 	SDT_PROBE1(vfs, namecache, purge, done, vp);
2659 	if (!cache_has_entries(vp))
2660 		return;
2661 	cache_purge_impl(vp);
2662 }
2663 
2664 /*
2665  * Only to be used by vgone.
2666  */
2667 void
2668 cache_purge_vgone(struct vnode *vp)
2669 {
2670 	struct mtx *vlp;
2671 
2672 	VNPASS(VN_IS_DOOMED(vp), vp);
2673 	if (cache_has_entries(vp)) {
2674 		cache_purge_impl(vp);
2675 		return;
2676 	}
2677 
2678 	/*
2679 	 * Serialize against a potential thread doing cache_purge.
2680 	 */
2681 	vlp = VP2VNODELOCK(vp);
2682 	mtx_wait_unlocked(vlp);
2683 	if (cache_has_entries(vp)) {
2684 		cache_purge_impl(vp);
2685 		return;
2686 	}
2687 	return;
2688 }
2689 
2690 /*
2691  * Remove all negative entries for a particular directory vnode.
2692  */
2693 void
2694 cache_purge_negative(struct vnode *vp)
2695 {
2696 	struct cache_freebatch batch;
2697 	struct namecache *ncp, *nnp;
2698 	struct mtx *vlp;
2699 
2700 	SDT_PROBE1(vfs, namecache, purge_negative, done, vp);
2701 	if (LIST_EMPTY(&vp->v_cache_src))
2702 		return;
2703 	TAILQ_INIT(&batch);
2704 	vlp = VP2VNODELOCK(vp);
2705 	mtx_lock(vlp);
2706 	LIST_FOREACH_SAFE(ncp, &vp->v_cache_src, nc_src, nnp) {
2707 		if (!(ncp->nc_flag & NCF_NEGATIVE))
2708 			continue;
2709 		cache_zap_negative_locked_vnode_kl(ncp, vp);
2710 		TAILQ_INSERT_TAIL(&batch, ncp, nc_dst);
2711 	}
2712 	mtx_unlock(vlp);
2713 	cache_free_batch(&batch);
2714 }
2715 
2716 /*
2717  * Entry points for modifying VOP operations.
2718  */
2719 void
2720 cache_vop_rename(struct vnode *fdvp, struct vnode *fvp, struct vnode *tdvp,
2721     struct vnode *tvp, struct componentname *fcnp, struct componentname *tcnp)
2722 {
2723 
2724 	ASSERT_VOP_IN_SEQC(fdvp);
2725 	ASSERT_VOP_IN_SEQC(fvp);
2726 	ASSERT_VOP_IN_SEQC(tdvp);
2727 	if (tvp != NULL)
2728 		ASSERT_VOP_IN_SEQC(tvp);
2729 
2730 	cache_purge(fvp);
2731 	if (tvp != NULL) {
2732 		cache_purge(tvp);
2733 		KASSERT(!cache_remove_cnp(tdvp, tcnp),
2734 		    ("%s: lingering negative entry", __func__));
2735 	} else {
2736 		cache_remove_cnp(tdvp, tcnp);
2737 	}
2738 
2739 	/*
2740 	 * TODO
2741 	 *
2742 	 * Historically renaming was always purging all revelang entries,
2743 	 * but that's quite wasteful. In particular turns out that in many cases
2744 	 * the target file is immediately accessed after rename, inducing a cache
2745 	 * miss.
2746 	 *
2747 	 * Recode this to reduce relocking and reuse the existing entry (if any)
2748 	 * instead of just removing it above and allocating a new one here.
2749 	 */
2750 	if (cache_rename_add) {
2751 		cache_enter(tdvp, fvp, tcnp);
2752 	}
2753 }
2754 
2755 void
2756 cache_vop_rmdir(struct vnode *dvp, struct vnode *vp)
2757 {
2758 
2759 	ASSERT_VOP_IN_SEQC(dvp);
2760 	ASSERT_VOP_IN_SEQC(vp);
2761 	cache_purge(vp);
2762 }
2763 
2764 #ifdef INVARIANTS
2765 /*
2766  * Validate that if an entry exists it matches.
2767  */
2768 void
2769 cache_validate(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2770 {
2771 	struct namecache *ncp;
2772 	struct mtx *blp;
2773 	uint32_t hash;
2774 
2775 	hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
2776 	if (CK_SLIST_EMPTY(NCHHASH(hash)))
2777 		return;
2778 	blp = HASH2BUCKETLOCK(hash);
2779 	mtx_lock(blp);
2780 	CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
2781 		if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
2782 		    !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen)) {
2783 			if (ncp->nc_vp != vp)
2784 				panic("%s: mismatch (%p != %p); ncp %p [%s] dvp %p vp %p\n",
2785 				    __func__, vp, ncp->nc_vp, ncp, ncp->nc_name, ncp->nc_dvp,
2786 				    ncp->nc_vp);
2787 		}
2788 	}
2789 	mtx_unlock(blp);
2790 }
2791 #endif
2792 
2793 /*
2794  * Flush all entries referencing a particular filesystem.
2795  */
2796 void
2797 cache_purgevfs(struct mount *mp)
2798 {
2799 	struct vnode *vp, *mvp;
2800 
2801 	SDT_PROBE1(vfs, namecache, purgevfs, done, mp);
2802 	/*
2803 	 * Somewhat wasteful iteration over all vnodes. Would be better to
2804 	 * support filtering and avoid the interlock to begin with.
2805 	 */
2806 	MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
2807 		if (!cache_has_entries(vp)) {
2808 			VI_UNLOCK(vp);
2809 			continue;
2810 		}
2811 		vholdl(vp);
2812 		VI_UNLOCK(vp);
2813 		cache_purge(vp);
2814 		vdrop(vp);
2815 	}
2816 }
2817 
2818 /*
2819  * Perform canonical checks and cache lookup and pass on to filesystem
2820  * through the vop_cachedlookup only if needed.
2821  */
2822 
2823 int
2824 vfs_cache_lookup(struct vop_lookup_args *ap)
2825 {
2826 	struct vnode *dvp;
2827 	int error;
2828 	struct vnode **vpp = ap->a_vpp;
2829 	struct componentname *cnp = ap->a_cnp;
2830 	int flags = cnp->cn_flags;
2831 
2832 	*vpp = NULL;
2833 	dvp = ap->a_dvp;
2834 
2835 	if (dvp->v_type != VDIR)
2836 		return (ENOTDIR);
2837 
2838 	if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
2839 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
2840 		return (EROFS);
2841 
2842 	error = vn_dir_check_exec(dvp, cnp);
2843 	if (error != 0)
2844 		return (error);
2845 
2846 	error = cache_lookup(dvp, vpp, cnp, NULL, NULL);
2847 	if (error == 0)
2848 		return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
2849 	if (error == -1)
2850 		return (0);
2851 	return (error);
2852 }
2853 
2854 /* Implementation of the getcwd syscall. */
2855 int
2856 sys___getcwd(struct thread *td, struct __getcwd_args *uap)
2857 {
2858 	char *buf, *retbuf;
2859 	size_t buflen;
2860 	int error;
2861 
2862 	buflen = uap->buflen;
2863 	if (__predict_false(buflen < 2))
2864 		return (EINVAL);
2865 	if (buflen > MAXPATHLEN)
2866 		buflen = MAXPATHLEN;
2867 
2868 	buf = uma_zalloc(namei_zone, M_WAITOK);
2869 	error = vn_getcwd(buf, &retbuf, &buflen);
2870 	if (error == 0)
2871 		error = copyout(retbuf, uap->buf, buflen);
2872 	uma_zfree(namei_zone, buf);
2873 	return (error);
2874 }
2875 
2876 int
2877 vn_getcwd(char *buf, char **retbuf, size_t *buflen)
2878 {
2879 	struct pwd *pwd;
2880 	int error;
2881 
2882 	vfs_smr_enter();
2883 	pwd = pwd_get_smr();
2884 	error = vn_fullpath_any_smr(pwd->pwd_cdir, pwd->pwd_rdir, buf, retbuf,
2885 	    buflen, 0);
2886 	VFS_SMR_ASSERT_NOT_ENTERED();
2887 	if (error < 0) {
2888 		pwd = pwd_hold(curthread);
2889 		error = vn_fullpath_any(pwd->pwd_cdir, pwd->pwd_rdir, buf,
2890 		    retbuf, buflen);
2891 		pwd_drop(pwd);
2892 	}
2893 
2894 #ifdef KTRACE
2895 	if (KTRPOINT(curthread, KTR_NAMEI) && error == 0)
2896 		ktrnamei(*retbuf);
2897 #endif
2898 	return (error);
2899 }
2900 
2901 static int
2902 kern___realpathat(struct thread *td, int fd, const char *path, char *buf,
2903     size_t size, int flags, enum uio_seg pathseg)
2904 {
2905 	struct nameidata nd;
2906 	char *retbuf, *freebuf;
2907 	int error;
2908 
2909 	if (flags != 0)
2910 		return (EINVAL);
2911 	NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | SAVENAME | WANTPARENT | AUDITVNODE1,
2912 	    pathseg, path, fd, &cap_fstat_rights, td);
2913 	if ((error = namei(&nd)) != 0)
2914 		return (error);
2915 	error = vn_fullpath_hardlink(&nd, &retbuf, &freebuf, &size);
2916 	if (error == 0) {
2917 		error = copyout(retbuf, buf, size);
2918 		free(freebuf, M_TEMP);
2919 	}
2920 	NDFREE(&nd, 0);
2921 	return (error);
2922 }
2923 
2924 int
2925 sys___realpathat(struct thread *td, struct __realpathat_args *uap)
2926 {
2927 
2928 	return (kern___realpathat(td, uap->fd, uap->path, uap->buf, uap->size,
2929 	    uap->flags, UIO_USERSPACE));
2930 }
2931 
2932 /*
2933  * Retrieve the full filesystem path that correspond to a vnode from the name
2934  * cache (if available)
2935  */
2936 int
2937 vn_fullpath(struct vnode *vp, char **retbuf, char **freebuf)
2938 {
2939 	struct pwd *pwd;
2940 	char *buf;
2941 	size_t buflen;
2942 	int error;
2943 
2944 	if (__predict_false(vp == NULL))
2945 		return (EINVAL);
2946 
2947 	buflen = MAXPATHLEN;
2948 	buf = malloc(buflen, M_TEMP, M_WAITOK);
2949 	vfs_smr_enter();
2950 	pwd = pwd_get_smr();
2951 	error = vn_fullpath_any_smr(vp, pwd->pwd_rdir, buf, retbuf, &buflen, 0);
2952 	VFS_SMR_ASSERT_NOT_ENTERED();
2953 	if (error < 0) {
2954 		pwd = pwd_hold(curthread);
2955 		error = vn_fullpath_any(vp, pwd->pwd_rdir, buf, retbuf, &buflen);
2956 		pwd_drop(pwd);
2957 	}
2958 	if (error == 0)
2959 		*freebuf = buf;
2960 	else
2961 		free(buf, M_TEMP);
2962 	return (error);
2963 }
2964 
2965 /*
2966  * This function is similar to vn_fullpath, but it attempts to lookup the
2967  * pathname relative to the global root mount point.  This is required for the
2968  * auditing sub-system, as audited pathnames must be absolute, relative to the
2969  * global root mount point.
2970  */
2971 int
2972 vn_fullpath_global(struct vnode *vp, char **retbuf, char **freebuf)
2973 {
2974 	char *buf;
2975 	size_t buflen;
2976 	int error;
2977 
2978 	if (__predict_false(vp == NULL))
2979 		return (EINVAL);
2980 	buflen = MAXPATHLEN;
2981 	buf = malloc(buflen, M_TEMP, M_WAITOK);
2982 	vfs_smr_enter();
2983 	error = vn_fullpath_any_smr(vp, rootvnode, buf, retbuf, &buflen, 0);
2984 	VFS_SMR_ASSERT_NOT_ENTERED();
2985 	if (error < 0) {
2986 		error = vn_fullpath_any(vp, rootvnode, buf, retbuf, &buflen);
2987 	}
2988 	if (error == 0)
2989 		*freebuf = buf;
2990 	else
2991 		free(buf, M_TEMP);
2992 	return (error);
2993 }
2994 
2995 static struct namecache *
2996 vn_dd_from_dst(struct vnode *vp)
2997 {
2998 	struct namecache *ncp;
2999 
3000 	cache_assert_vnode_locked(vp);
3001 	TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst) {
3002 		if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
3003 			return (ncp);
3004 	}
3005 	return (NULL);
3006 }
3007 
3008 int
3009 vn_vptocnp(struct vnode **vp, char *buf, size_t *buflen)
3010 {
3011 	struct vnode *dvp;
3012 	struct namecache *ncp;
3013 	struct mtx *vlp;
3014 	int error;
3015 
3016 	vlp = VP2VNODELOCK(*vp);
3017 	mtx_lock(vlp);
3018 	ncp = (*vp)->v_cache_dd;
3019 	if (ncp != NULL && (ncp->nc_flag & NCF_ISDOTDOT) == 0) {
3020 		KASSERT(ncp == vn_dd_from_dst(*vp),
3021 		    ("%s: mismatch for dd entry (%p != %p)", __func__,
3022 		    ncp, vn_dd_from_dst(*vp)));
3023 	} else {
3024 		ncp = vn_dd_from_dst(*vp);
3025 	}
3026 	if (ncp != NULL) {
3027 		if (*buflen < ncp->nc_nlen) {
3028 			mtx_unlock(vlp);
3029 			vrele(*vp);
3030 			counter_u64_add(numfullpathfail4, 1);
3031 			error = ENOMEM;
3032 			SDT_PROBE3(vfs, namecache, fullpath, return, error,
3033 			    vp, NULL);
3034 			return (error);
3035 		}
3036 		*buflen -= ncp->nc_nlen;
3037 		memcpy(buf + *buflen, ncp->nc_name, ncp->nc_nlen);
3038 		SDT_PROBE3(vfs, namecache, fullpath, hit, ncp->nc_dvp,
3039 		    ncp->nc_name, vp);
3040 		dvp = *vp;
3041 		*vp = ncp->nc_dvp;
3042 		vref(*vp);
3043 		mtx_unlock(vlp);
3044 		vrele(dvp);
3045 		return (0);
3046 	}
3047 	SDT_PROBE1(vfs, namecache, fullpath, miss, vp);
3048 
3049 	mtx_unlock(vlp);
3050 	vn_lock(*vp, LK_SHARED | LK_RETRY);
3051 	error = VOP_VPTOCNP(*vp, &dvp, buf, buflen);
3052 	vput(*vp);
3053 	if (error) {
3054 		counter_u64_add(numfullpathfail2, 1);
3055 		SDT_PROBE3(vfs, namecache, fullpath, return,  error, vp, NULL);
3056 		return (error);
3057 	}
3058 
3059 	*vp = dvp;
3060 	if (VN_IS_DOOMED(dvp)) {
3061 		/* forced unmount */
3062 		vrele(dvp);
3063 		error = ENOENT;
3064 		SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
3065 		return (error);
3066 	}
3067 	/*
3068 	 * *vp has its use count incremented still.
3069 	 */
3070 
3071 	return (0);
3072 }
3073 
3074 /*
3075  * Resolve a directory to a pathname.
3076  *
3077  * The name of the directory can always be found in the namecache or fetched
3078  * from the filesystem. There is also guaranteed to be only one parent, meaning
3079  * we can just follow vnodes up until we find the root.
3080  *
3081  * The vnode must be referenced.
3082  */
3083 static int
3084 vn_fullpath_dir(struct vnode *vp, struct vnode *rdir, char *buf, char **retbuf,
3085     size_t *len, size_t addend)
3086 {
3087 #ifdef KDTRACE_HOOKS
3088 	struct vnode *startvp = vp;
3089 #endif
3090 	struct vnode *vp1;
3091 	size_t buflen;
3092 	int error;
3093 	bool slash_prefixed;
3094 
3095 	VNPASS(vp->v_type == VDIR || VN_IS_DOOMED(vp), vp);
3096 	VNPASS(vp->v_usecount > 0, vp);
3097 
3098 	buflen = *len;
3099 
3100 	slash_prefixed = true;
3101 	if (addend == 0) {
3102 		MPASS(*len >= 2);
3103 		buflen--;
3104 		buf[buflen] = '\0';
3105 		slash_prefixed = false;
3106 	}
3107 
3108 	error = 0;
3109 
3110 	SDT_PROBE1(vfs, namecache, fullpath, entry, vp);
3111 	counter_u64_add(numfullpathcalls, 1);
3112 	while (vp != rdir && vp != rootvnode) {
3113 		/*
3114 		 * The vp vnode must be already fully constructed,
3115 		 * since it is either found in namecache or obtained
3116 		 * from VOP_VPTOCNP().  We may test for VV_ROOT safely
3117 		 * without obtaining the vnode lock.
3118 		 */
3119 		if ((vp->v_vflag & VV_ROOT) != 0) {
3120 			vn_lock(vp, LK_RETRY | LK_SHARED);
3121 
3122 			/*
3123 			 * With the vnode locked, check for races with
3124 			 * unmount, forced or not.  Note that we
3125 			 * already verified that vp is not equal to
3126 			 * the root vnode, which means that
3127 			 * mnt_vnodecovered can be NULL only for the
3128 			 * case of unmount.
3129 			 */
3130 			if (VN_IS_DOOMED(vp) ||
3131 			    (vp1 = vp->v_mount->mnt_vnodecovered) == NULL ||
3132 			    vp1->v_mountedhere != vp->v_mount) {
3133 				vput(vp);
3134 				error = ENOENT;
3135 				SDT_PROBE3(vfs, namecache, fullpath, return,
3136 				    error, vp, NULL);
3137 				break;
3138 			}
3139 
3140 			vref(vp1);
3141 			vput(vp);
3142 			vp = vp1;
3143 			continue;
3144 		}
3145 		if (vp->v_type != VDIR) {
3146 			vrele(vp);
3147 			counter_u64_add(numfullpathfail1, 1);
3148 			error = ENOTDIR;
3149 			SDT_PROBE3(vfs, namecache, fullpath, return,
3150 			    error, vp, NULL);
3151 			break;
3152 		}
3153 		error = vn_vptocnp(&vp, buf, &buflen);
3154 		if (error)
3155 			break;
3156 		if (buflen == 0) {
3157 			vrele(vp);
3158 			error = ENOMEM;
3159 			SDT_PROBE3(vfs, namecache, fullpath, return, error,
3160 			    startvp, NULL);
3161 			break;
3162 		}
3163 		buf[--buflen] = '/';
3164 		slash_prefixed = true;
3165 	}
3166 	if (error)
3167 		return (error);
3168 	if (!slash_prefixed) {
3169 		if (buflen == 0) {
3170 			vrele(vp);
3171 			counter_u64_add(numfullpathfail4, 1);
3172 			SDT_PROBE3(vfs, namecache, fullpath, return, ENOMEM,
3173 			    startvp, NULL);
3174 			return (ENOMEM);
3175 		}
3176 		buf[--buflen] = '/';
3177 	}
3178 	counter_u64_add(numfullpathfound, 1);
3179 	vrele(vp);
3180 
3181 	*retbuf = buf + buflen;
3182 	SDT_PROBE3(vfs, namecache, fullpath, return, 0, startvp, *retbuf);
3183 	*len -= buflen;
3184 	*len += addend;
3185 	return (0);
3186 }
3187 
3188 /*
3189  * Resolve an arbitrary vnode to a pathname.
3190  *
3191  * Note 2 caveats:
3192  * - hardlinks are not tracked, thus if the vnode is not a directory this can
3193  *   resolve to a different path than the one used to find it
3194  * - namecache is not mandatory, meaning names are not guaranteed to be added
3195  *   (in which case resolving fails)
3196  */
3197 static void __inline
3198 cache_rev_failed_impl(int *reason, int line)
3199 {
3200 
3201 	*reason = line;
3202 }
3203 #define cache_rev_failed(var)	cache_rev_failed_impl((var), __LINE__)
3204 
3205 static int
3206 vn_fullpath_any_smr(struct vnode *vp, struct vnode *rdir, char *buf,
3207     char **retbuf, size_t *buflen, size_t addend)
3208 {
3209 #ifdef KDTRACE_HOOKS
3210 	struct vnode *startvp = vp;
3211 #endif
3212 	struct vnode *tvp;
3213 	struct mount *mp;
3214 	struct namecache *ncp;
3215 	size_t orig_buflen;
3216 	int reason;
3217 	int error;
3218 #ifdef KDTRACE_HOOKS
3219 	int i;
3220 #endif
3221 	seqc_t vp_seqc, tvp_seqc;
3222 	u_char nc_flag;
3223 
3224 	VFS_SMR_ASSERT_ENTERED();
3225 
3226 	if (!cache_fast_revlookup) {
3227 		vfs_smr_exit();
3228 		return (-1);
3229 	}
3230 
3231 	orig_buflen = *buflen;
3232 
3233 	if (addend == 0) {
3234 		MPASS(*buflen >= 2);
3235 		*buflen -= 1;
3236 		buf[*buflen] = '\0';
3237 	}
3238 
3239 	if (vp == rdir || vp == rootvnode) {
3240 		if (addend == 0) {
3241 			*buflen -= 1;
3242 			buf[*buflen] = '/';
3243 		}
3244 		goto out_ok;
3245 	}
3246 
3247 #ifdef KDTRACE_HOOKS
3248 	i = 0;
3249 #endif
3250 	error = -1;
3251 	ncp = NULL; /* for sdt probe down below */
3252 	vp_seqc = vn_seqc_read_any(vp);
3253 	if (seqc_in_modify(vp_seqc)) {
3254 		cache_rev_failed(&reason);
3255 		goto out_abort;
3256 	}
3257 
3258 	for (;;) {
3259 #ifdef KDTRACE_HOOKS
3260 		i++;
3261 #endif
3262 		if ((vp->v_vflag & VV_ROOT) != 0) {
3263 			mp = atomic_load_ptr(&vp->v_mount);
3264 			if (mp == NULL) {
3265 				cache_rev_failed(&reason);
3266 				goto out_abort;
3267 			}
3268 			tvp = atomic_load_ptr(&mp->mnt_vnodecovered);
3269 			tvp_seqc = vn_seqc_read_any(tvp);
3270 			if (seqc_in_modify(tvp_seqc)) {
3271 				cache_rev_failed(&reason);
3272 				goto out_abort;
3273 			}
3274 			if (!vn_seqc_consistent(vp, vp_seqc)) {
3275 				cache_rev_failed(&reason);
3276 				goto out_abort;
3277 			}
3278 			vp = tvp;
3279 			vp_seqc = tvp_seqc;
3280 			continue;
3281 		}
3282 		ncp = atomic_load_consume_ptr(&vp->v_cache_dd);
3283 		if (ncp == NULL) {
3284 			cache_rev_failed(&reason);
3285 			goto out_abort;
3286 		}
3287 		nc_flag = atomic_load_char(&ncp->nc_flag);
3288 		if ((nc_flag & NCF_ISDOTDOT) != 0) {
3289 			cache_rev_failed(&reason);
3290 			goto out_abort;
3291 		}
3292 		if (ncp->nc_nlen >= *buflen) {
3293 			cache_rev_failed(&reason);
3294 			error = ENOMEM;
3295 			goto out_abort;
3296 		}
3297 		*buflen -= ncp->nc_nlen;
3298 		memcpy(buf + *buflen, ncp->nc_name, ncp->nc_nlen);
3299 		*buflen -= 1;
3300 		buf[*buflen] = '/';
3301 		tvp = ncp->nc_dvp;
3302 		tvp_seqc = vn_seqc_read_any(tvp);
3303 		if (seqc_in_modify(tvp_seqc)) {
3304 			cache_rev_failed(&reason);
3305 			goto out_abort;
3306 		}
3307 		if (!vn_seqc_consistent(vp, vp_seqc)) {
3308 			cache_rev_failed(&reason);
3309 			goto out_abort;
3310 		}
3311 		/*
3312 		 * Acquire fence provided by vn_seqc_read_any above.
3313 		 */
3314 		if (__predict_false(atomic_load_ptr(&vp->v_cache_dd) != ncp)) {
3315 			cache_rev_failed(&reason);
3316 			goto out_abort;
3317 		}
3318 		if (!cache_ncp_canuse(ncp)) {
3319 			cache_rev_failed(&reason);
3320 			goto out_abort;
3321 		}
3322 		vp = tvp;
3323 		vp_seqc = tvp_seqc;
3324 		if (vp == rdir || vp == rootvnode)
3325 			break;
3326 	}
3327 out_ok:
3328 	vfs_smr_exit();
3329 	*retbuf = buf + *buflen;
3330 	*buflen = orig_buflen - *buflen + addend;
3331 	SDT_PROBE2(vfs, namecache, fullpath_smr, hit, startvp, *retbuf);
3332 	return (0);
3333 
3334 out_abort:
3335 	*buflen = orig_buflen;
3336 	SDT_PROBE4(vfs, namecache, fullpath_smr, miss, startvp, ncp, reason, i);
3337 	vfs_smr_exit();
3338 	return (error);
3339 }
3340 
3341 static int
3342 vn_fullpath_any(struct vnode *vp, struct vnode *rdir, char *buf, char **retbuf,
3343     size_t *buflen)
3344 {
3345 	size_t orig_buflen, addend;
3346 	int error;
3347 
3348 	if (*buflen < 2)
3349 		return (EINVAL);
3350 
3351 	orig_buflen = *buflen;
3352 
3353 	vref(vp);
3354 	addend = 0;
3355 	if (vp->v_type != VDIR) {
3356 		*buflen -= 1;
3357 		buf[*buflen] = '\0';
3358 		error = vn_vptocnp(&vp, buf, buflen);
3359 		if (error)
3360 			return (error);
3361 		if (*buflen == 0) {
3362 			vrele(vp);
3363 			return (ENOMEM);
3364 		}
3365 		*buflen -= 1;
3366 		buf[*buflen] = '/';
3367 		addend = orig_buflen - *buflen;
3368 	}
3369 
3370 	return (vn_fullpath_dir(vp, rdir, buf, retbuf, buflen, addend));
3371 }
3372 
3373 /*
3374  * Resolve an arbitrary vnode to a pathname (taking care of hardlinks).
3375  *
3376  * Since the namecache does not track hardlinks, the caller is expected to first
3377  * look up the target vnode with SAVENAME | WANTPARENT flags passed to namei.
3378  *
3379  * Then we have 2 cases:
3380  * - if the found vnode is a directory, the path can be constructed just by
3381  *   following names up the chain
3382  * - otherwise we populate the buffer with the saved name and start resolving
3383  *   from the parent
3384  */
3385 static int
3386 vn_fullpath_hardlink(struct nameidata *ndp, char **retbuf, char **freebuf,
3387     size_t *buflen)
3388 {
3389 	char *buf, *tmpbuf;
3390 	struct pwd *pwd;
3391 	struct componentname *cnp;
3392 	struct vnode *vp;
3393 	size_t addend;
3394 	int error;
3395 	enum vtype type;
3396 
3397 	if (*buflen < 2)
3398 		return (EINVAL);
3399 	if (*buflen > MAXPATHLEN)
3400 		*buflen = MAXPATHLEN;
3401 
3402 	buf = malloc(*buflen, M_TEMP, M_WAITOK);
3403 
3404 	addend = 0;
3405 	vp = ndp->ni_vp;
3406 	/*
3407 	 * Check for VBAD to work around the vp_crossmp bug in lookup().
3408 	 *
3409 	 * For example consider tmpfs on /tmp and realpath /tmp. ni_vp will be
3410 	 * set to mount point's root vnode while ni_dvp will be vp_crossmp.
3411 	 * If the type is VDIR (like in this very case) we can skip looking
3412 	 * at ni_dvp in the first place. However, since vnodes get passed here
3413 	 * unlocked the target may transition to doomed state (type == VBAD)
3414 	 * before we get to evaluate the condition. If this happens, we will
3415 	 * populate part of the buffer and descend to vn_fullpath_dir with
3416 	 * vp == vp_crossmp. Prevent the problem by checking for VBAD.
3417 	 *
3418 	 * This should be atomic_load(&vp->v_type) but it is illegal to take
3419 	 * an address of a bit field, even if said field is sized to char.
3420 	 * Work around the problem by reading the value into a full-sized enum
3421 	 * and then re-reading it with atomic_load which will still prevent
3422 	 * the compiler from re-reading down the road.
3423 	 */
3424 	type = vp->v_type;
3425 	type = atomic_load_int(&type);
3426 	if (type == VBAD) {
3427 		error = ENOENT;
3428 		goto out_bad;
3429 	}
3430 	if (type != VDIR) {
3431 		cnp = &ndp->ni_cnd;
3432 		addend = cnp->cn_namelen + 2;
3433 		if (*buflen < addend) {
3434 			error = ENOMEM;
3435 			goto out_bad;
3436 		}
3437 		*buflen -= addend;
3438 		tmpbuf = buf + *buflen;
3439 		tmpbuf[0] = '/';
3440 		memcpy(&tmpbuf[1], cnp->cn_nameptr, cnp->cn_namelen);
3441 		tmpbuf[addend - 1] = '\0';
3442 		vp = ndp->ni_dvp;
3443 	}
3444 
3445 	vfs_smr_enter();
3446 	pwd = pwd_get_smr();
3447 	error = vn_fullpath_any_smr(vp, pwd->pwd_rdir, buf, retbuf, buflen,
3448 	    addend);
3449 	VFS_SMR_ASSERT_NOT_ENTERED();
3450 	if (error < 0) {
3451 		pwd = pwd_hold(curthread);
3452 		vref(vp);
3453 		error = vn_fullpath_dir(vp, pwd->pwd_rdir, buf, retbuf, buflen,
3454 		    addend);
3455 		pwd_drop(pwd);
3456 		if (error != 0)
3457 			goto out_bad;
3458 	}
3459 
3460 	*freebuf = buf;
3461 
3462 	return (0);
3463 out_bad:
3464 	free(buf, M_TEMP);
3465 	return (error);
3466 }
3467 
3468 struct vnode *
3469 vn_dir_dd_ino(struct vnode *vp)
3470 {
3471 	struct namecache *ncp;
3472 	struct vnode *ddvp;
3473 	struct mtx *vlp;
3474 	enum vgetstate vs;
3475 
3476 	ASSERT_VOP_LOCKED(vp, "vn_dir_dd_ino");
3477 	vlp = VP2VNODELOCK(vp);
3478 	mtx_lock(vlp);
3479 	TAILQ_FOREACH(ncp, &(vp->v_cache_dst), nc_dst) {
3480 		if ((ncp->nc_flag & NCF_ISDOTDOT) != 0)
3481 			continue;
3482 		ddvp = ncp->nc_dvp;
3483 		vs = vget_prep(ddvp);
3484 		mtx_unlock(vlp);
3485 		if (vget_finish(ddvp, LK_SHARED | LK_NOWAIT, vs))
3486 			return (NULL);
3487 		return (ddvp);
3488 	}
3489 	mtx_unlock(vlp);
3490 	return (NULL);
3491 }
3492 
3493 int
3494 vn_commname(struct vnode *vp, char *buf, u_int buflen)
3495 {
3496 	struct namecache *ncp;
3497 	struct mtx *vlp;
3498 	int l;
3499 
3500 	vlp = VP2VNODELOCK(vp);
3501 	mtx_lock(vlp);
3502 	TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst)
3503 		if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
3504 			break;
3505 	if (ncp == NULL) {
3506 		mtx_unlock(vlp);
3507 		return (ENOENT);
3508 	}
3509 	l = min(ncp->nc_nlen, buflen - 1);
3510 	memcpy(buf, ncp->nc_name, l);
3511 	mtx_unlock(vlp);
3512 	buf[l] = '\0';
3513 	return (0);
3514 }
3515 
3516 /*
3517  * This function updates path string to vnode's full global path
3518  * and checks the size of the new path string against the pathlen argument.
3519  *
3520  * Requires a locked, referenced vnode.
3521  * Vnode is re-locked on success or ENODEV, otherwise unlocked.
3522  *
3523  * If vp is a directory, the call to vn_fullpath_global() always succeeds
3524  * because it falls back to the ".." lookup if the namecache lookup fails.
3525  */
3526 int
3527 vn_path_to_global_path(struct thread *td, struct vnode *vp, char *path,
3528     u_int pathlen)
3529 {
3530 	struct nameidata nd;
3531 	struct vnode *vp1;
3532 	char *rpath, *fbuf;
3533 	int error;
3534 
3535 	ASSERT_VOP_ELOCKED(vp, __func__);
3536 
3537 	/* Construct global filesystem path from vp. */
3538 	VOP_UNLOCK(vp);
3539 	error = vn_fullpath_global(vp, &rpath, &fbuf);
3540 
3541 	if (error != 0) {
3542 		vrele(vp);
3543 		return (error);
3544 	}
3545 
3546 	if (strlen(rpath) >= pathlen) {
3547 		vrele(vp);
3548 		error = ENAMETOOLONG;
3549 		goto out;
3550 	}
3551 
3552 	/*
3553 	 * Re-lookup the vnode by path to detect a possible rename.
3554 	 * As a side effect, the vnode is relocked.
3555 	 * If vnode was renamed, return ENOENT.
3556 	 */
3557 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
3558 	    UIO_SYSSPACE, path, td);
3559 	error = namei(&nd);
3560 	if (error != 0) {
3561 		vrele(vp);
3562 		goto out;
3563 	}
3564 	NDFREE(&nd, NDF_ONLY_PNBUF);
3565 	vp1 = nd.ni_vp;
3566 	vrele(vp);
3567 	if (vp1 == vp)
3568 		strcpy(path, rpath);
3569 	else {
3570 		vput(vp1);
3571 		error = ENOENT;
3572 	}
3573 
3574 out:
3575 	free(fbuf, M_TEMP);
3576 	return (error);
3577 }
3578 
3579 #ifdef DDB
3580 static void
3581 db_print_vpath(struct vnode *vp)
3582 {
3583 
3584 	while (vp != NULL) {
3585 		db_printf("%p: ", vp);
3586 		if (vp == rootvnode) {
3587 			db_printf("/");
3588 			vp = NULL;
3589 		} else {
3590 			if (vp->v_vflag & VV_ROOT) {
3591 				db_printf("<mount point>");
3592 				vp = vp->v_mount->mnt_vnodecovered;
3593 			} else {
3594 				struct namecache *ncp;
3595 				char *ncn;
3596 				int i;
3597 
3598 				ncp = TAILQ_FIRST(&vp->v_cache_dst);
3599 				if (ncp != NULL) {
3600 					ncn = ncp->nc_name;
3601 					for (i = 0; i < ncp->nc_nlen; i++)
3602 						db_printf("%c", *ncn++);
3603 					vp = ncp->nc_dvp;
3604 				} else {
3605 					vp = NULL;
3606 				}
3607 			}
3608 		}
3609 		db_printf("\n");
3610 	}
3611 
3612 	return;
3613 }
3614 
3615 DB_SHOW_COMMAND(vpath, db_show_vpath)
3616 {
3617 	struct vnode *vp;
3618 
3619 	if (!have_addr) {
3620 		db_printf("usage: show vpath <struct vnode *>\n");
3621 		return;
3622 	}
3623 
3624 	vp = (struct vnode *)addr;
3625 	db_print_vpath(vp);
3626 }
3627 
3628 #endif
3629 
3630 static int cache_fast_lookup = 1;
3631 static char __read_frequently cache_fast_lookup_enabled = true;
3632 
3633 #define CACHE_FPL_FAILED	-2020
3634 
3635 void
3636 cache_fast_lookup_enabled_recalc(void)
3637 {
3638 	int lookup_flag;
3639 	int mac_on;
3640 
3641 #ifdef MAC
3642 	mac_on = mac_vnode_check_lookup_enabled();
3643 	mac_on |= mac_vnode_check_readlink_enabled();
3644 #else
3645 	mac_on = 0;
3646 #endif
3647 
3648 	lookup_flag = atomic_load_int(&cache_fast_lookup);
3649 	if (lookup_flag && !mac_on) {
3650 		atomic_store_char(&cache_fast_lookup_enabled, true);
3651 	} else {
3652 		atomic_store_char(&cache_fast_lookup_enabled, false);
3653 	}
3654 }
3655 
3656 static int
3657 syscal_vfs_cache_fast_lookup(SYSCTL_HANDLER_ARGS)
3658 {
3659 	int error, old;
3660 
3661 	old = atomic_load_int(&cache_fast_lookup);
3662 	error = sysctl_handle_int(oidp, arg1, arg2, req);
3663 	if (error == 0 && req->newptr && old != atomic_load_int(&cache_fast_lookup))
3664 		cache_fast_lookup_enabled_recalc();
3665 	return (error);
3666 }
3667 SYSCTL_PROC(_vfs, OID_AUTO, cache_fast_lookup, CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_MPSAFE,
3668     &cache_fast_lookup, 0, syscal_vfs_cache_fast_lookup, "IU", "");
3669 
3670 /*
3671  * Components of nameidata (or objects it can point to) which may
3672  * need restoring in case fast path lookup fails.
3673  */
3674 struct nameidata_outer {
3675 	size_t ni_pathlen;
3676 	int cn_flags;
3677 };
3678 
3679 struct nameidata_saved {
3680 #ifdef INVARIANTS
3681 	char *cn_nameptr;
3682 	size_t ni_pathlen;
3683 #endif
3684 };
3685 
3686 #ifdef INVARIANTS
3687 struct cache_fpl_debug {
3688 	size_t ni_pathlen;
3689 };
3690 #endif
3691 
3692 struct cache_fpl {
3693 	struct nameidata *ndp;
3694 	struct componentname *cnp;
3695 	char *nulchar;
3696 	struct pwd **pwd;
3697 	struct vnode *dvp;
3698 	struct vnode *tvp;
3699 	seqc_t dvp_seqc;
3700 	seqc_t tvp_seqc;
3701 	struct nameidata_saved snd;
3702 	struct nameidata_outer snd_outer;
3703 	int line;
3704 	enum cache_fpl_status status:8;
3705 	bool in_smr;
3706 	bool fsearch;
3707 	bool savename;
3708 #ifdef INVARIANTS
3709 	struct cache_fpl_debug debug;
3710 #endif
3711 };
3712 
3713 static bool cache_fplookup_is_mp(struct cache_fpl *fpl);
3714 static int cache_fplookup_cross_mount(struct cache_fpl *fpl);
3715 static int cache_fplookup_partial_setup(struct cache_fpl *fpl);
3716 static int cache_fplookup_skip_slashes(struct cache_fpl *fpl);
3717 static int cache_fplookup_trailingslash(struct cache_fpl *fpl);
3718 static int cache_fplookup_preparse(struct cache_fpl *fpl);
3719 static void cache_fpl_pathlen_dec(struct cache_fpl *fpl);
3720 static void cache_fpl_pathlen_inc(struct cache_fpl *fpl);
3721 static void cache_fpl_pathlen_add(struct cache_fpl *fpl, size_t n);
3722 static void cache_fpl_pathlen_sub(struct cache_fpl *fpl, size_t n);
3723 
3724 static void
3725 cache_fpl_cleanup_cnp(struct componentname *cnp)
3726 {
3727 
3728 	uma_zfree(namei_zone, cnp->cn_pnbuf);
3729 #ifdef DIAGNOSTIC
3730 	cnp->cn_pnbuf = NULL;
3731 	cnp->cn_nameptr = NULL;
3732 #endif
3733 }
3734 
3735 static struct vnode *
3736 cache_fpl_handle_root(struct cache_fpl *fpl)
3737 {
3738 	struct nameidata *ndp;
3739 	struct componentname *cnp;
3740 
3741 	ndp = fpl->ndp;
3742 	cnp = fpl->cnp;
3743 
3744 	MPASS(*(cnp->cn_nameptr) == '/');
3745 	cnp->cn_nameptr++;
3746 	cache_fpl_pathlen_dec(fpl);
3747 
3748 	if (__predict_false(*(cnp->cn_nameptr) == '/')) {
3749 		do {
3750 			cnp->cn_nameptr++;
3751 			cache_fpl_pathlen_dec(fpl);
3752 		} while (*(cnp->cn_nameptr) == '/');
3753 	}
3754 
3755 	return (ndp->ni_rootdir);
3756 }
3757 
3758 static void
3759 cache_fpl_checkpoint_outer(struct cache_fpl *fpl)
3760 {
3761 
3762 	fpl->snd_outer.ni_pathlen = fpl->ndp->ni_pathlen;
3763 	fpl->snd_outer.cn_flags = fpl->ndp->ni_cnd.cn_flags;
3764 }
3765 
3766 static void
3767 cache_fpl_checkpoint(struct cache_fpl *fpl)
3768 {
3769 
3770 #ifdef INVARIANTS
3771 	fpl->snd.cn_nameptr = fpl->ndp->ni_cnd.cn_nameptr;
3772 	fpl->snd.ni_pathlen = fpl->debug.ni_pathlen;
3773 #endif
3774 }
3775 
3776 static void
3777 cache_fpl_restore_partial(struct cache_fpl *fpl)
3778 {
3779 
3780 	fpl->ndp->ni_cnd.cn_flags = fpl->snd_outer.cn_flags;
3781 #ifdef INVARIANTS
3782 	fpl->debug.ni_pathlen = fpl->snd.ni_pathlen;
3783 #endif
3784 }
3785 
3786 static void
3787 cache_fpl_restore_abort(struct cache_fpl *fpl)
3788 {
3789 
3790 	cache_fpl_restore_partial(fpl);
3791 	/*
3792 	 * It is 0 on entry by API contract.
3793 	 */
3794 	fpl->ndp->ni_resflags = 0;
3795 	fpl->ndp->ni_cnd.cn_nameptr = fpl->ndp->ni_cnd.cn_pnbuf;
3796 	fpl->ndp->ni_pathlen = fpl->snd_outer.ni_pathlen;
3797 }
3798 
3799 #ifdef INVARIANTS
3800 #define cache_fpl_smr_assert_entered(fpl) ({			\
3801 	struct cache_fpl *_fpl = (fpl);				\
3802 	MPASS(_fpl->in_smr == true);				\
3803 	VFS_SMR_ASSERT_ENTERED();				\
3804 })
3805 #define cache_fpl_smr_assert_not_entered(fpl) ({		\
3806 	struct cache_fpl *_fpl = (fpl);				\
3807 	MPASS(_fpl->in_smr == false);				\
3808 	VFS_SMR_ASSERT_NOT_ENTERED();				\
3809 })
3810 static void
3811 cache_fpl_assert_status(struct cache_fpl *fpl)
3812 {
3813 
3814 	switch (fpl->status) {
3815 	case CACHE_FPL_STATUS_UNSET:
3816 		__assert_unreachable();
3817 		break;
3818 	case CACHE_FPL_STATUS_DESTROYED:
3819 	case CACHE_FPL_STATUS_ABORTED:
3820 	case CACHE_FPL_STATUS_PARTIAL:
3821 	case CACHE_FPL_STATUS_HANDLED:
3822 		break;
3823 	}
3824 }
3825 #else
3826 #define cache_fpl_smr_assert_entered(fpl) do { } while (0)
3827 #define cache_fpl_smr_assert_not_entered(fpl) do { } while (0)
3828 #define cache_fpl_assert_status(fpl) do { } while (0)
3829 #endif
3830 
3831 #define cache_fpl_smr_enter_initial(fpl) ({			\
3832 	struct cache_fpl *_fpl = (fpl);				\
3833 	vfs_smr_enter();					\
3834 	_fpl->in_smr = true;					\
3835 })
3836 
3837 #define cache_fpl_smr_enter(fpl) ({				\
3838 	struct cache_fpl *_fpl = (fpl);				\
3839 	MPASS(_fpl->in_smr == false);				\
3840 	vfs_smr_enter();					\
3841 	_fpl->in_smr = true;					\
3842 })
3843 
3844 #define cache_fpl_smr_exit(fpl) ({				\
3845 	struct cache_fpl *_fpl = (fpl);				\
3846 	MPASS(_fpl->in_smr == true);				\
3847 	vfs_smr_exit();						\
3848 	_fpl->in_smr = false;					\
3849 })
3850 
3851 static int
3852 cache_fpl_aborted_early_impl(struct cache_fpl *fpl, int line)
3853 {
3854 
3855 	if (fpl->status != CACHE_FPL_STATUS_UNSET) {
3856 		KASSERT(fpl->status == CACHE_FPL_STATUS_PARTIAL,
3857 		    ("%s: converting to abort from %d at %d, set at %d\n",
3858 		    __func__, fpl->status, line, fpl->line));
3859 	}
3860 	cache_fpl_smr_assert_not_entered(fpl);
3861 	fpl->status = CACHE_FPL_STATUS_ABORTED;
3862 	fpl->line = line;
3863 	return (CACHE_FPL_FAILED);
3864 }
3865 
3866 #define cache_fpl_aborted_early(x)	cache_fpl_aborted_early_impl((x), __LINE__)
3867 
3868 static int __noinline
3869 cache_fpl_aborted_impl(struct cache_fpl *fpl, int line)
3870 {
3871 	struct nameidata *ndp;
3872 	struct componentname *cnp;
3873 
3874 	ndp = fpl->ndp;
3875 	cnp = fpl->cnp;
3876 
3877 	if (fpl->status != CACHE_FPL_STATUS_UNSET) {
3878 		KASSERT(fpl->status == CACHE_FPL_STATUS_PARTIAL,
3879 		    ("%s: converting to abort from %d at %d, set at %d\n",
3880 		    __func__, fpl->status, line, fpl->line));
3881 	}
3882 	fpl->status = CACHE_FPL_STATUS_ABORTED;
3883 	fpl->line = line;
3884 	if (fpl->in_smr)
3885 		cache_fpl_smr_exit(fpl);
3886 	cache_fpl_restore_abort(fpl);
3887 	/*
3888 	 * Resolving symlinks overwrites data passed by the caller.
3889 	 * Let namei know.
3890 	 */
3891 	if (ndp->ni_loopcnt > 0) {
3892 		fpl->status = CACHE_FPL_STATUS_DESTROYED;
3893 		cache_fpl_cleanup_cnp(cnp);
3894 	}
3895 	return (CACHE_FPL_FAILED);
3896 }
3897 
3898 #define cache_fpl_aborted(x)	cache_fpl_aborted_impl((x), __LINE__)
3899 
3900 static int __noinline
3901 cache_fpl_partial_impl(struct cache_fpl *fpl, int line)
3902 {
3903 
3904 	KASSERT(fpl->status == CACHE_FPL_STATUS_UNSET,
3905 	    ("%s: setting to partial at %d, but already set to %d at %d\n",
3906 	    __func__, line, fpl->status, fpl->line));
3907 	cache_fpl_smr_assert_entered(fpl);
3908 	fpl->status = CACHE_FPL_STATUS_PARTIAL;
3909 	fpl->line = line;
3910 	return (cache_fplookup_partial_setup(fpl));
3911 }
3912 
3913 #define cache_fpl_partial(x)	cache_fpl_partial_impl((x), __LINE__)
3914 
3915 static int
3916 cache_fpl_handled_impl(struct cache_fpl *fpl, int line)
3917 {
3918 
3919 	KASSERT(fpl->status == CACHE_FPL_STATUS_UNSET,
3920 	    ("%s: setting to handled at %d, but already set to %d at %d\n",
3921 	    __func__, line, fpl->status, fpl->line));
3922 	cache_fpl_smr_assert_not_entered(fpl);
3923 	fpl->status = CACHE_FPL_STATUS_HANDLED;
3924 	fpl->line = line;
3925 	return (0);
3926 }
3927 
3928 #define cache_fpl_handled(x)	cache_fpl_handled_impl((x), __LINE__)
3929 
3930 static int
3931 cache_fpl_handled_error_impl(struct cache_fpl *fpl, int error, int line)
3932 {
3933 
3934 	KASSERT(fpl->status == CACHE_FPL_STATUS_UNSET,
3935 	    ("%s: setting to handled at %d, but already set to %d at %d\n",
3936 	    __func__, line, fpl->status, fpl->line));
3937 	MPASS(error != 0);
3938 	MPASS(error != CACHE_FPL_FAILED);
3939 	cache_fpl_smr_assert_not_entered(fpl);
3940 	fpl->status = CACHE_FPL_STATUS_HANDLED;
3941 	fpl->line = line;
3942 	fpl->dvp = NULL;
3943 	fpl->tvp = NULL;
3944 	fpl->savename = false;
3945 	return (error);
3946 }
3947 
3948 #define cache_fpl_handled_error(x, e)	cache_fpl_handled_error_impl((x), (e), __LINE__)
3949 
3950 static bool
3951 cache_fpl_terminated(struct cache_fpl *fpl)
3952 {
3953 
3954 	return (fpl->status != CACHE_FPL_STATUS_UNSET);
3955 }
3956 
3957 #define CACHE_FPL_SUPPORTED_CN_FLAGS \
3958 	(NC_NOMAKEENTRY | NC_KEEPPOSENTRY | LOCKLEAF | LOCKPARENT | WANTPARENT | \
3959 	 FAILIFEXISTS | FOLLOW | LOCKSHARED | SAVENAME | SAVESTART | WILLBEDIR | \
3960 	 ISOPEN | NOMACCHECK | AUDITVNODE1 | AUDITVNODE2 | NOCAPCHECK)
3961 
3962 #define CACHE_FPL_INTERNAL_CN_FLAGS \
3963 	(ISDOTDOT | MAKEENTRY | ISLASTCN)
3964 
3965 _Static_assert((CACHE_FPL_SUPPORTED_CN_FLAGS & CACHE_FPL_INTERNAL_CN_FLAGS) == 0,
3966     "supported and internal flags overlap");
3967 
3968 static bool
3969 cache_fpl_islastcn(struct nameidata *ndp)
3970 {
3971 
3972 	return (*ndp->ni_next == 0);
3973 }
3974 
3975 static bool
3976 cache_fpl_istrailingslash(struct cache_fpl *fpl)
3977 {
3978 
3979 	return (*(fpl->nulchar - 1) == '/');
3980 }
3981 
3982 static bool
3983 cache_fpl_isdotdot(struct componentname *cnp)
3984 {
3985 
3986 	if (cnp->cn_namelen == 2 &&
3987 	    cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
3988 		return (true);
3989 	return (false);
3990 }
3991 
3992 static bool
3993 cache_can_fplookup(struct cache_fpl *fpl)
3994 {
3995 	struct nameidata *ndp;
3996 	struct componentname *cnp;
3997 	struct thread *td;
3998 
3999 	ndp = fpl->ndp;
4000 	cnp = fpl->cnp;
4001 	td = cnp->cn_thread;
4002 
4003 	if (!atomic_load_char(&cache_fast_lookup_enabled)) {
4004 		cache_fpl_aborted_early(fpl);
4005 		return (false);
4006 	}
4007 	if ((cnp->cn_flags & ~CACHE_FPL_SUPPORTED_CN_FLAGS) != 0) {
4008 		cache_fpl_aborted_early(fpl);
4009 		return (false);
4010 	}
4011 	if (IN_CAPABILITY_MODE(td)) {
4012 		cache_fpl_aborted_early(fpl);
4013 		return (false);
4014 	}
4015 	if (AUDITING_TD(td)) {
4016 		cache_fpl_aborted_early(fpl);
4017 		return (false);
4018 	}
4019 	if (ndp->ni_startdir != NULL) {
4020 		cache_fpl_aborted_early(fpl);
4021 		return (false);
4022 	}
4023 	return (true);
4024 }
4025 
4026 static int
4027 cache_fplookup_dirfd(struct cache_fpl *fpl, struct vnode **vpp)
4028 {
4029 	struct nameidata *ndp;
4030 	int error;
4031 	bool fsearch;
4032 
4033 	ndp = fpl->ndp;
4034 	error = fgetvp_lookup_smr(ndp->ni_dirfd, ndp, vpp, &fsearch);
4035 	if (__predict_false(error != 0)) {
4036 		return (cache_fpl_aborted(fpl));
4037 	}
4038 	fpl->fsearch = fsearch;
4039 	return (0);
4040 }
4041 
4042 static int __noinline
4043 cache_fplookup_negative_promote(struct cache_fpl *fpl, struct namecache *oncp,
4044     uint32_t hash)
4045 {
4046 	struct componentname *cnp;
4047 	struct vnode *dvp;
4048 
4049 	cnp = fpl->cnp;
4050 	dvp = fpl->dvp;
4051 
4052 	cache_fpl_smr_exit(fpl);
4053 	if (cache_neg_promote_cond(dvp, cnp, oncp, hash))
4054 		return (cache_fpl_handled_error(fpl, ENOENT));
4055 	else
4056 		return (cache_fpl_aborted(fpl));
4057 }
4058 
4059 /*
4060  * The target vnode is not supported, prepare for the slow path to take over.
4061  */
4062 static int __noinline
4063 cache_fplookup_partial_setup(struct cache_fpl *fpl)
4064 {
4065 	struct nameidata *ndp;
4066 	struct componentname *cnp;
4067 	enum vgetstate dvs;
4068 	struct vnode *dvp;
4069 	struct pwd *pwd;
4070 	seqc_t dvp_seqc;
4071 
4072 	ndp = fpl->ndp;
4073 	cnp = fpl->cnp;
4074 	pwd = *(fpl->pwd);
4075 	dvp = fpl->dvp;
4076 	dvp_seqc = fpl->dvp_seqc;
4077 
4078 	if (!pwd_hold_smr(pwd)) {
4079 		return (cache_fpl_aborted(fpl));
4080 	}
4081 
4082 	/*
4083 	 * Note that seqc is checked before the vnode is locked, so by
4084 	 * the time regular lookup gets to it it may have moved.
4085 	 *
4086 	 * Ultimately this does not affect correctness, any lookup errors
4087 	 * are userspace racing with itself. It is guaranteed that any
4088 	 * path which ultimately gets found could also have been found
4089 	 * by regular lookup going all the way in absence of concurrent
4090 	 * modifications.
4091 	 */
4092 	dvs = vget_prep_smr(dvp);
4093 	cache_fpl_smr_exit(fpl);
4094 	if (__predict_false(dvs == VGET_NONE)) {
4095 		pwd_drop(pwd);
4096 		return (cache_fpl_aborted(fpl));
4097 	}
4098 
4099 	vget_finish_ref(dvp, dvs);
4100 	if (!vn_seqc_consistent(dvp, dvp_seqc)) {
4101 		vrele(dvp);
4102 		pwd_drop(pwd);
4103 		return (cache_fpl_aborted(fpl));
4104 	}
4105 
4106 	cache_fpl_restore_partial(fpl);
4107 #ifdef INVARIANTS
4108 	if (cnp->cn_nameptr != fpl->snd.cn_nameptr) {
4109 		panic("%s: cn_nameptr mismatch (%p != %p) full [%s]\n", __func__,
4110 		    cnp->cn_nameptr, fpl->snd.cn_nameptr, cnp->cn_pnbuf);
4111 	}
4112 #endif
4113 
4114 	ndp->ni_startdir = dvp;
4115 	cnp->cn_flags |= MAKEENTRY;
4116 	if (cache_fpl_islastcn(ndp))
4117 		cnp->cn_flags |= ISLASTCN;
4118 	if (cache_fpl_isdotdot(cnp))
4119 		cnp->cn_flags |= ISDOTDOT;
4120 
4121 	/*
4122 	 * Skip potential extra slashes parsing did not take care of.
4123 	 * cache_fplookup_skip_slashes explains the mechanism.
4124 	 */
4125 	if (__predict_false(*(cnp->cn_nameptr) == '/')) {
4126 		do {
4127 			cnp->cn_nameptr++;
4128 			cache_fpl_pathlen_dec(fpl);
4129 		} while (*(cnp->cn_nameptr) == '/');
4130 	}
4131 
4132 	ndp->ni_pathlen = fpl->nulchar - cnp->cn_nameptr + 1;
4133 #ifdef INVARIANTS
4134 	if (ndp->ni_pathlen != fpl->debug.ni_pathlen) {
4135 		panic("%s: mismatch (%zu != %zu) nulchar %p nameptr %p [%s] ; full string [%s]\n",
4136 		    __func__, ndp->ni_pathlen, fpl->debug.ni_pathlen, fpl->nulchar,
4137 		    cnp->cn_nameptr, cnp->cn_nameptr, cnp->cn_pnbuf);
4138 	}
4139 #endif
4140 	return (0);
4141 }
4142 
4143 static int
4144 cache_fplookup_final_child(struct cache_fpl *fpl, enum vgetstate tvs)
4145 {
4146 	struct componentname *cnp;
4147 	struct vnode *tvp;
4148 	seqc_t tvp_seqc;
4149 	int error, lkflags;
4150 
4151 	cnp = fpl->cnp;
4152 	tvp = fpl->tvp;
4153 	tvp_seqc = fpl->tvp_seqc;
4154 
4155 	if ((cnp->cn_flags & LOCKLEAF) != 0) {
4156 		lkflags = LK_SHARED;
4157 		if ((cnp->cn_flags & LOCKSHARED) == 0)
4158 			lkflags = LK_EXCLUSIVE;
4159 		error = vget_finish(tvp, lkflags, tvs);
4160 		if (__predict_false(error != 0)) {
4161 			return (cache_fpl_aborted(fpl));
4162 		}
4163 	} else {
4164 		vget_finish_ref(tvp, tvs);
4165 	}
4166 
4167 	if (!vn_seqc_consistent(tvp, tvp_seqc)) {
4168 		if ((cnp->cn_flags & LOCKLEAF) != 0)
4169 			vput(tvp);
4170 		else
4171 			vrele(tvp);
4172 		return (cache_fpl_aborted(fpl));
4173 	}
4174 
4175 	return (cache_fpl_handled(fpl));
4176 }
4177 
4178 /*
4179  * They want to possibly modify the state of the namecache.
4180  */
4181 static int __noinline
4182 cache_fplookup_final_modifying(struct cache_fpl *fpl)
4183 {
4184 	struct nameidata *ndp;
4185 	struct componentname *cnp;
4186 	enum vgetstate dvs;
4187 	struct vnode *dvp, *tvp;
4188 	struct mount *mp;
4189 	seqc_t dvp_seqc;
4190 	int error;
4191 	bool docache;
4192 
4193 	ndp = fpl->ndp;
4194 	cnp = fpl->cnp;
4195 	dvp = fpl->dvp;
4196 	dvp_seqc = fpl->dvp_seqc;
4197 
4198 	MPASS(*(cnp->cn_nameptr) != '/');
4199 	MPASS(cache_fpl_islastcn(ndp));
4200 	if ((cnp->cn_flags & LOCKPARENT) == 0)
4201 		MPASS((cnp->cn_flags & WANTPARENT) != 0);
4202 	MPASS((cnp->cn_flags & TRAILINGSLASH) == 0);
4203 	MPASS(cnp->cn_nameiop == CREATE || cnp->cn_nameiop == DELETE ||
4204 	    cnp->cn_nameiop == RENAME);
4205 	MPASS((cnp->cn_flags & MAKEENTRY) == 0);
4206 	MPASS((cnp->cn_flags & ISDOTDOT) == 0);
4207 
4208 	docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
4209 	if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
4210 		docache = false;
4211 
4212 	/*
4213 	 * Regular lookup nulifies the slash, which we don't do here.
4214 	 * Don't take chances with filesystem routines seeing it for
4215 	 * the last entry.
4216 	 */
4217 	if (cache_fpl_istrailingslash(fpl)) {
4218 		return (cache_fpl_partial(fpl));
4219 	}
4220 
4221 	mp = atomic_load_ptr(&dvp->v_mount);
4222 	if (__predict_false(mp == NULL)) {
4223 		return (cache_fpl_aborted(fpl));
4224 	}
4225 
4226 	if (__predict_false(mp->mnt_flag & MNT_RDONLY)) {
4227 		cache_fpl_smr_exit(fpl);
4228 		/*
4229 		 * Original code keeps not checking for CREATE which
4230 		 * might be a bug. For now let the old lookup decide.
4231 		 */
4232 		if (cnp->cn_nameiop == CREATE) {
4233 			return (cache_fpl_aborted(fpl));
4234 		}
4235 		return (cache_fpl_handled_error(fpl, EROFS));
4236 	}
4237 
4238 	if (fpl->tvp != NULL && (cnp->cn_flags & FAILIFEXISTS) != 0) {
4239 		cache_fpl_smr_exit(fpl);
4240 		return (cache_fpl_handled_error(fpl, EEXIST));
4241 	}
4242 
4243 	/*
4244 	 * Secure access to dvp; check cache_fplookup_partial_setup for
4245 	 * reasoning.
4246 	 *
4247 	 * XXX At least UFS requires its lookup routine to be called for
4248 	 * the last path component, which leads to some level of complication
4249 	 * and inefficiency:
4250 	 * - the target routine always locks the target vnode, but our caller
4251 	 *   may not need it locked
4252 	 * - some of the VOP machinery asserts that the parent is locked, which
4253 	 *   once more may be not required
4254 	 *
4255 	 * TODO: add a flag for filesystems which don't need this.
4256 	 */
4257 	dvs = vget_prep_smr(dvp);
4258 	cache_fpl_smr_exit(fpl);
4259 	if (__predict_false(dvs == VGET_NONE)) {
4260 		return (cache_fpl_aborted(fpl));
4261 	}
4262 
4263 	vget_finish_ref(dvp, dvs);
4264 	if (!vn_seqc_consistent(dvp, dvp_seqc)) {
4265 		vrele(dvp);
4266 		return (cache_fpl_aborted(fpl));
4267 	}
4268 
4269 	error = vn_lock(dvp, LK_EXCLUSIVE);
4270 	if (__predict_false(error != 0)) {
4271 		vrele(dvp);
4272 		return (cache_fpl_aborted(fpl));
4273 	}
4274 
4275 	tvp = NULL;
4276 	cnp->cn_flags |= ISLASTCN;
4277 	if (docache)
4278 		cnp->cn_flags |= MAKEENTRY;
4279 	if (cache_fpl_isdotdot(cnp))
4280 		cnp->cn_flags |= ISDOTDOT;
4281 	cnp->cn_lkflags = LK_EXCLUSIVE;
4282 	error = VOP_LOOKUP(dvp, &tvp, cnp);
4283 	switch (error) {
4284 	case EJUSTRETURN:
4285 	case 0:
4286 		break;
4287 	case ENOTDIR:
4288 	case ENOENT:
4289 		vput(dvp);
4290 		return (cache_fpl_handled_error(fpl, error));
4291 	default:
4292 		vput(dvp);
4293 		return (cache_fpl_aborted(fpl));
4294 	}
4295 
4296 	fpl->tvp = tvp;
4297 	fpl->savename = (cnp->cn_flags & SAVENAME) != 0;
4298 
4299 	if (tvp == NULL) {
4300 		if ((cnp->cn_flags & SAVESTART) != 0) {
4301 			ndp->ni_startdir = dvp;
4302 			vrefact(ndp->ni_startdir);
4303 			cnp->cn_flags |= SAVENAME;
4304 			fpl->savename = true;
4305 		}
4306 		MPASS(error == EJUSTRETURN);
4307 		if ((cnp->cn_flags & LOCKPARENT) == 0) {
4308 			VOP_UNLOCK(dvp);
4309 		}
4310 		return (cache_fpl_handled(fpl));
4311 	}
4312 
4313 	/*
4314 	 * There are very hairy corner cases concerning various flag combinations
4315 	 * and locking state. In particular here we only hold one lock instead of
4316 	 * two.
4317 	 *
4318 	 * Skip the complexity as it is of no significance for normal workloads.
4319 	 */
4320 	if (__predict_false(tvp == dvp)) {
4321 		vput(dvp);
4322 		vrele(tvp);
4323 		return (cache_fpl_aborted(fpl));
4324 	}
4325 
4326 	/*
4327 	 * If they want the symlink itself we are fine, but if they want to
4328 	 * follow it regular lookup has to be engaged.
4329 	 */
4330 	if (tvp->v_type == VLNK) {
4331 		if ((cnp->cn_flags & FOLLOW) != 0) {
4332 			vput(dvp);
4333 			vput(tvp);
4334 			return (cache_fpl_aborted(fpl));
4335 		}
4336 	}
4337 
4338 	/*
4339 	 * Since we expect this to be the terminal vnode it should almost never
4340 	 * be a mount point.
4341 	 */
4342 	if (__predict_false(cache_fplookup_is_mp(fpl))) {
4343 		vput(dvp);
4344 		vput(tvp);
4345 		return (cache_fpl_aborted(fpl));
4346 	}
4347 
4348 	if ((cnp->cn_flags & FAILIFEXISTS) != 0) {
4349 		vput(dvp);
4350 		vput(tvp);
4351 		return (cache_fpl_handled_error(fpl, EEXIST));
4352 	}
4353 
4354 	if ((cnp->cn_flags & LOCKLEAF) == 0) {
4355 		VOP_UNLOCK(tvp);
4356 	}
4357 
4358 	if ((cnp->cn_flags & LOCKPARENT) == 0) {
4359 		VOP_UNLOCK(dvp);
4360 	}
4361 
4362 	if ((cnp->cn_flags & SAVESTART) != 0) {
4363 		ndp->ni_startdir = dvp;
4364 		vrefact(ndp->ni_startdir);
4365 		cnp->cn_flags |= SAVENAME;
4366 		fpl->savename = true;
4367 	}
4368 
4369 	return (cache_fpl_handled(fpl));
4370 }
4371 
4372 static int __noinline
4373 cache_fplookup_modifying(struct cache_fpl *fpl)
4374 {
4375 	struct nameidata *ndp;
4376 
4377 	ndp = fpl->ndp;
4378 
4379 	if (!cache_fpl_islastcn(ndp)) {
4380 		return (cache_fpl_partial(fpl));
4381 	}
4382 	return (cache_fplookup_final_modifying(fpl));
4383 }
4384 
4385 static int __noinline
4386 cache_fplookup_final_withparent(struct cache_fpl *fpl)
4387 {
4388 	struct componentname *cnp;
4389 	enum vgetstate dvs, tvs;
4390 	struct vnode *dvp, *tvp;
4391 	seqc_t dvp_seqc;
4392 	int error;
4393 
4394 	cnp = fpl->cnp;
4395 	dvp = fpl->dvp;
4396 	dvp_seqc = fpl->dvp_seqc;
4397 	tvp = fpl->tvp;
4398 
4399 	MPASS((cnp->cn_flags & (LOCKPARENT|WANTPARENT)) != 0);
4400 
4401 	/*
4402 	 * This is less efficient than it can be for simplicity.
4403 	 */
4404 	dvs = vget_prep_smr(dvp);
4405 	if (__predict_false(dvs == VGET_NONE)) {
4406 		return (cache_fpl_aborted(fpl));
4407 	}
4408 	tvs = vget_prep_smr(tvp);
4409 	if (__predict_false(tvs == VGET_NONE)) {
4410 		cache_fpl_smr_exit(fpl);
4411 		vget_abort(dvp, dvs);
4412 		return (cache_fpl_aborted(fpl));
4413 	}
4414 
4415 	cache_fpl_smr_exit(fpl);
4416 
4417 	if ((cnp->cn_flags & LOCKPARENT) != 0) {
4418 		error = vget_finish(dvp, LK_EXCLUSIVE, dvs);
4419 		if (__predict_false(error != 0)) {
4420 			vget_abort(tvp, tvs);
4421 			return (cache_fpl_aborted(fpl));
4422 		}
4423 	} else {
4424 		vget_finish_ref(dvp, dvs);
4425 	}
4426 
4427 	if (!vn_seqc_consistent(dvp, dvp_seqc)) {
4428 		vget_abort(tvp, tvs);
4429 		if ((cnp->cn_flags & LOCKPARENT) != 0)
4430 			vput(dvp);
4431 		else
4432 			vrele(dvp);
4433 		return (cache_fpl_aborted(fpl));
4434 	}
4435 
4436 	error = cache_fplookup_final_child(fpl, tvs);
4437 	if (__predict_false(error != 0)) {
4438 		MPASS(fpl->status == CACHE_FPL_STATUS_ABORTED);
4439 		if ((cnp->cn_flags & LOCKPARENT) != 0)
4440 			vput(dvp);
4441 		else
4442 			vrele(dvp);
4443 		return (error);
4444 	}
4445 
4446 	MPASS(fpl->status == CACHE_FPL_STATUS_HANDLED);
4447 	return (0);
4448 }
4449 
4450 static int
4451 cache_fplookup_final(struct cache_fpl *fpl)
4452 {
4453 	struct componentname *cnp;
4454 	enum vgetstate tvs;
4455 	struct vnode *dvp, *tvp;
4456 	seqc_t dvp_seqc;
4457 
4458 	cnp = fpl->cnp;
4459 	dvp = fpl->dvp;
4460 	dvp_seqc = fpl->dvp_seqc;
4461 	tvp = fpl->tvp;
4462 
4463 	MPASS(*(cnp->cn_nameptr) != '/');
4464 
4465 	if (cnp->cn_nameiop != LOOKUP) {
4466 		return (cache_fplookup_final_modifying(fpl));
4467 	}
4468 
4469 	if ((cnp->cn_flags & (LOCKPARENT|WANTPARENT)) != 0)
4470 		return (cache_fplookup_final_withparent(fpl));
4471 
4472 	tvs = vget_prep_smr(tvp);
4473 	if (__predict_false(tvs == VGET_NONE)) {
4474 		return (cache_fpl_partial(fpl));
4475 	}
4476 
4477 	if (!vn_seqc_consistent(dvp, dvp_seqc)) {
4478 		cache_fpl_smr_exit(fpl);
4479 		vget_abort(tvp, tvs);
4480 		return (cache_fpl_aborted(fpl));
4481 	}
4482 
4483 	cache_fpl_smr_exit(fpl);
4484 	return (cache_fplookup_final_child(fpl, tvs));
4485 }
4486 
4487 /*
4488  * Comment from locked lookup:
4489  * Check for degenerate name (e.g. / or "") which is a way of talking about a
4490  * directory, e.g. like "/." or ".".
4491  */
4492 static int __noinline
4493 cache_fplookup_degenerate(struct cache_fpl *fpl)
4494 {
4495 	struct componentname *cnp;
4496 	struct vnode *dvp;
4497 	enum vgetstate dvs;
4498 	int error, lkflags;
4499 
4500 	fpl->tvp = fpl->dvp;
4501 	fpl->tvp_seqc = fpl->dvp_seqc;
4502 
4503 	cnp = fpl->cnp;
4504 	dvp = fpl->dvp;
4505 
4506 	if (__predict_false(cnp->cn_nameiop != LOOKUP)) {
4507 		cache_fpl_smr_exit(fpl);
4508 		return (cache_fpl_handled_error(fpl, EISDIR));
4509 	}
4510 
4511 	MPASS((cnp->cn_flags & SAVESTART) == 0);
4512 
4513 	if ((cnp->cn_flags & (LOCKPARENT|WANTPARENT)) != 0) {
4514 		return (cache_fplookup_final_withparent(fpl));
4515 	}
4516 
4517 	dvs = vget_prep_smr(dvp);
4518 	cache_fpl_smr_exit(fpl);
4519 	if (__predict_false(dvs == VGET_NONE)) {
4520 		return (cache_fpl_aborted(fpl));
4521 	}
4522 
4523 	if ((cnp->cn_flags & LOCKLEAF) != 0) {
4524 		lkflags = LK_SHARED;
4525 		if ((cnp->cn_flags & LOCKSHARED) == 0)
4526 			lkflags = LK_EXCLUSIVE;
4527 		error = vget_finish(dvp, lkflags, dvs);
4528 		if (__predict_false(error != 0)) {
4529 			return (cache_fpl_aborted(fpl));
4530 		}
4531 	} else {
4532 		vget_finish_ref(dvp, dvs);
4533 	}
4534 	return (cache_fpl_handled(fpl));
4535 }
4536 
4537 static int __noinline
4538 cache_fplookup_noentry(struct cache_fpl *fpl)
4539 {
4540 	struct nameidata *ndp;
4541 	struct componentname *cnp;
4542 	enum vgetstate dvs;
4543 	struct vnode *dvp, *tvp;
4544 	seqc_t dvp_seqc;
4545 	int error;
4546 	bool docache;
4547 
4548 	ndp = fpl->ndp;
4549 	cnp = fpl->cnp;
4550 	dvp = fpl->dvp;
4551 	dvp_seqc = fpl->dvp_seqc;
4552 
4553 	MPASS((cnp->cn_flags & MAKEENTRY) == 0);
4554 	MPASS((cnp->cn_flags & ISDOTDOT) == 0);
4555 	MPASS(!cache_fpl_isdotdot(cnp));
4556 
4557 	/*
4558 	 * Hack: delayed name len checking.
4559 	 */
4560 	if (__predict_false(cnp->cn_namelen > NAME_MAX)) {
4561 		cache_fpl_smr_exit(fpl);
4562 		return (cache_fpl_handled_error(fpl, ENAMETOOLONG));
4563 	}
4564 
4565 	if (cnp->cn_nameptr[0] == '/') {
4566 		return (cache_fplookup_skip_slashes(fpl));
4567 	}
4568 
4569 	if (cnp->cn_nameptr[0] == '\0') {
4570 		return (cache_fplookup_trailingslash(fpl));
4571 	}
4572 
4573 	if (cnp->cn_nameiop != LOOKUP) {
4574 		fpl->tvp = NULL;
4575 		return (cache_fplookup_modifying(fpl));
4576 	}
4577 
4578 	MPASS((cnp->cn_flags & SAVESTART) == 0);
4579 
4580 	/*
4581 	 * Only try to fill in the component if it is the last one,
4582 	 * otherwise not only there may be several to handle but the
4583 	 * walk may be complicated.
4584 	 */
4585 	if (!cache_fpl_islastcn(ndp)) {
4586 		return (cache_fpl_partial(fpl));
4587 	}
4588 
4589 	/*
4590 	 * Regular lookup nulifies the slash, which we don't do here.
4591 	 * Don't take chances with filesystem routines seeing it for
4592 	 * the last entry.
4593 	 */
4594 	if (cache_fpl_istrailingslash(fpl)) {
4595 		return (cache_fpl_partial(fpl));
4596 	}
4597 
4598 	/*
4599 	 * Secure access to dvp; check cache_fplookup_partial_setup for
4600 	 * reasoning.
4601 	 */
4602 	dvs = vget_prep_smr(dvp);
4603 	cache_fpl_smr_exit(fpl);
4604 	if (__predict_false(dvs == VGET_NONE)) {
4605 		return (cache_fpl_aborted(fpl));
4606 	}
4607 
4608 	vget_finish_ref(dvp, dvs);
4609 	if (!vn_seqc_consistent(dvp, dvp_seqc)) {
4610 		vrele(dvp);
4611 		return (cache_fpl_aborted(fpl));
4612 	}
4613 
4614 	error = vn_lock(dvp, LK_SHARED);
4615 	if (__predict_false(error != 0)) {
4616 		vrele(dvp);
4617 		return (cache_fpl_aborted(fpl));
4618 	}
4619 
4620 	tvp = NULL;
4621 	/*
4622 	 * TODO: provide variants which don't require locking either vnode.
4623 	 */
4624 	cnp->cn_flags |= ISLASTCN;
4625 	docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
4626 	if (docache)
4627 		cnp->cn_flags |= MAKEENTRY;
4628 	cnp->cn_lkflags = LK_SHARED;
4629 	if ((cnp->cn_flags & LOCKSHARED) == 0) {
4630 		cnp->cn_lkflags = LK_EXCLUSIVE;
4631 	}
4632 	error = VOP_LOOKUP(dvp, &tvp, cnp);
4633 	switch (error) {
4634 	case EJUSTRETURN:
4635 	case 0:
4636 		break;
4637 	case ENOTDIR:
4638 	case ENOENT:
4639 		vput(dvp);
4640 		return (cache_fpl_handled_error(fpl, error));
4641 	default:
4642 		vput(dvp);
4643 		return (cache_fpl_aborted(fpl));
4644 	}
4645 
4646 	fpl->tvp = tvp;
4647 	if (!fpl->savename) {
4648 		MPASS((cnp->cn_flags & SAVENAME) == 0);
4649 	}
4650 
4651 	if (tvp == NULL) {
4652 		MPASS(error == EJUSTRETURN);
4653 		if ((cnp->cn_flags & (WANTPARENT | LOCKPARENT)) == 0) {
4654 			vput(dvp);
4655 		} else if ((cnp->cn_flags & LOCKPARENT) == 0) {
4656 			VOP_UNLOCK(dvp);
4657 		}
4658 		return (cache_fpl_handled(fpl));
4659 	}
4660 
4661 	if (tvp->v_type == VLNK) {
4662 		if ((cnp->cn_flags & FOLLOW) != 0) {
4663 			vput(dvp);
4664 			vput(tvp);
4665 			return (cache_fpl_aborted(fpl));
4666 		}
4667 	}
4668 
4669 	if (__predict_false(cache_fplookup_is_mp(fpl))) {
4670 		vput(dvp);
4671 		vput(tvp);
4672 		return (cache_fpl_aborted(fpl));
4673 	}
4674 
4675 	if ((cnp->cn_flags & LOCKLEAF) == 0) {
4676 		VOP_UNLOCK(tvp);
4677 	}
4678 
4679 	if ((cnp->cn_flags & (WANTPARENT | LOCKPARENT)) == 0) {
4680 		vput(dvp);
4681 	} else if ((cnp->cn_flags & LOCKPARENT) == 0) {
4682 		VOP_UNLOCK(dvp);
4683 	}
4684 	return (cache_fpl_handled(fpl));
4685 }
4686 
4687 static int __noinline
4688 cache_fplookup_dot(struct cache_fpl *fpl)
4689 {
4690 	int error;
4691 
4692 	MPASS(!seqc_in_modify(fpl->dvp_seqc));
4693 	/*
4694 	 * Just re-assign the value. seqc will be checked later for the first
4695 	 * non-dot path component in line and/or before deciding to return the
4696 	 * vnode.
4697 	 */
4698 	fpl->tvp = fpl->dvp;
4699 	fpl->tvp_seqc = fpl->dvp_seqc;
4700 
4701 	counter_u64_add(dothits, 1);
4702 	SDT_PROBE3(vfs, namecache, lookup, hit, fpl->dvp, ".", fpl->dvp);
4703 
4704 	error = 0;
4705 	if (cache_fplookup_is_mp(fpl)) {
4706 		error = cache_fplookup_cross_mount(fpl);
4707 	}
4708 	return (error);
4709 }
4710 
4711 static int __noinline
4712 cache_fplookup_dotdot(struct cache_fpl *fpl)
4713 {
4714 	struct nameidata *ndp;
4715 	struct componentname *cnp;
4716 	struct namecache *ncp;
4717 	struct vnode *dvp;
4718 	struct prison *pr;
4719 	u_char nc_flag;
4720 
4721 	ndp = fpl->ndp;
4722 	cnp = fpl->cnp;
4723 	dvp = fpl->dvp;
4724 
4725 	MPASS(cache_fpl_isdotdot(cnp));
4726 
4727 	/*
4728 	 * XXX this is racy the same way regular lookup is
4729 	 */
4730 	for (pr = cnp->cn_cred->cr_prison; pr != NULL;
4731 	    pr = pr->pr_parent)
4732 		if (dvp == pr->pr_root)
4733 			break;
4734 
4735 	if (dvp == ndp->ni_rootdir ||
4736 	    dvp == ndp->ni_topdir ||
4737 	    dvp == rootvnode ||
4738 	    pr != NULL) {
4739 		fpl->tvp = dvp;
4740 		fpl->tvp_seqc = vn_seqc_read_any(dvp);
4741 		if (seqc_in_modify(fpl->tvp_seqc)) {
4742 			return (cache_fpl_aborted(fpl));
4743 		}
4744 		return (0);
4745 	}
4746 
4747 	if ((dvp->v_vflag & VV_ROOT) != 0) {
4748 		/*
4749 		 * TODO
4750 		 * The opposite of climb mount is needed here.
4751 		 */
4752 		return (cache_fpl_partial(fpl));
4753 	}
4754 
4755 	ncp = atomic_load_consume_ptr(&dvp->v_cache_dd);
4756 	if (ncp == NULL) {
4757 		return (cache_fpl_aborted(fpl));
4758 	}
4759 
4760 	nc_flag = atomic_load_char(&ncp->nc_flag);
4761 	if ((nc_flag & NCF_ISDOTDOT) != 0) {
4762 		if ((nc_flag & NCF_NEGATIVE) != 0)
4763 			return (cache_fpl_aborted(fpl));
4764 		fpl->tvp = ncp->nc_vp;
4765 	} else {
4766 		fpl->tvp = ncp->nc_dvp;
4767 	}
4768 
4769 	fpl->tvp_seqc = vn_seqc_read_any(fpl->tvp);
4770 	if (seqc_in_modify(fpl->tvp_seqc)) {
4771 		return (cache_fpl_partial(fpl));
4772 	}
4773 
4774 	/*
4775 	 * Acquire fence provided by vn_seqc_read_any above.
4776 	 */
4777 	if (__predict_false(atomic_load_ptr(&dvp->v_cache_dd) != ncp)) {
4778 		return (cache_fpl_aborted(fpl));
4779 	}
4780 
4781 	if (!cache_ncp_canuse(ncp)) {
4782 		return (cache_fpl_aborted(fpl));
4783 	}
4784 
4785 	counter_u64_add(dotdothits, 1);
4786 	return (0);
4787 }
4788 
4789 static int __noinline
4790 cache_fplookup_neg(struct cache_fpl *fpl, struct namecache *ncp, uint32_t hash)
4791 {
4792 	u_char nc_flag;
4793 	bool neg_promote;
4794 
4795 	nc_flag = atomic_load_char(&ncp->nc_flag);
4796 	MPASS((nc_flag & NCF_NEGATIVE) != 0);
4797 	/*
4798 	 * If they want to create an entry we need to replace this one.
4799 	 */
4800 	if (__predict_false(fpl->cnp->cn_nameiop != LOOKUP)) {
4801 		fpl->tvp = NULL;
4802 		return (cache_fplookup_modifying(fpl));
4803 	}
4804 	neg_promote = cache_neg_hit_prep(ncp);
4805 	if (!cache_fpl_neg_ncp_canuse(ncp)) {
4806 		cache_neg_hit_abort(ncp);
4807 		return (cache_fpl_partial(fpl));
4808 	}
4809 	if (neg_promote) {
4810 		return (cache_fplookup_negative_promote(fpl, ncp, hash));
4811 	}
4812 	cache_neg_hit_finish(ncp);
4813 	cache_fpl_smr_exit(fpl);
4814 	return (cache_fpl_handled_error(fpl, ENOENT));
4815 }
4816 
4817 /*
4818  * Resolve a symlink. Called by filesystem-specific routines.
4819  *
4820  * Code flow is:
4821  * ... -> cache_fplookup_symlink -> VOP_FPLOOKUP_SYMLINK -> cache_symlink_resolve
4822  */
4823 int
4824 cache_symlink_resolve(struct cache_fpl *fpl, const char *string, size_t len)
4825 {
4826 	struct nameidata *ndp;
4827 	struct componentname *cnp;
4828 	size_t adjust;
4829 
4830 	ndp = fpl->ndp;
4831 	cnp = fpl->cnp;
4832 
4833 	if (__predict_false(len == 0)) {
4834 		return (ENOENT);
4835 	}
4836 
4837 	if (__predict_false(len > MAXPATHLEN - 2)) {
4838 		if (cache_fpl_istrailingslash(fpl)) {
4839 			return (EAGAIN);
4840 		}
4841 	}
4842 
4843 	ndp->ni_pathlen = fpl->nulchar - cnp->cn_nameptr - cnp->cn_namelen + 1;
4844 #ifdef INVARIANTS
4845 	if (ndp->ni_pathlen != fpl->debug.ni_pathlen) {
4846 		panic("%s: mismatch (%zu != %zu) nulchar %p nameptr %p [%s] ; full string [%s]\n",
4847 		    __func__, ndp->ni_pathlen, fpl->debug.ni_pathlen, fpl->nulchar,
4848 		    cnp->cn_nameptr, cnp->cn_nameptr, cnp->cn_pnbuf);
4849 	}
4850 #endif
4851 
4852 	if (__predict_false(len + ndp->ni_pathlen > MAXPATHLEN)) {
4853 		return (ENAMETOOLONG);
4854 	}
4855 
4856 	if (__predict_false(ndp->ni_loopcnt++ >= MAXSYMLINKS)) {
4857 		return (ELOOP);
4858 	}
4859 
4860 	adjust = len;
4861 	if (ndp->ni_pathlen > 1) {
4862 		bcopy(ndp->ni_next, cnp->cn_pnbuf + len, ndp->ni_pathlen);
4863 	} else {
4864 		if (cache_fpl_istrailingslash(fpl)) {
4865 			adjust = len + 1;
4866 			cnp->cn_pnbuf[len] = '/';
4867 			cnp->cn_pnbuf[len + 1] = '\0';
4868 		} else {
4869 			cnp->cn_pnbuf[len] = '\0';
4870 		}
4871 	}
4872 	bcopy(string, cnp->cn_pnbuf, len);
4873 
4874 	ndp->ni_pathlen += adjust;
4875 	cache_fpl_pathlen_add(fpl, adjust);
4876 	cnp->cn_nameptr = cnp->cn_pnbuf;
4877 	fpl->nulchar = &cnp->cn_nameptr[ndp->ni_pathlen - 1];
4878 
4879 	return (0);
4880 }
4881 
4882 static int __noinline
4883 cache_fplookup_symlink(struct cache_fpl *fpl)
4884 {
4885 	struct mount *mp;
4886 	struct nameidata *ndp;
4887 	struct componentname *cnp;
4888 	struct vnode *dvp, *tvp;
4889 	int error;
4890 
4891 	ndp = fpl->ndp;
4892 	cnp = fpl->cnp;
4893 	dvp = fpl->dvp;
4894 	tvp = fpl->tvp;
4895 
4896 	if (cache_fpl_islastcn(ndp)) {
4897 		if ((cnp->cn_flags & FOLLOW) == 0) {
4898 			return (cache_fplookup_final(fpl));
4899 		}
4900 	}
4901 
4902 	mp = atomic_load_ptr(&dvp->v_mount);
4903 	if (__predict_false(mp == NULL)) {
4904 		return (cache_fpl_aborted(fpl));
4905 	}
4906 
4907 	/*
4908 	 * Note this check races against setting the flag just like regular
4909 	 * lookup.
4910 	 */
4911 	if (__predict_false((mp->mnt_flag & MNT_NOSYMFOLLOW) != 0)) {
4912 		cache_fpl_smr_exit(fpl);
4913 		return (cache_fpl_handled_error(fpl, EACCES));
4914 	}
4915 
4916 	error = VOP_FPLOOKUP_SYMLINK(tvp, fpl);
4917 	if (__predict_false(error != 0)) {
4918 		switch (error) {
4919 		case EAGAIN:
4920 			return (cache_fpl_partial(fpl));
4921 		case ENOENT:
4922 		case ENAMETOOLONG:
4923 		case ELOOP:
4924 			cache_fpl_smr_exit(fpl);
4925 			return (cache_fpl_handled_error(fpl, error));
4926 		default:
4927 			return (cache_fpl_aborted(fpl));
4928 		}
4929 	}
4930 
4931 	if (*(cnp->cn_nameptr) == '/') {
4932 		fpl->dvp = cache_fpl_handle_root(fpl);
4933 		fpl->dvp_seqc = vn_seqc_read_any(fpl->dvp);
4934 		if (seqc_in_modify(fpl->dvp_seqc)) {
4935 			return (cache_fpl_aborted(fpl));
4936 		}
4937 	}
4938 
4939 	return (cache_fplookup_preparse(fpl));
4940 }
4941 
4942 static int
4943 cache_fplookup_next(struct cache_fpl *fpl)
4944 {
4945 	struct componentname *cnp;
4946 	struct namecache *ncp;
4947 	struct vnode *dvp, *tvp;
4948 	u_char nc_flag;
4949 	uint32_t hash;
4950 	int error;
4951 
4952 	cnp = fpl->cnp;
4953 	dvp = fpl->dvp;
4954 
4955 	if (__predict_false(cnp->cn_nameptr[0] == '.')) {
4956 		if (cnp->cn_namelen == 1) {
4957 			return (cache_fplookup_dot(fpl));
4958 		}
4959 		if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
4960 			return (cache_fplookup_dotdot(fpl));
4961 		}
4962 	}
4963 
4964 	MPASS(!cache_fpl_isdotdot(cnp));
4965 
4966 	hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
4967 
4968 	CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
4969 		if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
4970 		    !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
4971 			break;
4972 	}
4973 
4974 	if (__predict_false(ncp == NULL)) {
4975 		return (cache_fplookup_noentry(fpl));
4976 	}
4977 
4978 	tvp = atomic_load_ptr(&ncp->nc_vp);
4979 	nc_flag = atomic_load_char(&ncp->nc_flag);
4980 	if ((nc_flag & NCF_NEGATIVE) != 0) {
4981 		return (cache_fplookup_neg(fpl, ncp, hash));
4982 	}
4983 
4984 	if (!cache_ncp_canuse(ncp)) {
4985 		return (cache_fpl_partial(fpl));
4986 	}
4987 
4988 	fpl->tvp = tvp;
4989 	fpl->tvp_seqc = vn_seqc_read_any(tvp);
4990 	if (seqc_in_modify(fpl->tvp_seqc)) {
4991 		return (cache_fpl_partial(fpl));
4992 	}
4993 
4994 	counter_u64_add(numposhits, 1);
4995 	SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name, tvp);
4996 
4997 	error = 0;
4998 	if (cache_fplookup_is_mp(fpl)) {
4999 		error = cache_fplookup_cross_mount(fpl);
5000 	}
5001 	return (error);
5002 }
5003 
5004 static bool
5005 cache_fplookup_mp_supported(struct mount *mp)
5006 {
5007 
5008 	MPASS(mp != NULL);
5009 	if ((mp->mnt_kern_flag & MNTK_FPLOOKUP) == 0)
5010 		return (false);
5011 	return (true);
5012 }
5013 
5014 /*
5015  * Walk up the mount stack (if any).
5016  *
5017  * Correctness is provided in the following ways:
5018  * - all vnodes are protected from freeing with SMR
5019  * - struct mount objects are type stable making them always safe to access
5020  * - stability of the particular mount is provided by busying it
5021  * - relationship between the vnode which is mounted on and the mount is
5022  *   verified with the vnode sequence counter after busying
5023  * - association between root vnode of the mount and the mount is protected
5024  *   by busy
5025  *
5026  * From that point on we can read the sequence counter of the root vnode
5027  * and get the next mount on the stack (if any) using the same protection.
5028  *
5029  * By the end of successful walk we are guaranteed the reached state was
5030  * indeed present at least at some point which matches the regular lookup.
5031  */
5032 static int __noinline
5033 cache_fplookup_climb_mount(struct cache_fpl *fpl)
5034 {
5035 	struct mount *mp, *prev_mp;
5036 	struct mount_pcpu *mpcpu, *prev_mpcpu;
5037 	struct vnode *vp;
5038 	seqc_t vp_seqc;
5039 
5040 	vp = fpl->tvp;
5041 	vp_seqc = fpl->tvp_seqc;
5042 
5043 	VNPASS(vp->v_type == VDIR || vp->v_type == VBAD, vp);
5044 	mp = atomic_load_ptr(&vp->v_mountedhere);
5045 	if (__predict_false(mp == NULL)) {
5046 		return (0);
5047 	}
5048 
5049 	prev_mp = NULL;
5050 	for (;;) {
5051 		if (!vfs_op_thread_enter_crit(mp, mpcpu)) {
5052 			if (prev_mp != NULL)
5053 				vfs_op_thread_exit_crit(prev_mp, prev_mpcpu);
5054 			return (cache_fpl_partial(fpl));
5055 		}
5056 		if (prev_mp != NULL)
5057 			vfs_op_thread_exit_crit(prev_mp, prev_mpcpu);
5058 		if (!vn_seqc_consistent(vp, vp_seqc)) {
5059 			vfs_op_thread_exit_crit(mp, mpcpu);
5060 			return (cache_fpl_partial(fpl));
5061 		}
5062 		if (!cache_fplookup_mp_supported(mp)) {
5063 			vfs_op_thread_exit_crit(mp, mpcpu);
5064 			return (cache_fpl_partial(fpl));
5065 		}
5066 		vp = atomic_load_ptr(&mp->mnt_rootvnode);
5067 		if (vp == NULL) {
5068 			vfs_op_thread_exit_crit(mp, mpcpu);
5069 			return (cache_fpl_partial(fpl));
5070 		}
5071 		vp_seqc = vn_seqc_read_any(vp);
5072 		if (seqc_in_modify(vp_seqc)) {
5073 			vfs_op_thread_exit_crit(mp, mpcpu);
5074 			return (cache_fpl_partial(fpl));
5075 		}
5076 		prev_mp = mp;
5077 		prev_mpcpu = mpcpu;
5078 		mp = atomic_load_ptr(&vp->v_mountedhere);
5079 		if (mp == NULL)
5080 			break;
5081 	}
5082 
5083 	vfs_op_thread_exit_crit(prev_mp, prev_mpcpu);
5084 	fpl->tvp = vp;
5085 	fpl->tvp_seqc = vp_seqc;
5086 	return (0);
5087 }
5088 
5089 static int __noinline
5090 cache_fplookup_cross_mount(struct cache_fpl *fpl)
5091 {
5092 	struct mount *mp;
5093 	struct mount_pcpu *mpcpu;
5094 	struct vnode *vp;
5095 	seqc_t vp_seqc;
5096 
5097 	vp = fpl->tvp;
5098 	vp_seqc = fpl->tvp_seqc;
5099 
5100 	VNPASS(vp->v_type == VDIR || vp->v_type == VBAD, vp);
5101 	mp = atomic_load_ptr(&vp->v_mountedhere);
5102 	if (__predict_false(mp == NULL)) {
5103 		return (0);
5104 	}
5105 
5106 	if (!vfs_op_thread_enter_crit(mp, mpcpu)) {
5107 		return (cache_fpl_partial(fpl));
5108 	}
5109 	if (!vn_seqc_consistent(vp, vp_seqc)) {
5110 		vfs_op_thread_exit_crit(mp, mpcpu);
5111 		return (cache_fpl_partial(fpl));
5112 	}
5113 	if (!cache_fplookup_mp_supported(mp)) {
5114 		vfs_op_thread_exit_crit(mp, mpcpu);
5115 		return (cache_fpl_partial(fpl));
5116 	}
5117 	vp = atomic_load_ptr(&mp->mnt_rootvnode);
5118 	if (__predict_false(vp == NULL)) {
5119 		vfs_op_thread_exit_crit(mp, mpcpu);
5120 		return (cache_fpl_partial(fpl));
5121 	}
5122 	vp_seqc = vn_seqc_read_any(vp);
5123 	vfs_op_thread_exit_crit(mp, mpcpu);
5124 	if (seqc_in_modify(vp_seqc)) {
5125 		return (cache_fpl_partial(fpl));
5126 	}
5127 	mp = atomic_load_ptr(&vp->v_mountedhere);
5128 	if (__predict_false(mp != NULL)) {
5129 		/*
5130 		 * There are possibly more mount points on top.
5131 		 * Normally this does not happen so for simplicity just start
5132 		 * over.
5133 		 */
5134 		return (cache_fplookup_climb_mount(fpl));
5135 	}
5136 
5137 	fpl->tvp = vp;
5138 	fpl->tvp_seqc = vp_seqc;
5139 	return (0);
5140 }
5141 
5142 /*
5143  * Check if a vnode is mounted on.
5144  */
5145 static bool
5146 cache_fplookup_is_mp(struct cache_fpl *fpl)
5147 {
5148 	struct vnode *vp;
5149 
5150 	vp = fpl->tvp;
5151 	return ((vn_irflag_read(vp) & VIRF_MOUNTPOINT) != 0);
5152 }
5153 
5154 /*
5155  * Parse the path.
5156  *
5157  * The code was originally copy-pasted from regular lookup and despite
5158  * clean ups leaves performance on the table. Any modifications here
5159  * must take into account that in case off fallback the resulting
5160  * nameidata state has to be compatible with the original.
5161  */
5162 
5163 /*
5164  * Debug ni_pathlen tracking.
5165  */
5166 #ifdef INVARIANTS
5167 static void
5168 cache_fpl_pathlen_add(struct cache_fpl *fpl, size_t n)
5169 {
5170 
5171 	fpl->debug.ni_pathlen += n;
5172 	KASSERT(fpl->debug.ni_pathlen <= PATH_MAX,
5173 	    ("%s: pathlen overflow to %zd\n", __func__, fpl->debug.ni_pathlen));
5174 }
5175 
5176 static void
5177 cache_fpl_pathlen_sub(struct cache_fpl *fpl, size_t n)
5178 {
5179 
5180 	fpl->debug.ni_pathlen -= n;
5181 	KASSERT(fpl->debug.ni_pathlen <= PATH_MAX,
5182 	    ("%s: pathlen underflow to %zd\n", __func__, fpl->debug.ni_pathlen));
5183 }
5184 
5185 static void
5186 cache_fpl_pathlen_inc(struct cache_fpl *fpl)
5187 {
5188 
5189 	cache_fpl_pathlen_add(fpl, 1);
5190 }
5191 
5192 static void
5193 cache_fpl_pathlen_dec(struct cache_fpl *fpl)
5194 {
5195 
5196 	cache_fpl_pathlen_sub(fpl, 1);
5197 }
5198 #else
5199 static void
5200 cache_fpl_pathlen_add(struct cache_fpl *fpl, size_t n)
5201 {
5202 }
5203 
5204 static void
5205 cache_fpl_pathlen_sub(struct cache_fpl *fpl, size_t n)
5206 {
5207 }
5208 
5209 static void
5210 cache_fpl_pathlen_inc(struct cache_fpl *fpl)
5211 {
5212 }
5213 
5214 static void
5215 cache_fpl_pathlen_dec(struct cache_fpl *fpl)
5216 {
5217 }
5218 #endif
5219 
5220 static int __always_inline
5221 cache_fplookup_preparse(struct cache_fpl *fpl)
5222 {
5223 	struct componentname *cnp;
5224 
5225 	cnp = fpl->cnp;
5226 
5227 	if (__predict_false(cnp->cn_nameptr[0] == '\0')) {
5228 		return (cache_fplookup_degenerate(fpl));
5229 	}
5230 
5231 	/*
5232 	 * By this point the shortest possible pathname is one character + nul
5233 	 * terminator, hence 2.
5234 	 */
5235 	KASSERT(fpl->debug.ni_pathlen >= 2, ("%s: pathlen %zu\n", __func__,
5236 	    fpl->debug.ni_pathlen));
5237 	KASSERT(&cnp->cn_nameptr[fpl->debug.ni_pathlen - 2] == fpl->nulchar - 1,
5238 	    ("%s: mismatch on string (%p != %p) [%s]\n", __func__,
5239 	    &cnp->cn_nameptr[fpl->debug.ni_pathlen - 2], fpl->nulchar - 1,
5240 	    cnp->cn_pnbuf));
5241 	return (0);
5242 }
5243 
5244 static void
5245 cache_fplookup_parse(struct cache_fpl *fpl)
5246 {
5247 	struct nameidata *ndp;
5248 	struct componentname *cnp;
5249 	char *cp;
5250 
5251 	ndp = fpl->ndp;
5252 	cnp = fpl->cnp;
5253 
5254 	/*
5255 	 * Find the end of this path component, it is either / or nul.
5256 	 *
5257 	 * Store / as a temporary sentinel so that we only have one character
5258 	 * to test for. Pathnames tend to be short so this should not be
5259 	 * resulting in cache misses.
5260 	 */
5261 	KASSERT(&cnp->cn_nameptr[fpl->debug.ni_pathlen - 1] == fpl->nulchar,
5262 	    ("%s: mismatch between pathlen (%zu) and nulchar (%p != %p), string [%s]\n",
5263 	    __func__, fpl->debug.ni_pathlen, &cnp->cn_nameptr[fpl->debug.ni_pathlen - 1],
5264 	    fpl->nulchar, cnp->cn_pnbuf));
5265 	KASSERT(*fpl->nulchar == '\0',
5266 	    ("%s: expected nul at %p; string [%s]\n", __func__, fpl->nulchar,
5267 	    cnp->cn_pnbuf));
5268 	*fpl->nulchar = '/';
5269 	for (cp = cnp->cn_nameptr; *cp != '/'; cp++) {
5270 		KASSERT(*cp != '\0',
5271 		    ("%s: encountered unexpected nul; string [%s]\n", __func__,
5272 		    cnp->cn_nameptr));
5273 		continue;
5274 	}
5275 	*fpl->nulchar = '\0';
5276 
5277 	cnp->cn_namelen = cp - cnp->cn_nameptr;
5278 	cache_fpl_pathlen_sub(fpl, cnp->cn_namelen);
5279 	/*
5280 	 * Hack: we have to check if the found path component's length exceeds
5281 	 * NAME_MAX. However, the condition is very rarely true and check can
5282 	 * be elided in the common case -- if an entry was found in the cache,
5283 	 * then it could not have been too long to begin with.
5284 	 */
5285 	ndp->ni_next = cp;
5286 }
5287 
5288 static void
5289 cache_fplookup_parse_advance(struct cache_fpl *fpl)
5290 {
5291 	struct nameidata *ndp;
5292 	struct componentname *cnp;
5293 
5294 	ndp = fpl->ndp;
5295 	cnp = fpl->cnp;
5296 
5297 	cnp->cn_nameptr = ndp->ni_next;
5298 	KASSERT(*(cnp->cn_nameptr) == '/',
5299 	    ("%s: should have seen slash at %p ; buf %p [%s]\n", __func__,
5300 	    cnp->cn_nameptr, cnp->cn_pnbuf, cnp->cn_pnbuf));
5301 	cnp->cn_nameptr++;
5302 	cache_fpl_pathlen_dec(fpl);
5303 }
5304 
5305 /*
5306  * Skip spurious slashes in a pathname (e.g., "foo///bar") and retry.
5307  *
5308  * Lockless lookup tries to elide checking for spurious slashes and should they
5309  * be present is guaranteed to fail to find an entry. In this case the caller
5310  * must check if the name starts with a slash and call this routine.  It is
5311  * going to fast forward across the spurious slashes and set the state up for
5312  * retry.
5313  */
5314 static int __noinline
5315 cache_fplookup_skip_slashes(struct cache_fpl *fpl)
5316 {
5317 	struct nameidata *ndp;
5318 	struct componentname *cnp;
5319 
5320 	ndp = fpl->ndp;
5321 	cnp = fpl->cnp;
5322 
5323 	MPASS(*(cnp->cn_nameptr) == '/');
5324 	do {
5325 		cnp->cn_nameptr++;
5326 		cache_fpl_pathlen_dec(fpl);
5327 	} while (*(cnp->cn_nameptr) == '/');
5328 
5329 	/*
5330 	 * Go back to one slash so that cache_fplookup_parse_advance has
5331 	 * something to skip.
5332 	 */
5333 	cnp->cn_nameptr--;
5334 	cache_fpl_pathlen_inc(fpl);
5335 
5336 	/*
5337 	 * cache_fplookup_parse_advance starts from ndp->ni_next
5338 	 */
5339 	ndp->ni_next = cnp->cn_nameptr;
5340 
5341 	/*
5342 	 * See cache_fplookup_dot.
5343 	 */
5344 	fpl->tvp = fpl->dvp;
5345 	fpl->tvp_seqc = fpl->dvp_seqc;
5346 
5347 	return (0);
5348 }
5349 
5350 /*
5351  * Handle trailing slashes (e.g., "foo/").
5352  *
5353  * If a trailing slash is found the terminal vnode must be a directory.
5354  * Regular lookup shortens the path by nulifying the first trailing slash and
5355  * sets the TRAILINGSLASH flag to denote this took place. There are several
5356  * checks on it performed later.
5357  *
5358  * Similarly to spurious slashes, lockless lookup handles this in a speculative
5359  * manner relying on an invariant that a non-directory vnode will get a miss.
5360  * In this case cn_nameptr[0] == '\0' and cn_namelen == 0.
5361  *
5362  * Thus for a path like "foo/bar/" the code unwinds the state back to 'bar/'
5363  * and denotes this is the last path component, which avoids looping back.
5364  *
5365  * Only plain lookups are supported for now to restrict corner cases to handle.
5366  */
5367 static int __noinline
5368 cache_fplookup_trailingslash(struct cache_fpl *fpl)
5369 {
5370 #ifdef INVARIANTS
5371 	size_t ni_pathlen;
5372 #endif
5373 	struct nameidata *ndp;
5374 	struct componentname *cnp;
5375 	struct namecache *ncp;
5376 	struct vnode *tvp;
5377 	char *cn_nameptr_orig, *cn_nameptr_slash;
5378 	seqc_t tvp_seqc;
5379 	u_char nc_flag;
5380 
5381 	ndp = fpl->ndp;
5382 	cnp = fpl->cnp;
5383 	tvp = fpl->tvp;
5384 	tvp_seqc = fpl->tvp_seqc;
5385 
5386 	MPASS(fpl->dvp == fpl->tvp);
5387 	KASSERT(cache_fpl_istrailingslash(fpl),
5388 	    ("%s: expected trailing slash at %p; string [%s]\n", __func__, fpl->nulchar - 1,
5389 	    cnp->cn_pnbuf));
5390 	KASSERT(cnp->cn_nameptr[0] == '\0',
5391 	    ("%s: expected nul char at %p; string [%s]\n", __func__, &cnp->cn_nameptr[0],
5392 	    cnp->cn_pnbuf));
5393 	KASSERT(cnp->cn_namelen == 0,
5394 	    ("%s: namelen 0 but got %ld; string [%s]\n", __func__, cnp->cn_namelen,
5395 	    cnp->cn_pnbuf));
5396 	MPASS(cnp->cn_nameptr > cnp->cn_pnbuf);
5397 
5398 	if (cnp->cn_nameiop != LOOKUP) {
5399 		return (cache_fpl_aborted(fpl));
5400 	}
5401 
5402 	if (__predict_false(tvp->v_type != VDIR)) {
5403 		if (!vn_seqc_consistent(tvp, tvp_seqc)) {
5404 			return (cache_fpl_aborted(fpl));
5405 		}
5406 		cache_fpl_smr_exit(fpl);
5407 		return (cache_fpl_handled_error(fpl, ENOTDIR));
5408 	}
5409 
5410 	/*
5411 	 * Denote the last component.
5412 	 */
5413 	ndp->ni_next = &cnp->cn_nameptr[0];
5414 	MPASS(cache_fpl_islastcn(ndp));
5415 
5416 	/*
5417 	 * Unwind trailing slashes.
5418 	 */
5419 	cn_nameptr_orig = cnp->cn_nameptr;
5420 	while (cnp->cn_nameptr >= cnp->cn_pnbuf) {
5421 		cnp->cn_nameptr--;
5422 		if (cnp->cn_nameptr[0] != '/') {
5423 			break;
5424 		}
5425 	}
5426 
5427 	/*
5428 	 * Unwind to the beginning of the path component.
5429 	 *
5430 	 * Note the path may or may not have started with a slash.
5431 	 */
5432 	cn_nameptr_slash = cnp->cn_nameptr;
5433 	while (cnp->cn_nameptr > cnp->cn_pnbuf) {
5434 		cnp->cn_nameptr--;
5435 		if (cnp->cn_nameptr[0] == '/') {
5436 			break;
5437 		}
5438 	}
5439 	if (cnp->cn_nameptr[0] == '/') {
5440 		cnp->cn_nameptr++;
5441 	}
5442 
5443 	cnp->cn_namelen = cn_nameptr_slash - cnp->cn_nameptr + 1;
5444 	cache_fpl_pathlen_add(fpl, cn_nameptr_orig - cnp->cn_nameptr);
5445 	cache_fpl_checkpoint(fpl);
5446 
5447 #ifdef INVARIANTS
5448 	ni_pathlen = fpl->nulchar - cnp->cn_nameptr + 1;
5449 	if (ni_pathlen != fpl->debug.ni_pathlen) {
5450 		panic("%s: mismatch (%zu != %zu) nulchar %p nameptr %p [%s] ; full string [%s]\n",
5451 		    __func__, ni_pathlen, fpl->debug.ni_pathlen, fpl->nulchar,
5452 		    cnp->cn_nameptr, cnp->cn_nameptr, cnp->cn_pnbuf);
5453 	}
5454 #endif
5455 
5456 	/*
5457 	 * The previous directory is this one.
5458 	 */
5459 	if (cnp->cn_nameptr[0] == '.' && cnp->cn_namelen == 1) {
5460 		return (0);
5461 	}
5462 
5463 	/*
5464 	 * The previous directory is something else.
5465 	 */
5466 	tvp = fpl->tvp;
5467 	ncp = atomic_load_consume_ptr(&tvp->v_cache_dd);
5468 	if (__predict_false(ncp == NULL)) {
5469 		return (cache_fpl_aborted(fpl));
5470 	}
5471 	nc_flag = atomic_load_char(&ncp->nc_flag);
5472 	if ((nc_flag & NCF_ISDOTDOT) != 0) {
5473 		return (cache_fpl_aborted(fpl));
5474 	}
5475 	fpl->dvp = ncp->nc_dvp;
5476 	fpl->dvp_seqc = vn_seqc_read_any(fpl->dvp);
5477 	if (seqc_in_modify(fpl->dvp_seqc)) {
5478 		return (cache_fpl_aborted(fpl));
5479 	}
5480 	return (0);
5481 }
5482 
5483 /*
5484  * See the API contract for VOP_FPLOOKUP_VEXEC.
5485  */
5486 static int __noinline
5487 cache_fplookup_failed_vexec(struct cache_fpl *fpl, int error)
5488 {
5489 	struct componentname *cnp;
5490 	struct vnode *dvp;
5491 	seqc_t dvp_seqc;
5492 
5493 	cnp = fpl->cnp;
5494 	dvp = fpl->dvp;
5495 	dvp_seqc = fpl->dvp_seqc;
5496 
5497 	/*
5498 	 * Hack: delayed name len checking.
5499 	 */
5500 	if (__predict_false(cnp->cn_namelen > NAME_MAX)) {
5501 		cache_fpl_smr_exit(fpl);
5502 		return (cache_fpl_handled_error(fpl, ENAMETOOLONG));
5503 	}
5504 
5505 	/*
5506 	 * Hack: they may be looking up foo/bar, where foo is not a directory.
5507 	 * In such a case we need to return ENOTDIR, but we may happen to get
5508 	 * here with a different error.
5509 	 */
5510 	if (dvp->v_type != VDIR) {
5511 		error = ENOTDIR;
5512 	}
5513 
5514 	/*
5515 	 * Hack: handle O_SEARCH.
5516 	 *
5517 	 * Open Group Base Specifications Issue 7, 2018 edition states:
5518 	 * <quote>
5519 	 * If the access mode of the open file description associated with the
5520 	 * file descriptor is not O_SEARCH, the function shall check whether
5521 	 * directory searches are permitted using the current permissions of
5522 	 * the directory underlying the file descriptor. If the access mode is
5523 	 * O_SEARCH, the function shall not perform the check.
5524 	 * </quote>
5525 	 *
5526 	 * Regular lookup tests for the NOEXECCHECK flag for every path
5527 	 * component to decide whether to do the permission check. However,
5528 	 * since most lookups never have the flag (and when they do it is only
5529 	 * present for the first path component), lockless lookup only acts on
5530 	 * it if there is a permission problem. Here the flag is represented
5531 	 * with a boolean so that we don't have to clear it on the way out.
5532 	 *
5533 	 * For simplicity this always aborts.
5534 	 * TODO: check if this is the first lookup and ignore the permission
5535 	 * problem. Note the flag has to survive fallback (if it happens to be
5536 	 * performed).
5537 	 */
5538 	if (fpl->fsearch) {
5539 		return (cache_fpl_aborted(fpl));
5540 	}
5541 
5542 	switch (error) {
5543 	case EAGAIN:
5544 		if (!vn_seqc_consistent(dvp, dvp_seqc)) {
5545 			error = cache_fpl_aborted(fpl);
5546 		} else {
5547 			cache_fpl_partial(fpl);
5548 		}
5549 		break;
5550 	default:
5551 		if (!vn_seqc_consistent(dvp, dvp_seqc)) {
5552 			error = cache_fpl_aborted(fpl);
5553 		} else {
5554 			cache_fpl_smr_exit(fpl);
5555 			cache_fpl_handled_error(fpl, error);
5556 		}
5557 		break;
5558 	}
5559 	return (error);
5560 }
5561 
5562 static int
5563 cache_fplookup_impl(struct vnode *dvp, struct cache_fpl *fpl)
5564 {
5565 	struct nameidata *ndp;
5566 	struct componentname *cnp;
5567 	struct mount *mp;
5568 	int error;
5569 
5570 	ndp = fpl->ndp;
5571 	cnp = fpl->cnp;
5572 
5573 	cache_fpl_checkpoint(fpl);
5574 
5575 	/*
5576 	 * The vnode at hand is almost always stable, skip checking for it.
5577 	 * Worst case this postpones the check towards the end of the iteration
5578 	 * of the main loop.
5579 	 */
5580 	fpl->dvp = dvp;
5581 	fpl->dvp_seqc = vn_seqc_read_notmodify(fpl->dvp);
5582 
5583 	mp = atomic_load_ptr(&dvp->v_mount);
5584 	if (__predict_false(mp == NULL || !cache_fplookup_mp_supported(mp))) {
5585 		return (cache_fpl_aborted(fpl));
5586 	}
5587 
5588 	error = cache_fplookup_preparse(fpl);
5589 	if (__predict_false(cache_fpl_terminated(fpl))) {
5590 		return (error);
5591 	}
5592 
5593 	for (;;) {
5594 		cache_fplookup_parse(fpl);
5595 
5596 		error = VOP_FPLOOKUP_VEXEC(fpl->dvp, cnp->cn_cred);
5597 		if (__predict_false(error != 0)) {
5598 			error = cache_fplookup_failed_vexec(fpl, error);
5599 			break;
5600 		}
5601 
5602 		error = cache_fplookup_next(fpl);
5603 		if (__predict_false(cache_fpl_terminated(fpl))) {
5604 			break;
5605 		}
5606 
5607 		VNPASS(!seqc_in_modify(fpl->tvp_seqc), fpl->tvp);
5608 
5609 		if (fpl->tvp->v_type == VLNK) {
5610 			error = cache_fplookup_symlink(fpl);
5611 			if (cache_fpl_terminated(fpl)) {
5612 				break;
5613 			}
5614 		} else {
5615 			if (cache_fpl_islastcn(ndp)) {
5616 				error = cache_fplookup_final(fpl);
5617 				break;
5618 			}
5619 
5620 			if (!vn_seqc_consistent(fpl->dvp, fpl->dvp_seqc)) {
5621 				error = cache_fpl_aborted(fpl);
5622 				break;
5623 			}
5624 
5625 			fpl->dvp = fpl->tvp;
5626 			fpl->dvp_seqc = fpl->tvp_seqc;
5627 			cache_fplookup_parse_advance(fpl);
5628 		}
5629 
5630 		cache_fpl_checkpoint(fpl);
5631 	}
5632 
5633 	return (error);
5634 }
5635 
5636 /*
5637  * Fast path lookup protected with SMR and sequence counters.
5638  *
5639  * Note: all VOP_FPLOOKUP_VEXEC routines have a comment referencing this one.
5640  *
5641  * Filesystems can opt in by setting the MNTK_FPLOOKUP flag and meeting criteria
5642  * outlined below.
5643  *
5644  * Traditional vnode lookup conceptually looks like this:
5645  *
5646  * vn_lock(current);
5647  * for (;;) {
5648  *	next = find();
5649  *	vn_lock(next);
5650  *	vn_unlock(current);
5651  *	current = next;
5652  *	if (last)
5653  *	    break;
5654  * }
5655  * return (current);
5656  *
5657  * Each jump to the next vnode is safe memory-wise and atomic with respect to
5658  * any modifications thanks to holding respective locks.
5659  *
5660  * The same guarantee can be provided with a combination of safe memory
5661  * reclamation and sequence counters instead. If all operations which affect
5662  * the relationship between the current vnode and the one we are looking for
5663  * also modify the counter, we can verify whether all the conditions held as
5664  * we made the jump. This includes things like permissions, mount points etc.
5665  * Counter modification is provided by enclosing relevant places in
5666  * vn_seqc_write_begin()/end() calls.
5667  *
5668  * Thus this translates to:
5669  *
5670  * vfs_smr_enter();
5671  * dvp_seqc = seqc_read_any(dvp);
5672  * if (seqc_in_modify(dvp_seqc)) // someone is altering the vnode
5673  *     abort();
5674  * for (;;) {
5675  * 	tvp = find();
5676  * 	tvp_seqc = seqc_read_any(tvp);
5677  * 	if (seqc_in_modify(tvp_seqc)) // someone is altering the target vnode
5678  * 	    abort();
5679  * 	if (!seqc_consistent(dvp, dvp_seqc) // someone is altering the vnode
5680  * 	    abort();
5681  * 	dvp = tvp; // we know nothing of importance has changed
5682  * 	dvp_seqc = tvp_seqc; // store the counter for the tvp iteration
5683  * 	if (last)
5684  * 	    break;
5685  * }
5686  * vget(); // secure the vnode
5687  * if (!seqc_consistent(tvp, tvp_seqc) // final check
5688  * 	    abort();
5689  * // at this point we know nothing has changed for any parent<->child pair
5690  * // as they were crossed during the lookup, meaning we matched the guarantee
5691  * // of the locked variant
5692  * return (tvp);
5693  *
5694  * The API contract for VOP_FPLOOKUP_VEXEC routines is as follows:
5695  * - they are called while within vfs_smr protection which they must never exit
5696  * - EAGAIN can be returned to denote checking could not be performed, it is
5697  *   always valid to return it
5698  * - if the sequence counter has not changed the result must be valid
5699  * - if the sequence counter has changed both false positives and false negatives
5700  *   are permitted (since the result will be rejected later)
5701  * - for simple cases of unix permission checks vaccess_vexec_smr can be used
5702  *
5703  * Caveats to watch out for:
5704  * - vnodes are passed unlocked and unreferenced with nothing stopping
5705  *   VOP_RECLAIM, in turn meaning that ->v_data can become NULL. It is advised
5706  *   to use atomic_load_ptr to fetch it.
5707  * - the aforementioned object can also get freed, meaning absent other means it
5708  *   should be protected with vfs_smr
5709  * - either safely checking permissions as they are modified or guaranteeing
5710  *   their stability is left to the routine
5711  */
5712 int
5713 cache_fplookup(struct nameidata *ndp, enum cache_fpl_status *status,
5714     struct pwd **pwdp)
5715 {
5716 	struct cache_fpl fpl;
5717 	struct pwd *pwd;
5718 	struct vnode *dvp;
5719 	struct componentname *cnp;
5720 	int error;
5721 
5722 	fpl.status = CACHE_FPL_STATUS_UNSET;
5723 	fpl.in_smr = false;
5724 	fpl.ndp = ndp;
5725 	fpl.cnp = cnp = &ndp->ni_cnd;
5726 	MPASS(ndp->ni_lcf == 0);
5727 	MPASS(curthread == cnp->cn_thread);
5728 	KASSERT ((cnp->cn_flags & CACHE_FPL_INTERNAL_CN_FLAGS) == 0,
5729 	    ("%s: internal flags found in cn_flags %" PRIx64, __func__,
5730 	    cnp->cn_flags));
5731 	if ((cnp->cn_flags & SAVESTART) != 0) {
5732 		MPASS(cnp->cn_nameiop != LOOKUP);
5733 	}
5734 	MPASS(cnp->cn_nameptr == cnp->cn_pnbuf);
5735 
5736 	if (__predict_false(!cache_can_fplookup(&fpl))) {
5737 		*status = fpl.status;
5738 		SDT_PROBE3(vfs, fplookup, lookup, done, ndp, fpl.line, fpl.status);
5739 		return (EOPNOTSUPP);
5740 	}
5741 
5742 	cache_fpl_checkpoint_outer(&fpl);
5743 
5744 	cache_fpl_smr_enter_initial(&fpl);
5745 #ifdef INVARIANTS
5746 	fpl.debug.ni_pathlen = ndp->ni_pathlen;
5747 #endif
5748 	fpl.nulchar = &cnp->cn_nameptr[ndp->ni_pathlen - 1];
5749 	fpl.fsearch = false;
5750 	fpl.savename = (cnp->cn_flags & SAVENAME) != 0;
5751 	fpl.pwd = pwdp;
5752 	pwd = pwd_get_smr();
5753 	*(fpl.pwd) = pwd;
5754 	ndp->ni_rootdir = pwd->pwd_rdir;
5755 	ndp->ni_topdir = pwd->pwd_jdir;
5756 
5757 	if (cnp->cn_pnbuf[0] == '/') {
5758 		dvp = cache_fpl_handle_root(&fpl);
5759 		MPASS(ndp->ni_resflags == 0);
5760 		ndp->ni_resflags = NIRES_ABS;
5761 	} else {
5762 		if (ndp->ni_dirfd == AT_FDCWD) {
5763 			dvp = pwd->pwd_cdir;
5764 		} else {
5765 			error = cache_fplookup_dirfd(&fpl, &dvp);
5766 			if (__predict_false(error != 0)) {
5767 				goto out;
5768 			}
5769 		}
5770 	}
5771 
5772 	SDT_PROBE4(vfs, namei, lookup, entry, dvp, cnp->cn_pnbuf, cnp->cn_flags, true);
5773 	error = cache_fplookup_impl(dvp, &fpl);
5774 out:
5775 	cache_fpl_smr_assert_not_entered(&fpl);
5776 	cache_fpl_assert_status(&fpl);
5777 	*status = fpl.status;
5778 	if (SDT_PROBES_ENABLED()) {
5779 		SDT_PROBE3(vfs, fplookup, lookup, done, ndp, fpl.line, fpl.status);
5780 		if (fpl.status == CACHE_FPL_STATUS_HANDLED)
5781 			SDT_PROBE4(vfs, namei, lookup, return, error, ndp->ni_vp, true,
5782 			    ndp);
5783 	}
5784 
5785 	if (__predict_true(fpl.status == CACHE_FPL_STATUS_HANDLED)) {
5786 		MPASS(error != CACHE_FPL_FAILED);
5787 		if (error != 0) {
5788 			MPASS(fpl.dvp == NULL);
5789 			MPASS(fpl.tvp == NULL);
5790 			MPASS(fpl.savename == false);
5791 		}
5792 		ndp->ni_dvp = fpl.dvp;
5793 		ndp->ni_vp = fpl.tvp;
5794 		if (fpl.savename) {
5795 			cnp->cn_flags |= HASBUF;
5796 		} else {
5797 			cache_fpl_cleanup_cnp(cnp);
5798 		}
5799 	}
5800 	return (error);
5801 }
5802