xref: /freebsd/sys/kern/vfs_cache.c (revision d0ba1baed3f6e4936a0c1b89c25f6c59168ef6de)
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_ktrace.h"
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/counter.h>
45 #include <sys/filedesc.h>
46 #include <sys/fnv_hash.h>
47 #include <sys/kernel.h>
48 #include <sys/lock.h>
49 #include <sys/malloc.h>
50 #include <sys/fcntl.h>
51 #include <sys/mount.h>
52 #include <sys/namei.h>
53 #include <sys/proc.h>
54 #include <sys/rwlock.h>
55 #include <sys/sdt.h>
56 #include <sys/smp.h>
57 #include <sys/syscallsubr.h>
58 #include <sys/sysctl.h>
59 #include <sys/sysproto.h>
60 #include <sys/vnode.h>
61 #ifdef KTRACE
62 #include <sys/ktrace.h>
63 #endif
64 
65 #include <vm/uma.h>
66 
67 SDT_PROVIDER_DECLARE(vfs);
68 SDT_PROBE_DEFINE3(vfs, namecache, enter, done, "struct vnode *", "char *",
69     "struct vnode *");
70 SDT_PROBE_DEFINE2(vfs, namecache, enter_negative, done, "struct vnode *",
71     "char *");
72 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, entry, "struct vnode *");
73 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, hit, "struct vnode *",
74     "char *", "struct vnode *");
75 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, miss, "struct vnode *");
76 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, return, "int",
77     "struct vnode *", "char *");
78 SDT_PROBE_DEFINE3(vfs, namecache, lookup, hit, "struct vnode *", "char *",
79     "struct vnode *");
80 SDT_PROBE_DEFINE2(vfs, namecache, lookup, hit__negative,
81     "struct vnode *", "char *");
82 SDT_PROBE_DEFINE2(vfs, namecache, lookup, miss, "struct vnode *",
83     "char *");
84 SDT_PROBE_DEFINE1(vfs, namecache, purge, done, "struct vnode *");
85 SDT_PROBE_DEFINE1(vfs, namecache, purge_negative, done, "struct vnode *");
86 SDT_PROBE_DEFINE1(vfs, namecache, purgevfs, done, "struct mount *");
87 SDT_PROBE_DEFINE3(vfs, namecache, zap, done, "struct vnode *", "char *",
88     "struct vnode *");
89 SDT_PROBE_DEFINE3(vfs, namecache, zap_negative, done, "struct vnode *",
90     "char *", "int");
91 SDT_PROBE_DEFINE3(vfs, namecache, shrink_negative, done, "struct vnode *",
92     "char *", "int");
93 
94 /*
95  * This structure describes the elements in the cache of recent
96  * names looked up by namei.
97  */
98 
99 struct	namecache {
100 	LIST_ENTRY(namecache) nc_hash;	/* hash chain */
101 	LIST_ENTRY(namecache) nc_src;	/* source vnode list */
102 	TAILQ_ENTRY(namecache) nc_dst;	/* destination vnode list */
103 	struct	vnode *nc_dvp;		/* vnode of parent of name */
104 	union {
105 		struct	vnode *nu_vp;	/* vnode the name refers to */
106 		u_int	nu_neghits;	/* negative entry hits */
107 	} n_un;
108 	u_char	nc_flag;		/* flag bits */
109 	u_char	nc_nlen;		/* length of name */
110 	char	nc_name[0];		/* segment name + nul */
111 };
112 
113 /*
114  * struct namecache_ts repeats struct namecache layout up to the
115  * nc_nlen member.
116  * struct namecache_ts is used in place of struct namecache when time(s) need
117  * to be stored.  The nc_dotdottime field is used when a cache entry is mapping
118  * both a non-dotdot directory name plus dotdot for the directory's
119  * parent.
120  */
121 struct	namecache_ts {
122 	struct	timespec nc_time;	/* timespec provided by fs */
123 	struct	timespec nc_dotdottime;	/* dotdot timespec provided by fs */
124 	int	nc_ticks;		/* ticks value when entry was added */
125 	struct namecache nc_nc;
126 };
127 
128 #define	nc_vp		n_un.nu_vp
129 #define	nc_neghits	n_un.nu_neghits
130 
131 /*
132  * Flags in namecache.nc_flag
133  */
134 #define NCF_WHITE	0x01
135 #define NCF_ISDOTDOT	0x02
136 #define	NCF_TS		0x04
137 #define	NCF_DTS		0x08
138 #define	NCF_DVDROP	0x10
139 #define	NCF_NEGATIVE	0x20
140 #define	NCF_HOTNEGATIVE	0x40
141 
142 /*
143  * Name caching works as follows:
144  *
145  * Names found by directory scans are retained in a cache
146  * for future reference.  It is managed LRU, so frequently
147  * used names will hang around.  Cache is indexed by hash value
148  * obtained from (vp, name) where vp refers to the directory
149  * containing name.
150  *
151  * If it is a "negative" entry, (i.e. for a name that is known NOT to
152  * exist) the vnode pointer will be NULL.
153  *
154  * Upon reaching the last segment of a path, if the reference
155  * is for DELETE, or NOCACHE is set (rewrite), and the
156  * name is located in the cache, it will be dropped.
157  *
158  * These locks are used (in the order in which they can be taken):
159  * NAME		TYPE	ROLE
160  * vnodelock	mtx	vnode lists and v_cache_dd field protection
161  * bucketlock	rwlock	for access to given set of hash buckets
162  * neglist	mtx	negative entry LRU management
163  *
164  * Additionally, ncneg_shrink_lock mtx is used to have at most one thread
165  * shrinking the LRU list.
166  *
167  * It is legal to take multiple vnodelock and bucketlock locks. The locking
168  * order is lower address first. Both are recursive.
169  *
170  * "." lookups are lockless.
171  *
172  * ".." and vnode -> name lookups require vnodelock.
173  *
174  * name -> vnode lookup requires the relevant bucketlock to be held for reading.
175  *
176  * Insertions and removals of entries require involved vnodes and bucketlocks
177  * to be write-locked to prevent other threads from seeing the entry.
178  *
179  * Some lookups result in removal of the found entry (e.g. getting rid of a
180  * negative entry with the intent to create a positive one), which poses a
181  * problem when multiple threads reach the state. Similarly, two different
182  * threads can purge two different vnodes and try to remove the same name.
183  *
184  * If the already held vnode lock is lower than the second required lock, we
185  * can just take the other lock. However, in the opposite case, this could
186  * deadlock. As such, this is resolved by trylocking and if that fails unlocking
187  * the first node, locking everything in order and revalidating the state.
188  */
189 
190 /*
191  * Structures associated with name caching.
192  */
193 #define NCHHASH(hash) \
194 	(&nchashtbl[(hash) & nchash])
195 static __read_mostly LIST_HEAD(nchashhead, namecache) *nchashtbl;/* Hash Table */
196 static u_long __read_mostly	nchash;			/* size of hash table */
197 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0,
198     "Size of namecache hash table");
199 static u_long __read_mostly	ncnegfactor = 12; /* ratio of negative entries */
200 SYSCTL_ULONG(_vfs, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0,
201     "Ratio of negative namecache entries");
202 static u_long __exclusive_cache_line	numneg;	/* number of negative entries allocated */
203 SYSCTL_ULONG(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0,
204     "Number of negative entries in namecache");
205 static u_long __exclusive_cache_line	numcache;/* number of cache entries allocated */
206 SYSCTL_ULONG(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0,
207     "Number of namecache entries");
208 static u_long __exclusive_cache_line	numcachehv;/* number of cache entries with vnodes held */
209 SYSCTL_ULONG(_debug, OID_AUTO, numcachehv, CTLFLAG_RD, &numcachehv, 0,
210     "Number of namecache entries with vnodes held");
211 u_int __read_mostly	ncsizefactor = 2;
212 SYSCTL_UINT(_vfs, OID_AUTO, ncsizefactor, CTLFLAG_RW, &ncsizefactor, 0,
213     "Size factor for namecache");
214 static u_int __read_mostly	ncpurgeminvnodes;
215 SYSCTL_UINT(_vfs, OID_AUTO, ncpurgeminvnodes, CTLFLAG_RW, &ncpurgeminvnodes, 0,
216     "Number of vnodes below which purgevfs ignores the request");
217 static u_int __read_mostly	ncneghitsrequeue = 8;
218 SYSCTL_UINT(_vfs, OID_AUTO, ncneghitsrequeue, CTLFLAG_RW, &ncneghitsrequeue, 0,
219     "Number of hits to requeue a negative entry in the LRU list");
220 
221 struct nchstats	nchstats;		/* cache effectiveness statistics */
222 
223 static struct mtx       ncneg_shrink_lock;
224 static int	shrink_list_turn;
225 
226 struct neglist {
227 	struct mtx		nl_lock;
228 	TAILQ_HEAD(, namecache) nl_list;
229 } __aligned(CACHE_LINE_SIZE);
230 
231 static struct neglist __read_mostly	*neglists;
232 static struct neglist ncneg_hot;
233 
234 #define	numneglists (ncneghash + 1)
235 static u_int __read_mostly	ncneghash;
236 static inline struct neglist *
237 NCP2NEGLIST(struct namecache *ncp)
238 {
239 
240 	return (&neglists[(((uintptr_t)(ncp) >> 8) & ncneghash)]);
241 }
242 
243 #define	numbucketlocks (ncbuckethash + 1)
244 static u_int __read_mostly  ncbuckethash;
245 static struct rwlock_padalign __read_mostly  *bucketlocks;
246 #define	HASH2BUCKETLOCK(hash) \
247 	((struct rwlock *)(&bucketlocks[((hash) & ncbuckethash)]))
248 
249 #define	numvnodelocks (ncvnodehash + 1)
250 static u_int __read_mostly  ncvnodehash;
251 static struct mtx __read_mostly *vnodelocks;
252 static inline struct mtx *
253 VP2VNODELOCK(struct vnode *vp)
254 {
255 
256 	return (&vnodelocks[(((uintptr_t)(vp) >> 8) & ncvnodehash)]);
257 }
258 
259 /*
260  * UMA zones for the VFS cache.
261  *
262  * The small cache is used for entries with short names, which are the
263  * most common.  The large cache is used for entries which are too big to
264  * fit in the small cache.
265  */
266 static uma_zone_t __read_mostly cache_zone_small;
267 static uma_zone_t __read_mostly cache_zone_small_ts;
268 static uma_zone_t __read_mostly cache_zone_large;
269 static uma_zone_t __read_mostly cache_zone_large_ts;
270 
271 #define	CACHE_PATH_CUTOFF	35
272 
273 static struct namecache *
274 cache_alloc(int len, int ts)
275 {
276 	struct namecache_ts *ncp_ts;
277 	struct namecache *ncp;
278 
279 	if (__predict_false(ts)) {
280 		if (len <= CACHE_PATH_CUTOFF)
281 			ncp_ts = uma_zalloc(cache_zone_small_ts, M_WAITOK);
282 		else
283 			ncp_ts = uma_zalloc(cache_zone_large_ts, M_WAITOK);
284 		ncp = &ncp_ts->nc_nc;
285 	} else {
286 		if (len <= CACHE_PATH_CUTOFF)
287 			ncp = uma_zalloc(cache_zone_small, M_WAITOK);
288 		else
289 			ncp = uma_zalloc(cache_zone_large, M_WAITOK);
290 	}
291 	return (ncp);
292 }
293 
294 static void
295 cache_free(struct namecache *ncp)
296 {
297 	struct namecache_ts *ncp_ts;
298 
299 	if (ncp == NULL)
300 		return;
301 	if ((ncp->nc_flag & NCF_DVDROP) != 0)
302 		vdrop(ncp->nc_dvp);
303 	if (__predict_false(ncp->nc_flag & NCF_TS)) {
304 		ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
305 		if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
306 			uma_zfree(cache_zone_small_ts, ncp_ts);
307 		else
308 			uma_zfree(cache_zone_large_ts, ncp_ts);
309 	} else {
310 		if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
311 			uma_zfree(cache_zone_small, ncp);
312 		else
313 			uma_zfree(cache_zone_large, ncp);
314 	}
315 }
316 
317 static void
318 cache_out_ts(struct namecache *ncp, struct timespec *tsp, int *ticksp)
319 {
320 	struct namecache_ts *ncp_ts;
321 
322 	KASSERT((ncp->nc_flag & NCF_TS) != 0 ||
323 	    (tsp == NULL && ticksp == NULL),
324 	    ("No NCF_TS"));
325 
326 	if (tsp == NULL && ticksp == NULL)
327 		return;
328 
329 	ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
330 	if (tsp != NULL)
331 		*tsp = ncp_ts->nc_time;
332 	if (ticksp != NULL)
333 		*ticksp = ncp_ts->nc_ticks;
334 }
335 
336 static int __read_mostly	doingcache = 1;	/* 1 => enable the cache */
337 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0,
338     "VFS namecache enabled");
339 
340 /* Export size information to userland */
341 SYSCTL_INT(_debug_sizeof, OID_AUTO, namecache, CTLFLAG_RD, SYSCTL_NULL_INT_PTR,
342     sizeof(struct namecache), "sizeof(struct namecache)");
343 
344 /*
345  * The new name cache statistics
346  */
347 static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0,
348     "Name cache statistics");
349 #define STATNODE_ULONG(name, descr)	\
350 	SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, descr);
351 #define STATNODE_COUNTER(name, descr)	\
352 	static counter_u64_t __read_mostly name; \
353 	SYSCTL_COUNTER_U64(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, descr);
354 STATNODE_ULONG(numneg, "Number of negative cache entries");
355 STATNODE_ULONG(numcache, "Number of cache entries");
356 STATNODE_COUNTER(numcalls, "Number of cache lookups");
357 STATNODE_COUNTER(dothits, "Number of '.' hits");
358 STATNODE_COUNTER(dotdothits, "Number of '..' hits");
359 STATNODE_COUNTER(numchecks, "Number of checks in lookup");
360 STATNODE_COUNTER(nummiss, "Number of cache misses");
361 STATNODE_COUNTER(nummisszap, "Number of cache misses we do not want to cache");
362 STATNODE_COUNTER(numposzaps,
363     "Number of cache hits (positive) we do not want to cache");
364 STATNODE_COUNTER(numposhits, "Number of cache hits (positive)");
365 STATNODE_COUNTER(numnegzaps,
366     "Number of cache hits (negative) we do not want to cache");
367 STATNODE_COUNTER(numneghits, "Number of cache hits (negative)");
368 /* These count for kern___getcwd(), too. */
369 STATNODE_COUNTER(numfullpathcalls, "Number of fullpath search calls");
370 STATNODE_COUNTER(numfullpathfail1, "Number of fullpath search errors (ENOTDIR)");
371 STATNODE_COUNTER(numfullpathfail2,
372     "Number of fullpath search errors (VOP_VPTOCNP failures)");
373 STATNODE_COUNTER(numfullpathfail4, "Number of fullpath search errors (ENOMEM)");
374 STATNODE_COUNTER(numfullpathfound, "Number of successful fullpath calls");
375 static long zap_and_exit_bucket_fail; STATNODE_ULONG(zap_and_exit_bucket_fail,
376     "Number of times zap_and_exit failed to lock");
377 static long cache_lock_vnodes_cel_3_failures;
378 STATNODE_ULONG(cache_lock_vnodes_cel_3_failures,
379     "Number of times 3-way vnode locking failed");
380 
381 static void cache_zap_locked(struct namecache *ncp, bool neg_locked);
382 static int vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
383     char *buf, char **retbuf, u_int buflen);
384 
385 static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
386 
387 static int cache_yield;
388 SYSCTL_INT(_vfs_cache, OID_AUTO, yield, CTLFLAG_RD, &cache_yield, 0,
389     "Number of times cache called yield");
390 
391 static void
392 cache_maybe_yield(void)
393 {
394 
395 	if (should_yield()) {
396 		cache_yield++;
397 		kern_yield(PRI_USER);
398 	}
399 }
400 
401 static inline void
402 cache_assert_vlp_locked(struct mtx *vlp)
403 {
404 
405 	if (vlp != NULL)
406 		mtx_assert(vlp, MA_OWNED);
407 }
408 
409 static inline void
410 cache_assert_vnode_locked(struct vnode *vp)
411 {
412 	struct mtx *vlp;
413 
414 	vlp = VP2VNODELOCK(vp);
415 	cache_assert_vlp_locked(vlp);
416 }
417 
418 static uint32_t
419 cache_get_hash(char *name, u_char len, struct vnode *dvp)
420 {
421 	uint32_t hash;
422 
423 	hash = fnv_32_buf(name, len, FNV1_32_INIT);
424 	hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
425 	return (hash);
426 }
427 
428 static inline struct rwlock *
429 NCP2BUCKETLOCK(struct namecache *ncp)
430 {
431 	uint32_t hash;
432 
433 	hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen, ncp->nc_dvp);
434 	return (HASH2BUCKETLOCK(hash));
435 }
436 
437 #ifdef INVARIANTS
438 static void
439 cache_assert_bucket_locked(struct namecache *ncp, int mode)
440 {
441 	struct rwlock *blp;
442 
443 	blp = NCP2BUCKETLOCK(ncp);
444 	rw_assert(blp, mode);
445 }
446 #else
447 #define cache_assert_bucket_locked(x, y) do { } while (0)
448 #endif
449 
450 #define cache_sort(x, y)	_cache_sort((void **)(x), (void **)(y))
451 static void
452 _cache_sort(void **p1, void **p2)
453 {
454 	void *tmp;
455 
456 	if (*p1 > *p2) {
457 		tmp = *p2;
458 		*p2 = *p1;
459 		*p1 = tmp;
460 	}
461 }
462 
463 static void
464 cache_lock_all_buckets(void)
465 {
466 	u_int i;
467 
468 	for (i = 0; i < numbucketlocks; i++)
469 		rw_wlock(&bucketlocks[i]);
470 }
471 
472 static void
473 cache_unlock_all_buckets(void)
474 {
475 	u_int i;
476 
477 	for (i = 0; i < numbucketlocks; i++)
478 		rw_wunlock(&bucketlocks[i]);
479 }
480 
481 static void
482 cache_lock_all_vnodes(void)
483 {
484 	u_int i;
485 
486 	for (i = 0; i < numvnodelocks; i++)
487 		mtx_lock(&vnodelocks[i]);
488 }
489 
490 static void
491 cache_unlock_all_vnodes(void)
492 {
493 	u_int i;
494 
495 	for (i = 0; i < numvnodelocks; i++)
496 		mtx_unlock(&vnodelocks[i]);
497 }
498 
499 static int
500 cache_trylock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
501 {
502 
503 	cache_sort(&vlp1, &vlp2);
504 	MPASS(vlp2 != NULL);
505 
506 	if (vlp1 != NULL) {
507 		if (!mtx_trylock(vlp1))
508 			return (EAGAIN);
509 	}
510 	if (!mtx_trylock(vlp2)) {
511 		if (vlp1 != NULL)
512 			mtx_unlock(vlp1);
513 		return (EAGAIN);
514 	}
515 
516 	return (0);
517 }
518 
519 static void
520 cache_unlock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
521 {
522 
523 	MPASS(vlp1 != NULL || vlp2 != NULL);
524 
525 	if (vlp1 != NULL)
526 		mtx_unlock(vlp1);
527 	if (vlp2 != NULL)
528 		mtx_unlock(vlp2);
529 }
530 
531 static int
532 sysctl_nchstats(SYSCTL_HANDLER_ARGS)
533 {
534 	struct nchstats snap;
535 
536 	if (req->oldptr == NULL)
537 		return (SYSCTL_OUT(req, 0, sizeof(snap)));
538 
539 	snap = nchstats;
540 	snap.ncs_goodhits = counter_u64_fetch(numposhits);
541 	snap.ncs_neghits = counter_u64_fetch(numneghits);
542 	snap.ncs_badhits = counter_u64_fetch(numposzaps) +
543 	    counter_u64_fetch(numnegzaps);
544 	snap.ncs_miss = counter_u64_fetch(nummisszap) +
545 	    counter_u64_fetch(nummiss);
546 
547 	return (SYSCTL_OUT(req, &snap, sizeof(snap)));
548 }
549 SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE | CTLFLAG_RD |
550     CTLFLAG_MPSAFE, 0, 0, sysctl_nchstats, "LU",
551     "VFS cache effectiveness statistics");
552 
553 #ifdef DIAGNOSTIC
554 /*
555  * Grab an atomic snapshot of the name cache hash chain lengths
556  */
557 static SYSCTL_NODE(_debug, OID_AUTO, hashstat, CTLFLAG_RW, NULL,
558     "hash table stats");
559 
560 static int
561 sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS)
562 {
563 	struct nchashhead *ncpp;
564 	struct namecache *ncp;
565 	int i, error, n_nchash, *cntbuf;
566 
567 retry:
568 	n_nchash = nchash + 1;	/* nchash is max index, not count */
569 	if (req->oldptr == NULL)
570 		return SYSCTL_OUT(req, 0, n_nchash * sizeof(int));
571 	cntbuf = malloc(n_nchash * sizeof(int), M_TEMP, M_ZERO | M_WAITOK);
572 	cache_lock_all_buckets();
573 	if (n_nchash != nchash + 1) {
574 		cache_unlock_all_buckets();
575 		free(cntbuf, M_TEMP);
576 		goto retry;
577 	}
578 	/* Scan hash tables counting entries */
579 	for (ncpp = nchashtbl, i = 0; i < n_nchash; ncpp++, i++)
580 		LIST_FOREACH(ncp, ncpp, nc_hash)
581 			cntbuf[i]++;
582 	cache_unlock_all_buckets();
583 	for (error = 0, i = 0; i < n_nchash; i++)
584 		if ((error = SYSCTL_OUT(req, &cntbuf[i], sizeof(int))) != 0)
585 			break;
586 	free(cntbuf, M_TEMP);
587 	return (error);
588 }
589 SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD|
590     CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_rawnchash, "S,int",
591     "nchash chain lengths");
592 
593 static int
594 sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS)
595 {
596 	int error;
597 	struct nchashhead *ncpp;
598 	struct namecache *ncp;
599 	int n_nchash;
600 	int count, maxlength, used, pct;
601 
602 	if (!req->oldptr)
603 		return SYSCTL_OUT(req, 0, 4 * sizeof(int));
604 
605 	cache_lock_all_buckets();
606 	n_nchash = nchash + 1;	/* nchash is max index, not count */
607 	used = 0;
608 	maxlength = 0;
609 
610 	/* Scan hash tables for applicable entries */
611 	for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
612 		count = 0;
613 		LIST_FOREACH(ncp, ncpp, nc_hash) {
614 			count++;
615 		}
616 		if (count)
617 			used++;
618 		if (maxlength < count)
619 			maxlength = count;
620 	}
621 	n_nchash = nchash + 1;
622 	cache_unlock_all_buckets();
623 	pct = (used * 100) / (n_nchash / 100);
624 	error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash));
625 	if (error)
626 		return (error);
627 	error = SYSCTL_OUT(req, &used, sizeof(used));
628 	if (error)
629 		return (error);
630 	error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength));
631 	if (error)
632 		return (error);
633 	error = SYSCTL_OUT(req, &pct, sizeof(pct));
634 	if (error)
635 		return (error);
636 	return (0);
637 }
638 SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD|
639     CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_nchash, "I",
640     "nchash statistics (number of total/used buckets, maximum chain length, usage percentage)");
641 #endif
642 
643 /*
644  * Negative entries management
645  *
646  * A variation of LRU scheme is used. New entries are hashed into one of
647  * numneglists cold lists. Entries get promoted to the hot list on first hit.
648  * Partial LRU for the hot list is maintained by requeueing them every
649  * ncneghitsrequeue hits.
650  *
651  * The shrinker will demote hot list head and evict from the cold list in a
652  * round-robin manner.
653  */
654 static void
655 cache_negative_hit(struct namecache *ncp)
656 {
657 	struct neglist *neglist;
658 	u_int hits;
659 
660 	MPASS(ncp->nc_flag & NCF_NEGATIVE);
661 	hits = atomic_fetchadd_int(&ncp->nc_neghits, 1);
662 	if (ncp->nc_flag & NCF_HOTNEGATIVE) {
663 		if ((hits % ncneghitsrequeue) != 0)
664 			return;
665 		mtx_lock(&ncneg_hot.nl_lock);
666 		if (ncp->nc_flag & NCF_HOTNEGATIVE) {
667 			TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
668 			TAILQ_INSERT_TAIL(&ncneg_hot.nl_list, ncp, nc_dst);
669 			mtx_unlock(&ncneg_hot.nl_lock);
670 			return;
671 		}
672 		/*
673 		 * The shrinker cleared the flag and removed the entry from
674 		 * the hot list. Put it back.
675 		 */
676 	} else {
677 		mtx_lock(&ncneg_hot.nl_lock);
678 	}
679 	neglist = NCP2NEGLIST(ncp);
680 	mtx_lock(&neglist->nl_lock);
681 	if (!(ncp->nc_flag & NCF_HOTNEGATIVE)) {
682 		TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
683 		TAILQ_INSERT_TAIL(&ncneg_hot.nl_list, ncp, nc_dst);
684 		ncp->nc_flag |= NCF_HOTNEGATIVE;
685 	}
686 	mtx_unlock(&neglist->nl_lock);
687 	mtx_unlock(&ncneg_hot.nl_lock);
688 }
689 
690 static void
691 cache_negative_insert(struct namecache *ncp, bool neg_locked)
692 {
693 	struct neglist *neglist;
694 
695 	MPASS(ncp->nc_flag & NCF_NEGATIVE);
696 	cache_assert_bucket_locked(ncp, RA_WLOCKED);
697 	neglist = NCP2NEGLIST(ncp);
698 	if (!neg_locked) {
699 		mtx_lock(&neglist->nl_lock);
700 	} else {
701 		mtx_assert(&neglist->nl_lock, MA_OWNED);
702 	}
703 	TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst);
704 	if (!neg_locked)
705 		mtx_unlock(&neglist->nl_lock);
706 	atomic_add_rel_long(&numneg, 1);
707 }
708 
709 static void
710 cache_negative_remove(struct namecache *ncp, bool neg_locked)
711 {
712 	struct neglist *neglist;
713 	bool hot_locked = false;
714 	bool list_locked = false;
715 
716 	MPASS(ncp->nc_flag & NCF_NEGATIVE);
717 	cache_assert_bucket_locked(ncp, RA_WLOCKED);
718 	neglist = NCP2NEGLIST(ncp);
719 	if (!neg_locked) {
720 		if (ncp->nc_flag & NCF_HOTNEGATIVE) {
721 			hot_locked = true;
722 			mtx_lock(&ncneg_hot.nl_lock);
723 			if (!(ncp->nc_flag & NCF_HOTNEGATIVE)) {
724 				list_locked = true;
725 				mtx_lock(&neglist->nl_lock);
726 			}
727 		} else {
728 			list_locked = true;
729 			mtx_lock(&neglist->nl_lock);
730 		}
731 	}
732 	if (ncp->nc_flag & NCF_HOTNEGATIVE) {
733 		mtx_assert(&ncneg_hot.nl_lock, MA_OWNED);
734 		TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
735 	} else {
736 		mtx_assert(&neglist->nl_lock, MA_OWNED);
737 		TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
738 	}
739 	if (list_locked)
740 		mtx_unlock(&neglist->nl_lock);
741 	if (hot_locked)
742 		mtx_unlock(&ncneg_hot.nl_lock);
743 	atomic_subtract_rel_long(&numneg, 1);
744 }
745 
746 static void
747 cache_negative_shrink_select(int start, struct namecache **ncpp,
748     struct neglist **neglistpp)
749 {
750 	struct neglist *neglist;
751 	struct namecache *ncp;
752 	int i;
753 
754 	*ncpp = ncp = NULL;
755 	neglist = NULL;
756 
757 	for (i = start; i < numneglists; i++) {
758 		neglist = &neglists[i];
759 		if (TAILQ_FIRST(&neglist->nl_list) == NULL)
760 			continue;
761 		mtx_lock(&neglist->nl_lock);
762 		ncp = TAILQ_FIRST(&neglist->nl_list);
763 		if (ncp != NULL)
764 			break;
765 		mtx_unlock(&neglist->nl_lock);
766 	}
767 
768 	*neglistpp = neglist;
769 	*ncpp = ncp;
770 }
771 
772 static void
773 cache_negative_zap_one(void)
774 {
775 	struct namecache *ncp, *ncp2;
776 	struct neglist *neglist;
777 	struct mtx *dvlp;
778 	struct rwlock *blp;
779 
780 	if (!mtx_trylock(&ncneg_shrink_lock))
781 		return;
782 
783 	mtx_lock(&ncneg_hot.nl_lock);
784 	ncp = TAILQ_FIRST(&ncneg_hot.nl_list);
785 	if (ncp != NULL) {
786 		neglist = NCP2NEGLIST(ncp);
787 		mtx_lock(&neglist->nl_lock);
788 		TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
789 		TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst);
790 		ncp->nc_flag &= ~NCF_HOTNEGATIVE;
791 		mtx_unlock(&neglist->nl_lock);
792 	}
793 
794 	cache_negative_shrink_select(shrink_list_turn, &ncp, &neglist);
795 	shrink_list_turn++;
796 	if (shrink_list_turn == numneglists)
797 		shrink_list_turn = 0;
798 	if (ncp == NULL && shrink_list_turn == 0)
799 		cache_negative_shrink_select(shrink_list_turn, &ncp, &neglist);
800 	if (ncp == NULL) {
801 		mtx_unlock(&ncneg_hot.nl_lock);
802 		goto out;
803 	}
804 
805 	MPASS(ncp->nc_flag & NCF_NEGATIVE);
806 	dvlp = VP2VNODELOCK(ncp->nc_dvp);
807 	blp = NCP2BUCKETLOCK(ncp);
808 	mtx_unlock(&neglist->nl_lock);
809 	mtx_unlock(&ncneg_hot.nl_lock);
810 	mtx_lock(dvlp);
811 	rw_wlock(blp);
812 	mtx_lock(&neglist->nl_lock);
813 	ncp2 = TAILQ_FIRST(&neglist->nl_list);
814 	if (ncp != ncp2 || dvlp != VP2VNODELOCK(ncp2->nc_dvp) ||
815 	    blp != NCP2BUCKETLOCK(ncp2) || !(ncp2->nc_flag & NCF_NEGATIVE)) {
816 		ncp = NULL;
817 		goto out_unlock_all;
818 	}
819 	SDT_PROBE3(vfs, namecache, shrink_negative, done, ncp->nc_dvp,
820 	    ncp->nc_name, ncp->nc_neghits);
821 
822 	cache_zap_locked(ncp, true);
823 out_unlock_all:
824 	mtx_unlock(&neglist->nl_lock);
825 	rw_wunlock(blp);
826 	mtx_unlock(dvlp);
827 out:
828 	mtx_unlock(&ncneg_shrink_lock);
829 	cache_free(ncp);
830 }
831 
832 /*
833  * cache_zap_locked():
834  *
835  *   Removes a namecache entry from cache, whether it contains an actual
836  *   pointer to a vnode or if it is just a negative cache entry.
837  */
838 static void
839 cache_zap_locked(struct namecache *ncp, bool neg_locked)
840 {
841 
842 	if (!(ncp->nc_flag & NCF_NEGATIVE))
843 		cache_assert_vnode_locked(ncp->nc_vp);
844 	cache_assert_vnode_locked(ncp->nc_dvp);
845 	cache_assert_bucket_locked(ncp, RA_WLOCKED);
846 
847 	CTR2(KTR_VFS, "cache_zap(%p) vp %p", ncp,
848 	    (ncp->nc_flag & NCF_NEGATIVE) ? NULL : ncp->nc_vp);
849 	if (!(ncp->nc_flag & NCF_NEGATIVE)) {
850 		SDT_PROBE3(vfs, namecache, zap, done, ncp->nc_dvp,
851 		    ncp->nc_name, ncp->nc_vp);
852 	} else {
853 		SDT_PROBE3(vfs, namecache, zap_negative, done, ncp->nc_dvp,
854 		    ncp->nc_name, ncp->nc_neghits);
855 	}
856 	LIST_REMOVE(ncp, nc_hash);
857 	if (!(ncp->nc_flag & NCF_NEGATIVE)) {
858 		TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
859 		if (ncp == ncp->nc_vp->v_cache_dd)
860 			ncp->nc_vp->v_cache_dd = NULL;
861 	} else {
862 		cache_negative_remove(ncp, neg_locked);
863 	}
864 	if (ncp->nc_flag & NCF_ISDOTDOT) {
865 		if (ncp == ncp->nc_dvp->v_cache_dd)
866 			ncp->nc_dvp->v_cache_dd = NULL;
867 	} else {
868 		LIST_REMOVE(ncp, nc_src);
869 		if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) {
870 			ncp->nc_flag |= NCF_DVDROP;
871 			atomic_subtract_rel_long(&numcachehv, 1);
872 		}
873 	}
874 	atomic_subtract_rel_long(&numcache, 1);
875 }
876 
877 static void
878 cache_zap_negative_locked_vnode_kl(struct namecache *ncp, struct vnode *vp)
879 {
880 	struct rwlock *blp;
881 
882 	MPASS(ncp->nc_dvp == vp);
883 	MPASS(ncp->nc_flag & NCF_NEGATIVE);
884 	cache_assert_vnode_locked(vp);
885 
886 	blp = NCP2BUCKETLOCK(ncp);
887 	rw_wlock(blp);
888 	cache_zap_locked(ncp, false);
889 	rw_wunlock(blp);
890 }
891 
892 static bool
893 cache_zap_locked_vnode_kl2(struct namecache *ncp, struct vnode *vp,
894     struct mtx **vlpp)
895 {
896 	struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
897 	struct rwlock *blp;
898 
899 	MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
900 	cache_assert_vnode_locked(vp);
901 
902 	if (ncp->nc_flag & NCF_NEGATIVE) {
903 		if (*vlpp != NULL) {
904 			mtx_unlock(*vlpp);
905 			*vlpp = NULL;
906 		}
907 		cache_zap_negative_locked_vnode_kl(ncp, vp);
908 		return (true);
909 	}
910 
911 	pvlp = VP2VNODELOCK(vp);
912 	blp = NCP2BUCKETLOCK(ncp);
913 	vlp1 = VP2VNODELOCK(ncp->nc_dvp);
914 	vlp2 = VP2VNODELOCK(ncp->nc_vp);
915 
916 	if (*vlpp == vlp1 || *vlpp == vlp2) {
917 		to_unlock = *vlpp;
918 		*vlpp = NULL;
919 	} else {
920 		if (*vlpp != NULL) {
921 			mtx_unlock(*vlpp);
922 			*vlpp = NULL;
923 		}
924 		cache_sort(&vlp1, &vlp2);
925 		if (vlp1 == pvlp) {
926 			mtx_lock(vlp2);
927 			to_unlock = vlp2;
928 		} else {
929 			if (!mtx_trylock(vlp1))
930 				goto out_relock;
931 			to_unlock = vlp1;
932 		}
933 	}
934 	rw_wlock(blp);
935 	cache_zap_locked(ncp, false);
936 	rw_wunlock(blp);
937 	if (to_unlock != NULL)
938 		mtx_unlock(to_unlock);
939 	return (true);
940 
941 out_relock:
942 	mtx_unlock(vlp2);
943 	mtx_lock(vlp1);
944 	mtx_lock(vlp2);
945 	MPASS(*vlpp == NULL);
946 	*vlpp = vlp1;
947 	return (false);
948 }
949 
950 static int
951 cache_zap_locked_vnode(struct namecache *ncp, struct vnode *vp)
952 {
953 	struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
954 	struct rwlock *blp;
955 	int error = 0;
956 
957 	MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
958 	cache_assert_vnode_locked(vp);
959 
960 	pvlp = VP2VNODELOCK(vp);
961 	if (ncp->nc_flag & NCF_NEGATIVE) {
962 		cache_zap_negative_locked_vnode_kl(ncp, vp);
963 		goto out;
964 	}
965 
966 	blp = NCP2BUCKETLOCK(ncp);
967 	vlp1 = VP2VNODELOCK(ncp->nc_dvp);
968 	vlp2 = VP2VNODELOCK(ncp->nc_vp);
969 	cache_sort(&vlp1, &vlp2);
970 	if (vlp1 == pvlp) {
971 		mtx_lock(vlp2);
972 		to_unlock = vlp2;
973 	} else {
974 		if (!mtx_trylock(vlp1)) {
975 			error = EAGAIN;
976 			goto out;
977 		}
978 		to_unlock = vlp1;
979 	}
980 	rw_wlock(blp);
981 	cache_zap_locked(ncp, false);
982 	rw_wunlock(blp);
983 	mtx_unlock(to_unlock);
984 out:
985 	mtx_unlock(pvlp);
986 	return (error);
987 }
988 
989 static int
990 cache_zap_wlocked_bucket(struct namecache *ncp, struct rwlock *blp)
991 {
992 	struct mtx *dvlp, *vlp;
993 
994 	cache_assert_bucket_locked(ncp, RA_WLOCKED);
995 
996 	dvlp = VP2VNODELOCK(ncp->nc_dvp);
997 	vlp = NULL;
998 	if (!(ncp->nc_flag & NCF_NEGATIVE))
999 		vlp = VP2VNODELOCK(ncp->nc_vp);
1000 	if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1001 		cache_zap_locked(ncp, false);
1002 		rw_wunlock(blp);
1003 		cache_unlock_vnodes(dvlp, vlp);
1004 		return (0);
1005 	}
1006 
1007 	rw_wunlock(blp);
1008 	return (EAGAIN);
1009 }
1010 
1011 static int
1012 cache_zap_rlocked_bucket(struct namecache *ncp, struct rwlock *blp)
1013 {
1014 	struct mtx *dvlp, *vlp;
1015 
1016 	cache_assert_bucket_locked(ncp, RA_RLOCKED);
1017 
1018 	dvlp = VP2VNODELOCK(ncp->nc_dvp);
1019 	vlp = NULL;
1020 	if (!(ncp->nc_flag & NCF_NEGATIVE))
1021 		vlp = VP2VNODELOCK(ncp->nc_vp);
1022 	if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1023 		rw_runlock(blp);
1024 		rw_wlock(blp);
1025 		cache_zap_locked(ncp, false);
1026 		rw_wunlock(blp);
1027 		cache_unlock_vnodes(dvlp, vlp);
1028 		return (0);
1029 	}
1030 
1031 	rw_runlock(blp);
1032 	return (EAGAIN);
1033 }
1034 
1035 static int
1036 cache_zap_wlocked_bucket_kl(struct namecache *ncp, struct rwlock *blp,
1037     struct mtx **vlpp1, struct mtx **vlpp2)
1038 {
1039 	struct mtx *dvlp, *vlp;
1040 
1041 	cache_assert_bucket_locked(ncp, RA_WLOCKED);
1042 
1043 	dvlp = VP2VNODELOCK(ncp->nc_dvp);
1044 	vlp = NULL;
1045 	if (!(ncp->nc_flag & NCF_NEGATIVE))
1046 		vlp = VP2VNODELOCK(ncp->nc_vp);
1047 	cache_sort(&dvlp, &vlp);
1048 
1049 	if (*vlpp1 == dvlp && *vlpp2 == vlp) {
1050 		cache_zap_locked(ncp, false);
1051 		cache_unlock_vnodes(dvlp, vlp);
1052 		*vlpp1 = NULL;
1053 		*vlpp2 = NULL;
1054 		return (0);
1055 	}
1056 
1057 	if (*vlpp1 != NULL)
1058 		mtx_unlock(*vlpp1);
1059 	if (*vlpp2 != NULL)
1060 		mtx_unlock(*vlpp2);
1061 	*vlpp1 = NULL;
1062 	*vlpp2 = NULL;
1063 
1064 	if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1065 		cache_zap_locked(ncp, false);
1066 		cache_unlock_vnodes(dvlp, vlp);
1067 		return (0);
1068 	}
1069 
1070 	rw_wunlock(blp);
1071 	*vlpp1 = dvlp;
1072 	*vlpp2 = vlp;
1073 	if (*vlpp1 != NULL)
1074 		mtx_lock(*vlpp1);
1075 	mtx_lock(*vlpp2);
1076 	rw_wlock(blp);
1077 	return (EAGAIN);
1078 }
1079 
1080 static void
1081 cache_lookup_unlock(struct rwlock *blp, struct mtx *vlp)
1082 {
1083 
1084 	if (blp != NULL) {
1085 		rw_runlock(blp);
1086 	} else {
1087 		mtx_unlock(vlp);
1088 	}
1089 }
1090 
1091 static int __noinline
1092 cache_lookup_dot(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1093     struct timespec *tsp, int *ticksp)
1094 {
1095 	int ltype;
1096 
1097 	*vpp = dvp;
1098 	CTR2(KTR_VFS, "cache_lookup(%p, %s) found via .",
1099 			dvp, cnp->cn_nameptr);
1100 	counter_u64_add(dothits, 1);
1101 	SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", *vpp);
1102 	if (tsp != NULL)
1103 		timespecclear(tsp);
1104 	if (ticksp != NULL)
1105 		*ticksp = ticks;
1106 	vrefact(*vpp);
1107 	/*
1108 	 * When we lookup "." we still can be asked to lock it
1109 	 * differently...
1110 	 */
1111 	ltype = cnp->cn_lkflags & LK_TYPE_MASK;
1112 	if (ltype != VOP_ISLOCKED(*vpp)) {
1113 		if (ltype == LK_EXCLUSIVE) {
1114 			vn_lock(*vpp, LK_UPGRADE | LK_RETRY);
1115 			if ((*vpp)->v_iflag & VI_DOOMED) {
1116 				/* forced unmount */
1117 				vrele(*vpp);
1118 				*vpp = NULL;
1119 				return (ENOENT);
1120 			}
1121 		} else
1122 			vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY);
1123 	}
1124 	return (-1);
1125 }
1126 
1127 /*
1128  * Lookup an entry in the cache
1129  *
1130  * Lookup is called with dvp pointing to the directory to search,
1131  * cnp pointing to the name of the entry being sought. If the lookup
1132  * succeeds, the vnode is returned in *vpp, and a status of -1 is
1133  * returned. If the lookup determines that the name does not exist
1134  * (negative caching), a status of ENOENT is returned. If the lookup
1135  * fails, a status of zero is returned.  If the directory vnode is
1136  * recycled out from under us due to a forced unmount, a status of
1137  * ENOENT is returned.
1138  *
1139  * vpp is locked and ref'd on return.  If we're looking up DOTDOT, dvp is
1140  * unlocked.  If we're looking up . an extra ref is taken, but the lock is
1141  * not recursively acquired.
1142  */
1143 
1144 static __noinline int
1145 cache_lookup_nomakeentry(struct vnode *dvp, struct vnode **vpp,
1146     struct componentname *cnp, struct timespec *tsp, int *ticksp)
1147 {
1148 	struct namecache *ncp;
1149 	struct rwlock *blp;
1150 	struct mtx *dvlp, *dvlp2;
1151 	uint32_t hash;
1152 	int error;
1153 
1154 	if (cnp->cn_namelen == 2 &&
1155 	    cnp->cn_nameptr[0] == '.' && cnp->cn_nameptr[1] == '.') {
1156 		counter_u64_add(dotdothits, 1);
1157 		dvlp = VP2VNODELOCK(dvp);
1158 		dvlp2 = NULL;
1159 		mtx_lock(dvlp);
1160 retry_dotdot:
1161 		ncp = dvp->v_cache_dd;
1162 		if (ncp == NULL) {
1163 			SDT_PROBE3(vfs, namecache, lookup, miss, dvp,
1164 			    "..", NULL);
1165 			mtx_unlock(dvlp);
1166 			if (dvlp2 != NULL)
1167 				mtx_unlock(dvlp2);
1168 			return (0);
1169 		}
1170 		if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1171 			if (ncp->nc_dvp != dvp)
1172 				panic("dvp %p v_cache_dd %p\n", dvp, ncp);
1173 			if (!cache_zap_locked_vnode_kl2(ncp,
1174 			    dvp, &dvlp2))
1175 				goto retry_dotdot;
1176 			MPASS(dvp->v_cache_dd == NULL);
1177 			mtx_unlock(dvlp);
1178 			if (dvlp2 != NULL)
1179 				mtx_unlock(dvlp2);
1180 			cache_free(ncp);
1181 		} else {
1182 			dvp->v_cache_dd = NULL;
1183 			mtx_unlock(dvlp);
1184 			if (dvlp2 != NULL)
1185 				mtx_unlock(dvlp2);
1186 		}
1187 		return (0);
1188 	}
1189 
1190 	hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1191 	blp = HASH2BUCKETLOCK(hash);
1192 retry:
1193 	if (LIST_EMPTY(NCHHASH(hash)))
1194 		goto out_no_entry;
1195 
1196 	rw_wlock(blp);
1197 
1198 	LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1199 		counter_u64_add(numchecks, 1);
1200 		if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1201 		    !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1202 			break;
1203 	}
1204 
1205 	/* We failed to find an entry */
1206 	if (ncp == NULL) {
1207 		rw_wunlock(blp);
1208 		goto out_no_entry;
1209 	}
1210 
1211 	counter_u64_add(numposzaps, 1);
1212 
1213 	error = cache_zap_wlocked_bucket(ncp, blp);
1214 	if (error != 0) {
1215 		zap_and_exit_bucket_fail++;
1216 		cache_maybe_yield();
1217 		goto retry;
1218 	}
1219 	cache_free(ncp);
1220 	return (0);
1221 out_no_entry:
1222 	SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr, NULL);
1223 	counter_u64_add(nummisszap, 1);
1224 	return (0);
1225 }
1226 
1227 int
1228 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1229     struct timespec *tsp, int *ticksp)
1230 {
1231 	struct namecache_ts *ncp_ts;
1232 	struct namecache *ncp;
1233 	struct rwlock *blp;
1234 	struct mtx *dvlp;
1235 	uint32_t hash;
1236 	int error, ltype;
1237 
1238 	if (__predict_false(!doingcache)) {
1239 		cnp->cn_flags &= ~MAKEENTRY;
1240 		return (0);
1241 	}
1242 
1243 	counter_u64_add(numcalls, 1);
1244 
1245 	if (__predict_false(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.'))
1246 		return (cache_lookup_dot(dvp, vpp, cnp, tsp, ticksp));
1247 
1248 	if ((cnp->cn_flags & MAKEENTRY) == 0)
1249 		return (cache_lookup_nomakeentry(dvp, vpp, cnp, tsp, ticksp));
1250 
1251 retry:
1252 	blp = NULL;
1253 	dvlp = NULL;
1254 	error = 0;
1255 	if (cnp->cn_namelen == 2 &&
1256 	    cnp->cn_nameptr[0] == '.' && cnp->cn_nameptr[1] == '.') {
1257 		counter_u64_add(dotdothits, 1);
1258 		dvlp = VP2VNODELOCK(dvp);
1259 		mtx_lock(dvlp);
1260 		ncp = dvp->v_cache_dd;
1261 		if (ncp == NULL) {
1262 			SDT_PROBE3(vfs, namecache, lookup, miss, dvp,
1263 			    "..", NULL);
1264 			mtx_unlock(dvlp);
1265 			return (0);
1266 		}
1267 		if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1268 			if (ncp->nc_flag & NCF_NEGATIVE)
1269 				*vpp = NULL;
1270 			else
1271 				*vpp = ncp->nc_vp;
1272 		} else
1273 			*vpp = ncp->nc_dvp;
1274 		/* Return failure if negative entry was found. */
1275 		if (*vpp == NULL)
1276 			goto negative_success;
1277 		CTR3(KTR_VFS, "cache_lookup(%p, %s) found %p via ..",
1278 		    dvp, cnp->cn_nameptr, *vpp);
1279 		SDT_PROBE3(vfs, namecache, lookup, hit, dvp, "..",
1280 		    *vpp);
1281 		cache_out_ts(ncp, tsp, ticksp);
1282 		if ((ncp->nc_flag & (NCF_ISDOTDOT | NCF_DTS)) ==
1283 		    NCF_DTS && tsp != NULL) {
1284 			ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1285 			*tsp = ncp_ts->nc_dotdottime;
1286 		}
1287 		goto success;
1288 	}
1289 
1290 	hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1291 	blp = HASH2BUCKETLOCK(hash);
1292 	rw_rlock(blp);
1293 
1294 	LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1295 		counter_u64_add(numchecks, 1);
1296 		if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1297 		    !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1298 			break;
1299 	}
1300 
1301 	/* We failed to find an entry */
1302 	if (ncp == NULL) {
1303 		rw_runlock(blp);
1304 		SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
1305 		    NULL);
1306 		counter_u64_add(nummiss, 1);
1307 		return (0);
1308 	}
1309 
1310 	/* We found a "positive" match, return the vnode */
1311 	if (!(ncp->nc_flag & NCF_NEGATIVE)) {
1312 		counter_u64_add(numposhits, 1);
1313 		*vpp = ncp->nc_vp;
1314 		CTR4(KTR_VFS, "cache_lookup(%p, %s) found %p via ncp %p",
1315 		    dvp, cnp->cn_nameptr, *vpp, ncp);
1316 		SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name,
1317 		    *vpp);
1318 		cache_out_ts(ncp, tsp, ticksp);
1319 		goto success;
1320 	}
1321 
1322 negative_success:
1323 	/* We found a negative match, and want to create it, so purge */
1324 	if (cnp->cn_nameiop == CREATE) {
1325 		counter_u64_add(numnegzaps, 1);
1326 		goto zap_and_exit;
1327 	}
1328 
1329 	counter_u64_add(numneghits, 1);
1330 	cache_negative_hit(ncp);
1331 	if (ncp->nc_flag & NCF_WHITE)
1332 		cnp->cn_flags |= ISWHITEOUT;
1333 	SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp,
1334 	    ncp->nc_name);
1335 	cache_out_ts(ncp, tsp, ticksp);
1336 	cache_lookup_unlock(blp, dvlp);
1337 	return (ENOENT);
1338 
1339 success:
1340 	/*
1341 	 * On success we return a locked and ref'd vnode as per the lookup
1342 	 * protocol.
1343 	 */
1344 	MPASS(dvp != *vpp);
1345 	ltype = 0;	/* silence gcc warning */
1346 	if (cnp->cn_flags & ISDOTDOT) {
1347 		ltype = VOP_ISLOCKED(dvp);
1348 		VOP_UNLOCK(dvp, 0);
1349 	}
1350 	vhold(*vpp);
1351 	cache_lookup_unlock(blp, dvlp);
1352 	error = vget(*vpp, cnp->cn_lkflags | LK_VNHELD, cnp->cn_thread);
1353 	if (cnp->cn_flags & ISDOTDOT) {
1354 		vn_lock(dvp, ltype | LK_RETRY);
1355 		if (dvp->v_iflag & VI_DOOMED) {
1356 			if (error == 0)
1357 				vput(*vpp);
1358 			*vpp = NULL;
1359 			return (ENOENT);
1360 		}
1361 	}
1362 	if (error) {
1363 		*vpp = NULL;
1364 		goto retry;
1365 	}
1366 	if ((cnp->cn_flags & ISLASTCN) &&
1367 	    (cnp->cn_lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE) {
1368 		ASSERT_VOP_ELOCKED(*vpp, "cache_lookup");
1369 	}
1370 	return (-1);
1371 
1372 zap_and_exit:
1373 	if (blp != NULL)
1374 		error = cache_zap_rlocked_bucket(ncp, blp);
1375 	else
1376 		error = cache_zap_locked_vnode(ncp, dvp);
1377 	if (error != 0) {
1378 		zap_and_exit_bucket_fail++;
1379 		cache_maybe_yield();
1380 		goto retry;
1381 	}
1382 	cache_free(ncp);
1383 	return (0);
1384 }
1385 
1386 struct celockstate {
1387 	struct mtx *vlp[3];
1388 	struct rwlock *blp[2];
1389 };
1390 CTASSERT((nitems(((struct celockstate *)0)->vlp) == 3));
1391 CTASSERT((nitems(((struct celockstate *)0)->blp) == 2));
1392 
1393 static inline void
1394 cache_celockstate_init(struct celockstate *cel)
1395 {
1396 
1397 	bzero(cel, sizeof(*cel));
1398 }
1399 
1400 static void
1401 cache_lock_vnodes_cel(struct celockstate *cel, struct vnode *vp,
1402     struct vnode *dvp)
1403 {
1404 	struct mtx *vlp1, *vlp2;
1405 
1406 	MPASS(cel->vlp[0] == NULL);
1407 	MPASS(cel->vlp[1] == NULL);
1408 	MPASS(cel->vlp[2] == NULL);
1409 
1410 	MPASS(vp != NULL || dvp != NULL);
1411 
1412 	vlp1 = VP2VNODELOCK(vp);
1413 	vlp2 = VP2VNODELOCK(dvp);
1414 	cache_sort(&vlp1, &vlp2);
1415 
1416 	if (vlp1 != NULL) {
1417 		mtx_lock(vlp1);
1418 		cel->vlp[0] = vlp1;
1419 	}
1420 	mtx_lock(vlp2);
1421 	cel->vlp[1] = vlp2;
1422 }
1423 
1424 static void
1425 cache_unlock_vnodes_cel(struct celockstate *cel)
1426 {
1427 
1428 	MPASS(cel->vlp[0] != NULL || cel->vlp[1] != NULL);
1429 
1430 	if (cel->vlp[0] != NULL)
1431 		mtx_unlock(cel->vlp[0]);
1432 	if (cel->vlp[1] != NULL)
1433 		mtx_unlock(cel->vlp[1]);
1434 	if (cel->vlp[2] != NULL)
1435 		mtx_unlock(cel->vlp[2]);
1436 }
1437 
1438 static bool
1439 cache_lock_vnodes_cel_3(struct celockstate *cel, struct vnode *vp)
1440 {
1441 	struct mtx *vlp;
1442 	bool ret;
1443 
1444 	cache_assert_vlp_locked(cel->vlp[0]);
1445 	cache_assert_vlp_locked(cel->vlp[1]);
1446 	MPASS(cel->vlp[2] == NULL);
1447 
1448 	MPASS(vp != NULL);
1449 	vlp = VP2VNODELOCK(vp);
1450 
1451 	ret = true;
1452 	if (vlp >= cel->vlp[1]) {
1453 		mtx_lock(vlp);
1454 	} else {
1455 		if (mtx_trylock(vlp))
1456 			goto out;
1457 		cache_lock_vnodes_cel_3_failures++;
1458 		cache_unlock_vnodes_cel(cel);
1459 		if (vlp < cel->vlp[0]) {
1460 			mtx_lock(vlp);
1461 			mtx_lock(cel->vlp[0]);
1462 			mtx_lock(cel->vlp[1]);
1463 		} else {
1464 			if (cel->vlp[0] != NULL)
1465 				mtx_lock(cel->vlp[0]);
1466 			mtx_lock(vlp);
1467 			mtx_lock(cel->vlp[1]);
1468 		}
1469 		ret = false;
1470 	}
1471 out:
1472 	cel->vlp[2] = vlp;
1473 	return (ret);
1474 }
1475 
1476 static void
1477 cache_lock_buckets_cel(struct celockstate *cel, struct rwlock *blp1,
1478     struct rwlock *blp2)
1479 {
1480 
1481 	MPASS(cel->blp[0] == NULL);
1482 	MPASS(cel->blp[1] == NULL);
1483 
1484 	cache_sort(&blp1, &blp2);
1485 
1486 	if (blp1 != NULL) {
1487 		rw_wlock(blp1);
1488 		cel->blp[0] = blp1;
1489 	}
1490 	rw_wlock(blp2);
1491 	cel->blp[1] = blp2;
1492 }
1493 
1494 static void
1495 cache_unlock_buckets_cel(struct celockstate *cel)
1496 {
1497 
1498 	if (cel->blp[0] != NULL)
1499 		rw_wunlock(cel->blp[0]);
1500 	rw_wunlock(cel->blp[1]);
1501 }
1502 
1503 /*
1504  * Lock part of the cache affected by the insertion.
1505  *
1506  * This means vnodelocks for dvp, vp and the relevant bucketlock.
1507  * However, insertion can result in removal of an old entry. In this
1508  * case we have an additional vnode and bucketlock pair to lock. If the
1509  * entry is negative, ncelock is locked instead of the vnode.
1510  *
1511  * That is, in the worst case we have to lock 3 vnodes and 2 bucketlocks, while
1512  * preserving the locking order (smaller address first).
1513  */
1514 static void
1515 cache_enter_lock(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1516     uint32_t hash)
1517 {
1518 	struct namecache *ncp;
1519 	struct rwlock *blps[2];
1520 
1521 	blps[0] = HASH2BUCKETLOCK(hash);
1522 	for (;;) {
1523 		blps[1] = NULL;
1524 		cache_lock_vnodes_cel(cel, dvp, vp);
1525 		if (vp == NULL || vp->v_type != VDIR)
1526 			break;
1527 		ncp = vp->v_cache_dd;
1528 		if (ncp == NULL)
1529 			break;
1530 		if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1531 			break;
1532 		MPASS(ncp->nc_dvp == vp);
1533 		blps[1] = NCP2BUCKETLOCK(ncp);
1534 		if (ncp->nc_flag & NCF_NEGATIVE)
1535 			break;
1536 		if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1537 			break;
1538 		/*
1539 		 * All vnodes got re-locked. Re-validate the state and if
1540 		 * nothing changed we are done. Otherwise restart.
1541 		 */
1542 		if (ncp == vp->v_cache_dd &&
1543 		    (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1544 		    blps[1] == NCP2BUCKETLOCK(ncp) &&
1545 		    VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1546 			break;
1547 		cache_unlock_vnodes_cel(cel);
1548 		cel->vlp[0] = NULL;
1549 		cel->vlp[1] = NULL;
1550 		cel->vlp[2] = NULL;
1551 	}
1552 	cache_lock_buckets_cel(cel, blps[0], blps[1]);
1553 }
1554 
1555 static void
1556 cache_enter_lock_dd(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1557     uint32_t hash)
1558 {
1559 	struct namecache *ncp;
1560 	struct rwlock *blps[2];
1561 
1562 	blps[0] = HASH2BUCKETLOCK(hash);
1563 	for (;;) {
1564 		blps[1] = NULL;
1565 		cache_lock_vnodes_cel(cel, dvp, vp);
1566 		ncp = dvp->v_cache_dd;
1567 		if (ncp == NULL)
1568 			break;
1569 		if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1570 			break;
1571 		MPASS(ncp->nc_dvp == dvp);
1572 		blps[1] = NCP2BUCKETLOCK(ncp);
1573 		if (ncp->nc_flag & NCF_NEGATIVE)
1574 			break;
1575 		if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1576 			break;
1577 		if (ncp == dvp->v_cache_dd &&
1578 		    (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1579 		    blps[1] == NCP2BUCKETLOCK(ncp) &&
1580 		    VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1581 			break;
1582 		cache_unlock_vnodes_cel(cel);
1583 		cel->vlp[0] = NULL;
1584 		cel->vlp[1] = NULL;
1585 		cel->vlp[2] = NULL;
1586 	}
1587 	cache_lock_buckets_cel(cel, blps[0], blps[1]);
1588 }
1589 
1590 static void
1591 cache_enter_unlock(struct celockstate *cel)
1592 {
1593 
1594 	cache_unlock_buckets_cel(cel);
1595 	cache_unlock_vnodes_cel(cel);
1596 }
1597 
1598 /*
1599  * Add an entry to the cache.
1600  */
1601 void
1602 cache_enter_time(struct vnode *dvp, struct vnode *vp, struct componentname *cnp,
1603     struct timespec *tsp, struct timespec *dtsp)
1604 {
1605 	struct celockstate cel;
1606 	struct namecache *ncp, *n2, *ndd;
1607 	struct namecache_ts *ncp_ts, *n2_ts;
1608 	struct nchashhead *ncpp;
1609 	struct neglist *neglist;
1610 	uint32_t hash;
1611 	int flag;
1612 	int len;
1613 	bool neg_locked;
1614 	int lnumcache;
1615 
1616 	CTR3(KTR_VFS, "cache_enter(%p, %p, %s)", dvp, vp, cnp->cn_nameptr);
1617 	VNASSERT(vp == NULL || (vp->v_iflag & VI_DOOMED) == 0, vp,
1618 	    ("cache_enter: Adding a doomed vnode"));
1619 	VNASSERT(dvp == NULL || (dvp->v_iflag & VI_DOOMED) == 0, dvp,
1620 	    ("cache_enter: Doomed vnode used as src"));
1621 
1622 	if (__predict_false(!doingcache))
1623 		return;
1624 
1625 	/*
1626 	 * Avoid blowout in namecache entries.
1627 	 */
1628 	if (__predict_false(numcache >= desiredvnodes * ncsizefactor))
1629 		return;
1630 
1631 	cache_celockstate_init(&cel);
1632 	ndd = NULL;
1633 	ncp_ts = NULL;
1634 	flag = 0;
1635 	if (cnp->cn_nameptr[0] == '.') {
1636 		if (cnp->cn_namelen == 1)
1637 			return;
1638 		if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
1639 			len = cnp->cn_namelen;
1640 			hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1641 			cache_enter_lock_dd(&cel, dvp, vp, hash);
1642 			/*
1643 			 * If dotdot entry already exists, just retarget it
1644 			 * to new parent vnode, otherwise continue with new
1645 			 * namecache entry allocation.
1646 			 */
1647 			if ((ncp = dvp->v_cache_dd) != NULL &&
1648 			    ncp->nc_flag & NCF_ISDOTDOT) {
1649 				KASSERT(ncp->nc_dvp == dvp,
1650 				    ("wrong isdotdot parent"));
1651 				neg_locked = false;
1652 				if (ncp->nc_flag & NCF_NEGATIVE || vp == NULL) {
1653 					neglist = NCP2NEGLIST(ncp);
1654 					mtx_lock(&ncneg_hot.nl_lock);
1655 					mtx_lock(&neglist->nl_lock);
1656 					neg_locked = true;
1657 				}
1658 				if (!(ncp->nc_flag & NCF_NEGATIVE)) {
1659 					TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst,
1660 					    ncp, nc_dst);
1661 				} else {
1662 					cache_negative_remove(ncp, true);
1663 				}
1664 				if (vp != NULL) {
1665 					TAILQ_INSERT_HEAD(&vp->v_cache_dst,
1666 					    ncp, nc_dst);
1667 					ncp->nc_flag &= ~(NCF_NEGATIVE|NCF_HOTNEGATIVE);
1668 				} else {
1669 					ncp->nc_flag &= ~(NCF_HOTNEGATIVE);
1670 					ncp->nc_flag |= NCF_NEGATIVE;
1671 					cache_negative_insert(ncp, true);
1672 				}
1673 				if (neg_locked) {
1674 					mtx_unlock(&neglist->nl_lock);
1675 					mtx_unlock(&ncneg_hot.nl_lock);
1676 				}
1677 				ncp->nc_vp = vp;
1678 				cache_enter_unlock(&cel);
1679 				return;
1680 			}
1681 			dvp->v_cache_dd = NULL;
1682 			cache_enter_unlock(&cel);
1683 			cache_celockstate_init(&cel);
1684 			SDT_PROBE3(vfs, namecache, enter, done, dvp, "..", vp);
1685 			flag = NCF_ISDOTDOT;
1686 		}
1687 	}
1688 
1689 	/*
1690 	 * Calculate the hash key and setup as much of the new
1691 	 * namecache entry as possible before acquiring the lock.
1692 	 */
1693 	ncp = cache_alloc(cnp->cn_namelen, tsp != NULL);
1694 	ncp->nc_flag = flag;
1695 	ncp->nc_vp = vp;
1696 	if (vp == NULL)
1697 		ncp->nc_flag |= NCF_NEGATIVE;
1698 	ncp->nc_dvp = dvp;
1699 	if (tsp != NULL) {
1700 		ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1701 		ncp_ts->nc_time = *tsp;
1702 		ncp_ts->nc_ticks = ticks;
1703 		ncp_ts->nc_nc.nc_flag |= NCF_TS;
1704 		if (dtsp != NULL) {
1705 			ncp_ts->nc_dotdottime = *dtsp;
1706 			ncp_ts->nc_nc.nc_flag |= NCF_DTS;
1707 		}
1708 	}
1709 	len = ncp->nc_nlen = cnp->cn_namelen;
1710 	hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1711 	strlcpy(ncp->nc_name, cnp->cn_nameptr, len + 1);
1712 	cache_enter_lock(&cel, dvp, vp, hash);
1713 
1714 	/*
1715 	 * See if this vnode or negative entry is already in the cache
1716 	 * with this name.  This can happen with concurrent lookups of
1717 	 * the same path name.
1718 	 */
1719 	ncpp = NCHHASH(hash);
1720 	LIST_FOREACH(n2, ncpp, nc_hash) {
1721 		if (n2->nc_dvp == dvp &&
1722 		    n2->nc_nlen == cnp->cn_namelen &&
1723 		    !bcmp(n2->nc_name, cnp->cn_nameptr, n2->nc_nlen)) {
1724 			if (tsp != NULL) {
1725 				KASSERT((n2->nc_flag & NCF_TS) != 0,
1726 				    ("no NCF_TS"));
1727 				n2_ts = __containerof(n2, struct namecache_ts, nc_nc);
1728 				n2_ts->nc_time = ncp_ts->nc_time;
1729 				n2_ts->nc_ticks = ncp_ts->nc_ticks;
1730 				if (dtsp != NULL) {
1731 					n2_ts->nc_dotdottime = ncp_ts->nc_dotdottime;
1732 					if (ncp->nc_flag & NCF_NEGATIVE)
1733 						mtx_lock(&ncneg_hot.nl_lock);
1734 					n2_ts->nc_nc.nc_flag |= NCF_DTS;
1735 					if (ncp->nc_flag & NCF_NEGATIVE)
1736 						mtx_unlock(&ncneg_hot.nl_lock);
1737 				}
1738 			}
1739 			goto out_unlock_free;
1740 		}
1741 	}
1742 
1743 	if (flag == NCF_ISDOTDOT) {
1744 		/*
1745 		 * See if we are trying to add .. entry, but some other lookup
1746 		 * has populated v_cache_dd pointer already.
1747 		 */
1748 		if (dvp->v_cache_dd != NULL)
1749 			goto out_unlock_free;
1750 		KASSERT(vp == NULL || vp->v_type == VDIR,
1751 		    ("wrong vnode type %p", vp));
1752 		dvp->v_cache_dd = ncp;
1753 	}
1754 
1755 	if (vp != NULL) {
1756 		if (vp->v_type == VDIR) {
1757 			if (flag != NCF_ISDOTDOT) {
1758 				/*
1759 				 * For this case, the cache entry maps both the
1760 				 * directory name in it and the name ".." for the
1761 				 * directory's parent.
1762 				 */
1763 				if ((ndd = vp->v_cache_dd) != NULL) {
1764 					if ((ndd->nc_flag & NCF_ISDOTDOT) != 0)
1765 						cache_zap_locked(ndd, false);
1766 					else
1767 						ndd = NULL;
1768 				}
1769 				vp->v_cache_dd = ncp;
1770 			}
1771 		} else {
1772 			vp->v_cache_dd = NULL;
1773 		}
1774 	}
1775 
1776 	if (flag != NCF_ISDOTDOT) {
1777 		if (LIST_EMPTY(&dvp->v_cache_src)) {
1778 			vhold(dvp);
1779 			atomic_add_rel_long(&numcachehv, 1);
1780 		}
1781 		LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
1782 	}
1783 
1784 	/*
1785 	 * Insert the new namecache entry into the appropriate chain
1786 	 * within the cache entries table.
1787 	 */
1788 	LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
1789 
1790 	/*
1791 	 * If the entry is "negative", we place it into the
1792 	 * "negative" cache queue, otherwise, we place it into the
1793 	 * destination vnode's cache entries queue.
1794 	 */
1795 	if (vp != NULL) {
1796 		TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
1797 		SDT_PROBE3(vfs, namecache, enter, done, dvp, ncp->nc_name,
1798 		    vp);
1799 	} else {
1800 		if (cnp->cn_flags & ISWHITEOUT)
1801 			ncp->nc_flag |= NCF_WHITE;
1802 		cache_negative_insert(ncp, false);
1803 		SDT_PROBE2(vfs, namecache, enter_negative, done, dvp,
1804 		    ncp->nc_name);
1805 	}
1806 	cache_enter_unlock(&cel);
1807 	lnumcache = atomic_fetchadd_long(&numcache, 1) + 1;
1808 	if (numneg * ncnegfactor > lnumcache)
1809 		cache_negative_zap_one();
1810 	cache_free(ndd);
1811 	return;
1812 out_unlock_free:
1813 	cache_enter_unlock(&cel);
1814 	cache_free(ncp);
1815 	return;
1816 }
1817 
1818 static u_int
1819 cache_roundup_2(u_int val)
1820 {
1821 	u_int res;
1822 
1823 	for (res = 1; res <= val; res <<= 1)
1824 		continue;
1825 
1826 	return (res);
1827 }
1828 
1829 /*
1830  * Name cache initialization, from vfs_init() when we are booting
1831  */
1832 static void
1833 nchinit(void *dummy __unused)
1834 {
1835 	u_int i;
1836 
1837 	cache_zone_small = uma_zcreate("S VFS Cache",
1838 	    sizeof(struct namecache) + CACHE_PATH_CUTOFF + 1,
1839 	    NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache),
1840 	    UMA_ZONE_ZINIT);
1841 	cache_zone_small_ts = uma_zcreate("STS VFS Cache",
1842 	    sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1,
1843 	    NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache_ts),
1844 	    UMA_ZONE_ZINIT);
1845 	cache_zone_large = uma_zcreate("L VFS Cache",
1846 	    sizeof(struct namecache) + NAME_MAX + 1,
1847 	    NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache),
1848 	    UMA_ZONE_ZINIT);
1849 	cache_zone_large_ts = uma_zcreate("LTS VFS Cache",
1850 	    sizeof(struct namecache_ts) + NAME_MAX + 1,
1851 	    NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache_ts),
1852 	    UMA_ZONE_ZINIT);
1853 
1854 	nchashtbl = hashinit(desiredvnodes * 2, M_VFSCACHE, &nchash);
1855 	ncbuckethash = cache_roundup_2(mp_ncpus * 64) - 1;
1856 	if (ncbuckethash > nchash)
1857 		ncbuckethash = nchash;
1858 	bucketlocks = malloc(sizeof(*bucketlocks) * numbucketlocks, M_VFSCACHE,
1859 	    M_WAITOK | M_ZERO);
1860 	for (i = 0; i < numbucketlocks; i++)
1861 		rw_init_flags(&bucketlocks[i], "ncbuc", RW_DUPOK | RW_RECURSE);
1862 	ncvnodehash = cache_roundup_2(mp_ncpus * 64) - 1;
1863 	vnodelocks = malloc(sizeof(*vnodelocks) * numvnodelocks, M_VFSCACHE,
1864 	    M_WAITOK | M_ZERO);
1865 	for (i = 0; i < numvnodelocks; i++)
1866 		mtx_init(&vnodelocks[i], "ncvn", NULL, MTX_DUPOK | MTX_RECURSE);
1867 	ncpurgeminvnodes = numbucketlocks;
1868 
1869 	ncneghash = 3;
1870 	neglists = malloc(sizeof(*neglists) * numneglists, M_VFSCACHE,
1871 	    M_WAITOK | M_ZERO);
1872 	for (i = 0; i < numneglists; i++) {
1873 		mtx_init(&neglists[i].nl_lock, "ncnegl", NULL, MTX_DEF);
1874 		TAILQ_INIT(&neglists[i].nl_list);
1875 	}
1876 	mtx_init(&ncneg_hot.nl_lock, "ncneglh", NULL, MTX_DEF);
1877 	TAILQ_INIT(&ncneg_hot.nl_list);
1878 
1879 	mtx_init(&ncneg_shrink_lock, "ncnegs", NULL, MTX_DEF);
1880 
1881 	numcalls = counter_u64_alloc(M_WAITOK);
1882 	dothits = counter_u64_alloc(M_WAITOK);
1883 	dotdothits = counter_u64_alloc(M_WAITOK);
1884 	numchecks = counter_u64_alloc(M_WAITOK);
1885 	nummiss = counter_u64_alloc(M_WAITOK);
1886 	nummisszap = counter_u64_alloc(M_WAITOK);
1887 	numposzaps = counter_u64_alloc(M_WAITOK);
1888 	numposhits = counter_u64_alloc(M_WAITOK);
1889 	numnegzaps = counter_u64_alloc(M_WAITOK);
1890 	numneghits = counter_u64_alloc(M_WAITOK);
1891 	numfullpathcalls = counter_u64_alloc(M_WAITOK);
1892 	numfullpathfail1 = counter_u64_alloc(M_WAITOK);
1893 	numfullpathfail2 = counter_u64_alloc(M_WAITOK);
1894 	numfullpathfail4 = counter_u64_alloc(M_WAITOK);
1895 	numfullpathfound = counter_u64_alloc(M_WAITOK);
1896 }
1897 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL);
1898 
1899 void
1900 cache_changesize(int newmaxvnodes)
1901 {
1902 	struct nchashhead *new_nchashtbl, *old_nchashtbl;
1903 	u_long new_nchash, old_nchash;
1904 	struct namecache *ncp;
1905 	uint32_t hash;
1906 	int i;
1907 
1908 	newmaxvnodes = cache_roundup_2(newmaxvnodes * 2);
1909 	if (newmaxvnodes < numbucketlocks)
1910 		newmaxvnodes = numbucketlocks;
1911 
1912 	new_nchashtbl = hashinit(newmaxvnodes, M_VFSCACHE, &new_nchash);
1913 	/* If same hash table size, nothing to do */
1914 	if (nchash == new_nchash) {
1915 		free(new_nchashtbl, M_VFSCACHE);
1916 		return;
1917 	}
1918 	/*
1919 	 * Move everything from the old hash table to the new table.
1920 	 * None of the namecache entries in the table can be removed
1921 	 * because to do so, they have to be removed from the hash table.
1922 	 */
1923 	cache_lock_all_vnodes();
1924 	cache_lock_all_buckets();
1925 	old_nchashtbl = nchashtbl;
1926 	old_nchash = nchash;
1927 	nchashtbl = new_nchashtbl;
1928 	nchash = new_nchash;
1929 	for (i = 0; i <= old_nchash; i++) {
1930 		while ((ncp = LIST_FIRST(&old_nchashtbl[i])) != NULL) {
1931 			hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen,
1932 			    ncp->nc_dvp);
1933 			LIST_REMOVE(ncp, nc_hash);
1934 			LIST_INSERT_HEAD(NCHHASH(hash), ncp, nc_hash);
1935 		}
1936 	}
1937 	cache_unlock_all_buckets();
1938 	cache_unlock_all_vnodes();
1939 	free(old_nchashtbl, M_VFSCACHE);
1940 }
1941 
1942 /*
1943  * Invalidate all entries to a particular vnode.
1944  */
1945 void
1946 cache_purge(struct vnode *vp)
1947 {
1948 	TAILQ_HEAD(, namecache) ncps;
1949 	struct namecache *ncp, *nnp;
1950 	struct mtx *vlp, *vlp2;
1951 
1952 	CTR1(KTR_VFS, "cache_purge(%p)", vp);
1953 	SDT_PROBE1(vfs, namecache, purge, done, vp);
1954 	if (LIST_EMPTY(&vp->v_cache_src) && TAILQ_EMPTY(&vp->v_cache_dst) &&
1955 	    vp->v_cache_dd == NULL)
1956 		return;
1957 	TAILQ_INIT(&ncps);
1958 	vlp = VP2VNODELOCK(vp);
1959 	vlp2 = NULL;
1960 	mtx_lock(vlp);
1961 retry:
1962 	while (!LIST_EMPTY(&vp->v_cache_src)) {
1963 		ncp = LIST_FIRST(&vp->v_cache_src);
1964 		if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
1965 			goto retry;
1966 		TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
1967 	}
1968 	while (!TAILQ_EMPTY(&vp->v_cache_dst)) {
1969 		ncp = TAILQ_FIRST(&vp->v_cache_dst);
1970 		if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
1971 			goto retry;
1972 		TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
1973 	}
1974 	ncp = vp->v_cache_dd;
1975 	if (ncp != NULL) {
1976 		KASSERT(ncp->nc_flag & NCF_ISDOTDOT,
1977 		   ("lost dotdot link"));
1978 		if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
1979 			goto retry;
1980 		TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
1981 	}
1982 	KASSERT(vp->v_cache_dd == NULL, ("incomplete purge"));
1983 	mtx_unlock(vlp);
1984 	if (vlp2 != NULL)
1985 		mtx_unlock(vlp2);
1986 	TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
1987 		cache_free(ncp);
1988 	}
1989 }
1990 
1991 /*
1992  * Invalidate all negative entries for a particular directory vnode.
1993  */
1994 void
1995 cache_purge_negative(struct vnode *vp)
1996 {
1997 	TAILQ_HEAD(, namecache) ncps;
1998 	struct namecache *ncp, *nnp;
1999 	struct mtx *vlp;
2000 
2001 	CTR1(KTR_VFS, "cache_purge_negative(%p)", vp);
2002 	SDT_PROBE1(vfs, namecache, purge_negative, done, vp);
2003 	if (LIST_EMPTY(&vp->v_cache_src))
2004 		return;
2005 	TAILQ_INIT(&ncps);
2006 	vlp = VP2VNODELOCK(vp);
2007 	mtx_lock(vlp);
2008 	LIST_FOREACH_SAFE(ncp, &vp->v_cache_src, nc_src, nnp) {
2009 		if (!(ncp->nc_flag & NCF_NEGATIVE))
2010 			continue;
2011 		cache_zap_negative_locked_vnode_kl(ncp, vp);
2012 		TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2013 	}
2014 	mtx_unlock(vlp);
2015 	TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2016 		cache_free(ncp);
2017 	}
2018 }
2019 
2020 /*
2021  * Flush all entries referencing a particular filesystem.
2022  */
2023 void
2024 cache_purgevfs(struct mount *mp, bool force)
2025 {
2026 	TAILQ_HEAD(, namecache) ncps;
2027 	struct mtx *vlp1, *vlp2;
2028 	struct rwlock *blp;
2029 	struct nchashhead *bucket;
2030 	struct namecache *ncp, *nnp;
2031 	u_long i, j, n_nchash;
2032 	int error;
2033 
2034 	/* Scan hash tables for applicable entries */
2035 	SDT_PROBE1(vfs, namecache, purgevfs, done, mp);
2036 	if (!force && mp->mnt_nvnodelistsize <= ncpurgeminvnodes)
2037 		return;
2038 	TAILQ_INIT(&ncps);
2039 	n_nchash = nchash + 1;
2040 	vlp1 = vlp2 = NULL;
2041 	for (i = 0; i < numbucketlocks; i++) {
2042 		blp = (struct rwlock *)&bucketlocks[i];
2043 		rw_wlock(blp);
2044 		for (j = i; j < n_nchash; j += numbucketlocks) {
2045 retry:
2046 			bucket = &nchashtbl[j];
2047 			LIST_FOREACH_SAFE(ncp, bucket, nc_hash, nnp) {
2048 				cache_assert_bucket_locked(ncp, RA_WLOCKED);
2049 				if (ncp->nc_dvp->v_mount != mp)
2050 					continue;
2051 				error = cache_zap_wlocked_bucket_kl(ncp, blp,
2052 				    &vlp1, &vlp2);
2053 				if (error != 0)
2054 					goto retry;
2055 				TAILQ_INSERT_HEAD(&ncps, ncp, nc_dst);
2056 			}
2057 		}
2058 		rw_wunlock(blp);
2059 		if (vlp1 == NULL && vlp2 == NULL)
2060 			cache_maybe_yield();
2061 	}
2062 	if (vlp1 != NULL)
2063 		mtx_unlock(vlp1);
2064 	if (vlp2 != NULL)
2065 		mtx_unlock(vlp2);
2066 
2067 	TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2068 		cache_free(ncp);
2069 	}
2070 }
2071 
2072 /*
2073  * Perform canonical checks and cache lookup and pass on to filesystem
2074  * through the vop_cachedlookup only if needed.
2075  */
2076 
2077 int
2078 vfs_cache_lookup(struct vop_lookup_args *ap)
2079 {
2080 	struct vnode *dvp;
2081 	int error;
2082 	struct vnode **vpp = ap->a_vpp;
2083 	struct componentname *cnp = ap->a_cnp;
2084 	struct ucred *cred = cnp->cn_cred;
2085 	int flags = cnp->cn_flags;
2086 	struct thread *td = cnp->cn_thread;
2087 
2088 	*vpp = NULL;
2089 	dvp = ap->a_dvp;
2090 
2091 	if (dvp->v_type != VDIR)
2092 		return (ENOTDIR);
2093 
2094 	if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
2095 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
2096 		return (EROFS);
2097 
2098 	error = VOP_ACCESS(dvp, VEXEC, cred, td);
2099 	if (error)
2100 		return (error);
2101 
2102 	error = cache_lookup(dvp, vpp, cnp, NULL, NULL);
2103 	if (error == 0)
2104 		return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
2105 	if (error == -1)
2106 		return (0);
2107 	return (error);
2108 }
2109 
2110 /*
2111  * XXX All of these sysctls would probably be more productive dead.
2112  */
2113 static int __read_mostly disablecwd;
2114 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0,
2115    "Disable the getcwd syscall");
2116 
2117 /* Implementation of the getcwd syscall. */
2118 int
2119 sys___getcwd(struct thread *td, struct __getcwd_args *uap)
2120 {
2121 
2122 	return (kern___getcwd(td, uap->buf, UIO_USERSPACE, uap->buflen,
2123 	    MAXPATHLEN));
2124 }
2125 
2126 int
2127 kern___getcwd(struct thread *td, char *buf, enum uio_seg bufseg, size_t buflen,
2128     size_t path_max)
2129 {
2130 	char *bp, *tmpbuf;
2131 	struct filedesc *fdp;
2132 	struct vnode *cdir, *rdir;
2133 	int error;
2134 
2135 	if (__predict_false(disablecwd))
2136 		return (ENODEV);
2137 	if (__predict_false(buflen < 2))
2138 		return (EINVAL);
2139 	if (buflen > path_max)
2140 		buflen = path_max;
2141 
2142 	tmpbuf = malloc(buflen, M_TEMP, M_WAITOK);
2143 	fdp = td->td_proc->p_fd;
2144 	FILEDESC_SLOCK(fdp);
2145 	cdir = fdp->fd_cdir;
2146 	vrefact(cdir);
2147 	rdir = fdp->fd_rdir;
2148 	vrefact(rdir);
2149 	FILEDESC_SUNLOCK(fdp);
2150 	error = vn_fullpath1(td, cdir, rdir, tmpbuf, &bp, buflen);
2151 	vrele(rdir);
2152 	vrele(cdir);
2153 
2154 	if (!error) {
2155 		if (bufseg == UIO_SYSSPACE)
2156 			bcopy(bp, buf, strlen(bp) + 1);
2157 		else
2158 			error = copyout(bp, buf, strlen(bp) + 1);
2159 #ifdef KTRACE
2160 	if (KTRPOINT(curthread, KTR_NAMEI))
2161 		ktrnamei(bp);
2162 #endif
2163 	}
2164 	free(tmpbuf, M_TEMP);
2165 	return (error);
2166 }
2167 
2168 /*
2169  * Thus begins the fullpath magic.
2170  */
2171 
2172 static int __read_mostly disablefullpath;
2173 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW, &disablefullpath, 0,
2174     "Disable the vn_fullpath function");
2175 
2176 /*
2177  * Retrieve the full filesystem path that correspond to a vnode from the name
2178  * cache (if available)
2179  */
2180 int
2181 vn_fullpath(struct thread *td, struct vnode *vn, char **retbuf, char **freebuf)
2182 {
2183 	char *buf;
2184 	struct filedesc *fdp;
2185 	struct vnode *rdir;
2186 	int error;
2187 
2188 	if (__predict_false(disablefullpath))
2189 		return (ENODEV);
2190 	if (__predict_false(vn == NULL))
2191 		return (EINVAL);
2192 
2193 	buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
2194 	fdp = td->td_proc->p_fd;
2195 	FILEDESC_SLOCK(fdp);
2196 	rdir = fdp->fd_rdir;
2197 	vrefact(rdir);
2198 	FILEDESC_SUNLOCK(fdp);
2199 	error = vn_fullpath1(td, vn, rdir, buf, retbuf, MAXPATHLEN);
2200 	vrele(rdir);
2201 
2202 	if (!error)
2203 		*freebuf = buf;
2204 	else
2205 		free(buf, M_TEMP);
2206 	return (error);
2207 }
2208 
2209 /*
2210  * This function is similar to vn_fullpath, but it attempts to lookup the
2211  * pathname relative to the global root mount point.  This is required for the
2212  * auditing sub-system, as audited pathnames must be absolute, relative to the
2213  * global root mount point.
2214  */
2215 int
2216 vn_fullpath_global(struct thread *td, struct vnode *vn,
2217     char **retbuf, char **freebuf)
2218 {
2219 	char *buf;
2220 	int error;
2221 
2222 	if (__predict_false(disablefullpath))
2223 		return (ENODEV);
2224 	if (__predict_false(vn == NULL))
2225 		return (EINVAL);
2226 	buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
2227 	error = vn_fullpath1(td, vn, rootvnode, buf, retbuf, MAXPATHLEN);
2228 	if (!error)
2229 		*freebuf = buf;
2230 	else
2231 		free(buf, M_TEMP);
2232 	return (error);
2233 }
2234 
2235 int
2236 vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, u_int *buflen)
2237 {
2238 	struct vnode *dvp;
2239 	struct namecache *ncp;
2240 	struct mtx *vlp;
2241 	int error;
2242 
2243 	vlp = VP2VNODELOCK(*vp);
2244 	mtx_lock(vlp);
2245 	TAILQ_FOREACH(ncp, &((*vp)->v_cache_dst), nc_dst) {
2246 		if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2247 			break;
2248 	}
2249 	if (ncp != NULL) {
2250 		if (*buflen < ncp->nc_nlen) {
2251 			mtx_unlock(vlp);
2252 			vrele(*vp);
2253 			counter_u64_add(numfullpathfail4, 1);
2254 			error = ENOMEM;
2255 			SDT_PROBE3(vfs, namecache, fullpath, return, error,
2256 			    vp, NULL);
2257 			return (error);
2258 		}
2259 		*buflen -= ncp->nc_nlen;
2260 		memcpy(buf + *buflen, ncp->nc_name, ncp->nc_nlen);
2261 		SDT_PROBE3(vfs, namecache, fullpath, hit, ncp->nc_dvp,
2262 		    ncp->nc_name, vp);
2263 		dvp = *vp;
2264 		*vp = ncp->nc_dvp;
2265 		vref(*vp);
2266 		mtx_unlock(vlp);
2267 		vrele(dvp);
2268 		return (0);
2269 	}
2270 	SDT_PROBE1(vfs, namecache, fullpath, miss, vp);
2271 
2272 	mtx_unlock(vlp);
2273 	vn_lock(*vp, LK_SHARED | LK_RETRY);
2274 	error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen);
2275 	vput(*vp);
2276 	if (error) {
2277 		counter_u64_add(numfullpathfail2, 1);
2278 		SDT_PROBE3(vfs, namecache, fullpath, return,  error, vp, NULL);
2279 		return (error);
2280 	}
2281 
2282 	*vp = dvp;
2283 	if (dvp->v_iflag & VI_DOOMED) {
2284 		/* forced unmount */
2285 		vrele(dvp);
2286 		error = ENOENT;
2287 		SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2288 		return (error);
2289 	}
2290 	/*
2291 	 * *vp has its use count incremented still.
2292 	 */
2293 
2294 	return (0);
2295 }
2296 
2297 /*
2298  * The magic behind kern___getcwd() and vn_fullpath().
2299  */
2300 static int
2301 vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
2302     char *buf, char **retbuf, u_int buflen)
2303 {
2304 	int error, slash_prefixed;
2305 #ifdef KDTRACE_HOOKS
2306 	struct vnode *startvp = vp;
2307 #endif
2308 	struct vnode *vp1;
2309 
2310 	buflen--;
2311 	buf[buflen] = '\0';
2312 	error = 0;
2313 	slash_prefixed = 0;
2314 
2315 	SDT_PROBE1(vfs, namecache, fullpath, entry, vp);
2316 	counter_u64_add(numfullpathcalls, 1);
2317 	vref(vp);
2318 	if (vp->v_type != VDIR) {
2319 		error = vn_vptocnp(&vp, td->td_ucred, buf, &buflen);
2320 		if (error)
2321 			return (error);
2322 		if (buflen == 0) {
2323 			vrele(vp);
2324 			return (ENOMEM);
2325 		}
2326 		buf[--buflen] = '/';
2327 		slash_prefixed = 1;
2328 	}
2329 	while (vp != rdir && vp != rootvnode) {
2330 		/*
2331 		 * The vp vnode must be already fully constructed,
2332 		 * since it is either found in namecache or obtained
2333 		 * from VOP_VPTOCNP().  We may test for VV_ROOT safely
2334 		 * without obtaining the vnode lock.
2335 		 */
2336 		if ((vp->v_vflag & VV_ROOT) != 0) {
2337 			vn_lock(vp, LK_RETRY | LK_SHARED);
2338 
2339 			/*
2340 			 * With the vnode locked, check for races with
2341 			 * unmount, forced or not.  Note that we
2342 			 * already verified that vp is not equal to
2343 			 * the root vnode, which means that
2344 			 * mnt_vnodecovered can be NULL only for the
2345 			 * case of unmount.
2346 			 */
2347 			if ((vp->v_iflag & VI_DOOMED) != 0 ||
2348 			    (vp1 = vp->v_mount->mnt_vnodecovered) == NULL ||
2349 			    vp1->v_mountedhere != vp->v_mount) {
2350 				vput(vp);
2351 				error = ENOENT;
2352 				SDT_PROBE3(vfs, namecache, fullpath, return,
2353 				    error, vp, NULL);
2354 				break;
2355 			}
2356 
2357 			vref(vp1);
2358 			vput(vp);
2359 			vp = vp1;
2360 			continue;
2361 		}
2362 		if (vp->v_type != VDIR) {
2363 			vrele(vp);
2364 			counter_u64_add(numfullpathfail1, 1);
2365 			error = ENOTDIR;
2366 			SDT_PROBE3(vfs, namecache, fullpath, return,
2367 			    error, vp, NULL);
2368 			break;
2369 		}
2370 		error = vn_vptocnp(&vp, td->td_ucred, buf, &buflen);
2371 		if (error)
2372 			break;
2373 		if (buflen == 0) {
2374 			vrele(vp);
2375 			error = ENOMEM;
2376 			SDT_PROBE3(vfs, namecache, fullpath, return, error,
2377 			    startvp, NULL);
2378 			break;
2379 		}
2380 		buf[--buflen] = '/';
2381 		slash_prefixed = 1;
2382 	}
2383 	if (error)
2384 		return (error);
2385 	if (!slash_prefixed) {
2386 		if (buflen == 0) {
2387 			vrele(vp);
2388 			counter_u64_add(numfullpathfail4, 1);
2389 			SDT_PROBE3(vfs, namecache, fullpath, return, ENOMEM,
2390 			    startvp, NULL);
2391 			return (ENOMEM);
2392 		}
2393 		buf[--buflen] = '/';
2394 	}
2395 	counter_u64_add(numfullpathfound, 1);
2396 	vrele(vp);
2397 
2398 	SDT_PROBE3(vfs, namecache, fullpath, return, 0, startvp, buf + buflen);
2399 	*retbuf = buf + buflen;
2400 	return (0);
2401 }
2402 
2403 struct vnode *
2404 vn_dir_dd_ino(struct vnode *vp)
2405 {
2406 	struct namecache *ncp;
2407 	struct vnode *ddvp;
2408 	struct mtx *vlp;
2409 
2410 	ASSERT_VOP_LOCKED(vp, "vn_dir_dd_ino");
2411 	vlp = VP2VNODELOCK(vp);
2412 	mtx_lock(vlp);
2413 	TAILQ_FOREACH(ncp, &(vp->v_cache_dst), nc_dst) {
2414 		if ((ncp->nc_flag & NCF_ISDOTDOT) != 0)
2415 			continue;
2416 		ddvp = ncp->nc_dvp;
2417 		vhold(ddvp);
2418 		mtx_unlock(vlp);
2419 		if (vget(ddvp, LK_SHARED | LK_NOWAIT | LK_VNHELD, curthread))
2420 			return (NULL);
2421 		return (ddvp);
2422 	}
2423 	mtx_unlock(vlp);
2424 	return (NULL);
2425 }
2426 
2427 int
2428 vn_commname(struct vnode *vp, char *buf, u_int buflen)
2429 {
2430 	struct namecache *ncp;
2431 	struct mtx *vlp;
2432 	int l;
2433 
2434 	vlp = VP2VNODELOCK(vp);
2435 	mtx_lock(vlp);
2436 	TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst)
2437 		if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2438 			break;
2439 	if (ncp == NULL) {
2440 		mtx_unlock(vlp);
2441 		return (ENOENT);
2442 	}
2443 	l = min(ncp->nc_nlen, buflen - 1);
2444 	memcpy(buf, ncp->nc_name, l);
2445 	mtx_unlock(vlp);
2446 	buf[l] = '\0';
2447 	return (0);
2448 }
2449 
2450 /* ABI compat shims for old kernel modules. */
2451 #undef cache_enter
2452 
2453 void	cache_enter(struct vnode *dvp, struct vnode *vp,
2454 	    struct componentname *cnp);
2455 
2456 void
2457 cache_enter(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2458 {
2459 
2460 	cache_enter_time(dvp, vp, cnp, NULL, NULL);
2461 }
2462 
2463 /*
2464  * This function updates path string to vnode's full global path
2465  * and checks the size of the new path string against the pathlen argument.
2466  *
2467  * Requires a locked, referenced vnode.
2468  * Vnode is re-locked on success or ENODEV, otherwise unlocked.
2469  *
2470  * If sysctl debug.disablefullpath is set, ENODEV is returned,
2471  * vnode is left locked and path remain untouched.
2472  *
2473  * If vp is a directory, the call to vn_fullpath_global() always succeeds
2474  * because it falls back to the ".." lookup if the namecache lookup fails.
2475  */
2476 int
2477 vn_path_to_global_path(struct thread *td, struct vnode *vp, char *path,
2478     u_int pathlen)
2479 {
2480 	struct nameidata nd;
2481 	struct vnode *vp1;
2482 	char *rpath, *fbuf;
2483 	int error;
2484 
2485 	ASSERT_VOP_ELOCKED(vp, __func__);
2486 
2487 	/* Return ENODEV if sysctl debug.disablefullpath==1 */
2488 	if (__predict_false(disablefullpath))
2489 		return (ENODEV);
2490 
2491 	/* Construct global filesystem path from vp. */
2492 	VOP_UNLOCK(vp, 0);
2493 	error = vn_fullpath_global(td, vp, &rpath, &fbuf);
2494 
2495 	if (error != 0) {
2496 		vrele(vp);
2497 		return (error);
2498 	}
2499 
2500 	if (strlen(rpath) >= pathlen) {
2501 		vrele(vp);
2502 		error = ENAMETOOLONG;
2503 		goto out;
2504 	}
2505 
2506 	/*
2507 	 * Re-lookup the vnode by path to detect a possible rename.
2508 	 * As a side effect, the vnode is relocked.
2509 	 * If vnode was renamed, return ENOENT.
2510 	 */
2511 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
2512 	    UIO_SYSSPACE, path, td);
2513 	error = namei(&nd);
2514 	if (error != 0) {
2515 		vrele(vp);
2516 		goto out;
2517 	}
2518 	NDFREE(&nd, NDF_ONLY_PNBUF);
2519 	vp1 = nd.ni_vp;
2520 	vrele(vp);
2521 	if (vp1 == vp)
2522 		strcpy(path, rpath);
2523 	else {
2524 		vput(vp1);
2525 		error = ENOENT;
2526 	}
2527 
2528 out:
2529 	free(fbuf, M_TEMP);
2530 	return (error);
2531 }
2532