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 cacheing.
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 static 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 sysctl_nchstats(SYSCTL_HANDLER_ARGS)
316 struct nchstats snap;
318 if (req->oldptr == NULL)
319 return (SYSCTL_OUT(req, 0, sizeof(snap)));
322 snap.ncs_goodhits = counter_u64_fetch(numposhits);
323 snap.ncs_neghits = counter_u64_fetch(numneghits);
324 snap.ncs_badhits = counter_u64_fetch(numposzaps) +
325 counter_u64_fetch(numnegzaps);
326 snap.ncs_miss = counter_u64_fetch(nummisszap) +
327 counter_u64_fetch(nummiss);
329 return (SYSCTL_OUT(req, &snap, sizeof(snap)));
331 SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE | CTLFLAG_RD |
332 CTLFLAG_MPSAFE, 0, 0, sysctl_nchstats, "LU",
333 "VFS cache effectiveness statistics");
337 * Grab an atomic snapshot of the name cache hash chain lengths
339 static SYSCTL_NODE(_debug, OID_AUTO, hashstat, CTLFLAG_RW, NULL,
343 sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS)
345 struct nchashhead *ncpp;
346 struct namecache *ncp;
347 int i, error, n_nchash, *cntbuf;
350 n_nchash = nchash + 1; /* nchash is max index, not count */
351 if (req->oldptr == NULL)
352 return SYSCTL_OUT(req, 0, n_nchash * sizeof(int));
353 cntbuf = malloc(n_nchash * sizeof(int), M_TEMP, M_ZERO | M_WAITOK);
355 if (n_nchash != nchash + 1) {
357 free(cntbuf, M_TEMP);
360 /* Scan hash tables counting entries */
361 for (ncpp = nchashtbl, i = 0; i < n_nchash; ncpp++, i++)
362 LIST_FOREACH(ncp, ncpp, nc_hash)
365 for (error = 0, i = 0; i < n_nchash; i++)
366 if ((error = SYSCTL_OUT(req, &cntbuf[i], sizeof(int))) != 0)
368 free(cntbuf, M_TEMP);
371 SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD|
372 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_rawnchash, "S,int",
373 "nchash chain lengths");
376 sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS)
379 struct nchashhead *ncpp;
380 struct namecache *ncp;
382 int count, maxlength, used, pct;
385 return SYSCTL_OUT(req, 0, 4 * sizeof(int));
388 n_nchash = nchash + 1; /* nchash is max index, not count */
392 /* Scan hash tables for applicable entries */
393 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
395 LIST_FOREACH(ncp, ncpp, nc_hash) {
400 if (maxlength < count)
403 n_nchash = nchash + 1;
405 pct = (used * 100) / (n_nchash / 100);
406 error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash));
409 error = SYSCTL_OUT(req, &used, sizeof(used));
412 error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength));
415 error = SYSCTL_OUT(req, &pct, sizeof(pct));
420 SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD|
421 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_nchash, "I",
422 "nchash statistics (number of total/used buckets, maximum chain length, usage percentage)");
428 * Removes a namecache entry from cache, whether it contains an actual
429 * pointer to a vnode or if it is just a negative cache entry.
432 cache_zap(struct namecache *ncp)
436 rw_assert(&cache_lock, RA_WLOCKED);
437 CTR2(KTR_VFS, "cache_zap(%p) vp %p", ncp, ncp->nc_vp);
438 if (ncp->nc_vp != NULL) {
439 SDT_PROBE3(vfs, namecache, zap, done, ncp->nc_dvp,
440 nc_get_name(ncp), ncp->nc_vp);
442 SDT_PROBE2(vfs, namecache, zap_negative, done, ncp->nc_dvp,
446 LIST_REMOVE(ncp, nc_hash);
447 if (ncp->nc_flag & NCF_ISDOTDOT) {
448 if (ncp == ncp->nc_dvp->v_cache_dd)
449 ncp->nc_dvp->v_cache_dd = NULL;
451 LIST_REMOVE(ncp, nc_src);
452 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) {
458 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
459 if (ncp == ncp->nc_vp->v_cache_dd)
460 ncp->nc_vp->v_cache_dd = NULL;
462 TAILQ_REMOVE(&ncneg, ncp, nc_dst);
472 * Lookup an entry in the cache
474 * Lookup is called with dvp pointing to the directory to search,
475 * cnp pointing to the name of the entry being sought. If the lookup
476 * succeeds, the vnode is returned in *vpp, and a status of -1 is
477 * returned. If the lookup determines that the name does not exist
478 * (negative cacheing), a status of ENOENT is returned. If the lookup
479 * fails, a status of zero is returned. If the directory vnode is
480 * recycled out from under us due to a forced unmount, a status of
481 * ENOENT is returned.
483 * vpp is locked and ref'd on return. If we're looking up DOTDOT, dvp is
484 * unlocked. If we're looking up . an extra ref is taken, but the lock is
485 * not recursively acquired.
489 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
490 struct timespec *tsp, int *ticksp)
492 struct namecache *ncp;
494 int error, ltype, wlocked;
497 cnp->cn_flags &= ~MAKEENTRY;
503 counter_u64_add(numcalls, 1);
507 if (cnp->cn_nameptr[0] == '.') {
508 if (cnp->cn_namelen == 1) {
510 CTR2(KTR_VFS, "cache_lookup(%p, %s) found via .",
511 dvp, cnp->cn_nameptr);
512 counter_u64_add(dothits, 1);
513 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", *vpp);
520 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
521 counter_u64_add(dotdothits, 1);
522 if (dvp->v_cache_dd == NULL) {
523 SDT_PROBE3(vfs, namecache, lookup, miss, dvp,
527 if ((cnp->cn_flags & MAKEENTRY) == 0) {
528 if (!wlocked && !CACHE_UPGRADE_LOCK())
530 if (dvp->v_cache_dd->nc_flag & NCF_ISDOTDOT)
531 cache_zap(dvp->v_cache_dd);
532 dvp->v_cache_dd = NULL;
536 ncp = dvp->v_cache_dd;
537 if (ncp->nc_flag & NCF_ISDOTDOT)
541 /* Return failure if negative entry was found. */
543 goto negative_success;
544 CTR3(KTR_VFS, "cache_lookup(%p, %s) found %p via ..",
545 dvp, cnp->cn_nameptr, *vpp);
546 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, "..",
548 cache_out_ts(ncp, tsp, ticksp);
549 if ((ncp->nc_flag & (NCF_ISDOTDOT | NCF_DTS)) ==
550 NCF_DTS && tsp != NULL)
551 *tsp = ((struct namecache_ts *)ncp)->
557 hash = fnv_32_buf(cnp->cn_nameptr, cnp->cn_namelen, FNV1_32_INIT);
558 hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
559 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
560 counter_u64_add(numchecks, 1);
561 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
562 !bcmp(nc_get_name(ncp), cnp->cn_nameptr, ncp->nc_nlen))
566 /* We failed to find an entry */
568 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
570 if ((cnp->cn_flags & MAKEENTRY) == 0) {
571 counter_u64_add(nummisszap, 1);
573 counter_u64_add(nummiss, 1);
578 /* We don't want to have an entry, so dump it */
579 if ((cnp->cn_flags & MAKEENTRY) == 0) {
580 counter_u64_add(numposzaps, 1);
581 if (!wlocked && !CACHE_UPGRADE_LOCK())
588 /* We found a "positive" match, return the vnode */
590 counter_u64_add(numposhits, 1);
592 CTR4(KTR_VFS, "cache_lookup(%p, %s) found %p via ncp %p",
593 dvp, cnp->cn_nameptr, *vpp, ncp);
594 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, nc_get_name(ncp),
596 cache_out_ts(ncp, tsp, ticksp);
601 /* We found a negative match, and want to create it, so purge */
602 if (cnp->cn_nameiop == CREATE) {
603 counter_u64_add(numnegzaps, 1);
604 if (!wlocked && !CACHE_UPGRADE_LOCK())
611 if (!wlocked && !CACHE_UPGRADE_LOCK())
613 counter_u64_add(numneghits, 1);
615 * We found a "negative" match, so we shift it to the end of
616 * the "negative" cache entries queue to satisfy LRU. Also,
617 * check to see if the entry is a whiteout; indicate this to
618 * the componentname, if so.
620 TAILQ_REMOVE(&ncneg, ncp, nc_dst);
621 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
622 if (ncp->nc_flag & NCF_WHITE)
623 cnp->cn_flags |= ISWHITEOUT;
624 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp,
626 cache_out_ts(ncp, tsp, ticksp);
632 * We need to update the cache after our lookup, so upgrade to
633 * a write lock and retry the operation.
643 * On success we return a locked and ref'd vnode as per the lookup
646 if (dvp == *vpp) { /* lookup on "." */
653 * When we lookup "." we still can be asked to lock it
656 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
657 if (ltype != VOP_ISLOCKED(*vpp)) {
658 if (ltype == LK_EXCLUSIVE) {
659 vn_lock(*vpp, LK_UPGRADE | LK_RETRY);
660 if ((*vpp)->v_iflag & VI_DOOMED) {
667 vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY);
671 ltype = 0; /* silence gcc warning */
672 if (cnp->cn_flags & ISDOTDOT) {
673 ltype = VOP_ISLOCKED(dvp);
681 error = vget(*vpp, cnp->cn_lkflags | LK_VNHELD, cnp->cn_thread);
682 if (cnp->cn_flags & ISDOTDOT) {
683 vn_lock(dvp, ltype | LK_RETRY);
684 if (dvp->v_iflag & VI_DOOMED) {
695 if ((cnp->cn_flags & ISLASTCN) &&
696 (cnp->cn_lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE) {
697 ASSERT_VOP_ELOCKED(*vpp, "cache_lookup");
710 * Add an entry to the cache.
713 cache_enter_time(struct vnode *dvp, struct vnode *vp, struct componentname *cnp,
714 struct timespec *tsp, struct timespec *dtsp)
716 struct namecache *ncp, *n2;
717 struct namecache_ts *n3;
718 struct nchashhead *ncpp;
725 CTR3(KTR_VFS, "cache_enter(%p, %p, %s)", dvp, vp, cnp->cn_nameptr);
726 VNASSERT(vp == NULL || (vp->v_iflag & VI_DOOMED) == 0, vp,
727 ("cache_enter: Adding a doomed vnode"));
728 VNASSERT(dvp == NULL || (dvp->v_iflag & VI_DOOMED) == 0, dvp,
729 ("cache_enter: Doomed vnode used as src"));
735 * Avoid blowout in namecache entries.
737 if (numcache >= desiredvnodes * ncsizefactor)
741 if (cnp->cn_nameptr[0] == '.') {
742 if (cnp->cn_namelen == 1)
744 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
747 * If dotdot entry already exists, just retarget it
748 * to new parent vnode, otherwise continue with new
749 * namecache entry allocation.
751 if ((ncp = dvp->v_cache_dd) != NULL &&
752 ncp->nc_flag & NCF_ISDOTDOT) {
753 KASSERT(ncp->nc_dvp == dvp,
754 ("wrong isdotdot parent"));
755 if (ncp->nc_vp != NULL) {
756 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst,
759 TAILQ_REMOVE(&ncneg, ncp, nc_dst);
763 TAILQ_INSERT_HEAD(&vp->v_cache_dst,
766 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
773 dvp->v_cache_dd = NULL;
774 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 = fnv_32_buf(cnp->cn_nameptr, len, FNV1_32_INIT);
803 strlcpy(nc_get_name(ncp), cnp->cn_nameptr, len + 1);
804 hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
808 * See if this vnode or negative entry is already in the cache
809 * with this name. This can happen with concurrent lookups of
810 * the same path name.
812 ncpp = NCHHASH(hash);
813 LIST_FOREACH(n2, ncpp, nc_hash) {
814 if (n2->nc_dvp == dvp &&
815 n2->nc_nlen == cnp->cn_namelen &&
816 !bcmp(nc_get_name(n2), cnp->cn_nameptr, n2->nc_nlen)) {
818 KASSERT((n2->nc_flag & NCF_TS) != 0,
820 n3 = (struct namecache_ts *)n2;
822 ((struct namecache_ts *)ncp)->nc_time;
824 ((struct namecache_ts *)ncp)->nc_ticks;
827 ((struct namecache_ts *)ncp)->
829 n3->nc_flag |= NCF_DTS;
838 if (flag == NCF_ISDOTDOT) {
840 * See if we are trying to add .. entry, but some other lookup
841 * has populated v_cache_dd pointer already.
843 if (dvp->v_cache_dd != NULL) {
848 KASSERT(vp == NULL || vp->v_type == VDIR,
849 ("wrong vnode type %p", vp));
850 dvp->v_cache_dd = ncp;
856 if (cnp->cn_flags & ISWHITEOUT)
857 ncp->nc_flag |= NCF_WHITE;
858 } else if (vp->v_type == VDIR) {
859 if (flag != NCF_ISDOTDOT) {
861 * For this case, the cache entry maps both the
862 * directory name in it and the name ".." for the
863 * directory's parent.
865 if ((n2 = vp->v_cache_dd) != NULL &&
866 (n2->nc_flag & NCF_ISDOTDOT) != 0)
868 vp->v_cache_dd = ncp;
871 vp->v_cache_dd = NULL;
875 * Insert the new namecache entry into the appropriate chain
876 * within the cache entries table.
878 LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
879 if (flag != NCF_ISDOTDOT) {
880 if (LIST_EMPTY(&dvp->v_cache_src)) {
884 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
888 * If the entry is "negative", we place it into the
889 * "negative" cache queue, otherwise, we place it into the
890 * destination vnode's cache entries queue.
893 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
894 SDT_PROBE3(vfs, namecache, enter, done, dvp, nc_get_name(ncp),
897 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",
915 * Name cache initialization, from vfs_init() when we are booting
918 nchinit(void *dummy __unused)
923 cache_zone_small = uma_zcreate("S VFS Cache",
924 sizeof(struct namecache) + CACHE_PATH_CUTOFF + 1,
925 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
926 cache_zone_small_ts = uma_zcreate("STS VFS Cache",
927 sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1,
928 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
929 cache_zone_large = uma_zcreate("L VFS Cache",
930 sizeof(struct namecache) + NAME_MAX + 1,
931 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
932 cache_zone_large_ts = uma_zcreate("LTS VFS Cache",
933 sizeof(struct namecache_ts) + NAME_MAX + 1,
934 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
936 nchashtbl = hashinit(desiredvnodes * 2, M_VFSCACHE, &nchash);
938 numcalls = counter_u64_alloc(M_WAITOK);
939 dothits = counter_u64_alloc(M_WAITOK);
940 dotdothits = counter_u64_alloc(M_WAITOK);
941 numchecks = counter_u64_alloc(M_WAITOK);
942 nummiss = counter_u64_alloc(M_WAITOK);
943 nummisszap = counter_u64_alloc(M_WAITOK);
944 numposzaps = counter_u64_alloc(M_WAITOK);
945 numposhits = counter_u64_alloc(M_WAITOK);
946 numnegzaps = counter_u64_alloc(M_WAITOK);
947 numneghits = counter_u64_alloc(M_WAITOK);
948 numfullpathcalls = counter_u64_alloc(M_WAITOK);
949 numfullpathfail1 = counter_u64_alloc(M_WAITOK);
950 numfullpathfail2 = counter_u64_alloc(M_WAITOK);
951 numfullpathfail4 = counter_u64_alloc(M_WAITOK);
952 numfullpathfound = counter_u64_alloc(M_WAITOK);
954 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL);
957 cache_changesize(int newmaxvnodes)
959 struct nchashhead *new_nchashtbl, *old_nchashtbl;
960 u_long new_nchash, old_nchash;
961 struct namecache *ncp;
965 new_nchashtbl = hashinit(newmaxvnodes * 2, M_VFSCACHE, &new_nchash);
966 /* If same hash table size, nothing to do */
967 if (nchash == new_nchash) {
968 free(new_nchashtbl, M_VFSCACHE);
972 * Move everything from the old hash table to the new table.
973 * None of the namecache entries in the table can be removed
974 * because to do so, they have to be removed from the hash table.
977 old_nchashtbl = nchashtbl;
979 nchashtbl = new_nchashtbl;
981 for (i = 0; i <= old_nchash; i++) {
982 while ((ncp = LIST_FIRST(&old_nchashtbl[i])) != NULL) {
983 hash = fnv_32_buf(nc_get_name(ncp), ncp->nc_nlen,
985 hash = fnv_32_buf(&ncp->nc_dvp, sizeof(ncp->nc_dvp),
987 LIST_REMOVE(ncp, nc_hash);
988 LIST_INSERT_HEAD(NCHHASH(hash), ncp, nc_hash);
992 free(old_nchashtbl, M_VFSCACHE);
996 * Invalidate all entries to a particular vnode.
999 cache_purge(struct vnode *vp)
1002 CTR1(KTR_VFS, "cache_purge(%p)", vp);
1003 SDT_PROBE1(vfs, namecache, purge, done, vp);
1005 while (!LIST_EMPTY(&vp->v_cache_src))
1006 cache_zap(LIST_FIRST(&vp->v_cache_src));
1007 while (!TAILQ_EMPTY(&vp->v_cache_dst))
1008 cache_zap(TAILQ_FIRST(&vp->v_cache_dst));
1009 if (vp->v_cache_dd != NULL) {
1010 KASSERT(vp->v_cache_dd->nc_flag & NCF_ISDOTDOT,
1011 ("lost dotdot link"));
1012 cache_zap(vp->v_cache_dd);
1014 KASSERT(vp->v_cache_dd == NULL, ("incomplete purge"));
1019 * Invalidate all negative entries for a particular directory vnode.
1022 cache_purge_negative(struct vnode *vp)
1024 struct namecache *cp, *ncp;
1026 CTR1(KTR_VFS, "cache_purge_negative(%p)", vp);
1027 SDT_PROBE1(vfs, namecache, purge_negative, done, vp);
1029 LIST_FOREACH_SAFE(cp, &vp->v_cache_src, nc_src, ncp) {
1030 if (cp->nc_vp == NULL)
1037 * Flush all entries referencing a particular filesystem.
1040 cache_purgevfs(struct mount *mp)
1042 struct nchashhead *ncpp;
1043 struct namecache *ncp, *nnp;
1045 /* Scan hash tables for applicable entries */
1046 SDT_PROBE1(vfs, namecache, purgevfs, done, mp);
1048 for (ncpp = &nchashtbl[nchash]; ncpp >= nchashtbl; ncpp--) {
1049 LIST_FOREACH_SAFE(ncp, ncpp, nc_hash, nnp) {
1050 if (ncp->nc_dvp->v_mount == mp)
1058 * Perform canonical checks and cache lookup and pass on to filesystem
1059 * through the vop_cachedlookup only if needed.
1063 vfs_cache_lookup(struct vop_lookup_args *ap)
1067 struct vnode **vpp = ap->a_vpp;
1068 struct componentname *cnp = ap->a_cnp;
1069 struct ucred *cred = cnp->cn_cred;
1070 int flags = cnp->cn_flags;
1071 struct thread *td = cnp->cn_thread;
1076 if (dvp->v_type != VDIR)
1079 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
1080 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
1083 error = VOP_ACCESS(dvp, VEXEC, cred, td);
1087 error = cache_lookup(dvp, vpp, cnp, NULL, NULL);
1089 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
1096 * XXX All of these sysctls would probably be more productive dead.
1098 static int disablecwd;
1099 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0,
1100 "Disable the getcwd syscall");
1102 /* Implementation of the getcwd syscall. */
1104 sys___getcwd(struct thread *td, struct __getcwd_args *uap)
1107 return (kern___getcwd(td, uap->buf, UIO_USERSPACE, uap->buflen,
1112 kern___getcwd(struct thread *td, char *buf, enum uio_seg bufseg, u_int buflen,
1116 struct filedesc *fdp;
1117 struct vnode *cdir, *rdir;
1124 if (buflen > path_max)
1127 tmpbuf = malloc(buflen, M_TEMP, M_WAITOK);
1128 fdp = td->td_proc->p_fd;
1129 FILEDESC_SLOCK(fdp);
1130 cdir = fdp->fd_cdir;
1132 rdir = fdp->fd_rdir;
1134 FILEDESC_SUNLOCK(fdp);
1135 error = vn_fullpath1(td, cdir, rdir, tmpbuf, &bp, buflen);
1140 if (bufseg == UIO_SYSSPACE)
1141 bcopy(bp, buf, strlen(bp) + 1);
1143 error = copyout(bp, buf, strlen(bp) + 1);
1145 if (KTRPOINT(curthread, KTR_NAMEI))
1149 free(tmpbuf, M_TEMP);
1154 * Thus begins the fullpath magic.
1157 static int disablefullpath;
1158 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW, &disablefullpath, 0,
1159 "Disable the vn_fullpath function");
1162 * Retrieve the full filesystem path that correspond to a vnode from the name
1163 * cache (if available)
1166 vn_fullpath(struct thread *td, struct vnode *vn, char **retbuf, char **freebuf)
1169 struct filedesc *fdp;
1173 if (disablefullpath)
1178 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1179 fdp = td->td_proc->p_fd;
1180 FILEDESC_SLOCK(fdp);
1181 rdir = fdp->fd_rdir;
1183 FILEDESC_SUNLOCK(fdp);
1184 error = vn_fullpath1(td, vn, rdir, buf, retbuf, MAXPATHLEN);
1195 * This function is similar to vn_fullpath, but it attempts to lookup the
1196 * pathname relative to the global root mount point. This is required for the
1197 * auditing sub-system, as audited pathnames must be absolute, relative to the
1198 * global root mount point.
1201 vn_fullpath_global(struct thread *td, struct vnode *vn,
1202 char **retbuf, char **freebuf)
1207 if (disablefullpath)
1211 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1212 error = vn_fullpath1(td, vn, rootvnode, buf, retbuf, MAXPATHLEN);
1221 vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, u_int *buflen)
1226 error = vn_vptocnp_locked(vp, cred, buf, buflen);
1233 vn_vptocnp_locked(struct vnode **vp, struct ucred *cred, char *buf,
1237 struct namecache *ncp;
1240 TAILQ_FOREACH(ncp, &((*vp)->v_cache_dst), nc_dst) {
1241 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1245 if (*buflen < ncp->nc_nlen) {
1248 counter_u64_add(numfullpathfail4, 1);
1250 SDT_PROBE3(vfs, namecache, fullpath, return, error,
1254 *buflen -= ncp->nc_nlen;
1255 memcpy(buf + *buflen, nc_get_name(ncp), ncp->nc_nlen);
1256 SDT_PROBE3(vfs, namecache, fullpath, hit, ncp->nc_dvp,
1257 nc_get_name(ncp), vp);
1266 SDT_PROBE1(vfs, namecache, fullpath, miss, vp);
1269 vn_lock(*vp, LK_SHARED | LK_RETRY);
1270 error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen);
1273 counter_u64_add(numfullpathfail2, 1);
1274 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
1280 if (dvp->v_iflag & VI_DOOMED) {
1281 /* forced unmount */
1285 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
1289 * *vp has its use count incremented still.
1296 * The magic behind kern___getcwd() and vn_fullpath().
1299 vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
1300 char *buf, char **retbuf, u_int buflen)
1302 int error, slash_prefixed;
1303 #ifdef KDTRACE_HOOKS
1304 struct vnode *startvp = vp;
1313 SDT_PROBE1(vfs, namecache, fullpath, entry, vp);
1314 counter_u64_add(numfullpathcalls, 1);
1317 if (vp->v_type != VDIR) {
1318 error = vn_vptocnp_locked(&vp, td->td_ucred, buf, &buflen);
1326 buf[--buflen] = '/';
1329 while (vp != rdir && vp != rootvnode) {
1330 if (vp->v_vflag & VV_ROOT) {
1331 if (vp->v_iflag & VI_DOOMED) { /* forced unmount */
1335 SDT_PROBE3(vfs, namecache, fullpath, return,
1339 vp1 = vp->v_mount->mnt_vnodecovered;
1347 if (vp->v_type != VDIR) {
1350 counter_u64_add(numfullpathfail1, 1);
1352 SDT_PROBE3(vfs, namecache, fullpath, return,
1356 error = vn_vptocnp_locked(&vp, td->td_ucred, buf, &buflen);
1363 SDT_PROBE3(vfs, namecache, fullpath, return, error,
1367 buf[--buflen] = '/';
1372 if (!slash_prefixed) {
1376 counter_u64_add(numfullpathfail4, 1);
1377 SDT_PROBE3(vfs, namecache, fullpath, return, ENOMEM,
1381 buf[--buflen] = '/';
1383 counter_u64_add(numfullpathfound, 1);
1387 SDT_PROBE3(vfs, namecache, fullpath, return, 0, startvp, buf + buflen);
1388 *retbuf = buf + buflen;
1393 vn_dir_dd_ino(struct vnode *vp)
1395 struct namecache *ncp;
1398 ASSERT_VOP_LOCKED(vp, "vn_dir_dd_ino");
1400 TAILQ_FOREACH(ncp, &(vp->v_cache_dst), nc_dst) {
1401 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0)
1406 if (vget(ddvp, LK_SHARED | LK_NOWAIT | LK_VNHELD, curthread))
1415 vn_commname(struct vnode *vp, char *buf, u_int buflen)
1417 struct namecache *ncp;
1421 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst)
1422 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1428 l = min(ncp->nc_nlen, buflen - 1);
1429 memcpy(buf, nc_get_name(ncp), l);
1435 /* ABI compat shims for old kernel modules. */
1438 void cache_enter(struct vnode *dvp, struct vnode *vp,
1439 struct componentname *cnp);
1442 cache_enter(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
1445 cache_enter_time(dvp, vp, cnp, NULL, NULL);
1449 * This function updates path string to vnode's full global path
1450 * and checks the size of the new path string against the pathlen argument.
1452 * Requires a locked, referenced vnode.
1453 * Vnode is re-locked on success or ENODEV, otherwise unlocked.
1455 * If sysctl debug.disablefullpath is set, ENODEV is returned,
1456 * vnode is left locked and path remain untouched.
1458 * If vp is a directory, the call to vn_fullpath_global() always succeeds
1459 * because it falls back to the ".." lookup if the namecache lookup fails.
1462 vn_path_to_global_path(struct thread *td, struct vnode *vp, char *path,
1465 struct nameidata nd;
1470 ASSERT_VOP_ELOCKED(vp, __func__);
1472 /* Return ENODEV if sysctl debug.disablefullpath==1 */
1473 if (disablefullpath)
1476 /* Construct global filesystem path from vp. */
1478 error = vn_fullpath_global(td, vp, &rpath, &fbuf);
1485 if (strlen(rpath) >= pathlen) {
1487 error = ENAMETOOLONG;
1492 * Re-lookup the vnode by path to detect a possible rename.
1493 * As a side effect, the vnode is relocked.
1494 * If vnode was renamed, return ENOENT.
1496 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
1497 UIO_SYSSPACE, path, td);
1503 NDFREE(&nd, NDF_ONLY_PNBUF);
1507 strcpy(path, rpath);