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