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