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