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