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 "struct 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 *", "struct 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 * Name caching works as follows:
107 * Names found by directory scans are retained in a cache
108 * for future reference. It is managed LRU, so frequently
109 * used names will hang around. Cache is indexed by hash value
110 * obtained from (vp, name) where vp refers to the directory
113 * If it is a "negative" entry, (i.e. for a name that is known NOT to
114 * exist) the vnode pointer will be NULL.
116 * Upon reaching the last segment of a path, if the reference
117 * is for DELETE, or NOCACHE is set (rewrite), and the
118 * name is located in the cache, it will be dropped.
122 * Structures associated with name cacheing.
124 #define NCHHASH(hash) \
125 (&nchashtbl[(hash) & nchash])
126 static LIST_HEAD(nchashhead, namecache) *nchashtbl; /* Hash Table */
127 static TAILQ_HEAD(, namecache) ncneg; /* Hash Table */
128 static u_long nchash; /* size of hash table */
129 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0,
130 "Size of namecache hash table");
131 static u_long ncnegfactor = 16; /* ratio of negative entries */
132 /* _debug sysctl left for backward compatibility */
133 SYSCTL_ULONG(_debug, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0, "");
134 SYSCTL_ULONG(_vfs, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0,
135 "Ratio of negative namecache entries");
136 static u_long numneg; /* number of negative entries allocated */
137 SYSCTL_ULONG(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0,
138 "Number of negative entries in namecache");
139 static u_long numcache; /* number of cache entries allocated */
140 SYSCTL_ULONG(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0,
141 "Number of namecache entries");
142 static u_long numcachehv; /* number of cache entries with vnodes held */
143 SYSCTL_ULONG(_debug, OID_AUTO, numcachehv, CTLFLAG_RD, &numcachehv, 0,
144 "Number of namecache entries with vnodes held");
145 static u_int ncsizefactor = 2;
146 SYSCTL_UINT(_vfs, OID_AUTO, ncsizefactor, CTLFLAG_RW, &ncsizefactor, 0,
147 "Size factor for namecache");
149 struct nchstats nchstats; /* cache effectiveness statistics */
151 static struct rwlock cache_lock;
152 RW_SYSINIT(vfscache, &cache_lock, "Name Cache");
154 #define CACHE_UPGRADE_LOCK() rw_try_upgrade(&cache_lock)
155 #define CACHE_RLOCK() rw_rlock(&cache_lock)
156 #define CACHE_RUNLOCK() rw_runlock(&cache_lock)
157 #define CACHE_WLOCK() rw_wlock(&cache_lock)
158 #define CACHE_WUNLOCK() rw_wunlock(&cache_lock)
161 * UMA zones for the VFS cache.
163 * The small cache is used for entries with short names, which are the
164 * most common. The large cache is used for entries which are too big to
165 * fit in the small cache.
167 static uma_zone_t cache_zone_small;
168 static uma_zone_t cache_zone_large;
170 #define CACHE_PATH_CUTOFF 35
171 #define CACHE_ZONE_SMALL (sizeof(struct namecache) + CACHE_PATH_CUTOFF \
173 #define CACHE_ZONE_LARGE (sizeof(struct namecache) + NAME_MAX + 1)
175 #define cache_alloc(len) uma_zalloc(((len) <= CACHE_PATH_CUTOFF) ? \
176 cache_zone_small : cache_zone_large, M_WAITOK)
177 #define cache_free(ncp) do { \
179 uma_zfree(((ncp)->nc_nlen <= CACHE_PATH_CUTOFF) ? \
180 cache_zone_small : cache_zone_large, (ncp)); \
183 static int doingcache = 1; /* 1 => enable the cache */
184 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0,
185 "VFS namecache enabled");
187 /* Export size information to userland */
188 SYSCTL_INT(_debug_sizeof, OID_AUTO, namecache, CTLFLAG_RD, 0,
189 sizeof(struct namecache), "sizeof(struct namecache)");
192 * The new name cache statistics
194 static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0,
195 "Name cache statistics");
196 #define STATNODE(mode, name, var, descr) \
197 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, mode, var, 0, descr);
198 STATNODE(CTLFLAG_RD, numneg, &numneg, "Number of negative cache entries");
199 STATNODE(CTLFLAG_RD, numcache, &numcache, "Number of cache entries");
200 static u_long numcalls; STATNODE(CTLFLAG_RD, numcalls, &numcalls,
201 "Number of cache lookups");
202 static u_long dothits; STATNODE(CTLFLAG_RD, dothits, &dothits,
203 "Number of '.' hits");
204 static u_long dotdothits; STATNODE(CTLFLAG_RD, dotdothits, &dotdothits,
205 "Number of '..' hits");
206 static u_long numchecks; STATNODE(CTLFLAG_RD, numchecks, &numchecks,
207 "Number of checks in lookup");
208 static u_long nummiss; STATNODE(CTLFLAG_RD, nummiss, &nummiss,
209 "Number of cache misses");
210 static u_long nummisszap; STATNODE(CTLFLAG_RD, nummisszap, &nummisszap,
211 "Number of cache misses we do not want to cache");
212 static u_long numposzaps; STATNODE(CTLFLAG_RD, numposzaps, &numposzaps,
213 "Number of cache hits (positive) we do not want to cache");
214 static u_long numposhits; STATNODE(CTLFLAG_RD, numposhits, &numposhits,
215 "Number of cache hits (positive)");
216 static u_long numnegzaps; STATNODE(CTLFLAG_RD, numnegzaps, &numnegzaps,
217 "Number of cache hits (negative) we do not want to cache");
218 static u_long numneghits; STATNODE(CTLFLAG_RD, numneghits, &numneghits,
219 "Number of cache hits (negative)");
220 static u_long numupgrades; STATNODE(CTLFLAG_RD, numupgrades, &numupgrades,
221 "Number of updates of the cache after lookup (write lock + retry)");
223 SYSCTL_OPAQUE(_vfs_cache, OID_AUTO, nchstats, CTLFLAG_RD | CTLFLAG_MPSAFE,
224 &nchstats, sizeof(nchstats), "LU",
225 "VFS cache effectiveness statistics");
229 static void cache_zap(struct namecache *ncp);
230 static int vn_vptocnp_locked(struct vnode **vp, struct ucred *cred, char *buf,
232 static int vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
233 char *buf, char **retbuf, u_int buflen);
235 static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
238 * Flags in namecache.nc_flag
240 #define NCF_WHITE 0x01
241 #define NCF_ISDOTDOT 0x02
245 * Grab an atomic snapshot of the name cache hash chain lengths
247 SYSCTL_NODE(_debug, OID_AUTO, hashstat, CTLFLAG_RW, NULL, "hash table stats");
250 sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS)
253 struct nchashhead *ncpp;
254 struct namecache *ncp;
258 n_nchash = nchash + 1; /* nchash is max index, not count */
260 return SYSCTL_OUT(req, 0, n_nchash * sizeof(int));
262 /* Scan hash tables for applicable entries */
263 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
266 LIST_FOREACH(ncp, ncpp, nc_hash) {
270 error = SYSCTL_OUT(req, &count, sizeof(count));
276 SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD|
277 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_rawnchash, "S,int",
278 "nchash chain lengths");
281 sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS)
284 struct nchashhead *ncpp;
285 struct namecache *ncp;
287 int count, maxlength, used, pct;
290 return SYSCTL_OUT(req, 0, 4 * sizeof(int));
292 n_nchash = nchash + 1; /* nchash is max index, not count */
296 /* Scan hash tables for applicable entries */
297 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
300 LIST_FOREACH(ncp, ncpp, nc_hash) {
306 if (maxlength < count)
309 n_nchash = nchash + 1;
310 pct = (used * 100 * 100) / n_nchash;
311 error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash));
314 error = SYSCTL_OUT(req, &used, sizeof(used));
317 error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength));
320 error = SYSCTL_OUT(req, &pct, sizeof(pct));
325 SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD|
326 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_nchash, "I",
327 "nchash chain lengths");
333 * Removes a namecache entry from cache, whether it contains an actual
334 * pointer to a vnode or if it is just a negative cache entry.
338 struct namecache *ncp;
342 rw_assert(&cache_lock, RA_WLOCKED);
343 CTR2(KTR_VFS, "cache_zap(%p) vp %p", ncp, ncp->nc_vp);
345 if (ncp->nc_vp != NULL) {
346 SDT_PROBE(vfs, namecache, zap, done, ncp->nc_dvp,
347 ncp->nc_name, ncp->nc_vp, 0, 0);
349 SDT_PROBE(vfs, namecache, zap_negative, done, ncp->nc_dvp,
350 ncp->nc_name, 0, 0, 0);
354 LIST_REMOVE(ncp, nc_hash);
355 if (ncp->nc_flag & NCF_ISDOTDOT) {
356 if (ncp == ncp->nc_dvp->v_cache_dd)
357 ncp->nc_dvp->v_cache_dd = NULL;
359 LIST_REMOVE(ncp, nc_src);
360 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) {
366 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
367 if (ncp == ncp->nc_vp->v_cache_dd)
368 ncp->nc_vp->v_cache_dd = NULL;
370 TAILQ_REMOVE(&ncneg, ncp, nc_dst);
380 * Lookup an entry in the cache
382 * Lookup is called with dvp pointing to the directory to search,
383 * cnp pointing to the name of the entry being sought. If the lookup
384 * succeeds, the vnode is returned in *vpp, and a status of -1 is
385 * returned. If the lookup determines that the name does not exist
386 * (negative cacheing), a status of ENOENT is returned. If the lookup
387 * fails, a status of zero is returned. If the directory vnode is
388 * recycled out from under us due to a forced unmount, a status of
389 * ENOENT is returned.
391 * vpp is locked and ref'd on return. If we're looking up DOTDOT, dvp is
392 * unlocked. If we're looking up . an extra ref is taken, but the lock is
393 * not recursively acquired.
397 cache_lookup(dvp, vpp, cnp)
400 struct componentname *cnp;
402 struct namecache *ncp;
404 int error, ltype, wlocked;
407 cnp->cn_flags &= ~MAKEENTRY;
417 if (cnp->cn_nameptr[0] == '.') {
418 if (cnp->cn_namelen == 1) {
420 CTR2(KTR_VFS, "cache_lookup(%p, %s) found via .",
421 dvp, cnp->cn_nameptr);
423 SDT_PROBE(vfs, namecache, lookup, hit, dvp, ".",
427 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
429 if (dvp->v_cache_dd == NULL) {
430 SDT_PROBE(vfs, namecache, lookup, miss, dvp,
434 if ((cnp->cn_flags & MAKEENTRY) == 0) {
435 if (!wlocked && !CACHE_UPGRADE_LOCK())
437 if (dvp->v_cache_dd->nc_flag & NCF_ISDOTDOT)
438 cache_zap(dvp->v_cache_dd);
439 dvp->v_cache_dd = NULL;
443 if (dvp->v_cache_dd->nc_flag & NCF_ISDOTDOT)
444 *vpp = dvp->v_cache_dd->nc_vp;
446 *vpp = dvp->v_cache_dd->nc_dvp;
447 /* Return failure if negative entry was found. */
449 ncp = dvp->v_cache_dd;
450 goto negative_success;
452 CTR3(KTR_VFS, "cache_lookup(%p, %s) found %p via ..",
453 dvp, cnp->cn_nameptr, *vpp);
454 SDT_PROBE(vfs, namecache, lookup, hit, dvp, "..",
460 hash = fnv_32_buf(cnp->cn_nameptr, cnp->cn_namelen, FNV1_32_INIT);
461 hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
462 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
464 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
465 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
469 /* We failed to find an entry */
471 SDT_PROBE(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
473 if ((cnp->cn_flags & MAKEENTRY) == 0) {
482 /* We don't want to have an entry, so dump it */
483 if ((cnp->cn_flags & MAKEENTRY) == 0) {
485 nchstats.ncs_badhits++;
486 if (!wlocked && !CACHE_UPGRADE_LOCK())
493 /* We found a "positive" match, return the vnode */
496 nchstats.ncs_goodhits++;
498 CTR4(KTR_VFS, "cache_lookup(%p, %s) found %p via ncp %p",
499 dvp, cnp->cn_nameptr, *vpp, ncp);
500 SDT_PROBE(vfs, namecache, lookup, hit, dvp, ncp->nc_name,
506 /* We found a negative match, and want to create it, so purge */
507 if (cnp->cn_nameiop == CREATE) {
509 nchstats.ncs_badhits++;
510 if (!wlocked && !CACHE_UPGRADE_LOCK())
517 if (!wlocked && !CACHE_UPGRADE_LOCK())
521 * We found a "negative" match, so we shift it to the end of
522 * the "negative" cache entries queue to satisfy LRU. Also,
523 * check to see if the entry is a whiteout; indicate this to
524 * the componentname, if so.
526 TAILQ_REMOVE(&ncneg, ncp, nc_dst);
527 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
528 nchstats.ncs_neghits++;
529 if (ncp->nc_flag & NCF_WHITE)
530 cnp->cn_flags |= ISWHITEOUT;
531 SDT_PROBE(vfs, namecache, lookup, hit_negative, dvp, ncp->nc_name,
538 * We need to update the cache after our lookup, so upgrade to
539 * a write lock and retry the operation.
549 * On success we return a locked and ref'd vnode as per the lookup
552 if (dvp == *vpp) { /* lookup on "." */
559 * When we lookup "." we still can be asked to lock it
562 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
563 if (ltype != VOP_ISLOCKED(*vpp)) {
564 if (ltype == LK_EXCLUSIVE) {
565 vn_lock(*vpp, LK_UPGRADE | LK_RETRY);
566 if ((*vpp)->v_iflag & VI_DOOMED) {
573 vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY);
577 ltype = 0; /* silence gcc warning */
578 if (cnp->cn_flags & ISDOTDOT) {
579 ltype = VOP_ISLOCKED(dvp);
587 error = vget(*vpp, cnp->cn_lkflags | LK_INTERLOCK, cnp->cn_thread);
588 if (cnp->cn_flags & ISDOTDOT) {
589 vn_lock(dvp, ltype | LK_RETRY);
590 if (dvp->v_iflag & VI_DOOMED) {
601 if ((cnp->cn_flags & ISLASTCN) &&
602 (cnp->cn_lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE) {
603 ASSERT_VOP_ELOCKED(*vpp, "cache_lookup");
616 * Add an entry to the cache.
619 cache_enter(dvp, vp, cnp)
622 struct componentname *cnp;
624 struct namecache *ncp, *n2;
625 struct nchashhead *ncpp;
632 CTR3(KTR_VFS, "cache_enter(%p, %p, %s)", dvp, vp, cnp->cn_nameptr);
633 VNASSERT(vp == NULL || (vp->v_iflag & VI_DOOMED) == 0, vp,
634 ("cache_enter: Adding a doomed vnode"));
635 VNASSERT(dvp == NULL || (dvp->v_iflag & VI_DOOMED) == 0, dvp,
636 ("cache_enter: Doomed vnode used as src"));
642 * Avoid blowout in namecache entries.
644 if (numcache >= desiredvnodes * ncsizefactor)
648 if (cnp->cn_nameptr[0] == '.') {
649 if (cnp->cn_namelen == 1)
651 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
654 * If dotdot entry already exists, just retarget it
655 * to new parent vnode, otherwise continue with new
656 * namecache entry allocation.
658 if ((ncp = dvp->v_cache_dd) != NULL &&
659 ncp->nc_flag & NCF_ISDOTDOT) {
660 KASSERT(ncp->nc_dvp == dvp,
661 ("wrong isdotdot parent"));
662 if (ncp->nc_vp != NULL)
663 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst,
666 TAILQ_REMOVE(&ncneg, ncp, nc_dst);
668 TAILQ_INSERT_HEAD(&vp->v_cache_dst,
671 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
676 dvp->v_cache_dd = NULL;
677 SDT_PROBE(vfs, namecache, enter, done, dvp, "..", vp,
688 * Calculate the hash key and setup as much of the new
689 * namecache entry as possible before acquiring the lock.
691 ncp = cache_alloc(cnp->cn_namelen);
695 len = ncp->nc_nlen = cnp->cn_namelen;
696 hash = fnv_32_buf(cnp->cn_nameptr, len, FNV1_32_INIT);
697 strlcpy(ncp->nc_name, cnp->cn_nameptr, len + 1);
698 hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
702 * See if this vnode or negative entry is already in the cache
703 * with this name. This can happen with concurrent lookups of
704 * the same path name.
706 ncpp = NCHHASH(hash);
707 LIST_FOREACH(n2, ncpp, nc_hash) {
708 if (n2->nc_dvp == dvp &&
709 n2->nc_nlen == cnp->cn_namelen &&
710 !bcmp(n2->nc_name, cnp->cn_nameptr, n2->nc_nlen)) {
717 if (flag == NCF_ISDOTDOT) {
719 * See if we are trying to add .. entry, but some other lookup
720 * has populated v_cache_dd pointer already.
722 if (dvp->v_cache_dd != NULL) {
727 KASSERT(vp == NULL || vp->v_type == VDIR,
728 ("wrong vnode type %p", vp));
729 dvp->v_cache_dd = ncp;
735 if (cnp->cn_flags & ISWHITEOUT)
736 ncp->nc_flag |= NCF_WHITE;
737 } else if (vp->v_type == VDIR) {
738 if (flag != NCF_ISDOTDOT) {
739 if ((n2 = vp->v_cache_dd) != NULL &&
740 (n2->nc_flag & NCF_ISDOTDOT) != 0)
742 vp->v_cache_dd = ncp;
745 vp->v_cache_dd = NULL;
749 * Insert the new namecache entry into the appropriate chain
750 * within the cache entries table.
752 LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
753 if (flag != NCF_ISDOTDOT) {
754 if (LIST_EMPTY(&dvp->v_cache_src)) {
758 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
762 * If the entry is "negative", we place it into the
763 * "negative" cache queue, otherwise, we place it into the
764 * destination vnode's cache entries queue.
767 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
768 SDT_PROBE(vfs, namecache, enter, done, dvp, ncp->nc_name, vp,
771 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
772 SDT_PROBE(vfs, namecache, enter_negative, done, dvp,
773 ncp->nc_name, 0, 0, 0);
775 if (numneg * ncnegfactor > numcache) {
776 ncp = TAILQ_FIRST(&ncneg);
787 * Name cache initialization, from vfs_init() when we are booting
790 nchinit(void *dummy __unused)
795 cache_zone_small = uma_zcreate("S VFS Cache", CACHE_ZONE_SMALL, NULL,
796 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
797 cache_zone_large = uma_zcreate("L VFS Cache", CACHE_ZONE_LARGE, NULL,
798 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
800 nchashtbl = hashinit(desiredvnodes * 2, M_VFSCACHE, &nchash);
802 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL);
806 * Invalidate all entries to a particular vnode.
813 CTR1(KTR_VFS, "cache_purge(%p)", vp);
814 SDT_PROBE(vfs, namecache, purge, done, vp, 0, 0, 0, 0);
816 while (!LIST_EMPTY(&vp->v_cache_src))
817 cache_zap(LIST_FIRST(&vp->v_cache_src));
818 while (!TAILQ_EMPTY(&vp->v_cache_dst))
819 cache_zap(TAILQ_FIRST(&vp->v_cache_dst));
820 if (vp->v_cache_dd != NULL) {
821 KASSERT(vp->v_cache_dd->nc_flag & NCF_ISDOTDOT,
822 ("lost dotdot link"));
823 cache_zap(vp->v_cache_dd);
825 KASSERT(vp->v_cache_dd == NULL, ("incomplete purge"));
830 * Invalidate all negative entries for a particular directory vnode.
833 cache_purge_negative(vp)
836 struct namecache *cp, *ncp;
838 CTR1(KTR_VFS, "cache_purge_negative(%p)", vp);
839 SDT_PROBE(vfs, namecache, purge_negative, done, vp, 0, 0, 0, 0);
841 LIST_FOREACH_SAFE(cp, &vp->v_cache_src, nc_src, ncp) {
842 if (cp->nc_vp == NULL)
849 * Flush all entries referencing a particular filesystem.
855 struct nchashhead *ncpp;
856 struct namecache *ncp, *nnp;
858 /* Scan hash tables for applicable entries */
859 SDT_PROBE(vfs, namecache, purgevfs, done, mp, 0, 0, 0, 0);
861 for (ncpp = &nchashtbl[nchash]; ncpp >= nchashtbl; ncpp--) {
862 LIST_FOREACH_SAFE(ncp, ncpp, nc_hash, nnp) {
863 if (ncp->nc_dvp->v_mount == mp)
871 * Perform canonical checks and cache lookup and pass on to filesystem
872 * through the vop_cachedlookup only if needed.
877 struct vop_lookup_args /* {
879 struct vnode **a_vpp;
880 struct componentname *a_cnp;
885 struct vnode **vpp = ap->a_vpp;
886 struct componentname *cnp = ap->a_cnp;
887 struct ucred *cred = cnp->cn_cred;
888 int flags = cnp->cn_flags;
889 struct thread *td = cnp->cn_thread;
894 if (dvp->v_type != VDIR)
897 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
898 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
901 error = VOP_ACCESS(dvp, VEXEC, cred, td);
905 error = cache_lookup(dvp, vpp, cnp);
907 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
914 #ifndef _SYS_SYSPROTO_H_
915 struct __getcwd_args {
922 * XXX All of these sysctls would probably be more productive dead.
924 static int disablecwd;
925 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0,
926 "Disable the getcwd syscall");
928 /* Implementation of the getcwd syscall. */
932 struct __getcwd_args *uap;
935 return (kern___getcwd(td, uap->buf, UIO_USERSPACE, uap->buflen));
939 kern___getcwd(struct thread *td, u_char *buf, enum uio_seg bufseg, u_int buflen)
942 struct filedesc *fdp;
943 struct vnode *cdir, *rdir;
944 int error, vfslocked;
950 if (buflen > MAXPATHLEN)
953 tmpbuf = malloc(buflen, M_TEMP, M_WAITOK);
954 fdp = td->td_proc->p_fd;
960 FILEDESC_SUNLOCK(fdp);
961 error = vn_fullpath1(td, cdir, rdir, tmpbuf, &bp, buflen);
962 vfslocked = VFS_LOCK_GIANT(rdir->v_mount);
964 VFS_UNLOCK_GIANT(vfslocked);
965 vfslocked = VFS_LOCK_GIANT(cdir->v_mount);
967 VFS_UNLOCK_GIANT(vfslocked);
970 if (bufseg == UIO_SYSSPACE)
971 bcopy(bp, buf, strlen(bp) + 1);
973 error = copyout(bp, buf, strlen(bp) + 1);
975 if (KTRPOINT(curthread, KTR_NAMEI))
979 free(tmpbuf, M_TEMP);
984 * Thus begins the fullpath magic.
988 #define STATNODE(name, descr) \
990 SYSCTL_UINT(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, descr)
992 static int disablefullpath;
993 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW, &disablefullpath, 0,
994 "Disable the vn_fullpath function");
996 /* These count for kern___getcwd(), too. */
997 STATNODE(numfullpathcalls, "Number of fullpath search calls");
998 STATNODE(numfullpathfail1, "Number of fullpath search errors (ENOTDIR)");
999 STATNODE(numfullpathfail2,
1000 "Number of fullpath search errors (VOP_VPTOCNP failures)");
1001 STATNODE(numfullpathfail4, "Number of fullpath search errors (ENOMEM)");
1002 STATNODE(numfullpathfound, "Number of successful fullpath calls");
1005 * Retrieve the full filesystem path that correspond to a vnode from the name
1006 * cache (if available)
1009 vn_fullpath(struct thread *td, struct vnode *vn, char **retbuf, char **freebuf)
1012 struct filedesc *fdp;
1014 int error, vfslocked;
1016 if (disablefullpath)
1021 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1022 fdp = td->td_proc->p_fd;
1023 FILEDESC_SLOCK(fdp);
1024 rdir = fdp->fd_rdir;
1026 FILEDESC_SUNLOCK(fdp);
1027 error = vn_fullpath1(td, vn, rdir, buf, retbuf, MAXPATHLEN);
1028 vfslocked = VFS_LOCK_GIANT(rdir->v_mount);
1030 VFS_UNLOCK_GIANT(vfslocked);
1040 * This function is similar to vn_fullpath, but it attempts to lookup the
1041 * pathname relative to the global root mount point. This is required for the
1042 * auditing sub-system, as audited pathnames must be absolute, relative to the
1043 * global root mount point.
1046 vn_fullpath_global(struct thread *td, struct vnode *vn,
1047 char **retbuf, char **freebuf)
1052 if (disablefullpath)
1056 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1057 error = vn_fullpath1(td, vn, rootvnode, buf, retbuf, MAXPATHLEN);
1066 vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, u_int *buflen)
1071 error = vn_vptocnp_locked(vp, cred, buf, buflen);
1074 * vn_vptocnp_locked() dropped hold acquired by
1075 * VOP_VPTOCNP immediately after locking the
1076 * cache. Since we are going to drop the cache rlock,
1077 * re-hold the result.
1086 vn_vptocnp_locked(struct vnode **vp, struct ucred *cred, char *buf,
1090 struct namecache *ncp;
1091 int error, vfslocked;
1093 TAILQ_FOREACH(ncp, &((*vp)->v_cache_dst), nc_dst) {
1094 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1098 if (*buflen < ncp->nc_nlen) {
1102 SDT_PROBE(vfs, namecache, fullpath, return, error,
1106 *buflen -= ncp->nc_nlen;
1107 memcpy(buf + *buflen, ncp->nc_name, ncp->nc_nlen);
1108 SDT_PROBE(vfs, namecache, fullpath, hit, ncp->nc_dvp,
1109 ncp->nc_name, vp, 0, 0);
1113 SDT_PROBE(vfs, namecache, fullpath, miss, vp, 0, 0, 0, 0);
1117 vfslocked = VFS_LOCK_GIANT((*vp)->v_mount);
1118 vn_lock(*vp, LK_SHARED | LK_RETRY);
1119 error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen);
1122 VFS_UNLOCK_GIANT(vfslocked);
1125 SDT_PROBE(vfs, namecache, fullpath, return, error, vp,
1132 if ((*vp)->v_iflag & VI_DOOMED) {
1133 /* forced unmount */
1137 SDT_PROBE(vfs, namecache, fullpath, return, error, vp,
1147 * The magic behind kern___getcwd() and vn_fullpath().
1150 vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
1151 char *buf, char **retbuf, u_int buflen)
1153 int error, slash_prefixed;
1154 #ifdef KDTRACE_HOOKS
1155 struct vnode *startvp = vp;
1163 SDT_PROBE(vfs, namecache, fullpath, entry, vp, 0, 0, 0, 0);
1166 if (vp->v_type != VDIR) {
1167 error = vn_vptocnp_locked(&vp, td->td_ucred, buf, &buflen);
1174 buf[--buflen] = '/';
1177 while (vp != rdir && vp != rootvnode) {
1178 if (vp->v_vflag & VV_ROOT) {
1179 if (vp->v_iflag & VI_DOOMED) { /* forced unmount */
1182 SDT_PROBE(vfs, namecache, fullpath, return,
1183 error, vp, NULL, 0, 0);
1186 vp = vp->v_mount->mnt_vnodecovered;
1189 if (vp->v_type != VDIR) {
1193 SDT_PROBE(vfs, namecache, fullpath, return,
1194 error, vp, NULL, 0, 0);
1197 error = vn_vptocnp_locked(&vp, td->td_ucred, buf, &buflen);
1203 SDT_PROBE(vfs, namecache, fullpath, return, error,
1204 startvp, NULL, 0, 0);
1207 buf[--buflen] = '/';
1212 if (!slash_prefixed) {
1216 SDT_PROBE(vfs, namecache, fullpath, return, ENOMEM,
1217 startvp, NULL, 0, 0);
1220 buf[--buflen] = '/';
1225 SDT_PROBE(vfs, namecache, fullpath, return, 0, startvp, buf + buflen,
1227 *retbuf = buf + buflen;
1232 vn_commname(struct vnode *vp, char *buf, u_int buflen)
1234 struct namecache *ncp;
1238 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst)
1239 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1245 l = min(ncp->nc_nlen, buflen - 1);
1246 memcpy(buf, ncp->nc_name, l);