]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/vfs_cache.c
vfs: clean MNTK_FPLOOKUP if MNT_UNION is set
[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/rwlock.h>
59 #include <sys/seqc.h>
60 #include <sys/sdt.h>
61 #include <sys/smr.h>
62 #include <sys/smp.h>
63 #include <sys/syscallsubr.h>
64 #include <sys/sysctl.h>
65 #include <sys/sysproto.h>
66 #include <sys/vnode.h>
67 #include <ck_queue.h>
68 #ifdef KTRACE
69 #include <sys/ktrace.h>
70 #endif
71
72 #include <sys/capsicum.h>
73
74 #include <security/audit/audit.h>
75 #include <security/mac/mac_framework.h>
76
77 #ifdef DDB
78 #include <ddb/ddb.h>
79 #endif
80
81 #include <vm/uma.h>
82
83 SDT_PROVIDER_DECLARE(vfs);
84 SDT_PROBE_DEFINE3(vfs, namecache, enter, done, "struct vnode *", "char *",
85     "struct vnode *");
86 SDT_PROBE_DEFINE2(vfs, namecache, enter_negative, done, "struct vnode *",
87     "char *");
88 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, entry, "struct vnode *");
89 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, hit, "struct vnode *",
90     "char *", "struct vnode *");
91 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, miss, "struct vnode *");
92 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, return, "int",
93     "struct vnode *", "char *");
94 SDT_PROBE_DEFINE3(vfs, namecache, lookup, hit, "struct vnode *", "char *",
95     "struct vnode *");
96 SDT_PROBE_DEFINE2(vfs, namecache, lookup, hit__negative,
97     "struct vnode *", "char *");
98 SDT_PROBE_DEFINE2(vfs, namecache, lookup, miss, "struct vnode *",
99     "char *");
100 SDT_PROBE_DEFINE1(vfs, namecache, purge, done, "struct vnode *");
101 SDT_PROBE_DEFINE1(vfs, namecache, purge_negative, done, "struct vnode *");
102 SDT_PROBE_DEFINE1(vfs, namecache, purgevfs, done, "struct mount *");
103 SDT_PROBE_DEFINE3(vfs, namecache, zap, done, "struct vnode *", "char *",
104     "struct vnode *");
105 SDT_PROBE_DEFINE2(vfs, namecache, zap_negative, done, "struct vnode *",
106     "char *");
107 SDT_PROBE_DEFINE2(vfs, namecache, shrink_negative, done, "struct vnode *",
108     "char *");
109
110 SDT_PROBE_DEFINE3(vfs, fplookup, lookup, done, "struct nameidata", "int", "bool");
111 SDT_PROBE_DECLARE(vfs, namei, lookup, entry);
112 SDT_PROBE_DECLARE(vfs, namei, lookup, return);
113
114 /*
115  * This structure describes the elements in the cache of recent
116  * names looked up by namei.
117  */
118 struct negstate {
119         u_char neg_flag;
120 };
121 _Static_assert(sizeof(struct negstate) <= sizeof(struct vnode *),
122     "the state must fit in a union with a pointer without growing it");
123
124 struct  namecache {
125         LIST_ENTRY(namecache) nc_src;   /* source vnode list */
126         TAILQ_ENTRY(namecache) nc_dst;  /* destination vnode list */
127         CK_SLIST_ENTRY(namecache) nc_hash;/* hash chain */
128         struct  vnode *nc_dvp;          /* vnode of parent of name */
129         union {
130                 struct  vnode *nu_vp;   /* vnode the name refers to */
131                 struct  negstate nu_neg;/* negative entry state */
132         } n_un;
133         u_char  nc_flag;                /* flag bits */
134         u_char  nc_nlen;                /* length of name */
135         char    nc_name[0];             /* segment name + nul */
136 };
137
138 /*
139  * struct namecache_ts repeats struct namecache layout up to the
140  * nc_nlen member.
141  * struct namecache_ts is used in place of struct namecache when time(s) need
142  * to be stored.  The nc_dotdottime field is used when a cache entry is mapping
143  * both a non-dotdot directory name plus dotdot for the directory's
144  * parent.
145  *
146  * See below for alignment requirement.
147  */
148 struct  namecache_ts {
149         struct  timespec nc_time;       /* timespec provided by fs */
150         struct  timespec nc_dotdottime; /* dotdot timespec provided by fs */
151         int     nc_ticks;               /* ticks value when entry was added */
152         struct namecache nc_nc;
153 };
154
155 /*
156  * At least mips n32 performs 64-bit accesses to timespec as found
157  * in namecache_ts and requires them to be aligned. Since others
158  * may be in the same spot suffer a little bit and enforce the
159  * alignment for everyone. Note this is a nop for 64-bit platforms.
160  */
161 #define CACHE_ZONE_ALIGNMENT    UMA_ALIGNOF(time_t)
162
163 #define nc_vp           n_un.nu_vp
164 #define nc_neg          n_un.nu_neg
165
166 /*
167  * Flags in namecache.nc_flag
168  */
169 #define NCF_WHITE       0x01
170 #define NCF_ISDOTDOT    0x02
171 #define NCF_TS          0x04
172 #define NCF_DTS         0x08
173 #define NCF_DVDROP      0x10
174 #define NCF_NEGATIVE    0x20
175 #define NCF_INVALID     0x40
176 #define NCF_WIP         0x80
177
178 /*
179  * Flags in negstate.neg_flag
180  */
181 #define NEG_HOT         0x01
182
183 /*
184  * Mark an entry as invalid.
185  *
186  * This is called before it starts getting deconstructed.
187  */
188 static void
189 cache_ncp_invalidate(struct namecache *ncp)
190 {
191
192         KASSERT((ncp->nc_flag & NCF_INVALID) == 0,
193             ("%s: entry %p already invalid", __func__, ncp));
194         atomic_store_char(&ncp->nc_flag, ncp->nc_flag | NCF_INVALID);
195         atomic_thread_fence_rel();
196 }
197
198 /*
199  * Check whether the entry can be safely used.
200  *
201  * All places which elide locks are supposed to call this after they are
202  * done with reading from an entry.
203  */
204 static bool
205 cache_ncp_canuse(struct namecache *ncp)
206 {
207
208         atomic_thread_fence_acq();
209         return ((atomic_load_char(&ncp->nc_flag) & (NCF_INVALID | NCF_WIP)) == 0);
210 }
211
212 /*
213  * Name caching works as follows:
214  *
215  * Names found by directory scans are retained in a cache
216  * for future reference.  It is managed LRU, so frequently
217  * used names will hang around.  Cache is indexed by hash value
218  * obtained from (dvp, name) where dvp refers to the directory
219  * containing name.
220  *
221  * If it is a "negative" entry, (i.e. for a name that is known NOT to
222  * exist) the vnode pointer will be NULL.
223  *
224  * Upon reaching the last segment of a path, if the reference
225  * is for DELETE, or NOCACHE is set (rewrite), and the
226  * name is located in the cache, it will be dropped.
227  *
228  * These locks are used (in the order in which they can be taken):
229  * NAME         TYPE    ROLE
230  * vnodelock    mtx     vnode lists and v_cache_dd field protection
231  * bucketlock   rwlock  for access to given set of hash buckets
232  * neglist      mtx     negative entry LRU management
233  *
234  * Additionally, ncneg_shrink_lock mtx is used to have at most one thread
235  * shrinking the LRU list.
236  *
237  * It is legal to take multiple vnodelock and bucketlock locks. The locking
238  * order is lower address first. Both are recursive.
239  *
240  * "." lookups are lockless.
241  *
242  * ".." and vnode -> name lookups require vnodelock.
243  *
244  * name -> vnode lookup requires the relevant bucketlock to be held for reading.
245  *
246  * Insertions and removals of entries require involved vnodes and bucketlocks
247  * to be write-locked to prevent other threads from seeing the entry.
248  *
249  * Some lookups result in removal of the found entry (e.g. getting rid of a
250  * negative entry with the intent to create a positive one), which poses a
251  * problem when multiple threads reach the state. Similarly, two different
252  * threads can purge two different vnodes and try to remove the same name.
253  *
254  * If the already held vnode lock is lower than the second required lock, we
255  * can just take the other lock. However, in the opposite case, this could
256  * deadlock. As such, this is resolved by trylocking and if that fails unlocking
257  * the first node, locking everything in order and revalidating the state.
258  */
259
260 VFS_SMR_DECLARE;
261
262 /*
263  * Structures associated with name caching.
264  */
265 #define NCHHASH(hash) \
266         (&nchashtbl[(hash) & nchash])
267 static __read_mostly CK_SLIST_HEAD(nchashhead, namecache) *nchashtbl;/* Hash Table */
268 static u_long __read_mostly     nchash;                 /* size of hash table */
269 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0,
270     "Size of namecache hash table");
271 static u_long __read_mostly     ncnegfactor = 5; /* ratio of negative entries */
272 SYSCTL_ULONG(_vfs, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0,
273     "Ratio of negative namecache entries");
274 static u_long __exclusive_cache_line    numneg; /* number of negative entries allocated */
275 static u_long __exclusive_cache_line    numcache;/* number of cache entries allocated */
276 u_int ncsizefactor = 2;
277 SYSCTL_UINT(_vfs, OID_AUTO, ncsizefactor, CTLFLAG_RW, &ncsizefactor, 0,
278     "Size factor for namecache");
279 static u_int __read_mostly      ncpurgeminvnodes;
280 SYSCTL_UINT(_vfs, OID_AUTO, ncpurgeminvnodes, CTLFLAG_RW, &ncpurgeminvnodes, 0,
281     "Number of vnodes below which purgevfs ignores the request");
282 static u_int __read_mostly      ncsize; /* the size as computed on creation or resizing */
283
284 struct nchstats nchstats;               /* cache effectiveness statistics */
285
286 static struct mtx __exclusive_cache_line        ncneg_shrink_lock;
287
288 struct neglist {
289         struct mtx              nl_lock;
290         TAILQ_HEAD(, namecache) nl_list;
291 } __aligned(CACHE_LINE_SIZE);
292
293 static struct neglist __read_mostly     *neglists;
294 static struct neglist ncneg_hot;
295 static u_long numhotneg;
296
297 #define ncneghash       3
298 #define numneglists     (ncneghash + 1)
299 static inline struct neglist *
300 NCP2NEGLIST(struct namecache *ncp)
301 {
302
303         return (&neglists[(((uintptr_t)(ncp) >> 8) & ncneghash)]);
304 }
305
306 static inline struct negstate *
307 NCP2NEGSTATE(struct namecache *ncp)
308 {
309
310         MPASS(ncp->nc_flag & NCF_NEGATIVE);
311         return (&ncp->nc_neg);
312 }
313
314 #define numbucketlocks (ncbuckethash + 1)
315 static u_int __read_mostly  ncbuckethash;
316 static struct rwlock_padalign __read_mostly  *bucketlocks;
317 #define HASH2BUCKETLOCK(hash) \
318         ((struct rwlock *)(&bucketlocks[((hash) & ncbuckethash)]))
319
320 #define numvnodelocks (ncvnodehash + 1)
321 static u_int __read_mostly  ncvnodehash;
322 static struct mtx __read_mostly *vnodelocks;
323 static inline struct mtx *
324 VP2VNODELOCK(struct vnode *vp)
325 {
326
327         return (&vnodelocks[(((uintptr_t)(vp) >> 8) & ncvnodehash)]);
328 }
329
330 /*
331  * UMA zones for the VFS cache.
332  *
333  * The small cache is used for entries with short names, which are the
334  * most common.  The large cache is used for entries which are too big to
335  * fit in the small cache.
336  */
337 static uma_zone_t __read_mostly cache_zone_small;
338 static uma_zone_t __read_mostly cache_zone_small_ts;
339 static uma_zone_t __read_mostly cache_zone_large;
340 static uma_zone_t __read_mostly cache_zone_large_ts;
341
342 #define CACHE_PATH_CUTOFF       35
343
344 static struct namecache *
345 cache_alloc(int len, int ts)
346 {
347         struct namecache_ts *ncp_ts;
348         struct namecache *ncp;
349
350         if (__predict_false(ts)) {
351                 if (len <= CACHE_PATH_CUTOFF)
352                         ncp_ts = uma_zalloc_smr(cache_zone_small_ts, M_WAITOK);
353                 else
354                         ncp_ts = uma_zalloc_smr(cache_zone_large_ts, M_WAITOK);
355                 ncp = &ncp_ts->nc_nc;
356         } else {
357                 if (len <= CACHE_PATH_CUTOFF)
358                         ncp = uma_zalloc_smr(cache_zone_small, M_WAITOK);
359                 else
360                         ncp = uma_zalloc_smr(cache_zone_large, M_WAITOK);
361         }
362         return (ncp);
363 }
364
365 static void
366 cache_free(struct namecache *ncp)
367 {
368         struct namecache_ts *ncp_ts;
369
370         if (ncp == NULL)
371                 return;
372         if ((ncp->nc_flag & NCF_DVDROP) != 0)
373                 vdrop(ncp->nc_dvp);
374         if (__predict_false(ncp->nc_flag & NCF_TS)) {
375                 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
376                 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
377                         uma_zfree_smr(cache_zone_small_ts, ncp_ts);
378                 else
379                         uma_zfree_smr(cache_zone_large_ts, ncp_ts);
380         } else {
381                 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
382                         uma_zfree_smr(cache_zone_small, ncp);
383                 else
384                         uma_zfree_smr(cache_zone_large, ncp);
385         }
386 }
387
388 static void
389 cache_out_ts(struct namecache *ncp, struct timespec *tsp, int *ticksp)
390 {
391         struct namecache_ts *ncp_ts;
392
393         KASSERT((ncp->nc_flag & NCF_TS) != 0 ||
394             (tsp == NULL && ticksp == NULL),
395             ("No NCF_TS"));
396
397         if (tsp == NULL && ticksp == NULL)
398                 return;
399
400         ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
401         if (tsp != NULL)
402                 *tsp = ncp_ts->nc_time;
403         if (ticksp != NULL)
404                 *ticksp = ncp_ts->nc_ticks;
405 }
406
407 #ifdef DEBUG_CACHE
408 static int __read_mostly        doingcache = 1; /* 1 => enable the cache */
409 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0,
410     "VFS namecache enabled");
411 #endif
412
413 /* Export size information to userland */
414 SYSCTL_INT(_debug_sizeof, OID_AUTO, namecache, CTLFLAG_RD, SYSCTL_NULL_INT_PTR,
415     sizeof(struct namecache), "sizeof(struct namecache)");
416
417 /*
418  * The new name cache statistics
419  */
420 static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
421     "Name cache statistics");
422 #define STATNODE_ULONG(name, descr)                                     \
423         SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, descr);
424 #define STATNODE_COUNTER(name, descr)                                   \
425         static COUNTER_U64_DEFINE_EARLY(name);                          \
426         SYSCTL_COUNTER_U64(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, \
427             descr);
428 STATNODE_ULONG(numneg, "Number of negative cache entries");
429 STATNODE_ULONG(numcache, "Number of cache entries");
430 STATNODE_COUNTER(numcachehv, "Number of namecache entries with vnodes held");
431 STATNODE_COUNTER(numdrops, "Number of dropped entries due to reaching the limit");
432 STATNODE_COUNTER(dothits, "Number of '.' hits");
433 STATNODE_COUNTER(dotdothits, "Number of '..' hits");
434 STATNODE_COUNTER(nummiss, "Number of cache misses");
435 STATNODE_COUNTER(nummisszap, "Number of cache misses we do not want to cache");
436 STATNODE_COUNTER(numposzaps,
437     "Number of cache hits (positive) we do not want to cache");
438 STATNODE_COUNTER(numposhits, "Number of cache hits (positive)");
439 STATNODE_COUNTER(numnegzaps,
440     "Number of cache hits (negative) we do not want to cache");
441 STATNODE_COUNTER(numneghits, "Number of cache hits (negative)");
442 /* These count for vn_getcwd(), too. */
443 STATNODE_COUNTER(numfullpathcalls, "Number of fullpath search calls");
444 STATNODE_COUNTER(numfullpathfail1, "Number of fullpath search errors (ENOTDIR)");
445 STATNODE_COUNTER(numfullpathfail2,
446     "Number of fullpath search errors (VOP_VPTOCNP failures)");
447 STATNODE_COUNTER(numfullpathfail4, "Number of fullpath search errors (ENOMEM)");
448 STATNODE_COUNTER(numfullpathfound, "Number of successful fullpath calls");
449 STATNODE_COUNTER(zap_and_exit_bucket_relock_success,
450     "Number of successful removals after relocking");
451 static long zap_and_exit_bucket_fail; STATNODE_ULONG(zap_and_exit_bucket_fail,
452     "Number of times zap_and_exit failed to lock");
453 static long zap_and_exit_bucket_fail2; STATNODE_ULONG(zap_and_exit_bucket_fail2,
454     "Number of times zap_and_exit failed to lock");
455 static long cache_lock_vnodes_cel_3_failures;
456 STATNODE_ULONG(cache_lock_vnodes_cel_3_failures,
457     "Number of times 3-way vnode locking failed");
458 STATNODE_ULONG(numhotneg, "Number of hot negative entries");
459 STATNODE_COUNTER(numneg_evicted,
460     "Number of negative entries evicted when adding a new entry");
461 STATNODE_COUNTER(shrinking_skipped,
462     "Number of times shrinking was already in progress");
463
464 static void cache_zap_locked(struct namecache *ncp);
465 static int vn_fullpath_hardlink(struct thread *td, struct nameidata *ndp, char **retbuf,
466     char **freebuf, size_t *buflen);
467 static int vn_fullpath_any(struct thread *td, struct vnode *vp, struct vnode *rdir,
468     char *buf, char **retbuf, size_t *buflen);
469 static int vn_fullpath_dir(struct thread *td, struct vnode *vp, struct vnode *rdir,
470     char *buf, char **retbuf, size_t *len, bool slash_prefixed, size_t addend);
471
472 static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
473
474 static int cache_yield;
475 SYSCTL_INT(_vfs_cache, OID_AUTO, yield, CTLFLAG_RD, &cache_yield, 0,
476     "Number of times cache called yield");
477
478 static void __noinline
479 cache_maybe_yield(void)
480 {
481
482         if (should_yield()) {
483                 cache_yield++;
484                 kern_yield(PRI_USER);
485         }
486 }
487
488 static inline void
489 cache_assert_vlp_locked(struct mtx *vlp)
490 {
491
492         if (vlp != NULL)
493                 mtx_assert(vlp, MA_OWNED);
494 }
495
496 static inline void
497 cache_assert_vnode_locked(struct vnode *vp)
498 {
499         struct mtx *vlp;
500
501         vlp = VP2VNODELOCK(vp);
502         cache_assert_vlp_locked(vlp);
503 }
504
505 /*
506  * TODO: With the value stored we can do better than computing the hash based
507  * on the address and the choice of FNV should also be revisisted.
508  */
509 static void
510 cache_prehash(struct vnode *vp)
511 {
512
513         vp->v_nchash = fnv_32_buf(&vp, sizeof(vp), FNV1_32_INIT);
514 }
515
516 static uint32_t
517 cache_get_hash(char *name, u_char len, struct vnode *dvp)
518 {
519
520         return (fnv_32_buf(name, len, dvp->v_nchash));
521 }
522
523 static inline struct nchashhead *
524 NCP2BUCKET(struct namecache *ncp)
525 {
526         uint32_t hash;
527
528         hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen, ncp->nc_dvp);
529         return (NCHHASH(hash));
530 }
531
532 static inline struct rwlock *
533 NCP2BUCKETLOCK(struct namecache *ncp)
534 {
535         uint32_t hash;
536
537         hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen, ncp->nc_dvp);
538         return (HASH2BUCKETLOCK(hash));
539 }
540
541 #ifdef INVARIANTS
542 static void
543 cache_assert_bucket_locked(struct namecache *ncp, int mode)
544 {
545         struct rwlock *blp;
546
547         blp = NCP2BUCKETLOCK(ncp);
548         rw_assert(blp, mode);
549 }
550 #else
551 #define cache_assert_bucket_locked(x, y) do { } while (0)
552 #endif
553
554 #define cache_sort_vnodes(x, y) _cache_sort_vnodes((void **)(x), (void **)(y))
555 static void
556 _cache_sort_vnodes(void **p1, void **p2)
557 {
558         void *tmp;
559
560         MPASS(*p1 != NULL || *p2 != NULL);
561
562         if (*p1 > *p2) {
563                 tmp = *p2;
564                 *p2 = *p1;
565                 *p1 = tmp;
566         }
567 }
568
569 static void
570 cache_lock_all_buckets(void)
571 {
572         u_int i;
573
574         for (i = 0; i < numbucketlocks; i++)
575                 rw_wlock(&bucketlocks[i]);
576 }
577
578 static void
579 cache_unlock_all_buckets(void)
580 {
581         u_int i;
582
583         for (i = 0; i < numbucketlocks; i++)
584                 rw_wunlock(&bucketlocks[i]);
585 }
586
587 static void
588 cache_lock_all_vnodes(void)
589 {
590         u_int i;
591
592         for (i = 0; i < numvnodelocks; i++)
593                 mtx_lock(&vnodelocks[i]);
594 }
595
596 static void
597 cache_unlock_all_vnodes(void)
598 {
599         u_int i;
600
601         for (i = 0; i < numvnodelocks; i++)
602                 mtx_unlock(&vnodelocks[i]);
603 }
604
605 static int
606 cache_trylock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
607 {
608
609         cache_sort_vnodes(&vlp1, &vlp2);
610
611         if (vlp1 != NULL) {
612                 if (!mtx_trylock(vlp1))
613                         return (EAGAIN);
614         }
615         if (!mtx_trylock(vlp2)) {
616                 if (vlp1 != NULL)
617                         mtx_unlock(vlp1);
618                 return (EAGAIN);
619         }
620
621         return (0);
622 }
623
624 static void
625 cache_lock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
626 {
627
628         MPASS(vlp1 != NULL || vlp2 != NULL);
629         MPASS(vlp1 <= vlp2);
630
631         if (vlp1 != NULL)
632                 mtx_lock(vlp1);
633         if (vlp2 != NULL)
634                 mtx_lock(vlp2);
635 }
636
637 static void
638 cache_unlock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
639 {
640
641         MPASS(vlp1 != NULL || vlp2 != NULL);
642
643         if (vlp1 != NULL)
644                 mtx_unlock(vlp1);
645         if (vlp2 != NULL)
646                 mtx_unlock(vlp2);
647 }
648
649 static int
650 sysctl_nchstats(SYSCTL_HANDLER_ARGS)
651 {
652         struct nchstats snap;
653
654         if (req->oldptr == NULL)
655                 return (SYSCTL_OUT(req, 0, sizeof(snap)));
656
657         snap = nchstats;
658         snap.ncs_goodhits = counter_u64_fetch(numposhits);
659         snap.ncs_neghits = counter_u64_fetch(numneghits);
660         snap.ncs_badhits = counter_u64_fetch(numposzaps) +
661             counter_u64_fetch(numnegzaps);
662         snap.ncs_miss = counter_u64_fetch(nummisszap) +
663             counter_u64_fetch(nummiss);
664
665         return (SYSCTL_OUT(req, &snap, sizeof(snap)));
666 }
667 SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE | CTLFLAG_RD |
668     CTLFLAG_MPSAFE, 0, 0, sysctl_nchstats, "LU",
669     "VFS cache effectiveness statistics");
670
671 #ifdef DIAGNOSTIC
672 /*
673  * Grab an atomic snapshot of the name cache hash chain lengths
674  */
675 static SYSCTL_NODE(_debug, OID_AUTO, hashstat,
676     CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
677     "hash table stats");
678
679 static int
680 sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS)
681 {
682         struct nchashhead *ncpp;
683         struct namecache *ncp;
684         int i, error, n_nchash, *cntbuf;
685
686 retry:
687         n_nchash = nchash + 1;  /* nchash is max index, not count */
688         if (req->oldptr == NULL)
689                 return SYSCTL_OUT(req, 0, n_nchash * sizeof(int));
690         cntbuf = malloc(n_nchash * sizeof(int), M_TEMP, M_ZERO | M_WAITOK);
691         cache_lock_all_buckets();
692         if (n_nchash != nchash + 1) {
693                 cache_unlock_all_buckets();
694                 free(cntbuf, M_TEMP);
695                 goto retry;
696         }
697         /* Scan hash tables counting entries */
698         for (ncpp = nchashtbl, i = 0; i < n_nchash; ncpp++, i++)
699                 CK_SLIST_FOREACH(ncp, ncpp, nc_hash)
700                         cntbuf[i]++;
701         cache_unlock_all_buckets();
702         for (error = 0, i = 0; i < n_nchash; i++)
703                 if ((error = SYSCTL_OUT(req, &cntbuf[i], sizeof(int))) != 0)
704                         break;
705         free(cntbuf, M_TEMP);
706         return (error);
707 }
708 SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD|
709     CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_rawnchash, "S,int",
710     "nchash chain lengths");
711
712 static int
713 sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS)
714 {
715         int error;
716         struct nchashhead *ncpp;
717         struct namecache *ncp;
718         int n_nchash;
719         int count, maxlength, used, pct;
720
721         if (!req->oldptr)
722                 return SYSCTL_OUT(req, 0, 4 * sizeof(int));
723
724         cache_lock_all_buckets();
725         n_nchash = nchash + 1;  /* nchash is max index, not count */
726         used = 0;
727         maxlength = 0;
728
729         /* Scan hash tables for applicable entries */
730         for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
731                 count = 0;
732                 CK_SLIST_FOREACH(ncp, ncpp, nc_hash) {
733                         count++;
734                 }
735                 if (count)
736                         used++;
737                 if (maxlength < count)
738                         maxlength = count;
739         }
740         n_nchash = nchash + 1;
741         cache_unlock_all_buckets();
742         pct = (used * 100) / (n_nchash / 100);
743         error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash));
744         if (error)
745                 return (error);
746         error = SYSCTL_OUT(req, &used, sizeof(used));
747         if (error)
748                 return (error);
749         error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength));
750         if (error)
751                 return (error);
752         error = SYSCTL_OUT(req, &pct, sizeof(pct));
753         if (error)
754                 return (error);
755         return (0);
756 }
757 SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD|
758     CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_nchash, "I",
759     "nchash statistics (number of total/used buckets, maximum chain length, usage percentage)");
760 #endif
761
762 /*
763  * Negative entries management
764  *
765  * A variation of LRU scheme is used. New entries are hashed into one of
766  * numneglists cold lists. Entries get promoted to the hot list on first hit.
767  *
768  * The shrinker will demote hot list head and evict from the cold list in a
769  * round-robin manner.
770  */
771 static void
772 cache_negative_init(struct namecache *ncp)
773 {
774         struct negstate *negstate;
775
776         ncp->nc_flag |= NCF_NEGATIVE;
777         negstate = NCP2NEGSTATE(ncp);
778         negstate->neg_flag = 0;
779 }
780
781 static void
782 cache_negative_hit(struct namecache *ncp)
783 {
784         struct neglist *neglist;
785         struct negstate *negstate;
786
787         negstate = NCP2NEGSTATE(ncp);
788         if ((negstate->neg_flag & NEG_HOT) != 0)
789                 return;
790         neglist = NCP2NEGLIST(ncp);
791         mtx_lock(&ncneg_hot.nl_lock);
792         mtx_lock(&neglist->nl_lock);
793         if ((negstate->neg_flag & NEG_HOT) == 0) {
794                 numhotneg++;
795                 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
796                 TAILQ_INSERT_TAIL(&ncneg_hot.nl_list, ncp, nc_dst);
797                 negstate->neg_flag |= NEG_HOT;
798         }
799         mtx_unlock(&neglist->nl_lock);
800         mtx_unlock(&ncneg_hot.nl_lock);
801 }
802
803 static void
804 cache_negative_insert(struct namecache *ncp)
805 {
806         struct neglist *neglist;
807
808         MPASS(ncp->nc_flag & NCF_NEGATIVE);
809         cache_assert_bucket_locked(ncp, RA_WLOCKED);
810         neglist = NCP2NEGLIST(ncp);
811         mtx_lock(&neglist->nl_lock);
812         TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst);
813         mtx_unlock(&neglist->nl_lock);
814         atomic_add_rel_long(&numneg, 1);
815 }
816
817 static void
818 cache_negative_remove(struct namecache *ncp)
819 {
820         struct neglist *neglist;
821         struct negstate *negstate;
822         bool hot_locked = false;
823         bool list_locked = false;
824
825         cache_assert_bucket_locked(ncp, RA_WLOCKED);
826         neglist = NCP2NEGLIST(ncp);
827         negstate = NCP2NEGSTATE(ncp);
828         if ((negstate->neg_flag & NEG_HOT) != 0) {
829                 hot_locked = true;
830                 mtx_lock(&ncneg_hot.nl_lock);
831                 if ((negstate->neg_flag & NEG_HOT) == 0) {
832                         list_locked = true;
833                         mtx_lock(&neglist->nl_lock);
834                 }
835         } else {
836                 list_locked = true;
837                 mtx_lock(&neglist->nl_lock);
838                 /*
839                  * We may be racing against promotion in lockless lookup.
840                  */
841                 if ((negstate->neg_flag & NEG_HOT) != 0) {
842                         mtx_unlock(&neglist->nl_lock);
843                         hot_locked = true;
844                         mtx_lock(&ncneg_hot.nl_lock);
845                         mtx_lock(&neglist->nl_lock);
846                 }
847         }
848         if ((negstate->neg_flag & NEG_HOT) != 0) {
849                 mtx_assert(&ncneg_hot.nl_lock, MA_OWNED);
850                 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
851                 numhotneg--;
852         } else {
853                 mtx_assert(&neglist->nl_lock, MA_OWNED);
854                 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
855         }
856         if (list_locked)
857                 mtx_unlock(&neglist->nl_lock);
858         if (hot_locked)
859                 mtx_unlock(&ncneg_hot.nl_lock);
860         atomic_subtract_rel_long(&numneg, 1);
861 }
862
863 static void
864 cache_negative_shrink_select(struct namecache **ncpp,
865     struct neglist **neglistpp)
866 {
867         struct neglist *neglist;
868         struct namecache *ncp;
869         static u_int cycle;
870         u_int i;
871
872         *ncpp = ncp = NULL;
873
874         for (i = 0; i < numneglists; i++) {
875                 neglist = &neglists[(cycle + i) % numneglists];
876                 if (TAILQ_FIRST(&neglist->nl_list) == NULL)
877                         continue;
878                 mtx_lock(&neglist->nl_lock);
879                 ncp = TAILQ_FIRST(&neglist->nl_list);
880                 if (ncp != NULL)
881                         break;
882                 mtx_unlock(&neglist->nl_lock);
883         }
884
885         *neglistpp = neglist;
886         *ncpp = ncp;
887         cycle++;
888 }
889
890 static void
891 cache_negative_zap_one(void)
892 {
893         struct namecache *ncp, *ncp2;
894         struct neglist *neglist;
895         struct negstate *negstate;
896         struct mtx *dvlp;
897         struct rwlock *blp;
898
899         if (mtx_owner(&ncneg_shrink_lock) != NULL ||
900             !mtx_trylock(&ncneg_shrink_lock)) {
901                 counter_u64_add(shrinking_skipped, 1);
902                 return;
903         }
904
905         mtx_lock(&ncneg_hot.nl_lock);
906         ncp = TAILQ_FIRST(&ncneg_hot.nl_list);
907         if (ncp != NULL) {
908                 neglist = NCP2NEGLIST(ncp);
909                 negstate = NCP2NEGSTATE(ncp);
910                 mtx_lock(&neglist->nl_lock);
911                 MPASS((negstate->neg_flag & NEG_HOT) != 0);
912                 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
913                 TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst);
914                 negstate->neg_flag &= ~NEG_HOT;
915                 numhotneg--;
916                 mtx_unlock(&neglist->nl_lock);
917         }
918         mtx_unlock(&ncneg_hot.nl_lock);
919
920         cache_negative_shrink_select(&ncp, &neglist);
921
922         mtx_unlock(&ncneg_shrink_lock);
923         if (ncp == NULL)
924                 return;
925
926         MPASS(ncp->nc_flag & NCF_NEGATIVE);
927         dvlp = VP2VNODELOCK(ncp->nc_dvp);
928         blp = NCP2BUCKETLOCK(ncp);
929         mtx_unlock(&neglist->nl_lock);
930         mtx_lock(dvlp);
931         rw_wlock(blp);
932         /*
933          * Enter SMR to safely check the negative list.
934          * Even if the found pointer matches, the entry may now be reallocated
935          * and used by a different vnode.
936          */
937         vfs_smr_enter();
938         ncp2 = TAILQ_FIRST(&neglist->nl_list);
939         if (ncp != ncp2 || dvlp != VP2VNODELOCK(ncp2->nc_dvp) ||
940             blp != NCP2BUCKETLOCK(ncp2)) {
941                 vfs_smr_exit();
942                 ncp = NULL;
943         } else {
944                 vfs_smr_exit();
945                 SDT_PROBE2(vfs, namecache, shrink_negative, done, ncp->nc_dvp,
946                     ncp->nc_name);
947                 cache_zap_locked(ncp);
948                 counter_u64_add(numneg_evicted, 1);
949         }
950         rw_wunlock(blp);
951         mtx_unlock(dvlp);
952         cache_free(ncp);
953 }
954
955 /*
956  * cache_zap_locked():
957  *
958  *   Removes a namecache entry from cache, whether it contains an actual
959  *   pointer to a vnode or if it is just a negative cache entry.
960  */
961 static void
962 cache_zap_locked(struct namecache *ncp)
963 {
964         struct nchashhead *ncpp;
965
966         if (!(ncp->nc_flag & NCF_NEGATIVE))
967                 cache_assert_vnode_locked(ncp->nc_vp);
968         cache_assert_vnode_locked(ncp->nc_dvp);
969         cache_assert_bucket_locked(ncp, RA_WLOCKED);
970
971         CTR2(KTR_VFS, "cache_zap(%p) vp %p", ncp,
972             (ncp->nc_flag & NCF_NEGATIVE) ? NULL : ncp->nc_vp);
973
974         cache_ncp_invalidate(ncp);
975
976         ncpp = NCP2BUCKET(ncp);
977         CK_SLIST_REMOVE(ncpp, ncp, namecache, nc_hash);
978         if (!(ncp->nc_flag & NCF_NEGATIVE)) {
979                 SDT_PROBE3(vfs, namecache, zap, done, ncp->nc_dvp,
980                     ncp->nc_name, ncp->nc_vp);
981                 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
982                 if (ncp == ncp->nc_vp->v_cache_dd) {
983                         vn_seqc_write_begin_unheld(ncp->nc_vp);
984                         ncp->nc_vp->v_cache_dd = NULL;
985                         vn_seqc_write_end(ncp->nc_vp);
986                 }
987         } else {
988                 SDT_PROBE2(vfs, namecache, zap_negative, done, ncp->nc_dvp,
989                     ncp->nc_name);
990                 cache_negative_remove(ncp);
991         }
992         if (ncp->nc_flag & NCF_ISDOTDOT) {
993                 if (ncp == ncp->nc_dvp->v_cache_dd) {
994                         vn_seqc_write_begin_unheld(ncp->nc_dvp);
995                         ncp->nc_dvp->v_cache_dd = NULL;
996                         vn_seqc_write_end(ncp->nc_dvp);
997                 }
998         } else {
999                 LIST_REMOVE(ncp, nc_src);
1000                 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) {
1001                         ncp->nc_flag |= NCF_DVDROP;
1002                         counter_u64_add(numcachehv, -1);
1003                 }
1004         }
1005         atomic_subtract_rel_long(&numcache, 1);
1006 }
1007
1008 static void
1009 cache_zap_negative_locked_vnode_kl(struct namecache *ncp, struct vnode *vp)
1010 {
1011         struct rwlock *blp;
1012
1013         MPASS(ncp->nc_dvp == vp);
1014         MPASS(ncp->nc_flag & NCF_NEGATIVE);
1015         cache_assert_vnode_locked(vp);
1016
1017         blp = NCP2BUCKETLOCK(ncp);
1018         rw_wlock(blp);
1019         cache_zap_locked(ncp);
1020         rw_wunlock(blp);
1021 }
1022
1023 static bool
1024 cache_zap_locked_vnode_kl2(struct namecache *ncp, struct vnode *vp,
1025     struct mtx **vlpp)
1026 {
1027         struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
1028         struct rwlock *blp;
1029
1030         MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
1031         cache_assert_vnode_locked(vp);
1032
1033         if (ncp->nc_flag & NCF_NEGATIVE) {
1034                 if (*vlpp != NULL) {
1035                         mtx_unlock(*vlpp);
1036                         *vlpp = NULL;
1037                 }
1038                 cache_zap_negative_locked_vnode_kl(ncp, vp);
1039                 return (true);
1040         }
1041
1042         pvlp = VP2VNODELOCK(vp);
1043         blp = NCP2BUCKETLOCK(ncp);
1044         vlp1 = VP2VNODELOCK(ncp->nc_dvp);
1045         vlp2 = VP2VNODELOCK(ncp->nc_vp);
1046
1047         if (*vlpp == vlp1 || *vlpp == vlp2) {
1048                 to_unlock = *vlpp;
1049                 *vlpp = NULL;
1050         } else {
1051                 if (*vlpp != NULL) {
1052                         mtx_unlock(*vlpp);
1053                         *vlpp = NULL;
1054                 }
1055                 cache_sort_vnodes(&vlp1, &vlp2);
1056                 if (vlp1 == pvlp) {
1057                         mtx_lock(vlp2);
1058                         to_unlock = vlp2;
1059                 } else {
1060                         if (!mtx_trylock(vlp1))
1061                                 goto out_relock;
1062                         to_unlock = vlp1;
1063                 }
1064         }
1065         rw_wlock(blp);
1066         cache_zap_locked(ncp);
1067         rw_wunlock(blp);
1068         if (to_unlock != NULL)
1069                 mtx_unlock(to_unlock);
1070         return (true);
1071
1072 out_relock:
1073         mtx_unlock(vlp2);
1074         mtx_lock(vlp1);
1075         mtx_lock(vlp2);
1076         MPASS(*vlpp == NULL);
1077         *vlpp = vlp1;
1078         return (false);
1079 }
1080
1081 static int __noinline
1082 cache_zap_locked_vnode(struct namecache *ncp, struct vnode *vp)
1083 {
1084         struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
1085         struct rwlock *blp;
1086         int error = 0;
1087
1088         MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
1089         cache_assert_vnode_locked(vp);
1090
1091         pvlp = VP2VNODELOCK(vp);
1092         if (ncp->nc_flag & NCF_NEGATIVE) {
1093                 cache_zap_negative_locked_vnode_kl(ncp, vp);
1094                 goto out;
1095         }
1096
1097         blp = NCP2BUCKETLOCK(ncp);
1098         vlp1 = VP2VNODELOCK(ncp->nc_dvp);
1099         vlp2 = VP2VNODELOCK(ncp->nc_vp);
1100         cache_sort_vnodes(&vlp1, &vlp2);
1101         if (vlp1 == pvlp) {
1102                 mtx_lock(vlp2);
1103                 to_unlock = vlp2;
1104         } else {
1105                 if (!mtx_trylock(vlp1)) {
1106                         error = EAGAIN;
1107                         goto out;
1108                 }
1109                 to_unlock = vlp1;
1110         }
1111         rw_wlock(blp);
1112         cache_zap_locked(ncp);
1113         rw_wunlock(blp);
1114         mtx_unlock(to_unlock);
1115 out:
1116         mtx_unlock(pvlp);
1117         return (error);
1118 }
1119
1120 /*
1121  * If trylocking failed we can get here. We know enough to take all needed locks
1122  * in the right order and re-lookup the entry.
1123  */
1124 static int
1125 cache_zap_unlocked_bucket(struct namecache *ncp, struct componentname *cnp,
1126     struct vnode *dvp, struct mtx *dvlp, struct mtx *vlp, uint32_t hash,
1127     struct rwlock *blp)
1128 {
1129         struct namecache *rncp;
1130
1131         cache_assert_bucket_locked(ncp, RA_UNLOCKED);
1132
1133         cache_sort_vnodes(&dvlp, &vlp);
1134         cache_lock_vnodes(dvlp, vlp);
1135         rw_wlock(blp);
1136         CK_SLIST_FOREACH(rncp, (NCHHASH(hash)), nc_hash) {
1137                 if (rncp == ncp && rncp->nc_dvp == dvp &&
1138                     rncp->nc_nlen == cnp->cn_namelen &&
1139                     !bcmp(rncp->nc_name, cnp->cn_nameptr, rncp->nc_nlen))
1140                         break;
1141         }
1142         if (rncp != NULL) {
1143                 cache_zap_locked(rncp);
1144                 rw_wunlock(blp);
1145                 cache_unlock_vnodes(dvlp, vlp);
1146                 counter_u64_add(zap_and_exit_bucket_relock_success, 1);
1147                 return (0);
1148         }
1149
1150         rw_wunlock(blp);
1151         cache_unlock_vnodes(dvlp, vlp);
1152         return (EAGAIN);
1153 }
1154
1155 static int __noinline
1156 cache_zap_wlocked_bucket(struct namecache *ncp, struct componentname *cnp,
1157     uint32_t hash, struct rwlock *blp)
1158 {
1159         struct mtx *dvlp, *vlp;
1160         struct vnode *dvp;
1161
1162         cache_assert_bucket_locked(ncp, RA_WLOCKED);
1163
1164         dvlp = VP2VNODELOCK(ncp->nc_dvp);
1165         vlp = NULL;
1166         if (!(ncp->nc_flag & NCF_NEGATIVE))
1167                 vlp = VP2VNODELOCK(ncp->nc_vp);
1168         if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1169                 cache_zap_locked(ncp);
1170                 rw_wunlock(blp);
1171                 cache_unlock_vnodes(dvlp, vlp);
1172                 return (0);
1173         }
1174
1175         dvp = ncp->nc_dvp;
1176         rw_wunlock(blp);
1177         return (cache_zap_unlocked_bucket(ncp, cnp, dvp, dvlp, vlp, hash, blp));
1178 }
1179
1180 static int __noinline
1181 cache_zap_rlocked_bucket(struct namecache *ncp, struct componentname *cnp,
1182     uint32_t hash, struct rwlock *blp)
1183 {
1184         struct mtx *dvlp, *vlp;
1185         struct vnode *dvp;
1186
1187         cache_assert_bucket_locked(ncp, RA_RLOCKED);
1188
1189         dvlp = VP2VNODELOCK(ncp->nc_dvp);
1190         vlp = NULL;
1191         if (!(ncp->nc_flag & NCF_NEGATIVE))
1192                 vlp = VP2VNODELOCK(ncp->nc_vp);
1193         if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1194                 rw_runlock(blp);
1195                 rw_wlock(blp);
1196                 cache_zap_locked(ncp);
1197                 rw_wunlock(blp);
1198                 cache_unlock_vnodes(dvlp, vlp);
1199                 return (0);
1200         }
1201
1202         dvp = ncp->nc_dvp;
1203         rw_runlock(blp);
1204         return (cache_zap_unlocked_bucket(ncp, cnp, dvp, dvlp, vlp, hash, blp));
1205 }
1206
1207 static int
1208 cache_zap_wlocked_bucket_kl(struct namecache *ncp, struct rwlock *blp,
1209     struct mtx **vlpp1, struct mtx **vlpp2)
1210 {
1211         struct mtx *dvlp, *vlp;
1212
1213         cache_assert_bucket_locked(ncp, RA_WLOCKED);
1214
1215         dvlp = VP2VNODELOCK(ncp->nc_dvp);
1216         vlp = NULL;
1217         if (!(ncp->nc_flag & NCF_NEGATIVE))
1218                 vlp = VP2VNODELOCK(ncp->nc_vp);
1219         cache_sort_vnodes(&dvlp, &vlp);
1220
1221         if (*vlpp1 == dvlp && *vlpp2 == vlp) {
1222                 cache_zap_locked(ncp);
1223                 cache_unlock_vnodes(dvlp, vlp);
1224                 *vlpp1 = NULL;
1225                 *vlpp2 = NULL;
1226                 return (0);
1227         }
1228
1229         if (*vlpp1 != NULL)
1230                 mtx_unlock(*vlpp1);
1231         if (*vlpp2 != NULL)
1232                 mtx_unlock(*vlpp2);
1233         *vlpp1 = NULL;
1234         *vlpp2 = NULL;
1235
1236         if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1237                 cache_zap_locked(ncp);
1238                 cache_unlock_vnodes(dvlp, vlp);
1239                 return (0);
1240         }
1241
1242         rw_wunlock(blp);
1243         *vlpp1 = dvlp;
1244         *vlpp2 = vlp;
1245         if (*vlpp1 != NULL)
1246                 mtx_lock(*vlpp1);
1247         mtx_lock(*vlpp2);
1248         rw_wlock(blp);
1249         return (EAGAIN);
1250 }
1251
1252 static void
1253 cache_lookup_unlock(struct rwlock *blp, struct mtx *vlp)
1254 {
1255
1256         if (blp != NULL) {
1257                 rw_runlock(blp);
1258         } else {
1259                 mtx_unlock(vlp);
1260         }
1261 }
1262
1263 static int __noinline
1264 cache_lookup_dot(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1265     struct timespec *tsp, int *ticksp)
1266 {
1267         int ltype;
1268
1269         *vpp = dvp;
1270         CTR2(KTR_VFS, "cache_lookup(%p, %s) found via .",
1271                         dvp, cnp->cn_nameptr);
1272         counter_u64_add(dothits, 1);
1273         SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", *vpp);
1274         if (tsp != NULL)
1275                 timespecclear(tsp);
1276         if (ticksp != NULL)
1277                 *ticksp = ticks;
1278         vrefact(*vpp);
1279         /*
1280          * When we lookup "." we still can be asked to lock it
1281          * differently...
1282          */
1283         ltype = cnp->cn_lkflags & LK_TYPE_MASK;
1284         if (ltype != VOP_ISLOCKED(*vpp)) {
1285                 if (ltype == LK_EXCLUSIVE) {
1286                         vn_lock(*vpp, LK_UPGRADE | LK_RETRY);
1287                         if (VN_IS_DOOMED((*vpp))) {
1288                                 /* forced unmount */
1289                                 vrele(*vpp);
1290                                 *vpp = NULL;
1291                                 return (ENOENT);
1292                         }
1293                 } else
1294                         vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY);
1295         }
1296         return (-1);
1297 }
1298
1299 static __noinline int
1300 cache_lookup_nomakeentry(struct vnode *dvp, struct vnode **vpp,
1301     struct componentname *cnp, struct timespec *tsp, int *ticksp)
1302 {
1303         struct namecache *ncp;
1304         struct rwlock *blp;
1305         struct mtx *dvlp, *dvlp2;
1306         uint32_t hash;
1307         int error;
1308
1309         if (cnp->cn_namelen == 2 &&
1310             cnp->cn_nameptr[0] == '.' && cnp->cn_nameptr[1] == '.') {
1311                 counter_u64_add(dotdothits, 1);
1312                 dvlp = VP2VNODELOCK(dvp);
1313                 dvlp2 = NULL;
1314                 mtx_lock(dvlp);
1315 retry_dotdot:
1316                 ncp = dvp->v_cache_dd;
1317                 if (ncp == NULL) {
1318                         SDT_PROBE3(vfs, namecache, lookup, miss, dvp,
1319                             "..", NULL);
1320                         mtx_unlock(dvlp);
1321                         if (dvlp2 != NULL)
1322                                 mtx_unlock(dvlp2);
1323                         return (0);
1324                 }
1325                 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1326                         if (ncp->nc_dvp != dvp)
1327                                 panic("dvp %p v_cache_dd %p\n", dvp, ncp);
1328                         if (!cache_zap_locked_vnode_kl2(ncp,
1329                             dvp, &dvlp2))
1330                                 goto retry_dotdot;
1331                         MPASS(dvp->v_cache_dd == NULL);
1332                         mtx_unlock(dvlp);
1333                         if (dvlp2 != NULL)
1334                                 mtx_unlock(dvlp2);
1335                         cache_free(ncp);
1336                 } else {
1337                         vn_seqc_write_begin(dvp);
1338                         dvp->v_cache_dd = NULL;
1339                         vn_seqc_write_end(dvp);
1340                         mtx_unlock(dvlp);
1341                         if (dvlp2 != NULL)
1342                                 mtx_unlock(dvlp2);
1343                 }
1344                 return (0);
1345         }
1346
1347         hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1348         blp = HASH2BUCKETLOCK(hash);
1349 retry:
1350         if (CK_SLIST_EMPTY(NCHHASH(hash)))
1351                 goto out_no_entry;
1352
1353         rw_wlock(blp);
1354
1355         CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1356                 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1357                     !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1358                         break;
1359         }
1360
1361         /* We failed to find an entry */
1362         if (ncp == NULL) {
1363                 rw_wunlock(blp);
1364                 goto out_no_entry;
1365         }
1366
1367         error = cache_zap_wlocked_bucket(ncp, cnp, hash, blp);
1368         if (__predict_false(error != 0)) {
1369                 zap_and_exit_bucket_fail++;
1370                 cache_maybe_yield();
1371                 goto retry;
1372         }
1373         counter_u64_add(numposzaps, 1);
1374         cache_free(ncp);
1375         return (0);
1376 out_no_entry:
1377         SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr, NULL);
1378         counter_u64_add(nummisszap, 1);
1379         return (0);
1380 }
1381
1382 /**
1383  * Lookup a name in the name cache
1384  *
1385  * # Arguments
1386  *
1387  * - dvp:       Parent directory in which to search.
1388  * - vpp:       Return argument.  Will contain desired vnode on cache hit.
1389  * - cnp:       Parameters of the name search.  The most interesting bits of
1390  *              the cn_flags field have the following meanings:
1391  *      - MAKEENTRY:    If clear, free an entry from the cache rather than look
1392  *                      it up.
1393  *      - ISDOTDOT:     Must be set if and only if cn_nameptr == ".."
1394  * - tsp:       Return storage for cache timestamp.  On a successful (positive
1395  *              or negative) lookup, tsp will be filled with any timespec that
1396  *              was stored when this cache entry was created.  However, it will
1397  *              be clear for "." entries.
1398  * - ticks:     Return storage for alternate cache timestamp.  On a successful
1399  *              (positive or negative) lookup, it will contain the ticks value
1400  *              that was current when the cache entry was created, unless cnp
1401  *              was ".".
1402  *
1403  * # Returns
1404  *
1405  * - -1:        A positive cache hit.  vpp will contain the desired vnode.
1406  * - ENOENT:    A negative cache hit, or dvp was recycled out from under us due
1407  *              to a forced unmount.  vpp will not be modified.  If the entry
1408  *              is a whiteout, then the ISWHITEOUT flag will be set in
1409  *              cnp->cn_flags.
1410  * - 0:         A cache miss.  vpp will not be modified.
1411  *
1412  * # Locking
1413  *
1414  * On a cache hit, vpp will be returned locked and ref'd.  If we're looking up
1415  * .., dvp is unlocked.  If we're looking up . an extra ref is taken, but the
1416  * lock is not recursively acquired.
1417  */
1418 int
1419 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1420     struct timespec *tsp, int *ticksp)
1421 {
1422         struct namecache_ts *ncp_ts;
1423         struct namecache *ncp;
1424         struct negstate *negstate;
1425         struct rwlock *blp;
1426         struct mtx *dvlp;
1427         uint32_t hash;
1428         enum vgetstate vs;
1429         int error, ltype;
1430         bool try_smr, doing_smr, whiteout;
1431
1432 #ifdef DEBUG_CACHE
1433         if (__predict_false(!doingcache)) {
1434                 cnp->cn_flags &= ~MAKEENTRY;
1435                 return (0);
1436         }
1437 #endif
1438
1439         if (__predict_false(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.'))
1440                 return (cache_lookup_dot(dvp, vpp, cnp, tsp, ticksp));
1441
1442         if ((cnp->cn_flags & MAKEENTRY) == 0)
1443                 return (cache_lookup_nomakeentry(dvp, vpp, cnp, tsp, ticksp));
1444
1445         try_smr = true;
1446         if (cnp->cn_nameiop == CREATE)
1447                 try_smr = false;
1448 retry:
1449         doing_smr = false;
1450         blp = NULL;
1451         dvlp = NULL;
1452         error = 0;
1453         if (cnp->cn_namelen == 2 &&
1454             cnp->cn_nameptr[0] == '.' && cnp->cn_nameptr[1] == '.') {
1455                 counter_u64_add(dotdothits, 1);
1456                 dvlp = VP2VNODELOCK(dvp);
1457                 mtx_lock(dvlp);
1458                 ncp = dvp->v_cache_dd;
1459                 if (ncp == NULL) {
1460                         SDT_PROBE3(vfs, namecache, lookup, miss, dvp,
1461                             "..", NULL);
1462                         mtx_unlock(dvlp);
1463                         return (0);
1464                 }
1465                 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1466                         if (ncp->nc_flag & NCF_NEGATIVE)
1467                                 *vpp = NULL;
1468                         else
1469                                 *vpp = ncp->nc_vp;
1470                 } else
1471                         *vpp = ncp->nc_dvp;
1472                 /* Return failure if negative entry was found. */
1473                 if (*vpp == NULL)
1474                         goto negative_success;
1475                 CTR3(KTR_VFS, "cache_lookup(%p, %s) found %p via ..",
1476                     dvp, cnp->cn_nameptr, *vpp);
1477                 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, "..",
1478                     *vpp);
1479                 cache_out_ts(ncp, tsp, ticksp);
1480                 if ((ncp->nc_flag & (NCF_ISDOTDOT | NCF_DTS)) ==
1481                     NCF_DTS && tsp != NULL) {
1482                         ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1483                         *tsp = ncp_ts->nc_dotdottime;
1484                 }
1485                 goto success;
1486         }
1487
1488         hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1489 retry_hashed:
1490         if (try_smr) {
1491                 vfs_smr_enter();
1492                 doing_smr = true;
1493                 try_smr = false;
1494         } else {
1495                 blp = HASH2BUCKETLOCK(hash);
1496                 rw_rlock(blp);
1497         }
1498
1499         CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1500                 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1501                     !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1502                         break;
1503         }
1504
1505         /* We failed to find an entry */
1506         if (__predict_false(ncp == NULL)) {
1507                 if (doing_smr)
1508                         vfs_smr_exit();
1509                 else
1510                         rw_runlock(blp);
1511                 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
1512                     NULL);
1513                 counter_u64_add(nummiss, 1);
1514                 return (0);
1515         }
1516
1517         if (ncp->nc_flag & NCF_NEGATIVE)
1518                 goto negative_success;
1519
1520         /* We found a "positive" match, return the vnode */
1521         counter_u64_add(numposhits, 1);
1522         *vpp = ncp->nc_vp;
1523         CTR4(KTR_VFS, "cache_lookup(%p, %s) found %p via ncp %p",
1524             dvp, cnp->cn_nameptr, *vpp, ncp);
1525         SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name,
1526             *vpp);
1527         cache_out_ts(ncp, tsp, ticksp);
1528 success:
1529         /*
1530          * On success we return a locked and ref'd vnode as per the lookup
1531          * protocol.
1532          */
1533         MPASS(dvp != *vpp);
1534         ltype = 0;      /* silence gcc warning */
1535         if (cnp->cn_flags & ISDOTDOT) {
1536                 ltype = VOP_ISLOCKED(dvp);
1537                 VOP_UNLOCK(dvp);
1538         }
1539         if (doing_smr) {
1540                 if (!cache_ncp_canuse(ncp)) {
1541                         vfs_smr_exit();
1542                         *vpp = NULL;
1543                         goto retry;
1544                 }
1545                 vs = vget_prep_smr(*vpp);
1546                 vfs_smr_exit();
1547                 if (__predict_false(vs == VGET_NONE)) {
1548                         *vpp = NULL;
1549                         goto retry;
1550                 }
1551         } else {
1552                 vs = vget_prep(*vpp);
1553                 cache_lookup_unlock(blp, dvlp);
1554         }
1555         error = vget_finish(*vpp, cnp->cn_lkflags, vs);
1556         if (cnp->cn_flags & ISDOTDOT) {
1557                 vn_lock(dvp, ltype | LK_RETRY);
1558                 if (VN_IS_DOOMED(dvp)) {
1559                         if (error == 0)
1560                                 vput(*vpp);
1561                         *vpp = NULL;
1562                         return (ENOENT);
1563                 }
1564         }
1565         if (error) {
1566                 *vpp = NULL;
1567                 goto retry;
1568         }
1569         if ((cnp->cn_flags & ISLASTCN) &&
1570             (cnp->cn_lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE) {
1571                 ASSERT_VOP_ELOCKED(*vpp, "cache_lookup");
1572         }
1573         return (-1);
1574
1575 negative_success:
1576         /* We found a negative match, and want to create it, so purge */
1577         if (cnp->cn_nameiop == CREATE) {
1578                 MPASS(!doing_smr);
1579                 counter_u64_add(numnegzaps, 1);
1580                 goto zap_and_exit;
1581         }
1582
1583         SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp, ncp->nc_name);
1584         cache_out_ts(ncp, tsp, ticksp);
1585         counter_u64_add(numneghits, 1);
1586         whiteout = (ncp->nc_flag & NCF_WHITE);
1587
1588         if (doing_smr) {
1589                 /*
1590                  * We need to take locks to promote an entry.
1591                  */
1592                 negstate = NCP2NEGSTATE(ncp);
1593                 if ((negstate->neg_flag & NEG_HOT) == 0 ||
1594                     !cache_ncp_canuse(ncp)) {
1595                         vfs_smr_exit();
1596                         doing_smr = false;
1597                         goto retry_hashed;
1598                 }
1599                 vfs_smr_exit();
1600         } else {
1601                 cache_negative_hit(ncp);
1602                 cache_lookup_unlock(blp, dvlp);
1603         }
1604         if (whiteout)
1605                 cnp->cn_flags |= ISWHITEOUT;
1606         return (ENOENT);
1607
1608 zap_and_exit:
1609         MPASS(!doing_smr);
1610         if (blp != NULL)
1611                 error = cache_zap_rlocked_bucket(ncp, cnp, hash, blp);
1612         else
1613                 error = cache_zap_locked_vnode(ncp, dvp);
1614         if (__predict_false(error != 0)) {
1615                 zap_and_exit_bucket_fail2++;
1616                 cache_maybe_yield();
1617                 goto retry;
1618         }
1619         cache_free(ncp);
1620         return (0);
1621 }
1622
1623 struct celockstate {
1624         struct mtx *vlp[3];
1625         struct rwlock *blp[2];
1626 };
1627 CTASSERT((nitems(((struct celockstate *)0)->vlp) == 3));
1628 CTASSERT((nitems(((struct celockstate *)0)->blp) == 2));
1629
1630 static inline void
1631 cache_celockstate_init(struct celockstate *cel)
1632 {
1633
1634         bzero(cel, sizeof(*cel));
1635 }
1636
1637 static void
1638 cache_lock_vnodes_cel(struct celockstate *cel, struct vnode *vp,
1639     struct vnode *dvp)
1640 {
1641         struct mtx *vlp1, *vlp2;
1642
1643         MPASS(cel->vlp[0] == NULL);
1644         MPASS(cel->vlp[1] == NULL);
1645         MPASS(cel->vlp[2] == NULL);
1646
1647         MPASS(vp != NULL || dvp != NULL);
1648
1649         vlp1 = VP2VNODELOCK(vp);
1650         vlp2 = VP2VNODELOCK(dvp);
1651         cache_sort_vnodes(&vlp1, &vlp2);
1652
1653         if (vlp1 != NULL) {
1654                 mtx_lock(vlp1);
1655                 cel->vlp[0] = vlp1;
1656         }
1657         mtx_lock(vlp2);
1658         cel->vlp[1] = vlp2;
1659 }
1660
1661 static void
1662 cache_unlock_vnodes_cel(struct celockstate *cel)
1663 {
1664
1665         MPASS(cel->vlp[0] != NULL || cel->vlp[1] != NULL);
1666
1667         if (cel->vlp[0] != NULL)
1668                 mtx_unlock(cel->vlp[0]);
1669         if (cel->vlp[1] != NULL)
1670                 mtx_unlock(cel->vlp[1]);
1671         if (cel->vlp[2] != NULL)
1672                 mtx_unlock(cel->vlp[2]);
1673 }
1674
1675 static bool
1676 cache_lock_vnodes_cel_3(struct celockstate *cel, struct vnode *vp)
1677 {
1678         struct mtx *vlp;
1679         bool ret;
1680
1681         cache_assert_vlp_locked(cel->vlp[0]);
1682         cache_assert_vlp_locked(cel->vlp[1]);
1683         MPASS(cel->vlp[2] == NULL);
1684
1685         MPASS(vp != NULL);
1686         vlp = VP2VNODELOCK(vp);
1687
1688         ret = true;
1689         if (vlp >= cel->vlp[1]) {
1690                 mtx_lock(vlp);
1691         } else {
1692                 if (mtx_trylock(vlp))
1693                         goto out;
1694                 cache_lock_vnodes_cel_3_failures++;
1695                 cache_unlock_vnodes_cel(cel);
1696                 if (vlp < cel->vlp[0]) {
1697                         mtx_lock(vlp);
1698                         mtx_lock(cel->vlp[0]);
1699                         mtx_lock(cel->vlp[1]);
1700                 } else {
1701                         if (cel->vlp[0] != NULL)
1702                                 mtx_lock(cel->vlp[0]);
1703                         mtx_lock(vlp);
1704                         mtx_lock(cel->vlp[1]);
1705                 }
1706                 ret = false;
1707         }
1708 out:
1709         cel->vlp[2] = vlp;
1710         return (ret);
1711 }
1712
1713 static void
1714 cache_lock_buckets_cel(struct celockstate *cel, struct rwlock *blp1,
1715     struct rwlock *blp2)
1716 {
1717
1718         MPASS(cel->blp[0] == NULL);
1719         MPASS(cel->blp[1] == NULL);
1720
1721         cache_sort_vnodes(&blp1, &blp2);
1722
1723         if (blp1 != NULL) {
1724                 rw_wlock(blp1);
1725                 cel->blp[0] = blp1;
1726         }
1727         rw_wlock(blp2);
1728         cel->blp[1] = blp2;
1729 }
1730
1731 static void
1732 cache_unlock_buckets_cel(struct celockstate *cel)
1733 {
1734
1735         if (cel->blp[0] != NULL)
1736                 rw_wunlock(cel->blp[0]);
1737         rw_wunlock(cel->blp[1]);
1738 }
1739
1740 /*
1741  * Lock part of the cache affected by the insertion.
1742  *
1743  * This means vnodelocks for dvp, vp and the relevant bucketlock.
1744  * However, insertion can result in removal of an old entry. In this
1745  * case we have an additional vnode and bucketlock pair to lock. If the
1746  * entry is negative, ncelock is locked instead of the vnode.
1747  *
1748  * That is, in the worst case we have to lock 3 vnodes and 2 bucketlocks, while
1749  * preserving the locking order (smaller address first).
1750  */
1751 static void
1752 cache_enter_lock(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1753     uint32_t hash)
1754 {
1755         struct namecache *ncp;
1756         struct rwlock *blps[2];
1757
1758         blps[0] = HASH2BUCKETLOCK(hash);
1759         for (;;) {
1760                 blps[1] = NULL;
1761                 cache_lock_vnodes_cel(cel, dvp, vp);
1762                 if (vp == NULL || vp->v_type != VDIR)
1763                         break;
1764                 ncp = vp->v_cache_dd;
1765                 if (ncp == NULL)
1766                         break;
1767                 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1768                         break;
1769                 MPASS(ncp->nc_dvp == vp);
1770                 blps[1] = NCP2BUCKETLOCK(ncp);
1771                 if (ncp->nc_flag & NCF_NEGATIVE)
1772                         break;
1773                 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1774                         break;
1775                 /*
1776                  * All vnodes got re-locked. Re-validate the state and if
1777                  * nothing changed we are done. Otherwise restart.
1778                  */
1779                 if (ncp == vp->v_cache_dd &&
1780                     (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1781                     blps[1] == NCP2BUCKETLOCK(ncp) &&
1782                     VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1783                         break;
1784                 cache_unlock_vnodes_cel(cel);
1785                 cel->vlp[0] = NULL;
1786                 cel->vlp[1] = NULL;
1787                 cel->vlp[2] = NULL;
1788         }
1789         cache_lock_buckets_cel(cel, blps[0], blps[1]);
1790 }
1791
1792 static void
1793 cache_enter_lock_dd(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1794     uint32_t hash)
1795 {
1796         struct namecache *ncp;
1797         struct rwlock *blps[2];
1798
1799         blps[0] = HASH2BUCKETLOCK(hash);
1800         for (;;) {
1801                 blps[1] = NULL;
1802                 cache_lock_vnodes_cel(cel, dvp, vp);
1803                 ncp = dvp->v_cache_dd;
1804                 if (ncp == NULL)
1805                         break;
1806                 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1807                         break;
1808                 MPASS(ncp->nc_dvp == dvp);
1809                 blps[1] = NCP2BUCKETLOCK(ncp);
1810                 if (ncp->nc_flag & NCF_NEGATIVE)
1811                         break;
1812                 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1813                         break;
1814                 if (ncp == dvp->v_cache_dd &&
1815                     (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1816                     blps[1] == NCP2BUCKETLOCK(ncp) &&
1817                     VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1818                         break;
1819                 cache_unlock_vnodes_cel(cel);
1820                 cel->vlp[0] = NULL;
1821                 cel->vlp[1] = NULL;
1822                 cel->vlp[2] = NULL;
1823         }
1824         cache_lock_buckets_cel(cel, blps[0], blps[1]);
1825 }
1826
1827 static void
1828 cache_enter_unlock(struct celockstate *cel)
1829 {
1830
1831         cache_unlock_buckets_cel(cel);
1832         cache_unlock_vnodes_cel(cel);
1833 }
1834
1835 static void __noinline
1836 cache_enter_dotdot_prep(struct vnode *dvp, struct vnode *vp,
1837     struct componentname *cnp)
1838 {
1839         struct celockstate cel;
1840         struct namecache *ncp;
1841         uint32_t hash;
1842         int len;
1843
1844         if (dvp->v_cache_dd == NULL)
1845                 return;
1846         len = cnp->cn_namelen;
1847         cache_celockstate_init(&cel);
1848         hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1849         cache_enter_lock_dd(&cel, dvp, vp, hash);
1850         vn_seqc_write_begin(dvp);
1851         ncp = dvp->v_cache_dd;
1852         if (ncp != NULL && (ncp->nc_flag & NCF_ISDOTDOT)) {
1853                 KASSERT(ncp->nc_dvp == dvp, ("wrong isdotdot parent"));
1854                 cache_zap_locked(ncp);
1855         } else {
1856                 ncp = NULL;
1857         }
1858         dvp->v_cache_dd = NULL;
1859         vn_seqc_write_end(dvp);
1860         cache_enter_unlock(&cel);
1861         cache_free(ncp);
1862 }
1863
1864 /*
1865  * Add an entry to the cache.
1866  */
1867 void
1868 cache_enter_time(struct vnode *dvp, struct vnode *vp, struct componentname *cnp,
1869     struct timespec *tsp, struct timespec *dtsp)
1870 {
1871         struct celockstate cel;
1872         struct namecache *ncp, *n2, *ndd;
1873         struct namecache_ts *ncp_ts, *n2_ts;
1874         struct nchashhead *ncpp;
1875         uint32_t hash;
1876         int flag;
1877         int len;
1878         u_long lnumcache;
1879
1880         CTR3(KTR_VFS, "cache_enter(%p, %p, %s)", dvp, vp, cnp->cn_nameptr);
1881         VNASSERT(vp == NULL || !VN_IS_DOOMED(vp), vp,
1882             ("cache_enter: Adding a doomed vnode"));
1883         VNASSERT(dvp == NULL || !VN_IS_DOOMED(dvp), dvp,
1884             ("cache_enter: Doomed vnode used as src"));
1885
1886 #ifdef DEBUG_CACHE
1887         if (__predict_false(!doingcache))
1888                 return;
1889 #endif
1890
1891         flag = 0;
1892         if (__predict_false(cnp->cn_nameptr[0] == '.')) {
1893                 if (cnp->cn_namelen == 1)
1894                         return;
1895                 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
1896                         cache_enter_dotdot_prep(dvp, vp, cnp);
1897                         flag = NCF_ISDOTDOT;
1898                 }
1899         }
1900
1901         /*
1902          * Avoid blowout in namecache entries.
1903          */
1904         lnumcache = atomic_fetchadd_long(&numcache, 1) + 1;
1905         if (__predict_false(lnumcache >= ncsize)) {
1906                 atomic_add_long(&numcache, -1);
1907                 counter_u64_add(numdrops, 1);
1908                 return;
1909         }
1910
1911         cache_celockstate_init(&cel);
1912         ndd = NULL;
1913         ncp_ts = NULL;
1914
1915         /*
1916          * Calculate the hash key and setup as much of the new
1917          * namecache entry as possible before acquiring the lock.
1918          */
1919         ncp = cache_alloc(cnp->cn_namelen, tsp != NULL);
1920         ncp->nc_flag = flag | NCF_WIP;
1921         ncp->nc_vp = vp;
1922         if (vp == NULL)
1923                 cache_negative_init(ncp);
1924         ncp->nc_dvp = dvp;
1925         if (tsp != NULL) {
1926                 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1927                 ncp_ts->nc_time = *tsp;
1928                 ncp_ts->nc_ticks = ticks;
1929                 ncp_ts->nc_nc.nc_flag |= NCF_TS;
1930                 if (dtsp != NULL) {
1931                         ncp_ts->nc_dotdottime = *dtsp;
1932                         ncp_ts->nc_nc.nc_flag |= NCF_DTS;
1933                 }
1934         }
1935         len = ncp->nc_nlen = cnp->cn_namelen;
1936         hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1937         memcpy(ncp->nc_name, cnp->cn_nameptr, len);
1938         ncp->nc_name[len] = '\0';
1939         cache_enter_lock(&cel, dvp, vp, hash);
1940
1941         /*
1942          * See if this vnode or negative entry is already in the cache
1943          * with this name.  This can happen with concurrent lookups of
1944          * the same path name.
1945          */
1946         ncpp = NCHHASH(hash);
1947         CK_SLIST_FOREACH(n2, ncpp, nc_hash) {
1948                 if (n2->nc_dvp == dvp &&
1949                     n2->nc_nlen == cnp->cn_namelen &&
1950                     !bcmp(n2->nc_name, cnp->cn_nameptr, n2->nc_nlen)) {
1951                         if (tsp != NULL) {
1952                                 KASSERT((n2->nc_flag & NCF_TS) != 0,
1953                                     ("no NCF_TS"));
1954                                 n2_ts = __containerof(n2, struct namecache_ts, nc_nc);
1955                                 n2_ts->nc_time = ncp_ts->nc_time;
1956                                 n2_ts->nc_ticks = ncp_ts->nc_ticks;
1957                                 if (dtsp != NULL) {
1958                                         n2_ts->nc_dotdottime = ncp_ts->nc_dotdottime;
1959                                         n2_ts->nc_nc.nc_flag |= NCF_DTS;
1960                                 }
1961                         }
1962                         goto out_unlock_free;
1963                 }
1964         }
1965
1966         if (flag == NCF_ISDOTDOT) {
1967                 /*
1968                  * See if we are trying to add .. entry, but some other lookup
1969                  * has populated v_cache_dd pointer already.
1970                  */
1971                 if (dvp->v_cache_dd != NULL)
1972                         goto out_unlock_free;
1973                 KASSERT(vp == NULL || vp->v_type == VDIR,
1974                     ("wrong vnode type %p", vp));
1975                 vn_seqc_write_begin(dvp);
1976                 dvp->v_cache_dd = ncp;
1977                 vn_seqc_write_end(dvp);
1978         }
1979
1980         if (vp != NULL) {
1981                 if (vp->v_type == VDIR) {
1982                         if (flag != NCF_ISDOTDOT) {
1983                                 /*
1984                                  * For this case, the cache entry maps both the
1985                                  * directory name in it and the name ".." for the
1986                                  * directory's parent.
1987                                  */
1988                                 vn_seqc_write_begin(vp);
1989                                 if ((ndd = vp->v_cache_dd) != NULL) {
1990                                         if ((ndd->nc_flag & NCF_ISDOTDOT) != 0)
1991                                                 cache_zap_locked(ndd);
1992                                         else
1993                                                 ndd = NULL;
1994                                 }
1995                                 vp->v_cache_dd = ncp;
1996                                 vn_seqc_write_end(vp);
1997                         }
1998                 } else {
1999                         if (vp->v_cache_dd != NULL) {
2000                                 vn_seqc_write_begin(vp);
2001                                 vp->v_cache_dd = NULL;
2002                                 vn_seqc_write_end(vp);
2003                         }
2004                 }
2005         }
2006
2007         if (flag != NCF_ISDOTDOT) {
2008                 if (LIST_EMPTY(&dvp->v_cache_src)) {
2009                         vhold(dvp);
2010                         counter_u64_add(numcachehv, 1);
2011                 }
2012                 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
2013         }
2014
2015         /*
2016          * If the entry is "negative", we place it into the
2017          * "negative" cache queue, otherwise, we place it into the
2018          * destination vnode's cache entries queue.
2019          */
2020         if (vp != NULL) {
2021                 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
2022                 SDT_PROBE3(vfs, namecache, enter, done, dvp, ncp->nc_name,
2023                     vp);
2024         } else {
2025                 if (cnp->cn_flags & ISWHITEOUT)
2026                         ncp->nc_flag |= NCF_WHITE;
2027                 cache_negative_insert(ncp);
2028                 SDT_PROBE2(vfs, namecache, enter_negative, done, dvp,
2029                     ncp->nc_name);
2030         }
2031
2032         /*
2033          * Insert the new namecache entry into the appropriate chain
2034          * within the cache entries table.
2035          */
2036         CK_SLIST_INSERT_HEAD(ncpp, ncp, nc_hash);
2037
2038         atomic_thread_fence_rel();
2039         /*
2040          * Mark the entry as fully constructed.
2041          * It is immutable past this point until its removal.
2042          */
2043         atomic_store_char(&ncp->nc_flag, ncp->nc_flag & ~NCF_WIP);
2044
2045         cache_enter_unlock(&cel);
2046         if (numneg * ncnegfactor > lnumcache)
2047                 cache_negative_zap_one();
2048         cache_free(ndd);
2049         return;
2050 out_unlock_free:
2051         cache_enter_unlock(&cel);
2052         atomic_add_long(&numcache, -1);
2053         cache_free(ncp);
2054         return;
2055 }
2056
2057 static u_int
2058 cache_roundup_2(u_int val)
2059 {
2060         u_int res;
2061
2062         for (res = 1; res <= val; res <<= 1)
2063                 continue;
2064
2065         return (res);
2066 }
2067
2068 static struct nchashhead *
2069 nchinittbl(u_long elements, u_long *hashmask)
2070 {
2071         struct nchashhead *hashtbl;
2072         u_long hashsize, i;
2073
2074         hashsize = cache_roundup_2(elements) / 2;
2075
2076         hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), M_VFSCACHE, M_WAITOK);
2077         for (i = 0; i < hashsize; i++)
2078                 CK_SLIST_INIT(&hashtbl[i]);
2079         *hashmask = hashsize - 1;
2080         return (hashtbl);
2081 }
2082
2083 static void
2084 ncfreetbl(struct nchashhead *hashtbl)
2085 {
2086
2087         free(hashtbl, M_VFSCACHE);
2088 }
2089
2090 /*
2091  * Name cache initialization, from vfs_init() when we are booting
2092  */
2093 static void
2094 nchinit(void *dummy __unused)
2095 {
2096         u_int i;
2097
2098         cache_zone_small = uma_zcreate("S VFS Cache",
2099             sizeof(struct namecache) + CACHE_PATH_CUTOFF + 1,
2100             NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT,
2101             UMA_ZONE_ZINIT);
2102         cache_zone_small_ts = uma_zcreate("STS VFS Cache",
2103             sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1,
2104             NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT,
2105             UMA_ZONE_ZINIT);
2106         cache_zone_large = uma_zcreate("L VFS Cache",
2107             sizeof(struct namecache) + NAME_MAX + 1,
2108             NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT,
2109             UMA_ZONE_ZINIT);
2110         cache_zone_large_ts = uma_zcreate("LTS VFS Cache",
2111             sizeof(struct namecache_ts) + NAME_MAX + 1,
2112             NULL, NULL, NULL, NULL, CACHE_ZONE_ALIGNMENT,
2113             UMA_ZONE_ZINIT);
2114
2115         VFS_SMR_ZONE_SET(cache_zone_small);
2116         VFS_SMR_ZONE_SET(cache_zone_small_ts);
2117         VFS_SMR_ZONE_SET(cache_zone_large);
2118         VFS_SMR_ZONE_SET(cache_zone_large_ts);
2119
2120         ncsize = desiredvnodes * ncsizefactor;
2121         nchashtbl = nchinittbl(desiredvnodes * 2, &nchash);
2122         ncbuckethash = cache_roundup_2(mp_ncpus * mp_ncpus) - 1;
2123         if (ncbuckethash < 7) /* arbitrarily chosen to avoid having one lock */
2124                 ncbuckethash = 7;
2125         if (ncbuckethash > nchash)
2126                 ncbuckethash = nchash;
2127         bucketlocks = malloc(sizeof(*bucketlocks) * numbucketlocks, M_VFSCACHE,
2128             M_WAITOK | M_ZERO);
2129         for (i = 0; i < numbucketlocks; i++)
2130                 rw_init_flags(&bucketlocks[i], "ncbuc", RW_DUPOK | RW_RECURSE);
2131         ncvnodehash = ncbuckethash;
2132         vnodelocks = malloc(sizeof(*vnodelocks) * numvnodelocks, M_VFSCACHE,
2133             M_WAITOK | M_ZERO);
2134         for (i = 0; i < numvnodelocks; i++)
2135                 mtx_init(&vnodelocks[i], "ncvn", NULL, MTX_DUPOK | MTX_RECURSE);
2136         ncpurgeminvnodes = numbucketlocks * 2;
2137
2138         neglists = malloc(sizeof(*neglists) * numneglists, M_VFSCACHE,
2139             M_WAITOK | M_ZERO);
2140         for (i = 0; i < numneglists; i++) {
2141                 mtx_init(&neglists[i].nl_lock, "ncnegl", NULL, MTX_DEF);
2142                 TAILQ_INIT(&neglists[i].nl_list);
2143         }
2144         mtx_init(&ncneg_hot.nl_lock, "ncneglh", NULL, MTX_DEF);
2145         TAILQ_INIT(&ncneg_hot.nl_list);
2146
2147         mtx_init(&ncneg_shrink_lock, "ncnegs", NULL, MTX_DEF);
2148 }
2149 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL);
2150
2151 void
2152 cache_vnode_init(struct vnode *vp)
2153 {
2154
2155         LIST_INIT(&vp->v_cache_src);
2156         TAILQ_INIT(&vp->v_cache_dst);
2157         vp->v_cache_dd = NULL;
2158         cache_prehash(vp);
2159 }
2160
2161 void
2162 cache_changesize(u_long newmaxvnodes)
2163 {
2164         struct nchashhead *new_nchashtbl, *old_nchashtbl;
2165         u_long new_nchash, old_nchash;
2166         struct namecache *ncp;
2167         uint32_t hash;
2168         u_long newncsize;
2169         int i;
2170
2171         newncsize = newmaxvnodes * ncsizefactor;
2172         newmaxvnodes = cache_roundup_2(newmaxvnodes * 2);
2173         if (newmaxvnodes < numbucketlocks)
2174                 newmaxvnodes = numbucketlocks;
2175
2176         new_nchashtbl = nchinittbl(newmaxvnodes, &new_nchash);
2177         /* If same hash table size, nothing to do */
2178         if (nchash == new_nchash) {
2179                 ncfreetbl(new_nchashtbl);
2180                 return;
2181         }
2182         /*
2183          * Move everything from the old hash table to the new table.
2184          * None of the namecache entries in the table can be removed
2185          * because to do so, they have to be removed from the hash table.
2186          */
2187         cache_lock_all_vnodes();
2188         cache_lock_all_buckets();
2189         old_nchashtbl = nchashtbl;
2190         old_nchash = nchash;
2191         nchashtbl = new_nchashtbl;
2192         nchash = new_nchash;
2193         for (i = 0; i <= old_nchash; i++) {
2194                 while ((ncp = CK_SLIST_FIRST(&old_nchashtbl[i])) != NULL) {
2195                         hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen,
2196                             ncp->nc_dvp);
2197                         CK_SLIST_REMOVE(&old_nchashtbl[i], ncp, namecache, nc_hash);
2198                         CK_SLIST_INSERT_HEAD(NCHHASH(hash), ncp, nc_hash);
2199                 }
2200         }
2201         ncsize = newncsize;
2202         cache_unlock_all_buckets();
2203         cache_unlock_all_vnodes();
2204         ncfreetbl(old_nchashtbl);
2205 }
2206
2207 /*
2208  * Invalidate all entries from and to a particular vnode.
2209  */
2210 static void
2211 cache_purge_impl(struct vnode *vp)
2212 {
2213         TAILQ_HEAD(, namecache) ncps;
2214         struct namecache *ncp, *nnp;
2215         struct mtx *vlp, *vlp2;
2216
2217         TAILQ_INIT(&ncps);
2218         vlp = VP2VNODELOCK(vp);
2219         vlp2 = NULL;
2220         mtx_assert(vlp, MA_OWNED);
2221 retry:
2222         while (!LIST_EMPTY(&vp->v_cache_src)) {
2223                 ncp = LIST_FIRST(&vp->v_cache_src);
2224                 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2225                         goto retry;
2226                 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2227         }
2228         while (!TAILQ_EMPTY(&vp->v_cache_dst)) {
2229                 ncp = TAILQ_FIRST(&vp->v_cache_dst);
2230                 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2231                         goto retry;
2232                 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2233         }
2234         ncp = vp->v_cache_dd;
2235         if (ncp != NULL) {
2236                 KASSERT(ncp->nc_flag & NCF_ISDOTDOT,
2237                    ("lost dotdot link"));
2238                 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2239                         goto retry;
2240                 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2241         }
2242         KASSERT(vp->v_cache_dd == NULL, ("incomplete purge"));
2243         mtx_unlock(vlp);
2244         if (vlp2 != NULL)
2245                 mtx_unlock(vlp2);
2246         TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2247                 cache_free(ncp);
2248         }
2249 }
2250
2251 void
2252 cache_purge(struct vnode *vp)
2253 {
2254         struct mtx *vlp;
2255
2256         SDT_PROBE1(vfs, namecache, purge, done, vp);
2257         if (LIST_EMPTY(&vp->v_cache_src) && TAILQ_EMPTY(&vp->v_cache_dst) &&
2258             vp->v_cache_dd == NULL)
2259                 return;
2260         vlp = VP2VNODELOCK(vp);
2261         mtx_lock(vlp);
2262         cache_purge_impl(vp);
2263 }
2264
2265 /*
2266  * Only to be used by vgone.
2267  */
2268 void
2269 cache_purge_vgone(struct vnode *vp)
2270 {
2271         struct mtx *vlp;
2272
2273         VNPASS(VN_IS_DOOMED(vp), vp);
2274         vlp = VP2VNODELOCK(vp);
2275         if (!(LIST_EMPTY(&vp->v_cache_src) && TAILQ_EMPTY(&vp->v_cache_dst) &&
2276             vp->v_cache_dd == NULL)) {
2277                 mtx_lock(vlp);
2278                 cache_purge_impl(vp);
2279                 mtx_assert(vlp, MA_NOTOWNED);
2280                 return;
2281         }
2282
2283         /*
2284          * All the NULL pointer state we found above may be transient.
2285          * Serialize against a possible thread doing cache_purge.
2286          */
2287         mtx_wait_unlocked(vlp);
2288         if (!(LIST_EMPTY(&vp->v_cache_src) && TAILQ_EMPTY(&vp->v_cache_dst) &&
2289             vp->v_cache_dd == NULL)) {
2290                 mtx_lock(vlp);
2291                 cache_purge_impl(vp);
2292                 mtx_assert(vlp, MA_NOTOWNED);
2293                 return;
2294         }
2295         return;
2296 }
2297
2298 /*
2299  * Invalidate all negative entries for a particular directory vnode.
2300  */
2301 void
2302 cache_purge_negative(struct vnode *vp)
2303 {
2304         TAILQ_HEAD(, namecache) ncps;
2305         struct namecache *ncp, *nnp;
2306         struct mtx *vlp;
2307
2308         CTR1(KTR_VFS, "cache_purge_negative(%p)", vp);
2309         SDT_PROBE1(vfs, namecache, purge_negative, done, vp);
2310         if (LIST_EMPTY(&vp->v_cache_src))
2311                 return;
2312         TAILQ_INIT(&ncps);
2313         vlp = VP2VNODELOCK(vp);
2314         mtx_lock(vlp);
2315         LIST_FOREACH_SAFE(ncp, &vp->v_cache_src, nc_src, nnp) {
2316                 if (!(ncp->nc_flag & NCF_NEGATIVE))
2317                         continue;
2318                 cache_zap_negative_locked_vnode_kl(ncp, vp);
2319                 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2320         }
2321         mtx_unlock(vlp);
2322         TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2323                 cache_free(ncp);
2324         }
2325 }
2326
2327 /*
2328  * Flush all entries referencing a particular filesystem.
2329  */
2330 void
2331 cache_purgevfs(struct mount *mp, bool force)
2332 {
2333         TAILQ_HEAD(, namecache) ncps;
2334         struct mtx *vlp1, *vlp2;
2335         struct rwlock *blp;
2336         struct nchashhead *bucket;
2337         struct namecache *ncp, *nnp;
2338         u_long i, j, n_nchash;
2339         int error;
2340
2341         /* Scan hash tables for applicable entries */
2342         SDT_PROBE1(vfs, namecache, purgevfs, done, mp);
2343         if (!force && mp->mnt_nvnodelistsize <= ncpurgeminvnodes)
2344                 return;
2345         TAILQ_INIT(&ncps);
2346         n_nchash = nchash + 1;
2347         vlp1 = vlp2 = NULL;
2348         for (i = 0; i < numbucketlocks; i++) {
2349                 blp = (struct rwlock *)&bucketlocks[i];
2350                 rw_wlock(blp);
2351                 for (j = i; j < n_nchash; j += numbucketlocks) {
2352 retry:
2353                         bucket = &nchashtbl[j];
2354                         CK_SLIST_FOREACH_SAFE(ncp, bucket, nc_hash, nnp) {
2355                                 cache_assert_bucket_locked(ncp, RA_WLOCKED);
2356                                 if (ncp->nc_dvp->v_mount != mp)
2357                                         continue;
2358                                 error = cache_zap_wlocked_bucket_kl(ncp, blp,
2359                                     &vlp1, &vlp2);
2360                                 if (error != 0)
2361                                         goto retry;
2362                                 TAILQ_INSERT_HEAD(&ncps, ncp, nc_dst);
2363                         }
2364                 }
2365                 rw_wunlock(blp);
2366                 if (vlp1 == NULL && vlp2 == NULL)
2367                         cache_maybe_yield();
2368         }
2369         if (vlp1 != NULL)
2370                 mtx_unlock(vlp1);
2371         if (vlp2 != NULL)
2372                 mtx_unlock(vlp2);
2373
2374         TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2375                 cache_free(ncp);
2376         }
2377 }
2378
2379 /*
2380  * Perform canonical checks and cache lookup and pass on to filesystem
2381  * through the vop_cachedlookup only if needed.
2382  */
2383
2384 int
2385 vfs_cache_lookup(struct vop_lookup_args *ap)
2386 {
2387         struct vnode *dvp;
2388         int error;
2389         struct vnode **vpp = ap->a_vpp;
2390         struct componentname *cnp = ap->a_cnp;
2391         int flags = cnp->cn_flags;
2392
2393         *vpp = NULL;
2394         dvp = ap->a_dvp;
2395
2396         if (dvp->v_type != VDIR)
2397                 return (ENOTDIR);
2398
2399         if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
2400             (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
2401                 return (EROFS);
2402
2403         error = vn_dir_check_exec(dvp, cnp);
2404         if (error != 0)
2405                 return (error);
2406
2407         error = cache_lookup(dvp, vpp, cnp, NULL, NULL);
2408         if (error == 0)
2409                 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
2410         if (error == -1)
2411                 return (0);
2412         return (error);
2413 }
2414
2415 /* Implementation of the getcwd syscall. */
2416 int
2417 sys___getcwd(struct thread *td, struct __getcwd_args *uap)
2418 {
2419         char *buf, *retbuf;
2420         size_t buflen;
2421         int error;
2422
2423         buflen = uap->buflen;
2424         if (__predict_false(buflen < 2))
2425                 return (EINVAL);
2426         if (buflen > MAXPATHLEN)
2427                 buflen = MAXPATHLEN;
2428
2429         buf = malloc(buflen, M_TEMP, M_WAITOK);
2430         error = vn_getcwd(td, buf, &retbuf, &buflen);
2431         if (error == 0)
2432                 error = copyout(retbuf, uap->buf, buflen);
2433         free(buf, M_TEMP);
2434         return (error);
2435 }
2436
2437 int
2438 vn_getcwd(struct thread *td, char *buf, char **retbuf, size_t *buflen)
2439 {
2440         struct pwd *pwd;
2441         int error;
2442
2443         pwd = pwd_hold(td);
2444         error = vn_fullpath_any(td, pwd->pwd_cdir, pwd->pwd_rdir, buf, retbuf, buflen);
2445         pwd_drop(pwd);
2446
2447 #ifdef KTRACE
2448         if (KTRPOINT(curthread, KTR_NAMEI) && error == 0)
2449                 ktrnamei(*retbuf);
2450 #endif
2451         return (error);
2452 }
2453
2454 static int
2455 kern___realpathat(struct thread *td, int fd, const char *path, char *buf,
2456     size_t size, int flags, enum uio_seg pathseg)
2457 {
2458         struct nameidata nd;
2459         char *retbuf, *freebuf;
2460         int error;
2461
2462         if (flags != 0)
2463                 return (EINVAL);
2464         NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | SAVENAME | WANTPARENT | AUDITVNODE1,
2465             pathseg, path, fd, &cap_fstat_rights, td);
2466         if ((error = namei(&nd)) != 0)
2467                 return (error);
2468         error = vn_fullpath_hardlink(td, &nd, &retbuf, &freebuf, &size);
2469         if (error == 0) {
2470                 error = copyout(retbuf, buf, size);
2471                 free(freebuf, M_TEMP);
2472         }
2473         NDFREE(&nd, 0);
2474         return (error);
2475 }
2476
2477 int
2478 sys___realpathat(struct thread *td, struct __realpathat_args *uap)
2479 {
2480
2481         return (kern___realpathat(td, uap->fd, uap->path, uap->buf, uap->size,
2482             uap->flags, UIO_USERSPACE));
2483 }
2484
2485 /*
2486  * Retrieve the full filesystem path that correspond to a vnode from the name
2487  * cache (if available)
2488  */
2489 int
2490 vn_fullpath(struct thread *td, struct vnode *vn, char **retbuf, char **freebuf)
2491 {
2492         struct pwd *pwd;
2493         char *buf;
2494         size_t buflen;
2495         int error;
2496
2497         if (__predict_false(vn == NULL))
2498                 return (EINVAL);
2499
2500         buflen = MAXPATHLEN;
2501         buf = malloc(buflen, M_TEMP, M_WAITOK);
2502         pwd = pwd_hold(td);
2503         error = vn_fullpath_any(td, vn, pwd->pwd_rdir, buf, retbuf, &buflen);
2504         pwd_drop(pwd);
2505
2506         if (!error)
2507                 *freebuf = buf;
2508         else
2509                 free(buf, M_TEMP);
2510         return (error);
2511 }
2512
2513 /*
2514  * This function is similar to vn_fullpath, but it attempts to lookup the
2515  * pathname relative to the global root mount point.  This is required for the
2516  * auditing sub-system, as audited pathnames must be absolute, relative to the
2517  * global root mount point.
2518  */
2519 int
2520 vn_fullpath_global(struct thread *td, struct vnode *vn,
2521     char **retbuf, char **freebuf)
2522 {
2523         char *buf;
2524         size_t buflen;
2525         int error;
2526
2527         if (__predict_false(vn == NULL))
2528                 return (EINVAL);
2529         buflen = MAXPATHLEN;
2530         buf = malloc(buflen, M_TEMP, M_WAITOK);
2531         error = vn_fullpath_any(td, vn, rootvnode, buf, retbuf, &buflen);
2532         if (!error)
2533                 *freebuf = buf;
2534         else
2535                 free(buf, M_TEMP);
2536         return (error);
2537 }
2538
2539 int
2540 vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, size_t *buflen)
2541 {
2542         struct vnode *dvp;
2543         struct namecache *ncp;
2544         struct mtx *vlp;
2545         int error;
2546
2547         vlp = VP2VNODELOCK(*vp);
2548         mtx_lock(vlp);
2549         TAILQ_FOREACH(ncp, &((*vp)->v_cache_dst), nc_dst) {
2550                 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2551                         break;
2552         }
2553         if (ncp != NULL) {
2554                 if (*buflen < ncp->nc_nlen) {
2555                         mtx_unlock(vlp);
2556                         vrele(*vp);
2557                         counter_u64_add(numfullpathfail4, 1);
2558                         error = ENOMEM;
2559                         SDT_PROBE3(vfs, namecache, fullpath, return, error,
2560                             vp, NULL);
2561                         return (error);
2562                 }
2563                 *buflen -= ncp->nc_nlen;
2564                 memcpy(buf + *buflen, ncp->nc_name, ncp->nc_nlen);
2565                 SDT_PROBE3(vfs, namecache, fullpath, hit, ncp->nc_dvp,
2566                     ncp->nc_name, vp);
2567                 dvp = *vp;
2568                 *vp = ncp->nc_dvp;
2569                 vref(*vp);
2570                 mtx_unlock(vlp);
2571                 vrele(dvp);
2572                 return (0);
2573         }
2574         SDT_PROBE1(vfs, namecache, fullpath, miss, vp);
2575
2576         mtx_unlock(vlp);
2577         vn_lock(*vp, LK_SHARED | LK_RETRY);
2578         error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen);
2579         vput(*vp);
2580         if (error) {
2581                 counter_u64_add(numfullpathfail2, 1);
2582                 SDT_PROBE3(vfs, namecache, fullpath, return,  error, vp, NULL);
2583                 return (error);
2584         }
2585
2586         *vp = dvp;
2587         if (VN_IS_DOOMED(dvp)) {
2588                 /* forced unmount */
2589                 vrele(dvp);
2590                 error = ENOENT;
2591                 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2592                 return (error);
2593         }
2594         /*
2595          * *vp has its use count incremented still.
2596          */
2597
2598         return (0);
2599 }
2600
2601 /*
2602  * Resolve a directory to a pathname.
2603  *
2604  * The name of the directory can always be found in the namecache or fetched
2605  * from the filesystem. There is also guaranteed to be only one parent, meaning
2606  * we can just follow vnodes up until we find the root.
2607  *
2608  * The vnode must be referenced.
2609  */
2610 static int
2611 vn_fullpath_dir(struct thread *td, struct vnode *vp, struct vnode *rdir,
2612     char *buf, char **retbuf, size_t *len, bool slash_prefixed, size_t addend)
2613 {
2614 #ifdef KDTRACE_HOOKS
2615         struct vnode *startvp = vp;
2616 #endif
2617         struct vnode *vp1;
2618         size_t buflen;
2619         int error;
2620
2621         VNPASS(vp->v_type == VDIR || VN_IS_DOOMED(vp), vp);
2622         VNPASS(vp->v_usecount > 0, vp);
2623
2624         buflen = *len;
2625
2626         if (!slash_prefixed) {
2627                 MPASS(*len >= 2);
2628                 buflen--;
2629                 buf[buflen] = '\0';
2630         }
2631
2632         error = 0;
2633
2634         SDT_PROBE1(vfs, namecache, fullpath, entry, vp);
2635         counter_u64_add(numfullpathcalls, 1);
2636         while (vp != rdir && vp != rootvnode) {
2637                 /*
2638                  * The vp vnode must be already fully constructed,
2639                  * since it is either found in namecache or obtained
2640                  * from VOP_VPTOCNP().  We may test for VV_ROOT safely
2641                  * without obtaining the vnode lock.
2642                  */
2643                 if ((vp->v_vflag & VV_ROOT) != 0) {
2644                         vn_lock(vp, LK_RETRY | LK_SHARED);
2645
2646                         /*
2647                          * With the vnode locked, check for races with
2648                          * unmount, forced or not.  Note that we
2649                          * already verified that vp is not equal to
2650                          * the root vnode, which means that
2651                          * mnt_vnodecovered can be NULL only for the
2652                          * case of unmount.
2653                          */
2654                         if (VN_IS_DOOMED(vp) ||
2655                             (vp1 = vp->v_mount->mnt_vnodecovered) == NULL ||
2656                             vp1->v_mountedhere != vp->v_mount) {
2657                                 vput(vp);
2658                                 error = ENOENT;
2659                                 SDT_PROBE3(vfs, namecache, fullpath, return,
2660                                     error, vp, NULL);
2661                                 break;
2662                         }
2663
2664                         vref(vp1);
2665                         vput(vp);
2666                         vp = vp1;
2667                         continue;
2668                 }
2669                 if (vp->v_type != VDIR) {
2670                         vrele(vp);
2671                         counter_u64_add(numfullpathfail1, 1);
2672                         error = ENOTDIR;
2673                         SDT_PROBE3(vfs, namecache, fullpath, return,
2674                             error, vp, NULL);
2675                         break;
2676                 }
2677                 error = vn_vptocnp(&vp, td->td_ucred, buf, &buflen);
2678                 if (error)
2679                         break;
2680                 if (buflen == 0) {
2681                         vrele(vp);
2682                         error = ENOMEM;
2683                         SDT_PROBE3(vfs, namecache, fullpath, return, error,
2684                             startvp, NULL);
2685                         break;
2686                 }
2687                 buf[--buflen] = '/';
2688                 slash_prefixed = true;
2689         }
2690         if (error)
2691                 return (error);
2692         if (!slash_prefixed) {
2693                 if (buflen == 0) {
2694                         vrele(vp);
2695                         counter_u64_add(numfullpathfail4, 1);
2696                         SDT_PROBE3(vfs, namecache, fullpath, return, ENOMEM,
2697                             startvp, NULL);
2698                         return (ENOMEM);
2699                 }
2700                 buf[--buflen] = '/';
2701         }
2702         counter_u64_add(numfullpathfound, 1);
2703         vrele(vp);
2704
2705         *retbuf = buf + buflen;
2706         SDT_PROBE3(vfs, namecache, fullpath, return, 0, startvp, *retbuf);
2707         *len -= buflen;
2708         *len += addend;
2709         return (0);
2710 }
2711
2712 /*
2713  * Resolve an arbitrary vnode to a pathname.
2714  *
2715  * Note 2 caveats:
2716  * - hardlinks are not tracked, thus if the vnode is not a directory this can
2717  *   resolve to a different path than the one used to find it
2718  * - namecache is not mandatory, meaning names are not guaranteed to be added
2719  *   (in which case resolving fails)
2720  */
2721 static int
2722 vn_fullpath_any(struct thread *td, struct vnode *vp, struct vnode *rdir,
2723     char *buf, char **retbuf, size_t *buflen)
2724 {
2725         size_t orig_buflen;
2726         bool slash_prefixed;
2727         int error;
2728
2729         if (*buflen < 2)
2730                 return (EINVAL);
2731
2732         orig_buflen = *buflen;
2733
2734         vref(vp);
2735         slash_prefixed = false;
2736         if (vp->v_type != VDIR) {
2737                 *buflen -= 1;
2738                 buf[*buflen] = '\0';
2739                 error = vn_vptocnp(&vp, td->td_ucred, buf, buflen);
2740                 if (error)
2741                         return (error);
2742                 if (*buflen == 0) {
2743                         vrele(vp);
2744                         return (ENOMEM);
2745                 }
2746                 *buflen -= 1;
2747                 buf[*buflen] = '/';
2748                 slash_prefixed = true;
2749         }
2750
2751         return (vn_fullpath_dir(td, vp, rdir, buf, retbuf, buflen, slash_prefixed,
2752             orig_buflen - *buflen));
2753 }
2754
2755 /*
2756  * Resolve an arbitrary vnode to a pathname (taking care of hardlinks).
2757  *
2758  * Since the namecache does not track handlings, the caller is expected to first
2759  * look up the target vnode with SAVENAME | WANTPARENT flags passed to namei.
2760  *
2761  * Then we have 2 cases:
2762  * - if the found vnode is a directory, the path can be constructed just by
2763  *   fullowing names up the chain
2764  * - otherwise we populate the buffer with the saved name and start resolving
2765  *   from the parent
2766  */
2767 static int
2768 vn_fullpath_hardlink(struct thread *td, struct nameidata *ndp, char **retbuf,
2769     char **freebuf, size_t *buflen)
2770 {
2771         char *buf, *tmpbuf;
2772         struct pwd *pwd;
2773         struct componentname *cnp;
2774         struct vnode *vp;
2775         size_t addend;
2776         int error;
2777         bool slash_prefixed;
2778
2779         if (*buflen < 2)
2780                 return (EINVAL);
2781         if (*buflen > MAXPATHLEN)
2782                 *buflen = MAXPATHLEN;
2783
2784         slash_prefixed = false;
2785
2786         buf = malloc(*buflen, M_TEMP, M_WAITOK);
2787         pwd = pwd_hold(td);
2788
2789         addend = 0;
2790         vp = ndp->ni_vp;
2791         if (vp->v_type != VDIR) {
2792                 cnp = &ndp->ni_cnd;
2793                 addend = cnp->cn_namelen + 2;
2794                 if (*buflen < addend) {
2795                         error = ENOMEM;
2796                         goto out_bad;
2797                 }
2798                 *buflen -= addend;
2799                 tmpbuf = buf + *buflen;
2800                 tmpbuf[0] = '/';
2801                 memcpy(&tmpbuf[1], cnp->cn_nameptr, cnp->cn_namelen);
2802                 tmpbuf[addend - 1] = '\0';
2803                 slash_prefixed = true;
2804                 vp = ndp->ni_dvp;
2805         }
2806
2807         vref(vp);
2808         error = vn_fullpath_dir(td, vp, pwd->pwd_rdir, buf, retbuf, buflen,
2809             slash_prefixed, addend);
2810         if (error != 0)
2811                 goto out_bad;
2812
2813         pwd_drop(pwd);
2814         *freebuf = buf;
2815
2816         return (0);
2817 out_bad:
2818         pwd_drop(pwd);
2819         free(buf, M_TEMP);
2820         return (error);
2821 }
2822
2823 struct vnode *
2824 vn_dir_dd_ino(struct vnode *vp)
2825 {
2826         struct namecache *ncp;
2827         struct vnode *ddvp;
2828         struct mtx *vlp;
2829         enum vgetstate vs;
2830
2831         ASSERT_VOP_LOCKED(vp, "vn_dir_dd_ino");
2832         vlp = VP2VNODELOCK(vp);
2833         mtx_lock(vlp);
2834         TAILQ_FOREACH(ncp, &(vp->v_cache_dst), nc_dst) {
2835                 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0)
2836                         continue;
2837                 ddvp = ncp->nc_dvp;
2838                 vs = vget_prep(ddvp);
2839                 mtx_unlock(vlp);
2840                 if (vget_finish(ddvp, LK_SHARED | LK_NOWAIT, vs))
2841                         return (NULL);
2842                 return (ddvp);
2843         }
2844         mtx_unlock(vlp);
2845         return (NULL);
2846 }
2847
2848 int
2849 vn_commname(struct vnode *vp, char *buf, u_int buflen)
2850 {
2851         struct namecache *ncp;
2852         struct mtx *vlp;
2853         int l;
2854
2855         vlp = VP2VNODELOCK(vp);
2856         mtx_lock(vlp);
2857         TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst)
2858                 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2859                         break;
2860         if (ncp == NULL) {
2861                 mtx_unlock(vlp);
2862                 return (ENOENT);
2863         }
2864         l = min(ncp->nc_nlen, buflen - 1);
2865         memcpy(buf, ncp->nc_name, l);
2866         mtx_unlock(vlp);
2867         buf[l] = '\0';
2868         return (0);
2869 }
2870
2871 /*
2872  * This function updates path string to vnode's full global path
2873  * and checks the size of the new path string against the pathlen argument.
2874  *
2875  * Requires a locked, referenced vnode.
2876  * Vnode is re-locked on success or ENODEV, otherwise unlocked.
2877  *
2878  * If vp is a directory, the call to vn_fullpath_global() always succeeds
2879  * because it falls back to the ".." lookup if the namecache lookup fails.
2880  */
2881 int
2882 vn_path_to_global_path(struct thread *td, struct vnode *vp, char *path,
2883     u_int pathlen)
2884 {
2885         struct nameidata nd;
2886         struct vnode *vp1;
2887         char *rpath, *fbuf;
2888         int error;
2889
2890         ASSERT_VOP_ELOCKED(vp, __func__);
2891
2892         /* Construct global filesystem path from vp. */
2893         VOP_UNLOCK(vp);
2894         error = vn_fullpath_global(td, vp, &rpath, &fbuf);
2895
2896         if (error != 0) {
2897                 vrele(vp);
2898                 return (error);
2899         }
2900
2901         if (strlen(rpath) >= pathlen) {
2902                 vrele(vp);
2903                 error = ENAMETOOLONG;
2904                 goto out;
2905         }
2906
2907         /*
2908          * Re-lookup the vnode by path to detect a possible rename.
2909          * As a side effect, the vnode is relocked.
2910          * If vnode was renamed, return ENOENT.
2911          */
2912         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
2913             UIO_SYSSPACE, path, td);
2914         error = namei(&nd);
2915         if (error != 0) {
2916                 vrele(vp);
2917                 goto out;
2918         }
2919         NDFREE(&nd, NDF_ONLY_PNBUF);
2920         vp1 = nd.ni_vp;
2921         vrele(vp);
2922         if (vp1 == vp)
2923                 strcpy(path, rpath);
2924         else {
2925                 vput(vp1);
2926                 error = ENOENT;
2927         }
2928
2929 out:
2930         free(fbuf, M_TEMP);
2931         return (error);
2932 }
2933
2934 #ifdef DDB
2935 static void
2936 db_print_vpath(struct vnode *vp)
2937 {
2938
2939         while (vp != NULL) {
2940                 db_printf("%p: ", vp);
2941                 if (vp == rootvnode) {
2942                         db_printf("/");
2943                         vp = NULL;
2944                 } else {
2945                         if (vp->v_vflag & VV_ROOT) {
2946                                 db_printf("<mount point>");
2947                                 vp = vp->v_mount->mnt_vnodecovered;
2948                         } else {
2949                                 struct namecache *ncp;
2950                                 char *ncn;
2951                                 int i;
2952
2953                                 ncp = TAILQ_FIRST(&vp->v_cache_dst);
2954                                 if (ncp != NULL) {
2955                                         ncn = ncp->nc_name;
2956                                         for (i = 0; i < ncp->nc_nlen; i++)
2957                                                 db_printf("%c", *ncn++);
2958                                         vp = ncp->nc_dvp;
2959                                 } else {
2960                                         vp = NULL;
2961                                 }
2962                         }
2963                 }
2964                 db_printf("\n");
2965         }
2966
2967         return;
2968 }
2969
2970 DB_SHOW_COMMAND(vpath, db_show_vpath)
2971 {
2972         struct vnode *vp;
2973
2974         if (!have_addr) {
2975                 db_printf("usage: show vpath <struct vnode *>\n");
2976                 return;
2977         }
2978
2979         vp = (struct vnode *)addr;
2980         db_print_vpath(vp);
2981 }
2982
2983 #endif
2984
2985 extern uma_zone_t namei_zone;
2986
2987 static bool __read_frequently cache_fast_lookup = true;
2988 SYSCTL_BOOL(_vfs, OID_AUTO, cache_fast_lookup, CTLFLAG_RW,
2989     &cache_fast_lookup, 0, "");
2990
2991 #define CACHE_FPL_FAILED        -2020
2992
2993 static void
2994 cache_fpl_cleanup_cnp(struct componentname *cnp)
2995 {
2996
2997         uma_zfree(namei_zone, cnp->cn_pnbuf);
2998 #ifdef DIAGNOSTIC
2999         cnp->cn_pnbuf = NULL;
3000         cnp->cn_nameptr = NULL;
3001 #endif
3002 }
3003
3004 static void
3005 cache_fpl_handle_root(struct nameidata *ndp, struct vnode **dpp)
3006 {
3007         struct componentname *cnp;
3008
3009         cnp = &ndp->ni_cnd;
3010         while (*(cnp->cn_nameptr) == '/') {
3011                 cnp->cn_nameptr++;
3012                 ndp->ni_pathlen--;
3013         }
3014
3015         *dpp = ndp->ni_rootdir;
3016 }
3017
3018 /*
3019  * Components of nameidata (or objects it can point to) which may
3020  * need restoring in case fast path lookup fails.
3021  */
3022 struct nameidata_saved {
3023         long cn_namelen;
3024         char *cn_nameptr;
3025         size_t ni_pathlen;
3026         int cn_flags;
3027 };
3028
3029 struct cache_fpl {
3030         struct nameidata *ndp;
3031         struct componentname *cnp;
3032         struct pwd *pwd;
3033         struct vnode *dvp;
3034         struct vnode *tvp;
3035         seqc_t dvp_seqc;
3036         seqc_t tvp_seqc;
3037         struct nameidata_saved snd;
3038         int line;
3039         enum cache_fpl_status status:8;
3040         bool in_smr;
3041 };
3042
3043 static void
3044 cache_fpl_checkpoint(struct cache_fpl *fpl, struct nameidata_saved *snd)
3045 {
3046
3047         snd->cn_flags = fpl->ndp->ni_cnd.cn_flags;
3048         snd->cn_namelen = fpl->ndp->ni_cnd.cn_namelen;
3049         snd->cn_nameptr = fpl->ndp->ni_cnd.cn_nameptr;
3050         snd->ni_pathlen = fpl->ndp->ni_pathlen;
3051 }
3052
3053 static void
3054 cache_fpl_restore(struct cache_fpl *fpl, struct nameidata_saved *snd)
3055 {
3056
3057         fpl->ndp->ni_cnd.cn_flags = snd->cn_flags;
3058         fpl->ndp->ni_cnd.cn_namelen = snd->cn_namelen;
3059         fpl->ndp->ni_cnd.cn_nameptr = snd->cn_nameptr;
3060         fpl->ndp->ni_pathlen = snd->ni_pathlen;
3061 }
3062
3063 #ifdef INVARIANTS
3064 #define cache_fpl_smr_assert_entered(fpl) ({                    \
3065         struct cache_fpl *_fpl = (fpl);                         \
3066         MPASS(_fpl->in_smr == true);                            \
3067         VFS_SMR_ASSERT_ENTERED();                               \
3068 })
3069 #define cache_fpl_smr_assert_not_entered(fpl) ({                \
3070         struct cache_fpl *_fpl = (fpl);                         \
3071         MPASS(_fpl->in_smr == false);                           \
3072         VFS_SMR_ASSERT_NOT_ENTERED();                           \
3073 })
3074 #else
3075 #define cache_fpl_smr_assert_entered(fpl) do { } while (0)
3076 #define cache_fpl_smr_assert_not_entered(fpl) do { } while (0)
3077 #endif
3078
3079 #define cache_fpl_smr_enter_initial(fpl) ({                     \
3080         struct cache_fpl *_fpl = (fpl);                         \
3081         vfs_smr_enter();                                        \
3082         _fpl->in_smr = true;                                    \
3083 })
3084
3085 #define cache_fpl_smr_enter(fpl) ({                             \
3086         struct cache_fpl *_fpl = (fpl);                         \
3087         MPASS(_fpl->in_smr == false);                           \
3088         vfs_smr_enter();                                        \
3089         _fpl->in_smr = true;                                    \
3090 })
3091
3092 #define cache_fpl_smr_exit(fpl) ({                              \
3093         struct cache_fpl *_fpl = (fpl);                         \
3094         MPASS(_fpl->in_smr == true);                            \
3095         vfs_smr_exit();                                         \
3096         _fpl->in_smr = false;                                   \
3097 })
3098
3099 static int
3100 cache_fpl_aborted_impl(struct cache_fpl *fpl, int line)
3101 {
3102
3103         if (fpl->status != CACHE_FPL_STATUS_UNSET) {
3104                 KASSERT(fpl->status == CACHE_FPL_STATUS_PARTIAL,
3105                     ("%s: converting to abort from %d at %d, set at %d\n",
3106                     __func__, fpl->status, line, fpl->line));
3107         }
3108         fpl->status = CACHE_FPL_STATUS_ABORTED;
3109         fpl->line = line;
3110         return (CACHE_FPL_FAILED);
3111 }
3112
3113 #define cache_fpl_aborted(x)    cache_fpl_aborted_impl((x), __LINE__)
3114
3115 static int
3116 cache_fpl_partial_impl(struct cache_fpl *fpl, int line)
3117 {
3118
3119         KASSERT(fpl->status == CACHE_FPL_STATUS_UNSET,
3120             ("%s: setting to partial at %d, but already set to %d at %d\n",
3121             __func__, line, fpl->status, fpl->line));
3122         cache_fpl_smr_assert_entered(fpl);
3123         fpl->status = CACHE_FPL_STATUS_PARTIAL;
3124         fpl->line = line;
3125         return (CACHE_FPL_FAILED);
3126 }
3127
3128 #define cache_fpl_partial(x)    cache_fpl_partial_impl((x), __LINE__)
3129
3130 static int
3131 cache_fpl_handled_impl(struct cache_fpl *fpl, int error, int line)
3132 {
3133
3134         KASSERT(fpl->status == CACHE_FPL_STATUS_UNSET,
3135             ("%s: setting to handled at %d, but already set to %d at %d\n",
3136             __func__, line, fpl->status, fpl->line));
3137         cache_fpl_smr_assert_not_entered(fpl);
3138         MPASS(error != CACHE_FPL_FAILED);
3139         fpl->status = CACHE_FPL_STATUS_HANDLED;
3140         fpl->line = line;
3141         return (error);
3142 }
3143
3144 #define cache_fpl_handled(x, e) cache_fpl_handled_impl((x), (e), __LINE__)
3145
3146 #define CACHE_FPL_SUPPORTED_CN_FLAGS \
3147         (LOCKLEAF | LOCKPARENT | WANTPARENT | NOCACHE | FOLLOW | LOCKSHARED | SAVENAME | \
3148          WILLBEDIR | ISOPEN | NOMACCHECK | AUDITVNODE1 | AUDITVNODE2)
3149
3150 #define CACHE_FPL_INTERNAL_CN_FLAGS \
3151         (ISDOTDOT | MAKEENTRY | ISLASTCN)
3152
3153 _Static_assert((CACHE_FPL_SUPPORTED_CN_FLAGS & CACHE_FPL_INTERNAL_CN_FLAGS) == 0,
3154     "supported and internal flags overlap");
3155
3156 static bool
3157 cache_fpl_islastcn(struct nameidata *ndp)
3158 {
3159
3160         return (*ndp->ni_next == 0);
3161 }
3162
3163 static bool
3164 cache_fpl_isdotdot(struct componentname *cnp)
3165 {
3166
3167         if (cnp->cn_namelen == 2 &&
3168             cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
3169                 return (true);
3170         return (false);
3171 }
3172
3173 static bool
3174 cache_can_fplookup(struct cache_fpl *fpl)
3175 {
3176         struct nameidata *ndp;
3177         struct componentname *cnp;
3178         struct thread *td;
3179
3180         ndp = fpl->ndp;
3181         cnp = fpl->cnp;
3182         td = cnp->cn_thread;
3183
3184         if (!cache_fast_lookup) {
3185                 cache_fpl_aborted(fpl);
3186                 return (false);
3187         }
3188 #ifdef MAC
3189         if (mac_vnode_check_lookup_enabled()) {
3190                 cache_fpl_aborted(fpl);
3191                 return (false);
3192         }
3193 #endif
3194         if ((cnp->cn_flags & ~CACHE_FPL_SUPPORTED_CN_FLAGS) != 0) {
3195                 cache_fpl_aborted(fpl);
3196                 return (false);
3197         }
3198         if (ndp->ni_dirfd != AT_FDCWD) {
3199                 cache_fpl_aborted(fpl);
3200                 return (false);
3201         }
3202         if (IN_CAPABILITY_MODE(td)) {
3203                 cache_fpl_aborted(fpl);
3204                 return (false);
3205         }
3206         if (AUDITING_TD(td)) {
3207                 cache_fpl_aborted(fpl);
3208                 return (false);
3209         }
3210         if (ndp->ni_startdir != NULL) {
3211                 cache_fpl_aborted(fpl);
3212                 return (false);
3213         }
3214         return (true);
3215 }
3216
3217 static bool
3218 cache_fplookup_vnode_supported(struct vnode *vp)
3219 {
3220
3221         return (vp->v_type != VLNK);
3222 }
3223
3224 /*
3225  * Move a negative entry to the hot list.
3226  *
3227  * We have to take locks, but they may be contended and in the worst
3228  * case we may need to go off CPU. We don't want to spin within the
3229  * smr section and we can't block with it. Instead we are going to
3230  * look up the entry again.
3231  */
3232 static int __noinline
3233 cache_fplookup_negative_promote(struct cache_fpl *fpl, struct namecache *oncp,
3234     uint32_t hash)
3235 {
3236         struct componentname *cnp;
3237         struct namecache *ncp;
3238         struct neglist *neglist;
3239         struct negstate *negstate;
3240         struct vnode *dvp;
3241         u_char nc_flag;
3242
3243         cnp = fpl->cnp;
3244         dvp = fpl->dvp;
3245
3246         if (!vhold_smr(dvp))
3247                 return (cache_fpl_aborted(fpl));
3248
3249         neglist = NCP2NEGLIST(oncp);
3250         cache_fpl_smr_exit(fpl);
3251
3252         mtx_lock(&ncneg_hot.nl_lock);
3253         mtx_lock(&neglist->nl_lock);
3254         /*
3255          * For hash iteration.
3256          */
3257         cache_fpl_smr_enter(fpl);
3258
3259         /*
3260          * Avoid all surprises by only succeeding if we got the same entry and
3261          * bailing completely otherwise.
3262          *
3263          * In particular at this point there can be a new ncp which matches the
3264          * search but hashes to a different neglist.
3265          */
3266         CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
3267                 if (ncp == oncp)
3268                         break;
3269         }
3270
3271         /*
3272          * No match to begin with.
3273          */
3274         if (__predict_false(ncp == NULL)) {
3275                 goto out_abort;
3276         }
3277
3278         /*
3279          * The newly found entry may be something different...
3280          */
3281         if (!(ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
3282             !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))) {
3283                 goto out_abort;
3284         }
3285
3286         /*
3287          * ... and not even negative.
3288          */
3289         nc_flag = atomic_load_char(&ncp->nc_flag);
3290         if ((nc_flag & NCF_NEGATIVE) == 0) {
3291                 goto out_abort;
3292         }
3293
3294         if (__predict_false(!cache_ncp_canuse(ncp))) {
3295                 goto out_abort;
3296         }
3297
3298         negstate = NCP2NEGSTATE(ncp);
3299         if ((negstate->neg_flag & NEG_HOT) == 0) {
3300                 numhotneg++;
3301                 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
3302                 TAILQ_INSERT_TAIL(&ncneg_hot.nl_list, ncp, nc_dst);
3303                 negstate->neg_flag |= NEG_HOT;
3304         }
3305
3306         SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp, ncp->nc_name);
3307         counter_u64_add(numneghits, 1);
3308         cache_fpl_smr_exit(fpl);
3309         mtx_unlock(&neglist->nl_lock);
3310         mtx_unlock(&ncneg_hot.nl_lock);
3311         vdrop(dvp);
3312         return (cache_fpl_handled(fpl, ENOENT));
3313 out_abort:
3314         cache_fpl_smr_exit(fpl);
3315         mtx_unlock(&neglist->nl_lock);
3316         mtx_unlock(&ncneg_hot.nl_lock);
3317         vdrop(dvp);
3318         return (cache_fpl_aborted(fpl));
3319 }
3320
3321 /*
3322  * The target vnode is not supported, prepare for the slow path to take over.
3323  */
3324 static int __noinline
3325 cache_fplookup_partial_setup(struct cache_fpl *fpl)
3326 {
3327         struct nameidata *ndp;
3328         struct componentname *cnp;
3329         enum vgetstate dvs;
3330         struct vnode *dvp;
3331         struct pwd *pwd;
3332         seqc_t dvp_seqc;
3333
3334         ndp = fpl->ndp;
3335         cnp = fpl->cnp;
3336         dvp = fpl->dvp;
3337         dvp_seqc = fpl->dvp_seqc;
3338
3339         dvs = vget_prep_smr(dvp);
3340         if (__predict_false(dvs == VGET_NONE)) {
3341                 cache_fpl_smr_exit(fpl);
3342                 return (cache_fpl_aborted(fpl));
3343         }
3344
3345         cache_fpl_smr_exit(fpl);
3346
3347         vget_finish_ref(dvp, dvs);
3348         if (!vn_seqc_consistent(dvp, dvp_seqc)) {
3349                 vrele(dvp);
3350                 return (cache_fpl_aborted(fpl));
3351         }
3352
3353         pwd = pwd_hold(curthread);
3354         if (fpl->pwd != pwd) {
3355                 vrele(dvp);
3356                 pwd_drop(pwd);
3357                 return (cache_fpl_aborted(fpl));
3358         }
3359
3360         cache_fpl_restore(fpl, &fpl->snd);
3361
3362         ndp->ni_startdir = dvp;
3363         cnp->cn_flags |= MAKEENTRY;
3364         if (cache_fpl_islastcn(ndp))
3365                 cnp->cn_flags |= ISLASTCN;
3366         if (cache_fpl_isdotdot(cnp))
3367                 cnp->cn_flags |= ISDOTDOT;
3368
3369         return (0);
3370 }
3371
3372 static int
3373 cache_fplookup_final_child(struct cache_fpl *fpl, enum vgetstate tvs)
3374 {
3375         struct componentname *cnp;
3376         struct vnode *tvp;
3377         seqc_t tvp_seqc;
3378         int error, lkflags;
3379
3380         cnp = fpl->cnp;
3381         tvp = fpl->tvp;
3382         tvp_seqc = fpl->tvp_seqc;
3383
3384         if ((cnp->cn_flags & LOCKLEAF) != 0) {
3385                 lkflags = LK_SHARED;
3386                 if ((cnp->cn_flags & LOCKSHARED) == 0)
3387                         lkflags = LK_EXCLUSIVE;
3388                 error = vget_finish(tvp, lkflags, tvs);
3389                 if (__predict_false(error != 0)) {
3390                         return (cache_fpl_aborted(fpl));
3391                 }
3392         } else {
3393                 vget_finish_ref(tvp, tvs);
3394         }
3395
3396         if (!vn_seqc_consistent(tvp, tvp_seqc)) {
3397                 if ((cnp->cn_flags & LOCKLEAF) != 0)
3398                         vput(tvp);
3399                 else
3400                         vrele(tvp);
3401                 return (cache_fpl_aborted(fpl));
3402         }
3403
3404         return (cache_fpl_handled(fpl, 0));
3405 }
3406
3407 /*
3408  * They want to possibly modify the state of the namecache.
3409  *
3410  * Don't try to match the API contract, just leave.
3411  * TODO: this leaves scalability on the table
3412  */
3413 static int
3414 cache_fplookup_final_modifying(struct cache_fpl *fpl)
3415 {
3416         struct componentname *cnp;
3417
3418         cnp = fpl->cnp;
3419         MPASS(cnp->cn_nameiop != LOOKUP);
3420         return (cache_fpl_partial(fpl));
3421 }
3422
3423 static int __noinline
3424 cache_fplookup_final_withparent(struct cache_fpl *fpl)
3425 {
3426         struct componentname *cnp;
3427         enum vgetstate dvs, tvs;
3428         struct vnode *dvp, *tvp;
3429         seqc_t dvp_seqc, tvp_seqc;
3430         int error;
3431
3432         cnp = fpl->cnp;
3433         dvp = fpl->dvp;
3434         dvp_seqc = fpl->dvp_seqc;
3435         tvp = fpl->tvp;
3436         tvp_seqc = fpl->tvp_seqc;
3437
3438         MPASS((cnp->cn_flags & (LOCKPARENT|WANTPARENT)) != 0);
3439
3440         /*
3441          * This is less efficient than it can be for simplicity.
3442          */
3443         dvs = vget_prep_smr(dvp);
3444         if (__predict_false(dvs == VGET_NONE)) {
3445                 return (cache_fpl_aborted(fpl));
3446         }
3447         tvs = vget_prep_smr(tvp);
3448         if (__predict_false(tvs == VGET_NONE)) {
3449                 cache_fpl_smr_exit(fpl);
3450                 vget_abort(dvp, dvs);
3451                 return (cache_fpl_aborted(fpl));
3452         }
3453
3454         cache_fpl_smr_exit(fpl);
3455
3456         if ((cnp->cn_flags & LOCKPARENT) != 0) {
3457                 error = vget_finish(dvp, LK_EXCLUSIVE, dvs);
3458                 if (__predict_false(error != 0)) {
3459                         vget_abort(tvp, tvs);
3460                         return (cache_fpl_aborted(fpl));
3461                 }
3462         } else {
3463                 vget_finish_ref(dvp, dvs);
3464         }
3465
3466         if (!vn_seqc_consistent(dvp, dvp_seqc)) {
3467                 vget_abort(tvp, tvs);
3468                 if ((cnp->cn_flags & LOCKPARENT) != 0)
3469                         vput(dvp);
3470                 else
3471                         vrele(dvp);
3472                 return (cache_fpl_aborted(fpl));
3473         }
3474
3475         error = cache_fplookup_final_child(fpl, tvs);
3476         if (__predict_false(error != 0)) {
3477                 MPASS(fpl->status == CACHE_FPL_STATUS_ABORTED);
3478                 if ((cnp->cn_flags & LOCKPARENT) != 0)
3479                         vput(dvp);
3480                 else
3481                         vrele(dvp);
3482                 return (error);
3483         }
3484
3485         MPASS(fpl->status == CACHE_FPL_STATUS_HANDLED);
3486         return (0);
3487 }
3488
3489 static int
3490 cache_fplookup_final(struct cache_fpl *fpl)
3491 {
3492         struct componentname *cnp;
3493         enum vgetstate tvs;
3494         struct vnode *dvp, *tvp;
3495         seqc_t dvp_seqc, tvp_seqc;
3496
3497         cnp = fpl->cnp;
3498         dvp = fpl->dvp;
3499         dvp_seqc = fpl->dvp_seqc;
3500         tvp = fpl->tvp;
3501         tvp_seqc = fpl->tvp_seqc;
3502
3503         VNPASS(cache_fplookup_vnode_supported(dvp), dvp);
3504
3505         if (cnp->cn_nameiop != LOOKUP) {
3506                 return (cache_fplookup_final_modifying(fpl));
3507         }
3508
3509         if ((cnp->cn_flags & (LOCKPARENT|WANTPARENT)) != 0)
3510                 return (cache_fplookup_final_withparent(fpl));
3511
3512         tvs = vget_prep_smr(tvp);
3513         if (__predict_false(tvs == VGET_NONE)) {
3514                 return (cache_fpl_partial(fpl));
3515         }
3516
3517         if (!vn_seqc_consistent(dvp, dvp_seqc)) {
3518                 cache_fpl_smr_exit(fpl);
3519                 vget_abort(tvp, tvs);
3520                 return (cache_fpl_aborted(fpl));
3521         }
3522
3523         cache_fpl_smr_exit(fpl);
3524         return (cache_fplookup_final_child(fpl, tvs));
3525 }
3526
3527 static int __noinline
3528 cache_fplookup_dot(struct cache_fpl *fpl)
3529 {
3530         struct vnode *dvp;
3531
3532         dvp = fpl->dvp;
3533
3534         fpl->tvp = dvp;
3535         fpl->tvp_seqc = vn_seqc_read_any(dvp);
3536         if (seqc_in_modify(fpl->tvp_seqc)) {
3537                 return (cache_fpl_aborted(fpl));
3538         }
3539
3540         counter_u64_add(dothits, 1);
3541         SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", dvp);
3542
3543         return (0);
3544 }
3545
3546 static int __noinline
3547 cache_fplookup_dotdot(struct cache_fpl *fpl)
3548 {
3549         struct nameidata *ndp;
3550         struct componentname *cnp;
3551         struct namecache *ncp;
3552         struct vnode *dvp;
3553         struct prison *pr;
3554         u_char nc_flag;
3555
3556         ndp = fpl->ndp;
3557         cnp = fpl->cnp;
3558         dvp = fpl->dvp;
3559
3560         /*
3561          * XXX this is racy the same way regular lookup is
3562          */
3563         for (pr = cnp->cn_cred->cr_prison; pr != NULL;
3564             pr = pr->pr_parent)
3565                 if (dvp == pr->pr_root)
3566                         break;
3567
3568         if (dvp == ndp->ni_rootdir ||
3569             dvp == ndp->ni_topdir ||
3570             dvp == rootvnode ||
3571             pr != NULL) {
3572                 fpl->tvp = dvp;
3573                 fpl->tvp_seqc = vn_seqc_read_any(dvp);
3574                 if (seqc_in_modify(fpl->tvp_seqc)) {
3575                         return (cache_fpl_aborted(fpl));
3576                 }
3577                 return (0);
3578         }
3579
3580         if ((dvp->v_vflag & VV_ROOT) != 0) {
3581                 /*
3582                  * TODO
3583                  * The opposite of climb mount is needed here.
3584                  */
3585                 return (cache_fpl_aborted(fpl));
3586         }
3587
3588         ncp = atomic_load_ptr(&dvp->v_cache_dd);
3589         if (ncp == NULL) {
3590                 return (cache_fpl_aborted(fpl));
3591         }
3592
3593         nc_flag = atomic_load_char(&ncp->nc_flag);
3594         if ((nc_flag & NCF_ISDOTDOT) != 0) {
3595                 if ((nc_flag & NCF_NEGATIVE) != 0)
3596                         return (cache_fpl_aborted(fpl));
3597                 fpl->tvp = ncp->nc_vp;
3598         } else {
3599                 fpl->tvp = ncp->nc_dvp;
3600         }
3601
3602         if (__predict_false(!cache_ncp_canuse(ncp))) {
3603                 return (cache_fpl_aborted(fpl));
3604         }
3605
3606         fpl->tvp_seqc = vn_seqc_read_any(fpl->tvp);
3607         if (seqc_in_modify(fpl->tvp_seqc)) {
3608                 return (cache_fpl_partial(fpl));
3609         }
3610
3611         counter_u64_add(dotdothits, 1);
3612         return (0);
3613 }
3614
3615 static int
3616 cache_fplookup_next(struct cache_fpl *fpl)
3617 {
3618         struct componentname *cnp;
3619         struct namecache *ncp;
3620         struct negstate *negstate;
3621         struct vnode *dvp, *tvp;
3622         u_char nc_flag;
3623         uint32_t hash;
3624         bool neg_hot;
3625
3626         cnp = fpl->cnp;
3627         dvp = fpl->dvp;
3628
3629         if (__predict_false(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.')) {
3630                 return (cache_fplookup_dot(fpl));
3631         }
3632
3633         hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
3634
3635         CK_SLIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
3636                 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
3637                     !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
3638                         break;
3639         }
3640
3641         /*
3642          * If there is no entry we have to punt to the slow path to perform
3643          * actual lookup. Should there be nothing with this name a negative
3644          * entry will be created.
3645          */
3646         if (__predict_false(ncp == NULL)) {
3647                 return (cache_fpl_partial(fpl));
3648         }
3649
3650         tvp = atomic_load_ptr(&ncp->nc_vp);
3651         nc_flag = atomic_load_char(&ncp->nc_flag);
3652         if ((nc_flag & NCF_NEGATIVE) != 0) {
3653                 /*
3654                  * If they want to create an entry we need to replace this one.
3655                  */
3656                 if (__predict_false(fpl->cnp->cn_nameiop == CREATE)) {
3657                         return (cache_fpl_partial(fpl));
3658                 }
3659                 negstate = NCP2NEGSTATE(ncp);
3660                 neg_hot = ((negstate->neg_flag & NEG_HOT) != 0);
3661                 if (__predict_false(!cache_ncp_canuse(ncp))) {
3662                         return (cache_fpl_partial(fpl));
3663                 }
3664                 if (__predict_false((nc_flag & NCF_WHITE) != 0)) {
3665                         return (cache_fpl_partial(fpl));
3666                 }
3667                 if (!neg_hot) {
3668                         return (cache_fplookup_negative_promote(fpl, ncp, hash));
3669                 }
3670                 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp,
3671                     ncp->nc_name);
3672                 counter_u64_add(numneghits, 1);
3673                 cache_fpl_smr_exit(fpl);
3674                 return (cache_fpl_handled(fpl, ENOENT));
3675         }
3676
3677         if (__predict_false(!cache_ncp_canuse(ncp))) {
3678                 return (cache_fpl_partial(fpl));
3679         }
3680
3681         fpl->tvp = tvp;
3682         fpl->tvp_seqc = vn_seqc_read_any(tvp);
3683         if (seqc_in_modify(fpl->tvp_seqc)) {
3684                 return (cache_fpl_partial(fpl));
3685         }
3686
3687         if (!cache_fplookup_vnode_supported(tvp)) {
3688                 return (cache_fpl_partial(fpl));
3689         }
3690
3691         counter_u64_add(numposhits, 1);
3692         SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name, tvp);
3693         return (0);
3694 }
3695
3696 static bool
3697 cache_fplookup_mp_supported(struct mount *mp)
3698 {
3699
3700         if (mp == NULL)
3701                 return (false);
3702         if ((mp->mnt_kern_flag & MNTK_FPLOOKUP) == 0)
3703                 return (false);
3704         return (true);
3705 }
3706
3707 /*
3708  * Walk up the mount stack (if any).
3709  *
3710  * Correctness is provided in the following ways:
3711  * - all vnodes are protected from freeing with SMR
3712  * - struct mount objects are type stable making them always safe to access
3713  * - stability of the particular mount is provided by busying it
3714  * - relationship between the vnode which is mounted on and the mount is
3715  *   verified with the vnode sequence counter after busying
3716  * - association between root vnode of the mount and the mount is protected
3717  *   by busy
3718  *
3719  * From that point on we can read the sequence counter of the root vnode
3720  * and get the next mount on the stack (if any) using the same protection.
3721  *
3722  * By the end of successful walk we are guaranteed the reached state was
3723  * indeed present at least at some point which matches the regular lookup.
3724  */
3725 static int __noinline
3726 cache_fplookup_climb_mount(struct cache_fpl *fpl)
3727 {
3728         struct mount *mp, *prev_mp;
3729         struct vnode *vp;
3730         seqc_t vp_seqc;
3731
3732         vp = fpl->tvp;
3733         vp_seqc = fpl->tvp_seqc;
3734
3735         VNPASS(vp->v_type == VDIR || vp->v_type == VBAD, vp);
3736         mp = atomic_load_ptr(&vp->v_mountedhere);
3737         if (mp == NULL)
3738                 return (0);
3739
3740         prev_mp = NULL;
3741         for (;;) {
3742                 if (!vfs_op_thread_enter_crit(mp)) {
3743                         if (prev_mp != NULL)
3744                                 vfs_op_thread_exit_crit(prev_mp);
3745                         return (cache_fpl_partial(fpl));
3746                 }
3747                 if (prev_mp != NULL)
3748                         vfs_op_thread_exit_crit(prev_mp);
3749                 if (!vn_seqc_consistent(vp, vp_seqc)) {
3750                         vfs_op_thread_exit_crit(mp);
3751                         return (cache_fpl_partial(fpl));
3752                 }
3753                 if (!cache_fplookup_mp_supported(mp)) {
3754                         vfs_op_thread_exit_crit(mp);
3755                         return (cache_fpl_partial(fpl));
3756                 }
3757                 vp = atomic_load_ptr(&mp->mnt_rootvnode);
3758                 if (vp == NULL || VN_IS_DOOMED(vp)) {
3759                         vfs_op_thread_exit_crit(mp);
3760                         return (cache_fpl_partial(fpl));
3761                 }
3762                 vp_seqc = vn_seqc_read_any(vp);
3763                 if (seqc_in_modify(vp_seqc)) {
3764                         vfs_op_thread_exit_crit(mp);
3765                         return (cache_fpl_partial(fpl));
3766                 }
3767                 prev_mp = mp;
3768                 mp = atomic_load_ptr(&vp->v_mountedhere);
3769                 if (mp == NULL)
3770                         break;
3771         }
3772
3773         vfs_op_thread_exit_crit(prev_mp);
3774         fpl->tvp = vp;
3775         fpl->tvp_seqc = vp_seqc;
3776         return (0);
3777 }
3778
3779 static bool
3780 cache_fplookup_need_climb_mount(struct cache_fpl *fpl)
3781 {
3782         struct mount *mp;
3783         struct vnode *vp;
3784
3785         vp = fpl->tvp;
3786
3787         /*
3788          * Hack: while this is a union, the pointer tends to be NULL so save on
3789          * a branch.
3790          */
3791         mp = atomic_load_ptr(&vp->v_mountedhere);
3792         if (mp == NULL)
3793                 return (false);
3794         if (vp->v_type == VDIR)
3795                 return (true);
3796         return (false);
3797 }
3798
3799 /*
3800  * Parse the path.
3801  *
3802  * The code is mostly copy-pasted from regular lookup, see lookup().
3803  * The structure is maintained along with comments for easier maintenance.
3804  * Deduplicating the code will become feasible after fast path lookup
3805  * becomes more feature-complete.
3806  */
3807 static int
3808 cache_fplookup_parse(struct cache_fpl *fpl)
3809 {
3810         struct nameidata *ndp;
3811         struct componentname *cnp;
3812         char *cp;
3813         char *prev_ni_next;             /* saved ndp->ni_next */
3814         size_t prev_ni_pathlen;         /* saved ndp->ni_pathlen */
3815
3816         ndp = fpl->ndp;
3817         cnp = fpl->cnp;
3818
3819         /*
3820          * Search a new directory.
3821          *
3822          * The last component of the filename is left accessible via
3823          * cnp->cn_nameptr for callers that need the name. Callers needing
3824          * the name set the SAVENAME flag. When done, they assume
3825          * responsibility for freeing the pathname buffer.
3826          */
3827         for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
3828                 continue;
3829         cnp->cn_namelen = cp - cnp->cn_nameptr;
3830         if (__predict_false(cnp->cn_namelen > NAME_MAX)) {
3831                 cache_fpl_smr_exit(fpl);
3832                 return (cache_fpl_handled(fpl, ENAMETOOLONG));
3833         }
3834         prev_ni_pathlen = ndp->ni_pathlen;
3835         ndp->ni_pathlen -= cnp->cn_namelen;
3836         KASSERT(ndp->ni_pathlen <= PATH_MAX,
3837             ("%s: ni_pathlen underflow to %zd\n", __func__, ndp->ni_pathlen));
3838         prev_ni_next = ndp->ni_next;
3839         ndp->ni_next = cp;
3840
3841         /*
3842          * Replace multiple slashes by a single slash and trailing slashes
3843          * by a null.  This must be done before VOP_LOOKUP() because some
3844          * fs's don't know about trailing slashes.  Remember if there were
3845          * trailing slashes to handle symlinks, existing non-directories
3846          * and non-existing files that won't be directories specially later.
3847          */
3848         while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
3849                 cp++;
3850                 ndp->ni_pathlen--;
3851                 if (*cp == '\0') {
3852                         /*
3853                          * TODO
3854                          * Regular lookup performs the following:
3855                          * *ndp->ni_next = '\0';
3856                          * cnp->cn_flags |= TRAILINGSLASH;
3857                          *
3858                          * Which is problematic since it modifies data read
3859                          * from userspace. Then if fast path lookup was to
3860                          * abort we would have to either restore it or convey
3861                          * the flag. Since this is a corner case just ignore
3862                          * it for simplicity.
3863                          */
3864                         return (cache_fpl_partial(fpl));
3865                 }
3866         }
3867         ndp->ni_next = cp;
3868
3869         /*
3870          * Check for degenerate name (e.g. / or "")
3871          * which is a way of talking about a directory,
3872          * e.g. like "/." or ".".
3873          *
3874          * TODO
3875          * Another corner case handled by the regular lookup
3876          */
3877         if (__predict_false(cnp->cn_nameptr[0] == '\0')) {
3878                 return (cache_fpl_partial(fpl));
3879         }
3880         return (0);
3881 }
3882
3883 static void
3884 cache_fplookup_parse_advance(struct cache_fpl *fpl)
3885 {
3886         struct nameidata *ndp;
3887         struct componentname *cnp;
3888
3889         ndp = fpl->ndp;
3890         cnp = fpl->cnp;
3891
3892         cnp->cn_nameptr = ndp->ni_next;
3893         while (*cnp->cn_nameptr == '/') {
3894                 cnp->cn_nameptr++;
3895                 ndp->ni_pathlen--;
3896         }
3897 }
3898
3899 static int __noinline
3900 cache_fplookup_failed_vexec(struct cache_fpl *fpl, int error)
3901 {
3902
3903         switch (error) {
3904         case EAGAIN:
3905                 /*
3906                  * Can happen when racing against vgone.
3907                  * */
3908         case EOPNOTSUPP:
3909                 cache_fpl_partial(fpl);
3910                 break;
3911         default:
3912                 /*
3913                  * See the API contract for VOP_FPLOOKUP_VEXEC.
3914                  */
3915                 if (!vn_seqc_consistent(fpl->dvp, fpl->dvp_seqc)) {
3916                         error = cache_fpl_aborted(fpl);
3917                 } else {
3918                         cache_fpl_smr_exit(fpl);
3919                         cache_fpl_handled(fpl, error);
3920                 }
3921                 break;
3922         }
3923         return (error);
3924 }
3925
3926 static int
3927 cache_fplookup_impl(struct vnode *dvp, struct cache_fpl *fpl)
3928 {
3929         struct nameidata *ndp;
3930         struct componentname *cnp;
3931         struct mount *mp;
3932         int error;
3933
3934         error = CACHE_FPL_FAILED;
3935         ndp = fpl->ndp;
3936         cnp = fpl->cnp;
3937
3938         cache_fpl_checkpoint(fpl, &fpl->snd);
3939
3940         fpl->dvp = dvp;
3941         fpl->dvp_seqc = vn_seqc_read_any(fpl->dvp);
3942         if (seqc_in_modify(fpl->dvp_seqc)) {
3943                 cache_fpl_aborted(fpl);
3944                 goto out;
3945         }
3946         mp = atomic_load_ptr(&fpl->dvp->v_mount);
3947         if (!cache_fplookup_mp_supported(mp)) {
3948                 cache_fpl_aborted(fpl);
3949                 goto out;
3950         }
3951
3952         VNPASS(cache_fplookup_vnode_supported(fpl->dvp), fpl->dvp);
3953
3954         for (;;) {
3955                 error = cache_fplookup_parse(fpl);
3956                 if (__predict_false(error != 0)) {
3957                         break;
3958                 }
3959
3960                 VNPASS(cache_fplookup_vnode_supported(fpl->dvp), fpl->dvp);
3961
3962                 error = VOP_FPLOOKUP_VEXEC(fpl->dvp, cnp->cn_cred);
3963                 if (__predict_false(error != 0)) {
3964                         error = cache_fplookup_failed_vexec(fpl, error);
3965                         break;
3966                 }
3967
3968                 if (__predict_false(cache_fpl_isdotdot(cnp))) {
3969                         error = cache_fplookup_dotdot(fpl);
3970                         if (__predict_false(error != 0)) {
3971                                 break;
3972                         }
3973                 } else {
3974                         error = cache_fplookup_next(fpl);
3975                         if (__predict_false(error != 0)) {
3976                                 break;
3977                         }
3978
3979                         VNPASS(!seqc_in_modify(fpl->tvp_seqc), fpl->tvp);
3980
3981                         if (cache_fplookup_need_climb_mount(fpl)) {
3982                                 error = cache_fplookup_climb_mount(fpl);
3983                                 if (__predict_false(error != 0)) {
3984                                         break;
3985                                 }
3986                         }
3987                 }
3988
3989                 VNPASS(!seqc_in_modify(fpl->tvp_seqc), fpl->tvp);
3990
3991                 if (cache_fpl_islastcn(ndp)) {
3992                         error = cache_fplookup_final(fpl);
3993                         break;
3994                 }
3995
3996                 if (!vn_seqc_consistent(fpl->dvp, fpl->dvp_seqc)) {
3997                         error = cache_fpl_aborted(fpl);
3998                         break;
3999                 }
4000
4001                 fpl->dvp = fpl->tvp;
4002                 fpl->dvp_seqc = fpl->tvp_seqc;
4003
4004                 cache_fplookup_parse_advance(fpl);
4005                 cache_fpl_checkpoint(fpl, &fpl->snd);
4006         }
4007 out:
4008         switch (fpl->status) {
4009         case CACHE_FPL_STATUS_UNSET:
4010                 __assert_unreachable();
4011                 break;
4012         case CACHE_FPL_STATUS_PARTIAL:
4013                 cache_fpl_smr_assert_entered(fpl);
4014                 return (cache_fplookup_partial_setup(fpl));
4015         case CACHE_FPL_STATUS_ABORTED:
4016                 if (fpl->in_smr)
4017                         cache_fpl_smr_exit(fpl);
4018                 return (CACHE_FPL_FAILED);
4019         case CACHE_FPL_STATUS_HANDLED:
4020                 MPASS(error != CACHE_FPL_FAILED);
4021                 cache_fpl_smr_assert_not_entered(fpl);
4022                 if (__predict_false(error != 0)) {
4023                         ndp->ni_dvp = NULL;
4024                         ndp->ni_vp = NULL;
4025                         cache_fpl_cleanup_cnp(cnp);
4026                         return (error);
4027                 }
4028                 ndp->ni_dvp = fpl->dvp;
4029                 ndp->ni_vp = fpl->tvp;
4030                 if (cnp->cn_flags & SAVENAME)
4031                         cnp->cn_flags |= HASBUF;
4032                 else
4033                         cache_fpl_cleanup_cnp(cnp);
4034                 return (error);
4035         }
4036 }
4037
4038 /*
4039  * Fast path lookup protected with SMR and sequence counters.
4040  *
4041  * Note: all VOP_FPLOOKUP_VEXEC routines have a comment referencing this one.
4042  *
4043  * Filesystems can opt in by setting the MNTK_FPLOOKUP flag and meeting criteria
4044  * outlined below.
4045  *
4046  * Traditional vnode lookup conceptually looks like this:
4047  *
4048  * vn_lock(current);
4049  * for (;;) {
4050  *      next = find();
4051  *      vn_lock(next);
4052  *      vn_unlock(current);
4053  *      current = next;
4054  *      if (last)
4055  *          break;
4056  * }
4057  * return (current);
4058  *
4059  * Each jump to the next vnode is safe memory-wise and atomic with respect to
4060  * any modifications thanks to holding respective locks.
4061  *
4062  * The same guarantee can be provided with a combination of safe memory
4063  * reclamation and sequence counters instead. If all operations which affect
4064  * the relationship between the current vnode and the one we are looking for
4065  * also modify the counter, we can verify whether all the conditions held as
4066  * we made the jump. This includes things like permissions, mount points etc.
4067  * Counter modification is provided by enclosing relevant places in
4068  * vn_seqc_write_begin()/end() calls.
4069  *
4070  * Thus this translates to:
4071  *
4072  * vfs_smr_enter();
4073  * dvp_seqc = seqc_read_any(dvp);
4074  * if (seqc_in_modify(dvp_seqc)) // someone is altering the vnode
4075  *     abort();
4076  * for (;;) {
4077  *      tvp = find();
4078  *      tvp_seqc = seqc_read_any(tvp);
4079  *      if (seqc_in_modify(tvp_seqc)) // someone is altering the target vnode
4080  *          abort();
4081  *      if (!seqc_consistent(dvp, dvp_seqc) // someone is altering the vnode
4082  *          abort();
4083  *      dvp = tvp; // we know nothing of importance has changed
4084  *      dvp_seqc = tvp_seqc; // store the counter for the tvp iteration
4085  *      if (last)
4086  *          break;
4087  * }
4088  * vget(); // secure the vnode
4089  * if (!seqc_consistent(tvp, tvp_seqc) // final check
4090  *          abort();
4091  * // at this point we know nothing has changed for any parent<->child pair
4092  * // as they were crossed during the lookup, meaning we matched the guarantee
4093  * // of the locked variant
4094  * return (tvp);
4095  *
4096  * The API contract for VOP_FPLOOKUP_VEXEC routines is as follows:
4097  * - they are called while within vfs_smr protection which they must never exit
4098  * - EAGAIN can be returned to denote checking could not be performed, it is
4099  *   always valid to return it
4100  * - if the sequence counter has not changed the result must be valid
4101  * - if the sequence counter has changed both false positives and false negatives
4102  *   are permitted (since the result will be rejected later)
4103  * - for simple cases of unix permission checks vaccess_vexec_smr can be used
4104  *
4105  * Caveats to watch out for:
4106  * - vnodes are passed unlocked and unreferenced with nothing stopping
4107  *   VOP_RECLAIM, in turn meaning that ->v_data can become NULL. It is advised
4108  *   to use atomic_load_ptr to fetch it.
4109  * - the aforementioned object can also get freed, meaning absent other means it
4110  *   should be protected with vfs_smr
4111  * - either safely checking permissions as they are modified or guaranteeing
4112  *   their stability is left to the routine
4113  */
4114 int
4115 cache_fplookup(struct nameidata *ndp, enum cache_fpl_status *status,
4116     struct pwd **pwdp)
4117 {
4118         struct cache_fpl fpl;
4119         struct pwd *pwd;
4120         struct vnode *dvp;
4121         struct componentname *cnp;
4122         struct nameidata_saved orig;
4123         int error;
4124
4125         MPASS(ndp->ni_lcf == 0);
4126
4127         fpl.status = CACHE_FPL_STATUS_UNSET;
4128         fpl.ndp = ndp;
4129         fpl.cnp = &ndp->ni_cnd;
4130         MPASS(curthread == fpl.cnp->cn_thread);
4131
4132         if (!cache_can_fplookup(&fpl)) {
4133                 SDT_PROBE3(vfs, fplookup, lookup, done, ndp, fpl.line, fpl.status);
4134                 *status = fpl.status;
4135                 return (EOPNOTSUPP);
4136         }
4137
4138         cache_fpl_checkpoint(&fpl, &orig);
4139
4140         cache_fpl_smr_enter_initial(&fpl);
4141         pwd = pwd_get_smr();
4142         fpl.pwd = pwd;
4143         ndp->ni_rootdir = pwd->pwd_rdir;
4144         ndp->ni_topdir = pwd->pwd_jdir;
4145
4146         cnp = fpl.cnp;
4147         cnp->cn_nameptr = cnp->cn_pnbuf;
4148         if (cnp->cn_pnbuf[0] == '/') {
4149                 cache_fpl_handle_root(ndp, &dvp);
4150         } else {
4151                 MPASS(ndp->ni_dirfd == AT_FDCWD);
4152                 dvp = pwd->pwd_cdir;
4153         }
4154
4155         SDT_PROBE4(vfs, namei, lookup, entry, dvp, cnp->cn_pnbuf, cnp->cn_flags, true);
4156
4157         error = cache_fplookup_impl(dvp, &fpl);
4158         cache_fpl_smr_assert_not_entered(&fpl);
4159         SDT_PROBE3(vfs, fplookup, lookup, done, ndp, fpl.line, fpl.status);
4160
4161         *status = fpl.status;
4162         switch (fpl.status) {
4163         case CACHE_FPL_STATUS_UNSET:
4164                 __assert_unreachable();
4165                 break;
4166         case CACHE_FPL_STATUS_HANDLED:
4167                 SDT_PROBE3(vfs, namei, lookup, return, error,
4168                     (error == 0 ? ndp->ni_vp : NULL), true);
4169                 break;
4170         case CACHE_FPL_STATUS_PARTIAL:
4171                 *pwdp = fpl.pwd;
4172                 /*
4173                  * Status restored by cache_fplookup_partial_setup.
4174                  */
4175                 break;
4176         case CACHE_FPL_STATUS_ABORTED:
4177                 cache_fpl_restore(&fpl, &orig);
4178                 break;
4179         }
4180         return (error);
4181 }