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