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, 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 SYSCTL_ULONG(_vfs, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0,
133 "Ratio of negative namecache entries");
134 static u_long numneg; /* number of negative entries allocated */
135 SYSCTL_ULONG(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0,
136 "Number of negative entries in namecache");
137 static u_long numcache; /* number of cache entries allocated */
138 SYSCTL_ULONG(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0,
139 "Number of namecache entries");
140 static u_long numcachehv; /* number of cache entries with vnodes held */
141 SYSCTL_ULONG(_debug, OID_AUTO, numcachehv, CTLFLAG_RD, &numcachehv, 0,
142 "Number of namecache entries with vnodes held");
143 static u_int ncsizefactor = 2;
144 SYSCTL_UINT(_vfs, OID_AUTO, ncsizefactor, CTLFLAG_RW, &ncsizefactor, 0,
145 "Size factor for namecache");
147 struct nchstats nchstats; /* cache effectiveness statistics */
149 static struct rwlock cache_lock;
150 RW_SYSINIT(vfscache, &cache_lock, "Name Cache");
152 #define CACHE_UPGRADE_LOCK() rw_try_upgrade(&cache_lock)
153 #define CACHE_RLOCK() rw_rlock(&cache_lock)
154 #define CACHE_RUNLOCK() rw_runlock(&cache_lock)
155 #define CACHE_WLOCK() rw_wlock(&cache_lock)
156 #define CACHE_WUNLOCK() rw_wunlock(&cache_lock)
159 * UMA zones for the VFS cache.
161 * The small cache is used for entries with short names, which are the
162 * most common. The large cache is used for entries which are too big to
163 * fit in the small cache.
165 static uma_zone_t cache_zone_small;
166 static uma_zone_t cache_zone_large;
168 #define CACHE_PATH_CUTOFF 35
169 #define CACHE_ZONE_SMALL (sizeof(struct namecache) + CACHE_PATH_CUTOFF \
171 #define CACHE_ZONE_LARGE (sizeof(struct namecache) + NAME_MAX + 1)
173 #define cache_alloc(len) uma_zalloc(((len) <= CACHE_PATH_CUTOFF) ? \
174 cache_zone_small : cache_zone_large, M_WAITOK)
175 #define cache_free(ncp) do { \
177 uma_zfree(((ncp)->nc_nlen <= CACHE_PATH_CUTOFF) ? \
178 cache_zone_small : cache_zone_large, (ncp)); \
181 static int doingcache = 1; /* 1 => enable the cache */
182 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0,
183 "VFS namecache enabled");
185 /* Export size information to userland */
186 SYSCTL_INT(_debug_sizeof, OID_AUTO, namecache, CTLFLAG_RD, 0,
187 sizeof(struct namecache), "sizeof(struct namecache)");
190 * The new name cache statistics
192 static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0,
193 "Name cache statistics");
194 #define STATNODE(mode, name, var, descr) \
195 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, mode, var, 0, descr);
196 STATNODE(CTLFLAG_RD, numneg, &numneg, "Number of negative cache entries");
197 STATNODE(CTLFLAG_RD, numcache, &numcache, "Number of cache entries");
198 static u_long numcalls; STATNODE(CTLFLAG_RD, numcalls, &numcalls,
199 "Number of cache lookups");
200 static u_long dothits; STATNODE(CTLFLAG_RD, dothits, &dothits,
201 "Number of '.' hits");
202 static u_long dotdothits; STATNODE(CTLFLAG_RD, dotdothits, &dotdothits,
203 "Number of '..' hits");
204 static u_long numchecks; STATNODE(CTLFLAG_RD, numchecks, &numchecks,
205 "Number of checks in lookup");
206 static u_long nummiss; STATNODE(CTLFLAG_RD, nummiss, &nummiss,
207 "Number of cache misses");
208 static u_long nummisszap; STATNODE(CTLFLAG_RD, nummisszap, &nummisszap,
209 "Number of cache misses we do not want to cache");
210 static u_long numposzaps; STATNODE(CTLFLAG_RD, numposzaps, &numposzaps,
211 "Number of cache hits (positive) we do not want to cache");
212 static u_long numposhits; STATNODE(CTLFLAG_RD, numposhits, &numposhits,
213 "Number of cache hits (positive)");
214 static u_long numnegzaps; STATNODE(CTLFLAG_RD, numnegzaps, &numnegzaps,
215 "Number of cache hits (negative) we do not want to cache");
216 static u_long numneghits; STATNODE(CTLFLAG_RD, numneghits, &numneghits,
217 "Number of cache hits (negative)");
218 static u_long numupgrades; STATNODE(CTLFLAG_RD, numupgrades, &numupgrades,
219 "Number of updates of the cache after lookup (write lock + retry)");
221 SYSCTL_OPAQUE(_vfs_cache, OID_AUTO, nchstats, CTLFLAG_RD | CTLFLAG_MPSAFE,
222 &nchstats, sizeof(nchstats), "LU",
223 "VFS cache effectiveness statistics");
227 static void cache_zap(struct namecache *ncp);
228 static int vn_vptocnp_locked(struct vnode **vp, struct ucred *cred, char *buf,
230 static int vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
231 char *buf, char **retbuf, u_int buflen);
233 static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
236 * Flags in namecache.nc_flag
238 #define NCF_WHITE 0x01
239 #define NCF_ISDOTDOT 0x02
243 * Grab an atomic snapshot of the name cache hash chain lengths
245 SYSCTL_NODE(_debug, OID_AUTO, hashstat, CTLFLAG_RW, NULL, "hash table stats");
248 sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS)
251 struct nchashhead *ncpp;
252 struct namecache *ncp;
256 n_nchash = nchash + 1; /* nchash is max index, not count */
258 return SYSCTL_OUT(req, 0, n_nchash * sizeof(int));
260 /* Scan hash tables for applicable entries */
261 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
264 LIST_FOREACH(ncp, ncpp, nc_hash) {
268 error = SYSCTL_OUT(req, &count, sizeof(count));
274 SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD|
275 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_rawnchash, "S,int",
276 "nchash chain lengths");
279 sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS)
282 struct nchashhead *ncpp;
283 struct namecache *ncp;
285 int count, maxlength, used, pct;
288 return SYSCTL_OUT(req, 0, 4 * sizeof(int));
290 n_nchash = nchash + 1; /* nchash is max index, not count */
294 /* Scan hash tables for applicable entries */
295 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
298 LIST_FOREACH(ncp, ncpp, nc_hash) {
304 if (maxlength < count)
307 n_nchash = nchash + 1;
308 pct = (used * 100 * 100) / n_nchash;
309 error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash));
312 error = SYSCTL_OUT(req, &used, sizeof(used));
315 error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength));
318 error = SYSCTL_OUT(req, &pct, sizeof(pct));
323 SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD|
324 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_nchash, "I",
325 "nchash chain lengths");
331 * Removes a namecache entry from cache, whether it contains an actual
332 * pointer to a vnode or if it is just a negative cache entry.
336 struct namecache *ncp;
340 rw_assert(&cache_lock, RA_WLOCKED);
341 CTR2(KTR_VFS, "cache_zap(%p) vp %p", ncp, ncp->nc_vp);
343 if (ncp->nc_vp != NULL) {
344 SDT_PROBE(vfs, namecache, zap, done, ncp->nc_dvp,
345 ncp->nc_name, ncp->nc_vp, 0, 0);
347 SDT_PROBE(vfs, namecache, zap_negative, done, ncp->nc_dvp,
348 ncp->nc_name, 0, 0, 0);
352 LIST_REMOVE(ncp, nc_hash);
353 if (ncp->nc_flag & NCF_ISDOTDOT) {
354 if (ncp == ncp->nc_dvp->v_cache_dd)
355 ncp->nc_dvp->v_cache_dd = NULL;
357 LIST_REMOVE(ncp, nc_src);
358 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) {
364 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
365 if (ncp == ncp->nc_vp->v_cache_dd)
366 ncp->nc_vp->v_cache_dd = NULL;
368 TAILQ_REMOVE(&ncneg, ncp, nc_dst);
378 * Lookup an entry in the cache
380 * Lookup is called with dvp pointing to the directory to search,
381 * cnp pointing to the name of the entry being sought. If the lookup
382 * succeeds, the vnode is returned in *vpp, and a status of -1 is
383 * returned. If the lookup determines that the name does not exist
384 * (negative cacheing), a status of ENOENT is returned. If the lookup
385 * fails, a status of zero is returned. If the directory vnode is
386 * recycled out from under us due to a forced unmount, a status of
387 * ENOENT is returned.
389 * vpp is locked and ref'd on return. If we're looking up DOTDOT, dvp is
390 * unlocked. If we're looking up . an extra ref is taken, but the lock is
391 * not recursively acquired.
395 cache_lookup(dvp, vpp, cnp)
398 struct componentname *cnp;
400 struct namecache *ncp;
402 int error, ltype, wlocked;
405 cnp->cn_flags &= ~MAKEENTRY;
415 if (cnp->cn_nameptr[0] == '.') {
416 if (cnp->cn_namelen == 1) {
418 CTR2(KTR_VFS, "cache_lookup(%p, %s) found via .",
419 dvp, cnp->cn_nameptr);
421 SDT_PROBE(vfs, namecache, lookup, hit, dvp, ".",
425 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
427 if (dvp->v_cache_dd == NULL) {
428 SDT_PROBE(vfs, namecache, lookup, miss, dvp,
432 if ((cnp->cn_flags & MAKEENTRY) == 0) {
433 if (!wlocked && !CACHE_UPGRADE_LOCK())
435 if (dvp->v_cache_dd->nc_flag & NCF_ISDOTDOT)
436 cache_zap(dvp->v_cache_dd);
437 dvp->v_cache_dd = NULL;
441 if (dvp->v_cache_dd->nc_flag & NCF_ISDOTDOT)
442 *vpp = dvp->v_cache_dd->nc_vp;
444 *vpp = dvp->v_cache_dd->nc_dvp;
445 /* Return failure if negative entry was found. */
447 ncp = dvp->v_cache_dd;
448 goto negative_success;
450 CTR3(KTR_VFS, "cache_lookup(%p, %s) found %p via ..",
451 dvp, cnp->cn_nameptr, *vpp);
452 SDT_PROBE(vfs, namecache, lookup, hit, dvp, "..",
458 hash = fnv_32_buf(cnp->cn_nameptr, cnp->cn_namelen, FNV1_32_INIT);
459 hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
460 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
462 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
463 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
467 /* We failed to find an entry */
469 SDT_PROBE(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
471 if ((cnp->cn_flags & MAKEENTRY) == 0) {
480 /* We don't want to have an entry, so dump it */
481 if ((cnp->cn_flags & MAKEENTRY) == 0) {
483 nchstats.ncs_badhits++;
484 if (!wlocked && !CACHE_UPGRADE_LOCK())
491 /* We found a "positive" match, return the vnode */
494 nchstats.ncs_goodhits++;
496 CTR4(KTR_VFS, "cache_lookup(%p, %s) found %p via ncp %p",
497 dvp, cnp->cn_nameptr, *vpp, ncp);
498 SDT_PROBE(vfs, namecache, lookup, hit, dvp, ncp->nc_name,
504 /* We found a negative match, and want to create it, so purge */
505 if (cnp->cn_nameiop == CREATE) {
507 nchstats.ncs_badhits++;
508 if (!wlocked && !CACHE_UPGRADE_LOCK())
515 if (!wlocked && !CACHE_UPGRADE_LOCK())
519 * We found a "negative" match, so we shift it to the end of
520 * the "negative" cache entries queue to satisfy LRU. Also,
521 * check to see if the entry is a whiteout; indicate this to
522 * the componentname, if so.
524 TAILQ_REMOVE(&ncneg, ncp, nc_dst);
525 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
526 nchstats.ncs_neghits++;
527 if (ncp->nc_flag & NCF_WHITE)
528 cnp->cn_flags |= ISWHITEOUT;
529 SDT_PROBE(vfs, namecache, lookup, hit_negative, dvp, ncp->nc_name,
536 * We need to update the cache after our lookup, so upgrade to
537 * a write lock and retry the operation.
547 * On success we return a locked and ref'd vnode as per the lookup
550 if (dvp == *vpp) { /* lookup on "." */
557 * When we lookup "." we still can be asked to lock it
560 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
561 if (ltype != VOP_ISLOCKED(*vpp)) {
562 if (ltype == LK_EXCLUSIVE) {
563 vn_lock(*vpp, LK_UPGRADE | LK_RETRY);
564 if ((*vpp)->v_iflag & VI_DOOMED) {
571 vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY);
575 ltype = 0; /* silence gcc warning */
576 if (cnp->cn_flags & ISDOTDOT) {
577 ltype = VOP_ISLOCKED(dvp);
585 error = vget(*vpp, cnp->cn_lkflags | LK_INTERLOCK, cnp->cn_thread);
586 if (cnp->cn_flags & ISDOTDOT) {
587 vn_lock(dvp, ltype | LK_RETRY);
588 if (dvp->v_iflag & VI_DOOMED) {
599 if ((cnp->cn_flags & ISLASTCN) &&
600 (cnp->cn_lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE) {
601 ASSERT_VOP_ELOCKED(*vpp, "cache_lookup");
614 * Add an entry to the cache.
617 cache_enter(dvp, vp, cnp)
620 struct componentname *cnp;
622 struct namecache *ncp, *n2;
623 struct nchashhead *ncpp;
630 CTR3(KTR_VFS, "cache_enter(%p, %p, %s)", dvp, vp, cnp->cn_nameptr);
631 VNASSERT(vp == NULL || (vp->v_iflag & VI_DOOMED) == 0, vp,
632 ("cache_enter: Adding a doomed vnode"));
633 VNASSERT(dvp == NULL || (dvp->v_iflag & VI_DOOMED) == 0, dvp,
634 ("cache_enter: Doomed vnode used as src"));
640 * Avoid blowout in namecache entries.
642 if (numcache >= desiredvnodes * ncsizefactor)
646 if (cnp->cn_nameptr[0] == '.') {
647 if (cnp->cn_namelen == 1)
649 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
652 * If dotdot entry already exists, just retarget it
653 * to new parent vnode, otherwise continue with new
654 * namecache entry allocation.
656 if ((ncp = dvp->v_cache_dd) != NULL &&
657 ncp->nc_flag & NCF_ISDOTDOT) {
658 KASSERT(ncp->nc_dvp == dvp,
659 ("wrong isdotdot parent"));
660 if (ncp->nc_vp != NULL)
661 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst,
664 TAILQ_REMOVE(&ncneg, ncp, nc_dst);
666 TAILQ_INSERT_HEAD(&vp->v_cache_dst,
669 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
674 dvp->v_cache_dd = NULL;
675 SDT_PROBE(vfs, namecache, enter, done, dvp, "..", vp,
686 * Calculate the hash key and setup as much of the new
687 * namecache entry as possible before acquiring the lock.
689 ncp = cache_alloc(cnp->cn_namelen);
693 len = ncp->nc_nlen = cnp->cn_namelen;
694 hash = fnv_32_buf(cnp->cn_nameptr, len, FNV1_32_INIT);
695 strlcpy(ncp->nc_name, cnp->cn_nameptr, len + 1);
696 hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
700 * See if this vnode or negative entry is already in the cache
701 * with this name. This can happen with concurrent lookups of
702 * the same path name.
704 ncpp = NCHHASH(hash);
705 LIST_FOREACH(n2, ncpp, nc_hash) {
706 if (n2->nc_dvp == dvp &&
707 n2->nc_nlen == cnp->cn_namelen &&
708 !bcmp(n2->nc_name, cnp->cn_nameptr, n2->nc_nlen)) {
715 if (flag == NCF_ISDOTDOT) {
717 * See if we are trying to add .. entry, but some other lookup
718 * has populated v_cache_dd pointer already.
720 if (dvp->v_cache_dd != NULL) {
725 KASSERT(vp == NULL || vp->v_type == VDIR,
726 ("wrong vnode type %p", vp));
727 dvp->v_cache_dd = ncp;
733 if (cnp->cn_flags & ISWHITEOUT)
734 ncp->nc_flag |= NCF_WHITE;
735 } else if (vp->v_type == VDIR) {
736 if (flag != NCF_ISDOTDOT) {
737 if ((n2 = vp->v_cache_dd) != NULL &&
738 (n2->nc_flag & NCF_ISDOTDOT) != 0)
740 vp->v_cache_dd = ncp;
743 vp->v_cache_dd = NULL;
747 * Insert the new namecache entry into the appropriate chain
748 * within the cache entries table.
750 LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
751 if (flag != NCF_ISDOTDOT) {
752 if (LIST_EMPTY(&dvp->v_cache_src)) {
756 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
760 * If the entry is "negative", we place it into the
761 * "negative" cache queue, otherwise, we place it into the
762 * destination vnode's cache entries queue.
765 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
766 SDT_PROBE(vfs, namecache, enter, done, dvp, ncp->nc_name, vp,
769 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
770 SDT_PROBE(vfs, namecache, enter_negative, done, dvp,
771 ncp->nc_name, 0, 0, 0);
773 if (numneg * ncnegfactor > numcache) {
774 ncp = TAILQ_FIRST(&ncneg);
785 * Name cache initialization, from vfs_init() when we are booting
788 nchinit(void *dummy __unused)
793 cache_zone_small = uma_zcreate("S VFS Cache", CACHE_ZONE_SMALL, NULL,
794 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
795 cache_zone_large = uma_zcreate("L VFS Cache", CACHE_ZONE_LARGE, NULL,
796 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
798 nchashtbl = hashinit(desiredvnodes * 2, M_VFSCACHE, &nchash);
800 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL);
804 * Invalidate all entries to a particular vnode.
811 CTR1(KTR_VFS, "cache_purge(%p)", vp);
812 SDT_PROBE(vfs, namecache, purge, done, vp, 0, 0, 0, 0);
814 while (!LIST_EMPTY(&vp->v_cache_src))
815 cache_zap(LIST_FIRST(&vp->v_cache_src));
816 while (!TAILQ_EMPTY(&vp->v_cache_dst))
817 cache_zap(TAILQ_FIRST(&vp->v_cache_dst));
818 if (vp->v_cache_dd != NULL) {
819 KASSERT(vp->v_cache_dd->nc_flag & NCF_ISDOTDOT,
820 ("lost dotdot link"));
821 cache_zap(vp->v_cache_dd);
823 KASSERT(vp->v_cache_dd == NULL, ("incomplete purge"));
828 * Invalidate all negative entries for a particular directory vnode.
831 cache_purge_negative(vp)
834 struct namecache *cp, *ncp;
836 CTR1(KTR_VFS, "cache_purge_negative(%p)", vp);
837 SDT_PROBE(vfs, namecache, purge_negative, done, vp, 0, 0, 0, 0);
839 LIST_FOREACH_SAFE(cp, &vp->v_cache_src, nc_src, ncp) {
840 if (cp->nc_vp == NULL)
847 * Flush all entries referencing a particular filesystem.
853 struct nchashhead *ncpp;
854 struct namecache *ncp, *nnp;
856 /* Scan hash tables for applicable entries */
857 SDT_PROBE(vfs, namecache, purgevfs, done, mp, 0, 0, 0, 0);
859 for (ncpp = &nchashtbl[nchash]; ncpp >= nchashtbl; ncpp--) {
860 LIST_FOREACH_SAFE(ncp, ncpp, nc_hash, nnp) {
861 if (ncp->nc_dvp->v_mount == mp)
869 * Perform canonical checks and cache lookup and pass on to filesystem
870 * through the vop_cachedlookup only if needed.
875 struct vop_lookup_args /* {
877 struct vnode **a_vpp;
878 struct componentname *a_cnp;
883 struct vnode **vpp = ap->a_vpp;
884 struct componentname *cnp = ap->a_cnp;
885 struct ucred *cred = cnp->cn_cred;
886 int flags = cnp->cn_flags;
887 struct thread *td = cnp->cn_thread;
892 if (dvp->v_type != VDIR)
895 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
896 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
899 error = VOP_ACCESS(dvp, VEXEC, cred, td);
903 error = cache_lookup(dvp, vpp, cnp);
905 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
912 #ifndef _SYS_SYSPROTO_H_
913 struct __getcwd_args {
920 * XXX All of these sysctls would probably be more productive dead.
922 static int disablecwd;
923 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0,
924 "Disable the getcwd syscall");
926 /* Implementation of the getcwd syscall. */
930 struct __getcwd_args *uap;
933 return (kern___getcwd(td, uap->buf, UIO_USERSPACE, uap->buflen));
937 kern___getcwd(struct thread *td, u_char *buf, enum uio_seg bufseg, u_int buflen)
940 struct filedesc *fdp;
941 struct vnode *cdir, *rdir;
942 int error, vfslocked;
948 if (buflen > MAXPATHLEN)
951 tmpbuf = malloc(buflen, M_TEMP, M_WAITOK);
952 fdp = td->td_proc->p_fd;
958 FILEDESC_SUNLOCK(fdp);
959 error = vn_fullpath1(td, cdir, rdir, tmpbuf, &bp, buflen);
960 vfslocked = VFS_LOCK_GIANT(rdir->v_mount);
962 VFS_UNLOCK_GIANT(vfslocked);
963 vfslocked = VFS_LOCK_GIANT(cdir->v_mount);
965 VFS_UNLOCK_GIANT(vfslocked);
968 if (bufseg == UIO_SYSSPACE)
969 bcopy(bp, buf, strlen(bp) + 1);
971 error = copyout(bp, buf, strlen(bp) + 1);
973 if (KTRPOINT(curthread, KTR_NAMEI))
977 free(tmpbuf, M_TEMP);
982 * Thus begins the fullpath magic.
986 #define STATNODE(name, descr) \
988 SYSCTL_UINT(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, descr)
990 static int disablefullpath;
991 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW, &disablefullpath, 0,
992 "Disable the vn_fullpath function");
994 /* These count for kern___getcwd(), too. */
995 STATNODE(numfullpathcalls, "Number of fullpath search calls");
996 STATNODE(numfullpathfail1, "Number of fullpath search errors (ENOTDIR)");
997 STATNODE(numfullpathfail2,
998 "Number of fullpath search errors (VOP_VPTOCNP failures)");
999 STATNODE(numfullpathfail4, "Number of fullpath search errors (ENOMEM)");
1000 STATNODE(numfullpathfound, "Number of successful fullpath calls");
1003 * Retrieve the full filesystem path that correspond to a vnode from the name
1004 * cache (if available)
1007 vn_fullpath(struct thread *td, struct vnode *vn, char **retbuf, char **freebuf)
1010 struct filedesc *fdp;
1012 int error, vfslocked;
1014 if (disablefullpath)
1019 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1020 fdp = td->td_proc->p_fd;
1021 FILEDESC_SLOCK(fdp);
1022 rdir = fdp->fd_rdir;
1024 FILEDESC_SUNLOCK(fdp);
1025 error = vn_fullpath1(td, vn, rdir, buf, retbuf, MAXPATHLEN);
1026 vfslocked = VFS_LOCK_GIANT(rdir->v_mount);
1028 VFS_UNLOCK_GIANT(vfslocked);
1038 * This function is similar to vn_fullpath, but it attempts to lookup the
1039 * pathname relative to the global root mount point. This is required for the
1040 * auditing sub-system, as audited pathnames must be absolute, relative to the
1041 * global root mount point.
1044 vn_fullpath_global(struct thread *td, struct vnode *vn,
1045 char **retbuf, char **freebuf)
1050 if (disablefullpath)
1054 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1055 error = vn_fullpath1(td, vn, rootvnode, buf, retbuf, MAXPATHLEN);
1064 vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, u_int *buflen)
1069 error = vn_vptocnp_locked(vp, cred, buf, buflen);
1072 * vn_vptocnp_locked() dropped hold acquired by
1073 * VOP_VPTOCNP immediately after locking the
1074 * cache. Since we are going to drop the cache rlock,
1075 * re-hold the result.
1084 vn_vptocnp_locked(struct vnode **vp, struct ucred *cred, char *buf,
1088 struct namecache *ncp;
1089 int error, vfslocked;
1091 TAILQ_FOREACH(ncp, &((*vp)->v_cache_dst), nc_dst) {
1092 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1096 if (*buflen < ncp->nc_nlen) {
1100 SDT_PROBE(vfs, namecache, fullpath, return, error,
1104 *buflen -= ncp->nc_nlen;
1105 memcpy(buf + *buflen, ncp->nc_name, ncp->nc_nlen);
1106 SDT_PROBE(vfs, namecache, fullpath, hit, ncp->nc_dvp,
1107 ncp->nc_name, vp, 0, 0);
1111 SDT_PROBE(vfs, namecache, fullpath, miss, vp, 0, 0, 0, 0);
1115 vfslocked = VFS_LOCK_GIANT((*vp)->v_mount);
1116 vn_lock(*vp, LK_SHARED | LK_RETRY);
1117 error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen);
1120 VFS_UNLOCK_GIANT(vfslocked);
1123 SDT_PROBE(vfs, namecache, fullpath, return, error, vp,
1130 if ((*vp)->v_iflag & VI_DOOMED) {
1131 /* forced unmount */
1135 SDT_PROBE(vfs, namecache, fullpath, return, error, vp,
1145 * The magic behind kern___getcwd() and vn_fullpath().
1148 vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
1149 char *buf, char **retbuf, u_int buflen)
1151 int error, slash_prefixed;
1152 #ifdef KDTRACE_HOOKS
1153 struct vnode *startvp = vp;
1161 SDT_PROBE(vfs, namecache, fullpath, entry, vp, 0, 0, 0, 0);
1164 if (vp->v_type != VDIR) {
1165 error = vn_vptocnp_locked(&vp, td->td_ucred, buf, &buflen);
1172 buf[--buflen] = '/';
1175 while (vp != rdir && vp != rootvnode) {
1176 if (vp->v_vflag & VV_ROOT) {
1177 if (vp->v_iflag & VI_DOOMED) { /* forced unmount */
1180 SDT_PROBE(vfs, namecache, fullpath, return,
1181 error, vp, NULL, 0, 0);
1184 vp = vp->v_mount->mnt_vnodecovered;
1187 if (vp->v_type != VDIR) {
1191 SDT_PROBE(vfs, namecache, fullpath, return,
1192 error, vp, NULL, 0, 0);
1195 error = vn_vptocnp_locked(&vp, td->td_ucred, buf, &buflen);
1201 SDT_PROBE(vfs, namecache, fullpath, return, error,
1202 startvp, NULL, 0, 0);
1205 buf[--buflen] = '/';
1210 if (!slash_prefixed) {
1214 SDT_PROBE(vfs, namecache, fullpath, return, ENOMEM,
1215 startvp, NULL, 0, 0);
1218 buf[--buflen] = '/';
1223 SDT_PROBE(vfs, namecache, fullpath, return, 0, startvp, buf + buflen,
1225 *retbuf = buf + buflen;
1230 vn_commname(struct vnode *vp, char *buf, u_int buflen)
1232 struct namecache *ncp;
1236 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst)
1237 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1243 l = min(ncp->nc_nlen, buflen - 1);
1244 memcpy(buf, ncp->nc_name, l);