1 /* $NetBSD: vfs_vnode.c,v 1.116 2020/03/22 18:45:28 ad Exp $ */ 2 3 /*- 4 * Copyright (c) 1997-2011, 2019, 2020 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 * NASA Ames Research Center, by Charles M. Hannum, and by Andrew Doran. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (c) 1989, 1993 35 * The Regents of the University of California. All rights reserved. 36 * (c) UNIX System Laboratories, Inc. 37 * All or some portions of this file are derived from material licensed 38 * to the University of California by American Telephone and Telegraph 39 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 40 * the permission of UNIX System Laboratories, Inc. 41 * 42 * Redistribution and use in source and binary forms, with or without 43 * modification, are permitted provided that the following conditions 44 * are met: 45 * 1. Redistributions of source code must retain the above copyright 46 * notice, this list of conditions and the following disclaimer. 47 * 2. Redistributions in binary form must reproduce the above copyright 48 * notice, this list of conditions and the following disclaimer in the 49 * documentation and/or other materials provided with the distribution. 50 * 3. Neither the name of the University nor the names of its contributors 51 * may be used to endorse or promote products derived from this software 52 * without specific prior written permission. 53 * 54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 64 * SUCH DAMAGE. 65 * 66 * @(#)vfs_subr.c 8.13 (Berkeley) 4/18/94 67 */ 68 69 /* 70 * The vnode cache subsystem. 71 * 72 * Life-cycle 73 * 74 * Normally, there are two points where new vnodes are created: 75 * VOP_CREATE(9) and VOP_LOOKUP(9). The life-cycle of a vnode 76 * starts in one of the following ways: 77 * 78 * - Allocation, via vcache_get(9) or vcache_new(9). 79 * - Reclamation of inactive vnode, via vcache_vget(9). 80 * 81 * Recycle from a free list, via getnewvnode(9) -> getcleanvnode(9) 82 * was another, traditional way. Currently, only the draining thread 83 * recycles the vnodes. This behaviour might be revisited. 84 * 85 * The life-cycle ends when the last reference is dropped, usually 86 * in VOP_REMOVE(9). In such case, VOP_INACTIVE(9) is called to inform 87 * the file system that vnode is inactive. Via this call, file system 88 * indicates whether vnode can be recycled (usually, it checks its own 89 * references, e.g. count of links, whether the file was removed). 90 * 91 * Depending on indication, vnode can be put into a free list (cache), 92 * or cleaned via vcache_reclaim, which calls VOP_RECLAIM(9) to 93 * disassociate underlying file system from the vnode, and finally 94 * destroyed. 95 * 96 * Vnode state 97 * 98 * Vnode is always in one of six states: 99 * - MARKER This is a marker vnode to help list traversal. It 100 * will never change its state. 101 * - LOADING Vnode is associating underlying file system and not 102 * yet ready to use. 103 * - LOADED Vnode has associated underlying file system and is 104 * ready to use. 105 * - BLOCKED Vnode is active but cannot get new references. 106 * - RECLAIMING Vnode is disassociating from the underlying file 107 * system. 108 * - RECLAIMED Vnode has disassociated from underlying file system 109 * and is dead. 110 * 111 * Valid state changes are: 112 * LOADING -> LOADED 113 * Vnode has been initialised in vcache_get() or 114 * vcache_new() and is ready to use. 115 * LOADED -> RECLAIMING 116 * Vnode starts disassociation from underlying file 117 * system in vcache_reclaim(). 118 * RECLAIMING -> RECLAIMED 119 * Vnode finished disassociation from underlying file 120 * system in vcache_reclaim(). 121 * LOADED -> BLOCKED 122 * Either vcache_rekey*() is changing the vnode key or 123 * vrelel() is about to call VOP_INACTIVE(). 124 * BLOCKED -> LOADED 125 * The block condition is over. 126 * LOADING -> RECLAIMED 127 * Either vcache_get() or vcache_new() failed to 128 * associate the underlying file system or vcache_rekey*() 129 * drops a vnode used as placeholder. 130 * 131 * Of these states LOADING, BLOCKED and RECLAIMING are intermediate 132 * and it is possible to wait for state change. 133 * 134 * State is protected with v_interlock with one exception: 135 * to change from LOADING both v_interlock and vcache_lock must be held 136 * so it is possible to check "state == LOADING" without holding 137 * v_interlock. See vcache_get() for details. 138 * 139 * Reference counting 140 * 141 * Vnode is considered active, if reference count (vnode_t::v_usecount) 142 * is non-zero. It is maintained using: vref(9) and vrele(9), as well 143 * as vput(9), routines. Common points holding references are e.g. 144 * file openings, current working directory, mount points, etc. 145 * 146 * Note on v_usecount and its locking 147 * 148 * At nearly all points it is known that v_usecount could be zero, 149 * the vnode_t::v_interlock will be held. To change the count away 150 * from zero, the interlock must be held. To change from a non-zero 151 * value to zero, again the interlock must be held. 152 * 153 * Changing the usecount from a non-zero value to a non-zero value can 154 * safely be done using atomic operations, without the interlock held. 155 */ 156 157 #include <sys/cdefs.h> 158 __KERNEL_RCSID(0, "$NetBSD: vfs_vnode.c,v 1.116 2020/03/22 18:45:28 ad Exp $"); 159 160 #ifdef _KERNEL_OPT 161 #include "opt_pax.h" 162 #endif 163 164 #include <sys/param.h> 165 #include <sys/kernel.h> 166 167 #include <sys/atomic.h> 168 #include <sys/buf.h> 169 #include <sys/conf.h> 170 #include <sys/device.h> 171 #include <sys/hash.h> 172 #include <sys/kauth.h> 173 #include <sys/kmem.h> 174 #include <sys/kthread.h> 175 #include <sys/module.h> 176 #include <sys/mount.h> 177 #include <sys/namei.h> 178 #include <sys/pax.h> 179 #include <sys/syscallargs.h> 180 #include <sys/sysctl.h> 181 #include <sys/systm.h> 182 #include <sys/vnode_impl.h> 183 #include <sys/wapbl.h> 184 #include <sys/fstrans.h> 185 186 #include <uvm/uvm.h> 187 #include <uvm/uvm_readahead.h> 188 #include <uvm/uvm_stat.h> 189 190 /* Flags to vrelel. */ 191 #define VRELEL_ASYNC 0x0001 /* Always defer to vrele thread. */ 192 193 #define LRU_VRELE 0 194 #define LRU_FREE 1 195 #define LRU_HOLD 2 196 #define LRU_COUNT 3 197 198 /* 199 * There are three lru lists: one holds vnodes waiting for async release, 200 * one is for vnodes which have no buffer/page references and one for those 201 * which do (i.e. v_holdcnt is non-zero). We put the lists into a single, 202 * private cache line as vnodes migrate between them while under the same 203 * lock (vdrain_lock). 204 */ 205 u_int numvnodes __cacheline_aligned; 206 static vnodelst_t lru_list[LRU_COUNT] __cacheline_aligned; 207 static kmutex_t vdrain_lock __cacheline_aligned; 208 static kcondvar_t vdrain_cv; 209 static int vdrain_gen; 210 static kcondvar_t vdrain_gen_cv; 211 static bool vdrain_retry; 212 static lwp_t * vdrain_lwp; 213 SLIST_HEAD(hashhead, vnode_impl); 214 static kmutex_t vcache_lock __cacheline_aligned; 215 static kcondvar_t vcache_cv; 216 static u_int vcache_hashsize; 217 static u_long vcache_hashmask; 218 static struct hashhead *vcache_hashtab; 219 static pool_cache_t vcache_pool; 220 static void lru_requeue(vnode_t *, vnodelst_t *); 221 static vnodelst_t * lru_which(vnode_t *); 222 static vnode_impl_t * vcache_alloc(void); 223 static void vcache_dealloc(vnode_impl_t *); 224 static void vcache_free(vnode_impl_t *); 225 static void vcache_init(void); 226 static void vcache_reinit(void); 227 static void vcache_reclaim(vnode_t *); 228 static void vrelel(vnode_t *, int, int); 229 static void vdrain_thread(void *); 230 static void vnpanic(vnode_t *, const char *, ...) 231 __printflike(2, 3); 232 233 /* Routines having to do with the management of the vnode table. */ 234 extern struct mount *dead_rootmount; 235 extern int (**dead_vnodeop_p)(void *); 236 extern int (**spec_vnodeop_p)(void *); 237 extern struct vfsops dead_vfsops; 238 239 /* Vnode state operations and diagnostics. */ 240 241 #if defined(DIAGNOSTIC) 242 243 #define VSTATE_VALID(state) \ 244 ((state) != VS_ACTIVE && (state) != VS_MARKER) 245 #define VSTATE_GET(vp) \ 246 vstate_assert_get((vp), __func__, __LINE__) 247 #define VSTATE_CHANGE(vp, from, to) \ 248 vstate_assert_change((vp), (from), (to), __func__, __LINE__) 249 #define VSTATE_WAIT_STABLE(vp) \ 250 vstate_assert_wait_stable((vp), __func__, __LINE__) 251 252 void 253 _vstate_assert(vnode_t *vp, enum vnode_state state, const char *func, int line, 254 bool has_lock) 255 { 256 vnode_impl_t *vip = VNODE_TO_VIMPL(vp); 257 258 if (!has_lock) { 259 /* 260 * Prevent predictive loads from the CPU, but check the state 261 * without loooking first. 262 */ 263 membar_enter(); 264 if (state == VS_ACTIVE && vp->v_usecount > 0 && 265 (vip->vi_state == VS_LOADED || vip->vi_state == VS_BLOCKED)) 266 return; 267 if (vip->vi_state == state) 268 return; 269 mutex_enter((vp)->v_interlock); 270 } 271 272 KASSERTMSG(mutex_owned(vp->v_interlock), "at %s:%d", func, line); 273 274 if ((state == VS_ACTIVE && vp->v_usecount > 0 && 275 (vip->vi_state == VS_LOADED || vip->vi_state == VS_BLOCKED)) || 276 vip->vi_state == state) { 277 if (!has_lock) 278 mutex_exit((vp)->v_interlock); 279 return; 280 } 281 vnpanic(vp, "state is %s, usecount %d, expected %s at %s:%d", 282 vstate_name(vip->vi_state), vp->v_usecount, 283 vstate_name(state), func, line); 284 } 285 286 static enum vnode_state 287 vstate_assert_get(vnode_t *vp, const char *func, int line) 288 { 289 vnode_impl_t *vip = VNODE_TO_VIMPL(vp); 290 291 KASSERTMSG(mutex_owned(vp->v_interlock), "at %s:%d", func, line); 292 if (! VSTATE_VALID(vip->vi_state)) 293 vnpanic(vp, "state is %s at %s:%d", 294 vstate_name(vip->vi_state), func, line); 295 296 return vip->vi_state; 297 } 298 299 static void 300 vstate_assert_wait_stable(vnode_t *vp, const char *func, int line) 301 { 302 vnode_impl_t *vip = VNODE_TO_VIMPL(vp); 303 304 KASSERTMSG(mutex_owned(vp->v_interlock), "at %s:%d", func, line); 305 if (! VSTATE_VALID(vip->vi_state)) 306 vnpanic(vp, "state is %s at %s:%d", 307 vstate_name(vip->vi_state), func, line); 308 309 while (vip->vi_state != VS_LOADED && vip->vi_state != VS_RECLAIMED) 310 cv_wait(&vp->v_cv, vp->v_interlock); 311 312 if (! VSTATE_VALID(vip->vi_state)) 313 vnpanic(vp, "state is %s at %s:%d", 314 vstate_name(vip->vi_state), func, line); 315 } 316 317 static void 318 vstate_assert_change(vnode_t *vp, enum vnode_state from, enum vnode_state to, 319 const char *func, int line) 320 { 321 vnode_impl_t *vip = VNODE_TO_VIMPL(vp); 322 323 KASSERTMSG(mutex_owned(vp->v_interlock), "at %s:%d", func, line); 324 if (from == VS_LOADING) 325 KASSERTMSG(mutex_owned(&vcache_lock), "at %s:%d", func, line); 326 327 if (! VSTATE_VALID(from)) 328 vnpanic(vp, "from is %s at %s:%d", 329 vstate_name(from), func, line); 330 if (! VSTATE_VALID(to)) 331 vnpanic(vp, "to is %s at %s:%d", 332 vstate_name(to), func, line); 333 if (vip->vi_state != from) 334 vnpanic(vp, "from is %s, expected %s at %s:%d\n", 335 vstate_name(vip->vi_state), vstate_name(from), func, line); 336 if ((from == VS_BLOCKED || to == VS_BLOCKED) && vp->v_usecount != 1) 337 vnpanic(vp, "%s to %s with usecount %d at %s:%d", 338 vstate_name(from), vstate_name(to), vp->v_usecount, 339 func, line); 340 341 vip->vi_state = to; 342 if (from == VS_LOADING) 343 cv_broadcast(&vcache_cv); 344 if (to == VS_LOADED || to == VS_RECLAIMED) 345 cv_broadcast(&vp->v_cv); 346 } 347 348 #else /* defined(DIAGNOSTIC) */ 349 350 #define VSTATE_GET(vp) \ 351 (VNODE_TO_VIMPL((vp))->vi_state) 352 #define VSTATE_CHANGE(vp, from, to) \ 353 vstate_change((vp), (from), (to)) 354 #define VSTATE_WAIT_STABLE(vp) \ 355 vstate_wait_stable((vp)) 356 void 357 _vstate_assert(vnode_t *vp, enum vnode_state state, const char *func, int line, 358 bool has_lock) 359 { 360 361 } 362 363 static void 364 vstate_wait_stable(vnode_t *vp) 365 { 366 vnode_impl_t *vip = VNODE_TO_VIMPL(vp); 367 368 while (vip->vi_state != VS_LOADED && vip->vi_state != VS_RECLAIMED) 369 cv_wait(&vp->v_cv, vp->v_interlock); 370 } 371 372 static void 373 vstate_change(vnode_t *vp, enum vnode_state from, enum vnode_state to) 374 { 375 vnode_impl_t *vip = VNODE_TO_VIMPL(vp); 376 377 vip->vi_state = to; 378 if (from == VS_LOADING) 379 cv_broadcast(&vcache_cv); 380 if (to == VS_LOADED || to == VS_RECLAIMED) 381 cv_broadcast(&vp->v_cv); 382 } 383 384 #endif /* defined(DIAGNOSTIC) */ 385 386 void 387 vfs_vnode_sysinit(void) 388 { 389 int error __diagused, i; 390 391 dead_rootmount = vfs_mountalloc(&dead_vfsops, NULL); 392 KASSERT(dead_rootmount != NULL); 393 dead_rootmount->mnt_iflag |= IMNT_MPSAFE; 394 395 mutex_init(&vdrain_lock, MUTEX_DEFAULT, IPL_NONE); 396 for (i = 0; i < LRU_COUNT; i++) { 397 TAILQ_INIT(&lru_list[i]); 398 } 399 vcache_init(); 400 401 cv_init(&vdrain_cv, "vdrain"); 402 cv_init(&vdrain_gen_cv, "vdrainwt"); 403 error = kthread_create(PRI_VM, KTHREAD_MPSAFE, NULL, vdrain_thread, 404 NULL, &vdrain_lwp, "vdrain"); 405 KASSERTMSG((error == 0), "kthread_create(vdrain) failed: %d", error); 406 } 407 408 /* 409 * Allocate a new marker vnode. 410 */ 411 vnode_t * 412 vnalloc_marker(struct mount *mp) 413 { 414 vnode_impl_t *vip; 415 vnode_t *vp; 416 417 vip = pool_cache_get(vcache_pool, PR_WAITOK); 418 memset(vip, 0, sizeof(*vip)); 419 vp = VIMPL_TO_VNODE(vip); 420 uvm_obj_init(&vp->v_uobj, &uvm_vnodeops, true, 1); 421 vp->v_mount = mp; 422 vp->v_type = VBAD; 423 vp->v_interlock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE); 424 vip->vi_state = VS_MARKER; 425 426 return vp; 427 } 428 429 /* 430 * Free a marker vnode. 431 */ 432 void 433 vnfree_marker(vnode_t *vp) 434 { 435 vnode_impl_t *vip; 436 437 vip = VNODE_TO_VIMPL(vp); 438 KASSERT(vip->vi_state == VS_MARKER); 439 mutex_obj_free(vp->v_interlock); 440 uvm_obj_destroy(&vp->v_uobj, true); 441 pool_cache_put(vcache_pool, vip); 442 } 443 444 /* 445 * Test a vnode for being a marker vnode. 446 */ 447 bool 448 vnis_marker(vnode_t *vp) 449 { 450 451 return (VNODE_TO_VIMPL(vp)->vi_state == VS_MARKER); 452 } 453 454 /* 455 * Return the lru list this node should be on. 456 */ 457 static vnodelst_t * 458 lru_which(vnode_t *vp) 459 { 460 461 KASSERT(mutex_owned(vp->v_interlock)); 462 463 if (vp->v_holdcnt > 0) 464 return &lru_list[LRU_HOLD]; 465 else 466 return &lru_list[LRU_FREE]; 467 } 468 469 /* 470 * Put vnode to end of given list. 471 * Both the current and the new list may be NULL, used on vnode alloc/free. 472 * Adjust numvnodes and signal vdrain thread if there is work. 473 */ 474 static void 475 lru_requeue(vnode_t *vp, vnodelst_t *listhd) 476 { 477 vnode_impl_t *vip; 478 int d; 479 480 /* 481 * If the vnode is on the correct list, and was put there recently, 482 * then leave it be, thus avoiding huge cache and lock contention. 483 */ 484 vip = VNODE_TO_VIMPL(vp); 485 if (listhd == vip->vi_lrulisthd && 486 (hardclock_ticks - vip->vi_lrulisttm) < hz) { 487 return; 488 } 489 490 mutex_enter(&vdrain_lock); 491 d = 0; 492 if (vip->vi_lrulisthd != NULL) 493 TAILQ_REMOVE(vip->vi_lrulisthd, vip, vi_lrulist); 494 else 495 d++; 496 vip->vi_lrulisthd = listhd; 497 vip->vi_lrulisttm = hardclock_ticks; 498 if (vip->vi_lrulisthd != NULL) 499 TAILQ_INSERT_TAIL(vip->vi_lrulisthd, vip, vi_lrulist); 500 else 501 d--; 502 if (d != 0) { 503 /* 504 * Looks strange? This is not a bug. Don't store 505 * numvnodes unless there is a change - avoid false 506 * sharing on MP. 507 */ 508 numvnodes += d; 509 } 510 if (numvnodes > desiredvnodes || listhd == &lru_list[LRU_VRELE]) 511 cv_broadcast(&vdrain_cv); 512 mutex_exit(&vdrain_lock); 513 } 514 515 /* 516 * Release deferred vrele vnodes for this mount. 517 * Called with file system suspended. 518 */ 519 void 520 vrele_flush(struct mount *mp) 521 { 522 vnode_impl_t *vip, *marker; 523 vnode_t *vp; 524 525 KASSERT(fstrans_is_owner(mp)); 526 527 marker = VNODE_TO_VIMPL(vnalloc_marker(NULL)); 528 529 mutex_enter(&vdrain_lock); 530 TAILQ_INSERT_HEAD(&lru_list[LRU_VRELE], marker, vi_lrulist); 531 532 while ((vip = TAILQ_NEXT(marker, vi_lrulist))) { 533 TAILQ_REMOVE(&lru_list[LRU_VRELE], marker, vi_lrulist); 534 TAILQ_INSERT_AFTER(&lru_list[LRU_VRELE], vip, marker, 535 vi_lrulist); 536 vp = VIMPL_TO_VNODE(vip); 537 if (vnis_marker(vp)) 538 continue; 539 540 KASSERT(vip->vi_lrulisthd == &lru_list[LRU_VRELE]); 541 TAILQ_REMOVE(vip->vi_lrulisthd, vip, vi_lrulist); 542 vip->vi_lrulisthd = &lru_list[LRU_HOLD]; 543 vip->vi_lrulisttm = hardclock_ticks; 544 TAILQ_INSERT_TAIL(vip->vi_lrulisthd, vip, vi_lrulist); 545 mutex_exit(&vdrain_lock); 546 547 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 548 mutex_enter(vp->v_interlock); 549 vrelel(vp, 0, LK_EXCLUSIVE); 550 551 mutex_enter(&vdrain_lock); 552 } 553 554 TAILQ_REMOVE(&lru_list[LRU_VRELE], marker, vi_lrulist); 555 mutex_exit(&vdrain_lock); 556 557 vnfree_marker(VIMPL_TO_VNODE(marker)); 558 } 559 560 /* 561 * Reclaim a cached vnode. Used from vdrain_thread only. 562 */ 563 static __inline void 564 vdrain_remove(vnode_t *vp) 565 { 566 struct mount *mp; 567 568 KASSERT(mutex_owned(&vdrain_lock)); 569 570 /* Probe usecount (unlocked). */ 571 if (vp->v_usecount > 0) 572 return; 573 /* Try v_interlock -- we lock the wrong direction! */ 574 if (!mutex_tryenter(vp->v_interlock)) 575 return; 576 /* Probe usecount and state. */ 577 if (vp->v_usecount > 0 || VSTATE_GET(vp) != VS_LOADED) { 578 mutex_exit(vp->v_interlock); 579 return; 580 } 581 mp = vp->v_mount; 582 if (fstrans_start_nowait(mp) != 0) { 583 mutex_exit(vp->v_interlock); 584 return; 585 } 586 vdrain_retry = true; 587 mutex_exit(&vdrain_lock); 588 589 if (vcache_vget(vp) == 0) { 590 if (!vrecycle(vp)) { 591 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 592 mutex_enter(vp->v_interlock); 593 vrelel(vp, 0, LK_EXCLUSIVE); 594 } 595 } 596 fstrans_done(mp); 597 598 mutex_enter(&vdrain_lock); 599 } 600 601 /* 602 * Release a cached vnode. Used from vdrain_thread only. 603 */ 604 static __inline void 605 vdrain_vrele(vnode_t *vp) 606 { 607 vnode_impl_t *vip = VNODE_TO_VIMPL(vp); 608 struct mount *mp; 609 610 KASSERT(mutex_owned(&vdrain_lock)); 611 612 mp = vp->v_mount; 613 if (fstrans_start_nowait(mp) != 0) 614 return; 615 616 /* 617 * First remove the vnode from the vrele list. 618 * Put it on the last lru list, the last vrele() 619 * will put it back onto the right list before 620 * its v_usecount reaches zero. 621 */ 622 KASSERT(vip->vi_lrulisthd == &lru_list[LRU_VRELE]); 623 TAILQ_REMOVE(vip->vi_lrulisthd, vip, vi_lrulist); 624 vip->vi_lrulisthd = &lru_list[LRU_HOLD]; 625 vip->vi_lrulisttm = hardclock_ticks; 626 TAILQ_INSERT_TAIL(vip->vi_lrulisthd, vip, vi_lrulist); 627 628 vdrain_retry = true; 629 mutex_exit(&vdrain_lock); 630 631 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 632 mutex_enter(vp->v_interlock); 633 vrelel(vp, 0, LK_EXCLUSIVE); 634 fstrans_done(mp); 635 636 mutex_enter(&vdrain_lock); 637 } 638 639 /* 640 * Helper thread to keep the number of vnodes below desiredvnodes 641 * and release vnodes from asynchronous vrele. 642 */ 643 static void 644 vdrain_thread(void *cookie) 645 { 646 int i; 647 u_int target; 648 vnode_impl_t *vip, *marker; 649 650 marker = VNODE_TO_VIMPL(vnalloc_marker(NULL)); 651 652 mutex_enter(&vdrain_lock); 653 654 for (;;) { 655 vdrain_retry = false; 656 target = desiredvnodes - desiredvnodes/10; 657 658 for (i = 0; i < LRU_COUNT; i++) { 659 TAILQ_INSERT_HEAD(&lru_list[i], marker, vi_lrulist); 660 while ((vip = TAILQ_NEXT(marker, vi_lrulist))) { 661 TAILQ_REMOVE(&lru_list[i], marker, vi_lrulist); 662 TAILQ_INSERT_AFTER(&lru_list[i], vip, marker, 663 vi_lrulist); 664 if (vnis_marker(VIMPL_TO_VNODE(vip))) 665 continue; 666 if (i == LRU_VRELE) 667 vdrain_vrele(VIMPL_TO_VNODE(vip)); 668 else if (numvnodes < target) 669 break; 670 else 671 vdrain_remove(VIMPL_TO_VNODE(vip)); 672 } 673 TAILQ_REMOVE(&lru_list[i], marker, vi_lrulist); 674 } 675 676 if (vdrain_retry) { 677 mutex_exit(&vdrain_lock); 678 yield(); 679 mutex_enter(&vdrain_lock); 680 } else { 681 vdrain_gen++; 682 cv_broadcast(&vdrain_gen_cv); 683 cv_wait(&vdrain_cv, &vdrain_lock); 684 } 685 } 686 } 687 688 /* 689 * Try to drop reference on a vnode. Abort if we are releasing the 690 * last reference. Note: this _must_ succeed if not the last reference. 691 */ 692 static bool 693 vtryrele(vnode_t *vp) 694 { 695 u_int use, next; 696 697 for (use = atomic_load_relaxed(&vp->v_usecount);; use = next) { 698 if (__predict_false(use == 1)) { 699 return false; 700 } 701 KASSERT(use > 1); 702 next = atomic_cas_uint(&vp->v_usecount, use, use - 1); 703 if (__predict_true(next == use)) { 704 return true; 705 } 706 } 707 } 708 709 /* 710 * vput: unlock and release the reference. 711 */ 712 void 713 vput(vnode_t *vp) 714 { 715 int lktype; 716 717 /* 718 * Do an unlocked check of v_usecount. If it looks like we're not 719 * about to drop the last reference, then unlock the vnode and try 720 * to drop the reference. If it ends up being the last reference 721 * after all, vrelel() can fix it all up. Most of the time this 722 * will all go to plan. 723 */ 724 if (atomic_load_relaxed(&vp->v_usecount) > 1) { 725 VOP_UNLOCK(vp); 726 if (vtryrele(vp)) { 727 return; 728 } 729 lktype = LK_NONE; 730 } else if ((vp->v_vflag & VV_LOCKSWORK) == 0) { 731 lktype = LK_EXCLUSIVE; 732 } else { 733 lktype = VOP_ISLOCKED(vp); 734 KASSERT(lktype != LK_NONE); 735 } 736 mutex_enter(vp->v_interlock); 737 vrelel(vp, 0, lktype); 738 } 739 740 /* 741 * Vnode release. If reference count drops to zero, call inactive 742 * routine and either return to freelist or free to the pool. 743 */ 744 static void 745 vrelel(vnode_t *vp, int flags, int lktype) 746 { 747 const bool async = ((flags & VRELEL_ASYNC) != 0); 748 bool recycle, defer; 749 int error; 750 751 KASSERT(mutex_owned(vp->v_interlock)); 752 753 if (__predict_false(vp->v_op == dead_vnodeop_p && 754 VSTATE_GET(vp) != VS_RECLAIMED)) { 755 vnpanic(vp, "dead but not clean"); 756 } 757 758 /* 759 * If not the last reference, just drop the reference count and 760 * unlock. VOP_UNLOCK() is called here without a vnode reference 761 * held, but is ok as the hold of v_interlock will stop the vnode 762 * from disappearing. 763 */ 764 if (vtryrele(vp)) { 765 if (lktype != LK_NONE) { 766 VOP_UNLOCK(vp); 767 } 768 mutex_exit(vp->v_interlock); 769 return; 770 } 771 if (vp->v_usecount <= 0 || vp->v_writecount != 0) { 772 vnpanic(vp, "%s: bad ref count", __func__); 773 } 774 775 #ifdef DIAGNOSTIC 776 if ((vp->v_type == VBLK || vp->v_type == VCHR) && 777 vp->v_specnode != NULL && vp->v_specnode->sn_opencnt != 0) { 778 vprint("vrelel: missing VOP_CLOSE()", vp); 779 } 780 #endif 781 782 /* 783 * First try to get the vnode locked for VOP_INACTIVE(). 784 * Defer vnode release to vdrain_thread if caller requests 785 * it explicitly, is the pagedaemon or the lock failed. 786 */ 787 defer = false; 788 if ((curlwp == uvm.pagedaemon_lwp) || async) { 789 defer = true; 790 } else if (lktype == LK_SHARED) { 791 /* Excellent chance of getting, if the last ref. */ 792 error = vn_lock(vp, LK_UPGRADE | LK_RETRY | 793 LK_NOWAIT); 794 if (error != 0) { 795 defer = true; 796 } else { 797 lktype = LK_EXCLUSIVE; 798 } 799 } else if (lktype == LK_NONE) { 800 /* Excellent chance of getting, if the last ref. */ 801 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | 802 LK_NOWAIT); 803 if (error != 0) { 804 defer = true; 805 } else { 806 lktype = LK_EXCLUSIVE; 807 } 808 } 809 KASSERT(mutex_owned(vp->v_interlock)); 810 if (defer) { 811 /* 812 * Defer reclaim to the kthread; it's not safe to 813 * clean it here. We donate it our last reference. 814 */ 815 if (lktype != LK_NONE) { 816 VOP_UNLOCK(vp); 817 } 818 lru_requeue(vp, &lru_list[LRU_VRELE]); 819 mutex_exit(vp->v_interlock); 820 return; 821 } 822 KASSERT(lktype == LK_EXCLUSIVE); 823 824 /* 825 * If not clean, deactivate the vnode, but preserve 826 * our reference across the call to VOP_INACTIVE(). 827 */ 828 if (VSTATE_GET(vp) == VS_RECLAIMED) { 829 VOP_UNLOCK(vp); 830 } else { 831 VSTATE_CHANGE(vp, VS_LOADED, VS_BLOCKED); 832 mutex_exit(vp->v_interlock); 833 834 /* 835 * The vnode must not gain another reference while being 836 * deactivated. If VOP_INACTIVE() indicates that 837 * the described file has been deleted, then recycle 838 * the vnode. 839 * 840 * Note that VOP_INACTIVE() will not drop the vnode lock. 841 */ 842 recycle = false; 843 VOP_INACTIVE(vp, &recycle); 844 if (!recycle) 845 VOP_UNLOCK(vp); 846 rw_enter(vp->v_uobj.vmobjlock, RW_WRITER); 847 mutex_enter(vp->v_interlock); 848 VSTATE_CHANGE(vp, VS_BLOCKED, VS_LOADED); 849 if (!recycle) { 850 if (vtryrele(vp)) { 851 mutex_exit(vp->v_interlock); 852 rw_exit(vp->v_uobj.vmobjlock); 853 return; 854 } 855 } 856 857 /* Take care of space accounting. */ 858 if ((vp->v_iflag & VI_EXECMAP) != 0 && 859 vp->v_uobj.uo_npages != 0) { 860 cpu_count(CPU_COUNT_EXECPAGES, -vp->v_uobj.uo_npages); 861 cpu_count(CPU_COUNT_FILEPAGES, vp->v_uobj.uo_npages); 862 } 863 vp->v_iflag &= ~(VI_TEXT|VI_EXECMAP|VI_WRMAP); 864 vp->v_vflag &= ~VV_MAPPED; 865 rw_exit(vp->v_uobj.vmobjlock); 866 867 /* 868 * Recycle the vnode if the file is now unused (unlinked), 869 * otherwise just free it. 870 */ 871 if (recycle) { 872 VSTATE_ASSERT(vp, VS_LOADED); 873 /* vcache_reclaim drops the lock. */ 874 vcache_reclaim(vp); 875 } 876 KASSERT(vp->v_usecount > 0); 877 } 878 879 if (atomic_dec_uint_nv(&vp->v_usecount) != 0) { 880 /* Gained another reference while being reclaimed. */ 881 mutex_exit(vp->v_interlock); 882 return; 883 } 884 885 if (VSTATE_GET(vp) == VS_RECLAIMED && vp->v_holdcnt == 0) { 886 /* 887 * It's clean so destroy it. It isn't referenced 888 * anywhere since it has been reclaimed. 889 */ 890 vcache_free(VNODE_TO_VIMPL(vp)); 891 } else { 892 /* 893 * Otherwise, put it back onto the freelist. It 894 * can't be destroyed while still associated with 895 * a file system. 896 */ 897 lru_requeue(vp, lru_which(vp)); 898 mutex_exit(vp->v_interlock); 899 } 900 } 901 902 void 903 vrele(vnode_t *vp) 904 { 905 906 if (vtryrele(vp)) { 907 return; 908 } 909 mutex_enter(vp->v_interlock); 910 vrelel(vp, 0, LK_NONE); 911 } 912 913 /* 914 * Asynchronous vnode release, vnode is released in different context. 915 */ 916 void 917 vrele_async(vnode_t *vp) 918 { 919 920 if (vtryrele(vp)) { 921 return; 922 } 923 mutex_enter(vp->v_interlock); 924 vrelel(vp, VRELEL_ASYNC, LK_NONE); 925 } 926 927 /* 928 * Vnode reference, where a reference is already held by some other 929 * object (for example, a file structure). 930 * 931 * NB: we have lockless code sequences that rely on this not blocking. 932 */ 933 void 934 vref(vnode_t *vp) 935 { 936 937 KASSERT(atomic_load_relaxed(&vp->v_usecount) != 0); 938 939 atomic_inc_uint(&vp->v_usecount); 940 } 941 942 /* 943 * Page or buffer structure gets a reference. 944 * Called with v_interlock held. 945 */ 946 void 947 vholdl(vnode_t *vp) 948 { 949 950 KASSERT(mutex_owned(vp->v_interlock)); 951 952 if (vp->v_holdcnt++ == 0 && vp->v_usecount == 0) 953 lru_requeue(vp, lru_which(vp)); 954 } 955 956 /* 957 * Page or buffer structure gets a reference. 958 */ 959 void 960 vhold(vnode_t *vp) 961 { 962 963 mutex_enter(vp->v_interlock); 964 vholdl(vp); 965 mutex_exit(vp->v_interlock); 966 } 967 968 /* 969 * Page or buffer structure frees a reference. 970 * Called with v_interlock held. 971 */ 972 void 973 holdrelel(vnode_t *vp) 974 { 975 976 KASSERT(mutex_owned(vp->v_interlock)); 977 978 if (vp->v_holdcnt <= 0) { 979 vnpanic(vp, "%s: holdcnt vp %p", __func__, vp); 980 } 981 982 vp->v_holdcnt--; 983 if (vp->v_holdcnt == 0 && vp->v_usecount == 0) 984 lru_requeue(vp, lru_which(vp)); 985 } 986 987 /* 988 * Page or buffer structure frees a reference. 989 */ 990 void 991 holdrele(vnode_t *vp) 992 { 993 994 mutex_enter(vp->v_interlock); 995 holdrelel(vp); 996 mutex_exit(vp->v_interlock); 997 } 998 999 /* 1000 * Recycle an unused vnode if caller holds the last reference. 1001 */ 1002 bool 1003 vrecycle(vnode_t *vp) 1004 { 1005 int error __diagused; 1006 1007 mutex_enter(vp->v_interlock); 1008 1009 /* Make sure we hold the last reference. */ 1010 VSTATE_WAIT_STABLE(vp); 1011 if (vp->v_usecount != 1) { 1012 mutex_exit(vp->v_interlock); 1013 return false; 1014 } 1015 1016 /* If the vnode is already clean we're done. */ 1017 if (VSTATE_GET(vp) != VS_LOADED) { 1018 VSTATE_ASSERT(vp, VS_RECLAIMED); 1019 vrelel(vp, 0, LK_NONE); 1020 return true; 1021 } 1022 1023 /* Prevent further references until the vnode is locked. */ 1024 VSTATE_CHANGE(vp, VS_LOADED, VS_BLOCKED); 1025 mutex_exit(vp->v_interlock); 1026 1027 /* 1028 * On a leaf file system this lock will always succeed as we hold 1029 * the last reference and prevent further references. 1030 * On layered file systems waiting for the lock would open a can of 1031 * deadlocks as the lower vnodes may have other active references. 1032 */ 1033 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_NOWAIT); 1034 1035 mutex_enter(vp->v_interlock); 1036 VSTATE_CHANGE(vp, VS_BLOCKED, VS_LOADED); 1037 1038 if (error) { 1039 mutex_exit(vp->v_interlock); 1040 return false; 1041 } 1042 1043 KASSERT(vp->v_usecount == 1); 1044 vcache_reclaim(vp); 1045 vrelel(vp, 0, LK_NONE); 1046 1047 return true; 1048 } 1049 1050 /* 1051 * Helper for vrevoke() to propagate suspension from lastmp 1052 * to thismp. Both args may be NULL. 1053 * Returns the currently suspended file system or NULL. 1054 */ 1055 static struct mount * 1056 vrevoke_suspend_next(struct mount *lastmp, struct mount *thismp) 1057 { 1058 int error; 1059 1060 if (lastmp == thismp) 1061 return thismp; 1062 1063 if (lastmp != NULL) 1064 vfs_resume(lastmp); 1065 1066 if (thismp == NULL) 1067 return NULL; 1068 1069 do { 1070 error = vfs_suspend(thismp, 0); 1071 } while (error == EINTR || error == ERESTART); 1072 1073 if (error == 0) 1074 return thismp; 1075 1076 KASSERT(error == EOPNOTSUPP); 1077 return NULL; 1078 } 1079 1080 /* 1081 * Eliminate all activity associated with the requested vnode 1082 * and with all vnodes aliased to the requested vnode. 1083 */ 1084 void 1085 vrevoke(vnode_t *vp) 1086 { 1087 struct mount *mp; 1088 vnode_t *vq; 1089 enum vtype type; 1090 dev_t dev; 1091 1092 KASSERT(vp->v_usecount > 0); 1093 1094 mp = vrevoke_suspend_next(NULL, vp->v_mount); 1095 1096 mutex_enter(vp->v_interlock); 1097 VSTATE_WAIT_STABLE(vp); 1098 if (VSTATE_GET(vp) == VS_RECLAIMED) { 1099 mutex_exit(vp->v_interlock); 1100 } else if (vp->v_type != VBLK && vp->v_type != VCHR) { 1101 atomic_inc_uint(&vp->v_usecount); 1102 mutex_exit(vp->v_interlock); 1103 vgone(vp); 1104 } else { 1105 dev = vp->v_rdev; 1106 type = vp->v_type; 1107 mutex_exit(vp->v_interlock); 1108 1109 while (spec_node_lookup_by_dev(type, dev, &vq) == 0) { 1110 mp = vrevoke_suspend_next(mp, vq->v_mount); 1111 vgone(vq); 1112 } 1113 } 1114 vrevoke_suspend_next(mp, NULL); 1115 } 1116 1117 /* 1118 * Eliminate all activity associated with a vnode in preparation for 1119 * reuse. Drops a reference from the vnode. 1120 */ 1121 void 1122 vgone(vnode_t *vp) 1123 { 1124 int lktype; 1125 1126 KASSERT(vp->v_mount == dead_rootmount || fstrans_is_owner(vp->v_mount)); 1127 1128 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1129 lktype = LK_EXCLUSIVE; 1130 mutex_enter(vp->v_interlock); 1131 VSTATE_WAIT_STABLE(vp); 1132 if (VSTATE_GET(vp) == VS_LOADED) { 1133 vcache_reclaim(vp); 1134 lktype = LK_NONE; 1135 } 1136 VSTATE_ASSERT(vp, VS_RECLAIMED); 1137 vrelel(vp, 0, lktype); 1138 } 1139 1140 static inline uint32_t 1141 vcache_hash(const struct vcache_key *key) 1142 { 1143 uint32_t hash = HASH32_BUF_INIT; 1144 1145 KASSERT(key->vk_key_len > 0); 1146 1147 hash = hash32_buf(&key->vk_mount, sizeof(struct mount *), hash); 1148 hash = hash32_buf(key->vk_key, key->vk_key_len, hash); 1149 return hash; 1150 } 1151 1152 static void 1153 vcache_init(void) 1154 { 1155 1156 vcache_pool = pool_cache_init(sizeof(vnode_impl_t), coherency_unit, 1157 0, 0, "vcachepl", NULL, IPL_NONE, NULL, NULL, NULL); 1158 KASSERT(vcache_pool != NULL); 1159 mutex_init(&vcache_lock, MUTEX_DEFAULT, IPL_NONE); 1160 cv_init(&vcache_cv, "vcache"); 1161 vcache_hashsize = desiredvnodes; 1162 vcache_hashtab = hashinit(desiredvnodes, HASH_SLIST, true, 1163 &vcache_hashmask); 1164 } 1165 1166 static void 1167 vcache_reinit(void) 1168 { 1169 int i; 1170 uint32_t hash; 1171 u_long oldmask, newmask; 1172 struct hashhead *oldtab, *newtab; 1173 vnode_impl_t *vip; 1174 1175 newtab = hashinit(desiredvnodes, HASH_SLIST, true, &newmask); 1176 mutex_enter(&vcache_lock); 1177 oldtab = vcache_hashtab; 1178 oldmask = vcache_hashmask; 1179 vcache_hashsize = desiredvnodes; 1180 vcache_hashtab = newtab; 1181 vcache_hashmask = newmask; 1182 for (i = 0; i <= oldmask; i++) { 1183 while ((vip = SLIST_FIRST(&oldtab[i])) != NULL) { 1184 SLIST_REMOVE(&oldtab[i], vip, vnode_impl, vi_hash); 1185 hash = vcache_hash(&vip->vi_key); 1186 SLIST_INSERT_HEAD(&newtab[hash & vcache_hashmask], 1187 vip, vi_hash); 1188 } 1189 } 1190 mutex_exit(&vcache_lock); 1191 hashdone(oldtab, HASH_SLIST, oldmask); 1192 } 1193 1194 static inline vnode_impl_t * 1195 vcache_hash_lookup(const struct vcache_key *key, uint32_t hash) 1196 { 1197 struct hashhead *hashp; 1198 vnode_impl_t *vip; 1199 1200 KASSERT(mutex_owned(&vcache_lock)); 1201 1202 hashp = &vcache_hashtab[hash & vcache_hashmask]; 1203 SLIST_FOREACH(vip, hashp, vi_hash) { 1204 if (key->vk_mount != vip->vi_key.vk_mount) 1205 continue; 1206 if (key->vk_key_len != vip->vi_key.vk_key_len) 1207 continue; 1208 if (memcmp(key->vk_key, vip->vi_key.vk_key, key->vk_key_len)) 1209 continue; 1210 return vip; 1211 } 1212 return NULL; 1213 } 1214 1215 /* 1216 * Allocate a new, uninitialized vcache node. 1217 */ 1218 static vnode_impl_t * 1219 vcache_alloc(void) 1220 { 1221 vnode_impl_t *vip; 1222 vnode_t *vp; 1223 1224 vip = pool_cache_get(vcache_pool, PR_WAITOK); 1225 vp = VIMPL_TO_VNODE(vip); 1226 memset(vip, 0, sizeof(*vip)); 1227 1228 rw_init(&vip->vi_lock); 1229 vp->v_interlock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE); 1230 1231 uvm_obj_init(&vp->v_uobj, &uvm_vnodeops, true, 1); 1232 cv_init(&vp->v_cv, "vnode"); 1233 cache_vnode_init(vp); 1234 1235 vp->v_usecount = 1; 1236 vp->v_type = VNON; 1237 vp->v_size = vp->v_writesize = VSIZENOTSET; 1238 1239 vip->vi_state = VS_LOADING; 1240 1241 lru_requeue(vp, &lru_list[LRU_FREE]); 1242 1243 return vip; 1244 } 1245 1246 /* 1247 * Deallocate a vcache node in state VS_LOADING. 1248 * 1249 * vcache_lock held on entry and released on return. 1250 */ 1251 static void 1252 vcache_dealloc(vnode_impl_t *vip) 1253 { 1254 vnode_t *vp; 1255 1256 KASSERT(mutex_owned(&vcache_lock)); 1257 1258 vp = VIMPL_TO_VNODE(vip); 1259 vfs_ref(dead_rootmount); 1260 vfs_insmntque(vp, dead_rootmount); 1261 mutex_enter(vp->v_interlock); 1262 vp->v_op = dead_vnodeop_p; 1263 VSTATE_CHANGE(vp, VS_LOADING, VS_RECLAIMED); 1264 mutex_exit(&vcache_lock); 1265 vrelel(vp, 0, LK_NONE); 1266 } 1267 1268 /* 1269 * Free an unused, unreferenced vcache node. 1270 * v_interlock locked on entry. 1271 */ 1272 static void 1273 vcache_free(vnode_impl_t *vip) 1274 { 1275 vnode_t *vp; 1276 1277 vp = VIMPL_TO_VNODE(vip); 1278 KASSERT(mutex_owned(vp->v_interlock)); 1279 1280 KASSERT(vp->v_usecount == 0); 1281 KASSERT(vp->v_holdcnt == 0); 1282 KASSERT(vp->v_writecount == 0); 1283 lru_requeue(vp, NULL); 1284 mutex_exit(vp->v_interlock); 1285 1286 vfs_insmntque(vp, NULL); 1287 if (vp->v_type == VBLK || vp->v_type == VCHR) 1288 spec_node_destroy(vp); 1289 1290 mutex_obj_free(vp->v_interlock); 1291 rw_destroy(&vip->vi_lock); 1292 uvm_obj_destroy(&vp->v_uobj, true); 1293 cv_destroy(&vp->v_cv); 1294 cache_vnode_fini(vp); 1295 pool_cache_put(vcache_pool, vip); 1296 } 1297 1298 /* 1299 * Try to get an initial reference on this cached vnode. 1300 * Returns zero on success, ENOENT if the vnode has been reclaimed and 1301 * EBUSY if the vnode state is unstable. 1302 * 1303 * v_interlock locked on entry and unlocked on exit. 1304 */ 1305 int 1306 vcache_tryvget(vnode_t *vp) 1307 { 1308 int error = 0; 1309 1310 KASSERT(mutex_owned(vp->v_interlock)); 1311 1312 if (__predict_false(VSTATE_GET(vp) == VS_RECLAIMED)) 1313 error = ENOENT; 1314 else if (__predict_false(VSTATE_GET(vp) != VS_LOADED)) 1315 error = EBUSY; 1316 else if (vp->v_usecount == 0) 1317 vp->v_usecount = 1; 1318 else 1319 atomic_inc_uint(&vp->v_usecount); 1320 1321 mutex_exit(vp->v_interlock); 1322 1323 return error; 1324 } 1325 1326 /* 1327 * Try to get an initial reference on this cached vnode. 1328 * Returns zero on success and ENOENT if the vnode has been reclaimed. 1329 * Will wait for the vnode state to be stable. 1330 * 1331 * v_interlock locked on entry and unlocked on exit. 1332 */ 1333 int 1334 vcache_vget(vnode_t *vp) 1335 { 1336 1337 KASSERT(mutex_owned(vp->v_interlock)); 1338 1339 /* Increment hold count to prevent vnode from disappearing. */ 1340 vp->v_holdcnt++; 1341 VSTATE_WAIT_STABLE(vp); 1342 vp->v_holdcnt--; 1343 1344 /* If this was the last reference to a reclaimed vnode free it now. */ 1345 if (__predict_false(VSTATE_GET(vp) == VS_RECLAIMED)) { 1346 if (vp->v_holdcnt == 0 && vp->v_usecount == 0) 1347 vcache_free(VNODE_TO_VIMPL(vp)); 1348 else 1349 mutex_exit(vp->v_interlock); 1350 return ENOENT; 1351 } 1352 VSTATE_ASSERT(vp, VS_LOADED); 1353 if (vp->v_usecount == 0) 1354 vp->v_usecount = 1; 1355 else 1356 atomic_inc_uint(&vp->v_usecount); 1357 mutex_exit(vp->v_interlock); 1358 1359 return 0; 1360 } 1361 1362 /* 1363 * Get a vnode / fs node pair by key and return it referenced through vpp. 1364 */ 1365 int 1366 vcache_get(struct mount *mp, const void *key, size_t key_len, 1367 struct vnode **vpp) 1368 { 1369 int error; 1370 uint32_t hash; 1371 const void *new_key; 1372 struct vnode *vp; 1373 struct vcache_key vcache_key; 1374 vnode_impl_t *vip, *new_vip; 1375 1376 new_key = NULL; 1377 *vpp = NULL; 1378 1379 vcache_key.vk_mount = mp; 1380 vcache_key.vk_key = key; 1381 vcache_key.vk_key_len = key_len; 1382 hash = vcache_hash(&vcache_key); 1383 1384 again: 1385 mutex_enter(&vcache_lock); 1386 vip = vcache_hash_lookup(&vcache_key, hash); 1387 1388 /* If found, take a reference or retry. */ 1389 if (__predict_true(vip != NULL)) { 1390 /* 1391 * If the vnode is loading we cannot take the v_interlock 1392 * here as it might change during load (see uvm_obj_setlock()). 1393 * As changing state from VS_LOADING requires both vcache_lock 1394 * and v_interlock it is safe to test with vcache_lock held. 1395 * 1396 * Wait for vnodes changing state from VS_LOADING and retry. 1397 */ 1398 if (__predict_false(vip->vi_state == VS_LOADING)) { 1399 cv_wait(&vcache_cv, &vcache_lock); 1400 mutex_exit(&vcache_lock); 1401 goto again; 1402 } 1403 vp = VIMPL_TO_VNODE(vip); 1404 mutex_enter(vp->v_interlock); 1405 mutex_exit(&vcache_lock); 1406 error = vcache_vget(vp); 1407 if (error == ENOENT) 1408 goto again; 1409 if (error == 0) 1410 *vpp = vp; 1411 KASSERT((error != 0) == (*vpp == NULL)); 1412 return error; 1413 } 1414 mutex_exit(&vcache_lock); 1415 1416 /* Allocate and initialize a new vcache / vnode pair. */ 1417 error = vfs_busy(mp); 1418 if (error) 1419 return error; 1420 new_vip = vcache_alloc(); 1421 new_vip->vi_key = vcache_key; 1422 vp = VIMPL_TO_VNODE(new_vip); 1423 mutex_enter(&vcache_lock); 1424 vip = vcache_hash_lookup(&vcache_key, hash); 1425 if (vip == NULL) { 1426 SLIST_INSERT_HEAD(&vcache_hashtab[hash & vcache_hashmask], 1427 new_vip, vi_hash); 1428 vip = new_vip; 1429 } 1430 1431 /* If another thread beat us inserting this node, retry. */ 1432 if (vip != new_vip) { 1433 vcache_dealloc(new_vip); 1434 vfs_unbusy(mp); 1435 goto again; 1436 } 1437 mutex_exit(&vcache_lock); 1438 1439 /* Load the fs node. Exclusive as new_node is VS_LOADING. */ 1440 error = VFS_LOADVNODE(mp, vp, key, key_len, &new_key); 1441 if (error) { 1442 mutex_enter(&vcache_lock); 1443 SLIST_REMOVE(&vcache_hashtab[hash & vcache_hashmask], 1444 new_vip, vnode_impl, vi_hash); 1445 vcache_dealloc(new_vip); 1446 vfs_unbusy(mp); 1447 KASSERT(*vpp == NULL); 1448 return error; 1449 } 1450 KASSERT(new_key != NULL); 1451 KASSERT(memcmp(key, new_key, key_len) == 0); 1452 KASSERT(vp->v_op != NULL); 1453 vfs_insmntque(vp, mp); 1454 if ((mp->mnt_iflag & IMNT_MPSAFE) != 0) 1455 vp->v_vflag |= VV_MPSAFE; 1456 vfs_ref(mp); 1457 vfs_unbusy(mp); 1458 1459 /* Finished loading, finalize node. */ 1460 mutex_enter(&vcache_lock); 1461 new_vip->vi_key.vk_key = new_key; 1462 mutex_enter(vp->v_interlock); 1463 VSTATE_CHANGE(vp, VS_LOADING, VS_LOADED); 1464 mutex_exit(vp->v_interlock); 1465 mutex_exit(&vcache_lock); 1466 *vpp = vp; 1467 return 0; 1468 } 1469 1470 /* 1471 * Create a new vnode / fs node pair and return it referenced through vpp. 1472 */ 1473 int 1474 vcache_new(struct mount *mp, struct vnode *dvp, struct vattr *vap, 1475 kauth_cred_t cred, void *extra, struct vnode **vpp) 1476 { 1477 int error; 1478 uint32_t hash; 1479 struct vnode *vp, *ovp; 1480 vnode_impl_t *vip, *ovip; 1481 1482 *vpp = NULL; 1483 1484 /* Allocate and initialize a new vcache / vnode pair. */ 1485 error = vfs_busy(mp); 1486 if (error) 1487 return error; 1488 vip = vcache_alloc(); 1489 vip->vi_key.vk_mount = mp; 1490 vp = VIMPL_TO_VNODE(vip); 1491 1492 /* Create and load the fs node. */ 1493 error = VFS_NEWVNODE(mp, dvp, vp, vap, cred, extra, 1494 &vip->vi_key.vk_key_len, &vip->vi_key.vk_key); 1495 if (error) { 1496 mutex_enter(&vcache_lock); 1497 vcache_dealloc(vip); 1498 vfs_unbusy(mp); 1499 KASSERT(*vpp == NULL); 1500 return error; 1501 } 1502 KASSERT(vp->v_op != NULL); 1503 KASSERT((vip->vi_key.vk_key_len == 0) == (mp == dead_rootmount)); 1504 if (vip->vi_key.vk_key_len > 0) { 1505 KASSERT(vip->vi_key.vk_key != NULL); 1506 hash = vcache_hash(&vip->vi_key); 1507 1508 /* 1509 * Wait for previous instance to be reclaimed, 1510 * then insert new node. 1511 */ 1512 mutex_enter(&vcache_lock); 1513 while ((ovip = vcache_hash_lookup(&vip->vi_key, hash))) { 1514 ovp = VIMPL_TO_VNODE(ovip); 1515 mutex_enter(ovp->v_interlock); 1516 mutex_exit(&vcache_lock); 1517 error = vcache_vget(ovp); 1518 KASSERT(error == ENOENT); 1519 mutex_enter(&vcache_lock); 1520 } 1521 SLIST_INSERT_HEAD(&vcache_hashtab[hash & vcache_hashmask], 1522 vip, vi_hash); 1523 mutex_exit(&vcache_lock); 1524 } 1525 vfs_insmntque(vp, mp); 1526 if ((mp->mnt_iflag & IMNT_MPSAFE) != 0) 1527 vp->v_vflag |= VV_MPSAFE; 1528 vfs_ref(mp); 1529 vfs_unbusy(mp); 1530 1531 /* Finished loading, finalize node. */ 1532 mutex_enter(&vcache_lock); 1533 mutex_enter(vp->v_interlock); 1534 VSTATE_CHANGE(vp, VS_LOADING, VS_LOADED); 1535 mutex_exit(&vcache_lock); 1536 mutex_exit(vp->v_interlock); 1537 *vpp = vp; 1538 return 0; 1539 } 1540 1541 /* 1542 * Prepare key change: update old cache nodes key and lock new cache node. 1543 * Return an error if the new node already exists. 1544 */ 1545 int 1546 vcache_rekey_enter(struct mount *mp, struct vnode *vp, 1547 const void *old_key, size_t old_key_len, 1548 const void *new_key, size_t new_key_len) 1549 { 1550 uint32_t old_hash, new_hash; 1551 struct vcache_key old_vcache_key, new_vcache_key; 1552 vnode_impl_t *vip, *new_vip; 1553 1554 old_vcache_key.vk_mount = mp; 1555 old_vcache_key.vk_key = old_key; 1556 old_vcache_key.vk_key_len = old_key_len; 1557 old_hash = vcache_hash(&old_vcache_key); 1558 1559 new_vcache_key.vk_mount = mp; 1560 new_vcache_key.vk_key = new_key; 1561 new_vcache_key.vk_key_len = new_key_len; 1562 new_hash = vcache_hash(&new_vcache_key); 1563 1564 new_vip = vcache_alloc(); 1565 new_vip->vi_key = new_vcache_key; 1566 1567 /* Insert locked new node used as placeholder. */ 1568 mutex_enter(&vcache_lock); 1569 vip = vcache_hash_lookup(&new_vcache_key, new_hash); 1570 if (vip != NULL) { 1571 vcache_dealloc(new_vip); 1572 return EEXIST; 1573 } 1574 SLIST_INSERT_HEAD(&vcache_hashtab[new_hash & vcache_hashmask], 1575 new_vip, vi_hash); 1576 1577 /* Replace old nodes key with the temporary copy. */ 1578 vip = vcache_hash_lookup(&old_vcache_key, old_hash); 1579 KASSERT(vip != NULL); 1580 KASSERT(VIMPL_TO_VNODE(vip) == vp); 1581 KASSERT(vip->vi_key.vk_key != old_vcache_key.vk_key); 1582 vip->vi_key = old_vcache_key; 1583 mutex_exit(&vcache_lock); 1584 return 0; 1585 } 1586 1587 /* 1588 * Key change complete: update old node and remove placeholder. 1589 */ 1590 void 1591 vcache_rekey_exit(struct mount *mp, struct vnode *vp, 1592 const void *old_key, size_t old_key_len, 1593 const void *new_key, size_t new_key_len) 1594 { 1595 uint32_t old_hash, new_hash; 1596 struct vcache_key old_vcache_key, new_vcache_key; 1597 vnode_impl_t *vip, *new_vip; 1598 struct vnode *new_vp; 1599 1600 old_vcache_key.vk_mount = mp; 1601 old_vcache_key.vk_key = old_key; 1602 old_vcache_key.vk_key_len = old_key_len; 1603 old_hash = vcache_hash(&old_vcache_key); 1604 1605 new_vcache_key.vk_mount = mp; 1606 new_vcache_key.vk_key = new_key; 1607 new_vcache_key.vk_key_len = new_key_len; 1608 new_hash = vcache_hash(&new_vcache_key); 1609 1610 mutex_enter(&vcache_lock); 1611 1612 /* Lookup old and new node. */ 1613 vip = vcache_hash_lookup(&old_vcache_key, old_hash); 1614 KASSERT(vip != NULL); 1615 KASSERT(VIMPL_TO_VNODE(vip) == vp); 1616 1617 new_vip = vcache_hash_lookup(&new_vcache_key, new_hash); 1618 KASSERT(new_vip != NULL); 1619 KASSERT(new_vip->vi_key.vk_key_len == new_key_len); 1620 new_vp = VIMPL_TO_VNODE(new_vip); 1621 mutex_enter(new_vp->v_interlock); 1622 VSTATE_ASSERT(VIMPL_TO_VNODE(new_vip), VS_LOADING); 1623 mutex_exit(new_vp->v_interlock); 1624 1625 /* Rekey old node and put it onto its new hashlist. */ 1626 vip->vi_key = new_vcache_key; 1627 if (old_hash != new_hash) { 1628 SLIST_REMOVE(&vcache_hashtab[old_hash & vcache_hashmask], 1629 vip, vnode_impl, vi_hash); 1630 SLIST_INSERT_HEAD(&vcache_hashtab[new_hash & vcache_hashmask], 1631 vip, vi_hash); 1632 } 1633 1634 /* Remove new node used as placeholder. */ 1635 SLIST_REMOVE(&vcache_hashtab[new_hash & vcache_hashmask], 1636 new_vip, vnode_impl, vi_hash); 1637 vcache_dealloc(new_vip); 1638 } 1639 1640 /* 1641 * Disassociate the underlying file system from a vnode. 1642 * 1643 * Must be called with vnode locked and will return unlocked. 1644 * Must be called with the interlock held, and will return with it held. 1645 */ 1646 static void 1647 vcache_reclaim(vnode_t *vp) 1648 { 1649 lwp_t *l = curlwp; 1650 vnode_impl_t *vip = VNODE_TO_VIMPL(vp); 1651 struct mount *mp = vp->v_mount; 1652 uint32_t hash; 1653 uint8_t temp_buf[64], *temp_key; 1654 size_t temp_key_len; 1655 bool recycle, active; 1656 int error; 1657 1658 KASSERT((vp->v_vflag & VV_LOCKSWORK) == 0 || 1659 VOP_ISLOCKED(vp) == LK_EXCLUSIVE); 1660 KASSERT(mutex_owned(vp->v_interlock)); 1661 KASSERT(vp->v_usecount != 0); 1662 1663 active = (vp->v_usecount > 1); 1664 temp_key_len = vip->vi_key.vk_key_len; 1665 /* 1666 * Prevent the vnode from being recycled or brought into use 1667 * while we clean it out. 1668 */ 1669 VSTATE_CHANGE(vp, VS_LOADED, VS_RECLAIMING); 1670 mutex_exit(vp->v_interlock); 1671 1672 rw_enter(vp->v_uobj.vmobjlock, RW_WRITER); 1673 mutex_enter(vp->v_interlock); 1674 if ((vp->v_iflag & VI_EXECMAP) != 0 && vp->v_uobj.uo_npages != 0) { 1675 cpu_count(CPU_COUNT_EXECPAGES, -vp->v_uobj.uo_npages); 1676 cpu_count(CPU_COUNT_FILEPAGES, vp->v_uobj.uo_npages); 1677 } 1678 vp->v_iflag &= ~(VI_TEXT|VI_EXECMAP); 1679 vp->v_iflag |= VI_DEADCHECK; /* for genfs_getpages() */ 1680 mutex_exit(vp->v_interlock); 1681 rw_exit(vp->v_uobj.vmobjlock); 1682 1683 /* 1684 * With vnode state set to reclaiming, purge name cache immediately 1685 * to prevent new handles on vnode, and wait for existing threads 1686 * trying to get a handle to notice VS_RECLAIMED status and abort. 1687 */ 1688 cache_purge(vp); 1689 1690 /* Replace the vnode key with a temporary copy. */ 1691 if (vip->vi_key.vk_key_len > sizeof(temp_buf)) { 1692 temp_key = kmem_alloc(temp_key_len, KM_SLEEP); 1693 } else { 1694 temp_key = temp_buf; 1695 } 1696 if (vip->vi_key.vk_key_len > 0) { 1697 mutex_enter(&vcache_lock); 1698 memcpy(temp_key, vip->vi_key.vk_key, temp_key_len); 1699 vip->vi_key.vk_key = temp_key; 1700 mutex_exit(&vcache_lock); 1701 } 1702 1703 fstrans_start(mp); 1704 1705 /* 1706 * Clean out any cached data associated with the vnode. 1707 * If purging an active vnode, it must be closed and 1708 * deactivated before being reclaimed. 1709 */ 1710 error = vinvalbuf(vp, V_SAVE, NOCRED, l, 0, 0); 1711 if (error != 0) { 1712 if (wapbl_vphaswapbl(vp)) 1713 WAPBL_DISCARD(wapbl_vptomp(vp)); 1714 error = vinvalbuf(vp, 0, NOCRED, l, 0, 0); 1715 } 1716 KASSERTMSG((error == 0), "vinvalbuf failed: %d", error); 1717 KASSERT((vp->v_iflag & VI_ONWORKLST) == 0); 1718 if (active && (vp->v_type == VBLK || vp->v_type == VCHR)) { 1719 spec_node_revoke(vp); 1720 } 1721 1722 /* 1723 * Disassociate the underlying file system from the vnode. 1724 * VOP_INACTIVE leaves the vnode locked; VOP_RECLAIM unlocks 1725 * the vnode, and may destroy the vnode so that VOP_UNLOCK 1726 * would no longer function. 1727 */ 1728 VOP_INACTIVE(vp, &recycle); 1729 KASSERT((vp->v_vflag & VV_LOCKSWORK) == 0 || 1730 VOP_ISLOCKED(vp) == LK_EXCLUSIVE); 1731 if (VOP_RECLAIM(vp)) { 1732 vnpanic(vp, "%s: cannot reclaim", __func__); 1733 } 1734 1735 KASSERT(vp->v_data == NULL); 1736 KASSERT((vp->v_iflag & VI_PAGES) == 0); 1737 1738 if (vp->v_type == VREG && vp->v_ractx != NULL) { 1739 uvm_ra_freectx(vp->v_ractx); 1740 vp->v_ractx = NULL; 1741 } 1742 1743 if (vip->vi_key.vk_key_len > 0) { 1744 /* Remove from vnode cache. */ 1745 hash = vcache_hash(&vip->vi_key); 1746 mutex_enter(&vcache_lock); 1747 KASSERT(vip == vcache_hash_lookup(&vip->vi_key, hash)); 1748 SLIST_REMOVE(&vcache_hashtab[hash & vcache_hashmask], 1749 vip, vnode_impl, vi_hash); 1750 mutex_exit(&vcache_lock); 1751 } 1752 if (temp_key != temp_buf) 1753 kmem_free(temp_key, temp_key_len); 1754 1755 /* Done with purge, notify sleepers of the grim news. */ 1756 mutex_enter(vp->v_interlock); 1757 vp->v_op = dead_vnodeop_p; 1758 vp->v_vflag |= VV_LOCKSWORK; 1759 VSTATE_CHANGE(vp, VS_RECLAIMING, VS_RECLAIMED); 1760 vp->v_tag = VT_NON; 1761 KNOTE(&vp->v_klist, NOTE_REVOKE); 1762 mutex_exit(vp->v_interlock); 1763 1764 /* 1765 * Move to dead mount. Must be after changing the operations 1766 * vector as vnode operations enter the mount before using the 1767 * operations vector. See sys/kern/vnode_if.c. 1768 */ 1769 vp->v_vflag &= ~VV_ROOT; 1770 vfs_ref(dead_rootmount); 1771 vfs_insmntque(vp, dead_rootmount); 1772 1773 #ifdef PAX_SEGVGUARD 1774 pax_segvguard_cleanup(vp); 1775 #endif /* PAX_SEGVGUARD */ 1776 1777 mutex_enter(vp->v_interlock); 1778 fstrans_done(mp); 1779 KASSERT((vp->v_iflag & VI_ONWORKLST) == 0); 1780 } 1781 1782 /* 1783 * Disassociate the underlying file system from an open device vnode 1784 * and make it anonymous. 1785 * 1786 * Vnode unlocked on entry, drops a reference to the vnode. 1787 */ 1788 void 1789 vcache_make_anon(vnode_t *vp) 1790 { 1791 vnode_impl_t *vip = VNODE_TO_VIMPL(vp); 1792 uint32_t hash; 1793 bool recycle; 1794 1795 KASSERT(vp->v_type == VBLK || vp->v_type == VCHR); 1796 KASSERT(vp->v_mount == dead_rootmount || fstrans_is_owner(vp->v_mount)); 1797 VSTATE_ASSERT_UNLOCKED(vp, VS_ACTIVE); 1798 1799 /* Remove from vnode cache. */ 1800 hash = vcache_hash(&vip->vi_key); 1801 mutex_enter(&vcache_lock); 1802 KASSERT(vip == vcache_hash_lookup(&vip->vi_key, hash)); 1803 SLIST_REMOVE(&vcache_hashtab[hash & vcache_hashmask], 1804 vip, vnode_impl, vi_hash); 1805 vip->vi_key.vk_mount = dead_rootmount; 1806 vip->vi_key.vk_key_len = 0; 1807 vip->vi_key.vk_key = NULL; 1808 mutex_exit(&vcache_lock); 1809 1810 /* 1811 * Disassociate the underlying file system from the vnode. 1812 * VOP_INACTIVE leaves the vnode locked; VOP_RECLAIM unlocks 1813 * the vnode, and may destroy the vnode so that VOP_UNLOCK 1814 * would no longer function. 1815 */ 1816 if (vn_lock(vp, LK_EXCLUSIVE)) { 1817 vnpanic(vp, "%s: cannot lock", __func__); 1818 } 1819 VOP_INACTIVE(vp, &recycle); 1820 KASSERT((vp->v_vflag & VV_LOCKSWORK) == 0 || 1821 VOP_ISLOCKED(vp) == LK_EXCLUSIVE); 1822 if (VOP_RECLAIM(vp)) { 1823 vnpanic(vp, "%s: cannot reclaim", __func__); 1824 } 1825 1826 /* Purge name cache. */ 1827 cache_purge(vp); 1828 1829 /* Done with purge, change operations vector. */ 1830 mutex_enter(vp->v_interlock); 1831 vp->v_op = spec_vnodeop_p; 1832 vp->v_vflag |= VV_MPSAFE; 1833 vp->v_vflag &= ~VV_LOCKSWORK; 1834 mutex_exit(vp->v_interlock); 1835 1836 /* 1837 * Move to dead mount. Must be after changing the operations 1838 * vector as vnode operations enter the mount before using the 1839 * operations vector. See sys/kern/vnode_if.c. 1840 */ 1841 vfs_ref(dead_rootmount); 1842 vfs_insmntque(vp, dead_rootmount); 1843 1844 vrele(vp); 1845 } 1846 1847 /* 1848 * Update outstanding I/O count and do wakeup if requested. 1849 */ 1850 void 1851 vwakeup(struct buf *bp) 1852 { 1853 vnode_t *vp; 1854 1855 if ((vp = bp->b_vp) == NULL) 1856 return; 1857 1858 KASSERT(bp->b_objlock == vp->v_interlock); 1859 KASSERT(mutex_owned(bp->b_objlock)); 1860 1861 if (--vp->v_numoutput < 0) 1862 vnpanic(vp, "%s: neg numoutput, vp %p", __func__, vp); 1863 if (vp->v_numoutput == 0) 1864 cv_broadcast(&vp->v_cv); 1865 } 1866 1867 /* 1868 * Test a vnode for being or becoming dead. Returns one of: 1869 * EBUSY: vnode is becoming dead, with "flags == VDEAD_NOWAIT" only. 1870 * ENOENT: vnode is dead. 1871 * 0: otherwise. 1872 * 1873 * Whenever this function returns a non-zero value all future 1874 * calls will also return a non-zero value. 1875 */ 1876 int 1877 vdead_check(struct vnode *vp, int flags) 1878 { 1879 1880 KASSERT(mutex_owned(vp->v_interlock)); 1881 1882 if (! ISSET(flags, VDEAD_NOWAIT)) 1883 VSTATE_WAIT_STABLE(vp); 1884 1885 if (VSTATE_GET(vp) == VS_RECLAIMING) { 1886 KASSERT(ISSET(flags, VDEAD_NOWAIT)); 1887 return EBUSY; 1888 } else if (VSTATE_GET(vp) == VS_RECLAIMED) { 1889 return ENOENT; 1890 } 1891 1892 return 0; 1893 } 1894 1895 int 1896 vfs_drainvnodes(void) 1897 { 1898 int i, gen; 1899 1900 mutex_enter(&vdrain_lock); 1901 for (i = 0; i < 2; i++) { 1902 gen = vdrain_gen; 1903 while (gen == vdrain_gen) { 1904 cv_broadcast(&vdrain_cv); 1905 cv_wait(&vdrain_gen_cv, &vdrain_lock); 1906 } 1907 } 1908 mutex_exit(&vdrain_lock); 1909 1910 if (numvnodes >= desiredvnodes) 1911 return EBUSY; 1912 1913 if (vcache_hashsize != desiredvnodes) 1914 vcache_reinit(); 1915 1916 return 0; 1917 } 1918 1919 void 1920 vnpanic(vnode_t *vp, const char *fmt, ...) 1921 { 1922 va_list ap; 1923 1924 #ifdef DIAGNOSTIC 1925 vprint(NULL, vp); 1926 #endif 1927 va_start(ap, fmt); 1928 vpanic(fmt, ap); 1929 va_end(ap); 1930 } 1931 1932 void 1933 vshareilock(vnode_t *tvp, vnode_t *fvp) 1934 { 1935 kmutex_t *oldlock; 1936 1937 oldlock = tvp->v_interlock; 1938 mutex_obj_hold(fvp->v_interlock); 1939 tvp->v_interlock = fvp->v_interlock; 1940 mutex_obj_free(oldlock); 1941 } 1942