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 2008 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 #include <sys/zfs_context.h> 29 #include <sys/spa_impl.h> 30 #include <sys/zio.h> 31 #include <sys/zio_checksum.h> 32 #include <sys/zio_compress.h> 33 #include <sys/dmu.h> 34 #include <sys/dmu_tx.h> 35 #include <sys/zap.h> 36 #include <sys/zil.h> 37 #include <sys/vdev_impl.h> 38 #include <sys/metaslab.h> 39 #include <sys/uberblock_impl.h> 40 #include <sys/txg.h> 41 #include <sys/avl.h> 42 #include <sys/unique.h> 43 #include <sys/dsl_pool.h> 44 #include <sys/dsl_dir.h> 45 #include <sys/dsl_prop.h> 46 #include <sys/fs/zfs.h> 47 #include <sys/metaslab_impl.h> 48 #include "zfs_prop.h" 49 50 /* 51 * SPA locking 52 * 53 * There are four basic locks for managing spa_t structures: 54 * 55 * spa_namespace_lock (global mutex) 56 * 57 * This lock must be acquired to do any of the following: 58 * 59 * - Lookup a spa_t by name 60 * - Add or remove a spa_t from the namespace 61 * - Increase spa_refcount from non-zero 62 * - Check if spa_refcount is zero 63 * - Rename a spa_t 64 * - add/remove/attach/detach devices 65 * - Held for the duration of create/destroy/import/export 66 * 67 * It does not need to handle recursion. A create or destroy may 68 * reference objects (files or zvols) in other pools, but by 69 * definition they must have an existing reference, and will never need 70 * to lookup a spa_t by name. 71 * 72 * spa_refcount (per-spa refcount_t protected by mutex) 73 * 74 * This reference count keep track of any active users of the spa_t. The 75 * spa_t cannot be destroyed or freed while this is non-zero. Internally, 76 * the refcount is never really 'zero' - opening a pool implicitly keeps 77 * some references in the DMU. Internally we check against spa_minref, but 78 * present the image of a zero/non-zero value to consumers. 79 * 80 * spa_config_lock (per-spa read-priority rwlock) 81 * 82 * This protects the spa_t from config changes, and must be held in 83 * the following circumstances: 84 * 85 * - RW_READER to perform I/O to the spa 86 * - RW_WRITER to change the vdev config 87 * 88 * spa_config_cache_lock (per-spa mutex) 89 * 90 * This mutex prevents the spa_config nvlist from being updated. No 91 * other locks are required to obtain this lock, although implicitly you 92 * must have the namespace lock or non-zero refcount to have any kind 93 * of spa_t pointer at all. 94 * 95 * The locking order is fairly straightforward: 96 * 97 * spa_namespace_lock -> spa_refcount 98 * 99 * The namespace lock must be acquired to increase the refcount from 0 100 * or to check if it is zero. 101 * 102 * spa_refcount -> spa_config_lock 103 * 104 * There must be at least one valid reference on the spa_t to acquire 105 * the config lock. 106 * 107 * spa_namespace_lock -> spa_config_lock 108 * 109 * The namespace lock must always be taken before the config lock. 110 * 111 * 112 * The spa_namespace_lock and spa_config_cache_lock can be acquired directly and 113 * are globally visible. 114 * 115 * The namespace is manipulated using the following functions, all which require 116 * the spa_namespace_lock to be held. 117 * 118 * spa_lookup() Lookup a spa_t by name. 119 * 120 * spa_add() Create a new spa_t in the namespace. 121 * 122 * spa_remove() Remove a spa_t from the namespace. This also 123 * frees up any memory associated with the spa_t. 124 * 125 * spa_next() Returns the next spa_t in the system, or the 126 * first if NULL is passed. 127 * 128 * spa_evict_all() Shutdown and remove all spa_t structures in 129 * the system. 130 * 131 * spa_guid_exists() Determine whether a pool/device guid exists. 132 * 133 * The spa_refcount is manipulated using the following functions: 134 * 135 * spa_open_ref() Adds a reference to the given spa_t. Must be 136 * called with spa_namespace_lock held if the 137 * refcount is currently zero. 138 * 139 * spa_close() Remove a reference from the spa_t. This will 140 * not free the spa_t or remove it from the 141 * namespace. No locking is required. 142 * 143 * spa_refcount_zero() Returns true if the refcount is currently 144 * zero. Must be called with spa_namespace_lock 145 * held. 146 * 147 * The spa_config_lock is a form of rwlock. It must be held as RW_READER 148 * to perform I/O to the pool, and as RW_WRITER to change the vdev config. 149 * The spa_config_lock is manipulated with spa_config_{enter,exit,held}(). 150 * 151 * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit(). 152 * 153 * spa_vdev_enter() Acquire the namespace lock and the config lock 154 * for writing. 155 * 156 * spa_vdev_exit() Release the config lock, wait for all I/O 157 * to complete, sync the updated configs to the 158 * cache, and release the namespace lock. 159 * 160 * The spa_name() function also requires either the spa_namespace_lock 161 * or the spa_config_lock, as both are needed to do a rename. spa_rename() is 162 * also implemented within this file since is requires manipulation of the 163 * namespace. 164 */ 165 166 static avl_tree_t spa_namespace_avl; 167 kmutex_t spa_namespace_lock; 168 static kcondvar_t spa_namespace_cv; 169 static int spa_active_count; 170 int spa_max_replication_override = SPA_DVAS_PER_BP; 171 172 static kmutex_t spa_spare_lock; 173 static avl_tree_t spa_spare_avl; 174 static kmutex_t spa_l2cache_lock; 175 static avl_tree_t spa_l2cache_avl; 176 177 kmem_cache_t *spa_buffer_pool; 178 int spa_mode; 179 180 #ifdef ZFS_DEBUG 181 /* Everything except dprintf is on by default in debug builds */ 182 int zfs_flags = ~ZFS_DEBUG_DPRINTF; 183 #else 184 int zfs_flags = 0; 185 #endif 186 187 /* 188 * zfs_recover can be set to nonzero to attempt to recover from 189 * otherwise-fatal errors, typically caused by on-disk corruption. When 190 * set, calls to zfs_panic_recover() will turn into warning messages. 191 */ 192 int zfs_recover = 0; 193 194 195 /* 196 * ========================================================================== 197 * SPA config locking 198 * ========================================================================== 199 */ 200 static void 201 spa_config_lock_init(spa_config_lock_t *scl) 202 { 203 mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL); 204 scl->scl_writer = NULL; 205 cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL); 206 refcount_create(&scl->scl_count); 207 } 208 209 static void 210 spa_config_lock_destroy(spa_config_lock_t *scl) 211 { 212 mutex_destroy(&scl->scl_lock); 213 ASSERT(scl->scl_writer == NULL); 214 cv_destroy(&scl->scl_cv); 215 refcount_destroy(&scl->scl_count); 216 } 217 218 void 219 spa_config_enter(spa_t *spa, krw_t rw, void *tag) 220 { 221 spa_config_lock_t *scl = &spa->spa_config_lock; 222 223 mutex_enter(&scl->scl_lock); 224 225 if (rw == RW_READER) { 226 while (scl->scl_writer != NULL && scl->scl_writer != curthread) 227 cv_wait(&scl->scl_cv, &scl->scl_lock); 228 } else { 229 while (!refcount_is_zero(&scl->scl_count) && 230 scl->scl_writer != curthread) 231 cv_wait(&scl->scl_cv, &scl->scl_lock); 232 scl->scl_writer = curthread; 233 } 234 235 (void) refcount_add(&scl->scl_count, tag); 236 237 mutex_exit(&scl->scl_lock); 238 } 239 240 void 241 spa_config_exit(spa_t *spa, void *tag) 242 { 243 spa_config_lock_t *scl = &spa->spa_config_lock; 244 245 mutex_enter(&scl->scl_lock); 246 247 ASSERT(!refcount_is_zero(&scl->scl_count)); 248 249 if (refcount_remove(&scl->scl_count, tag) == 0) { 250 cv_broadcast(&scl->scl_cv); 251 ASSERT(scl->scl_writer == NULL || scl->scl_writer == curthread); 252 scl->scl_writer = NULL; /* OK in either case */ 253 } 254 255 mutex_exit(&scl->scl_lock); 256 } 257 258 boolean_t 259 spa_config_held(spa_t *spa, krw_t rw) 260 { 261 spa_config_lock_t *scl = &spa->spa_config_lock; 262 263 if (rw == RW_READER) 264 return (!refcount_is_zero(&scl->scl_count)); 265 else 266 return (scl->scl_writer == curthread); 267 } 268 269 /* 270 * ========================================================================== 271 * SPA namespace functions 272 * ========================================================================== 273 */ 274 275 /* 276 * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held. 277 * Returns NULL if no matching spa_t is found. 278 */ 279 spa_t * 280 spa_lookup(const char *name) 281 { 282 spa_t search, *spa; 283 avl_index_t where; 284 char c; 285 char *cp; 286 287 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 288 289 /* 290 * If it's a full dataset name, figure out the pool name and 291 * just use that. 292 */ 293 cp = strpbrk(name, "/@"); 294 if (cp) { 295 c = *cp; 296 *cp = '\0'; 297 } 298 299 search.spa_name = (char *)name; 300 spa = avl_find(&spa_namespace_avl, &search, &where); 301 302 if (cp) 303 *cp = c; 304 305 return (spa); 306 } 307 308 /* 309 * Create an uninitialized spa_t with the given name. Requires 310 * spa_namespace_lock. The caller must ensure that the spa_t doesn't already 311 * exist by calling spa_lookup() first. 312 */ 313 spa_t * 314 spa_add(const char *name, const char *altroot) 315 { 316 spa_t *spa; 317 spa_config_dirent_t *dp; 318 319 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 320 321 spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP); 322 323 rw_init(&spa->spa_traverse_lock, NULL, RW_DEFAULT, NULL); 324 325 mutex_init(&spa->spa_uberblock_lock, NULL, MUTEX_DEFAULT, NULL); 326 mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL); 327 mutex_init(&spa->spa_config_cache_lock, NULL, MUTEX_DEFAULT, NULL); 328 mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL); 329 mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL); 330 mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL); 331 mutex_init(&spa->spa_sync_bplist.bpl_lock, NULL, MUTEX_DEFAULT, NULL); 332 mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL); 333 mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL); 334 335 cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL); 336 cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL); 337 338 spa->spa_name = spa_strdup(name); 339 spa->spa_state = POOL_STATE_UNINITIALIZED; 340 spa->spa_freeze_txg = UINT64_MAX; 341 spa->spa_final_txg = UINT64_MAX; 342 343 refcount_create(&spa->spa_refcount); 344 spa_config_lock_init(&spa->spa_config_lock); 345 346 avl_add(&spa_namespace_avl, spa); 347 348 mutex_init(&spa->spa_zio_lock, NULL, MUTEX_DEFAULT, NULL); 349 350 /* 351 * Set the alternate root, if there is one. 352 */ 353 if (altroot) { 354 spa->spa_root = spa_strdup(altroot); 355 spa_active_count++; 356 } 357 358 /* 359 * Every pool starts with the default cachefile 360 */ 361 list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t), 362 offsetof(spa_config_dirent_t, scd_link)); 363 364 dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP); 365 dp->scd_path = spa_strdup(spa_config_path); 366 list_insert_head(&spa->spa_config_list, dp); 367 368 return (spa); 369 } 370 371 /* 372 * Removes a spa_t from the namespace, freeing up any memory used. Requires 373 * spa_namespace_lock. This is called only after the spa_t has been closed and 374 * deactivated. 375 */ 376 void 377 spa_remove(spa_t *spa) 378 { 379 spa_config_dirent_t *dp; 380 381 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 382 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 383 384 avl_remove(&spa_namespace_avl, spa); 385 cv_broadcast(&spa_namespace_cv); 386 387 if (spa->spa_root) { 388 spa_strfree(spa->spa_root); 389 spa_active_count--; 390 } 391 392 if (spa->spa_name) 393 spa_strfree(spa->spa_name); 394 395 while ((dp = list_head(&spa->spa_config_list)) != NULL) { 396 list_remove(&spa->spa_config_list, dp); 397 if (dp->scd_path != NULL) 398 spa_strfree(dp->scd_path); 399 kmem_free(dp, sizeof (spa_config_dirent_t)); 400 } 401 402 list_destroy(&spa->spa_config_list); 403 404 spa_config_set(spa, NULL); 405 406 refcount_destroy(&spa->spa_refcount); 407 408 spa_config_lock_destroy(&spa->spa_config_lock); 409 410 rw_destroy(&spa->spa_traverse_lock); 411 412 cv_destroy(&spa->spa_async_cv); 413 cv_destroy(&spa->spa_scrub_io_cv); 414 415 mutex_destroy(&spa->spa_uberblock_lock); 416 mutex_destroy(&spa->spa_async_lock); 417 mutex_destroy(&spa->spa_config_cache_lock); 418 mutex_destroy(&spa->spa_scrub_lock); 419 mutex_destroy(&spa->spa_errlog_lock); 420 mutex_destroy(&spa->spa_errlist_lock); 421 mutex_destroy(&spa->spa_sync_bplist.bpl_lock); 422 mutex_destroy(&spa->spa_history_lock); 423 mutex_destroy(&spa->spa_props_lock); 424 mutex_destroy(&spa->spa_zio_lock); 425 426 kmem_free(spa, sizeof (spa_t)); 427 } 428 429 /* 430 * Given a pool, return the next pool in the namespace, or NULL if there is 431 * none. If 'prev' is NULL, return the first pool. 432 */ 433 spa_t * 434 spa_next(spa_t *prev) 435 { 436 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 437 438 if (prev) 439 return (AVL_NEXT(&spa_namespace_avl, prev)); 440 else 441 return (avl_first(&spa_namespace_avl)); 442 } 443 444 /* 445 * ========================================================================== 446 * SPA refcount functions 447 * ========================================================================== 448 */ 449 450 /* 451 * Add a reference to the given spa_t. Must have at least one reference, or 452 * have the namespace lock held. 453 */ 454 void 455 spa_open_ref(spa_t *spa, void *tag) 456 { 457 ASSERT(refcount_count(&spa->spa_refcount) >= spa->spa_minref || 458 MUTEX_HELD(&spa_namespace_lock)); 459 (void) refcount_add(&spa->spa_refcount, tag); 460 } 461 462 /* 463 * Remove a reference to the given spa_t. Must have at least one reference, or 464 * have the namespace lock held. 465 */ 466 void 467 spa_close(spa_t *spa, void *tag) 468 { 469 ASSERT(refcount_count(&spa->spa_refcount) > spa->spa_minref || 470 MUTEX_HELD(&spa_namespace_lock)); 471 (void) refcount_remove(&spa->spa_refcount, tag); 472 } 473 474 /* 475 * Check to see if the spa refcount is zero. Must be called with 476 * spa_namespace_lock held. We really compare against spa_minref, which is the 477 * number of references acquired when opening a pool 478 */ 479 boolean_t 480 spa_refcount_zero(spa_t *spa) 481 { 482 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 483 484 return (refcount_count(&spa->spa_refcount) == spa->spa_minref); 485 } 486 487 /* 488 * ========================================================================== 489 * SPA spare and l2cache tracking 490 * ========================================================================== 491 */ 492 493 /* 494 * Hot spares and cache devices are tracked using the same code below, 495 * for 'auxiliary' devices. 496 */ 497 498 typedef struct spa_aux { 499 uint64_t aux_guid; 500 uint64_t aux_pool; 501 avl_node_t aux_avl; 502 int aux_count; 503 } spa_aux_t; 504 505 static int 506 spa_aux_compare(const void *a, const void *b) 507 { 508 const spa_aux_t *sa = a; 509 const spa_aux_t *sb = b; 510 511 if (sa->aux_guid < sb->aux_guid) 512 return (-1); 513 else if (sa->aux_guid > sb->aux_guid) 514 return (1); 515 else 516 return (0); 517 } 518 519 void 520 spa_aux_add(vdev_t *vd, avl_tree_t *avl) 521 { 522 avl_index_t where; 523 spa_aux_t search; 524 spa_aux_t *aux; 525 526 search.aux_guid = vd->vdev_guid; 527 if ((aux = avl_find(avl, &search, &where)) != NULL) { 528 aux->aux_count++; 529 } else { 530 aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP); 531 aux->aux_guid = vd->vdev_guid; 532 aux->aux_count = 1; 533 avl_insert(avl, aux, where); 534 } 535 } 536 537 void 538 spa_aux_remove(vdev_t *vd, avl_tree_t *avl) 539 { 540 spa_aux_t search; 541 spa_aux_t *aux; 542 avl_index_t where; 543 544 search.aux_guid = vd->vdev_guid; 545 aux = avl_find(avl, &search, &where); 546 547 ASSERT(aux != NULL); 548 549 if (--aux->aux_count == 0) { 550 avl_remove(avl, aux); 551 kmem_free(aux, sizeof (spa_aux_t)); 552 } else if (aux->aux_pool == spa_guid(vd->vdev_spa)) { 553 aux->aux_pool = 0ULL; 554 } 555 } 556 557 boolean_t 558 spa_aux_exists(uint64_t guid, uint64_t *pool, avl_tree_t *avl) 559 { 560 spa_aux_t search, *found; 561 avl_index_t where; 562 563 search.aux_guid = guid; 564 found = avl_find(avl, &search, &where); 565 566 if (pool) { 567 if (found) 568 *pool = found->aux_pool; 569 else 570 *pool = 0ULL; 571 } 572 573 return (found != NULL); 574 } 575 576 void 577 spa_aux_activate(vdev_t *vd, avl_tree_t *avl) 578 { 579 spa_aux_t search, *found; 580 avl_index_t where; 581 582 search.aux_guid = vd->vdev_guid; 583 found = avl_find(avl, &search, &where); 584 ASSERT(found != NULL); 585 ASSERT(found->aux_pool == 0ULL); 586 587 found->aux_pool = spa_guid(vd->vdev_spa); 588 } 589 590 /* 591 * Spares are tracked globally due to the following constraints: 592 * 593 * - A spare may be part of multiple pools. 594 * - A spare may be added to a pool even if it's actively in use within 595 * another pool. 596 * - A spare in use in any pool can only be the source of a replacement if 597 * the target is a spare in the same pool. 598 * 599 * We keep track of all spares on the system through the use of a reference 600 * counted AVL tree. When a vdev is added as a spare, or used as a replacement 601 * spare, then we bump the reference count in the AVL tree. In addition, we set 602 * the 'vdev_isspare' member to indicate that the device is a spare (active or 603 * inactive). When a spare is made active (used to replace a device in the 604 * pool), we also keep track of which pool its been made a part of. 605 * 606 * The 'spa_spare_lock' protects the AVL tree. These functions are normally 607 * called under the spa_namespace lock as part of vdev reconfiguration. The 608 * separate spare lock exists for the status query path, which does not need to 609 * be completely consistent with respect to other vdev configuration changes. 610 */ 611 612 static int 613 spa_spare_compare(const void *a, const void *b) 614 { 615 return (spa_aux_compare(a, b)); 616 } 617 618 void 619 spa_spare_add(vdev_t *vd) 620 { 621 mutex_enter(&spa_spare_lock); 622 ASSERT(!vd->vdev_isspare); 623 spa_aux_add(vd, &spa_spare_avl); 624 vd->vdev_isspare = B_TRUE; 625 mutex_exit(&spa_spare_lock); 626 } 627 628 void 629 spa_spare_remove(vdev_t *vd) 630 { 631 mutex_enter(&spa_spare_lock); 632 ASSERT(vd->vdev_isspare); 633 spa_aux_remove(vd, &spa_spare_avl); 634 vd->vdev_isspare = B_FALSE; 635 mutex_exit(&spa_spare_lock); 636 } 637 638 boolean_t 639 spa_spare_exists(uint64_t guid, uint64_t *pool) 640 { 641 boolean_t found; 642 643 mutex_enter(&spa_spare_lock); 644 found = spa_aux_exists(guid, pool, &spa_spare_avl); 645 mutex_exit(&spa_spare_lock); 646 647 return (found); 648 } 649 650 void 651 spa_spare_activate(vdev_t *vd) 652 { 653 mutex_enter(&spa_spare_lock); 654 ASSERT(vd->vdev_isspare); 655 spa_aux_activate(vd, &spa_spare_avl); 656 mutex_exit(&spa_spare_lock); 657 } 658 659 /* 660 * Level 2 ARC devices are tracked globally for the same reasons as spares. 661 * Cache devices currently only support one pool per cache device, and so 662 * for these devices the aux reference count is currently unused beyond 1. 663 */ 664 665 static int 666 spa_l2cache_compare(const void *a, const void *b) 667 { 668 return (spa_aux_compare(a, b)); 669 } 670 671 void 672 spa_l2cache_add(vdev_t *vd) 673 { 674 mutex_enter(&spa_l2cache_lock); 675 ASSERT(!vd->vdev_isl2cache); 676 spa_aux_add(vd, &spa_l2cache_avl); 677 vd->vdev_isl2cache = B_TRUE; 678 mutex_exit(&spa_l2cache_lock); 679 } 680 681 void 682 spa_l2cache_remove(vdev_t *vd) 683 { 684 mutex_enter(&spa_l2cache_lock); 685 ASSERT(vd->vdev_isl2cache); 686 spa_aux_remove(vd, &spa_l2cache_avl); 687 vd->vdev_isl2cache = B_FALSE; 688 mutex_exit(&spa_l2cache_lock); 689 } 690 691 boolean_t 692 spa_l2cache_exists(uint64_t guid, uint64_t *pool) 693 { 694 boolean_t found; 695 696 mutex_enter(&spa_l2cache_lock); 697 found = spa_aux_exists(guid, pool, &spa_l2cache_avl); 698 mutex_exit(&spa_l2cache_lock); 699 700 return (found); 701 } 702 703 void 704 spa_l2cache_activate(vdev_t *vd) 705 { 706 mutex_enter(&spa_l2cache_lock); 707 ASSERT(vd->vdev_isl2cache); 708 spa_aux_activate(vd, &spa_l2cache_avl); 709 mutex_exit(&spa_l2cache_lock); 710 } 711 712 void 713 spa_l2cache_space_update(vdev_t *vd, int64_t space, int64_t alloc) 714 { 715 vdev_space_update(vd, space, alloc, B_FALSE); 716 } 717 718 /* 719 * ========================================================================== 720 * SPA vdev locking 721 * ========================================================================== 722 */ 723 724 /* 725 * Lock the given spa_t for the purpose of adding or removing a vdev. 726 * Grabs the global spa_namespace_lock plus the spa config lock for writing. 727 * It returns the next transaction group for the spa_t. 728 */ 729 uint64_t 730 spa_vdev_enter(spa_t *spa) 731 { 732 mutex_enter(&spa_namespace_lock); 733 734 spa_config_enter(spa, RW_WRITER, spa); 735 736 return (spa_last_synced_txg(spa) + 1); 737 } 738 739 /* 740 * Unlock the spa_t after adding or removing a vdev. Besides undoing the 741 * locking of spa_vdev_enter(), we also want make sure the transactions have 742 * synced to disk, and then update the global configuration cache with the new 743 * information. 744 */ 745 int 746 spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error) 747 { 748 int config_changed = B_FALSE; 749 750 ASSERT(txg > spa_last_synced_txg(spa)); 751 752 /* 753 * Reassess the DTLs. 754 */ 755 vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE); 756 757 /* 758 * If the config changed, notify the scrub thread that it must restart. 759 */ 760 if (error == 0 && !list_is_empty(&spa->spa_dirty_list)) { 761 dsl_pool_scrub_restart(spa->spa_dsl_pool); 762 config_changed = B_TRUE; 763 } 764 765 spa_config_exit(spa, spa); 766 767 /* 768 * Note: this txg_wait_synced() is important because it ensures 769 * that there won't be more than one config change per txg. 770 * This allows us to use the txg as the generation number. 771 */ 772 if (error == 0) 773 txg_wait_synced(spa->spa_dsl_pool, txg); 774 775 if (vd != NULL) { 776 ASSERT(!vd->vdev_detached || vd->vdev_dtl.smo_object == 0); 777 vdev_free(vd); 778 } 779 780 /* 781 * If the config changed, update the config cache. 782 */ 783 if (config_changed) 784 spa_config_sync(spa, B_FALSE, B_TRUE); 785 786 mutex_exit(&spa_namespace_lock); 787 788 return (error); 789 } 790 791 /* 792 * ========================================================================== 793 * Miscellaneous functions 794 * ========================================================================== 795 */ 796 797 /* 798 * Rename a spa_t. 799 */ 800 int 801 spa_rename(const char *name, const char *newname) 802 { 803 spa_t *spa; 804 int err; 805 806 /* 807 * Lookup the spa_t and grab the config lock for writing. We need to 808 * actually open the pool so that we can sync out the necessary labels. 809 * It's OK to call spa_open() with the namespace lock held because we 810 * allow recursive calls for other reasons. 811 */ 812 mutex_enter(&spa_namespace_lock); 813 if ((err = spa_open(name, &spa, FTAG)) != 0) { 814 mutex_exit(&spa_namespace_lock); 815 return (err); 816 } 817 818 spa_config_enter(spa, RW_WRITER, FTAG); 819 820 avl_remove(&spa_namespace_avl, spa); 821 spa_strfree(spa->spa_name); 822 spa->spa_name = spa_strdup(newname); 823 avl_add(&spa_namespace_avl, spa); 824 825 /* 826 * Sync all labels to disk with the new names by marking the root vdev 827 * dirty and waiting for it to sync. It will pick up the new pool name 828 * during the sync. 829 */ 830 vdev_config_dirty(spa->spa_root_vdev); 831 832 spa_config_exit(spa, FTAG); 833 834 txg_wait_synced(spa->spa_dsl_pool, 0); 835 836 /* 837 * Sync the updated config cache. 838 */ 839 spa_config_sync(spa, B_FALSE, B_TRUE); 840 841 spa_close(spa, FTAG); 842 843 mutex_exit(&spa_namespace_lock); 844 845 return (0); 846 } 847 848 849 /* 850 * Determine whether a pool with given pool_guid exists. If device_guid is 851 * non-zero, determine whether the pool exists *and* contains a device with the 852 * specified device_guid. 853 */ 854 boolean_t 855 spa_guid_exists(uint64_t pool_guid, uint64_t device_guid) 856 { 857 spa_t *spa; 858 avl_tree_t *t = &spa_namespace_avl; 859 860 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 861 862 for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) { 863 if (spa->spa_state == POOL_STATE_UNINITIALIZED) 864 continue; 865 if (spa->spa_root_vdev == NULL) 866 continue; 867 if (spa_guid(spa) == pool_guid) { 868 if (device_guid == 0) 869 break; 870 871 if (vdev_lookup_by_guid(spa->spa_root_vdev, 872 device_guid) != NULL) 873 break; 874 875 /* 876 * Check any devices we may be in the process of adding. 877 */ 878 if (spa->spa_pending_vdev) { 879 if (vdev_lookup_by_guid(spa->spa_pending_vdev, 880 device_guid) != NULL) 881 break; 882 } 883 } 884 } 885 886 return (spa != NULL); 887 } 888 889 char * 890 spa_strdup(const char *s) 891 { 892 size_t len; 893 char *new; 894 895 len = strlen(s); 896 new = kmem_alloc(len + 1, KM_SLEEP); 897 bcopy(s, new, len); 898 new[len] = '\0'; 899 900 return (new); 901 } 902 903 void 904 spa_strfree(char *s) 905 { 906 kmem_free(s, strlen(s) + 1); 907 } 908 909 uint64_t 910 spa_get_random(uint64_t range) 911 { 912 uint64_t r; 913 914 ASSERT(range != 0); 915 916 (void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t)); 917 918 return (r % range); 919 } 920 921 void 922 sprintf_blkptr(char *buf, int len, const blkptr_t *bp) 923 { 924 int d; 925 926 if (bp == NULL) { 927 (void) snprintf(buf, len, "<NULL>"); 928 return; 929 } 930 931 if (BP_IS_HOLE(bp)) { 932 (void) snprintf(buf, len, "<hole>"); 933 return; 934 } 935 936 (void) snprintf(buf, len, "[L%llu %s] %llxL/%llxP ", 937 (u_longlong_t)BP_GET_LEVEL(bp), 938 dmu_ot[BP_GET_TYPE(bp)].ot_name, 939 (u_longlong_t)BP_GET_LSIZE(bp), 940 (u_longlong_t)BP_GET_PSIZE(bp)); 941 942 for (d = 0; d < BP_GET_NDVAS(bp); d++) { 943 const dva_t *dva = &bp->blk_dva[d]; 944 (void) snprintf(buf + strlen(buf), len - strlen(buf), 945 "DVA[%d]=<%llu:%llx:%llx> ", d, 946 (u_longlong_t)DVA_GET_VDEV(dva), 947 (u_longlong_t)DVA_GET_OFFSET(dva), 948 (u_longlong_t)DVA_GET_ASIZE(dva)); 949 } 950 951 (void) snprintf(buf + strlen(buf), len - strlen(buf), 952 "%s %s %s %s birth=%llu fill=%llu cksum=%llx:%llx:%llx:%llx", 953 zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name, 954 zio_compress_table[BP_GET_COMPRESS(bp)].ci_name, 955 BP_GET_BYTEORDER(bp) == 0 ? "BE" : "LE", 956 BP_IS_GANG(bp) ? "gang" : "contiguous", 957 (u_longlong_t)bp->blk_birth, 958 (u_longlong_t)bp->blk_fill, 959 (u_longlong_t)bp->blk_cksum.zc_word[0], 960 (u_longlong_t)bp->blk_cksum.zc_word[1], 961 (u_longlong_t)bp->blk_cksum.zc_word[2], 962 (u_longlong_t)bp->blk_cksum.zc_word[3]); 963 } 964 965 void 966 spa_freeze(spa_t *spa) 967 { 968 uint64_t freeze_txg = 0; 969 970 spa_config_enter(spa, RW_WRITER, FTAG); 971 if (spa->spa_freeze_txg == UINT64_MAX) { 972 freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE; 973 spa->spa_freeze_txg = freeze_txg; 974 } 975 spa_config_exit(spa, FTAG); 976 if (freeze_txg != 0) 977 txg_wait_synced(spa_get_dsl(spa), freeze_txg); 978 } 979 980 void 981 zfs_panic_recover(const char *fmt, ...) 982 { 983 va_list adx; 984 985 va_start(adx, fmt); 986 vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx); 987 va_end(adx); 988 } 989 990 /* 991 * ========================================================================== 992 * Accessor functions 993 * ========================================================================== 994 */ 995 996 krwlock_t * 997 spa_traverse_rwlock(spa_t *spa) 998 { 999 return (&spa->spa_traverse_lock); 1000 } 1001 1002 boolean_t 1003 spa_traverse_wanted(spa_t *spa) 1004 { 1005 return (spa->spa_traverse_wanted); 1006 } 1007 1008 dsl_pool_t * 1009 spa_get_dsl(spa_t *spa) 1010 { 1011 return (spa->spa_dsl_pool); 1012 } 1013 1014 blkptr_t * 1015 spa_get_rootblkptr(spa_t *spa) 1016 { 1017 return (&spa->spa_ubsync.ub_rootbp); 1018 } 1019 1020 void 1021 spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp) 1022 { 1023 spa->spa_uberblock.ub_rootbp = *bp; 1024 } 1025 1026 void 1027 spa_altroot(spa_t *spa, char *buf, size_t buflen) 1028 { 1029 if (spa->spa_root == NULL) 1030 buf[0] = '\0'; 1031 else 1032 (void) strncpy(buf, spa->spa_root, buflen); 1033 } 1034 1035 int 1036 spa_sync_pass(spa_t *spa) 1037 { 1038 return (spa->spa_sync_pass); 1039 } 1040 1041 char * 1042 spa_name(spa_t *spa) 1043 { 1044 /* 1045 * Accessing the name requires holding either the namespace lock or the 1046 * config lock, both of which are required to do a rename. 1047 */ 1048 ASSERT(MUTEX_HELD(&spa_namespace_lock) || 1049 spa_config_held(spa, RW_READER)); 1050 1051 return (spa->spa_name); 1052 } 1053 1054 uint64_t 1055 spa_guid(spa_t *spa) 1056 { 1057 /* 1058 * If we fail to parse the config during spa_load(), we can go through 1059 * the error path (which posts an ereport) and end up here with no root 1060 * vdev. We stash the original pool guid in 'spa_load_guid' to handle 1061 * this case. 1062 */ 1063 if (spa->spa_root_vdev != NULL) 1064 return (spa->spa_root_vdev->vdev_guid); 1065 else 1066 return (spa->spa_load_guid); 1067 } 1068 1069 uint64_t 1070 spa_last_synced_txg(spa_t *spa) 1071 { 1072 return (spa->spa_ubsync.ub_txg); 1073 } 1074 1075 uint64_t 1076 spa_first_txg(spa_t *spa) 1077 { 1078 return (spa->spa_first_txg); 1079 } 1080 1081 int 1082 spa_state(spa_t *spa) 1083 { 1084 return (spa->spa_state); 1085 } 1086 1087 uint64_t 1088 spa_freeze_txg(spa_t *spa) 1089 { 1090 return (spa->spa_freeze_txg); 1091 } 1092 1093 /* 1094 * Return how much space is allocated in the pool (ie. sum of all asize) 1095 */ 1096 uint64_t 1097 spa_get_alloc(spa_t *spa) 1098 { 1099 return (spa->spa_root_vdev->vdev_stat.vs_alloc); 1100 } 1101 1102 /* 1103 * Return how much (raid-z inflated) space there is in the pool. 1104 */ 1105 uint64_t 1106 spa_get_space(spa_t *spa) 1107 { 1108 return (spa->spa_root_vdev->vdev_stat.vs_space); 1109 } 1110 1111 /* 1112 * Return the amount of raid-z-deflated space in the pool. 1113 */ 1114 uint64_t 1115 spa_get_dspace(spa_t *spa) 1116 { 1117 if (spa->spa_deflate) 1118 return (spa->spa_root_vdev->vdev_stat.vs_dspace); 1119 else 1120 return (spa->spa_root_vdev->vdev_stat.vs_space); 1121 } 1122 1123 /* ARGSUSED */ 1124 uint64_t 1125 spa_get_asize(spa_t *spa, uint64_t lsize) 1126 { 1127 /* 1128 * For now, the worst case is 512-byte RAID-Z blocks, in which 1129 * case the space requirement is exactly 2x; so just assume that. 1130 * Add to this the fact that we can have up to 3 DVAs per bp, and 1131 * we have to multiply by a total of 6x. 1132 */ 1133 return (lsize * 6); 1134 } 1135 1136 /* 1137 * Return the failure mode that has been set to this pool. The default 1138 * behavior will be to block all I/Os when a complete failure occurs. 1139 */ 1140 uint8_t 1141 spa_get_failmode(spa_t *spa) 1142 { 1143 return (spa->spa_failmode); 1144 } 1145 1146 uint64_t 1147 spa_version(spa_t *spa) 1148 { 1149 return (spa->spa_ubsync.ub_version); 1150 } 1151 1152 int 1153 spa_max_replication(spa_t *spa) 1154 { 1155 /* 1156 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to 1157 * handle BPs with more than one DVA allocated. Set our max 1158 * replication level accordingly. 1159 */ 1160 if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS) 1161 return (1); 1162 return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override)); 1163 } 1164 1165 uint64_t 1166 bp_get_dasize(spa_t *spa, const blkptr_t *bp) 1167 { 1168 int sz = 0, i; 1169 1170 if (!spa->spa_deflate) 1171 return (BP_GET_ASIZE(bp)); 1172 1173 spa_config_enter(spa, RW_READER, FTAG); 1174 for (i = 0; i < SPA_DVAS_PER_BP; i++) { 1175 vdev_t *vd = 1176 vdev_lookup_top(spa, DVA_GET_VDEV(&bp->blk_dva[i])); 1177 if (vd) 1178 sz += (DVA_GET_ASIZE(&bp->blk_dva[i]) >> 1179 SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio; 1180 } 1181 spa_config_exit(spa, FTAG); 1182 return (sz); 1183 } 1184 1185 /* 1186 * ========================================================================== 1187 * Initialization and Termination 1188 * ========================================================================== 1189 */ 1190 1191 static int 1192 spa_name_compare(const void *a1, const void *a2) 1193 { 1194 const spa_t *s1 = a1; 1195 const spa_t *s2 = a2; 1196 int s; 1197 1198 s = strcmp(s1->spa_name, s2->spa_name); 1199 if (s > 0) 1200 return (1); 1201 if (s < 0) 1202 return (-1); 1203 return (0); 1204 } 1205 1206 int 1207 spa_busy(void) 1208 { 1209 return (spa_active_count); 1210 } 1211 1212 void 1213 spa_boot_init() 1214 { 1215 spa_config_load(); 1216 } 1217 1218 void 1219 spa_init(int mode) 1220 { 1221 mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL); 1222 mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL); 1223 mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL); 1224 cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL); 1225 1226 avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t), 1227 offsetof(spa_t, spa_avl)); 1228 1229 avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t), 1230 offsetof(spa_aux_t, aux_avl)); 1231 1232 avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t), 1233 offsetof(spa_aux_t, aux_avl)); 1234 1235 spa_mode = mode; 1236 1237 refcount_init(); 1238 unique_init(); 1239 zio_init(); 1240 dmu_init(); 1241 zil_init(); 1242 vdev_cache_stat_init(); 1243 zfs_prop_init(); 1244 zpool_prop_init(); 1245 spa_config_load(); 1246 } 1247 1248 void 1249 spa_fini(void) 1250 { 1251 spa_evict_all(); 1252 1253 vdev_cache_stat_fini(); 1254 zil_fini(); 1255 dmu_fini(); 1256 zio_fini(); 1257 unique_fini(); 1258 refcount_fini(); 1259 1260 avl_destroy(&spa_namespace_avl); 1261 avl_destroy(&spa_spare_avl); 1262 avl_destroy(&spa_l2cache_avl); 1263 1264 cv_destroy(&spa_namespace_cv); 1265 mutex_destroy(&spa_namespace_lock); 1266 mutex_destroy(&spa_spare_lock); 1267 mutex_destroy(&spa_l2cache_lock); 1268 } 1269 1270 /* 1271 * Return whether this pool has slogs. No locking needed. 1272 * It's not a problem if the wrong answer is returned as it's only for 1273 * performance and not correctness 1274 */ 1275 boolean_t 1276 spa_has_slogs(spa_t *spa) 1277 { 1278 return (spa->spa_log_class->mc_rotor != NULL); 1279 } 1280 1281 /* 1282 * Return whether this pool is the root pool. 1283 */ 1284 boolean_t 1285 spa_is_root(spa_t *spa) 1286 { 1287 return (spa->spa_is_root); 1288 } 1289