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 * 4. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)vfs_cache.c 8.5 (Berkeley) 3/22/95 33 */ 34 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 #include <sys/param.h> 39 #include <sys/filedesc.h> 40 #include <sys/fnv_hash.h> 41 #include <sys/kernel.h> 42 #include <sys/lock.h> 43 #include <sys/malloc.h> 44 #include <sys/mount.h> 45 #include <sys/mutex.h> 46 #include <sys/namei.h> 47 #include <sys/proc.h> 48 #include <sys/syscallsubr.h> 49 #include <sys/sysctl.h> 50 #include <sys/sysproto.h> 51 #include <sys/systm.h> 52 #include <sys/vnode.h> 53 54 #include <vm/uma.h> 55 56 /* 57 * This structure describes the elements in the cache of recent 58 * names looked up by namei. 59 */ 60 61 struct namecache { 62 LIST_ENTRY(namecache) nc_hash; /* hash chain */ 63 LIST_ENTRY(namecache) nc_src; /* source vnode list */ 64 TAILQ_ENTRY(namecache) nc_dst; /* destination vnode list */ 65 struct vnode *nc_dvp; /* vnode of parent of name */ 66 struct vnode *nc_vp; /* vnode the name refers to */ 67 u_char nc_flag; /* flag bits */ 68 u_char nc_nlen; /* length of name */ 69 char nc_name[0]; /* segment name */ 70 }; 71 72 /* 73 * Name caching works as follows: 74 * 75 * Names found by directory scans are retained in a cache 76 * for future reference. It is managed LRU, so frequently 77 * used names will hang around. Cache is indexed by hash value 78 * obtained from (vp, name) where vp refers to the directory 79 * containing name. 80 * 81 * If it is a "negative" entry, (i.e. for a name that is known NOT to 82 * exist) the vnode pointer will be NULL. 83 * 84 * Upon reaching the last segment of a path, if the reference 85 * is for DELETE, or NOCACHE is set (rewrite), and the 86 * name is located in the cache, it will be dropped. 87 */ 88 89 /* 90 * Structures associated with name cacheing. 91 */ 92 #define NCHHASH(hash) \ 93 (&nchashtbl[(hash) & nchash]) 94 static LIST_HEAD(nchashhead, namecache) *nchashtbl; /* Hash Table */ 95 static TAILQ_HEAD(, namecache) ncneg; /* Hash Table */ 96 static u_long nchash; /* size of hash table */ 97 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0, ""); 98 static u_long ncnegfactor = 16; /* ratio of negative entries */ 99 SYSCTL_ULONG(_debug, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0, ""); 100 static u_long numneg; /* number of cache entries allocated */ 101 SYSCTL_ULONG(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0, ""); 102 static u_long numcache; /* number of cache entries allocated */ 103 SYSCTL_ULONG(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0, ""); 104 static u_long numcachehv; /* number of cache entries with vnodes held */ 105 SYSCTL_ULONG(_debug, OID_AUTO, numcachehv, CTLFLAG_RD, &numcachehv, 0, ""); 106 #if 0 107 static u_long numcachepl; /* number of cache purge for leaf entries */ 108 SYSCTL_ULONG(_debug, OID_AUTO, numcachepl, CTLFLAG_RD, &numcachepl, 0, ""); 109 #endif 110 struct nchstats nchstats; /* cache effectiveness statistics */ 111 112 static struct mtx cache_lock; 113 MTX_SYSINIT(vfscache, &cache_lock, "Name Cache", MTX_DEF); 114 115 #define CACHE_LOCK() mtx_lock(&cache_lock) 116 #define CACHE_UNLOCK() mtx_unlock(&cache_lock) 117 118 /* 119 * UMA zones for the VFS cache. 120 * 121 * The small cache is used for entries with short names, which are the 122 * most common. The large cache is used for entries which are too big to 123 * fit in the small cache. 124 */ 125 static uma_zone_t cache_zone_small; 126 static uma_zone_t cache_zone_large; 127 128 #define CACHE_PATH_CUTOFF 32 129 #define CACHE_ZONE_SMALL (sizeof(struct namecache) + CACHE_PATH_CUTOFF) 130 #define CACHE_ZONE_LARGE (sizeof(struct namecache) + NAME_MAX) 131 132 #define cache_alloc(len) uma_zalloc(((len) <= CACHE_PATH_CUTOFF) ? \ 133 cache_zone_small : cache_zone_large, M_WAITOK) 134 #define cache_free(ncp) do { \ 135 if (ncp != NULL) \ 136 uma_zfree(((ncp)->nc_nlen <= CACHE_PATH_CUTOFF) ? \ 137 cache_zone_small : cache_zone_large, (ncp)); \ 138 } while (0) 139 140 static int doingcache = 1; /* 1 => enable the cache */ 141 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0, ""); 142 143 /* Export size information to userland */ 144 SYSCTL_INT(_debug_sizeof, OID_AUTO, namecache, CTLFLAG_RD, 0, 145 sizeof(struct namecache), ""); 146 147 /* 148 * The new name cache statistics 149 */ 150 static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0, "Name cache statistics"); 151 #define STATNODE(mode, name, var) \ 152 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, mode, var, 0, ""); 153 STATNODE(CTLFLAG_RD, numneg, &numneg); 154 STATNODE(CTLFLAG_RD, numcache, &numcache); 155 static u_long numcalls; STATNODE(CTLFLAG_RD, numcalls, &numcalls); 156 static u_long dothits; STATNODE(CTLFLAG_RD, dothits, &dothits); 157 static u_long dotdothits; STATNODE(CTLFLAG_RD, dotdothits, &dotdothits); 158 static u_long numchecks; STATNODE(CTLFLAG_RD, numchecks, &numchecks); 159 static u_long nummiss; STATNODE(CTLFLAG_RD, nummiss, &nummiss); 160 static u_long nummisszap; STATNODE(CTLFLAG_RD, nummisszap, &nummisszap); 161 static u_long numposzaps; STATNODE(CTLFLAG_RD, numposzaps, &numposzaps); 162 static u_long numposhits; STATNODE(CTLFLAG_RD, numposhits, &numposhits); 163 static u_long numnegzaps; STATNODE(CTLFLAG_RD, numnegzaps, &numnegzaps); 164 static u_long numneghits; STATNODE(CTLFLAG_RD, numneghits, &numneghits); 165 166 SYSCTL_OPAQUE(_vfs_cache, OID_AUTO, nchstats, CTLFLAG_RD, &nchstats, 167 sizeof(nchstats), "LU", "VFS cache effectiveness statistics"); 168 169 170 171 static void cache_zap(struct namecache *ncp); 172 static int vn_vptocnp(struct vnode **vp, char **bp, char *buf, u_int *buflen); 173 static int vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir, 174 char *buf, char **retbuf, u_int buflen); 175 176 static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries"); 177 178 /* 179 * Flags in namecache.nc_flag 180 */ 181 #define NCF_WHITE 1 182 183 /* 184 * Grab an atomic snapshot of the name cache hash chain lengths 185 */ 186 SYSCTL_NODE(_debug, OID_AUTO, hashstat, CTLFLAG_RW, NULL, "hash table stats"); 187 188 static int 189 sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS) 190 { 191 int error; 192 struct nchashhead *ncpp; 193 struct namecache *ncp; 194 int n_nchash; 195 int count; 196 197 n_nchash = nchash + 1; /* nchash is max index, not count */ 198 if (!req->oldptr) 199 return SYSCTL_OUT(req, 0, n_nchash * sizeof(int)); 200 201 /* Scan hash tables for applicable entries */ 202 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) { 203 CACHE_LOCK(); 204 count = 0; 205 LIST_FOREACH(ncp, ncpp, nc_hash) { 206 count++; 207 } 208 CACHE_UNLOCK(); 209 error = SYSCTL_OUT(req, &count, sizeof(count)); 210 if (error) 211 return (error); 212 } 213 return (0); 214 } 215 SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD, 216 0, 0, sysctl_debug_hashstat_rawnchash, "S,int", "nchash chain lengths"); 217 218 static int 219 sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS) 220 { 221 int error; 222 struct nchashhead *ncpp; 223 struct namecache *ncp; 224 int n_nchash; 225 int count, maxlength, used, pct; 226 227 if (!req->oldptr) 228 return SYSCTL_OUT(req, 0, 4 * sizeof(int)); 229 230 n_nchash = nchash + 1; /* nchash is max index, not count */ 231 used = 0; 232 maxlength = 0; 233 234 /* Scan hash tables for applicable entries */ 235 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) { 236 count = 0; 237 CACHE_LOCK(); 238 LIST_FOREACH(ncp, ncpp, nc_hash) { 239 count++; 240 } 241 CACHE_UNLOCK(); 242 if (count) 243 used++; 244 if (maxlength < count) 245 maxlength = count; 246 } 247 n_nchash = nchash + 1; 248 pct = (used * 100 * 100) / n_nchash; 249 error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash)); 250 if (error) 251 return (error); 252 error = SYSCTL_OUT(req, &used, sizeof(used)); 253 if (error) 254 return (error); 255 error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength)); 256 if (error) 257 return (error); 258 error = SYSCTL_OUT(req, &pct, sizeof(pct)); 259 if (error) 260 return (error); 261 return (0); 262 } 263 SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD, 264 0, 0, sysctl_debug_hashstat_nchash, "I", "nchash chain lengths"); 265 266 /* 267 * cache_zap(): 268 * 269 * Removes a namecache entry from cache, whether it contains an actual 270 * pointer to a vnode or if it is just a negative cache entry. 271 */ 272 static void 273 cache_zap(ncp) 274 struct namecache *ncp; 275 { 276 struct vnode *vp; 277 278 mtx_assert(&cache_lock, MA_OWNED); 279 CTR2(KTR_VFS, "cache_zap(%p) vp %p", ncp, ncp->nc_vp); 280 vp = NULL; 281 LIST_REMOVE(ncp, nc_hash); 282 LIST_REMOVE(ncp, nc_src); 283 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) { 284 vp = ncp->nc_dvp; 285 numcachehv--; 286 } 287 if (ncp->nc_vp) { 288 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst); 289 ncp->nc_vp->v_dd = NULL; 290 } else { 291 TAILQ_REMOVE(&ncneg, ncp, nc_dst); 292 numneg--; 293 } 294 numcache--; 295 cache_free(ncp); 296 if (vp) 297 vdrop(vp); 298 } 299 300 /* 301 * Lookup an entry in the cache 302 * 303 * Lookup is called with dvp pointing to the directory to search, 304 * cnp pointing to the name of the entry being sought. If the lookup 305 * succeeds, the vnode is returned in *vpp, and a status of -1 is 306 * returned. If the lookup determines that the name does not exist 307 * (negative cacheing), a status of ENOENT is returned. If the lookup 308 * fails, a status of zero is returned. If the directory vnode is 309 * recycled out from under us due to a forced unmount, a status of 310 * EBADF is returned. 311 * 312 * vpp is locked and ref'd on return. If we're looking up DOTDOT, dvp is 313 * unlocked. If we're looking up . an extra ref is taken, but the lock is 314 * not recursively acquired. 315 */ 316 317 int 318 cache_lookup(dvp, vpp, cnp) 319 struct vnode *dvp; 320 struct vnode **vpp; 321 struct componentname *cnp; 322 { 323 struct namecache *ncp; 324 u_int32_t hash; 325 int error, ltype; 326 327 if (!doingcache) { 328 cnp->cn_flags &= ~MAKEENTRY; 329 return (0); 330 } 331 retry: 332 CACHE_LOCK(); 333 numcalls++; 334 335 if (cnp->cn_nameptr[0] == '.') { 336 if (cnp->cn_namelen == 1) { 337 *vpp = dvp; 338 CTR2(KTR_VFS, "cache_lookup(%p, %s) found via .", 339 dvp, cnp->cn_nameptr); 340 dothits++; 341 goto success; 342 } 343 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') { 344 dotdothits++; 345 if (dvp->v_dd == NULL || 346 (cnp->cn_flags & MAKEENTRY) == 0) { 347 CACHE_UNLOCK(); 348 return (0); 349 } 350 *vpp = dvp->v_dd; 351 CTR3(KTR_VFS, "cache_lookup(%p, %s) found %p via ..", 352 dvp, cnp->cn_nameptr, *vpp); 353 goto success; 354 } 355 } 356 357 hash = fnv_32_buf(cnp->cn_nameptr, cnp->cn_namelen, FNV1_32_INIT); 358 hash = fnv_32_buf(&dvp, sizeof(dvp), hash); 359 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) { 360 numchecks++; 361 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen && 362 !bcmp(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen)) 363 break; 364 } 365 366 /* We failed to find an entry */ 367 if (ncp == 0) { 368 if ((cnp->cn_flags & MAKEENTRY) == 0) { 369 nummisszap++; 370 } else { 371 nummiss++; 372 } 373 nchstats.ncs_miss++; 374 CACHE_UNLOCK(); 375 return (0); 376 } 377 378 /* We don't want to have an entry, so dump it */ 379 if ((cnp->cn_flags & MAKEENTRY) == 0) { 380 numposzaps++; 381 nchstats.ncs_badhits++; 382 cache_zap(ncp); 383 CACHE_UNLOCK(); 384 return (0); 385 } 386 387 /* We found a "positive" match, return the vnode */ 388 if (ncp->nc_vp) { 389 numposhits++; 390 nchstats.ncs_goodhits++; 391 *vpp = ncp->nc_vp; 392 CTR4(KTR_VFS, "cache_lookup(%p, %s) found %p via ncp %p", 393 dvp, cnp->cn_nameptr, *vpp, ncp); 394 goto success; 395 } 396 397 /* We found a negative match, and want to create it, so purge */ 398 if (cnp->cn_nameiop == CREATE) { 399 numnegzaps++; 400 nchstats.ncs_badhits++; 401 cache_zap(ncp); 402 CACHE_UNLOCK(); 403 return (0); 404 } 405 406 numneghits++; 407 /* 408 * We found a "negative" match, so we shift it to the end of 409 * the "negative" cache entries queue to satisfy LRU. Also, 410 * check to see if the entry is a whiteout; indicate this to 411 * the componentname, if so. 412 */ 413 TAILQ_REMOVE(&ncneg, ncp, nc_dst); 414 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst); 415 nchstats.ncs_neghits++; 416 if (ncp->nc_flag & NCF_WHITE) 417 cnp->cn_flags |= ISWHITEOUT; 418 CACHE_UNLOCK(); 419 return (ENOENT); 420 421 success: 422 /* 423 * On success we return a locked and ref'd vnode as per the lookup 424 * protocol. 425 */ 426 if (dvp == *vpp) { /* lookup on "." */ 427 VREF(*vpp); 428 CACHE_UNLOCK(); 429 /* 430 * When we lookup "." we still can be asked to lock it 431 * differently... 432 */ 433 ltype = cnp->cn_lkflags & LK_TYPE_MASK; 434 if (ltype != VOP_ISLOCKED(*vpp)) { 435 if (ltype == LK_EXCLUSIVE) { 436 vn_lock(*vpp, LK_UPGRADE | LK_RETRY); 437 if ((*vpp)->v_iflag & VI_DOOMED) { 438 /* forced unmount */ 439 vrele(*vpp); 440 *vpp = NULL; 441 return (EBADF); 442 } 443 } else 444 vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY); 445 } 446 return (-1); 447 } 448 ltype = 0; /* silence gcc warning */ 449 if (cnp->cn_flags & ISDOTDOT) { 450 ltype = VOP_ISLOCKED(dvp); 451 VOP_UNLOCK(dvp, 0); 452 } 453 VI_LOCK(*vpp); 454 CACHE_UNLOCK(); 455 error = vget(*vpp, cnp->cn_lkflags | LK_INTERLOCK, cnp->cn_thread); 456 if (cnp->cn_flags & ISDOTDOT) 457 vn_lock(dvp, ltype | LK_RETRY); 458 if (error) { 459 *vpp = NULL; 460 goto retry; 461 } 462 if ((cnp->cn_flags & ISLASTCN) && 463 (cnp->cn_lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE) { 464 ASSERT_VOP_ELOCKED(*vpp, "cache_lookup"); 465 } 466 return (-1); 467 } 468 469 /* 470 * Add an entry to the cache. 471 */ 472 void 473 cache_enter(dvp, vp, cnp) 474 struct vnode *dvp; 475 struct vnode *vp; 476 struct componentname *cnp; 477 { 478 struct namecache *ncp, *n2; 479 struct nchashhead *ncpp; 480 u_int32_t hash; 481 int hold; 482 int zap; 483 int len; 484 485 CTR3(KTR_VFS, "cache_enter(%p, %p, %s)", dvp, vp, cnp->cn_nameptr); 486 VNASSERT(vp == NULL || (vp->v_iflag & VI_DOOMED) == 0, vp, 487 ("cahe_enter: Adding a doomed vnode")); 488 489 if (!doingcache) 490 return; 491 492 if (cnp->cn_nameptr[0] == '.') { 493 if (cnp->cn_namelen == 1) { 494 return; 495 } 496 /* 497 * For dotdot lookups only cache the v_dd pointer if the 498 * directory has a link back to its parent via v_cache_dst. 499 * Without this an unlinked directory would keep a soft 500 * reference to its parent which could not be NULLd at 501 * cache_purge() time. 502 */ 503 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') { 504 CACHE_LOCK(); 505 if (!TAILQ_EMPTY(&dvp->v_cache_dst)) 506 dvp->v_dd = vp; 507 CACHE_UNLOCK(); 508 return; 509 } 510 } 511 512 hold = 0; 513 zap = 0; 514 515 /* 516 * Calculate the hash key and setup as much of the new 517 * namecache entry as possible before acquiring the lock. 518 */ 519 ncp = cache_alloc(cnp->cn_namelen); 520 ncp->nc_vp = vp; 521 ncp->nc_dvp = dvp; 522 len = ncp->nc_nlen = cnp->cn_namelen; 523 hash = fnv_32_buf(cnp->cn_nameptr, len, FNV1_32_INIT); 524 bcopy(cnp->cn_nameptr, ncp->nc_name, len); 525 hash = fnv_32_buf(&dvp, sizeof(dvp), hash); 526 CACHE_LOCK(); 527 528 /* 529 * See if this vnode or negative entry is already in the cache 530 * with this name. This can happen with concurrent lookups of 531 * the same path name. 532 */ 533 ncpp = NCHHASH(hash); 534 LIST_FOREACH(n2, ncpp, nc_hash) { 535 if (n2->nc_dvp == dvp && 536 n2->nc_nlen == cnp->cn_namelen && 537 !bcmp(n2->nc_name, cnp->cn_nameptr, n2->nc_nlen)) { 538 CACHE_UNLOCK(); 539 cache_free(ncp); 540 return; 541 } 542 } 543 544 numcache++; 545 if (!vp) { 546 numneg++; 547 ncp->nc_flag = cnp->cn_flags & ISWHITEOUT ? NCF_WHITE : 0; 548 } else if (vp->v_type == VDIR) { 549 vp->v_dd = dvp; 550 } else { 551 vp->v_dd = NULL; 552 } 553 554 /* 555 * Insert the new namecache entry into the appropriate chain 556 * within the cache entries table. 557 */ 558 LIST_INSERT_HEAD(ncpp, ncp, nc_hash); 559 if (LIST_EMPTY(&dvp->v_cache_src)) { 560 hold = 1; 561 numcachehv++; 562 } 563 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src); 564 /* 565 * If the entry is "negative", we place it into the 566 * "negative" cache queue, otherwise, we place it into the 567 * destination vnode's cache entries queue. 568 */ 569 if (vp) { 570 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst); 571 } else { 572 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst); 573 } 574 if (numneg * ncnegfactor > numcache) { 575 ncp = TAILQ_FIRST(&ncneg); 576 zap = 1; 577 } 578 if (hold) 579 vhold(dvp); 580 if (zap) 581 cache_zap(ncp); 582 CACHE_UNLOCK(); 583 } 584 585 /* 586 * Name cache initialization, from vfs_init() when we are booting 587 */ 588 static void 589 nchinit(void *dummy __unused) 590 { 591 592 TAILQ_INIT(&ncneg); 593 594 cache_zone_small = uma_zcreate("S VFS Cache", CACHE_ZONE_SMALL, NULL, 595 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT); 596 cache_zone_large = uma_zcreate("L VFS Cache", CACHE_ZONE_LARGE, NULL, 597 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT); 598 599 nchashtbl = hashinit(desiredvnodes * 2, M_VFSCACHE, &nchash); 600 } 601 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL); 602 603 604 /* 605 * Invalidate all entries to a particular vnode. 606 */ 607 void 608 cache_purge(vp) 609 struct vnode *vp; 610 { 611 612 CTR1(KTR_VFS, "cache_purge(%p)", vp); 613 CACHE_LOCK(); 614 while (!LIST_EMPTY(&vp->v_cache_src)) 615 cache_zap(LIST_FIRST(&vp->v_cache_src)); 616 while (!TAILQ_EMPTY(&vp->v_cache_dst)) 617 cache_zap(TAILQ_FIRST(&vp->v_cache_dst)); 618 vp->v_dd = NULL; 619 CACHE_UNLOCK(); 620 } 621 622 /* 623 * Flush all entries referencing a particular filesystem. 624 */ 625 void 626 cache_purgevfs(mp) 627 struct mount *mp; 628 { 629 struct nchashhead *ncpp; 630 struct namecache *ncp, *nnp; 631 632 /* Scan hash tables for applicable entries */ 633 CACHE_LOCK(); 634 for (ncpp = &nchashtbl[nchash]; ncpp >= nchashtbl; ncpp--) { 635 LIST_FOREACH_SAFE(ncp, ncpp, nc_hash, nnp) { 636 if (ncp->nc_dvp->v_mount == mp) 637 cache_zap(ncp); 638 } 639 } 640 CACHE_UNLOCK(); 641 } 642 643 /* 644 * Perform canonical checks and cache lookup and pass on to filesystem 645 * through the vop_cachedlookup only if needed. 646 */ 647 648 int 649 vfs_cache_lookup(ap) 650 struct vop_lookup_args /* { 651 struct vnode *a_dvp; 652 struct vnode **a_vpp; 653 struct componentname *a_cnp; 654 } */ *ap; 655 { 656 struct vnode *dvp; 657 int error; 658 struct vnode **vpp = ap->a_vpp; 659 struct componentname *cnp = ap->a_cnp; 660 struct ucred *cred = cnp->cn_cred; 661 int flags = cnp->cn_flags; 662 struct thread *td = cnp->cn_thread; 663 664 *vpp = NULL; 665 dvp = ap->a_dvp; 666 667 if (dvp->v_type != VDIR) 668 return (ENOTDIR); 669 670 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) && 671 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) 672 return (EROFS); 673 674 error = VOP_ACCESS(dvp, VEXEC, cred, td); 675 if (error) 676 return (error); 677 678 error = cache_lookup(dvp, vpp, cnp); 679 if (error == 0) 680 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp)); 681 if (error == -1) 682 return (0); 683 return (error); 684 } 685 686 687 #ifndef _SYS_SYSPROTO_H_ 688 struct __getcwd_args { 689 u_char *buf; 690 u_int buflen; 691 }; 692 #endif 693 694 /* 695 * XXX All of these sysctls would probably be more productive dead. 696 */ 697 static int disablecwd; 698 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0, 699 "Disable the getcwd syscall"); 700 701 /* Implementation of the getcwd syscall. */ 702 int 703 __getcwd(td, uap) 704 struct thread *td; 705 struct __getcwd_args *uap; 706 { 707 708 return (kern___getcwd(td, uap->buf, UIO_USERSPACE, uap->buflen)); 709 } 710 711 int 712 kern___getcwd(struct thread *td, u_char *buf, enum uio_seg bufseg, u_int buflen) 713 { 714 char *bp, *tmpbuf; 715 struct filedesc *fdp; 716 struct vnode *cdir, *rdir; 717 int error, vfslocked; 718 719 if (disablecwd) 720 return (ENODEV); 721 if (buflen < 2) 722 return (EINVAL); 723 if (buflen > MAXPATHLEN) 724 buflen = MAXPATHLEN; 725 726 tmpbuf = malloc(buflen, M_TEMP, M_WAITOK); 727 fdp = td->td_proc->p_fd; 728 FILEDESC_SLOCK(fdp); 729 cdir = fdp->fd_cdir; 730 VREF(cdir); 731 rdir = fdp->fd_rdir; 732 VREF(rdir); 733 FILEDESC_SUNLOCK(fdp); 734 error = vn_fullpath1(td, cdir, rdir, tmpbuf, &bp, buflen); 735 vfslocked = VFS_LOCK_GIANT(rdir->v_mount); 736 vrele(rdir); 737 VFS_UNLOCK_GIANT(vfslocked); 738 vfslocked = VFS_LOCK_GIANT(cdir->v_mount); 739 vrele(cdir); 740 VFS_UNLOCK_GIANT(vfslocked); 741 742 if (!error) { 743 if (bufseg == UIO_SYSSPACE) 744 bcopy(bp, buf, strlen(bp) + 1); 745 else 746 error = copyout(bp, buf, strlen(bp) + 1); 747 } 748 free(tmpbuf, M_TEMP); 749 return (error); 750 } 751 752 /* 753 * Thus begins the fullpath magic. 754 */ 755 756 #undef STATNODE 757 #define STATNODE(name) \ 758 static u_int name; \ 759 SYSCTL_UINT(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, "") 760 761 static int disablefullpath; 762 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW, &disablefullpath, 0, 763 "Disable the vn_fullpath function"); 764 765 /* These count for kern___getcwd(), too. */ 766 STATNODE(numfullpathcalls); 767 STATNODE(numfullpathfail1); 768 STATNODE(numfullpathfail2); 769 STATNODE(numfullpathfail4); 770 STATNODE(numfullpathfound); 771 772 /* 773 * Retrieve the full filesystem path that correspond to a vnode from the name 774 * cache (if available) 775 */ 776 int 777 vn_fullpath(struct thread *td, struct vnode *vn, char **retbuf, char **freebuf) 778 { 779 char *buf; 780 struct filedesc *fdp; 781 struct vnode *rdir; 782 int error, vfslocked; 783 784 if (disablefullpath) 785 return (ENODEV); 786 if (vn == NULL) 787 return (EINVAL); 788 789 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 790 fdp = td->td_proc->p_fd; 791 FILEDESC_SLOCK(fdp); 792 rdir = fdp->fd_rdir; 793 VREF(rdir); 794 FILEDESC_SUNLOCK(fdp); 795 error = vn_fullpath1(td, vn, rdir, buf, retbuf, MAXPATHLEN); 796 vfslocked = VFS_LOCK_GIANT(rdir->v_mount); 797 vrele(rdir); 798 VFS_UNLOCK_GIANT(vfslocked); 799 800 if (!error) 801 *freebuf = buf; 802 else 803 free(buf, M_TEMP); 804 return (error); 805 } 806 807 /* 808 * This function is similar to vn_fullpath, but it attempts to lookup the 809 * pathname relative to the global root mount point. This is required for the 810 * auditing sub-system, as audited pathnames must be absolute, relative to the 811 * global root mount point. 812 */ 813 int 814 vn_fullpath_global(struct thread *td, struct vnode *vn, 815 char **retbuf, char **freebuf) 816 { 817 char *buf; 818 int error; 819 820 if (disablefullpath) 821 return (ENODEV); 822 if (vn == NULL) 823 return (EINVAL); 824 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 825 error = vn_fullpath1(td, vn, rootvnode, buf, retbuf, MAXPATHLEN); 826 if (!error) 827 *freebuf = buf; 828 else 829 free(buf, M_TEMP); 830 return (error); 831 } 832 833 static int 834 vn_vptocnp(struct vnode **vp, char **bp, char *buf, u_int *buflen) 835 { 836 struct vnode *dvp; 837 int error, vfslocked; 838 839 vhold(*vp); 840 CACHE_UNLOCK(); 841 vfslocked = VFS_LOCK_GIANT((*vp)->v_mount); 842 vn_lock(*vp, LK_SHARED | LK_RETRY); 843 error = VOP_VPTOCNP(*vp, &dvp, buf, buflen); 844 VOP_UNLOCK(*vp, 0); 845 vdrop(*vp); 846 VFS_UNLOCK_GIANT(vfslocked); 847 if (error) { 848 numfullpathfail2++; 849 return (error); 850 } 851 *bp = buf + *buflen; 852 *vp = dvp; 853 CACHE_LOCK(); 854 if ((*vp)->v_iflag & VI_DOOMED) { 855 /* forced unmount */ 856 CACHE_UNLOCK(); 857 vdrop(*vp); 858 return (ENOENT); 859 } 860 vdrop(*vp); 861 862 return (0); 863 } 864 865 /* 866 * The magic behind kern___getcwd() and vn_fullpath(). 867 */ 868 static int 869 vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir, 870 char *buf, char **retbuf, u_int buflen) 871 { 872 char *bp; 873 int error, i, slash_prefixed; 874 struct namecache *ncp; 875 876 buflen--; 877 bp = buf + buflen; 878 *bp = '\0'; 879 error = 0; 880 slash_prefixed = 0; 881 882 CACHE_LOCK(); 883 numfullpathcalls++; 884 if (vp->v_type != VDIR) { 885 ncp = TAILQ_FIRST(&vp->v_cache_dst); 886 if (ncp != NULL) { 887 for (i = ncp->nc_nlen - 1; i >= 0 && bp > buf; i--) 888 *--bp = ncp->nc_name[i]; 889 if (bp == buf) { 890 numfullpathfail4++; 891 CACHE_UNLOCK(); 892 return (ENOMEM); 893 } 894 vp = ncp->nc_dvp; 895 } else { 896 error = vn_vptocnp(&vp, &bp, buf, &buflen); 897 if (error) { 898 return (error); 899 } 900 } 901 *--bp = '/'; 902 buflen--; 903 if (buflen < 0) { 904 numfullpathfail4++; 905 CACHE_UNLOCK(); 906 return (ENOMEM); 907 } 908 slash_prefixed = 1; 909 } 910 while (vp != rdir && vp != rootvnode) { 911 if (vp->v_vflag & VV_ROOT) { 912 if (vp->v_iflag & VI_DOOMED) { /* forced unmount */ 913 CACHE_UNLOCK(); 914 error = EBADF; 915 break; 916 } 917 vp = vp->v_mount->mnt_vnodecovered; 918 continue; 919 } 920 if (vp->v_type != VDIR) { 921 numfullpathfail1++; 922 CACHE_UNLOCK(); 923 error = ENOTDIR; 924 break; 925 } 926 ncp = TAILQ_FIRST(&vp->v_cache_dst); 927 if (ncp != NULL) { 928 MPASS(vp->v_dd == NULL || ncp->nc_dvp == vp->v_dd); 929 buflen -= ncp->nc_nlen - 1; 930 for (i = ncp->nc_nlen - 1; i >= 0 && bp != buf; i--) 931 *--bp = ncp->nc_name[i]; 932 if (bp == buf) { 933 numfullpathfail4++; 934 CACHE_UNLOCK(); 935 error = ENOMEM; 936 break; 937 } 938 vp = ncp->nc_dvp; 939 } else { 940 error = vn_vptocnp(&vp, &bp, buf, &buflen); 941 if (error) { 942 break; 943 } 944 } 945 *--bp = '/'; 946 buflen--; 947 if (buflen < 0) { 948 numfullpathfail4++; 949 CACHE_UNLOCK(); 950 error = ENOMEM; 951 break; 952 } 953 slash_prefixed = 1; 954 } 955 if (error) 956 return (error); 957 if (!slash_prefixed) { 958 if (bp == buf) { 959 numfullpathfail4++; 960 CACHE_UNLOCK(); 961 return (ENOMEM); 962 } else { 963 *--bp = '/'; 964 } 965 } 966 numfullpathfound++; 967 CACHE_UNLOCK(); 968 969 *retbuf = bp; 970 return (0); 971 } 972 973 int 974 vn_commname(struct vnode *vp, char *buf, u_int buflen) 975 { 976 struct namecache *ncp; 977 int l; 978 979 CACHE_LOCK(); 980 ncp = TAILQ_FIRST(&vp->v_cache_dst); 981 if (!ncp) { 982 CACHE_UNLOCK(); 983 return (ENOENT); 984 } 985 l = min(ncp->nc_nlen, buflen - 1); 986 memcpy(buf, ncp->nc_name, l); 987 CACHE_UNLOCK(); 988 buf[l] = '\0'; 989 return (0); 990 } 991