xref: /csrg-svn/sys/kern/vfs_cache.c (revision 46747)
137487Smckusick /*
237487Smckusick  * Copyright (c) 1989 The Regents of the University of California.
337487Smckusick  * All rights reserved.
437487Smckusick  *
544455Sbostic  * %sccs.include.redist.c%
637487Smckusick  *
7*46747Smckusick  *	@(#)vfs_cache.c	7.8 (Berkeley) 02/28/91
837487Smckusick  */
937487Smckusick 
1037487Smckusick #include "param.h"
1137487Smckusick #include "systm.h"
1237487Smckusick #include "time.h"
1345117Smckusick #include "mount.h"
1437487Smckusick #include "vnode.h"
1537487Smckusick #include "namei.h"
1638768Smckusick #include "errno.h"
1740881Smckusick #include "malloc.h"
1837487Smckusick 
1937487Smckusick /*
2037487Smckusick  * Name caching works as follows:
2137487Smckusick  *
2237487Smckusick  * Names found by directory scans are retained in a cache
2337487Smckusick  * for future reference.  It is managed LRU, so frequently
2437487Smckusick  * used names will hang around.  Cache is indexed by hash value
2538768Smckusick  * obtained from (vp, name) where vp refers to the directory
2638768Smckusick  * containing name.
2737487Smckusick  *
2837487Smckusick  * For simplicity (and economy of storage), names longer than
2937487Smckusick  * a maximum length of NCHNAMLEN are not cached; they occur
3037487Smckusick  * infrequently in any case, and are almost never of interest.
3137487Smckusick  *
3237487Smckusick  * Upon reaching the last segment of a path, if the reference
3337487Smckusick  * is for DELETE, or NOCACHE is set (rewrite), and the
3437487Smckusick  * name is located in the cache, it will be dropped.
3537487Smckusick  */
3637487Smckusick 
3737487Smckusick /*
3837487Smckusick  * Structures associated with name cacheing.
3937487Smckusick  */
4037487Smckusick union nchash {
4137487Smckusick 	union	nchash *nch_head[2];
4237487Smckusick 	struct	namecache *nch_chain[2];
4340881Smckusick } *nchashtbl;
4437487Smckusick #define	nch_forw	nch_chain[0]
4537487Smckusick #define	nch_back	nch_chain[1]
4637487Smckusick 
4740881Smckusick u_long	nchash;				/* size of hash table - 1 */
4840881Smckusick long	numcache;			/* number of cache entries allocated */
4937487Smckusick struct	namecache *nchhead, **nchtail;	/* LRU chain pointers */
5037487Smckusick struct	nchstats nchstats;		/* cache effectiveness statistics */
5137487Smckusick 
5238768Smckusick int doingcache = 1;			/* 1 => enable the cache */
5338768Smckusick 
5437487Smckusick /*
5537487Smckusick  * Look for a the name in the cache. We don't do this
5637487Smckusick  * if the segment name is long, simply so the cache can avoid
5737487Smckusick  * holding long names (which would either waste space, or
5837487Smckusick  * add greatly to the complexity).
5938768Smckusick  *
6038768Smckusick  * Lookup is called with ni_dvp pointing to the directory to search,
6138768Smckusick  * ni_ptr pointing to the name of the entry being sought, ni_namelen
6238768Smckusick  * tells the length of the name, and ni_hash contains a hash of
6338768Smckusick  * the name. If the lookup succeeds, the vnode is returned in ni_vp
6438768Smckusick  * and a status of -1 is returned. If the lookup determines that
6538768Smckusick  * the name does not exist (negative cacheing), a status of ENOENT
6638768Smckusick  * is returned. If the lookup fails, a status of zero is returned.
6737487Smckusick  */
6837487Smckusick cache_lookup(ndp)
6937487Smckusick 	register struct nameidata *ndp;
7037487Smckusick {
7138768Smckusick 	register struct vnode *dvp;
7237487Smckusick 	register struct namecache *ncp;
7337487Smckusick 	union nchash *nhp;
7437487Smckusick 
7538768Smckusick 	if (!doingcache)
7638768Smckusick 		return (0);
7738768Smckusick 	if (ndp->ni_namelen > NCHNAMLEN) {
7837487Smckusick 		nchstats.ncs_long++;
7937487Smckusick 		ndp->ni_makeentry = 0;
8037487Smckusick 		return (0);
8137487Smckusick 	}
8238768Smckusick 	dvp = ndp->ni_dvp;
8340881Smckusick 	nhp = &nchashtbl[ndp->ni_hash & nchash];
8437487Smckusick 	for (ncp = nhp->nch_forw; ncp != (struct namecache *)nhp;
8537487Smckusick 	    ncp = ncp->nc_forw) {
8638768Smckusick 		if (ncp->nc_dvp == dvp &&
8738768Smckusick 		    ncp->nc_dvpid == dvp->v_id &&
8838768Smckusick 		    ncp->nc_nlen == ndp->ni_namelen &&
8938768Smckusick 		    !bcmp(ncp->nc_name, ndp->ni_ptr, (unsigned)ncp->nc_nlen))
9037487Smckusick 			break;
9137487Smckusick 	}
9237487Smckusick 	if (ncp == (struct namecache *)nhp) {
9337487Smckusick 		nchstats.ncs_miss++;
9437487Smckusick 		return (0);
9537487Smckusick 	}
9638768Smckusick 	if (!ndp->ni_makeentry) {
9738768Smckusick 		nchstats.ncs_badhits++;
9838768Smckusick 	} else if (ncp->nc_vp == NULL) {
99*46747Smckusick 		if ((ndp->ni_nameiop & OPMASK) != CREATE) {
100*46747Smckusick 			nchstats.ncs_neghits++;
101*46747Smckusick 			/*
102*46747Smckusick 			 * Move this slot to end of LRU chain,
103*46747Smckusick 			 * if not already there.
104*46747Smckusick 			 */
105*46747Smckusick 			if (ncp->nc_nxt) {
106*46747Smckusick 				/* remove from LRU chain */
107*46747Smckusick 				*ncp->nc_prev = ncp->nc_nxt;
108*46747Smckusick 				ncp->nc_nxt->nc_prev = ncp->nc_prev;
109*46747Smckusick 				/* and replace at end of it */
110*46747Smckusick 				ncp->nc_nxt = NULL;
111*46747Smckusick 				ncp->nc_prev = nchtail;
112*46747Smckusick 				*nchtail = ncp;
113*46747Smckusick 				nchtail = &ncp->nc_nxt;
114*46747Smckusick 			}
115*46747Smckusick 			return (ENOENT);
11637487Smckusick 		}
11738768Smckusick 	} else if (ncp->nc_vpid != ncp->nc_vp->v_id) {
11838768Smckusick 		nchstats.ncs_falsehits++;
11938768Smckusick 	} else {
12037487Smckusick 		nchstats.ncs_goodhits++;
12138768Smckusick 		/*
12238768Smckusick 		 * move this slot to end of LRU chain, if not already there
12338768Smckusick 		 */
12438768Smckusick 		if (ncp->nc_nxt) {
12538768Smckusick 			/* remove from LRU chain */
12638768Smckusick 			*ncp->nc_prev = ncp->nc_nxt;
12738768Smckusick 			ncp->nc_nxt->nc_prev = ncp->nc_prev;
12838768Smckusick 			/* and replace at end of it */
12938768Smckusick 			ncp->nc_nxt = NULL;
13038768Smckusick 			ncp->nc_prev = nchtail;
13138768Smckusick 			*nchtail = ncp;
13238768Smckusick 			nchtail = &ncp->nc_nxt;
13338768Smckusick 		}
13438768Smckusick 		ndp->ni_vp = ncp->nc_vp;
13538768Smckusick 		return (-1);
13637487Smckusick 	}
13737487Smckusick 
13837487Smckusick 	/*
13937487Smckusick 	 * Last component and we are renaming or deleting,
14037487Smckusick 	 * the cache entry is invalid, or otherwise don't
14137487Smckusick 	 * want cache entry to exist.
14237487Smckusick 	 */
14337487Smckusick 	/* remove from LRU chain */
14437487Smckusick 	*ncp->nc_prev = ncp->nc_nxt;
14537487Smckusick 	if (ncp->nc_nxt)
14637487Smckusick 		ncp->nc_nxt->nc_prev = ncp->nc_prev;
14737487Smckusick 	else
14837487Smckusick 		nchtail = ncp->nc_prev;
14938768Smckusick 	/* remove from hash chain */
15038768Smckusick 	remque(ncp);
15137487Smckusick 	/* insert at head of LRU list (first to grab) */
15237487Smckusick 	ncp->nc_nxt = nchhead;
15337487Smckusick 	ncp->nc_prev = &nchhead;
15437487Smckusick 	nchhead->nc_prev = &ncp->nc_nxt;
15537487Smckusick 	nchhead = ncp;
15637487Smckusick 	/* and make a dummy hash chain */
15737487Smckusick 	ncp->nc_forw = ncp;
15837487Smckusick 	ncp->nc_back = ncp;
15937487Smckusick 	return (0);
16037487Smckusick }
16137487Smckusick 
16237487Smckusick /*
16337487Smckusick  * Add an entry to the cache
16437487Smckusick  */
16537487Smckusick cache_enter(ndp)
16637487Smckusick 	register struct nameidata *ndp;
16737487Smckusick {
16837487Smckusick 	register struct namecache *ncp;
16937487Smckusick 	union nchash *nhp;
17037487Smckusick 
17138768Smckusick 	if (!doingcache)
17238768Smckusick 		return;
17337487Smckusick 	/*
17437487Smckusick 	 * Free the cache slot at head of lru chain.
17537487Smckusick 	 */
17640881Smckusick 	if (numcache < desiredvnodes) {
17740881Smckusick 		ncp = (struct namecache *)
17845117Smckusick 			malloc((u_long)sizeof *ncp, M_CACHE, M_WAITOK);
17940881Smckusick 		bzero((char *)ncp, sizeof *ncp);
18040881Smckusick 		numcache++;
18140881Smckusick 	} else if (ncp = nchhead) {
18237487Smckusick 		/* remove from lru chain */
18337487Smckusick 		*ncp->nc_prev = ncp->nc_nxt;
18437487Smckusick 		if (ncp->nc_nxt)
18537487Smckusick 			ncp->nc_nxt->nc_prev = ncp->nc_prev;
18637487Smckusick 		else
18737487Smckusick 			nchtail = ncp->nc_prev;
18838768Smckusick 		/* remove from old hash chain */
18938768Smckusick 		remque(ncp);
19040881Smckusick 	} else
19140881Smckusick 		return;
19240881Smckusick 	/* grab the vnode we just found */
19340881Smckusick 	ncp->nc_vp = ndp->ni_vp;
19440881Smckusick 	if (ndp->ni_vp)
19540881Smckusick 		ncp->nc_vpid = ndp->ni_vp->v_id;
19640881Smckusick 	else
19740881Smckusick 		ncp->nc_vpid = 0;
19840881Smckusick 	/* fill in cache info */
19940881Smckusick 	ncp->nc_dvp = ndp->ni_dvp;
20040881Smckusick 	ncp->nc_dvpid = ndp->ni_dvp->v_id;
20140881Smckusick 	ncp->nc_nlen = ndp->ni_namelen;
20240881Smckusick 	bcopy(ndp->ni_ptr, ncp->nc_name, (unsigned)ncp->nc_nlen);
20340881Smckusick 	/* link at end of lru chain */
20440881Smckusick 	ncp->nc_nxt = NULL;
20540881Smckusick 	ncp->nc_prev = nchtail;
20640881Smckusick 	*nchtail = ncp;
20740881Smckusick 	nchtail = &ncp->nc_nxt;
20840881Smckusick 	/* and insert on hash chain */
20940881Smckusick 	nhp = &nchashtbl[ndp->ni_hash & nchash];
21040881Smckusick 	insque(ncp, nhp);
21137487Smckusick }
21237487Smckusick 
21337487Smckusick /*
21440881Smckusick  * Name cache initialization, from vfs_init() when we are booting
21537487Smckusick  */
21637487Smckusick nchinit()
21737487Smckusick {
21837487Smckusick 	register union nchash *nchp;
21940881Smckusick 	long nchashsize;
22037487Smckusick 
22137487Smckusick 	nchhead = 0;
22237487Smckusick 	nchtail = &nchhead;
22340881Smckusick 	nchashsize = roundup((desiredvnodes + 1) * sizeof *nchp / 2,
22440881Smckusick 		NBPG * CLSIZE);
22545117Smckusick 	nchashtbl = (union nchash *)malloc((u_long)nchashsize,
22645117Smckusick 	    M_CACHE, M_WAITOK);
22740881Smckusick 	for (nchash = 1; nchash <= nchashsize / sizeof *nchp; nchash <<= 1)
22840881Smckusick 		/* void */;
22940881Smckusick 	nchash = (nchash >> 1) - 1;
23040881Smckusick 	for (nchp = &nchashtbl[nchash]; nchp >= nchashtbl; nchp--) {
23137487Smckusick 		nchp->nch_head[0] = nchp;
23237487Smckusick 		nchp->nch_head[1] = nchp;
23337487Smckusick 	}
23437487Smckusick }
23537487Smckusick 
23637487Smckusick /*
23737487Smckusick  * Cache flush, a particular vnode; called when a vnode is renamed to
23838768Smckusick  * hide entries that would now be invalid
23937487Smckusick  */
24037487Smckusick cache_purge(vp)
24138768Smckusick 	struct vnode *vp;
24237487Smckusick {
24340881Smckusick 	union nchash *nhp;
24438768Smckusick 	struct namecache *ncp;
24537487Smckusick 
24638768Smckusick 	vp->v_id = ++nextvnodeid;
24738768Smckusick 	if (nextvnodeid != 0)
24838768Smckusick 		return;
24940881Smckusick 	for (nhp = &nchashtbl[nchash]; nhp >= nchashtbl; nhp--) {
25040881Smckusick 		for (ncp = nhp->nch_forw; ncp != (struct namecache *)nhp;
25140881Smckusick 		    ncp = ncp->nc_forw) {
25240881Smckusick 			ncp->nc_vpid = 0;
25340881Smckusick 			ncp->nc_dvpid = 0;
25440881Smckusick 		}
25537487Smckusick 	}
25638768Smckusick 	vp->v_id = ++nextvnodeid;
25737487Smckusick }
25837487Smckusick 
25937487Smckusick /*
26037487Smckusick  * Cache flush, a whole filesystem; called when filesys is umounted to
26137487Smckusick  * remove entries that would now be invalid
26237487Smckusick  *
26337487Smckusick  * The line "nxtcp = nchhead" near the end is to avoid potential problems
26437487Smckusick  * if the cache lru chain is modified while we are dumping the
26537487Smckusick  * inode.  This makes the algorithm O(n^2), but do you think I care?
26637487Smckusick  */
26737487Smckusick cache_purgevfs(mp)
26845117Smckusick 	struct mount *mp;
26937487Smckusick {
27037487Smckusick 	register struct namecache *ncp, *nxtcp;
27137487Smckusick 
27237487Smckusick 	for (ncp = nchhead; ncp; ncp = nxtcp) {
27337487Smckusick 		nxtcp = ncp->nc_nxt;
27438768Smckusick 		if (ncp->nc_dvp == NULL || ncp->nc_dvp->v_mount != mp)
27537487Smckusick 			continue;
27637487Smckusick 		/* free the resources we had */
27737487Smckusick 		ncp->nc_vp = NULL;
27838768Smckusick 		ncp->nc_dvp = NULL;
27937487Smckusick 		remque(ncp);		/* remove entry from its hash chain */
28037487Smckusick 		ncp->nc_forw = ncp;	/* and make a dummy one */
28137487Smckusick 		ncp->nc_back = ncp;
28237487Smckusick 		/* delete this entry from LRU chain */
28337487Smckusick 		*ncp->nc_prev = nxtcp;
28437487Smckusick 		if (nxtcp)
28537487Smckusick 			nxtcp->nc_prev = ncp->nc_prev;
28637487Smckusick 		else
28737487Smckusick 			nchtail = ncp->nc_prev;
28837487Smckusick 		/* cause rescan of list, it may have altered */
28937487Smckusick 		nxtcp = nchhead;
29037487Smckusick 		/* put the now-free entry at head of LRU */
29137487Smckusick 		ncp->nc_nxt = nxtcp;
29237487Smckusick 		ncp->nc_prev = &nchhead;
29337487Smckusick 		nxtcp->nc_prev = &ncp->nc_nxt;
29437487Smckusick 		nchhead = ncp;
29537487Smckusick 	}
29637487Smckusick }
297