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 2009 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* 28 * This file contains all the routines used when modifying on-disk SPA state. 29 * This includes opening, importing, destroying, exporting a pool, and syncing a 30 * pool. 31 */ 32 33 #include <sys/zfs_context.h> 34 #include <sys/fm/fs/zfs.h> 35 #include <sys/spa_impl.h> 36 #include <sys/zio.h> 37 #include <sys/zio_checksum.h> 38 #include <sys/dmu.h> 39 #include <sys/dmu_tx.h> 40 #include <sys/zap.h> 41 #include <sys/zil.h> 42 #include <sys/ddt.h> 43 #include <sys/vdev_impl.h> 44 #include <sys/metaslab.h> 45 #include <sys/metaslab_impl.h> 46 #include <sys/uberblock_impl.h> 47 #include <sys/txg.h> 48 #include <sys/avl.h> 49 #include <sys/dmu_traverse.h> 50 #include <sys/dmu_objset.h> 51 #include <sys/unique.h> 52 #include <sys/dsl_pool.h> 53 #include <sys/dsl_dataset.h> 54 #include <sys/dsl_dir.h> 55 #include <sys/dsl_prop.h> 56 #include <sys/dsl_synctask.h> 57 #include <sys/fs/zfs.h> 58 #include <sys/arc.h> 59 #include <sys/callb.h> 60 #include <sys/systeminfo.h> 61 #include <sys/sunddi.h> 62 #include <sys/spa_boot.h> 63 #include <sys/zfs_ioctl.h> 64 65 #ifdef _KERNEL 66 #include <sys/zone.h> 67 #include <sys/bootprops.h> 68 #endif /* _KERNEL */ 69 70 #include "zfs_prop.h" 71 #include "zfs_comutil.h" 72 73 enum zti_modes { 74 zti_mode_fixed, /* value is # of threads (min 1) */ 75 zti_mode_online_percent, /* value is % of online CPUs */ 76 zti_mode_tune, /* fill from zio_taskq_tune_* */ 77 zti_nmodes 78 }; 79 80 #define ZTI_THREAD_FIX(n) { zti_mode_fixed, (n) } 81 #define ZTI_THREAD_PCT(n) { zti_mode_online_percent, (n) } 82 #define ZTI_THREAD_TUNE { zti_mode_tune, 0 } 83 84 #define ZTI_THREAD_ONE ZTI_THREAD_FIX(1) 85 86 typedef struct zio_taskq_info { 87 const char *zti_name; 88 struct { 89 enum zti_modes zti_mode; 90 uint_t zti_value; 91 } zti_nthreads[ZIO_TASKQ_TYPES]; 92 } zio_taskq_info_t; 93 94 static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = { 95 "issue", "intr" 96 }; 97 98 const zio_taskq_info_t zio_taskqs[ZIO_TYPES] = { 99 /* ISSUE INTR */ 100 { "spa_zio_null", { ZTI_THREAD_ONE, ZTI_THREAD_ONE } }, 101 { "spa_zio_read", { ZTI_THREAD_FIX(8), ZTI_THREAD_TUNE } }, 102 { "spa_zio_write", { ZTI_THREAD_TUNE, ZTI_THREAD_FIX(8) } }, 103 { "spa_zio_free", { ZTI_THREAD_ONE, ZTI_THREAD_ONE } }, 104 { "spa_zio_claim", { ZTI_THREAD_ONE, ZTI_THREAD_ONE } }, 105 { "spa_zio_ioctl", { ZTI_THREAD_ONE, ZTI_THREAD_ONE } }, 106 }; 107 108 enum zti_modes zio_taskq_tune_mode = zti_mode_online_percent; 109 uint_t zio_taskq_tune_value = 80; /* #threads = 80% of # online CPUs */ 110 111 static void spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx); 112 static boolean_t spa_has_active_shared_spare(spa_t *spa); 113 114 /* 115 * ========================================================================== 116 * SPA properties routines 117 * ========================================================================== 118 */ 119 120 /* 121 * Add a (source=src, propname=propval) list to an nvlist. 122 */ 123 static void 124 spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval, 125 uint64_t intval, zprop_source_t src) 126 { 127 const char *propname = zpool_prop_to_name(prop); 128 nvlist_t *propval; 129 130 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 131 VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0); 132 133 if (strval != NULL) 134 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0); 135 else 136 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0); 137 138 VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0); 139 nvlist_free(propval); 140 } 141 142 /* 143 * Get property values from the spa configuration. 144 */ 145 static void 146 spa_prop_get_config(spa_t *spa, nvlist_t **nvp) 147 { 148 uint64_t size; 149 uint64_t used; 150 uint64_t cap, version; 151 zprop_source_t src = ZPROP_SRC_NONE; 152 spa_config_dirent_t *dp; 153 154 ASSERT(MUTEX_HELD(&spa->spa_props_lock)); 155 156 if (spa->spa_root_vdev != NULL) { 157 used = metaslab_class_get_alloc(spa_normal_class(spa)); 158 size = metaslab_class_get_space(spa_normal_class(spa)); 159 spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src); 160 spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src); 161 spa_prop_add_list(*nvp, ZPOOL_PROP_USED, NULL, used, src); 162 spa_prop_add_list(*nvp, ZPOOL_PROP_AVAILABLE, NULL, 163 size - used, src); 164 165 cap = (size == 0) ? 0 : (used * 100 / size); 166 spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src); 167 168 spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL, 169 ddt_get_pool_dedup_ratio(spa), src); 170 171 spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL, 172 spa->spa_root_vdev->vdev_state, src); 173 174 version = spa_version(spa); 175 if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) 176 src = ZPROP_SRC_DEFAULT; 177 else 178 src = ZPROP_SRC_LOCAL; 179 spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src); 180 } 181 182 spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src); 183 184 if (spa->spa_root != NULL) 185 spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root, 186 0, ZPROP_SRC_LOCAL); 187 188 if ((dp = list_head(&spa->spa_config_list)) != NULL) { 189 if (dp->scd_path == NULL) { 190 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 191 "none", 0, ZPROP_SRC_LOCAL); 192 } else if (strcmp(dp->scd_path, spa_config_path) != 0) { 193 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 194 dp->scd_path, 0, ZPROP_SRC_LOCAL); 195 } 196 } 197 } 198 199 /* 200 * Get zpool property values. 201 */ 202 int 203 spa_prop_get(spa_t *spa, nvlist_t **nvp) 204 { 205 objset_t *mos = spa->spa_meta_objset; 206 zap_cursor_t zc; 207 zap_attribute_t za; 208 int err; 209 210 VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 211 212 mutex_enter(&spa->spa_props_lock); 213 214 /* 215 * Get properties from the spa config. 216 */ 217 spa_prop_get_config(spa, nvp); 218 219 /* If no pool property object, no more prop to get. */ 220 if (spa->spa_pool_props_object == 0) { 221 mutex_exit(&spa->spa_props_lock); 222 return (0); 223 } 224 225 /* 226 * Get properties from the MOS pool property object. 227 */ 228 for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); 229 (err = zap_cursor_retrieve(&zc, &za)) == 0; 230 zap_cursor_advance(&zc)) { 231 uint64_t intval = 0; 232 char *strval = NULL; 233 zprop_source_t src = ZPROP_SRC_DEFAULT; 234 zpool_prop_t prop; 235 236 if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL) 237 continue; 238 239 switch (za.za_integer_length) { 240 case 8: 241 /* integer property */ 242 if (za.za_first_integer != 243 zpool_prop_default_numeric(prop)) 244 src = ZPROP_SRC_LOCAL; 245 246 if (prop == ZPOOL_PROP_BOOTFS) { 247 dsl_pool_t *dp; 248 dsl_dataset_t *ds = NULL; 249 250 dp = spa_get_dsl(spa); 251 rw_enter(&dp->dp_config_rwlock, RW_READER); 252 if (err = dsl_dataset_hold_obj(dp, 253 za.za_first_integer, FTAG, &ds)) { 254 rw_exit(&dp->dp_config_rwlock); 255 break; 256 } 257 258 strval = kmem_alloc( 259 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1, 260 KM_SLEEP); 261 dsl_dataset_name(ds, strval); 262 dsl_dataset_rele(ds, FTAG); 263 rw_exit(&dp->dp_config_rwlock); 264 } else { 265 strval = NULL; 266 intval = za.za_first_integer; 267 } 268 269 spa_prop_add_list(*nvp, prop, strval, intval, src); 270 271 if (strval != NULL) 272 kmem_free(strval, 273 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1); 274 275 break; 276 277 case 1: 278 /* string property */ 279 strval = kmem_alloc(za.za_num_integers, KM_SLEEP); 280 err = zap_lookup(mos, spa->spa_pool_props_object, 281 za.za_name, 1, za.za_num_integers, strval); 282 if (err) { 283 kmem_free(strval, za.za_num_integers); 284 break; 285 } 286 spa_prop_add_list(*nvp, prop, strval, 0, src); 287 kmem_free(strval, za.za_num_integers); 288 break; 289 290 default: 291 break; 292 } 293 } 294 zap_cursor_fini(&zc); 295 mutex_exit(&spa->spa_props_lock); 296 out: 297 if (err && err != ENOENT) { 298 nvlist_free(*nvp); 299 *nvp = NULL; 300 return (err); 301 } 302 303 return (0); 304 } 305 306 /* 307 * Validate the given pool properties nvlist and modify the list 308 * for the property values to be set. 309 */ 310 static int 311 spa_prop_validate(spa_t *spa, nvlist_t *props) 312 { 313 nvpair_t *elem; 314 int error = 0, reset_bootfs = 0; 315 uint64_t objnum; 316 317 elem = NULL; 318 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 319 zpool_prop_t prop; 320 char *propname, *strval; 321 uint64_t intval; 322 objset_t *os; 323 char *slash; 324 325 propname = nvpair_name(elem); 326 327 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) 328 return (EINVAL); 329 330 switch (prop) { 331 case ZPOOL_PROP_VERSION: 332 error = nvpair_value_uint64(elem, &intval); 333 if (!error && 334 (intval < spa_version(spa) || intval > SPA_VERSION)) 335 error = EINVAL; 336 break; 337 338 case ZPOOL_PROP_DELEGATION: 339 case ZPOOL_PROP_AUTOREPLACE: 340 case ZPOOL_PROP_LISTSNAPS: 341 case ZPOOL_PROP_AUTOEXPAND: 342 error = nvpair_value_uint64(elem, &intval); 343 if (!error && intval > 1) 344 error = EINVAL; 345 break; 346 347 case ZPOOL_PROP_BOOTFS: 348 /* 349 * If the pool version is less than SPA_VERSION_BOOTFS, 350 * or the pool is still being created (version == 0), 351 * the bootfs property cannot be set. 352 */ 353 if (spa_version(spa) < SPA_VERSION_BOOTFS) { 354 error = ENOTSUP; 355 break; 356 } 357 358 /* 359 * Make sure the vdev config is bootable 360 */ 361 if (!vdev_is_bootable(spa->spa_root_vdev)) { 362 error = ENOTSUP; 363 break; 364 } 365 366 reset_bootfs = 1; 367 368 error = nvpair_value_string(elem, &strval); 369 370 if (!error) { 371 uint64_t compress; 372 373 if (strval == NULL || strval[0] == '\0') { 374 objnum = zpool_prop_default_numeric( 375 ZPOOL_PROP_BOOTFS); 376 break; 377 } 378 379 if (error = dmu_objset_hold(strval, FTAG, &os)) 380 break; 381 382 /* Must be ZPL and not gzip compressed. */ 383 384 if (dmu_objset_type(os) != DMU_OST_ZFS) { 385 error = ENOTSUP; 386 } else if ((error = dsl_prop_get_integer(strval, 387 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 388 &compress, NULL)) == 0 && 389 !BOOTFS_COMPRESS_VALID(compress)) { 390 error = ENOTSUP; 391 } else { 392 objnum = dmu_objset_id(os); 393 } 394 dmu_objset_rele(os, FTAG); 395 } 396 break; 397 398 case ZPOOL_PROP_FAILUREMODE: 399 error = nvpair_value_uint64(elem, &intval); 400 if (!error && (intval < ZIO_FAILURE_MODE_WAIT || 401 intval > ZIO_FAILURE_MODE_PANIC)) 402 error = EINVAL; 403 404 /* 405 * This is a special case which only occurs when 406 * the pool has completely failed. This allows 407 * the user to change the in-core failmode property 408 * without syncing it out to disk (I/Os might 409 * currently be blocked). We do this by returning 410 * EIO to the caller (spa_prop_set) to trick it 411 * into thinking we encountered a property validation 412 * error. 413 */ 414 if (!error && spa_suspended(spa)) { 415 spa->spa_failmode = intval; 416 error = EIO; 417 } 418 break; 419 420 case ZPOOL_PROP_CACHEFILE: 421 if ((error = nvpair_value_string(elem, &strval)) != 0) 422 break; 423 424 if (strval[0] == '\0') 425 break; 426 427 if (strcmp(strval, "none") == 0) 428 break; 429 430 if (strval[0] != '/') { 431 error = EINVAL; 432 break; 433 } 434 435 slash = strrchr(strval, '/'); 436 ASSERT(slash != NULL); 437 438 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 439 strcmp(slash, "/..") == 0) 440 error = EINVAL; 441 break; 442 443 case ZPOOL_PROP_DEDUPDITTO: 444 if (spa_version(spa) < SPA_VERSION_DEDUP) 445 error = ENOTSUP; 446 else 447 error = nvpair_value_uint64(elem, &intval); 448 if (error == 0 && 449 intval != 0 && intval < ZIO_DEDUPDITTO_MIN) 450 error = EINVAL; 451 break; 452 } 453 454 if (error) 455 break; 456 } 457 458 if (!error && reset_bootfs) { 459 error = nvlist_remove(props, 460 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); 461 462 if (!error) { 463 error = nvlist_add_uint64(props, 464 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); 465 } 466 } 467 468 return (error); 469 } 470 471 void 472 spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync) 473 { 474 char *cachefile; 475 spa_config_dirent_t *dp; 476 477 if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), 478 &cachefile) != 0) 479 return; 480 481 dp = kmem_alloc(sizeof (spa_config_dirent_t), 482 KM_SLEEP); 483 484 if (cachefile[0] == '\0') 485 dp->scd_path = spa_strdup(spa_config_path); 486 else if (strcmp(cachefile, "none") == 0) 487 dp->scd_path = NULL; 488 else 489 dp->scd_path = spa_strdup(cachefile); 490 491 list_insert_head(&spa->spa_config_list, dp); 492 if (need_sync) 493 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 494 } 495 496 int 497 spa_prop_set(spa_t *spa, nvlist_t *nvp) 498 { 499 int error; 500 nvpair_t *elem; 501 boolean_t need_sync = B_FALSE; 502 zpool_prop_t prop; 503 504 if ((error = spa_prop_validate(spa, nvp)) != 0) 505 return (error); 506 507 elem = NULL; 508 while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) { 509 if ((prop = zpool_name_to_prop( 510 nvpair_name(elem))) == ZPROP_INVAL) 511 return (EINVAL); 512 513 if (prop == ZPOOL_PROP_CACHEFILE || prop == ZPOOL_PROP_ALTROOT) 514 continue; 515 516 need_sync = B_TRUE; 517 break; 518 } 519 520 if (need_sync) 521 return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props, 522 spa, nvp, 3)); 523 else 524 return (0); 525 } 526 527 /* 528 * If the bootfs property value is dsobj, clear it. 529 */ 530 void 531 spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 532 { 533 if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 534 VERIFY(zap_remove(spa->spa_meta_objset, 535 spa->spa_pool_props_object, 536 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 537 spa->spa_bootfs = 0; 538 } 539 } 540 541 /* 542 * ========================================================================== 543 * SPA state manipulation (open/create/destroy/import/export) 544 * ========================================================================== 545 */ 546 547 static int 548 spa_error_entry_compare(const void *a, const void *b) 549 { 550 spa_error_entry_t *sa = (spa_error_entry_t *)a; 551 spa_error_entry_t *sb = (spa_error_entry_t *)b; 552 int ret; 553 554 ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, 555 sizeof (zbookmark_t)); 556 557 if (ret < 0) 558 return (-1); 559 else if (ret > 0) 560 return (1); 561 else 562 return (0); 563 } 564 565 /* 566 * Utility function which retrieves copies of the current logs and 567 * re-initializes them in the process. 568 */ 569 void 570 spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 571 { 572 ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 573 574 bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 575 bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 576 577 avl_create(&spa->spa_errlist_scrub, 578 spa_error_entry_compare, sizeof (spa_error_entry_t), 579 offsetof(spa_error_entry_t, se_avl)); 580 avl_create(&spa->spa_errlist_last, 581 spa_error_entry_compare, sizeof (spa_error_entry_t), 582 offsetof(spa_error_entry_t, se_avl)); 583 } 584 585 /* 586 * Activate an uninitialized pool. 587 */ 588 static void 589 spa_activate(spa_t *spa, int mode) 590 { 591 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 592 593 spa->spa_state = POOL_STATE_ACTIVE; 594 spa->spa_mode = mode; 595 596 spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops); 597 spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops); 598 599 for (int t = 0; t < ZIO_TYPES; t++) { 600 const zio_taskq_info_t *ztip = &zio_taskqs[t]; 601 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 602 enum zti_modes mode = ztip->zti_nthreads[q].zti_mode; 603 uint_t value = ztip->zti_nthreads[q].zti_value; 604 char name[32]; 605 606 (void) snprintf(name, sizeof (name), 607 "%s_%s", ztip->zti_name, zio_taskq_types[q]); 608 609 if (mode == zti_mode_tune) { 610 mode = zio_taskq_tune_mode; 611 value = zio_taskq_tune_value; 612 if (mode == zti_mode_tune) 613 mode = zti_mode_online_percent; 614 } 615 616 switch (mode) { 617 case zti_mode_fixed: 618 ASSERT3U(value, >=, 1); 619 value = MAX(value, 1); 620 621 spa->spa_zio_taskq[t][q] = taskq_create(name, 622 value, maxclsyspri, 50, INT_MAX, 623 TASKQ_PREPOPULATE); 624 break; 625 626 case zti_mode_online_percent: 627 spa->spa_zio_taskq[t][q] = taskq_create(name, 628 value, maxclsyspri, 50, INT_MAX, 629 TASKQ_PREPOPULATE | TASKQ_THREADS_CPU_PCT); 630 break; 631 632 case zti_mode_tune: 633 default: 634 panic("unrecognized mode for " 635 "zio_taskqs[%u]->zti_nthreads[%u] (%u:%u) " 636 "in spa_activate()", 637 t, q, mode, value); 638 break; 639 } 640 } 641 } 642 643 list_create(&spa->spa_config_dirty_list, sizeof (vdev_t), 644 offsetof(vdev_t, vdev_config_dirty_node)); 645 list_create(&spa->spa_state_dirty_list, sizeof (vdev_t), 646 offsetof(vdev_t, vdev_state_dirty_node)); 647 648 txg_list_create(&spa->spa_vdev_txg_list, 649 offsetof(struct vdev, vdev_txg_node)); 650 651 avl_create(&spa->spa_errlist_scrub, 652 spa_error_entry_compare, sizeof (spa_error_entry_t), 653 offsetof(spa_error_entry_t, se_avl)); 654 avl_create(&spa->spa_errlist_last, 655 spa_error_entry_compare, sizeof (spa_error_entry_t), 656 offsetof(spa_error_entry_t, se_avl)); 657 } 658 659 /* 660 * Opposite of spa_activate(). 661 */ 662 static void 663 spa_deactivate(spa_t *spa) 664 { 665 ASSERT(spa->spa_sync_on == B_FALSE); 666 ASSERT(spa->spa_dsl_pool == NULL); 667 ASSERT(spa->spa_root_vdev == NULL); 668 ASSERT(spa->spa_async_zio_root == NULL); 669 ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 670 671 txg_list_destroy(&spa->spa_vdev_txg_list); 672 673 list_destroy(&spa->spa_config_dirty_list); 674 list_destroy(&spa->spa_state_dirty_list); 675 676 for (int t = 0; t < ZIO_TYPES; t++) { 677 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 678 taskq_destroy(spa->spa_zio_taskq[t][q]); 679 spa->spa_zio_taskq[t][q] = NULL; 680 } 681 } 682 683 metaslab_class_destroy(spa->spa_normal_class); 684 spa->spa_normal_class = NULL; 685 686 metaslab_class_destroy(spa->spa_log_class); 687 spa->spa_log_class = NULL; 688 689 /* 690 * If this was part of an import or the open otherwise failed, we may 691 * still have errors left in the queues. Empty them just in case. 692 */ 693 spa_errlog_drain(spa); 694 695 avl_destroy(&spa->spa_errlist_scrub); 696 avl_destroy(&spa->spa_errlist_last); 697 698 spa->spa_state = POOL_STATE_UNINITIALIZED; 699 } 700 701 /* 702 * Verify a pool configuration, and construct the vdev tree appropriately. This 703 * will create all the necessary vdevs in the appropriate layout, with each vdev 704 * in the CLOSED state. This will prep the pool before open/creation/import. 705 * All vdev validation is done by the vdev_alloc() routine. 706 */ 707 static int 708 spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 709 uint_t id, int atype) 710 { 711 nvlist_t **child; 712 uint_t children; 713 int error; 714 715 if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 716 return (error); 717 718 if ((*vdp)->vdev_ops->vdev_op_leaf) 719 return (0); 720 721 error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 722 &child, &children); 723 724 if (error == ENOENT) 725 return (0); 726 727 if (error) { 728 vdev_free(*vdp); 729 *vdp = NULL; 730 return (EINVAL); 731 } 732 733 for (int c = 0; c < children; c++) { 734 vdev_t *vd; 735 if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 736 atype)) != 0) { 737 vdev_free(*vdp); 738 *vdp = NULL; 739 return (error); 740 } 741 } 742 743 ASSERT(*vdp != NULL); 744 745 return (0); 746 } 747 748 /* 749 * Opposite of spa_load(). 750 */ 751 static void 752 spa_unload(spa_t *spa) 753 { 754 int i; 755 756 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 757 758 /* 759 * Stop async tasks. 760 */ 761 spa_async_suspend(spa); 762 763 /* 764 * Stop syncing. 765 */ 766 if (spa->spa_sync_on) { 767 txg_sync_stop(spa->spa_dsl_pool); 768 spa->spa_sync_on = B_FALSE; 769 } 770 771 /* 772 * Wait for any outstanding async I/O to complete. 773 */ 774 if (spa->spa_async_zio_root != NULL) { 775 (void) zio_wait(spa->spa_async_zio_root); 776 spa->spa_async_zio_root = NULL; 777 } 778 779 /* 780 * Close the dsl pool. 781 */ 782 if (spa->spa_dsl_pool) { 783 dsl_pool_close(spa->spa_dsl_pool); 784 spa->spa_dsl_pool = NULL; 785 } 786 787 ddt_unload(spa); 788 789 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 790 791 /* 792 * Drop and purge level 2 cache 793 */ 794 spa_l2cache_drop(spa); 795 796 /* 797 * Close all vdevs. 798 */ 799 if (spa->spa_root_vdev) 800 vdev_free(spa->spa_root_vdev); 801 ASSERT(spa->spa_root_vdev == NULL); 802 803 for (i = 0; i < spa->spa_spares.sav_count; i++) 804 vdev_free(spa->spa_spares.sav_vdevs[i]); 805 if (spa->spa_spares.sav_vdevs) { 806 kmem_free(spa->spa_spares.sav_vdevs, 807 spa->spa_spares.sav_count * sizeof (void *)); 808 spa->spa_spares.sav_vdevs = NULL; 809 } 810 if (spa->spa_spares.sav_config) { 811 nvlist_free(spa->spa_spares.sav_config); 812 spa->spa_spares.sav_config = NULL; 813 } 814 spa->spa_spares.sav_count = 0; 815 816 for (i = 0; i < spa->spa_l2cache.sav_count; i++) 817 vdev_free(spa->spa_l2cache.sav_vdevs[i]); 818 if (spa->spa_l2cache.sav_vdevs) { 819 kmem_free(spa->spa_l2cache.sav_vdevs, 820 spa->spa_l2cache.sav_count * sizeof (void *)); 821 spa->spa_l2cache.sav_vdevs = NULL; 822 } 823 if (spa->spa_l2cache.sav_config) { 824 nvlist_free(spa->spa_l2cache.sav_config); 825 spa->spa_l2cache.sav_config = NULL; 826 } 827 spa->spa_l2cache.sav_count = 0; 828 829 spa->spa_async_suspended = 0; 830 831 spa_config_exit(spa, SCL_ALL, FTAG); 832 } 833 834 /* 835 * Load (or re-load) the current list of vdevs describing the active spares for 836 * this pool. When this is called, we have some form of basic information in 837 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and 838 * then re-generate a more complete list including status information. 839 */ 840 static void 841 spa_load_spares(spa_t *spa) 842 { 843 nvlist_t **spares; 844 uint_t nspares; 845 int i; 846 vdev_t *vd, *tvd; 847 848 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 849 850 /* 851 * First, close and free any existing spare vdevs. 852 */ 853 for (i = 0; i < spa->spa_spares.sav_count; i++) { 854 vd = spa->spa_spares.sav_vdevs[i]; 855 856 /* Undo the call to spa_activate() below */ 857 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 858 B_FALSE)) != NULL && tvd->vdev_isspare) 859 spa_spare_remove(tvd); 860 vdev_close(vd); 861 vdev_free(vd); 862 } 863 864 if (spa->spa_spares.sav_vdevs) 865 kmem_free(spa->spa_spares.sav_vdevs, 866 spa->spa_spares.sav_count * sizeof (void *)); 867 868 if (spa->spa_spares.sav_config == NULL) 869 nspares = 0; 870 else 871 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 872 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 873 874 spa->spa_spares.sav_count = (int)nspares; 875 spa->spa_spares.sav_vdevs = NULL; 876 877 if (nspares == 0) 878 return; 879 880 /* 881 * Construct the array of vdevs, opening them to get status in the 882 * process. For each spare, there is potentially two different vdev_t 883 * structures associated with it: one in the list of spares (used only 884 * for basic validation purposes) and one in the active vdev 885 * configuration (if it's spared in). During this phase we open and 886 * validate each vdev on the spare list. If the vdev also exists in the 887 * active configuration, then we also mark this vdev as an active spare. 888 */ 889 spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *), 890 KM_SLEEP); 891 for (i = 0; i < spa->spa_spares.sav_count; i++) { 892 VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 893 VDEV_ALLOC_SPARE) == 0); 894 ASSERT(vd != NULL); 895 896 spa->spa_spares.sav_vdevs[i] = vd; 897 898 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 899 B_FALSE)) != NULL) { 900 if (!tvd->vdev_isspare) 901 spa_spare_add(tvd); 902 903 /* 904 * We only mark the spare active if we were successfully 905 * able to load the vdev. Otherwise, importing a pool 906 * with a bad active spare would result in strange 907 * behavior, because multiple pool would think the spare 908 * is actively in use. 909 * 910 * There is a vulnerability here to an equally bizarre 911 * circumstance, where a dead active spare is later 912 * brought back to life (onlined or otherwise). Given 913 * the rarity of this scenario, and the extra complexity 914 * it adds, we ignore the possibility. 915 */ 916 if (!vdev_is_dead(tvd)) 917 spa_spare_activate(tvd); 918 } 919 920 vd->vdev_top = vd; 921 vd->vdev_aux = &spa->spa_spares; 922 923 if (vdev_open(vd) != 0) 924 continue; 925 926 if (vdev_validate_aux(vd) == 0) 927 spa_spare_add(vd); 928 } 929 930 /* 931 * Recompute the stashed list of spares, with status information 932 * this time. 933 */ 934 VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 935 DATA_TYPE_NVLIST_ARRAY) == 0); 936 937 spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), 938 KM_SLEEP); 939 for (i = 0; i < spa->spa_spares.sav_count; i++) 940 spares[i] = vdev_config_generate(spa, 941 spa->spa_spares.sav_vdevs[i], B_TRUE, B_TRUE, B_FALSE); 942 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 943 ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0); 944 for (i = 0; i < spa->spa_spares.sav_count; i++) 945 nvlist_free(spares[i]); 946 kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); 947 } 948 949 /* 950 * Load (or re-load) the current list of vdevs describing the active l2cache for 951 * this pool. When this is called, we have some form of basic information in 952 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and 953 * then re-generate a more complete list including status information. 954 * Devices which are already active have their details maintained, and are 955 * not re-opened. 956 */ 957 static void 958 spa_load_l2cache(spa_t *spa) 959 { 960 nvlist_t **l2cache; 961 uint_t nl2cache; 962 int i, j, oldnvdevs; 963 uint64_t guid; 964 vdev_t *vd, **oldvdevs, **newvdevs; 965 spa_aux_vdev_t *sav = &spa->spa_l2cache; 966 967 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 968 969 if (sav->sav_config != NULL) { 970 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 971 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 972 newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); 973 } else { 974 nl2cache = 0; 975 } 976 977 oldvdevs = sav->sav_vdevs; 978 oldnvdevs = sav->sav_count; 979 sav->sav_vdevs = NULL; 980 sav->sav_count = 0; 981 982 /* 983 * Process new nvlist of vdevs. 984 */ 985 for (i = 0; i < nl2cache; i++) { 986 VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID, 987 &guid) == 0); 988 989 newvdevs[i] = NULL; 990 for (j = 0; j < oldnvdevs; j++) { 991 vd = oldvdevs[j]; 992 if (vd != NULL && guid == vd->vdev_guid) { 993 /* 994 * Retain previous vdev for add/remove ops. 995 */ 996 newvdevs[i] = vd; 997 oldvdevs[j] = NULL; 998 break; 999 } 1000 } 1001 1002 if (newvdevs[i] == NULL) { 1003 /* 1004 * Create new vdev 1005 */ 1006 VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, 1007 VDEV_ALLOC_L2CACHE) == 0); 1008 ASSERT(vd != NULL); 1009 newvdevs[i] = vd; 1010 1011 /* 1012 * Commit this vdev as an l2cache device, 1013 * even if it fails to open. 1014 */ 1015 spa_l2cache_add(vd); 1016 1017 vd->vdev_top = vd; 1018 vd->vdev_aux = sav; 1019 1020 spa_l2cache_activate(vd); 1021 1022 if (vdev_open(vd) != 0) 1023 continue; 1024 1025 (void) vdev_validate_aux(vd); 1026 1027 if (!vdev_is_dead(vd)) 1028 l2arc_add_vdev(spa, vd); 1029 } 1030 } 1031 1032 /* 1033 * Purge vdevs that were dropped 1034 */ 1035 for (i = 0; i < oldnvdevs; i++) { 1036 uint64_t pool; 1037 1038 vd = oldvdevs[i]; 1039 if (vd != NULL) { 1040 if (spa_l2cache_exists(vd->vdev_guid, &pool) && 1041 pool != 0ULL && l2arc_vdev_present(vd)) 1042 l2arc_remove_vdev(vd); 1043 (void) vdev_close(vd); 1044 spa_l2cache_remove(vd); 1045 } 1046 } 1047 1048 if (oldvdevs) 1049 kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); 1050 1051 if (sav->sav_config == NULL) 1052 goto out; 1053 1054 sav->sav_vdevs = newvdevs; 1055 sav->sav_count = (int)nl2cache; 1056 1057 /* 1058 * Recompute the stashed list of l2cache devices, with status 1059 * information this time. 1060 */ 1061 VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 1062 DATA_TYPE_NVLIST_ARRAY) == 0); 1063 1064 l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 1065 for (i = 0; i < sav->sav_count; i++) 1066 l2cache[i] = vdev_config_generate(spa, 1067 sav->sav_vdevs[i], B_TRUE, B_FALSE, B_TRUE); 1068 VERIFY(nvlist_add_nvlist_array(sav->sav_config, 1069 ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0); 1070 out: 1071 for (i = 0; i < sav->sav_count; i++) 1072 nvlist_free(l2cache[i]); 1073 if (sav->sav_count) 1074 kmem_free(l2cache, sav->sav_count * sizeof (void *)); 1075 } 1076 1077 static int 1078 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 1079 { 1080 dmu_buf_t *db; 1081 char *packed = NULL; 1082 size_t nvsize = 0; 1083 int error; 1084 *value = NULL; 1085 1086 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 1087 nvsize = *(uint64_t *)db->db_data; 1088 dmu_buf_rele(db, FTAG); 1089 1090 packed = kmem_alloc(nvsize, KM_SLEEP); 1091 error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed, 1092 DMU_READ_PREFETCH); 1093 if (error == 0) 1094 error = nvlist_unpack(packed, nvsize, value, 0); 1095 kmem_free(packed, nvsize); 1096 1097 return (error); 1098 } 1099 1100 /* 1101 * Checks to see if the given vdev could not be opened, in which case we post a 1102 * sysevent to notify the autoreplace code that the device has been removed. 1103 */ 1104 static void 1105 spa_check_removed(vdev_t *vd) 1106 { 1107 for (int c = 0; c < vd->vdev_children; c++) 1108 spa_check_removed(vd->vdev_child[c]); 1109 1110 if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) { 1111 zfs_post_autoreplace(vd->vdev_spa, vd); 1112 spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK); 1113 } 1114 } 1115 1116 /* 1117 * Load the slog device state from the config object since it's possible 1118 * that the label does not contain the most up-to-date information. 1119 */ 1120 void 1121 spa_load_log_state(spa_t *spa, nvlist_t *nv) 1122 { 1123 vdev_t *ovd, *rvd = spa->spa_root_vdev; 1124 1125 /* 1126 * Load the original root vdev tree from the passed config. 1127 */ 1128 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1129 VERIFY(spa_config_parse(spa, &ovd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0); 1130 1131 for (int c = 0; c < rvd->vdev_children; c++) { 1132 vdev_t *cvd = rvd->vdev_child[c]; 1133 if (cvd->vdev_islog) 1134 vdev_load_log_state(cvd, ovd->vdev_child[c]); 1135 } 1136 vdev_free(ovd); 1137 spa_config_exit(spa, SCL_ALL, FTAG); 1138 } 1139 1140 /* 1141 * Check for missing log devices 1142 */ 1143 int 1144 spa_check_logs(spa_t *spa) 1145 { 1146 switch (spa->spa_log_state) { 1147 case SPA_LOG_MISSING: 1148 /* need to recheck in case slog has been restored */ 1149 case SPA_LOG_UNKNOWN: 1150 if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL, 1151 DS_FIND_CHILDREN)) { 1152 spa->spa_log_state = SPA_LOG_MISSING; 1153 return (1); 1154 } 1155 break; 1156 } 1157 return (0); 1158 } 1159 1160 static void 1161 spa_aux_check_removed(spa_aux_vdev_t *sav) 1162 { 1163 for (int i = 0; i < sav->sav_count; i++) 1164 spa_check_removed(sav->sav_vdevs[i]); 1165 } 1166 1167 void 1168 spa_claim_notify(zio_t *zio) 1169 { 1170 spa_t *spa = zio->io_spa; 1171 1172 if (zio->io_error) 1173 return; 1174 1175 mutex_enter(&spa->spa_props_lock); /* any mutex will do */ 1176 if (spa->spa_claim_max_txg < zio->io_bp->blk_birth) 1177 spa->spa_claim_max_txg = zio->io_bp->blk_birth; 1178 mutex_exit(&spa->spa_props_lock); 1179 } 1180 1181 typedef struct spa_load_error { 1182 uint64_t sle_metadata_count; 1183 uint64_t sle_data_count; 1184 } spa_load_error_t; 1185 1186 static void 1187 spa_load_verify_done(zio_t *zio) 1188 { 1189 blkptr_t *bp = zio->io_bp; 1190 spa_load_error_t *sle = zio->io_private; 1191 dmu_object_type_t type = BP_GET_TYPE(bp); 1192 int error = zio->io_error; 1193 1194 if (error) { 1195 if ((BP_GET_LEVEL(bp) != 0 || dmu_ot[type].ot_metadata) && 1196 type != DMU_OT_INTENT_LOG) 1197 atomic_add_64(&sle->sle_metadata_count, 1); 1198 else 1199 atomic_add_64(&sle->sle_data_count, 1); 1200 } 1201 zio_data_buf_free(zio->io_data, zio->io_size); 1202 } 1203 1204 /*ARGSUSED*/ 1205 static int 1206 spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 1207 const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg) 1208 { 1209 if (bp != NULL) { 1210 zio_t *rio = arg; 1211 size_t size = BP_GET_PSIZE(bp); 1212 void *data = zio_data_buf_alloc(size); 1213 1214 zio_nowait(zio_read(rio, spa, bp, data, size, 1215 spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB, 1216 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL | 1217 ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb)); 1218 } 1219 return (0); 1220 } 1221 1222 static int 1223 spa_load_verify(spa_t *spa) 1224 { 1225 zio_t *rio; 1226 spa_load_error_t sle = { 0 }; 1227 zpool_rewind_policy_t policy; 1228 boolean_t verify_ok = B_FALSE; 1229 int error; 1230 1231 rio = zio_root(spa, NULL, &sle, 1232 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 1233 1234 error = traverse_pool(spa, spa_load_verify_cb, rio, 1235 spa->spa_verify_min_txg); 1236 1237 (void) zio_wait(rio); 1238 1239 zpool_get_rewind_policy(spa->spa_config, &policy); 1240 1241 spa->spa_load_meta_errors = sle.sle_metadata_count; 1242 spa->spa_load_data_errors = sle.sle_data_count; 1243 1244 if (!error && sle.sle_metadata_count <= policy.zrp_maxmeta && 1245 sle.sle_data_count <= policy.zrp_maxdata) { 1246 verify_ok = B_TRUE; 1247 spa->spa_load_txg = spa->spa_uberblock.ub_txg; 1248 spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp; 1249 } 1250 1251 if (error) { 1252 if (error != ENXIO && error != EIO) 1253 error = EIO; 1254 return (error); 1255 } 1256 1257 return (verify_ok ? 0 : EIO); 1258 } 1259 1260 /* 1261 * Load an existing storage pool, using the pool's builtin spa_config as a 1262 * source of configuration information. 1263 */ 1264 static int 1265 spa_load(spa_t *spa, spa_load_state_t state, int mosconfig) 1266 { 1267 int error = 0; 1268 nvlist_t *nvconfig, *nvroot = NULL; 1269 vdev_t *rvd; 1270 uberblock_t *ub = &spa->spa_uberblock; 1271 uint64_t config_cache_txg = spa->spa_config_txg; 1272 uint64_t pool_guid; 1273 uint64_t version; 1274 uint64_t autoreplace = 0; 1275 int orig_mode = spa->spa_mode; 1276 char *ereport = FM_EREPORT_ZFS_POOL; 1277 nvlist_t *config = spa->spa_config; 1278 1279 /* 1280 * If this is an untrusted config, access the pool in read-only mode. 1281 * This prevents things like resilvering recently removed devices. 1282 */ 1283 if (!mosconfig) 1284 spa->spa_mode = FREAD; 1285 1286 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 1287 1288 spa->spa_load_state = state; 1289 1290 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) || 1291 nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) { 1292 error = EINVAL; 1293 goto out; 1294 } 1295 1296 /* 1297 * Versioning wasn't explicitly added to the label until later, so if 1298 * it's not present treat it as the initial version. 1299 */ 1300 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0) 1301 version = SPA_VERSION_INITIAL; 1302 1303 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 1304 &spa->spa_config_txg); 1305 1306 if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && 1307 spa_guid_exists(pool_guid, 0)) { 1308 error = EEXIST; 1309 goto out; 1310 } 1311 1312 spa->spa_load_guid = pool_guid; 1313 1314 /* 1315 * Create "The Godfather" zio to hold all async IOs 1316 */ 1317 spa->spa_async_zio_root = zio_root(spa, NULL, NULL, 1318 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER); 1319 1320 /* 1321 * Parse the configuration into a vdev tree. We explicitly set the 1322 * value that will be returned by spa_version() since parsing the 1323 * configuration requires knowing the version number. 1324 */ 1325 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1326 spa->spa_ubsync.ub_version = version; 1327 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD); 1328 spa_config_exit(spa, SCL_ALL, FTAG); 1329 1330 if (error != 0) 1331 goto out; 1332 1333 ASSERT(spa->spa_root_vdev == rvd); 1334 ASSERT(spa_guid(spa) == pool_guid); 1335 1336 /* 1337 * Try to open all vdevs, loading each label in the process. 1338 */ 1339 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1340 error = vdev_open(rvd); 1341 spa_config_exit(spa, SCL_ALL, FTAG); 1342 if (error != 0) 1343 goto out; 1344 1345 /* 1346 * We need to validate the vdev labels against the configuration that 1347 * we have in hand, which is dependent on the setting of mosconfig. If 1348 * mosconfig is true then we're validating the vdev labels based on 1349 * that config. Otherwise, we're validating against the cached config 1350 * (zpool.cache) that was read when we loaded the zfs module, and then 1351 * later we will recursively call spa_load() and validate against 1352 * the vdev config. 1353 */ 1354 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1355 error = vdev_validate(rvd); 1356 spa_config_exit(spa, SCL_ALL, FTAG); 1357 if (error != 0) 1358 goto out; 1359 1360 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 1361 error = ENXIO; 1362 goto out; 1363 } 1364 1365 /* 1366 * Find the best uberblock. 1367 */ 1368 vdev_uberblock_load(NULL, rvd, ub); 1369 1370 /* 1371 * If we weren't able to find a single valid uberblock, return failure. 1372 */ 1373 if (ub->ub_txg == 0) { 1374 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1375 VDEV_AUX_CORRUPT_DATA); 1376 error = ENXIO; 1377 goto out; 1378 } 1379 1380 /* 1381 * If the pool is newer than the code, we can't open it. 1382 */ 1383 if (ub->ub_version > SPA_VERSION) { 1384 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1385 VDEV_AUX_VERSION_NEWER); 1386 error = ENOTSUP; 1387 goto out; 1388 } 1389 1390 /* 1391 * If the vdev guid sum doesn't match the uberblock, we have an 1392 * incomplete configuration. 1393 */ 1394 if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) { 1395 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1396 VDEV_AUX_BAD_GUID_SUM); 1397 error = ENXIO; 1398 goto out; 1399 } 1400 1401 /* 1402 * Initialize internal SPA structures. 1403 */ 1404 spa->spa_state = POOL_STATE_ACTIVE; 1405 spa->spa_ubsync = spa->spa_uberblock; 1406 spa->spa_verify_min_txg = spa->spa_extreme_rewind ? 1407 TXG_INITIAL : spa_last_synced_txg(spa) - TXG_DEFER_SIZE; 1408 spa->spa_first_txg = spa->spa_last_ubsync_txg ? 1409 spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1; 1410 spa->spa_claim_max_txg = spa->spa_first_txg; 1411 1412 error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 1413 if (error) { 1414 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1415 VDEV_AUX_CORRUPT_DATA); 1416 error = EIO; 1417 goto out; 1418 } 1419 spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 1420 1421 if (zap_lookup(spa->spa_meta_objset, 1422 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 1423 sizeof (uint64_t), 1, &spa->spa_config_object) != 0) { 1424 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1425 VDEV_AUX_CORRUPT_DATA); 1426 error = EIO; 1427 goto out; 1428 } 1429 1430 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0) { 1431 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1432 VDEV_AUX_CORRUPT_DATA); 1433 error = EIO; 1434 goto out; 1435 } 1436 1437 if (!mosconfig) { 1438 uint64_t hostid; 1439 1440 if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig, 1441 ZPOOL_CONFIG_HOSTID, &hostid) == 0) { 1442 char *hostname; 1443 unsigned long myhostid = 0; 1444 1445 VERIFY(nvlist_lookup_string(nvconfig, 1446 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); 1447 1448 #ifdef _KERNEL 1449 myhostid = zone_get_hostid(NULL); 1450 #else /* _KERNEL */ 1451 /* 1452 * We're emulating the system's hostid in userland, so 1453 * we can't use zone_get_hostid(). 1454 */ 1455 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 1456 #endif /* _KERNEL */ 1457 if (hostid != 0 && myhostid != 0 && 1458 hostid != myhostid) { 1459 cmn_err(CE_WARN, "pool '%s' could not be " 1460 "loaded as it was last accessed by " 1461 "another system (host: %s hostid: 0x%lx). " 1462 "See: http://www.sun.com/msg/ZFS-8000-EY", 1463 spa_name(spa), hostname, 1464 (unsigned long)hostid); 1465 error = EBADF; 1466 goto out; 1467 } 1468 } 1469 1470 spa_config_set(spa, nvconfig); 1471 spa_unload(spa); 1472 spa_deactivate(spa); 1473 spa_activate(spa, orig_mode); 1474 1475 return (spa_load(spa, state, B_TRUE)); 1476 } 1477 1478 if (zap_lookup(spa->spa_meta_objset, 1479 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 1480 sizeof (uint64_t), 1, &spa->spa_deferred_bplist_obj) != 0) { 1481 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1482 VDEV_AUX_CORRUPT_DATA); 1483 error = EIO; 1484 goto out; 1485 } 1486 1487 /* 1488 * Load the bit that tells us to use the new accounting function 1489 * (raid-z deflation). If we have an older pool, this will not 1490 * be present. 1491 */ 1492 error = zap_lookup(spa->spa_meta_objset, 1493 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 1494 sizeof (uint64_t), 1, &spa->spa_deflate); 1495 if (error != 0 && error != ENOENT) { 1496 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1497 VDEV_AUX_CORRUPT_DATA); 1498 error = EIO; 1499 goto out; 1500 } 1501 1502 /* 1503 * Load the persistent error log. If we have an older pool, this will 1504 * not be present. 1505 */ 1506 error = zap_lookup(spa->spa_meta_objset, 1507 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST, 1508 sizeof (uint64_t), 1, &spa->spa_errlog_last); 1509 if (error != 0 && error != ENOENT) { 1510 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1511 VDEV_AUX_CORRUPT_DATA); 1512 error = EIO; 1513 goto out; 1514 } 1515 1516 error = zap_lookup(spa->spa_meta_objset, 1517 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB, 1518 sizeof (uint64_t), 1, &spa->spa_errlog_scrub); 1519 if (error != 0 && error != ENOENT) { 1520 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1521 VDEV_AUX_CORRUPT_DATA); 1522 error = EIO; 1523 goto out; 1524 } 1525 1526 /* 1527 * Load the history object. If we have an older pool, this 1528 * will not be present. 1529 */ 1530 error = zap_lookup(spa->spa_meta_objset, 1531 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY, 1532 sizeof (uint64_t), 1, &spa->spa_history); 1533 if (error != 0 && error != ENOENT) { 1534 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1535 VDEV_AUX_CORRUPT_DATA); 1536 error = EIO; 1537 goto out; 1538 } 1539 1540 /* 1541 * Load any hot spares for this pool. 1542 */ 1543 error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 1544 DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares.sav_object); 1545 if (error != 0 && error != ENOENT) { 1546 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1547 VDEV_AUX_CORRUPT_DATA); 1548 error = EIO; 1549 goto out; 1550 } 1551 if (error == 0) { 1552 ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); 1553 if (load_nvlist(spa, spa->spa_spares.sav_object, 1554 &spa->spa_spares.sav_config) != 0) { 1555 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1556 VDEV_AUX_CORRUPT_DATA); 1557 error = EIO; 1558 goto out; 1559 } 1560 1561 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1562 spa_load_spares(spa); 1563 spa_config_exit(spa, SCL_ALL, FTAG); 1564 } 1565 1566 /* 1567 * Load any level 2 ARC devices for this pool. 1568 */ 1569 error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 1570 DMU_POOL_L2CACHE, sizeof (uint64_t), 1, 1571 &spa->spa_l2cache.sav_object); 1572 if (error != 0 && error != ENOENT) { 1573 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1574 VDEV_AUX_CORRUPT_DATA); 1575 error = EIO; 1576 goto out; 1577 } 1578 if (error == 0) { 1579 ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); 1580 if (load_nvlist(spa, spa->spa_l2cache.sav_object, 1581 &spa->spa_l2cache.sav_config) != 0) { 1582 vdev_set_state(rvd, B_TRUE, 1583 VDEV_STATE_CANT_OPEN, 1584 VDEV_AUX_CORRUPT_DATA); 1585 error = EIO; 1586 goto out; 1587 } 1588 1589 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1590 spa_load_l2cache(spa); 1591 spa_config_exit(spa, SCL_ALL, FTAG); 1592 } 1593 1594 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 1595 1596 error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 1597 DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object); 1598 1599 if (error && error != ENOENT) { 1600 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1601 VDEV_AUX_CORRUPT_DATA); 1602 error = EIO; 1603 goto out; 1604 } 1605 1606 if (error == 0) { 1607 (void) zap_lookup(spa->spa_meta_objset, 1608 spa->spa_pool_props_object, 1609 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), 1610 sizeof (uint64_t), 1, &spa->spa_bootfs); 1611 (void) zap_lookup(spa->spa_meta_objset, 1612 spa->spa_pool_props_object, 1613 zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE), 1614 sizeof (uint64_t), 1, &autoreplace); 1615 spa->spa_autoreplace = (autoreplace != 0); 1616 (void) zap_lookup(spa->spa_meta_objset, 1617 spa->spa_pool_props_object, 1618 zpool_prop_to_name(ZPOOL_PROP_DELEGATION), 1619 sizeof (uint64_t), 1, &spa->spa_delegation); 1620 (void) zap_lookup(spa->spa_meta_objset, 1621 spa->spa_pool_props_object, 1622 zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE), 1623 sizeof (uint64_t), 1, &spa->spa_failmode); 1624 (void) zap_lookup(spa->spa_meta_objset, 1625 spa->spa_pool_props_object, 1626 zpool_prop_to_name(ZPOOL_PROP_AUTOEXPAND), 1627 sizeof (uint64_t), 1, &spa->spa_autoexpand); 1628 (void) zap_lookup(spa->spa_meta_objset, 1629 spa->spa_pool_props_object, 1630 zpool_prop_to_name(ZPOOL_PROP_DEDUPDITTO), 1631 sizeof (uint64_t), 1, &spa->spa_dedup_ditto); 1632 } 1633 1634 /* 1635 * If the 'autoreplace' property is set, then post a resource notifying 1636 * the ZFS DE that it should not issue any faults for unopenable 1637 * devices. We also iterate over the vdevs, and post a sysevent for any 1638 * unopenable vdevs so that the normal autoreplace handler can take 1639 * over. 1640 */ 1641 if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) { 1642 spa_check_removed(spa->spa_root_vdev); 1643 /* 1644 * For the import case, this is done in spa_import(), because 1645 * at this point we're using the spare definitions from 1646 * the MOS config, not necessarily from the userland config. 1647 */ 1648 if (state != SPA_LOAD_IMPORT) { 1649 spa_aux_check_removed(&spa->spa_spares); 1650 spa_aux_check_removed(&spa->spa_l2cache); 1651 } 1652 } 1653 1654 /* 1655 * Load the vdev state for all toplevel vdevs. 1656 */ 1657 vdev_load(rvd); 1658 1659 /* 1660 * Propagate the leaf DTLs we just loaded all the way up the tree. 1661 */ 1662 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1663 vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 1664 spa_config_exit(spa, SCL_ALL, FTAG); 1665 1666 /* 1667 * Check the state of the root vdev. If it can't be opened, it 1668 * indicates one or more toplevel vdevs are faulted. 1669 */ 1670 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 1671 error = ENXIO; 1672 goto out; 1673 } 1674 1675 /* 1676 * Load the DDTs (dedup tables). 1677 */ 1678 error = ddt_load(spa); 1679 if (error != 0) { 1680 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1681 VDEV_AUX_CORRUPT_DATA); 1682 error = EIO; 1683 goto out; 1684 } 1685 1686 if (state != SPA_LOAD_TRYIMPORT) { 1687 error = spa_load_verify(spa); 1688 if (error) { 1689 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1690 VDEV_AUX_CORRUPT_DATA); 1691 goto out; 1692 } 1693 } 1694 1695 /* 1696 * Load the intent log state and check log integrity. 1697 */ 1698 VERIFY(nvlist_lookup_nvlist(nvconfig, ZPOOL_CONFIG_VDEV_TREE, 1699 &nvroot) == 0); 1700 spa_load_log_state(spa, nvroot); 1701 nvlist_free(nvconfig); 1702 1703 if (spa_check_logs(spa)) { 1704 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN, 1705 VDEV_AUX_BAD_LOG); 1706 error = ENXIO; 1707 ereport = FM_EREPORT_ZFS_LOG_REPLAY; 1708 goto out; 1709 } 1710 1711 if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER || 1712 spa->spa_load_max_txg == UINT64_MAX)) { 1713 dmu_tx_t *tx; 1714 int need_update = B_FALSE; 1715 1716 ASSERT(state != SPA_LOAD_TRYIMPORT); 1717 1718 /* 1719 * Claim log blocks that haven't been committed yet. 1720 * This must all happen in a single txg. 1721 * Note: spa_claim_max_txg is updated by spa_claim_notify(), 1722 * invoked from zil_claim_log_block()'s i/o done callback. 1723 * Price of rollback is that we abandon the log. 1724 */ 1725 spa->spa_claiming = B_TRUE; 1726 1727 tx = dmu_tx_create_assigned(spa_get_dsl(spa), 1728 spa_first_txg(spa)); 1729 (void) dmu_objset_find(spa_name(spa), 1730 zil_claim, tx, DS_FIND_CHILDREN); 1731 dmu_tx_commit(tx); 1732 1733 spa->spa_claiming = B_FALSE; 1734 1735 spa->spa_log_state = SPA_LOG_GOOD; 1736 spa->spa_sync_on = B_TRUE; 1737 txg_sync_start(spa->spa_dsl_pool); 1738 1739 /* 1740 * Wait for all claims to sync. We sync up to the highest 1741 * claimed log block birth time so that claimed log blocks 1742 * don't appear to be from the future. spa_claim_max_txg 1743 * will have been set for us by either zil_check_log_chain() 1744 * (invoked from spa_check_logs()) or zil_claim() above. 1745 */ 1746 txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg); 1747 1748 /* 1749 * If the config cache is stale, or we have uninitialized 1750 * metaslabs (see spa_vdev_add()), then update the config. 1751 * 1752 * If spa_load_verbatim is true, trust the current 1753 * in-core spa_config and update the disk labels. 1754 */ 1755 if (config_cache_txg != spa->spa_config_txg || 1756 state == SPA_LOAD_IMPORT || spa->spa_load_verbatim || 1757 state == SPA_LOAD_RECOVER) 1758 need_update = B_TRUE; 1759 1760 for (int c = 0; c < rvd->vdev_children; c++) 1761 if (rvd->vdev_child[c]->vdev_ms_array == 0) 1762 need_update = B_TRUE; 1763 1764 /* 1765 * Update the config cache asychronously in case we're the 1766 * root pool, in which case the config cache isn't writable yet. 1767 */ 1768 if (need_update) 1769 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 1770 1771 /* 1772 * Check all DTLs to see if anything needs resilvering. 1773 */ 1774 if (vdev_resilver_needed(rvd, NULL, NULL)) 1775 spa_async_request(spa, SPA_ASYNC_RESILVER); 1776 1777 /* 1778 * Delete any inconsistent datasets. 1779 */ 1780 (void) dmu_objset_find(spa_name(spa), 1781 dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN); 1782 1783 /* 1784 * Clean up any stale temporary dataset userrefs. 1785 */ 1786 dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool); 1787 } 1788 1789 error = 0; 1790 out: 1791 1792 spa->spa_minref = refcount_count(&spa->spa_refcount); 1793 if (error && error != EBADF) 1794 zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0); 1795 spa->spa_load_state = SPA_LOAD_NONE; 1796 spa->spa_ena = 0; 1797 1798 return (error); 1799 } 1800 1801 static int 1802 spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig) 1803 { 1804 spa_unload(spa); 1805 spa_deactivate(spa); 1806 1807 spa->spa_load_max_txg--; 1808 1809 spa_activate(spa, spa_mode_global); 1810 spa_async_suspend(spa); 1811 1812 return (spa_load(spa, state, mosconfig)); 1813 } 1814 1815 static int 1816 spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig, 1817 uint64_t max_request, boolean_t extreme) 1818 { 1819 nvlist_t *config = NULL; 1820 int load_error, rewind_error; 1821 uint64_t safe_rollback_txg; 1822 uint64_t min_txg; 1823 1824 if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) 1825 spa->spa_load_max_txg = spa->spa_load_txg; 1826 else 1827 spa->spa_load_max_txg = max_request; 1828 1829 load_error = rewind_error = spa_load(spa, state, mosconfig); 1830 if (load_error == 0) 1831 return (0); 1832 1833 if (spa->spa_root_vdev != NULL) 1834 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 1835 1836 spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg; 1837 spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp; 1838 1839 /* specific txg requested */ 1840 if (spa->spa_load_max_txg != UINT64_MAX && !extreme) { 1841 nvlist_free(config); 1842 return (load_error); 1843 } 1844 1845 /* Price of rolling back is discarding txgs, including log */ 1846 if (state == SPA_LOAD_RECOVER) 1847 spa->spa_log_state = SPA_LOG_CLEAR; 1848 1849 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg; 1850 safe_rollback_txg = spa->spa_uberblock.ub_txg - TXG_DEFER_SIZE; 1851 1852 min_txg = extreme ? TXG_INITIAL : safe_rollback_txg; 1853 while (rewind_error && (spa->spa_uberblock.ub_txg >= min_txg)) { 1854 if (spa->spa_load_max_txg < safe_rollback_txg) 1855 spa->spa_extreme_rewind = B_TRUE; 1856 rewind_error = spa_load_retry(spa, state, mosconfig); 1857 } 1858 1859 if (config) 1860 spa_rewind_data_to_nvlist(spa, config); 1861 1862 spa->spa_extreme_rewind = B_FALSE; 1863 spa->spa_load_max_txg = UINT64_MAX; 1864 1865 if (config && (rewind_error || state != SPA_LOAD_RECOVER)) 1866 spa_config_set(spa, config); 1867 1868 return (state == SPA_LOAD_RECOVER ? rewind_error : load_error); 1869 } 1870 1871 /* 1872 * Pool Open/Import 1873 * 1874 * The import case is identical to an open except that the configuration is sent 1875 * down from userland, instead of grabbed from the configuration cache. For the 1876 * case of an open, the pool configuration will exist in the 1877 * POOL_STATE_UNINITIALIZED state. 1878 * 1879 * The stats information (gen/count/ustats) is used to gather vdev statistics at 1880 * the same time open the pool, without having to keep around the spa_t in some 1881 * ambiguous state. 1882 */ 1883 static int 1884 spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy, 1885 nvlist_t **config) 1886 { 1887 spa_t *spa; 1888 boolean_t norewind; 1889 boolean_t extreme; 1890 zpool_rewind_policy_t policy; 1891 spa_load_state_t state = SPA_LOAD_OPEN; 1892 int error; 1893 int locked = B_FALSE; 1894 1895 *spapp = NULL; 1896 1897 zpool_get_rewind_policy(nvpolicy, &policy); 1898 if (policy.zrp_request & ZPOOL_DO_REWIND) 1899 state = SPA_LOAD_RECOVER; 1900 norewind = (policy.zrp_request == ZPOOL_NO_REWIND); 1901 extreme = ((policy.zrp_request & ZPOOL_EXTREME_REWIND) != 0); 1902 1903 /* 1904 * As disgusting as this is, we need to support recursive calls to this 1905 * function because dsl_dir_open() is called during spa_load(), and ends 1906 * up calling spa_open() again. The real fix is to figure out how to 1907 * avoid dsl_dir_open() calling this in the first place. 1908 */ 1909 if (mutex_owner(&spa_namespace_lock) != curthread) { 1910 mutex_enter(&spa_namespace_lock); 1911 locked = B_TRUE; 1912 } 1913 1914 if ((spa = spa_lookup(pool)) == NULL) { 1915 if (locked) 1916 mutex_exit(&spa_namespace_lock); 1917 return (ENOENT); 1918 } 1919 1920 if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 1921 1922 spa_activate(spa, spa_mode_global); 1923 1924 if (spa->spa_last_open_failed && norewind) { 1925 if (config != NULL && spa->spa_config) 1926 VERIFY(nvlist_dup(spa->spa_config, 1927 config, KM_SLEEP) == 0); 1928 spa_deactivate(spa); 1929 if (locked) 1930 mutex_exit(&spa_namespace_lock); 1931 return (spa->spa_last_open_failed); 1932 } 1933 1934 if (state != SPA_LOAD_RECOVER) 1935 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 1936 1937 error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg, 1938 extreme); 1939 1940 if (error == EBADF) { 1941 /* 1942 * If vdev_validate() returns failure (indicated by 1943 * EBADF), it indicates that one of the vdevs indicates 1944 * that the pool has been exported or destroyed. If 1945 * this is the case, the config cache is out of sync and 1946 * we should remove the pool from the namespace. 1947 */ 1948 spa_unload(spa); 1949 spa_deactivate(spa); 1950 spa_config_sync(spa, B_TRUE, B_TRUE); 1951 spa_remove(spa); 1952 if (locked) 1953 mutex_exit(&spa_namespace_lock); 1954 return (ENOENT); 1955 } 1956 1957 if (error) { 1958 /* 1959 * We can't open the pool, but we still have useful 1960 * information: the state of each vdev after the 1961 * attempted vdev_open(). Return this to the user. 1962 */ 1963 if (config != NULL && spa->spa_config) 1964 VERIFY(nvlist_dup(spa->spa_config, config, 1965 KM_SLEEP) == 0); 1966 spa_unload(spa); 1967 spa_deactivate(spa); 1968 spa->spa_last_open_failed = error; 1969 if (locked) 1970 mutex_exit(&spa_namespace_lock); 1971 *spapp = NULL; 1972 return (error); 1973 } 1974 1975 } 1976 1977 spa_open_ref(spa, tag); 1978 1979 spa->spa_last_open_failed = 0; 1980 1981 if (config != NULL) 1982 *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 1983 1984 spa->spa_last_ubsync_txg = 0; 1985 spa->spa_load_txg = 0; 1986 1987 if (locked) 1988 mutex_exit(&spa_namespace_lock); 1989 1990 *spapp = spa; 1991 1992 return (0); 1993 } 1994 1995 int 1996 spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy, 1997 nvlist_t **config) 1998 { 1999 return (spa_open_common(name, spapp, tag, policy, config)); 2000 } 2001 2002 int 2003 spa_open(const char *name, spa_t **spapp, void *tag) 2004 { 2005 return (spa_open_common(name, spapp, tag, NULL, NULL)); 2006 } 2007 2008 /* 2009 * Lookup the given spa_t, incrementing the inject count in the process, 2010 * preventing it from being exported or destroyed. 2011 */ 2012 spa_t * 2013 spa_inject_addref(char *name) 2014 { 2015 spa_t *spa; 2016 2017 mutex_enter(&spa_namespace_lock); 2018 if ((spa = spa_lookup(name)) == NULL) { 2019 mutex_exit(&spa_namespace_lock); 2020 return (NULL); 2021 } 2022 spa->spa_inject_ref++; 2023 mutex_exit(&spa_namespace_lock); 2024 2025 return (spa); 2026 } 2027 2028 void 2029 spa_inject_delref(spa_t *spa) 2030 { 2031 mutex_enter(&spa_namespace_lock); 2032 spa->spa_inject_ref--; 2033 mutex_exit(&spa_namespace_lock); 2034 } 2035 2036 /* 2037 * Add spares device information to the nvlist. 2038 */ 2039 static void 2040 spa_add_spares(spa_t *spa, nvlist_t *config) 2041 { 2042 nvlist_t **spares; 2043 uint_t i, nspares; 2044 nvlist_t *nvroot; 2045 uint64_t guid; 2046 vdev_stat_t *vs; 2047 uint_t vsc; 2048 uint64_t pool; 2049 2050 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 2051 2052 if (spa->spa_spares.sav_count == 0) 2053 return; 2054 2055 VERIFY(nvlist_lookup_nvlist(config, 2056 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 2057 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 2058 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 2059 if (nspares != 0) { 2060 VERIFY(nvlist_add_nvlist_array(nvroot, 2061 ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 2062 VERIFY(nvlist_lookup_nvlist_array(nvroot, 2063 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 2064 2065 /* 2066 * Go through and find any spares which have since been 2067 * repurposed as an active spare. If this is the case, update 2068 * their status appropriately. 2069 */ 2070 for (i = 0; i < nspares; i++) { 2071 VERIFY(nvlist_lookup_uint64(spares[i], 2072 ZPOOL_CONFIG_GUID, &guid) == 0); 2073 if (spa_spare_exists(guid, &pool, NULL) && 2074 pool != 0ULL) { 2075 VERIFY(nvlist_lookup_uint64_array( 2076 spares[i], ZPOOL_CONFIG_STATS, 2077 (uint64_t **)&vs, &vsc) == 0); 2078 vs->vs_state = VDEV_STATE_CANT_OPEN; 2079 vs->vs_aux = VDEV_AUX_SPARED; 2080 } 2081 } 2082 } 2083 } 2084 2085 /* 2086 * Add l2cache device information to the nvlist, including vdev stats. 2087 */ 2088 static void 2089 spa_add_l2cache(spa_t *spa, nvlist_t *config) 2090 { 2091 nvlist_t **l2cache; 2092 uint_t i, j, nl2cache; 2093 nvlist_t *nvroot; 2094 uint64_t guid; 2095 vdev_t *vd; 2096 vdev_stat_t *vs; 2097 uint_t vsc; 2098 2099 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 2100 2101 if (spa->spa_l2cache.sav_count == 0) 2102 return; 2103 2104 VERIFY(nvlist_lookup_nvlist(config, 2105 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 2106 VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 2107 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 2108 if (nl2cache != 0) { 2109 VERIFY(nvlist_add_nvlist_array(nvroot, 2110 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 2111 VERIFY(nvlist_lookup_nvlist_array(nvroot, 2112 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 2113 2114 /* 2115 * Update level 2 cache device stats. 2116 */ 2117 2118 for (i = 0; i < nl2cache; i++) { 2119 VERIFY(nvlist_lookup_uint64(l2cache[i], 2120 ZPOOL_CONFIG_GUID, &guid) == 0); 2121 2122 vd = NULL; 2123 for (j = 0; j < spa->spa_l2cache.sav_count; j++) { 2124 if (guid == 2125 spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { 2126 vd = spa->spa_l2cache.sav_vdevs[j]; 2127 break; 2128 } 2129 } 2130 ASSERT(vd != NULL); 2131 2132 VERIFY(nvlist_lookup_uint64_array(l2cache[i], 2133 ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0); 2134 vdev_get_stats(vd, vs); 2135 } 2136 } 2137 } 2138 2139 int 2140 spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen) 2141 { 2142 int error; 2143 spa_t *spa; 2144 2145 *config = NULL; 2146 error = spa_open_common(name, &spa, FTAG, NULL, config); 2147 2148 if (spa != NULL) { 2149 /* 2150 * This still leaves a window of inconsistency where the spares 2151 * or l2cache devices could change and the config would be 2152 * self-inconsistent. 2153 */ 2154 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 2155 2156 if (*config != NULL) { 2157 VERIFY(nvlist_add_uint64(*config, 2158 ZPOOL_CONFIG_ERRCOUNT, 2159 spa_get_errlog_size(spa)) == 0); 2160 2161 if (spa_suspended(spa)) 2162 VERIFY(nvlist_add_uint64(*config, 2163 ZPOOL_CONFIG_SUSPENDED, 2164 spa->spa_failmode) == 0); 2165 2166 spa_add_spares(spa, *config); 2167 spa_add_l2cache(spa, *config); 2168 } 2169 } 2170 2171 /* 2172 * We want to get the alternate root even for faulted pools, so we cheat 2173 * and call spa_lookup() directly. 2174 */ 2175 if (altroot) { 2176 if (spa == NULL) { 2177 mutex_enter(&spa_namespace_lock); 2178 spa = spa_lookup(name); 2179 if (spa) 2180 spa_altroot(spa, altroot, buflen); 2181 else 2182 altroot[0] = '\0'; 2183 spa = NULL; 2184 mutex_exit(&spa_namespace_lock); 2185 } else { 2186 spa_altroot(spa, altroot, buflen); 2187 } 2188 } 2189 2190 if (spa != NULL) { 2191 spa_config_exit(spa, SCL_CONFIG, FTAG); 2192 spa_close(spa, FTAG); 2193 } 2194 2195 return (error); 2196 } 2197 2198 /* 2199 * Validate that the auxiliary device array is well formed. We must have an 2200 * array of nvlists, each which describes a valid leaf vdev. If this is an 2201 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be 2202 * specified, as long as they are well-formed. 2203 */ 2204 static int 2205 spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, 2206 spa_aux_vdev_t *sav, const char *config, uint64_t version, 2207 vdev_labeltype_t label) 2208 { 2209 nvlist_t **dev; 2210 uint_t i, ndev; 2211 vdev_t *vd; 2212 int error; 2213 2214 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 2215 2216 /* 2217 * It's acceptable to have no devs specified. 2218 */ 2219 if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) 2220 return (0); 2221 2222 if (ndev == 0) 2223 return (EINVAL); 2224 2225 /* 2226 * Make sure the pool is formatted with a version that supports this 2227 * device type. 2228 */ 2229 if (spa_version(spa) < version) 2230 return (ENOTSUP); 2231 2232 /* 2233 * Set the pending device list so we correctly handle device in-use 2234 * checking. 2235 */ 2236 sav->sav_pending = dev; 2237 sav->sav_npending = ndev; 2238 2239 for (i = 0; i < ndev; i++) { 2240 if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, 2241 mode)) != 0) 2242 goto out; 2243 2244 if (!vd->vdev_ops->vdev_op_leaf) { 2245 vdev_free(vd); 2246 error = EINVAL; 2247 goto out; 2248 } 2249 2250 /* 2251 * The L2ARC currently only supports disk devices in 2252 * kernel context. For user-level testing, we allow it. 2253 */ 2254 #ifdef _KERNEL 2255 if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) && 2256 strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) { 2257 error = ENOTBLK; 2258 goto out; 2259 } 2260 #endif 2261 vd->vdev_top = vd; 2262 2263 if ((error = vdev_open(vd)) == 0 && 2264 (error = vdev_label_init(vd, crtxg, label)) == 0) { 2265 VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, 2266 vd->vdev_guid) == 0); 2267 } 2268 2269 vdev_free(vd); 2270 2271 if (error && 2272 (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) 2273 goto out; 2274 else 2275 error = 0; 2276 } 2277 2278 out: 2279 sav->sav_pending = NULL; 2280 sav->sav_npending = 0; 2281 return (error); 2282 } 2283 2284 static int 2285 spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 2286 { 2287 int error; 2288 2289 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 2290 2291 if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, 2292 &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, 2293 VDEV_LABEL_SPARE)) != 0) { 2294 return (error); 2295 } 2296 2297 return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, 2298 &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, 2299 VDEV_LABEL_L2CACHE)); 2300 } 2301 2302 static void 2303 spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, 2304 const char *config) 2305 { 2306 int i; 2307 2308 if (sav->sav_config != NULL) { 2309 nvlist_t **olddevs; 2310 uint_t oldndevs; 2311 nvlist_t **newdevs; 2312 2313 /* 2314 * Generate new dev list by concatentating with the 2315 * current dev list. 2316 */ 2317 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, 2318 &olddevs, &oldndevs) == 0); 2319 2320 newdevs = kmem_alloc(sizeof (void *) * 2321 (ndevs + oldndevs), KM_SLEEP); 2322 for (i = 0; i < oldndevs; i++) 2323 VERIFY(nvlist_dup(olddevs[i], &newdevs[i], 2324 KM_SLEEP) == 0); 2325 for (i = 0; i < ndevs; i++) 2326 VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], 2327 KM_SLEEP) == 0); 2328 2329 VERIFY(nvlist_remove(sav->sav_config, config, 2330 DATA_TYPE_NVLIST_ARRAY) == 0); 2331 2332 VERIFY(nvlist_add_nvlist_array(sav->sav_config, 2333 config, newdevs, ndevs + oldndevs) == 0); 2334 for (i = 0; i < oldndevs + ndevs; i++) 2335 nvlist_free(newdevs[i]); 2336 kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); 2337 } else { 2338 /* 2339 * Generate a new dev list. 2340 */ 2341 VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, 2342 KM_SLEEP) == 0); 2343 VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, 2344 devs, ndevs) == 0); 2345 } 2346 } 2347 2348 /* 2349 * Stop and drop level 2 ARC devices 2350 */ 2351 void 2352 spa_l2cache_drop(spa_t *spa) 2353 { 2354 vdev_t *vd; 2355 int i; 2356 spa_aux_vdev_t *sav = &spa->spa_l2cache; 2357 2358 for (i = 0; i < sav->sav_count; i++) { 2359 uint64_t pool; 2360 2361 vd = sav->sav_vdevs[i]; 2362 ASSERT(vd != NULL); 2363 2364 if (spa_l2cache_exists(vd->vdev_guid, &pool) && 2365 pool != 0ULL && l2arc_vdev_present(vd)) 2366 l2arc_remove_vdev(vd); 2367 if (vd->vdev_isl2cache) 2368 spa_l2cache_remove(vd); 2369 vdev_clear_stats(vd); 2370 (void) vdev_close(vd); 2371 } 2372 } 2373 2374 /* 2375 * Pool Creation 2376 */ 2377 int 2378 spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, 2379 const char *history_str, nvlist_t *zplprops) 2380 { 2381 spa_t *spa; 2382 char *altroot = NULL; 2383 vdev_t *rvd; 2384 dsl_pool_t *dp; 2385 dmu_tx_t *tx; 2386 int error = 0; 2387 uint64_t txg = TXG_INITIAL; 2388 nvlist_t **spares, **l2cache; 2389 uint_t nspares, nl2cache; 2390 uint64_t version; 2391 2392 /* 2393 * If this pool already exists, return failure. 2394 */ 2395 mutex_enter(&spa_namespace_lock); 2396 if (spa_lookup(pool) != NULL) { 2397 mutex_exit(&spa_namespace_lock); 2398 return (EEXIST); 2399 } 2400 2401 /* 2402 * Allocate a new spa_t structure. 2403 */ 2404 (void) nvlist_lookup_string(props, 2405 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 2406 spa = spa_add(pool, NULL, altroot); 2407 spa_activate(spa, spa_mode_global); 2408 2409 if (props && (error = spa_prop_validate(spa, props))) { 2410 spa_deactivate(spa); 2411 spa_remove(spa); 2412 mutex_exit(&spa_namespace_lock); 2413 return (error); 2414 } 2415 2416 if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION), 2417 &version) != 0) 2418 version = SPA_VERSION; 2419 ASSERT(version <= SPA_VERSION); 2420 2421 spa->spa_first_txg = txg; 2422 spa->spa_uberblock.ub_txg = txg - 1; 2423 spa->spa_uberblock.ub_version = version; 2424 spa->spa_ubsync = spa->spa_uberblock; 2425 2426 /* 2427 * Create "The Godfather" zio to hold all async IOs 2428 */ 2429 spa->spa_async_zio_root = zio_root(spa, NULL, NULL, 2430 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER); 2431 2432 /* 2433 * Create the root vdev. 2434 */ 2435 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2436 2437 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 2438 2439 ASSERT(error != 0 || rvd != NULL); 2440 ASSERT(error != 0 || spa->spa_root_vdev == rvd); 2441 2442 if (error == 0 && !zfs_allocatable_devs(nvroot)) 2443 error = EINVAL; 2444 2445 if (error == 0 && 2446 (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 2447 (error = spa_validate_aux(spa, nvroot, txg, 2448 VDEV_ALLOC_ADD)) == 0) { 2449 for (int c = 0; c < rvd->vdev_children; c++) { 2450 vdev_metaslab_set_size(rvd->vdev_child[c]); 2451 vdev_expand(rvd->vdev_child[c], txg); 2452 } 2453 } 2454 2455 spa_config_exit(spa, SCL_ALL, FTAG); 2456 2457 if (error != 0) { 2458 spa_unload(spa); 2459 spa_deactivate(spa); 2460 spa_remove(spa); 2461 mutex_exit(&spa_namespace_lock); 2462 return (error); 2463 } 2464 2465 /* 2466 * Get the list of spares, if specified. 2467 */ 2468 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 2469 &spares, &nspares) == 0) { 2470 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, 2471 KM_SLEEP) == 0); 2472 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 2473 ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 2474 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2475 spa_load_spares(spa); 2476 spa_config_exit(spa, SCL_ALL, FTAG); 2477 spa->spa_spares.sav_sync = B_TRUE; 2478 } 2479 2480 /* 2481 * Get the list of level 2 cache devices, if specified. 2482 */ 2483 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 2484 &l2cache, &nl2cache) == 0) { 2485 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 2486 NV_UNIQUE_NAME, KM_SLEEP) == 0); 2487 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 2488 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 2489 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2490 spa_load_l2cache(spa); 2491 spa_config_exit(spa, SCL_ALL, FTAG); 2492 spa->spa_l2cache.sav_sync = B_TRUE; 2493 } 2494 2495 spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg); 2496 spa->spa_meta_objset = dp->dp_meta_objset; 2497 2498 tx = dmu_tx_create_assigned(dp, txg); 2499 2500 /* 2501 * Create the pool config object. 2502 */ 2503 spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 2504 DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, 2505 DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 2506 2507 if (zap_add(spa->spa_meta_objset, 2508 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 2509 sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 2510 cmn_err(CE_PANIC, "failed to add pool config"); 2511 } 2512 2513 /* Newly created pools with the right version are always deflated. */ 2514 if (version >= SPA_VERSION_RAIDZ_DEFLATE) { 2515 spa->spa_deflate = TRUE; 2516 if (zap_add(spa->spa_meta_objset, 2517 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 2518 sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 2519 cmn_err(CE_PANIC, "failed to add deflate"); 2520 } 2521 } 2522 2523 /* 2524 * Create the deferred-free bplist object. Turn off compression 2525 * because sync-to-convergence takes longer if the blocksize 2526 * keeps changing. 2527 */ 2528 spa->spa_deferred_bplist_obj = bplist_create(spa->spa_meta_objset, 2529 1 << 14, tx); 2530 dmu_object_set_compress(spa->spa_meta_objset, 2531 spa->spa_deferred_bplist_obj, ZIO_COMPRESS_OFF, tx); 2532 2533 if (zap_add(spa->spa_meta_objset, 2534 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST, 2535 sizeof (uint64_t), 1, &spa->spa_deferred_bplist_obj, tx) != 0) { 2536 cmn_err(CE_PANIC, "failed to add bplist"); 2537 } 2538 2539 /* 2540 * Create the pool's history object. 2541 */ 2542 if (version >= SPA_VERSION_ZPOOL_HISTORY) 2543 spa_history_create_obj(spa, tx); 2544 2545 /* 2546 * Set pool properties. 2547 */ 2548 spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 2549 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 2550 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); 2551 spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND); 2552 2553 if (props != NULL) { 2554 spa_configfile_set(spa, props, B_FALSE); 2555 spa_sync_props(spa, props, CRED(), tx); 2556 } 2557 2558 /* 2559 * Create DDTs (dedup tables). 2560 */ 2561 ddt_create(spa); 2562 2563 dmu_tx_commit(tx); 2564 2565 spa->spa_sync_on = B_TRUE; 2566 txg_sync_start(spa->spa_dsl_pool); 2567 2568 /* 2569 * We explicitly wait for the first transaction to complete so that our 2570 * bean counters are appropriately updated. 2571 */ 2572 txg_wait_synced(spa->spa_dsl_pool, txg); 2573 2574 spa_config_sync(spa, B_FALSE, B_TRUE); 2575 2576 if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL) 2577 (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE); 2578 spa_history_log_version(spa, LOG_POOL_CREATE); 2579 2580 spa->spa_minref = refcount_count(&spa->spa_refcount); 2581 2582 mutex_exit(&spa_namespace_lock); 2583 2584 return (0); 2585 } 2586 2587 #ifdef _KERNEL 2588 /* 2589 * Get the root pool information from the root disk, then import the root pool 2590 * during the system boot up time. 2591 */ 2592 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **); 2593 2594 static nvlist_t * 2595 spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid) 2596 { 2597 nvlist_t *config; 2598 nvlist_t *nvtop, *nvroot; 2599 uint64_t pgid; 2600 2601 if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0) 2602 return (NULL); 2603 2604 /* 2605 * Add this top-level vdev to the child array. 2606 */ 2607 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 2608 &nvtop) == 0); 2609 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 2610 &pgid) == 0); 2611 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0); 2612 2613 /* 2614 * Put this pool's top-level vdevs into a root vdev. 2615 */ 2616 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2617 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, 2618 VDEV_TYPE_ROOT) == 0); 2619 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0); 2620 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0); 2621 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 2622 &nvtop, 1) == 0); 2623 2624 /* 2625 * Replace the existing vdev_tree with the new root vdev in 2626 * this pool's configuration (remove the old, add the new). 2627 */ 2628 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 2629 nvlist_free(nvroot); 2630 return (config); 2631 } 2632 2633 /* 2634 * Walk the vdev tree and see if we can find a device with "better" 2635 * configuration. A configuration is "better" if the label on that 2636 * device has a more recent txg. 2637 */ 2638 static void 2639 spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg) 2640 { 2641 for (int c = 0; c < vd->vdev_children; c++) 2642 spa_alt_rootvdev(vd->vdev_child[c], avd, txg); 2643 2644 if (vd->vdev_ops->vdev_op_leaf) { 2645 nvlist_t *label; 2646 uint64_t label_txg; 2647 2648 if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid, 2649 &label) != 0) 2650 return; 2651 2652 VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG, 2653 &label_txg) == 0); 2654 2655 /* 2656 * Do we have a better boot device? 2657 */ 2658 if (label_txg > *txg) { 2659 *txg = label_txg; 2660 *avd = vd; 2661 } 2662 nvlist_free(label); 2663 } 2664 } 2665 2666 /* 2667 * Import a root pool. 2668 * 2669 * For x86. devpath_list will consist of devid and/or physpath name of 2670 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a"). 2671 * The GRUB "findroot" command will return the vdev we should boot. 2672 * 2673 * For Sparc, devpath_list consists the physpath name of the booting device 2674 * no matter the rootpool is a single device pool or a mirrored pool. 2675 * e.g. 2676 * "/pci@1f,0/ide@d/disk@0,0:a" 2677 */ 2678 int 2679 spa_import_rootpool(char *devpath, char *devid) 2680 { 2681 spa_t *spa; 2682 vdev_t *rvd, *bvd, *avd = NULL; 2683 nvlist_t *config, *nvtop; 2684 uint64_t guid, txg; 2685 char *pname; 2686 int error; 2687 2688 /* 2689 * Read the label from the boot device and generate a configuration. 2690 */ 2691 config = spa_generate_rootconf(devpath, devid, &guid); 2692 #if defined(_OBP) && defined(_KERNEL) 2693 if (config == NULL) { 2694 if (strstr(devpath, "/iscsi/ssd") != NULL) { 2695 /* iscsi boot */ 2696 get_iscsi_bootpath_phy(devpath); 2697 config = spa_generate_rootconf(devpath, devid, &guid); 2698 } 2699 } 2700 #endif 2701 if (config == NULL) { 2702 cmn_err(CE_NOTE, "Can not read the pool label from '%s'", 2703 devpath); 2704 return (EIO); 2705 } 2706 2707 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 2708 &pname) == 0); 2709 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0); 2710 2711 mutex_enter(&spa_namespace_lock); 2712 if ((spa = spa_lookup(pname)) != NULL) { 2713 /* 2714 * Remove the existing root pool from the namespace so that we 2715 * can replace it with the correct config we just read in. 2716 */ 2717 spa_remove(spa); 2718 } 2719 2720 spa = spa_add(pname, config, NULL); 2721 spa->spa_is_root = B_TRUE; 2722 spa->spa_load_verbatim = B_TRUE; 2723 2724 /* 2725 * Build up a vdev tree based on the boot device's label config. 2726 */ 2727 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 2728 &nvtop) == 0); 2729 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2730 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0, 2731 VDEV_ALLOC_ROOTPOOL); 2732 spa_config_exit(spa, SCL_ALL, FTAG); 2733 if (error) { 2734 mutex_exit(&spa_namespace_lock); 2735 nvlist_free(config); 2736 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'", 2737 pname); 2738 return (error); 2739 } 2740 2741 /* 2742 * Get the boot vdev. 2743 */ 2744 if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) { 2745 cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu", 2746 (u_longlong_t)guid); 2747 error = ENOENT; 2748 goto out; 2749 } 2750 2751 /* 2752 * Determine if there is a better boot device. 2753 */ 2754 avd = bvd; 2755 spa_alt_rootvdev(rvd, &avd, &txg); 2756 if (avd != bvd) { 2757 cmn_err(CE_NOTE, "The boot device is 'degraded'. Please " 2758 "try booting from '%s'", avd->vdev_path); 2759 error = EINVAL; 2760 goto out; 2761 } 2762 2763 /* 2764 * If the boot device is part of a spare vdev then ensure that 2765 * we're booting off the active spare. 2766 */ 2767 if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops && 2768 !bvd->vdev_isspare) { 2769 cmn_err(CE_NOTE, "The boot device is currently spared. Please " 2770 "try booting from '%s'", 2771 bvd->vdev_parent->vdev_child[1]->vdev_path); 2772 error = EINVAL; 2773 goto out; 2774 } 2775 2776 error = 0; 2777 spa_history_log_version(spa, LOG_POOL_IMPORT); 2778 out: 2779 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2780 vdev_free(rvd); 2781 spa_config_exit(spa, SCL_ALL, FTAG); 2782 mutex_exit(&spa_namespace_lock); 2783 2784 nvlist_free(config); 2785 return (error); 2786 } 2787 2788 #endif 2789 2790 /* 2791 * Take a pool and insert it into the namespace as if it had been loaded at 2792 * boot. 2793 */ 2794 int 2795 spa_import_verbatim(const char *pool, nvlist_t *config, nvlist_t *props) 2796 { 2797 spa_t *spa; 2798 zpool_rewind_policy_t policy; 2799 char *altroot = NULL; 2800 2801 mutex_enter(&spa_namespace_lock); 2802 if (spa_lookup(pool) != NULL) { 2803 mutex_exit(&spa_namespace_lock); 2804 return (EEXIST); 2805 } 2806 2807 (void) nvlist_lookup_string(props, 2808 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 2809 spa = spa_add(pool, config, altroot); 2810 2811 zpool_get_rewind_policy(config, &policy); 2812 spa->spa_load_max_txg = policy.zrp_txg; 2813 2814 spa->spa_load_verbatim = B_TRUE; 2815 2816 if (props != NULL) 2817 spa_configfile_set(spa, props, B_FALSE); 2818 2819 spa_config_sync(spa, B_FALSE, B_TRUE); 2820 2821 mutex_exit(&spa_namespace_lock); 2822 spa_history_log_version(spa, LOG_POOL_IMPORT); 2823 2824 return (0); 2825 } 2826 2827 /* 2828 * Import a non-root pool into the system. 2829 */ 2830 int 2831 spa_import(const char *pool, nvlist_t *config, nvlist_t *props) 2832 { 2833 spa_t *spa; 2834 char *altroot = NULL; 2835 spa_load_state_t state = SPA_LOAD_IMPORT; 2836 zpool_rewind_policy_t policy; 2837 int error; 2838 nvlist_t *nvroot; 2839 nvlist_t **spares, **l2cache; 2840 uint_t nspares, nl2cache; 2841 2842 /* 2843 * If a pool with this name exists, return failure. 2844 */ 2845 mutex_enter(&spa_namespace_lock); 2846 if ((spa = spa_lookup(pool)) != NULL) { 2847 mutex_exit(&spa_namespace_lock); 2848 return (EEXIST); 2849 } 2850 2851 zpool_get_rewind_policy(config, &policy); 2852 if (policy.zrp_request & ZPOOL_DO_REWIND) 2853 state = SPA_LOAD_RECOVER; 2854 2855 /* 2856 * Create and initialize the spa structure. 2857 */ 2858 (void) nvlist_lookup_string(props, 2859 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 2860 spa = spa_add(pool, config, altroot); 2861 spa_activate(spa, spa_mode_global); 2862 2863 /* 2864 * Don't start async tasks until we know everything is healthy. 2865 */ 2866 spa_async_suspend(spa); 2867 2868 /* 2869 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig 2870 * because the user-supplied config is actually the one to trust when 2871 * doing an import. 2872 */ 2873 if (state != SPA_LOAD_RECOVER) 2874 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 2875 error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg, 2876 ((policy.zrp_request & ZPOOL_EXTREME_REWIND) != 0)); 2877 2878 /* 2879 * Propagate anything learned about failing or best txgs 2880 * back to caller 2881 */ 2882 spa_rewind_data_to_nvlist(spa, config); 2883 2884 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2885 /* 2886 * Toss any existing sparelist, as it doesn't have any validity 2887 * anymore, and conflicts with spa_has_spare(). 2888 */ 2889 if (spa->spa_spares.sav_config) { 2890 nvlist_free(spa->spa_spares.sav_config); 2891 spa->spa_spares.sav_config = NULL; 2892 spa_load_spares(spa); 2893 } 2894 if (spa->spa_l2cache.sav_config) { 2895 nvlist_free(spa->spa_l2cache.sav_config); 2896 spa->spa_l2cache.sav_config = NULL; 2897 spa_load_l2cache(spa); 2898 } 2899 2900 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 2901 &nvroot) == 0); 2902 if (error == 0) 2903 error = spa_validate_aux(spa, nvroot, -1ULL, 2904 VDEV_ALLOC_SPARE); 2905 if (error == 0) 2906 error = spa_validate_aux(spa, nvroot, -1ULL, 2907 VDEV_ALLOC_L2CACHE); 2908 spa_config_exit(spa, SCL_ALL, FTAG); 2909 2910 if (props != NULL) 2911 spa_configfile_set(spa, props, B_FALSE); 2912 2913 if (error != 0 || (props && spa_writeable(spa) && 2914 (error = spa_prop_set(spa, props)))) { 2915 spa_unload(spa); 2916 spa_deactivate(spa); 2917 spa_remove(spa); 2918 mutex_exit(&spa_namespace_lock); 2919 return (error); 2920 } 2921 2922 spa_async_resume(spa); 2923 2924 /* 2925 * Override any spares and level 2 cache devices as specified by 2926 * the user, as these may have correct device names/devids, etc. 2927 */ 2928 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 2929 &spares, &nspares) == 0) { 2930 if (spa->spa_spares.sav_config) 2931 VERIFY(nvlist_remove(spa->spa_spares.sav_config, 2932 ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 2933 else 2934 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, 2935 NV_UNIQUE_NAME, KM_SLEEP) == 0); 2936 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 2937 ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 2938 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2939 spa_load_spares(spa); 2940 spa_config_exit(spa, SCL_ALL, FTAG); 2941 spa->spa_spares.sav_sync = B_TRUE; 2942 } 2943 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 2944 &l2cache, &nl2cache) == 0) { 2945 if (spa->spa_l2cache.sav_config) 2946 VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, 2947 ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); 2948 else 2949 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 2950 NV_UNIQUE_NAME, KM_SLEEP) == 0); 2951 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 2952 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 2953 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2954 spa_load_l2cache(spa); 2955 spa_config_exit(spa, SCL_ALL, FTAG); 2956 spa->spa_l2cache.sav_sync = B_TRUE; 2957 } 2958 2959 /* 2960 * Check for any removed devices. 2961 */ 2962 if (spa->spa_autoreplace) { 2963 spa_aux_check_removed(&spa->spa_spares); 2964 spa_aux_check_removed(&spa->spa_l2cache); 2965 } 2966 2967 if (spa_writeable(spa)) { 2968 /* 2969 * Update the config cache to include the newly-imported pool. 2970 */ 2971 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 2972 } 2973 2974 /* 2975 * It's possible that the pool was expanded while it was exported. 2976 * We kick off an async task to handle this for us. 2977 */ 2978 spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); 2979 2980 mutex_exit(&spa_namespace_lock); 2981 spa_history_log_version(spa, LOG_POOL_IMPORT); 2982 2983 return (0); 2984 } 2985 2986 2987 /* 2988 * This (illegal) pool name is used when temporarily importing a spa_t in order 2989 * to get the vdev stats associated with the imported devices. 2990 */ 2991 #define TRYIMPORT_NAME "$import" 2992 2993 nvlist_t * 2994 spa_tryimport(nvlist_t *tryconfig) 2995 { 2996 nvlist_t *config = NULL; 2997 char *poolname; 2998 spa_t *spa; 2999 uint64_t state; 3000 int error; 3001 3002 if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 3003 return (NULL); 3004 3005 if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 3006 return (NULL); 3007 3008 /* 3009 * Create and initialize the spa structure. 3010 */ 3011 mutex_enter(&spa_namespace_lock); 3012 spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL); 3013 spa_activate(spa, FREAD); 3014 3015 /* 3016 * Pass off the heavy lifting to spa_load(). 3017 * Pass TRUE for mosconfig because the user-supplied config 3018 * is actually the one to trust when doing an import. 3019 */ 3020 error = spa_load(spa, SPA_LOAD_TRYIMPORT, B_TRUE); 3021 3022 /* 3023 * If 'tryconfig' was at least parsable, return the current config. 3024 */ 3025 if (spa->spa_root_vdev != NULL) { 3026 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 3027 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 3028 poolname) == 0); 3029 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 3030 state) == 0); 3031 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 3032 spa->spa_uberblock.ub_timestamp) == 0); 3033 3034 /* 3035 * If the bootfs property exists on this pool then we 3036 * copy it out so that external consumers can tell which 3037 * pools are bootable. 3038 */ 3039 if ((!error || error == EEXIST) && spa->spa_bootfs) { 3040 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 3041 3042 /* 3043 * We have to play games with the name since the 3044 * pool was opened as TRYIMPORT_NAME. 3045 */ 3046 if (dsl_dsobj_to_dsname(spa_name(spa), 3047 spa->spa_bootfs, tmpname) == 0) { 3048 char *cp; 3049 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 3050 3051 cp = strchr(tmpname, '/'); 3052 if (cp == NULL) { 3053 (void) strlcpy(dsname, tmpname, 3054 MAXPATHLEN); 3055 } else { 3056 (void) snprintf(dsname, MAXPATHLEN, 3057 "%s/%s", poolname, ++cp); 3058 } 3059 VERIFY(nvlist_add_string(config, 3060 ZPOOL_CONFIG_BOOTFS, dsname) == 0); 3061 kmem_free(dsname, MAXPATHLEN); 3062 } 3063 kmem_free(tmpname, MAXPATHLEN); 3064 } 3065 3066 /* 3067 * Add the list of hot spares and level 2 cache devices. 3068 */ 3069 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 3070 spa_add_spares(spa, config); 3071 spa_add_l2cache(spa, config); 3072 spa_config_exit(spa, SCL_CONFIG, FTAG); 3073 } 3074 3075 spa_unload(spa); 3076 spa_deactivate(spa); 3077 spa_remove(spa); 3078 mutex_exit(&spa_namespace_lock); 3079 3080 return (config); 3081 } 3082 3083 /* 3084 * Pool export/destroy 3085 * 3086 * The act of destroying or exporting a pool is very simple. We make sure there 3087 * is no more pending I/O and any references to the pool are gone. Then, we 3088 * update the pool state and sync all the labels to disk, removing the 3089 * configuration from the cache afterwards. If the 'hardforce' flag is set, then 3090 * we don't sync the labels or remove the configuration cache. 3091 */ 3092 static int 3093 spa_export_common(char *pool, int new_state, nvlist_t **oldconfig, 3094 boolean_t force, boolean_t hardforce) 3095 { 3096 spa_t *spa; 3097 3098 if (oldconfig) 3099 *oldconfig = NULL; 3100 3101 if (!(spa_mode_global & FWRITE)) 3102 return (EROFS); 3103 3104 mutex_enter(&spa_namespace_lock); 3105 if ((spa = spa_lookup(pool)) == NULL) { 3106 mutex_exit(&spa_namespace_lock); 3107 return (ENOENT); 3108 } 3109 3110 /* 3111 * Put a hold on the pool, drop the namespace lock, stop async tasks, 3112 * reacquire the namespace lock, and see if we can export. 3113 */ 3114 spa_open_ref(spa, FTAG); 3115 mutex_exit(&spa_namespace_lock); 3116 spa_async_suspend(spa); 3117 mutex_enter(&spa_namespace_lock); 3118 spa_close(spa, FTAG); 3119 3120 /* 3121 * The pool will be in core if it's openable, 3122 * in which case we can modify its state. 3123 */ 3124 if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 3125 /* 3126 * Objsets may be open only because they're dirty, so we 3127 * have to force it to sync before checking spa_refcnt. 3128 */ 3129 txg_wait_synced(spa->spa_dsl_pool, 0); 3130 3131 /* 3132 * A pool cannot be exported or destroyed if there are active 3133 * references. If we are resetting a pool, allow references by 3134 * fault injection handlers. 3135 */ 3136 if (!spa_refcount_zero(spa) || 3137 (spa->spa_inject_ref != 0 && 3138 new_state != POOL_STATE_UNINITIALIZED)) { 3139 spa_async_resume(spa); 3140 mutex_exit(&spa_namespace_lock); 3141 return (EBUSY); 3142 } 3143 3144 /* 3145 * A pool cannot be exported if it has an active shared spare. 3146 * This is to prevent other pools stealing the active spare 3147 * from an exported pool. At user's own will, such pool can 3148 * be forcedly exported. 3149 */ 3150 if (!force && new_state == POOL_STATE_EXPORTED && 3151 spa_has_active_shared_spare(spa)) { 3152 spa_async_resume(spa); 3153 mutex_exit(&spa_namespace_lock); 3154 return (EXDEV); 3155 } 3156 3157 /* 3158 * We want this to be reflected on every label, 3159 * so mark them all dirty. spa_unload() will do the 3160 * final sync that pushes these changes out. 3161 */ 3162 if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { 3163 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 3164 spa->spa_state = new_state; 3165 spa->spa_final_txg = spa_last_synced_txg(spa) + 1; 3166 vdev_config_dirty(spa->spa_root_vdev); 3167 spa_config_exit(spa, SCL_ALL, FTAG); 3168 } 3169 } 3170 3171 spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY); 3172 3173 if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 3174 spa_unload(spa); 3175 spa_deactivate(spa); 3176 } 3177 3178 if (oldconfig && spa->spa_config) 3179 VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 3180 3181 if (new_state != POOL_STATE_UNINITIALIZED) { 3182 if (!hardforce) 3183 spa_config_sync(spa, B_TRUE, B_TRUE); 3184 spa_remove(spa); 3185 } 3186 mutex_exit(&spa_namespace_lock); 3187 3188 return (0); 3189 } 3190 3191 /* 3192 * Destroy a storage pool. 3193 */ 3194 int 3195 spa_destroy(char *pool) 3196 { 3197 return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, 3198 B_FALSE, B_FALSE)); 3199 } 3200 3201 /* 3202 * Export a storage pool. 3203 */ 3204 int 3205 spa_export(char *pool, nvlist_t **oldconfig, boolean_t force, 3206 boolean_t hardforce) 3207 { 3208 return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, 3209 force, hardforce)); 3210 } 3211 3212 /* 3213 * Similar to spa_export(), this unloads the spa_t without actually removing it 3214 * from the namespace in any way. 3215 */ 3216 int 3217 spa_reset(char *pool) 3218 { 3219 return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, 3220 B_FALSE, B_FALSE)); 3221 } 3222 3223 /* 3224 * ========================================================================== 3225 * Device manipulation 3226 * ========================================================================== 3227 */ 3228 3229 /* 3230 * Add a device to a storage pool. 3231 */ 3232 int 3233 spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 3234 { 3235 uint64_t txg, id; 3236 int error; 3237 vdev_t *rvd = spa->spa_root_vdev; 3238 vdev_t *vd, *tvd; 3239 nvlist_t **spares, **l2cache; 3240 uint_t nspares, nl2cache; 3241 3242 txg = spa_vdev_enter(spa); 3243 3244 if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 3245 VDEV_ALLOC_ADD)) != 0) 3246 return (spa_vdev_exit(spa, NULL, txg, error)); 3247 3248 spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */ 3249 3250 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, 3251 &nspares) != 0) 3252 nspares = 0; 3253 3254 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, 3255 &nl2cache) != 0) 3256 nl2cache = 0; 3257 3258 if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) 3259 return (spa_vdev_exit(spa, vd, txg, EINVAL)); 3260 3261 if (vd->vdev_children != 0 && 3262 (error = vdev_create(vd, txg, B_FALSE)) != 0) 3263 return (spa_vdev_exit(spa, vd, txg, error)); 3264 3265 /* 3266 * We must validate the spares and l2cache devices after checking the 3267 * children. Otherwise, vdev_inuse() will blindly overwrite the spare. 3268 */ 3269 if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) 3270 return (spa_vdev_exit(spa, vd, txg, error)); 3271 3272 /* 3273 * Transfer each new top-level vdev from vd to rvd. 3274 */ 3275 for (int c = 0; c < vd->vdev_children; c++) { 3276 3277 /* 3278 * Set the vdev id to the first hole, if one exists. 3279 */ 3280 for (id = 0; id < rvd->vdev_children; id++) { 3281 if (rvd->vdev_child[id]->vdev_ishole) { 3282 vdev_free(rvd->vdev_child[id]); 3283 break; 3284 } 3285 } 3286 tvd = vd->vdev_child[c]; 3287 vdev_remove_child(vd, tvd); 3288 tvd->vdev_id = id; 3289 vdev_add_child(rvd, tvd); 3290 vdev_config_dirty(tvd); 3291 } 3292 3293 if (nspares != 0) { 3294 spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, 3295 ZPOOL_CONFIG_SPARES); 3296 spa_load_spares(spa); 3297 spa->spa_spares.sav_sync = B_TRUE; 3298 } 3299 3300 if (nl2cache != 0) { 3301 spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, 3302 ZPOOL_CONFIG_L2CACHE); 3303 spa_load_l2cache(spa); 3304 spa->spa_l2cache.sav_sync = B_TRUE; 3305 } 3306 3307 /* 3308 * We have to be careful when adding new vdevs to an existing pool. 3309 * If other threads start allocating from these vdevs before we 3310 * sync the config cache, and we lose power, then upon reboot we may 3311 * fail to open the pool because there are DVAs that the config cache 3312 * can't translate. Therefore, we first add the vdevs without 3313 * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 3314 * and then let spa_config_update() initialize the new metaslabs. 3315 * 3316 * spa_load() checks for added-but-not-initialized vdevs, so that 3317 * if we lose power at any point in this sequence, the remaining 3318 * steps will be completed the next time we load the pool. 3319 */ 3320 (void) spa_vdev_exit(spa, vd, txg, 0); 3321 3322 mutex_enter(&spa_namespace_lock); 3323 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 3324 mutex_exit(&spa_namespace_lock); 3325 3326 return (0); 3327 } 3328 3329 /* 3330 * Attach a device to a mirror. The arguments are the path to any device 3331 * in the mirror, and the nvroot for the new device. If the path specifies 3332 * a device that is not mirrored, we automatically insert the mirror vdev. 3333 * 3334 * If 'replacing' is specified, the new device is intended to replace the 3335 * existing device; in this case the two devices are made into their own 3336 * mirror using the 'replacing' vdev, which is functionally identical to 3337 * the mirror vdev (it actually reuses all the same ops) but has a few 3338 * extra rules: you can't attach to it after it's been created, and upon 3339 * completion of resilvering, the first disk (the one being replaced) 3340 * is automatically detached. 3341 */ 3342 int 3343 spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 3344 { 3345 uint64_t txg, open_txg; 3346 vdev_t *rvd = spa->spa_root_vdev; 3347 vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 3348 vdev_ops_t *pvops; 3349 char *oldvdpath, *newvdpath; 3350 int newvd_isspare; 3351 int error; 3352 3353 txg = spa_vdev_enter(spa); 3354 3355 oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); 3356 3357 if (oldvd == NULL) 3358 return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 3359 3360 if (!oldvd->vdev_ops->vdev_op_leaf) 3361 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 3362 3363 pvd = oldvd->vdev_parent; 3364 3365 if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 3366 VDEV_ALLOC_ADD)) != 0) 3367 return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 3368 3369 if (newrootvd->vdev_children != 1) 3370 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 3371 3372 newvd = newrootvd->vdev_child[0]; 3373 3374 if (!newvd->vdev_ops->vdev_op_leaf) 3375 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 3376 3377 if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 3378 return (spa_vdev_exit(spa, newrootvd, txg, error)); 3379 3380 /* 3381 * Spares can't replace logs 3382 */ 3383 if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare) 3384 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 3385 3386 if (!replacing) { 3387 /* 3388 * For attach, the only allowable parent is a mirror or the root 3389 * vdev. 3390 */ 3391 if (pvd->vdev_ops != &vdev_mirror_ops && 3392 pvd->vdev_ops != &vdev_root_ops) 3393 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 3394 3395 pvops = &vdev_mirror_ops; 3396 } else { 3397 /* 3398 * Active hot spares can only be replaced by inactive hot 3399 * spares. 3400 */ 3401 if (pvd->vdev_ops == &vdev_spare_ops && 3402 pvd->vdev_child[1] == oldvd && 3403 !spa_has_spare(spa, newvd->vdev_guid)) 3404 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 3405 3406 /* 3407 * If the source is a hot spare, and the parent isn't already a 3408 * spare, then we want to create a new hot spare. Otherwise, we 3409 * want to create a replacing vdev. The user is not allowed to 3410 * attach to a spared vdev child unless the 'isspare' state is 3411 * the same (spare replaces spare, non-spare replaces 3412 * non-spare). 3413 */ 3414 if (pvd->vdev_ops == &vdev_replacing_ops) 3415 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 3416 else if (pvd->vdev_ops == &vdev_spare_ops && 3417 newvd->vdev_isspare != oldvd->vdev_isspare) 3418 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 3419 else if (pvd->vdev_ops != &vdev_spare_ops && 3420 newvd->vdev_isspare) 3421 pvops = &vdev_spare_ops; 3422 else 3423 pvops = &vdev_replacing_ops; 3424 } 3425 3426 /* 3427 * Make sure the new device is big enough. 3428 */ 3429 if (newvd->vdev_asize < vdev_get_min_asize(oldvd)) 3430 return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 3431 3432 /* 3433 * The new device cannot have a higher alignment requirement 3434 * than the top-level vdev. 3435 */ 3436 if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 3437 return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 3438 3439 /* 3440 * If this is an in-place replacement, update oldvd's path and devid 3441 * to make it distinguishable from newvd, and unopenable from now on. 3442 */ 3443 if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 3444 spa_strfree(oldvd->vdev_path); 3445 oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 3446 KM_SLEEP); 3447 (void) sprintf(oldvd->vdev_path, "%s/%s", 3448 newvd->vdev_path, "old"); 3449 if (oldvd->vdev_devid != NULL) { 3450 spa_strfree(oldvd->vdev_devid); 3451 oldvd->vdev_devid = NULL; 3452 } 3453 } 3454 3455 /* 3456 * If the parent is not a mirror, or if we're replacing, insert the new 3457 * mirror/replacing/spare vdev above oldvd. 3458 */ 3459 if (pvd->vdev_ops != pvops) 3460 pvd = vdev_add_parent(oldvd, pvops); 3461 3462 ASSERT(pvd->vdev_top->vdev_parent == rvd); 3463 ASSERT(pvd->vdev_ops == pvops); 3464 ASSERT(oldvd->vdev_parent == pvd); 3465 3466 /* 3467 * Extract the new device from its root and add it to pvd. 3468 */ 3469 vdev_remove_child(newrootvd, newvd); 3470 newvd->vdev_id = pvd->vdev_children; 3471 newvd->vdev_crtxg = oldvd->vdev_crtxg; 3472 vdev_add_child(pvd, newvd); 3473 3474 tvd = newvd->vdev_top; 3475 ASSERT(pvd->vdev_top == tvd); 3476 ASSERT(tvd->vdev_parent == rvd); 3477 3478 vdev_config_dirty(tvd); 3479 3480 /* 3481 * Set newvd's DTL to [TXG_INITIAL, open_txg]. It will propagate 3482 * upward when spa_vdev_exit() calls vdev_dtl_reassess(). 3483 */ 3484 open_txg = txg + TXG_CONCURRENT_STATES - 1; 3485 3486 vdev_dtl_dirty(newvd, DTL_MISSING, 3487 TXG_INITIAL, open_txg - TXG_INITIAL + 1); 3488 3489 if (newvd->vdev_isspare) { 3490 spa_spare_activate(newvd); 3491 spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE); 3492 } 3493 3494 oldvdpath = spa_strdup(oldvd->vdev_path); 3495 newvdpath = spa_strdup(newvd->vdev_path); 3496 newvd_isspare = newvd->vdev_isspare; 3497 3498 /* 3499 * Mark newvd's DTL dirty in this txg. 3500 */ 3501 vdev_dirty(tvd, VDD_DTL, newvd, txg); 3502 3503 (void) spa_vdev_exit(spa, newrootvd, open_txg, 0); 3504 3505 spa_history_internal_log(LOG_POOL_VDEV_ATTACH, spa, NULL, 3506 CRED(), "%s vdev=%s %s vdev=%s", 3507 replacing && newvd_isspare ? "spare in" : 3508 replacing ? "replace" : "attach", newvdpath, 3509 replacing ? "for" : "to", oldvdpath); 3510 3511 spa_strfree(oldvdpath); 3512 spa_strfree(newvdpath); 3513 3514 /* 3515 * Kick off a resilver to update newvd. 3516 */ 3517 VERIFY3U(spa_scrub(spa, POOL_SCRUB_RESILVER), ==, 0); 3518 3519 return (0); 3520 } 3521 3522 /* 3523 * Detach a device from a mirror or replacing vdev. 3524 * If 'replace_done' is specified, only detach if the parent 3525 * is a replacing vdev. 3526 */ 3527 int 3528 spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done) 3529 { 3530 uint64_t txg; 3531 int error; 3532 vdev_t *rvd = spa->spa_root_vdev; 3533 vdev_t *vd, *pvd, *cvd, *tvd; 3534 boolean_t unspare = B_FALSE; 3535 uint64_t unspare_guid; 3536 size_t len; 3537 3538 txg = spa_vdev_enter(spa); 3539 3540 vd = spa_lookup_by_guid(spa, guid, B_FALSE); 3541 3542 if (vd == NULL) 3543 return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 3544 3545 if (!vd->vdev_ops->vdev_op_leaf) 3546 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 3547 3548 pvd = vd->vdev_parent; 3549 3550 /* 3551 * If the parent/child relationship is not as expected, don't do it. 3552 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing 3553 * vdev that's replacing B with C. The user's intent in replacing 3554 * is to go from M(A,B) to M(A,C). If the user decides to cancel 3555 * the replace by detaching C, the expected behavior is to end up 3556 * M(A,B). But suppose that right after deciding to detach C, 3557 * the replacement of B completes. We would have M(A,C), and then 3558 * ask to detach C, which would leave us with just A -- not what 3559 * the user wanted. To prevent this, we make sure that the 3560 * parent/child relationship hasn't changed -- in this example, 3561 * that C's parent is still the replacing vdev R. 3562 */ 3563 if (pvd->vdev_guid != pguid && pguid != 0) 3564 return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 3565 3566 /* 3567 * If replace_done is specified, only remove this device if it's 3568 * the first child of a replacing vdev. For the 'spare' vdev, either 3569 * disk can be removed. 3570 */ 3571 if (replace_done) { 3572 if (pvd->vdev_ops == &vdev_replacing_ops) { 3573 if (vd->vdev_id != 0) 3574 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 3575 } else if (pvd->vdev_ops != &vdev_spare_ops) { 3576 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 3577 } 3578 } 3579 3580 ASSERT(pvd->vdev_ops != &vdev_spare_ops || 3581 spa_version(spa) >= SPA_VERSION_SPARES); 3582 3583 /* 3584 * Only mirror, replacing, and spare vdevs support detach. 3585 */ 3586 if (pvd->vdev_ops != &vdev_replacing_ops && 3587 pvd->vdev_ops != &vdev_mirror_ops && 3588 pvd->vdev_ops != &vdev_spare_ops) 3589 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 3590 3591 /* 3592 * If this device has the only valid copy of some data, 3593 * we cannot safely detach it. 3594 */ 3595 if (vdev_dtl_required(vd)) 3596 return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 3597 3598 ASSERT(pvd->vdev_children >= 2); 3599 3600 /* 3601 * If we are detaching the second disk from a replacing vdev, then 3602 * check to see if we changed the original vdev's path to have "/old" 3603 * at the end in spa_vdev_attach(). If so, undo that change now. 3604 */ 3605 if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 && 3606 pvd->vdev_child[0]->vdev_path != NULL && 3607 pvd->vdev_child[1]->vdev_path != NULL) { 3608 ASSERT(pvd->vdev_child[1] == vd); 3609 cvd = pvd->vdev_child[0]; 3610 len = strlen(vd->vdev_path); 3611 if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && 3612 strcmp(cvd->vdev_path + len, "/old") == 0) { 3613 spa_strfree(cvd->vdev_path); 3614 cvd->vdev_path = spa_strdup(vd->vdev_path); 3615 } 3616 } 3617 3618 /* 3619 * If we are detaching the original disk from a spare, then it implies 3620 * that the spare should become a real disk, and be removed from the 3621 * active spare list for the pool. 3622 */ 3623 if (pvd->vdev_ops == &vdev_spare_ops && 3624 vd->vdev_id == 0 && pvd->vdev_child[1]->vdev_isspare) 3625 unspare = B_TRUE; 3626 3627 /* 3628 * Erase the disk labels so the disk can be used for other things. 3629 * This must be done after all other error cases are handled, 3630 * but before we disembowel vd (so we can still do I/O to it). 3631 * But if we can't do it, don't treat the error as fatal -- 3632 * it may be that the unwritability of the disk is the reason 3633 * it's being detached! 3634 */ 3635 error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 3636 3637 /* 3638 * Remove vd from its parent and compact the parent's children. 3639 */ 3640 vdev_remove_child(pvd, vd); 3641 vdev_compact_children(pvd); 3642 3643 /* 3644 * Remember one of the remaining children so we can get tvd below. 3645 */ 3646 cvd = pvd->vdev_child[0]; 3647 3648 /* 3649 * If we need to remove the remaining child from the list of hot spares, 3650 * do it now, marking the vdev as no longer a spare in the process. 3651 * We must do this before vdev_remove_parent(), because that can 3652 * change the GUID if it creates a new toplevel GUID. For a similar 3653 * reason, we must remove the spare now, in the same txg as the detach; 3654 * otherwise someone could attach a new sibling, change the GUID, and 3655 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail. 3656 */ 3657 if (unspare) { 3658 ASSERT(cvd->vdev_isspare); 3659 spa_spare_remove(cvd); 3660 unspare_guid = cvd->vdev_guid; 3661 (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 3662 } 3663 3664 /* 3665 * If the parent mirror/replacing vdev only has one child, 3666 * the parent is no longer needed. Remove it from the tree. 3667 */ 3668 if (pvd->vdev_children == 1) 3669 vdev_remove_parent(cvd); 3670 3671 /* 3672 * We don't set tvd until now because the parent we just removed 3673 * may have been the previous top-level vdev. 3674 */ 3675 tvd = cvd->vdev_top; 3676 ASSERT(tvd->vdev_parent == rvd); 3677 3678 /* 3679 * Reevaluate the parent vdev state. 3680 */ 3681 vdev_propagate_state(cvd); 3682 3683 /* 3684 * If the 'autoexpand' property is set on the pool then automatically 3685 * try to expand the size of the pool. For example if the device we 3686 * just detached was smaller than the others, it may be possible to 3687 * add metaslabs (i.e. grow the pool). We need to reopen the vdev 3688 * first so that we can obtain the updated sizes of the leaf vdevs. 3689 */ 3690 if (spa->spa_autoexpand) { 3691 vdev_reopen(tvd); 3692 vdev_expand(tvd, txg); 3693 } 3694 3695 vdev_config_dirty(tvd); 3696 3697 /* 3698 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 3699 * vd->vdev_detached is set and free vd's DTL object in syncing context. 3700 * But first make sure we're not on any *other* txg's DTL list, to 3701 * prevent vd from being accessed after it's freed. 3702 */ 3703 for (int t = 0; t < TXG_SIZE; t++) 3704 (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 3705 vd->vdev_detached = B_TRUE; 3706 vdev_dirty(tvd, VDD_DTL, vd, txg); 3707 3708 spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE); 3709 3710 error = spa_vdev_exit(spa, vd, txg, 0); 3711 3712 /* 3713 * If this was the removal of the original device in a hot spare vdev, 3714 * then we want to go through and remove the device from the hot spare 3715 * list of every other pool. 3716 */ 3717 if (unspare) { 3718 spa_t *myspa = spa; 3719 spa = NULL; 3720 mutex_enter(&spa_namespace_lock); 3721 while ((spa = spa_next(spa)) != NULL) { 3722 if (spa->spa_state != POOL_STATE_ACTIVE) 3723 continue; 3724 if (spa == myspa) 3725 continue; 3726 spa_open_ref(spa, FTAG); 3727 mutex_exit(&spa_namespace_lock); 3728 (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 3729 mutex_enter(&spa_namespace_lock); 3730 spa_close(spa, FTAG); 3731 } 3732 mutex_exit(&spa_namespace_lock); 3733 } 3734 3735 return (error); 3736 } 3737 3738 static nvlist_t * 3739 spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid) 3740 { 3741 for (int i = 0; i < count; i++) { 3742 uint64_t guid; 3743 3744 VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID, 3745 &guid) == 0); 3746 3747 if (guid == target_guid) 3748 return (nvpp[i]); 3749 } 3750 3751 return (NULL); 3752 } 3753 3754 static void 3755 spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count, 3756 nvlist_t *dev_to_remove) 3757 { 3758 nvlist_t **newdev = NULL; 3759 3760 if (count > 1) 3761 newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP); 3762 3763 for (int i = 0, j = 0; i < count; i++) { 3764 if (dev[i] == dev_to_remove) 3765 continue; 3766 VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0); 3767 } 3768 3769 VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0); 3770 VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0); 3771 3772 for (int i = 0; i < count - 1; i++) 3773 nvlist_free(newdev[i]); 3774 3775 if (count > 1) 3776 kmem_free(newdev, (count - 1) * sizeof (void *)); 3777 } 3778 3779 /* 3780 * Removing a device from the vdev namespace requires several steps 3781 * and can take a significant amount of time. As a result we use 3782 * the spa_vdev_config_[enter/exit] functions which allow us to 3783 * grab and release the spa_config_lock while still holding the namespace 3784 * lock. During each step the configuration is synced out. 3785 */ 3786 3787 /* 3788 * Initial phase of device removal - stop future allocations from this device. 3789 */ 3790 void 3791 spa_vdev_remove_start(spa_t *spa, vdev_t *vd) 3792 { 3793 metaslab_group_t *mg = vd->vdev_mg; 3794 3795 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 3796 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 3797 ASSERT(vd == vd->vdev_top); 3798 3799 /* 3800 * Remove our vdev from the allocatable vdevs 3801 */ 3802 if (mg) 3803 metaslab_class_remove(mg->mg_class, mg); 3804 } 3805 3806 /* 3807 * Evacuate the device. 3808 */ 3809 int 3810 spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd) 3811 { 3812 uint64_t txg; 3813 int error; 3814 3815 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 3816 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 3817 ASSERT(vd == vd->vdev_top); 3818 3819 /* 3820 * Evacuate the device. We don't hold the config lock as writer 3821 * since we need to do I/O but we do keep the 3822 * spa_namespace_lock held. Once this completes the device 3823 * should no longer have any blocks allocated on it. 3824 */ 3825 if (vd->vdev_islog) { 3826 /* 3827 * Evacuate the device. 3828 */ 3829 if (error = dmu_objset_find(spa_name(spa), 3830 zil_vdev_offline, NULL, DS_FIND_CHILDREN)) { 3831 uint64_t txg; 3832 3833 txg = spa_vdev_config_enter(spa); 3834 metaslab_class_add(spa->spa_log_class, 3835 vd->vdev_mg); 3836 return (spa_vdev_exit(spa, NULL, txg, error)); 3837 } 3838 txg_wait_synced(spa_get_dsl(spa), 0); 3839 } 3840 3841 /* 3842 * Remove any remaining MOS metadata associated with the device. 3843 */ 3844 txg = spa_vdev_config_enter(spa); 3845 vd->vdev_removing = B_TRUE; 3846 vdev_dirty(vd, 0, NULL, txg); 3847 vdev_config_dirty(vd); 3848 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 3849 3850 return (0); 3851 } 3852 3853 /* 3854 * Complete the removal by cleaning up the namespace. 3855 */ 3856 void 3857 spa_vdev_remove_done(spa_t *spa, vdev_t *vd) 3858 { 3859 vdev_t *rvd = spa->spa_root_vdev; 3860 metaslab_group_t *mg = vd->vdev_mg; 3861 uint64_t id = vd->vdev_id; 3862 boolean_t last_vdev = (id == (rvd->vdev_children - 1)); 3863 3864 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 3865 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 3866 ASSERT(vd == vd->vdev_top); 3867 3868 (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 3869 3870 if (list_link_active(&vd->vdev_state_dirty_node)) 3871 vdev_state_clean(vd); 3872 if (list_link_active(&vd->vdev_config_dirty_node)) 3873 vdev_config_clean(vd); 3874 3875 vdev_free(vd); 3876 3877 /* 3878 * It's possible that another thread is trying todo a spa_vdev_add() 3879 * at the same time we're trying remove it. As a result the 3880 * added vdev may not have initialized its metaslabs yet. 3881 */ 3882 if (mg != NULL) 3883 metaslab_group_destroy(mg); 3884 3885 if (last_vdev) { 3886 vdev_compact_children(rvd); 3887 } else { 3888 vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops); 3889 vdev_add_child(rvd, vd); 3890 } 3891 vdev_config_dirty(rvd); 3892 3893 /* 3894 * Reassess the health of our root vdev. 3895 */ 3896 vdev_reopen(rvd); 3897 } 3898 3899 /* 3900 * Remove a device from the pool. Currently, this supports removing only hot 3901 * spares, slogs, and level 2 ARC devices. 3902 */ 3903 int 3904 spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 3905 { 3906 vdev_t *vd; 3907 nvlist_t **spares, **l2cache, *nv; 3908 uint64_t txg = 0; 3909 uint_t nspares, nl2cache; 3910 int error = 0; 3911 boolean_t locked = MUTEX_HELD(&spa_namespace_lock); 3912 3913 if (!locked) 3914 txg = spa_vdev_enter(spa); 3915 3916 vd = spa_lookup_by_guid(spa, guid, B_FALSE); 3917 3918 if (spa->spa_spares.sav_vdevs != NULL && 3919 nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 3920 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 && 3921 (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) { 3922 /* 3923 * Only remove the hot spare if it's not currently in use 3924 * in this pool. 3925 */ 3926 if (vd == NULL || unspare) { 3927 spa_vdev_remove_aux(spa->spa_spares.sav_config, 3928 ZPOOL_CONFIG_SPARES, spares, nspares, nv); 3929 spa_load_spares(spa); 3930 spa->spa_spares.sav_sync = B_TRUE; 3931 } else { 3932 error = EBUSY; 3933 } 3934 } else if (spa->spa_l2cache.sav_vdevs != NULL && 3935 nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 3936 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 && 3937 (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) { 3938 /* 3939 * Cache devices can always be removed. 3940 */ 3941 spa_vdev_remove_aux(spa->spa_l2cache.sav_config, 3942 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv); 3943 spa_load_l2cache(spa); 3944 spa->spa_l2cache.sav_sync = B_TRUE; 3945 } else if (vd != NULL && vd->vdev_islog) { 3946 ASSERT(!locked); 3947 ASSERT(vd == vd->vdev_top); 3948 3949 /* 3950 * XXX - Once we have bp-rewrite this should 3951 * become the common case. 3952 */ 3953 3954 /* 3955 * 1. Stop allocations 3956 * 2. Evacuate the device (i.e. kill off stubby and 3957 * metadata) and wait for it to complete (i.e. sync). 3958 * 3. Cleanup the vdev namespace. 3959 */ 3960 spa_vdev_remove_start(spa, vd); 3961 3962 /* 3963 * Wait for the youngest allocations and frees to sync, 3964 * and then wait for the deferral of those frees to finish. 3965 */ 3966 spa_vdev_config_exit(spa, NULL, 3967 txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG); 3968 3969 if ((error = spa_vdev_remove_evacuate(spa, vd)) != 0) 3970 return (error); 3971 txg = spa_vdev_config_enter(spa); 3972 3973 spa_vdev_remove_done(spa, vd); 3974 3975 } else if (vd != NULL) { 3976 /* 3977 * Normal vdevs cannot be removed (yet). 3978 */ 3979 error = ENOTSUP; 3980 } else { 3981 /* 3982 * There is no vdev of any kind with the specified guid. 3983 */ 3984 error = ENOENT; 3985 } 3986 3987 if (!locked) 3988 return (spa_vdev_exit(spa, NULL, txg, error)); 3989 3990 return (error); 3991 } 3992 3993 /* 3994 * Find any device that's done replacing, or a vdev marked 'unspare' that's 3995 * current spared, so we can detach it. 3996 */ 3997 static vdev_t * 3998 spa_vdev_resilver_done_hunt(vdev_t *vd) 3999 { 4000 vdev_t *newvd, *oldvd; 4001 4002 for (int c = 0; c < vd->vdev_children; c++) { 4003 oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 4004 if (oldvd != NULL) 4005 return (oldvd); 4006 } 4007 4008 /* 4009 * Check for a completed replacement. 4010 */ 4011 if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) { 4012 oldvd = vd->vdev_child[0]; 4013 newvd = vd->vdev_child[1]; 4014 4015 if (vdev_dtl_empty(newvd, DTL_MISSING) && 4016 !vdev_dtl_required(oldvd)) 4017 return (oldvd); 4018 } 4019 4020 /* 4021 * Check for a completed resilver with the 'unspare' flag set. 4022 */ 4023 if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) { 4024 newvd = vd->vdev_child[0]; 4025 oldvd = vd->vdev_child[1]; 4026 4027 if (newvd->vdev_unspare && 4028 vdev_dtl_empty(newvd, DTL_MISSING) && 4029 !vdev_dtl_required(oldvd)) { 4030 newvd->vdev_unspare = 0; 4031 return (oldvd); 4032 } 4033 } 4034 4035 return (NULL); 4036 } 4037 4038 static void 4039 spa_vdev_resilver_done(spa_t *spa) 4040 { 4041 vdev_t *vd, *pvd, *ppvd; 4042 uint64_t guid, sguid, pguid, ppguid; 4043 4044 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 4045 4046 while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 4047 pvd = vd->vdev_parent; 4048 ppvd = pvd->vdev_parent; 4049 guid = vd->vdev_guid; 4050 pguid = pvd->vdev_guid; 4051 ppguid = ppvd->vdev_guid; 4052 sguid = 0; 4053 /* 4054 * If we have just finished replacing a hot spared device, then 4055 * we need to detach the parent's first child (the original hot 4056 * spare) as well. 4057 */ 4058 if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0) { 4059 ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 4060 ASSERT(ppvd->vdev_children == 2); 4061 sguid = ppvd->vdev_child[1]->vdev_guid; 4062 } 4063 spa_config_exit(spa, SCL_ALL, FTAG); 4064 if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0) 4065 return; 4066 if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0) 4067 return; 4068 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 4069 } 4070 4071 spa_config_exit(spa, SCL_ALL, FTAG); 4072 } 4073 4074 /* 4075 * Update the stored path or FRU for this vdev. Dirty the vdev configuration, 4076 * relying on spa_vdev_enter/exit() to synchronize the labels and cache. 4077 */ 4078 int 4079 spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value, 4080 boolean_t ispath) 4081 { 4082 vdev_t *vd; 4083 uint64_t txg; 4084 4085 txg = spa_vdev_enter(spa); 4086 4087 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 4088 return (spa_vdev_exit(spa, NULL, txg, ENOENT)); 4089 4090 if (!vd->vdev_ops->vdev_op_leaf) 4091 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 4092 4093 if (ispath) { 4094 spa_strfree(vd->vdev_path); 4095 vd->vdev_path = spa_strdup(value); 4096 } else { 4097 if (vd->vdev_fru != NULL) 4098 spa_strfree(vd->vdev_fru); 4099 vd->vdev_fru = spa_strdup(value); 4100 } 4101 4102 vdev_config_dirty(vd->vdev_top); 4103 4104 return (spa_vdev_exit(spa, NULL, txg, 0)); 4105 } 4106 4107 int 4108 spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 4109 { 4110 return (spa_vdev_set_common(spa, guid, newpath, B_TRUE)); 4111 } 4112 4113 int 4114 spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru) 4115 { 4116 return (spa_vdev_set_common(spa, guid, newfru, B_FALSE)); 4117 } 4118 4119 /* 4120 * ========================================================================== 4121 * SPA Scrubbing 4122 * ========================================================================== 4123 */ 4124 4125 int 4126 spa_scrub(spa_t *spa, pool_scrub_type_t type) 4127 { 4128 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 4129 4130 if ((uint_t)type >= POOL_SCRUB_TYPES) 4131 return (ENOTSUP); 4132 4133 /* 4134 * If a resilver was requested, but there is no DTL on a 4135 * writeable leaf device, we have nothing to do. 4136 */ 4137 if (type == POOL_SCRUB_RESILVER && 4138 !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { 4139 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 4140 return (0); 4141 } 4142 4143 if (type == POOL_SCRUB_EVERYTHING && 4144 spa->spa_dsl_pool->dp_scrub_func != SCRUB_FUNC_NONE && 4145 spa->spa_dsl_pool->dp_scrub_isresilver) 4146 return (EBUSY); 4147 4148 if (type == POOL_SCRUB_EVERYTHING || type == POOL_SCRUB_RESILVER) { 4149 return (dsl_pool_scrub_clean(spa->spa_dsl_pool)); 4150 } else if (type == POOL_SCRUB_NONE) { 4151 return (dsl_pool_scrub_cancel(spa->spa_dsl_pool)); 4152 } else { 4153 return (EINVAL); 4154 } 4155 } 4156 4157 /* 4158 * ========================================================================== 4159 * SPA async task processing 4160 * ========================================================================== 4161 */ 4162 4163 static void 4164 spa_async_remove(spa_t *spa, vdev_t *vd) 4165 { 4166 if (vd->vdev_remove_wanted) { 4167 vd->vdev_remove_wanted = 0; 4168 vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); 4169 4170 /* 4171 * We want to clear the stats, but we don't want to do a full 4172 * vdev_clear() as that will cause us to throw away 4173 * degraded/faulted state as well as attempt to reopen the 4174 * device, all of which is a waste. 4175 */ 4176 vd->vdev_stat.vs_read_errors = 0; 4177 vd->vdev_stat.vs_write_errors = 0; 4178 vd->vdev_stat.vs_checksum_errors = 0; 4179 4180 vdev_state_dirty(vd->vdev_top); 4181 } 4182 4183 for (int c = 0; c < vd->vdev_children; c++) 4184 spa_async_remove(spa, vd->vdev_child[c]); 4185 } 4186 4187 static void 4188 spa_async_probe(spa_t *spa, vdev_t *vd) 4189 { 4190 if (vd->vdev_probe_wanted) { 4191 vd->vdev_probe_wanted = 0; 4192 vdev_reopen(vd); /* vdev_open() does the actual probe */ 4193 } 4194 4195 for (int c = 0; c < vd->vdev_children; c++) 4196 spa_async_probe(spa, vd->vdev_child[c]); 4197 } 4198 4199 static void 4200 spa_async_autoexpand(spa_t *spa, vdev_t *vd) 4201 { 4202 sysevent_id_t eid; 4203 nvlist_t *attr; 4204 char *physpath; 4205 4206 if (!spa->spa_autoexpand) 4207 return; 4208 4209 for (int c = 0; c < vd->vdev_children; c++) { 4210 vdev_t *cvd = vd->vdev_child[c]; 4211 spa_async_autoexpand(spa, cvd); 4212 } 4213 4214 if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL) 4215 return; 4216 4217 physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 4218 (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath); 4219 4220 VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0); 4221 VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0); 4222 4223 (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS, 4224 ESC_DEV_DLE, attr, &eid, DDI_SLEEP); 4225 4226 nvlist_free(attr); 4227 kmem_free(physpath, MAXPATHLEN); 4228 } 4229 4230 static void 4231 spa_async_thread(spa_t *spa) 4232 { 4233 int tasks; 4234 4235 ASSERT(spa->spa_sync_on); 4236 4237 mutex_enter(&spa->spa_async_lock); 4238 tasks = spa->spa_async_tasks; 4239 spa->spa_async_tasks = 0; 4240 mutex_exit(&spa->spa_async_lock); 4241 4242 /* 4243 * See if the config needs to be updated. 4244 */ 4245 if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 4246 uint64_t old_space, new_space; 4247 4248 mutex_enter(&spa_namespace_lock); 4249 old_space = metaslab_class_get_space(spa_normal_class(spa)); 4250 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 4251 new_space = metaslab_class_get_space(spa_normal_class(spa)); 4252 mutex_exit(&spa_namespace_lock); 4253 4254 /* 4255 * If the pool grew as a result of the config update, 4256 * then log an internal history event. 4257 */ 4258 if (new_space != old_space) { 4259 spa_history_internal_log(LOG_POOL_VDEV_ONLINE, 4260 spa, NULL, CRED(), 4261 "pool '%s' size: %llu(+%llu)", 4262 spa_name(spa), new_space, new_space - old_space); 4263 } 4264 } 4265 4266 /* 4267 * See if any devices need to be marked REMOVED. 4268 */ 4269 if (tasks & SPA_ASYNC_REMOVE) { 4270 spa_vdev_state_enter(spa, SCL_NONE); 4271 spa_async_remove(spa, spa->spa_root_vdev); 4272 for (int i = 0; i < spa->spa_l2cache.sav_count; i++) 4273 spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); 4274 for (int i = 0; i < spa->spa_spares.sav_count; i++) 4275 spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); 4276 (void) spa_vdev_state_exit(spa, NULL, 0); 4277 } 4278 4279 if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) { 4280 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 4281 spa_async_autoexpand(spa, spa->spa_root_vdev); 4282 spa_config_exit(spa, SCL_CONFIG, FTAG); 4283 } 4284 4285 /* 4286 * See if any devices need to be probed. 4287 */ 4288 if (tasks & SPA_ASYNC_PROBE) { 4289 spa_vdev_state_enter(spa, SCL_NONE); 4290 spa_async_probe(spa, spa->spa_root_vdev); 4291 (void) spa_vdev_state_exit(spa, NULL, 0); 4292 } 4293 4294 /* 4295 * If any devices are done replacing, detach them. 4296 */ 4297 if (tasks & SPA_ASYNC_RESILVER_DONE) 4298 spa_vdev_resilver_done(spa); 4299 4300 /* 4301 * Kick off a resilver. 4302 */ 4303 if (tasks & SPA_ASYNC_RESILVER) 4304 VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER) == 0); 4305 4306 /* 4307 * Let the world know that we're done. 4308 */ 4309 mutex_enter(&spa->spa_async_lock); 4310 spa->spa_async_thread = NULL; 4311 cv_broadcast(&spa->spa_async_cv); 4312 mutex_exit(&spa->spa_async_lock); 4313 thread_exit(); 4314 } 4315 4316 void 4317 spa_async_suspend(spa_t *spa) 4318 { 4319 mutex_enter(&spa->spa_async_lock); 4320 spa->spa_async_suspended++; 4321 while (spa->spa_async_thread != NULL) 4322 cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 4323 mutex_exit(&spa->spa_async_lock); 4324 } 4325 4326 void 4327 spa_async_resume(spa_t *spa) 4328 { 4329 mutex_enter(&spa->spa_async_lock); 4330 ASSERT(spa->spa_async_suspended != 0); 4331 spa->spa_async_suspended--; 4332 mutex_exit(&spa->spa_async_lock); 4333 } 4334 4335 static void 4336 spa_async_dispatch(spa_t *spa) 4337 { 4338 mutex_enter(&spa->spa_async_lock); 4339 if (spa->spa_async_tasks && !spa->spa_async_suspended && 4340 spa->spa_async_thread == NULL && 4341 rootdir != NULL && !vn_is_readonly(rootdir)) 4342 spa->spa_async_thread = thread_create(NULL, 0, 4343 spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 4344 mutex_exit(&spa->spa_async_lock); 4345 } 4346 4347 void 4348 spa_async_request(spa_t *spa, int task) 4349 { 4350 mutex_enter(&spa->spa_async_lock); 4351 spa->spa_async_tasks |= task; 4352 mutex_exit(&spa->spa_async_lock); 4353 } 4354 4355 /* 4356 * ========================================================================== 4357 * SPA syncing routines 4358 * ========================================================================== 4359 */ 4360 static void 4361 spa_sync_deferred_bplist(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx, uint64_t txg) 4362 { 4363 blkptr_t blk; 4364 uint64_t itor = 0; 4365 uint8_t c = 1; 4366 4367 while (bplist_iterate(bpl, &itor, &blk) == 0) { 4368 ASSERT(blk.blk_birth < txg); 4369 zio_free(spa, txg, &blk); 4370 } 4371 4372 bplist_vacate(bpl, tx); 4373 4374 /* 4375 * Pre-dirty the first block so we sync to convergence faster. 4376 * (Usually only the first block is needed.) 4377 */ 4378 dmu_write(bpl->bpl_mos, spa->spa_deferred_bplist_obj, 0, 1, &c, tx); 4379 } 4380 4381 static void 4382 spa_sync_free(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 4383 { 4384 zio_t *zio = arg; 4385 4386 zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp, 4387 zio->io_flags)); 4388 } 4389 4390 static void 4391 spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 4392 { 4393 char *packed = NULL; 4394 size_t bufsize; 4395 size_t nvsize = 0; 4396 dmu_buf_t *db; 4397 4398 VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 4399 4400 /* 4401 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration 4402 * information. This avoids the dbuf_will_dirty() path and 4403 * saves us a pre-read to get data we don't actually care about. 4404 */ 4405 bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE); 4406 packed = kmem_alloc(bufsize, KM_SLEEP); 4407 4408 VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 4409 KM_SLEEP) == 0); 4410 bzero(packed + nvsize, bufsize - nvsize); 4411 4412 dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); 4413 4414 kmem_free(packed, bufsize); 4415 4416 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 4417 dmu_buf_will_dirty(db, tx); 4418 *(uint64_t *)db->db_data = nvsize; 4419 dmu_buf_rele(db, FTAG); 4420 } 4421 4422 static void 4423 spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, 4424 const char *config, const char *entry) 4425 { 4426 nvlist_t *nvroot; 4427 nvlist_t **list; 4428 int i; 4429 4430 if (!sav->sav_sync) 4431 return; 4432 4433 /* 4434 * Update the MOS nvlist describing the list of available devices. 4435 * spa_validate_aux() will have already made sure this nvlist is 4436 * valid and the vdevs are labeled appropriately. 4437 */ 4438 if (sav->sav_object == 0) { 4439 sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, 4440 DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, 4441 sizeof (uint64_t), tx); 4442 VERIFY(zap_update(spa->spa_meta_objset, 4443 DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, 4444 &sav->sav_object, tx) == 0); 4445 } 4446 4447 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 4448 if (sav->sav_count == 0) { 4449 VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); 4450 } else { 4451 list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 4452 for (i = 0; i < sav->sav_count; i++) 4453 list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], 4454 B_FALSE, B_FALSE, B_TRUE); 4455 VERIFY(nvlist_add_nvlist_array(nvroot, config, list, 4456 sav->sav_count) == 0); 4457 for (i = 0; i < sav->sav_count; i++) 4458 nvlist_free(list[i]); 4459 kmem_free(list, sav->sav_count * sizeof (void *)); 4460 } 4461 4462 spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); 4463 nvlist_free(nvroot); 4464 4465 sav->sav_sync = B_FALSE; 4466 } 4467 4468 static void 4469 spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 4470 { 4471 nvlist_t *config; 4472 4473 if (list_is_empty(&spa->spa_config_dirty_list)) 4474 return; 4475 4476 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 4477 4478 config = spa_config_generate(spa, spa->spa_root_vdev, 4479 dmu_tx_get_txg(tx), B_FALSE); 4480 4481 spa_config_exit(spa, SCL_STATE, FTAG); 4482 4483 if (spa->spa_config_syncing) 4484 nvlist_free(spa->spa_config_syncing); 4485 spa->spa_config_syncing = config; 4486 4487 spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 4488 } 4489 4490 /* 4491 * Set zpool properties. 4492 */ 4493 static void 4494 spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 4495 { 4496 spa_t *spa = arg1; 4497 objset_t *mos = spa->spa_meta_objset; 4498 nvlist_t *nvp = arg2; 4499 nvpair_t *elem; 4500 uint64_t intval; 4501 char *strval; 4502 zpool_prop_t prop; 4503 const char *propname; 4504 zprop_type_t proptype; 4505 4506 mutex_enter(&spa->spa_props_lock); 4507 4508 elem = NULL; 4509 while ((elem = nvlist_next_nvpair(nvp, elem))) { 4510 switch (prop = zpool_name_to_prop(nvpair_name(elem))) { 4511 case ZPOOL_PROP_VERSION: 4512 /* 4513 * Only set version for non-zpool-creation cases 4514 * (set/import). spa_create() needs special care 4515 * for version setting. 4516 */ 4517 if (tx->tx_txg != TXG_INITIAL) { 4518 VERIFY(nvpair_value_uint64(elem, 4519 &intval) == 0); 4520 ASSERT(intval <= SPA_VERSION); 4521 ASSERT(intval >= spa_version(spa)); 4522 spa->spa_uberblock.ub_version = intval; 4523 vdev_config_dirty(spa->spa_root_vdev); 4524 } 4525 break; 4526 4527 case ZPOOL_PROP_ALTROOT: 4528 /* 4529 * 'altroot' is a non-persistent property. It should 4530 * have been set temporarily at creation or import time. 4531 */ 4532 ASSERT(spa->spa_root != NULL); 4533 break; 4534 4535 case ZPOOL_PROP_CACHEFILE: 4536 /* 4537 * 'cachefile' is also a non-persisitent property. 4538 */ 4539 break; 4540 default: 4541 /* 4542 * Set pool property values in the poolprops mos object. 4543 */ 4544 if (spa->spa_pool_props_object == 0) { 4545 VERIFY((spa->spa_pool_props_object = 4546 zap_create(mos, DMU_OT_POOL_PROPS, 4547 DMU_OT_NONE, 0, tx)) > 0); 4548 4549 VERIFY(zap_update(mos, 4550 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, 4551 8, 1, &spa->spa_pool_props_object, tx) 4552 == 0); 4553 } 4554 4555 /* normalize the property name */ 4556 propname = zpool_prop_to_name(prop); 4557 proptype = zpool_prop_get_type(prop); 4558 4559 if (nvpair_type(elem) == DATA_TYPE_STRING) { 4560 ASSERT(proptype == PROP_TYPE_STRING); 4561 VERIFY(nvpair_value_string(elem, &strval) == 0); 4562 VERIFY(zap_update(mos, 4563 spa->spa_pool_props_object, propname, 4564 1, strlen(strval) + 1, strval, tx) == 0); 4565 4566 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 4567 VERIFY(nvpair_value_uint64(elem, &intval) == 0); 4568 4569 if (proptype == PROP_TYPE_INDEX) { 4570 const char *unused; 4571 VERIFY(zpool_prop_index_to_string( 4572 prop, intval, &unused) == 0); 4573 } 4574 VERIFY(zap_update(mos, 4575 spa->spa_pool_props_object, propname, 4576 8, 1, &intval, tx) == 0); 4577 } else { 4578 ASSERT(0); /* not allowed */ 4579 } 4580 4581 switch (prop) { 4582 case ZPOOL_PROP_DELEGATION: 4583 spa->spa_delegation = intval; 4584 break; 4585 case ZPOOL_PROP_BOOTFS: 4586 spa->spa_bootfs = intval; 4587 break; 4588 case ZPOOL_PROP_FAILUREMODE: 4589 spa->spa_failmode = intval; 4590 break; 4591 case ZPOOL_PROP_AUTOEXPAND: 4592 spa->spa_autoexpand = intval; 4593 spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); 4594 break; 4595 case ZPOOL_PROP_DEDUPDITTO: 4596 spa->spa_dedup_ditto = intval; 4597 break; 4598 default: 4599 break; 4600 } 4601 } 4602 4603 /* log internal history if this is not a zpool create */ 4604 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY && 4605 tx->tx_txg != TXG_INITIAL) { 4606 spa_history_internal_log(LOG_POOL_PROPSET, 4607 spa, tx, cr, "%s %lld %s", 4608 nvpair_name(elem), intval, spa_name(spa)); 4609 } 4610 } 4611 4612 mutex_exit(&spa->spa_props_lock); 4613 } 4614 4615 /* 4616 * Sync the specified transaction group. New blocks may be dirtied as 4617 * part of the process, so we iterate until it converges. 4618 */ 4619 void 4620 spa_sync(spa_t *spa, uint64_t txg) 4621 { 4622 dsl_pool_t *dp = spa->spa_dsl_pool; 4623 objset_t *mos = spa->spa_meta_objset; 4624 bplist_t *defer_bpl = &spa->spa_deferred_bplist; 4625 bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK]; 4626 vdev_t *rvd = spa->spa_root_vdev; 4627 vdev_t *vd; 4628 dmu_tx_t *tx; 4629 int error; 4630 4631 /* 4632 * Lock out configuration changes. 4633 */ 4634 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 4635 4636 spa->spa_syncing_txg = txg; 4637 spa->spa_sync_pass = 0; 4638 4639 /* 4640 * If there are any pending vdev state changes, convert them 4641 * into config changes that go out with this transaction group. 4642 */ 4643 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 4644 while (list_head(&spa->spa_state_dirty_list) != NULL) { 4645 /* 4646 * We need the write lock here because, for aux vdevs, 4647 * calling vdev_config_dirty() modifies sav_config. 4648 * This is ugly and will become unnecessary when we 4649 * eliminate the aux vdev wart by integrating all vdevs 4650 * into the root vdev tree. 4651 */ 4652 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 4653 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER); 4654 while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { 4655 vdev_state_clean(vd); 4656 vdev_config_dirty(vd); 4657 } 4658 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 4659 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); 4660 } 4661 spa_config_exit(spa, SCL_STATE, FTAG); 4662 4663 VERIFY(0 == bplist_open(defer_bpl, mos, spa->spa_deferred_bplist_obj)); 4664 4665 tx = dmu_tx_create_assigned(dp, txg); 4666 4667 /* 4668 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 4669 * set spa_deflate if we have no raid-z vdevs. 4670 */ 4671 if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 4672 spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 4673 int i; 4674 4675 for (i = 0; i < rvd->vdev_children; i++) { 4676 vd = rvd->vdev_child[i]; 4677 if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 4678 break; 4679 } 4680 if (i == rvd->vdev_children) { 4681 spa->spa_deflate = TRUE; 4682 VERIFY(0 == zap_add(spa->spa_meta_objset, 4683 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 4684 sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 4685 } 4686 } 4687 4688 if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && 4689 spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { 4690 dsl_pool_create_origin(dp, tx); 4691 4692 /* Keeping the origin open increases spa_minref */ 4693 spa->spa_minref += 3; 4694 } 4695 4696 if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && 4697 spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { 4698 dsl_pool_upgrade_clones(dp, tx); 4699 } 4700 4701 /* 4702 * If anything has changed in this txg, push the deferred frees 4703 * from the previous txg. If not, leave them alone so that we 4704 * don't generate work on an otherwise idle system. 4705 */ 4706 if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || 4707 !txg_list_empty(&dp->dp_dirty_dirs, txg) || 4708 !txg_list_empty(&dp->dp_sync_tasks, txg)) 4709 spa_sync_deferred_bplist(spa, defer_bpl, tx, txg); 4710 4711 /* 4712 * Iterate to convergence. 4713 */ 4714 do { 4715 int pass = ++spa->spa_sync_pass; 4716 4717 spa_sync_config_object(spa, tx); 4718 spa_sync_aux_dev(spa, &spa->spa_spares, tx, 4719 ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); 4720 spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, 4721 ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); 4722 spa_errlog_sync(spa, txg); 4723 dsl_pool_sync(dp, txg); 4724 4725 if (pass <= SYNC_PASS_DEFERRED_FREE) { 4726 zio_t *zio = zio_root(spa, NULL, NULL, 0); 4727 bplist_sync(free_bpl, spa_sync_free, zio, tx); 4728 VERIFY(zio_wait(zio) == 0); 4729 } else { 4730 bplist_sync(free_bpl, bplist_enqueue_cb, defer_bpl, tx); 4731 } 4732 4733 ddt_sync(spa, txg); 4734 4735 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) 4736 vdev_sync(vd, txg); 4737 4738 } while (dmu_objset_is_dirty(mos, txg)); 4739 4740 ASSERT(free_bpl->bpl_queue == NULL); 4741 4742 bplist_close(defer_bpl); 4743 4744 /* 4745 * Rewrite the vdev configuration (which includes the uberblock) 4746 * to commit the transaction group. 4747 * 4748 * If there are no dirty vdevs, we sync the uberblock to a few 4749 * random top-level vdevs that are known to be visible in the 4750 * config cache (see spa_vdev_add() for a complete description). 4751 * If there *are* dirty vdevs, sync the uberblock to all vdevs. 4752 */ 4753 for (;;) { 4754 /* 4755 * We hold SCL_STATE to prevent vdev open/close/etc. 4756 * while we're attempting to write the vdev labels. 4757 */ 4758 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 4759 4760 if (list_is_empty(&spa->spa_config_dirty_list)) { 4761 vdev_t *svd[SPA_DVAS_PER_BP]; 4762 int svdcount = 0; 4763 int children = rvd->vdev_children; 4764 int c0 = spa_get_random(children); 4765 4766 for (int c = 0; c < children; c++) { 4767 vd = rvd->vdev_child[(c0 + c) % children]; 4768 if (vd->vdev_ms_array == 0 || vd->vdev_islog) 4769 continue; 4770 svd[svdcount++] = vd; 4771 if (svdcount == SPA_DVAS_PER_BP) 4772 break; 4773 } 4774 error = vdev_config_sync(svd, svdcount, txg, B_FALSE); 4775 if (error != 0) 4776 error = vdev_config_sync(svd, svdcount, txg, 4777 B_TRUE); 4778 } else { 4779 error = vdev_config_sync(rvd->vdev_child, 4780 rvd->vdev_children, txg, B_FALSE); 4781 if (error != 0) 4782 error = vdev_config_sync(rvd->vdev_child, 4783 rvd->vdev_children, txg, B_TRUE); 4784 } 4785 4786 spa_config_exit(spa, SCL_STATE, FTAG); 4787 4788 if (error == 0) 4789 break; 4790 zio_suspend(spa, NULL); 4791 zio_resume_wait(spa); 4792 } 4793 dmu_tx_commit(tx); 4794 4795 /* 4796 * Clear the dirty config list. 4797 */ 4798 while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL) 4799 vdev_config_clean(vd); 4800 4801 /* 4802 * Now that the new config has synced transactionally, 4803 * let it become visible to the config cache. 4804 */ 4805 if (spa->spa_config_syncing != NULL) { 4806 spa_config_set(spa, spa->spa_config_syncing); 4807 spa->spa_config_txg = txg; 4808 spa->spa_config_syncing = NULL; 4809 } 4810 4811 spa->spa_ubsync = spa->spa_uberblock; 4812 4813 dsl_pool_sync_done(dp, txg); 4814 4815 /* 4816 * Update usable space statistics. 4817 */ 4818 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 4819 vdev_sync_done(vd, txg); 4820 4821 /* 4822 * It had better be the case that we didn't dirty anything 4823 * since vdev_config_sync(). 4824 */ 4825 ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 4826 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 4827 ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 4828 ASSERT(defer_bpl->bpl_queue == NULL); 4829 ASSERT(free_bpl->bpl_queue == NULL); 4830 4831 spa->spa_sync_pass = 0; 4832 4833 spa_config_exit(spa, SCL_CONFIG, FTAG); 4834 4835 spa_handle_ignored_writes(spa); 4836 4837 /* 4838 * If any async tasks have been requested, kick them off. 4839 */ 4840 spa_async_dispatch(spa); 4841 } 4842 4843 /* 4844 * Sync all pools. We don't want to hold the namespace lock across these 4845 * operations, so we take a reference on the spa_t and drop the lock during the 4846 * sync. 4847 */ 4848 void 4849 spa_sync_allpools(void) 4850 { 4851 spa_t *spa = NULL; 4852 mutex_enter(&spa_namespace_lock); 4853 while ((spa = spa_next(spa)) != NULL) { 4854 if (spa_state(spa) != POOL_STATE_ACTIVE || spa_suspended(spa)) 4855 continue; 4856 spa_open_ref(spa, FTAG); 4857 mutex_exit(&spa_namespace_lock); 4858 txg_wait_synced(spa_get_dsl(spa), 0); 4859 mutex_enter(&spa_namespace_lock); 4860 spa_close(spa, FTAG); 4861 } 4862 mutex_exit(&spa_namespace_lock); 4863 } 4864 4865 /* 4866 * ========================================================================== 4867 * Miscellaneous routines 4868 * ========================================================================== 4869 */ 4870 4871 /* 4872 * Remove all pools in the system. 4873 */ 4874 void 4875 spa_evict_all(void) 4876 { 4877 spa_t *spa; 4878 4879 /* 4880 * Remove all cached state. All pools should be closed now, 4881 * so every spa in the AVL tree should be unreferenced. 4882 */ 4883 mutex_enter(&spa_namespace_lock); 4884 while ((spa = spa_next(NULL)) != NULL) { 4885 /* 4886 * Stop async tasks. The async thread may need to detach 4887 * a device that's been replaced, which requires grabbing 4888 * spa_namespace_lock, so we must drop it here. 4889 */ 4890 spa_open_ref(spa, FTAG); 4891 mutex_exit(&spa_namespace_lock); 4892 spa_async_suspend(spa); 4893 mutex_enter(&spa_namespace_lock); 4894 spa_close(spa, FTAG); 4895 4896 if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 4897 spa_unload(spa); 4898 spa_deactivate(spa); 4899 } 4900 spa_remove(spa); 4901 } 4902 mutex_exit(&spa_namespace_lock); 4903 } 4904 4905 vdev_t * 4906 spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux) 4907 { 4908 vdev_t *vd; 4909 int i; 4910 4911 if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) 4912 return (vd); 4913 4914 if (aux) { 4915 for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 4916 vd = spa->spa_l2cache.sav_vdevs[i]; 4917 if (vd->vdev_guid == guid) 4918 return (vd); 4919 } 4920 4921 for (i = 0; i < spa->spa_spares.sav_count; i++) { 4922 vd = spa->spa_spares.sav_vdevs[i]; 4923 if (vd->vdev_guid == guid) 4924 return (vd); 4925 } 4926 } 4927 4928 return (NULL); 4929 } 4930 4931 void 4932 spa_upgrade(spa_t *spa, uint64_t version) 4933 { 4934 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 4935 4936 /* 4937 * This should only be called for a non-faulted pool, and since a 4938 * future version would result in an unopenable pool, this shouldn't be 4939 * possible. 4940 */ 4941 ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION); 4942 ASSERT(version >= spa->spa_uberblock.ub_version); 4943 4944 spa->spa_uberblock.ub_version = version; 4945 vdev_config_dirty(spa->spa_root_vdev); 4946 4947 spa_config_exit(spa, SCL_ALL, FTAG); 4948 4949 txg_wait_synced(spa_get_dsl(spa), 0); 4950 } 4951 4952 boolean_t 4953 spa_has_spare(spa_t *spa, uint64_t guid) 4954 { 4955 int i; 4956 uint64_t spareguid; 4957 spa_aux_vdev_t *sav = &spa->spa_spares; 4958 4959 for (i = 0; i < sav->sav_count; i++) 4960 if (sav->sav_vdevs[i]->vdev_guid == guid) 4961 return (B_TRUE); 4962 4963 for (i = 0; i < sav->sav_npending; i++) { 4964 if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, 4965 &spareguid) == 0 && spareguid == guid) 4966 return (B_TRUE); 4967 } 4968 4969 return (B_FALSE); 4970 } 4971 4972 /* 4973 * Check if a pool has an active shared spare device. 4974 * Note: reference count of an active spare is 2, as a spare and as a replace 4975 */ 4976 static boolean_t 4977 spa_has_active_shared_spare(spa_t *spa) 4978 { 4979 int i, refcnt; 4980 uint64_t pool; 4981 spa_aux_vdev_t *sav = &spa->spa_spares; 4982 4983 for (i = 0; i < sav->sav_count; i++) { 4984 if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, 4985 &refcnt) && pool != 0ULL && pool == spa_guid(spa) && 4986 refcnt > 2) 4987 return (B_TRUE); 4988 } 4989 4990 return (B_FALSE); 4991 } 4992 4993 /* 4994 * Post a sysevent corresponding to the given event. The 'name' must be one of 4995 * the event definitions in sys/sysevent/eventdefs.h. The payload will be 4996 * filled in from the spa and (optionally) the vdev. This doesn't do anything 4997 * in the userland libzpool, as we don't want consumers to misinterpret ztest 4998 * or zdb as real changes. 4999 */ 5000 void 5001 spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) 5002 { 5003 #ifdef _KERNEL 5004 sysevent_t *ev; 5005 sysevent_attr_list_t *attr = NULL; 5006 sysevent_value_t value; 5007 sysevent_id_t eid; 5008 5009 ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 5010 SE_SLEEP); 5011 5012 value.value_type = SE_DATA_TYPE_STRING; 5013 value.value.sv_string = spa_name(spa); 5014 if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 5015 goto done; 5016 5017 value.value_type = SE_DATA_TYPE_UINT64; 5018 value.value.sv_uint64 = spa_guid(spa); 5019 if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 5020 goto done; 5021 5022 if (vd) { 5023 value.value_type = SE_DATA_TYPE_UINT64; 5024 value.value.sv_uint64 = vd->vdev_guid; 5025 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 5026 SE_SLEEP) != 0) 5027 goto done; 5028 5029 if (vd->vdev_path) { 5030 value.value_type = SE_DATA_TYPE_STRING; 5031 value.value.sv_string = vd->vdev_path; 5032 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 5033 &value, SE_SLEEP) != 0) 5034 goto done; 5035 } 5036 } 5037 5038 if (sysevent_attach_attributes(ev, attr) != 0) 5039 goto done; 5040 attr = NULL; 5041 5042 (void) log_sysevent(ev, SE_SLEEP, &eid); 5043 5044 done: 5045 if (attr) 5046 sysevent_free_attr(attr); 5047 sysevent_free(ev); 5048 #endif 5049 } 5050