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/filedesc.h>
43 #include <sys/fnv_hash.h>
44 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/namei.h>
50 #include <sys/rwlock.h>
52 #include <sys/syscallsubr.h>
53 #include <sys/sysctl.h>
54 #include <sys/sysproto.h>
55 #include <sys/systm.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 "struct char *", "struct vnode *");
71 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, miss, "struct vnode *");
72 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, return, "int", "struct vnode *",
74 SDT_PROBE_DEFINE3(vfs, namecache, lookup, hit, "struct vnode *", "char *",
76 SDT_PROBE_DEFINE2(vfs, namecache, lookup, hit_negative, "struct vnode *",
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 * 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 static u_long ncnegfactor = 16; /* ratio of negative entries */
131 SYSCTL_ULONG(_debug, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0, "");
132 static u_long numneg; /* number of cache entries allocated */
133 SYSCTL_ULONG(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0, "");
134 static u_long numcache; /* number of cache entries allocated */
135 SYSCTL_ULONG(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0, "");
136 static u_long numcachehv; /* number of cache entries with vnodes held */
137 SYSCTL_ULONG(_debug, OID_AUTO, numcachehv, CTLFLAG_RD, &numcachehv, 0, "");
139 static u_long numcachepl; /* number of cache purge for leaf entries */
140 SYSCTL_ULONG(_debug, OID_AUTO, numcachepl, CTLFLAG_RD, &numcachepl, 0, "");
142 struct nchstats nchstats; /* cache effectiveness statistics */
144 static struct rwlock cache_lock;
145 RW_SYSINIT(vfscache, &cache_lock, "Name Cache");
147 #define CACHE_UPGRADE_LOCK() rw_try_upgrade(&cache_lock)
148 #define CACHE_RLOCK() rw_rlock(&cache_lock)
149 #define CACHE_RUNLOCK() rw_runlock(&cache_lock)
150 #define CACHE_WLOCK() rw_wlock(&cache_lock)
151 #define CACHE_WUNLOCK() rw_wunlock(&cache_lock)
154 * UMA zones for the VFS cache.
156 * The small cache is used for entries with short names, which are the
157 * most common. The large cache is used for entries which are too big to
158 * fit in the small cache.
160 static uma_zone_t cache_zone_small;
161 static uma_zone_t cache_zone_large;
163 #define CACHE_PATH_CUTOFF 35
164 #define CACHE_ZONE_SMALL (sizeof(struct namecache) + CACHE_PATH_CUTOFF \
166 #define CACHE_ZONE_LARGE (sizeof(struct namecache) + NAME_MAX + 1)
168 #define cache_alloc(len) uma_zalloc(((len) <= CACHE_PATH_CUTOFF) ? \
169 cache_zone_small : cache_zone_large, M_WAITOK)
170 #define cache_free(ncp) do { \
172 uma_zfree(((ncp)->nc_nlen <= CACHE_PATH_CUTOFF) ? \
173 cache_zone_small : cache_zone_large, (ncp)); \
176 static int doingcache = 1; /* 1 => enable the cache */
177 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0, "");
179 /* Export size information to userland */
180 SYSCTL_INT(_debug_sizeof, OID_AUTO, namecache, CTLFLAG_RD, 0,
181 sizeof(struct namecache), "");
184 * The new name cache statistics
186 static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0, "Name cache statistics");
187 #define STATNODE(mode, name, var) \
188 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, mode, var, 0, "");
189 STATNODE(CTLFLAG_RD, numneg, &numneg);
190 STATNODE(CTLFLAG_RD, numcache, &numcache);
191 static u_long numcalls; STATNODE(CTLFLAG_RD, numcalls, &numcalls);
192 static u_long dothits; STATNODE(CTLFLAG_RD, dothits, &dothits);
193 static u_long dotdothits; STATNODE(CTLFLAG_RD, dotdothits, &dotdothits);
194 static u_long numchecks; STATNODE(CTLFLAG_RD, numchecks, &numchecks);
195 static u_long nummiss; STATNODE(CTLFLAG_RD, nummiss, &nummiss);
196 static u_long nummisszap; STATNODE(CTLFLAG_RD, nummisszap, &nummisszap);
197 static u_long numposzaps; STATNODE(CTLFLAG_RD, numposzaps, &numposzaps);
198 static u_long numposhits; STATNODE(CTLFLAG_RD, numposhits, &numposhits);
199 static u_long numnegzaps; STATNODE(CTLFLAG_RD, numnegzaps, &numnegzaps);
200 static u_long numneghits; STATNODE(CTLFLAG_RD, numneghits, &numneghits);
201 static u_long numupgrades; STATNODE(CTLFLAG_RD, numupgrades, &numupgrades);
203 SYSCTL_OPAQUE(_vfs_cache, OID_AUTO, nchstats, CTLFLAG_RD | CTLFLAG_MPSAFE,
204 &nchstats, sizeof(nchstats), "LU", "VFS cache effectiveness statistics");
208 static void cache_zap(struct namecache *ncp);
209 static int vn_vptocnp_locked(struct vnode **vp, struct ucred *cred, char *buf,
211 static int vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
212 char *buf, char **retbuf, u_int buflen);
214 static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
217 * Flags in namecache.nc_flag
219 #define NCF_WHITE 0x01
220 #define NCF_ISDOTDOT 0x02
224 * Grab an atomic snapshot of the name cache hash chain lengths
226 SYSCTL_NODE(_debug, OID_AUTO, hashstat, CTLFLAG_RW, NULL, "hash table stats");
229 sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS)
232 struct nchashhead *ncpp;
233 struct namecache *ncp;
237 n_nchash = nchash + 1; /* nchash is max index, not count */
239 return SYSCTL_OUT(req, 0, n_nchash * sizeof(int));
241 /* Scan hash tables for applicable entries */
242 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
245 LIST_FOREACH(ncp, ncpp, nc_hash) {
249 error = SYSCTL_OUT(req, &count, sizeof(count));
255 SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD|
256 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_rawnchash, "S,int",
257 "nchash chain lengths");
260 sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS)
263 struct nchashhead *ncpp;
264 struct namecache *ncp;
266 int count, maxlength, used, pct;
269 return SYSCTL_OUT(req, 0, 4 * sizeof(int));
271 n_nchash = nchash + 1; /* nchash is max index, not count */
275 /* Scan hash tables for applicable entries */
276 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
279 LIST_FOREACH(ncp, ncpp, nc_hash) {
285 if (maxlength < count)
288 n_nchash = nchash + 1;
289 pct = (used * 100 * 100) / n_nchash;
290 error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash));
293 error = SYSCTL_OUT(req, &used, sizeof(used));
296 error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength));
299 error = SYSCTL_OUT(req, &pct, sizeof(pct));
304 SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD|
305 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_nchash, "I",
306 "nchash chain lengths");
312 * Removes a namecache entry from cache, whether it contains an actual
313 * pointer to a vnode or if it is just a negative cache entry.
317 struct namecache *ncp;
321 rw_assert(&cache_lock, RA_WLOCKED);
322 CTR2(KTR_VFS, "cache_zap(%p) vp %p", ncp, ncp->nc_vp);
324 if (ncp->nc_vp != NULL) {
325 SDT_PROBE(vfs, namecache, zap, done, ncp->nc_dvp,
326 ncp->nc_name, ncp->nc_vp, 0, 0);
328 SDT_PROBE(vfs, namecache, zap_negative, done, ncp->nc_dvp,
329 ncp->nc_name, 0, 0, 0);
333 LIST_REMOVE(ncp, nc_hash);
334 if (ncp->nc_flag & NCF_ISDOTDOT) {
335 if (ncp == ncp->nc_dvp->v_cache_dd)
336 ncp->nc_dvp->v_cache_dd = NULL;
338 LIST_REMOVE(ncp, nc_src);
339 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) {
345 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
346 if (ncp == ncp->nc_vp->v_cache_dd)
347 ncp->nc_vp->v_cache_dd = NULL;
349 TAILQ_REMOVE(&ncneg, ncp, nc_dst);
359 * Lookup an entry in the cache
361 * Lookup is called with dvp pointing to the directory to search,
362 * cnp pointing to the name of the entry being sought. If the lookup
363 * succeeds, the vnode is returned in *vpp, and a status of -1 is
364 * returned. If the lookup determines that the name does not exist
365 * (negative cacheing), a status of ENOENT is returned. If the lookup
366 * fails, a status of zero is returned. If the directory vnode is
367 * recycled out from under us due to a forced unmount, a status of
368 * ENOENT is returned.
370 * vpp is locked and ref'd on return. If we're looking up DOTDOT, dvp is
371 * unlocked. If we're looking up . an extra ref is taken, but the lock is
372 * not recursively acquired.
376 cache_lookup(dvp, vpp, cnp)
379 struct componentname *cnp;
381 struct namecache *ncp;
383 int error, ltype, wlocked;
386 cnp->cn_flags &= ~MAKEENTRY;
396 if (cnp->cn_nameptr[0] == '.') {
397 if (cnp->cn_namelen == 1) {
399 CTR2(KTR_VFS, "cache_lookup(%p, %s) found via .",
400 dvp, cnp->cn_nameptr);
402 SDT_PROBE(vfs, namecache, lookup, hit, dvp, ".",
406 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
408 if (dvp->v_cache_dd == NULL) {
409 SDT_PROBE(vfs, namecache, lookup, miss, dvp,
413 if ((cnp->cn_flags & MAKEENTRY) == 0) {
414 if (!wlocked && !CACHE_UPGRADE_LOCK())
416 if (dvp->v_cache_dd->nc_flag & NCF_ISDOTDOT)
417 cache_zap(dvp->v_cache_dd);
418 dvp->v_cache_dd = NULL;
422 if (dvp->v_cache_dd->nc_flag & NCF_ISDOTDOT)
423 *vpp = dvp->v_cache_dd->nc_vp;
425 *vpp = dvp->v_cache_dd->nc_dvp;
426 /* Return failure if negative entry was found. */
428 ncp = dvp->v_cache_dd;
429 goto negative_success;
431 CTR3(KTR_VFS, "cache_lookup(%p, %s) found %p via ..",
432 dvp, cnp->cn_nameptr, *vpp);
433 SDT_PROBE(vfs, namecache, lookup, hit, dvp, "..",
439 hash = fnv_32_buf(cnp->cn_nameptr, cnp->cn_namelen, FNV1_32_INIT);
440 hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
441 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
443 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
444 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
448 /* We failed to find an entry */
450 SDT_PROBE(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
452 if ((cnp->cn_flags & MAKEENTRY) == 0) {
461 /* We don't want to have an entry, so dump it */
462 if ((cnp->cn_flags & MAKEENTRY) == 0) {
464 nchstats.ncs_badhits++;
465 if (!wlocked && !CACHE_UPGRADE_LOCK())
472 /* We found a "positive" match, return the vnode */
475 nchstats.ncs_goodhits++;
477 CTR4(KTR_VFS, "cache_lookup(%p, %s) found %p via ncp %p",
478 dvp, cnp->cn_nameptr, *vpp, ncp);
479 SDT_PROBE(vfs, namecache, lookup, hit, dvp, ncp->nc_name,
485 /* We found a negative match, and want to create it, so purge */
486 if (cnp->cn_nameiop == CREATE) {
488 nchstats.ncs_badhits++;
489 if (!wlocked && !CACHE_UPGRADE_LOCK())
496 if (!wlocked && !CACHE_UPGRADE_LOCK())
500 * We found a "negative" match, so we shift it to the end of
501 * the "negative" cache entries queue to satisfy LRU. Also,
502 * check to see if the entry is a whiteout; indicate this to
503 * the componentname, if so.
505 TAILQ_REMOVE(&ncneg, ncp, nc_dst);
506 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
507 nchstats.ncs_neghits++;
508 if (ncp->nc_flag & NCF_WHITE)
509 cnp->cn_flags |= ISWHITEOUT;
510 SDT_PROBE(vfs, namecache, lookup, hit_negative, dvp, ncp->nc_name,
517 * We need to update the cache after our lookup, so upgrade to
518 * a write lock and retry the operation.
528 * On success we return a locked and ref'd vnode as per the lookup
531 if (dvp == *vpp) { /* lookup on "." */
538 * When we lookup "." we still can be asked to lock it
541 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
542 if (ltype != VOP_ISLOCKED(*vpp)) {
543 if (ltype == LK_EXCLUSIVE) {
544 vn_lock(*vpp, LK_UPGRADE | LK_RETRY);
545 if ((*vpp)->v_iflag & VI_DOOMED) {
552 vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY);
556 ltype = 0; /* silence gcc warning */
557 if (cnp->cn_flags & ISDOTDOT) {
558 ltype = VOP_ISLOCKED(dvp);
566 error = vget(*vpp, cnp->cn_lkflags | LK_INTERLOCK, cnp->cn_thread);
567 if (cnp->cn_flags & ISDOTDOT) {
568 vn_lock(dvp, ltype | LK_RETRY);
569 if (dvp->v_iflag & VI_DOOMED) {
580 if ((cnp->cn_flags & ISLASTCN) &&
581 (cnp->cn_lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE) {
582 ASSERT_VOP_ELOCKED(*vpp, "cache_lookup");
595 * Add an entry to the cache.
598 cache_enter(dvp, vp, cnp)
601 struct componentname *cnp;
603 struct namecache *ncp, *n2;
604 struct nchashhead *ncpp;
611 CTR3(KTR_VFS, "cache_enter(%p, %p, %s)", dvp, vp, cnp->cn_nameptr);
612 VNASSERT(vp == NULL || (vp->v_iflag & VI_DOOMED) == 0, vp,
613 ("cahe_enter: Adding a doomed vnode"));
619 * Avoid blowout in namecache entries.
621 if (numcache >= desiredvnodes * 2)
625 if (cnp->cn_nameptr[0] == '.') {
626 if (cnp->cn_namelen == 1)
628 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
631 * If dotdot entry already exists, just retarget it
632 * to new parent vnode, otherwise continue with new
633 * namecache entry allocation.
635 if ((ncp = dvp->v_cache_dd) != NULL &&
636 ncp->nc_flag & NCF_ISDOTDOT) {
637 KASSERT(ncp->nc_dvp == dvp,
638 ("wrong isdotdot parent"));
639 if (ncp->nc_vp != NULL)
640 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst,
643 TAILQ_REMOVE(&ncneg, ncp, nc_dst);
645 TAILQ_INSERT_HEAD(&vp->v_cache_dst,
648 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
653 dvp->v_cache_dd = NULL;
654 SDT_PROBE(vfs, namecache, enter, done, dvp, "..", vp,
665 * Calculate the hash key and setup as much of the new
666 * namecache entry as possible before acquiring the lock.
668 ncp = cache_alloc(cnp->cn_namelen);
672 len = ncp->nc_nlen = cnp->cn_namelen;
673 hash = fnv_32_buf(cnp->cn_nameptr, len, FNV1_32_INIT);
674 strlcpy(ncp->nc_name, cnp->cn_nameptr, len + 1);
675 hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
679 * See if this vnode or negative entry is already in the cache
680 * with this name. This can happen with concurrent lookups of
681 * the same path name.
683 ncpp = NCHHASH(hash);
684 LIST_FOREACH(n2, ncpp, nc_hash) {
685 if (n2->nc_dvp == dvp &&
686 n2->nc_nlen == cnp->cn_namelen &&
687 !bcmp(n2->nc_name, cnp->cn_nameptr, n2->nc_nlen)) {
694 if (flag == NCF_ISDOTDOT) {
696 * See if we are trying to add .. entry, but some other lookup
697 * has populated v_cache_dd pointer already.
699 if (dvp->v_cache_dd != NULL) {
704 KASSERT(vp == NULL || vp->v_type == VDIR,
705 ("wrong vnode type %p", vp));
706 dvp->v_cache_dd = ncp;
712 if (cnp->cn_flags & ISWHITEOUT)
713 ncp->nc_flag |= NCF_WHITE;
714 } else if (vp->v_type == VDIR) {
715 if (flag != NCF_ISDOTDOT) {
716 if ((n2 = vp->v_cache_dd) != NULL &&
717 (n2->nc_flag & NCF_ISDOTDOT) != 0)
719 vp->v_cache_dd = ncp;
722 vp->v_cache_dd = NULL;
726 * Insert the new namecache entry into the appropriate chain
727 * within the cache entries table.
729 LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
730 if (flag != NCF_ISDOTDOT) {
731 if (LIST_EMPTY(&dvp->v_cache_src)) {
735 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
739 * If the entry is "negative", we place it into the
740 * "negative" cache queue, otherwise, we place it into the
741 * destination vnode's cache entries queue.
744 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
745 SDT_PROBE(vfs, namecache, enter, done, dvp, ncp->nc_name, vp,
748 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
749 SDT_PROBE(vfs, namecache, enter_negative, done, dvp,
750 ncp->nc_name, 0, 0, 0);
752 if (numneg * ncnegfactor > numcache) {
753 ncp = TAILQ_FIRST(&ncneg);
764 * Name cache initialization, from vfs_init() when we are booting
767 nchinit(void *dummy __unused)
772 cache_zone_small = uma_zcreate("S VFS Cache", CACHE_ZONE_SMALL, NULL,
773 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
774 cache_zone_large = uma_zcreate("L VFS Cache", CACHE_ZONE_LARGE, NULL,
775 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
777 nchashtbl = hashinit(desiredvnodes * 2, M_VFSCACHE, &nchash);
779 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL);
783 * Invalidate all entries to a particular vnode.
790 CTR1(KTR_VFS, "cache_purge(%p)", vp);
791 SDT_PROBE(vfs, namecache, purge, done, vp, 0, 0, 0, 0);
793 while (!LIST_EMPTY(&vp->v_cache_src))
794 cache_zap(LIST_FIRST(&vp->v_cache_src));
795 while (!TAILQ_EMPTY(&vp->v_cache_dst))
796 cache_zap(TAILQ_FIRST(&vp->v_cache_dst));
797 if (vp->v_cache_dd != NULL) {
798 KASSERT(vp->v_cache_dd->nc_flag & NCF_ISDOTDOT,
799 ("lost dotdot link"));
800 cache_zap(vp->v_cache_dd);
802 KASSERT(vp->v_cache_dd == NULL, ("incomplete purge"));
807 * Invalidate all negative entries for a particular directory vnode.
810 cache_purge_negative(vp)
813 struct namecache *cp, *ncp;
815 CTR1(KTR_VFS, "cache_purge_negative(%p)", vp);
816 SDT_PROBE(vfs, namecache, purge_negative, done, vp, 0, 0, 0, 0);
818 LIST_FOREACH_SAFE(cp, &vp->v_cache_src, nc_src, ncp) {
819 if (cp->nc_vp == NULL)
826 * Flush all entries referencing a particular filesystem.
832 struct nchashhead *ncpp;
833 struct namecache *ncp, *nnp;
835 /* Scan hash tables for applicable entries */
836 SDT_PROBE(vfs, namecache, purgevfs, done, mp, 0, 0, 0, 0);
838 for (ncpp = &nchashtbl[nchash]; ncpp >= nchashtbl; ncpp--) {
839 LIST_FOREACH_SAFE(ncp, ncpp, nc_hash, nnp) {
840 if (ncp->nc_dvp->v_mount == mp)
848 * Perform canonical checks and cache lookup and pass on to filesystem
849 * through the vop_cachedlookup only if needed.
854 struct vop_lookup_args /* {
856 struct vnode **a_vpp;
857 struct componentname *a_cnp;
862 struct vnode **vpp = ap->a_vpp;
863 struct componentname *cnp = ap->a_cnp;
864 struct ucred *cred = cnp->cn_cred;
865 int flags = cnp->cn_flags;
866 struct thread *td = cnp->cn_thread;
871 if (dvp->v_type != VDIR)
874 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
875 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
878 error = VOP_ACCESS(dvp, VEXEC, cred, td);
882 error = cache_lookup(dvp, vpp, cnp);
884 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
891 #ifndef _SYS_SYSPROTO_H_
892 struct __getcwd_args {
899 * XXX All of these sysctls would probably be more productive dead.
901 static int disablecwd;
902 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0,
903 "Disable the getcwd syscall");
905 /* Implementation of the getcwd syscall. */
909 struct __getcwd_args *uap;
912 return (kern___getcwd(td, uap->buf, UIO_USERSPACE, uap->buflen));
916 kern___getcwd(struct thread *td, u_char *buf, enum uio_seg bufseg, u_int buflen)
919 struct filedesc *fdp;
920 struct vnode *cdir, *rdir;
921 int error, vfslocked;
927 if (buflen > MAXPATHLEN)
930 tmpbuf = malloc(buflen, M_TEMP, M_WAITOK);
931 fdp = td->td_proc->p_fd;
937 FILEDESC_SUNLOCK(fdp);
938 error = vn_fullpath1(td, cdir, rdir, tmpbuf, &bp, buflen);
939 vfslocked = VFS_LOCK_GIANT(rdir->v_mount);
941 VFS_UNLOCK_GIANT(vfslocked);
942 vfslocked = VFS_LOCK_GIANT(cdir->v_mount);
944 VFS_UNLOCK_GIANT(vfslocked);
947 if (bufseg == UIO_SYSSPACE)
948 bcopy(bp, buf, strlen(bp) + 1);
950 error = copyout(bp, buf, strlen(bp) + 1);
952 if (KTRPOINT(curthread, KTR_NAMEI))
956 free(tmpbuf, M_TEMP);
961 * Thus begins the fullpath magic.
965 #define STATNODE(name) \
967 SYSCTL_UINT(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, "")
969 static int disablefullpath;
970 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW, &disablefullpath, 0,
971 "Disable the vn_fullpath function");
973 /* These count for kern___getcwd(), too. */
974 STATNODE(numfullpathcalls);
975 STATNODE(numfullpathfail1);
976 STATNODE(numfullpathfail2);
977 STATNODE(numfullpathfail4);
978 STATNODE(numfullpathfound);
981 * Retrieve the full filesystem path that correspond to a vnode from the name
982 * cache (if available)
985 vn_fullpath(struct thread *td, struct vnode *vn, char **retbuf, char **freebuf)
988 struct filedesc *fdp;
990 int error, vfslocked;
997 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
998 fdp = td->td_proc->p_fd;
1000 rdir = fdp->fd_rdir;
1002 FILEDESC_SUNLOCK(fdp);
1003 error = vn_fullpath1(td, vn, rdir, buf, retbuf, MAXPATHLEN);
1004 vfslocked = VFS_LOCK_GIANT(rdir->v_mount);
1006 VFS_UNLOCK_GIANT(vfslocked);
1016 * This function is similar to vn_fullpath, but it attempts to lookup the
1017 * pathname relative to the global root mount point. This is required for the
1018 * auditing sub-system, as audited pathnames must be absolute, relative to the
1019 * global root mount point.
1022 vn_fullpath_global(struct thread *td, struct vnode *vn,
1023 char **retbuf, char **freebuf)
1028 if (disablefullpath)
1032 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1033 error = vn_fullpath1(td, vn, rootvnode, buf, retbuf, MAXPATHLEN);
1042 vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, u_int *buflen)
1047 error = vn_vptocnp_locked(vp, cred, buf, buflen);
1050 * vn_vptocnp_locked() dropped hold acquired by
1051 * VOP_VPTOCNP immediately after locking the
1052 * cache. Since we are going to drop the cache rlock,
1053 * re-hold the result.
1062 vn_vptocnp_locked(struct vnode **vp, struct ucred *cred, char *buf,
1066 struct namecache *ncp;
1067 int error, vfslocked;
1069 TAILQ_FOREACH(ncp, &((*vp)->v_cache_dst), nc_dst) {
1070 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1074 if (*buflen < ncp->nc_nlen) {
1078 SDT_PROBE(vfs, namecache, fullpath, return, error,
1082 *buflen -= ncp->nc_nlen;
1083 memcpy(buf + *buflen, ncp->nc_name, ncp->nc_nlen);
1084 SDT_PROBE(vfs, namecache, fullpath, hit, ncp->nc_dvp,
1085 ncp->nc_name, vp, 0, 0);
1089 SDT_PROBE(vfs, namecache, fullpath, miss, vp, 0, 0, 0, 0);
1093 vfslocked = VFS_LOCK_GIANT((*vp)->v_mount);
1094 vn_lock(*vp, LK_SHARED | LK_RETRY);
1095 error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen);
1098 VFS_UNLOCK_GIANT(vfslocked);
1101 SDT_PROBE(vfs, namecache, fullpath, return, error, vp,
1108 if ((*vp)->v_iflag & VI_DOOMED) {
1109 /* forced unmount */
1113 SDT_PROBE(vfs, namecache, fullpath, return, error, vp,
1123 * The magic behind kern___getcwd() and vn_fullpath().
1126 vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
1127 char *buf, char **retbuf, u_int buflen)
1129 int error, slash_prefixed;
1130 #ifdef KDTRACE_HOOKS
1131 struct vnode *startvp = vp;
1139 SDT_PROBE(vfs, namecache, fullpath, entry, vp, 0, 0, 0, 0);
1142 if (vp->v_type != VDIR) {
1143 error = vn_vptocnp_locked(&vp, td->td_ucred, buf, &buflen);
1150 buf[--buflen] = '/';
1153 while (vp != rdir && vp != rootvnode) {
1154 if (vp->v_vflag & VV_ROOT) {
1155 if (vp->v_iflag & VI_DOOMED) { /* forced unmount */
1158 SDT_PROBE(vfs, namecache, fullpath, return,
1159 error, vp, NULL, 0, 0);
1162 vp = vp->v_mount->mnt_vnodecovered;
1165 if (vp->v_type != VDIR) {
1169 SDT_PROBE(vfs, namecache, fullpath, return,
1170 error, vp, NULL, 0, 0);
1173 error = vn_vptocnp_locked(&vp, td->td_ucred, buf, &buflen);
1179 SDT_PROBE(vfs, namecache, fullpath, return, error,
1180 startvp, NULL, 0, 0);
1183 buf[--buflen] = '/';
1188 if (!slash_prefixed) {
1192 SDT_PROBE(vfs, namecache, fullpath, return, ENOMEM,
1193 startvp, NULL, 0, 0);
1196 buf[--buflen] = '/';
1201 SDT_PROBE(vfs, namecache, fullpath, return, 0, startvp, buf + buflen,
1203 *retbuf = buf + buflen;
1208 vn_commname(struct vnode *vp, char *buf, u_int buflen)
1210 struct namecache *ncp;
1214 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst)
1215 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1221 l = min(ncp->nc_nlen, buflen - 1);
1222 memcpy(buf, ncp->nc_name, l);