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