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