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>
60 #include <sys/syscallsubr.h>
61 #include <sys/sysctl.h>
62 #include <sys/sysproto.h>
63 #include <sys/vnode.h>
65 #include <sys/ktrace.h>
74 SDT_PROVIDER_DECLARE(vfs);
75 SDT_PROBE_DEFINE3(vfs, namecache, enter, done, "struct vnode *", "char *",
77 SDT_PROBE_DEFINE2(vfs, namecache, enter_negative, done, "struct vnode *",
79 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, entry, "struct vnode *");
80 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, hit, "struct vnode *",
81 "char *", "struct vnode *");
82 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, miss, "struct vnode *");
83 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, return, "int",
84 "struct vnode *", "char *");
85 SDT_PROBE_DEFINE3(vfs, namecache, lookup, hit, "struct vnode *", "char *",
87 SDT_PROBE_DEFINE2(vfs, namecache, lookup, hit__negative,
88 "struct vnode *", "char *");
89 SDT_PROBE_DEFINE2(vfs, namecache, lookup, miss, "struct vnode *",
91 SDT_PROBE_DEFINE1(vfs, namecache, purge, done, "struct vnode *");
92 SDT_PROBE_DEFINE1(vfs, namecache, purge_negative, done, "struct vnode *");
93 SDT_PROBE_DEFINE1(vfs, namecache, purgevfs, done, "struct mount *");
94 SDT_PROBE_DEFINE3(vfs, namecache, zap, done, "struct vnode *", "char *",
96 SDT_PROBE_DEFINE2(vfs, namecache, zap_negative, done, "struct vnode *",
98 SDT_PROBE_DEFINE2(vfs, namecache, shrink_negative, done, "struct vnode *",
102 * This structure describes the elements in the cache of recent
103 * names looked up by namei.
107 LIST_ENTRY(namecache) nc_hash; /* hash chain */
108 LIST_ENTRY(namecache) nc_src; /* source vnode list */
109 TAILQ_ENTRY(namecache) nc_dst; /* destination vnode list */
110 struct vnode *nc_dvp; /* vnode of parent of name */
112 struct vnode *nu_vp; /* vnode the name refers to */
114 u_char nc_flag; /* flag bits */
115 u_char nc_nlen; /* length of name */
116 char nc_name[0]; /* segment name + nul */
120 * struct namecache_ts repeats struct namecache layout up to the
122 * struct namecache_ts is used in place of struct namecache when time(s) need
123 * to be stored. The nc_dotdottime field is used when a cache entry is mapping
124 * both a non-dotdot directory name plus dotdot for the directory's
127 struct namecache_ts {
128 struct timespec nc_time; /* timespec provided by fs */
129 struct timespec nc_dotdottime; /* dotdot timespec provided by fs */
130 int nc_ticks; /* ticks value when entry was added */
131 struct namecache nc_nc;
134 #define nc_vp n_un.nu_vp
137 * Flags in namecache.nc_flag
139 #define NCF_WHITE 0x01
140 #define NCF_ISDOTDOT 0x02
143 #define NCF_DVDROP 0x10
144 #define NCF_NEGATIVE 0x20
145 #define NCF_HOTNEGATIVE 0x40
148 * Name caching works as follows:
150 * Names found by directory scans are retained in a cache
151 * for future reference. It is managed LRU, so frequently
152 * used names will hang around. Cache is indexed by hash value
153 * obtained from (dvp, name) where dvp refers to the directory
156 * If it is a "negative" entry, (i.e. for a name that is known NOT to
157 * exist) the vnode pointer will be NULL.
159 * Upon reaching the last segment of a path, if the reference
160 * is for DELETE, or NOCACHE is set (rewrite), and the
161 * name is located in the cache, it will be dropped.
163 * These locks are used (in the order in which they can be taken):
165 * vnodelock mtx vnode lists and v_cache_dd field protection
166 * bucketlock rwlock for access to given set of hash buckets
167 * neglist mtx negative entry LRU management
169 * Additionally, ncneg_shrink_lock mtx is used to have at most one thread
170 * shrinking the LRU list.
172 * It is legal to take multiple vnodelock and bucketlock locks. The locking
173 * order is lower address first. Both are recursive.
175 * "." lookups are lockless.
177 * ".." and vnode -> name lookups require vnodelock.
179 * name -> vnode lookup requires the relevant bucketlock to be held for reading.
181 * Insertions and removals of entries require involved vnodes and bucketlocks
182 * to be write-locked to prevent other threads from seeing the entry.
184 * Some lookups result in removal of the found entry (e.g. getting rid of a
185 * negative entry with the intent to create a positive one), which poses a
186 * problem when multiple threads reach the state. Similarly, two different
187 * threads can purge two different vnodes and try to remove the same name.
189 * If the already held vnode lock is lower than the second required lock, we
190 * can just take the other lock. However, in the opposite case, this could
191 * deadlock. As such, this is resolved by trylocking and if that fails unlocking
192 * the first node, locking everything in order and revalidating the state.
196 * Structures associated with name caching.
198 #define NCHHASH(hash) \
199 (&nchashtbl[(hash) & nchash])
200 static __read_mostly LIST_HEAD(nchashhead, namecache) *nchashtbl;/* Hash Table */
201 static u_long __read_mostly nchash; /* size of hash table */
202 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0,
203 "Size of namecache hash table");
204 static u_long __read_mostly ncnegfactor = 5; /* ratio of negative entries */
205 SYSCTL_ULONG(_vfs, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0,
206 "Ratio of negative namecache entries");
207 static u_long __exclusive_cache_line numneg; /* number of negative entries allocated */
208 static u_long __exclusive_cache_line numcache;/* number of cache entries allocated */
209 u_int ncsizefactor = 2;
210 SYSCTL_UINT(_vfs, OID_AUTO, ncsizefactor, CTLFLAG_RW, &ncsizefactor, 0,
211 "Size factor for namecache");
212 static u_int __read_mostly ncpurgeminvnodes;
213 SYSCTL_UINT(_vfs, OID_AUTO, ncpurgeminvnodes, CTLFLAG_RW, &ncpurgeminvnodes, 0,
214 "Number of vnodes below which purgevfs ignores the request");
215 static u_int __read_mostly ncsize; /* the size as computed on creation or resizing */
217 struct nchstats nchstats; /* cache effectiveness statistics */
219 static struct mtx __exclusive_cache_line ncneg_shrink_lock;
220 static int shrink_list_turn;
224 TAILQ_HEAD(, namecache) nl_list;
225 } __aligned(CACHE_LINE_SIZE);
227 static struct neglist __read_mostly *neglists;
228 static struct neglist ncneg_hot;
229 static u_long numhotneg;
231 #define numneglists (ncneghash + 1)
232 static u_int __read_mostly ncneghash;
233 static inline struct neglist *
234 NCP2NEGLIST(struct namecache *ncp)
237 return (&neglists[(((uintptr_t)(ncp) >> 8) & ncneghash)]);
240 #define numbucketlocks (ncbuckethash + 1)
241 static u_int __read_mostly ncbuckethash;
242 static struct rwlock_padalign __read_mostly *bucketlocks;
243 #define HASH2BUCKETLOCK(hash) \
244 ((struct rwlock *)(&bucketlocks[((hash) & ncbuckethash)]))
246 #define numvnodelocks (ncvnodehash + 1)
247 static u_int __read_mostly ncvnodehash;
248 static struct mtx __read_mostly *vnodelocks;
249 static inline struct mtx *
250 VP2VNODELOCK(struct vnode *vp)
253 return (&vnodelocks[(((uintptr_t)(vp) >> 8) & ncvnodehash)]);
257 * UMA zones for the VFS cache.
259 * The small cache is used for entries with short names, which are the
260 * most common. The large cache is used for entries which are too big to
261 * fit in the small cache.
263 static uma_zone_t __read_mostly cache_zone_small;
264 static uma_zone_t __read_mostly cache_zone_small_ts;
265 static uma_zone_t __read_mostly cache_zone_large;
266 static uma_zone_t __read_mostly cache_zone_large_ts;
268 #define CACHE_PATH_CUTOFF 35
270 static struct namecache *
271 cache_alloc(int len, int ts)
273 struct namecache_ts *ncp_ts;
274 struct namecache *ncp;
276 if (__predict_false(ts)) {
277 if (len <= CACHE_PATH_CUTOFF)
278 ncp_ts = uma_zalloc(cache_zone_small_ts, M_WAITOK);
280 ncp_ts = uma_zalloc(cache_zone_large_ts, M_WAITOK);
281 ncp = &ncp_ts->nc_nc;
283 if (len <= CACHE_PATH_CUTOFF)
284 ncp = uma_zalloc(cache_zone_small, M_WAITOK);
286 ncp = uma_zalloc(cache_zone_large, M_WAITOK);
292 cache_free(struct namecache *ncp)
294 struct namecache_ts *ncp_ts;
298 if ((ncp->nc_flag & NCF_DVDROP) != 0)
300 if (__predict_false(ncp->nc_flag & NCF_TS)) {
301 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
302 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
303 uma_zfree(cache_zone_small_ts, ncp_ts);
305 uma_zfree(cache_zone_large_ts, ncp_ts);
307 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
308 uma_zfree(cache_zone_small, ncp);
310 uma_zfree(cache_zone_large, ncp);
315 cache_out_ts(struct namecache *ncp, struct timespec *tsp, int *ticksp)
317 struct namecache_ts *ncp_ts;
319 KASSERT((ncp->nc_flag & NCF_TS) != 0 ||
320 (tsp == NULL && ticksp == NULL),
323 if (tsp == NULL && ticksp == NULL)
326 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
328 *tsp = ncp_ts->nc_time;
330 *ticksp = ncp_ts->nc_ticks;
334 static int __read_mostly doingcache = 1; /* 1 => enable the cache */
335 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0,
336 "VFS namecache enabled");
339 /* Export size information to userland */
340 SYSCTL_INT(_debug_sizeof, OID_AUTO, namecache, CTLFLAG_RD, SYSCTL_NULL_INT_PTR,
341 sizeof(struct namecache), "sizeof(struct namecache)");
344 * The new name cache statistics
346 static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
347 "Name cache statistics");
348 #define STATNODE_ULONG(name, descr) \
349 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, descr);
350 #define STATNODE_COUNTER(name, descr) \
351 static COUNTER_U64_DEFINE_EARLY(name); \
352 SYSCTL_COUNTER_U64(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, \
354 STATNODE_ULONG(numneg, "Number of negative cache entries");
355 STATNODE_ULONG(numcache, "Number of cache entries");
356 STATNODE_COUNTER(numcachehv, "Number of namecache entries with vnodes held");
357 STATNODE_COUNTER(numcalls, "Number of cache lookups");
358 STATNODE_COUNTER(dothits, "Number of '.' hits");
359 STATNODE_COUNTER(dotdothits, "Number of '..' hits");
360 STATNODE_COUNTER(numchecks, "Number of checks in lookup");
361 STATNODE_COUNTER(nummiss, "Number of cache misses");
362 STATNODE_COUNTER(nummisszap, "Number of cache misses we do not want to cache");
363 STATNODE_COUNTER(numposzaps,
364 "Number of cache hits (positive) we do not want to cache");
365 STATNODE_COUNTER(numposhits, "Number of cache hits (positive)");
366 STATNODE_COUNTER(numnegzaps,
367 "Number of cache hits (negative) we do not want to cache");
368 STATNODE_COUNTER(numneghits, "Number of cache hits (negative)");
369 /* These count for vn_getcwd(), too. */
370 STATNODE_COUNTER(numfullpathcalls, "Number of fullpath search calls");
371 STATNODE_COUNTER(numfullpathfail1, "Number of fullpath search errors (ENOTDIR)");
372 STATNODE_COUNTER(numfullpathfail2,
373 "Number of fullpath search errors (VOP_VPTOCNP failures)");
374 STATNODE_COUNTER(numfullpathfail4, "Number of fullpath search errors (ENOMEM)");
375 STATNODE_COUNTER(numfullpathfound, "Number of successful fullpath calls");
376 STATNODE_COUNTER(zap_and_exit_bucket_relock_success,
377 "Number of successful removals after relocking");
378 static long zap_and_exit_bucket_fail; STATNODE_ULONG(zap_and_exit_bucket_fail,
379 "Number of times zap_and_exit failed to lock");
380 static long zap_and_exit_bucket_fail2; STATNODE_ULONG(zap_and_exit_bucket_fail2,
381 "Number of times zap_and_exit failed to lock");
382 static long cache_lock_vnodes_cel_3_failures;
383 STATNODE_ULONG(cache_lock_vnodes_cel_3_failures,
384 "Number of times 3-way vnode locking failed");
385 STATNODE_ULONG(numhotneg, "Number of hot negative entries");
386 STATNODE_COUNTER(numneg_evicted,
387 "Number of negative entries evicted when adding a new entry");
388 STATNODE_COUNTER(shrinking_skipped,
389 "Number of times shrinking was already in progress");
391 static void cache_zap_locked(struct namecache *ncp, bool neg_locked);
392 static int vn_fullpath_hardlink(struct thread *td, struct nameidata *ndp, char **retbuf,
393 char **freebuf, size_t *buflen);
394 static int vn_fullpath_any(struct thread *td, struct vnode *vp, struct vnode *rdir,
395 char *buf, char **retbuf, size_t *buflen);
396 static int vn_fullpath_dir(struct thread *td, struct vnode *vp, struct vnode *rdir,
397 char *buf, char **retbuf, size_t *len, bool slash_prefixed, size_t addend);
399 static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
401 static int cache_yield;
402 SYSCTL_INT(_vfs_cache, OID_AUTO, yield, CTLFLAG_RD, &cache_yield, 0,
403 "Number of times cache called yield");
405 static void __noinline
406 cache_maybe_yield(void)
409 if (should_yield()) {
411 kern_yield(PRI_USER);
416 cache_assert_vlp_locked(struct mtx *vlp)
420 mtx_assert(vlp, MA_OWNED);
424 cache_assert_vnode_locked(struct vnode *vp)
428 vlp = VP2VNODELOCK(vp);
429 cache_assert_vlp_locked(vlp);
433 cache_get_hash(char *name, u_char len, struct vnode *dvp)
437 hash = fnv_32_buf(name, len, FNV1_32_INIT);
438 hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
442 static inline struct rwlock *
443 NCP2BUCKETLOCK(struct namecache *ncp)
447 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen, ncp->nc_dvp);
448 return (HASH2BUCKETLOCK(hash));
453 cache_assert_bucket_locked(struct namecache *ncp, int mode)
457 blp = NCP2BUCKETLOCK(ncp);
458 rw_assert(blp, mode);
461 #define cache_assert_bucket_locked(x, y) do { } while (0)
464 #define cache_sort_vnodes(x, y) _cache_sort_vnodes((void **)(x), (void **)(y))
466 _cache_sort_vnodes(void **p1, void **p2)
470 MPASS(*p1 != NULL || *p2 != NULL);
480 cache_lock_all_buckets(void)
484 for (i = 0; i < numbucketlocks; i++)
485 rw_wlock(&bucketlocks[i]);
489 cache_unlock_all_buckets(void)
493 for (i = 0; i < numbucketlocks; i++)
494 rw_wunlock(&bucketlocks[i]);
498 cache_lock_all_vnodes(void)
502 for (i = 0; i < numvnodelocks; i++)
503 mtx_lock(&vnodelocks[i]);
507 cache_unlock_all_vnodes(void)
511 for (i = 0; i < numvnodelocks; i++)
512 mtx_unlock(&vnodelocks[i]);
516 cache_trylock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
519 cache_sort_vnodes(&vlp1, &vlp2);
522 if (!mtx_trylock(vlp1))
525 if (!mtx_trylock(vlp2)) {
535 cache_lock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
538 MPASS(vlp1 != NULL || vlp2 != NULL);
548 cache_unlock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
551 MPASS(vlp1 != NULL || vlp2 != NULL);
560 sysctl_nchstats(SYSCTL_HANDLER_ARGS)
562 struct nchstats snap;
564 if (req->oldptr == NULL)
565 return (SYSCTL_OUT(req, 0, sizeof(snap)));
568 snap.ncs_goodhits = counter_u64_fetch(numposhits);
569 snap.ncs_neghits = counter_u64_fetch(numneghits);
570 snap.ncs_badhits = counter_u64_fetch(numposzaps) +
571 counter_u64_fetch(numnegzaps);
572 snap.ncs_miss = counter_u64_fetch(nummisszap) +
573 counter_u64_fetch(nummiss);
575 return (SYSCTL_OUT(req, &snap, sizeof(snap)));
577 SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE | CTLFLAG_RD |
578 CTLFLAG_MPSAFE, 0, 0, sysctl_nchstats, "LU",
579 "VFS cache effectiveness statistics");
583 * Grab an atomic snapshot of the name cache hash chain lengths
585 static SYSCTL_NODE(_debug, OID_AUTO, hashstat,
586 CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
590 sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS)
592 struct nchashhead *ncpp;
593 struct namecache *ncp;
594 int i, error, n_nchash, *cntbuf;
597 n_nchash = nchash + 1; /* nchash is max index, not count */
598 if (req->oldptr == NULL)
599 return SYSCTL_OUT(req, 0, n_nchash * sizeof(int));
600 cntbuf = malloc(n_nchash * sizeof(int), M_TEMP, M_ZERO | M_WAITOK);
601 cache_lock_all_buckets();
602 if (n_nchash != nchash + 1) {
603 cache_unlock_all_buckets();
604 free(cntbuf, M_TEMP);
607 /* Scan hash tables counting entries */
608 for (ncpp = nchashtbl, i = 0; i < n_nchash; ncpp++, i++)
609 LIST_FOREACH(ncp, ncpp, nc_hash)
611 cache_unlock_all_buckets();
612 for (error = 0, i = 0; i < n_nchash; i++)
613 if ((error = SYSCTL_OUT(req, &cntbuf[i], sizeof(int))) != 0)
615 free(cntbuf, M_TEMP);
618 SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD|
619 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_rawnchash, "S,int",
620 "nchash chain lengths");
623 sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS)
626 struct nchashhead *ncpp;
627 struct namecache *ncp;
629 int count, maxlength, used, pct;
632 return SYSCTL_OUT(req, 0, 4 * sizeof(int));
634 cache_lock_all_buckets();
635 n_nchash = nchash + 1; /* nchash is max index, not count */
639 /* Scan hash tables for applicable entries */
640 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
642 LIST_FOREACH(ncp, ncpp, nc_hash) {
647 if (maxlength < count)
650 n_nchash = nchash + 1;
651 cache_unlock_all_buckets();
652 pct = (used * 100) / (n_nchash / 100);
653 error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash));
656 error = SYSCTL_OUT(req, &used, sizeof(used));
659 error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength));
662 error = SYSCTL_OUT(req, &pct, sizeof(pct));
667 SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD|
668 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_nchash, "I",
669 "nchash statistics (number of total/used buckets, maximum chain length, usage percentage)");
673 * Negative entries management
675 * A variation of LRU scheme is used. New entries are hashed into one of
676 * numneglists cold lists. Entries get promoted to the hot list on first hit.
678 * The shrinker will demote hot list head and evict from the cold list in a
679 * round-robin manner.
682 cache_negative_hit(struct namecache *ncp)
684 struct neglist *neglist;
686 MPASS(ncp->nc_flag & NCF_NEGATIVE);
687 if (ncp->nc_flag & NCF_HOTNEGATIVE)
689 neglist = NCP2NEGLIST(ncp);
690 mtx_lock(&ncneg_hot.nl_lock);
691 mtx_lock(&neglist->nl_lock);
692 if (!(ncp->nc_flag & NCF_HOTNEGATIVE)) {
694 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
695 TAILQ_INSERT_TAIL(&ncneg_hot.nl_list, ncp, nc_dst);
696 ncp->nc_flag |= NCF_HOTNEGATIVE;
698 mtx_unlock(&neglist->nl_lock);
699 mtx_unlock(&ncneg_hot.nl_lock);
703 cache_negative_insert(struct namecache *ncp, bool neg_locked)
705 struct neglist *neglist;
707 MPASS(ncp->nc_flag & NCF_NEGATIVE);
708 cache_assert_bucket_locked(ncp, RA_WLOCKED);
709 neglist = NCP2NEGLIST(ncp);
711 mtx_lock(&neglist->nl_lock);
713 mtx_assert(&neglist->nl_lock, MA_OWNED);
715 TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst);
717 mtx_unlock(&neglist->nl_lock);
718 atomic_add_rel_long(&numneg, 1);
722 cache_negative_remove(struct namecache *ncp, bool neg_locked)
724 struct neglist *neglist;
725 bool hot_locked = false;
726 bool list_locked = false;
728 MPASS(ncp->nc_flag & NCF_NEGATIVE);
729 cache_assert_bucket_locked(ncp, RA_WLOCKED);
730 neglist = NCP2NEGLIST(ncp);
732 if (ncp->nc_flag & NCF_HOTNEGATIVE) {
734 mtx_lock(&ncneg_hot.nl_lock);
735 if (!(ncp->nc_flag & NCF_HOTNEGATIVE)) {
737 mtx_lock(&neglist->nl_lock);
741 mtx_lock(&neglist->nl_lock);
744 if (ncp->nc_flag & NCF_HOTNEGATIVE) {
745 mtx_assert(&ncneg_hot.nl_lock, MA_OWNED);
746 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
749 mtx_assert(&neglist->nl_lock, MA_OWNED);
750 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
753 mtx_unlock(&neglist->nl_lock);
755 mtx_unlock(&ncneg_hot.nl_lock);
756 atomic_subtract_rel_long(&numneg, 1);
760 cache_negative_shrink_select(int start, struct namecache **ncpp,
761 struct neglist **neglistpp)
763 struct neglist *neglist;
764 struct namecache *ncp;
770 for (i = start; i < numneglists; i++) {
771 neglist = &neglists[i];
772 if (TAILQ_FIRST(&neglist->nl_list) == NULL)
774 mtx_lock(&neglist->nl_lock);
775 ncp = TAILQ_FIRST(&neglist->nl_list);
778 mtx_unlock(&neglist->nl_lock);
781 *neglistpp = neglist;
786 cache_negative_zap_one(void)
788 struct namecache *ncp, *ncp2;
789 struct neglist *neglist;
793 if (mtx_owner(&ncneg_shrink_lock) != NULL ||
794 !mtx_trylock(&ncneg_shrink_lock)) {
795 counter_u64_add(shrinking_skipped, 1);
799 mtx_lock(&ncneg_hot.nl_lock);
800 ncp = TAILQ_FIRST(&ncneg_hot.nl_list);
802 neglist = NCP2NEGLIST(ncp);
803 mtx_lock(&neglist->nl_lock);
804 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
805 TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst);
806 ncp->nc_flag &= ~NCF_HOTNEGATIVE;
808 mtx_unlock(&neglist->nl_lock);
810 mtx_unlock(&ncneg_hot.nl_lock);
812 cache_negative_shrink_select(shrink_list_turn, &ncp, &neglist);
814 if (shrink_list_turn == numneglists)
815 shrink_list_turn = 0;
816 if (ncp == NULL && shrink_list_turn == 0)
817 cache_negative_shrink_select(shrink_list_turn, &ncp, &neglist);
818 mtx_unlock(&ncneg_shrink_lock);
822 MPASS(ncp->nc_flag & NCF_NEGATIVE);
823 dvlp = VP2VNODELOCK(ncp->nc_dvp);
824 blp = NCP2BUCKETLOCK(ncp);
825 mtx_unlock(&neglist->nl_lock);
828 mtx_lock(&neglist->nl_lock);
829 ncp2 = TAILQ_FIRST(&neglist->nl_list);
830 if (ncp != ncp2 || dvlp != VP2VNODELOCK(ncp2->nc_dvp) ||
831 blp != NCP2BUCKETLOCK(ncp2) || !(ncp2->nc_flag & NCF_NEGATIVE)) {
834 SDT_PROBE2(vfs, namecache, shrink_negative, done, ncp->nc_dvp,
837 cache_zap_locked(ncp, true);
838 counter_u64_add(numneg_evicted, 1);
840 mtx_unlock(&neglist->nl_lock);
847 * cache_zap_locked():
849 * Removes a namecache entry from cache, whether it contains an actual
850 * pointer to a vnode or if it is just a negative cache entry.
853 cache_zap_locked(struct namecache *ncp, bool neg_locked)
856 if (!(ncp->nc_flag & NCF_NEGATIVE))
857 cache_assert_vnode_locked(ncp->nc_vp);
858 cache_assert_vnode_locked(ncp->nc_dvp);
859 cache_assert_bucket_locked(ncp, RA_WLOCKED);
861 CTR2(KTR_VFS, "cache_zap(%p) vp %p", ncp,
862 (ncp->nc_flag & NCF_NEGATIVE) ? NULL : ncp->nc_vp);
863 LIST_REMOVE(ncp, nc_hash);
864 if (!(ncp->nc_flag & NCF_NEGATIVE)) {
865 SDT_PROBE3(vfs, namecache, zap, done, ncp->nc_dvp,
866 ncp->nc_name, ncp->nc_vp);
867 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
868 if (ncp == ncp->nc_vp->v_cache_dd)
869 ncp->nc_vp->v_cache_dd = NULL;
871 SDT_PROBE2(vfs, namecache, zap_negative, done, ncp->nc_dvp,
873 cache_negative_remove(ncp, neg_locked);
875 if (ncp->nc_flag & NCF_ISDOTDOT) {
876 if (ncp == ncp->nc_dvp->v_cache_dd)
877 ncp->nc_dvp->v_cache_dd = NULL;
879 LIST_REMOVE(ncp, nc_src);
880 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) {
881 ncp->nc_flag |= NCF_DVDROP;
882 counter_u64_add(numcachehv, -1);
885 atomic_subtract_rel_long(&numcache, 1);
889 cache_zap_negative_locked_vnode_kl(struct namecache *ncp, struct vnode *vp)
893 MPASS(ncp->nc_dvp == vp);
894 MPASS(ncp->nc_flag & NCF_NEGATIVE);
895 cache_assert_vnode_locked(vp);
897 blp = NCP2BUCKETLOCK(ncp);
899 cache_zap_locked(ncp, false);
904 cache_zap_locked_vnode_kl2(struct namecache *ncp, struct vnode *vp,
907 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
910 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
911 cache_assert_vnode_locked(vp);
913 if (ncp->nc_flag & NCF_NEGATIVE) {
918 cache_zap_negative_locked_vnode_kl(ncp, vp);
922 pvlp = VP2VNODELOCK(vp);
923 blp = NCP2BUCKETLOCK(ncp);
924 vlp1 = VP2VNODELOCK(ncp->nc_dvp);
925 vlp2 = VP2VNODELOCK(ncp->nc_vp);
927 if (*vlpp == vlp1 || *vlpp == vlp2) {
935 cache_sort_vnodes(&vlp1, &vlp2);
940 if (!mtx_trylock(vlp1))
946 cache_zap_locked(ncp, false);
948 if (to_unlock != NULL)
949 mtx_unlock(to_unlock);
956 MPASS(*vlpp == NULL);
961 static int __noinline
962 cache_zap_locked_vnode(struct namecache *ncp, struct vnode *vp)
964 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
968 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
969 cache_assert_vnode_locked(vp);
971 pvlp = VP2VNODELOCK(vp);
972 if (ncp->nc_flag & NCF_NEGATIVE) {
973 cache_zap_negative_locked_vnode_kl(ncp, vp);
977 blp = NCP2BUCKETLOCK(ncp);
978 vlp1 = VP2VNODELOCK(ncp->nc_dvp);
979 vlp2 = VP2VNODELOCK(ncp->nc_vp);
980 cache_sort_vnodes(&vlp1, &vlp2);
985 if (!mtx_trylock(vlp1)) {
992 cache_zap_locked(ncp, false);
994 mtx_unlock(to_unlock);
1001 * If trylocking failed we can get here. We know enough to take all needed locks
1002 * in the right order and re-lookup the entry.
1005 cache_zap_unlocked_bucket(struct namecache *ncp, struct componentname *cnp,
1006 struct vnode *dvp, struct mtx *dvlp, struct mtx *vlp, uint32_t hash,
1009 struct namecache *rncp;
1011 cache_assert_bucket_locked(ncp, RA_UNLOCKED);
1013 cache_sort_vnodes(&dvlp, &vlp);
1014 cache_lock_vnodes(dvlp, vlp);
1016 LIST_FOREACH(rncp, (NCHHASH(hash)), nc_hash) {
1017 if (rncp == ncp && rncp->nc_dvp == dvp &&
1018 rncp->nc_nlen == cnp->cn_namelen &&
1019 !bcmp(rncp->nc_name, cnp->cn_nameptr, rncp->nc_nlen))
1023 cache_zap_locked(rncp, false);
1025 cache_unlock_vnodes(dvlp, vlp);
1026 counter_u64_add(zap_and_exit_bucket_relock_success, 1);
1031 cache_unlock_vnodes(dvlp, vlp);
1035 static int __noinline
1036 cache_zap_wlocked_bucket(struct namecache *ncp, struct componentname *cnp,
1037 uint32_t hash, struct rwlock *blp)
1039 struct mtx *dvlp, *vlp;
1042 cache_assert_bucket_locked(ncp, RA_WLOCKED);
1044 dvlp = VP2VNODELOCK(ncp->nc_dvp);
1046 if (!(ncp->nc_flag & NCF_NEGATIVE))
1047 vlp = VP2VNODELOCK(ncp->nc_vp);
1048 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1049 cache_zap_locked(ncp, false);
1051 cache_unlock_vnodes(dvlp, vlp);
1057 return (cache_zap_unlocked_bucket(ncp, cnp, dvp, dvlp, vlp, hash, blp));
1060 static int __noinline
1061 cache_zap_rlocked_bucket(struct namecache *ncp, struct componentname *cnp,
1062 uint32_t hash, struct rwlock *blp)
1064 struct mtx *dvlp, *vlp;
1067 cache_assert_bucket_locked(ncp, RA_RLOCKED);
1069 dvlp = VP2VNODELOCK(ncp->nc_dvp);
1071 if (!(ncp->nc_flag & NCF_NEGATIVE))
1072 vlp = VP2VNODELOCK(ncp->nc_vp);
1073 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1076 cache_zap_locked(ncp, false);
1078 cache_unlock_vnodes(dvlp, vlp);
1084 return (cache_zap_unlocked_bucket(ncp, cnp, dvp, dvlp, vlp, hash, blp));
1088 cache_zap_wlocked_bucket_kl(struct namecache *ncp, struct rwlock *blp,
1089 struct mtx **vlpp1, struct mtx **vlpp2)
1091 struct mtx *dvlp, *vlp;
1093 cache_assert_bucket_locked(ncp, RA_WLOCKED);
1095 dvlp = VP2VNODELOCK(ncp->nc_dvp);
1097 if (!(ncp->nc_flag & NCF_NEGATIVE))
1098 vlp = VP2VNODELOCK(ncp->nc_vp);
1099 cache_sort_vnodes(&dvlp, &vlp);
1101 if (*vlpp1 == dvlp && *vlpp2 == vlp) {
1102 cache_zap_locked(ncp, false);
1103 cache_unlock_vnodes(dvlp, vlp);
1116 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1117 cache_zap_locked(ncp, false);
1118 cache_unlock_vnodes(dvlp, vlp);
1133 cache_lookup_unlock(struct rwlock *blp, struct mtx *vlp)
1143 static int __noinline
1144 cache_lookup_dot(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1145 struct timespec *tsp, int *ticksp)
1150 CTR2(KTR_VFS, "cache_lookup(%p, %s) found via .",
1151 dvp, cnp->cn_nameptr);
1152 counter_u64_add(dothits, 1);
1153 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", *vpp);
1160 * When we lookup "." we still can be asked to lock it
1163 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
1164 if (ltype != VOP_ISLOCKED(*vpp)) {
1165 if (ltype == LK_EXCLUSIVE) {
1166 vn_lock(*vpp, LK_UPGRADE | LK_RETRY);
1167 if (VN_IS_DOOMED((*vpp))) {
1168 /* forced unmount */
1174 vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY);
1179 static __noinline int
1180 cache_lookup_nomakeentry(struct vnode *dvp, struct vnode **vpp,
1181 struct componentname *cnp, struct timespec *tsp, int *ticksp)
1183 struct namecache *ncp;
1185 struct mtx *dvlp, *dvlp2;
1189 if (cnp->cn_namelen == 2 &&
1190 cnp->cn_nameptr[0] == '.' && cnp->cn_nameptr[1] == '.') {
1191 counter_u64_add(dotdothits, 1);
1192 dvlp = VP2VNODELOCK(dvp);
1196 ncp = dvp->v_cache_dd;
1198 SDT_PROBE3(vfs, namecache, lookup, miss, dvp,
1205 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1206 if (ncp->nc_dvp != dvp)
1207 panic("dvp %p v_cache_dd %p\n", dvp, ncp);
1208 if (!cache_zap_locked_vnode_kl2(ncp,
1211 MPASS(dvp->v_cache_dd == NULL);
1217 dvp->v_cache_dd = NULL;
1225 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1226 blp = HASH2BUCKETLOCK(hash);
1228 if (LIST_EMPTY(NCHHASH(hash)))
1233 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1234 counter_u64_add(numchecks, 1);
1235 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1236 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1240 /* We failed to find an entry */
1246 error = cache_zap_wlocked_bucket(ncp, cnp, hash, blp);
1247 if (__predict_false(error != 0)) {
1248 zap_and_exit_bucket_fail++;
1249 cache_maybe_yield();
1252 counter_u64_add(numposzaps, 1);
1256 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr, NULL);
1257 counter_u64_add(nummisszap, 1);
1262 * Lookup a name in the name cache
1266 * - dvp: Parent directory in which to search.
1267 * - vpp: Return argument. Will contain desired vnode on cache hit.
1268 * - cnp: Parameters of the name search. The most interesting bits of
1269 * the cn_flags field have the following meanings:
1270 * - MAKEENTRY: If clear, free an entry from the cache rather than look
1272 * - ISDOTDOT: Must be set if and only if cn_nameptr == ".."
1273 * - tsp: Return storage for cache timestamp. On a successful (positive
1274 * or negative) lookup, tsp will be filled with any timespec that
1275 * was stored when this cache entry was created. However, it will
1276 * be clear for "." entries.
1277 * - ticks: Return storage for alternate cache timestamp. On a successful
1278 * (positive or negative) lookup, it will contain the ticks value
1279 * that was current when the cache entry was created, unless cnp
1284 * - -1: A positive cache hit. vpp will contain the desired vnode.
1285 * - ENOENT: A negative cache hit, or dvp was recycled out from under us due
1286 * to a forced unmount. vpp will not be modified. If the entry
1287 * is a whiteout, then the ISWHITEOUT flag will be set in
1289 * - 0: A cache miss. vpp will not be modified.
1293 * On a cache hit, vpp will be returned locked and ref'd. If we're looking up
1294 * .., dvp is unlocked. If we're looking up . an extra ref is taken, but the
1295 * lock is not recursively acquired.
1298 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1299 struct timespec *tsp, int *ticksp)
1301 struct namecache_ts *ncp_ts;
1302 struct namecache *ncp;
1310 if (__predict_false(!doingcache)) {
1311 cnp->cn_flags &= ~MAKEENTRY;
1316 counter_u64_add(numcalls, 1);
1318 if (__predict_false(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.'))
1319 return (cache_lookup_dot(dvp, vpp, cnp, tsp, ticksp));
1321 if ((cnp->cn_flags & MAKEENTRY) == 0)
1322 return (cache_lookup_nomakeentry(dvp, vpp, cnp, tsp, ticksp));
1328 if (cnp->cn_namelen == 2 &&
1329 cnp->cn_nameptr[0] == '.' && cnp->cn_nameptr[1] == '.') {
1330 counter_u64_add(dotdothits, 1);
1331 dvlp = VP2VNODELOCK(dvp);
1333 ncp = dvp->v_cache_dd;
1335 SDT_PROBE3(vfs, namecache, lookup, miss, dvp,
1340 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1341 if (ncp->nc_flag & NCF_NEGATIVE)
1347 /* Return failure if negative entry was found. */
1349 goto negative_success;
1350 CTR3(KTR_VFS, "cache_lookup(%p, %s) found %p via ..",
1351 dvp, cnp->cn_nameptr, *vpp);
1352 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, "..",
1354 cache_out_ts(ncp, tsp, ticksp);
1355 if ((ncp->nc_flag & (NCF_ISDOTDOT | NCF_DTS)) ==
1356 NCF_DTS && tsp != NULL) {
1357 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1358 *tsp = ncp_ts->nc_dotdottime;
1363 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1364 blp = HASH2BUCKETLOCK(hash);
1367 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1368 counter_u64_add(numchecks, 1);
1369 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1370 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1374 /* We failed to find an entry */
1375 if (__predict_false(ncp == NULL)) {
1377 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
1379 counter_u64_add(nummiss, 1);
1383 if (ncp->nc_flag & NCF_NEGATIVE)
1384 goto negative_success;
1386 /* We found a "positive" match, return the vnode */
1387 counter_u64_add(numposhits, 1);
1389 CTR4(KTR_VFS, "cache_lookup(%p, %s) found %p via ncp %p",
1390 dvp, cnp->cn_nameptr, *vpp, ncp);
1391 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name,
1393 cache_out_ts(ncp, tsp, ticksp);
1396 * On success we return a locked and ref'd vnode as per the lookup
1400 ltype = 0; /* silence gcc warning */
1401 if (cnp->cn_flags & ISDOTDOT) {
1402 ltype = VOP_ISLOCKED(dvp);
1405 vs = vget_prep(*vpp);
1406 cache_lookup_unlock(blp, dvlp);
1407 error = vget_finish(*vpp, cnp->cn_lkflags, vs);
1408 if (cnp->cn_flags & ISDOTDOT) {
1409 vn_lock(dvp, ltype | LK_RETRY);
1410 if (VN_IS_DOOMED(dvp)) {
1421 if ((cnp->cn_flags & ISLASTCN) &&
1422 (cnp->cn_lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE) {
1423 ASSERT_VOP_ELOCKED(*vpp, "cache_lookup");
1428 /* We found a negative match, and want to create it, so purge */
1429 if (cnp->cn_nameiop == CREATE) {
1430 counter_u64_add(numnegzaps, 1);
1434 counter_u64_add(numneghits, 1);
1435 cache_negative_hit(ncp);
1436 if (ncp->nc_flag & NCF_WHITE)
1437 cnp->cn_flags |= ISWHITEOUT;
1438 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp,
1440 cache_out_ts(ncp, tsp, ticksp);
1441 cache_lookup_unlock(blp, dvlp);
1446 error = cache_zap_rlocked_bucket(ncp, cnp, hash, blp);
1448 error = cache_zap_locked_vnode(ncp, dvp);
1449 if (__predict_false(error != 0)) {
1450 zap_and_exit_bucket_fail2++;
1451 cache_maybe_yield();
1458 struct celockstate {
1460 struct rwlock *blp[2];
1462 CTASSERT((nitems(((struct celockstate *)0)->vlp) == 3));
1463 CTASSERT((nitems(((struct celockstate *)0)->blp) == 2));
1466 cache_celockstate_init(struct celockstate *cel)
1469 bzero(cel, sizeof(*cel));
1473 cache_lock_vnodes_cel(struct celockstate *cel, struct vnode *vp,
1476 struct mtx *vlp1, *vlp2;
1478 MPASS(cel->vlp[0] == NULL);
1479 MPASS(cel->vlp[1] == NULL);
1480 MPASS(cel->vlp[2] == NULL);
1482 MPASS(vp != NULL || dvp != NULL);
1484 vlp1 = VP2VNODELOCK(vp);
1485 vlp2 = VP2VNODELOCK(dvp);
1486 cache_sort_vnodes(&vlp1, &vlp2);
1497 cache_unlock_vnodes_cel(struct celockstate *cel)
1500 MPASS(cel->vlp[0] != NULL || cel->vlp[1] != NULL);
1502 if (cel->vlp[0] != NULL)
1503 mtx_unlock(cel->vlp[0]);
1504 if (cel->vlp[1] != NULL)
1505 mtx_unlock(cel->vlp[1]);
1506 if (cel->vlp[2] != NULL)
1507 mtx_unlock(cel->vlp[2]);
1511 cache_lock_vnodes_cel_3(struct celockstate *cel, struct vnode *vp)
1516 cache_assert_vlp_locked(cel->vlp[0]);
1517 cache_assert_vlp_locked(cel->vlp[1]);
1518 MPASS(cel->vlp[2] == NULL);
1521 vlp = VP2VNODELOCK(vp);
1524 if (vlp >= cel->vlp[1]) {
1527 if (mtx_trylock(vlp))
1529 cache_lock_vnodes_cel_3_failures++;
1530 cache_unlock_vnodes_cel(cel);
1531 if (vlp < cel->vlp[0]) {
1533 mtx_lock(cel->vlp[0]);
1534 mtx_lock(cel->vlp[1]);
1536 if (cel->vlp[0] != NULL)
1537 mtx_lock(cel->vlp[0]);
1539 mtx_lock(cel->vlp[1]);
1549 cache_lock_buckets_cel(struct celockstate *cel, struct rwlock *blp1,
1550 struct rwlock *blp2)
1553 MPASS(cel->blp[0] == NULL);
1554 MPASS(cel->blp[1] == NULL);
1556 cache_sort_vnodes(&blp1, &blp2);
1567 cache_unlock_buckets_cel(struct celockstate *cel)
1570 if (cel->blp[0] != NULL)
1571 rw_wunlock(cel->blp[0]);
1572 rw_wunlock(cel->blp[1]);
1576 * Lock part of the cache affected by the insertion.
1578 * This means vnodelocks for dvp, vp and the relevant bucketlock.
1579 * However, insertion can result in removal of an old entry. In this
1580 * case we have an additional vnode and bucketlock pair to lock. If the
1581 * entry is negative, ncelock is locked instead of the vnode.
1583 * That is, in the worst case we have to lock 3 vnodes and 2 bucketlocks, while
1584 * preserving the locking order (smaller address first).
1587 cache_enter_lock(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1590 struct namecache *ncp;
1591 struct rwlock *blps[2];
1593 blps[0] = HASH2BUCKETLOCK(hash);
1596 cache_lock_vnodes_cel(cel, dvp, vp);
1597 if (vp == NULL || vp->v_type != VDIR)
1599 ncp = vp->v_cache_dd;
1602 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1604 MPASS(ncp->nc_dvp == vp);
1605 blps[1] = NCP2BUCKETLOCK(ncp);
1606 if (ncp->nc_flag & NCF_NEGATIVE)
1608 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1611 * All vnodes got re-locked. Re-validate the state and if
1612 * nothing changed we are done. Otherwise restart.
1614 if (ncp == vp->v_cache_dd &&
1615 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1616 blps[1] == NCP2BUCKETLOCK(ncp) &&
1617 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1619 cache_unlock_vnodes_cel(cel);
1624 cache_lock_buckets_cel(cel, blps[0], blps[1]);
1628 cache_enter_lock_dd(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1631 struct namecache *ncp;
1632 struct rwlock *blps[2];
1634 blps[0] = HASH2BUCKETLOCK(hash);
1637 cache_lock_vnodes_cel(cel, dvp, vp);
1638 ncp = dvp->v_cache_dd;
1641 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1643 MPASS(ncp->nc_dvp == dvp);
1644 blps[1] = NCP2BUCKETLOCK(ncp);
1645 if (ncp->nc_flag & NCF_NEGATIVE)
1647 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1649 if (ncp == dvp->v_cache_dd &&
1650 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1651 blps[1] == NCP2BUCKETLOCK(ncp) &&
1652 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1654 cache_unlock_vnodes_cel(cel);
1659 cache_lock_buckets_cel(cel, blps[0], blps[1]);
1663 cache_enter_unlock(struct celockstate *cel)
1666 cache_unlock_buckets_cel(cel);
1667 cache_unlock_vnodes_cel(cel);
1670 static void __noinline
1671 cache_enter_dotdot_prep(struct vnode *dvp, struct vnode *vp,
1672 struct componentname *cnp)
1674 struct celockstate cel;
1675 struct namecache *ncp;
1679 if (dvp->v_cache_dd == NULL)
1681 len = cnp->cn_namelen;
1682 cache_celockstate_init(&cel);
1683 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1684 cache_enter_lock_dd(&cel, dvp, vp, hash);
1685 ncp = dvp->v_cache_dd;
1686 if (ncp != NULL && (ncp->nc_flag & NCF_ISDOTDOT)) {
1687 KASSERT(ncp->nc_dvp == dvp, ("wrong isdotdot parent"));
1688 cache_zap_locked(ncp, false);
1692 dvp->v_cache_dd = NULL;
1693 cache_enter_unlock(&cel);
1698 * Add an entry to the cache.
1701 cache_enter_time(struct vnode *dvp, struct vnode *vp, struct componentname *cnp,
1702 struct timespec *tsp, struct timespec *dtsp)
1704 struct celockstate cel;
1705 struct namecache *ncp, *n2, *ndd;
1706 struct namecache_ts *ncp_ts, *n2_ts;
1707 struct nchashhead *ncpp;
1713 CTR3(KTR_VFS, "cache_enter(%p, %p, %s)", dvp, vp, cnp->cn_nameptr);
1714 VNASSERT(vp == NULL || !VN_IS_DOOMED(vp), vp,
1715 ("cache_enter: Adding a doomed vnode"));
1716 VNASSERT(dvp == NULL || !VN_IS_DOOMED(dvp), dvp,
1717 ("cache_enter: Doomed vnode used as src"));
1720 if (__predict_false(!doingcache))
1725 if (__predict_false(cnp->cn_nameptr[0] == '.')) {
1726 if (cnp->cn_namelen == 1)
1728 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
1729 cache_enter_dotdot_prep(dvp, vp, cnp);
1730 flag = NCF_ISDOTDOT;
1735 * Avoid blowout in namecache entries.
1737 lnumcache = atomic_fetchadd_long(&numcache, 1) + 1;
1738 if (__predict_false(lnumcache >= ncsize)) {
1739 atomic_add_long(&numcache, -1);
1743 cache_celockstate_init(&cel);
1748 * Calculate the hash key and setup as much of the new
1749 * namecache entry as possible before acquiring the lock.
1751 ncp = cache_alloc(cnp->cn_namelen, tsp != NULL);
1752 ncp->nc_flag = flag;
1755 ncp->nc_flag |= NCF_NEGATIVE;
1758 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1759 ncp_ts->nc_time = *tsp;
1760 ncp_ts->nc_ticks = ticks;
1761 ncp_ts->nc_nc.nc_flag |= NCF_TS;
1763 ncp_ts->nc_dotdottime = *dtsp;
1764 ncp_ts->nc_nc.nc_flag |= NCF_DTS;
1767 len = ncp->nc_nlen = cnp->cn_namelen;
1768 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1769 strlcpy(ncp->nc_name, cnp->cn_nameptr, len + 1);
1770 cache_enter_lock(&cel, dvp, vp, hash);
1773 * See if this vnode or negative entry is already in the cache
1774 * with this name. This can happen with concurrent lookups of
1775 * the same path name.
1777 ncpp = NCHHASH(hash);
1778 LIST_FOREACH(n2, ncpp, nc_hash) {
1779 if (n2->nc_dvp == dvp &&
1780 n2->nc_nlen == cnp->cn_namelen &&
1781 !bcmp(n2->nc_name, cnp->cn_nameptr, n2->nc_nlen)) {
1783 KASSERT((n2->nc_flag & NCF_TS) != 0,
1785 n2_ts = __containerof(n2, struct namecache_ts, nc_nc);
1786 n2_ts->nc_time = ncp_ts->nc_time;
1787 n2_ts->nc_ticks = ncp_ts->nc_ticks;
1789 n2_ts->nc_dotdottime = ncp_ts->nc_dotdottime;
1790 if (ncp->nc_flag & NCF_NEGATIVE)
1791 mtx_lock(&ncneg_hot.nl_lock);
1792 n2_ts->nc_nc.nc_flag |= NCF_DTS;
1793 if (ncp->nc_flag & NCF_NEGATIVE)
1794 mtx_unlock(&ncneg_hot.nl_lock);
1797 goto out_unlock_free;
1801 if (flag == NCF_ISDOTDOT) {
1803 * See if we are trying to add .. entry, but some other lookup
1804 * has populated v_cache_dd pointer already.
1806 if (dvp->v_cache_dd != NULL)
1807 goto out_unlock_free;
1808 KASSERT(vp == NULL || vp->v_type == VDIR,
1809 ("wrong vnode type %p", vp));
1810 dvp->v_cache_dd = ncp;
1814 if (vp->v_type == VDIR) {
1815 if (flag != NCF_ISDOTDOT) {
1817 * For this case, the cache entry maps both the
1818 * directory name in it and the name ".." for the
1819 * directory's parent.
1821 if ((ndd = vp->v_cache_dd) != NULL) {
1822 if ((ndd->nc_flag & NCF_ISDOTDOT) != 0)
1823 cache_zap_locked(ndd, false);
1827 vp->v_cache_dd = ncp;
1830 vp->v_cache_dd = NULL;
1834 if (flag != NCF_ISDOTDOT) {
1835 if (LIST_EMPTY(&dvp->v_cache_src)) {
1837 counter_u64_add(numcachehv, 1);
1839 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
1843 * Insert the new namecache entry into the appropriate chain
1844 * within the cache entries table.
1846 LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
1849 * If the entry is "negative", we place it into the
1850 * "negative" cache queue, otherwise, we place it into the
1851 * destination vnode's cache entries queue.
1854 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
1855 SDT_PROBE3(vfs, namecache, enter, done, dvp, ncp->nc_name,
1858 if (cnp->cn_flags & ISWHITEOUT)
1859 ncp->nc_flag |= NCF_WHITE;
1860 cache_negative_insert(ncp, false);
1861 SDT_PROBE2(vfs, namecache, enter_negative, done, dvp,
1864 cache_enter_unlock(&cel);
1865 if (numneg * ncnegfactor > lnumcache)
1866 cache_negative_zap_one();
1870 cache_enter_unlock(&cel);
1876 cache_roundup_2(u_int val)
1880 for (res = 1; res <= val; res <<= 1)
1887 * Name cache initialization, from vfs_init() when we are booting
1890 nchinit(void *dummy __unused)
1894 cache_zone_small = uma_zcreate("S VFS Cache",
1895 sizeof(struct namecache) + CACHE_PATH_CUTOFF + 1,
1896 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache),
1898 cache_zone_small_ts = uma_zcreate("STS VFS Cache",
1899 sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1,
1900 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache_ts),
1902 cache_zone_large = uma_zcreate("L VFS Cache",
1903 sizeof(struct namecache) + NAME_MAX + 1,
1904 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache),
1906 cache_zone_large_ts = uma_zcreate("LTS VFS Cache",
1907 sizeof(struct namecache_ts) + NAME_MAX + 1,
1908 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache_ts),
1911 ncsize = desiredvnodes * ncsizefactor;
1912 nchashtbl = hashinit(desiredvnodes * 2, M_VFSCACHE, &nchash);
1913 ncbuckethash = cache_roundup_2(mp_ncpus * mp_ncpus) - 1;
1914 if (ncbuckethash < 7) /* arbitrarily chosen to avoid having one lock */
1916 if (ncbuckethash > nchash)
1917 ncbuckethash = nchash;
1918 bucketlocks = malloc(sizeof(*bucketlocks) * numbucketlocks, M_VFSCACHE,
1920 for (i = 0; i < numbucketlocks; i++)
1921 rw_init_flags(&bucketlocks[i], "ncbuc", RW_DUPOK | RW_RECURSE);
1922 ncvnodehash = ncbuckethash;
1923 vnodelocks = malloc(sizeof(*vnodelocks) * numvnodelocks, M_VFSCACHE,
1925 for (i = 0; i < numvnodelocks; i++)
1926 mtx_init(&vnodelocks[i], "ncvn", NULL, MTX_DUPOK | MTX_RECURSE);
1927 ncpurgeminvnodes = numbucketlocks * 2;
1930 neglists = malloc(sizeof(*neglists) * numneglists, M_VFSCACHE,
1932 for (i = 0; i < numneglists; i++) {
1933 mtx_init(&neglists[i].nl_lock, "ncnegl", NULL, MTX_DEF);
1934 TAILQ_INIT(&neglists[i].nl_list);
1936 mtx_init(&ncneg_hot.nl_lock, "ncneglh", NULL, MTX_DEF);
1937 TAILQ_INIT(&ncneg_hot.nl_list);
1939 mtx_init(&ncneg_shrink_lock, "ncnegs", NULL, MTX_DEF);
1941 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL);
1944 cache_changesize(u_long newmaxvnodes)
1946 struct nchashhead *new_nchashtbl, *old_nchashtbl;
1947 u_long new_nchash, old_nchash;
1948 struct namecache *ncp;
1953 newncsize = newmaxvnodes * ncsizefactor;
1954 newmaxvnodes = cache_roundup_2(newmaxvnodes * 2);
1955 if (newmaxvnodes < numbucketlocks)
1956 newmaxvnodes = numbucketlocks;
1958 new_nchashtbl = hashinit(newmaxvnodes, M_VFSCACHE, &new_nchash);
1959 /* If same hash table size, nothing to do */
1960 if (nchash == new_nchash) {
1961 free(new_nchashtbl, M_VFSCACHE);
1965 * Move everything from the old hash table to the new table.
1966 * None of the namecache entries in the table can be removed
1967 * because to do so, they have to be removed from the hash table.
1969 cache_lock_all_vnodes();
1970 cache_lock_all_buckets();
1971 old_nchashtbl = nchashtbl;
1972 old_nchash = nchash;
1973 nchashtbl = new_nchashtbl;
1974 nchash = new_nchash;
1975 for (i = 0; i <= old_nchash; i++) {
1976 while ((ncp = LIST_FIRST(&old_nchashtbl[i])) != NULL) {
1977 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen,
1979 LIST_REMOVE(ncp, nc_hash);
1980 LIST_INSERT_HEAD(NCHHASH(hash), ncp, nc_hash);
1984 cache_unlock_all_buckets();
1985 cache_unlock_all_vnodes();
1986 free(old_nchashtbl, M_VFSCACHE);
1990 * Invalidate all entries from and to a particular vnode.
1993 cache_purge(struct vnode *vp)
1995 TAILQ_HEAD(, namecache) ncps;
1996 struct namecache *ncp, *nnp;
1997 struct mtx *vlp, *vlp2;
1999 CTR1(KTR_VFS, "cache_purge(%p)", vp);
2000 SDT_PROBE1(vfs, namecache, purge, done, vp);
2001 if (LIST_EMPTY(&vp->v_cache_src) && TAILQ_EMPTY(&vp->v_cache_dst) &&
2002 vp->v_cache_dd == NULL)
2005 vlp = VP2VNODELOCK(vp);
2009 while (!LIST_EMPTY(&vp->v_cache_src)) {
2010 ncp = LIST_FIRST(&vp->v_cache_src);
2011 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2013 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2015 while (!TAILQ_EMPTY(&vp->v_cache_dst)) {
2016 ncp = TAILQ_FIRST(&vp->v_cache_dst);
2017 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2019 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2021 ncp = vp->v_cache_dd;
2023 KASSERT(ncp->nc_flag & NCF_ISDOTDOT,
2024 ("lost dotdot link"));
2025 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2027 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2029 KASSERT(vp->v_cache_dd == NULL, ("incomplete purge"));
2033 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2039 * Invalidate all negative entries for a particular directory vnode.
2042 cache_purge_negative(struct vnode *vp)
2044 TAILQ_HEAD(, namecache) ncps;
2045 struct namecache *ncp, *nnp;
2048 CTR1(KTR_VFS, "cache_purge_negative(%p)", vp);
2049 SDT_PROBE1(vfs, namecache, purge_negative, done, vp);
2050 if (LIST_EMPTY(&vp->v_cache_src))
2053 vlp = VP2VNODELOCK(vp);
2055 LIST_FOREACH_SAFE(ncp, &vp->v_cache_src, nc_src, nnp) {
2056 if (!(ncp->nc_flag & NCF_NEGATIVE))
2058 cache_zap_negative_locked_vnode_kl(ncp, vp);
2059 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2062 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2068 * Flush all entries referencing a particular filesystem.
2071 cache_purgevfs(struct mount *mp, bool force)
2073 TAILQ_HEAD(, namecache) ncps;
2074 struct mtx *vlp1, *vlp2;
2076 struct nchashhead *bucket;
2077 struct namecache *ncp, *nnp;
2078 u_long i, j, n_nchash;
2081 /* Scan hash tables for applicable entries */
2082 SDT_PROBE1(vfs, namecache, purgevfs, done, mp);
2083 if (!force && mp->mnt_nvnodelistsize <= ncpurgeminvnodes)
2086 n_nchash = nchash + 1;
2088 for (i = 0; i < numbucketlocks; i++) {
2089 blp = (struct rwlock *)&bucketlocks[i];
2091 for (j = i; j < n_nchash; j += numbucketlocks) {
2093 bucket = &nchashtbl[j];
2094 LIST_FOREACH_SAFE(ncp, bucket, nc_hash, nnp) {
2095 cache_assert_bucket_locked(ncp, RA_WLOCKED);
2096 if (ncp->nc_dvp->v_mount != mp)
2098 error = cache_zap_wlocked_bucket_kl(ncp, blp,
2102 TAILQ_INSERT_HEAD(&ncps, ncp, nc_dst);
2106 if (vlp1 == NULL && vlp2 == NULL)
2107 cache_maybe_yield();
2114 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2120 * Perform canonical checks and cache lookup and pass on to filesystem
2121 * through the vop_cachedlookup only if needed.
2125 vfs_cache_lookup(struct vop_lookup_args *ap)
2129 struct vnode **vpp = ap->a_vpp;
2130 struct componentname *cnp = ap->a_cnp;
2131 int flags = cnp->cn_flags;
2136 if (dvp->v_type != VDIR)
2139 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
2140 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
2143 error = vn_dir_check_exec(dvp, cnp);
2147 error = cache_lookup(dvp, vpp, cnp, NULL, NULL);
2149 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
2155 /* Implementation of the getcwd syscall. */
2157 sys___getcwd(struct thread *td, struct __getcwd_args *uap)
2163 buflen = uap->buflen;
2164 if (__predict_false(buflen < 2))
2166 if (buflen > MAXPATHLEN)
2167 buflen = MAXPATHLEN;
2169 buf = malloc(buflen, M_TEMP, M_WAITOK);
2170 error = vn_getcwd(td, buf, &retbuf, &buflen);
2172 error = copyout(retbuf, uap->buf, buflen);
2178 vn_getcwd(struct thread *td, char *buf, char **retbuf, size_t *buflen)
2184 error = vn_fullpath_any(td, pwd->pwd_cdir, pwd->pwd_rdir, buf, retbuf, buflen);
2188 if (KTRPOINT(curthread, KTR_NAMEI) && error == 0)
2195 kern___realpathat(struct thread *td, int fd, const char *path, char *buf,
2196 size_t size, int flags, enum uio_seg pathseg)
2198 struct nameidata nd;
2199 char *retbuf, *freebuf;
2204 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | SAVENAME | WANTPARENT | AUDITVNODE1,
2205 pathseg, path, fd, &cap_fstat_rights, td);
2206 if ((error = namei(&nd)) != 0)
2208 error = vn_fullpath_hardlink(td, &nd, &retbuf, &freebuf, &size);
2210 error = copyout(retbuf, buf, size);
2211 free(freebuf, M_TEMP);
2218 sys___realpathat(struct thread *td, struct __realpathat_args *uap)
2221 return (kern___realpathat(td, uap->fd, uap->path, uap->buf, uap->size,
2222 uap->flags, UIO_USERSPACE));
2226 * Retrieve the full filesystem path that correspond to a vnode from the name
2227 * cache (if available)
2230 vn_fullpath(struct thread *td, struct vnode *vn, char **retbuf, char **freebuf)
2237 if (__predict_false(vn == NULL))
2240 buflen = MAXPATHLEN;
2241 buf = malloc(buflen, M_TEMP, M_WAITOK);
2243 error = vn_fullpath_any(td, vn, pwd->pwd_rdir, buf, retbuf, &buflen);
2254 * This function is similar to vn_fullpath, but it attempts to lookup the
2255 * pathname relative to the global root mount point. This is required for the
2256 * auditing sub-system, as audited pathnames must be absolute, relative to the
2257 * global root mount point.
2260 vn_fullpath_global(struct thread *td, struct vnode *vn,
2261 char **retbuf, char **freebuf)
2267 if (__predict_false(vn == NULL))
2269 buflen = MAXPATHLEN;
2270 buf = malloc(buflen, M_TEMP, M_WAITOK);
2271 error = vn_fullpath_any(td, vn, rootvnode, buf, retbuf, &buflen);
2280 vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, size_t *buflen)
2283 struct namecache *ncp;
2287 vlp = VP2VNODELOCK(*vp);
2289 TAILQ_FOREACH(ncp, &((*vp)->v_cache_dst), nc_dst) {
2290 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2294 if (*buflen < ncp->nc_nlen) {
2297 counter_u64_add(numfullpathfail4, 1);
2299 SDT_PROBE3(vfs, namecache, fullpath, return, error,
2303 *buflen -= ncp->nc_nlen;
2304 memcpy(buf + *buflen, ncp->nc_name, ncp->nc_nlen);
2305 SDT_PROBE3(vfs, namecache, fullpath, hit, ncp->nc_dvp,
2314 SDT_PROBE1(vfs, namecache, fullpath, miss, vp);
2317 vn_lock(*vp, LK_SHARED | LK_RETRY);
2318 error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen);
2321 counter_u64_add(numfullpathfail2, 1);
2322 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2327 if (VN_IS_DOOMED(dvp)) {
2328 /* forced unmount */
2331 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2335 * *vp has its use count incremented still.
2342 * Resolve a directory to a pathname.
2344 * The name of the directory can always be found in the namecache or fetched
2345 * from the filesystem. There is also guaranteed to be only one parent, meaning
2346 * we can just follow vnodes up until we find the root.
2348 * The vnode must be referenced.
2351 vn_fullpath_dir(struct thread *td, struct vnode *vp, struct vnode *rdir,
2352 char *buf, char **retbuf, size_t *len, bool slash_prefixed, size_t addend)
2354 #ifdef KDTRACE_HOOKS
2355 struct vnode *startvp = vp;
2361 VNPASS(vp->v_type == VDIR || VN_IS_DOOMED(vp), vp);
2362 VNPASS(vp->v_usecount > 0, vp);
2366 if (!slash_prefixed) {
2374 SDT_PROBE1(vfs, namecache, fullpath, entry, vp);
2375 counter_u64_add(numfullpathcalls, 1);
2376 while (vp != rdir && vp != rootvnode) {
2378 * The vp vnode must be already fully constructed,
2379 * since it is either found in namecache or obtained
2380 * from VOP_VPTOCNP(). We may test for VV_ROOT safely
2381 * without obtaining the vnode lock.
2383 if ((vp->v_vflag & VV_ROOT) != 0) {
2384 vn_lock(vp, LK_RETRY | LK_SHARED);
2387 * With the vnode locked, check for races with
2388 * unmount, forced or not. Note that we
2389 * already verified that vp is not equal to
2390 * the root vnode, which means that
2391 * mnt_vnodecovered can be NULL only for the
2394 if (VN_IS_DOOMED(vp) ||
2395 (vp1 = vp->v_mount->mnt_vnodecovered) == NULL ||
2396 vp1->v_mountedhere != vp->v_mount) {
2399 SDT_PROBE3(vfs, namecache, fullpath, return,
2409 if (vp->v_type != VDIR) {
2411 counter_u64_add(numfullpathfail1, 1);
2413 SDT_PROBE3(vfs, namecache, fullpath, return,
2417 error = vn_vptocnp(&vp, td->td_ucred, buf, &buflen);
2423 SDT_PROBE3(vfs, namecache, fullpath, return, error,
2427 buf[--buflen] = '/';
2428 slash_prefixed = true;
2432 if (!slash_prefixed) {
2435 counter_u64_add(numfullpathfail4, 1);
2436 SDT_PROBE3(vfs, namecache, fullpath, return, ENOMEM,
2440 buf[--buflen] = '/';
2442 counter_u64_add(numfullpathfound, 1);
2445 *retbuf = buf + buflen;
2446 SDT_PROBE3(vfs, namecache, fullpath, return, 0, startvp, *retbuf);
2453 * Resolve an arbitrary vnode to a pathname.
2456 * - hardlinks are not tracked, thus if the vnode is not a directory this can
2457 * resolve to a different path than the one used to find it
2458 * - namecache is not mandatory, meaning names are not guaranteed to be added
2459 * (in which case resolving fails)
2462 vn_fullpath_any(struct thread *td, struct vnode *vp, struct vnode *rdir,
2463 char *buf, char **retbuf, size_t *buflen)
2466 bool slash_prefixed;
2472 orig_buflen = *buflen;
2475 slash_prefixed = false;
2476 if (vp->v_type != VDIR) {
2478 buf[*buflen] = '\0';
2479 error = vn_vptocnp(&vp, td->td_ucred, buf, buflen);
2488 slash_prefixed = true;
2491 return (vn_fullpath_dir(td, vp, rdir, buf, retbuf, buflen, slash_prefixed,
2492 orig_buflen - *buflen));
2496 * Resolve an arbitrary vnode to a pathname (taking care of hardlinks).
2498 * Since the namecache does not track handlings, the caller is expected to first
2499 * look up the target vnode with SAVENAME | WANTPARENT flags passed to namei.
2501 * Then we have 2 cases:
2502 * - if the found vnode is a directory, the path can be constructed just by
2503 * fullowing names up the chain
2504 * - otherwise we populate the buffer with the saved name and start resolving
2508 vn_fullpath_hardlink(struct thread *td, struct nameidata *ndp, char **retbuf,
2509 char **freebuf, size_t *buflen)
2513 struct componentname *cnp;
2517 bool slash_prefixed;
2521 if (*buflen > MAXPATHLEN)
2522 *buflen = MAXPATHLEN;
2524 slash_prefixed = false;
2526 buf = malloc(*buflen, M_TEMP, M_WAITOK);
2531 if (vp->v_type != VDIR) {
2533 addend = cnp->cn_namelen + 2;
2534 if (*buflen < addend) {
2539 tmpbuf = buf + *buflen;
2541 memcpy(&tmpbuf[1], cnp->cn_nameptr, cnp->cn_namelen);
2542 tmpbuf[addend - 1] = '\0';
2543 slash_prefixed = true;
2548 error = vn_fullpath_dir(td, vp, pwd->pwd_rdir, buf, retbuf, buflen,
2549 slash_prefixed, addend);
2564 vn_dir_dd_ino(struct vnode *vp)
2566 struct namecache *ncp;
2571 ASSERT_VOP_LOCKED(vp, "vn_dir_dd_ino");
2572 vlp = VP2VNODELOCK(vp);
2574 TAILQ_FOREACH(ncp, &(vp->v_cache_dst), nc_dst) {
2575 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0)
2578 vs = vget_prep(ddvp);
2580 if (vget_finish(ddvp, LK_SHARED | LK_NOWAIT, vs))
2589 vn_commname(struct vnode *vp, char *buf, u_int buflen)
2591 struct namecache *ncp;
2595 vlp = VP2VNODELOCK(vp);
2597 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst)
2598 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2604 l = min(ncp->nc_nlen, buflen - 1);
2605 memcpy(buf, ncp->nc_name, l);
2612 * This function updates path string to vnode's full global path
2613 * and checks the size of the new path string against the pathlen argument.
2615 * Requires a locked, referenced vnode.
2616 * Vnode is re-locked on success or ENODEV, otherwise unlocked.
2618 * If vp is a directory, the call to vn_fullpath_global() always succeeds
2619 * because it falls back to the ".." lookup if the namecache lookup fails.
2622 vn_path_to_global_path(struct thread *td, struct vnode *vp, char *path,
2625 struct nameidata nd;
2630 ASSERT_VOP_ELOCKED(vp, __func__);
2632 /* Construct global filesystem path from vp. */
2634 error = vn_fullpath_global(td, vp, &rpath, &fbuf);
2641 if (strlen(rpath) >= pathlen) {
2643 error = ENAMETOOLONG;
2648 * Re-lookup the vnode by path to detect a possible rename.
2649 * As a side effect, the vnode is relocked.
2650 * If vnode was renamed, return ENOENT.
2652 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
2653 UIO_SYSSPACE, path, td);
2659 NDFREE(&nd, NDF_ONLY_PNBUF);
2663 strcpy(path, rpath);
2676 db_print_vpath(struct vnode *vp)
2679 while (vp != NULL) {
2680 db_printf("%p: ", vp);
2681 if (vp == rootvnode) {
2685 if (vp->v_vflag & VV_ROOT) {
2686 db_printf("<mount point>");
2687 vp = vp->v_mount->mnt_vnodecovered;
2689 struct namecache *ncp;
2693 ncp = TAILQ_FIRST(&vp->v_cache_dst);
2696 for (i = 0; i < ncp->nc_nlen; i++)
2697 db_printf("%c", *ncn++);
2710 DB_SHOW_COMMAND(vpath, db_show_vpath)
2715 db_printf("usage: show vpath <struct vnode *>\n");
2719 vp = (struct vnode *)addr;