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