xref: /csrg-svn/sys/kern/vfs_cache.c (revision 44455)
137487Smckusick /*
237487Smckusick  * Copyright (c) 1989 The Regents of the University of California.
337487Smckusick  * All rights reserved.
437487Smckusick  *
5*44455Sbostic  * %sccs.include.redist.c%
637487Smckusick  *
7*44455Sbostic  *	@(#)vfs_cache.c	7.6 (Berkeley) 06/28/90
837487Smckusick  */
937487Smckusick 
1037487Smckusick #include "param.h"
1137487Smckusick #include "systm.h"
1237487Smckusick #include "time.h"
1337487Smckusick #include "vnode.h"
1437487Smckusick #include "namei.h"
1538768Smckusick #include "errno.h"
1640881Smckusick #include "malloc.h"
1737487Smckusick 
1837487Smckusick /*
1937487Smckusick  * Name caching works as follows:
2037487Smckusick  *
2137487Smckusick  * Names found by directory scans are retained in a cache
2237487Smckusick  * for future reference.  It is managed LRU, so frequently
2337487Smckusick  * used names will hang around.  Cache is indexed by hash value
2438768Smckusick  * obtained from (vp, name) where vp refers to the directory
2538768Smckusick  * containing name.
2637487Smckusick  *
2737487Smckusick  * For simplicity (and economy of storage), names longer than
2837487Smckusick  * a maximum length of NCHNAMLEN are not cached; they occur
2937487Smckusick  * infrequently in any case, and are almost never of interest.
3037487Smckusick  *
3137487Smckusick  * Upon reaching the last segment of a path, if the reference
3237487Smckusick  * is for DELETE, or NOCACHE is set (rewrite), and the
3337487Smckusick  * name is located in the cache, it will be dropped.
3437487Smckusick  */
3537487Smckusick 
3637487Smckusick /*
3737487Smckusick  * Structures associated with name cacheing.
3837487Smckusick  */
3937487Smckusick union nchash {
4037487Smckusick 	union	nchash *nch_head[2];
4137487Smckusick 	struct	namecache *nch_chain[2];
4240881Smckusick } *nchashtbl;
4337487Smckusick #define	nch_forw	nch_chain[0]
4437487Smckusick #define	nch_back	nch_chain[1]
4537487Smckusick 
4640881Smckusick u_long	nchash;				/* size of hash table - 1 */
4740881Smckusick long	numcache;			/* number of cache entries allocated */
4837487Smckusick struct	namecache *nchhead, **nchtail;	/* LRU chain pointers */
4937487Smckusick struct	nchstats nchstats;		/* cache effectiveness statistics */
5037487Smckusick 
5138768Smckusick int doingcache = 1;			/* 1 => enable the cache */
5238768Smckusick 
5337487Smckusick /*
5437487Smckusick  * Look for a the name in the cache. We don't do this
5537487Smckusick  * if the segment name is long, simply so the cache can avoid
5637487Smckusick  * holding long names (which would either waste space, or
5737487Smckusick  * add greatly to the complexity).
5838768Smckusick  *
5938768Smckusick  * Lookup is called with ni_dvp pointing to the directory to search,
6038768Smckusick  * ni_ptr pointing to the name of the entry being sought, ni_namelen
6138768Smckusick  * tells the length of the name, and ni_hash contains a hash of
6238768Smckusick  * the name. If the lookup succeeds, the vnode is returned in ni_vp
6338768Smckusick  * and a status of -1 is returned. If the lookup determines that
6438768Smckusick  * the name does not exist (negative cacheing), a status of ENOENT
6538768Smckusick  * is returned. If the lookup fails, a status of zero is returned.
6637487Smckusick  */
6737487Smckusick cache_lookup(ndp)
6837487Smckusick 	register struct nameidata *ndp;
6937487Smckusick {
7038768Smckusick 	register struct vnode *dvp;
7137487Smckusick 	register struct namecache *ncp;
7237487Smckusick 	union nchash *nhp;
7337487Smckusick 
7438768Smckusick 	if (!doingcache)
7538768Smckusick 		return (0);
7638768Smckusick 	if (ndp->ni_namelen > NCHNAMLEN) {
7737487Smckusick 		nchstats.ncs_long++;
7837487Smckusick 		ndp->ni_makeentry = 0;
7937487Smckusick 		return (0);
8037487Smckusick 	}
8138768Smckusick 	dvp = ndp->ni_dvp;
8240881Smckusick 	nhp = &nchashtbl[ndp->ni_hash & nchash];
8337487Smckusick 	for (ncp = nhp->nch_forw; ncp != (struct namecache *)nhp;
8437487Smckusick 	    ncp = ncp->nc_forw) {
8538768Smckusick 		if (ncp->nc_dvp == dvp &&
8638768Smckusick 		    ncp->nc_dvpid == dvp->v_id &&
8738768Smckusick 		    ncp->nc_nlen == ndp->ni_namelen &&
8838768Smckusick 		    !bcmp(ncp->nc_name, ndp->ni_ptr, (unsigned)ncp->nc_nlen))
8937487Smckusick 			break;
9037487Smckusick 	}
9137487Smckusick 	if (ncp == (struct namecache *)nhp) {
9237487Smckusick 		nchstats.ncs_miss++;
9337487Smckusick 		return (0);
9437487Smckusick 	}
9538768Smckusick 	if (!ndp->ni_makeentry) {
9638768Smckusick 		nchstats.ncs_badhits++;
9738768Smckusick 	} else if (ncp->nc_vp == NULL) {
9838768Smckusick 		nchstats.ncs_neghits++;
9937487Smckusick 		/*
10038768Smckusick 		 * move this slot to end of LRU chain, if not already there
10137487Smckusick 		 */
10237487Smckusick 		if (ncp->nc_nxt) {
10337487Smckusick 			/* remove from LRU chain */
10437487Smckusick 			*ncp->nc_prev = ncp->nc_nxt;
10537487Smckusick 			ncp->nc_nxt->nc_prev = ncp->nc_prev;
10637487Smckusick 			/* and replace at end of it */
10737487Smckusick 			ncp->nc_nxt = NULL;
10837487Smckusick 			ncp->nc_prev = nchtail;
10937487Smckusick 			*nchtail = ncp;
11037487Smckusick 			nchtail = &ncp->nc_nxt;
11137487Smckusick 		}
11238768Smckusick 		return (ENOENT);
11338768Smckusick 	} else if (ncp->nc_vpid != ncp->nc_vp->v_id) {
11438768Smckusick 		nchstats.ncs_falsehits++;
11538768Smckusick 	} else {
11637487Smckusick 		nchstats.ncs_goodhits++;
11738768Smckusick 		/*
11838768Smckusick 		 * move this slot to end of LRU chain, if not already there
11938768Smckusick 		 */
12038768Smckusick 		if (ncp->nc_nxt) {
12138768Smckusick 			/* remove from LRU chain */
12238768Smckusick 			*ncp->nc_prev = ncp->nc_nxt;
12338768Smckusick 			ncp->nc_nxt->nc_prev = ncp->nc_prev;
12438768Smckusick 			/* and replace at end of it */
12538768Smckusick 			ncp->nc_nxt = NULL;
12638768Smckusick 			ncp->nc_prev = nchtail;
12738768Smckusick 			*nchtail = ncp;
12838768Smckusick 			nchtail = &ncp->nc_nxt;
12938768Smckusick 		}
13038768Smckusick 		ndp->ni_vp = ncp->nc_vp;
13138768Smckusick 		return (-1);
13237487Smckusick 	}
13337487Smckusick 
13437487Smckusick 	/*
13537487Smckusick 	 * Last component and we are renaming or deleting,
13637487Smckusick 	 * the cache entry is invalid, or otherwise don't
13737487Smckusick 	 * want cache entry to exist.
13837487Smckusick 	 */
13937487Smckusick 	/* remove from LRU chain */
14037487Smckusick 	*ncp->nc_prev = ncp->nc_nxt;
14137487Smckusick 	if (ncp->nc_nxt)
14237487Smckusick 		ncp->nc_nxt->nc_prev = ncp->nc_prev;
14337487Smckusick 	else
14437487Smckusick 		nchtail = ncp->nc_prev;
14538768Smckusick 	/* remove from hash chain */
14638768Smckusick 	remque(ncp);
14737487Smckusick 	/* insert at head of LRU list (first to grab) */
14837487Smckusick 	ncp->nc_nxt = nchhead;
14937487Smckusick 	ncp->nc_prev = &nchhead;
15037487Smckusick 	nchhead->nc_prev = &ncp->nc_nxt;
15137487Smckusick 	nchhead = ncp;
15237487Smckusick 	/* and make a dummy hash chain */
15337487Smckusick 	ncp->nc_forw = ncp;
15437487Smckusick 	ncp->nc_back = ncp;
15537487Smckusick 	return (0);
15637487Smckusick }
15737487Smckusick 
15837487Smckusick /*
15937487Smckusick  * Add an entry to the cache
16037487Smckusick  */
16137487Smckusick cache_enter(ndp)
16237487Smckusick 	register struct nameidata *ndp;
16337487Smckusick {
16437487Smckusick 	register struct namecache *ncp;
16537487Smckusick 	union nchash *nhp;
16637487Smckusick 
16738768Smckusick 	if (!doingcache)
16838768Smckusick 		return;
16937487Smckusick 	/*
17037487Smckusick 	 * Free the cache slot at head of lru chain.
17137487Smckusick 	 */
17240881Smckusick 	if (numcache < desiredvnodes) {
17340881Smckusick 		ncp = (struct namecache *)
17440881Smckusick 			malloc(sizeof *ncp, M_CACHE, M_WAITOK);
17540881Smckusick 		bzero((char *)ncp, sizeof *ncp);
17640881Smckusick 		numcache++;
17740881Smckusick 	} else if (ncp = nchhead) {
17837487Smckusick 		/* remove from lru chain */
17937487Smckusick 		*ncp->nc_prev = ncp->nc_nxt;
18037487Smckusick 		if (ncp->nc_nxt)
18137487Smckusick 			ncp->nc_nxt->nc_prev = ncp->nc_prev;
18237487Smckusick 		else
18337487Smckusick 			nchtail = ncp->nc_prev;
18438768Smckusick 		/* remove from old hash chain */
18538768Smckusick 		remque(ncp);
18640881Smckusick 	} else
18740881Smckusick 		return;
18840881Smckusick 	/* grab the vnode we just found */
18940881Smckusick 	ncp->nc_vp = ndp->ni_vp;
19040881Smckusick 	if (ndp->ni_vp)
19140881Smckusick 		ncp->nc_vpid = ndp->ni_vp->v_id;
19240881Smckusick 	else
19340881Smckusick 		ncp->nc_vpid = 0;
19440881Smckusick 	/* fill in cache info */
19540881Smckusick 	ncp->nc_dvp = ndp->ni_dvp;
19640881Smckusick 	ncp->nc_dvpid = ndp->ni_dvp->v_id;
19740881Smckusick 	ncp->nc_nlen = ndp->ni_namelen;
19840881Smckusick 	bcopy(ndp->ni_ptr, ncp->nc_name, (unsigned)ncp->nc_nlen);
19940881Smckusick 	/* link at end of lru chain */
20040881Smckusick 	ncp->nc_nxt = NULL;
20140881Smckusick 	ncp->nc_prev = nchtail;
20240881Smckusick 	*nchtail = ncp;
20340881Smckusick 	nchtail = &ncp->nc_nxt;
20440881Smckusick 	/* and insert on hash chain */
20540881Smckusick 	nhp = &nchashtbl[ndp->ni_hash & nchash];
20640881Smckusick 	insque(ncp, nhp);
20737487Smckusick }
20837487Smckusick 
20937487Smckusick /*
21040881Smckusick  * Name cache initialization, from vfs_init() when we are booting
21137487Smckusick  */
21237487Smckusick nchinit()
21337487Smckusick {
21437487Smckusick 	register union nchash *nchp;
21540881Smckusick 	long nchashsize;
21637487Smckusick 
21737487Smckusick 	nchhead = 0;
21837487Smckusick 	nchtail = &nchhead;
21940881Smckusick 	nchashsize = roundup((desiredvnodes + 1) * sizeof *nchp / 2,
22040881Smckusick 		NBPG * CLSIZE);
22140881Smckusick 	nchashtbl = (union nchash *)malloc(nchashsize, M_CACHE, M_WAITOK);
22240881Smckusick 	for (nchash = 1; nchash <= nchashsize / sizeof *nchp; nchash <<= 1)
22340881Smckusick 		/* void */;
22440881Smckusick 	nchash = (nchash >> 1) - 1;
22540881Smckusick 	for (nchp = &nchashtbl[nchash]; nchp >= nchashtbl; nchp--) {
22637487Smckusick 		nchp->nch_head[0] = nchp;
22737487Smckusick 		nchp->nch_head[1] = nchp;
22837487Smckusick 	}
22937487Smckusick }
23037487Smckusick 
23137487Smckusick /*
23237487Smckusick  * Cache flush, a particular vnode; called when a vnode is renamed to
23338768Smckusick  * hide entries that would now be invalid
23437487Smckusick  */
23537487Smckusick cache_purge(vp)
23638768Smckusick 	struct vnode *vp;
23737487Smckusick {
23840881Smckusick 	union nchash *nhp;
23938768Smckusick 	struct namecache *ncp;
24037487Smckusick 
24138768Smckusick 	vp->v_id = ++nextvnodeid;
24238768Smckusick 	if (nextvnodeid != 0)
24338768Smckusick 		return;
24440881Smckusick 	for (nhp = &nchashtbl[nchash]; nhp >= nchashtbl; nhp--) {
24540881Smckusick 		for (ncp = nhp->nch_forw; ncp != (struct namecache *)nhp;
24640881Smckusick 		    ncp = ncp->nc_forw) {
24740881Smckusick 			ncp->nc_vpid = 0;
24840881Smckusick 			ncp->nc_dvpid = 0;
24940881Smckusick 		}
25037487Smckusick 	}
25138768Smckusick 	vp->v_id = ++nextvnodeid;
25237487Smckusick }
25337487Smckusick 
25437487Smckusick /*
25537487Smckusick  * Cache flush, a whole filesystem; called when filesys is umounted to
25637487Smckusick  * remove entries that would now be invalid
25737487Smckusick  *
25837487Smckusick  * The line "nxtcp = nchhead" near the end is to avoid potential problems
25937487Smckusick  * if the cache lru chain is modified while we are dumping the
26037487Smckusick  * inode.  This makes the algorithm O(n^2), but do you think I care?
26137487Smckusick  */
26237487Smckusick cache_purgevfs(mp)
26337487Smckusick 	register struct mount *mp;
26437487Smckusick {
26537487Smckusick 	register struct namecache *ncp, *nxtcp;
26637487Smckusick 
26737487Smckusick 	for (ncp = nchhead; ncp; ncp = nxtcp) {
26837487Smckusick 		nxtcp = ncp->nc_nxt;
26938768Smckusick 		if (ncp->nc_dvp == NULL || ncp->nc_dvp->v_mount != mp)
27037487Smckusick 			continue;
27137487Smckusick 		/* free the resources we had */
27237487Smckusick 		ncp->nc_vp = NULL;
27338768Smckusick 		ncp->nc_dvp = NULL;
27437487Smckusick 		remque(ncp);		/* remove entry from its hash chain */
27537487Smckusick 		ncp->nc_forw = ncp;	/* and make a dummy one */
27637487Smckusick 		ncp->nc_back = ncp;
27737487Smckusick 		/* delete this entry from LRU chain */
27837487Smckusick 		*ncp->nc_prev = nxtcp;
27937487Smckusick 		if (nxtcp)
28037487Smckusick 			nxtcp->nc_prev = ncp->nc_prev;
28137487Smckusick 		else
28237487Smckusick 			nchtail = ncp->nc_prev;
28337487Smckusick 		/* cause rescan of list, it may have altered */
28437487Smckusick 		nxtcp = nchhead;
28537487Smckusick 		/* put the now-free entry at head of LRU */
28637487Smckusick 		ncp->nc_nxt = nxtcp;
28737487Smckusick 		ncp->nc_prev = &nchhead;
28837487Smckusick 		nxtcp->nc_prev = &ncp->nc_nxt;
28937487Smckusick 		nchhead = ncp;
29037487Smckusick 	}
29137487Smckusick }
292