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