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