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