2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993, 1995
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Poul-Henning Kamp of the FreeBSD Project.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)vfs_cache.c 8.5 (Berkeley) 3/22/95
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
41 #include "opt_ktrace.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/counter.h>
46 #include <sys/filedesc.h>
47 #include <sys/fnv_hash.h>
48 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #include <sys/fcntl.h>
53 #include <sys/mount.h>
54 #include <sys/namei.h>
56 #include <sys/rwlock.h>
59 #include <sys/syscallsubr.h>
60 #include <sys/sysctl.h>
61 #include <sys/sysproto.h>
62 #include <sys/vnode.h>
64 #include <sys/ktrace.h>
73 SDT_PROVIDER_DECLARE(vfs);
74 SDT_PROBE_DEFINE3(vfs, namecache, enter, done, "struct vnode *", "char *",
76 SDT_PROBE_DEFINE2(vfs, namecache, enter_negative, done, "struct vnode *",
78 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, entry, "struct vnode *");
79 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, hit, "struct vnode *",
80 "char *", "struct vnode *");
81 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, miss, "struct vnode *");
82 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, return, "int",
83 "struct vnode *", "char *");
84 SDT_PROBE_DEFINE3(vfs, namecache, lookup, hit, "struct vnode *", "char *",
86 SDT_PROBE_DEFINE2(vfs, namecache, lookup, hit__negative,
87 "struct vnode *", "char *");
88 SDT_PROBE_DEFINE2(vfs, namecache, lookup, miss, "struct vnode *",
90 SDT_PROBE_DEFINE1(vfs, namecache, purge, done, "struct vnode *");
91 SDT_PROBE_DEFINE1(vfs, namecache, purge_negative, done, "struct vnode *");
92 SDT_PROBE_DEFINE1(vfs, namecache, purgevfs, done, "struct mount *");
93 SDT_PROBE_DEFINE3(vfs, namecache, zap, done, "struct vnode *", "char *",
95 SDT_PROBE_DEFINE3(vfs, namecache, zap_negative, done, "struct vnode *",
97 SDT_PROBE_DEFINE3(vfs, namecache, shrink_negative, done, "struct vnode *",
101 * This structure describes the elements in the cache of recent
102 * names looked up by namei.
106 LIST_ENTRY(namecache) nc_hash; /* hash chain */
107 LIST_ENTRY(namecache) nc_src; /* source vnode list */
108 TAILQ_ENTRY(namecache) nc_dst; /* destination vnode list */
109 struct vnode *nc_dvp; /* vnode of parent of name */
111 struct vnode *nu_vp; /* vnode the name refers to */
112 u_int nu_neghits; /* negative entry hits */
114 u_char nc_flag; /* flag bits */
115 u_char nc_nlen; /* length of name */
116 char nc_name[0]; /* segment name + nul */
120 * struct namecache_ts repeats struct namecache layout up to the
122 * struct namecache_ts is used in place of struct namecache when time(s) need
123 * to be stored. The nc_dotdottime field is used when a cache entry is mapping
124 * both a non-dotdot directory name plus dotdot for the directory's
127 struct namecache_ts {
128 struct timespec nc_time; /* timespec provided by fs */
129 struct timespec nc_dotdottime; /* dotdot timespec provided by fs */
130 int nc_ticks; /* ticks value when entry was added */
131 struct namecache nc_nc;
134 #define nc_vp n_un.nu_vp
135 #define nc_neghits n_un.nu_neghits
138 * Flags in namecache.nc_flag
140 #define NCF_WHITE 0x01
141 #define NCF_ISDOTDOT 0x02
144 #define NCF_DVDROP 0x10
145 #define NCF_NEGATIVE 0x20
146 #define NCF_HOTNEGATIVE 0x40
149 * Name caching works as follows:
151 * Names found by directory scans are retained in a cache
152 * for future reference. It is managed LRU, so frequently
153 * used names will hang around. Cache is indexed by hash value
154 * obtained from (dvp, name) where dvp refers to the directory
157 * If it is a "negative" entry, (i.e. for a name that is known NOT to
158 * exist) the vnode pointer will be NULL.
160 * Upon reaching the last segment of a path, if the reference
161 * is for DELETE, or NOCACHE is set (rewrite), and the
162 * name is located in the cache, it will be dropped.
164 * These locks are used (in the order in which they can be taken):
166 * vnodelock mtx vnode lists and v_cache_dd field protection
167 * bucketlock rwlock for access to given set of hash buckets
168 * neglist mtx negative entry LRU management
170 * Additionally, ncneg_shrink_lock mtx is used to have at most one thread
171 * shrinking the LRU list.
173 * It is legal to take multiple vnodelock and bucketlock locks. The locking
174 * order is lower address first. Both are recursive.
176 * "." lookups are lockless.
178 * ".." and vnode -> name lookups require vnodelock.
180 * name -> vnode lookup requires the relevant bucketlock to be held for reading.
182 * Insertions and removals of entries require involved vnodes and bucketlocks
183 * to be write-locked to prevent other threads from seeing the entry.
185 * Some lookups result in removal of the found entry (e.g. getting rid of a
186 * negative entry with the intent to create a positive one), which poses a
187 * problem when multiple threads reach the state. Similarly, two different
188 * threads can purge two different vnodes and try to remove the same name.
190 * If the already held vnode lock is lower than the second required lock, we
191 * can just take the other lock. However, in the opposite case, this could
192 * deadlock. As such, this is resolved by trylocking and if that fails unlocking
193 * the first node, locking everything in order and revalidating the state.
197 * Structures associated with name caching.
199 #define NCHHASH(hash) \
200 (&nchashtbl[(hash) & nchash])
201 static __read_mostly LIST_HEAD(nchashhead, namecache) *nchashtbl;/* Hash Table */
202 static u_long __read_mostly nchash; /* size of hash table */
203 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0,
204 "Size of namecache hash table");
205 static u_long __read_mostly ncnegfactor = 12; /* ratio of negative entries */
206 SYSCTL_ULONG(_vfs, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0,
207 "Ratio of negative namecache entries");
208 static u_long __exclusive_cache_line numneg; /* number of negative entries allocated */
209 SYSCTL_ULONG(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0,
210 "Number of negative entries in namecache");
211 static u_long __exclusive_cache_line numcache;/* number of cache entries allocated */
212 SYSCTL_ULONG(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0,
213 "Number of namecache entries");
214 static u_long __exclusive_cache_line numcachehv;/* number of cache entries with vnodes held */
215 SYSCTL_ULONG(_debug, OID_AUTO, numcachehv, CTLFLAG_RD, &numcachehv, 0,
216 "Number of namecache entries with vnodes held");
217 u_int __read_mostly ncsizefactor = 2;
218 SYSCTL_UINT(_vfs, OID_AUTO, ncsizefactor, CTLFLAG_RW, &ncsizefactor, 0,
219 "Size factor for namecache");
220 static u_int __read_mostly ncpurgeminvnodes;
221 SYSCTL_UINT(_vfs, OID_AUTO, ncpurgeminvnodes, CTLFLAG_RW, &ncpurgeminvnodes, 0,
222 "Number of vnodes below which purgevfs ignores the request");
223 static u_int __read_mostly ncneghitsrequeue = 8;
224 SYSCTL_UINT(_vfs, OID_AUTO, ncneghitsrequeue, CTLFLAG_RW, &ncneghitsrequeue, 0,
225 "Number of hits to requeue a negative entry in the LRU list");
227 struct nchstats nchstats; /* cache effectiveness statistics */
229 static struct mtx __exclusive_cache_line ncneg_shrink_lock;
230 static int shrink_list_turn;
234 TAILQ_HEAD(, namecache) nl_list;
235 } __aligned(CACHE_LINE_SIZE);
237 static struct neglist __read_mostly *neglists;
238 static struct neglist ncneg_hot;
239 static u_long numhotneg;
241 #define numneglists (ncneghash + 1)
242 static u_int __read_mostly ncneghash;
243 static inline struct neglist *
244 NCP2NEGLIST(struct namecache *ncp)
247 return (&neglists[(((uintptr_t)(ncp) >> 8) & ncneghash)]);
250 #define numbucketlocks (ncbuckethash + 1)
251 static u_int __read_mostly ncbuckethash;
252 static struct rwlock_padalign __read_mostly *bucketlocks;
253 #define HASH2BUCKETLOCK(hash) \
254 ((struct rwlock *)(&bucketlocks[((hash) & ncbuckethash)]))
256 #define numvnodelocks (ncvnodehash + 1)
257 static u_int __read_mostly ncvnodehash;
258 static struct mtx __read_mostly *vnodelocks;
259 static inline struct mtx *
260 VP2VNODELOCK(struct vnode *vp)
263 return (&vnodelocks[(((uintptr_t)(vp) >> 8) & ncvnodehash)]);
267 * UMA zones for the VFS cache.
269 * The small cache is used for entries with short names, which are the
270 * most common. The large cache is used for entries which are too big to
271 * fit in the small cache.
273 static uma_zone_t __read_mostly cache_zone_small;
274 static uma_zone_t __read_mostly cache_zone_small_ts;
275 static uma_zone_t __read_mostly cache_zone_large;
276 static uma_zone_t __read_mostly cache_zone_large_ts;
278 #define CACHE_PATH_CUTOFF 35
280 static struct namecache *
281 cache_alloc(int len, int ts)
283 struct namecache_ts *ncp_ts;
284 struct namecache *ncp;
286 if (__predict_false(ts)) {
287 if (len <= CACHE_PATH_CUTOFF)
288 ncp_ts = uma_zalloc(cache_zone_small_ts, M_WAITOK);
290 ncp_ts = uma_zalloc(cache_zone_large_ts, M_WAITOK);
291 ncp = &ncp_ts->nc_nc;
293 if (len <= CACHE_PATH_CUTOFF)
294 ncp = uma_zalloc(cache_zone_small, M_WAITOK);
296 ncp = uma_zalloc(cache_zone_large, M_WAITOK);
302 cache_free(struct namecache *ncp)
304 struct namecache_ts *ncp_ts;
308 if ((ncp->nc_flag & NCF_DVDROP) != 0)
310 if (__predict_false(ncp->nc_flag & NCF_TS)) {
311 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
312 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
313 uma_zfree(cache_zone_small_ts, ncp_ts);
315 uma_zfree(cache_zone_large_ts, ncp_ts);
317 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
318 uma_zfree(cache_zone_small, ncp);
320 uma_zfree(cache_zone_large, ncp);
325 cache_out_ts(struct namecache *ncp, struct timespec *tsp, int *ticksp)
327 struct namecache_ts *ncp_ts;
329 KASSERT((ncp->nc_flag & NCF_TS) != 0 ||
330 (tsp == NULL && ticksp == NULL),
333 if (tsp == NULL && ticksp == NULL)
336 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
338 *tsp = ncp_ts->nc_time;
340 *ticksp = ncp_ts->nc_ticks;
343 static int __read_mostly doingcache = 1; /* 1 => enable the cache */
344 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0,
345 "VFS namecache enabled");
347 /* Export size information to userland */
348 SYSCTL_INT(_debug_sizeof, OID_AUTO, namecache, CTLFLAG_RD, SYSCTL_NULL_INT_PTR,
349 sizeof(struct namecache), "sizeof(struct namecache)");
352 * The new name cache statistics
354 static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0,
355 "Name cache statistics");
356 #define STATNODE_ULONG(name, descr) \
357 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, descr);
358 #define STATNODE_COUNTER(name, descr) \
359 static counter_u64_t __read_mostly name; \
360 SYSCTL_COUNTER_U64(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, descr);
361 STATNODE_ULONG(numneg, "Number of negative cache entries");
362 STATNODE_ULONG(numcache, "Number of cache entries");
363 STATNODE_COUNTER(numcalls, "Number of cache lookups");
364 STATNODE_COUNTER(dothits, "Number of '.' hits");
365 STATNODE_COUNTER(dotdothits, "Number of '..' hits");
366 STATNODE_COUNTER(numchecks, "Number of checks in lookup");
367 STATNODE_COUNTER(nummiss, "Number of cache misses");
368 STATNODE_COUNTER(nummisszap, "Number of cache misses we do not want to cache");
369 STATNODE_COUNTER(numposzaps,
370 "Number of cache hits (positive) we do not want to cache");
371 STATNODE_COUNTER(numposhits, "Number of cache hits (positive)");
372 STATNODE_COUNTER(numnegzaps,
373 "Number of cache hits (negative) we do not want to cache");
374 STATNODE_COUNTER(numneghits, "Number of cache hits (negative)");
375 /* These count for kern___getcwd(), too. */
376 STATNODE_COUNTER(numfullpathcalls, "Number of fullpath search calls");
377 STATNODE_COUNTER(numfullpathfail1, "Number of fullpath search errors (ENOTDIR)");
378 STATNODE_COUNTER(numfullpathfail2,
379 "Number of fullpath search errors (VOP_VPTOCNP failures)");
380 STATNODE_COUNTER(numfullpathfail4, "Number of fullpath search errors (ENOMEM)");
381 STATNODE_COUNTER(numfullpathfound, "Number of successful fullpath calls");
382 static long numneg_evicted; STATNODE_ULONG(numneg_evicted,
383 "Number of negative entries evicted when adding a new entry");
384 STATNODE_COUNTER(zap_and_exit_bucket_relock_success,
385 "Number of successful removals after relocking");
386 static long zap_and_exit_bucket_fail; STATNODE_ULONG(zap_and_exit_bucket_fail,
387 "Number of times zap_and_exit failed to lock");
388 static long zap_and_exit_bucket_fail2; STATNODE_ULONG(zap_and_exit_bucket_fail2,
389 "Number of times zap_and_exit failed to lock");
390 static long cache_lock_vnodes_cel_3_failures;
391 STATNODE_ULONG(cache_lock_vnodes_cel_3_failures,
392 "Number of times 3-way vnode locking failed");
393 STATNODE_ULONG(numhotneg, "Number of hot negative entries");
395 static void cache_zap_locked(struct namecache *ncp, bool neg_locked);
396 static int vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
397 char *buf, char **retbuf, u_int buflen);
399 static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
401 static int cache_yield;
402 SYSCTL_INT(_vfs_cache, OID_AUTO, yield, CTLFLAG_RD, &cache_yield, 0,
403 "Number of times cache called yield");
405 static void __noinline
406 cache_maybe_yield(void)
409 if (should_yield()) {
411 kern_yield(PRI_USER);
416 cache_assert_vlp_locked(struct mtx *vlp)
420 mtx_assert(vlp, MA_OWNED);
424 cache_assert_vnode_locked(struct vnode *vp)
428 vlp = VP2VNODELOCK(vp);
429 cache_assert_vlp_locked(vlp);
433 cache_get_hash(char *name, u_char len, struct vnode *dvp)
437 hash = fnv_32_buf(name, len, FNV1_32_INIT);
438 hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
442 static inline struct rwlock *
443 NCP2BUCKETLOCK(struct namecache *ncp)
447 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen, ncp->nc_dvp);
448 return (HASH2BUCKETLOCK(hash));
453 cache_assert_bucket_locked(struct namecache *ncp, int mode)
457 blp = NCP2BUCKETLOCK(ncp);
458 rw_assert(blp, mode);
461 #define cache_assert_bucket_locked(x, y) do { } while (0)
464 #define cache_sort_vnodes(x, y) _cache_sort_vnodes((void **)(x), (void **)(y))
466 _cache_sort_vnodes(void **p1, void **p2)
470 MPASS(*p1 != NULL || *p2 != NULL);
480 cache_lock_all_buckets(void)
484 for (i = 0; i < numbucketlocks; i++)
485 rw_wlock(&bucketlocks[i]);
489 cache_unlock_all_buckets(void)
493 for (i = 0; i < numbucketlocks; i++)
494 rw_wunlock(&bucketlocks[i]);
498 cache_lock_all_vnodes(void)
502 for (i = 0; i < numvnodelocks; i++)
503 mtx_lock(&vnodelocks[i]);
507 cache_unlock_all_vnodes(void)
511 for (i = 0; i < numvnodelocks; i++)
512 mtx_unlock(&vnodelocks[i]);
516 cache_trylock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
519 cache_sort_vnodes(&vlp1, &vlp2);
522 if (!mtx_trylock(vlp1))
525 if (!mtx_trylock(vlp2)) {
535 cache_lock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
538 MPASS(vlp1 != NULL || vlp2 != NULL);
548 cache_unlock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
551 MPASS(vlp1 != NULL || vlp2 != NULL);
560 sysctl_nchstats(SYSCTL_HANDLER_ARGS)
562 struct nchstats snap;
564 if (req->oldptr == NULL)
565 return (SYSCTL_OUT(req, 0, sizeof(snap)));
568 snap.ncs_goodhits = counter_u64_fetch(numposhits);
569 snap.ncs_neghits = counter_u64_fetch(numneghits);
570 snap.ncs_badhits = counter_u64_fetch(numposzaps) +
571 counter_u64_fetch(numnegzaps);
572 snap.ncs_miss = counter_u64_fetch(nummisszap) +
573 counter_u64_fetch(nummiss);
575 return (SYSCTL_OUT(req, &snap, sizeof(snap)));
577 SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE | CTLFLAG_RD |
578 CTLFLAG_MPSAFE, 0, 0, sysctl_nchstats, "LU",
579 "VFS cache effectiveness statistics");
583 * Grab an atomic snapshot of the name cache hash chain lengths
585 static SYSCTL_NODE(_debug, OID_AUTO, hashstat, CTLFLAG_RW, NULL,
589 sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS)
591 struct nchashhead *ncpp;
592 struct namecache *ncp;
593 int i, error, n_nchash, *cntbuf;
596 n_nchash = nchash + 1; /* nchash is max index, not count */
597 if (req->oldptr == NULL)
598 return SYSCTL_OUT(req, 0, n_nchash * sizeof(int));
599 cntbuf = malloc(n_nchash * sizeof(int), M_TEMP, M_ZERO | M_WAITOK);
600 cache_lock_all_buckets();
601 if (n_nchash != nchash + 1) {
602 cache_unlock_all_buckets();
603 free(cntbuf, M_TEMP);
606 /* Scan hash tables counting entries */
607 for (ncpp = nchashtbl, i = 0; i < n_nchash; ncpp++, i++)
608 LIST_FOREACH(ncp, ncpp, nc_hash)
610 cache_unlock_all_buckets();
611 for (error = 0, i = 0; i < n_nchash; i++)
612 if ((error = SYSCTL_OUT(req, &cntbuf[i], sizeof(int))) != 0)
614 free(cntbuf, M_TEMP);
617 SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD|
618 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_rawnchash, "S,int",
619 "nchash chain lengths");
622 sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS)
625 struct nchashhead *ncpp;
626 struct namecache *ncp;
628 int count, maxlength, used, pct;
631 return SYSCTL_OUT(req, 0, 4 * sizeof(int));
633 cache_lock_all_buckets();
634 n_nchash = nchash + 1; /* nchash is max index, not count */
638 /* Scan hash tables for applicable entries */
639 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
641 LIST_FOREACH(ncp, ncpp, nc_hash) {
646 if (maxlength < count)
649 n_nchash = nchash + 1;
650 cache_unlock_all_buckets();
651 pct = (used * 100) / (n_nchash / 100);
652 error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash));
655 error = SYSCTL_OUT(req, &used, sizeof(used));
658 error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength));
661 error = SYSCTL_OUT(req, &pct, sizeof(pct));
666 SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD|
667 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_nchash, "I",
668 "nchash statistics (number of total/used buckets, maximum chain length, usage percentage)");
672 * Negative entries management
674 * A variation of LRU scheme is used. New entries are hashed into one of
675 * numneglists cold lists. Entries get promoted to the hot list on first hit.
676 * Partial LRU for the hot list is maintained by requeueing them every
677 * ncneghitsrequeue hits.
679 * The shrinker will demote hot list head and evict from the cold list in a
680 * round-robin manner.
683 cache_negative_hit(struct namecache *ncp)
685 struct neglist *neglist;
688 MPASS(ncp->nc_flag & NCF_NEGATIVE);
689 hits = atomic_fetchadd_int(&ncp->nc_neghits, 1);
690 if (ncp->nc_flag & NCF_HOTNEGATIVE) {
691 if ((hits % ncneghitsrequeue) != 0)
693 mtx_lock(&ncneg_hot.nl_lock);
694 if (ncp->nc_flag & NCF_HOTNEGATIVE) {
695 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
696 TAILQ_INSERT_TAIL(&ncneg_hot.nl_list, ncp, nc_dst);
697 mtx_unlock(&ncneg_hot.nl_lock);
701 * The shrinker cleared the flag and removed the entry from
702 * the hot list. Put it back.
705 mtx_lock(&ncneg_hot.nl_lock);
707 neglist = NCP2NEGLIST(ncp);
708 mtx_lock(&neglist->nl_lock);
709 if (!(ncp->nc_flag & NCF_HOTNEGATIVE)) {
711 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
712 TAILQ_INSERT_TAIL(&ncneg_hot.nl_list, ncp, nc_dst);
713 ncp->nc_flag |= NCF_HOTNEGATIVE;
715 mtx_unlock(&neglist->nl_lock);
716 mtx_unlock(&ncneg_hot.nl_lock);
720 cache_negative_insert(struct namecache *ncp, bool neg_locked)
722 struct neglist *neglist;
724 MPASS(ncp->nc_flag & NCF_NEGATIVE);
725 cache_assert_bucket_locked(ncp, RA_WLOCKED);
726 neglist = NCP2NEGLIST(ncp);
728 mtx_lock(&neglist->nl_lock);
730 mtx_assert(&neglist->nl_lock, MA_OWNED);
732 TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst);
734 mtx_unlock(&neglist->nl_lock);
735 atomic_add_rel_long(&numneg, 1);
739 cache_negative_remove(struct namecache *ncp, bool neg_locked)
741 struct neglist *neglist;
742 bool hot_locked = false;
743 bool list_locked = false;
745 MPASS(ncp->nc_flag & NCF_NEGATIVE);
746 cache_assert_bucket_locked(ncp, RA_WLOCKED);
747 neglist = NCP2NEGLIST(ncp);
749 if (ncp->nc_flag & NCF_HOTNEGATIVE) {
751 mtx_lock(&ncneg_hot.nl_lock);
752 if (!(ncp->nc_flag & NCF_HOTNEGATIVE)) {
754 mtx_lock(&neglist->nl_lock);
758 mtx_lock(&neglist->nl_lock);
761 if (ncp->nc_flag & NCF_HOTNEGATIVE) {
762 mtx_assert(&ncneg_hot.nl_lock, MA_OWNED);
763 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
766 mtx_assert(&neglist->nl_lock, MA_OWNED);
767 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
770 mtx_unlock(&neglist->nl_lock);
772 mtx_unlock(&ncneg_hot.nl_lock);
773 atomic_subtract_rel_long(&numneg, 1);
777 cache_negative_shrink_select(int start, struct namecache **ncpp,
778 struct neglist **neglistpp)
780 struct neglist *neglist;
781 struct namecache *ncp;
787 for (i = start; i < numneglists; i++) {
788 neglist = &neglists[i];
789 if (TAILQ_FIRST(&neglist->nl_list) == NULL)
791 mtx_lock(&neglist->nl_lock);
792 ncp = TAILQ_FIRST(&neglist->nl_list);
795 mtx_unlock(&neglist->nl_lock);
798 *neglistpp = neglist;
803 cache_negative_zap_one(void)
805 struct namecache *ncp, *ncp2;
806 struct neglist *neglist;
810 if (mtx_owner(&ncneg_shrink_lock) != NULL ||
811 !mtx_trylock(&ncneg_shrink_lock))
814 mtx_lock(&ncneg_hot.nl_lock);
815 ncp = TAILQ_FIRST(&ncneg_hot.nl_list);
817 neglist = NCP2NEGLIST(ncp);
818 mtx_lock(&neglist->nl_lock);
819 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
820 TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst);
821 ncp->nc_flag &= ~NCF_HOTNEGATIVE;
823 mtx_unlock(&neglist->nl_lock);
825 mtx_unlock(&ncneg_hot.nl_lock);
827 cache_negative_shrink_select(shrink_list_turn, &ncp, &neglist);
829 if (shrink_list_turn == numneglists)
830 shrink_list_turn = 0;
831 if (ncp == NULL && shrink_list_turn == 0)
832 cache_negative_shrink_select(shrink_list_turn, &ncp, &neglist);
836 MPASS(ncp->nc_flag & NCF_NEGATIVE);
837 dvlp = VP2VNODELOCK(ncp->nc_dvp);
838 blp = NCP2BUCKETLOCK(ncp);
839 mtx_unlock(&neglist->nl_lock);
842 mtx_lock(&neglist->nl_lock);
843 ncp2 = TAILQ_FIRST(&neglist->nl_list);
844 if (ncp != ncp2 || dvlp != VP2VNODELOCK(ncp2->nc_dvp) ||
845 blp != NCP2BUCKETLOCK(ncp2) || !(ncp2->nc_flag & NCF_NEGATIVE)) {
849 SDT_PROBE3(vfs, namecache, shrink_negative, done, ncp->nc_dvp,
850 ncp->nc_name, ncp->nc_neghits);
852 cache_zap_locked(ncp, true);
855 mtx_unlock(&neglist->nl_lock);
859 mtx_unlock(&ncneg_shrink_lock);
864 * cache_zap_locked():
866 * Removes a namecache entry from cache, whether it contains an actual
867 * pointer to a vnode or if it is just a negative cache entry.
870 cache_zap_locked(struct namecache *ncp, bool neg_locked)
873 if (!(ncp->nc_flag & NCF_NEGATIVE))
874 cache_assert_vnode_locked(ncp->nc_vp);
875 cache_assert_vnode_locked(ncp->nc_dvp);
876 cache_assert_bucket_locked(ncp, RA_WLOCKED);
878 CTR2(KTR_VFS, "cache_zap(%p) vp %p", ncp,
879 (ncp->nc_flag & NCF_NEGATIVE) ? NULL : ncp->nc_vp);
880 LIST_REMOVE(ncp, nc_hash);
881 if (!(ncp->nc_flag & NCF_NEGATIVE)) {
882 SDT_PROBE3(vfs, namecache, zap, done, ncp->nc_dvp,
883 ncp->nc_name, ncp->nc_vp);
884 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
885 if (ncp == ncp->nc_vp->v_cache_dd)
886 ncp->nc_vp->v_cache_dd = NULL;
888 SDT_PROBE3(vfs, namecache, zap_negative, done, ncp->nc_dvp,
889 ncp->nc_name, ncp->nc_neghits);
890 cache_negative_remove(ncp, neg_locked);
892 if (ncp->nc_flag & NCF_ISDOTDOT) {
893 if (ncp == ncp->nc_dvp->v_cache_dd)
894 ncp->nc_dvp->v_cache_dd = NULL;
896 LIST_REMOVE(ncp, nc_src);
897 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) {
898 ncp->nc_flag |= NCF_DVDROP;
899 atomic_subtract_rel_long(&numcachehv, 1);
902 atomic_subtract_rel_long(&numcache, 1);
906 cache_zap_negative_locked_vnode_kl(struct namecache *ncp, struct vnode *vp)
910 MPASS(ncp->nc_dvp == vp);
911 MPASS(ncp->nc_flag & NCF_NEGATIVE);
912 cache_assert_vnode_locked(vp);
914 blp = NCP2BUCKETLOCK(ncp);
916 cache_zap_locked(ncp, false);
921 cache_zap_locked_vnode_kl2(struct namecache *ncp, struct vnode *vp,
924 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
927 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
928 cache_assert_vnode_locked(vp);
930 if (ncp->nc_flag & NCF_NEGATIVE) {
935 cache_zap_negative_locked_vnode_kl(ncp, vp);
939 pvlp = VP2VNODELOCK(vp);
940 blp = NCP2BUCKETLOCK(ncp);
941 vlp1 = VP2VNODELOCK(ncp->nc_dvp);
942 vlp2 = VP2VNODELOCK(ncp->nc_vp);
944 if (*vlpp == vlp1 || *vlpp == vlp2) {
952 cache_sort_vnodes(&vlp1, &vlp2);
957 if (!mtx_trylock(vlp1))
963 cache_zap_locked(ncp, false);
965 if (to_unlock != NULL)
966 mtx_unlock(to_unlock);
973 MPASS(*vlpp == NULL);
978 static int __noinline
979 cache_zap_locked_vnode(struct namecache *ncp, struct vnode *vp)
981 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
985 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
986 cache_assert_vnode_locked(vp);
988 pvlp = VP2VNODELOCK(vp);
989 if (ncp->nc_flag & NCF_NEGATIVE) {
990 cache_zap_negative_locked_vnode_kl(ncp, vp);
994 blp = NCP2BUCKETLOCK(ncp);
995 vlp1 = VP2VNODELOCK(ncp->nc_dvp);
996 vlp2 = VP2VNODELOCK(ncp->nc_vp);
997 cache_sort_vnodes(&vlp1, &vlp2);
1002 if (!mtx_trylock(vlp1)) {
1009 cache_zap_locked(ncp, false);
1011 mtx_unlock(to_unlock);
1018 * If trylocking failed we can get here. We know enough to take all needed locks
1019 * in the right order and re-lookup the entry.
1022 cache_zap_unlocked_bucket(struct namecache *ncp, struct componentname *cnp,
1023 struct vnode *dvp, struct mtx *dvlp, struct mtx *vlp, uint32_t hash,
1026 struct namecache *rncp;
1028 cache_assert_bucket_locked(ncp, RA_UNLOCKED);
1030 cache_sort_vnodes(&dvlp, &vlp);
1031 cache_lock_vnodes(dvlp, vlp);
1033 LIST_FOREACH(rncp, (NCHHASH(hash)), nc_hash) {
1034 if (rncp == ncp && rncp->nc_dvp == dvp &&
1035 rncp->nc_nlen == cnp->cn_namelen &&
1036 !bcmp(rncp->nc_name, cnp->cn_nameptr, rncp->nc_nlen))
1040 cache_zap_locked(rncp, false);
1042 cache_unlock_vnodes(dvlp, vlp);
1043 counter_u64_add(zap_and_exit_bucket_relock_success, 1);
1048 cache_unlock_vnodes(dvlp, vlp);
1052 static int __noinline
1053 cache_zap_wlocked_bucket(struct namecache *ncp, struct componentname *cnp,
1054 uint32_t hash, struct rwlock *blp)
1056 struct mtx *dvlp, *vlp;
1059 cache_assert_bucket_locked(ncp, RA_WLOCKED);
1061 dvlp = VP2VNODELOCK(ncp->nc_dvp);
1063 if (!(ncp->nc_flag & NCF_NEGATIVE))
1064 vlp = VP2VNODELOCK(ncp->nc_vp);
1065 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1066 cache_zap_locked(ncp, false);
1068 cache_unlock_vnodes(dvlp, vlp);
1074 return (cache_zap_unlocked_bucket(ncp, cnp, dvp, dvlp, vlp, hash, blp));
1077 static int __noinline
1078 cache_zap_rlocked_bucket(struct namecache *ncp, struct componentname *cnp,
1079 uint32_t hash, struct rwlock *blp)
1081 struct mtx *dvlp, *vlp;
1084 cache_assert_bucket_locked(ncp, RA_RLOCKED);
1086 dvlp = VP2VNODELOCK(ncp->nc_dvp);
1088 if (!(ncp->nc_flag & NCF_NEGATIVE))
1089 vlp = VP2VNODELOCK(ncp->nc_vp);
1090 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1093 cache_zap_locked(ncp, false);
1095 cache_unlock_vnodes(dvlp, vlp);
1101 return (cache_zap_unlocked_bucket(ncp, cnp, dvp, dvlp, vlp, hash, blp));
1105 cache_zap_wlocked_bucket_kl(struct namecache *ncp, struct rwlock *blp,
1106 struct mtx **vlpp1, struct mtx **vlpp2)
1108 struct mtx *dvlp, *vlp;
1110 cache_assert_bucket_locked(ncp, RA_WLOCKED);
1112 dvlp = VP2VNODELOCK(ncp->nc_dvp);
1114 if (!(ncp->nc_flag & NCF_NEGATIVE))
1115 vlp = VP2VNODELOCK(ncp->nc_vp);
1116 cache_sort_vnodes(&dvlp, &vlp);
1118 if (*vlpp1 == dvlp && *vlpp2 == vlp) {
1119 cache_zap_locked(ncp, false);
1120 cache_unlock_vnodes(dvlp, vlp);
1133 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1134 cache_zap_locked(ncp, false);
1135 cache_unlock_vnodes(dvlp, vlp);
1150 cache_lookup_unlock(struct rwlock *blp, struct mtx *vlp)
1160 static int __noinline
1161 cache_lookup_dot(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1162 struct timespec *tsp, int *ticksp)
1167 CTR2(KTR_VFS, "cache_lookup(%p, %s) found via .",
1168 dvp, cnp->cn_nameptr);
1169 counter_u64_add(dothits, 1);
1170 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", *vpp);
1177 * When we lookup "." we still can be asked to lock it
1180 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
1181 if (ltype != VOP_ISLOCKED(*vpp)) {
1182 if (ltype == LK_EXCLUSIVE) {
1183 vn_lock(*vpp, LK_UPGRADE | LK_RETRY);
1184 if ((*vpp)->v_iflag & VI_DOOMED) {
1185 /* forced unmount */
1191 vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY);
1196 static __noinline int
1197 cache_lookup_nomakeentry(struct vnode *dvp, struct vnode **vpp,
1198 struct componentname *cnp, struct timespec *tsp, int *ticksp)
1200 struct namecache *ncp;
1202 struct mtx *dvlp, *dvlp2;
1206 if (cnp->cn_namelen == 2 &&
1207 cnp->cn_nameptr[0] == '.' && cnp->cn_nameptr[1] == '.') {
1208 counter_u64_add(dotdothits, 1);
1209 dvlp = VP2VNODELOCK(dvp);
1213 ncp = dvp->v_cache_dd;
1215 SDT_PROBE3(vfs, namecache, lookup, miss, dvp,
1222 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1223 if (ncp->nc_dvp != dvp)
1224 panic("dvp %p v_cache_dd %p\n", dvp, ncp);
1225 if (!cache_zap_locked_vnode_kl2(ncp,
1228 MPASS(dvp->v_cache_dd == NULL);
1234 dvp->v_cache_dd = NULL;
1242 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1243 blp = HASH2BUCKETLOCK(hash);
1245 if (LIST_EMPTY(NCHHASH(hash)))
1250 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1251 counter_u64_add(numchecks, 1);
1252 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1253 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1257 /* We failed to find an entry */
1263 error = cache_zap_wlocked_bucket(ncp, cnp, hash, blp);
1264 if (__predict_false(error != 0)) {
1265 zap_and_exit_bucket_fail++;
1266 cache_maybe_yield();
1269 counter_u64_add(numposzaps, 1);
1273 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr, NULL);
1274 counter_u64_add(nummisszap, 1);
1279 * Lookup a name in the name cache
1283 * - dvp: Parent directory in which to search.
1284 * - vpp: Return argument. Will contain desired vnode on cache hit.
1285 * - cnp: Parameters of the name search. The most interesting bits of
1286 * the cn_flags field have the following meanings:
1287 * - MAKEENTRY: If clear, free an entry from the cache rather than look
1289 * - ISDOTDOT: Must be set if and only if cn_nameptr == ".."
1290 * - tsp: Return storage for cache timestamp. On a successful (positive
1291 * or negative) lookup, tsp will be filled with any timespec that
1292 * was stored when this cache entry was created. However, it will
1293 * be clear for "." entries.
1294 * - ticks: Return storage for alternate cache timestamp. On a successful
1295 * (positive or negative) lookup, it will contain the ticks value
1296 * that was current when the cache entry was created, unless cnp
1301 * - -1: A positive cache hit. vpp will contain the desired vnode.
1302 * - ENOENT: A negative cache hit, or dvp was recycled out from under us due
1303 * to a forced unmount. vpp will not be modified. If the entry
1304 * is a whiteout, then the ISWHITEOUT flag will be set in
1306 * - 0: A cache miss. vpp will not be modified.
1310 * On a cache hit, vpp will be returned locked and ref'd. If we're looking up
1311 * .., dvp is unlocked. If we're looking up . an extra ref is taken, but the
1312 * lock is not recursively acquired.
1315 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1316 struct timespec *tsp, int *ticksp)
1318 struct namecache_ts *ncp_ts;
1319 struct namecache *ncp;
1326 if (__predict_false(!doingcache)) {
1327 cnp->cn_flags &= ~MAKEENTRY;
1331 counter_u64_add(numcalls, 1);
1333 if (__predict_false(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.'))
1334 return (cache_lookup_dot(dvp, vpp, cnp, tsp, ticksp));
1336 if ((cnp->cn_flags & MAKEENTRY) == 0)
1337 return (cache_lookup_nomakeentry(dvp, vpp, cnp, tsp, ticksp));
1343 if (cnp->cn_namelen == 2 &&
1344 cnp->cn_nameptr[0] == '.' && cnp->cn_nameptr[1] == '.') {
1345 counter_u64_add(dotdothits, 1);
1346 dvlp = VP2VNODELOCK(dvp);
1348 ncp = dvp->v_cache_dd;
1350 SDT_PROBE3(vfs, namecache, lookup, miss, dvp,
1355 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1356 if (ncp->nc_flag & NCF_NEGATIVE)
1362 /* Return failure if negative entry was found. */
1364 goto negative_success;
1365 CTR3(KTR_VFS, "cache_lookup(%p, %s) found %p via ..",
1366 dvp, cnp->cn_nameptr, *vpp);
1367 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, "..",
1369 cache_out_ts(ncp, tsp, ticksp);
1370 if ((ncp->nc_flag & (NCF_ISDOTDOT | NCF_DTS)) ==
1371 NCF_DTS && tsp != NULL) {
1372 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1373 *tsp = ncp_ts->nc_dotdottime;
1378 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1379 blp = HASH2BUCKETLOCK(hash);
1382 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1383 counter_u64_add(numchecks, 1);
1384 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1385 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1389 /* We failed to find an entry */
1390 if (__predict_false(ncp == NULL)) {
1392 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
1394 counter_u64_add(nummiss, 1);
1398 if (ncp->nc_flag & NCF_NEGATIVE)
1399 goto negative_success;
1401 /* We found a "positive" match, return the vnode */
1402 counter_u64_add(numposhits, 1);
1404 CTR4(KTR_VFS, "cache_lookup(%p, %s) found %p via ncp %p",
1405 dvp, cnp->cn_nameptr, *vpp, ncp);
1406 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name,
1408 cache_out_ts(ncp, tsp, ticksp);
1411 * On success we return a locked and ref'd vnode as per the lookup
1415 ltype = 0; /* silence gcc warning */
1416 if (cnp->cn_flags & ISDOTDOT) {
1417 ltype = VOP_ISLOCKED(dvp);
1420 vs = vget_prep(*vpp);
1421 cache_lookup_unlock(blp, dvlp);
1422 error = vget_finish(*vpp, cnp->cn_lkflags, vs);
1423 if (cnp->cn_flags & ISDOTDOT) {
1424 vn_lock(dvp, ltype | LK_RETRY);
1425 if (dvp->v_iflag & VI_DOOMED) {
1436 if ((cnp->cn_flags & ISLASTCN) &&
1437 (cnp->cn_lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE) {
1438 ASSERT_VOP_ELOCKED(*vpp, "cache_lookup");
1443 /* We found a negative match, and want to create it, so purge */
1444 if (cnp->cn_nameiop == CREATE) {
1445 counter_u64_add(numnegzaps, 1);
1449 counter_u64_add(numneghits, 1);
1450 cache_negative_hit(ncp);
1451 if (ncp->nc_flag & NCF_WHITE)
1452 cnp->cn_flags |= ISWHITEOUT;
1453 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp,
1455 cache_out_ts(ncp, tsp, ticksp);
1456 cache_lookup_unlock(blp, dvlp);
1461 error = cache_zap_rlocked_bucket(ncp, cnp, hash, blp);
1463 error = cache_zap_locked_vnode(ncp, dvp);
1464 if (__predict_false(error != 0)) {
1465 zap_and_exit_bucket_fail2++;
1466 cache_maybe_yield();
1473 struct celockstate {
1475 struct rwlock *blp[2];
1477 CTASSERT((nitems(((struct celockstate *)0)->vlp) == 3));
1478 CTASSERT((nitems(((struct celockstate *)0)->blp) == 2));
1481 cache_celockstate_init(struct celockstate *cel)
1484 bzero(cel, sizeof(*cel));
1488 cache_lock_vnodes_cel(struct celockstate *cel, struct vnode *vp,
1491 struct mtx *vlp1, *vlp2;
1493 MPASS(cel->vlp[0] == NULL);
1494 MPASS(cel->vlp[1] == NULL);
1495 MPASS(cel->vlp[2] == NULL);
1497 MPASS(vp != NULL || dvp != NULL);
1499 vlp1 = VP2VNODELOCK(vp);
1500 vlp2 = VP2VNODELOCK(dvp);
1501 cache_sort_vnodes(&vlp1, &vlp2);
1512 cache_unlock_vnodes_cel(struct celockstate *cel)
1515 MPASS(cel->vlp[0] != NULL || cel->vlp[1] != NULL);
1517 if (cel->vlp[0] != NULL)
1518 mtx_unlock(cel->vlp[0]);
1519 if (cel->vlp[1] != NULL)
1520 mtx_unlock(cel->vlp[1]);
1521 if (cel->vlp[2] != NULL)
1522 mtx_unlock(cel->vlp[2]);
1526 cache_lock_vnodes_cel_3(struct celockstate *cel, struct vnode *vp)
1531 cache_assert_vlp_locked(cel->vlp[0]);
1532 cache_assert_vlp_locked(cel->vlp[1]);
1533 MPASS(cel->vlp[2] == NULL);
1536 vlp = VP2VNODELOCK(vp);
1539 if (vlp >= cel->vlp[1]) {
1542 if (mtx_trylock(vlp))
1544 cache_lock_vnodes_cel_3_failures++;
1545 cache_unlock_vnodes_cel(cel);
1546 if (vlp < cel->vlp[0]) {
1548 mtx_lock(cel->vlp[0]);
1549 mtx_lock(cel->vlp[1]);
1551 if (cel->vlp[0] != NULL)
1552 mtx_lock(cel->vlp[0]);
1554 mtx_lock(cel->vlp[1]);
1564 cache_lock_buckets_cel(struct celockstate *cel, struct rwlock *blp1,
1565 struct rwlock *blp2)
1568 MPASS(cel->blp[0] == NULL);
1569 MPASS(cel->blp[1] == NULL);
1571 cache_sort_vnodes(&blp1, &blp2);
1582 cache_unlock_buckets_cel(struct celockstate *cel)
1585 if (cel->blp[0] != NULL)
1586 rw_wunlock(cel->blp[0]);
1587 rw_wunlock(cel->blp[1]);
1591 * Lock part of the cache affected by the insertion.
1593 * This means vnodelocks for dvp, vp and the relevant bucketlock.
1594 * However, insertion can result in removal of an old entry. In this
1595 * case we have an additional vnode and bucketlock pair to lock. If the
1596 * entry is negative, ncelock is locked instead of the vnode.
1598 * That is, in the worst case we have to lock 3 vnodes and 2 bucketlocks, while
1599 * preserving the locking order (smaller address first).
1602 cache_enter_lock(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1605 struct namecache *ncp;
1606 struct rwlock *blps[2];
1608 blps[0] = HASH2BUCKETLOCK(hash);
1611 cache_lock_vnodes_cel(cel, dvp, vp);
1612 if (vp == NULL || vp->v_type != VDIR)
1614 ncp = vp->v_cache_dd;
1617 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1619 MPASS(ncp->nc_dvp == vp);
1620 blps[1] = NCP2BUCKETLOCK(ncp);
1621 if (ncp->nc_flag & NCF_NEGATIVE)
1623 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1626 * All vnodes got re-locked. Re-validate the state and if
1627 * nothing changed we are done. Otherwise restart.
1629 if (ncp == vp->v_cache_dd &&
1630 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1631 blps[1] == NCP2BUCKETLOCK(ncp) &&
1632 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1634 cache_unlock_vnodes_cel(cel);
1639 cache_lock_buckets_cel(cel, blps[0], blps[1]);
1643 cache_enter_lock_dd(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1646 struct namecache *ncp;
1647 struct rwlock *blps[2];
1649 blps[0] = HASH2BUCKETLOCK(hash);
1652 cache_lock_vnodes_cel(cel, dvp, vp);
1653 ncp = dvp->v_cache_dd;
1656 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1658 MPASS(ncp->nc_dvp == dvp);
1659 blps[1] = NCP2BUCKETLOCK(ncp);
1660 if (ncp->nc_flag & NCF_NEGATIVE)
1662 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1664 if (ncp == dvp->v_cache_dd &&
1665 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1666 blps[1] == NCP2BUCKETLOCK(ncp) &&
1667 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1669 cache_unlock_vnodes_cel(cel);
1674 cache_lock_buckets_cel(cel, blps[0], blps[1]);
1678 cache_enter_unlock(struct celockstate *cel)
1681 cache_unlock_buckets_cel(cel);
1682 cache_unlock_vnodes_cel(cel);
1686 * Add an entry to the cache.
1689 cache_enter_time(struct vnode *dvp, struct vnode *vp, struct componentname *cnp,
1690 struct timespec *tsp, struct timespec *dtsp)
1692 struct celockstate cel;
1693 struct namecache *ncp, *n2, *ndd;
1694 struct namecache_ts *ncp_ts, *n2_ts;
1695 struct nchashhead *ncpp;
1696 struct neglist *neglist;
1700 bool neg_locked, held_dvp;
1703 CTR3(KTR_VFS, "cache_enter(%p, %p, %s)", dvp, vp, cnp->cn_nameptr);
1704 VNASSERT(vp == NULL || (vp->v_iflag & VI_DOOMED) == 0, vp,
1705 ("cache_enter: Adding a doomed vnode"));
1706 VNASSERT(dvp == NULL || (dvp->v_iflag & VI_DOOMED) == 0, dvp,
1707 ("cache_enter: Doomed vnode used as src"));
1709 if (__predict_false(!doingcache))
1713 * Avoid blowout in namecache entries.
1715 lnumcache = atomic_fetchadd_long(&numcache, 1) + 1;
1716 if (__predict_false(lnumcache >= desiredvnodes * ncsizefactor)) {
1717 atomic_add_long(&numcache, -1);
1721 cache_celockstate_init(&cel);
1725 if (cnp->cn_nameptr[0] == '.') {
1726 if (cnp->cn_namelen == 1)
1728 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
1729 len = cnp->cn_namelen;
1730 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1731 cache_enter_lock_dd(&cel, dvp, vp, hash);
1733 * If dotdot entry already exists, just retarget it
1734 * to new parent vnode, otherwise continue with new
1735 * namecache entry allocation.
1737 if ((ncp = dvp->v_cache_dd) != NULL &&
1738 ncp->nc_flag & NCF_ISDOTDOT) {
1739 KASSERT(ncp->nc_dvp == dvp,
1740 ("wrong isdotdot parent"));
1742 if (ncp->nc_flag & NCF_NEGATIVE || vp == NULL) {
1743 neglist = NCP2NEGLIST(ncp);
1744 mtx_lock(&ncneg_hot.nl_lock);
1745 mtx_lock(&neglist->nl_lock);
1748 if (!(ncp->nc_flag & NCF_NEGATIVE)) {
1749 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst,
1752 cache_negative_remove(ncp, true);
1755 TAILQ_INSERT_HEAD(&vp->v_cache_dst,
1757 if (ncp->nc_flag & NCF_HOTNEGATIVE)
1759 ncp->nc_flag &= ~(NCF_NEGATIVE|NCF_HOTNEGATIVE);
1761 if (ncp->nc_flag & NCF_HOTNEGATIVE) {
1763 ncp->nc_flag &= ~(NCF_HOTNEGATIVE);
1765 ncp->nc_flag |= NCF_NEGATIVE;
1766 cache_negative_insert(ncp, true);
1769 mtx_unlock(&neglist->nl_lock);
1770 mtx_unlock(&ncneg_hot.nl_lock);
1773 cache_enter_unlock(&cel);
1776 dvp->v_cache_dd = NULL;
1777 cache_enter_unlock(&cel);
1778 cache_celockstate_init(&cel);
1779 SDT_PROBE3(vfs, namecache, enter, done, dvp, "..", vp);
1780 flag = NCF_ISDOTDOT;
1785 if (LIST_EMPTY(&dvp->v_cache_src) && flag != NCF_ISDOTDOT) {
1787 atomic_add_long(&numcachehv, 1);
1792 * Calculate the hash key and setup as much of the new
1793 * namecache entry as possible before acquiring the lock.
1795 ncp = cache_alloc(cnp->cn_namelen, tsp != NULL);
1796 ncp->nc_flag = flag;
1799 ncp->nc_flag |= NCF_NEGATIVE;
1802 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1803 ncp_ts->nc_time = *tsp;
1804 ncp_ts->nc_ticks = ticks;
1805 ncp_ts->nc_nc.nc_flag |= NCF_TS;
1807 ncp_ts->nc_dotdottime = *dtsp;
1808 ncp_ts->nc_nc.nc_flag |= NCF_DTS;
1811 len = ncp->nc_nlen = cnp->cn_namelen;
1812 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1813 strlcpy(ncp->nc_name, cnp->cn_nameptr, len + 1);
1814 cache_enter_lock(&cel, dvp, vp, hash);
1817 * See if this vnode or negative entry is already in the cache
1818 * with this name. This can happen with concurrent lookups of
1819 * the same path name.
1821 ncpp = NCHHASH(hash);
1822 LIST_FOREACH(n2, ncpp, nc_hash) {
1823 if (n2->nc_dvp == dvp &&
1824 n2->nc_nlen == cnp->cn_namelen &&
1825 !bcmp(n2->nc_name, cnp->cn_nameptr, n2->nc_nlen)) {
1827 KASSERT((n2->nc_flag & NCF_TS) != 0,
1829 n2_ts = __containerof(n2, struct namecache_ts, nc_nc);
1830 n2_ts->nc_time = ncp_ts->nc_time;
1831 n2_ts->nc_ticks = ncp_ts->nc_ticks;
1833 n2_ts->nc_dotdottime = ncp_ts->nc_dotdottime;
1834 if (ncp->nc_flag & NCF_NEGATIVE)
1835 mtx_lock(&ncneg_hot.nl_lock);
1836 n2_ts->nc_nc.nc_flag |= NCF_DTS;
1837 if (ncp->nc_flag & NCF_NEGATIVE)
1838 mtx_unlock(&ncneg_hot.nl_lock);
1841 goto out_unlock_free;
1845 if (flag == NCF_ISDOTDOT) {
1847 * See if we are trying to add .. entry, but some other lookup
1848 * has populated v_cache_dd pointer already.
1850 if (dvp->v_cache_dd != NULL)
1851 goto out_unlock_free;
1852 KASSERT(vp == NULL || vp->v_type == VDIR,
1853 ("wrong vnode type %p", vp));
1854 dvp->v_cache_dd = ncp;
1858 if (vp->v_type == VDIR) {
1859 if (flag != NCF_ISDOTDOT) {
1861 * For this case, the cache entry maps both the
1862 * directory name in it and the name ".." for the
1863 * directory's parent.
1865 if ((ndd = vp->v_cache_dd) != NULL) {
1866 if ((ndd->nc_flag & NCF_ISDOTDOT) != 0)
1867 cache_zap_locked(ndd, false);
1871 vp->v_cache_dd = ncp;
1874 vp->v_cache_dd = NULL;
1878 if (flag != NCF_ISDOTDOT) {
1879 if (LIST_EMPTY(&dvp->v_cache_src)) {
1882 atomic_add_long(&numcachehv, 1);
1887 * This will not take the interlock as someone
1888 * else already holds the vnode on account of
1889 * the namecache and we hold locks preventing
1890 * this from changing.
1893 atomic_subtract_long(&numcachehv, 1);
1896 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
1900 * Insert the new namecache entry into the appropriate chain
1901 * within the cache entries table.
1903 LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
1906 * If the entry is "negative", we place it into the
1907 * "negative" cache queue, otherwise, we place it into the
1908 * destination vnode's cache entries queue.
1911 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
1912 SDT_PROBE3(vfs, namecache, enter, done, dvp, ncp->nc_name,
1915 if (cnp->cn_flags & ISWHITEOUT)
1916 ncp->nc_flag |= NCF_WHITE;
1917 cache_negative_insert(ncp, false);
1918 SDT_PROBE2(vfs, namecache, enter_negative, done, dvp,
1921 cache_enter_unlock(&cel);
1922 if (numneg * ncnegfactor > lnumcache)
1923 cache_negative_zap_one();
1927 cache_enter_unlock(&cel);
1931 atomic_subtract_long(&numcachehv, 1);
1937 cache_roundup_2(u_int val)
1941 for (res = 1; res <= val; res <<= 1)
1948 * Name cache initialization, from vfs_init() when we are booting
1951 nchinit(void *dummy __unused)
1955 cache_zone_small = uma_zcreate("S VFS Cache",
1956 sizeof(struct namecache) + CACHE_PATH_CUTOFF + 1,
1957 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache),
1959 cache_zone_small_ts = uma_zcreate("STS VFS Cache",
1960 sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1,
1961 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache_ts),
1963 cache_zone_large = uma_zcreate("L VFS Cache",
1964 sizeof(struct namecache) + NAME_MAX + 1,
1965 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache),
1967 cache_zone_large_ts = uma_zcreate("LTS VFS Cache",
1968 sizeof(struct namecache_ts) + NAME_MAX + 1,
1969 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache_ts),
1972 nchashtbl = hashinit(desiredvnodes * 2, M_VFSCACHE, &nchash);
1973 ncbuckethash = cache_roundup_2(mp_ncpus * mp_ncpus) - 1;
1974 if (ncbuckethash < 7) /* arbitrarily chosen to avoid having one lock */
1976 if (ncbuckethash > nchash)
1977 ncbuckethash = nchash;
1978 bucketlocks = malloc(sizeof(*bucketlocks) * numbucketlocks, M_VFSCACHE,
1980 for (i = 0; i < numbucketlocks; i++)
1981 rw_init_flags(&bucketlocks[i], "ncbuc", RW_DUPOK | RW_RECURSE);
1982 ncvnodehash = ncbuckethash;
1983 vnodelocks = malloc(sizeof(*vnodelocks) * numvnodelocks, M_VFSCACHE,
1985 for (i = 0; i < numvnodelocks; i++)
1986 mtx_init(&vnodelocks[i], "ncvn", NULL, MTX_DUPOK | MTX_RECURSE);
1987 ncpurgeminvnodes = numbucketlocks * 2;
1990 neglists = malloc(sizeof(*neglists) * numneglists, M_VFSCACHE,
1992 for (i = 0; i < numneglists; i++) {
1993 mtx_init(&neglists[i].nl_lock, "ncnegl", NULL, MTX_DEF);
1994 TAILQ_INIT(&neglists[i].nl_list);
1996 mtx_init(&ncneg_hot.nl_lock, "ncneglh", NULL, MTX_DEF);
1997 TAILQ_INIT(&ncneg_hot.nl_list);
1999 mtx_init(&ncneg_shrink_lock, "ncnegs", NULL, MTX_DEF);
2001 numcalls = counter_u64_alloc(M_WAITOK);
2002 dothits = counter_u64_alloc(M_WAITOK);
2003 dotdothits = counter_u64_alloc(M_WAITOK);
2004 numchecks = counter_u64_alloc(M_WAITOK);
2005 nummiss = counter_u64_alloc(M_WAITOK);
2006 nummisszap = counter_u64_alloc(M_WAITOK);
2007 numposzaps = counter_u64_alloc(M_WAITOK);
2008 numposhits = counter_u64_alloc(M_WAITOK);
2009 numnegzaps = counter_u64_alloc(M_WAITOK);
2010 numneghits = counter_u64_alloc(M_WAITOK);
2011 numfullpathcalls = counter_u64_alloc(M_WAITOK);
2012 numfullpathfail1 = counter_u64_alloc(M_WAITOK);
2013 numfullpathfail2 = counter_u64_alloc(M_WAITOK);
2014 numfullpathfail4 = counter_u64_alloc(M_WAITOK);
2015 numfullpathfound = counter_u64_alloc(M_WAITOK);
2016 zap_and_exit_bucket_relock_success = counter_u64_alloc(M_WAITOK);
2018 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL);
2021 cache_changesize(int newmaxvnodes)
2023 struct nchashhead *new_nchashtbl, *old_nchashtbl;
2024 u_long new_nchash, old_nchash;
2025 struct namecache *ncp;
2029 newmaxvnodes = cache_roundup_2(newmaxvnodes * 2);
2030 if (newmaxvnodes < numbucketlocks)
2031 newmaxvnodes = numbucketlocks;
2033 new_nchashtbl = hashinit(newmaxvnodes, M_VFSCACHE, &new_nchash);
2034 /* If same hash table size, nothing to do */
2035 if (nchash == new_nchash) {
2036 free(new_nchashtbl, M_VFSCACHE);
2040 * Move everything from the old hash table to the new table.
2041 * None of the namecache entries in the table can be removed
2042 * because to do so, they have to be removed from the hash table.
2044 cache_lock_all_vnodes();
2045 cache_lock_all_buckets();
2046 old_nchashtbl = nchashtbl;
2047 old_nchash = nchash;
2048 nchashtbl = new_nchashtbl;
2049 nchash = new_nchash;
2050 for (i = 0; i <= old_nchash; i++) {
2051 while ((ncp = LIST_FIRST(&old_nchashtbl[i])) != NULL) {
2052 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen,
2054 LIST_REMOVE(ncp, nc_hash);
2055 LIST_INSERT_HEAD(NCHHASH(hash), ncp, nc_hash);
2058 cache_unlock_all_buckets();
2059 cache_unlock_all_vnodes();
2060 free(old_nchashtbl, M_VFSCACHE);
2064 * Invalidate all entries from and to a particular vnode.
2067 cache_purge(struct vnode *vp)
2069 TAILQ_HEAD(, namecache) ncps;
2070 struct namecache *ncp, *nnp;
2071 struct mtx *vlp, *vlp2;
2073 CTR1(KTR_VFS, "cache_purge(%p)", vp);
2074 SDT_PROBE1(vfs, namecache, purge, done, vp);
2075 if (LIST_EMPTY(&vp->v_cache_src) && TAILQ_EMPTY(&vp->v_cache_dst) &&
2076 vp->v_cache_dd == NULL)
2079 vlp = VP2VNODELOCK(vp);
2083 while (!LIST_EMPTY(&vp->v_cache_src)) {
2084 ncp = LIST_FIRST(&vp->v_cache_src);
2085 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2087 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2089 while (!TAILQ_EMPTY(&vp->v_cache_dst)) {
2090 ncp = TAILQ_FIRST(&vp->v_cache_dst);
2091 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2093 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2095 ncp = vp->v_cache_dd;
2097 KASSERT(ncp->nc_flag & NCF_ISDOTDOT,
2098 ("lost dotdot link"));
2099 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
2101 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2103 KASSERT(vp->v_cache_dd == NULL, ("incomplete purge"));
2107 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2113 * Invalidate all negative entries for a particular directory vnode.
2116 cache_purge_negative(struct vnode *vp)
2118 TAILQ_HEAD(, namecache) ncps;
2119 struct namecache *ncp, *nnp;
2122 CTR1(KTR_VFS, "cache_purge_negative(%p)", vp);
2123 SDT_PROBE1(vfs, namecache, purge_negative, done, vp);
2124 if (LIST_EMPTY(&vp->v_cache_src))
2127 vlp = VP2VNODELOCK(vp);
2129 LIST_FOREACH_SAFE(ncp, &vp->v_cache_src, nc_src, nnp) {
2130 if (!(ncp->nc_flag & NCF_NEGATIVE))
2132 cache_zap_negative_locked_vnode_kl(ncp, vp);
2133 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
2136 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2142 * Flush all entries referencing a particular filesystem.
2145 cache_purgevfs(struct mount *mp, bool force)
2147 TAILQ_HEAD(, namecache) ncps;
2148 struct mtx *vlp1, *vlp2;
2150 struct nchashhead *bucket;
2151 struct namecache *ncp, *nnp;
2152 u_long i, j, n_nchash;
2155 /* Scan hash tables for applicable entries */
2156 SDT_PROBE1(vfs, namecache, purgevfs, done, mp);
2157 if (!force && mp->mnt_nvnodelistsize <= ncpurgeminvnodes)
2160 n_nchash = nchash + 1;
2162 for (i = 0; i < numbucketlocks; i++) {
2163 blp = (struct rwlock *)&bucketlocks[i];
2165 for (j = i; j < n_nchash; j += numbucketlocks) {
2167 bucket = &nchashtbl[j];
2168 LIST_FOREACH_SAFE(ncp, bucket, nc_hash, nnp) {
2169 cache_assert_bucket_locked(ncp, RA_WLOCKED);
2170 if (ncp->nc_dvp->v_mount != mp)
2172 error = cache_zap_wlocked_bucket_kl(ncp, blp,
2176 TAILQ_INSERT_HEAD(&ncps, ncp, nc_dst);
2180 if (vlp1 == NULL && vlp2 == NULL)
2181 cache_maybe_yield();
2188 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
2194 * Perform canonical checks and cache lookup and pass on to filesystem
2195 * through the vop_cachedlookup only if needed.
2199 vfs_cache_lookup(struct vop_lookup_args *ap)
2203 struct vnode **vpp = ap->a_vpp;
2204 struct componentname *cnp = ap->a_cnp;
2205 struct ucred *cred = cnp->cn_cred;
2206 int flags = cnp->cn_flags;
2207 struct thread *td = cnp->cn_thread;
2212 if (dvp->v_type != VDIR)
2215 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
2216 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
2219 error = VOP_ACCESS(dvp, VEXEC, cred, td);
2223 error = cache_lookup(dvp, vpp, cnp, NULL, NULL);
2225 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
2232 * XXX All of these sysctls would probably be more productive dead.
2234 static int __read_mostly disablecwd;
2235 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0,
2236 "Disable the getcwd syscall");
2238 /* Implementation of the getcwd syscall. */
2240 sys___getcwd(struct thread *td, struct __getcwd_args *uap)
2243 return (kern___getcwd(td, uap->buf, UIO_USERSPACE, uap->buflen,
2248 kern___getcwd(struct thread *td, char *buf, enum uio_seg bufseg, size_t buflen,
2252 struct filedesc *fdp;
2253 struct vnode *cdir, *rdir;
2256 if (__predict_false(disablecwd))
2258 if (__predict_false(buflen < 2))
2260 if (buflen > path_max)
2263 tmpbuf = malloc(buflen, M_TEMP, M_WAITOK);
2264 fdp = td->td_proc->p_fd;
2265 FILEDESC_SLOCK(fdp);
2266 cdir = fdp->fd_cdir;
2268 rdir = fdp->fd_rdir;
2270 FILEDESC_SUNLOCK(fdp);
2271 error = vn_fullpath1(td, cdir, rdir, tmpbuf, &bp, buflen);
2276 if (bufseg == UIO_SYSSPACE)
2277 bcopy(bp, buf, strlen(bp) + 1);
2279 error = copyout(bp, buf, strlen(bp) + 1);
2281 if (KTRPOINT(curthread, KTR_NAMEI))
2285 free(tmpbuf, M_TEMP);
2290 * Thus begins the fullpath magic.
2293 static int __read_mostly disablefullpath;
2294 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW, &disablefullpath, 0,
2295 "Disable the vn_fullpath function");
2298 * Retrieve the full filesystem path that correspond to a vnode from the name
2299 * cache (if available)
2302 vn_fullpath(struct thread *td, struct vnode *vn, char **retbuf, char **freebuf)
2305 struct filedesc *fdp;
2309 if (__predict_false(disablefullpath))
2311 if (__predict_false(vn == NULL))
2314 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
2315 fdp = td->td_proc->p_fd;
2316 FILEDESC_SLOCK(fdp);
2317 rdir = fdp->fd_rdir;
2319 FILEDESC_SUNLOCK(fdp);
2320 error = vn_fullpath1(td, vn, rdir, buf, retbuf, MAXPATHLEN);
2331 * This function is similar to vn_fullpath, but it attempts to lookup the
2332 * pathname relative to the global root mount point. This is required for the
2333 * auditing sub-system, as audited pathnames must be absolute, relative to the
2334 * global root mount point.
2337 vn_fullpath_global(struct thread *td, struct vnode *vn,
2338 char **retbuf, char **freebuf)
2343 if (__predict_false(disablefullpath))
2345 if (__predict_false(vn == NULL))
2347 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
2348 error = vn_fullpath1(td, vn, rootvnode, buf, retbuf, MAXPATHLEN);
2357 vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, u_int *buflen)
2360 struct namecache *ncp;
2364 vlp = VP2VNODELOCK(*vp);
2366 TAILQ_FOREACH(ncp, &((*vp)->v_cache_dst), nc_dst) {
2367 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2371 if (*buflen < ncp->nc_nlen) {
2374 counter_u64_add(numfullpathfail4, 1);
2376 SDT_PROBE3(vfs, namecache, fullpath, return, error,
2380 *buflen -= ncp->nc_nlen;
2381 memcpy(buf + *buflen, ncp->nc_name, ncp->nc_nlen);
2382 SDT_PROBE3(vfs, namecache, fullpath, hit, ncp->nc_dvp,
2391 SDT_PROBE1(vfs, namecache, fullpath, miss, vp);
2394 vn_lock(*vp, LK_SHARED | LK_RETRY);
2395 error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen);
2398 counter_u64_add(numfullpathfail2, 1);
2399 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2404 if (dvp->v_iflag & VI_DOOMED) {
2405 /* forced unmount */
2408 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2412 * *vp has its use count incremented still.
2419 * The magic behind kern___getcwd() and vn_fullpath().
2422 vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
2423 char *buf, char **retbuf, u_int buflen)
2425 int error, slash_prefixed;
2426 #ifdef KDTRACE_HOOKS
2427 struct vnode *startvp = vp;
2436 SDT_PROBE1(vfs, namecache, fullpath, entry, vp);
2437 counter_u64_add(numfullpathcalls, 1);
2439 if (vp->v_type != VDIR) {
2440 error = vn_vptocnp(&vp, td->td_ucred, buf, &buflen);
2447 buf[--buflen] = '/';
2450 while (vp != rdir && vp != rootvnode) {
2452 * The vp vnode must be already fully constructed,
2453 * since it is either found in namecache or obtained
2454 * from VOP_VPTOCNP(). We may test for VV_ROOT safely
2455 * without obtaining the vnode lock.
2457 if ((vp->v_vflag & VV_ROOT) != 0) {
2458 vn_lock(vp, LK_RETRY | LK_SHARED);
2461 * With the vnode locked, check for races with
2462 * unmount, forced or not. Note that we
2463 * already verified that vp is not equal to
2464 * the root vnode, which means that
2465 * mnt_vnodecovered can be NULL only for the
2468 if ((vp->v_iflag & VI_DOOMED) != 0 ||
2469 (vp1 = vp->v_mount->mnt_vnodecovered) == NULL ||
2470 vp1->v_mountedhere != vp->v_mount) {
2473 SDT_PROBE3(vfs, namecache, fullpath, return,
2483 if (vp->v_type != VDIR) {
2485 counter_u64_add(numfullpathfail1, 1);
2487 SDT_PROBE3(vfs, namecache, fullpath, return,
2491 error = vn_vptocnp(&vp, td->td_ucred, buf, &buflen);
2497 SDT_PROBE3(vfs, namecache, fullpath, return, error,
2501 buf[--buflen] = '/';
2506 if (!slash_prefixed) {
2509 counter_u64_add(numfullpathfail4, 1);
2510 SDT_PROBE3(vfs, namecache, fullpath, return, ENOMEM,
2514 buf[--buflen] = '/';
2516 counter_u64_add(numfullpathfound, 1);
2519 SDT_PROBE3(vfs, namecache, fullpath, return, 0, startvp, buf + buflen);
2520 *retbuf = buf + buflen;
2525 vn_dir_dd_ino(struct vnode *vp)
2527 struct namecache *ncp;
2532 ASSERT_VOP_LOCKED(vp, "vn_dir_dd_ino");
2533 vlp = VP2VNODELOCK(vp);
2535 TAILQ_FOREACH(ncp, &(vp->v_cache_dst), nc_dst) {
2536 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0)
2539 vs = vget_prep(ddvp);
2541 if (vget_finish(ddvp, LK_SHARED | LK_NOWAIT, vs))
2550 vn_commname(struct vnode *vp, char *buf, u_int buflen)
2552 struct namecache *ncp;
2556 vlp = VP2VNODELOCK(vp);
2558 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst)
2559 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2565 l = min(ncp->nc_nlen, buflen - 1);
2566 memcpy(buf, ncp->nc_name, l);
2573 * This function updates path string to vnode's full global path
2574 * and checks the size of the new path string against the pathlen argument.
2576 * Requires a locked, referenced vnode.
2577 * Vnode is re-locked on success or ENODEV, otherwise unlocked.
2579 * If sysctl debug.disablefullpath is set, ENODEV is returned,
2580 * vnode is left locked and path remain untouched.
2582 * If vp is a directory, the call to vn_fullpath_global() always succeeds
2583 * because it falls back to the ".." lookup if the namecache lookup fails.
2586 vn_path_to_global_path(struct thread *td, struct vnode *vp, char *path,
2589 struct nameidata nd;
2594 ASSERT_VOP_ELOCKED(vp, __func__);
2596 /* Return ENODEV if sysctl debug.disablefullpath==1 */
2597 if (__predict_false(disablefullpath))
2600 /* Construct global filesystem path from vp. */
2602 error = vn_fullpath_global(td, vp, &rpath, &fbuf);
2609 if (strlen(rpath) >= pathlen) {
2611 error = ENAMETOOLONG;
2616 * Re-lookup the vnode by path to detect a possible rename.
2617 * As a side effect, the vnode is relocked.
2618 * If vnode was renamed, return ENOENT.
2620 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
2621 UIO_SYSSPACE, path, td);
2627 NDFREE(&nd, NDF_ONLY_PNBUF);
2631 strcpy(path, rpath);
2644 db_print_vpath(struct vnode *vp)
2647 while (vp != NULL) {
2648 db_printf("%p: ", vp);
2649 if (vp == rootvnode) {
2653 if (vp->v_vflag & VV_ROOT) {
2654 db_printf("<mount point>");
2655 vp = vp->v_mount->mnt_vnodecovered;
2657 struct namecache *ncp;
2661 ncp = TAILQ_FIRST(&vp->v_cache_dst);
2664 for (i = 0; i < ncp->nc_nlen; i++)
2665 db_printf("%c", *ncn++);
2678 DB_SHOW_COMMAND(vpath, db_show_vpath)
2683 db_printf("usage: show vpath <struct vnode *>\n");
2687 vp = (struct vnode *)addr;