2 * Copyright (c) 1989, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Poul-Henning Kamp of the FreeBSD Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)vfs_cache.c 8.5 (Berkeley) 3/22/95
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include "opt_ktrace.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/counter.h>
43 #include <sys/filedesc.h>
44 #include <sys/fnv_hash.h>
45 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/fcntl.h>
49 #include <sys/mount.h>
50 #include <sys/namei.h>
52 #include <sys/rwlock.h>
55 #include <sys/syscallsubr.h>
56 #include <sys/sysctl.h>
57 #include <sys/sysproto.h>
58 #include <sys/vnode.h>
60 #include <sys/ktrace.h>
65 SDT_PROVIDER_DECLARE(vfs);
66 SDT_PROBE_DEFINE3(vfs, namecache, enter, done, "struct vnode *", "char *",
68 SDT_PROBE_DEFINE2(vfs, namecache, enter_negative, done, "struct vnode *",
70 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, entry, "struct vnode *");
71 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, hit, "struct vnode *",
72 "char *", "struct vnode *");
73 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, miss, "struct vnode *");
74 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, return, "int",
75 "struct vnode *", "char *");
76 SDT_PROBE_DEFINE3(vfs, namecache, lookup, hit, "struct vnode *", "char *",
78 SDT_PROBE_DEFINE2(vfs, namecache, lookup, hit__negative,
79 "struct vnode *", "char *");
80 SDT_PROBE_DEFINE2(vfs, namecache, lookup, miss, "struct vnode *",
82 SDT_PROBE_DEFINE1(vfs, namecache, purge, done, "struct vnode *");
83 SDT_PROBE_DEFINE1(vfs, namecache, purge_negative, done, "struct vnode *");
84 SDT_PROBE_DEFINE1(vfs, namecache, purgevfs, done, "struct mount *");
85 SDT_PROBE_DEFINE3(vfs, namecache, zap, done, "struct vnode *", "char *",
87 SDT_PROBE_DEFINE3(vfs, namecache, zap_negative, done, "struct vnode *",
89 SDT_PROBE_DEFINE3(vfs, namecache, shrink_negative, done, "struct vnode *",
93 * This structure describes the elements in the cache of recent
94 * names looked up by namei.
98 LIST_ENTRY(namecache) nc_hash; /* hash chain */
99 LIST_ENTRY(namecache) nc_src; /* source vnode list */
100 TAILQ_ENTRY(namecache) nc_dst; /* destination vnode list */
101 struct vnode *nc_dvp; /* vnode of parent of name */
103 struct vnode *nu_vp; /* vnode the name refers to */
104 u_int nu_neghits; /* negative entry hits */
106 u_char nc_flag; /* flag bits */
107 u_char nc_nlen; /* length of name */
108 char nc_name[0]; /* segment name + nul */
112 * struct namecache_ts repeats struct namecache layout up to the
114 * struct namecache_ts is used in place of struct namecache when time(s) need
115 * to be stored. The nc_dotdottime field is used when a cache entry is mapping
116 * both a non-dotdot directory name plus dotdot for the directory's
119 struct namecache_ts {
120 struct timespec nc_time; /* timespec provided by fs */
121 struct timespec nc_dotdottime; /* dotdot timespec provided by fs */
122 int nc_ticks; /* ticks value when entry was added */
123 struct namecache nc_nc;
126 #define nc_vp n_un.nu_vp
127 #define nc_neghits n_un.nu_neghits
130 * Flags in namecache.nc_flag
132 #define NCF_WHITE 0x01
133 #define NCF_ISDOTDOT 0x02
136 #define NCF_DVDROP 0x10
137 #define NCF_NEGATIVE 0x20
138 #define NCF_HOTNEGATIVE 0x40
141 * Name caching works as follows:
143 * Names found by directory scans are retained in a cache
144 * for future reference. It is managed LRU, so frequently
145 * used names will hang around. Cache is indexed by hash value
146 * obtained from (vp, name) where vp refers to the directory
149 * If it is a "negative" entry, (i.e. for a name that is known NOT to
150 * exist) the vnode pointer will be NULL.
152 * Upon reaching the last segment of a path, if the reference
153 * is for DELETE, or NOCACHE is set (rewrite), and the
154 * name is located in the cache, it will be dropped.
156 * These locks are used (in the order in which they can be taken):
158 * vnodelock mtx vnode lists and v_cache_dd field protection
159 * bucketlock rwlock for access to given set of hash buckets
160 * neglist mtx negative entry LRU management
162 * Additionally, ncneg_shrink_lock mtx is used to have at most one thread
163 * shrinking the LRU list.
165 * It is legal to take multiple vnodelock and bucketlock locks. The locking
166 * order is lower address first. Both are recursive.
168 * "." lookups are lockless.
170 * ".." and vnode -> name lookups require vnodelock.
172 * name -> vnode lookup requires the relevant bucketlock to be held for reading.
174 * Insertions and removals of entries require involved vnodes and bucketlocks
175 * to be write-locked to prevent other threads from seeing the entry.
177 * Some lookups result in removal of the found entry (e.g. getting rid of a
178 * negative entry with the intent to create a positive one), which poses a
179 * problem when multiple threads reach the state. Similarly, two different
180 * threads can purge two different vnodes and try to remove the same name.
182 * If the already held vnode lock is lower than the second required lock, we
183 * can just take the other lock. However, in the opposite case, this could
184 * deadlock. As such, this is resolved by trylocking and if that fails unlocking
185 * the first node, locking everything in order and revalidating the state.
189 * Structures associated with name caching.
191 #define NCHHASH(hash) \
192 (&nchashtbl[(hash) & nchash])
193 static __read_mostly LIST_HEAD(nchashhead, namecache) *nchashtbl;/* Hash Table */
194 static u_long __read_mostly nchash; /* size of hash table */
195 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0,
196 "Size of namecache hash table");
197 static u_long __read_mostly ncnegfactor = 16; /* ratio of negative entries */
198 SYSCTL_ULONG(_vfs, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0,
199 "Ratio of negative namecache entries");
200 static u_long __exclusive_cache_line numneg; /* number of negative entries allocated */
201 SYSCTL_ULONG(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0,
202 "Number of negative entries in namecache");
203 static u_long __exclusive_cache_line numcache;/* number of cache entries allocated */
204 SYSCTL_ULONG(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0,
205 "Number of namecache entries");
206 static u_long __exclusive_cache_line numcachehv;/* number of cache entries with vnodes held */
207 SYSCTL_ULONG(_debug, OID_AUTO, numcachehv, CTLFLAG_RD, &numcachehv, 0,
208 "Number of namecache entries with vnodes held");
209 u_int __read_mostly ncsizefactor = 2;
210 SYSCTL_UINT(_vfs, OID_AUTO, ncsizefactor, CTLFLAG_RW, &ncsizefactor, 0,
211 "Size factor for namecache");
212 static u_int __read_mostly ncpurgeminvnodes;
213 SYSCTL_UINT(_vfs, OID_AUTO, ncpurgeminvnodes, CTLFLAG_RW, &ncpurgeminvnodes, 0,
214 "Number of vnodes below which purgevfs ignores the request");
215 static u_int __read_mostly ncneghitsrequeue = 8;
216 SYSCTL_UINT(_vfs, OID_AUTO, ncneghitsrequeue, CTLFLAG_RW, &ncneghitsrequeue, 0,
217 "Number of hits to requeue a negative entry in the LRU list");
219 struct nchstats nchstats; /* cache effectiveness statistics */
221 static struct mtx ncneg_shrink_lock;
222 static int shrink_list_turn;
226 TAILQ_HEAD(, namecache) nl_list;
227 } __aligned(CACHE_LINE_SIZE);
229 static struct neglist __read_mostly *neglists;
230 static struct neglist ncneg_hot;
232 #define numneglists (ncneghash + 1)
233 static u_int __read_mostly ncneghash;
234 static inline struct neglist *
235 NCP2NEGLIST(struct namecache *ncp)
238 return (&neglists[(((uintptr_t)(ncp) >> 8) & ncneghash)]);
241 #define numbucketlocks (ncbuckethash + 1)
242 static u_int __read_mostly ncbuckethash;
243 static struct rwlock_padalign __read_mostly *bucketlocks;
244 #define HASH2BUCKETLOCK(hash) \
245 ((struct rwlock *)(&bucketlocks[((hash) & ncbuckethash)]))
247 #define numvnodelocks (ncvnodehash + 1)
248 static u_int __read_mostly ncvnodehash;
249 static struct mtx __read_mostly *vnodelocks;
250 static inline struct mtx *
251 VP2VNODELOCK(struct vnode *vp)
254 return (&vnodelocks[(((uintptr_t)(vp) >> 8) & ncvnodehash)]);
258 * UMA zones for the VFS cache.
260 * The small cache is used for entries with short names, which are the
261 * most common. The large cache is used for entries which are too big to
262 * fit in the small cache.
264 static uma_zone_t __read_mostly cache_zone_small;
265 static uma_zone_t __read_mostly cache_zone_small_ts;
266 static uma_zone_t __read_mostly cache_zone_large;
267 static uma_zone_t __read_mostly cache_zone_large_ts;
269 #define CACHE_PATH_CUTOFF 35
271 static struct namecache *
272 cache_alloc(int len, int ts)
274 struct namecache_ts *ncp_ts;
275 struct namecache *ncp;
277 if (__predict_false(ts)) {
278 if (len <= CACHE_PATH_CUTOFF)
279 ncp_ts = uma_zalloc(cache_zone_small_ts, M_WAITOK);
281 ncp_ts = uma_zalloc(cache_zone_large_ts, M_WAITOK);
282 ncp = &ncp_ts->nc_nc;
284 if (len <= CACHE_PATH_CUTOFF)
285 ncp = uma_zalloc(cache_zone_small, M_WAITOK);
287 ncp = uma_zalloc(cache_zone_large, M_WAITOK);
293 cache_free(struct namecache *ncp)
295 struct namecache_ts *ncp_ts;
299 if ((ncp->nc_flag & NCF_DVDROP) != 0)
301 if (__predict_false(ncp->nc_flag & NCF_TS)) {
302 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
303 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
304 uma_zfree(cache_zone_small_ts, ncp_ts);
306 uma_zfree(cache_zone_large_ts, ncp_ts);
308 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF)
309 uma_zfree(cache_zone_small, ncp);
311 uma_zfree(cache_zone_large, ncp);
316 cache_out_ts(struct namecache *ncp, struct timespec *tsp, int *ticksp)
318 struct namecache_ts *ncp_ts;
320 KASSERT((ncp->nc_flag & NCF_TS) != 0 ||
321 (tsp == NULL && ticksp == NULL),
324 if (tsp == NULL && ticksp == NULL)
327 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
329 *tsp = ncp_ts->nc_time;
331 *ticksp = ncp_ts->nc_ticks;
334 static int __read_mostly doingcache = 1; /* 1 => enable the cache */
335 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0,
336 "VFS namecache enabled");
338 /* Export size information to userland */
339 SYSCTL_INT(_debug_sizeof, OID_AUTO, namecache, CTLFLAG_RD, SYSCTL_NULL_INT_PTR,
340 sizeof(struct namecache), "sizeof(struct namecache)");
343 * The new name cache statistics
345 static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0,
346 "Name cache statistics");
347 #define STATNODE_ULONG(name, descr) \
348 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, descr);
349 #define STATNODE_COUNTER(name, descr) \
350 static counter_u64_t __read_mostly name; \
351 SYSCTL_COUNTER_U64(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, descr);
352 STATNODE_ULONG(numneg, "Number of negative cache entries");
353 STATNODE_ULONG(numcache, "Number of cache entries");
354 STATNODE_COUNTER(numcalls, "Number of cache lookups");
355 STATNODE_COUNTER(dothits, "Number of '.' hits");
356 STATNODE_COUNTER(dotdothits, "Number of '..' hits");
357 STATNODE_COUNTER(numchecks, "Number of checks in lookup");
358 STATNODE_COUNTER(nummiss, "Number of cache misses");
359 STATNODE_COUNTER(nummisszap, "Number of cache misses we do not want to cache");
360 STATNODE_COUNTER(numposzaps,
361 "Number of cache hits (positive) we do not want to cache");
362 STATNODE_COUNTER(numposhits, "Number of cache hits (positive)");
363 STATNODE_COUNTER(numnegzaps,
364 "Number of cache hits (negative) we do not want to cache");
365 STATNODE_COUNTER(numneghits, "Number of cache hits (negative)");
366 /* These count for kern___getcwd(), too. */
367 STATNODE_COUNTER(numfullpathcalls, "Number of fullpath search calls");
368 STATNODE_COUNTER(numfullpathfail1, "Number of fullpath search errors (ENOTDIR)");
369 STATNODE_COUNTER(numfullpathfail2,
370 "Number of fullpath search errors (VOP_VPTOCNP failures)");
371 STATNODE_COUNTER(numfullpathfail4, "Number of fullpath search errors (ENOMEM)");
372 STATNODE_COUNTER(numfullpathfound, "Number of successful fullpath calls");
373 static long zap_and_exit_bucket_fail; STATNODE_ULONG(zap_and_exit_bucket_fail,
374 "Number of times zap_and_exit failed to lock");
375 static long cache_lock_vnodes_cel_3_failures;
376 STATNODE_ULONG(cache_lock_vnodes_cel_3_failures,
377 "Number of times 3-way vnode locking failed");
379 static void cache_zap_locked(struct namecache *ncp, bool neg_locked);
380 static int vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
381 char *buf, char **retbuf, u_int buflen);
383 static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
385 static int cache_yield;
386 SYSCTL_INT(_vfs_cache, OID_AUTO, yield, CTLFLAG_RD, &cache_yield, 0,
387 "Number of times cache called yield");
390 cache_maybe_yield(void)
393 if (should_yield()) {
395 kern_yield(PRI_USER);
400 cache_assert_vlp_locked(struct mtx *vlp)
404 mtx_assert(vlp, MA_OWNED);
408 cache_assert_vnode_locked(struct vnode *vp)
412 vlp = VP2VNODELOCK(vp);
413 cache_assert_vlp_locked(vlp);
417 cache_get_hash(char *name, u_char len, struct vnode *dvp)
421 hash = fnv_32_buf(name, len, FNV1_32_INIT);
422 hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
426 static inline struct rwlock *
427 NCP2BUCKETLOCK(struct namecache *ncp)
431 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen, ncp->nc_dvp);
432 return (HASH2BUCKETLOCK(hash));
437 cache_assert_bucket_locked(struct namecache *ncp, int mode)
441 blp = NCP2BUCKETLOCK(ncp);
442 rw_assert(blp, mode);
445 #define cache_assert_bucket_locked(x, y) do { } while (0)
448 #define cache_sort(x, y) _cache_sort((void **)(x), (void **)(y))
450 _cache_sort(void **p1, void **p2)
462 cache_lock_all_buckets(void)
466 for (i = 0; i < numbucketlocks; i++)
467 rw_wlock(&bucketlocks[i]);
471 cache_unlock_all_buckets(void)
475 for (i = 0; i < numbucketlocks; i++)
476 rw_wunlock(&bucketlocks[i]);
480 cache_lock_all_vnodes(void)
484 for (i = 0; i < numvnodelocks; i++)
485 mtx_lock(&vnodelocks[i]);
489 cache_unlock_all_vnodes(void)
493 for (i = 0; i < numvnodelocks; i++)
494 mtx_unlock(&vnodelocks[i]);
498 cache_trylock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
501 cache_sort(&vlp1, &vlp2);
505 if (!mtx_trylock(vlp1))
508 if (!mtx_trylock(vlp2)) {
518 cache_unlock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
521 MPASS(vlp1 != NULL || vlp2 != NULL);
530 sysctl_nchstats(SYSCTL_HANDLER_ARGS)
532 struct nchstats snap;
534 if (req->oldptr == NULL)
535 return (SYSCTL_OUT(req, 0, sizeof(snap)));
538 snap.ncs_goodhits = counter_u64_fetch(numposhits);
539 snap.ncs_neghits = counter_u64_fetch(numneghits);
540 snap.ncs_badhits = counter_u64_fetch(numposzaps) +
541 counter_u64_fetch(numnegzaps);
542 snap.ncs_miss = counter_u64_fetch(nummisszap) +
543 counter_u64_fetch(nummiss);
545 return (SYSCTL_OUT(req, &snap, sizeof(snap)));
547 SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE | CTLFLAG_RD |
548 CTLFLAG_MPSAFE, 0, 0, sysctl_nchstats, "LU",
549 "VFS cache effectiveness statistics");
553 * Grab an atomic snapshot of the name cache hash chain lengths
555 static SYSCTL_NODE(_debug, OID_AUTO, hashstat, CTLFLAG_RW, NULL,
559 sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS)
561 struct nchashhead *ncpp;
562 struct namecache *ncp;
563 int i, error, n_nchash, *cntbuf;
566 n_nchash = nchash + 1; /* nchash is max index, not count */
567 if (req->oldptr == NULL)
568 return SYSCTL_OUT(req, 0, n_nchash * sizeof(int));
569 cntbuf = malloc(n_nchash * sizeof(int), M_TEMP, M_ZERO | M_WAITOK);
570 cache_lock_all_buckets();
571 if (n_nchash != nchash + 1) {
572 cache_unlock_all_buckets();
573 free(cntbuf, M_TEMP);
576 /* Scan hash tables counting entries */
577 for (ncpp = nchashtbl, i = 0; i < n_nchash; ncpp++, i++)
578 LIST_FOREACH(ncp, ncpp, nc_hash)
580 cache_unlock_all_buckets();
581 for (error = 0, i = 0; i < n_nchash; i++)
582 if ((error = SYSCTL_OUT(req, &cntbuf[i], sizeof(int))) != 0)
584 free(cntbuf, M_TEMP);
587 SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD|
588 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_rawnchash, "S,int",
589 "nchash chain lengths");
592 sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS)
595 struct nchashhead *ncpp;
596 struct namecache *ncp;
598 int count, maxlength, used, pct;
601 return SYSCTL_OUT(req, 0, 4 * sizeof(int));
603 cache_lock_all_buckets();
604 n_nchash = nchash + 1; /* nchash is max index, not count */
608 /* Scan hash tables for applicable entries */
609 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
611 LIST_FOREACH(ncp, ncpp, nc_hash) {
616 if (maxlength < count)
619 n_nchash = nchash + 1;
620 cache_unlock_all_buckets();
621 pct = (used * 100) / (n_nchash / 100);
622 error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash));
625 error = SYSCTL_OUT(req, &used, sizeof(used));
628 error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength));
631 error = SYSCTL_OUT(req, &pct, sizeof(pct));
636 SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD|
637 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_nchash, "I",
638 "nchash statistics (number of total/used buckets, maximum chain length, usage percentage)");
642 * Negative entries management
644 * A variation of LRU scheme is used. New entries are hashed into one of
645 * numneglists cold lists. Entries get promoted to the hot list on first hit.
646 * Partial LRU for the hot list is maintained by requeueing them every
647 * ncneghitsrequeue hits.
649 * The shrinker will demote hot list head and evict from the cold list in a
650 * round-robin manner.
653 cache_negative_hit(struct namecache *ncp)
655 struct neglist *neglist;
658 MPASS(ncp->nc_flag & NCF_NEGATIVE);
659 hits = atomic_fetchadd_int(&ncp->nc_neghits, 1);
660 if (ncp->nc_flag & NCF_HOTNEGATIVE) {
661 if ((hits % ncneghitsrequeue) != 0)
663 mtx_lock(&ncneg_hot.nl_lock);
664 if (ncp->nc_flag & NCF_HOTNEGATIVE) {
665 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
666 TAILQ_INSERT_TAIL(&ncneg_hot.nl_list, ncp, nc_dst);
667 mtx_unlock(&ncneg_hot.nl_lock);
671 * The shrinker cleared the flag and removed the entry from
672 * the hot list. Put it back.
675 mtx_lock(&ncneg_hot.nl_lock);
677 neglist = NCP2NEGLIST(ncp);
678 mtx_lock(&neglist->nl_lock);
679 if (!(ncp->nc_flag & NCF_HOTNEGATIVE)) {
680 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
681 TAILQ_INSERT_TAIL(&ncneg_hot.nl_list, ncp, nc_dst);
682 ncp->nc_flag |= NCF_HOTNEGATIVE;
684 mtx_unlock(&neglist->nl_lock);
685 mtx_unlock(&ncneg_hot.nl_lock);
689 cache_negative_insert(struct namecache *ncp, bool neg_locked)
691 struct neglist *neglist;
693 MPASS(ncp->nc_flag & NCF_NEGATIVE);
694 cache_assert_bucket_locked(ncp, RA_WLOCKED);
695 neglist = NCP2NEGLIST(ncp);
697 mtx_lock(&neglist->nl_lock);
699 mtx_assert(&neglist->nl_lock, MA_OWNED);
701 TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst);
703 mtx_unlock(&neglist->nl_lock);
704 atomic_add_rel_long(&numneg, 1);
708 cache_negative_remove(struct namecache *ncp, bool neg_locked)
710 struct neglist *neglist;
711 bool hot_locked = false;
712 bool list_locked = false;
714 MPASS(ncp->nc_flag & NCF_NEGATIVE);
715 cache_assert_bucket_locked(ncp, RA_WLOCKED);
716 neglist = NCP2NEGLIST(ncp);
718 if (ncp->nc_flag & NCF_HOTNEGATIVE) {
720 mtx_lock(&ncneg_hot.nl_lock);
721 if (!(ncp->nc_flag & NCF_HOTNEGATIVE)) {
723 mtx_lock(&neglist->nl_lock);
727 mtx_lock(&neglist->nl_lock);
730 if (ncp->nc_flag & NCF_HOTNEGATIVE) {
731 mtx_assert(&ncneg_hot.nl_lock, MA_OWNED);
732 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
734 mtx_assert(&neglist->nl_lock, MA_OWNED);
735 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
738 mtx_unlock(&neglist->nl_lock);
740 mtx_unlock(&ncneg_hot.nl_lock);
741 atomic_subtract_rel_long(&numneg, 1);
745 cache_negative_shrink_select(int start, struct namecache **ncpp,
746 struct neglist **neglistpp)
748 struct neglist *neglist;
749 struct namecache *ncp;
754 for (i = start; i < numneglists; i++) {
755 neglist = &neglists[i];
756 if (TAILQ_FIRST(&neglist->nl_list) == NULL)
758 mtx_lock(&neglist->nl_lock);
759 ncp = TAILQ_FIRST(&neglist->nl_list);
762 mtx_unlock(&neglist->nl_lock);
765 *neglistpp = neglist;
770 cache_negative_zap_one(void)
772 struct namecache *ncp, *ncp2;
773 struct neglist *neglist;
777 if (!mtx_trylock(&ncneg_shrink_lock))
780 mtx_lock(&ncneg_hot.nl_lock);
781 ncp = TAILQ_FIRST(&ncneg_hot.nl_list);
783 neglist = NCP2NEGLIST(ncp);
784 mtx_lock(&neglist->nl_lock);
785 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
786 TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst);
787 ncp->nc_flag &= ~NCF_HOTNEGATIVE;
788 mtx_unlock(&neglist->nl_lock);
791 cache_negative_shrink_select(shrink_list_turn, &ncp, &neglist);
793 if (shrink_list_turn == numneglists)
794 shrink_list_turn = 0;
795 if (ncp == NULL && shrink_list_turn == 0)
796 cache_negative_shrink_select(shrink_list_turn, &ncp, &neglist);
798 mtx_unlock(&ncneg_hot.nl_lock);
802 MPASS(ncp->nc_flag & NCF_NEGATIVE);
803 dvlp = VP2VNODELOCK(ncp->nc_dvp);
804 blp = NCP2BUCKETLOCK(ncp);
805 mtx_unlock(&neglist->nl_lock);
806 mtx_unlock(&ncneg_hot.nl_lock);
809 mtx_lock(&neglist->nl_lock);
810 ncp2 = TAILQ_FIRST(&neglist->nl_list);
811 if (ncp != ncp2 || dvlp != VP2VNODELOCK(ncp2->nc_dvp) ||
812 blp != NCP2BUCKETLOCK(ncp2) || !(ncp2->nc_flag & NCF_NEGATIVE)) {
816 SDT_PROBE3(vfs, namecache, shrink_negative, done, ncp->nc_dvp,
817 ncp->nc_name, ncp->nc_neghits);
819 cache_zap_locked(ncp, true);
821 mtx_unlock(&neglist->nl_lock);
825 mtx_unlock(&ncneg_shrink_lock);
830 * cache_zap_locked():
832 * Removes a namecache entry from cache, whether it contains an actual
833 * pointer to a vnode or if it is just a negative cache entry.
836 cache_zap_locked(struct namecache *ncp, bool neg_locked)
839 if (!(ncp->nc_flag & NCF_NEGATIVE))
840 cache_assert_vnode_locked(ncp->nc_vp);
841 cache_assert_vnode_locked(ncp->nc_dvp);
842 cache_assert_bucket_locked(ncp, RA_WLOCKED);
844 CTR2(KTR_VFS, "cache_zap(%p) vp %p", ncp,
845 (ncp->nc_flag & NCF_NEGATIVE) ? NULL : ncp->nc_vp);
846 if (!(ncp->nc_flag & NCF_NEGATIVE)) {
847 SDT_PROBE3(vfs, namecache, zap, done, ncp->nc_dvp,
848 ncp->nc_name, ncp->nc_vp);
850 SDT_PROBE3(vfs, namecache, zap_negative, done, ncp->nc_dvp,
851 ncp->nc_name, ncp->nc_neghits);
853 LIST_REMOVE(ncp, nc_hash);
854 if (!(ncp->nc_flag & NCF_NEGATIVE)) {
855 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
856 if (ncp == ncp->nc_vp->v_cache_dd)
857 ncp->nc_vp->v_cache_dd = NULL;
859 cache_negative_remove(ncp, neg_locked);
861 if (ncp->nc_flag & NCF_ISDOTDOT) {
862 if (ncp == ncp->nc_dvp->v_cache_dd)
863 ncp->nc_dvp->v_cache_dd = NULL;
865 LIST_REMOVE(ncp, nc_src);
866 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) {
867 ncp->nc_flag |= NCF_DVDROP;
868 atomic_subtract_rel_long(&numcachehv, 1);
871 atomic_subtract_rel_long(&numcache, 1);
875 cache_zap_negative_locked_vnode_kl(struct namecache *ncp, struct vnode *vp)
879 MPASS(ncp->nc_dvp == vp);
880 MPASS(ncp->nc_flag & NCF_NEGATIVE);
881 cache_assert_vnode_locked(vp);
883 blp = NCP2BUCKETLOCK(ncp);
885 cache_zap_locked(ncp, false);
890 cache_zap_locked_vnode_kl2(struct namecache *ncp, struct vnode *vp,
893 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
896 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
897 cache_assert_vnode_locked(vp);
899 if (ncp->nc_flag & NCF_NEGATIVE) {
904 cache_zap_negative_locked_vnode_kl(ncp, vp);
908 pvlp = VP2VNODELOCK(vp);
909 blp = NCP2BUCKETLOCK(ncp);
910 vlp1 = VP2VNODELOCK(ncp->nc_dvp);
911 vlp2 = VP2VNODELOCK(ncp->nc_vp);
913 if (*vlpp == vlp1 || *vlpp == vlp2) {
921 cache_sort(&vlp1, &vlp2);
926 if (!mtx_trylock(vlp1))
932 cache_zap_locked(ncp, false);
934 if (to_unlock != NULL)
935 mtx_unlock(to_unlock);
942 MPASS(*vlpp == NULL);
948 cache_zap_locked_vnode(struct namecache *ncp, struct vnode *vp)
950 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
954 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
955 cache_assert_vnode_locked(vp);
957 pvlp = VP2VNODELOCK(vp);
958 if (ncp->nc_flag & NCF_NEGATIVE) {
959 cache_zap_negative_locked_vnode_kl(ncp, vp);
963 blp = NCP2BUCKETLOCK(ncp);
964 vlp1 = VP2VNODELOCK(ncp->nc_dvp);
965 vlp2 = VP2VNODELOCK(ncp->nc_vp);
966 cache_sort(&vlp1, &vlp2);
971 if (!mtx_trylock(vlp1)) {
978 cache_zap_locked(ncp, false);
980 mtx_unlock(to_unlock);
987 cache_zap_rlocked_bucket(struct namecache *ncp, struct rwlock *blp)
989 struct mtx *dvlp, *vlp;
991 cache_assert_bucket_locked(ncp, RA_RLOCKED);
993 dvlp = VP2VNODELOCK(ncp->nc_dvp);
995 if (!(ncp->nc_flag & NCF_NEGATIVE))
996 vlp = VP2VNODELOCK(ncp->nc_vp);
997 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1000 cache_zap_locked(ncp, false);
1002 cache_unlock_vnodes(dvlp, vlp);
1011 cache_zap_wlocked_bucket_kl(struct namecache *ncp, struct rwlock *blp,
1012 struct mtx **vlpp1, struct mtx **vlpp2)
1014 struct mtx *dvlp, *vlp;
1016 cache_assert_bucket_locked(ncp, RA_WLOCKED);
1018 dvlp = VP2VNODELOCK(ncp->nc_dvp);
1020 if (!(ncp->nc_flag & NCF_NEGATIVE))
1021 vlp = VP2VNODELOCK(ncp->nc_vp);
1022 cache_sort(&dvlp, &vlp);
1024 if (*vlpp1 == dvlp && *vlpp2 == vlp) {
1025 cache_zap_locked(ncp, false);
1026 cache_unlock_vnodes(dvlp, vlp);
1039 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1040 cache_zap_locked(ncp, false);
1041 cache_unlock_vnodes(dvlp, vlp);
1056 cache_lookup_unlock(struct rwlock *blp, struct mtx *vlp)
1061 mtx_assert(vlp, MA_NOTOWNED);
1067 static int __noinline
1068 cache_lookup_dot(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1069 struct timespec *tsp, int *ticksp)
1074 CTR2(KTR_VFS, "cache_lookup(%p, %s) found via .",
1075 dvp, cnp->cn_nameptr);
1076 counter_u64_add(dothits, 1);
1077 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", *vpp);
1084 * When we lookup "." we still can be asked to lock it
1087 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
1088 if (ltype != VOP_ISLOCKED(*vpp)) {
1089 if (ltype == LK_EXCLUSIVE) {
1090 vn_lock(*vpp, LK_UPGRADE | LK_RETRY);
1091 if ((*vpp)->v_iflag & VI_DOOMED) {
1092 /* forced unmount */
1098 vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY);
1104 * Lookup an entry in the cache
1106 * Lookup is called with dvp pointing to the directory to search,
1107 * cnp pointing to the name of the entry being sought. If the lookup
1108 * succeeds, the vnode is returned in *vpp, and a status of -1 is
1109 * returned. If the lookup determines that the name does not exist
1110 * (negative caching), a status of ENOENT is returned. If the lookup
1111 * fails, a status of zero is returned. If the directory vnode is
1112 * recycled out from under us due to a forced unmount, a status of
1113 * ENOENT is returned.
1115 * vpp is locked and ref'd on return. If we're looking up DOTDOT, dvp is
1116 * unlocked. If we're looking up . an extra ref is taken, but the lock is
1117 * not recursively acquired.
1121 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1122 struct timespec *tsp, int *ticksp)
1124 struct namecache_ts *ncp_ts;
1125 struct namecache *ncp;
1127 struct mtx *dvlp, *dvlp2;
1131 if (__predict_false(!doingcache)) {
1132 cnp->cn_flags &= ~MAKEENTRY;
1137 dvlp = VP2VNODELOCK(dvp);
1139 counter_u64_add(numcalls, 1);
1141 if (cnp->cn_nameptr[0] == '.') {
1142 if (cnp->cn_namelen == 1)
1143 return (cache_lookup_dot(dvp, vpp, cnp, tsp, ticksp));
1144 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
1145 counter_u64_add(dotdothits, 1);
1149 ncp = dvp->v_cache_dd;
1151 SDT_PROBE3(vfs, namecache, lookup, miss, dvp,
1156 if ((cnp->cn_flags & MAKEENTRY) == 0) {
1157 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1158 if (ncp->nc_dvp != dvp)
1159 panic("dvp %p v_cache_dd %p\n", dvp, ncp);
1160 if (!cache_zap_locked_vnode_kl2(ncp,
1163 MPASS(dvp->v_cache_dd == NULL);
1169 dvp->v_cache_dd = NULL;
1176 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1177 if (ncp->nc_flag & NCF_NEGATIVE)
1183 /* Return failure if negative entry was found. */
1185 goto negative_success;
1186 CTR3(KTR_VFS, "cache_lookup(%p, %s) found %p via ..",
1187 dvp, cnp->cn_nameptr, *vpp);
1188 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, "..",
1190 cache_out_ts(ncp, tsp, ticksp);
1191 if ((ncp->nc_flag & (NCF_ISDOTDOT | NCF_DTS)) ==
1192 NCF_DTS && tsp != NULL) {
1193 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1194 *tsp = ncp_ts->nc_dotdottime;
1200 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1201 blp = HASH2BUCKETLOCK(hash);
1204 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1205 counter_u64_add(numchecks, 1);
1206 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1207 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
1211 /* We failed to find an entry */
1213 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
1215 if ((cnp->cn_flags & MAKEENTRY) == 0) {
1216 counter_u64_add(nummisszap, 1);
1218 counter_u64_add(nummiss, 1);
1220 cache_lookup_unlock(blp, dvlp);
1224 /* We don't want to have an entry, so dump it */
1225 if ((cnp->cn_flags & MAKEENTRY) == 0) {
1226 counter_u64_add(numposzaps, 1);
1230 /* We found a "positive" match, return the vnode */
1231 if (!(ncp->nc_flag & NCF_NEGATIVE)) {
1232 counter_u64_add(numposhits, 1);
1234 CTR4(KTR_VFS, "cache_lookup(%p, %s) found %p via ncp %p",
1235 dvp, cnp->cn_nameptr, *vpp, ncp);
1236 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ncp->nc_name,
1238 cache_out_ts(ncp, tsp, ticksp);
1243 /* We found a negative match, and want to create it, so purge */
1244 if (cnp->cn_nameiop == CREATE) {
1245 counter_u64_add(numnegzaps, 1);
1249 counter_u64_add(numneghits, 1);
1250 cache_negative_hit(ncp);
1251 if (ncp->nc_flag & NCF_WHITE)
1252 cnp->cn_flags |= ISWHITEOUT;
1253 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp,
1255 cache_out_ts(ncp, tsp, ticksp);
1256 cache_lookup_unlock(blp, dvlp);
1261 * On success we return a locked and ref'd vnode as per the lookup
1265 ltype = 0; /* silence gcc warning */
1266 if (cnp->cn_flags & ISDOTDOT) {
1267 ltype = VOP_ISLOCKED(dvp);
1271 cache_lookup_unlock(blp, dvlp);
1272 error = vget(*vpp, cnp->cn_lkflags | LK_VNHELD, cnp->cn_thread);
1273 if (cnp->cn_flags & ISDOTDOT) {
1274 vn_lock(dvp, ltype | LK_RETRY);
1275 if (dvp->v_iflag & VI_DOOMED) {
1286 if ((cnp->cn_flags & ISLASTCN) &&
1287 (cnp->cn_lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE) {
1288 ASSERT_VOP_ELOCKED(*vpp, "cache_lookup");
1294 error = cache_zap_rlocked_bucket(ncp, blp);
1296 error = cache_zap_locked_vnode(ncp, dvp);
1298 zap_and_exit_bucket_fail++;
1299 cache_maybe_yield();
1306 struct celockstate {
1308 struct rwlock *blp[2];
1310 CTASSERT((nitems(((struct celockstate *)0)->vlp) == 3));
1311 CTASSERT((nitems(((struct celockstate *)0)->blp) == 2));
1314 cache_celockstate_init(struct celockstate *cel)
1317 bzero(cel, sizeof(*cel));
1321 cache_lock_vnodes_cel(struct celockstate *cel, struct vnode *vp,
1324 struct mtx *vlp1, *vlp2;
1326 MPASS(cel->vlp[0] == NULL);
1327 MPASS(cel->vlp[1] == NULL);
1328 MPASS(cel->vlp[2] == NULL);
1330 MPASS(vp != NULL || dvp != NULL);
1332 vlp1 = VP2VNODELOCK(vp);
1333 vlp2 = VP2VNODELOCK(dvp);
1334 cache_sort(&vlp1, &vlp2);
1345 cache_unlock_vnodes_cel(struct celockstate *cel)
1348 MPASS(cel->vlp[0] != NULL || cel->vlp[1] != NULL);
1350 if (cel->vlp[0] != NULL)
1351 mtx_unlock(cel->vlp[0]);
1352 if (cel->vlp[1] != NULL)
1353 mtx_unlock(cel->vlp[1]);
1354 if (cel->vlp[2] != NULL)
1355 mtx_unlock(cel->vlp[2]);
1359 cache_lock_vnodes_cel_3(struct celockstate *cel, struct vnode *vp)
1364 cache_assert_vlp_locked(cel->vlp[0]);
1365 cache_assert_vlp_locked(cel->vlp[1]);
1366 MPASS(cel->vlp[2] == NULL);
1369 vlp = VP2VNODELOCK(vp);
1372 if (vlp >= cel->vlp[1]) {
1375 if (mtx_trylock(vlp))
1377 cache_lock_vnodes_cel_3_failures++;
1378 cache_unlock_vnodes_cel(cel);
1379 if (vlp < cel->vlp[0]) {
1381 mtx_lock(cel->vlp[0]);
1382 mtx_lock(cel->vlp[1]);
1384 if (cel->vlp[0] != NULL)
1385 mtx_lock(cel->vlp[0]);
1387 mtx_lock(cel->vlp[1]);
1397 cache_lock_buckets_cel(struct celockstate *cel, struct rwlock *blp1,
1398 struct rwlock *blp2)
1401 MPASS(cel->blp[0] == NULL);
1402 MPASS(cel->blp[1] == NULL);
1404 cache_sort(&blp1, &blp2);
1415 cache_unlock_buckets_cel(struct celockstate *cel)
1418 if (cel->blp[0] != NULL)
1419 rw_wunlock(cel->blp[0]);
1420 rw_wunlock(cel->blp[1]);
1424 * Lock part of the cache affected by the insertion.
1426 * This means vnodelocks for dvp, vp and the relevant bucketlock.
1427 * However, insertion can result in removal of an old entry. In this
1428 * case we have an additional vnode and bucketlock pair to lock. If the
1429 * entry is negative, ncelock is locked instead of the vnode.
1431 * That is, in the worst case we have to lock 3 vnodes and 2 bucketlocks, while
1432 * preserving the locking order (smaller address first).
1435 cache_enter_lock(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1438 struct namecache *ncp;
1439 struct rwlock *blps[2];
1441 blps[0] = HASH2BUCKETLOCK(hash);
1444 cache_lock_vnodes_cel(cel, dvp, vp);
1445 if (vp == NULL || vp->v_type != VDIR)
1447 ncp = vp->v_cache_dd;
1450 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1452 MPASS(ncp->nc_dvp == vp);
1453 blps[1] = NCP2BUCKETLOCK(ncp);
1454 if (ncp->nc_flag & NCF_NEGATIVE)
1456 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1459 * All vnodes got re-locked. Re-validate the state and if
1460 * nothing changed we are done. Otherwise restart.
1462 if (ncp == vp->v_cache_dd &&
1463 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1464 blps[1] == NCP2BUCKETLOCK(ncp) &&
1465 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1467 cache_unlock_vnodes_cel(cel);
1472 cache_lock_buckets_cel(cel, blps[0], blps[1]);
1476 cache_enter_lock_dd(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1479 struct namecache *ncp;
1480 struct rwlock *blps[2];
1482 blps[0] = HASH2BUCKETLOCK(hash);
1485 cache_lock_vnodes_cel(cel, dvp, vp);
1486 ncp = dvp->v_cache_dd;
1489 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1491 MPASS(ncp->nc_dvp == dvp);
1492 blps[1] = NCP2BUCKETLOCK(ncp);
1493 if (ncp->nc_flag & NCF_NEGATIVE)
1495 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1497 if (ncp == dvp->v_cache_dd &&
1498 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1499 blps[1] == NCP2BUCKETLOCK(ncp) &&
1500 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1502 cache_unlock_vnodes_cel(cel);
1507 cache_lock_buckets_cel(cel, blps[0], blps[1]);
1511 cache_enter_unlock(struct celockstate *cel)
1514 cache_unlock_buckets_cel(cel);
1515 cache_unlock_vnodes_cel(cel);
1519 * Add an entry to the cache.
1522 cache_enter_time(struct vnode *dvp, struct vnode *vp, struct componentname *cnp,
1523 struct timespec *tsp, struct timespec *dtsp)
1525 struct celockstate cel;
1526 struct namecache *ncp, *n2, *ndd;
1527 struct namecache_ts *ncp_ts, *n2_ts;
1528 struct nchashhead *ncpp;
1529 struct neglist *neglist;
1535 CTR3(KTR_VFS, "cache_enter(%p, %p, %s)", dvp, vp, cnp->cn_nameptr);
1536 VNASSERT(vp == NULL || (vp->v_iflag & VI_DOOMED) == 0, vp,
1537 ("cache_enter: Adding a doomed vnode"));
1538 VNASSERT(dvp == NULL || (dvp->v_iflag & VI_DOOMED) == 0, dvp,
1539 ("cache_enter: Doomed vnode used as src"));
1541 if (__predict_false(!doingcache))
1545 * Avoid blowout in namecache entries.
1547 if (__predict_false(numcache >= desiredvnodes * ncsizefactor))
1550 cache_celockstate_init(&cel);
1553 if (cnp->cn_nameptr[0] == '.') {
1554 if (cnp->cn_namelen == 1)
1556 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
1557 len = cnp->cn_namelen;
1558 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1559 cache_enter_lock_dd(&cel, dvp, vp, hash);
1561 * If dotdot entry already exists, just retarget it
1562 * to new parent vnode, otherwise continue with new
1563 * namecache entry allocation.
1565 if ((ncp = dvp->v_cache_dd) != NULL &&
1566 ncp->nc_flag & NCF_ISDOTDOT) {
1567 KASSERT(ncp->nc_dvp == dvp,
1568 ("wrong isdotdot parent"));
1570 if (ncp->nc_flag & NCF_NEGATIVE || vp == NULL) {
1571 neglist = NCP2NEGLIST(ncp);
1572 mtx_lock(&ncneg_hot.nl_lock);
1573 mtx_lock(&neglist->nl_lock);
1576 if (!(ncp->nc_flag & NCF_NEGATIVE)) {
1577 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst,
1580 cache_negative_remove(ncp, true);
1583 TAILQ_INSERT_HEAD(&vp->v_cache_dst,
1585 ncp->nc_flag &= ~(NCF_NEGATIVE|NCF_HOTNEGATIVE);
1587 ncp->nc_flag &= ~(NCF_HOTNEGATIVE);
1588 ncp->nc_flag |= NCF_NEGATIVE;
1589 cache_negative_insert(ncp, true);
1592 mtx_unlock(&neglist->nl_lock);
1593 mtx_unlock(&ncneg_hot.nl_lock);
1596 cache_enter_unlock(&cel);
1599 dvp->v_cache_dd = NULL;
1600 cache_enter_unlock(&cel);
1601 cache_celockstate_init(&cel);
1602 SDT_PROBE3(vfs, namecache, enter, done, dvp, "..", vp);
1603 flag = NCF_ISDOTDOT;
1608 * Calculate the hash key and setup as much of the new
1609 * namecache entry as possible before acquiring the lock.
1611 ncp = cache_alloc(cnp->cn_namelen, tsp != NULL);
1612 ncp->nc_flag = flag;
1615 ncp->nc_flag |= NCF_NEGATIVE;
1618 ncp_ts = __containerof(ncp, struct namecache_ts, nc_nc);
1619 ncp_ts->nc_time = *tsp;
1620 ncp_ts->nc_ticks = ticks;
1621 ncp_ts->nc_nc.nc_flag |= NCF_TS;
1623 ncp_ts->nc_dotdottime = *dtsp;
1624 ncp_ts->nc_nc.nc_flag |= NCF_DTS;
1627 len = ncp->nc_nlen = cnp->cn_namelen;
1628 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1629 strlcpy(ncp->nc_name, cnp->cn_nameptr, len + 1);
1630 cache_enter_lock(&cel, dvp, vp, hash);
1633 * See if this vnode or negative entry is already in the cache
1634 * with this name. This can happen with concurrent lookups of
1635 * the same path name.
1637 ncpp = NCHHASH(hash);
1638 LIST_FOREACH(n2, ncpp, nc_hash) {
1639 if (n2->nc_dvp == dvp &&
1640 n2->nc_nlen == cnp->cn_namelen &&
1641 !bcmp(n2->nc_name, cnp->cn_nameptr, n2->nc_nlen)) {
1643 KASSERT((n2->nc_flag & NCF_TS) != 0,
1645 n2_ts = __containerof(n2, struct namecache_ts, nc_nc);
1646 n2_ts->nc_time = ncp_ts->nc_time;
1647 n2_ts->nc_ticks = ncp_ts->nc_ticks;
1649 n2_ts->nc_dotdottime = ncp_ts->nc_dotdottime;
1650 if (ncp->nc_flag & NCF_NEGATIVE)
1651 mtx_lock(&ncneg_hot.nl_lock);
1652 n2_ts->nc_nc.nc_flag |= NCF_DTS;
1653 if (ncp->nc_flag & NCF_NEGATIVE)
1654 mtx_unlock(&ncneg_hot.nl_lock);
1657 goto out_unlock_free;
1661 if (flag == NCF_ISDOTDOT) {
1663 * See if we are trying to add .. entry, but some other lookup
1664 * has populated v_cache_dd pointer already.
1666 if (dvp->v_cache_dd != NULL)
1667 goto out_unlock_free;
1668 KASSERT(vp == NULL || vp->v_type == VDIR,
1669 ("wrong vnode type %p", vp));
1670 dvp->v_cache_dd = ncp;
1673 atomic_add_rel_long(&numcache, 1);
1675 if (vp->v_type == VDIR) {
1676 if (flag != NCF_ISDOTDOT) {
1678 * For this case, the cache entry maps both the
1679 * directory name in it and the name ".." for the
1680 * directory's parent.
1682 if ((ndd = vp->v_cache_dd) != NULL) {
1683 if ((ndd->nc_flag & NCF_ISDOTDOT) != 0)
1684 cache_zap_locked(ndd, false);
1688 vp->v_cache_dd = ncp;
1691 vp->v_cache_dd = NULL;
1695 if (flag != NCF_ISDOTDOT) {
1696 if (LIST_EMPTY(&dvp->v_cache_src)) {
1698 atomic_add_rel_long(&numcachehv, 1);
1700 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
1704 * Insert the new namecache entry into the appropriate chain
1705 * within the cache entries table.
1707 LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
1710 * If the entry is "negative", we place it into the
1711 * "negative" cache queue, otherwise, we place it into the
1712 * destination vnode's cache entries queue.
1715 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
1716 SDT_PROBE3(vfs, namecache, enter, done, dvp, ncp->nc_name,
1719 if (cnp->cn_flags & ISWHITEOUT)
1720 ncp->nc_flag |= NCF_WHITE;
1721 cache_negative_insert(ncp, false);
1722 SDT_PROBE2(vfs, namecache, enter_negative, done, dvp,
1725 cache_enter_unlock(&cel);
1726 if (numneg * ncnegfactor > numcache)
1727 cache_negative_zap_one();
1731 cache_enter_unlock(&cel);
1737 cache_roundup_2(u_int val)
1741 for (res = 1; res <= val; res <<= 1)
1748 * Name cache initialization, from vfs_init() when we are booting
1751 nchinit(void *dummy __unused)
1755 cache_zone_small = uma_zcreate("S VFS Cache",
1756 sizeof(struct namecache) + CACHE_PATH_CUTOFF + 1,
1757 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache),
1759 cache_zone_small_ts = uma_zcreate("STS VFS Cache",
1760 sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1,
1761 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache_ts),
1763 cache_zone_large = uma_zcreate("L VFS Cache",
1764 sizeof(struct namecache) + NAME_MAX + 1,
1765 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache),
1767 cache_zone_large_ts = uma_zcreate("LTS VFS Cache",
1768 sizeof(struct namecache_ts) + NAME_MAX + 1,
1769 NULL, NULL, NULL, NULL, UMA_ALIGNOF(struct namecache_ts),
1772 nchashtbl = hashinit(desiredvnodes * 2, M_VFSCACHE, &nchash);
1773 ncbuckethash = cache_roundup_2(mp_ncpus * 64) - 1;
1774 if (ncbuckethash > nchash)
1775 ncbuckethash = nchash;
1776 bucketlocks = malloc(sizeof(*bucketlocks) * numbucketlocks, M_VFSCACHE,
1778 for (i = 0; i < numbucketlocks; i++)
1779 rw_init_flags(&bucketlocks[i], "ncbuc", RW_DUPOK | RW_RECURSE);
1780 ncvnodehash = cache_roundup_2(mp_ncpus * 64) - 1;
1781 vnodelocks = malloc(sizeof(*vnodelocks) * numvnodelocks, M_VFSCACHE,
1783 for (i = 0; i < numvnodelocks; i++)
1784 mtx_init(&vnodelocks[i], "ncvn", NULL, MTX_DUPOK | MTX_RECURSE);
1785 ncpurgeminvnodes = numbucketlocks;
1788 neglists = malloc(sizeof(*neglists) * numneglists, M_VFSCACHE,
1790 for (i = 0; i < numneglists; i++) {
1791 mtx_init(&neglists[i].nl_lock, "ncnegl", NULL, MTX_DEF);
1792 TAILQ_INIT(&neglists[i].nl_list);
1794 mtx_init(&ncneg_hot.nl_lock, "ncneglh", NULL, MTX_DEF);
1795 TAILQ_INIT(&ncneg_hot.nl_list);
1797 mtx_init(&ncneg_shrink_lock, "ncnegs", NULL, MTX_DEF);
1799 numcalls = counter_u64_alloc(M_WAITOK);
1800 dothits = counter_u64_alloc(M_WAITOK);
1801 dotdothits = counter_u64_alloc(M_WAITOK);
1802 numchecks = counter_u64_alloc(M_WAITOK);
1803 nummiss = counter_u64_alloc(M_WAITOK);
1804 nummisszap = counter_u64_alloc(M_WAITOK);
1805 numposzaps = counter_u64_alloc(M_WAITOK);
1806 numposhits = counter_u64_alloc(M_WAITOK);
1807 numnegzaps = counter_u64_alloc(M_WAITOK);
1808 numneghits = counter_u64_alloc(M_WAITOK);
1809 numfullpathcalls = counter_u64_alloc(M_WAITOK);
1810 numfullpathfail1 = counter_u64_alloc(M_WAITOK);
1811 numfullpathfail2 = counter_u64_alloc(M_WAITOK);
1812 numfullpathfail4 = counter_u64_alloc(M_WAITOK);
1813 numfullpathfound = counter_u64_alloc(M_WAITOK);
1815 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL);
1818 cache_changesize(int newmaxvnodes)
1820 struct nchashhead *new_nchashtbl, *old_nchashtbl;
1821 u_long new_nchash, old_nchash;
1822 struct namecache *ncp;
1826 newmaxvnodes = cache_roundup_2(newmaxvnodes * 2);
1827 if (newmaxvnodes < numbucketlocks)
1828 newmaxvnodes = numbucketlocks;
1830 new_nchashtbl = hashinit(newmaxvnodes, M_VFSCACHE, &new_nchash);
1831 /* If same hash table size, nothing to do */
1832 if (nchash == new_nchash) {
1833 free(new_nchashtbl, M_VFSCACHE);
1837 * Move everything from the old hash table to the new table.
1838 * None of the namecache entries in the table can be removed
1839 * because to do so, they have to be removed from the hash table.
1841 cache_lock_all_vnodes();
1842 cache_lock_all_buckets();
1843 old_nchashtbl = nchashtbl;
1844 old_nchash = nchash;
1845 nchashtbl = new_nchashtbl;
1846 nchash = new_nchash;
1847 for (i = 0; i <= old_nchash; i++) {
1848 while ((ncp = LIST_FIRST(&old_nchashtbl[i])) != NULL) {
1849 hash = cache_get_hash(ncp->nc_name, ncp->nc_nlen,
1851 LIST_REMOVE(ncp, nc_hash);
1852 LIST_INSERT_HEAD(NCHHASH(hash), ncp, nc_hash);
1855 cache_unlock_all_buckets();
1856 cache_unlock_all_vnodes();
1857 free(old_nchashtbl, M_VFSCACHE);
1861 * Invalidate all entries to a particular vnode.
1864 cache_purge(struct vnode *vp)
1866 TAILQ_HEAD(, namecache) ncps;
1867 struct namecache *ncp, *nnp;
1868 struct mtx *vlp, *vlp2;
1870 CTR1(KTR_VFS, "cache_purge(%p)", vp);
1871 SDT_PROBE1(vfs, namecache, purge, done, vp);
1872 if (LIST_EMPTY(&vp->v_cache_src) && TAILQ_EMPTY(&vp->v_cache_dst) &&
1873 vp->v_cache_dd == NULL)
1876 vlp = VP2VNODELOCK(vp);
1880 while (!LIST_EMPTY(&vp->v_cache_src)) {
1881 ncp = LIST_FIRST(&vp->v_cache_src);
1882 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
1884 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
1886 while (!TAILQ_EMPTY(&vp->v_cache_dst)) {
1887 ncp = TAILQ_FIRST(&vp->v_cache_dst);
1888 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
1890 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
1892 ncp = vp->v_cache_dd;
1894 KASSERT(ncp->nc_flag & NCF_ISDOTDOT,
1895 ("lost dotdot link"));
1896 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
1898 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
1900 KASSERT(vp->v_cache_dd == NULL, ("incomplete purge"));
1904 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
1910 * Invalidate all negative entries for a particular directory vnode.
1913 cache_purge_negative(struct vnode *vp)
1915 TAILQ_HEAD(, namecache) ncps;
1916 struct namecache *ncp, *nnp;
1919 CTR1(KTR_VFS, "cache_purge_negative(%p)", vp);
1920 SDT_PROBE1(vfs, namecache, purge_negative, done, vp);
1922 vlp = VP2VNODELOCK(vp);
1924 LIST_FOREACH_SAFE(ncp, &vp->v_cache_src, nc_src, nnp) {
1925 if (!(ncp->nc_flag & NCF_NEGATIVE))
1927 cache_zap_negative_locked_vnode_kl(ncp, vp);
1928 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
1931 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
1937 * Flush all entries referencing a particular filesystem.
1940 cache_purgevfs(struct mount *mp, bool force)
1942 TAILQ_HEAD(, namecache) ncps;
1943 struct mtx *vlp1, *vlp2;
1945 struct nchashhead *bucket;
1946 struct namecache *ncp, *nnp;
1947 u_long i, j, n_nchash;
1950 /* Scan hash tables for applicable entries */
1951 SDT_PROBE1(vfs, namecache, purgevfs, done, mp);
1952 if (!force && mp->mnt_nvnodelistsize <= ncpurgeminvnodes)
1955 n_nchash = nchash + 1;
1957 for (i = 0; i < numbucketlocks; i++) {
1958 blp = (struct rwlock *)&bucketlocks[i];
1960 for (j = i; j < n_nchash; j += numbucketlocks) {
1962 bucket = &nchashtbl[j];
1963 LIST_FOREACH_SAFE(ncp, bucket, nc_hash, nnp) {
1964 cache_assert_bucket_locked(ncp, RA_WLOCKED);
1965 if (ncp->nc_dvp->v_mount != mp)
1967 error = cache_zap_wlocked_bucket_kl(ncp, blp,
1971 TAILQ_INSERT_HEAD(&ncps, ncp, nc_dst);
1975 if (vlp1 == NULL && vlp2 == NULL)
1976 cache_maybe_yield();
1983 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
1989 * Perform canonical checks and cache lookup and pass on to filesystem
1990 * through the vop_cachedlookup only if needed.
1994 vfs_cache_lookup(struct vop_lookup_args *ap)
1998 struct vnode **vpp = ap->a_vpp;
1999 struct componentname *cnp = ap->a_cnp;
2000 struct ucred *cred = cnp->cn_cred;
2001 int flags = cnp->cn_flags;
2002 struct thread *td = cnp->cn_thread;
2007 if (dvp->v_type != VDIR)
2010 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
2011 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
2014 error = VOP_ACCESS(dvp, VEXEC, cred, td);
2018 error = cache_lookup(dvp, vpp, cnp, NULL, NULL);
2020 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
2027 * XXX All of these sysctls would probably be more productive dead.
2029 static int __read_mostly disablecwd;
2030 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0,
2031 "Disable the getcwd syscall");
2033 /* Implementation of the getcwd syscall. */
2035 sys___getcwd(struct thread *td, struct __getcwd_args *uap)
2038 return (kern___getcwd(td, uap->buf, UIO_USERSPACE, uap->buflen,
2043 kern___getcwd(struct thread *td, char *buf, enum uio_seg bufseg, size_t buflen,
2047 struct filedesc *fdp;
2048 struct vnode *cdir, *rdir;
2051 if (__predict_false(disablecwd))
2053 if (__predict_false(buflen < 2))
2055 if (buflen > path_max)
2058 tmpbuf = malloc(buflen, M_TEMP, M_WAITOK);
2059 fdp = td->td_proc->p_fd;
2060 FILEDESC_SLOCK(fdp);
2061 cdir = fdp->fd_cdir;
2063 rdir = fdp->fd_rdir;
2065 FILEDESC_SUNLOCK(fdp);
2066 error = vn_fullpath1(td, cdir, rdir, tmpbuf, &bp, buflen);
2071 if (bufseg == UIO_SYSSPACE)
2072 bcopy(bp, buf, strlen(bp) + 1);
2074 error = copyout(bp, buf, strlen(bp) + 1);
2076 if (KTRPOINT(curthread, KTR_NAMEI))
2080 free(tmpbuf, M_TEMP);
2085 * Thus begins the fullpath magic.
2088 static int __read_mostly disablefullpath;
2089 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW, &disablefullpath, 0,
2090 "Disable the vn_fullpath function");
2093 * Retrieve the full filesystem path that correspond to a vnode from the name
2094 * cache (if available)
2097 vn_fullpath(struct thread *td, struct vnode *vn, char **retbuf, char **freebuf)
2100 struct filedesc *fdp;
2104 if (__predict_false(disablefullpath))
2106 if (__predict_false(vn == NULL))
2109 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
2110 fdp = td->td_proc->p_fd;
2111 FILEDESC_SLOCK(fdp);
2112 rdir = fdp->fd_rdir;
2114 FILEDESC_SUNLOCK(fdp);
2115 error = vn_fullpath1(td, vn, rdir, buf, retbuf, MAXPATHLEN);
2126 * This function is similar to vn_fullpath, but it attempts to lookup the
2127 * pathname relative to the global root mount point. This is required for the
2128 * auditing sub-system, as audited pathnames must be absolute, relative to the
2129 * global root mount point.
2132 vn_fullpath_global(struct thread *td, struct vnode *vn,
2133 char **retbuf, char **freebuf)
2138 if (__predict_false(disablefullpath))
2140 if (__predict_false(vn == NULL))
2142 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
2143 error = vn_fullpath1(td, vn, rootvnode, buf, retbuf, MAXPATHLEN);
2152 vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, u_int *buflen)
2155 struct namecache *ncp;
2159 vlp = VP2VNODELOCK(*vp);
2161 TAILQ_FOREACH(ncp, &((*vp)->v_cache_dst), nc_dst) {
2162 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2166 if (*buflen < ncp->nc_nlen) {
2169 counter_u64_add(numfullpathfail4, 1);
2171 SDT_PROBE3(vfs, namecache, fullpath, return, error,
2175 *buflen -= ncp->nc_nlen;
2176 memcpy(buf + *buflen, ncp->nc_name, ncp->nc_nlen);
2177 SDT_PROBE3(vfs, namecache, fullpath, hit, ncp->nc_dvp,
2186 SDT_PROBE1(vfs, namecache, fullpath, miss, vp);
2189 vn_lock(*vp, LK_SHARED | LK_RETRY);
2190 error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen);
2193 counter_u64_add(numfullpathfail2, 1);
2194 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2199 if (dvp->v_iflag & VI_DOOMED) {
2200 /* forced unmount */
2203 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2207 * *vp has its use count incremented still.
2214 * The magic behind kern___getcwd() and vn_fullpath().
2217 vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
2218 char *buf, char **retbuf, u_int buflen)
2220 int error, slash_prefixed;
2221 #ifdef KDTRACE_HOOKS
2222 struct vnode *startvp = vp;
2231 SDT_PROBE1(vfs, namecache, fullpath, entry, vp);
2232 counter_u64_add(numfullpathcalls, 1);
2234 if (vp->v_type != VDIR) {
2235 error = vn_vptocnp(&vp, td->td_ucred, buf, &buflen);
2242 buf[--buflen] = '/';
2245 while (vp != rdir && vp != rootvnode) {
2247 * The vp vnode must be already fully constructed,
2248 * since it is either found in namecache or obtained
2249 * from VOP_VPTOCNP(). We may test for VV_ROOT safely
2250 * without obtaining the vnode lock.
2252 if ((vp->v_vflag & VV_ROOT) != 0) {
2253 vn_lock(vp, LK_RETRY | LK_SHARED);
2256 * With the vnode locked, check for races with
2257 * unmount, forced or not. Note that we
2258 * already verified that vp is not equal to
2259 * the root vnode, which means that
2260 * mnt_vnodecovered can be NULL only for the
2263 if ((vp->v_iflag & VI_DOOMED) != 0 ||
2264 (vp1 = vp->v_mount->mnt_vnodecovered) == NULL ||
2265 vp1->v_mountedhere != vp->v_mount) {
2268 SDT_PROBE3(vfs, namecache, fullpath, return,
2278 if (vp->v_type != VDIR) {
2280 counter_u64_add(numfullpathfail1, 1);
2282 SDT_PROBE3(vfs, namecache, fullpath, return,
2286 error = vn_vptocnp(&vp, td->td_ucred, buf, &buflen);
2292 SDT_PROBE3(vfs, namecache, fullpath, return, error,
2296 buf[--buflen] = '/';
2301 if (!slash_prefixed) {
2304 counter_u64_add(numfullpathfail4, 1);
2305 SDT_PROBE3(vfs, namecache, fullpath, return, ENOMEM,
2309 buf[--buflen] = '/';
2311 counter_u64_add(numfullpathfound, 1);
2314 SDT_PROBE3(vfs, namecache, fullpath, return, 0, startvp, buf + buflen);
2315 *retbuf = buf + buflen;
2320 vn_dir_dd_ino(struct vnode *vp)
2322 struct namecache *ncp;
2326 ASSERT_VOP_LOCKED(vp, "vn_dir_dd_ino");
2327 vlp = VP2VNODELOCK(vp);
2329 TAILQ_FOREACH(ncp, &(vp->v_cache_dst), nc_dst) {
2330 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0)
2335 if (vget(ddvp, LK_SHARED | LK_NOWAIT | LK_VNHELD, curthread))
2344 vn_commname(struct vnode *vp, char *buf, u_int buflen)
2346 struct namecache *ncp;
2350 vlp = VP2VNODELOCK(vp);
2352 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst)
2353 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2359 l = min(ncp->nc_nlen, buflen - 1);
2360 memcpy(buf, ncp->nc_name, l);
2366 /* ABI compat shims for old kernel modules. */
2369 void cache_enter(struct vnode *dvp, struct vnode *vp,
2370 struct componentname *cnp);
2373 cache_enter(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2376 cache_enter_time(dvp, vp, cnp, NULL, NULL);
2380 * This function updates path string to vnode's full global path
2381 * and checks the size of the new path string against the pathlen argument.
2383 * Requires a locked, referenced vnode.
2384 * Vnode is re-locked on success or ENODEV, otherwise unlocked.
2386 * If sysctl debug.disablefullpath is set, ENODEV is returned,
2387 * vnode is left locked and path remain untouched.
2389 * If vp is a directory, the call to vn_fullpath_global() always succeeds
2390 * because it falls back to the ".." lookup if the namecache lookup fails.
2393 vn_path_to_global_path(struct thread *td, struct vnode *vp, char *path,
2396 struct nameidata nd;
2401 ASSERT_VOP_ELOCKED(vp, __func__);
2403 /* Return ENODEV if sysctl debug.disablefullpath==1 */
2404 if (__predict_false(disablefullpath))
2407 /* Construct global filesystem path from vp. */
2409 error = vn_fullpath_global(td, vp, &rpath, &fbuf);
2416 if (strlen(rpath) >= pathlen) {
2418 error = ENAMETOOLONG;
2423 * Re-lookup the vnode by path to detect a possible rename.
2424 * As a side effect, the vnode is relocked.
2425 * If vnode was renamed, return ENOENT.
2427 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
2428 UIO_SYSSPACE, path, td);
2434 NDFREE(&nd, NDF_ONLY_PNBUF);
2438 strcpy(path, rpath);