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