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 /* 23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* 30 * This file contains all the routines used when modifying on-disk SPA state. 31 * This includes opening, importing, destroying, exporting a pool, and syncing a 32 * pool. 33 */ 34 35 #include <sys/zfs_context.h> 36 #include <sys/fm/fs/zfs.h> 37 #include <sys/spa_impl.h> 38 #include <sys/zio.h> 39 #include <sys/zio_checksum.h> 40 #include <sys/zio_compress.h> 41 #include <sys/dmu.h> 42 #include <sys/dmu_tx.h> 43 #include <sys/zap.h> 44 #include <sys/zil.h> 45 #include <sys/vdev_impl.h> 46 #include <sys/metaslab.h> 47 #include <sys/uberblock_impl.h> 48 #include <sys/txg.h> 49 #include <sys/avl.h> 50 #include <sys/dmu_traverse.h> 51 #include <sys/dmu_objset.h> 52 #include <sys/unique.h> 53 #include <sys/dsl_pool.h> 54 #include <sys/dsl_dataset.h> 55 #include <sys/dsl_dir.h> 56 #include <sys/dsl_prop.h> 57 #include <sys/dsl_synctask.h> 58 #include <sys/fs/zfs.h> 59 #include <sys/callb.h> 60 #include <sys/systeminfo.h> 61 #include <sys/sunddi.h> 62 63 int zio_taskq_threads = 8; 64 65 /* 66 * ========================================================================== 67 * SPA state manipulation (open/create/destroy/import/export) 68 * ========================================================================== 69 */ 70 71 static int 72 spa_error_entry_compare(const void *a, const void *b) 73 { 74 spa_error_entry_t *sa = (spa_error_entry_t *)a; 75 spa_error_entry_t *sb = (spa_error_entry_t *)b; 76 int ret; 77 78 ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, 79 sizeof (zbookmark_t)); 80 81 if (ret < 0) 82 return (-1); 83 else if (ret > 0) 84 return (1); 85 else 86 return (0); 87 } 88 89 /* 90 * Utility function which retrieves copies of the current logs and 91 * re-initializes them in the process. 92 */ 93 void 94 spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 95 { 96 ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 97 98 bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 99 bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 100 101 avl_create(&spa->spa_errlist_scrub, 102 spa_error_entry_compare, sizeof (spa_error_entry_t), 103 offsetof(spa_error_entry_t, se_avl)); 104 avl_create(&spa->spa_errlist_last, 105 spa_error_entry_compare, sizeof (spa_error_entry_t), 106 offsetof(spa_error_entry_t, se_avl)); 107 } 108 109 /* 110 * Activate an uninitialized pool. 111 */ 112 static void 113 spa_activate(spa_t *spa) 114 { 115 int t; 116 117 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 118 119 spa->spa_state = POOL_STATE_ACTIVE; 120 121 spa->spa_normal_class = metaslab_class_create(); 122 123 for (t = 0; t < ZIO_TYPES; t++) { 124 spa->spa_zio_issue_taskq[t] = taskq_create("spa_zio_issue", 125 zio_taskq_threads, maxclsyspri, 50, INT_MAX, 126 TASKQ_PREPOPULATE); 127 spa->spa_zio_intr_taskq[t] = taskq_create("spa_zio_intr", 128 zio_taskq_threads, maxclsyspri, 50, INT_MAX, 129 TASKQ_PREPOPULATE); 130 } 131 132 rw_init(&spa->spa_traverse_lock, NULL, RW_DEFAULT, NULL); 133 134 mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL); 135 mutex_init(&spa->spa_config_cache_lock, NULL, MUTEX_DEFAULT, NULL); 136 mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL); 137 mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL); 138 mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL); 139 mutex_init(&spa->spa_config_lock.scl_lock, NULL, MUTEX_DEFAULT, NULL); 140 mutex_init(&spa->spa_sync_bplist.bpl_lock, NULL, MUTEX_DEFAULT, NULL); 141 mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL); 142 mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL); 143 144 list_create(&spa->spa_dirty_list, sizeof (vdev_t), 145 offsetof(vdev_t, vdev_dirty_node)); 146 147 txg_list_create(&spa->spa_vdev_txg_list, 148 offsetof(struct vdev, vdev_txg_node)); 149 150 avl_create(&spa->spa_errlist_scrub, 151 spa_error_entry_compare, sizeof (spa_error_entry_t), 152 offsetof(spa_error_entry_t, se_avl)); 153 avl_create(&spa->spa_errlist_last, 154 spa_error_entry_compare, sizeof (spa_error_entry_t), 155 offsetof(spa_error_entry_t, se_avl)); 156 } 157 158 /* 159 * Opposite of spa_activate(). 160 */ 161 static void 162 spa_deactivate(spa_t *spa) 163 { 164 int t; 165 166 ASSERT(spa->spa_sync_on == B_FALSE); 167 ASSERT(spa->spa_dsl_pool == NULL); 168 ASSERT(spa->spa_root_vdev == NULL); 169 170 ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 171 172 txg_list_destroy(&spa->spa_vdev_txg_list); 173 174 list_destroy(&spa->spa_dirty_list); 175 176 rw_destroy(&spa->spa_traverse_lock); 177 178 for (t = 0; t < ZIO_TYPES; t++) { 179 taskq_destroy(spa->spa_zio_issue_taskq[t]); 180 taskq_destroy(spa->spa_zio_intr_taskq[t]); 181 spa->spa_zio_issue_taskq[t] = NULL; 182 spa->spa_zio_intr_taskq[t] = NULL; 183 } 184 185 metaslab_class_destroy(spa->spa_normal_class); 186 spa->spa_normal_class = NULL; 187 188 /* 189 * If this was part of an import or the open otherwise failed, we may 190 * still have errors left in the queues. Empty them just in case. 191 */ 192 spa_errlog_drain(spa); 193 194 avl_destroy(&spa->spa_errlist_scrub); 195 avl_destroy(&spa->spa_errlist_last); 196 197 spa->spa_state = POOL_STATE_UNINITIALIZED; 198 } 199 200 /* 201 * Verify a pool configuration, and construct the vdev tree appropriately. This 202 * will create all the necessary vdevs in the appropriate layout, with each vdev 203 * in the CLOSED state. This will prep the pool before open/creation/import. 204 * All vdev validation is done by the vdev_alloc() routine. 205 */ 206 static int 207 spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 208 uint_t id, int atype) 209 { 210 nvlist_t **child; 211 uint_t c, children; 212 int error; 213 214 if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 215 return (error); 216 217 if ((*vdp)->vdev_ops->vdev_op_leaf) 218 return (0); 219 220 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 221 &child, &children) != 0) { 222 vdev_free(*vdp); 223 *vdp = NULL; 224 return (EINVAL); 225 } 226 227 for (c = 0; c < children; c++) { 228 vdev_t *vd; 229 if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 230 atype)) != 0) { 231 vdev_free(*vdp); 232 *vdp = NULL; 233 return (error); 234 } 235 } 236 237 ASSERT(*vdp != NULL); 238 239 return (0); 240 } 241 242 /* 243 * Opposite of spa_load(). 244 */ 245 static void 246 spa_unload(spa_t *spa) 247 { 248 int i; 249 250 /* 251 * Stop async tasks. 252 */ 253 spa_async_suspend(spa); 254 255 /* 256 * Stop syncing. 257 */ 258 if (spa->spa_sync_on) { 259 txg_sync_stop(spa->spa_dsl_pool); 260 spa->spa_sync_on = B_FALSE; 261 } 262 263 /* 264 * Wait for any outstanding prefetch I/O to complete. 265 */ 266 spa_config_enter(spa, RW_WRITER, FTAG); 267 spa_config_exit(spa, FTAG); 268 269 /* 270 * Close the dsl pool. 271 */ 272 if (spa->spa_dsl_pool) { 273 dsl_pool_close(spa->spa_dsl_pool); 274 spa->spa_dsl_pool = NULL; 275 } 276 277 /* 278 * Close all vdevs. 279 */ 280 if (spa->spa_root_vdev) 281 vdev_free(spa->spa_root_vdev); 282 ASSERT(spa->spa_root_vdev == NULL); 283 284 for (i = 0; i < spa->spa_nspares; i++) 285 vdev_free(spa->spa_spares[i]); 286 if (spa->spa_spares) { 287 kmem_free(spa->spa_spares, spa->spa_nspares * sizeof (void *)); 288 spa->spa_spares = NULL; 289 } 290 if (spa->spa_sparelist) { 291 nvlist_free(spa->spa_sparelist); 292 spa->spa_sparelist = NULL; 293 } 294 295 spa->spa_async_suspended = 0; 296 } 297 298 /* 299 * Load (or re-load) the current list of vdevs describing the active spares for 300 * this pool. When this is called, we have some form of basic information in 301 * 'spa_sparelist'. We parse this into vdevs, try to open them, and then 302 * re-generate a more complete list including status information. 303 */ 304 static void 305 spa_load_spares(spa_t *spa) 306 { 307 nvlist_t **spares; 308 uint_t nspares; 309 int i; 310 vdev_t *vd, *tvd; 311 312 /* 313 * First, close and free any existing spare vdevs. 314 */ 315 for (i = 0; i < spa->spa_nspares; i++) { 316 vd = spa->spa_spares[i]; 317 318 /* Undo the call to spa_activate() below */ 319 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid)) != NULL && 320 tvd->vdev_isspare) 321 spa_spare_remove(tvd); 322 vdev_close(vd); 323 vdev_free(vd); 324 } 325 326 if (spa->spa_spares) 327 kmem_free(spa->spa_spares, spa->spa_nspares * sizeof (void *)); 328 329 if (spa->spa_sparelist == NULL) 330 nspares = 0; 331 else 332 VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist, 333 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 334 335 spa->spa_nspares = (int)nspares; 336 spa->spa_spares = NULL; 337 338 if (nspares == 0) 339 return; 340 341 /* 342 * Construct the array of vdevs, opening them to get status in the 343 * process. For each spare, there is potentially two different vdev_t 344 * structures associated with it: one in the list of spares (used only 345 * for basic validation purposes) and one in the active vdev 346 * configuration (if it's spared in). During this phase we open and 347 * validate each vdev on the spare list. If the vdev also exists in the 348 * active configuration, then we also mark this vdev as an active spare. 349 */ 350 spa->spa_spares = kmem_alloc(nspares * sizeof (void *), KM_SLEEP); 351 for (i = 0; i < spa->spa_nspares; i++) { 352 VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 353 VDEV_ALLOC_SPARE) == 0); 354 ASSERT(vd != NULL); 355 356 spa->spa_spares[i] = vd; 357 358 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid)) != NULL) { 359 if (!tvd->vdev_isspare) 360 spa_spare_add(tvd); 361 362 /* 363 * We only mark the spare active if we were successfully 364 * able to load the vdev. Otherwise, importing a pool 365 * with a bad active spare would result in strange 366 * behavior, because multiple pool would think the spare 367 * is actively in use. 368 * 369 * There is a vulnerability here to an equally bizarre 370 * circumstance, where a dead active spare is later 371 * brought back to life (onlined or otherwise). Given 372 * the rarity of this scenario, and the extra complexity 373 * it adds, we ignore the possibility. 374 */ 375 if (!vdev_is_dead(tvd)) 376 spa_spare_activate(tvd); 377 } 378 379 if (vdev_open(vd) != 0) 380 continue; 381 382 vd->vdev_top = vd; 383 (void) vdev_validate_spare(vd); 384 } 385 386 /* 387 * Recompute the stashed list of spares, with status information 388 * this time. 389 */ 390 VERIFY(nvlist_remove(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 391 DATA_TYPE_NVLIST_ARRAY) == 0); 392 393 spares = kmem_alloc(spa->spa_nspares * sizeof (void *), KM_SLEEP); 394 for (i = 0; i < spa->spa_nspares; i++) 395 spares[i] = vdev_config_generate(spa, spa->spa_spares[i], 396 B_TRUE, B_TRUE); 397 VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 398 spares, spa->spa_nspares) == 0); 399 for (i = 0; i < spa->spa_nspares; i++) 400 nvlist_free(spares[i]); 401 kmem_free(spares, spa->spa_nspares * sizeof (void *)); 402 } 403 404 static int 405 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 406 { 407 dmu_buf_t *db; 408 char *packed = NULL; 409 size_t nvsize = 0; 410 int error; 411 *value = NULL; 412 413 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 414 nvsize = *(uint64_t *)db->db_data; 415 dmu_buf_rele(db, FTAG); 416 417 packed = kmem_alloc(nvsize, KM_SLEEP); 418 error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed); 419 if (error == 0) 420 error = nvlist_unpack(packed, nvsize, value, 0); 421 kmem_free(packed, nvsize); 422 423 return (error); 424 } 425 426 /* 427 * Checks to see if the given vdev could not be opened, in which case we post a 428 * sysevent to notify the autoreplace code that the device has been removed. 429 */ 430 static void 431 spa_check_removed(vdev_t *vd) 432 { 433 int c; 434 435 for (c = 0; c < vd->vdev_children; c++) 436 spa_check_removed(vd->vdev_child[c]); 437 438 if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) { 439 zfs_post_autoreplace(vd->vdev_spa, vd); 440 spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK); 441 } 442 } 443 444 /* 445 * Load an existing storage pool, using the pool's builtin spa_config as a 446 * source of configuration information. 447 */ 448 static int 449 spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig) 450 { 451 int error = 0; 452 nvlist_t *nvroot = NULL; 453 vdev_t *rvd; 454 uberblock_t *ub = &spa->spa_uberblock; 455 uint64_t config_cache_txg = spa->spa_config_txg; 456 uint64_t pool_guid; 457 uint64_t version; 458 zio_t *zio; 459 uint64_t autoreplace = 0; 460 461 spa->spa_load_state = state; 462 463 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) || 464 nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) { 465 error = EINVAL; 466 goto out; 467 } 468 469 /* 470 * Versioning wasn't explicitly added to the label until later, so if 471 * it's not present treat it as the initial version. 472 */ 473 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0) 474 version = ZFS_VERSION_INITIAL; 475 476 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 477 &spa->spa_config_txg); 478 479 if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 480 spa_guid_exists(pool_guid, 0)) { 481 error = EEXIST; 482 goto out; 483 } 484 485 spa->spa_load_guid = pool_guid; 486 487 /* 488 * Parse the configuration into a vdev tree. We explicitly set the 489 * value that will be returned by spa_version() since parsing the 490 * configuration requires knowing the version number. 491 */ 492 spa_config_enter(spa, RW_WRITER, FTAG); 493 spa->spa_ubsync.ub_version = version; 494 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD); 495 spa_config_exit(spa, FTAG); 496 497 if (error != 0) 498 goto out; 499 500 ASSERT(spa->spa_root_vdev == rvd); 501 ASSERT(spa_guid(spa) == pool_guid); 502 503 /* 504 * Try to open all vdevs, loading each label in the process. 505 */ 506 error = vdev_open(rvd); 507 if (error != 0) 508 goto out; 509 510 /* 511 * Validate the labels for all leaf vdevs. We need to grab the config 512 * lock because all label I/O is done with the ZIO_FLAG_CONFIG_HELD 513 * flag. 514 */ 515 spa_config_enter(spa, RW_READER, FTAG); 516 error = vdev_validate(rvd); 517 spa_config_exit(spa, FTAG); 518 519 if (error != 0) 520 goto out; 521 522 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 523 error = ENXIO; 524 goto out; 525 } 526 527 /* 528 * Find the best uberblock. 529 */ 530 bzero(ub, sizeof (uberblock_t)); 531 532 zio = zio_root(spa, NULL, NULL, 533 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 534 vdev_uberblock_load(zio, rvd, ub); 535 error = zio_wait(zio); 536 537 /* 538 * If we weren't able to find a single valid uberblock, return failure. 539 */ 540 if (ub->ub_txg == 0) { 541 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 542 VDEV_AUX_CORRUPT_DATA); 543 error = ENXIO; 544 goto out; 545 } 546 547 /* 548 * If the pool is newer than the code, we can't open it. 549 */ 550 if (ub->ub_version > ZFS_VERSION) { 551 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 552 VDEV_AUX_VERSION_NEWER); 553 error = ENOTSUP; 554 goto out; 555 } 556 557 /* 558 * If the vdev guid sum doesn't match the uberblock, we have an 559 * incomplete configuration. 560 */ 561 if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) { 562 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 563 VDEV_AUX_BAD_GUID_SUM); 564 error = ENXIO; 565 goto out; 566 } 567 568 /* 569 * Initialize internal SPA structures. 570 */ 571 spa->spa_state = POOL_STATE_ACTIVE; 572 spa->spa_ubsync = spa->spa_uberblock; 573 spa->spa_first_txg = spa_last_synced_txg(spa) + 1; 574 error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 575 if (error) { 576 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 577 VDEV_AUX_CORRUPT_DATA); 578 goto out; 579 } 580 spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 581 582 if (zap_lookup(spa->spa_meta_objset, 583 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 584 sizeof (uint64_t), 1, &spa->spa_config_object) != 0) { 585 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 586 VDEV_AUX_CORRUPT_DATA); 587 error = EIO; 588 goto out; 589 } 590 591 if (!mosconfig) { 592 nvlist_t *newconfig; 593 uint64_t hostid; 594 595 if (load_nvlist(spa, spa->spa_config_object, &newconfig) != 0) { 596 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 597 VDEV_AUX_CORRUPT_DATA); 598 error = EIO; 599 goto out; 600 } 601 602 if (nvlist_lookup_uint64(newconfig, ZPOOL_CONFIG_HOSTID, 603 &hostid) == 0) { 604 char *hostname; 605 unsigned long myhostid = 0; 606 607 VERIFY(nvlist_lookup_string(newconfig, 608 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 609 610 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 611 if (hostid != 0 && myhostid != 0 && 612 (unsigned long)hostid != myhostid) { 613 cmn_err(CE_WARN, "pool '%s' could not be " 614 "loaded as it was last accessed by " 615 "another system (host: %s hostid: 0x%lx). " 616 "See: http://www.sun.com/msg/ZFS-8000-EY", 617 spa->spa_name, hostname, 618 (unsigned long)hostid); 619 error = EBADF; 620 goto out; 621 } 622 } 623 624 spa_config_set(spa, newconfig); 625 spa_unload(spa); 626 spa_deactivate(spa); 627 spa_activate(spa); 628 629 return (spa_load(spa, newconfig, state, B_TRUE)); 630 } 631 632 if (zap_lookup(spa->spa_meta_objset, 633 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 634 sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) { 635 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 636 VDEV_AUX_CORRUPT_DATA); 637 error = EIO; 638 goto out; 639 } 640 641 /* 642 * Load the bit that tells us to use the new accounting function 643 * (raid-z deflation). If we have an older pool, this will not 644 * be present. 645 */ 646 error = zap_lookup(spa->spa_meta_objset, 647 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 648 sizeof (uint64_t), 1, &spa->spa_deflate); 649 if (error != 0 && error != ENOENT) { 650 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 651 VDEV_AUX_CORRUPT_DATA); 652 error = EIO; 653 goto out; 654 } 655 656 /* 657 * Load the persistent error log. If we have an older pool, this will 658 * not be present. 659 */ 660 error = zap_lookup(spa->spa_meta_objset, 661 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST, 662 sizeof (uint64_t), 1, &spa->spa_errlog_last); 663 if (error != 0 && error != ENOENT) { 664 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 665 VDEV_AUX_CORRUPT_DATA); 666 error = EIO; 667 goto out; 668 } 669 670 error = zap_lookup(spa->spa_meta_objset, 671 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB, 672 sizeof (uint64_t), 1, &spa->spa_errlog_scrub); 673 if (error != 0 && error != ENOENT) { 674 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 675 VDEV_AUX_CORRUPT_DATA); 676 error = EIO; 677 goto out; 678 } 679 680 /* 681 * Load the history object. If we have an older pool, this 682 * will not be present. 683 */ 684 error = zap_lookup(spa->spa_meta_objset, 685 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY, 686 sizeof (uint64_t), 1, &spa->spa_history); 687 if (error != 0 && error != ENOENT) { 688 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 689 VDEV_AUX_CORRUPT_DATA); 690 error = EIO; 691 goto out; 692 } 693 694 /* 695 * Load any hot spares for this pool. 696 */ 697 error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 698 DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares_object); 699 if (error != 0 && error != ENOENT) { 700 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 701 VDEV_AUX_CORRUPT_DATA); 702 error = EIO; 703 goto out; 704 } 705 if (error == 0) { 706 ASSERT(spa_version(spa) >= ZFS_VERSION_SPARES); 707 if (load_nvlist(spa, spa->spa_spares_object, 708 &spa->spa_sparelist) != 0) { 709 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 710 VDEV_AUX_CORRUPT_DATA); 711 error = EIO; 712 goto out; 713 } 714 715 spa_config_enter(spa, RW_WRITER, FTAG); 716 spa_load_spares(spa); 717 spa_config_exit(spa, FTAG); 718 } 719 720 error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 721 DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object); 722 723 if (error && error != ENOENT) { 724 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 725 VDEV_AUX_CORRUPT_DATA); 726 error = EIO; 727 goto out; 728 } 729 730 if (error == 0) { 731 (void) zap_lookup(spa->spa_meta_objset, 732 spa->spa_pool_props_object, 733 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), 734 sizeof (uint64_t), 1, &spa->spa_bootfs); 735 (void) zap_lookup(spa->spa_meta_objset, 736 spa->spa_pool_props_object, 737 zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE), 738 sizeof (uint64_t), 1, &autoreplace); 739 } 740 741 /* 742 * If the 'autoreplace' property is set, then post a resource notifying 743 * the ZFS DE that it should not issue any faults for unopenable 744 * devices. We also iterate over the vdevs, and post a sysevent for any 745 * unopenable vdevs so that the normal autoreplace handler can take 746 * over. 747 */ 748 if (autoreplace) 749 spa_check_removed(spa->spa_root_vdev); 750 751 /* 752 * Load the vdev state for all toplevel vdevs. 753 */ 754 vdev_load(rvd); 755 756 /* 757 * Propagate the leaf DTLs we just loaded all the way up the tree. 758 */ 759 spa_config_enter(spa, RW_WRITER, FTAG); 760 vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 761 spa_config_exit(spa, FTAG); 762 763 /* 764 * Check the state of the root vdev. If it can't be opened, it 765 * indicates one or more toplevel vdevs are faulted. 766 */ 767 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 768 error = ENXIO; 769 goto out; 770 } 771 772 if ((spa_mode & FWRITE) && state != SPA_LOAD_TRYIMPORT) { 773 dmu_tx_t *tx; 774 int need_update = B_FALSE; 775 int c; 776 777 /* 778 * Claim log blocks that haven't been committed yet. 779 * This must all happen in a single txg. 780 */ 781 tx = dmu_tx_create_assigned(spa_get_dsl(spa), 782 spa_first_txg(spa)); 783 (void) dmu_objset_find(spa->spa_name, 784 zil_claim, tx, DS_FIND_CHILDREN); 785 dmu_tx_commit(tx); 786 787 spa->spa_sync_on = B_TRUE; 788 txg_sync_start(spa->spa_dsl_pool); 789 790 /* 791 * Wait for all claims to sync. 792 */ 793 txg_wait_synced(spa->spa_dsl_pool, 0); 794 795 /* 796 * If the config cache is stale, or we have uninitialized 797 * metaslabs (see spa_vdev_add()), then update the config. 798 */ 799 if (config_cache_txg != spa->spa_config_txg || 800 state == SPA_LOAD_IMPORT) 801 need_update = B_TRUE; 802 803 for (c = 0; c < rvd->vdev_children; c++) 804 if (rvd->vdev_child[c]->vdev_ms_array == 0) 805 need_update = B_TRUE; 806 807 /* 808 * Update the config cache asychronously in case we're the 809 * root pool, in which case the config cache isn't writable yet. 810 */ 811 if (need_update) 812 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 813 } 814 815 error = 0; 816 out: 817 if (error && error != EBADF) 818 zfs_ereport_post(FM_EREPORT_ZFS_POOL, spa, NULL, NULL, 0, 0); 819 spa->spa_load_state = SPA_LOAD_NONE; 820 spa->spa_ena = 0; 821 822 return (error); 823 } 824 825 /* 826 * Pool Open/Import 827 * 828 * The import case is identical to an open except that the configuration is sent 829 * down from userland, instead of grabbed from the configuration cache. For the 830 * case of an open, the pool configuration will exist in the 831 * POOL_STATE_UNINITIALIZED state. 832 * 833 * The stats information (gen/count/ustats) is used to gather vdev statistics at 834 * the same time open the pool, without having to keep around the spa_t in some 835 * ambiguous state. 836 */ 837 static int 838 spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config) 839 { 840 spa_t *spa; 841 int error; 842 int loaded = B_FALSE; 843 int locked = B_FALSE; 844 845 *spapp = NULL; 846 847 /* 848 * As disgusting as this is, we need to support recursive calls to this 849 * function because dsl_dir_open() is called during spa_load(), and ends 850 * up calling spa_open() again. The real fix is to figure out how to 851 * avoid dsl_dir_open() calling this in the first place. 852 */ 853 if (mutex_owner(&spa_namespace_lock) != curthread) { 854 mutex_enter(&spa_namespace_lock); 855 locked = B_TRUE; 856 } 857 858 if ((spa = spa_lookup(pool)) == NULL) { 859 if (locked) 860 mutex_exit(&spa_namespace_lock); 861 return (ENOENT); 862 } 863 if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 864 865 spa_activate(spa); 866 867 error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE); 868 869 if (error == EBADF) { 870 /* 871 * If vdev_validate() returns failure (indicated by 872 * EBADF), it indicates that one of the vdevs indicates 873 * that the pool has been exported or destroyed. If 874 * this is the case, the config cache is out of sync and 875 * we should remove the pool from the namespace. 876 */ 877 zfs_post_ok(spa, NULL); 878 spa_unload(spa); 879 spa_deactivate(spa); 880 spa_remove(spa); 881 spa_config_sync(); 882 if (locked) 883 mutex_exit(&spa_namespace_lock); 884 return (ENOENT); 885 } 886 887 if (error) { 888 /* 889 * We can't open the pool, but we still have useful 890 * information: the state of each vdev after the 891 * attempted vdev_open(). Return this to the user. 892 */ 893 if (config != NULL && spa->spa_root_vdev != NULL) { 894 spa_config_enter(spa, RW_READER, FTAG); 895 *config = spa_config_generate(spa, NULL, -1ULL, 896 B_TRUE); 897 spa_config_exit(spa, FTAG); 898 } 899 spa_unload(spa); 900 spa_deactivate(spa); 901 spa->spa_last_open_failed = B_TRUE; 902 if (locked) 903 mutex_exit(&spa_namespace_lock); 904 *spapp = NULL; 905 return (error); 906 } else { 907 zfs_post_ok(spa, NULL); 908 spa->spa_last_open_failed = B_FALSE; 909 } 910 911 loaded = B_TRUE; 912 } 913 914 spa_open_ref(spa, tag); 915 916 /* 917 * If we just loaded the pool, resilver anything that's out of date. 918 */ 919 if (loaded && (spa_mode & FWRITE)) 920 VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 921 922 if (locked) 923 mutex_exit(&spa_namespace_lock); 924 925 *spapp = spa; 926 927 if (config != NULL) { 928 spa_config_enter(spa, RW_READER, FTAG); 929 *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 930 spa_config_exit(spa, FTAG); 931 } 932 933 return (0); 934 } 935 936 int 937 spa_open(const char *name, spa_t **spapp, void *tag) 938 { 939 return (spa_open_common(name, spapp, tag, NULL)); 940 } 941 942 /* 943 * Lookup the given spa_t, incrementing the inject count in the process, 944 * preventing it from being exported or destroyed. 945 */ 946 spa_t * 947 spa_inject_addref(char *name) 948 { 949 spa_t *spa; 950 951 mutex_enter(&spa_namespace_lock); 952 if ((spa = spa_lookup(name)) == NULL) { 953 mutex_exit(&spa_namespace_lock); 954 return (NULL); 955 } 956 spa->spa_inject_ref++; 957 mutex_exit(&spa_namespace_lock); 958 959 return (spa); 960 } 961 962 void 963 spa_inject_delref(spa_t *spa) 964 { 965 mutex_enter(&spa_namespace_lock); 966 spa->spa_inject_ref--; 967 mutex_exit(&spa_namespace_lock); 968 } 969 970 static void 971 spa_add_spares(spa_t *spa, nvlist_t *config) 972 { 973 nvlist_t **spares; 974 uint_t i, nspares; 975 nvlist_t *nvroot; 976 uint64_t guid; 977 vdev_stat_t *vs; 978 uint_t vsc; 979 uint64_t pool; 980 981 if (spa->spa_nspares == 0) 982 return; 983 984 VERIFY(nvlist_lookup_nvlist(config, 985 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 986 VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist, 987 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 988 if (nspares != 0) { 989 VERIFY(nvlist_add_nvlist_array(nvroot, 990 ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 991 VERIFY(nvlist_lookup_nvlist_array(nvroot, 992 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 993 994 /* 995 * Go through and find any spares which have since been 996 * repurposed as an active spare. If this is the case, update 997 * their status appropriately. 998 */ 999 for (i = 0; i < nspares; i++) { 1000 VERIFY(nvlist_lookup_uint64(spares[i], 1001 ZPOOL_CONFIG_GUID, &guid) == 0); 1002 if (spa_spare_exists(guid, &pool) && pool != 0ULL) { 1003 VERIFY(nvlist_lookup_uint64_array( 1004 spares[i], ZPOOL_CONFIG_STATS, 1005 (uint64_t **)&vs, &vsc) == 0); 1006 vs->vs_state = VDEV_STATE_CANT_OPEN; 1007 vs->vs_aux = VDEV_AUX_SPARED; 1008 } 1009 } 1010 } 1011 } 1012 1013 int 1014 spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen) 1015 { 1016 int error; 1017 spa_t *spa; 1018 1019 *config = NULL; 1020 error = spa_open_common(name, &spa, FTAG, config); 1021 1022 if (spa && *config != NULL) { 1023 VERIFY(nvlist_add_uint64(*config, ZPOOL_CONFIG_ERRCOUNT, 1024 spa_get_errlog_size(spa)) == 0); 1025 1026 spa_add_spares(spa, *config); 1027 } 1028 1029 /* 1030 * We want to get the alternate root even for faulted pools, so we cheat 1031 * and call spa_lookup() directly. 1032 */ 1033 if (altroot) { 1034 if (spa == NULL) { 1035 mutex_enter(&spa_namespace_lock); 1036 spa = spa_lookup(name); 1037 if (spa) 1038 spa_altroot(spa, altroot, buflen); 1039 else 1040 altroot[0] = '\0'; 1041 spa = NULL; 1042 mutex_exit(&spa_namespace_lock); 1043 } else { 1044 spa_altroot(spa, altroot, buflen); 1045 } 1046 } 1047 1048 if (spa != NULL) 1049 spa_close(spa, FTAG); 1050 1051 return (error); 1052 } 1053 1054 /* 1055 * Validate that the 'spares' array is well formed. We must have an array of 1056 * nvlists, each which describes a valid leaf vdev. If this is an import (mode 1057 * is VDEV_ALLOC_SPARE), then we allow corrupted spares to be specified, as long 1058 * as they are well-formed. 1059 */ 1060 static int 1061 spa_validate_spares(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 1062 { 1063 nvlist_t **spares; 1064 uint_t i, nspares; 1065 vdev_t *vd; 1066 int error; 1067 1068 /* 1069 * It's acceptable to have no spares specified. 1070 */ 1071 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 1072 &spares, &nspares) != 0) 1073 return (0); 1074 1075 if (nspares == 0) 1076 return (EINVAL); 1077 1078 /* 1079 * Make sure the pool is formatted with a version that supports hot 1080 * spares. 1081 */ 1082 if (spa_version(spa) < ZFS_VERSION_SPARES) 1083 return (ENOTSUP); 1084 1085 /* 1086 * Set the pending spare list so we correctly handle device in-use 1087 * checking. 1088 */ 1089 spa->spa_pending_spares = spares; 1090 spa->spa_pending_nspares = nspares; 1091 1092 for (i = 0; i < nspares; i++) { 1093 if ((error = spa_config_parse(spa, &vd, spares[i], NULL, 0, 1094 mode)) != 0) 1095 goto out; 1096 1097 if (!vd->vdev_ops->vdev_op_leaf) { 1098 vdev_free(vd); 1099 error = EINVAL; 1100 goto out; 1101 } 1102 1103 vd->vdev_top = vd; 1104 1105 if ((error = vdev_open(vd)) == 0 && 1106 (error = vdev_label_init(vd, crtxg, 1107 VDEV_LABEL_SPARE)) == 0) { 1108 VERIFY(nvlist_add_uint64(spares[i], ZPOOL_CONFIG_GUID, 1109 vd->vdev_guid) == 0); 1110 } 1111 1112 vdev_free(vd); 1113 1114 if (error && mode != VDEV_ALLOC_SPARE) 1115 goto out; 1116 else 1117 error = 0; 1118 } 1119 1120 out: 1121 spa->spa_pending_spares = NULL; 1122 spa->spa_pending_nspares = 0; 1123 return (error); 1124 } 1125 1126 /* 1127 * Pool Creation 1128 */ 1129 int 1130 spa_create(const char *pool, nvlist_t *nvroot, const char *altroot) 1131 { 1132 spa_t *spa; 1133 vdev_t *rvd; 1134 dsl_pool_t *dp; 1135 dmu_tx_t *tx; 1136 int c, error = 0; 1137 uint64_t txg = TXG_INITIAL; 1138 nvlist_t **spares; 1139 uint_t nspares; 1140 1141 /* 1142 * If this pool already exists, return failure. 1143 */ 1144 mutex_enter(&spa_namespace_lock); 1145 if (spa_lookup(pool) != NULL) { 1146 mutex_exit(&spa_namespace_lock); 1147 return (EEXIST); 1148 } 1149 1150 /* 1151 * Allocate a new spa_t structure. 1152 */ 1153 spa = spa_add(pool, altroot); 1154 spa_activate(spa); 1155 1156 spa->spa_uberblock.ub_txg = txg - 1; 1157 spa->spa_uberblock.ub_version = ZFS_VERSION; 1158 spa->spa_ubsync = spa->spa_uberblock; 1159 1160 /* 1161 * Create the root vdev. 1162 */ 1163 spa_config_enter(spa, RW_WRITER, FTAG); 1164 1165 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 1166 1167 ASSERT(error != 0 || rvd != NULL); 1168 ASSERT(error != 0 || spa->spa_root_vdev == rvd); 1169 1170 if (error == 0 && rvd->vdev_children == 0) 1171 error = EINVAL; 1172 1173 if (error == 0 && 1174 (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 1175 (error = spa_validate_spares(spa, nvroot, txg, 1176 VDEV_ALLOC_ADD)) == 0) { 1177 for (c = 0; c < rvd->vdev_children; c++) 1178 vdev_init(rvd->vdev_child[c], txg); 1179 vdev_config_dirty(rvd); 1180 } 1181 1182 spa_config_exit(spa, FTAG); 1183 1184 if (error != 0) { 1185 spa_unload(spa); 1186 spa_deactivate(spa); 1187 spa_remove(spa); 1188 mutex_exit(&spa_namespace_lock); 1189 return (error); 1190 } 1191 1192 /* 1193 * Get the list of spares, if specified. 1194 */ 1195 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 1196 &spares, &nspares) == 0) { 1197 VERIFY(nvlist_alloc(&spa->spa_sparelist, NV_UNIQUE_NAME, 1198 KM_SLEEP) == 0); 1199 VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 1200 ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 1201 spa_config_enter(spa, RW_WRITER, FTAG); 1202 spa_load_spares(spa); 1203 spa_config_exit(spa, FTAG); 1204 spa->spa_sync_spares = B_TRUE; 1205 } 1206 1207 spa->spa_dsl_pool = dp = dsl_pool_create(spa, txg); 1208 spa->spa_meta_objset = dp->dp_meta_objset; 1209 1210 tx = dmu_tx_create_assigned(dp, txg); 1211 1212 /* 1213 * Create the pool config object. 1214 */ 1215 spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 1216 DMU_OT_PACKED_NVLIST, 1 << 14, 1217 DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 1218 1219 if (zap_add(spa->spa_meta_objset, 1220 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 1221 sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 1222 cmn_err(CE_PANIC, "failed to add pool config"); 1223 } 1224 1225 /* Newly created pools are always deflated. */ 1226 spa->spa_deflate = TRUE; 1227 if (zap_add(spa->spa_meta_objset, 1228 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 1229 sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 1230 cmn_err(CE_PANIC, "failed to add deflate"); 1231 } 1232 1233 /* 1234 * Create the deferred-free bplist object. Turn off compression 1235 * because sync-to-convergence takes longer if the blocksize 1236 * keeps changing. 1237 */ 1238 spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset, 1239 1 << 14, tx); 1240 dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 1241 ZIO_COMPRESS_OFF, tx); 1242 1243 if (zap_add(spa->spa_meta_objset, 1244 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 1245 sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) { 1246 cmn_err(CE_PANIC, "failed to add bplist"); 1247 } 1248 1249 /* 1250 * Create the pool's history object. 1251 */ 1252 spa_history_create_obj(spa, tx); 1253 1254 dmu_tx_commit(tx); 1255 1256 spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 1257 spa->spa_sync_on = B_TRUE; 1258 txg_sync_start(spa->spa_dsl_pool); 1259 1260 /* 1261 * We explicitly wait for the first transaction to complete so that our 1262 * bean counters are appropriately updated. 1263 */ 1264 txg_wait_synced(spa->spa_dsl_pool, txg); 1265 1266 spa_config_sync(); 1267 1268 mutex_exit(&spa_namespace_lock); 1269 1270 return (0); 1271 } 1272 1273 /* 1274 * Import the given pool into the system. We set up the necessary spa_t and 1275 * then call spa_load() to do the dirty work. 1276 */ 1277 int 1278 spa_import(const char *pool, nvlist_t *config, const char *altroot) 1279 { 1280 spa_t *spa; 1281 int error; 1282 nvlist_t *nvroot; 1283 nvlist_t **spares; 1284 uint_t nspares; 1285 1286 if (!(spa_mode & FWRITE)) 1287 return (EROFS); 1288 1289 /* 1290 * If a pool with this name exists, return failure. 1291 */ 1292 mutex_enter(&spa_namespace_lock); 1293 if (spa_lookup(pool) != NULL) { 1294 mutex_exit(&spa_namespace_lock); 1295 return (EEXIST); 1296 } 1297 1298 /* 1299 * Create and initialize the spa structure. 1300 */ 1301 spa = spa_add(pool, altroot); 1302 spa_activate(spa); 1303 1304 /* 1305 * Pass off the heavy lifting to spa_load(). 1306 * Pass TRUE for mosconfig because the user-supplied config 1307 * is actually the one to trust when doing an import. 1308 */ 1309 error = spa_load(spa, config, SPA_LOAD_IMPORT, B_TRUE); 1310 1311 spa_config_enter(spa, RW_WRITER, FTAG); 1312 /* 1313 * Toss any existing sparelist, as it doesn't have any validity anymore, 1314 * and conflicts with spa_has_spare(). 1315 */ 1316 if (spa->spa_sparelist) { 1317 nvlist_free(spa->spa_sparelist); 1318 spa->spa_sparelist = NULL; 1319 spa_load_spares(spa); 1320 } 1321 1322 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 1323 &nvroot) == 0); 1324 if (error == 0) 1325 error = spa_validate_spares(spa, nvroot, -1ULL, 1326 VDEV_ALLOC_SPARE); 1327 spa_config_exit(spa, FTAG); 1328 1329 if (error != 0) { 1330 spa_unload(spa); 1331 spa_deactivate(spa); 1332 spa_remove(spa); 1333 mutex_exit(&spa_namespace_lock); 1334 return (error); 1335 } 1336 1337 /* 1338 * Override any spares as specified by the user, as these may have 1339 * correct device names/devids, etc. 1340 */ 1341 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 1342 &spares, &nspares) == 0) { 1343 if (spa->spa_sparelist) 1344 VERIFY(nvlist_remove(spa->spa_sparelist, 1345 ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 1346 else 1347 VERIFY(nvlist_alloc(&spa->spa_sparelist, 1348 NV_UNIQUE_NAME, KM_SLEEP) == 0); 1349 VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 1350 ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 1351 spa_config_enter(spa, RW_WRITER, FTAG); 1352 spa_load_spares(spa); 1353 spa_config_exit(spa, FTAG); 1354 spa->spa_sync_spares = B_TRUE; 1355 } 1356 1357 /* 1358 * Update the config cache to include the newly-imported pool. 1359 */ 1360 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 1361 1362 /* 1363 * Resilver anything that's out of date. 1364 */ 1365 if (spa_mode & FWRITE) 1366 VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 1367 1368 mutex_exit(&spa_namespace_lock); 1369 1370 return (0); 1371 } 1372 1373 /* 1374 * This (illegal) pool name is used when temporarily importing a spa_t in order 1375 * to get the vdev stats associated with the imported devices. 1376 */ 1377 #define TRYIMPORT_NAME "$import" 1378 1379 nvlist_t * 1380 spa_tryimport(nvlist_t *tryconfig) 1381 { 1382 nvlist_t *config = NULL; 1383 char *poolname; 1384 spa_t *spa; 1385 uint64_t state; 1386 1387 if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 1388 return (NULL); 1389 1390 if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 1391 return (NULL); 1392 1393 /* 1394 * Create and initialize the spa structure. 1395 */ 1396 mutex_enter(&spa_namespace_lock); 1397 spa = spa_add(TRYIMPORT_NAME, NULL); 1398 spa_activate(spa); 1399 1400 /* 1401 * Pass off the heavy lifting to spa_load(). 1402 * Pass TRUE for mosconfig because the user-supplied config 1403 * is actually the one to trust when doing an import. 1404 */ 1405 (void) spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE); 1406 1407 /* 1408 * If 'tryconfig' was at least parsable, return the current config. 1409 */ 1410 if (spa->spa_root_vdev != NULL) { 1411 spa_config_enter(spa, RW_READER, FTAG); 1412 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 1413 spa_config_exit(spa, FTAG); 1414 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 1415 poolname) == 0); 1416 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 1417 state) == 0); 1418 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 1419 spa->spa_uberblock.ub_timestamp) == 0); 1420 1421 /* 1422 * Add the list of hot spares. 1423 */ 1424 spa_add_spares(spa, config); 1425 } 1426 1427 spa_unload(spa); 1428 spa_deactivate(spa); 1429 spa_remove(spa); 1430 mutex_exit(&spa_namespace_lock); 1431 1432 return (config); 1433 } 1434 1435 /* 1436 * Pool export/destroy 1437 * 1438 * The act of destroying or exporting a pool is very simple. We make sure there 1439 * is no more pending I/O and any references to the pool are gone. Then, we 1440 * update the pool state and sync all the labels to disk, removing the 1441 * configuration from the cache afterwards. 1442 */ 1443 static int 1444 spa_export_common(char *pool, int new_state, nvlist_t **oldconfig) 1445 { 1446 spa_t *spa; 1447 1448 if (oldconfig) 1449 *oldconfig = NULL; 1450 1451 if (!(spa_mode & FWRITE)) 1452 return (EROFS); 1453 1454 mutex_enter(&spa_namespace_lock); 1455 if ((spa = spa_lookup(pool)) == NULL) { 1456 mutex_exit(&spa_namespace_lock); 1457 return (ENOENT); 1458 } 1459 1460 /* 1461 * Put a hold on the pool, drop the namespace lock, stop async tasks, 1462 * reacquire the namespace lock, and see if we can export. 1463 */ 1464 spa_open_ref(spa, FTAG); 1465 mutex_exit(&spa_namespace_lock); 1466 spa_async_suspend(spa); 1467 mutex_enter(&spa_namespace_lock); 1468 spa_close(spa, FTAG); 1469 1470 /* 1471 * The pool will be in core if it's openable, 1472 * in which case we can modify its state. 1473 */ 1474 if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 1475 /* 1476 * Objsets may be open only because they're dirty, so we 1477 * have to force it to sync before checking spa_refcnt. 1478 */ 1479 spa_scrub_suspend(spa); 1480 txg_wait_synced(spa->spa_dsl_pool, 0); 1481 1482 /* 1483 * A pool cannot be exported or destroyed if there are active 1484 * references. If we are resetting a pool, allow references by 1485 * fault injection handlers. 1486 */ 1487 if (!spa_refcount_zero(spa) || 1488 (spa->spa_inject_ref != 0 && 1489 new_state != POOL_STATE_UNINITIALIZED)) { 1490 spa_scrub_resume(spa); 1491 spa_async_resume(spa); 1492 mutex_exit(&spa_namespace_lock); 1493 return (EBUSY); 1494 } 1495 1496 spa_scrub_resume(spa); 1497 VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0); 1498 1499 /* 1500 * We want this to be reflected on every label, 1501 * so mark them all dirty. spa_unload() will do the 1502 * final sync that pushes these changes out. 1503 */ 1504 if (new_state != POOL_STATE_UNINITIALIZED) { 1505 spa_config_enter(spa, RW_WRITER, FTAG); 1506 spa->spa_state = new_state; 1507 spa->spa_final_txg = spa_last_synced_txg(spa) + 1; 1508 vdev_config_dirty(spa->spa_root_vdev); 1509 spa_config_exit(spa, FTAG); 1510 } 1511 } 1512 1513 spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); 1514 1515 if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 1516 spa_unload(spa); 1517 spa_deactivate(spa); 1518 } 1519 1520 if (oldconfig && spa->spa_config) 1521 VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 1522 1523 if (new_state != POOL_STATE_UNINITIALIZED) { 1524 spa_remove(spa); 1525 spa_config_sync(); 1526 } 1527 mutex_exit(&spa_namespace_lock); 1528 1529 return (0); 1530 } 1531 1532 /* 1533 * Destroy a storage pool. 1534 */ 1535 int 1536 spa_destroy(char *pool) 1537 { 1538 return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL)); 1539 } 1540 1541 /* 1542 * Export a storage pool. 1543 */ 1544 int 1545 spa_export(char *pool, nvlist_t **oldconfig) 1546 { 1547 return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig)); 1548 } 1549 1550 /* 1551 * Similar to spa_export(), this unloads the spa_t without actually removing it 1552 * from the namespace in any way. 1553 */ 1554 int 1555 spa_reset(char *pool) 1556 { 1557 return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL)); 1558 } 1559 1560 1561 /* 1562 * ========================================================================== 1563 * Device manipulation 1564 * ========================================================================== 1565 */ 1566 1567 /* 1568 * Add capacity to a storage pool. 1569 */ 1570 int 1571 spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 1572 { 1573 uint64_t txg; 1574 int c, error; 1575 vdev_t *rvd = spa->spa_root_vdev; 1576 vdev_t *vd, *tvd; 1577 nvlist_t **spares; 1578 uint_t i, nspares; 1579 1580 txg = spa_vdev_enter(spa); 1581 1582 if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 1583 VDEV_ALLOC_ADD)) != 0) 1584 return (spa_vdev_exit(spa, NULL, txg, error)); 1585 1586 spa->spa_pending_vdev = vd; 1587 1588 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 1589 &spares, &nspares) != 0) 1590 nspares = 0; 1591 1592 if (vd->vdev_children == 0 && nspares == 0) { 1593 spa->spa_pending_vdev = NULL; 1594 return (spa_vdev_exit(spa, vd, txg, EINVAL)); 1595 } 1596 1597 if (vd->vdev_children != 0) { 1598 if ((error = vdev_create(vd, txg, B_FALSE)) != 0) { 1599 spa->spa_pending_vdev = NULL; 1600 return (spa_vdev_exit(spa, vd, txg, error)); 1601 } 1602 } 1603 1604 /* 1605 * We must validate the spares after checking the children. Otherwise, 1606 * vdev_inuse() will blindly overwrite the spare. 1607 */ 1608 if ((error = spa_validate_spares(spa, nvroot, txg, 1609 VDEV_ALLOC_ADD)) != 0) { 1610 spa->spa_pending_vdev = NULL; 1611 return (spa_vdev_exit(spa, vd, txg, error)); 1612 } 1613 1614 spa->spa_pending_vdev = NULL; 1615 1616 /* 1617 * Transfer each new top-level vdev from vd to rvd. 1618 */ 1619 for (c = 0; c < vd->vdev_children; c++) { 1620 tvd = vd->vdev_child[c]; 1621 vdev_remove_child(vd, tvd); 1622 tvd->vdev_id = rvd->vdev_children; 1623 vdev_add_child(rvd, tvd); 1624 vdev_config_dirty(tvd); 1625 } 1626 1627 if (nspares != 0) { 1628 if (spa->spa_sparelist != NULL) { 1629 nvlist_t **oldspares; 1630 uint_t oldnspares; 1631 nvlist_t **newspares; 1632 1633 VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist, 1634 ZPOOL_CONFIG_SPARES, &oldspares, &oldnspares) == 0); 1635 1636 newspares = kmem_alloc(sizeof (void *) * 1637 (nspares + oldnspares), KM_SLEEP); 1638 for (i = 0; i < oldnspares; i++) 1639 VERIFY(nvlist_dup(oldspares[i], 1640 &newspares[i], KM_SLEEP) == 0); 1641 for (i = 0; i < nspares; i++) 1642 VERIFY(nvlist_dup(spares[i], 1643 &newspares[i + oldnspares], 1644 KM_SLEEP) == 0); 1645 1646 VERIFY(nvlist_remove(spa->spa_sparelist, 1647 ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 1648 1649 VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 1650 ZPOOL_CONFIG_SPARES, newspares, 1651 nspares + oldnspares) == 0); 1652 for (i = 0; i < oldnspares + nspares; i++) 1653 nvlist_free(newspares[i]); 1654 kmem_free(newspares, (oldnspares + nspares) * 1655 sizeof (void *)); 1656 } else { 1657 VERIFY(nvlist_alloc(&spa->spa_sparelist, 1658 NV_UNIQUE_NAME, KM_SLEEP) == 0); 1659 VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, 1660 ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 1661 } 1662 1663 spa_load_spares(spa); 1664 spa->spa_sync_spares = B_TRUE; 1665 } 1666 1667 /* 1668 * We have to be careful when adding new vdevs to an existing pool. 1669 * If other threads start allocating from these vdevs before we 1670 * sync the config cache, and we lose power, then upon reboot we may 1671 * fail to open the pool because there are DVAs that the config cache 1672 * can't translate. Therefore, we first add the vdevs without 1673 * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 1674 * and then let spa_config_update() initialize the new metaslabs. 1675 * 1676 * spa_load() checks for added-but-not-initialized vdevs, so that 1677 * if we lose power at any point in this sequence, the remaining 1678 * steps will be completed the next time we load the pool. 1679 */ 1680 (void) spa_vdev_exit(spa, vd, txg, 0); 1681 1682 mutex_enter(&spa_namespace_lock); 1683 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 1684 mutex_exit(&spa_namespace_lock); 1685 1686 return (0); 1687 } 1688 1689 /* 1690 * Attach a device to a mirror. The arguments are the path to any device 1691 * in the mirror, and the nvroot for the new device. If the path specifies 1692 * a device that is not mirrored, we automatically insert the mirror vdev. 1693 * 1694 * If 'replacing' is specified, the new device is intended to replace the 1695 * existing device; in this case the two devices are made into their own 1696 * mirror using the 'replacing' vdev, which is functionally identical to 1697 * the mirror vdev (it actually reuses all the same ops) but has a few 1698 * extra rules: you can't attach to it after it's been created, and upon 1699 * completion of resilvering, the first disk (the one being replaced) 1700 * is automatically detached. 1701 */ 1702 int 1703 spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 1704 { 1705 uint64_t txg, open_txg; 1706 int error; 1707 vdev_t *rvd = spa->spa_root_vdev; 1708 vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 1709 vdev_ops_t *pvops; 1710 1711 txg = spa_vdev_enter(spa); 1712 1713 oldvd = vdev_lookup_by_guid(rvd, guid); 1714 1715 if (oldvd == NULL) 1716 return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 1717 1718 if (!oldvd->vdev_ops->vdev_op_leaf) 1719 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 1720 1721 pvd = oldvd->vdev_parent; 1722 1723 if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 1724 VDEV_ALLOC_ADD)) != 0) 1725 return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 1726 1727 if (newrootvd->vdev_children != 1) 1728 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 1729 1730 newvd = newrootvd->vdev_child[0]; 1731 1732 if (!newvd->vdev_ops->vdev_op_leaf) 1733 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 1734 1735 if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 1736 return (spa_vdev_exit(spa, newrootvd, txg, error)); 1737 1738 if (!replacing) { 1739 /* 1740 * For attach, the only allowable parent is a mirror or the root 1741 * vdev. 1742 */ 1743 if (pvd->vdev_ops != &vdev_mirror_ops && 1744 pvd->vdev_ops != &vdev_root_ops) 1745 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 1746 1747 pvops = &vdev_mirror_ops; 1748 } else { 1749 /* 1750 * Active hot spares can only be replaced by inactive hot 1751 * spares. 1752 */ 1753 if (pvd->vdev_ops == &vdev_spare_ops && 1754 pvd->vdev_child[1] == oldvd && 1755 !spa_has_spare(spa, newvd->vdev_guid)) 1756 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 1757 1758 /* 1759 * If the source is a hot spare, and the parent isn't already a 1760 * spare, then we want to create a new hot spare. Otherwise, we 1761 * want to create a replacing vdev. The user is not allowed to 1762 * attach to a spared vdev child unless the 'isspare' state is 1763 * the same (spare replaces spare, non-spare replaces 1764 * non-spare). 1765 */ 1766 if (pvd->vdev_ops == &vdev_replacing_ops) 1767 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 1768 else if (pvd->vdev_ops == &vdev_spare_ops && 1769 newvd->vdev_isspare != oldvd->vdev_isspare) 1770 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 1771 else if (pvd->vdev_ops != &vdev_spare_ops && 1772 newvd->vdev_isspare) 1773 pvops = &vdev_spare_ops; 1774 else 1775 pvops = &vdev_replacing_ops; 1776 } 1777 1778 /* 1779 * Compare the new device size with the replaceable/attachable 1780 * device size. 1781 */ 1782 if (newvd->vdev_psize < vdev_get_rsize(oldvd)) 1783 return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 1784 1785 /* 1786 * The new device cannot have a higher alignment requirement 1787 * than the top-level vdev. 1788 */ 1789 if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 1790 return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 1791 1792 /* 1793 * If this is an in-place replacement, update oldvd's path and devid 1794 * to make it distinguishable from newvd, and unopenable from now on. 1795 */ 1796 if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 1797 spa_strfree(oldvd->vdev_path); 1798 oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 1799 KM_SLEEP); 1800 (void) sprintf(oldvd->vdev_path, "%s/%s", 1801 newvd->vdev_path, "old"); 1802 if (oldvd->vdev_devid != NULL) { 1803 spa_strfree(oldvd->vdev_devid); 1804 oldvd->vdev_devid = NULL; 1805 } 1806 } 1807 1808 /* 1809 * If the parent is not a mirror, or if we're replacing, insert the new 1810 * mirror/replacing/spare vdev above oldvd. 1811 */ 1812 if (pvd->vdev_ops != pvops) 1813 pvd = vdev_add_parent(oldvd, pvops); 1814 1815 ASSERT(pvd->vdev_top->vdev_parent == rvd); 1816 ASSERT(pvd->vdev_ops == pvops); 1817 ASSERT(oldvd->vdev_parent == pvd); 1818 1819 /* 1820 * Extract the new device from its root and add it to pvd. 1821 */ 1822 vdev_remove_child(newrootvd, newvd); 1823 newvd->vdev_id = pvd->vdev_children; 1824 vdev_add_child(pvd, newvd); 1825 1826 /* 1827 * If newvd is smaller than oldvd, but larger than its rsize, 1828 * the addition of newvd may have decreased our parent's asize. 1829 */ 1830 pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize); 1831 1832 tvd = newvd->vdev_top; 1833 ASSERT(pvd->vdev_top == tvd); 1834 ASSERT(tvd->vdev_parent == rvd); 1835 1836 vdev_config_dirty(tvd); 1837 1838 /* 1839 * Set newvd's DTL to [TXG_INITIAL, open_txg]. It will propagate 1840 * upward when spa_vdev_exit() calls vdev_dtl_reassess(). 1841 */ 1842 open_txg = txg + TXG_CONCURRENT_STATES - 1; 1843 1844 mutex_enter(&newvd->vdev_dtl_lock); 1845 space_map_add(&newvd->vdev_dtl_map, TXG_INITIAL, 1846 open_txg - TXG_INITIAL + 1); 1847 mutex_exit(&newvd->vdev_dtl_lock); 1848 1849 if (newvd->vdev_isspare) 1850 spa_spare_activate(newvd); 1851 1852 /* 1853 * Mark newvd's DTL dirty in this txg. 1854 */ 1855 vdev_dirty(tvd, VDD_DTL, newvd, txg); 1856 1857 (void) spa_vdev_exit(spa, newrootvd, open_txg, 0); 1858 1859 /* 1860 * Kick off a resilver to update newvd. We need to grab the namespace 1861 * lock because spa_scrub() needs to post a sysevent with the pool name. 1862 */ 1863 mutex_enter(&spa_namespace_lock); 1864 VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 1865 mutex_exit(&spa_namespace_lock); 1866 1867 return (0); 1868 } 1869 1870 /* 1871 * Detach a device from a mirror or replacing vdev. 1872 * If 'replace_done' is specified, only detach if the parent 1873 * is a replacing vdev. 1874 */ 1875 int 1876 spa_vdev_detach(spa_t *spa, uint64_t guid, int replace_done) 1877 { 1878 uint64_t txg; 1879 int c, t, error; 1880 vdev_t *rvd = spa->spa_root_vdev; 1881 vdev_t *vd, *pvd, *cvd, *tvd; 1882 boolean_t unspare = B_FALSE; 1883 uint64_t unspare_guid; 1884 1885 txg = spa_vdev_enter(spa); 1886 1887 vd = vdev_lookup_by_guid(rvd, guid); 1888 1889 if (vd == NULL) 1890 return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 1891 1892 if (!vd->vdev_ops->vdev_op_leaf) 1893 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 1894 1895 pvd = vd->vdev_parent; 1896 1897 /* 1898 * If replace_done is specified, only remove this device if it's 1899 * the first child of a replacing vdev. For the 'spare' vdev, either 1900 * disk can be removed. 1901 */ 1902 if (replace_done) { 1903 if (pvd->vdev_ops == &vdev_replacing_ops) { 1904 if (vd->vdev_id != 0) 1905 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 1906 } else if (pvd->vdev_ops != &vdev_spare_ops) { 1907 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 1908 } 1909 } 1910 1911 ASSERT(pvd->vdev_ops != &vdev_spare_ops || 1912 spa_version(spa) >= ZFS_VERSION_SPARES); 1913 1914 /* 1915 * Only mirror, replacing, and spare vdevs support detach. 1916 */ 1917 if (pvd->vdev_ops != &vdev_replacing_ops && 1918 pvd->vdev_ops != &vdev_mirror_ops && 1919 pvd->vdev_ops != &vdev_spare_ops) 1920 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 1921 1922 /* 1923 * If there's only one replica, you can't detach it. 1924 */ 1925 if (pvd->vdev_children <= 1) 1926 return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 1927 1928 /* 1929 * If all siblings have non-empty DTLs, this device may have the only 1930 * valid copy of the data, which means we cannot safely detach it. 1931 * 1932 * XXX -- as in the vdev_offline() case, we really want a more 1933 * precise DTL check. 1934 */ 1935 for (c = 0; c < pvd->vdev_children; c++) { 1936 uint64_t dirty; 1937 1938 cvd = pvd->vdev_child[c]; 1939 if (cvd == vd) 1940 continue; 1941 if (vdev_is_dead(cvd)) 1942 continue; 1943 mutex_enter(&cvd->vdev_dtl_lock); 1944 dirty = cvd->vdev_dtl_map.sm_space | 1945 cvd->vdev_dtl_scrub.sm_space; 1946 mutex_exit(&cvd->vdev_dtl_lock); 1947 if (!dirty) 1948 break; 1949 } 1950 1951 /* 1952 * If we are a replacing or spare vdev, then we can always detach the 1953 * latter child, as that is how one cancels the operation. 1954 */ 1955 if ((pvd->vdev_ops == &vdev_mirror_ops || vd->vdev_id != 1) && 1956 c == pvd->vdev_children) 1957 return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 1958 1959 /* 1960 * If we are detaching the original disk from a spare, then it implies 1961 * that the spare should become a real disk, and be removed from the 1962 * active spare list for the pool. 1963 */ 1964 if (pvd->vdev_ops == &vdev_spare_ops && 1965 vd->vdev_id == 0) 1966 unspare = B_TRUE; 1967 1968 /* 1969 * Erase the disk labels so the disk can be used for other things. 1970 * This must be done after all other error cases are handled, 1971 * but before we disembowel vd (so we can still do I/O to it). 1972 * But if we can't do it, don't treat the error as fatal -- 1973 * it may be that the unwritability of the disk is the reason 1974 * it's being detached! 1975 */ 1976 error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 1977 1978 /* 1979 * Remove vd from its parent and compact the parent's children. 1980 */ 1981 vdev_remove_child(pvd, vd); 1982 vdev_compact_children(pvd); 1983 1984 /* 1985 * Remember one of the remaining children so we can get tvd below. 1986 */ 1987 cvd = pvd->vdev_child[0]; 1988 1989 /* 1990 * If we need to remove the remaining child from the list of hot spares, 1991 * do it now, marking the vdev as no longer a spare in the process. We 1992 * must do this before vdev_remove_parent(), because that can change the 1993 * GUID if it creates a new toplevel GUID. 1994 */ 1995 if (unspare) { 1996 ASSERT(cvd->vdev_isspare); 1997 spa_spare_remove(cvd); 1998 unspare_guid = cvd->vdev_guid; 1999 } 2000 2001 /* 2002 * If the parent mirror/replacing vdev only has one child, 2003 * the parent is no longer needed. Remove it from the tree. 2004 */ 2005 if (pvd->vdev_children == 1) 2006 vdev_remove_parent(cvd); 2007 2008 /* 2009 * We don't set tvd until now because the parent we just removed 2010 * may have been the previous top-level vdev. 2011 */ 2012 tvd = cvd->vdev_top; 2013 ASSERT(tvd->vdev_parent == rvd); 2014 2015 /* 2016 * Reevaluate the parent vdev state. 2017 */ 2018 vdev_propagate_state(cvd); 2019 2020 /* 2021 * If the device we just detached was smaller than the others, it may be 2022 * possible to add metaslabs (i.e. grow the pool). vdev_metaslab_init() 2023 * can't fail because the existing metaslabs are already in core, so 2024 * there's nothing to read from disk. 2025 */ 2026 VERIFY(vdev_metaslab_init(tvd, txg) == 0); 2027 2028 vdev_config_dirty(tvd); 2029 2030 /* 2031 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 2032 * vd->vdev_detached is set and free vd's DTL object in syncing context. 2033 * But first make sure we're not on any *other* txg's DTL list, to 2034 * prevent vd from being accessed after it's freed. 2035 */ 2036 for (t = 0; t < TXG_SIZE; t++) 2037 (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 2038 vd->vdev_detached = B_TRUE; 2039 vdev_dirty(tvd, VDD_DTL, vd, txg); 2040 2041 spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); 2042 2043 error = spa_vdev_exit(spa, vd, txg, 0); 2044 2045 /* 2046 * If this was the removal of the original device in a hot spare vdev, 2047 * then we want to go through and remove the device from the hot spare 2048 * list of every other pool. 2049 */ 2050 if (unspare) { 2051 spa = NULL; 2052 mutex_enter(&spa_namespace_lock); 2053 while ((spa = spa_next(spa)) != NULL) { 2054 if (spa->spa_state != POOL_STATE_ACTIVE) 2055 continue; 2056 2057 (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 2058 } 2059 mutex_exit(&spa_namespace_lock); 2060 } 2061 2062 return (error); 2063 } 2064 2065 /* 2066 * Remove a device from the pool. Currently, this supports removing only hot 2067 * spares. 2068 */ 2069 int 2070 spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 2071 { 2072 vdev_t *vd; 2073 nvlist_t **spares, *nv, **newspares; 2074 uint_t i, j, nspares; 2075 int ret = 0; 2076 2077 spa_config_enter(spa, RW_WRITER, FTAG); 2078 2079 vd = spa_lookup_by_guid(spa, guid); 2080 2081 nv = NULL; 2082 if (spa->spa_spares != NULL && 2083 nvlist_lookup_nvlist_array(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 2084 &spares, &nspares) == 0) { 2085 for (i = 0; i < nspares; i++) { 2086 uint64_t theguid; 2087 2088 VERIFY(nvlist_lookup_uint64(spares[i], 2089 ZPOOL_CONFIG_GUID, &theguid) == 0); 2090 if (theguid == guid) { 2091 nv = spares[i]; 2092 break; 2093 } 2094 } 2095 } 2096 2097 /* 2098 * We only support removing a hot spare, and only if it's not currently 2099 * in use in this pool. 2100 */ 2101 if (nv == NULL && vd == NULL) { 2102 ret = ENOENT; 2103 goto out; 2104 } 2105 2106 if (nv == NULL && vd != NULL) { 2107 ret = ENOTSUP; 2108 goto out; 2109 } 2110 2111 if (!unspare && nv != NULL && vd != NULL) { 2112 ret = EBUSY; 2113 goto out; 2114 } 2115 2116 if (nspares == 1) { 2117 newspares = NULL; 2118 } else { 2119 newspares = kmem_alloc((nspares - 1) * sizeof (void *), 2120 KM_SLEEP); 2121 for (i = 0, j = 0; i < nspares; i++) { 2122 if (spares[i] != nv) 2123 VERIFY(nvlist_dup(spares[i], 2124 &newspares[j++], KM_SLEEP) == 0); 2125 } 2126 } 2127 2128 VERIFY(nvlist_remove(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 2129 DATA_TYPE_NVLIST_ARRAY) == 0); 2130 VERIFY(nvlist_add_nvlist_array(spa->spa_sparelist, ZPOOL_CONFIG_SPARES, 2131 newspares, nspares - 1) == 0); 2132 for (i = 0; i < nspares - 1; i++) 2133 nvlist_free(newspares[i]); 2134 kmem_free(newspares, (nspares - 1) * sizeof (void *)); 2135 spa_load_spares(spa); 2136 spa->spa_sync_spares = B_TRUE; 2137 2138 out: 2139 spa_config_exit(spa, FTAG); 2140 2141 return (ret); 2142 } 2143 2144 /* 2145 * Find any device that's done replacing, or a vdev marked 'unspare' that's 2146 * current spared, so we can detach it. 2147 */ 2148 static vdev_t * 2149 spa_vdev_resilver_done_hunt(vdev_t *vd) 2150 { 2151 vdev_t *newvd, *oldvd; 2152 int c; 2153 2154 for (c = 0; c < vd->vdev_children; c++) { 2155 oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 2156 if (oldvd != NULL) 2157 return (oldvd); 2158 } 2159 2160 /* 2161 * Check for a completed replacement. 2162 */ 2163 if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) { 2164 oldvd = vd->vdev_child[0]; 2165 newvd = vd->vdev_child[1]; 2166 2167 mutex_enter(&newvd->vdev_dtl_lock); 2168 if (newvd->vdev_dtl_map.sm_space == 0 && 2169 newvd->vdev_dtl_scrub.sm_space == 0) { 2170 mutex_exit(&newvd->vdev_dtl_lock); 2171 return (oldvd); 2172 } 2173 mutex_exit(&newvd->vdev_dtl_lock); 2174 } 2175 2176 /* 2177 * Check for a completed resilver with the 'unspare' flag set. 2178 */ 2179 if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) { 2180 newvd = vd->vdev_child[0]; 2181 oldvd = vd->vdev_child[1]; 2182 2183 mutex_enter(&newvd->vdev_dtl_lock); 2184 if (newvd->vdev_unspare && 2185 newvd->vdev_dtl_map.sm_space == 0 && 2186 newvd->vdev_dtl_scrub.sm_space == 0) { 2187 newvd->vdev_unspare = 0; 2188 mutex_exit(&newvd->vdev_dtl_lock); 2189 return (oldvd); 2190 } 2191 mutex_exit(&newvd->vdev_dtl_lock); 2192 } 2193 2194 return (NULL); 2195 } 2196 2197 static void 2198 spa_vdev_resilver_done(spa_t *spa) 2199 { 2200 vdev_t *vd; 2201 vdev_t *pvd; 2202 uint64_t guid; 2203 uint64_t pguid = 0; 2204 2205 spa_config_enter(spa, RW_READER, FTAG); 2206 2207 while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 2208 guid = vd->vdev_guid; 2209 /* 2210 * If we have just finished replacing a hot spared device, then 2211 * we need to detach the parent's first child (the original hot 2212 * spare) as well. 2213 */ 2214 pvd = vd->vdev_parent; 2215 if (pvd->vdev_parent->vdev_ops == &vdev_spare_ops && 2216 pvd->vdev_id == 0) { 2217 ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 2218 ASSERT(pvd->vdev_parent->vdev_children == 2); 2219 pguid = pvd->vdev_parent->vdev_child[1]->vdev_guid; 2220 } 2221 spa_config_exit(spa, FTAG); 2222 if (spa_vdev_detach(spa, guid, B_TRUE) != 0) 2223 return; 2224 if (pguid != 0 && spa_vdev_detach(spa, pguid, B_TRUE) != 0) 2225 return; 2226 spa_config_enter(spa, RW_READER, FTAG); 2227 } 2228 2229 spa_config_exit(spa, FTAG); 2230 } 2231 2232 /* 2233 * Update the stored path for this vdev. Dirty the vdev configuration, relying 2234 * on spa_vdev_enter/exit() to synchronize the labels and cache. 2235 */ 2236 int 2237 spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 2238 { 2239 vdev_t *rvd, *vd; 2240 uint64_t txg; 2241 2242 rvd = spa->spa_root_vdev; 2243 2244 txg = spa_vdev_enter(spa); 2245 2246 if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL) { 2247 /* 2248 * Determine if this is a reference to a hot spare. In that 2249 * case, update the path as stored in the spare list. 2250 */ 2251 nvlist_t **spares; 2252 uint_t i, nspares; 2253 if (spa->spa_sparelist != NULL) { 2254 VERIFY(nvlist_lookup_nvlist_array(spa->spa_sparelist, 2255 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 2256 for (i = 0; i < nspares; i++) { 2257 uint64_t theguid; 2258 VERIFY(nvlist_lookup_uint64(spares[i], 2259 ZPOOL_CONFIG_GUID, &theguid) == 0); 2260 if (theguid == guid) 2261 break; 2262 } 2263 2264 if (i == nspares) 2265 return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 2266 2267 VERIFY(nvlist_add_string(spares[i], 2268 ZPOOL_CONFIG_PATH, newpath) == 0); 2269 spa_load_spares(spa); 2270 spa->spa_sync_spares = B_TRUE; 2271 return (spa_vdev_exit(spa, NULL, txg, 0)); 2272 } else { 2273 return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 2274 } 2275 } 2276 2277 if (!vd->vdev_ops->vdev_op_leaf) 2278 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 2279 2280 spa_strfree(vd->vdev_path); 2281 vd->vdev_path = spa_strdup(newpath); 2282 2283 vdev_config_dirty(vd->vdev_top); 2284 2285 return (spa_vdev_exit(spa, NULL, txg, 0)); 2286 } 2287 2288 /* 2289 * ========================================================================== 2290 * SPA Scrubbing 2291 * ========================================================================== 2292 */ 2293 2294 static void 2295 spa_scrub_io_done(zio_t *zio) 2296 { 2297 spa_t *spa = zio->io_spa; 2298 2299 arc_data_buf_free(zio->io_data, zio->io_size); 2300 2301 mutex_enter(&spa->spa_scrub_lock); 2302 if (zio->io_error && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) { 2303 vdev_t *vd = zio->io_vd ? zio->io_vd : spa->spa_root_vdev; 2304 spa->spa_scrub_errors++; 2305 mutex_enter(&vd->vdev_stat_lock); 2306 vd->vdev_stat.vs_scrub_errors++; 2307 mutex_exit(&vd->vdev_stat_lock); 2308 } 2309 2310 if (--spa->spa_scrub_inflight < spa->spa_scrub_maxinflight) 2311 cv_broadcast(&spa->spa_scrub_io_cv); 2312 2313 ASSERT(spa->spa_scrub_inflight >= 0); 2314 2315 mutex_exit(&spa->spa_scrub_lock); 2316 } 2317 2318 static void 2319 spa_scrub_io_start(spa_t *spa, blkptr_t *bp, int priority, int flags, 2320 zbookmark_t *zb) 2321 { 2322 size_t size = BP_GET_LSIZE(bp); 2323 void *data; 2324 2325 mutex_enter(&spa->spa_scrub_lock); 2326 /* 2327 * Do not give too much work to vdev(s). 2328 */ 2329 while (spa->spa_scrub_inflight >= spa->spa_scrub_maxinflight) { 2330 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 2331 } 2332 spa->spa_scrub_inflight++; 2333 mutex_exit(&spa->spa_scrub_lock); 2334 2335 data = arc_data_buf_alloc(size); 2336 2337 if (zb->zb_level == -1 && BP_GET_TYPE(bp) != DMU_OT_OBJSET) 2338 flags |= ZIO_FLAG_SPECULATIVE; /* intent log block */ 2339 2340 flags |= ZIO_FLAG_SCRUB_THREAD | ZIO_FLAG_CANFAIL; 2341 2342 zio_nowait(zio_read(NULL, spa, bp, data, size, 2343 spa_scrub_io_done, NULL, priority, flags, zb)); 2344 } 2345 2346 /* ARGSUSED */ 2347 static int 2348 spa_scrub_cb(traverse_blk_cache_t *bc, spa_t *spa, void *a) 2349 { 2350 blkptr_t *bp = &bc->bc_blkptr; 2351 vdev_t *vd = spa->spa_root_vdev; 2352 dva_t *dva = bp->blk_dva; 2353 int needs_resilver = B_FALSE; 2354 int d; 2355 2356 if (bc->bc_errno) { 2357 /* 2358 * We can't scrub this block, but we can continue to scrub 2359 * the rest of the pool. Note the error and move along. 2360 */ 2361 mutex_enter(&spa->spa_scrub_lock); 2362 spa->spa_scrub_errors++; 2363 mutex_exit(&spa->spa_scrub_lock); 2364 2365 mutex_enter(&vd->vdev_stat_lock); 2366 vd->vdev_stat.vs_scrub_errors++; 2367 mutex_exit(&vd->vdev_stat_lock); 2368 2369 return (ERESTART); 2370 } 2371 2372 ASSERT(bp->blk_birth < spa->spa_scrub_maxtxg); 2373 2374 for (d = 0; d < BP_GET_NDVAS(bp); d++) { 2375 vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[d])); 2376 2377 ASSERT(vd != NULL); 2378 2379 /* 2380 * Keep track of how much data we've examined so that 2381 * zpool(1M) status can make useful progress reports. 2382 */ 2383 mutex_enter(&vd->vdev_stat_lock); 2384 vd->vdev_stat.vs_scrub_examined += DVA_GET_ASIZE(&dva[d]); 2385 mutex_exit(&vd->vdev_stat_lock); 2386 2387 if (spa->spa_scrub_type == POOL_SCRUB_RESILVER) { 2388 if (DVA_GET_GANG(&dva[d])) { 2389 /* 2390 * Gang members may be spread across multiple 2391 * vdevs, so the best we can do is look at the 2392 * pool-wide DTL. 2393 * XXX -- it would be better to change our 2394 * allocation policy to ensure that this can't 2395 * happen. 2396 */ 2397 vd = spa->spa_root_vdev; 2398 } 2399 if (vdev_dtl_contains(&vd->vdev_dtl_map, 2400 bp->blk_birth, 1)) 2401 needs_resilver = B_TRUE; 2402 } 2403 } 2404 2405 if (spa->spa_scrub_type == POOL_SCRUB_EVERYTHING) 2406 spa_scrub_io_start(spa, bp, ZIO_PRIORITY_SCRUB, 2407 ZIO_FLAG_SCRUB, &bc->bc_bookmark); 2408 else if (needs_resilver) 2409 spa_scrub_io_start(spa, bp, ZIO_PRIORITY_RESILVER, 2410 ZIO_FLAG_RESILVER, &bc->bc_bookmark); 2411 2412 return (0); 2413 } 2414 2415 static void 2416 spa_scrub_thread(spa_t *spa) 2417 { 2418 callb_cpr_t cprinfo; 2419 traverse_handle_t *th = spa->spa_scrub_th; 2420 vdev_t *rvd = spa->spa_root_vdev; 2421 pool_scrub_type_t scrub_type = spa->spa_scrub_type; 2422 int error = 0; 2423 boolean_t complete; 2424 2425 CALLB_CPR_INIT(&cprinfo, &spa->spa_scrub_lock, callb_generic_cpr, FTAG); 2426 2427 /* 2428 * If we're restarting due to a snapshot create/delete, 2429 * wait for that to complete. 2430 */ 2431 txg_wait_synced(spa_get_dsl(spa), 0); 2432 2433 dprintf("start %s mintxg=%llu maxtxg=%llu\n", 2434 scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub", 2435 spa->spa_scrub_mintxg, spa->spa_scrub_maxtxg); 2436 2437 spa_config_enter(spa, RW_WRITER, FTAG); 2438 vdev_reopen(rvd); /* purge all vdev caches */ 2439 vdev_config_dirty(rvd); /* rewrite all disk labels */ 2440 vdev_scrub_stat_update(rvd, scrub_type, B_FALSE); 2441 spa_config_exit(spa, FTAG); 2442 2443 mutex_enter(&spa->spa_scrub_lock); 2444 spa->spa_scrub_errors = 0; 2445 spa->spa_scrub_active = 1; 2446 ASSERT(spa->spa_scrub_inflight == 0); 2447 2448 while (!spa->spa_scrub_stop) { 2449 CALLB_CPR_SAFE_BEGIN(&cprinfo); 2450 while (spa->spa_scrub_suspended) { 2451 spa->spa_scrub_active = 0; 2452 cv_broadcast(&spa->spa_scrub_cv); 2453 cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 2454 spa->spa_scrub_active = 1; 2455 } 2456 CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_scrub_lock); 2457 2458 if (spa->spa_scrub_restart_txg != 0) 2459 break; 2460 2461 mutex_exit(&spa->spa_scrub_lock); 2462 error = traverse_more(th); 2463 mutex_enter(&spa->spa_scrub_lock); 2464 if (error != EAGAIN) 2465 break; 2466 } 2467 2468 while (spa->spa_scrub_inflight) 2469 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 2470 2471 spa->spa_scrub_active = 0; 2472 cv_broadcast(&spa->spa_scrub_cv); 2473 2474 mutex_exit(&spa->spa_scrub_lock); 2475 2476 spa_config_enter(spa, RW_WRITER, FTAG); 2477 2478 mutex_enter(&spa->spa_scrub_lock); 2479 2480 /* 2481 * Note: we check spa_scrub_restart_txg under both spa_scrub_lock 2482 * AND the spa config lock to synchronize with any config changes 2483 * that revise the DTLs under spa_vdev_enter() / spa_vdev_exit(). 2484 */ 2485 if (spa->spa_scrub_restart_txg != 0) 2486 error = ERESTART; 2487 2488 if (spa->spa_scrub_stop) 2489 error = EINTR; 2490 2491 /* 2492 * Even if there were uncorrectable errors, we consider the scrub 2493 * completed. The downside is that if there is a transient error during 2494 * a resilver, we won't resilver the data properly to the target. But 2495 * if the damage is permanent (more likely) we will resilver forever, 2496 * which isn't really acceptable. Since there is enough information for 2497 * the user to know what has failed and why, this seems like a more 2498 * tractable approach. 2499 */ 2500 complete = (error == 0); 2501 2502 dprintf("end %s to maxtxg=%llu %s, traverse=%d, %llu errors, stop=%u\n", 2503 scrub_type == POOL_SCRUB_RESILVER ? "resilver" : "scrub", 2504 spa->spa_scrub_maxtxg, complete ? "done" : "FAILED", 2505 error, spa->spa_scrub_errors, spa->spa_scrub_stop); 2506 2507 mutex_exit(&spa->spa_scrub_lock); 2508 2509 /* 2510 * If the scrub/resilver completed, update all DTLs to reflect this. 2511 * Whether it succeeded or not, vacate all temporary scrub DTLs. 2512 */ 2513 vdev_dtl_reassess(rvd, spa_last_synced_txg(spa) + 1, 2514 complete ? spa->spa_scrub_maxtxg : 0, B_TRUE); 2515 vdev_scrub_stat_update(rvd, POOL_SCRUB_NONE, complete); 2516 spa_errlog_rotate(spa); 2517 2518 if (scrub_type == POOL_SCRUB_RESILVER && complete) 2519 spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_FINISH); 2520 2521 spa_config_exit(spa, FTAG); 2522 2523 mutex_enter(&spa->spa_scrub_lock); 2524 2525 /* 2526 * We may have finished replacing a device. 2527 * Let the async thread assess this and handle the detach. 2528 */ 2529 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 2530 2531 /* 2532 * If we were told to restart, our final act is to start a new scrub. 2533 */ 2534 if (error == ERESTART) 2535 spa_async_request(spa, scrub_type == POOL_SCRUB_RESILVER ? 2536 SPA_ASYNC_RESILVER : SPA_ASYNC_SCRUB); 2537 2538 spa->spa_scrub_type = POOL_SCRUB_NONE; 2539 spa->spa_scrub_active = 0; 2540 spa->spa_scrub_thread = NULL; 2541 cv_broadcast(&spa->spa_scrub_cv); 2542 CALLB_CPR_EXIT(&cprinfo); /* drops &spa->spa_scrub_lock */ 2543 thread_exit(); 2544 } 2545 2546 void 2547 spa_scrub_suspend(spa_t *spa) 2548 { 2549 mutex_enter(&spa->spa_scrub_lock); 2550 spa->spa_scrub_suspended++; 2551 while (spa->spa_scrub_active) { 2552 cv_broadcast(&spa->spa_scrub_cv); 2553 cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 2554 } 2555 while (spa->spa_scrub_inflight) 2556 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 2557 mutex_exit(&spa->spa_scrub_lock); 2558 } 2559 2560 void 2561 spa_scrub_resume(spa_t *spa) 2562 { 2563 mutex_enter(&spa->spa_scrub_lock); 2564 ASSERT(spa->spa_scrub_suspended != 0); 2565 if (--spa->spa_scrub_suspended == 0) 2566 cv_broadcast(&spa->spa_scrub_cv); 2567 mutex_exit(&spa->spa_scrub_lock); 2568 } 2569 2570 void 2571 spa_scrub_restart(spa_t *spa, uint64_t txg) 2572 { 2573 /* 2574 * Something happened (e.g. snapshot create/delete) that means 2575 * we must restart any in-progress scrubs. The itinerary will 2576 * fix this properly. 2577 */ 2578 mutex_enter(&spa->spa_scrub_lock); 2579 spa->spa_scrub_restart_txg = txg; 2580 mutex_exit(&spa->spa_scrub_lock); 2581 } 2582 2583 int 2584 spa_scrub(spa_t *spa, pool_scrub_type_t type, boolean_t force) 2585 { 2586 space_seg_t *ss; 2587 uint64_t mintxg, maxtxg; 2588 vdev_t *rvd = spa->spa_root_vdev; 2589 2590 if ((uint_t)type >= POOL_SCRUB_TYPES) 2591 return (ENOTSUP); 2592 2593 mutex_enter(&spa->spa_scrub_lock); 2594 2595 /* 2596 * If there's a scrub or resilver already in progress, stop it. 2597 */ 2598 while (spa->spa_scrub_thread != NULL) { 2599 /* 2600 * Don't stop a resilver unless forced. 2601 */ 2602 if (spa->spa_scrub_type == POOL_SCRUB_RESILVER && !force) { 2603 mutex_exit(&spa->spa_scrub_lock); 2604 return (EBUSY); 2605 } 2606 spa->spa_scrub_stop = 1; 2607 cv_broadcast(&spa->spa_scrub_cv); 2608 cv_wait(&spa->spa_scrub_cv, &spa->spa_scrub_lock); 2609 } 2610 2611 /* 2612 * Terminate the previous traverse. 2613 */ 2614 if (spa->spa_scrub_th != NULL) { 2615 traverse_fini(spa->spa_scrub_th); 2616 spa->spa_scrub_th = NULL; 2617 } 2618 2619 if (rvd == NULL) { 2620 ASSERT(spa->spa_scrub_stop == 0); 2621 ASSERT(spa->spa_scrub_type == type); 2622 ASSERT(spa->spa_scrub_restart_txg == 0); 2623 mutex_exit(&spa->spa_scrub_lock); 2624 return (0); 2625 } 2626 2627 mintxg = TXG_INITIAL - 1; 2628 maxtxg = spa_last_synced_txg(spa) + 1; 2629 2630 mutex_enter(&rvd->vdev_dtl_lock); 2631 2632 if (rvd->vdev_dtl_map.sm_space == 0) { 2633 /* 2634 * The pool-wide DTL is empty. 2635 * If this is a resilver, there's nothing to do except 2636 * check whether any in-progress replacements have completed. 2637 */ 2638 if (type == POOL_SCRUB_RESILVER) { 2639 type = POOL_SCRUB_NONE; 2640 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 2641 } 2642 } else { 2643 /* 2644 * The pool-wide DTL is non-empty. 2645 * If this is a normal scrub, upgrade to a resilver instead. 2646 */ 2647 if (type == POOL_SCRUB_EVERYTHING) 2648 type = POOL_SCRUB_RESILVER; 2649 } 2650 2651 if (type == POOL_SCRUB_RESILVER) { 2652 /* 2653 * Determine the resilvering boundaries. 2654 * 2655 * Note: (mintxg, maxtxg) is an open interval, 2656 * i.e. mintxg and maxtxg themselves are not included. 2657 * 2658 * Note: for maxtxg, we MIN with spa_last_synced_txg(spa) + 1 2659 * so we don't claim to resilver a txg that's still changing. 2660 */ 2661 ss = avl_first(&rvd->vdev_dtl_map.sm_root); 2662 mintxg = ss->ss_start - 1; 2663 ss = avl_last(&rvd->vdev_dtl_map.sm_root); 2664 maxtxg = MIN(ss->ss_end, maxtxg); 2665 2666 spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_START); 2667 } 2668 2669 mutex_exit(&rvd->vdev_dtl_lock); 2670 2671 spa->spa_scrub_stop = 0; 2672 spa->spa_scrub_type = type; 2673 spa->spa_scrub_restart_txg = 0; 2674 2675 if (type != POOL_SCRUB_NONE) { 2676 spa->spa_scrub_mintxg = mintxg; 2677 spa->spa_scrub_maxtxg = maxtxg; 2678 spa->spa_scrub_th = traverse_init(spa, spa_scrub_cb, NULL, 2679 ADVANCE_PRE | ADVANCE_PRUNE | ADVANCE_ZIL, 2680 ZIO_FLAG_CANFAIL); 2681 traverse_add_pool(spa->spa_scrub_th, mintxg, maxtxg); 2682 spa->spa_scrub_thread = thread_create(NULL, 0, 2683 spa_scrub_thread, spa, 0, &p0, TS_RUN, minclsyspri); 2684 } 2685 2686 mutex_exit(&spa->spa_scrub_lock); 2687 2688 return (0); 2689 } 2690 2691 /* 2692 * ========================================================================== 2693 * SPA async task processing 2694 * ========================================================================== 2695 */ 2696 2697 static void 2698 spa_async_remove(spa_t *spa, vdev_t *vd) 2699 { 2700 vdev_t *tvd; 2701 int c; 2702 2703 for (c = 0; c < vd->vdev_children; c++) { 2704 tvd = vd->vdev_child[c]; 2705 if (tvd->vdev_remove_wanted) { 2706 tvd->vdev_remove_wanted = 0; 2707 vdev_set_state(tvd, B_FALSE, VDEV_STATE_REMOVED, 2708 VDEV_AUX_NONE); 2709 vdev_clear(spa, tvd); 2710 vdev_config_dirty(tvd->vdev_top); 2711 } 2712 spa_async_remove(spa, tvd); 2713 } 2714 } 2715 2716 static void 2717 spa_async_thread(spa_t *spa) 2718 { 2719 int tasks; 2720 uint64_t txg; 2721 2722 ASSERT(spa->spa_sync_on); 2723 2724 mutex_enter(&spa->spa_async_lock); 2725 tasks = spa->spa_async_tasks; 2726 spa->spa_async_tasks = 0; 2727 mutex_exit(&spa->spa_async_lock); 2728 2729 /* 2730 * See if the config needs to be updated. 2731 */ 2732 if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 2733 mutex_enter(&spa_namespace_lock); 2734 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 2735 mutex_exit(&spa_namespace_lock); 2736 } 2737 2738 /* 2739 * See if any devices need to be marked REMOVED. 2740 */ 2741 if (tasks & SPA_ASYNC_REMOVE) { 2742 txg = spa_vdev_enter(spa); 2743 spa_async_remove(spa, spa->spa_root_vdev); 2744 (void) spa_vdev_exit(spa, NULL, txg, 0); 2745 } 2746 2747 /* 2748 * If any devices are done replacing, detach them. 2749 */ 2750 if (tasks & SPA_ASYNC_RESILVER_DONE) 2751 spa_vdev_resilver_done(spa); 2752 2753 /* 2754 * Kick off a scrub. When starting a RESILVER scrub (or an EVERYTHING 2755 * scrub which can become a resilver), we need to hold 2756 * spa_namespace_lock() because the sysevent we post via 2757 * spa_event_notify() needs to get the name of the pool. 2758 */ 2759 if (tasks & SPA_ASYNC_SCRUB) { 2760 mutex_enter(&spa_namespace_lock); 2761 VERIFY(spa_scrub(spa, POOL_SCRUB_EVERYTHING, B_TRUE) == 0); 2762 mutex_exit(&spa_namespace_lock); 2763 } 2764 2765 /* 2766 * Kick off a resilver. 2767 */ 2768 if (tasks & SPA_ASYNC_RESILVER) { 2769 mutex_enter(&spa_namespace_lock); 2770 VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 2771 mutex_exit(&spa_namespace_lock); 2772 } 2773 2774 /* 2775 * Let the world know that we're done. 2776 */ 2777 mutex_enter(&spa->spa_async_lock); 2778 spa->spa_async_thread = NULL; 2779 cv_broadcast(&spa->spa_async_cv); 2780 mutex_exit(&spa->spa_async_lock); 2781 thread_exit(); 2782 } 2783 2784 void 2785 spa_async_suspend(spa_t *spa) 2786 { 2787 mutex_enter(&spa->spa_async_lock); 2788 spa->spa_async_suspended++; 2789 while (spa->spa_async_thread != NULL) 2790 cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 2791 mutex_exit(&spa->spa_async_lock); 2792 } 2793 2794 void 2795 spa_async_resume(spa_t *spa) 2796 { 2797 mutex_enter(&spa->spa_async_lock); 2798 ASSERT(spa->spa_async_suspended != 0); 2799 spa->spa_async_suspended--; 2800 mutex_exit(&spa->spa_async_lock); 2801 } 2802 2803 static void 2804 spa_async_dispatch(spa_t *spa) 2805 { 2806 mutex_enter(&spa->spa_async_lock); 2807 if (spa->spa_async_tasks && !spa->spa_async_suspended && 2808 spa->spa_async_thread == NULL && 2809 rootdir != NULL && !vn_is_readonly(rootdir)) 2810 spa->spa_async_thread = thread_create(NULL, 0, 2811 spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 2812 mutex_exit(&spa->spa_async_lock); 2813 } 2814 2815 void 2816 spa_async_request(spa_t *spa, int task) 2817 { 2818 mutex_enter(&spa->spa_async_lock); 2819 spa->spa_async_tasks |= task; 2820 mutex_exit(&spa->spa_async_lock); 2821 } 2822 2823 /* 2824 * ========================================================================== 2825 * SPA syncing routines 2826 * ========================================================================== 2827 */ 2828 2829 static void 2830 spa_sync_deferred_frees(spa_t *spa, uint64_t txg) 2831 { 2832 bplist_t *bpl = &spa->spa_sync_bplist; 2833 dmu_tx_t *tx; 2834 blkptr_t blk; 2835 uint64_t itor = 0; 2836 zio_t *zio; 2837 int error; 2838 uint8_t c = 1; 2839 2840 zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CONFIG_HELD); 2841 2842 while (bplist_iterate(bpl, &itor, &blk) == 0) 2843 zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL)); 2844 2845 error = zio_wait(zio); 2846 ASSERT3U(error, ==, 0); 2847 2848 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 2849 bplist_vacate(bpl, tx); 2850 2851 /* 2852 * Pre-dirty the first block so we sync to convergence faster. 2853 * (Usually only the first block is needed.) 2854 */ 2855 dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx); 2856 dmu_tx_commit(tx); 2857 } 2858 2859 static void 2860 spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 2861 { 2862 char *packed = NULL; 2863 size_t nvsize = 0; 2864 dmu_buf_t *db; 2865 2866 VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 2867 2868 packed = kmem_alloc(nvsize, KM_SLEEP); 2869 2870 VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 2871 KM_SLEEP) == 0); 2872 2873 dmu_write(spa->spa_meta_objset, obj, 0, nvsize, packed, tx); 2874 2875 kmem_free(packed, nvsize); 2876 2877 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 2878 dmu_buf_will_dirty(db, tx); 2879 *(uint64_t *)db->db_data = nvsize; 2880 dmu_buf_rele(db, FTAG); 2881 } 2882 2883 static void 2884 spa_sync_spares(spa_t *spa, dmu_tx_t *tx) 2885 { 2886 nvlist_t *nvroot; 2887 nvlist_t **spares; 2888 int i; 2889 2890 if (!spa->spa_sync_spares) 2891 return; 2892 2893 /* 2894 * Update the MOS nvlist describing the list of available spares. 2895 * spa_validate_spares() will have already made sure this nvlist is 2896 * valid and the vdevs are labeled appropriately. 2897 */ 2898 if (spa->spa_spares_object == 0) { 2899 spa->spa_spares_object = dmu_object_alloc(spa->spa_meta_objset, 2900 DMU_OT_PACKED_NVLIST, 1 << 14, 2901 DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 2902 VERIFY(zap_update(spa->spa_meta_objset, 2903 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SPARES, 2904 sizeof (uint64_t), 1, &spa->spa_spares_object, tx) == 0); 2905 } 2906 2907 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2908 if (spa->spa_nspares == 0) { 2909 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 2910 NULL, 0) == 0); 2911 } else { 2912 spares = kmem_alloc(spa->spa_nspares * sizeof (void *), 2913 KM_SLEEP); 2914 for (i = 0; i < spa->spa_nspares; i++) 2915 spares[i] = vdev_config_generate(spa, 2916 spa->spa_spares[i], B_FALSE, B_TRUE); 2917 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 2918 spares, spa->spa_nspares) == 0); 2919 for (i = 0; i < spa->spa_nspares; i++) 2920 nvlist_free(spares[i]); 2921 kmem_free(spares, spa->spa_nspares * sizeof (void *)); 2922 } 2923 2924 spa_sync_nvlist(spa, spa->spa_spares_object, nvroot, tx); 2925 nvlist_free(nvroot); 2926 2927 spa->spa_sync_spares = B_FALSE; 2928 } 2929 2930 static void 2931 spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 2932 { 2933 nvlist_t *config; 2934 2935 if (list_is_empty(&spa->spa_dirty_list)) 2936 return; 2937 2938 config = spa_config_generate(spa, NULL, dmu_tx_get_txg(tx), B_FALSE); 2939 2940 if (spa->spa_config_syncing) 2941 nvlist_free(spa->spa_config_syncing); 2942 spa->spa_config_syncing = config; 2943 2944 spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 2945 } 2946 2947 static void 2948 spa_sync_props(void *arg1, void *arg2, dmu_tx_t *tx) 2949 { 2950 spa_t *spa = arg1; 2951 nvlist_t *nvp = arg2; 2952 nvpair_t *nvpair; 2953 objset_t *mos = spa->spa_meta_objset; 2954 uint64_t zapobj; 2955 uint64_t intval; 2956 2957 mutex_enter(&spa->spa_props_lock); 2958 if (spa->spa_pool_props_object == 0) { 2959 zapobj = zap_create(mos, DMU_OT_POOL_PROPS, DMU_OT_NONE, 0, tx); 2960 VERIFY(zapobj > 0); 2961 2962 spa->spa_pool_props_object = zapobj; 2963 2964 VERIFY(zap_update(mos, DMU_POOL_DIRECTORY_OBJECT, 2965 DMU_POOL_PROPS, 8, 1, 2966 &spa->spa_pool_props_object, tx) == 0); 2967 } 2968 mutex_exit(&spa->spa_props_lock); 2969 2970 nvpair = NULL; 2971 while ((nvpair = nvlist_next_nvpair(nvp, nvpair))) { 2972 switch (zpool_name_to_prop(nvpair_name(nvpair))) { 2973 case ZPOOL_PROP_BOOTFS: 2974 VERIFY(nvlist_lookup_uint64(nvp, 2975 nvpair_name(nvpair), &spa->spa_bootfs) == 0); 2976 VERIFY(zap_update(mos, 2977 spa->spa_pool_props_object, 2978 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), 8, 1, 2979 &spa->spa_bootfs, tx) == 0); 2980 break; 2981 2982 case ZPOOL_PROP_AUTOREPLACE: 2983 VERIFY(nvlist_lookup_uint64(nvp, 2984 nvpair_name(nvpair), &intval) == 0); 2985 VERIFY(zap_update(mos, 2986 spa->spa_pool_props_object, 2987 zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE), 8, 1, 2988 &intval, tx) == 0); 2989 break; 2990 } 2991 } 2992 } 2993 2994 /* 2995 * Sync the specified transaction group. New blocks may be dirtied as 2996 * part of the process, so we iterate until it converges. 2997 */ 2998 void 2999 spa_sync(spa_t *spa, uint64_t txg) 3000 { 3001 dsl_pool_t *dp = spa->spa_dsl_pool; 3002 objset_t *mos = spa->spa_meta_objset; 3003 bplist_t *bpl = &spa->spa_sync_bplist; 3004 vdev_t *rvd = spa->spa_root_vdev; 3005 vdev_t *vd; 3006 dmu_tx_t *tx; 3007 int dirty_vdevs; 3008 3009 /* 3010 * Lock out configuration changes. 3011 */ 3012 spa_config_enter(spa, RW_READER, FTAG); 3013 3014 spa->spa_syncing_txg = txg; 3015 spa->spa_sync_pass = 0; 3016 3017 VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj)); 3018 3019 tx = dmu_tx_create_assigned(dp, txg); 3020 3021 /* 3022 * If we are upgrading to ZFS_VERSION_RAIDZ_DEFLATE this txg, 3023 * set spa_deflate if we have no raid-z vdevs. 3024 */ 3025 if (spa->spa_ubsync.ub_version < ZFS_VERSION_RAIDZ_DEFLATE && 3026 spa->spa_uberblock.ub_version >= ZFS_VERSION_RAIDZ_DEFLATE) { 3027 int i; 3028 3029 for (i = 0; i < rvd->vdev_children; i++) { 3030 vd = rvd->vdev_child[i]; 3031 if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 3032 break; 3033 } 3034 if (i == rvd->vdev_children) { 3035 spa->spa_deflate = TRUE; 3036 VERIFY(0 == zap_add(spa->spa_meta_objset, 3037 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 3038 sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 3039 } 3040 } 3041 3042 /* 3043 * If anything has changed in this txg, push the deferred frees 3044 * from the previous txg. If not, leave them alone so that we 3045 * don't generate work on an otherwise idle system. 3046 */ 3047 if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 3048 !txg_list_empty(&dp->dp_dirty_dirs, txg) || 3049 !txg_list_empty(&dp->dp_sync_tasks, txg)) 3050 spa_sync_deferred_frees(spa, txg); 3051 3052 /* 3053 * Iterate to convergence. 3054 */ 3055 do { 3056 spa->spa_sync_pass++; 3057 3058 spa_sync_config_object(spa, tx); 3059 spa_sync_spares(spa, tx); 3060 spa_errlog_sync(spa, txg); 3061 dsl_pool_sync(dp, txg); 3062 3063 dirty_vdevs = 0; 3064 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) { 3065 vdev_sync(vd, txg); 3066 dirty_vdevs++; 3067 } 3068 3069 bplist_sync(bpl, tx); 3070 } while (dirty_vdevs); 3071 3072 bplist_close(bpl); 3073 3074 dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass); 3075 3076 /* 3077 * Rewrite the vdev configuration (which includes the uberblock) 3078 * to commit the transaction group. 3079 * 3080 * If there are any dirty vdevs, sync the uberblock to all vdevs. 3081 * Otherwise, pick a random top-level vdev that's known to be 3082 * visible in the config cache (see spa_vdev_add() for details). 3083 * If the write fails, try the next vdev until we're tried them all. 3084 */ 3085 if (!list_is_empty(&spa->spa_dirty_list)) { 3086 VERIFY(vdev_config_sync(rvd, txg) == 0); 3087 } else { 3088 int children = rvd->vdev_children; 3089 int c0 = spa_get_random(children); 3090 int c; 3091 3092 for (c = 0; c < children; c++) { 3093 vd = rvd->vdev_child[(c0 + c) % children]; 3094 if (vd->vdev_ms_array == 0) 3095 continue; 3096 if (vdev_config_sync(vd, txg) == 0) 3097 break; 3098 } 3099 if (c == children) 3100 VERIFY(vdev_config_sync(rvd, txg) == 0); 3101 } 3102 3103 dmu_tx_commit(tx); 3104 3105 /* 3106 * Clear the dirty config list. 3107 */ 3108 while ((vd = list_head(&spa->spa_dirty_list)) != NULL) 3109 vdev_config_clean(vd); 3110 3111 /* 3112 * Now that the new config has synced transactionally, 3113 * let it become visible to the config cache. 3114 */ 3115 if (spa->spa_config_syncing != NULL) { 3116 spa_config_set(spa, spa->spa_config_syncing); 3117 spa->spa_config_txg = txg; 3118 spa->spa_config_syncing = NULL; 3119 } 3120 3121 /* 3122 * Make a stable copy of the fully synced uberblock. 3123 * We use this as the root for pool traversals. 3124 */ 3125 spa->spa_traverse_wanted = 1; /* tells traverse_more() to stop */ 3126 3127 spa_scrub_suspend(spa); /* stop scrubbing and finish I/Os */ 3128 3129 rw_enter(&spa->spa_traverse_lock, RW_WRITER); 3130 spa->spa_traverse_wanted = 0; 3131 spa->spa_ubsync = spa->spa_uberblock; 3132 rw_exit(&spa->spa_traverse_lock); 3133 3134 spa_scrub_resume(spa); /* resume scrub with new ubsync */ 3135 3136 /* 3137 * Clean up the ZIL records for the synced txg. 3138 */ 3139 dsl_pool_zil_clean(dp); 3140 3141 /* 3142 * Update usable space statistics. 3143 */ 3144 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 3145 vdev_sync_done(vd, txg); 3146 3147 /* 3148 * It had better be the case that we didn't dirty anything 3149 * since vdev_config_sync(). 3150 */ 3151 ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 3152 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 3153 ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 3154 ASSERT(bpl->bpl_queue == NULL); 3155 3156 spa_config_exit(spa, FTAG); 3157 3158 /* 3159 * If any async tasks have been requested, kick them off. 3160 */ 3161 spa_async_dispatch(spa); 3162 } 3163 3164 /* 3165 * Sync all pools. We don't want to hold the namespace lock across these 3166 * operations, so we take a reference on the spa_t and drop the lock during the 3167 * sync. 3168 */ 3169 void 3170 spa_sync_allpools(void) 3171 { 3172 spa_t *spa = NULL; 3173 mutex_enter(&spa_namespace_lock); 3174 while ((spa = spa_next(spa)) != NULL) { 3175 if (spa_state(spa) != POOL_STATE_ACTIVE) 3176 continue; 3177 spa_open_ref(spa, FTAG); 3178 mutex_exit(&spa_namespace_lock); 3179 txg_wait_synced(spa_get_dsl(spa), 0); 3180 mutex_enter(&spa_namespace_lock); 3181 spa_close(spa, FTAG); 3182 } 3183 mutex_exit(&spa_namespace_lock); 3184 } 3185 3186 /* 3187 * ========================================================================== 3188 * Miscellaneous routines 3189 * ========================================================================== 3190 */ 3191 3192 /* 3193 * Remove all pools in the system. 3194 */ 3195 void 3196 spa_evict_all(void) 3197 { 3198 spa_t *spa; 3199 3200 /* 3201 * Remove all cached state. All pools should be closed now, 3202 * so every spa in the AVL tree should be unreferenced. 3203 */ 3204 mutex_enter(&spa_namespace_lock); 3205 while ((spa = spa_next(NULL)) != NULL) { 3206 /* 3207 * Stop async tasks. The async thread may need to detach 3208 * a device that's been replaced, which requires grabbing 3209 * spa_namespace_lock, so we must drop it here. 3210 */ 3211 spa_open_ref(spa, FTAG); 3212 mutex_exit(&spa_namespace_lock); 3213 spa_async_suspend(spa); 3214 VERIFY(spa_scrub(spa, POOL_SCRUB_NONE, B_TRUE) == 0); 3215 mutex_enter(&spa_namespace_lock); 3216 spa_close(spa, FTAG); 3217 3218 if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 3219 spa_unload(spa); 3220 spa_deactivate(spa); 3221 } 3222 spa_remove(spa); 3223 } 3224 mutex_exit(&spa_namespace_lock); 3225 } 3226 3227 vdev_t * 3228 spa_lookup_by_guid(spa_t *spa, uint64_t guid) 3229 { 3230 return (vdev_lookup_by_guid(spa->spa_root_vdev, guid)); 3231 } 3232 3233 void 3234 spa_upgrade(spa_t *spa) 3235 { 3236 spa_config_enter(spa, RW_WRITER, FTAG); 3237 3238 /* 3239 * This should only be called for a non-faulted pool, and since a 3240 * future version would result in an unopenable pool, this shouldn't be 3241 * possible. 3242 */ 3243 ASSERT(spa->spa_uberblock.ub_version <= ZFS_VERSION); 3244 3245 spa->spa_uberblock.ub_version = ZFS_VERSION; 3246 vdev_config_dirty(spa->spa_root_vdev); 3247 3248 spa_config_exit(spa, FTAG); 3249 3250 txg_wait_synced(spa_get_dsl(spa), 0); 3251 } 3252 3253 boolean_t 3254 spa_has_spare(spa_t *spa, uint64_t guid) 3255 { 3256 int i; 3257 uint64_t spareguid; 3258 3259 for (i = 0; i < spa->spa_nspares; i++) 3260 if (spa->spa_spares[i]->vdev_guid == guid) 3261 return (B_TRUE); 3262 3263 for (i = 0; i < spa->spa_pending_nspares; i++) { 3264 if (nvlist_lookup_uint64(spa->spa_pending_spares[i], 3265 ZPOOL_CONFIG_GUID, &spareguid) == 0 && 3266 spareguid == guid) 3267 return (B_TRUE); 3268 } 3269 3270 return (B_FALSE); 3271 } 3272 3273 int 3274 spa_set_props(spa_t *spa, nvlist_t *nvp) 3275 { 3276 return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props, 3277 spa, nvp, 3)); 3278 } 3279 3280 int 3281 spa_get_props(spa_t *spa, nvlist_t **nvp) 3282 { 3283 zap_cursor_t zc; 3284 zap_attribute_t za; 3285 objset_t *mos = spa->spa_meta_objset; 3286 zfs_source_t src; 3287 zpool_prop_t prop; 3288 nvlist_t *propval; 3289 uint64_t value; 3290 int err; 3291 3292 VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 3293 3294 mutex_enter(&spa->spa_props_lock); 3295 /* If no props object, then just return empty nvlist */ 3296 if (spa->spa_pool_props_object == 0) { 3297 mutex_exit(&spa->spa_props_lock); 3298 return (0); 3299 } 3300 3301 for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); 3302 (err = zap_cursor_retrieve(&zc, &za)) == 0; 3303 zap_cursor_advance(&zc)) { 3304 3305 if ((prop = zpool_name_to_prop(za.za_name)) == ZFS_PROP_INVAL) 3306 continue; 3307 3308 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 3309 switch (za.za_integer_length) { 3310 case 8: 3311 if (zpool_prop_default_numeric(prop) == 3312 za.za_first_integer) 3313 src = ZFS_SRC_DEFAULT; 3314 else 3315 src = ZFS_SRC_LOCAL; 3316 value = za.za_first_integer; 3317 3318 if (prop == ZPOOL_PROP_BOOTFS) { 3319 dsl_pool_t *dp; 3320 dsl_dataset_t *ds = NULL; 3321 char strval[MAXPATHLEN]; 3322 3323 dp = spa_get_dsl(spa); 3324 rw_enter(&dp->dp_config_rwlock, RW_READER); 3325 if ((err = dsl_dataset_open_obj(dp, 3326 za.za_first_integer, NULL, DS_MODE_NONE, 3327 FTAG, &ds)) != 0) { 3328 rw_exit(&dp->dp_config_rwlock); 3329 break; 3330 } 3331 dsl_dataset_name(ds, strval); 3332 dsl_dataset_close(ds, DS_MODE_NONE, FTAG); 3333 rw_exit(&dp->dp_config_rwlock); 3334 3335 VERIFY(nvlist_add_uint64(propval, 3336 ZFS_PROP_SOURCE, src) == 0); 3337 VERIFY(nvlist_add_string(propval, 3338 ZFS_PROP_VALUE, strval) == 0); 3339 } else { 3340 VERIFY(nvlist_add_uint64(propval, 3341 ZFS_PROP_SOURCE, src) == 0); 3342 VERIFY(nvlist_add_uint64(propval, 3343 ZFS_PROP_VALUE, value) == 0); 3344 } 3345 VERIFY(nvlist_add_nvlist(*nvp, za.za_name, 3346 propval) == 0); 3347 break; 3348 } 3349 nvlist_free(propval); 3350 } 3351 zap_cursor_fini(&zc); 3352 mutex_exit(&spa->spa_props_lock); 3353 if (err && err != ENOENT) { 3354 nvlist_free(*nvp); 3355 return (err); 3356 } 3357 3358 return (0); 3359 } 3360 3361 /* 3362 * If the bootfs property value is dsobj, clear it. 3363 */ 3364 void 3365 spa_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 3366 { 3367 if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 3368 VERIFY(zap_remove(spa->spa_meta_objset, 3369 spa->spa_pool_props_object, 3370 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 3371 spa->spa_bootfs = 0; 3372 } 3373 } 3374 3375 /* 3376 * Post a sysevent corresponding to the given event. The 'name' must be one of 3377 * the event definitions in sys/sysevent/eventdefs.h. The payload will be 3378 * filled in from the spa and (optionally) the vdev. This doesn't do anything 3379 * in the userland libzpool, as we don't want consumers to misinterpret ztest 3380 * or zdb as real changes. 3381 */ 3382 void 3383 spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) 3384 { 3385 #ifdef _KERNEL 3386 sysevent_t *ev; 3387 sysevent_attr_list_t *attr = NULL; 3388 sysevent_value_t value; 3389 sysevent_id_t eid; 3390 3391 ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 3392 SE_SLEEP); 3393 3394 value.value_type = SE_DATA_TYPE_STRING; 3395 value.value.sv_string = spa_name(spa); 3396 if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 3397 goto done; 3398 3399 value.value_type = SE_DATA_TYPE_UINT64; 3400 value.value.sv_uint64 = spa_guid(spa); 3401 if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 3402 goto done; 3403 3404 if (vd) { 3405 value.value_type = SE_DATA_TYPE_UINT64; 3406 value.value.sv_uint64 = vd->vdev_guid; 3407 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 3408 SE_SLEEP) != 0) 3409 goto done; 3410 3411 if (vd->vdev_path) { 3412 value.value_type = SE_DATA_TYPE_STRING; 3413 value.value.sv_string = vd->vdev_path; 3414 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 3415 &value, SE_SLEEP) != 0) 3416 goto done; 3417 } 3418 } 3419 3420 (void) log_sysevent(ev, SE_SLEEP, &eid); 3421 3422 done: 3423 if (attr) 3424 sysevent_free_attr(attr); 3425 sysevent_free(ev); 3426 #endif 3427 } 3428