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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)vfs_cache.c 8.5 (Berkeley) 3/22/95
37 * $FreeBSD: src/sys/kern/vfs_cache.c,v 1.42.2.6 2001/10/05 20:07:03 dillon Exp $
38 * $DragonFly: src/sys/kern/vfs_cache.c,v 1.5 2003/07/24 01:41:25 dillon Exp $
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/sysctl.h>
45 #include <sys/mount.h>
46 #include <sys/vnode.h>
47 #include <sys/malloc.h>
48 #include <sys/sysproto.h>
50 #include <sys/namei.h>
51 #include <sys/filedesc.h>
52 #include <sys/fnv_hash.h>
55 * This structure describes the elements in the cache of recent
56 * names looked up by namei.
60 LIST_ENTRY(namecache) nc_hash; /* hash chain */
61 LIST_ENTRY(namecache) nc_src; /* source vnode list */
62 TAILQ_ENTRY(namecache) nc_dst; /* destination vnode list */
63 struct vnode *nc_dvp; /* vnode of parent of name */
64 struct vnode *nc_vp; /* vnode the name refers to */
65 u_char nc_flag; /* flag bits */
66 u_char nc_nlen; /* length of name */
67 char nc_name[0]; /* segment name */
71 * Name caching works as follows:
73 * Names found by directory scans are retained in a cache
74 * for future reference. It is managed LRU, so frequently
75 * used names will hang around. Cache is indexed by hash value
76 * obtained from (vp, name) where vp refers to the directory
79 * If it is a "negative" entry, (i.e. for a name that is known NOT to
80 * exist) the vnode pointer will be NULL.
82 * Upon reaching the last segment of a path, if the reference
83 * is for DELETE, or NOCACHE is set (rewrite), and the
84 * name is located in the cache, it will be dropped.
88 * Structures associated with name cacheing.
90 #define NCHHASH(hash) \
91 (&nchashtbl[(hash) & nchash])
92 static LIST_HEAD(nchashhead, namecache) *nchashtbl; /* Hash Table */
93 static TAILQ_HEAD(, namecache) ncneg; /* Hash Table */
94 static u_long nchash; /* size of hash table */
95 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0, "");
96 static u_long ncnegfactor = 16; /* ratio of negative entries */
97 SYSCTL_ULONG(_debug, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0, "");
98 static u_long numneg; /* number of cache entries allocated */
99 SYSCTL_ULONG(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0, "");
100 static u_long numcache; /* number of cache entries allocated */
101 SYSCTL_ULONG(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0, "");
102 struct nchstats nchstats; /* cache effectiveness statistics */
104 static int doingcache = 1; /* 1 => enable the cache */
105 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0, "");
106 SYSCTL_INT(_debug, OID_AUTO, vnsize, CTLFLAG_RD, 0, sizeof(struct vnode), "");
107 SYSCTL_INT(_debug, OID_AUTO, ncsize, CTLFLAG_RD, 0, sizeof(struct namecache), "");
110 * The new name cache statistics
112 SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0, "Name cache statistics");
113 #define STATNODE(mode, name, var) \
114 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, mode, var, 0, "");
115 STATNODE(CTLFLAG_RD, numneg, &numneg);
116 STATNODE(CTLFLAG_RD, numcache, &numcache);
117 static u_long numcalls; STATNODE(CTLFLAG_RD, numcalls, &numcalls);
118 static u_long dothits; STATNODE(CTLFLAG_RD, dothits, &dothits);
119 static u_long dotdothits; STATNODE(CTLFLAG_RD, dotdothits, &dotdothits);
120 static u_long numchecks; STATNODE(CTLFLAG_RD, numchecks, &numchecks);
121 static u_long nummiss; STATNODE(CTLFLAG_RD, nummiss, &nummiss);
122 static u_long nummisszap; STATNODE(CTLFLAG_RD, nummisszap, &nummisszap);
123 static u_long numposzaps; STATNODE(CTLFLAG_RD, numposzaps, &numposzaps);
124 static u_long numposhits; STATNODE(CTLFLAG_RD, numposhits, &numposhits);
125 static u_long numnegzaps; STATNODE(CTLFLAG_RD, numnegzaps, &numnegzaps);
126 static u_long numneghits; STATNODE(CTLFLAG_RD, numneghits, &numneghits);
129 static void cache_zap __P((struct namecache *ncp));
131 MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries");
134 * Flags in namecache.nc_flag
138 * Delete an entry from its hash list and move it to the front
139 * of the LRU list for immediate reuse.
143 struct namecache *ncp;
145 LIST_REMOVE(ncp, nc_hash);
146 LIST_REMOVE(ncp, nc_src);
147 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src))
150 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
152 TAILQ_REMOVE(&ncneg, ncp, nc_dst);
156 free(ncp, M_VFSCACHE);
160 * Lookup an entry in the cache
162 * We don't do this if the segment name is long, simply so the cache
163 * can avoid holding long names (which would either waste space, or
164 * add greatly to the complexity).
166 * Lookup is called with dvp pointing to the directory to search,
167 * cnp pointing to the name of the entry being sought. If the lookup
168 * succeeds, the vnode is returned in *vpp, and a status of -1 is
169 * returned. If the lookup determines that the name does not exist
170 * (negative cacheing), a status of ENOENT is returned. If the lookup
171 * fails, a status of zero is returned.
175 cache_lookup(dvp, vpp, cnp)
178 struct componentname *cnp;
180 struct namecache *ncp;
184 cnp->cn_flags &= ~MAKEENTRY;
190 if (cnp->cn_nameptr[0] == '.') {
191 if (cnp->cn_namelen == 1) {
196 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
198 if (dvp->v_dd->v_id != dvp->v_ddid ||
199 (cnp->cn_flags & MAKEENTRY) == 0) {
208 hash = fnv_32_buf(cnp->cn_nameptr, cnp->cn_namelen, FNV1_32_INIT);
209 hash = fnv_32_buf(&dvp->v_id, sizeof(dvp->v_id), hash);
210 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) {
212 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
213 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
217 /* We failed to find an entry */
219 if ((cnp->cn_flags & MAKEENTRY) == 0) {
228 /* We don't want to have an entry, so dump it */
229 if ((cnp->cn_flags & MAKEENTRY) == 0) {
231 nchstats.ncs_badhits++;
236 /* We found a "positive" match, return the vnode */
239 nchstats.ncs_goodhits++;
244 /* We found a negative match, and want to create it, so purge */
245 if (cnp->cn_nameiop == CREATE) {
247 nchstats.ncs_badhits++;
254 * We found a "negative" match, ENOENT notifies client of this match.
255 * The nc_vpid field records whether this is a whiteout.
257 TAILQ_REMOVE(&ncneg, ncp, nc_dst);
258 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
259 nchstats.ncs_neghits++;
260 if (ncp->nc_flag & NCF_WHITE)
261 cnp->cn_flags |= ISWHITEOUT;
266 * Add an entry to the cache.
269 cache_enter(dvp, vp, cnp)
272 struct componentname *cnp;
274 struct namecache *ncp;
275 struct nchashhead *ncpp;
282 if (cnp->cn_nameptr[0] == '.') {
283 if (cnp->cn_namelen == 1) {
286 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
289 dvp->v_ddid = vp->v_id;
298 ncp = (struct namecache *)
299 malloc(sizeof *ncp + cnp->cn_namelen, M_VFSCACHE, M_WAITOK);
300 bzero((char *)ncp, sizeof *ncp);
304 ncp->nc_flag = cnp->cn_flags & ISWHITEOUT ? NCF_WHITE : 0;
305 } else if (vp->v_type == VDIR) {
307 vp->v_ddid = dvp->v_id;
311 * Fill in cache info, if vp is NULL this is a "negative" cache entry.
312 * For negative entries, we have to record whether it is a whiteout.
313 * the whiteout flag is stored in the nc_vpid field which is
318 len = ncp->nc_nlen = cnp->cn_namelen;
319 hash = fnv_32_buf(cnp->cn_nameptr, len, FNV1_32_INIT);
320 bcopy(cnp->cn_nameptr, ncp->nc_name, len);
321 hash = fnv_32_buf(&dvp->v_id, sizeof(dvp->v_id), hash);
322 ncpp = NCHHASH(hash);
323 LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
324 if (LIST_EMPTY(&dvp->v_cache_src))
326 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
328 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
330 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
332 if (numneg * ncnegfactor > numcache) {
333 ncp = TAILQ_FIRST(&ncneg);
339 * Name cache initialization, from vfs_init() when we are booting
346 nchashtbl = hashinit(desiredvnodes*2, M_VFSCACHE, &nchash);
350 * Invalidate all entries to a particular vnode.
352 * Remove all entries in the namecache relating to this vnode and
353 * change the v_id. We take the v_id from a global counter, since
354 * it becomes a handy sequence number in crash-dumps that way.
355 * No valid vnode will ever have (v_id == 0).
357 * XXX: Only time and the size of v_id prevents this from failing:
358 * XXX: In theory we should hunt down all (struct vnode*, v_id)
359 * XXX: soft references and nuke them, at least on the global
360 * XXX: v_id wraparound. The period of resistance can be extended
361 * XXX: by incrementing each vnodes v_id individually instead of
362 * XXX: using the global v_id.
369 static u_long nextid;
371 while (!LIST_EMPTY(&vp->v_cache_src))
372 cache_zap(LIST_FIRST(&vp->v_cache_src));
373 while (!TAILQ_EMPTY(&vp->v_cache_dst))
374 cache_zap(TAILQ_FIRST(&vp->v_cache_dst));
378 while (nextid == vp->v_id || !nextid);
385 * Flush all entries referencing a particular filesystem.
387 * Since we need to check it anyway, we will flush all the invalid
388 * entries at the same time.
394 struct nchashhead *ncpp;
395 struct namecache *ncp, *nnp;
397 /* Scan hash tables for applicable entries */
398 for (ncpp = &nchashtbl[nchash]; ncpp >= nchashtbl; ncpp--) {
399 for (ncp = LIST_FIRST(ncpp); ncp != 0; ncp = nnp) {
400 nnp = LIST_NEXT(ncp, nc_hash);
401 if (ncp->nc_dvp->v_mount == mp) {
411 * Test whether this (directory) vnode's namei cache entry contains
412 * subdirectories or not. Used to determine whether the directory is
413 * a leaf in the namei cache or not. Note: the directory may still
414 * contain files in the namei cache.
416 * Returns 0 if the directory is a leaf, -1 if it isn't.
419 cache_leaf_test(struct vnode *vp)
421 struct namecache *ncpc;
423 for (ncpc = LIST_FIRST(&vp->v_cache_src);
425 ncpc = LIST_NEXT(ncpc, nc_src)
427 if (ncpc->nc_vp != NULL && ncpc->nc_vp->v_type == VDIR)
434 * Perform canonical checks and cache lookup and pass on to filesystem
435 * through the vop_cachedlookup only if needed.
440 struct vop_lookup_args /* {
442 struct vnode **a_vpp;
443 struct componentname *a_cnp;
446 struct vnode *dvp, *vp;
449 struct vnode **vpp = ap->a_vpp;
450 struct componentname *cnp = ap->a_cnp;
451 struct ucred *cred = cnp->cn_cred;
452 int flags = cnp->cn_flags;
453 struct thread *td = cnp->cn_td;
454 u_long vpid; /* capability number of vnode */
458 lockparent = flags & LOCKPARENT;
460 if (dvp->v_type != VDIR)
463 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
464 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
467 error = VOP_ACCESS(dvp, VEXEC, cred, td);
472 error = cache_lookup(dvp, vpp, cnp);
475 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
482 cnp->cn_flags &= ~PDIRUNLOCK;
483 if (dvp == vp) { /* lookup on "." */
486 } else if (flags & ISDOTDOT) {
487 VOP_UNLOCK(dvp, 0, td);
488 cnp->cn_flags |= PDIRUNLOCK;
489 error = vget(vp, LK_EXCLUSIVE, td);
490 if (!error && lockparent && (flags & ISLASTCN)) {
491 if ((error = vn_lock(dvp, LK_EXCLUSIVE, td)) == 0)
492 cnp->cn_flags &= ~PDIRUNLOCK;
495 error = vget(vp, LK_EXCLUSIVE, td);
496 if (!lockparent || error || !(flags & ISLASTCN)) {
497 VOP_UNLOCK(dvp, 0, td);
498 cnp->cn_flags |= PDIRUNLOCK;
502 * Check that the capability number did not change
503 * while we were waiting for the lock.
506 if (vpid == vp->v_id)
509 if (lockparent && dvp != vp && (flags & ISLASTCN)) {
510 VOP_UNLOCK(dvp, 0, td);
511 cnp->cn_flags |= PDIRUNLOCK;
514 if (cnp->cn_flags & PDIRUNLOCK) {
515 error = vn_lock(dvp, LK_EXCLUSIVE, td);
518 cnp->cn_flags &= ~PDIRUNLOCK;
520 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp));
523 static int disablecwd;
524 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0, "");
526 static u_long numcwdcalls; STATNODE(CTLFLAG_RD, numcwdcalls, &numcwdcalls);
527 static u_long numcwdfail1; STATNODE(CTLFLAG_RD, numcwdfail1, &numcwdfail1);
528 static u_long numcwdfail2; STATNODE(CTLFLAG_RD, numcwdfail2, &numcwdfail2);
529 static u_long numcwdfail3; STATNODE(CTLFLAG_RD, numcwdfail3, &numcwdfail3);
530 static u_long numcwdfail4; STATNODE(CTLFLAG_RD, numcwdfail4, &numcwdfail4);
531 static u_long numcwdfound; STATNODE(CTLFLAG_RD, numcwdfound, &numcwdfound);
534 __getcwd(struct __getcwd_args *uap)
536 struct proc *p = curproc;
538 int error, i, slash_prefixed;
539 struct filedesc *fdp;
540 struct namecache *ncp;
548 if (uap->buflen > MAXPATHLEN)
549 uap->buflen = MAXPATHLEN;
550 buf = bp = malloc(uap->buflen, M_TEMP, M_WAITOK);
551 bp += uap->buflen - 1;
555 for (vp = fdp->fd_cdir; vp != fdp->fd_rdir && vp != rootvnode;) {
556 if (vp->v_flag & VROOT) {
557 if (vp->v_mount == NULL) { /* forced unmount */
561 vp = vp->v_mount->mnt_vnodecovered;
564 if (vp->v_dd->v_id != vp->v_ddid) {
569 ncp = TAILQ_FIRST(&vp->v_cache_dst);
575 if (ncp->nc_dvp != vp->v_dd) {
580 for (i = ncp->nc_nlen - 1; i >= 0; i--) {
586 *--bp = ncp->nc_name[i];
597 if (!slash_prefixed) {
606 error = copyout(bp, uap->buf, strlen(bp) + 1);
612 * Thus begins the fullpath magic.
616 #define STATNODE(name) \
618 SYSCTL_UINT(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, "")
620 static int disablefullpath;
621 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW,
622 &disablefullpath, 0, "");
624 STATNODE(numfullpathcalls);
625 STATNODE(numfullpathfail1);
626 STATNODE(numfullpathfail2);
627 STATNODE(numfullpathfail3);
628 STATNODE(numfullpathfail4);
629 STATNODE(numfullpathfound);
632 textvp_fullpath(struct proc *p, char **retbuf, char **retfreebuf) {
634 int i, slash_prefixed;
635 struct filedesc *fdp;
636 struct namecache *ncp;
637 struct vnode *vp, *textvp;
642 textvp = p->p_textvp;
645 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
646 bp = buf + MAXPATHLEN - 1;
650 for (vp = textvp; vp != fdp->fd_rdir && vp != rootvnode;) {
651 if (vp->v_flag & VROOT) {
652 if (vp->v_mount == NULL) { /* forced unmount */
656 vp = vp->v_mount->mnt_vnodecovered;
659 if (vp != textvp && vp->v_dd->v_id != vp->v_ddid) {
664 ncp = TAILQ_FIRST(&vp->v_cache_dst);
670 if (vp != textvp && ncp->nc_dvp != vp->v_dd) {
675 for (i = ncp->nc_nlen - 1; i >= 0; i--) {
681 *--bp = ncp->nc_name[i];
692 if (!slash_prefixed) {