2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993, 1995
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Poul-Henning Kamp of the FreeBSD Project.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
34 * @(#)vfs_cache.c 8.5 (Berkeley) 3/22/95
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
41 #include "opt_ktrace.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/counter.h>
46 #include <sys/filedesc.h>
47 #include <sys/fnv_hash.h>
48 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #include <sys/fcntl.h>
53 #include <sys/mount.h>
54 #include <sys/namei.h>
56 #include <sys/rwlock.h>
59 #include <sys/syscallsubr.h>
60 #include <sys/sysctl.h>
61 #include <sys/sysproto.h>
62 #include <sys/vnode.h>
64 #include <sys/ktrace.h>
73 SDT_PROVIDER_DECLARE(vfs);
74 SDT_PROBE_DEFINE3(vfs, namecache, enter, done, "struct vnode *", "char *",
76 SDT_PROBE_DEFINE2(vfs, namecache, enter_negative, done, "struct vnode *",
78 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, entry, "struct vnode *");
79 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, hit, "struct vnode *",
80 "char *", "struct vnode *");
81 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, miss, "struct vnode *");
82 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, return, "int",
83 "struct vnode *", "char *");
84 SDT_PROBE_DEFINE3(vfs, namecache, lookup, hit, "struct vnode *", "char *",
86 SDT_PROBE_DEFINE2(vfs, namecache, lookup, hit__negative,
87 "struct vnode *", "char *");
88 SDT_PROBE_DEFINE2(vfs, namecache, lookup, miss, "struct vnode *",
90 SDT_PROBE_DEFINE1(vfs, namecache, purge, done, "struct vnode *");
91 SDT_PROBE_DEFINE1(vfs, namecache, purge_negative, done, "struct vnode *");
92 SDT_PROBE_DEFINE1(vfs, namecache, purgevfs, done, "struct mount *");
93 SDT_PROBE_DEFINE3(vfs, namecache, zap, done, "struct vnode *", "char *",
95 SDT_PROBE_DEFINE2(vfs, namecache, zap_negative, done, "struct vnode *",
97 SDT_PROBE_DEFINE2(vfs, namecache, shrink_negative, done, "struct vnode *",
101 * This structure describes the elements in the cache of recent
102 * names looked up by namei.
106 LIST_ENTRY(namecache) nc_hash; /* hash chain */
107 LIST_ENTRY(namecache) nc_src; /* source vnode list */
108 TAILQ_ENTRY(namecache) nc_dst; /* destination vnode list */
109 struct vnode *nc_dvp; /* vnode of parent of name */
111 struct vnode *nu_vp; /* vnode the name refers to */
113 u_char nc_flag; /* flag bits */
114 u_char nc_nlen; /* length of name */
115 char nc_name[0]; /* segment name + nul */
119 * struct namecache_ts repeats struct namecache layout up to the
121 * struct namecache_ts is used in place of struct namecache when time(s) need
122 * to be stored. The nc_dotdottime field is used when a cache entry is mapping
123 * both a non-dotdot directory name plus dotdot for the directory's
126 struct namecache_ts {
127 struct timespec nc_time; /* timespec provided by fs */
128 struct timespec nc_dotdottime; /* dotdot timespec provided by fs */
129 int nc_ticks; /* ticks value when entry was added */
130 struct namecache nc_nc;
133 #define nc_vp n_un.nu_vp
136 * Flags in namecache.nc_flag
138 #define NCF_WHITE 0x01
139 #define NCF_ISDOTDOT 0x02
142 #define NCF_DVDROP 0x10
143 #define NCF_NEGATIVE 0x20
144 #define NCF_HOTNEGATIVE 0x40
147 * Name caching works as follows:
149 * Names found by directory scans are retained in a cache
150 * for future reference. It is managed LRU, so frequently
151 * used names will hang around. Cache is indexed by hash value
152 * obtained from (dvp, name) where dvp refers to the directory
155 * If it is a "negative" entry, (i.e. for a name that is known NOT to
156 * exist) the vnode pointer will be NULL.
158 * Upon reaching the last segment of a path, if the reference
159 * is for DELETE, or NOCACHE is set (rewrite), and the
160 * name is located in the cache, it will be dropped.
162 * These locks are used (in the order in which they can be taken):
164 * vnodelock mtx vnode lists and v_cache_dd field protection
165 * bucketlock rwlock for access to given set of hash buckets
166 * neglist mtx negative entry LRU management
168 * Additionally, ncneg_shrink_lock mtx is used to have at most one thread
169 * shrinking the LRU list.
171 * It is legal to take multiple vnodelock and bucketlock locks. The locking
172 * order is lower address first. Both are recursive.
174 * "." lookups are lockless.
176 * ".." and vnode -> name lookups require vnodelock.
178 * name -> vnode lookup requires the relevant bucketlock to be held for reading.
180 * Insertions and removals of entries require involved vnodes and bucketlocks
181 * to be write-locked to prevent other threads from seeing the entry.
183 * Some lookups result in removal of the found entry (e.g. getting rid of a
184 * negative entry with the intent to create a positive one), which poses a
185 * problem when multiple threads reach the state. Similarly, two different
186 * threads can purge two different vnodes and try to remove the same name.
188 * If the already held vnode lock is lower than the second required lock, we
189 * can just take the other lock. However, in the opposite case, this could
190 * deadlock. As such, this is resolved by trylocking and if that fails unlocking
191 * the first node, locking everything in order and revalidating the state.
195 * Structures associated with name caching.
197 #define NCHHASH(hash) \
198 (&nchashtbl[(hash) & nchash])
199 static __read_mostly LIST_HEAD(nchashhead, namecache) *nchashtbl;/* Hash Table */
200 static u_long __read_mostly nchash; /* size of hash table */
201 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0,
202 "Size of namecache hash table");
203 static u_long __read_mostly ncnegfactor = 5; /* ratio of negative entries */
204 SYSCTL_ULONG(_vfs, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0,
205 "Ratio of negative namecache entries");
206 static u_long __exclusive_cache_line numneg; /* number of negative entries allocated */
207 static u_long __exclusive_cache_line numcache;/* number of cache entries allocated */
208 u_int ncsizefactor = 2;
209 SYSCTL_UINT(_vfs, OID_AUTO, ncsizefactor, CTLFLAG_RW, &ncsizefactor, 0,
210 "Size factor for namecache");
211 static u_int __read_mostly ncpurgeminvnodes;
212 SYSCTL_UINT(_vfs, OID_AUTO, ncpurgeminvnodes, CTLFLAG_RW, &ncpurgeminvnodes, 0,
213 "Number of vnodes below which purgevfs ignores the request");
214 static u_int __read_mostly ncsize; /* the size as computed on creation or resizing */
216 struct nchstats nchstats; /* cache effectiveness statistics */
218 static struct mtx __exclusive_cache_line ncneg_shrink_lock;
219 static int shrink_list_turn;
223 TAILQ_HEAD(, namecache) nl_list;
224 } __aligned(CACHE_LINE_SIZE);
226 static struct neglist __read_mostly *neglists;
227 static struct neglist ncneg_hot;
228 static u_long numhotneg;
230 #define numneglists (ncneghash + 1)
231 static u_int __read_mostly ncneghash;
232 static inline struct neglist *
233 NCP2NEGLIST(struct namecache *ncp)
236 return (&neglists[(((uintptr_t)(ncp) >> 8) & ncneghash)]);
239 #define numbucketlocks (ncbuckethash + 1)
240 static u_int __read_mostly ncbuckethash;
241 static struct rwlock_padalign __read_mostly *bucketlocks;
242 #define HASH2BUCKETLOCK(hash) \
243 ((struct rwlock *)(&bucketlocks[((hash) & ncbuckethash)]))
245 #define numvnodelocks (ncvnodehash + 1)
246 static u_int __read_mostly ncvnodehash;
247 static struct mtx __read_mostly *vnodelocks;
248 static inline struct mtx *
249 VP2VNODELOCK(struct vnode *vp)
252 return (&vnodelocks[(((uintptr_t)(vp) >> 8) & ncvnodehash)]);
256 * UMA zones for the VFS cache.
258 * The small cache is used for entries with short names, which are the
259 * most common. The large cache is used for entries which are too big to
260 * fit in the small cache.
262 static uma_zone_t __read_mostly cache_zone_small;
263 static uma_zone_t __read_mostly cache_zone_small_ts;
264 static uma_zone_t __read_mostly cache_zone_large;
265 static uma_zone_t __read_mostly cache_zone_large_ts;
267 #define CACHE_PATH_CUTOFF 35
269 static struct namecache *
270 cache_alloc(int len, int ts)
272 struct namecache_ts *ncp_ts;
273 struct namecache *ncp;
275 if (__predict_false(ts)) {
276 if (len <= CACHE_PATH_CUTOFF)
277 ncp_ts = uma_zalloc(cache_zone_small_ts, M_WAITOK);
279 ncp_ts = uma_zalloc(cache_zone_large_ts, M_WAITOK);
280 ncp = &ncp_ts->nc_nc;
282 if (len <= CACHE_PATH_CUTOFF)
283 ncp = uma_zalloc(cache_zone_small, M_WAITOK);
285 ncp = uma_zalloc(cache_zone_large, M_WAITOK);
291 cache_free(struct namecache *ncp)
293 struct namecache_ts *ncp_ts;
297 if ((ncp->nc_flag & NCF_DVDROP) != 0)
299 if (__predict_false(ncp->nc_flag & NCF_TS)) {
300 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
301 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
302 uma_zfree(cache_zone_small_ts, ncp_ts);
304 uma_zfree(cache_zone_large_ts, ncp_ts);
306 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
307 uma_zfree(cache_zone_small, ncp);
309 uma_zfree(cache_zone_large, ncp);
314 cache_out_ts(struct namecache *ncp, struct timespec *tsp, int *ticksp)
316 struct namecache_ts *ncp_ts;
318 KASSERT((ncp->nc_flag & NCF_TS) != 0 ||
319 (tsp == NULL && ticksp == NULL),
322 if (tsp == NULL && ticksp == NULL)
325 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
327 *tsp = ncp_ts->nc_time;
329 *ticksp = ncp_ts->nc_ticks;
333 static int __read_mostly doingcache = 1; /* 1 => enable the cache */
334 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0,
335 "VFS namecache enabled");
338 /* Export size information to userland */
339 SYSCTL_INT(_debug_sizeof, OID_AUTO, namecache, CTLFLAG_RD, SYSCTL_NULL_INT_PTR,
340 sizeof(struct namecache), "sizeof(struct namecache)");
343 * The new name cache statistics
345 static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0,
346 "Name cache statistics");
347 #define STATNODE_ULONG(name, descr) \
348 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, descr);
349 #define STATNODE_COUNTER(name, descr) \
350 static counter_u64_t __read_mostly name; \
351 SYSCTL_COUNTER_U64(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, descr);
352 STATNODE_ULONG(numneg, "Number of negative cache entries");
353 STATNODE_ULONG(numcache, "Number of cache entries");
354 STATNODE_COUNTER(numcachehv, "Number of namecache entries with vnodes held");
355 STATNODE_COUNTER(numcalls, "Number of cache lookups");
356 STATNODE_COUNTER(dothits, "Number of '.' hits");
357 STATNODE_COUNTER(dotdothits, "Number of '..' hits");
358 STATNODE_COUNTER(numchecks, "Number of checks in lookup");
359 STATNODE_COUNTER(nummiss, "Number of cache misses");
360 STATNODE_COUNTER(nummisszap, "Number of cache misses we do not want to cache");
361 STATNODE_COUNTER(numposzaps,
362 "Number of cache hits (positive) we do not want to cache");
363 STATNODE_COUNTER(numposhits, "Number of cache hits (positive)");
364 STATNODE_COUNTER(numnegzaps,
365 "Number of cache hits (negative) we do not want to cache");
366 STATNODE_COUNTER(numneghits, "Number of cache hits (negative)");
367 /* These count for vn_getcwd(), too. */
368 STATNODE_COUNTER(numfullpathcalls, "Number of fullpath search calls");
369 STATNODE_COUNTER(numfullpathfail1, "Number of fullpath search errors (ENOTDIR)");
370 STATNODE_COUNTER(numfullpathfail2,
371 "Number of fullpath search errors (VOP_VPTOCNP failures)");
372 STATNODE_COUNTER(numfullpathfail4, "Number of fullpath search errors (ENOMEM)");
373 STATNODE_COUNTER(numfullpathfound, "Number of successful fullpath calls");
374 STATNODE_COUNTER(zap_and_exit_bucket_relock_success,
375 "Number of successful removals after relocking");
376 static long zap_and_exit_bucket_fail; STATNODE_ULONG(zap_and_exit_bucket_fail,
377 "Number of times zap_and_exit failed to lock");
378 static long zap_and_exit_bucket_fail2; STATNODE_ULONG(zap_and_exit_bucket_fail2,
379 "Number of times zap_and_exit failed to lock");
380 static long cache_lock_vnodes_cel_3_failures;
381 STATNODE_ULONG(cache_lock_vnodes_cel_3_failures,
382 "Number of times 3-way vnode locking failed");
383 STATNODE_ULONG(numhotneg, "Number of hot negative entries");
384 STATNODE_COUNTER(numneg_evicted,
385 "Number of negative entries evicted when adding a new entry");
386 STATNODE_COUNTER(shrinking_skipped,
387 "Number of times shrinking was already in progress");
389 static void cache_zap_locked(struct namecache *ncp, bool neg_locked);
390 static int vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
391 char *buf, char **retbuf, size_t *buflen);
393 static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
395 static int cache_yield;
396 SYSCTL_INT(_vfs_cache, OID_AUTO, yield, CTLFLAG_RD, &cache_yield, 0,
397 "Number of times cache called yield");
399 static void __noinline
400 cache_maybe_yield(void)
403 if (should_yield()) {
405 kern_yield(PRI_USER);
410 cache_assert_vlp_locked(struct mtx *vlp)
414 mtx_assert(vlp, MA_OWNED);
418 cache_assert_vnode_locked(struct vnode *vp)
422 vlp = VP2VNODELOCK(vp);
423 cache_assert_vlp_locked(vlp);
427 cache_get_hash(char *name, u_char len, struct vnode *dvp)
431 hash = fnv_32_buf(name, len, FNV1_32_INIT);
432 hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
436 static inline struct rwlock *
437 NCP2BUCKETLOCK(struct namecache *ncp)
441 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen, ncp->nc_dvp);
442 return (HASH2BUCKETLOCK(hash));
447 cache_assert_bucket_locked(struct namecache *ncp, int mode)
451 blp = NCP2BUCKETLOCK(ncp);
452 rw_assert(blp, mode);
455 #define cache_assert_bucket_locked(x, y) do { } while (0)
458 #define cache_sort_vnodes(x, y) _cache_sort_vnodes((void **)(x), (void **)(y))
460 _cache_sort_vnodes(void **p1, void **p2)
464 MPASS(*p1 != NULL || *p2 != NULL);
474 cache_lock_all_buckets(void)
478 for (i = 0; i < numbucketlocks; i++)
479 rw_wlock(&bucketlocks[i]);
483 cache_unlock_all_buckets(void)
487 for (i = 0; i < numbucketlocks; i++)
488 rw_wunlock(&bucketlocks[i]);
492 cache_lock_all_vnodes(void)
496 for (i = 0; i < numvnodelocks; i++)
497 mtx_lock(&vnodelocks[i]);
501 cache_unlock_all_vnodes(void)
505 for (i = 0; i < numvnodelocks; i++)
506 mtx_unlock(&vnodelocks[i]);
510 cache_trylock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
513 cache_sort_vnodes(&vlp1, &vlp2);
516 if (!mtx_trylock(vlp1))
519 if (!mtx_trylock(vlp2)) {
529 cache_lock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
532 MPASS(vlp1 != NULL || vlp2 != NULL);
542 cache_unlock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
545 MPASS(vlp1 != NULL || vlp2 != NULL);
554 sysctl_nchstats(SYSCTL_HANDLER_ARGS)
556 struct nchstats snap;
558 if (req->oldptr == NULL)
559 return (SYSCTL_OUT(req, 0, sizeof(snap)));
562 snap.ncs_goodhits = counter_u64_fetch(numposhits);
563 snap.ncs_neghits = counter_u64_fetch(numneghits);
564 snap.ncs_badhits = counter_u64_fetch(numposzaps) +
565 counter_u64_fetch(numnegzaps);
566 snap.ncs_miss = counter_u64_fetch(nummisszap) +
567 counter_u64_fetch(nummiss);
569 return (SYSCTL_OUT(req, &snap, sizeof(snap)));
571 SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE | CTLFLAG_RD |
572 CTLFLAG_MPSAFE, 0, 0, sysctl_nchstats, "LU",
573 "VFS cache effectiveness statistics");
577 * Grab an atomic snapshot of the name cache hash chain lengths
579 static SYSCTL_NODE(_debug, OID_AUTO, hashstat, CTLFLAG_RW, NULL,
583 sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS)
585 struct nchashhead *ncpp;
586 struct namecache *ncp;
587 int i, error, n_nchash, *cntbuf;
590 n_nchash = nchash + 1; /* nchash is max index, not count */
591 if (req->oldptr == NULL)
592 return SYSCTL_OUT(req, 0, n_nchash * sizeof(int));
593 cntbuf = malloc(n_nchash * sizeof(int), M_TEMP, M_ZERO | M_WAITOK);
594 cache_lock_all_buckets();
595 if (n_nchash != nchash + 1) {
596 cache_unlock_all_buckets();
597 free(cntbuf, M_TEMP);
600 /* Scan hash tables counting entries */
601 for (ncpp = nchashtbl, i = 0; i < n_nchash; ncpp++, i++)
602 LIST_FOREACH(ncp, ncpp, nc_hash)
604 cache_unlock_all_buckets();
605 for (error = 0, i = 0; i < n_nchash; i++)
606 if ((error = SYSCTL_OUT(req, &cntbuf[i], sizeof(int))) != 0)
608 free(cntbuf, M_TEMP);
611 SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD|
612 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_rawnchash, "S,int",
613 "nchash chain lengths");
616 sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS)
619 struct nchashhead *ncpp;
620 struct namecache *ncp;
622 int count, maxlength, used, pct;
625 return SYSCTL_OUT(req, 0, 4 * sizeof(int));
627 cache_lock_all_buckets();
628 n_nchash = nchash + 1; /* nchash is max index, not count */
632 /* Scan hash tables for applicable entries */
633 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
635 LIST_FOREACH(ncp, ncpp, nc_hash) {
640 if (maxlength < count)
643 n_nchash = nchash + 1;
644 cache_unlock_all_buckets();
645 pct = (used * 100) / (n_nchash / 100);
646 error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash));
649 error = SYSCTL_OUT(req, &used, sizeof(used));
652 error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength));
655 error = SYSCTL_OUT(req, &pct, sizeof(pct));
660 SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD|
661 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_nchash, "I",
662 "nchash statistics (number of total/used buckets, maximum chain length, usage percentage)");
666 * Negative entries management
668 * A variation of LRU scheme is used. New entries are hashed into one of
669 * numneglists cold lists. Entries get promoted to the hot list on first hit.
671 * The shrinker will demote hot list head and evict from the cold list in a
672 * round-robin manner.
675 cache_negative_hit(struct namecache *ncp)
677 struct neglist *neglist;
679 MPASS(ncp->nc_flag & NCF_NEGATIVE);
680 if (ncp->nc_flag & NCF_HOTNEGATIVE)
682 neglist = NCP2NEGLIST(ncp);
683 mtx_lock(&ncneg_hot.nl_lock);
684 mtx_lock(&neglist->nl_lock);
685 if (!(ncp->nc_flag & NCF_HOTNEGATIVE)) {
687 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
688 TAILQ_INSERT_TAIL(&ncneg_hot.nl_list, ncp, nc_dst);
689 ncp->nc_flag |= NCF_HOTNEGATIVE;
691 mtx_unlock(&neglist->nl_lock);
692 mtx_unlock(&ncneg_hot.nl_lock);
696 cache_negative_insert(struct namecache *ncp, bool neg_locked)
698 struct neglist *neglist;
700 MPASS(ncp->nc_flag & NCF_NEGATIVE);
701 cache_assert_bucket_locked(ncp, RA_WLOCKED);
702 neglist = NCP2NEGLIST(ncp);
704 mtx_lock(&neglist->nl_lock);
706 mtx_assert(&neglist->nl_lock, MA_OWNED);
708 TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst);
710 mtx_unlock(&neglist->nl_lock);
711 atomic_add_rel_long(&numneg, 1);
715 cache_negative_remove(struct namecache *ncp, bool neg_locked)
717 struct neglist *neglist;
718 bool hot_locked = false;
719 bool list_locked = false;
721 MPASS(ncp->nc_flag & NCF_NEGATIVE);
722 cache_assert_bucket_locked(ncp, RA_WLOCKED);
723 neglist = NCP2NEGLIST(ncp);
725 if (ncp->nc_flag & NCF_HOTNEGATIVE) {
727 mtx_lock(&ncneg_hot.nl_lock);
728 if (!(ncp->nc_flag & NCF_HOTNEGATIVE)) {
730 mtx_lock(&neglist->nl_lock);
734 mtx_lock(&neglist->nl_lock);
737 if (ncp->nc_flag & NCF_HOTNEGATIVE) {
738 mtx_assert(&ncneg_hot.nl_lock, MA_OWNED);
739 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
742 mtx_assert(&neglist->nl_lock, MA_OWNED);
743 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
746 mtx_unlock(&neglist->nl_lock);
748 mtx_unlock(&ncneg_hot.nl_lock);
749 atomic_subtract_rel_long(&numneg, 1);
753 cache_negative_shrink_select(int start, struct namecache **ncpp,
754 struct neglist **neglistpp)
756 struct neglist *neglist;
757 struct namecache *ncp;
763 for (i = start; i < numneglists; i++) {
764 neglist = &neglists[i];
765 if (TAILQ_FIRST(&neglist->nl_list) == NULL)
767 mtx_lock(&neglist->nl_lock);
768 ncp = TAILQ_FIRST(&neglist->nl_list);
771 mtx_unlock(&neglist->nl_lock);
774 *neglistpp = neglist;
779 cache_negative_zap_one(void)
781 struct namecache *ncp, *ncp2;
782 struct neglist *neglist;
786 if (mtx_owner(&ncneg_shrink_lock) != NULL ||
787 !mtx_trylock(&ncneg_shrink_lock)) {
788 counter_u64_add(shrinking_skipped, 1);
792 mtx_lock(&ncneg_hot.nl_lock);
793 ncp = TAILQ_FIRST(&ncneg_hot.nl_list);
795 neglist = NCP2NEGLIST(ncp);
796 mtx_lock(&neglist->nl_lock);
797 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
798 TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst);
799 ncp->nc_flag &= ~NCF_HOTNEGATIVE;
801 mtx_unlock(&neglist->nl_lock);
803 mtx_unlock(&ncneg_hot.nl_lock);
805 cache_negative_shrink_select(shrink_list_turn, &ncp, &neglist);
807 if (shrink_list_turn == numneglists)
808 shrink_list_turn = 0;
809 if (ncp == NULL && shrink_list_turn == 0)
810 cache_negative_shrink_select(shrink_list_turn, &ncp, &neglist);
811 mtx_unlock(&ncneg_shrink_lock);
815 MPASS(ncp->nc_flag & NCF_NEGATIVE);
816 dvlp = VP2VNODELOCK(ncp->nc_dvp);
817 blp = NCP2BUCKETLOCK(ncp);
818 mtx_unlock(&neglist->nl_lock);
821 mtx_lock(&neglist->nl_lock);
822 ncp2 = TAILQ_FIRST(&neglist->nl_list);
823 if (ncp != ncp2 || dvlp != VP2VNODELOCK(ncp2->nc_dvp) ||
824 blp != NCP2BUCKETLOCK(ncp2) || !(ncp2->nc_flag & NCF_NEGATIVE)) {
827 SDT_PROBE2(vfs, namecache, shrink_negative, done, ncp->nc_dvp,
830 cache_zap_locked(ncp, true);
831 counter_u64_add(numneg_evicted, 1);
833 mtx_unlock(&neglist->nl_lock);
840 * cache_zap_locked():
842 * Removes a namecache entry from cache, whether it contains an actual
843 * pointer to a vnode or if it is just a negative cache entry.
846 cache_zap_locked(struct namecache *ncp, bool neg_locked)
849 if (!(ncp->nc_flag & NCF_NEGATIVE))
850 cache_assert_vnode_locked(ncp->nc_vp);
851 cache_assert_vnode_locked(ncp->nc_dvp);
852 cache_assert_bucket_locked(ncp, RA_WLOCKED);
854 CTR2(KTR_VFS, "cache_zap(%p) vp %p", ncp,
855 (ncp->nc_flag & NCF_NEGATIVE) ? NULL : ncp->nc_vp);
856 LIST_REMOVE(ncp, nc_hash);
857 if (!(ncp->nc_flag & NCF_NEGATIVE)) {
858 SDT_PROBE3(vfs, namecache, zap, done, ncp->nc_dvp,
859 ncp->nc_name, ncp->nc_vp);
860 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
861 if (ncp == ncp->nc_vp->v_cache_dd)
862 ncp->nc_vp->v_cache_dd = NULL;
864 SDT_PROBE2(vfs, namecache, zap_negative, done, ncp->nc_dvp,
866 cache_negative_remove(ncp, neg_locked);
868 if (ncp->nc_flag & NCF_ISDOTDOT) {
869 if (ncp == ncp->nc_dvp->v_cache_dd)
870 ncp->nc_dvp->v_cache_dd = NULL;
872 LIST_REMOVE(ncp, nc_src);
873 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) {
874 ncp->nc_flag |= NCF_DVDROP;
875 counter_u64_add(numcachehv, -1);
878 atomic_subtract_rel_long(&numcache, 1);
882 cache_zap_negative_locked_vnode_kl(struct namecache *ncp, struct vnode *vp)
886 MPASS(ncp->nc_dvp == vp);
887 MPASS(ncp->nc_flag & NCF_NEGATIVE);
888 cache_assert_vnode_locked(vp);
890 blp = NCP2BUCKETLOCK(ncp);
892 cache_zap_locked(ncp, false);
897 cache_zap_locked_vnode_kl2(struct namecache *ncp, struct vnode *vp,
900 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
903 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
904 cache_assert_vnode_locked(vp);
906 if (ncp->nc_flag & NCF_NEGATIVE) {
911 cache_zap_negative_locked_vnode_kl(ncp, vp);
915 pvlp = VP2VNODELOCK(vp);
916 blp = NCP2BUCKETLOCK(ncp);
917 vlp1 = VP2VNODELOCK(ncp->nc_dvp);
918 vlp2 = VP2VNODELOCK(ncp->nc_vp);
920 if (*vlpp == vlp1 || *vlpp == vlp2) {
928 cache_sort_vnodes(&vlp1, &vlp2);
933 if (!mtx_trylock(vlp1))
939 cache_zap_locked(ncp, false);
941 if (to_unlock != NULL)
942 mtx_unlock(to_unlock);
949 MPASS(*vlpp == NULL);
954 static int __noinline
955 cache_zap_locked_vnode(struct namecache *ncp, struct vnode *vp)
957 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
961 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
962 cache_assert_vnode_locked(vp);
964 pvlp = VP2VNODELOCK(vp);
965 if (ncp->nc_flag & NCF_NEGATIVE) {
966 cache_zap_negative_locked_vnode_kl(ncp, vp);
970 blp = NCP2BUCKETLOCK(ncp);
971 vlp1 = VP2VNODELOCK(ncp->nc_dvp);
972 vlp2 = VP2VNODELOCK(ncp->nc_vp);
973 cache_sort_vnodes(&vlp1, &vlp2);
978 if (!mtx_trylock(vlp1)) {
985 cache_zap_locked(ncp, false);
987 mtx_unlock(to_unlock);
994 * If trylocking failed we can get here. We know enough to take all needed locks
995 * in the right order and re-lookup the entry.
998 cache_zap_unlocked_bucket(struct namecache *ncp, struct componentname *cnp,
999 struct vnode *dvp, struct mtx *dvlp, struct mtx *vlp, uint32_t hash,
1002 struct namecache *rncp;
1004 cache_assert_bucket_locked(ncp, RA_UNLOCKED);
1006 cache_sort_vnodes(&dvlp, &vlp);
1007 cache_lock_vnodes(dvlp, vlp);
1009 LIST_FOREACH(rncp, (NCHHASH(hash)), nc_hash) {
1010 if (rncp == ncp && rncp->nc_dvp == dvp &&
1011 rncp->nc_nlen == cnp->cn_namelen &&
1012 !bcmp(rncp->nc_name, cnp->cn_nameptr, rncp->nc_nlen))
1016 cache_zap_locked(rncp, false);
1018 cache_unlock_vnodes(dvlp, vlp);
1019 counter_u64_add(zap_and_exit_bucket_relock_success, 1);
1024 cache_unlock_vnodes(dvlp, vlp);
1028 static int __noinline
1029 cache_zap_wlocked_bucket(struct namecache *ncp, struct componentname *cnp,
1030 uint32_t hash, struct rwlock *blp)
1032 struct mtx *dvlp, *vlp;
1035 cache_assert_bucket_locked(ncp, RA_WLOCKED);
1037 dvlp = VP2VNODELOCK(ncp->nc_dvp);
1039 if (!(ncp->nc_flag & NCF_NEGATIVE))
1040 vlp = VP2VNODELOCK(ncp->nc_vp);
1041 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1042 cache_zap_locked(ncp, false);
1044 cache_unlock_vnodes(dvlp, vlp);
1050 return (cache_zap_unlocked_bucket(ncp, cnp, dvp, dvlp, vlp, hash, blp));
1053 static int __noinline
1054 cache_zap_rlocked_bucket(struct namecache *ncp, struct componentname *cnp,
1055 uint32_t hash, struct rwlock *blp)
1057 struct mtx *dvlp, *vlp;
1060 cache_assert_bucket_locked(ncp, RA_RLOCKED);
1062 dvlp = VP2VNODELOCK(ncp->nc_dvp);
1064 if (!(ncp->nc_flag & NCF_NEGATIVE))
1065 vlp = VP2VNODELOCK(ncp->nc_vp);
1066 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1069 cache_zap_locked(ncp, false);
1071 cache_unlock_vnodes(dvlp, vlp);
1077 return (cache_zap_unlocked_bucket(ncp, cnp, dvp, dvlp, vlp, hash, blp));
1081 cache_zap_wlocked_bucket_kl(struct namecache *ncp, struct rwlock *blp,
1082 struct mtx **vlpp1, struct mtx **vlpp2)
1084 struct mtx *dvlp, *vlp;
1086 cache_assert_bucket_locked(ncp, RA_WLOCKED);
1088 dvlp = VP2VNODELOCK(ncp->nc_dvp);
1090 if (!(ncp->nc_flag & NCF_NEGATIVE))
1091 vlp = VP2VNODELOCK(ncp->nc_vp);
1092 cache_sort_vnodes(&dvlp, &vlp);
1094 if (*vlpp1 == dvlp && *vlpp2 == vlp) {
1095 cache_zap_locked(ncp, false);
1096 cache_unlock_vnodes(dvlp, vlp);
1109 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1110 cache_zap_locked(ncp, false);
1111 cache_unlock_vnodes(dvlp, vlp);
1126 cache_lookup_unlock(struct rwlock *blp, struct mtx *vlp)
1136 static int __noinline
1137 cache_lookup_dot(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1138 struct timespec *tsp, int *ticksp)
1143 CTR2(KTR_VFS, "cache_lookup(%p, %s) found via .",
1144 dvp, cnp->cn_nameptr);
1145 counter_u64_add(dothits, 1);
1146 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", *vpp);
1153 * When we lookup "." we still can be asked to lock it
1156 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
1157 if (ltype != VOP_ISLOCKED(*vpp)) {
1158 if (ltype == LK_EXCLUSIVE) {
1159 vn_lock(*vpp, LK_UPGRADE | LK_RETRY);
1160 if (VN_IS_DOOMED((*vpp))) {
1161 /* forced unmount */
1167 vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY);
1172 static __noinline int
1173 cache_lookup_nomakeentry(struct vnode *dvp, struct vnode **vpp,
1174 struct componentname *cnp, struct timespec *tsp, int *ticksp)
1176 struct namecache *ncp;
1178 struct mtx *dvlp, *dvlp2;
1182 if (cnp->cn_namelen == 2 &&
1183 cnp->cn_nameptr[0] == '.' && cnp->cn_nameptr[1] == '.') {
1184 counter_u64_add(dotdothits, 1);
1185 dvlp = VP2VNODELOCK(dvp);
1189 ncp = dvp->v_cache_dd;
1191 SDT_PROBE3(vfs, namecache, lookup, miss, dvp,
1198 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1199 if (ncp->nc_dvp != dvp)
1200 panic("dvp %p v_cache_dd %p\n", dvp, ncp);
1201 if (!cache_zap_locked_vnode_kl2(ncp,
1204 MPASS(dvp->v_cache_dd == NULL);
1210 dvp->v_cache_dd = NULL;
1218 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1219 blp = HASH2BUCKETLOCK(hash);
1221 if (LIST_EMPTY(NCHHASH(hash)))
1226 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1227 counter_u64_add(numchecks, 1);
1228 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1229 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1233 /* We failed to find an entry */
1239 error = cache_zap_wlocked_bucket(ncp, cnp, hash, blp);
1240 if (__predict_false(error != 0)) {
1241 zap_and_exit_bucket_fail++;
1242 cache_maybe_yield();
1245 counter_u64_add(numposzaps, 1);
1249 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr, NULL);
1250 counter_u64_add(nummisszap, 1);
1255 * Lookup a name in the name cache
1259 * - dvp: Parent directory in which to search.
1260 * - vpp: Return argument. Will contain desired vnode on cache hit.
1261 * - cnp: Parameters of the name search. The most interesting bits of
1262 * the cn_flags field have the following meanings:
1263 * - MAKEENTRY: If clear, free an entry from the cache rather than look
1265 * - ISDOTDOT: Must be set if and only if cn_nameptr == ".."
1266 * - tsp: Return storage for cache timestamp. On a successful (positive
1267 * or negative) lookup, tsp will be filled with any timespec that
1268 * was stored when this cache entry was created. However, it will
1269 * be clear for "." entries.
1270 * - ticks: Return storage for alternate cache timestamp. On a successful
1271 * (positive or negative) lookup, it will contain the ticks value
1272 * that was current when the cache entry was created, unless cnp
1277 * - -1: A positive cache hit. vpp will contain the desired vnode.
1278 * - ENOENT: A negative cache hit, or dvp was recycled out from under us due
1279 * to a forced unmount. vpp will not be modified. If the entry
1280 * is a whiteout, then the ISWHITEOUT flag will be set in
1282 * - 0: A cache miss. vpp will not be modified.
1286 * On a cache hit, vpp will be returned locked and ref'd. If we're looking up
1287 * .., dvp is unlocked. If we're looking up . an extra ref is taken, but the
1288 * lock is not recursively acquired.
1291 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1292 struct timespec *tsp, int *ticksp)
1294 struct namecache_ts *ncp_ts;
1295 struct namecache *ncp;
1303 if (__predict_false(!doingcache)) {
1304 cnp->cn_flags &= ~MAKEENTRY;
1309 counter_u64_add(numcalls, 1);
1311 if (__predict_false(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.'))
1312 return (cache_lookup_dot(dvp, vpp, cnp, tsp, ticksp));
1314 if ((cnp->cn_flags & MAKEENTRY) == 0)
1315 return (cache_lookup_nomakeentry(dvp, vpp, cnp, tsp, ticksp));
1321 if (cnp->cn_namelen == 2 &&
1322 cnp->cn_nameptr[0] == '.' && cnp->cn_nameptr[1] == '.') {
1323 counter_u64_add(dotdothits, 1);
1324 dvlp = VP2VNODELOCK(dvp);
1326 ncp = dvp->v_cache_dd;
1328 SDT_PROBE3(vfs, namecache, lookup, miss, dvp,
1333 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1334 if (ncp->nc_flag & NCF_NEGATIVE)
1340 /* Return failure if negative entry was found. */
1342 goto negative_success;
1343 CTR3(KTR_VFS, "cache_lookup(%p, %s) found %p via ..",
1344 dvp, cnp->cn_nameptr, *vpp);
1345 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, "..",
1347 cache_out_ts(ncp, tsp, ticksp);
1348 if ((ncp->nc_flag & (NCF_ISDOTDOT | NCF_DTS)) ==
1349 NCF_DTS && tsp != NULL) {
1350 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1351 *tsp = ncp_ts->nc_dotdottime;
1356 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1357 blp = HASH2BUCKETLOCK(hash);
1360 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1361 counter_u64_add(numchecks, 1);
1362 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1363 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1367 /* We failed to find an entry */
1368 if (__predict_false(ncp == NULL)) {
1370 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
1372 counter_u64_add(nummiss, 1);
1376 if (ncp->nc_flag & NCF_NEGATIVE)
1377 goto negative_success;
1379 /* We found a "positive" match, return the vnode */
1380 counter_u64_add(numposhits, 1);
1382 CTR4(KTR_VFS, "cache_lookup(%p, %s) found %p via ncp %p",
1383 dvp, cnp->cn_nameptr, *vpp, ncp);
1384 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name,
1386 cache_out_ts(ncp, tsp, ticksp);
1389 * On success we return a locked and ref'd vnode as per the lookup
1393 ltype = 0; /* silence gcc warning */
1394 if (cnp->cn_flags & ISDOTDOT) {
1395 ltype = VOP_ISLOCKED(dvp);
1398 vs = vget_prep(*vpp);
1399 cache_lookup_unlock(blp, dvlp);
1400 error = vget_finish(*vpp, cnp->cn_lkflags, vs);
1401 if (cnp->cn_flags & ISDOTDOT) {
1402 vn_lock(dvp, ltype | LK_RETRY);
1403 if (VN_IS_DOOMED(dvp)) {
1414 if ((cnp->cn_flags & ISLASTCN) &&
1415 (cnp->cn_lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE) {
1416 ASSERT_VOP_ELOCKED(*vpp, "cache_lookup");
1421 /* We found a negative match, and want to create it, so purge */
1422 if (cnp->cn_nameiop == CREATE) {
1423 counter_u64_add(numnegzaps, 1);
1427 counter_u64_add(numneghits, 1);
1428 cache_negative_hit(ncp);
1429 if (ncp->nc_flag & NCF_WHITE)
1430 cnp->cn_flags |= ISWHITEOUT;
1431 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp,
1433 cache_out_ts(ncp, tsp, ticksp);
1434 cache_lookup_unlock(blp, dvlp);
1439 error = cache_zap_rlocked_bucket(ncp, cnp, hash, blp);
1441 error = cache_zap_locked_vnode(ncp, dvp);
1442 if (__predict_false(error != 0)) {
1443 zap_and_exit_bucket_fail2++;
1444 cache_maybe_yield();
1451 struct celockstate {
1453 struct rwlock *blp[2];
1455 CTASSERT((nitems(((struct celockstate *)0)->vlp) == 3));
1456 CTASSERT((nitems(((struct celockstate *)0)->blp) == 2));
1459 cache_celockstate_init(struct celockstate *cel)
1462 bzero(cel, sizeof(*cel));
1466 cache_lock_vnodes_cel(struct celockstate *cel, struct vnode *vp,
1469 struct mtx *vlp1, *vlp2;
1471 MPASS(cel->vlp[0] == NULL);
1472 MPASS(cel->vlp[1] == NULL);
1473 MPASS(cel->vlp[2] == NULL);
1475 MPASS(vp != NULL || dvp != NULL);
1477 vlp1 = VP2VNODELOCK(vp);
1478 vlp2 = VP2VNODELOCK(dvp);
1479 cache_sort_vnodes(&vlp1, &vlp2);
1490 cache_unlock_vnodes_cel(struct celockstate *cel)
1493 MPASS(cel->vlp[0] != NULL || cel->vlp[1] != NULL);
1495 if (cel->vlp[0] != NULL)
1496 mtx_unlock(cel->vlp[0]);
1497 if (cel->vlp[1] != NULL)
1498 mtx_unlock(cel->vlp[1]);
1499 if (cel->vlp[2] != NULL)
1500 mtx_unlock(cel->vlp[2]);
1504 cache_lock_vnodes_cel_3(struct celockstate *cel, struct vnode *vp)
1509 cache_assert_vlp_locked(cel->vlp[0]);
1510 cache_assert_vlp_locked(cel->vlp[1]);
1511 MPASS(cel->vlp[2] == NULL);
1514 vlp = VP2VNODELOCK(vp);
1517 if (vlp >= cel->vlp[1]) {
1520 if (mtx_trylock(vlp))
1522 cache_lock_vnodes_cel_3_failures++;
1523 cache_unlock_vnodes_cel(cel);
1524 if (vlp < cel->vlp[0]) {
1526 mtx_lock(cel->vlp[0]);
1527 mtx_lock(cel->vlp[1]);
1529 if (cel->vlp[0] != NULL)
1530 mtx_lock(cel->vlp[0]);
1532 mtx_lock(cel->vlp[1]);
1542 cache_lock_buckets_cel(struct celockstate *cel, struct rwlock *blp1,
1543 struct rwlock *blp2)
1546 MPASS(cel->blp[0] == NULL);
1547 MPASS(cel->blp[1] == NULL);
1549 cache_sort_vnodes(&blp1, &blp2);
1560 cache_unlock_buckets_cel(struct celockstate *cel)
1563 if (cel->blp[0] != NULL)
1564 rw_wunlock(cel->blp[0]);
1565 rw_wunlock(cel->blp[1]);
1569 * Lock part of the cache affected by the insertion.
1571 * This means vnodelocks for dvp, vp and the relevant bucketlock.
1572 * However, insertion can result in removal of an old entry. In this
1573 * case we have an additional vnode and bucketlock pair to lock. If the
1574 * entry is negative, ncelock is locked instead of the vnode.
1576 * That is, in the worst case we have to lock 3 vnodes and 2 bucketlocks, while
1577 * preserving the locking order (smaller address first).
1580 cache_enter_lock(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1583 struct namecache *ncp;
1584 struct rwlock *blps[2];
1586 blps[0] = HASH2BUCKETLOCK(hash);
1589 cache_lock_vnodes_cel(cel, dvp, vp);
1590 if (vp == NULL || vp->v_type != VDIR)
1592 ncp = vp->v_cache_dd;
1595 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1597 MPASS(ncp->nc_dvp == vp);
1598 blps[1] = NCP2BUCKETLOCK(ncp);
1599 if (ncp->nc_flag & NCF_NEGATIVE)
1601 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1604 * All vnodes got re-locked. Re-validate the state and if
1605 * nothing changed we are done. Otherwise restart.
1607 if (ncp == vp->v_cache_dd &&
1608 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1609 blps[1] == NCP2BUCKETLOCK(ncp) &&
1610 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1612 cache_unlock_vnodes_cel(cel);
1617 cache_lock_buckets_cel(cel, blps[0], blps[1]);
1621 cache_enter_lock_dd(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1624 struct namecache *ncp;
1625 struct rwlock *blps[2];
1627 blps[0] = HASH2BUCKETLOCK(hash);
1630 cache_lock_vnodes_cel(cel, dvp, vp);
1631 ncp = dvp->v_cache_dd;
1634 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1636 MPASS(ncp->nc_dvp == dvp);
1637 blps[1] = NCP2BUCKETLOCK(ncp);
1638 if (ncp->nc_flag & NCF_NEGATIVE)
1640 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1642 if (ncp == dvp->v_cache_dd &&
1643 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1644 blps[1] == NCP2BUCKETLOCK(ncp) &&
1645 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1647 cache_unlock_vnodes_cel(cel);
1652 cache_lock_buckets_cel(cel, blps[0], blps[1]);
1656 cache_enter_unlock(struct celockstate *cel)
1659 cache_unlock_buckets_cel(cel);
1660 cache_unlock_vnodes_cel(cel);
1663 static void __noinline
1664 cache_enter_dotdot_prep(struct vnode *dvp, struct vnode *vp,
1665 struct componentname *cnp)
1667 struct celockstate cel;
1668 struct namecache *ncp;
1672 if (dvp->v_cache_dd == NULL)
1674 len = cnp->cn_namelen;
1675 cache_celockstate_init(&cel);
1676 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1677 cache_enter_lock_dd(&cel, dvp, vp, hash);
1678 ncp = dvp->v_cache_dd;
1679 if (ncp != NULL && (ncp->nc_flag & NCF_ISDOTDOT)) {
1680 KASSERT(ncp->nc_dvp == dvp, ("wrong isdotdot parent"));
1681 cache_zap_locked(ncp, false);
1685 dvp->v_cache_dd = NULL;
1686 cache_enter_unlock(&cel);
1691 * Add an entry to the cache.
1694 cache_enter_time(struct vnode *dvp, struct vnode *vp, struct componentname *cnp,
1695 struct timespec *tsp, struct timespec *dtsp)
1697 struct celockstate cel;
1698 struct namecache *ncp, *n2, *ndd;
1699 struct namecache_ts *ncp_ts, *n2_ts;
1700 struct nchashhead *ncpp;
1706 CTR3(KTR_VFS, "cache_enter(%p, %p, %s)", dvp, vp, cnp->cn_nameptr);
1707 VNASSERT(vp == NULL || !VN_IS_DOOMED(vp), vp,
1708 ("cache_enter: Adding a doomed vnode"));
1709 VNASSERT(dvp == NULL || !VN_IS_DOOMED(dvp), dvp,
1710 ("cache_enter: Doomed vnode used as src"));
1713 if (__predict_false(!doingcache))
1718 if (__predict_false(cnp->cn_nameptr[0] == '.')) {
1719 if (cnp->cn_namelen == 1)
1721 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
1722 cache_enter_dotdot_prep(dvp, vp, cnp);
1723 flag = NCF_ISDOTDOT;
1728 * Avoid blowout in namecache entries.
1730 lnumcache = atomic_fetchadd_long(&numcache, 1) + 1;
1731 if (__predict_false(lnumcache >= ncsize)) {
1732 atomic_add_long(&numcache, -1);
1736 cache_celockstate_init(&cel);
1741 * Calculate the hash key and setup as much of the new
1742 * namecache entry as possible before acquiring the lock.
1744 ncp = cache_alloc(cnp->cn_namelen, tsp != NULL);
1745 ncp->nc_flag = flag;
1748 ncp->nc_flag |= NCF_NEGATIVE;
1751 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1752 ncp_ts->nc_time = *tsp;
1753 ncp_ts->nc_ticks = ticks;
1754 ncp_ts->nc_nc.nc_flag |= NCF_TS;
1756 ncp_ts->nc_dotdottime = *dtsp;
1757 ncp_ts->nc_nc.nc_flag |= NCF_DTS;
1760 len = ncp->nc_nlen = cnp->cn_namelen;
1761 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1762 strlcpy(ncp->nc_name, cnp->cn_nameptr, len + 1);
1763 cache_enter_lock(&cel, dvp, vp, hash);
1766 * See if this vnode or negative entry is already in the cache
1767 * with this name. This can happen with concurrent lookups of
1768 * the same path name.
1770 ncpp = NCHHASH(hash);
1771 LIST_FOREACH(n2, ncpp, nc_hash) {
1772 if (n2->nc_dvp == dvp &&
1773 n2->nc_nlen == cnp->cn_namelen &&
1774 !bcmp(n2->nc_name, cnp->cn_nameptr, n2->nc_nlen)) {
1776 KASSERT((n2->nc_flag & NCF_TS) != 0,
1778 n2_ts = __containerof(n2, struct namecache_ts, nc_nc);
1779 n2_ts->nc_time = ncp_ts->nc_time;
1780 n2_ts->nc_ticks = ncp_ts->nc_ticks;
1782 n2_ts->nc_dotdottime = ncp_ts->nc_dotdottime;
1783 if (ncp->nc_flag & NCF_NEGATIVE)
1784 mtx_lock(&ncneg_hot.nl_lock);
1785 n2_ts->nc_nc.nc_flag |= NCF_DTS;
1786 if (ncp->nc_flag & NCF_NEGATIVE)
1787 mtx_unlock(&ncneg_hot.nl_lock);
1790 goto out_unlock_free;
1794 if (flag == NCF_ISDOTDOT) {
1796 * See if we are trying to add .. entry, but some other lookup
1797 * has populated v_cache_dd pointer already.
1799 if (dvp->v_cache_dd != NULL)
1800 goto out_unlock_free;
1801 KASSERT(vp == NULL || vp->v_type == VDIR,
1802 ("wrong vnode type %p", vp));
1803 dvp->v_cache_dd = ncp;
1807 if (vp->v_type == VDIR) {
1808 if (flag != NCF_ISDOTDOT) {
1810 * For this case, the cache entry maps both the
1811 * directory name in it and the name ".." for the
1812 * directory's parent.
1814 if ((ndd = vp->v_cache_dd) != NULL) {
1815 if ((ndd->nc_flag & NCF_ISDOTDOT) != 0)
1816 cache_zap_locked(ndd, false);
1820 vp->v_cache_dd = ncp;
1823 vp->v_cache_dd = NULL;
1827 if (flag != NCF_ISDOTDOT) {
1828 if (LIST_EMPTY(&dvp->v_cache_src)) {
1830 counter_u64_add(numcachehv, 1);
1832 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
1836 * Insert the new namecache entry into the appropriate chain
1837 * within the cache entries table.
1839 LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
1842 * If the entry is "negative", we place it into the
1843 * "negative" cache queue, otherwise, we place it into the
1844 * destination vnode's cache entries queue.
1847 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
1848 SDT_PROBE3(vfs, namecache, enter, done, dvp, ncp->nc_name,
1851 if (cnp->cn_flags & ISWHITEOUT)
1852 ncp->nc_flag |= NCF_WHITE;
1853 cache_negative_insert(ncp, false);
1854 SDT_PROBE2(vfs, namecache, enter_negative, done, dvp,
1857 cache_enter_unlock(&cel);
1858 if (numneg * ncnegfactor > lnumcache)
1859 cache_negative_zap_one();
1863 cache_enter_unlock(&cel);
1869 cache_roundup_2(u_int val)
1873 for (res = 1; res <= val; res <<= 1)
1880 * Name cache initialization, from vfs_init() when we are booting
1883 nchinit(void *dummy __unused)
1887 cache_zone_small = uma_zcreate("S VFS Cache",
1888 sizeof(struct namecache) + CACHE_PATH_CUTOFF + 1,
1889 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache),
1891 cache_zone_small_ts = uma_zcreate("STS VFS Cache",
1892 sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1,
1893 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache_ts),
1895 cache_zone_large = uma_zcreate("L VFS Cache",
1896 sizeof(struct namecache) + NAME_MAX + 1,
1897 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache),
1899 cache_zone_large_ts = uma_zcreate("LTS VFS Cache",
1900 sizeof(struct namecache_ts) + NAME_MAX + 1,
1901 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache_ts),
1904 ncsize = desiredvnodes * ncsizefactor;
1905 nchashtbl = hashinit(desiredvnodes * 2, M_VFSCACHE, &nchash);
1906 ncbuckethash = cache_roundup_2(mp_ncpus * mp_ncpus) - 1;
1907 if (ncbuckethash < 7) /* arbitrarily chosen to avoid having one lock */
1909 if (ncbuckethash > nchash)
1910 ncbuckethash = nchash;
1911 bucketlocks = malloc(sizeof(*bucketlocks) * numbucketlocks, M_VFSCACHE,
1913 for (i = 0; i < numbucketlocks; i++)
1914 rw_init_flags(&bucketlocks[i], "ncbuc", RW_DUPOK | RW_RECURSE);
1915 ncvnodehash = ncbuckethash;
1916 vnodelocks = malloc(sizeof(*vnodelocks) * numvnodelocks, M_VFSCACHE,
1918 for (i = 0; i < numvnodelocks; i++)
1919 mtx_init(&vnodelocks[i], "ncvn", NULL, MTX_DUPOK | MTX_RECURSE);
1920 ncpurgeminvnodes = numbucketlocks * 2;
1923 neglists = malloc(sizeof(*neglists) * numneglists, M_VFSCACHE,
1925 for (i = 0; i < numneglists; i++) {
1926 mtx_init(&neglists[i].nl_lock, "ncnegl", NULL, MTX_DEF);
1927 TAILQ_INIT(&neglists[i].nl_list);
1929 mtx_init(&ncneg_hot.nl_lock, "ncneglh", NULL, MTX_DEF);
1930 TAILQ_INIT(&ncneg_hot.nl_list);
1932 mtx_init(&ncneg_shrink_lock, "ncnegs", NULL, MTX_DEF);
1934 numcachehv = counter_u64_alloc(M_WAITOK);
1935 numcalls = counter_u64_alloc(M_WAITOK);
1936 dothits = counter_u64_alloc(M_WAITOK);
1937 dotdothits = counter_u64_alloc(M_WAITOK);
1938 numchecks = counter_u64_alloc(M_WAITOK);
1939 nummiss = counter_u64_alloc(M_WAITOK);
1940 nummisszap = counter_u64_alloc(M_WAITOK);
1941 numposzaps = counter_u64_alloc(M_WAITOK);
1942 numposhits = counter_u64_alloc(M_WAITOK);
1943 numnegzaps = counter_u64_alloc(M_WAITOK);
1944 numneghits = counter_u64_alloc(M_WAITOK);
1945 numfullpathcalls = counter_u64_alloc(M_WAITOK);
1946 numfullpathfail1 = counter_u64_alloc(M_WAITOK);
1947 numfullpathfail2 = counter_u64_alloc(M_WAITOK);
1948 numfullpathfail4 = counter_u64_alloc(M_WAITOK);
1949 numfullpathfound = counter_u64_alloc(M_WAITOK);
1950 zap_and_exit_bucket_relock_success = counter_u64_alloc(M_WAITOK);
1951 numneg_evicted = counter_u64_alloc(M_WAITOK);
1952 shrinking_skipped = counter_u64_alloc(M_WAITOK);
1954 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL);
1957 cache_changesize(u_long newmaxvnodes)
1959 struct nchashhead *new_nchashtbl, *old_nchashtbl;
1960 u_long new_nchash, old_nchash;
1961 struct namecache *ncp;
1966 newncsize = newmaxvnodes * ncsizefactor;
1967 newmaxvnodes = cache_roundup_2(newmaxvnodes * 2);
1968 if (newmaxvnodes < numbucketlocks)
1969 newmaxvnodes = numbucketlocks;
1971 new_nchashtbl = hashinit(newmaxvnodes, M_VFSCACHE, &new_nchash);
1972 /* If same hash table size, nothing to do */
1973 if (nchash == new_nchash) {
1974 free(new_nchashtbl, M_VFSCACHE);
1978 * Move everything from the old hash table to the new table.
1979 * None of the namecache entries in the table can be removed
1980 * because to do so, they have to be removed from the hash table.
1982 cache_lock_all_vnodes();
1983 cache_lock_all_buckets();
1984 old_nchashtbl = nchashtbl;
1985 old_nchash = nchash;
1986 nchashtbl = new_nchashtbl;
1987 nchash = new_nchash;
1988 for (i = 0; i <= old_nchash; i++) {
1989 while ((ncp = LIST_FIRST(&old_nchashtbl[i])) != NULL) {
1990 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen,
1992 LIST_REMOVE(ncp, nc_hash);
1993 LIST_INSERT_HEAD(NCHHASH(hash), ncp, nc_hash);
1997 cache_unlock_all_buckets();
1998 cache_unlock_all_vnodes();
1999 free(old_nchashtbl, M_VFSCACHE);
2003 * Invalidate all entries from and to a particular vnode.
2006 cache_purge(struct vnode *vp)
2008 TAILQ_HEAD(, namecache) ncps;
2009 struct namecache *ncp, *nnp;
2010 struct mtx *vlp, *vlp2;
2012 CTR1(KTR_VFS, "cache_purge(%p)", vp);
2013 SDT_PROBE1(vfs, namecache, purge, done, vp);
2014 if (LIST_EMPTY(&vp->v_cache_src) && TAILQ_EMPTY(&vp->v_cache_dst) &&
2015 vp->v_cache_dd == NULL)
2018 vlp = VP2VNODELOCK(vp);
2022 while (!LIST_EMPTY(&vp->v_cache_src)) {
2023 ncp = LIST_FIRST(&vp->v_cache_src);
2024 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2026 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2028 while (!TAILQ_EMPTY(&vp->v_cache_dst)) {
2029 ncp = TAILQ_FIRST(&vp->v_cache_dst);
2030 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2032 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2034 ncp = vp->v_cache_dd;
2036 KASSERT(ncp->nc_flag & NCF_ISDOTDOT,
2037 ("lost dotdot link"));
2038 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2040 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2042 KASSERT(vp->v_cache_dd == NULL, ("incomplete purge"));
2046 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2052 * Invalidate all negative entries for a particular directory vnode.
2055 cache_purge_negative(struct vnode *vp)
2057 TAILQ_HEAD(, namecache) ncps;
2058 struct namecache *ncp, *nnp;
2061 CTR1(KTR_VFS, "cache_purge_negative(%p)", vp);
2062 SDT_PROBE1(vfs, namecache, purge_negative, done, vp);
2063 if (LIST_EMPTY(&vp->v_cache_src))
2066 vlp = VP2VNODELOCK(vp);
2068 LIST_FOREACH_SAFE(ncp, &vp->v_cache_src, nc_src, nnp) {
2069 if (!(ncp->nc_flag & NCF_NEGATIVE))
2071 cache_zap_negative_locked_vnode_kl(ncp, vp);
2072 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2075 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2081 * Flush all entries referencing a particular filesystem.
2084 cache_purgevfs(struct mount *mp, bool force)
2086 TAILQ_HEAD(, namecache) ncps;
2087 struct mtx *vlp1, *vlp2;
2089 struct nchashhead *bucket;
2090 struct namecache *ncp, *nnp;
2091 u_long i, j, n_nchash;
2094 /* Scan hash tables for applicable entries */
2095 SDT_PROBE1(vfs, namecache, purgevfs, done, mp);
2096 if (!force && mp->mnt_nvnodelistsize <= ncpurgeminvnodes)
2099 n_nchash = nchash + 1;
2101 for (i = 0; i < numbucketlocks; i++) {
2102 blp = (struct rwlock *)&bucketlocks[i];
2104 for (j = i; j < n_nchash; j += numbucketlocks) {
2106 bucket = &nchashtbl[j];
2107 LIST_FOREACH_SAFE(ncp, bucket, nc_hash, nnp) {
2108 cache_assert_bucket_locked(ncp, RA_WLOCKED);
2109 if (ncp->nc_dvp->v_mount != mp)
2111 error = cache_zap_wlocked_bucket_kl(ncp, blp,
2115 TAILQ_INSERT_HEAD(&ncps, ncp, nc_dst);
2119 if (vlp1 == NULL && vlp2 == NULL)
2120 cache_maybe_yield();
2127 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2133 * Perform canonical checks and cache lookup and pass on to filesystem
2134 * through the vop_cachedlookup only if needed.
2138 vfs_cache_lookup(struct vop_lookup_args *ap)
2142 struct vnode **vpp = ap->a_vpp;
2143 struct componentname *cnp = ap->a_cnp;
2144 struct ucred *cred = cnp->cn_cred;
2145 int flags = cnp->cn_flags;
2146 struct thread *td = cnp->cn_thread;
2151 if (dvp->v_type != VDIR)
2154 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
2155 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
2158 error = VOP_ACCESS(dvp, VEXEC, cred, td);
2162 error = cache_lookup(dvp, vpp, cnp, NULL, NULL);
2164 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
2170 /* Implementation of the getcwd syscall. */
2172 sys___getcwd(struct thread *td, struct __getcwd_args *uap)
2178 buflen = uap->buflen;
2179 if (__predict_false(buflen < 2))
2181 if (buflen > MAXPATHLEN)
2182 buflen = MAXPATHLEN;
2184 buf = malloc(buflen, M_TEMP, M_WAITOK);
2185 error = vn_getcwd(td, buf, &retbuf, &buflen);
2187 error = copyout(retbuf, uap->buf, buflen);
2193 vn_getcwd(struct thread *td, char *buf, char **retbuf, size_t *buflen)
2195 struct filedesc *fdp;
2196 struct vnode *cdir, *rdir;
2199 fdp = td->td_proc->p_fd;
2200 FILEDESC_SLOCK(fdp);
2201 cdir = fdp->fd_cdir;
2203 rdir = fdp->fd_rdir;
2205 FILEDESC_SUNLOCK(fdp);
2206 error = vn_fullpath1(td, cdir, rdir, buf, retbuf, buflen);
2211 if (KTRPOINT(curthread, KTR_NAMEI) && error == 0)
2218 * Retrieve the full filesystem path that correspond to a vnode from the name
2219 * cache (if available)
2222 vn_fullpath(struct thread *td, struct vnode *vn, char **retbuf, char **freebuf)
2225 struct filedesc *fdp;
2230 if (__predict_false(vn == NULL))
2233 buflen = MAXPATHLEN;
2234 buf = malloc(buflen, M_TEMP, M_WAITOK);
2235 fdp = td->td_proc->p_fd;
2236 FILEDESC_SLOCK(fdp);
2237 rdir = fdp->fd_rdir;
2239 FILEDESC_SUNLOCK(fdp);
2240 error = vn_fullpath1(td, vn, rdir, buf, retbuf, &buflen);
2251 * This function is similar to vn_fullpath, but it attempts to lookup the
2252 * pathname relative to the global root mount point. This is required for the
2253 * auditing sub-system, as audited pathnames must be absolute, relative to the
2254 * global root mount point.
2257 vn_fullpath_global(struct thread *td, struct vnode *vn,
2258 char **retbuf, char **freebuf)
2264 if (__predict_false(vn == NULL))
2266 buflen = MAXPATHLEN;
2267 buf = malloc(buflen, M_TEMP, M_WAITOK);
2268 error = vn_fullpath1(td, vn, rootvnode, buf, retbuf, &buflen);
2277 vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, size_t *buflen)
2280 struct namecache *ncp;
2284 vlp = VP2VNODELOCK(*vp);
2286 TAILQ_FOREACH(ncp, &((*vp)->v_cache_dst), nc_dst) {
2287 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2291 if (*buflen < ncp->nc_nlen) {
2294 counter_u64_add(numfullpathfail4, 1);
2296 SDT_PROBE3(vfs, namecache, fullpath, return, error,
2300 *buflen -= ncp->nc_nlen;
2301 memcpy(buf + *buflen, ncp->nc_name, ncp->nc_nlen);
2302 SDT_PROBE3(vfs, namecache, fullpath, hit, ncp->nc_dvp,
2311 SDT_PROBE1(vfs, namecache, fullpath, miss, vp);
2314 vn_lock(*vp, LK_SHARED | LK_RETRY);
2315 error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen);
2318 counter_u64_add(numfullpathfail2, 1);
2319 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2324 if (VN_IS_DOOMED(dvp)) {
2325 /* forced unmount */
2328 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2332 * *vp has its use count incremented still.
2339 * The magic behind vn_getcwd() and vn_fullpath().
2342 vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
2343 char *buf, char **retbuf, size_t *len)
2345 int error, slash_prefixed;
2346 #ifdef KDTRACE_HOOKS
2347 struct vnode *startvp = vp;
2359 SDT_PROBE1(vfs, namecache, fullpath, entry, vp);
2360 counter_u64_add(numfullpathcalls, 1);
2362 if (vp->v_type != VDIR) {
2363 error = vn_vptocnp(&vp, td->td_ucred, buf, &buflen);
2370 buf[--buflen] = '/';
2373 while (vp != rdir && vp != rootvnode) {
2375 * The vp vnode must be already fully constructed,
2376 * since it is either found in namecache or obtained
2377 * from VOP_VPTOCNP(). We may test for VV_ROOT safely
2378 * without obtaining the vnode lock.
2380 if ((vp->v_vflag & VV_ROOT) != 0) {
2381 vn_lock(vp, LK_RETRY | LK_SHARED);
2384 * With the vnode locked, check for races with
2385 * unmount, forced or not. Note that we
2386 * already verified that vp is not equal to
2387 * the root vnode, which means that
2388 * mnt_vnodecovered can be NULL only for the
2391 if (VN_IS_DOOMED(vp) ||
2392 (vp1 = vp->v_mount->mnt_vnodecovered) == NULL ||
2393 vp1->v_mountedhere != vp->v_mount) {
2396 SDT_PROBE3(vfs, namecache, fullpath, return,
2406 if (vp->v_type != VDIR) {
2408 counter_u64_add(numfullpathfail1, 1);
2410 SDT_PROBE3(vfs, namecache, fullpath, return,
2414 error = vn_vptocnp(&vp, td->td_ucred, buf, &buflen);
2420 SDT_PROBE3(vfs, namecache, fullpath, return, error,
2424 buf[--buflen] = '/';
2429 if (!slash_prefixed) {
2432 counter_u64_add(numfullpathfail4, 1);
2433 SDT_PROBE3(vfs, namecache, fullpath, return, ENOMEM,
2437 buf[--buflen] = '/';
2439 counter_u64_add(numfullpathfound, 1);
2442 SDT_PROBE3(vfs, namecache, fullpath, return, 0, startvp, buf + buflen);
2443 *retbuf = buf + buflen;
2449 vn_dir_dd_ino(struct vnode *vp)
2451 struct namecache *ncp;
2456 ASSERT_VOP_LOCKED(vp, "vn_dir_dd_ino");
2457 vlp = VP2VNODELOCK(vp);
2459 TAILQ_FOREACH(ncp, &(vp->v_cache_dst), nc_dst) {
2460 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0)
2463 vs = vget_prep(ddvp);
2465 if (vget_finish(ddvp, LK_SHARED | LK_NOWAIT, vs))
2474 vn_commname(struct vnode *vp, char *buf, u_int buflen)
2476 struct namecache *ncp;
2480 vlp = VP2VNODELOCK(vp);
2482 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst)
2483 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2489 l = min(ncp->nc_nlen, buflen - 1);
2490 memcpy(buf, ncp->nc_name, l);
2497 * This function updates path string to vnode's full global path
2498 * and checks the size of the new path string against the pathlen argument.
2500 * Requires a locked, referenced vnode.
2501 * Vnode is re-locked on success or ENODEV, otherwise unlocked.
2503 * If vp is a directory, the call to vn_fullpath_global() always succeeds
2504 * because it falls back to the ".." lookup if the namecache lookup fails.
2507 vn_path_to_global_path(struct thread *td, struct vnode *vp, char *path,
2510 struct nameidata nd;
2515 ASSERT_VOP_ELOCKED(vp, __func__);
2517 /* Construct global filesystem path from vp. */
2519 error = vn_fullpath_global(td, vp, &rpath, &fbuf);
2526 if (strlen(rpath) >= pathlen) {
2528 error = ENAMETOOLONG;
2533 * Re-lookup the vnode by path to detect a possible rename.
2534 * As a side effect, the vnode is relocked.
2535 * If vnode was renamed, return ENOENT.
2537 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
2538 UIO_SYSSPACE, path, td);
2544 NDFREE(&nd, NDF_ONLY_PNBUF);
2548 strcpy(path, rpath);
2561 db_print_vpath(struct vnode *vp)
2564 while (vp != NULL) {
2565 db_printf("%p: ", vp);
2566 if (vp == rootvnode) {
2570 if (vp->v_vflag & VV_ROOT) {
2571 db_printf("<mount point>");
2572 vp = vp->v_mount->mnt_vnodecovered;
2574 struct namecache *ncp;
2578 ncp = TAILQ_FIRST(&vp->v_cache_dst);
2581 for (i = 0; i < ncp->nc_nlen; i++)
2582 db_printf("%c", *ncn++);
2595 DB_SHOW_COMMAND(vpath, db_show_vpath)
2600 db_printf("usage: show vpath <struct vnode *>\n");
2604 vp = (struct vnode *)addr;