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