1 /* 2 * Copyright (c) 1989, 1993, 1995 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Poul-Henning Kamp of the FreeBSD Project. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)vfs_cache.c 8.5 (Berkeley) 3/22/95 37 * $Id: vfs_cache.c,v 1.36 1997/11/07 08:53:05 phk Exp $ 38 */ 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/kernel.h> 43 #include <sys/sysctl.h> 44 #include <sys/mount.h> 45 #include <sys/vnode.h> 46 #include <sys/namei.h> 47 #include <sys/malloc.h> 48 49 50 /* 51 * Name caching works as follows: 52 * 53 * Names found by directory scans are retained in a cache 54 * for future reference. It is managed LRU, so frequently 55 * used names will hang around. Cache is indexed by hash value 56 * obtained from (vp, name) where vp refers to the directory 57 * containing name. 58 * 59 * If it is a "negative" entry, (i.e. for a name that is known NOT to 60 * exist) the vnode pointer will be NULL. 61 * 62 * Upon reaching the last segment of a path, if the reference 63 * is for DELETE, or NOCACHE is set (rewrite), and the 64 * name is located in the cache, it will be dropped. 65 */ 66 67 /* 68 * Structures associated with name cacheing. 69 */ 70 #define NCHHASH(dvp, cnp) \ 71 (&nchashtbl[((dvp)->v_id + (cnp)->cn_hash) & nchash]) 72 static LIST_HEAD(nchashhead, namecache) *nchashtbl; /* Hash Table */ 73 static TAILQ_HEAD(, namecache) ncneg; /* Hash Table */ 74 static u_long nchash; /* size of hash table */ 75 SYSCTL_INT(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0, ""); 76 static u_long ncnegfactor = 16; /* ratio of negative entries */ 77 SYSCTL_INT(_debug, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0, ""); 78 static u_long numneg; /* number of cache entries allocated */ 79 SYSCTL_INT(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0, ""); 80 static u_long numcache; /* number of cache entries allocated */ 81 SYSCTL_INT(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0, ""); 82 struct nchstats nchstats; /* cache effectiveness statistics */ 83 84 static int doingcache = 1; /* 1 => enable the cache */ 85 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0, ""); 86 SYSCTL_INT(_debug, OID_AUTO, vnsize, CTLFLAG_RD, 0, sizeof(struct vnode), ""); 87 SYSCTL_INT(_debug, OID_AUTO, ncsize, CTLFLAG_RD, 0, sizeof(struct namecache), ""); 88 89 /* 90 * The new name cache statistics 91 */ 92 SYSCTL_NODE(_vfs, CTL_VFS, cache, CTLFLAG_RW, 0, 93 "Name cache statistics"); 94 #define STATNODE(mode, name, var) \ 95 SYSCTL_INT(_vfs_cache, OID_AUTO, name, mode, var, 0, ""); 96 STATNODE(CTLFLAG_RD, numneg, &numneg); 97 STATNODE(CTLFLAG_RD, numcache, &numcache); 98 static u_long numcalls; STATNODE(CTLFLAG_RD, numcalls, &numcalls); 99 static u_long dothits; STATNODE(CTLFLAG_RD, dothits, &dothits); 100 static u_long dotdothits; STATNODE(CTLFLAG_RD, dotdothits, &dotdothits); 101 static u_long numchecks; STATNODE(CTLFLAG_RD, numchecks, &numchecks); 102 static u_long nummiss; STATNODE(CTLFLAG_RD, nummiss, &nummiss); 103 static u_long nummisszap; STATNODE(CTLFLAG_RD, nummisszap, &nummisszap); 104 static u_long numposzaps; STATNODE(CTLFLAG_RD, numposzaps, &numposzaps); 105 static u_long numposhits; STATNODE(CTLFLAG_RD, numposhits, &numposhits); 106 static u_long numnegzaps; STATNODE(CTLFLAG_RD, numnegzaps, &numnegzaps); 107 static u_long numneghits; STATNODE(CTLFLAG_RD, numneghits, &numneghits); 108 109 110 static void cache_zap __P((struct namecache *ncp)); 111 112 /* 113 * Flags in namecache.nc_flag 114 */ 115 #define NCF_WHITE 1 116 /* 117 * Delete an entry from its hash list and move it to the front 118 * of the LRU list for immediate reuse. 119 */ 120 static void 121 cache_zap(ncp) 122 struct namecache *ncp; 123 { 124 LIST_REMOVE(ncp, nc_hash); 125 LIST_REMOVE(ncp, nc_src); 126 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) 127 vdrop(ncp->nc_dvp); 128 if (ncp->nc_vp) { 129 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst); 130 } else { 131 TAILQ_REMOVE(&ncneg, ncp, nc_dst); 132 numneg--; 133 } 134 numcache--; 135 free(ncp, M_CACHE); 136 } 137 138 /* 139 * Lookup an entry in the cache 140 * 141 * We don't do this if the segment name is long, simply so the cache 142 * can avoid holding long names (which would either waste space, or 143 * add greatly to the complexity). 144 * 145 * Lookup is called with dvp pointing to the directory to search, 146 * cnp pointing to the name of the entry being sought. If the lookup 147 * succeeds, the vnode is returned in *vpp, and a status of -1 is 148 * returned. If the lookup determines that the name does not exist 149 * (negative cacheing), a status of ENOENT is returned. If the lookup 150 * fails, a status of zero is returned. 151 */ 152 153 int 154 cache_lookup(dvp, vpp, cnp) 155 struct vnode *dvp; 156 struct vnode **vpp; 157 struct componentname *cnp; 158 { 159 register struct namecache *ncp; 160 161 if (!doingcache) { 162 cnp->cn_flags &= ~MAKEENTRY; 163 return (0); 164 } 165 166 numcalls++; 167 168 if (cnp->cn_nameptr[0] == '.') { 169 if (cnp->cn_namelen == 1) { 170 *vpp = dvp; 171 dothits++; 172 return (-1); 173 } 174 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') { 175 dotdothits++; 176 if (dvp->v_dd->v_id != dvp->v_ddid || 177 (cnp->cn_flags & MAKEENTRY) == 0) { 178 dvp->v_ddid = 0; 179 return (0); 180 } 181 *vpp = dvp->v_dd; 182 return (-1); 183 } 184 } 185 186 LIST_FOREACH(ncp, (NCHHASH(dvp, cnp)), nc_hash) { 187 numchecks++; 188 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen && 189 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen)) 190 break; 191 } 192 193 /* We failed to find an entry */ 194 if (ncp == 0) { 195 if ((cnp->cn_flags & MAKEENTRY) == 0) { 196 nummisszap++; 197 } else { 198 nummiss++; 199 } 200 nchstats.ncs_miss++; 201 return (0); 202 } 203 204 /* We don't want to have an entry, so dump it */ 205 if ((cnp->cn_flags & MAKEENTRY) == 0) { 206 numposzaps++; 207 nchstats.ncs_badhits++; 208 cache_zap(ncp); 209 return (0); 210 } 211 212 /* We found a "positive" match, return the vnode */ 213 if (ncp->nc_vp) { 214 numposhits++; 215 nchstats.ncs_goodhits++; 216 *vpp = ncp->nc_vp; 217 return (-1); 218 } 219 220 /* We found a negative match, and want to create it, so purge */ 221 if (cnp->cn_nameiop == CREATE) { 222 numnegzaps++; 223 nchstats.ncs_badhits++; 224 cache_zap(ncp); 225 return (0); 226 } 227 228 numneghits++; 229 /* 230 * We found a "negative" match, ENOENT notifies client of this match. 231 * The nc_vpid field records whether this is a whiteout. 232 */ 233 TAILQ_REMOVE(&ncneg, ncp, nc_dst); 234 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst); 235 nchstats.ncs_neghits++; 236 if (ncp->nc_flag & NCF_WHITE) 237 cnp->cn_flags |= ISWHITEOUT; 238 return (ENOENT); 239 } 240 241 /* 242 * Add an entry to the cache. 243 */ 244 void 245 cache_enter(dvp, vp, cnp) 246 struct vnode *dvp; 247 struct vnode *vp; 248 struct componentname *cnp; 249 { 250 register struct namecache *ncp; 251 register struct nchashhead *ncpp; 252 253 if (!doingcache) 254 return; 255 256 if (cnp->cn_nameptr[0] == '.') { 257 if (cnp->cn_namelen == 1) { 258 return; 259 } 260 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') { 261 if (vp) { 262 dvp->v_dd = vp; 263 dvp->v_ddid = vp->v_id; 264 } else { 265 dvp->v_dd = dvp; 266 dvp->v_ddid = 0; 267 } 268 return; 269 } 270 } 271 272 ncp = (struct namecache *) 273 malloc(sizeof *ncp + cnp->cn_namelen, M_CACHE, M_WAITOK); 274 bzero((char *)ncp, sizeof *ncp); 275 numcache++; 276 if (!vp) { 277 numneg++; 278 ncp->nc_flag = cnp->cn_flags & ISWHITEOUT ? NCF_WHITE : 0; 279 } else if (vp->v_type == VDIR) { 280 vp->v_dd = dvp; 281 vp->v_ddid = dvp->v_id; 282 } 283 284 /* 285 * Fill in cache info, if vp is NULL this is a "negative" cache entry. 286 * For negative entries, we have to record whether it is a whiteout. 287 * the whiteout flag is stored in the nc_vpid field which is 288 * otherwise unused. 289 */ 290 ncp->nc_vp = vp; 291 ncp->nc_dvp = dvp; 292 ncp->nc_nlen = cnp->cn_namelen; 293 bcopy(cnp->cn_nameptr, ncp->nc_name, ncp->nc_nlen); 294 ncpp = NCHHASH(dvp, cnp); 295 LIST_INSERT_HEAD(ncpp, ncp, nc_hash); 296 if (LIST_EMPTY(&dvp->v_cache_src)) 297 vhold(dvp); 298 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src); 299 if (vp) { 300 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst); 301 } else { 302 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst); 303 } 304 if (numneg*ncnegfactor > numcache) { 305 ncp = TAILQ_FIRST(&ncneg); 306 cache_zap(ncp); 307 } 308 } 309 310 /* 311 * Name cache initialization, from vfs_init() when we are booting 312 */ 313 void 314 nchinit() 315 { 316 317 TAILQ_INIT(&ncneg); 318 nchashtbl = hashinit(desiredvnodes*2, M_CACHE, &nchash); 319 } 320 321 /* 322 * Invalidate all entries to particular vnode. 323 * 324 * We actually just increment the v_id, that will do it. The stale entries 325 * will be purged by lookup as they get found. If the v_id wraps around, we 326 * need to ditch the entire cache, to avoid confusion. No valid vnode will 327 * ever have (v_id == 0). 328 */ 329 void 330 cache_purge(vp) 331 struct vnode *vp; 332 { 333 static u_long nextid; 334 335 while (!LIST_EMPTY(&vp->v_cache_src)) 336 cache_zap(LIST_FIRST(&vp->v_cache_src)); 337 while (!TAILQ_EMPTY(&vp->v_cache_dst)) 338 cache_zap(TAILQ_FIRST(&vp->v_cache_dst)); 339 340 nextid++; 341 while (nextid == vp->v_id || !nextid) 342 continue; 343 vp->v_id = nextid; 344 vp->v_dd = vp; 345 vp->v_ddid = 0; 346 } 347 348 /* 349 * Flush all entries referencing a particular filesystem. 350 * 351 * Since we need to check it anyway, we will flush all the invalid 352 * entries at the same time. 353 */ 354 void 355 cache_purgevfs(mp) 356 struct mount *mp; 357 { 358 struct nchashhead *ncpp; 359 struct namecache *ncp, *nnp; 360 361 /* Scan hash tables for applicable entries */ 362 for (ncpp = &nchashtbl[nchash]; ncpp >= nchashtbl; ncpp--) { 363 for (ncp = LIST_FIRST(ncpp); ncp != 0; ncp = nnp) { 364 nnp = LIST_NEXT(ncp, nc_hash); 365 if (ncp->nc_dvp->v_mount == mp) { 366 cache_zap(ncp); 367 } 368 } 369 } 370 } 371 372 /* 373 * Perform canonical checks and cache lookup and pass on to filesystem 374 * through the vop_cachedlookup only if needed. 375 */ 376 377 int 378 vfs_cache_lookup(ap) 379 struct vop_lookup_args /* { 380 struct vnode *a_dvp; 381 struct vnode **a_vpp; 382 struct componentname *a_cnp; 383 } */ *ap; 384 { 385 struct vnode *vdp; 386 struct vnode *pdp; 387 int lockparent; 388 int error; 389 struct vnode **vpp = ap->a_vpp; 390 struct componentname *cnp = ap->a_cnp; 391 struct ucred *cred = cnp->cn_cred; 392 int flags = cnp->cn_flags; 393 struct proc *p = cnp->cn_proc; 394 u_long vpid; /* capability number of vnode */ 395 396 *vpp = NULL; 397 vdp = ap->a_dvp; 398 lockparent = flags & LOCKPARENT; 399 400 if (vdp->v_type != VDIR) 401 return (ENOTDIR); 402 403 if ((flags & ISLASTCN) && (vdp->v_mount->mnt_flag & MNT_RDONLY) && 404 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) 405 return (EROFS); 406 407 error = VOP_ACCESS(vdp, VEXEC, cred, cnp->cn_proc); 408 409 if (error) 410 return (error); 411 412 error = cache_lookup(vdp, vpp, cnp); 413 414 if (!error) 415 return (VOP_CACHEDLOOKUP(ap->a_dvp, ap->a_vpp, ap->a_cnp)); 416 417 if (error == ENOENT) 418 return (error); 419 420 pdp = vdp; 421 vdp = *vpp; 422 vpid = vdp->v_id; 423 if (pdp == vdp) { /* lookup on "." */ 424 VREF(vdp); 425 error = 0; 426 } else if (flags & ISDOTDOT) { 427 VOP_UNLOCK(pdp, 0, p); 428 error = vget(vdp, LK_EXCLUSIVE, p); 429 if (!error && lockparent && (flags & ISLASTCN)) 430 error = vn_lock(pdp, LK_EXCLUSIVE, p); 431 } else { 432 error = vget(vdp, LK_EXCLUSIVE, p); 433 if (!lockparent || error || !(flags & ISLASTCN)) 434 VOP_UNLOCK(pdp, 0, p); 435 } 436 /* 437 * Check that the capability number did not change 438 * while we were waiting for the lock. 439 */ 440 if (!error) { 441 if (vpid == vdp->v_id) 442 return (0); 443 vput(vdp); 444 if (lockparent && pdp != vdp && (flags & ISLASTCN)) 445 VOP_UNLOCK(pdp, 0, p); 446 } 447 error = vn_lock(pdp, LK_EXCLUSIVE, p); 448 if (error) 449 return (error); 450 return (VOP_CACHEDLOOKUP(ap->a_dvp, ap->a_vpp, ap->a_cnp)); 451 } 452