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/fcntl.h>
49 #include <sys/mount.h>
50 #include <sys/namei.h>
52 #include <sys/rwlock.h>
54 #include <sys/syscallsubr.h>
55 #include <sys/sysctl.h>
56 #include <sys/sysproto.h>
57 #include <sys/vnode.h>
59 #include <sys/ktrace.h>
64 SDT_PROVIDER_DECLARE(vfs);
65 SDT_PROBE_DEFINE3(vfs, namecache, enter, done, "struct vnode *", "char *",
67 SDT_PROBE_DEFINE2(vfs, namecache, enter_negative, done, "struct vnode *",
69 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, entry, "struct vnode *");
70 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, hit, "struct vnode *",
71 "char *", "struct vnode *");
72 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, miss, "struct vnode *");
73 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, return, "int",
74 "struct vnode *", "char *");
75 SDT_PROBE_DEFINE3(vfs, namecache, lookup, hit, "struct vnode *", "char *",
77 SDT_PROBE_DEFINE2(vfs, namecache, lookup, hit__negative,
78 "struct vnode *", "char *");
79 SDT_PROBE_DEFINE2(vfs, namecache, lookup, miss, "struct vnode *",
81 SDT_PROBE_DEFINE1(vfs, namecache, purge, done, "struct vnode *");
82 SDT_PROBE_DEFINE1(vfs, namecache, purge_negative, done, "struct vnode *");
83 SDT_PROBE_DEFINE1(vfs, namecache, purgevfs, done, "struct mount *");
84 SDT_PROBE_DEFINE3(vfs, namecache, zap, done, "struct vnode *", "char *",
86 SDT_PROBE_DEFINE2(vfs, namecache, zap_negative, done, "struct vnode *",
90 * This structure describes the elements in the cache of recent
91 * names looked up by namei.
95 LIST_ENTRY(namecache) nc_hash; /* hash chain */
96 LIST_ENTRY(namecache) nc_src; /* source vnode list */
97 TAILQ_ENTRY(namecache) nc_dst; /* destination vnode list */
98 struct vnode *nc_dvp; /* vnode of parent of name */
99 struct vnode *nc_vp; /* vnode the name refers to */
100 u_char nc_flag; /* flag bits */
101 u_char nc_nlen; /* length of name */
102 char nc_name[0]; /* segment name + nul */
106 * struct namecache_ts repeats struct namecache layout up to the
108 * struct namecache_ts is used in place of struct namecache when time(s) need
109 * to be stored. The nc_dotdottime field is used when a cache entry is mapping
110 * both a non-dotdot directory name plus dotdot for the directory's
113 struct namecache_ts {
114 LIST_ENTRY(namecache) nc_hash; /* hash chain */
115 LIST_ENTRY(namecache) nc_src; /* source vnode list */
116 TAILQ_ENTRY(namecache) nc_dst; /* destination vnode list */
117 struct vnode *nc_dvp; /* vnode of parent of name */
118 struct vnode *nc_vp; /* vnode the name refers to */
119 u_char nc_flag; /* flag bits */
120 u_char nc_nlen; /* length of name */
121 struct timespec nc_time; /* timespec provided by fs */
122 struct timespec nc_dotdottime; /* dotdot timespec provided by fs */
123 int nc_ticks; /* ticks value when entry was added */
124 char nc_name[0]; /* segment name + nul */
128 * Flags in namecache.nc_flag
130 #define NCF_WHITE 0x01
131 #define NCF_ISDOTDOT 0x02
136 * Name caching works as follows:
138 * Names found by directory scans are retained in a cache
139 * for future reference. It is managed LRU, so frequently
140 * used names will hang around. Cache is indexed by hash value
141 * obtained from (vp, name) where vp refers to the directory
144 * If it is a "negative" entry, (i.e. for a name that is known NOT to
145 * exist) the vnode pointer will be NULL.
147 * Upon reaching the last segment of a path, if the reference
148 * is for DELETE, or NOCACHE is set (rewrite), and the
149 * name is located in the cache, it will be dropped.
153 * Structures associated with name caching.
155 #define NCHHASH(hash) \
156 (&nchashtbl[(hash) & nchash])
157 static LIST_HEAD(nchashhead, namecache) *nchashtbl; /* Hash Table */
158 static TAILQ_HEAD(, namecache) ncneg; /* Hash Table */
159 static u_long nchash; /* size of hash table */
160 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0,
161 "Size of namecache hash table");
162 static u_long ncnegfactor = 16; /* ratio of negative entries */
163 SYSCTL_ULONG(_vfs, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0,
164 "Ratio of negative namecache entries");
165 static u_long numneg; /* number of negative entries allocated */
166 SYSCTL_ULONG(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0,
167 "Number of negative entries in namecache");
168 static u_long numcache; /* number of cache entries allocated */
169 SYSCTL_ULONG(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0,
170 "Number of namecache entries");
171 static u_long numcachehv; /* number of cache entries with vnodes held */
172 SYSCTL_ULONG(_debug, OID_AUTO, numcachehv, CTLFLAG_RD, &numcachehv, 0,
173 "Number of namecache entries with vnodes held");
174 static u_int ncsizefactor = 2;
175 SYSCTL_UINT(_vfs, OID_AUTO, ncsizefactor, CTLFLAG_RW, &ncsizefactor, 0,
176 "Size factor for namecache");
178 struct nchstats nchstats; /* cache effectiveness statistics */
180 static struct rwlock cache_lock;
181 RW_SYSINIT(vfscache, &cache_lock, "Name Cache");
183 #define CACHE_UPGRADE_LOCK() rw_try_upgrade(&cache_lock)
184 #define CACHE_RLOCK() rw_rlock(&cache_lock)
185 #define CACHE_RUNLOCK() rw_runlock(&cache_lock)
186 #define CACHE_WLOCK() rw_wlock(&cache_lock)
187 #define CACHE_WUNLOCK() rw_wunlock(&cache_lock)
190 * UMA zones for the VFS cache.
192 * The small cache is used for entries with short names, which are the
193 * most common. The large cache is used for entries which are too big to
194 * fit in the small cache.
196 static uma_zone_t cache_zone_small;
197 static uma_zone_t cache_zone_small_ts;
198 static uma_zone_t cache_zone_large;
199 static uma_zone_t cache_zone_large_ts;
201 #define CACHE_PATH_CUTOFF 35
203 static struct namecache *
204 cache_alloc(int len, int ts)
207 if (len > CACHE_PATH_CUTOFF) {
209 return (uma_zalloc(cache_zone_large_ts, M_WAITOK));
211 return (uma_zalloc(cache_zone_large, M_WAITOK));
214 return (uma_zalloc(cache_zone_small_ts, M_WAITOK));
216 return (uma_zalloc(cache_zone_small, M_WAITOK));
220 cache_free(struct namecache *ncp)
226 ts = ncp->nc_flag & NCF_TS;
227 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF) {
229 uma_zfree(cache_zone_small_ts, ncp);
231 uma_zfree(cache_zone_small, ncp);
233 uma_zfree(cache_zone_large_ts, ncp);
235 uma_zfree(cache_zone_large, ncp);
239 nc_get_name(struct namecache *ncp)
241 struct namecache_ts *ncp_ts;
243 if ((ncp->nc_flag & NCF_TS) == 0)
244 return (ncp->nc_name);
245 ncp_ts = (struct namecache_ts *)ncp;
246 return (ncp_ts->nc_name);
250 cache_out_ts(struct namecache *ncp, struct timespec *tsp, int *ticksp)
253 KASSERT((ncp->nc_flag & NCF_TS) != 0 ||
254 (tsp == NULL && ticksp == NULL),
258 *tsp = ((struct namecache_ts *)ncp)->nc_time;
260 *ticksp = ((struct namecache_ts *)ncp)->nc_ticks;
263 static int doingcache = 1; /* 1 => enable the cache */
264 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0,
265 "VFS namecache enabled");
267 /* Export size information to userland */
268 SYSCTL_INT(_debug_sizeof, OID_AUTO, namecache, CTLFLAG_RD, SYSCTL_NULL_INT_PTR,
269 sizeof(struct namecache), "sizeof(struct namecache)");
272 * The new name cache statistics
274 static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0,
275 "Name cache statistics");
276 #define STATNODE(mode, name, var, descr) \
277 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, mode, var, 0, descr);
278 STATNODE(CTLFLAG_RD, numneg, &numneg, "Number of negative cache entries");
279 STATNODE(CTLFLAG_RD, numcache, &numcache, "Number of cache entries");
280 static u_long numcalls; STATNODE(CTLFLAG_RD, numcalls, &numcalls,
281 "Number of cache lookups");
282 static u_long dothits; STATNODE(CTLFLAG_RD, dothits, &dothits,
283 "Number of '.' hits");
284 static u_long dotdothits; STATNODE(CTLFLAG_RD, dotdothits, &dotdothits,
285 "Number of '..' hits");
286 static u_long numchecks; STATNODE(CTLFLAG_RD, numchecks, &numchecks,
287 "Number of checks in lookup");
288 static u_long nummiss; STATNODE(CTLFLAG_RD, nummiss, &nummiss,
289 "Number of cache misses");
290 static u_long nummisszap; STATNODE(CTLFLAG_RD, nummisszap, &nummisszap,
291 "Number of cache misses we do not want to cache");
292 static u_long numposzaps; STATNODE(CTLFLAG_RD, numposzaps, &numposzaps,
293 "Number of cache hits (positive) we do not want to cache");
294 static u_long numposhits; STATNODE(CTLFLAG_RD, numposhits, &numposhits,
295 "Number of cache hits (positive)");
296 static u_long numnegzaps; STATNODE(CTLFLAG_RD, numnegzaps, &numnegzaps,
297 "Number of cache hits (negative) we do not want to cache");
298 static u_long numneghits; STATNODE(CTLFLAG_RD, numneghits, &numneghits,
299 "Number of cache hits (negative)");
300 static u_long numupgrades; STATNODE(CTLFLAG_RD, numupgrades, &numupgrades,
301 "Number of updates of the cache after lookup (write lock + retry)");
303 SYSCTL_OPAQUE(_vfs_cache, OID_AUTO, nchstats, CTLFLAG_RD | CTLFLAG_MPSAFE,
304 &nchstats, sizeof(nchstats), "LU",
305 "VFS cache effectiveness statistics");
309 static void cache_zap(struct namecache *ncp);
310 static int vn_vptocnp_locked(struct vnode **vp, struct ucred *cred, char *buf,
312 static int vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
313 char *buf, char **retbuf, u_int buflen);
315 static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
319 * Grab an atomic snapshot of the name cache hash chain lengths
321 static SYSCTL_NODE(_debug, OID_AUTO, hashstat, CTLFLAG_RW, NULL,
325 sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS)
327 struct nchashhead *ncpp;
328 struct namecache *ncp;
329 int i, error, n_nchash, *cntbuf;
332 n_nchash = nchash + 1; /* nchash is max index, not count */
334 return SYSCTL_OUT(req, 0, n_nchash * sizeof(int));
335 cntbuf = malloc(n_nchash * sizeof(int), M_TEMP, M_ZERO | M_WAITOK);
337 if (n_nchash != nchash + 1) {
339 free(cntbuf, M_TEMP);
342 /* Scan hash tables counting entries */
343 for (ncpp = nchashtbl, i = 0; i < n_nchash; ncpp++, i++)
344 LIST_FOREACH(ncp, ncpp, nc_hash)
347 for (error = 0, i = 0; i < n_nchash; i++)
348 if ((error = SYSCTL_OUT(req, &cntbuf[i], sizeof(int))) != 0)
350 free(cntbuf, M_TEMP);
353 SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD|
354 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_rawnchash, "S,int",
355 "nchash chain lengths");
358 sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS)
361 struct nchashhead *ncpp;
362 struct namecache *ncp;
364 int count, maxlength, used, pct;
367 return SYSCTL_OUT(req, 0, 4 * sizeof(int));
369 n_nchash = nchash + 1; /* nchash is max index, not count */
373 /* Scan hash tables for applicable entries */
374 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) {
377 LIST_FOREACH(ncp, ncpp, nc_hash) {
383 if (maxlength < count)
386 n_nchash = nchash + 1;
387 pct = (used * 100) / (n_nchash / 100);
388 error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash));
391 error = SYSCTL_OUT(req, &used, sizeof(used));
394 error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength));
397 error = SYSCTL_OUT(req, &pct, sizeof(pct));
402 SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD|
403 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_nchash, "I",
404 "nchash statistics (number of total/used buckets, maximum chain length, usage percentage)");
410 * Removes a namecache entry from cache, whether it contains an actual
411 * pointer to a vnode or if it is just a negative cache entry.
415 struct namecache *ncp;
419 rw_assert(&cache_lock, RA_WLOCKED);
420 CTR2(KTR_VFS, "cache_zap(%p) vp %p", ncp, ncp->nc_vp);
421 if (ncp->nc_vp != NULL) {
422 SDT_PROBE3(vfs, namecache, zap, done, ncp->nc_dvp,
423 nc_get_name(ncp), ncp->nc_vp);
425 SDT_PROBE2(vfs, namecache, zap_negative, done, ncp->nc_dvp,
429 LIST_REMOVE(ncp, nc_hash);
430 if (ncp->nc_flag & NCF_ISDOTDOT) {
431 if (ncp == ncp->nc_dvp->v_cache_dd)
432 ncp->nc_dvp->v_cache_dd = NULL;
434 LIST_REMOVE(ncp, nc_src);
435 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) {
441 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
442 if (ncp == ncp->nc_vp->v_cache_dd)
443 ncp->nc_vp->v_cache_dd = NULL;
445 TAILQ_REMOVE(&ncneg, ncp, nc_dst);
455 * Lookup an entry in the cache
457 * Lookup is called with dvp pointing to the directory to search,
458 * cnp pointing to the name of the entry being sought. If the lookup
459 * succeeds, the vnode is returned in *vpp, and a status of -1 is
460 * returned. If the lookup determines that the name does not exist
461 * (negative caching), a status of ENOENT is returned. If the lookup
462 * fails, a status of zero is returned. If the directory vnode is
463 * recycled out from under us due to a forced unmount, a status of
464 * ENOENT is returned.
466 * vpp is locked and ref'd on return. If we're looking up DOTDOT, dvp is
467 * unlocked. If we're looking up . an extra ref is taken, but the lock is
468 * not recursively acquired.
472 cache_lookup(dvp, vpp, cnp, tsp, ticksp)
475 struct componentname *cnp;
476 struct timespec *tsp;
479 struct namecache *ncp;
481 int error, ltype, wlocked;
484 cnp->cn_flags &= ~MAKEENTRY;
494 if (cnp->cn_nameptr[0] == '.') {
495 if (cnp->cn_namelen == 1) {
497 CTR2(KTR_VFS, "cache_lookup(%p, %s) found via .",
498 dvp, cnp->cn_nameptr);
500 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", *vpp);
507 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
509 if (dvp->v_cache_dd == NULL) {
510 SDT_PROBE3(vfs, namecache, lookup, miss, dvp,
514 if ((cnp->cn_flags & MAKEENTRY) == 0) {
515 if (!wlocked && !CACHE_UPGRADE_LOCK())
517 if (dvp->v_cache_dd->nc_flag & NCF_ISDOTDOT)
518 cache_zap(dvp->v_cache_dd);
519 dvp->v_cache_dd = NULL;
523 ncp = dvp->v_cache_dd;
524 if (ncp->nc_flag & NCF_ISDOTDOT)
528 /* Return failure if negative entry was found. */
530 goto negative_success;
531 CTR3(KTR_VFS, "cache_lookup(%p, %s) found %p via ..",
532 dvp, cnp->cn_nameptr, *vpp);
533 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, "..",
535 cache_out_ts(ncp, tsp, ticksp);
536 if ((ncp->nc_flag & (NCF_ISDOTDOT | NCF_DTS)) ==
537 NCF_DTS && tsp != NULL)
538 *tsp = ((struct namecache_ts *)ncp)->
544 hash = fnv_32_buf(cnp->cn_nameptr, cnp->cn_namelen, FNV1_32_INIT);
545 hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
546 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
548 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
549 !bcmp(nc_get_name(ncp), cnp->cn_nameptr, ncp->nc_nlen))
553 /* We failed to find an entry */
555 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr,
557 if ((cnp->cn_flags & MAKEENTRY) == 0) {
566 /* We don't want to have an entry, so dump it */
567 if ((cnp->cn_flags & MAKEENTRY) == 0) {
569 nchstats.ncs_badhits++;
570 if (!wlocked && !CACHE_UPGRADE_LOCK())
577 /* We found a "positive" match, return the vnode */
580 nchstats.ncs_goodhits++;
582 CTR4(KTR_VFS, "cache_lookup(%p, %s) found %p via ncp %p",
583 dvp, cnp->cn_nameptr, *vpp, ncp);
584 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, nc_get_name(ncp),
586 cache_out_ts(ncp, tsp, ticksp);
591 /* We found a negative match, and want to create it, so purge */
592 if (cnp->cn_nameiop == CREATE) {
594 nchstats.ncs_badhits++;
595 if (!wlocked && !CACHE_UPGRADE_LOCK())
602 if (!wlocked && !CACHE_UPGRADE_LOCK())
606 * We found a "negative" match, so we shift it to the end of
607 * the "negative" cache entries queue to satisfy LRU. Also,
608 * check to see if the entry is a whiteout; indicate this to
609 * the componentname, if so.
611 TAILQ_REMOVE(&ncneg, ncp, nc_dst);
612 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
613 nchstats.ncs_neghits++;
614 if (ncp->nc_flag & NCF_WHITE)
615 cnp->cn_flags |= ISWHITEOUT;
616 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp,
618 cache_out_ts(ncp, tsp, ticksp);
624 * We need to update the cache after our lookup, so upgrade to
625 * a write lock and retry the operation.
635 * On success we return a locked and ref'd vnode as per the lookup
638 if (dvp == *vpp) { /* lookup on "." */
645 * When we lookup "." we still can be asked to lock it
648 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
649 if (ltype != VOP_ISLOCKED(*vpp)) {
650 if (ltype == LK_EXCLUSIVE) {
651 vn_lock(*vpp, LK_UPGRADE | LK_RETRY);
652 if ((*vpp)->v_iflag & VI_DOOMED) {
659 vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY);
663 ltype = 0; /* silence gcc warning */
664 if (cnp->cn_flags & ISDOTDOT) {
665 ltype = VOP_ISLOCKED(dvp);
673 error = vget(*vpp, cnp->cn_lkflags | LK_INTERLOCK, cnp->cn_thread);
674 if (cnp->cn_flags & ISDOTDOT) {
675 vn_lock(dvp, ltype | LK_RETRY);
676 if (dvp->v_iflag & VI_DOOMED) {
687 if ((cnp->cn_flags & ISLASTCN) &&
688 (cnp->cn_lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE) {
689 ASSERT_VOP_ELOCKED(*vpp, "cache_lookup");
702 * Add an entry to the cache.
705 cache_enter_time(dvp, vp, cnp, tsp, dtsp)
708 struct componentname *cnp;
709 struct timespec *tsp;
710 struct timespec *dtsp;
712 struct namecache *ncp, *n2;
713 struct namecache_ts *n3;
714 struct nchashhead *ncpp;
721 CTR3(KTR_VFS, "cache_enter(%p, %p, %s)", dvp, vp, cnp->cn_nameptr);
722 VNASSERT(vp == NULL || (vp->v_iflag & VI_DOOMED) == 0, vp,
723 ("cache_enter: Adding a doomed vnode"));
724 VNASSERT(dvp == NULL || (dvp->v_iflag & VI_DOOMED) == 0, dvp,
725 ("cache_enter: Doomed vnode used as src"));
731 * Avoid blowout in namecache entries.
733 if (numcache >= desiredvnodes * ncsizefactor)
737 if (cnp->cn_nameptr[0] == '.') {
738 if (cnp->cn_namelen == 1)
740 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
743 * If dotdot entry already exists, just retarget it
744 * to new parent vnode, otherwise continue with new
745 * namecache entry allocation.
747 if ((ncp = dvp->v_cache_dd) != NULL &&
748 ncp->nc_flag & NCF_ISDOTDOT) {
749 KASSERT(ncp->nc_dvp == dvp,
750 ("wrong isdotdot parent"));
751 if (ncp->nc_vp != NULL) {
752 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst,
755 TAILQ_REMOVE(&ncneg, ncp, nc_dst);
759 TAILQ_INSERT_HEAD(&vp->v_cache_dst,
762 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
769 dvp->v_cache_dd = NULL;
770 SDT_PROBE3(vfs, namecache, enter, done, dvp, "..", vp);
780 * Calculate the hash key and setup as much of the new
781 * namecache entry as possible before acquiring the lock.
783 ncp = cache_alloc(cnp->cn_namelen, tsp != NULL);
788 n3 = (struct namecache_ts *)ncp;
790 n3->nc_ticks = ticks;
791 n3->nc_flag |= NCF_TS;
793 n3->nc_dotdottime = *dtsp;
794 n3->nc_flag |= NCF_DTS;
797 len = ncp->nc_nlen = cnp->cn_namelen;
798 hash = fnv_32_buf(cnp->cn_nameptr, len, FNV1_32_INIT);
799 strlcpy(nc_get_name(ncp), cnp->cn_nameptr, len + 1);
800 hash = fnv_32_buf(&dvp, sizeof(dvp), hash);
804 * See if this vnode or negative entry is already in the cache
805 * with this name. This can happen with concurrent lookups of
806 * the same path name.
808 ncpp = NCHHASH(hash);
809 LIST_FOREACH(n2, ncpp, nc_hash) {
810 if (n2->nc_dvp == dvp &&
811 n2->nc_nlen == cnp->cn_namelen &&
812 !bcmp(nc_get_name(n2), cnp->cn_nameptr, n2->nc_nlen)) {
814 KASSERT((n2->nc_flag & NCF_TS) != 0,
816 n3 = (struct namecache_ts *)n2;
818 ((struct namecache_ts *)ncp)->nc_time;
820 ((struct namecache_ts *)ncp)->nc_ticks;
823 ((struct namecache_ts *)ncp)->
825 n3->nc_flag |= NCF_DTS;
834 if (flag == NCF_ISDOTDOT) {
836 * See if we are trying to add .. entry, but some other lookup
837 * has populated v_cache_dd pointer already.
839 if (dvp->v_cache_dd != NULL) {
844 KASSERT(vp == NULL || vp->v_type == VDIR,
845 ("wrong vnode type %p", vp));
846 dvp->v_cache_dd = ncp;
852 if (cnp->cn_flags & ISWHITEOUT)
853 ncp->nc_flag |= NCF_WHITE;
854 } else if (vp->v_type == VDIR) {
855 if (flag != NCF_ISDOTDOT) {
857 * For this case, the cache entry maps both the
858 * directory name in it and the name ".." for the
859 * directory's parent.
861 if ((n2 = vp->v_cache_dd) != NULL &&
862 (n2->nc_flag & NCF_ISDOTDOT) != 0)
864 vp->v_cache_dd = ncp;
867 vp->v_cache_dd = NULL;
871 * Insert the new namecache entry into the appropriate chain
872 * within the cache entries table.
874 LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
875 if (flag != NCF_ISDOTDOT) {
876 if (LIST_EMPTY(&dvp->v_cache_src)) {
880 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
884 * If the entry is "negative", we place it into the
885 * "negative" cache queue, otherwise, we place it into the
886 * destination vnode's cache entries queue.
889 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
890 SDT_PROBE3(vfs, namecache, enter, done, dvp, nc_get_name(ncp),
893 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
894 SDT_PROBE2(vfs, namecache, enter_negative, done, dvp,
897 if (numneg * ncnegfactor > numcache) {
898 ncp = TAILQ_FIRST(&ncneg);
899 KASSERT(ncp->nc_vp == NULL, ("ncp %p vp %p on ncneg",
911 * Name cache initialization, from vfs_init() when we are booting
914 nchinit(void *dummy __unused)
919 cache_zone_small = uma_zcreate("S VFS Cache",
920 sizeof(struct namecache) + CACHE_PATH_CUTOFF + 1,
921 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
922 cache_zone_small_ts = uma_zcreate("STS VFS Cache",
923 sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1,
924 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
925 cache_zone_large = uma_zcreate("L VFS Cache",
926 sizeof(struct namecache) + NAME_MAX + 1,
927 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
928 cache_zone_large_ts = uma_zcreate("LTS VFS Cache",
929 sizeof(struct namecache_ts) + NAME_MAX + 1,
930 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
932 nchashtbl = hashinit(desiredvnodes * 2, M_VFSCACHE, &nchash);
934 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL);
937 cache_changesize(int newmaxvnodes)
939 struct nchashhead *new_nchashtbl, *old_nchashtbl;
940 u_long new_nchash, old_nchash;
941 struct namecache *ncp;
945 new_nchashtbl = hashinit(newmaxvnodes * 2, M_VFSCACHE, &new_nchash);
946 /* If same hash table size, nothing to do */
947 if (nchash == new_nchash) {
948 free(new_nchashtbl, M_VFSCACHE);
952 * Move everything from the old hash table to the new table.
953 * None of the namecache entries in the table can be removed
954 * because to do so, they have to be removed from the hash table.
957 old_nchashtbl = nchashtbl;
959 nchashtbl = new_nchashtbl;
961 for (i = 0; i <= old_nchash; i++) {
962 while ((ncp = LIST_FIRST(&old_nchashtbl[i])) != NULL) {
963 hash = fnv_32_buf(nc_get_name(ncp), ncp->nc_nlen,
965 hash = fnv_32_buf(&ncp->nc_dvp, sizeof(ncp->nc_dvp),
967 LIST_REMOVE(ncp, nc_hash);
968 LIST_INSERT_HEAD(NCHHASH(hash), ncp, nc_hash);
972 free(old_nchashtbl, M_VFSCACHE);
976 * Invalidate all entries to a particular vnode.
983 CTR1(KTR_VFS, "cache_purge(%p)", vp);
984 SDT_PROBE1(vfs, namecache, purge, done, vp);
986 while (!LIST_EMPTY(&vp->v_cache_src))
987 cache_zap(LIST_FIRST(&vp->v_cache_src));
988 while (!TAILQ_EMPTY(&vp->v_cache_dst))
989 cache_zap(TAILQ_FIRST(&vp->v_cache_dst));
990 if (vp->v_cache_dd != NULL) {
991 KASSERT(vp->v_cache_dd->nc_flag & NCF_ISDOTDOT,
992 ("lost dotdot link"));
993 cache_zap(vp->v_cache_dd);
995 KASSERT(vp->v_cache_dd == NULL, ("incomplete purge"));
1000 * Invalidate all negative entries for a particular directory vnode.
1003 cache_purge_negative(vp)
1006 struct namecache *cp, *ncp;
1008 CTR1(KTR_VFS, "cache_purge_negative(%p)", vp);
1009 SDT_PROBE1(vfs, namecache, purge_negative, done, vp);
1011 LIST_FOREACH_SAFE(cp, &vp->v_cache_src, nc_src, ncp) {
1012 if (cp->nc_vp == NULL)
1019 * Flush all entries referencing a particular filesystem.
1025 struct nchashhead *ncpp;
1026 struct namecache *ncp, *nnp;
1028 /* Scan hash tables for applicable entries */
1029 SDT_PROBE1(vfs, namecache, purgevfs, done, mp);
1031 for (ncpp = &nchashtbl[nchash]; ncpp >= nchashtbl; ncpp--) {
1032 LIST_FOREACH_SAFE(ncp, ncpp, nc_hash, nnp) {
1033 if (ncp->nc_dvp->v_mount == mp)
1041 * Perform canonical checks and cache lookup and pass on to filesystem
1042 * through the vop_cachedlookup only if needed.
1046 vfs_cache_lookup(ap)
1047 struct vop_lookup_args /* {
1048 struct vnode *a_dvp;
1049 struct vnode **a_vpp;
1050 struct componentname *a_cnp;
1055 struct vnode **vpp = ap->a_vpp;
1056 struct componentname *cnp = ap->a_cnp;
1057 struct ucred *cred = cnp->cn_cred;
1058 int flags = cnp->cn_flags;
1059 struct thread *td = cnp->cn_thread;
1064 if (dvp->v_type != VDIR)
1067 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
1068 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
1071 error = VOP_ACCESS(dvp, VEXEC, cred, td);
1075 error = cache_lookup(dvp, vpp, cnp, NULL, NULL);
1077 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
1084 * XXX All of these sysctls would probably be more productive dead.
1086 static int disablecwd;
1087 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0,
1088 "Disable the getcwd syscall");
1090 /* Implementation of the getcwd syscall. */
1092 sys___getcwd(td, uap)
1094 struct __getcwd_args *uap;
1097 return (kern___getcwd(td, uap->buf, UIO_USERSPACE, uap->buflen));
1101 kern___getcwd(struct thread *td, char *buf, enum uio_seg bufseg, u_int buflen)
1104 struct filedesc *fdp;
1105 struct vnode *cdir, *rdir;
1112 if (buflen > MAXPATHLEN)
1113 buflen = MAXPATHLEN;
1115 tmpbuf = malloc(buflen, M_TEMP, M_WAITOK);
1116 fdp = td->td_proc->p_fd;
1117 FILEDESC_SLOCK(fdp);
1118 cdir = fdp->fd_cdir;
1120 rdir = fdp->fd_rdir;
1122 FILEDESC_SUNLOCK(fdp);
1123 error = vn_fullpath1(td, cdir, rdir, tmpbuf, &bp, buflen);
1128 if (bufseg == UIO_SYSSPACE)
1129 bcopy(bp, buf, strlen(bp) + 1);
1131 error = copyout(bp, buf, strlen(bp) + 1);
1133 if (KTRPOINT(curthread, KTR_NAMEI))
1137 free(tmpbuf, M_TEMP);
1142 * Thus begins the fullpath magic.
1146 #define STATNODE(name, descr) \
1147 static u_int name; \
1148 SYSCTL_UINT(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, descr)
1150 static int disablefullpath;
1151 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW, &disablefullpath, 0,
1152 "Disable the vn_fullpath function");
1154 /* These count for kern___getcwd(), too. */
1155 STATNODE(numfullpathcalls, "Number of fullpath search calls");
1156 STATNODE(numfullpathfail1, "Number of fullpath search errors (ENOTDIR)");
1157 STATNODE(numfullpathfail2,
1158 "Number of fullpath search errors (VOP_VPTOCNP failures)");
1159 STATNODE(numfullpathfail4, "Number of fullpath search errors (ENOMEM)");
1160 STATNODE(numfullpathfound, "Number of successful fullpath calls");
1163 * Retrieve the full filesystem path that correspond to a vnode from the name
1164 * cache (if available)
1167 vn_fullpath(struct thread *td, struct vnode *vn, char **retbuf, char **freebuf)
1170 struct filedesc *fdp;
1174 if (disablefullpath)
1179 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1180 fdp = td->td_proc->p_fd;
1181 FILEDESC_SLOCK(fdp);
1182 rdir = fdp->fd_rdir;
1184 FILEDESC_SUNLOCK(fdp);
1185 error = vn_fullpath1(td, vn, rdir, buf, retbuf, MAXPATHLEN);
1196 * This function is similar to vn_fullpath, but it attempts to lookup the
1197 * pathname relative to the global root mount point. This is required for the
1198 * auditing sub-system, as audited pathnames must be absolute, relative to the
1199 * global root mount point.
1202 vn_fullpath_global(struct thread *td, struct vnode *vn,
1203 char **retbuf, char **freebuf)
1208 if (disablefullpath)
1212 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1213 error = vn_fullpath1(td, vn, rootvnode, buf, retbuf, MAXPATHLEN);
1222 vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, u_int *buflen)
1227 error = vn_vptocnp_locked(vp, cred, buf, buflen);
1234 vn_vptocnp_locked(struct vnode **vp, struct ucred *cred, char *buf,
1238 struct namecache *ncp;
1241 TAILQ_FOREACH(ncp, &((*vp)->v_cache_dst), nc_dst) {
1242 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1246 if (*buflen < ncp->nc_nlen) {
1251 SDT_PROBE3(vfs, namecache, fullpath, return, error,
1255 *buflen -= ncp->nc_nlen;
1256 memcpy(buf + *buflen, nc_get_name(ncp), ncp->nc_nlen);
1257 SDT_PROBE3(vfs, namecache, fullpath, hit, ncp->nc_dvp,
1258 nc_get_name(ncp), vp);
1267 SDT_PROBE1(vfs, namecache, fullpath, miss, vp);
1270 vn_lock(*vp, LK_SHARED | LK_RETRY);
1271 error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen);
1275 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
1281 if (dvp->v_iflag & VI_DOOMED) {
1282 /* forced unmount */
1286 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL);
1290 * *vp has its use count incremented still.
1297 * The magic behind kern___getcwd() and vn_fullpath().
1300 vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir,
1301 char *buf, char **retbuf, u_int buflen)
1303 int error, slash_prefixed;
1304 #ifdef KDTRACE_HOOKS
1305 struct vnode *startvp = vp;
1314 SDT_PROBE1(vfs, namecache, fullpath, entry, vp);
1318 if (vp->v_type != VDIR) {
1319 error = vn_vptocnp_locked(&vp, td->td_ucred, buf, &buflen);
1327 buf[--buflen] = '/';
1330 while (vp != rdir && vp != rootvnode) {
1331 if (vp->v_vflag & VV_ROOT) {
1332 if (vp->v_iflag & VI_DOOMED) { /* forced unmount */
1336 SDT_PROBE3(vfs, namecache, fullpath, return,
1340 vp1 = vp->v_mount->mnt_vnodecovered;
1348 if (vp->v_type != VDIR) {
1353 SDT_PROBE3(vfs, namecache, fullpath, return,
1357 error = vn_vptocnp_locked(&vp, td->td_ucred, buf, &buflen);
1364 SDT_PROBE3(vfs, namecache, fullpath, return, error,
1368 buf[--buflen] = '/';
1373 if (!slash_prefixed) {
1378 SDT_PROBE3(vfs, namecache, fullpath, return, ENOMEM,
1382 buf[--buflen] = '/';
1388 SDT_PROBE3(vfs, namecache, fullpath, return, 0, startvp, buf + buflen);
1389 *retbuf = buf + buflen;
1394 vn_dir_dd_ino(struct vnode *vp)
1396 struct namecache *ncp;
1399 ASSERT_VOP_LOCKED(vp, "vn_dir_dd_ino");
1401 TAILQ_FOREACH(ncp, &(vp->v_cache_dst), nc_dst) {
1402 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0)
1407 if (vget(ddvp, LK_INTERLOCK | LK_SHARED | LK_NOWAIT, curthread))
1416 vn_commname(struct vnode *vp, char *buf, u_int buflen)
1418 struct namecache *ncp;
1422 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst)
1423 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0)
1429 l = min(ncp->nc_nlen, buflen - 1);
1430 memcpy(buf, nc_get_name(ncp), l);
1436 /* ABI compat shims for old kernel modules. */
1439 void cache_enter(struct vnode *dvp, struct vnode *vp,
1440 struct componentname *cnp);
1443 cache_enter(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
1446 cache_enter_time(dvp, vp, cnp, NULL, NULL);
1450 * This function updates path string to vnode's full global path
1451 * and checks the size of the new path string against the pathlen argument.
1453 * Requires a locked, referenced vnode and GIANT lock held.
1454 * Vnode is re-locked on success or ENODEV, otherwise unlocked.
1456 * If sysctl debug.disablefullpath is set, ENODEV is returned,
1457 * vnode is left locked and path remain untouched.
1459 * If vp is a directory, the call to vn_fullpath_global() always succeeds
1460 * because it falls back to the ".." lookup if the namecache lookup fails.
1463 vn_path_to_global_path(struct thread *td, struct vnode *vp, char *path,
1466 struct nameidata nd;
1471 ASSERT_VOP_ELOCKED(vp, __func__);
1473 /* Return ENODEV if sysctl debug.disablefullpath==1 */
1474 if (disablefullpath)
1477 /* Construct global filesystem path from vp. */
1479 error = vn_fullpath_global(td, vp, &rpath, &fbuf);
1486 if (strlen(rpath) >= pathlen) {
1488 error = ENAMETOOLONG;
1493 * Re-lookup the vnode by path to detect a possible rename.
1494 * As a side effect, the vnode is relocked.
1495 * If vnode was renamed, return ENOENT.
1497 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
1498 UIO_SYSSPACE, path, td);
1504 NDFREE(&nd, NDF_ONLY_PNBUF);
1508 strcpy(path, rpath);