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