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;
757 for (i = start; i < numneglists; i++) {
758 neglist = &neglists[i];
759 if (TAILQ_FIRST(&neglist->nl_list) == NULL)
761 mtx_lock(&neglist->nl_lock);
762 ncp = TAILQ_FIRST(&neglist->nl_list);
765 mtx_unlock(&neglist->nl_lock);
768 *neglistpp = neglist;
773 cache_negative_zap_one(void)
775 struct namecache *ncp, *ncp2;
776 struct neglist *neglist;
780 if (!mtx_trylock(&ncneg_shrink_lock))
783 mtx_lock(&ncneg_hot.nl_lock);
784 ncp = TAILQ_FIRST(&ncneg_hot.nl_list);
786 neglist = NCP2NEGLIST(ncp);
787 mtx_lock(&neglist->nl_lock);
788 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
789 TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst);
790 ncp->nc_flag &= ~NCF_HOTNEGATIVE;
791 mtx_unlock(&neglist->nl_lock);
794 cache_negative_shrink_select(shrink_list_turn, &ncp, &neglist);
796 if (shrink_list_turn == numneglists)
797 shrink_list_turn = 0;
798 if (ncp == NULL && shrink_list_turn == 0)
799 cache_negative_shrink_select(shrink_list_turn, &ncp, &neglist);
801 mtx_unlock(&ncneg_hot.nl_lock);
805 MPASS(ncp->nc_flag & NCF_NEGATIVE);
806 dvlp = VP2VNODELOCK(ncp->nc_dvp);
807 blp = NCP2BUCKETLOCK(ncp);
808 mtx_unlock(&neglist->nl_lock);
809 mtx_unlock(&ncneg_hot.nl_lock);
812 mtx_lock(&neglist->nl_lock);
813 ncp2 = TAILQ_FIRST(&neglist->nl_list);
814 if (ncp != ncp2 || dvlp != VP2VNODELOCK(ncp2->nc_dvp) ||
815 blp != NCP2BUCKETLOCK(ncp2) || !(ncp2->nc_flag & NCF_NEGATIVE)) {
819 SDT_PROBE3(vfs, namecache, shrink_negative, done, ncp->nc_dvp,
820 ncp->nc_name, ncp->nc_neghits);
822 cache_zap_locked(ncp, true);
824 mtx_unlock(&neglist->nl_lock);
828 mtx_unlock(&ncneg_shrink_lock);
833 * cache_zap_locked():
835 * Removes a namecache entry from cache, whether it contains an actual
836 * pointer to a vnode or if it is just a negative cache entry.
839 cache_zap_locked(struct namecache *ncp, bool neg_locked)
842 if (!(ncp->nc_flag & NCF_NEGATIVE))
843 cache_assert_vnode_locked(ncp->nc_vp);
844 cache_assert_vnode_locked(ncp->nc_dvp);
845 cache_assert_bucket_locked(ncp, RA_WLOCKED);
847 CTR2(KTR_VFS, "cache_zap(%p) vp %p", ncp,
848 (ncp->nc_flag & NCF_NEGATIVE) ? NULL : ncp->nc_vp);
849 if (!(ncp->nc_flag & NCF_NEGATIVE)) {
850 SDT_PROBE3(vfs, namecache, zap, done, ncp->nc_dvp,
851 ncp->nc_name, ncp->nc_vp);
853 SDT_PROBE3(vfs, namecache, zap_negative, done, ncp->nc_dvp,
854 ncp->nc_name, ncp->nc_neghits);
856 LIST_REMOVE(ncp, nc_hash);
857 if (!(ncp->nc_flag & NCF_NEGATIVE)) {
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 cache_negative_remove(ncp, neg_locked);
864 if (ncp->nc_flag & NCF_ISDOTDOT) {
865 if (ncp == ncp->nc_dvp->v_cache_dd)
866 ncp->nc_dvp->v_cache_dd = NULL;
868 LIST_REMOVE(ncp, nc_src);
869 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) {
870 ncp->nc_flag |= NCF_DVDROP;
871 atomic_subtract_rel_long(&numcachehv, 1);
874 atomic_subtract_rel_long(&numcache, 1);
878 cache_zap_negative_locked_vnode_kl(struct namecache *ncp, struct vnode *vp)
882 MPASS(ncp->nc_dvp == vp);
883 MPASS(ncp->nc_flag & NCF_NEGATIVE);
884 cache_assert_vnode_locked(vp);
886 blp = NCP2BUCKETLOCK(ncp);
888 cache_zap_locked(ncp, false);
893 cache_zap_locked_vnode_kl2(struct namecache *ncp, struct vnode *vp,
896 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
899 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
900 cache_assert_vnode_locked(vp);
902 if (ncp->nc_flag & NCF_NEGATIVE) {
907 cache_zap_negative_locked_vnode_kl(ncp, vp);
911 pvlp = VP2VNODELOCK(vp);
912 blp = NCP2BUCKETLOCK(ncp);
913 vlp1 = VP2VNODELOCK(ncp->nc_dvp);
914 vlp2 = VP2VNODELOCK(ncp->nc_vp);
916 if (*vlpp == vlp1 || *vlpp == vlp2) {
924 cache_sort(&vlp1, &vlp2);
929 if (!mtx_trylock(vlp1))
935 cache_zap_locked(ncp, false);
937 if (to_unlock != NULL)
938 mtx_unlock(to_unlock);
945 MPASS(*vlpp == NULL);
951 cache_zap_locked_vnode(struct namecache *ncp, struct vnode *vp)
953 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
957 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
958 cache_assert_vnode_locked(vp);
960 pvlp = VP2VNODELOCK(vp);
961 if (ncp->nc_flag & NCF_NEGATIVE) {
962 cache_zap_negative_locked_vnode_kl(ncp, vp);
966 blp = NCP2BUCKETLOCK(ncp);
967 vlp1 = VP2VNODELOCK(ncp->nc_dvp);
968 vlp2 = VP2VNODELOCK(ncp->nc_vp);
969 cache_sort(&vlp1, &vlp2);
974 if (!mtx_trylock(vlp1)) {
981 cache_zap_locked(ncp, false);
983 mtx_unlock(to_unlock);
990 cache_zap_wlocked_bucket(struct namecache *ncp, struct rwlock *blp)
992 struct mtx *dvlp, *vlp;
994 cache_assert_bucket_locked(ncp, RA_WLOCKED);
996 dvlp = VP2VNODELOCK(ncp->nc_dvp);
998 if (!(ncp->nc_flag & NCF_NEGATIVE))
999 vlp = VP2VNODELOCK(ncp->nc_vp);
1000 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1001 cache_zap_locked(ncp, false);
1003 cache_unlock_vnodes(dvlp, vlp);
1012 cache_zap_rlocked_bucket(struct namecache *ncp, struct rwlock *blp)
1014 struct mtx *dvlp, *vlp;
1016 cache_assert_bucket_locked(ncp, RA_RLOCKED);
1018 dvlp = VP2VNODELOCK(ncp->nc_dvp);
1020 if (!(ncp->nc_flag & NCF_NEGATIVE))
1021 vlp = VP2VNODELOCK(ncp->nc_vp);
1022 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1025 cache_zap_locked(ncp, false);
1027 cache_unlock_vnodes(dvlp, vlp);
1036 cache_zap_wlocked_bucket_kl(struct namecache *ncp, struct rwlock *blp,
1037 struct mtx **vlpp1, struct mtx **vlpp2)
1039 struct mtx *dvlp, *vlp;
1041 cache_assert_bucket_locked(ncp, RA_WLOCKED);
1043 dvlp = VP2VNODELOCK(ncp->nc_dvp);
1045 if (!(ncp->nc_flag & NCF_NEGATIVE))
1046 vlp = VP2VNODELOCK(ncp->nc_vp);
1047 cache_sort(&dvlp, &vlp);
1049 if (*vlpp1 == dvlp && *vlpp2 == vlp) {
1050 cache_zap_locked(ncp, false);
1051 cache_unlock_vnodes(dvlp, vlp);
1064 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1065 cache_zap_locked(ncp, false);
1066 cache_unlock_vnodes(dvlp, vlp);
1081 cache_lookup_unlock(struct rwlock *blp, struct mtx *vlp)
1091 static int __noinline
1092 cache_lookup_dot(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1093 struct timespec *tsp, int *ticksp)
1098 CTR2(KTR_VFS, "cache_lookup(%p, %s) found via .",
1099 dvp, cnp->cn_nameptr);
1100 counter_u64_add(dothits, 1);
1101 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", *vpp);
1108 * When we lookup "." we still can be asked to lock it
1111 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
1112 if (ltype != VOP_ISLOCKED(*vpp)) {
1113 if (ltype == LK_EXCLUSIVE) {
1114 vn_lock(*vpp, LK_UPGRADE | LK_RETRY);
1115 if ((*vpp)->v_iflag & VI_DOOMED) {
1116 /* forced unmount */
1122 vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY);
1128 * Lookup an entry in the cache
1130 * Lookup is called with dvp pointing to the directory to search,
1131 * cnp pointing to the name of the entry being sought. If the lookup
1132 * succeeds, the vnode is returned in *vpp, and a status of -1 is
1133 * returned. If the lookup determines that the name does not exist
1134 * (negative caching), a status of ENOENT is returned. If the lookup
1135 * fails, a status of zero is returned. If the directory vnode is
1136 * recycled out from under us due to a forced unmount, a status of
1137 * ENOENT is returned.
1139 * vpp is locked and ref'd on return. If we're looking up DOTDOT, dvp is
1140 * unlocked. If we're looking up . an extra ref is taken, but the lock is
1141 * not recursively acquired.
1144 static __noinline int
1145 cache_lookup_nomakeentry(struct vnode *dvp, struct vnode **vpp,
1146 struct componentname *cnp, struct timespec *tsp, int *ticksp)
1148 struct namecache *ncp;
1150 struct mtx *dvlp, *dvlp2;
1154 if (cnp->cn_namelen == 2 &&
1155 cnp->cn_nameptr[0] == '.' && cnp->cn_nameptr[1] == '.') {
1156 counter_u64_add(dotdothits, 1);
1157 dvlp = VP2VNODELOCK(dvp);
1161 ncp = dvp->v_cache_dd;
1163 SDT_PROBE3(vfs, namecache, lookup, miss, dvp,
1170 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1171 if (ncp->nc_dvp != dvp)
1172 panic("dvp %p v_cache_dd %p\n", dvp, ncp);
1173 if (!cache_zap_locked_vnode_kl2(ncp,
1176 MPASS(dvp->v_cache_dd == NULL);
1182 dvp->v_cache_dd = NULL;
1190 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1191 blp = HASH2BUCKETLOCK(hash);
1193 if (LIST_EMPTY(NCHHASH(hash)))
1198 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1199 counter_u64_add(numchecks, 1);
1200 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1201 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1205 /* We failed to find an entry */
1211 counter_u64_add(numposzaps, 1);
1213 error = cache_zap_wlocked_bucket(ncp, blp);
1215 zap_and_exit_bucket_fail++;
1216 cache_maybe_yield();
1222 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr, NULL);
1223 counter_u64_add(nummisszap, 1);
1228 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1229 struct timespec *tsp, int *ticksp)
1231 struct namecache_ts *ncp_ts;
1232 struct namecache *ncp;
1238 if (__predict_false(!doingcache)) {
1239 cnp->cn_flags &= ~MAKEENTRY;
1243 counter_u64_add(numcalls, 1);
1245 if (__predict_false(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.'))
1246 return (cache_lookup_dot(dvp, vpp, cnp, tsp, ticksp));
1248 if ((cnp->cn_flags & MAKEENTRY) == 0)
1249 return (cache_lookup_nomakeentry(dvp, vpp, cnp, tsp, ticksp));
1255 if (cnp->cn_namelen == 2 &&
1256 cnp->cn_nameptr[0] == '.' && cnp->cn_nameptr[1] == '.') {
1257 counter_u64_add(dotdothits, 1);
1258 dvlp = VP2VNODELOCK(dvp);
1260 ncp = dvp->v_cache_dd;
1262 SDT_PROBE3(vfs, namecache, lookup, miss, dvp,
1267 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1268 if (ncp->nc_flag & NCF_NEGATIVE)
1274 /* Return failure if negative entry was found. */
1276 goto negative_success;
1277 CTR3(KTR_VFS, "cache_lookup(%p, %s) found %p via ..",
1278 dvp, cnp->cn_nameptr, *vpp);
1279 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, "..",
1281 cache_out_ts(ncp, tsp, ticksp);
1282 if ((ncp->nc_flag & (NCF_ISDOTDOT | NCF_DTS)) ==
1283 NCF_DTS && tsp != NULL) {
1284 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1285 *tsp = ncp_ts->nc_dotdottime;
1290 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1291 blp = HASH2BUCKETLOCK(hash);
1294 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1295 counter_u64_add(numchecks, 1);
1296 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1297 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1301 /* We failed to find an entry */
1304 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
1306 counter_u64_add(nummiss, 1);
1310 /* We found a "positive" match, return the vnode */
1311 if (!(ncp->nc_flag & NCF_NEGATIVE)) {
1312 counter_u64_add(numposhits, 1);
1314 CTR4(KTR_VFS, "cache_lookup(%p, %s) found %p via ncp %p",
1315 dvp, cnp->cn_nameptr, *vpp, ncp);
1316 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name,
1318 cache_out_ts(ncp, tsp, ticksp);
1323 /* We found a negative match, and want to create it, so purge */
1324 if (cnp->cn_nameiop == CREATE) {
1325 counter_u64_add(numnegzaps, 1);
1329 counter_u64_add(numneghits, 1);
1330 cache_negative_hit(ncp);
1331 if (ncp->nc_flag & NCF_WHITE)
1332 cnp->cn_flags |= ISWHITEOUT;
1333 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp,
1335 cache_out_ts(ncp, tsp, ticksp);
1336 cache_lookup_unlock(blp, dvlp);
1341 * On success we return a locked and ref'd vnode as per the lookup
1345 ltype = 0; /* silence gcc warning */
1346 if (cnp->cn_flags & ISDOTDOT) {
1347 ltype = VOP_ISLOCKED(dvp);
1351 cache_lookup_unlock(blp, dvlp);
1352 error = vget(*vpp, cnp->cn_lkflags | LK_VNHELD, cnp->cn_thread);
1353 if (cnp->cn_flags & ISDOTDOT) {
1354 vn_lock(dvp, ltype | LK_RETRY);
1355 if (dvp->v_iflag & VI_DOOMED) {
1366 if ((cnp->cn_flags & ISLASTCN) &&
1367 (cnp->cn_lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE) {
1368 ASSERT_VOP_ELOCKED(*vpp, "cache_lookup");
1374 error = cache_zap_rlocked_bucket(ncp, blp);
1376 error = cache_zap_locked_vnode(ncp, dvp);
1378 zap_and_exit_bucket_fail++;
1379 cache_maybe_yield();
1386 struct celockstate {
1388 struct rwlock *blp[2];
1390 CTASSERT((nitems(((struct celockstate *)0)->vlp) == 3));
1391 CTASSERT((nitems(((struct celockstate *)0)->blp) == 2));
1394 cache_celockstate_init(struct celockstate *cel)
1397 bzero(cel, sizeof(*cel));
1401 cache_lock_vnodes_cel(struct celockstate *cel, struct vnode *vp,
1404 struct mtx *vlp1, *vlp2;
1406 MPASS(cel->vlp[0] == NULL);
1407 MPASS(cel->vlp[1] == NULL);
1408 MPASS(cel->vlp[2] == NULL);
1410 MPASS(vp != NULL || dvp != NULL);
1412 vlp1 = VP2VNODELOCK(vp);
1413 vlp2 = VP2VNODELOCK(dvp);
1414 cache_sort(&vlp1, &vlp2);
1425 cache_unlock_vnodes_cel(struct celockstate *cel)
1428 MPASS(cel->vlp[0] != NULL || cel->vlp[1] != NULL);
1430 if (cel->vlp[0] != NULL)
1431 mtx_unlock(cel->vlp[0]);
1432 if (cel->vlp[1] != NULL)
1433 mtx_unlock(cel->vlp[1]);
1434 if (cel->vlp[2] != NULL)
1435 mtx_unlock(cel->vlp[2]);
1439 cache_lock_vnodes_cel_3(struct celockstate *cel, struct vnode *vp)
1444 cache_assert_vlp_locked(cel->vlp[0]);
1445 cache_assert_vlp_locked(cel->vlp[1]);
1446 MPASS(cel->vlp[2] == NULL);
1449 vlp = VP2VNODELOCK(vp);
1452 if (vlp >= cel->vlp[1]) {
1455 if (mtx_trylock(vlp))
1457 cache_lock_vnodes_cel_3_failures++;
1458 cache_unlock_vnodes_cel(cel);
1459 if (vlp < cel->vlp[0]) {
1461 mtx_lock(cel->vlp[0]);
1462 mtx_lock(cel->vlp[1]);
1464 if (cel->vlp[0] != NULL)
1465 mtx_lock(cel->vlp[0]);
1467 mtx_lock(cel->vlp[1]);
1477 cache_lock_buckets_cel(struct celockstate *cel, struct rwlock *blp1,
1478 struct rwlock *blp2)
1481 MPASS(cel->blp[0] == NULL);
1482 MPASS(cel->blp[1] == NULL);
1484 cache_sort(&blp1, &blp2);
1495 cache_unlock_buckets_cel(struct celockstate *cel)
1498 if (cel->blp[0] != NULL)
1499 rw_wunlock(cel->blp[0]);
1500 rw_wunlock(cel->blp[1]);
1504 * Lock part of the cache affected by the insertion.
1506 * This means vnodelocks for dvp, vp and the relevant bucketlock.
1507 * However, insertion can result in removal of an old entry. In this
1508 * case we have an additional vnode and bucketlock pair to lock. If the
1509 * entry is negative, ncelock is locked instead of the vnode.
1511 * That is, in the worst case we have to lock 3 vnodes and 2 bucketlocks, while
1512 * preserving the locking order (smaller address first).
1515 cache_enter_lock(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1518 struct namecache *ncp;
1519 struct rwlock *blps[2];
1521 blps[0] = HASH2BUCKETLOCK(hash);
1524 cache_lock_vnodes_cel(cel, dvp, vp);
1525 if (vp == NULL || vp->v_type != VDIR)
1527 ncp = vp->v_cache_dd;
1530 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1532 MPASS(ncp->nc_dvp == vp);
1533 blps[1] = NCP2BUCKETLOCK(ncp);
1534 if (ncp->nc_flag & NCF_NEGATIVE)
1536 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1539 * All vnodes got re-locked. Re-validate the state and if
1540 * nothing changed we are done. Otherwise restart.
1542 if (ncp == vp->v_cache_dd &&
1543 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1544 blps[1] == NCP2BUCKETLOCK(ncp) &&
1545 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1547 cache_unlock_vnodes_cel(cel);
1552 cache_lock_buckets_cel(cel, blps[0], blps[1]);
1556 cache_enter_lock_dd(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1559 struct namecache *ncp;
1560 struct rwlock *blps[2];
1562 blps[0] = HASH2BUCKETLOCK(hash);
1565 cache_lock_vnodes_cel(cel, dvp, vp);
1566 ncp = dvp->v_cache_dd;
1569 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1571 MPASS(ncp->nc_dvp == dvp);
1572 blps[1] = NCP2BUCKETLOCK(ncp);
1573 if (ncp->nc_flag & NCF_NEGATIVE)
1575 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1577 if (ncp == dvp->v_cache_dd &&
1578 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1579 blps[1] == NCP2BUCKETLOCK(ncp) &&
1580 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1582 cache_unlock_vnodes_cel(cel);
1587 cache_lock_buckets_cel(cel, blps[0], blps[1]);
1591 cache_enter_unlock(struct celockstate *cel)
1594 cache_unlock_buckets_cel(cel);
1595 cache_unlock_vnodes_cel(cel);
1599 * Add an entry to the cache.
1602 cache_enter_time(struct vnode *dvp, struct vnode *vp, struct componentname *cnp,
1603 struct timespec *tsp, struct timespec *dtsp)
1605 struct celockstate cel;
1606 struct namecache *ncp, *n2, *ndd;
1607 struct namecache_ts *ncp_ts, *n2_ts;
1608 struct nchashhead *ncpp;
1609 struct neglist *neglist;
1616 CTR3(KTR_VFS, "cache_enter(%p, %p, %s)", dvp, vp, cnp->cn_nameptr);
1617 VNASSERT(vp == NULL || (vp->v_iflag & VI_DOOMED) == 0, vp,
1618 ("cache_enter: Adding a doomed vnode"));
1619 VNASSERT(dvp == NULL || (dvp->v_iflag & VI_DOOMED) == 0, dvp,
1620 ("cache_enter: Doomed vnode used as src"));
1622 if (__predict_false(!doingcache))
1626 * Avoid blowout in namecache entries.
1628 if (__predict_false(numcache >= desiredvnodes * ncsizefactor))
1631 cache_celockstate_init(&cel);
1635 if (cnp->cn_nameptr[0] == '.') {
1636 if (cnp->cn_namelen == 1)
1638 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
1639 len = cnp->cn_namelen;
1640 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1641 cache_enter_lock_dd(&cel, dvp, vp, hash);
1643 * If dotdot entry already exists, just retarget it
1644 * to new parent vnode, otherwise continue with new
1645 * namecache entry allocation.
1647 if ((ncp = dvp->v_cache_dd) != NULL &&
1648 ncp->nc_flag & NCF_ISDOTDOT) {
1649 KASSERT(ncp->nc_dvp == dvp,
1650 ("wrong isdotdot parent"));
1652 if (ncp->nc_flag & NCF_NEGATIVE || vp == NULL) {
1653 neglist = NCP2NEGLIST(ncp);
1654 mtx_lock(&ncneg_hot.nl_lock);
1655 mtx_lock(&neglist->nl_lock);
1658 if (!(ncp->nc_flag & NCF_NEGATIVE)) {
1659 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst,
1662 cache_negative_remove(ncp, true);
1665 TAILQ_INSERT_HEAD(&vp->v_cache_dst,
1667 ncp->nc_flag &= ~(NCF_NEGATIVE|NCF_HOTNEGATIVE);
1669 ncp->nc_flag &= ~(NCF_HOTNEGATIVE);
1670 ncp->nc_flag |= NCF_NEGATIVE;
1671 cache_negative_insert(ncp, true);
1674 mtx_unlock(&neglist->nl_lock);
1675 mtx_unlock(&ncneg_hot.nl_lock);
1678 cache_enter_unlock(&cel);
1681 dvp->v_cache_dd = NULL;
1682 cache_enter_unlock(&cel);
1683 cache_celockstate_init(&cel);
1684 SDT_PROBE3(vfs, namecache, enter, done, dvp, "..", vp);
1685 flag = NCF_ISDOTDOT;
1690 * Calculate the hash key and setup as much of the new
1691 * namecache entry as possible before acquiring the lock.
1693 ncp = cache_alloc(cnp->cn_namelen, tsp != NULL);
1694 ncp->nc_flag = flag;
1697 ncp->nc_flag |= NCF_NEGATIVE;
1700 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1701 ncp_ts->nc_time = *tsp;
1702 ncp_ts->nc_ticks = ticks;
1703 ncp_ts->nc_nc.nc_flag |= NCF_TS;
1705 ncp_ts->nc_dotdottime = *dtsp;
1706 ncp_ts->nc_nc.nc_flag |= NCF_DTS;
1709 len = ncp->nc_nlen = cnp->cn_namelen;
1710 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1711 strlcpy(ncp->nc_name, cnp->cn_nameptr, len + 1);
1712 cache_enter_lock(&cel, dvp, vp, hash);
1715 * See if this vnode or negative entry is already in the cache
1716 * with this name. This can happen with concurrent lookups of
1717 * the same path name.
1719 ncpp = NCHHASH(hash);
1720 LIST_FOREACH(n2, ncpp, nc_hash) {
1721 if (n2->nc_dvp == dvp &&
1722 n2->nc_nlen == cnp->cn_namelen &&
1723 !bcmp(n2->nc_name, cnp->cn_nameptr, n2->nc_nlen)) {
1725 KASSERT((n2->nc_flag & NCF_TS) != 0,
1727 n2_ts = __containerof(n2, struct namecache_ts, nc_nc);
1728 n2_ts->nc_time = ncp_ts->nc_time;
1729 n2_ts->nc_ticks = ncp_ts->nc_ticks;
1731 n2_ts->nc_dotdottime = ncp_ts->nc_dotdottime;
1732 if (ncp->nc_flag & NCF_NEGATIVE)
1733 mtx_lock(&ncneg_hot.nl_lock);
1734 n2_ts->nc_nc.nc_flag |= NCF_DTS;
1735 if (ncp->nc_flag & NCF_NEGATIVE)
1736 mtx_unlock(&ncneg_hot.nl_lock);
1739 goto out_unlock_free;
1743 if (flag == NCF_ISDOTDOT) {
1745 * See if we are trying to add .. entry, but some other lookup
1746 * has populated v_cache_dd pointer already.
1748 if (dvp->v_cache_dd != NULL)
1749 goto out_unlock_free;
1750 KASSERT(vp == NULL || vp->v_type == VDIR,
1751 ("wrong vnode type %p", vp));
1752 dvp->v_cache_dd = ncp;
1756 if (vp->v_type == VDIR) {
1757 if (flag != NCF_ISDOTDOT) {
1759 * For this case, the cache entry maps both the
1760 * directory name in it and the name ".." for the
1761 * directory's parent.
1763 if ((ndd = vp->v_cache_dd) != NULL) {
1764 if ((ndd->nc_flag & NCF_ISDOTDOT) != 0)
1765 cache_zap_locked(ndd, false);
1769 vp->v_cache_dd = ncp;
1772 vp->v_cache_dd = NULL;
1776 if (flag != NCF_ISDOTDOT) {
1777 if (LIST_EMPTY(&dvp->v_cache_src)) {
1779 atomic_add_rel_long(&numcachehv, 1);
1781 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
1785 * Insert the new namecache entry into the appropriate chain
1786 * within the cache entries table.
1788 LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
1791 * If the entry is "negative", we place it into the
1792 * "negative" cache queue, otherwise, we place it into the
1793 * destination vnode's cache entries queue.
1796 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
1797 SDT_PROBE3(vfs, namecache, enter, done, dvp, ncp->nc_name,
1800 if (cnp->cn_flags & ISWHITEOUT)
1801 ncp->nc_flag |= NCF_WHITE;
1802 cache_negative_insert(ncp, false);
1803 SDT_PROBE2(vfs, namecache, enter_negative, done, dvp,
1806 cache_enter_unlock(&cel);
1807 lnumcache = atomic_fetchadd_long(&numcache, 1) + 1;
1808 if (numneg * ncnegfactor > lnumcache)
1809 cache_negative_zap_one();
1813 cache_enter_unlock(&cel);
1819 cache_roundup_2(u_int val)
1823 for (res = 1; res <= val; res <<= 1)
1830 * Name cache initialization, from vfs_init() when we are booting
1833 nchinit(void *dummy __unused)
1837 cache_zone_small = uma_zcreate("S VFS Cache",
1838 sizeof(struct namecache) + CACHE_PATH_CUTOFF + 1,
1839 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache),
1841 cache_zone_small_ts = uma_zcreate("STS VFS Cache",
1842 sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1,
1843 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache_ts),
1845 cache_zone_large = uma_zcreate("L VFS Cache",
1846 sizeof(struct namecache) + NAME_MAX + 1,
1847 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache),
1849 cache_zone_large_ts = uma_zcreate("LTS VFS Cache",
1850 sizeof(struct namecache_ts) + NAME_MAX + 1,
1851 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache_ts),
1854 nchashtbl = hashinit(desiredvnodes * 2, M_VFSCACHE, &nchash);
1855 ncbuckethash = cache_roundup_2(mp_ncpus * 64) - 1;
1856 if (ncbuckethash > nchash)
1857 ncbuckethash = nchash;
1858 bucketlocks = malloc(sizeof(*bucketlocks) * numbucketlocks, M_VFSCACHE,
1860 for (i = 0; i < numbucketlocks; i++)
1861 rw_init_flags(&bucketlocks[i], "ncbuc", RW_DUPOK | RW_RECURSE);
1862 ncvnodehash = cache_roundup_2(mp_ncpus * 64) - 1;
1863 vnodelocks = malloc(sizeof(*vnodelocks) * numvnodelocks, M_VFSCACHE,
1865 for (i = 0; i < numvnodelocks; i++)
1866 mtx_init(&vnodelocks[i], "ncvn", NULL, MTX_DUPOK | MTX_RECURSE);
1867 ncpurgeminvnodes = numbucketlocks;
1870 neglists = malloc(sizeof(*neglists) * numneglists, M_VFSCACHE,
1872 for (i = 0; i < numneglists; i++) {
1873 mtx_init(&neglists[i].nl_lock, "ncnegl", NULL, MTX_DEF);
1874 TAILQ_INIT(&neglists[i].nl_list);
1876 mtx_init(&ncneg_hot.nl_lock, "ncneglh", NULL, MTX_DEF);
1877 TAILQ_INIT(&ncneg_hot.nl_list);
1879 mtx_init(&ncneg_shrink_lock, "ncnegs", NULL, MTX_DEF);
1881 numcalls = counter_u64_alloc(M_WAITOK);
1882 dothits = counter_u64_alloc(M_WAITOK);
1883 dotdothits = counter_u64_alloc(M_WAITOK);
1884 numchecks = counter_u64_alloc(M_WAITOK);
1885 nummiss = counter_u64_alloc(M_WAITOK);
1886 nummisszap = counter_u64_alloc(M_WAITOK);
1887 numposzaps = counter_u64_alloc(M_WAITOK);
1888 numposhits = counter_u64_alloc(M_WAITOK);
1889 numnegzaps = counter_u64_alloc(M_WAITOK);
1890 numneghits = counter_u64_alloc(M_WAITOK);
1891 numfullpathcalls = counter_u64_alloc(M_WAITOK);
1892 numfullpathfail1 = counter_u64_alloc(M_WAITOK);
1893 numfullpathfail2 = counter_u64_alloc(M_WAITOK);
1894 numfullpathfail4 = counter_u64_alloc(M_WAITOK);
1895 numfullpathfound = counter_u64_alloc(M_WAITOK);
1897 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL);
1900 cache_changesize(int newmaxvnodes)
1902 struct nchashhead *new_nchashtbl, *old_nchashtbl;
1903 u_long new_nchash, old_nchash;
1904 struct namecache *ncp;
1908 newmaxvnodes = cache_roundup_2(newmaxvnodes * 2);
1909 if (newmaxvnodes < numbucketlocks)
1910 newmaxvnodes = numbucketlocks;
1912 new_nchashtbl = hashinit(newmaxvnodes, M_VFSCACHE, &new_nchash);
1913 /* If same hash table size, nothing to do */
1914 if (nchash == new_nchash) {
1915 free(new_nchashtbl, M_VFSCACHE);
1919 * Move everything from the old hash table to the new table.
1920 * None of the namecache entries in the table can be removed
1921 * because to do so, they have to be removed from the hash table.
1923 cache_lock_all_vnodes();
1924 cache_lock_all_buckets();
1925 old_nchashtbl = nchashtbl;
1926 old_nchash = nchash;
1927 nchashtbl = new_nchashtbl;
1928 nchash = new_nchash;
1929 for (i = 0; i <= old_nchash; i++) {
1930 while ((ncp = LIST_FIRST(&old_nchashtbl[i])) != NULL) {
1931 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen,
1933 LIST_REMOVE(ncp, nc_hash);
1934 LIST_INSERT_HEAD(NCHHASH(hash), ncp, nc_hash);
1937 cache_unlock_all_buckets();
1938 cache_unlock_all_vnodes();
1939 free(old_nchashtbl, M_VFSCACHE);
1943 * Invalidate all entries to a particular vnode.
1946 cache_purge(struct vnode *vp)
1948 TAILQ_HEAD(, namecache) ncps;
1949 struct namecache *ncp, *nnp;
1950 struct mtx *vlp, *vlp2;
1952 CTR1(KTR_VFS, "cache_purge(%p)", vp);
1953 SDT_PROBE1(vfs, namecache, purge, done, vp);
1954 if (LIST_EMPTY(&vp->v_cache_src) && TAILQ_EMPTY(&vp->v_cache_dst) &&
1955 vp->v_cache_dd == NULL)
1958 vlp = VP2VNODELOCK(vp);
1962 while (!LIST_EMPTY(&vp->v_cache_src)) {
1963 ncp = LIST_FIRST(&vp->v_cache_src);
1964 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
1966 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
1968 while (!TAILQ_EMPTY(&vp->v_cache_dst)) {
1969 ncp = TAILQ_FIRST(&vp->v_cache_dst);
1970 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
1972 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
1974 ncp = vp->v_cache_dd;
1976 KASSERT(ncp->nc_flag & NCF_ISDOTDOT,
1977 ("lost dotdot link"));
1978 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
1980 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
1982 KASSERT(vp->v_cache_dd == NULL, ("incomplete purge"));
1986 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
1992 * Invalidate all negative entries for a particular directory vnode.
1995 cache_purge_negative(struct vnode *vp)
1997 TAILQ_HEAD(, namecache) ncps;
1998 struct namecache *ncp, *nnp;
2001 CTR1(KTR_VFS, "cache_purge_negative(%p)", vp);
2002 SDT_PROBE1(vfs, namecache, purge_negative, done, vp);
2003 if (LIST_EMPTY(&vp->v_cache_src))
2006 vlp = VP2VNODELOCK(vp);
2008 LIST_FOREACH_SAFE(ncp, &vp->v_cache_src, nc_src, nnp) {
2009 if (!(ncp->nc_flag & NCF_NEGATIVE))
2011 cache_zap_negative_locked_vnode_kl(ncp, vp);
2012 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2015 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2021 * Flush all entries referencing a particular filesystem.
2024 cache_purgevfs(struct mount *mp, bool force)
2026 TAILQ_HEAD(, namecache) ncps;
2027 struct mtx *vlp1, *vlp2;
2029 struct nchashhead *bucket;
2030 struct namecache *ncp, *nnp;
2031 u_long i, j, n_nchash;
2034 /* Scan hash tables for applicable entries */
2035 SDT_PROBE1(vfs, namecache, purgevfs, done, mp);
2036 if (!force && mp->mnt_nvnodelistsize <= ncpurgeminvnodes)
2039 n_nchash = nchash + 1;
2041 for (i = 0; i < numbucketlocks; i++) {
2042 blp = (struct rwlock *)&bucketlocks[i];
2044 for (j = i; j < n_nchash; j += numbucketlocks) {
2046 bucket = &nchashtbl[j];
2047 LIST_FOREACH_SAFE(ncp, bucket, nc_hash, nnp) {
2048 cache_assert_bucket_locked(ncp, RA_WLOCKED);
2049 if (ncp->nc_dvp->v_mount != mp)
2051 error = cache_zap_wlocked_bucket_kl(ncp, blp,
2055 TAILQ_INSERT_HEAD(&ncps, ncp, nc_dst);
2059 if (vlp1 == NULL && vlp2 == NULL)
2060 cache_maybe_yield();
2067 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2073 * Perform canonical checks and cache lookup and pass on to filesystem
2074 * through the vop_cachedlookup only if needed.
2078 vfs_cache_lookup(struct vop_lookup_args *ap)
2082 struct vnode **vpp = ap->a_vpp;
2083 struct componentname *cnp = ap->a_cnp;
2084 struct ucred *cred = cnp->cn_cred;
2085 int flags = cnp->cn_flags;
2086 struct thread *td = cnp->cn_thread;
2091 if (dvp->v_type != VDIR)
2094 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
2095 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
2098 error = VOP_ACCESS(dvp, VEXEC, cred, td);
2102 error = cache_lookup(dvp, vpp, cnp, NULL, NULL);
2104 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
2111 * XXX All of these sysctls would probably be more productive dead.
2113 static int __read_mostly disablecwd;
2114 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0,
2115 "Disable the getcwd syscall");
2117 /* Implementation of the getcwd syscall. */
2119 sys___getcwd(struct thread *td, struct __getcwd_args *uap)
2122 return (kern___getcwd(td, uap->buf, UIO_USERSPACE, uap->buflen,
2127 kern___getcwd(struct thread *td, char *buf, enum uio_seg bufseg, size_t buflen,
2131 struct filedesc *fdp;
2132 struct vnode *cdir, *rdir;
2135 if (__predict_false(disablecwd))
2137 if (__predict_false(buflen < 2))
2139 if (buflen > path_max)
2142 tmpbuf = malloc(buflen, M_TEMP, M_WAITOK);
2143 fdp = td->td_proc->p_fd;
2144 FILEDESC_SLOCK(fdp);
2145 cdir = fdp->fd_cdir;
2147 rdir = fdp->fd_rdir;
2149 FILEDESC_SUNLOCK(fdp);
2150 error = vn_fullpath1(td, cdir, rdir, tmpbuf, &bp, buflen);
2155 if (bufseg == UIO_SYSSPACE)
2156 bcopy(bp, buf, strlen(bp) + 1);
2158 error = copyout(bp, buf, strlen(bp) + 1);
2160 if (KTRPOINT(curthread, KTR_NAMEI))
2164 free(tmpbuf, M_TEMP);
2169 * Thus begins the fullpath magic.
2172 static int __read_mostly disablefullpath;
2173 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW, &disablefullpath, 0,
2174 "Disable the vn_fullpath function");
2177 * Retrieve the full filesystem path that correspond to a vnode from the name
2178 * cache (if available)
2181 vn_fullpath(struct thread *td, struct vnode *vn, char **retbuf, char **freebuf)
2184 struct filedesc *fdp;
2188 if (__predict_false(disablefullpath))
2190 if (__predict_false(vn == NULL))
2193 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
2194 fdp = td->td_proc->p_fd;
2195 FILEDESC_SLOCK(fdp);
2196 rdir = fdp->fd_rdir;
2198 FILEDESC_SUNLOCK(fdp);
2199 error = vn_fullpath1(td, vn, rdir, buf, retbuf, MAXPATHLEN);
2210 * This function is similar to vn_fullpath, but it attempts to lookup the
2211 * pathname relative to the global root mount point. This is required for the
2212 * auditing sub-system, as audited pathnames must be absolute, relative to the
2213 * global root mount point.
2216 vn_fullpath_global(struct thread *td, struct vnode *vn,
2217 char **retbuf, char **freebuf)
2222 if (__predict_false(disablefullpath))
2224 if (__predict_false(vn == NULL))
2226 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
2227 error = vn_fullpath1(td, vn, rootvnode, buf, retbuf, MAXPATHLEN);
2236 vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, u_int *buflen)
2239 struct namecache *ncp;
2243 vlp = VP2VNODELOCK(*vp);
2245 TAILQ_FOREACH(ncp, &((*vp)->v_cache_dst), nc_dst) {
2246 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2250 if (*buflen < ncp->nc_nlen) {
2253 counter_u64_add(numfullpathfail4, 1);
2255 SDT_PROBE3(vfs, namecache, fullpath, return, error,
2259 *buflen -= ncp->nc_nlen;
2260 memcpy(buf + *buflen, ncp->nc_name, ncp->nc_nlen);
2261 SDT_PROBE3(vfs, namecache, fullpath, hit, ncp->nc_dvp,
2270 SDT_PROBE1(vfs, namecache, fullpath, miss, vp);
2273 vn_lock(*vp, LK_SHARED | LK_RETRY);
2274 error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen);
2277 counter_u64_add(numfullpathfail2, 1);
2278 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2283 if (dvp->v_iflag & VI_DOOMED) {
2284 /* forced unmount */
2287 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2291 * *vp has its use count incremented still.
2298 * The magic behind kern___getcwd() and vn_fullpath().
2301 vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
2302 char *buf, char **retbuf, u_int buflen)
2304 int error, slash_prefixed;
2305 #ifdef KDTRACE_HOOKS
2306 struct vnode *startvp = vp;
2315 SDT_PROBE1(vfs, namecache, fullpath, entry, vp);
2316 counter_u64_add(numfullpathcalls, 1);
2318 if (vp->v_type != VDIR) {
2319 error = vn_vptocnp(&vp, td->td_ucred, buf, &buflen);
2326 buf[--buflen] = '/';
2329 while (vp != rdir && vp != rootvnode) {
2331 * The vp vnode must be already fully constructed,
2332 * since it is either found in namecache or obtained
2333 * from VOP_VPTOCNP(). We may test for VV_ROOT safely
2334 * without obtaining the vnode lock.
2336 if ((vp->v_vflag & VV_ROOT) != 0) {
2337 vn_lock(vp, LK_RETRY | LK_SHARED);
2340 * With the vnode locked, check for races with
2341 * unmount, forced or not. Note that we
2342 * already verified that vp is not equal to
2343 * the root vnode, which means that
2344 * mnt_vnodecovered can be NULL only for the
2347 if ((vp->v_iflag & VI_DOOMED) != 0 ||
2348 (vp1 = vp->v_mount->mnt_vnodecovered) == NULL ||
2349 vp1->v_mountedhere != vp->v_mount) {
2352 SDT_PROBE3(vfs, namecache, fullpath, return,
2362 if (vp->v_type != VDIR) {
2364 counter_u64_add(numfullpathfail1, 1);
2366 SDT_PROBE3(vfs, namecache, fullpath, return,
2370 error = vn_vptocnp(&vp, td->td_ucred, buf, &buflen);
2376 SDT_PROBE3(vfs, namecache, fullpath, return, error,
2380 buf[--buflen] = '/';
2385 if (!slash_prefixed) {
2388 counter_u64_add(numfullpathfail4, 1);
2389 SDT_PROBE3(vfs, namecache, fullpath, return, ENOMEM,
2393 buf[--buflen] = '/';
2395 counter_u64_add(numfullpathfound, 1);
2398 SDT_PROBE3(vfs, namecache, fullpath, return, 0, startvp, buf + buflen);
2399 *retbuf = buf + buflen;
2404 vn_dir_dd_ino(struct vnode *vp)
2406 struct namecache *ncp;
2410 ASSERT_VOP_LOCKED(vp, "vn_dir_dd_ino");
2411 vlp = VP2VNODELOCK(vp);
2413 TAILQ_FOREACH(ncp, &(vp->v_cache_dst), nc_dst) {
2414 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0)
2419 if (vget(ddvp, LK_SHARED | LK_NOWAIT | LK_VNHELD, curthread))
2428 vn_commname(struct vnode *vp, char *buf, u_int buflen)
2430 struct namecache *ncp;
2434 vlp = VP2VNODELOCK(vp);
2436 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst)
2437 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2443 l = min(ncp->nc_nlen, buflen - 1);
2444 memcpy(buf, ncp->nc_name, l);
2450 /* ABI compat shims for old kernel modules. */
2453 void cache_enter(struct vnode *dvp, struct vnode *vp,
2454 struct componentname *cnp);
2457 cache_enter(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2460 cache_enter_time(dvp, vp, cnp, NULL, NULL);
2464 * This function updates path string to vnode's full global path
2465 * and checks the size of the new path string against the pathlen argument.
2467 * Requires a locked, referenced vnode.
2468 * Vnode is re-locked on success or ENODEV, otherwise unlocked.
2470 * If sysctl debug.disablefullpath is set, ENODEV is returned,
2471 * vnode is left locked and path remain untouched.
2473 * If vp is a directory, the call to vn_fullpath_global() always succeeds
2474 * because it falls back to the ".." lookup if the namecache lookup fails.
2477 vn_path_to_global_path(struct thread *td, struct vnode *vp, char *path,
2480 struct nameidata nd;
2485 ASSERT_VOP_ELOCKED(vp, __func__);
2487 /* Return ENODEV if sysctl debug.disablefullpath==1 */
2488 if (__predict_false(disablefullpath))
2491 /* Construct global filesystem path from vp. */
2493 error = vn_fullpath_global(td, vp, &rpath, &fbuf);
2500 if (strlen(rpath) >= pathlen) {
2502 error = ENAMETOOLONG;
2507 * Re-lookup the vnode by path to detect a possible rename.
2508 * As a side effect, the vnode is relocked.
2509 * If vnode was renamed, return ENOENT.
2511 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
2512 UIO_SYSSPACE, path, td);
2518 NDFREE(&nd, NDF_ONLY_PNBUF);
2522 strcpy(path, rpath);