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