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