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