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$");
40 #include "opt_ktrace.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/counter.h>
45 #include <sys/filedesc.h>
46 #include <sys/fnv_hash.h>
47 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/fcntl.h>
51 #include <sys/mount.h>
52 #include <sys/namei.h>
54 #include <sys/rwlock.h>
57 #include <sys/syscallsubr.h>
58 #include <sys/sysctl.h>
59 #include <sys/sysproto.h>
60 #include <sys/vnode.h>
62 #include <sys/ktrace.h>
67 SDT_PROVIDER_DECLARE(vfs);
68 SDT_PROBE_DEFINE3(vfs, namecache, enter, done, "struct vnode *", "char *",
70 SDT_PROBE_DEFINE2(vfs, namecache, enter_negative, done, "struct vnode *",
72 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, entry, "struct vnode *");
73 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, hit, "struct vnode *",
74 "char *", "struct vnode *");
75 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, miss, "struct vnode *");
76 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, return, "int",
77 "struct vnode *", "char *");
78 SDT_PROBE_DEFINE3(vfs, namecache, lookup, hit, "struct vnode *", "char *",
80 SDT_PROBE_DEFINE2(vfs, namecache, lookup, hit__negative,
81 "struct vnode *", "char *");
82 SDT_PROBE_DEFINE2(vfs, namecache, lookup, miss, "struct vnode *",
84 SDT_PROBE_DEFINE1(vfs, namecache, purge, done, "struct vnode *");
85 SDT_PROBE_DEFINE1(vfs, namecache, purge_negative, done, "struct vnode *");
86 SDT_PROBE_DEFINE1(vfs, namecache, purgevfs, done, "struct mount *");
87 SDT_PROBE_DEFINE3(vfs, namecache, zap, done, "struct vnode *", "char *",
89 SDT_PROBE_DEFINE3(vfs, namecache, zap_negative, done, "struct vnode *",
91 SDT_PROBE_DEFINE3(vfs, namecache, shrink_negative, done, "struct vnode *",
95 * This structure describes the elements in the cache of recent
96 * names looked up by namei.
100 LIST_ENTRY(namecache) nc_hash; /* hash chain */
101 LIST_ENTRY(namecache) nc_src; /* source vnode list */
102 TAILQ_ENTRY(namecache) nc_dst; /* destination vnode list */
103 struct vnode *nc_dvp; /* vnode of parent of name */
105 struct vnode *nu_vp; /* vnode the name refers to */
106 u_int nu_neghits; /* negative entry hits */
108 u_char nc_flag; /* flag bits */
109 u_char nc_nlen; /* length of name */
110 char nc_name[0]; /* segment name + nul */
114 * struct namecache_ts repeats struct namecache layout up to the
116 * struct namecache_ts is used in place of struct namecache when time(s) need
117 * to be stored. The nc_dotdottime field is used when a cache entry is mapping
118 * both a non-dotdot directory name plus dotdot for the directory's
121 struct namecache_ts {
122 struct timespec nc_time; /* timespec provided by fs */
123 struct timespec nc_dotdottime; /* dotdot timespec provided by fs */
124 int nc_ticks; /* ticks value when entry was added */
125 struct namecache nc_nc;
128 #define nc_vp n_un.nu_vp
129 #define nc_neghits n_un.nu_neghits
132 * Flags in namecache.nc_flag
134 #define NCF_WHITE 0x01
135 #define NCF_ISDOTDOT 0x02
138 #define NCF_DVDROP 0x10
139 #define NCF_NEGATIVE 0x20
140 #define NCF_HOTNEGATIVE 0x40
143 * Name caching works as follows:
145 * Names found by directory scans are retained in a cache
146 * for future reference. It is managed LRU, so frequently
147 * used names will hang around. Cache is indexed by hash value
148 * obtained from (vp, name) where vp refers to the directory
151 * If it is a "negative" entry, (i.e. for a name that is known NOT to
152 * exist) the vnode pointer will be NULL.
154 * Upon reaching the last segment of a path, if the reference
155 * is for DELETE, or NOCACHE is set (rewrite), and the
156 * name is located in the cache, it will be dropped.
158 * These locks are used (in the order in which they can be taken):
160 * vnodelock mtx vnode lists and v_cache_dd field protection
161 * bucketlock rwlock for access to given set of hash buckets
162 * neglist mtx negative entry LRU management
164 * Additionally, ncneg_shrink_lock mtx is used to have at most one thread
165 * shrinking the LRU list.
167 * It is legal to take multiple vnodelock and bucketlock locks. The locking
168 * order is lower address first. Both are recursive.
170 * "." lookups are lockless.
172 * ".." and vnode -> name lookups require vnodelock.
174 * name -> vnode lookup requires the relevant bucketlock to be held for reading.
176 * Insertions and removals of entries require involved vnodes and bucketlocks
177 * to be write-locked to prevent other threads from seeing the entry.
179 * Some lookups result in removal of the found entry (e.g. getting rid of a
180 * negative entry with the intent to create a positive one), which poses a
181 * problem when multiple threads reach the state. Similarly, two different
182 * threads can purge two different vnodes and try to remove the same name.
184 * If the already held vnode lock is lower than the second required lock, we
185 * can just take the other lock. However, in the opposite case, this could
186 * deadlock. As such, this is resolved by trylocking and if that fails unlocking
187 * the first node, locking everything in order and revalidating the state.
191 * Structures associated with name caching.
193 #define NCHHASH(hash) \
194 (&nchashtbl[(hash) & nchash])
195 static __read_mostly LIST_HEAD(nchashhead, namecache) *nchashtbl;/* Hash Table */
196 static u_long __read_mostly nchash; /* size of hash table */
197 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0,
198 "Size of namecache hash table");
199 static u_long __read_mostly ncnegfactor = 12; /* ratio of negative entries */
200 SYSCTL_ULONG(_vfs, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0,
201 "Ratio of negative namecache entries");
202 static u_long __exclusive_cache_line numneg; /* number of negative entries allocated */
203 SYSCTL_ULONG(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0,
204 "Number of negative entries in namecache");
205 static u_long __exclusive_cache_line numcache;/* number of cache entries allocated */
206 SYSCTL_ULONG(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0,
207 "Number of namecache entries");
208 static u_long __exclusive_cache_line numcachehv;/* number of cache entries with vnodes held */
209 SYSCTL_ULONG(_debug, OID_AUTO, numcachehv, CTLFLAG_RD, &numcachehv, 0,
210 "Number of namecache entries with vnodes held");
211 u_int __read_mostly ncsizefactor = 2;
212 SYSCTL_UINT(_vfs, OID_AUTO, ncsizefactor, CTLFLAG_RW, &ncsizefactor, 0,
213 "Size factor for namecache");
214 static u_int __read_mostly ncpurgeminvnodes;
215 SYSCTL_UINT(_vfs, OID_AUTO, ncpurgeminvnodes, CTLFLAG_RW, &ncpurgeminvnodes, 0,
216 "Number of vnodes below which purgevfs ignores the request");
217 static u_int __read_mostly ncneghitsrequeue = 8;
218 SYSCTL_UINT(_vfs, OID_AUTO, ncneghitsrequeue, CTLFLAG_RW, &ncneghitsrequeue, 0,
219 "Number of hits to requeue a negative entry in the LRU list");
221 struct nchstats nchstats; /* cache effectiveness statistics */
223 static struct mtx ncneg_shrink_lock;
224 static int shrink_list_turn;
228 TAILQ_HEAD(, namecache) nl_list;
229 } __aligned(CACHE_LINE_SIZE);
231 static struct neglist __read_mostly *neglists;
232 static struct neglist ncneg_hot;
234 #define numneglists (ncneghash + 1)
235 static u_int __read_mostly ncneghash;
236 static inline struct neglist *
237 NCP2NEGLIST(struct namecache *ncp)
240 return (&neglists[(((uintptr_t)(ncp) >> 8) & ncneghash)]);
243 #define numbucketlocks (ncbuckethash + 1)
244 static u_int __read_mostly ncbuckethash;
245 static struct rwlock_padalign __read_mostly *bucketlocks;
246 #define HASH2BUCKETLOCK(hash) \
247 ((struct rwlock *)(&bucketlocks[((hash) & ncbuckethash)]))
249 #define numvnodelocks (ncvnodehash + 1)
250 static u_int __read_mostly ncvnodehash;
251 static struct mtx __read_mostly *vnodelocks;
252 static inline struct mtx *
253 VP2VNODELOCK(struct vnode *vp)
256 return (&vnodelocks[(((uintptr_t)(vp) >> 8) & ncvnodehash)]);
260 * UMA zones for the VFS cache.
262 * The small cache is used for entries with short names, which are the
263 * most common. The large cache is used for entries which are too big to
264 * fit in the small cache.
266 static uma_zone_t __read_mostly cache_zone_small;
267 static uma_zone_t __read_mostly cache_zone_small_ts;
268 static uma_zone_t __read_mostly cache_zone_large;
269 static uma_zone_t __read_mostly cache_zone_large_ts;
271 #define CACHE_PATH_CUTOFF 35
273 static struct namecache *
274 cache_alloc(int len, int ts)
276 struct namecache_ts *ncp_ts;
277 struct namecache *ncp;
279 if (__predict_false(ts)) {
280 if (len <= CACHE_PATH_CUTOFF)
281 ncp_ts = uma_zalloc(cache_zone_small_ts, M_WAITOK);
283 ncp_ts = uma_zalloc(cache_zone_large_ts, M_WAITOK);
284 ncp = &ncp_ts->nc_nc;
286 if (len <= CACHE_PATH_CUTOFF)
287 ncp = uma_zalloc(cache_zone_small, M_WAITOK);
289 ncp = uma_zalloc(cache_zone_large, M_WAITOK);
295 cache_free(struct namecache *ncp)
297 struct namecache_ts *ncp_ts;
301 if ((ncp->nc_flag & NCF_DVDROP) != 0)
303 if (__predict_false(ncp->nc_flag & NCF_TS)) {
304 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
305 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
306 uma_zfree(cache_zone_small_ts, ncp_ts);
308 uma_zfree(cache_zone_large_ts, ncp_ts);
310 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
311 uma_zfree(cache_zone_small, ncp);
313 uma_zfree(cache_zone_large, ncp);
318 cache_out_ts(struct namecache *ncp, struct timespec *tsp, int *ticksp)
320 struct namecache_ts *ncp_ts;
322 KASSERT((ncp->nc_flag & NCF_TS) != 0 ||
323 (tsp == NULL && ticksp == NULL),
326 if (tsp == NULL && ticksp == NULL)
329 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
331 *tsp = ncp_ts->nc_time;
333 *ticksp = ncp_ts->nc_ticks;
336 static int __read_mostly doingcache = 1; /* 1 => enable the cache */
337 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0,
338 "VFS namecache enabled");
340 /* Export size information to userland */
341 SYSCTL_INT(_debug_sizeof, OID_AUTO, namecache, CTLFLAG_RD, SYSCTL_NULL_INT_PTR,
342 sizeof(struct namecache), "sizeof(struct namecache)");
345 * The new name cache statistics
347 static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0,
348 "Name cache statistics");
349 #define STATNODE_ULONG(name, descr) \
350 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, descr);
351 #define STATNODE_COUNTER(name, descr) \
352 static counter_u64_t __read_mostly name; \
353 SYSCTL_COUNTER_U64(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, descr);
354 STATNODE_ULONG(numneg, "Number of negative cache entries");
355 STATNODE_ULONG(numcache, "Number of cache entries");
356 STATNODE_COUNTER(numcalls, "Number of cache lookups");
357 STATNODE_COUNTER(dothits, "Number of '.' hits");
358 STATNODE_COUNTER(dotdothits, "Number of '..' hits");
359 STATNODE_COUNTER(numchecks, "Number of checks in lookup");
360 STATNODE_COUNTER(nummiss, "Number of cache misses");
361 STATNODE_COUNTER(nummisszap, "Number of cache misses we do not want to cache");
362 STATNODE_COUNTER(numposzaps,
363 "Number of cache hits (positive) we do not want to cache");
364 STATNODE_COUNTER(numposhits, "Number of cache hits (positive)");
365 STATNODE_COUNTER(numnegzaps,
366 "Number of cache hits (negative) we do not want to cache");
367 STATNODE_COUNTER(numneghits, "Number of cache hits (negative)");
368 /* These count for kern___getcwd(), too. */
369 STATNODE_COUNTER(numfullpathcalls, "Number of fullpath search calls");
370 STATNODE_COUNTER(numfullpathfail1, "Number of fullpath search errors (ENOTDIR)");
371 STATNODE_COUNTER(numfullpathfail2,
372 "Number of fullpath search errors (VOP_VPTOCNP failures)");
373 STATNODE_COUNTER(numfullpathfail4, "Number of fullpath search errors (ENOMEM)");
374 STATNODE_COUNTER(numfullpathfound, "Number of successful fullpath calls");
375 static long zap_and_exit_bucket_fail; STATNODE_ULONG(zap_and_exit_bucket_fail,
376 "Number of times zap_and_exit failed to lock");
377 static long cache_lock_vnodes_cel_3_failures;
378 STATNODE_ULONG(cache_lock_vnodes_cel_3_failures,
379 "Number of times 3-way vnode locking failed");
381 static void cache_zap_locked(struct namecache *ncp, bool neg_locked);
382 static int vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
383 char *buf, char **retbuf, u_int buflen);
385 static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
387 static int cache_yield;
388 SYSCTL_INT(_vfs_cache, OID_AUTO, yield, CTLFLAG_RD, &cache_yield, 0,
389 "Number of times cache called yield");
392 cache_maybe_yield(void)
395 if (should_yield()) {
397 kern_yield(PRI_USER);
402 cache_assert_vlp_locked(struct mtx *vlp)
406 mtx_assert(vlp, MA_OWNED);
410 cache_assert_vnode_locked(struct vnode *vp)
414 vlp = VP2VNODELOCK(vp);
415 cache_assert_vlp_locked(vlp);
419 cache_get_hash(char *name, u_char len, struct vnode *dvp)
423 hash = fnv_32_buf(name, len, FNV1_32_INIT);
424 hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
428 static inline struct rwlock *
429 NCP2BUCKETLOCK(struct namecache *ncp)
433 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen, ncp->nc_dvp);
434 return (HASH2BUCKETLOCK(hash));
439 cache_assert_bucket_locked(struct namecache *ncp, int mode)
443 blp = NCP2BUCKETLOCK(ncp);
444 rw_assert(blp, mode);
447 #define cache_assert_bucket_locked(x, y) do { } while (0)
450 #define cache_sort(x, y) _cache_sort((void **)(x), (void **)(y))
452 _cache_sort(void **p1, void **p2)
464 cache_lock_all_buckets(void)
468 for (i = 0; i < numbucketlocks; i++)
469 rw_wlock(&bucketlocks[i]);
473 cache_unlock_all_buckets(void)
477 for (i = 0; i < numbucketlocks; i++)
478 rw_wunlock(&bucketlocks[i]);
482 cache_lock_all_vnodes(void)
486 for (i = 0; i < numvnodelocks; i++)
487 mtx_lock(&vnodelocks[i]);
491 cache_unlock_all_vnodes(void)
495 for (i = 0; i < numvnodelocks; i++)
496 mtx_unlock(&vnodelocks[i]);
500 cache_trylock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
503 cache_sort(&vlp1, &vlp2);
507 if (!mtx_trylock(vlp1))
510 if (!mtx_trylock(vlp2)) {
520 cache_unlock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
523 MPASS(vlp1 != NULL || vlp2 != NULL);
532 sysctl_nchstats(SYSCTL_HANDLER_ARGS)
534 struct nchstats snap;
536 if (req->oldptr == NULL)
537 return (SYSCTL_OUT(req, 0, sizeof(snap)));
540 snap.ncs_goodhits = counter_u64_fetch(numposhits);
541 snap.ncs_neghits = counter_u64_fetch(numneghits);
542 snap.ncs_badhits = counter_u64_fetch(numposzaps) +
543 counter_u64_fetch(numnegzaps);
544 snap.ncs_miss = counter_u64_fetch(nummisszap) +
545 counter_u64_fetch(nummiss);
547 return (SYSCTL_OUT(req, &snap, sizeof(snap)));
549 SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE | CTLFLAG_RD |
550 CTLFLAG_MPSAFE, 0, 0, sysctl_nchstats, "LU",
551 "VFS cache effectiveness statistics");
555 * Grab an atomic snapshot of the name cache hash chain lengths
557 static SYSCTL_NODE(_debug, OID_AUTO, hashstat, CTLFLAG_RW, NULL,
561 sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS)
563 struct nchashhead *ncpp;
564 struct namecache *ncp;
565 int i, error, n_nchash, *cntbuf;
568 n_nchash = nchash + 1; /* nchash is max index, not count */
569 if (req->oldptr == NULL)
570 return SYSCTL_OUT(req, 0, n_nchash * sizeof(int));
571 cntbuf = malloc(n_nchash * sizeof(int), M_TEMP, M_ZERO | M_WAITOK);
572 cache_lock_all_buckets();
573 if (n_nchash != nchash + 1) {
574 cache_unlock_all_buckets();
575 free(cntbuf, M_TEMP);
578 /* Scan hash tables counting entries */
579 for (ncpp = nchashtbl, i = 0; i < n_nchash; ncpp++, i++)
580 LIST_FOREACH(ncp, ncpp, nc_hash)
582 cache_unlock_all_buckets();
583 for (error = 0, i = 0; i < n_nchash; i++)
584 if ((error = SYSCTL_OUT(req, &cntbuf[i], sizeof(int))) != 0)
586 free(cntbuf, M_TEMP);
589 SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD|
590 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_rawnchash, "S,int",
591 "nchash chain lengths");
594 sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS)
597 struct nchashhead *ncpp;
598 struct namecache *ncp;
600 int count, maxlength, used, pct;
603 return SYSCTL_OUT(req, 0, 4 * sizeof(int));
605 cache_lock_all_buckets();
606 n_nchash = nchash + 1; /* nchash is max index, not count */
610 /* Scan hash tables for applicable entries */
611 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
613 LIST_FOREACH(ncp, ncpp, nc_hash) {
618 if (maxlength < count)
621 n_nchash = nchash + 1;
622 cache_unlock_all_buckets();
623 pct = (used * 100) / (n_nchash / 100);
624 error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash));
627 error = SYSCTL_OUT(req, &used, sizeof(used));
630 error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength));
633 error = SYSCTL_OUT(req, &pct, sizeof(pct));
638 SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD|
639 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_nchash, "I",
640 "nchash statistics (number of total/used buckets, maximum chain length, usage percentage)");
644 * Negative entries management
646 * A variation of LRU scheme is used. New entries are hashed into one of
647 * numneglists cold lists. Entries get promoted to the hot list on first hit.
648 * Partial LRU for the hot list is maintained by requeueing them every
649 * ncneghitsrequeue hits.
651 * The shrinker will demote hot list head and evict from the cold list in a
652 * round-robin manner.
655 cache_negative_hit(struct namecache *ncp)
657 struct neglist *neglist;
660 MPASS(ncp->nc_flag & NCF_NEGATIVE);
661 hits = atomic_fetchadd_int(&ncp->nc_neghits, 1);
662 if (ncp->nc_flag & NCF_HOTNEGATIVE) {
663 if ((hits % ncneghitsrequeue) != 0)
665 mtx_lock(&ncneg_hot.nl_lock);
666 if (ncp->nc_flag & NCF_HOTNEGATIVE) {
667 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
668 TAILQ_INSERT_TAIL(&ncneg_hot.nl_list, ncp, nc_dst);
669 mtx_unlock(&ncneg_hot.nl_lock);
673 * The shrinker cleared the flag and removed the entry from
674 * the hot list. Put it back.
677 mtx_lock(&ncneg_hot.nl_lock);
679 neglist = NCP2NEGLIST(ncp);
680 mtx_lock(&neglist->nl_lock);
681 if (!(ncp->nc_flag & NCF_HOTNEGATIVE)) {
682 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
683 TAILQ_INSERT_TAIL(&ncneg_hot.nl_list, ncp, nc_dst);
684 ncp->nc_flag |= NCF_HOTNEGATIVE;
686 mtx_unlock(&neglist->nl_lock);
687 mtx_unlock(&ncneg_hot.nl_lock);
691 cache_negative_insert(struct namecache *ncp, bool neg_locked)
693 struct neglist *neglist;
695 MPASS(ncp->nc_flag & NCF_NEGATIVE);
696 cache_assert_bucket_locked(ncp, RA_WLOCKED);
697 neglist = NCP2NEGLIST(ncp);
699 mtx_lock(&neglist->nl_lock);
701 mtx_assert(&neglist->nl_lock, MA_OWNED);
703 TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst);
705 mtx_unlock(&neglist->nl_lock);
706 atomic_add_rel_long(&numneg, 1);
710 cache_negative_remove(struct namecache *ncp, bool neg_locked)
712 struct neglist *neglist;
713 bool hot_locked = false;
714 bool list_locked = false;
716 MPASS(ncp->nc_flag & NCF_NEGATIVE);
717 cache_assert_bucket_locked(ncp, RA_WLOCKED);
718 neglist = NCP2NEGLIST(ncp);
720 if (ncp->nc_flag & NCF_HOTNEGATIVE) {
722 mtx_lock(&ncneg_hot.nl_lock);
723 if (!(ncp->nc_flag & NCF_HOTNEGATIVE)) {
725 mtx_lock(&neglist->nl_lock);
729 mtx_lock(&neglist->nl_lock);
732 if (ncp->nc_flag & NCF_HOTNEGATIVE) {
733 mtx_assert(&ncneg_hot.nl_lock, MA_OWNED);
734 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
736 mtx_assert(&neglist->nl_lock, MA_OWNED);
737 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
740 mtx_unlock(&neglist->nl_lock);
742 mtx_unlock(&ncneg_hot.nl_lock);
743 atomic_subtract_rel_long(&numneg, 1);
747 cache_negative_shrink_select(int start, struct namecache **ncpp,
748 struct neglist **neglistpp)
750 struct neglist *neglist;
751 struct namecache *ncp;
756 for (i = start; i < numneglists; i++) {
757 neglist = &neglists[i];
758 if (TAILQ_FIRST(&neglist->nl_list) == NULL)
760 mtx_lock(&neglist->nl_lock);
761 ncp = TAILQ_FIRST(&neglist->nl_list);
764 mtx_unlock(&neglist->nl_lock);
767 *neglistpp = neglist;
772 cache_negative_zap_one(void)
774 struct namecache *ncp, *ncp2;
775 struct neglist *neglist;
779 if (!mtx_trylock(&ncneg_shrink_lock))
782 mtx_lock(&ncneg_hot.nl_lock);
783 ncp = TAILQ_FIRST(&ncneg_hot.nl_list);
785 neglist = NCP2NEGLIST(ncp);
786 mtx_lock(&neglist->nl_lock);
787 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
788 TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst);
789 ncp->nc_flag &= ~NCF_HOTNEGATIVE;
790 mtx_unlock(&neglist->nl_lock);
793 cache_negative_shrink_select(shrink_list_turn, &ncp, &neglist);
795 if (shrink_list_turn == numneglists)
796 shrink_list_turn = 0;
797 if (ncp == NULL && shrink_list_turn == 0)
798 cache_negative_shrink_select(shrink_list_turn, &ncp, &neglist);
800 mtx_unlock(&ncneg_hot.nl_lock);
804 MPASS(ncp->nc_flag & NCF_NEGATIVE);
805 dvlp = VP2VNODELOCK(ncp->nc_dvp);
806 blp = NCP2BUCKETLOCK(ncp);
807 mtx_unlock(&neglist->nl_lock);
808 mtx_unlock(&ncneg_hot.nl_lock);
811 mtx_lock(&neglist->nl_lock);
812 ncp2 = TAILQ_FIRST(&neglist->nl_list);
813 if (ncp != ncp2 || dvlp != VP2VNODELOCK(ncp2->nc_dvp) ||
814 blp != NCP2BUCKETLOCK(ncp2) || !(ncp2->nc_flag & NCF_NEGATIVE)) {
818 SDT_PROBE3(vfs, namecache, shrink_negative, done, ncp->nc_dvp,
819 ncp->nc_name, ncp->nc_neghits);
821 cache_zap_locked(ncp, true);
823 mtx_unlock(&neglist->nl_lock);
827 mtx_unlock(&ncneg_shrink_lock);
832 * cache_zap_locked():
834 * Removes a namecache entry from cache, whether it contains an actual
835 * pointer to a vnode or if it is just a negative cache entry.
838 cache_zap_locked(struct namecache *ncp, bool neg_locked)
841 if (!(ncp->nc_flag & NCF_NEGATIVE))
842 cache_assert_vnode_locked(ncp->nc_vp);
843 cache_assert_vnode_locked(ncp->nc_dvp);
844 cache_assert_bucket_locked(ncp, RA_WLOCKED);
846 CTR2(KTR_VFS, "cache_zap(%p) vp %p", ncp,
847 (ncp->nc_flag & NCF_NEGATIVE) ? NULL : ncp->nc_vp);
848 if (!(ncp->nc_flag & NCF_NEGATIVE)) {
849 SDT_PROBE3(vfs, namecache, zap, done, ncp->nc_dvp,
850 ncp->nc_name, ncp->nc_vp);
852 SDT_PROBE3(vfs, namecache, zap_negative, done, ncp->nc_dvp,
853 ncp->nc_name, ncp->nc_neghits);
855 LIST_REMOVE(ncp, nc_hash);
856 if (!(ncp->nc_flag & NCF_NEGATIVE)) {
857 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
858 if (ncp == ncp->nc_vp->v_cache_dd)
859 ncp->nc_vp->v_cache_dd = NULL;
861 cache_negative_remove(ncp, neg_locked);
863 if (ncp->nc_flag & NCF_ISDOTDOT) {
864 if (ncp == ncp->nc_dvp->v_cache_dd)
865 ncp->nc_dvp->v_cache_dd = NULL;
867 LIST_REMOVE(ncp, nc_src);
868 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) {
869 ncp->nc_flag |= NCF_DVDROP;
870 atomic_subtract_rel_long(&numcachehv, 1);
873 atomic_subtract_rel_long(&numcache, 1);
877 cache_zap_negative_locked_vnode_kl(struct namecache *ncp, struct vnode *vp)
881 MPASS(ncp->nc_dvp == vp);
882 MPASS(ncp->nc_flag & NCF_NEGATIVE);
883 cache_assert_vnode_locked(vp);
885 blp = NCP2BUCKETLOCK(ncp);
887 cache_zap_locked(ncp, false);
892 cache_zap_locked_vnode_kl2(struct namecache *ncp, struct vnode *vp,
895 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
898 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
899 cache_assert_vnode_locked(vp);
901 if (ncp->nc_flag & NCF_NEGATIVE) {
906 cache_zap_negative_locked_vnode_kl(ncp, vp);
910 pvlp = VP2VNODELOCK(vp);
911 blp = NCP2BUCKETLOCK(ncp);
912 vlp1 = VP2VNODELOCK(ncp->nc_dvp);
913 vlp2 = VP2VNODELOCK(ncp->nc_vp);
915 if (*vlpp == vlp1 || *vlpp == vlp2) {
923 cache_sort(&vlp1, &vlp2);
928 if (!mtx_trylock(vlp1))
934 cache_zap_locked(ncp, false);
936 if (to_unlock != NULL)
937 mtx_unlock(to_unlock);
944 MPASS(*vlpp == NULL);
950 cache_zap_locked_vnode(struct namecache *ncp, struct vnode *vp)
952 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
956 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
957 cache_assert_vnode_locked(vp);
959 pvlp = VP2VNODELOCK(vp);
960 if (ncp->nc_flag & NCF_NEGATIVE) {
961 cache_zap_negative_locked_vnode_kl(ncp, vp);
965 blp = NCP2BUCKETLOCK(ncp);
966 vlp1 = VP2VNODELOCK(ncp->nc_dvp);
967 vlp2 = VP2VNODELOCK(ncp->nc_vp);
968 cache_sort(&vlp1, &vlp2);
973 if (!mtx_trylock(vlp1)) {
980 cache_zap_locked(ncp, false);
982 mtx_unlock(to_unlock);
989 cache_zap_wlocked_bucket(struct namecache *ncp, struct rwlock *blp)
991 struct mtx *dvlp, *vlp;
993 cache_assert_bucket_locked(ncp, RA_WLOCKED);
995 dvlp = VP2VNODELOCK(ncp->nc_dvp);
997 if (!(ncp->nc_flag & NCF_NEGATIVE))
998 vlp = VP2VNODELOCK(ncp->nc_vp);
999 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1000 cache_zap_locked(ncp, false);
1002 cache_unlock_vnodes(dvlp, vlp);
1011 cache_zap_rlocked_bucket(struct namecache *ncp, struct rwlock *blp)
1013 struct mtx *dvlp, *vlp;
1015 cache_assert_bucket_locked(ncp, RA_RLOCKED);
1017 dvlp = VP2VNODELOCK(ncp->nc_dvp);
1019 if (!(ncp->nc_flag & NCF_NEGATIVE))
1020 vlp = VP2VNODELOCK(ncp->nc_vp);
1021 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1024 cache_zap_locked(ncp, false);
1026 cache_unlock_vnodes(dvlp, vlp);
1035 cache_zap_wlocked_bucket_kl(struct namecache *ncp, struct rwlock *blp,
1036 struct mtx **vlpp1, struct mtx **vlpp2)
1038 struct mtx *dvlp, *vlp;
1040 cache_assert_bucket_locked(ncp, RA_WLOCKED);
1042 dvlp = VP2VNODELOCK(ncp->nc_dvp);
1044 if (!(ncp->nc_flag & NCF_NEGATIVE))
1045 vlp = VP2VNODELOCK(ncp->nc_vp);
1046 cache_sort(&dvlp, &vlp);
1048 if (*vlpp1 == dvlp && *vlpp2 == vlp) {
1049 cache_zap_locked(ncp, false);
1050 cache_unlock_vnodes(dvlp, vlp);
1063 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1064 cache_zap_locked(ncp, false);
1065 cache_unlock_vnodes(dvlp, vlp);
1080 cache_lookup_unlock(struct rwlock *blp, struct mtx *vlp)
1090 static int __noinline
1091 cache_lookup_dot(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1092 struct timespec *tsp, int *ticksp)
1097 CTR2(KTR_VFS, "cache_lookup(%p, %s) found via .",
1098 dvp, cnp->cn_nameptr);
1099 counter_u64_add(dothits, 1);
1100 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", *vpp);
1107 * When we lookup "." we still can be asked to lock it
1110 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
1111 if (ltype != VOP_ISLOCKED(*vpp)) {
1112 if (ltype == LK_EXCLUSIVE) {
1113 vn_lock(*vpp, LK_UPGRADE | LK_RETRY);
1114 if ((*vpp)->v_iflag & VI_DOOMED) {
1115 /* forced unmount */
1121 vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY);
1127 * Lookup an entry in the cache
1129 * Lookup is called with dvp pointing to the directory to search,
1130 * cnp pointing to the name of the entry being sought. If the lookup
1131 * succeeds, the vnode is returned in *vpp, and a status of -1 is
1132 * returned. If the lookup determines that the name does not exist
1133 * (negative caching), a status of ENOENT is returned. If the lookup
1134 * fails, a status of zero is returned. If the directory vnode is
1135 * recycled out from under us due to a forced unmount, a status of
1136 * ENOENT is returned.
1138 * vpp is locked and ref'd on return. If we're looking up DOTDOT, dvp is
1139 * unlocked. If we're looking up . an extra ref is taken, but the lock is
1140 * not recursively acquired.
1143 static __noinline int
1144 cache_lookup_nomakeentry(struct vnode *dvp, struct vnode **vpp,
1145 struct componentname *cnp, struct timespec *tsp, int *ticksp)
1147 struct namecache *ncp;
1149 struct mtx *dvlp, *dvlp2;
1153 if (cnp->cn_namelen == 2 &&
1154 cnp->cn_nameptr[0] == '.' && cnp->cn_nameptr[1] == '.') {
1155 counter_u64_add(dotdothits, 1);
1156 dvlp = VP2VNODELOCK(dvp);
1160 ncp = dvp->v_cache_dd;
1162 SDT_PROBE3(vfs, namecache, lookup, miss, dvp,
1169 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1170 if (ncp->nc_dvp != dvp)
1171 panic("dvp %p v_cache_dd %p\n", dvp, ncp);
1172 if (!cache_zap_locked_vnode_kl2(ncp,
1175 MPASS(dvp->v_cache_dd == NULL);
1181 dvp->v_cache_dd = NULL;
1189 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1190 blp = HASH2BUCKETLOCK(hash);
1192 if (LIST_EMPTY(NCHHASH(hash)))
1197 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1198 counter_u64_add(numchecks, 1);
1199 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1200 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1204 /* We failed to find an entry */
1210 counter_u64_add(numposzaps, 1);
1212 error = cache_zap_wlocked_bucket(ncp, blp);
1214 zap_and_exit_bucket_fail++;
1215 cache_maybe_yield();
1221 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr, NULL);
1222 counter_u64_add(nummisszap, 1);
1227 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1228 struct timespec *tsp, int *ticksp)
1230 struct namecache_ts *ncp_ts;
1231 struct namecache *ncp;
1233 struct mtx *dvlp, *dvlp2;
1237 if (__predict_false(!doingcache)) {
1238 cnp->cn_flags &= ~MAKEENTRY;
1242 counter_u64_add(numcalls, 1);
1244 if (__predict_false(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.'))
1245 return (cache_lookup_dot(dvp, vpp, cnp, tsp, ticksp));
1247 if ((cnp->cn_flags & MAKEENTRY) == 0)
1248 return (cache_lookup_nomakeentry(dvp, vpp, cnp, tsp, ticksp));
1253 if (cnp->cn_namelen == 2 &&
1254 cnp->cn_nameptr[0] == '.' && cnp->cn_nameptr[1] == '.') {
1255 counter_u64_add(dotdothits, 1);
1256 dvlp = VP2VNODELOCK(dvp);
1259 ncp = dvp->v_cache_dd;
1261 SDT_PROBE3(vfs, namecache, lookup, miss, dvp,
1266 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1267 if (ncp->nc_flag & NCF_NEGATIVE)
1273 /* Return failure if negative entry was found. */
1275 goto negative_success;
1276 CTR3(KTR_VFS, "cache_lookup(%p, %s) found %p via ..",
1277 dvp, cnp->cn_nameptr, *vpp);
1278 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, "..",
1280 cache_out_ts(ncp, tsp, ticksp);
1281 if ((ncp->nc_flag & (NCF_ISDOTDOT | NCF_DTS)) ==
1282 NCF_DTS && tsp != NULL) {
1283 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1284 *tsp = ncp_ts->nc_dotdottime;
1289 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1290 blp = HASH2BUCKETLOCK(hash);
1293 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1294 counter_u64_add(numchecks, 1);
1295 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1296 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1300 /* We failed to find an entry */
1303 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
1305 counter_u64_add(nummiss, 1);
1309 /* We found a "positive" match, return the vnode */
1310 if (!(ncp->nc_flag & NCF_NEGATIVE)) {
1311 counter_u64_add(numposhits, 1);
1313 CTR4(KTR_VFS, "cache_lookup(%p, %s) found %p via ncp %p",
1314 dvp, cnp->cn_nameptr, *vpp, ncp);
1315 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name,
1317 cache_out_ts(ncp, tsp, ticksp);
1322 /* We found a negative match, and want to create it, so purge */
1323 if (cnp->cn_nameiop == CREATE) {
1324 counter_u64_add(numnegzaps, 1);
1328 counter_u64_add(numneghits, 1);
1329 cache_negative_hit(ncp);
1330 if (ncp->nc_flag & NCF_WHITE)
1331 cnp->cn_flags |= ISWHITEOUT;
1332 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp,
1334 cache_out_ts(ncp, tsp, ticksp);
1335 cache_lookup_unlock(blp, dvlp);
1340 * On success we return a locked and ref'd vnode as per the lookup
1344 ltype = 0; /* silence gcc warning */
1345 if (cnp->cn_flags & ISDOTDOT) {
1346 ltype = VOP_ISLOCKED(dvp);
1350 cache_lookup_unlock(blp, dvlp);
1351 error = vget(*vpp, cnp->cn_lkflags | LK_VNHELD, cnp->cn_thread);
1352 if (cnp->cn_flags & ISDOTDOT) {
1353 vn_lock(dvp, ltype | LK_RETRY);
1354 if (dvp->v_iflag & VI_DOOMED) {
1365 if ((cnp->cn_flags & ISLASTCN) &&
1366 (cnp->cn_lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE) {
1367 ASSERT_VOP_ELOCKED(*vpp, "cache_lookup");
1373 error = cache_zap_rlocked_bucket(ncp, blp);
1375 error = cache_zap_locked_vnode(ncp, dvp);
1377 zap_and_exit_bucket_fail++;
1378 cache_maybe_yield();
1385 struct celockstate {
1387 struct rwlock *blp[2];
1389 CTASSERT((nitems(((struct celockstate *)0)->vlp) == 3));
1390 CTASSERT((nitems(((struct celockstate *)0)->blp) == 2));
1393 cache_celockstate_init(struct celockstate *cel)
1396 bzero(cel, sizeof(*cel));
1400 cache_lock_vnodes_cel(struct celockstate *cel, struct vnode *vp,
1403 struct mtx *vlp1, *vlp2;
1405 MPASS(cel->vlp[0] == NULL);
1406 MPASS(cel->vlp[1] == NULL);
1407 MPASS(cel->vlp[2] == NULL);
1409 MPASS(vp != NULL || dvp != NULL);
1411 vlp1 = VP2VNODELOCK(vp);
1412 vlp2 = VP2VNODELOCK(dvp);
1413 cache_sort(&vlp1, &vlp2);
1424 cache_unlock_vnodes_cel(struct celockstate *cel)
1427 MPASS(cel->vlp[0] != NULL || cel->vlp[1] != NULL);
1429 if (cel->vlp[0] != NULL)
1430 mtx_unlock(cel->vlp[0]);
1431 if (cel->vlp[1] != NULL)
1432 mtx_unlock(cel->vlp[1]);
1433 if (cel->vlp[2] != NULL)
1434 mtx_unlock(cel->vlp[2]);
1438 cache_lock_vnodes_cel_3(struct celockstate *cel, struct vnode *vp)
1443 cache_assert_vlp_locked(cel->vlp[0]);
1444 cache_assert_vlp_locked(cel->vlp[1]);
1445 MPASS(cel->vlp[2] == NULL);
1448 vlp = VP2VNODELOCK(vp);
1451 if (vlp >= cel->vlp[1]) {
1454 if (mtx_trylock(vlp))
1456 cache_lock_vnodes_cel_3_failures++;
1457 cache_unlock_vnodes_cel(cel);
1458 if (vlp < cel->vlp[0]) {
1460 mtx_lock(cel->vlp[0]);
1461 mtx_lock(cel->vlp[1]);
1463 if (cel->vlp[0] != NULL)
1464 mtx_lock(cel->vlp[0]);
1466 mtx_lock(cel->vlp[1]);
1476 cache_lock_buckets_cel(struct celockstate *cel, struct rwlock *blp1,
1477 struct rwlock *blp2)
1480 MPASS(cel->blp[0] == NULL);
1481 MPASS(cel->blp[1] == NULL);
1483 cache_sort(&blp1, &blp2);
1494 cache_unlock_buckets_cel(struct celockstate *cel)
1497 if (cel->blp[0] != NULL)
1498 rw_wunlock(cel->blp[0]);
1499 rw_wunlock(cel->blp[1]);
1503 * Lock part of the cache affected by the insertion.
1505 * This means vnodelocks for dvp, vp and the relevant bucketlock.
1506 * However, insertion can result in removal of an old entry. In this
1507 * case we have an additional vnode and bucketlock pair to lock. If the
1508 * entry is negative, ncelock is locked instead of the vnode.
1510 * That is, in the worst case we have to lock 3 vnodes and 2 bucketlocks, while
1511 * preserving the locking order (smaller address first).
1514 cache_enter_lock(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1517 struct namecache *ncp;
1518 struct rwlock *blps[2];
1520 blps[0] = HASH2BUCKETLOCK(hash);
1523 cache_lock_vnodes_cel(cel, dvp, vp);
1524 if (vp == NULL || vp->v_type != VDIR)
1526 ncp = vp->v_cache_dd;
1529 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1531 MPASS(ncp->nc_dvp == vp);
1532 blps[1] = NCP2BUCKETLOCK(ncp);
1533 if (ncp->nc_flag & NCF_NEGATIVE)
1535 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1538 * All vnodes got re-locked. Re-validate the state and if
1539 * nothing changed we are done. Otherwise restart.
1541 if (ncp == vp->v_cache_dd &&
1542 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1543 blps[1] == NCP2BUCKETLOCK(ncp) &&
1544 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1546 cache_unlock_vnodes_cel(cel);
1551 cache_lock_buckets_cel(cel, blps[0], blps[1]);
1555 cache_enter_lock_dd(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1558 struct namecache *ncp;
1559 struct rwlock *blps[2];
1561 blps[0] = HASH2BUCKETLOCK(hash);
1564 cache_lock_vnodes_cel(cel, dvp, vp);
1565 ncp = dvp->v_cache_dd;
1568 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1570 MPASS(ncp->nc_dvp == dvp);
1571 blps[1] = NCP2BUCKETLOCK(ncp);
1572 if (ncp->nc_flag & NCF_NEGATIVE)
1574 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1576 if (ncp == dvp->v_cache_dd &&
1577 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1578 blps[1] == NCP2BUCKETLOCK(ncp) &&
1579 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1581 cache_unlock_vnodes_cel(cel);
1586 cache_lock_buckets_cel(cel, blps[0], blps[1]);
1590 cache_enter_unlock(struct celockstate *cel)
1593 cache_unlock_buckets_cel(cel);
1594 cache_unlock_vnodes_cel(cel);
1598 * Add an entry to the cache.
1601 cache_enter_time(struct vnode *dvp, struct vnode *vp, struct componentname *cnp,
1602 struct timespec *tsp, struct timespec *dtsp)
1604 struct celockstate cel;
1605 struct namecache *ncp, *n2, *ndd;
1606 struct namecache_ts *ncp_ts, *n2_ts;
1607 struct nchashhead *ncpp;
1608 struct neglist *neglist;
1615 CTR3(KTR_VFS, "cache_enter(%p, %p, %s)", dvp, vp, cnp->cn_nameptr);
1616 VNASSERT(vp == NULL || (vp->v_iflag & VI_DOOMED) == 0, vp,
1617 ("cache_enter: Adding a doomed vnode"));
1618 VNASSERT(dvp == NULL || (dvp->v_iflag & VI_DOOMED) == 0, dvp,
1619 ("cache_enter: Doomed vnode used as src"));
1621 if (__predict_false(!doingcache))
1625 * Avoid blowout in namecache entries.
1627 if (__predict_false(numcache >= desiredvnodes * ncsizefactor))
1630 cache_celockstate_init(&cel);
1633 if (cnp->cn_nameptr[0] == '.') {
1634 if (cnp->cn_namelen == 1)
1636 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
1637 len = cnp->cn_namelen;
1638 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1639 cache_enter_lock_dd(&cel, dvp, vp, hash);
1641 * If dotdot entry already exists, just retarget it
1642 * to new parent vnode, otherwise continue with new
1643 * namecache entry allocation.
1645 if ((ncp = dvp->v_cache_dd) != NULL &&
1646 ncp->nc_flag & NCF_ISDOTDOT) {
1647 KASSERT(ncp->nc_dvp == dvp,
1648 ("wrong isdotdot parent"));
1650 if (ncp->nc_flag & NCF_NEGATIVE || vp == NULL) {
1651 neglist = NCP2NEGLIST(ncp);
1652 mtx_lock(&ncneg_hot.nl_lock);
1653 mtx_lock(&neglist->nl_lock);
1656 if (!(ncp->nc_flag & NCF_NEGATIVE)) {
1657 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst,
1660 cache_negative_remove(ncp, true);
1663 TAILQ_INSERT_HEAD(&vp->v_cache_dst,
1665 ncp->nc_flag &= ~(NCF_NEGATIVE|NCF_HOTNEGATIVE);
1667 ncp->nc_flag &= ~(NCF_HOTNEGATIVE);
1668 ncp->nc_flag |= NCF_NEGATIVE;
1669 cache_negative_insert(ncp, true);
1672 mtx_unlock(&neglist->nl_lock);
1673 mtx_unlock(&ncneg_hot.nl_lock);
1676 cache_enter_unlock(&cel);
1679 dvp->v_cache_dd = NULL;
1680 cache_enter_unlock(&cel);
1681 cache_celockstate_init(&cel);
1682 SDT_PROBE3(vfs, namecache, enter, done, dvp, "..", vp);
1683 flag = NCF_ISDOTDOT;
1688 * Calculate the hash key and setup as much of the new
1689 * namecache entry as possible before acquiring the lock.
1691 ncp = cache_alloc(cnp->cn_namelen, tsp != NULL);
1692 ncp->nc_flag = flag;
1695 ncp->nc_flag |= NCF_NEGATIVE;
1698 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1699 ncp_ts->nc_time = *tsp;
1700 ncp_ts->nc_ticks = ticks;
1701 ncp_ts->nc_nc.nc_flag |= NCF_TS;
1703 ncp_ts->nc_dotdottime = *dtsp;
1704 ncp_ts->nc_nc.nc_flag |= NCF_DTS;
1707 len = ncp->nc_nlen = cnp->cn_namelen;
1708 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1709 strlcpy(ncp->nc_name, cnp->cn_nameptr, len + 1);
1710 cache_enter_lock(&cel, dvp, vp, hash);
1713 * See if this vnode or negative entry is already in the cache
1714 * with this name. This can happen with concurrent lookups of
1715 * the same path name.
1717 ncpp = NCHHASH(hash);
1718 LIST_FOREACH(n2, ncpp, nc_hash) {
1719 if (n2->nc_dvp == dvp &&
1720 n2->nc_nlen == cnp->cn_namelen &&
1721 !bcmp(n2->nc_name, cnp->cn_nameptr, n2->nc_nlen)) {
1723 KASSERT((n2->nc_flag & NCF_TS) != 0,
1725 n2_ts = __containerof(n2, struct namecache_ts, nc_nc);
1726 n2_ts->nc_time = ncp_ts->nc_time;
1727 n2_ts->nc_ticks = ncp_ts->nc_ticks;
1729 n2_ts->nc_dotdottime = ncp_ts->nc_dotdottime;
1730 if (ncp->nc_flag & NCF_NEGATIVE)
1731 mtx_lock(&ncneg_hot.nl_lock);
1732 n2_ts->nc_nc.nc_flag |= NCF_DTS;
1733 if (ncp->nc_flag & NCF_NEGATIVE)
1734 mtx_unlock(&ncneg_hot.nl_lock);
1737 goto out_unlock_free;
1741 if (flag == NCF_ISDOTDOT) {
1743 * See if we are trying to add .. entry, but some other lookup
1744 * has populated v_cache_dd pointer already.
1746 if (dvp->v_cache_dd != NULL)
1747 goto out_unlock_free;
1748 KASSERT(vp == NULL || vp->v_type == VDIR,
1749 ("wrong vnode type %p", vp));
1750 dvp->v_cache_dd = ncp;
1754 if (vp->v_type == VDIR) {
1755 if (flag != NCF_ISDOTDOT) {
1757 * For this case, the cache entry maps both the
1758 * directory name in it and the name ".." for the
1759 * directory's parent.
1761 if ((ndd = vp->v_cache_dd) != NULL) {
1762 if ((ndd->nc_flag & NCF_ISDOTDOT) != 0)
1763 cache_zap_locked(ndd, false);
1767 vp->v_cache_dd = ncp;
1770 vp->v_cache_dd = NULL;
1774 if (flag != NCF_ISDOTDOT) {
1775 if (LIST_EMPTY(&dvp->v_cache_src)) {
1777 atomic_add_rel_long(&numcachehv, 1);
1779 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
1783 * Insert the new namecache entry into the appropriate chain
1784 * within the cache entries table.
1786 LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
1789 * If the entry is "negative", we place it into the
1790 * "negative" cache queue, otherwise, we place it into the
1791 * destination vnode's cache entries queue.
1794 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
1795 SDT_PROBE3(vfs, namecache, enter, done, dvp, ncp->nc_name,
1798 if (cnp->cn_flags & ISWHITEOUT)
1799 ncp->nc_flag |= NCF_WHITE;
1800 cache_negative_insert(ncp, false);
1801 SDT_PROBE2(vfs, namecache, enter_negative, done, dvp,
1804 cache_enter_unlock(&cel);
1805 lnumcache = atomic_fetchadd_long(&numcache, 1) + 1;
1806 if (numneg * ncnegfactor > lnumcache)
1807 cache_negative_zap_one();
1811 cache_enter_unlock(&cel);
1817 cache_roundup_2(u_int val)
1821 for (res = 1; res <= val; res <<= 1)
1828 * Name cache initialization, from vfs_init() when we are booting
1831 nchinit(void *dummy __unused)
1835 cache_zone_small = uma_zcreate("S VFS Cache",
1836 sizeof(struct namecache) + CACHE_PATH_CUTOFF + 1,
1837 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache),
1839 cache_zone_small_ts = uma_zcreate("STS VFS Cache",
1840 sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1,
1841 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache_ts),
1843 cache_zone_large = uma_zcreate("L VFS Cache",
1844 sizeof(struct namecache) + NAME_MAX + 1,
1845 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache),
1847 cache_zone_large_ts = uma_zcreate("LTS VFS Cache",
1848 sizeof(struct namecache_ts) + NAME_MAX + 1,
1849 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache_ts),
1852 nchashtbl = hashinit(desiredvnodes * 2, M_VFSCACHE, &nchash);
1853 ncbuckethash = cache_roundup_2(mp_ncpus * 64) - 1;
1854 if (ncbuckethash > nchash)
1855 ncbuckethash = nchash;
1856 bucketlocks = malloc(sizeof(*bucketlocks) * numbucketlocks, M_VFSCACHE,
1858 for (i = 0; i < numbucketlocks; i++)
1859 rw_init_flags(&bucketlocks[i], "ncbuc", RW_DUPOK | RW_RECURSE);
1860 ncvnodehash = cache_roundup_2(mp_ncpus * 64) - 1;
1861 vnodelocks = malloc(sizeof(*vnodelocks) * numvnodelocks, M_VFSCACHE,
1863 for (i = 0; i < numvnodelocks; i++)
1864 mtx_init(&vnodelocks[i], "ncvn", NULL, MTX_DUPOK | MTX_RECURSE);
1865 ncpurgeminvnodes = numbucketlocks;
1868 neglists = malloc(sizeof(*neglists) * numneglists, M_VFSCACHE,
1870 for (i = 0; i < numneglists; i++) {
1871 mtx_init(&neglists[i].nl_lock, "ncnegl", NULL, MTX_DEF);
1872 TAILQ_INIT(&neglists[i].nl_list);
1874 mtx_init(&ncneg_hot.nl_lock, "ncneglh", NULL, MTX_DEF);
1875 TAILQ_INIT(&ncneg_hot.nl_list);
1877 mtx_init(&ncneg_shrink_lock, "ncnegs", NULL, MTX_DEF);
1879 numcalls = counter_u64_alloc(M_WAITOK);
1880 dothits = counter_u64_alloc(M_WAITOK);
1881 dotdothits = counter_u64_alloc(M_WAITOK);
1882 numchecks = counter_u64_alloc(M_WAITOK);
1883 nummiss = counter_u64_alloc(M_WAITOK);
1884 nummisszap = counter_u64_alloc(M_WAITOK);
1885 numposzaps = counter_u64_alloc(M_WAITOK);
1886 numposhits = counter_u64_alloc(M_WAITOK);
1887 numnegzaps = counter_u64_alloc(M_WAITOK);
1888 numneghits = counter_u64_alloc(M_WAITOK);
1889 numfullpathcalls = counter_u64_alloc(M_WAITOK);
1890 numfullpathfail1 = counter_u64_alloc(M_WAITOK);
1891 numfullpathfail2 = counter_u64_alloc(M_WAITOK);
1892 numfullpathfail4 = counter_u64_alloc(M_WAITOK);
1893 numfullpathfound = counter_u64_alloc(M_WAITOK);
1895 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL);
1898 cache_changesize(int newmaxvnodes)
1900 struct nchashhead *new_nchashtbl, *old_nchashtbl;
1901 u_long new_nchash, old_nchash;
1902 struct namecache *ncp;
1906 newmaxvnodes = cache_roundup_2(newmaxvnodes * 2);
1907 if (newmaxvnodes < numbucketlocks)
1908 newmaxvnodes = numbucketlocks;
1910 new_nchashtbl = hashinit(newmaxvnodes, M_VFSCACHE, &new_nchash);
1911 /* If same hash table size, nothing to do */
1912 if (nchash == new_nchash) {
1913 free(new_nchashtbl, M_VFSCACHE);
1917 * Move everything from the old hash table to the new table.
1918 * None of the namecache entries in the table can be removed
1919 * because to do so, they have to be removed from the hash table.
1921 cache_lock_all_vnodes();
1922 cache_lock_all_buckets();
1923 old_nchashtbl = nchashtbl;
1924 old_nchash = nchash;
1925 nchashtbl = new_nchashtbl;
1926 nchash = new_nchash;
1927 for (i = 0; i <= old_nchash; i++) {
1928 while ((ncp = LIST_FIRST(&old_nchashtbl[i])) != NULL) {
1929 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen,
1931 LIST_REMOVE(ncp, nc_hash);
1932 LIST_INSERT_HEAD(NCHHASH(hash), ncp, nc_hash);
1935 cache_unlock_all_buckets();
1936 cache_unlock_all_vnodes();
1937 free(old_nchashtbl, M_VFSCACHE);
1941 * Invalidate all entries to a particular vnode.
1944 cache_purge(struct vnode *vp)
1946 TAILQ_HEAD(, namecache) ncps;
1947 struct namecache *ncp, *nnp;
1948 struct mtx *vlp, *vlp2;
1950 CTR1(KTR_VFS, "cache_purge(%p)", vp);
1951 SDT_PROBE1(vfs, namecache, purge, done, vp);
1952 if (LIST_EMPTY(&vp->v_cache_src) && TAILQ_EMPTY(&vp->v_cache_dst) &&
1953 vp->v_cache_dd == NULL)
1956 vlp = VP2VNODELOCK(vp);
1960 while (!LIST_EMPTY(&vp->v_cache_src)) {
1961 ncp = LIST_FIRST(&vp->v_cache_src);
1962 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
1964 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
1966 while (!TAILQ_EMPTY(&vp->v_cache_dst)) {
1967 ncp = TAILQ_FIRST(&vp->v_cache_dst);
1968 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
1970 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
1972 ncp = vp->v_cache_dd;
1974 KASSERT(ncp->nc_flag & NCF_ISDOTDOT,
1975 ("lost dotdot link"));
1976 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
1978 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
1980 KASSERT(vp->v_cache_dd == NULL, ("incomplete purge"));
1984 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
1990 * Invalidate all negative entries for a particular directory vnode.
1993 cache_purge_negative(struct vnode *vp)
1995 TAILQ_HEAD(, namecache) ncps;
1996 struct namecache *ncp, *nnp;
1999 CTR1(KTR_VFS, "cache_purge_negative(%p)", vp);
2000 SDT_PROBE1(vfs, namecache, purge_negative, done, vp);
2001 if (LIST_EMPTY(&vp->v_cache_src))
2004 vlp = VP2VNODELOCK(vp);
2006 LIST_FOREACH_SAFE(ncp, &vp->v_cache_src, nc_src, nnp) {
2007 if (!(ncp->nc_flag & NCF_NEGATIVE))
2009 cache_zap_negative_locked_vnode_kl(ncp, vp);
2010 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2013 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2019 * Flush all entries referencing a particular filesystem.
2022 cache_purgevfs(struct mount *mp, bool force)
2024 TAILQ_HEAD(, namecache) ncps;
2025 struct mtx *vlp1, *vlp2;
2027 struct nchashhead *bucket;
2028 struct namecache *ncp, *nnp;
2029 u_long i, j, n_nchash;
2032 /* Scan hash tables for applicable entries */
2033 SDT_PROBE1(vfs, namecache, purgevfs, done, mp);
2034 if (!force && mp->mnt_nvnodelistsize <= ncpurgeminvnodes)
2037 n_nchash = nchash + 1;
2039 for (i = 0; i < numbucketlocks; i++) {
2040 blp = (struct rwlock *)&bucketlocks[i];
2042 for (j = i; j < n_nchash; j += numbucketlocks) {
2044 bucket = &nchashtbl[j];
2045 LIST_FOREACH_SAFE(ncp, bucket, nc_hash, nnp) {
2046 cache_assert_bucket_locked(ncp, RA_WLOCKED);
2047 if (ncp->nc_dvp->v_mount != mp)
2049 error = cache_zap_wlocked_bucket_kl(ncp, blp,
2053 TAILQ_INSERT_HEAD(&ncps, ncp, nc_dst);
2057 if (vlp1 == NULL && vlp2 == NULL)
2058 cache_maybe_yield();
2065 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2071 * Perform canonical checks and cache lookup and pass on to filesystem
2072 * through the vop_cachedlookup only if needed.
2076 vfs_cache_lookup(struct vop_lookup_args *ap)
2080 struct vnode **vpp = ap->a_vpp;
2081 struct componentname *cnp = ap->a_cnp;
2082 struct ucred *cred = cnp->cn_cred;
2083 int flags = cnp->cn_flags;
2084 struct thread *td = cnp->cn_thread;
2089 if (dvp->v_type != VDIR)
2092 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
2093 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
2096 error = VOP_ACCESS(dvp, VEXEC, cred, td);
2100 error = cache_lookup(dvp, vpp, cnp, NULL, NULL);
2102 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
2109 * XXX All of these sysctls would probably be more productive dead.
2111 static int __read_mostly disablecwd;
2112 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0,
2113 "Disable the getcwd syscall");
2115 /* Implementation of the getcwd syscall. */
2117 sys___getcwd(struct thread *td, struct __getcwd_args *uap)
2120 return (kern___getcwd(td, uap->buf, UIO_USERSPACE, uap->buflen,
2125 kern___getcwd(struct thread *td, char *buf, enum uio_seg bufseg, size_t buflen,
2129 struct filedesc *fdp;
2130 struct vnode *cdir, *rdir;
2133 if (__predict_false(disablecwd))
2135 if (__predict_false(buflen < 2))
2137 if (buflen > path_max)
2140 tmpbuf = malloc(buflen, M_TEMP, M_WAITOK);
2141 fdp = td->td_proc->p_fd;
2142 FILEDESC_SLOCK(fdp);
2143 cdir = fdp->fd_cdir;
2145 rdir = fdp->fd_rdir;
2147 FILEDESC_SUNLOCK(fdp);
2148 error = vn_fullpath1(td, cdir, rdir, tmpbuf, &bp, buflen);
2153 if (bufseg == UIO_SYSSPACE)
2154 bcopy(bp, buf, strlen(bp) + 1);
2156 error = copyout(bp, buf, strlen(bp) + 1);
2158 if (KTRPOINT(curthread, KTR_NAMEI))
2162 free(tmpbuf, M_TEMP);
2167 * Thus begins the fullpath magic.
2170 static int __read_mostly disablefullpath;
2171 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW, &disablefullpath, 0,
2172 "Disable the vn_fullpath function");
2175 * Retrieve the full filesystem path that correspond to a vnode from the name
2176 * cache (if available)
2179 vn_fullpath(struct thread *td, struct vnode *vn, char **retbuf, char **freebuf)
2182 struct filedesc *fdp;
2186 if (__predict_false(disablefullpath))
2188 if (__predict_false(vn == NULL))
2191 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
2192 fdp = td->td_proc->p_fd;
2193 FILEDESC_SLOCK(fdp);
2194 rdir = fdp->fd_rdir;
2196 FILEDESC_SUNLOCK(fdp);
2197 error = vn_fullpath1(td, vn, rdir, buf, retbuf, MAXPATHLEN);
2208 * This function is similar to vn_fullpath, but it attempts to lookup the
2209 * pathname relative to the global root mount point. This is required for the
2210 * auditing sub-system, as audited pathnames must be absolute, relative to the
2211 * global root mount point.
2214 vn_fullpath_global(struct thread *td, struct vnode *vn,
2215 char **retbuf, char **freebuf)
2220 if (__predict_false(disablefullpath))
2222 if (__predict_false(vn == NULL))
2224 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
2225 error = vn_fullpath1(td, vn, rootvnode, buf, retbuf, MAXPATHLEN);
2234 vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, u_int *buflen)
2237 struct namecache *ncp;
2241 vlp = VP2VNODELOCK(*vp);
2243 TAILQ_FOREACH(ncp, &((*vp)->v_cache_dst), nc_dst) {
2244 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2248 if (*buflen < ncp->nc_nlen) {
2251 counter_u64_add(numfullpathfail4, 1);
2253 SDT_PROBE3(vfs, namecache, fullpath, return, error,
2257 *buflen -= ncp->nc_nlen;
2258 memcpy(buf + *buflen, ncp->nc_name, ncp->nc_nlen);
2259 SDT_PROBE3(vfs, namecache, fullpath, hit, ncp->nc_dvp,
2268 SDT_PROBE1(vfs, namecache, fullpath, miss, vp);
2271 vn_lock(*vp, LK_SHARED | LK_RETRY);
2272 error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen);
2275 counter_u64_add(numfullpathfail2, 1);
2276 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2281 if (dvp->v_iflag & VI_DOOMED) {
2282 /* forced unmount */
2285 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2289 * *vp has its use count incremented still.
2296 * The magic behind kern___getcwd() and vn_fullpath().
2299 vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
2300 char *buf, char **retbuf, u_int buflen)
2302 int error, slash_prefixed;
2303 #ifdef KDTRACE_HOOKS
2304 struct vnode *startvp = vp;
2313 SDT_PROBE1(vfs, namecache, fullpath, entry, vp);
2314 counter_u64_add(numfullpathcalls, 1);
2316 if (vp->v_type != VDIR) {
2317 error = vn_vptocnp(&vp, td->td_ucred, buf, &buflen);
2324 buf[--buflen] = '/';
2327 while (vp != rdir && vp != rootvnode) {
2329 * The vp vnode must be already fully constructed,
2330 * since it is either found in namecache or obtained
2331 * from VOP_VPTOCNP(). We may test for VV_ROOT safely
2332 * without obtaining the vnode lock.
2334 if ((vp->v_vflag & VV_ROOT) != 0) {
2335 vn_lock(vp, LK_RETRY | LK_SHARED);
2338 * With the vnode locked, check for races with
2339 * unmount, forced or not. Note that we
2340 * already verified that vp is not equal to
2341 * the root vnode, which means that
2342 * mnt_vnodecovered can be NULL only for the
2345 if ((vp->v_iflag & VI_DOOMED) != 0 ||
2346 (vp1 = vp->v_mount->mnt_vnodecovered) == NULL ||
2347 vp1->v_mountedhere != vp->v_mount) {
2350 SDT_PROBE3(vfs, namecache, fullpath, return,
2360 if (vp->v_type != VDIR) {
2362 counter_u64_add(numfullpathfail1, 1);
2364 SDT_PROBE3(vfs, namecache, fullpath, return,
2368 error = vn_vptocnp(&vp, td->td_ucred, buf, &buflen);
2374 SDT_PROBE3(vfs, namecache, fullpath, return, error,
2378 buf[--buflen] = '/';
2383 if (!slash_prefixed) {
2386 counter_u64_add(numfullpathfail4, 1);
2387 SDT_PROBE3(vfs, namecache, fullpath, return, ENOMEM,
2391 buf[--buflen] = '/';
2393 counter_u64_add(numfullpathfound, 1);
2396 SDT_PROBE3(vfs, namecache, fullpath, return, 0, startvp, buf + buflen);
2397 *retbuf = buf + buflen;
2402 vn_dir_dd_ino(struct vnode *vp)
2404 struct namecache *ncp;
2408 ASSERT_VOP_LOCKED(vp, "vn_dir_dd_ino");
2409 vlp = VP2VNODELOCK(vp);
2411 TAILQ_FOREACH(ncp, &(vp->v_cache_dst), nc_dst) {
2412 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0)
2417 if (vget(ddvp, LK_SHARED | LK_NOWAIT | LK_VNHELD, curthread))
2426 vn_commname(struct vnode *vp, char *buf, u_int buflen)
2428 struct namecache *ncp;
2432 vlp = VP2VNODELOCK(vp);
2434 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst)
2435 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2441 l = min(ncp->nc_nlen, buflen - 1);
2442 memcpy(buf, ncp->nc_name, l);
2448 /* ABI compat shims for old kernel modules. */
2451 void cache_enter(struct vnode *dvp, struct vnode *vp,
2452 struct componentname *cnp);
2455 cache_enter(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2458 cache_enter_time(dvp, vp, cnp, NULL, NULL);
2462 * This function updates path string to vnode's full global path
2463 * and checks the size of the new path string against the pathlen argument.
2465 * Requires a locked, referenced vnode.
2466 * Vnode is re-locked on success or ENODEV, otherwise unlocked.
2468 * If sysctl debug.disablefullpath is set, ENODEV is returned,
2469 * vnode is left locked and path remain untouched.
2471 * If vp is a directory, the call to vn_fullpath_global() always succeeds
2472 * because it falls back to the ".." lookup if the namecache lookup fails.
2475 vn_path_to_global_path(struct thread *td, struct vnode *vp, char *path,
2478 struct nameidata nd;
2483 ASSERT_VOP_ELOCKED(vp, __func__);
2485 /* Return ENODEV if sysctl debug.disablefullpath==1 */
2486 if (__predict_false(disablefullpath))
2489 /* Construct global filesystem path from vp. */
2491 error = vn_fullpath_global(td, vp, &rpath, &fbuf);
2498 if (strlen(rpath) >= pathlen) {
2500 error = ENAMETOOLONG;
2505 * Re-lookup the vnode by path to detect a possible rename.
2506 * As a side effect, the vnode is relocked.
2507 * If vnode was renamed, return ENOENT.
2509 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
2510 UIO_SYSSPACE, path, td);
2516 NDFREE(&nd, NDF_ONLY_PNBUF);
2520 strcpy(path, rpath);