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. 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 "opt_ktrace.h" 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/counter.h> 43 #include <sys/filedesc.h> 44 #include <sys/fnv_hash.h> 45 #include <sys/kernel.h> 46 #include <sys/lock.h> 47 #include <sys/malloc.h> 48 #include <sys/fcntl.h> 49 #include <sys/mount.h> 50 #include <sys/namei.h> 51 #include <sys/proc.h> 52 #include <sys/rwlock.h> 53 #include <sys/sdt.h> 54 #include <sys/smp.h> 55 #include <sys/syscallsubr.h> 56 #include <sys/sysctl.h> 57 #include <sys/sysproto.h> 58 #include <sys/vnode.h> 59 #ifdef KTRACE 60 #include <sys/ktrace.h> 61 #endif 62 63 #include <vm/uma.h> 64 65 SDT_PROVIDER_DECLARE(vfs); 66 SDT_PROBE_DEFINE3(vfs, namecache, enter, done, "struct vnode *", "char *", 67 "struct vnode *"); 68 SDT_PROBE_DEFINE2(vfs, namecache, enter_negative, done, "struct vnode *", 69 "char *"); 70 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, entry, "struct vnode *"); 71 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, hit, "struct vnode *", 72 "char *", "struct vnode *"); 73 SDT_PROBE_DEFINE1(vfs, namecache, fullpath, miss, "struct vnode *"); 74 SDT_PROBE_DEFINE3(vfs, namecache, fullpath, return, "int", 75 "struct vnode *", "char *"); 76 SDT_PROBE_DEFINE3(vfs, namecache, lookup, hit, "struct vnode *", "char *", 77 "struct vnode *"); 78 SDT_PROBE_DEFINE2(vfs, namecache, lookup, hit__negative, 79 "struct vnode *", "char *"); 80 SDT_PROBE_DEFINE2(vfs, namecache, lookup, miss, "struct vnode *", 81 "char *"); 82 SDT_PROBE_DEFINE1(vfs, namecache, purge, done, "struct vnode *"); 83 SDT_PROBE_DEFINE1(vfs, namecache, purge_negative, done, "struct vnode *"); 84 SDT_PROBE_DEFINE1(vfs, namecache, purgevfs, done, "struct mount *"); 85 SDT_PROBE_DEFINE3(vfs, namecache, zap, done, "struct vnode *", "char *", 86 "struct vnode *"); 87 SDT_PROBE_DEFINE3(vfs, namecache, zap_negative, done, "struct vnode *", 88 "char *", "int"); 89 SDT_PROBE_DEFINE3(vfs, namecache, shrink_negative, done, "struct vnode *", 90 "char *", "int"); 91 92 /* 93 * This structure describes the elements in the cache of recent 94 * names looked up by namei. 95 */ 96 97 struct namecache { 98 LIST_ENTRY(namecache) nc_hash; /* hash chain */ 99 LIST_ENTRY(namecache) nc_src; /* source vnode list */ 100 TAILQ_ENTRY(namecache) nc_dst; /* destination vnode list */ 101 struct vnode *nc_dvp; /* vnode of parent of name */ 102 union { 103 struct vnode *nu_vp; /* vnode the name refers to */ 104 u_int nu_neghits; /* negative entry hits */ 105 } n_un; 106 u_char nc_flag; /* flag bits */ 107 u_char nc_nlen; /* length of name */ 108 char nc_name[0]; /* segment name + nul */ 109 }; 110 111 /* 112 * struct namecache_ts repeats struct namecache layout up to the 113 * nc_nlen member. 114 * struct namecache_ts is used in place of struct namecache when time(s) need 115 * to be stored. The nc_dotdottime field is used when a cache entry is mapping 116 * both a non-dotdot directory name plus dotdot for the directory's 117 * parent. 118 */ 119 struct namecache_ts { 120 LIST_ENTRY(namecache) nc_hash; /* hash chain */ 121 LIST_ENTRY(namecache) nc_src; /* source vnode list */ 122 TAILQ_ENTRY(namecache) nc_dst; /* destination vnode list */ 123 struct vnode *nc_dvp; /* vnode of parent of name */ 124 union { 125 struct vnode *nu_vp; /* vnode the name refers to */ 126 u_int nu_neghits; /* negative entry hits */ 127 } n_un; 128 u_char nc_flag; /* flag bits */ 129 u_char nc_nlen; /* length of name */ 130 struct timespec nc_time; /* timespec provided by fs */ 131 struct timespec nc_dotdottime; /* dotdot timespec provided by fs */ 132 int nc_ticks; /* ticks value when entry was added */ 133 char nc_name[0]; /* segment name + nul */ 134 }; 135 136 #define nc_vp n_un.nu_vp 137 #define nc_neghits n_un.nu_neghits 138 139 /* 140 * Flags in namecache.nc_flag 141 */ 142 #define NCF_WHITE 0x01 143 #define NCF_ISDOTDOT 0x02 144 #define NCF_TS 0x04 145 #define NCF_DTS 0x08 146 #define NCF_DVDROP 0x10 147 #define NCF_NEGATIVE 0x20 148 #define NCF_HOTNEGATIVE 0x40 149 150 /* 151 * Name caching works as follows: 152 * 153 * Names found by directory scans are retained in a cache 154 * for future reference. It is managed LRU, so frequently 155 * used names will hang around. Cache is indexed by hash value 156 * obtained from (vp, name) where vp refers to the directory 157 * containing name. 158 * 159 * If it is a "negative" entry, (i.e. for a name that is known NOT to 160 * exist) the vnode pointer will be NULL. 161 * 162 * Upon reaching the last segment of a path, if the reference 163 * is for DELETE, or NOCACHE is set (rewrite), and the 164 * name is located in the cache, it will be dropped. 165 * 166 * These locks are used (in the order in which they can be taken): 167 * NAME TYPE ROLE 168 * vnodelock mtx vnode lists and v_cache_dd field protection 169 * bucketlock rwlock for access to given set of hash buckets 170 * neglist mtx negative entry LRU management 171 * 172 * Additionally, ncneg_shrink_lock mtx is used to have at most one thread 173 * shrinking the LRU list. 174 * 175 * It is legal to take multiple vnodelock and bucketlock locks. The locking 176 * order is lower address first. Both are recursive. 177 * 178 * "." lookups are lockless. 179 * 180 * ".." and vnode -> name lookups require vnodelock. 181 * 182 * name -> vnode lookup requires the relevant bucketlock to be held for reading. 183 * 184 * Insertions and removals of entries require involved vnodes and bucketlocks 185 * to be write-locked to prevent other threads from seeing the entry. 186 * 187 * Some lookups result in removal of the found entry (e.g. getting rid of a 188 * negative entry with the intent to create a positive one), which poses a 189 * problem when multiple threads reach the state. Similarly, two different 190 * threads can purge two different vnodes and try to remove the same name. 191 * 192 * If the already held vnode lock is lower than the second required lock, we 193 * can just take the other lock. However, in the opposite case, this could 194 * deadlock. As such, this is resolved by trylocking and if that fails unlocking 195 * the first node, locking everything in order and revalidating the state. 196 */ 197 198 /* 199 * Structures associated with name caching. 200 */ 201 #define NCHHASH(hash) \ 202 (&nchashtbl[(hash) & nchash]) 203 static LIST_HEAD(nchashhead, namecache) *nchashtbl; /* Hash Table */ 204 static u_long nchash; /* size of hash table */ 205 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0, 206 "Size of namecache hash table"); 207 static u_long ncnegfactor = 16; /* ratio of negative entries */ 208 SYSCTL_ULONG(_vfs, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0, 209 "Ratio of negative namecache entries"); 210 static u_long numneg; /* number of negative entries allocated */ 211 SYSCTL_ULONG(_debug, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0, 212 "Number of negative entries in namecache"); 213 static u_long numcache; /* number of cache entries allocated */ 214 SYSCTL_ULONG(_debug, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0, 215 "Number of namecache entries"); 216 static u_long numcachehv; /* number of cache entries with vnodes held */ 217 SYSCTL_ULONG(_debug, OID_AUTO, numcachehv, CTLFLAG_RD, &numcachehv, 0, 218 "Number of namecache entries with vnodes held"); 219 u_int ncsizefactor = 2; 220 SYSCTL_UINT(_vfs, OID_AUTO, ncsizefactor, CTLFLAG_RW, &ncsizefactor, 0, 221 "Size factor for namecache"); 222 static u_int ncpurgeminvnodes; 223 SYSCTL_UINT(_vfs, OID_AUTO, ncpurgeminvnodes, CTLFLAG_RW, &ncpurgeminvnodes, 0, 224 "Number of vnodes below which purgevfs ignores the request"); 225 static u_int ncneghitsrequeue = 8; 226 SYSCTL_UINT(_vfs, OID_AUTO, ncneghitsrequeue, CTLFLAG_RW, &ncneghitsrequeue, 0, 227 "Number of hits to requeue a negative entry in the LRU list"); 228 229 struct nchstats nchstats; /* cache effectiveness statistics */ 230 231 static struct mtx ncneg_shrink_lock; 232 MTX_SYSINIT(vfscache_shrink_neg, &ncneg_shrink_lock, "Name Cache shrink neg", 233 MTX_DEF); 234 235 struct neglist { 236 struct mtx nl_lock; 237 TAILQ_HEAD(, namecache) nl_list; 238 } __aligned(CACHE_LINE_SIZE); 239 240 static struct neglist *neglists; 241 static struct neglist ncneg_hot; 242 243 static int shrink_list_turn; 244 245 static u_int numneglists; 246 static inline struct neglist * 247 NCP2NEGLIST(struct namecache *ncp) 248 { 249 250 return (&neglists[(((uintptr_t)(ncp) >> 8) % numneglists)]); 251 } 252 253 static u_int numbucketlocks; 254 static struct rwlock_padalign *bucketlocks; 255 #define HASH2BUCKETLOCK(hash) \ 256 ((struct rwlock *)(&bucketlocks[((hash) % numbucketlocks)])) 257 258 static u_int numvnodelocks; 259 static struct mtx *vnodelocks; 260 static inline struct mtx * 261 VP2VNODELOCK(struct vnode *vp) 262 { 263 struct mtx *vlp; 264 265 if (vp == NULL) 266 return (NULL); 267 vlp = &vnodelocks[(((uintptr_t)(vp) >> 8) % numvnodelocks)]; 268 return (vlp); 269 } 270 271 /* 272 * UMA zones for the VFS cache. 273 * 274 * The small cache is used for entries with short names, which are the 275 * most common. The large cache is used for entries which are too big to 276 * fit in the small cache. 277 */ 278 static uma_zone_t cache_zone_small; 279 static uma_zone_t cache_zone_small_ts; 280 static uma_zone_t cache_zone_large; 281 static uma_zone_t cache_zone_large_ts; 282 283 #define CACHE_PATH_CUTOFF 35 284 285 static struct namecache * 286 cache_alloc(int len, int ts) 287 { 288 289 if (len > CACHE_PATH_CUTOFF) { 290 if (ts) 291 return (uma_zalloc(cache_zone_large_ts, M_WAITOK)); 292 else 293 return (uma_zalloc(cache_zone_large, M_WAITOK)); 294 } 295 if (ts) 296 return (uma_zalloc(cache_zone_small_ts, M_WAITOK)); 297 else 298 return (uma_zalloc(cache_zone_small, M_WAITOK)); 299 } 300 301 static void 302 cache_free(struct namecache *ncp) 303 { 304 int ts; 305 306 if (ncp == NULL) 307 return; 308 ts = ncp->nc_flag & NCF_TS; 309 if ((ncp->nc_flag & NCF_DVDROP) != 0) 310 vdrop(ncp->nc_dvp); 311 if (ncp->nc_nlen <= CACHE_PATH_CUTOFF) { 312 if (ts) 313 uma_zfree(cache_zone_small_ts, ncp); 314 else 315 uma_zfree(cache_zone_small, ncp); 316 } else if (ts) 317 uma_zfree(cache_zone_large_ts, ncp); 318 else 319 uma_zfree(cache_zone_large, ncp); 320 } 321 322 static char * 323 nc_get_name(struct namecache *ncp) 324 { 325 struct namecache_ts *ncp_ts; 326 327 if ((ncp->nc_flag & NCF_TS) == 0) 328 return (ncp->nc_name); 329 ncp_ts = (struct namecache_ts *)ncp; 330 return (ncp_ts->nc_name); 331 } 332 333 static void 334 cache_out_ts(struct namecache *ncp, struct timespec *tsp, int *ticksp) 335 { 336 337 KASSERT((ncp->nc_flag & NCF_TS) != 0 || 338 (tsp == NULL && ticksp == NULL), 339 ("No NCF_TS")); 340 341 if (tsp != NULL) 342 *tsp = ((struct namecache_ts *)ncp)->nc_time; 343 if (ticksp != NULL) 344 *ticksp = ((struct namecache_ts *)ncp)->nc_ticks; 345 } 346 347 static int doingcache = 1; /* 1 => enable the cache */ 348 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0, 349 "VFS namecache enabled"); 350 351 /* Export size information to userland */ 352 SYSCTL_INT(_debug_sizeof, OID_AUTO, namecache, CTLFLAG_RD, SYSCTL_NULL_INT_PTR, 353 sizeof(struct namecache), "sizeof(struct namecache)"); 354 355 /* 356 * The new name cache statistics 357 */ 358 static SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0, 359 "Name cache statistics"); 360 #define STATNODE_ULONG(name, descr) \ 361 SYSCTL_ULONG(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, 0, descr); 362 #define STATNODE_COUNTER(name, descr) \ 363 static counter_u64_t name; \ 364 SYSCTL_COUNTER_U64(_vfs_cache, OID_AUTO, name, CTLFLAG_RD, &name, descr); 365 STATNODE_ULONG(numneg, "Number of negative cache entries"); 366 STATNODE_ULONG(numcache, "Number of cache entries"); 367 STATNODE_COUNTER(numcalls, "Number of cache lookups"); 368 STATNODE_COUNTER(dothits, "Number of '.' hits"); 369 STATNODE_COUNTER(dotdothits, "Number of '..' hits"); 370 STATNODE_COUNTER(numchecks, "Number of checks in lookup"); 371 STATNODE_COUNTER(nummiss, "Number of cache misses"); 372 STATNODE_COUNTER(nummisszap, "Number of cache misses we do not want to cache"); 373 STATNODE_COUNTER(numposzaps, 374 "Number of cache hits (positive) we do not want to cache"); 375 STATNODE_COUNTER(numposhits, "Number of cache hits (positive)"); 376 STATNODE_COUNTER(numnegzaps, 377 "Number of cache hits (negative) we do not want to cache"); 378 STATNODE_COUNTER(numneghits, "Number of cache hits (negative)"); 379 /* These count for kern___getcwd(), too. */ 380 STATNODE_COUNTER(numfullpathcalls, "Number of fullpath search calls"); 381 STATNODE_COUNTER(numfullpathfail1, "Number of fullpath search errors (ENOTDIR)"); 382 STATNODE_COUNTER(numfullpathfail2, 383 "Number of fullpath search errors (VOP_VPTOCNP failures)"); 384 STATNODE_COUNTER(numfullpathfail4, "Number of fullpath search errors (ENOMEM)"); 385 STATNODE_COUNTER(numfullpathfound, "Number of successful fullpath calls"); 386 static long zap_and_exit_bucket_fail; STATNODE_ULONG(zap_and_exit_bucket_fail, 387 "Number of times zap_and_exit failed to lock"); 388 static long cache_lock_vnodes_cel_3_failures; 389 STATNODE_ULONG(cache_lock_vnodes_cel_3_failures, 390 "Number of times 3-way vnode locking failed"); 391 392 static void cache_zap_locked(struct namecache *ncp, bool neg_locked); 393 static int vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir, 394 char *buf, char **retbuf, u_int buflen); 395 396 static MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries"); 397 398 static int cache_yield; 399 SYSCTL_INT(_vfs_cache, OID_AUTO, yield, CTLFLAG_RD, &cache_yield, 0, 400 "Number of times cache called yield"); 401 402 static void 403 cache_maybe_yield(void) 404 { 405 406 if (should_yield()) { 407 cache_yield++; 408 kern_yield(PRI_USER); 409 } 410 } 411 412 static inline void 413 cache_assert_vlp_locked(struct mtx *vlp) 414 { 415 416 if (vlp != NULL) 417 mtx_assert(vlp, MA_OWNED); 418 } 419 420 static inline void 421 cache_assert_vnode_locked(struct vnode *vp) 422 { 423 struct mtx *vlp; 424 425 vlp = VP2VNODELOCK(vp); 426 cache_assert_vlp_locked(vlp); 427 } 428 429 static uint32_t 430 cache_get_hash(char *name, u_char len, struct vnode *dvp) 431 { 432 uint32_t hash; 433 434 hash = fnv_32_buf(name, len, FNV1_32_INIT); 435 hash = fnv_32_buf(&dvp, sizeof(dvp), hash); 436 return (hash); 437 } 438 439 static inline struct rwlock * 440 NCP2BUCKETLOCK(struct namecache *ncp) 441 { 442 uint32_t hash; 443 444 hash = cache_get_hash(nc_get_name(ncp), ncp->nc_nlen, ncp->nc_dvp); 445 return (HASH2BUCKETLOCK(hash)); 446 } 447 448 #ifdef INVARIANTS 449 static void 450 cache_assert_bucket_locked(struct namecache *ncp, int mode) 451 { 452 struct rwlock *blp; 453 454 blp = NCP2BUCKETLOCK(ncp); 455 rw_assert(blp, mode); 456 } 457 #else 458 #define cache_assert_bucket_locked(x, y) do { } while (0) 459 #endif 460 461 #define cache_sort(x, y) _cache_sort((void **)(x), (void **)(y)) 462 static void 463 _cache_sort(void **p1, void **p2) 464 { 465 void *tmp; 466 467 if (*p1 > *p2) { 468 tmp = *p2; 469 *p2 = *p1; 470 *p1 = tmp; 471 } 472 } 473 474 static void 475 cache_lock_all_buckets(void) 476 { 477 u_int i; 478 479 for (i = 0; i < numbucketlocks; i++) 480 rw_wlock(&bucketlocks[i]); 481 } 482 483 static void 484 cache_unlock_all_buckets(void) 485 { 486 u_int i; 487 488 for (i = 0; i < numbucketlocks; i++) 489 rw_wunlock(&bucketlocks[i]); 490 } 491 492 static void 493 cache_lock_all_vnodes(void) 494 { 495 u_int i; 496 497 for (i = 0; i < numvnodelocks; i++) 498 mtx_lock(&vnodelocks[i]); 499 } 500 501 static void 502 cache_unlock_all_vnodes(void) 503 { 504 u_int i; 505 506 for (i = 0; i < numvnodelocks; i++) 507 mtx_unlock(&vnodelocks[i]); 508 } 509 510 static int 511 cache_trylock_vnodes(struct mtx *vlp1, struct mtx *vlp2) 512 { 513 514 cache_sort(&vlp1, &vlp2); 515 MPASS(vlp2 != NULL); 516 517 if (vlp1 != NULL) { 518 if (!mtx_trylock(vlp1)) 519 return (EAGAIN); 520 } 521 if (!mtx_trylock(vlp2)) { 522 if (vlp1 != NULL) 523 mtx_unlock(vlp1); 524 return (EAGAIN); 525 } 526 527 return (0); 528 } 529 530 static void 531 cache_unlock_vnodes(struct mtx *vlp1, struct mtx *vlp2) 532 { 533 534 MPASS(vlp1 != NULL || vlp2 != NULL); 535 536 if (vlp1 != NULL) 537 mtx_unlock(vlp1); 538 if (vlp2 != NULL) 539 mtx_unlock(vlp2); 540 } 541 542 static int 543 sysctl_nchstats(SYSCTL_HANDLER_ARGS) 544 { 545 struct nchstats snap; 546 547 if (req->oldptr == NULL) 548 return (SYSCTL_OUT(req, 0, sizeof(snap))); 549 550 snap = nchstats; 551 snap.ncs_goodhits = counter_u64_fetch(numposhits); 552 snap.ncs_neghits = counter_u64_fetch(numneghits); 553 snap.ncs_badhits = counter_u64_fetch(numposzaps) + 554 counter_u64_fetch(numnegzaps); 555 snap.ncs_miss = counter_u64_fetch(nummisszap) + 556 counter_u64_fetch(nummiss); 557 558 return (SYSCTL_OUT(req, &snap, sizeof(snap))); 559 } 560 SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE | CTLFLAG_RD | 561 CTLFLAG_MPSAFE, 0, 0, sysctl_nchstats, "LU", 562 "VFS cache effectiveness statistics"); 563 564 #ifdef DIAGNOSTIC 565 /* 566 * Grab an atomic snapshot of the name cache hash chain lengths 567 */ 568 static SYSCTL_NODE(_debug, OID_AUTO, hashstat, CTLFLAG_RW, NULL, 569 "hash table stats"); 570 571 static int 572 sysctl_debug_hashstat_rawnchash(SYSCTL_HANDLER_ARGS) 573 { 574 struct nchashhead *ncpp; 575 struct namecache *ncp; 576 int i, error, n_nchash, *cntbuf; 577 578 retry: 579 n_nchash = nchash + 1; /* nchash is max index, not count */ 580 if (req->oldptr == NULL) 581 return SYSCTL_OUT(req, 0, n_nchash * sizeof(int)); 582 cntbuf = malloc(n_nchash * sizeof(int), M_TEMP, M_ZERO | M_WAITOK); 583 cache_lock_all_buckets(); 584 if (n_nchash != nchash + 1) { 585 cache_unlock_all_buckets(); 586 free(cntbuf, M_TEMP); 587 goto retry; 588 } 589 /* Scan hash tables counting entries */ 590 for (ncpp = nchashtbl, i = 0; i < n_nchash; ncpp++, i++) 591 LIST_FOREACH(ncp, ncpp, nc_hash) 592 cntbuf[i]++; 593 cache_unlock_all_buckets(); 594 for (error = 0, i = 0; i < n_nchash; i++) 595 if ((error = SYSCTL_OUT(req, &cntbuf[i], sizeof(int))) != 0) 596 break; 597 free(cntbuf, M_TEMP); 598 return (error); 599 } 600 SYSCTL_PROC(_debug_hashstat, OID_AUTO, rawnchash, CTLTYPE_INT|CTLFLAG_RD| 601 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_rawnchash, "S,int", 602 "nchash chain lengths"); 603 604 static int 605 sysctl_debug_hashstat_nchash(SYSCTL_HANDLER_ARGS) 606 { 607 int error; 608 struct nchashhead *ncpp; 609 struct namecache *ncp; 610 int n_nchash; 611 int count, maxlength, used, pct; 612 613 if (!req->oldptr) 614 return SYSCTL_OUT(req, 0, 4 * sizeof(int)); 615 616 cache_lock_all_buckets(); 617 n_nchash = nchash + 1; /* nchash is max index, not count */ 618 used = 0; 619 maxlength = 0; 620 621 /* Scan hash tables for applicable entries */ 622 for (ncpp = nchashtbl; n_nchash > 0; n_nchash--, ncpp++) { 623 count = 0; 624 LIST_FOREACH(ncp, ncpp, nc_hash) { 625 count++; 626 } 627 if (count) 628 used++; 629 if (maxlength < count) 630 maxlength = count; 631 } 632 n_nchash = nchash + 1; 633 cache_unlock_all_buckets(); 634 pct = (used * 100) / (n_nchash / 100); 635 error = SYSCTL_OUT(req, &n_nchash, sizeof(n_nchash)); 636 if (error) 637 return (error); 638 error = SYSCTL_OUT(req, &used, sizeof(used)); 639 if (error) 640 return (error); 641 error = SYSCTL_OUT(req, &maxlength, sizeof(maxlength)); 642 if (error) 643 return (error); 644 error = SYSCTL_OUT(req, &pct, sizeof(pct)); 645 if (error) 646 return (error); 647 return (0); 648 } 649 SYSCTL_PROC(_debug_hashstat, OID_AUTO, nchash, CTLTYPE_INT|CTLFLAG_RD| 650 CTLFLAG_MPSAFE, 0, 0, sysctl_debug_hashstat_nchash, "I", 651 "nchash statistics (number of total/used buckets, maximum chain length, usage percentage)"); 652 #endif 653 654 /* 655 * Negative entries management 656 * 657 * A variation of LRU scheme is used. New entries are hashed into one of 658 * numneglists cold lists. Entries get promoted to the hot list on first hit. 659 * Partial LRU for the hot list is maintained by requeueing them every 660 * ncneghitsrequeue hits. 661 * 662 * The shrinker will demote hot list head and evict from the cold list in a 663 * round-robin manner. 664 */ 665 static void 666 cache_negative_hit(struct namecache *ncp) 667 { 668 struct neglist *neglist; 669 u_int hits; 670 671 MPASS(ncp->nc_flag & NCF_NEGATIVE); 672 hits = atomic_fetchadd_int(&ncp->nc_neghits, 1); 673 if (ncp->nc_flag & NCF_HOTNEGATIVE) { 674 if ((hits % ncneghitsrequeue) != 0) 675 return; 676 mtx_lock(&ncneg_hot.nl_lock); 677 if (ncp->nc_flag & NCF_HOTNEGATIVE) { 678 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst); 679 TAILQ_INSERT_TAIL(&ncneg_hot.nl_list, ncp, nc_dst); 680 mtx_unlock(&ncneg_hot.nl_lock); 681 return; 682 } 683 /* 684 * The shrinker cleared the flag and removed the entry from 685 * the hot list. Put it back. 686 */ 687 } else { 688 mtx_lock(&ncneg_hot.nl_lock); 689 } 690 neglist = NCP2NEGLIST(ncp); 691 mtx_lock(&neglist->nl_lock); 692 if (!(ncp->nc_flag & NCF_HOTNEGATIVE)) { 693 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst); 694 TAILQ_INSERT_TAIL(&ncneg_hot.nl_list, ncp, nc_dst); 695 ncp->nc_flag |= NCF_HOTNEGATIVE; 696 } 697 mtx_unlock(&neglist->nl_lock); 698 mtx_unlock(&ncneg_hot.nl_lock); 699 } 700 701 static void 702 cache_negative_insert(struct namecache *ncp, bool neg_locked) 703 { 704 struct neglist *neglist; 705 706 MPASS(ncp->nc_flag & NCF_NEGATIVE); 707 cache_assert_bucket_locked(ncp, RA_WLOCKED); 708 neglist = NCP2NEGLIST(ncp); 709 if (!neg_locked) { 710 mtx_lock(&neglist->nl_lock); 711 } else { 712 mtx_assert(&neglist->nl_lock, MA_OWNED); 713 } 714 TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst); 715 if (!neg_locked) 716 mtx_unlock(&neglist->nl_lock); 717 atomic_add_rel_long(&numneg, 1); 718 } 719 720 static void 721 cache_negative_remove(struct namecache *ncp, bool neg_locked) 722 { 723 struct neglist *neglist; 724 bool hot_locked = false; 725 bool list_locked = false; 726 727 MPASS(ncp->nc_flag & NCF_NEGATIVE); 728 cache_assert_bucket_locked(ncp, RA_WLOCKED); 729 neglist = NCP2NEGLIST(ncp); 730 if (!neg_locked) { 731 if (ncp->nc_flag & NCF_HOTNEGATIVE) { 732 hot_locked = true; 733 mtx_lock(&ncneg_hot.nl_lock); 734 if (!(ncp->nc_flag & NCF_HOTNEGATIVE)) { 735 list_locked = true; 736 mtx_lock(&neglist->nl_lock); 737 } 738 } else { 739 list_locked = true; 740 mtx_lock(&neglist->nl_lock); 741 } 742 } else { 743 mtx_assert(&neglist->nl_lock, MA_OWNED); 744 mtx_assert(&ncneg_hot.nl_lock, MA_OWNED); 745 } 746 if (ncp->nc_flag & NCF_HOTNEGATIVE) { 747 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst); 748 } else { 749 TAILQ_REMOVE(&neglist->nl_list, ncp, nc_dst); 750 } 751 if (list_locked) 752 mtx_unlock(&neglist->nl_lock); 753 if (hot_locked) 754 mtx_unlock(&ncneg_hot.nl_lock); 755 atomic_subtract_rel_long(&numneg, 1); 756 } 757 758 static void 759 cache_negative_shrink_select(int start, struct namecache **ncpp, 760 struct neglist **neglistpp) 761 { 762 struct neglist *neglist; 763 struct namecache *ncp; 764 int i; 765 766 for (i = start; i < numneglists; i++) { 767 neglist = &neglists[i]; 768 if (TAILQ_FIRST(&neglist->nl_list) == NULL) 769 continue; 770 mtx_lock(&neglist->nl_lock); 771 ncp = TAILQ_FIRST(&neglist->nl_list); 772 if (ncp != NULL) 773 break; 774 mtx_unlock(&neglist->nl_lock); 775 } 776 777 *neglistpp = neglist; 778 *ncpp = ncp; 779 } 780 781 static void 782 cache_negative_zap_one(void) 783 { 784 struct namecache *ncp, *ncp2, *ncpc; 785 struct neglist *neglist; 786 struct mtx *dvlp; 787 struct rwlock *blp; 788 789 if (!mtx_trylock(&ncneg_shrink_lock)) 790 return; 791 792 ncpc = NULL; 793 mtx_lock(&ncneg_hot.nl_lock); 794 ncp = TAILQ_FIRST(&ncneg_hot.nl_list); 795 if (ncp != NULL) { 796 neglist = NCP2NEGLIST(ncp); 797 mtx_lock(&neglist->nl_lock); 798 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst); 799 TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst); 800 ncp->nc_flag &= ~NCF_HOTNEGATIVE; 801 mtx_unlock(&neglist->nl_lock); 802 } 803 804 cache_negative_shrink_select(shrink_list_turn, &ncp, &neglist); 805 shrink_list_turn++; 806 if (shrink_list_turn == numneglists) 807 shrink_list_turn = 0; 808 if (ncp == NULL && shrink_list_turn == 0) 809 cache_negative_shrink_select(shrink_list_turn, &ncp, &neglist); 810 if (ncp == NULL) { 811 mtx_unlock(&ncneg_hot.nl_lock); 812 goto out; 813 } 814 815 MPASS(ncp->nc_flag & NCF_NEGATIVE); 816 dvlp = VP2VNODELOCK(ncp->nc_dvp); 817 blp = NCP2BUCKETLOCK(ncp); 818 mtx_unlock(&neglist->nl_lock); 819 mtx_unlock(&ncneg_hot.nl_lock); 820 mtx_lock(dvlp); 821 rw_wlock(blp); 822 mtx_lock(&ncneg_hot.nl_lock); 823 mtx_lock(&neglist->nl_lock); 824 ncp2 = TAILQ_FIRST(&neglist->nl_list); 825 if (ncp != ncp2 || dvlp != VP2VNODELOCK(ncp2->nc_dvp) || 826 blp != NCP2BUCKETLOCK(ncp2) || !(ncp2->nc_flag & NCF_NEGATIVE)) { 827 ncp = NULL; 828 goto out_unlock_all; 829 } 830 SDT_PROBE3(vfs, namecache, shrink_negative, done, ncp->nc_dvp, 831 nc_get_name(ncp), ncp->nc_neghits); 832 833 cache_zap_locked(ncp, true); 834 out_unlock_all: 835 mtx_unlock(&neglist->nl_lock); 836 mtx_unlock(&ncneg_hot.nl_lock); 837 rw_wunlock(blp); 838 mtx_unlock(dvlp); 839 out: 840 mtx_unlock(&ncneg_shrink_lock); 841 cache_free(ncp); 842 } 843 844 /* 845 * cache_zap_locked(): 846 * 847 * Removes a namecache entry from cache, whether it contains an actual 848 * pointer to a vnode or if it is just a negative cache entry. 849 */ 850 static void 851 cache_zap_locked(struct namecache *ncp, bool neg_locked) 852 { 853 854 if (!(ncp->nc_flag & NCF_NEGATIVE)) 855 cache_assert_vnode_locked(ncp->nc_vp); 856 cache_assert_vnode_locked(ncp->nc_dvp); 857 cache_assert_bucket_locked(ncp, RA_WLOCKED); 858 859 CTR2(KTR_VFS, "cache_zap(%p) vp %p", ncp, 860 (ncp->nc_flag & NCF_NEGATIVE) ? NULL : ncp->nc_vp); 861 if (!(ncp->nc_flag & NCF_NEGATIVE)) { 862 SDT_PROBE3(vfs, namecache, zap, done, ncp->nc_dvp, 863 nc_get_name(ncp), ncp->nc_vp); 864 } else { 865 SDT_PROBE3(vfs, namecache, zap_negative, done, ncp->nc_dvp, 866 nc_get_name(ncp), ncp->nc_neghits); 867 } 868 LIST_REMOVE(ncp, nc_hash); 869 if (ncp->nc_flag & NCF_ISDOTDOT) { 870 if (ncp == ncp->nc_dvp->v_cache_dd) 871 ncp->nc_dvp->v_cache_dd = NULL; 872 } else { 873 LIST_REMOVE(ncp, nc_src); 874 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) { 875 ncp->nc_flag |= NCF_DVDROP; 876 atomic_subtract_rel_long(&numcachehv, 1); 877 } 878 } 879 if (!(ncp->nc_flag & NCF_NEGATIVE)) { 880 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst); 881 if (ncp == ncp->nc_vp->v_cache_dd) 882 ncp->nc_vp->v_cache_dd = NULL; 883 } else { 884 cache_negative_remove(ncp, neg_locked); 885 } 886 atomic_subtract_rel_long(&numcache, 1); 887 } 888 889 static void 890 cache_zap_negative_locked_vnode_kl(struct namecache *ncp, struct vnode *vp) 891 { 892 struct rwlock *blp; 893 894 MPASS(ncp->nc_dvp == vp); 895 MPASS(ncp->nc_flag & NCF_NEGATIVE); 896 cache_assert_vnode_locked(vp); 897 898 blp = NCP2BUCKETLOCK(ncp); 899 rw_wlock(blp); 900 cache_zap_locked(ncp, false); 901 rw_wunlock(blp); 902 } 903 904 static bool 905 cache_zap_locked_vnode_kl2(struct namecache *ncp, struct vnode *vp, 906 struct mtx **vlpp) 907 { 908 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock; 909 struct rwlock *blp; 910 911 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp); 912 cache_assert_vnode_locked(vp); 913 914 if (ncp->nc_flag & NCF_NEGATIVE) { 915 if (*vlpp != NULL) { 916 mtx_unlock(*vlpp); 917 *vlpp = NULL; 918 } 919 cache_zap_negative_locked_vnode_kl(ncp, vp); 920 return (true); 921 } 922 923 pvlp = VP2VNODELOCK(vp); 924 blp = NCP2BUCKETLOCK(ncp); 925 vlp1 = VP2VNODELOCK(ncp->nc_dvp); 926 vlp2 = VP2VNODELOCK(ncp->nc_vp); 927 928 if (*vlpp == vlp1 || *vlpp == vlp2) { 929 to_unlock = *vlpp; 930 *vlpp = NULL; 931 } else { 932 if (*vlpp != NULL) { 933 mtx_unlock(*vlpp); 934 *vlpp = NULL; 935 } 936 cache_sort(&vlp1, &vlp2); 937 if (vlp1 == pvlp) { 938 mtx_lock(vlp2); 939 to_unlock = vlp2; 940 } else { 941 if (!mtx_trylock(vlp1)) 942 goto out_relock; 943 to_unlock = vlp1; 944 } 945 } 946 rw_wlock(blp); 947 cache_zap_locked(ncp, false); 948 rw_wunlock(blp); 949 if (to_unlock != NULL) 950 mtx_unlock(to_unlock); 951 return (true); 952 953 out_relock: 954 mtx_unlock(vlp2); 955 mtx_lock(vlp1); 956 mtx_lock(vlp2); 957 MPASS(*vlpp == NULL); 958 *vlpp = vlp1; 959 return (false); 960 } 961 962 static int 963 cache_zap_locked_vnode(struct namecache *ncp, struct vnode *vp) 964 { 965 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock; 966 struct rwlock *blp; 967 int error = 0; 968 969 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp); 970 cache_assert_vnode_locked(vp); 971 972 pvlp = VP2VNODELOCK(vp); 973 if (ncp->nc_flag & NCF_NEGATIVE) { 974 cache_zap_negative_locked_vnode_kl(ncp, vp); 975 goto out; 976 } 977 978 blp = NCP2BUCKETLOCK(ncp); 979 vlp1 = VP2VNODELOCK(ncp->nc_dvp); 980 vlp2 = VP2VNODELOCK(ncp->nc_vp); 981 cache_sort(&vlp1, &vlp2); 982 if (vlp1 == pvlp) { 983 mtx_lock(vlp2); 984 to_unlock = vlp2; 985 } else { 986 if (!mtx_trylock(vlp1)) { 987 error = EAGAIN; 988 goto out; 989 } 990 to_unlock = vlp1; 991 } 992 rw_wlock(blp); 993 cache_zap_locked(ncp, false); 994 rw_wunlock(blp); 995 mtx_unlock(to_unlock); 996 out: 997 mtx_unlock(pvlp); 998 return (error); 999 } 1000 1001 static int 1002 cache_zap_rlocked_bucket(struct namecache *ncp, struct rwlock *blp) 1003 { 1004 struct mtx *dvlp, *vlp; 1005 1006 cache_assert_bucket_locked(ncp, RA_RLOCKED); 1007 1008 dvlp = VP2VNODELOCK(ncp->nc_dvp); 1009 vlp = NULL; 1010 if (!(ncp->nc_flag & NCF_NEGATIVE)) 1011 vlp = VP2VNODELOCK(ncp->nc_vp); 1012 if (cache_trylock_vnodes(dvlp, vlp) == 0) { 1013 rw_runlock(blp); 1014 rw_wlock(blp); 1015 cache_zap_locked(ncp, false); 1016 rw_wunlock(blp); 1017 cache_unlock_vnodes(dvlp, vlp); 1018 return (0); 1019 } 1020 1021 rw_runlock(blp); 1022 return (EAGAIN); 1023 } 1024 1025 static int 1026 cache_zap_wlocked_bucket_kl(struct namecache *ncp, struct rwlock *blp, 1027 struct mtx **vlpp1, struct mtx **vlpp2) 1028 { 1029 struct mtx *dvlp, *vlp; 1030 1031 cache_assert_bucket_locked(ncp, RA_WLOCKED); 1032 1033 dvlp = VP2VNODELOCK(ncp->nc_dvp); 1034 vlp = NULL; 1035 if (!(ncp->nc_flag & NCF_NEGATIVE)) 1036 vlp = VP2VNODELOCK(ncp->nc_vp); 1037 cache_sort(&dvlp, &vlp); 1038 1039 if (*vlpp1 == dvlp && *vlpp2 == vlp) { 1040 cache_zap_locked(ncp, false); 1041 cache_unlock_vnodes(dvlp, vlp); 1042 *vlpp1 = NULL; 1043 *vlpp2 = NULL; 1044 return (0); 1045 } 1046 1047 if (*vlpp1 != NULL) 1048 mtx_unlock(*vlpp1); 1049 if (*vlpp2 != NULL) 1050 mtx_unlock(*vlpp2); 1051 *vlpp1 = NULL; 1052 *vlpp2 = NULL; 1053 1054 if (cache_trylock_vnodes(dvlp, vlp) == 0) { 1055 cache_zap_locked(ncp, false); 1056 cache_unlock_vnodes(dvlp, vlp); 1057 return (0); 1058 } 1059 1060 rw_wunlock(blp); 1061 *vlpp1 = dvlp; 1062 *vlpp2 = vlp; 1063 if (*vlpp1 != NULL) 1064 mtx_lock(*vlpp1); 1065 mtx_lock(*vlpp2); 1066 rw_wlock(blp); 1067 return (EAGAIN); 1068 } 1069 1070 static void 1071 cache_lookup_unlock(struct rwlock *blp, struct mtx *vlp) 1072 { 1073 1074 if (blp != NULL) { 1075 rw_runlock(blp); 1076 mtx_assert(vlp, MA_NOTOWNED); 1077 } else { 1078 mtx_unlock(vlp); 1079 } 1080 } 1081 1082 /* 1083 * Lookup an entry in the cache 1084 * 1085 * Lookup is called with dvp pointing to the directory to search, 1086 * cnp pointing to the name of the entry being sought. If the lookup 1087 * succeeds, the vnode is returned in *vpp, and a status of -1 is 1088 * returned. If the lookup determines that the name does not exist 1089 * (negative caching), a status of ENOENT is returned. If the lookup 1090 * fails, a status of zero is returned. If the directory vnode is 1091 * recycled out from under us due to a forced unmount, a status of 1092 * ENOENT is returned. 1093 * 1094 * vpp is locked and ref'd on return. If we're looking up DOTDOT, dvp is 1095 * unlocked. If we're looking up . an extra ref is taken, but the lock is 1096 * not recursively acquired. 1097 */ 1098 1099 int 1100 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp, 1101 struct timespec *tsp, int *ticksp) 1102 { 1103 struct namecache *ncp; 1104 struct rwlock *blp; 1105 struct mtx *dvlp, *dvlp2; 1106 uint32_t hash; 1107 int error, ltype; 1108 1109 if (!doingcache) { 1110 cnp->cn_flags &= ~MAKEENTRY; 1111 return (0); 1112 } 1113 retry: 1114 blp = NULL; 1115 dvlp = VP2VNODELOCK(dvp); 1116 error = 0; 1117 counter_u64_add(numcalls, 1); 1118 1119 if (cnp->cn_nameptr[0] == '.') { 1120 if (cnp->cn_namelen == 1) { 1121 *vpp = dvp; 1122 CTR2(KTR_VFS, "cache_lookup(%p, %s) found via .", 1123 dvp, cnp->cn_nameptr); 1124 counter_u64_add(dothits, 1); 1125 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", *vpp); 1126 if (tsp != NULL) 1127 timespecclear(tsp); 1128 if (ticksp != NULL) 1129 *ticksp = ticks; 1130 VREF(*vpp); 1131 /* 1132 * When we lookup "." we still can be asked to lock it 1133 * differently... 1134 */ 1135 ltype = cnp->cn_lkflags & LK_TYPE_MASK; 1136 if (ltype != VOP_ISLOCKED(*vpp)) { 1137 if (ltype == LK_EXCLUSIVE) { 1138 vn_lock(*vpp, LK_UPGRADE | LK_RETRY); 1139 if ((*vpp)->v_iflag & VI_DOOMED) { 1140 /* forced unmount */ 1141 vrele(*vpp); 1142 *vpp = NULL; 1143 return (ENOENT); 1144 } 1145 } else 1146 vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY); 1147 } 1148 return (-1); 1149 } 1150 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') { 1151 counter_u64_add(dotdothits, 1); 1152 dvlp2 = NULL; 1153 mtx_lock(dvlp); 1154 retry_dotdot: 1155 ncp = dvp->v_cache_dd; 1156 if (ncp == NULL) { 1157 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, 1158 "..", NULL); 1159 mtx_unlock(dvlp); 1160 return (0); 1161 } 1162 if ((cnp->cn_flags & MAKEENTRY) == 0) { 1163 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) { 1164 if (ncp->nc_dvp != dvp) 1165 panic("dvp %p v_cache_dd %p\n", dvp, ncp); 1166 if (!cache_zap_locked_vnode_kl2(ncp, 1167 dvp, &dvlp2)) 1168 goto retry_dotdot; 1169 MPASS(dvp->v_cache_dd == NULL); 1170 mtx_unlock(dvlp); 1171 if (dvlp2 != NULL) 1172 mtx_unlock(dvlp2); 1173 cache_free(ncp); 1174 } else { 1175 dvp->v_cache_dd = NULL; 1176 mtx_unlock(dvlp); 1177 if (dvlp2 != NULL) 1178 mtx_unlock(dvlp2); 1179 } 1180 return (0); 1181 } 1182 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) { 1183 if (ncp->nc_flag & NCF_NEGATIVE) 1184 *vpp = NULL; 1185 else 1186 *vpp = ncp->nc_vp; 1187 } else 1188 *vpp = ncp->nc_dvp; 1189 /* Return failure if negative entry was found. */ 1190 if (*vpp == NULL) 1191 goto negative_success; 1192 CTR3(KTR_VFS, "cache_lookup(%p, %s) found %p via ..", 1193 dvp, cnp->cn_nameptr, *vpp); 1194 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, "..", 1195 *vpp); 1196 cache_out_ts(ncp, tsp, ticksp); 1197 if ((ncp->nc_flag & (NCF_ISDOTDOT | NCF_DTS)) == 1198 NCF_DTS && tsp != NULL) 1199 *tsp = ((struct namecache_ts *)ncp)-> 1200 nc_dotdottime; 1201 goto success; 1202 } 1203 } 1204 1205 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp); 1206 blp = HASH2BUCKETLOCK(hash); 1207 rw_rlock(blp); 1208 1209 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) { 1210 counter_u64_add(numchecks, 1); 1211 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen && 1212 !bcmp(nc_get_name(ncp), cnp->cn_nameptr, ncp->nc_nlen)) 1213 break; 1214 } 1215 1216 /* We failed to find an entry */ 1217 if (ncp == NULL) { 1218 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr, 1219 NULL); 1220 if ((cnp->cn_flags & MAKEENTRY) == 0) { 1221 counter_u64_add(nummisszap, 1); 1222 } else { 1223 counter_u64_add(nummiss, 1); 1224 } 1225 goto unlock; 1226 } 1227 1228 /* We don't want to have an entry, so dump it */ 1229 if ((cnp->cn_flags & MAKEENTRY) == 0) { 1230 counter_u64_add(numposzaps, 1); 1231 goto zap_and_exit; 1232 } 1233 1234 /* We found a "positive" match, return the vnode */ 1235 if (!(ncp->nc_flag & NCF_NEGATIVE)) { 1236 counter_u64_add(numposhits, 1); 1237 *vpp = ncp->nc_vp; 1238 CTR4(KTR_VFS, "cache_lookup(%p, %s) found %p via ncp %p", 1239 dvp, cnp->cn_nameptr, *vpp, ncp); 1240 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, nc_get_name(ncp), 1241 *vpp); 1242 cache_out_ts(ncp, tsp, ticksp); 1243 goto success; 1244 } 1245 1246 negative_success: 1247 /* We found a negative match, and want to create it, so purge */ 1248 if (cnp->cn_nameiop == CREATE) { 1249 counter_u64_add(numnegzaps, 1); 1250 goto zap_and_exit; 1251 } 1252 1253 counter_u64_add(numneghits, 1); 1254 cache_negative_hit(ncp); 1255 if (ncp->nc_flag & NCF_WHITE) 1256 cnp->cn_flags |= ISWHITEOUT; 1257 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp, 1258 nc_get_name(ncp)); 1259 cache_out_ts(ncp, tsp, ticksp); 1260 cache_lookup_unlock(blp, dvlp); 1261 return (ENOENT); 1262 1263 success: 1264 /* 1265 * On success we return a locked and ref'd vnode as per the lookup 1266 * protocol. 1267 */ 1268 MPASS(dvp != *vpp); 1269 ltype = 0; /* silence gcc warning */ 1270 if (cnp->cn_flags & ISDOTDOT) { 1271 ltype = VOP_ISLOCKED(dvp); 1272 VOP_UNLOCK(dvp, 0); 1273 } 1274 vhold(*vpp); 1275 cache_lookup_unlock(blp, dvlp); 1276 error = vget(*vpp, cnp->cn_lkflags | LK_VNHELD, cnp->cn_thread); 1277 if (cnp->cn_flags & ISDOTDOT) { 1278 vn_lock(dvp, ltype | LK_RETRY); 1279 if (dvp->v_iflag & VI_DOOMED) { 1280 if (error == 0) 1281 vput(*vpp); 1282 *vpp = NULL; 1283 return (ENOENT); 1284 } 1285 } 1286 if (error) { 1287 *vpp = NULL; 1288 goto retry; 1289 } 1290 if ((cnp->cn_flags & ISLASTCN) && 1291 (cnp->cn_lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE) { 1292 ASSERT_VOP_ELOCKED(*vpp, "cache_lookup"); 1293 } 1294 return (-1); 1295 1296 unlock: 1297 cache_lookup_unlock(blp, dvlp); 1298 return (0); 1299 1300 zap_and_exit: 1301 if (blp != NULL) 1302 error = cache_zap_rlocked_bucket(ncp, blp); 1303 else 1304 error = cache_zap_locked_vnode(ncp, dvp); 1305 if (error != 0) { 1306 zap_and_exit_bucket_fail++; 1307 cache_maybe_yield(); 1308 goto retry; 1309 } 1310 cache_free(ncp); 1311 return (0); 1312 } 1313 1314 struct celockstate { 1315 struct mtx *vlp[3]; 1316 struct rwlock *blp[2]; 1317 }; 1318 CTASSERT((nitems(((struct celockstate *)0)->vlp) == 3)); 1319 CTASSERT((nitems(((struct celockstate *)0)->blp) == 2)); 1320 1321 static inline void 1322 cache_celockstate_init(struct celockstate *cel) 1323 { 1324 1325 bzero(cel, sizeof(*cel)); 1326 } 1327 1328 static void 1329 cache_lock_vnodes_cel(struct celockstate *cel, struct vnode *vp, 1330 struct vnode *dvp) 1331 { 1332 struct mtx *vlp1, *vlp2; 1333 1334 MPASS(cel->vlp[0] == NULL); 1335 MPASS(cel->vlp[1] == NULL); 1336 MPASS(cel->vlp[2] == NULL); 1337 1338 MPASS(vp != NULL || dvp != NULL); 1339 1340 vlp1 = VP2VNODELOCK(vp); 1341 vlp2 = VP2VNODELOCK(dvp); 1342 cache_sort(&vlp1, &vlp2); 1343 1344 if (vlp1 != NULL) { 1345 mtx_lock(vlp1); 1346 cel->vlp[0] = vlp1; 1347 } 1348 mtx_lock(vlp2); 1349 cel->vlp[1] = vlp2; 1350 } 1351 1352 static void 1353 cache_unlock_vnodes_cel(struct celockstate *cel) 1354 { 1355 1356 MPASS(cel->vlp[0] != NULL || cel->vlp[1] != NULL); 1357 1358 if (cel->vlp[0] != NULL) 1359 mtx_unlock(cel->vlp[0]); 1360 if (cel->vlp[1] != NULL) 1361 mtx_unlock(cel->vlp[1]); 1362 if (cel->vlp[2] != NULL) 1363 mtx_unlock(cel->vlp[2]); 1364 } 1365 1366 static bool 1367 cache_lock_vnodes_cel_3(struct celockstate *cel, struct vnode *vp) 1368 { 1369 struct mtx *vlp; 1370 bool ret; 1371 1372 cache_assert_vlp_locked(cel->vlp[0]); 1373 cache_assert_vlp_locked(cel->vlp[1]); 1374 MPASS(cel->vlp[2] == NULL); 1375 1376 vlp = VP2VNODELOCK(vp); 1377 MPASS(vlp != NULL); 1378 1379 ret = true; 1380 if (vlp >= cel->vlp[1]) { 1381 mtx_lock(vlp); 1382 } else { 1383 if (mtx_trylock(vlp)) 1384 goto out; 1385 cache_lock_vnodes_cel_3_failures++; 1386 cache_unlock_vnodes_cel(cel); 1387 if (vlp < cel->vlp[0]) { 1388 mtx_lock(vlp); 1389 mtx_lock(cel->vlp[0]); 1390 mtx_lock(cel->vlp[1]); 1391 } else { 1392 if (cel->vlp[0] != NULL) 1393 mtx_lock(cel->vlp[0]); 1394 mtx_lock(vlp); 1395 mtx_lock(cel->vlp[1]); 1396 } 1397 ret = false; 1398 } 1399 out: 1400 cel->vlp[2] = vlp; 1401 return (ret); 1402 } 1403 1404 static void 1405 cache_lock_buckets_cel(struct celockstate *cel, struct rwlock *blp1, 1406 struct rwlock *blp2) 1407 { 1408 1409 MPASS(cel->blp[0] == NULL); 1410 MPASS(cel->blp[1] == NULL); 1411 1412 cache_sort(&blp1, &blp2); 1413 1414 if (blp1 != NULL) { 1415 rw_wlock(blp1); 1416 cel->blp[0] = blp1; 1417 } 1418 rw_wlock(blp2); 1419 cel->blp[1] = blp2; 1420 } 1421 1422 static void 1423 cache_unlock_buckets_cel(struct celockstate *cel) 1424 { 1425 1426 if (cel->blp[0] != NULL) 1427 rw_wunlock(cel->blp[0]); 1428 rw_wunlock(cel->blp[1]); 1429 } 1430 1431 /* 1432 * Lock part of the cache affected by the insertion. 1433 * 1434 * This means vnodelocks for dvp, vp and the relevant bucketlock. 1435 * However, insertion can result in removal of an old entry. In this 1436 * case we have an additional vnode and bucketlock pair to lock. If the 1437 * entry is negative, ncelock is locked instead of the vnode. 1438 * 1439 * That is, in the worst case we have to lock 3 vnodes and 2 bucketlocks, while 1440 * preserving the locking order (smaller address first). 1441 */ 1442 static void 1443 cache_enter_lock(struct celockstate *cel, struct vnode *dvp, struct vnode *vp, 1444 uint32_t hash) 1445 { 1446 struct namecache *ncp; 1447 struct rwlock *blps[2]; 1448 1449 blps[0] = HASH2BUCKETLOCK(hash); 1450 for (;;) { 1451 blps[1] = NULL; 1452 cache_lock_vnodes_cel(cel, dvp, vp); 1453 if (vp == NULL || vp->v_type != VDIR) 1454 break; 1455 ncp = vp->v_cache_dd; 1456 if (ncp == NULL) 1457 break; 1458 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0) 1459 break; 1460 MPASS(ncp->nc_dvp == vp); 1461 blps[1] = NCP2BUCKETLOCK(ncp); 1462 if (ncp->nc_flag & NCF_NEGATIVE) 1463 break; 1464 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp)) 1465 break; 1466 /* 1467 * All vnodes got re-locked. Re-validate the state and if 1468 * nothing changed we are done. Otherwise restart. 1469 */ 1470 if (ncp == vp->v_cache_dd && 1471 (ncp->nc_flag & NCF_ISDOTDOT) != 0 && 1472 blps[1] == NCP2BUCKETLOCK(ncp) && 1473 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2]) 1474 break; 1475 cache_unlock_vnodes_cel(cel); 1476 cel->vlp[0] = NULL; 1477 cel->vlp[1] = NULL; 1478 cel->vlp[2] = NULL; 1479 } 1480 cache_lock_buckets_cel(cel, blps[0], blps[1]); 1481 } 1482 1483 static void 1484 cache_enter_lock_dd(struct celockstate *cel, struct vnode *dvp, struct vnode *vp, 1485 uint32_t hash) 1486 { 1487 struct namecache *ncp; 1488 struct rwlock *blps[2]; 1489 1490 blps[0] = HASH2BUCKETLOCK(hash); 1491 for (;;) { 1492 blps[1] = NULL; 1493 cache_lock_vnodes_cel(cel, dvp, vp); 1494 ncp = dvp->v_cache_dd; 1495 if (ncp == NULL) 1496 break; 1497 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0) 1498 break; 1499 MPASS(ncp->nc_dvp == dvp); 1500 blps[1] = NCP2BUCKETLOCK(ncp); 1501 if (ncp->nc_flag & NCF_NEGATIVE) 1502 break; 1503 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp)) 1504 break; 1505 if (ncp == dvp->v_cache_dd && 1506 (ncp->nc_flag & NCF_ISDOTDOT) != 0 && 1507 blps[1] == NCP2BUCKETLOCK(ncp) && 1508 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2]) 1509 break; 1510 cache_unlock_vnodes_cel(cel); 1511 cel->vlp[0] = NULL; 1512 cel->vlp[1] = NULL; 1513 cel->vlp[2] = NULL; 1514 } 1515 cache_lock_buckets_cel(cel, blps[0], blps[1]); 1516 } 1517 1518 static void 1519 cache_enter_unlock(struct celockstate *cel) 1520 { 1521 1522 cache_unlock_buckets_cel(cel); 1523 cache_unlock_vnodes_cel(cel); 1524 } 1525 1526 /* 1527 * Add an entry to the cache. 1528 */ 1529 void 1530 cache_enter_time(struct vnode *dvp, struct vnode *vp, struct componentname *cnp, 1531 struct timespec *tsp, struct timespec *dtsp) 1532 { 1533 struct celockstate cel; 1534 struct namecache *ncp, *n2, *ndd; 1535 struct namecache_ts *n3; 1536 struct nchashhead *ncpp; 1537 struct neglist *neglist; 1538 uint32_t hash; 1539 int flag; 1540 int len; 1541 bool neg_locked; 1542 1543 CTR3(KTR_VFS, "cache_enter(%p, %p, %s)", dvp, vp, cnp->cn_nameptr); 1544 VNASSERT(vp == NULL || (vp->v_iflag & VI_DOOMED) == 0, vp, 1545 ("cache_enter: Adding a doomed vnode")); 1546 VNASSERT(dvp == NULL || (dvp->v_iflag & VI_DOOMED) == 0, dvp, 1547 ("cache_enter: Doomed vnode used as src")); 1548 1549 if (!doingcache) 1550 return; 1551 1552 /* 1553 * Avoid blowout in namecache entries. 1554 */ 1555 if (numcache >= desiredvnodes * ncsizefactor) 1556 return; 1557 1558 cache_celockstate_init(&cel); 1559 ndd = NULL; 1560 flag = 0; 1561 if (cnp->cn_nameptr[0] == '.') { 1562 if (cnp->cn_namelen == 1) 1563 return; 1564 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') { 1565 len = cnp->cn_namelen; 1566 hash = cache_get_hash(cnp->cn_nameptr, len, dvp); 1567 cache_enter_lock_dd(&cel, dvp, vp, hash); 1568 /* 1569 * If dotdot entry already exists, just retarget it 1570 * to new parent vnode, otherwise continue with new 1571 * namecache entry allocation. 1572 */ 1573 if ((ncp = dvp->v_cache_dd) != NULL && 1574 ncp->nc_flag & NCF_ISDOTDOT) { 1575 KASSERT(ncp->nc_dvp == dvp, 1576 ("wrong isdotdot parent")); 1577 neg_locked = false; 1578 if (ncp->nc_flag & NCF_NEGATIVE || vp == NULL) { 1579 neglist = NCP2NEGLIST(ncp); 1580 mtx_lock(&ncneg_hot.nl_lock); 1581 mtx_lock(&neglist->nl_lock); 1582 neg_locked = true; 1583 } 1584 if (!(ncp->nc_flag & NCF_NEGATIVE)) { 1585 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, 1586 ncp, nc_dst); 1587 } else { 1588 cache_negative_remove(ncp, true); 1589 } 1590 if (vp != NULL) { 1591 TAILQ_INSERT_HEAD(&vp->v_cache_dst, 1592 ncp, nc_dst); 1593 ncp->nc_flag &= ~(NCF_NEGATIVE|NCF_HOTNEGATIVE); 1594 } else { 1595 ncp->nc_flag &= ~(NCF_HOTNEGATIVE); 1596 ncp->nc_flag |= NCF_NEGATIVE; 1597 cache_negative_insert(ncp, true); 1598 } 1599 if (neg_locked) { 1600 mtx_unlock(&neglist->nl_lock); 1601 mtx_unlock(&ncneg_hot.nl_lock); 1602 } 1603 ncp->nc_vp = vp; 1604 cache_enter_unlock(&cel); 1605 return; 1606 } 1607 dvp->v_cache_dd = NULL; 1608 cache_enter_unlock(&cel); 1609 cache_celockstate_init(&cel); 1610 SDT_PROBE3(vfs, namecache, enter, done, dvp, "..", vp); 1611 flag = NCF_ISDOTDOT; 1612 } 1613 } 1614 1615 /* 1616 * Calculate the hash key and setup as much of the new 1617 * namecache entry as possible before acquiring the lock. 1618 */ 1619 ncp = cache_alloc(cnp->cn_namelen, tsp != NULL); 1620 ncp->nc_flag = flag; 1621 ncp->nc_vp = vp; 1622 if (vp == NULL) 1623 ncp->nc_flag |= NCF_NEGATIVE; 1624 ncp->nc_dvp = dvp; 1625 if (tsp != NULL) { 1626 n3 = (struct namecache_ts *)ncp; 1627 n3->nc_time = *tsp; 1628 n3->nc_ticks = ticks; 1629 n3->nc_flag |= NCF_TS; 1630 if (dtsp != NULL) { 1631 n3->nc_dotdottime = *dtsp; 1632 n3->nc_flag |= NCF_DTS; 1633 } 1634 } 1635 len = ncp->nc_nlen = cnp->cn_namelen; 1636 hash = cache_get_hash(cnp->cn_nameptr, len, dvp); 1637 strlcpy(nc_get_name(ncp), cnp->cn_nameptr, len + 1); 1638 cache_enter_lock(&cel, dvp, vp, hash); 1639 1640 /* 1641 * See if this vnode or negative entry is already in the cache 1642 * with this name. This can happen with concurrent lookups of 1643 * the same path name. 1644 */ 1645 ncpp = NCHHASH(hash); 1646 LIST_FOREACH(n2, ncpp, nc_hash) { 1647 if (n2->nc_dvp == dvp && 1648 n2->nc_nlen == cnp->cn_namelen && 1649 !bcmp(nc_get_name(n2), cnp->cn_nameptr, n2->nc_nlen)) { 1650 if (tsp != NULL) { 1651 KASSERT((n2->nc_flag & NCF_TS) != 0, 1652 ("no NCF_TS")); 1653 n3 = (struct namecache_ts *)n2; 1654 n3->nc_time = 1655 ((struct namecache_ts *)ncp)->nc_time; 1656 n3->nc_ticks = 1657 ((struct namecache_ts *)ncp)->nc_ticks; 1658 if (dtsp != NULL) { 1659 n3->nc_dotdottime = 1660 ((struct namecache_ts *)ncp)-> 1661 nc_dotdottime; 1662 if (ncp->nc_flag & NCF_NEGATIVE) 1663 mtx_lock(&ncneg_hot.nl_lock); 1664 n3->nc_flag |= NCF_DTS; 1665 if (ncp->nc_flag & NCF_NEGATIVE) 1666 mtx_unlock(&ncneg_hot.nl_lock); 1667 } 1668 } 1669 goto out_unlock_free; 1670 } 1671 } 1672 1673 if (flag == NCF_ISDOTDOT) { 1674 /* 1675 * See if we are trying to add .. entry, but some other lookup 1676 * has populated v_cache_dd pointer already. 1677 */ 1678 if (dvp->v_cache_dd != NULL) 1679 goto out_unlock_free; 1680 KASSERT(vp == NULL || vp->v_type == VDIR, 1681 ("wrong vnode type %p", vp)); 1682 dvp->v_cache_dd = ncp; 1683 } 1684 1685 atomic_add_rel_long(&numcache, 1); 1686 if (vp != NULL) { 1687 if (vp->v_type == VDIR) { 1688 if (flag != NCF_ISDOTDOT) { 1689 /* 1690 * For this case, the cache entry maps both the 1691 * directory name in it and the name ".." for the 1692 * directory's parent. 1693 */ 1694 if ((ndd = vp->v_cache_dd) != NULL) { 1695 if ((ndd->nc_flag & NCF_ISDOTDOT) != 0) 1696 cache_zap_locked(ndd, false); 1697 else 1698 ndd = NULL; 1699 } 1700 vp->v_cache_dd = ncp; 1701 } 1702 } else { 1703 vp->v_cache_dd = NULL; 1704 } 1705 } 1706 1707 if (flag != NCF_ISDOTDOT) { 1708 if (LIST_EMPTY(&dvp->v_cache_src)) { 1709 vhold(dvp); 1710 atomic_add_rel_long(&numcachehv, 1); 1711 } 1712 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src); 1713 } 1714 1715 /* 1716 * Insert the new namecache entry into the appropriate chain 1717 * within the cache entries table. 1718 */ 1719 LIST_INSERT_HEAD(ncpp, ncp, nc_hash); 1720 1721 /* 1722 * If the entry is "negative", we place it into the 1723 * "negative" cache queue, otherwise, we place it into the 1724 * destination vnode's cache entries queue. 1725 */ 1726 if (vp != NULL) { 1727 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst); 1728 SDT_PROBE3(vfs, namecache, enter, done, dvp, nc_get_name(ncp), 1729 vp); 1730 } else { 1731 if (cnp->cn_flags & ISWHITEOUT) 1732 ncp->nc_flag |= NCF_WHITE; 1733 cache_negative_insert(ncp, false); 1734 SDT_PROBE2(vfs, namecache, enter_negative, done, dvp, 1735 nc_get_name(ncp)); 1736 } 1737 cache_enter_unlock(&cel); 1738 if (numneg * ncnegfactor > numcache) 1739 cache_negative_zap_one(); 1740 cache_free(ndd); 1741 return; 1742 out_unlock_free: 1743 cache_enter_unlock(&cel); 1744 cache_free(ncp); 1745 return; 1746 } 1747 1748 static u_int 1749 cache_roundup_2(u_int val) 1750 { 1751 u_int res; 1752 1753 for (res = 1; res <= val; res <<= 1) 1754 continue; 1755 1756 return (res); 1757 } 1758 1759 /* 1760 * Name cache initialization, from vfs_init() when we are booting 1761 */ 1762 static void 1763 nchinit(void *dummy __unused) 1764 { 1765 u_int i; 1766 1767 cache_zone_small = uma_zcreate("S VFS Cache", 1768 sizeof(struct namecache) + CACHE_PATH_CUTOFF + 1, 1769 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT); 1770 cache_zone_small_ts = uma_zcreate("STS VFS Cache", 1771 sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1, 1772 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT); 1773 cache_zone_large = uma_zcreate("L VFS Cache", 1774 sizeof(struct namecache) + NAME_MAX + 1, 1775 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT); 1776 cache_zone_large_ts = uma_zcreate("LTS VFS Cache", 1777 sizeof(struct namecache_ts) + NAME_MAX + 1, 1778 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT); 1779 1780 nchashtbl = hashinit(desiredvnodes * 2, M_VFSCACHE, &nchash); 1781 numbucketlocks = cache_roundup_2(mp_ncpus * 64); 1782 bucketlocks = malloc(sizeof(*bucketlocks) * numbucketlocks, M_VFSCACHE, 1783 M_WAITOK | M_ZERO); 1784 for (i = 0; i < numbucketlocks; i++) 1785 rw_init_flags(&bucketlocks[i], "ncbuc", RW_DUPOK | RW_RECURSE); 1786 numvnodelocks = cache_roundup_2(mp_ncpus * 64); 1787 vnodelocks = malloc(sizeof(*vnodelocks) * numvnodelocks, M_VFSCACHE, 1788 M_WAITOK | M_ZERO); 1789 for (i = 0; i < numvnodelocks; i++) 1790 mtx_init(&vnodelocks[i], "ncvn", NULL, MTX_DUPOK | MTX_RECURSE); 1791 ncpurgeminvnodes = numbucketlocks; 1792 1793 numneglists = 4; 1794 neglists = malloc(sizeof(*neglists) * numneglists, M_VFSCACHE, 1795 M_WAITOK | M_ZERO); 1796 for (i = 0; i < numneglists; i++) { 1797 mtx_init(&neglists[i].nl_lock, "ncnegl", NULL, MTX_DEF); 1798 TAILQ_INIT(&neglists[i].nl_list); 1799 } 1800 mtx_init(&ncneg_hot.nl_lock, "ncneglh", NULL, MTX_DEF); 1801 TAILQ_INIT(&ncneg_hot.nl_list); 1802 1803 numcalls = counter_u64_alloc(M_WAITOK); 1804 dothits = counter_u64_alloc(M_WAITOK); 1805 dotdothits = counter_u64_alloc(M_WAITOK); 1806 numchecks = counter_u64_alloc(M_WAITOK); 1807 nummiss = counter_u64_alloc(M_WAITOK); 1808 nummisszap = counter_u64_alloc(M_WAITOK); 1809 numposzaps = counter_u64_alloc(M_WAITOK); 1810 numposhits = counter_u64_alloc(M_WAITOK); 1811 numnegzaps = counter_u64_alloc(M_WAITOK); 1812 numneghits = counter_u64_alloc(M_WAITOK); 1813 numfullpathcalls = counter_u64_alloc(M_WAITOK); 1814 numfullpathfail1 = counter_u64_alloc(M_WAITOK); 1815 numfullpathfail2 = counter_u64_alloc(M_WAITOK); 1816 numfullpathfail4 = counter_u64_alloc(M_WAITOK); 1817 numfullpathfound = counter_u64_alloc(M_WAITOK); 1818 } 1819 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL); 1820 1821 void 1822 cache_changesize(int newmaxvnodes) 1823 { 1824 struct nchashhead *new_nchashtbl, *old_nchashtbl; 1825 u_long new_nchash, old_nchash; 1826 struct namecache *ncp; 1827 uint32_t hash; 1828 int i; 1829 1830 new_nchashtbl = hashinit(newmaxvnodes * 2, M_VFSCACHE, &new_nchash); 1831 /* If same hash table size, nothing to do */ 1832 if (nchash == new_nchash) { 1833 free(new_nchashtbl, M_VFSCACHE); 1834 return; 1835 } 1836 /* 1837 * Move everything from the old hash table to the new table. 1838 * None of the namecache entries in the table can be removed 1839 * because to do so, they have to be removed from the hash table. 1840 */ 1841 cache_lock_all_vnodes(); 1842 cache_lock_all_buckets(); 1843 old_nchashtbl = nchashtbl; 1844 old_nchash = nchash; 1845 nchashtbl = new_nchashtbl; 1846 nchash = new_nchash; 1847 for (i = 0; i <= old_nchash; i++) { 1848 while ((ncp = LIST_FIRST(&old_nchashtbl[i])) != NULL) { 1849 hash = cache_get_hash(nc_get_name(ncp), ncp->nc_nlen, 1850 ncp->nc_dvp); 1851 LIST_REMOVE(ncp, nc_hash); 1852 LIST_INSERT_HEAD(NCHHASH(hash), ncp, nc_hash); 1853 } 1854 } 1855 cache_unlock_all_buckets(); 1856 cache_unlock_all_vnodes(); 1857 free(old_nchashtbl, M_VFSCACHE); 1858 } 1859 1860 /* 1861 * Invalidate all entries to a particular vnode. 1862 */ 1863 void 1864 cache_purge(struct vnode *vp) 1865 { 1866 TAILQ_HEAD(, namecache) ncps; 1867 struct namecache *ncp, *nnp; 1868 struct mtx *vlp, *vlp2; 1869 1870 CTR1(KTR_VFS, "cache_purge(%p)", vp); 1871 SDT_PROBE1(vfs, namecache, purge, done, vp); 1872 if (LIST_EMPTY(&vp->v_cache_src) && TAILQ_EMPTY(&vp->v_cache_dst) && 1873 vp->v_cache_dd == NULL) 1874 return; 1875 TAILQ_INIT(&ncps); 1876 vlp = VP2VNODELOCK(vp); 1877 vlp2 = NULL; 1878 mtx_lock(vlp); 1879 retry: 1880 while (!LIST_EMPTY(&vp->v_cache_src)) { 1881 ncp = LIST_FIRST(&vp->v_cache_src); 1882 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2)) 1883 goto retry; 1884 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst); 1885 } 1886 while (!TAILQ_EMPTY(&vp->v_cache_dst)) { 1887 ncp = TAILQ_FIRST(&vp->v_cache_dst); 1888 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2)) 1889 goto retry; 1890 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst); 1891 } 1892 ncp = vp->v_cache_dd; 1893 if (ncp != NULL) { 1894 KASSERT(ncp->nc_flag & NCF_ISDOTDOT, 1895 ("lost dotdot link")); 1896 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2)) 1897 goto retry; 1898 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst); 1899 } 1900 KASSERT(vp->v_cache_dd == NULL, ("incomplete purge")); 1901 mtx_unlock(vlp); 1902 if (vlp2 != NULL) 1903 mtx_unlock(vlp2); 1904 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) { 1905 cache_free(ncp); 1906 } 1907 } 1908 1909 /* 1910 * Invalidate all negative entries for a particular directory vnode. 1911 */ 1912 void 1913 cache_purge_negative(struct vnode *vp) 1914 { 1915 TAILQ_HEAD(, namecache) ncps; 1916 struct namecache *ncp, *nnp; 1917 struct mtx *vlp; 1918 1919 CTR1(KTR_VFS, "cache_purge_negative(%p)", vp); 1920 SDT_PROBE1(vfs, namecache, purge_negative, done, vp); 1921 TAILQ_INIT(&ncps); 1922 vlp = VP2VNODELOCK(vp); 1923 mtx_lock(vlp); 1924 LIST_FOREACH_SAFE(ncp, &vp->v_cache_src, nc_src, nnp) { 1925 if (!(ncp->nc_flag & NCF_NEGATIVE)) 1926 continue; 1927 cache_zap_negative_locked_vnode_kl(ncp, vp); 1928 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst); 1929 } 1930 mtx_unlock(vlp); 1931 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) { 1932 cache_free(ncp); 1933 } 1934 } 1935 1936 /* 1937 * Flush all entries referencing a particular filesystem. 1938 */ 1939 void 1940 cache_purgevfs(struct mount *mp, bool force) 1941 { 1942 TAILQ_HEAD(, namecache) ncps; 1943 struct mtx *vlp1, *vlp2; 1944 struct rwlock *blp; 1945 struct nchashhead *bucket; 1946 struct namecache *ncp, *nnp; 1947 u_long i, j, n_nchash; 1948 int error; 1949 1950 /* Scan hash tables for applicable entries */ 1951 SDT_PROBE1(vfs, namecache, purgevfs, done, mp); 1952 if (!force && mp->mnt_nvnodelistsize <= ncpurgeminvnodes) 1953 return; 1954 TAILQ_INIT(&ncps); 1955 n_nchash = nchash + 1; 1956 vlp1 = vlp2 = NULL; 1957 for (i = 0; i < numbucketlocks; i++) { 1958 blp = (struct rwlock *)&bucketlocks[i]; 1959 rw_wlock(blp); 1960 for (j = i; j < n_nchash; j += numbucketlocks) { 1961 retry: 1962 bucket = &nchashtbl[j]; 1963 LIST_FOREACH_SAFE(ncp, bucket, nc_hash, nnp) { 1964 cache_assert_bucket_locked(ncp, RA_WLOCKED); 1965 if (ncp->nc_dvp->v_mount != mp) 1966 continue; 1967 error = cache_zap_wlocked_bucket_kl(ncp, blp, 1968 &vlp1, &vlp2); 1969 if (error != 0) 1970 goto retry; 1971 TAILQ_INSERT_HEAD(&ncps, ncp, nc_dst); 1972 } 1973 } 1974 rw_wunlock(blp); 1975 if (vlp1 == NULL && vlp2 == NULL) 1976 cache_maybe_yield(); 1977 } 1978 if (vlp1 != NULL) 1979 mtx_unlock(vlp1); 1980 if (vlp2 != NULL) 1981 mtx_unlock(vlp2); 1982 1983 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) { 1984 cache_free(ncp); 1985 } 1986 } 1987 1988 /* 1989 * Perform canonical checks and cache lookup and pass on to filesystem 1990 * through the vop_cachedlookup only if needed. 1991 */ 1992 1993 int 1994 vfs_cache_lookup(struct vop_lookup_args *ap) 1995 { 1996 struct vnode *dvp; 1997 int error; 1998 struct vnode **vpp = ap->a_vpp; 1999 struct componentname *cnp = ap->a_cnp; 2000 struct ucred *cred = cnp->cn_cred; 2001 int flags = cnp->cn_flags; 2002 struct thread *td = cnp->cn_thread; 2003 2004 *vpp = NULL; 2005 dvp = ap->a_dvp; 2006 2007 if (dvp->v_type != VDIR) 2008 return (ENOTDIR); 2009 2010 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) && 2011 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) 2012 return (EROFS); 2013 2014 error = VOP_ACCESS(dvp, VEXEC, cred, td); 2015 if (error) 2016 return (error); 2017 2018 error = cache_lookup(dvp, vpp, cnp, NULL, NULL); 2019 if (error == 0) 2020 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp)); 2021 if (error == -1) 2022 return (0); 2023 return (error); 2024 } 2025 2026 /* 2027 * XXX All of these sysctls would probably be more productive dead. 2028 */ 2029 static int disablecwd; 2030 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0, 2031 "Disable the getcwd syscall"); 2032 2033 /* Implementation of the getcwd syscall. */ 2034 int 2035 sys___getcwd(struct thread *td, struct __getcwd_args *uap) 2036 { 2037 2038 return (kern___getcwd(td, uap->buf, UIO_USERSPACE, uap->buflen, 2039 MAXPATHLEN)); 2040 } 2041 2042 int 2043 kern___getcwd(struct thread *td, char *buf, enum uio_seg bufseg, u_int buflen, 2044 u_int path_max) 2045 { 2046 char *bp, *tmpbuf; 2047 struct filedesc *fdp; 2048 struct vnode *cdir, *rdir; 2049 int error; 2050 2051 if (disablecwd) 2052 return (ENODEV); 2053 if (buflen < 2) 2054 return (EINVAL); 2055 if (buflen > path_max) 2056 buflen = path_max; 2057 2058 tmpbuf = malloc(buflen, M_TEMP, M_WAITOK); 2059 fdp = td->td_proc->p_fd; 2060 FILEDESC_SLOCK(fdp); 2061 cdir = fdp->fd_cdir; 2062 VREF(cdir); 2063 rdir = fdp->fd_rdir; 2064 VREF(rdir); 2065 FILEDESC_SUNLOCK(fdp); 2066 error = vn_fullpath1(td, cdir, rdir, tmpbuf, &bp, buflen); 2067 vrele(rdir); 2068 vrele(cdir); 2069 2070 if (!error) { 2071 if (bufseg == UIO_SYSSPACE) 2072 bcopy(bp, buf, strlen(bp) + 1); 2073 else 2074 error = copyout(bp, buf, strlen(bp) + 1); 2075 #ifdef KTRACE 2076 if (KTRPOINT(curthread, KTR_NAMEI)) 2077 ktrnamei(bp); 2078 #endif 2079 } 2080 free(tmpbuf, M_TEMP); 2081 return (error); 2082 } 2083 2084 /* 2085 * Thus begins the fullpath magic. 2086 */ 2087 2088 static int disablefullpath; 2089 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW, &disablefullpath, 0, 2090 "Disable the vn_fullpath function"); 2091 2092 /* 2093 * Retrieve the full filesystem path that correspond to a vnode from the name 2094 * cache (if available) 2095 */ 2096 int 2097 vn_fullpath(struct thread *td, struct vnode *vn, char **retbuf, char **freebuf) 2098 { 2099 char *buf; 2100 struct filedesc *fdp; 2101 struct vnode *rdir; 2102 int error; 2103 2104 if (disablefullpath) 2105 return (ENODEV); 2106 if (vn == NULL) 2107 return (EINVAL); 2108 2109 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 2110 fdp = td->td_proc->p_fd; 2111 FILEDESC_SLOCK(fdp); 2112 rdir = fdp->fd_rdir; 2113 VREF(rdir); 2114 FILEDESC_SUNLOCK(fdp); 2115 error = vn_fullpath1(td, vn, rdir, buf, retbuf, MAXPATHLEN); 2116 vrele(rdir); 2117 2118 if (!error) 2119 *freebuf = buf; 2120 else 2121 free(buf, M_TEMP); 2122 return (error); 2123 } 2124 2125 /* 2126 * This function is similar to vn_fullpath, but it attempts to lookup the 2127 * pathname relative to the global root mount point. This is required for the 2128 * auditing sub-system, as audited pathnames must be absolute, relative to the 2129 * global root mount point. 2130 */ 2131 int 2132 vn_fullpath_global(struct thread *td, struct vnode *vn, 2133 char **retbuf, char **freebuf) 2134 { 2135 char *buf; 2136 int error; 2137 2138 if (disablefullpath) 2139 return (ENODEV); 2140 if (vn == NULL) 2141 return (EINVAL); 2142 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 2143 error = vn_fullpath1(td, vn, rootvnode, buf, retbuf, MAXPATHLEN); 2144 if (!error) 2145 *freebuf = buf; 2146 else 2147 free(buf, M_TEMP); 2148 return (error); 2149 } 2150 2151 int 2152 vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, u_int *buflen) 2153 { 2154 struct vnode *dvp; 2155 struct namecache *ncp; 2156 struct mtx *vlp; 2157 int error; 2158 2159 vlp = VP2VNODELOCK(*vp); 2160 mtx_lock(vlp); 2161 TAILQ_FOREACH(ncp, &((*vp)->v_cache_dst), nc_dst) { 2162 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0) 2163 break; 2164 } 2165 if (ncp != NULL) { 2166 if (*buflen < ncp->nc_nlen) { 2167 mtx_unlock(vlp); 2168 vrele(*vp); 2169 counter_u64_add(numfullpathfail4, 1); 2170 error = ENOMEM; 2171 SDT_PROBE3(vfs, namecache, fullpath, return, error, 2172 vp, NULL); 2173 return (error); 2174 } 2175 *buflen -= ncp->nc_nlen; 2176 memcpy(buf + *buflen, nc_get_name(ncp), ncp->nc_nlen); 2177 SDT_PROBE3(vfs, namecache, fullpath, hit, ncp->nc_dvp, 2178 nc_get_name(ncp), vp); 2179 dvp = *vp; 2180 *vp = ncp->nc_dvp; 2181 vref(*vp); 2182 mtx_unlock(vlp); 2183 vrele(dvp); 2184 return (0); 2185 } 2186 SDT_PROBE1(vfs, namecache, fullpath, miss, vp); 2187 2188 mtx_unlock(vlp); 2189 vn_lock(*vp, LK_SHARED | LK_RETRY); 2190 error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen); 2191 vput(*vp); 2192 if (error) { 2193 counter_u64_add(numfullpathfail2, 1); 2194 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL); 2195 return (error); 2196 } 2197 2198 *vp = dvp; 2199 if (dvp->v_iflag & VI_DOOMED) { 2200 /* forced unmount */ 2201 vrele(dvp); 2202 error = ENOENT; 2203 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL); 2204 return (error); 2205 } 2206 /* 2207 * *vp has its use count incremented still. 2208 */ 2209 2210 return (0); 2211 } 2212 2213 /* 2214 * The magic behind kern___getcwd() and vn_fullpath(). 2215 */ 2216 static int 2217 vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir, 2218 char *buf, char **retbuf, u_int buflen) 2219 { 2220 int error, slash_prefixed; 2221 #ifdef KDTRACE_HOOKS 2222 struct vnode *startvp = vp; 2223 #endif 2224 struct vnode *vp1; 2225 2226 buflen--; 2227 buf[buflen] = '\0'; 2228 error = 0; 2229 slash_prefixed = 0; 2230 2231 SDT_PROBE1(vfs, namecache, fullpath, entry, vp); 2232 counter_u64_add(numfullpathcalls, 1); 2233 vref(vp); 2234 if (vp->v_type != VDIR) { 2235 error = vn_vptocnp(&vp, td->td_ucred, buf, &buflen); 2236 if (error) 2237 return (error); 2238 if (buflen == 0) { 2239 vrele(vp); 2240 return (ENOMEM); 2241 } 2242 buf[--buflen] = '/'; 2243 slash_prefixed = 1; 2244 } 2245 while (vp != rdir && vp != rootvnode) { 2246 if (vp->v_vflag & VV_ROOT) { 2247 if (vp->v_iflag & VI_DOOMED) { /* forced unmount */ 2248 vrele(vp); 2249 error = ENOENT; 2250 SDT_PROBE3(vfs, namecache, fullpath, return, 2251 error, vp, NULL); 2252 break; 2253 } 2254 vp1 = vp->v_mount->mnt_vnodecovered; 2255 vref(vp1); 2256 vrele(vp); 2257 vp = vp1; 2258 continue; 2259 } 2260 if (vp->v_type != VDIR) { 2261 vrele(vp); 2262 counter_u64_add(numfullpathfail1, 1); 2263 error = ENOTDIR; 2264 SDT_PROBE3(vfs, namecache, fullpath, return, 2265 error, vp, NULL); 2266 break; 2267 } 2268 error = vn_vptocnp(&vp, td->td_ucred, buf, &buflen); 2269 if (error) 2270 break; 2271 if (buflen == 0) { 2272 vrele(vp); 2273 error = ENOMEM; 2274 SDT_PROBE3(vfs, namecache, fullpath, return, error, 2275 startvp, NULL); 2276 break; 2277 } 2278 buf[--buflen] = '/'; 2279 slash_prefixed = 1; 2280 } 2281 if (error) 2282 return (error); 2283 if (!slash_prefixed) { 2284 if (buflen == 0) { 2285 vrele(vp); 2286 counter_u64_add(numfullpathfail4, 1); 2287 SDT_PROBE3(vfs, namecache, fullpath, return, ENOMEM, 2288 startvp, NULL); 2289 return (ENOMEM); 2290 } 2291 buf[--buflen] = '/'; 2292 } 2293 counter_u64_add(numfullpathfound, 1); 2294 vrele(vp); 2295 2296 SDT_PROBE3(vfs, namecache, fullpath, return, 0, startvp, buf + buflen); 2297 *retbuf = buf + buflen; 2298 return (0); 2299 } 2300 2301 struct vnode * 2302 vn_dir_dd_ino(struct vnode *vp) 2303 { 2304 struct namecache *ncp; 2305 struct vnode *ddvp; 2306 struct mtx *vlp; 2307 2308 ASSERT_VOP_LOCKED(vp, "vn_dir_dd_ino"); 2309 vlp = VP2VNODELOCK(vp); 2310 mtx_lock(vlp); 2311 TAILQ_FOREACH(ncp, &(vp->v_cache_dst), nc_dst) { 2312 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) 2313 continue; 2314 ddvp = ncp->nc_dvp; 2315 vhold(ddvp); 2316 mtx_unlock(vlp); 2317 if (vget(ddvp, LK_SHARED | LK_NOWAIT | LK_VNHELD, curthread)) 2318 return (NULL); 2319 return (ddvp); 2320 } 2321 mtx_unlock(vlp); 2322 return (NULL); 2323 } 2324 2325 int 2326 vn_commname(struct vnode *vp, char *buf, u_int buflen) 2327 { 2328 struct namecache *ncp; 2329 struct mtx *vlp; 2330 int l; 2331 2332 vlp = VP2VNODELOCK(vp); 2333 mtx_lock(vlp); 2334 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst) 2335 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0) 2336 break; 2337 if (ncp == NULL) { 2338 mtx_unlock(vlp); 2339 return (ENOENT); 2340 } 2341 l = min(ncp->nc_nlen, buflen - 1); 2342 memcpy(buf, nc_get_name(ncp), l); 2343 mtx_unlock(vlp); 2344 buf[l] = '\0'; 2345 return (0); 2346 } 2347 2348 /* ABI compat shims for old kernel modules. */ 2349 #undef cache_enter 2350 2351 void cache_enter(struct vnode *dvp, struct vnode *vp, 2352 struct componentname *cnp); 2353 2354 void 2355 cache_enter(struct vnode *dvp, struct vnode *vp, struct componentname *cnp) 2356 { 2357 2358 cache_enter_time(dvp, vp, cnp, NULL, NULL); 2359 } 2360 2361 /* 2362 * This function updates path string to vnode's full global path 2363 * and checks the size of the new path string against the pathlen argument. 2364 * 2365 * Requires a locked, referenced vnode. 2366 * Vnode is re-locked on success or ENODEV, otherwise unlocked. 2367 * 2368 * If sysctl debug.disablefullpath is set, ENODEV is returned, 2369 * vnode is left locked and path remain untouched. 2370 * 2371 * If vp is a directory, the call to vn_fullpath_global() always succeeds 2372 * because it falls back to the ".." lookup if the namecache lookup fails. 2373 */ 2374 int 2375 vn_path_to_global_path(struct thread *td, struct vnode *vp, char *path, 2376 u_int pathlen) 2377 { 2378 struct nameidata nd; 2379 struct vnode *vp1; 2380 char *rpath, *fbuf; 2381 int error; 2382 2383 ASSERT_VOP_ELOCKED(vp, __func__); 2384 2385 /* Return ENODEV if sysctl debug.disablefullpath==1 */ 2386 if (disablefullpath) 2387 return (ENODEV); 2388 2389 /* Construct global filesystem path from vp. */ 2390 VOP_UNLOCK(vp, 0); 2391 error = vn_fullpath_global(td, vp, &rpath, &fbuf); 2392 2393 if (error != 0) { 2394 vrele(vp); 2395 return (error); 2396 } 2397 2398 if (strlen(rpath) >= pathlen) { 2399 vrele(vp); 2400 error = ENAMETOOLONG; 2401 goto out; 2402 } 2403 2404 /* 2405 * Re-lookup the vnode by path to detect a possible rename. 2406 * As a side effect, the vnode is relocked. 2407 * If vnode was renamed, return ENOENT. 2408 */ 2409 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, 2410 UIO_SYSSPACE, path, td); 2411 error = namei(&nd); 2412 if (error != 0) { 2413 vrele(vp); 2414 goto out; 2415 } 2416 NDFREE(&nd, NDF_ONLY_PNBUF); 2417 vp1 = nd.ni_vp; 2418 vrele(vp); 2419 if (vp1 == vp) 2420 strcpy(path, rpath); 2421 else { 2422 vput(vp1); 2423 error = ENOENT; 2424 } 2425 2426 out: 2427 free(fbuf, M_TEMP); 2428 return (error); 2429 } 2430