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/filedesc.h>
43 #include <sys/fnv_hash.h>
44 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/fcntl.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, "struct vnode *", "char *",
66 SDT_PROBE_DEFINE2(vfs, namecache, enter_negative, done, "struct vnode *",
68 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, entry, "struct vnode *");
69 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, hit, "struct vnode *",
70 "char *", "struct vnode *");
71 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, miss, "struct vnode *");
72 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, return, "int",
73 "struct vnode *", "char *");
74 SDT_PROBE_DEFINE3(vfs, namecache, lookup, hit, "struct vnode *", "char *",
76 SDT_PROBE_DEFINE2(vfs, namecache, lookup, hit__negative,
77 "struct vnode *", "char *");
78 SDT_PROBE_DEFINE2(vfs, namecache, lookup, miss, "struct vnode *",
80 SDT_PROBE_DEFINE1(vfs, namecache, purge, done, "struct vnode *");
81 SDT_PROBE_DEFINE1(vfs, namecache, purge_negative, done, "struct vnode *");
82 SDT_PROBE_DEFINE1(vfs, namecache, purgevfs, done, "struct mount *");
83 SDT_PROBE_DEFINE3(vfs, namecache, zap, done, "struct vnode *", "char *",
85 SDT_PROBE_DEFINE2(vfs, namecache, zap_negative, 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 SYSCTL_ULONG(_vfs, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0,
163 "Ratio of negative namecache entries");
164 static u_long numneg; /* number of negative entries allocated */
165 SYSCTL_ULONG(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0,
166 "Number of negative entries in namecache");
167 static u_long numcache; /* number of cache entries allocated */
168 SYSCTL_ULONG(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0,
169 "Number of namecache entries");
170 static u_long numcachehv; /* number of cache entries with vnodes held */
171 SYSCTL_ULONG(_debug, OID_AUTO, numcachehv, CTLFLAG_RD, &numcachehv, 0,
172 "Number of namecache entries with vnodes held");
173 static u_int ncsizefactor = 2;
174 SYSCTL_UINT(_vfs, OID_AUTO, ncsizefactor, CTLFLAG_RW, &ncsizefactor, 0,
175 "Size factor for namecache");
177 struct nchstats nchstats; /* cache effectiveness statistics */
179 static struct rwlock cache_lock;
180 RW_SYSINIT(vfscache, &cache_lock, "Name Cache");
182 #define CACHE_UPGRADE_LOCK() rw_try_upgrade(&cache_lock)
183 #define CACHE_RLOCK() rw_rlock(&cache_lock)
184 #define CACHE_RUNLOCK() rw_runlock(&cache_lock)
185 #define CACHE_WLOCK() rw_wlock(&cache_lock)
186 #define CACHE_WUNLOCK() rw_wunlock(&cache_lock)
189 * UMA zones for the VFS cache.
191 * The small cache is used for entries with short names, which are the
192 * most common. The large cache is used for entries which are too big to
193 * fit in the small cache.
195 static uma_zone_t cache_zone_small;
196 static uma_zone_t cache_zone_small_ts;
197 static uma_zone_t cache_zone_large;
198 static uma_zone_t cache_zone_large_ts;
200 #define CACHE_PATH_CUTOFF 35
202 static struct namecache *
203 cache_alloc(int len, int ts)
206 if (len > CACHE_PATH_CUTOFF) {
208 return (uma_zalloc(cache_zone_large_ts, M_WAITOK));
210 return (uma_zalloc(cache_zone_large, M_WAITOK));
213 return (uma_zalloc(cache_zone_small_ts, M_WAITOK));
215 return (uma_zalloc(cache_zone_small, M_WAITOK));
219 cache_free(struct namecache *ncp)
225 ts = ncp->nc_flag & NCF_TS;
226 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF) {
228 uma_zfree(cache_zone_small_ts, ncp);
230 uma_zfree(cache_zone_small, ncp);
232 uma_zfree(cache_zone_large_ts, ncp);
234 uma_zfree(cache_zone_large, ncp);
238 nc_get_name(struct namecache *ncp)
240 struct namecache_ts *ncp_ts;
242 if ((ncp->nc_flag & NCF_TS) == 0)
243 return (ncp->nc_name);
244 ncp_ts = (struct namecache_ts *)ncp;
245 return (ncp_ts->nc_name);
249 cache_out_ts(struct namecache *ncp, struct timespec *tsp, int *ticksp)
252 KASSERT((ncp->nc_flag & NCF_TS) != 0 ||
253 (tsp == NULL && ticksp == NULL),
257 *tsp = ((struct namecache_ts *)ncp)->nc_time;
259 *ticksp = ((struct namecache_ts *)ncp)->nc_ticks;
262 static int doingcache = 1; /* 1 => enable the cache */
263 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0,
264 "VFS namecache enabled");
266 /* Export size information to userland */
267 SYSCTL_INT(_debug_sizeof, OID_AUTO, namecache, CTLFLAG_RD, SYSCTL_NULL_INT_PTR,
268 sizeof(struct namecache), "sizeof(struct namecache)");
271 * The new name cache statistics
273 static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0,
274 "Name cache statistics");
275 #define STATNODE(mode, name, var, descr) \
276 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, mode, var, 0, descr);
277 STATNODE(CTLFLAG_RD, numneg, &numneg, "Number of negative cache entries");
278 STATNODE(CTLFLAG_RD, numcache, &numcache, "Number of cache entries");
279 static u_long numcalls; STATNODE(CTLFLAG_RD, numcalls, &numcalls,
280 "Number of cache lookups");
281 static u_long dothits; STATNODE(CTLFLAG_RD, dothits, &dothits,
282 "Number of '.' hits");
283 static u_long dotdothits; STATNODE(CTLFLAG_RD, dotdothits, &dotdothits,
284 "Number of '..' hits");
285 static u_long numchecks; STATNODE(CTLFLAG_RD, numchecks, &numchecks,
286 "Number of checks in lookup");
287 static u_long nummiss; STATNODE(CTLFLAG_RD, nummiss, &nummiss,
288 "Number of cache misses");
289 static u_long nummisszap; STATNODE(CTLFLAG_RD, nummisszap, &nummisszap,
290 "Number of cache misses we do not want to cache");
291 static u_long numposzaps; STATNODE(CTLFLAG_RD, numposzaps, &numposzaps,
292 "Number of cache hits (positive) we do not want to cache");
293 static u_long numposhits; STATNODE(CTLFLAG_RD, numposhits, &numposhits,
294 "Number of cache hits (positive)");
295 static u_long numnegzaps; STATNODE(CTLFLAG_RD, numnegzaps, &numnegzaps,
296 "Number of cache hits (negative) we do not want to cache");
297 static u_long numneghits; STATNODE(CTLFLAG_RD, numneghits, &numneghits,
298 "Number of cache hits (negative)");
299 static u_long numupgrades; STATNODE(CTLFLAG_RD, numupgrades, &numupgrades,
300 "Number of updates of the cache after lookup (write lock + retry)");
302 SYSCTL_OPAQUE(_vfs_cache, OID_AUTO, nchstats, CTLFLAG_RD | CTLFLAG_MPSAFE,
303 &nchstats, sizeof(nchstats), "LU",
304 "VFS cache effectiveness statistics");
308 static void cache_zap(struct namecache *ncp);
309 static int vn_vptocnp_locked(struct vnode **vp, struct ucred *cred, char *buf,
311 static int vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
312 char *buf, char **retbuf, u_int buflen);
314 static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
318 * Grab an atomic snapshot of the name cache hash chain lengths
320 static SYSCTL_NODE(_debug, OID_AUTO, hashstat, CTLFLAG_RW, NULL,
324 sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS)
326 struct nchashhead *ncpp;
327 struct namecache *ncp;
328 int i, error, n_nchash, *cntbuf;
330 n_nchash = nchash + 1; /* nchash is max index, not count */
331 if (req->oldptr == NULL)
332 return SYSCTL_OUT(req, 0, n_nchash * sizeof(int));
333 cntbuf = malloc(n_nchash * sizeof(int), M_TEMP, M_ZERO | M_WAITOK);
335 /* Scan hash tables counting entries */
336 for (ncpp = nchashtbl, i = 0; i < n_nchash; ncpp++, i++)
337 LIST_FOREACH(ncp, ncpp, nc_hash)
340 for (error = 0, i = 0; i < n_nchash; i++)
341 if ((error = SYSCTL_OUT(req, &cntbuf[i], sizeof(int))) != 0)
343 free(cntbuf, M_TEMP);
346 SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD|
347 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_rawnchash, "S,int",
348 "nchash chain lengths");
351 sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS)
354 struct nchashhead *ncpp;
355 struct namecache *ncp;
357 int count, maxlength, used, pct;
360 return SYSCTL_OUT(req, 0, 4 * sizeof(int));
363 n_nchash = nchash + 1; /* nchash is max index, not count */
367 /* Scan hash tables for applicable entries */
368 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
370 LIST_FOREACH(ncp, ncpp, nc_hash) {
375 if (maxlength < count)
378 n_nchash = nchash + 1;
380 pct = (used * 100) / (n_nchash / 100);
381 error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash));
384 error = SYSCTL_OUT(req, &used, sizeof(used));
387 error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength));
390 error = SYSCTL_OUT(req, &pct, sizeof(pct));
395 SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD|
396 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_nchash, "I",
397 "nchash statistics (number of total/used buckets, maximum chain length, usage percentage)");
403 * Removes a namecache entry from cache, whether it contains an actual
404 * pointer to a vnode or if it is just a negative cache entry.
408 struct namecache *ncp;
412 rw_assert(&cache_lock, RA_WLOCKED);
413 CTR2(KTR_VFS, "cache_zap(%p) vp %p", ncp, ncp->nc_vp);
415 if (ncp->nc_vp != NULL) {
416 SDT_PROBE(vfs, namecache, zap, done, ncp->nc_dvp,
417 nc_get_name(ncp), ncp->nc_vp, 0, 0);
419 SDT_PROBE(vfs, namecache, zap_negative, done, ncp->nc_dvp,
420 nc_get_name(ncp), 0, 0, 0);
424 LIST_REMOVE(ncp, nc_hash);
425 if (ncp->nc_flag & NCF_ISDOTDOT) {
426 if (ncp == ncp->nc_dvp->v_cache_dd)
427 ncp->nc_dvp->v_cache_dd = NULL;
429 LIST_REMOVE(ncp, nc_src);
430 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) {
436 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
437 if (ncp == ncp->nc_vp->v_cache_dd)
438 ncp->nc_vp->v_cache_dd = NULL;
440 TAILQ_REMOVE(&ncneg, ncp, nc_dst);
450 * Lookup an entry in the cache
452 * Lookup is called with dvp pointing to the directory to search,
453 * cnp pointing to the name of the entry being sought. If the lookup
454 * succeeds, the vnode is returned in *vpp, and a status of -1 is
455 * returned. If the lookup determines that the name does not exist
456 * (negative cacheing), a status of ENOENT is returned. If the lookup
457 * fails, a status of zero is returned. If the directory vnode is
458 * recycled out from under us due to a forced unmount, a status of
459 * ENOENT is returned.
461 * vpp is locked and ref'd on return. If we're looking up DOTDOT, dvp is
462 * unlocked. If we're looking up . an extra ref is taken, but the lock is
463 * not recursively acquired.
467 cache_lookup(dvp, vpp, cnp, tsp, ticksp)
470 struct componentname *cnp;
471 struct timespec *tsp;
474 struct namecache *ncp;
476 int error, ltype, wlocked;
479 cnp->cn_flags &= ~MAKEENTRY;
489 if (cnp->cn_nameptr[0] == '.') {
490 if (cnp->cn_namelen == 1) {
492 CTR2(KTR_VFS, "cache_lookup(%p, %s) found via .",
493 dvp, cnp->cn_nameptr);
495 SDT_PROBE(vfs, namecache, lookup, hit, dvp, ".",
503 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
505 if (dvp->v_cache_dd == NULL) {
506 SDT_PROBE(vfs, namecache, lookup, miss, dvp,
510 if ((cnp->cn_flags & MAKEENTRY) == 0) {
511 if (!wlocked && !CACHE_UPGRADE_LOCK())
513 if (dvp->v_cache_dd->nc_flag & NCF_ISDOTDOT)
514 cache_zap(dvp->v_cache_dd);
515 dvp->v_cache_dd = NULL;
519 ncp = dvp->v_cache_dd;
520 if (ncp->nc_flag & NCF_ISDOTDOT)
524 /* Return failure if negative entry was found. */
526 goto negative_success;
527 CTR3(KTR_VFS, "cache_lookup(%p, %s) found %p via ..",
528 dvp, cnp->cn_nameptr, *vpp);
529 SDT_PROBE(vfs, namecache, lookup, hit, dvp, "..",
531 cache_out_ts(ncp, tsp, ticksp);
532 if ((ncp->nc_flag & (NCF_ISDOTDOT | NCF_DTS)) ==
533 NCF_DTS && tsp != NULL)
534 *tsp = ((struct namecache_ts *)ncp)->
540 hash = fnv_32_buf(cnp->cn_nameptr, cnp->cn_namelen, FNV1_32_INIT);
541 hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
542 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
544 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
545 !bcmp(nc_get_name(ncp), cnp->cn_nameptr, ncp->nc_nlen))
549 /* We failed to find an entry */
551 SDT_PROBE(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
553 if ((cnp->cn_flags & MAKEENTRY) == 0) {
562 /* We don't want to have an entry, so dump it */
563 if ((cnp->cn_flags & MAKEENTRY) == 0) {
565 nchstats.ncs_badhits++;
566 if (!wlocked && !CACHE_UPGRADE_LOCK())
573 /* We found a "positive" match, return the vnode */
576 nchstats.ncs_goodhits++;
578 CTR4(KTR_VFS, "cache_lookup(%p, %s) found %p via ncp %p",
579 dvp, cnp->cn_nameptr, *vpp, ncp);
580 SDT_PROBE(vfs, namecache, lookup, hit, dvp, nc_get_name(ncp),
582 cache_out_ts(ncp, tsp, ticksp);
587 /* We found a negative match, and want to create it, so purge */
588 if (cnp->cn_nameiop == CREATE) {
590 nchstats.ncs_badhits++;
591 if (!wlocked && !CACHE_UPGRADE_LOCK())
598 if (!wlocked && !CACHE_UPGRADE_LOCK())
602 * We found a "negative" match, so we shift it to the end of
603 * the "negative" cache entries queue to satisfy LRU. Also,
604 * check to see if the entry is a whiteout; indicate this to
605 * the componentname, if so.
607 TAILQ_REMOVE(&ncneg, ncp, nc_dst);
608 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
609 nchstats.ncs_neghits++;
610 if (ncp->nc_flag & NCF_WHITE)
611 cnp->cn_flags |= ISWHITEOUT;
612 SDT_PROBE(vfs, namecache, lookup, hit__negative, dvp, nc_get_name(ncp),
614 cache_out_ts(ncp, tsp, ticksp);
620 * We need to update the cache after our lookup, so upgrade to
621 * a write lock and retry the operation.
631 * On success we return a locked and ref'd vnode as per the lookup
634 if (dvp == *vpp) { /* lookup on "." */
641 * When we lookup "." we still can be asked to lock it
644 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
645 if (ltype != VOP_ISLOCKED(*vpp)) {
646 if (ltype == LK_EXCLUSIVE) {
647 vn_lock(*vpp, LK_UPGRADE | LK_RETRY);
648 if ((*vpp)->v_iflag & VI_DOOMED) {
655 vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY);
659 ltype = 0; /* silence gcc warning */
660 if (cnp->cn_flags & ISDOTDOT) {
661 ltype = VOP_ISLOCKED(dvp);
669 error = vget(*vpp, cnp->cn_lkflags | LK_INTERLOCK, cnp->cn_thread);
670 if (cnp->cn_flags & ISDOTDOT) {
671 vn_lock(dvp, ltype | LK_RETRY);
672 if (dvp->v_iflag & VI_DOOMED) {
683 if ((cnp->cn_flags & ISLASTCN) &&
684 (cnp->cn_lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE) {
685 ASSERT_VOP_ELOCKED(*vpp, "cache_lookup");
698 * Add an entry to the cache.
701 cache_enter_time(dvp, vp, cnp, tsp, dtsp)
704 struct componentname *cnp;
705 struct timespec *tsp;
706 struct timespec *dtsp;
708 struct namecache *ncp, *n2;
709 struct namecache_ts *n3;
710 struct nchashhead *ncpp;
717 CTR3(KTR_VFS, "cache_enter(%p, %p, %s)", dvp, vp, cnp->cn_nameptr);
718 VNASSERT(vp == NULL || (vp->v_iflag & VI_DOOMED) == 0, vp,
719 ("cache_enter: Adding a doomed vnode"));
720 VNASSERT(dvp == NULL || (dvp->v_iflag & VI_DOOMED) == 0, dvp,
721 ("cache_enter: Doomed vnode used as src"));
727 * Avoid blowout in namecache entries.
729 if (numcache >= desiredvnodes * ncsizefactor)
733 if (cnp->cn_nameptr[0] == '.') {
734 if (cnp->cn_namelen == 1)
736 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
739 * If dotdot entry already exists, just retarget it
740 * to new parent vnode, otherwise continue with new
741 * namecache entry allocation.
743 if ((ncp = dvp->v_cache_dd) != NULL &&
744 ncp->nc_flag & NCF_ISDOTDOT) {
745 KASSERT(ncp->nc_dvp == dvp,
746 ("wrong isdotdot parent"));
747 if (ncp->nc_vp != NULL) {
748 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst,
751 TAILQ_REMOVE(&ncneg, ncp, nc_dst);
755 TAILQ_INSERT_HEAD(&vp->v_cache_dst,
758 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
765 dvp->v_cache_dd = NULL;
766 SDT_PROBE(vfs, namecache, enter, done, dvp, "..", vp,
777 * Calculate the hash key and setup as much of the new
778 * namecache entry as possible before acquiring the lock.
780 ncp = cache_alloc(cnp->cn_namelen, tsp != NULL);
785 n3 = (struct namecache_ts *)ncp;
787 n3->nc_ticks = ticks;
788 n3->nc_flag |= NCF_TS;
790 n3->nc_dotdottime = *dtsp;
791 n3->nc_flag |= NCF_DTS;
794 len = ncp->nc_nlen = cnp->cn_namelen;
795 hash = fnv_32_buf(cnp->cn_nameptr, len, FNV1_32_INIT);
796 strlcpy(nc_get_name(ncp), cnp->cn_nameptr, len + 1);
797 hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
801 * See if this vnode or negative entry is already in the cache
802 * with this name. This can happen with concurrent lookups of
803 * the same path name.
805 ncpp = NCHHASH(hash);
806 LIST_FOREACH(n2, ncpp, nc_hash) {
807 if (n2->nc_dvp == dvp &&
808 n2->nc_nlen == cnp->cn_namelen &&
809 !bcmp(nc_get_name(n2), cnp->cn_nameptr, n2->nc_nlen)) {
811 KASSERT((n2->nc_flag & NCF_TS) != 0,
813 n3 = (struct namecache_ts *)n2;
815 ((struct namecache_ts *)ncp)->nc_time;
817 ((struct namecache_ts *)ncp)->nc_ticks;
820 ((struct namecache_ts *)ncp)->
822 n3->nc_flag |= NCF_DTS;
831 if (flag == NCF_ISDOTDOT) {
833 * See if we are trying to add .. entry, but some other lookup
834 * has populated v_cache_dd pointer already.
836 if (dvp->v_cache_dd != NULL) {
841 KASSERT(vp == NULL || vp->v_type == VDIR,
842 ("wrong vnode type %p", vp));
843 dvp->v_cache_dd = ncp;
849 if (cnp->cn_flags & ISWHITEOUT)
850 ncp->nc_flag |= NCF_WHITE;
851 } else if (vp->v_type == VDIR) {
852 if (flag != NCF_ISDOTDOT) {
854 * For this case, the cache entry maps both the
855 * directory name in it and the name ".." for the
856 * directory's parent.
858 if ((n2 = vp->v_cache_dd) != NULL &&
859 (n2->nc_flag & NCF_ISDOTDOT) != 0)
861 vp->v_cache_dd = ncp;
864 vp->v_cache_dd = NULL;
868 * Insert the new namecache entry into the appropriate chain
869 * within the cache entries table.
871 LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
872 if (flag != NCF_ISDOTDOT) {
873 if (LIST_EMPTY(&dvp->v_cache_src)) {
877 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
881 * If the entry is "negative", we place it into the
882 * "negative" cache queue, otherwise, we place it into the
883 * destination vnode's cache entries queue.
886 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
887 SDT_PROBE(vfs, namecache, enter, done, dvp, nc_get_name(ncp),
890 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
891 SDT_PROBE(vfs, namecache, enter_negative, done, dvp,
892 nc_get_name(ncp), 0, 0, 0);
894 if (numneg * ncnegfactor > numcache) {
895 ncp = TAILQ_FIRST(&ncneg);
896 KASSERT(ncp->nc_vp == NULL, ("ncp %p vp %p on ncneg",
908 * Name cache initialization, from vfs_init() when we are booting
911 nchinit(void *dummy __unused)
916 cache_zone_small = uma_zcreate("S VFS Cache",
917 sizeof(struct namecache) + CACHE_PATH_CUTOFF + 1,
918 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
919 cache_zone_small_ts = uma_zcreate("STS VFS Cache",
920 sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1,
921 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
922 cache_zone_large = uma_zcreate("L VFS Cache",
923 sizeof(struct namecache) + NAME_MAX + 1,
924 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
925 cache_zone_large_ts = uma_zcreate("LTS VFS Cache",
926 sizeof(struct namecache_ts) + NAME_MAX + 1,
927 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
929 nchashtbl = hashinit(desiredvnodes * 2, M_VFSCACHE, &nchash);
931 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL);
935 * Invalidate all entries to a particular vnode.
942 CTR1(KTR_VFS, "cache_purge(%p)", vp);
943 SDT_PROBE(vfs, namecache, purge, done, vp, 0, 0, 0, 0);
945 while (!LIST_EMPTY(&vp->v_cache_src))
946 cache_zap(LIST_FIRST(&vp->v_cache_src));
947 while (!TAILQ_EMPTY(&vp->v_cache_dst))
948 cache_zap(TAILQ_FIRST(&vp->v_cache_dst));
949 if (vp->v_cache_dd != NULL) {
950 KASSERT(vp->v_cache_dd->nc_flag & NCF_ISDOTDOT,
951 ("lost dotdot link"));
952 cache_zap(vp->v_cache_dd);
954 KASSERT(vp->v_cache_dd == NULL, ("incomplete purge"));
959 * Invalidate all negative entries for a particular directory vnode.
962 cache_purge_negative(vp)
965 struct namecache *cp, *ncp;
967 CTR1(KTR_VFS, "cache_purge_negative(%p)", vp);
968 SDT_PROBE(vfs, namecache, purge_negative, done, vp, 0, 0, 0, 0);
970 LIST_FOREACH_SAFE(cp, &vp->v_cache_src, nc_src, ncp) {
971 if (cp->nc_vp == NULL)
978 * Flush all entries referencing a particular filesystem.
984 struct nchashhead *ncpp;
985 struct namecache *ncp, *nnp;
987 /* Scan hash tables for applicable entries */
988 SDT_PROBE(vfs, namecache, purgevfs, done, mp, 0, 0, 0, 0);
990 for (ncpp = &nchashtbl[nchash]; ncpp >= nchashtbl; ncpp--) {
991 LIST_FOREACH_SAFE(ncp, ncpp, nc_hash, nnp) {
992 if (ncp->nc_dvp->v_mount == mp)
1000 * Perform canonical checks and cache lookup and pass on to filesystem
1001 * through the vop_cachedlookup only if needed.
1005 vfs_cache_lookup(ap)
1006 struct vop_lookup_args /* {
1007 struct vnode *a_dvp;
1008 struct vnode **a_vpp;
1009 struct componentname *a_cnp;
1014 struct vnode **vpp = ap->a_vpp;
1015 struct componentname *cnp = ap->a_cnp;
1016 struct ucred *cred = cnp->cn_cred;
1017 int flags = cnp->cn_flags;
1018 struct thread *td = cnp->cn_thread;
1023 if (dvp->v_type != VDIR)
1026 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
1027 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
1030 error = VOP_ACCESS(dvp, VEXEC, cred, td);
1034 error = cache_lookup(dvp, vpp, cnp, NULL, NULL);
1036 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
1043 * XXX All of these sysctls would probably be more productive dead.
1045 static int disablecwd;
1046 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0,
1047 "Disable the getcwd syscall");
1049 /* Implementation of the getcwd syscall. */
1051 sys___getcwd(td, uap)
1053 struct __getcwd_args *uap;
1056 return (kern___getcwd(td, uap->buf, UIO_USERSPACE, uap->buflen,
1061 kern___getcwd(struct thread *td, char *buf, enum uio_seg bufseg, u_int buflen,
1065 struct filedesc *fdp;
1066 struct vnode *cdir, *rdir;
1073 if (buflen > path_max)
1076 tmpbuf = malloc(buflen, M_TEMP, M_WAITOK);
1077 fdp = td->td_proc->p_fd;
1078 FILEDESC_SLOCK(fdp);
1079 cdir = fdp->fd_cdir;
1081 rdir = fdp->fd_rdir;
1083 FILEDESC_SUNLOCK(fdp);
1084 error = vn_fullpath1(td, cdir, rdir, tmpbuf, &bp, buflen);
1089 if (bufseg == UIO_SYSSPACE)
1090 bcopy(bp, buf, strlen(bp) + 1);
1092 error = copyout(bp, buf, strlen(bp) + 1);
1094 if (KTRPOINT(curthread, KTR_NAMEI))
1098 free(tmpbuf, M_TEMP);
1103 * Thus begins the fullpath magic.
1107 #define STATNODE(name, descr) \
1108 static u_int name; \
1109 SYSCTL_UINT(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, descr)
1111 static int disablefullpath;
1112 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW, &disablefullpath, 0,
1113 "Disable the vn_fullpath function");
1115 /* These count for kern___getcwd(), too. */
1116 STATNODE(numfullpathcalls, "Number of fullpath search calls");
1117 STATNODE(numfullpathfail1, "Number of fullpath search errors (ENOTDIR)");
1118 STATNODE(numfullpathfail2,
1119 "Number of fullpath search errors (VOP_VPTOCNP failures)");
1120 STATNODE(numfullpathfail4, "Number of fullpath search errors (ENOMEM)");
1121 STATNODE(numfullpathfound, "Number of successful fullpath calls");
1124 * Retrieve the full filesystem path that correspond to a vnode from the name
1125 * cache (if available)
1128 vn_fullpath(struct thread *td, struct vnode *vn, char **retbuf, char **freebuf)
1131 struct filedesc *fdp;
1135 if (disablefullpath)
1140 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1141 fdp = td->td_proc->p_fd;
1142 FILEDESC_SLOCK(fdp);
1143 rdir = fdp->fd_rdir;
1145 FILEDESC_SUNLOCK(fdp);
1146 error = vn_fullpath1(td, vn, rdir, buf, retbuf, MAXPATHLEN);
1157 * This function is similar to vn_fullpath, but it attempts to lookup the
1158 * pathname relative to the global root mount point. This is required for the
1159 * auditing sub-system, as audited pathnames must be absolute, relative to the
1160 * global root mount point.
1163 vn_fullpath_global(struct thread *td, struct vnode *vn,
1164 char **retbuf, char **freebuf)
1169 if (disablefullpath)
1173 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1174 error = vn_fullpath1(td, vn, rootvnode, buf, retbuf, MAXPATHLEN);
1183 vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, u_int *buflen)
1188 error = vn_vptocnp_locked(vp, cred, buf, buflen);
1195 vn_vptocnp_locked(struct vnode **vp, struct ucred *cred, char *buf,
1199 struct namecache *ncp;
1202 TAILQ_FOREACH(ncp, &((*vp)->v_cache_dst), nc_dst) {
1203 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1207 if (*buflen < ncp->nc_nlen) {
1212 SDT_PROBE(vfs, namecache, fullpath, return, error,
1216 *buflen -= ncp->nc_nlen;
1217 memcpy(buf + *buflen, nc_get_name(ncp), ncp->nc_nlen);
1218 SDT_PROBE(vfs, namecache, fullpath, hit, ncp->nc_dvp,
1219 nc_get_name(ncp), vp, 0, 0);
1228 SDT_PROBE(vfs, namecache, fullpath, miss, vp, 0, 0, 0, 0);
1231 vn_lock(*vp, LK_SHARED | LK_RETRY);
1232 error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen);
1236 SDT_PROBE(vfs, namecache, fullpath, return, error, vp,
1243 if (dvp->v_iflag & VI_DOOMED) {
1244 /* forced unmount */
1248 SDT_PROBE(vfs, namecache, fullpath, return, error, vp,
1253 * *vp has its use count incremented still.
1260 * The magic behind kern___getcwd() and vn_fullpath().
1263 vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
1264 char *buf, char **retbuf, u_int buflen)
1266 int error, slash_prefixed;
1267 #ifdef KDTRACE_HOOKS
1268 struct vnode *startvp = vp;
1277 SDT_PROBE(vfs, namecache, fullpath, entry, vp, 0, 0, 0, 0);
1281 if (vp->v_type != VDIR) {
1282 error = vn_vptocnp_locked(&vp, td->td_ucred, buf, &buflen);
1290 buf[--buflen] = '/';
1293 while (vp != rdir && vp != rootvnode) {
1294 if (vp->v_vflag & VV_ROOT) {
1295 if (vp->v_iflag & VI_DOOMED) { /* forced unmount */
1299 SDT_PROBE(vfs, namecache, fullpath, return,
1300 error, vp, NULL, 0, 0);
1303 vp1 = vp->v_mount->mnt_vnodecovered;
1311 if (vp->v_type != VDIR) {
1316 SDT_PROBE(vfs, namecache, fullpath, return,
1317 error, vp, NULL, 0, 0);
1320 error = vn_vptocnp_locked(&vp, td->td_ucred, buf, &buflen);
1327 SDT_PROBE(vfs, namecache, fullpath, return, error,
1328 startvp, NULL, 0, 0);
1331 buf[--buflen] = '/';
1336 if (!slash_prefixed) {
1341 SDT_PROBE(vfs, namecache, fullpath, return, ENOMEM,
1342 startvp, NULL, 0, 0);
1345 buf[--buflen] = '/';
1351 SDT_PROBE(vfs, namecache, fullpath, return, 0, startvp, buf + buflen,
1353 *retbuf = buf + buflen;
1358 vn_dir_dd_ino(struct vnode *vp)
1360 struct namecache *ncp;
1363 ASSERT_VOP_LOCKED(vp, "vn_dir_dd_ino");
1365 TAILQ_FOREACH(ncp, &(vp->v_cache_dst), nc_dst) {
1366 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0)
1371 if (vget(ddvp, LK_INTERLOCK | LK_SHARED | LK_NOWAIT, curthread))
1380 vn_commname(struct vnode *vp, char *buf, u_int buflen)
1382 struct namecache *ncp;
1386 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst)
1387 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1393 l = min(ncp->nc_nlen, buflen - 1);
1394 memcpy(buf, nc_get_name(ncp), l);
1400 /* ABI compat shims for old kernel modules. */
1403 void cache_enter(struct vnode *dvp, struct vnode *vp,
1404 struct componentname *cnp);
1407 cache_enter(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
1410 cache_enter_time(dvp, vp, cnp, NULL, NULL);
1414 * This function updates path string to vnode's full global path
1415 * and checks the size of the new path string against the pathlen argument.
1417 * Requires a locked, referenced vnode.
1418 * Vnode is re-locked on success or ENODEV, otherwise unlocked.
1420 * If sysctl debug.disablefullpath is set, ENODEV is returned,
1421 * vnode is left locked and path remain untouched.
1423 * If vp is a directory, the call to vn_fullpath_global() always succeeds
1424 * because it falls back to the ".." lookup if the namecache lookup fails.
1427 vn_path_to_global_path(struct thread *td, struct vnode *vp, char *path,
1430 struct nameidata nd;
1435 ASSERT_VOP_ELOCKED(vp, __func__);
1437 /* Return ENODEV if sysctl debug.disablefullpath==1 */
1438 if (disablefullpath)
1441 /* Construct global filesystem path from vp. */
1443 error = vn_fullpath_global(td, vp, &rpath, &fbuf);
1450 if (strlen(rpath) >= pathlen) {
1452 error = ENAMETOOLONG;
1457 * Re-lookup the vnode by path to detect a possible rename.
1458 * As a side effect, the vnode is relocked.
1459 * If vnode was renamed, return ENOENT.
1461 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
1462 UIO_SYSSPACE, path, td);
1468 NDFREE(&nd, NDF_ONLY_PNBUF);
1472 strcpy(path, rpath);