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