vfs_cache.c revision 1.48 1 /* $NetBSD: vfs_cache.c,v 1.48 2003/07/31 15:13:05 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.48 2003/07/31 15:13:05 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) ((cnp)->cn_hash & nchash)
83
84 LIST_HEAD(ncvhashhead, namecache) *ncvhashtbl;
85 u_long ncvhash; /* size of hash table - 1 */
86 #define NCVHASH(vp) (((uintptr_t)(vp) >> 3) & ncvhash)
87
88 TAILQ_HEAD(, namecache) nclruhead; /* LRU chain */
89 struct nchstats nchstats; /* cache effectiveness statistics */
90
91 struct pool namecache_pool;
92
93 MALLOC_DEFINE(M_CACHE, "namecache", "Dynamically allocated cache entries");
94
95 int doingcache = 1; /* 1 => enable the cache */
96
97 /* A single lock to protect cache insertion, removal and lookup */
98 static struct simplelock namecache_slock = SIMPLELOCK_INITIALIZER;
99
100 static void cache_remove(struct namecache *);
101 static void cache_free(struct namecache *);
102
103 static void
104 cache_remove(struct namecache *ncp)
105 {
106
107 LOCK_ASSERT(simple_lock_held(&namecache_slock));
108
109 ncp->nc_dvp = NULL;
110 ncp->nc_vp = NULL;
111
112 TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
113 if (ncp->nc_hash.le_prev != NULL) {
114 LIST_REMOVE(ncp, nc_hash);
115 ncp->nc_hash.le_prev = NULL;
116 }
117 if (ncp->nc_vhash.le_prev != NULL) {
118 LIST_REMOVE(ncp, nc_vhash);
119 ncp->nc_vhash.le_prev = NULL;
120 }
121 if (ncp->nc_vlist.le_prev != NULL) {
122 LIST_REMOVE(ncp, nc_vlist);
123 ncp->nc_vlist.le_prev = NULL;
124 }
125 if (ncp->nc_dvlist.le_prev != NULL) {
126 LIST_REMOVE(ncp, nc_dvlist);
127 ncp->nc_dvlist.le_prev = NULL;
128 }
129 }
130
131 static void
132 cache_free(struct namecache *ncp)
133 {
134
135 pool_put(&namecache_pool, ncp);
136 numcache--; /* XXX MP */
137 }
138
139 /*
140 * Look for a the name in the cache. We don't do this
141 * if the segment name is long, simply so the cache can avoid
142 * holding long names (which would either waste space, or
143 * add greatly to the complexity).
144 *
145 * Lookup is called with ni_dvp pointing to the directory to search,
146 * ni_ptr pointing to the name of the entry being sought, ni_namelen
147 * tells the length of the name, and ni_hash contains a hash of
148 * the name. If the lookup succeeds, the vnode is locked, stored in ni_vp
149 * and a status of zero is returned. If the locking fails for whatever
150 * reason, the vnode is unlocked and the error is returned to caller.
151 * If the lookup determines that the name does not exist (negative cacheing),
152 * a status of ENOENT is returned. If the lookup fails, a status of -1
153 * is returned.
154 */
155 int
156 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
157 {
158 struct namecache *ncp;
159 struct nchashhead *ncpp;
160 struct vnode *vp;
161 int error;
162
163 if (!doingcache) {
164 cnp->cn_flags &= ~MAKEENTRY;
165 *vpp = NULL;
166 return (-1);
167 }
168
169 simple_lock(&namecache_slock);
170 if (cnp->cn_namelen > NCHNAMLEN) {
171 nchstats.ncs_long++;
172 cnp->cn_flags &= ~MAKEENTRY;
173 goto fail_wlock;
174 }
175 ncpp = &nchashtbl[NCHASH(cnp, dvp)];
176 LIST_FOREACH(ncp, ncpp, nc_hash) {
177 if (ncp->nc_dvp == dvp &&
178 ncp->nc_nlen == cnp->cn_namelen &&
179 !memcmp(ncp->nc_name, cnp->cn_nameptr, (u_int)ncp->nc_nlen))
180 break;
181 }
182 if (ncp == 0) {
183 nchstats.ncs_miss++;
184 goto fail_wlock;
185 }
186 if ((cnp->cn_flags & MAKEENTRY) == 0) {
187 nchstats.ncs_badhits++;
188 goto remove;
189 } else if (ncp->nc_vp == NULL) {
190 /*
191 * Restore the ISWHITEOUT flag saved earlier.
192 */
193 cnp->cn_flags |= ncp->nc_vpid;
194 if (cnp->cn_nameiop != CREATE ||
195 (cnp->cn_flags & ISLASTCN) == 0) {
196 nchstats.ncs_neghits++;
197 /*
198 * Move this slot to end of LRU chain,
199 * if not already there.
200 */
201 if (TAILQ_NEXT(ncp, nc_lru) != 0) {
202 TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
203 TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
204 }
205 simple_unlock(&namecache_slock);
206 return (ENOENT);
207 } else {
208 nchstats.ncs_badhits++;
209 goto remove;
210 }
211 }
212
213 vp = ncp->nc_vp;
214 /* Release the name cache mutex while we acquire vnode locks */
215 simple_unlock(&namecache_slock);
216
217 if (vp == dvp) { /* lookup on "." */
218 VREF(dvp);
219 error = 0;
220 } else if (cnp->cn_flags & ISDOTDOT) {
221 VOP_UNLOCK(dvp, 0);
222 cnp->cn_flags |= PDIRUNLOCK;
223 error = vget(vp, LK_EXCLUSIVE);
224 /*
225 * If the above vget() succeeded and both LOCKPARENT and
226 * ISLASTCN is set, lock the directory vnode as well.
227 */
228 if (!error && (~cnp->cn_flags & (LOCKPARENT|ISLASTCN)) == 0) {
229 if ((error = vn_lock(dvp, LK_EXCLUSIVE)) != 0) {
230 vput(vp);
231 return (error);
232 }
233 cnp->cn_flags &= ~PDIRUNLOCK;
234 }
235 } else {
236 error = vget(vp, LK_EXCLUSIVE);
237 /*
238 * If the above vget() failed or either of LOCKPARENT or
239 * ISLASTCN is set, unlock the directory vnode.
240 */
241 if (error || (~cnp->cn_flags & (LOCKPARENT|ISLASTCN)) != 0) {
242 VOP_UNLOCK(dvp, 0);
243 cnp->cn_flags |= PDIRUNLOCK;
244 }
245 }
246
247 /*
248 * Check that the lock succeeded, and that the capability number did
249 * not change while we were waiting for the lock.
250 */
251 if (error) {
252 /* XXXSMP - updating stats without lock; do we care? */
253 if (!error) {
254 vput(vp);
255 nchstats.ncs_falsehits++;
256 } else
257 nchstats.ncs_badhits++;
258
259 /*
260 * The parent needs to be locked when we return to VOP_LOOKUP().
261 * The `.' case here should be extremely rare (if it can happen
262 * at all), so we don't bother optimizing out the unlock/relock.
263 */
264 if (vp == dvp ||
265 error || (~cnp->cn_flags & (LOCKPARENT|ISLASTCN)) != 0) {
266 if ((error = vn_lock(dvp, LK_EXCLUSIVE)) != 0)
267 return (error);
268 cnp->cn_flags &= ~PDIRUNLOCK;
269 }
270 *vpp = NULL;
271 return (-1);
272 }
273
274 simple_lock(&namecache_slock);
275 nchstats.ncs_goodhits++;
276 /*
277 * Move this slot to end of LRU chain, if not already there.
278 */
279 if (TAILQ_NEXT(ncp, nc_lru) != 0) {
280 TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
281 TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
282 }
283
284 simple_unlock(&namecache_slock);
285 *vpp = vp;
286 return (0);
287
288 remove:
289 /*
290 * Last component and we are renaming or deleting,
291 * the cache entry is invalid, or otherwise don't
292 * want cache entry to exist.
293 */
294 TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
295 LIST_REMOVE(ncp, nc_hash);
296 ncp->nc_hash.le_prev = NULL;
297 if (ncp->nc_vhash.le_prev != NULL) {
298 LIST_REMOVE(ncp, nc_vhash);
299 ncp->nc_vhash.le_prev = NULL;
300 }
301 TAILQ_INSERT_HEAD(&nclruhead, ncp, nc_lru);
302
303 fail_wlock:
304 simple_unlock(&namecache_slock);
305 *vpp = NULL;
306 return (-1);
307 }
308
309 /*
310 * Scan cache looking for name of directory entry pointing at vp.
311 *
312 * Fill in dvpp.
313 *
314 * If bufp is non-NULL, also place the name in the buffer which starts
315 * at bufp, immediately before *bpp, and move bpp backwards to point
316 * at the start of it. (Yes, this is a little baroque, but it's done
317 * this way to cater to the whims of getcwd).
318 *
319 * Returns 0 on success, -1 on cache miss, positive errno on failure.
320 */
321 int
322 cache_revlookup(struct vnode *vp, struct vnode **dvpp, char **bpp, char *bufp)
323 {
324 struct namecache *ncp;
325 struct vnode *dvp;
326 struct ncvhashhead *nvcpp;
327 char *bp;
328
329 if (!doingcache)
330 goto out;
331
332 nvcpp = &ncvhashtbl[NCVHASH(vp)];
333
334 simple_lock(&namecache_slock);
335 LIST_FOREACH(ncp, nvcpp, nc_vhash) {
336 if (ncp->nc_vp == vp &&
337 (dvp = ncp->nc_dvp) != NULL &&
338 dvp != vp) { /* avoid pesky . entries.. */
339
340 #ifdef DIAGNOSTIC
341 if (ncp->nc_nlen == 1 &&
342 ncp->nc_name[0] == '.')
343 panic("cache_revlookup: found entry for .");
344
345 if (ncp->nc_nlen == 2 &&
346 ncp->nc_name[0] == '.' &&
347 ncp->nc_name[1] == '.')
348 panic("cache_revlookup: found entry for ..");
349 #endif
350 nchstats.ncs_revhits++;
351
352 if (bufp) {
353 bp = *bpp;
354 bp -= ncp->nc_nlen;
355 if (bp <= bufp) {
356 *dvpp = NULL;
357 simple_unlock(&namecache_slock);
358 return (ERANGE);
359 }
360 memcpy(bp, ncp->nc_name, ncp->nc_nlen);
361 *bpp = bp;
362 }
363
364 /* XXX MP: how do we know dvp won't evaporate? */
365 *dvpp = dvp;
366 simple_unlock(&namecache_slock);
367 return (0);
368 }
369 }
370 nchstats.ncs_revmiss++;
371 simple_unlock(&namecache_slock);
372 out:
373 *dvpp = NULL;
374 return (-1);
375 }
376
377 /*
378 * Add an entry to the cache
379 */
380 void
381 cache_enter(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
382 {
383 struct namecache *ncp;
384 struct nchashhead *ncpp;
385 struct ncvhashhead *nvcpp;
386
387 #ifdef DIAGNOSTIC
388 if (cnp->cn_namelen > NCHNAMLEN)
389 panic("cache_enter: name too long");
390 #endif
391 if (!doingcache)
392 return;
393 /*
394 * Free the cache slot at head of lru chain.
395 */
396 simple_lock(&namecache_slock);
397 if (numcache < numvnodes) {
398 numcache++;
399 simple_unlock(&namecache_slock);
400 ncp = pool_get(&namecache_pool, PR_WAITOK);
401 memset(ncp, 0, sizeof(*ncp));
402 simple_lock(&namecache_slock);
403 } else if ((ncp = TAILQ_FIRST(&nclruhead)) != NULL) {
404 cache_remove(ncp);
405 } else {
406 simple_unlock(&namecache_slock);
407 return;
408 }
409 /* Grab the vnode we just found. */
410 ncp->nc_vp = vp;
411 if (vp == NULL) {
412 /*
413 * For negative hits, save the ISWHITEOUT flag so we can
414 * restore it later when the cache entry is used again.
415 */
416 ncp->nc_vpid = cnp->cn_flags & ISWHITEOUT;
417 }
418 /* Fill in cache info. */
419 ncp->nc_dvp = dvp;
420 LIST_INSERT_HEAD(&dvp->v_dnclist, ncp, nc_dvlist);
421 if (vp)
422 LIST_INSERT_HEAD(&vp->v_nclist, ncp, nc_vlist);
423 ncp->nc_nlen = cnp->cn_namelen;
424 memcpy(ncp->nc_name, cnp->cn_nameptr, (unsigned)ncp->nc_nlen);
425 TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
426 ncpp = &nchashtbl[NCHASH(cnp, dvp)];
427 LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
428
429 ncp->nc_vhash.le_prev = NULL;
430 ncp->nc_vhash.le_next = NULL;
431
432 /*
433 * Create reverse-cache entries (used in getcwd) for directories.
434 */
435 if (vp != NULL &&
436 vp != dvp &&
437 #ifndef NAMECACHE_ENTER_REVERSE
438 vp->v_type == VDIR &&
439 #endif
440 (ncp->nc_nlen > 2 ||
441 (ncp->nc_nlen > 1 && ncp->nc_name[1] != '.') ||
442 (/* ncp->nc_nlen > 0 && */ ncp->nc_name[0] != '.'))) {
443 nvcpp = &ncvhashtbl[NCVHASH(vp)];
444 LIST_INSERT_HEAD(nvcpp, ncp, nc_vhash);
445 }
446 simple_unlock(&namecache_slock);
447 }
448
449 /*
450 * Name cache initialization, from vfs_init() when we are booting
451 */
452 void
453 nchinit(void)
454 {
455
456 TAILQ_INIT(&nclruhead);
457 nchashtbl =
458 hashinit(desiredvnodes, HASH_LIST, M_CACHE, M_WAITOK, &nchash);
459 ncvhashtbl =
460 #ifdef NAMECACHE_ENTER_REVERSE
461 hashinit(desiredvnodes, HASH_LIST, M_CACHE, M_WAITOK, &ncvhash);
462 #else
463 hashinit(desiredvnodes/8, HASH_LIST, M_CACHE, M_WAITOK, &ncvhash);
464 #endif
465 pool_init(&namecache_pool, sizeof(struct namecache), 0, 0, 0,
466 "ncachepl", &pool_allocator_nointr);
467 }
468
469 /*
470 * Name cache reinitialization, for when the maximum number of vnodes increases.
471 */
472 void
473 nchreinit(void)
474 {
475 struct namecache *ncp;
476 struct nchashhead *oldhash1, *hash1;
477 struct ncvhashhead *oldhash2, *hash2;
478 u_long i, oldmask1, oldmask2, mask1, mask2;
479
480 hash1 = hashinit(desiredvnodes, HASH_LIST, M_CACHE, M_WAITOK, &mask1);
481 hash2 =
482 #ifdef NAMECACHE_ENTER_REVERSE
483 hashinit(desiredvnodes, HASH_LIST, M_CACHE, M_WAITOK, &mask2);
484 #else
485 hashinit(desiredvnodes/8, HASH_LIST, M_CACHE, M_WAITOK, &mask2);
486 #endif
487 simple_lock(&namecache_slock);
488 oldhash1 = nchashtbl;
489 oldmask1 = nchash;
490 nchashtbl = hash1;
491 nchash = mask1;
492 oldhash2 = ncvhashtbl;
493 oldmask2 = ncvhash;
494 ncvhashtbl = hash2;
495 ncvhash = mask2;
496 for (i = 0; i <= oldmask1; i++) {
497 while ((ncp = LIST_FIRST(&oldhash1[i])) != NULL) {
498 LIST_REMOVE(ncp, nc_hash);
499 ncp->nc_hash.le_prev = NULL;
500 }
501 }
502 for (i = 0; i <= oldmask2; i++) {
503 while ((ncp = LIST_FIRST(&oldhash2[i])) != NULL) {
504 LIST_REMOVE(ncp, nc_vhash);
505 ncp->nc_vhash.le_prev = NULL;
506 }
507 }
508 simple_unlock(&namecache_slock);
509 hashdone(oldhash1, M_CACHE);
510 hashdone(oldhash2, M_CACHE);
511 }
512
513 /*
514 * Cache flush, a particular vnode; called when a vnode is renamed to
515 * hide entries that would now be invalid
516 */
517 void
518 cache_purge(struct vnode *vp)
519 {
520 struct namecache *ncp, *ncnext;
521
522 simple_lock(&namecache_slock);
523 for (ncp = LIST_FIRST(&vp->v_nclist); ncp != NULL; ncp = ncnext) {
524 ncnext = LIST_NEXT(ncp, nc_vlist);
525 cache_remove(ncp);
526 cache_free(ncp);
527 }
528 for (ncp = LIST_FIRST(&vp->v_dnclist); ncp != NULL; ncp = ncnext) {
529 ncnext = LIST_NEXT(ncp, nc_dvlist);
530 cache_remove(ncp);
531 cache_free(ncp);
532 }
533 simple_unlock(&namecache_slock);
534 }
535
536 /*
537 * Cache flush, a whole filesystem; called when filesys is umounted to
538 * remove entries that would now be invalid.
539 */
540 void
541 cache_purgevfs(struct mount *mp)
542 {
543 struct namecache *ncp, *nxtcp;
544
545 simple_lock(&namecache_slock);
546 for (ncp = TAILQ_FIRST(&nclruhead); ncp != NULL; ncp = nxtcp) {
547 nxtcp = TAILQ_NEXT(ncp, nc_lru);
548 if (ncp->nc_dvp == NULL || ncp->nc_dvp->v_mount != mp) {
549 continue;
550 }
551 /* Free the resources we had. */
552 cache_remove(ncp);
553 cache_free(ncp);
554 }
555 simple_unlock(&namecache_slock);
556 }
557
558 #ifdef DDB
559 void
560 namecache_print(struct vnode *vp, void (*pr)(const char *, ...))
561 {
562 struct vnode *dvp = NULL;
563 struct namecache *ncp;
564
565 TAILQ_FOREACH(ncp, &nclruhead, nc_lru) {
566 if (ncp->nc_vp == vp) {
567 (*pr)("name %.*s\n", ncp->nc_nlen, ncp->nc_name);
568 dvp = ncp->nc_dvp;
569 }
570 }
571 if (dvp == NULL) {
572 (*pr)("name not found\n");
573 return;
574 }
575 vp = dvp;
576 TAILQ_FOREACH(ncp, &nclruhead, nc_lru) {
577 if (ncp->nc_vp == vp) {
578 (*pr)("parent %.*s\n", ncp->nc_nlen, ncp->nc_name);
579 }
580 }
581 }
582 #endif
583