xref: /original-bsd/sys/kern/vfs_cache.c (revision 6ab384a1)
1 /*
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)vfs_cache.c	7.7 (Berkeley) 08/24/90
8  */
9 
10 #include "param.h"
11 #include "systm.h"
12 #include "time.h"
13 #include "mount.h"
14 #include "vnode.h"
15 #include "namei.h"
16 #include "errno.h"
17 #include "malloc.h"
18 
19 /*
20  * Name caching works as follows:
21  *
22  * Names found by directory scans are retained in a cache
23  * for future reference.  It is managed LRU, so frequently
24  * used names will hang around.  Cache is indexed by hash value
25  * obtained from (vp, name) where vp refers to the directory
26  * containing name.
27  *
28  * For simplicity (and economy of storage), names longer than
29  * a maximum length of NCHNAMLEN are not cached; they occur
30  * infrequently in any case, and are almost never of interest.
31  *
32  * Upon reaching the last segment of a path, if the reference
33  * is for DELETE, or NOCACHE is set (rewrite), and the
34  * name is located in the cache, it will be dropped.
35  */
36 
37 /*
38  * Structures associated with name cacheing.
39  */
40 union nchash {
41 	union	nchash *nch_head[2];
42 	struct	namecache *nch_chain[2];
43 } *nchashtbl;
44 #define	nch_forw	nch_chain[0]
45 #define	nch_back	nch_chain[1]
46 
47 u_long	nchash;				/* size of hash table - 1 */
48 long	numcache;			/* number of cache entries allocated */
49 struct	namecache *nchhead, **nchtail;	/* LRU chain pointers */
50 struct	nchstats nchstats;		/* cache effectiveness statistics */
51 
52 int doingcache = 1;			/* 1 => enable the cache */
53 
54 /*
55  * Look for a the name in the cache. We don't do this
56  * if the segment name is long, simply so the cache can avoid
57  * holding long names (which would either waste space, or
58  * add greatly to the complexity).
59  *
60  * Lookup is called with ni_dvp pointing to the directory to search,
61  * ni_ptr pointing to the name of the entry being sought, ni_namelen
62  * tells the length of the name, and ni_hash contains a hash of
63  * the name. If the lookup succeeds, the vnode is returned in ni_vp
64  * and a status of -1 is returned. If the lookup determines that
65  * the name does not exist (negative cacheing), a status of ENOENT
66  * is returned. If the lookup fails, a status of zero is returned.
67  */
68 cache_lookup(ndp)
69 	register struct nameidata *ndp;
70 {
71 	register struct vnode *dvp;
72 	register struct namecache *ncp;
73 	union nchash *nhp;
74 
75 	if (!doingcache)
76 		return (0);
77 	if (ndp->ni_namelen > NCHNAMLEN) {
78 		nchstats.ncs_long++;
79 		ndp->ni_makeentry = 0;
80 		return (0);
81 	}
82 	dvp = ndp->ni_dvp;
83 	nhp = &nchashtbl[ndp->ni_hash & nchash];
84 	for (ncp = nhp->nch_forw; ncp != (struct namecache *)nhp;
85 	    ncp = ncp->nc_forw) {
86 		if (ncp->nc_dvp == dvp &&
87 		    ncp->nc_dvpid == dvp->v_id &&
88 		    ncp->nc_nlen == ndp->ni_namelen &&
89 		    !bcmp(ncp->nc_name, ndp->ni_ptr, (unsigned)ncp->nc_nlen))
90 			break;
91 	}
92 	if (ncp == (struct namecache *)nhp) {
93 		nchstats.ncs_miss++;
94 		return (0);
95 	}
96 	if (!ndp->ni_makeentry) {
97 		nchstats.ncs_badhits++;
98 	} else if (ncp->nc_vp == NULL) {
99 		nchstats.ncs_neghits++;
100 		/*
101 		 * move this slot to end of LRU chain, if not already there
102 		 */
103 		if (ncp->nc_nxt) {
104 			/* remove from LRU chain */
105 			*ncp->nc_prev = ncp->nc_nxt;
106 			ncp->nc_nxt->nc_prev = ncp->nc_prev;
107 			/* and replace at end of it */
108 			ncp->nc_nxt = NULL;
109 			ncp->nc_prev = nchtail;
110 			*nchtail = ncp;
111 			nchtail = &ncp->nc_nxt;
112 		}
113 		return (ENOENT);
114 	} else if (ncp->nc_vpid != ncp->nc_vp->v_id) {
115 		nchstats.ncs_falsehits++;
116 	} else {
117 		nchstats.ncs_goodhits++;
118 		/*
119 		 * move this slot to end of LRU chain, if not already there
120 		 */
121 		if (ncp->nc_nxt) {
122 			/* remove from LRU chain */
123 			*ncp->nc_prev = ncp->nc_nxt;
124 			ncp->nc_nxt->nc_prev = ncp->nc_prev;
125 			/* and replace at end of it */
126 			ncp->nc_nxt = NULL;
127 			ncp->nc_prev = nchtail;
128 			*nchtail = ncp;
129 			nchtail = &ncp->nc_nxt;
130 		}
131 		ndp->ni_vp = ncp->nc_vp;
132 		return (-1);
133 	}
134 
135 	/*
136 	 * Last component and we are renaming or deleting,
137 	 * the cache entry is invalid, or otherwise don't
138 	 * want cache entry to exist.
139 	 */
140 	/* remove from LRU chain */
141 	*ncp->nc_prev = ncp->nc_nxt;
142 	if (ncp->nc_nxt)
143 		ncp->nc_nxt->nc_prev = ncp->nc_prev;
144 	else
145 		nchtail = ncp->nc_prev;
146 	/* remove from hash chain */
147 	remque(ncp);
148 	/* insert at head of LRU list (first to grab) */
149 	ncp->nc_nxt = nchhead;
150 	ncp->nc_prev = &nchhead;
151 	nchhead->nc_prev = &ncp->nc_nxt;
152 	nchhead = ncp;
153 	/* and make a dummy hash chain */
154 	ncp->nc_forw = ncp;
155 	ncp->nc_back = ncp;
156 	return (0);
157 }
158 
159 /*
160  * Add an entry to the cache
161  */
162 cache_enter(ndp)
163 	register struct nameidata *ndp;
164 {
165 	register struct namecache *ncp;
166 	union nchash *nhp;
167 
168 	if (!doingcache)
169 		return;
170 	/*
171 	 * Free the cache slot at head of lru chain.
172 	 */
173 	if (numcache < desiredvnodes) {
174 		ncp = (struct namecache *)
175 			malloc((u_long)sizeof *ncp, M_CACHE, M_WAITOK);
176 		bzero((char *)ncp, sizeof *ncp);
177 		numcache++;
178 	} else if (ncp = nchhead) {
179 		/* remove from lru chain */
180 		*ncp->nc_prev = ncp->nc_nxt;
181 		if (ncp->nc_nxt)
182 			ncp->nc_nxt->nc_prev = ncp->nc_prev;
183 		else
184 			nchtail = ncp->nc_prev;
185 		/* remove from old hash chain */
186 		remque(ncp);
187 	} else
188 		return;
189 	/* grab the vnode we just found */
190 	ncp->nc_vp = ndp->ni_vp;
191 	if (ndp->ni_vp)
192 		ncp->nc_vpid = ndp->ni_vp->v_id;
193 	else
194 		ncp->nc_vpid = 0;
195 	/* fill in cache info */
196 	ncp->nc_dvp = ndp->ni_dvp;
197 	ncp->nc_dvpid = ndp->ni_dvp->v_id;
198 	ncp->nc_nlen = ndp->ni_namelen;
199 	bcopy(ndp->ni_ptr, ncp->nc_name, (unsigned)ncp->nc_nlen);
200 	/* link at end of lru chain */
201 	ncp->nc_nxt = NULL;
202 	ncp->nc_prev = nchtail;
203 	*nchtail = ncp;
204 	nchtail = &ncp->nc_nxt;
205 	/* and insert on hash chain */
206 	nhp = &nchashtbl[ndp->ni_hash & nchash];
207 	insque(ncp, nhp);
208 }
209 
210 /*
211  * Name cache initialization, from vfs_init() when we are booting
212  */
213 nchinit()
214 {
215 	register union nchash *nchp;
216 	long nchashsize;
217 
218 	nchhead = 0;
219 	nchtail = &nchhead;
220 	nchashsize = roundup((desiredvnodes + 1) * sizeof *nchp / 2,
221 		NBPG * CLSIZE);
222 	nchashtbl = (union nchash *)malloc((u_long)nchashsize,
223 	    M_CACHE, M_WAITOK);
224 	for (nchash = 1; nchash <= nchashsize / sizeof *nchp; nchash <<= 1)
225 		/* void */;
226 	nchash = (nchash >> 1) - 1;
227 	for (nchp = &nchashtbl[nchash]; nchp >= nchashtbl; nchp--) {
228 		nchp->nch_head[0] = nchp;
229 		nchp->nch_head[1] = nchp;
230 	}
231 }
232 
233 /*
234  * Cache flush, a particular vnode; called when a vnode is renamed to
235  * hide entries that would now be invalid
236  */
237 cache_purge(vp)
238 	struct vnode *vp;
239 {
240 	union nchash *nhp;
241 	struct namecache *ncp;
242 
243 	vp->v_id = ++nextvnodeid;
244 	if (nextvnodeid != 0)
245 		return;
246 	for (nhp = &nchashtbl[nchash]; nhp >= nchashtbl; nhp--) {
247 		for (ncp = nhp->nch_forw; ncp != (struct namecache *)nhp;
248 		    ncp = ncp->nc_forw) {
249 			ncp->nc_vpid = 0;
250 			ncp->nc_dvpid = 0;
251 		}
252 	}
253 	vp->v_id = ++nextvnodeid;
254 }
255 
256 /*
257  * Cache flush, a whole filesystem; called when filesys is umounted to
258  * remove entries that would now be invalid
259  *
260  * The line "nxtcp = nchhead" near the end is to avoid potential problems
261  * if the cache lru chain is modified while we are dumping the
262  * inode.  This makes the algorithm O(n^2), but do you think I care?
263  */
264 cache_purgevfs(mp)
265 	struct mount *mp;
266 {
267 	register struct namecache *ncp, *nxtcp;
268 
269 	for (ncp = nchhead; ncp; ncp = nxtcp) {
270 		nxtcp = ncp->nc_nxt;
271 		if (ncp->nc_dvp == NULL || ncp->nc_dvp->v_mount != mp)
272 			continue;
273 		/* free the resources we had */
274 		ncp->nc_vp = NULL;
275 		ncp->nc_dvp = NULL;
276 		remque(ncp);		/* remove entry from its hash chain */
277 		ncp->nc_forw = ncp;	/* and make a dummy one */
278 		ncp->nc_back = ncp;
279 		/* delete this entry from LRU chain */
280 		*ncp->nc_prev = nxtcp;
281 		if (nxtcp)
282 			nxtcp->nc_prev = ncp->nc_prev;
283 		else
284 			nchtail = ncp->nc_prev;
285 		/* cause rescan of list, it may have altered */
286 		nxtcp = nchhead;
287 		/* put the now-free entry at head of LRU */
288 		ncp->nc_nxt = nxtcp;
289 		ncp->nc_prev = &nchhead;
290 		nxtcp->nc_prev = &ncp->nc_nxt;
291 		nchhead = ncp;
292 	}
293 }
294