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>
54 #include <sys/syscallsubr.h>
55 #include <sys/sysctl.h>
56 #include <sys/sysproto.h>
57 #include <sys/vnode.h>
59 #include <sys/ktrace.h>
64 SDT_PROVIDER_DECLARE(vfs);
65 SDT_PROBE_DEFINE3(vfs, namecache, enter, done, "struct vnode *", "char *",
67 SDT_PROBE_DEFINE2(vfs, namecache, enter_negative, done, "struct vnode *",
69 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, entry, "struct vnode *");
70 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, hit, "struct vnode *",
71 "char *", "struct vnode *");
72 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, miss, "struct vnode *");
73 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, return, "int",
74 "struct vnode *", "char *");
75 SDT_PROBE_DEFINE3(vfs, namecache, lookup, hit, "struct vnode *", "char *",
77 SDT_PROBE_DEFINE2(vfs, namecache, lookup, hit__negative,
78 "struct vnode *", "char *");
79 SDT_PROBE_DEFINE2(vfs, namecache, lookup, miss, "struct vnode *",
81 SDT_PROBE_DEFINE1(vfs, namecache, purge, done, "struct vnode *");
82 SDT_PROBE_DEFINE1(vfs, namecache, purge_negative, done, "struct vnode *");
83 SDT_PROBE_DEFINE1(vfs, namecache, purgevfs, done, "struct mount *");
84 SDT_PROBE_DEFINE3(vfs, namecache, zap, done, "struct vnode *", "char *",
86 SDT_PROBE_DEFINE2(vfs, namecache, zap_negative, done, "struct vnode *",
90 * This structure describes the elements in the cache of recent
91 * names looked up by namei.
95 LIST_ENTRY(namecache) nc_hash; /* hash chain */
96 LIST_ENTRY(namecache) nc_src; /* source vnode list */
97 TAILQ_ENTRY(namecache) nc_dst; /* destination vnode list */
98 struct vnode *nc_dvp; /* vnode of parent of name */
99 struct vnode *nc_vp; /* vnode the name refers to */
100 u_char nc_flag; /* flag bits */
101 u_char nc_nlen; /* length of name */
102 char nc_name[0]; /* segment name + nul */
106 * struct namecache_ts repeats struct namecache layout up to the
108 * struct namecache_ts is used in place of struct namecache when time(s) need
109 * to be stored. The nc_dotdottime field is used when a cache entry is mapping
110 * both a non-dotdot directory name plus dotdot for the directory's
113 struct namecache_ts {
114 LIST_ENTRY(namecache) nc_hash; /* hash chain */
115 LIST_ENTRY(namecache) nc_src; /* source vnode list */
116 TAILQ_ENTRY(namecache) nc_dst; /* destination vnode list */
117 struct vnode *nc_dvp; /* vnode of parent of name */
118 struct vnode *nc_vp; /* vnode the name refers to */
119 u_char nc_flag; /* flag bits */
120 u_char nc_nlen; /* length of name */
121 struct timespec nc_time; /* timespec provided by fs */
122 struct timespec nc_dotdottime; /* dotdot timespec provided by fs */
123 int nc_ticks; /* ticks value when entry was added */
124 char nc_name[0]; /* segment name + nul */
128 * Flags in namecache.nc_flag
130 #define NCF_WHITE 0x01
131 #define NCF_ISDOTDOT 0x02
136 * Name caching works as follows:
138 * Names found by directory scans are retained in a cache
139 * for future reference. It is managed LRU, so frequently
140 * used names will hang around. Cache is indexed by hash value
141 * obtained from (vp, name) where vp refers to the directory
144 * If it is a "negative" entry, (i.e. for a name that is known NOT to
145 * exist) the vnode pointer will be NULL.
147 * Upon reaching the last segment of a path, if the reference
148 * is for DELETE, or NOCACHE is set (rewrite), and the
149 * name is located in the cache, it will be dropped.
153 * Structures associated with name caching.
155 #define NCHHASH(hash) \
156 (&nchashtbl[(hash) & nchash])
157 static LIST_HEAD(nchashhead, namecache) *nchashtbl; /* Hash Table */
158 static TAILQ_HEAD(, namecache) ncneg; /* Hash Table */
159 static u_long nchash; /* size of hash table */
160 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0,
161 "Size of namecache hash table");
162 static u_long ncnegfactor = 16; /* ratio of negative entries */
163 SYSCTL_ULONG(_vfs, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0,
164 "Ratio of negative namecache entries");
165 static u_long numneg; /* number of negative entries allocated */
166 SYSCTL_ULONG(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0,
167 "Number of negative entries in namecache");
168 static u_long numcache; /* number of cache entries allocated */
169 SYSCTL_ULONG(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0,
170 "Number of namecache entries");
171 static u_long numcachehv; /* number of cache entries with vnodes held */
172 SYSCTL_ULONG(_debug, OID_AUTO, numcachehv, CTLFLAG_RD, &numcachehv, 0,
173 "Number of namecache entries with vnodes held");
174 u_int ncsizefactor = 2;
175 SYSCTL_UINT(_vfs, OID_AUTO, ncsizefactor, CTLFLAG_RW, &ncsizefactor, 0,
176 "Size factor for namecache");
178 struct nchstats nchstats; /* cache effectiveness statistics */
180 static struct rwlock cache_lock;
181 RW_SYSINIT(vfscache, &cache_lock, "Name Cache");
183 #define CACHE_UPGRADE_LOCK() rw_try_upgrade(&cache_lock)
184 #define CACHE_RLOCK() rw_rlock(&cache_lock)
185 #define CACHE_RUNLOCK() rw_runlock(&cache_lock)
186 #define CACHE_WLOCK() rw_wlock(&cache_lock)
187 #define CACHE_WUNLOCK() rw_wunlock(&cache_lock)
190 * UMA zones for the VFS cache.
192 * The small cache is used for entries with short names, which are the
193 * most common. The large cache is used for entries which are too big to
194 * fit in the small cache.
196 static uma_zone_t cache_zone_small;
197 static uma_zone_t cache_zone_small_ts;
198 static uma_zone_t cache_zone_large;
199 static uma_zone_t cache_zone_large_ts;
201 #define CACHE_PATH_CUTOFF 35
203 static struct namecache *
204 cache_alloc(int len, int ts)
207 if (len > CACHE_PATH_CUTOFF) {
209 return (uma_zalloc(cache_zone_large_ts, M_WAITOK));
211 return (uma_zalloc(cache_zone_large, M_WAITOK));
214 return (uma_zalloc(cache_zone_small_ts, M_WAITOK));
216 return (uma_zalloc(cache_zone_small, M_WAITOK));
220 cache_free(struct namecache *ncp)
226 ts = ncp->nc_flag & NCF_TS;
227 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF) {
229 uma_zfree(cache_zone_small_ts, ncp);
231 uma_zfree(cache_zone_small, ncp);
233 uma_zfree(cache_zone_large_ts, ncp);
235 uma_zfree(cache_zone_large, ncp);
239 nc_get_name(struct namecache *ncp)
241 struct namecache_ts *ncp_ts;
243 if ((ncp->nc_flag & NCF_TS) == 0)
244 return (ncp->nc_name);
245 ncp_ts = (struct namecache_ts *)ncp;
246 return (ncp_ts->nc_name);
250 cache_out_ts(struct namecache *ncp, struct timespec *tsp, int *ticksp)
253 KASSERT((ncp->nc_flag & NCF_TS) != 0 ||
254 (tsp == NULL && ticksp == NULL),
258 *tsp = ((struct namecache_ts *)ncp)->nc_time;
260 *ticksp = ((struct namecache_ts *)ncp)->nc_ticks;
263 static int doingcache = 1; /* 1 => enable the cache */
264 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0,
265 "VFS namecache enabled");
267 /* Export size information to userland */
268 SYSCTL_INT(_debug_sizeof, OID_AUTO, namecache, CTLFLAG_RD, SYSCTL_NULL_INT_PTR,
269 sizeof(struct namecache), "sizeof(struct namecache)");
272 * The new name cache statistics
274 static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0,
275 "Name cache statistics");
276 #define STATNODE_ULONG(name, descr) \
277 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, descr);
278 #define STATNODE_COUNTER(name, descr) \
279 static counter_u64_t name; \
280 SYSCTL_COUNTER_U64(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, descr);
281 STATNODE_ULONG(numneg, "Number of negative cache entries");
282 STATNODE_ULONG(numcache, "Number of cache entries");
283 STATNODE_COUNTER(numcalls, "Number of cache lookups");
284 STATNODE_COUNTER(dothits, "Number of '.' hits");
285 STATNODE_COUNTER(dotdothits, "Number of '..' hits");
286 STATNODE_COUNTER(numchecks, "Number of checks in lookup");
287 STATNODE_COUNTER(nummiss, "Number of cache misses");
288 STATNODE_COUNTER(nummisszap, "Number of cache misses we do not want to cache");
289 STATNODE_COUNTER(numposzaps,
290 "Number of cache hits (positive) we do not want to cache");
291 STATNODE_COUNTER(numposhits, "Number of cache hits (positive)");
292 STATNODE_COUNTER(numnegzaps,
293 "Number of cache hits (negative) we do not want to cache");
294 STATNODE_COUNTER(numneghits, "Number of cache hits (negative)");
295 /* These count for kern___getcwd(), too. */
296 STATNODE_COUNTER(numfullpathcalls, "Number of fullpath search calls");
297 STATNODE_COUNTER(numfullpathfail1, "Number of fullpath search errors (ENOTDIR)");
298 STATNODE_COUNTER(numfullpathfail2,
299 "Number of fullpath search errors (VOP_VPTOCNP failures)");
300 STATNODE_COUNTER(numfullpathfail4, "Number of fullpath search errors (ENOMEM)");
301 STATNODE_COUNTER(numfullpathfound, "Number of successful fullpath calls");
302 static long numupgrades; STATNODE_ULONG(numupgrades,
303 "Number of updates of the cache after lookup (write lock + retry)");
305 static void cache_zap(struct namecache *ncp);
306 static int vn_vptocnp_locked(struct vnode **vp, struct ucred *cred, char *buf,
308 static int vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
309 char *buf, char **retbuf, u_int buflen);
311 static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
314 cache_get_hash(char *name, u_char len, struct vnode *dvp)
318 hash = fnv_32_buf(name, len, FNV1_32_INIT);
319 hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
324 sysctl_nchstats(SYSCTL_HANDLER_ARGS)
326 struct nchstats snap;
328 if (req->oldptr == NULL)
329 return (SYSCTL_OUT(req, 0, sizeof(snap)));
332 snap.ncs_goodhits = counter_u64_fetch(numposhits);
333 snap.ncs_neghits = counter_u64_fetch(numneghits);
334 snap.ncs_badhits = counter_u64_fetch(numposzaps) +
335 counter_u64_fetch(numnegzaps);
336 snap.ncs_miss = counter_u64_fetch(nummisszap) +
337 counter_u64_fetch(nummiss);
339 return (SYSCTL_OUT(req, &snap, sizeof(snap)));
341 SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE | CTLFLAG_RD |
342 CTLFLAG_MPSAFE, 0, 0, sysctl_nchstats, "LU",
343 "VFS cache effectiveness statistics");
347 * Grab an atomic snapshot of the name cache hash chain lengths
349 static SYSCTL_NODE(_debug, OID_AUTO, hashstat, CTLFLAG_RW, NULL,
353 sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS)
355 struct nchashhead *ncpp;
356 struct namecache *ncp;
357 int i, error, n_nchash, *cntbuf;
360 n_nchash = nchash + 1; /* nchash is max index, not count */
361 if (req->oldptr == NULL)
362 return SYSCTL_OUT(req, 0, n_nchash * sizeof(int));
363 cntbuf = malloc(n_nchash * sizeof(int), M_TEMP, M_ZERO | M_WAITOK);
365 if (n_nchash != nchash + 1) {
367 free(cntbuf, M_TEMP);
370 /* Scan hash tables counting entries */
371 for (ncpp = nchashtbl, i = 0; i < n_nchash; ncpp++, i++)
372 LIST_FOREACH(ncp, ncpp, nc_hash)
375 for (error = 0, i = 0; i < n_nchash; i++)
376 if ((error = SYSCTL_OUT(req, &cntbuf[i], sizeof(int))) != 0)
378 free(cntbuf, M_TEMP);
381 SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD|
382 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_rawnchash, "S,int",
383 "nchash chain lengths");
386 sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS)
389 struct nchashhead *ncpp;
390 struct namecache *ncp;
392 int count, maxlength, used, pct;
395 return SYSCTL_OUT(req, 0, 4 * sizeof(int));
398 n_nchash = nchash + 1; /* nchash is max index, not count */
402 /* Scan hash tables for applicable entries */
403 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
405 LIST_FOREACH(ncp, ncpp, nc_hash) {
410 if (maxlength < count)
413 n_nchash = nchash + 1;
415 pct = (used * 100) / (n_nchash / 100);
416 error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash));
419 error = SYSCTL_OUT(req, &used, sizeof(used));
422 error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength));
425 error = SYSCTL_OUT(req, &pct, sizeof(pct));
430 SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD|
431 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_nchash, "I",
432 "nchash statistics (number of total/used buckets, maximum chain length, usage percentage)");
438 * Removes a namecache entry from cache, whether it contains an actual
439 * pointer to a vnode or if it is just a negative cache entry.
442 cache_zap(struct namecache *ncp)
446 rw_assert(&cache_lock, RA_WLOCKED);
447 CTR2(KTR_VFS, "cache_zap(%p) vp %p", ncp, ncp->nc_vp);
448 if (ncp->nc_vp != NULL) {
449 SDT_PROBE3(vfs, namecache, zap, done, ncp->nc_dvp,
450 nc_get_name(ncp), ncp->nc_vp);
452 SDT_PROBE2(vfs, namecache, zap_negative, done, ncp->nc_dvp,
456 LIST_REMOVE(ncp, nc_hash);
457 if (ncp->nc_flag & NCF_ISDOTDOT) {
458 if (ncp == ncp->nc_dvp->v_cache_dd)
459 ncp->nc_dvp->v_cache_dd = NULL;
461 LIST_REMOVE(ncp, nc_src);
462 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) {
468 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
469 if (ncp == ncp->nc_vp->v_cache_dd)
470 ncp->nc_vp->v_cache_dd = NULL;
472 TAILQ_REMOVE(&ncneg, ncp, nc_dst);
482 * Lookup an entry in the cache
484 * Lookup is called with dvp pointing to the directory to search,
485 * cnp pointing to the name of the entry being sought. If the lookup
486 * succeeds, the vnode is returned in *vpp, and a status of -1 is
487 * returned. If the lookup determines that the name does not exist
488 * (negative caching), a status of ENOENT is returned. If the lookup
489 * fails, a status of zero is returned. If the directory vnode is
490 * recycled out from under us due to a forced unmount, a status of
491 * ENOENT is returned.
493 * vpp is locked and ref'd on return. If we're looking up DOTDOT, dvp is
494 * unlocked. If we're looking up . an extra ref is taken, but the lock is
495 * not recursively acquired.
499 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
500 struct timespec *tsp, int *ticksp)
502 struct namecache *ncp;
504 int error, ltype, wlocked;
507 cnp->cn_flags &= ~MAKEENTRY;
512 counter_u64_add(numcalls, 1);
516 if (cnp->cn_nameptr[0] == '.') {
517 if (cnp->cn_namelen == 1) {
519 CTR2(KTR_VFS, "cache_lookup(%p, %s) found via .",
520 dvp, cnp->cn_nameptr);
521 counter_u64_add(dothits, 1);
522 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", *vpp);
529 * When we lookup "." we still can be asked to lock it
532 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
533 if (ltype != VOP_ISLOCKED(*vpp)) {
534 if (ltype == LK_EXCLUSIVE) {
535 vn_lock(*vpp, LK_UPGRADE | LK_RETRY);
536 if ((*vpp)->v_iflag & VI_DOOMED) {
543 vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY);
549 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
550 counter_u64_add(dotdothits, 1);
551 if (dvp->v_cache_dd == NULL) {
552 SDT_PROBE3(vfs, namecache, lookup, miss, dvp,
556 if ((cnp->cn_flags & MAKEENTRY) == 0) {
557 if (!wlocked && !CACHE_UPGRADE_LOCK())
559 if (dvp->v_cache_dd->nc_flag & NCF_ISDOTDOT)
560 cache_zap(dvp->v_cache_dd);
561 dvp->v_cache_dd = NULL;
565 ncp = dvp->v_cache_dd;
566 if (ncp->nc_flag & NCF_ISDOTDOT)
570 /* Return failure if negative entry was found. */
572 goto negative_success;
573 CTR3(KTR_VFS, "cache_lookup(%p, %s) found %p via ..",
574 dvp, cnp->cn_nameptr, *vpp);
575 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, "..",
577 cache_out_ts(ncp, tsp, ticksp);
578 if ((ncp->nc_flag & (NCF_ISDOTDOT | NCF_DTS)) ==
579 NCF_DTS && tsp != NULL)
580 *tsp = ((struct namecache_ts *)ncp)->
587 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp);
588 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
589 counter_u64_add(numchecks, 1);
590 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
591 !bcmp(nc_get_name(ncp), cnp->cn_nameptr, ncp->nc_nlen))
595 /* We failed to find an entry */
597 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
599 if ((cnp->cn_flags & MAKEENTRY) == 0) {
600 counter_u64_add(nummisszap, 1);
602 counter_u64_add(nummiss, 1);
607 /* We don't want to have an entry, so dump it */
608 if ((cnp->cn_flags & MAKEENTRY) == 0) {
609 counter_u64_add(numposzaps, 1);
610 if (!wlocked && !CACHE_UPGRADE_LOCK())
617 /* We found a "positive" match, return the vnode */
619 counter_u64_add(numposhits, 1);
621 CTR4(KTR_VFS, "cache_lookup(%p, %s) found %p via ncp %p",
622 dvp, cnp->cn_nameptr, *vpp, ncp);
623 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, nc_get_name(ncp),
625 cache_out_ts(ncp, tsp, ticksp);
630 /* We found a negative match, and want to create it, so purge */
631 if (cnp->cn_nameiop == CREATE) {
632 counter_u64_add(numnegzaps, 1);
633 if (!wlocked && !CACHE_UPGRADE_LOCK())
640 if (!wlocked && !CACHE_UPGRADE_LOCK())
642 counter_u64_add(numneghits, 1);
644 * We found a "negative" match, so we shift it to the end of
645 * the "negative" cache entries queue to satisfy LRU. Also,
646 * check to see if the entry is a whiteout; indicate this to
647 * the componentname, if so.
649 TAILQ_REMOVE(&ncneg, ncp, nc_dst);
650 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
651 if (ncp->nc_flag & NCF_WHITE)
652 cnp->cn_flags |= ISWHITEOUT;
653 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp,
655 cache_out_ts(ncp, tsp, ticksp);
661 * We need to update the cache after our lookup, so upgrade to
662 * a write lock and retry the operation.
672 * On success we return a locked and ref'd vnode as per the lookup
676 ltype = 0; /* silence gcc warning */
677 if (cnp->cn_flags & ISDOTDOT) {
678 ltype = VOP_ISLOCKED(dvp);
686 error = vget(*vpp, cnp->cn_lkflags | LK_VNHELD, cnp->cn_thread);
687 if (cnp->cn_flags & ISDOTDOT) {
688 vn_lock(dvp, ltype | LK_RETRY);
689 if (dvp->v_iflag & VI_DOOMED) {
700 if ((cnp->cn_flags & ISLASTCN) &&
701 (cnp->cn_lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE) {
702 ASSERT_VOP_ELOCKED(*vpp, "cache_lookup");
715 * Add an entry to the cache.
718 cache_enter_time(struct vnode *dvp, struct vnode *vp, struct componentname *cnp,
719 struct timespec *tsp, struct timespec *dtsp)
721 struct namecache *ncp, *n2;
722 struct namecache_ts *n3;
723 struct nchashhead *ncpp;
728 CTR3(KTR_VFS, "cache_enter(%p, %p, %s)", dvp, vp, cnp->cn_nameptr);
729 VNASSERT(vp == NULL || (vp->v_iflag & VI_DOOMED) == 0, vp,
730 ("cache_enter: Adding a doomed vnode"));
731 VNASSERT(dvp == NULL || (dvp->v_iflag & VI_DOOMED) == 0, dvp,
732 ("cache_enter: Doomed vnode used as src"));
738 * Avoid blowout in namecache entries.
740 if (numcache >= desiredvnodes * ncsizefactor)
744 if (cnp->cn_nameptr[0] == '.') {
745 if (cnp->cn_namelen == 1)
747 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
750 * If dotdot entry already exists, just retarget it
751 * to new parent vnode, otherwise continue with new
752 * namecache entry allocation.
754 if ((ncp = dvp->v_cache_dd) != NULL &&
755 ncp->nc_flag & NCF_ISDOTDOT) {
756 KASSERT(ncp->nc_dvp == dvp,
757 ("wrong isdotdot parent"));
758 if (ncp->nc_vp != NULL) {
759 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst,
762 TAILQ_REMOVE(&ncneg, ncp, nc_dst);
766 TAILQ_INSERT_HEAD(&vp->v_cache_dst,
769 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
776 dvp->v_cache_dd = NULL;
777 SDT_PROBE3(vfs, namecache, enter, done, dvp, "..", vp);
784 * Calculate the hash key and setup as much of the new
785 * namecache entry as possible before acquiring the lock.
787 ncp = cache_alloc(cnp->cn_namelen, tsp != NULL);
792 n3 = (struct namecache_ts *)ncp;
794 n3->nc_ticks = ticks;
795 n3->nc_flag |= NCF_TS;
797 n3->nc_dotdottime = *dtsp;
798 n3->nc_flag |= NCF_DTS;
801 len = ncp->nc_nlen = cnp->cn_namelen;
802 hash = cache_get_hash(cnp->cn_nameptr, len, dvp);
803 strlcpy(nc_get_name(ncp), cnp->cn_nameptr, len + 1);
807 * See if this vnode or negative entry is already in the cache
808 * with this name. This can happen with concurrent lookups of
809 * the same path name.
811 ncpp = NCHHASH(hash);
812 LIST_FOREACH(n2, ncpp, nc_hash) {
813 if (n2->nc_dvp == dvp &&
814 n2->nc_nlen == cnp->cn_namelen &&
815 !bcmp(nc_get_name(n2), cnp->cn_nameptr, n2->nc_nlen)) {
817 KASSERT((n2->nc_flag & NCF_TS) != 0,
819 n3 = (struct namecache_ts *)n2;
821 ((struct namecache_ts *)ncp)->nc_time;
823 ((struct namecache_ts *)ncp)->nc_ticks;
826 ((struct namecache_ts *)ncp)->
828 n3->nc_flag |= NCF_DTS;
837 if (flag == NCF_ISDOTDOT) {
839 * See if we are trying to add .. entry, but some other lookup
840 * has populated v_cache_dd pointer already.
842 if (dvp->v_cache_dd != NULL) {
847 KASSERT(vp == NULL || vp->v_type == VDIR,
848 ("wrong vnode type %p", vp));
849 dvp->v_cache_dd = ncp;
854 if (vp->v_type == VDIR) {
855 if (flag != NCF_ISDOTDOT) {
857 * For this case, the cache entry maps both the
858 * directory name in it and the name ".." for the
859 * directory's parent.
861 if ((n2 = vp->v_cache_dd) != NULL &&
862 (n2->nc_flag & NCF_ISDOTDOT) != 0)
864 vp->v_cache_dd = ncp;
867 vp->v_cache_dd = NULL;
872 * Insert the new namecache entry into the appropriate chain
873 * within the cache entries table.
875 LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
876 if (flag != NCF_ISDOTDOT) {
877 if (LIST_EMPTY(&dvp->v_cache_src)) {
881 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
885 * If the entry is "negative", we place it into the
886 * "negative" cache queue, otherwise, we place it into the
887 * destination vnode's cache entries queue.
890 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
891 SDT_PROBE3(vfs, namecache, enter, done, dvp, nc_get_name(ncp),
894 if (cnp->cn_flags & ISWHITEOUT)
895 ncp->nc_flag |= NCF_WHITE;
896 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
898 SDT_PROBE2(vfs, namecache, enter_negative, done, dvp,
901 if (numneg * ncnegfactor > numcache) {
902 ncp = TAILQ_FIRST(&ncneg);
903 KASSERT(ncp->nc_vp == NULL, ("ncp %p vp %p on ncneg",
911 * Name cache initialization, from vfs_init() when we are booting
914 nchinit(void *dummy __unused)
919 cache_zone_small = uma_zcreate("S VFS Cache",
920 sizeof(struct namecache) + CACHE_PATH_CUTOFF + 1,
921 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
922 cache_zone_small_ts = uma_zcreate("STS VFS Cache",
923 sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1,
924 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
925 cache_zone_large = uma_zcreate("L VFS Cache",
926 sizeof(struct namecache) + NAME_MAX + 1,
927 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
928 cache_zone_large_ts = uma_zcreate("LTS VFS Cache",
929 sizeof(struct namecache_ts) + NAME_MAX + 1,
930 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
932 nchashtbl = hashinit(desiredvnodes * 2, M_VFSCACHE, &nchash);
934 numcalls = counter_u64_alloc(M_WAITOK);
935 dothits = counter_u64_alloc(M_WAITOK);
936 dotdothits = counter_u64_alloc(M_WAITOK);
937 numchecks = counter_u64_alloc(M_WAITOK);
938 nummiss = counter_u64_alloc(M_WAITOK);
939 nummisszap = counter_u64_alloc(M_WAITOK);
940 numposzaps = counter_u64_alloc(M_WAITOK);
941 numposhits = counter_u64_alloc(M_WAITOK);
942 numnegzaps = counter_u64_alloc(M_WAITOK);
943 numneghits = counter_u64_alloc(M_WAITOK);
944 numfullpathcalls = counter_u64_alloc(M_WAITOK);
945 numfullpathfail1 = counter_u64_alloc(M_WAITOK);
946 numfullpathfail2 = counter_u64_alloc(M_WAITOK);
947 numfullpathfail4 = counter_u64_alloc(M_WAITOK);
948 numfullpathfound = counter_u64_alloc(M_WAITOK);
950 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL);
953 cache_changesize(int newmaxvnodes)
955 struct nchashhead *new_nchashtbl, *old_nchashtbl;
956 u_long new_nchash, old_nchash;
957 struct namecache *ncp;
961 new_nchashtbl = hashinit(newmaxvnodes * 2, M_VFSCACHE, &new_nchash);
962 /* If same hash table size, nothing to do */
963 if (nchash == new_nchash) {
964 free(new_nchashtbl, M_VFSCACHE);
968 * Move everything from the old hash table to the new table.
969 * None of the namecache entries in the table can be removed
970 * because to do so, they have to be removed from the hash table.
973 old_nchashtbl = nchashtbl;
975 nchashtbl = new_nchashtbl;
977 for (i = 0; i <= old_nchash; i++) {
978 while ((ncp = LIST_FIRST(&old_nchashtbl[i])) != NULL) {
979 hash = cache_get_hash(nc_get_name(ncp), ncp->nc_nlen,
981 LIST_REMOVE(ncp, nc_hash);
982 LIST_INSERT_HEAD(NCHHASH(hash), ncp, nc_hash);
986 free(old_nchashtbl, M_VFSCACHE);
990 * Invalidate all entries to a particular vnode.
993 cache_purge(struct vnode *vp)
996 CTR1(KTR_VFS, "cache_purge(%p)", vp);
997 SDT_PROBE1(vfs, namecache, purge, done, vp);
999 while (!LIST_EMPTY(&vp->v_cache_src))
1000 cache_zap(LIST_FIRST(&vp->v_cache_src));
1001 while (!TAILQ_EMPTY(&vp->v_cache_dst))
1002 cache_zap(TAILQ_FIRST(&vp->v_cache_dst));
1003 if (vp->v_cache_dd != NULL) {
1004 KASSERT(vp->v_cache_dd->nc_flag & NCF_ISDOTDOT,
1005 ("lost dotdot link"));
1006 cache_zap(vp->v_cache_dd);
1008 KASSERT(vp->v_cache_dd == NULL, ("incomplete purge"));
1013 * Invalidate all negative entries for a particular directory vnode.
1016 cache_purge_negative(struct vnode *vp)
1018 struct namecache *cp, *ncp;
1020 CTR1(KTR_VFS, "cache_purge_negative(%p)", vp);
1021 SDT_PROBE1(vfs, namecache, purge_negative, done, vp);
1023 LIST_FOREACH_SAFE(cp, &vp->v_cache_src, nc_src, ncp) {
1024 if (cp->nc_vp == NULL)
1031 * Flush all entries referencing a particular filesystem.
1034 cache_purgevfs(struct mount *mp)
1036 struct nchashhead *ncpp;
1037 struct namecache *ncp, *nnp;
1039 /* Scan hash tables for applicable entries */
1040 SDT_PROBE1(vfs, namecache, purgevfs, done, mp);
1042 for (ncpp = &nchashtbl[nchash]; ncpp >= nchashtbl; ncpp--) {
1043 LIST_FOREACH_SAFE(ncp, ncpp, nc_hash, nnp) {
1044 if (ncp->nc_dvp->v_mount == mp)
1052 * Perform canonical checks and cache lookup and pass on to filesystem
1053 * through the vop_cachedlookup only if needed.
1057 vfs_cache_lookup(struct vop_lookup_args *ap)
1061 struct vnode **vpp = ap->a_vpp;
1062 struct componentname *cnp = ap->a_cnp;
1063 struct ucred *cred = cnp->cn_cred;
1064 int flags = cnp->cn_flags;
1065 struct thread *td = cnp->cn_thread;
1070 if (dvp->v_type != VDIR)
1073 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
1074 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
1077 error = VOP_ACCESS(dvp, VEXEC, cred, td);
1081 error = cache_lookup(dvp, vpp, cnp, NULL, NULL);
1083 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
1090 * XXX All of these sysctls would probably be more productive dead.
1092 static int disablecwd;
1093 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0,
1094 "Disable the getcwd syscall");
1096 /* Implementation of the getcwd syscall. */
1098 sys___getcwd(struct thread *td, struct __getcwd_args *uap)
1101 return (kern___getcwd(td, uap->buf, UIO_USERSPACE, uap->buflen,
1106 kern___getcwd(struct thread *td, char *buf, enum uio_seg bufseg, u_int buflen,
1110 struct filedesc *fdp;
1111 struct vnode *cdir, *rdir;
1118 if (buflen > path_max)
1121 tmpbuf = malloc(buflen, M_TEMP, M_WAITOK);
1122 fdp = td->td_proc->p_fd;
1123 FILEDESC_SLOCK(fdp);
1124 cdir = fdp->fd_cdir;
1126 rdir = fdp->fd_rdir;
1128 FILEDESC_SUNLOCK(fdp);
1129 error = vn_fullpath1(td, cdir, rdir, tmpbuf, &bp, buflen);
1134 if (bufseg == UIO_SYSSPACE)
1135 bcopy(bp, buf, strlen(bp) + 1);
1137 error = copyout(bp, buf, strlen(bp) + 1);
1139 if (KTRPOINT(curthread, KTR_NAMEI))
1143 free(tmpbuf, M_TEMP);
1148 * Thus begins the fullpath magic.
1151 static int disablefullpath;
1152 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW, &disablefullpath, 0,
1153 "Disable the vn_fullpath function");
1156 * Retrieve the full filesystem path that correspond to a vnode from the name
1157 * cache (if available)
1160 vn_fullpath(struct thread *td, struct vnode *vn, char **retbuf, char **freebuf)
1163 struct filedesc *fdp;
1167 if (disablefullpath)
1172 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1173 fdp = td->td_proc->p_fd;
1174 FILEDESC_SLOCK(fdp);
1175 rdir = fdp->fd_rdir;
1177 FILEDESC_SUNLOCK(fdp);
1178 error = vn_fullpath1(td, vn, rdir, buf, retbuf, MAXPATHLEN);
1189 * This function is similar to vn_fullpath, but it attempts to lookup the
1190 * pathname relative to the global root mount point. This is required for the
1191 * auditing sub-system, as audited pathnames must be absolute, relative to the
1192 * global root mount point.
1195 vn_fullpath_global(struct thread *td, struct vnode *vn,
1196 char **retbuf, char **freebuf)
1201 if (disablefullpath)
1205 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1206 error = vn_fullpath1(td, vn, rootvnode, buf, retbuf, MAXPATHLEN);
1215 vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, u_int *buflen)
1220 error = vn_vptocnp_locked(vp, cred, buf, buflen);
1227 vn_vptocnp_locked(struct vnode **vp, struct ucred *cred, char *buf,
1231 struct namecache *ncp;
1234 TAILQ_FOREACH(ncp, &((*vp)->v_cache_dst), nc_dst) {
1235 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1239 if (*buflen < ncp->nc_nlen) {
1242 counter_u64_add(numfullpathfail4, 1);
1244 SDT_PROBE3(vfs, namecache, fullpath, return, error,
1248 *buflen -= ncp->nc_nlen;
1249 memcpy(buf + *buflen, nc_get_name(ncp), ncp->nc_nlen);
1250 SDT_PROBE3(vfs, namecache, fullpath, hit, ncp->nc_dvp,
1251 nc_get_name(ncp), vp);
1260 SDT_PROBE1(vfs, namecache, fullpath, miss, vp);
1263 vn_lock(*vp, LK_SHARED | LK_RETRY);
1264 error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen);
1267 counter_u64_add(numfullpathfail2, 1);
1268 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
1274 if (dvp->v_iflag & VI_DOOMED) {
1275 /* forced unmount */
1279 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
1283 * *vp has its use count incremented still.
1290 * The magic behind kern___getcwd() and vn_fullpath().
1293 vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
1294 char *buf, char **retbuf, u_int buflen)
1296 int error, slash_prefixed;
1297 #ifdef KDTRACE_HOOKS
1298 struct vnode *startvp = vp;
1307 SDT_PROBE1(vfs, namecache, fullpath, entry, vp);
1308 counter_u64_add(numfullpathcalls, 1);
1311 if (vp->v_type != VDIR) {
1312 error = vn_vptocnp_locked(&vp, td->td_ucred, buf, &buflen);
1320 buf[--buflen] = '/';
1323 while (vp != rdir && vp != rootvnode) {
1324 if (vp->v_vflag & VV_ROOT) {
1325 if (vp->v_iflag & VI_DOOMED) { /* forced unmount */
1329 SDT_PROBE3(vfs, namecache, fullpath, return,
1333 vp1 = vp->v_mount->mnt_vnodecovered;
1341 if (vp->v_type != VDIR) {
1344 counter_u64_add(numfullpathfail1, 1);
1346 SDT_PROBE3(vfs, namecache, fullpath, return,
1350 error = vn_vptocnp_locked(&vp, td->td_ucred, buf, &buflen);
1357 SDT_PROBE3(vfs, namecache, fullpath, return, error,
1361 buf[--buflen] = '/';
1366 if (!slash_prefixed) {
1370 counter_u64_add(numfullpathfail4, 1);
1371 SDT_PROBE3(vfs, namecache, fullpath, return, ENOMEM,
1375 buf[--buflen] = '/';
1377 counter_u64_add(numfullpathfound, 1);
1381 SDT_PROBE3(vfs, namecache, fullpath, return, 0, startvp, buf + buflen);
1382 *retbuf = buf + buflen;
1387 vn_dir_dd_ino(struct vnode *vp)
1389 struct namecache *ncp;
1392 ASSERT_VOP_LOCKED(vp, "vn_dir_dd_ino");
1394 TAILQ_FOREACH(ncp, &(vp->v_cache_dst), nc_dst) {
1395 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0)
1400 if (vget(ddvp, LK_SHARED | LK_NOWAIT | LK_VNHELD, curthread))
1409 vn_commname(struct vnode *vp, char *buf, u_int buflen)
1411 struct namecache *ncp;
1415 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst)
1416 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1422 l = min(ncp->nc_nlen, buflen - 1);
1423 memcpy(buf, nc_get_name(ncp), l);
1429 /* ABI compat shims for old kernel modules. */
1432 void cache_enter(struct vnode *dvp, struct vnode *vp,
1433 struct componentname *cnp);
1436 cache_enter(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
1439 cache_enter_time(dvp, vp, cnp, NULL, NULL);
1443 * This function updates path string to vnode's full global path
1444 * and checks the size of the new path string against the pathlen argument.
1446 * Requires a locked, referenced vnode.
1447 * Vnode is re-locked on success or ENODEV, otherwise unlocked.
1449 * If sysctl debug.disablefullpath is set, ENODEV is returned,
1450 * vnode is left locked and path remain untouched.
1452 * If vp is a directory, the call to vn_fullpath_global() always succeeds
1453 * because it falls back to the ".." lookup if the namecache lookup fails.
1456 vn_path_to_global_path(struct thread *td, struct vnode *vp, char *path,
1459 struct nameidata nd;
1464 ASSERT_VOP_ELOCKED(vp, __func__);
1466 /* Return ENODEV if sysctl debug.disablefullpath==1 */
1467 if (disablefullpath)
1470 /* Construct global filesystem path from vp. */
1472 error = vn_fullpath_global(td, vp, &rpath, &fbuf);
1479 if (strlen(rpath) >= pathlen) {
1481 error = ENAMETOOLONG;
1486 * Re-lookup the vnode by path to detect a possible rename.
1487 * As a side effect, the vnode is relocked.
1488 * If vnode was renamed, return ENOENT.
1490 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
1491 UIO_SYSSPACE, path, td);
1497 NDFREE(&nd, NDF_ONLY_PNBUF);
1501 strcpy(path, rpath);