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