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/capsicum.h>
46 #include <sys/counter.h>
47 #include <sys/filedesc.h>
48 #include <sys/fnv_hash.h>
49 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/fcntl.h>
54 #include <sys/mount.h>
55 #include <sys/namei.h>
57 #include <sys/rwlock.h>
61 #include <sys/syscallsubr.h>
62 #include <sys/sysctl.h>
63 #include <sys/sysproto.h>
64 #include <sys/vnode.h>
67 #include <sys/ktrace.h>
76 SDT_PROVIDER_DECLARE(vfs);
77 SDT_PROBE_DEFINE3(vfs, namecache, enter, done, "struct vnode *", "char *",
79 SDT_PROBE_DEFINE2(vfs, namecache, enter_negative, done, "struct vnode *",
81 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, entry, "struct vnode *");
82 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, hit, "struct vnode *",
83 "char *", "struct vnode *");
84 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, miss, "struct vnode *");
85 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, return, "int",
86 "struct vnode *", "char *");
87 SDT_PROBE_DEFINE3(vfs, namecache, lookup, hit, "struct vnode *", "char *",
89 SDT_PROBE_DEFINE2(vfs, namecache, lookup, hit__negative,
90 "struct vnode *", "char *");
91 SDT_PROBE_DEFINE2(vfs, namecache, lookup, miss, "struct vnode *",
93 SDT_PROBE_DEFINE1(vfs, namecache, purge, done, "struct vnode *");
94 SDT_PROBE_DEFINE1(vfs, namecache, purge_negative, done, "struct vnode *");
95 SDT_PROBE_DEFINE1(vfs, namecache, purgevfs, done, "struct mount *");
96 SDT_PROBE_DEFINE3(vfs, namecache, zap, done, "struct vnode *", "char *",
98 SDT_PROBE_DEFINE2(vfs, namecache, zap_negative, done, "struct vnode *",
100 SDT_PROBE_DEFINE2(vfs, namecache, shrink_negative, done, "struct vnode *",
104 * This structure describes the elements in the cache of recent
105 * names looked up by namei.
109 CK_LIST_ENTRY(namecache) nc_hash;/* hash chain */
110 LIST_ENTRY(namecache) nc_src; /* source vnode list */
111 TAILQ_ENTRY(namecache) nc_dst; /* destination vnode list */
112 struct vnode *nc_dvp; /* vnode of parent of name */
114 struct vnode *nu_vp; /* vnode the name refers to */
116 u_char nc_flag; /* flag bits */
117 u_char nc_nlen; /* length of name */
118 char nc_name[0]; /* segment name + nul */
122 * struct namecache_ts repeats struct namecache layout up to the
124 * struct namecache_ts is used in place of struct namecache when time(s) need
125 * to be stored. The nc_dotdottime field is used when a cache entry is mapping
126 * both a non-dotdot directory name plus dotdot for the directory's
129 struct namecache_ts {
130 struct timespec nc_time; /* timespec provided by fs */
131 struct timespec nc_dotdottime; /* dotdot timespec provided by fs */
132 int nc_ticks; /* ticks value when entry was added */
133 struct namecache nc_nc;
136 #define nc_vp n_un.nu_vp
139 * Flags in namecache.nc_flag
141 #define NCF_WHITE 0x01
142 #define NCF_ISDOTDOT 0x02
145 #define NCF_DVDROP 0x10
146 #define NCF_NEGATIVE 0x20
147 #define NCF_HOTNEGATIVE 0x40
148 #define NCF_INVALID 0x80
151 cache_ncp_invalid(struct namecache *ncp)
154 atomic_thread_fence_acq();
155 return ((ncp->nc_flag & NCF_INVALID) != 0);
159 cache_ncp_invalidate(struct namecache *ncp)
162 atomic_thread_fence_rel();
163 KASSERT((ncp->nc_flag & NCF_INVALID) == 0,
164 ("%s: entry %p already invalid", __func__, ncp));
165 ncp->nc_flag |= NCF_INVALID;
169 * Name caching works as follows:
171 * Names found by directory scans are retained in a cache
172 * for future reference. It is managed LRU, so frequently
173 * used names will hang around. Cache is indexed by hash value
174 * obtained from (dvp, name) where dvp refers to the directory
177 * If it is a "negative" entry, (i.e. for a name that is known NOT to
178 * exist) the vnode pointer will be NULL.
180 * Upon reaching the last segment of a path, if the reference
181 * is for DELETE, or NOCACHE is set (rewrite), and the
182 * name is located in the cache, it will be dropped.
184 * These locks are used (in the order in which they can be taken):
186 * vnodelock mtx vnode lists and v_cache_dd field protection
187 * bucketlock rwlock for access to given set of hash buckets
188 * neglist mtx negative entry LRU management
190 * Additionally, ncneg_shrink_lock mtx is used to have at most one thread
191 * shrinking the LRU list.
193 * It is legal to take multiple vnodelock and bucketlock locks. The locking
194 * order is lower address first. Both are recursive.
196 * "." lookups are lockless.
198 * ".." and vnode -> name lookups require vnodelock.
200 * name -> vnode lookup requires the relevant bucketlock to be held for reading.
202 * Insertions and removals of entries require involved vnodes and bucketlocks
203 * to be write-locked to prevent other threads from seeing the entry.
205 * Some lookups result in removal of the found entry (e.g. getting rid of a
206 * negative entry with the intent to create a positive one), which poses a
207 * problem when multiple threads reach the state. Similarly, two different
208 * threads can purge two different vnodes and try to remove the same name.
210 * If the already held vnode lock is lower than the second required lock, we
211 * can just take the other lock. However, in the opposite case, this could
212 * deadlock. As such, this is resolved by trylocking and if that fails unlocking
213 * the first node, locking everything in order and revalidating the state.
219 * Structures associated with name caching.
221 #define NCHHASH(hash) \
222 (&nchashtbl[(hash) & nchash])
223 static __read_mostly CK_LIST_HEAD(nchashhead, namecache) *nchashtbl;/* Hash Table */
224 static u_long __read_mostly nchash; /* size of hash table */
225 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0,
226 "Size of namecache hash table");
227 static u_long __read_mostly ncnegfactor = 5; /* ratio of negative entries */
228 SYSCTL_ULONG(_vfs, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0,
229 "Ratio of negative namecache entries");
230 static u_long __exclusive_cache_line numneg; /* number of negative entries allocated */
231 static u_long __exclusive_cache_line numcache;/* number of cache entries allocated */
232 u_int ncsizefactor = 2;
233 SYSCTL_UINT(_vfs, OID_AUTO, ncsizefactor, CTLFLAG_RW, &ncsizefactor, 0,
234 "Size factor for namecache");
235 static u_int __read_mostly ncpurgeminvnodes;
236 SYSCTL_UINT(_vfs, OID_AUTO, ncpurgeminvnodes, CTLFLAG_RW, &ncpurgeminvnodes, 0,
237 "Number of vnodes below which purgevfs ignores the request");
238 static u_int __read_mostly ncsize; /* the size as computed on creation or resizing */
240 struct nchstats nchstats; /* cache effectiveness statistics */
242 static struct mtx __exclusive_cache_line ncneg_shrink_lock;
243 static int shrink_list_turn;
247 TAILQ_HEAD(, namecache) nl_list;
248 } __aligned(CACHE_LINE_SIZE);
250 static struct neglist __read_mostly *neglists;
251 static struct neglist ncneg_hot;
252 static u_long numhotneg;
254 #define numneglists (ncneghash + 1)
255 static u_int __read_mostly ncneghash;
256 static inline struct neglist *
257 NCP2NEGLIST(struct namecache *ncp)
260 return (&neglists[(((uintptr_t)(ncp) >> 8) & ncneghash)]);
263 #define numbucketlocks (ncbuckethash + 1)
264 static u_int __read_mostly ncbuckethash;
265 static struct rwlock_padalign __read_mostly *bucketlocks;
266 #define HASH2BUCKETLOCK(hash) \
267 ((struct rwlock *)(&bucketlocks[((hash) & ncbuckethash)]))
269 #define numvnodelocks (ncvnodehash + 1)
270 static u_int __read_mostly ncvnodehash;
271 static struct mtx __read_mostly *vnodelocks;
272 static inline struct mtx *
273 VP2VNODELOCK(struct vnode *vp)
276 return (&vnodelocks[(((uintptr_t)(vp) >> 8) & ncvnodehash)]);
280 * UMA zones for the VFS cache.
282 * The small cache is used for entries with short names, which are the
283 * most common. The large cache is used for entries which are too big to
284 * fit in the small cache.
286 static uma_zone_t __read_mostly cache_zone_small;
287 static uma_zone_t __read_mostly cache_zone_small_ts;
288 static uma_zone_t __read_mostly cache_zone_large;
289 static uma_zone_t __read_mostly cache_zone_large_ts;
291 #define CACHE_PATH_CUTOFF 35
293 static struct namecache *
294 cache_alloc(int len, int ts)
296 struct namecache_ts *ncp_ts;
297 struct namecache *ncp;
299 if (__predict_false(ts)) {
300 if (len <= CACHE_PATH_CUTOFF)
301 ncp_ts = uma_zalloc_smr(cache_zone_small_ts, M_WAITOK);
303 ncp_ts = uma_zalloc_smr(cache_zone_large_ts, M_WAITOK);
304 ncp = &ncp_ts->nc_nc;
306 if (len <= CACHE_PATH_CUTOFF)
307 ncp = uma_zalloc_smr(cache_zone_small, M_WAITOK);
309 ncp = uma_zalloc_smr(cache_zone_large, M_WAITOK);
315 cache_free(struct namecache *ncp)
317 struct namecache_ts *ncp_ts;
321 if ((ncp->nc_flag & NCF_DVDROP) != 0)
323 if (__predict_false(ncp->nc_flag & NCF_TS)) {
324 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
325 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
326 uma_zfree_smr(cache_zone_small_ts, ncp_ts);
328 uma_zfree_smr(cache_zone_large_ts, ncp_ts);
330 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
331 uma_zfree_smr(cache_zone_small, ncp);
333 uma_zfree_smr(cache_zone_large, ncp);
338 cache_out_ts(struct namecache *ncp, struct timespec *tsp, int *ticksp)
340 struct namecache_ts *ncp_ts;
342 KASSERT((ncp->nc_flag & NCF_TS) != 0 ||
343 (tsp == NULL && ticksp == NULL),
346 if (tsp == NULL && ticksp == NULL)
349 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
351 *tsp = ncp_ts->nc_time;
353 *ticksp = ncp_ts->nc_ticks;
357 static int __read_mostly doingcache = 1; /* 1 => enable the cache */
358 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0,
359 "VFS namecache enabled");
362 /* Export size information to userland */
363 SYSCTL_INT(_debug_sizeof, OID_AUTO, namecache, CTLFLAG_RD, SYSCTL_NULL_INT_PTR,
364 sizeof(struct namecache), "sizeof(struct namecache)");
367 * The new name cache statistics
369 static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
370 "Name cache statistics");
371 #define STATNODE_ULONG(name, descr) \
372 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, descr);
373 #define STATNODE_COUNTER(name, descr) \
374 static COUNTER_U64_DEFINE_EARLY(name); \
375 SYSCTL_COUNTER_U64(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, \
377 STATNODE_ULONG(numneg, "Number of negative cache entries");
378 STATNODE_ULONG(numcache, "Number of cache entries");
379 STATNODE_COUNTER(numcachehv, "Number of namecache entries with vnodes held");
380 STATNODE_COUNTER(numcalls, "Number of cache lookups");
381 STATNODE_COUNTER(dothits, "Number of '.' hits");
382 STATNODE_COUNTER(dotdothits, "Number of '..' hits");
383 STATNODE_COUNTER(numchecks, "Number of checks in lookup");
384 STATNODE_COUNTER(nummiss, "Number of cache misses");
385 STATNODE_COUNTER(nummisszap, "Number of cache misses we do not want to cache");
386 STATNODE_COUNTER(numposzaps,
387 "Number of cache hits (positive) we do not want to cache");
388 STATNODE_COUNTER(numposhits, "Number of cache hits (positive)");
389 STATNODE_COUNTER(numnegzaps,
390 "Number of cache hits (negative) we do not want to cache");
391 STATNODE_COUNTER(numneghits, "Number of cache hits (negative)");
392 /* These count for vn_getcwd(), too. */
393 STATNODE_COUNTER(numfullpathcalls, "Number of fullpath search calls");
394 STATNODE_COUNTER(numfullpathfail1, "Number of fullpath search errors (ENOTDIR)");
395 STATNODE_COUNTER(numfullpathfail2,
396 "Number of fullpath search errors (VOP_VPTOCNP failures)");
397 STATNODE_COUNTER(numfullpathfail4, "Number of fullpath search errors (ENOMEM)");
398 STATNODE_COUNTER(numfullpathfound, "Number of successful fullpath calls");
399 STATNODE_COUNTER(zap_and_exit_bucket_relock_success,
400 "Number of successful removals after relocking");
401 static long zap_and_exit_bucket_fail; STATNODE_ULONG(zap_and_exit_bucket_fail,
402 "Number of times zap_and_exit failed to lock");
403 static long zap_and_exit_bucket_fail2; STATNODE_ULONG(zap_and_exit_bucket_fail2,
404 "Number of times zap_and_exit failed to lock");
405 static long cache_lock_vnodes_cel_3_failures;
406 STATNODE_ULONG(cache_lock_vnodes_cel_3_failures,
407 "Number of times 3-way vnode locking failed");
408 STATNODE_ULONG(numhotneg, "Number of hot negative entries");
409 STATNODE_COUNTER(numneg_evicted,
410 "Number of negative entries evicted when adding a new entry");
411 STATNODE_COUNTER(shrinking_skipped,
412 "Number of times shrinking was already in progress");
414 static void cache_zap_locked(struct namecache *ncp, bool neg_locked);
415 static int vn_fullpath_hardlink(struct thread *td, struct nameidata *ndp, char **retbuf,
416 char **freebuf, size_t *buflen);
417 static int vn_fullpath_any(struct thread *td, struct vnode *vp, struct vnode *rdir,
418 char *buf, char **retbuf, size_t *buflen);
419 static int vn_fullpath_dir(struct thread *td, struct vnode *vp, struct vnode *rdir,
420 char *buf, char **retbuf, size_t *len, bool slash_prefixed, size_t addend);
422 static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
424 static int cache_yield;
425 SYSCTL_INT(_vfs_cache, OID_AUTO, yield, CTLFLAG_RD, &cache_yield, 0,
426 "Number of times cache called yield");
428 static void __noinline
429 cache_maybe_yield(void)
432 if (should_yield()) {
434 kern_yield(PRI_USER);
439 cache_assert_vlp_locked(struct mtx *vlp)
443 mtx_assert(vlp, MA_OWNED);
447 cache_assert_vnode_locked(struct vnode *vp)
451 vlp = VP2VNODELOCK(vp);
452 cache_assert_vlp_locked(vlp);
456 cache_get_hash(char *name, u_char len, struct vnode *dvp)
460 hash = fnv_32_buf(name, len, FNV1_32_INIT);
461 hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
465 static inline struct rwlock *
466 NCP2BUCKETLOCK(struct namecache *ncp)
470 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen, ncp->nc_dvp);
471 return (HASH2BUCKETLOCK(hash));
476 cache_assert_bucket_locked(struct namecache *ncp, int mode)
480 blp = NCP2BUCKETLOCK(ncp);
481 rw_assert(blp, mode);
484 #define cache_assert_bucket_locked(x, y) do { } while (0)
487 #define cache_sort_vnodes(x, y) _cache_sort_vnodes((void **)(x), (void **)(y))
489 _cache_sort_vnodes(void **p1, void **p2)
493 MPASS(*p1 != NULL || *p2 != NULL);
503 cache_lock_all_buckets(void)
507 for (i = 0; i < numbucketlocks; i++)
508 rw_wlock(&bucketlocks[i]);
512 cache_unlock_all_buckets(void)
516 for (i = 0; i < numbucketlocks; i++)
517 rw_wunlock(&bucketlocks[i]);
521 cache_lock_all_vnodes(void)
525 for (i = 0; i < numvnodelocks; i++)
526 mtx_lock(&vnodelocks[i]);
530 cache_unlock_all_vnodes(void)
534 for (i = 0; i < numvnodelocks; i++)
535 mtx_unlock(&vnodelocks[i]);
539 cache_trylock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
542 cache_sort_vnodes(&vlp1, &vlp2);
545 if (!mtx_trylock(vlp1))
548 if (!mtx_trylock(vlp2)) {
558 cache_lock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
561 MPASS(vlp1 != NULL || vlp2 != NULL);
571 cache_unlock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
574 MPASS(vlp1 != NULL || vlp2 != NULL);
583 sysctl_nchstats(SYSCTL_HANDLER_ARGS)
585 struct nchstats snap;
587 if (req->oldptr == NULL)
588 return (SYSCTL_OUT(req, 0, sizeof(snap)));
591 snap.ncs_goodhits = counter_u64_fetch(numposhits);
592 snap.ncs_neghits = counter_u64_fetch(numneghits);
593 snap.ncs_badhits = counter_u64_fetch(numposzaps) +
594 counter_u64_fetch(numnegzaps);
595 snap.ncs_miss = counter_u64_fetch(nummisszap) +
596 counter_u64_fetch(nummiss);
598 return (SYSCTL_OUT(req, &snap, sizeof(snap)));
600 SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE | CTLFLAG_RD |
601 CTLFLAG_MPSAFE, 0, 0, sysctl_nchstats, "LU",
602 "VFS cache effectiveness statistics");
606 * Grab an atomic snapshot of the name cache hash chain lengths
608 static SYSCTL_NODE(_debug, OID_AUTO, hashstat,
609 CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
613 sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS)
615 struct nchashhead *ncpp;
616 struct namecache *ncp;
617 int i, error, n_nchash, *cntbuf;
620 n_nchash = nchash + 1; /* nchash is max index, not count */
621 if (req->oldptr == NULL)
622 return SYSCTL_OUT(req, 0, n_nchash * sizeof(int));
623 cntbuf = malloc(n_nchash * sizeof(int), M_TEMP, M_ZERO | M_WAITOK);
624 cache_lock_all_buckets();
625 if (n_nchash != nchash + 1) {
626 cache_unlock_all_buckets();
627 free(cntbuf, M_TEMP);
630 /* Scan hash tables counting entries */
631 for (ncpp = nchashtbl, i = 0; i < n_nchash; ncpp++, i++)
632 CK_LIST_FOREACH(ncp, ncpp, nc_hash)
634 cache_unlock_all_buckets();
635 for (error = 0, i = 0; i < n_nchash; i++)
636 if ((error = SYSCTL_OUT(req, &cntbuf[i], sizeof(int))) != 0)
638 free(cntbuf, M_TEMP);
641 SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD|
642 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_rawnchash, "S,int",
643 "nchash chain lengths");
646 sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS)
649 struct nchashhead *ncpp;
650 struct namecache *ncp;
652 int count, maxlength, used, pct;
655 return SYSCTL_OUT(req, 0, 4 * sizeof(int));
657 cache_lock_all_buckets();
658 n_nchash = nchash + 1; /* nchash is max index, not count */
662 /* Scan hash tables for applicable entries */
663 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
665 CK_LIST_FOREACH(ncp, ncpp, nc_hash) {
670 if (maxlength < count)
673 n_nchash = nchash + 1;
674 cache_unlock_all_buckets();
675 pct = (used * 100) / (n_nchash / 100);
676 error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash));
679 error = SYSCTL_OUT(req, &used, sizeof(used));
682 error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength));
685 error = SYSCTL_OUT(req, &pct, sizeof(pct));
690 SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD|
691 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_nchash, "I",
692 "nchash statistics (number of total/used buckets, maximum chain length, usage percentage)");
696 * Negative entries management
698 * A variation of LRU scheme is used. New entries are hashed into one of
699 * numneglists cold lists. Entries get promoted to the hot list on first hit.
701 * The shrinker will demote hot list head and evict from the cold list in a
702 * round-robin manner.
705 cache_negative_hit(struct namecache *ncp)
707 struct neglist *neglist;
709 MPASS(ncp->nc_flag & NCF_NEGATIVE);
710 if (ncp->nc_flag & NCF_HOTNEGATIVE)
712 neglist = NCP2NEGLIST(ncp);
713 mtx_lock(&ncneg_hot.nl_lock);
714 mtx_lock(&neglist->nl_lock);
715 if (!(ncp->nc_flag & NCF_HOTNEGATIVE)) {
717 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
718 TAILQ_INSERT_TAIL(&ncneg_hot.nl_list, ncp, nc_dst);
719 ncp->nc_flag |= NCF_HOTNEGATIVE;
721 mtx_unlock(&neglist->nl_lock);
722 mtx_unlock(&ncneg_hot.nl_lock);
726 cache_negative_insert(struct namecache *ncp, bool neg_locked)
728 struct neglist *neglist;
730 MPASS(ncp->nc_flag & NCF_NEGATIVE);
731 cache_assert_bucket_locked(ncp, RA_WLOCKED);
732 neglist = NCP2NEGLIST(ncp);
734 mtx_lock(&neglist->nl_lock);
736 mtx_assert(&neglist->nl_lock, MA_OWNED);
738 TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst);
740 mtx_unlock(&neglist->nl_lock);
741 atomic_add_rel_long(&numneg, 1);
745 cache_negative_remove(struct namecache *ncp, bool neg_locked)
747 struct neglist *neglist;
748 bool hot_locked = false;
749 bool list_locked = false;
751 MPASS(ncp->nc_flag & NCF_NEGATIVE);
752 cache_assert_bucket_locked(ncp, RA_WLOCKED);
753 neglist = NCP2NEGLIST(ncp);
755 if (ncp->nc_flag & NCF_HOTNEGATIVE) {
757 mtx_lock(&ncneg_hot.nl_lock);
758 if (!(ncp->nc_flag & NCF_HOTNEGATIVE)) {
760 mtx_lock(&neglist->nl_lock);
764 mtx_lock(&neglist->nl_lock);
767 if (ncp->nc_flag & NCF_HOTNEGATIVE) {
768 mtx_assert(&ncneg_hot.nl_lock, MA_OWNED);
769 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
772 mtx_assert(&neglist->nl_lock, MA_OWNED);
773 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
776 mtx_unlock(&neglist->nl_lock);
778 mtx_unlock(&ncneg_hot.nl_lock);
779 atomic_subtract_rel_long(&numneg, 1);
783 cache_negative_shrink_select(int start, struct namecache **ncpp,
784 struct neglist **neglistpp)
786 struct neglist *neglist;
787 struct namecache *ncp;
793 for (i = start; i < numneglists; i++) {
794 neglist = &neglists[i];
795 if (TAILQ_FIRST(&neglist->nl_list) == NULL)
797 mtx_lock(&neglist->nl_lock);
798 ncp = TAILQ_FIRST(&neglist->nl_list);
801 mtx_unlock(&neglist->nl_lock);
804 *neglistpp = neglist;
809 cache_negative_zap_one(void)
811 struct namecache *ncp, *ncp2;
812 struct neglist *neglist;
816 if (mtx_owner(&ncneg_shrink_lock) != NULL ||
817 !mtx_trylock(&ncneg_shrink_lock)) {
818 counter_u64_add(shrinking_skipped, 1);
822 mtx_lock(&ncneg_hot.nl_lock);
823 ncp = TAILQ_FIRST(&ncneg_hot.nl_list);
825 neglist = NCP2NEGLIST(ncp);
826 mtx_lock(&neglist->nl_lock);
827 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
828 TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst);
829 ncp->nc_flag &= ~NCF_HOTNEGATIVE;
831 mtx_unlock(&neglist->nl_lock);
833 mtx_unlock(&ncneg_hot.nl_lock);
835 cache_negative_shrink_select(shrink_list_turn, &ncp, &neglist);
837 if (shrink_list_turn == numneglists)
838 shrink_list_turn = 0;
839 if (ncp == NULL && shrink_list_turn == 0)
840 cache_negative_shrink_select(shrink_list_turn, &ncp, &neglist);
841 mtx_unlock(&ncneg_shrink_lock);
845 MPASS(ncp->nc_flag & NCF_NEGATIVE);
846 dvlp = VP2VNODELOCK(ncp->nc_dvp);
847 blp = NCP2BUCKETLOCK(ncp);
848 mtx_unlock(&neglist->nl_lock);
851 mtx_lock(&neglist->nl_lock);
852 ncp2 = TAILQ_FIRST(&neglist->nl_list);
853 if (ncp != ncp2 || dvlp != VP2VNODELOCK(ncp2->nc_dvp) ||
854 blp != NCP2BUCKETLOCK(ncp2) || !(ncp2->nc_flag & NCF_NEGATIVE)) {
857 SDT_PROBE2(vfs, namecache, shrink_negative, done, ncp->nc_dvp,
860 cache_zap_locked(ncp, true);
861 counter_u64_add(numneg_evicted, 1);
863 mtx_unlock(&neglist->nl_lock);
870 * cache_zap_locked():
872 * Removes a namecache entry from cache, whether it contains an actual
873 * pointer to a vnode or if it is just a negative cache entry.
876 cache_zap_locked(struct namecache *ncp, bool neg_locked)
879 if (!(ncp->nc_flag & NCF_NEGATIVE))
880 cache_assert_vnode_locked(ncp->nc_vp);
881 cache_assert_vnode_locked(ncp->nc_dvp);
882 cache_assert_bucket_locked(ncp, RA_WLOCKED);
884 CTR2(KTR_VFS, "cache_zap(%p) vp %p", ncp,
885 (ncp->nc_flag & NCF_NEGATIVE) ? NULL : ncp->nc_vp);
887 cache_ncp_invalidate(ncp);
889 CK_LIST_REMOVE(ncp, nc_hash);
890 if (!(ncp->nc_flag & NCF_NEGATIVE)) {
891 SDT_PROBE3(vfs, namecache, zap, done, ncp->nc_dvp,
892 ncp->nc_name, ncp->nc_vp);
893 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
894 if (ncp == ncp->nc_vp->v_cache_dd)
895 ncp->nc_vp->v_cache_dd = NULL;
897 SDT_PROBE2(vfs, namecache, zap_negative, done, ncp->nc_dvp,
899 cache_negative_remove(ncp, neg_locked);
901 if (ncp->nc_flag & NCF_ISDOTDOT) {
902 if (ncp == ncp->nc_dvp->v_cache_dd)
903 ncp->nc_dvp->v_cache_dd = NULL;
905 LIST_REMOVE(ncp, nc_src);
906 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) {
907 ncp->nc_flag |= NCF_DVDROP;
908 counter_u64_add(numcachehv, -1);
911 atomic_subtract_rel_long(&numcache, 1);
915 cache_zap_negative_locked_vnode_kl(struct namecache *ncp, struct vnode *vp)
919 MPASS(ncp->nc_dvp == vp);
920 MPASS(ncp->nc_flag & NCF_NEGATIVE);
921 cache_assert_vnode_locked(vp);
923 blp = NCP2BUCKETLOCK(ncp);
925 cache_zap_locked(ncp, false);
930 cache_zap_locked_vnode_kl2(struct namecache *ncp, struct vnode *vp,
933 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
936 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
937 cache_assert_vnode_locked(vp);
939 if (ncp->nc_flag & NCF_NEGATIVE) {
944 cache_zap_negative_locked_vnode_kl(ncp, vp);
948 pvlp = VP2VNODELOCK(vp);
949 blp = NCP2BUCKETLOCK(ncp);
950 vlp1 = VP2VNODELOCK(ncp->nc_dvp);
951 vlp2 = VP2VNODELOCK(ncp->nc_vp);
953 if (*vlpp == vlp1 || *vlpp == vlp2) {
961 cache_sort_vnodes(&vlp1, &vlp2);
966 if (!mtx_trylock(vlp1))
972 cache_zap_locked(ncp, false);
974 if (to_unlock != NULL)
975 mtx_unlock(to_unlock);
982 MPASS(*vlpp == NULL);
987 static int __noinline
988 cache_zap_locked_vnode(struct namecache *ncp, struct vnode *vp)
990 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
994 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
995 cache_assert_vnode_locked(vp);
997 pvlp = VP2VNODELOCK(vp);
998 if (ncp->nc_flag & NCF_NEGATIVE) {
999 cache_zap_negative_locked_vnode_kl(ncp, vp);
1003 blp = NCP2BUCKETLOCK(ncp);
1004 vlp1 = VP2VNODELOCK(ncp->nc_dvp);
1005 vlp2 = VP2VNODELOCK(ncp->nc_vp);
1006 cache_sort_vnodes(&vlp1, &vlp2);
1011 if (!mtx_trylock(vlp1)) {
1018 cache_zap_locked(ncp, false);
1020 mtx_unlock(to_unlock);
1027 * If trylocking failed we can get here. We know enough to take all needed locks
1028 * in the right order and re-lookup the entry.
1031 cache_zap_unlocked_bucket(struct namecache *ncp, struct componentname *cnp,
1032 struct vnode *dvp, struct mtx *dvlp, struct mtx *vlp, uint32_t hash,
1035 struct namecache *rncp;
1037 cache_assert_bucket_locked(ncp, RA_UNLOCKED);
1039 cache_sort_vnodes(&dvlp, &vlp);
1040 cache_lock_vnodes(dvlp, vlp);
1042 CK_LIST_FOREACH(rncp, (NCHHASH(hash)), nc_hash) {
1043 if (rncp == ncp && rncp->nc_dvp == dvp &&
1044 rncp->nc_nlen == cnp->cn_namelen &&
1045 !bcmp(rncp->nc_name, cnp->cn_nameptr, rncp->nc_nlen))
1049 cache_zap_locked(rncp, false);
1051 cache_unlock_vnodes(dvlp, vlp);
1052 counter_u64_add(zap_and_exit_bucket_relock_success, 1);
1057 cache_unlock_vnodes(dvlp, vlp);
1061 static int __noinline
1062 cache_zap_wlocked_bucket(struct namecache *ncp, struct componentname *cnp,
1063 uint32_t hash, struct rwlock *blp)
1065 struct mtx *dvlp, *vlp;
1068 cache_assert_bucket_locked(ncp, RA_WLOCKED);
1070 dvlp = VP2VNODELOCK(ncp->nc_dvp);
1072 if (!(ncp->nc_flag & NCF_NEGATIVE))
1073 vlp = VP2VNODELOCK(ncp->nc_vp);
1074 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1075 cache_zap_locked(ncp, false);
1077 cache_unlock_vnodes(dvlp, vlp);
1083 return (cache_zap_unlocked_bucket(ncp, cnp, dvp, dvlp, vlp, hash, blp));
1086 static int __noinline
1087 cache_zap_rlocked_bucket(struct namecache *ncp, struct componentname *cnp,
1088 uint32_t hash, struct rwlock *blp)
1090 struct mtx *dvlp, *vlp;
1093 cache_assert_bucket_locked(ncp, RA_RLOCKED);
1095 dvlp = VP2VNODELOCK(ncp->nc_dvp);
1097 if (!(ncp->nc_flag & NCF_NEGATIVE))
1098 vlp = VP2VNODELOCK(ncp->nc_vp);
1099 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1102 cache_zap_locked(ncp, false);
1104 cache_unlock_vnodes(dvlp, vlp);
1110 return (cache_zap_unlocked_bucket(ncp, cnp, dvp, dvlp, vlp, hash, blp));
1114 cache_zap_wlocked_bucket_kl(struct namecache *ncp, struct rwlock *blp,
1115 struct mtx **vlpp1, struct mtx **vlpp2)
1117 struct mtx *dvlp, *vlp;
1119 cache_assert_bucket_locked(ncp, RA_WLOCKED);
1121 dvlp = VP2VNODELOCK(ncp->nc_dvp);
1123 if (!(ncp->nc_flag & NCF_NEGATIVE))
1124 vlp = VP2VNODELOCK(ncp->nc_vp);
1125 cache_sort_vnodes(&dvlp, &vlp);
1127 if (*vlpp1 == dvlp && *vlpp2 == vlp) {
1128 cache_zap_locked(ncp, false);
1129 cache_unlock_vnodes(dvlp, vlp);
1142 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1143 cache_zap_locked(ncp, false);
1144 cache_unlock_vnodes(dvlp, vlp);
1159 cache_lookup_unlock(struct rwlock *blp, struct mtx *vlp)
1169 static int __noinline
1170 cache_lookup_dot(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1171 struct timespec *tsp, int *ticksp)
1176 CTR2(KTR_VFS, "cache_lookup(%p, %s) found via .",
1177 dvp, cnp->cn_nameptr);
1178 counter_u64_add(dothits, 1);
1179 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", *vpp);
1186 * When we lookup "." we still can be asked to lock it
1189 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
1190 if (ltype != VOP_ISLOCKED(*vpp)) {
1191 if (ltype == LK_EXCLUSIVE) {
1192 vn_lock(*vpp, LK_UPGRADE | LK_RETRY);
1193 if (VN_IS_DOOMED((*vpp))) {
1194 /* forced unmount */
1200 vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY);
1205 static __noinline int
1206 cache_lookup_nomakeentry(struct vnode *dvp, struct vnode **vpp,
1207 struct componentname *cnp, struct timespec *tsp, int *ticksp)
1209 struct namecache *ncp;
1211 struct mtx *dvlp, *dvlp2;
1215 if (cnp->cn_namelen == 2 &&
1216 cnp->cn_nameptr[0] == '.' && cnp->cn_nameptr[1] == '.') {
1217 counter_u64_add(dotdothits, 1);
1218 dvlp = VP2VNODELOCK(dvp);
1222 ncp = dvp->v_cache_dd;
1224 SDT_PROBE3(vfs, namecache, lookup, miss, dvp,
1231 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1232 if (ncp->nc_dvp != dvp)
1233 panic("dvp %p v_cache_dd %p\n", dvp, ncp);
1234 if (!cache_zap_locked_vnode_kl2(ncp,
1237 MPASS(dvp->v_cache_dd == NULL);
1243 dvp->v_cache_dd = NULL;
1251 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1252 blp = HASH2BUCKETLOCK(hash);
1254 if (CK_LIST_EMPTY(NCHHASH(hash)))
1259 CK_LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1260 counter_u64_add(numchecks, 1);
1261 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1262 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1266 /* We failed to find an entry */
1272 error = cache_zap_wlocked_bucket(ncp, cnp, hash, blp);
1273 if (__predict_false(error != 0)) {
1274 zap_and_exit_bucket_fail++;
1275 cache_maybe_yield();
1278 counter_u64_add(numposzaps, 1);
1282 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr, NULL);
1283 counter_u64_add(nummisszap, 1);
1288 * Lookup a name in the name cache
1292 * - dvp: Parent directory in which to search.
1293 * - vpp: Return argument. Will contain desired vnode on cache hit.
1294 * - cnp: Parameters of the name search. The most interesting bits of
1295 * the cn_flags field have the following meanings:
1296 * - MAKEENTRY: If clear, free an entry from the cache rather than look
1298 * - ISDOTDOT: Must be set if and only if cn_nameptr == ".."
1299 * - tsp: Return storage for cache timestamp. On a successful (positive
1300 * or negative) lookup, tsp will be filled with any timespec that
1301 * was stored when this cache entry was created. However, it will
1302 * be clear for "." entries.
1303 * - ticks: Return storage for alternate cache timestamp. On a successful
1304 * (positive or negative) lookup, it will contain the ticks value
1305 * that was current when the cache entry was created, unless cnp
1310 * - -1: A positive cache hit. vpp will contain the desired vnode.
1311 * - ENOENT: A negative cache hit, or dvp was recycled out from under us due
1312 * to a forced unmount. vpp will not be modified. If the entry
1313 * is a whiteout, then the ISWHITEOUT flag will be set in
1315 * - 0: A cache miss. vpp will not be modified.
1319 * On a cache hit, vpp will be returned locked and ref'd. If we're looking up
1320 * .., dvp is unlocked. If we're looking up . an extra ref is taken, but the
1321 * lock is not recursively acquired.
1324 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1325 struct timespec *tsp, int *ticksp)
1327 struct namecache_ts *ncp_ts;
1328 struct namecache *ncp;
1334 bool try_smr, doing_smr;
1337 if (__predict_false(!doingcache)) {
1338 cnp->cn_flags &= ~MAKEENTRY;
1343 counter_u64_add(numcalls, 1);
1345 if (__predict_false(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.'))
1346 return (cache_lookup_dot(dvp, vpp, cnp, tsp, ticksp));
1348 if ((cnp->cn_flags & MAKEENTRY) == 0)
1349 return (cache_lookup_nomakeentry(dvp, vpp, cnp, tsp, ticksp));
1352 if (cnp->cn_nameiop == CREATE)
1359 if (cnp->cn_namelen == 2 &&
1360 cnp->cn_nameptr[0] == '.' && cnp->cn_nameptr[1] == '.') {
1361 counter_u64_add(dotdothits, 1);
1362 dvlp = VP2VNODELOCK(dvp);
1364 ncp = dvp->v_cache_dd;
1366 SDT_PROBE3(vfs, namecache, lookup, miss, dvp,
1371 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1372 if (ncp->nc_flag & NCF_NEGATIVE)
1378 /* Return failure if negative entry was found. */
1380 goto negative_success;
1381 CTR3(KTR_VFS, "cache_lookup(%p, %s) found %p via ..",
1382 dvp, cnp->cn_nameptr, *vpp);
1383 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, "..",
1385 cache_out_ts(ncp, tsp, ticksp);
1386 if ((ncp->nc_flag & (NCF_ISDOTDOT | NCF_DTS)) ==
1387 NCF_DTS && tsp != NULL) {
1388 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1389 *tsp = ncp_ts->nc_dotdottime;
1394 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1401 blp = HASH2BUCKETLOCK(hash);
1405 CK_LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1406 counter_u64_add(numchecks, 1);
1407 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1408 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1412 /* We failed to find an entry */
1413 if (__predict_false(ncp == NULL)) {
1418 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
1420 counter_u64_add(nummiss, 1);
1424 if (ncp->nc_flag & NCF_NEGATIVE)
1425 goto negative_success;
1427 /* We found a "positive" match, return the vnode */
1428 counter_u64_add(numposhits, 1);
1430 CTR4(KTR_VFS, "cache_lookup(%p, %s) found %p via ncp %p",
1431 dvp, cnp->cn_nameptr, *vpp, ncp);
1432 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name,
1434 cache_out_ts(ncp, tsp, ticksp);
1437 * On success we return a locked and ref'd vnode as per the lookup
1441 ltype = 0; /* silence gcc warning */
1442 if (cnp->cn_flags & ISDOTDOT) {
1443 ltype = VOP_ISLOCKED(dvp);
1447 if (cache_ncp_invalid(ncp)) {
1452 vs = vget_prep_smr(*vpp);
1454 if (vs == VGET_NONE) {
1459 vs = vget_prep(*vpp);
1460 cache_lookup_unlock(blp, dvlp);
1462 error = vget_finish(*vpp, cnp->cn_lkflags, vs);
1463 if (cnp->cn_flags & ISDOTDOT) {
1464 vn_lock(dvp, ltype | LK_RETRY);
1465 if (VN_IS_DOOMED(dvp)) {
1476 if ((cnp->cn_flags & ISLASTCN) &&
1477 (cnp->cn_lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE) {
1478 ASSERT_VOP_ELOCKED(*vpp, "cache_lookup");
1483 /* We found a negative match, and want to create it, so purge */
1484 if (cnp->cn_nameiop == CREATE) {
1486 counter_u64_add(numnegzaps, 1);
1491 if ((ncp->nc_flag & NCF_HOTNEGATIVE) == 0) {
1493 * We need to take locks to promote the entry.
1500 cache_negative_hit(ncp);
1502 counter_u64_add(numneghits, 1);
1503 if (ncp->nc_flag & NCF_WHITE)
1504 cnp->cn_flags |= ISWHITEOUT;
1505 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp,
1507 cache_out_ts(ncp, tsp, ticksp);
1511 cache_lookup_unlock(blp, dvlp);
1517 error = cache_zap_rlocked_bucket(ncp, cnp, hash, blp);
1519 error = cache_zap_locked_vnode(ncp, dvp);
1520 if (__predict_false(error != 0)) {
1521 zap_and_exit_bucket_fail2++;
1522 cache_maybe_yield();
1529 struct celockstate {
1531 struct rwlock *blp[2];
1533 CTASSERT((nitems(((struct celockstate *)0)->vlp) == 3));
1534 CTASSERT((nitems(((struct celockstate *)0)->blp) == 2));
1537 cache_celockstate_init(struct celockstate *cel)
1540 bzero(cel, sizeof(*cel));
1544 cache_lock_vnodes_cel(struct celockstate *cel, struct vnode *vp,
1547 struct mtx *vlp1, *vlp2;
1549 MPASS(cel->vlp[0] == NULL);
1550 MPASS(cel->vlp[1] == NULL);
1551 MPASS(cel->vlp[2] == NULL);
1553 MPASS(vp != NULL || dvp != NULL);
1555 vlp1 = VP2VNODELOCK(vp);
1556 vlp2 = VP2VNODELOCK(dvp);
1557 cache_sort_vnodes(&vlp1, &vlp2);
1568 cache_unlock_vnodes_cel(struct celockstate *cel)
1571 MPASS(cel->vlp[0] != NULL || cel->vlp[1] != NULL);
1573 if (cel->vlp[0] != NULL)
1574 mtx_unlock(cel->vlp[0]);
1575 if (cel->vlp[1] != NULL)
1576 mtx_unlock(cel->vlp[1]);
1577 if (cel->vlp[2] != NULL)
1578 mtx_unlock(cel->vlp[2]);
1582 cache_lock_vnodes_cel_3(struct celockstate *cel, struct vnode *vp)
1587 cache_assert_vlp_locked(cel->vlp[0]);
1588 cache_assert_vlp_locked(cel->vlp[1]);
1589 MPASS(cel->vlp[2] == NULL);
1592 vlp = VP2VNODELOCK(vp);
1595 if (vlp >= cel->vlp[1]) {
1598 if (mtx_trylock(vlp))
1600 cache_lock_vnodes_cel_3_failures++;
1601 cache_unlock_vnodes_cel(cel);
1602 if (vlp < cel->vlp[0]) {
1604 mtx_lock(cel->vlp[0]);
1605 mtx_lock(cel->vlp[1]);
1607 if (cel->vlp[0] != NULL)
1608 mtx_lock(cel->vlp[0]);
1610 mtx_lock(cel->vlp[1]);
1620 cache_lock_buckets_cel(struct celockstate *cel, struct rwlock *blp1,
1621 struct rwlock *blp2)
1624 MPASS(cel->blp[0] == NULL);
1625 MPASS(cel->blp[1] == NULL);
1627 cache_sort_vnodes(&blp1, &blp2);
1638 cache_unlock_buckets_cel(struct celockstate *cel)
1641 if (cel->blp[0] != NULL)
1642 rw_wunlock(cel->blp[0]);
1643 rw_wunlock(cel->blp[1]);
1647 * Lock part of the cache affected by the insertion.
1649 * This means vnodelocks for dvp, vp and the relevant bucketlock.
1650 * However, insertion can result in removal of an old entry. In this
1651 * case we have an additional vnode and bucketlock pair to lock. If the
1652 * entry is negative, ncelock is locked instead of the vnode.
1654 * That is, in the worst case we have to lock 3 vnodes and 2 bucketlocks, while
1655 * preserving the locking order (smaller address first).
1658 cache_enter_lock(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1661 struct namecache *ncp;
1662 struct rwlock *blps[2];
1664 blps[0] = HASH2BUCKETLOCK(hash);
1667 cache_lock_vnodes_cel(cel, dvp, vp);
1668 if (vp == NULL || vp->v_type != VDIR)
1670 ncp = vp->v_cache_dd;
1673 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1675 MPASS(ncp->nc_dvp == vp);
1676 blps[1] = NCP2BUCKETLOCK(ncp);
1677 if (ncp->nc_flag & NCF_NEGATIVE)
1679 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1682 * All vnodes got re-locked. Re-validate the state and if
1683 * nothing changed we are done. Otherwise restart.
1685 if (ncp == vp->v_cache_dd &&
1686 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1687 blps[1] == NCP2BUCKETLOCK(ncp) &&
1688 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1690 cache_unlock_vnodes_cel(cel);
1695 cache_lock_buckets_cel(cel, blps[0], blps[1]);
1699 cache_enter_lock_dd(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1702 struct namecache *ncp;
1703 struct rwlock *blps[2];
1705 blps[0] = HASH2BUCKETLOCK(hash);
1708 cache_lock_vnodes_cel(cel, dvp, vp);
1709 ncp = dvp->v_cache_dd;
1712 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1714 MPASS(ncp->nc_dvp == dvp);
1715 blps[1] = NCP2BUCKETLOCK(ncp);
1716 if (ncp->nc_flag & NCF_NEGATIVE)
1718 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1720 if (ncp == dvp->v_cache_dd &&
1721 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1722 blps[1] == NCP2BUCKETLOCK(ncp) &&
1723 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1725 cache_unlock_vnodes_cel(cel);
1730 cache_lock_buckets_cel(cel, blps[0], blps[1]);
1734 cache_enter_unlock(struct celockstate *cel)
1737 cache_unlock_buckets_cel(cel);
1738 cache_unlock_vnodes_cel(cel);
1741 static void __noinline
1742 cache_enter_dotdot_prep(struct vnode *dvp, struct vnode *vp,
1743 struct componentname *cnp)
1745 struct celockstate cel;
1746 struct namecache *ncp;
1750 if (dvp->v_cache_dd == NULL)
1752 len = cnp->cn_namelen;
1753 cache_celockstate_init(&cel);
1754 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1755 cache_enter_lock_dd(&cel, dvp, vp, hash);
1756 ncp = dvp->v_cache_dd;
1757 if (ncp != NULL && (ncp->nc_flag & NCF_ISDOTDOT)) {
1758 KASSERT(ncp->nc_dvp == dvp, ("wrong isdotdot parent"));
1759 cache_zap_locked(ncp, false);
1763 dvp->v_cache_dd = NULL;
1764 cache_enter_unlock(&cel);
1769 * Add an entry to the cache.
1772 cache_enter_time(struct vnode *dvp, struct vnode *vp, struct componentname *cnp,
1773 struct timespec *tsp, struct timespec *dtsp)
1775 struct celockstate cel;
1776 struct namecache *ncp, *n2, *ndd;
1777 struct namecache_ts *ncp_ts, *n2_ts;
1778 struct nchashhead *ncpp;
1784 CTR3(KTR_VFS, "cache_enter(%p, %p, %s)", dvp, vp, cnp->cn_nameptr);
1785 VNASSERT(vp == NULL || !VN_IS_DOOMED(vp), vp,
1786 ("cache_enter: Adding a doomed vnode"));
1787 VNASSERT(dvp == NULL || !VN_IS_DOOMED(dvp), dvp,
1788 ("cache_enter: Doomed vnode used as src"));
1791 if (__predict_false(!doingcache))
1796 if (__predict_false(cnp->cn_nameptr[0] == '.')) {
1797 if (cnp->cn_namelen == 1)
1799 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
1800 cache_enter_dotdot_prep(dvp, vp, cnp);
1801 flag = NCF_ISDOTDOT;
1806 * Avoid blowout in namecache entries.
1808 lnumcache = atomic_fetchadd_long(&numcache, 1) + 1;
1809 if (__predict_false(lnumcache >= ncsize)) {
1810 atomic_add_long(&numcache, -1);
1814 cache_celockstate_init(&cel);
1819 * Calculate the hash key and setup as much of the new
1820 * namecache entry as possible before acquiring the lock.
1822 ncp = cache_alloc(cnp->cn_namelen, tsp != NULL);
1823 ncp->nc_flag = flag;
1826 ncp->nc_flag |= NCF_NEGATIVE;
1829 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1830 ncp_ts->nc_time = *tsp;
1831 ncp_ts->nc_ticks = ticks;
1832 ncp_ts->nc_nc.nc_flag |= NCF_TS;
1834 ncp_ts->nc_dotdottime = *dtsp;
1835 ncp_ts->nc_nc.nc_flag |= NCF_DTS;
1838 len = ncp->nc_nlen = cnp->cn_namelen;
1839 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1840 strlcpy(ncp->nc_name, cnp->cn_nameptr, len + 1);
1841 cache_enter_lock(&cel, dvp, vp, hash);
1844 * See if this vnode or negative entry is already in the cache
1845 * with this name. This can happen with concurrent lookups of
1846 * the same path name.
1848 ncpp = NCHHASH(hash);
1849 CK_LIST_FOREACH(n2, ncpp, nc_hash) {
1850 if (n2->nc_dvp == dvp &&
1851 n2->nc_nlen == cnp->cn_namelen &&
1852 !bcmp(n2->nc_name, cnp->cn_nameptr, n2->nc_nlen)) {
1854 KASSERT((n2->nc_flag & NCF_TS) != 0,
1856 n2_ts = __containerof(n2, struct namecache_ts, nc_nc);
1857 n2_ts->nc_time = ncp_ts->nc_time;
1858 n2_ts->nc_ticks = ncp_ts->nc_ticks;
1860 n2_ts->nc_dotdottime = ncp_ts->nc_dotdottime;
1861 if (ncp->nc_flag & NCF_NEGATIVE)
1862 mtx_lock(&ncneg_hot.nl_lock);
1863 n2_ts->nc_nc.nc_flag |= NCF_DTS;
1864 if (ncp->nc_flag & NCF_NEGATIVE)
1865 mtx_unlock(&ncneg_hot.nl_lock);
1868 goto out_unlock_free;
1872 if (flag == NCF_ISDOTDOT) {
1874 * See if we are trying to add .. entry, but some other lookup
1875 * has populated v_cache_dd pointer already.
1877 if (dvp->v_cache_dd != NULL)
1878 goto out_unlock_free;
1879 KASSERT(vp == NULL || vp->v_type == VDIR,
1880 ("wrong vnode type %p", vp));
1881 dvp->v_cache_dd = ncp;
1885 if (vp->v_type == VDIR) {
1886 if (flag != NCF_ISDOTDOT) {
1888 * For this case, the cache entry maps both the
1889 * directory name in it and the name ".." for the
1890 * directory's parent.
1892 if ((ndd = vp->v_cache_dd) != NULL) {
1893 if ((ndd->nc_flag & NCF_ISDOTDOT) != 0)
1894 cache_zap_locked(ndd, false);
1898 vp->v_cache_dd = ncp;
1901 vp->v_cache_dd = NULL;
1905 if (flag != NCF_ISDOTDOT) {
1906 if (LIST_EMPTY(&dvp->v_cache_src)) {
1908 counter_u64_add(numcachehv, 1);
1910 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
1914 * If the entry is "negative", we place it into the
1915 * "negative" cache queue, otherwise, we place it into the
1916 * destination vnode's cache entries queue.
1919 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
1920 SDT_PROBE3(vfs, namecache, enter, done, dvp, ncp->nc_name,
1923 if (cnp->cn_flags & ISWHITEOUT)
1924 ncp->nc_flag |= NCF_WHITE;
1925 cache_negative_insert(ncp, false);
1926 SDT_PROBE2(vfs, namecache, enter_negative, done, dvp,
1930 atomic_thread_fence_rel();
1932 * Insert the new namecache entry into the appropriate chain
1933 * within the cache entries table.
1935 CK_LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
1937 cache_enter_unlock(&cel);
1938 if (numneg * ncnegfactor > lnumcache)
1939 cache_negative_zap_one();
1943 cache_enter_unlock(&cel);
1949 cache_roundup_2(u_int val)
1953 for (res = 1; res <= val; res <<= 1)
1960 * Name cache initialization, from vfs_init() when we are booting
1963 nchinit(void *dummy __unused)
1967 cache_zone_small = uma_zcreate("S VFS Cache",
1968 sizeof(struct namecache) + CACHE_PATH_CUTOFF + 1,
1969 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache),
1971 cache_zone_small_ts = uma_zcreate("STS VFS Cache",
1972 sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1,
1973 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache_ts),
1975 cache_zone_large = uma_zcreate("L VFS Cache",
1976 sizeof(struct namecache) + NAME_MAX + 1,
1977 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache),
1979 cache_zone_large_ts = uma_zcreate("LTS VFS Cache",
1980 sizeof(struct namecache_ts) + NAME_MAX + 1,
1981 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache_ts),
1984 VFS_SMR_ZONE_SET(cache_zone_small);
1985 VFS_SMR_ZONE_SET(cache_zone_small_ts);
1986 VFS_SMR_ZONE_SET(cache_zone_large);
1987 VFS_SMR_ZONE_SET(cache_zone_large_ts);
1989 ncsize = desiredvnodes * ncsizefactor;
1990 nchashtbl = hashinit(desiredvnodes * 2, M_VFSCACHE, &nchash);
1991 ncbuckethash = cache_roundup_2(mp_ncpus * mp_ncpus) - 1;
1992 if (ncbuckethash < 7) /* arbitrarily chosen to avoid having one lock */
1994 if (ncbuckethash > nchash)
1995 ncbuckethash = nchash;
1996 bucketlocks = malloc(sizeof(*bucketlocks) * numbucketlocks, M_VFSCACHE,
1998 for (i = 0; i < numbucketlocks; i++)
1999 rw_init_flags(&bucketlocks[i], "ncbuc", RW_DUPOK | RW_RECURSE);
2000 ncvnodehash = ncbuckethash;
2001 vnodelocks = malloc(sizeof(*vnodelocks) * numvnodelocks, M_VFSCACHE,
2003 for (i = 0; i < numvnodelocks; i++)
2004 mtx_init(&vnodelocks[i], "ncvn", NULL, MTX_DUPOK | MTX_RECURSE);
2005 ncpurgeminvnodes = numbucketlocks * 2;
2008 neglists = malloc(sizeof(*neglists) * numneglists, M_VFSCACHE,
2010 for (i = 0; i < numneglists; i++) {
2011 mtx_init(&neglists[i].nl_lock, "ncnegl", NULL, MTX_DEF);
2012 TAILQ_INIT(&neglists[i].nl_list);
2014 mtx_init(&ncneg_hot.nl_lock, "ncneglh", NULL, MTX_DEF);
2015 TAILQ_INIT(&ncneg_hot.nl_list);
2017 mtx_init(&ncneg_shrink_lock, "ncnegs", NULL, MTX_DEF);
2019 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL);
2022 cache_changesize(u_long newmaxvnodes)
2024 struct nchashhead *new_nchashtbl, *old_nchashtbl;
2025 u_long new_nchash, old_nchash;
2026 struct namecache *ncp;
2031 newncsize = newmaxvnodes * ncsizefactor;
2032 newmaxvnodes = cache_roundup_2(newmaxvnodes * 2);
2033 if (newmaxvnodes < numbucketlocks)
2034 newmaxvnodes = numbucketlocks;
2036 new_nchashtbl = hashinit(newmaxvnodes, M_VFSCACHE, &new_nchash);
2037 /* If same hash table size, nothing to do */
2038 if (nchash == new_nchash) {
2039 free(new_nchashtbl, M_VFSCACHE);
2043 * Move everything from the old hash table to the new table.
2044 * None of the namecache entries in the table can be removed
2045 * because to do so, they have to be removed from the hash table.
2047 cache_lock_all_vnodes();
2048 cache_lock_all_buckets();
2049 old_nchashtbl = nchashtbl;
2050 old_nchash = nchash;
2051 nchashtbl = new_nchashtbl;
2052 nchash = new_nchash;
2053 for (i = 0; i <= old_nchash; i++) {
2054 while ((ncp = CK_LIST_FIRST(&old_nchashtbl[i])) != NULL) {
2055 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen,
2057 CK_LIST_REMOVE(ncp, nc_hash);
2058 CK_LIST_INSERT_HEAD(NCHHASH(hash), ncp, nc_hash);
2062 cache_unlock_all_buckets();
2063 cache_unlock_all_vnodes();
2064 free(old_nchashtbl, M_VFSCACHE);
2068 * Invalidate all entries from and to a particular vnode.
2071 cache_purge(struct vnode *vp)
2073 TAILQ_HEAD(, namecache) ncps;
2074 struct namecache *ncp, *nnp;
2075 struct mtx *vlp, *vlp2;
2077 CTR1(KTR_VFS, "cache_purge(%p)", vp);
2078 SDT_PROBE1(vfs, namecache, purge, done, vp);
2079 if (LIST_EMPTY(&vp->v_cache_src) && TAILQ_EMPTY(&vp->v_cache_dst) &&
2080 vp->v_cache_dd == NULL)
2083 vlp = VP2VNODELOCK(vp);
2087 while (!LIST_EMPTY(&vp->v_cache_src)) {
2088 ncp = LIST_FIRST(&vp->v_cache_src);
2089 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2091 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2093 while (!TAILQ_EMPTY(&vp->v_cache_dst)) {
2094 ncp = TAILQ_FIRST(&vp->v_cache_dst);
2095 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2097 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2099 ncp = vp->v_cache_dd;
2101 KASSERT(ncp->nc_flag & NCF_ISDOTDOT,
2102 ("lost dotdot link"));
2103 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2105 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2107 KASSERT(vp->v_cache_dd == NULL, ("incomplete purge"));
2111 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2117 * Invalidate all negative entries for a particular directory vnode.
2120 cache_purge_negative(struct vnode *vp)
2122 TAILQ_HEAD(, namecache) ncps;
2123 struct namecache *ncp, *nnp;
2126 CTR1(KTR_VFS, "cache_purge_negative(%p)", vp);
2127 SDT_PROBE1(vfs, namecache, purge_negative, done, vp);
2128 if (LIST_EMPTY(&vp->v_cache_src))
2131 vlp = VP2VNODELOCK(vp);
2133 LIST_FOREACH_SAFE(ncp, &vp->v_cache_src, nc_src, nnp) {
2134 if (!(ncp->nc_flag & NCF_NEGATIVE))
2136 cache_zap_negative_locked_vnode_kl(ncp, vp);
2137 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2140 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2146 * Flush all entries referencing a particular filesystem.
2149 cache_purgevfs(struct mount *mp, bool force)
2151 TAILQ_HEAD(, namecache) ncps;
2152 struct mtx *vlp1, *vlp2;
2154 struct nchashhead *bucket;
2155 struct namecache *ncp, *nnp;
2156 u_long i, j, n_nchash;
2159 /* Scan hash tables for applicable entries */
2160 SDT_PROBE1(vfs, namecache, purgevfs, done, mp);
2161 if (!force && mp->mnt_nvnodelistsize <= ncpurgeminvnodes)
2164 n_nchash = nchash + 1;
2166 for (i = 0; i < numbucketlocks; i++) {
2167 blp = (struct rwlock *)&bucketlocks[i];
2169 for (j = i; j < n_nchash; j += numbucketlocks) {
2171 bucket = &nchashtbl[j];
2172 CK_LIST_FOREACH_SAFE(ncp, bucket, nc_hash, nnp) {
2173 cache_assert_bucket_locked(ncp, RA_WLOCKED);
2174 if (ncp->nc_dvp->v_mount != mp)
2176 error = cache_zap_wlocked_bucket_kl(ncp, blp,
2180 TAILQ_INSERT_HEAD(&ncps, ncp, nc_dst);
2184 if (vlp1 == NULL && vlp2 == NULL)
2185 cache_maybe_yield();
2192 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2198 * Perform canonical checks and cache lookup and pass on to filesystem
2199 * through the vop_cachedlookup only if needed.
2203 vfs_cache_lookup(struct vop_lookup_args *ap)
2207 struct vnode **vpp = ap->a_vpp;
2208 struct componentname *cnp = ap->a_cnp;
2209 int flags = cnp->cn_flags;
2214 if (dvp->v_type != VDIR)
2217 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
2218 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
2221 error = vn_dir_check_exec(dvp, cnp);
2225 error = cache_lookup(dvp, vpp, cnp, NULL, NULL);
2227 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
2233 /* Implementation of the getcwd syscall. */
2235 sys___getcwd(struct thread *td, struct __getcwd_args *uap)
2241 buflen = uap->buflen;
2242 if (__predict_false(buflen < 2))
2244 if (buflen > MAXPATHLEN)
2245 buflen = MAXPATHLEN;
2247 buf = malloc(buflen, M_TEMP, M_WAITOK);
2248 error = vn_getcwd(td, buf, &retbuf, &buflen);
2250 error = copyout(retbuf, uap->buf, buflen);
2256 vn_getcwd(struct thread *td, char *buf, char **retbuf, size_t *buflen)
2262 error = vn_fullpath_any(td, pwd->pwd_cdir, pwd->pwd_rdir, buf, retbuf, buflen);
2266 if (KTRPOINT(curthread, KTR_NAMEI) && error == 0)
2273 kern___realpathat(struct thread *td, int fd, const char *path, char *buf,
2274 size_t size, int flags, enum uio_seg pathseg)
2276 struct nameidata nd;
2277 char *retbuf, *freebuf;
2282 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | SAVENAME | WANTPARENT | AUDITVNODE1,
2283 pathseg, path, fd, &cap_fstat_rights, td);
2284 if ((error = namei(&nd)) != 0)
2286 error = vn_fullpath_hardlink(td, &nd, &retbuf, &freebuf, &size);
2288 error = copyout(retbuf, buf, size);
2289 free(freebuf, M_TEMP);
2296 sys___realpathat(struct thread *td, struct __realpathat_args *uap)
2299 return (kern___realpathat(td, uap->fd, uap->path, uap->buf, uap->size,
2300 uap->flags, UIO_USERSPACE));
2304 * Retrieve the full filesystem path that correspond to a vnode from the name
2305 * cache (if available)
2308 vn_fullpath(struct thread *td, struct vnode *vn, char **retbuf, char **freebuf)
2315 if (__predict_false(vn == NULL))
2318 buflen = MAXPATHLEN;
2319 buf = malloc(buflen, M_TEMP, M_WAITOK);
2321 error = vn_fullpath_any(td, vn, pwd->pwd_rdir, buf, retbuf, &buflen);
2332 * This function is similar to vn_fullpath, but it attempts to lookup the
2333 * pathname relative to the global root mount point. This is required for the
2334 * auditing sub-system, as audited pathnames must be absolute, relative to the
2335 * global root mount point.
2338 vn_fullpath_global(struct thread *td, struct vnode *vn,
2339 char **retbuf, char **freebuf)
2345 if (__predict_false(vn == NULL))
2347 buflen = MAXPATHLEN;
2348 buf = malloc(buflen, M_TEMP, M_WAITOK);
2349 error = vn_fullpath_any(td, vn, rootvnode, buf, retbuf, &buflen);
2358 vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, size_t *buflen)
2361 struct namecache *ncp;
2365 vlp = VP2VNODELOCK(*vp);
2367 TAILQ_FOREACH(ncp, &((*vp)->v_cache_dst), nc_dst) {
2368 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2372 if (*buflen < ncp->nc_nlen) {
2375 counter_u64_add(numfullpathfail4, 1);
2377 SDT_PROBE3(vfs, namecache, fullpath, return, error,
2381 *buflen -= ncp->nc_nlen;
2382 memcpy(buf + *buflen, ncp->nc_name, ncp->nc_nlen);
2383 SDT_PROBE3(vfs, namecache, fullpath, hit, ncp->nc_dvp,
2392 SDT_PROBE1(vfs, namecache, fullpath, miss, vp);
2395 vn_lock(*vp, LK_SHARED | LK_RETRY);
2396 error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen);
2399 counter_u64_add(numfullpathfail2, 1);
2400 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2405 if (VN_IS_DOOMED(dvp)) {
2406 /* forced unmount */
2409 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2413 * *vp has its use count incremented still.
2420 * Resolve a directory to a pathname.
2422 * The name of the directory can always be found in the namecache or fetched
2423 * from the filesystem. There is also guaranteed to be only one parent, meaning
2424 * we can just follow vnodes up until we find the root.
2426 * The vnode must be referenced.
2429 vn_fullpath_dir(struct thread *td, struct vnode *vp, struct vnode *rdir,
2430 char *buf, char **retbuf, size_t *len, bool slash_prefixed, size_t addend)
2432 #ifdef KDTRACE_HOOKS
2433 struct vnode *startvp = vp;
2439 VNPASS(vp->v_type == VDIR || VN_IS_DOOMED(vp), vp);
2440 VNPASS(vp->v_usecount > 0, vp);
2444 if (!slash_prefixed) {
2452 SDT_PROBE1(vfs, namecache, fullpath, entry, vp);
2453 counter_u64_add(numfullpathcalls, 1);
2454 while (vp != rdir && vp != rootvnode) {
2456 * The vp vnode must be already fully constructed,
2457 * since it is either found in namecache or obtained
2458 * from VOP_VPTOCNP(). We may test for VV_ROOT safely
2459 * without obtaining the vnode lock.
2461 if ((vp->v_vflag & VV_ROOT) != 0) {
2462 vn_lock(vp, LK_RETRY | LK_SHARED);
2465 * With the vnode locked, check for races with
2466 * unmount, forced or not. Note that we
2467 * already verified that vp is not equal to
2468 * the root vnode, which means that
2469 * mnt_vnodecovered can be NULL only for the
2472 if (VN_IS_DOOMED(vp) ||
2473 (vp1 = vp->v_mount->mnt_vnodecovered) == NULL ||
2474 vp1->v_mountedhere != vp->v_mount) {
2477 SDT_PROBE3(vfs, namecache, fullpath, return,
2487 if (vp->v_type != VDIR) {
2489 counter_u64_add(numfullpathfail1, 1);
2491 SDT_PROBE3(vfs, namecache, fullpath, return,
2495 error = vn_vptocnp(&vp, td->td_ucred, buf, &buflen);
2501 SDT_PROBE3(vfs, namecache, fullpath, return, error,
2505 buf[--buflen] = '/';
2506 slash_prefixed = true;
2510 if (!slash_prefixed) {
2513 counter_u64_add(numfullpathfail4, 1);
2514 SDT_PROBE3(vfs, namecache, fullpath, return, ENOMEM,
2518 buf[--buflen] = '/';
2520 counter_u64_add(numfullpathfound, 1);
2523 *retbuf = buf + buflen;
2524 SDT_PROBE3(vfs, namecache, fullpath, return, 0, startvp, *retbuf);
2531 * Resolve an arbitrary vnode to a pathname.
2534 * - hardlinks are not tracked, thus if the vnode is not a directory this can
2535 * resolve to a different path than the one used to find it
2536 * - namecache is not mandatory, meaning names are not guaranteed to be added
2537 * (in which case resolving fails)
2540 vn_fullpath_any(struct thread *td, struct vnode *vp, struct vnode *rdir,
2541 char *buf, char **retbuf, size_t *buflen)
2544 bool slash_prefixed;
2550 orig_buflen = *buflen;
2553 slash_prefixed = false;
2554 if (vp->v_type != VDIR) {
2556 buf[*buflen] = '\0';
2557 error = vn_vptocnp(&vp, td->td_ucred, buf, buflen);
2566 slash_prefixed = true;
2569 return (vn_fullpath_dir(td, vp, rdir, buf, retbuf, buflen, slash_prefixed,
2570 orig_buflen - *buflen));
2574 * Resolve an arbitrary vnode to a pathname (taking care of hardlinks).
2576 * Since the namecache does not track handlings, the caller is expected to first
2577 * look up the target vnode with SAVENAME | WANTPARENT flags passed to namei.
2579 * Then we have 2 cases:
2580 * - if the found vnode is a directory, the path can be constructed just by
2581 * fullowing names up the chain
2582 * - otherwise we populate the buffer with the saved name and start resolving
2586 vn_fullpath_hardlink(struct thread *td, struct nameidata *ndp, char **retbuf,
2587 char **freebuf, size_t *buflen)
2591 struct componentname *cnp;
2595 bool slash_prefixed;
2599 if (*buflen > MAXPATHLEN)
2600 *buflen = MAXPATHLEN;
2602 slash_prefixed = false;
2604 buf = malloc(*buflen, M_TEMP, M_WAITOK);
2609 if (vp->v_type != VDIR) {
2611 addend = cnp->cn_namelen + 2;
2612 if (*buflen < addend) {
2617 tmpbuf = buf + *buflen;
2619 memcpy(&tmpbuf[1], cnp->cn_nameptr, cnp->cn_namelen);
2620 tmpbuf[addend - 1] = '\0';
2621 slash_prefixed = true;
2626 error = vn_fullpath_dir(td, vp, pwd->pwd_rdir, buf, retbuf, buflen,
2627 slash_prefixed, addend);
2642 vn_dir_dd_ino(struct vnode *vp)
2644 struct namecache *ncp;
2649 ASSERT_VOP_LOCKED(vp, "vn_dir_dd_ino");
2650 vlp = VP2VNODELOCK(vp);
2652 TAILQ_FOREACH(ncp, &(vp->v_cache_dst), nc_dst) {
2653 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0)
2656 vs = vget_prep(ddvp);
2658 if (vget_finish(ddvp, LK_SHARED | LK_NOWAIT, vs))
2667 vn_commname(struct vnode *vp, char *buf, u_int buflen)
2669 struct namecache *ncp;
2673 vlp = VP2VNODELOCK(vp);
2675 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst)
2676 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2682 l = min(ncp->nc_nlen, buflen - 1);
2683 memcpy(buf, ncp->nc_name, l);
2690 * This function updates path string to vnode's full global path
2691 * and checks the size of the new path string against the pathlen argument.
2693 * Requires a locked, referenced vnode.
2694 * Vnode is re-locked on success or ENODEV, otherwise unlocked.
2696 * If vp is a directory, the call to vn_fullpath_global() always succeeds
2697 * because it falls back to the ".." lookup if the namecache lookup fails.
2700 vn_path_to_global_path(struct thread *td, struct vnode *vp, char *path,
2703 struct nameidata nd;
2708 ASSERT_VOP_ELOCKED(vp, __func__);
2710 /* Construct global filesystem path from vp. */
2712 error = vn_fullpath_global(td, vp, &rpath, &fbuf);
2719 if (strlen(rpath) >= pathlen) {
2721 error = ENAMETOOLONG;
2726 * Re-lookup the vnode by path to detect a possible rename.
2727 * As a side effect, the vnode is relocked.
2728 * If vnode was renamed, return ENOENT.
2730 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
2731 UIO_SYSSPACE, path, td);
2737 NDFREE(&nd, NDF_ONLY_PNBUF);
2741 strcpy(path, rpath);
2754 db_print_vpath(struct vnode *vp)
2757 while (vp != NULL) {
2758 db_printf("%p: ", vp);
2759 if (vp == rootvnode) {
2763 if (vp->v_vflag & VV_ROOT) {
2764 db_printf("<mount point>");
2765 vp = vp->v_mount->mnt_vnodecovered;
2767 struct namecache *ncp;
2771 ncp = TAILQ_FIRST(&vp->v_cache_dst);
2774 for (i = 0; i < ncp->nc_nlen; i++)
2775 db_printf("%c", *ncn++);
2788 DB_SHOW_COMMAND(vpath, db_show_vpath)
2793 db_printf("usage: show vpath <struct vnode *>\n");
2797 vp = (struct vnode *)addr;