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_kdtrace.h"
39 #include "opt_ktrace.h"
41 #include <sys/param.h>
42 #include <sys/systm.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/mount.h>
49 #include <sys/namei.h>
51 #include <sys/rwlock.h>
53 #include <sys/syscallsubr.h>
54 #include <sys/sysctl.h>
55 #include <sys/sysproto.h>
56 #include <sys/vnode.h>
58 #include <sys/ktrace.h>
63 SDT_PROVIDER_DECLARE(vfs);
64 SDT_PROBE_DEFINE3(vfs, namecache, enter, done, done, "struct vnode *", "char *",
66 SDT_PROBE_DEFINE2(vfs, namecache, enter_negative, done, done, "struct vnode *",
68 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, entry, entry, "struct vnode *");
69 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, hit, hit, "struct vnode *",
70 "char *", "struct vnode *");
71 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, miss, miss, "struct vnode *");
72 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, return, return, "int",
73 "struct vnode *", "char *");
74 SDT_PROBE_DEFINE3(vfs, namecache, lookup, hit, hit, "struct vnode *", "char *",
76 SDT_PROBE_DEFINE2(vfs, namecache, lookup, hit_negative, hit-negative,
77 "struct vnode *", "char *");
78 SDT_PROBE_DEFINE2(vfs, namecache, lookup, miss, miss, "struct vnode *",
80 SDT_PROBE_DEFINE1(vfs, namecache, purge, done, done, "struct vnode *");
81 SDT_PROBE_DEFINE1(vfs, namecache, purge_negative, done, done, "struct vnode *");
82 SDT_PROBE_DEFINE1(vfs, namecache, purgevfs, done, done, "struct mount *");
83 SDT_PROBE_DEFINE3(vfs, namecache, zap, done, done, "struct vnode *", "char *",
85 SDT_PROBE_DEFINE2(vfs, namecache, zap_negative, done, done, "struct vnode *",
89 * This structure describes the elements in the cache of recent
90 * names looked up by namei.
94 LIST_ENTRY(namecache) nc_hash; /* hash chain */
95 LIST_ENTRY(namecache) nc_src; /* source vnode list */
96 TAILQ_ENTRY(namecache) nc_dst; /* destination vnode list */
97 struct vnode *nc_dvp; /* vnode of parent of name */
98 struct vnode *nc_vp; /* vnode the name refers to */
99 u_char nc_flag; /* flag bits */
100 u_char nc_nlen; /* length of name */
101 char nc_name[0]; /* segment name + nul */
105 * struct namecache_ts repeats struct namecache layout up to the
107 * struct namecache_ts is used in place of struct namecache when time(s) need
108 * to be stored. The nc_dotdottime field is used when a cache entry is mapping
109 * both a non-dotdot directory name plus dotdot for the directory's
112 struct namecache_ts {
113 LIST_ENTRY(namecache) nc_hash; /* hash chain */
114 LIST_ENTRY(namecache) nc_src; /* source vnode list */
115 TAILQ_ENTRY(namecache) nc_dst; /* destination vnode list */
116 struct vnode *nc_dvp; /* vnode of parent of name */
117 struct vnode *nc_vp; /* vnode the name refers to */
118 u_char nc_flag; /* flag bits */
119 u_char nc_nlen; /* length of name */
120 struct timespec nc_time; /* timespec provided by fs */
121 struct timespec nc_dotdottime; /* dotdot timespec provided by fs */
122 int nc_ticks; /* ticks value when entry was added */
123 char nc_name[0]; /* segment name + nul */
127 * Flags in namecache.nc_flag
129 #define NCF_WHITE 0x01
130 #define NCF_ISDOTDOT 0x02
135 * Name caching works as follows:
137 * Names found by directory scans are retained in a cache
138 * for future reference. It is managed LRU, so frequently
139 * used names will hang around. Cache is indexed by hash value
140 * obtained from (vp, name) where vp refers to the directory
143 * If it is a "negative" entry, (i.e. for a name that is known NOT to
144 * exist) the vnode pointer will be NULL.
146 * Upon reaching the last segment of a path, if the reference
147 * is for DELETE, or NOCACHE is set (rewrite), and the
148 * name is located in the cache, it will be dropped.
152 * Structures associated with name cacheing.
154 #define NCHHASH(hash) \
155 (&nchashtbl[(hash) & nchash])
156 static LIST_HEAD(nchashhead, namecache) *nchashtbl; /* Hash Table */
157 static TAILQ_HEAD(, namecache) ncneg; /* Hash Table */
158 static u_long nchash; /* size of hash table */
159 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0,
160 "Size of namecache hash table");
161 static u_long ncnegfactor = 16; /* ratio of negative entries */
162 /* _debug sysctl left for backward compatibility */
163 SYSCTL_ULONG(_debug, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0, "");
164 SYSCTL_ULONG(_vfs, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0,
165 "Ratio of negative namecache entries");
166 static u_long numneg; /* number of negative entries allocated */
167 SYSCTL_ULONG(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0,
168 "Number of negative entries in namecache");
169 static u_long numcache; /* number of cache entries allocated */
170 SYSCTL_ULONG(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0,
171 "Number of namecache entries");
172 static u_long numcachehv; /* number of cache entries with vnodes held */
173 SYSCTL_ULONG(_debug, OID_AUTO, numcachehv, CTLFLAG_RD, &numcachehv, 0,
174 "Number of namecache entries with vnodes held");
175 static u_int ncsizefactor = 2;
176 SYSCTL_UINT(_vfs, OID_AUTO, ncsizefactor, CTLFLAG_RW, &ncsizefactor, 0,
177 "Size factor for namecache");
179 struct nchstats nchstats; /* cache effectiveness statistics */
181 static struct rwlock cache_lock;
182 RW_SYSINIT(vfscache, &cache_lock, "Name Cache");
184 #define CACHE_UPGRADE_LOCK() rw_try_upgrade(&cache_lock)
185 #define CACHE_RLOCK() rw_rlock(&cache_lock)
186 #define CACHE_RUNLOCK() rw_runlock(&cache_lock)
187 #define CACHE_WLOCK() rw_wlock(&cache_lock)
188 #define CACHE_WUNLOCK() rw_wunlock(&cache_lock)
191 * UMA zones for the VFS cache.
193 * The small cache is used for entries with short names, which are the
194 * most common. The large cache is used for entries which are too big to
195 * fit in the small cache.
197 static uma_zone_t cache_zone_small;
198 static uma_zone_t cache_zone_small_ts;
199 static uma_zone_t cache_zone_large;
200 static uma_zone_t cache_zone_large_ts;
202 #define CACHE_PATH_CUTOFF 35
204 static struct namecache *
205 cache_alloc(int len, int ts)
208 if (len > CACHE_PATH_CUTOFF) {
210 return (uma_zalloc(cache_zone_large_ts, M_WAITOK));
212 return (uma_zalloc(cache_zone_large, M_WAITOK));
215 return (uma_zalloc(cache_zone_small_ts, M_WAITOK));
217 return (uma_zalloc(cache_zone_small, M_WAITOK));
221 cache_free(struct namecache *ncp)
227 ts = ncp->nc_flag & NCF_TS;
228 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF) {
230 uma_zfree(cache_zone_small_ts, ncp);
232 uma_zfree(cache_zone_small, ncp);
234 uma_zfree(cache_zone_large_ts, ncp);
236 uma_zfree(cache_zone_large, ncp);
240 nc_get_name(struct namecache *ncp)
242 struct namecache_ts *ncp_ts;
244 if ((ncp->nc_flag & NCF_TS) == 0)
245 return (ncp->nc_name);
246 ncp_ts = (struct namecache_ts *)ncp;
247 return (ncp_ts->nc_name);
251 cache_out_ts(struct namecache *ncp, struct timespec *tsp, int *ticksp)
254 KASSERT((ncp->nc_flag & NCF_TS) != 0 ||
255 (tsp == NULL && ticksp == NULL),
259 *tsp = ((struct namecache_ts *)ncp)->nc_time;
261 *ticksp = ((struct namecache_ts *)ncp)->nc_ticks;
264 static int doingcache = 1; /* 1 => enable the cache */
265 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0,
266 "VFS namecache enabled");
268 /* Export size information to userland */
269 SYSCTL_INT(_debug_sizeof, OID_AUTO, namecache, CTLFLAG_RD, 0,
270 sizeof(struct namecache), "sizeof(struct namecache)");
273 * The new name cache statistics
275 static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0,
276 "Name cache statistics");
277 #define STATNODE(mode, name, var, descr) \
278 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, mode, var, 0, descr);
279 STATNODE(CTLFLAG_RD, numneg, &numneg, "Number of negative cache entries");
280 STATNODE(CTLFLAG_RD, numcache, &numcache, "Number of cache entries");
281 static u_long numcalls; STATNODE(CTLFLAG_RD, numcalls, &numcalls,
282 "Number of cache lookups");
283 static u_long dothits; STATNODE(CTLFLAG_RD, dothits, &dothits,
284 "Number of '.' hits");
285 static u_long dotdothits; STATNODE(CTLFLAG_RD, dotdothits, &dotdothits,
286 "Number of '..' hits");
287 static u_long numchecks; STATNODE(CTLFLAG_RD, numchecks, &numchecks,
288 "Number of checks in lookup");
289 static u_long nummiss; STATNODE(CTLFLAG_RD, nummiss, &nummiss,
290 "Number of cache misses");
291 static u_long nummisszap; STATNODE(CTLFLAG_RD, nummisszap, &nummisszap,
292 "Number of cache misses we do not want to cache");
293 static u_long numposzaps; STATNODE(CTLFLAG_RD, numposzaps, &numposzaps,
294 "Number of cache hits (positive) we do not want to cache");
295 static u_long numposhits; STATNODE(CTLFLAG_RD, numposhits, &numposhits,
296 "Number of cache hits (positive)");
297 static u_long numnegzaps; STATNODE(CTLFLAG_RD, numnegzaps, &numnegzaps,
298 "Number of cache hits (negative) we do not want to cache");
299 static u_long numneghits; STATNODE(CTLFLAG_RD, numneghits, &numneghits,
300 "Number of cache hits (negative)");
301 static u_long numupgrades; STATNODE(CTLFLAG_RD, numupgrades, &numupgrades,
302 "Number of updates of the cache after lookup (write lock + retry)");
304 SYSCTL_OPAQUE(_vfs_cache, OID_AUTO, nchstats, CTLFLAG_RD | CTLFLAG_MPSAFE,
305 &nchstats, sizeof(nchstats), "LU",
306 "VFS cache effectiveness statistics");
310 static void cache_zap(struct namecache *ncp);
311 static int vn_vptocnp_locked(struct vnode **vp, struct ucred *cred, char *buf,
313 static int vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
314 char *buf, char **retbuf, u_int buflen);
316 static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
320 * Grab an atomic snapshot of the name cache hash chain lengths
322 SYSCTL_NODE(_debug, OID_AUTO, hashstat, CTLFLAG_RW, NULL, "hash table stats");
325 sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS)
328 struct nchashhead *ncpp;
329 struct namecache *ncp;
333 n_nchash = nchash + 1; /* nchash is max index, not count */
335 return SYSCTL_OUT(req, 0, n_nchash * sizeof(int));
337 /* Scan hash tables for applicable entries */
338 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
341 LIST_FOREACH(ncp, ncpp, nc_hash) {
345 error = SYSCTL_OUT(req, &count, sizeof(count));
351 SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD|
352 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_rawnchash, "S,int",
353 "nchash chain lengths");
356 sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS)
359 struct nchashhead *ncpp;
360 struct namecache *ncp;
362 int count, maxlength, used, pct;
365 return SYSCTL_OUT(req, 0, 4 * sizeof(int));
367 n_nchash = nchash + 1; /* nchash is max index, not count */
371 /* Scan hash tables for applicable entries */
372 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
375 LIST_FOREACH(ncp, ncpp, nc_hash) {
381 if (maxlength < count)
384 n_nchash = nchash + 1;
385 pct = (used * 100 * 100) / n_nchash;
386 error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash));
389 error = SYSCTL_OUT(req, &used, sizeof(used));
392 error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength));
395 error = SYSCTL_OUT(req, &pct, sizeof(pct));
400 SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD|
401 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_nchash, "I",
402 "nchash chain lengths");
408 * Removes a namecache entry from cache, whether it contains an actual
409 * pointer to a vnode or if it is just a negative cache entry.
413 struct namecache *ncp;
417 rw_assert(&cache_lock, RA_WLOCKED);
418 CTR2(KTR_VFS, "cache_zap(%p) vp %p", ncp, ncp->nc_vp);
420 if (ncp->nc_vp != NULL) {
421 SDT_PROBE(vfs, namecache, zap, done, ncp->nc_dvp,
422 nc_get_name(ncp), ncp->nc_vp, 0, 0);
424 SDT_PROBE(vfs, namecache, zap_negative, done, ncp->nc_dvp,
425 nc_get_name(ncp), 0, 0, 0);
429 LIST_REMOVE(ncp, nc_hash);
430 if (ncp->nc_flag & NCF_ISDOTDOT) {
431 if (ncp == ncp->nc_dvp->v_cache_dd)
432 ncp->nc_dvp->v_cache_dd = NULL;
434 LIST_REMOVE(ncp, nc_src);
435 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) {
441 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
442 if (ncp == ncp->nc_vp->v_cache_dd)
443 ncp->nc_vp->v_cache_dd = NULL;
445 TAILQ_REMOVE(&ncneg, ncp, nc_dst);
455 * Lookup an entry in the cache
457 * Lookup is called with dvp pointing to the directory to search,
458 * cnp pointing to the name of the entry being sought. If the lookup
459 * succeeds, the vnode is returned in *vpp, and a status of -1 is
460 * returned. If the lookup determines that the name does not exist
461 * (negative cacheing), a status of ENOENT is returned. If the lookup
462 * fails, a status of zero is returned. If the directory vnode is
463 * recycled out from under us due to a forced unmount, a status of
464 * ENOENT is returned.
466 * vpp is locked and ref'd on return. If we're looking up DOTDOT, dvp is
467 * unlocked. If we're looking up . an extra ref is taken, but the lock is
468 * not recursively acquired.
472 cache_lookup_times(dvp, vpp, cnp, tsp, ticksp)
475 struct componentname *cnp;
476 struct timespec *tsp;
479 struct namecache *ncp;
481 int error, ltype, wlocked;
484 cnp->cn_flags &= ~MAKEENTRY;
494 if (cnp->cn_nameptr[0] == '.') {
495 if (cnp->cn_namelen == 1) {
497 CTR2(KTR_VFS, "cache_lookup(%p, %s) found via .",
498 dvp, cnp->cn_nameptr);
500 SDT_PROBE(vfs, namecache, lookup, hit, dvp, ".",
508 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
510 if (dvp->v_cache_dd == NULL) {
511 SDT_PROBE(vfs, namecache, lookup, miss, dvp,
515 if ((cnp->cn_flags & MAKEENTRY) == 0) {
516 if (!wlocked && !CACHE_UPGRADE_LOCK())
518 if (dvp->v_cache_dd->nc_flag & NCF_ISDOTDOT)
519 cache_zap(dvp->v_cache_dd);
520 dvp->v_cache_dd = NULL;
524 ncp = dvp->v_cache_dd;
525 if (ncp->nc_flag & NCF_ISDOTDOT)
529 /* Return failure if negative entry was found. */
531 goto negative_success;
532 CTR3(KTR_VFS, "cache_lookup(%p, %s) found %p via ..",
533 dvp, cnp->cn_nameptr, *vpp);
534 SDT_PROBE(vfs, namecache, lookup, hit, dvp, "..",
536 cache_out_ts(ncp, tsp, ticksp);
537 if ((ncp->nc_flag & (NCF_ISDOTDOT | NCF_DTS)) ==
538 NCF_DTS && tsp != NULL)
539 *tsp = ((struct namecache_ts *)ncp)->
545 hash = fnv_32_buf(cnp->cn_nameptr, cnp->cn_namelen, FNV1_32_INIT);
546 hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
547 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
549 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
550 !bcmp(nc_get_name(ncp), cnp->cn_nameptr, ncp->nc_nlen))
554 /* We failed to find an entry */
556 SDT_PROBE(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
558 if ((cnp->cn_flags & MAKEENTRY) == 0) {
567 /* We don't want to have an entry, so dump it */
568 if ((cnp->cn_flags & MAKEENTRY) == 0) {
570 nchstats.ncs_badhits++;
571 if (!wlocked && !CACHE_UPGRADE_LOCK())
578 /* We found a "positive" match, return the vnode */
581 nchstats.ncs_goodhits++;
583 CTR4(KTR_VFS, "cache_lookup(%p, %s) found %p via ncp %p",
584 dvp, cnp->cn_nameptr, *vpp, ncp);
585 SDT_PROBE(vfs, namecache, lookup, hit, dvp, nc_get_name(ncp),
587 cache_out_ts(ncp, tsp, ticksp);
592 /* We found a negative match, and want to create it, so purge */
593 if (cnp->cn_nameiop == CREATE) {
595 nchstats.ncs_badhits++;
596 if (!wlocked && !CACHE_UPGRADE_LOCK())
603 if (!wlocked && !CACHE_UPGRADE_LOCK())
607 * We found a "negative" match, so we shift it to the end of
608 * the "negative" cache entries queue to satisfy LRU. Also,
609 * check to see if the entry is a whiteout; indicate this to
610 * the componentname, if so.
612 TAILQ_REMOVE(&ncneg, ncp, nc_dst);
613 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
614 nchstats.ncs_neghits++;
615 if (ncp->nc_flag & NCF_WHITE)
616 cnp->cn_flags |= ISWHITEOUT;
617 SDT_PROBE(vfs, namecache, lookup, hit_negative, dvp, nc_get_name(ncp),
619 cache_out_ts(ncp, tsp, ticksp);
625 * We need to update the cache after our lookup, so upgrade to
626 * a write lock and retry the operation.
636 * On success we return a locked and ref'd vnode as per the lookup
639 if (dvp == *vpp) { /* lookup on "." */
646 * When we lookup "." we still can be asked to lock it
649 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
650 if (ltype != VOP_ISLOCKED(*vpp)) {
651 if (ltype == LK_EXCLUSIVE) {
652 vn_lock(*vpp, LK_UPGRADE | LK_RETRY);
653 if ((*vpp)->v_iflag & VI_DOOMED) {
660 vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY);
664 ltype = 0; /* silence gcc warning */
665 if (cnp->cn_flags & ISDOTDOT) {
666 ltype = VOP_ISLOCKED(dvp);
674 error = vget(*vpp, cnp->cn_lkflags | LK_INTERLOCK, cnp->cn_thread);
675 if (cnp->cn_flags & ISDOTDOT) {
676 vn_lock(dvp, ltype | LK_RETRY);
677 if (dvp->v_iflag & VI_DOOMED) {
688 if ((cnp->cn_flags & ISLASTCN) &&
689 (cnp->cn_lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE) {
690 ASSERT_VOP_ELOCKED(*vpp, "cache_lookup");
703 * Add an entry to the cache.
706 cache_enter_time(dvp, vp, cnp, tsp, dtsp)
709 struct componentname *cnp;
710 struct timespec *tsp;
711 struct timespec *dtsp;
713 struct namecache *ncp, *n2;
714 struct namecache_ts *n3;
715 struct nchashhead *ncpp;
722 CTR3(KTR_VFS, "cache_enter(%p, %p, %s)", dvp, vp, cnp->cn_nameptr);
723 VNASSERT(vp == NULL || (vp->v_iflag & VI_DOOMED) == 0, vp,
724 ("cache_enter: Adding a doomed vnode"));
725 VNASSERT(dvp == NULL || (dvp->v_iflag & VI_DOOMED) == 0, dvp,
726 ("cache_enter: Doomed vnode used as src"));
732 * Avoid blowout in namecache entries.
734 if (numcache >= desiredvnodes * ncsizefactor)
738 if (cnp->cn_nameptr[0] == '.') {
739 if (cnp->cn_namelen == 1)
741 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
744 * If dotdot entry already exists, just retarget it
745 * to new parent vnode, otherwise continue with new
746 * namecache entry allocation.
748 if ((ncp = dvp->v_cache_dd) != NULL &&
749 ncp->nc_flag & NCF_ISDOTDOT) {
750 KASSERT(ncp->nc_dvp == dvp,
751 ("wrong isdotdot parent"));
752 if (ncp->nc_vp != NULL)
753 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst,
756 TAILQ_REMOVE(&ncneg, ncp, nc_dst);
758 TAILQ_INSERT_HEAD(&vp->v_cache_dst,
761 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
766 dvp->v_cache_dd = NULL;
767 SDT_PROBE(vfs, namecache, enter, done, dvp, "..", vp,
778 * Calculate the hash key and setup as much of the new
779 * namecache entry as possible before acquiring the lock.
781 ncp = cache_alloc(cnp->cn_namelen, tsp != NULL);
786 n3 = (struct namecache_ts *)ncp;
788 n3->nc_ticks = ticks;
789 n3->nc_flag |= NCF_TS;
791 n3->nc_dotdottime = *dtsp;
792 n3->nc_flag |= NCF_DTS;
795 len = ncp->nc_nlen = cnp->cn_namelen;
796 hash = fnv_32_buf(cnp->cn_nameptr, len, FNV1_32_INIT);
797 strlcpy(nc_get_name(ncp), cnp->cn_nameptr, len + 1);
798 hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
802 * See if this vnode or negative entry is already in the cache
803 * with this name. This can happen with concurrent lookups of
804 * the same path name.
806 ncpp = NCHHASH(hash);
807 LIST_FOREACH(n2, ncpp, nc_hash) {
808 if (n2->nc_dvp == dvp &&
809 n2->nc_nlen == cnp->cn_namelen &&
810 !bcmp(nc_get_name(n2), cnp->cn_nameptr, n2->nc_nlen)) {
812 KASSERT((n2->nc_flag & NCF_TS) != 0,
814 n3 = (struct namecache_ts *)n2;
816 ((struct namecache_ts *)ncp)->nc_time;
818 ((struct namecache_ts *)ncp)->nc_ticks;
821 ((struct namecache_ts *)ncp)->
823 n3->nc_flag |= NCF_DTS;
832 if (flag == NCF_ISDOTDOT) {
834 * See if we are trying to add .. entry, but some other lookup
835 * has populated v_cache_dd pointer already.
837 if (dvp->v_cache_dd != NULL) {
842 KASSERT(vp == NULL || vp->v_type == VDIR,
843 ("wrong vnode type %p", vp));
844 dvp->v_cache_dd = ncp;
850 if (cnp->cn_flags & ISWHITEOUT)
851 ncp->nc_flag |= NCF_WHITE;
852 } else if (vp->v_type == VDIR) {
853 if (flag != NCF_ISDOTDOT) {
855 * For this case, the cache entry maps both the
856 * directory name in it and the name ".." for the
857 * directory's parent.
859 if ((n2 = vp->v_cache_dd) != NULL &&
860 (n2->nc_flag & NCF_ISDOTDOT) != 0)
862 vp->v_cache_dd = ncp;
865 vp->v_cache_dd = NULL;
869 * Insert the new namecache entry into the appropriate chain
870 * within the cache entries table.
872 LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
873 if (flag != NCF_ISDOTDOT) {
874 if (LIST_EMPTY(&dvp->v_cache_src)) {
878 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
882 * If the entry is "negative", we place it into the
883 * "negative" cache queue, otherwise, we place it into the
884 * destination vnode's cache entries queue.
887 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
888 SDT_PROBE(vfs, namecache, enter, done, dvp, nc_get_name(ncp),
891 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
892 SDT_PROBE(vfs, namecache, enter_negative, done, dvp,
893 nc_get_name(ncp), 0, 0, 0);
895 if (numneg * ncnegfactor > numcache) {
896 ncp = TAILQ_FIRST(&ncneg);
907 * Name cache initialization, from vfs_init() when we are booting
910 nchinit(void *dummy __unused)
915 cache_zone_small = uma_zcreate("S VFS Cache",
916 sizeof(struct namecache) + CACHE_PATH_CUTOFF + 1,
917 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
918 cache_zone_small_ts = uma_zcreate("STS VFS Cache",
919 sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1,
920 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
921 cache_zone_large = uma_zcreate("L VFS Cache",
922 sizeof(struct namecache) + NAME_MAX + 1,
923 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
924 cache_zone_large_ts = uma_zcreate("LTS VFS Cache",
925 sizeof(struct namecache_ts) + NAME_MAX + 1,
926 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
928 nchashtbl = hashinit(desiredvnodes * 2, M_VFSCACHE, &nchash);
930 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL);
934 * Invalidate all entries to a particular vnode.
941 CTR1(KTR_VFS, "cache_purge(%p)", vp);
942 SDT_PROBE(vfs, namecache, purge, done, vp, 0, 0, 0, 0);
944 while (!LIST_EMPTY(&vp->v_cache_src))
945 cache_zap(LIST_FIRST(&vp->v_cache_src));
946 while (!TAILQ_EMPTY(&vp->v_cache_dst))
947 cache_zap(TAILQ_FIRST(&vp->v_cache_dst));
948 if (vp->v_cache_dd != NULL) {
949 KASSERT(vp->v_cache_dd->nc_flag & NCF_ISDOTDOT,
950 ("lost dotdot link"));
951 cache_zap(vp->v_cache_dd);
953 KASSERT(vp->v_cache_dd == NULL, ("incomplete purge"));
958 * Invalidate all negative entries for a particular directory vnode.
961 cache_purge_negative(vp)
964 struct namecache *cp, *ncp;
966 CTR1(KTR_VFS, "cache_purge_negative(%p)", vp);
967 SDT_PROBE(vfs, namecache, purge_negative, done, vp, 0, 0, 0, 0);
969 LIST_FOREACH_SAFE(cp, &vp->v_cache_src, nc_src, ncp) {
970 if (cp->nc_vp == NULL)
977 * Flush all entries referencing a particular filesystem.
983 struct nchashhead *ncpp;
984 struct namecache *ncp, *nnp;
986 /* Scan hash tables for applicable entries */
987 SDT_PROBE(vfs, namecache, purgevfs, done, mp, 0, 0, 0, 0);
989 for (ncpp = &nchashtbl[nchash]; ncpp >= nchashtbl; ncpp--) {
990 LIST_FOREACH_SAFE(ncp, ncpp, nc_hash, nnp) {
991 if (ncp->nc_dvp->v_mount == mp)
999 * Perform canonical checks and cache lookup and pass on to filesystem
1000 * through the vop_cachedlookup only if needed.
1004 vfs_cache_lookup(ap)
1005 struct vop_lookup_args /* {
1006 struct vnode *a_dvp;
1007 struct vnode **a_vpp;
1008 struct componentname *a_cnp;
1013 struct vnode **vpp = ap->a_vpp;
1014 struct componentname *cnp = ap->a_cnp;
1015 struct ucred *cred = cnp->cn_cred;
1016 int flags = cnp->cn_flags;
1017 struct thread *td = cnp->cn_thread;
1022 if (dvp->v_type != VDIR)
1025 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
1026 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
1029 error = VOP_ACCESS(dvp, VEXEC, cred, td);
1033 error = cache_lookup(dvp, vpp, cnp);
1035 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
1042 #ifndef _SYS_SYSPROTO_H_
1043 struct __getcwd_args {
1050 * XXX All of these sysctls would probably be more productive dead.
1052 static int disablecwd;
1053 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0,
1054 "Disable the getcwd syscall");
1056 /* Implementation of the getcwd syscall. */
1060 struct __getcwd_args *uap;
1063 return (kern___getcwd(td, uap->buf, UIO_USERSPACE, uap->buflen));
1067 kern___getcwd(struct thread *td, u_char *buf, enum uio_seg bufseg, u_int buflen)
1070 struct filedesc *fdp;
1071 struct vnode *cdir, *rdir;
1072 int error, vfslocked;
1078 if (buflen > MAXPATHLEN)
1079 buflen = MAXPATHLEN;
1081 tmpbuf = malloc(buflen, M_TEMP, M_WAITOK);
1082 fdp = td->td_proc->p_fd;
1083 FILEDESC_SLOCK(fdp);
1084 cdir = fdp->fd_cdir;
1086 rdir = fdp->fd_rdir;
1088 FILEDESC_SUNLOCK(fdp);
1089 error = vn_fullpath1(td, cdir, rdir, tmpbuf, &bp, buflen);
1090 vfslocked = VFS_LOCK_GIANT(rdir->v_mount);
1092 VFS_UNLOCK_GIANT(vfslocked);
1093 vfslocked = VFS_LOCK_GIANT(cdir->v_mount);
1095 VFS_UNLOCK_GIANT(vfslocked);
1098 if (bufseg == UIO_SYSSPACE)
1099 bcopy(bp, buf, strlen(bp) + 1);
1101 error = copyout(bp, buf, strlen(bp) + 1);
1103 if (KTRPOINT(curthread, KTR_NAMEI))
1107 free(tmpbuf, M_TEMP);
1112 * Thus begins the fullpath magic.
1116 #define STATNODE(name, descr) \
1117 static u_int name; \
1118 SYSCTL_UINT(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, descr)
1120 static int disablefullpath;
1121 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW, &disablefullpath, 0,
1122 "Disable the vn_fullpath function");
1124 /* These count for kern___getcwd(), too. */
1125 STATNODE(numfullpathcalls, "Number of fullpath search calls");
1126 STATNODE(numfullpathfail1, "Number of fullpath search errors (ENOTDIR)");
1127 STATNODE(numfullpathfail2,
1128 "Number of fullpath search errors (VOP_VPTOCNP failures)");
1129 STATNODE(numfullpathfail4, "Number of fullpath search errors (ENOMEM)");
1130 STATNODE(numfullpathfound, "Number of successful fullpath calls");
1133 * Retrieve the full filesystem path that correspond to a vnode from the name
1134 * cache (if available)
1137 vn_fullpath(struct thread *td, struct vnode *vn, char **retbuf, char **freebuf)
1140 struct filedesc *fdp;
1142 int error, vfslocked;
1144 if (disablefullpath)
1149 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1150 fdp = td->td_proc->p_fd;
1151 FILEDESC_SLOCK(fdp);
1152 rdir = fdp->fd_rdir;
1154 FILEDESC_SUNLOCK(fdp);
1155 error = vn_fullpath1(td, vn, rdir, buf, retbuf, MAXPATHLEN);
1156 vfslocked = VFS_LOCK_GIANT(rdir->v_mount);
1158 VFS_UNLOCK_GIANT(vfslocked);
1168 * This function is similar to vn_fullpath, but it attempts to lookup the
1169 * pathname relative to the global root mount point. This is required for the
1170 * auditing sub-system, as audited pathnames must be absolute, relative to the
1171 * global root mount point.
1174 vn_fullpath_global(struct thread *td, struct vnode *vn,
1175 char **retbuf, char **freebuf)
1180 if (disablefullpath)
1184 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1185 error = vn_fullpath1(td, vn, rootvnode, buf, retbuf, MAXPATHLEN);
1194 vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, u_int *buflen)
1199 error = vn_vptocnp_locked(vp, cred, buf, buflen);
1202 * vn_vptocnp_locked() dropped hold acquired by
1203 * VOP_VPTOCNP immediately after locking the
1204 * cache. Since we are going to drop the cache rlock,
1205 * re-hold the result.
1214 vn_vptocnp_locked(struct vnode **vp, struct ucred *cred, char *buf,
1218 struct namecache *ncp;
1219 int error, vfslocked;
1221 TAILQ_FOREACH(ncp, &((*vp)->v_cache_dst), nc_dst) {
1222 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1226 if (*buflen < ncp->nc_nlen) {
1230 SDT_PROBE(vfs, namecache, fullpath, return, error,
1234 *buflen -= ncp->nc_nlen;
1235 memcpy(buf + *buflen, nc_get_name(ncp), ncp->nc_nlen);
1236 SDT_PROBE(vfs, namecache, fullpath, hit, ncp->nc_dvp,
1237 nc_get_name(ncp), vp, 0, 0);
1241 SDT_PROBE(vfs, namecache, fullpath, miss, vp, 0, 0, 0, 0);
1245 vfslocked = VFS_LOCK_GIANT((*vp)->v_mount);
1246 vn_lock(*vp, LK_SHARED | LK_RETRY);
1247 error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen);
1250 VFS_UNLOCK_GIANT(vfslocked);
1253 SDT_PROBE(vfs, namecache, fullpath, return, error, vp,
1260 if ((*vp)->v_iflag & VI_DOOMED) {
1261 /* forced unmount */
1265 SDT_PROBE(vfs, namecache, fullpath, return, error, vp,
1275 * The magic behind kern___getcwd() and vn_fullpath().
1278 vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
1279 char *buf, char **retbuf, u_int buflen)
1281 int error, slash_prefixed;
1282 #ifdef KDTRACE_HOOKS
1283 struct vnode *startvp = vp;
1291 SDT_PROBE(vfs, namecache, fullpath, entry, vp, 0, 0, 0, 0);
1294 if (vp->v_type != VDIR) {
1295 error = vn_vptocnp_locked(&vp, td->td_ucred, buf, &buflen);
1302 buf[--buflen] = '/';
1305 while (vp != rdir && vp != rootvnode) {
1306 if (vp->v_vflag & VV_ROOT) {
1307 if (vp->v_iflag & VI_DOOMED) { /* forced unmount */
1310 SDT_PROBE(vfs, namecache, fullpath, return,
1311 error, vp, NULL, 0, 0);
1314 vp = vp->v_mount->mnt_vnodecovered;
1317 if (vp->v_type != VDIR) {
1321 SDT_PROBE(vfs, namecache, fullpath, return,
1322 error, vp, NULL, 0, 0);
1325 error = vn_vptocnp_locked(&vp, td->td_ucred, buf, &buflen);
1331 SDT_PROBE(vfs, namecache, fullpath, return, error,
1332 startvp, NULL, 0, 0);
1335 buf[--buflen] = '/';
1340 if (!slash_prefixed) {
1344 SDT_PROBE(vfs, namecache, fullpath, return, ENOMEM,
1345 startvp, NULL, 0, 0);
1348 buf[--buflen] = '/';
1353 SDT_PROBE(vfs, namecache, fullpath, return, 0, startvp, buf + buflen,
1355 *retbuf = buf + buflen;
1360 vn_commname(struct vnode *vp, char *buf, u_int buflen)
1362 struct namecache *ncp;
1366 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst)
1367 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1373 l = min(ncp->nc_nlen, buflen - 1);
1374 memcpy(buf, nc_get_name(ncp), l);
1380 /* ABI compat shims for old kernel modules. */
1384 void cache_enter(struct vnode *dvp, struct vnode *vp,
1385 struct componentname *cnp);
1386 int cache_lookup(struct vnode *dvp, struct vnode **vpp,
1387 struct componentname *cnp);
1390 cache_enter(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
1393 cache_enter_time(dvp, vp, cnp, NULL, NULL);
1397 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
1400 return (cache_lookup_times(dvp, vpp, cnp, NULL, NULL));