1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright (c) 2011, 2019 by Delphix. All rights reserved. 24 * Copyright 2015 Nexenta Systems, Inc. All rights reserved. 25 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. 26 * Copyright 2013 Saso Kiselkov. All rights reserved. 27 * Copyright (c) 2014 Integros [integros.com] 28 * Copyright (c) 2017 Datto Inc. 29 * Copyright 2019 Joyent, Inc. 30 * Copyright (c) 2017, Intel Corporation. 31 * Copyright 2020 Joyent, Inc. 32 */ 33 34 #include <sys/zfs_context.h> 35 #include <sys/spa_impl.h> 36 #include <sys/spa_boot.h> 37 #include <sys/zio.h> 38 #include <sys/zio_checksum.h> 39 #include <sys/zio_compress.h> 40 #include <sys/dmu.h> 41 #include <sys/dmu_tx.h> 42 #include <sys/zap.h> 43 #include <sys/zil.h> 44 #include <sys/vdev_impl.h> 45 #include <sys/vdev_initialize.h> 46 #include <sys/vdev_trim.h> 47 #include <sys/vdev_raidz.h> 48 #include <sys/metaslab.h> 49 #include <sys/uberblock_impl.h> 50 #include <sys/txg.h> 51 #include <sys/avl.h> 52 #include <sys/unique.h> 53 #include <sys/dsl_pool.h> 54 #include <sys/dsl_dir.h> 55 #include <sys/dsl_prop.h> 56 #include <sys/dsl_scan.h> 57 #include <sys/fs/zfs.h> 58 #include <sys/metaslab_impl.h> 59 #include <sys/arc.h> 60 #include <sys/ddt.h> 61 #include "zfs_prop.h" 62 #include <sys/btree.h> 63 #include <sys/zfeature.h> 64 65 /* 66 * SPA locking 67 * 68 * There are three basic locks for managing spa_t structures: 69 * 70 * spa_namespace_lock (global mutex) 71 * 72 * This lock must be acquired to do any of the following: 73 * 74 * - Lookup a spa_t by name 75 * - Add or remove a spa_t from the namespace 76 * - Increase spa_refcount from non-zero 77 * - Check if spa_refcount is zero 78 * - Rename a spa_t 79 * - add/remove/attach/detach devices 80 * - Held for the duration of create/destroy/import/export 81 * 82 * It does not need to handle recursion. A create or destroy may 83 * reference objects (files or zvols) in other pools, but by 84 * definition they must have an existing reference, and will never need 85 * to lookup a spa_t by name. 86 * 87 * spa_refcount (per-spa zfs_refcount_t protected by mutex) 88 * 89 * This reference count keep track of any active users of the spa_t. The 90 * spa_t cannot be destroyed or freed while this is non-zero. Internally, 91 * the refcount is never really 'zero' - opening a pool implicitly keeps 92 * some references in the DMU. Internally we check against spa_minref, but 93 * present the image of a zero/non-zero value to consumers. 94 * 95 * spa_config_lock[] (per-spa array of rwlocks) 96 * 97 * This protects the spa_t from config changes, and must be held in 98 * the following circumstances: 99 * 100 * - RW_READER to perform I/O to the spa 101 * - RW_WRITER to change the vdev config 102 * 103 * The locking order is fairly straightforward: 104 * 105 * spa_namespace_lock -> spa_refcount 106 * 107 * The namespace lock must be acquired to increase the refcount from 0 108 * or to check if it is zero. 109 * 110 * spa_refcount -> spa_config_lock[] 111 * 112 * There must be at least one valid reference on the spa_t to acquire 113 * the config lock. 114 * 115 * spa_namespace_lock -> spa_config_lock[] 116 * 117 * The namespace lock must always be taken before the config lock. 118 * 119 * 120 * The spa_namespace_lock can be acquired directly and is globally visible. 121 * 122 * The namespace is manipulated using the following functions, all of which 123 * require the spa_namespace_lock to be held. 124 * 125 * spa_lookup() Lookup a spa_t by name. 126 * 127 * spa_add() Create a new spa_t in the namespace. 128 * 129 * spa_remove() Remove a spa_t from the namespace. This also 130 * frees up any memory associated with the spa_t. 131 * 132 * spa_next() Returns the next spa_t in the system, or the 133 * first if NULL is passed. 134 * 135 * spa_evict_all() Shutdown and remove all spa_t structures in 136 * the system. 137 * 138 * spa_guid_exists() Determine whether a pool/device guid exists. 139 * 140 * The spa_refcount is manipulated using the following functions: 141 * 142 * spa_open_ref() Adds a reference to the given spa_t. Must be 143 * called with spa_namespace_lock held if the 144 * refcount is currently zero. 145 * 146 * spa_close() Remove a reference from the spa_t. This will 147 * not free the spa_t or remove it from the 148 * namespace. No locking is required. 149 * 150 * spa_refcount_zero() Returns true if the refcount is currently 151 * zero. Must be called with spa_namespace_lock 152 * held. 153 * 154 * The spa_config_lock[] is an array of rwlocks, ordered as follows: 155 * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV. 156 * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}(). 157 * 158 * To read the configuration, it suffices to hold one of these locks as reader. 159 * To modify the configuration, you must hold all locks as writer. To modify 160 * vdev state without altering the vdev tree's topology (e.g. online/offline), 161 * you must hold SCL_STATE and SCL_ZIO as writer. 162 * 163 * We use these distinct config locks to avoid recursive lock entry. 164 * For example, spa_sync() (which holds SCL_CONFIG as reader) induces 165 * block allocations (SCL_ALLOC), which may require reading space maps 166 * from disk (dmu_read() -> zio_read() -> SCL_ZIO). 167 * 168 * The spa config locks cannot be normal rwlocks because we need the 169 * ability to hand off ownership. For example, SCL_ZIO is acquired 170 * by the issuing thread and later released by an interrupt thread. 171 * They do, however, obey the usual write-wanted semantics to prevent 172 * writer (i.e. system administrator) starvation. 173 * 174 * The lock acquisition rules are as follows: 175 * 176 * SCL_CONFIG 177 * Protects changes to the vdev tree topology, such as vdev 178 * add/remove/attach/detach. Protects the dirty config list 179 * (spa_config_dirty_list) and the set of spares and l2arc devices. 180 * 181 * SCL_STATE 182 * Protects changes to pool state and vdev state, such as vdev 183 * online/offline/fault/degrade/clear. Protects the dirty state list 184 * (spa_state_dirty_list) and global pool state (spa_state). 185 * 186 * SCL_ALLOC 187 * Protects changes to metaslab groups and classes. 188 * Held as reader by metaslab_alloc() and metaslab_claim(). 189 * 190 * SCL_ZIO 191 * Held by bp-level zios (those which have no io_vd upon entry) 192 * to prevent changes to the vdev tree. The bp-level zio implicitly 193 * protects all of its vdev child zios, which do not hold SCL_ZIO. 194 * 195 * SCL_FREE 196 * Protects changes to metaslab groups and classes. 197 * Held as reader by metaslab_free(). SCL_FREE is distinct from 198 * SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free 199 * blocks in zio_done() while another i/o that holds either 200 * SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete. 201 * 202 * SCL_VDEV 203 * Held as reader to prevent changes to the vdev tree during trivial 204 * inquiries such as bp_get_dsize(). SCL_VDEV is distinct from the 205 * other locks, and lower than all of them, to ensure that it's safe 206 * to acquire regardless of caller context. 207 * 208 * In addition, the following rules apply: 209 * 210 * (a) spa_props_lock protects pool properties, spa_config and spa_config_list. 211 * The lock ordering is SCL_CONFIG > spa_props_lock. 212 * 213 * (b) I/O operations on leaf vdevs. For any zio operation that takes 214 * an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(), 215 * or zio_write_phys() -- the caller must ensure that the config cannot 216 * cannot change in the interim, and that the vdev cannot be reopened. 217 * SCL_STATE as reader suffices for both. 218 * 219 * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit(). 220 * 221 * spa_vdev_enter() Acquire the namespace lock and the config lock 222 * for writing. 223 * 224 * spa_vdev_exit() Release the config lock, wait for all I/O 225 * to complete, sync the updated configs to the 226 * cache, and release the namespace lock. 227 * 228 * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit(). 229 * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual 230 * locking is, always, based on spa_namespace_lock and spa_config_lock[]. 231 */ 232 233 static avl_tree_t spa_namespace_avl; 234 kmutex_t spa_namespace_lock; 235 static kcondvar_t spa_namespace_cv; 236 static int spa_active_count; 237 int spa_max_replication_override = SPA_DVAS_PER_BP; 238 239 static kmutex_t spa_spare_lock; 240 static avl_tree_t spa_spare_avl; 241 static kmutex_t spa_l2cache_lock; 242 static avl_tree_t spa_l2cache_avl; 243 244 kmem_cache_t *spa_buffer_pool; 245 int spa_mode_global; 246 247 #ifdef ZFS_DEBUG 248 /* 249 * Everything except dprintf, spa, and indirect_remap is on by default 250 * in debug builds. 251 */ 252 int zfs_flags = ~(ZFS_DEBUG_DPRINTF | ZFS_DEBUG_INDIRECT_REMAP); 253 #else 254 int zfs_flags = 0; 255 #endif 256 257 /* 258 * zfs_recover can be set to nonzero to attempt to recover from 259 * otherwise-fatal errors, typically caused by on-disk corruption. When 260 * set, calls to zfs_panic_recover() will turn into warning messages. 261 * This should only be used as a last resort, as it typically results 262 * in leaked space, or worse. 263 */ 264 boolean_t zfs_recover = B_FALSE; 265 266 /* 267 * If destroy encounters an EIO while reading metadata (e.g. indirect 268 * blocks), space referenced by the missing metadata can not be freed. 269 * Normally this causes the background destroy to become "stalled", as 270 * it is unable to make forward progress. While in this stalled state, 271 * all remaining space to free from the error-encountering filesystem is 272 * "temporarily leaked". Set this flag to cause it to ignore the EIO, 273 * permanently leak the space from indirect blocks that can not be read, 274 * and continue to free everything else that it can. 275 * 276 * The default, "stalling" behavior is useful if the storage partially 277 * fails (i.e. some but not all i/os fail), and then later recovers. In 278 * this case, we will be able to continue pool operations while it is 279 * partially failed, and when it recovers, we can continue to free the 280 * space, with no leaks. However, note that this case is actually 281 * fairly rare. 282 * 283 * Typically pools either (a) fail completely (but perhaps temporarily, 284 * e.g. a top-level vdev going offline), or (b) have localized, 285 * permanent errors (e.g. disk returns the wrong data due to bit flip or 286 * firmware bug). In case (a), this setting does not matter because the 287 * pool will be suspended and the sync thread will not be able to make 288 * forward progress regardless. In case (b), because the error is 289 * permanent, the best we can do is leak the minimum amount of space, 290 * which is what setting this flag will do. Therefore, it is reasonable 291 * for this flag to normally be set, but we chose the more conservative 292 * approach of not setting it, so that there is no possibility of 293 * leaking space in the "partial temporary" failure case. 294 */ 295 boolean_t zfs_free_leak_on_eio = B_FALSE; 296 297 /* 298 * Expiration time in milliseconds. This value has two meanings. First it is 299 * used to determine when the spa_deadman() logic should fire. By default the 300 * spa_deadman() will fire if spa_sync() has not completed in 1000 seconds. 301 * Secondly, the value determines if an I/O is considered "hung". Any I/O that 302 * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting 303 * in a system panic. 304 */ 305 uint64_t zfs_deadman_synctime_ms = 1000000ULL; 306 307 /* 308 * Check time in milliseconds. This defines the frequency at which we check 309 * for hung I/O. 310 */ 311 uint64_t zfs_deadman_checktime_ms = 5000ULL; 312 313 /* 314 * Override the zfs deadman behavior via /etc/system. By default the 315 * deadman is enabled except on VMware and sparc deployments. 316 */ 317 int zfs_deadman_enabled = -1; 318 319 #if defined(__amd64__) || defined(__i386__) 320 /* 321 * Should we allow the use of mechanisms that depend on saving and restoring 322 * the FPU state? This was disabled initially due to stability issues in 323 * the kernel FPU routines; see bug 13717. As of the fixes for 13902 and 324 * 13915, it has once again been enabled. 325 */ 326 int zfs_fpu_enabled = 1; 327 #endif 328 329 /* 330 * The worst case is single-sector max-parity RAID-Z blocks, in which 331 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1) 332 * times the size; so just assume that. Add to this the fact that 333 * we can have up to 3 DVAs per bp, and one more factor of 2 because 334 * the block may be dittoed with up to 3 DVAs by ddt_sync(). All together, 335 * the worst case is: 336 * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24 337 */ 338 int spa_asize_inflation = 24; 339 340 /* 341 * Normally, we don't allow the last 3.2% (1/(2^spa_slop_shift)) of space in 342 * the pool to be consumed. This ensures that we don't run the pool 343 * completely out of space, due to unaccounted changes (e.g. to the MOS). 344 * It also limits the worst-case time to allocate space. If we have 345 * less than this amount of free space, most ZPL operations (e.g. write, 346 * create) will return ENOSPC. 347 * 348 * Certain operations (e.g. file removal, most administrative actions) can 349 * use half the slop space. They will only return ENOSPC if less than half 350 * the slop space is free. Typically, once the pool has less than the slop 351 * space free, the user will use these operations to free up space in the pool. 352 * These are the operations that call dsl_pool_adjustedsize() with the netfree 353 * argument set to TRUE. 354 * 355 * Operations that are almost guaranteed to free up space in the absence of 356 * a pool checkpoint can use up to three quarters of the slop space 357 * (e.g zfs destroy). 358 * 359 * A very restricted set of operations are always permitted, regardless of 360 * the amount of free space. These are the operations that call 361 * dsl_sync_task(ZFS_SPACE_CHECK_NONE). If these operations result in a net 362 * increase in the amount of space used, it is possible to run the pool 363 * completely out of space, causing it to be permanently read-only. 364 * 365 * Note that on very small pools, the slop space will be larger than 366 * 3.2%, in an effort to have it be at least spa_min_slop (128MB), 367 * but we never allow it to be more than half the pool size. 368 * 369 * See also the comments in zfs_space_check_t. 370 */ 371 int spa_slop_shift = 5; 372 uint64_t spa_min_slop = 128 * 1024 * 1024; 373 374 int spa_allocators = 4; 375 376 /*PRINTFLIKE2*/ 377 void 378 spa_load_failed(spa_t *spa, const char *fmt, ...) 379 { 380 va_list adx; 381 char buf[256]; 382 383 va_start(adx, fmt); 384 (void) vsnprintf(buf, sizeof (buf), fmt, adx); 385 va_end(adx); 386 387 zfs_dbgmsg("spa_load(%s, config %s): FAILED: %s", spa->spa_name, 388 spa->spa_trust_config ? "trusted" : "untrusted", buf); 389 } 390 391 /*PRINTFLIKE2*/ 392 void 393 spa_load_note(spa_t *spa, const char *fmt, ...) 394 { 395 va_list adx; 396 char buf[256]; 397 398 va_start(adx, fmt); 399 (void) vsnprintf(buf, sizeof (buf), fmt, adx); 400 va_end(adx); 401 402 zfs_dbgmsg("spa_load(%s, config %s): %s", spa->spa_name, 403 spa->spa_trust_config ? "trusted" : "untrusted", buf); 404 } 405 406 /* 407 * By default dedup and user data indirects land in the special class 408 */ 409 int zfs_ddt_data_is_special = B_TRUE; 410 int zfs_user_indirect_is_special = B_TRUE; 411 412 /* 413 * The percentage of special class final space reserved for metadata only. 414 * Once we allocate 100 - zfs_special_class_metadata_reserve_pct we only 415 * let metadata into the class. 416 */ 417 int zfs_special_class_metadata_reserve_pct = 25; 418 419 /* 420 * ========================================================================== 421 * SPA config locking 422 * ========================================================================== 423 */ 424 static void 425 spa_config_lock_init(spa_t *spa) 426 { 427 for (int i = 0; i < SCL_LOCKS; i++) { 428 spa_config_lock_t *scl = &spa->spa_config_lock[i]; 429 mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL); 430 cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL); 431 zfs_refcount_create_untracked(&scl->scl_count); 432 scl->scl_writer = NULL; 433 scl->scl_write_wanted = 0; 434 } 435 } 436 437 static void 438 spa_config_lock_destroy(spa_t *spa) 439 { 440 for (int i = 0; i < SCL_LOCKS; i++) { 441 spa_config_lock_t *scl = &spa->spa_config_lock[i]; 442 mutex_destroy(&scl->scl_lock); 443 cv_destroy(&scl->scl_cv); 444 zfs_refcount_destroy(&scl->scl_count); 445 ASSERT(scl->scl_writer == NULL); 446 ASSERT(scl->scl_write_wanted == 0); 447 } 448 } 449 450 int 451 spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw) 452 { 453 for (int i = 0; i < SCL_LOCKS; i++) { 454 spa_config_lock_t *scl = &spa->spa_config_lock[i]; 455 if (!(locks & (1 << i))) 456 continue; 457 mutex_enter(&scl->scl_lock); 458 if (rw == RW_READER) { 459 if (scl->scl_writer || scl->scl_write_wanted) { 460 mutex_exit(&scl->scl_lock); 461 spa_config_exit(spa, locks & ((1 << i) - 1), 462 tag); 463 return (0); 464 } 465 } else { 466 ASSERT(scl->scl_writer != curthread); 467 if (!zfs_refcount_is_zero(&scl->scl_count)) { 468 mutex_exit(&scl->scl_lock); 469 spa_config_exit(spa, locks & ((1 << i) - 1), 470 tag); 471 return (0); 472 } 473 scl->scl_writer = curthread; 474 } 475 (void) zfs_refcount_add(&scl->scl_count, tag); 476 mutex_exit(&scl->scl_lock); 477 } 478 return (1); 479 } 480 481 void 482 spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw) 483 { 484 int wlocks_held = 0; 485 486 ASSERT3U(SCL_LOCKS, <, sizeof (wlocks_held) * NBBY); 487 488 for (int i = 0; i < SCL_LOCKS; i++) { 489 spa_config_lock_t *scl = &spa->spa_config_lock[i]; 490 if (scl->scl_writer == curthread) 491 wlocks_held |= (1 << i); 492 if (!(locks & (1 << i))) 493 continue; 494 mutex_enter(&scl->scl_lock); 495 if (rw == RW_READER) { 496 while (scl->scl_writer || scl->scl_write_wanted) { 497 cv_wait(&scl->scl_cv, &scl->scl_lock); 498 } 499 } else { 500 ASSERT(scl->scl_writer != curthread); 501 while (!zfs_refcount_is_zero(&scl->scl_count)) { 502 scl->scl_write_wanted++; 503 cv_wait(&scl->scl_cv, &scl->scl_lock); 504 scl->scl_write_wanted--; 505 } 506 scl->scl_writer = curthread; 507 } 508 (void) zfs_refcount_add(&scl->scl_count, tag); 509 mutex_exit(&scl->scl_lock); 510 } 511 ASSERT3U(wlocks_held, <=, locks); 512 } 513 514 void 515 spa_config_exit(spa_t *spa, int locks, void *tag) 516 { 517 for (int i = SCL_LOCKS - 1; i >= 0; i--) { 518 spa_config_lock_t *scl = &spa->spa_config_lock[i]; 519 if (!(locks & (1 << i))) 520 continue; 521 mutex_enter(&scl->scl_lock); 522 ASSERT(!zfs_refcount_is_zero(&scl->scl_count)); 523 if (zfs_refcount_remove(&scl->scl_count, tag) == 0) { 524 ASSERT(scl->scl_writer == NULL || 525 scl->scl_writer == curthread); 526 scl->scl_writer = NULL; /* OK in either case */ 527 cv_broadcast(&scl->scl_cv); 528 } 529 mutex_exit(&scl->scl_lock); 530 } 531 } 532 533 int 534 spa_config_held(spa_t *spa, int locks, krw_t rw) 535 { 536 int locks_held = 0; 537 538 for (int i = 0; i < SCL_LOCKS; i++) { 539 spa_config_lock_t *scl = &spa->spa_config_lock[i]; 540 if (!(locks & (1 << i))) 541 continue; 542 if ((rw == RW_READER && 543 !zfs_refcount_is_zero(&scl->scl_count)) || 544 (rw == RW_WRITER && scl->scl_writer == curthread)) 545 locks_held |= 1 << i; 546 } 547 548 return (locks_held); 549 } 550 551 /* 552 * ========================================================================== 553 * SPA namespace functions 554 * ========================================================================== 555 */ 556 557 /* 558 * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held. 559 * Returns NULL if no matching spa_t is found. 560 */ 561 spa_t * 562 spa_lookup(const char *name) 563 { 564 static spa_t search; /* spa_t is large; don't allocate on stack */ 565 spa_t *spa; 566 avl_index_t where; 567 char *cp; 568 569 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 570 571 (void) strlcpy(search.spa_name, name, sizeof (search.spa_name)); 572 573 /* 574 * If it's a full dataset name, figure out the pool name and 575 * just use that. 576 */ 577 cp = strpbrk(search.spa_name, "/@#"); 578 if (cp != NULL) 579 *cp = '\0'; 580 581 spa = avl_find(&spa_namespace_avl, &search, &where); 582 583 return (spa); 584 } 585 586 /* 587 * Fires when spa_sync has not completed within zfs_deadman_synctime_ms. 588 * If the zfs_deadman_enabled flag is set then it inspects all vdev queues 589 * looking for potentially hung I/Os. 590 */ 591 void 592 spa_deadman(void *arg) 593 { 594 spa_t *spa = arg; 595 596 /* 597 * Disable the deadman timer if the pool is suspended. 598 */ 599 if (spa_suspended(spa)) { 600 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY)); 601 return; 602 } 603 604 zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu", 605 (gethrtime() - spa->spa_sync_starttime) / NANOSEC, 606 ++spa->spa_deadman_calls); 607 if (zfs_deadman_enabled) 608 vdev_deadman(spa->spa_root_vdev); 609 } 610 611 int 612 spa_log_sm_sort_by_txg(const void *va, const void *vb) 613 { 614 const spa_log_sm_t *a = va; 615 const spa_log_sm_t *b = vb; 616 617 return (TREE_CMP(a->sls_txg, b->sls_txg)); 618 } 619 620 /* 621 * Create an uninitialized spa_t with the given name. Requires 622 * spa_namespace_lock. The caller must ensure that the spa_t doesn't already 623 * exist by calling spa_lookup() first. 624 */ 625 spa_t * 626 spa_add(const char *name, nvlist_t *config, const char *altroot) 627 { 628 spa_t *spa; 629 spa_config_dirent_t *dp; 630 cyc_handler_t hdlr; 631 cyc_time_t when; 632 633 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 634 635 spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP); 636 637 mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL); 638 mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL); 639 mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL); 640 mutex_init(&spa->spa_evicting_os_lock, NULL, MUTEX_DEFAULT, NULL); 641 mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL); 642 mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL); 643 mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL); 644 mutex_init(&spa->spa_cksum_tmpls_lock, NULL, MUTEX_DEFAULT, NULL); 645 mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL); 646 mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL); 647 mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL); 648 mutex_init(&spa->spa_iokstat_lock, NULL, MUTEX_DEFAULT, NULL); 649 mutex_init(&spa->spa_flushed_ms_lock, NULL, MUTEX_DEFAULT, NULL); 650 mutex_init(&spa->spa_imp_kstat_lock, NULL, MUTEX_DEFAULT, NULL); 651 652 cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL); 653 cv_init(&spa->spa_evicting_os_cv, NULL, CV_DEFAULT, NULL); 654 cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL); 655 cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL); 656 cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL); 657 658 for (int t = 0; t < TXG_SIZE; t++) 659 bplist_create(&spa->spa_free_bplist[t]); 660 661 (void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name)); 662 spa->spa_state = POOL_STATE_UNINITIALIZED; 663 spa->spa_freeze_txg = UINT64_MAX; 664 spa->spa_final_txg = UINT64_MAX; 665 spa->spa_load_max_txg = UINT64_MAX; 666 spa->spa_proc = &p0; 667 spa->spa_proc_state = SPA_PROC_NONE; 668 spa->spa_trust_config = B_TRUE; 669 670 hdlr.cyh_func = spa_deadman; 671 hdlr.cyh_arg = spa; 672 hdlr.cyh_level = CY_LOW_LEVEL; 673 674 spa->spa_deadman_synctime = MSEC2NSEC(zfs_deadman_synctime_ms); 675 676 /* 677 * This determines how often we need to check for hung I/Os after 678 * the cyclic has already fired. Since checking for hung I/Os is 679 * an expensive operation we don't want to check too frequently. 680 * Instead wait for 5 seconds before checking again. 681 */ 682 when.cyt_interval = MSEC2NSEC(zfs_deadman_checktime_ms); 683 when.cyt_when = CY_INFINITY; 684 mutex_enter(&cpu_lock); 685 spa->spa_deadman_cycid = cyclic_add(&hdlr, &when); 686 mutex_exit(&cpu_lock); 687 688 zfs_refcount_create(&spa->spa_refcount); 689 spa_config_lock_init(spa); 690 691 avl_add(&spa_namespace_avl, spa); 692 693 /* 694 * Set the alternate root, if there is one. 695 */ 696 if (altroot) { 697 spa->spa_root = spa_strdup(altroot); 698 spa_active_count++; 699 } 700 701 spa->spa_alloc_count = spa_allocators; 702 spa->spa_alloc_locks = kmem_zalloc(spa->spa_alloc_count * 703 sizeof (kmutex_t), KM_SLEEP); 704 spa->spa_alloc_trees = kmem_zalloc(spa->spa_alloc_count * 705 sizeof (avl_tree_t), KM_SLEEP); 706 for (int i = 0; i < spa->spa_alloc_count; i++) { 707 mutex_init(&spa->spa_alloc_locks[i], NULL, MUTEX_DEFAULT, NULL); 708 avl_create(&spa->spa_alloc_trees[i], zio_bookmark_compare, 709 sizeof (zio_t), offsetof(zio_t, io_alloc_node)); 710 } 711 avl_create(&spa->spa_metaslabs_by_flushed, metaslab_sort_by_flushed, 712 sizeof (metaslab_t), offsetof(metaslab_t, ms_spa_txg_node)); 713 avl_create(&spa->spa_sm_logs_by_txg, spa_log_sm_sort_by_txg, 714 sizeof (spa_log_sm_t), offsetof(spa_log_sm_t, sls_node)); 715 list_create(&spa->spa_log_summary, sizeof (log_summary_entry_t), 716 offsetof(log_summary_entry_t, lse_node)); 717 718 /* 719 * Every pool starts with the default cachefile 720 */ 721 list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t), 722 offsetof(spa_config_dirent_t, scd_link)); 723 724 dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP); 725 dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path); 726 list_insert_head(&spa->spa_config_list, dp); 727 728 VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME, 729 KM_SLEEP) == 0); 730 731 if (config != NULL) { 732 nvlist_t *features; 733 734 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ, 735 &features) == 0) { 736 VERIFY(nvlist_dup(features, &spa->spa_label_features, 737 0) == 0); 738 } 739 740 VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0); 741 } 742 743 if (spa->spa_label_features == NULL) { 744 VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME, 745 KM_SLEEP) == 0); 746 } 747 748 spa->spa_iokstat = kstat_create("zfs", 0, name, 749 "disk", KSTAT_TYPE_IO, 1, 0); 750 if (spa->spa_iokstat) { 751 spa->spa_iokstat->ks_lock = &spa->spa_iokstat_lock; 752 kstat_install(spa->spa_iokstat); 753 } 754 755 spa->spa_min_ashift = INT_MAX; 756 spa->spa_max_ashift = 0; 757 758 /* 759 * As a pool is being created, treat all features as disabled by 760 * setting SPA_FEATURE_DISABLED for all entries in the feature 761 * refcount cache. 762 */ 763 for (int i = 0; i < SPA_FEATURES; i++) { 764 spa->spa_feat_refcount_cache[i] = SPA_FEATURE_DISABLED; 765 } 766 767 list_create(&spa->spa_leaf_list, sizeof (vdev_t), 768 offsetof(vdev_t, vdev_leaf_node)); 769 770 return (spa); 771 } 772 773 /* 774 * Removes a spa_t from the namespace, freeing up any memory used. Requires 775 * spa_namespace_lock. This is called only after the spa_t has been closed and 776 * deactivated. 777 */ 778 void 779 spa_remove(spa_t *spa) 780 { 781 spa_config_dirent_t *dp; 782 783 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 784 ASSERT(spa_state(spa) == POOL_STATE_UNINITIALIZED); 785 ASSERT3U(zfs_refcount_count(&spa->spa_refcount), ==, 0); 786 787 nvlist_free(spa->spa_config_splitting); 788 789 avl_remove(&spa_namespace_avl, spa); 790 cv_broadcast(&spa_namespace_cv); 791 792 if (spa->spa_root) { 793 spa_strfree(spa->spa_root); 794 spa_active_count--; 795 } 796 797 while ((dp = list_head(&spa->spa_config_list)) != NULL) { 798 list_remove(&spa->spa_config_list, dp); 799 if (dp->scd_path != NULL) 800 spa_strfree(dp->scd_path); 801 kmem_free(dp, sizeof (spa_config_dirent_t)); 802 } 803 804 for (int i = 0; i < spa->spa_alloc_count; i++) { 805 avl_destroy(&spa->spa_alloc_trees[i]); 806 mutex_destroy(&spa->spa_alloc_locks[i]); 807 } 808 kmem_free(spa->spa_alloc_locks, spa->spa_alloc_count * 809 sizeof (kmutex_t)); 810 kmem_free(spa->spa_alloc_trees, spa->spa_alloc_count * 811 sizeof (avl_tree_t)); 812 813 avl_destroy(&spa->spa_metaslabs_by_flushed); 814 avl_destroy(&spa->spa_sm_logs_by_txg); 815 list_destroy(&spa->spa_log_summary); 816 list_destroy(&spa->spa_config_list); 817 list_destroy(&spa->spa_leaf_list); 818 819 nvlist_free(spa->spa_label_features); 820 nvlist_free(spa->spa_load_info); 821 spa_config_set(spa, NULL); 822 823 mutex_enter(&cpu_lock); 824 if (spa->spa_deadman_cycid != CYCLIC_NONE) 825 cyclic_remove(spa->spa_deadman_cycid); 826 mutex_exit(&cpu_lock); 827 spa->spa_deadman_cycid = CYCLIC_NONE; 828 829 zfs_refcount_destroy(&spa->spa_refcount); 830 831 spa_config_lock_destroy(spa); 832 833 kstat_delete(spa->spa_iokstat); 834 spa->spa_iokstat = NULL; 835 836 for (int t = 0; t < TXG_SIZE; t++) 837 bplist_destroy(&spa->spa_free_bplist[t]); 838 839 zio_checksum_templates_free(spa); 840 841 cv_destroy(&spa->spa_async_cv); 842 cv_destroy(&spa->spa_evicting_os_cv); 843 cv_destroy(&spa->spa_proc_cv); 844 cv_destroy(&spa->spa_scrub_io_cv); 845 cv_destroy(&spa->spa_suspend_cv); 846 847 mutex_destroy(&spa->spa_flushed_ms_lock); 848 mutex_destroy(&spa->spa_async_lock); 849 mutex_destroy(&spa->spa_errlist_lock); 850 mutex_destroy(&spa->spa_errlog_lock); 851 mutex_destroy(&spa->spa_evicting_os_lock); 852 mutex_destroy(&spa->spa_history_lock); 853 mutex_destroy(&spa->spa_proc_lock); 854 mutex_destroy(&spa->spa_props_lock); 855 mutex_destroy(&spa->spa_cksum_tmpls_lock); 856 mutex_destroy(&spa->spa_scrub_lock); 857 mutex_destroy(&spa->spa_suspend_lock); 858 mutex_destroy(&spa->spa_vdev_top_lock); 859 mutex_destroy(&spa->spa_iokstat_lock); 860 mutex_destroy(&spa->spa_imp_kstat_lock); 861 862 kmem_free(spa, sizeof (spa_t)); 863 } 864 865 /* 866 * Given a pool, return the next pool in the namespace, or NULL if there is 867 * none. If 'prev' is NULL, return the first pool. 868 */ 869 spa_t * 870 spa_next(spa_t *prev) 871 { 872 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 873 874 if (prev) 875 return (AVL_NEXT(&spa_namespace_avl, prev)); 876 else 877 return (avl_first(&spa_namespace_avl)); 878 } 879 880 /* 881 * ========================================================================== 882 * SPA refcount functions 883 * ========================================================================== 884 */ 885 886 /* 887 * Add a reference to the given spa_t. Must have at least one reference, or 888 * have the namespace lock held. 889 */ 890 void 891 spa_open_ref(spa_t *spa, void *tag) 892 { 893 ASSERT(zfs_refcount_count(&spa->spa_refcount) >= spa->spa_minref || 894 MUTEX_HELD(&spa_namespace_lock)); 895 (void) zfs_refcount_add(&spa->spa_refcount, tag); 896 } 897 898 /* 899 * Remove a reference to the given spa_t. Must have at least one reference, or 900 * have the namespace lock held. 901 */ 902 void 903 spa_close(spa_t *spa, void *tag) 904 { 905 ASSERT(zfs_refcount_count(&spa->spa_refcount) > spa->spa_minref || 906 MUTEX_HELD(&spa_namespace_lock)); 907 (void) zfs_refcount_remove(&spa->spa_refcount, tag); 908 } 909 910 /* 911 * Remove a reference to the given spa_t held by a dsl dir that is 912 * being asynchronously released. Async releases occur from a taskq 913 * performing eviction of dsl datasets and dirs. The namespace lock 914 * isn't held and the hold by the object being evicted may contribute to 915 * spa_minref (e.g. dataset or directory released during pool export), 916 * so the asserts in spa_close() do not apply. 917 */ 918 void 919 spa_async_close(spa_t *spa, void *tag) 920 { 921 (void) zfs_refcount_remove(&spa->spa_refcount, tag); 922 } 923 924 /* 925 * Check to see if the spa refcount is zero. Must be called with 926 * spa_namespace_lock held. We really compare against spa_minref, which is the 927 * number of references acquired when opening a pool 928 */ 929 boolean_t 930 spa_refcount_zero(spa_t *spa) 931 { 932 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 933 934 return (zfs_refcount_count(&spa->spa_refcount) == spa->spa_minref); 935 } 936 937 /* 938 * ========================================================================== 939 * SPA spare and l2cache tracking 940 * ========================================================================== 941 */ 942 943 /* 944 * Hot spares and cache devices are tracked using the same code below, 945 * for 'auxiliary' devices. 946 */ 947 948 typedef struct spa_aux { 949 uint64_t aux_guid; 950 uint64_t aux_pool; 951 avl_node_t aux_avl; 952 int aux_count; 953 } spa_aux_t; 954 955 static inline int 956 spa_aux_compare(const void *a, const void *b) 957 { 958 const spa_aux_t *sa = (const spa_aux_t *)a; 959 const spa_aux_t *sb = (const spa_aux_t *)b; 960 961 return (TREE_CMP(sa->aux_guid, sb->aux_guid)); 962 } 963 964 void 965 spa_aux_add(vdev_t *vd, avl_tree_t *avl) 966 { 967 avl_index_t where; 968 spa_aux_t search; 969 spa_aux_t *aux; 970 971 search.aux_guid = vd->vdev_guid; 972 if ((aux = avl_find(avl, &search, &where)) != NULL) { 973 aux->aux_count++; 974 } else { 975 aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP); 976 aux->aux_guid = vd->vdev_guid; 977 aux->aux_count = 1; 978 avl_insert(avl, aux, where); 979 } 980 } 981 982 void 983 spa_aux_remove(vdev_t *vd, avl_tree_t *avl) 984 { 985 spa_aux_t search; 986 spa_aux_t *aux; 987 avl_index_t where; 988 989 search.aux_guid = vd->vdev_guid; 990 aux = avl_find(avl, &search, &where); 991 992 ASSERT(aux != NULL); 993 994 if (--aux->aux_count == 0) { 995 avl_remove(avl, aux); 996 kmem_free(aux, sizeof (spa_aux_t)); 997 } else if (aux->aux_pool == spa_guid(vd->vdev_spa)) { 998 aux->aux_pool = 0ULL; 999 } 1000 } 1001 1002 boolean_t 1003 spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl) 1004 { 1005 spa_aux_t search, *found; 1006 1007 search.aux_guid = guid; 1008 found = avl_find(avl, &search, NULL); 1009 1010 if (pool) { 1011 if (found) 1012 *pool = found->aux_pool; 1013 else 1014 *pool = 0ULL; 1015 } 1016 1017 if (refcnt) { 1018 if (found) 1019 *refcnt = found->aux_count; 1020 else 1021 *refcnt = 0; 1022 } 1023 1024 return (found != NULL); 1025 } 1026 1027 void 1028 spa_aux_activate(vdev_t *vd, avl_tree_t *avl) 1029 { 1030 spa_aux_t search, *found; 1031 avl_index_t where; 1032 1033 search.aux_guid = vd->vdev_guid; 1034 found = avl_find(avl, &search, &where); 1035 ASSERT(found != NULL); 1036 ASSERT(found->aux_pool == 0ULL); 1037 1038 found->aux_pool = spa_guid(vd->vdev_spa); 1039 } 1040 1041 /* 1042 * Spares are tracked globally due to the following constraints: 1043 * 1044 * - A spare may be part of multiple pools. 1045 * - A spare may be added to a pool even if it's actively in use within 1046 * another pool. 1047 * - A spare in use in any pool can only be the source of a replacement if 1048 * the target is a spare in the same pool. 1049 * 1050 * We keep track of all spares on the system through the use of a reference 1051 * counted AVL tree. When a vdev is added as a spare, or used as a replacement 1052 * spare, then we bump the reference count in the AVL tree. In addition, we set 1053 * the 'vdev_isspare' member to indicate that the device is a spare (active or 1054 * inactive). When a spare is made active (used to replace a device in the 1055 * pool), we also keep track of which pool its been made a part of. 1056 * 1057 * The 'spa_spare_lock' protects the AVL tree. These functions are normally 1058 * called under the spa_namespace lock as part of vdev reconfiguration. The 1059 * separate spare lock exists for the status query path, which does not need to 1060 * be completely consistent with respect to other vdev configuration changes. 1061 */ 1062 1063 /* 1064 * Poll the spare vdevs to make sure they are not faulty. 1065 * 1066 * The probe operation will raise an ENXIO error and create an FM ereport if the 1067 * probe fails. 1068 */ 1069 void 1070 spa_spare_poll(spa_t *spa) 1071 { 1072 boolean_t async_request = B_FALSE; 1073 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 1074 for (int i = 0; i < spa->spa_spares.sav_count; i++) { 1075 spa_aux_t search, *found; 1076 vdev_t *vd = spa->spa_spares.sav_vdevs[i]; 1077 1078 search.aux_guid = vd->vdev_guid; 1079 1080 mutex_enter(&spa_spare_lock); 1081 found = avl_find(&spa_spare_avl, &search, NULL); 1082 /* This spare is in use by a pool. */ 1083 if (found != NULL && found->aux_pool != 0) { 1084 mutex_exit(&spa_spare_lock); 1085 continue; 1086 } 1087 mutex_exit(&spa_spare_lock); 1088 1089 vd->vdev_probe_wanted = B_TRUE; 1090 async_request = B_TRUE; 1091 } 1092 if (async_request) 1093 spa_async_request(spa, SPA_ASYNC_PROBE); 1094 1095 spa_config_exit(spa, SCL_STATE, FTAG); 1096 } 1097 1098 static int 1099 spa_spare_compare(const void *a, const void *b) 1100 { 1101 return (spa_aux_compare(a, b)); 1102 } 1103 1104 void 1105 spa_spare_add(vdev_t *vd) 1106 { 1107 mutex_enter(&spa_spare_lock); 1108 ASSERT(!vd->vdev_isspare); 1109 spa_aux_add(vd, &spa_spare_avl); 1110 vd->vdev_isspare = B_TRUE; 1111 mutex_exit(&spa_spare_lock); 1112 } 1113 1114 void 1115 spa_spare_remove(vdev_t *vd) 1116 { 1117 mutex_enter(&spa_spare_lock); 1118 ASSERT(vd->vdev_isspare); 1119 spa_aux_remove(vd, &spa_spare_avl); 1120 vd->vdev_isspare = B_FALSE; 1121 mutex_exit(&spa_spare_lock); 1122 } 1123 1124 boolean_t 1125 spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt) 1126 { 1127 boolean_t found; 1128 1129 mutex_enter(&spa_spare_lock); 1130 found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl); 1131 mutex_exit(&spa_spare_lock); 1132 1133 return (found); 1134 } 1135 1136 void 1137 spa_spare_activate(vdev_t *vd) 1138 { 1139 mutex_enter(&spa_spare_lock); 1140 ASSERT(vd->vdev_isspare); 1141 spa_aux_activate(vd, &spa_spare_avl); 1142 mutex_exit(&spa_spare_lock); 1143 } 1144 1145 /* 1146 * Level 2 ARC devices are tracked globally for the same reasons as spares. 1147 * Cache devices currently only support one pool per cache device, and so 1148 * for these devices the aux reference count is currently unused beyond 1. 1149 */ 1150 1151 static int 1152 spa_l2cache_compare(const void *a, const void *b) 1153 { 1154 return (spa_aux_compare(a, b)); 1155 } 1156 1157 void 1158 spa_l2cache_add(vdev_t *vd) 1159 { 1160 mutex_enter(&spa_l2cache_lock); 1161 ASSERT(!vd->vdev_isl2cache); 1162 spa_aux_add(vd, &spa_l2cache_avl); 1163 vd->vdev_isl2cache = B_TRUE; 1164 mutex_exit(&spa_l2cache_lock); 1165 } 1166 1167 void 1168 spa_l2cache_remove(vdev_t *vd) 1169 { 1170 mutex_enter(&spa_l2cache_lock); 1171 ASSERT(vd->vdev_isl2cache); 1172 spa_aux_remove(vd, &spa_l2cache_avl); 1173 vd->vdev_isl2cache = B_FALSE; 1174 mutex_exit(&spa_l2cache_lock); 1175 } 1176 1177 boolean_t 1178 spa_l2cache_exists(uint64_t guid, uint64_t *pool) 1179 { 1180 boolean_t found; 1181 1182 mutex_enter(&spa_l2cache_lock); 1183 found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl); 1184 mutex_exit(&spa_l2cache_lock); 1185 1186 return (found); 1187 } 1188 1189 void 1190 spa_l2cache_activate(vdev_t *vd) 1191 { 1192 mutex_enter(&spa_l2cache_lock); 1193 ASSERT(vd->vdev_isl2cache); 1194 spa_aux_activate(vd, &spa_l2cache_avl); 1195 mutex_exit(&spa_l2cache_lock); 1196 } 1197 1198 /* 1199 * ========================================================================== 1200 * SPA vdev locking 1201 * ========================================================================== 1202 */ 1203 1204 /* 1205 * Lock the given spa_t for the purpose of adding or removing a vdev. 1206 * Grabs the global spa_namespace_lock plus the spa config lock for writing. 1207 * It returns the next transaction group for the spa_t. 1208 */ 1209 uint64_t 1210 spa_vdev_enter(spa_t *spa) 1211 { 1212 mutex_enter(&spa->spa_vdev_top_lock); 1213 mutex_enter(&spa_namespace_lock); 1214 1215 vdev_autotrim_stop_all(spa); 1216 1217 return (spa_vdev_config_enter(spa)); 1218 } 1219 1220 /* 1221 * Internal implementation for spa_vdev_enter(). Used when a vdev 1222 * operation requires multiple syncs (i.e. removing a device) while 1223 * keeping the spa_namespace_lock held. 1224 */ 1225 uint64_t 1226 spa_vdev_config_enter(spa_t *spa) 1227 { 1228 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 1229 1230 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER); 1231 1232 return (spa_last_synced_txg(spa) + 1); 1233 } 1234 1235 /* 1236 * Used in combination with spa_vdev_config_enter() to allow the syncing 1237 * of multiple transactions without releasing the spa_namespace_lock. 1238 */ 1239 void 1240 spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag) 1241 { 1242 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 1243 1244 int config_changed = B_FALSE; 1245 1246 ASSERT(txg > spa_last_synced_txg(spa)); 1247 1248 spa->spa_pending_vdev = NULL; 1249 1250 /* 1251 * Reassess the DTLs. 1252 */ 1253 vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE); 1254 1255 if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) { 1256 config_changed = B_TRUE; 1257 spa->spa_config_generation++; 1258 } 1259 1260 /* 1261 * Verify the metaslab classes. 1262 */ 1263 ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0); 1264 ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0); 1265 ASSERT(metaslab_class_validate(spa_special_class(spa)) == 0); 1266 ASSERT(metaslab_class_validate(spa_dedup_class(spa)) == 0); 1267 1268 spa_config_exit(spa, SCL_ALL, spa); 1269 1270 /* 1271 * Panic the system if the specified tag requires it. This 1272 * is useful for ensuring that configurations are updated 1273 * transactionally. 1274 */ 1275 if (zio_injection_enabled) 1276 zio_handle_panic_injection(spa, tag, 0); 1277 1278 /* 1279 * Note: this txg_wait_synced() is important because it ensures 1280 * that there won't be more than one config change per txg. 1281 * This allows us to use the txg as the generation number. 1282 */ 1283 if (error == 0) 1284 txg_wait_synced(spa->spa_dsl_pool, txg); 1285 1286 if (vd != NULL) { 1287 ASSERT(!vd->vdev_detached || vd->vdev_dtl_sm == NULL); 1288 if (vd->vdev_ops->vdev_op_leaf) { 1289 mutex_enter(&vd->vdev_initialize_lock); 1290 vdev_initialize_stop(vd, VDEV_INITIALIZE_CANCELED, 1291 NULL); 1292 mutex_exit(&vd->vdev_initialize_lock); 1293 1294 mutex_enter(&vd->vdev_trim_lock); 1295 vdev_trim_stop(vd, VDEV_TRIM_CANCELED, NULL); 1296 mutex_exit(&vd->vdev_trim_lock); 1297 } 1298 1299 /* 1300 * The vdev may be both a leaf and top-level device. 1301 */ 1302 vdev_autotrim_stop_wait(vd); 1303 1304 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER); 1305 vdev_free(vd); 1306 spa_config_exit(spa, SCL_ALL, spa); 1307 } 1308 1309 /* 1310 * If the config changed, update the config cache. 1311 */ 1312 if (config_changed) 1313 spa_write_cachefile(spa, B_FALSE, B_TRUE); 1314 } 1315 1316 /* 1317 * Unlock the spa_t after adding or removing a vdev. Besides undoing the 1318 * locking of spa_vdev_enter(), we also want make sure the transactions have 1319 * synced to disk, and then update the global configuration cache with the new 1320 * information. 1321 */ 1322 int 1323 spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error) 1324 { 1325 vdev_autotrim_restart(spa); 1326 1327 spa_vdev_config_exit(spa, vd, txg, error, FTAG); 1328 mutex_exit(&spa_namespace_lock); 1329 mutex_exit(&spa->spa_vdev_top_lock); 1330 1331 return (error); 1332 } 1333 1334 /* 1335 * Lock the given spa_t for the purpose of changing vdev state. 1336 */ 1337 void 1338 spa_vdev_state_enter(spa_t *spa, int oplocks) 1339 { 1340 int locks = SCL_STATE_ALL | oplocks; 1341 1342 /* 1343 * Root pools may need to read of the underlying devfs filesystem 1344 * when opening up a vdev. Unfortunately if we're holding the 1345 * SCL_ZIO lock it will result in a deadlock when we try to issue 1346 * the read from the root filesystem. Instead we "prefetch" 1347 * the associated vnodes that we need prior to opening the 1348 * underlying devices and cache them so that we can prevent 1349 * any I/O when we are doing the actual open. 1350 */ 1351 if (spa_is_root(spa)) { 1352 int low = locks & ~(SCL_ZIO - 1); 1353 int high = locks & ~low; 1354 1355 spa_config_enter(spa, high, spa, RW_WRITER); 1356 vdev_hold(spa->spa_root_vdev); 1357 spa_config_enter(spa, low, spa, RW_WRITER); 1358 } else { 1359 spa_config_enter(spa, locks, spa, RW_WRITER); 1360 } 1361 spa->spa_vdev_locks = locks; 1362 } 1363 1364 int 1365 spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error) 1366 { 1367 boolean_t config_changed = B_FALSE; 1368 1369 if (vd != NULL || error == 0) 1370 vdev_dtl_reassess(vd ? vd->vdev_top : spa->spa_root_vdev, 1371 0, 0, B_FALSE); 1372 1373 if (vd != NULL) { 1374 vdev_state_dirty(vd->vdev_top); 1375 config_changed = B_TRUE; 1376 spa->spa_config_generation++; 1377 } 1378 1379 if (spa_is_root(spa)) 1380 vdev_rele(spa->spa_root_vdev); 1381 1382 ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL); 1383 spa_config_exit(spa, spa->spa_vdev_locks, spa); 1384 1385 /* 1386 * If anything changed, wait for it to sync. This ensures that, 1387 * from the system administrator's perspective, zpool(1M) commands 1388 * are synchronous. This is important for things like zpool offline: 1389 * when the command completes, you expect no further I/O from ZFS. 1390 */ 1391 if (vd != NULL) 1392 txg_wait_synced(spa->spa_dsl_pool, 0); 1393 1394 /* 1395 * If the config changed, update the config cache. 1396 */ 1397 if (config_changed) { 1398 mutex_enter(&spa_namespace_lock); 1399 spa_write_cachefile(spa, B_FALSE, B_TRUE); 1400 mutex_exit(&spa_namespace_lock); 1401 } 1402 1403 return (error); 1404 } 1405 1406 /* 1407 * ========================================================================== 1408 * Miscellaneous functions 1409 * ========================================================================== 1410 */ 1411 1412 void 1413 spa_activate_mos_feature(spa_t *spa, const char *feature, dmu_tx_t *tx) 1414 { 1415 if (!nvlist_exists(spa->spa_label_features, feature)) { 1416 fnvlist_add_boolean(spa->spa_label_features, feature); 1417 /* 1418 * When we are creating the pool (tx_txg==TXG_INITIAL), we can't 1419 * dirty the vdev config because lock SCL_CONFIG is not held. 1420 * Thankfully, in this case we don't need to dirty the config 1421 * because it will be written out anyway when we finish 1422 * creating the pool. 1423 */ 1424 if (tx->tx_txg != TXG_INITIAL) 1425 vdev_config_dirty(spa->spa_root_vdev); 1426 } 1427 } 1428 1429 void 1430 spa_deactivate_mos_feature(spa_t *spa, const char *feature) 1431 { 1432 if (nvlist_remove_all(spa->spa_label_features, feature) == 0) 1433 vdev_config_dirty(spa->spa_root_vdev); 1434 } 1435 1436 /* 1437 * Return the spa_t associated with given pool_guid, if it exists. If 1438 * device_guid is non-zero, determine whether the pool exists *and* contains 1439 * a device with the specified device_guid. 1440 */ 1441 spa_t * 1442 spa_by_guid(uint64_t pool_guid, uint64_t device_guid) 1443 { 1444 spa_t *spa; 1445 avl_tree_t *t = &spa_namespace_avl; 1446 1447 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 1448 1449 for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) { 1450 if (spa->spa_state == POOL_STATE_UNINITIALIZED) 1451 continue; 1452 if (spa->spa_root_vdev == NULL) 1453 continue; 1454 if (spa_guid(spa) == pool_guid) { 1455 if (device_guid == 0) 1456 break; 1457 1458 if (vdev_lookup_by_guid(spa->spa_root_vdev, 1459 device_guid) != NULL) 1460 break; 1461 1462 /* 1463 * Check any devices we may be in the process of adding. 1464 */ 1465 if (spa->spa_pending_vdev) { 1466 if (vdev_lookup_by_guid(spa->spa_pending_vdev, 1467 device_guid) != NULL) 1468 break; 1469 } 1470 } 1471 } 1472 1473 return (spa); 1474 } 1475 1476 /* 1477 * Determine whether a pool with the given pool_guid exists. 1478 */ 1479 boolean_t 1480 spa_guid_exists(uint64_t pool_guid, uint64_t device_guid) 1481 { 1482 return (spa_by_guid(pool_guid, device_guid) != NULL); 1483 } 1484 1485 char * 1486 spa_strdup(const char *s) 1487 { 1488 size_t len; 1489 char *new; 1490 1491 len = strlen(s); 1492 new = kmem_alloc(len + 1, KM_SLEEP); 1493 bcopy(s, new, len); 1494 new[len] = '\0'; 1495 1496 return (new); 1497 } 1498 1499 void 1500 spa_strfree(char *s) 1501 { 1502 kmem_free(s, strlen(s) + 1); 1503 } 1504 1505 uint64_t 1506 spa_get_random(uint64_t range) 1507 { 1508 uint64_t r; 1509 1510 ASSERT(range != 0); 1511 1512 if (range == 1) 1513 return (0); 1514 1515 (void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t)); 1516 1517 return (r % range); 1518 } 1519 1520 uint64_t 1521 spa_generate_guid(spa_t *spa) 1522 { 1523 uint64_t guid = spa_get_random(-1ULL); 1524 1525 if (spa != NULL) { 1526 while (guid == 0 || spa_guid_exists(spa_guid(spa), guid)) 1527 guid = spa_get_random(-1ULL); 1528 } else { 1529 while (guid == 0 || spa_guid_exists(guid, 0)) 1530 guid = spa_get_random(-1ULL); 1531 } 1532 1533 return (guid); 1534 } 1535 1536 void 1537 snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp) 1538 { 1539 char type[256]; 1540 char *checksum = NULL; 1541 char *compress = NULL; 1542 1543 if (bp != NULL) { 1544 if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) { 1545 dmu_object_byteswap_t bswap = 1546 DMU_OT_BYTESWAP(BP_GET_TYPE(bp)); 1547 (void) snprintf(type, sizeof (type), "bswap %s %s", 1548 DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ? 1549 "metadata" : "data", 1550 dmu_ot_byteswap[bswap].ob_name); 1551 } else { 1552 (void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name, 1553 sizeof (type)); 1554 } 1555 if (!BP_IS_EMBEDDED(bp)) { 1556 checksum = 1557 zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name; 1558 } 1559 compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name; 1560 } 1561 1562 SNPRINTF_BLKPTR(snprintf, ' ', buf, buflen, bp, type, checksum, 1563 compress); 1564 } 1565 1566 void 1567 spa_freeze(spa_t *spa) 1568 { 1569 uint64_t freeze_txg = 0; 1570 1571 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1572 if (spa->spa_freeze_txg == UINT64_MAX) { 1573 freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE; 1574 spa->spa_freeze_txg = freeze_txg; 1575 } 1576 spa_config_exit(spa, SCL_ALL, FTAG); 1577 if (freeze_txg != 0) 1578 txg_wait_synced(spa_get_dsl(spa), freeze_txg); 1579 } 1580 1581 void 1582 zfs_panic_recover(const char *fmt, ...) 1583 { 1584 va_list adx; 1585 1586 va_start(adx, fmt); 1587 vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx); 1588 va_end(adx); 1589 } 1590 1591 /* 1592 * This is a stripped-down version of strtoull, suitable only for converting 1593 * lowercase hexadecimal numbers that don't overflow. 1594 */ 1595 uint64_t 1596 zfs_strtonum(const char *str, char **nptr) 1597 { 1598 uint64_t val = 0; 1599 char c; 1600 int digit; 1601 1602 while ((c = *str) != '\0') { 1603 if (c >= '0' && c <= '9') 1604 digit = c - '0'; 1605 else if (c >= 'a' && c <= 'f') 1606 digit = 10 + c - 'a'; 1607 else 1608 break; 1609 1610 val *= 16; 1611 val += digit; 1612 1613 str++; 1614 } 1615 1616 if (nptr) 1617 *nptr = (char *)str; 1618 1619 return (val); 1620 } 1621 1622 void 1623 spa_activate_allocation_classes(spa_t *spa, dmu_tx_t *tx) 1624 { 1625 /* 1626 * We bump the feature refcount for each special vdev added to the pool 1627 */ 1628 ASSERT(spa_feature_is_enabled(spa, SPA_FEATURE_ALLOCATION_CLASSES)); 1629 spa_feature_incr(spa, SPA_FEATURE_ALLOCATION_CLASSES, tx); 1630 } 1631 1632 /* 1633 * ========================================================================== 1634 * Accessor functions 1635 * ========================================================================== 1636 */ 1637 1638 boolean_t 1639 spa_shutting_down(spa_t *spa) 1640 { 1641 return (spa->spa_async_suspended); 1642 } 1643 1644 dsl_pool_t * 1645 spa_get_dsl(spa_t *spa) 1646 { 1647 return (spa->spa_dsl_pool); 1648 } 1649 1650 boolean_t 1651 spa_is_initializing(spa_t *spa) 1652 { 1653 return (spa->spa_is_initializing); 1654 } 1655 1656 boolean_t 1657 spa_indirect_vdevs_loaded(spa_t *spa) 1658 { 1659 return (spa->spa_indirect_vdevs_loaded); 1660 } 1661 1662 blkptr_t * 1663 spa_get_rootblkptr(spa_t *spa) 1664 { 1665 return (&spa->spa_ubsync.ub_rootbp); 1666 } 1667 1668 void 1669 spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp) 1670 { 1671 spa->spa_uberblock.ub_rootbp = *bp; 1672 } 1673 1674 void 1675 spa_altroot(spa_t *spa, char *buf, size_t buflen) 1676 { 1677 if (spa->spa_root == NULL) 1678 buf[0] = '\0'; 1679 else 1680 (void) strncpy(buf, spa->spa_root, buflen); 1681 } 1682 1683 int 1684 spa_sync_pass(spa_t *spa) 1685 { 1686 return (spa->spa_sync_pass); 1687 } 1688 1689 char * 1690 spa_name(spa_t *spa) 1691 { 1692 return (spa->spa_name); 1693 } 1694 1695 uint64_t 1696 spa_guid(spa_t *spa) 1697 { 1698 dsl_pool_t *dp = spa_get_dsl(spa); 1699 uint64_t guid; 1700 1701 /* 1702 * If we fail to parse the config during spa_load(), we can go through 1703 * the error path (which posts an ereport) and end up here with no root 1704 * vdev. We stash the original pool guid in 'spa_config_guid' to handle 1705 * this case. 1706 */ 1707 if (spa->spa_root_vdev == NULL) 1708 return (spa->spa_config_guid); 1709 1710 guid = spa->spa_last_synced_guid != 0 ? 1711 spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid; 1712 1713 /* 1714 * Return the most recently synced out guid unless we're 1715 * in syncing context. 1716 */ 1717 if (dp && dsl_pool_sync_context(dp)) 1718 return (spa->spa_root_vdev->vdev_guid); 1719 else 1720 return (guid); 1721 } 1722 1723 uint64_t 1724 spa_load_guid(spa_t *spa) 1725 { 1726 /* 1727 * This is a GUID that exists solely as a reference for the 1728 * purposes of the arc. It is generated at load time, and 1729 * is never written to persistent storage. 1730 */ 1731 return (spa->spa_load_guid); 1732 } 1733 1734 uint64_t 1735 spa_last_synced_txg(spa_t *spa) 1736 { 1737 return (spa->spa_ubsync.ub_txg); 1738 } 1739 1740 uint64_t 1741 spa_first_txg(spa_t *spa) 1742 { 1743 return (spa->spa_first_txg); 1744 } 1745 1746 uint64_t 1747 spa_syncing_txg(spa_t *spa) 1748 { 1749 return (spa->spa_syncing_txg); 1750 } 1751 1752 /* 1753 * Return the last txg where data can be dirtied. The final txgs 1754 * will be used to just clear out any deferred frees that remain. 1755 */ 1756 uint64_t 1757 spa_final_dirty_txg(spa_t *spa) 1758 { 1759 return (spa->spa_final_txg - TXG_DEFER_SIZE); 1760 } 1761 1762 pool_state_t 1763 spa_state(spa_t *spa) 1764 { 1765 return (spa->spa_state); 1766 } 1767 1768 spa_load_state_t 1769 spa_load_state(spa_t *spa) 1770 { 1771 return (spa->spa_load_state); 1772 } 1773 1774 uint64_t 1775 spa_freeze_txg(spa_t *spa) 1776 { 1777 return (spa->spa_freeze_txg); 1778 } 1779 1780 /* ARGSUSED */ 1781 uint64_t 1782 spa_get_worst_case_asize(spa_t *spa, uint64_t lsize) 1783 { 1784 return (lsize * spa_asize_inflation); 1785 } 1786 1787 /* 1788 * Return the amount of slop space in bytes. It is 1/32 of the pool (3.2%), 1789 * or at least 128MB, unless that would cause it to be more than half the 1790 * pool size. 1791 * 1792 * See the comment above spa_slop_shift for details. 1793 */ 1794 uint64_t 1795 spa_get_slop_space(spa_t *spa) 1796 { 1797 uint64_t space = spa_get_dspace(spa); 1798 return (MAX(space >> spa_slop_shift, MIN(space >> 1, spa_min_slop))); 1799 } 1800 1801 uint64_t 1802 spa_get_dspace(spa_t *spa) 1803 { 1804 return (spa->spa_dspace); 1805 } 1806 1807 uint64_t 1808 spa_get_checkpoint_space(spa_t *spa) 1809 { 1810 return (spa->spa_checkpoint_info.sci_dspace); 1811 } 1812 1813 void 1814 spa_update_dspace(spa_t *spa) 1815 { 1816 spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) + 1817 ddt_get_dedup_dspace(spa); 1818 if (spa->spa_vdev_removal != NULL) { 1819 /* 1820 * We can't allocate from the removing device, so 1821 * subtract its size. This prevents the DMU/DSL from 1822 * filling up the (now smaller) pool while we are in the 1823 * middle of removing the device. 1824 * 1825 * Note that the DMU/DSL doesn't actually know or care 1826 * how much space is allocated (it does its own tracking 1827 * of how much space has been logically used). So it 1828 * doesn't matter that the data we are moving may be 1829 * allocated twice (on the old device and the new 1830 * device). 1831 */ 1832 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 1833 vdev_t *vd = 1834 vdev_lookup_top(spa, spa->spa_vdev_removal->svr_vdev_id); 1835 spa->spa_dspace -= spa_deflate(spa) ? 1836 vd->vdev_stat.vs_dspace : vd->vdev_stat.vs_space; 1837 spa_config_exit(spa, SCL_VDEV, FTAG); 1838 } 1839 } 1840 1841 /* 1842 * Return the failure mode that has been set to this pool. The default 1843 * behavior will be to block all I/Os when a complete failure occurs. 1844 */ 1845 uint8_t 1846 spa_get_failmode(spa_t *spa) 1847 { 1848 return (spa->spa_failmode); 1849 } 1850 1851 boolean_t 1852 spa_suspended(spa_t *spa) 1853 { 1854 return (spa->spa_suspended != ZIO_SUSPEND_NONE); 1855 } 1856 1857 uint64_t 1858 spa_version(spa_t *spa) 1859 { 1860 return (spa->spa_ubsync.ub_version); 1861 } 1862 1863 boolean_t 1864 spa_deflate(spa_t *spa) 1865 { 1866 return (spa->spa_deflate); 1867 } 1868 1869 metaslab_class_t * 1870 spa_normal_class(spa_t *spa) 1871 { 1872 return (spa->spa_normal_class); 1873 } 1874 1875 metaslab_class_t * 1876 spa_log_class(spa_t *spa) 1877 { 1878 return (spa->spa_log_class); 1879 } 1880 1881 metaslab_class_t * 1882 spa_special_class(spa_t *spa) 1883 { 1884 return (spa->spa_special_class); 1885 } 1886 1887 metaslab_class_t * 1888 spa_dedup_class(spa_t *spa) 1889 { 1890 return (spa->spa_dedup_class); 1891 } 1892 1893 /* 1894 * Locate an appropriate allocation class 1895 */ 1896 metaslab_class_t * 1897 spa_preferred_class(spa_t *spa, uint64_t size, dmu_object_type_t objtype, 1898 uint_t level, uint_t special_smallblk) 1899 { 1900 if (DMU_OT_IS_ZIL(objtype)) { 1901 if (spa->spa_log_class->mc_groups != 0) 1902 return (spa_log_class(spa)); 1903 else 1904 return (spa_normal_class(spa)); 1905 } 1906 1907 boolean_t has_special_class = spa->spa_special_class->mc_groups != 0; 1908 1909 if (DMU_OT_IS_DDT(objtype)) { 1910 if (spa->spa_dedup_class->mc_groups != 0) 1911 return (spa_dedup_class(spa)); 1912 else if (has_special_class && zfs_ddt_data_is_special) 1913 return (spa_special_class(spa)); 1914 else 1915 return (spa_normal_class(spa)); 1916 } 1917 1918 /* Indirect blocks for user data can land in special if allowed */ 1919 if (level > 0 && (DMU_OT_IS_FILE(objtype) || objtype == DMU_OT_ZVOL)) { 1920 if (has_special_class && zfs_user_indirect_is_special) 1921 return (spa_special_class(spa)); 1922 else 1923 return (spa_normal_class(spa)); 1924 } 1925 1926 if (DMU_OT_IS_METADATA(objtype) || level > 0) { 1927 if (has_special_class) 1928 return (spa_special_class(spa)); 1929 else 1930 return (spa_normal_class(spa)); 1931 } 1932 1933 /* 1934 * Allow small file blocks in special class in some cases (like 1935 * for the dRAID vdev feature). But always leave a reserve of 1936 * zfs_special_class_metadata_reserve_pct exclusively for metadata. 1937 */ 1938 if (DMU_OT_IS_FILE(objtype) && 1939 has_special_class && size <= special_smallblk) { 1940 metaslab_class_t *special = spa_special_class(spa); 1941 uint64_t alloc = metaslab_class_get_alloc(special); 1942 uint64_t space = metaslab_class_get_space(special); 1943 uint64_t limit = 1944 (space * (100 - zfs_special_class_metadata_reserve_pct)) 1945 / 100; 1946 1947 if (alloc < limit) 1948 return (special); 1949 } 1950 1951 return (spa_normal_class(spa)); 1952 } 1953 1954 void 1955 spa_evicting_os_register(spa_t *spa, objset_t *os) 1956 { 1957 mutex_enter(&spa->spa_evicting_os_lock); 1958 list_insert_head(&spa->spa_evicting_os_list, os); 1959 mutex_exit(&spa->spa_evicting_os_lock); 1960 } 1961 1962 void 1963 spa_evicting_os_deregister(spa_t *spa, objset_t *os) 1964 { 1965 mutex_enter(&spa->spa_evicting_os_lock); 1966 list_remove(&spa->spa_evicting_os_list, os); 1967 cv_broadcast(&spa->spa_evicting_os_cv); 1968 mutex_exit(&spa->spa_evicting_os_lock); 1969 } 1970 1971 void 1972 spa_evicting_os_wait(spa_t *spa) 1973 { 1974 mutex_enter(&spa->spa_evicting_os_lock); 1975 while (!list_is_empty(&spa->spa_evicting_os_list)) 1976 cv_wait(&spa->spa_evicting_os_cv, &spa->spa_evicting_os_lock); 1977 mutex_exit(&spa->spa_evicting_os_lock); 1978 1979 dmu_buf_user_evict_wait(); 1980 } 1981 1982 int 1983 spa_max_replication(spa_t *spa) 1984 { 1985 /* 1986 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to 1987 * handle BPs with more than one DVA allocated. Set our max 1988 * replication level accordingly. 1989 */ 1990 if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS) 1991 return (1); 1992 return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override)); 1993 } 1994 1995 int 1996 spa_prev_software_version(spa_t *spa) 1997 { 1998 return (spa->spa_prev_software_version); 1999 } 2000 2001 uint64_t 2002 spa_deadman_synctime(spa_t *spa) 2003 { 2004 return (spa->spa_deadman_synctime); 2005 } 2006 2007 spa_autotrim_t 2008 spa_get_autotrim(spa_t *spa) 2009 { 2010 return (spa->spa_autotrim); 2011 } 2012 2013 uint64_t 2014 dva_get_dsize_sync(spa_t *spa, const dva_t *dva) 2015 { 2016 uint64_t asize = DVA_GET_ASIZE(dva); 2017 uint64_t dsize = asize; 2018 2019 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 2020 2021 if (asize != 0 && spa->spa_deflate) { 2022 vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva)); 2023 dsize = (asize >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio; 2024 } 2025 2026 return (dsize); 2027 } 2028 2029 uint64_t 2030 bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp) 2031 { 2032 uint64_t dsize = 0; 2033 2034 for (int d = 0; d < BP_GET_NDVAS(bp); d++) 2035 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]); 2036 2037 return (dsize); 2038 } 2039 2040 uint64_t 2041 bp_get_dsize(spa_t *spa, const blkptr_t *bp) 2042 { 2043 uint64_t dsize = 0; 2044 2045 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 2046 2047 for (int d = 0; d < BP_GET_NDVAS(bp); d++) 2048 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]); 2049 2050 spa_config_exit(spa, SCL_VDEV, FTAG); 2051 2052 return (dsize); 2053 } 2054 2055 uint64_t 2056 spa_dirty_data(spa_t *spa) 2057 { 2058 return (spa->spa_dsl_pool->dp_dirty_total); 2059 } 2060 2061 /* 2062 * ========================================================================== 2063 * SPA Import Progress Routines 2064 * The illumos implementation of these are different from OpenZFS. OpenZFS 2065 * uses the Linux /proc fs, whereas we use a kstat on the spa. 2066 * ========================================================================== 2067 */ 2068 2069 typedef struct spa_import_progress { 2070 kstat_named_t sip_load_state; 2071 kstat_named_t sip_mmp_sec_remaining; /* MMP activity check */ 2072 kstat_named_t sip_load_max_txg; /* rewind txg */ 2073 } spa_import_progress_t; 2074 2075 static void 2076 spa_import_progress_init(void) 2077 { 2078 } 2079 2080 static void 2081 spa_import_progress_destroy(void) 2082 { 2083 } 2084 2085 void spa_import_progress_add(spa_t *); 2086 2087 int 2088 spa_import_progress_set_state(spa_t *spa, spa_load_state_t load_state) 2089 { 2090 if (spa->spa_imp_kstat == NULL) 2091 spa_import_progress_add(spa); 2092 2093 mutex_enter(&spa->spa_imp_kstat_lock); 2094 if (spa->spa_imp_kstat != NULL) { 2095 spa_import_progress_t *sip = spa->spa_imp_kstat->ks_data; 2096 if (sip != NULL) 2097 sip->sip_load_state.value.ui64 = (uint64_t)load_state; 2098 } 2099 mutex_exit(&spa->spa_imp_kstat_lock); 2100 2101 return (0); 2102 } 2103 2104 int 2105 spa_import_progress_set_max_txg(spa_t *spa, uint64_t load_max_txg) 2106 { 2107 if (spa->spa_imp_kstat == NULL) 2108 spa_import_progress_add(spa); 2109 2110 mutex_enter(&spa->spa_imp_kstat_lock); 2111 if (spa->spa_imp_kstat != NULL) { 2112 spa_import_progress_t *sip = spa->spa_imp_kstat->ks_data; 2113 if (sip != NULL) 2114 sip->sip_load_max_txg.value.ui64 = load_max_txg; 2115 } 2116 mutex_exit(&spa->spa_imp_kstat_lock); 2117 2118 return (0); 2119 } 2120 2121 int 2122 spa_import_progress_set_mmp_check(spa_t *spa, uint64_t mmp_sec_remaining) 2123 { 2124 if (spa->spa_imp_kstat == NULL) 2125 spa_import_progress_add(spa); 2126 2127 mutex_enter(&spa->spa_imp_kstat_lock); 2128 if (spa->spa_imp_kstat != NULL) { 2129 spa_import_progress_t *sip = spa->spa_imp_kstat->ks_data; 2130 if (sip != NULL) 2131 sip->sip_mmp_sec_remaining.value.ui64 = 2132 mmp_sec_remaining; 2133 } 2134 mutex_exit(&spa->spa_imp_kstat_lock); 2135 2136 return (0); 2137 } 2138 2139 /* 2140 * A new import is in progress. Add an entry. 2141 */ 2142 void 2143 spa_import_progress_add(spa_t *spa) 2144 { 2145 char *poolname = NULL; 2146 spa_import_progress_t *sip; 2147 2148 mutex_enter(&spa->spa_imp_kstat_lock); 2149 if (spa->spa_imp_kstat != NULL) { 2150 sip = spa->spa_imp_kstat->ks_data; 2151 sip->sip_load_state.value.ui64 = (uint64_t)spa_load_state(spa); 2152 mutex_exit(&spa->spa_imp_kstat_lock); 2153 return; 2154 } 2155 2156 (void) nvlist_lookup_string(spa->spa_config, ZPOOL_CONFIG_POOL_NAME, 2157 &poolname); 2158 if (poolname == NULL) 2159 poolname = spa_name(spa); 2160 2161 spa->spa_imp_kstat = kstat_create("zfs_import", 0, poolname, 2162 "zfs_misc", KSTAT_TYPE_NAMED, 2163 sizeof (spa_import_progress_t) / sizeof (kstat_named_t), 2164 KSTAT_FLAG_VIRTUAL); 2165 if (spa->spa_imp_kstat != NULL) { 2166 sip = kmem_alloc(sizeof (spa_import_progress_t), KM_SLEEP); 2167 spa->spa_imp_kstat->ks_data = sip; 2168 2169 sip->sip_load_state.value.ui64 = (uint64_t)spa_load_state(spa); 2170 2171 kstat_named_init(&sip->sip_load_state, 2172 "spa_load_state", KSTAT_DATA_UINT64); 2173 kstat_named_init(&sip->sip_mmp_sec_remaining, 2174 "mmp_sec_remaining", KSTAT_DATA_UINT64); 2175 kstat_named_init(&sip->sip_load_max_txg, 2176 "spa_load_max_txg", KSTAT_DATA_UINT64); 2177 spa->spa_imp_kstat->ks_lock = &spa->spa_imp_kstat_lock; 2178 kstat_install(spa->spa_imp_kstat); 2179 } 2180 mutex_exit(&spa->spa_imp_kstat_lock); 2181 } 2182 2183 void 2184 spa_import_progress_remove(spa_t *spa) 2185 { 2186 if (spa->spa_imp_kstat != NULL) { 2187 void *data = spa->spa_imp_kstat->ks_data; 2188 2189 kstat_delete(spa->spa_imp_kstat); 2190 spa->spa_imp_kstat = NULL; 2191 kmem_free(data, sizeof (spa_import_progress_t)); 2192 } 2193 } 2194 2195 /* 2196 * ========================================================================== 2197 * Initialization and Termination 2198 * ========================================================================== 2199 */ 2200 2201 static int 2202 spa_name_compare(const void *a1, const void *a2) 2203 { 2204 const spa_t *s1 = a1; 2205 const spa_t *s2 = a2; 2206 int s; 2207 2208 s = strcmp(s1->spa_name, s2->spa_name); 2209 2210 return (TREE_ISIGN(s)); 2211 } 2212 2213 int 2214 spa_busy(void) 2215 { 2216 return (spa_active_count); 2217 } 2218 2219 void 2220 spa_boot_init() 2221 { 2222 spa_config_load(); 2223 } 2224 2225 void 2226 spa_init(int mode) 2227 { 2228 mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL); 2229 mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL); 2230 mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL); 2231 cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL); 2232 2233 avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t), 2234 offsetof(spa_t, spa_avl)); 2235 2236 avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t), 2237 offsetof(spa_aux_t, aux_avl)); 2238 2239 avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t), 2240 offsetof(spa_aux_t, aux_avl)); 2241 2242 spa_mode_global = mode; 2243 2244 #ifdef _KERNEL 2245 spa_arch_init(); 2246 #else 2247 if (spa_mode_global != FREAD && dprintf_find_string("watch")) { 2248 arc_procfd = open("/proc/self/ctl", O_WRONLY); 2249 if (arc_procfd == -1) { 2250 perror("could not enable watchpoints: " 2251 "opening /proc/self/ctl failed: "); 2252 } else { 2253 arc_watch = B_TRUE; 2254 } 2255 } 2256 #endif 2257 2258 zfs_refcount_init(); 2259 unique_init(); 2260 zfs_btree_init(); 2261 metaslab_stat_init(); 2262 zio_init(); 2263 dmu_init(); 2264 zil_init(); 2265 vdev_cache_stat_init(); 2266 vdev_mirror_stat_init(); 2267 vdev_raidz_math_init(); 2268 zfs_prop_init(); 2269 zpool_prop_init(); 2270 zpool_feature_init(); 2271 spa_config_load(); 2272 l2arc_start(); 2273 scan_init(); 2274 spa_import_progress_init(); 2275 } 2276 2277 void 2278 spa_fini(void) 2279 { 2280 l2arc_stop(); 2281 2282 spa_evict_all(); 2283 2284 vdev_cache_stat_fini(); 2285 vdev_mirror_stat_fini(); 2286 vdev_raidz_math_fini(); 2287 zil_fini(); 2288 dmu_fini(); 2289 zio_fini(); 2290 metaslab_stat_fini(); 2291 zfs_btree_fini(); 2292 unique_fini(); 2293 zfs_refcount_fini(); 2294 scan_fini(); 2295 spa_import_progress_destroy(); 2296 2297 avl_destroy(&spa_namespace_avl); 2298 avl_destroy(&spa_spare_avl); 2299 avl_destroy(&spa_l2cache_avl); 2300 2301 cv_destroy(&spa_namespace_cv); 2302 mutex_destroy(&spa_namespace_lock); 2303 mutex_destroy(&spa_spare_lock); 2304 mutex_destroy(&spa_l2cache_lock); 2305 } 2306 2307 /* 2308 * Return whether this pool has slogs. No locking needed. 2309 * It's not a problem if the wrong answer is returned as it's only for 2310 * performance and not correctness 2311 */ 2312 boolean_t 2313 spa_has_slogs(spa_t *spa) 2314 { 2315 return (spa->spa_log_class->mc_rotor != NULL); 2316 } 2317 2318 spa_log_state_t 2319 spa_get_log_state(spa_t *spa) 2320 { 2321 return (spa->spa_log_state); 2322 } 2323 2324 void 2325 spa_set_log_state(spa_t *spa, spa_log_state_t state) 2326 { 2327 spa->spa_log_state = state; 2328 } 2329 2330 boolean_t 2331 spa_is_root(spa_t *spa) 2332 { 2333 return (spa->spa_is_root); 2334 } 2335 2336 boolean_t 2337 spa_writeable(spa_t *spa) 2338 { 2339 return (!!(spa->spa_mode & FWRITE) && spa->spa_trust_config); 2340 } 2341 2342 /* 2343 * Returns true if there is a pending sync task in any of the current 2344 * syncing txg, the current quiescing txg, or the current open txg. 2345 */ 2346 boolean_t 2347 spa_has_pending_synctask(spa_t *spa) 2348 { 2349 return (!txg_all_lists_empty(&spa->spa_dsl_pool->dp_sync_tasks) || 2350 !txg_all_lists_empty(&spa->spa_dsl_pool->dp_early_sync_tasks)); 2351 } 2352 2353 int 2354 spa_mode(spa_t *spa) 2355 { 2356 return (spa->spa_mode); 2357 } 2358 2359 uint64_t 2360 spa_bootfs(spa_t *spa) 2361 { 2362 return (spa->spa_bootfs); 2363 } 2364 2365 uint64_t 2366 spa_delegation(spa_t *spa) 2367 { 2368 return (spa->spa_delegation); 2369 } 2370 2371 objset_t * 2372 spa_meta_objset(spa_t *spa) 2373 { 2374 return (spa->spa_meta_objset); 2375 } 2376 2377 enum zio_checksum 2378 spa_dedup_checksum(spa_t *spa) 2379 { 2380 return (spa->spa_dedup_checksum); 2381 } 2382 2383 /* 2384 * Reset pool scan stat per scan pass (or reboot). 2385 */ 2386 void 2387 spa_scan_stat_init(spa_t *spa) 2388 { 2389 /* data not stored on disk */ 2390 spa->spa_scan_pass_start = gethrestime_sec(); 2391 if (dsl_scan_is_paused_scrub(spa->spa_dsl_pool->dp_scan)) 2392 spa->spa_scan_pass_scrub_pause = spa->spa_scan_pass_start; 2393 else 2394 spa->spa_scan_pass_scrub_pause = 0; 2395 spa->spa_scan_pass_scrub_spent_paused = 0; 2396 spa->spa_scan_pass_exam = 0; 2397 spa->spa_scan_pass_issued = 0; 2398 vdev_scan_stat_init(spa->spa_root_vdev); 2399 } 2400 2401 /* 2402 * Get scan stats for zpool status reports 2403 */ 2404 int 2405 spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps) 2406 { 2407 dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL; 2408 2409 if (scn == NULL || scn->scn_phys.scn_func == POOL_SCAN_NONE) 2410 return (SET_ERROR(ENOENT)); 2411 bzero(ps, sizeof (pool_scan_stat_t)); 2412 2413 /* data stored on disk */ 2414 ps->pss_func = scn->scn_phys.scn_func; 2415 ps->pss_state = scn->scn_phys.scn_state; 2416 ps->pss_start_time = scn->scn_phys.scn_start_time; 2417 ps->pss_end_time = scn->scn_phys.scn_end_time; 2418 ps->pss_to_examine = scn->scn_phys.scn_to_examine; 2419 ps->pss_to_process = scn->scn_phys.scn_to_process; 2420 ps->pss_processed = scn->scn_phys.scn_processed; 2421 ps->pss_errors = scn->scn_phys.scn_errors; 2422 ps->pss_examined = scn->scn_phys.scn_examined; 2423 ps->pss_issued = 2424 scn->scn_issued_before_pass + spa->spa_scan_pass_issued; 2425 ps->pss_state = scn->scn_phys.scn_state; 2426 2427 /* data not stored on disk */ 2428 ps->pss_pass_start = spa->spa_scan_pass_start; 2429 ps->pss_pass_exam = spa->spa_scan_pass_exam; 2430 ps->pss_pass_issued = spa->spa_scan_pass_issued; 2431 ps->pss_pass_scrub_pause = spa->spa_scan_pass_scrub_pause; 2432 ps->pss_pass_scrub_spent_paused = spa->spa_scan_pass_scrub_spent_paused; 2433 2434 return (0); 2435 } 2436 2437 int 2438 spa_maxblocksize(spa_t *spa) 2439 { 2440 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) 2441 return (SPA_MAXBLOCKSIZE); 2442 else 2443 return (SPA_OLD_MAXBLOCKSIZE); 2444 } 2445 2446 int 2447 spa_maxdnodesize(spa_t *spa) 2448 { 2449 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_DNODE)) 2450 return (DNODE_MAX_SIZE); 2451 else 2452 return (DNODE_MIN_SIZE); 2453 } 2454 2455 boolean_t 2456 spa_multihost(spa_t *spa) 2457 { 2458 return (spa->spa_multihost ? B_TRUE : B_FALSE); 2459 } 2460 2461 unsigned long 2462 spa_get_hostid(void) 2463 { 2464 unsigned long myhostid; 2465 2466 #ifdef _KERNEL 2467 myhostid = zone_get_hostid(NULL); 2468 #else /* _KERNEL */ 2469 /* 2470 * We're emulating the system's hostid in userland, so 2471 * we can't use zone_get_hostid(). 2472 */ 2473 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 2474 #endif /* _KERNEL */ 2475 2476 return (myhostid); 2477 } 2478 2479 /* 2480 * Returns the txg that the last device removal completed. No indirect mappings 2481 * have been added since this txg. 2482 */ 2483 uint64_t 2484 spa_get_last_removal_txg(spa_t *spa) 2485 { 2486 uint64_t vdevid; 2487 uint64_t ret = -1ULL; 2488 2489 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 2490 /* 2491 * sr_prev_indirect_vdev is only modified while holding all the 2492 * config locks, so it is sufficient to hold SCL_VDEV as reader when 2493 * examining it. 2494 */ 2495 vdevid = spa->spa_removing_phys.sr_prev_indirect_vdev; 2496 2497 while (vdevid != -1ULL) { 2498 vdev_t *vd = vdev_lookup_top(spa, vdevid); 2499 vdev_indirect_births_t *vib = vd->vdev_indirect_births; 2500 2501 ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops); 2502 2503 /* 2504 * If the removal did not remap any data, we don't care. 2505 */ 2506 if (vdev_indirect_births_count(vib) != 0) { 2507 ret = vdev_indirect_births_last_entry_txg(vib); 2508 break; 2509 } 2510 2511 vdevid = vd->vdev_indirect_config.vic_prev_indirect_vdev; 2512 } 2513 spa_config_exit(spa, SCL_VDEV, FTAG); 2514 2515 IMPLY(ret != -1ULL, 2516 spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL)); 2517 2518 return (ret); 2519 } 2520 2521 boolean_t 2522 spa_trust_config(spa_t *spa) 2523 { 2524 return (spa->spa_trust_config); 2525 } 2526 2527 uint64_t 2528 spa_missing_tvds_allowed(spa_t *spa) 2529 { 2530 return (spa->spa_missing_tvds_allowed); 2531 } 2532 2533 space_map_t * 2534 spa_syncing_log_sm(spa_t *spa) 2535 { 2536 return (spa->spa_syncing_log_sm); 2537 } 2538 2539 void 2540 spa_set_missing_tvds(spa_t *spa, uint64_t missing) 2541 { 2542 spa->spa_missing_tvds = missing; 2543 } 2544 2545 boolean_t 2546 spa_top_vdevs_spacemap_addressable(spa_t *spa) 2547 { 2548 vdev_t *rvd = spa->spa_root_vdev; 2549 for (uint64_t c = 0; c < rvd->vdev_children; c++) { 2550 if (!vdev_is_spacemap_addressable(rvd->vdev_child[c])) 2551 return (B_FALSE); 2552 } 2553 return (B_TRUE); 2554 } 2555 2556 boolean_t 2557 spa_has_checkpoint(spa_t *spa) 2558 { 2559 return (spa->spa_checkpoint_txg != 0); 2560 } 2561 2562 boolean_t 2563 spa_importing_readonly_checkpoint(spa_t *spa) 2564 { 2565 return ((spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT) && 2566 spa->spa_mode == FREAD); 2567 } 2568 2569 uint64_t 2570 spa_min_claim_txg(spa_t *spa) 2571 { 2572 uint64_t checkpoint_txg = spa->spa_uberblock.ub_checkpoint_txg; 2573 2574 if (checkpoint_txg != 0) 2575 return (checkpoint_txg + 1); 2576 2577 return (spa->spa_first_txg); 2578 } 2579 2580 /* 2581 * If there is a checkpoint, async destroys may consume more space from 2582 * the pool instead of freeing it. In an attempt to save the pool from 2583 * getting suspended when it is about to run out of space, we stop 2584 * processing async destroys. 2585 */ 2586 boolean_t 2587 spa_suspend_async_destroy(spa_t *spa) 2588 { 2589 dsl_pool_t *dp = spa_get_dsl(spa); 2590 2591 uint64_t unreserved = dsl_pool_unreserved_space(dp, 2592 ZFS_SPACE_CHECK_EXTRA_RESERVED); 2593 uint64_t used = dsl_dir_phys(dp->dp_root_dir)->dd_used_bytes; 2594 uint64_t avail = (unreserved > used) ? (unreserved - used) : 0; 2595 2596 if (spa_has_checkpoint(spa) && avail == 0) 2597 return (B_TRUE); 2598 2599 return (B_FALSE); 2600 } 2601