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