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 *ncpp = ncp = NULL; 767 768 for (i = start; i < numneglists; i++) { 769 neglist = &neglists[i]; 770 if (TAILQ_FIRST(&neglist->nl_list) == NULL) 771 continue; 772 mtx_lock(&neglist->nl_lock); 773 ncp = TAILQ_FIRST(&neglist->nl_list); 774 if (ncp != NULL) 775 break; 776 mtx_unlock(&neglist->nl_lock); 777 } 778 779 *neglistpp = neglist; 780 *ncpp = ncp; 781 } 782 783 static void 784 cache_negative_zap_one(void) 785 { 786 struct namecache *ncp, *ncp2; 787 struct neglist *neglist; 788 struct mtx *dvlp; 789 struct rwlock *blp; 790 791 if (!mtx_trylock(&ncneg_shrink_lock)) 792 return; 793 794 mtx_lock(&ncneg_hot.nl_lock); 795 ncp = TAILQ_FIRST(&ncneg_hot.nl_list); 796 if (ncp != NULL) { 797 neglist = NCP2NEGLIST(ncp); 798 mtx_lock(&neglist->nl_lock); 799 TAILQ_REMOVE(&ncneg_hot.nl_list, ncp, nc_dst); 800 TAILQ_INSERT_TAIL(&neglist->nl_list, ncp, nc_dst); 801 ncp->nc_flag &= ~NCF_HOTNEGATIVE; 802 mtx_unlock(&neglist->nl_lock); 803 } 804 805 cache_negative_shrink_select(shrink_list_turn, &ncp, &neglist); 806 shrink_list_turn++; 807 if (shrink_list_turn == numneglists) 808 shrink_list_turn = 0; 809 if (ncp == NULL && shrink_list_turn == 0) 810 cache_negative_shrink_select(shrink_list_turn, &ncp, &neglist); 811 if (ncp == NULL) { 812 mtx_unlock(&ncneg_hot.nl_lock); 813 goto out; 814 } 815 816 MPASS(ncp->nc_flag & NCF_NEGATIVE); 817 dvlp = VP2VNODELOCK(ncp->nc_dvp); 818 blp = NCP2BUCKETLOCK(ncp); 819 mtx_unlock(&neglist->nl_lock); 820 mtx_unlock(&ncneg_hot.nl_lock); 821 mtx_lock(dvlp); 822 rw_wlock(blp); 823 mtx_lock(&ncneg_hot.nl_lock); 824 mtx_lock(&neglist->nl_lock); 825 ncp2 = TAILQ_FIRST(&neglist->nl_list); 826 if (ncp != ncp2 || dvlp != VP2VNODELOCK(ncp2->nc_dvp) || 827 blp != NCP2BUCKETLOCK(ncp2) || !(ncp2->nc_flag & NCF_NEGATIVE)) { 828 ncp = NULL; 829 goto out_unlock_all; 830 } 831 SDT_PROBE3(vfs, namecache, shrink_negative, done, ncp->nc_dvp, 832 nc_get_name(ncp), ncp->nc_neghits); 833 834 cache_zap_locked(ncp, true); 835 out_unlock_all: 836 mtx_unlock(&neglist->nl_lock); 837 mtx_unlock(&ncneg_hot.nl_lock); 838 rw_wunlock(blp); 839 mtx_unlock(dvlp); 840 out: 841 mtx_unlock(&ncneg_shrink_lock); 842 cache_free(ncp); 843 } 844 845 /* 846 * cache_zap_locked(): 847 * 848 * Removes a namecache entry from cache, whether it contains an actual 849 * pointer to a vnode or if it is just a negative cache entry. 850 */ 851 static void 852 cache_zap_locked(struct namecache *ncp, bool neg_locked) 853 { 854 855 if (!(ncp->nc_flag & NCF_NEGATIVE)) 856 cache_assert_vnode_locked(ncp->nc_vp); 857 cache_assert_vnode_locked(ncp->nc_dvp); 858 cache_assert_bucket_locked(ncp, RA_WLOCKED); 859 860 CTR2(KTR_VFS, "cache_zap(%p) vp %p", ncp, 861 (ncp->nc_flag & NCF_NEGATIVE) ? NULL : ncp->nc_vp); 862 if (!(ncp->nc_flag & NCF_NEGATIVE)) { 863 SDT_PROBE3(vfs, namecache, zap, done, ncp->nc_dvp, 864 nc_get_name(ncp), ncp->nc_vp); 865 } else { 866 SDT_PROBE3(vfs, namecache, zap_negative, done, ncp->nc_dvp, 867 nc_get_name(ncp), ncp->nc_neghits); 868 } 869 LIST_REMOVE(ncp, nc_hash); 870 if (!(ncp->nc_flag & NCF_NEGATIVE)) { 871 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_dst); 872 if (ncp == ncp->nc_vp->v_cache_dd) 873 ncp->nc_vp->v_cache_dd = NULL; 874 } else { 875 cache_negative_remove(ncp, neg_locked); 876 } 877 if (ncp->nc_flag & NCF_ISDOTDOT) { 878 if (ncp == ncp->nc_dvp->v_cache_dd) 879 ncp->nc_dvp->v_cache_dd = NULL; 880 } else { 881 LIST_REMOVE(ncp, nc_src); 882 if (LIST_EMPTY(&ncp->nc_dvp->v_cache_src)) { 883 ncp->nc_flag |= NCF_DVDROP; 884 atomic_subtract_rel_long(&numcachehv, 1); 885 } 886 } 887 atomic_subtract_rel_long(&numcache, 1); 888 } 889 890 static void 891 cache_zap_negative_locked_vnode_kl(struct namecache *ncp, struct vnode *vp) 892 { 893 struct rwlock *blp; 894 895 MPASS(ncp->nc_dvp == vp); 896 MPASS(ncp->nc_flag & NCF_NEGATIVE); 897 cache_assert_vnode_locked(vp); 898 899 blp = NCP2BUCKETLOCK(ncp); 900 rw_wlock(blp); 901 cache_zap_locked(ncp, false); 902 rw_wunlock(blp); 903 } 904 905 static bool 906 cache_zap_locked_vnode_kl2(struct namecache *ncp, struct vnode *vp, 907 struct mtx **vlpp) 908 { 909 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock; 910 struct rwlock *blp; 911 912 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp); 913 cache_assert_vnode_locked(vp); 914 915 if (ncp->nc_flag & NCF_NEGATIVE) { 916 if (*vlpp != NULL) { 917 mtx_unlock(*vlpp); 918 *vlpp = NULL; 919 } 920 cache_zap_negative_locked_vnode_kl(ncp, vp); 921 return (true); 922 } 923 924 pvlp = VP2VNODELOCK(vp); 925 blp = NCP2BUCKETLOCK(ncp); 926 vlp1 = VP2VNODELOCK(ncp->nc_dvp); 927 vlp2 = VP2VNODELOCK(ncp->nc_vp); 928 929 if (*vlpp == vlp1 || *vlpp == vlp2) { 930 to_unlock = *vlpp; 931 *vlpp = NULL; 932 } else { 933 if (*vlpp != NULL) { 934 mtx_unlock(*vlpp); 935 *vlpp = NULL; 936 } 937 cache_sort(&vlp1, &vlp2); 938 if (vlp1 == pvlp) { 939 mtx_lock(vlp2); 940 to_unlock = vlp2; 941 } else { 942 if (!mtx_trylock(vlp1)) 943 goto out_relock; 944 to_unlock = vlp1; 945 } 946 } 947 rw_wlock(blp); 948 cache_zap_locked(ncp, false); 949 rw_wunlock(blp); 950 if (to_unlock != NULL) 951 mtx_unlock(to_unlock); 952 return (true); 953 954 out_relock: 955 mtx_unlock(vlp2); 956 mtx_lock(vlp1); 957 mtx_lock(vlp2); 958 MPASS(*vlpp == NULL); 959 *vlpp = vlp1; 960 return (false); 961 } 962 963 static int 964 cache_zap_locked_vnode(struct namecache *ncp, struct vnode *vp) 965 { 966 struct mtx *pvlp, *vlp1, *vlp2, *to_unlock; 967 struct rwlock *blp; 968 int error = 0; 969 970 MPASS(vp == ncp->nc_dvp || vp == ncp->nc_vp); 971 cache_assert_vnode_locked(vp); 972 973 pvlp = VP2VNODELOCK(vp); 974 if (ncp->nc_flag & NCF_NEGATIVE) { 975 cache_zap_negative_locked_vnode_kl(ncp, vp); 976 goto out; 977 } 978 979 blp = NCP2BUCKETLOCK(ncp); 980 vlp1 = VP2VNODELOCK(ncp->nc_dvp); 981 vlp2 = VP2VNODELOCK(ncp->nc_vp); 982 cache_sort(&vlp1, &vlp2); 983 if (vlp1 == pvlp) { 984 mtx_lock(vlp2); 985 to_unlock = vlp2; 986 } else { 987 if (!mtx_trylock(vlp1)) { 988 error = EAGAIN; 989 goto out; 990 } 991 to_unlock = vlp1; 992 } 993 rw_wlock(blp); 994 cache_zap_locked(ncp, false); 995 rw_wunlock(blp); 996 mtx_unlock(to_unlock); 997 out: 998 mtx_unlock(pvlp); 999 return (error); 1000 } 1001 1002 static int 1003 cache_zap_rlocked_bucket(struct namecache *ncp, struct rwlock *blp) 1004 { 1005 struct mtx *dvlp, *vlp; 1006 1007 cache_assert_bucket_locked(ncp, RA_RLOCKED); 1008 1009 dvlp = VP2VNODELOCK(ncp->nc_dvp); 1010 vlp = NULL; 1011 if (!(ncp->nc_flag & NCF_NEGATIVE)) 1012 vlp = VP2VNODELOCK(ncp->nc_vp); 1013 if (cache_trylock_vnodes(dvlp, vlp) == 0) { 1014 rw_runlock(blp); 1015 rw_wlock(blp); 1016 cache_zap_locked(ncp, false); 1017 rw_wunlock(blp); 1018 cache_unlock_vnodes(dvlp, vlp); 1019 return (0); 1020 } 1021 1022 rw_runlock(blp); 1023 return (EAGAIN); 1024 } 1025 1026 static int 1027 cache_zap_wlocked_bucket_kl(struct namecache *ncp, struct rwlock *blp, 1028 struct mtx **vlpp1, struct mtx **vlpp2) 1029 { 1030 struct mtx *dvlp, *vlp; 1031 1032 cache_assert_bucket_locked(ncp, RA_WLOCKED); 1033 1034 dvlp = VP2VNODELOCK(ncp->nc_dvp); 1035 vlp = NULL; 1036 if (!(ncp->nc_flag & NCF_NEGATIVE)) 1037 vlp = VP2VNODELOCK(ncp->nc_vp); 1038 cache_sort(&dvlp, &vlp); 1039 1040 if (*vlpp1 == dvlp && *vlpp2 == vlp) { 1041 cache_zap_locked(ncp, false); 1042 cache_unlock_vnodes(dvlp, vlp); 1043 *vlpp1 = NULL; 1044 *vlpp2 = NULL; 1045 return (0); 1046 } 1047 1048 if (*vlpp1 != NULL) 1049 mtx_unlock(*vlpp1); 1050 if (*vlpp2 != NULL) 1051 mtx_unlock(*vlpp2); 1052 *vlpp1 = NULL; 1053 *vlpp2 = NULL; 1054 1055 if (cache_trylock_vnodes(dvlp, vlp) == 0) { 1056 cache_zap_locked(ncp, false); 1057 cache_unlock_vnodes(dvlp, vlp); 1058 return (0); 1059 } 1060 1061 rw_wunlock(blp); 1062 *vlpp1 = dvlp; 1063 *vlpp2 = vlp; 1064 if (*vlpp1 != NULL) 1065 mtx_lock(*vlpp1); 1066 mtx_lock(*vlpp2); 1067 rw_wlock(blp); 1068 return (EAGAIN); 1069 } 1070 1071 static void 1072 cache_lookup_unlock(struct rwlock *blp, struct mtx *vlp) 1073 { 1074 1075 if (blp != NULL) { 1076 rw_runlock(blp); 1077 mtx_assert(vlp, MA_NOTOWNED); 1078 } else { 1079 mtx_unlock(vlp); 1080 } 1081 } 1082 1083 /* 1084 * Lookup an entry in the cache 1085 * 1086 * Lookup is called with dvp pointing to the directory to search, 1087 * cnp pointing to the name of the entry being sought. If the lookup 1088 * succeeds, the vnode is returned in *vpp, and a status of -1 is 1089 * returned. If the lookup determines that the name does not exist 1090 * (negative caching), a status of ENOENT is returned. If the lookup 1091 * fails, a status of zero is returned. If the directory vnode is 1092 * recycled out from under us due to a forced unmount, a status of 1093 * ENOENT is returned. 1094 * 1095 * vpp is locked and ref'd on return. If we're looking up DOTDOT, dvp is 1096 * unlocked. If we're looking up . an extra ref is taken, but the lock is 1097 * not recursively acquired. 1098 */ 1099 1100 int 1101 cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp, 1102 struct timespec *tsp, int *ticksp) 1103 { 1104 struct namecache *ncp; 1105 struct rwlock *blp; 1106 struct mtx *dvlp, *dvlp2; 1107 uint32_t hash; 1108 int error, ltype; 1109 1110 if (!doingcache) { 1111 cnp->cn_flags &= ~MAKEENTRY; 1112 return (0); 1113 } 1114 retry: 1115 blp = NULL; 1116 dvlp = VP2VNODELOCK(dvp); 1117 error = 0; 1118 counter_u64_add(numcalls, 1); 1119 1120 if (cnp->cn_nameptr[0] == '.') { 1121 if (cnp->cn_namelen == 1) { 1122 *vpp = dvp; 1123 CTR2(KTR_VFS, "cache_lookup(%p, %s) found via .", 1124 dvp, cnp->cn_nameptr); 1125 counter_u64_add(dothits, 1); 1126 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, ".", *vpp); 1127 if (tsp != NULL) 1128 timespecclear(tsp); 1129 if (ticksp != NULL) 1130 *ticksp = ticks; 1131 VREF(*vpp); 1132 /* 1133 * When we lookup "." we still can be asked to lock it 1134 * differently... 1135 */ 1136 ltype = cnp->cn_lkflags & LK_TYPE_MASK; 1137 if (ltype != VOP_ISLOCKED(*vpp)) { 1138 if (ltype == LK_EXCLUSIVE) { 1139 vn_lock(*vpp, LK_UPGRADE | LK_RETRY); 1140 if ((*vpp)->v_iflag & VI_DOOMED) { 1141 /* forced unmount */ 1142 vrele(*vpp); 1143 *vpp = NULL; 1144 return (ENOENT); 1145 } 1146 } else 1147 vn_lock(*vpp, LK_DOWNGRADE | LK_RETRY); 1148 } 1149 return (-1); 1150 } 1151 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') { 1152 counter_u64_add(dotdothits, 1); 1153 dvlp2 = NULL; 1154 mtx_lock(dvlp); 1155 retry_dotdot: 1156 ncp = dvp->v_cache_dd; 1157 if (ncp == NULL) { 1158 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, 1159 "..", NULL); 1160 mtx_unlock(dvlp); 1161 return (0); 1162 } 1163 if ((cnp->cn_flags & MAKEENTRY) == 0) { 1164 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) { 1165 if (ncp->nc_dvp != dvp) 1166 panic("dvp %p v_cache_dd %p\n", dvp, ncp); 1167 if (!cache_zap_locked_vnode_kl2(ncp, 1168 dvp, &dvlp2)) 1169 goto retry_dotdot; 1170 MPASS(dvp->v_cache_dd == NULL); 1171 mtx_unlock(dvlp); 1172 if (dvlp2 != NULL) 1173 mtx_unlock(dvlp2); 1174 cache_free(ncp); 1175 } else { 1176 dvp->v_cache_dd = NULL; 1177 mtx_unlock(dvlp); 1178 if (dvlp2 != NULL) 1179 mtx_unlock(dvlp2); 1180 } 1181 return (0); 1182 } 1183 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) { 1184 if (ncp->nc_flag & NCF_NEGATIVE) 1185 *vpp = NULL; 1186 else 1187 *vpp = ncp->nc_vp; 1188 } else 1189 *vpp = ncp->nc_dvp; 1190 /* Return failure if negative entry was found. */ 1191 if (*vpp == NULL) 1192 goto negative_success; 1193 CTR3(KTR_VFS, "cache_lookup(%p, %s) found %p via ..", 1194 dvp, cnp->cn_nameptr, *vpp); 1195 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, "..", 1196 *vpp); 1197 cache_out_ts(ncp, tsp, ticksp); 1198 if ((ncp->nc_flag & (NCF_ISDOTDOT | NCF_DTS)) == 1199 NCF_DTS && tsp != NULL) 1200 *tsp = ((struct namecache_ts *)ncp)-> 1201 nc_dotdottime; 1202 goto success; 1203 } 1204 } 1205 1206 hash = cache_get_hash(cnp->cn_nameptr, cnp->cn_namelen, dvp); 1207 blp = HASH2BUCKETLOCK(hash); 1208 rw_rlock(blp); 1209 1210 LIST_FOREACH(ncp, (NCHHASH(hash)), nc_hash) { 1211 counter_u64_add(numchecks, 1); 1212 if (ncp->nc_dvp == dvp && ncp->nc_nlen == cnp->cn_namelen && 1213 !bcmp(nc_get_name(ncp), cnp->cn_nameptr, ncp->nc_nlen)) 1214 break; 1215 } 1216 1217 /* We failed to find an entry */ 1218 if (ncp == NULL) { 1219 SDT_PROBE3(vfs, namecache, lookup, miss, dvp, cnp->cn_nameptr, 1220 NULL); 1221 if ((cnp->cn_flags & MAKEENTRY) == 0) { 1222 counter_u64_add(nummisszap, 1); 1223 } else { 1224 counter_u64_add(nummiss, 1); 1225 } 1226 goto unlock; 1227 } 1228 1229 /* We don't want to have an entry, so dump it */ 1230 if ((cnp->cn_flags & MAKEENTRY) == 0) { 1231 counter_u64_add(numposzaps, 1); 1232 goto zap_and_exit; 1233 } 1234 1235 /* We found a "positive" match, return the vnode */ 1236 if (!(ncp->nc_flag & NCF_NEGATIVE)) { 1237 counter_u64_add(numposhits, 1); 1238 *vpp = ncp->nc_vp; 1239 CTR4(KTR_VFS, "cache_lookup(%p, %s) found %p via ncp %p", 1240 dvp, cnp->cn_nameptr, *vpp, ncp); 1241 SDT_PROBE3(vfs, namecache, lookup, hit, dvp, nc_get_name(ncp), 1242 *vpp); 1243 cache_out_ts(ncp, tsp, ticksp); 1244 goto success; 1245 } 1246 1247 negative_success: 1248 /* We found a negative match, and want to create it, so purge */ 1249 if (cnp->cn_nameiop == CREATE) { 1250 counter_u64_add(numnegzaps, 1); 1251 goto zap_and_exit; 1252 } 1253 1254 counter_u64_add(numneghits, 1); 1255 cache_negative_hit(ncp); 1256 if (ncp->nc_flag & NCF_WHITE) 1257 cnp->cn_flags |= ISWHITEOUT; 1258 SDT_PROBE2(vfs, namecache, lookup, hit__negative, dvp, 1259 nc_get_name(ncp)); 1260 cache_out_ts(ncp, tsp, ticksp); 1261 cache_lookup_unlock(blp, dvlp); 1262 return (ENOENT); 1263 1264 success: 1265 /* 1266 * On success we return a locked and ref'd vnode as per the lookup 1267 * protocol. 1268 */ 1269 MPASS(dvp != *vpp); 1270 ltype = 0; /* silence gcc warning */ 1271 if (cnp->cn_flags & ISDOTDOT) { 1272 ltype = VOP_ISLOCKED(dvp); 1273 VOP_UNLOCK(dvp, 0); 1274 } 1275 vhold(*vpp); 1276 cache_lookup_unlock(blp, dvlp); 1277 error = vget(*vpp, cnp->cn_lkflags | LK_VNHELD, cnp->cn_thread); 1278 if (cnp->cn_flags & ISDOTDOT) { 1279 vn_lock(dvp, ltype | LK_RETRY); 1280 if (dvp->v_iflag & VI_DOOMED) { 1281 if (error == 0) 1282 vput(*vpp); 1283 *vpp = NULL; 1284 return (ENOENT); 1285 } 1286 } 1287 if (error) { 1288 *vpp = NULL; 1289 goto retry; 1290 } 1291 if ((cnp->cn_flags & ISLASTCN) && 1292 (cnp->cn_lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE) { 1293 ASSERT_VOP_ELOCKED(*vpp, "cache_lookup"); 1294 } 1295 return (-1); 1296 1297 unlock: 1298 cache_lookup_unlock(blp, dvlp); 1299 return (0); 1300 1301 zap_and_exit: 1302 if (blp != NULL) 1303 error = cache_zap_rlocked_bucket(ncp, blp); 1304 else 1305 error = cache_zap_locked_vnode(ncp, dvp); 1306 if (error != 0) { 1307 zap_and_exit_bucket_fail++; 1308 cache_maybe_yield(); 1309 goto retry; 1310 } 1311 cache_free(ncp); 1312 return (0); 1313 } 1314 1315 struct celockstate { 1316 struct mtx *vlp[3]; 1317 struct rwlock *blp[2]; 1318 }; 1319 CTASSERT((nitems(((struct celockstate *)0)->vlp) == 3)); 1320 CTASSERT((nitems(((struct celockstate *)0)->blp) == 2)); 1321 1322 static inline void 1323 cache_celockstate_init(struct celockstate *cel) 1324 { 1325 1326 bzero(cel, sizeof(*cel)); 1327 } 1328 1329 static void 1330 cache_lock_vnodes_cel(struct celockstate *cel, struct vnode *vp, 1331 struct vnode *dvp) 1332 { 1333 struct mtx *vlp1, *vlp2; 1334 1335 MPASS(cel->vlp[0] == NULL); 1336 MPASS(cel->vlp[1] == NULL); 1337 MPASS(cel->vlp[2] == NULL); 1338 1339 MPASS(vp != NULL || dvp != NULL); 1340 1341 vlp1 = VP2VNODELOCK(vp); 1342 vlp2 = VP2VNODELOCK(dvp); 1343 cache_sort(&vlp1, &vlp2); 1344 1345 if (vlp1 != NULL) { 1346 mtx_lock(vlp1); 1347 cel->vlp[0] = vlp1; 1348 } 1349 mtx_lock(vlp2); 1350 cel->vlp[1] = vlp2; 1351 } 1352 1353 static void 1354 cache_unlock_vnodes_cel(struct celockstate *cel) 1355 { 1356 1357 MPASS(cel->vlp[0] != NULL || cel->vlp[1] != NULL); 1358 1359 if (cel->vlp[0] != NULL) 1360 mtx_unlock(cel->vlp[0]); 1361 if (cel->vlp[1] != NULL) 1362 mtx_unlock(cel->vlp[1]); 1363 if (cel->vlp[2] != NULL) 1364 mtx_unlock(cel->vlp[2]); 1365 } 1366 1367 static bool 1368 cache_lock_vnodes_cel_3(struct celockstate *cel, struct vnode *vp) 1369 { 1370 struct mtx *vlp; 1371 bool ret; 1372 1373 cache_assert_vlp_locked(cel->vlp[0]); 1374 cache_assert_vlp_locked(cel->vlp[1]); 1375 MPASS(cel->vlp[2] == NULL); 1376 1377 vlp = VP2VNODELOCK(vp); 1378 MPASS(vlp != NULL); 1379 1380 ret = true; 1381 if (vlp >= cel->vlp[1]) { 1382 mtx_lock(vlp); 1383 } else { 1384 if (mtx_trylock(vlp)) 1385 goto out; 1386 cache_lock_vnodes_cel_3_failures++; 1387 cache_unlock_vnodes_cel(cel); 1388 if (vlp < cel->vlp[0]) { 1389 mtx_lock(vlp); 1390 mtx_lock(cel->vlp[0]); 1391 mtx_lock(cel->vlp[1]); 1392 } else { 1393 if (cel->vlp[0] != NULL) 1394 mtx_lock(cel->vlp[0]); 1395 mtx_lock(vlp); 1396 mtx_lock(cel->vlp[1]); 1397 } 1398 ret = false; 1399 } 1400 out: 1401 cel->vlp[2] = vlp; 1402 return (ret); 1403 } 1404 1405 static void 1406 cache_lock_buckets_cel(struct celockstate *cel, struct rwlock *blp1, 1407 struct rwlock *blp2) 1408 { 1409 1410 MPASS(cel->blp[0] == NULL); 1411 MPASS(cel->blp[1] == NULL); 1412 1413 cache_sort(&blp1, &blp2); 1414 1415 if (blp1 != NULL) { 1416 rw_wlock(blp1); 1417 cel->blp[0] = blp1; 1418 } 1419 rw_wlock(blp2); 1420 cel->blp[1] = blp2; 1421 } 1422 1423 static void 1424 cache_unlock_buckets_cel(struct celockstate *cel) 1425 { 1426 1427 if (cel->blp[0] != NULL) 1428 rw_wunlock(cel->blp[0]); 1429 rw_wunlock(cel->blp[1]); 1430 } 1431 1432 /* 1433 * Lock part of the cache affected by the insertion. 1434 * 1435 * This means vnodelocks for dvp, vp and the relevant bucketlock. 1436 * However, insertion can result in removal of an old entry. In this 1437 * case we have an additional vnode and bucketlock pair to lock. If the 1438 * entry is negative, ncelock is locked instead of the vnode. 1439 * 1440 * That is, in the worst case we have to lock 3 vnodes and 2 bucketlocks, while 1441 * preserving the locking order (smaller address first). 1442 */ 1443 static void 1444 cache_enter_lock(struct celockstate *cel, struct vnode *dvp, struct vnode *vp, 1445 uint32_t hash) 1446 { 1447 struct namecache *ncp; 1448 struct rwlock *blps[2]; 1449 1450 blps[0] = HASH2BUCKETLOCK(hash); 1451 for (;;) { 1452 blps[1] = NULL; 1453 cache_lock_vnodes_cel(cel, dvp, vp); 1454 if (vp == NULL || vp->v_type != VDIR) 1455 break; 1456 ncp = vp->v_cache_dd; 1457 if (ncp == NULL) 1458 break; 1459 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0) 1460 break; 1461 MPASS(ncp->nc_dvp == vp); 1462 blps[1] = NCP2BUCKETLOCK(ncp); 1463 if (ncp->nc_flag & NCF_NEGATIVE) 1464 break; 1465 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp)) 1466 break; 1467 /* 1468 * All vnodes got re-locked. Re-validate the state and if 1469 * nothing changed we are done. Otherwise restart. 1470 */ 1471 if (ncp == vp->v_cache_dd && 1472 (ncp->nc_flag & NCF_ISDOTDOT) != 0 && 1473 blps[1] == NCP2BUCKETLOCK(ncp) && 1474 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2]) 1475 break; 1476 cache_unlock_vnodes_cel(cel); 1477 cel->vlp[0] = NULL; 1478 cel->vlp[1] = NULL; 1479 cel->vlp[2] = NULL; 1480 } 1481 cache_lock_buckets_cel(cel, blps[0], blps[1]); 1482 } 1483 1484 static void 1485 cache_enter_lock_dd(struct celockstate *cel, struct vnode *dvp, struct vnode *vp, 1486 uint32_t hash) 1487 { 1488 struct namecache *ncp; 1489 struct rwlock *blps[2]; 1490 1491 blps[0] = HASH2BUCKETLOCK(hash); 1492 for (;;) { 1493 blps[1] = NULL; 1494 cache_lock_vnodes_cel(cel, dvp, vp); 1495 ncp = dvp->v_cache_dd; 1496 if (ncp == NULL) 1497 break; 1498 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0) 1499 break; 1500 MPASS(ncp->nc_dvp == dvp); 1501 blps[1] = NCP2BUCKETLOCK(ncp); 1502 if (ncp->nc_flag & NCF_NEGATIVE) 1503 break; 1504 if (cache_lock_vnodes_cel_3(cel, ncp->nc_vp)) 1505 break; 1506 if (ncp == dvp->v_cache_dd && 1507 (ncp->nc_flag & NCF_ISDOTDOT) != 0 && 1508 blps[1] == NCP2BUCKETLOCK(ncp) && 1509 VP2VNODELOCK(ncp->nc_vp) == cel->vlp[2]) 1510 break; 1511 cache_unlock_vnodes_cel(cel); 1512 cel->vlp[0] = NULL; 1513 cel->vlp[1] = NULL; 1514 cel->vlp[2] = NULL; 1515 } 1516 cache_lock_buckets_cel(cel, blps[0], blps[1]); 1517 } 1518 1519 static void 1520 cache_enter_unlock(struct celockstate *cel) 1521 { 1522 1523 cache_unlock_buckets_cel(cel); 1524 cache_unlock_vnodes_cel(cel); 1525 } 1526 1527 /* 1528 * Add an entry to the cache. 1529 */ 1530 void 1531 cache_enter_time(struct vnode *dvp, struct vnode *vp, struct componentname *cnp, 1532 struct timespec *tsp, struct timespec *dtsp) 1533 { 1534 struct celockstate cel; 1535 struct namecache *ncp, *n2, *ndd; 1536 struct namecache_ts *n3; 1537 struct nchashhead *ncpp; 1538 struct neglist *neglist; 1539 uint32_t hash; 1540 int flag; 1541 int len; 1542 bool neg_locked; 1543 1544 CTR3(KTR_VFS, "cache_enter(%p, %p, %s)", dvp, vp, cnp->cn_nameptr); 1545 VNASSERT(vp == NULL || (vp->v_iflag & VI_DOOMED) == 0, vp, 1546 ("cache_enter: Adding a doomed vnode")); 1547 VNASSERT(dvp == NULL || (dvp->v_iflag & VI_DOOMED) == 0, dvp, 1548 ("cache_enter: Doomed vnode used as src")); 1549 1550 if (!doingcache) 1551 return; 1552 1553 /* 1554 * Avoid blowout in namecache entries. 1555 */ 1556 if (numcache >= desiredvnodes * ncsizefactor) 1557 return; 1558 1559 cache_celockstate_init(&cel); 1560 ndd = NULL; 1561 flag = 0; 1562 if (cnp->cn_nameptr[0] == '.') { 1563 if (cnp->cn_namelen == 1) 1564 return; 1565 if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') { 1566 len = cnp->cn_namelen; 1567 hash = cache_get_hash(cnp->cn_nameptr, len, dvp); 1568 cache_enter_lock_dd(&cel, dvp, vp, hash); 1569 /* 1570 * If dotdot entry already exists, just retarget it 1571 * to new parent vnode, otherwise continue with new 1572 * namecache entry allocation. 1573 */ 1574 if ((ncp = dvp->v_cache_dd) != NULL && 1575 ncp->nc_flag & NCF_ISDOTDOT) { 1576 KASSERT(ncp->nc_dvp == dvp, 1577 ("wrong isdotdot parent")); 1578 neg_locked = false; 1579 if (ncp->nc_flag & NCF_NEGATIVE || vp == NULL) { 1580 neglist = NCP2NEGLIST(ncp); 1581 mtx_lock(&ncneg_hot.nl_lock); 1582 mtx_lock(&neglist->nl_lock); 1583 neg_locked = true; 1584 } 1585 if (!(ncp->nc_flag & NCF_NEGATIVE)) { 1586 TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, 1587 ncp, nc_dst); 1588 } else { 1589 cache_negative_remove(ncp, true); 1590 } 1591 if (vp != NULL) { 1592 TAILQ_INSERT_HEAD(&vp->v_cache_dst, 1593 ncp, nc_dst); 1594 ncp->nc_flag &= ~(NCF_NEGATIVE|NCF_HOTNEGATIVE); 1595 } else { 1596 ncp->nc_flag &= ~(NCF_HOTNEGATIVE); 1597 ncp->nc_flag |= NCF_NEGATIVE; 1598 cache_negative_insert(ncp, true); 1599 } 1600 if (neg_locked) { 1601 mtx_unlock(&neglist->nl_lock); 1602 mtx_unlock(&ncneg_hot.nl_lock); 1603 } 1604 ncp->nc_vp = vp; 1605 cache_enter_unlock(&cel); 1606 return; 1607 } 1608 dvp->v_cache_dd = NULL; 1609 cache_enter_unlock(&cel); 1610 cache_celockstate_init(&cel); 1611 SDT_PROBE3(vfs, namecache, enter, done, dvp, "..", vp); 1612 flag = NCF_ISDOTDOT; 1613 } 1614 } 1615 1616 /* 1617 * Calculate the hash key and setup as much of the new 1618 * namecache entry as possible before acquiring the lock. 1619 */ 1620 ncp = cache_alloc(cnp->cn_namelen, tsp != NULL); 1621 ncp->nc_flag = flag; 1622 ncp->nc_vp = vp; 1623 if (vp == NULL) 1624 ncp->nc_flag |= NCF_NEGATIVE; 1625 ncp->nc_dvp = dvp; 1626 if (tsp != NULL) { 1627 n3 = (struct namecache_ts *)ncp; 1628 n3->nc_time = *tsp; 1629 n3->nc_ticks = ticks; 1630 n3->nc_flag |= NCF_TS; 1631 if (dtsp != NULL) { 1632 n3->nc_dotdottime = *dtsp; 1633 n3->nc_flag |= NCF_DTS; 1634 } 1635 } 1636 len = ncp->nc_nlen = cnp->cn_namelen; 1637 hash = cache_get_hash(cnp->cn_nameptr, len, dvp); 1638 strlcpy(nc_get_name(ncp), cnp->cn_nameptr, len + 1); 1639 cache_enter_lock(&cel, dvp, vp, hash); 1640 1641 /* 1642 * See if this vnode or negative entry is already in the cache 1643 * with this name. This can happen with concurrent lookups of 1644 * the same path name. 1645 */ 1646 ncpp = NCHHASH(hash); 1647 LIST_FOREACH(n2, ncpp, nc_hash) { 1648 if (n2->nc_dvp == dvp && 1649 n2->nc_nlen == cnp->cn_namelen && 1650 !bcmp(nc_get_name(n2), cnp->cn_nameptr, n2->nc_nlen)) { 1651 if (tsp != NULL) { 1652 KASSERT((n2->nc_flag & NCF_TS) != 0, 1653 ("no NCF_TS")); 1654 n3 = (struct namecache_ts *)n2; 1655 n3->nc_time = 1656 ((struct namecache_ts *)ncp)->nc_time; 1657 n3->nc_ticks = 1658 ((struct namecache_ts *)ncp)->nc_ticks; 1659 if (dtsp != NULL) { 1660 n3->nc_dotdottime = 1661 ((struct namecache_ts *)ncp)-> 1662 nc_dotdottime; 1663 if (ncp->nc_flag & NCF_NEGATIVE) 1664 mtx_lock(&ncneg_hot.nl_lock); 1665 n3->nc_flag |= NCF_DTS; 1666 if (ncp->nc_flag & NCF_NEGATIVE) 1667 mtx_unlock(&ncneg_hot.nl_lock); 1668 } 1669 } 1670 goto out_unlock_free; 1671 } 1672 } 1673 1674 if (flag == NCF_ISDOTDOT) { 1675 /* 1676 * See if we are trying to add .. entry, but some other lookup 1677 * has populated v_cache_dd pointer already. 1678 */ 1679 if (dvp->v_cache_dd != NULL) 1680 goto out_unlock_free; 1681 KASSERT(vp == NULL || vp->v_type == VDIR, 1682 ("wrong vnode type %p", vp)); 1683 dvp->v_cache_dd = ncp; 1684 } 1685 1686 atomic_add_rel_long(&numcache, 1); 1687 if (vp != NULL) { 1688 if (vp->v_type == VDIR) { 1689 if (flag != NCF_ISDOTDOT) { 1690 /* 1691 * For this case, the cache entry maps both the 1692 * directory name in it and the name ".." for the 1693 * directory's parent. 1694 */ 1695 if ((ndd = vp->v_cache_dd) != NULL) { 1696 if ((ndd->nc_flag & NCF_ISDOTDOT) != 0) 1697 cache_zap_locked(ndd, false); 1698 else 1699 ndd = NULL; 1700 } 1701 vp->v_cache_dd = ncp; 1702 } 1703 } else { 1704 vp->v_cache_dd = NULL; 1705 } 1706 } 1707 1708 if (flag != NCF_ISDOTDOT) { 1709 if (LIST_EMPTY(&dvp->v_cache_src)) { 1710 vhold(dvp); 1711 atomic_add_rel_long(&numcachehv, 1); 1712 } 1713 LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src); 1714 } 1715 1716 /* 1717 * Insert the new namecache entry into the appropriate chain 1718 * within the cache entries table. 1719 */ 1720 LIST_INSERT_HEAD(ncpp, ncp, nc_hash); 1721 1722 /* 1723 * If the entry is "negative", we place it into the 1724 * "negative" cache queue, otherwise, we place it into the 1725 * destination vnode's cache entries queue. 1726 */ 1727 if (vp != NULL) { 1728 TAILQ_INSERT_HEAD(&vp->v_cache_dst, ncp, nc_dst); 1729 SDT_PROBE3(vfs, namecache, enter, done, dvp, nc_get_name(ncp), 1730 vp); 1731 } else { 1732 if (cnp->cn_flags & ISWHITEOUT) 1733 ncp->nc_flag |= NCF_WHITE; 1734 cache_negative_insert(ncp, false); 1735 SDT_PROBE2(vfs, namecache, enter_negative, done, dvp, 1736 nc_get_name(ncp)); 1737 } 1738 cache_enter_unlock(&cel); 1739 if (numneg * ncnegfactor > numcache) 1740 cache_negative_zap_one(); 1741 cache_free(ndd); 1742 return; 1743 out_unlock_free: 1744 cache_enter_unlock(&cel); 1745 cache_free(ncp); 1746 return; 1747 } 1748 1749 static u_int 1750 cache_roundup_2(u_int val) 1751 { 1752 u_int res; 1753 1754 for (res = 1; res <= val; res <<= 1) 1755 continue; 1756 1757 return (res); 1758 } 1759 1760 /* 1761 * Name cache initialization, from vfs_init() when we are booting 1762 */ 1763 static void 1764 nchinit(void *dummy __unused) 1765 { 1766 u_int i; 1767 1768 cache_zone_small = uma_zcreate("S VFS Cache", 1769 sizeof(struct namecache) + CACHE_PATH_CUTOFF + 1, 1770 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT); 1771 cache_zone_small_ts = uma_zcreate("STS VFS Cache", 1772 sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1, 1773 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT); 1774 cache_zone_large = uma_zcreate("L VFS Cache", 1775 sizeof(struct namecache) + NAME_MAX + 1, 1776 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT); 1777 cache_zone_large_ts = uma_zcreate("LTS VFS Cache", 1778 sizeof(struct namecache_ts) + NAME_MAX + 1, 1779 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_ZINIT); 1780 1781 nchashtbl = hashinit(desiredvnodes * 2, M_VFSCACHE, &nchash); 1782 numbucketlocks = cache_roundup_2(mp_ncpus * 64); 1783 if (numbucketlocks > nchash + 1) 1784 numbucketlocks = nchash + 1; 1785 bucketlocks = malloc(sizeof(*bucketlocks) * numbucketlocks, M_VFSCACHE, 1786 M_WAITOK | M_ZERO); 1787 for (i = 0; i < numbucketlocks; i++) 1788 rw_init_flags(&bucketlocks[i], "ncbuc", RW_DUPOK | RW_RECURSE); 1789 numvnodelocks = cache_roundup_2(mp_ncpus * 64); 1790 vnodelocks = malloc(sizeof(*vnodelocks) * numvnodelocks, M_VFSCACHE, 1791 M_WAITOK | M_ZERO); 1792 for (i = 0; i < numvnodelocks; i++) 1793 mtx_init(&vnodelocks[i], "ncvn", NULL, MTX_DUPOK | MTX_RECURSE); 1794 ncpurgeminvnodes = numbucketlocks; 1795 1796 numneglists = 4; 1797 neglists = malloc(sizeof(*neglists) * numneglists, M_VFSCACHE, 1798 M_WAITOK | M_ZERO); 1799 for (i = 0; i < numneglists; i++) { 1800 mtx_init(&neglists[i].nl_lock, "ncnegl", NULL, MTX_DEF); 1801 TAILQ_INIT(&neglists[i].nl_list); 1802 } 1803 mtx_init(&ncneg_hot.nl_lock, "ncneglh", NULL, MTX_DEF); 1804 TAILQ_INIT(&ncneg_hot.nl_list); 1805 1806 numcalls = counter_u64_alloc(M_WAITOK); 1807 dothits = counter_u64_alloc(M_WAITOK); 1808 dotdothits = counter_u64_alloc(M_WAITOK); 1809 numchecks = counter_u64_alloc(M_WAITOK); 1810 nummiss = counter_u64_alloc(M_WAITOK); 1811 nummisszap = counter_u64_alloc(M_WAITOK); 1812 numposzaps = counter_u64_alloc(M_WAITOK); 1813 numposhits = counter_u64_alloc(M_WAITOK); 1814 numnegzaps = counter_u64_alloc(M_WAITOK); 1815 numneghits = counter_u64_alloc(M_WAITOK); 1816 numfullpathcalls = counter_u64_alloc(M_WAITOK); 1817 numfullpathfail1 = counter_u64_alloc(M_WAITOK); 1818 numfullpathfail2 = counter_u64_alloc(M_WAITOK); 1819 numfullpathfail4 = counter_u64_alloc(M_WAITOK); 1820 numfullpathfound = counter_u64_alloc(M_WAITOK); 1821 } 1822 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nchinit, NULL); 1823 1824 void 1825 cache_changesize(int newmaxvnodes) 1826 { 1827 struct nchashhead *new_nchashtbl, *old_nchashtbl; 1828 u_long new_nchash, old_nchash; 1829 struct namecache *ncp; 1830 uint32_t hash; 1831 int i; 1832 1833 newmaxvnodes = cache_roundup_2(newmaxvnodes * 2); 1834 if (newmaxvnodes < numbucketlocks) 1835 newmaxvnodes = numbucketlocks; 1836 1837 new_nchashtbl = hashinit(newmaxvnodes, M_VFSCACHE, &new_nchash); 1838 /* If same hash table size, nothing to do */ 1839 if (nchash == new_nchash) { 1840 free(new_nchashtbl, M_VFSCACHE); 1841 return; 1842 } 1843 /* 1844 * Move everything from the old hash table to the new table. 1845 * None of the namecache entries in the table can be removed 1846 * because to do so, they have to be removed from the hash table. 1847 */ 1848 cache_lock_all_vnodes(); 1849 cache_lock_all_buckets(); 1850 old_nchashtbl = nchashtbl; 1851 old_nchash = nchash; 1852 nchashtbl = new_nchashtbl; 1853 nchash = new_nchash; 1854 for (i = 0; i <= old_nchash; i++) { 1855 while ((ncp = LIST_FIRST(&old_nchashtbl[i])) != NULL) { 1856 hash = cache_get_hash(nc_get_name(ncp), ncp->nc_nlen, 1857 ncp->nc_dvp); 1858 LIST_REMOVE(ncp, nc_hash); 1859 LIST_INSERT_HEAD(NCHHASH(hash), ncp, nc_hash); 1860 } 1861 } 1862 cache_unlock_all_buckets(); 1863 cache_unlock_all_vnodes(); 1864 free(old_nchashtbl, M_VFSCACHE); 1865 } 1866 1867 /* 1868 * Invalidate all entries to a particular vnode. 1869 */ 1870 void 1871 cache_purge(struct vnode *vp) 1872 { 1873 TAILQ_HEAD(, namecache) ncps; 1874 struct namecache *ncp, *nnp; 1875 struct mtx *vlp, *vlp2; 1876 1877 CTR1(KTR_VFS, "cache_purge(%p)", vp); 1878 SDT_PROBE1(vfs, namecache, purge, done, vp); 1879 if (LIST_EMPTY(&vp->v_cache_src) && TAILQ_EMPTY(&vp->v_cache_dst) && 1880 vp->v_cache_dd == NULL) 1881 return; 1882 TAILQ_INIT(&ncps); 1883 vlp = VP2VNODELOCK(vp); 1884 vlp2 = NULL; 1885 mtx_lock(vlp); 1886 retry: 1887 while (!LIST_EMPTY(&vp->v_cache_src)) { 1888 ncp = LIST_FIRST(&vp->v_cache_src); 1889 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2)) 1890 goto retry; 1891 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst); 1892 } 1893 while (!TAILQ_EMPTY(&vp->v_cache_dst)) { 1894 ncp = TAILQ_FIRST(&vp->v_cache_dst); 1895 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2)) 1896 goto retry; 1897 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst); 1898 } 1899 ncp = vp->v_cache_dd; 1900 if (ncp != NULL) { 1901 KASSERT(ncp->nc_flag & NCF_ISDOTDOT, 1902 ("lost dotdot link")); 1903 if (!cache_zap_locked_vnode_kl2(ncp, vp, &vlp2)) 1904 goto retry; 1905 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst); 1906 } 1907 KASSERT(vp->v_cache_dd == NULL, ("incomplete purge")); 1908 mtx_unlock(vlp); 1909 if (vlp2 != NULL) 1910 mtx_unlock(vlp2); 1911 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) { 1912 cache_free(ncp); 1913 } 1914 } 1915 1916 /* 1917 * Invalidate all negative entries for a particular directory vnode. 1918 */ 1919 void 1920 cache_purge_negative(struct vnode *vp) 1921 { 1922 TAILQ_HEAD(, namecache) ncps; 1923 struct namecache *ncp, *nnp; 1924 struct mtx *vlp; 1925 1926 CTR1(KTR_VFS, "cache_purge_negative(%p)", vp); 1927 SDT_PROBE1(vfs, namecache, purge_negative, done, vp); 1928 TAILQ_INIT(&ncps); 1929 vlp = VP2VNODELOCK(vp); 1930 mtx_lock(vlp); 1931 LIST_FOREACH_SAFE(ncp, &vp->v_cache_src, nc_src, nnp) { 1932 if (!(ncp->nc_flag & NCF_NEGATIVE)) 1933 continue; 1934 cache_zap_negative_locked_vnode_kl(ncp, vp); 1935 TAILQ_INSERT_TAIL(&ncps, ncp, nc_dst); 1936 } 1937 mtx_unlock(vlp); 1938 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) { 1939 cache_free(ncp); 1940 } 1941 } 1942 1943 /* 1944 * Flush all entries referencing a particular filesystem. 1945 */ 1946 void 1947 cache_purgevfs(struct mount *mp, bool force) 1948 { 1949 TAILQ_HEAD(, namecache) ncps; 1950 struct mtx *vlp1, *vlp2; 1951 struct rwlock *blp; 1952 struct nchashhead *bucket; 1953 struct namecache *ncp, *nnp; 1954 u_long i, j, n_nchash; 1955 int error; 1956 1957 /* Scan hash tables for applicable entries */ 1958 SDT_PROBE1(vfs, namecache, purgevfs, done, mp); 1959 if (!force && mp->mnt_nvnodelistsize <= ncpurgeminvnodes) 1960 return; 1961 TAILQ_INIT(&ncps); 1962 n_nchash = nchash + 1; 1963 vlp1 = vlp2 = NULL; 1964 for (i = 0; i < numbucketlocks; i++) { 1965 blp = (struct rwlock *)&bucketlocks[i]; 1966 rw_wlock(blp); 1967 for (j = i; j < n_nchash; j += numbucketlocks) { 1968 retry: 1969 bucket = &nchashtbl[j]; 1970 LIST_FOREACH_SAFE(ncp, bucket, nc_hash, nnp) { 1971 cache_assert_bucket_locked(ncp, RA_WLOCKED); 1972 if (ncp->nc_dvp->v_mount != mp) 1973 continue; 1974 error = cache_zap_wlocked_bucket_kl(ncp, blp, 1975 &vlp1, &vlp2); 1976 if (error != 0) 1977 goto retry; 1978 TAILQ_INSERT_HEAD(&ncps, ncp, nc_dst); 1979 } 1980 } 1981 rw_wunlock(blp); 1982 if (vlp1 == NULL && vlp2 == NULL) 1983 cache_maybe_yield(); 1984 } 1985 if (vlp1 != NULL) 1986 mtx_unlock(vlp1); 1987 if (vlp2 != NULL) 1988 mtx_unlock(vlp2); 1989 1990 TAILQ_FOREACH_SAFE(ncp, &ncps, nc_dst, nnp) { 1991 cache_free(ncp); 1992 } 1993 } 1994 1995 /* 1996 * Perform canonical checks and cache lookup and pass on to filesystem 1997 * through the vop_cachedlookup only if needed. 1998 */ 1999 2000 int 2001 vfs_cache_lookup(struct vop_lookup_args *ap) 2002 { 2003 struct vnode *dvp; 2004 int error; 2005 struct vnode **vpp = ap->a_vpp; 2006 struct componentname *cnp = ap->a_cnp; 2007 struct ucred *cred = cnp->cn_cred; 2008 int flags = cnp->cn_flags; 2009 struct thread *td = cnp->cn_thread; 2010 2011 *vpp = NULL; 2012 dvp = ap->a_dvp; 2013 2014 if (dvp->v_type != VDIR) 2015 return (ENOTDIR); 2016 2017 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) && 2018 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) 2019 return (EROFS); 2020 2021 error = VOP_ACCESS(dvp, VEXEC, cred, td); 2022 if (error) 2023 return (error); 2024 2025 error = cache_lookup(dvp, vpp, cnp, NULL, NULL); 2026 if (error == 0) 2027 return (VOP_CACHEDLOOKUP(dvp, vpp, cnp)); 2028 if (error == -1) 2029 return (0); 2030 return (error); 2031 } 2032 2033 /* 2034 * XXX All of these sysctls would probably be more productive dead. 2035 */ 2036 static int disablecwd; 2037 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0, 2038 "Disable the getcwd syscall"); 2039 2040 /* Implementation of the getcwd syscall. */ 2041 int 2042 sys___getcwd(struct thread *td, struct __getcwd_args *uap) 2043 { 2044 2045 return (kern___getcwd(td, uap->buf, UIO_USERSPACE, uap->buflen, 2046 MAXPATHLEN)); 2047 } 2048 2049 int 2050 kern___getcwd(struct thread *td, char *buf, enum uio_seg bufseg, u_int buflen, 2051 u_int path_max) 2052 { 2053 char *bp, *tmpbuf; 2054 struct filedesc *fdp; 2055 struct vnode *cdir, *rdir; 2056 int error; 2057 2058 if (disablecwd) 2059 return (ENODEV); 2060 if (buflen < 2) 2061 return (EINVAL); 2062 if (buflen > path_max) 2063 buflen = path_max; 2064 2065 tmpbuf = malloc(buflen, M_TEMP, M_WAITOK); 2066 fdp = td->td_proc->p_fd; 2067 FILEDESC_SLOCK(fdp); 2068 cdir = fdp->fd_cdir; 2069 vrefact(cdir); 2070 rdir = fdp->fd_rdir; 2071 vrefact(rdir); 2072 FILEDESC_SUNLOCK(fdp); 2073 error = vn_fullpath1(td, cdir, rdir, tmpbuf, &bp, buflen); 2074 vrele(rdir); 2075 vrele(cdir); 2076 2077 if (!error) { 2078 if (bufseg == UIO_SYSSPACE) 2079 bcopy(bp, buf, strlen(bp) + 1); 2080 else 2081 error = copyout(bp, buf, strlen(bp) + 1); 2082 #ifdef KTRACE 2083 if (KTRPOINT(curthread, KTR_NAMEI)) 2084 ktrnamei(bp); 2085 #endif 2086 } 2087 free(tmpbuf, M_TEMP); 2088 return (error); 2089 } 2090 2091 /* 2092 * Thus begins the fullpath magic. 2093 */ 2094 2095 static int disablefullpath; 2096 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW, &disablefullpath, 0, 2097 "Disable the vn_fullpath function"); 2098 2099 /* 2100 * Retrieve the full filesystem path that correspond to a vnode from the name 2101 * cache (if available) 2102 */ 2103 int 2104 vn_fullpath(struct thread *td, struct vnode *vn, char **retbuf, char **freebuf) 2105 { 2106 char *buf; 2107 struct filedesc *fdp; 2108 struct vnode *rdir; 2109 int error; 2110 2111 if (disablefullpath) 2112 return (ENODEV); 2113 if (vn == NULL) 2114 return (EINVAL); 2115 2116 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 2117 fdp = td->td_proc->p_fd; 2118 FILEDESC_SLOCK(fdp); 2119 rdir = fdp->fd_rdir; 2120 VREF(rdir); 2121 FILEDESC_SUNLOCK(fdp); 2122 error = vn_fullpath1(td, vn, rdir, buf, retbuf, MAXPATHLEN); 2123 vrele(rdir); 2124 2125 if (!error) 2126 *freebuf = buf; 2127 else 2128 free(buf, M_TEMP); 2129 return (error); 2130 } 2131 2132 /* 2133 * This function is similar to vn_fullpath, but it attempts to lookup the 2134 * pathname relative to the global root mount point. This is required for the 2135 * auditing sub-system, as audited pathnames must be absolute, relative to the 2136 * global root mount point. 2137 */ 2138 int 2139 vn_fullpath_global(struct thread *td, struct vnode *vn, 2140 char **retbuf, char **freebuf) 2141 { 2142 char *buf; 2143 int error; 2144 2145 if (disablefullpath) 2146 return (ENODEV); 2147 if (vn == NULL) 2148 return (EINVAL); 2149 buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 2150 error = vn_fullpath1(td, vn, rootvnode, buf, retbuf, MAXPATHLEN); 2151 if (!error) 2152 *freebuf = buf; 2153 else 2154 free(buf, M_TEMP); 2155 return (error); 2156 } 2157 2158 int 2159 vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, u_int *buflen) 2160 { 2161 struct vnode *dvp; 2162 struct namecache *ncp; 2163 struct mtx *vlp; 2164 int error; 2165 2166 vlp = VP2VNODELOCK(*vp); 2167 mtx_lock(vlp); 2168 TAILQ_FOREACH(ncp, &((*vp)->v_cache_dst), nc_dst) { 2169 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0) 2170 break; 2171 } 2172 if (ncp != NULL) { 2173 if (*buflen < ncp->nc_nlen) { 2174 mtx_unlock(vlp); 2175 vrele(*vp); 2176 counter_u64_add(numfullpathfail4, 1); 2177 error = ENOMEM; 2178 SDT_PROBE3(vfs, namecache, fullpath, return, error, 2179 vp, NULL); 2180 return (error); 2181 } 2182 *buflen -= ncp->nc_nlen; 2183 memcpy(buf + *buflen, nc_get_name(ncp), ncp->nc_nlen); 2184 SDT_PROBE3(vfs, namecache, fullpath, hit, ncp->nc_dvp, 2185 nc_get_name(ncp), vp); 2186 dvp = *vp; 2187 *vp = ncp->nc_dvp; 2188 vref(*vp); 2189 mtx_unlock(vlp); 2190 vrele(dvp); 2191 return (0); 2192 } 2193 SDT_PROBE1(vfs, namecache, fullpath, miss, vp); 2194 2195 mtx_unlock(vlp); 2196 vn_lock(*vp, LK_SHARED | LK_RETRY); 2197 error = VOP_VPTOCNP(*vp, &dvp, cred, buf, buflen); 2198 vput(*vp); 2199 if (error) { 2200 counter_u64_add(numfullpathfail2, 1); 2201 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL); 2202 return (error); 2203 } 2204 2205 *vp = dvp; 2206 if (dvp->v_iflag & VI_DOOMED) { 2207 /* forced unmount */ 2208 vrele(dvp); 2209 error = ENOENT; 2210 SDT_PROBE3(vfs, namecache, fullpath, return, error, vp, NULL); 2211 return (error); 2212 } 2213 /* 2214 * *vp has its use count incremented still. 2215 */ 2216 2217 return (0); 2218 } 2219 2220 /* 2221 * The magic behind kern___getcwd() and vn_fullpath(). 2222 */ 2223 static int 2224 vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir, 2225 char *buf, char **retbuf, u_int buflen) 2226 { 2227 int error, slash_prefixed; 2228 #ifdef KDTRACE_HOOKS 2229 struct vnode *startvp = vp; 2230 #endif 2231 struct vnode *vp1; 2232 2233 buflen--; 2234 buf[buflen] = '\0'; 2235 error = 0; 2236 slash_prefixed = 0; 2237 2238 SDT_PROBE1(vfs, namecache, fullpath, entry, vp); 2239 counter_u64_add(numfullpathcalls, 1); 2240 vref(vp); 2241 if (vp->v_type != VDIR) { 2242 error = vn_vptocnp(&vp, td->td_ucred, buf, &buflen); 2243 if (error) 2244 return (error); 2245 if (buflen == 0) { 2246 vrele(vp); 2247 return (ENOMEM); 2248 } 2249 buf[--buflen] = '/'; 2250 slash_prefixed = 1; 2251 } 2252 while (vp != rdir && vp != rootvnode) { 2253 /* 2254 * The vp vnode must be already fully constructed, 2255 * since it is either found in namecache or obtained 2256 * from VOP_VPTOCNP(). We may test for VV_ROOT safely 2257 * without obtaining the vnode lock. 2258 */ 2259 if ((vp->v_vflag & VV_ROOT) != 0) { 2260 vn_lock(vp, LK_RETRY | LK_SHARED); 2261 2262 /* 2263 * With the vnode locked, check for races with 2264 * unmount, forced or not. Note that we 2265 * already verified that vp is not equal to 2266 * the root vnode, which means that 2267 * mnt_vnodecovered can be NULL only for the 2268 * case of unmount. 2269 */ 2270 if ((vp->v_iflag & VI_DOOMED) != 0 || 2271 (vp1 = vp->v_mount->mnt_vnodecovered) == NULL || 2272 vp1->v_mountedhere != vp->v_mount) { 2273 vput(vp); 2274 error = ENOENT; 2275 SDT_PROBE3(vfs, namecache, fullpath, return, 2276 error, vp, NULL); 2277 break; 2278 } 2279 2280 vref(vp1); 2281 vput(vp); 2282 vp = vp1; 2283 continue; 2284 } 2285 if (vp->v_type != VDIR) { 2286 vrele(vp); 2287 counter_u64_add(numfullpathfail1, 1); 2288 error = ENOTDIR; 2289 SDT_PROBE3(vfs, namecache, fullpath, return, 2290 error, vp, NULL); 2291 break; 2292 } 2293 error = vn_vptocnp(&vp, td->td_ucred, buf, &buflen); 2294 if (error) 2295 break; 2296 if (buflen == 0) { 2297 vrele(vp); 2298 error = ENOMEM; 2299 SDT_PROBE3(vfs, namecache, fullpath, return, error, 2300 startvp, NULL); 2301 break; 2302 } 2303 buf[--buflen] = '/'; 2304 slash_prefixed = 1; 2305 } 2306 if (error) 2307 return (error); 2308 if (!slash_prefixed) { 2309 if (buflen == 0) { 2310 vrele(vp); 2311 counter_u64_add(numfullpathfail4, 1); 2312 SDT_PROBE3(vfs, namecache, fullpath, return, ENOMEM, 2313 startvp, NULL); 2314 return (ENOMEM); 2315 } 2316 buf[--buflen] = '/'; 2317 } 2318 counter_u64_add(numfullpathfound, 1); 2319 vrele(vp); 2320 2321 SDT_PROBE3(vfs, namecache, fullpath, return, 0, startvp, buf + buflen); 2322 *retbuf = buf + buflen; 2323 return (0); 2324 } 2325 2326 struct vnode * 2327 vn_dir_dd_ino(struct vnode *vp) 2328 { 2329 struct namecache *ncp; 2330 struct vnode *ddvp; 2331 struct mtx *vlp; 2332 2333 ASSERT_VOP_LOCKED(vp, "vn_dir_dd_ino"); 2334 vlp = VP2VNODELOCK(vp); 2335 mtx_lock(vlp); 2336 TAILQ_FOREACH(ncp, &(vp->v_cache_dst), nc_dst) { 2337 if ((ncp->nc_flag & NCF_ISDOTDOT) != 0) 2338 continue; 2339 ddvp = ncp->nc_dvp; 2340 vhold(ddvp); 2341 mtx_unlock(vlp); 2342 if (vget(ddvp, LK_SHARED | LK_NOWAIT | LK_VNHELD, curthread)) 2343 return (NULL); 2344 return (ddvp); 2345 } 2346 mtx_unlock(vlp); 2347 return (NULL); 2348 } 2349 2350 int 2351 vn_commname(struct vnode *vp, char *buf, u_int buflen) 2352 { 2353 struct namecache *ncp; 2354 struct mtx *vlp; 2355 int l; 2356 2357 vlp = VP2VNODELOCK(vp); 2358 mtx_lock(vlp); 2359 TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_dst) 2360 if ((ncp->nc_flag & NCF_ISDOTDOT) == 0) 2361 break; 2362 if (ncp == NULL) { 2363 mtx_unlock(vlp); 2364 return (ENOENT); 2365 } 2366 l = min(ncp->nc_nlen, buflen - 1); 2367 memcpy(buf, nc_get_name(ncp), l); 2368 mtx_unlock(vlp); 2369 buf[l] = '\0'; 2370 return (0); 2371 } 2372 2373 /* ABI compat shims for old kernel modules. */ 2374 #undef cache_enter 2375 2376 void cache_enter(struct vnode *dvp, struct vnode *vp, 2377 struct componentname *cnp); 2378 2379 void 2380 cache_enter(struct vnode *dvp, struct vnode *vp, struct componentname *cnp) 2381 { 2382 2383 cache_enter_time(dvp, vp, cnp, NULL, NULL); 2384 } 2385 2386 /* 2387 * This function updates path string to vnode's full global path 2388 * and checks the size of the new path string against the pathlen argument. 2389 * 2390 * Requires a locked, referenced vnode. 2391 * Vnode is re-locked on success or ENODEV, otherwise unlocked. 2392 * 2393 * If sysctl debug.disablefullpath is set, ENODEV is returned, 2394 * vnode is left locked and path remain untouched. 2395 * 2396 * If vp is a directory, the call to vn_fullpath_global() always succeeds 2397 * because it falls back to the ".." lookup if the namecache lookup fails. 2398 */ 2399 int 2400 vn_path_to_global_path(struct thread *td, struct vnode *vp, char *path, 2401 u_int pathlen) 2402 { 2403 struct nameidata nd; 2404 struct vnode *vp1; 2405 char *rpath, *fbuf; 2406 int error; 2407 2408 ASSERT_VOP_ELOCKED(vp, __func__); 2409 2410 /* Return ENODEV if sysctl debug.disablefullpath==1 */ 2411 if (disablefullpath) 2412 return (ENODEV); 2413 2414 /* Construct global filesystem path from vp. */ 2415 VOP_UNLOCK(vp, 0); 2416 error = vn_fullpath_global(td, vp, &rpath, &fbuf); 2417 2418 if (error != 0) { 2419 vrele(vp); 2420 return (error); 2421 } 2422 2423 if (strlen(rpath) >= pathlen) { 2424 vrele(vp); 2425 error = ENAMETOOLONG; 2426 goto out; 2427 } 2428 2429 /* 2430 * Re-lookup the vnode by path to detect a possible rename. 2431 * As a side effect, the vnode is relocked. 2432 * If vnode was renamed, return ENOENT. 2433 */ 2434 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, 2435 UIO_SYSSPACE, path, td); 2436 error = namei(&nd); 2437 if (error != 0) { 2438 vrele(vp); 2439 goto out; 2440 } 2441 NDFREE(&nd, NDF_ONLY_PNBUF); 2442 vp1 = nd.ni_vp; 2443 vrele(vp); 2444 if (vp1 == vp) 2445 strcpy(path, rpath); 2446 else { 2447 vput(vp1); 2448 error = ENOENT; 2449 } 2450 2451 out: 2452 free(fbuf, M_TEMP); 2453 return (error); 2454 } 2455