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