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 */ 24 25 #include <sys/zfs_context.h> 26 #include <sys/spa_impl.h> 27 #include <sys/zio.h> 28 #include <sys/zio_checksum.h> 29 #include <sys/zio_compress.h> 30 #include <sys/dmu.h> 31 #include <sys/dmu_tx.h> 32 #include <sys/zap.h> 33 #include <sys/zil.h> 34 #include <sys/vdev_impl.h> 35 #include <sys/metaslab.h> 36 #include <sys/uberblock_impl.h> 37 #include <sys/txg.h> 38 #include <sys/avl.h> 39 #include <sys/unique.h> 40 #include <sys/dsl_pool.h> 41 #include <sys/dsl_dir.h> 42 #include <sys/dsl_prop.h> 43 #include <sys/fs/zfs.h> 44 #include <sys/metaslab_impl.h> 45 #include <sys/arc.h> 46 #include <sys/ddt.h> 47 #include "zfs_prop.h" 48 49 /* 50 * SPA locking 51 * 52 * There are four basic locks for managing spa_t structures: 53 * 54 * spa_namespace_lock (global mutex) 55 * 56 * This lock must be acquired to do any of the following: 57 * 58 * - Lookup a spa_t by name 59 * - Add or remove a spa_t from the namespace 60 * - Increase spa_refcount from non-zero 61 * - Check if spa_refcount is zero 62 * - Rename a spa_t 63 * - add/remove/attach/detach devices 64 * - Held for the duration of create/destroy/import/export 65 * 66 * It does not need to handle recursion. A create or destroy may 67 * reference objects (files or zvols) in other pools, but by 68 * definition they must have an existing reference, and will never need 69 * to lookup a spa_t by name. 70 * 71 * spa_refcount (per-spa refcount_t protected by mutex) 72 * 73 * This reference count keep track of any active users of the spa_t. The 74 * spa_t cannot be destroyed or freed while this is non-zero. Internally, 75 * the refcount is never really 'zero' - opening a pool implicitly keeps 76 * some references in the DMU. Internally we check against spa_minref, but 77 * present the image of a zero/non-zero value to consumers. 78 * 79 * spa_config_lock[] (per-spa array of rwlocks) 80 * 81 * This protects the spa_t from config changes, and must be held in 82 * the following circumstances: 83 * 84 * - RW_READER to perform I/O to the spa 85 * - RW_WRITER to change the vdev config 86 * 87 * The locking order is fairly straightforward: 88 * 89 * spa_namespace_lock -> spa_refcount 90 * 91 * The namespace lock must be acquired to increase the refcount from 0 92 * or to check if it is zero. 93 * 94 * spa_refcount -> spa_config_lock[] 95 * 96 * There must be at least one valid reference on the spa_t to acquire 97 * the config lock. 98 * 99 * spa_namespace_lock -> spa_config_lock[] 100 * 101 * The namespace lock must always be taken before the config lock. 102 * 103 * 104 * The spa_namespace_lock can be acquired directly and is globally visible. 105 * 106 * The namespace is manipulated using the following functions, all of which 107 * require the spa_namespace_lock to be held. 108 * 109 * spa_lookup() Lookup a spa_t by name. 110 * 111 * spa_add() Create a new spa_t in the namespace. 112 * 113 * spa_remove() Remove a spa_t from the namespace. This also 114 * frees up any memory associated with the spa_t. 115 * 116 * spa_next() Returns the next spa_t in the system, or the 117 * first if NULL is passed. 118 * 119 * spa_evict_all() Shutdown and remove all spa_t structures in 120 * the system. 121 * 122 * spa_guid_exists() Determine whether a pool/device guid exists. 123 * 124 * The spa_refcount is manipulated using the following functions: 125 * 126 * spa_open_ref() Adds a reference to the given spa_t. Must be 127 * called with spa_namespace_lock held if the 128 * refcount is currently zero. 129 * 130 * spa_close() Remove a reference from the spa_t. This will 131 * not free the spa_t or remove it from the 132 * namespace. No locking is required. 133 * 134 * spa_refcount_zero() Returns true if the refcount is currently 135 * zero. Must be called with spa_namespace_lock 136 * held. 137 * 138 * The spa_config_lock[] is an array of rwlocks, ordered as follows: 139 * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV. 140 * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}(). 141 * 142 * To read the configuration, it suffices to hold one of these locks as reader. 143 * To modify the configuration, you must hold all locks as writer. To modify 144 * vdev state without altering the vdev tree's topology (e.g. online/offline), 145 * you must hold SCL_STATE and SCL_ZIO as writer. 146 * 147 * We use these distinct config locks to avoid recursive lock entry. 148 * For example, spa_sync() (which holds SCL_CONFIG as reader) induces 149 * block allocations (SCL_ALLOC), which may require reading space maps 150 * from disk (dmu_read() -> zio_read() -> SCL_ZIO). 151 * 152 * The spa config locks cannot be normal rwlocks because we need the 153 * ability to hand off ownership. For example, SCL_ZIO is acquired 154 * by the issuing thread and later released by an interrupt thread. 155 * They do, however, obey the usual write-wanted semantics to prevent 156 * writer (i.e. system administrator) starvation. 157 * 158 * The lock acquisition rules are as follows: 159 * 160 * SCL_CONFIG 161 * Protects changes to the vdev tree topology, such as vdev 162 * add/remove/attach/detach. Protects the dirty config list 163 * (spa_config_dirty_list) and the set of spares and l2arc devices. 164 * 165 * SCL_STATE 166 * Protects changes to pool state and vdev state, such as vdev 167 * online/offline/fault/degrade/clear. Protects the dirty state list 168 * (spa_state_dirty_list) and global pool state (spa_state). 169 * 170 * SCL_ALLOC 171 * Protects changes to metaslab groups and classes. 172 * Held as reader by metaslab_alloc() and metaslab_claim(). 173 * 174 * SCL_ZIO 175 * Held by bp-level zios (those which have no io_vd upon entry) 176 * to prevent changes to the vdev tree. The bp-level zio implicitly 177 * protects all of its vdev child zios, which do not hold SCL_ZIO. 178 * 179 * SCL_FREE 180 * Protects changes to metaslab groups and classes. 181 * Held as reader by metaslab_free(). SCL_FREE is distinct from 182 * SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free 183 * blocks in zio_done() while another i/o that holds either 184 * SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete. 185 * 186 * SCL_VDEV 187 * Held as reader to prevent changes to the vdev tree during trivial 188 * inquiries such as bp_get_dsize(). SCL_VDEV is distinct from the 189 * other locks, and lower than all of them, to ensure that it's safe 190 * to acquire regardless of caller context. 191 * 192 * In addition, the following rules apply: 193 * 194 * (a) spa_props_lock protects pool properties, spa_config and spa_config_list. 195 * The lock ordering is SCL_CONFIG > spa_props_lock. 196 * 197 * (b) I/O operations on leaf vdevs. For any zio operation that takes 198 * an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(), 199 * or zio_write_phys() -- the caller must ensure that the config cannot 200 * cannot change in the interim, and that the vdev cannot be reopened. 201 * SCL_STATE as reader suffices for both. 202 * 203 * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit(). 204 * 205 * spa_vdev_enter() Acquire the namespace lock and the config lock 206 * for writing. 207 * 208 * spa_vdev_exit() Release the config lock, wait for all I/O 209 * to complete, sync the updated configs to the 210 * cache, and release the namespace lock. 211 * 212 * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit(). 213 * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual 214 * locking is, always, based on spa_namespace_lock and spa_config_lock[]. 215 * 216 * spa_rename() is also implemented within this file since is requires 217 * manipulation of the namespace. 218 */ 219 220 static avl_tree_t spa_namespace_avl; 221 kmutex_t spa_namespace_lock; 222 static kcondvar_t spa_namespace_cv; 223 static int spa_active_count; 224 int spa_max_replication_override = SPA_DVAS_PER_BP; 225 226 static kmutex_t spa_spare_lock; 227 static avl_tree_t spa_spare_avl; 228 static kmutex_t spa_l2cache_lock; 229 static avl_tree_t spa_l2cache_avl; 230 231 kmem_cache_t *spa_buffer_pool; 232 int spa_mode_global; 233 234 #ifdef ZFS_DEBUG 235 /* Everything except dprintf is on by default in debug builds */ 236 int zfs_flags = ~ZFS_DEBUG_DPRINTF; 237 #else 238 int zfs_flags = 0; 239 #endif 240 241 /* 242 * zfs_recover can be set to nonzero to attempt to recover from 243 * otherwise-fatal errors, typically caused by on-disk corruption. When 244 * set, calls to zfs_panic_recover() will turn into warning messages. 245 */ 246 int zfs_recover = 0; 247 248 249 /* 250 * ========================================================================== 251 * SPA config locking 252 * ========================================================================== 253 */ 254 static void 255 spa_config_lock_init(spa_t *spa) 256 { 257 for (int i = 0; i < SCL_LOCKS; i++) { 258 spa_config_lock_t *scl = &spa->spa_config_lock[i]; 259 mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL); 260 cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL); 261 refcount_create(&scl->scl_count); 262 scl->scl_writer = NULL; 263 scl->scl_write_wanted = 0; 264 } 265 } 266 267 static void 268 spa_config_lock_destroy(spa_t *spa) 269 { 270 for (int i = 0; i < SCL_LOCKS; i++) { 271 spa_config_lock_t *scl = &spa->spa_config_lock[i]; 272 mutex_destroy(&scl->scl_lock); 273 cv_destroy(&scl->scl_cv); 274 refcount_destroy(&scl->scl_count); 275 ASSERT(scl->scl_writer == NULL); 276 ASSERT(scl->scl_write_wanted == 0); 277 } 278 } 279 280 int 281 spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw) 282 { 283 for (int i = 0; i < SCL_LOCKS; i++) { 284 spa_config_lock_t *scl = &spa->spa_config_lock[i]; 285 if (!(locks & (1 << i))) 286 continue; 287 mutex_enter(&scl->scl_lock); 288 if (rw == RW_READER) { 289 if (scl->scl_writer || scl->scl_write_wanted) { 290 mutex_exit(&scl->scl_lock); 291 spa_config_exit(spa, locks ^ (1 << i), tag); 292 return (0); 293 } 294 } else { 295 ASSERT(scl->scl_writer != curthread); 296 if (!refcount_is_zero(&scl->scl_count)) { 297 mutex_exit(&scl->scl_lock); 298 spa_config_exit(spa, locks ^ (1 << i), tag); 299 return (0); 300 } 301 scl->scl_writer = curthread; 302 } 303 (void) refcount_add(&scl->scl_count, tag); 304 mutex_exit(&scl->scl_lock); 305 } 306 return (1); 307 } 308 309 void 310 spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw) 311 { 312 int wlocks_held = 0; 313 314 for (int i = 0; i < SCL_LOCKS; i++) { 315 spa_config_lock_t *scl = &spa->spa_config_lock[i]; 316 if (scl->scl_writer == curthread) 317 wlocks_held |= (1 << i); 318 if (!(locks & (1 << i))) 319 continue; 320 mutex_enter(&scl->scl_lock); 321 if (rw == RW_READER) { 322 while (scl->scl_writer || scl->scl_write_wanted) { 323 cv_wait(&scl->scl_cv, &scl->scl_lock); 324 } 325 } else { 326 ASSERT(scl->scl_writer != curthread); 327 while (!refcount_is_zero(&scl->scl_count)) { 328 scl->scl_write_wanted++; 329 cv_wait(&scl->scl_cv, &scl->scl_lock); 330 scl->scl_write_wanted--; 331 } 332 scl->scl_writer = curthread; 333 } 334 (void) refcount_add(&scl->scl_count, tag); 335 mutex_exit(&scl->scl_lock); 336 } 337 ASSERT(wlocks_held <= locks); 338 } 339 340 void 341 spa_config_exit(spa_t *spa, int locks, void *tag) 342 { 343 for (int i = SCL_LOCKS - 1; i >= 0; i--) { 344 spa_config_lock_t *scl = &spa->spa_config_lock[i]; 345 if (!(locks & (1 << i))) 346 continue; 347 mutex_enter(&scl->scl_lock); 348 ASSERT(!refcount_is_zero(&scl->scl_count)); 349 if (refcount_remove(&scl->scl_count, tag) == 0) { 350 ASSERT(scl->scl_writer == NULL || 351 scl->scl_writer == curthread); 352 scl->scl_writer = NULL; /* OK in either case */ 353 cv_broadcast(&scl->scl_cv); 354 } 355 mutex_exit(&scl->scl_lock); 356 } 357 } 358 359 int 360 spa_config_held(spa_t *spa, int locks, krw_t rw) 361 { 362 int locks_held = 0; 363 364 for (int i = 0; i < SCL_LOCKS; i++) { 365 spa_config_lock_t *scl = &spa->spa_config_lock[i]; 366 if (!(locks & (1 << i))) 367 continue; 368 if ((rw == RW_READER && !refcount_is_zero(&scl->scl_count)) || 369 (rw == RW_WRITER && scl->scl_writer == curthread)) 370 locks_held |= 1 << i; 371 } 372 373 return (locks_held); 374 } 375 376 /* 377 * ========================================================================== 378 * SPA namespace functions 379 * ========================================================================== 380 */ 381 382 /* 383 * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held. 384 * Returns NULL if no matching spa_t is found. 385 */ 386 spa_t * 387 spa_lookup(const char *name) 388 { 389 static spa_t search; /* spa_t is large; don't allocate on stack */ 390 spa_t *spa; 391 avl_index_t where; 392 char c; 393 char *cp; 394 395 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 396 397 /* 398 * If it's a full dataset name, figure out the pool name and 399 * just use that. 400 */ 401 cp = strpbrk(name, "/@"); 402 if (cp) { 403 c = *cp; 404 *cp = '\0'; 405 } 406 407 (void) strlcpy(search.spa_name, name, sizeof (search.spa_name)); 408 spa = avl_find(&spa_namespace_avl, &search, &where); 409 410 if (cp) 411 *cp = c; 412 413 return (spa); 414 } 415 416 /* 417 * Create an uninitialized spa_t with the given name. Requires 418 * spa_namespace_lock. The caller must ensure that the spa_t doesn't already 419 * exist by calling spa_lookup() first. 420 */ 421 spa_t * 422 spa_add(const char *name, nvlist_t *config, const char *altroot) 423 { 424 spa_t *spa; 425 spa_config_dirent_t *dp; 426 427 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 428 429 spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP); 430 431 mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL); 432 mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL); 433 mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL); 434 mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL); 435 mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL); 436 mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL); 437 mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL); 438 mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL); 439 mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL); 440 441 cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL); 442 cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL); 443 cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL); 444 cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL); 445 446 for (int t = 0; t < TXG_SIZE; t++) 447 bplist_init(&spa->spa_free_bplist[t]); 448 bplist_init(&spa->spa_deferred_bplist); 449 450 (void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name)); 451 spa->spa_state = POOL_STATE_UNINITIALIZED; 452 spa->spa_freeze_txg = UINT64_MAX; 453 spa->spa_final_txg = UINT64_MAX; 454 spa->spa_load_max_txg = UINT64_MAX; 455 spa->spa_proc = &p0; 456 spa->spa_proc_state = SPA_PROC_NONE; 457 458 refcount_create(&spa->spa_refcount); 459 spa_config_lock_init(spa); 460 461 avl_add(&spa_namespace_avl, spa); 462 463 /* 464 * Set the alternate root, if there is one. 465 */ 466 if (altroot) { 467 spa->spa_root = spa_strdup(altroot); 468 spa_active_count++; 469 } 470 471 /* 472 * Every pool starts with the default cachefile 473 */ 474 list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t), 475 offsetof(spa_config_dirent_t, scd_link)); 476 477 dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP); 478 dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path); 479 list_insert_head(&spa->spa_config_list, dp); 480 481 if (config != NULL) 482 VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0); 483 484 return (spa); 485 } 486 487 /* 488 * Removes a spa_t from the namespace, freeing up any memory used. Requires 489 * spa_namespace_lock. This is called only after the spa_t has been closed and 490 * deactivated. 491 */ 492 void 493 spa_remove(spa_t *spa) 494 { 495 spa_config_dirent_t *dp; 496 497 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 498 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 499 500 nvlist_free(spa->spa_config_splitting); 501 502 avl_remove(&spa_namespace_avl, spa); 503 cv_broadcast(&spa_namespace_cv); 504 505 if (spa->spa_root) { 506 spa_strfree(spa->spa_root); 507 spa_active_count--; 508 } 509 510 while ((dp = list_head(&spa->spa_config_list)) != NULL) { 511 list_remove(&spa->spa_config_list, dp); 512 if (dp->scd_path != NULL) 513 spa_strfree(dp->scd_path); 514 kmem_free(dp, sizeof (spa_config_dirent_t)); 515 } 516 517 list_destroy(&spa->spa_config_list); 518 519 spa_config_set(spa, NULL); 520 521 refcount_destroy(&spa->spa_refcount); 522 523 spa_config_lock_destroy(spa); 524 525 for (int t = 0; t < TXG_SIZE; t++) 526 bplist_fini(&spa->spa_free_bplist[t]); 527 bplist_fini(&spa->spa_deferred_bplist); 528 529 cv_destroy(&spa->spa_async_cv); 530 cv_destroy(&spa->spa_proc_cv); 531 cv_destroy(&spa->spa_scrub_io_cv); 532 cv_destroy(&spa->spa_suspend_cv); 533 534 mutex_destroy(&spa->spa_async_lock); 535 mutex_destroy(&spa->spa_errlist_lock); 536 mutex_destroy(&spa->spa_errlog_lock); 537 mutex_destroy(&spa->spa_history_lock); 538 mutex_destroy(&spa->spa_proc_lock); 539 mutex_destroy(&spa->spa_props_lock); 540 mutex_destroy(&spa->spa_scrub_lock); 541 mutex_destroy(&spa->spa_suspend_lock); 542 mutex_destroy(&spa->spa_vdev_top_lock); 543 544 kmem_free(spa, sizeof (spa_t)); 545 } 546 547 /* 548 * Given a pool, return the next pool in the namespace, or NULL if there is 549 * none. If 'prev' is NULL, return the first pool. 550 */ 551 spa_t * 552 spa_next(spa_t *prev) 553 { 554 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 555 556 if (prev) 557 return (AVL_NEXT(&spa_namespace_avl, prev)); 558 else 559 return (avl_first(&spa_namespace_avl)); 560 } 561 562 /* 563 * ========================================================================== 564 * SPA refcount functions 565 * ========================================================================== 566 */ 567 568 /* 569 * Add a reference to the given spa_t. Must have at least one reference, or 570 * have the namespace lock held. 571 */ 572 void 573 spa_open_ref(spa_t *spa, void *tag) 574 { 575 ASSERT(refcount_count(&spa->spa_refcount) >= spa->spa_minref || 576 MUTEX_HELD(&spa_namespace_lock)); 577 (void) refcount_add(&spa->spa_refcount, tag); 578 } 579 580 /* 581 * Remove a reference to the given spa_t. Must have at least one reference, or 582 * have the namespace lock held. 583 */ 584 void 585 spa_close(spa_t *spa, void *tag) 586 { 587 ASSERT(refcount_count(&spa->spa_refcount) > spa->spa_minref || 588 MUTEX_HELD(&spa_namespace_lock)); 589 (void) refcount_remove(&spa->spa_refcount, tag); 590 } 591 592 /* 593 * Check to see if the spa refcount is zero. Must be called with 594 * spa_namespace_lock held. We really compare against spa_minref, which is the 595 * number of references acquired when opening a pool 596 */ 597 boolean_t 598 spa_refcount_zero(spa_t *spa) 599 { 600 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 601 602 return (refcount_count(&spa->spa_refcount) == spa->spa_minref); 603 } 604 605 /* 606 * ========================================================================== 607 * SPA spare and l2cache tracking 608 * ========================================================================== 609 */ 610 611 /* 612 * Hot spares and cache devices are tracked using the same code below, 613 * for 'auxiliary' devices. 614 */ 615 616 typedef struct spa_aux { 617 uint64_t aux_guid; 618 uint64_t aux_pool; 619 avl_node_t aux_avl; 620 int aux_count; 621 } spa_aux_t; 622 623 static int 624 spa_aux_compare(const void *a, const void *b) 625 { 626 const spa_aux_t *sa = a; 627 const spa_aux_t *sb = b; 628 629 if (sa->aux_guid < sb->aux_guid) 630 return (-1); 631 else if (sa->aux_guid > sb->aux_guid) 632 return (1); 633 else 634 return (0); 635 } 636 637 void 638 spa_aux_add(vdev_t *vd, avl_tree_t *avl) 639 { 640 avl_index_t where; 641 spa_aux_t search; 642 spa_aux_t *aux; 643 644 search.aux_guid = vd->vdev_guid; 645 if ((aux = avl_find(avl, &search, &where)) != NULL) { 646 aux->aux_count++; 647 } else { 648 aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP); 649 aux->aux_guid = vd->vdev_guid; 650 aux->aux_count = 1; 651 avl_insert(avl, aux, where); 652 } 653 } 654 655 void 656 spa_aux_remove(vdev_t *vd, avl_tree_t *avl) 657 { 658 spa_aux_t search; 659 spa_aux_t *aux; 660 avl_index_t where; 661 662 search.aux_guid = vd->vdev_guid; 663 aux = avl_find(avl, &search, &where); 664 665 ASSERT(aux != NULL); 666 667 if (--aux->aux_count == 0) { 668 avl_remove(avl, aux); 669 kmem_free(aux, sizeof (spa_aux_t)); 670 } else if (aux->aux_pool == spa_guid(vd->vdev_spa)) { 671 aux->aux_pool = 0ULL; 672 } 673 } 674 675 boolean_t 676 spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl) 677 { 678 spa_aux_t search, *found; 679 680 search.aux_guid = guid; 681 found = avl_find(avl, &search, NULL); 682 683 if (pool) { 684 if (found) 685 *pool = found->aux_pool; 686 else 687 *pool = 0ULL; 688 } 689 690 if (refcnt) { 691 if (found) 692 *refcnt = found->aux_count; 693 else 694 *refcnt = 0; 695 } 696 697 return (found != NULL); 698 } 699 700 void 701 spa_aux_activate(vdev_t *vd, avl_tree_t *avl) 702 { 703 spa_aux_t search, *found; 704 avl_index_t where; 705 706 search.aux_guid = vd->vdev_guid; 707 found = avl_find(avl, &search, &where); 708 ASSERT(found != NULL); 709 ASSERT(found->aux_pool == 0ULL); 710 711 found->aux_pool = spa_guid(vd->vdev_spa); 712 } 713 714 /* 715 * Spares are tracked globally due to the following constraints: 716 * 717 * - A spare may be part of multiple pools. 718 * - A spare may be added to a pool even if it's actively in use within 719 * another pool. 720 * - A spare in use in any pool can only be the source of a replacement if 721 * the target is a spare in the same pool. 722 * 723 * We keep track of all spares on the system through the use of a reference 724 * counted AVL tree. When a vdev is added as a spare, or used as a replacement 725 * spare, then we bump the reference count in the AVL tree. In addition, we set 726 * the 'vdev_isspare' member to indicate that the device is a spare (active or 727 * inactive). When a spare is made active (used to replace a device in the 728 * pool), we also keep track of which pool its been made a part of. 729 * 730 * The 'spa_spare_lock' protects the AVL tree. These functions are normally 731 * called under the spa_namespace lock as part of vdev reconfiguration. The 732 * separate spare lock exists for the status query path, which does not need to 733 * be completely consistent with respect to other vdev configuration changes. 734 */ 735 736 static int 737 spa_spare_compare(const void *a, const void *b) 738 { 739 return (spa_aux_compare(a, b)); 740 } 741 742 void 743 spa_spare_add(vdev_t *vd) 744 { 745 mutex_enter(&spa_spare_lock); 746 ASSERT(!vd->vdev_isspare); 747 spa_aux_add(vd, &spa_spare_avl); 748 vd->vdev_isspare = B_TRUE; 749 mutex_exit(&spa_spare_lock); 750 } 751 752 void 753 spa_spare_remove(vdev_t *vd) 754 { 755 mutex_enter(&spa_spare_lock); 756 ASSERT(vd->vdev_isspare); 757 spa_aux_remove(vd, &spa_spare_avl); 758 vd->vdev_isspare = B_FALSE; 759 mutex_exit(&spa_spare_lock); 760 } 761 762 boolean_t 763 spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt) 764 { 765 boolean_t found; 766 767 mutex_enter(&spa_spare_lock); 768 found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl); 769 mutex_exit(&spa_spare_lock); 770 771 return (found); 772 } 773 774 void 775 spa_spare_activate(vdev_t *vd) 776 { 777 mutex_enter(&spa_spare_lock); 778 ASSERT(vd->vdev_isspare); 779 spa_aux_activate(vd, &spa_spare_avl); 780 mutex_exit(&spa_spare_lock); 781 } 782 783 /* 784 * Level 2 ARC devices are tracked globally for the same reasons as spares. 785 * Cache devices currently only support one pool per cache device, and so 786 * for these devices the aux reference count is currently unused beyond 1. 787 */ 788 789 static int 790 spa_l2cache_compare(const void *a, const void *b) 791 { 792 return (spa_aux_compare(a, b)); 793 } 794 795 void 796 spa_l2cache_add(vdev_t *vd) 797 { 798 mutex_enter(&spa_l2cache_lock); 799 ASSERT(!vd->vdev_isl2cache); 800 spa_aux_add(vd, &spa_l2cache_avl); 801 vd->vdev_isl2cache = B_TRUE; 802 mutex_exit(&spa_l2cache_lock); 803 } 804 805 void 806 spa_l2cache_remove(vdev_t *vd) 807 { 808 mutex_enter(&spa_l2cache_lock); 809 ASSERT(vd->vdev_isl2cache); 810 spa_aux_remove(vd, &spa_l2cache_avl); 811 vd->vdev_isl2cache = B_FALSE; 812 mutex_exit(&spa_l2cache_lock); 813 } 814 815 boolean_t 816 spa_l2cache_exists(uint64_t guid, uint64_t *pool) 817 { 818 boolean_t found; 819 820 mutex_enter(&spa_l2cache_lock); 821 found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl); 822 mutex_exit(&spa_l2cache_lock); 823 824 return (found); 825 } 826 827 void 828 spa_l2cache_activate(vdev_t *vd) 829 { 830 mutex_enter(&spa_l2cache_lock); 831 ASSERT(vd->vdev_isl2cache); 832 spa_aux_activate(vd, &spa_l2cache_avl); 833 mutex_exit(&spa_l2cache_lock); 834 } 835 836 /* 837 * ========================================================================== 838 * SPA vdev locking 839 * ========================================================================== 840 */ 841 842 /* 843 * Lock the given spa_t for the purpose of adding or removing a vdev. 844 * Grabs the global spa_namespace_lock plus the spa config lock for writing. 845 * It returns the next transaction group for the spa_t. 846 */ 847 uint64_t 848 spa_vdev_enter(spa_t *spa) 849 { 850 mutex_enter(&spa->spa_vdev_top_lock); 851 mutex_enter(&spa_namespace_lock); 852 return (spa_vdev_config_enter(spa)); 853 } 854 855 /* 856 * Internal implementation for spa_vdev_enter(). Used when a vdev 857 * operation requires multiple syncs (i.e. removing a device) while 858 * keeping the spa_namespace_lock held. 859 */ 860 uint64_t 861 spa_vdev_config_enter(spa_t *spa) 862 { 863 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 864 865 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER); 866 867 return (spa_last_synced_txg(spa) + 1); 868 } 869 870 /* 871 * Used in combination with spa_vdev_config_enter() to allow the syncing 872 * of multiple transactions without releasing the spa_namespace_lock. 873 */ 874 void 875 spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag) 876 { 877 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 878 879 int config_changed = B_FALSE; 880 881 ASSERT(txg > spa_last_synced_txg(spa)); 882 883 spa->spa_pending_vdev = NULL; 884 885 /* 886 * Reassess the DTLs. 887 */ 888 vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE); 889 890 /* 891 * If the config changed, notify the scrub thread that it must restart. 892 */ 893 if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) { 894 dsl_pool_scrub_restart(spa->spa_dsl_pool); 895 config_changed = B_TRUE; 896 spa->spa_config_generation++; 897 } 898 899 /* 900 * Verify the metaslab classes. 901 */ 902 ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0); 903 ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0); 904 905 spa_config_exit(spa, SCL_ALL, spa); 906 907 /* 908 * Panic the system if the specified tag requires it. This 909 * is useful for ensuring that configurations are updated 910 * transactionally. 911 */ 912 if (zio_injection_enabled) 913 zio_handle_panic_injection(spa, tag, 0); 914 915 /* 916 * Note: this txg_wait_synced() is important because it ensures 917 * that there won't be more than one config change per txg. 918 * This allows us to use the txg as the generation number. 919 */ 920 if (error == 0) 921 txg_wait_synced(spa->spa_dsl_pool, txg); 922 923 if (vd != NULL) { 924 ASSERT(!vd->vdev_detached || vd->vdev_dtl_smo.smo_object == 0); 925 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER); 926 vdev_free(vd); 927 spa_config_exit(spa, SCL_ALL, spa); 928 } 929 930 /* 931 * If the config changed, update the config cache. 932 */ 933 if (config_changed) 934 spa_config_sync(spa, B_FALSE, B_TRUE); 935 } 936 937 /* 938 * Unlock the spa_t after adding or removing a vdev. Besides undoing the 939 * locking of spa_vdev_enter(), we also want make sure the transactions have 940 * synced to disk, and then update the global configuration cache with the new 941 * information. 942 */ 943 int 944 spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error) 945 { 946 spa_vdev_config_exit(spa, vd, txg, error, FTAG); 947 mutex_exit(&spa_namespace_lock); 948 mutex_exit(&spa->spa_vdev_top_lock); 949 950 return (error); 951 } 952 953 /* 954 * Lock the given spa_t for the purpose of changing vdev state. 955 */ 956 void 957 spa_vdev_state_enter(spa_t *spa, int oplocks) 958 { 959 int locks = SCL_STATE_ALL | oplocks; 960 961 /* 962 * Root pools may need to read of the underlying devfs filesystem 963 * when opening up a vdev. Unfortunately if we're holding the 964 * SCL_ZIO lock it will result in a deadlock when we try to issue 965 * the read from the root filesystem. Instead we "prefetch" 966 * the associated vnodes that we need prior to opening the 967 * underlying devices and cache them so that we can prevent 968 * any I/O when we are doing the actual open. 969 */ 970 if (spa_is_root(spa)) { 971 int low = locks & ~(SCL_ZIO - 1); 972 int high = locks & ~low; 973 974 spa_config_enter(spa, high, spa, RW_WRITER); 975 vdev_hold(spa->spa_root_vdev); 976 spa_config_enter(spa, low, spa, RW_WRITER); 977 } else { 978 spa_config_enter(spa, locks, spa, RW_WRITER); 979 } 980 spa->spa_vdev_locks = locks; 981 } 982 983 int 984 spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error) 985 { 986 boolean_t config_changed = B_FALSE; 987 988 if (vd != NULL || error == 0) 989 vdev_dtl_reassess(vd ? vd->vdev_top : spa->spa_root_vdev, 990 0, 0, B_FALSE); 991 992 if (vd != NULL) { 993 vdev_state_dirty(vd->vdev_top); 994 config_changed = B_TRUE; 995 spa->spa_config_generation++; 996 } 997 998 if (spa_is_root(spa)) 999 vdev_rele(spa->spa_root_vdev); 1000 1001 ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL); 1002 spa_config_exit(spa, spa->spa_vdev_locks, spa); 1003 1004 /* 1005 * If anything changed, wait for it to sync. This ensures that, 1006 * from the system administrator's perspective, zpool(1M) commands 1007 * are synchronous. This is important for things like zpool offline: 1008 * when the command completes, you expect no further I/O from ZFS. 1009 */ 1010 if (vd != NULL) 1011 txg_wait_synced(spa->spa_dsl_pool, 0); 1012 1013 /* 1014 * If the config changed, update the config cache. 1015 */ 1016 if (config_changed) { 1017 mutex_enter(&spa_namespace_lock); 1018 spa_config_sync(spa, B_FALSE, B_TRUE); 1019 mutex_exit(&spa_namespace_lock); 1020 } 1021 1022 return (error); 1023 } 1024 1025 /* 1026 * ========================================================================== 1027 * Miscellaneous functions 1028 * ========================================================================== 1029 */ 1030 1031 /* 1032 * Rename a spa_t. 1033 */ 1034 int 1035 spa_rename(const char *name, const char *newname) 1036 { 1037 spa_t *spa; 1038 int err; 1039 1040 /* 1041 * Lookup the spa_t and grab the config lock for writing. We need to 1042 * actually open the pool so that we can sync out the necessary labels. 1043 * It's OK to call spa_open() with the namespace lock held because we 1044 * allow recursive calls for other reasons. 1045 */ 1046 mutex_enter(&spa_namespace_lock); 1047 if ((err = spa_open(name, &spa, FTAG)) != 0) { 1048 mutex_exit(&spa_namespace_lock); 1049 return (err); 1050 } 1051 1052 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1053 1054 avl_remove(&spa_namespace_avl, spa); 1055 (void) strlcpy(spa->spa_name, newname, sizeof (spa->spa_name)); 1056 avl_add(&spa_namespace_avl, spa); 1057 1058 /* 1059 * Sync all labels to disk with the new names by marking the root vdev 1060 * dirty and waiting for it to sync. It will pick up the new pool name 1061 * during the sync. 1062 */ 1063 vdev_config_dirty(spa->spa_root_vdev); 1064 1065 spa_config_exit(spa, SCL_ALL, FTAG); 1066 1067 txg_wait_synced(spa->spa_dsl_pool, 0); 1068 1069 /* 1070 * Sync the updated config cache. 1071 */ 1072 spa_config_sync(spa, B_FALSE, B_TRUE); 1073 1074 spa_close(spa, FTAG); 1075 1076 mutex_exit(&spa_namespace_lock); 1077 1078 return (0); 1079 } 1080 1081 1082 /* 1083 * Determine whether a pool with given pool_guid exists. If device_guid is 1084 * non-zero, determine whether the pool exists *and* contains a device with the 1085 * specified device_guid. 1086 */ 1087 boolean_t 1088 spa_guid_exists(uint64_t pool_guid, uint64_t device_guid) 1089 { 1090 spa_t *spa; 1091 avl_tree_t *t = &spa_namespace_avl; 1092 1093 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 1094 1095 for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) { 1096 if (spa->spa_state == POOL_STATE_UNINITIALIZED) 1097 continue; 1098 if (spa->spa_root_vdev == NULL) 1099 continue; 1100 if (spa_guid(spa) == pool_guid) { 1101 if (device_guid == 0) 1102 break; 1103 1104 if (vdev_lookup_by_guid(spa->spa_root_vdev, 1105 device_guid) != NULL) 1106 break; 1107 1108 /* 1109 * Check any devices we may be in the process of adding. 1110 */ 1111 if (spa->spa_pending_vdev) { 1112 if (vdev_lookup_by_guid(spa->spa_pending_vdev, 1113 device_guid) != NULL) 1114 break; 1115 } 1116 } 1117 } 1118 1119 return (spa != NULL); 1120 } 1121 1122 char * 1123 spa_strdup(const char *s) 1124 { 1125 size_t len; 1126 char *new; 1127 1128 len = strlen(s); 1129 new = kmem_alloc(len + 1, KM_SLEEP); 1130 bcopy(s, new, len); 1131 new[len] = '\0'; 1132 1133 return (new); 1134 } 1135 1136 void 1137 spa_strfree(char *s) 1138 { 1139 kmem_free(s, strlen(s) + 1); 1140 } 1141 1142 uint64_t 1143 spa_get_random(uint64_t range) 1144 { 1145 uint64_t r; 1146 1147 ASSERT(range != 0); 1148 1149 (void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t)); 1150 1151 return (r % range); 1152 } 1153 1154 uint64_t 1155 spa_generate_guid(spa_t *spa) 1156 { 1157 uint64_t guid = spa_get_random(-1ULL); 1158 1159 if (spa != NULL) { 1160 while (guid == 0 || spa_guid_exists(spa_guid(spa), guid)) 1161 guid = spa_get_random(-1ULL); 1162 } else { 1163 while (guid == 0 || spa_guid_exists(guid, 0)) 1164 guid = spa_get_random(-1ULL); 1165 } 1166 1167 return (guid); 1168 } 1169 1170 void 1171 sprintf_blkptr(char *buf, const blkptr_t *bp) 1172 { 1173 char *type = NULL; 1174 char *checksum = NULL; 1175 char *compress = NULL; 1176 1177 if (bp != NULL) { 1178 type = dmu_ot[BP_GET_TYPE(bp)].ot_name; 1179 checksum = zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name; 1180 compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name; 1181 } 1182 1183 SPRINTF_BLKPTR(snprintf, ' ', buf, bp, type, checksum, compress); 1184 } 1185 1186 void 1187 spa_freeze(spa_t *spa) 1188 { 1189 uint64_t freeze_txg = 0; 1190 1191 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1192 if (spa->spa_freeze_txg == UINT64_MAX) { 1193 freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE; 1194 spa->spa_freeze_txg = freeze_txg; 1195 } 1196 spa_config_exit(spa, SCL_ALL, FTAG); 1197 if (freeze_txg != 0) 1198 txg_wait_synced(spa_get_dsl(spa), freeze_txg); 1199 } 1200 1201 void 1202 zfs_panic_recover(const char *fmt, ...) 1203 { 1204 va_list adx; 1205 1206 va_start(adx, fmt); 1207 vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx); 1208 va_end(adx); 1209 } 1210 1211 /* 1212 * ========================================================================== 1213 * Accessor functions 1214 * ========================================================================== 1215 */ 1216 1217 boolean_t 1218 spa_shutting_down(spa_t *spa) 1219 { 1220 return (spa->spa_async_suspended); 1221 } 1222 1223 dsl_pool_t * 1224 spa_get_dsl(spa_t *spa) 1225 { 1226 return (spa->spa_dsl_pool); 1227 } 1228 1229 blkptr_t * 1230 spa_get_rootblkptr(spa_t *spa) 1231 { 1232 return (&spa->spa_ubsync.ub_rootbp); 1233 } 1234 1235 void 1236 spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp) 1237 { 1238 spa->spa_uberblock.ub_rootbp = *bp; 1239 } 1240 1241 void 1242 spa_altroot(spa_t *spa, char *buf, size_t buflen) 1243 { 1244 if (spa->spa_root == NULL) 1245 buf[0] = '\0'; 1246 else 1247 (void) strncpy(buf, spa->spa_root, buflen); 1248 } 1249 1250 int 1251 spa_sync_pass(spa_t *spa) 1252 { 1253 return (spa->spa_sync_pass); 1254 } 1255 1256 char * 1257 spa_name(spa_t *spa) 1258 { 1259 return (spa->spa_name); 1260 } 1261 1262 uint64_t 1263 spa_guid(spa_t *spa) 1264 { 1265 /* 1266 * If we fail to parse the config during spa_load(), we can go through 1267 * the error path (which posts an ereport) and end up here with no root 1268 * vdev. We stash the original pool guid in 'spa_load_guid' to handle 1269 * this case. 1270 */ 1271 if (spa->spa_root_vdev != NULL) 1272 return (spa->spa_root_vdev->vdev_guid); 1273 else 1274 return (spa->spa_load_guid); 1275 } 1276 1277 uint64_t 1278 spa_last_synced_txg(spa_t *spa) 1279 { 1280 return (spa->spa_ubsync.ub_txg); 1281 } 1282 1283 uint64_t 1284 spa_first_txg(spa_t *spa) 1285 { 1286 return (spa->spa_first_txg); 1287 } 1288 1289 uint64_t 1290 spa_syncing_txg(spa_t *spa) 1291 { 1292 return (spa->spa_syncing_txg); 1293 } 1294 1295 pool_state_t 1296 spa_state(spa_t *spa) 1297 { 1298 return (spa->spa_state); 1299 } 1300 1301 spa_load_state_t 1302 spa_load_state(spa_t *spa) 1303 { 1304 return (spa->spa_load_state); 1305 } 1306 1307 uint64_t 1308 spa_freeze_txg(spa_t *spa) 1309 { 1310 return (spa->spa_freeze_txg); 1311 } 1312 1313 /* ARGSUSED */ 1314 uint64_t 1315 spa_get_asize(spa_t *spa, uint64_t lsize) 1316 { 1317 /* 1318 * The worst case is single-sector max-parity RAID-Z blocks, in which 1319 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1) 1320 * times the size; so just assume that. Add to this the fact that 1321 * we can have up to 3 DVAs per bp, and one more factor of 2 because 1322 * the block may be dittoed with up to 3 DVAs by ddt_sync(). 1323 */ 1324 return (lsize * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2); 1325 } 1326 1327 uint64_t 1328 spa_get_dspace(spa_t *spa) 1329 { 1330 return (spa->spa_dspace); 1331 } 1332 1333 void 1334 spa_update_dspace(spa_t *spa) 1335 { 1336 spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) + 1337 ddt_get_dedup_dspace(spa); 1338 } 1339 1340 /* 1341 * Return the failure mode that has been set to this pool. The default 1342 * behavior will be to block all I/Os when a complete failure occurs. 1343 */ 1344 uint8_t 1345 spa_get_failmode(spa_t *spa) 1346 { 1347 return (spa->spa_failmode); 1348 } 1349 1350 boolean_t 1351 spa_suspended(spa_t *spa) 1352 { 1353 return (spa->spa_suspended); 1354 } 1355 1356 uint64_t 1357 spa_version(spa_t *spa) 1358 { 1359 return (spa->spa_ubsync.ub_version); 1360 } 1361 1362 boolean_t 1363 spa_deflate(spa_t *spa) 1364 { 1365 return (spa->spa_deflate); 1366 } 1367 1368 metaslab_class_t * 1369 spa_normal_class(spa_t *spa) 1370 { 1371 return (spa->spa_normal_class); 1372 } 1373 1374 metaslab_class_t * 1375 spa_log_class(spa_t *spa) 1376 { 1377 return (spa->spa_log_class); 1378 } 1379 1380 int 1381 spa_max_replication(spa_t *spa) 1382 { 1383 /* 1384 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to 1385 * handle BPs with more than one DVA allocated. Set our max 1386 * replication level accordingly. 1387 */ 1388 if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS) 1389 return (1); 1390 return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override)); 1391 } 1392 1393 uint64_t 1394 dva_get_dsize_sync(spa_t *spa, const dva_t *dva) 1395 { 1396 uint64_t asize = DVA_GET_ASIZE(dva); 1397 uint64_t dsize = asize; 1398 1399 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 1400 1401 if (asize != 0 && spa->spa_deflate) { 1402 vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva)); 1403 dsize = (asize >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio; 1404 } 1405 1406 return (dsize); 1407 } 1408 1409 uint64_t 1410 bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp) 1411 { 1412 uint64_t dsize = 0; 1413 1414 for (int d = 0; d < SPA_DVAS_PER_BP; d++) 1415 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]); 1416 1417 return (dsize); 1418 } 1419 1420 uint64_t 1421 bp_get_dsize(spa_t *spa, const blkptr_t *bp) 1422 { 1423 uint64_t dsize = 0; 1424 1425 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 1426 1427 for (int d = 0; d < SPA_DVAS_PER_BP; d++) 1428 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]); 1429 1430 spa_config_exit(spa, SCL_VDEV, FTAG); 1431 1432 return (dsize); 1433 } 1434 1435 /* 1436 * ========================================================================== 1437 * Initialization and Termination 1438 * ========================================================================== 1439 */ 1440 1441 static int 1442 spa_name_compare(const void *a1, const void *a2) 1443 { 1444 const spa_t *s1 = a1; 1445 const spa_t *s2 = a2; 1446 int s; 1447 1448 s = strcmp(s1->spa_name, s2->spa_name); 1449 if (s > 0) 1450 return (1); 1451 if (s < 0) 1452 return (-1); 1453 return (0); 1454 } 1455 1456 int 1457 spa_busy(void) 1458 { 1459 return (spa_active_count); 1460 } 1461 1462 void 1463 spa_boot_init() 1464 { 1465 spa_config_load(); 1466 } 1467 1468 void 1469 spa_init(int mode) 1470 { 1471 mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL); 1472 mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL); 1473 mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL); 1474 cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL); 1475 1476 avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t), 1477 offsetof(spa_t, spa_avl)); 1478 1479 avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t), 1480 offsetof(spa_aux_t, aux_avl)); 1481 1482 avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t), 1483 offsetof(spa_aux_t, aux_avl)); 1484 1485 spa_mode_global = mode; 1486 1487 refcount_init(); 1488 unique_init(); 1489 zio_init(); 1490 dmu_init(); 1491 zil_init(); 1492 vdev_cache_stat_init(); 1493 zfs_prop_init(); 1494 zpool_prop_init(); 1495 spa_config_load(); 1496 l2arc_start(); 1497 } 1498 1499 void 1500 spa_fini(void) 1501 { 1502 l2arc_stop(); 1503 1504 spa_evict_all(); 1505 1506 vdev_cache_stat_fini(); 1507 zil_fini(); 1508 dmu_fini(); 1509 zio_fini(); 1510 unique_fini(); 1511 refcount_fini(); 1512 1513 avl_destroy(&spa_namespace_avl); 1514 avl_destroy(&spa_spare_avl); 1515 avl_destroy(&spa_l2cache_avl); 1516 1517 cv_destroy(&spa_namespace_cv); 1518 mutex_destroy(&spa_namespace_lock); 1519 mutex_destroy(&spa_spare_lock); 1520 mutex_destroy(&spa_l2cache_lock); 1521 } 1522 1523 /* 1524 * Return whether this pool has slogs. No locking needed. 1525 * It's not a problem if the wrong answer is returned as it's only for 1526 * performance and not correctness 1527 */ 1528 boolean_t 1529 spa_has_slogs(spa_t *spa) 1530 { 1531 return (spa->spa_log_class->mc_rotor != NULL); 1532 } 1533 1534 spa_log_state_t 1535 spa_get_log_state(spa_t *spa) 1536 { 1537 return (spa->spa_log_state); 1538 } 1539 1540 void 1541 spa_set_log_state(spa_t *spa, spa_log_state_t state) 1542 { 1543 spa->spa_log_state = state; 1544 } 1545 1546 boolean_t 1547 spa_is_root(spa_t *spa) 1548 { 1549 return (spa->spa_is_root); 1550 } 1551 1552 boolean_t 1553 spa_writeable(spa_t *spa) 1554 { 1555 return (!!(spa->spa_mode & FWRITE)); 1556 } 1557 1558 int 1559 spa_mode(spa_t *spa) 1560 { 1561 return (spa->spa_mode); 1562 } 1563 1564 uint64_t 1565 spa_bootfs(spa_t *spa) 1566 { 1567 return (spa->spa_bootfs); 1568 } 1569 1570 uint64_t 1571 spa_delegation(spa_t *spa) 1572 { 1573 return (spa->spa_delegation); 1574 } 1575 1576 objset_t * 1577 spa_meta_objset(spa_t *spa) 1578 { 1579 return (spa->spa_meta_objset); 1580 } 1581 1582 enum zio_checksum 1583 spa_dedup_checksum(spa_t *spa) 1584 { 1585 return (spa->spa_dedup_checksum); 1586 } 1587