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