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