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