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