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 LIST_ENTRY(namecache) nc_hash; /* hash chain */
121 LIST_ENTRY(namecache) nc_src; /* source vnode list */
122 TAILQ_ENTRY(namecache) nc_dst; /* destination vnode list */
123 struct vnode *nc_dvp; /* vnode of parent of name */
125 struct vnode *nu_vp; /* vnode the name refers to */
126 u_int nu_neghits; /* negative entry hits */
128 u_char nc_flag; /* flag bits */
129 u_char nc_nlen; /* length of name */
130 struct timespec nc_time; /* timespec provided by fs */
131 struct timespec nc_dotdottime; /* dotdot timespec provided by fs */
132 int nc_ticks; /* ticks value when entry was added */
133 char nc_name[0]; /* segment name + nul */
136 #define nc_vp n_un.nu_vp
137 #define nc_neghits n_un.nu_neghits
140 * Flags in namecache.nc_flag
142 #define NCF_WHITE 0x01
143 #define NCF_ISDOTDOT 0x02
146 #define NCF_DVDROP 0x10
147 #define NCF_NEGATIVE 0x20
148 #define NCF_HOTNEGATIVE 0x40
151 * Name caching works as follows:
153 * Names found by directory scans are retained in a cache
154 * for future reference. It is managed LRU, so frequently
155 * used names will hang around. Cache is indexed by hash value
156 * obtained from (vp, name) where vp refers to the directory
159 * If it is a "negative" entry, (i.e. for a name that is known NOT to
160 * exist) the vnode pointer will be NULL.
162 * Upon reaching the last segment of a path, if the reference
163 * is for DELETE, or NOCACHE is set (rewrite), and the
164 * name is located in the cache, it will be dropped.
166 * These locks are used (in the order in which they can be taken):
168 * vnodelock mtx vnode lists and v_cache_dd field protection
169 * bucketlock rwlock for access to given set of hash buckets
170 * neglist mtx negative entry LRU management
172 * Additionally, ncneg_shrink_lock mtx is used to have at most one thread
173 * shrinking the LRU list.
175 * It is legal to take multiple vnodelock and bucketlock locks. The locking
176 * order is lower address first. Both are recursive.
178 * "." lookups are lockless.
180 * ".." and vnode -> name lookups require vnodelock.
182 * name -> vnode lookup requires the relevant bucketlock to be held for reading.
184 * Insertions and removals of entries require involved vnodes and bucketlocks
185 * to be write-locked to prevent other threads from seeing the entry.
187 * Some lookups result in removal of the found entry (e.g. getting rid of a
188 * negative entry with the intent to create a positive one), which poses a
189 * problem when multiple threads reach the state. Similarly, two different
190 * threads can purge two different vnodes and try to remove the same name.
192 * If the already held vnode lock is lower than the second required lock, we
193 * can just take the other lock. However, in the opposite case, this could
194 * deadlock. As such, this is resolved by trylocking and if that fails unlocking
195 * the first node, locking everything in order and revalidating the state.
199 * Structures associated with name caching.
201 #define NCHHASH(hash) \
202 (&nchashtbl[(hash) & nchash])
203 static LIST_HEAD(nchashhead, namecache) *nchashtbl; /* Hash Table */
204 static u_long nchash; /* size of hash table */
205 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0,
206 "Size of namecache hash table");
207 static u_long ncnegfactor = 16; /* ratio of negative entries */
208 SYSCTL_ULONG(_vfs, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0,
209 "Ratio of negative namecache entries");
210 static u_long numneg; /* number of negative entries allocated */
211 SYSCTL_ULONG(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0,
212 "Number of negative entries in namecache");
213 static u_long numcache; /* number of cache entries allocated */
214 SYSCTL_ULONG(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0,
215 "Number of namecache entries");
216 static u_long numcachehv; /* number of cache entries with vnodes held */
217 SYSCTL_ULONG(_debug, OID_AUTO, numcachehv, CTLFLAG_RD, &numcachehv, 0,
218 "Number of namecache entries with vnodes held");
219 u_int ncsizefactor = 2;
220 SYSCTL_UINT(_vfs, OID_AUTO, ncsizefactor, CTLFLAG_RW, &ncsizefactor, 0,
221 "Size factor for namecache");
222 static u_int ncpurgeminvnodes;
223 SYSCTL_UINT(_vfs, OID_AUTO, ncpurgeminvnodes, CTLFLAG_RW, &ncpurgeminvnodes, 0,
224 "Number of vnodes below which purgevfs ignores the request");
225 static u_int ncneghitsrequeue = 8;
226 SYSCTL_UINT(_vfs, OID_AUTO, ncneghitsrequeue, CTLFLAG_RW, &ncneghitsrequeue, 0,
227 "Number of hits to requeue a negative entry in the LRU list");
229 struct nchstats nchstats; /* cache effectiveness statistics */
231 static struct mtx ncneg_shrink_lock;
235 TAILQ_HEAD(, namecache) nl_list;
236 } __aligned(CACHE_LINE_SIZE);
238 static struct neglist *neglists;
239 static struct neglist ncneg_hot;
241 static int shrink_list_turn;
243 #define numneglists (ncneghash + 1)
244 static u_int ncneghash;
245 static inline struct neglist *
246 NCP2NEGLIST(struct namecache *ncp)
249 return (&neglists[(((uintptr_t)(ncp) >> 8) & ncneghash)]);
252 #define numbucketlocks (ncbuckethash + 1)
253 static u_int ncbuckethash;
254 static struct rwlock_padalign *bucketlocks;
255 #define HASH2BUCKETLOCK(hash) \
256 ((struct rwlock *)(&bucketlocks[((hash) & ncbuckethash)]))
258 #define numvnodelocks (ncvnodehash + 1)
259 static u_int ncvnodehash;
260 static struct mtx *vnodelocks;
261 static inline struct mtx *
262 VP2VNODELOCK(struct vnode *vp)
265 return (&vnodelocks[(((uintptr_t)(vp) >> 8) & ncvnodehash)]);
269 * UMA zones for the VFS cache.
271 * The small cache is used for entries with short names, which are the
272 * most common. The large cache is used for entries which are too big to
273 * fit in the small cache.
275 static uma_zone_t cache_zone_small;
276 static uma_zone_t cache_zone_small_ts;
277 static uma_zone_t cache_zone_large;
278 static uma_zone_t cache_zone_large_ts;
280 #define CACHE_PATH_CUTOFF 35
282 static struct namecache *
283 cache_alloc(int len, int ts)
286 if (len > CACHE_PATH_CUTOFF) {
288 return (uma_zalloc(cache_zone_large_ts, M_WAITOK));
290 return (uma_zalloc(cache_zone_large, M_WAITOK));
293 return (uma_zalloc(cache_zone_small_ts, M_WAITOK));
295 return (uma_zalloc(cache_zone_small, M_WAITOK));
299 cache_free(struct namecache *ncp)
305 ts = ncp->nc_flag & NCF_TS;
306 if ((ncp->nc_flag & NCF_DVDROP) != 0)
308 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF) {
310 uma_zfree(cache_zone_small_ts, ncp);
312 uma_zfree(cache_zone_small, ncp);
314 uma_zfree(cache_zone_large_ts, ncp);
316 uma_zfree(cache_zone_large, ncp);
320 nc_get_name(struct namecache *ncp)
322 struct namecache_ts *ncp_ts;
324 if ((ncp->nc_flag & NCF_TS) == 0)
325 return (ncp->nc_name);
326 ncp_ts = (struct namecache_ts *)ncp;
327 return (ncp_ts->nc_name);
331 cache_out_ts(struct namecache *ncp, struct timespec *tsp, int *ticksp)
334 KASSERT((ncp->nc_flag & NCF_TS) != 0 ||
335 (tsp == NULL && ticksp == NULL),
339 *tsp = ((struct namecache_ts *)ncp)->nc_time;
341 *ticksp = ((struct namecache_ts *)ncp)->nc_ticks;
344 static int doingcache = 1; /* 1 => enable the cache */
345 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0,
346 "VFS namecache enabled");
348 /* Export size information to userland */
349 SYSCTL_INT(_debug_sizeof, OID_AUTO, namecache, CTLFLAG_RD, SYSCTL_NULL_INT_PTR,
350 sizeof(struct namecache), "sizeof(struct namecache)");
353 * The new name cache statistics
355 static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0,
356 "Name cache statistics");
357 #define STATNODE_ULONG(name, descr) \
358 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, descr);
359 #define STATNODE_COUNTER(name, descr) \
360 static counter_u64_t name; \
361 SYSCTL_COUNTER_U64(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, descr);
362 STATNODE_ULONG(numneg, "Number of negative cache entries");
363 STATNODE_ULONG(numcache, "Number of cache entries");
364 STATNODE_COUNTER(numcalls, "Number of cache lookups");
365 STATNODE_COUNTER(dothits, "Number of '.' hits");
366 STATNODE_COUNTER(dotdothits, "Number of '..' hits");
367 STATNODE_COUNTER(numchecks, "Number of checks in lookup");
368 STATNODE_COUNTER(nummiss, "Number of cache misses");
369 STATNODE_COUNTER(nummisszap, "Number of cache misses we do not want to cache");
370 STATNODE_COUNTER(numposzaps,
371 "Number of cache hits (positive) we do not want to cache");
372 STATNODE_COUNTER(numposhits, "Number of cache hits (positive)");
373 STATNODE_COUNTER(numnegzaps,
374 "Number of cache hits (negative) we do not want to cache");
375 STATNODE_COUNTER(numneghits, "Number of cache hits (negative)");
376 /* These count for kern___getcwd(), too. */
377 STATNODE_COUNTER(numfullpathcalls, "Number of fullpath search calls");
378 STATNODE_COUNTER(numfullpathfail1, "Number of fullpath search errors (ENOTDIR)");
379 STATNODE_COUNTER(numfullpathfail2,
380 "Number of fullpath search errors (VOP_VPTOCNP failures)");
381 STATNODE_COUNTER(numfullpathfail4, "Number of fullpath search errors (ENOMEM)");
382 STATNODE_COUNTER(numfullpathfound, "Number of successful fullpath calls");
383 static long zap_and_exit_bucket_fail; STATNODE_ULONG(zap_and_exit_bucket_fail,
384 "Number of times zap_and_exit failed to lock");
385 static long cache_lock_vnodes_cel_3_failures;
386 STATNODE_ULONG(cache_lock_vnodes_cel_3_failures,
387 "Number of times 3-way vnode locking failed");
389 static void cache_zap_locked(struct namecache *ncp, bool neg_locked);
390 static int vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
391 char *buf, char **retbuf, u_int buflen);
393 static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
395 static int cache_yield;
396 SYSCTL_INT(_vfs_cache, OID_AUTO, yield, CTLFLAG_RD, &cache_yield, 0,
397 "Number of times cache called yield");
400 cache_maybe_yield(void)
403 if (should_yield()) {
405 kern_yield(PRI_USER);
410 cache_assert_vlp_locked(struct mtx *vlp)
414 mtx_assert(vlp, MA_OWNED);
418 cache_assert_vnode_locked(struct vnode *vp)
422 vlp = VP2VNODELOCK(vp);
423 cache_assert_vlp_locked(vlp);
427 cache_get_hash(char *name, u_char len, struct vnode *dvp)
431 hash = fnv_32_buf(name, len, FNV1_32_INIT);
432 hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
436 static inline struct rwlock *
437 NCP2BUCKETLOCK(struct namecache *ncp)
441 hash = cache_get_hash(nc_get_name(ncp), ncp->nc_nlen, ncp->nc_dvp);
442 return (HASH2BUCKETLOCK(hash));
447 cache_assert_bucket_locked(struct namecache *ncp, int mode)
451 blp = NCP2BUCKETLOCK(ncp);
452 rw_assert(blp, mode);
455 #define cache_assert_bucket_locked(x, y) do { } while (0)
458 #define cache_sort(x, y) _cache_sort((void **)(x), (void **)(y))
460 _cache_sort(void **p1, void **p2)
472 cache_lock_all_buckets(void)
476 for (i = 0; i < numbucketlocks; i++)
477 rw_wlock(&bucketlocks[i]);
481 cache_unlock_all_buckets(void)
485 for (i = 0; i < numbucketlocks; i++)
486 rw_wunlock(&bucketlocks[i]);
490 cache_lock_all_vnodes(void)
494 for (i = 0; i < numvnodelocks; i++)
495 mtx_lock(&vnodelocks[i]);
499 cache_unlock_all_vnodes(void)
503 for (i = 0; i < numvnodelocks; i++)
504 mtx_unlock(&vnodelocks[i]);
508 cache_trylock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
511 cache_sort(&vlp1, &vlp2);
515 if (!mtx_trylock(vlp1))
518 if (!mtx_trylock(vlp2)) {
528 cache_unlock_vnodes(struct mtx *vlp1, struct mtx *vlp2)
531 MPASS(vlp1 != NULL || vlp2 != NULL);
540 sysctl_nchstats(SYSCTL_HANDLER_ARGS)
542 struct nchstats snap;
544 if (req->oldptr == NULL)
545 return (SYSCTL_OUT(req, 0, sizeof(snap)));
548 snap.ncs_goodhits = counter_u64_fetch(numposhits);
549 snap.ncs_neghits = counter_u64_fetch(numneghits);
550 snap.ncs_badhits = counter_u64_fetch(numposzaps) +
551 counter_u64_fetch(numnegzaps);
552 snap.ncs_miss = counter_u64_fetch(nummisszap) +
553 counter_u64_fetch(nummiss);
555 return (SYSCTL_OUT(req, &snap, sizeof(snap)));
557 SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE | CTLFLAG_RD |
558 CTLFLAG_MPSAFE, 0, 0, sysctl_nchstats, "LU",
559 "VFS cache effectiveness statistics");
563 * Grab an atomic snapshot of the name cache hash chain lengths
565 static SYSCTL_NODE(_debug, OID_AUTO, hashstat, CTLFLAG_RW, NULL,
569 sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS)
571 struct nchashhead *ncpp;
572 struct namecache *ncp;
573 int i, error, n_nchash, *cntbuf;
576 n_nchash = nchash + 1; /* nchash is max index, not count */
577 if (req->oldptr == NULL)
578 return SYSCTL_OUT(req, 0, n_nchash * sizeof(int));
579 cntbuf = malloc(n_nchash * sizeof(int), M_TEMP, M_ZERO | M_WAITOK);
580 cache_lock_all_buckets();
581 if (n_nchash != nchash + 1) {
582 cache_unlock_all_buckets();
583 free(cntbuf, M_TEMP);
586 /* Scan hash tables counting entries */
587 for (ncpp = nchashtbl, i = 0; i < n_nchash; ncpp++, i++)
588 LIST_FOREACH(ncp, ncpp, nc_hash)
590 cache_unlock_all_buckets();
591 for (error = 0, i = 0; i < n_nchash; i++)
592 if ((error = SYSCTL_OUT(req, &cntbuf[i], sizeof(int))) != 0)
594 free(cntbuf, M_TEMP);
597 SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD|
598 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_rawnchash, "S,int",
599 "nchash chain lengths");
602 sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS)
605 struct nchashhead *ncpp;
606 struct namecache *ncp;
608 int count, maxlength, used, pct;
611 return SYSCTL_OUT(req, 0, 4 * sizeof(int));
613 cache_lock_all_buckets();
614 n_nchash = nchash + 1; /* nchash is max index, not count */
618 /* Scan hash tables for applicable entries */
619 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
621 LIST_FOREACH(ncp, ncpp, nc_hash) {
626 if (maxlength < count)
629 n_nchash = nchash + 1;
630 cache_unlock_all_buckets();
631 pct = (used * 100) / (n_nchash / 100);
632 error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash));
635 error = SYSCTL_OUT(req, &used, sizeof(used));
638 error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength));
641 error = SYSCTL_OUT(req, &pct, sizeof(pct));
646 SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD|
647 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_nchash, "I",
648 "nchash statistics (number of total/used buckets, maximum chain length, usage percentage)");
652 * Negative entries management
654 * A variation of LRU scheme is used. New entries are hashed into one of
655 * numneglists cold lists. Entries get promoted to the hot list on first hit.
656 * Partial LRU for the hot list is maintained by requeueing them every
657 * ncneghitsrequeue hits.
659 * The shrinker will demote hot list head and evict from the cold list in a
660 * round-robin manner.
663 cache_negative_hit(struct namecache *ncp)
665 struct neglist *neglist;
668 MPASS(ncp->nc_flag & NCF_NEGATIVE);
669 hits = atomic_fetchadd_int(&ncp->nc_neghits, 1);
670 if (ncp->nc_flag & NCF_HOTNEGATIVE) {
671 if ((hits % ncneghitsrequeue) != 0)
673 mtx_lock(&ncneg_hot.nl_lock);
674 if (ncp->nc_flag & NCF_HOTNEGATIVE) {
675 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
676 TAILQ_INSERT_TAIL(&ncneg_hot.nl_list, ncp, nc_dst);
677 mtx_unlock(&ncneg_hot.nl_lock);
681 * The shrinker cleared the flag and removed the entry from
682 * the hot list. Put it back.
685 mtx_lock(&ncneg_hot.nl_lock);
687 neglist = NCP2NEGLIST(ncp);
688 mtx_lock(&neglist->nl_lock);
689 if (!(ncp->nc_flag & NCF_HOTNEGATIVE)) {
690 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
691 TAILQ_INSERT_TAIL(&ncneg_hot.nl_list, ncp, nc_dst);
692 ncp->nc_flag |= NCF_HOTNEGATIVE;
694 mtx_unlock(&neglist->nl_lock);
695 mtx_unlock(&ncneg_hot.nl_lock);
699 cache_negative_insert(struct namecache *ncp, bool neg_locked)
701 struct neglist *neglist;
703 MPASS(ncp->nc_flag & NCF_NEGATIVE);
704 cache_assert_bucket_locked(ncp, RA_WLOCKED);
705 neglist = NCP2NEGLIST(ncp);
707 mtx_lock(&neglist->nl_lock);
709 mtx_assert(&neglist->nl_lock, MA_OWNED);
711 TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst);
713 mtx_unlock(&neglist->nl_lock);
714 atomic_add_rel_long(&numneg, 1);
718 cache_negative_remove(struct namecache *ncp, bool neg_locked)
720 struct neglist *neglist;
721 bool hot_locked = false;
722 bool list_locked = false;
724 MPASS(ncp->nc_flag & NCF_NEGATIVE);
725 cache_assert_bucket_locked(ncp, RA_WLOCKED);
726 neglist = NCP2NEGLIST(ncp);
728 if (ncp->nc_flag & NCF_HOTNEGATIVE) {
730 mtx_lock(&ncneg_hot.nl_lock);
731 if (!(ncp->nc_flag & NCF_HOTNEGATIVE)) {
733 mtx_lock(&neglist->nl_lock);
737 mtx_lock(&neglist->nl_lock);
740 mtx_assert(&neglist->nl_lock, MA_OWNED);
741 mtx_assert(&ncneg_hot.nl_lock, MA_OWNED);
743 if (ncp->nc_flag & NCF_HOTNEGATIVE) {
744 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
746 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst);
749 mtx_unlock(&neglist->nl_lock);
751 mtx_unlock(&ncneg_hot.nl_lock);
752 atomic_subtract_rel_long(&numneg, 1);
756 cache_negative_shrink_select(int start, struct namecache **ncpp,
757 struct neglist **neglistpp)
759 struct neglist *neglist;
760 struct namecache *ncp;
765 for (i = start; i < numneglists; i++) {
766 neglist = &neglists[i];
767 if (TAILQ_FIRST(&neglist->nl_list) == NULL)
769 mtx_lock(&neglist->nl_lock);
770 ncp = TAILQ_FIRST(&neglist->nl_list);
773 mtx_unlock(&neglist->nl_lock);
776 *neglistpp = neglist;
781 cache_negative_zap_one(void)
783 struct namecache *ncp, *ncp2;
784 struct neglist *neglist;
788 if (!mtx_trylock(&ncneg_shrink_lock))
791 mtx_lock(&ncneg_hot.nl_lock);
792 ncp = TAILQ_FIRST(&ncneg_hot.nl_list);
794 neglist = NCP2NEGLIST(ncp);
795 mtx_lock(&neglist->nl_lock);
796 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst);
797 TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst);
798 ncp->nc_flag &= ~NCF_HOTNEGATIVE;
799 mtx_unlock(&neglist->nl_lock);
802 cache_negative_shrink_select(shrink_list_turn, &ncp, &neglist);
804 if (shrink_list_turn == numneglists)
805 shrink_list_turn = 0;
806 if (ncp == NULL && shrink_list_turn == 0)
807 cache_negative_shrink_select(shrink_list_turn, &ncp, &neglist);
809 mtx_unlock(&ncneg_hot.nl_lock);
813 MPASS(ncp->nc_flag & NCF_NEGATIVE);
814 dvlp = VP2VNODELOCK(ncp->nc_dvp);
815 blp = NCP2BUCKETLOCK(ncp);
816 mtx_unlock(&neglist->nl_lock);
817 mtx_unlock(&ncneg_hot.nl_lock);
820 mtx_lock(&ncneg_hot.nl_lock);
821 mtx_lock(&neglist->nl_lock);
822 ncp2 = TAILQ_FIRST(&neglist->nl_list);
823 if (ncp != ncp2 || dvlp != VP2VNODELOCK(ncp2->nc_dvp) ||
824 blp != NCP2BUCKETLOCK(ncp2) || !(ncp2->nc_flag & NCF_NEGATIVE)) {
828 SDT_PROBE3(vfs, namecache, shrink_negative, done, ncp->nc_dvp,
829 nc_get_name(ncp), ncp->nc_neghits);
831 cache_zap_locked(ncp, true);
833 mtx_unlock(&neglist->nl_lock);
834 mtx_unlock(&ncneg_hot.nl_lock);
838 mtx_unlock(&ncneg_shrink_lock);
843 * cache_zap_locked():
845 * Removes a namecache entry from cache, whether it contains an actual
846 * pointer to a vnode or if it is just a negative cache entry.
849 cache_zap_locked(struct namecache *ncp, bool neg_locked)
852 if (!(ncp->nc_flag & NCF_NEGATIVE))
853 cache_assert_vnode_locked(ncp->nc_vp);
854 cache_assert_vnode_locked(ncp->nc_dvp);
855 cache_assert_bucket_locked(ncp, RA_WLOCKED);
857 CTR2(KTR_VFS, "cache_zap(%p) vp %p", ncp,
858 (ncp->nc_flag & NCF_NEGATIVE) ? NULL : ncp->nc_vp);
859 if (!(ncp->nc_flag & NCF_NEGATIVE)) {
860 SDT_PROBE3(vfs, namecache, zap, done, ncp->nc_dvp,
861 nc_get_name(ncp), ncp->nc_vp);
863 SDT_PROBE3(vfs, namecache, zap_negative, done, ncp->nc_dvp,
864 nc_get_name(ncp), ncp->nc_neghits);
866 LIST_REMOVE(ncp, nc_hash);
867 if (!(ncp->nc_flag & NCF_NEGATIVE)) {
868 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
869 if (ncp == ncp->nc_vp->v_cache_dd)
870 ncp->nc_vp->v_cache_dd = NULL;
872 cache_negative_remove(ncp, neg_locked);
874 if (ncp->nc_flag & NCF_ISDOTDOT) {
875 if (ncp == ncp->nc_dvp->v_cache_dd)
876 ncp->nc_dvp->v_cache_dd = NULL;
878 LIST_REMOVE(ncp, nc_src);
879 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) {
880 ncp->nc_flag |= NCF_DVDROP;
881 atomic_subtract_rel_long(&numcachehv, 1);
884 atomic_subtract_rel_long(&numcache, 1);
888 cache_zap_negative_locked_vnode_kl(struct namecache *ncp, struct vnode *vp)
892 MPASS(ncp->nc_dvp == vp);
893 MPASS(ncp->nc_flag & NCF_NEGATIVE);
894 cache_assert_vnode_locked(vp);
896 blp = NCP2BUCKETLOCK(ncp);
898 cache_zap_locked(ncp, false);
903 cache_zap_locked_vnode_kl2(struct namecache *ncp, struct vnode *vp,
906 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
909 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
910 cache_assert_vnode_locked(vp);
912 if (ncp->nc_flag & NCF_NEGATIVE) {
917 cache_zap_negative_locked_vnode_kl(ncp, vp);
921 pvlp = VP2VNODELOCK(vp);
922 blp = NCP2BUCKETLOCK(ncp);
923 vlp1 = VP2VNODELOCK(ncp->nc_dvp);
924 vlp2 = VP2VNODELOCK(ncp->nc_vp);
926 if (*vlpp == vlp1 || *vlpp == vlp2) {
934 cache_sort(&vlp1, &vlp2);
939 if (!mtx_trylock(vlp1))
945 cache_zap_locked(ncp, false);
947 if (to_unlock != NULL)
948 mtx_unlock(to_unlock);
955 MPASS(*vlpp == NULL);
961 cache_zap_locked_vnode(struct namecache *ncp, struct vnode *vp)
963 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock;
967 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp);
968 cache_assert_vnode_locked(vp);
970 pvlp = VP2VNODELOCK(vp);
971 if (ncp->nc_flag & NCF_NEGATIVE) {
972 cache_zap_negative_locked_vnode_kl(ncp, vp);
976 blp = NCP2BUCKETLOCK(ncp);
977 vlp1 = VP2VNODELOCK(ncp->nc_dvp);
978 vlp2 = VP2VNODELOCK(ncp->nc_vp);
979 cache_sort(&vlp1, &vlp2);
984 if (!mtx_trylock(vlp1)) {
991 cache_zap_locked(ncp, false);
993 mtx_unlock(to_unlock);
1000 cache_zap_rlocked_bucket(struct namecache *ncp, struct rwlock *blp)
1002 struct mtx *dvlp, *vlp;
1004 cache_assert_bucket_locked(ncp, RA_RLOCKED);
1006 dvlp = VP2VNODELOCK(ncp->nc_dvp);
1008 if (!(ncp->nc_flag & NCF_NEGATIVE))
1009 vlp = VP2VNODELOCK(ncp->nc_vp);
1010 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1013 cache_zap_locked(ncp, false);
1015 cache_unlock_vnodes(dvlp, vlp);
1024 cache_zap_wlocked_bucket_kl(struct namecache *ncp, struct rwlock *blp,
1025 struct mtx **vlpp1, struct mtx **vlpp2)
1027 struct mtx *dvlp, *vlp;
1029 cache_assert_bucket_locked(ncp, RA_WLOCKED);
1031 dvlp = VP2VNODELOCK(ncp->nc_dvp);
1033 if (!(ncp->nc_flag & NCF_NEGATIVE))
1034 vlp = VP2VNODELOCK(ncp->nc_vp);
1035 cache_sort(&dvlp, &vlp);
1037 if (*vlpp1 == dvlp && *vlpp2 == vlp) {
1038 cache_zap_locked(ncp, false);
1039 cache_unlock_vnodes(dvlp, vlp);
1052 if (cache_trylock_vnodes(dvlp, vlp) == 0) {
1053 cache_zap_locked(ncp, false);
1054 cache_unlock_vnodes(dvlp, vlp);
1069 cache_lookup_unlock(struct rwlock *blp, struct mtx *vlp)
1074 mtx_assert(vlp, MA_NOTOWNED);
1081 * Lookup an entry in the cache
1083 * Lookup is called with dvp pointing to the directory to search,
1084 * cnp pointing to the name of the entry being sought. If the lookup
1085 * succeeds, the vnode is returned in *vpp, and a status of -1 is
1086 * returned. If the lookup determines that the name does not exist
1087 * (negative caching), a status of ENOENT is returned. If the lookup
1088 * fails, a status of zero is returned. If the directory vnode is
1089 * recycled out from under us due to a forced unmount, a status of
1090 * ENOENT is returned.
1092 * vpp is locked and ref'd on return. If we're looking up DOTDOT, dvp is
1093 * unlocked. If we're looking up . an extra ref is taken, but the lock is
1094 * not recursively acquired.
1098 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1099 struct timespec *tsp, int *ticksp)
1101 struct namecache *ncp;
1103 struct mtx *dvlp, *dvlp2;
1107 if (__predict_false(!doingcache)) {
1108 cnp->cn_flags &= ~MAKEENTRY;
1113 dvlp = VP2VNODELOCK(dvp);
1115 counter_u64_add(numcalls, 1);
1117 if (cnp->cn_nameptr[0] == '.') {
1118 if (cnp->cn_namelen == 1) {
1120 CTR2(KTR_VFS, "cache_lookup(%p, %s) found via .",
1121 dvp, cnp->cn_nameptr);
1122 counter_u64_add(dothits, 1);
1123 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", *vpp);
1130 * When we lookup "." we still can be asked to lock it
1133 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
1134 if (ltype != VOP_ISLOCKED(*vpp)) {
1135 if (ltype == LK_EXCLUSIVE) {
1136 vn_lock(*vpp, LK_UPGRADE | LK_RETRY);
1137 if ((*vpp)->v_iflag & VI_DOOMED) {
1138 /* forced unmount */
1144 vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY);
1148 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
1149 counter_u64_add(dotdothits, 1);
1153 ncp = dvp->v_cache_dd;
1155 SDT_PROBE3(vfs, namecache, lookup, miss, dvp,
1160 if ((cnp->cn_flags & MAKEENTRY) == 0) {
1161 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1162 if (ncp->nc_dvp != dvp)
1163 panic("dvp %p v_cache_dd %p\n", dvp, ncp);
1164 if (!cache_zap_locked_vnode_kl2(ncp,
1167 MPASS(dvp->v_cache_dd == NULL);
1173 dvp->v_cache_dd = NULL;
1180 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) {
1181 if (ncp->nc_flag & NCF_NEGATIVE)
1187 /* Return failure if negative entry was found. */
1189 goto negative_success;
1190 CTR3(KTR_VFS, "cache_lookup(%p, %s) found %p via ..",
1191 dvp, cnp->cn_nameptr, *vpp);
1192 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, "..",
1194 cache_out_ts(ncp, tsp, ticksp);
1195 if ((ncp->nc_flag & (NCF_ISDOTDOT | NCF_DTS)) ==
1196 NCF_DTS && tsp != NULL)
1197 *tsp = ((struct namecache_ts *)ncp)->
1203 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
1204 blp = HASH2BUCKETLOCK(hash);
1207 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
1208 counter_u64_add(numchecks, 1);
1209 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
1210 !bcmp(nc_get_name(ncp), cnp->cn_nameptr, ncp->nc_nlen))
1214 /* We failed to find an entry */
1216 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
1218 if ((cnp->cn_flags & MAKEENTRY) == 0) {
1219 counter_u64_add(nummisszap, 1);
1221 counter_u64_add(nummiss, 1);
1226 /* We don't want to have an entry, so dump it */
1227 if ((cnp->cn_flags & MAKEENTRY) == 0) {
1228 counter_u64_add(numposzaps, 1);
1232 /* We found a "positive" match, return the vnode */
1233 if (!(ncp->nc_flag & NCF_NEGATIVE)) {
1234 counter_u64_add(numposhits, 1);
1236 CTR4(KTR_VFS, "cache_lookup(%p, %s) found %p via ncp %p",
1237 dvp, cnp->cn_nameptr, *vpp, ncp);
1238 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, nc_get_name(ncp),
1240 cache_out_ts(ncp, tsp, ticksp);
1245 /* We found a negative match, and want to create it, so purge */
1246 if (cnp->cn_nameiop == CREATE) {
1247 counter_u64_add(numnegzaps, 1);
1251 counter_u64_add(numneghits, 1);
1252 cache_negative_hit(ncp);
1253 if (ncp->nc_flag & NCF_WHITE)
1254 cnp->cn_flags |= ISWHITEOUT;
1255 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp,
1257 cache_out_ts(ncp, tsp, ticksp);
1258 cache_lookup_unlock(blp, dvlp);
1263 * On success we return a locked and ref'd vnode as per the lookup
1267 ltype = 0; /* silence gcc warning */
1268 if (cnp->cn_flags & ISDOTDOT) {
1269 ltype = VOP_ISLOCKED(dvp);
1273 cache_lookup_unlock(blp, dvlp);
1274 error = vget(*vpp, cnp->cn_lkflags | LK_VNHELD, cnp->cn_thread);
1275 if (cnp->cn_flags & ISDOTDOT) {
1276 vn_lock(dvp, ltype | LK_RETRY);
1277 if (dvp->v_iflag & VI_DOOMED) {
1288 if ((cnp->cn_flags & ISLASTCN) &&
1289 (cnp->cn_lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE) {
1290 ASSERT_VOP_ELOCKED(*vpp, "cache_lookup");
1295 cache_lookup_unlock(blp, dvlp);
1300 error = cache_zap_rlocked_bucket(ncp, blp);
1302 error = cache_zap_locked_vnode(ncp, dvp);
1304 zap_and_exit_bucket_fail++;
1305 cache_maybe_yield();
1312 struct celockstate {
1314 struct rwlock *blp[2];
1316 CTASSERT((nitems(((struct celockstate *)0)->vlp) == 3));
1317 CTASSERT((nitems(((struct celockstate *)0)->blp) == 2));
1320 cache_celockstate_init(struct celockstate *cel)
1323 bzero(cel, sizeof(*cel));
1327 cache_lock_vnodes_cel(struct celockstate *cel, struct vnode *vp,
1330 struct mtx *vlp1, *vlp2;
1332 MPASS(cel->vlp[0] == NULL);
1333 MPASS(cel->vlp[1] == NULL);
1334 MPASS(cel->vlp[2] == NULL);
1336 MPASS(vp != NULL || dvp != NULL);
1338 vlp1 = VP2VNODELOCK(vp);
1339 vlp2 = VP2VNODELOCK(dvp);
1340 cache_sort(&vlp1, &vlp2);
1351 cache_unlock_vnodes_cel(struct celockstate *cel)
1354 MPASS(cel->vlp[0] != NULL || cel->vlp[1] != NULL);
1356 if (cel->vlp[0] != NULL)
1357 mtx_unlock(cel->vlp[0]);
1358 if (cel->vlp[1] != NULL)
1359 mtx_unlock(cel->vlp[1]);
1360 if (cel->vlp[2] != NULL)
1361 mtx_unlock(cel->vlp[2]);
1365 cache_lock_vnodes_cel_3(struct celockstate *cel, struct vnode *vp)
1370 cache_assert_vlp_locked(cel->vlp[0]);
1371 cache_assert_vlp_locked(cel->vlp[1]);
1372 MPASS(cel->vlp[2] == NULL);
1375 vlp = VP2VNODELOCK(vp);
1378 if (vlp >= cel->vlp[1]) {
1381 if (mtx_trylock(vlp))
1383 cache_lock_vnodes_cel_3_failures++;
1384 cache_unlock_vnodes_cel(cel);
1385 if (vlp < cel->vlp[0]) {
1387 mtx_lock(cel->vlp[0]);
1388 mtx_lock(cel->vlp[1]);
1390 if (cel->vlp[0] != NULL)
1391 mtx_lock(cel->vlp[0]);
1393 mtx_lock(cel->vlp[1]);
1403 cache_lock_buckets_cel(struct celockstate *cel, struct rwlock *blp1,
1404 struct rwlock *blp2)
1407 MPASS(cel->blp[0] == NULL);
1408 MPASS(cel->blp[1] == NULL);
1410 cache_sort(&blp1, &blp2);
1421 cache_unlock_buckets_cel(struct celockstate *cel)
1424 if (cel->blp[0] != NULL)
1425 rw_wunlock(cel->blp[0]);
1426 rw_wunlock(cel->blp[1]);
1430 * Lock part of the cache affected by the insertion.
1432 * This means vnodelocks for dvp, vp and the relevant bucketlock.
1433 * However, insertion can result in removal of an old entry. In this
1434 * case we have an additional vnode and bucketlock pair to lock. If the
1435 * entry is negative, ncelock is locked instead of the vnode.
1437 * That is, in the worst case we have to lock 3 vnodes and 2 bucketlocks, while
1438 * preserving the locking order (smaller address first).
1441 cache_enter_lock(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1444 struct namecache *ncp;
1445 struct rwlock *blps[2];
1447 blps[0] = HASH2BUCKETLOCK(hash);
1450 cache_lock_vnodes_cel(cel, dvp, vp);
1451 if (vp == NULL || vp->v_type != VDIR)
1453 ncp = vp->v_cache_dd;
1456 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1458 MPASS(ncp->nc_dvp == vp);
1459 blps[1] = NCP2BUCKETLOCK(ncp);
1460 if (ncp->nc_flag & NCF_NEGATIVE)
1462 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1465 * All vnodes got re-locked. Re-validate the state and if
1466 * nothing changed we are done. Otherwise restart.
1468 if (ncp == vp->v_cache_dd &&
1469 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1470 blps[1] == NCP2BUCKETLOCK(ncp) &&
1471 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1473 cache_unlock_vnodes_cel(cel);
1478 cache_lock_buckets_cel(cel, blps[0], blps[1]);
1482 cache_enter_lock_dd(struct celockstate *cel, struct vnode *dvp, struct vnode *vp,
1485 struct namecache *ncp;
1486 struct rwlock *blps[2];
1488 blps[0] = HASH2BUCKETLOCK(hash);
1491 cache_lock_vnodes_cel(cel, dvp, vp);
1492 ncp = dvp->v_cache_dd;
1495 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1497 MPASS(ncp->nc_dvp == dvp);
1498 blps[1] = NCP2BUCKETLOCK(ncp);
1499 if (ncp->nc_flag & NCF_NEGATIVE)
1501 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp))
1503 if (ncp == dvp->v_cache_dd &&
1504 (ncp->nc_flag & NCF_ISDOTDOT) != 0 &&
1505 blps[1] == NCP2BUCKETLOCK(ncp) &&
1506 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2])
1508 cache_unlock_vnodes_cel(cel);
1513 cache_lock_buckets_cel(cel, blps[0], blps[1]);
1517 cache_enter_unlock(struct celockstate *cel)
1520 cache_unlock_buckets_cel(cel);
1521 cache_unlock_vnodes_cel(cel);
1525 * Add an entry to the cache.
1528 cache_enter_time(struct vnode *dvp, struct vnode *vp, struct componentname *cnp,
1529 struct timespec *tsp, struct timespec *dtsp)
1531 struct celockstate cel;
1532 struct namecache *ncp, *n2, *ndd;
1533 struct namecache_ts *n3;
1534 struct nchashhead *ncpp;
1535 struct neglist *neglist;
1541 CTR3(KTR_VFS, "cache_enter(%p, %p, %s)", dvp, vp, cnp->cn_nameptr);
1542 VNASSERT(vp == NULL || (vp->v_iflag & VI_DOOMED) == 0, vp,
1543 ("cache_enter: Adding a doomed vnode"));
1544 VNASSERT(dvp == NULL || (dvp->v_iflag & VI_DOOMED) == 0, dvp,
1545 ("cache_enter: Doomed vnode used as src"));
1547 if (__predict_false(!doingcache))
1551 * Avoid blowout in namecache entries.
1553 if (__predict_false(numcache >= desiredvnodes * ncsizefactor))
1556 cache_celockstate_init(&cel);
1559 if (cnp->cn_nameptr[0] == '.') {
1560 if (cnp->cn_namelen == 1)
1562 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
1563 len = cnp->cn_namelen;
1564 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1565 cache_enter_lock_dd(&cel, dvp, vp, hash);
1567 * If dotdot entry already exists, just retarget it
1568 * to new parent vnode, otherwise continue with new
1569 * namecache entry allocation.
1571 if ((ncp = dvp->v_cache_dd) != NULL &&
1572 ncp->nc_flag & NCF_ISDOTDOT) {
1573 KASSERT(ncp->nc_dvp == dvp,
1574 ("wrong isdotdot parent"));
1576 if (ncp->nc_flag & NCF_NEGATIVE || vp == NULL) {
1577 neglist = NCP2NEGLIST(ncp);
1578 mtx_lock(&ncneg_hot.nl_lock);
1579 mtx_lock(&neglist->nl_lock);
1582 if (!(ncp->nc_flag & NCF_NEGATIVE)) {
1583 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst,
1586 cache_negative_remove(ncp, true);
1589 TAILQ_INSERT_HEAD(&vp->v_cache_dst,
1591 ncp->nc_flag &= ~(NCF_NEGATIVE|NCF_HOTNEGATIVE);
1593 ncp->nc_flag &= ~(NCF_HOTNEGATIVE);
1594 ncp->nc_flag |= NCF_NEGATIVE;
1595 cache_negative_insert(ncp, true);
1598 mtx_unlock(&neglist->nl_lock);
1599 mtx_unlock(&ncneg_hot.nl_lock);
1602 cache_enter_unlock(&cel);
1605 dvp->v_cache_dd = NULL;
1606 cache_enter_unlock(&cel);
1607 cache_celockstate_init(&cel);
1608 SDT_PROBE3(vfs, namecache, enter, done, dvp, "..", vp);
1609 flag = NCF_ISDOTDOT;
1614 * Calculate the hash key and setup as much of the new
1615 * namecache entry as possible before acquiring the lock.
1617 ncp = cache_alloc(cnp->cn_namelen, tsp != NULL);
1618 ncp->nc_flag = flag;
1621 ncp->nc_flag |= NCF_NEGATIVE;
1624 n3 = (struct namecache_ts *)ncp;
1626 n3->nc_ticks = ticks;
1627 n3->nc_flag |= NCF_TS;
1629 n3->nc_dotdottime = *dtsp;
1630 n3->nc_flag |= NCF_DTS;
1633 len = ncp->nc_nlen = cnp->cn_namelen;
1634 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
1635 strlcpy(nc_get_name(ncp), cnp->cn_nameptr, len + 1);
1636 cache_enter_lock(&cel, dvp, vp, hash);
1639 * See if this vnode or negative entry is already in the cache
1640 * with this name. This can happen with concurrent lookups of
1641 * the same path name.
1643 ncpp = NCHHASH(hash);
1644 LIST_FOREACH(n2, ncpp, nc_hash) {
1645 if (n2->nc_dvp == dvp &&
1646 n2->nc_nlen == cnp->cn_namelen &&
1647 !bcmp(nc_get_name(n2), cnp->cn_nameptr, n2->nc_nlen)) {
1649 KASSERT((n2->nc_flag & NCF_TS) != 0,
1651 n3 = (struct namecache_ts *)n2;
1653 ((struct namecache_ts *)ncp)->nc_time;
1655 ((struct namecache_ts *)ncp)->nc_ticks;
1658 ((struct namecache_ts *)ncp)->
1660 if (ncp->nc_flag & NCF_NEGATIVE)
1661 mtx_lock(&ncneg_hot.nl_lock);
1662 n3->nc_flag |= NCF_DTS;
1663 if (ncp->nc_flag & NCF_NEGATIVE)
1664 mtx_unlock(&ncneg_hot.nl_lock);
1667 goto out_unlock_free;
1671 if (flag == NCF_ISDOTDOT) {
1673 * See if we are trying to add .. entry, but some other lookup
1674 * has populated v_cache_dd pointer already.
1676 if (dvp->v_cache_dd != NULL)
1677 goto out_unlock_free;
1678 KASSERT(vp == NULL || vp->v_type == VDIR,
1679 ("wrong vnode type %p", vp));
1680 dvp->v_cache_dd = ncp;
1683 atomic_add_rel_long(&numcache, 1);
1685 if (vp->v_type == VDIR) {
1686 if (flag != NCF_ISDOTDOT) {
1688 * For this case, the cache entry maps both the
1689 * directory name in it and the name ".." for the
1690 * directory's parent.
1692 if ((ndd = vp->v_cache_dd) != NULL) {
1693 if ((ndd->nc_flag & NCF_ISDOTDOT) != 0)
1694 cache_zap_locked(ndd, false);
1698 vp->v_cache_dd = ncp;
1701 vp->v_cache_dd = NULL;
1705 if (flag != NCF_ISDOTDOT) {
1706 if (LIST_EMPTY(&dvp->v_cache_src)) {
1708 atomic_add_rel_long(&numcachehv, 1);
1710 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
1714 * Insert the new namecache entry into the appropriate chain
1715 * within the cache entries table.
1717 LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
1720 * If the entry is "negative", we place it into the
1721 * "negative" cache queue, otherwise, we place it into the
1722 * destination vnode's cache entries queue.
1725 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
1726 SDT_PROBE3(vfs, namecache, enter, done, dvp, nc_get_name(ncp),
1729 if (cnp->cn_flags & ISWHITEOUT)
1730 ncp->nc_flag |= NCF_WHITE;
1731 cache_negative_insert(ncp, false);
1732 SDT_PROBE2(vfs, namecache, enter_negative, done, dvp,
1735 cache_enter_unlock(&cel);
1736 if (numneg * ncnegfactor > numcache)
1737 cache_negative_zap_one();
1741 cache_enter_unlock(&cel);
1747 cache_roundup_2(u_int val)
1751 for (res = 1; res <= val; res <<= 1)
1758 * Name cache initialization, from vfs_init() when we are booting
1761 nchinit(void *dummy __unused)
1765 cache_zone_small = uma_zcreate("S VFS Cache",
1766 sizeof(struct namecache) + CACHE_PATH_CUTOFF + 1,
1767 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
1768 cache_zone_small_ts = uma_zcreate("STS VFS Cache",
1769 sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1,
1770 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
1771 cache_zone_large = uma_zcreate("L VFS Cache",
1772 sizeof(struct namecache) + NAME_MAX + 1,
1773 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
1774 cache_zone_large_ts = uma_zcreate("LTS VFS Cache",
1775 sizeof(struct namecache_ts) + NAME_MAX + 1,
1776 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
1778 nchashtbl = hashinit(desiredvnodes * 2, M_VFSCACHE, &nchash);
1779 ncbuckethash = cache_roundup_2(mp_ncpus * 64) - 1;
1780 if (ncbuckethash > nchash)
1781 ncbuckethash = nchash;
1782 bucketlocks = malloc(sizeof(*bucketlocks) * numbucketlocks, M_VFSCACHE,
1784 for (i = 0; i < numbucketlocks; i++)
1785 rw_init_flags(&bucketlocks[i], "ncbuc", RW_DUPOK | RW_RECURSE);
1786 ncvnodehash = cache_roundup_2(mp_ncpus * 64) - 1;
1787 vnodelocks = malloc(sizeof(*vnodelocks) * numvnodelocks, M_VFSCACHE,
1789 for (i = 0; i < numvnodelocks; i++)
1790 mtx_init(&vnodelocks[i], "ncvn", NULL, MTX_DUPOK | MTX_RECURSE);
1791 ncpurgeminvnodes = numbucketlocks;
1794 neglists = malloc(sizeof(*neglists) * numneglists, M_VFSCACHE,
1796 for (i = 0; i < numneglists; i++) {
1797 mtx_init(&neglists[i].nl_lock, "ncnegl", NULL, MTX_DEF);
1798 TAILQ_INIT(&neglists[i].nl_list);
1800 mtx_init(&ncneg_hot.nl_lock, "ncneglh", NULL, MTX_DEF);
1801 TAILQ_INIT(&ncneg_hot.nl_list);
1803 mtx_init(&ncneg_shrink_lock, "ncnegs", NULL, MTX_DEF);
1805 numcalls = counter_u64_alloc(M_WAITOK);
1806 dothits = counter_u64_alloc(M_WAITOK);
1807 dotdothits = counter_u64_alloc(M_WAITOK);
1808 numchecks = counter_u64_alloc(M_WAITOK);
1809 nummiss = counter_u64_alloc(M_WAITOK);
1810 nummisszap = counter_u64_alloc(M_WAITOK);
1811 numposzaps = counter_u64_alloc(M_WAITOK);
1812 numposhits = counter_u64_alloc(M_WAITOK);
1813 numnegzaps = counter_u64_alloc(M_WAITOK);
1814 numneghits = counter_u64_alloc(M_WAITOK);
1815 numfullpathcalls = counter_u64_alloc(M_WAITOK);
1816 numfullpathfail1 = counter_u64_alloc(M_WAITOK);
1817 numfullpathfail2 = counter_u64_alloc(M_WAITOK);
1818 numfullpathfail4 = counter_u64_alloc(M_WAITOK);
1819 numfullpathfound = counter_u64_alloc(M_WAITOK);
1821 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL);
1824 cache_changesize(int newmaxvnodes)
1826 struct nchashhead *new_nchashtbl, *old_nchashtbl;
1827 u_long new_nchash, old_nchash;
1828 struct namecache *ncp;
1832 newmaxvnodes = cache_roundup_2(newmaxvnodes * 2);
1833 if (newmaxvnodes < numbucketlocks)
1834 newmaxvnodes = numbucketlocks;
1836 new_nchashtbl = hashinit(newmaxvnodes, M_VFSCACHE, &new_nchash);
1837 /* If same hash table size, nothing to do */
1838 if (nchash == new_nchash) {
1839 free(new_nchashtbl, M_VFSCACHE);
1843 * Move everything from the old hash table to the new table.
1844 * None of the namecache entries in the table can be removed
1845 * because to do so, they have to be removed from the hash table.
1847 cache_lock_all_vnodes();
1848 cache_lock_all_buckets();
1849 old_nchashtbl = nchashtbl;
1850 old_nchash = nchash;
1851 nchashtbl = new_nchashtbl;
1852 nchash = new_nchash;
1853 for (i = 0; i <= old_nchash; i++) {
1854 while ((ncp = LIST_FIRST(&old_nchashtbl[i])) != NULL) {
1855 hash = cache_get_hash(nc_get_name(ncp), ncp->nc_nlen,
1857 LIST_REMOVE(ncp, nc_hash);
1858 LIST_INSERT_HEAD(NCHHASH(hash), ncp, nc_hash);
1861 cache_unlock_all_buckets();
1862 cache_unlock_all_vnodes();
1863 free(old_nchashtbl, M_VFSCACHE);
1867 * Invalidate all entries to a particular vnode.
1870 cache_purge(struct vnode *vp)
1872 TAILQ_HEAD(, namecache) ncps;
1873 struct namecache *ncp, *nnp;
1874 struct mtx *vlp, *vlp2;
1876 CTR1(KTR_VFS, "cache_purge(%p)", vp);
1877 SDT_PROBE1(vfs, namecache, purge, done, vp);
1878 if (LIST_EMPTY(&vp->v_cache_src) && TAILQ_EMPTY(&vp->v_cache_dst) &&
1879 vp->v_cache_dd == NULL)
1882 vlp = VP2VNODELOCK(vp);
1886 while (!LIST_EMPTY(&vp->v_cache_src)) {
1887 ncp = LIST_FIRST(&vp->v_cache_src);
1888 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
1890 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
1892 while (!TAILQ_EMPTY(&vp->v_cache_dst)) {
1893 ncp = TAILQ_FIRST(&vp->v_cache_dst);
1894 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
1896 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
1898 ncp = vp->v_cache_dd;
1900 KASSERT(ncp->nc_flag & NCF_ISDOTDOT,
1901 ("lost dotdot link"));
1902 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2))
1904 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
1906 KASSERT(vp->v_cache_dd == NULL, ("incomplete purge"));
1910 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
1916 * Invalidate all negative entries for a particular directory vnode.
1919 cache_purge_negative(struct vnode *vp)
1921 TAILQ_HEAD(, namecache) ncps;
1922 struct namecache *ncp, *nnp;
1925 CTR1(KTR_VFS, "cache_purge_negative(%p)", vp);
1926 SDT_PROBE1(vfs, namecache, purge_negative, done, vp);
1928 vlp = VP2VNODELOCK(vp);
1930 LIST_FOREACH_SAFE(ncp, &vp->v_cache_src, nc_src, nnp) {
1931 if (!(ncp->nc_flag & NCF_NEGATIVE))
1933 cache_zap_negative_locked_vnode_kl(ncp, vp);
1934 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
1937 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
1943 * Flush all entries referencing a particular filesystem.
1946 cache_purgevfs(struct mount *mp, bool force)
1948 TAILQ_HEAD(, namecache) ncps;
1949 struct mtx *vlp1, *vlp2;
1951 struct nchashhead *bucket;
1952 struct namecache *ncp, *nnp;
1953 u_long i, j, n_nchash;
1956 /* Scan hash tables for applicable entries */
1957 SDT_PROBE1(vfs, namecache, purgevfs, done, mp);
1958 if (!force && mp->mnt_nvnodelistsize <= ncpurgeminvnodes)
1961 n_nchash = nchash + 1;
1963 for (i = 0; i < numbucketlocks; i++) {
1964 blp = (struct rwlock *)&bucketlocks[i];
1966 for (j = i; j < n_nchash; j += numbucketlocks) {
1968 bucket = &nchashtbl[j];
1969 LIST_FOREACH_SAFE(ncp, bucket, nc_hash, nnp) {
1970 cache_assert_bucket_locked(ncp, RA_WLOCKED);
1971 if (ncp->nc_dvp->v_mount != mp)
1973 error = cache_zap_wlocked_bucket_kl(ncp, blp,
1977 TAILQ_INSERT_HEAD(&ncps, ncp, nc_dst);
1981 if (vlp1 == NULL && vlp2 == NULL)
1982 cache_maybe_yield();
1989 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
1995 * Perform canonical checks and cache lookup and pass on to filesystem
1996 * through the vop_cachedlookup only if needed.
2000 vfs_cache_lookup(struct vop_lookup_args *ap)
2004 struct vnode **vpp = ap->a_vpp;
2005 struct componentname *cnp = ap->a_cnp;
2006 struct ucred *cred = cnp->cn_cred;
2007 int flags = cnp->cn_flags;
2008 struct thread *td = cnp->cn_thread;
2013 if (dvp->v_type != VDIR)
2016 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
2017 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
2020 error = VOP_ACCESS(dvp, VEXEC, cred, td);
2024 error = cache_lookup(dvp, vpp, cnp, NULL, NULL);
2026 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
2033 * XXX All of these sysctls would probably be more productive dead.
2035 static int disablecwd;
2036 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0,
2037 "Disable the getcwd syscall");
2039 /* Implementation of the getcwd syscall. */
2041 sys___getcwd(struct thread *td, struct __getcwd_args *uap)
2044 return (kern___getcwd(td, uap->buf, UIO_USERSPACE, uap->buflen,
2049 kern___getcwd(struct thread *td, char *buf, enum uio_seg bufseg, u_int buflen,
2053 struct filedesc *fdp;
2054 struct vnode *cdir, *rdir;
2057 if (__predict_false(disablecwd))
2059 if (__predict_false(buflen < 2))
2061 if (buflen > path_max)
2064 tmpbuf = malloc(buflen, M_TEMP, M_WAITOK);
2065 fdp = td->td_proc->p_fd;
2066 FILEDESC_SLOCK(fdp);
2067 cdir = fdp->fd_cdir;
2069 rdir = fdp->fd_rdir;
2071 FILEDESC_SUNLOCK(fdp);
2072 error = vn_fullpath1(td, cdir, rdir, tmpbuf, &bp, buflen);
2077 if (bufseg == UIO_SYSSPACE)
2078 bcopy(bp, buf, strlen(bp) + 1);
2080 error = copyout(bp, buf, strlen(bp) + 1);
2082 if (KTRPOINT(curthread, KTR_NAMEI))
2086 free(tmpbuf, M_TEMP);
2091 * Thus begins the fullpath magic.
2094 static int disablefullpath;
2095 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW, &disablefullpath, 0,
2096 "Disable the vn_fullpath function");
2099 * Retrieve the full filesystem path that correspond to a vnode from the name
2100 * cache (if available)
2103 vn_fullpath(struct thread *td, struct vnode *vn, char **retbuf, char **freebuf)
2106 struct filedesc *fdp;
2110 if (__predict_false(disablefullpath))
2112 if (__predict_false(vn == NULL))
2115 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
2116 fdp = td->td_proc->p_fd;
2117 FILEDESC_SLOCK(fdp);
2118 rdir = fdp->fd_rdir;
2120 FILEDESC_SUNLOCK(fdp);
2121 error = vn_fullpath1(td, vn, rdir, buf, retbuf, MAXPATHLEN);
2132 * This function is similar to vn_fullpath, but it attempts to lookup the
2133 * pathname relative to the global root mount point. This is required for the
2134 * auditing sub-system, as audited pathnames must be absolute, relative to the
2135 * global root mount point.
2138 vn_fullpath_global(struct thread *td, struct vnode *vn,
2139 char **retbuf, char **freebuf)
2144 if (__predict_false(disablefullpath))
2146 if (__predict_false(vn == NULL))
2148 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
2149 error = vn_fullpath1(td, vn, rootvnode, buf, retbuf, MAXPATHLEN);
2158 vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, u_int *buflen)
2161 struct namecache *ncp;
2165 vlp = VP2VNODELOCK(*vp);
2167 TAILQ_FOREACH(ncp, &((*vp)->v_cache_dst), nc_dst) {
2168 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2172 if (*buflen < ncp->nc_nlen) {
2175 counter_u64_add(numfullpathfail4, 1);
2177 SDT_PROBE3(vfs, namecache, fullpath, return, error,
2181 *buflen -= ncp->nc_nlen;
2182 memcpy(buf + *buflen, nc_get_name(ncp), ncp->nc_nlen);
2183 SDT_PROBE3(vfs, namecache, fullpath, hit, ncp->nc_dvp,
2184 nc_get_name(ncp), vp);
2192 SDT_PROBE1(vfs, namecache, fullpath, miss, vp);
2195 vn_lock(*vp, LK_SHARED | LK_RETRY);
2196 error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen);
2199 counter_u64_add(numfullpathfail2, 1);
2200 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2205 if (dvp->v_iflag & VI_DOOMED) {
2206 /* forced unmount */
2209 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
2213 * *vp has its use count incremented still.
2220 * The magic behind kern___getcwd() and vn_fullpath().
2223 vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
2224 char *buf, char **retbuf, u_int buflen)
2226 int error, slash_prefixed;
2227 #ifdef KDTRACE_HOOKS
2228 struct vnode *startvp = vp;
2237 SDT_PROBE1(vfs, namecache, fullpath, entry, vp);
2238 counter_u64_add(numfullpathcalls, 1);
2240 if (vp->v_type != VDIR) {
2241 error = vn_vptocnp(&vp, td->td_ucred, buf, &buflen);
2248 buf[--buflen] = '/';
2251 while (vp != rdir && vp != rootvnode) {
2253 * The vp vnode must be already fully constructed,
2254 * since it is either found in namecache or obtained
2255 * from VOP_VPTOCNP(). We may test for VV_ROOT safely
2256 * without obtaining the vnode lock.
2258 if ((vp->v_vflag & VV_ROOT) != 0) {
2259 vn_lock(vp, LK_RETRY | LK_SHARED);
2262 * With the vnode locked, check for races with
2263 * unmount, forced or not. Note that we
2264 * already verified that vp is not equal to
2265 * the root vnode, which means that
2266 * mnt_vnodecovered can be NULL only for the
2269 if ((vp->v_iflag & VI_DOOMED) != 0 ||
2270 (vp1 = vp->v_mount->mnt_vnodecovered) == NULL ||
2271 vp1->v_mountedhere != vp->v_mount) {
2274 SDT_PROBE3(vfs, namecache, fullpath, return,
2284 if (vp->v_type != VDIR) {
2286 counter_u64_add(numfullpathfail1, 1);
2288 SDT_PROBE3(vfs, namecache, fullpath, return,
2292 error = vn_vptocnp(&vp, td->td_ucred, buf, &buflen);
2298 SDT_PROBE3(vfs, namecache, fullpath, return, error,
2302 buf[--buflen] = '/';
2307 if (!slash_prefixed) {
2310 counter_u64_add(numfullpathfail4, 1);
2311 SDT_PROBE3(vfs, namecache, fullpath, return, ENOMEM,
2315 buf[--buflen] = '/';
2317 counter_u64_add(numfullpathfound, 1);
2320 SDT_PROBE3(vfs, namecache, fullpath, return, 0, startvp, buf + buflen);
2321 *retbuf = buf + buflen;
2326 vn_dir_dd_ino(struct vnode *vp)
2328 struct namecache *ncp;
2332 ASSERT_VOP_LOCKED(vp, "vn_dir_dd_ino");
2333 vlp = VP2VNODELOCK(vp);
2335 TAILQ_FOREACH(ncp, &(vp->v_cache_dst), nc_dst) {
2336 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0)
2341 if (vget(ddvp, LK_SHARED | LK_NOWAIT | LK_VNHELD, curthread))
2350 vn_commname(struct vnode *vp, char *buf, u_int buflen)
2352 struct namecache *ncp;
2356 vlp = VP2VNODELOCK(vp);
2358 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst)
2359 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
2365 l = min(ncp->nc_nlen, buflen - 1);
2366 memcpy(buf, nc_get_name(ncp), l);
2372 /* ABI compat shims for old kernel modules. */
2375 void cache_enter(struct vnode *dvp, struct vnode *vp,
2376 struct componentname *cnp);
2379 cache_enter(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2382 cache_enter_time(dvp, vp, cnp, NULL, NULL);
2386 * This function updates path string to vnode's full global path
2387 * and checks the size of the new path string against the pathlen argument.
2389 * Requires a locked, referenced vnode.
2390 * Vnode is re-locked on success or ENODEV, otherwise unlocked.
2392 * If sysctl debug.disablefullpath is set, ENODEV is returned,
2393 * vnode is left locked and path remain untouched.
2395 * If vp is a directory, the call to vn_fullpath_global() always succeeds
2396 * because it falls back to the ".." lookup if the namecache lookup fails.
2399 vn_path_to_global_path(struct thread *td, struct vnode *vp, char *path,
2402 struct nameidata nd;
2407 ASSERT_VOP_ELOCKED(vp, __func__);
2409 /* Return ENODEV if sysctl debug.disablefullpath==1 */
2410 if (__predict_false(disablefullpath))
2413 /* Construct global filesystem path from vp. */
2415 error = vn_fullpath_global(td, vp, &rpath, &fbuf);
2422 if (strlen(rpath) >= pathlen) {
2424 error = ENAMETOOLONG;
2429 * Re-lookup the vnode by path to detect a possible rename.
2430 * As a side effect, the vnode is relocked.
2431 * If vnode was renamed, return ENOENT.
2433 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
2434 UIO_SYSSPACE, path, td);
2440 NDFREE(&nd, NDF_ONLY_PNBUF);
2444 strcpy(path, rpath);