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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright (c) 2011, 2019 by Delphix. All rights reserved. 25 * Copyright (c) 2015, Nexenta Systems, Inc. All rights reserved. 26 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. 27 * Copyright 2013 Saso Kiselkov. All rights reserved. 28 * Copyright (c) 2014 Integros [integros.com] 29 * Copyright 2016 Toomas Soome <tsoome@me.com> 30 * Copyright (c) 2017, 2019, Datto Inc. All rights reserved. 31 * Copyright 2019 Joyent, Inc. 32 * Copyright (c) 2017, Intel Corporation. 33 * Copyright 2020 Joshua M. Clulow <josh@sysmgr.org> 34 * Copyright 2021 OmniOS Community Edition (OmniOSce) Association. 35 * Copyright 2022 Oxide Computer Company 36 * Copyright 2023 MNX Cloud, Inc. 37 */ 38 39 /* 40 * SPA: Storage Pool Allocator 41 * 42 * This file contains all the routines used when modifying on-disk SPA state. 43 * This includes opening, importing, destroying, exporting a pool, and syncing a 44 * pool. 45 */ 46 47 #include <sys/zfs_context.h> 48 #include <sys/fm/fs/zfs.h> 49 #include <sys/spa_impl.h> 50 #include <sys/zio.h> 51 #include <sys/zio_checksum.h> 52 #include <sys/dmu.h> 53 #include <sys/dmu_tx.h> 54 #include <sys/zap.h> 55 #include <sys/zil.h> 56 #include <sys/ddt.h> 57 #include <sys/vdev_impl.h> 58 #include <sys/vdev_removal.h> 59 #include <sys/vdev_indirect_mapping.h> 60 #include <sys/vdev_indirect_births.h> 61 #include <sys/vdev_initialize.h> 62 #include <sys/vdev_trim.h> 63 #include <sys/metaslab.h> 64 #include <sys/metaslab_impl.h> 65 #include <sys/mmp.h> 66 #include <sys/uberblock_impl.h> 67 #include <sys/txg.h> 68 #include <sys/avl.h> 69 #include <sys/bpobj.h> 70 #include <sys/dmu_traverse.h> 71 #include <sys/dmu_objset.h> 72 #include <sys/unique.h> 73 #include <sys/dsl_pool.h> 74 #include <sys/dsl_dataset.h> 75 #include <sys/dsl_dir.h> 76 #include <sys/dsl_prop.h> 77 #include <sys/dsl_synctask.h> 78 #include <sys/fs/zfs.h> 79 #include <sys/arc.h> 80 #include <sys/callb.h> 81 #include <sys/systeminfo.h> 82 #include <sys/spa_boot.h> 83 #include <sys/zfs_ioctl.h> 84 #include <sys/dsl_scan.h> 85 #include <sys/zfeature.h> 86 #include <sys/dsl_destroy.h> 87 #include <sys/abd.h> 88 89 #ifdef _KERNEL 90 #include <sys/bootprops.h> 91 #include <sys/callb.h> 92 #include <sys/cpupart.h> 93 #include <sys/pool.h> 94 #include <sys/sysdc.h> 95 #include <sys/zone.h> 96 #endif /* _KERNEL */ 97 98 #include "zfs_prop.h" 99 #include "zfs_comutil.h" 100 101 /* 102 * The interval, in seconds, at which failed configuration cache file writes 103 * should be retried. 104 */ 105 int zfs_ccw_retry_interval = 300; 106 107 typedef enum zti_modes { 108 ZTI_MODE_FIXED, /* value is # of threads (min 1) */ 109 ZTI_MODE_BATCH, /* cpu-intensive; value is ignored */ 110 ZTI_MODE_NULL, /* don't create a taskq */ 111 ZTI_NMODES 112 } zti_modes_t; 113 114 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) } 115 #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 } 116 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 } 117 118 #define ZTI_N(n) ZTI_P(n, 1) 119 #define ZTI_ONE ZTI_N(1) 120 121 typedef struct zio_taskq_info { 122 zti_modes_t zti_mode; 123 uint_t zti_value; 124 uint_t zti_count; 125 } zio_taskq_info_t; 126 127 static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = { 128 "issue", "issue_high", "intr", "intr_high" 129 }; 130 131 /* 132 * This table defines the taskq settings for each ZFS I/O type. When 133 * initializing a pool, we use this table to create an appropriately sized 134 * taskq. Some operations are low volume and therefore have a small, static 135 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE 136 * macros. Other operations process a large amount of data; the ZTI_BATCH 137 * macro causes us to create a taskq oriented for throughput. Some operations 138 * are so high frequency and short-lived that the taskq itself can become a 139 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an 140 * additional degree of parallelism specified by the number of threads per- 141 * taskq and the number of taskqs; when dispatching an event in this case, the 142 * particular taskq is chosen at random. 143 * 144 * The different taskq priorities are to handle the different contexts (issue 145 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that 146 * need to be handled with minimum delay. 147 */ 148 const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = { 149 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */ 150 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* NULL */ 151 { ZTI_N(8), ZTI_NULL, ZTI_P(12, 8), ZTI_NULL }, /* READ */ 152 { ZTI_BATCH, ZTI_N(5), ZTI_N(8), ZTI_N(5) }, /* WRITE */ 153 { ZTI_P(12, 8), ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* FREE */ 154 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* CLAIM */ 155 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* IOCTL */ 156 { ZTI_N(4), ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* TRIM */ 157 }; 158 159 static void spa_sync_version(void *arg, dmu_tx_t *tx); 160 static void spa_sync_props(void *arg, dmu_tx_t *tx); 161 static boolean_t spa_has_active_shared_spare(spa_t *spa); 162 static int spa_load_impl(spa_t *spa, spa_import_type_t type, char **ereport); 163 static void spa_vdev_resilver_done(spa_t *spa); 164 165 uint_t zio_taskq_batch_pct = 75; /* 1 thread per cpu in pset */ 166 id_t zio_taskq_psrset_bind = PS_NONE; 167 boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */ 168 uint_t zio_taskq_basedc = 80; /* base duty cycle */ 169 170 boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */ 171 extern int zfs_sync_pass_deferred_free; 172 173 /* 174 * Report any spa_load_verify errors found, but do not fail spa_load. 175 * This is used by zdb to analyze non-idle pools. 176 */ 177 boolean_t spa_load_verify_dryrun = B_FALSE; 178 179 /* 180 * This (illegal) pool name is used when temporarily importing a spa_t in order 181 * to get the vdev stats associated with the imported devices. 182 */ 183 #define TRYIMPORT_NAME "$import" 184 185 /* 186 * For debugging purposes: print out vdev tree during pool import. 187 */ 188 boolean_t spa_load_print_vdev_tree = B_FALSE; 189 190 /* 191 * A non-zero value for zfs_max_missing_tvds means that we allow importing 192 * pools with missing top-level vdevs. This is strictly intended for advanced 193 * pool recovery cases since missing data is almost inevitable. Pools with 194 * missing devices can only be imported read-only for safety reasons, and their 195 * fail-mode will be automatically set to "continue". 196 * 197 * With 1 missing vdev we should be able to import the pool and mount all 198 * datasets. User data that was not modified after the missing device has been 199 * added should be recoverable. This means that snapshots created prior to the 200 * addition of that device should be completely intact. 201 * 202 * With 2 missing vdevs, some datasets may fail to mount since there are 203 * dataset statistics that are stored as regular metadata. Some data might be 204 * recoverable if those vdevs were added recently. 205 * 206 * With 3 or more missing vdevs, the pool is severely damaged and MOS entries 207 * may be missing entirely. Chances of data recovery are very low. Note that 208 * there are also risks of performing an inadvertent rewind as we might be 209 * missing all the vdevs with the latest uberblocks. 210 */ 211 uint64_t zfs_max_missing_tvds = 0; 212 213 /* 214 * The parameters below are similar to zfs_max_missing_tvds but are only 215 * intended for a preliminary open of the pool with an untrusted config which 216 * might be incomplete or out-dated. 217 * 218 * We are more tolerant for pools opened from a cachefile since we could have 219 * an out-dated cachefile where a device removal was not registered. 220 * We could have set the limit arbitrarily high but in the case where devices 221 * are really missing we would want to return the proper error codes; we chose 222 * SPA_DVAS_PER_BP - 1 so that some copies of the MOS would still be available 223 * and we get a chance to retrieve the trusted config. 224 */ 225 uint64_t zfs_max_missing_tvds_cachefile = SPA_DVAS_PER_BP - 1; 226 227 /* 228 * In the case where config was assembled by scanning device paths (/dev/dsks 229 * by default) we are less tolerant since all the existing devices should have 230 * been detected and we want spa_load to return the right error codes. 231 */ 232 uint64_t zfs_max_missing_tvds_scan = 0; 233 234 /* 235 * Interval in seconds at which to poll spare vdevs for health. 236 * Setting this to zero disables spare polling. 237 * Set to three hours by default. 238 */ 239 uint_t spa_spare_poll_interval_seconds = 60 * 60 * 3; 240 241 /* 242 * Debugging aid that pauses spa_sync() towards the end. 243 */ 244 boolean_t zfs_pause_spa_sync = B_FALSE; 245 246 /* 247 * ========================================================================== 248 * SPA properties routines 249 * ========================================================================== 250 */ 251 252 /* 253 * Add a (source=src, propname=propval) list to an nvlist. 254 */ 255 static void 256 spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval, 257 uint64_t intval, zprop_source_t src) 258 { 259 const char *propname = zpool_prop_to_name(prop); 260 nvlist_t *propval; 261 262 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); 263 VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0); 264 265 if (strval != NULL) 266 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0); 267 else 268 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0); 269 270 VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0); 271 nvlist_free(propval); 272 } 273 274 /* 275 * Get property values from the spa configuration. 276 */ 277 static void 278 spa_prop_get_config(spa_t *spa, nvlist_t **nvp) 279 { 280 vdev_t *rvd = spa->spa_root_vdev; 281 dsl_pool_t *pool = spa->spa_dsl_pool; 282 uint64_t size, alloc, cap, version; 283 zprop_source_t src = ZPROP_SRC_NONE; 284 spa_config_dirent_t *dp; 285 metaslab_class_t *mc = spa_normal_class(spa); 286 287 ASSERT(MUTEX_HELD(&spa->spa_props_lock)); 288 289 if (rvd != NULL) { 290 alloc = metaslab_class_get_alloc(mc); 291 alloc += metaslab_class_get_alloc(spa_special_class(spa)); 292 alloc += metaslab_class_get_alloc(spa_dedup_class(spa)); 293 294 size = metaslab_class_get_space(mc); 295 size += metaslab_class_get_space(spa_special_class(spa)); 296 size += metaslab_class_get_space(spa_dedup_class(spa)); 297 298 spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src); 299 spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src); 300 spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src); 301 spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL, 302 size - alloc, src); 303 spa_prop_add_list(*nvp, ZPOOL_PROP_CHECKPOINT, NULL, 304 spa->spa_checkpoint_info.sci_dspace, src); 305 306 spa_prop_add_list(*nvp, ZPOOL_PROP_FRAGMENTATION, NULL, 307 metaslab_class_fragmentation(mc), src); 308 spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL, 309 metaslab_class_expandable_space(mc), src); 310 spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL, 311 (spa_mode(spa) == FREAD), src); 312 313 cap = (size == 0) ? 0 : (alloc * 100 / size); 314 spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src); 315 316 spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL, 317 ddt_get_pool_dedup_ratio(spa), src); 318 319 spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL, 320 rvd->vdev_state, src); 321 322 version = spa_version(spa); 323 if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) 324 src = ZPROP_SRC_DEFAULT; 325 else 326 src = ZPROP_SRC_LOCAL; 327 spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src); 328 } 329 330 if (pool != NULL) { 331 /* 332 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS, 333 * when opening pools before this version freedir will be NULL. 334 */ 335 if (pool->dp_free_dir != NULL) { 336 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL, 337 dsl_dir_phys(pool->dp_free_dir)->dd_used_bytes, 338 src); 339 } else { 340 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, 341 NULL, 0, src); 342 } 343 344 if (pool->dp_leak_dir != NULL) { 345 spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, NULL, 346 dsl_dir_phys(pool->dp_leak_dir)->dd_used_bytes, 347 src); 348 } else { 349 spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, 350 NULL, 0, src); 351 } 352 } 353 354 spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src); 355 356 if (spa->spa_comment != NULL) { 357 spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment, 358 0, ZPROP_SRC_LOCAL); 359 } 360 361 if (spa->spa_root != NULL) 362 spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root, 363 0, ZPROP_SRC_LOCAL); 364 365 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) { 366 spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL, 367 MIN(zfs_max_recordsize, SPA_MAXBLOCKSIZE), ZPROP_SRC_NONE); 368 } else { 369 spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL, 370 SPA_OLD_MAXBLOCKSIZE, ZPROP_SRC_NONE); 371 } 372 373 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_DNODE)) { 374 spa_prop_add_list(*nvp, ZPOOL_PROP_MAXDNODESIZE, NULL, 375 DNODE_MAX_SIZE, ZPROP_SRC_NONE); 376 } else { 377 spa_prop_add_list(*nvp, ZPOOL_PROP_MAXDNODESIZE, NULL, 378 DNODE_MIN_SIZE, ZPROP_SRC_NONE); 379 } 380 381 if ((dp = list_head(&spa->spa_config_list)) != NULL) { 382 if (dp->scd_path == NULL) { 383 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 384 "none", 0, ZPROP_SRC_LOCAL); 385 } else if (strcmp(dp->scd_path, spa_config_path) != 0) { 386 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, 387 dp->scd_path, 0, ZPROP_SRC_LOCAL); 388 } 389 } 390 } 391 392 /* 393 * Get zpool property values. 394 */ 395 int 396 spa_prop_get(spa_t *spa, nvlist_t **nvp) 397 { 398 objset_t *mos = spa->spa_meta_objset; 399 zap_cursor_t zc; 400 zap_attribute_t za; 401 int err; 402 403 VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0); 404 405 mutex_enter(&spa->spa_props_lock); 406 407 /* 408 * Get properties from the spa config. 409 */ 410 spa_prop_get_config(spa, nvp); 411 412 /* If no pool property object, no more prop to get. */ 413 if (mos == NULL || spa->spa_pool_props_object == 0) { 414 mutex_exit(&spa->spa_props_lock); 415 return (0); 416 } 417 418 /* 419 * Get properties from the MOS pool property object. 420 */ 421 for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); 422 (err = zap_cursor_retrieve(&zc, &za)) == 0; 423 zap_cursor_advance(&zc)) { 424 uint64_t intval = 0; 425 char *strval = NULL; 426 zprop_source_t src = ZPROP_SRC_DEFAULT; 427 zpool_prop_t prop; 428 429 if ((prop = zpool_name_to_prop(za.za_name)) == ZPOOL_PROP_INVAL) 430 continue; 431 432 switch (za.za_integer_length) { 433 case 8: 434 /* integer property */ 435 if (za.za_first_integer != 436 zpool_prop_default_numeric(prop)) 437 src = ZPROP_SRC_LOCAL; 438 439 if (prop == ZPOOL_PROP_BOOTFS) { 440 dsl_pool_t *dp; 441 dsl_dataset_t *ds = NULL; 442 443 dp = spa_get_dsl(spa); 444 dsl_pool_config_enter(dp, FTAG); 445 err = dsl_dataset_hold_obj(dp, 446 za.za_first_integer, FTAG, &ds); 447 if (err != 0) { 448 dsl_pool_config_exit(dp, FTAG); 449 break; 450 } 451 452 strval = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, 453 KM_SLEEP); 454 dsl_dataset_name(ds, strval); 455 dsl_dataset_rele(ds, FTAG); 456 dsl_pool_config_exit(dp, FTAG); 457 } else { 458 strval = NULL; 459 intval = za.za_first_integer; 460 } 461 462 spa_prop_add_list(*nvp, prop, strval, intval, src); 463 464 if (strval != NULL) 465 kmem_free(strval, ZFS_MAX_DATASET_NAME_LEN); 466 467 break; 468 469 case 1: 470 /* string property */ 471 strval = kmem_alloc(za.za_num_integers, KM_SLEEP); 472 err = zap_lookup(mos, spa->spa_pool_props_object, 473 za.za_name, 1, za.za_num_integers, strval); 474 if (err) { 475 kmem_free(strval, za.za_num_integers); 476 break; 477 } 478 spa_prop_add_list(*nvp, prop, strval, 0, src); 479 kmem_free(strval, za.za_num_integers); 480 break; 481 482 default: 483 break; 484 } 485 } 486 zap_cursor_fini(&zc); 487 mutex_exit(&spa->spa_props_lock); 488 if (err && err != ENOENT) { 489 nvlist_free(*nvp); 490 *nvp = NULL; 491 return (err); 492 } 493 494 return (0); 495 } 496 497 /* 498 * Validate the given pool properties nvlist and modify the list 499 * for the property values to be set. 500 */ 501 static int 502 spa_prop_validate(spa_t *spa, nvlist_t *props) 503 { 504 nvpair_t *elem; 505 int error = 0, reset_bootfs = 0; 506 uint64_t objnum = 0; 507 boolean_t has_feature = B_FALSE; 508 509 elem = NULL; 510 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 511 uint64_t intval; 512 char *strval, *slash, *check, *fname; 513 const char *propname = nvpair_name(elem); 514 zpool_prop_t prop = zpool_name_to_prop(propname); 515 516 switch (prop) { 517 case ZPOOL_PROP_INVAL: 518 if (!zpool_prop_feature(propname)) { 519 error = SET_ERROR(EINVAL); 520 break; 521 } 522 523 /* 524 * Sanitize the input. 525 */ 526 if (nvpair_type(elem) != DATA_TYPE_UINT64) { 527 error = SET_ERROR(EINVAL); 528 break; 529 } 530 531 if (nvpair_value_uint64(elem, &intval) != 0) { 532 error = SET_ERROR(EINVAL); 533 break; 534 } 535 536 if (intval != 0) { 537 error = SET_ERROR(EINVAL); 538 break; 539 } 540 541 fname = strchr(propname, '@') + 1; 542 if (zfeature_lookup_name(fname, NULL) != 0) { 543 error = SET_ERROR(EINVAL); 544 break; 545 } 546 547 has_feature = B_TRUE; 548 break; 549 550 case ZPOOL_PROP_VERSION: 551 error = nvpair_value_uint64(elem, &intval); 552 if (!error && 553 (intval < spa_version(spa) || 554 intval > SPA_VERSION_BEFORE_FEATURES || 555 has_feature)) 556 error = SET_ERROR(EINVAL); 557 break; 558 559 case ZPOOL_PROP_DELEGATION: 560 case ZPOOL_PROP_AUTOREPLACE: 561 case ZPOOL_PROP_LISTSNAPS: 562 case ZPOOL_PROP_AUTOEXPAND: 563 case ZPOOL_PROP_AUTOTRIM: 564 error = nvpair_value_uint64(elem, &intval); 565 if (!error && intval > 1) 566 error = SET_ERROR(EINVAL); 567 break; 568 569 case ZPOOL_PROP_MULTIHOST: 570 error = nvpair_value_uint64(elem, &intval); 571 if (!error && intval > 1) 572 error = SET_ERROR(EINVAL); 573 574 if (!error && !spa_get_hostid()) 575 error = SET_ERROR(ENOTSUP); 576 577 break; 578 579 case ZPOOL_PROP_BOOTFS: 580 /* 581 * If the pool version is less than SPA_VERSION_BOOTFS, 582 * or the pool is still being created (version == 0), 583 * the bootfs property cannot be set. 584 */ 585 if (spa_version(spa) < SPA_VERSION_BOOTFS) { 586 error = SET_ERROR(ENOTSUP); 587 break; 588 } 589 590 /* 591 * Make sure the vdev config is bootable 592 */ 593 if (!vdev_is_bootable(spa->spa_root_vdev)) { 594 error = SET_ERROR(ENOTSUP); 595 break; 596 } 597 598 reset_bootfs = 1; 599 600 error = nvpair_value_string(elem, &strval); 601 602 if (!error) { 603 objset_t *os; 604 uint64_t propval; 605 606 if (strval == NULL || strval[0] == '\0') { 607 objnum = zpool_prop_default_numeric( 608 ZPOOL_PROP_BOOTFS); 609 break; 610 } 611 612 error = dmu_objset_hold(strval, FTAG, &os); 613 if (error != 0) 614 break; 615 616 /* 617 * Must be ZPL, and its property settings 618 * must be supported. 619 */ 620 621 if (dmu_objset_type(os) != DMU_OST_ZFS) { 622 error = SET_ERROR(ENOTSUP); 623 } else if ((error = 624 dsl_prop_get_int_ds(dmu_objset_ds(os), 625 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 626 &propval)) == 0 && 627 !BOOTFS_COMPRESS_VALID(propval)) { 628 error = SET_ERROR(ENOTSUP); 629 } else { 630 objnum = dmu_objset_id(os); 631 } 632 dmu_objset_rele(os, FTAG); 633 } 634 break; 635 636 case ZPOOL_PROP_FAILUREMODE: 637 error = nvpair_value_uint64(elem, &intval); 638 if (!error && (intval < ZIO_FAILURE_MODE_WAIT || 639 intval > ZIO_FAILURE_MODE_PANIC)) 640 error = SET_ERROR(EINVAL); 641 642 /* 643 * This is a special case which only occurs when 644 * the pool has completely failed. This allows 645 * the user to change the in-core failmode property 646 * without syncing it out to disk (I/Os might 647 * currently be blocked). We do this by returning 648 * EIO to the caller (spa_prop_set) to trick it 649 * into thinking we encountered a property validation 650 * error. 651 */ 652 if (!error && spa_suspended(spa)) { 653 spa->spa_failmode = intval; 654 error = SET_ERROR(EIO); 655 } 656 break; 657 658 case ZPOOL_PROP_CACHEFILE: 659 if ((error = nvpair_value_string(elem, &strval)) != 0) 660 break; 661 662 if (strval[0] == '\0') 663 break; 664 665 if (strcmp(strval, "none") == 0) 666 break; 667 668 if (strval[0] != '/') { 669 error = SET_ERROR(EINVAL); 670 break; 671 } 672 673 slash = strrchr(strval, '/'); 674 ASSERT(slash != NULL); 675 676 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 677 strcmp(slash, "/..") == 0) 678 error = SET_ERROR(EINVAL); 679 break; 680 681 case ZPOOL_PROP_COMMENT: 682 if ((error = nvpair_value_string(elem, &strval)) != 0) 683 break; 684 for (check = strval; *check != '\0'; check++) { 685 /* 686 * The kernel doesn't have an easy isprint() 687 * check. For this kernel check, we merely 688 * check ASCII apart from DEL. Fix this if 689 * there is an easy-to-use kernel isprint(). 690 */ 691 if (*check >= 0x7f) { 692 error = SET_ERROR(EINVAL); 693 break; 694 } 695 } 696 if (strlen(strval) > ZPROP_MAX_COMMENT) 697 error = E2BIG; 698 break; 699 700 case ZPOOL_PROP_DEDUPDITTO: 701 if (spa_version(spa) < SPA_VERSION_DEDUP) 702 error = SET_ERROR(ENOTSUP); 703 else 704 error = nvpair_value_uint64(elem, &intval); 705 if (error == 0 && 706 intval != 0 && intval < ZIO_DEDUPDITTO_MIN) 707 error = SET_ERROR(EINVAL); 708 break; 709 } 710 711 if (error) 712 break; 713 } 714 715 if (!error && reset_bootfs) { 716 error = nvlist_remove(props, 717 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); 718 719 if (!error) { 720 error = nvlist_add_uint64(props, 721 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); 722 } 723 } 724 725 return (error); 726 } 727 728 void 729 spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync) 730 { 731 char *cachefile; 732 spa_config_dirent_t *dp; 733 734 if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), 735 &cachefile) != 0) 736 return; 737 738 dp = kmem_alloc(sizeof (spa_config_dirent_t), 739 KM_SLEEP); 740 741 if (cachefile[0] == '\0') 742 dp->scd_path = spa_strdup(spa_config_path); 743 else if (strcmp(cachefile, "none") == 0) 744 dp->scd_path = NULL; 745 else 746 dp->scd_path = spa_strdup(cachefile); 747 748 list_insert_head(&spa->spa_config_list, dp); 749 if (need_sync) 750 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 751 } 752 753 int 754 spa_prop_set(spa_t *spa, nvlist_t *nvp) 755 { 756 int error; 757 nvpair_t *elem = NULL; 758 boolean_t need_sync = B_FALSE; 759 760 if ((error = spa_prop_validate(spa, nvp)) != 0) 761 return (error); 762 763 while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) { 764 zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem)); 765 766 if (prop == ZPOOL_PROP_CACHEFILE || 767 prop == ZPOOL_PROP_ALTROOT || 768 prop == ZPOOL_PROP_READONLY) 769 continue; 770 771 if (prop == ZPOOL_PROP_VERSION || prop == ZPOOL_PROP_INVAL) { 772 uint64_t ver; 773 774 if (prop == ZPOOL_PROP_VERSION) { 775 VERIFY(nvpair_value_uint64(elem, &ver) == 0); 776 } else { 777 ASSERT(zpool_prop_feature(nvpair_name(elem))); 778 ver = SPA_VERSION_FEATURES; 779 need_sync = B_TRUE; 780 } 781 782 /* Save time if the version is already set. */ 783 if (ver == spa_version(spa)) 784 continue; 785 786 /* 787 * In addition to the pool directory object, we might 788 * create the pool properties object, the features for 789 * read object, the features for write object, or the 790 * feature descriptions object. 791 */ 792 error = dsl_sync_task(spa->spa_name, NULL, 793 spa_sync_version, &ver, 794 6, ZFS_SPACE_CHECK_RESERVED); 795 if (error) 796 return (error); 797 continue; 798 } 799 800 need_sync = B_TRUE; 801 break; 802 } 803 804 if (need_sync) { 805 return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props, 806 nvp, 6, ZFS_SPACE_CHECK_RESERVED)); 807 } 808 809 return (0); 810 } 811 812 /* 813 * If the bootfs property value is dsobj, clear it. 814 */ 815 void 816 spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) 817 { 818 if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { 819 VERIFY(zap_remove(spa->spa_meta_objset, 820 spa->spa_pool_props_object, 821 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); 822 spa->spa_bootfs = 0; 823 } 824 } 825 826 /*ARGSUSED*/ 827 static int 828 spa_change_guid_check(void *arg, dmu_tx_t *tx) 829 { 830 uint64_t *newguid = arg; 831 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 832 vdev_t *rvd = spa->spa_root_vdev; 833 uint64_t vdev_state; 834 835 if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { 836 int error = (spa_has_checkpoint(spa)) ? 837 ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; 838 return (SET_ERROR(error)); 839 } 840 841 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 842 vdev_state = rvd->vdev_state; 843 spa_config_exit(spa, SCL_STATE, FTAG); 844 845 if (vdev_state != VDEV_STATE_HEALTHY) 846 return (SET_ERROR(ENXIO)); 847 848 ASSERT3U(spa_guid(spa), !=, *newguid); 849 850 return (0); 851 } 852 853 static void 854 spa_change_guid_sync(void *arg, dmu_tx_t *tx) 855 { 856 uint64_t *newguid = arg; 857 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 858 uint64_t oldguid; 859 vdev_t *rvd = spa->spa_root_vdev; 860 861 oldguid = spa_guid(spa); 862 863 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 864 rvd->vdev_guid = *newguid; 865 rvd->vdev_guid_sum += (*newguid - oldguid); 866 vdev_config_dirty(rvd); 867 spa_config_exit(spa, SCL_STATE, FTAG); 868 869 spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu", 870 oldguid, *newguid); 871 } 872 873 /* 874 * Change the GUID for the pool. This is done so that we can later 875 * re-import a pool built from a clone of our own vdevs. We will modify 876 * the root vdev's guid, our own pool guid, and then mark all of our 877 * vdevs dirty. Note that we must make sure that all our vdevs are 878 * online when we do this, or else any vdevs that weren't present 879 * would be orphaned from our pool. We are also going to issue a 880 * sysevent to update any watchers. 881 */ 882 int 883 spa_change_guid(spa_t *spa) 884 { 885 int error; 886 uint64_t guid; 887 888 mutex_enter(&spa->spa_vdev_top_lock); 889 mutex_enter(&spa_namespace_lock); 890 guid = spa_generate_guid(NULL); 891 892 error = dsl_sync_task(spa->spa_name, spa_change_guid_check, 893 spa_change_guid_sync, &guid, 5, ZFS_SPACE_CHECK_RESERVED); 894 895 if (error == 0) { 896 spa_write_cachefile(spa, B_FALSE, B_TRUE); 897 spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_REGUID); 898 } 899 900 mutex_exit(&spa_namespace_lock); 901 mutex_exit(&spa->spa_vdev_top_lock); 902 903 return (error); 904 } 905 906 /* 907 * ========================================================================== 908 * SPA state manipulation (open/create/destroy/import/export) 909 * ========================================================================== 910 */ 911 912 static int 913 spa_error_entry_compare(const void *a, const void *b) 914 { 915 const spa_error_entry_t *sa = (const spa_error_entry_t *)a; 916 const spa_error_entry_t *sb = (const spa_error_entry_t *)b; 917 int ret; 918 919 ret = memcmp(&sa->se_bookmark, &sb->se_bookmark, 920 sizeof (zbookmark_phys_t)); 921 922 return (TREE_ISIGN(ret)); 923 } 924 925 /* 926 * Utility function which retrieves copies of the current logs and 927 * re-initializes them in the process. 928 */ 929 void 930 spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) 931 { 932 ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); 933 934 bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); 935 bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); 936 937 avl_create(&spa->spa_errlist_scrub, 938 spa_error_entry_compare, sizeof (spa_error_entry_t), 939 offsetof(spa_error_entry_t, se_avl)); 940 avl_create(&spa->spa_errlist_last, 941 spa_error_entry_compare, sizeof (spa_error_entry_t), 942 offsetof(spa_error_entry_t, se_avl)); 943 } 944 945 static void 946 spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q) 947 { 948 const zio_taskq_info_t *ztip = &zio_taskqs[t][q]; 949 enum zti_modes mode = ztip->zti_mode; 950 uint_t value = ztip->zti_value; 951 uint_t count = ztip->zti_count; 952 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 953 char name[32]; 954 uint_t flags = 0; 955 boolean_t batch = B_FALSE; 956 957 if (mode == ZTI_MODE_NULL) { 958 tqs->stqs_count = 0; 959 tqs->stqs_taskq = NULL; 960 return; 961 } 962 963 ASSERT3U(count, >, 0); 964 965 tqs->stqs_count = count; 966 tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP); 967 968 switch (mode) { 969 case ZTI_MODE_FIXED: 970 ASSERT3U(value, >=, 1); 971 value = MAX(value, 1); 972 break; 973 974 case ZTI_MODE_BATCH: 975 batch = B_TRUE; 976 flags |= TASKQ_THREADS_CPU_PCT; 977 value = zio_taskq_batch_pct; 978 break; 979 980 default: 981 panic("unrecognized mode for %s_%s taskq (%u:%u) in " 982 "spa_activate()", 983 zio_type_name[t], zio_taskq_types[q], mode, value); 984 break; 985 } 986 987 for (uint_t i = 0; i < count; i++) { 988 taskq_t *tq; 989 990 if (count > 1) { 991 (void) snprintf(name, sizeof (name), "%s_%s_%u", 992 zio_type_name[t], zio_taskq_types[q], i); 993 } else { 994 (void) snprintf(name, sizeof (name), "%s_%s", 995 zio_type_name[t], zio_taskq_types[q]); 996 } 997 998 if (zio_taskq_sysdc && spa->spa_proc != &p0) { 999 if (batch) 1000 flags |= TASKQ_DC_BATCH; 1001 1002 tq = taskq_create_sysdc(name, value, 50, INT_MAX, 1003 spa->spa_proc, zio_taskq_basedc, flags); 1004 } else { 1005 pri_t pri = maxclsyspri; 1006 /* 1007 * The write issue taskq can be extremely CPU 1008 * intensive. Run it at slightly lower priority 1009 * than the other taskqs. 1010 */ 1011 if (t == ZIO_TYPE_WRITE && q == ZIO_TASKQ_ISSUE) 1012 pri--; 1013 1014 tq = taskq_create_proc(name, value, pri, 50, 1015 INT_MAX, spa->spa_proc, flags); 1016 } 1017 1018 tqs->stqs_taskq[i] = tq; 1019 } 1020 } 1021 1022 static void 1023 spa_taskqs_fini(spa_t *spa, zio_type_t t, zio_taskq_type_t q) 1024 { 1025 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 1026 1027 if (tqs->stqs_taskq == NULL) { 1028 ASSERT0(tqs->stqs_count); 1029 return; 1030 } 1031 1032 for (uint_t i = 0; i < tqs->stqs_count; i++) { 1033 ASSERT3P(tqs->stqs_taskq[i], !=, NULL); 1034 taskq_destroy(tqs->stqs_taskq[i]); 1035 } 1036 1037 kmem_free(tqs->stqs_taskq, tqs->stqs_count * sizeof (taskq_t *)); 1038 tqs->stqs_taskq = NULL; 1039 } 1040 1041 /* 1042 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority. 1043 * Note that a type may have multiple discrete taskqs to avoid lock contention 1044 * on the taskq itself. In that case we choose which taskq at random by using 1045 * the low bits of gethrtime(). 1046 */ 1047 void 1048 spa_taskq_dispatch_ent(spa_t *spa, zio_type_t t, zio_taskq_type_t q, 1049 task_func_t *func, void *arg, uint_t flags, taskq_ent_t *ent) 1050 { 1051 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 1052 taskq_t *tq; 1053 1054 ASSERT3P(tqs->stqs_taskq, !=, NULL); 1055 ASSERT3U(tqs->stqs_count, !=, 0); 1056 1057 if (tqs->stqs_count == 1) { 1058 tq = tqs->stqs_taskq[0]; 1059 } else { 1060 tq = tqs->stqs_taskq[gethrtime() % tqs->stqs_count]; 1061 } 1062 1063 taskq_dispatch_ent(tq, func, arg, flags, ent); 1064 } 1065 1066 static void 1067 spa_create_zio_taskqs(spa_t *spa) 1068 { 1069 for (int t = 0; t < ZIO_TYPES; t++) { 1070 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 1071 spa_taskqs_init(spa, t, q); 1072 } 1073 } 1074 } 1075 1076 #ifdef _KERNEL 1077 static void 1078 spa_thread(void *arg) 1079 { 1080 callb_cpr_t cprinfo; 1081 spa_t *spa = arg; 1082 char spa_id_readable[CB_MAXNAME + 1]; 1083 user_t *pu = PTOU(curproc); 1084 1085 (void) snprintf(spa_id_readable, sizeof (spa_id_readable), "SPA:0x%p", 1086 spa); 1087 1088 CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr, 1089 spa_id_readable); 1090 1091 ASSERT(curproc != &p0); 1092 (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs), 1093 "zpool-%s", spa->spa_name); 1094 (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm)); 1095 1096 /* bind this thread to the requested psrset */ 1097 if (zio_taskq_psrset_bind != PS_NONE) { 1098 pool_lock(); 1099 mutex_enter(&cpu_lock); 1100 mutex_enter(&pidlock); 1101 mutex_enter(&curproc->p_lock); 1102 1103 if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind, 1104 0, NULL, NULL) == 0) { 1105 curthread->t_bind_pset = zio_taskq_psrset_bind; 1106 } else { 1107 cmn_err(CE_WARN, 1108 "Couldn't bind process for zfs pool \"%s\" to " 1109 "pset %d\n", spa->spa_name, zio_taskq_psrset_bind); 1110 } 1111 1112 mutex_exit(&curproc->p_lock); 1113 mutex_exit(&pidlock); 1114 mutex_exit(&cpu_lock); 1115 pool_unlock(); 1116 } 1117 1118 if (zio_taskq_sysdc) { 1119 sysdc_thread_enter(curthread, 100, 0); 1120 } 1121 1122 spa->spa_proc = curproc; 1123 spa->spa_did = curthread->t_did; 1124 1125 spa_create_zio_taskqs(spa); 1126 1127 mutex_enter(&spa->spa_proc_lock); 1128 ASSERT(spa->spa_proc_state == SPA_PROC_CREATED); 1129 1130 spa->spa_proc_state = SPA_PROC_ACTIVE; 1131 cv_broadcast(&spa->spa_proc_cv); 1132 1133 CALLB_CPR_SAFE_BEGIN(&cprinfo); 1134 while (spa->spa_proc_state == SPA_PROC_ACTIVE) 1135 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); 1136 CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock); 1137 1138 ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE); 1139 spa->spa_proc_state = SPA_PROC_GONE; 1140 spa->spa_proc = &p0; 1141 cv_broadcast(&spa->spa_proc_cv); 1142 CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */ 1143 1144 mutex_enter(&curproc->p_lock); 1145 lwp_exit(); 1146 } 1147 #endif 1148 1149 /* 1150 * Activate an uninitialized pool. 1151 */ 1152 static void 1153 spa_activate(spa_t *spa, int mode) 1154 { 1155 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 1156 1157 spa->spa_state = POOL_STATE_ACTIVE; 1158 spa->spa_mode = mode; 1159 1160 spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops); 1161 spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops); 1162 spa->spa_special_class = metaslab_class_create(spa, zfs_metaslab_ops); 1163 spa->spa_dedup_class = metaslab_class_create(spa, zfs_metaslab_ops); 1164 1165 /* Try to create a covering process */ 1166 mutex_enter(&spa->spa_proc_lock); 1167 ASSERT(spa->spa_proc_state == SPA_PROC_NONE); 1168 ASSERT(spa->spa_proc == &p0); 1169 spa->spa_did = 0; 1170 1171 /* Only create a process if we're going to be around a while. */ 1172 if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) { 1173 if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri, 1174 NULL, 0) == 0) { 1175 spa->spa_proc_state = SPA_PROC_CREATED; 1176 while (spa->spa_proc_state == SPA_PROC_CREATED) { 1177 cv_wait(&spa->spa_proc_cv, 1178 &spa->spa_proc_lock); 1179 } 1180 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); 1181 ASSERT(spa->spa_proc != &p0); 1182 ASSERT(spa->spa_did != 0); 1183 } else { 1184 #ifdef _KERNEL 1185 cmn_err(CE_WARN, 1186 "Couldn't create process for zfs pool \"%s\"\n", 1187 spa->spa_name); 1188 #endif 1189 } 1190 } 1191 mutex_exit(&spa->spa_proc_lock); 1192 1193 /* If we didn't create a process, we need to create our taskqs. */ 1194 if (spa->spa_proc == &p0) { 1195 spa_create_zio_taskqs(spa); 1196 } 1197 1198 for (size_t i = 0; i < TXG_SIZE; i++) { 1199 spa->spa_txg_zio[i] = zio_root(spa, NULL, NULL, 1200 ZIO_FLAG_CANFAIL); 1201 } 1202 1203 list_create(&spa->spa_config_dirty_list, sizeof (vdev_t), 1204 offsetof(vdev_t, vdev_config_dirty_node)); 1205 list_create(&spa->spa_evicting_os_list, sizeof (objset_t), 1206 offsetof(objset_t, os_evicting_node)); 1207 list_create(&spa->spa_state_dirty_list, sizeof (vdev_t), 1208 offsetof(vdev_t, vdev_state_dirty_node)); 1209 1210 txg_list_create(&spa->spa_vdev_txg_list, spa, 1211 offsetof(struct vdev, vdev_txg_node)); 1212 1213 avl_create(&spa->spa_errlist_scrub, 1214 spa_error_entry_compare, sizeof (spa_error_entry_t), 1215 offsetof(spa_error_entry_t, se_avl)); 1216 avl_create(&spa->spa_errlist_last, 1217 spa_error_entry_compare, sizeof (spa_error_entry_t), 1218 offsetof(spa_error_entry_t, se_avl)); 1219 1220 spa_keystore_init(&spa->spa_keystore); 1221 1222 /* 1223 * The taskq to upgrade datasets in this pool. Currently used by 1224 * feature SPA_FEATURE_USEROBJ_ACCOUNTING/SPA_FEATURE_PROJECT_QUOTA. 1225 */ 1226 spa->spa_upgrade_taskq = taskq_create("z_upgrade", boot_ncpus, 1227 minclsyspri, 1, INT_MAX, TASKQ_DYNAMIC); 1228 } 1229 1230 /* 1231 * Opposite of spa_activate(). 1232 */ 1233 static void 1234 spa_deactivate(spa_t *spa) 1235 { 1236 ASSERT(spa->spa_sync_on == B_FALSE); 1237 ASSERT(spa->spa_dsl_pool == NULL); 1238 ASSERT(spa->spa_root_vdev == NULL); 1239 ASSERT(spa->spa_async_zio_root == NULL); 1240 ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); 1241 1242 spa_evicting_os_wait(spa); 1243 1244 if (spa->spa_upgrade_taskq) { 1245 taskq_destroy(spa->spa_upgrade_taskq); 1246 spa->spa_upgrade_taskq = NULL; 1247 } 1248 1249 txg_list_destroy(&spa->spa_vdev_txg_list); 1250 1251 list_destroy(&spa->spa_config_dirty_list); 1252 list_destroy(&spa->spa_evicting_os_list); 1253 list_destroy(&spa->spa_state_dirty_list); 1254 1255 for (int t = 0; t < ZIO_TYPES; t++) { 1256 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { 1257 spa_taskqs_fini(spa, t, q); 1258 } 1259 } 1260 1261 for (size_t i = 0; i < TXG_SIZE; i++) { 1262 ASSERT3P(spa->spa_txg_zio[i], !=, NULL); 1263 VERIFY0(zio_wait(spa->spa_txg_zio[i])); 1264 spa->spa_txg_zio[i] = NULL; 1265 } 1266 1267 metaslab_class_destroy(spa->spa_normal_class); 1268 spa->spa_normal_class = NULL; 1269 1270 metaslab_class_destroy(spa->spa_log_class); 1271 spa->spa_log_class = NULL; 1272 1273 metaslab_class_destroy(spa->spa_special_class); 1274 spa->spa_special_class = NULL; 1275 1276 metaslab_class_destroy(spa->spa_dedup_class); 1277 spa->spa_dedup_class = NULL; 1278 1279 /* 1280 * If this was part of an import or the open otherwise failed, we may 1281 * still have errors left in the queues. Empty them just in case. 1282 */ 1283 spa_errlog_drain(spa); 1284 avl_destroy(&spa->spa_errlist_scrub); 1285 avl_destroy(&spa->spa_errlist_last); 1286 1287 spa_keystore_fini(&spa->spa_keystore); 1288 1289 spa->spa_state = POOL_STATE_UNINITIALIZED; 1290 1291 mutex_enter(&spa->spa_proc_lock); 1292 if (spa->spa_proc_state != SPA_PROC_NONE) { 1293 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); 1294 spa->spa_proc_state = SPA_PROC_DEACTIVATE; 1295 cv_broadcast(&spa->spa_proc_cv); 1296 while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) { 1297 ASSERT(spa->spa_proc != &p0); 1298 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); 1299 } 1300 ASSERT(spa->spa_proc_state == SPA_PROC_GONE); 1301 spa->spa_proc_state = SPA_PROC_NONE; 1302 } 1303 ASSERT(spa->spa_proc == &p0); 1304 mutex_exit(&spa->spa_proc_lock); 1305 1306 /* 1307 * We want to make sure spa_thread() has actually exited the ZFS 1308 * module, so that the module can't be unloaded out from underneath 1309 * it. 1310 */ 1311 if (spa->spa_did != 0) { 1312 thread_join(spa->spa_did); 1313 spa->spa_did = 0; 1314 } 1315 } 1316 1317 /* 1318 * Verify a pool configuration, and construct the vdev tree appropriately. This 1319 * will create all the necessary vdevs in the appropriate layout, with each vdev 1320 * in the CLOSED state. This will prep the pool before open/creation/import. 1321 * All vdev validation is done by the vdev_alloc() routine. 1322 */ 1323 static int 1324 spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, 1325 uint_t id, int atype) 1326 { 1327 nvlist_t **child; 1328 uint_t children; 1329 int error; 1330 1331 if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) 1332 return (error); 1333 1334 if ((*vdp)->vdev_ops->vdev_op_leaf) 1335 return (0); 1336 1337 error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1338 &child, &children); 1339 1340 if (error == ENOENT) 1341 return (0); 1342 1343 if (error) { 1344 vdev_free(*vdp); 1345 *vdp = NULL; 1346 return (SET_ERROR(EINVAL)); 1347 } 1348 1349 for (int c = 0; c < children; c++) { 1350 vdev_t *vd; 1351 if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, 1352 atype)) != 0) { 1353 vdev_free(*vdp); 1354 *vdp = NULL; 1355 return (error); 1356 } 1357 } 1358 1359 ASSERT(*vdp != NULL); 1360 1361 return (0); 1362 } 1363 1364 static boolean_t 1365 spa_should_flush_logs_on_unload(spa_t *spa) 1366 { 1367 if (!spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP)) 1368 return (B_FALSE); 1369 1370 if (!spa_writeable(spa)) 1371 return (B_FALSE); 1372 1373 if (!spa->spa_sync_on) 1374 return (B_FALSE); 1375 1376 if (spa_state(spa) != POOL_STATE_EXPORTED) 1377 return (B_FALSE); 1378 1379 if (zfs_keep_log_spacemaps_at_export) 1380 return (B_FALSE); 1381 1382 return (B_TRUE); 1383 } 1384 1385 /* 1386 * Opens a transaction that will set the flag that will instruct 1387 * spa_sync to attempt to flush all the metaslabs for that txg. 1388 */ 1389 static void 1390 spa_unload_log_sm_flush_all(spa_t *spa) 1391 { 1392 dmu_tx_t *tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); 1393 1394 VERIFY0(dmu_tx_assign(tx, TXG_WAIT)); 1395 1396 ASSERT3U(spa->spa_log_flushall_txg, ==, 0); 1397 spa->spa_log_flushall_txg = dmu_tx_get_txg(tx); 1398 1399 dmu_tx_commit(tx); 1400 txg_wait_synced(spa_get_dsl(spa), spa->spa_log_flushall_txg); 1401 } 1402 1403 static void 1404 spa_unload_log_sm_metadata(spa_t *spa) 1405 { 1406 void *cookie = NULL; 1407 spa_log_sm_t *sls; 1408 1409 while ((sls = avl_destroy_nodes(&spa->spa_sm_logs_by_txg, 1410 &cookie)) != NULL) { 1411 VERIFY0(sls->sls_mscount); 1412 kmem_free(sls, sizeof (spa_log_sm_t)); 1413 } 1414 1415 for (log_summary_entry_t *e = list_head(&spa->spa_log_summary); 1416 e != NULL; e = list_head(&spa->spa_log_summary)) { 1417 VERIFY0(e->lse_mscount); 1418 list_remove(&spa->spa_log_summary, e); 1419 kmem_free(e, sizeof (log_summary_entry_t)); 1420 } 1421 1422 spa->spa_unflushed_stats.sus_nblocks = 0; 1423 spa->spa_unflushed_stats.sus_memused = 0; 1424 spa->spa_unflushed_stats.sus_blocklimit = 0; 1425 } 1426 1427 /* 1428 * Opposite of spa_load(). 1429 */ 1430 static void 1431 spa_unload(spa_t *spa) 1432 { 1433 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 1434 ASSERT(spa_state(spa) != POOL_STATE_UNINITIALIZED); 1435 1436 spa_import_progress_remove(spa); 1437 spa_load_note(spa, "UNLOADING"); 1438 1439 /* 1440 * If the log space map feature is enabled and the pool is getting 1441 * exported (but not destroyed), we want to spend some time flushing 1442 * as many metaslabs as we can in an attempt to destroy log space 1443 * maps and save import time. 1444 */ 1445 if (spa_should_flush_logs_on_unload(spa)) 1446 spa_unload_log_sm_flush_all(spa); 1447 1448 /* 1449 * Stop async tasks. 1450 */ 1451 spa_async_suspend(spa); 1452 1453 if (spa->spa_root_vdev) { 1454 vdev_t *root_vdev = spa->spa_root_vdev; 1455 vdev_initialize_stop_all(root_vdev, VDEV_INITIALIZE_ACTIVE); 1456 vdev_trim_stop_all(root_vdev, VDEV_TRIM_ACTIVE); 1457 vdev_autotrim_stop_all(spa); 1458 } 1459 1460 /* 1461 * Stop syncing. 1462 */ 1463 if (spa->spa_sync_on) { 1464 txg_sync_stop(spa->spa_dsl_pool); 1465 spa->spa_sync_on = B_FALSE; 1466 } 1467 1468 /* 1469 * This ensures that there is no async metaslab prefetching 1470 * while we attempt to unload the spa. 1471 */ 1472 if (spa->spa_root_vdev != NULL) { 1473 for (int c = 0; c < spa->spa_root_vdev->vdev_children; c++) { 1474 vdev_t *vc = spa->spa_root_vdev->vdev_child[c]; 1475 if (vc->vdev_mg != NULL) 1476 taskq_wait(vc->vdev_mg->mg_taskq); 1477 } 1478 } 1479 1480 if (spa->spa_mmp.mmp_thread) 1481 mmp_thread_stop(spa); 1482 1483 /* 1484 * Wait for any outstanding async I/O to complete. 1485 */ 1486 if (spa->spa_async_zio_root != NULL) { 1487 for (int i = 0; i < max_ncpus; i++) 1488 (void) zio_wait(spa->spa_async_zio_root[i]); 1489 kmem_free(spa->spa_async_zio_root, max_ncpus * sizeof (void *)); 1490 spa->spa_async_zio_root = NULL; 1491 } 1492 1493 if (spa->spa_vdev_removal != NULL) { 1494 spa_vdev_removal_destroy(spa->spa_vdev_removal); 1495 spa->spa_vdev_removal = NULL; 1496 } 1497 1498 if (spa->spa_condense_zthr != NULL) { 1499 zthr_destroy(spa->spa_condense_zthr); 1500 spa->spa_condense_zthr = NULL; 1501 } 1502 1503 if (spa->spa_checkpoint_discard_zthr != NULL) { 1504 zthr_destroy(spa->spa_checkpoint_discard_zthr); 1505 spa->spa_checkpoint_discard_zthr = NULL; 1506 } 1507 1508 spa_condense_fini(spa); 1509 1510 bpobj_close(&spa->spa_deferred_bpobj); 1511 1512 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER); 1513 1514 /* 1515 * Close all vdevs. 1516 */ 1517 if (spa->spa_root_vdev) 1518 vdev_free(spa->spa_root_vdev); 1519 ASSERT(spa->spa_root_vdev == NULL); 1520 1521 /* 1522 * Close the dsl pool. 1523 */ 1524 if (spa->spa_dsl_pool) { 1525 dsl_pool_close(spa->spa_dsl_pool); 1526 spa->spa_dsl_pool = NULL; 1527 spa->spa_meta_objset = NULL; 1528 } 1529 1530 ddt_unload(spa); 1531 spa_unload_log_sm_metadata(spa); 1532 1533 /* 1534 * Drop and purge level 2 cache 1535 */ 1536 spa_l2cache_drop(spa); 1537 1538 for (int i = 0; i < spa->spa_spares.sav_count; i++) 1539 vdev_free(spa->spa_spares.sav_vdevs[i]); 1540 if (spa->spa_spares.sav_vdevs) { 1541 kmem_free(spa->spa_spares.sav_vdevs, 1542 spa->spa_spares.sav_count * sizeof (void *)); 1543 spa->spa_spares.sav_vdevs = NULL; 1544 } 1545 if (spa->spa_spares.sav_config) { 1546 nvlist_free(spa->spa_spares.sav_config); 1547 spa->spa_spares.sav_config = NULL; 1548 } 1549 spa->spa_spares.sav_count = 0; 1550 1551 for (int i = 0; i < spa->spa_l2cache.sav_count; i++) { 1552 vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]); 1553 vdev_free(spa->spa_l2cache.sav_vdevs[i]); 1554 } 1555 if (spa->spa_l2cache.sav_vdevs) { 1556 kmem_free(spa->spa_l2cache.sav_vdevs, 1557 spa->spa_l2cache.sav_count * sizeof (void *)); 1558 spa->spa_l2cache.sav_vdevs = NULL; 1559 } 1560 if (spa->spa_l2cache.sav_config) { 1561 nvlist_free(spa->spa_l2cache.sav_config); 1562 spa->spa_l2cache.sav_config = NULL; 1563 } 1564 spa->spa_l2cache.sav_count = 0; 1565 1566 spa->spa_async_suspended = 0; 1567 1568 spa->spa_indirect_vdevs_loaded = B_FALSE; 1569 1570 if (spa->spa_comment != NULL) { 1571 spa_strfree(spa->spa_comment); 1572 spa->spa_comment = NULL; 1573 } 1574 1575 spa_config_exit(spa, SCL_ALL, spa); 1576 } 1577 1578 /* 1579 * Load (or re-load) the current list of vdevs describing the active spares for 1580 * this pool. When this is called, we have some form of basic information in 1581 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and 1582 * then re-generate a more complete list including status information. 1583 */ 1584 void 1585 spa_load_spares(spa_t *spa) 1586 { 1587 nvlist_t **spares; 1588 uint_t nspares; 1589 int i; 1590 vdev_t *vd, *tvd; 1591 1592 #ifndef _KERNEL 1593 /* 1594 * zdb opens both the current state of the pool and the 1595 * checkpointed state (if present), with a different spa_t. 1596 * 1597 * As spare vdevs are shared among open pools, we skip loading 1598 * them when we load the checkpointed state of the pool. 1599 */ 1600 if (!spa_writeable(spa)) 1601 return; 1602 #endif 1603 1604 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1605 1606 /* 1607 * First, close and free any existing spare vdevs. 1608 */ 1609 for (i = 0; i < spa->spa_spares.sav_count; i++) { 1610 vd = spa->spa_spares.sav_vdevs[i]; 1611 1612 /* Undo the call to spa_activate() below */ 1613 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 1614 B_FALSE)) != NULL && tvd->vdev_isspare) 1615 spa_spare_remove(tvd); 1616 vdev_close(vd); 1617 vdev_free(vd); 1618 } 1619 1620 if (spa->spa_spares.sav_vdevs) 1621 kmem_free(spa->spa_spares.sav_vdevs, 1622 spa->spa_spares.sav_count * sizeof (void *)); 1623 1624 if (spa->spa_spares.sav_config == NULL) 1625 nspares = 0; 1626 else 1627 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 1628 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 1629 1630 spa->spa_spares.sav_count = (int)nspares; 1631 spa->spa_spares.sav_vdevs = NULL; 1632 1633 if (nspares == 0) 1634 return; 1635 1636 /* 1637 * Construct the array of vdevs, opening them to get status in the 1638 * process. For each spare, there is potentially two different vdev_t 1639 * structures associated with it: one in the list of spares (used only 1640 * for basic validation purposes) and one in the active vdev 1641 * configuration (if it's spared in). During this phase we open and 1642 * validate each vdev on the spare list. If the vdev also exists in the 1643 * active configuration, then we also mark this vdev as an active spare. 1644 */ 1645 spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *), 1646 KM_SLEEP); 1647 for (i = 0; i < spa->spa_spares.sav_count; i++) { 1648 VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, 1649 VDEV_ALLOC_SPARE) == 0); 1650 ASSERT(vd != NULL); 1651 1652 spa->spa_spares.sav_vdevs[i] = vd; 1653 1654 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, 1655 B_FALSE)) != NULL) { 1656 if (!tvd->vdev_isspare) 1657 spa_spare_add(tvd); 1658 1659 /* 1660 * We only mark the spare active if we were successfully 1661 * able to load the vdev. Otherwise, importing a pool 1662 * with a bad active spare would result in strange 1663 * behavior, because multiple pool would think the spare 1664 * is actively in use. 1665 * 1666 * There is a vulnerability here to an equally bizarre 1667 * circumstance, where a dead active spare is later 1668 * brought back to life (onlined or otherwise). Given 1669 * the rarity of this scenario, and the extra complexity 1670 * it adds, we ignore the possibility. 1671 */ 1672 if (!vdev_is_dead(tvd)) 1673 spa_spare_activate(tvd); 1674 } 1675 1676 vd->vdev_top = vd; 1677 vd->vdev_aux = &spa->spa_spares; 1678 1679 if (vdev_open(vd) != 0) 1680 continue; 1681 1682 if (vdev_validate_aux(vd) == 0) 1683 spa_spare_add(vd); 1684 } 1685 1686 /* 1687 * Recompute the stashed list of spares, with status information 1688 * this time. 1689 */ 1690 VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, 1691 DATA_TYPE_NVLIST_ARRAY) == 0); 1692 1693 spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), 1694 KM_SLEEP); 1695 for (i = 0; i < spa->spa_spares.sav_count; i++) 1696 spares[i] = vdev_config_generate(spa, 1697 spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE); 1698 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 1699 ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0); 1700 for (i = 0; i < spa->spa_spares.sav_count; i++) 1701 nvlist_free(spares[i]); 1702 kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); 1703 } 1704 1705 /* 1706 * Load (or re-load) the current list of vdevs describing the active l2cache for 1707 * this pool. When this is called, we have some form of basic information in 1708 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and 1709 * then re-generate a more complete list including status information. 1710 * Devices which are already active have their details maintained, and are 1711 * not re-opened. 1712 */ 1713 void 1714 spa_load_l2cache(spa_t *spa) 1715 { 1716 nvlist_t **l2cache; 1717 uint_t nl2cache; 1718 int i, j, oldnvdevs; 1719 uint64_t guid; 1720 vdev_t *vd, **oldvdevs, **newvdevs; 1721 spa_aux_vdev_t *sav = &spa->spa_l2cache; 1722 1723 #ifndef _KERNEL 1724 /* 1725 * zdb opens both the current state of the pool and the 1726 * checkpointed state (if present), with a different spa_t. 1727 * 1728 * As L2 caches are part of the ARC which is shared among open 1729 * pools, we skip loading them when we load the checkpointed 1730 * state of the pool. 1731 */ 1732 if (!spa_writeable(spa)) 1733 return; 1734 #endif 1735 1736 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1737 1738 nl2cache = 0; 1739 newvdevs = NULL; 1740 if (sav->sav_config != NULL) { 1741 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 1742 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 1743 if (nl2cache > 0) { 1744 newvdevs = kmem_alloc( 1745 nl2cache * sizeof (void *), KM_SLEEP); 1746 } 1747 } 1748 1749 oldvdevs = sav->sav_vdevs; 1750 oldnvdevs = sav->sav_count; 1751 sav->sav_vdevs = NULL; 1752 sav->sav_count = 0; 1753 1754 /* 1755 * Process new nvlist of vdevs. 1756 */ 1757 for (i = 0; i < nl2cache; i++) { 1758 VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID, 1759 &guid) == 0); 1760 1761 newvdevs[i] = NULL; 1762 for (j = 0; j < oldnvdevs; j++) { 1763 vd = oldvdevs[j]; 1764 if (vd != NULL && guid == vd->vdev_guid) { 1765 /* 1766 * Retain previous vdev for add/remove ops. 1767 */ 1768 newvdevs[i] = vd; 1769 oldvdevs[j] = NULL; 1770 break; 1771 } 1772 } 1773 1774 if (newvdevs[i] == NULL) { 1775 /* 1776 * Create new vdev 1777 */ 1778 VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, 1779 VDEV_ALLOC_L2CACHE) == 0); 1780 ASSERT(vd != NULL); 1781 newvdevs[i] = vd; 1782 1783 /* 1784 * Commit this vdev as an l2cache device, 1785 * even if it fails to open. 1786 */ 1787 spa_l2cache_add(vd); 1788 1789 vd->vdev_top = vd; 1790 vd->vdev_aux = sav; 1791 1792 spa_l2cache_activate(vd); 1793 1794 if (vdev_open(vd) != 0) 1795 continue; 1796 1797 (void) vdev_validate_aux(vd); 1798 1799 if (!vdev_is_dead(vd)) 1800 l2arc_add_vdev(spa, vd); 1801 } 1802 } 1803 1804 /* 1805 * Purge vdevs that were dropped 1806 */ 1807 for (i = 0; i < oldnvdevs; i++) { 1808 uint64_t pool; 1809 1810 vd = oldvdevs[i]; 1811 if (vd != NULL) { 1812 ASSERT(vd->vdev_isl2cache); 1813 1814 if (spa_l2cache_exists(vd->vdev_guid, &pool) && 1815 pool != 0ULL && l2arc_vdev_present(vd)) 1816 l2arc_remove_vdev(vd); 1817 vdev_clear_stats(vd); 1818 vdev_free(vd); 1819 } 1820 } 1821 1822 if (oldvdevs) 1823 kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); 1824 1825 if (sav->sav_config == NULL) 1826 goto out; 1827 1828 sav->sav_vdevs = newvdevs; 1829 sav->sav_count = (int)nl2cache; 1830 1831 /* 1832 * Recompute the stashed list of l2cache devices, with status 1833 * information this time. 1834 */ 1835 VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, 1836 DATA_TYPE_NVLIST_ARRAY) == 0); 1837 1838 l2cache = NULL; 1839 if (sav->sav_count > 0) { 1840 l2cache = kmem_alloc( 1841 sav->sav_count * sizeof (void *), KM_SLEEP); 1842 } 1843 for (i = 0; i < sav->sav_count; i++) 1844 l2cache[i] = vdev_config_generate(spa, 1845 sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE); 1846 VERIFY(nvlist_add_nvlist_array(sav->sav_config, 1847 ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0); 1848 out: 1849 for (i = 0; i < sav->sav_count; i++) 1850 nvlist_free(l2cache[i]); 1851 if (sav->sav_count) 1852 kmem_free(l2cache, sav->sav_count * sizeof (void *)); 1853 } 1854 1855 static int 1856 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 1857 { 1858 dmu_buf_t *db; 1859 char *packed = NULL; 1860 size_t nvsize = 0; 1861 int error; 1862 *value = NULL; 1863 1864 error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db); 1865 if (error != 0) 1866 return (error); 1867 1868 nvsize = *(uint64_t *)db->db_data; 1869 dmu_buf_rele(db, FTAG); 1870 1871 packed = kmem_alloc(nvsize, KM_SLEEP); 1872 error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed, 1873 DMU_READ_PREFETCH); 1874 if (error == 0) 1875 error = nvlist_unpack(packed, nvsize, value, 0); 1876 kmem_free(packed, nvsize); 1877 1878 return (error); 1879 } 1880 1881 /* 1882 * Concrete top-level vdevs that are not missing and are not logs. At every 1883 * spa_sync we write new uberblocks to at least SPA_SYNC_MIN_VDEVS core tvds. 1884 */ 1885 static uint64_t 1886 spa_healthy_core_tvds(spa_t *spa) 1887 { 1888 vdev_t *rvd = spa->spa_root_vdev; 1889 uint64_t tvds = 0; 1890 1891 for (uint64_t i = 0; i < rvd->vdev_children; i++) { 1892 vdev_t *vd = rvd->vdev_child[i]; 1893 if (vd->vdev_islog) 1894 continue; 1895 if (vdev_is_concrete(vd) && !vdev_is_dead(vd)) 1896 tvds++; 1897 } 1898 1899 return (tvds); 1900 } 1901 1902 /* 1903 * Checks to see if the given vdev could not be opened, in which case we post a 1904 * sysevent to notify the autoreplace code that the device has been removed. 1905 */ 1906 static void 1907 spa_check_removed(vdev_t *vd) 1908 { 1909 for (uint64_t c = 0; c < vd->vdev_children; c++) 1910 spa_check_removed(vd->vdev_child[c]); 1911 1912 if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) && 1913 vdev_is_concrete(vd)) { 1914 zfs_post_autoreplace(vd->vdev_spa, vd); 1915 spa_event_notify(vd->vdev_spa, vd, NULL, ESC_ZFS_VDEV_CHECK); 1916 } 1917 } 1918 1919 static int 1920 spa_check_for_missing_logs(spa_t *spa) 1921 { 1922 vdev_t *rvd = spa->spa_root_vdev; 1923 1924 /* 1925 * If we're doing a normal import, then build up any additional 1926 * diagnostic information about missing log devices. 1927 * We'll pass this up to the user for further processing. 1928 */ 1929 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) { 1930 nvlist_t **child, *nv; 1931 uint64_t idx = 0; 1932 1933 child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **), 1934 KM_SLEEP); 1935 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1936 1937 for (uint64_t c = 0; c < rvd->vdev_children; c++) { 1938 vdev_t *tvd = rvd->vdev_child[c]; 1939 1940 /* 1941 * We consider a device as missing only if it failed 1942 * to open (i.e. offline or faulted is not considered 1943 * as missing). 1944 */ 1945 if (tvd->vdev_islog && 1946 tvd->vdev_state == VDEV_STATE_CANT_OPEN) { 1947 child[idx++] = vdev_config_generate(spa, tvd, 1948 B_FALSE, VDEV_CONFIG_MISSING); 1949 } 1950 } 1951 1952 if (idx > 0) { 1953 fnvlist_add_nvlist_array(nv, 1954 ZPOOL_CONFIG_CHILDREN, child, idx); 1955 fnvlist_add_nvlist(spa->spa_load_info, 1956 ZPOOL_CONFIG_MISSING_DEVICES, nv); 1957 1958 for (uint64_t i = 0; i < idx; i++) 1959 nvlist_free(child[i]); 1960 } 1961 nvlist_free(nv); 1962 kmem_free(child, rvd->vdev_children * sizeof (char **)); 1963 1964 if (idx > 0) { 1965 spa_load_failed(spa, "some log devices are missing"); 1966 vdev_dbgmsg_print_tree(rvd, 2); 1967 return (SET_ERROR(ENXIO)); 1968 } 1969 } else { 1970 for (uint64_t c = 0; c < rvd->vdev_children; c++) { 1971 vdev_t *tvd = rvd->vdev_child[c]; 1972 1973 if (tvd->vdev_islog && 1974 tvd->vdev_state == VDEV_STATE_CANT_OPEN) { 1975 spa_set_log_state(spa, SPA_LOG_CLEAR); 1976 spa_load_note(spa, "some log devices are " 1977 "missing, ZIL is dropped."); 1978 vdev_dbgmsg_print_tree(rvd, 2); 1979 break; 1980 } 1981 } 1982 } 1983 1984 return (0); 1985 } 1986 1987 /* 1988 * Check for missing log devices 1989 */ 1990 static boolean_t 1991 spa_check_logs(spa_t *spa) 1992 { 1993 boolean_t rv = B_FALSE; 1994 dsl_pool_t *dp = spa_get_dsl(spa); 1995 1996 switch (spa->spa_log_state) { 1997 case SPA_LOG_MISSING: 1998 /* need to recheck in case slog has been restored */ 1999 case SPA_LOG_UNKNOWN: 2000 rv = (dmu_objset_find_dp(dp, dp->dp_root_dir_obj, 2001 zil_check_log_chain, NULL, DS_FIND_CHILDREN) != 0); 2002 if (rv) 2003 spa_set_log_state(spa, SPA_LOG_MISSING); 2004 break; 2005 } 2006 return (rv); 2007 } 2008 2009 static boolean_t 2010 spa_passivate_log(spa_t *spa) 2011 { 2012 vdev_t *rvd = spa->spa_root_vdev; 2013 boolean_t slog_found = B_FALSE; 2014 2015 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 2016 2017 if (!spa_has_slogs(spa)) 2018 return (B_FALSE); 2019 2020 for (int c = 0; c < rvd->vdev_children; c++) { 2021 vdev_t *tvd = rvd->vdev_child[c]; 2022 metaslab_group_t *mg = tvd->vdev_mg; 2023 2024 if (tvd->vdev_islog) { 2025 metaslab_group_passivate(mg); 2026 slog_found = B_TRUE; 2027 } 2028 } 2029 2030 return (slog_found); 2031 } 2032 2033 static void 2034 spa_activate_log(spa_t *spa) 2035 { 2036 vdev_t *rvd = spa->spa_root_vdev; 2037 2038 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 2039 2040 for (int c = 0; c < rvd->vdev_children; c++) { 2041 vdev_t *tvd = rvd->vdev_child[c]; 2042 metaslab_group_t *mg = tvd->vdev_mg; 2043 2044 if (tvd->vdev_islog) 2045 metaslab_group_activate(mg); 2046 } 2047 } 2048 2049 int 2050 spa_reset_logs(spa_t *spa) 2051 { 2052 int error; 2053 2054 error = dmu_objset_find(spa_name(spa), zil_reset, 2055 NULL, DS_FIND_CHILDREN); 2056 if (error == 0) { 2057 /* 2058 * We successfully offlined the log device, sync out the 2059 * current txg so that the "stubby" block can be removed 2060 * by zil_sync(). 2061 */ 2062 txg_wait_synced(spa->spa_dsl_pool, 0); 2063 } 2064 return (error); 2065 } 2066 2067 static void 2068 spa_aux_check_removed(spa_aux_vdev_t *sav) 2069 { 2070 for (int i = 0; i < sav->sav_count; i++) 2071 spa_check_removed(sav->sav_vdevs[i]); 2072 } 2073 2074 void 2075 spa_claim_notify(zio_t *zio) 2076 { 2077 spa_t *spa = zio->io_spa; 2078 2079 if (zio->io_error) 2080 return; 2081 2082 mutex_enter(&spa->spa_props_lock); /* any mutex will do */ 2083 if (spa->spa_claim_max_txg < zio->io_bp->blk_birth) 2084 spa->spa_claim_max_txg = zio->io_bp->blk_birth; 2085 mutex_exit(&spa->spa_props_lock); 2086 } 2087 2088 typedef struct spa_load_error { 2089 uint64_t sle_meta_count; 2090 uint64_t sle_data_count; 2091 } spa_load_error_t; 2092 2093 static void 2094 spa_load_verify_done(zio_t *zio) 2095 { 2096 blkptr_t *bp = zio->io_bp; 2097 spa_load_error_t *sle = zio->io_private; 2098 dmu_object_type_t type = BP_GET_TYPE(bp); 2099 int error = zio->io_error; 2100 spa_t *spa = zio->io_spa; 2101 2102 abd_free(zio->io_abd); 2103 if (error) { 2104 if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) && 2105 type != DMU_OT_INTENT_LOG) 2106 atomic_inc_64(&sle->sle_meta_count); 2107 else 2108 atomic_inc_64(&sle->sle_data_count); 2109 } 2110 2111 mutex_enter(&spa->spa_scrub_lock); 2112 spa->spa_load_verify_ios--; 2113 cv_broadcast(&spa->spa_scrub_io_cv); 2114 mutex_exit(&spa->spa_scrub_lock); 2115 } 2116 2117 /* 2118 * Maximum number of concurrent scrub i/os to create while verifying 2119 * a pool while importing it. 2120 */ 2121 int spa_load_verify_maxinflight = 10000; 2122 boolean_t spa_load_verify_metadata = B_TRUE; 2123 boolean_t spa_load_verify_data = B_TRUE; 2124 2125 /*ARGSUSED*/ 2126 static int 2127 spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 2128 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) 2129 { 2130 if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp)) 2131 return (0); 2132 /* 2133 * Note: normally this routine will not be called if 2134 * spa_load_verify_metadata is not set. However, it may be useful 2135 * to manually set the flag after the traversal has begun. 2136 */ 2137 if (!spa_load_verify_metadata) 2138 return (0); 2139 if (!BP_IS_METADATA(bp) && !spa_load_verify_data) 2140 return (0); 2141 2142 zio_t *rio = arg; 2143 size_t size = BP_GET_PSIZE(bp); 2144 2145 mutex_enter(&spa->spa_scrub_lock); 2146 while (spa->spa_load_verify_ios >= spa_load_verify_maxinflight) 2147 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 2148 spa->spa_load_verify_ios++; 2149 mutex_exit(&spa->spa_scrub_lock); 2150 2151 zio_nowait(zio_read(rio, spa, bp, abd_alloc_for_io(size, B_FALSE), size, 2152 spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB, 2153 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL | 2154 ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb)); 2155 return (0); 2156 } 2157 2158 /* ARGSUSED */ 2159 int 2160 verify_dataset_name_len(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg) 2161 { 2162 if (dsl_dataset_namelen(ds) >= ZFS_MAX_DATASET_NAME_LEN) 2163 return (SET_ERROR(ENAMETOOLONG)); 2164 2165 return (0); 2166 } 2167 2168 static int 2169 spa_load_verify(spa_t *spa) 2170 { 2171 zio_t *rio; 2172 spa_load_error_t sle = { 0 }; 2173 zpool_load_policy_t policy; 2174 boolean_t verify_ok = B_FALSE; 2175 int error = 0; 2176 2177 zpool_get_load_policy(spa->spa_config, &policy); 2178 2179 if (policy.zlp_rewind & ZPOOL_NEVER_REWIND) 2180 return (0); 2181 2182 dsl_pool_config_enter(spa->spa_dsl_pool, FTAG); 2183 error = dmu_objset_find_dp(spa->spa_dsl_pool, 2184 spa->spa_dsl_pool->dp_root_dir_obj, verify_dataset_name_len, NULL, 2185 DS_FIND_CHILDREN); 2186 dsl_pool_config_exit(spa->spa_dsl_pool, FTAG); 2187 if (error != 0) 2188 return (error); 2189 2190 rio = zio_root(spa, NULL, &sle, 2191 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 2192 2193 if (spa_load_verify_metadata) { 2194 if (spa->spa_extreme_rewind) { 2195 spa_load_note(spa, "performing a complete scan of the " 2196 "pool since extreme rewind is on. This may take " 2197 "a very long time.\n (spa_load_verify_data=%u, " 2198 "spa_load_verify_metadata=%u)", 2199 spa_load_verify_data, spa_load_verify_metadata); 2200 } 2201 error = traverse_pool(spa, spa->spa_verify_min_txg, 2202 TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | 2203 TRAVERSE_NO_DECRYPT, spa_load_verify_cb, rio); 2204 } 2205 2206 (void) zio_wait(rio); 2207 2208 spa->spa_load_meta_errors = sle.sle_meta_count; 2209 spa->spa_load_data_errors = sle.sle_data_count; 2210 2211 if (sle.sle_meta_count != 0 || sle.sle_data_count != 0) { 2212 spa_load_note(spa, "spa_load_verify found %llu metadata errors " 2213 "and %llu data errors", (u_longlong_t)sle.sle_meta_count, 2214 (u_longlong_t)sle.sle_data_count); 2215 } 2216 2217 if (spa_load_verify_dryrun || 2218 (!error && sle.sle_meta_count <= policy.zlp_maxmeta && 2219 sle.sle_data_count <= policy.zlp_maxdata)) { 2220 int64_t loss = 0; 2221 2222 verify_ok = B_TRUE; 2223 spa->spa_load_txg = spa->spa_uberblock.ub_txg; 2224 spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp; 2225 2226 loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts; 2227 VERIFY(nvlist_add_uint64(spa->spa_load_info, 2228 ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0); 2229 VERIFY(nvlist_add_int64(spa->spa_load_info, 2230 ZPOOL_CONFIG_REWIND_TIME, loss) == 0); 2231 VERIFY(nvlist_add_uint64(spa->spa_load_info, 2232 ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0); 2233 } else { 2234 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg; 2235 } 2236 2237 if (spa_load_verify_dryrun) 2238 return (0); 2239 2240 if (error) { 2241 if (error != ENXIO && error != EIO) 2242 error = SET_ERROR(EIO); 2243 return (error); 2244 } 2245 2246 return (verify_ok ? 0 : EIO); 2247 } 2248 2249 /* 2250 * Find a value in the pool props object. 2251 */ 2252 static void 2253 spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val) 2254 { 2255 (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object, 2256 zpool_prop_to_name(prop), sizeof (uint64_t), 1, val); 2257 } 2258 2259 /* 2260 * Find a value in the pool directory object. 2261 */ 2262 static int 2263 spa_dir_prop(spa_t *spa, const char *name, uint64_t *val, boolean_t log_enoent) 2264 { 2265 int error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 2266 name, sizeof (uint64_t), 1, val); 2267 2268 if (error != 0 && (error != ENOENT || log_enoent)) { 2269 spa_load_failed(spa, "couldn't get '%s' value in MOS directory " 2270 "[error=%d]", name, error); 2271 } 2272 2273 return (error); 2274 } 2275 2276 static int 2277 spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err) 2278 { 2279 vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux); 2280 return (SET_ERROR(err)); 2281 } 2282 2283 static void 2284 spa_spawn_aux_threads(spa_t *spa) 2285 { 2286 ASSERT(spa_writeable(spa)); 2287 2288 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 2289 2290 spa_start_indirect_condensing_thread(spa); 2291 2292 ASSERT3P(spa->spa_checkpoint_discard_zthr, ==, NULL); 2293 spa->spa_checkpoint_discard_zthr = 2294 zthr_create(spa_checkpoint_discard_thread_check, 2295 spa_checkpoint_discard_thread, spa); 2296 } 2297 2298 /* 2299 * Fix up config after a partly-completed split. This is done with the 2300 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off 2301 * pool have that entry in their config, but only the splitting one contains 2302 * a list of all the guids of the vdevs that are being split off. 2303 * 2304 * This function determines what to do with that list: either rejoin 2305 * all the disks to the pool, or complete the splitting process. To attempt 2306 * the rejoin, each disk that is offlined is marked online again, and 2307 * we do a reopen() call. If the vdev label for every disk that was 2308 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL) 2309 * then we call vdev_split() on each disk, and complete the split. 2310 * 2311 * Otherwise we leave the config alone, with all the vdevs in place in 2312 * the original pool. 2313 */ 2314 static void 2315 spa_try_repair(spa_t *spa, nvlist_t *config) 2316 { 2317 uint_t extracted; 2318 uint64_t *glist; 2319 uint_t i, gcount; 2320 nvlist_t *nvl; 2321 vdev_t **vd; 2322 boolean_t attempt_reopen; 2323 2324 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0) 2325 return; 2326 2327 /* check that the config is complete */ 2328 if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, 2329 &glist, &gcount) != 0) 2330 return; 2331 2332 vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP); 2333 2334 /* attempt to online all the vdevs & validate */ 2335 attempt_reopen = B_TRUE; 2336 for (i = 0; i < gcount; i++) { 2337 if (glist[i] == 0) /* vdev is hole */ 2338 continue; 2339 2340 vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE); 2341 if (vd[i] == NULL) { 2342 /* 2343 * Don't bother attempting to reopen the disks; 2344 * just do the split. 2345 */ 2346 attempt_reopen = B_FALSE; 2347 } else { 2348 /* attempt to re-online it */ 2349 vd[i]->vdev_offline = B_FALSE; 2350 } 2351 } 2352 2353 if (attempt_reopen) { 2354 vdev_reopen(spa->spa_root_vdev); 2355 2356 /* check each device to see what state it's in */ 2357 for (extracted = 0, i = 0; i < gcount; i++) { 2358 if (vd[i] != NULL && 2359 vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL) 2360 break; 2361 ++extracted; 2362 } 2363 } 2364 2365 /* 2366 * If every disk has been moved to the new pool, or if we never 2367 * even attempted to look at them, then we split them off for 2368 * good. 2369 */ 2370 if (!attempt_reopen || gcount == extracted) { 2371 for (i = 0; i < gcount; i++) 2372 if (vd[i] != NULL) 2373 vdev_split(vd[i]); 2374 vdev_reopen(spa->spa_root_vdev); 2375 } 2376 2377 kmem_free(vd, gcount * sizeof (vdev_t *)); 2378 } 2379 2380 static int 2381 spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type) 2382 { 2383 char *ereport = FM_EREPORT_ZFS_POOL; 2384 int error; 2385 2386 spa->spa_load_state = state; 2387 (void) spa_import_progress_set_state(spa, spa_load_state(spa)); 2388 2389 gethrestime(&spa->spa_loaded_ts); 2390 error = spa_load_impl(spa, type, &ereport); 2391 2392 /* 2393 * Don't count references from objsets that are already closed 2394 * and are making their way through the eviction process. 2395 */ 2396 spa_evicting_os_wait(spa); 2397 spa->spa_minref = zfs_refcount_count(&spa->spa_refcount); 2398 if (error) { 2399 if (error != EEXIST) { 2400 spa->spa_loaded_ts.tv_sec = 0; 2401 spa->spa_loaded_ts.tv_nsec = 0; 2402 } 2403 if (error != EBADF) { 2404 (void) zfs_ereport_post(ereport, spa, 2405 NULL, NULL, NULL, 0, 0); 2406 } 2407 } 2408 spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE; 2409 spa->spa_ena = 0; 2410 2411 (void) spa_import_progress_set_state(spa, spa_load_state(spa)); 2412 2413 return (error); 2414 } 2415 2416 /* 2417 * Count the number of per-vdev ZAPs associated with all of the vdevs in the 2418 * vdev tree rooted in the given vd, and ensure that each ZAP is present in the 2419 * spa's per-vdev ZAP list. 2420 */ 2421 static uint64_t 2422 vdev_count_verify_zaps(vdev_t *vd) 2423 { 2424 spa_t *spa = vd->vdev_spa; 2425 uint64_t total = 0; 2426 if (vd->vdev_top_zap != 0) { 2427 total++; 2428 ASSERT0(zap_lookup_int(spa->spa_meta_objset, 2429 spa->spa_all_vdev_zaps, vd->vdev_top_zap)); 2430 } 2431 if (vd->vdev_leaf_zap != 0) { 2432 total++; 2433 ASSERT0(zap_lookup_int(spa->spa_meta_objset, 2434 spa->spa_all_vdev_zaps, vd->vdev_leaf_zap)); 2435 } 2436 2437 for (uint64_t i = 0; i < vd->vdev_children; i++) { 2438 total += vdev_count_verify_zaps(vd->vdev_child[i]); 2439 } 2440 2441 return (total); 2442 } 2443 2444 /* 2445 * Determine whether the activity check is required. 2446 */ 2447 static boolean_t 2448 spa_activity_check_required(spa_t *spa, uberblock_t *ub, nvlist_t *label, 2449 nvlist_t *config) 2450 { 2451 uint64_t state = 0; 2452 uint64_t hostid = 0; 2453 uint64_t tryconfig_txg = 0; 2454 uint64_t tryconfig_timestamp = 0; 2455 uint16_t tryconfig_mmp_seq = 0; 2456 nvlist_t *nvinfo; 2457 2458 if (nvlist_exists(config, ZPOOL_CONFIG_LOAD_INFO)) { 2459 nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO); 2460 (void) nvlist_lookup_uint64(nvinfo, ZPOOL_CONFIG_MMP_TXG, 2461 &tryconfig_txg); 2462 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 2463 &tryconfig_timestamp); 2464 (void) nvlist_lookup_uint16(nvinfo, ZPOOL_CONFIG_MMP_SEQ, 2465 &tryconfig_mmp_seq); 2466 } 2467 2468 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, &state); 2469 2470 /* 2471 * Disable the MMP activity check - This is used by zdb which 2472 * is intended to be used on potentially active pools. 2473 */ 2474 if (spa->spa_import_flags & ZFS_IMPORT_SKIP_MMP) 2475 return (B_FALSE); 2476 2477 /* 2478 * Skip the activity check when the MMP feature is disabled. 2479 */ 2480 if (ub->ub_mmp_magic == MMP_MAGIC && ub->ub_mmp_delay == 0) 2481 return (B_FALSE); 2482 2483 /* 2484 * If the tryconfig_ values are nonzero, they are the results of an 2485 * earlier tryimport. If they all match the uberblock we just found, 2486 * then the pool has not changed and we return false so we do not test 2487 * a second time. 2488 */ 2489 if (tryconfig_txg && tryconfig_txg == ub->ub_txg && 2490 tryconfig_timestamp && tryconfig_timestamp == ub->ub_timestamp && 2491 tryconfig_mmp_seq && tryconfig_mmp_seq == 2492 (MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0)) 2493 return (B_FALSE); 2494 2495 /* 2496 * Allow the activity check to be skipped when importing the pool 2497 * on the same host which last imported it. Since the hostid from 2498 * configuration may be stale use the one read from the label. 2499 */ 2500 if (nvlist_exists(label, ZPOOL_CONFIG_HOSTID)) 2501 hostid = fnvlist_lookup_uint64(label, ZPOOL_CONFIG_HOSTID); 2502 2503 if (hostid == spa_get_hostid()) 2504 return (B_FALSE); 2505 2506 /* 2507 * Skip the activity test when the pool was cleanly exported. 2508 */ 2509 if (state != POOL_STATE_ACTIVE) 2510 return (B_FALSE); 2511 2512 return (B_TRUE); 2513 } 2514 2515 /* 2516 * Nanoseconds the activity check must watch for changes on-disk. 2517 */ 2518 static uint64_t 2519 spa_activity_check_duration(spa_t *spa, uberblock_t *ub) 2520 { 2521 uint64_t import_intervals = MAX(zfs_multihost_import_intervals, 1); 2522 uint64_t multihost_interval = MSEC2NSEC( 2523 MMP_INTERVAL_OK(zfs_multihost_interval)); 2524 uint64_t import_delay = MAX(NANOSEC, import_intervals * 2525 multihost_interval); 2526 2527 /* 2528 * Local tunables determine a minimum duration except for the case 2529 * where we know when the remote host will suspend the pool if MMP 2530 * writes do not land. 2531 * 2532 * See Big Theory comment at the top of mmp.c for the reasoning behind 2533 * these cases and times. 2534 */ 2535 2536 ASSERT(MMP_IMPORT_SAFETY_FACTOR >= 100); 2537 2538 if (MMP_INTERVAL_VALID(ub) && MMP_FAIL_INT_VALID(ub) && 2539 MMP_FAIL_INT(ub) > 0) { 2540 2541 /* MMP on remote host will suspend pool after failed writes */ 2542 import_delay = MMP_FAIL_INT(ub) * MSEC2NSEC(MMP_INTERVAL(ub)) * 2543 MMP_IMPORT_SAFETY_FACTOR / 100; 2544 2545 zfs_dbgmsg("fail_intvals>0 import_delay=%llu ub_mmp " 2546 "mmp_fails=%llu ub_mmp mmp_interval=%llu " 2547 "import_intervals=%u", import_delay, MMP_FAIL_INT(ub), 2548 MMP_INTERVAL(ub), import_intervals); 2549 2550 } else if (MMP_INTERVAL_VALID(ub) && MMP_FAIL_INT_VALID(ub) && 2551 MMP_FAIL_INT(ub) == 0) { 2552 2553 /* MMP on remote host will never suspend pool */ 2554 import_delay = MAX(import_delay, (MSEC2NSEC(MMP_INTERVAL(ub)) + 2555 ub->ub_mmp_delay) * import_intervals); 2556 2557 zfs_dbgmsg("fail_intvals=0 import_delay=%llu ub_mmp " 2558 "mmp_interval=%llu ub_mmp_delay=%llu " 2559 "import_intervals=%u", import_delay, MMP_INTERVAL(ub), 2560 ub->ub_mmp_delay, import_intervals); 2561 2562 } else if (MMP_VALID(ub)) { 2563 /* 2564 * zfs-0.7 compatability case 2565 */ 2566 2567 import_delay = MAX(import_delay, (multihost_interval + 2568 ub->ub_mmp_delay) * import_intervals); 2569 2570 zfs_dbgmsg("import_delay=%llu ub_mmp_delay=%llu " 2571 "import_intervals=%u leaves=%u", import_delay, 2572 ub->ub_mmp_delay, import_intervals, 2573 vdev_count_leaves(spa)); 2574 } else { 2575 /* Using local tunings is the only reasonable option */ 2576 zfs_dbgmsg("pool last imported on non-MMP aware " 2577 "host using import_delay=%llu multihost_interval=%llu " 2578 "import_intervals=%u", import_delay, multihost_interval, 2579 import_intervals); 2580 } 2581 2582 return (import_delay); 2583 } 2584 2585 /* 2586 * Perform the import activity check. If the user canceled the import or 2587 * we detected activity then fail. 2588 */ 2589 static int 2590 spa_activity_check(spa_t *spa, uberblock_t *ub, nvlist_t *config) 2591 { 2592 uint64_t txg = ub->ub_txg; 2593 uint64_t timestamp = ub->ub_timestamp; 2594 uint64_t mmp_config = ub->ub_mmp_config; 2595 uint16_t mmp_seq = MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0; 2596 uint64_t import_delay; 2597 hrtime_t import_expire; 2598 nvlist_t *mmp_label = NULL; 2599 vdev_t *rvd = spa->spa_root_vdev; 2600 kcondvar_t cv; 2601 kmutex_t mtx; 2602 int error = 0; 2603 2604 cv_init(&cv, NULL, CV_DEFAULT, NULL); 2605 mutex_init(&mtx, NULL, MUTEX_DEFAULT, NULL); 2606 mutex_enter(&mtx); 2607 2608 /* 2609 * If ZPOOL_CONFIG_MMP_TXG is present an activity check was performed 2610 * during the earlier tryimport. If the txg recorded there is 0 then 2611 * the pool is known to be active on another host. 2612 * 2613 * Otherwise, the pool might be in use on another host. Check for 2614 * changes in the uberblocks on disk if necessary. 2615 */ 2616 if (nvlist_exists(config, ZPOOL_CONFIG_LOAD_INFO)) { 2617 nvlist_t *nvinfo = fnvlist_lookup_nvlist(config, 2618 ZPOOL_CONFIG_LOAD_INFO); 2619 2620 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_TXG) && 2621 fnvlist_lookup_uint64(nvinfo, ZPOOL_CONFIG_MMP_TXG) == 0) { 2622 vdev_uberblock_load(rvd, ub, &mmp_label); 2623 error = SET_ERROR(EREMOTEIO); 2624 goto out; 2625 } 2626 } 2627 2628 import_delay = spa_activity_check_duration(spa, ub); 2629 2630 /* Add a small random factor in case of simultaneous imports (0-25%) */ 2631 import_delay += import_delay * spa_get_random(250) / 1000; 2632 2633 import_expire = gethrtime() + import_delay; 2634 2635 while (gethrtime() < import_expire) { 2636 (void) spa_import_progress_set_mmp_check(spa, 2637 NSEC2SEC(import_expire - gethrtime())); 2638 2639 vdev_uberblock_load(rvd, ub, &mmp_label); 2640 2641 if (txg != ub->ub_txg || timestamp != ub->ub_timestamp || 2642 mmp_seq != (MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0)) { 2643 zfs_dbgmsg("multihost activity detected " 2644 "txg %llu ub_txg %llu " 2645 "timestamp %llu ub_timestamp %llu " 2646 "mmp_config %#llx ub_mmp_config %#llx", 2647 txg, ub->ub_txg, timestamp, ub->ub_timestamp, 2648 mmp_config, ub->ub_mmp_config); 2649 2650 error = SET_ERROR(EREMOTEIO); 2651 break; 2652 } 2653 2654 if (mmp_label) { 2655 nvlist_free(mmp_label); 2656 mmp_label = NULL; 2657 } 2658 2659 error = cv_timedwait_sig(&cv, &mtx, ddi_get_lbolt() + hz); 2660 if (error != -1) { 2661 error = SET_ERROR(EINTR); 2662 break; 2663 } 2664 error = 0; 2665 } 2666 2667 out: 2668 mutex_exit(&mtx); 2669 mutex_destroy(&mtx); 2670 cv_destroy(&cv); 2671 2672 /* 2673 * If the pool is determined to be active store the status in the 2674 * spa->spa_load_info nvlist. If the remote hostname or hostid are 2675 * available from configuration read from disk store them as well. 2676 * This allows 'zpool import' to generate a more useful message. 2677 * 2678 * ZPOOL_CONFIG_MMP_STATE - observed pool status (mandatory) 2679 * ZPOOL_CONFIG_MMP_HOSTNAME - hostname from the active pool 2680 * ZPOOL_CONFIG_MMP_HOSTID - hostid from the active pool 2681 */ 2682 if (error == EREMOTEIO) { 2683 char *hostname = "<unknown>"; 2684 uint64_t hostid = 0; 2685 2686 if (mmp_label) { 2687 if (nvlist_exists(mmp_label, ZPOOL_CONFIG_HOSTNAME)) { 2688 hostname = fnvlist_lookup_string(mmp_label, 2689 ZPOOL_CONFIG_HOSTNAME); 2690 fnvlist_add_string(spa->spa_load_info, 2691 ZPOOL_CONFIG_MMP_HOSTNAME, hostname); 2692 } 2693 2694 if (nvlist_exists(mmp_label, ZPOOL_CONFIG_HOSTID)) { 2695 hostid = fnvlist_lookup_uint64(mmp_label, 2696 ZPOOL_CONFIG_HOSTID); 2697 fnvlist_add_uint64(spa->spa_load_info, 2698 ZPOOL_CONFIG_MMP_HOSTID, hostid); 2699 } 2700 } 2701 2702 fnvlist_add_uint64(spa->spa_load_info, 2703 ZPOOL_CONFIG_MMP_STATE, MMP_STATE_ACTIVE); 2704 fnvlist_add_uint64(spa->spa_load_info, 2705 ZPOOL_CONFIG_MMP_TXG, 0); 2706 2707 error = spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO); 2708 } 2709 2710 if (mmp_label) 2711 nvlist_free(mmp_label); 2712 2713 return (error); 2714 } 2715 2716 static int 2717 spa_verify_host(spa_t *spa, nvlist_t *mos_config) 2718 { 2719 uint64_t hostid; 2720 char *hostname; 2721 uint64_t myhostid = 0; 2722 2723 if (!spa_is_root(spa) && nvlist_lookup_uint64(mos_config, 2724 ZPOOL_CONFIG_HOSTID, &hostid) == 0) { 2725 hostname = fnvlist_lookup_string(mos_config, 2726 ZPOOL_CONFIG_HOSTNAME); 2727 2728 myhostid = zone_get_hostid(NULL); 2729 2730 if (hostid != 0 && myhostid != 0 && hostid != myhostid) { 2731 cmn_err(CE_WARN, "pool '%s' could not be " 2732 "loaded as it was last accessed by " 2733 "another system (host: %s hostid: 0x%llx). " 2734 "See: http://illumos.org/msg/ZFS-8000-EY", 2735 spa_name(spa), hostname, (u_longlong_t)hostid); 2736 spa_load_failed(spa, "hostid verification failed: pool " 2737 "last accessed by host: %s (hostid: 0x%llx)", 2738 hostname, (u_longlong_t)hostid); 2739 return (SET_ERROR(EBADF)); 2740 } 2741 } 2742 2743 return (0); 2744 } 2745 2746 static int 2747 spa_ld_parse_config(spa_t *spa, spa_import_type_t type) 2748 { 2749 int error = 0; 2750 nvlist_t *nvtree, *nvl, *config = spa->spa_config; 2751 int parse; 2752 vdev_t *rvd; 2753 uint64_t pool_guid; 2754 char *comment; 2755 2756 /* 2757 * Versioning wasn't explicitly added to the label until later, so if 2758 * it's not present treat it as the initial version. 2759 */ 2760 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 2761 &spa->spa_ubsync.ub_version) != 0) 2762 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL; 2763 2764 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) { 2765 spa_load_failed(spa, "invalid config provided: '%s' missing", 2766 ZPOOL_CONFIG_POOL_GUID); 2767 return (SET_ERROR(EINVAL)); 2768 } 2769 2770 /* 2771 * If we are doing an import, ensure that the pool is not already 2772 * imported by checking if its pool guid already exists in the 2773 * spa namespace. 2774 * 2775 * The only case that we allow an already imported pool to be 2776 * imported again, is when the pool is checkpointed and we want to 2777 * look at its checkpointed state from userland tools like zdb. 2778 */ 2779 #ifdef _KERNEL 2780 if ((spa->spa_load_state == SPA_LOAD_IMPORT || 2781 spa->spa_load_state == SPA_LOAD_TRYIMPORT) && 2782 spa_guid_exists(pool_guid, 0)) { 2783 #else 2784 if ((spa->spa_load_state == SPA_LOAD_IMPORT || 2785 spa->spa_load_state == SPA_LOAD_TRYIMPORT) && 2786 spa_guid_exists(pool_guid, 0) && 2787 !spa_importing_readonly_checkpoint(spa)) { 2788 #endif 2789 spa_load_failed(spa, "a pool with guid %llu is already open", 2790 (u_longlong_t)pool_guid); 2791 return (SET_ERROR(EEXIST)); 2792 } 2793 2794 spa->spa_config_guid = pool_guid; 2795 2796 nvlist_free(spa->spa_load_info); 2797 spa->spa_load_info = fnvlist_alloc(); 2798 2799 ASSERT(spa->spa_comment == NULL); 2800 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0) 2801 spa->spa_comment = spa_strdup(comment); 2802 2803 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 2804 &spa->spa_config_txg); 2805 2806 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) == 0) 2807 spa->spa_config_splitting = fnvlist_dup(nvl); 2808 2809 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtree)) { 2810 spa_load_failed(spa, "invalid config provided: '%s' missing", 2811 ZPOOL_CONFIG_VDEV_TREE); 2812 return (SET_ERROR(EINVAL)); 2813 } 2814 2815 /* 2816 * Create "The Godfather" zio to hold all async IOs 2817 */ 2818 spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *), 2819 KM_SLEEP); 2820 for (int i = 0; i < max_ncpus; i++) { 2821 spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, 2822 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | 2823 ZIO_FLAG_GODFATHER); 2824 } 2825 2826 /* 2827 * Parse the configuration into a vdev tree. We explicitly set the 2828 * value that will be returned by spa_version() since parsing the 2829 * configuration requires knowing the version number. 2830 */ 2831 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2832 parse = (type == SPA_IMPORT_EXISTING ? 2833 VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT); 2834 error = spa_config_parse(spa, &rvd, nvtree, NULL, 0, parse); 2835 spa_config_exit(spa, SCL_ALL, FTAG); 2836 2837 if (error != 0) { 2838 spa_load_failed(spa, "unable to parse config [error=%d]", 2839 error); 2840 return (error); 2841 } 2842 2843 ASSERT(spa->spa_root_vdev == rvd); 2844 ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT); 2845 ASSERT3U(spa->spa_max_ashift, <=, SPA_MAXBLOCKSHIFT); 2846 2847 if (type != SPA_IMPORT_ASSEMBLE) { 2848 ASSERT(spa_guid(spa) == pool_guid); 2849 } 2850 2851 return (0); 2852 } 2853 2854 /* 2855 * Recursively open all vdevs in the vdev tree. This function is called twice: 2856 * first with the untrusted config, then with the trusted config. 2857 */ 2858 static int 2859 spa_ld_open_vdevs(spa_t *spa) 2860 { 2861 int error = 0; 2862 2863 /* 2864 * spa_missing_tvds_allowed defines how many top-level vdevs can be 2865 * missing/unopenable for the root vdev to be still considered openable. 2866 */ 2867 if (spa->spa_trust_config) { 2868 spa->spa_missing_tvds_allowed = zfs_max_missing_tvds; 2869 } else if (spa->spa_config_source == SPA_CONFIG_SRC_CACHEFILE) { 2870 spa->spa_missing_tvds_allowed = zfs_max_missing_tvds_cachefile; 2871 } else if (spa->spa_config_source == SPA_CONFIG_SRC_SCAN) { 2872 spa->spa_missing_tvds_allowed = zfs_max_missing_tvds_scan; 2873 } else { 2874 spa->spa_missing_tvds_allowed = 0; 2875 } 2876 2877 spa->spa_missing_tvds_allowed = 2878 MAX(zfs_max_missing_tvds, spa->spa_missing_tvds_allowed); 2879 2880 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2881 error = vdev_open(spa->spa_root_vdev); 2882 spa_config_exit(spa, SCL_ALL, FTAG); 2883 2884 if (spa->spa_missing_tvds != 0) { 2885 spa_load_note(spa, "vdev tree has %lld missing top-level " 2886 "vdevs.", (u_longlong_t)spa->spa_missing_tvds); 2887 if (spa->spa_trust_config && (spa->spa_mode & FWRITE)) { 2888 /* 2889 * Although theoretically we could allow users to open 2890 * incomplete pools in RW mode, we'd need to add a lot 2891 * of extra logic (e.g. adjust pool space to account 2892 * for missing vdevs). 2893 * This limitation also prevents users from accidentally 2894 * opening the pool in RW mode during data recovery and 2895 * damaging it further. 2896 */ 2897 spa_load_note(spa, "pools with missing top-level " 2898 "vdevs can only be opened in read-only mode."); 2899 error = SET_ERROR(ENXIO); 2900 } else { 2901 spa_load_note(spa, "current settings allow for maximum " 2902 "%lld missing top-level vdevs at this stage.", 2903 (u_longlong_t)spa->spa_missing_tvds_allowed); 2904 } 2905 } 2906 if (error != 0) { 2907 spa_load_failed(spa, "unable to open vdev tree [error=%d]", 2908 error); 2909 } 2910 if (spa->spa_missing_tvds != 0 || error != 0) 2911 vdev_dbgmsg_print_tree(spa->spa_root_vdev, 2); 2912 2913 return (error); 2914 } 2915 2916 /* 2917 * We need to validate the vdev labels against the configuration that 2918 * we have in hand. This function is called twice: first with an untrusted 2919 * config, then with a trusted config. The validation is more strict when the 2920 * config is trusted. 2921 */ 2922 static int 2923 spa_ld_validate_vdevs(spa_t *spa) 2924 { 2925 int error = 0; 2926 vdev_t *rvd = spa->spa_root_vdev; 2927 2928 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 2929 error = vdev_validate(rvd); 2930 spa_config_exit(spa, SCL_ALL, FTAG); 2931 2932 if (error != 0) { 2933 spa_load_failed(spa, "vdev_validate failed [error=%d]", error); 2934 return (error); 2935 } 2936 2937 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { 2938 spa_load_failed(spa, "cannot open vdev tree after invalidating " 2939 "some vdevs"); 2940 vdev_dbgmsg_print_tree(rvd, 2); 2941 return (SET_ERROR(ENXIO)); 2942 } 2943 2944 return (0); 2945 } 2946 2947 static void 2948 spa_ld_select_uberblock_done(spa_t *spa, uberblock_t *ub) 2949 { 2950 spa->spa_state = POOL_STATE_ACTIVE; 2951 spa->spa_ubsync = spa->spa_uberblock; 2952 spa->spa_verify_min_txg = spa->spa_extreme_rewind ? 2953 TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1; 2954 spa->spa_first_txg = spa->spa_last_ubsync_txg ? 2955 spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1; 2956 spa->spa_claim_max_txg = spa->spa_first_txg; 2957 spa->spa_prev_software_version = ub->ub_software_version; 2958 } 2959 2960 static int 2961 spa_ld_select_uberblock(spa_t *spa, spa_import_type_t type) 2962 { 2963 vdev_t *rvd = spa->spa_root_vdev; 2964 nvlist_t *label; 2965 uberblock_t *ub = &spa->spa_uberblock; 2966 boolean_t activity_check = B_FALSE; 2967 2968 /* 2969 * If we are opening the checkpointed state of the pool by 2970 * rewinding to it, at this point we will have written the 2971 * checkpointed uberblock to the vdev labels, so searching 2972 * the labels will find the right uberblock. However, if 2973 * we are opening the checkpointed state read-only, we have 2974 * not modified the labels. Therefore, we must ignore the 2975 * labels and continue using the spa_uberblock that was set 2976 * by spa_ld_checkpoint_rewind. 2977 * 2978 * Note that it would be fine to ignore the labels when 2979 * rewinding (opening writeable) as well. However, if we 2980 * crash just after writing the labels, we will end up 2981 * searching the labels. Doing so in the common case means 2982 * that this code path gets exercised normally, rather than 2983 * just in the edge case. 2984 */ 2985 if (ub->ub_checkpoint_txg != 0 && 2986 spa_importing_readonly_checkpoint(spa)) { 2987 spa_ld_select_uberblock_done(spa, ub); 2988 return (0); 2989 } 2990 2991 /* 2992 * Find the best uberblock. 2993 */ 2994 vdev_uberblock_load(rvd, ub, &label); 2995 2996 /* 2997 * If we weren't able to find a single valid uberblock, return failure. 2998 */ 2999 if (ub->ub_txg == 0) { 3000 nvlist_free(label); 3001 spa_load_failed(spa, "no valid uberblock found"); 3002 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO)); 3003 } 3004 3005 if (spa->spa_load_max_txg != UINT64_MAX) { 3006 (void) spa_import_progress_set_max_txg(spa, 3007 (u_longlong_t)spa->spa_load_max_txg); 3008 } 3009 spa_load_note(spa, "using uberblock with txg=%llu", 3010 (u_longlong_t)ub->ub_txg); 3011 3012 /* 3013 * For pools which have the multihost property on determine if the 3014 * pool is truly inactive and can be safely imported. Prevent 3015 * hosts which don't have a hostid set from importing the pool. 3016 */ 3017 activity_check = spa_activity_check_required(spa, ub, label, 3018 spa->spa_config); 3019 if (activity_check) { 3020 if (ub->ub_mmp_magic == MMP_MAGIC && ub->ub_mmp_delay && 3021 spa_get_hostid() == 0) { 3022 nvlist_free(label); 3023 fnvlist_add_uint64(spa->spa_load_info, 3024 ZPOOL_CONFIG_MMP_STATE, MMP_STATE_NO_HOSTID); 3025 return (spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO)); 3026 } 3027 3028 int error = spa_activity_check(spa, ub, spa->spa_config); 3029 if (error) { 3030 nvlist_free(label); 3031 return (error); 3032 } 3033 3034 fnvlist_add_uint64(spa->spa_load_info, 3035 ZPOOL_CONFIG_MMP_STATE, MMP_STATE_INACTIVE); 3036 fnvlist_add_uint64(spa->spa_load_info, 3037 ZPOOL_CONFIG_MMP_TXG, ub->ub_txg); 3038 fnvlist_add_uint16(spa->spa_load_info, 3039 ZPOOL_CONFIG_MMP_SEQ, 3040 (MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0)); 3041 } 3042 3043 /* 3044 * If the pool has an unsupported version we can't open it. 3045 */ 3046 if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) { 3047 nvlist_free(label); 3048 spa_load_failed(spa, "version %llu is not supported", 3049 (u_longlong_t)ub->ub_version); 3050 return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP)); 3051 } 3052 3053 if (ub->ub_version >= SPA_VERSION_FEATURES) { 3054 nvlist_t *features; 3055 3056 /* 3057 * If we weren't able to find what's necessary for reading the 3058 * MOS in the label, return failure. 3059 */ 3060 if (label == NULL) { 3061 spa_load_failed(spa, "label config unavailable"); 3062 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, 3063 ENXIO)); 3064 } 3065 3066 if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_FEATURES_FOR_READ, 3067 &features) != 0) { 3068 nvlist_free(label); 3069 spa_load_failed(spa, "invalid label: '%s' missing", 3070 ZPOOL_CONFIG_FEATURES_FOR_READ); 3071 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, 3072 ENXIO)); 3073 } 3074 3075 /* 3076 * Update our in-core representation with the definitive values 3077 * from the label. 3078 */ 3079 nvlist_free(spa->spa_label_features); 3080 VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0); 3081 } 3082 3083 nvlist_free(label); 3084 3085 /* 3086 * Look through entries in the label nvlist's features_for_read. If 3087 * there is a feature listed there which we don't understand then we 3088 * cannot open a pool. 3089 */ 3090 if (ub->ub_version >= SPA_VERSION_FEATURES) { 3091 nvlist_t *unsup_feat; 3092 3093 VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) == 3094 0); 3095 3096 for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features, 3097 NULL); nvp != NULL; 3098 nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) { 3099 if (!zfeature_is_supported(nvpair_name(nvp))) { 3100 VERIFY(nvlist_add_string(unsup_feat, 3101 nvpair_name(nvp), "") == 0); 3102 } 3103 } 3104 3105 if (!nvlist_empty(unsup_feat)) { 3106 VERIFY(nvlist_add_nvlist(spa->spa_load_info, 3107 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0); 3108 nvlist_free(unsup_feat); 3109 spa_load_failed(spa, "some features are unsupported"); 3110 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, 3111 ENOTSUP)); 3112 } 3113 3114 nvlist_free(unsup_feat); 3115 } 3116 3117 if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) { 3118 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 3119 spa_try_repair(spa, spa->spa_config); 3120 spa_config_exit(spa, SCL_ALL, FTAG); 3121 nvlist_free(spa->spa_config_splitting); 3122 spa->spa_config_splitting = NULL; 3123 } 3124 3125 /* 3126 * Initialize internal SPA structures. 3127 */ 3128 spa_ld_select_uberblock_done(spa, ub); 3129 3130 return (0); 3131 } 3132 3133 static int 3134 spa_ld_open_rootbp(spa_t *spa) 3135 { 3136 int error = 0; 3137 vdev_t *rvd = spa->spa_root_vdev; 3138 3139 error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool); 3140 if (error != 0) { 3141 spa_load_failed(spa, "unable to open rootbp in dsl_pool_init " 3142 "[error=%d]", error); 3143 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 3144 } 3145 spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; 3146 3147 return (0); 3148 } 3149 3150 static int 3151 spa_ld_trusted_config(spa_t *spa, spa_import_type_t type, 3152 boolean_t reloading) 3153 { 3154 vdev_t *mrvd, *rvd = spa->spa_root_vdev; 3155 nvlist_t *nv, *mos_config, *policy; 3156 int error = 0, copy_error; 3157 uint64_t healthy_tvds, healthy_tvds_mos; 3158 uint64_t mos_config_txg; 3159 3160 if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object, B_TRUE) 3161 != 0) 3162 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 3163 3164 /* 3165 * If we're assembling a pool from a split, the config provided is 3166 * already trusted so there is nothing to do. 3167 */ 3168 if (type == SPA_IMPORT_ASSEMBLE) 3169 return (0); 3170 3171 healthy_tvds = spa_healthy_core_tvds(spa); 3172 3173 if (load_nvlist(spa, spa->spa_config_object, &mos_config) 3174 != 0) { 3175 spa_load_failed(spa, "unable to retrieve MOS config"); 3176 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 3177 } 3178 3179 /* 3180 * If we are doing an open, pool owner wasn't verified yet, thus do 3181 * the verification here. 3182 */ 3183 if (spa->spa_load_state == SPA_LOAD_OPEN) { 3184 error = spa_verify_host(spa, mos_config); 3185 if (error != 0) { 3186 nvlist_free(mos_config); 3187 return (error); 3188 } 3189 } 3190 3191 nv = fnvlist_lookup_nvlist(mos_config, ZPOOL_CONFIG_VDEV_TREE); 3192 3193 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 3194 3195 /* 3196 * Build a new vdev tree from the trusted config 3197 */ 3198 VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0); 3199 3200 /* 3201 * Vdev paths in the MOS may be obsolete. If the untrusted config was 3202 * obtained by scanning /dev/dsk, then it will have the right vdev 3203 * paths. We update the trusted MOS config with this information. 3204 * We first try to copy the paths with vdev_copy_path_strict, which 3205 * succeeds only when both configs have exactly the same vdev tree. 3206 * If that fails, we fall back to a more flexible method that has a 3207 * best effort policy. 3208 */ 3209 copy_error = vdev_copy_path_strict(rvd, mrvd); 3210 if (copy_error != 0 || spa_load_print_vdev_tree) { 3211 spa_load_note(spa, "provided vdev tree:"); 3212 vdev_dbgmsg_print_tree(rvd, 2); 3213 spa_load_note(spa, "MOS vdev tree:"); 3214 vdev_dbgmsg_print_tree(mrvd, 2); 3215 } 3216 if (copy_error != 0) { 3217 spa_load_note(spa, "vdev_copy_path_strict failed, falling " 3218 "back to vdev_copy_path_relaxed"); 3219 vdev_copy_path_relaxed(rvd, mrvd); 3220 } 3221 3222 vdev_close(rvd); 3223 vdev_free(rvd); 3224 spa->spa_root_vdev = mrvd; 3225 rvd = mrvd; 3226 spa_config_exit(spa, SCL_ALL, FTAG); 3227 3228 /* 3229 * We will use spa_config if we decide to reload the spa or if spa_load 3230 * fails and we rewind. We must thus regenerate the config using the 3231 * MOS information with the updated paths. ZPOOL_LOAD_POLICY is used to 3232 * pass settings on how to load the pool and is not stored in the MOS. 3233 * We copy it over to our new, trusted config. 3234 */ 3235 mos_config_txg = fnvlist_lookup_uint64(mos_config, 3236 ZPOOL_CONFIG_POOL_TXG); 3237 nvlist_free(mos_config); 3238 mos_config = spa_config_generate(spa, NULL, mos_config_txg, B_FALSE); 3239 if (nvlist_lookup_nvlist(spa->spa_config, ZPOOL_LOAD_POLICY, 3240 &policy) == 0) 3241 fnvlist_add_nvlist(mos_config, ZPOOL_LOAD_POLICY, policy); 3242 spa_config_set(spa, mos_config); 3243 spa->spa_config_source = SPA_CONFIG_SRC_MOS; 3244 3245 /* 3246 * Now that we got the config from the MOS, we should be more strict 3247 * in checking blkptrs and can make assumptions about the consistency 3248 * of the vdev tree. spa_trust_config must be set to true before opening 3249 * vdevs in order for them to be writeable. 3250 */ 3251 spa->spa_trust_config = B_TRUE; 3252 3253 /* 3254 * Open and validate the new vdev tree 3255 */ 3256 error = spa_ld_open_vdevs(spa); 3257 if (error != 0) 3258 return (error); 3259 3260 error = spa_ld_validate_vdevs(spa); 3261 if (error != 0) 3262 return (error); 3263 3264 if (copy_error != 0 || spa_load_print_vdev_tree) { 3265 spa_load_note(spa, "final vdev tree:"); 3266 vdev_dbgmsg_print_tree(rvd, 2); 3267 } 3268 3269 if (spa->spa_load_state != SPA_LOAD_TRYIMPORT && 3270 !spa->spa_extreme_rewind && zfs_max_missing_tvds == 0) { 3271 /* 3272 * Sanity check to make sure that we are indeed loading the 3273 * latest uberblock. If we missed SPA_SYNC_MIN_VDEVS tvds 3274 * in the config provided and they happened to be the only ones 3275 * to have the latest uberblock, we could involuntarily perform 3276 * an extreme rewind. 3277 */ 3278 healthy_tvds_mos = spa_healthy_core_tvds(spa); 3279 if (healthy_tvds_mos - healthy_tvds >= 3280 SPA_SYNC_MIN_VDEVS) { 3281 spa_load_note(spa, "config provided misses too many " 3282 "top-level vdevs compared to MOS (%lld vs %lld). ", 3283 (u_longlong_t)healthy_tvds, 3284 (u_longlong_t)healthy_tvds_mos); 3285 spa_load_note(spa, "vdev tree:"); 3286 vdev_dbgmsg_print_tree(rvd, 2); 3287 if (reloading) { 3288 spa_load_failed(spa, "config was already " 3289 "provided from MOS. Aborting."); 3290 return (spa_vdev_err(rvd, 3291 VDEV_AUX_CORRUPT_DATA, EIO)); 3292 } 3293 spa_load_note(spa, "spa must be reloaded using MOS " 3294 "config"); 3295 return (SET_ERROR(EAGAIN)); 3296 } 3297 } 3298 3299 error = spa_check_for_missing_logs(spa); 3300 if (error != 0) 3301 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO)); 3302 3303 if (rvd->vdev_guid_sum != spa->spa_uberblock.ub_guid_sum) { 3304 spa_load_failed(spa, "uberblock guid sum doesn't match MOS " 3305 "guid sum (%llu != %llu)", 3306 (u_longlong_t)spa->spa_uberblock.ub_guid_sum, 3307 (u_longlong_t)rvd->vdev_guid_sum); 3308 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, 3309 ENXIO)); 3310 } 3311 3312 return (0); 3313 } 3314 3315 static int 3316 spa_ld_open_indirect_vdev_metadata(spa_t *spa) 3317 { 3318 int error = 0; 3319 vdev_t *rvd = spa->spa_root_vdev; 3320 3321 /* 3322 * Everything that we read before spa_remove_init() must be stored 3323 * on concreted vdevs. Therefore we do this as early as possible. 3324 */ 3325 error = spa_remove_init(spa); 3326 if (error != 0) { 3327 spa_load_failed(spa, "spa_remove_init failed [error=%d]", 3328 error); 3329 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 3330 } 3331 3332 /* 3333 * Retrieve information needed to condense indirect vdev mappings. 3334 */ 3335 error = spa_condense_init(spa); 3336 if (error != 0) { 3337 spa_load_failed(spa, "spa_condense_init failed [error=%d]", 3338 error); 3339 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error)); 3340 } 3341 3342 return (0); 3343 } 3344 3345 static int 3346 spa_ld_check_features(spa_t *spa, boolean_t *missing_feat_writep) 3347 { 3348 int error = 0; 3349 vdev_t *rvd = spa->spa_root_vdev; 3350 3351 if (spa_version(spa) >= SPA_VERSION_FEATURES) { 3352 boolean_t missing_feat_read = B_FALSE; 3353 nvlist_t *unsup_feat, *enabled_feat; 3354 3355 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ, 3356 &spa->spa_feat_for_read_obj, B_TRUE) != 0) { 3357 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 3358 } 3359 3360 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE, 3361 &spa->spa_feat_for_write_obj, B_TRUE) != 0) { 3362 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 3363 } 3364 3365 if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS, 3366 &spa->spa_feat_desc_obj, B_TRUE) != 0) { 3367 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 3368 } 3369 3370 enabled_feat = fnvlist_alloc(); 3371 unsup_feat = fnvlist_alloc(); 3372 3373 if (!spa_features_check(spa, B_FALSE, 3374 unsup_feat, enabled_feat)) 3375 missing_feat_read = B_TRUE; 3376 3377 if (spa_writeable(spa) || 3378 spa->spa_load_state == SPA_LOAD_TRYIMPORT) { 3379 if (!spa_features_check(spa, B_TRUE, 3380 unsup_feat, enabled_feat)) { 3381 *missing_feat_writep = B_TRUE; 3382 } 3383 } 3384 3385 fnvlist_add_nvlist(spa->spa_load_info, 3386 ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat); 3387 3388 if (!nvlist_empty(unsup_feat)) { 3389 fnvlist_add_nvlist(spa->spa_load_info, 3390 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat); 3391 } 3392 3393 fnvlist_free(enabled_feat); 3394 fnvlist_free(unsup_feat); 3395 3396 if (!missing_feat_read) { 3397 fnvlist_add_boolean(spa->spa_load_info, 3398 ZPOOL_CONFIG_CAN_RDONLY); 3399 } 3400 3401 /* 3402 * If the state is SPA_LOAD_TRYIMPORT, our objective is 3403 * twofold: to determine whether the pool is available for 3404 * import in read-write mode and (if it is not) whether the 3405 * pool is available for import in read-only mode. If the pool 3406 * is available for import in read-write mode, it is displayed 3407 * as available in userland; if it is not available for import 3408 * in read-only mode, it is displayed as unavailable in 3409 * userland. If the pool is available for import in read-only 3410 * mode but not read-write mode, it is displayed as unavailable 3411 * in userland with a special note that the pool is actually 3412 * available for open in read-only mode. 3413 * 3414 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are 3415 * missing a feature for write, we must first determine whether 3416 * the pool can be opened read-only before returning to 3417 * userland in order to know whether to display the 3418 * abovementioned note. 3419 */ 3420 if (missing_feat_read || (*missing_feat_writep && 3421 spa_writeable(spa))) { 3422 spa_load_failed(spa, "pool uses unsupported features"); 3423 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, 3424 ENOTSUP)); 3425 } 3426 3427 /* 3428 * Load refcounts for ZFS features from disk into an in-memory 3429 * cache during SPA initialization. 3430 */ 3431 for (spa_feature_t i = 0; i < SPA_FEATURES; i++) { 3432 uint64_t refcount; 3433 3434 error = feature_get_refcount_from_disk(spa, 3435 &spa_feature_table[i], &refcount); 3436 if (error == 0) { 3437 spa->spa_feat_refcount_cache[i] = refcount; 3438 } else if (error == ENOTSUP) { 3439 spa->spa_feat_refcount_cache[i] = 3440 SPA_FEATURE_DISABLED; 3441 } else { 3442 spa_load_failed(spa, "error getting refcount " 3443 "for feature %s [error=%d]", 3444 spa_feature_table[i].fi_guid, error); 3445 return (spa_vdev_err(rvd, 3446 VDEV_AUX_CORRUPT_DATA, EIO)); 3447 } 3448 } 3449 } 3450 3451 if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) { 3452 if (spa_dir_prop(spa, DMU_POOL_FEATURE_ENABLED_TXG, 3453 &spa->spa_feat_enabled_txg_obj, B_TRUE) != 0) 3454 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 3455 } 3456 3457 /* 3458 * Encryption was added before bookmark_v2, even though bookmark_v2 3459 * is now a dependency. If this pool has encryption enabled without 3460 * bookmark_v2, trigger an errata message. 3461 */ 3462 if (spa_feature_is_enabled(spa, SPA_FEATURE_ENCRYPTION) && 3463 !spa_feature_is_enabled(spa, SPA_FEATURE_BOOKMARK_V2)) { 3464 spa->spa_errata = ZPOOL_ERRATA_ZOL_8308_ENCRYPTION; 3465 } 3466 3467 return (0); 3468 } 3469 3470 static int 3471 spa_ld_load_special_directories(spa_t *spa) 3472 { 3473 int error = 0; 3474 vdev_t *rvd = spa->spa_root_vdev; 3475 3476 spa->spa_is_initializing = B_TRUE; 3477 error = dsl_pool_open(spa->spa_dsl_pool); 3478 spa->spa_is_initializing = B_FALSE; 3479 if (error != 0) { 3480 spa_load_failed(spa, "dsl_pool_open failed [error=%d]", error); 3481 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 3482 } 3483 3484 return (0); 3485 } 3486 3487 static int 3488 spa_ld_get_props(spa_t *spa) 3489 { 3490 int error = 0; 3491 uint64_t obj; 3492 vdev_t *rvd = spa->spa_root_vdev; 3493 3494 /* Grab the secret checksum salt from the MOS. */ 3495 error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 3496 DMU_POOL_CHECKSUM_SALT, 1, 3497 sizeof (spa->spa_cksum_salt.zcs_bytes), 3498 spa->spa_cksum_salt.zcs_bytes); 3499 if (error == ENOENT) { 3500 /* Generate a new salt for subsequent use */ 3501 (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes, 3502 sizeof (spa->spa_cksum_salt.zcs_bytes)); 3503 } else if (error != 0) { 3504 spa_load_failed(spa, "unable to retrieve checksum salt from " 3505 "MOS [error=%d]", error); 3506 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 3507 } 3508 3509 if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj, B_TRUE) != 0) 3510 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 3511 error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj); 3512 if (error != 0) { 3513 spa_load_failed(spa, "error opening deferred-frees bpobj " 3514 "[error=%d]", error); 3515 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 3516 } 3517 3518 /* 3519 * Load the bit that tells us to use the new accounting function 3520 * (raid-z deflation). If we have an older pool, this will not 3521 * be present. 3522 */ 3523 error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate, B_FALSE); 3524 if (error != 0 && error != ENOENT) 3525 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 3526 3527 error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION, 3528 &spa->spa_creation_version, B_FALSE); 3529 if (error != 0 && error != ENOENT) 3530 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 3531 3532 /* 3533 * Load the persistent error log. If we have an older pool, this will 3534 * not be present. 3535 */ 3536 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last, 3537 B_FALSE); 3538 if (error != 0 && error != ENOENT) 3539 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 3540 3541 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB, 3542 &spa->spa_errlog_scrub, B_FALSE); 3543 if (error != 0 && error != ENOENT) 3544 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 3545 3546 /* 3547 * Load the history object. If we have an older pool, this 3548 * will not be present. 3549 */ 3550 error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history, B_FALSE); 3551 if (error != 0 && error != ENOENT) 3552 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 3553 3554 /* 3555 * Load the per-vdev ZAP map. If we have an older pool, this will not 3556 * be present; in this case, defer its creation to a later time to 3557 * avoid dirtying the MOS this early / out of sync context. See 3558 * spa_sync_config_object. 3559 */ 3560 3561 /* The sentinel is only available in the MOS config. */ 3562 nvlist_t *mos_config; 3563 if (load_nvlist(spa, spa->spa_config_object, &mos_config) != 0) { 3564 spa_load_failed(spa, "unable to retrieve MOS config"); 3565 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 3566 } 3567 3568 error = spa_dir_prop(spa, DMU_POOL_VDEV_ZAP_MAP, 3569 &spa->spa_all_vdev_zaps, B_FALSE); 3570 3571 if (error == ENOENT) { 3572 VERIFY(!nvlist_exists(mos_config, 3573 ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)); 3574 spa->spa_avz_action = AVZ_ACTION_INITIALIZE; 3575 ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev)); 3576 } else if (error != 0) { 3577 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 3578 } else if (!nvlist_exists(mos_config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)) { 3579 /* 3580 * An older version of ZFS overwrote the sentinel value, so 3581 * we have orphaned per-vdev ZAPs in the MOS. Defer their 3582 * destruction to later; see spa_sync_config_object. 3583 */ 3584 spa->spa_avz_action = AVZ_ACTION_DESTROY; 3585 /* 3586 * We're assuming that no vdevs have had their ZAPs created 3587 * before this. Better be sure of it. 3588 */ 3589 ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev)); 3590 } 3591 nvlist_free(mos_config); 3592 3593 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 3594 3595 error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object, 3596 B_FALSE); 3597 if (error && error != ENOENT) 3598 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 3599 3600 if (error == 0) { 3601 uint64_t autoreplace; 3602 3603 spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs); 3604 spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace); 3605 spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation); 3606 spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode); 3607 spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand); 3608 spa_prop_find(spa, ZPOOL_PROP_BOOTSIZE, &spa->spa_bootsize); 3609 spa_prop_find(spa, ZPOOL_PROP_MULTIHOST, &spa->spa_multihost); 3610 spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO, 3611 &spa->spa_dedup_ditto); 3612 spa_prop_find(spa, ZPOOL_PROP_AUTOTRIM, &spa->spa_autotrim); 3613 spa->spa_autoreplace = (autoreplace != 0); 3614 } 3615 3616 /* 3617 * If we are importing a pool with missing top-level vdevs, 3618 * we enforce that the pool doesn't panic or get suspended on 3619 * error since the likelihood of missing data is extremely high. 3620 */ 3621 if (spa->spa_missing_tvds > 0 && 3622 spa->spa_failmode != ZIO_FAILURE_MODE_CONTINUE && 3623 spa->spa_load_state != SPA_LOAD_TRYIMPORT) { 3624 spa_load_note(spa, "forcing failmode to 'continue' " 3625 "as some top level vdevs are missing"); 3626 spa->spa_failmode = ZIO_FAILURE_MODE_CONTINUE; 3627 } 3628 3629 return (0); 3630 } 3631 3632 static int 3633 spa_ld_open_aux_vdevs(spa_t *spa, spa_import_type_t type) 3634 { 3635 int error = 0; 3636 vdev_t *rvd = spa->spa_root_vdev; 3637 3638 /* 3639 * If we're assembling the pool from the split-off vdevs of 3640 * an existing pool, we don't want to attach the spares & cache 3641 * devices. 3642 */ 3643 3644 /* 3645 * Load any hot spares for this pool. 3646 */ 3647 error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object, 3648 B_FALSE); 3649 if (error != 0 && error != ENOENT) 3650 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 3651 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { 3652 ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); 3653 if (load_nvlist(spa, spa->spa_spares.sav_object, 3654 &spa->spa_spares.sav_config) != 0) { 3655 spa_load_failed(spa, "error loading spares nvlist"); 3656 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 3657 } 3658 3659 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 3660 spa_load_spares(spa); 3661 spa_config_exit(spa, SCL_ALL, FTAG); 3662 } else if (error == 0) { 3663 spa->spa_spares.sav_sync = B_TRUE; 3664 } 3665 3666 /* 3667 * Load any level 2 ARC devices for this pool. 3668 */ 3669 error = spa_dir_prop(spa, DMU_POOL_L2CACHE, 3670 &spa->spa_l2cache.sav_object, B_FALSE); 3671 if (error != 0 && error != ENOENT) 3672 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 3673 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { 3674 ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); 3675 if (load_nvlist(spa, spa->spa_l2cache.sav_object, 3676 &spa->spa_l2cache.sav_config) != 0) { 3677 spa_load_failed(spa, "error loading l2cache nvlist"); 3678 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 3679 } 3680 3681 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 3682 spa_load_l2cache(spa); 3683 spa_config_exit(spa, SCL_ALL, FTAG); 3684 } else if (error == 0) { 3685 spa->spa_l2cache.sav_sync = B_TRUE; 3686 } 3687 3688 return (0); 3689 } 3690 3691 static int 3692 spa_ld_load_vdev_metadata(spa_t *spa) 3693 { 3694 int error = 0; 3695 vdev_t *rvd = spa->spa_root_vdev; 3696 3697 /* 3698 * If the 'multihost' property is set, then never allow a pool to 3699 * be imported when the system hostid is zero. The exception to 3700 * this rule is zdb which is always allowed to access pools. 3701 */ 3702 if (spa_multihost(spa) && spa_get_hostid() == 0 && 3703 (spa->spa_import_flags & ZFS_IMPORT_SKIP_MMP) == 0) { 3704 fnvlist_add_uint64(spa->spa_load_info, 3705 ZPOOL_CONFIG_MMP_STATE, MMP_STATE_NO_HOSTID); 3706 return (spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO)); 3707 } 3708 3709 /* 3710 * If the 'autoreplace' property is set, then post a resource notifying 3711 * the ZFS DE that it should not issue any faults for unopenable 3712 * devices. We also iterate over the vdevs, and post a sysevent for any 3713 * unopenable vdevs so that the normal autoreplace handler can take 3714 * over. 3715 */ 3716 if (spa->spa_autoreplace && spa->spa_load_state != SPA_LOAD_TRYIMPORT) { 3717 spa_check_removed(spa->spa_root_vdev); 3718 /* 3719 * For the import case, this is done in spa_import(), because 3720 * at this point we're using the spare definitions from 3721 * the MOS config, not necessarily from the userland config. 3722 */ 3723 if (spa->spa_load_state != SPA_LOAD_IMPORT) { 3724 spa_aux_check_removed(&spa->spa_spares); 3725 spa_aux_check_removed(&spa->spa_l2cache); 3726 } 3727 } 3728 3729 /* 3730 * Load the vdev metadata such as metaslabs, DTLs, spacemap object, etc. 3731 */ 3732 error = vdev_load(rvd); 3733 if (error != 0) { 3734 spa_load_failed(spa, "vdev_load failed [error=%d]", error); 3735 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error)); 3736 } 3737 3738 error = spa_ld_log_spacemaps(spa); 3739 if (error != 0) { 3740 spa_load_failed(spa, "spa_ld_log_sm_data failed [error=%d]", 3741 error); 3742 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error)); 3743 } 3744 3745 /* 3746 * Propagate the leaf DTLs we just loaded all the way up the vdev tree. 3747 */ 3748 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 3749 vdev_dtl_reassess(rvd, 0, 0, B_FALSE); 3750 spa_config_exit(spa, SCL_ALL, FTAG); 3751 3752 return (0); 3753 } 3754 3755 static int 3756 spa_ld_load_dedup_tables(spa_t *spa) 3757 { 3758 int error = 0; 3759 vdev_t *rvd = spa->spa_root_vdev; 3760 3761 error = ddt_load(spa); 3762 if (error != 0) { 3763 spa_load_failed(spa, "ddt_load failed [error=%d]", error); 3764 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); 3765 } 3766 3767 return (0); 3768 } 3769 3770 static int 3771 spa_ld_verify_logs(spa_t *spa, spa_import_type_t type, char **ereport) 3772 { 3773 vdev_t *rvd = spa->spa_root_vdev; 3774 3775 if (type != SPA_IMPORT_ASSEMBLE && spa_writeable(spa)) { 3776 boolean_t missing = spa_check_logs(spa); 3777 if (missing) { 3778 if (spa->spa_missing_tvds != 0) { 3779 spa_load_note(spa, "spa_check_logs failed " 3780 "so dropping the logs"); 3781 } else { 3782 *ereport = FM_EREPORT_ZFS_LOG_REPLAY; 3783 spa_load_failed(spa, "spa_check_logs failed"); 3784 return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, 3785 ENXIO)); 3786 } 3787 } 3788 } 3789 3790 return (0); 3791 } 3792 3793 static int 3794 spa_ld_verify_pool_data(spa_t *spa) 3795 { 3796 int error = 0; 3797 vdev_t *rvd = spa->spa_root_vdev; 3798 3799 /* 3800 * We've successfully opened the pool, verify that we're ready 3801 * to start pushing transactions. 3802 */ 3803 if (spa->spa_load_state != SPA_LOAD_TRYIMPORT) { 3804 error = spa_load_verify(spa); 3805 if (error != 0) { 3806 spa_load_failed(spa, "spa_load_verify failed " 3807 "[error=%d]", error); 3808 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, 3809 error)); 3810 } 3811 } 3812 3813 return (0); 3814 } 3815 3816 static void 3817 spa_ld_claim_log_blocks(spa_t *spa) 3818 { 3819 dmu_tx_t *tx; 3820 dsl_pool_t *dp = spa_get_dsl(spa); 3821 3822 /* 3823 * Claim log blocks that haven't been committed yet. 3824 * This must all happen in a single txg. 3825 * Note: spa_claim_max_txg is updated by spa_claim_notify(), 3826 * invoked from zil_claim_log_block()'s i/o done callback. 3827 * Price of rollback is that we abandon the log. 3828 */ 3829 spa->spa_claiming = B_TRUE; 3830 3831 tx = dmu_tx_create_assigned(dp, spa_first_txg(spa)); 3832 (void) dmu_objset_find_dp(dp, dp->dp_root_dir_obj, 3833 zil_claim, tx, DS_FIND_CHILDREN); 3834 dmu_tx_commit(tx); 3835 3836 spa->spa_claiming = B_FALSE; 3837 3838 spa_set_log_state(spa, SPA_LOG_GOOD); 3839 } 3840 3841 static void 3842 spa_ld_check_for_config_update(spa_t *spa, uint64_t config_cache_txg, 3843 boolean_t update_config_cache) 3844 { 3845 vdev_t *rvd = spa->spa_root_vdev; 3846 int need_update = B_FALSE; 3847 3848 /* 3849 * If the config cache is stale, or we have uninitialized 3850 * metaslabs (see spa_vdev_add()), then update the config. 3851 * 3852 * If this is a verbatim import, trust the current 3853 * in-core spa_config and update the disk labels. 3854 */ 3855 if (update_config_cache || config_cache_txg != spa->spa_config_txg || 3856 spa->spa_load_state == SPA_LOAD_IMPORT || 3857 spa->spa_load_state == SPA_LOAD_RECOVER || 3858 (spa->spa_import_flags & ZFS_IMPORT_VERBATIM)) 3859 need_update = B_TRUE; 3860 3861 for (int c = 0; c < rvd->vdev_children; c++) 3862 if (rvd->vdev_child[c]->vdev_ms_array == 0) 3863 need_update = B_TRUE; 3864 3865 /* 3866 * Update the config cache asychronously in case we're the 3867 * root pool, in which case the config cache isn't writable yet. 3868 */ 3869 if (need_update) 3870 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 3871 } 3872 3873 static void 3874 spa_ld_prepare_for_reload(spa_t *spa) 3875 { 3876 int mode = spa->spa_mode; 3877 int async_suspended = spa->spa_async_suspended; 3878 3879 spa_unload(spa); 3880 spa_deactivate(spa); 3881 spa_activate(spa, mode); 3882 3883 /* 3884 * We save the value of spa_async_suspended as it gets reset to 0 by 3885 * spa_unload(). We want to restore it back to the original value before 3886 * returning as we might be calling spa_async_resume() later. 3887 */ 3888 spa->spa_async_suspended = async_suspended; 3889 } 3890 3891 static int 3892 spa_ld_read_checkpoint_txg(spa_t *spa) 3893 { 3894 uberblock_t checkpoint; 3895 int error = 0; 3896 3897 ASSERT0(spa->spa_checkpoint_txg); 3898 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 3899 3900 error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 3901 DMU_POOL_ZPOOL_CHECKPOINT, sizeof (uint64_t), 3902 sizeof (uberblock_t) / sizeof (uint64_t), &checkpoint); 3903 3904 if (error == ENOENT) 3905 return (0); 3906 3907 if (error != 0) 3908 return (error); 3909 3910 ASSERT3U(checkpoint.ub_txg, !=, 0); 3911 ASSERT3U(checkpoint.ub_checkpoint_txg, !=, 0); 3912 ASSERT3U(checkpoint.ub_timestamp, !=, 0); 3913 spa->spa_checkpoint_txg = checkpoint.ub_txg; 3914 spa->spa_checkpoint_info.sci_timestamp = checkpoint.ub_timestamp; 3915 3916 return (0); 3917 } 3918 3919 static int 3920 spa_ld_mos_init(spa_t *spa, spa_import_type_t type) 3921 { 3922 int error = 0; 3923 3924 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 3925 ASSERT(spa->spa_config_source != SPA_CONFIG_SRC_NONE); 3926 3927 /* 3928 * Never trust the config that is provided unless we are assembling 3929 * a pool following a split. 3930 * This means don't trust blkptrs and the vdev tree in general. This 3931 * also effectively puts the spa in read-only mode since 3932 * spa_writeable() checks for spa_trust_config to be true. 3933 * We will later load a trusted config from the MOS. 3934 */ 3935 if (type != SPA_IMPORT_ASSEMBLE) 3936 spa->spa_trust_config = B_FALSE; 3937 3938 /* 3939 * Parse the config provided to create a vdev tree. 3940 */ 3941 error = spa_ld_parse_config(spa, type); 3942 if (error != 0) 3943 return (error); 3944 3945 spa_import_progress_add(spa); 3946 3947 /* 3948 * Now that we have the vdev tree, try to open each vdev. This involves 3949 * opening the underlying physical device, retrieving its geometry and 3950 * probing the vdev with a dummy I/O. The state of each vdev will be set 3951 * based on the success of those operations. After this we'll be ready 3952 * to read from the vdevs. 3953 */ 3954 error = spa_ld_open_vdevs(spa); 3955 if (error != 0) 3956 return (error); 3957 3958 /* 3959 * Read the label of each vdev and make sure that the GUIDs stored 3960 * there match the GUIDs in the config provided. 3961 * If we're assembling a new pool that's been split off from an 3962 * existing pool, the labels haven't yet been updated so we skip 3963 * validation for now. 3964 */ 3965 if (type != SPA_IMPORT_ASSEMBLE) { 3966 error = spa_ld_validate_vdevs(spa); 3967 if (error != 0) 3968 return (error); 3969 } 3970 3971 /* 3972 * Read all vdev labels to find the best uberblock (i.e. latest, 3973 * unless spa_load_max_txg is set) and store it in spa_uberblock. We 3974 * get the list of features required to read blkptrs in the MOS from 3975 * the vdev label with the best uberblock and verify that our version 3976 * of zfs supports them all. 3977 */ 3978 error = spa_ld_select_uberblock(spa, type); 3979 if (error != 0) 3980 return (error); 3981 3982 /* 3983 * Pass that uberblock to the dsl_pool layer which will open the root 3984 * blkptr. This blkptr points to the latest version of the MOS and will 3985 * allow us to read its contents. 3986 */ 3987 error = spa_ld_open_rootbp(spa); 3988 if (error != 0) 3989 return (error); 3990 3991 return (0); 3992 } 3993 3994 static int 3995 spa_ld_checkpoint_rewind(spa_t *spa) 3996 { 3997 uberblock_t checkpoint; 3998 int error = 0; 3999 4000 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 4001 ASSERT(spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT); 4002 4003 error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 4004 DMU_POOL_ZPOOL_CHECKPOINT, sizeof (uint64_t), 4005 sizeof (uberblock_t) / sizeof (uint64_t), &checkpoint); 4006 4007 if (error != 0) { 4008 spa_load_failed(spa, "unable to retrieve checkpointed " 4009 "uberblock from the MOS config [error=%d]", error); 4010 4011 if (error == ENOENT) 4012 error = ZFS_ERR_NO_CHECKPOINT; 4013 4014 return (error); 4015 } 4016 4017 ASSERT3U(checkpoint.ub_txg, <, spa->spa_uberblock.ub_txg); 4018 ASSERT3U(checkpoint.ub_txg, ==, checkpoint.ub_checkpoint_txg); 4019 4020 /* 4021 * We need to update the txg and timestamp of the checkpointed 4022 * uberblock to be higher than the latest one. This ensures that 4023 * the checkpointed uberblock is selected if we were to close and 4024 * reopen the pool right after we've written it in the vdev labels. 4025 * (also see block comment in vdev_uberblock_compare) 4026 */ 4027 checkpoint.ub_txg = spa->spa_uberblock.ub_txg + 1; 4028 checkpoint.ub_timestamp = gethrestime_sec(); 4029 4030 /* 4031 * Set current uberblock to be the checkpointed uberblock. 4032 */ 4033 spa->spa_uberblock = checkpoint; 4034 4035 /* 4036 * If we are doing a normal rewind, then the pool is open for 4037 * writing and we sync the "updated" checkpointed uberblock to 4038 * disk. Once this is done, we've basically rewound the whole 4039 * pool and there is no way back. 4040 * 4041 * There are cases when we don't want to attempt and sync the 4042 * checkpointed uberblock to disk because we are opening a 4043 * pool as read-only. Specifically, verifying the checkpointed 4044 * state with zdb, and importing the checkpointed state to get 4045 * a "preview" of its content. 4046 */ 4047 if (spa_writeable(spa)) { 4048 vdev_t *rvd = spa->spa_root_vdev; 4049 4050 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 4051 vdev_t *svd[SPA_SYNC_MIN_VDEVS] = { NULL }; 4052 int svdcount = 0; 4053 int children = rvd->vdev_children; 4054 int c0 = spa_get_random(children); 4055 4056 for (int c = 0; c < children; c++) { 4057 vdev_t *vd = rvd->vdev_child[(c0 + c) % children]; 4058 4059 /* Stop when revisiting the first vdev */ 4060 if (c > 0 && svd[0] == vd) 4061 break; 4062 4063 if (vd->vdev_ms_array == 0 || vd->vdev_islog || 4064 !vdev_is_concrete(vd)) 4065 continue; 4066 4067 svd[svdcount++] = vd; 4068 if (svdcount == SPA_SYNC_MIN_VDEVS) 4069 break; 4070 } 4071 error = vdev_config_sync(svd, svdcount, spa->spa_first_txg); 4072 if (error == 0) 4073 spa->spa_last_synced_guid = rvd->vdev_guid; 4074 spa_config_exit(spa, SCL_ALL, FTAG); 4075 4076 if (error != 0) { 4077 spa_load_failed(spa, "failed to write checkpointed " 4078 "uberblock to the vdev labels [error=%d]", error); 4079 return (error); 4080 } 4081 } 4082 4083 return (0); 4084 } 4085 4086 static int 4087 spa_ld_mos_with_trusted_config(spa_t *spa, spa_import_type_t type, 4088 boolean_t *update_config_cache) 4089 { 4090 int error; 4091 4092 /* 4093 * Parse the config for pool, open and validate vdevs, 4094 * select an uberblock, and use that uberblock to open 4095 * the MOS. 4096 */ 4097 error = spa_ld_mos_init(spa, type); 4098 if (error != 0) 4099 return (error); 4100 4101 /* 4102 * Retrieve the trusted config stored in the MOS and use it to create 4103 * a new, exact version of the vdev tree, then reopen all vdevs. 4104 */ 4105 error = spa_ld_trusted_config(spa, type, B_FALSE); 4106 if (error == EAGAIN) { 4107 if (update_config_cache != NULL) 4108 *update_config_cache = B_TRUE; 4109 4110 /* 4111 * Redo the loading process with the trusted config if it is 4112 * too different from the untrusted config. 4113 */ 4114 spa_ld_prepare_for_reload(spa); 4115 spa_load_note(spa, "RELOADING"); 4116 error = spa_ld_mos_init(spa, type); 4117 if (error != 0) 4118 return (error); 4119 4120 error = spa_ld_trusted_config(spa, type, B_TRUE); 4121 if (error != 0) 4122 return (error); 4123 4124 } else if (error != 0) { 4125 return (error); 4126 } 4127 4128 return (0); 4129 } 4130 4131 /* 4132 * Load an existing storage pool, using the config provided. This config 4133 * describes which vdevs are part of the pool and is later validated against 4134 * partial configs present in each vdev's label and an entire copy of the 4135 * config stored in the MOS. 4136 */ 4137 static int 4138 spa_load_impl(spa_t *spa, spa_import_type_t type, char **ereport) 4139 { 4140 int error = 0; 4141 boolean_t missing_feat_write = B_FALSE; 4142 boolean_t checkpoint_rewind = 4143 (spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT); 4144 boolean_t update_config_cache = B_FALSE; 4145 4146 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 4147 ASSERT(spa->spa_config_source != SPA_CONFIG_SRC_NONE); 4148 4149 spa_load_note(spa, "LOADING"); 4150 4151 error = spa_ld_mos_with_trusted_config(spa, type, &update_config_cache); 4152 if (error != 0) 4153 return (error); 4154 4155 /* 4156 * If we are rewinding to the checkpoint then we need to repeat 4157 * everything we've done so far in this function but this time 4158 * selecting the checkpointed uberblock and using that to open 4159 * the MOS. 4160 */ 4161 if (checkpoint_rewind) { 4162 /* 4163 * If we are rewinding to the checkpoint update config cache 4164 * anyway. 4165 */ 4166 update_config_cache = B_TRUE; 4167 4168 /* 4169 * Extract the checkpointed uberblock from the current MOS 4170 * and use this as the pool's uberblock from now on. If the 4171 * pool is imported as writeable we also write the checkpoint 4172 * uberblock to the labels, making the rewind permanent. 4173 */ 4174 error = spa_ld_checkpoint_rewind(spa); 4175 if (error != 0) 4176 return (error); 4177 4178 /* 4179 * Redo the loading process process again with the 4180 * checkpointed uberblock. 4181 */ 4182 spa_ld_prepare_for_reload(spa); 4183 spa_load_note(spa, "LOADING checkpointed uberblock"); 4184 error = spa_ld_mos_with_trusted_config(spa, type, NULL); 4185 if (error != 0) 4186 return (error); 4187 } 4188 4189 /* 4190 * Retrieve the checkpoint txg if the pool has a checkpoint. 4191 */ 4192 error = spa_ld_read_checkpoint_txg(spa); 4193 if (error != 0) 4194 return (error); 4195 4196 /* 4197 * Retrieve the mapping of indirect vdevs. Those vdevs were removed 4198 * from the pool and their contents were re-mapped to other vdevs. Note 4199 * that everything that we read before this step must have been 4200 * rewritten on concrete vdevs after the last device removal was 4201 * initiated. Otherwise we could be reading from indirect vdevs before 4202 * we have loaded their mappings. 4203 */ 4204 error = spa_ld_open_indirect_vdev_metadata(spa); 4205 if (error != 0) 4206 return (error); 4207 4208 /* 4209 * Retrieve the full list of active features from the MOS and check if 4210 * they are all supported. 4211 */ 4212 error = spa_ld_check_features(spa, &missing_feat_write); 4213 if (error != 0) 4214 return (error); 4215 4216 /* 4217 * Load several special directories from the MOS needed by the dsl_pool 4218 * layer. 4219 */ 4220 error = spa_ld_load_special_directories(spa); 4221 if (error != 0) 4222 return (error); 4223 4224 /* 4225 * Retrieve pool properties from the MOS. 4226 */ 4227 error = spa_ld_get_props(spa); 4228 if (error != 0) 4229 return (error); 4230 4231 /* 4232 * Retrieve the list of auxiliary devices - cache devices and spares - 4233 * and open them. 4234 */ 4235 error = spa_ld_open_aux_vdevs(spa, type); 4236 if (error != 0) 4237 return (error); 4238 4239 /* 4240 * Load the metadata for all vdevs. Also check if unopenable devices 4241 * should be autoreplaced. 4242 */ 4243 error = spa_ld_load_vdev_metadata(spa); 4244 if (error != 0) 4245 return (error); 4246 4247 error = spa_ld_load_dedup_tables(spa); 4248 if (error != 0) 4249 return (error); 4250 4251 /* 4252 * Verify the logs now to make sure we don't have any unexpected errors 4253 * when we claim log blocks later. 4254 */ 4255 error = spa_ld_verify_logs(spa, type, ereport); 4256 if (error != 0) 4257 return (error); 4258 4259 if (missing_feat_write) { 4260 ASSERT(spa->spa_load_state == SPA_LOAD_TRYIMPORT); 4261 4262 /* 4263 * At this point, we know that we can open the pool in 4264 * read-only mode but not read-write mode. We now have enough 4265 * information and can return to userland. 4266 */ 4267 return (spa_vdev_err(spa->spa_root_vdev, VDEV_AUX_UNSUP_FEAT, 4268 ENOTSUP)); 4269 } 4270 4271 /* 4272 * Traverse the last txgs to make sure the pool was left off in a safe 4273 * state. When performing an extreme rewind, we verify the whole pool, 4274 * which can take a very long time. 4275 */ 4276 error = spa_ld_verify_pool_data(spa); 4277 if (error != 0) 4278 return (error); 4279 4280 /* 4281 * Calculate the deflated space for the pool. This must be done before 4282 * we write anything to the pool because we'd need to update the space 4283 * accounting using the deflated sizes. 4284 */ 4285 spa_update_dspace(spa); 4286 4287 /* 4288 * We have now retrieved all the information we needed to open the 4289 * pool. If we are importing the pool in read-write mode, a few 4290 * additional steps must be performed to finish the import. 4291 */ 4292 if (spa_writeable(spa) && (spa->spa_load_state == SPA_LOAD_RECOVER || 4293 spa->spa_load_max_txg == UINT64_MAX)) { 4294 uint64_t config_cache_txg = spa->spa_config_txg; 4295 4296 ASSERT(spa->spa_load_state != SPA_LOAD_TRYIMPORT); 4297 4298 /* 4299 * In case of a checkpoint rewind, log the original txg 4300 * of the checkpointed uberblock. 4301 */ 4302 if (checkpoint_rewind) { 4303 spa_history_log_internal(spa, "checkpoint rewind", 4304 NULL, "rewound state to txg=%llu", 4305 (u_longlong_t)spa->spa_uberblock.ub_checkpoint_txg); 4306 } 4307 4308 /* 4309 * Traverse the ZIL and claim all blocks. 4310 */ 4311 spa_ld_claim_log_blocks(spa); 4312 4313 /* 4314 * Kick-off the syncing thread. 4315 */ 4316 spa->spa_sync_on = B_TRUE; 4317 txg_sync_start(spa->spa_dsl_pool); 4318 mmp_thread_start(spa); 4319 4320 /* 4321 * Wait for all claims to sync. We sync up to the highest 4322 * claimed log block birth time so that claimed log blocks 4323 * don't appear to be from the future. spa_claim_max_txg 4324 * will have been set for us by ZIL traversal operations 4325 * performed above. 4326 */ 4327 txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg); 4328 4329 /* 4330 * Check if we need to request an update of the config. On the 4331 * next sync, we would update the config stored in vdev labels 4332 * and the cachefile (by default /etc/zfs/zpool.cache). 4333 */ 4334 spa_ld_check_for_config_update(spa, config_cache_txg, 4335 update_config_cache); 4336 4337 /* 4338 * Check all DTLs to see if anything needs resilvering. 4339 */ 4340 if (!dsl_scan_resilvering(spa->spa_dsl_pool) && 4341 vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) 4342 spa_async_request(spa, SPA_ASYNC_RESILVER); 4343 4344 /* 4345 * Log the fact that we booted up (so that we can detect if 4346 * we rebooted in the middle of an operation). 4347 */ 4348 spa_history_log_version(spa, "open"); 4349 4350 spa_restart_removal(spa); 4351 spa_spawn_aux_threads(spa); 4352 4353 /* 4354 * Delete any inconsistent datasets. 4355 * 4356 * Note: 4357 * Since we may be issuing deletes for clones here, 4358 * we make sure to do so after we've spawned all the 4359 * auxiliary threads above (from which the livelist 4360 * deletion zthr is part of). 4361 */ 4362 (void) dmu_objset_find(spa_name(spa), 4363 dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN); 4364 4365 /* 4366 * Clean up any stale temporary dataset userrefs. 4367 */ 4368 dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool); 4369 4370 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 4371 vdev_initialize_restart(spa->spa_root_vdev); 4372 vdev_trim_restart(spa->spa_root_vdev); 4373 vdev_autotrim_restart(spa); 4374 spa_config_exit(spa, SCL_CONFIG, FTAG); 4375 } 4376 4377 spa_import_progress_remove(spa); 4378 spa_async_request(spa, SPA_ASYNC_L2CACHE_REBUILD); 4379 4380 spa_load_note(spa, "LOADED"); 4381 4382 return (0); 4383 } 4384 4385 static int 4386 spa_load_retry(spa_t *spa, spa_load_state_t state) 4387 { 4388 int mode = spa->spa_mode; 4389 4390 spa_unload(spa); 4391 spa_deactivate(spa); 4392 4393 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg - 1; 4394 4395 spa_activate(spa, mode); 4396 spa_async_suspend(spa); 4397 4398 spa_load_note(spa, "spa_load_retry: rewind, max txg: %llu", 4399 (u_longlong_t)spa->spa_load_max_txg); 4400 4401 return (spa_load(spa, state, SPA_IMPORT_EXISTING)); 4402 } 4403 4404 /* 4405 * If spa_load() fails this function will try loading prior txg's. If 4406 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool 4407 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this 4408 * function will not rewind the pool and will return the same error as 4409 * spa_load(). 4410 */ 4411 static int 4412 spa_load_best(spa_t *spa, spa_load_state_t state, uint64_t max_request, 4413 int rewind_flags) 4414 { 4415 nvlist_t *loadinfo = NULL; 4416 nvlist_t *config = NULL; 4417 int load_error, rewind_error; 4418 uint64_t safe_rewind_txg; 4419 uint64_t min_txg; 4420 4421 if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) { 4422 spa->spa_load_max_txg = spa->spa_load_txg; 4423 spa_set_log_state(spa, SPA_LOG_CLEAR); 4424 } else { 4425 spa->spa_load_max_txg = max_request; 4426 if (max_request != UINT64_MAX) 4427 spa->spa_extreme_rewind = B_TRUE; 4428 } 4429 4430 load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING); 4431 if (load_error == 0) 4432 return (0); 4433 if (load_error == ZFS_ERR_NO_CHECKPOINT) { 4434 /* 4435 * When attempting checkpoint-rewind on a pool with no 4436 * checkpoint, we should not attempt to load uberblocks 4437 * from previous txgs when spa_load fails. 4438 */ 4439 ASSERT(spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT); 4440 spa_import_progress_remove(spa); 4441 return (load_error); 4442 } 4443 4444 if (spa->spa_root_vdev != NULL) 4445 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 4446 4447 spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg; 4448 spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp; 4449 4450 if (rewind_flags & ZPOOL_NEVER_REWIND) { 4451 nvlist_free(config); 4452 spa_import_progress_remove(spa); 4453 return (load_error); 4454 } 4455 4456 if (state == SPA_LOAD_RECOVER) { 4457 /* Price of rolling back is discarding txgs, including log */ 4458 spa_set_log_state(spa, SPA_LOG_CLEAR); 4459 } else { 4460 /* 4461 * If we aren't rolling back save the load info from our first 4462 * import attempt so that we can restore it after attempting 4463 * to rewind. 4464 */ 4465 loadinfo = spa->spa_load_info; 4466 spa->spa_load_info = fnvlist_alloc(); 4467 } 4468 4469 spa->spa_load_max_txg = spa->spa_last_ubsync_txg; 4470 safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE; 4471 min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ? 4472 TXG_INITIAL : safe_rewind_txg; 4473 4474 /* 4475 * Continue as long as we're finding errors, we're still within 4476 * the acceptable rewind range, and we're still finding uberblocks 4477 */ 4478 while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg && 4479 spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) { 4480 if (spa->spa_load_max_txg < safe_rewind_txg) 4481 spa->spa_extreme_rewind = B_TRUE; 4482 rewind_error = spa_load_retry(spa, state); 4483 } 4484 4485 spa->spa_extreme_rewind = B_FALSE; 4486 spa->spa_load_max_txg = UINT64_MAX; 4487 4488 if (config && (rewind_error || state != SPA_LOAD_RECOVER)) 4489 spa_config_set(spa, config); 4490 else 4491 nvlist_free(config); 4492 4493 if (state == SPA_LOAD_RECOVER) { 4494 ASSERT3P(loadinfo, ==, NULL); 4495 spa_import_progress_remove(spa); 4496 return (rewind_error); 4497 } else { 4498 /* Store the rewind info as part of the initial load info */ 4499 fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO, 4500 spa->spa_load_info); 4501 4502 /* Restore the initial load info */ 4503 fnvlist_free(spa->spa_load_info); 4504 spa->spa_load_info = loadinfo; 4505 4506 spa_import_progress_remove(spa); 4507 return (load_error); 4508 } 4509 } 4510 4511 /* 4512 * Pool Open/Import 4513 * 4514 * The import case is identical to an open except that the configuration is sent 4515 * down from userland, instead of grabbed from the configuration cache. For the 4516 * case of an open, the pool configuration will exist in the 4517 * POOL_STATE_UNINITIALIZED state. 4518 * 4519 * The stats information (gen/count/ustats) is used to gather vdev statistics at 4520 * the same time open the pool, without having to keep around the spa_t in some 4521 * ambiguous state. 4522 */ 4523 static int 4524 spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy, 4525 nvlist_t **config) 4526 { 4527 spa_t *spa; 4528 spa_load_state_t state = SPA_LOAD_OPEN; 4529 int error; 4530 int locked = B_FALSE; 4531 4532 *spapp = NULL; 4533 4534 /* 4535 * As disgusting as this is, we need to support recursive calls to this 4536 * function because dsl_dir_open() is called during spa_load(), and ends 4537 * up calling spa_open() again. The real fix is to figure out how to 4538 * avoid dsl_dir_open() calling this in the first place. 4539 */ 4540 if (mutex_owner(&spa_namespace_lock) != curthread) { 4541 mutex_enter(&spa_namespace_lock); 4542 locked = B_TRUE; 4543 } 4544 4545 if ((spa = spa_lookup(pool)) == NULL) { 4546 if (locked) 4547 mutex_exit(&spa_namespace_lock); 4548 return (SET_ERROR(ENOENT)); 4549 } 4550 4551 if (spa->spa_state == POOL_STATE_UNINITIALIZED) { 4552 zpool_load_policy_t policy; 4553 4554 zpool_get_load_policy(nvpolicy ? nvpolicy : spa->spa_config, 4555 &policy); 4556 if (policy.zlp_rewind & ZPOOL_DO_REWIND) 4557 state = SPA_LOAD_RECOVER; 4558 4559 spa_activate(spa, spa_mode_global); 4560 4561 if (state != SPA_LOAD_RECOVER) 4562 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 4563 spa->spa_config_source = SPA_CONFIG_SRC_CACHEFILE; 4564 4565 zfs_dbgmsg("spa_open_common: opening %s", pool); 4566 error = spa_load_best(spa, state, policy.zlp_txg, 4567 policy.zlp_rewind); 4568 4569 if (error == EBADF) { 4570 /* 4571 * If vdev_validate() returns failure (indicated by 4572 * EBADF), it indicates that one of the vdevs indicates 4573 * that the pool has been exported or destroyed. If 4574 * this is the case, the config cache is out of sync and 4575 * we should remove the pool from the namespace. 4576 */ 4577 spa_unload(spa); 4578 spa_deactivate(spa); 4579 spa_write_cachefile(spa, B_TRUE, B_TRUE); 4580 spa_remove(spa); 4581 if (locked) 4582 mutex_exit(&spa_namespace_lock); 4583 return (SET_ERROR(ENOENT)); 4584 } 4585 4586 if (error) { 4587 /* 4588 * We can't open the pool, but we still have useful 4589 * information: the state of each vdev after the 4590 * attempted vdev_open(). Return this to the user. 4591 */ 4592 if (config != NULL && spa->spa_config) { 4593 VERIFY(nvlist_dup(spa->spa_config, config, 4594 KM_SLEEP) == 0); 4595 VERIFY(nvlist_add_nvlist(*config, 4596 ZPOOL_CONFIG_LOAD_INFO, 4597 spa->spa_load_info) == 0); 4598 } 4599 spa_unload(spa); 4600 spa_deactivate(spa); 4601 spa->spa_last_open_failed = error; 4602 if (locked) 4603 mutex_exit(&spa_namespace_lock); 4604 *spapp = NULL; 4605 return (error); 4606 } 4607 } 4608 4609 spa_open_ref(spa, tag); 4610 4611 if (config != NULL) 4612 *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 4613 4614 /* 4615 * If we've recovered the pool, pass back any information we 4616 * gathered while doing the load. 4617 */ 4618 if (state == SPA_LOAD_RECOVER) { 4619 VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO, 4620 spa->spa_load_info) == 0); 4621 } 4622 4623 if (locked) { 4624 spa->spa_last_open_failed = 0; 4625 spa->spa_last_ubsync_txg = 0; 4626 spa->spa_load_txg = 0; 4627 mutex_exit(&spa_namespace_lock); 4628 } 4629 4630 *spapp = spa; 4631 4632 return (0); 4633 } 4634 4635 int 4636 spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy, 4637 nvlist_t **config) 4638 { 4639 return (spa_open_common(name, spapp, tag, policy, config)); 4640 } 4641 4642 int 4643 spa_open(const char *name, spa_t **spapp, void *tag) 4644 { 4645 return (spa_open_common(name, spapp, tag, NULL, NULL)); 4646 } 4647 4648 /* 4649 * Lookup the given spa_t, incrementing the inject count in the process, 4650 * preventing it from being exported or destroyed. 4651 */ 4652 spa_t * 4653 spa_inject_addref(char *name) 4654 { 4655 spa_t *spa; 4656 4657 mutex_enter(&spa_namespace_lock); 4658 if ((spa = spa_lookup(name)) == NULL) { 4659 mutex_exit(&spa_namespace_lock); 4660 return (NULL); 4661 } 4662 spa->spa_inject_ref++; 4663 mutex_exit(&spa_namespace_lock); 4664 4665 return (spa); 4666 } 4667 4668 void 4669 spa_inject_delref(spa_t *spa) 4670 { 4671 mutex_enter(&spa_namespace_lock); 4672 spa->spa_inject_ref--; 4673 mutex_exit(&spa_namespace_lock); 4674 } 4675 4676 /* 4677 * Add spares device information to the nvlist. 4678 */ 4679 static void 4680 spa_add_spares(spa_t *spa, nvlist_t *config) 4681 { 4682 nvlist_t **spares; 4683 uint_t i, nspares; 4684 nvlist_t *nvroot; 4685 uint64_t guid; 4686 vdev_stat_t *vs; 4687 uint_t vsc; 4688 uint64_t pool; 4689 4690 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 4691 4692 if (spa->spa_spares.sav_count == 0) 4693 return; 4694 4695 VERIFY(nvlist_lookup_nvlist(config, 4696 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 4697 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 4698 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 4699 if (nspares != 0) { 4700 VERIFY(nvlist_add_nvlist_array(nvroot, 4701 ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 4702 VERIFY(nvlist_lookup_nvlist_array(nvroot, 4703 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); 4704 4705 /* 4706 * Go through and find any spares which have since been 4707 * repurposed as an active spare. If this is the case, update 4708 * their status appropriately. 4709 */ 4710 for (i = 0; i < nspares; i++) { 4711 VERIFY(nvlist_lookup_uint64(spares[i], 4712 ZPOOL_CONFIG_GUID, &guid) == 0); 4713 if (spa_spare_exists(guid, &pool, NULL) && 4714 pool != 0ULL) { 4715 VERIFY(nvlist_lookup_uint64_array( 4716 spares[i], ZPOOL_CONFIG_VDEV_STATS, 4717 (uint64_t **)&vs, &vsc) == 0); 4718 vs->vs_state = VDEV_STATE_CANT_OPEN; 4719 vs->vs_aux = VDEV_AUX_SPARED; 4720 } 4721 } 4722 } 4723 } 4724 4725 /* 4726 * Add l2cache device information to the nvlist, including vdev stats. 4727 */ 4728 static void 4729 spa_add_l2cache(spa_t *spa, nvlist_t *config) 4730 { 4731 nvlist_t **l2cache; 4732 uint_t i, j, nl2cache; 4733 nvlist_t *nvroot; 4734 uint64_t guid; 4735 vdev_t *vd; 4736 vdev_stat_t *vs; 4737 uint_t vsc; 4738 4739 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 4740 4741 if (spa->spa_l2cache.sav_count == 0) 4742 return; 4743 4744 VERIFY(nvlist_lookup_nvlist(config, 4745 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 4746 VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 4747 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 4748 if (nl2cache != 0) { 4749 VERIFY(nvlist_add_nvlist_array(nvroot, 4750 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 4751 VERIFY(nvlist_lookup_nvlist_array(nvroot, 4752 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); 4753 4754 /* 4755 * Update level 2 cache device stats. 4756 */ 4757 4758 for (i = 0; i < nl2cache; i++) { 4759 VERIFY(nvlist_lookup_uint64(l2cache[i], 4760 ZPOOL_CONFIG_GUID, &guid) == 0); 4761 4762 vd = NULL; 4763 for (j = 0; j < spa->spa_l2cache.sav_count; j++) { 4764 if (guid == 4765 spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { 4766 vd = spa->spa_l2cache.sav_vdevs[j]; 4767 break; 4768 } 4769 } 4770 ASSERT(vd != NULL); 4771 4772 VERIFY(nvlist_lookup_uint64_array(l2cache[i], 4773 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc) 4774 == 0); 4775 vdev_get_stats(vd, vs); 4776 vdev_config_generate_stats(vd, l2cache[i]); 4777 4778 } 4779 } 4780 } 4781 4782 static void 4783 spa_add_feature_stats(spa_t *spa, nvlist_t *config) 4784 { 4785 nvlist_t *features; 4786 zap_cursor_t zc; 4787 zap_attribute_t za; 4788 4789 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); 4790 VERIFY(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP) == 0); 4791 4792 if (spa->spa_feat_for_read_obj != 0) { 4793 for (zap_cursor_init(&zc, spa->spa_meta_objset, 4794 spa->spa_feat_for_read_obj); 4795 zap_cursor_retrieve(&zc, &za) == 0; 4796 zap_cursor_advance(&zc)) { 4797 ASSERT(za.za_integer_length == sizeof (uint64_t) && 4798 za.za_num_integers == 1); 4799 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name, 4800 za.za_first_integer)); 4801 } 4802 zap_cursor_fini(&zc); 4803 } 4804 4805 if (spa->spa_feat_for_write_obj != 0) { 4806 for (zap_cursor_init(&zc, spa->spa_meta_objset, 4807 spa->spa_feat_for_write_obj); 4808 zap_cursor_retrieve(&zc, &za) == 0; 4809 zap_cursor_advance(&zc)) { 4810 ASSERT(za.za_integer_length == sizeof (uint64_t) && 4811 za.za_num_integers == 1); 4812 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name, 4813 za.za_first_integer)); 4814 } 4815 zap_cursor_fini(&zc); 4816 } 4817 4818 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS, 4819 features) == 0); 4820 nvlist_free(features); 4821 } 4822 4823 int 4824 spa_get_stats(const char *name, nvlist_t **config, 4825 char *altroot, size_t buflen) 4826 { 4827 int error; 4828 spa_t *spa; 4829 4830 *config = NULL; 4831 error = spa_open_common(name, &spa, FTAG, NULL, config); 4832 4833 if (spa != NULL) { 4834 /* 4835 * This still leaves a window of inconsistency where the spares 4836 * or l2cache devices could change and the config would be 4837 * self-inconsistent. 4838 */ 4839 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 4840 4841 if (*config != NULL) { 4842 uint64_t loadtimes[2]; 4843 4844 loadtimes[0] = spa->spa_loaded_ts.tv_sec; 4845 loadtimes[1] = spa->spa_loaded_ts.tv_nsec; 4846 VERIFY(nvlist_add_uint64_array(*config, 4847 ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0); 4848 4849 VERIFY(nvlist_add_uint64(*config, 4850 ZPOOL_CONFIG_ERRCOUNT, 4851 spa_get_errlog_size(spa)) == 0); 4852 4853 if (spa_suspended(spa)) { 4854 VERIFY(nvlist_add_uint64(*config, 4855 ZPOOL_CONFIG_SUSPENDED, 4856 spa->spa_failmode) == 0); 4857 VERIFY(nvlist_add_uint64(*config, 4858 ZPOOL_CONFIG_SUSPENDED_REASON, 4859 spa->spa_suspended) == 0); 4860 } 4861 4862 spa_add_spares(spa, *config); 4863 spa_add_l2cache(spa, *config); 4864 spa_add_feature_stats(spa, *config); 4865 } 4866 } 4867 4868 /* 4869 * We want to get the alternate root even for faulted pools, so we cheat 4870 * and call spa_lookup() directly. 4871 */ 4872 if (altroot) { 4873 if (spa == NULL) { 4874 mutex_enter(&spa_namespace_lock); 4875 spa = spa_lookup(name); 4876 if (spa) 4877 spa_altroot(spa, altroot, buflen); 4878 else 4879 altroot[0] = '\0'; 4880 spa = NULL; 4881 mutex_exit(&spa_namespace_lock); 4882 } else { 4883 spa_altroot(spa, altroot, buflen); 4884 } 4885 } 4886 4887 if (spa != NULL) { 4888 spa_config_exit(spa, SCL_CONFIG, FTAG); 4889 spa_close(spa, FTAG); 4890 } 4891 4892 return (error); 4893 } 4894 4895 /* 4896 * Validate that the auxiliary device array is well formed. We must have an 4897 * array of nvlists, each which describes a valid leaf vdev. If this is an 4898 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be 4899 * specified, as long as they are well-formed. 4900 */ 4901 static int 4902 spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, 4903 spa_aux_vdev_t *sav, const char *config, uint64_t version, 4904 vdev_labeltype_t label) 4905 { 4906 nvlist_t **dev; 4907 uint_t i, ndev; 4908 vdev_t *vd; 4909 int error; 4910 4911 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 4912 4913 /* 4914 * It's acceptable to have no devs specified. 4915 */ 4916 if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) 4917 return (0); 4918 4919 if (ndev == 0) 4920 return (SET_ERROR(EINVAL)); 4921 4922 /* 4923 * Make sure the pool is formatted with a version that supports this 4924 * device type. 4925 */ 4926 if (spa_version(spa) < version) 4927 return (SET_ERROR(ENOTSUP)); 4928 4929 /* 4930 * Set the pending device list so we correctly handle device in-use 4931 * checking. 4932 */ 4933 sav->sav_pending = dev; 4934 sav->sav_npending = ndev; 4935 4936 for (i = 0; i < ndev; i++) { 4937 if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, 4938 mode)) != 0) 4939 goto out; 4940 4941 if (!vd->vdev_ops->vdev_op_leaf) { 4942 vdev_free(vd); 4943 error = SET_ERROR(EINVAL); 4944 goto out; 4945 } 4946 4947 vd->vdev_top = vd; 4948 4949 if ((error = vdev_open(vd)) == 0 && 4950 (error = vdev_label_init(vd, crtxg, label)) == 0) { 4951 VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, 4952 vd->vdev_guid) == 0); 4953 } 4954 4955 vdev_free(vd); 4956 4957 if (error && 4958 (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) 4959 goto out; 4960 else 4961 error = 0; 4962 } 4963 4964 out: 4965 sav->sav_pending = NULL; 4966 sav->sav_npending = 0; 4967 return (error); 4968 } 4969 4970 static int 4971 spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) 4972 { 4973 int error; 4974 4975 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 4976 4977 if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, 4978 &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, 4979 VDEV_LABEL_SPARE)) != 0) { 4980 return (error); 4981 } 4982 4983 return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, 4984 &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, 4985 VDEV_LABEL_L2CACHE)); 4986 } 4987 4988 static void 4989 spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, 4990 const char *config) 4991 { 4992 int i; 4993 4994 if (sav->sav_config != NULL) { 4995 nvlist_t **olddevs; 4996 uint_t oldndevs; 4997 nvlist_t **newdevs; 4998 4999 /* 5000 * Generate new dev list by concatentating with the 5001 * current dev list. 5002 */ 5003 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, 5004 &olddevs, &oldndevs) == 0); 5005 5006 newdevs = kmem_alloc(sizeof (void *) * 5007 (ndevs + oldndevs), KM_SLEEP); 5008 for (i = 0; i < oldndevs; i++) 5009 VERIFY(nvlist_dup(olddevs[i], &newdevs[i], 5010 KM_SLEEP) == 0); 5011 for (i = 0; i < ndevs; i++) 5012 VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], 5013 KM_SLEEP) == 0); 5014 5015 VERIFY(nvlist_remove(sav->sav_config, config, 5016 DATA_TYPE_NVLIST_ARRAY) == 0); 5017 5018 VERIFY(nvlist_add_nvlist_array(sav->sav_config, 5019 config, newdevs, ndevs + oldndevs) == 0); 5020 for (i = 0; i < oldndevs + ndevs; i++) 5021 nvlist_free(newdevs[i]); 5022 kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); 5023 } else { 5024 /* 5025 * Generate a new dev list. 5026 */ 5027 VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, 5028 KM_SLEEP) == 0); 5029 VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, 5030 devs, ndevs) == 0); 5031 } 5032 } 5033 5034 /* 5035 * Stop and drop level 2 ARC devices 5036 */ 5037 void 5038 spa_l2cache_drop(spa_t *spa) 5039 { 5040 vdev_t *vd; 5041 int i; 5042 spa_aux_vdev_t *sav = &spa->spa_l2cache; 5043 5044 for (i = 0; i < sav->sav_count; i++) { 5045 uint64_t pool; 5046 5047 vd = sav->sav_vdevs[i]; 5048 ASSERT(vd != NULL); 5049 5050 if (spa_l2cache_exists(vd->vdev_guid, &pool) && 5051 pool != 0ULL && l2arc_vdev_present(vd)) 5052 l2arc_remove_vdev(vd); 5053 } 5054 } 5055 5056 /* 5057 * Verify encryption parameters for spa creation. If we are encrypting, we must 5058 * have the encryption feature flag enabled. 5059 */ 5060 static int 5061 spa_create_check_encryption_params(dsl_crypto_params_t *dcp, 5062 boolean_t has_encryption) 5063 { 5064 if (dcp->cp_crypt != ZIO_CRYPT_OFF && 5065 dcp->cp_crypt != ZIO_CRYPT_INHERIT && 5066 !has_encryption) 5067 return (SET_ERROR(ENOTSUP)); 5068 5069 return (dmu_objset_create_crypt_check(NULL, dcp, NULL)); 5070 } 5071 5072 /* 5073 * Pool Creation 5074 */ 5075 int 5076 spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, 5077 nvlist_t *zplprops, dsl_crypto_params_t *dcp) 5078 { 5079 spa_t *spa; 5080 char *altroot = NULL; 5081 vdev_t *rvd; 5082 dsl_pool_t *dp; 5083 dmu_tx_t *tx; 5084 int error = 0; 5085 uint64_t txg = TXG_INITIAL; 5086 nvlist_t **spares, **l2cache; 5087 uint_t nspares, nl2cache; 5088 uint64_t version, obj; 5089 boolean_t has_features; 5090 char *poolname; 5091 nvlist_t *nvl; 5092 boolean_t has_encryption; 5093 spa_feature_t feat; 5094 char *feat_name; 5095 5096 if (props == NULL || 5097 nvlist_lookup_string(props, 5098 zpool_prop_to_name(ZPOOL_PROP_TNAME), &poolname) != 0) 5099 poolname = (char *)pool; 5100 5101 /* 5102 * If this pool already exists, return failure. 5103 */ 5104 mutex_enter(&spa_namespace_lock); 5105 if (spa_lookup(poolname) != NULL) { 5106 mutex_exit(&spa_namespace_lock); 5107 return (SET_ERROR(EEXIST)); 5108 } 5109 5110 /* 5111 * Allocate a new spa_t structure. 5112 */ 5113 nvl = fnvlist_alloc(); 5114 fnvlist_add_string(nvl, ZPOOL_CONFIG_POOL_NAME, pool); 5115 (void) nvlist_lookup_string(props, 5116 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 5117 spa = spa_add(poolname, nvl, altroot); 5118 fnvlist_free(nvl); 5119 spa_activate(spa, spa_mode_global); 5120 5121 if (props && (error = spa_prop_validate(spa, props))) { 5122 spa_deactivate(spa); 5123 spa_remove(spa); 5124 mutex_exit(&spa_namespace_lock); 5125 return (error); 5126 } 5127 5128 /* 5129 * Temporary pool names should never be written to disk. 5130 */ 5131 if (poolname != pool) 5132 spa->spa_import_flags |= ZFS_IMPORT_TEMP_NAME; 5133 5134 has_features = B_FALSE; 5135 has_encryption = B_FALSE; 5136 for (nvpair_t *elem = nvlist_next_nvpair(props, NULL); 5137 elem != NULL; elem = nvlist_next_nvpair(props, elem)) { 5138 if (zpool_prop_feature(nvpair_name(elem))) { 5139 has_features = B_TRUE; 5140 feat_name = strchr(nvpair_name(elem), '@') + 1; 5141 VERIFY0(zfeature_lookup_name(feat_name, &feat)); 5142 if (feat == SPA_FEATURE_ENCRYPTION) 5143 has_encryption = B_TRUE; 5144 } 5145 } 5146 5147 /* verify encryption params, if they were provided */ 5148 if (dcp != NULL) { 5149 error = spa_create_check_encryption_params(dcp, has_encryption); 5150 if (error != 0) { 5151 spa_deactivate(spa); 5152 spa_remove(spa); 5153 mutex_exit(&spa_namespace_lock); 5154 return (error); 5155 } 5156 } 5157 5158 if (has_features || nvlist_lookup_uint64(props, 5159 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) { 5160 version = SPA_VERSION; 5161 } 5162 ASSERT(SPA_VERSION_IS_SUPPORTED(version)); 5163 5164 spa->spa_first_txg = txg; 5165 spa->spa_uberblock.ub_txg = txg - 1; 5166 spa->spa_uberblock.ub_version = version; 5167 spa->spa_ubsync = spa->spa_uberblock; 5168 spa->spa_load_state = SPA_LOAD_CREATE; 5169 spa->spa_removing_phys.sr_state = DSS_NONE; 5170 spa->spa_removing_phys.sr_removing_vdev = -1; 5171 spa->spa_removing_phys.sr_prev_indirect_vdev = -1; 5172 spa->spa_indirect_vdevs_loaded = B_TRUE; 5173 5174 /* 5175 * Create "The Godfather" zio to hold all async IOs 5176 */ 5177 spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *), 5178 KM_SLEEP); 5179 for (int i = 0; i < max_ncpus; i++) { 5180 spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, 5181 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | 5182 ZIO_FLAG_GODFATHER); 5183 } 5184 5185 /* 5186 * Create the root vdev. 5187 */ 5188 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 5189 5190 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); 5191 5192 ASSERT(error != 0 || rvd != NULL); 5193 ASSERT(error != 0 || spa->spa_root_vdev == rvd); 5194 5195 if (error == 0 && !zfs_allocatable_devs(nvroot)) 5196 error = SET_ERROR(EINVAL); 5197 5198 if (error == 0 && 5199 (error = vdev_create(rvd, txg, B_FALSE)) == 0 && 5200 (error = spa_validate_aux(spa, nvroot, txg, 5201 VDEV_ALLOC_ADD)) == 0) { 5202 /* 5203 * instantiate the metaslab groups (this will dirty the vdevs) 5204 * we can no longer error exit past this point 5205 */ 5206 for (int c = 0; error == 0 && c < rvd->vdev_children; c++) { 5207 vdev_t *vd = rvd->vdev_child[c]; 5208 5209 vdev_metaslab_set_size(vd); 5210 vdev_expand(vd, txg); 5211 } 5212 } 5213 5214 spa_config_exit(spa, SCL_ALL, FTAG); 5215 5216 if (error != 0) { 5217 spa_unload(spa); 5218 spa_deactivate(spa); 5219 spa_remove(spa); 5220 mutex_exit(&spa_namespace_lock); 5221 return (error); 5222 } 5223 5224 /* 5225 * Get the list of spares, if specified. 5226 */ 5227 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 5228 &spares, &nspares) == 0) { 5229 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, 5230 KM_SLEEP) == 0); 5231 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 5232 ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 5233 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 5234 spa_load_spares(spa); 5235 spa_config_exit(spa, SCL_ALL, FTAG); 5236 spa->spa_spares.sav_sync = B_TRUE; 5237 } 5238 5239 /* 5240 * Get the list of level 2 cache devices, if specified. 5241 */ 5242 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 5243 &l2cache, &nl2cache) == 0) { 5244 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 5245 NV_UNIQUE_NAME, KM_SLEEP) == 0); 5246 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 5247 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 5248 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 5249 spa_load_l2cache(spa); 5250 spa_config_exit(spa, SCL_ALL, FTAG); 5251 spa->spa_l2cache.sav_sync = B_TRUE; 5252 } 5253 5254 spa->spa_is_initializing = B_TRUE; 5255 spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, dcp, txg); 5256 spa->spa_is_initializing = B_FALSE; 5257 5258 /* 5259 * Create DDTs (dedup tables). 5260 */ 5261 ddt_create(spa); 5262 5263 spa_update_dspace(spa); 5264 5265 tx = dmu_tx_create_assigned(dp, txg); 5266 5267 /* 5268 * Create the pool config object. 5269 */ 5270 spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, 5271 DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, 5272 DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); 5273 5274 if (zap_add(spa->spa_meta_objset, 5275 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, 5276 sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { 5277 cmn_err(CE_PANIC, "failed to add pool config"); 5278 } 5279 5280 if (zap_add(spa->spa_meta_objset, 5281 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION, 5282 sizeof (uint64_t), 1, &version, tx) != 0) { 5283 cmn_err(CE_PANIC, "failed to add pool version"); 5284 } 5285 5286 /* Newly created pools with the right version are always deflated. */ 5287 if (version >= SPA_VERSION_RAIDZ_DEFLATE) { 5288 spa->spa_deflate = TRUE; 5289 if (zap_add(spa->spa_meta_objset, 5290 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 5291 sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { 5292 cmn_err(CE_PANIC, "failed to add deflate"); 5293 } 5294 } 5295 5296 /* 5297 * Create the deferred-free bpobj. Turn off compression 5298 * because sync-to-convergence takes longer if the blocksize 5299 * keeps changing. 5300 */ 5301 obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx); 5302 dmu_object_set_compress(spa->spa_meta_objset, obj, 5303 ZIO_COMPRESS_OFF, tx); 5304 if (zap_add(spa->spa_meta_objset, 5305 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ, 5306 sizeof (uint64_t), 1, &obj, tx) != 0) { 5307 cmn_err(CE_PANIC, "failed to add bpobj"); 5308 } 5309 VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj, 5310 spa->spa_meta_objset, obj)); 5311 5312 /* 5313 * Create the pool's history object. 5314 */ 5315 if (version >= SPA_VERSION_ZPOOL_HISTORY) 5316 spa_history_create_obj(spa, tx); 5317 5318 /* 5319 * Generate some random noise for salted checksums to operate on. 5320 */ 5321 (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes, 5322 sizeof (spa->spa_cksum_salt.zcs_bytes)); 5323 5324 /* 5325 * Set pool properties. 5326 */ 5327 spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); 5328 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); 5329 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); 5330 spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND); 5331 spa->spa_multihost = zpool_prop_default_numeric(ZPOOL_PROP_MULTIHOST); 5332 spa->spa_autotrim = zpool_prop_default_numeric(ZPOOL_PROP_AUTOTRIM); 5333 5334 if (props != NULL) { 5335 spa_configfile_set(spa, props, B_FALSE); 5336 spa_sync_props(props, tx); 5337 } 5338 5339 dmu_tx_commit(tx); 5340 5341 spa->spa_sync_on = B_TRUE; 5342 txg_sync_start(spa->spa_dsl_pool); 5343 mmp_thread_start(spa); 5344 5345 /* 5346 * We explicitly wait for the first transaction to complete so that our 5347 * bean counters are appropriately updated. 5348 */ 5349 txg_wait_synced(spa->spa_dsl_pool, txg); 5350 5351 spa_spawn_aux_threads(spa); 5352 5353 spa_write_cachefile(spa, B_FALSE, B_TRUE); 5354 spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_CREATE); 5355 5356 spa_history_log_version(spa, "create"); 5357 5358 /* 5359 * Don't count references from objsets that are already closed 5360 * and are making their way through the eviction process. 5361 */ 5362 spa_evicting_os_wait(spa); 5363 spa->spa_minref = zfs_refcount_count(&spa->spa_refcount); 5364 spa->spa_load_state = SPA_LOAD_NONE; 5365 5366 mutex_exit(&spa_namespace_lock); 5367 5368 return (0); 5369 } 5370 5371 #ifdef _KERNEL 5372 /* 5373 * Get the root pool information from the root disk, then import the root pool 5374 * during the system boot up time. 5375 */ 5376 static nvlist_t * 5377 spa_generate_rootconf(const char *devpath, const char *devid, uint64_t *guid, 5378 uint64_t pool_guid) 5379 { 5380 nvlist_t *config; 5381 nvlist_t *nvtop, *nvroot; 5382 uint64_t pgid; 5383 5384 if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0) 5385 return (NULL); 5386 5387 /* 5388 * Add this top-level vdev to the child array. 5389 */ 5390 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 5391 &nvtop) == 0); 5392 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 5393 &pgid) == 0); 5394 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0); 5395 5396 if (pool_guid != 0 && pool_guid != pgid) { 5397 /* 5398 * The boot loader provided a pool GUID, but it does not match 5399 * the one we found in the label. Return failure so that we 5400 * can fall back to the full device scan. 5401 */ 5402 zfs_dbgmsg("spa_generate_rootconf: loader pool guid %llu != " 5403 "label pool guid %llu", (u_longlong_t)pool_guid, 5404 (u_longlong_t)pgid); 5405 nvlist_free(config); 5406 return (NULL); 5407 } 5408 5409 /* 5410 * Put this pool's top-level vdevs into a root vdev. 5411 */ 5412 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 5413 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, 5414 VDEV_TYPE_ROOT) == 0); 5415 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0); 5416 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0); 5417 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 5418 &nvtop, 1) == 0); 5419 5420 /* 5421 * Replace the existing vdev_tree with the new root vdev in 5422 * this pool's configuration (remove the old, add the new). 5423 */ 5424 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 5425 nvlist_free(nvroot); 5426 return (config); 5427 } 5428 5429 /* 5430 * Walk the vdev tree and see if we can find a device with "better" 5431 * configuration. A configuration is "better" if the label on that 5432 * device has a more recent txg. 5433 */ 5434 static void 5435 spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg) 5436 { 5437 for (int c = 0; c < vd->vdev_children; c++) 5438 spa_alt_rootvdev(vd->vdev_child[c], avd, txg); 5439 5440 if (vd->vdev_ops->vdev_op_leaf) { 5441 nvlist_t *label; 5442 uint64_t label_txg; 5443 5444 if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid, 5445 &label) != 0) 5446 return; 5447 5448 VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG, 5449 &label_txg) == 0); 5450 5451 /* 5452 * Do we have a better boot device? 5453 */ 5454 if (label_txg > *txg) { 5455 *txg = label_txg; 5456 *avd = vd; 5457 } 5458 nvlist_free(label); 5459 } 5460 } 5461 5462 /* 5463 * Import a root pool. 5464 * 5465 * For x86. devpath_list will consist of devid and/or physpath name of 5466 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a"). 5467 * The GRUB "findroot" command will return the vdev we should boot. 5468 * 5469 * For Sparc, devpath_list consists the physpath name of the booting device 5470 * no matter the rootpool is a single device pool or a mirrored pool. 5471 * e.g. 5472 * "/pci@1f,0/ide@d/disk@0,0:a" 5473 */ 5474 int 5475 spa_import_rootpool(char *devpath, char *devid, uint64_t pool_guid, 5476 uint64_t vdev_guid) 5477 { 5478 spa_t *spa; 5479 vdev_t *rvd, *bvd, *avd = NULL; 5480 nvlist_t *config, *nvtop; 5481 uint64_t guid, txg; 5482 char *pname; 5483 int error; 5484 const char *altdevpath = NULL; 5485 const char *rdpath = NULL; 5486 5487 if ((rdpath = vdev_disk_preroot_force_path()) != NULL) { 5488 /* 5489 * We expect to import a single-vdev pool from a specific 5490 * device such as a ramdisk device. We don't care what the 5491 * pool label says. 5492 */ 5493 config = spa_generate_rootconf(rdpath, NULL, &guid, 0); 5494 if (config != NULL) { 5495 goto configok; 5496 } 5497 cmn_err(CE_NOTE, "Cannot use root disk device '%s'", rdpath); 5498 return (SET_ERROR(EIO)); 5499 } 5500 5501 /* 5502 * Read the label from the boot device and generate a configuration. 5503 */ 5504 config = spa_generate_rootconf(devpath, devid, &guid, pool_guid); 5505 #if defined(_OBP) && defined(_KERNEL) 5506 if (config == NULL) { 5507 if (strstr(devpath, "/iscsi/ssd") != NULL) { 5508 /* iscsi boot */ 5509 get_iscsi_bootpath_phy(devpath); 5510 config = spa_generate_rootconf(devpath, devid, &guid, 5511 pool_guid); 5512 } 5513 } 5514 #endif 5515 5516 /* 5517 * We were unable to import the pool using the /devices path or devid 5518 * provided by the boot loader. This may be the case if the boot 5519 * device has been connected to a different location in the system, or 5520 * if a new boot environment has changed the driver used to access the 5521 * boot device. 5522 * 5523 * Attempt an exhaustive scan of all visible block devices to see if we 5524 * can locate an alternative /devices path with a label that matches 5525 * the expected pool and vdev GUID. 5526 */ 5527 if (config == NULL && (altdevpath = 5528 vdev_disk_preroot_lookup(pool_guid, vdev_guid)) != NULL) { 5529 cmn_err(CE_NOTE, "Original /devices path (%s) not available; " 5530 "ZFS is trying an alternate path (%s)", devpath, 5531 altdevpath); 5532 config = spa_generate_rootconf(altdevpath, NULL, &guid, 5533 pool_guid); 5534 } 5535 5536 if (config == NULL) { 5537 cmn_err(CE_NOTE, "Cannot read the pool label from '%s'", 5538 devpath); 5539 return (SET_ERROR(EIO)); 5540 } 5541 5542 configok: 5543 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 5544 &pname) == 0); 5545 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0); 5546 5547 mutex_enter(&spa_namespace_lock); 5548 if ((spa = spa_lookup(pname)) != NULL) { 5549 /* 5550 * Remove the existing root pool from the namespace so that we 5551 * can replace it with the correct config we just read in. 5552 */ 5553 spa_remove(spa); 5554 } 5555 5556 spa = spa_add(pname, config, NULL); 5557 spa->spa_is_root = B_TRUE; 5558 spa->spa_import_flags = ZFS_IMPORT_VERBATIM; 5559 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 5560 &spa->spa_ubsync.ub_version) != 0) 5561 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL; 5562 5563 /* 5564 * Build up a vdev tree based on the boot device's label config. 5565 */ 5566 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 5567 &nvtop) == 0); 5568 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 5569 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0, 5570 VDEV_ALLOC_ROOTPOOL); 5571 spa_config_exit(spa, SCL_ALL, FTAG); 5572 if (error) { 5573 mutex_exit(&spa_namespace_lock); 5574 nvlist_free(config); 5575 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'", 5576 pname); 5577 return (error); 5578 } 5579 5580 /* 5581 * Get the boot vdev. 5582 */ 5583 if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) { 5584 cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu", 5585 (u_longlong_t)guid); 5586 error = SET_ERROR(ENOENT); 5587 goto out; 5588 } 5589 5590 /* 5591 * Determine if there is a better boot device. 5592 */ 5593 avd = bvd; 5594 spa_alt_rootvdev(rvd, &avd, &txg); 5595 if (avd != bvd) { 5596 cmn_err(CE_NOTE, "The boot device is 'degraded'. Please " 5597 "try booting from '%s'", avd->vdev_path); 5598 error = SET_ERROR(EINVAL); 5599 goto out; 5600 } 5601 5602 /* 5603 * If the boot device is part of a spare vdev then ensure that 5604 * we're booting off the active spare. 5605 */ 5606 if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops && 5607 !bvd->vdev_isspare) { 5608 cmn_err(CE_NOTE, "The boot device is currently spared. Please " 5609 "try booting from '%s'", 5610 bvd->vdev_parent-> 5611 vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path); 5612 error = SET_ERROR(EINVAL); 5613 goto out; 5614 } 5615 5616 /* 5617 * The root disk may have been expanded while the system was offline. 5618 * Kick off an async task to check for and handle expansion. 5619 */ 5620 spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); 5621 5622 error = 0; 5623 out: 5624 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 5625 vdev_free(rvd); 5626 spa_config_exit(spa, SCL_ALL, FTAG); 5627 mutex_exit(&spa_namespace_lock); 5628 5629 nvlist_free(config); 5630 return (error); 5631 } 5632 5633 #endif 5634 5635 /* 5636 * Import a non-root pool into the system. 5637 */ 5638 int 5639 spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags) 5640 { 5641 spa_t *spa; 5642 char *altroot = NULL; 5643 spa_load_state_t state = SPA_LOAD_IMPORT; 5644 zpool_load_policy_t policy; 5645 uint64_t mode = spa_mode_global; 5646 uint64_t readonly = B_FALSE; 5647 int error; 5648 nvlist_t *nvroot; 5649 nvlist_t **spares, **l2cache; 5650 uint_t nspares, nl2cache; 5651 5652 /* 5653 * If a pool with this name exists, return failure. 5654 */ 5655 mutex_enter(&spa_namespace_lock); 5656 if (spa_lookup(pool) != NULL) { 5657 mutex_exit(&spa_namespace_lock); 5658 return (SET_ERROR(EEXIST)); 5659 } 5660 5661 /* 5662 * Create and initialize the spa structure. 5663 */ 5664 (void) nvlist_lookup_string(props, 5665 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 5666 (void) nvlist_lookup_uint64(props, 5667 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly); 5668 if (readonly) 5669 mode = FREAD; 5670 spa = spa_add(pool, config, altroot); 5671 spa->spa_import_flags = flags; 5672 5673 /* 5674 * Verbatim import - Take a pool and insert it into the namespace 5675 * as if it had been loaded at boot. 5676 */ 5677 if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) { 5678 if (props != NULL) 5679 spa_configfile_set(spa, props, B_FALSE); 5680 5681 spa_write_cachefile(spa, B_FALSE, B_TRUE); 5682 spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_IMPORT); 5683 zfs_dbgmsg("spa_import: verbatim import of %s", pool); 5684 mutex_exit(&spa_namespace_lock); 5685 return (0); 5686 } 5687 5688 spa_activate(spa, mode); 5689 5690 /* 5691 * Don't start async tasks until we know everything is healthy. 5692 */ 5693 spa_async_suspend(spa); 5694 5695 zpool_get_load_policy(config, &policy); 5696 if (policy.zlp_rewind & ZPOOL_DO_REWIND) 5697 state = SPA_LOAD_RECOVER; 5698 5699 spa->spa_config_source = SPA_CONFIG_SRC_TRYIMPORT; 5700 5701 if (state != SPA_LOAD_RECOVER) { 5702 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; 5703 zfs_dbgmsg("spa_import: importing %s", pool); 5704 } else { 5705 zfs_dbgmsg("spa_import: importing %s, max_txg=%lld " 5706 "(RECOVERY MODE)", pool, (longlong_t)policy.zlp_txg); 5707 } 5708 error = spa_load_best(spa, state, policy.zlp_txg, policy.zlp_rewind); 5709 5710 /* 5711 * Propagate anything learned while loading the pool and pass it 5712 * back to caller (i.e. rewind info, missing devices, etc). 5713 */ 5714 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, 5715 spa->spa_load_info) == 0); 5716 5717 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 5718 /* 5719 * Toss any existing sparelist, as it doesn't have any validity 5720 * anymore, and conflicts with spa_has_spare(). 5721 */ 5722 if (spa->spa_spares.sav_config) { 5723 nvlist_free(spa->spa_spares.sav_config); 5724 spa->spa_spares.sav_config = NULL; 5725 spa_load_spares(spa); 5726 } 5727 if (spa->spa_l2cache.sav_config) { 5728 nvlist_free(spa->spa_l2cache.sav_config); 5729 spa->spa_l2cache.sav_config = NULL; 5730 spa_load_l2cache(spa); 5731 } 5732 5733 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 5734 &nvroot) == 0); 5735 if (error == 0) 5736 error = spa_validate_aux(spa, nvroot, -1ULL, 5737 VDEV_ALLOC_SPARE); 5738 if (error == 0) 5739 error = spa_validate_aux(spa, nvroot, -1ULL, 5740 VDEV_ALLOC_L2CACHE); 5741 spa_config_exit(spa, SCL_ALL, FTAG); 5742 5743 if (props != NULL) 5744 spa_configfile_set(spa, props, B_FALSE); 5745 5746 if (error != 0 || (props && spa_writeable(spa) && 5747 (error = spa_prop_set(spa, props)))) { 5748 spa_unload(spa); 5749 spa_deactivate(spa); 5750 spa_remove(spa); 5751 mutex_exit(&spa_namespace_lock); 5752 return (error); 5753 } 5754 5755 spa_async_resume(spa); 5756 5757 /* 5758 * Override any spares and level 2 cache devices as specified by 5759 * the user, as these may have correct device names/devids, etc. 5760 */ 5761 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 5762 &spares, &nspares) == 0) { 5763 if (spa->spa_spares.sav_config) 5764 VERIFY(nvlist_remove(spa->spa_spares.sav_config, 5765 ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); 5766 else 5767 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, 5768 NV_UNIQUE_NAME, KM_SLEEP) == 0); 5769 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, 5770 ZPOOL_CONFIG_SPARES, spares, nspares) == 0); 5771 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 5772 spa_load_spares(spa); 5773 spa_config_exit(spa, SCL_ALL, FTAG); 5774 spa->spa_spares.sav_sync = B_TRUE; 5775 } 5776 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 5777 &l2cache, &nl2cache) == 0) { 5778 if (spa->spa_l2cache.sav_config) 5779 VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, 5780 ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); 5781 else 5782 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, 5783 NV_UNIQUE_NAME, KM_SLEEP) == 0); 5784 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, 5785 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); 5786 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 5787 spa_load_l2cache(spa); 5788 spa_config_exit(spa, SCL_ALL, FTAG); 5789 spa->spa_l2cache.sav_sync = B_TRUE; 5790 } 5791 5792 /* 5793 * Check for any removed devices. 5794 */ 5795 if (spa->spa_autoreplace) { 5796 spa_aux_check_removed(&spa->spa_spares); 5797 spa_aux_check_removed(&spa->spa_l2cache); 5798 } 5799 5800 if (spa_writeable(spa)) { 5801 /* 5802 * Update the config cache to include the newly-imported pool. 5803 */ 5804 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 5805 } 5806 5807 /* 5808 * It's possible that the pool was expanded while it was exported. 5809 * We kick off an async task to handle this for us. 5810 */ 5811 spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); 5812 5813 spa_history_log_version(spa, "import"); 5814 5815 spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_IMPORT); 5816 5817 mutex_exit(&spa_namespace_lock); 5818 5819 return (0); 5820 } 5821 5822 nvlist_t * 5823 spa_tryimport(nvlist_t *tryconfig) 5824 { 5825 nvlist_t *config = NULL; 5826 char *poolname, *cachefile; 5827 spa_t *spa; 5828 uint64_t state; 5829 int error; 5830 zpool_load_policy_t policy; 5831 5832 if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) 5833 return (NULL); 5834 5835 if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) 5836 return (NULL); 5837 5838 /* 5839 * Create and initialize the spa structure. 5840 */ 5841 mutex_enter(&spa_namespace_lock); 5842 spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL); 5843 spa_activate(spa, FREAD); 5844 5845 /* 5846 * Rewind pool if a max txg was provided. 5847 */ 5848 zpool_get_load_policy(spa->spa_config, &policy); 5849 if (policy.zlp_txg != UINT64_MAX) { 5850 spa->spa_load_max_txg = policy.zlp_txg; 5851 spa->spa_extreme_rewind = B_TRUE; 5852 zfs_dbgmsg("spa_tryimport: importing %s, max_txg=%lld", 5853 poolname, (longlong_t)policy.zlp_txg); 5854 } else { 5855 zfs_dbgmsg("spa_tryimport: importing %s", poolname); 5856 } 5857 5858 if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_CACHEFILE, &cachefile) 5859 == 0) { 5860 zfs_dbgmsg("spa_tryimport: using cachefile '%s'", cachefile); 5861 spa->spa_config_source = SPA_CONFIG_SRC_CACHEFILE; 5862 } else { 5863 spa->spa_config_source = SPA_CONFIG_SRC_SCAN; 5864 } 5865 5866 error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING); 5867 5868 /* 5869 * If 'tryconfig' was at least parsable, return the current config. 5870 */ 5871 if (spa->spa_root_vdev != NULL) { 5872 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); 5873 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 5874 poolname) == 0); 5875 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 5876 state) == 0); 5877 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, 5878 spa->spa_uberblock.ub_timestamp) == 0); 5879 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, 5880 spa->spa_load_info) == 0); 5881 5882 /* 5883 * If the bootfs property exists on this pool then we 5884 * copy it out so that external consumers can tell which 5885 * pools are bootable. 5886 */ 5887 if ((!error || error == EEXIST) && spa->spa_bootfs) { 5888 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 5889 5890 /* 5891 * We have to play games with the name since the 5892 * pool was opened as TRYIMPORT_NAME. 5893 */ 5894 if (dsl_dsobj_to_dsname(spa_name(spa), 5895 spa->spa_bootfs, tmpname) == 0) { 5896 char *cp; 5897 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 5898 5899 cp = strchr(tmpname, '/'); 5900 if (cp == NULL) { 5901 (void) strlcpy(dsname, tmpname, 5902 MAXPATHLEN); 5903 } else { 5904 (void) snprintf(dsname, MAXPATHLEN, 5905 "%s/%s", poolname, ++cp); 5906 } 5907 VERIFY(nvlist_add_string(config, 5908 ZPOOL_CONFIG_BOOTFS, dsname) == 0); 5909 kmem_free(dsname, MAXPATHLEN); 5910 } 5911 kmem_free(tmpname, MAXPATHLEN); 5912 } 5913 5914 /* 5915 * Add the list of hot spares and level 2 cache devices. 5916 */ 5917 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 5918 spa_add_spares(spa, config); 5919 spa_add_l2cache(spa, config); 5920 spa_config_exit(spa, SCL_CONFIG, FTAG); 5921 } 5922 5923 spa_unload(spa); 5924 spa_deactivate(spa); 5925 spa_remove(spa); 5926 mutex_exit(&spa_namespace_lock); 5927 5928 return (config); 5929 } 5930 5931 /* 5932 * Pool export/destroy 5933 * 5934 * The act of destroying or exporting a pool is very simple. We make sure there 5935 * is no more pending I/O and any references to the pool are gone. Then, we 5936 * update the pool state and sync all the labels to disk, removing the 5937 * configuration from the cache afterwards. If the 'hardforce' flag is set, then 5938 * we don't sync the labels or remove the configuration cache. 5939 */ 5940 static int 5941 spa_export_common(char *pool, int new_state, nvlist_t **oldconfig, 5942 boolean_t force, boolean_t hardforce) 5943 { 5944 spa_t *spa; 5945 5946 if (oldconfig) 5947 *oldconfig = NULL; 5948 5949 if (!(spa_mode_global & FWRITE)) 5950 return (SET_ERROR(EROFS)); 5951 5952 mutex_enter(&spa_namespace_lock); 5953 if ((spa = spa_lookup(pool)) == NULL) { 5954 mutex_exit(&spa_namespace_lock); 5955 return (SET_ERROR(ENOENT)); 5956 } 5957 5958 /* 5959 * Put a hold on the pool, drop the namespace lock, stop async tasks, 5960 * reacquire the namespace lock, and see if we can export. 5961 */ 5962 spa_open_ref(spa, FTAG); 5963 mutex_exit(&spa_namespace_lock); 5964 spa_async_suspend(spa); 5965 mutex_enter(&spa_namespace_lock); 5966 spa_close(spa, FTAG); 5967 5968 /* 5969 * The pool will be in core if it's openable, 5970 * in which case we can modify its state. 5971 */ 5972 if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { 5973 5974 /* 5975 * Objsets may be open only because they're dirty, so we 5976 * have to force it to sync before checking spa_refcnt. 5977 */ 5978 txg_wait_synced(spa->spa_dsl_pool, 0); 5979 spa_evicting_os_wait(spa); 5980 5981 /* 5982 * A pool cannot be exported or destroyed if there are active 5983 * references. If we are resetting a pool, allow references by 5984 * fault injection handlers. 5985 */ 5986 if (!spa_refcount_zero(spa) || 5987 (spa->spa_inject_ref != 0 && 5988 new_state != POOL_STATE_UNINITIALIZED)) { 5989 spa_async_resume(spa); 5990 mutex_exit(&spa_namespace_lock); 5991 return (SET_ERROR(EBUSY)); 5992 } 5993 5994 /* 5995 * A pool cannot be exported if it has an active shared spare. 5996 * This is to prevent other pools stealing the active spare 5997 * from an exported pool. At user's own will, such pool can 5998 * be forcedly exported. 5999 */ 6000 if (!force && new_state == POOL_STATE_EXPORTED && 6001 spa_has_active_shared_spare(spa)) { 6002 spa_async_resume(spa); 6003 mutex_exit(&spa_namespace_lock); 6004 return (SET_ERROR(EXDEV)); 6005 } 6006 6007 /* 6008 * We're about to export or destroy this pool. Make sure 6009 * we stop all initialization and trim activity here before 6010 * we set the spa_final_txg. This will ensure that all 6011 * dirty data resulting from the initialization is 6012 * committed to disk before we unload the pool. 6013 */ 6014 if (spa->spa_root_vdev != NULL) { 6015 vdev_t *rvd = spa->spa_root_vdev; 6016 vdev_initialize_stop_all(rvd, VDEV_INITIALIZE_ACTIVE); 6017 vdev_trim_stop_all(rvd, VDEV_TRIM_ACTIVE); 6018 vdev_autotrim_stop_all(spa); 6019 } 6020 6021 /* 6022 * We want this to be reflected on every label, 6023 * so mark them all dirty. spa_unload() will do the 6024 * final sync that pushes these changes out. 6025 */ 6026 if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { 6027 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 6028 spa->spa_state = new_state; 6029 spa->spa_final_txg = spa_last_synced_txg(spa) + 6030 TXG_DEFER_SIZE + 1; 6031 vdev_config_dirty(spa->spa_root_vdev); 6032 spa_config_exit(spa, SCL_ALL, FTAG); 6033 } 6034 } 6035 6036 spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_DESTROY); 6037 6038 if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 6039 spa_unload(spa); 6040 spa_deactivate(spa); 6041 } 6042 6043 if (oldconfig && spa->spa_config) 6044 VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); 6045 6046 if (new_state != POOL_STATE_UNINITIALIZED) { 6047 if (!hardforce) 6048 spa_write_cachefile(spa, B_TRUE, B_TRUE); 6049 spa_remove(spa); 6050 } 6051 mutex_exit(&spa_namespace_lock); 6052 6053 return (0); 6054 } 6055 6056 /* 6057 * Destroy a storage pool. 6058 */ 6059 int 6060 spa_destroy(char *pool) 6061 { 6062 return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, 6063 B_FALSE, B_FALSE)); 6064 } 6065 6066 /* 6067 * Export a storage pool. 6068 */ 6069 int 6070 spa_export(char *pool, nvlist_t **oldconfig, boolean_t force, 6071 boolean_t hardforce) 6072 { 6073 return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, 6074 force, hardforce)); 6075 } 6076 6077 /* 6078 * Similar to spa_export(), this unloads the spa_t without actually removing it 6079 * from the namespace in any way. 6080 */ 6081 int 6082 spa_reset(char *pool) 6083 { 6084 return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, 6085 B_FALSE, B_FALSE)); 6086 } 6087 6088 /* 6089 * ========================================================================== 6090 * Device manipulation 6091 * ========================================================================== 6092 */ 6093 6094 /* 6095 * Add a device to a storage pool. 6096 */ 6097 int 6098 spa_vdev_add(spa_t *spa, nvlist_t *nvroot) 6099 { 6100 uint64_t txg; 6101 int error; 6102 vdev_t *rvd = spa->spa_root_vdev; 6103 vdev_t *vd, *tvd; 6104 nvlist_t **spares, **l2cache; 6105 uint_t nspares, nl2cache; 6106 6107 ASSERT(spa_writeable(spa)); 6108 6109 txg = spa_vdev_enter(spa); 6110 6111 if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, 6112 VDEV_ALLOC_ADD)) != 0) 6113 return (spa_vdev_exit(spa, NULL, txg, error)); 6114 6115 spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */ 6116 6117 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, 6118 &nspares) != 0) 6119 nspares = 0; 6120 6121 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, 6122 &nl2cache) != 0) 6123 nl2cache = 0; 6124 6125 if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) 6126 return (spa_vdev_exit(spa, vd, txg, EINVAL)); 6127 6128 if (vd->vdev_children != 0 && 6129 (error = vdev_create(vd, txg, B_FALSE)) != 0) 6130 return (spa_vdev_exit(spa, vd, txg, error)); 6131 6132 /* 6133 * We must validate the spares and l2cache devices after checking the 6134 * children. Otherwise, vdev_inuse() will blindly overwrite the spare. 6135 */ 6136 if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) 6137 return (spa_vdev_exit(spa, vd, txg, error)); 6138 6139 /* 6140 * If we are in the middle of a device removal, we can only add 6141 * devices which match the existing devices in the pool. 6142 * If we are in the middle of a removal, or have some indirect 6143 * vdevs, we can not add raidz toplevels. 6144 */ 6145 if (spa->spa_vdev_removal != NULL || 6146 spa->spa_removing_phys.sr_prev_indirect_vdev != -1) { 6147 for (int c = 0; c < vd->vdev_children; c++) { 6148 tvd = vd->vdev_child[c]; 6149 if (spa->spa_vdev_removal != NULL && 6150 tvd->vdev_ashift != spa->spa_max_ashift) { 6151 return (spa_vdev_exit(spa, vd, txg, EINVAL)); 6152 } 6153 /* Fail if top level vdev is raidz */ 6154 if (tvd->vdev_ops == &vdev_raidz_ops) { 6155 return (spa_vdev_exit(spa, vd, txg, EINVAL)); 6156 } 6157 /* 6158 * Need the top level mirror to be 6159 * a mirror of leaf vdevs only 6160 */ 6161 if (tvd->vdev_ops == &vdev_mirror_ops) { 6162 for (uint64_t cid = 0; 6163 cid < tvd->vdev_children; cid++) { 6164 vdev_t *cvd = tvd->vdev_child[cid]; 6165 if (!cvd->vdev_ops->vdev_op_leaf) { 6166 return (spa_vdev_exit(spa, vd, 6167 txg, EINVAL)); 6168 } 6169 } 6170 } 6171 } 6172 } 6173 6174 for (int c = 0; c < vd->vdev_children; c++) { 6175 tvd = vd->vdev_child[c]; 6176 vdev_remove_child(vd, tvd); 6177 tvd->vdev_id = rvd->vdev_children; 6178 vdev_add_child(rvd, tvd); 6179 vdev_config_dirty(tvd); 6180 } 6181 6182 if (nspares != 0) { 6183 spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, 6184 ZPOOL_CONFIG_SPARES); 6185 spa_load_spares(spa); 6186 spa->spa_spares.sav_sync = B_TRUE; 6187 } 6188 6189 if (nl2cache != 0) { 6190 spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, 6191 ZPOOL_CONFIG_L2CACHE); 6192 spa_load_l2cache(spa); 6193 spa->spa_l2cache.sav_sync = B_TRUE; 6194 } 6195 6196 /* 6197 * We have to be careful when adding new vdevs to an existing pool. 6198 * If other threads start allocating from these vdevs before we 6199 * sync the config cache, and we lose power, then upon reboot we may 6200 * fail to open the pool because there are DVAs that the config cache 6201 * can't translate. Therefore, we first add the vdevs without 6202 * initializing metaslabs; sync the config cache (via spa_vdev_exit()); 6203 * and then let spa_config_update() initialize the new metaslabs. 6204 * 6205 * spa_load() checks for added-but-not-initialized vdevs, so that 6206 * if we lose power at any point in this sequence, the remaining 6207 * steps will be completed the next time we load the pool. 6208 */ 6209 (void) spa_vdev_exit(spa, vd, txg, 0); 6210 6211 mutex_enter(&spa_namespace_lock); 6212 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 6213 spa_event_notify(spa, NULL, NULL, ESC_ZFS_VDEV_ADD); 6214 mutex_exit(&spa_namespace_lock); 6215 6216 return (0); 6217 } 6218 6219 /* 6220 * Attach a device to a mirror. The arguments are the path to any device 6221 * in the mirror, and the nvroot for the new device. If the path specifies 6222 * a device that is not mirrored, we automatically insert the mirror vdev. 6223 * 6224 * If 'replacing' is specified, the new device is intended to replace the 6225 * existing device; in this case the two devices are made into their own 6226 * mirror using the 'replacing' vdev, which is functionally identical to 6227 * the mirror vdev (it actually reuses all the same ops) but has a few 6228 * extra rules: you can't attach to it after it's been created, and upon 6229 * completion of resilvering, the first disk (the one being replaced) 6230 * is automatically detached. 6231 */ 6232 int 6233 spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) 6234 { 6235 uint64_t txg, dtl_max_txg; 6236 vdev_t *rvd = spa->spa_root_vdev; 6237 vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; 6238 vdev_ops_t *pvops; 6239 char *oldvdpath, *newvdpath; 6240 int newvd_isspare; 6241 int error; 6242 6243 ASSERT(spa_writeable(spa)); 6244 6245 txg = spa_vdev_enter(spa); 6246 6247 oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); 6248 6249 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 6250 if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { 6251 error = (spa_has_checkpoint(spa)) ? 6252 ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; 6253 return (spa_vdev_exit(spa, NULL, txg, error)); 6254 } 6255 6256 if (spa->spa_vdev_removal != NULL) 6257 return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 6258 6259 if (oldvd == NULL) 6260 return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 6261 6262 if (!oldvd->vdev_ops->vdev_op_leaf) 6263 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 6264 6265 pvd = oldvd->vdev_parent; 6266 6267 if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, 6268 VDEV_ALLOC_ATTACH)) != 0) 6269 return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 6270 6271 if (newrootvd->vdev_children != 1) 6272 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 6273 6274 newvd = newrootvd->vdev_child[0]; 6275 6276 if (!newvd->vdev_ops->vdev_op_leaf) 6277 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); 6278 6279 if ((error = vdev_create(newrootvd, txg, replacing)) != 0) 6280 return (spa_vdev_exit(spa, newrootvd, txg, error)); 6281 6282 /* 6283 * Spares can't replace logs 6284 */ 6285 if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare) 6286 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 6287 6288 if (!replacing) { 6289 /* 6290 * For attach, the only allowable parent is a mirror or the root 6291 * vdev. 6292 */ 6293 if (pvd->vdev_ops != &vdev_mirror_ops && 6294 pvd->vdev_ops != &vdev_root_ops) 6295 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 6296 6297 pvops = &vdev_mirror_ops; 6298 } else { 6299 /* 6300 * Active hot spares can only be replaced by inactive hot 6301 * spares. 6302 */ 6303 if (pvd->vdev_ops == &vdev_spare_ops && 6304 oldvd->vdev_isspare && 6305 !spa_has_spare(spa, newvd->vdev_guid)) 6306 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 6307 6308 /* 6309 * If the source is a hot spare, and the parent isn't already a 6310 * spare, then we want to create a new hot spare. Otherwise, we 6311 * want to create a replacing vdev. The user is not allowed to 6312 * attach to a spared vdev child unless the 'isspare' state is 6313 * the same (spare replaces spare, non-spare replaces 6314 * non-spare). 6315 */ 6316 if (pvd->vdev_ops == &vdev_replacing_ops && 6317 spa_version(spa) < SPA_VERSION_MULTI_REPLACE) { 6318 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 6319 } else if (pvd->vdev_ops == &vdev_spare_ops && 6320 newvd->vdev_isspare != oldvd->vdev_isspare) { 6321 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); 6322 } 6323 6324 if (newvd->vdev_isspare) 6325 pvops = &vdev_spare_ops; 6326 else 6327 pvops = &vdev_replacing_ops; 6328 } 6329 6330 /* 6331 * Make sure the new device is big enough. 6332 */ 6333 if (newvd->vdev_asize < vdev_get_min_asize(oldvd)) 6334 return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); 6335 6336 /* 6337 * The new device cannot have a higher alignment requirement 6338 * than the top-level vdev. 6339 */ 6340 if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) 6341 return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); 6342 6343 /* 6344 * If this is an in-place replacement, update oldvd's path and devid 6345 * to make it distinguishable from newvd, and unopenable from now on. 6346 */ 6347 if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { 6348 spa_strfree(oldvd->vdev_path); 6349 oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, 6350 KM_SLEEP); 6351 (void) sprintf(oldvd->vdev_path, "%s/%s", 6352 newvd->vdev_path, "old"); 6353 if (oldvd->vdev_devid != NULL) { 6354 spa_strfree(oldvd->vdev_devid); 6355 oldvd->vdev_devid = NULL; 6356 } 6357 } 6358 6359 /* mark the device being resilvered */ 6360 newvd->vdev_resilver_txg = txg; 6361 6362 /* 6363 * If the parent is not a mirror, or if we're replacing, insert the new 6364 * mirror/replacing/spare vdev above oldvd. 6365 */ 6366 if (pvd->vdev_ops != pvops) 6367 pvd = vdev_add_parent(oldvd, pvops); 6368 6369 ASSERT(pvd->vdev_top->vdev_parent == rvd); 6370 ASSERT(pvd->vdev_ops == pvops); 6371 ASSERT(oldvd->vdev_parent == pvd); 6372 6373 /* 6374 * Extract the new device from its root and add it to pvd. 6375 */ 6376 vdev_remove_child(newrootvd, newvd); 6377 newvd->vdev_id = pvd->vdev_children; 6378 newvd->vdev_crtxg = oldvd->vdev_crtxg; 6379 vdev_add_child(pvd, newvd); 6380 6381 tvd = newvd->vdev_top; 6382 ASSERT(pvd->vdev_top == tvd); 6383 ASSERT(tvd->vdev_parent == rvd); 6384 6385 vdev_config_dirty(tvd); 6386 6387 /* 6388 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account 6389 * for any dmu_sync-ed blocks. It will propagate upward when 6390 * spa_vdev_exit() calls vdev_dtl_reassess(). 6391 */ 6392 dtl_max_txg = txg + TXG_CONCURRENT_STATES; 6393 6394 vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL, 6395 dtl_max_txg - TXG_INITIAL); 6396 6397 if (newvd->vdev_isspare) { 6398 spa_spare_activate(newvd); 6399 spa_event_notify(spa, newvd, NULL, ESC_ZFS_VDEV_SPARE); 6400 } 6401 6402 oldvdpath = spa_strdup(oldvd->vdev_path); 6403 newvdpath = spa_strdup(newvd->vdev_path); 6404 newvd_isspare = newvd->vdev_isspare; 6405 6406 /* 6407 * Mark newvd's DTL dirty in this txg. 6408 */ 6409 vdev_dirty(tvd, VDD_DTL, newvd, txg); 6410 6411 /* 6412 * Schedule the resilver to restart in the future. We do this to 6413 * ensure that dmu_sync-ed blocks have been stitched into the 6414 * respective datasets. We do not do this if resilvers have been 6415 * deferred. 6416 */ 6417 if (dsl_scan_resilvering(spa_get_dsl(spa)) && 6418 spa_feature_is_enabled(spa, SPA_FEATURE_RESILVER_DEFER)) 6419 vdev_defer_resilver(newvd); 6420 else 6421 dsl_scan_restart_resilver(spa->spa_dsl_pool, dtl_max_txg); 6422 6423 if (spa->spa_bootfs) 6424 spa_event_notify(spa, newvd, NULL, ESC_ZFS_BOOTFS_VDEV_ATTACH); 6425 6426 spa_event_notify(spa, newvd, NULL, ESC_ZFS_VDEV_ATTACH); 6427 6428 /* 6429 * Commit the config 6430 */ 6431 (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0); 6432 6433 spa_history_log_internal(spa, "vdev attach", NULL, 6434 "%s vdev=%s %s vdev=%s", 6435 replacing && newvd_isspare ? "spare in" : 6436 replacing ? "replace" : "attach", newvdpath, 6437 replacing ? "for" : "to", oldvdpath); 6438 6439 spa_strfree(oldvdpath); 6440 spa_strfree(newvdpath); 6441 6442 return (0); 6443 } 6444 6445 /* 6446 * Detach a device from a mirror or replacing vdev. 6447 * 6448 * If 'replace_done' is specified, only detach if the parent 6449 * is a replacing vdev. 6450 */ 6451 int 6452 spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done) 6453 { 6454 uint64_t txg; 6455 int error; 6456 vdev_t *rvd = spa->spa_root_vdev; 6457 vdev_t *vd, *pvd, *cvd, *tvd; 6458 boolean_t unspare = B_FALSE; 6459 uint64_t unspare_guid = 0; 6460 char *vdpath; 6461 6462 ASSERT(spa_writeable(spa)); 6463 6464 txg = spa_vdev_enter(spa); 6465 6466 vd = spa_lookup_by_guid(spa, guid, B_FALSE); 6467 6468 /* 6469 * Besides being called directly from the userland through the 6470 * ioctl interface, spa_vdev_detach() can be potentially called 6471 * at the end of spa_vdev_resilver_done(). 6472 * 6473 * In the regular case, when we have a checkpoint this shouldn't 6474 * happen as we never empty the DTLs of a vdev during the scrub 6475 * [see comment in dsl_scan_done()]. Thus spa_vdev_resilvering_done() 6476 * should never get here when we have a checkpoint. 6477 * 6478 * That said, even in a case when we checkpoint the pool exactly 6479 * as spa_vdev_resilver_done() calls this function everything 6480 * should be fine as the resilver will return right away. 6481 */ 6482 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 6483 if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { 6484 error = (spa_has_checkpoint(spa)) ? 6485 ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; 6486 return (spa_vdev_exit(spa, NULL, txg, error)); 6487 } 6488 6489 if (vd == NULL) 6490 return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 6491 6492 if (!vd->vdev_ops->vdev_op_leaf) 6493 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 6494 6495 pvd = vd->vdev_parent; 6496 6497 /* 6498 * If the parent/child relationship is not as expected, don't do it. 6499 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing 6500 * vdev that's replacing B with C. The user's intent in replacing 6501 * is to go from M(A,B) to M(A,C). If the user decides to cancel 6502 * the replace by detaching C, the expected behavior is to end up 6503 * M(A,B). But suppose that right after deciding to detach C, 6504 * the replacement of B completes. We would have M(A,C), and then 6505 * ask to detach C, which would leave us with just A -- not what 6506 * the user wanted. To prevent this, we make sure that the 6507 * parent/child relationship hasn't changed -- in this example, 6508 * that C's parent is still the replacing vdev R. 6509 */ 6510 if (pvd->vdev_guid != pguid && pguid != 0) 6511 return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 6512 6513 /* 6514 * Only 'replacing' or 'spare' vdevs can be replaced. 6515 */ 6516 if (replace_done && pvd->vdev_ops != &vdev_replacing_ops && 6517 pvd->vdev_ops != &vdev_spare_ops) 6518 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 6519 6520 ASSERT(pvd->vdev_ops != &vdev_spare_ops || 6521 spa_version(spa) >= SPA_VERSION_SPARES); 6522 6523 /* 6524 * Only mirror, replacing, and spare vdevs support detach. 6525 */ 6526 if (pvd->vdev_ops != &vdev_replacing_ops && 6527 pvd->vdev_ops != &vdev_mirror_ops && 6528 pvd->vdev_ops != &vdev_spare_ops) 6529 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 6530 6531 /* 6532 * If this device has the only valid copy of some data, 6533 * we cannot safely detach it. 6534 */ 6535 if (vdev_dtl_required(vd)) 6536 return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 6537 6538 ASSERT(pvd->vdev_children >= 2); 6539 6540 /* 6541 * If we are detaching the second disk from a replacing vdev, then 6542 * check to see if we changed the original vdev's path to have "/old" 6543 * at the end in spa_vdev_attach(). If so, undo that change now. 6544 */ 6545 if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 && 6546 vd->vdev_path != NULL) { 6547 size_t len = strlen(vd->vdev_path); 6548 6549 for (int c = 0; c < pvd->vdev_children; c++) { 6550 cvd = pvd->vdev_child[c]; 6551 6552 if (cvd == vd || cvd->vdev_path == NULL) 6553 continue; 6554 6555 if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && 6556 strcmp(cvd->vdev_path + len, "/old") == 0) { 6557 spa_strfree(cvd->vdev_path); 6558 cvd->vdev_path = spa_strdup(vd->vdev_path); 6559 break; 6560 } 6561 } 6562 } 6563 6564 /* 6565 * If we are detaching the original disk from a spare, then it implies 6566 * that the spare should become a real disk, and be removed from the 6567 * active spare list for the pool. 6568 */ 6569 if (pvd->vdev_ops == &vdev_spare_ops && 6570 vd->vdev_id == 0 && 6571 pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare) 6572 unspare = B_TRUE; 6573 6574 /* 6575 * Erase the disk labels so the disk can be used for other things. 6576 * This must be done after all other error cases are handled, 6577 * but before we disembowel vd (so we can still do I/O to it). 6578 * But if we can't do it, don't treat the error as fatal -- 6579 * it may be that the unwritability of the disk is the reason 6580 * it's being detached! 6581 */ 6582 error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 6583 6584 /* 6585 * Remove vd from its parent and compact the parent's children. 6586 */ 6587 vdev_remove_child(pvd, vd); 6588 vdev_compact_children(pvd); 6589 6590 /* 6591 * Remember one of the remaining children so we can get tvd below. 6592 */ 6593 cvd = pvd->vdev_child[pvd->vdev_children - 1]; 6594 6595 /* 6596 * If we need to remove the remaining child from the list of hot spares, 6597 * do it now, marking the vdev as no longer a spare in the process. 6598 * We must do this before vdev_remove_parent(), because that can 6599 * change the GUID if it creates a new toplevel GUID. For a similar 6600 * reason, we must remove the spare now, in the same txg as the detach; 6601 * otherwise someone could attach a new sibling, change the GUID, and 6602 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail. 6603 */ 6604 if (unspare) { 6605 ASSERT(cvd->vdev_isspare); 6606 spa_spare_remove(cvd); 6607 unspare_guid = cvd->vdev_guid; 6608 (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); 6609 cvd->vdev_unspare = B_TRUE; 6610 } 6611 6612 /* 6613 * If the parent mirror/replacing vdev only has one child, 6614 * the parent is no longer needed. Remove it from the tree. 6615 */ 6616 if (pvd->vdev_children == 1) { 6617 if (pvd->vdev_ops == &vdev_spare_ops) 6618 cvd->vdev_unspare = B_FALSE; 6619 vdev_remove_parent(cvd); 6620 } 6621 6622 /* 6623 * We don't set tvd until now because the parent we just removed 6624 * may have been the previous top-level vdev. 6625 */ 6626 tvd = cvd->vdev_top; 6627 ASSERT(tvd->vdev_parent == rvd); 6628 6629 /* 6630 * Reevaluate the parent vdev state. 6631 */ 6632 vdev_propagate_state(cvd); 6633 6634 /* 6635 * If the 'autoexpand' property is set on the pool then automatically 6636 * try to expand the size of the pool. For example if the device we 6637 * just detached was smaller than the others, it may be possible to 6638 * add metaslabs (i.e. grow the pool). We need to reopen the vdev 6639 * first so that we can obtain the updated sizes of the leaf vdevs. 6640 */ 6641 if (spa->spa_autoexpand) { 6642 vdev_reopen(tvd); 6643 vdev_expand(tvd, txg); 6644 } 6645 6646 vdev_config_dirty(tvd); 6647 6648 /* 6649 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that 6650 * vd->vdev_detached is set and free vd's DTL object in syncing context. 6651 * But first make sure we're not on any *other* txg's DTL list, to 6652 * prevent vd from being accessed after it's freed. 6653 */ 6654 vdpath = spa_strdup(vd->vdev_path); 6655 for (int t = 0; t < TXG_SIZE; t++) 6656 (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); 6657 vd->vdev_detached = B_TRUE; 6658 vdev_dirty(tvd, VDD_DTL, vd, txg); 6659 6660 spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_REMOVE); 6661 6662 /* hang on to the spa before we release the lock */ 6663 spa_open_ref(spa, FTAG); 6664 6665 error = spa_vdev_exit(spa, vd, txg, 0); 6666 6667 spa_history_log_internal(spa, "detach", NULL, 6668 "vdev=%s", vdpath); 6669 spa_strfree(vdpath); 6670 6671 /* 6672 * If this was the removal of the original device in a hot spare vdev, 6673 * then we want to go through and remove the device from the hot spare 6674 * list of every other pool. 6675 */ 6676 if (unspare) { 6677 spa_t *altspa = NULL; 6678 6679 mutex_enter(&spa_namespace_lock); 6680 while ((altspa = spa_next(altspa)) != NULL) { 6681 if (altspa->spa_state != POOL_STATE_ACTIVE || 6682 altspa == spa) 6683 continue; 6684 6685 spa_open_ref(altspa, FTAG); 6686 mutex_exit(&spa_namespace_lock); 6687 (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE); 6688 mutex_enter(&spa_namespace_lock); 6689 spa_close(altspa, FTAG); 6690 } 6691 mutex_exit(&spa_namespace_lock); 6692 6693 /* search the rest of the vdevs for spares to remove */ 6694 spa_vdev_resilver_done(spa); 6695 } 6696 6697 /* all done with the spa; OK to release */ 6698 mutex_enter(&spa_namespace_lock); 6699 spa_close(spa, FTAG); 6700 mutex_exit(&spa_namespace_lock); 6701 6702 return (error); 6703 } 6704 6705 static int 6706 spa_vdev_initialize_impl(spa_t *spa, uint64_t guid, uint64_t cmd_type, 6707 list_t *vd_list) 6708 { 6709 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 6710 6711 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); 6712 6713 /* Look up vdev and ensure it's a leaf. */ 6714 vdev_t *vd = spa_lookup_by_guid(spa, guid, B_FALSE); 6715 if (vd == NULL || vd->vdev_detached) { 6716 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 6717 return (SET_ERROR(ENODEV)); 6718 } else if (!vd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(vd)) { 6719 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 6720 return (SET_ERROR(EINVAL)); 6721 } else if (!vdev_writeable(vd)) { 6722 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 6723 return (SET_ERROR(EROFS)); 6724 } 6725 mutex_enter(&vd->vdev_initialize_lock); 6726 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 6727 6728 /* 6729 * When we activate an initialize action we check to see 6730 * if the vdev_initialize_thread is NULL. We do this instead 6731 * of using the vdev_initialize_state since there might be 6732 * a previous initialization process which has completed but 6733 * the thread is not exited. 6734 */ 6735 if (cmd_type == POOL_INITIALIZE_START && 6736 (vd->vdev_initialize_thread != NULL || 6737 vd->vdev_top->vdev_removing)) { 6738 mutex_exit(&vd->vdev_initialize_lock); 6739 return (SET_ERROR(EBUSY)); 6740 } else if (cmd_type == POOL_INITIALIZE_CANCEL && 6741 (vd->vdev_initialize_state != VDEV_INITIALIZE_ACTIVE && 6742 vd->vdev_initialize_state != VDEV_INITIALIZE_SUSPENDED)) { 6743 mutex_exit(&vd->vdev_initialize_lock); 6744 return (SET_ERROR(ESRCH)); 6745 } else if (cmd_type == POOL_INITIALIZE_SUSPEND && 6746 vd->vdev_initialize_state != VDEV_INITIALIZE_ACTIVE) { 6747 mutex_exit(&vd->vdev_initialize_lock); 6748 return (SET_ERROR(ESRCH)); 6749 } 6750 6751 switch (cmd_type) { 6752 case POOL_INITIALIZE_START: 6753 vdev_initialize(vd); 6754 break; 6755 case POOL_INITIALIZE_CANCEL: 6756 vdev_initialize_stop(vd, VDEV_INITIALIZE_CANCELED, vd_list); 6757 break; 6758 case POOL_INITIALIZE_SUSPEND: 6759 vdev_initialize_stop(vd, VDEV_INITIALIZE_SUSPENDED, vd_list); 6760 break; 6761 default: 6762 panic("invalid cmd_type %llu", (unsigned long long)cmd_type); 6763 } 6764 mutex_exit(&vd->vdev_initialize_lock); 6765 6766 return (0); 6767 } 6768 6769 int 6770 spa_vdev_initialize(spa_t *spa, nvlist_t *nv, uint64_t cmd_type, 6771 nvlist_t *vdev_errlist) 6772 { 6773 int total_errors = 0; 6774 list_t vd_list; 6775 6776 list_create(&vd_list, sizeof (vdev_t), 6777 offsetof(vdev_t, vdev_initialize_node)); 6778 6779 /* 6780 * We hold the namespace lock through the whole function 6781 * to prevent any changes to the pool while we're starting or 6782 * stopping initialization. The config and state locks are held so that 6783 * we can properly assess the vdev state before we commit to 6784 * the initializing operation. 6785 */ 6786 mutex_enter(&spa_namespace_lock); 6787 6788 for (nvpair_t *pair = nvlist_next_nvpair(nv, NULL); 6789 pair != NULL; pair = nvlist_next_nvpair(nv, pair)) { 6790 uint64_t vdev_guid = fnvpair_value_uint64(pair); 6791 6792 int error = spa_vdev_initialize_impl(spa, vdev_guid, cmd_type, 6793 &vd_list); 6794 if (error != 0) { 6795 char guid_as_str[MAXNAMELEN]; 6796 6797 (void) snprintf(guid_as_str, sizeof (guid_as_str), 6798 "%llu", (unsigned long long)vdev_guid); 6799 fnvlist_add_int64(vdev_errlist, guid_as_str, error); 6800 total_errors++; 6801 } 6802 } 6803 6804 /* Wait for all initialize threads to stop. */ 6805 vdev_initialize_stop_wait(spa, &vd_list); 6806 6807 /* Sync out the initializing state */ 6808 txg_wait_synced(spa->spa_dsl_pool, 0); 6809 mutex_exit(&spa_namespace_lock); 6810 6811 list_destroy(&vd_list); 6812 6813 return (total_errors); 6814 } 6815 6816 static int 6817 spa_vdev_trim_impl(spa_t *spa, uint64_t guid, uint64_t cmd_type, 6818 uint64_t rate, boolean_t partial, boolean_t secure, list_t *vd_list) 6819 { 6820 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 6821 6822 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); 6823 6824 /* Look up vdev and ensure it's a leaf. */ 6825 vdev_t *vd = spa_lookup_by_guid(spa, guid, B_FALSE); 6826 if (vd == NULL || vd->vdev_detached) { 6827 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 6828 return (SET_ERROR(ENODEV)); 6829 } else if (!vd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(vd)) { 6830 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 6831 return (SET_ERROR(EINVAL)); 6832 } else if (!vdev_writeable(vd)) { 6833 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 6834 return (SET_ERROR(EROFS)); 6835 } else if (!vd->vdev_has_trim) { 6836 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 6837 return (SET_ERROR(EOPNOTSUPP)); 6838 } else if (secure && !vd->vdev_has_securetrim) { 6839 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 6840 return (SET_ERROR(EOPNOTSUPP)); 6841 } 6842 mutex_enter(&vd->vdev_trim_lock); 6843 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 6844 6845 /* 6846 * When we activate a TRIM action we check to see if the 6847 * vdev_trim_thread is NULL. We do this instead of using the 6848 * vdev_trim_state since there might be a previous TRIM process 6849 * which has completed but the thread is not exited. 6850 */ 6851 if (cmd_type == POOL_TRIM_START && 6852 (vd->vdev_trim_thread != NULL || vd->vdev_top->vdev_removing)) { 6853 mutex_exit(&vd->vdev_trim_lock); 6854 return (SET_ERROR(EBUSY)); 6855 } else if (cmd_type == POOL_TRIM_CANCEL && 6856 (vd->vdev_trim_state != VDEV_TRIM_ACTIVE && 6857 vd->vdev_trim_state != VDEV_TRIM_SUSPENDED)) { 6858 mutex_exit(&vd->vdev_trim_lock); 6859 return (SET_ERROR(ESRCH)); 6860 } else if (cmd_type == POOL_TRIM_SUSPEND && 6861 vd->vdev_trim_state != VDEV_TRIM_ACTIVE) { 6862 mutex_exit(&vd->vdev_trim_lock); 6863 return (SET_ERROR(ESRCH)); 6864 } 6865 6866 switch (cmd_type) { 6867 case POOL_TRIM_START: 6868 vdev_trim(vd, rate, partial, secure); 6869 break; 6870 case POOL_TRIM_CANCEL: 6871 vdev_trim_stop(vd, VDEV_TRIM_CANCELED, vd_list); 6872 break; 6873 case POOL_TRIM_SUSPEND: 6874 vdev_trim_stop(vd, VDEV_TRIM_SUSPENDED, vd_list); 6875 break; 6876 default: 6877 panic("invalid cmd_type %llu", (unsigned long long)cmd_type); 6878 } 6879 mutex_exit(&vd->vdev_trim_lock); 6880 6881 return (0); 6882 } 6883 6884 /* 6885 * Initiates a manual TRIM for the requested vdevs. This kicks off individual 6886 * TRIM threads for each child vdev. These threads pass over all of the free 6887 * space in the vdev's metaslabs and issues TRIM commands for that space. 6888 */ 6889 int 6890 spa_vdev_trim(spa_t *spa, nvlist_t *nv, uint64_t cmd_type, uint64_t rate, 6891 boolean_t partial, boolean_t secure, nvlist_t *vdev_errlist) 6892 { 6893 int total_errors = 0; 6894 list_t vd_list; 6895 6896 list_create(&vd_list, sizeof (vdev_t), 6897 offsetof(vdev_t, vdev_trim_node)); 6898 6899 /* 6900 * We hold the namespace lock through the whole function 6901 * to prevent any changes to the pool while we're starting or 6902 * stopping TRIM. The config and state locks are held so that 6903 * we can properly assess the vdev state before we commit to 6904 * the TRIM operation. 6905 */ 6906 mutex_enter(&spa_namespace_lock); 6907 6908 for (nvpair_t *pair = nvlist_next_nvpair(nv, NULL); 6909 pair != NULL; pair = nvlist_next_nvpair(nv, pair)) { 6910 uint64_t vdev_guid = fnvpair_value_uint64(pair); 6911 6912 int error = spa_vdev_trim_impl(spa, vdev_guid, cmd_type, 6913 rate, partial, secure, &vd_list); 6914 if (error != 0) { 6915 char guid_as_str[MAXNAMELEN]; 6916 6917 (void) snprintf(guid_as_str, sizeof (guid_as_str), 6918 "%llu", (unsigned long long)vdev_guid); 6919 fnvlist_add_int64(vdev_errlist, guid_as_str, error); 6920 total_errors++; 6921 } 6922 } 6923 6924 /* Wait for all TRIM threads to stop. */ 6925 vdev_trim_stop_wait(spa, &vd_list); 6926 6927 /* Sync out the TRIM state */ 6928 txg_wait_synced(spa->spa_dsl_pool, 0); 6929 mutex_exit(&spa_namespace_lock); 6930 6931 list_destroy(&vd_list); 6932 6933 return (total_errors); 6934 } 6935 6936 /* 6937 * Split a set of devices from their mirrors, and create a new pool from them. 6938 */ 6939 int 6940 spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config, 6941 nvlist_t *props, boolean_t exp) 6942 { 6943 int error = 0; 6944 uint64_t txg, *glist; 6945 spa_t *newspa; 6946 uint_t c, children, lastlog; 6947 nvlist_t **child, *nvl, *tmp; 6948 dmu_tx_t *tx; 6949 char *altroot = NULL; 6950 vdev_t *rvd, **vml = NULL; /* vdev modify list */ 6951 boolean_t activate_slog; 6952 6953 ASSERT(spa_writeable(spa)); 6954 6955 txg = spa_vdev_enter(spa); 6956 6957 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 6958 if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { 6959 error = (spa_has_checkpoint(spa)) ? 6960 ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; 6961 return (spa_vdev_exit(spa, NULL, txg, error)); 6962 } 6963 6964 /* clear the log and flush everything up to now */ 6965 activate_slog = spa_passivate_log(spa); 6966 (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 6967 error = spa_reset_logs(spa); 6968 txg = spa_vdev_config_enter(spa); 6969 6970 if (activate_slog) 6971 spa_activate_log(spa); 6972 6973 if (error != 0) 6974 return (spa_vdev_exit(spa, NULL, txg, error)); 6975 6976 /* check new spa name before going any further */ 6977 if (spa_lookup(newname) != NULL) 6978 return (spa_vdev_exit(spa, NULL, txg, EEXIST)); 6979 6980 /* 6981 * scan through all the children to ensure they're all mirrors 6982 */ 6983 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 || 6984 nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child, 6985 &children) != 0) 6986 return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 6987 6988 /* first, check to ensure we've got the right child count */ 6989 rvd = spa->spa_root_vdev; 6990 lastlog = 0; 6991 for (c = 0; c < rvd->vdev_children; c++) { 6992 vdev_t *vd = rvd->vdev_child[c]; 6993 6994 /* don't count the holes & logs as children */ 6995 if (vd->vdev_islog || !vdev_is_concrete(vd)) { 6996 if (lastlog == 0) 6997 lastlog = c; 6998 continue; 6999 } 7000 7001 lastlog = 0; 7002 } 7003 if (children != (lastlog != 0 ? lastlog : rvd->vdev_children)) 7004 return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 7005 7006 /* next, ensure no spare or cache devices are part of the split */ 7007 if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 || 7008 nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0) 7009 return (spa_vdev_exit(spa, NULL, txg, EINVAL)); 7010 7011 vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP); 7012 glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP); 7013 7014 /* then, loop over each vdev and validate it */ 7015 for (c = 0; c < children; c++) { 7016 uint64_t is_hole = 0; 7017 7018 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 7019 &is_hole); 7020 7021 if (is_hole != 0) { 7022 if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole || 7023 spa->spa_root_vdev->vdev_child[c]->vdev_islog) { 7024 continue; 7025 } else { 7026 error = SET_ERROR(EINVAL); 7027 break; 7028 } 7029 } 7030 7031 /* which disk is going to be split? */ 7032 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID, 7033 &glist[c]) != 0) { 7034 error = SET_ERROR(EINVAL); 7035 break; 7036 } 7037 7038 /* look it up in the spa */ 7039 vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE); 7040 if (vml[c] == NULL) { 7041 error = SET_ERROR(ENODEV); 7042 break; 7043 } 7044 7045 /* make sure there's nothing stopping the split */ 7046 if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops || 7047 vml[c]->vdev_islog || 7048 !vdev_is_concrete(vml[c]) || 7049 vml[c]->vdev_isspare || 7050 vml[c]->vdev_isl2cache || 7051 !vdev_writeable(vml[c]) || 7052 vml[c]->vdev_children != 0 || 7053 vml[c]->vdev_state != VDEV_STATE_HEALTHY || 7054 c != spa->spa_root_vdev->vdev_child[c]->vdev_id) { 7055 error = SET_ERROR(EINVAL); 7056 break; 7057 } 7058 7059 if (vdev_dtl_required(vml[c])) { 7060 error = SET_ERROR(EBUSY); 7061 break; 7062 } 7063 7064 /* we need certain info from the top level */ 7065 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY, 7066 vml[c]->vdev_top->vdev_ms_array) == 0); 7067 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT, 7068 vml[c]->vdev_top->vdev_ms_shift) == 0); 7069 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE, 7070 vml[c]->vdev_top->vdev_asize) == 0); 7071 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT, 7072 vml[c]->vdev_top->vdev_ashift) == 0); 7073 7074 /* transfer per-vdev ZAPs */ 7075 ASSERT3U(vml[c]->vdev_leaf_zap, !=, 0); 7076 VERIFY0(nvlist_add_uint64(child[c], 7077 ZPOOL_CONFIG_VDEV_LEAF_ZAP, vml[c]->vdev_leaf_zap)); 7078 7079 ASSERT3U(vml[c]->vdev_top->vdev_top_zap, !=, 0); 7080 VERIFY0(nvlist_add_uint64(child[c], 7081 ZPOOL_CONFIG_VDEV_TOP_ZAP, 7082 vml[c]->vdev_parent->vdev_top_zap)); 7083 } 7084 7085 if (error != 0) { 7086 kmem_free(vml, children * sizeof (vdev_t *)); 7087 kmem_free(glist, children * sizeof (uint64_t)); 7088 return (spa_vdev_exit(spa, NULL, txg, error)); 7089 } 7090 7091 /* stop writers from using the disks */ 7092 for (c = 0; c < children; c++) { 7093 if (vml[c] != NULL) 7094 vml[c]->vdev_offline = B_TRUE; 7095 } 7096 vdev_reopen(spa->spa_root_vdev); 7097 7098 /* 7099 * Temporarily record the splitting vdevs in the spa config. This 7100 * will disappear once the config is regenerated. 7101 */ 7102 VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); 7103 VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, 7104 glist, children) == 0); 7105 kmem_free(glist, children * sizeof (uint64_t)); 7106 7107 mutex_enter(&spa->spa_props_lock); 7108 VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT, 7109 nvl) == 0); 7110 mutex_exit(&spa->spa_props_lock); 7111 spa->spa_config_splitting = nvl; 7112 vdev_config_dirty(spa->spa_root_vdev); 7113 7114 /* configure and create the new pool */ 7115 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0); 7116 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 7117 exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0); 7118 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, 7119 spa_version(spa)) == 0); 7120 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, 7121 spa->spa_config_txg) == 0); 7122 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID, 7123 spa_generate_guid(NULL)) == 0); 7124 VERIFY0(nvlist_add_boolean(config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)); 7125 (void) nvlist_lookup_string(props, 7126 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); 7127 7128 /* add the new pool to the namespace */ 7129 newspa = spa_add(newname, config, altroot); 7130 newspa->spa_avz_action = AVZ_ACTION_REBUILD; 7131 newspa->spa_config_txg = spa->spa_config_txg; 7132 spa_set_log_state(newspa, SPA_LOG_CLEAR); 7133 7134 /* release the spa config lock, retaining the namespace lock */ 7135 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); 7136 7137 if (zio_injection_enabled) 7138 zio_handle_panic_injection(spa, FTAG, 1); 7139 7140 spa_activate(newspa, spa_mode_global); 7141 spa_async_suspend(newspa); 7142 7143 /* 7144 * Temporarily stop the initializing and TRIM activity. We set the 7145 * state to ACTIVE so that we know to resume initializing or TRIM 7146 * once the split has completed. 7147 */ 7148 list_t vd_initialize_list; 7149 list_create(&vd_initialize_list, sizeof (vdev_t), 7150 offsetof(vdev_t, vdev_initialize_node)); 7151 7152 list_t vd_trim_list; 7153 list_create(&vd_trim_list, sizeof (vdev_t), 7154 offsetof(vdev_t, vdev_trim_node)); 7155 7156 for (c = 0; c < children; c++) { 7157 if (vml[c] != NULL) { 7158 mutex_enter(&vml[c]->vdev_initialize_lock); 7159 vdev_initialize_stop(vml[c], 7160 VDEV_INITIALIZE_ACTIVE, &vd_initialize_list); 7161 mutex_exit(&vml[c]->vdev_initialize_lock); 7162 7163 mutex_enter(&vml[c]->vdev_trim_lock); 7164 vdev_trim_stop(vml[c], VDEV_TRIM_ACTIVE, &vd_trim_list); 7165 mutex_exit(&vml[c]->vdev_trim_lock); 7166 } 7167 } 7168 7169 vdev_initialize_stop_wait(spa, &vd_initialize_list); 7170 vdev_trim_stop_wait(spa, &vd_trim_list); 7171 7172 list_destroy(&vd_initialize_list); 7173 list_destroy(&vd_trim_list); 7174 7175 newspa->spa_config_source = SPA_CONFIG_SRC_SPLIT; 7176 7177 /* create the new pool from the disks of the original pool */ 7178 error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE); 7179 if (error) 7180 goto out; 7181 7182 /* if that worked, generate a real config for the new pool */ 7183 if (newspa->spa_root_vdev != NULL) { 7184 VERIFY(nvlist_alloc(&newspa->spa_config_splitting, 7185 NV_UNIQUE_NAME, KM_SLEEP) == 0); 7186 VERIFY(nvlist_add_uint64(newspa->spa_config_splitting, 7187 ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0); 7188 spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL, 7189 B_TRUE)); 7190 } 7191 7192 /* set the props */ 7193 if (props != NULL) { 7194 spa_configfile_set(newspa, props, B_FALSE); 7195 error = spa_prop_set(newspa, props); 7196 if (error) 7197 goto out; 7198 } 7199 7200 /* flush everything */ 7201 txg = spa_vdev_config_enter(newspa); 7202 vdev_config_dirty(newspa->spa_root_vdev); 7203 (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG); 7204 7205 if (zio_injection_enabled) 7206 zio_handle_panic_injection(spa, FTAG, 2); 7207 7208 spa_async_resume(newspa); 7209 7210 /* finally, update the original pool's config */ 7211 txg = spa_vdev_config_enter(spa); 7212 tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); 7213 error = dmu_tx_assign(tx, TXG_WAIT); 7214 if (error != 0) 7215 dmu_tx_abort(tx); 7216 for (c = 0; c < children; c++) { 7217 if (vml[c] != NULL) { 7218 vdev_split(vml[c]); 7219 if (error == 0) 7220 spa_history_log_internal(spa, "detach", tx, 7221 "vdev=%s", vml[c]->vdev_path); 7222 7223 vdev_free(vml[c]); 7224 } 7225 } 7226 spa->spa_avz_action = AVZ_ACTION_REBUILD; 7227 vdev_config_dirty(spa->spa_root_vdev); 7228 spa->spa_config_splitting = NULL; 7229 nvlist_free(nvl); 7230 if (error == 0) 7231 dmu_tx_commit(tx); 7232 (void) spa_vdev_exit(spa, NULL, txg, 0); 7233 7234 if (zio_injection_enabled) 7235 zio_handle_panic_injection(spa, FTAG, 3); 7236 7237 /* split is complete; log a history record */ 7238 spa_history_log_internal(newspa, "split", NULL, 7239 "from pool %s", spa_name(spa)); 7240 7241 kmem_free(vml, children * sizeof (vdev_t *)); 7242 7243 /* if we're not going to mount the filesystems in userland, export */ 7244 if (exp) 7245 error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL, 7246 B_FALSE, B_FALSE); 7247 7248 return (error); 7249 7250 out: 7251 spa_unload(newspa); 7252 spa_deactivate(newspa); 7253 spa_remove(newspa); 7254 7255 txg = spa_vdev_config_enter(spa); 7256 7257 /* re-online all offlined disks */ 7258 for (c = 0; c < children; c++) { 7259 if (vml[c] != NULL) 7260 vml[c]->vdev_offline = B_FALSE; 7261 } 7262 7263 /* restart initializing or trimming disks as necessary */ 7264 spa_async_request(spa, SPA_ASYNC_INITIALIZE_RESTART); 7265 spa_async_request(spa, SPA_ASYNC_TRIM_RESTART); 7266 spa_async_request(spa, SPA_ASYNC_AUTOTRIM_RESTART); 7267 7268 vdev_reopen(spa->spa_root_vdev); 7269 7270 nvlist_free(spa->spa_config_splitting); 7271 spa->spa_config_splitting = NULL; 7272 (void) spa_vdev_exit(spa, NULL, txg, error); 7273 7274 kmem_free(vml, children * sizeof (vdev_t *)); 7275 return (error); 7276 } 7277 7278 /* 7279 * Find any device that's done replacing, or a vdev marked 'unspare' that's 7280 * currently spared, so we can detach it. 7281 */ 7282 static vdev_t * 7283 spa_vdev_resilver_done_hunt(vdev_t *vd) 7284 { 7285 vdev_t *newvd, *oldvd; 7286 7287 for (int c = 0; c < vd->vdev_children; c++) { 7288 oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); 7289 if (oldvd != NULL) 7290 return (oldvd); 7291 } 7292 7293 /* 7294 * Check for a completed replacement. We always consider the first 7295 * vdev in the list to be the oldest vdev, and the last one to be 7296 * the newest (see spa_vdev_attach() for how that works). In 7297 * the case where the newest vdev is faulted, we will not automatically 7298 * remove it after a resilver completes. This is OK as it will require 7299 * user intervention to determine which disk the admin wishes to keep. 7300 */ 7301 if (vd->vdev_ops == &vdev_replacing_ops) { 7302 ASSERT(vd->vdev_children > 1); 7303 7304 newvd = vd->vdev_child[vd->vdev_children - 1]; 7305 oldvd = vd->vdev_child[0]; 7306 7307 if (vdev_dtl_empty(newvd, DTL_MISSING) && 7308 vdev_dtl_empty(newvd, DTL_OUTAGE) && 7309 !vdev_dtl_required(oldvd)) 7310 return (oldvd); 7311 } 7312 7313 /* 7314 * Check for a completed resilver with the 'unspare' flag set. 7315 * Also potentially update faulted state. 7316 */ 7317 if (vd->vdev_ops == &vdev_spare_ops) { 7318 vdev_t *first = vd->vdev_child[0]; 7319 vdev_t *last = vd->vdev_child[vd->vdev_children - 1]; 7320 7321 if (last->vdev_unspare) { 7322 oldvd = first; 7323 newvd = last; 7324 } else if (first->vdev_unspare) { 7325 oldvd = last; 7326 newvd = first; 7327 } else { 7328 oldvd = NULL; 7329 } 7330 7331 if (oldvd != NULL && 7332 vdev_dtl_empty(newvd, DTL_MISSING) && 7333 vdev_dtl_empty(newvd, DTL_OUTAGE) && 7334 !vdev_dtl_required(oldvd)) 7335 return (oldvd); 7336 7337 vdev_propagate_state(vd); 7338 7339 /* 7340 * If there are more than two spares attached to a disk, 7341 * and those spares are not required, then we want to 7342 * attempt to free them up now so that they can be used 7343 * by other pools. Once we're back down to a single 7344 * disk+spare, we stop removing them. 7345 */ 7346 if (vd->vdev_children > 2) { 7347 newvd = vd->vdev_child[1]; 7348 7349 if (newvd->vdev_isspare && last->vdev_isspare && 7350 vdev_dtl_empty(last, DTL_MISSING) && 7351 vdev_dtl_empty(last, DTL_OUTAGE) && 7352 !vdev_dtl_required(newvd)) 7353 return (newvd); 7354 } 7355 } 7356 7357 return (NULL); 7358 } 7359 7360 static void 7361 spa_vdev_resilver_done(spa_t *spa) 7362 { 7363 vdev_t *vd, *pvd, *ppvd; 7364 uint64_t guid, sguid, pguid, ppguid; 7365 7366 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 7367 7368 while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { 7369 pvd = vd->vdev_parent; 7370 ppvd = pvd->vdev_parent; 7371 guid = vd->vdev_guid; 7372 pguid = pvd->vdev_guid; 7373 ppguid = ppvd->vdev_guid; 7374 sguid = 0; 7375 /* 7376 * If we have just finished replacing a hot spared device, then 7377 * we need to detach the parent's first child (the original hot 7378 * spare) as well. 7379 */ 7380 if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 && 7381 ppvd->vdev_children == 2) { 7382 ASSERT(pvd->vdev_ops == &vdev_replacing_ops); 7383 sguid = ppvd->vdev_child[1]->vdev_guid; 7384 } 7385 ASSERT(vd->vdev_resilver_txg == 0 || !vdev_dtl_required(vd)); 7386 7387 spa_config_exit(spa, SCL_ALL, FTAG); 7388 if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0) 7389 return; 7390 if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0) 7391 return; 7392 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 7393 } 7394 7395 spa_config_exit(spa, SCL_ALL, FTAG); 7396 } 7397 7398 /* 7399 * Update the stored path or FRU for this vdev. 7400 */ 7401 int 7402 spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value, 7403 boolean_t ispath) 7404 { 7405 vdev_t *vd; 7406 boolean_t sync = B_FALSE; 7407 7408 ASSERT(spa_writeable(spa)); 7409 7410 spa_vdev_state_enter(spa, SCL_ALL); 7411 7412 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 7413 return (spa_vdev_state_exit(spa, NULL, ENOENT)); 7414 7415 if (!vd->vdev_ops->vdev_op_leaf) 7416 return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 7417 7418 if (ispath) { 7419 if (strcmp(value, vd->vdev_path) != 0) { 7420 spa_strfree(vd->vdev_path); 7421 vd->vdev_path = spa_strdup(value); 7422 sync = B_TRUE; 7423 } 7424 } else { 7425 if (vd->vdev_fru == NULL) { 7426 vd->vdev_fru = spa_strdup(value); 7427 sync = B_TRUE; 7428 } else if (strcmp(value, vd->vdev_fru) != 0) { 7429 spa_strfree(vd->vdev_fru); 7430 vd->vdev_fru = spa_strdup(value); 7431 sync = B_TRUE; 7432 } 7433 } 7434 7435 return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0)); 7436 } 7437 7438 int 7439 spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) 7440 { 7441 return (spa_vdev_set_common(spa, guid, newpath, B_TRUE)); 7442 } 7443 7444 int 7445 spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru) 7446 { 7447 return (spa_vdev_set_common(spa, guid, newfru, B_FALSE)); 7448 } 7449 7450 /* 7451 * ========================================================================== 7452 * SPA Scanning 7453 * ========================================================================== 7454 */ 7455 int 7456 spa_scrub_pause_resume(spa_t *spa, pool_scrub_cmd_t cmd) 7457 { 7458 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 7459 7460 if (dsl_scan_resilvering(spa->spa_dsl_pool)) 7461 return (SET_ERROR(EBUSY)); 7462 7463 return (dsl_scrub_set_pause_resume(spa->spa_dsl_pool, cmd)); 7464 } 7465 7466 int 7467 spa_scan_stop(spa_t *spa) 7468 { 7469 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 7470 if (dsl_scan_resilvering(spa->spa_dsl_pool)) 7471 return (SET_ERROR(EBUSY)); 7472 return (dsl_scan_cancel(spa->spa_dsl_pool)); 7473 } 7474 7475 int 7476 spa_scan(spa_t *spa, pool_scan_func_t func) 7477 { 7478 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); 7479 7480 if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE) 7481 return (SET_ERROR(ENOTSUP)); 7482 7483 if (func == POOL_SCAN_RESILVER && 7484 !spa_feature_is_enabled(spa, SPA_FEATURE_RESILVER_DEFER)) 7485 return (SET_ERROR(ENOTSUP)); 7486 7487 /* 7488 * If a resilver was requested, but there is no DTL on a 7489 * writeable leaf device, we have nothing to do. 7490 */ 7491 if (func == POOL_SCAN_RESILVER && 7492 !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { 7493 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 7494 return (0); 7495 } 7496 7497 return (dsl_scan(spa->spa_dsl_pool, func)); 7498 } 7499 7500 /* 7501 * ========================================================================== 7502 * SPA async task processing 7503 * ========================================================================== 7504 */ 7505 7506 static void 7507 spa_async_remove(spa_t *spa, vdev_t *vd) 7508 { 7509 if (vd->vdev_remove_wanted) { 7510 vd->vdev_remove_wanted = B_FALSE; 7511 vd->vdev_delayed_close = B_FALSE; 7512 vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); 7513 7514 /* 7515 * We want to clear the stats, but we don't want to do a full 7516 * vdev_clear() as that will cause us to throw away 7517 * degraded/faulted state as well as attempt to reopen the 7518 * device, all of which is a waste. 7519 */ 7520 vd->vdev_stat.vs_read_errors = 0; 7521 vd->vdev_stat.vs_write_errors = 0; 7522 vd->vdev_stat.vs_checksum_errors = 0; 7523 7524 vdev_state_dirty(vd->vdev_top); 7525 } 7526 7527 for (int c = 0; c < vd->vdev_children; c++) 7528 spa_async_remove(spa, vd->vdev_child[c]); 7529 } 7530 7531 static void 7532 spa_async_probe(spa_t *spa, vdev_t *vd) 7533 { 7534 if (vd->vdev_probe_wanted) { 7535 vd->vdev_probe_wanted = B_FALSE; 7536 vdev_reopen(vd); /* vdev_open() does the actual probe */ 7537 } 7538 7539 for (int c = 0; c < vd->vdev_children; c++) 7540 spa_async_probe(spa, vd->vdev_child[c]); 7541 } 7542 7543 static void 7544 spa_async_autoexpand(spa_t *spa, vdev_t *vd) 7545 { 7546 char *physpath; 7547 7548 if (!spa->spa_autoexpand) 7549 return; 7550 7551 for (int c = 0; c < vd->vdev_children; c++) { 7552 vdev_t *cvd = vd->vdev_child[c]; 7553 spa_async_autoexpand(spa, cvd); 7554 } 7555 7556 if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL) 7557 return; 7558 7559 physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 7560 (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath); 7561 7562 zfs_post_dle_sysevent(physpath); 7563 7564 kmem_free(physpath, MAXPATHLEN); 7565 } 7566 7567 static void 7568 spa_async_thread(void *arg) 7569 { 7570 spa_t *spa = (spa_t *)arg; 7571 dsl_pool_t *dp = spa->spa_dsl_pool; 7572 int tasks; 7573 7574 ASSERT(spa->spa_sync_on); 7575 7576 mutex_enter(&spa->spa_async_lock); 7577 tasks = spa->spa_async_tasks; 7578 spa->spa_async_tasks = 0; 7579 mutex_exit(&spa->spa_async_lock); 7580 7581 /* 7582 * See if the config needs to be updated. 7583 */ 7584 if (tasks & SPA_ASYNC_CONFIG_UPDATE) { 7585 uint64_t old_space, new_space; 7586 7587 mutex_enter(&spa_namespace_lock); 7588 old_space = metaslab_class_get_space(spa_normal_class(spa)); 7589 old_space += metaslab_class_get_space(spa_special_class(spa)); 7590 old_space += metaslab_class_get_space(spa_dedup_class(spa)); 7591 7592 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); 7593 7594 new_space = metaslab_class_get_space(spa_normal_class(spa)); 7595 new_space += metaslab_class_get_space(spa_special_class(spa)); 7596 new_space += metaslab_class_get_space(spa_dedup_class(spa)); 7597 mutex_exit(&spa_namespace_lock); 7598 7599 /* 7600 * If the pool grew as a result of the config update, 7601 * then log an internal history event. 7602 */ 7603 if (new_space != old_space) { 7604 spa_history_log_internal(spa, "vdev online", NULL, 7605 "pool '%s' size: %llu(+%llu)", 7606 spa_name(spa), new_space, new_space - old_space); 7607 } 7608 } 7609 7610 /* 7611 * See if any devices need to be marked REMOVED. 7612 */ 7613 if (tasks & SPA_ASYNC_REMOVE) { 7614 spa_vdev_state_enter(spa, SCL_NONE); 7615 spa_async_remove(spa, spa->spa_root_vdev); 7616 for (int i = 0; i < spa->spa_l2cache.sav_count; i++) 7617 spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); 7618 for (int i = 0; i < spa->spa_spares.sav_count; i++) 7619 spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); 7620 (void) spa_vdev_state_exit(spa, NULL, 0); 7621 } 7622 7623 if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) { 7624 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 7625 spa_async_autoexpand(spa, spa->spa_root_vdev); 7626 spa_config_exit(spa, SCL_CONFIG, FTAG); 7627 } 7628 7629 /* 7630 * See if any devices need to be probed. 7631 */ 7632 if (tasks & SPA_ASYNC_PROBE) { 7633 spa_vdev_state_enter(spa, SCL_NONE); 7634 spa_async_probe(spa, spa->spa_root_vdev); 7635 for (int i = 0; i < spa->spa_spares.sav_count; i++) 7636 spa_async_probe(spa, spa->spa_spares.sav_vdevs[i]); 7637 (void) spa_vdev_state_exit(spa, NULL, 0); 7638 } 7639 7640 /* 7641 * If any devices are done replacing, detach them. 7642 */ 7643 if (tasks & SPA_ASYNC_RESILVER_DONE) 7644 spa_vdev_resilver_done(spa); 7645 7646 /* 7647 * Kick off a resilver. 7648 */ 7649 if (tasks & SPA_ASYNC_RESILVER && 7650 (!dsl_scan_resilvering(dp) || 7651 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_RESILVER_DEFER))) 7652 dsl_scan_restart_resilver(dp, 0); 7653 7654 if (tasks & SPA_ASYNC_INITIALIZE_RESTART) { 7655 mutex_enter(&spa_namespace_lock); 7656 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 7657 vdev_initialize_restart(spa->spa_root_vdev); 7658 spa_config_exit(spa, SCL_CONFIG, FTAG); 7659 mutex_exit(&spa_namespace_lock); 7660 } 7661 7662 if (tasks & SPA_ASYNC_TRIM_RESTART) { 7663 mutex_enter(&spa_namespace_lock); 7664 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 7665 vdev_trim_restart(spa->spa_root_vdev); 7666 spa_config_exit(spa, SCL_CONFIG, FTAG); 7667 mutex_exit(&spa_namespace_lock); 7668 } 7669 7670 if (tasks & SPA_ASYNC_AUTOTRIM_RESTART) { 7671 mutex_enter(&spa_namespace_lock); 7672 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 7673 vdev_autotrim_restart(spa); 7674 spa_config_exit(spa, SCL_CONFIG, FTAG); 7675 mutex_exit(&spa_namespace_lock); 7676 } 7677 7678 /* 7679 * Kick off L2 cache rebuilding. 7680 */ 7681 if (tasks & SPA_ASYNC_L2CACHE_REBUILD) { 7682 mutex_enter(&spa_namespace_lock); 7683 spa_config_enter(spa, SCL_L2ARC, FTAG, RW_READER); 7684 l2arc_spa_rebuild_start(spa); 7685 spa_config_exit(spa, SCL_L2ARC, FTAG); 7686 mutex_exit(&spa_namespace_lock); 7687 } 7688 7689 /* 7690 * Let the world know that we're done. 7691 */ 7692 mutex_enter(&spa->spa_async_lock); 7693 spa->spa_async_thread = NULL; 7694 cv_broadcast(&spa->spa_async_cv); 7695 mutex_exit(&spa->spa_async_lock); 7696 thread_exit(); 7697 } 7698 7699 void 7700 spa_async_suspend(spa_t *spa) 7701 { 7702 mutex_enter(&spa->spa_async_lock); 7703 spa->spa_async_suspended++; 7704 while (spa->spa_async_thread != NULL) 7705 cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); 7706 mutex_exit(&spa->spa_async_lock); 7707 7708 spa_vdev_remove_suspend(spa); 7709 7710 zthr_t *condense_thread = spa->spa_condense_zthr; 7711 if (condense_thread != NULL) 7712 zthr_cancel(condense_thread); 7713 7714 zthr_t *discard_thread = spa->spa_checkpoint_discard_zthr; 7715 if (discard_thread != NULL) 7716 zthr_cancel(discard_thread); 7717 } 7718 7719 void 7720 spa_async_resume(spa_t *spa) 7721 { 7722 mutex_enter(&spa->spa_async_lock); 7723 ASSERT(spa->spa_async_suspended != 0); 7724 spa->spa_async_suspended--; 7725 mutex_exit(&spa->spa_async_lock); 7726 spa_restart_removal(spa); 7727 7728 zthr_t *condense_thread = spa->spa_condense_zthr; 7729 if (condense_thread != NULL) 7730 zthr_resume(condense_thread); 7731 7732 zthr_t *discard_thread = spa->spa_checkpoint_discard_zthr; 7733 if (discard_thread != NULL) 7734 zthr_resume(discard_thread); 7735 } 7736 7737 static boolean_t 7738 spa_async_tasks_pending(spa_t *spa) 7739 { 7740 uint_t non_config_tasks; 7741 uint_t config_task; 7742 boolean_t config_task_suspended; 7743 7744 non_config_tasks = spa->spa_async_tasks & ~SPA_ASYNC_CONFIG_UPDATE; 7745 config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE; 7746 if (spa->spa_ccw_fail_time == 0) { 7747 config_task_suspended = B_FALSE; 7748 } else { 7749 config_task_suspended = 7750 (gethrtime() - spa->spa_ccw_fail_time) < 7751 (zfs_ccw_retry_interval * NANOSEC); 7752 } 7753 7754 return (non_config_tasks || (config_task && !config_task_suspended)); 7755 } 7756 7757 static void 7758 spa_async_dispatch(spa_t *spa) 7759 { 7760 mutex_enter(&spa->spa_async_lock); 7761 if (spa_async_tasks_pending(spa) && 7762 !spa->spa_async_suspended && 7763 spa->spa_async_thread == NULL && 7764 rootdir != NULL) 7765 spa->spa_async_thread = thread_create(NULL, 0, 7766 spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); 7767 mutex_exit(&spa->spa_async_lock); 7768 } 7769 7770 void 7771 spa_async_request(spa_t *spa, int task) 7772 { 7773 zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task); 7774 mutex_enter(&spa->spa_async_lock); 7775 spa->spa_async_tasks |= task; 7776 mutex_exit(&spa->spa_async_lock); 7777 } 7778 7779 int 7780 spa_async_tasks(spa_t *spa) 7781 { 7782 return (spa->spa_async_tasks); 7783 } 7784 7785 /* 7786 * ========================================================================== 7787 * SPA syncing routines 7788 * ========================================================================== 7789 */ 7790 7791 static int 7792 bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 7793 { 7794 bpobj_t *bpo = arg; 7795 bpobj_enqueue(bpo, bp, tx); 7796 return (0); 7797 } 7798 7799 static int 7800 spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 7801 { 7802 zio_t *zio = arg; 7803 7804 zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp, 7805 zio->io_flags)); 7806 return (0); 7807 } 7808 7809 /* 7810 * Note: this simple function is not inlined to make it easier to dtrace the 7811 * amount of time spent syncing frees. 7812 */ 7813 static void 7814 spa_sync_frees(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx) 7815 { 7816 zio_t *zio = zio_root(spa, NULL, NULL, 0); 7817 bplist_iterate(bpl, spa_free_sync_cb, zio, tx); 7818 VERIFY(zio_wait(zio) == 0); 7819 } 7820 7821 /* 7822 * Note: this simple function is not inlined to make it easier to dtrace the 7823 * amount of time spent syncing deferred frees. 7824 */ 7825 static void 7826 spa_sync_deferred_frees(spa_t *spa, dmu_tx_t *tx) 7827 { 7828 if (spa_sync_pass(spa) != 1) 7829 return; 7830 7831 /* 7832 * Note: 7833 * If the log space map feature is active, we stop deferring 7834 * frees to the next TXG and therefore running this function 7835 * would be considered a no-op as spa_deferred_bpobj should 7836 * not have any entries. 7837 * 7838 * That said we run this function anyway (instead of returning 7839 * immediately) for the edge-case scenario where we just 7840 * activated the log space map feature in this TXG but we have 7841 * deferred frees from the previous TXG. 7842 */ 7843 zio_t *zio = zio_root(spa, NULL, NULL, 0); 7844 VERIFY3U(bpobj_iterate(&spa->spa_deferred_bpobj, 7845 spa_free_sync_cb, zio, tx), ==, 0); 7846 VERIFY0(zio_wait(zio)); 7847 } 7848 7849 7850 static void 7851 spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) 7852 { 7853 char *packed = NULL; 7854 size_t bufsize; 7855 size_t nvsize = 0; 7856 dmu_buf_t *db; 7857 7858 VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); 7859 7860 /* 7861 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration 7862 * information. This avoids the dmu_buf_will_dirty() path and 7863 * saves us a pre-read to get data we don't actually care about. 7864 */ 7865 bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE); 7866 packed = kmem_alloc(bufsize, KM_SLEEP); 7867 7868 VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, 7869 KM_SLEEP) == 0); 7870 bzero(packed + nvsize, bufsize - nvsize); 7871 7872 dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); 7873 7874 kmem_free(packed, bufsize); 7875 7876 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); 7877 dmu_buf_will_dirty(db, tx); 7878 *(uint64_t *)db->db_data = nvsize; 7879 dmu_buf_rele(db, FTAG); 7880 } 7881 7882 static void 7883 spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, 7884 const char *config, const char *entry) 7885 { 7886 nvlist_t *nvroot; 7887 nvlist_t **list; 7888 int i; 7889 7890 if (!sav->sav_sync) 7891 return; 7892 7893 /* 7894 * Update the MOS nvlist describing the list of available devices. 7895 * spa_validate_aux() will have already made sure this nvlist is 7896 * valid and the vdevs are labeled appropriately. 7897 */ 7898 if (sav->sav_object == 0) { 7899 sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, 7900 DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, 7901 sizeof (uint64_t), tx); 7902 VERIFY(zap_update(spa->spa_meta_objset, 7903 DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, 7904 &sav->sav_object, tx) == 0); 7905 } 7906 7907 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); 7908 if (sav->sav_count == 0) { 7909 VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); 7910 } else { 7911 list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP); 7912 for (i = 0; i < sav->sav_count; i++) 7913 list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], 7914 B_FALSE, VDEV_CONFIG_L2CACHE); 7915 VERIFY(nvlist_add_nvlist_array(nvroot, config, list, 7916 sav->sav_count) == 0); 7917 for (i = 0; i < sav->sav_count; i++) 7918 nvlist_free(list[i]); 7919 kmem_free(list, sav->sav_count * sizeof (void *)); 7920 } 7921 7922 spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); 7923 nvlist_free(nvroot); 7924 7925 sav->sav_sync = B_FALSE; 7926 } 7927 7928 /* 7929 * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t. 7930 * The all-vdev ZAP must be empty. 7931 */ 7932 static void 7933 spa_avz_build(vdev_t *vd, uint64_t avz, dmu_tx_t *tx) 7934 { 7935 spa_t *spa = vd->vdev_spa; 7936 if (vd->vdev_top_zap != 0) { 7937 VERIFY0(zap_add_int(spa->spa_meta_objset, avz, 7938 vd->vdev_top_zap, tx)); 7939 } 7940 if (vd->vdev_leaf_zap != 0) { 7941 VERIFY0(zap_add_int(spa->spa_meta_objset, avz, 7942 vd->vdev_leaf_zap, tx)); 7943 } 7944 for (uint64_t i = 0; i < vd->vdev_children; i++) { 7945 spa_avz_build(vd->vdev_child[i], avz, tx); 7946 } 7947 } 7948 7949 static void 7950 spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) 7951 { 7952 nvlist_t *config; 7953 7954 /* 7955 * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS, 7956 * its config may not be dirty but we still need to build per-vdev ZAPs. 7957 * Similarly, if the pool is being assembled (e.g. after a split), we 7958 * need to rebuild the AVZ although the config may not be dirty. 7959 */ 7960 if (list_is_empty(&spa->spa_config_dirty_list) && 7961 spa->spa_avz_action == AVZ_ACTION_NONE) 7962 return; 7963 7964 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 7965 7966 ASSERT(spa->spa_avz_action == AVZ_ACTION_NONE || 7967 spa->spa_avz_action == AVZ_ACTION_INITIALIZE || 7968 spa->spa_all_vdev_zaps != 0); 7969 7970 if (spa->spa_avz_action == AVZ_ACTION_REBUILD) { 7971 /* Make and build the new AVZ */ 7972 uint64_t new_avz = zap_create(spa->spa_meta_objset, 7973 DMU_OTN_ZAP_METADATA, DMU_OT_NONE, 0, tx); 7974 spa_avz_build(spa->spa_root_vdev, new_avz, tx); 7975 7976 /* Diff old AVZ with new one */ 7977 zap_cursor_t zc; 7978 zap_attribute_t za; 7979 7980 for (zap_cursor_init(&zc, spa->spa_meta_objset, 7981 spa->spa_all_vdev_zaps); 7982 zap_cursor_retrieve(&zc, &za) == 0; 7983 zap_cursor_advance(&zc)) { 7984 uint64_t vdzap = za.za_first_integer; 7985 if (zap_lookup_int(spa->spa_meta_objset, new_avz, 7986 vdzap) == ENOENT) { 7987 /* 7988 * ZAP is listed in old AVZ but not in new one; 7989 * destroy it 7990 */ 7991 VERIFY0(zap_destroy(spa->spa_meta_objset, vdzap, 7992 tx)); 7993 } 7994 } 7995 7996 zap_cursor_fini(&zc); 7997 7998 /* Destroy the old AVZ */ 7999 VERIFY0(zap_destroy(spa->spa_meta_objset, 8000 spa->spa_all_vdev_zaps, tx)); 8001 8002 /* Replace the old AVZ in the dir obj with the new one */ 8003 VERIFY0(zap_update(spa->spa_meta_objset, 8004 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, 8005 sizeof (new_avz), 1, &new_avz, tx)); 8006 8007 spa->spa_all_vdev_zaps = new_avz; 8008 } else if (spa->spa_avz_action == AVZ_ACTION_DESTROY) { 8009 zap_cursor_t zc; 8010 zap_attribute_t za; 8011 8012 /* Walk through the AVZ and destroy all listed ZAPs */ 8013 for (zap_cursor_init(&zc, spa->spa_meta_objset, 8014 spa->spa_all_vdev_zaps); 8015 zap_cursor_retrieve(&zc, &za) == 0; 8016 zap_cursor_advance(&zc)) { 8017 uint64_t zap = za.za_first_integer; 8018 VERIFY0(zap_destroy(spa->spa_meta_objset, zap, tx)); 8019 } 8020 8021 zap_cursor_fini(&zc); 8022 8023 /* Destroy and unlink the AVZ itself */ 8024 VERIFY0(zap_destroy(spa->spa_meta_objset, 8025 spa->spa_all_vdev_zaps, tx)); 8026 VERIFY0(zap_remove(spa->spa_meta_objset, 8027 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, tx)); 8028 spa->spa_all_vdev_zaps = 0; 8029 } 8030 8031 if (spa->spa_all_vdev_zaps == 0) { 8032 spa->spa_all_vdev_zaps = zap_create_link(spa->spa_meta_objset, 8033 DMU_OTN_ZAP_METADATA, DMU_POOL_DIRECTORY_OBJECT, 8034 DMU_POOL_VDEV_ZAP_MAP, tx); 8035 } 8036 spa->spa_avz_action = AVZ_ACTION_NONE; 8037 8038 /* Create ZAPs for vdevs that don't have them. */ 8039 vdev_construct_zaps(spa->spa_root_vdev, tx); 8040 8041 config = spa_config_generate(spa, spa->spa_root_vdev, 8042 dmu_tx_get_txg(tx), B_FALSE); 8043 8044 /* 8045 * If we're upgrading the spa version then make sure that 8046 * the config object gets updated with the correct version. 8047 */ 8048 if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version) 8049 fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, 8050 spa->spa_uberblock.ub_version); 8051 8052 spa_config_exit(spa, SCL_STATE, FTAG); 8053 8054 nvlist_free(spa->spa_config_syncing); 8055 spa->spa_config_syncing = config; 8056 8057 spa_sync_nvlist(spa, spa->spa_config_object, config, tx); 8058 } 8059 8060 static void 8061 spa_sync_version(void *arg, dmu_tx_t *tx) 8062 { 8063 uint64_t *versionp = arg; 8064 uint64_t version = *versionp; 8065 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 8066 8067 /* 8068 * Setting the version is special cased when first creating the pool. 8069 */ 8070 ASSERT(tx->tx_txg != TXG_INITIAL); 8071 8072 ASSERT(SPA_VERSION_IS_SUPPORTED(version)); 8073 ASSERT(version >= spa_version(spa)); 8074 8075 spa->spa_uberblock.ub_version = version; 8076 vdev_config_dirty(spa->spa_root_vdev); 8077 spa_history_log_internal(spa, "set", tx, "version=%lld", version); 8078 } 8079 8080 /* 8081 * Set zpool properties. 8082 */ 8083 static void 8084 spa_sync_props(void *arg, dmu_tx_t *tx) 8085 { 8086 nvlist_t *nvp = arg; 8087 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 8088 objset_t *mos = spa->spa_meta_objset; 8089 nvpair_t *elem = NULL; 8090 8091 mutex_enter(&spa->spa_props_lock); 8092 8093 while ((elem = nvlist_next_nvpair(nvp, elem))) { 8094 uint64_t intval; 8095 char *strval, *fname; 8096 zpool_prop_t prop; 8097 const char *propname; 8098 zprop_type_t proptype; 8099 spa_feature_t fid; 8100 8101 switch (prop = zpool_name_to_prop(nvpair_name(elem))) { 8102 case ZPOOL_PROP_INVAL: 8103 /* 8104 * We checked this earlier in spa_prop_validate(). 8105 */ 8106 ASSERT(zpool_prop_feature(nvpair_name(elem))); 8107 8108 fname = strchr(nvpair_name(elem), '@') + 1; 8109 VERIFY0(zfeature_lookup_name(fname, &fid)); 8110 8111 spa_feature_enable(spa, fid, tx); 8112 spa_history_log_internal(spa, "set", tx, 8113 "%s=enabled", nvpair_name(elem)); 8114 break; 8115 8116 case ZPOOL_PROP_VERSION: 8117 intval = fnvpair_value_uint64(elem); 8118 /* 8119 * The version is synced seperatly before other 8120 * properties and should be correct by now. 8121 */ 8122 ASSERT3U(spa_version(spa), >=, intval); 8123 break; 8124 8125 case ZPOOL_PROP_ALTROOT: 8126 /* 8127 * 'altroot' is a non-persistent property. It should 8128 * have been set temporarily at creation or import time. 8129 */ 8130 ASSERT(spa->spa_root != NULL); 8131 break; 8132 8133 case ZPOOL_PROP_READONLY: 8134 case ZPOOL_PROP_CACHEFILE: 8135 /* 8136 * 'readonly' and 'cachefile' are also non-persisitent 8137 * properties. 8138 */ 8139 break; 8140 case ZPOOL_PROP_COMMENT: 8141 strval = fnvpair_value_string(elem); 8142 if (spa->spa_comment != NULL) 8143 spa_strfree(spa->spa_comment); 8144 spa->spa_comment = spa_strdup(strval); 8145 /* 8146 * We need to dirty the configuration on all the vdevs 8147 * so that their labels get updated. It's unnecessary 8148 * to do this for pool creation since the vdev's 8149 * configuratoin has already been dirtied. 8150 */ 8151 if (tx->tx_txg != TXG_INITIAL) 8152 vdev_config_dirty(spa->spa_root_vdev); 8153 spa_history_log_internal(spa, "set", tx, 8154 "%s=%s", nvpair_name(elem), strval); 8155 break; 8156 default: 8157 /* 8158 * Set pool property values in the poolprops mos object. 8159 */ 8160 if (spa->spa_pool_props_object == 0) { 8161 spa->spa_pool_props_object = 8162 zap_create_link(mos, DMU_OT_POOL_PROPS, 8163 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, 8164 tx); 8165 } 8166 8167 /* normalize the property name */ 8168 propname = zpool_prop_to_name(prop); 8169 proptype = zpool_prop_get_type(prop); 8170 8171 if (nvpair_type(elem) == DATA_TYPE_STRING) { 8172 ASSERT(proptype == PROP_TYPE_STRING); 8173 strval = fnvpair_value_string(elem); 8174 VERIFY0(zap_update(mos, 8175 spa->spa_pool_props_object, propname, 8176 1, strlen(strval) + 1, strval, tx)); 8177 spa_history_log_internal(spa, "set", tx, 8178 "%s=%s", nvpair_name(elem), strval); 8179 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { 8180 intval = fnvpair_value_uint64(elem); 8181 8182 if (proptype == PROP_TYPE_INDEX) { 8183 const char *unused; 8184 VERIFY0(zpool_prop_index_to_string( 8185 prop, intval, &unused)); 8186 } 8187 VERIFY0(zap_update(mos, 8188 spa->spa_pool_props_object, propname, 8189 8, 1, &intval, tx)); 8190 spa_history_log_internal(spa, "set", tx, 8191 "%s=%lld", nvpair_name(elem), intval); 8192 } else { 8193 ASSERT(0); /* not allowed */ 8194 } 8195 8196 switch (prop) { 8197 case ZPOOL_PROP_DELEGATION: 8198 spa->spa_delegation = intval; 8199 break; 8200 case ZPOOL_PROP_BOOTFS: 8201 spa->spa_bootfs = intval; 8202 break; 8203 case ZPOOL_PROP_FAILUREMODE: 8204 spa->spa_failmode = intval; 8205 break; 8206 case ZPOOL_PROP_AUTOTRIM: 8207 spa->spa_autotrim = intval; 8208 spa_async_request(spa, 8209 SPA_ASYNC_AUTOTRIM_RESTART); 8210 break; 8211 case ZPOOL_PROP_AUTOEXPAND: 8212 spa->spa_autoexpand = intval; 8213 if (tx->tx_txg != TXG_INITIAL) 8214 spa_async_request(spa, 8215 SPA_ASYNC_AUTOEXPAND); 8216 break; 8217 case ZPOOL_PROP_MULTIHOST: 8218 spa->spa_multihost = intval; 8219 break; 8220 case ZPOOL_PROP_DEDUPDITTO: 8221 spa->spa_dedup_ditto = intval; 8222 break; 8223 default: 8224 break; 8225 } 8226 } 8227 8228 } 8229 8230 mutex_exit(&spa->spa_props_lock); 8231 } 8232 8233 /* 8234 * Perform one-time upgrade on-disk changes. spa_version() does not 8235 * reflect the new version this txg, so there must be no changes this 8236 * txg to anything that the upgrade code depends on after it executes. 8237 * Therefore this must be called after dsl_pool_sync() does the sync 8238 * tasks. 8239 */ 8240 static void 8241 spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx) 8242 { 8243 if (spa_sync_pass(spa) != 1) 8244 return; 8245 8246 dsl_pool_t *dp = spa->spa_dsl_pool; 8247 rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG); 8248 8249 if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && 8250 spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { 8251 dsl_pool_create_origin(dp, tx); 8252 8253 /* Keeping the origin open increases spa_minref */ 8254 spa->spa_minref += 3; 8255 } 8256 8257 if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && 8258 spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { 8259 dsl_pool_upgrade_clones(dp, tx); 8260 } 8261 8262 if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES && 8263 spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) { 8264 dsl_pool_upgrade_dir_clones(dp, tx); 8265 8266 /* Keeping the freedir open increases spa_minref */ 8267 spa->spa_minref += 3; 8268 } 8269 8270 if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES && 8271 spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { 8272 spa_feature_create_zap_objects(spa, tx); 8273 } 8274 8275 /* 8276 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable 8277 * when possibility to use lz4 compression for metadata was added 8278 * Old pools that have this feature enabled must be upgraded to have 8279 * this feature active 8280 */ 8281 if (spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { 8282 boolean_t lz4_en = spa_feature_is_enabled(spa, 8283 SPA_FEATURE_LZ4_COMPRESS); 8284 boolean_t lz4_ac = spa_feature_is_active(spa, 8285 SPA_FEATURE_LZ4_COMPRESS); 8286 8287 if (lz4_en && !lz4_ac) 8288 spa_feature_incr(spa, SPA_FEATURE_LZ4_COMPRESS, tx); 8289 } 8290 8291 /* 8292 * If we haven't written the salt, do so now. Note that the 8293 * feature may not be activated yet, but that's fine since 8294 * the presence of this ZAP entry is backwards compatible. 8295 */ 8296 if (zap_contains(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 8297 DMU_POOL_CHECKSUM_SALT) == ENOENT) { 8298 VERIFY0(zap_add(spa->spa_meta_objset, 8299 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CHECKSUM_SALT, 1, 8300 sizeof (spa->spa_cksum_salt.zcs_bytes), 8301 spa->spa_cksum_salt.zcs_bytes, tx)); 8302 } 8303 8304 rrw_exit(&dp->dp_config_rwlock, FTAG); 8305 } 8306 8307 static void 8308 vdev_indirect_state_sync_verify(vdev_t *vd) 8309 { 8310 vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping; 8311 vdev_indirect_births_t *vib = vd->vdev_indirect_births; 8312 8313 if (vd->vdev_ops == &vdev_indirect_ops) { 8314 ASSERT(vim != NULL); 8315 ASSERT(vib != NULL); 8316 } 8317 8318 if (vdev_obsolete_sm_object(vd) != 0) { 8319 ASSERT(vd->vdev_obsolete_sm != NULL); 8320 ASSERT(vd->vdev_removing || 8321 vd->vdev_ops == &vdev_indirect_ops); 8322 ASSERT(vdev_indirect_mapping_num_entries(vim) > 0); 8323 ASSERT(vdev_indirect_mapping_bytes_mapped(vim) > 0); 8324 8325 ASSERT3U(vdev_obsolete_sm_object(vd), ==, 8326 space_map_object(vd->vdev_obsolete_sm)); 8327 ASSERT3U(vdev_indirect_mapping_bytes_mapped(vim), >=, 8328 space_map_allocated(vd->vdev_obsolete_sm)); 8329 } 8330 ASSERT(vd->vdev_obsolete_segments != NULL); 8331 8332 /* 8333 * Since frees / remaps to an indirect vdev can only 8334 * happen in syncing context, the obsolete segments 8335 * tree must be empty when we start syncing. 8336 */ 8337 ASSERT0(range_tree_space(vd->vdev_obsolete_segments)); 8338 } 8339 8340 /* 8341 * Set the top-level vdev's max queue depth. Evaluate each top-level's 8342 * async write queue depth in case it changed. The max queue depth will 8343 * not change in the middle of syncing out this txg. 8344 */ 8345 static void 8346 spa_sync_adjust_vdev_max_queue_depth(spa_t *spa) 8347 { 8348 ASSERT(spa_writeable(spa)); 8349 8350 vdev_t *rvd = spa->spa_root_vdev; 8351 uint32_t max_queue_depth = zfs_vdev_async_write_max_active * 8352 zfs_vdev_queue_depth_pct / 100; 8353 metaslab_class_t *normal = spa_normal_class(spa); 8354 metaslab_class_t *special = spa_special_class(spa); 8355 metaslab_class_t *dedup = spa_dedup_class(spa); 8356 8357 uint64_t slots_per_allocator = 0; 8358 for (int c = 0; c < rvd->vdev_children; c++) { 8359 vdev_t *tvd = rvd->vdev_child[c]; 8360 8361 metaslab_group_t *mg = tvd->vdev_mg; 8362 if (mg == NULL || !metaslab_group_initialized(mg)) 8363 continue; 8364 8365 metaslab_class_t *mc = mg->mg_class; 8366 if (mc != normal && mc != special && mc != dedup) 8367 continue; 8368 8369 /* 8370 * It is safe to do a lock-free check here because only async 8371 * allocations look at mg_max_alloc_queue_depth, and async 8372 * allocations all happen from spa_sync(). 8373 */ 8374 for (int i = 0; i < spa->spa_alloc_count; i++) 8375 ASSERT0(zfs_refcount_count( 8376 &(mg->mg_alloc_queue_depth[i]))); 8377 mg->mg_max_alloc_queue_depth = max_queue_depth; 8378 8379 for (int i = 0; i < spa->spa_alloc_count; i++) { 8380 mg->mg_cur_max_alloc_queue_depth[i] = 8381 zfs_vdev_def_queue_depth; 8382 } 8383 slots_per_allocator += zfs_vdev_def_queue_depth; 8384 } 8385 8386 for (int i = 0; i < spa->spa_alloc_count; i++) { 8387 ASSERT0(zfs_refcount_count(&normal->mc_alloc_slots[i])); 8388 ASSERT0(zfs_refcount_count(&special->mc_alloc_slots[i])); 8389 ASSERT0(zfs_refcount_count(&dedup->mc_alloc_slots[i])); 8390 normal->mc_alloc_max_slots[i] = slots_per_allocator; 8391 special->mc_alloc_max_slots[i] = slots_per_allocator; 8392 dedup->mc_alloc_max_slots[i] = slots_per_allocator; 8393 } 8394 normal->mc_alloc_throttle_enabled = zio_dva_throttle_enabled; 8395 special->mc_alloc_throttle_enabled = zio_dva_throttle_enabled; 8396 dedup->mc_alloc_throttle_enabled = zio_dva_throttle_enabled; 8397 } 8398 8399 static void 8400 spa_sync_condense_indirect(spa_t *spa, dmu_tx_t *tx) 8401 { 8402 ASSERT(spa_writeable(spa)); 8403 8404 vdev_t *rvd = spa->spa_root_vdev; 8405 for (int c = 0; c < rvd->vdev_children; c++) { 8406 vdev_t *vd = rvd->vdev_child[c]; 8407 vdev_indirect_state_sync_verify(vd); 8408 8409 if (vdev_indirect_should_condense(vd)) { 8410 spa_condense_indirect_start_sync(vd, tx); 8411 break; 8412 } 8413 } 8414 } 8415 8416 static void 8417 spa_sync_iterate_to_convergence(spa_t *spa, dmu_tx_t *tx) 8418 { 8419 objset_t *mos = spa->spa_meta_objset; 8420 dsl_pool_t *dp = spa->spa_dsl_pool; 8421 uint64_t txg = tx->tx_txg; 8422 bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK]; 8423 8424 do { 8425 int pass = ++spa->spa_sync_pass; 8426 8427 spa_sync_config_object(spa, tx); 8428 spa_sync_aux_dev(spa, &spa->spa_spares, tx, 8429 ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); 8430 spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, 8431 ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); 8432 spa_errlog_sync(spa, txg); 8433 dsl_pool_sync(dp, txg); 8434 8435 if (pass < zfs_sync_pass_deferred_free || 8436 spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP)) { 8437 /* 8438 * If the log space map feature is active we don't 8439 * care about deferred frees and the deferred bpobj 8440 * as the log space map should effectively have the 8441 * same results (i.e. appending only to one object). 8442 */ 8443 spa_sync_frees(spa, free_bpl, tx); 8444 } else { 8445 /* 8446 * We can not defer frees in pass 1, because 8447 * we sync the deferred frees later in pass 1. 8448 */ 8449 ASSERT3U(pass, >, 1); 8450 bplist_iterate(free_bpl, bpobj_enqueue_cb, 8451 &spa->spa_deferred_bpobj, tx); 8452 } 8453 8454 ddt_sync(spa, txg); 8455 dsl_scan_sync(dp, tx); 8456 svr_sync(spa, tx); 8457 spa_sync_upgrades(spa, tx); 8458 8459 spa_flush_metaslabs(spa, tx); 8460 8461 vdev_t *vd = NULL; 8462 while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) 8463 != NULL) 8464 vdev_sync(vd, txg); 8465 8466 /* 8467 * Note: We need to check if the MOS is dirty because we could 8468 * have marked the MOS dirty without updating the uberblock 8469 * (e.g. if we have sync tasks but no dirty user data). We need 8470 * to check the uberblock's rootbp because it is updated if we 8471 * have synced out dirty data (though in this case the MOS will 8472 * most likely also be dirty due to second order effects, we 8473 * don't want to rely on that here). 8474 */ 8475 if (pass == 1 && 8476 spa->spa_uberblock.ub_rootbp.blk_birth < txg && 8477 !dmu_objset_is_dirty(mos, txg)) { 8478 /* 8479 * Nothing changed on the first pass, therefore this 8480 * TXG is a no-op. Avoid syncing deferred frees, so 8481 * that we can keep this TXG as a no-op. 8482 */ 8483 ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 8484 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 8485 ASSERT(txg_list_empty(&dp->dp_sync_tasks, txg)); 8486 ASSERT(txg_list_empty(&dp->dp_early_sync_tasks, txg)); 8487 break; 8488 } 8489 8490 spa_sync_deferred_frees(spa, tx); 8491 } while (dmu_objset_is_dirty(mos, txg)); 8492 } 8493 8494 /* 8495 * Rewrite the vdev configuration (which includes the uberblock) to 8496 * commit the transaction group. 8497 * 8498 * If there are no dirty vdevs, we sync the uberblock to a few random 8499 * top-level vdevs that are known to be visible in the config cache 8500 * (see spa_vdev_add() for a complete description). If there *are* dirty 8501 * vdevs, sync the uberblock to all vdevs. 8502 */ 8503 static void 8504 spa_sync_rewrite_vdev_config(spa_t *spa, dmu_tx_t *tx) 8505 { 8506 vdev_t *rvd = spa->spa_root_vdev; 8507 uint64_t txg = tx->tx_txg; 8508 8509 for (;;) { 8510 int error = 0; 8511 8512 /* 8513 * We hold SCL_STATE to prevent vdev open/close/etc. 8514 * while we're attempting to write the vdev labels. 8515 */ 8516 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 8517 8518 if (list_is_empty(&spa->spa_config_dirty_list)) { 8519 vdev_t *svd[SPA_SYNC_MIN_VDEVS] = { NULL }; 8520 int svdcount = 0; 8521 int children = rvd->vdev_children; 8522 int c0 = spa_get_random(children); 8523 8524 for (int c = 0; c < children; c++) { 8525 vdev_t *vd = 8526 rvd->vdev_child[(c0 + c) % children]; 8527 8528 /* Stop when revisiting the first vdev */ 8529 if (c > 0 && svd[0] == vd) 8530 break; 8531 8532 if (vd->vdev_ms_array == 0 || 8533 vd->vdev_islog || 8534 !vdev_is_concrete(vd)) 8535 continue; 8536 8537 svd[svdcount++] = vd; 8538 if (svdcount == SPA_SYNC_MIN_VDEVS) 8539 break; 8540 } 8541 error = vdev_config_sync(svd, svdcount, txg); 8542 } else { 8543 error = vdev_config_sync(rvd->vdev_child, 8544 rvd->vdev_children, txg); 8545 } 8546 8547 if (error == 0) 8548 spa->spa_last_synced_guid = rvd->vdev_guid; 8549 8550 spa_config_exit(spa, SCL_STATE, FTAG); 8551 8552 if (error == 0) 8553 break; 8554 zio_suspend(spa, NULL, ZIO_SUSPEND_IOERR); 8555 zio_resume_wait(spa); 8556 } 8557 } 8558 8559 /* 8560 * Sync the specified transaction group. New blocks may be dirtied as 8561 * part of the process, so we iterate until it converges. 8562 */ 8563 void 8564 spa_sync(spa_t *spa, uint64_t txg) 8565 { 8566 vdev_t *vd = NULL; 8567 8568 VERIFY(spa_writeable(spa)); 8569 8570 /* 8571 * Wait for i/os issued in open context that need to complete 8572 * before this txg syncs. 8573 */ 8574 (void) zio_wait(spa->spa_txg_zio[txg & TXG_MASK]); 8575 spa->spa_txg_zio[txg & TXG_MASK] = zio_root(spa, NULL, NULL, 8576 ZIO_FLAG_CANFAIL); 8577 8578 /* 8579 * Lock out configuration changes. 8580 */ 8581 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 8582 8583 spa->spa_syncing_txg = txg; 8584 spa->spa_sync_pass = 0; 8585 8586 for (int i = 0; i < spa->spa_alloc_count; i++) { 8587 mutex_enter(&spa->spa_alloc_locks[i]); 8588 VERIFY0(avl_numnodes(&spa->spa_alloc_trees[i])); 8589 mutex_exit(&spa->spa_alloc_locks[i]); 8590 } 8591 8592 /* 8593 * If there are any pending vdev state changes, convert them 8594 * into config changes that go out with this transaction group. 8595 */ 8596 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 8597 while (list_head(&spa->spa_state_dirty_list) != NULL) { 8598 /* 8599 * We need the write lock here because, for aux vdevs, 8600 * calling vdev_config_dirty() modifies sav_config. 8601 * This is ugly and will become unnecessary when we 8602 * eliminate the aux vdev wart by integrating all vdevs 8603 * into the root vdev tree. 8604 */ 8605 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 8606 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER); 8607 while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { 8608 vdev_state_clean(vd); 8609 vdev_config_dirty(vd); 8610 } 8611 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 8612 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); 8613 } 8614 spa_config_exit(spa, SCL_STATE, FTAG); 8615 8616 dsl_pool_t *dp = spa->spa_dsl_pool; 8617 dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg); 8618 8619 spa->spa_sync_starttime = gethrtime(); 8620 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, 8621 spa->spa_sync_starttime + spa->spa_deadman_synctime)); 8622 8623 /* 8624 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, 8625 * set spa_deflate if we have no raid-z vdevs. 8626 */ 8627 if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && 8628 spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { 8629 vdev_t *rvd = spa->spa_root_vdev; 8630 8631 int i; 8632 for (i = 0; i < rvd->vdev_children; i++) { 8633 vd = rvd->vdev_child[i]; 8634 if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) 8635 break; 8636 } 8637 if (i == rvd->vdev_children) { 8638 spa->spa_deflate = TRUE; 8639 VERIFY0(zap_add(spa->spa_meta_objset, 8640 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, 8641 sizeof (uint64_t), 1, &spa->spa_deflate, tx)); 8642 } 8643 } 8644 8645 spa_sync_adjust_vdev_max_queue_depth(spa); 8646 8647 spa_sync_condense_indirect(spa, tx); 8648 8649 spa_sync_iterate_to_convergence(spa, tx); 8650 8651 #ifdef ZFS_DEBUG 8652 if (!list_is_empty(&spa->spa_config_dirty_list)) { 8653 /* 8654 * Make sure that the number of ZAPs for all the vdevs matches 8655 * the number of ZAPs in the per-vdev ZAP list. This only gets 8656 * called if the config is dirty; otherwise there may be 8657 * outstanding AVZ operations that weren't completed in 8658 * spa_sync_config_object. 8659 */ 8660 uint64_t all_vdev_zap_entry_count; 8661 ASSERT0(zap_count(spa->spa_meta_objset, 8662 spa->spa_all_vdev_zaps, &all_vdev_zap_entry_count)); 8663 ASSERT3U(vdev_count_verify_zaps(spa->spa_root_vdev), ==, 8664 all_vdev_zap_entry_count); 8665 } 8666 #endif 8667 8668 if (spa->spa_vdev_removal != NULL) { 8669 ASSERT0(spa->spa_vdev_removal->svr_bytes_done[txg & TXG_MASK]); 8670 } 8671 8672 spa_sync_rewrite_vdev_config(spa, tx); 8673 dmu_tx_commit(tx); 8674 8675 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY)); 8676 8677 /* 8678 * Clear the dirty config list. 8679 */ 8680 while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL) 8681 vdev_config_clean(vd); 8682 8683 /* 8684 * Now that the new config has synced transactionally, 8685 * let it become visible to the config cache. 8686 */ 8687 if (spa->spa_config_syncing != NULL) { 8688 spa_config_set(spa, spa->spa_config_syncing); 8689 spa->spa_config_txg = txg; 8690 spa->spa_config_syncing = NULL; 8691 } 8692 8693 dsl_pool_sync_done(dp, txg); 8694 8695 for (int i = 0; i < spa->spa_alloc_count; i++) { 8696 mutex_enter(&spa->spa_alloc_locks[i]); 8697 VERIFY0(avl_numnodes(&spa->spa_alloc_trees[i])); 8698 mutex_exit(&spa->spa_alloc_locks[i]); 8699 } 8700 8701 /* 8702 * Update usable space statistics. 8703 */ 8704 while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) 8705 != NULL) 8706 vdev_sync_done(vd, txg); 8707 8708 metaslab_class_evict_old(spa->spa_normal_class, txg); 8709 metaslab_class_evict_old(spa->spa_log_class, txg); 8710 8711 spa_sync_close_syncing_log_sm(spa); 8712 8713 spa_update_dspace(spa); 8714 8715 /* 8716 * It had better be the case that we didn't dirty anything 8717 * since vdev_config_sync(). 8718 */ 8719 ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); 8720 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); 8721 ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); 8722 8723 while (zfs_pause_spa_sync) 8724 delay(1); 8725 8726 spa->spa_sync_pass = 0; 8727 8728 /* 8729 * Update the last synced uberblock here. We want to do this at 8730 * the end of spa_sync() so that consumers of spa_last_synced_txg() 8731 * will be guaranteed that all the processing associated with 8732 * that txg has been completed. 8733 */ 8734 spa->spa_ubsync = spa->spa_uberblock; 8735 spa_config_exit(spa, SCL_CONFIG, FTAG); 8736 8737 spa_handle_ignored_writes(spa); 8738 8739 /* Mark unused spares as needing a health check. */ 8740 if (spa_spare_poll_interval_seconds != 0 && 8741 NSEC2SEC(gethrtime() - spa->spa_spares_last_polled) > 8742 spa_spare_poll_interval_seconds) { 8743 spa_spare_poll(spa); 8744 spa->spa_spares_last_polled = gethrtime(); 8745 } 8746 8747 /* 8748 * If any async tasks have been requested, kick them off. 8749 */ 8750 spa_async_dispatch(spa); 8751 } 8752 8753 /* 8754 * Sync all pools. We don't want to hold the namespace lock across these 8755 * operations, so we take a reference on the spa_t and drop the lock during the 8756 * sync. 8757 */ 8758 void 8759 spa_sync_allpools(void) 8760 { 8761 spa_t *spa = NULL; 8762 mutex_enter(&spa_namespace_lock); 8763 while ((spa = spa_next(spa)) != NULL) { 8764 if (spa_state(spa) != POOL_STATE_ACTIVE || 8765 !spa_writeable(spa) || spa_suspended(spa)) 8766 continue; 8767 spa_open_ref(spa, FTAG); 8768 mutex_exit(&spa_namespace_lock); 8769 txg_wait_synced(spa_get_dsl(spa), 0); 8770 mutex_enter(&spa_namespace_lock); 8771 spa_close(spa, FTAG); 8772 } 8773 mutex_exit(&spa_namespace_lock); 8774 } 8775 8776 /* 8777 * ========================================================================== 8778 * Miscellaneous routines 8779 * ========================================================================== 8780 */ 8781 8782 /* 8783 * Remove all pools in the system. 8784 */ 8785 void 8786 spa_evict_all(void) 8787 { 8788 spa_t *spa; 8789 8790 /* 8791 * Remove all cached state. All pools should be closed now, 8792 * so every spa in the AVL tree should be unreferenced. 8793 */ 8794 mutex_enter(&spa_namespace_lock); 8795 while ((spa = spa_next(NULL)) != NULL) { 8796 /* 8797 * Stop async tasks. The async thread may need to detach 8798 * a device that's been replaced, which requires grabbing 8799 * spa_namespace_lock, so we must drop it here. 8800 */ 8801 spa_open_ref(spa, FTAG); 8802 mutex_exit(&spa_namespace_lock); 8803 spa_async_suspend(spa); 8804 mutex_enter(&spa_namespace_lock); 8805 spa_close(spa, FTAG); 8806 8807 if (spa->spa_state != POOL_STATE_UNINITIALIZED) { 8808 spa_unload(spa); 8809 spa_deactivate(spa); 8810 } 8811 spa_remove(spa); 8812 } 8813 mutex_exit(&spa_namespace_lock); 8814 } 8815 8816 vdev_t * 8817 spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux) 8818 { 8819 vdev_t *vd; 8820 int i; 8821 8822 if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) 8823 return (vd); 8824 8825 if (aux) { 8826 for (i = 0; i < spa->spa_l2cache.sav_count; i++) { 8827 vd = spa->spa_l2cache.sav_vdevs[i]; 8828 if (vd->vdev_guid == guid) 8829 return (vd); 8830 } 8831 8832 for (i = 0; i < spa->spa_spares.sav_count; i++) { 8833 vd = spa->spa_spares.sav_vdevs[i]; 8834 if (vd->vdev_guid == guid) 8835 return (vd); 8836 } 8837 } 8838 8839 return (NULL); 8840 } 8841 8842 void 8843 spa_upgrade(spa_t *spa, uint64_t version) 8844 { 8845 ASSERT(spa_writeable(spa)); 8846 8847 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 8848 8849 /* 8850 * This should only be called for a non-faulted pool, and since a 8851 * future version would result in an unopenable pool, this shouldn't be 8852 * possible. 8853 */ 8854 ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version)); 8855 ASSERT3U(version, >=, spa->spa_uberblock.ub_version); 8856 8857 spa->spa_uberblock.ub_version = version; 8858 vdev_config_dirty(spa->spa_root_vdev); 8859 8860 spa_config_exit(spa, SCL_ALL, FTAG); 8861 8862 txg_wait_synced(spa_get_dsl(spa), 0); 8863 } 8864 8865 boolean_t 8866 spa_has_spare(spa_t *spa, uint64_t guid) 8867 { 8868 int i; 8869 uint64_t spareguid; 8870 spa_aux_vdev_t *sav = &spa->spa_spares; 8871 8872 for (i = 0; i < sav->sav_count; i++) 8873 if (sav->sav_vdevs[i]->vdev_guid == guid) 8874 return (B_TRUE); 8875 8876 for (i = 0; i < sav->sav_npending; i++) { 8877 if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, 8878 &spareguid) == 0 && spareguid == guid) 8879 return (B_TRUE); 8880 } 8881 8882 return (B_FALSE); 8883 } 8884 8885 /* 8886 * Check if a pool has an active shared spare device. 8887 * Note: reference count of an active spare is 2, as a spare and as a replace 8888 */ 8889 static boolean_t 8890 spa_has_active_shared_spare(spa_t *spa) 8891 { 8892 int i, refcnt; 8893 uint64_t pool; 8894 spa_aux_vdev_t *sav = &spa->spa_spares; 8895 8896 for (i = 0; i < sav->sav_count; i++) { 8897 if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, 8898 &refcnt) && pool != 0ULL && pool == spa_guid(spa) && 8899 refcnt > 2) 8900 return (B_TRUE); 8901 } 8902 8903 return (B_FALSE); 8904 } 8905 8906 uint64_t 8907 spa_total_metaslabs(spa_t *spa) 8908 { 8909 vdev_t *rvd = spa->spa_root_vdev; 8910 uint64_t m = 0; 8911 8912 for (uint64_t c = 0; c < rvd->vdev_children; c++) { 8913 vdev_t *vd = rvd->vdev_child[c]; 8914 if (!vdev_is_concrete(vd)) 8915 continue; 8916 m += vd->vdev_ms_count; 8917 } 8918 return (m); 8919 } 8920 8921 sysevent_t * 8922 spa_event_create(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, const char *name) 8923 { 8924 sysevent_t *ev = NULL; 8925 #ifdef _KERNEL 8926 sysevent_attr_list_t *attr = NULL; 8927 sysevent_value_t value; 8928 8929 ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs", 8930 SE_SLEEP); 8931 ASSERT(ev != NULL); 8932 8933 value.value_type = SE_DATA_TYPE_STRING; 8934 value.value.sv_string = spa_name(spa); 8935 if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0) 8936 goto done; 8937 8938 value.value_type = SE_DATA_TYPE_UINT64; 8939 value.value.sv_uint64 = spa_guid(spa); 8940 if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0) 8941 goto done; 8942 8943 if (vd) { 8944 value.value_type = SE_DATA_TYPE_UINT64; 8945 value.value.sv_uint64 = vd->vdev_guid; 8946 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value, 8947 SE_SLEEP) != 0) 8948 goto done; 8949 8950 if (vd->vdev_path) { 8951 value.value_type = SE_DATA_TYPE_STRING; 8952 value.value.sv_string = vd->vdev_path; 8953 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH, 8954 &value, SE_SLEEP) != 0) 8955 goto done; 8956 } 8957 } 8958 8959 if (hist_nvl != NULL) { 8960 fnvlist_merge((nvlist_t *)attr, hist_nvl); 8961 } 8962 8963 if (sysevent_attach_attributes(ev, attr) != 0) 8964 goto done; 8965 attr = NULL; 8966 8967 done: 8968 if (attr) 8969 sysevent_free_attr(attr); 8970 8971 #endif 8972 return (ev); 8973 } 8974 8975 void 8976 spa_event_post(sysevent_t *ev) 8977 { 8978 #ifdef _KERNEL 8979 sysevent_id_t eid; 8980 8981 (void) log_sysevent(ev, SE_SLEEP, &eid); 8982 sysevent_free(ev); 8983 #endif 8984 } 8985 8986 void 8987 spa_event_discard(sysevent_t *ev) 8988 { 8989 #ifdef _KERNEL 8990 sysevent_free(ev); 8991 #endif 8992 } 8993 8994 /* 8995 * Post a sysevent corresponding to the given event. The 'name' must be one of 8996 * the event definitions in sys/sysevent/eventdefs.h. The payload will be 8997 * filled in from the spa and (optionally) the vdev and history nvl. This 8998 * doesn't do anything in the userland libzpool, as we don't want consumers to 8999 * misinterpret ztest or zdb as real changes. 9000 */ 9001 void 9002 spa_event_notify(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, const char *name) 9003 { 9004 spa_event_post(spa_event_create(spa, vd, hist_nvl, name)); 9005 } 9006