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