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