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