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