xref: /freebsd/sys/kern/vfs_cache.c (revision a8445737e740901f5f2c8d24c12ef7fc8b00134e)
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.37 1997/12/19 23:18:37 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 particular vnode.
322  *
323  * We actually just increment the v_id, that will do it. The stale entries
324  * will be purged by lookup as they get found. If the v_id wraps around, we
325  * need to ditch the entire cache, to avoid confusion. No valid vnode will
326  * ever have (v_id == 0).
327  */
328 void
329 cache_purge(vp)
330 	struct vnode *vp;
331 {
332 	static u_long nextid;
333 
334 	while (!LIST_EMPTY(&vp->v_cache_src))
335 		cache_zap(LIST_FIRST(&vp->v_cache_src));
336 	while (!TAILQ_EMPTY(&vp->v_cache_dst))
337 		cache_zap(TAILQ_FIRST(&vp->v_cache_dst));
338 
339 	nextid++;
340 	while (nextid == vp->v_id || !nextid)
341 		continue;
342 	vp->v_id = nextid;
343 	vp->v_dd = vp;
344 	vp->v_ddid = 0;
345 }
346 
347 /*
348  * Flush all entries referencing a particular filesystem.
349  *
350  * Since we need to check it anyway, we will flush all the invalid
351  * entries at the same time.
352  */
353 void
354 cache_purgevfs(mp)
355 	struct mount *mp;
356 {
357 	struct nchashhead *ncpp;
358 	struct namecache *ncp, *nnp;
359 
360 	/* Scan hash tables for applicable entries */
361 	for (ncpp = &nchashtbl[nchash]; ncpp >= nchashtbl; ncpp--) {
362 		for (ncp = LIST_FIRST(ncpp); ncp != 0; ncp = nnp) {
363 			nnp = LIST_NEXT(ncp, nc_hash);
364 			if (ncp->nc_dvp->v_mount == mp) {
365 				cache_zap(ncp);
366 			}
367 		}
368 	}
369 }
370 
371 /*
372  * Perform canonical checks and cache lookup and pass on to filesystem
373  * through the vop_cachedlookup only if needed.
374  */
375 
376 int
377 vfs_cache_lookup(ap)
378 	struct vop_lookup_args /* {
379 		struct vnode *a_dvp;
380 		struct vnode **a_vpp;
381 		struct componentname *a_cnp;
382 	} */ *ap;
383 {
384 	struct vnode *vdp;
385 	struct vnode *pdp;
386 	int lockparent;
387 	int error;
388 	struct vnode **vpp = ap->a_vpp;
389 	struct componentname *cnp = ap->a_cnp;
390 	struct ucred *cred = cnp->cn_cred;
391 	int flags = cnp->cn_flags;
392 	struct proc *p = cnp->cn_proc;
393 	u_long vpid;	/* capability number of vnode */
394 
395 	*vpp = NULL;
396 	vdp = ap->a_dvp;
397 	lockparent = flags & LOCKPARENT;
398 
399 	if (vdp->v_type != VDIR)
400                 return (ENOTDIR);
401 
402 	if ((flags & ISLASTCN) && (vdp->v_mount->mnt_flag & MNT_RDONLY) &&
403 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
404 		return (EROFS);
405 
406 	error = VOP_ACCESS(vdp, VEXEC, cred, cnp->cn_proc);
407 
408 	if (error)
409 		return (error);
410 
411 	error = cache_lookup(vdp, vpp, cnp);
412 
413 	if (!error)
414 		return (VOP_CACHEDLOOKUP(ap->a_dvp, ap->a_vpp, ap->a_cnp));
415 
416 	if (error == ENOENT)
417 		return (error);
418 
419 	pdp = vdp;
420 	vdp = *vpp;
421 	vpid = vdp->v_id;
422 	if (pdp == vdp) {   /* lookup on "." */
423 		VREF(vdp);
424 		error = 0;
425 	} else if (flags & ISDOTDOT) {
426 		VOP_UNLOCK(pdp, 0, p);
427 		error = vget(vdp, LK_EXCLUSIVE, p);
428 		if (!error && lockparent && (flags & ISLASTCN))
429 			error = vn_lock(pdp, LK_EXCLUSIVE, p);
430 	} else {
431 		error = vget(vdp, LK_EXCLUSIVE, p);
432 		if (!lockparent || error || !(flags & ISLASTCN))
433 			VOP_UNLOCK(pdp, 0, p);
434 	}
435 	/*
436 	 * Check that the capability number did not change
437 	 * while we were waiting for the lock.
438 	 */
439 	if (!error) {
440 		if (vpid == vdp->v_id)
441 			return (0);
442 		vput(vdp);
443 		if (lockparent && pdp != vdp && (flags & ISLASTCN))
444 			VOP_UNLOCK(pdp, 0, p);
445 	}
446 	error = vn_lock(pdp, LK_EXCLUSIVE, p);
447 	if (error)
448 		return (error);
449 	return (VOP_CACHEDLOOKUP(ap->a_dvp, ap->a_vpp, ap->a_cnp));
450 }
451