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>
50 #include <sys/malloc.h>
51 #include <sys/fcntl.h>
52 #include <sys/mount.h>
53 #include <sys/namei.h>
55 #include <sys/rwlock.h>
58 #include <sys/syscallsubr.h>
59 #include <sys/sysctl.h>
60 #include <sys/sysproto.h>
61 #include <sys/vnode.h>
63 #include <sys/ktrace.h>
72 SDT_PROVIDER_DECLARE(vfs);
73 SDT_PROBE_DEFINE3(vfs, namecache, enter, done, "struct vnode *", "char *",
75 SDT_PROBE_DEFINE2(vfs, namecache, enter_negative, done, "struct vnode *",
77 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, entry, "struct vnode *");
78 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, hit, "struct vnode *",
79 "char *", "struct vnode *");
80 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, miss, "struct vnode *");
81 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, return, "int",
82 "struct vnode *", "char *");
83 SDT_PROBE_DEFINE3(vfs, namecache, lookup, hit, "struct vnode *", "char *",
85 SDT_PROBE_DEFINE2(vfs, namecache, lookup, hit__negative,
86 "struct vnode *", "char *");
87 SDT_PROBE_DEFINE2(vfs, namecache, lookup, miss, "struct vnode *",
89 SDT_PROBE_DEFINE1(vfs, namecache, purge, done, "struct vnode *");
90 SDT_PROBE_DEFINE1(vfs, namecache, purge_negative, done, "struct vnode *");
91 SDT_PROBE_DEFINE1(vfs, namecache, purgevfs, done, "struct mount *");
92 SDT_PROBE_DEFINE3(vfs, namecache, zap, done, "struct vnode *", "char *",
94 SDT_PROBE_DEFINE3(vfs, namecache, zap_negative, done, "struct vnode *",
96 SDT_PROBE_DEFINE3(vfs, namecache, shrink_negative, done, "struct vnode *",
100 * This structure describes the elements in the cache of recent
101 * names looked up by namei.
105 LIST_ENTRY(namecache) nc_hash; /* hash chain */
106 LIST_ENTRY(namecache) nc_src; /* source vnode list */
107 TAILQ_ENTRY(namecache) nc_dst; /* destination vnode list */
108 struct vnode *nc_dvp; /* vnode of parent of name */
110 struct vnode *nu_vp; /* vnode the name refers to */
111 u_int nu_neghits; /* negative entry hits */
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
134 #define nc_neghits n_un.nu_neghits
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 = 12; /* 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 SYSCTL_ULONG(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0,
209 "Number of negative entries in namecache");
210 static u_long __exclusive_cache_line numcache;/* number of cache entries allocated */
211 SYSCTL_ULONG(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0,
212 "Number of namecache entries");
213 static u_long __exclusive_cache_line numcachehv;/* number of cache entries with vnodes held */
214 SYSCTL_ULONG(_debug, OID_AUTO, numcachehv, CTLFLAG_RD, &numcachehv, 0,
215 "Number of namecache entries with vnodes held");
216 u_int __read_mostly ncsizefactor = 2;
217 SYSCTL_UINT(_vfs, OID_AUTO, ncsizefactor, CTLFLAG_RW, &ncsizefactor, 0,
218 "Size factor for namecache");
219 static u_int __read_mostly ncpurgeminvnodes;
220 SYSCTL_UINT(_vfs, OID_AUTO, ncpurgeminvnodes, CTLFLAG_RW, &ncpurgeminvnodes, 0,
221 "Number of vnodes below which purgevfs ignores the request");
222 static u_int __read_mostly ncneghitsrequeue = 8;
223 SYSCTL_UINT(_vfs, OID_AUTO, ncneghitsrequeue, CTLFLAG_RW, &ncneghitsrequeue, 0,
224 "Number of hits to requeue a negative entry in the LRU list");
226 struct nchstats nchstats; /* cache effectiveness statistics */
228 static struct mtx ncneg_shrink_lock;
229 static int shrink_list_turn;
233 TAILQ_HEAD(, namecache) nl_list;
234 } __aligned(CACHE_LINE_SIZE);
236 static struct neglist __read_mostly *neglists;
237 static struct neglist ncneg_hot;
239 #define numneglists (ncneghash + 1)
240 static u_int __read_mostly ncneghash;
241 static inline struct neglist *
242 NCP2NEGLIST(struct namecache *ncp)
245 return (&neglists[(((uintptr_t)(ncp) >> 8) & ncneghash)]);
248 #define numbucketlocks (ncbuckethash + 1)
249 static u_int __read_mostly ncbuckethash;
250 static struct rwlock_padalign __read_mostly *bucketlocks;
251 #define HASH2BUCKETLOCK(hash) \
252 ((struct rwlock *)(&bucketlocks[((hash) & ncbuckethash)]))
254 #define numvnodelocks (ncvnodehash + 1)
255 static u_int __read_mostly ncvnodehash;
256 static struct mtx __read_mostly *vnodelocks;
257 static inline struct mtx *
258 VP2VNODELOCK(struct vnode *vp)
261 return (&vnodelocks[(((uintptr_t)(vp) >> 8) & ncvnodehash)]);
265 * UMA zones for the VFS cache.
267 * The small cache is used for entries with short names, which are the
268 * most common. The large cache is used for entries which are too big to
269 * fit in the small cache.
271 static uma_zone_t __read_mostly cache_zone_small;
272 static uma_zone_t __read_mostly cache_zone_small_ts;
273 static uma_zone_t __read_mostly cache_zone_large;
274 static uma_zone_t __read_mostly cache_zone_large_ts;
276 #define CACHE_PATH_CUTOFF 35
278 static struct namecache *
279 cache_alloc(int len, int ts)
281 struct namecache_ts *ncp_ts;
282 struct namecache *ncp;
284 if (__predict_false(ts)) {
285 if (len <= CACHE_PATH_CUTOFF)
286 ncp_ts = uma_zalloc(cache_zone_small_ts, M_WAITOK);
288 ncp_ts = uma_zalloc(cache_zone_large_ts, M_WAITOK);
289 ncp = &ncp_ts->nc_nc;
291 if (len <= CACHE_PATH_CUTOFF)
292 ncp = uma_zalloc(cache_zone_small, M_WAITOK);
294 ncp = uma_zalloc(cache_zone_large, M_WAITOK);
300 cache_free(struct namecache *ncp)
302 struct namecache_ts *ncp_ts;
306 if ((ncp->nc_flag & NCF_DVDROP) != 0)
308 if (__predict_false(ncp->nc_flag & NCF_TS)) {
309 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
310 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
311 uma_zfree(cache_zone_small_ts, ncp_ts);
313 uma_zfree(cache_zone_large_ts, ncp_ts);
315 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
316 uma_zfree(cache_zone_small, ncp);
318 uma_zfree(cache_zone_large, ncp);
323 cache_out_ts(struct namecache *ncp, struct timespec *tsp, int *ticksp)
325 struct namecache_ts *ncp_ts;
327 KASSERT((ncp->nc_flag & NCF_TS) != 0 ||
328 (tsp == NULL && ticksp == NULL),
331 if (tsp == NULL && ticksp == NULL)
334 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
336 *tsp = ncp_ts->nc_time;
338 *ticksp = ncp_ts->nc_ticks;
341 static int __read_mostly doingcache = 1; /* 1 => enable the cache */
342 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0,
343 "VFS namecache enabled");
345 /* Export size information to userland */
346 SYSCTL_INT(_debug_sizeof, OID_AUTO, namecache, CTLFLAG_RD, SYSCTL_NULL_INT_PTR,
347 sizeof(struct namecache), "sizeof(struct namecache)");
350 * The new name cache statistics
352 static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0,
353 "Name cache statistics");
354 #define STATNODE_ULONG(name, descr) \
355 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, descr);
356 #define STATNODE_COUNTER(name, descr) \
357 static counter_u64_t __read_mostly name; \
358 SYSCTL_COUNTER_U64(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, descr);
359 STATNODE_ULONG(numneg, "Number of negative cache entries");
360 STATNODE_ULONG(numcache, "Number of cache entries");
361 STATNODE_COUNTER(numcalls, "Number of cache lookups");
362 STATNODE_COUNTER(dothits, "Number of '.' hits");
363 STATNODE_COUNTER(dotdothits, "Number of '..' hits");
364 STATNODE_COUNTER(numchecks, "Number of checks in lookup");
365 STATNODE_COUNTER(nummiss, "Number of cache misses");
366 STATNODE_COUNTER(nummisszap, "Number of cache misses we do not want to cache");
367 STATNODE_COUNTER(numposzaps,
368 "Number of cache hits (positive) we do not want to cache");
369 STATNODE_COUNTER(numposhits, "Number of cache hits (positive)");
370 STATNODE_COUNTER(numnegzaps,
371 "Number of cache hits (negative) we do not want to cache");
372 STATNODE_COUNTER(numneghits, "Number of cache hits (negative)");
373 /* These count for kern___getcwd(), too. */
374 STATNODE_COUNTER(numfullpathcalls, "Number of fullpath search calls");
375 STATNODE_COUNTER(numfullpathfail1, "Number of fullpath search errors (ENOTDIR)");
376 STATNODE_COUNTER(numfullpathfail2,
377 "Number of fullpath search errors (VOP_VPTOCNP failures)");
378 STATNODE_COUNTER(numfullpathfail4, "Number of fullpath search errors (ENOMEM)");
379 STATNODE_COUNTER(numfullpathfound, "Number of successful fullpath calls");
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 cache_lock_vnodes_cel_3_failures;
383 STATNODE_ULONG(cache_lock_vnodes_cel_3_failures,
384 "Number of times 3-way vnode locking failed");
386 static void cache_zap_locked(struct namecache *ncp, bool neg_locked);
387 static int vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
388 char *buf, char **retbuf, u_int buflen);
390 static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
392 static int cache_yield;
393 SYSCTL_INT(_vfs_cache, OID_AUTO, yield, CTLFLAG_RD, &cache_yield, 0,
394 "Number of times cache called yield");
397 cache_maybe_yield(void)
400 if (should_yield()) {
402 kern_yield(PRI_USER);
407 cache_assert_vlp_locked(struct mtx *vlp)
411 mtx_assert(vlp, MA_OWNED);
415 cache_assert_vnode_locked(struct vnode *vp)
419 vlp = VP2VNODELOCK(vp);
420 cache_assert_vlp_locked(vlp);
424 cache_get_hash(char *name, u_char len, struct vnode *dvp)
428 hash = fnv_32_buf(name, len, FNV1_32_INIT);
429 hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
433 static inline struct rwlock *
434 NCP2BUCKETLOCK(struct namecache *ncp)
438 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen, ncp->nc_dvp);
439 return (HASH2BUCKETLOCK(hash));
444 cache_assert_bucket_locked(struct namecache *ncp, int mode)
448 blp = NCP2BUCKETLOCK(ncp);
449 rw_assert(blp, mode);
452 #define cache_assert_bucket_locked(x, y) do { } while (0)
455 #define cache_sort(x, y) _cache_sort((void **)(x), (void **)(y))
457 _cache_sort(void **p1, void **p2)
469 cache_lock_all_buckets(void)
473 for (i = 0; i < numbucketlocks; i++)
474 rw_wlock(&bucketlocks[i]);
478 cache_unlock_all_buckets(void)
482 for (i = 0; i < numbucketlocks; i++)
483 rw_wunlock(&bucketlocks[i]);
487 cache_lock_all_vnodes(void)
491 for (i = 0; i < numvnodelocks; i++)
492 mtx_lock(&vnodelocks[i]);
496 cache_unlock_all_vnodes(void)
500 for (i = 0; i < numvnodelocks; i++)
501 mtx_unlock(&vnodelocks[i]);
505 cache_trylock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
508 cache_sort(&vlp1, &vlp2);
512 if (!mtx_trylock(vlp1))
515 if (!mtx_trylock(vlp2)) {
525 cache_unlock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
528 MPASS(vlp1 != NULL || vlp2 != NULL);
537 sysctl_nchstats(SYSCTL_HANDLER_ARGS)
539 struct nchstats snap;
541 if (req->oldptr == NULL)
542 return (SYSCTL_OUT(req, 0, sizeof(snap)));
545 snap.ncs_goodhits = counter_u64_fetch(numposhits);
546 snap.ncs_neghits = counter_u64_fetch(numneghits);
547 snap.ncs_badhits = counter_u64_fetch(numposzaps) +
548 counter_u64_fetch(numnegzaps);
549 snap.ncs_miss = counter_u64_fetch(nummisszap) +
550 counter_u64_fetch(nummiss);
552 return (SYSCTL_OUT(req, &snap, sizeof(snap)));
554 SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE | CTLFLAG_RD |
555 CTLFLAG_MPSAFE, 0, 0, sysctl_nchstats, "LU",
556 "VFS cache effectiveness statistics");
560 * Grab an atomic snapshot of the name cache hash chain lengths
562 static SYSCTL_NODE(_debug, OID_AUTO, hashstat, CTLFLAG_RW, NULL,
566 sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS)
568 struct nchashhead *ncpp;
569 struct namecache *ncp;
570 int i, error, n_nchash, *cntbuf;
573 n_nchash = nchash + 1; /* nchash is max index, not count */
574 if (req->oldptr == NULL)
575 return SYSCTL_OUT(req, 0, n_nchash * sizeof(int));
576 cntbuf = malloc(n_nchash * sizeof(int), M_TEMP, M_ZERO | M_WAITOK);
577 cache_lock_all_buckets();
578 if (n_nchash != nchash + 1) {
579 cache_unlock_all_buckets();
580 free(cntbuf, M_TEMP);
583 /* Scan hash tables counting entries */
584 for (ncpp = nchashtbl, i = 0; i < n_nchash; ncpp++, i++)
585 LIST_FOREACH(ncp, ncpp, nc_hash)
587 cache_unlock_all_buckets();
588 for (error = 0, i = 0; i < n_nchash; i++)
589 if ((error = SYSCTL_OUT(req, &cntbuf[i], sizeof(int))) != 0)
591 free(cntbuf, M_TEMP);
594 SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD|
595 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_rawnchash, "S,int",
596 "nchash chain lengths");
599 sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS)
602 struct nchashhead *ncpp;
603 struct namecache *ncp;
605 int count, maxlength, used, pct;
608 return SYSCTL_OUT(req, 0, 4 * sizeof(int));
610 cache_lock_all_buckets();
611 n_nchash = nchash + 1; /* nchash is max index, not count */
615 /* Scan hash tables for applicable entries */
616 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
618 LIST_FOREACH(ncp, ncpp, nc_hash) {
623 if (maxlength < count)
626 n_nchash = nchash + 1;
627 cache_unlock_all_buckets();
628 pct = (used * 100) / (n_nchash / 100);
629 error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash));
632 error = SYSCTL_OUT(req, &used, sizeof(used));
635 error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength));
638 error = SYSCTL_OUT(req, &pct, sizeof(pct));
643 SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD|
644 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_nchash, "I",
645 "nchash statistics (number of total/used buckets, maximum chain length, usage percentage)");
649 * Negative entries management
651 * A variation of LRU scheme is used. New entries are hashed into one of
652 * numneglists cold lists. Entries get promoted to the hot list on first hit.
653 * Partial LRU for the hot list is maintained by requeueing them every
654 * ncneghitsrequeue hits.
656 * The shrinker will demote hot list head and evict from the cold list in a
657 * round-robin manner.
660 cache_negative_hit(struct namecache *ncp)
662 struct neglist *neglist;
665 MPASS(ncp->nc_flag & NCF_NEGATIVE);
666 hits = atomic_fetchadd_int(&ncp->nc_neghits, 1);
667 if (ncp->nc_flag & NCF_HOTNEGATIVE) {
668 if ((hits % ncneghitsrequeue) != 0)
670 mtx_lock(&ncneg_hot.nl_lock);
671 if (ncp->nc_flag & NCF_HOTNEGATIVE) {
672 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
673 TAILQ_INSERT_TAIL(&ncneg_hot.nl_list, ncp, nc_dst);
674 mtx_unlock(&ncneg_hot.nl_lock);
678 * The shrinker cleared the flag and removed the entry from
679 * the hot list. Put it back.
682 mtx_lock(&ncneg_hot.nl_lock);
684 neglist = NCP2NEGLIST(ncp);
685 mtx_lock(&neglist->nl_lock);
686 if (!(ncp->nc_flag & NCF_HOTNEGATIVE)) {
687 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
688 TAILQ_INSERT_TAIL(&ncneg_hot.nl_list, ncp, nc_dst);
689 ncp->nc_flag |= NCF_HOTNEGATIVE;
691 mtx_unlock(&neglist->nl_lock);
692 mtx_unlock(&ncneg_hot.nl_lock);
696 cache_negative_insert(struct namecache *ncp, bool neg_locked)
698 struct neglist *neglist;
700 MPASS(ncp->nc_flag & NCF_NEGATIVE);
701 cache_assert_bucket_locked(ncp, RA_WLOCKED);
702 neglist = NCP2NEGLIST(ncp);
704 mtx_lock(&neglist->nl_lock);
706 mtx_assert(&neglist->nl_lock, MA_OWNED);
708 TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst);
710 mtx_unlock(&neglist->nl_lock);
711 atomic_add_rel_long(&numneg, 1);
715 cache_negative_remove(struct namecache *ncp, bool neg_locked)
717 struct neglist *neglist;
718 bool hot_locked = false;
719 bool list_locked = false;
721 MPASS(ncp->nc_flag & NCF_NEGATIVE);
722 cache_assert_bucket_locked(ncp, RA_WLOCKED);
723 neglist = NCP2NEGLIST(ncp);
725 if (ncp->nc_flag & NCF_HOTNEGATIVE) {
727 mtx_lock(&ncneg_hot.nl_lock);
728 if (!(ncp->nc_flag & NCF_HOTNEGATIVE)) {
730 mtx_lock(&neglist->nl_lock);
734 mtx_lock(&neglist->nl_lock);
737 if (ncp->nc_flag & NCF_HOTNEGATIVE) {
738 mtx_assert(&ncneg_hot.nl_lock, MA_OWNED);
739 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
741 mtx_assert(&neglist->nl_lock, MA_OWNED);
742 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
745 mtx_unlock(&neglist->nl_lock);
747 mtx_unlock(&ncneg_hot.nl_lock);
748 atomic_subtract_rel_long(&numneg, 1);
752 cache_negative_shrink_select(int start, struct namecache **ncpp,
753 struct neglist **neglistpp)
755 struct neglist *neglist;
756 struct namecache *ncp;
762 for (i = start; i < numneglists; i++) {
763 neglist = &neglists[i];
764 if (TAILQ_FIRST(&neglist->nl_list) == NULL)
766 mtx_lock(&neglist->nl_lock);
767 ncp = TAILQ_FIRST(&neglist->nl_list);
770 mtx_unlock(&neglist->nl_lock);
773 *neglistpp = neglist;
778 cache_negative_zap_one(void)
780 struct namecache *ncp, *ncp2;
781 struct neglist *neglist;
785 if (!mtx_trylock(&ncneg_shrink_lock))
788 mtx_lock(&ncneg_hot.nl_lock);
789 ncp = TAILQ_FIRST(&ncneg_hot.nl_list);
791 neglist = NCP2NEGLIST(ncp);
792 mtx_lock(&neglist->nl_lock);
793 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
794 TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst);
795 ncp->nc_flag &= ~NCF_HOTNEGATIVE;
796 mtx_unlock(&neglist->nl_lock);
799 cache_negative_shrink_select(shrink_list_turn, &ncp, &neglist);
801 if (shrink_list_turn == numneglists)
802 shrink_list_turn = 0;
803 if (ncp == NULL && shrink_list_turn == 0)
804 cache_negative_shrink_select(shrink_list_turn, &ncp, &neglist);
806 mtx_unlock(&ncneg_hot.nl_lock);
810 MPASS(ncp->nc_flag & NCF_NEGATIVE);
811 dvlp = VP2VNODELOCK(ncp->nc_dvp);
812 blp = NCP2BUCKETLOCK(ncp);
813 mtx_unlock(&neglist->nl_lock);
814 mtx_unlock(&ncneg_hot.nl_lock);
817 mtx_lock(&neglist->nl_lock);
818 ncp2 = TAILQ_FIRST(&neglist->nl_list);
819 if (ncp != ncp2 || dvlp != VP2VNODELOCK(ncp2->nc_dvp) ||
820 blp != NCP2BUCKETLOCK(ncp2) || !(ncp2->nc_flag & NCF_NEGATIVE)) {
824 SDT_PROBE3(vfs, namecache, shrink_negative, done, ncp->nc_dvp,
825 ncp->nc_name, ncp->nc_neghits);
827 cache_zap_locked(ncp, true);
829 mtx_unlock(&neglist->nl_lock);
833 mtx_unlock(&ncneg_shrink_lock);
838 * cache_zap_locked():
840 * Removes a namecache entry from cache, whether it contains an actual
841 * pointer to a vnode or if it is just a negative cache entry.
844 cache_zap_locked(struct namecache *ncp, bool neg_locked)
847 if (!(ncp->nc_flag & NCF_NEGATIVE))
848 cache_assert_vnode_locked(ncp->nc_vp);
849 cache_assert_vnode_locked(ncp->nc_dvp);
850 cache_assert_bucket_locked(ncp, RA_WLOCKED);
852 CTR2(KTR_VFS, "cache_zap(%p) vp %p", ncp,
853 (ncp->nc_flag & NCF_NEGATIVE) ? NULL : ncp->nc_vp);
854 LIST_REMOVE(ncp, nc_hash);
855 if (!(ncp->nc_flag & NCF_NEGATIVE)) {
856 SDT_PROBE3(vfs, namecache, zap, done, ncp->nc_dvp,
857 ncp->nc_name, ncp->nc_vp);
858 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
859 if (ncp == ncp->nc_vp->v_cache_dd)
860 ncp->nc_vp->v_cache_dd = NULL;
862 SDT_PROBE3(vfs, namecache, zap_negative, done, ncp->nc_dvp,
863 ncp->nc_name, ncp->nc_neghits);
864 cache_negative_remove(ncp, neg_locked);
866 if (ncp->nc_flag & NCF_ISDOTDOT) {
867 if (ncp == ncp->nc_dvp->v_cache_dd)
868 ncp->nc_dvp->v_cache_dd = NULL;
870 LIST_REMOVE(ncp, nc_src);
871 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) {
872 ncp->nc_flag |= NCF_DVDROP;
873 atomic_subtract_rel_long(&numcachehv, 1);
876 atomic_subtract_rel_long(&numcache, 1);
880 cache_zap_negative_locked_vnode_kl(struct namecache *ncp, struct vnode *vp)
884 MPASS(ncp->nc_dvp == vp);
885 MPASS(ncp->nc_flag & NCF_NEGATIVE);
886 cache_assert_vnode_locked(vp);
888 blp = NCP2BUCKETLOCK(ncp);
890 cache_zap_locked(ncp, false);
895 cache_zap_locked_vnode_kl2(struct namecache *ncp, struct vnode *vp,
898 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
901 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
902 cache_assert_vnode_locked(vp);
904 if (ncp->nc_flag & NCF_NEGATIVE) {
909 cache_zap_negative_locked_vnode_kl(ncp, vp);
913 pvlp = VP2VNODELOCK(vp);
914 blp = NCP2BUCKETLOCK(ncp);
915 vlp1 = VP2VNODELOCK(ncp->nc_dvp);
916 vlp2 = VP2VNODELOCK(ncp->nc_vp);
918 if (*vlpp == vlp1 || *vlpp == vlp2) {
926 cache_sort(&vlp1, &vlp2);
931 if (!mtx_trylock(vlp1))
937 cache_zap_locked(ncp, false);
939 if (to_unlock != NULL)
940 mtx_unlock(to_unlock);
947 MPASS(*vlpp == NULL);
953 cache_zap_locked_vnode(struct namecache *ncp, struct vnode *vp)
955 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
959 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
960 cache_assert_vnode_locked(vp);
962 pvlp = VP2VNODELOCK(vp);
963 if (ncp->nc_flag & NCF_NEGATIVE) {
964 cache_zap_negative_locked_vnode_kl(ncp, vp);
968 blp = NCP2BUCKETLOCK(ncp);
969 vlp1 = VP2VNODELOCK(ncp->nc_dvp);
970 vlp2 = VP2VNODELOCK(ncp->nc_vp);
971 cache_sort(&vlp1, &vlp2);
976 if (!mtx_trylock(vlp1)) {
983 cache_zap_locked(ncp, false);
985 mtx_unlock(to_unlock);
992 cache_zap_wlocked_bucket(struct namecache *ncp, struct rwlock *blp)
994 struct mtx *dvlp, *vlp;
996 cache_assert_bucket_locked(ncp, RA_WLOCKED);
998 dvlp = VP2VNODELOCK(ncp->nc_dvp);
1000 if (!(ncp->nc_flag & NCF_NEGATIVE))
1001 vlp = VP2VNODELOCK(ncp->nc_vp);
1002 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1003 cache_zap_locked(ncp, false);
1005 cache_unlock_vnodes(dvlp, vlp);
1014 cache_zap_rlocked_bucket(struct namecache *ncp, struct rwlock *blp)
1016 struct mtx *dvlp, *vlp;
1018 cache_assert_bucket_locked(ncp, RA_RLOCKED);
1020 dvlp = VP2VNODELOCK(ncp->nc_dvp);
1022 if (!(ncp->nc_flag & NCF_NEGATIVE))
1023 vlp = VP2VNODELOCK(ncp->nc_vp);
1024 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1027 cache_zap_locked(ncp, false);
1029 cache_unlock_vnodes(dvlp, vlp);
1038 cache_zap_wlocked_bucket_kl(struct namecache *ncp, struct rwlock *blp,
1039 struct mtx **vlpp1, struct mtx **vlpp2)
1041 struct mtx *dvlp, *vlp;
1043 cache_assert_bucket_locked(ncp, RA_WLOCKED);
1045 dvlp = VP2VNODELOCK(ncp->nc_dvp);
1047 if (!(ncp->nc_flag & NCF_NEGATIVE))
1048 vlp = VP2VNODELOCK(ncp->nc_vp);
1049 cache_sort(&dvlp, &vlp);
1051 if (*vlpp1 == dvlp && *vlpp2 == vlp) {
1052 cache_zap_locked(ncp, false);
1053 cache_unlock_vnodes(dvlp, vlp);
1066 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1067 cache_zap_locked(ncp, false);
1068 cache_unlock_vnodes(dvlp, vlp);
1083 cache_lookup_unlock(struct rwlock *blp, struct mtx *vlp)
1093 static int __noinline
1094 cache_lookup_dot(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1095 struct timespec *tsp, int *ticksp)
1100 CTR2(KTR_VFS, "cache_lookup(%p, %s) found via .",
1101 dvp, cnp->cn_nameptr);
1102 counter_u64_add(dothits, 1);
1103 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", *vpp);
1110 * When we lookup "." we still can be asked to lock it
1113 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
1114 if (ltype != VOP_ISLOCKED(*vpp)) {
1115 if (ltype == LK_EXCLUSIVE) {
1116 vn_lock(*vpp, LK_UPGRADE | LK_RETRY);
1117 if ((*vpp)->v_iflag & VI_DOOMED) {
1118 /* forced unmount */
1124 vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY);
1129 static __noinline int
1130 cache_lookup_nomakeentry(struct vnode *dvp, struct vnode **vpp,
1131 struct componentname *cnp, struct timespec *tsp, int *ticksp)
1133 struct namecache *ncp;
1135 struct mtx *dvlp, *dvlp2;
1139 if (cnp->cn_namelen == 2 &&
1140 cnp->cn_nameptr[0] == '.' && cnp->cn_nameptr[1] == '.') {
1141 counter_u64_add(dotdothits, 1);
1142 dvlp = VP2VNODELOCK(dvp);
1146 ncp = dvp->v_cache_dd;
1148 SDT_PROBE3(vfs, namecache, lookup, miss, dvp,
1155 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1156 if (ncp->nc_dvp != dvp)
1157 panic("dvp %p v_cache_dd %p\n", dvp, ncp);
1158 if (!cache_zap_locked_vnode_kl2(ncp,
1161 MPASS(dvp->v_cache_dd == NULL);
1167 dvp->v_cache_dd = NULL;
1175 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1176 blp = HASH2BUCKETLOCK(hash);
1178 if (LIST_EMPTY(NCHHASH(hash)))
1183 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1184 counter_u64_add(numchecks, 1);
1185 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1186 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1190 /* We failed to find an entry */
1196 counter_u64_add(numposzaps, 1);
1198 error = cache_zap_wlocked_bucket(ncp, blp);
1200 zap_and_exit_bucket_fail++;
1201 cache_maybe_yield();
1207 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr, NULL);
1208 counter_u64_add(nummisszap, 1);
1213 * Lookup a name in the name cache
1217 * - dvp: Parent directory in which to search.
1218 * - vpp: Return argument. Will contain desired vnode on cache hit.
1219 * - cnp: Parameters of the name search. The most interesting bits of
1220 * the cn_flags field have the following meanings:
1221 * - MAKEENTRY: If clear, free an entry from the cache rather than look
1223 * - ISDOTDOT: Must be set if and only if cn_nameptr == ".."
1224 * - tsp: Return storage for cache timestamp. On a successful (positive
1225 * or negative) lookup, tsp will be filled with any timespec that
1226 * was stored when this cache entry was created. However, it will
1227 * be clear for "." entries.
1228 * - ticks: Return storage for alternate cache timestamp. On a successful
1229 * (positive or negative) lookup, it will contain the ticks value
1230 * that was current when the cache entry was created, unless cnp
1235 * - -1: A positive cache hit. vpp will contain the desired vnode.
1236 * - ENOENT: A negative cache hit, or dvp was recycled out from under us due
1237 * to a forced unmount. vpp will not be modified. If the entry
1238 * is a whiteout, then the ISWHITEOUT flag will be set in
1240 * - 0: A cache miss. vpp will not be modified.
1244 * On a cache hit, vpp will be returned locked and ref'd. If we're looking up
1245 * .., dvp is unlocked. If we're looking up . an extra ref is taken, but the
1246 * lock is not recursively acquired.
1249 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1250 struct timespec *tsp, int *ticksp)
1252 struct namecache_ts *ncp_ts;
1253 struct namecache *ncp;
1259 if (__predict_false(!doingcache)) {
1260 cnp->cn_flags &= ~MAKEENTRY;
1264 counter_u64_add(numcalls, 1);
1266 if (__predict_false(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.'))
1267 return (cache_lookup_dot(dvp, vpp, cnp, tsp, ticksp));
1269 if ((cnp->cn_flags & MAKEENTRY) == 0)
1270 return (cache_lookup_nomakeentry(dvp, vpp, cnp, tsp, ticksp));
1276 if (cnp->cn_namelen == 2 &&
1277 cnp->cn_nameptr[0] == '.' && cnp->cn_nameptr[1] == '.') {
1278 counter_u64_add(dotdothits, 1);
1279 dvlp = VP2VNODELOCK(dvp);
1281 ncp = dvp->v_cache_dd;
1283 SDT_PROBE3(vfs, namecache, lookup, miss, dvp,
1288 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1289 if (ncp->nc_flag & NCF_NEGATIVE)
1295 /* Return failure if negative entry was found. */
1297 goto negative_success;
1298 CTR3(KTR_VFS, "cache_lookup(%p, %s) found %p via ..",
1299 dvp, cnp->cn_nameptr, *vpp);
1300 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, "..",
1302 cache_out_ts(ncp, tsp, ticksp);
1303 if ((ncp->nc_flag & (NCF_ISDOTDOT | NCF_DTS)) ==
1304 NCF_DTS && tsp != NULL) {
1305 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1306 *tsp = ncp_ts->nc_dotdottime;
1311 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1312 blp = HASH2BUCKETLOCK(hash);
1315 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1316 counter_u64_add(numchecks, 1);
1317 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1318 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1322 /* We failed to find an entry */
1325 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
1327 counter_u64_add(nummiss, 1);
1331 /* We found a "positive" match, return the vnode */
1332 if (!(ncp->nc_flag & NCF_NEGATIVE)) {
1333 counter_u64_add(numposhits, 1);
1335 CTR4(KTR_VFS, "cache_lookup(%p, %s) found %p via ncp %p",
1336 dvp, cnp->cn_nameptr, *vpp, ncp);
1337 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name,
1339 cache_out_ts(ncp, tsp, ticksp);
1344 /* We found a negative match, and want to create it, so purge */
1345 if (cnp->cn_nameiop == CREATE) {
1346 counter_u64_add(numnegzaps, 1);
1350 counter_u64_add(numneghits, 1);
1351 cache_negative_hit(ncp);
1352 if (ncp->nc_flag & NCF_WHITE)
1353 cnp->cn_flags |= ISWHITEOUT;
1354 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp,
1356 cache_out_ts(ncp, tsp, ticksp);
1357 cache_lookup_unlock(blp, dvlp);
1362 * On success we return a locked and ref'd vnode as per the lookup
1366 ltype = 0; /* silence gcc warning */
1367 if (cnp->cn_flags & ISDOTDOT) {
1368 ltype = VOP_ISLOCKED(dvp);
1372 cache_lookup_unlock(blp, dvlp);
1373 error = vget(*vpp, cnp->cn_lkflags | LK_VNHELD, cnp->cn_thread);
1374 if (cnp->cn_flags & ISDOTDOT) {
1375 vn_lock(dvp, ltype | LK_RETRY);
1376 if (dvp->v_iflag & VI_DOOMED) {
1387 if ((cnp->cn_flags & ISLASTCN) &&
1388 (cnp->cn_lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE) {
1389 ASSERT_VOP_ELOCKED(*vpp, "cache_lookup");
1395 error = cache_zap_rlocked_bucket(ncp, blp);
1397 error = cache_zap_locked_vnode(ncp, dvp);
1399 zap_and_exit_bucket_fail++;
1400 cache_maybe_yield();
1407 struct celockstate {
1409 struct rwlock *blp[2];
1411 CTASSERT((nitems(((struct celockstate *)0)->vlp) == 3));
1412 CTASSERT((nitems(((struct celockstate *)0)->blp) == 2));
1415 cache_celockstate_init(struct celockstate *cel)
1418 bzero(cel, sizeof(*cel));
1422 cache_lock_vnodes_cel(struct celockstate *cel, struct vnode *vp,
1425 struct mtx *vlp1, *vlp2;
1427 MPASS(cel->vlp[0] == NULL);
1428 MPASS(cel->vlp[1] == NULL);
1429 MPASS(cel->vlp[2] == NULL);
1431 MPASS(vp != NULL || dvp != NULL);
1433 vlp1 = VP2VNODELOCK(vp);
1434 vlp2 = VP2VNODELOCK(dvp);
1435 cache_sort(&vlp1, &vlp2);
1446 cache_unlock_vnodes_cel(struct celockstate *cel)
1449 MPASS(cel->vlp[0] != NULL || cel->vlp[1] != NULL);
1451 if (cel->vlp[0] != NULL)
1452 mtx_unlock(cel->vlp[0]);
1453 if (cel->vlp[1] != NULL)
1454 mtx_unlock(cel->vlp[1]);
1455 if (cel->vlp[2] != NULL)
1456 mtx_unlock(cel->vlp[2]);
1460 cache_lock_vnodes_cel_3(struct celockstate *cel, struct vnode *vp)
1465 cache_assert_vlp_locked(cel->vlp[0]);
1466 cache_assert_vlp_locked(cel->vlp[1]);
1467 MPASS(cel->vlp[2] == NULL);
1470 vlp = VP2VNODELOCK(vp);
1473 if (vlp >= cel->vlp[1]) {
1476 if (mtx_trylock(vlp))
1478 cache_lock_vnodes_cel_3_failures++;
1479 cache_unlock_vnodes_cel(cel);
1480 if (vlp < cel->vlp[0]) {
1482 mtx_lock(cel->vlp[0]);
1483 mtx_lock(cel->vlp[1]);
1485 if (cel->vlp[0] != NULL)
1486 mtx_lock(cel->vlp[0]);
1488 mtx_lock(cel->vlp[1]);
1498 cache_lock_buckets_cel(struct celockstate *cel, struct rwlock *blp1,
1499 struct rwlock *blp2)
1502 MPASS(cel->blp[0] == NULL);
1503 MPASS(cel->blp[1] == NULL);
1505 cache_sort(&blp1, &blp2);
1516 cache_unlock_buckets_cel(struct celockstate *cel)
1519 if (cel->blp[0] != NULL)
1520 rw_wunlock(cel->blp[0]);
1521 rw_wunlock(cel->blp[1]);
1525 * Lock part of the cache affected by the insertion.
1527 * This means vnodelocks for dvp, vp and the relevant bucketlock.
1528 * However, insertion can result in removal of an old entry. In this
1529 * case we have an additional vnode and bucketlock pair to lock. If the
1530 * entry is negative, ncelock is locked instead of the vnode.
1532 * That is, in the worst case we have to lock 3 vnodes and 2 bucketlocks, while
1533 * preserving the locking order (smaller address first).
1536 cache_enter_lock(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1539 struct namecache *ncp;
1540 struct rwlock *blps[2];
1542 blps[0] = HASH2BUCKETLOCK(hash);
1545 cache_lock_vnodes_cel(cel, dvp, vp);
1546 if (vp == NULL || vp->v_type != VDIR)
1548 ncp = vp->v_cache_dd;
1551 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1553 MPASS(ncp->nc_dvp == vp);
1554 blps[1] = NCP2BUCKETLOCK(ncp);
1555 if (ncp->nc_flag & NCF_NEGATIVE)
1557 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1560 * All vnodes got re-locked. Re-validate the state and if
1561 * nothing changed we are done. Otherwise restart.
1563 if (ncp == vp->v_cache_dd &&
1564 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1565 blps[1] == NCP2BUCKETLOCK(ncp) &&
1566 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1568 cache_unlock_vnodes_cel(cel);
1573 cache_lock_buckets_cel(cel, blps[0], blps[1]);
1577 cache_enter_lock_dd(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1580 struct namecache *ncp;
1581 struct rwlock *blps[2];
1583 blps[0] = HASH2BUCKETLOCK(hash);
1586 cache_lock_vnodes_cel(cel, dvp, vp);
1587 ncp = dvp->v_cache_dd;
1590 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1592 MPASS(ncp->nc_dvp == dvp);
1593 blps[1] = NCP2BUCKETLOCK(ncp);
1594 if (ncp->nc_flag & NCF_NEGATIVE)
1596 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1598 if (ncp == dvp->v_cache_dd &&
1599 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1600 blps[1] == NCP2BUCKETLOCK(ncp) &&
1601 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1603 cache_unlock_vnodes_cel(cel);
1608 cache_lock_buckets_cel(cel, blps[0], blps[1]);
1612 cache_enter_unlock(struct celockstate *cel)
1615 cache_unlock_buckets_cel(cel);
1616 cache_unlock_vnodes_cel(cel);
1620 * Add an entry to the cache.
1623 cache_enter_time(struct vnode *dvp, struct vnode *vp, struct componentname *cnp,
1624 struct timespec *tsp, struct timespec *dtsp)
1626 struct celockstate cel;
1627 struct namecache *ncp, *n2, *ndd;
1628 struct namecache_ts *ncp_ts, *n2_ts;
1629 struct nchashhead *ncpp;
1630 struct neglist *neglist;
1637 CTR3(KTR_VFS, "cache_enter(%p, %p, %s)", dvp, vp, cnp->cn_nameptr);
1638 VNASSERT(vp == NULL || (vp->v_iflag & VI_DOOMED) == 0, vp,
1639 ("cache_enter: Adding a doomed vnode"));
1640 VNASSERT(dvp == NULL || (dvp->v_iflag & VI_DOOMED) == 0, dvp,
1641 ("cache_enter: Doomed vnode used as src"));
1643 if (__predict_false(!doingcache))
1647 * Avoid blowout in namecache entries.
1649 lnumcache = atomic_fetchadd_long(&numcache, 1) + 1;
1650 if (__predict_false(lnumcache >= desiredvnodes * ncsizefactor)) {
1651 atomic_add_long(&numcache, -1);
1655 cache_celockstate_init(&cel);
1659 if (cnp->cn_nameptr[0] == '.') {
1660 if (cnp->cn_namelen == 1)
1662 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
1663 len = cnp->cn_namelen;
1664 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1665 cache_enter_lock_dd(&cel, dvp, vp, hash);
1667 * If dotdot entry already exists, just retarget it
1668 * to new parent vnode, otherwise continue with new
1669 * namecache entry allocation.
1671 if ((ncp = dvp->v_cache_dd) != NULL &&
1672 ncp->nc_flag & NCF_ISDOTDOT) {
1673 KASSERT(ncp->nc_dvp == dvp,
1674 ("wrong isdotdot parent"));
1676 if (ncp->nc_flag & NCF_NEGATIVE || vp == NULL) {
1677 neglist = NCP2NEGLIST(ncp);
1678 mtx_lock(&ncneg_hot.nl_lock);
1679 mtx_lock(&neglist->nl_lock);
1682 if (!(ncp->nc_flag & NCF_NEGATIVE)) {
1683 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst,
1686 cache_negative_remove(ncp, true);
1689 TAILQ_INSERT_HEAD(&vp->v_cache_dst,
1691 ncp->nc_flag &= ~(NCF_NEGATIVE|NCF_HOTNEGATIVE);
1693 ncp->nc_flag &= ~(NCF_HOTNEGATIVE);
1694 ncp->nc_flag |= NCF_NEGATIVE;
1695 cache_negative_insert(ncp, true);
1698 mtx_unlock(&neglist->nl_lock);
1699 mtx_unlock(&ncneg_hot.nl_lock);
1702 cache_enter_unlock(&cel);
1705 dvp->v_cache_dd = NULL;
1706 cache_enter_unlock(&cel);
1707 cache_celockstate_init(&cel);
1708 SDT_PROBE3(vfs, namecache, enter, done, dvp, "..", vp);
1709 flag = NCF_ISDOTDOT;
1714 * Calculate the hash key and setup as much of the new
1715 * namecache entry as possible before acquiring the lock.
1717 ncp = cache_alloc(cnp->cn_namelen, tsp != NULL);
1718 ncp->nc_flag = flag;
1721 ncp->nc_flag |= NCF_NEGATIVE;
1724 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1725 ncp_ts->nc_time = *tsp;
1726 ncp_ts->nc_ticks = ticks;
1727 ncp_ts->nc_nc.nc_flag |= NCF_TS;
1729 ncp_ts->nc_dotdottime = *dtsp;
1730 ncp_ts->nc_nc.nc_flag |= NCF_DTS;
1733 len = ncp->nc_nlen = cnp->cn_namelen;
1734 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1735 strlcpy(ncp->nc_name, cnp->cn_nameptr, len + 1);
1736 cache_enter_lock(&cel, dvp, vp, hash);
1739 * See if this vnode or negative entry is already in the cache
1740 * with this name. This can happen with concurrent lookups of
1741 * the same path name.
1743 ncpp = NCHHASH(hash);
1744 LIST_FOREACH(n2, ncpp, nc_hash) {
1745 if (n2->nc_dvp == dvp &&
1746 n2->nc_nlen == cnp->cn_namelen &&
1747 !bcmp(n2->nc_name, cnp->cn_nameptr, n2->nc_nlen)) {
1749 KASSERT((n2->nc_flag & NCF_TS) != 0,
1751 n2_ts = __containerof(n2, struct namecache_ts, nc_nc);
1752 n2_ts->nc_time = ncp_ts->nc_time;
1753 n2_ts->nc_ticks = ncp_ts->nc_ticks;
1755 n2_ts->nc_dotdottime = ncp_ts->nc_dotdottime;
1756 if (ncp->nc_flag & NCF_NEGATIVE)
1757 mtx_lock(&ncneg_hot.nl_lock);
1758 n2_ts->nc_nc.nc_flag |= NCF_DTS;
1759 if (ncp->nc_flag & NCF_NEGATIVE)
1760 mtx_unlock(&ncneg_hot.nl_lock);
1763 goto out_unlock_free;
1767 if (flag == NCF_ISDOTDOT) {
1769 * See if we are trying to add .. entry, but some other lookup
1770 * has populated v_cache_dd pointer already.
1772 if (dvp->v_cache_dd != NULL)
1773 goto out_unlock_free;
1774 KASSERT(vp == NULL || vp->v_type == VDIR,
1775 ("wrong vnode type %p", vp));
1776 dvp->v_cache_dd = ncp;
1780 if (vp->v_type == VDIR) {
1781 if (flag != NCF_ISDOTDOT) {
1783 * For this case, the cache entry maps both the
1784 * directory name in it and the name ".." for the
1785 * directory's parent.
1787 if ((ndd = vp->v_cache_dd) != NULL) {
1788 if ((ndd->nc_flag & NCF_ISDOTDOT) != 0)
1789 cache_zap_locked(ndd, false);
1793 vp->v_cache_dd = ncp;
1796 vp->v_cache_dd = NULL;
1800 if (flag != NCF_ISDOTDOT) {
1801 if (LIST_EMPTY(&dvp->v_cache_src)) {
1803 atomic_add_rel_long(&numcachehv, 1);
1805 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
1809 * Insert the new namecache entry into the appropriate chain
1810 * within the cache entries table.
1812 LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
1815 * If the entry is "negative", we place it into the
1816 * "negative" cache queue, otherwise, we place it into the
1817 * destination vnode's cache entries queue.
1820 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
1821 SDT_PROBE3(vfs, namecache, enter, done, dvp, ncp->nc_name,
1824 if (cnp->cn_flags & ISWHITEOUT)
1825 ncp->nc_flag |= NCF_WHITE;
1826 cache_negative_insert(ncp, false);
1827 SDT_PROBE2(vfs, namecache, enter_negative, done, dvp,
1830 cache_enter_unlock(&cel);
1831 if (numneg * ncnegfactor > lnumcache)
1832 cache_negative_zap_one();
1836 cache_enter_unlock(&cel);
1842 cache_roundup_2(u_int val)
1846 for (res = 1; res <= val; res <<= 1)
1853 * Name cache initialization, from vfs_init() when we are booting
1856 nchinit(void *dummy __unused)
1860 cache_zone_small = uma_zcreate("S VFS Cache",
1861 sizeof(struct namecache) + CACHE_PATH_CUTOFF + 1,
1862 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache),
1864 cache_zone_small_ts = uma_zcreate("STS VFS Cache",
1865 sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1,
1866 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache_ts),
1868 cache_zone_large = uma_zcreate("L VFS Cache",
1869 sizeof(struct namecache) + NAME_MAX + 1,
1870 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache),
1872 cache_zone_large_ts = uma_zcreate("LTS VFS Cache",
1873 sizeof(struct namecache_ts) + NAME_MAX + 1,
1874 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache_ts),
1877 nchashtbl = hashinit(desiredvnodes * 2, M_VFSCACHE, &nchash);
1878 ncbuckethash = cache_roundup_2(mp_ncpus * 64) - 1;
1879 if (ncbuckethash > nchash)
1880 ncbuckethash = nchash;
1881 bucketlocks = malloc(sizeof(*bucketlocks) * numbucketlocks, M_VFSCACHE,
1883 for (i = 0; i < numbucketlocks; i++)
1884 rw_init_flags(&bucketlocks[i], "ncbuc", RW_DUPOK | RW_RECURSE);
1885 ncvnodehash = cache_roundup_2(mp_ncpus * 64) - 1;
1886 vnodelocks = malloc(sizeof(*vnodelocks) * numvnodelocks, M_VFSCACHE,
1888 for (i = 0; i < numvnodelocks; i++)
1889 mtx_init(&vnodelocks[i], "ncvn", NULL, MTX_DUPOK | MTX_RECURSE);
1890 ncpurgeminvnodes = numbucketlocks;
1893 neglists = malloc(sizeof(*neglists) * numneglists, M_VFSCACHE,
1895 for (i = 0; i < numneglists; i++) {
1896 mtx_init(&neglists[i].nl_lock, "ncnegl", NULL, MTX_DEF);
1897 TAILQ_INIT(&neglists[i].nl_list);
1899 mtx_init(&ncneg_hot.nl_lock, "ncneglh", NULL, MTX_DEF);
1900 TAILQ_INIT(&ncneg_hot.nl_list);
1902 mtx_init(&ncneg_shrink_lock, "ncnegs", NULL, MTX_DEF);
1904 numcalls = counter_u64_alloc(M_WAITOK);
1905 dothits = counter_u64_alloc(M_WAITOK);
1906 dotdothits = counter_u64_alloc(M_WAITOK);
1907 numchecks = counter_u64_alloc(M_WAITOK);
1908 nummiss = counter_u64_alloc(M_WAITOK);
1909 nummisszap = counter_u64_alloc(M_WAITOK);
1910 numposzaps = counter_u64_alloc(M_WAITOK);
1911 numposhits = counter_u64_alloc(M_WAITOK);
1912 numnegzaps = counter_u64_alloc(M_WAITOK);
1913 numneghits = counter_u64_alloc(M_WAITOK);
1914 numfullpathcalls = counter_u64_alloc(M_WAITOK);
1915 numfullpathfail1 = counter_u64_alloc(M_WAITOK);
1916 numfullpathfail2 = counter_u64_alloc(M_WAITOK);
1917 numfullpathfail4 = counter_u64_alloc(M_WAITOK);
1918 numfullpathfound = counter_u64_alloc(M_WAITOK);
1920 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL);
1923 cache_changesize(int newmaxvnodes)
1925 struct nchashhead *new_nchashtbl, *old_nchashtbl;
1926 u_long new_nchash, old_nchash;
1927 struct namecache *ncp;
1931 newmaxvnodes = cache_roundup_2(newmaxvnodes * 2);
1932 if (newmaxvnodes < numbucketlocks)
1933 newmaxvnodes = numbucketlocks;
1935 new_nchashtbl = hashinit(newmaxvnodes, M_VFSCACHE, &new_nchash);
1936 /* If same hash table size, nothing to do */
1937 if (nchash == new_nchash) {
1938 free(new_nchashtbl, M_VFSCACHE);
1942 * Move everything from the old hash table to the new table.
1943 * None of the namecache entries in the table can be removed
1944 * because to do so, they have to be removed from the hash table.
1946 cache_lock_all_vnodes();
1947 cache_lock_all_buckets();
1948 old_nchashtbl = nchashtbl;
1949 old_nchash = nchash;
1950 nchashtbl = new_nchashtbl;
1951 nchash = new_nchash;
1952 for (i = 0; i <= old_nchash; i++) {
1953 while ((ncp = LIST_FIRST(&old_nchashtbl[i])) != NULL) {
1954 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen,
1956 LIST_REMOVE(ncp, nc_hash);
1957 LIST_INSERT_HEAD(NCHHASH(hash), ncp, nc_hash);
1960 cache_unlock_all_buckets();
1961 cache_unlock_all_vnodes();
1962 free(old_nchashtbl, M_VFSCACHE);
1966 * Invalidate all entries to a particular vnode.
1969 cache_purge(struct vnode *vp)
1971 TAILQ_HEAD(, namecache) ncps;
1972 struct namecache *ncp, *nnp;
1973 struct mtx *vlp, *vlp2;
1975 CTR1(KTR_VFS, "cache_purge(%p)", vp);
1976 SDT_PROBE1(vfs, namecache, purge, done, vp);
1977 if (LIST_EMPTY(&vp->v_cache_src) && TAILQ_EMPTY(&vp->v_cache_dst) &&
1978 vp->v_cache_dd == NULL)
1981 vlp = VP2VNODELOCK(vp);
1985 while (!LIST_EMPTY(&vp->v_cache_src)) {
1986 ncp = LIST_FIRST(&vp->v_cache_src);
1987 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
1989 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
1991 while (!TAILQ_EMPTY(&vp->v_cache_dst)) {
1992 ncp = TAILQ_FIRST(&vp->v_cache_dst);
1993 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
1995 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
1997 ncp = vp->v_cache_dd;
1999 KASSERT(ncp->nc_flag & NCF_ISDOTDOT,
2000 ("lost dotdot link"));
2001 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2003 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2005 KASSERT(vp->v_cache_dd == NULL, ("incomplete purge"));
2009 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2015 * Invalidate all negative entries for a particular directory vnode.
2018 cache_purge_negative(struct vnode *vp)
2020 TAILQ_HEAD(, namecache) ncps;
2021 struct namecache *ncp, *nnp;
2024 CTR1(KTR_VFS, "cache_purge_negative(%p)", vp);
2025 SDT_PROBE1(vfs, namecache, purge_negative, done, vp);
2026 if (LIST_EMPTY(&vp->v_cache_src))
2029 vlp = VP2VNODELOCK(vp);
2031 LIST_FOREACH_SAFE(ncp, &vp->v_cache_src, nc_src, nnp) {
2032 if (!(ncp->nc_flag & NCF_NEGATIVE))
2034 cache_zap_negative_locked_vnode_kl(ncp, vp);
2035 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2038 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2044 * Flush all entries referencing a particular filesystem.
2047 cache_purgevfs(struct mount *mp, bool force)
2049 TAILQ_HEAD(, namecache) ncps;
2050 struct mtx *vlp1, *vlp2;
2052 struct nchashhead *bucket;
2053 struct namecache *ncp, *nnp;
2054 u_long i, j, n_nchash;
2057 /* Scan hash tables for applicable entries */
2058 SDT_PROBE1(vfs, namecache, purgevfs, done, mp);
2059 if (!force && mp->mnt_nvnodelistsize <= ncpurgeminvnodes)
2062 n_nchash = nchash + 1;
2064 for (i = 0; i < numbucketlocks; i++) {
2065 blp = (struct rwlock *)&bucketlocks[i];
2067 for (j = i; j < n_nchash; j += numbucketlocks) {
2069 bucket = &nchashtbl[j];
2070 LIST_FOREACH_SAFE(ncp, bucket, nc_hash, nnp) {
2071 cache_assert_bucket_locked(ncp, RA_WLOCKED);
2072 if (ncp->nc_dvp->v_mount != mp)
2074 error = cache_zap_wlocked_bucket_kl(ncp, blp,
2078 TAILQ_INSERT_HEAD(&ncps, ncp, nc_dst);
2082 if (vlp1 == NULL && vlp2 == NULL)
2083 cache_maybe_yield();
2090 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2096 * Perform canonical checks and cache lookup and pass on to filesystem
2097 * through the vop_cachedlookup only if needed.
2101 vfs_cache_lookup(struct vop_lookup_args *ap)
2105 struct vnode **vpp = ap->a_vpp;
2106 struct componentname *cnp = ap->a_cnp;
2107 struct ucred *cred = cnp->cn_cred;
2108 int flags = cnp->cn_flags;
2109 struct thread *td = cnp->cn_thread;
2114 if (dvp->v_type != VDIR)
2117 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
2118 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
2121 error = VOP_ACCESS(dvp, VEXEC, cred, td);
2125 error = cache_lookup(dvp, vpp, cnp, NULL, NULL);
2127 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
2134 * XXX All of these sysctls would probably be more productive dead.
2136 static int __read_mostly disablecwd;
2137 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0,
2138 "Disable the getcwd syscall");
2140 /* Implementation of the getcwd syscall. */
2142 sys___getcwd(struct thread *td, struct __getcwd_args *uap)
2145 return (kern___getcwd(td, uap->buf, UIO_USERSPACE, uap->buflen,
2150 kern___getcwd(struct thread *td, char *buf, enum uio_seg bufseg, size_t buflen,
2154 struct filedesc *fdp;
2155 struct vnode *cdir, *rdir;
2158 if (__predict_false(disablecwd))
2160 if (__predict_false(buflen < 2))
2162 if (buflen > path_max)
2165 tmpbuf = malloc(buflen, M_TEMP, M_WAITOK);
2166 fdp = td->td_proc->p_fd;
2167 FILEDESC_SLOCK(fdp);
2168 cdir = fdp->fd_cdir;
2170 rdir = fdp->fd_rdir;
2172 FILEDESC_SUNLOCK(fdp);
2173 error = vn_fullpath1(td, cdir, rdir, tmpbuf, &bp, buflen);
2178 if (bufseg == UIO_SYSSPACE)
2179 bcopy(bp, buf, strlen(bp) + 1);
2181 error = copyout(bp, buf, strlen(bp) + 1);
2183 if (KTRPOINT(curthread, KTR_NAMEI))
2187 free(tmpbuf, M_TEMP);
2192 * Thus begins the fullpath magic.
2195 static int __read_mostly disablefullpath;
2196 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW, &disablefullpath, 0,
2197 "Disable the vn_fullpath function");
2200 * Retrieve the full filesystem path that correspond to a vnode from the name
2201 * cache (if available)
2204 vn_fullpath(struct thread *td, struct vnode *vn, char **retbuf, char **freebuf)
2207 struct filedesc *fdp;
2211 if (__predict_false(disablefullpath))
2213 if (__predict_false(vn == NULL))
2216 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
2217 fdp = td->td_proc->p_fd;
2218 FILEDESC_SLOCK(fdp);
2219 rdir = fdp->fd_rdir;
2221 FILEDESC_SUNLOCK(fdp);
2222 error = vn_fullpath1(td, vn, rdir, buf, retbuf, MAXPATHLEN);
2233 * This function is similar to vn_fullpath, but it attempts to lookup the
2234 * pathname relative to the global root mount point. This is required for the
2235 * auditing sub-system, as audited pathnames must be absolute, relative to the
2236 * global root mount point.
2239 vn_fullpath_global(struct thread *td, struct vnode *vn,
2240 char **retbuf, char **freebuf)
2245 if (__predict_false(disablefullpath))
2247 if (__predict_false(vn == NULL))
2249 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
2250 error = vn_fullpath1(td, vn, rootvnode, buf, retbuf, MAXPATHLEN);
2259 vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, u_int *buflen)
2262 struct namecache *ncp;
2266 vlp = VP2VNODELOCK(*vp);
2268 TAILQ_FOREACH(ncp, &((*vp)->v_cache_dst), nc_dst) {
2269 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2273 if (*buflen < ncp->nc_nlen) {
2276 counter_u64_add(numfullpathfail4, 1);
2278 SDT_PROBE3(vfs, namecache, fullpath, return, error,
2282 *buflen -= ncp->nc_nlen;
2283 memcpy(buf + *buflen, ncp->nc_name, ncp->nc_nlen);
2284 SDT_PROBE3(vfs, namecache, fullpath, hit, ncp->nc_dvp,
2293 SDT_PROBE1(vfs, namecache, fullpath, miss, vp);
2296 vn_lock(*vp, LK_SHARED | LK_RETRY);
2297 error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen);
2300 counter_u64_add(numfullpathfail2, 1);
2301 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2306 if (dvp->v_iflag & VI_DOOMED) {
2307 /* forced unmount */
2310 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2314 * *vp has its use count incremented still.
2321 * The magic behind kern___getcwd() and vn_fullpath().
2324 vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
2325 char *buf, char **retbuf, u_int buflen)
2327 int error, slash_prefixed;
2328 #ifdef KDTRACE_HOOKS
2329 struct vnode *startvp = vp;
2338 SDT_PROBE1(vfs, namecache, fullpath, entry, vp);
2339 counter_u64_add(numfullpathcalls, 1);
2341 if (vp->v_type != VDIR) {
2342 error = vn_vptocnp(&vp, td->td_ucred, buf, &buflen);
2349 buf[--buflen] = '/';
2352 while (vp != rdir && vp != rootvnode) {
2354 * The vp vnode must be already fully constructed,
2355 * since it is either found in namecache or obtained
2356 * from VOP_VPTOCNP(). We may test for VV_ROOT safely
2357 * without obtaining the vnode lock.
2359 if ((vp->v_vflag & VV_ROOT) != 0) {
2360 vn_lock(vp, LK_RETRY | LK_SHARED);
2363 * With the vnode locked, check for races with
2364 * unmount, forced or not. Note that we
2365 * already verified that vp is not equal to
2366 * the root vnode, which means that
2367 * mnt_vnodecovered can be NULL only for the
2370 if ((vp->v_iflag & VI_DOOMED) != 0 ||
2371 (vp1 = vp->v_mount->mnt_vnodecovered) == NULL ||
2372 vp1->v_mountedhere != vp->v_mount) {
2375 SDT_PROBE3(vfs, namecache, fullpath, return,
2385 if (vp->v_type != VDIR) {
2387 counter_u64_add(numfullpathfail1, 1);
2389 SDT_PROBE3(vfs, namecache, fullpath, return,
2393 error = vn_vptocnp(&vp, td->td_ucred, buf, &buflen);
2399 SDT_PROBE3(vfs, namecache, fullpath, return, error,
2403 buf[--buflen] = '/';
2408 if (!slash_prefixed) {
2411 counter_u64_add(numfullpathfail4, 1);
2412 SDT_PROBE3(vfs, namecache, fullpath, return, ENOMEM,
2416 buf[--buflen] = '/';
2418 counter_u64_add(numfullpathfound, 1);
2421 SDT_PROBE3(vfs, namecache, fullpath, return, 0, startvp, buf + buflen);
2422 *retbuf = buf + buflen;
2427 vn_dir_dd_ino(struct vnode *vp)
2429 struct namecache *ncp;
2433 ASSERT_VOP_LOCKED(vp, "vn_dir_dd_ino");
2434 vlp = VP2VNODELOCK(vp);
2436 TAILQ_FOREACH(ncp, &(vp->v_cache_dst), nc_dst) {
2437 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0)
2442 if (vget(ddvp, LK_SHARED | LK_NOWAIT | LK_VNHELD, curthread))
2451 vn_commname(struct vnode *vp, char *buf, u_int buflen)
2453 struct namecache *ncp;
2457 vlp = VP2VNODELOCK(vp);
2459 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst)
2460 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2466 l = min(ncp->nc_nlen, buflen - 1);
2467 memcpy(buf, ncp->nc_name, l);
2474 * This function updates path string to vnode's full global path
2475 * and checks the size of the new path string against the pathlen argument.
2477 * Requires a locked, referenced vnode.
2478 * Vnode is re-locked on success or ENODEV, otherwise unlocked.
2480 * If sysctl debug.disablefullpath is set, ENODEV is returned,
2481 * vnode is left locked and path remain untouched.
2483 * If vp is a directory, the call to vn_fullpath_global() always succeeds
2484 * because it falls back to the ".." lookup if the namecache lookup fails.
2487 vn_path_to_global_path(struct thread *td, struct vnode *vp, char *path,
2490 struct nameidata nd;
2495 ASSERT_VOP_ELOCKED(vp, __func__);
2497 /* Return ENODEV if sysctl debug.disablefullpath==1 */
2498 if (__predict_false(disablefullpath))
2501 /* Construct global filesystem path from vp. */
2503 error = vn_fullpath_global(td, vp, &rpath, &fbuf);
2510 if (strlen(rpath) >= pathlen) {
2512 error = ENAMETOOLONG;
2517 * Re-lookup the vnode by path to detect a possible rename.
2518 * As a side effect, the vnode is relocked.
2519 * If vnode was renamed, return ENOENT.
2521 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
2522 UIO_SYSSPACE, path, td);
2528 NDFREE(&nd, NDF_ONLY_PNBUF);
2532 strcpy(path, rpath);
2545 db_print_vpath(struct vnode *vp)
2548 while (vp != NULL) {
2549 db_printf("%p: ", vp);
2550 if (vp == rootvnode) {
2554 if (vp->v_vflag & VV_ROOT) {
2555 db_printf("<mount point>");
2556 vp = vp->v_mount->mnt_vnodecovered;
2558 struct namecache *ncp;
2562 ncp = TAILQ_FIRST(&vp->v_cache_dst);
2565 for (i = 0; i < ncp->nc_nlen; i++)
2566 db_printf("%c", *ncn++);
2579 DB_SHOW_COMMAND(vpath, db_show_vpath)
2584 db_printf("usage: show vpath <struct vnode *>\n");
2588 vp = (struct vnode *)addr;