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