2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993, 1995
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Poul-Henning Kamp of the FreeBSD Project.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)vfs_cache.c 8.5 (Berkeley) 3/22/95
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
41 #include "opt_ktrace.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/counter.h>
46 #include <sys/filedesc.h>
47 #include <sys/fnv_hash.h>
48 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #include <sys/fcntl.h>
53 #include <sys/mount.h>
54 #include <sys/namei.h>
56 #include <sys/rwlock.h>
59 #include <sys/syscallsubr.h>
60 #include <sys/sysctl.h>
61 #include <sys/sysproto.h>
62 #include <sys/vnode.h>
64 #include <sys/ktrace.h>
73 SDT_PROVIDER_DECLARE(vfs);
74 SDT_PROBE_DEFINE3(vfs, namecache, enter, done, "struct vnode *", "char *",
76 SDT_PROBE_DEFINE2(vfs, namecache, enter_negative, done, "struct vnode *",
78 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, entry, "struct vnode *");
79 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, hit, "struct vnode *",
80 "char *", "struct vnode *");
81 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, miss, "struct vnode *");
82 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, return, "int",
83 "struct vnode *", "char *");
84 SDT_PROBE_DEFINE3(vfs, namecache, lookup, hit, "struct vnode *", "char *",
86 SDT_PROBE_DEFINE2(vfs, namecache, lookup, hit__negative,
87 "struct vnode *", "char *");
88 SDT_PROBE_DEFINE2(vfs, namecache, lookup, miss, "struct vnode *",
90 SDT_PROBE_DEFINE1(vfs, namecache, purge, done, "struct vnode *");
91 SDT_PROBE_DEFINE1(vfs, namecache, purge_negative, done, "struct vnode *");
92 SDT_PROBE_DEFINE1(vfs, namecache, purgevfs, done, "struct mount *");
93 SDT_PROBE_DEFINE3(vfs, namecache, zap, done, "struct vnode *", "char *",
95 SDT_PROBE_DEFINE2(vfs, namecache, zap_negative, done, "struct vnode *",
97 SDT_PROBE_DEFINE2(vfs, namecache, shrink_negative, done, "struct vnode *",
101 * This structure describes the elements in the cache of recent
102 * names looked up by namei.
106 LIST_ENTRY(namecache) nc_hash; /* hash chain */
107 LIST_ENTRY(namecache) nc_src; /* source vnode list */
108 TAILQ_ENTRY(namecache) nc_dst; /* destination vnode list */
109 struct vnode *nc_dvp; /* vnode of parent of name */
111 struct vnode *nu_vp; /* vnode the name refers to */
113 u_char nc_flag; /* flag bits */
114 u_char nc_nlen; /* length of name */
115 char nc_name[0]; /* segment name + nul */
119 * struct namecache_ts repeats struct namecache layout up to the
121 * struct namecache_ts is used in place of struct namecache when time(s) need
122 * to be stored. The nc_dotdottime field is used when a cache entry is mapping
123 * both a non-dotdot directory name plus dotdot for the directory's
126 struct namecache_ts {
127 struct timespec nc_time; /* timespec provided by fs */
128 struct timespec nc_dotdottime; /* dotdot timespec provided by fs */
129 int nc_ticks; /* ticks value when entry was added */
130 struct namecache nc_nc;
133 #define nc_vp n_un.nu_vp
136 * Flags in namecache.nc_flag
138 #define NCF_WHITE 0x01
139 #define NCF_ISDOTDOT 0x02
142 #define NCF_DVDROP 0x10
143 #define NCF_NEGATIVE 0x20
144 #define NCF_HOTNEGATIVE 0x40
147 * Name caching works as follows:
149 * Names found by directory scans are retained in a cache
150 * for future reference. It is managed LRU, so frequently
151 * used names will hang around. Cache is indexed by hash value
152 * obtained from (dvp, name) where dvp refers to the directory
155 * If it is a "negative" entry, (i.e. for a name that is known NOT to
156 * exist) the vnode pointer will be NULL.
158 * Upon reaching the last segment of a path, if the reference
159 * is for DELETE, or NOCACHE is set (rewrite), and the
160 * name is located in the cache, it will be dropped.
162 * These locks are used (in the order in which they can be taken):
164 * vnodelock mtx vnode lists and v_cache_dd field protection
165 * bucketlock rwlock for access to given set of hash buckets
166 * neglist mtx negative entry LRU management
168 * Additionally, ncneg_shrink_lock mtx is used to have at most one thread
169 * shrinking the LRU list.
171 * It is legal to take multiple vnodelock and bucketlock locks. The locking
172 * order is lower address first. Both are recursive.
174 * "." lookups are lockless.
176 * ".." and vnode -> name lookups require vnodelock.
178 * name -> vnode lookup requires the relevant bucketlock to be held for reading.
180 * Insertions and removals of entries require involved vnodes and bucketlocks
181 * to be write-locked to prevent other threads from seeing the entry.
183 * Some lookups result in removal of the found entry (e.g. getting rid of a
184 * negative entry with the intent to create a positive one), which poses a
185 * problem when multiple threads reach the state. Similarly, two different
186 * threads can purge two different vnodes and try to remove the same name.
188 * If the already held vnode lock is lower than the second required lock, we
189 * can just take the other lock. However, in the opposite case, this could
190 * deadlock. As such, this is resolved by trylocking and if that fails unlocking
191 * the first node, locking everything in order and revalidating the state.
195 * Structures associated with name caching.
197 #define NCHHASH(hash) \
198 (&nchashtbl[(hash) & nchash])
199 static __read_mostly LIST_HEAD(nchashhead, namecache) *nchashtbl;/* Hash Table */
200 static u_long __read_mostly nchash; /* size of hash table */
201 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0,
202 "Size of namecache hash table");
203 static u_long __read_mostly ncnegfactor = 5; /* ratio of negative entries */
204 SYSCTL_ULONG(_vfs, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0,
205 "Ratio of negative namecache entries");
206 static u_long __exclusive_cache_line numneg; /* number of negative entries allocated */
207 SYSCTL_ULONG(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0,
208 "Number of negative entries in namecache");
209 static u_long __exclusive_cache_line numcache;/* number of cache entries allocated */
210 SYSCTL_ULONG(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0,
211 "Number of namecache entries");
212 static u_long __exclusive_cache_line numcachehv;/* number of cache entries with vnodes held */
213 SYSCTL_ULONG(_debug, OID_AUTO, numcachehv, CTLFLAG_RD, &numcachehv, 0,
214 "Number of namecache entries with vnodes held");
215 u_int ncsizefactor = 2;
216 SYSCTL_UINT(_vfs, OID_AUTO, ncsizefactor, CTLFLAG_RW, &ncsizefactor, 0,
217 "Size factor for namecache");
218 static u_int __read_mostly ncpurgeminvnodes;
219 SYSCTL_UINT(_vfs, OID_AUTO, ncpurgeminvnodes, CTLFLAG_RW, &ncpurgeminvnodes, 0,
220 "Number of vnodes below which purgevfs ignores the request");
221 static u_int __read_mostly ncsize; /* the size as computed on creation or resizing */
223 struct nchstats nchstats; /* cache effectiveness statistics */
225 static struct mtx __exclusive_cache_line ncneg_shrink_lock;
226 static int shrink_list_turn;
230 TAILQ_HEAD(, namecache) nl_list;
231 } __aligned(CACHE_LINE_SIZE);
233 static struct neglist __read_mostly *neglists;
234 static struct neglist ncneg_hot;
235 static u_long numhotneg;
237 #define numneglists (ncneghash + 1)
238 static u_int __read_mostly ncneghash;
239 static inline struct neglist *
240 NCP2NEGLIST(struct namecache *ncp)
243 return (&neglists[(((uintptr_t)(ncp) >> 8) & ncneghash)]);
246 #define numbucketlocks (ncbuckethash + 1)
247 static u_int __read_mostly ncbuckethash;
248 static struct rwlock_padalign __read_mostly *bucketlocks;
249 #define HASH2BUCKETLOCK(hash) \
250 ((struct rwlock *)(&bucketlocks[((hash) & ncbuckethash)]))
252 #define numvnodelocks (ncvnodehash + 1)
253 static u_int __read_mostly ncvnodehash;
254 static struct mtx __read_mostly *vnodelocks;
255 static inline struct mtx *
256 VP2VNODELOCK(struct vnode *vp)
259 return (&vnodelocks[(((uintptr_t)(vp) >> 8) & ncvnodehash)]);
263 * UMA zones for the VFS cache.
265 * The small cache is used for entries with short names, which are the
266 * most common. The large cache is used for entries which are too big to
267 * fit in the small cache.
269 static uma_zone_t __read_mostly cache_zone_small;
270 static uma_zone_t __read_mostly cache_zone_small_ts;
271 static uma_zone_t __read_mostly cache_zone_large;
272 static uma_zone_t __read_mostly cache_zone_large_ts;
274 #define CACHE_PATH_CUTOFF 35
276 static struct namecache *
277 cache_alloc(int len, int ts)
279 struct namecache_ts *ncp_ts;
280 struct namecache *ncp;
282 if (__predict_false(ts)) {
283 if (len <= CACHE_PATH_CUTOFF)
284 ncp_ts = uma_zalloc(cache_zone_small_ts, M_WAITOK);
286 ncp_ts = uma_zalloc(cache_zone_large_ts, M_WAITOK);
287 ncp = &ncp_ts->nc_nc;
289 if (len <= CACHE_PATH_CUTOFF)
290 ncp = uma_zalloc(cache_zone_small, M_WAITOK);
292 ncp = uma_zalloc(cache_zone_large, M_WAITOK);
298 cache_free(struct namecache *ncp)
300 struct namecache_ts *ncp_ts;
304 if ((ncp->nc_flag & NCF_DVDROP) != 0)
306 if (__predict_false(ncp->nc_flag & NCF_TS)) {
307 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
308 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
309 uma_zfree(cache_zone_small_ts, ncp_ts);
311 uma_zfree(cache_zone_large_ts, ncp_ts);
313 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
314 uma_zfree(cache_zone_small, ncp);
316 uma_zfree(cache_zone_large, ncp);
321 cache_out_ts(struct namecache *ncp, struct timespec *tsp, int *ticksp)
323 struct namecache_ts *ncp_ts;
325 KASSERT((ncp->nc_flag & NCF_TS) != 0 ||
326 (tsp == NULL && ticksp == NULL),
329 if (tsp == NULL && ticksp == NULL)
332 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
334 *tsp = ncp_ts->nc_time;
336 *ticksp = ncp_ts->nc_ticks;
339 static int __read_mostly doingcache = 1; /* 1 => enable the cache */
340 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0,
341 "VFS namecache enabled");
343 /* Export size information to userland */
344 SYSCTL_INT(_debug_sizeof, OID_AUTO, namecache, CTLFLAG_RD, SYSCTL_NULL_INT_PTR,
345 sizeof(struct namecache), "sizeof(struct namecache)");
348 * The new name cache statistics
350 static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0,
351 "Name cache statistics");
352 #define STATNODE_ULONG(name, descr) \
353 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, descr);
354 #define STATNODE_COUNTER(name, descr) \
355 static counter_u64_t __read_mostly name; \
356 SYSCTL_COUNTER_U64(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, descr);
357 STATNODE_ULONG(numneg, "Number of negative cache entries");
358 STATNODE_ULONG(numcache, "Number of cache entries");
359 STATNODE_COUNTER(numcalls, "Number of cache lookups");
360 STATNODE_COUNTER(dothits, "Number of '.' hits");
361 STATNODE_COUNTER(dotdothits, "Number of '..' hits");
362 STATNODE_COUNTER(numchecks, "Number of checks in lookup");
363 STATNODE_COUNTER(nummiss, "Number of cache misses");
364 STATNODE_COUNTER(nummisszap, "Number of cache misses we do not want to cache");
365 STATNODE_COUNTER(numposzaps,
366 "Number of cache hits (positive) we do not want to cache");
367 STATNODE_COUNTER(numposhits, "Number of cache hits (positive)");
368 STATNODE_COUNTER(numnegzaps,
369 "Number of cache hits (negative) we do not want to cache");
370 STATNODE_COUNTER(numneghits, "Number of cache hits (negative)");
371 /* These count for kern___getcwd(), too. */
372 STATNODE_COUNTER(numfullpathcalls, "Number of fullpath search calls");
373 STATNODE_COUNTER(numfullpathfail1, "Number of fullpath search errors (ENOTDIR)");
374 STATNODE_COUNTER(numfullpathfail2,
375 "Number of fullpath search errors (VOP_VPTOCNP failures)");
376 STATNODE_COUNTER(numfullpathfail4, "Number of fullpath search errors (ENOMEM)");
377 STATNODE_COUNTER(numfullpathfound, "Number of successful fullpath calls");
378 STATNODE_COUNTER(zap_and_exit_bucket_relock_success,
379 "Number of successful removals after relocking");
380 static long zap_and_exit_bucket_fail; STATNODE_ULONG(zap_and_exit_bucket_fail,
381 "Number of times zap_and_exit failed to lock");
382 static long zap_and_exit_bucket_fail2; STATNODE_ULONG(zap_and_exit_bucket_fail2,
383 "Number of times zap_and_exit failed to lock");
384 static long cache_lock_vnodes_cel_3_failures;
385 STATNODE_ULONG(cache_lock_vnodes_cel_3_failures,
386 "Number of times 3-way vnode locking failed");
387 STATNODE_ULONG(numhotneg, "Number of hot negative entries");
388 STATNODE_COUNTER(numneg_evicted,
389 "Number of negative entries evicted when adding a new entry");
390 STATNODE_COUNTER(shrinking_skipped,
391 "Number of times shrinking was already in progress");
393 static void cache_zap_locked(struct namecache *ncp, bool neg_locked);
394 static int vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
395 char *buf, char **retbuf, u_int buflen);
397 static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
399 static int cache_yield;
400 SYSCTL_INT(_vfs_cache, OID_AUTO, yield, CTLFLAG_RD, &cache_yield, 0,
401 "Number of times cache called yield");
403 static void __noinline
404 cache_maybe_yield(void)
407 if (should_yield()) {
409 kern_yield(PRI_USER);
414 cache_assert_vlp_locked(struct mtx *vlp)
418 mtx_assert(vlp, MA_OWNED);
422 cache_assert_vnode_locked(struct vnode *vp)
426 vlp = VP2VNODELOCK(vp);
427 cache_assert_vlp_locked(vlp);
431 cache_get_hash(char *name, u_char len, struct vnode *dvp)
435 hash = fnv_32_buf(name, len, FNV1_32_INIT);
436 hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
440 static inline struct rwlock *
441 NCP2BUCKETLOCK(struct namecache *ncp)
445 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen, ncp->nc_dvp);
446 return (HASH2BUCKETLOCK(hash));
451 cache_assert_bucket_locked(struct namecache *ncp, int mode)
455 blp = NCP2BUCKETLOCK(ncp);
456 rw_assert(blp, mode);
459 #define cache_assert_bucket_locked(x, y) do { } while (0)
462 #define cache_sort_vnodes(x, y) _cache_sort_vnodes((void **)(x), (void **)(y))
464 _cache_sort_vnodes(void **p1, void **p2)
468 MPASS(*p1 != NULL || *p2 != NULL);
478 cache_lock_all_buckets(void)
482 for (i = 0; i < numbucketlocks; i++)
483 rw_wlock(&bucketlocks[i]);
487 cache_unlock_all_buckets(void)
491 for (i = 0; i < numbucketlocks; i++)
492 rw_wunlock(&bucketlocks[i]);
496 cache_lock_all_vnodes(void)
500 for (i = 0; i < numvnodelocks; i++)
501 mtx_lock(&vnodelocks[i]);
505 cache_unlock_all_vnodes(void)
509 for (i = 0; i < numvnodelocks; i++)
510 mtx_unlock(&vnodelocks[i]);
514 cache_trylock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
517 cache_sort_vnodes(&vlp1, &vlp2);
520 if (!mtx_trylock(vlp1))
523 if (!mtx_trylock(vlp2)) {
533 cache_lock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
536 MPASS(vlp1 != NULL || vlp2 != NULL);
546 cache_unlock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
549 MPASS(vlp1 != NULL || vlp2 != NULL);
558 sysctl_nchstats(SYSCTL_HANDLER_ARGS)
560 struct nchstats snap;
562 if (req->oldptr == NULL)
563 return (SYSCTL_OUT(req, 0, sizeof(snap)));
566 snap.ncs_goodhits = counter_u64_fetch(numposhits);
567 snap.ncs_neghits = counter_u64_fetch(numneghits);
568 snap.ncs_badhits = counter_u64_fetch(numposzaps) +
569 counter_u64_fetch(numnegzaps);
570 snap.ncs_miss = counter_u64_fetch(nummisszap) +
571 counter_u64_fetch(nummiss);
573 return (SYSCTL_OUT(req, &snap, sizeof(snap)));
575 SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE | CTLFLAG_RD |
576 CTLFLAG_MPSAFE, 0, 0, sysctl_nchstats, "LU",
577 "VFS cache effectiveness statistics");
581 * Grab an atomic snapshot of the name cache hash chain lengths
583 static SYSCTL_NODE(_debug, OID_AUTO, hashstat, CTLFLAG_RW, NULL,
587 sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS)
589 struct nchashhead *ncpp;
590 struct namecache *ncp;
591 int i, error, n_nchash, *cntbuf;
594 n_nchash = nchash + 1; /* nchash is max index, not count */
595 if (req->oldptr == NULL)
596 return SYSCTL_OUT(req, 0, n_nchash * sizeof(int));
597 cntbuf = malloc(n_nchash * sizeof(int), M_TEMP, M_ZERO | M_WAITOK);
598 cache_lock_all_buckets();
599 if (n_nchash != nchash + 1) {
600 cache_unlock_all_buckets();
601 free(cntbuf, M_TEMP);
604 /* Scan hash tables counting entries */
605 for (ncpp = nchashtbl, i = 0; i < n_nchash; ncpp++, i++)
606 LIST_FOREACH(ncp, ncpp, nc_hash)
608 cache_unlock_all_buckets();
609 for (error = 0, i = 0; i < n_nchash; i++)
610 if ((error = SYSCTL_OUT(req, &cntbuf[i], sizeof(int))) != 0)
612 free(cntbuf, M_TEMP);
615 SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD|
616 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_rawnchash, "S,int",
617 "nchash chain lengths");
620 sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS)
623 struct nchashhead *ncpp;
624 struct namecache *ncp;
626 int count, maxlength, used, pct;
629 return SYSCTL_OUT(req, 0, 4 * sizeof(int));
631 cache_lock_all_buckets();
632 n_nchash = nchash + 1; /* nchash is max index, not count */
636 /* Scan hash tables for applicable entries */
637 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
639 LIST_FOREACH(ncp, ncpp, nc_hash) {
644 if (maxlength < count)
647 n_nchash = nchash + 1;
648 cache_unlock_all_buckets();
649 pct = (used * 100) / (n_nchash / 100);
650 error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash));
653 error = SYSCTL_OUT(req, &used, sizeof(used));
656 error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength));
659 error = SYSCTL_OUT(req, &pct, sizeof(pct));
664 SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD|
665 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_nchash, "I",
666 "nchash statistics (number of total/used buckets, maximum chain length, usage percentage)");
670 * Negative entries management
672 * A variation of LRU scheme is used. New entries are hashed into one of
673 * numneglists cold lists. Entries get promoted to the hot list on first hit.
675 * The shrinker will demote hot list head and evict from the cold list in a
676 * round-robin manner.
679 cache_negative_hit(struct namecache *ncp)
681 struct neglist *neglist;
683 MPASS(ncp->nc_flag & NCF_NEGATIVE);
684 if (ncp->nc_flag & NCF_HOTNEGATIVE)
686 neglist = NCP2NEGLIST(ncp);
687 mtx_lock(&ncneg_hot.nl_lock);
688 mtx_lock(&neglist->nl_lock);
689 if (!(ncp->nc_flag & NCF_HOTNEGATIVE)) {
691 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
692 TAILQ_INSERT_TAIL(&ncneg_hot.nl_list, ncp, nc_dst);
693 ncp->nc_flag |= NCF_HOTNEGATIVE;
695 mtx_unlock(&neglist->nl_lock);
696 mtx_unlock(&ncneg_hot.nl_lock);
700 cache_negative_insert(struct namecache *ncp, bool neg_locked)
702 struct neglist *neglist;
704 MPASS(ncp->nc_flag & NCF_NEGATIVE);
705 cache_assert_bucket_locked(ncp, RA_WLOCKED);
706 neglist = NCP2NEGLIST(ncp);
708 mtx_lock(&neglist->nl_lock);
710 mtx_assert(&neglist->nl_lock, MA_OWNED);
712 TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst);
714 mtx_unlock(&neglist->nl_lock);
715 atomic_add_rel_long(&numneg, 1);
719 cache_negative_remove(struct namecache *ncp, bool neg_locked)
721 struct neglist *neglist;
722 bool hot_locked = false;
723 bool list_locked = false;
725 MPASS(ncp->nc_flag & NCF_NEGATIVE);
726 cache_assert_bucket_locked(ncp, RA_WLOCKED);
727 neglist = NCP2NEGLIST(ncp);
729 if (ncp->nc_flag & NCF_HOTNEGATIVE) {
731 mtx_lock(&ncneg_hot.nl_lock);
732 if (!(ncp->nc_flag & NCF_HOTNEGATIVE)) {
734 mtx_lock(&neglist->nl_lock);
738 mtx_lock(&neglist->nl_lock);
741 if (ncp->nc_flag & NCF_HOTNEGATIVE) {
742 mtx_assert(&ncneg_hot.nl_lock, MA_OWNED);
743 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
746 mtx_assert(&neglist->nl_lock, MA_OWNED);
747 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
750 mtx_unlock(&neglist->nl_lock);
752 mtx_unlock(&ncneg_hot.nl_lock);
753 atomic_subtract_rel_long(&numneg, 1);
757 cache_negative_shrink_select(int start, struct namecache **ncpp,
758 struct neglist **neglistpp)
760 struct neglist *neglist;
761 struct namecache *ncp;
767 for (i = start; i < numneglists; i++) {
768 neglist = &neglists[i];
769 if (TAILQ_FIRST(&neglist->nl_list) == NULL)
771 mtx_lock(&neglist->nl_lock);
772 ncp = TAILQ_FIRST(&neglist->nl_list);
775 mtx_unlock(&neglist->nl_lock);
778 *neglistpp = neglist;
783 cache_negative_zap_one(void)
785 struct namecache *ncp, *ncp2;
786 struct neglist *neglist;
790 if (mtx_owner(&ncneg_shrink_lock) != NULL ||
791 !mtx_trylock(&ncneg_shrink_lock)) {
792 counter_u64_add(shrinking_skipped, 1);
796 mtx_lock(&ncneg_hot.nl_lock);
797 ncp = TAILQ_FIRST(&ncneg_hot.nl_list);
799 neglist = NCP2NEGLIST(ncp);
800 mtx_lock(&neglist->nl_lock);
801 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
802 TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst);
803 ncp->nc_flag &= ~NCF_HOTNEGATIVE;
805 mtx_unlock(&neglist->nl_lock);
807 mtx_unlock(&ncneg_hot.nl_lock);
809 cache_negative_shrink_select(shrink_list_turn, &ncp, &neglist);
811 if (shrink_list_turn == numneglists)
812 shrink_list_turn = 0;
813 if (ncp == NULL && shrink_list_turn == 0)
814 cache_negative_shrink_select(shrink_list_turn, &ncp, &neglist);
815 mtx_unlock(&ncneg_shrink_lock);
819 MPASS(ncp->nc_flag & NCF_NEGATIVE);
820 dvlp = VP2VNODELOCK(ncp->nc_dvp);
821 blp = NCP2BUCKETLOCK(ncp);
822 mtx_unlock(&neglist->nl_lock);
825 mtx_lock(&neglist->nl_lock);
826 ncp2 = TAILQ_FIRST(&neglist->nl_list);
827 if (ncp != ncp2 || dvlp != VP2VNODELOCK(ncp2->nc_dvp) ||
828 blp != NCP2BUCKETLOCK(ncp2) || !(ncp2->nc_flag & NCF_NEGATIVE)) {
831 SDT_PROBE2(vfs, namecache, shrink_negative, done, ncp->nc_dvp,
834 cache_zap_locked(ncp, true);
835 counter_u64_add(numneg_evicted, 1);
837 mtx_unlock(&neglist->nl_lock);
844 * cache_zap_locked():
846 * Removes a namecache entry from cache, whether it contains an actual
847 * pointer to a vnode or if it is just a negative cache entry.
850 cache_zap_locked(struct namecache *ncp, bool neg_locked)
853 if (!(ncp->nc_flag & NCF_NEGATIVE))
854 cache_assert_vnode_locked(ncp->nc_vp);
855 cache_assert_vnode_locked(ncp->nc_dvp);
856 cache_assert_bucket_locked(ncp, RA_WLOCKED);
858 CTR2(KTR_VFS, "cache_zap(%p) vp %p", ncp,
859 (ncp->nc_flag & NCF_NEGATIVE) ? NULL : ncp->nc_vp);
860 LIST_REMOVE(ncp, nc_hash);
861 if (!(ncp->nc_flag & NCF_NEGATIVE)) {
862 SDT_PROBE3(vfs, namecache, zap, done, ncp->nc_dvp,
863 ncp->nc_name, ncp->nc_vp);
864 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
865 if (ncp == ncp->nc_vp->v_cache_dd)
866 ncp->nc_vp->v_cache_dd = NULL;
868 SDT_PROBE2(vfs, namecache, zap_negative, done, ncp->nc_dvp,
870 cache_negative_remove(ncp, neg_locked);
872 if (ncp->nc_flag & NCF_ISDOTDOT) {
873 if (ncp == ncp->nc_dvp->v_cache_dd)
874 ncp->nc_dvp->v_cache_dd = NULL;
876 LIST_REMOVE(ncp, nc_src);
877 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) {
878 ncp->nc_flag |= NCF_DVDROP;
879 atomic_subtract_rel_long(&numcachehv, 1);
882 atomic_subtract_rel_long(&numcache, 1);
886 cache_zap_negative_locked_vnode_kl(struct namecache *ncp, struct vnode *vp)
890 MPASS(ncp->nc_dvp == vp);
891 MPASS(ncp->nc_flag & NCF_NEGATIVE);
892 cache_assert_vnode_locked(vp);
894 blp = NCP2BUCKETLOCK(ncp);
896 cache_zap_locked(ncp, false);
901 cache_zap_locked_vnode_kl2(struct namecache *ncp, struct vnode *vp,
904 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
907 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
908 cache_assert_vnode_locked(vp);
910 if (ncp->nc_flag & NCF_NEGATIVE) {
915 cache_zap_negative_locked_vnode_kl(ncp, vp);
919 pvlp = VP2VNODELOCK(vp);
920 blp = NCP2BUCKETLOCK(ncp);
921 vlp1 = VP2VNODELOCK(ncp->nc_dvp);
922 vlp2 = VP2VNODELOCK(ncp->nc_vp);
924 if (*vlpp == vlp1 || *vlpp == vlp2) {
932 cache_sort_vnodes(&vlp1, &vlp2);
937 if (!mtx_trylock(vlp1))
943 cache_zap_locked(ncp, false);
945 if (to_unlock != NULL)
946 mtx_unlock(to_unlock);
953 MPASS(*vlpp == NULL);
958 static int __noinline
959 cache_zap_locked_vnode(struct namecache *ncp, struct vnode *vp)
961 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
965 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
966 cache_assert_vnode_locked(vp);
968 pvlp = VP2VNODELOCK(vp);
969 if (ncp->nc_flag & NCF_NEGATIVE) {
970 cache_zap_negative_locked_vnode_kl(ncp, vp);
974 blp = NCP2BUCKETLOCK(ncp);
975 vlp1 = VP2VNODELOCK(ncp->nc_dvp);
976 vlp2 = VP2VNODELOCK(ncp->nc_vp);
977 cache_sort_vnodes(&vlp1, &vlp2);
982 if (!mtx_trylock(vlp1)) {
989 cache_zap_locked(ncp, false);
991 mtx_unlock(to_unlock);
998 * If trylocking failed we can get here. We know enough to take all needed locks
999 * in the right order and re-lookup the entry.
1002 cache_zap_unlocked_bucket(struct namecache *ncp, struct componentname *cnp,
1003 struct vnode *dvp, struct mtx *dvlp, struct mtx *vlp, uint32_t hash,
1006 struct namecache *rncp;
1008 cache_assert_bucket_locked(ncp, RA_UNLOCKED);
1010 cache_sort_vnodes(&dvlp, &vlp);
1011 cache_lock_vnodes(dvlp, vlp);
1013 LIST_FOREACH(rncp, (NCHHASH(hash)), nc_hash) {
1014 if (rncp == ncp && rncp->nc_dvp == dvp &&
1015 rncp->nc_nlen == cnp->cn_namelen &&
1016 !bcmp(rncp->nc_name, cnp->cn_nameptr, rncp->nc_nlen))
1020 cache_zap_locked(rncp, false);
1022 cache_unlock_vnodes(dvlp, vlp);
1023 counter_u64_add(zap_and_exit_bucket_relock_success, 1);
1028 cache_unlock_vnodes(dvlp, vlp);
1032 static int __noinline
1033 cache_zap_wlocked_bucket(struct namecache *ncp, struct componentname *cnp,
1034 uint32_t hash, struct rwlock *blp)
1036 struct mtx *dvlp, *vlp;
1039 cache_assert_bucket_locked(ncp, RA_WLOCKED);
1041 dvlp = VP2VNODELOCK(ncp->nc_dvp);
1043 if (!(ncp->nc_flag & NCF_NEGATIVE))
1044 vlp = VP2VNODELOCK(ncp->nc_vp);
1045 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1046 cache_zap_locked(ncp, false);
1048 cache_unlock_vnodes(dvlp, vlp);
1054 return (cache_zap_unlocked_bucket(ncp, cnp, dvp, dvlp, vlp, hash, blp));
1057 static int __noinline
1058 cache_zap_rlocked_bucket(struct namecache *ncp, struct componentname *cnp,
1059 uint32_t hash, struct rwlock *blp)
1061 struct mtx *dvlp, *vlp;
1064 cache_assert_bucket_locked(ncp, RA_RLOCKED);
1066 dvlp = VP2VNODELOCK(ncp->nc_dvp);
1068 if (!(ncp->nc_flag & NCF_NEGATIVE))
1069 vlp = VP2VNODELOCK(ncp->nc_vp);
1070 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1073 cache_zap_locked(ncp, false);
1075 cache_unlock_vnodes(dvlp, vlp);
1081 return (cache_zap_unlocked_bucket(ncp, cnp, dvp, dvlp, vlp, hash, blp));
1085 cache_zap_wlocked_bucket_kl(struct namecache *ncp, struct rwlock *blp,
1086 struct mtx **vlpp1, struct mtx **vlpp2)
1088 struct mtx *dvlp, *vlp;
1090 cache_assert_bucket_locked(ncp, RA_WLOCKED);
1092 dvlp = VP2VNODELOCK(ncp->nc_dvp);
1094 if (!(ncp->nc_flag & NCF_NEGATIVE))
1095 vlp = VP2VNODELOCK(ncp->nc_vp);
1096 cache_sort_vnodes(&dvlp, &vlp);
1098 if (*vlpp1 == dvlp && *vlpp2 == vlp) {
1099 cache_zap_locked(ncp, false);
1100 cache_unlock_vnodes(dvlp, vlp);
1113 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1114 cache_zap_locked(ncp, false);
1115 cache_unlock_vnodes(dvlp, vlp);
1130 cache_lookup_unlock(struct rwlock *blp, struct mtx *vlp)
1140 static int __noinline
1141 cache_lookup_dot(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1142 struct timespec *tsp, int *ticksp)
1147 CTR2(KTR_VFS, "cache_lookup(%p, %s) found via .",
1148 dvp, cnp->cn_nameptr);
1149 counter_u64_add(dothits, 1);
1150 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", *vpp);
1157 * When we lookup "." we still can be asked to lock it
1160 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
1161 if (ltype != VOP_ISLOCKED(*vpp)) {
1162 if (ltype == LK_EXCLUSIVE) {
1163 vn_lock(*vpp, LK_UPGRADE | LK_RETRY);
1164 if ((*vpp)->v_iflag & VI_DOOMED) {
1165 /* forced unmount */
1171 vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY);
1176 static __noinline int
1177 cache_lookup_nomakeentry(struct vnode *dvp, struct vnode **vpp,
1178 struct componentname *cnp, struct timespec *tsp, int *ticksp)
1180 struct namecache *ncp;
1182 struct mtx *dvlp, *dvlp2;
1186 if (cnp->cn_namelen == 2 &&
1187 cnp->cn_nameptr[0] == '.' && cnp->cn_nameptr[1] == '.') {
1188 counter_u64_add(dotdothits, 1);
1189 dvlp = VP2VNODELOCK(dvp);
1193 ncp = dvp->v_cache_dd;
1195 SDT_PROBE3(vfs, namecache, lookup, miss, dvp,
1202 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1203 if (ncp->nc_dvp != dvp)
1204 panic("dvp %p v_cache_dd %p\n", dvp, ncp);
1205 if (!cache_zap_locked_vnode_kl2(ncp,
1208 MPASS(dvp->v_cache_dd == NULL);
1214 dvp->v_cache_dd = NULL;
1222 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1223 blp = HASH2BUCKETLOCK(hash);
1225 if (LIST_EMPTY(NCHHASH(hash)))
1230 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1231 counter_u64_add(numchecks, 1);
1232 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1233 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1237 /* We failed to find an entry */
1243 error = cache_zap_wlocked_bucket(ncp, cnp, hash, blp);
1244 if (__predict_false(error != 0)) {
1245 zap_and_exit_bucket_fail++;
1246 cache_maybe_yield();
1249 counter_u64_add(numposzaps, 1);
1253 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr, NULL);
1254 counter_u64_add(nummisszap, 1);
1259 * Lookup a name in the name cache
1263 * - dvp: Parent directory in which to search.
1264 * - vpp: Return argument. Will contain desired vnode on cache hit.
1265 * - cnp: Parameters of the name search. The most interesting bits of
1266 * the cn_flags field have the following meanings:
1267 * - MAKEENTRY: If clear, free an entry from the cache rather than look
1269 * - ISDOTDOT: Must be set if and only if cn_nameptr == ".."
1270 * - tsp: Return storage for cache timestamp. On a successful (positive
1271 * or negative) lookup, tsp will be filled with any timespec that
1272 * was stored when this cache entry was created. However, it will
1273 * be clear for "." entries.
1274 * - ticks: Return storage for alternate cache timestamp. On a successful
1275 * (positive or negative) lookup, it will contain the ticks value
1276 * that was current when the cache entry was created, unless cnp
1281 * - -1: A positive cache hit. vpp will contain the desired vnode.
1282 * - ENOENT: A negative cache hit, or dvp was recycled out from under us due
1283 * to a forced unmount. vpp will not be modified. If the entry
1284 * is a whiteout, then the ISWHITEOUT flag will be set in
1286 * - 0: A cache miss. vpp will not be modified.
1290 * On a cache hit, vpp will be returned locked and ref'd. If we're looking up
1291 * .., dvp is unlocked. If we're looking up . an extra ref is taken, but the
1292 * lock is not recursively acquired.
1295 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1296 struct timespec *tsp, int *ticksp)
1298 struct namecache_ts *ncp_ts;
1299 struct namecache *ncp;
1306 if (__predict_false(!doingcache)) {
1307 cnp->cn_flags &= ~MAKEENTRY;
1311 counter_u64_add(numcalls, 1);
1313 if (__predict_false(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.'))
1314 return (cache_lookup_dot(dvp, vpp, cnp, tsp, ticksp));
1316 if ((cnp->cn_flags & MAKEENTRY) == 0)
1317 return (cache_lookup_nomakeentry(dvp, vpp, cnp, tsp, ticksp));
1323 if (cnp->cn_namelen == 2 &&
1324 cnp->cn_nameptr[0] == '.' && cnp->cn_nameptr[1] == '.') {
1325 counter_u64_add(dotdothits, 1);
1326 dvlp = VP2VNODELOCK(dvp);
1328 ncp = dvp->v_cache_dd;
1330 SDT_PROBE3(vfs, namecache, lookup, miss, dvp,
1335 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1336 if (ncp->nc_flag & NCF_NEGATIVE)
1342 /* Return failure if negative entry was found. */
1344 goto negative_success;
1345 CTR3(KTR_VFS, "cache_lookup(%p, %s) found %p via ..",
1346 dvp, cnp->cn_nameptr, *vpp);
1347 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, "..",
1349 cache_out_ts(ncp, tsp, ticksp);
1350 if ((ncp->nc_flag & (NCF_ISDOTDOT | NCF_DTS)) ==
1351 NCF_DTS && tsp != NULL) {
1352 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1353 *tsp = ncp_ts->nc_dotdottime;
1358 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1359 blp = HASH2BUCKETLOCK(hash);
1362 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1363 counter_u64_add(numchecks, 1);
1364 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1365 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1369 /* We failed to find an entry */
1370 if (__predict_false(ncp == NULL)) {
1372 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
1374 counter_u64_add(nummiss, 1);
1378 if (ncp->nc_flag & NCF_NEGATIVE)
1379 goto negative_success;
1381 /* We found a "positive" match, return the vnode */
1382 counter_u64_add(numposhits, 1);
1384 CTR4(KTR_VFS, "cache_lookup(%p, %s) found %p via ncp %p",
1385 dvp, cnp->cn_nameptr, *vpp, ncp);
1386 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name,
1388 cache_out_ts(ncp, tsp, ticksp);
1391 * On success we return a locked and ref'd vnode as per the lookup
1395 ltype = 0; /* silence gcc warning */
1396 if (cnp->cn_flags & ISDOTDOT) {
1397 ltype = VOP_ISLOCKED(dvp);
1400 vs = vget_prep(*vpp);
1401 cache_lookup_unlock(blp, dvlp);
1402 error = vget_finish(*vpp, cnp->cn_lkflags, vs);
1403 if (cnp->cn_flags & ISDOTDOT) {
1404 vn_lock(dvp, ltype | LK_RETRY);
1405 if (dvp->v_iflag & VI_DOOMED) {
1416 if ((cnp->cn_flags & ISLASTCN) &&
1417 (cnp->cn_lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE) {
1418 ASSERT_VOP_ELOCKED(*vpp, "cache_lookup");
1423 /* We found a negative match, and want to create it, so purge */
1424 if (cnp->cn_nameiop == CREATE) {
1425 counter_u64_add(numnegzaps, 1);
1429 counter_u64_add(numneghits, 1);
1430 cache_negative_hit(ncp);
1431 if (ncp->nc_flag & NCF_WHITE)
1432 cnp->cn_flags |= ISWHITEOUT;
1433 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp,
1435 cache_out_ts(ncp, tsp, ticksp);
1436 cache_lookup_unlock(blp, dvlp);
1441 error = cache_zap_rlocked_bucket(ncp, cnp, hash, blp);
1443 error = cache_zap_locked_vnode(ncp, dvp);
1444 if (__predict_false(error != 0)) {
1445 zap_and_exit_bucket_fail2++;
1446 cache_maybe_yield();
1453 struct celockstate {
1455 struct rwlock *blp[2];
1457 CTASSERT((nitems(((struct celockstate *)0)->vlp) == 3));
1458 CTASSERT((nitems(((struct celockstate *)0)->blp) == 2));
1461 cache_celockstate_init(struct celockstate *cel)
1464 bzero(cel, sizeof(*cel));
1468 cache_lock_vnodes_cel(struct celockstate *cel, struct vnode *vp,
1471 struct mtx *vlp1, *vlp2;
1473 MPASS(cel->vlp[0] == NULL);
1474 MPASS(cel->vlp[1] == NULL);
1475 MPASS(cel->vlp[2] == NULL);
1477 MPASS(vp != NULL || dvp != NULL);
1479 vlp1 = VP2VNODELOCK(vp);
1480 vlp2 = VP2VNODELOCK(dvp);
1481 cache_sort_vnodes(&vlp1, &vlp2);
1492 cache_unlock_vnodes_cel(struct celockstate *cel)
1495 MPASS(cel->vlp[0] != NULL || cel->vlp[1] != NULL);
1497 if (cel->vlp[0] != NULL)
1498 mtx_unlock(cel->vlp[0]);
1499 if (cel->vlp[1] != NULL)
1500 mtx_unlock(cel->vlp[1]);
1501 if (cel->vlp[2] != NULL)
1502 mtx_unlock(cel->vlp[2]);
1506 cache_lock_vnodes_cel_3(struct celockstate *cel, struct vnode *vp)
1511 cache_assert_vlp_locked(cel->vlp[0]);
1512 cache_assert_vlp_locked(cel->vlp[1]);
1513 MPASS(cel->vlp[2] == NULL);
1516 vlp = VP2VNODELOCK(vp);
1519 if (vlp >= cel->vlp[1]) {
1522 if (mtx_trylock(vlp))
1524 cache_lock_vnodes_cel_3_failures++;
1525 cache_unlock_vnodes_cel(cel);
1526 if (vlp < cel->vlp[0]) {
1528 mtx_lock(cel->vlp[0]);
1529 mtx_lock(cel->vlp[1]);
1531 if (cel->vlp[0] != NULL)
1532 mtx_lock(cel->vlp[0]);
1534 mtx_lock(cel->vlp[1]);
1544 cache_lock_buckets_cel(struct celockstate *cel, struct rwlock *blp1,
1545 struct rwlock *blp2)
1548 MPASS(cel->blp[0] == NULL);
1549 MPASS(cel->blp[1] == NULL);
1551 cache_sort_vnodes(&blp1, &blp2);
1562 cache_unlock_buckets_cel(struct celockstate *cel)
1565 if (cel->blp[0] != NULL)
1566 rw_wunlock(cel->blp[0]);
1567 rw_wunlock(cel->blp[1]);
1571 * Lock part of the cache affected by the insertion.
1573 * This means vnodelocks for dvp, vp and the relevant bucketlock.
1574 * However, insertion can result in removal of an old entry. In this
1575 * case we have an additional vnode and bucketlock pair to lock. If the
1576 * entry is negative, ncelock is locked instead of the vnode.
1578 * That is, in the worst case we have to lock 3 vnodes and 2 bucketlocks, while
1579 * preserving the locking order (smaller address first).
1582 cache_enter_lock(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1585 struct namecache *ncp;
1586 struct rwlock *blps[2];
1588 blps[0] = HASH2BUCKETLOCK(hash);
1591 cache_lock_vnodes_cel(cel, dvp, vp);
1592 if (vp == NULL || vp->v_type != VDIR)
1594 ncp = vp->v_cache_dd;
1597 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1599 MPASS(ncp->nc_dvp == vp);
1600 blps[1] = NCP2BUCKETLOCK(ncp);
1601 if (ncp->nc_flag & NCF_NEGATIVE)
1603 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1606 * All vnodes got re-locked. Re-validate the state and if
1607 * nothing changed we are done. Otherwise restart.
1609 if (ncp == vp->v_cache_dd &&
1610 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1611 blps[1] == NCP2BUCKETLOCK(ncp) &&
1612 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1614 cache_unlock_vnodes_cel(cel);
1619 cache_lock_buckets_cel(cel, blps[0], blps[1]);
1623 cache_enter_lock_dd(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1626 struct namecache *ncp;
1627 struct rwlock *blps[2];
1629 blps[0] = HASH2BUCKETLOCK(hash);
1632 cache_lock_vnodes_cel(cel, dvp, vp);
1633 ncp = dvp->v_cache_dd;
1636 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1638 MPASS(ncp->nc_dvp == dvp);
1639 blps[1] = NCP2BUCKETLOCK(ncp);
1640 if (ncp->nc_flag & NCF_NEGATIVE)
1642 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1644 if (ncp == dvp->v_cache_dd &&
1645 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1646 blps[1] == NCP2BUCKETLOCK(ncp) &&
1647 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1649 cache_unlock_vnodes_cel(cel);
1654 cache_lock_buckets_cel(cel, blps[0], blps[1]);
1658 cache_enter_unlock(struct celockstate *cel)
1661 cache_unlock_buckets_cel(cel);
1662 cache_unlock_vnodes_cel(cel);
1666 * Add an entry to the cache.
1669 cache_enter_time(struct vnode *dvp, struct vnode *vp, struct componentname *cnp,
1670 struct timespec *tsp, struct timespec *dtsp)
1672 struct celockstate cel;
1673 struct namecache *ncp, *n2, *ndd;
1674 struct namecache_ts *ncp_ts, *n2_ts;
1675 struct nchashhead *ncpp;
1676 struct neglist *neglist;
1680 bool neg_locked, held_dvp;
1683 CTR3(KTR_VFS, "cache_enter(%p, %p, %s)", dvp, vp, cnp->cn_nameptr);
1684 VNASSERT(vp == NULL || (vp->v_iflag & VI_DOOMED) == 0, vp,
1685 ("cache_enter: Adding a doomed vnode"));
1686 VNASSERT(dvp == NULL || (dvp->v_iflag & VI_DOOMED) == 0, dvp,
1687 ("cache_enter: Doomed vnode used as src"));
1689 if (__predict_false(!doingcache))
1693 * Avoid blowout in namecache entries.
1695 lnumcache = atomic_fetchadd_long(&numcache, 1) + 1;
1696 if (__predict_false(lnumcache >= ncsize)) {
1697 atomic_add_long(&numcache, -1);
1701 cache_celockstate_init(&cel);
1705 if (cnp->cn_nameptr[0] == '.') {
1706 if (cnp->cn_namelen == 1)
1708 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
1709 len = cnp->cn_namelen;
1710 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1711 cache_enter_lock_dd(&cel, dvp, vp, hash);
1713 * If dotdot entry already exists, just retarget it
1714 * to new parent vnode, otherwise continue with new
1715 * namecache entry allocation.
1717 if ((ncp = dvp->v_cache_dd) != NULL &&
1718 ncp->nc_flag & NCF_ISDOTDOT) {
1719 KASSERT(ncp->nc_dvp == dvp,
1720 ("wrong isdotdot parent"));
1722 if (ncp->nc_flag & NCF_NEGATIVE || vp == NULL) {
1723 neglist = NCP2NEGLIST(ncp);
1724 mtx_lock(&ncneg_hot.nl_lock);
1725 mtx_lock(&neglist->nl_lock);
1728 if (!(ncp->nc_flag & NCF_NEGATIVE)) {
1729 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst,
1732 cache_negative_remove(ncp, true);
1735 TAILQ_INSERT_HEAD(&vp->v_cache_dst,
1737 if (ncp->nc_flag & NCF_HOTNEGATIVE)
1739 ncp->nc_flag &= ~(NCF_NEGATIVE|NCF_HOTNEGATIVE);
1741 if (ncp->nc_flag & NCF_HOTNEGATIVE) {
1743 ncp->nc_flag &= ~(NCF_HOTNEGATIVE);
1745 ncp->nc_flag |= NCF_NEGATIVE;
1746 cache_negative_insert(ncp, true);
1749 mtx_unlock(&neglist->nl_lock);
1750 mtx_unlock(&ncneg_hot.nl_lock);
1753 cache_enter_unlock(&cel);
1756 dvp->v_cache_dd = NULL;
1757 cache_enter_unlock(&cel);
1758 cache_celockstate_init(&cel);
1759 SDT_PROBE3(vfs, namecache, enter, done, dvp, "..", vp);
1760 flag = NCF_ISDOTDOT;
1765 if (LIST_EMPTY(&dvp->v_cache_src) && flag != NCF_ISDOTDOT) {
1767 atomic_add_long(&numcachehv, 1);
1772 * Calculate the hash key and setup as much of the new
1773 * namecache entry as possible before acquiring the lock.
1775 ncp = cache_alloc(cnp->cn_namelen, tsp != NULL);
1776 ncp->nc_flag = flag;
1779 ncp->nc_flag |= NCF_NEGATIVE;
1782 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1783 ncp_ts->nc_time = *tsp;
1784 ncp_ts->nc_ticks = ticks;
1785 ncp_ts->nc_nc.nc_flag |= NCF_TS;
1787 ncp_ts->nc_dotdottime = *dtsp;
1788 ncp_ts->nc_nc.nc_flag |= NCF_DTS;
1791 len = ncp->nc_nlen = cnp->cn_namelen;
1792 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1793 strlcpy(ncp->nc_name, cnp->cn_nameptr, len + 1);
1794 cache_enter_lock(&cel, dvp, vp, hash);
1797 * See if this vnode or negative entry is already in the cache
1798 * with this name. This can happen with concurrent lookups of
1799 * the same path name.
1801 ncpp = NCHHASH(hash);
1802 LIST_FOREACH(n2, ncpp, nc_hash) {
1803 if (n2->nc_dvp == dvp &&
1804 n2->nc_nlen == cnp->cn_namelen &&
1805 !bcmp(n2->nc_name, cnp->cn_nameptr, n2->nc_nlen)) {
1807 KASSERT((n2->nc_flag & NCF_TS) != 0,
1809 n2_ts = __containerof(n2, struct namecache_ts, nc_nc);
1810 n2_ts->nc_time = ncp_ts->nc_time;
1811 n2_ts->nc_ticks = ncp_ts->nc_ticks;
1813 n2_ts->nc_dotdottime = ncp_ts->nc_dotdottime;
1814 if (ncp->nc_flag & NCF_NEGATIVE)
1815 mtx_lock(&ncneg_hot.nl_lock);
1816 n2_ts->nc_nc.nc_flag |= NCF_DTS;
1817 if (ncp->nc_flag & NCF_NEGATIVE)
1818 mtx_unlock(&ncneg_hot.nl_lock);
1821 goto out_unlock_free;
1825 if (flag == NCF_ISDOTDOT) {
1827 * See if we are trying to add .. entry, but some other lookup
1828 * has populated v_cache_dd pointer already.
1830 if (dvp->v_cache_dd != NULL)
1831 goto out_unlock_free;
1832 KASSERT(vp == NULL || vp->v_type == VDIR,
1833 ("wrong vnode type %p", vp));
1834 dvp->v_cache_dd = ncp;
1838 if (vp->v_type == VDIR) {
1839 if (flag != NCF_ISDOTDOT) {
1841 * For this case, the cache entry maps both the
1842 * directory name in it and the name ".." for the
1843 * directory's parent.
1845 if ((ndd = vp->v_cache_dd) != NULL) {
1846 if ((ndd->nc_flag & NCF_ISDOTDOT) != 0)
1847 cache_zap_locked(ndd, false);
1851 vp->v_cache_dd = ncp;
1854 vp->v_cache_dd = NULL;
1858 if (flag != NCF_ISDOTDOT) {
1859 if (LIST_EMPTY(&dvp->v_cache_src)) {
1862 atomic_add_long(&numcachehv, 1);
1867 * This will not take the interlock as someone
1868 * else already holds the vnode on account of
1869 * the namecache and we hold locks preventing
1870 * this from changing.
1873 atomic_subtract_long(&numcachehv, 1);
1876 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
1880 * Insert the new namecache entry into the appropriate chain
1881 * within the cache entries table.
1883 LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
1886 * If the entry is "negative", we place it into the
1887 * "negative" cache queue, otherwise, we place it into the
1888 * destination vnode's cache entries queue.
1891 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
1892 SDT_PROBE3(vfs, namecache, enter, done, dvp, ncp->nc_name,
1895 if (cnp->cn_flags & ISWHITEOUT)
1896 ncp->nc_flag |= NCF_WHITE;
1897 cache_negative_insert(ncp, false);
1898 SDT_PROBE2(vfs, namecache, enter_negative, done, dvp,
1901 cache_enter_unlock(&cel);
1902 if (numneg * ncnegfactor > lnumcache)
1903 cache_negative_zap_one();
1907 cache_enter_unlock(&cel);
1911 atomic_subtract_long(&numcachehv, 1);
1917 cache_roundup_2(u_int val)
1921 for (res = 1; res <= val; res <<= 1)
1928 * Name cache initialization, from vfs_init() when we are booting
1931 nchinit(void *dummy __unused)
1935 cache_zone_small = uma_zcreate("S VFS Cache",
1936 sizeof(struct namecache) + CACHE_PATH_CUTOFF + 1,
1937 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache),
1939 cache_zone_small_ts = uma_zcreate("STS VFS Cache",
1940 sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1,
1941 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache_ts),
1943 cache_zone_large = uma_zcreate("L VFS Cache",
1944 sizeof(struct namecache) + NAME_MAX + 1,
1945 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache),
1947 cache_zone_large_ts = uma_zcreate("LTS VFS Cache",
1948 sizeof(struct namecache_ts) + NAME_MAX + 1,
1949 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache_ts),
1952 ncsize = desiredvnodes * ncsizefactor;
1953 nchashtbl = hashinit(desiredvnodes * 2, M_VFSCACHE, &nchash);
1954 ncbuckethash = cache_roundup_2(mp_ncpus * mp_ncpus) - 1;
1955 if (ncbuckethash < 7) /* arbitrarily chosen to avoid having one lock */
1957 if (ncbuckethash > nchash)
1958 ncbuckethash = nchash;
1959 bucketlocks = malloc(sizeof(*bucketlocks) * numbucketlocks, M_VFSCACHE,
1961 for (i = 0; i < numbucketlocks; i++)
1962 rw_init_flags(&bucketlocks[i], "ncbuc", RW_DUPOK | RW_RECURSE);
1963 ncvnodehash = ncbuckethash;
1964 vnodelocks = malloc(sizeof(*vnodelocks) * numvnodelocks, M_VFSCACHE,
1966 for (i = 0; i < numvnodelocks; i++)
1967 mtx_init(&vnodelocks[i], "ncvn", NULL, MTX_DUPOK | MTX_RECURSE);
1968 ncpurgeminvnodes = numbucketlocks * 2;
1971 neglists = malloc(sizeof(*neglists) * numneglists, M_VFSCACHE,
1973 for (i = 0; i < numneglists; i++) {
1974 mtx_init(&neglists[i].nl_lock, "ncnegl", NULL, MTX_DEF);
1975 TAILQ_INIT(&neglists[i].nl_list);
1977 mtx_init(&ncneg_hot.nl_lock, "ncneglh", NULL, MTX_DEF);
1978 TAILQ_INIT(&ncneg_hot.nl_list);
1980 mtx_init(&ncneg_shrink_lock, "ncnegs", NULL, MTX_DEF);
1982 numcalls = counter_u64_alloc(M_WAITOK);
1983 dothits = counter_u64_alloc(M_WAITOK);
1984 dotdothits = counter_u64_alloc(M_WAITOK);
1985 numchecks = counter_u64_alloc(M_WAITOK);
1986 nummiss = counter_u64_alloc(M_WAITOK);
1987 nummisszap = counter_u64_alloc(M_WAITOK);
1988 numposzaps = counter_u64_alloc(M_WAITOK);
1989 numposhits = counter_u64_alloc(M_WAITOK);
1990 numnegzaps = counter_u64_alloc(M_WAITOK);
1991 numneghits = counter_u64_alloc(M_WAITOK);
1992 numfullpathcalls = counter_u64_alloc(M_WAITOK);
1993 numfullpathfail1 = counter_u64_alloc(M_WAITOK);
1994 numfullpathfail2 = counter_u64_alloc(M_WAITOK);
1995 numfullpathfail4 = counter_u64_alloc(M_WAITOK);
1996 numfullpathfound = counter_u64_alloc(M_WAITOK);
1997 zap_and_exit_bucket_relock_success = counter_u64_alloc(M_WAITOK);
1998 numneg_evicted = counter_u64_alloc(M_WAITOK);
1999 shrinking_skipped = counter_u64_alloc(M_WAITOK);
2001 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL);
2004 cache_changesize(int newmaxvnodes)
2006 struct nchashhead *new_nchashtbl, *old_nchashtbl;
2007 u_long new_nchash, old_nchash;
2008 struct namecache *ncp;
2013 newncsize = newmaxvnodes * ncsizefactor;
2014 newmaxvnodes = cache_roundup_2(newmaxvnodes * 2);
2015 if (newmaxvnodes < numbucketlocks)
2016 newmaxvnodes = numbucketlocks;
2018 new_nchashtbl = hashinit(newmaxvnodes, M_VFSCACHE, &new_nchash);
2019 /* If same hash table size, nothing to do */
2020 if (nchash == new_nchash) {
2021 free(new_nchashtbl, M_VFSCACHE);
2025 * Move everything from the old hash table to the new table.
2026 * None of the namecache entries in the table can be removed
2027 * because to do so, they have to be removed from the hash table.
2029 cache_lock_all_vnodes();
2030 cache_lock_all_buckets();
2031 old_nchashtbl = nchashtbl;
2032 old_nchash = nchash;
2033 nchashtbl = new_nchashtbl;
2034 nchash = new_nchash;
2035 for (i = 0; i <= old_nchash; i++) {
2036 while ((ncp = LIST_FIRST(&old_nchashtbl[i])) != NULL) {
2037 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen,
2039 LIST_REMOVE(ncp, nc_hash);
2040 LIST_INSERT_HEAD(NCHHASH(hash), ncp, nc_hash);
2044 cache_unlock_all_buckets();
2045 cache_unlock_all_vnodes();
2046 free(old_nchashtbl, M_VFSCACHE);
2050 * Invalidate all entries from and to a particular vnode.
2053 cache_purge(struct vnode *vp)
2055 TAILQ_HEAD(, namecache) ncps;
2056 struct namecache *ncp, *nnp;
2057 struct mtx *vlp, *vlp2;
2059 CTR1(KTR_VFS, "cache_purge(%p)", vp);
2060 SDT_PROBE1(vfs, namecache, purge, done, vp);
2061 if (LIST_EMPTY(&vp->v_cache_src) && TAILQ_EMPTY(&vp->v_cache_dst) &&
2062 vp->v_cache_dd == NULL)
2065 vlp = VP2VNODELOCK(vp);
2069 while (!LIST_EMPTY(&vp->v_cache_src)) {
2070 ncp = LIST_FIRST(&vp->v_cache_src);
2071 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2073 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2075 while (!TAILQ_EMPTY(&vp->v_cache_dst)) {
2076 ncp = TAILQ_FIRST(&vp->v_cache_dst);
2077 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2079 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2081 ncp = vp->v_cache_dd;
2083 KASSERT(ncp->nc_flag & NCF_ISDOTDOT,
2084 ("lost dotdot link"));
2085 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2087 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2089 KASSERT(vp->v_cache_dd == NULL, ("incomplete purge"));
2093 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2099 * Invalidate all negative entries for a particular directory vnode.
2102 cache_purge_negative(struct vnode *vp)
2104 TAILQ_HEAD(, namecache) ncps;
2105 struct namecache *ncp, *nnp;
2108 CTR1(KTR_VFS, "cache_purge_negative(%p)", vp);
2109 SDT_PROBE1(vfs, namecache, purge_negative, done, vp);
2110 if (LIST_EMPTY(&vp->v_cache_src))
2113 vlp = VP2VNODELOCK(vp);
2115 LIST_FOREACH_SAFE(ncp, &vp->v_cache_src, nc_src, nnp) {
2116 if (!(ncp->nc_flag & NCF_NEGATIVE))
2118 cache_zap_negative_locked_vnode_kl(ncp, vp);
2119 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2122 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2128 * Flush all entries referencing a particular filesystem.
2131 cache_purgevfs(struct mount *mp, bool force)
2133 TAILQ_HEAD(, namecache) ncps;
2134 struct mtx *vlp1, *vlp2;
2136 struct nchashhead *bucket;
2137 struct namecache *ncp, *nnp;
2138 u_long i, j, n_nchash;
2141 /* Scan hash tables for applicable entries */
2142 SDT_PROBE1(vfs, namecache, purgevfs, done, mp);
2143 if (!force && mp->mnt_nvnodelistsize <= ncpurgeminvnodes)
2146 n_nchash = nchash + 1;
2148 for (i = 0; i < numbucketlocks; i++) {
2149 blp = (struct rwlock *)&bucketlocks[i];
2151 for (j = i; j < n_nchash; j += numbucketlocks) {
2153 bucket = &nchashtbl[j];
2154 LIST_FOREACH_SAFE(ncp, bucket, nc_hash, nnp) {
2155 cache_assert_bucket_locked(ncp, RA_WLOCKED);
2156 if (ncp->nc_dvp->v_mount != mp)
2158 error = cache_zap_wlocked_bucket_kl(ncp, blp,
2162 TAILQ_INSERT_HEAD(&ncps, ncp, nc_dst);
2166 if (vlp1 == NULL && vlp2 == NULL)
2167 cache_maybe_yield();
2174 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2180 * Perform canonical checks and cache lookup and pass on to filesystem
2181 * through the vop_cachedlookup only if needed.
2185 vfs_cache_lookup(struct vop_lookup_args *ap)
2189 struct vnode **vpp = ap->a_vpp;
2190 struct componentname *cnp = ap->a_cnp;
2191 struct ucred *cred = cnp->cn_cred;
2192 int flags = cnp->cn_flags;
2193 struct thread *td = cnp->cn_thread;
2198 if (dvp->v_type != VDIR)
2201 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
2202 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
2205 error = VOP_ACCESS(dvp, VEXEC, cred, td);
2209 error = cache_lookup(dvp, vpp, cnp, NULL, NULL);
2211 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
2218 * XXX All of these sysctls would probably be more productive dead.
2220 static int __read_mostly disablecwd;
2221 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0,
2222 "Disable the getcwd syscall");
2224 /* Implementation of the getcwd syscall. */
2226 sys___getcwd(struct thread *td, struct __getcwd_args *uap)
2229 return (kern___getcwd(td, uap->buf, UIO_USERSPACE, uap->buflen,
2234 kern___getcwd(struct thread *td, char *buf, enum uio_seg bufseg, size_t buflen,
2238 struct filedesc *fdp;
2239 struct vnode *cdir, *rdir;
2242 if (__predict_false(disablecwd))
2244 if (__predict_false(buflen < 2))
2246 if (buflen > path_max)
2249 tmpbuf = malloc(buflen, M_TEMP, M_WAITOK);
2250 fdp = td->td_proc->p_fd;
2251 FILEDESC_SLOCK(fdp);
2252 cdir = fdp->fd_cdir;
2254 rdir = fdp->fd_rdir;
2256 FILEDESC_SUNLOCK(fdp);
2257 error = vn_fullpath1(td, cdir, rdir, tmpbuf, &bp, buflen);
2262 if (bufseg == UIO_SYSSPACE)
2263 bcopy(bp, buf, strlen(bp) + 1);
2265 error = copyout(bp, buf, strlen(bp) + 1);
2267 if (KTRPOINT(curthread, KTR_NAMEI))
2271 free(tmpbuf, M_TEMP);
2276 * Thus begins the fullpath magic.
2279 static int __read_mostly disablefullpath;
2280 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW, &disablefullpath, 0,
2281 "Disable the vn_fullpath function");
2284 * Retrieve the full filesystem path that correspond to a vnode from the name
2285 * cache (if available)
2288 vn_fullpath(struct thread *td, struct vnode *vn, char **retbuf, char **freebuf)
2291 struct filedesc *fdp;
2295 if (__predict_false(disablefullpath))
2297 if (__predict_false(vn == NULL))
2300 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
2301 fdp = td->td_proc->p_fd;
2302 FILEDESC_SLOCK(fdp);
2303 rdir = fdp->fd_rdir;
2305 FILEDESC_SUNLOCK(fdp);
2306 error = vn_fullpath1(td, vn, rdir, buf, retbuf, MAXPATHLEN);
2317 * This function is similar to vn_fullpath, but it attempts to lookup the
2318 * pathname relative to the global root mount point. This is required for the
2319 * auditing sub-system, as audited pathnames must be absolute, relative to the
2320 * global root mount point.
2323 vn_fullpath_global(struct thread *td, struct vnode *vn,
2324 char **retbuf, char **freebuf)
2329 if (__predict_false(disablefullpath))
2331 if (__predict_false(vn == NULL))
2333 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
2334 error = vn_fullpath1(td, vn, rootvnode, buf, retbuf, MAXPATHLEN);
2343 vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, u_int *buflen)
2346 struct namecache *ncp;
2350 vlp = VP2VNODELOCK(*vp);
2352 TAILQ_FOREACH(ncp, &((*vp)->v_cache_dst), nc_dst) {
2353 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2357 if (*buflen < ncp->nc_nlen) {
2360 counter_u64_add(numfullpathfail4, 1);
2362 SDT_PROBE3(vfs, namecache, fullpath, return, error,
2366 *buflen -= ncp->nc_nlen;
2367 memcpy(buf + *buflen, ncp->nc_name, ncp->nc_nlen);
2368 SDT_PROBE3(vfs, namecache, fullpath, hit, ncp->nc_dvp,
2377 SDT_PROBE1(vfs, namecache, fullpath, miss, vp);
2380 vn_lock(*vp, LK_SHARED | LK_RETRY);
2381 error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen);
2384 counter_u64_add(numfullpathfail2, 1);
2385 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2390 if (dvp->v_iflag & VI_DOOMED) {
2391 /* forced unmount */
2394 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2398 * *vp has its use count incremented still.
2405 * The magic behind kern___getcwd() and vn_fullpath().
2408 vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
2409 char *buf, char **retbuf, u_int buflen)
2411 int error, slash_prefixed;
2412 #ifdef KDTRACE_HOOKS
2413 struct vnode *startvp = vp;
2422 SDT_PROBE1(vfs, namecache, fullpath, entry, vp);
2423 counter_u64_add(numfullpathcalls, 1);
2425 if (vp->v_type != VDIR) {
2426 error = vn_vptocnp(&vp, td->td_ucred, buf, &buflen);
2433 buf[--buflen] = '/';
2436 while (vp != rdir && vp != rootvnode) {
2438 * The vp vnode must be already fully constructed,
2439 * since it is either found in namecache or obtained
2440 * from VOP_VPTOCNP(). We may test for VV_ROOT safely
2441 * without obtaining the vnode lock.
2443 if ((vp->v_vflag & VV_ROOT) != 0) {
2444 vn_lock(vp, LK_RETRY | LK_SHARED);
2447 * With the vnode locked, check for races with
2448 * unmount, forced or not. Note that we
2449 * already verified that vp is not equal to
2450 * the root vnode, which means that
2451 * mnt_vnodecovered can be NULL only for the
2454 if ((vp->v_iflag & VI_DOOMED) != 0 ||
2455 (vp1 = vp->v_mount->mnt_vnodecovered) == NULL ||
2456 vp1->v_mountedhere != vp->v_mount) {
2459 SDT_PROBE3(vfs, namecache, fullpath, return,
2469 if (vp->v_type != VDIR) {
2471 counter_u64_add(numfullpathfail1, 1);
2473 SDT_PROBE3(vfs, namecache, fullpath, return,
2477 error = vn_vptocnp(&vp, td->td_ucred, buf, &buflen);
2483 SDT_PROBE3(vfs, namecache, fullpath, return, error,
2487 buf[--buflen] = '/';
2492 if (!slash_prefixed) {
2495 counter_u64_add(numfullpathfail4, 1);
2496 SDT_PROBE3(vfs, namecache, fullpath, return, ENOMEM,
2500 buf[--buflen] = '/';
2502 counter_u64_add(numfullpathfound, 1);
2505 SDT_PROBE3(vfs, namecache, fullpath, return, 0, startvp, buf + buflen);
2506 *retbuf = buf + buflen;
2511 vn_dir_dd_ino(struct vnode *vp)
2513 struct namecache *ncp;
2518 ASSERT_VOP_LOCKED(vp, "vn_dir_dd_ino");
2519 vlp = VP2VNODELOCK(vp);
2521 TAILQ_FOREACH(ncp, &(vp->v_cache_dst), nc_dst) {
2522 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0)
2525 vs = vget_prep(ddvp);
2527 if (vget_finish(ddvp, LK_SHARED | LK_NOWAIT, vs))
2536 vn_commname(struct vnode *vp, char *buf, u_int buflen)
2538 struct namecache *ncp;
2542 vlp = VP2VNODELOCK(vp);
2544 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst)
2545 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2551 l = min(ncp->nc_nlen, buflen - 1);
2552 memcpy(buf, ncp->nc_name, l);
2559 * This function updates path string to vnode's full global path
2560 * and checks the size of the new path string against the pathlen argument.
2562 * Requires a locked, referenced vnode.
2563 * Vnode is re-locked on success or ENODEV, otherwise unlocked.
2565 * If sysctl debug.disablefullpath is set, ENODEV is returned,
2566 * vnode is left locked and path remain untouched.
2568 * If vp is a directory, the call to vn_fullpath_global() always succeeds
2569 * because it falls back to the ".." lookup if the namecache lookup fails.
2572 vn_path_to_global_path(struct thread *td, struct vnode *vp, char *path,
2575 struct nameidata nd;
2580 ASSERT_VOP_ELOCKED(vp, __func__);
2582 /* Return ENODEV if sysctl debug.disablefullpath==1 */
2583 if (__predict_false(disablefullpath))
2586 /* Construct global filesystem path from vp. */
2588 error = vn_fullpath_global(td, vp, &rpath, &fbuf);
2595 if (strlen(rpath) >= pathlen) {
2597 error = ENAMETOOLONG;
2602 * Re-lookup the vnode by path to detect a possible rename.
2603 * As a side effect, the vnode is relocked.
2604 * If vnode was renamed, return ENOENT.
2606 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
2607 UIO_SYSSPACE, path, td);
2613 NDFREE(&nd, NDF_ONLY_PNBUF);
2617 strcpy(path, rpath);
2630 db_print_vpath(struct vnode *vp)
2633 while (vp != NULL) {
2634 db_printf("%p: ", vp);
2635 if (vp == rootvnode) {
2639 if (vp->v_vflag & VV_ROOT) {
2640 db_printf("<mount point>");
2641 vp = vp->v_mount->mnt_vnodecovered;
2643 struct namecache *ncp;
2647 ncp = TAILQ_FIRST(&vp->v_cache_dst);
2650 for (i = 0; i < ncp->nc_nlen; i++)
2651 db_printf("%c", *ncn++);
2664 DB_SHOW_COMMAND(vpath, db_show_vpath)
2669 db_printf("usage: show vpath <struct vnode *>\n");
2673 vp = (struct vnode *)addr;