vfs_cache.c revision 1.2 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1989 The Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd *
33 1.2 cgd * from: @(#)vfs_cache.c 7.8 (Berkeley) 2/28/91
34 1.2 cgd * $Id: vfs_cache.c,v 1.2 1993/05/20 02:55:33 cgd Exp $
35 1.1 cgd */
36 1.1 cgd
37 1.1 cgd #include "param.h"
38 1.1 cgd #include "systm.h"
39 1.1 cgd #include "time.h"
40 1.1 cgd #include "mount.h"
41 1.1 cgd #include "vnode.h"
42 1.1 cgd #include "namei.h"
43 1.1 cgd #include "errno.h"
44 1.1 cgd #include "malloc.h"
45 1.1 cgd
46 1.1 cgd /*
47 1.1 cgd * Name caching works as follows:
48 1.1 cgd *
49 1.1 cgd * Names found by directory scans are retained in a cache
50 1.1 cgd * for future reference. It is managed LRU, so frequently
51 1.1 cgd * used names will hang around. Cache is indexed by hash value
52 1.1 cgd * obtained from (vp, name) where vp refers to the directory
53 1.1 cgd * containing name.
54 1.1 cgd *
55 1.1 cgd * For simplicity (and economy of storage), names longer than
56 1.1 cgd * a maximum length of NCHNAMLEN are not cached; they occur
57 1.1 cgd * infrequently in any case, and are almost never of interest.
58 1.1 cgd *
59 1.1 cgd * Upon reaching the last segment of a path, if the reference
60 1.1 cgd * is for DELETE, or NOCACHE is set (rewrite), and the
61 1.1 cgd * name is located in the cache, it will be dropped.
62 1.1 cgd */
63 1.1 cgd
64 1.1 cgd /*
65 1.1 cgd * Structures associated with name cacheing.
66 1.1 cgd */
67 1.1 cgd union nchash {
68 1.1 cgd union nchash *nch_head[2];
69 1.1 cgd struct namecache *nch_chain[2];
70 1.1 cgd } *nchashtbl;
71 1.1 cgd #define nch_forw nch_chain[0]
72 1.1 cgd #define nch_back nch_chain[1]
73 1.1 cgd
74 1.1 cgd u_long nchash; /* size of hash table - 1 */
75 1.1 cgd long numcache; /* number of cache entries allocated */
76 1.1 cgd struct namecache *nchhead, **nchtail; /* LRU chain pointers */
77 1.1 cgd struct nchstats nchstats; /* cache effectiveness statistics */
78 1.1 cgd
79 1.1 cgd int doingcache = 1; /* 1 => enable the cache */
80 1.1 cgd
81 1.1 cgd /*
82 1.1 cgd * Look for a the name in the cache. We don't do this
83 1.1 cgd * if the segment name is long, simply so the cache can avoid
84 1.1 cgd * holding long names (which would either waste space, or
85 1.1 cgd * add greatly to the complexity).
86 1.1 cgd *
87 1.1 cgd * Lookup is called with ni_dvp pointing to the directory to search,
88 1.1 cgd * ni_ptr pointing to the name of the entry being sought, ni_namelen
89 1.1 cgd * tells the length of the name, and ni_hash contains a hash of
90 1.1 cgd * the name. If the lookup succeeds, the vnode is returned in ni_vp
91 1.1 cgd * and a status of -1 is returned. If the lookup determines that
92 1.1 cgd * the name does not exist (negative cacheing), a status of ENOENT
93 1.1 cgd * is returned. If the lookup fails, a status of zero is returned.
94 1.1 cgd */
95 1.1 cgd cache_lookup(ndp)
96 1.1 cgd register struct nameidata *ndp;
97 1.1 cgd {
98 1.1 cgd register struct vnode *dvp;
99 1.1 cgd register struct namecache *ncp;
100 1.1 cgd union nchash *nhp;
101 1.1 cgd
102 1.1 cgd if (!doingcache)
103 1.1 cgd return (0);
104 1.1 cgd if (ndp->ni_namelen > NCHNAMLEN) {
105 1.1 cgd nchstats.ncs_long++;
106 1.1 cgd ndp->ni_makeentry = 0;
107 1.1 cgd return (0);
108 1.1 cgd }
109 1.1 cgd dvp = ndp->ni_dvp;
110 1.1 cgd nhp = &nchashtbl[ndp->ni_hash & nchash];
111 1.1 cgd for (ncp = nhp->nch_forw; ncp != (struct namecache *)nhp;
112 1.1 cgd ncp = ncp->nc_forw) {
113 1.1 cgd if (ncp->nc_dvp == dvp &&
114 1.1 cgd ncp->nc_dvpid == dvp->v_id &&
115 1.1 cgd ncp->nc_nlen == ndp->ni_namelen &&
116 1.1 cgd !bcmp(ncp->nc_name, ndp->ni_ptr, (unsigned)ncp->nc_nlen))
117 1.1 cgd break;
118 1.1 cgd }
119 1.1 cgd if (ncp == (struct namecache *)nhp) {
120 1.1 cgd nchstats.ncs_miss++;
121 1.1 cgd return (0);
122 1.1 cgd }
123 1.1 cgd if (!ndp->ni_makeentry) {
124 1.1 cgd nchstats.ncs_badhits++;
125 1.1 cgd } else if (ncp->nc_vp == NULL) {
126 1.1 cgd if ((ndp->ni_nameiop & OPMASK) != CREATE) {
127 1.1 cgd nchstats.ncs_neghits++;
128 1.1 cgd /*
129 1.1 cgd * Move this slot to end of LRU chain,
130 1.1 cgd * if not already there.
131 1.1 cgd */
132 1.1 cgd if (ncp->nc_nxt) {
133 1.1 cgd /* remove from LRU chain */
134 1.1 cgd *ncp->nc_prev = ncp->nc_nxt;
135 1.1 cgd ncp->nc_nxt->nc_prev = ncp->nc_prev;
136 1.1 cgd /* and replace at end of it */
137 1.1 cgd ncp->nc_nxt = NULL;
138 1.1 cgd ncp->nc_prev = nchtail;
139 1.1 cgd *nchtail = ncp;
140 1.1 cgd nchtail = &ncp->nc_nxt;
141 1.1 cgd }
142 1.1 cgd return (ENOENT);
143 1.1 cgd }
144 1.1 cgd } else if (ncp->nc_vpid != ncp->nc_vp->v_id) {
145 1.1 cgd nchstats.ncs_falsehits++;
146 1.1 cgd } else {
147 1.1 cgd nchstats.ncs_goodhits++;
148 1.1 cgd /*
149 1.1 cgd * move this slot to end of LRU chain, if not already there
150 1.1 cgd */
151 1.1 cgd if (ncp->nc_nxt) {
152 1.1 cgd /* remove from LRU chain */
153 1.1 cgd *ncp->nc_prev = ncp->nc_nxt;
154 1.1 cgd ncp->nc_nxt->nc_prev = ncp->nc_prev;
155 1.1 cgd /* and replace at end of it */
156 1.1 cgd ncp->nc_nxt = NULL;
157 1.1 cgd ncp->nc_prev = nchtail;
158 1.1 cgd *nchtail = ncp;
159 1.1 cgd nchtail = &ncp->nc_nxt;
160 1.1 cgd }
161 1.1 cgd ndp->ni_vp = ncp->nc_vp;
162 1.1 cgd return (-1);
163 1.1 cgd }
164 1.1 cgd
165 1.1 cgd /*
166 1.1 cgd * Last component and we are renaming or deleting,
167 1.1 cgd * the cache entry is invalid, or otherwise don't
168 1.1 cgd * want cache entry to exist.
169 1.1 cgd */
170 1.1 cgd /* remove from LRU chain */
171 1.1 cgd *ncp->nc_prev = ncp->nc_nxt;
172 1.1 cgd if (ncp->nc_nxt)
173 1.1 cgd ncp->nc_nxt->nc_prev = ncp->nc_prev;
174 1.1 cgd else
175 1.1 cgd nchtail = ncp->nc_prev;
176 1.1 cgd /* remove from hash chain */
177 1.1 cgd remque(ncp);
178 1.1 cgd /* insert at head of LRU list (first to grab) */
179 1.1 cgd ncp->nc_nxt = nchhead;
180 1.1 cgd ncp->nc_prev = &nchhead;
181 1.1 cgd nchhead->nc_prev = &ncp->nc_nxt;
182 1.1 cgd nchhead = ncp;
183 1.1 cgd /* and make a dummy hash chain */
184 1.1 cgd ncp->nc_forw = ncp;
185 1.1 cgd ncp->nc_back = ncp;
186 1.1 cgd return (0);
187 1.1 cgd }
188 1.1 cgd
189 1.1 cgd /*
190 1.1 cgd * Add an entry to the cache
191 1.1 cgd */
192 1.1 cgd cache_enter(ndp)
193 1.1 cgd register struct nameidata *ndp;
194 1.1 cgd {
195 1.1 cgd register struct namecache *ncp;
196 1.1 cgd union nchash *nhp;
197 1.1 cgd
198 1.1 cgd if (!doingcache)
199 1.1 cgd return;
200 1.1 cgd /*
201 1.1 cgd * Free the cache slot at head of lru chain.
202 1.1 cgd */
203 1.1 cgd if (numcache < desiredvnodes) {
204 1.1 cgd ncp = (struct namecache *)
205 1.1 cgd malloc((u_long)sizeof *ncp, M_CACHE, M_WAITOK);
206 1.1 cgd bzero((char *)ncp, sizeof *ncp);
207 1.1 cgd numcache++;
208 1.1 cgd } else if (ncp = nchhead) {
209 1.1 cgd /* remove from lru chain */
210 1.1 cgd *ncp->nc_prev = ncp->nc_nxt;
211 1.1 cgd if (ncp->nc_nxt)
212 1.1 cgd ncp->nc_nxt->nc_prev = ncp->nc_prev;
213 1.1 cgd else
214 1.1 cgd nchtail = ncp->nc_prev;
215 1.1 cgd /* remove from old hash chain */
216 1.1 cgd remque(ncp);
217 1.1 cgd } else
218 1.1 cgd return;
219 1.1 cgd /* grab the vnode we just found */
220 1.1 cgd ncp->nc_vp = ndp->ni_vp;
221 1.1 cgd if (ndp->ni_vp)
222 1.1 cgd ncp->nc_vpid = ndp->ni_vp->v_id;
223 1.1 cgd else
224 1.1 cgd ncp->nc_vpid = 0;
225 1.1 cgd /* fill in cache info */
226 1.1 cgd ncp->nc_dvp = ndp->ni_dvp;
227 1.1 cgd ncp->nc_dvpid = ndp->ni_dvp->v_id;
228 1.1 cgd ncp->nc_nlen = ndp->ni_namelen;
229 1.1 cgd bcopy(ndp->ni_ptr, ncp->nc_name, (unsigned)ncp->nc_nlen);
230 1.1 cgd /* link at end of lru chain */
231 1.1 cgd ncp->nc_nxt = NULL;
232 1.1 cgd ncp->nc_prev = nchtail;
233 1.1 cgd *nchtail = ncp;
234 1.1 cgd nchtail = &ncp->nc_nxt;
235 1.1 cgd /* and insert on hash chain */
236 1.1 cgd nhp = &nchashtbl[ndp->ni_hash & nchash];
237 1.1 cgd insque(ncp, nhp);
238 1.1 cgd }
239 1.1 cgd
240 1.1 cgd /*
241 1.1 cgd * Name cache initialization, from vfs_init() when we are booting
242 1.1 cgd */
243 1.1 cgd nchinit()
244 1.1 cgd {
245 1.1 cgd register union nchash *nchp;
246 1.1 cgd long nchashsize;
247 1.1 cgd
248 1.1 cgd nchhead = 0;
249 1.1 cgd nchtail = &nchhead;
250 1.1 cgd nchashsize = roundup((desiredvnodes + 1) * sizeof *nchp / 2,
251 1.1 cgd NBPG * CLSIZE);
252 1.1 cgd nchashtbl = (union nchash *)malloc((u_long)nchashsize,
253 1.1 cgd M_CACHE, M_WAITOK);
254 1.1 cgd for (nchash = 1; nchash <= nchashsize / sizeof *nchp; nchash <<= 1)
255 1.1 cgd /* void */;
256 1.1 cgd nchash = (nchash >> 1) - 1;
257 1.1 cgd for (nchp = &nchashtbl[nchash]; nchp >= nchashtbl; nchp--) {
258 1.1 cgd nchp->nch_head[0] = nchp;
259 1.1 cgd nchp->nch_head[1] = nchp;
260 1.1 cgd }
261 1.1 cgd }
262 1.1 cgd
263 1.1 cgd /*
264 1.1 cgd * Cache flush, a particular vnode; called when a vnode is renamed to
265 1.1 cgd * hide entries that would now be invalid
266 1.1 cgd */
267 1.1 cgd cache_purge(vp)
268 1.1 cgd struct vnode *vp;
269 1.1 cgd {
270 1.1 cgd union nchash *nhp;
271 1.1 cgd struct namecache *ncp;
272 1.1 cgd
273 1.1 cgd vp->v_id = ++nextvnodeid;
274 1.1 cgd if (nextvnodeid != 0)
275 1.1 cgd return;
276 1.1 cgd for (nhp = &nchashtbl[nchash]; nhp >= nchashtbl; nhp--) {
277 1.1 cgd for (ncp = nhp->nch_forw; ncp != (struct namecache *)nhp;
278 1.1 cgd ncp = ncp->nc_forw) {
279 1.1 cgd ncp->nc_vpid = 0;
280 1.1 cgd ncp->nc_dvpid = 0;
281 1.1 cgd }
282 1.1 cgd }
283 1.1 cgd vp->v_id = ++nextvnodeid;
284 1.1 cgd }
285 1.1 cgd
286 1.1 cgd /*
287 1.1 cgd * Cache flush, a whole filesystem; called when filesys is umounted to
288 1.1 cgd * remove entries that would now be invalid
289 1.1 cgd *
290 1.1 cgd * The line "nxtcp = nchhead" near the end is to avoid potential problems
291 1.1 cgd * if the cache lru chain is modified while we are dumping the
292 1.1 cgd * inode. This makes the algorithm O(n^2), but do you think I care?
293 1.1 cgd */
294 1.1 cgd cache_purgevfs(mp)
295 1.1 cgd struct mount *mp;
296 1.1 cgd {
297 1.1 cgd register struct namecache *ncp, *nxtcp;
298 1.1 cgd
299 1.1 cgd for (ncp = nchhead; ncp; ncp = nxtcp) {
300 1.1 cgd nxtcp = ncp->nc_nxt;
301 1.1 cgd if (ncp->nc_dvp == NULL || ncp->nc_dvp->v_mount != mp)
302 1.1 cgd continue;
303 1.1 cgd /* free the resources we had */
304 1.1 cgd ncp->nc_vp = NULL;
305 1.1 cgd ncp->nc_dvp = NULL;
306 1.1 cgd remque(ncp); /* remove entry from its hash chain */
307 1.1 cgd ncp->nc_forw = ncp; /* and make a dummy one */
308 1.1 cgd ncp->nc_back = ncp;
309 1.1 cgd /* delete this entry from LRU chain */
310 1.1 cgd *ncp->nc_prev = nxtcp;
311 1.1 cgd if (nxtcp)
312 1.1 cgd nxtcp->nc_prev = ncp->nc_prev;
313 1.1 cgd else
314 1.1 cgd nchtail = ncp->nc_prev;
315 1.1 cgd /* cause rescan of list, it may have altered */
316 1.1 cgd nxtcp = nchhead;
317 1.1 cgd /* put the now-free entry at head of LRU */
318 1.1 cgd ncp->nc_nxt = nxtcp;
319 1.1 cgd ncp->nc_prev = &nchhead;
320 1.1 cgd nxtcp->nc_prev = &ncp->nc_nxt;
321 1.1 cgd nchhead = ncp;
322 1.1 cgd }
323 1.1 cgd }
324