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