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