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