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 is already in the cache with this name. 530 * This can happen with concurrent lookups of the same path 531 * name. 532 */ 533 if (vp) { 534 TAILQ_FOREACH(n2, &vp->v_cache_dst, nc_dst) { 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 } else { 544 TAILQ_FOREACH(n2, &ncneg, nc_dst) { 545 if (n2->nc_nlen == cnp->cn_namelen && 546 !bcmp(n2->nc_name, cnp->cn_nameptr, n2->nc_nlen)) { 547 CACHE_UNLOCK(); 548 cache_free(ncp); 549 return; 550 } 551 } 552 } 553 554 numcache++; 555 if (!vp) { 556 numneg++; 557 ncp->nc_flag = cnp->cn_flags & ISWHITEOUT ? NCF_WHITE : 0; 558 } else if (vp->v_type == VDIR) { 559 vp->v_dd = dvp; 560 } else { 561 vp->v_dd = NULL; 562 } 563 564 /* 565 * Insert the new namecache entry into the appropriate chain 566 * within the cache entries table. 567 */ 568 ncpp = NCHHASH(hash); 569 LIST_INSERT_HEAD(ncpp, ncp, nc_hash); 570 if (LIST_EMPTY(&dvp->v_cache_src)) { 571 hold = 1; 572 numcachehv++; 573 } 574 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src); 575 /* 576 * If the entry is "negative", we place it into the 577 * "negative" cache queue, otherwise, we place it into the 578 * destination vnode's cache entries queue. 579 */ 580 if (vp) { 581 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst); 582 } else { 583 TAILQ_INSERT_TAIL(&ncneg, ncp, nc_dst); 584 } 585 if (numneg * ncnegfactor > numcache) { 586 ncp = TAILQ_FIRST(&ncneg); 587 zap = 1; 588 } 589 if (hold) 590 vhold(dvp); 591 if (zap) 592 cache_zap(ncp); 593 CACHE_UNLOCK(); 594 } 595 596 /* 597 * Name cache initialization, from vfs_init() when we are booting 598 */ 599 static void 600 nchinit(void *dummy __unused) 601 { 602 603 TAILQ_INIT(&ncneg); 604 605 cache_zone_small = uma_zcreate("S VFS Cache", CACHE_ZONE_SMALL, NULL, 606 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT); 607 cache_zone_large = uma_zcreate("L VFS Cache", CACHE_ZONE_LARGE, NULL, 608 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT); 609 610 nchashtbl = hashinit(desiredvnodes * 2, M_VFSCACHE, &nchash); 611 } 612 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL); 613 614 615 /* 616 * Invalidate all entries to a particular vnode. 617 */ 618 void 619 cache_purge(vp) 620 struct vnode *vp; 621 { 622 623 CTR1(KTR_VFS, "cache_purge(%p)", vp); 624 CACHE_LOCK(); 625 while (!LIST_EMPTY(&vp->v_cache_src)) 626 cache_zap(LIST_FIRST(&vp->v_cache_src)); 627 while (!TAILQ_EMPTY(&vp->v_cache_dst)) 628 cache_zap(TAILQ_FIRST(&vp->v_cache_dst)); 629 vp->v_dd = NULL; 630 CACHE_UNLOCK(); 631 } 632 633 /* 634 * Flush all entries referencing a particular filesystem. 635 */ 636 void 637 cache_purgevfs(mp) 638 struct mount *mp; 639 { 640 struct nchashhead *ncpp; 641 struct namecache *ncp, *nnp; 642 643 /* Scan hash tables for applicable entries */ 644 CACHE_LOCK(); 645 for (ncpp = &nchashtbl[nchash]; ncpp >= nchashtbl; ncpp--) { 646 LIST_FOREACH_SAFE(ncp, ncpp, nc_hash, nnp) { 647 if (ncp->nc_dvp->v_mount == mp) 648 cache_zap(ncp); 649 } 650 } 651 CACHE_UNLOCK(); 652 } 653 654 /* 655 * Perform canonical checks and cache lookup and pass on to filesystem 656 * through the vop_cachedlookup only if needed. 657 */ 658 659 int 660 vfs_cache_lookup(ap) 661 struct vop_lookup_args /* { 662 struct vnode *a_dvp; 663 struct vnode **a_vpp; 664 struct componentname *a_cnp; 665 } */ *ap; 666 { 667 struct vnode *dvp; 668 int error; 669 struct vnode **vpp = ap->a_vpp; 670 struct componentname *cnp = ap->a_cnp; 671 struct ucred *cred = cnp->cn_cred; 672 int flags = cnp->cn_flags; 673 struct thread *td = cnp->cn_thread; 674 675 *vpp = NULL; 676 dvp = ap->a_dvp; 677 678 if (dvp->v_type != VDIR) 679 return (ENOTDIR); 680 681 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) && 682 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) 683 return (EROFS); 684 685 error = VOP_ACCESS(dvp, VEXEC, cred, td); 686 if (error) 687 return (error); 688 689 error = cache_lookup(dvp, vpp, cnp); 690 if (error == 0) 691 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp)); 692 if (error == -1) 693 return (0); 694 return (error); 695 } 696 697 698 #ifndef _SYS_SYSPROTO_H_ 699 struct __getcwd_args { 700 u_char *buf; 701 u_int buflen; 702 }; 703 #endif 704 705 /* 706 * XXX All of these sysctls would probably be more productive dead. 707 */ 708 static int disablecwd; 709 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0, 710 "Disable the getcwd syscall"); 711 712 /* Implementation of the getcwd syscall. */ 713 int 714 __getcwd(td, uap) 715 struct thread *td; 716 struct __getcwd_args *uap; 717 { 718 719 return (kern___getcwd(td, uap->buf, UIO_USERSPACE, uap->buflen)); 720 } 721 722 int 723 kern___getcwd(struct thread *td, u_char *buf, enum uio_seg bufseg, u_int buflen) 724 { 725 char *bp, *tmpbuf; 726 struct filedesc *fdp; 727 struct vnode *cdir, *rdir; 728 int error, vfslocked; 729 730 if (disablecwd) 731 return (ENODEV); 732 if (buflen < 2) 733 return (EINVAL); 734 if (buflen > MAXPATHLEN) 735 buflen = MAXPATHLEN; 736 737 tmpbuf = malloc(buflen, M_TEMP, M_WAITOK); 738 fdp = td->td_proc->p_fd; 739 FILEDESC_SLOCK(fdp); 740 cdir = fdp->fd_cdir; 741 VREF(cdir); 742 rdir = fdp->fd_rdir; 743 VREF(rdir); 744 FILEDESC_SUNLOCK(fdp); 745 error = vn_fullpath1(td, cdir, rdir, tmpbuf, &bp, buflen); 746 vfslocked = VFS_LOCK_GIANT(rdir->v_mount); 747 vrele(rdir); 748 VFS_UNLOCK_GIANT(vfslocked); 749 vfslocked = VFS_LOCK_GIANT(cdir->v_mount); 750 vrele(cdir); 751 VFS_UNLOCK_GIANT(vfslocked); 752 753 if (!error) { 754 if (bufseg == UIO_SYSSPACE) 755 bcopy(bp, buf, strlen(bp) + 1); 756 else 757 error = copyout(bp, buf, strlen(bp) + 1); 758 } 759 free(tmpbuf, M_TEMP); 760 return (error); 761 } 762 763 /* 764 * Thus begins the fullpath magic. 765 */ 766 767 #undef STATNODE 768 #define STATNODE(name) \ 769 static u_int name; \ 770 SYSCTL_UINT(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, "") 771 772 static int disablefullpath; 773 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW, &disablefullpath, 0, 774 "Disable the vn_fullpath function"); 775 776 /* These count for kern___getcwd(), too. */ 777 STATNODE(numfullpathcalls); 778 STATNODE(numfullpathfail1); 779 STATNODE(numfullpathfail2); 780 STATNODE(numfullpathfail4); 781 STATNODE(numfullpathfound); 782 783 /* 784 * Retrieve the full filesystem path that correspond to a vnode from the name 785 * cache (if available) 786 */ 787 int 788 vn_fullpath(struct thread *td, struct vnode *vn, char **retbuf, char **freebuf) 789 { 790 char *buf; 791 struct filedesc *fdp; 792 struct vnode *rdir; 793 int error, vfslocked; 794 795 if (disablefullpath) 796 return (ENODEV); 797 if (vn == NULL) 798 return (EINVAL); 799 800 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 801 fdp = td->td_proc->p_fd; 802 FILEDESC_SLOCK(fdp); 803 rdir = fdp->fd_rdir; 804 VREF(rdir); 805 FILEDESC_SUNLOCK(fdp); 806 error = vn_fullpath1(td, vn, rdir, buf, retbuf, MAXPATHLEN); 807 vfslocked = VFS_LOCK_GIANT(rdir->v_mount); 808 vrele(rdir); 809 VFS_UNLOCK_GIANT(vfslocked); 810 811 if (!error) 812 *freebuf = buf; 813 else 814 free(buf, M_TEMP); 815 return (error); 816 } 817 818 /* 819 * This function is similar to vn_fullpath, but it attempts to lookup the 820 * pathname relative to the global root mount point. This is required for the 821 * auditing sub-system, as audited pathnames must be absolute, relative to the 822 * global root mount point. 823 */ 824 int 825 vn_fullpath_global(struct thread *td, struct vnode *vn, 826 char **retbuf, char **freebuf) 827 { 828 char *buf; 829 int error; 830 831 if (disablefullpath) 832 return (ENODEV); 833 if (vn == NULL) 834 return (EINVAL); 835 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 836 error = vn_fullpath1(td, vn, rootvnode, buf, retbuf, MAXPATHLEN); 837 if (!error) 838 *freebuf = buf; 839 else 840 free(buf, M_TEMP); 841 return (error); 842 } 843 844 static int 845 vn_vptocnp(struct vnode **vp, char **bp, char *buf, u_int *buflen) 846 { 847 struct vnode *dvp; 848 int error, vfslocked; 849 850 vhold(*vp); 851 CACHE_UNLOCK(); 852 vfslocked = VFS_LOCK_GIANT((*vp)->v_mount); 853 vn_lock(*vp, LK_SHARED | LK_RETRY); 854 vdrop(*vp); 855 error = VOP_VPTOCNP(*vp, &dvp, buf, buflen); 856 VOP_UNLOCK(*vp, 0); 857 VFS_UNLOCK_GIANT(vfslocked); 858 if (error) { 859 numfullpathfail2++; 860 return (error); 861 } 862 *bp = buf + *buflen; 863 *vp = dvp; 864 CACHE_LOCK(); 865 if ((*vp)->v_iflag & VI_DOOMED) { 866 /* forced unmount */ 867 CACHE_UNLOCK(); 868 vdrop(*vp); 869 return (ENOENT); 870 } 871 vdrop(*vp); 872 873 return (0); 874 } 875 876 /* 877 * The magic behind kern___getcwd() and vn_fullpath(). 878 */ 879 static int 880 vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir, 881 char *buf, char **retbuf, u_int buflen) 882 { 883 char *bp; 884 int error, i, slash_prefixed; 885 struct namecache *ncp; 886 887 buflen--; 888 bp = buf + buflen; 889 *bp = '\0'; 890 error = 0; 891 slash_prefixed = 0; 892 893 CACHE_LOCK(); 894 numfullpathcalls++; 895 if (vp->v_type != VDIR) { 896 ncp = TAILQ_FIRST(&vp->v_cache_dst); 897 if (ncp != NULL) { 898 for (i = ncp->nc_nlen - 1; i >= 0 && bp > buf; i--) 899 *--bp = ncp->nc_name[i]; 900 if (bp == buf) { 901 numfullpathfail4++; 902 CACHE_UNLOCK(); 903 return (ENOMEM); 904 } 905 vp = ncp->nc_dvp; 906 } else { 907 error = vn_vptocnp(&vp, &bp, buf, &buflen); 908 if (error) { 909 return (error); 910 } 911 } 912 *--bp = '/'; 913 buflen--; 914 if (buflen < 0) { 915 numfullpathfail4++; 916 CACHE_UNLOCK(); 917 return (ENOMEM); 918 } 919 slash_prefixed = 1; 920 } 921 while (vp != rdir && vp != rootvnode) { 922 if (vp->v_vflag & VV_ROOT) { 923 if (vp->v_iflag & VI_DOOMED) { /* forced unmount */ 924 CACHE_UNLOCK(); 925 error = EBADF; 926 break; 927 } 928 vp = vp->v_mount->mnt_vnodecovered; 929 continue; 930 } 931 if (vp->v_type != VDIR) { 932 numfullpathfail1++; 933 CACHE_UNLOCK(); 934 error = ENOTDIR; 935 break; 936 } 937 ncp = TAILQ_FIRST(&vp->v_cache_dst); 938 if (ncp != NULL) { 939 MPASS(ncp->nc_dvp == vp->v_dd); 940 buflen -= ncp->nc_nlen - 1; 941 for (i = ncp->nc_nlen - 1; i >= 0 && bp != buf; i--) 942 *--bp = ncp->nc_name[i]; 943 if (bp == buf) { 944 numfullpathfail4++; 945 CACHE_UNLOCK(); 946 error = ENOMEM; 947 break; 948 } 949 vp = ncp->nc_dvp; 950 } else { 951 error = vn_vptocnp(&vp, &bp, buf, &buflen); 952 if (error) { 953 break; 954 } 955 } 956 *--bp = '/'; 957 buflen--; 958 if (buflen < 0) { 959 numfullpathfail4++; 960 CACHE_UNLOCK(); 961 error = ENOMEM; 962 break; 963 } 964 slash_prefixed = 1; 965 } 966 if (error) 967 return (error); 968 if (!slash_prefixed) { 969 if (bp == buf) { 970 numfullpathfail4++; 971 CACHE_UNLOCK(); 972 return (ENOMEM); 973 } else { 974 *--bp = '/'; 975 } 976 } 977 numfullpathfound++; 978 CACHE_UNLOCK(); 979 980 *retbuf = bp; 981 return (0); 982 } 983 984 int 985 vn_commname(struct vnode *vp, char *buf, u_int buflen) 986 { 987 struct namecache *ncp; 988 int l; 989 990 CACHE_LOCK(); 991 ncp = TAILQ_FIRST(&vp->v_cache_dst); 992 if (!ncp) { 993 CACHE_UNLOCK(); 994 return (ENOENT); 995 } 996 l = min(ncp->nc_nlen, buflen - 1); 997 memcpy(buf, ncp->nc_name, l); 998 CACHE_UNLOCK(); 999 buf[l] = '\0'; 1000 return (0); 1001 } 1002