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