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 "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), "");
192 * The new name cache statistics
194 static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0, "Name cache statistics");
195 #define STATNODE(mode, name, var) \
196 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, mode, var, 0, "");
197 STATNODE(CTLFLAG_RD, numneg, &numneg);
198 STATNODE(CTLFLAG_RD, numcache, &numcache);
199 static u_long numcalls; STATNODE(CTLFLAG_RD, numcalls, &numcalls);
200 static u_long dothits; STATNODE(CTLFLAG_RD, dothits, &dothits);
201 static u_long dotdothits; STATNODE(CTLFLAG_RD, dotdothits, &dotdothits);
202 static u_long numchecks; STATNODE(CTLFLAG_RD, numchecks, &numchecks);
203 static u_long nummiss; STATNODE(CTLFLAG_RD, nummiss, &nummiss);
204 static u_long nummisszap; STATNODE(CTLFLAG_RD, nummisszap, &nummisszap);
205 static u_long numposzaps; STATNODE(CTLFLAG_RD, numposzaps, &numposzaps);
206 static u_long numposhits; STATNODE(CTLFLAG_RD, numposhits, &numposhits);
207 static u_long numnegzaps; STATNODE(CTLFLAG_RD, numnegzaps, &numnegzaps);
208 static u_long numneghits; STATNODE(CTLFLAG_RD, numneghits, &numneghits);
209 static u_long numupgrades; STATNODE(CTLFLAG_RD, numupgrades, &numupgrades);
211 SYSCTL_OPAQUE(_vfs_cache, OID_AUTO, nchstats, CTLFLAG_RD | CTLFLAG_MPSAFE,
212 &nchstats, sizeof(nchstats), "LU", "VFS cache effectiveness statistics");
216 static void cache_zap(struct namecache *ncp);
217 static int vn_vptocnp_locked(struct vnode **vp, struct ucred *cred, char *buf,
219 static int vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
220 char *buf, char **retbuf, u_int buflen);
222 static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
225 * Flags in namecache.nc_flag
227 #define NCF_WHITE 0x01
228 #define NCF_ISDOTDOT 0x02
232 * Grab an atomic snapshot of the name cache hash chain lengths
234 SYSCTL_NODE(_debug, OID_AUTO, hashstat, CTLFLAG_RW, NULL, "hash table stats");
237 sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS)
240 struct nchashhead *ncpp;
241 struct namecache *ncp;
245 n_nchash = nchash + 1; /* nchash is max index, not count */
247 return SYSCTL_OUT(req, 0, n_nchash * sizeof(int));
249 /* Scan hash tables for applicable entries */
250 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
253 LIST_FOREACH(ncp, ncpp, nc_hash) {
257 error = SYSCTL_OUT(req, &count, sizeof(count));
263 SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD|
264 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_rawnchash, "S,int",
265 "nchash chain lengths");
268 sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS)
271 struct nchashhead *ncpp;
272 struct namecache *ncp;
274 int count, maxlength, used, pct;
277 return SYSCTL_OUT(req, 0, 4 * sizeof(int));
279 n_nchash = nchash + 1; /* nchash is max index, not count */
283 /* Scan hash tables for applicable entries */
284 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
287 LIST_FOREACH(ncp, ncpp, nc_hash) {
293 if (maxlength < count)
296 n_nchash = nchash + 1;
297 pct = (used * 100 * 100) / n_nchash;
298 error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash));
301 error = SYSCTL_OUT(req, &used, sizeof(used));
304 error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength));
307 error = SYSCTL_OUT(req, &pct, sizeof(pct));
312 SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD|
313 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_nchash, "I",
314 "nchash chain lengths");
320 * Removes a namecache entry from cache, whether it contains an actual
321 * pointer to a vnode or if it is just a negative cache entry.
325 struct namecache *ncp;
329 rw_assert(&cache_lock, RA_WLOCKED);
330 CTR2(KTR_VFS, "cache_zap(%p) vp %p", ncp, ncp->nc_vp);
332 if (ncp->nc_vp != NULL) {
333 SDT_PROBE(vfs, namecache, zap, done, ncp->nc_dvp,
334 ncp->nc_name, ncp->nc_vp, 0, 0);
336 SDT_PROBE(vfs, namecache, zap_negative, done, ncp->nc_dvp,
337 ncp->nc_name, 0, 0, 0);
341 LIST_REMOVE(ncp, nc_hash);
342 if (ncp->nc_flag & NCF_ISDOTDOT) {
343 if (ncp == ncp->nc_dvp->v_cache_dd)
344 ncp->nc_dvp->v_cache_dd = NULL;
346 LIST_REMOVE(ncp, nc_src);
347 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) {
353 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
354 if (ncp == ncp->nc_vp->v_cache_dd)
355 ncp->nc_vp->v_cache_dd = NULL;
357 TAILQ_REMOVE(&ncneg, ncp, nc_dst);
367 * Lookup an entry in the cache
369 * Lookup is called with dvp pointing to the directory to search,
370 * cnp pointing to the name of the entry being sought. If the lookup
371 * succeeds, the vnode is returned in *vpp, and a status of -1 is
372 * returned. If the lookup determines that the name does not exist
373 * (negative cacheing), a status of ENOENT is returned. If the lookup
374 * fails, a status of zero is returned. If the directory vnode is
375 * recycled out from under us due to a forced unmount, a status of
376 * ENOENT is returned.
378 * vpp is locked and ref'd on return. If we're looking up DOTDOT, dvp is
379 * unlocked. If we're looking up . an extra ref is taken, but the lock is
380 * not recursively acquired.
384 cache_lookup(dvp, vpp, cnp)
387 struct componentname *cnp;
389 struct namecache *ncp;
391 int error, ltype, wlocked;
394 cnp->cn_flags &= ~MAKEENTRY;
404 if (cnp->cn_nameptr[0] == '.') {
405 if (cnp->cn_namelen == 1) {
407 CTR2(KTR_VFS, "cache_lookup(%p, %s) found via .",
408 dvp, cnp->cn_nameptr);
410 SDT_PROBE(vfs, namecache, lookup, hit, dvp, ".",
414 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
416 if (dvp->v_cache_dd == NULL) {
417 SDT_PROBE(vfs, namecache, lookup, miss, dvp,
421 if ((cnp->cn_flags & MAKEENTRY) == 0) {
422 if (!wlocked && !CACHE_UPGRADE_LOCK())
424 if (dvp->v_cache_dd->nc_flag & NCF_ISDOTDOT)
425 cache_zap(dvp->v_cache_dd);
426 dvp->v_cache_dd = NULL;
430 if (dvp->v_cache_dd->nc_flag & NCF_ISDOTDOT)
431 *vpp = dvp->v_cache_dd->nc_vp;
433 *vpp = dvp->v_cache_dd->nc_dvp;
434 /* Return failure if negative entry was found. */
436 ncp = dvp->v_cache_dd;
437 goto negative_success;
439 CTR3(KTR_VFS, "cache_lookup(%p, %s) found %p via ..",
440 dvp, cnp->cn_nameptr, *vpp);
441 SDT_PROBE(vfs, namecache, lookup, hit, dvp, "..",
447 hash = fnv_32_buf(cnp->cn_nameptr, cnp->cn_namelen, FNV1_32_INIT);
448 hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
449 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
451 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
452 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
456 /* We failed to find an entry */
458 SDT_PROBE(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
460 if ((cnp->cn_flags & MAKEENTRY) == 0) {
469 /* We don't want to have an entry, so dump it */
470 if ((cnp->cn_flags & MAKEENTRY) == 0) {
472 nchstats.ncs_badhits++;
473 if (!wlocked && !CACHE_UPGRADE_LOCK())
480 /* We found a "positive" match, return the vnode */
483 nchstats.ncs_goodhits++;
485 CTR4(KTR_VFS, "cache_lookup(%p, %s) found %p via ncp %p",
486 dvp, cnp->cn_nameptr, *vpp, ncp);
487 SDT_PROBE(vfs, namecache, lookup, hit, dvp, ncp->nc_name,
493 /* We found a negative match, and want to create it, so purge */
494 if (cnp->cn_nameiop == CREATE) {
496 nchstats.ncs_badhits++;
497 if (!wlocked && !CACHE_UPGRADE_LOCK())
504 if (!wlocked && !CACHE_UPGRADE_LOCK())
508 * We found a "negative" match, so we shift it to the end of
509 * the "negative" cache entries queue to satisfy LRU. Also,
510 * check to see if the entry is a whiteout; indicate this to
511 * the componentname, if so.
513 TAILQ_REMOVE(&ncneg, ncp, nc_dst);
514 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
515 nchstats.ncs_neghits++;
516 if (ncp->nc_flag & NCF_WHITE)
517 cnp->cn_flags |= ISWHITEOUT;
518 SDT_PROBE(vfs, namecache, lookup, hit_negative, dvp, ncp->nc_name,
525 * We need to update the cache after our lookup, so upgrade to
526 * a write lock and retry the operation.
536 * On success we return a locked and ref'd vnode as per the lookup
539 if (dvp == *vpp) { /* lookup on "." */
546 * When we lookup "." we still can be asked to lock it
549 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
550 if (ltype != VOP_ISLOCKED(*vpp)) {
551 if (ltype == LK_EXCLUSIVE) {
552 vn_lock(*vpp, LK_UPGRADE | LK_RETRY);
553 if ((*vpp)->v_iflag & VI_DOOMED) {
560 vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY);
564 ltype = 0; /* silence gcc warning */
565 if (cnp->cn_flags & ISDOTDOT) {
566 ltype = VOP_ISLOCKED(dvp);
574 error = vget(*vpp, cnp->cn_lkflags | LK_INTERLOCK, cnp->cn_thread);
575 if (cnp->cn_flags & ISDOTDOT) {
576 vn_lock(dvp, ltype | LK_RETRY);
577 if (dvp->v_iflag & VI_DOOMED) {
588 if ((cnp->cn_flags & ISLASTCN) &&
589 (cnp->cn_lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE) {
590 ASSERT_VOP_ELOCKED(*vpp, "cache_lookup");
603 * Add an entry to the cache.
606 cache_enter(dvp, vp, cnp)
609 struct componentname *cnp;
611 struct namecache *ncp, *n2;
612 struct nchashhead *ncpp;
619 CTR3(KTR_VFS, "cache_enter(%p, %p, %s)", dvp, vp, cnp->cn_nameptr);
620 VNASSERT(vp == NULL || (vp->v_iflag & VI_DOOMED) == 0, vp,
621 ("cache_enter: Adding a doomed vnode"));
622 VNASSERT(dvp == NULL || (dvp->v_iflag & VI_DOOMED) == 0, dvp,
623 ("cache_enter: Doomed vnode used as src"));
629 * Avoid blowout in namecache entries.
631 if (numcache >= desiredvnodes * ncsizefactor)
635 if (cnp->cn_nameptr[0] == '.') {
636 if (cnp->cn_namelen == 1)
638 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
641 * If dotdot entry already exists, just retarget it
642 * to new parent vnode, otherwise continue with new
643 * namecache entry allocation.
645 if ((ncp = dvp->v_cache_dd) != NULL &&
646 ncp->nc_flag & NCF_ISDOTDOT) {
647 KASSERT(ncp->nc_dvp == dvp,
648 ("wrong isdotdot parent"));
649 if (ncp->nc_vp != NULL)
650 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst,
653 TAILQ_REMOVE(&ncneg, ncp, nc_dst);
655 TAILQ_INSERT_HEAD(&vp->v_cache_dst,
658 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
663 dvp->v_cache_dd = NULL;
664 SDT_PROBE(vfs, namecache, enter, done, dvp, "..", vp,
675 * Calculate the hash key and setup as much of the new
676 * namecache entry as possible before acquiring the lock.
678 ncp = cache_alloc(cnp->cn_namelen);
682 len = ncp->nc_nlen = cnp->cn_namelen;
683 hash = fnv_32_buf(cnp->cn_nameptr, len, FNV1_32_INIT);
684 strlcpy(ncp->nc_name, cnp->cn_nameptr, len + 1);
685 hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
689 * See if this vnode or negative entry is already in the cache
690 * with this name. This can happen with concurrent lookups of
691 * the same path name.
693 ncpp = NCHHASH(hash);
694 LIST_FOREACH(n2, ncpp, nc_hash) {
695 if (n2->nc_dvp == dvp &&
696 n2->nc_nlen == cnp->cn_namelen &&
697 !bcmp(n2->nc_name, cnp->cn_nameptr, n2->nc_nlen)) {
704 if (flag == NCF_ISDOTDOT) {
706 * See if we are trying to add .. entry, but some other lookup
707 * has populated v_cache_dd pointer already.
709 if (dvp->v_cache_dd != NULL) {
714 KASSERT(vp == NULL || vp->v_type == VDIR,
715 ("wrong vnode type %p", vp));
716 dvp->v_cache_dd = ncp;
722 if (cnp->cn_flags & ISWHITEOUT)
723 ncp->nc_flag |= NCF_WHITE;
724 } else if (vp->v_type == VDIR) {
725 if (flag != NCF_ISDOTDOT) {
726 if ((n2 = vp->v_cache_dd) != NULL &&
727 (n2->nc_flag & NCF_ISDOTDOT) != 0)
729 vp->v_cache_dd = ncp;
732 vp->v_cache_dd = NULL;
736 * Insert the new namecache entry into the appropriate chain
737 * within the cache entries table.
739 LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
740 if (flag != NCF_ISDOTDOT) {
741 if (LIST_EMPTY(&dvp->v_cache_src)) {
745 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
749 * If the entry is "negative", we place it into the
750 * "negative" cache queue, otherwise, we place it into the
751 * destination vnode's cache entries queue.
754 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
755 SDT_PROBE(vfs, namecache, enter, done, dvp, ncp->nc_name, vp,
758 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
759 SDT_PROBE(vfs, namecache, enter_negative, done, dvp,
760 ncp->nc_name, 0, 0, 0);
762 if (numneg * ncnegfactor > numcache) {
763 ncp = TAILQ_FIRST(&ncneg);
774 * Name cache initialization, from vfs_init() when we are booting
777 nchinit(void *dummy __unused)
782 cache_zone_small = uma_zcreate("S VFS Cache", CACHE_ZONE_SMALL, NULL,
783 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
784 cache_zone_large = uma_zcreate("L VFS Cache", CACHE_ZONE_LARGE, NULL,
785 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
787 nchashtbl = hashinit(desiredvnodes * 2, M_VFSCACHE, &nchash);
789 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL);
793 * Invalidate all entries to a particular vnode.
800 CTR1(KTR_VFS, "cache_purge(%p)", vp);
801 SDT_PROBE(vfs, namecache, purge, done, vp, 0, 0, 0, 0);
803 while (!LIST_EMPTY(&vp->v_cache_src))
804 cache_zap(LIST_FIRST(&vp->v_cache_src));
805 while (!TAILQ_EMPTY(&vp->v_cache_dst))
806 cache_zap(TAILQ_FIRST(&vp->v_cache_dst));
807 if (vp->v_cache_dd != NULL) {
808 KASSERT(vp->v_cache_dd->nc_flag & NCF_ISDOTDOT,
809 ("lost dotdot link"));
810 cache_zap(vp->v_cache_dd);
812 KASSERT(vp->v_cache_dd == NULL, ("incomplete purge"));
817 * Invalidate all negative entries for a particular directory vnode.
820 cache_purge_negative(vp)
823 struct namecache *cp, *ncp;
825 CTR1(KTR_VFS, "cache_purge_negative(%p)", vp);
826 SDT_PROBE(vfs, namecache, purge_negative, done, vp, 0, 0, 0, 0);
828 LIST_FOREACH_SAFE(cp, &vp->v_cache_src, nc_src, ncp) {
829 if (cp->nc_vp == NULL)
836 * Flush all entries referencing a particular filesystem.
842 struct nchashhead *ncpp;
843 struct namecache *ncp, *nnp;
845 /* Scan hash tables for applicable entries */
846 SDT_PROBE(vfs, namecache, purgevfs, done, mp, 0, 0, 0, 0);
848 for (ncpp = &nchashtbl[nchash]; ncpp >= nchashtbl; ncpp--) {
849 LIST_FOREACH_SAFE(ncp, ncpp, nc_hash, nnp) {
850 if (ncp->nc_dvp->v_mount == mp)
858 * Perform canonical checks and cache lookup and pass on to filesystem
859 * through the vop_cachedlookup only if needed.
864 struct vop_lookup_args /* {
866 struct vnode **a_vpp;
867 struct componentname *a_cnp;
872 struct vnode **vpp = ap->a_vpp;
873 struct componentname *cnp = ap->a_cnp;
874 struct ucred *cred = cnp->cn_cred;
875 int flags = cnp->cn_flags;
876 struct thread *td = cnp->cn_thread;
881 if (dvp->v_type != VDIR)
884 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
885 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
888 error = VOP_ACCESS(dvp, VEXEC, cred, td);
892 error = cache_lookup(dvp, vpp, cnp);
894 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
901 #ifndef _SYS_SYSPROTO_H_
902 struct __getcwd_args {
909 * XXX All of these sysctls would probably be more productive dead.
911 static int disablecwd;
912 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0,
913 "Disable the getcwd syscall");
915 /* Implementation of the getcwd syscall. */
919 struct __getcwd_args *uap;
922 return (kern___getcwd(td, uap->buf, UIO_USERSPACE, uap->buflen));
926 kern___getcwd(struct thread *td, u_char *buf, enum uio_seg bufseg, u_int buflen)
929 struct filedesc *fdp;
930 struct vnode *cdir, *rdir;
931 int error, vfslocked;
937 if (buflen > MAXPATHLEN)
940 tmpbuf = malloc(buflen, M_TEMP, M_WAITOK);
941 fdp = td->td_proc->p_fd;
947 FILEDESC_SUNLOCK(fdp);
948 error = vn_fullpath1(td, cdir, rdir, tmpbuf, &bp, buflen);
949 vfslocked = VFS_LOCK_GIANT(rdir->v_mount);
951 VFS_UNLOCK_GIANT(vfslocked);
952 vfslocked = VFS_LOCK_GIANT(cdir->v_mount);
954 VFS_UNLOCK_GIANT(vfslocked);
957 if (bufseg == UIO_SYSSPACE)
958 bcopy(bp, buf, strlen(bp) + 1);
960 error = copyout(bp, buf, strlen(bp) + 1);
962 if (KTRPOINT(curthread, KTR_NAMEI))
966 free(tmpbuf, M_TEMP);
971 * Thus begins the fullpath magic.
975 #define STATNODE(name) \
977 SYSCTL_UINT(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, "")
979 static int disablefullpath;
980 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW, &disablefullpath, 0,
981 "Disable the vn_fullpath function");
983 /* These count for kern___getcwd(), too. */
984 STATNODE(numfullpathcalls);
985 STATNODE(numfullpathfail1);
986 STATNODE(numfullpathfail2);
987 STATNODE(numfullpathfail4);
988 STATNODE(numfullpathfound);
991 * Retrieve the full filesystem path that correspond to a vnode from the name
992 * cache (if available)
995 vn_fullpath(struct thread *td, struct vnode *vn, char **retbuf, char **freebuf)
998 struct filedesc *fdp;
1000 int error, vfslocked;
1002 if (disablefullpath)
1007 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1008 fdp = td->td_proc->p_fd;
1009 FILEDESC_SLOCK(fdp);
1010 rdir = fdp->fd_rdir;
1012 FILEDESC_SUNLOCK(fdp);
1013 error = vn_fullpath1(td, vn, rdir, buf, retbuf, MAXPATHLEN);
1014 vfslocked = VFS_LOCK_GIANT(rdir->v_mount);
1016 VFS_UNLOCK_GIANT(vfslocked);
1026 * This function is similar to vn_fullpath, but it attempts to lookup the
1027 * pathname relative to the global root mount point. This is required for the
1028 * auditing sub-system, as audited pathnames must be absolute, relative to the
1029 * global root mount point.
1032 vn_fullpath_global(struct thread *td, struct vnode *vn,
1033 char **retbuf, char **freebuf)
1038 if (disablefullpath)
1042 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1043 error = vn_fullpath1(td, vn, rootvnode, buf, retbuf, MAXPATHLEN);
1052 vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, u_int *buflen)
1057 error = vn_vptocnp_locked(vp, cred, buf, buflen);
1060 * vn_vptocnp_locked() dropped hold acquired by
1061 * VOP_VPTOCNP immediately after locking the
1062 * cache. Since we are going to drop the cache rlock,
1063 * re-hold the result.
1072 vn_vptocnp_locked(struct vnode **vp, struct ucred *cred, char *buf,
1076 struct namecache *ncp;
1077 int error, vfslocked;
1079 TAILQ_FOREACH(ncp, &((*vp)->v_cache_dst), nc_dst) {
1080 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1084 if (*buflen < ncp->nc_nlen) {
1088 SDT_PROBE(vfs, namecache, fullpath, return, error,
1092 *buflen -= ncp->nc_nlen;
1093 memcpy(buf + *buflen, ncp->nc_name, ncp->nc_nlen);
1094 SDT_PROBE(vfs, namecache, fullpath, hit, ncp->nc_dvp,
1095 ncp->nc_name, vp, 0, 0);
1099 SDT_PROBE(vfs, namecache, fullpath, miss, vp, 0, 0, 0, 0);
1103 vfslocked = VFS_LOCK_GIANT((*vp)->v_mount);
1104 vn_lock(*vp, LK_SHARED | LK_RETRY);
1105 error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen);
1108 VFS_UNLOCK_GIANT(vfslocked);
1111 SDT_PROBE(vfs, namecache, fullpath, return, error, vp,
1118 if ((*vp)->v_iflag & VI_DOOMED) {
1119 /* forced unmount */
1123 SDT_PROBE(vfs, namecache, fullpath, return, error, vp,
1133 * The magic behind kern___getcwd() and vn_fullpath().
1136 vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
1137 char *buf, char **retbuf, u_int buflen)
1139 int error, slash_prefixed;
1140 #ifdef KDTRACE_HOOKS
1141 struct vnode *startvp = vp;
1149 SDT_PROBE(vfs, namecache, fullpath, entry, vp, 0, 0, 0, 0);
1152 if (vp->v_type != VDIR) {
1153 error = vn_vptocnp_locked(&vp, td->td_ucred, buf, &buflen);
1160 buf[--buflen] = '/';
1163 while (vp != rdir && vp != rootvnode) {
1164 if (vp->v_vflag & VV_ROOT) {
1165 if (vp->v_iflag & VI_DOOMED) { /* forced unmount */
1168 SDT_PROBE(vfs, namecache, fullpath, return,
1169 error, vp, NULL, 0, 0);
1172 vp = vp->v_mount->mnt_vnodecovered;
1175 if (vp->v_type != VDIR) {
1179 SDT_PROBE(vfs, namecache, fullpath, return,
1180 error, vp, NULL, 0, 0);
1183 error = vn_vptocnp_locked(&vp, td->td_ucred, buf, &buflen);
1189 SDT_PROBE(vfs, namecache, fullpath, return, error,
1190 startvp, NULL, 0, 0);
1193 buf[--buflen] = '/';
1198 if (!slash_prefixed) {
1202 SDT_PROBE(vfs, namecache, fullpath, return, ENOMEM,
1203 startvp, NULL, 0, 0);
1206 buf[--buflen] = '/';
1211 SDT_PROBE(vfs, namecache, fullpath, return, 0, startvp, buf + buflen,
1213 *retbuf = buf + buflen;
1218 vn_commname(struct vnode *vp, char *buf, u_int buflen)
1220 struct namecache *ncp;
1224 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst)
1225 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1231 l = min(ncp->nc_nlen, buflen - 1);
1232 memcpy(buf, ncp->nc_name, l);