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