1 /* 2 * Copyright (c) 2003,2004,2009 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Matthew Dillon <dillon@backplane.com> 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 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 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 3. Neither the name of The DragonFly Project nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific, prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * Copyright (c) 1989, 1993, 1995 35 * The Regents of the University of California. All rights reserved. 36 * 37 * This code is derived from software contributed to Berkeley by 38 * Poul-Henning Kamp of the FreeBSD Project. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions 42 * are met: 43 * 1. Redistributions of source code must retain the above copyright 44 * notice, this list of conditions and the following disclaimer. 45 * 2. Redistributions in binary form must reproduce the above copyright 46 * notice, this list of conditions and the following disclaimer in the 47 * documentation and/or other materials provided with the distribution. 48 * 3. Neither the name of the University nor the names of its contributors 49 * may be used to endorse or promote products derived from this software 50 * without specific prior written permission. 51 * 52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 62 * SUCH DAMAGE. 63 */ 64 65 #include <sys/param.h> 66 #include <sys/systm.h> 67 #include <sys/kernel.h> 68 #include <sys/sysctl.h> 69 #include <sys/mount.h> 70 #include <sys/vnode.h> 71 #include <sys/malloc.h> 72 #include <sys/sysproto.h> 73 #include <sys/spinlock.h> 74 #include <sys/proc.h> 75 #include <sys/namei.h> 76 #include <sys/nlookup.h> 77 #include <sys/filedesc.h> 78 #include <sys/fnv_hash.h> 79 #include <sys/globaldata.h> 80 #include <sys/kern_syscall.h> 81 #include <sys/dirent.h> 82 #include <ddb/ddb.h> 83 84 #include <sys/sysref2.h> 85 #include <sys/spinlock2.h> 86 #include <sys/mplock2.h> 87 88 #define MAX_RECURSION_DEPTH 64 89 90 /* 91 * Random lookups in the cache are accomplished with a hash table using 92 * a hash key of (nc_src_vp, name). Each hash chain has its own spin lock. 93 * 94 * Negative entries may exist and correspond to resolved namecache 95 * structures where nc_vp is NULL. In a negative entry, NCF_WHITEOUT 96 * will be set if the entry corresponds to a whited-out directory entry 97 * (verses simply not finding the entry at all). ncneglist is locked 98 * with a global spinlock (ncspin). 99 * 100 * MPSAFE RULES: 101 * 102 * (1) A ncp must be referenced before it can be locked. 103 * 104 * (2) A ncp must be locked in order to modify it. 105 * 106 * (3) ncp locks are always ordered child -> parent. That may seem 107 * backwards but forward scans use the hash table and thus can hold 108 * the parent unlocked when traversing downward. 109 * 110 * This allows insert/rename/delete/dot-dot and other operations 111 * to use ncp->nc_parent links. 112 * 113 * This also prevents a locked up e.g. NFS node from creating a 114 * chain reaction all the way back to the root vnode / namecache. 115 * 116 * (4) parent linkages require both the parent and child to be locked. 117 */ 118 119 /* 120 * Structures associated with name cacheing. 121 */ 122 #define NCHHASH(hash) (&nchashtbl[(hash) & nchash]) 123 #define MINNEG 1024 124 #define MINPOS 1024 125 #define NCMOUNT_NUMCACHE 1009 /* prime number */ 126 127 MALLOC_DEFINE(M_VFSCACHE, "vfscache", "VFS name cache entries"); 128 129 LIST_HEAD(nchash_list, namecache); 130 131 struct nchash_head { 132 struct nchash_list list; 133 struct spinlock spin; 134 }; 135 136 struct ncmount_cache { 137 struct spinlock spin; 138 struct namecache *ncp; 139 struct mount *mp; 140 int isneg; /* if != 0 mp is originator and not target */ 141 }; 142 143 static struct nchash_head *nchashtbl; 144 static struct namecache_list ncneglist; 145 static struct spinlock ncspin; 146 static struct ncmount_cache ncmount_cache[NCMOUNT_NUMCACHE]; 147 148 /* 149 * ncvp_debug - debug cache_fromvp(). This is used by the NFS server 150 * to create the namecache infrastructure leading to a dangling vnode. 151 * 152 * 0 Only errors are reported 153 * 1 Successes are reported 154 * 2 Successes + the whole directory scan is reported 155 * 3 Force the directory scan code run as if the parent vnode did not 156 * have a namecache record, even if it does have one. 157 */ 158 static int ncvp_debug; 159 SYSCTL_INT(_debug, OID_AUTO, ncvp_debug, CTLFLAG_RW, &ncvp_debug, 0, 160 "Namecache debug level (0-3)"); 161 162 static u_long nchash; /* size of hash table */ 163 SYSCTL_ULONG(_debug, OID_AUTO, nchash, CTLFLAG_RD, &nchash, 0, 164 "Size of namecache hash table"); 165 166 static int ncnegflush = 10; /* burst for negative flush */ 167 SYSCTL_INT(_debug, OID_AUTO, ncnegflush, CTLFLAG_RW, &ncnegflush, 0, 168 "Batch flush negative entries"); 169 170 static int ncposflush = 10; /* burst for positive flush */ 171 SYSCTL_INT(_debug, OID_AUTO, ncposflush, CTLFLAG_RW, &ncposflush, 0, 172 "Batch flush positive entries"); 173 174 static int ncnegfactor = 16; /* ratio of negative entries */ 175 SYSCTL_INT(_debug, OID_AUTO, ncnegfactor, CTLFLAG_RW, &ncnegfactor, 0, 176 "Ratio of namecache negative entries"); 177 178 static int nclockwarn; /* warn on locked entries in ticks */ 179 SYSCTL_INT(_debug, OID_AUTO, nclockwarn, CTLFLAG_RW, &nclockwarn, 0, 180 "Warn on locked namecache entries in ticks"); 181 182 static int numdefered; /* number of cache entries allocated */ 183 SYSCTL_INT(_debug, OID_AUTO, numdefered, CTLFLAG_RD, &numdefered, 0, 184 "Number of cache entries allocated"); 185 186 static int ncposlimit; /* number of cache entries allocated */ 187 SYSCTL_INT(_debug, OID_AUTO, ncposlimit, CTLFLAG_RW, &ncposlimit, 0, 188 "Number of cache entries allocated"); 189 190 static int ncp_shared_lock_disable = 0; 191 SYSCTL_INT(_debug, OID_AUTO, ncp_shared_lock_disable, CTLFLAG_RW, 192 &ncp_shared_lock_disable, 0, "Disable shared namecache locks"); 193 194 SYSCTL_INT(_debug, OID_AUTO, vnsize, CTLFLAG_RD, 0, sizeof(struct vnode), 195 "sizeof(struct vnode)"); 196 SYSCTL_INT(_debug, OID_AUTO, ncsize, CTLFLAG_RD, 0, sizeof(struct namecache), 197 "sizeof(struct namecache)"); 198 199 static int ncmount_cache_enable = 1; 200 SYSCTL_INT(_debug, OID_AUTO, ncmount_cache_enable, CTLFLAG_RW, 201 &ncmount_cache_enable, 0, "mount point cache"); 202 static long ncmount_cache_hit; 203 SYSCTL_LONG(_debug, OID_AUTO, ncmount_cache_hit, CTLFLAG_RW, 204 &ncmount_cache_hit, 0, "mpcache hits"); 205 static long ncmount_cache_miss; 206 SYSCTL_LONG(_debug, OID_AUTO, ncmount_cache_miss, CTLFLAG_RW, 207 &ncmount_cache_miss, 0, "mpcache misses"); 208 static long ncmount_cache_overwrite; 209 SYSCTL_LONG(_debug, OID_AUTO, ncmount_cache_overwrite, CTLFLAG_RW, 210 &ncmount_cache_overwrite, 0, "mpcache entry overwrites"); 211 212 static int cache_resolve_mp(struct mount *mp); 213 static struct vnode *cache_dvpref(struct namecache *ncp); 214 static void _cache_lock(struct namecache *ncp); 215 static void _cache_setunresolved(struct namecache *ncp); 216 static void _cache_cleanneg(int count); 217 static void _cache_cleanpos(int count); 218 static void _cache_cleandefered(void); 219 static void _cache_unlink(struct namecache *ncp); 220 221 /* 222 * The new name cache statistics 223 */ 224 SYSCTL_NODE(_vfs, OID_AUTO, cache, CTLFLAG_RW, 0, "Name cache statistics"); 225 static int numneg; 226 SYSCTL_INT(_vfs_cache, OID_AUTO, numneg, CTLFLAG_RD, &numneg, 0, 227 "Number of negative namecache entries"); 228 static int numcache; 229 SYSCTL_INT(_vfs_cache, OID_AUTO, numcache, CTLFLAG_RD, &numcache, 0, 230 "Number of namecaches entries"); 231 static u_long numcalls; 232 SYSCTL_ULONG(_vfs_cache, OID_AUTO, numcalls, CTLFLAG_RD, &numcalls, 0, 233 "Number of namecache lookups"); 234 static u_long numchecks; 235 SYSCTL_ULONG(_vfs_cache, OID_AUTO, numchecks, CTLFLAG_RD, &numchecks, 0, 236 "Number of checked entries in namecache lookups"); 237 238 struct nchstats nchstats[SMP_MAXCPU]; 239 /* 240 * Export VFS cache effectiveness statistics to user-land. 241 * 242 * The statistics are left for aggregation to user-land so 243 * neat things can be achieved, like observing per-CPU cache 244 * distribution. 245 */ 246 static int 247 sysctl_nchstats(SYSCTL_HANDLER_ARGS) 248 { 249 struct globaldata *gd; 250 int i, error; 251 252 error = 0; 253 for (i = 0; i < ncpus; ++i) { 254 gd = globaldata_find(i); 255 if ((error = SYSCTL_OUT(req, (void *)&(*gd->gd_nchstats), 256 sizeof(struct nchstats)))) 257 break; 258 } 259 260 return (error); 261 } 262 SYSCTL_PROC(_vfs_cache, OID_AUTO, nchstats, CTLTYPE_OPAQUE|CTLFLAG_RD, 263 0, 0, sysctl_nchstats, "S,nchstats", "VFS cache effectiveness statistics"); 264 265 static struct namecache *cache_zap(struct namecache *ncp, int nonblock); 266 267 /* 268 * Namespace locking. The caller must already hold a reference to the 269 * namecache structure in order to lock/unlock it. This function prevents 270 * the namespace from being created or destroyed by accessors other then 271 * the lock holder. 272 * 273 * Note that holding a locked namecache structure prevents other threads 274 * from making namespace changes (e.g. deleting or creating), prevents 275 * vnode association state changes by other threads, and prevents the 276 * namecache entry from being resolved or unresolved by other threads. 277 * 278 * An exclusive lock owner has full authority to associate/disassociate 279 * vnodes and resolve/unresolve the locked ncp. 280 * 281 * A shared lock owner only has authority to acquire the underlying vnode, 282 * if any. 283 * 284 * The primary lock field is nc_lockstatus. nc_locktd is set after the 285 * fact (when locking) or cleared prior to unlocking. 286 * 287 * WARNING! Holding a locked ncp will prevent a vnode from being destroyed 288 * or recycled, but it does NOT help you if the vnode had already 289 * initiated a recyclement. If this is important, use cache_get() 290 * rather then cache_lock() (and deal with the differences in the 291 * way the refs counter is handled). Or, alternatively, make an 292 * unconditional call to cache_validate() or cache_resolve() 293 * after cache_lock() returns. 294 */ 295 static 296 void 297 _cache_lock(struct namecache *ncp) 298 { 299 thread_t td; 300 int didwarn; 301 int begticks; 302 int error; 303 u_int count; 304 305 KKASSERT(ncp->nc_refs != 0); 306 didwarn = 0; 307 begticks = 0; 308 td = curthread; 309 310 for (;;) { 311 count = ncp->nc_lockstatus; 312 cpu_ccfence(); 313 314 if ((count & ~(NC_EXLOCK_REQ|NC_SHLOCK_REQ)) == 0) { 315 if (atomic_cmpset_int(&ncp->nc_lockstatus, 316 count, count + 1)) { 317 /* 318 * The vp associated with a locked ncp must 319 * be held to prevent it from being recycled. 320 * 321 * WARNING! If VRECLAIMED is set the vnode 322 * could already be in the middle of a recycle. 323 * Callers must use cache_vref() or 324 * cache_vget() on the locked ncp to 325 * validate the vp or set the cache entry 326 * to unresolved. 327 * 328 * NOTE! vhold() is allowed if we hold a 329 * lock on the ncp (which we do). 330 */ 331 ncp->nc_locktd = td; 332 if (ncp->nc_vp) 333 vhold(ncp->nc_vp); 334 break; 335 } 336 /* cmpset failed */ 337 continue; 338 } 339 if (ncp->nc_locktd == td) { 340 KKASSERT((count & NC_SHLOCK_FLAG) == 0); 341 if (atomic_cmpset_int(&ncp->nc_lockstatus, 342 count, count + 1)) { 343 break; 344 } 345 /* cmpset failed */ 346 continue; 347 } 348 tsleep_interlock(&ncp->nc_locktd, 0); 349 if (atomic_cmpset_int(&ncp->nc_lockstatus, count, 350 count | NC_EXLOCK_REQ) == 0) { 351 /* cmpset failed */ 352 continue; 353 } 354 if (begticks == 0) 355 begticks = ticks; 356 error = tsleep(&ncp->nc_locktd, PINTERLOCKED, 357 "clock", nclockwarn); 358 if (error == EWOULDBLOCK) { 359 if (didwarn == 0) { 360 didwarn = ticks; 361 kprintf("[diagnostic] cache_lock: " 362 "blocked on %p %08x", 363 ncp, count); 364 kprintf(" \"%*.*s\"\n", 365 ncp->nc_nlen, ncp->nc_nlen, 366 ncp->nc_name); 367 } 368 } 369 /* loop */ 370 } 371 if (didwarn) { 372 kprintf("[diagnostic] cache_lock: unblocked %*.*s after " 373 "%d secs\n", 374 ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name, 375 (int)(ticks + (hz / 2) - begticks) / hz); 376 } 377 } 378 379 /* 380 * The shared lock works similarly to the exclusive lock except 381 * nc_locktd is left NULL and we need an interlock (VHOLD) to 382 * prevent vhold() races, since the moment our cmpset_int succeeds 383 * another cpu can come in and get its own shared lock. 384 * 385 * A critical section is needed to prevent interruption during the 386 * VHOLD interlock. 387 */ 388 static 389 void 390 _cache_lock_shared(struct namecache *ncp) 391 { 392 int didwarn; 393 int error; 394 u_int count; 395 u_int optreq = NC_EXLOCK_REQ; 396 397 KKASSERT(ncp->nc_refs != 0); 398 didwarn = 0; 399 400 for (;;) { 401 count = ncp->nc_lockstatus; 402 cpu_ccfence(); 403 404 if ((count & ~NC_SHLOCK_REQ) == 0) { 405 crit_enter(); 406 if (atomic_cmpset_int(&ncp->nc_lockstatus, 407 count, 408 (count + 1) | NC_SHLOCK_FLAG | 409 NC_SHLOCK_VHOLD)) { 410 /* 411 * The vp associated with a locked ncp must 412 * be held to prevent it from being recycled. 413 * 414 * WARNING! If VRECLAIMED is set the vnode 415 * could already be in the middle of a recycle. 416 * Callers must use cache_vref() or 417 * cache_vget() on the locked ncp to 418 * validate the vp or set the cache entry 419 * to unresolved. 420 * 421 * NOTE! vhold() is allowed if we hold a 422 * lock on the ncp (which we do). 423 */ 424 if (ncp->nc_vp) 425 vhold(ncp->nc_vp); 426 atomic_clear_int(&ncp->nc_lockstatus, 427 NC_SHLOCK_VHOLD); 428 crit_exit(); 429 break; 430 } 431 /* cmpset failed */ 432 crit_exit(); 433 continue; 434 } 435 436 /* 437 * If already held shared we can just bump the count, but 438 * only allow this if nobody is trying to get the lock 439 * exclusively. If we are blocking too long ignore excl 440 * requests (which can race/deadlock us). 441 * 442 * VHOLD is a bit of a hack. Even though we successfully 443 * added another shared ref, the cpu that got the first 444 * shared ref might not yet have held the vnode. 445 */ 446 if ((count & (optreq|NC_SHLOCK_FLAG)) == NC_SHLOCK_FLAG) { 447 KKASSERT((count & ~(NC_EXLOCK_REQ | 448 NC_SHLOCK_REQ | 449 NC_SHLOCK_FLAG)) > 0); 450 if (atomic_cmpset_int(&ncp->nc_lockstatus, 451 count, count + 1)) { 452 while (ncp->nc_lockstatus & NC_SHLOCK_VHOLD) 453 cpu_pause(); 454 break; 455 } 456 continue; 457 } 458 tsleep_interlock(ncp, 0); 459 if (atomic_cmpset_int(&ncp->nc_lockstatus, count, 460 count | NC_SHLOCK_REQ) == 0) { 461 /* cmpset failed */ 462 continue; 463 } 464 error = tsleep(ncp, PINTERLOCKED, "clocksh", nclockwarn); 465 if (error == EWOULDBLOCK) { 466 optreq = 0; 467 if (didwarn == 0) { 468 didwarn = ticks; 469 kprintf("[diagnostic] cache_lock_shared: " 470 "blocked on %p %08x", 471 ncp, count); 472 kprintf(" \"%*.*s\"\n", 473 ncp->nc_nlen, ncp->nc_nlen, 474 ncp->nc_name); 475 } 476 } 477 /* loop */ 478 } 479 if (didwarn) { 480 kprintf("[diagnostic] cache_lock_shared: " 481 "unblocked %*.*s after %d secs\n", 482 ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name, 483 (int)(ticks - didwarn) / hz); 484 } 485 } 486 487 /* 488 * Lock ncp exclusively, return 0 on success. 489 * 490 * NOTE: nc_refs may be zero if the ncp is interlocked by circumstance, 491 * such as the case where one of its children is locked. 492 */ 493 static 494 int 495 _cache_lock_nonblock(struct namecache *ncp) 496 { 497 thread_t td; 498 u_int count; 499 500 td = curthread; 501 502 for (;;) { 503 count = ncp->nc_lockstatus; 504 505 if ((count & ~(NC_EXLOCK_REQ|NC_SHLOCK_REQ)) == 0) { 506 if (atomic_cmpset_int(&ncp->nc_lockstatus, 507 count, count + 1)) { 508 /* 509 * The vp associated with a locked ncp must 510 * be held to prevent it from being recycled. 511 * 512 * WARNING! If VRECLAIMED is set the vnode 513 * could already be in the middle of a recycle. 514 * Callers must use cache_vref() or 515 * cache_vget() on the locked ncp to 516 * validate the vp or set the cache entry 517 * to unresolved. 518 * 519 * NOTE! vhold() is allowed if we hold a 520 * lock on the ncp (which we do). 521 */ 522 ncp->nc_locktd = td; 523 if (ncp->nc_vp) 524 vhold(ncp->nc_vp); 525 break; 526 } 527 /* cmpset failed */ 528 continue; 529 } 530 if (ncp->nc_locktd == td) { 531 if (atomic_cmpset_int(&ncp->nc_lockstatus, 532 count, count + 1)) { 533 break; 534 } 535 /* cmpset failed */ 536 continue; 537 } 538 return(EWOULDBLOCK); 539 } 540 return(0); 541 } 542 543 /* 544 * The shared lock works similarly to the exclusive lock except 545 * nc_locktd is left NULL and we need an interlock (VHOLD) to 546 * prevent vhold() races, since the moment our cmpset_int succeeds 547 * another cpu can come in and get its own shared lock. 548 * 549 * A critical section is needed to prevent interruption during the 550 * VHOLD interlock. 551 */ 552 static 553 int 554 _cache_lock_shared_nonblock(struct namecache *ncp) 555 { 556 u_int count; 557 558 for (;;) { 559 count = ncp->nc_lockstatus; 560 561 if ((count & ~NC_SHLOCK_REQ) == 0) { 562 crit_enter(); 563 if (atomic_cmpset_int(&ncp->nc_lockstatus, 564 count, 565 (count + 1) | NC_SHLOCK_FLAG | 566 NC_SHLOCK_VHOLD)) { 567 /* 568 * The vp associated with a locked ncp must 569 * be held to prevent it from being recycled. 570 * 571 * WARNING! If VRECLAIMED is set the vnode 572 * could already be in the middle of a recycle. 573 * Callers must use cache_vref() or 574 * cache_vget() on the locked ncp to 575 * validate the vp or set the cache entry 576 * to unresolved. 577 * 578 * NOTE! vhold() is allowed if we hold a 579 * lock on the ncp (which we do). 580 */ 581 if (ncp->nc_vp) 582 vhold(ncp->nc_vp); 583 atomic_clear_int(&ncp->nc_lockstatus, 584 NC_SHLOCK_VHOLD); 585 crit_exit(); 586 break; 587 } 588 /* cmpset failed */ 589 crit_exit(); 590 continue; 591 } 592 593 /* 594 * If already held shared we can just bump the count, but 595 * only allow this if nobody is trying to get the lock 596 * exclusively. 597 * 598 * VHOLD is a bit of a hack. Even though we successfully 599 * added another shared ref, the cpu that got the first 600 * shared ref might not yet have held the vnode. 601 */ 602 if ((count & (NC_EXLOCK_REQ|NC_SHLOCK_FLAG)) == 603 NC_SHLOCK_FLAG) { 604 KKASSERT((count & ~(NC_EXLOCK_REQ | 605 NC_SHLOCK_REQ | 606 NC_SHLOCK_FLAG)) > 0); 607 if (atomic_cmpset_int(&ncp->nc_lockstatus, 608 count, count + 1)) { 609 while (ncp->nc_lockstatus & NC_SHLOCK_VHOLD) 610 cpu_pause(); 611 break; 612 } 613 continue; 614 } 615 return(EWOULDBLOCK); 616 } 617 return(0); 618 } 619 620 /* 621 * Helper function 622 * 623 * NOTE: nc_refs can be 0 (degenerate case during _cache_drop). 624 * 625 * nc_locktd must be NULLed out prior to nc_lockstatus getting cleared. 626 */ 627 static 628 void 629 _cache_unlock(struct namecache *ncp) 630 { 631 thread_t td __debugvar = curthread; 632 u_int count; 633 u_int ncount; 634 struct vnode *dropvp; 635 636 KKASSERT(ncp->nc_refs >= 0); 637 KKASSERT((ncp->nc_lockstatus & ~(NC_EXLOCK_REQ|NC_SHLOCK_REQ)) > 0); 638 KKASSERT((ncp->nc_lockstatus & NC_SHLOCK_FLAG) || ncp->nc_locktd == td); 639 640 count = ncp->nc_lockstatus; 641 cpu_ccfence(); 642 643 /* 644 * Clear nc_locktd prior to the atomic op (excl lock only) 645 */ 646 if ((count & ~(NC_EXLOCK_REQ|NC_SHLOCK_REQ)) == 1) 647 ncp->nc_locktd = NULL; 648 dropvp = NULL; 649 650 for (;;) { 651 if ((count & 652 ~(NC_EXLOCK_REQ|NC_SHLOCK_REQ|NC_SHLOCK_FLAG)) == 1) { 653 dropvp = ncp->nc_vp; 654 if (count & NC_EXLOCK_REQ) 655 ncount = count & NC_SHLOCK_REQ; /* cnt->0 */ 656 else 657 ncount = 0; 658 659 if (atomic_cmpset_int(&ncp->nc_lockstatus, 660 count, ncount)) { 661 if (count & NC_EXLOCK_REQ) 662 wakeup(&ncp->nc_locktd); 663 else if (count & NC_SHLOCK_REQ) 664 wakeup(ncp); 665 break; 666 } 667 dropvp = NULL; 668 } else { 669 KKASSERT((count & NC_SHLOCK_VHOLD) == 0); 670 KKASSERT((count & ~(NC_EXLOCK_REQ | 671 NC_SHLOCK_REQ | 672 NC_SHLOCK_FLAG)) > 1); 673 if (atomic_cmpset_int(&ncp->nc_lockstatus, 674 count, count - 1)) { 675 break; 676 } 677 } 678 count = ncp->nc_lockstatus; 679 cpu_ccfence(); 680 } 681 682 /* 683 * Don't actually drop the vp until we successfully clean out 684 * the lock, otherwise we may race another shared lock. 685 */ 686 if (dropvp) 687 vdrop(dropvp); 688 } 689 690 static 691 int 692 _cache_lockstatus(struct namecache *ncp) 693 { 694 if (ncp->nc_locktd == curthread) 695 return(LK_EXCLUSIVE); 696 if (ncp->nc_lockstatus & NC_SHLOCK_FLAG) 697 return(LK_SHARED); 698 return(-1); 699 } 700 701 /* 702 * cache_hold() and cache_drop() prevent the premature deletion of a 703 * namecache entry but do not prevent operations (such as zapping) on 704 * that namecache entry. 705 * 706 * This routine may only be called from outside this source module if 707 * nc_refs is already at least 1. 708 * 709 * This is a rare case where callers are allowed to hold a spinlock, 710 * so we can't ourselves. 711 */ 712 static __inline 713 struct namecache * 714 _cache_hold(struct namecache *ncp) 715 { 716 atomic_add_int(&ncp->nc_refs, 1); 717 return(ncp); 718 } 719 720 /* 721 * Drop a cache entry, taking care to deal with races. 722 * 723 * For potential 1->0 transitions we must hold the ncp lock to safely 724 * test its flags. An unresolved entry with no children must be zapped 725 * to avoid leaks. 726 * 727 * The call to cache_zap() itself will handle all remaining races and 728 * will decrement the ncp's refs regardless. If we are resolved or 729 * have children nc_refs can safely be dropped to 0 without having to 730 * zap the entry. 731 * 732 * NOTE: cache_zap() will re-check nc_refs and nc_list in a MPSAFE fashion. 733 * 734 * NOTE: cache_zap() may return a non-NULL referenced parent which must 735 * be dropped in a loop. 736 */ 737 static __inline 738 void 739 _cache_drop(struct namecache *ncp) 740 { 741 int refs; 742 743 while (ncp) { 744 KKASSERT(ncp->nc_refs > 0); 745 refs = ncp->nc_refs; 746 747 if (refs == 1) { 748 if (_cache_lock_nonblock(ncp) == 0) { 749 ncp->nc_flag &= ~NCF_DEFEREDZAP; 750 if ((ncp->nc_flag & NCF_UNRESOLVED) && 751 TAILQ_EMPTY(&ncp->nc_list)) { 752 ncp = cache_zap(ncp, 1); 753 continue; 754 } 755 if (atomic_cmpset_int(&ncp->nc_refs, 1, 0)) { 756 _cache_unlock(ncp); 757 break; 758 } 759 _cache_unlock(ncp); 760 } 761 } else { 762 if (atomic_cmpset_int(&ncp->nc_refs, refs, refs - 1)) 763 break; 764 } 765 cpu_pause(); 766 } 767 } 768 769 /* 770 * Link a new namecache entry to its parent and to the hash table. Be 771 * careful to avoid races if vhold() blocks in the future. 772 * 773 * Both ncp and par must be referenced and locked. 774 * 775 * NOTE: The hash table spinlock is held during this call, we can't do 776 * anything fancy. 777 */ 778 static void 779 _cache_link_parent(struct namecache *ncp, struct namecache *par, 780 struct nchash_head *nchpp) 781 { 782 KKASSERT(ncp->nc_parent == NULL); 783 ncp->nc_parent = par; 784 ncp->nc_head = nchpp; 785 786 /* 787 * Set inheritance flags. Note that the parent flags may be 788 * stale due to getattr potentially not having been run yet 789 * (it gets run during nlookup()'s). 790 */ 791 ncp->nc_flag &= ~(NCF_SF_PNOCACHE | NCF_UF_PCACHE); 792 if (par->nc_flag & (NCF_SF_NOCACHE | NCF_SF_PNOCACHE)) 793 ncp->nc_flag |= NCF_SF_PNOCACHE; 794 if (par->nc_flag & (NCF_UF_CACHE | NCF_UF_PCACHE)) 795 ncp->nc_flag |= NCF_UF_PCACHE; 796 797 LIST_INSERT_HEAD(&nchpp->list, ncp, nc_hash); 798 799 if (TAILQ_EMPTY(&par->nc_list)) { 800 TAILQ_INSERT_HEAD(&par->nc_list, ncp, nc_entry); 801 /* 802 * Any vp associated with an ncp which has children must 803 * be held to prevent it from being recycled. 804 */ 805 if (par->nc_vp) 806 vhold(par->nc_vp); 807 } else { 808 TAILQ_INSERT_HEAD(&par->nc_list, ncp, nc_entry); 809 } 810 } 811 812 /* 813 * Remove the parent and hash associations from a namecache structure. 814 * If this is the last child of the parent the cache_drop(par) will 815 * attempt to recursively zap the parent. 816 * 817 * ncp must be locked. This routine will acquire a temporary lock on 818 * the parent as wlel as the appropriate hash chain. 819 */ 820 static void 821 _cache_unlink_parent(struct namecache *ncp) 822 { 823 struct namecache *par; 824 struct vnode *dropvp; 825 826 if ((par = ncp->nc_parent) != NULL) { 827 KKASSERT(ncp->nc_parent == par); 828 _cache_hold(par); 829 _cache_lock(par); 830 spin_lock(&ncp->nc_head->spin); 831 LIST_REMOVE(ncp, nc_hash); 832 TAILQ_REMOVE(&par->nc_list, ncp, nc_entry); 833 dropvp = NULL; 834 if (par->nc_vp && TAILQ_EMPTY(&par->nc_list)) 835 dropvp = par->nc_vp; 836 spin_unlock(&ncp->nc_head->spin); 837 ncp->nc_parent = NULL; 838 ncp->nc_head = NULL; 839 _cache_unlock(par); 840 _cache_drop(par); 841 842 /* 843 * We can only safely vdrop with no spinlocks held. 844 */ 845 if (dropvp) 846 vdrop(dropvp); 847 } 848 } 849 850 /* 851 * Allocate a new namecache structure. Most of the code does not require 852 * zero-termination of the string but it makes vop_compat_ncreate() easier. 853 */ 854 static struct namecache * 855 cache_alloc(int nlen) 856 { 857 struct namecache *ncp; 858 859 ncp = kmalloc(sizeof(*ncp), M_VFSCACHE, M_WAITOK|M_ZERO); 860 if (nlen) 861 ncp->nc_name = kmalloc(nlen + 1, M_VFSCACHE, M_WAITOK); 862 ncp->nc_nlen = nlen; 863 ncp->nc_flag = NCF_UNRESOLVED; 864 ncp->nc_error = ENOTCONN; /* needs to be resolved */ 865 ncp->nc_refs = 1; 866 867 TAILQ_INIT(&ncp->nc_list); 868 _cache_lock(ncp); 869 return(ncp); 870 } 871 872 /* 873 * Can only be called for the case where the ncp has never been 874 * associated with anything (so no spinlocks are needed). 875 */ 876 static void 877 _cache_free(struct namecache *ncp) 878 { 879 KKASSERT(ncp->nc_refs == 1 && ncp->nc_lockstatus == 1); 880 if (ncp->nc_name) 881 kfree(ncp->nc_name, M_VFSCACHE); 882 kfree(ncp, M_VFSCACHE); 883 } 884 885 /* 886 * [re]initialize a nchandle. 887 */ 888 void 889 cache_zero(struct nchandle *nch) 890 { 891 nch->ncp = NULL; 892 nch->mount = NULL; 893 } 894 895 /* 896 * Ref and deref a namecache structure. 897 * 898 * The caller must specify a stable ncp pointer, typically meaning the 899 * ncp is already referenced but this can also occur indirectly through 900 * e.g. holding a lock on a direct child. 901 * 902 * WARNING: Caller may hold an unrelated read spinlock, which means we can't 903 * use read spinlocks here. 904 * 905 * MPSAFE if nch is 906 */ 907 struct nchandle * 908 cache_hold(struct nchandle *nch) 909 { 910 _cache_hold(nch->ncp); 911 atomic_add_int(&nch->mount->mnt_refs, 1); 912 return(nch); 913 } 914 915 /* 916 * Create a copy of a namecache handle for an already-referenced 917 * entry. 918 * 919 * MPSAFE if nch is 920 */ 921 void 922 cache_copy(struct nchandle *nch, struct nchandle *target) 923 { 924 *target = *nch; 925 if (target->ncp) 926 _cache_hold(target->ncp); 927 atomic_add_int(&nch->mount->mnt_refs, 1); 928 } 929 930 /* 931 * MPSAFE if nch is 932 */ 933 void 934 cache_changemount(struct nchandle *nch, struct mount *mp) 935 { 936 atomic_add_int(&nch->mount->mnt_refs, -1); 937 nch->mount = mp; 938 atomic_add_int(&nch->mount->mnt_refs, 1); 939 } 940 941 void 942 cache_drop(struct nchandle *nch) 943 { 944 atomic_add_int(&nch->mount->mnt_refs, -1); 945 _cache_drop(nch->ncp); 946 nch->ncp = NULL; 947 nch->mount = NULL; 948 } 949 950 int 951 cache_lockstatus(struct nchandle *nch) 952 { 953 return(_cache_lockstatus(nch->ncp)); 954 } 955 956 void 957 cache_lock(struct nchandle *nch) 958 { 959 _cache_lock(nch->ncp); 960 } 961 962 void 963 cache_lock_maybe_shared(struct nchandle *nch, int excl) 964 { 965 struct namecache *ncp = nch->ncp; 966 967 if (ncp_shared_lock_disable || excl || 968 (ncp->nc_flag & NCF_UNRESOLVED)) { 969 _cache_lock(ncp); 970 } else { 971 _cache_lock_shared(ncp); 972 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) { 973 if (ncp->nc_vp && (ncp->nc_vp->v_flag & VRECLAIMED)) { 974 _cache_unlock(ncp); 975 _cache_lock(ncp); 976 } 977 } else { 978 _cache_unlock(ncp); 979 _cache_lock(ncp); 980 } 981 } 982 } 983 984 /* 985 * Relock nch1 given an unlocked nch1 and a locked nch2. The caller 986 * is responsible for checking both for validity on return as they 987 * may have become invalid. 988 * 989 * We have to deal with potential deadlocks here, just ping pong 990 * the lock until we get it (we will always block somewhere when 991 * looping so this is not cpu-intensive). 992 * 993 * which = 0 nch1 not locked, nch2 is locked 994 * which = 1 nch1 is locked, nch2 is not locked 995 */ 996 void 997 cache_relock(struct nchandle *nch1, struct ucred *cred1, 998 struct nchandle *nch2, struct ucred *cred2) 999 { 1000 int which; 1001 1002 which = 0; 1003 1004 for (;;) { 1005 if (which == 0) { 1006 if (cache_lock_nonblock(nch1) == 0) { 1007 cache_resolve(nch1, cred1); 1008 break; 1009 } 1010 cache_unlock(nch2); 1011 cache_lock(nch1); 1012 cache_resolve(nch1, cred1); 1013 which = 1; 1014 } else { 1015 if (cache_lock_nonblock(nch2) == 0) { 1016 cache_resolve(nch2, cred2); 1017 break; 1018 } 1019 cache_unlock(nch1); 1020 cache_lock(nch2); 1021 cache_resolve(nch2, cred2); 1022 which = 0; 1023 } 1024 } 1025 } 1026 1027 int 1028 cache_lock_nonblock(struct nchandle *nch) 1029 { 1030 return(_cache_lock_nonblock(nch->ncp)); 1031 } 1032 1033 void 1034 cache_unlock(struct nchandle *nch) 1035 { 1036 _cache_unlock(nch->ncp); 1037 } 1038 1039 /* 1040 * ref-and-lock, unlock-and-deref functions. 1041 * 1042 * This function is primarily used by nlookup. Even though cache_lock 1043 * holds the vnode, it is possible that the vnode may have already 1044 * initiated a recyclement. 1045 * 1046 * We want cache_get() to return a definitively usable vnode or a 1047 * definitively unresolved ncp. 1048 */ 1049 static 1050 struct namecache * 1051 _cache_get(struct namecache *ncp) 1052 { 1053 _cache_hold(ncp); 1054 _cache_lock(ncp); 1055 if (ncp->nc_vp && (ncp->nc_vp->v_flag & VRECLAIMED)) 1056 _cache_setunresolved(ncp); 1057 return(ncp); 1058 } 1059 1060 /* 1061 * Attempt to obtain a shared lock on the ncp. A shared lock will only 1062 * be obtained if the ncp is resolved and the vnode (if not ENOENT) is 1063 * valid. Otherwise an exclusive lock will be acquired instead. 1064 */ 1065 static 1066 struct namecache * 1067 _cache_get_maybe_shared(struct namecache *ncp, int excl) 1068 { 1069 if (ncp_shared_lock_disable || excl || 1070 (ncp->nc_flag & NCF_UNRESOLVED)) { 1071 return(_cache_get(ncp)); 1072 } 1073 _cache_hold(ncp); 1074 _cache_lock_shared(ncp); 1075 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) { 1076 if (ncp->nc_vp && (ncp->nc_vp->v_flag & VRECLAIMED)) { 1077 _cache_unlock(ncp); 1078 ncp = _cache_get(ncp); 1079 _cache_drop(ncp); 1080 } 1081 } else { 1082 _cache_unlock(ncp); 1083 ncp = _cache_get(ncp); 1084 _cache_drop(ncp); 1085 } 1086 return(ncp); 1087 } 1088 1089 /* 1090 * This is a special form of _cache_lock() which only succeeds if 1091 * it can get a pristine, non-recursive lock. The caller must have 1092 * already ref'd the ncp. 1093 * 1094 * On success the ncp will be locked, on failure it will not. The 1095 * ref count does not change either way. 1096 * 1097 * We want _cache_lock_special() (on success) to return a definitively 1098 * usable vnode or a definitively unresolved ncp. 1099 */ 1100 static int 1101 _cache_lock_special(struct namecache *ncp) 1102 { 1103 if (_cache_lock_nonblock(ncp) == 0) { 1104 if ((ncp->nc_lockstatus & 1105 ~(NC_EXLOCK_REQ|NC_SHLOCK_REQ)) == 1) { 1106 if (ncp->nc_vp && (ncp->nc_vp->v_flag & VRECLAIMED)) 1107 _cache_setunresolved(ncp); 1108 return(0); 1109 } 1110 _cache_unlock(ncp); 1111 } 1112 return(EWOULDBLOCK); 1113 } 1114 1115 /* 1116 * This function tries to get a shared lock but will back-off to an exclusive 1117 * lock if: 1118 * 1119 * (1) Some other thread is trying to obtain an exclusive lock 1120 * (to prevent the exclusive requester from getting livelocked out 1121 * by many shared locks). 1122 * 1123 * (2) The current thread already owns an exclusive lock (to avoid 1124 * deadlocking). 1125 * 1126 * WARNING! On machines with lots of cores we really want to try hard to 1127 * get a shared lock or concurrent path lookups can chain-react 1128 * into a very high-latency exclusive lock. 1129 */ 1130 static int 1131 _cache_lock_shared_special(struct namecache *ncp) 1132 { 1133 /* 1134 * Only honor a successful shared lock (returning 0) if there is 1135 * no exclusive request pending and the vnode, if present, is not 1136 * in a reclaimed state. 1137 */ 1138 if (_cache_lock_shared_nonblock(ncp) == 0) { 1139 if ((ncp->nc_lockstatus & NC_EXLOCK_REQ) == 0) { 1140 if (ncp->nc_vp == NULL || 1141 (ncp->nc_vp->v_flag & VRECLAIMED) == 0) { 1142 return(0); 1143 } 1144 } 1145 _cache_unlock(ncp); 1146 return(EWOULDBLOCK); 1147 } 1148 1149 /* 1150 * Non-blocking shared lock failed. If we already own the exclusive 1151 * lock just acquire another exclusive lock (instead of deadlocking). 1152 * Otherwise acquire a shared lock. 1153 */ 1154 if (ncp->nc_locktd == curthread) { 1155 _cache_lock(ncp); 1156 return(0); 1157 } 1158 _cache_lock_shared(ncp); 1159 return(0); 1160 } 1161 1162 1163 /* 1164 * NOTE: The same nchandle can be passed for both arguments. 1165 */ 1166 void 1167 cache_get(struct nchandle *nch, struct nchandle *target) 1168 { 1169 KKASSERT(nch->ncp->nc_refs > 0); 1170 target->mount = nch->mount; 1171 target->ncp = _cache_get(nch->ncp); 1172 atomic_add_int(&target->mount->mnt_refs, 1); 1173 } 1174 1175 void 1176 cache_get_maybe_shared(struct nchandle *nch, struct nchandle *target, int excl) 1177 { 1178 KKASSERT(nch->ncp->nc_refs > 0); 1179 target->mount = nch->mount; 1180 target->ncp = _cache_get_maybe_shared(nch->ncp, excl); 1181 atomic_add_int(&target->mount->mnt_refs, 1); 1182 } 1183 1184 /* 1185 * 1186 */ 1187 static __inline 1188 void 1189 _cache_put(struct namecache *ncp) 1190 { 1191 _cache_unlock(ncp); 1192 _cache_drop(ncp); 1193 } 1194 1195 /* 1196 * 1197 */ 1198 void 1199 cache_put(struct nchandle *nch) 1200 { 1201 atomic_add_int(&nch->mount->mnt_refs, -1); 1202 _cache_put(nch->ncp); 1203 nch->ncp = NULL; 1204 nch->mount = NULL; 1205 } 1206 1207 /* 1208 * Resolve an unresolved ncp by associating a vnode with it. If the 1209 * vnode is NULL, a negative cache entry is created. 1210 * 1211 * The ncp should be locked on entry and will remain locked on return. 1212 */ 1213 static 1214 void 1215 _cache_setvp(struct mount *mp, struct namecache *ncp, struct vnode *vp) 1216 { 1217 KKASSERT(ncp->nc_flag & NCF_UNRESOLVED); 1218 KKASSERT(_cache_lockstatus(ncp) == LK_EXCLUSIVE); 1219 1220 if (vp != NULL) { 1221 /* 1222 * Any vp associated with an ncp which has children must 1223 * be held. Any vp associated with a locked ncp must be held. 1224 */ 1225 if (!TAILQ_EMPTY(&ncp->nc_list)) 1226 vhold(vp); 1227 spin_lock(&vp->v_spin); 1228 ncp->nc_vp = vp; 1229 TAILQ_INSERT_HEAD(&vp->v_namecache, ncp, nc_vnode); 1230 spin_unlock(&vp->v_spin); 1231 if (ncp->nc_lockstatus & ~(NC_EXLOCK_REQ|NC_SHLOCK_REQ)) 1232 vhold(vp); 1233 1234 /* 1235 * Set auxiliary flags 1236 */ 1237 switch(vp->v_type) { 1238 case VDIR: 1239 ncp->nc_flag |= NCF_ISDIR; 1240 break; 1241 case VLNK: 1242 ncp->nc_flag |= NCF_ISSYMLINK; 1243 /* XXX cache the contents of the symlink */ 1244 break; 1245 default: 1246 break; 1247 } 1248 atomic_add_int(&numcache, 1); 1249 ncp->nc_error = 0; 1250 /* XXX: this is a hack to work-around the lack of a real pfs vfs 1251 * implementation*/ 1252 if (mp != NULL) 1253 if (strncmp(mp->mnt_stat.f_fstypename, "null", 5) == 0) 1254 vp->v_pfsmp = mp; 1255 } else { 1256 /* 1257 * When creating a negative cache hit we set the 1258 * namecache_gen. A later resolve will clean out the 1259 * negative cache hit if the mount point's namecache_gen 1260 * has changed. Used by devfs, could also be used by 1261 * other remote FSs. 1262 */ 1263 ncp->nc_vp = NULL; 1264 spin_lock(&ncspin); 1265 TAILQ_INSERT_TAIL(&ncneglist, ncp, nc_vnode); 1266 ++numneg; 1267 spin_unlock(&ncspin); 1268 ncp->nc_error = ENOENT; 1269 if (mp) 1270 VFS_NCPGEN_SET(mp, ncp); 1271 } 1272 ncp->nc_flag &= ~(NCF_UNRESOLVED | NCF_DEFEREDZAP); 1273 } 1274 1275 /* 1276 * 1277 */ 1278 void 1279 cache_setvp(struct nchandle *nch, struct vnode *vp) 1280 { 1281 _cache_setvp(nch->mount, nch->ncp, vp); 1282 } 1283 1284 /* 1285 * 1286 */ 1287 void 1288 cache_settimeout(struct nchandle *nch, int nticks) 1289 { 1290 struct namecache *ncp = nch->ncp; 1291 1292 if ((ncp->nc_timeout = ticks + nticks) == 0) 1293 ncp->nc_timeout = 1; 1294 } 1295 1296 /* 1297 * Disassociate the vnode or negative-cache association and mark a 1298 * namecache entry as unresolved again. Note that the ncp is still 1299 * left in the hash table and still linked to its parent. 1300 * 1301 * The ncp should be locked and refd on entry and will remain locked and refd 1302 * on return. 1303 * 1304 * This routine is normally never called on a directory containing children. 1305 * However, NFS often does just that in its rename() code as a cop-out to 1306 * avoid complex namespace operations. This disconnects a directory vnode 1307 * from its namecache and can cause the OLDAPI and NEWAPI to get out of 1308 * sync. 1309 * 1310 */ 1311 static 1312 void 1313 _cache_setunresolved(struct namecache *ncp) 1314 { 1315 struct vnode *vp; 1316 1317 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) { 1318 ncp->nc_flag |= NCF_UNRESOLVED; 1319 ncp->nc_timeout = 0; 1320 ncp->nc_error = ENOTCONN; 1321 if ((vp = ncp->nc_vp) != NULL) { 1322 atomic_add_int(&numcache, -1); 1323 spin_lock(&vp->v_spin); 1324 ncp->nc_vp = NULL; 1325 TAILQ_REMOVE(&vp->v_namecache, ncp, nc_vnode); 1326 spin_unlock(&vp->v_spin); 1327 1328 /* 1329 * Any vp associated with an ncp with children is 1330 * held by that ncp. Any vp associated with a locked 1331 * ncp is held by that ncp. These conditions must be 1332 * undone when the vp is cleared out from the ncp. 1333 */ 1334 if (!TAILQ_EMPTY(&ncp->nc_list)) 1335 vdrop(vp); 1336 if (ncp->nc_lockstatus & ~(NC_EXLOCK_REQ|NC_SHLOCK_REQ)) 1337 vdrop(vp); 1338 } else { 1339 spin_lock(&ncspin); 1340 TAILQ_REMOVE(&ncneglist, ncp, nc_vnode); 1341 --numneg; 1342 spin_unlock(&ncspin); 1343 } 1344 ncp->nc_flag &= ~(NCF_WHITEOUT|NCF_ISDIR|NCF_ISSYMLINK); 1345 } 1346 } 1347 1348 /* 1349 * The cache_nresolve() code calls this function to automatically 1350 * set a resolved cache element to unresolved if it has timed out 1351 * or if it is a negative cache hit and the mount point namecache_gen 1352 * has changed. 1353 */ 1354 static __inline int 1355 _cache_auto_unresolve_test(struct mount *mp, struct namecache *ncp) 1356 { 1357 /* 1358 * Try to zap entries that have timed out. We have 1359 * to be careful here because locked leafs may depend 1360 * on the vnode remaining intact in a parent, so only 1361 * do this under very specific conditions. 1362 */ 1363 if (ncp->nc_timeout && (int)(ncp->nc_timeout - ticks) < 0 && 1364 TAILQ_EMPTY(&ncp->nc_list)) { 1365 return 1; 1366 } 1367 1368 /* 1369 * If a resolved negative cache hit is invalid due to 1370 * the mount's namecache generation being bumped, zap it. 1371 */ 1372 if (ncp->nc_vp == NULL && VFS_NCPGEN_TEST(mp, ncp)) { 1373 return 1; 1374 } 1375 1376 /* 1377 * Otherwise we are good 1378 */ 1379 return 0; 1380 } 1381 1382 static __inline void 1383 _cache_auto_unresolve(struct mount *mp, struct namecache *ncp) 1384 { 1385 /* 1386 * Already in an unresolved state, nothing to do. 1387 */ 1388 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) { 1389 if (_cache_auto_unresolve_test(mp, ncp)) 1390 _cache_setunresolved(ncp); 1391 } 1392 } 1393 1394 /* 1395 * 1396 */ 1397 void 1398 cache_setunresolved(struct nchandle *nch) 1399 { 1400 _cache_setunresolved(nch->ncp); 1401 } 1402 1403 /* 1404 * Determine if we can clear NCF_ISMOUNTPT by scanning the mountlist 1405 * looking for matches. This flag tells the lookup code when it must 1406 * check for a mount linkage and also prevents the directories in question 1407 * from being deleted or renamed. 1408 */ 1409 static 1410 int 1411 cache_clrmountpt_callback(struct mount *mp, void *data) 1412 { 1413 struct nchandle *nch = data; 1414 1415 if (mp->mnt_ncmounton.ncp == nch->ncp) 1416 return(1); 1417 if (mp->mnt_ncmountpt.ncp == nch->ncp) 1418 return(1); 1419 return(0); 1420 } 1421 1422 /* 1423 * 1424 */ 1425 void 1426 cache_clrmountpt(struct nchandle *nch) 1427 { 1428 int count; 1429 1430 count = mountlist_scan(cache_clrmountpt_callback, nch, 1431 MNTSCAN_FORWARD|MNTSCAN_NOBUSY); 1432 if (count == 0) 1433 nch->ncp->nc_flag &= ~NCF_ISMOUNTPT; 1434 } 1435 1436 /* 1437 * Invalidate portions of the namecache topology given a starting entry. 1438 * The passed ncp is set to an unresolved state and: 1439 * 1440 * The passed ncp must be referencxed and locked. The routine may unlock 1441 * and relock ncp several times, and will recheck the children and loop 1442 * to catch races. When done the passed ncp will be returned with the 1443 * reference and lock intact. 1444 * 1445 * CINV_DESTROY - Set a flag in the passed ncp entry indicating 1446 * that the physical underlying nodes have been 1447 * destroyed... as in deleted. For example, when 1448 * a directory is removed. This will cause record 1449 * lookups on the name to no longer be able to find 1450 * the record and tells the resolver to return failure 1451 * rather then trying to resolve through the parent. 1452 * 1453 * The topology itself, including ncp->nc_name, 1454 * remains intact. 1455 * 1456 * This only applies to the passed ncp, if CINV_CHILDREN 1457 * is specified the children are not flagged. 1458 * 1459 * CINV_CHILDREN - Set all children (recursively) to an unresolved 1460 * state as well. 1461 * 1462 * Note that this will also have the side effect of 1463 * cleaning out any unreferenced nodes in the topology 1464 * from the leaves up as the recursion backs out. 1465 * 1466 * Note that the topology for any referenced nodes remains intact, but 1467 * the nodes will be marked as having been destroyed and will be set 1468 * to an unresolved state. 1469 * 1470 * It is possible for cache_inval() to race a cache_resolve(), meaning that 1471 * the namecache entry may not actually be invalidated on return if it was 1472 * revalidated while recursing down into its children. This code guarentees 1473 * that the node(s) will go through an invalidation cycle, but does not 1474 * guarentee that they will remain in an invalidated state. 1475 * 1476 * Returns non-zero if a revalidation was detected during the invalidation 1477 * recursion, zero otherwise. Note that since only the original ncp is 1478 * locked the revalidation ultimately can only indicate that the original ncp 1479 * *MIGHT* no have been reresolved. 1480 * 1481 * DEEP RECURSION HANDLING - If a recursive invalidation recurses deeply we 1482 * have to avoid blowing out the kernel stack. We do this by saving the 1483 * deep namecache node and aborting the recursion, then re-recursing at that 1484 * node using a depth-first algorithm in order to allow multiple deep 1485 * recursions to chain through each other, then we restart the invalidation 1486 * from scratch. 1487 */ 1488 1489 struct cinvtrack { 1490 struct namecache *resume_ncp; 1491 int depth; 1492 }; 1493 1494 static int _cache_inval_internal(struct namecache *, int, struct cinvtrack *); 1495 1496 static 1497 int 1498 _cache_inval(struct namecache *ncp, int flags) 1499 { 1500 struct cinvtrack track; 1501 struct namecache *ncp2; 1502 int r; 1503 1504 track.depth = 0; 1505 track.resume_ncp = NULL; 1506 1507 for (;;) { 1508 r = _cache_inval_internal(ncp, flags, &track); 1509 if (track.resume_ncp == NULL) 1510 break; 1511 kprintf("Warning: deep namecache recursion at %s\n", 1512 ncp->nc_name); 1513 _cache_unlock(ncp); 1514 while ((ncp2 = track.resume_ncp) != NULL) { 1515 track.resume_ncp = NULL; 1516 _cache_lock(ncp2); 1517 _cache_inval_internal(ncp2, flags & ~CINV_DESTROY, 1518 &track); 1519 _cache_put(ncp2); 1520 } 1521 _cache_lock(ncp); 1522 } 1523 return(r); 1524 } 1525 1526 int 1527 cache_inval(struct nchandle *nch, int flags) 1528 { 1529 return(_cache_inval(nch->ncp, flags)); 1530 } 1531 1532 /* 1533 * Helper for _cache_inval(). The passed ncp is refd and locked and 1534 * remains that way on return, but may be unlocked/relocked multiple 1535 * times by the routine. 1536 */ 1537 static int 1538 _cache_inval_internal(struct namecache *ncp, int flags, struct cinvtrack *track) 1539 { 1540 struct namecache *nextkid; 1541 int rcnt = 0; 1542 1543 KKASSERT(_cache_lockstatus(ncp) == LK_EXCLUSIVE); 1544 1545 _cache_setunresolved(ncp); 1546 if (flags & CINV_DESTROY) { 1547 ncp->nc_flag |= NCF_DESTROYED; 1548 ++ncp->nc_generation; 1549 } 1550 while ((flags & CINV_CHILDREN) && 1551 (nextkid = TAILQ_FIRST(&ncp->nc_list)) != NULL 1552 ) { 1553 struct namecache *kid; 1554 int restart; 1555 1556 restart = 0; 1557 _cache_hold(nextkid); 1558 if (++track->depth > MAX_RECURSION_DEPTH) { 1559 track->resume_ncp = ncp; 1560 _cache_hold(ncp); 1561 ++rcnt; 1562 } 1563 while ((kid = nextkid) != NULL) { 1564 /* 1565 * Parent (ncp) must be locked for the iteration. 1566 */ 1567 nextkid = NULL; 1568 if (kid->nc_parent != ncp) { 1569 _cache_drop(kid); 1570 kprintf("cache_inval_internal restartA %s\n", ncp->nc_name); 1571 restart = 1; 1572 break; 1573 } 1574 if ((nextkid = TAILQ_NEXT(kid, nc_entry)) != NULL) 1575 _cache_hold(nextkid); 1576 1577 /* 1578 * Parent unlocked for this section to avoid 1579 * deadlocks. 1580 */ 1581 _cache_unlock(ncp); 1582 if (track->resume_ncp) { 1583 _cache_drop(kid); 1584 _cache_lock(ncp); 1585 break; 1586 } 1587 if ((kid->nc_flag & NCF_UNRESOLVED) == 0 || 1588 TAILQ_FIRST(&kid->nc_list) 1589 ) { 1590 _cache_lock(kid); 1591 if (kid->nc_parent != ncp) { 1592 kprintf("cache_inval_internal restartB %s\n", ncp->nc_name); 1593 restart = 1; 1594 _cache_unlock(kid); 1595 _cache_drop(kid); 1596 _cache_lock(ncp); 1597 break; 1598 } 1599 1600 rcnt += _cache_inval_internal(kid, flags & ~CINV_DESTROY, track); 1601 _cache_unlock(kid); 1602 } 1603 _cache_drop(kid); 1604 _cache_lock(ncp); 1605 } 1606 if (nextkid) 1607 _cache_drop(nextkid); 1608 --track->depth; 1609 if (restart == 0) 1610 break; 1611 } 1612 1613 /* 1614 * Someone could have gotten in there while ncp was unlocked, 1615 * retry if so. 1616 */ 1617 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) 1618 ++rcnt; 1619 return (rcnt); 1620 } 1621 1622 /* 1623 * Invalidate a vnode's namecache associations. To avoid races against 1624 * the resolver we do not invalidate a node which we previously invalidated 1625 * but which was then re-resolved while we were in the invalidation loop. 1626 * 1627 * Returns non-zero if any namecache entries remain after the invalidation 1628 * loop completed. 1629 * 1630 * NOTE: Unlike the namecache topology which guarentees that ncp's will not 1631 * be ripped out of the topology while held, the vnode's v_namecache 1632 * list has no such restriction. NCP's can be ripped out of the list 1633 * at virtually any time if not locked, even if held. 1634 * 1635 * In addition, the v_namecache list itself must be locked via 1636 * the vnode's spinlock. 1637 */ 1638 int 1639 cache_inval_vp(struct vnode *vp, int flags) 1640 { 1641 struct namecache *ncp; 1642 struct namecache *next; 1643 1644 restart: 1645 spin_lock(&vp->v_spin); 1646 ncp = TAILQ_FIRST(&vp->v_namecache); 1647 if (ncp) 1648 _cache_hold(ncp); 1649 while (ncp) { 1650 /* loop entered with ncp held and vp spin-locked */ 1651 if ((next = TAILQ_NEXT(ncp, nc_vnode)) != NULL) 1652 _cache_hold(next); 1653 spin_unlock(&vp->v_spin); 1654 _cache_lock(ncp); 1655 if (ncp->nc_vp != vp) { 1656 kprintf("Warning: cache_inval_vp: race-A detected on " 1657 "%s\n", ncp->nc_name); 1658 _cache_put(ncp); 1659 if (next) 1660 _cache_drop(next); 1661 goto restart; 1662 } 1663 _cache_inval(ncp, flags); 1664 _cache_put(ncp); /* also releases reference */ 1665 ncp = next; 1666 spin_lock(&vp->v_spin); 1667 if (ncp && ncp->nc_vp != vp) { 1668 spin_unlock(&vp->v_spin); 1669 kprintf("Warning: cache_inval_vp: race-B detected on " 1670 "%s\n", ncp->nc_name); 1671 _cache_drop(ncp); 1672 goto restart; 1673 } 1674 } 1675 spin_unlock(&vp->v_spin); 1676 return(TAILQ_FIRST(&vp->v_namecache) != NULL); 1677 } 1678 1679 /* 1680 * This routine is used instead of the normal cache_inval_vp() when we 1681 * are trying to recycle otherwise good vnodes. 1682 * 1683 * Return 0 on success, non-zero if not all namecache records could be 1684 * disassociated from the vnode (for various reasons). 1685 */ 1686 int 1687 cache_inval_vp_nonblock(struct vnode *vp) 1688 { 1689 struct namecache *ncp; 1690 struct namecache *next; 1691 1692 spin_lock(&vp->v_spin); 1693 ncp = TAILQ_FIRST(&vp->v_namecache); 1694 if (ncp) 1695 _cache_hold(ncp); 1696 while (ncp) { 1697 /* loop entered with ncp held */ 1698 if ((next = TAILQ_NEXT(ncp, nc_vnode)) != NULL) 1699 _cache_hold(next); 1700 spin_unlock(&vp->v_spin); 1701 if (_cache_lock_nonblock(ncp)) { 1702 _cache_drop(ncp); 1703 if (next) 1704 _cache_drop(next); 1705 goto done; 1706 } 1707 if (ncp->nc_vp != vp) { 1708 kprintf("Warning: cache_inval_vp: race-A detected on " 1709 "%s\n", ncp->nc_name); 1710 _cache_put(ncp); 1711 if (next) 1712 _cache_drop(next); 1713 goto done; 1714 } 1715 _cache_inval(ncp, 0); 1716 _cache_put(ncp); /* also releases reference */ 1717 ncp = next; 1718 spin_lock(&vp->v_spin); 1719 if (ncp && ncp->nc_vp != vp) { 1720 spin_unlock(&vp->v_spin); 1721 kprintf("Warning: cache_inval_vp: race-B detected on " 1722 "%s\n", ncp->nc_name); 1723 _cache_drop(ncp); 1724 goto done; 1725 } 1726 } 1727 spin_unlock(&vp->v_spin); 1728 done: 1729 return(TAILQ_FIRST(&vp->v_namecache) != NULL); 1730 } 1731 1732 /* 1733 * The source ncp has been renamed to the target ncp. Both fncp and tncp 1734 * must be locked. The target ncp is destroyed (as a normal rename-over 1735 * would destroy the target file or directory). 1736 * 1737 * Because there may be references to the source ncp we cannot copy its 1738 * contents to the target. Instead the source ncp is relinked as the target 1739 * and the target ncp is removed from the namecache topology. 1740 */ 1741 void 1742 cache_rename(struct nchandle *fnch, struct nchandle *tnch) 1743 { 1744 struct namecache *fncp = fnch->ncp; 1745 struct namecache *tncp = tnch->ncp; 1746 struct namecache *tncp_par; 1747 struct nchash_head *nchpp; 1748 u_int32_t hash; 1749 char *oname; 1750 char *nname; 1751 1752 ++fncp->nc_generation; 1753 ++tncp->nc_generation; 1754 if (tncp->nc_nlen) { 1755 nname = kmalloc(tncp->nc_nlen + 1, M_VFSCACHE, M_WAITOK); 1756 bcopy(tncp->nc_name, nname, tncp->nc_nlen); 1757 nname[tncp->nc_nlen] = 0; 1758 } else { 1759 nname = NULL; 1760 } 1761 1762 /* 1763 * Rename fncp (unlink) 1764 */ 1765 _cache_unlink_parent(fncp); 1766 oname = fncp->nc_name; 1767 fncp->nc_name = nname; 1768 fncp->nc_nlen = tncp->nc_nlen; 1769 if (oname) 1770 kfree(oname, M_VFSCACHE); 1771 1772 tncp_par = tncp->nc_parent; 1773 _cache_hold(tncp_par); 1774 _cache_lock(tncp_par); 1775 1776 /* 1777 * Rename fncp (relink) 1778 */ 1779 hash = fnv_32_buf(fncp->nc_name, fncp->nc_nlen, FNV1_32_INIT); 1780 hash = fnv_32_buf(&tncp_par, sizeof(tncp_par), hash); 1781 nchpp = NCHHASH(hash); 1782 1783 spin_lock(&nchpp->spin); 1784 _cache_link_parent(fncp, tncp_par, nchpp); 1785 spin_unlock(&nchpp->spin); 1786 1787 _cache_put(tncp_par); 1788 1789 /* 1790 * Get rid of the overwritten tncp (unlink) 1791 */ 1792 _cache_unlink(tncp); 1793 } 1794 1795 /* 1796 * Perform actions consistent with unlinking a file. The passed-in ncp 1797 * must be locked. 1798 * 1799 * The ncp is marked DESTROYED so it no longer shows up in searches, 1800 * and will be physically deleted when the vnode goes away. 1801 * 1802 * If the related vnode has no refs then we cycle it through vget()/vput() 1803 * to (possibly if we don't have a ref race) trigger a deactivation, 1804 * allowing the VFS to trivially detect and recycle the deleted vnode 1805 * via VOP_INACTIVE(). 1806 * 1807 * NOTE: _cache_rename() will automatically call _cache_unlink() on the 1808 * target ncp. 1809 */ 1810 void 1811 cache_unlink(struct nchandle *nch) 1812 { 1813 _cache_unlink(nch->ncp); 1814 } 1815 1816 static void 1817 _cache_unlink(struct namecache *ncp) 1818 { 1819 struct vnode *vp; 1820 1821 /* 1822 * Causes lookups to fail and allows another ncp with the same 1823 * name to be created under ncp->nc_parent. 1824 */ 1825 ncp->nc_flag |= NCF_DESTROYED; 1826 ++ncp->nc_generation; 1827 1828 /* 1829 * Attempt to trigger a deactivation. Set VREF_FINALIZE to 1830 * force action on the 1->0 transition. 1831 */ 1832 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0 && 1833 (vp = ncp->nc_vp) != NULL) { 1834 atomic_set_int(&vp->v_refcnt, VREF_FINALIZE); 1835 if (VREFCNT(vp) <= 0) { 1836 if (vget(vp, LK_SHARED) == 0) 1837 vput(vp); 1838 } 1839 } 1840 } 1841 1842 /* 1843 * Return non-zero if the nch might be associated with an open and/or mmap()'d 1844 * file. The easy solution is to just return non-zero if the vnode has refs. 1845 * Used to interlock hammer2 reclaims (VREF_FINALIZE should already be set to 1846 * force the reclaim). 1847 */ 1848 int 1849 cache_isopen(struct nchandle *nch) 1850 { 1851 struct vnode *vp; 1852 struct namecache *ncp = nch->ncp; 1853 1854 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0 && 1855 (vp = ncp->nc_vp) != NULL && 1856 VREFCNT(vp)) { 1857 return 1; 1858 } 1859 return 0; 1860 } 1861 1862 1863 /* 1864 * vget the vnode associated with the namecache entry. Resolve the namecache 1865 * entry if necessary. The passed ncp must be referenced and locked. If 1866 * the ncp is resolved it might be locked shared. 1867 * 1868 * lk_type may be LK_SHARED, LK_EXCLUSIVE. A ref'd, possibly locked 1869 * (depending on the passed lk_type) will be returned in *vpp with an error 1870 * of 0, or NULL will be returned in *vpp with a non-0 error code. The 1871 * most typical error is ENOENT, meaning that the ncp represents a negative 1872 * cache hit and there is no vnode to retrieve, but other errors can occur 1873 * too. 1874 * 1875 * The vget() can race a reclaim. If this occurs we re-resolve the 1876 * namecache entry. 1877 * 1878 * There are numerous places in the kernel where vget() is called on a 1879 * vnode while one or more of its namecache entries is locked. Releasing 1880 * a vnode never deadlocks against locked namecache entries (the vnode 1881 * will not get recycled while referenced ncp's exist). This means we 1882 * can safely acquire the vnode. In fact, we MUST NOT release the ncp 1883 * lock when acquiring the vp lock or we might cause a deadlock. 1884 * 1885 * NOTE: The passed-in ncp must be locked exclusively if it is initially 1886 * unresolved. If a reclaim race occurs the passed-in ncp will be 1887 * relocked exclusively before being re-resolved. 1888 */ 1889 int 1890 cache_vget(struct nchandle *nch, struct ucred *cred, 1891 int lk_type, struct vnode **vpp) 1892 { 1893 struct namecache *ncp; 1894 struct vnode *vp; 1895 int error; 1896 1897 ncp = nch->ncp; 1898 again: 1899 vp = NULL; 1900 if (ncp->nc_flag & NCF_UNRESOLVED) 1901 error = cache_resolve(nch, cred); 1902 else 1903 error = 0; 1904 1905 if (error == 0 && (vp = ncp->nc_vp) != NULL) { 1906 error = vget(vp, lk_type); 1907 if (error) { 1908 /* 1909 * VRECLAIM race 1910 * 1911 * The ncp may have been locked shared, we must relock 1912 * it exclusively before we can set it to unresolved. 1913 */ 1914 if (error == ENOENT) { 1915 kprintf("Warning: vnode reclaim race detected " 1916 "in cache_vget on %p (%s)\n", 1917 vp, ncp->nc_name); 1918 _cache_unlock(ncp); 1919 _cache_lock(ncp); 1920 _cache_setunresolved(ncp); 1921 goto again; 1922 } 1923 1924 /* 1925 * Not a reclaim race, some other error. 1926 */ 1927 KKASSERT(ncp->nc_vp == vp); 1928 vp = NULL; 1929 } else { 1930 KKASSERT(ncp->nc_vp == vp); 1931 KKASSERT((vp->v_flag & VRECLAIMED) == 0); 1932 } 1933 } 1934 if (error == 0 && vp == NULL) 1935 error = ENOENT; 1936 *vpp = vp; 1937 return(error); 1938 } 1939 1940 /* 1941 * Similar to cache_vget() but only acquires a ref on the vnode. 1942 * 1943 * NOTE: The passed-in ncp must be locked exclusively if it is initially 1944 * unresolved. If a reclaim race occurs the passed-in ncp will be 1945 * relocked exclusively before being re-resolved. 1946 */ 1947 int 1948 cache_vref(struct nchandle *nch, struct ucred *cred, struct vnode **vpp) 1949 { 1950 struct namecache *ncp; 1951 struct vnode *vp; 1952 int error; 1953 1954 ncp = nch->ncp; 1955 again: 1956 vp = NULL; 1957 if (ncp->nc_flag & NCF_UNRESOLVED) 1958 error = cache_resolve(nch, cred); 1959 else 1960 error = 0; 1961 1962 if (error == 0 && (vp = ncp->nc_vp) != NULL) { 1963 error = vget(vp, LK_SHARED); 1964 if (error) { 1965 /* 1966 * VRECLAIM race 1967 */ 1968 if (error == ENOENT) { 1969 kprintf("Warning: vnode reclaim race detected " 1970 "in cache_vget on %p (%s)\n", 1971 vp, ncp->nc_name); 1972 _cache_unlock(ncp); 1973 _cache_lock(ncp); 1974 _cache_setunresolved(ncp); 1975 goto again; 1976 } 1977 1978 /* 1979 * Not a reclaim race, some other error. 1980 */ 1981 KKASSERT(ncp->nc_vp == vp); 1982 vp = NULL; 1983 } else { 1984 KKASSERT(ncp->nc_vp == vp); 1985 KKASSERT((vp->v_flag & VRECLAIMED) == 0); 1986 /* caller does not want a lock */ 1987 vn_unlock(vp); 1988 } 1989 } 1990 if (error == 0 && vp == NULL) 1991 error = ENOENT; 1992 *vpp = vp; 1993 return(error); 1994 } 1995 1996 /* 1997 * Return a referenced vnode representing the parent directory of 1998 * ncp. 1999 * 2000 * Because the caller has locked the ncp it should not be possible for 2001 * the parent ncp to go away. However, the parent can unresolve its 2002 * dvp at any time so we must be able to acquire a lock on the parent 2003 * to safely access nc_vp. 2004 * 2005 * We have to leave par unlocked when vget()ing dvp to avoid a deadlock, 2006 * so use vhold()/vdrop() while holding the lock to prevent dvp from 2007 * getting destroyed. 2008 * 2009 * NOTE: vhold() is allowed when dvp has 0 refs if we hold a 2010 * lock on the ncp in question.. 2011 */ 2012 static struct vnode * 2013 cache_dvpref(struct namecache *ncp) 2014 { 2015 struct namecache *par; 2016 struct vnode *dvp; 2017 2018 dvp = NULL; 2019 if ((par = ncp->nc_parent) != NULL) { 2020 _cache_hold(par); 2021 _cache_lock(par); 2022 if ((par->nc_flag & NCF_UNRESOLVED) == 0) { 2023 if ((dvp = par->nc_vp) != NULL) 2024 vhold(dvp); 2025 } 2026 _cache_unlock(par); 2027 if (dvp) { 2028 if (vget(dvp, LK_SHARED) == 0) { 2029 vn_unlock(dvp); 2030 vdrop(dvp); 2031 /* return refd, unlocked dvp */ 2032 } else { 2033 vdrop(dvp); 2034 dvp = NULL; 2035 } 2036 } 2037 _cache_drop(par); 2038 } 2039 return(dvp); 2040 } 2041 2042 /* 2043 * Convert a directory vnode to a namecache record without any other 2044 * knowledge of the topology. This ONLY works with directory vnodes and 2045 * is ONLY used by the NFS server. dvp must be refd but unlocked, and the 2046 * returned ncp (if not NULL) will be held and unlocked. 2047 * 2048 * If 'makeit' is 0 and dvp has no existing namecache record, NULL is returned. 2049 * If 'makeit' is 1 we attempt to track-down and create the namecache topology 2050 * for dvp. This will fail only if the directory has been deleted out from 2051 * under the caller. 2052 * 2053 * Callers must always check for a NULL return no matter the value of 'makeit'. 2054 * 2055 * To avoid underflowing the kernel stack each recursive call increments 2056 * the makeit variable. 2057 */ 2058 2059 static int cache_inefficient_scan(struct nchandle *nch, struct ucred *cred, 2060 struct vnode *dvp, char *fakename); 2061 static int cache_fromdvp_try(struct vnode *dvp, struct ucred *cred, 2062 struct vnode **saved_dvp); 2063 2064 int 2065 cache_fromdvp(struct vnode *dvp, struct ucred *cred, int makeit, 2066 struct nchandle *nch) 2067 { 2068 struct vnode *saved_dvp; 2069 struct vnode *pvp; 2070 char *fakename; 2071 int error; 2072 2073 nch->ncp = NULL; 2074 nch->mount = dvp->v_mount; 2075 saved_dvp = NULL; 2076 fakename = NULL; 2077 2078 /* 2079 * Handle the makeit == 0 degenerate case 2080 */ 2081 if (makeit == 0) { 2082 spin_lock_shared(&dvp->v_spin); 2083 nch->ncp = TAILQ_FIRST(&dvp->v_namecache); 2084 if (nch->ncp) 2085 cache_hold(nch); 2086 spin_unlock_shared(&dvp->v_spin); 2087 } 2088 2089 /* 2090 * Loop until resolution, inside code will break out on error. 2091 */ 2092 while (makeit) { 2093 /* 2094 * Break out if we successfully acquire a working ncp. 2095 */ 2096 spin_lock_shared(&dvp->v_spin); 2097 nch->ncp = TAILQ_FIRST(&dvp->v_namecache); 2098 if (nch->ncp) { 2099 cache_hold(nch); 2100 spin_unlock_shared(&dvp->v_spin); 2101 break; 2102 } 2103 spin_unlock_shared(&dvp->v_spin); 2104 2105 /* 2106 * If dvp is the root of its filesystem it should already 2107 * have a namecache pointer associated with it as a side 2108 * effect of the mount, but it may have been disassociated. 2109 */ 2110 if (dvp->v_flag & VROOT) { 2111 nch->ncp = _cache_get(nch->mount->mnt_ncmountpt.ncp); 2112 error = cache_resolve_mp(nch->mount); 2113 _cache_put(nch->ncp); 2114 if (ncvp_debug) { 2115 kprintf("cache_fromdvp: resolve root of mount %p error %d", 2116 dvp->v_mount, error); 2117 } 2118 if (error) { 2119 if (ncvp_debug) 2120 kprintf(" failed\n"); 2121 nch->ncp = NULL; 2122 break; 2123 } 2124 if (ncvp_debug) 2125 kprintf(" succeeded\n"); 2126 continue; 2127 } 2128 2129 /* 2130 * If we are recursed too deeply resort to an O(n^2) 2131 * algorithm to resolve the namecache topology. The 2132 * resolved pvp is left referenced in saved_dvp to 2133 * prevent the tree from being destroyed while we loop. 2134 */ 2135 if (makeit > 20) { 2136 error = cache_fromdvp_try(dvp, cred, &saved_dvp); 2137 if (error) { 2138 kprintf("lookupdotdot(longpath) failed %d " 2139 "dvp %p\n", error, dvp); 2140 nch->ncp = NULL; 2141 break; 2142 } 2143 continue; 2144 } 2145 2146 /* 2147 * Get the parent directory and resolve its ncp. 2148 */ 2149 if (fakename) { 2150 kfree(fakename, M_TEMP); 2151 fakename = NULL; 2152 } 2153 error = vop_nlookupdotdot(*dvp->v_ops, dvp, &pvp, cred, 2154 &fakename); 2155 if (error) { 2156 kprintf("lookupdotdot failed %d dvp %p\n", error, dvp); 2157 break; 2158 } 2159 vn_unlock(pvp); 2160 2161 /* 2162 * Reuse makeit as a recursion depth counter. On success 2163 * nch will be fully referenced. 2164 */ 2165 cache_fromdvp(pvp, cred, makeit + 1, nch); 2166 vrele(pvp); 2167 if (nch->ncp == NULL) 2168 break; 2169 2170 /* 2171 * Do an inefficient scan of pvp (embodied by ncp) to look 2172 * for dvp. This will create a namecache record for dvp on 2173 * success. We loop up to recheck on success. 2174 * 2175 * ncp and dvp are both held but not locked. 2176 */ 2177 error = cache_inefficient_scan(nch, cred, dvp, fakename); 2178 if (error) { 2179 kprintf("cache_fromdvp: scan %p (%s) failed on dvp=%p\n", 2180 pvp, nch->ncp->nc_name, dvp); 2181 cache_drop(nch); 2182 /* nch was NULLed out, reload mount */ 2183 nch->mount = dvp->v_mount; 2184 break; 2185 } 2186 if (ncvp_debug) { 2187 kprintf("cache_fromdvp: scan %p (%s) succeeded\n", 2188 pvp, nch->ncp->nc_name); 2189 } 2190 cache_drop(nch); 2191 /* nch was NULLed out, reload mount */ 2192 nch->mount = dvp->v_mount; 2193 } 2194 2195 /* 2196 * If nch->ncp is non-NULL it will have been held already. 2197 */ 2198 if (fakename) 2199 kfree(fakename, M_TEMP); 2200 if (saved_dvp) 2201 vrele(saved_dvp); 2202 if (nch->ncp) 2203 return (0); 2204 return (EINVAL); 2205 } 2206 2207 /* 2208 * Go up the chain of parent directories until we find something 2209 * we can resolve into the namecache. This is very inefficient. 2210 */ 2211 static 2212 int 2213 cache_fromdvp_try(struct vnode *dvp, struct ucred *cred, 2214 struct vnode **saved_dvp) 2215 { 2216 struct nchandle nch; 2217 struct vnode *pvp; 2218 int error; 2219 static time_t last_fromdvp_report; 2220 char *fakename; 2221 2222 /* 2223 * Loop getting the parent directory vnode until we get something we 2224 * can resolve in the namecache. 2225 */ 2226 vref(dvp); 2227 nch.mount = dvp->v_mount; 2228 nch.ncp = NULL; 2229 fakename = NULL; 2230 2231 for (;;) { 2232 if (fakename) { 2233 kfree(fakename, M_TEMP); 2234 fakename = NULL; 2235 } 2236 error = vop_nlookupdotdot(*dvp->v_ops, dvp, &pvp, cred, 2237 &fakename); 2238 if (error) { 2239 vrele(dvp); 2240 break; 2241 } 2242 vn_unlock(pvp); 2243 spin_lock_shared(&pvp->v_spin); 2244 if ((nch.ncp = TAILQ_FIRST(&pvp->v_namecache)) != NULL) { 2245 _cache_hold(nch.ncp); 2246 spin_unlock_shared(&pvp->v_spin); 2247 vrele(pvp); 2248 break; 2249 } 2250 spin_unlock_shared(&pvp->v_spin); 2251 if (pvp->v_flag & VROOT) { 2252 nch.ncp = _cache_get(pvp->v_mount->mnt_ncmountpt.ncp); 2253 error = cache_resolve_mp(nch.mount); 2254 _cache_unlock(nch.ncp); 2255 vrele(pvp); 2256 if (error) { 2257 _cache_drop(nch.ncp); 2258 nch.ncp = NULL; 2259 vrele(dvp); 2260 } 2261 break; 2262 } 2263 vrele(dvp); 2264 dvp = pvp; 2265 } 2266 if (error == 0) { 2267 if (last_fromdvp_report != time_uptime) { 2268 last_fromdvp_report = time_uptime; 2269 kprintf("Warning: extremely inefficient path " 2270 "resolution on %s\n", 2271 nch.ncp->nc_name); 2272 } 2273 error = cache_inefficient_scan(&nch, cred, dvp, fakename); 2274 2275 /* 2276 * Hopefully dvp now has a namecache record associated with 2277 * it. Leave it referenced to prevent the kernel from 2278 * recycling the vnode. Otherwise extremely long directory 2279 * paths could result in endless recycling. 2280 */ 2281 if (*saved_dvp) 2282 vrele(*saved_dvp); 2283 *saved_dvp = dvp; 2284 _cache_drop(nch.ncp); 2285 } 2286 if (fakename) 2287 kfree(fakename, M_TEMP); 2288 return (error); 2289 } 2290 2291 /* 2292 * Do an inefficient scan of the directory represented by ncp looking for 2293 * the directory vnode dvp. ncp must be held but not locked on entry and 2294 * will be held on return. dvp must be refd but not locked on entry and 2295 * will remain refd on return. 2296 * 2297 * Why do this at all? Well, due to its stateless nature the NFS server 2298 * converts file handles directly to vnodes without necessarily going through 2299 * the namecache ops that would otherwise create the namecache topology 2300 * leading to the vnode. We could either (1) Change the namecache algorithms 2301 * to allow disconnect namecache records that are re-merged opportunistically, 2302 * or (2) Make the NFS server backtrack and scan to recover a connected 2303 * namecache topology in order to then be able to issue new API lookups. 2304 * 2305 * It turns out that (1) is a huge mess. It takes a nice clean set of 2306 * namecache algorithms and introduces a lot of complication in every subsystem 2307 * that calls into the namecache to deal with the re-merge case, especially 2308 * since we are using the namecache to placehold negative lookups and the 2309 * vnode might not be immediately assigned. (2) is certainly far less 2310 * efficient then (1), but since we are only talking about directories here 2311 * (which are likely to remain cached), the case does not actually run all 2312 * that often and has the supreme advantage of not polluting the namecache 2313 * algorithms. 2314 * 2315 * If a fakename is supplied just construct a namecache entry using the 2316 * fake name. 2317 */ 2318 static int 2319 cache_inefficient_scan(struct nchandle *nch, struct ucred *cred, 2320 struct vnode *dvp, char *fakename) 2321 { 2322 struct nlcomponent nlc; 2323 struct nchandle rncp; 2324 struct dirent *den; 2325 struct vnode *pvp; 2326 struct vattr vat; 2327 struct iovec iov; 2328 struct uio uio; 2329 int blksize; 2330 int eofflag; 2331 int bytes; 2332 char *rbuf; 2333 int error; 2334 2335 vat.va_blocksize = 0; 2336 if ((error = VOP_GETATTR(dvp, &vat)) != 0) 2337 return (error); 2338 cache_lock(nch); 2339 error = cache_vref(nch, cred, &pvp); 2340 cache_unlock(nch); 2341 if (error) 2342 return (error); 2343 if (ncvp_debug) { 2344 kprintf("inefficient_scan of (%p,%s): directory iosize %ld " 2345 "vattr fileid = %lld\n", 2346 nch->ncp, nch->ncp->nc_name, 2347 vat.va_blocksize, 2348 (long long)vat.va_fileid); 2349 } 2350 2351 /* 2352 * Use the supplied fakename if not NULL. Fake names are typically 2353 * not in the actual filesystem hierarchy. This is used by HAMMER 2354 * to glue @@timestamp recursions together. 2355 */ 2356 if (fakename) { 2357 nlc.nlc_nameptr = fakename; 2358 nlc.nlc_namelen = strlen(fakename); 2359 rncp = cache_nlookup(nch, &nlc); 2360 goto done; 2361 } 2362 2363 if ((blksize = vat.va_blocksize) == 0) 2364 blksize = DEV_BSIZE; 2365 rbuf = kmalloc(blksize, M_TEMP, M_WAITOK); 2366 rncp.ncp = NULL; 2367 2368 eofflag = 0; 2369 uio.uio_offset = 0; 2370 again: 2371 iov.iov_base = rbuf; 2372 iov.iov_len = blksize; 2373 uio.uio_iov = &iov; 2374 uio.uio_iovcnt = 1; 2375 uio.uio_resid = blksize; 2376 uio.uio_segflg = UIO_SYSSPACE; 2377 uio.uio_rw = UIO_READ; 2378 uio.uio_td = curthread; 2379 2380 if (ncvp_debug >= 2) 2381 kprintf("cache_inefficient_scan: readdir @ %08x\n", (int)uio.uio_offset); 2382 error = VOP_READDIR(pvp, &uio, cred, &eofflag, NULL, NULL); 2383 if (error == 0) { 2384 den = (struct dirent *)rbuf; 2385 bytes = blksize - uio.uio_resid; 2386 2387 while (bytes > 0) { 2388 if (ncvp_debug >= 2) { 2389 kprintf("cache_inefficient_scan: %*.*s\n", 2390 den->d_namlen, den->d_namlen, 2391 den->d_name); 2392 } 2393 if (den->d_type != DT_WHT && 2394 den->d_ino == vat.va_fileid) { 2395 if (ncvp_debug) { 2396 kprintf("cache_inefficient_scan: " 2397 "MATCHED inode %lld path %s/%*.*s\n", 2398 (long long)vat.va_fileid, 2399 nch->ncp->nc_name, 2400 den->d_namlen, den->d_namlen, 2401 den->d_name); 2402 } 2403 nlc.nlc_nameptr = den->d_name; 2404 nlc.nlc_namelen = den->d_namlen; 2405 rncp = cache_nlookup(nch, &nlc); 2406 KKASSERT(rncp.ncp != NULL); 2407 break; 2408 } 2409 bytes -= _DIRENT_DIRSIZ(den); 2410 den = _DIRENT_NEXT(den); 2411 } 2412 if (rncp.ncp == NULL && eofflag == 0 && uio.uio_resid != blksize) 2413 goto again; 2414 } 2415 kfree(rbuf, M_TEMP); 2416 done: 2417 vrele(pvp); 2418 if (rncp.ncp) { 2419 if (rncp.ncp->nc_flag & NCF_UNRESOLVED) { 2420 _cache_setvp(rncp.mount, rncp.ncp, dvp); 2421 if (ncvp_debug >= 2) { 2422 kprintf("cache_inefficient_scan: setvp %s/%s = %p\n", 2423 nch->ncp->nc_name, rncp.ncp->nc_name, dvp); 2424 } 2425 } else { 2426 if (ncvp_debug >= 2) { 2427 kprintf("cache_inefficient_scan: setvp %s/%s already set %p/%p\n", 2428 nch->ncp->nc_name, rncp.ncp->nc_name, dvp, 2429 rncp.ncp->nc_vp); 2430 } 2431 } 2432 if (rncp.ncp->nc_vp == NULL) 2433 error = rncp.ncp->nc_error; 2434 /* 2435 * Release rncp after a successful nlookup. rncp was fully 2436 * referenced. 2437 */ 2438 cache_put(&rncp); 2439 } else { 2440 kprintf("cache_inefficient_scan: dvp %p NOT FOUND in %s\n", 2441 dvp, nch->ncp->nc_name); 2442 error = ENOENT; 2443 } 2444 return (error); 2445 } 2446 2447 /* 2448 * Zap a namecache entry. The ncp is unconditionally set to an unresolved 2449 * state, which disassociates it from its vnode or ncneglist. 2450 * 2451 * Then, if there are no additional references to the ncp and no children, 2452 * the ncp is removed from the topology and destroyed. 2453 * 2454 * References and/or children may exist if the ncp is in the middle of the 2455 * topology, preventing the ncp from being destroyed. 2456 * 2457 * This function must be called with the ncp held and locked and will unlock 2458 * and drop it during zapping. 2459 * 2460 * If nonblock is non-zero and the parent ncp cannot be locked we give up. 2461 * This case can occur in the cache_drop() path. 2462 * 2463 * This function may returned a held (but NOT locked) parent node which the 2464 * caller must drop. We do this so _cache_drop() can loop, to avoid 2465 * blowing out the kernel stack. 2466 * 2467 * WARNING! For MPSAFE operation this routine must acquire up to three 2468 * spin locks to be able to safely test nc_refs. Lock order is 2469 * very important. 2470 * 2471 * hash spinlock if on hash list 2472 * parent spinlock if child of parent 2473 * (the ncp is unresolved so there is no vnode association) 2474 */ 2475 static struct namecache * 2476 cache_zap(struct namecache *ncp, int nonblock) 2477 { 2478 struct namecache *par; 2479 struct vnode *dropvp; 2480 struct nchash_head *nchpp; 2481 int refs; 2482 2483 /* 2484 * Disassociate the vnode or negative cache ref and set NCF_UNRESOLVED. 2485 */ 2486 _cache_setunresolved(ncp); 2487 2488 /* 2489 * Try to scrap the entry and possibly tail-recurse on its parent. 2490 * We only scrap unref'd (other then our ref) unresolved entries, 2491 * we do not scrap 'live' entries. 2492 * 2493 * Note that once the spinlocks are acquired if nc_refs == 1 no 2494 * other references are possible. If it isn't, however, we have 2495 * to decrement but also be sure to avoid a 1->0 transition. 2496 */ 2497 KKASSERT(ncp->nc_flag & NCF_UNRESOLVED); 2498 KKASSERT(ncp->nc_refs > 0); 2499 2500 /* 2501 * Acquire locks. Note that the parent can't go away while we hold 2502 * a child locked. 2503 */ 2504 nchpp = NULL; 2505 if ((par = ncp->nc_parent) != NULL) { 2506 if (nonblock) { 2507 for (;;) { 2508 if (_cache_lock_nonblock(par) == 0) 2509 break; 2510 refs = ncp->nc_refs; 2511 ncp->nc_flag |= NCF_DEFEREDZAP; 2512 ++numdefered; /* MP race ok */ 2513 if (atomic_cmpset_int(&ncp->nc_refs, 2514 refs, refs - 1)) { 2515 _cache_unlock(ncp); 2516 return(NULL); 2517 } 2518 cpu_pause(); 2519 } 2520 _cache_hold(par); 2521 } else { 2522 _cache_hold(par); 2523 _cache_lock(par); 2524 } 2525 nchpp = ncp->nc_head; 2526 spin_lock(&nchpp->spin); 2527 } 2528 2529 /* 2530 * At this point if we find refs == 1 it should not be possible for 2531 * anyone else to have access to the ncp. We are holding the only 2532 * possible access point left (nchpp) spin-locked. 2533 * 2534 * If someone other then us has a ref or we have children 2535 * we cannot zap the entry. The 1->0 transition and any 2536 * further list operation is protected by the spinlocks 2537 * we have acquired but other transitions are not. 2538 */ 2539 for (;;) { 2540 refs = ncp->nc_refs; 2541 cpu_ccfence(); 2542 if (refs == 1 && TAILQ_EMPTY(&ncp->nc_list)) 2543 break; 2544 if (atomic_cmpset_int(&ncp->nc_refs, refs, refs - 1)) { 2545 if (par) { 2546 spin_unlock(&nchpp->spin); 2547 _cache_put(par); 2548 } 2549 _cache_unlock(ncp); 2550 return(NULL); 2551 } 2552 cpu_pause(); 2553 } 2554 2555 /* 2556 * We are the only ref and with the spinlocks held no further 2557 * refs can be acquired by others. 2558 * 2559 * Remove us from the hash list and parent list. We have to 2560 * drop a ref on the parent's vp if the parent's list becomes 2561 * empty. 2562 */ 2563 dropvp = NULL; 2564 if (par) { 2565 KKASSERT(nchpp == ncp->nc_head); 2566 LIST_REMOVE(ncp, nc_hash); 2567 TAILQ_REMOVE(&par->nc_list, ncp, nc_entry); 2568 if (par->nc_vp && TAILQ_EMPTY(&par->nc_list)) 2569 dropvp = par->nc_vp; 2570 ncp->nc_head = NULL; 2571 ncp->nc_parent = NULL; 2572 spin_unlock(&nchpp->spin); 2573 _cache_unlock(par); 2574 } else { 2575 KKASSERT(ncp->nc_head == NULL); 2576 } 2577 2578 /* 2579 * ncp should not have picked up any refs. Physically 2580 * destroy the ncp. 2581 */ 2582 if (ncp->nc_refs != 1) { 2583 int save_refs = ncp->nc_refs; 2584 cpu_ccfence(); 2585 panic("cache_zap: %p bad refs %d (%d)\n", 2586 ncp, save_refs, atomic_fetchadd_int(&ncp->nc_refs, 0)); 2587 } 2588 KKASSERT(ncp->nc_refs == 1); 2589 /* _cache_unlock(ncp) not required */ 2590 ncp->nc_refs = -1; /* safety */ 2591 if (ncp->nc_name) 2592 kfree(ncp->nc_name, M_VFSCACHE); 2593 kfree(ncp, M_VFSCACHE); 2594 2595 /* 2596 * Delayed drop (we had to release our spinlocks) 2597 * 2598 * The refed parent (if not NULL) must be dropped. The 2599 * caller is responsible for looping. 2600 */ 2601 if (dropvp) 2602 vdrop(dropvp); 2603 return(par); 2604 } 2605 2606 /* 2607 * Clean up dangling negative cache and defered-drop entries in the 2608 * namecache. 2609 * 2610 * This routine is called in the critical path and also called from 2611 * vnlru(). When called from vnlru we use a lower limit to try to 2612 * deal with the negative cache before the critical path has to start 2613 * dealing with it. 2614 */ 2615 typedef enum { CHI_LOW, CHI_HIGH } cache_hs_t; 2616 2617 static cache_hs_t neg_cache_hysteresis_state[2] = { CHI_LOW, CHI_LOW }; 2618 static cache_hs_t pos_cache_hysteresis_state[2] = { CHI_LOW, CHI_LOW }; 2619 2620 void 2621 cache_hysteresis(int critpath) 2622 { 2623 int poslimit; 2624 int neglimit = desiredvnodes / ncnegfactor; 2625 int xnumcache = numcache; 2626 2627 if (critpath == 0) 2628 neglimit = neglimit * 8 / 10; 2629 2630 /* 2631 * Don't cache too many negative hits. We use hysteresis to reduce 2632 * the impact on the critical path. 2633 */ 2634 switch(neg_cache_hysteresis_state[critpath]) { 2635 case CHI_LOW: 2636 if (numneg > MINNEG && numneg > neglimit) { 2637 if (critpath) 2638 _cache_cleanneg(ncnegflush); 2639 else 2640 _cache_cleanneg(ncnegflush + 2641 numneg - neglimit); 2642 neg_cache_hysteresis_state[critpath] = CHI_HIGH; 2643 } 2644 break; 2645 case CHI_HIGH: 2646 if (numneg > MINNEG * 9 / 10 && 2647 numneg * 9 / 10 > neglimit 2648 ) { 2649 if (critpath) 2650 _cache_cleanneg(ncnegflush); 2651 else 2652 _cache_cleanneg(ncnegflush + 2653 numneg * 9 / 10 - neglimit); 2654 } else { 2655 neg_cache_hysteresis_state[critpath] = CHI_LOW; 2656 } 2657 break; 2658 } 2659 2660 /* 2661 * Don't cache too many positive hits. We use hysteresis to reduce 2662 * the impact on the critical path. 2663 * 2664 * Excessive positive hits can accumulate due to large numbers of 2665 * hardlinks (the vnode cache will not prevent hl ncps from growing 2666 * into infinity). 2667 */ 2668 if ((poslimit = ncposlimit) == 0) 2669 poslimit = desiredvnodes * 2; 2670 if (critpath == 0) 2671 poslimit = poslimit * 8 / 10; 2672 2673 switch(pos_cache_hysteresis_state[critpath]) { 2674 case CHI_LOW: 2675 if (xnumcache > poslimit && xnumcache > MINPOS) { 2676 if (critpath) 2677 _cache_cleanpos(ncposflush); 2678 else 2679 _cache_cleanpos(ncposflush + 2680 xnumcache - poslimit); 2681 pos_cache_hysteresis_state[critpath] = CHI_HIGH; 2682 } 2683 break; 2684 case CHI_HIGH: 2685 if (xnumcache > poslimit * 5 / 6 && xnumcache > MINPOS) { 2686 if (critpath) 2687 _cache_cleanpos(ncposflush); 2688 else 2689 _cache_cleanpos(ncposflush + 2690 xnumcache - poslimit * 5 / 6); 2691 } else { 2692 pos_cache_hysteresis_state[critpath] = CHI_LOW; 2693 } 2694 break; 2695 } 2696 2697 /* 2698 * Clean out dangling defered-zap ncps which could not 2699 * be cleanly dropped if too many build up. Note 2700 * that numdefered is not an exact number as such ncps 2701 * can be reused and the counter is not handled in a MP 2702 * safe manner by design. 2703 */ 2704 if (numdefered > neglimit) { 2705 _cache_cleandefered(); 2706 } 2707 } 2708 2709 /* 2710 * NEW NAMECACHE LOOKUP API 2711 * 2712 * Lookup an entry in the namecache. The passed par_nch must be referenced 2713 * and unlocked. A referenced and locked nchandle with a non-NULL nch.ncp 2714 * is ALWAYS returned, eve if the supplied component is illegal. 2715 * 2716 * The resulting namecache entry should be returned to the system with 2717 * cache_put() or cache_unlock() + cache_drop(). 2718 * 2719 * namecache locks are recursive but care must be taken to avoid lock order 2720 * reversals (hence why the passed par_nch must be unlocked). Locking 2721 * rules are to order for parent traversals, not for child traversals. 2722 * 2723 * Nobody else will be able to manipulate the associated namespace (e.g. 2724 * create, delete, rename, rename-target) until the caller unlocks the 2725 * entry. 2726 * 2727 * The returned entry will be in one of three states: positive hit (non-null 2728 * vnode), negative hit (null vnode), or unresolved (NCF_UNRESOLVED is set). 2729 * Unresolved entries must be resolved through the filesystem to associate the 2730 * vnode and/or determine whether a positive or negative hit has occured. 2731 * 2732 * It is not necessary to lock a directory in order to lock namespace under 2733 * that directory. In fact, it is explicitly not allowed to do that. A 2734 * directory is typically only locked when being created, renamed, or 2735 * destroyed. 2736 * 2737 * The directory (par) may be unresolved, in which case any returned child 2738 * will likely also be marked unresolved. Likely but not guarenteed. Since 2739 * the filesystem lookup requires a resolved directory vnode the caller is 2740 * responsible for resolving the namecache chain top-down. This API 2741 * specifically allows whole chains to be created in an unresolved state. 2742 */ 2743 struct nchandle 2744 cache_nlookup(struct nchandle *par_nch, struct nlcomponent *nlc) 2745 { 2746 struct nchandle nch; 2747 struct namecache *ncp; 2748 struct namecache *new_ncp; 2749 struct nchash_head *nchpp; 2750 struct mount *mp; 2751 u_int32_t hash; 2752 globaldata_t gd; 2753 int par_locked; 2754 2755 numcalls++; 2756 gd = mycpu; 2757 mp = par_nch->mount; 2758 par_locked = 0; 2759 2760 /* 2761 * This is a good time to call it, no ncp's are locked by 2762 * the caller or us. 2763 */ 2764 cache_hysteresis(1); 2765 2766 /* 2767 * Try to locate an existing entry 2768 */ 2769 hash = fnv_32_buf(nlc->nlc_nameptr, nlc->nlc_namelen, FNV1_32_INIT); 2770 hash = fnv_32_buf(&par_nch->ncp, sizeof(par_nch->ncp), hash); 2771 new_ncp = NULL; 2772 nchpp = NCHHASH(hash); 2773 restart: 2774 if (new_ncp) 2775 spin_lock(&nchpp->spin); 2776 else 2777 spin_lock_shared(&nchpp->spin); 2778 2779 LIST_FOREACH(ncp, &nchpp->list, nc_hash) { 2780 numchecks++; 2781 2782 /* 2783 * Break out if we find a matching entry. Note that 2784 * UNRESOLVED entries may match, but DESTROYED entries 2785 * do not. 2786 */ 2787 if (ncp->nc_parent == par_nch->ncp && 2788 ncp->nc_nlen == nlc->nlc_namelen && 2789 bcmp(ncp->nc_name, nlc->nlc_nameptr, ncp->nc_nlen) == 0 && 2790 (ncp->nc_flag & NCF_DESTROYED) == 0 2791 ) { 2792 _cache_hold(ncp); 2793 if (new_ncp) 2794 spin_unlock(&nchpp->spin); 2795 else 2796 spin_unlock_shared(&nchpp->spin); 2797 if (par_locked) { 2798 _cache_unlock(par_nch->ncp); 2799 par_locked = 0; 2800 } 2801 if (_cache_lock_special(ncp) == 0) { 2802 /* 2803 * Successfully locked but we must re-test 2804 * conditions that might have changed since 2805 * we did not have the lock before. 2806 */ 2807 if (ncp->nc_parent != par_nch->ncp || 2808 ncp->nc_nlen != nlc->nlc_namelen || 2809 bcmp(ncp->nc_name, nlc->nlc_nameptr, 2810 ncp->nc_nlen) || 2811 (ncp->nc_flag & NCF_DESTROYED)) { 2812 _cache_put(ncp); 2813 goto restart; 2814 } 2815 _cache_auto_unresolve(mp, ncp); 2816 if (new_ncp) 2817 _cache_free(new_ncp); 2818 goto found; 2819 } 2820 _cache_get(ncp); /* cycle the lock to block */ 2821 _cache_put(ncp); 2822 _cache_drop(ncp); 2823 goto restart; 2824 } 2825 } 2826 2827 /* 2828 * We failed to locate an entry, create a new entry and add it to 2829 * the cache. The parent ncp must also be locked so we 2830 * can link into it. 2831 * 2832 * We have to relookup after possibly blocking in kmalloc or 2833 * when locking par_nch. 2834 * 2835 * NOTE: nlc_namelen can be 0 and nlc_nameptr NULL as a special 2836 * mount case, in which case nc_name will be NULL. 2837 */ 2838 if (new_ncp == NULL) { 2839 spin_unlock_shared(&nchpp->spin); 2840 new_ncp = cache_alloc(nlc->nlc_namelen); 2841 if (nlc->nlc_namelen) { 2842 bcopy(nlc->nlc_nameptr, new_ncp->nc_name, 2843 nlc->nlc_namelen); 2844 new_ncp->nc_name[nlc->nlc_namelen] = 0; 2845 } 2846 goto restart; 2847 } 2848 2849 /* 2850 * NOTE! The spinlock is held exclusively here because new_ncp 2851 * is non-NULL. 2852 */ 2853 if (par_locked == 0) { 2854 spin_unlock(&nchpp->spin); 2855 _cache_lock(par_nch->ncp); 2856 par_locked = 1; 2857 goto restart; 2858 } 2859 2860 /* 2861 * WARNING! We still hold the spinlock. We have to set the hash 2862 * table entry atomically. 2863 */ 2864 ncp = new_ncp; 2865 _cache_link_parent(ncp, par_nch->ncp, nchpp); 2866 spin_unlock(&nchpp->spin); 2867 _cache_unlock(par_nch->ncp); 2868 /* par_locked = 0 - not used */ 2869 found: 2870 /* 2871 * stats and namecache size management 2872 */ 2873 if (ncp->nc_flag & NCF_UNRESOLVED) 2874 ++gd->gd_nchstats->ncs_miss; 2875 else if (ncp->nc_vp) 2876 ++gd->gd_nchstats->ncs_goodhits; 2877 else 2878 ++gd->gd_nchstats->ncs_neghits; 2879 nch.mount = mp; 2880 nch.ncp = ncp; 2881 atomic_add_int(&nch.mount->mnt_refs, 1); 2882 return(nch); 2883 } 2884 2885 /* 2886 * Attempt to lookup a namecache entry and return with a shared namecache 2887 * lock. 2888 */ 2889 int 2890 cache_nlookup_maybe_shared(struct nchandle *par_nch, struct nlcomponent *nlc, 2891 int excl, struct nchandle *res_nch) 2892 { 2893 struct namecache *ncp; 2894 struct nchash_head *nchpp; 2895 struct mount *mp; 2896 u_int32_t hash; 2897 globaldata_t gd; 2898 2899 /* 2900 * If exclusive requested or shared namecache locks are disabled, 2901 * return failure. 2902 */ 2903 if (ncp_shared_lock_disable || excl) 2904 return(EWOULDBLOCK); 2905 2906 numcalls++; 2907 gd = mycpu; 2908 mp = par_nch->mount; 2909 2910 /* 2911 * This is a good time to call it, no ncp's are locked by 2912 * the caller or us. 2913 */ 2914 cache_hysteresis(1); 2915 2916 /* 2917 * Try to locate an existing entry 2918 */ 2919 hash = fnv_32_buf(nlc->nlc_nameptr, nlc->nlc_namelen, FNV1_32_INIT); 2920 hash = fnv_32_buf(&par_nch->ncp, sizeof(par_nch->ncp), hash); 2921 nchpp = NCHHASH(hash); 2922 2923 spin_lock_shared(&nchpp->spin); 2924 2925 LIST_FOREACH(ncp, &nchpp->list, nc_hash) { 2926 numchecks++; 2927 2928 /* 2929 * Break out if we find a matching entry. Note that 2930 * UNRESOLVED entries may match, but DESTROYED entries 2931 * do not. 2932 */ 2933 if (ncp->nc_parent == par_nch->ncp && 2934 ncp->nc_nlen == nlc->nlc_namelen && 2935 bcmp(ncp->nc_name, nlc->nlc_nameptr, ncp->nc_nlen) == 0 && 2936 (ncp->nc_flag & NCF_DESTROYED) == 0 2937 ) { 2938 _cache_hold(ncp); 2939 spin_unlock_shared(&nchpp->spin); 2940 if (_cache_lock_shared_special(ncp) == 0) { 2941 if (ncp->nc_parent == par_nch->ncp && 2942 ncp->nc_nlen == nlc->nlc_namelen && 2943 bcmp(ncp->nc_name, nlc->nlc_nameptr, 2944 ncp->nc_nlen) == 0 && 2945 (ncp->nc_flag & NCF_DESTROYED) == 0 && 2946 (ncp->nc_flag & NCF_UNRESOLVED) == 0 && 2947 _cache_auto_unresolve_test(mp, ncp) == 0) { 2948 goto found; 2949 } 2950 _cache_unlock(ncp); 2951 } 2952 _cache_drop(ncp); 2953 spin_lock_shared(&nchpp->spin); 2954 break; 2955 } 2956 } 2957 2958 /* 2959 * Failure 2960 */ 2961 spin_unlock_shared(&nchpp->spin); 2962 return(EWOULDBLOCK); 2963 2964 /* 2965 * Success 2966 * 2967 * Note that nc_error might be non-zero (e.g ENOENT). 2968 */ 2969 found: 2970 res_nch->mount = mp; 2971 res_nch->ncp = ncp; 2972 ++gd->gd_nchstats->ncs_goodhits; 2973 atomic_add_int(&res_nch->mount->mnt_refs, 1); 2974 2975 KKASSERT(ncp->nc_error != EWOULDBLOCK); 2976 return(ncp->nc_error); 2977 } 2978 2979 /* 2980 * This is a non-blocking verison of cache_nlookup() used by 2981 * nfs_readdirplusrpc_uio(). It can fail for any reason and 2982 * will return nch.ncp == NULL in that case. 2983 */ 2984 struct nchandle 2985 cache_nlookup_nonblock(struct nchandle *par_nch, struct nlcomponent *nlc) 2986 { 2987 struct nchandle nch; 2988 struct namecache *ncp; 2989 struct namecache *new_ncp; 2990 struct nchash_head *nchpp; 2991 struct mount *mp; 2992 u_int32_t hash; 2993 globaldata_t gd; 2994 int par_locked; 2995 2996 numcalls++; 2997 gd = mycpu; 2998 mp = par_nch->mount; 2999 par_locked = 0; 3000 3001 /* 3002 * Try to locate an existing entry 3003 */ 3004 hash = fnv_32_buf(nlc->nlc_nameptr, nlc->nlc_namelen, FNV1_32_INIT); 3005 hash = fnv_32_buf(&par_nch->ncp, sizeof(par_nch->ncp), hash); 3006 new_ncp = NULL; 3007 nchpp = NCHHASH(hash); 3008 restart: 3009 spin_lock(&nchpp->spin); 3010 LIST_FOREACH(ncp, &nchpp->list, nc_hash) { 3011 numchecks++; 3012 3013 /* 3014 * Break out if we find a matching entry. Note that 3015 * UNRESOLVED entries may match, but DESTROYED entries 3016 * do not. 3017 */ 3018 if (ncp->nc_parent == par_nch->ncp && 3019 ncp->nc_nlen == nlc->nlc_namelen && 3020 bcmp(ncp->nc_name, nlc->nlc_nameptr, ncp->nc_nlen) == 0 && 3021 (ncp->nc_flag & NCF_DESTROYED) == 0 3022 ) { 3023 _cache_hold(ncp); 3024 spin_unlock(&nchpp->spin); 3025 if (par_locked) { 3026 _cache_unlock(par_nch->ncp); 3027 par_locked = 0; 3028 } 3029 if (_cache_lock_special(ncp) == 0) { 3030 if (ncp->nc_parent != par_nch->ncp || 3031 ncp->nc_nlen != nlc->nlc_namelen || 3032 bcmp(ncp->nc_name, nlc->nlc_nameptr, ncp->nc_nlen) || 3033 (ncp->nc_flag & NCF_DESTROYED)) { 3034 kprintf("cache_lookup_nonblock: " 3035 "ncp-race %p %*.*s\n", 3036 ncp, 3037 nlc->nlc_namelen, 3038 nlc->nlc_namelen, 3039 nlc->nlc_nameptr); 3040 _cache_unlock(ncp); 3041 _cache_drop(ncp); 3042 goto failed; 3043 } 3044 _cache_auto_unresolve(mp, ncp); 3045 if (new_ncp) { 3046 _cache_free(new_ncp); 3047 new_ncp = NULL; 3048 } 3049 goto found; 3050 } 3051 _cache_drop(ncp); 3052 goto failed; 3053 } 3054 } 3055 3056 /* 3057 * We failed to locate an entry, create a new entry and add it to 3058 * the cache. The parent ncp must also be locked so we 3059 * can link into it. 3060 * 3061 * We have to relookup after possibly blocking in kmalloc or 3062 * when locking par_nch. 3063 * 3064 * NOTE: nlc_namelen can be 0 and nlc_nameptr NULL as a special 3065 * mount case, in which case nc_name will be NULL. 3066 */ 3067 if (new_ncp == NULL) { 3068 spin_unlock(&nchpp->spin); 3069 new_ncp = cache_alloc(nlc->nlc_namelen); 3070 if (nlc->nlc_namelen) { 3071 bcopy(nlc->nlc_nameptr, new_ncp->nc_name, 3072 nlc->nlc_namelen); 3073 new_ncp->nc_name[nlc->nlc_namelen] = 0; 3074 } 3075 goto restart; 3076 } 3077 if (par_locked == 0) { 3078 spin_unlock(&nchpp->spin); 3079 if (_cache_lock_nonblock(par_nch->ncp) == 0) { 3080 par_locked = 1; 3081 goto restart; 3082 } 3083 goto failed; 3084 } 3085 3086 /* 3087 * WARNING! We still hold the spinlock. We have to set the hash 3088 * table entry atomically. 3089 */ 3090 ncp = new_ncp; 3091 _cache_link_parent(ncp, par_nch->ncp, nchpp); 3092 spin_unlock(&nchpp->spin); 3093 _cache_unlock(par_nch->ncp); 3094 /* par_locked = 0 - not used */ 3095 found: 3096 /* 3097 * stats and namecache size management 3098 */ 3099 if (ncp->nc_flag & NCF_UNRESOLVED) 3100 ++gd->gd_nchstats->ncs_miss; 3101 else if (ncp->nc_vp) 3102 ++gd->gd_nchstats->ncs_goodhits; 3103 else 3104 ++gd->gd_nchstats->ncs_neghits; 3105 nch.mount = mp; 3106 nch.ncp = ncp; 3107 atomic_add_int(&nch.mount->mnt_refs, 1); 3108 return(nch); 3109 failed: 3110 if (new_ncp) { 3111 _cache_free(new_ncp); 3112 new_ncp = NULL; 3113 } 3114 nch.mount = NULL; 3115 nch.ncp = NULL; 3116 return(nch); 3117 } 3118 3119 /* 3120 * The namecache entry is marked as being used as a mount point. 3121 * Locate the mount if it is visible to the caller. The DragonFly 3122 * mount system allows arbitrary loops in the topology and disentangles 3123 * those loops by matching against (mp, ncp) rather than just (ncp). 3124 * This means any given ncp can dive any number of mounts, depending 3125 * on the relative mount (e.g. nullfs) the caller is at in the topology. 3126 * 3127 * We use a very simple frontend cache to reduce SMP conflicts, 3128 * which we have to do because the mountlist scan needs an exclusive 3129 * lock around its ripout info list. Not to mention that there might 3130 * be a lot of mounts. 3131 */ 3132 struct findmount_info { 3133 struct mount *result; 3134 struct mount *nch_mount; 3135 struct namecache *nch_ncp; 3136 }; 3137 3138 static 3139 struct ncmount_cache * 3140 ncmount_cache_lookup(struct mount *mp, struct namecache *ncp) 3141 { 3142 int hash; 3143 3144 hash = ((int)(intptr_t)mp / sizeof(*mp)) ^ 3145 ((int)(intptr_t)ncp / sizeof(*ncp)); 3146 hash = (hash & 0x7FFFFFFF) % NCMOUNT_NUMCACHE; 3147 return (&ncmount_cache[hash]); 3148 } 3149 3150 static 3151 int 3152 cache_findmount_callback(struct mount *mp, void *data) 3153 { 3154 struct findmount_info *info = data; 3155 3156 /* 3157 * Check the mount's mounted-on point against the passed nch. 3158 */ 3159 if (mp->mnt_ncmounton.mount == info->nch_mount && 3160 mp->mnt_ncmounton.ncp == info->nch_ncp 3161 ) { 3162 info->result = mp; 3163 atomic_add_int(&mp->mnt_refs, 1); 3164 return(-1); 3165 } 3166 return(0); 3167 } 3168 3169 struct mount * 3170 cache_findmount(struct nchandle *nch) 3171 { 3172 struct findmount_info info; 3173 struct ncmount_cache *ncc; 3174 struct mount *mp; 3175 3176 /* 3177 * Fast 3178 */ 3179 if (ncmount_cache_enable == 0) { 3180 ncc = NULL; 3181 goto skip; 3182 } 3183 ncc = ncmount_cache_lookup(nch->mount, nch->ncp); 3184 if (ncc->ncp == nch->ncp) { 3185 spin_lock_shared(&ncc->spin); 3186 if (ncc->isneg == 0 && 3187 ncc->ncp == nch->ncp && (mp = ncc->mp) != NULL) { 3188 if (mp->mnt_ncmounton.mount == nch->mount && 3189 mp->mnt_ncmounton.ncp == nch->ncp) { 3190 /* 3191 * Cache hit (positive) 3192 */ 3193 atomic_add_int(&mp->mnt_refs, 1); 3194 spin_unlock_shared(&ncc->spin); 3195 ++ncmount_cache_hit; 3196 return(mp); 3197 } 3198 /* else cache miss */ 3199 } 3200 if (ncc->isneg && 3201 ncc->ncp == nch->ncp && ncc->mp == nch->mount) { 3202 /* 3203 * Cache hit (negative) 3204 */ 3205 spin_unlock_shared(&ncc->spin); 3206 ++ncmount_cache_hit; 3207 return(NULL); 3208 } 3209 spin_unlock_shared(&ncc->spin); 3210 } 3211 skip: 3212 3213 /* 3214 * Slow 3215 */ 3216 info.result = NULL; 3217 info.nch_mount = nch->mount; 3218 info.nch_ncp = nch->ncp; 3219 mountlist_scan(cache_findmount_callback, &info, 3220 MNTSCAN_FORWARD|MNTSCAN_NOBUSY); 3221 3222 /* 3223 * Cache the result. 3224 * 3225 * Negative lookups: We cache the originating {ncp,mp}. (mp) is 3226 * only used for pointer comparisons and is not 3227 * referenced (otherwise there would be dangling 3228 * refs). 3229 * 3230 * Positive lookups: We cache the originating {ncp} and the target 3231 * (mp). (mp) is referenced. 3232 * 3233 * Indeterminant: If the match is undergoing an unmount we do 3234 * not cache it to avoid racing cache_unmounting(), 3235 * but still return the match. 3236 */ 3237 if (ncc) { 3238 spin_lock(&ncc->spin); 3239 if (info.result == NULL) { 3240 if (ncc->isneg == 0 && ncc->mp) 3241 atomic_add_int(&ncc->mp->mnt_refs, -1); 3242 ncc->ncp = nch->ncp; 3243 ncc->mp = nch->mount; 3244 ncc->isneg = 1; 3245 spin_unlock(&ncc->spin); 3246 ++ncmount_cache_overwrite; 3247 } else if ((info.result->mnt_kern_flag & MNTK_UNMOUNT) == 0) { 3248 if (ncc->isneg == 0 && ncc->mp) 3249 atomic_add_int(&ncc->mp->mnt_refs, -1); 3250 atomic_add_int(&info.result->mnt_refs, 1); 3251 ncc->ncp = nch->ncp; 3252 ncc->mp = info.result; 3253 ncc->isneg = 0; 3254 spin_unlock(&ncc->spin); 3255 ++ncmount_cache_overwrite; 3256 } else { 3257 spin_unlock(&ncc->spin); 3258 } 3259 ++ncmount_cache_miss; 3260 } 3261 return(info.result); 3262 } 3263 3264 void 3265 cache_dropmount(struct mount *mp) 3266 { 3267 atomic_add_int(&mp->mnt_refs, -1); 3268 } 3269 3270 void 3271 cache_ismounting(struct mount *mp) 3272 { 3273 struct nchandle *nch = &mp->mnt_ncmounton; 3274 struct ncmount_cache *ncc; 3275 3276 ncc = ncmount_cache_lookup(nch->mount, nch->ncp); 3277 if (ncc->isneg && 3278 ncc->ncp == nch->ncp && ncc->mp == nch->mount) { 3279 spin_lock(&ncc->spin); 3280 if (ncc->isneg && 3281 ncc->ncp == nch->ncp && ncc->mp == nch->mount) { 3282 ncc->ncp = NULL; 3283 ncc->mp = NULL; 3284 } 3285 spin_unlock(&ncc->spin); 3286 } 3287 } 3288 3289 void 3290 cache_unmounting(struct mount *mp) 3291 { 3292 struct nchandle *nch = &mp->mnt_ncmounton; 3293 struct ncmount_cache *ncc; 3294 3295 ncc = ncmount_cache_lookup(nch->mount, nch->ncp); 3296 if (ncc->isneg == 0 && 3297 ncc->ncp == nch->ncp && ncc->mp == mp) { 3298 spin_lock(&ncc->spin); 3299 if (ncc->isneg == 0 && 3300 ncc->ncp == nch->ncp && ncc->mp == mp) { 3301 atomic_add_int(&mp->mnt_refs, -1); 3302 ncc->ncp = NULL; 3303 ncc->mp = NULL; 3304 } 3305 spin_unlock(&ncc->spin); 3306 } 3307 } 3308 3309 /* 3310 * Resolve an unresolved namecache entry, generally by looking it up. 3311 * The passed ncp must be locked and refd. 3312 * 3313 * Theoretically since a vnode cannot be recycled while held, and since 3314 * the nc_parent chain holds its vnode as long as children exist, the 3315 * direct parent of the cache entry we are trying to resolve should 3316 * have a valid vnode. If not then generate an error that we can 3317 * determine is related to a resolver bug. 3318 * 3319 * However, if a vnode was in the middle of a recyclement when the NCP 3320 * got locked, ncp->nc_vp might point to a vnode that is about to become 3321 * invalid. cache_resolve() handles this case by unresolving the entry 3322 * and then re-resolving it. 3323 * 3324 * Note that successful resolution does not necessarily return an error 3325 * code of 0. If the ncp resolves to a negative cache hit then ENOENT 3326 * will be returned. 3327 */ 3328 int 3329 cache_resolve(struct nchandle *nch, struct ucred *cred) 3330 { 3331 struct namecache *par_tmp; 3332 struct namecache *par; 3333 struct namecache *ncp; 3334 struct nchandle nctmp; 3335 struct mount *mp; 3336 struct vnode *dvp; 3337 int error; 3338 3339 ncp = nch->ncp; 3340 mp = nch->mount; 3341 KKASSERT(_cache_lockstatus(ncp) == LK_EXCLUSIVE); 3342 restart: 3343 /* 3344 * If the ncp is already resolved we have nothing to do. However, 3345 * we do want to guarentee that a usable vnode is returned when 3346 * a vnode is present, so make sure it hasn't been reclaimed. 3347 */ 3348 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) { 3349 if (ncp->nc_vp && (ncp->nc_vp->v_flag & VRECLAIMED)) 3350 _cache_setunresolved(ncp); 3351 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) 3352 return (ncp->nc_error); 3353 } 3354 3355 /* 3356 * If the ncp was destroyed it will never resolve again. This 3357 * can basically only happen when someone is chdir'd into an 3358 * empty directory which is then rmdir'd. We want to catch this 3359 * here and not dive the VFS because the VFS might actually 3360 * have a way to re-resolve the disconnected ncp, which will 3361 * result in inconsistencies in the cdir/nch for proc->p_fd. 3362 */ 3363 if (ncp->nc_flag & NCF_DESTROYED) { 3364 kprintf("Warning: cache_resolve: ncp '%s' was unlinked\n", 3365 ncp->nc_name); 3366 return(EINVAL); 3367 } 3368 3369 /* 3370 * Mount points need special handling because the parent does not 3371 * belong to the same filesystem as the ncp. 3372 */ 3373 if (ncp == mp->mnt_ncmountpt.ncp) 3374 return (cache_resolve_mp(mp)); 3375 3376 /* 3377 * We expect an unbroken chain of ncps to at least the mount point, 3378 * and even all the way to root (but this code doesn't have to go 3379 * past the mount point). 3380 */ 3381 if (ncp->nc_parent == NULL) { 3382 kprintf("EXDEV case 1 %p %*.*s\n", ncp, 3383 ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name); 3384 ncp->nc_error = EXDEV; 3385 return(ncp->nc_error); 3386 } 3387 3388 /* 3389 * The vp's of the parent directories in the chain are held via vhold() 3390 * due to the existance of the child, and should not disappear. 3391 * However, there are cases where they can disappear: 3392 * 3393 * - due to filesystem I/O errors. 3394 * - due to NFS being stupid about tracking the namespace and 3395 * destroys the namespace for entire directories quite often. 3396 * - due to forced unmounts. 3397 * - due to an rmdir (parent will be marked DESTROYED) 3398 * 3399 * When this occurs we have to track the chain backwards and resolve 3400 * it, looping until the resolver catches up to the current node. We 3401 * could recurse here but we might run ourselves out of kernel stack 3402 * so we do it in a more painful manner. This situation really should 3403 * not occur all that often, or if it does not have to go back too 3404 * many nodes to resolve the ncp. 3405 */ 3406 while ((dvp = cache_dvpref(ncp)) == NULL) { 3407 /* 3408 * This case can occur if a process is CD'd into a 3409 * directory which is then rmdir'd. If the parent is marked 3410 * destroyed there is no point trying to resolve it. 3411 */ 3412 if (ncp->nc_parent->nc_flag & NCF_DESTROYED) 3413 return(ENOENT); 3414 par = ncp->nc_parent; 3415 _cache_hold(par); 3416 _cache_lock(par); 3417 while ((par_tmp = par->nc_parent) != NULL && 3418 par_tmp->nc_vp == NULL) { 3419 _cache_hold(par_tmp); 3420 _cache_lock(par_tmp); 3421 _cache_put(par); 3422 par = par_tmp; 3423 } 3424 if (par->nc_parent == NULL) { 3425 kprintf("EXDEV case 2 %*.*s\n", 3426 par->nc_nlen, par->nc_nlen, par->nc_name); 3427 _cache_put(par); 3428 return (EXDEV); 3429 } 3430 /* 3431 * The parent is not set in stone, ref and lock it to prevent 3432 * it from disappearing. Also note that due to renames it 3433 * is possible for our ncp to move and for par to no longer 3434 * be one of its parents. We resolve it anyway, the loop 3435 * will handle any moves. 3436 */ 3437 _cache_get(par); /* additional hold/lock */ 3438 _cache_put(par); /* from earlier hold/lock */ 3439 if (par == nch->mount->mnt_ncmountpt.ncp) { 3440 cache_resolve_mp(nch->mount); 3441 } else if ((dvp = cache_dvpref(par)) == NULL) { 3442 kprintf("[diagnostic] cache_resolve: raced on %*.*s\n", par->nc_nlen, par->nc_nlen, par->nc_name); 3443 _cache_put(par); 3444 continue; 3445 } else { 3446 if (par->nc_flag & NCF_UNRESOLVED) { 3447 nctmp.mount = mp; 3448 nctmp.ncp = par; 3449 par->nc_error = VOP_NRESOLVE(&nctmp, dvp, cred); 3450 } 3451 vrele(dvp); 3452 } 3453 if ((error = par->nc_error) != 0) { 3454 if (par->nc_error != EAGAIN) { 3455 kprintf("EXDEV case 3 %*.*s error %d\n", 3456 par->nc_nlen, par->nc_nlen, par->nc_name, 3457 par->nc_error); 3458 _cache_put(par); 3459 return(error); 3460 } 3461 kprintf("[diagnostic] cache_resolve: EAGAIN par %p %*.*s\n", 3462 par, par->nc_nlen, par->nc_nlen, par->nc_name); 3463 } 3464 _cache_put(par); 3465 /* loop */ 3466 } 3467 3468 /* 3469 * Call VOP_NRESOLVE() to get the vp, then scan for any disconnected 3470 * ncp's and reattach them. If this occurs the original ncp is marked 3471 * EAGAIN to force a relookup. 3472 * 3473 * NOTE: in order to call VOP_NRESOLVE(), the parent of the passed 3474 * ncp must already be resolved. 3475 */ 3476 if (dvp) { 3477 nctmp.mount = mp; 3478 nctmp.ncp = ncp; 3479 ncp->nc_error = VOP_NRESOLVE(&nctmp, dvp, cred); 3480 vrele(dvp); 3481 } else { 3482 ncp->nc_error = EPERM; 3483 } 3484 if (ncp->nc_error == EAGAIN) { 3485 kprintf("[diagnostic] cache_resolve: EAGAIN ncp %p %*.*s\n", 3486 ncp, ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name); 3487 goto restart; 3488 } 3489 return(ncp->nc_error); 3490 } 3491 3492 /* 3493 * Resolve the ncp associated with a mount point. Such ncp's almost always 3494 * remain resolved and this routine is rarely called. NFS MPs tends to force 3495 * re-resolution more often due to its mac-truck-smash-the-namecache 3496 * method of tracking namespace changes. 3497 * 3498 * The semantics for this call is that the passed ncp must be locked on 3499 * entry and will be locked on return. However, if we actually have to 3500 * resolve the mount point we temporarily unlock the entry in order to 3501 * avoid race-to-root deadlocks due to e.g. dead NFS mounts. Because of 3502 * the unlock we have to recheck the flags after we relock. 3503 */ 3504 static int 3505 cache_resolve_mp(struct mount *mp) 3506 { 3507 struct namecache *ncp = mp->mnt_ncmountpt.ncp; 3508 struct vnode *vp; 3509 int error; 3510 3511 KKASSERT(mp != NULL); 3512 3513 /* 3514 * If the ncp is already resolved we have nothing to do. However, 3515 * we do want to guarentee that a usable vnode is returned when 3516 * a vnode is present, so make sure it hasn't been reclaimed. 3517 */ 3518 if ((ncp->nc_flag & NCF_UNRESOLVED) == 0) { 3519 if (ncp->nc_vp && (ncp->nc_vp->v_flag & VRECLAIMED)) 3520 _cache_setunresolved(ncp); 3521 } 3522 3523 if (ncp->nc_flag & NCF_UNRESOLVED) { 3524 _cache_unlock(ncp); 3525 while (vfs_busy(mp, 0)) 3526 ; 3527 error = VFS_ROOT(mp, &vp); 3528 _cache_lock(ncp); 3529 3530 /* 3531 * recheck the ncp state after relocking. 3532 */ 3533 if (ncp->nc_flag & NCF_UNRESOLVED) { 3534 ncp->nc_error = error; 3535 if (error == 0) { 3536 _cache_setvp(mp, ncp, vp); 3537 vput(vp); 3538 } else { 3539 kprintf("[diagnostic] cache_resolve_mp: failed" 3540 " to resolve mount %p err=%d ncp=%p\n", 3541 mp, error, ncp); 3542 _cache_setvp(mp, ncp, NULL); 3543 } 3544 } else if (error == 0) { 3545 vput(vp); 3546 } 3547 vfs_unbusy(mp); 3548 } 3549 return(ncp->nc_error); 3550 } 3551 3552 /* 3553 * Clean out negative cache entries when too many have accumulated. 3554 */ 3555 static void 3556 _cache_cleanneg(int count) 3557 { 3558 struct namecache *ncp; 3559 3560 /* 3561 * Attempt to clean out the specified number of negative cache 3562 * entries. 3563 */ 3564 while (count) { 3565 spin_lock(&ncspin); 3566 ncp = TAILQ_FIRST(&ncneglist); 3567 if (ncp == NULL) { 3568 spin_unlock(&ncspin); 3569 break; 3570 } 3571 TAILQ_REMOVE(&ncneglist, ncp, nc_vnode); 3572 TAILQ_INSERT_TAIL(&ncneglist, ncp, nc_vnode); 3573 _cache_hold(ncp); 3574 spin_unlock(&ncspin); 3575 3576 /* 3577 * This can race, so we must re-check that the ncp 3578 * is on the ncneglist after successfully locking it. 3579 */ 3580 if (_cache_lock_special(ncp) == 0) { 3581 if (ncp->nc_vp == NULL && 3582 (ncp->nc_flag & NCF_UNRESOLVED) == 0) { 3583 ncp = cache_zap(ncp, 1); 3584 if (ncp) 3585 _cache_drop(ncp); 3586 } else { 3587 kprintf("cache_cleanneg: race avoided\n"); 3588 _cache_unlock(ncp); 3589 } 3590 } else { 3591 _cache_drop(ncp); 3592 } 3593 --count; 3594 } 3595 } 3596 3597 /* 3598 * Clean out positive cache entries when too many have accumulated. 3599 */ 3600 static void 3601 _cache_cleanpos(int count) 3602 { 3603 static volatile int rover; 3604 struct nchash_head *nchpp; 3605 struct namecache *ncp; 3606 int rover_copy; 3607 3608 /* 3609 * Attempt to clean out the specified number of negative cache 3610 * entries. 3611 */ 3612 while (count) { 3613 rover_copy = ++rover; /* MPSAFEENOUGH */ 3614 cpu_ccfence(); 3615 nchpp = NCHHASH(rover_copy); 3616 3617 spin_lock_shared(&nchpp->spin); 3618 ncp = LIST_FIRST(&nchpp->list); 3619 while (ncp && (ncp->nc_flag & NCF_DESTROYED)) 3620 ncp = LIST_NEXT(ncp, nc_hash); 3621 if (ncp) 3622 _cache_hold(ncp); 3623 spin_unlock_shared(&nchpp->spin); 3624 3625 if (ncp) { 3626 if (_cache_lock_special(ncp) == 0) { 3627 ncp = cache_zap(ncp, 1); 3628 if (ncp) 3629 _cache_drop(ncp); 3630 } else { 3631 _cache_drop(ncp); 3632 } 3633 } 3634 --count; 3635 } 3636 } 3637 3638 /* 3639 * This is a kitchen sink function to clean out ncps which we 3640 * tried to zap from cache_drop() but failed because we were 3641 * unable to acquire the parent lock. 3642 * 3643 * Such entries can also be removed via cache_inval_vp(), such 3644 * as when unmounting. 3645 */ 3646 static void 3647 _cache_cleandefered(void) 3648 { 3649 struct nchash_head *nchpp; 3650 struct namecache *ncp; 3651 struct namecache dummy; 3652 int i; 3653 3654 numdefered = 0; 3655 bzero(&dummy, sizeof(dummy)); 3656 dummy.nc_flag = NCF_DESTROYED; 3657 dummy.nc_refs = 1; 3658 3659 for (i = 0; i <= nchash; ++i) { 3660 nchpp = &nchashtbl[i]; 3661 3662 spin_lock(&nchpp->spin); 3663 LIST_INSERT_HEAD(&nchpp->list, &dummy, nc_hash); 3664 ncp = &dummy; 3665 while ((ncp = LIST_NEXT(ncp, nc_hash)) != NULL) { 3666 if ((ncp->nc_flag & NCF_DEFEREDZAP) == 0) 3667 continue; 3668 LIST_REMOVE(&dummy, nc_hash); 3669 LIST_INSERT_AFTER(ncp, &dummy, nc_hash); 3670 _cache_hold(ncp); 3671 spin_unlock(&nchpp->spin); 3672 if (_cache_lock_nonblock(ncp) == 0) { 3673 ncp->nc_flag &= ~NCF_DEFEREDZAP; 3674 _cache_unlock(ncp); 3675 } 3676 _cache_drop(ncp); 3677 spin_lock(&nchpp->spin); 3678 ncp = &dummy; 3679 } 3680 LIST_REMOVE(&dummy, nc_hash); 3681 spin_unlock(&nchpp->spin); 3682 } 3683 } 3684 3685 /* 3686 * Name cache initialization, from vfsinit() when we are booting 3687 */ 3688 void 3689 nchinit(void) 3690 { 3691 int i; 3692 globaldata_t gd; 3693 3694 /* initialise per-cpu namecache effectiveness statistics. */ 3695 for (i = 0; i < ncpus; ++i) { 3696 gd = globaldata_find(i); 3697 gd->gd_nchstats = &nchstats[i]; 3698 } 3699 TAILQ_INIT(&ncneglist); 3700 spin_init(&ncspin, "nchinit"); 3701 nchashtbl = hashinit_ext(desiredvnodes / 2, 3702 sizeof(struct nchash_head), 3703 M_VFSCACHE, &nchash); 3704 for (i = 0; i <= (int)nchash; ++i) { 3705 LIST_INIT(&nchashtbl[i].list); 3706 spin_init(&nchashtbl[i].spin, "nchinit_hash"); 3707 } 3708 for (i = 0; i < NCMOUNT_NUMCACHE; ++i) 3709 spin_init(&ncmount_cache[i].spin, "nchinit_cache"); 3710 nclockwarn = 5 * hz; 3711 } 3712 3713 /* 3714 * Called from start_init() to bootstrap the root filesystem. Returns 3715 * a referenced, unlocked namecache record. 3716 */ 3717 void 3718 cache_allocroot(struct nchandle *nch, struct mount *mp, struct vnode *vp) 3719 { 3720 nch->ncp = cache_alloc(0); 3721 nch->mount = mp; 3722 atomic_add_int(&mp->mnt_refs, 1); 3723 if (vp) 3724 _cache_setvp(nch->mount, nch->ncp, vp); 3725 } 3726 3727 /* 3728 * vfs_cache_setroot() 3729 * 3730 * Create an association between the root of our namecache and 3731 * the root vnode. This routine may be called several times during 3732 * booting. 3733 * 3734 * If the caller intends to save the returned namecache pointer somewhere 3735 * it must cache_hold() it. 3736 */ 3737 void 3738 vfs_cache_setroot(struct vnode *nvp, struct nchandle *nch) 3739 { 3740 struct vnode *ovp; 3741 struct nchandle onch; 3742 3743 ovp = rootvnode; 3744 onch = rootnch; 3745 rootvnode = nvp; 3746 if (nch) 3747 rootnch = *nch; 3748 else 3749 cache_zero(&rootnch); 3750 if (ovp) 3751 vrele(ovp); 3752 if (onch.ncp) 3753 cache_drop(&onch); 3754 } 3755 3756 /* 3757 * XXX OLD API COMPAT FUNCTION. This really messes up the new namecache 3758 * topology and is being removed as quickly as possible. The new VOP_N*() 3759 * API calls are required to make specific adjustments using the supplied 3760 * ncp pointers rather then just bogusly purging random vnodes. 3761 * 3762 * Invalidate all namecache entries to a particular vnode as well as 3763 * any direct children of that vnode in the namecache. This is a 3764 * 'catch all' purge used by filesystems that do not know any better. 3765 * 3766 * Note that the linkage between the vnode and its namecache entries will 3767 * be removed, but the namecache entries themselves might stay put due to 3768 * active references from elsewhere in the system or due to the existance of 3769 * the children. The namecache topology is left intact even if we do not 3770 * know what the vnode association is. Such entries will be marked 3771 * NCF_UNRESOLVED. 3772 */ 3773 void 3774 cache_purge(struct vnode *vp) 3775 { 3776 cache_inval_vp(vp, CINV_DESTROY | CINV_CHILDREN); 3777 } 3778 3779 static int disablecwd; 3780 SYSCTL_INT(_debug, OID_AUTO, disablecwd, CTLFLAG_RW, &disablecwd, 0, 3781 "Disable getcwd"); 3782 3783 static u_long numcwdcalls; 3784 SYSCTL_ULONG(_vfs_cache, OID_AUTO, numcwdcalls, CTLFLAG_RD, &numcwdcalls, 0, 3785 "Number of current directory resolution calls"); 3786 static u_long numcwdfailnf; 3787 SYSCTL_ULONG(_vfs_cache, OID_AUTO, numcwdfailnf, CTLFLAG_RD, &numcwdfailnf, 0, 3788 "Number of current directory failures due to lack of file"); 3789 static u_long numcwdfailsz; 3790 SYSCTL_ULONG(_vfs_cache, OID_AUTO, numcwdfailsz, CTLFLAG_RD, &numcwdfailsz, 0, 3791 "Number of current directory failures due to large result"); 3792 static u_long numcwdfound; 3793 SYSCTL_ULONG(_vfs_cache, OID_AUTO, numcwdfound, CTLFLAG_RD, &numcwdfound, 0, 3794 "Number of current directory resolution successes"); 3795 3796 /* 3797 * MPALMOSTSAFE 3798 */ 3799 int 3800 sys___getcwd(struct __getcwd_args *uap) 3801 { 3802 u_int buflen; 3803 int error; 3804 char *buf; 3805 char *bp; 3806 3807 if (disablecwd) 3808 return (ENODEV); 3809 3810 buflen = uap->buflen; 3811 if (buflen == 0) 3812 return (EINVAL); 3813 if (buflen > MAXPATHLEN) 3814 buflen = MAXPATHLEN; 3815 3816 buf = kmalloc(buflen, M_TEMP, M_WAITOK); 3817 bp = kern_getcwd(buf, buflen, &error); 3818 if (error == 0) 3819 error = copyout(bp, uap->buf, strlen(bp) + 1); 3820 kfree(buf, M_TEMP); 3821 return (error); 3822 } 3823 3824 char * 3825 kern_getcwd(char *buf, size_t buflen, int *error) 3826 { 3827 struct proc *p = curproc; 3828 char *bp; 3829 int i, slash_prefixed; 3830 struct filedesc *fdp; 3831 struct nchandle nch; 3832 struct namecache *ncp; 3833 3834 numcwdcalls++; 3835 bp = buf; 3836 bp += buflen - 1; 3837 *bp = '\0'; 3838 fdp = p->p_fd; 3839 slash_prefixed = 0; 3840 3841 nch = fdp->fd_ncdir; 3842 ncp = nch.ncp; 3843 if (ncp) 3844 _cache_hold(ncp); 3845 3846 while (ncp && (ncp != fdp->fd_nrdir.ncp || 3847 nch.mount != fdp->fd_nrdir.mount) 3848 ) { 3849 /* 3850 * While traversing upwards if we encounter the root 3851 * of the current mount we have to skip to the mount point 3852 * in the underlying filesystem. 3853 */ 3854 if (ncp == nch.mount->mnt_ncmountpt.ncp) { 3855 nch = nch.mount->mnt_ncmounton; 3856 _cache_drop(ncp); 3857 ncp = nch.ncp; 3858 if (ncp) 3859 _cache_hold(ncp); 3860 continue; 3861 } 3862 3863 /* 3864 * Prepend the path segment 3865 */ 3866 for (i = ncp->nc_nlen - 1; i >= 0; i--) { 3867 if (bp == buf) { 3868 numcwdfailsz++; 3869 *error = ERANGE; 3870 bp = NULL; 3871 goto done; 3872 } 3873 *--bp = ncp->nc_name[i]; 3874 } 3875 if (bp == buf) { 3876 numcwdfailsz++; 3877 *error = ERANGE; 3878 bp = NULL; 3879 goto done; 3880 } 3881 *--bp = '/'; 3882 slash_prefixed = 1; 3883 3884 /* 3885 * Go up a directory. This isn't a mount point so we don't 3886 * have to check again. 3887 */ 3888 while ((nch.ncp = ncp->nc_parent) != NULL) { 3889 if (ncp_shared_lock_disable) 3890 _cache_lock(ncp); 3891 else 3892 _cache_lock_shared(ncp); 3893 if (nch.ncp != ncp->nc_parent) { 3894 _cache_unlock(ncp); 3895 continue; 3896 } 3897 _cache_hold(nch.ncp); 3898 _cache_unlock(ncp); 3899 break; 3900 } 3901 _cache_drop(ncp); 3902 ncp = nch.ncp; 3903 } 3904 if (ncp == NULL) { 3905 numcwdfailnf++; 3906 *error = ENOENT; 3907 bp = NULL; 3908 goto done; 3909 } 3910 if (!slash_prefixed) { 3911 if (bp == buf) { 3912 numcwdfailsz++; 3913 *error = ERANGE; 3914 bp = NULL; 3915 goto done; 3916 } 3917 *--bp = '/'; 3918 } 3919 numcwdfound++; 3920 *error = 0; 3921 done: 3922 if (ncp) 3923 _cache_drop(ncp); 3924 return (bp); 3925 } 3926 3927 /* 3928 * Thus begins the fullpath magic. 3929 * 3930 * The passed nchp is referenced but not locked. 3931 */ 3932 static int disablefullpath; 3933 SYSCTL_INT(_debug, OID_AUTO, disablefullpath, CTLFLAG_RW, 3934 &disablefullpath, 0, 3935 "Disable fullpath lookups"); 3936 3937 static u_int numfullpathcalls; 3938 SYSCTL_UINT(_vfs_cache, OID_AUTO, numfullpathcalls, CTLFLAG_RD, 3939 &numfullpathcalls, 0, 3940 "Number of full path resolutions in progress"); 3941 static u_int numfullpathfailnf; 3942 SYSCTL_UINT(_vfs_cache, OID_AUTO, numfullpathfailnf, CTLFLAG_RD, 3943 &numfullpathfailnf, 0, 3944 "Number of full path resolution failures due to lack of file"); 3945 static u_int numfullpathfailsz; 3946 SYSCTL_UINT(_vfs_cache, OID_AUTO, numfullpathfailsz, CTLFLAG_RD, 3947 &numfullpathfailsz, 0, 3948 "Number of full path resolution failures due to insufficient memory"); 3949 static u_int numfullpathfound; 3950 SYSCTL_UINT(_vfs_cache, OID_AUTO, numfullpathfound, CTLFLAG_RD, 3951 &numfullpathfound, 0, 3952 "Number of full path resolution successes"); 3953 3954 int 3955 cache_fullpath(struct proc *p, struct nchandle *nchp, struct nchandle *nchbase, 3956 char **retbuf, char **freebuf, int guess) 3957 { 3958 struct nchandle fd_nrdir; 3959 struct nchandle nch; 3960 struct namecache *ncp; 3961 struct mount *mp, *new_mp; 3962 char *bp, *buf; 3963 int slash_prefixed; 3964 int error = 0; 3965 int i; 3966 3967 atomic_add_int(&numfullpathcalls, -1); 3968 3969 *retbuf = NULL; 3970 *freebuf = NULL; 3971 3972 buf = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK); 3973 bp = buf + MAXPATHLEN - 1; 3974 *bp = '\0'; 3975 if (nchbase) 3976 fd_nrdir = *nchbase; 3977 else if (p != NULL) 3978 fd_nrdir = p->p_fd->fd_nrdir; 3979 else 3980 fd_nrdir = rootnch; 3981 slash_prefixed = 0; 3982 nch = *nchp; 3983 ncp = nch.ncp; 3984 if (ncp) 3985 _cache_hold(ncp); 3986 mp = nch.mount; 3987 3988 while (ncp && (ncp != fd_nrdir.ncp || mp != fd_nrdir.mount)) { 3989 new_mp = NULL; 3990 3991 /* 3992 * If we are asked to guess the upwards path, we do so whenever 3993 * we encounter an ncp marked as a mountpoint. We try to find 3994 * the actual mountpoint by finding the mountpoint with this 3995 * ncp. 3996 */ 3997 if (guess && (ncp->nc_flag & NCF_ISMOUNTPT)) { 3998 new_mp = mount_get_by_nc(ncp); 3999 } 4000 /* 4001 * While traversing upwards if we encounter the root 4002 * of the current mount we have to skip to the mount point. 4003 */ 4004 if (ncp == mp->mnt_ncmountpt.ncp) { 4005 new_mp = mp; 4006 } 4007 if (new_mp) { 4008 nch = new_mp->mnt_ncmounton; 4009 _cache_drop(ncp); 4010 ncp = nch.ncp; 4011 if (ncp) 4012 _cache_hold(ncp); 4013 mp = nch.mount; 4014 continue; 4015 } 4016 4017 /* 4018 * Prepend the path segment 4019 */ 4020 for (i = ncp->nc_nlen - 1; i >= 0; i--) { 4021 if (bp == buf) { 4022 numfullpathfailsz++; 4023 kfree(buf, M_TEMP); 4024 error = ENOMEM; 4025 goto done; 4026 } 4027 *--bp = ncp->nc_name[i]; 4028 } 4029 if (bp == buf) { 4030 numfullpathfailsz++; 4031 kfree(buf, M_TEMP); 4032 error = ENOMEM; 4033 goto done; 4034 } 4035 *--bp = '/'; 4036 slash_prefixed = 1; 4037 4038 /* 4039 * Go up a directory. This isn't a mount point so we don't 4040 * have to check again. 4041 * 4042 * We can only safely access nc_parent with ncp held locked. 4043 */ 4044 while ((nch.ncp = ncp->nc_parent) != NULL) { 4045 _cache_lock(ncp); 4046 if (nch.ncp != ncp->nc_parent) { 4047 _cache_unlock(ncp); 4048 continue; 4049 } 4050 _cache_hold(nch.ncp); 4051 _cache_unlock(ncp); 4052 break; 4053 } 4054 _cache_drop(ncp); 4055 ncp = nch.ncp; 4056 } 4057 if (ncp == NULL) { 4058 numfullpathfailnf++; 4059 kfree(buf, M_TEMP); 4060 error = ENOENT; 4061 goto done; 4062 } 4063 4064 if (!slash_prefixed) { 4065 if (bp == buf) { 4066 numfullpathfailsz++; 4067 kfree(buf, M_TEMP); 4068 error = ENOMEM; 4069 goto done; 4070 } 4071 *--bp = '/'; 4072 } 4073 numfullpathfound++; 4074 *retbuf = bp; 4075 *freebuf = buf; 4076 error = 0; 4077 done: 4078 if (ncp) 4079 _cache_drop(ncp); 4080 return(error); 4081 } 4082 4083 int 4084 vn_fullpath(struct proc *p, struct vnode *vn, char **retbuf, 4085 char **freebuf, int guess) 4086 { 4087 struct namecache *ncp; 4088 struct nchandle nch; 4089 int error; 4090 4091 *freebuf = NULL; 4092 atomic_add_int(&numfullpathcalls, 1); 4093 if (disablefullpath) 4094 return (ENODEV); 4095 4096 if (p == NULL) 4097 return (EINVAL); 4098 4099 /* vn is NULL, client wants us to use p->p_textvp */ 4100 if (vn == NULL) { 4101 if ((vn = p->p_textvp) == NULL) 4102 return (EINVAL); 4103 } 4104 spin_lock_shared(&vn->v_spin); 4105 TAILQ_FOREACH(ncp, &vn->v_namecache, nc_vnode) { 4106 if (ncp->nc_nlen) 4107 break; 4108 } 4109 if (ncp == NULL) { 4110 spin_unlock_shared(&vn->v_spin); 4111 return (EINVAL); 4112 } 4113 _cache_hold(ncp); 4114 spin_unlock_shared(&vn->v_spin); 4115 4116 atomic_add_int(&numfullpathcalls, -1); 4117 nch.ncp = ncp; 4118 nch.mount = vn->v_mount; 4119 error = cache_fullpath(p, &nch, NULL, retbuf, freebuf, guess); 4120 _cache_drop(ncp); 4121 return (error); 4122 } 4123