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