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 * 4. 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_DEFINE2(vfs, namecache, zap_negative, done, "struct vnode *",
91 * This structure describes the elements in the cache of recent
92 * names looked up by namei.
96 LIST_ENTRY(namecache) nc_hash; /* hash chain */
97 LIST_ENTRY(namecache) nc_src; /* source vnode list */
98 TAILQ_ENTRY(namecache) nc_dst; /* destination vnode list */
99 struct vnode *nc_dvp; /* vnode of parent of name */
100 struct vnode *nc_vp; /* vnode the name refers to */
101 u_char nc_flag; /* flag bits */
102 u_char nc_nlen; /* length of name */
103 char nc_name[0]; /* segment name + nul */
107 * struct namecache_ts repeats struct namecache layout up to the
109 * struct namecache_ts is used in place of struct namecache when time(s) need
110 * to be stored. The nc_dotdottime field is used when a cache entry is mapping
111 * both a non-dotdot directory name plus dotdot for the directory's
114 struct namecache_ts {
115 LIST_ENTRY(namecache) nc_hash; /* hash chain */
116 LIST_ENTRY(namecache) nc_src; /* source vnode list */
117 TAILQ_ENTRY(namecache) nc_dst; /* destination vnode list */
118 struct vnode *nc_dvp; /* vnode of parent of name */
119 struct vnode *nc_vp; /* vnode the name refers to */
120 u_char nc_flag; /* flag bits */
121 u_char nc_nlen; /* length of name */
122 struct timespec nc_time; /* timespec provided by fs */
123 struct timespec nc_dotdottime; /* dotdot timespec provided by fs */
124 int nc_ticks; /* ticks value when entry was added */
125 char nc_name[0]; /* segment name + nul */
129 * Flags in namecache.nc_flag
131 #define NCF_WHITE 0x01
132 #define NCF_ISDOTDOT 0x02
135 #define NCF_DVDROP 0x10
138 * Name caching works as follows:
140 * Names found by directory scans are retained in a cache
141 * for future reference. It is managed LRU, so frequently
142 * used names will hang around. Cache is indexed by hash value
143 * obtained from (vp, name) where vp refers to the directory
146 * If it is a "negative" entry, (i.e. for a name that is known NOT to
147 * exist) the vnode pointer will be NULL.
149 * Upon reaching the last segment of a path, if the reference
150 * is for DELETE, or NOCACHE is set (rewrite), and the
151 * name is located in the cache, it will be dropped.
153 * These locks are used (in the order in which they can be taken):
155 * cache_lock rwlock global, needed for all modifications
156 * bucketlock rwlock for access to given hash bucket
157 * ncneg_mtx mtx negative entry LRU management
159 * A name -> vnode lookup can be safely performed by either locking cache_lock
160 * or the relevant hash bucket.
162 * ".." and vnode -> name lookups require cache_lock.
164 * Modifications require both cache_lock and relevant bucketlock taken for
167 * Negative entry LRU management requires ncneg_mtx taken on top of either
168 * cache_lock or bucketlock.
172 * Structures associated with name caching.
174 #define NCHHASH(hash) \
175 (&nchashtbl[(hash) & nchash])
176 static LIST_HEAD(nchashhead, namecache) *nchashtbl; /* Hash Table */
177 static TAILQ_HEAD(, namecache) ncneg; /* Hash Table */
178 static u_long nchash; /* size of hash table */
179 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0,
180 "Size of namecache hash table");
181 static u_long ncnegfactor = 16; /* ratio of negative entries */
182 SYSCTL_ULONG(_vfs, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0,
183 "Ratio of negative namecache entries");
184 static u_long numneg; /* number of negative entries allocated */
185 SYSCTL_ULONG(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0,
186 "Number of negative entries in namecache");
187 static u_long numcache; /* number of cache entries allocated */
188 SYSCTL_ULONG(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0,
189 "Number of namecache entries");
190 static u_long numcachehv; /* number of cache entries with vnodes held */
191 SYSCTL_ULONG(_debug, OID_AUTO, numcachehv, CTLFLAG_RD, &numcachehv, 0,
192 "Number of namecache entries with vnodes held");
193 u_int ncsizefactor = 2;
194 SYSCTL_UINT(_vfs, OID_AUTO, ncsizefactor, CTLFLAG_RW, &ncsizefactor, 0,
195 "Size factor for namecache");
197 struct nchstats nchstats; /* cache effectiveness statistics */
199 static struct rwlock cache_lock;
200 RW_SYSINIT(vfscache, &cache_lock, "ncglobal");
202 #define CACHE_TRY_WLOCK() rw_try_wlock(&cache_lock)
203 #define CACHE_UPGRADE_LOCK() rw_try_upgrade(&cache_lock)
204 #define CACHE_RLOCK() rw_rlock(&cache_lock)
205 #define CACHE_RUNLOCK() rw_runlock(&cache_lock)
206 #define CACHE_WLOCK() rw_wlock(&cache_lock)
207 #define CACHE_WUNLOCK() rw_wunlock(&cache_lock)
209 static struct mtx_padalign ncneg_mtx;
210 MTX_SYSINIT(vfscache_neg, &ncneg_mtx, "ncneg", MTX_DEF);
212 static u_int numbucketlocks;
213 static struct rwlock_padalign *bucketlocks;
214 #define HASH2BUCKETLOCK(hash) \
215 ((struct rwlock *)(&bucketlocks[((hash) % numbucketlocks)]))
218 * UMA zones for the VFS cache.
220 * The small cache is used for entries with short names, which are the
221 * most common. The large cache is used for entries which are too big to
222 * fit in the small cache.
224 static uma_zone_t cache_zone_small;
225 static uma_zone_t cache_zone_small_ts;
226 static uma_zone_t cache_zone_large;
227 static uma_zone_t cache_zone_large_ts;
229 #define CACHE_PATH_CUTOFF 35
231 static struct namecache *
232 cache_alloc(int len, int ts)
235 if (len > CACHE_PATH_CUTOFF) {
237 return (uma_zalloc(cache_zone_large_ts, M_WAITOK));
239 return (uma_zalloc(cache_zone_large, M_WAITOK));
242 return (uma_zalloc(cache_zone_small_ts, M_WAITOK));
244 return (uma_zalloc(cache_zone_small, M_WAITOK));
248 cache_free(struct namecache *ncp)
254 ts = ncp->nc_flag & NCF_TS;
255 if ((ncp->nc_flag & NCF_DVDROP) != 0)
257 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF) {
259 uma_zfree(cache_zone_small_ts, ncp);
261 uma_zfree(cache_zone_small, ncp);
263 uma_zfree(cache_zone_large_ts, ncp);
265 uma_zfree(cache_zone_large, ncp);
269 nc_get_name(struct namecache *ncp)
271 struct namecache_ts *ncp_ts;
273 if ((ncp->nc_flag & NCF_TS) == 0)
274 return (ncp->nc_name);
275 ncp_ts = (struct namecache_ts *)ncp;
276 return (ncp_ts->nc_name);
280 cache_out_ts(struct namecache *ncp, struct timespec *tsp, int *ticksp)
283 KASSERT((ncp->nc_flag & NCF_TS) != 0 ||
284 (tsp == NULL && ticksp == NULL),
288 *tsp = ((struct namecache_ts *)ncp)->nc_time;
290 *ticksp = ((struct namecache_ts *)ncp)->nc_ticks;
293 static int doingcache = 1; /* 1 => enable the cache */
294 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0,
295 "VFS namecache enabled");
297 /* Export size information to userland */
298 SYSCTL_INT(_debug_sizeof, OID_AUTO, namecache, CTLFLAG_RD, SYSCTL_NULL_INT_PTR,
299 sizeof(struct namecache), "sizeof(struct namecache)");
302 * The new name cache statistics
304 static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0,
305 "Name cache statistics");
306 #define STATNODE_ULONG(name, descr) \
307 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, descr);
308 #define STATNODE_COUNTER(name, descr) \
309 static counter_u64_t name; \
310 SYSCTL_COUNTER_U64(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, descr);
311 STATNODE_ULONG(numneg, "Number of negative cache entries");
312 STATNODE_ULONG(numcache, "Number of cache entries");
313 STATNODE_COUNTER(numcalls, "Number of cache lookups");
314 STATNODE_COUNTER(dothits, "Number of '.' hits");
315 STATNODE_COUNTER(dotdothits, "Number of '..' hits");
316 STATNODE_COUNTER(numchecks, "Number of checks in lookup");
317 STATNODE_COUNTER(nummiss, "Number of cache misses");
318 STATNODE_COUNTER(nummisszap, "Number of cache misses we do not want to cache");
319 STATNODE_COUNTER(numposzaps,
320 "Number of cache hits (positive) we do not want to cache");
321 STATNODE_COUNTER(numposhits, "Number of cache hits (positive)");
322 STATNODE_COUNTER(numnegzaps,
323 "Number of cache hits (negative) we do not want to cache");
324 STATNODE_COUNTER(numneghits, "Number of cache hits (negative)");
325 /* These count for kern___getcwd(), too. */
326 STATNODE_COUNTER(numfullpathcalls, "Number of fullpath search calls");
327 STATNODE_COUNTER(numfullpathfail1, "Number of fullpath search errors (ENOTDIR)");
328 STATNODE_COUNTER(numfullpathfail2,
329 "Number of fullpath search errors (VOP_VPTOCNP failures)");
330 STATNODE_COUNTER(numfullpathfail4, "Number of fullpath search errors (ENOMEM)");
331 STATNODE_COUNTER(numfullpathfound, "Number of successful fullpath calls");
332 static long numupgrades; STATNODE_ULONG(numupgrades,
333 "Number of updates of the cache after lookup (write lock + retry)");
334 static long zap_and_exit_bucket_fail; STATNODE_ULONG(zap_and_exit_bucket_fail,
335 "Number of times bucketlocked zap_and_exit case failed to writelock");
337 static void cache_zap(struct namecache *ncp);
338 static int vn_vptocnp_locked(struct vnode **vp, struct ucred *cred, char *buf,
340 static int vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
341 char *buf, char **retbuf, u_int buflen);
343 static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
346 cache_get_hash(char *name, u_char len, struct vnode *dvp)
350 hash = fnv_32_buf(name, len, FNV1_32_INIT);
351 hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
357 cache_assert_bucket_locked(struct namecache *ncp, int mode)
359 struct rwlock *bucketlock;
362 hash = cache_get_hash(nc_get_name(ncp), ncp->nc_nlen, ncp->nc_dvp);
363 bucketlock = HASH2BUCKETLOCK(hash);
364 rw_assert(bucketlock, mode);
367 #define cache_assert_bucket_locked(x, y) do { } while (0)
371 cache_lock_all_buckets(void)
375 for (i = 0; i < numbucketlocks; i++)
376 rw_wlock(&bucketlocks[i]);
380 cache_unlock_all_buckets(void)
384 for (i = 0; i < numbucketlocks; i++)
385 rw_wunlock(&bucketlocks[i]);
389 sysctl_nchstats(SYSCTL_HANDLER_ARGS)
391 struct nchstats snap;
393 if (req->oldptr == NULL)
394 return (SYSCTL_OUT(req, 0, sizeof(snap)));
397 snap.ncs_goodhits = counter_u64_fetch(numposhits);
398 snap.ncs_neghits = counter_u64_fetch(numneghits);
399 snap.ncs_badhits = counter_u64_fetch(numposzaps) +
400 counter_u64_fetch(numnegzaps);
401 snap.ncs_miss = counter_u64_fetch(nummisszap) +
402 counter_u64_fetch(nummiss);
404 return (SYSCTL_OUT(req, &snap, sizeof(snap)));
406 SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE | CTLFLAG_RD |
407 CTLFLAG_MPSAFE, 0, 0, sysctl_nchstats, "LU",
408 "VFS cache effectiveness statistics");
412 * Grab an atomic snapshot of the name cache hash chain lengths
414 static SYSCTL_NODE(_debug, OID_AUTO, hashstat, CTLFLAG_RW, NULL,
418 sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS)
420 struct nchashhead *ncpp;
421 struct namecache *ncp;
422 int i, error, n_nchash, *cntbuf;
425 n_nchash = nchash + 1; /* nchash is max index, not count */
426 if (req->oldptr == NULL)
427 return SYSCTL_OUT(req, 0, n_nchash * sizeof(int));
428 cntbuf = malloc(n_nchash * sizeof(int), M_TEMP, M_ZERO | M_WAITOK);
430 if (n_nchash != nchash + 1) {
432 free(cntbuf, M_TEMP);
435 /* Scan hash tables counting entries */
436 for (ncpp = nchashtbl, i = 0; i < n_nchash; ncpp++, i++)
437 LIST_FOREACH(ncp, ncpp, nc_hash)
440 for (error = 0, i = 0; i < n_nchash; i++)
441 if ((error = SYSCTL_OUT(req, &cntbuf[i], sizeof(int))) != 0)
443 free(cntbuf, M_TEMP);
446 SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD|
447 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_rawnchash, "S,int",
448 "nchash chain lengths");
451 sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS)
454 struct nchashhead *ncpp;
455 struct namecache *ncp;
457 int count, maxlength, used, pct;
460 return SYSCTL_OUT(req, 0, 4 * sizeof(int));
463 n_nchash = nchash + 1; /* nchash is max index, not count */
467 /* Scan hash tables for applicable entries */
468 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
470 LIST_FOREACH(ncp, ncpp, nc_hash) {
475 if (maxlength < count)
478 n_nchash = nchash + 1;
480 pct = (used * 100) / (n_nchash / 100);
481 error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash));
484 error = SYSCTL_OUT(req, &used, sizeof(used));
487 error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength));
490 error = SYSCTL_OUT(req, &pct, sizeof(pct));
495 SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD|
496 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_nchash, "I",
497 "nchash statistics (number of total/used buckets, maximum chain length, usage percentage)");
501 * Negative entries management
504 cache_negative_hit(struct namecache *ncp)
507 mtx_lock(&ncneg_mtx);
508 TAILQ_REMOVE(&ncneg, ncp, nc_dst);
509 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
510 mtx_unlock(&ncneg_mtx);
514 cache_negative_insert(struct namecache *ncp)
517 rw_assert(&cache_lock, RA_WLOCKED);
518 cache_assert_bucket_locked(ncp, RA_WLOCKED);
519 MPASS(ncp->nc_vp == NULL);
520 mtx_lock(&ncneg_mtx);
521 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
523 mtx_unlock(&ncneg_mtx);
527 cache_negative_remove(struct namecache *ncp)
530 rw_assert(&cache_lock, RA_WLOCKED);
531 cache_assert_bucket_locked(ncp, RA_WLOCKED);
532 MPASS(ncp->nc_vp == NULL);
533 mtx_lock(&ncneg_mtx);
534 TAILQ_REMOVE(&ncneg, ncp, nc_dst);
536 mtx_unlock(&ncneg_mtx);
539 static struct namecache *
540 cache_negative_zap_one(void)
542 struct namecache *ncp;
544 rw_assert(&cache_lock, RA_WLOCKED);
545 ncp = TAILQ_FIRST(&ncneg);
546 KASSERT(ncp->nc_vp == NULL, ("ncp %p vp %p on ncneg",
555 * Removes a namecache entry from cache, whether it contains an actual
556 * pointer to a vnode or if it is just a negative cache entry.
559 cache_zap_locked(struct namecache *ncp)
562 rw_assert(&cache_lock, RA_WLOCKED);
563 cache_assert_bucket_locked(ncp, RA_WLOCKED);
564 CTR2(KTR_VFS, "cache_zap(%p) vp %p", ncp, ncp->nc_vp);
565 if (ncp->nc_vp != NULL) {
566 SDT_PROBE3(vfs, namecache, zap, done, ncp->nc_dvp,
567 nc_get_name(ncp), ncp->nc_vp);
569 SDT_PROBE2(vfs, namecache, zap_negative, done, ncp->nc_dvp,
572 LIST_REMOVE(ncp, nc_hash);
573 if (ncp->nc_flag & NCF_ISDOTDOT) {
574 if (ncp == ncp->nc_dvp->v_cache_dd)
575 ncp->nc_dvp->v_cache_dd = NULL;
577 LIST_REMOVE(ncp, nc_src);
578 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) {
579 ncp->nc_flag |= NCF_DVDROP;
584 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
585 if (ncp == ncp->nc_vp->v_cache_dd)
586 ncp->nc_vp->v_cache_dd = NULL;
588 cache_negative_remove(ncp);
594 cache_zap(struct namecache *ncp)
596 struct rwlock *bucketlock;
599 rw_assert(&cache_lock, RA_WLOCKED);
601 hash = cache_get_hash(nc_get_name(ncp), ncp->nc_nlen, ncp->nc_dvp);
602 bucketlock = HASH2BUCKETLOCK(hash);
603 rw_wlock(bucketlock);
604 cache_zap_locked(ncp);
605 rw_wunlock(bucketlock);
609 * Lookup an entry in the cache
611 * Lookup is called with dvp pointing to the directory to search,
612 * cnp pointing to the name of the entry being sought. If the lookup
613 * succeeds, the vnode is returned in *vpp, and a status of -1 is
614 * returned. If the lookup determines that the name does not exist
615 * (negative caching), a status of ENOENT is returned. If the lookup
616 * fails, a status of zero is returned. If the directory vnode is
617 * recycled out from under us due to a forced unmount, a status of
618 * ENOENT is returned.
620 * vpp is locked and ref'd on return. If we're looking up DOTDOT, dvp is
621 * unlocked. If we're looking up . an extra ref is taken, but the lock is
622 * not recursively acquired.
625 enum { UNLOCKED, WLOCKED, RLOCKED };
628 cache_unlock(int cache_locked)
631 switch (cache_locked) {
644 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
645 struct timespec *tsp, int *ticksp)
647 struct rwlock *bucketlock;
648 struct namecache *ncp;
650 int error, ltype, cache_locked;
653 cnp->cn_flags &= ~MAKEENTRY;
658 cache_locked = UNLOCKED;
660 counter_u64_add(numcalls, 1);
663 if (cnp->cn_nameptr[0] == '.') {
664 if (cnp->cn_namelen == 1) {
666 CTR2(KTR_VFS, "cache_lookup(%p, %s) found via .",
667 dvp, cnp->cn_nameptr);
668 counter_u64_add(dothits, 1);
669 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", *vpp);
676 * When we lookup "." we still can be asked to lock it
679 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
680 if (ltype != VOP_ISLOCKED(*vpp)) {
681 if (ltype == LK_EXCLUSIVE) {
682 vn_lock(*vpp, LK_UPGRADE | LK_RETRY);
683 if ((*vpp)->v_iflag & VI_DOOMED) {
690 vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY);
694 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
695 counter_u64_add(dotdothits, 1);
696 if (cache_locked == UNLOCKED) {
698 cache_locked = RLOCKED;
701 if (dvp->v_cache_dd == NULL) {
702 SDT_PROBE3(vfs, namecache, lookup, miss, dvp,
706 if ((cnp->cn_flags & MAKEENTRY) == 0) {
707 if (cache_locked != WLOCKED &&
708 !CACHE_UPGRADE_LOCK())
711 if (dvp->v_cache_dd->nc_flag & NCF_ISDOTDOT) {
712 ncp = dvp->v_cache_dd;
715 dvp->v_cache_dd = NULL;
720 ncp = dvp->v_cache_dd;
721 if (ncp->nc_flag & NCF_ISDOTDOT)
725 /* Return failure if negative entry was found. */
727 goto negative_success;
728 CTR3(KTR_VFS, "cache_lookup(%p, %s) found %p via ..",
729 dvp, cnp->cn_nameptr, *vpp);
730 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, "..",
732 cache_out_ts(ncp, tsp, ticksp);
733 if ((ncp->nc_flag & (NCF_ISDOTDOT | NCF_DTS)) ==
734 NCF_DTS && tsp != NULL)
735 *tsp = ((struct namecache_ts *)ncp)->
741 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
742 if (cache_locked == UNLOCKED) {
743 bucketlock = HASH2BUCKETLOCK(hash);
744 rw_rlock(bucketlock);
747 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
748 counter_u64_add(numchecks, 1);
749 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
750 !bcmp(nc_get_name(ncp), cnp->cn_nameptr, ncp->nc_nlen))
754 /* We failed to find an entry */
756 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
758 if ((cnp->cn_flags & MAKEENTRY) == 0) {
759 counter_u64_add(nummisszap, 1);
761 counter_u64_add(nummiss, 1);
766 /* We don't want to have an entry, so dump it */
767 if ((cnp->cn_flags & MAKEENTRY) == 0) {
768 counter_u64_add(numposzaps, 1);
772 /* We found a "positive" match, return the vnode */
774 counter_u64_add(numposhits, 1);
776 CTR4(KTR_VFS, "cache_lookup(%p, %s) found %p via ncp %p",
777 dvp, cnp->cn_nameptr, *vpp, ncp);
778 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, nc_get_name(ncp),
780 cache_out_ts(ncp, tsp, ticksp);
785 /* We found a negative match, and want to create it, so purge */
786 if (cnp->cn_nameiop == CREATE) {
787 counter_u64_add(numnegzaps, 1);
791 counter_u64_add(numneghits, 1);
792 cache_negative_hit(ncp);
793 if (ncp->nc_flag & NCF_WHITE)
794 cnp->cn_flags |= ISWHITEOUT;
795 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp,
797 cache_out_ts(ncp, tsp, ticksp);
798 MPASS(bucketlock != NULL || cache_locked != UNLOCKED);
799 if (bucketlock != NULL)
800 rw_runlock(bucketlock);
801 cache_unlock(cache_locked);
806 * We need to update the cache after our lookup, so upgrade to
807 * a write lock and retry the operation.
813 cache_locked = WLOCKED;
818 * On success we return a locked and ref'd vnode as per the lookup
822 ltype = 0; /* silence gcc warning */
823 if (cnp->cn_flags & ISDOTDOT) {
824 ltype = VOP_ISLOCKED(dvp);
828 MPASS(bucketlock != NULL || cache_locked != UNLOCKED);
829 if (bucketlock != NULL)
830 rw_runlock(bucketlock);
831 cache_unlock(cache_locked);
832 error = vget(*vpp, cnp->cn_lkflags | LK_VNHELD, cnp->cn_thread);
833 if (cnp->cn_flags & ISDOTDOT) {
834 vn_lock(dvp, ltype | LK_RETRY);
835 if (dvp->v_iflag & VI_DOOMED) {
846 if ((cnp->cn_flags & ISLASTCN) &&
847 (cnp->cn_lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE) {
848 ASSERT_VOP_ELOCKED(*vpp, "cache_lookup");
853 MPASS(bucketlock != NULL || cache_locked != UNLOCKED);
854 if (bucketlock != NULL)
855 rw_runlock(bucketlock);
856 cache_unlock(cache_locked);
860 if (bucketlock != NULL) {
861 rw_assert(&cache_lock, RA_UNLOCKED);
862 if (!CACHE_TRY_WLOCK()) {
863 rw_runlock(bucketlock);
865 zap_and_exit_bucket_fail++;
868 cache_locked = WLOCKED;
869 rw_runlock(bucketlock);
871 } else if (cache_locked != WLOCKED && !CACHE_UPGRADE_LOCK())
880 * Add an entry to the cache.
883 cache_enter_time(struct vnode *dvp, struct vnode *vp, struct componentname *cnp,
884 struct timespec *tsp, struct timespec *dtsp)
886 struct rwlock *bucketlock;
887 struct namecache *ncp, *n2, *ndd, *nneg;
888 struct namecache_ts *n3;
889 struct nchashhead *ncpp;
894 CTR3(KTR_VFS, "cache_enter(%p, %p, %s)", dvp, vp, cnp->cn_nameptr);
895 VNASSERT(vp == NULL || (vp->v_iflag & VI_DOOMED) == 0, vp,
896 ("cache_enter: Adding a doomed vnode"));
897 VNASSERT(dvp == NULL || (dvp->v_iflag & VI_DOOMED) == 0, dvp,
898 ("cache_enter: Doomed vnode used as src"));
904 * Avoid blowout in namecache entries.
906 if (numcache >= desiredvnodes * ncsizefactor)
911 if (cnp->cn_nameptr[0] == '.') {
912 if (cnp->cn_namelen == 1)
914 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
917 * If dotdot entry already exists, just retarget it
918 * to new parent vnode, otherwise continue with new
919 * namecache entry allocation.
921 if ((ncp = dvp->v_cache_dd) != NULL &&
922 ncp->nc_flag & NCF_ISDOTDOT) {
923 KASSERT(ncp->nc_dvp == dvp,
924 ("wrong isdotdot parent"));
925 if (ncp->nc_vp != NULL) {
926 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst,
929 cache_negative_remove(ncp);
932 TAILQ_INSERT_HEAD(&vp->v_cache_dst,
935 cache_negative_insert(ncp);
941 dvp->v_cache_dd = NULL;
942 SDT_PROBE3(vfs, namecache, enter, done, dvp, "..", vp);
949 * Calculate the hash key and setup as much of the new
950 * namecache entry as possible before acquiring the lock.
952 ncp = cache_alloc(cnp->cn_namelen, tsp != NULL);
957 n3 = (struct namecache_ts *)ncp;
959 n3->nc_ticks = ticks;
960 n3->nc_flag |= NCF_TS;
962 n3->nc_dotdottime = *dtsp;
963 n3->nc_flag |= NCF_DTS;
966 len = ncp->nc_nlen = cnp->cn_namelen;
967 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
968 strlcpy(nc_get_name(ncp), cnp->cn_nameptr, len + 1);
972 * See if this vnode or negative entry is already in the cache
973 * with this name. This can happen with concurrent lookups of
974 * the same path name.
976 ncpp = NCHHASH(hash);
977 LIST_FOREACH(n2, ncpp, nc_hash) {
978 if (n2->nc_dvp == dvp &&
979 n2->nc_nlen == cnp->cn_namelen &&
980 !bcmp(nc_get_name(n2), cnp->cn_nameptr, n2->nc_nlen)) {
982 KASSERT((n2->nc_flag & NCF_TS) != 0,
984 n3 = (struct namecache_ts *)n2;
986 ((struct namecache_ts *)ncp)->nc_time;
988 ((struct namecache_ts *)ncp)->nc_ticks;
991 ((struct namecache_ts *)ncp)->
993 n3->nc_flag |= NCF_DTS;
1002 if (flag == NCF_ISDOTDOT) {
1004 * See if we are trying to add .. entry, but some other lookup
1005 * has populated v_cache_dd pointer already.
1007 if (dvp->v_cache_dd != NULL) {
1012 KASSERT(vp == NULL || vp->v_type == VDIR,
1013 ("wrong vnode type %p", vp));
1014 dvp->v_cache_dd = ncp;
1019 if (vp->v_type == VDIR) {
1020 if (flag != NCF_ISDOTDOT) {
1022 * For this case, the cache entry maps both the
1023 * directory name in it and the name ".." for the
1024 * directory's parent.
1026 if ((ndd = vp->v_cache_dd) != NULL) {
1027 if ((ndd->nc_flag & NCF_ISDOTDOT) != 0)
1032 vp->v_cache_dd = ncp;
1035 vp->v_cache_dd = NULL;
1039 if (flag != NCF_ISDOTDOT) {
1040 if (LIST_EMPTY(&dvp->v_cache_src)) {
1044 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
1047 bucketlock = HASH2BUCKETLOCK(hash);
1048 rw_wlock(bucketlock);
1051 * Insert the new namecache entry into the appropriate chain
1052 * within the cache entries table.
1054 LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
1057 * If the entry is "negative", we place it into the
1058 * "negative" cache queue, otherwise, we place it into the
1059 * destination vnode's cache entries queue.
1062 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
1063 SDT_PROBE3(vfs, namecache, enter, done, dvp, nc_get_name(ncp),
1066 if (cnp->cn_flags & ISWHITEOUT)
1067 ncp->nc_flag |= NCF_WHITE;
1068 cache_negative_insert(ncp);
1069 SDT_PROBE2(vfs, namecache, enter_negative, done, dvp,
1072 rw_wunlock(bucketlock);
1073 if (numneg * ncnegfactor > numcache)
1074 nneg = cache_negative_zap_one();
1081 cache_roundup_2(u_int val)
1085 for (res = 1; res <= val; res <<= 1)
1092 * Name cache initialization, from vfs_init() when we are booting
1095 nchinit(void *dummy __unused)
1101 cache_zone_small = uma_zcreate("S VFS Cache",
1102 sizeof(struct namecache) + CACHE_PATH_CUTOFF + 1,
1103 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
1104 cache_zone_small_ts = uma_zcreate("STS VFS Cache",
1105 sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1,
1106 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
1107 cache_zone_large = uma_zcreate("L VFS Cache",
1108 sizeof(struct namecache) + NAME_MAX + 1,
1109 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
1110 cache_zone_large_ts = uma_zcreate("LTS VFS Cache",
1111 sizeof(struct namecache_ts) + NAME_MAX + 1,
1112 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
1114 nchashtbl = hashinit(desiredvnodes * 2, M_VFSCACHE, &nchash);
1115 numbucketlocks = cache_roundup_2(mp_ncpus * 16);
1116 if (numbucketlocks > nchash)
1117 numbucketlocks = nchash;
1118 bucketlocks = malloc(sizeof(*bucketlocks) * numbucketlocks, M_VFSCACHE,
1120 for (i = 0; i < numbucketlocks; i++)
1121 rw_init_flags(&bucketlocks[i], "ncbuc", RW_DUPOK);
1123 numcalls = counter_u64_alloc(M_WAITOK);
1124 dothits = counter_u64_alloc(M_WAITOK);
1125 dotdothits = counter_u64_alloc(M_WAITOK);
1126 numchecks = counter_u64_alloc(M_WAITOK);
1127 nummiss = counter_u64_alloc(M_WAITOK);
1128 nummisszap = counter_u64_alloc(M_WAITOK);
1129 numposzaps = counter_u64_alloc(M_WAITOK);
1130 numposhits = counter_u64_alloc(M_WAITOK);
1131 numnegzaps = counter_u64_alloc(M_WAITOK);
1132 numneghits = counter_u64_alloc(M_WAITOK);
1133 numfullpathcalls = counter_u64_alloc(M_WAITOK);
1134 numfullpathfail1 = counter_u64_alloc(M_WAITOK);
1135 numfullpathfail2 = counter_u64_alloc(M_WAITOK);
1136 numfullpathfail4 = counter_u64_alloc(M_WAITOK);
1137 numfullpathfound = counter_u64_alloc(M_WAITOK);
1139 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL);
1142 cache_changesize(int newmaxvnodes)
1144 struct nchashhead *new_nchashtbl, *old_nchashtbl;
1145 u_long new_nchash, old_nchash;
1146 struct namecache *ncp;
1150 new_nchashtbl = hashinit(newmaxvnodes * 2, M_VFSCACHE, &new_nchash);
1151 /* If same hash table size, nothing to do */
1152 if (nchash == new_nchash) {
1153 free(new_nchashtbl, M_VFSCACHE);
1157 * Move everything from the old hash table to the new table.
1158 * None of the namecache entries in the table can be removed
1159 * because to do so, they have to be removed from the hash table.
1162 cache_lock_all_buckets();
1163 old_nchashtbl = nchashtbl;
1164 old_nchash = nchash;
1165 nchashtbl = new_nchashtbl;
1166 nchash = new_nchash;
1167 for (i = 0; i <= old_nchash; i++) {
1168 while ((ncp = LIST_FIRST(&old_nchashtbl[i])) != NULL) {
1169 hash = cache_get_hash(nc_get_name(ncp), ncp->nc_nlen,
1171 LIST_REMOVE(ncp, nc_hash);
1172 LIST_INSERT_HEAD(NCHHASH(hash), ncp, nc_hash);
1175 cache_unlock_all_buckets();
1177 free(old_nchashtbl, M_VFSCACHE);
1181 * Invalidate all entries to a particular vnode.
1184 cache_purge(struct vnode *vp)
1186 TAILQ_HEAD(, namecache) ncps;
1187 struct namecache *ncp, *nnp;
1189 CTR1(KTR_VFS, "cache_purge(%p)", vp);
1190 SDT_PROBE1(vfs, namecache, purge, done, vp);
1193 while (!LIST_EMPTY(&vp->v_cache_src)) {
1194 ncp = LIST_FIRST(&vp->v_cache_src);
1196 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
1198 while (!TAILQ_EMPTY(&vp->v_cache_dst)) {
1199 ncp = TAILQ_FIRST(&vp->v_cache_dst);
1201 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
1203 if (vp->v_cache_dd != NULL) {
1204 ncp = vp->v_cache_dd;
1205 KASSERT(ncp->nc_flag & NCF_ISDOTDOT,
1206 ("lost dotdot link"));
1208 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
1210 KASSERT(vp->v_cache_dd == NULL, ("incomplete purge"));
1212 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
1218 * Invalidate all negative entries for a particular directory vnode.
1221 cache_purge_negative(struct vnode *vp)
1223 TAILQ_HEAD(, namecache) ncps;
1224 struct namecache *ncp, *nnp;
1226 CTR1(KTR_VFS, "cache_purge_negative(%p)", vp);
1227 SDT_PROBE1(vfs, namecache, purge_negative, done, vp);
1230 LIST_FOREACH_SAFE(ncp, &vp->v_cache_src, nc_src, nnp) {
1231 if (ncp->nc_vp != NULL)
1234 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst);
1237 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
1243 * Flush all entries referencing a particular filesystem.
1246 cache_purgevfs(struct mount *mp)
1248 TAILQ_HEAD(, namecache) ncps;
1249 struct rwlock *bucketlock;
1250 struct nchashhead *bucket;
1251 struct namecache *ncp, *nnp;
1252 u_long i, j, n_nchash;
1254 /* Scan hash tables for applicable entries */
1255 SDT_PROBE1(vfs, namecache, purgevfs, done, mp);
1258 n_nchash = nchash + 1;
1259 for (i = 0; i < numbucketlocks; i++) {
1260 bucketlock = (struct rwlock *)&bucketlocks[i];
1261 rw_wlock(bucketlock);
1262 for (j = i; j < n_nchash; j += numbucketlocks) {
1263 bucket = &nchashtbl[j];
1264 LIST_FOREACH_SAFE(ncp, bucket, nc_hash, nnp) {
1265 cache_assert_bucket_locked(ncp, RA_WLOCKED);
1266 if (ncp->nc_dvp->v_mount != mp)
1268 cache_zap_locked(ncp);
1269 TAILQ_INSERT_HEAD(&ncps, ncp, nc_dst);
1272 rw_wunlock(bucketlock);
1275 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) {
1281 * Perform canonical checks and cache lookup and pass on to filesystem
1282 * through the vop_cachedlookup only if needed.
1286 vfs_cache_lookup(struct vop_lookup_args *ap)
1290 struct vnode **vpp = ap->a_vpp;
1291 struct componentname *cnp = ap->a_cnp;
1292 struct ucred *cred = cnp->cn_cred;
1293 int flags = cnp->cn_flags;
1294 struct thread *td = cnp->cn_thread;
1299 if (dvp->v_type != VDIR)
1302 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
1303 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
1306 error = VOP_ACCESS(dvp, VEXEC, cred, td);
1310 error = cache_lookup(dvp, vpp, cnp, NULL, NULL);
1312 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
1319 * XXX All of these sysctls would probably be more productive dead.
1321 static int disablecwd;
1322 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0,
1323 "Disable the getcwd syscall");
1325 /* Implementation of the getcwd syscall. */
1327 sys___getcwd(struct thread *td, struct __getcwd_args *uap)
1330 return (kern___getcwd(td, uap->buf, UIO_USERSPACE, uap->buflen,
1335 kern___getcwd(struct thread *td, char *buf, enum uio_seg bufseg, u_int buflen,
1339 struct filedesc *fdp;
1340 struct vnode *cdir, *rdir;
1347 if (buflen > path_max)
1350 tmpbuf = malloc(buflen, M_TEMP, M_WAITOK);
1351 fdp = td->td_proc->p_fd;
1352 FILEDESC_SLOCK(fdp);
1353 cdir = fdp->fd_cdir;
1355 rdir = fdp->fd_rdir;
1357 FILEDESC_SUNLOCK(fdp);
1358 error = vn_fullpath1(td, cdir, rdir, tmpbuf, &bp, buflen);
1363 if (bufseg == UIO_SYSSPACE)
1364 bcopy(bp, buf, strlen(bp) + 1);
1366 error = copyout(bp, buf, strlen(bp) + 1);
1368 if (KTRPOINT(curthread, KTR_NAMEI))
1372 free(tmpbuf, M_TEMP);
1377 * Thus begins the fullpath magic.
1380 static int disablefullpath;
1381 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW, &disablefullpath, 0,
1382 "Disable the vn_fullpath function");
1385 * Retrieve the full filesystem path that correspond to a vnode from the name
1386 * cache (if available)
1389 vn_fullpath(struct thread *td, struct vnode *vn, char **retbuf, char **freebuf)
1392 struct filedesc *fdp;
1396 if (disablefullpath)
1401 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1402 fdp = td->td_proc->p_fd;
1403 FILEDESC_SLOCK(fdp);
1404 rdir = fdp->fd_rdir;
1406 FILEDESC_SUNLOCK(fdp);
1407 error = vn_fullpath1(td, vn, rdir, buf, retbuf, MAXPATHLEN);
1418 * This function is similar to vn_fullpath, but it attempts to lookup the
1419 * pathname relative to the global root mount point. This is required for the
1420 * auditing sub-system, as audited pathnames must be absolute, relative to the
1421 * global root mount point.
1424 vn_fullpath_global(struct thread *td, struct vnode *vn,
1425 char **retbuf, char **freebuf)
1430 if (disablefullpath)
1434 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1435 error = vn_fullpath1(td, vn, rootvnode, buf, retbuf, MAXPATHLEN);
1444 vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, u_int *buflen)
1449 error = vn_vptocnp_locked(vp, cred, buf, buflen);
1456 vn_vptocnp_locked(struct vnode **vp, struct ucred *cred, char *buf,
1460 struct namecache *ncp;
1463 TAILQ_FOREACH(ncp, &((*vp)->v_cache_dst), nc_dst) {
1464 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1468 if (*buflen < ncp->nc_nlen) {
1471 counter_u64_add(numfullpathfail4, 1);
1473 SDT_PROBE3(vfs, namecache, fullpath, return, error,
1477 *buflen -= ncp->nc_nlen;
1478 memcpy(buf + *buflen, nc_get_name(ncp), ncp->nc_nlen);
1479 SDT_PROBE3(vfs, namecache, fullpath, hit, ncp->nc_dvp,
1480 nc_get_name(ncp), vp);
1489 SDT_PROBE1(vfs, namecache, fullpath, miss, vp);
1492 vn_lock(*vp, LK_SHARED | LK_RETRY);
1493 error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen);
1496 counter_u64_add(numfullpathfail2, 1);
1497 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
1503 if (dvp->v_iflag & VI_DOOMED) {
1504 /* forced unmount */
1508 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
1512 * *vp has its use count incremented still.
1519 * The magic behind kern___getcwd() and vn_fullpath().
1522 vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
1523 char *buf, char **retbuf, u_int buflen)
1525 int error, slash_prefixed;
1526 #ifdef KDTRACE_HOOKS
1527 struct vnode *startvp = vp;
1536 SDT_PROBE1(vfs, namecache, fullpath, entry, vp);
1537 counter_u64_add(numfullpathcalls, 1);
1540 if (vp->v_type != VDIR) {
1541 error = vn_vptocnp_locked(&vp, td->td_ucred, buf, &buflen);
1549 buf[--buflen] = '/';
1552 while (vp != rdir && vp != rootvnode) {
1553 if (vp->v_vflag & VV_ROOT) {
1554 if (vp->v_iflag & VI_DOOMED) { /* forced unmount */
1558 SDT_PROBE3(vfs, namecache, fullpath, return,
1562 vp1 = vp->v_mount->mnt_vnodecovered;
1570 if (vp->v_type != VDIR) {
1573 counter_u64_add(numfullpathfail1, 1);
1575 SDT_PROBE3(vfs, namecache, fullpath, return,
1579 error = vn_vptocnp_locked(&vp, td->td_ucred, buf, &buflen);
1586 SDT_PROBE3(vfs, namecache, fullpath, return, error,
1590 buf[--buflen] = '/';
1595 if (!slash_prefixed) {
1599 counter_u64_add(numfullpathfail4, 1);
1600 SDT_PROBE3(vfs, namecache, fullpath, return, ENOMEM,
1604 buf[--buflen] = '/';
1606 counter_u64_add(numfullpathfound, 1);
1610 SDT_PROBE3(vfs, namecache, fullpath, return, 0, startvp, buf + buflen);
1611 *retbuf = buf + buflen;
1616 vn_dir_dd_ino(struct vnode *vp)
1618 struct namecache *ncp;
1621 ASSERT_VOP_LOCKED(vp, "vn_dir_dd_ino");
1623 TAILQ_FOREACH(ncp, &(vp->v_cache_dst), nc_dst) {
1624 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0)
1629 if (vget(ddvp, LK_SHARED | LK_NOWAIT | LK_VNHELD, curthread))
1638 vn_commname(struct vnode *vp, char *buf, u_int buflen)
1640 struct namecache *ncp;
1644 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst)
1645 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1651 l = min(ncp->nc_nlen, buflen - 1);
1652 memcpy(buf, nc_get_name(ncp), l);
1658 /* ABI compat shims for old kernel modules. */
1661 void cache_enter(struct vnode *dvp, struct vnode *vp,
1662 struct componentname *cnp);
1665 cache_enter(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
1668 cache_enter_time(dvp, vp, cnp, NULL, NULL);
1672 * This function updates path string to vnode's full global path
1673 * and checks the size of the new path string against the pathlen argument.
1675 * Requires a locked, referenced vnode.
1676 * Vnode is re-locked on success or ENODEV, otherwise unlocked.
1678 * If sysctl debug.disablefullpath is set, ENODEV is returned,
1679 * vnode is left locked and path remain untouched.
1681 * If vp is a directory, the call to vn_fullpath_global() always succeeds
1682 * because it falls back to the ".." lookup if the namecache lookup fails.
1685 vn_path_to_global_path(struct thread *td, struct vnode *vp, char *path,
1688 struct nameidata nd;
1693 ASSERT_VOP_ELOCKED(vp, __func__);
1695 /* Return ENODEV if sysctl debug.disablefullpath==1 */
1696 if (disablefullpath)
1699 /* Construct global filesystem path from vp. */
1701 error = vn_fullpath_global(td, vp, &rpath, &fbuf);
1708 if (strlen(rpath) >= pathlen) {
1710 error = ENAMETOOLONG;
1715 * Re-lookup the vnode by path to detect a possible rename.
1716 * As a side effect, the vnode is relocked.
1717 * If vnode was renamed, return ENOENT.
1719 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
1720 UIO_SYSSPACE, path, td);
1726 NDFREE(&nd, NDF_ONLY_PNBUF);
1730 strcpy(path, rpath);