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