xref: /freebsd/sys/kern/vfs_cache.c (revision 6e8394b8baa7d5d9153ab90de6824bcd19b3b4e1)
1 /*
2  * Copyright (c) 1989, 1993, 1995
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Poul-Henning Kamp of the FreeBSD Project.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
23  *
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
34  * SUCH DAMAGE.
35  *
36  *	@(#)vfs_cache.c	8.5 (Berkeley) 3/22/95
37  * $Id: vfs_cache.c,v 1.38 1998/09/09 07:41:41 bde Exp $
38  */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/sysctl.h>
44 #include <sys/mount.h>
45 #include <sys/vnode.h>
46 #include <sys/namei.h>
47 #include <sys/malloc.h>
48 
49 
50 /*
51  * Name caching works as follows:
52  *
53  * Names found by directory scans are retained in a cache
54  * for future reference.  It is managed LRU, so frequently
55  * used names will hang around.  Cache is indexed by hash value
56  * obtained from (vp, name) where vp refers to the directory
57  * containing name.
58  *
59  * If it is a "negative" entry, (i.e. for a name that is known NOT to
60  * exist) the vnode pointer will be NULL.
61  *
62  * Upon reaching the last segment of a path, if the reference
63  * is for DELETE, or NOCACHE is set (rewrite), and the
64  * name is located in the cache, it will be dropped.
65  */
66 
67 /*
68  * Structures associated with name cacheing.
69  */
70 #define NCHHASH(dvp, cnp) \
71 	(&nchashtbl[((dvp)->v_id + (cnp)->cn_hash) & nchash])
72 static LIST_HEAD(nchashhead, namecache) *nchashtbl;	/* Hash Table */
73 static TAILQ_HEAD(, namecache) ncneg;	/* Hash Table */
74 static u_long	nchash;			/* size of hash table */
75 SYSCTL_INT(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0, "");
76 static u_long	ncnegfactor = 16;	/* ratio of negative entries */
77 SYSCTL_INT(_debug, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0, "");
78 static u_long	numneg;		/* number of cache entries allocated */
79 SYSCTL_INT(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0, "");
80 static u_long	numcache;		/* number of cache entries allocated */
81 SYSCTL_INT(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0, "");
82 struct	nchstats nchstats;		/* cache effectiveness statistics */
83 
84 static int	doingcache = 1;		/* 1 => enable the cache */
85 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0, "");
86 SYSCTL_INT(_debug, OID_AUTO, vnsize, CTLFLAG_RD, 0, sizeof(struct vnode), "");
87 SYSCTL_INT(_debug, OID_AUTO, ncsize, CTLFLAG_RD, 0, sizeof(struct namecache), "");
88 
89 /*
90  * The new name cache statistics
91  */
92 SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0, "Name cache statistics");
93 #define STATNODE(mode, name, var) \
94 	SYSCTL_INT(_vfs_cache, OID_AUTO, name, mode, var, 0, "");
95 STATNODE(CTLFLAG_RD, numneg, &numneg);
96 STATNODE(CTLFLAG_RD, numcache, &numcache);
97 static u_long numcalls; STATNODE(CTLFLAG_RD, numcalls, &numcalls);
98 static u_long dothits; STATNODE(CTLFLAG_RD, dothits, &dothits);
99 static u_long dotdothits; STATNODE(CTLFLAG_RD, dotdothits, &dotdothits);
100 static u_long numchecks; STATNODE(CTLFLAG_RD, numchecks, &numchecks);
101 static u_long nummiss; STATNODE(CTLFLAG_RD, nummiss, &nummiss);
102 static u_long nummisszap; STATNODE(CTLFLAG_RD, nummisszap, &nummisszap);
103 static u_long numposzaps; STATNODE(CTLFLAG_RD, numposzaps, &numposzaps);
104 static u_long numposhits; STATNODE(CTLFLAG_RD, numposhits, &numposhits);
105 static u_long numnegzaps; STATNODE(CTLFLAG_RD, numnegzaps, &numnegzaps);
106 static u_long numneghits; STATNODE(CTLFLAG_RD, numneghits, &numneghits);
107 
108 
109 static void cache_zap __P((struct namecache *ncp));
110 
111 /*
112  * Flags in namecache.nc_flag
113  */
114 #define NCF_WHITE	1
115 /*
116  * Delete an entry from its hash list and move it to the front
117  * of the LRU list for immediate reuse.
118  */
119 static void
120 cache_zap(ncp)
121 	struct namecache *ncp;
122 {
123 	LIST_REMOVE(ncp, nc_hash);
124 	LIST_REMOVE(ncp, nc_src);
125 	if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src))
126 		vdrop(ncp->nc_dvp);
127 	if (ncp->nc_vp) {
128 		TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
129 	} else {
130 		TAILQ_REMOVE(&ncneg, ncp, nc_dst);
131 		numneg--;
132 	}
133 	numcache--;
134 	free(ncp, M_CACHE);
135 }
136 
137 /*
138  * Lookup an entry in the cache
139  *
140  * We don't do this if the segment name is long, simply so the cache
141  * can avoid holding long names (which would either waste space, or
142  * add greatly to the complexity).
143  *
144  * Lookup is called with dvp pointing to the directory to search,
145  * cnp pointing to the name of the entry being sought. If the lookup
146  * succeeds, the vnode is returned in *vpp, and a status of -1 is
147  * returned. If the lookup determines that the name does not exist
148  * (negative cacheing), a status of ENOENT is returned. If the lookup
149  * fails, a status of zero is returned.
150  */
151 
152 int
153 cache_lookup(dvp, vpp, cnp)
154 	struct vnode *dvp;
155 	struct vnode **vpp;
156 	struct componentname *cnp;
157 {
158 	register struct namecache *ncp;
159 
160 	if (!doingcache) {
161 		cnp->cn_flags &= ~MAKEENTRY;
162 		return (0);
163 	}
164 
165 	numcalls++;
166 
167 	if (cnp->cn_nameptr[0] == '.') {
168 		if (cnp->cn_namelen == 1) {
169 			*vpp = dvp;
170 			dothits++;
171 			return (-1);
172 		}
173 		if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
174 			dotdothits++;
175 			if (dvp->v_dd->v_id != dvp->v_ddid ||
176 			    (cnp->cn_flags & MAKEENTRY) == 0) {
177 				dvp->v_ddid = 0;
178 				return (0);
179 			}
180 			*vpp = dvp->v_dd;
181 			return (-1);
182 		}
183 	}
184 
185 	LIST_FOREACH(ncp, (NCHHASH(dvp, cnp)), nc_hash) {
186 		numchecks++;
187 		if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
188 		    !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen))
189 			break;
190 	}
191 
192 	/* We failed to find an entry */
193 	if (ncp == 0) {
194 		if ((cnp->cn_flags & MAKEENTRY) == 0) {
195 			nummisszap++;
196 		} else {
197 			nummiss++;
198 		}
199 		nchstats.ncs_miss++;
200 		return (0);
201 	}
202 
203 	/* We don't want to have an entry, so dump it */
204 	if ((cnp->cn_flags & MAKEENTRY) == 0) {
205 		numposzaps++;
206 		nchstats.ncs_badhits++;
207 		cache_zap(ncp);
208 		return (0);
209 	}
210 
211 	/* We found a "positive" match, return the vnode */
212         if (ncp->nc_vp) {
213 		numposhits++;
214 		nchstats.ncs_goodhits++;
215 		*vpp = ncp->nc_vp;
216 		return (-1);
217 	}
218 
219 	/* We found a negative match, and want to create it, so purge */
220 	if (cnp->cn_nameiop == CREATE) {
221 		numnegzaps++;
222 		nchstats.ncs_badhits++;
223 		cache_zap(ncp);
224 		return (0);
225 	}
226 
227 	numneghits++;
228 	/*
229 	 * We found a "negative" match, ENOENT notifies client of this match.
230 	 * The nc_vpid field records whether this is a whiteout.
231 	 */
232 	TAILQ_REMOVE(&ncneg, ncp, nc_dst);
233 	TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
234 	nchstats.ncs_neghits++;
235 	if (ncp->nc_flag & NCF_WHITE)
236 		cnp->cn_flags |= ISWHITEOUT;
237 	return (ENOENT);
238 }
239 
240 /*
241  * Add an entry to the cache.
242  */
243 void
244 cache_enter(dvp, vp, cnp)
245 	struct vnode *dvp;
246 	struct vnode *vp;
247 	struct componentname *cnp;
248 {
249 	register struct namecache *ncp;
250 	register struct nchashhead *ncpp;
251 
252 	if (!doingcache)
253 		return;
254 
255 	if (cnp->cn_nameptr[0] == '.') {
256 		if (cnp->cn_namelen == 1) {
257 			return;
258 		}
259 		if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
260 			if (vp) {
261 				dvp->v_dd = vp;
262 				dvp->v_ddid = vp->v_id;
263 			} else {
264 				dvp->v_dd = dvp;
265 				dvp->v_ddid = 0;
266 			}
267 			return;
268 		}
269 	}
270 
271 	ncp = (struct namecache *)
272 		malloc(sizeof *ncp + cnp->cn_namelen, M_CACHE, M_WAITOK);
273 	bzero((char *)ncp, sizeof *ncp);
274 	numcache++;
275 	if (!vp) {
276 		numneg++;
277 		ncp->nc_flag = cnp->cn_flags & ISWHITEOUT ? NCF_WHITE : 0;
278 	} else if (vp->v_type == VDIR) {
279 		vp->v_dd = dvp;
280 		vp->v_ddid = dvp->v_id;
281 	}
282 
283 	/*
284 	 * Fill in cache info, if vp is NULL this is a "negative" cache entry.
285 	 * For negative entries, we have to record whether it is a whiteout.
286 	 * the whiteout flag is stored in the nc_vpid field which is
287 	 * otherwise unused.
288 	 */
289 	ncp->nc_vp = vp;
290 	ncp->nc_dvp = dvp;
291 	ncp->nc_nlen = cnp->cn_namelen;
292 	bcopy(cnp->cn_nameptr, ncp->nc_name, ncp->nc_nlen);
293 	ncpp = NCHHASH(dvp, cnp);
294 	LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
295 	if (LIST_EMPTY(&dvp->v_cache_src))
296 		vhold(dvp);
297 	LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
298 	if (vp) {
299 		TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
300 	} else {
301 		TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
302 	}
303 	if (numneg*ncnegfactor > numcache) {
304 		ncp = TAILQ_FIRST(&ncneg);
305 		cache_zap(ncp);
306 	}
307 }
308 
309 /*
310  * Name cache initialization, from vfs_init() when we are booting
311  */
312 void
313 nchinit()
314 {
315 
316 	TAILQ_INIT(&ncneg);
317 	nchashtbl = hashinit(desiredvnodes*2, M_CACHE, &nchash);
318 }
319 
320 /*
321  * Invalidate all entries to a particular vnode.
322  *
323  * Remove all entries in the namecache relating to this vnode and
324  * change the v_id.  We take the v_id from a global counter, since
325  * it becomes a handy sequence number in crash-dumps that way.
326  * No valid vnode will ever have (v_id == 0).
327  *
328  * XXX: Only time and the size of v_id prevents this from failing:
329  * XXX: In theory we should hunt down all (struct vnode*, v_id)
330  * XXX: soft references and nuke them, at least on the global
331  * XXX: v_id wraparound.  The period of resistance can be extended
332  * XXX: by incrementing each vnodes v_id individually instead of
333  * XXX: using the global v_id.
334  */
335 
336 void
337 cache_purge(vp)
338 	struct vnode *vp;
339 {
340 	static u_long nextid;
341 
342 	while (!LIST_EMPTY(&vp->v_cache_src))
343 		cache_zap(LIST_FIRST(&vp->v_cache_src));
344 	while (!TAILQ_EMPTY(&vp->v_cache_dst))
345 		cache_zap(TAILQ_FIRST(&vp->v_cache_dst));
346 
347 	do
348 		nextid++;
349 	while (nextid == vp->v_id || !nextid);
350 	vp->v_id = nextid;
351 	vp->v_dd = vp;
352 	vp->v_ddid = 0;
353 }
354 
355 /*
356  * Flush all entries referencing a particular filesystem.
357  *
358  * Since we need to check it anyway, we will flush all the invalid
359  * entries at the same time.
360  */
361 void
362 cache_purgevfs(mp)
363 	struct mount *mp;
364 {
365 	struct nchashhead *ncpp;
366 	struct namecache *ncp, *nnp;
367 
368 	/* Scan hash tables for applicable entries */
369 	for (ncpp = &nchashtbl[nchash]; ncpp >= nchashtbl; ncpp--) {
370 		for (ncp = LIST_FIRST(ncpp); ncp != 0; ncp = nnp) {
371 			nnp = LIST_NEXT(ncp, nc_hash);
372 			if (ncp->nc_dvp->v_mount == mp) {
373 				cache_zap(ncp);
374 			}
375 		}
376 	}
377 }
378 
379 /*
380  * Perform canonical checks and cache lookup and pass on to filesystem
381  * through the vop_cachedlookup only if needed.
382  */
383 
384 int
385 vfs_cache_lookup(ap)
386 	struct vop_lookup_args /* {
387 		struct vnode *a_dvp;
388 		struct vnode **a_vpp;
389 		struct componentname *a_cnp;
390 	} */ *ap;
391 {
392 	struct vnode *vdp;
393 	struct vnode *pdp;
394 	int lockparent;
395 	int error;
396 	struct vnode **vpp = ap->a_vpp;
397 	struct componentname *cnp = ap->a_cnp;
398 	struct ucred *cred = cnp->cn_cred;
399 	int flags = cnp->cn_flags;
400 	struct proc *p = cnp->cn_proc;
401 	u_long vpid;	/* capability number of vnode */
402 
403 	*vpp = NULL;
404 	vdp = ap->a_dvp;
405 	lockparent = flags & LOCKPARENT;
406 
407 	if (vdp->v_type != VDIR)
408                 return (ENOTDIR);
409 
410 	if ((flags & ISLASTCN) && (vdp->v_mount->mnt_flag & MNT_RDONLY) &&
411 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
412 		return (EROFS);
413 
414 	error = VOP_ACCESS(vdp, VEXEC, cred, cnp->cn_proc);
415 
416 	if (error)
417 		return (error);
418 
419 	error = cache_lookup(vdp, vpp, cnp);
420 
421 	if (!error)
422 		return (VOP_CACHEDLOOKUP(ap->a_dvp, ap->a_vpp, ap->a_cnp));
423 
424 	if (error == ENOENT)
425 		return (error);
426 
427 	pdp = vdp;
428 	vdp = *vpp;
429 	vpid = vdp->v_id;
430 	if (pdp == vdp) {   /* lookup on "." */
431 		VREF(vdp);
432 		error = 0;
433 	} else if (flags & ISDOTDOT) {
434 		VOP_UNLOCK(pdp, 0, p);
435 		error = vget(vdp, LK_EXCLUSIVE, p);
436 		if (!error && lockparent && (flags & ISLASTCN))
437 			error = vn_lock(pdp, LK_EXCLUSIVE, p);
438 	} else {
439 		error = vget(vdp, LK_EXCLUSIVE, p);
440 		if (!lockparent || error || !(flags & ISLASTCN))
441 			VOP_UNLOCK(pdp, 0, p);
442 	}
443 	/*
444 	 * Check that the capability number did not change
445 	 * while we were waiting for the lock.
446 	 */
447 	if (!error) {
448 		if (vpid == vdp->v_id)
449 			return (0);
450 		vput(vdp);
451 		if (lockparent && pdp != vdp && (flags & ISLASTCN))
452 			VOP_UNLOCK(pdp, 0, p);
453 	}
454 	error = vn_lock(pdp, LK_EXCLUSIVE, p);
455 	if (error)
456 		return (error);
457 	return (VOP_CACHEDLOOKUP(ap->a_dvp, ap->a_vpp, ap->a_cnp));
458 }
459