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