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