vfs_cache.c revision 1.49 1 1.49 yamt /* $NetBSD: vfs_cache.c,v 1.49 2003/07/31 15:14:08 yamt Exp $ */
2 1.6 cgd
3 1.1 cgd /*
4 1.5 mycroft * Copyright (c) 1989, 1993
5 1.5 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd *
35 1.10 mycroft * @(#)vfs_cache.c 8.3 (Berkeley) 8/22/94
36 1.1 cgd */
37 1.32 lukem
38 1.32 lukem #include <sys/cdefs.h>
39 1.49 yamt __KERNEL_RCSID(0, "$NetBSD: vfs_cache.c,v 1.49 2003/07/31 15:14:08 yamt Exp $");
40 1.1 cgd
41 1.28 chs #include "opt_ddb.h"
42 1.29 fvdl #include "opt_revcache.h"
43 1.28 chs
44 1.4 mycroft #include <sys/param.h>
45 1.4 mycroft #include <sys/systm.h>
46 1.4 mycroft #include <sys/time.h>
47 1.4 mycroft #include <sys/mount.h>
48 1.4 mycroft #include <sys/vnode.h>
49 1.4 mycroft #include <sys/namei.h>
50 1.4 mycroft #include <sys/errno.h>
51 1.4 mycroft #include <sys/malloc.h>
52 1.18 thorpej #include <sys/pool.h>
53 1.39 pk #include <sys/lock.h>
54 1.1 cgd
55 1.1 cgd /*
56 1.1 cgd * Name caching works as follows:
57 1.1 cgd *
58 1.1 cgd * Names found by directory scans are retained in a cache
59 1.1 cgd * for future reference. It is managed LRU, so frequently
60 1.1 cgd * used names will hang around. Cache is indexed by hash value
61 1.20 jdolecek * obtained from (dvp, name) where dvp refers to the directory
62 1.1 cgd * containing name.
63 1.1 cgd *
64 1.1 cgd * For simplicity (and economy of storage), names longer than
65 1.1 cgd * a maximum length of NCHNAMLEN are not cached; they occur
66 1.1 cgd * infrequently in any case, and are almost never of interest.
67 1.1 cgd *
68 1.1 cgd * Upon reaching the last segment of a path, if the reference
69 1.1 cgd * is for DELETE, or NOCACHE is set (rewrite), and the
70 1.1 cgd * name is located in the cache, it will be dropped.
71 1.20 jdolecek * The entry is dropped also when it was not possible to lock
72 1.20 jdolecek * the cached vnode, either because vget() failed or the generation
73 1.20 jdolecek * number has changed while waiting for the lock.
74 1.1 cgd */
75 1.1 cgd
76 1.1 cgd /*
77 1.1 cgd * Structures associated with name cacheing.
78 1.1 cgd */
79 1.9 mycroft LIST_HEAD(nchashhead, namecache) *nchashtbl;
80 1.1 cgd u_long nchash; /* size of hash table - 1 */
81 1.1 cgd long numcache; /* number of cache entries allocated */
82 1.49 yamt #define NCHASH(cnp, dvp) \
83 1.49 yamt (((cnp)->cn_hash ^ ((uintptr_t)(dvp) >> 3)) & nchash)
84 1.19 sommerfe
85 1.19 sommerfe LIST_HEAD(ncvhashhead, namecache) *ncvhashtbl;
86 1.19 sommerfe u_long ncvhash; /* size of hash table - 1 */
87 1.48 yamt #define NCVHASH(vp) (((uintptr_t)(vp) >> 3) & ncvhash)
88 1.19 sommerfe
89 1.9 mycroft TAILQ_HEAD(, namecache) nclruhead; /* LRU chain */
90 1.1 cgd struct nchstats nchstats; /* cache effectiveness statistics */
91 1.1 cgd
92 1.18 thorpej struct pool namecache_pool;
93 1.38 thorpej
94 1.38 thorpej MALLOC_DEFINE(M_CACHE, "namecache", "Dynamically allocated cache entries");
95 1.18 thorpej
96 1.7 chopps int doingcache = 1; /* 1 => enable the cache */
97 1.1 cgd
98 1.39 pk /* A single lock to protect cache insertion, removal and lookup */
99 1.39 pk static struct simplelock namecache_slock = SIMPLELOCK_INITIALIZER;
100 1.39 pk
101 1.46 yamt static void cache_remove(struct namecache *);
102 1.46 yamt static void cache_free(struct namecache *);
103 1.46 yamt
104 1.46 yamt static void
105 1.46 yamt cache_remove(struct namecache *ncp)
106 1.46 yamt {
107 1.46 yamt
108 1.46 yamt LOCK_ASSERT(simple_lock_held(&namecache_slock));
109 1.46 yamt
110 1.46 yamt ncp->nc_dvp = NULL;
111 1.46 yamt ncp->nc_vp = NULL;
112 1.46 yamt
113 1.46 yamt TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
114 1.46 yamt if (ncp->nc_hash.le_prev != NULL) {
115 1.46 yamt LIST_REMOVE(ncp, nc_hash);
116 1.46 yamt ncp->nc_hash.le_prev = NULL;
117 1.46 yamt }
118 1.46 yamt if (ncp->nc_vhash.le_prev != NULL) {
119 1.46 yamt LIST_REMOVE(ncp, nc_vhash);
120 1.46 yamt ncp->nc_vhash.le_prev = NULL;
121 1.46 yamt }
122 1.46 yamt if (ncp->nc_vlist.le_prev != NULL) {
123 1.46 yamt LIST_REMOVE(ncp, nc_vlist);
124 1.46 yamt ncp->nc_vlist.le_prev = NULL;
125 1.46 yamt }
126 1.46 yamt if (ncp->nc_dvlist.le_prev != NULL) {
127 1.46 yamt LIST_REMOVE(ncp, nc_dvlist);
128 1.46 yamt ncp->nc_dvlist.le_prev = NULL;
129 1.46 yamt }
130 1.46 yamt }
131 1.46 yamt
132 1.46 yamt static void
133 1.46 yamt cache_free(struct namecache *ncp)
134 1.46 yamt {
135 1.46 yamt
136 1.46 yamt pool_put(&namecache_pool, ncp);
137 1.46 yamt numcache--; /* XXX MP */
138 1.46 yamt }
139 1.46 yamt
140 1.1 cgd /*
141 1.1 cgd * Look for a the name in the cache. We don't do this
142 1.1 cgd * if the segment name is long, simply so the cache can avoid
143 1.1 cgd * holding long names (which would either waste space, or
144 1.1 cgd * add greatly to the complexity).
145 1.1 cgd *
146 1.1 cgd * Lookup is called with ni_dvp pointing to the directory to search,
147 1.1 cgd * ni_ptr pointing to the name of the entry being sought, ni_namelen
148 1.1 cgd * tells the length of the name, and ni_hash contains a hash of
149 1.20 jdolecek * the name. If the lookup succeeds, the vnode is locked, stored in ni_vp
150 1.20 jdolecek * and a status of zero is returned. If the locking fails for whatever
151 1.20 jdolecek * reason, the vnode is unlocked and the error is returned to caller.
152 1.20 jdolecek * If the lookup determines that the name does not exist (negative cacheing),
153 1.20 jdolecek * a status of ENOENT is returned. If the lookup fails, a status of -1
154 1.20 jdolecek * is returned.
155 1.1 cgd */
156 1.5 mycroft int
157 1.34 enami cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
158 1.1 cgd {
159 1.23 augustss struct namecache *ncp;
160 1.23 augustss struct nchashhead *ncpp;
161 1.20 jdolecek struct vnode *vp;
162 1.36 thorpej int error;
163 1.1 cgd
164 1.8 cgd if (!doingcache) {
165 1.8 cgd cnp->cn_flags &= ~MAKEENTRY;
166 1.34 enami *vpp = NULL;
167 1.20 jdolecek return (-1);
168 1.8 cgd }
169 1.39 pk
170 1.39 pk simple_lock(&namecache_slock);
171 1.5 mycroft if (cnp->cn_namelen > NCHNAMLEN) {
172 1.1 cgd nchstats.ncs_long++;
173 1.5 mycroft cnp->cn_flags &= ~MAKEENTRY;
174 1.39 pk goto fail_wlock;
175 1.1 cgd }
176 1.30 chs ncpp = &nchashtbl[NCHASH(cnp, dvp)];
177 1.27 chs LIST_FOREACH(ncp, ncpp, nc_hash) {
178 1.1 cgd if (ncp->nc_dvp == dvp &&
179 1.5 mycroft ncp->nc_nlen == cnp->cn_namelen &&
180 1.17 perry !memcmp(ncp->nc_name, cnp->cn_nameptr, (u_int)ncp->nc_nlen))
181 1.1 cgd break;
182 1.1 cgd }
183 1.9 mycroft if (ncp == 0) {
184 1.1 cgd nchstats.ncs_miss++;
185 1.39 pk goto fail_wlock;
186 1.1 cgd }
187 1.9 mycroft if ((cnp->cn_flags & MAKEENTRY) == 0) {
188 1.1 cgd nchstats.ncs_badhits++;
189 1.20 jdolecek goto remove;
190 1.1 cgd } else if (ncp->nc_vp == NULL) {
191 1.11 mycroft /*
192 1.11 mycroft * Restore the ISWHITEOUT flag saved earlier.
193 1.11 mycroft */
194 1.11 mycroft cnp->cn_flags |= ncp->nc_vpid;
195 1.21 mycroft if (cnp->cn_nameiop != CREATE ||
196 1.21 mycroft (cnp->cn_flags & ISLASTCN) == 0) {
197 1.1 cgd nchstats.ncs_neghits++;
198 1.1 cgd /*
199 1.1 cgd * Move this slot to end of LRU chain,
200 1.1 cgd * if not already there.
201 1.1 cgd */
202 1.37 matt if (TAILQ_NEXT(ncp, nc_lru) != 0) {
203 1.9 mycroft TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
204 1.9 mycroft TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
205 1.1 cgd }
206 1.39 pk simple_unlock(&namecache_slock);
207 1.1 cgd return (ENOENT);
208 1.20 jdolecek } else {
209 1.15 fvdl nchstats.ncs_badhits++;
210 1.20 jdolecek goto remove;
211 1.20 jdolecek }
212 1.20 jdolecek }
213 1.20 jdolecek
214 1.20 jdolecek vp = ncp->nc_vp;
215 1.39 pk /* Release the name cache mutex while we acquire vnode locks */
216 1.39 pk simple_unlock(&namecache_slock);
217 1.39 pk
218 1.20 jdolecek if (vp == dvp) { /* lookup on "." */
219 1.20 jdolecek VREF(dvp);
220 1.20 jdolecek error = 0;
221 1.20 jdolecek } else if (cnp->cn_flags & ISDOTDOT) {
222 1.20 jdolecek VOP_UNLOCK(dvp, 0);
223 1.20 jdolecek cnp->cn_flags |= PDIRUNLOCK;
224 1.44 thorpej error = vget(vp, LK_EXCLUSIVE);
225 1.34 enami /*
226 1.34 enami * If the above vget() succeeded and both LOCKPARENT and
227 1.34 enami * ISLASTCN is set, lock the directory vnode as well.
228 1.34 enami */
229 1.20 jdolecek if (!error && (~cnp->cn_flags & (LOCKPARENT|ISLASTCN)) == 0) {
230 1.20 jdolecek if ((error = vn_lock(dvp, LK_EXCLUSIVE)) != 0) {
231 1.20 jdolecek vput(vp);
232 1.20 jdolecek return (error);
233 1.20 jdolecek }
234 1.20 jdolecek cnp->cn_flags &= ~PDIRUNLOCK;
235 1.20 jdolecek }
236 1.1 cgd } else {
237 1.44 thorpej error = vget(vp, LK_EXCLUSIVE);
238 1.34 enami /*
239 1.34 enami * If the above vget() failed or either of LOCKPARENT or
240 1.34 enami * ISLASTCN is set, unlock the directory vnode.
241 1.34 enami */
242 1.20 jdolecek if (error || (~cnp->cn_flags & (LOCKPARENT|ISLASTCN)) != 0) {
243 1.20 jdolecek VOP_UNLOCK(dvp, 0);
244 1.20 jdolecek cnp->cn_flags |= PDIRUNLOCK;
245 1.20 jdolecek }
246 1.20 jdolecek }
247 1.20 jdolecek
248 1.20 jdolecek /*
249 1.20 jdolecek * Check that the lock succeeded, and that the capability number did
250 1.20 jdolecek * not change while we were waiting for the lock.
251 1.20 jdolecek */
252 1.47 yamt if (error) {
253 1.39 pk /* XXXSMP - updating stats without lock; do we care? */
254 1.20 jdolecek if (!error) {
255 1.20 jdolecek vput(vp);
256 1.20 jdolecek nchstats.ncs_falsehits++;
257 1.20 jdolecek } else
258 1.20 jdolecek nchstats.ncs_badhits++;
259 1.39 pk
260 1.1 cgd /*
261 1.20 jdolecek * The parent needs to be locked when we return to VOP_LOOKUP().
262 1.20 jdolecek * The `.' case here should be extremely rare (if it can happen
263 1.20 jdolecek * at all), so we don't bother optimizing out the unlock/relock.
264 1.1 cgd */
265 1.20 jdolecek if (vp == dvp ||
266 1.20 jdolecek error || (~cnp->cn_flags & (LOCKPARENT|ISLASTCN)) != 0) {
267 1.20 jdolecek if ((error = vn_lock(dvp, LK_EXCLUSIVE)) != 0)
268 1.20 jdolecek return (error);
269 1.20 jdolecek cnp->cn_flags &= ~PDIRUNLOCK;
270 1.1 cgd }
271 1.31 chs *vpp = NULL;
272 1.31 chs return (-1);
273 1.20 jdolecek }
274 1.20 jdolecek
275 1.39 pk simple_lock(&namecache_slock);
276 1.20 jdolecek nchstats.ncs_goodhits++;
277 1.20 jdolecek /*
278 1.34 enami * Move this slot to end of LRU chain, if not already there.
279 1.20 jdolecek */
280 1.37 matt if (TAILQ_NEXT(ncp, nc_lru) != 0) {
281 1.20 jdolecek TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
282 1.20 jdolecek TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
283 1.1 cgd }
284 1.39 pk
285 1.39 pk simple_unlock(&namecache_slock);
286 1.20 jdolecek *vpp = vp;
287 1.20 jdolecek return (0);
288 1.1 cgd
289 1.20 jdolecek remove:
290 1.1 cgd /*
291 1.1 cgd * Last component and we are renaming or deleting,
292 1.1 cgd * the cache entry is invalid, or otherwise don't
293 1.1 cgd * want cache entry to exist.
294 1.1 cgd */
295 1.9 mycroft TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
296 1.9 mycroft LIST_REMOVE(ncp, nc_hash);
297 1.34 enami ncp->nc_hash.le_prev = NULL;
298 1.19 sommerfe if (ncp->nc_vhash.le_prev != NULL) {
299 1.19 sommerfe LIST_REMOVE(ncp, nc_vhash);
300 1.34 enami ncp->nc_vhash.le_prev = NULL;
301 1.19 sommerfe }
302 1.9 mycroft TAILQ_INSERT_HEAD(&nclruhead, ncp, nc_lru);
303 1.39 pk
304 1.39 pk fail_wlock:
305 1.39 pk simple_unlock(&namecache_slock);
306 1.34 enami *vpp = NULL;
307 1.20 jdolecek return (-1);
308 1.1 cgd }
309 1.1 cgd
310 1.1 cgd /*
311 1.19 sommerfe * Scan cache looking for name of directory entry pointing at vp.
312 1.19 sommerfe *
313 1.19 sommerfe * Fill in dvpp.
314 1.19 sommerfe *
315 1.19 sommerfe * If bufp is non-NULL, also place the name in the buffer which starts
316 1.19 sommerfe * at bufp, immediately before *bpp, and move bpp backwards to point
317 1.19 sommerfe * at the start of it. (Yes, this is a little baroque, but it's done
318 1.19 sommerfe * this way to cater to the whims of getcwd).
319 1.19 sommerfe *
320 1.19 sommerfe * Returns 0 on success, -1 on cache miss, positive errno on failure.
321 1.19 sommerfe */
322 1.19 sommerfe int
323 1.34 enami cache_revlookup(struct vnode *vp, struct vnode **dvpp, char **bpp, char *bufp)
324 1.19 sommerfe {
325 1.19 sommerfe struct namecache *ncp;
326 1.19 sommerfe struct vnode *dvp;
327 1.19 sommerfe struct ncvhashhead *nvcpp;
328 1.34 enami char *bp;
329 1.34 enami
330 1.19 sommerfe if (!doingcache)
331 1.19 sommerfe goto out;
332 1.19 sommerfe
333 1.30 chs nvcpp = &ncvhashtbl[NCVHASH(vp)];
334 1.19 sommerfe
335 1.39 pk simple_lock(&namecache_slock);
336 1.27 chs LIST_FOREACH(ncp, nvcpp, nc_vhash) {
337 1.34 enami if (ncp->nc_vp == vp &&
338 1.34 enami (dvp = ncp->nc_dvp) != NULL &&
339 1.47 yamt dvp != vp) { /* avoid pesky . entries.. */
340 1.34 enami
341 1.19 sommerfe #ifdef DIAGNOSTIC
342 1.34 enami if (ncp->nc_nlen == 1 &&
343 1.34 enami ncp->nc_name[0] == '.')
344 1.19 sommerfe panic("cache_revlookup: found entry for .");
345 1.19 sommerfe
346 1.34 enami if (ncp->nc_nlen == 2 &&
347 1.34 enami ncp->nc_name[0] == '.' &&
348 1.34 enami ncp->nc_name[1] == '.')
349 1.19 sommerfe panic("cache_revlookup: found entry for ..");
350 1.19 sommerfe #endif
351 1.19 sommerfe nchstats.ncs_revhits++;
352 1.19 sommerfe
353 1.19 sommerfe if (bufp) {
354 1.19 sommerfe bp = *bpp;
355 1.19 sommerfe bp -= ncp->nc_nlen;
356 1.19 sommerfe if (bp <= bufp) {
357 1.34 enami *dvpp = NULL;
358 1.42 fvdl simple_unlock(&namecache_slock);
359 1.34 enami return (ERANGE);
360 1.19 sommerfe }
361 1.19 sommerfe memcpy(bp, ncp->nc_name, ncp->nc_nlen);
362 1.19 sommerfe *bpp = bp;
363 1.19 sommerfe }
364 1.34 enami
365 1.19 sommerfe /* XXX MP: how do we know dvp won't evaporate? */
366 1.19 sommerfe *dvpp = dvp;
367 1.39 pk simple_unlock(&namecache_slock);
368 1.34 enami return (0);
369 1.19 sommerfe }
370 1.19 sommerfe }
371 1.19 sommerfe nchstats.ncs_revmiss++;
372 1.39 pk simple_unlock(&namecache_slock);
373 1.19 sommerfe out:
374 1.34 enami *dvpp = NULL;
375 1.34 enami return (-1);
376 1.19 sommerfe }
377 1.19 sommerfe
378 1.19 sommerfe /*
379 1.1 cgd * Add an entry to the cache
380 1.1 cgd */
381 1.13 christos void
382 1.34 enami cache_enter(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
383 1.1 cgd {
384 1.23 augustss struct namecache *ncp;
385 1.23 augustss struct nchashhead *ncpp;
386 1.23 augustss struct ncvhashhead *nvcpp;
387 1.1 cgd
388 1.5 mycroft #ifdef DIAGNOSTIC
389 1.5 mycroft if (cnp->cn_namelen > NCHNAMLEN)
390 1.5 mycroft panic("cache_enter: name too long");
391 1.5 mycroft #endif
392 1.1 cgd if (!doingcache)
393 1.1 cgd return;
394 1.1 cgd /*
395 1.1 cgd * Free the cache slot at head of lru chain.
396 1.1 cgd */
397 1.39 pk simple_lock(&namecache_slock);
398 1.24 chs if (numcache < numvnodes) {
399 1.39 pk numcache++;
400 1.39 pk simple_unlock(&namecache_slock);
401 1.18 thorpej ncp = pool_get(&namecache_pool, PR_WAITOK);
402 1.27 chs memset(ncp, 0, sizeof(*ncp));
403 1.39 pk simple_lock(&namecache_slock);
404 1.27 chs } else if ((ncp = TAILQ_FIRST(&nclruhead)) != NULL) {
405 1.46 yamt cache_remove(ncp);
406 1.39 pk } else {
407 1.39 pk simple_unlock(&namecache_slock);
408 1.1 cgd return;
409 1.39 pk }
410 1.34 enami /* Grab the vnode we just found. */
411 1.5 mycroft ncp->nc_vp = vp;
412 1.47 yamt if (vp == NULL) {
413 1.11 mycroft /*
414 1.11 mycroft * For negative hits, save the ISWHITEOUT flag so we can
415 1.11 mycroft * restore it later when the cache entry is used again.
416 1.11 mycroft */
417 1.11 mycroft ncp->nc_vpid = cnp->cn_flags & ISWHITEOUT;
418 1.11 mycroft }
419 1.34 enami /* Fill in cache info. */
420 1.5 mycroft ncp->nc_dvp = dvp;
421 1.46 yamt LIST_INSERT_HEAD(&dvp->v_dnclist, ncp, nc_dvlist);
422 1.46 yamt if (vp)
423 1.46 yamt LIST_INSERT_HEAD(&vp->v_nclist, ncp, nc_vlist);
424 1.5 mycroft ncp->nc_nlen = cnp->cn_namelen;
425 1.17 perry memcpy(ncp->nc_name, cnp->cn_nameptr, (unsigned)ncp->nc_nlen);
426 1.9 mycroft TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
427 1.30 chs ncpp = &nchashtbl[NCHASH(cnp, dvp)];
428 1.9 mycroft LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
429 1.19 sommerfe
430 1.34 enami ncp->nc_vhash.le_prev = NULL;
431 1.34 enami ncp->nc_vhash.le_next = NULL;
432 1.34 enami
433 1.19 sommerfe /*
434 1.19 sommerfe * Create reverse-cache entries (used in getcwd) for directories.
435 1.19 sommerfe */
436 1.33 enami if (vp != NULL &&
437 1.33 enami vp != dvp &&
438 1.29 fvdl #ifndef NAMECACHE_ENTER_REVERSE
439 1.33 enami vp->v_type == VDIR &&
440 1.29 fvdl #endif
441 1.33 enami (ncp->nc_nlen > 2 ||
442 1.33 enami (ncp->nc_nlen > 1 && ncp->nc_name[1] != '.') ||
443 1.33 enami (/* ncp->nc_nlen > 0 && */ ncp->nc_name[0] != '.'))) {
444 1.30 chs nvcpp = &ncvhashtbl[NCVHASH(vp)];
445 1.19 sommerfe LIST_INSERT_HEAD(nvcpp, ncp, nc_vhash);
446 1.19 sommerfe }
447 1.39 pk simple_unlock(&namecache_slock);
448 1.1 cgd }
449 1.1 cgd
450 1.1 cgd /*
451 1.1 cgd * Name cache initialization, from vfs_init() when we are booting
452 1.1 cgd */
453 1.13 christos void
454 1.34 enami nchinit(void)
455 1.1 cgd {
456 1.1 cgd
457 1.9 mycroft TAILQ_INIT(&nclruhead);
458 1.26 ad nchashtbl =
459 1.26 ad hashinit(desiredvnodes, HASH_LIST, M_CACHE, M_WAITOK, &nchash);
460 1.26 ad ncvhashtbl =
461 1.29 fvdl #ifdef NAMECACHE_ENTER_REVERSE
462 1.29 fvdl hashinit(desiredvnodes, HASH_LIST, M_CACHE, M_WAITOK, &ncvhash);
463 1.29 fvdl #else
464 1.26 ad hashinit(desiredvnodes/8, HASH_LIST, M_CACHE, M_WAITOK, &ncvhash);
465 1.29 fvdl #endif
466 1.18 thorpej pool_init(&namecache_pool, sizeof(struct namecache), 0, 0, 0,
467 1.35 thorpej "ncachepl", &pool_allocator_nointr);
468 1.30 chs }
469 1.30 chs
470 1.30 chs /*
471 1.30 chs * Name cache reinitialization, for when the maximum number of vnodes increases.
472 1.30 chs */
473 1.30 chs void
474 1.34 enami nchreinit(void)
475 1.30 chs {
476 1.30 chs struct namecache *ncp;
477 1.30 chs struct nchashhead *oldhash1, *hash1;
478 1.30 chs struct ncvhashhead *oldhash2, *hash2;
479 1.36 thorpej u_long i, oldmask1, oldmask2, mask1, mask2;
480 1.30 chs
481 1.30 chs hash1 = hashinit(desiredvnodes, HASH_LIST, M_CACHE, M_WAITOK, &mask1);
482 1.30 chs hash2 =
483 1.30 chs #ifdef NAMECACHE_ENTER_REVERSE
484 1.30 chs hashinit(desiredvnodes, HASH_LIST, M_CACHE, M_WAITOK, &mask2);
485 1.30 chs #else
486 1.30 chs hashinit(desiredvnodes/8, HASH_LIST, M_CACHE, M_WAITOK, &mask2);
487 1.30 chs #endif
488 1.40 jmc simple_lock(&namecache_slock);
489 1.30 chs oldhash1 = nchashtbl;
490 1.30 chs oldmask1 = nchash;
491 1.30 chs nchashtbl = hash1;
492 1.30 chs nchash = mask1;
493 1.30 chs oldhash2 = ncvhashtbl;
494 1.30 chs oldmask2 = ncvhash;
495 1.30 chs ncvhashtbl = hash2;
496 1.30 chs ncvhash = mask2;
497 1.30 chs for (i = 0; i <= oldmask1; i++) {
498 1.30 chs while ((ncp = LIST_FIRST(&oldhash1[i])) != NULL) {
499 1.30 chs LIST_REMOVE(ncp, nc_hash);
500 1.30 chs ncp->nc_hash.le_prev = NULL;
501 1.30 chs }
502 1.30 chs }
503 1.30 chs for (i = 0; i <= oldmask2; i++) {
504 1.30 chs while ((ncp = LIST_FIRST(&oldhash2[i])) != NULL) {
505 1.30 chs LIST_REMOVE(ncp, nc_vhash);
506 1.30 chs ncp->nc_vhash.le_prev = NULL;
507 1.30 chs }
508 1.30 chs }
509 1.41 enami simple_unlock(&namecache_slock);
510 1.30 chs hashdone(oldhash1, M_CACHE);
511 1.30 chs hashdone(oldhash2, M_CACHE);
512 1.1 cgd }
513 1.1 cgd
514 1.1 cgd /*
515 1.1 cgd * Cache flush, a particular vnode; called when a vnode is renamed to
516 1.1 cgd * hide entries that would now be invalid
517 1.1 cgd */
518 1.13 christos void
519 1.34 enami cache_purge(struct vnode *vp)
520 1.1 cgd {
521 1.46 yamt struct namecache *ncp, *ncnext;
522 1.1 cgd
523 1.39 pk simple_lock(&namecache_slock);
524 1.46 yamt for (ncp = LIST_FIRST(&vp->v_nclist); ncp != NULL; ncp = ncnext) {
525 1.46 yamt ncnext = LIST_NEXT(ncp, nc_vlist);
526 1.46 yamt cache_remove(ncp);
527 1.46 yamt cache_free(ncp);
528 1.46 yamt }
529 1.46 yamt for (ncp = LIST_FIRST(&vp->v_dnclist); ncp != NULL; ncp = ncnext) {
530 1.46 yamt ncnext = LIST_NEXT(ncp, nc_dvlist);
531 1.46 yamt cache_remove(ncp);
532 1.46 yamt cache_free(ncp);
533 1.46 yamt }
534 1.39 pk simple_unlock(&namecache_slock);
535 1.1 cgd }
536 1.1 cgd
537 1.1 cgd /*
538 1.1 cgd * Cache flush, a whole filesystem; called when filesys is umounted to
539 1.27 chs * remove entries that would now be invalid.
540 1.1 cgd */
541 1.13 christos void
542 1.34 enami cache_purgevfs(struct mount *mp)
543 1.1 cgd {
544 1.23 augustss struct namecache *ncp, *nxtcp;
545 1.1 cgd
546 1.39 pk simple_lock(&namecache_slock);
547 1.27 chs for (ncp = TAILQ_FIRST(&nclruhead); ncp != NULL; ncp = nxtcp) {
548 1.27 chs nxtcp = TAILQ_NEXT(ncp, nc_lru);
549 1.5 mycroft if (ncp->nc_dvp == NULL || ncp->nc_dvp->v_mount != mp) {
550 1.1 cgd continue;
551 1.5 mycroft }
552 1.34 enami /* Free the resources we had. */
553 1.46 yamt cache_remove(ncp);
554 1.46 yamt cache_free(ncp);
555 1.1 cgd }
556 1.39 pk simple_unlock(&namecache_slock);
557 1.1 cgd }
558 1.19 sommerfe
559 1.28 chs #ifdef DDB
560 1.28 chs void
561 1.28 chs namecache_print(struct vnode *vp, void (*pr)(const char *, ...))
562 1.28 chs {
563 1.28 chs struct vnode *dvp = NULL;
564 1.28 chs struct namecache *ncp;
565 1.28 chs
566 1.28 chs TAILQ_FOREACH(ncp, &nclruhead, nc_lru) {
567 1.47 yamt if (ncp->nc_vp == vp) {
568 1.28 chs (*pr)("name %.*s\n", ncp->nc_nlen, ncp->nc_name);
569 1.28 chs dvp = ncp->nc_dvp;
570 1.28 chs }
571 1.28 chs }
572 1.28 chs if (dvp == NULL) {
573 1.28 chs (*pr)("name not found\n");
574 1.28 chs return;
575 1.28 chs }
576 1.28 chs vp = dvp;
577 1.28 chs TAILQ_FOREACH(ncp, &nclruhead, nc_lru) {
578 1.47 yamt if (ncp->nc_vp == vp) {
579 1.28 chs (*pr)("parent %.*s\n", ncp->nc_nlen, ncp->nc_name);
580 1.28 chs }
581 1.28 chs }
582 1.28 chs }
583 1.28 chs #endif
584