vfs_cache.c revision 30474
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.34 1997/10/15 13:22:52 phk 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])
72static LIST_HEAD(nchashhead, namecache) *nchashtbl;	/* Hash Table */
73static TAILQ_HEAD(, namecache) ncneg;	/* Hash Table */
74static u_long	nchash;			/* size of hash table */
75SYSCTL_INT(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0, "");
76static u_long	ncnegfactor = 16;	/* ratio of negative entries */
77SYSCTL_INT(_debug, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0, "");
78static u_long	numneg;		/* number of cache entries allocated */
79SYSCTL_INT(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0, "");
80static u_long	numcache;		/* number of cache entries allocated */
81SYSCTL_INT(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0, "");
82struct	nchstats nchstats;		/* cache effectiveness statistics */
83
84static int	doingcache = 1;		/* 1 => enable the cache */
85SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0, "");
86SYSCTL_INT(_debug, OID_AUTO, vnsize, CTLFLAG_RD, 0, sizeof(struct vnode), "");
87SYSCTL_INT(_debug, OID_AUTO, ncsize, CTLFLAG_RD, 0, sizeof(struct namecache), "");
88
89/*
90 * The new name cache statistics
91 */
92SYSCTL_NODE(_vfs, CTL_VFS,	  cache,     CTLFLAG_RW, 0,
93	"Name cache statistics");
94#define STATNODE(mode, name, var) \
95	SYSCTL_INT(_vfs_cache, OID_AUTO, name, mode, var, 0, "");
96STATNODE(CTLFLAG_RD, numneg, &numneg);
97STATNODE(CTLFLAG_RD, numcache, &numcache);
98static u_long numcalls; STATNODE(CTLFLAG_RD, numcalls, &numcalls);
99static u_long dothits; STATNODE(CTLFLAG_RD, dothits, &dothits);
100static u_long dotdothits; STATNODE(CTLFLAG_RD, dotdothits, &dotdothits);
101static u_long numchecks; STATNODE(CTLFLAG_RD, numchecks, &numchecks);
102static u_long nummiss; STATNODE(CTLFLAG_RD, nummiss, &nummiss);
103static u_long nummisszap; STATNODE(CTLFLAG_RD, nummisszap, &nummisszap);
104static u_long numposzaps; STATNODE(CTLFLAG_RD, numposzaps, &numposzaps);
105static u_long numposhits; STATNODE(CTLFLAG_RD, numposhits, &numposhits);
106static u_long numnegzaps; STATNODE(CTLFLAG_RD, numnegzaps, &numnegzaps);
107static u_long numneghits; STATNODE(CTLFLAG_RD, numneghits, &numneghits);
108
109
110static void cache_zap __P((struct namecache *ncp));
111
112/*
113 * Flags in namecache.nc_flag
114 */
115#define NCF_WHITE	1
116/*
117 * Delete an entry from its hash list and move it to the front
118 * of the LRU list for immediate reuse.
119 */
120static void
121cache_zap(ncp)
122	struct namecache *ncp;
123{
124	LIST_REMOVE(ncp, nc_hash);
125	LIST_REMOVE(ncp, nc_src);
126	if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src))
127		vdrop(ncp->nc_dvp);
128	if (ncp->nc_vp) {
129		TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst);
130	} else {
131		TAILQ_REMOVE(&ncneg, ncp, nc_dst);
132		numneg--;
133	}
134	numcache--;
135	free(ncp, M_CACHE);
136}
137
138/*
139 * Lookup an entry in the cache
140 *
141 * We don't do this if the segment name is long, simply so the cache
142 * can avoid holding long names (which would either waste space, or
143 * add greatly to the complexity).
144 *
145 * Lookup is called with dvp pointing to the directory to search,
146 * cnp pointing to the name of the entry being sought. If the lookup
147 * succeeds, the vnode is returned in *vpp, and a status of -1 is
148 * returned. If the lookup determines that the name does not exist
149 * (negative cacheing), a status of ENOENT is returned. If the lookup
150 * fails, a status of zero is returned.
151 */
152
153int
154cache_lookup(dvp, vpp, cnp)
155	struct vnode *dvp;
156	struct vnode **vpp;
157	struct componentname *cnp;
158{
159	register struct namecache *ncp, *nnp;
160	register struct nchashhead *ncpp;
161
162	if (!doingcache) {
163		cnp->cn_flags &= ~MAKEENTRY;
164		return (0);
165	}
166
167	numcalls++;
168
169	if (cnp->cn_nameptr[0] == '.') {
170		if (cnp->cn_namelen == 1) {
171			*vpp = dvp;
172			dothits++;
173			return (-1);
174		}
175		if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
176			dotdothits++;
177			if (dvp->v_dd->v_id != dvp->v_ddid ||
178			    (cnp->cn_flags & MAKEENTRY) == 0) {
179				dvp->v_ddid = 0;
180				return (0);
181			}
182			*vpp = dvp->v_dd;
183			return (-1);
184		}
185	}
186
187	LIST_FOREACH(ncp, (NCHHASH(dvp, cnp)), nc_hash) {
188		numchecks++;
189		if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen &&
190		    !bcmp(ncp->nc_name, cnp->cn_nameptr, (u_int)ncp->nc_nlen))
191			break;
192	}
193
194	/* We failed to find an entry */
195	if (ncp == 0) {
196		if ((cnp->cn_flags & MAKEENTRY) == 0) {
197			nummisszap++;
198		} else {
199			nummiss++;
200		}
201		nchstats.ncs_miss++;
202		return (0);
203	}
204
205	/* We don't want to have an entry, so dump it */
206	if ((cnp->cn_flags & MAKEENTRY) == 0) {
207		numposzaps++;
208		nchstats.ncs_badhits++;
209		cache_zap(ncp);
210		return (0);
211	}
212
213	/* We found a "positive" match, return the vnode */
214        if (ncp->nc_vp) {
215		numposhits++;
216		nchstats.ncs_goodhits++;
217		*vpp = ncp->nc_vp;
218		return (-1);
219	}
220
221	/* We found a negative match, and want to create it, so purge */
222	if (cnp->cn_nameiop == CREATE) {
223		numnegzaps++;
224		nchstats.ncs_badhits++;
225		cache_zap(ncp);
226		return (0);
227	}
228
229	numneghits++;
230	/*
231	 * We found a "negative" match, ENOENT notifies client of this match.
232	 * The nc_vpid field records whether this is a whiteout.
233	 */
234	TAILQ_REMOVE(&ncneg, ncp, nc_dst);
235	TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
236	nchstats.ncs_neghits++;
237	if (ncp->nc_flag & NCF_WHITE)
238		cnp->cn_flags |= ISWHITEOUT;
239	return (ENOENT);
240}
241
242/*
243 * Add an entry to the cache.
244 */
245void
246cache_enter(dvp, vp, cnp)
247	struct vnode *dvp;
248	struct vnode *vp;
249	struct componentname *cnp;
250{
251	register struct namecache *ncp;
252	register struct nchashhead *ncpp;
253
254	if (!doingcache)
255		return;
256
257	if (cnp->cn_nameptr[0] == '.') {
258		if (cnp->cn_namelen == 1) {
259			return;
260		}
261		if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') {
262			if (vp) {
263				dvp->v_dd = vp;
264				dvp->v_ddid = vp->v_id;
265			} else {
266				dvp->v_dd = dvp;
267				dvp->v_ddid = 0;
268			}
269			return;
270		}
271	}
272
273	ncp = (struct namecache *)
274		malloc(sizeof *ncp + cnp->cn_namelen, M_CACHE, M_WAITOK);
275	bzero((char *)ncp, sizeof *ncp);
276	numcache++;
277	if (!vp) {
278		numneg++;
279		ncp->nc_flag = cnp->cn_flags & ISWHITEOUT ? NCF_WHITE : 0;
280	} else if (vp->v_type == VDIR) {
281		vp->v_dd = dvp;
282		vp->v_ddid = dvp->v_id;
283	}
284
285	/*
286	 * Fill in cache info, if vp is NULL this is a "negative" cache entry.
287	 * For negative entries, we have to record whether it is a whiteout.
288	 * the whiteout flag is stored in the nc_vpid field which is
289	 * otherwise unused.
290	 */
291	ncp->nc_vp = vp;
292	ncp->nc_dvp = dvp;
293	ncp->nc_nlen = cnp->cn_namelen;
294	bcopy(cnp->cn_nameptr, ncp->nc_name, (unsigned)ncp->nc_nlen);
295	ncpp = NCHHASH(dvp, cnp);
296	LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
297	if (LIST_EMPTY(&dvp->v_cache_src))
298		vhold(dvp);
299	LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src);
300	if (vp) {
301		TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst);
302	} else {
303		TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst);
304	}
305	if (numneg*ncnegfactor > numcache) {
306		ncp = TAILQ_FIRST(&ncneg);
307		cache_zap(ncp);
308	}
309}
310
311/*
312 * Name cache initialization, from vfs_init() when we are booting
313 */
314void
315nchinit()
316{
317
318	TAILQ_INIT(&ncneg);
319	nchashtbl = hashinit(desiredvnodes*2, M_CACHE, &nchash);
320}
321
322/*
323 * Invalidate all entries to particular vnode.
324 *
325 * We actually just increment the v_id, that will do it. The stale entries
326 * will be purged by lookup as they get found. If the v_id wraps around, we
327 * need to ditch the entire cache, to avoid confusion. No valid vnode will
328 * ever have (v_id == 0).
329 */
330void
331cache_purge(vp)
332	struct vnode *vp;
333{
334	struct namecache *ncp;
335	struct nchashhead *ncpp;
336	static u_long nextid;
337
338	while (!LIST_EMPTY(&vp->v_cache_src))
339		cache_zap(LIST_FIRST(&vp->v_cache_src));
340	while (!TAILQ_EMPTY(&vp->v_cache_dst))
341		cache_zap(TAILQ_FIRST(&vp->v_cache_dst));
342
343	nextid++;
344	while (nextid == vp->v_id || !nextid)
345		continue;
346	vp->v_id = nextid;
347	vp->v_dd = vp;
348	vp->v_ddid = 0;
349}
350
351/*
352 * Flush all entries referencing a particular filesystem.
353 *
354 * Since we need to check it anyway, we will flush all the invalid
355 * entries at the same time.
356 */
357void
358cache_purgevfs(mp)
359	struct mount *mp;
360{
361	struct nchashhead *ncpp;
362	struct namecache *ncp, *nnp;
363
364	/* Scan hash tables for applicable entries */
365	for (ncpp = &nchashtbl[nchash]; ncpp >= nchashtbl; ncpp--) {
366		for (ncp = LIST_FIRST(ncpp); ncp != 0; ncp = nnp) {
367			nnp = LIST_NEXT(ncp, nc_hash);
368			if (ncp->nc_dvp->v_mount == mp) {
369				cache_zap(ncp);
370			}
371		}
372	}
373}
374
375/*
376 * Perform canonical checks and cache lookup and pass on to filesystem
377 * through the vop_cachedlookup only if needed.
378 */
379
380int
381vfs_cache_lookup(ap)
382	struct vop_lookup_args /* {
383		struct vnode *a_dvp;
384		struct vnode **a_vpp;
385		struct componentname *a_cnp;
386	} */ *ap;
387{
388	struct vnode *vdp;
389	struct vnode *pdp;
390	int lockparent;
391	int error;
392	struct vnode **vpp = ap->a_vpp;
393	struct componentname *cnp = ap->a_cnp;
394	struct ucred *cred = cnp->cn_cred;
395	int flags = cnp->cn_flags;
396	struct proc *p = cnp->cn_proc;
397	u_long vpid;	/* capability number of vnode */
398
399	*vpp = NULL;
400	vdp = ap->a_dvp;
401	lockparent = flags & LOCKPARENT;
402
403	if (vdp->v_type != VDIR)
404                return (ENOTDIR);
405
406	if ((flags & ISLASTCN) && (vdp->v_mount->mnt_flag & MNT_RDONLY) &&
407	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
408		return (EROFS);
409
410	error = VOP_ACCESS(vdp, VEXEC, cred, cnp->cn_proc);
411
412	if (error)
413		return (error);
414
415	error = cache_lookup(vdp, vpp, cnp);
416
417	if (!error)
418		return (VOP_CACHEDLOOKUP(ap->a_dvp, ap->a_vpp, ap->a_cnp));
419
420	if (error == ENOENT)
421		return (error);
422
423	pdp = vdp;
424	vdp = *vpp;
425	vpid = vdp->v_id;
426	if (pdp == vdp) {   /* lookup on "." */
427		VREF(vdp);
428		error = 0;
429	} else if (flags & ISDOTDOT) {
430		VOP_UNLOCK(pdp, 0, p);
431		error = vget(vdp, LK_EXCLUSIVE, p);
432		if (!error && lockparent && (flags & ISLASTCN))
433			error = vn_lock(pdp, LK_EXCLUSIVE, p);
434	} else {
435		error = vget(vdp, LK_EXCLUSIVE, p);
436		if (!lockparent || error || !(flags & ISLASTCN))
437			VOP_UNLOCK(pdp, 0, p);
438	}
439	/*
440	 * Check that the capability number did not change
441	 * while we were waiting for the lock.
442	 */
443	if (!error) {
444		if (vpid == vdp->v_id)
445			return (0);
446		vput(vdp);
447		if (lockparent && pdp != vdp && (flags & ISLASTCN))
448			VOP_UNLOCK(pdp, 0, p);
449	}
450	error = vn_lock(pdp, LK_EXCLUSIVE, p);
451	if (error)
452		return (error);
453	return (VOP_CACHEDLOOKUP(ap->a_dvp, ap->a_vpp, ap->a_cnp));
454}
455