1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright (c) 2011, 2015 by Delphix. All rights reserved. 25 * Copyright 2017 Nexenta Systems, Inc. 26 * Copyright (c) 2014 Integros [integros.com] 27 * Copyright 2016 Toomas Soome <tsoome@me.com> 28 */ 29 30 #include <sys/zfs_context.h> 31 #include <sys/fm/fs/zfs.h> 32 #include <sys/spa.h> 33 #include <sys/spa_impl.h> 34 #include <sys/dmu.h> 35 #include <sys/dmu_tx.h> 36 #include <sys/vdev_impl.h> 37 #include <sys/uberblock_impl.h> 38 #include <sys/metaslab.h> 39 #include <sys/metaslab_impl.h> 40 #include <sys/space_map.h> 41 #include <sys/space_reftree.h> 42 #include <sys/zio.h> 43 #include <sys/zap.h> 44 #include <sys/fs/zfs.h> 45 #include <sys/arc.h> 46 #include <sys/zil.h> 47 #include <sys/dsl_scan.h> 48 49 /* 50 * Virtual device management. 51 */ 52 53 static vdev_ops_t *vdev_ops_table[] = { 54 &vdev_root_ops, 55 &vdev_raidz_ops, 56 &vdev_mirror_ops, 57 &vdev_replacing_ops, 58 &vdev_spare_ops, 59 &vdev_disk_ops, 60 &vdev_file_ops, 61 &vdev_missing_ops, 62 &vdev_hole_ops, 63 NULL 64 }; 65 66 /* maximum scrub/resilver I/O queue per leaf vdev */ 67 int zfs_scrub_limit = 10; 68 69 /* 70 * When a vdev is added, it will be divided into approximately (but no 71 * more than) this number of metaslabs. 72 */ 73 int metaslabs_per_vdev = 200; 74 75 /* 76 * Given a vdev type, return the appropriate ops vector. 77 */ 78 static vdev_ops_t * 79 vdev_getops(const char *type) 80 { 81 vdev_ops_t *ops, **opspp; 82 83 for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++) 84 if (strcmp(ops->vdev_op_type, type) == 0) 85 break; 86 87 return (ops); 88 } 89 90 /* 91 * Default asize function: return the MAX of psize with the asize of 92 * all children. This is what's used by anything other than RAID-Z. 93 */ 94 uint64_t 95 vdev_default_asize(vdev_t *vd, uint64_t psize) 96 { 97 uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift); 98 uint64_t csize; 99 100 for (int c = 0; c < vd->vdev_children; c++) { 101 csize = vdev_psize_to_asize(vd->vdev_child[c], psize); 102 asize = MAX(asize, csize); 103 } 104 105 return (asize); 106 } 107 108 /* 109 * Get the minimum allocatable size. We define the allocatable size as 110 * the vdev's asize rounded to the nearest metaslab. This allows us to 111 * replace or attach devices which don't have the same physical size but 112 * can still satisfy the same number of allocations. 113 */ 114 uint64_t 115 vdev_get_min_asize(vdev_t *vd) 116 { 117 vdev_t *pvd = vd->vdev_parent; 118 119 /* 120 * If our parent is NULL (inactive spare or cache) or is the root, 121 * just return our own asize. 122 */ 123 if (pvd == NULL) 124 return (vd->vdev_asize); 125 126 /* 127 * The top-level vdev just returns the allocatable size rounded 128 * to the nearest metaslab. 129 */ 130 if (vd == vd->vdev_top) 131 return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift)); 132 133 /* 134 * The allocatable space for a raidz vdev is N * sizeof(smallest child), 135 * so each child must provide at least 1/Nth of its asize. 136 */ 137 if (pvd->vdev_ops == &vdev_raidz_ops) 138 return ((pvd->vdev_min_asize + pvd->vdev_children - 1) / 139 pvd->vdev_children); 140 141 return (pvd->vdev_min_asize); 142 } 143 144 void 145 vdev_set_min_asize(vdev_t *vd) 146 { 147 vd->vdev_min_asize = vdev_get_min_asize(vd); 148 149 for (int c = 0; c < vd->vdev_children; c++) 150 vdev_set_min_asize(vd->vdev_child[c]); 151 } 152 153 vdev_t * 154 vdev_lookup_top(spa_t *spa, uint64_t vdev) 155 { 156 vdev_t *rvd = spa->spa_root_vdev; 157 158 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 159 160 if (vdev < rvd->vdev_children) { 161 ASSERT(rvd->vdev_child[vdev] != NULL); 162 return (rvd->vdev_child[vdev]); 163 } 164 165 return (NULL); 166 } 167 168 vdev_t * 169 vdev_lookup_by_guid(vdev_t *vd, uint64_t guid) 170 { 171 vdev_t *mvd; 172 173 if (vd->vdev_guid == guid) 174 return (vd); 175 176 for (int c = 0; c < vd->vdev_children; c++) 177 if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) != 178 NULL) 179 return (mvd); 180 181 return (NULL); 182 } 183 184 static int 185 vdev_count_leaves_impl(vdev_t *vd) 186 { 187 int n = 0; 188 189 if (vd->vdev_ops->vdev_op_leaf) 190 return (1); 191 192 for (int c = 0; c < vd->vdev_children; c++) 193 n += vdev_count_leaves_impl(vd->vdev_child[c]); 194 195 return (n); 196 } 197 198 int 199 vdev_count_leaves(spa_t *spa) 200 { 201 return (vdev_count_leaves_impl(spa->spa_root_vdev)); 202 } 203 204 void 205 vdev_add_child(vdev_t *pvd, vdev_t *cvd) 206 { 207 size_t oldsize, newsize; 208 uint64_t id = cvd->vdev_id; 209 vdev_t **newchild; 210 spa_t *spa = cvd->vdev_spa; 211 212 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 213 ASSERT(cvd->vdev_parent == NULL); 214 215 cvd->vdev_parent = pvd; 216 217 if (pvd == NULL) 218 return; 219 220 ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL); 221 222 oldsize = pvd->vdev_children * sizeof (vdev_t *); 223 pvd->vdev_children = MAX(pvd->vdev_children, id + 1); 224 newsize = pvd->vdev_children * sizeof (vdev_t *); 225 226 newchild = kmem_zalloc(newsize, KM_SLEEP); 227 if (pvd->vdev_child != NULL) { 228 bcopy(pvd->vdev_child, newchild, oldsize); 229 kmem_free(pvd->vdev_child, oldsize); 230 } 231 232 pvd->vdev_child = newchild; 233 pvd->vdev_child[id] = cvd; 234 235 cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd); 236 ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL); 237 238 /* 239 * Walk up all ancestors to update guid sum. 240 */ 241 for (; pvd != NULL; pvd = pvd->vdev_parent) 242 pvd->vdev_guid_sum += cvd->vdev_guid_sum; 243 } 244 245 void 246 vdev_remove_child(vdev_t *pvd, vdev_t *cvd) 247 { 248 int c; 249 uint_t id = cvd->vdev_id; 250 251 ASSERT(cvd->vdev_parent == pvd); 252 253 if (pvd == NULL) 254 return; 255 256 ASSERT(id < pvd->vdev_children); 257 ASSERT(pvd->vdev_child[id] == cvd); 258 259 pvd->vdev_child[id] = NULL; 260 cvd->vdev_parent = NULL; 261 262 for (c = 0; c < pvd->vdev_children; c++) 263 if (pvd->vdev_child[c]) 264 break; 265 266 if (c == pvd->vdev_children) { 267 kmem_free(pvd->vdev_child, c * sizeof (vdev_t *)); 268 pvd->vdev_child = NULL; 269 pvd->vdev_children = 0; 270 } 271 272 /* 273 * Walk up all ancestors to update guid sum. 274 */ 275 for (; pvd != NULL; pvd = pvd->vdev_parent) 276 pvd->vdev_guid_sum -= cvd->vdev_guid_sum; 277 } 278 279 /* 280 * Remove any holes in the child array. 281 */ 282 void 283 vdev_compact_children(vdev_t *pvd) 284 { 285 vdev_t **newchild, *cvd; 286 int oldc = pvd->vdev_children; 287 int newc; 288 289 ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 290 291 for (int c = newc = 0; c < oldc; c++) 292 if (pvd->vdev_child[c]) 293 newc++; 294 295 newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP); 296 297 for (int c = newc = 0; c < oldc; c++) { 298 if ((cvd = pvd->vdev_child[c]) != NULL) { 299 newchild[newc] = cvd; 300 cvd->vdev_id = newc++; 301 } 302 } 303 304 kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *)); 305 pvd->vdev_child = newchild; 306 pvd->vdev_children = newc; 307 } 308 309 /* 310 * Allocate and minimally initialize a vdev_t. 311 */ 312 vdev_t * 313 vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops) 314 { 315 vdev_t *vd; 316 317 vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP); 318 319 if (spa->spa_root_vdev == NULL) { 320 ASSERT(ops == &vdev_root_ops); 321 spa->spa_root_vdev = vd; 322 spa->spa_load_guid = spa_generate_guid(NULL); 323 } 324 325 if (guid == 0 && ops != &vdev_hole_ops) { 326 if (spa->spa_root_vdev == vd) { 327 /* 328 * The root vdev's guid will also be the pool guid, 329 * which must be unique among all pools. 330 */ 331 guid = spa_generate_guid(NULL); 332 } else { 333 /* 334 * Any other vdev's guid must be unique within the pool. 335 */ 336 guid = spa_generate_guid(spa); 337 } 338 ASSERT(!spa_guid_exists(spa_guid(spa), guid)); 339 } 340 341 vd->vdev_spa = spa; 342 vd->vdev_id = id; 343 vd->vdev_guid = guid; 344 vd->vdev_guid_sum = guid; 345 vd->vdev_ops = ops; 346 vd->vdev_state = VDEV_STATE_CLOSED; 347 vd->vdev_ishole = (ops == &vdev_hole_ops); 348 349 mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL); 350 mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL); 351 mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL); 352 mutex_init(&vd->vdev_queue_lock, NULL, MUTEX_DEFAULT, NULL); 353 for (int t = 0; t < DTL_TYPES; t++) { 354 vd->vdev_dtl[t] = range_tree_create(NULL, NULL, 355 &vd->vdev_dtl_lock); 356 } 357 txg_list_create(&vd->vdev_ms_list, 358 offsetof(struct metaslab, ms_txg_node)); 359 txg_list_create(&vd->vdev_dtl_list, 360 offsetof(struct vdev, vdev_dtl_node)); 361 vd->vdev_stat.vs_timestamp = gethrtime(); 362 vdev_queue_init(vd); 363 vdev_cache_init(vd); 364 365 return (vd); 366 } 367 368 /* 369 * Allocate a new vdev. The 'alloctype' is used to control whether we are 370 * creating a new vdev or loading an existing one - the behavior is slightly 371 * different for each case. 372 */ 373 int 374 vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id, 375 int alloctype) 376 { 377 vdev_ops_t *ops; 378 char *type; 379 uint64_t guid = 0, islog, nparity; 380 vdev_t *vd; 381 382 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 383 384 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0) 385 return (SET_ERROR(EINVAL)); 386 387 if ((ops = vdev_getops(type)) == NULL) 388 return (SET_ERROR(EINVAL)); 389 390 /* 391 * If this is a load, get the vdev guid from the nvlist. 392 * Otherwise, vdev_alloc_common() will generate one for us. 393 */ 394 if (alloctype == VDEV_ALLOC_LOAD) { 395 uint64_t label_id; 396 397 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) || 398 label_id != id) 399 return (SET_ERROR(EINVAL)); 400 401 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 402 return (SET_ERROR(EINVAL)); 403 } else if (alloctype == VDEV_ALLOC_SPARE) { 404 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 405 return (SET_ERROR(EINVAL)); 406 } else if (alloctype == VDEV_ALLOC_L2CACHE) { 407 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 408 return (SET_ERROR(EINVAL)); 409 } else if (alloctype == VDEV_ALLOC_ROOTPOOL) { 410 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 411 return (SET_ERROR(EINVAL)); 412 } 413 414 /* 415 * The first allocated vdev must be of type 'root'. 416 */ 417 if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL) 418 return (SET_ERROR(EINVAL)); 419 420 /* 421 * Determine whether we're a log vdev. 422 */ 423 islog = 0; 424 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog); 425 if (islog && spa_version(spa) < SPA_VERSION_SLOGS) 426 return (SET_ERROR(ENOTSUP)); 427 428 if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES) 429 return (SET_ERROR(ENOTSUP)); 430 431 /* 432 * Set the nparity property for RAID-Z vdevs. 433 */ 434 nparity = -1ULL; 435 if (ops == &vdev_raidz_ops) { 436 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 437 &nparity) == 0) { 438 if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY) 439 return (SET_ERROR(EINVAL)); 440 /* 441 * Previous versions could only support 1 or 2 parity 442 * device. 443 */ 444 if (nparity > 1 && 445 spa_version(spa) < SPA_VERSION_RAIDZ2) 446 return (SET_ERROR(ENOTSUP)); 447 if (nparity > 2 && 448 spa_version(spa) < SPA_VERSION_RAIDZ3) 449 return (SET_ERROR(ENOTSUP)); 450 } else { 451 /* 452 * We require the parity to be specified for SPAs that 453 * support multiple parity levels. 454 */ 455 if (spa_version(spa) >= SPA_VERSION_RAIDZ2) 456 return (SET_ERROR(EINVAL)); 457 /* 458 * Otherwise, we default to 1 parity device for RAID-Z. 459 */ 460 nparity = 1; 461 } 462 } else { 463 nparity = 0; 464 } 465 ASSERT(nparity != -1ULL); 466 467 vd = vdev_alloc_common(spa, id, guid, ops); 468 469 vd->vdev_islog = islog; 470 vd->vdev_nparity = nparity; 471 472 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0) 473 vd->vdev_path = spa_strdup(vd->vdev_path); 474 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0) 475 vd->vdev_devid = spa_strdup(vd->vdev_devid); 476 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH, 477 &vd->vdev_physpath) == 0) 478 vd->vdev_physpath = spa_strdup(vd->vdev_physpath); 479 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0) 480 vd->vdev_fru = spa_strdup(vd->vdev_fru); 481 482 /* 483 * Set the whole_disk property. If it's not specified, leave the value 484 * as -1. 485 */ 486 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 487 &vd->vdev_wholedisk) != 0) 488 vd->vdev_wholedisk = -1ULL; 489 490 /* 491 * Look for the 'not present' flag. This will only be set if the device 492 * was not present at the time of import. 493 */ 494 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 495 &vd->vdev_not_present); 496 497 /* 498 * Get the alignment requirement. 499 */ 500 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift); 501 502 /* 503 * Retrieve the vdev creation time. 504 */ 505 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG, 506 &vd->vdev_crtxg); 507 508 /* 509 * If we're a top-level vdev, try to load the allocation parameters. 510 */ 511 if (parent && !parent->vdev_parent && 512 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) { 513 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY, 514 &vd->vdev_ms_array); 515 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT, 516 &vd->vdev_ms_shift); 517 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE, 518 &vd->vdev_asize); 519 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING, 520 &vd->vdev_removing); 521 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP, 522 &vd->vdev_top_zap); 523 } else { 524 ASSERT0(vd->vdev_top_zap); 525 } 526 527 if (parent && !parent->vdev_parent && alloctype != VDEV_ALLOC_ATTACH) { 528 ASSERT(alloctype == VDEV_ALLOC_LOAD || 529 alloctype == VDEV_ALLOC_ADD || 530 alloctype == VDEV_ALLOC_SPLIT || 531 alloctype == VDEV_ALLOC_ROOTPOOL); 532 vd->vdev_mg = metaslab_group_create(islog ? 533 spa_log_class(spa) : spa_normal_class(spa), vd); 534 } 535 536 if (vd->vdev_ops->vdev_op_leaf && 537 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) { 538 (void) nvlist_lookup_uint64(nv, 539 ZPOOL_CONFIG_VDEV_LEAF_ZAP, &vd->vdev_leaf_zap); 540 } else { 541 ASSERT0(vd->vdev_leaf_zap); 542 } 543 544 /* 545 * If we're a leaf vdev, try to load the DTL object and other state. 546 */ 547 548 if (vd->vdev_ops->vdev_op_leaf && 549 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE || 550 alloctype == VDEV_ALLOC_ROOTPOOL)) { 551 if (alloctype == VDEV_ALLOC_LOAD) { 552 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL, 553 &vd->vdev_dtl_object); 554 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE, 555 &vd->vdev_unspare); 556 } 557 558 if (alloctype == VDEV_ALLOC_ROOTPOOL) { 559 uint64_t spare = 0; 560 561 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 562 &spare) == 0 && spare) 563 spa_spare_add(vd); 564 } 565 566 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, 567 &vd->vdev_offline); 568 569 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG, 570 &vd->vdev_resilver_txg); 571 572 /* 573 * When importing a pool, we want to ignore the persistent fault 574 * state, as the diagnosis made on another system may not be 575 * valid in the current context. Local vdevs will 576 * remain in the faulted state. 577 */ 578 if (spa_load_state(spa) == SPA_LOAD_OPEN) { 579 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, 580 &vd->vdev_faulted); 581 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED, 582 &vd->vdev_degraded); 583 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, 584 &vd->vdev_removed); 585 586 if (vd->vdev_faulted || vd->vdev_degraded) { 587 char *aux; 588 589 vd->vdev_label_aux = 590 VDEV_AUX_ERR_EXCEEDED; 591 if (nvlist_lookup_string(nv, 592 ZPOOL_CONFIG_AUX_STATE, &aux) == 0 && 593 strcmp(aux, "external") == 0) 594 vd->vdev_label_aux = VDEV_AUX_EXTERNAL; 595 } 596 } 597 } 598 599 /* 600 * Add ourselves to the parent's list of children. 601 */ 602 vdev_add_child(parent, vd); 603 604 *vdp = vd; 605 606 return (0); 607 } 608 609 void 610 vdev_free(vdev_t *vd) 611 { 612 spa_t *spa = vd->vdev_spa; 613 614 /* 615 * vdev_free() implies closing the vdev first. This is simpler than 616 * trying to ensure complicated semantics for all callers. 617 */ 618 vdev_close(vd); 619 620 ASSERT(!list_link_active(&vd->vdev_config_dirty_node)); 621 ASSERT(!list_link_active(&vd->vdev_state_dirty_node)); 622 623 /* 624 * Free all children. 625 */ 626 for (int c = 0; c < vd->vdev_children; c++) 627 vdev_free(vd->vdev_child[c]); 628 629 ASSERT(vd->vdev_child == NULL); 630 ASSERT(vd->vdev_guid_sum == vd->vdev_guid); 631 632 /* 633 * Discard allocation state. 634 */ 635 if (vd->vdev_mg != NULL) { 636 vdev_metaslab_fini(vd); 637 metaslab_group_destroy(vd->vdev_mg); 638 } 639 640 ASSERT0(vd->vdev_stat.vs_space); 641 ASSERT0(vd->vdev_stat.vs_dspace); 642 ASSERT0(vd->vdev_stat.vs_alloc); 643 644 /* 645 * Remove this vdev from its parent's child list. 646 */ 647 vdev_remove_child(vd->vdev_parent, vd); 648 649 ASSERT(vd->vdev_parent == NULL); 650 651 /* 652 * Clean up vdev structure. 653 */ 654 vdev_queue_fini(vd); 655 vdev_cache_fini(vd); 656 657 if (vd->vdev_path) 658 spa_strfree(vd->vdev_path); 659 if (vd->vdev_devid) 660 spa_strfree(vd->vdev_devid); 661 if (vd->vdev_physpath) 662 spa_strfree(vd->vdev_physpath); 663 if (vd->vdev_fru) 664 spa_strfree(vd->vdev_fru); 665 666 if (vd->vdev_isspare) 667 spa_spare_remove(vd); 668 if (vd->vdev_isl2cache) 669 spa_l2cache_remove(vd); 670 671 txg_list_destroy(&vd->vdev_ms_list); 672 txg_list_destroy(&vd->vdev_dtl_list); 673 674 mutex_enter(&vd->vdev_dtl_lock); 675 space_map_close(vd->vdev_dtl_sm); 676 for (int t = 0; t < DTL_TYPES; t++) { 677 range_tree_vacate(vd->vdev_dtl[t], NULL, NULL); 678 range_tree_destroy(vd->vdev_dtl[t]); 679 } 680 mutex_exit(&vd->vdev_dtl_lock); 681 682 mutex_destroy(&vd->vdev_queue_lock); 683 mutex_destroy(&vd->vdev_dtl_lock); 684 mutex_destroy(&vd->vdev_stat_lock); 685 mutex_destroy(&vd->vdev_probe_lock); 686 687 if (vd == spa->spa_root_vdev) 688 spa->spa_root_vdev = NULL; 689 690 kmem_free(vd, sizeof (vdev_t)); 691 } 692 693 /* 694 * Transfer top-level vdev state from svd to tvd. 695 */ 696 static void 697 vdev_top_transfer(vdev_t *svd, vdev_t *tvd) 698 { 699 spa_t *spa = svd->vdev_spa; 700 metaslab_t *msp; 701 vdev_t *vd; 702 int t; 703 704 ASSERT(tvd == tvd->vdev_top); 705 706 tvd->vdev_ms_array = svd->vdev_ms_array; 707 tvd->vdev_ms_shift = svd->vdev_ms_shift; 708 tvd->vdev_ms_count = svd->vdev_ms_count; 709 tvd->vdev_top_zap = svd->vdev_top_zap; 710 711 svd->vdev_ms_array = 0; 712 svd->vdev_ms_shift = 0; 713 svd->vdev_ms_count = 0; 714 svd->vdev_top_zap = 0; 715 716 if (tvd->vdev_mg) 717 ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg); 718 tvd->vdev_mg = svd->vdev_mg; 719 tvd->vdev_ms = svd->vdev_ms; 720 721 svd->vdev_mg = NULL; 722 svd->vdev_ms = NULL; 723 724 if (tvd->vdev_mg != NULL) 725 tvd->vdev_mg->mg_vd = tvd; 726 727 tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc; 728 tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space; 729 tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace; 730 731 svd->vdev_stat.vs_alloc = 0; 732 svd->vdev_stat.vs_space = 0; 733 svd->vdev_stat.vs_dspace = 0; 734 735 for (t = 0; t < TXG_SIZE; t++) { 736 while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL) 737 (void) txg_list_add(&tvd->vdev_ms_list, msp, t); 738 while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL) 739 (void) txg_list_add(&tvd->vdev_dtl_list, vd, t); 740 if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t)) 741 (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t); 742 } 743 744 if (list_link_active(&svd->vdev_config_dirty_node)) { 745 vdev_config_clean(svd); 746 vdev_config_dirty(tvd); 747 } 748 749 if (list_link_active(&svd->vdev_state_dirty_node)) { 750 vdev_state_clean(svd); 751 vdev_state_dirty(tvd); 752 } 753 754 tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio; 755 svd->vdev_deflate_ratio = 0; 756 757 tvd->vdev_islog = svd->vdev_islog; 758 svd->vdev_islog = 0; 759 } 760 761 static void 762 vdev_top_update(vdev_t *tvd, vdev_t *vd) 763 { 764 if (vd == NULL) 765 return; 766 767 vd->vdev_top = tvd; 768 769 for (int c = 0; c < vd->vdev_children; c++) 770 vdev_top_update(tvd, vd->vdev_child[c]); 771 } 772 773 /* 774 * Add a mirror/replacing vdev above an existing vdev. 775 */ 776 vdev_t * 777 vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops) 778 { 779 spa_t *spa = cvd->vdev_spa; 780 vdev_t *pvd = cvd->vdev_parent; 781 vdev_t *mvd; 782 783 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 784 785 mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops); 786 787 mvd->vdev_asize = cvd->vdev_asize; 788 mvd->vdev_min_asize = cvd->vdev_min_asize; 789 mvd->vdev_max_asize = cvd->vdev_max_asize; 790 mvd->vdev_ashift = cvd->vdev_ashift; 791 mvd->vdev_state = cvd->vdev_state; 792 mvd->vdev_crtxg = cvd->vdev_crtxg; 793 794 vdev_remove_child(pvd, cvd); 795 vdev_add_child(pvd, mvd); 796 cvd->vdev_id = mvd->vdev_children; 797 vdev_add_child(mvd, cvd); 798 vdev_top_update(cvd->vdev_top, cvd->vdev_top); 799 800 if (mvd == mvd->vdev_top) 801 vdev_top_transfer(cvd, mvd); 802 803 return (mvd); 804 } 805 806 /* 807 * Remove a 1-way mirror/replacing vdev from the tree. 808 */ 809 void 810 vdev_remove_parent(vdev_t *cvd) 811 { 812 vdev_t *mvd = cvd->vdev_parent; 813 vdev_t *pvd = mvd->vdev_parent; 814 815 ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 816 817 ASSERT(mvd->vdev_children == 1); 818 ASSERT(mvd->vdev_ops == &vdev_mirror_ops || 819 mvd->vdev_ops == &vdev_replacing_ops || 820 mvd->vdev_ops == &vdev_spare_ops); 821 cvd->vdev_ashift = mvd->vdev_ashift; 822 823 vdev_remove_child(mvd, cvd); 824 vdev_remove_child(pvd, mvd); 825 826 /* 827 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid. 828 * Otherwise, we could have detached an offline device, and when we 829 * go to import the pool we'll think we have two top-level vdevs, 830 * instead of a different version of the same top-level vdev. 831 */ 832 if (mvd->vdev_top == mvd) { 833 uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid; 834 cvd->vdev_orig_guid = cvd->vdev_guid; 835 cvd->vdev_guid += guid_delta; 836 cvd->vdev_guid_sum += guid_delta; 837 } 838 cvd->vdev_id = mvd->vdev_id; 839 vdev_add_child(pvd, cvd); 840 vdev_top_update(cvd->vdev_top, cvd->vdev_top); 841 842 if (cvd == cvd->vdev_top) 843 vdev_top_transfer(mvd, cvd); 844 845 ASSERT(mvd->vdev_children == 0); 846 vdev_free(mvd); 847 } 848 849 int 850 vdev_metaslab_init(vdev_t *vd, uint64_t txg) 851 { 852 spa_t *spa = vd->vdev_spa; 853 objset_t *mos = spa->spa_meta_objset; 854 uint64_t m; 855 uint64_t oldc = vd->vdev_ms_count; 856 uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift; 857 metaslab_t **mspp; 858 int error; 859 860 ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 861 862 /* 863 * This vdev is not being allocated from yet or is a hole. 864 */ 865 if (vd->vdev_ms_shift == 0) 866 return (0); 867 868 ASSERT(!vd->vdev_ishole); 869 870 /* 871 * Compute the raidz-deflation ratio. Note, we hard-code 872 * in 128k (1 << 17) because it is the "typical" blocksize. 873 * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change, 874 * otherwise it would inconsistently account for existing bp's. 875 */ 876 vd->vdev_deflate_ratio = (1 << 17) / 877 (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT); 878 879 ASSERT(oldc <= newc); 880 881 mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP); 882 883 if (oldc != 0) { 884 bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp)); 885 kmem_free(vd->vdev_ms, oldc * sizeof (*mspp)); 886 } 887 888 vd->vdev_ms = mspp; 889 vd->vdev_ms_count = newc; 890 891 for (m = oldc; m < newc; m++) { 892 uint64_t object = 0; 893 894 if (txg == 0) { 895 error = dmu_read(mos, vd->vdev_ms_array, 896 m * sizeof (uint64_t), sizeof (uint64_t), &object, 897 DMU_READ_PREFETCH); 898 if (error) 899 return (error); 900 } 901 902 error = metaslab_init(vd->vdev_mg, m, object, txg, 903 &(vd->vdev_ms[m])); 904 if (error) 905 return (error); 906 } 907 908 if (txg == 0) 909 spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER); 910 911 /* 912 * If the vdev is being removed we don't activate 913 * the metaslabs since we want to ensure that no new 914 * allocations are performed on this device. 915 */ 916 if (oldc == 0 && !vd->vdev_removing) 917 metaslab_group_activate(vd->vdev_mg); 918 919 if (txg == 0) 920 spa_config_exit(spa, SCL_ALLOC, FTAG); 921 922 return (0); 923 } 924 925 void 926 vdev_metaslab_fini(vdev_t *vd) 927 { 928 uint64_t m; 929 uint64_t count = vd->vdev_ms_count; 930 931 if (vd->vdev_ms != NULL) { 932 metaslab_group_passivate(vd->vdev_mg); 933 for (m = 0; m < count; m++) { 934 metaslab_t *msp = vd->vdev_ms[m]; 935 936 if (msp != NULL) 937 metaslab_fini(msp); 938 } 939 kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *)); 940 vd->vdev_ms = NULL; 941 } 942 } 943 944 typedef struct vdev_probe_stats { 945 boolean_t vps_readable; 946 boolean_t vps_writeable; 947 int vps_flags; 948 } vdev_probe_stats_t; 949 950 static void 951 vdev_probe_done(zio_t *zio) 952 { 953 spa_t *spa = zio->io_spa; 954 vdev_t *vd = zio->io_vd; 955 vdev_probe_stats_t *vps = zio->io_private; 956 957 ASSERT(vd->vdev_probe_zio != NULL); 958 959 if (zio->io_type == ZIO_TYPE_READ) { 960 if (zio->io_error == 0) 961 vps->vps_readable = 1; 962 if (zio->io_error == 0 && spa_writeable(spa)) { 963 zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd, 964 zio->io_offset, zio->io_size, zio->io_data, 965 ZIO_CHECKSUM_OFF, vdev_probe_done, vps, 966 ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE)); 967 } else { 968 zio_buf_free(zio->io_data, zio->io_size); 969 } 970 } else if (zio->io_type == ZIO_TYPE_WRITE) { 971 if (zio->io_error == 0) 972 vps->vps_writeable = 1; 973 zio_buf_free(zio->io_data, zio->io_size); 974 } else if (zio->io_type == ZIO_TYPE_NULL) { 975 zio_t *pio; 976 977 vd->vdev_cant_read |= !vps->vps_readable; 978 vd->vdev_cant_write |= !vps->vps_writeable; 979 980 if (vdev_readable(vd) && 981 (vdev_writeable(vd) || !spa_writeable(spa))) { 982 zio->io_error = 0; 983 } else { 984 ASSERT(zio->io_error != 0); 985 zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE, 986 spa, vd, NULL, 0, 0); 987 zio->io_error = SET_ERROR(ENXIO); 988 } 989 990 mutex_enter(&vd->vdev_probe_lock); 991 ASSERT(vd->vdev_probe_zio == zio); 992 vd->vdev_probe_zio = NULL; 993 mutex_exit(&vd->vdev_probe_lock); 994 995 zio_link_t *zl = NULL; 996 while ((pio = zio_walk_parents(zio, &zl)) != NULL) 997 if (!vdev_accessible(vd, pio)) 998 pio->io_error = SET_ERROR(ENXIO); 999 1000 kmem_free(vps, sizeof (*vps)); 1001 } 1002 } 1003 1004 /* 1005 * Determine whether this device is accessible. 1006 * 1007 * Read and write to several known locations: the pad regions of each 1008 * vdev label but the first, which we leave alone in case it contains 1009 * a VTOC. 1010 */ 1011 zio_t * 1012 vdev_probe(vdev_t *vd, zio_t *zio) 1013 { 1014 spa_t *spa = vd->vdev_spa; 1015 vdev_probe_stats_t *vps = NULL; 1016 zio_t *pio; 1017 1018 ASSERT(vd->vdev_ops->vdev_op_leaf); 1019 1020 /* 1021 * Don't probe the probe. 1022 */ 1023 if (zio && (zio->io_flags & ZIO_FLAG_PROBE)) 1024 return (NULL); 1025 1026 /* 1027 * To prevent 'probe storms' when a device fails, we create 1028 * just one probe i/o at a time. All zios that want to probe 1029 * this vdev will become parents of the probe io. 1030 */ 1031 mutex_enter(&vd->vdev_probe_lock); 1032 1033 if ((pio = vd->vdev_probe_zio) == NULL) { 1034 vps = kmem_zalloc(sizeof (*vps), KM_SLEEP); 1035 1036 vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE | 1037 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE | 1038 ZIO_FLAG_TRYHARD; 1039 1040 if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) { 1041 /* 1042 * vdev_cant_read and vdev_cant_write can only 1043 * transition from TRUE to FALSE when we have the 1044 * SCL_ZIO lock as writer; otherwise they can only 1045 * transition from FALSE to TRUE. This ensures that 1046 * any zio looking at these values can assume that 1047 * failures persist for the life of the I/O. That's 1048 * important because when a device has intermittent 1049 * connectivity problems, we want to ensure that 1050 * they're ascribed to the device (ENXIO) and not 1051 * the zio (EIO). 1052 * 1053 * Since we hold SCL_ZIO as writer here, clear both 1054 * values so the probe can reevaluate from first 1055 * principles. 1056 */ 1057 vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER; 1058 vd->vdev_cant_read = B_FALSE; 1059 vd->vdev_cant_write = B_FALSE; 1060 } 1061 1062 vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd, 1063 vdev_probe_done, vps, 1064 vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE); 1065 1066 /* 1067 * We can't change the vdev state in this context, so we 1068 * kick off an async task to do it on our behalf. 1069 */ 1070 if (zio != NULL) { 1071 vd->vdev_probe_wanted = B_TRUE; 1072 spa_async_request(spa, SPA_ASYNC_PROBE); 1073 } 1074 } 1075 1076 if (zio != NULL) 1077 zio_add_child(zio, pio); 1078 1079 mutex_exit(&vd->vdev_probe_lock); 1080 1081 if (vps == NULL) { 1082 ASSERT(zio != NULL); 1083 return (NULL); 1084 } 1085 1086 for (int l = 1; l < VDEV_LABELS; l++) { 1087 zio_nowait(zio_read_phys(pio, vd, 1088 vdev_label_offset(vd->vdev_psize, l, 1089 offsetof(vdev_label_t, vl_pad2)), 1090 VDEV_PAD_SIZE, zio_buf_alloc(VDEV_PAD_SIZE), 1091 ZIO_CHECKSUM_OFF, vdev_probe_done, vps, 1092 ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE)); 1093 } 1094 1095 if (zio == NULL) 1096 return (pio); 1097 1098 zio_nowait(pio); 1099 return (NULL); 1100 } 1101 1102 static void 1103 vdev_open_child(void *arg) 1104 { 1105 vdev_t *vd = arg; 1106 1107 vd->vdev_open_thread = curthread; 1108 vd->vdev_open_error = vdev_open(vd); 1109 vd->vdev_open_thread = NULL; 1110 } 1111 1112 boolean_t 1113 vdev_uses_zvols(vdev_t *vd) 1114 { 1115 if (vd->vdev_path && strncmp(vd->vdev_path, ZVOL_DIR, 1116 strlen(ZVOL_DIR)) == 0) 1117 return (B_TRUE); 1118 for (int c = 0; c < vd->vdev_children; c++) 1119 if (vdev_uses_zvols(vd->vdev_child[c])) 1120 return (B_TRUE); 1121 return (B_FALSE); 1122 } 1123 1124 void 1125 vdev_open_children(vdev_t *vd) 1126 { 1127 taskq_t *tq; 1128 int children = vd->vdev_children; 1129 1130 /* 1131 * in order to handle pools on top of zvols, do the opens 1132 * in a single thread so that the same thread holds the 1133 * spa_namespace_lock 1134 */ 1135 if (vdev_uses_zvols(vd)) { 1136 for (int c = 0; c < children; c++) 1137 vd->vdev_child[c]->vdev_open_error = 1138 vdev_open(vd->vdev_child[c]); 1139 return; 1140 } 1141 tq = taskq_create("vdev_open", children, minclsyspri, 1142 children, children, TASKQ_PREPOPULATE); 1143 1144 for (int c = 0; c < children; c++) 1145 VERIFY(taskq_dispatch(tq, vdev_open_child, vd->vdev_child[c], 1146 TQ_SLEEP) != NULL); 1147 1148 taskq_destroy(tq); 1149 } 1150 1151 /* 1152 * Prepare a virtual device for access. 1153 */ 1154 int 1155 vdev_open(vdev_t *vd) 1156 { 1157 spa_t *spa = vd->vdev_spa; 1158 int error; 1159 uint64_t osize = 0; 1160 uint64_t max_osize = 0; 1161 uint64_t asize, max_asize, psize; 1162 uint64_t ashift = 0; 1163 1164 ASSERT(vd->vdev_open_thread == curthread || 1165 spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 1166 ASSERT(vd->vdev_state == VDEV_STATE_CLOSED || 1167 vd->vdev_state == VDEV_STATE_CANT_OPEN || 1168 vd->vdev_state == VDEV_STATE_OFFLINE); 1169 1170 vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 1171 vd->vdev_cant_read = B_FALSE; 1172 vd->vdev_cant_write = B_FALSE; 1173 vd->vdev_min_asize = vdev_get_min_asize(vd); 1174 1175 /* 1176 * If this vdev is not removed, check its fault status. If it's 1177 * faulted, bail out of the open. 1178 */ 1179 if (!vd->vdev_removed && vd->vdev_faulted) { 1180 ASSERT(vd->vdev_children == 0); 1181 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED || 1182 vd->vdev_label_aux == VDEV_AUX_EXTERNAL); 1183 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 1184 vd->vdev_label_aux); 1185 return (SET_ERROR(ENXIO)); 1186 } else if (vd->vdev_offline) { 1187 ASSERT(vd->vdev_children == 0); 1188 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE); 1189 return (SET_ERROR(ENXIO)); 1190 } 1191 1192 error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize, &ashift); 1193 1194 /* 1195 * Reset the vdev_reopening flag so that we actually close 1196 * the vdev on error. 1197 */ 1198 vd->vdev_reopening = B_FALSE; 1199 if (zio_injection_enabled && error == 0) 1200 error = zio_handle_device_injection(vd, NULL, ENXIO); 1201 1202 if (error) { 1203 if (vd->vdev_removed && 1204 vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED) 1205 vd->vdev_removed = B_FALSE; 1206 1207 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1208 vd->vdev_stat.vs_aux); 1209 return (error); 1210 } 1211 1212 vd->vdev_removed = B_FALSE; 1213 1214 /* 1215 * Recheck the faulted flag now that we have confirmed that 1216 * the vdev is accessible. If we're faulted, bail. 1217 */ 1218 if (vd->vdev_faulted) { 1219 ASSERT(vd->vdev_children == 0); 1220 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED || 1221 vd->vdev_label_aux == VDEV_AUX_EXTERNAL); 1222 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 1223 vd->vdev_label_aux); 1224 return (SET_ERROR(ENXIO)); 1225 } 1226 1227 if (vd->vdev_degraded) { 1228 ASSERT(vd->vdev_children == 0); 1229 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 1230 VDEV_AUX_ERR_EXCEEDED); 1231 } else { 1232 vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0); 1233 } 1234 1235 /* 1236 * For hole or missing vdevs we just return success. 1237 */ 1238 if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) 1239 return (0); 1240 1241 for (int c = 0; c < vd->vdev_children; c++) { 1242 if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) { 1243 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 1244 VDEV_AUX_NONE); 1245 break; 1246 } 1247 } 1248 1249 osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t)); 1250 max_osize = P2ALIGN(max_osize, (uint64_t)sizeof (vdev_label_t)); 1251 1252 if (vd->vdev_children == 0) { 1253 if (osize < SPA_MINDEVSIZE) { 1254 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1255 VDEV_AUX_TOO_SMALL); 1256 return (SET_ERROR(EOVERFLOW)); 1257 } 1258 psize = osize; 1259 asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE); 1260 max_asize = max_osize - (VDEV_LABEL_START_SIZE + 1261 VDEV_LABEL_END_SIZE); 1262 } else { 1263 if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE - 1264 (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) { 1265 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1266 VDEV_AUX_TOO_SMALL); 1267 return (SET_ERROR(EOVERFLOW)); 1268 } 1269 psize = 0; 1270 asize = osize; 1271 max_asize = max_osize; 1272 } 1273 1274 vd->vdev_psize = psize; 1275 1276 /* 1277 * Make sure the allocatable size hasn't shrunk too much. 1278 */ 1279 if (asize < vd->vdev_min_asize) { 1280 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1281 VDEV_AUX_BAD_LABEL); 1282 return (SET_ERROR(EINVAL)); 1283 } 1284 1285 if (vd->vdev_asize == 0) { 1286 /* 1287 * This is the first-ever open, so use the computed values. 1288 * For testing purposes, a higher ashift can be requested. 1289 */ 1290 vd->vdev_asize = asize; 1291 vd->vdev_max_asize = max_asize; 1292 vd->vdev_ashift = MAX(ashift, vd->vdev_ashift); 1293 } else { 1294 /* 1295 * Detect if the alignment requirement has increased. 1296 * We don't want to make the pool unavailable, just 1297 * issue a warning instead. 1298 */ 1299 if (ashift > vd->vdev_top->vdev_ashift && 1300 vd->vdev_ops->vdev_op_leaf) { 1301 cmn_err(CE_WARN, 1302 "Disk, '%s', has a block alignment that is " 1303 "larger than the pool's alignment\n", 1304 vd->vdev_path); 1305 } 1306 vd->vdev_max_asize = max_asize; 1307 } 1308 1309 /* 1310 * If all children are healthy we update asize if either: 1311 * The asize has increased, due to a device expansion caused by dynamic 1312 * LUN growth or vdev replacement, and automatic expansion is enabled; 1313 * making the additional space available. 1314 * 1315 * The asize has decreased, due to a device shrink usually caused by a 1316 * vdev replace with a smaller device. This ensures that calculations 1317 * based of max_asize and asize e.g. esize are always valid. It's safe 1318 * to do this as we've already validated that asize is greater than 1319 * vdev_min_asize. 1320 */ 1321 if (vd->vdev_state == VDEV_STATE_HEALTHY && 1322 ((asize > vd->vdev_asize && 1323 (vd->vdev_expanding || spa->spa_autoexpand)) || 1324 (asize < vd->vdev_asize))) 1325 vd->vdev_asize = asize; 1326 1327 vdev_set_min_asize(vd); 1328 1329 /* 1330 * Ensure we can issue some IO before declaring the 1331 * vdev open for business. 1332 */ 1333 if (vd->vdev_ops->vdev_op_leaf && 1334 (error = zio_wait(vdev_probe(vd, NULL))) != 0) { 1335 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 1336 VDEV_AUX_ERR_EXCEEDED); 1337 return (error); 1338 } 1339 1340 /* 1341 * Track the min and max ashift values for normal data devices. 1342 */ 1343 if (vd->vdev_top == vd && vd->vdev_ashift != 0 && 1344 !vd->vdev_islog && vd->vdev_aux == NULL) { 1345 if (vd->vdev_ashift > spa->spa_max_ashift) 1346 spa->spa_max_ashift = vd->vdev_ashift; 1347 if (vd->vdev_ashift < spa->spa_min_ashift) 1348 spa->spa_min_ashift = vd->vdev_ashift; 1349 } 1350 1351 /* 1352 * If a leaf vdev has a DTL, and seems healthy, then kick off a 1353 * resilver. But don't do this if we are doing a reopen for a scrub, 1354 * since this would just restart the scrub we are already doing. 1355 */ 1356 if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen && 1357 vdev_resilver_needed(vd, NULL, NULL)) 1358 spa_async_request(spa, SPA_ASYNC_RESILVER); 1359 1360 return (0); 1361 } 1362 1363 /* 1364 * Called once the vdevs are all opened, this routine validates the label 1365 * contents. This needs to be done before vdev_load() so that we don't 1366 * inadvertently do repair I/Os to the wrong device. 1367 * 1368 * If 'strict' is false ignore the spa guid check. This is necessary because 1369 * if the machine crashed during a re-guid the new guid might have been written 1370 * to all of the vdev labels, but not the cached config. The strict check 1371 * will be performed when the pool is opened again using the mos config. 1372 * 1373 * This function will only return failure if one of the vdevs indicates that it 1374 * has since been destroyed or exported. This is only possible if 1375 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state 1376 * will be updated but the function will return 0. 1377 */ 1378 int 1379 vdev_validate(vdev_t *vd, boolean_t strict) 1380 { 1381 spa_t *spa = vd->vdev_spa; 1382 nvlist_t *label; 1383 uint64_t guid = 0, top_guid; 1384 uint64_t state; 1385 1386 for (int c = 0; c < vd->vdev_children; c++) 1387 if (vdev_validate(vd->vdev_child[c], strict) != 0) 1388 return (SET_ERROR(EBADF)); 1389 1390 /* 1391 * If the device has already failed, or was marked offline, don't do 1392 * any further validation. Otherwise, label I/O will fail and we will 1393 * overwrite the previous state. 1394 */ 1395 if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) { 1396 uint64_t aux_guid = 0; 1397 nvlist_t *nvl; 1398 uint64_t txg = spa_last_synced_txg(spa) != 0 ? 1399 spa_last_synced_txg(spa) : -1ULL; 1400 1401 if ((label = vdev_label_read_config(vd, txg)) == NULL) { 1402 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1403 VDEV_AUX_BAD_LABEL); 1404 return (0); 1405 } 1406 1407 /* 1408 * Determine if this vdev has been split off into another 1409 * pool. If so, then refuse to open it. 1410 */ 1411 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID, 1412 &aux_guid) == 0 && aux_guid == spa_guid(spa)) { 1413 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1414 VDEV_AUX_SPLIT_POOL); 1415 nvlist_free(label); 1416 return (0); 1417 } 1418 1419 if (strict && (nvlist_lookup_uint64(label, 1420 ZPOOL_CONFIG_POOL_GUID, &guid) != 0 || 1421 guid != spa_guid(spa))) { 1422 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1423 VDEV_AUX_CORRUPT_DATA); 1424 nvlist_free(label); 1425 return (0); 1426 } 1427 1428 if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl) 1429 != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID, 1430 &aux_guid) != 0) 1431 aux_guid = 0; 1432 1433 /* 1434 * If this vdev just became a top-level vdev because its 1435 * sibling was detached, it will have adopted the parent's 1436 * vdev guid -- but the label may or may not be on disk yet. 1437 * Fortunately, either version of the label will have the 1438 * same top guid, so if we're a top-level vdev, we can 1439 * safely compare to that instead. 1440 * 1441 * If we split this vdev off instead, then we also check the 1442 * original pool's guid. We don't want to consider the vdev 1443 * corrupt if it is partway through a split operation. 1444 */ 1445 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, 1446 &guid) != 0 || 1447 nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID, 1448 &top_guid) != 0 || 1449 ((vd->vdev_guid != guid && vd->vdev_guid != aux_guid) && 1450 (vd->vdev_guid != top_guid || vd != vd->vdev_top))) { 1451 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1452 VDEV_AUX_CORRUPT_DATA); 1453 nvlist_free(label); 1454 return (0); 1455 } 1456 1457 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, 1458 &state) != 0) { 1459 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1460 VDEV_AUX_CORRUPT_DATA); 1461 nvlist_free(label); 1462 return (0); 1463 } 1464 1465 nvlist_free(label); 1466 1467 /* 1468 * If this is a verbatim import, no need to check the 1469 * state of the pool. 1470 */ 1471 if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) && 1472 spa_load_state(spa) == SPA_LOAD_OPEN && 1473 state != POOL_STATE_ACTIVE) 1474 return (SET_ERROR(EBADF)); 1475 1476 /* 1477 * If we were able to open and validate a vdev that was 1478 * previously marked permanently unavailable, clear that state 1479 * now. 1480 */ 1481 if (vd->vdev_not_present) 1482 vd->vdev_not_present = 0; 1483 } 1484 1485 return (0); 1486 } 1487 1488 /* 1489 * Close a virtual device. 1490 */ 1491 void 1492 vdev_close(vdev_t *vd) 1493 { 1494 spa_t *spa = vd->vdev_spa; 1495 vdev_t *pvd = vd->vdev_parent; 1496 1497 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 1498 1499 /* 1500 * If our parent is reopening, then we are as well, unless we are 1501 * going offline. 1502 */ 1503 if (pvd != NULL && pvd->vdev_reopening) 1504 vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline); 1505 1506 vd->vdev_ops->vdev_op_close(vd); 1507 1508 vdev_cache_purge(vd); 1509 1510 /* 1511 * We record the previous state before we close it, so that if we are 1512 * doing a reopen(), we don't generate FMA ereports if we notice that 1513 * it's still faulted. 1514 */ 1515 vd->vdev_prevstate = vd->vdev_state; 1516 1517 if (vd->vdev_offline) 1518 vd->vdev_state = VDEV_STATE_OFFLINE; 1519 else 1520 vd->vdev_state = VDEV_STATE_CLOSED; 1521 vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 1522 } 1523 1524 void 1525 vdev_hold(vdev_t *vd) 1526 { 1527 spa_t *spa = vd->vdev_spa; 1528 1529 ASSERT(spa_is_root(spa)); 1530 if (spa->spa_state == POOL_STATE_UNINITIALIZED) 1531 return; 1532 1533 for (int c = 0; c < vd->vdev_children; c++) 1534 vdev_hold(vd->vdev_child[c]); 1535 1536 if (vd->vdev_ops->vdev_op_leaf) 1537 vd->vdev_ops->vdev_op_hold(vd); 1538 } 1539 1540 void 1541 vdev_rele(vdev_t *vd) 1542 { 1543 spa_t *spa = vd->vdev_spa; 1544 1545 ASSERT(spa_is_root(spa)); 1546 for (int c = 0; c < vd->vdev_children; c++) 1547 vdev_rele(vd->vdev_child[c]); 1548 1549 if (vd->vdev_ops->vdev_op_leaf) 1550 vd->vdev_ops->vdev_op_rele(vd); 1551 } 1552 1553 /* 1554 * Reopen all interior vdevs and any unopened leaves. We don't actually 1555 * reopen leaf vdevs which had previously been opened as they might deadlock 1556 * on the spa_config_lock. Instead we only obtain the leaf's physical size. 1557 * If the leaf has never been opened then open it, as usual. 1558 */ 1559 void 1560 vdev_reopen(vdev_t *vd) 1561 { 1562 spa_t *spa = vd->vdev_spa; 1563 1564 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 1565 1566 /* set the reopening flag unless we're taking the vdev offline */ 1567 vd->vdev_reopening = !vd->vdev_offline; 1568 vdev_close(vd); 1569 (void) vdev_open(vd); 1570 1571 /* 1572 * Call vdev_validate() here to make sure we have the same device. 1573 * Otherwise, a device with an invalid label could be successfully 1574 * opened in response to vdev_reopen(). 1575 */ 1576 if (vd->vdev_aux) { 1577 (void) vdev_validate_aux(vd); 1578 if (vdev_readable(vd) && vdev_writeable(vd) && 1579 vd->vdev_aux == &spa->spa_l2cache && 1580 !l2arc_vdev_present(vd)) 1581 l2arc_add_vdev(spa, vd); 1582 } else { 1583 (void) vdev_validate(vd, B_TRUE); 1584 } 1585 1586 /* 1587 * Reassess parent vdev's health. 1588 */ 1589 vdev_propagate_state(vd); 1590 } 1591 1592 int 1593 vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing) 1594 { 1595 int error; 1596 1597 /* 1598 * Normally, partial opens (e.g. of a mirror) are allowed. 1599 * For a create, however, we want to fail the request if 1600 * there are any components we can't open. 1601 */ 1602 error = vdev_open(vd); 1603 1604 if (error || vd->vdev_state != VDEV_STATE_HEALTHY) { 1605 vdev_close(vd); 1606 return (error ? error : ENXIO); 1607 } 1608 1609 /* 1610 * Recursively load DTLs and initialize all labels. 1611 */ 1612 if ((error = vdev_dtl_load(vd)) != 0 || 1613 (error = vdev_label_init(vd, txg, isreplacing ? 1614 VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) { 1615 vdev_close(vd); 1616 return (error); 1617 } 1618 1619 return (0); 1620 } 1621 1622 void 1623 vdev_metaslab_set_size(vdev_t *vd) 1624 { 1625 /* 1626 * Aim for roughly metaslabs_per_vdev (default 200) metaslabs per vdev. 1627 */ 1628 vd->vdev_ms_shift = highbit64(vd->vdev_asize / metaslabs_per_vdev); 1629 vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT); 1630 } 1631 1632 void 1633 vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg) 1634 { 1635 ASSERT(vd == vd->vdev_top); 1636 ASSERT(!vd->vdev_ishole); 1637 ASSERT(ISP2(flags)); 1638 ASSERT(spa_writeable(vd->vdev_spa)); 1639 1640 if (flags & VDD_METASLAB) 1641 (void) txg_list_add(&vd->vdev_ms_list, arg, txg); 1642 1643 if (flags & VDD_DTL) 1644 (void) txg_list_add(&vd->vdev_dtl_list, arg, txg); 1645 1646 (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg); 1647 } 1648 1649 void 1650 vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg) 1651 { 1652 for (int c = 0; c < vd->vdev_children; c++) 1653 vdev_dirty_leaves(vd->vdev_child[c], flags, txg); 1654 1655 if (vd->vdev_ops->vdev_op_leaf) 1656 vdev_dirty(vd->vdev_top, flags, vd, txg); 1657 } 1658 1659 /* 1660 * DTLs. 1661 * 1662 * A vdev's DTL (dirty time log) is the set of transaction groups for which 1663 * the vdev has less than perfect replication. There are four kinds of DTL: 1664 * 1665 * DTL_MISSING: txgs for which the vdev has no valid copies of the data 1666 * 1667 * DTL_PARTIAL: txgs for which data is available, but not fully replicated 1668 * 1669 * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon 1670 * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of 1671 * txgs that was scrubbed. 1672 * 1673 * DTL_OUTAGE: txgs which cannot currently be read, whether due to 1674 * persistent errors or just some device being offline. 1675 * Unlike the other three, the DTL_OUTAGE map is not generally 1676 * maintained; it's only computed when needed, typically to 1677 * determine whether a device can be detached. 1678 * 1679 * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device 1680 * either has the data or it doesn't. 1681 * 1682 * For interior vdevs such as mirror and RAID-Z the picture is more complex. 1683 * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because 1684 * if any child is less than fully replicated, then so is its parent. 1685 * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs, 1686 * comprising only those txgs which appear in 'maxfaults' or more children; 1687 * those are the txgs we don't have enough replication to read. For example, 1688 * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2); 1689 * thus, its DTL_MISSING consists of the set of txgs that appear in more than 1690 * two child DTL_MISSING maps. 1691 * 1692 * It should be clear from the above that to compute the DTLs and outage maps 1693 * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps. 1694 * Therefore, that is all we keep on disk. When loading the pool, or after 1695 * a configuration change, we generate all other DTLs from first principles. 1696 */ 1697 void 1698 vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 1699 { 1700 range_tree_t *rt = vd->vdev_dtl[t]; 1701 1702 ASSERT(t < DTL_TYPES); 1703 ASSERT(vd != vd->vdev_spa->spa_root_vdev); 1704 ASSERT(spa_writeable(vd->vdev_spa)); 1705 1706 mutex_enter(rt->rt_lock); 1707 if (!range_tree_contains(rt, txg, size)) 1708 range_tree_add(rt, txg, size); 1709 mutex_exit(rt->rt_lock); 1710 } 1711 1712 boolean_t 1713 vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 1714 { 1715 range_tree_t *rt = vd->vdev_dtl[t]; 1716 boolean_t dirty = B_FALSE; 1717 1718 ASSERT(t < DTL_TYPES); 1719 ASSERT(vd != vd->vdev_spa->spa_root_vdev); 1720 1721 mutex_enter(rt->rt_lock); 1722 if (range_tree_space(rt) != 0) 1723 dirty = range_tree_contains(rt, txg, size); 1724 mutex_exit(rt->rt_lock); 1725 1726 return (dirty); 1727 } 1728 1729 boolean_t 1730 vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t) 1731 { 1732 range_tree_t *rt = vd->vdev_dtl[t]; 1733 boolean_t empty; 1734 1735 mutex_enter(rt->rt_lock); 1736 empty = (range_tree_space(rt) == 0); 1737 mutex_exit(rt->rt_lock); 1738 1739 return (empty); 1740 } 1741 1742 /* 1743 * Returns the lowest txg in the DTL range. 1744 */ 1745 static uint64_t 1746 vdev_dtl_min(vdev_t *vd) 1747 { 1748 range_seg_t *rs; 1749 1750 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock)); 1751 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0); 1752 ASSERT0(vd->vdev_children); 1753 1754 rs = avl_first(&vd->vdev_dtl[DTL_MISSING]->rt_root); 1755 return (rs->rs_start - 1); 1756 } 1757 1758 /* 1759 * Returns the highest txg in the DTL. 1760 */ 1761 static uint64_t 1762 vdev_dtl_max(vdev_t *vd) 1763 { 1764 range_seg_t *rs; 1765 1766 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock)); 1767 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0); 1768 ASSERT0(vd->vdev_children); 1769 1770 rs = avl_last(&vd->vdev_dtl[DTL_MISSING]->rt_root); 1771 return (rs->rs_end); 1772 } 1773 1774 /* 1775 * Determine if a resilvering vdev should remove any DTL entries from 1776 * its range. If the vdev was resilvering for the entire duration of the 1777 * scan then it should excise that range from its DTLs. Otherwise, this 1778 * vdev is considered partially resilvered and should leave its DTL 1779 * entries intact. The comment in vdev_dtl_reassess() describes how we 1780 * excise the DTLs. 1781 */ 1782 static boolean_t 1783 vdev_dtl_should_excise(vdev_t *vd) 1784 { 1785 spa_t *spa = vd->vdev_spa; 1786 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; 1787 1788 ASSERT0(scn->scn_phys.scn_errors); 1789 ASSERT0(vd->vdev_children); 1790 1791 if (vd->vdev_resilver_txg == 0 || 1792 range_tree_space(vd->vdev_dtl[DTL_MISSING]) == 0) 1793 return (B_TRUE); 1794 1795 /* 1796 * When a resilver is initiated the scan will assign the scn_max_txg 1797 * value to the highest txg value that exists in all DTLs. If this 1798 * device's max DTL is not part of this scan (i.e. it is not in 1799 * the range (scn_min_txg, scn_max_txg] then it is not eligible 1800 * for excision. 1801 */ 1802 if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) { 1803 ASSERT3U(scn->scn_phys.scn_min_txg, <=, vdev_dtl_min(vd)); 1804 ASSERT3U(scn->scn_phys.scn_min_txg, <, vd->vdev_resilver_txg); 1805 ASSERT3U(vd->vdev_resilver_txg, <=, scn->scn_phys.scn_max_txg); 1806 return (B_TRUE); 1807 } 1808 return (B_FALSE); 1809 } 1810 1811 /* 1812 * Reassess DTLs after a config change or scrub completion. 1813 */ 1814 void 1815 vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done) 1816 { 1817 spa_t *spa = vd->vdev_spa; 1818 avl_tree_t reftree; 1819 int minref; 1820 1821 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 1822 1823 for (int c = 0; c < vd->vdev_children; c++) 1824 vdev_dtl_reassess(vd->vdev_child[c], txg, 1825 scrub_txg, scrub_done); 1826 1827 if (vd == spa->spa_root_vdev || vd->vdev_ishole || vd->vdev_aux) 1828 return; 1829 1830 if (vd->vdev_ops->vdev_op_leaf) { 1831 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; 1832 1833 mutex_enter(&vd->vdev_dtl_lock); 1834 1835 /* 1836 * If we've completed a scan cleanly then determine 1837 * if this vdev should remove any DTLs. We only want to 1838 * excise regions on vdevs that were available during 1839 * the entire duration of this scan. 1840 */ 1841 if (scrub_txg != 0 && 1842 (spa->spa_scrub_started || 1843 (scn != NULL && scn->scn_phys.scn_errors == 0)) && 1844 vdev_dtl_should_excise(vd)) { 1845 /* 1846 * We completed a scrub up to scrub_txg. If we 1847 * did it without rebooting, then the scrub dtl 1848 * will be valid, so excise the old region and 1849 * fold in the scrub dtl. Otherwise, leave the 1850 * dtl as-is if there was an error. 1851 * 1852 * There's little trick here: to excise the beginning 1853 * of the DTL_MISSING map, we put it into a reference 1854 * tree and then add a segment with refcnt -1 that 1855 * covers the range [0, scrub_txg). This means 1856 * that each txg in that range has refcnt -1 or 0. 1857 * We then add DTL_SCRUB with a refcnt of 2, so that 1858 * entries in the range [0, scrub_txg) will have a 1859 * positive refcnt -- either 1 or 2. We then convert 1860 * the reference tree into the new DTL_MISSING map. 1861 */ 1862 space_reftree_create(&reftree); 1863 space_reftree_add_map(&reftree, 1864 vd->vdev_dtl[DTL_MISSING], 1); 1865 space_reftree_add_seg(&reftree, 0, scrub_txg, -1); 1866 space_reftree_add_map(&reftree, 1867 vd->vdev_dtl[DTL_SCRUB], 2); 1868 space_reftree_generate_map(&reftree, 1869 vd->vdev_dtl[DTL_MISSING], 1); 1870 space_reftree_destroy(&reftree); 1871 } 1872 range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL); 1873 range_tree_walk(vd->vdev_dtl[DTL_MISSING], 1874 range_tree_add, vd->vdev_dtl[DTL_PARTIAL]); 1875 if (scrub_done) 1876 range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL); 1877 range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL); 1878 if (!vdev_readable(vd)) 1879 range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL); 1880 else 1881 range_tree_walk(vd->vdev_dtl[DTL_MISSING], 1882 range_tree_add, vd->vdev_dtl[DTL_OUTAGE]); 1883 1884 /* 1885 * If the vdev was resilvering and no longer has any 1886 * DTLs then reset its resilvering flag. 1887 */ 1888 if (vd->vdev_resilver_txg != 0 && 1889 range_tree_space(vd->vdev_dtl[DTL_MISSING]) == 0 && 1890 range_tree_space(vd->vdev_dtl[DTL_OUTAGE]) == 0) 1891 vd->vdev_resilver_txg = 0; 1892 1893 mutex_exit(&vd->vdev_dtl_lock); 1894 1895 if (txg != 0) 1896 vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg); 1897 return; 1898 } 1899 1900 mutex_enter(&vd->vdev_dtl_lock); 1901 for (int t = 0; t < DTL_TYPES; t++) { 1902 /* account for child's outage in parent's missing map */ 1903 int s = (t == DTL_MISSING) ? DTL_OUTAGE: t; 1904 if (t == DTL_SCRUB) 1905 continue; /* leaf vdevs only */ 1906 if (t == DTL_PARTIAL) 1907 minref = 1; /* i.e. non-zero */ 1908 else if (vd->vdev_nparity != 0) 1909 minref = vd->vdev_nparity + 1; /* RAID-Z */ 1910 else 1911 minref = vd->vdev_children; /* any kind of mirror */ 1912 space_reftree_create(&reftree); 1913 for (int c = 0; c < vd->vdev_children; c++) { 1914 vdev_t *cvd = vd->vdev_child[c]; 1915 mutex_enter(&cvd->vdev_dtl_lock); 1916 space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1); 1917 mutex_exit(&cvd->vdev_dtl_lock); 1918 } 1919 space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref); 1920 space_reftree_destroy(&reftree); 1921 } 1922 mutex_exit(&vd->vdev_dtl_lock); 1923 } 1924 1925 int 1926 vdev_dtl_load(vdev_t *vd) 1927 { 1928 spa_t *spa = vd->vdev_spa; 1929 objset_t *mos = spa->spa_meta_objset; 1930 int error = 0; 1931 1932 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) { 1933 ASSERT(!vd->vdev_ishole); 1934 1935 error = space_map_open(&vd->vdev_dtl_sm, mos, 1936 vd->vdev_dtl_object, 0, -1ULL, 0, &vd->vdev_dtl_lock); 1937 if (error) 1938 return (error); 1939 ASSERT(vd->vdev_dtl_sm != NULL); 1940 1941 mutex_enter(&vd->vdev_dtl_lock); 1942 1943 /* 1944 * Now that we've opened the space_map we need to update 1945 * the in-core DTL. 1946 */ 1947 space_map_update(vd->vdev_dtl_sm); 1948 1949 error = space_map_load(vd->vdev_dtl_sm, 1950 vd->vdev_dtl[DTL_MISSING], SM_ALLOC); 1951 mutex_exit(&vd->vdev_dtl_lock); 1952 1953 return (error); 1954 } 1955 1956 for (int c = 0; c < vd->vdev_children; c++) { 1957 error = vdev_dtl_load(vd->vdev_child[c]); 1958 if (error != 0) 1959 break; 1960 } 1961 1962 return (error); 1963 } 1964 1965 void 1966 vdev_destroy_unlink_zap(vdev_t *vd, uint64_t zapobj, dmu_tx_t *tx) 1967 { 1968 spa_t *spa = vd->vdev_spa; 1969 1970 VERIFY0(zap_destroy(spa->spa_meta_objset, zapobj, tx)); 1971 VERIFY0(zap_remove_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps, 1972 zapobj, tx)); 1973 } 1974 1975 uint64_t 1976 vdev_create_link_zap(vdev_t *vd, dmu_tx_t *tx) 1977 { 1978 spa_t *spa = vd->vdev_spa; 1979 uint64_t zap = zap_create(spa->spa_meta_objset, DMU_OTN_ZAP_METADATA, 1980 DMU_OT_NONE, 0, tx); 1981 1982 ASSERT(zap != 0); 1983 VERIFY0(zap_add_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps, 1984 zap, tx)); 1985 1986 return (zap); 1987 } 1988 1989 void 1990 vdev_construct_zaps(vdev_t *vd, dmu_tx_t *tx) 1991 { 1992 if (vd->vdev_ops != &vdev_hole_ops && 1993 vd->vdev_ops != &vdev_missing_ops && 1994 vd->vdev_ops != &vdev_root_ops && 1995 !vd->vdev_top->vdev_removing) { 1996 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_leaf_zap == 0) { 1997 vd->vdev_leaf_zap = vdev_create_link_zap(vd, tx); 1998 } 1999 if (vd == vd->vdev_top && vd->vdev_top_zap == 0) { 2000 vd->vdev_top_zap = vdev_create_link_zap(vd, tx); 2001 } 2002 } 2003 for (uint64_t i = 0; i < vd->vdev_children; i++) { 2004 vdev_construct_zaps(vd->vdev_child[i], tx); 2005 } 2006 } 2007 2008 void 2009 vdev_dtl_sync(vdev_t *vd, uint64_t txg) 2010 { 2011 spa_t *spa = vd->vdev_spa; 2012 range_tree_t *rt = vd->vdev_dtl[DTL_MISSING]; 2013 objset_t *mos = spa->spa_meta_objset; 2014 range_tree_t *rtsync; 2015 kmutex_t rtlock; 2016 dmu_tx_t *tx; 2017 uint64_t object = space_map_object(vd->vdev_dtl_sm); 2018 2019 ASSERT(!vd->vdev_ishole); 2020 ASSERT(vd->vdev_ops->vdev_op_leaf); 2021 2022 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 2023 2024 if (vd->vdev_detached || vd->vdev_top->vdev_removing) { 2025 mutex_enter(&vd->vdev_dtl_lock); 2026 space_map_free(vd->vdev_dtl_sm, tx); 2027 space_map_close(vd->vdev_dtl_sm); 2028 vd->vdev_dtl_sm = NULL; 2029 mutex_exit(&vd->vdev_dtl_lock); 2030 2031 /* 2032 * We only destroy the leaf ZAP for detached leaves or for 2033 * removed log devices. Removed data devices handle leaf ZAP 2034 * cleanup later, once cancellation is no longer possible. 2035 */ 2036 if (vd->vdev_leaf_zap != 0 && (vd->vdev_detached || 2037 vd->vdev_top->vdev_islog)) { 2038 vdev_destroy_unlink_zap(vd, vd->vdev_leaf_zap, tx); 2039 vd->vdev_leaf_zap = 0; 2040 } 2041 2042 dmu_tx_commit(tx); 2043 return; 2044 } 2045 2046 if (vd->vdev_dtl_sm == NULL) { 2047 uint64_t new_object; 2048 2049 new_object = space_map_alloc(mos, tx); 2050 VERIFY3U(new_object, !=, 0); 2051 2052 VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object, 2053 0, -1ULL, 0, &vd->vdev_dtl_lock)); 2054 ASSERT(vd->vdev_dtl_sm != NULL); 2055 } 2056 2057 mutex_init(&rtlock, NULL, MUTEX_DEFAULT, NULL); 2058 2059 rtsync = range_tree_create(NULL, NULL, &rtlock); 2060 2061 mutex_enter(&rtlock); 2062 2063 mutex_enter(&vd->vdev_dtl_lock); 2064 range_tree_walk(rt, range_tree_add, rtsync); 2065 mutex_exit(&vd->vdev_dtl_lock); 2066 2067 space_map_truncate(vd->vdev_dtl_sm, tx); 2068 space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, tx); 2069 range_tree_vacate(rtsync, NULL, NULL); 2070 2071 range_tree_destroy(rtsync); 2072 2073 mutex_exit(&rtlock); 2074 mutex_destroy(&rtlock); 2075 2076 /* 2077 * If the object for the space map has changed then dirty 2078 * the top level so that we update the config. 2079 */ 2080 if (object != space_map_object(vd->vdev_dtl_sm)) { 2081 zfs_dbgmsg("txg %llu, spa %s, DTL old object %llu, " 2082 "new object %llu", txg, spa_name(spa), object, 2083 space_map_object(vd->vdev_dtl_sm)); 2084 vdev_config_dirty(vd->vdev_top); 2085 } 2086 2087 dmu_tx_commit(tx); 2088 2089 mutex_enter(&vd->vdev_dtl_lock); 2090 space_map_update(vd->vdev_dtl_sm); 2091 mutex_exit(&vd->vdev_dtl_lock); 2092 } 2093 2094 /* 2095 * Determine whether the specified vdev can be offlined/detached/removed 2096 * without losing data. 2097 */ 2098 boolean_t 2099 vdev_dtl_required(vdev_t *vd) 2100 { 2101 spa_t *spa = vd->vdev_spa; 2102 vdev_t *tvd = vd->vdev_top; 2103 uint8_t cant_read = vd->vdev_cant_read; 2104 boolean_t required; 2105 2106 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 2107 2108 if (vd == spa->spa_root_vdev || vd == tvd) 2109 return (B_TRUE); 2110 2111 /* 2112 * Temporarily mark the device as unreadable, and then determine 2113 * whether this results in any DTL outages in the top-level vdev. 2114 * If not, we can safely offline/detach/remove the device. 2115 */ 2116 vd->vdev_cant_read = B_TRUE; 2117 vdev_dtl_reassess(tvd, 0, 0, B_FALSE); 2118 required = !vdev_dtl_empty(tvd, DTL_OUTAGE); 2119 vd->vdev_cant_read = cant_read; 2120 vdev_dtl_reassess(tvd, 0, 0, B_FALSE); 2121 2122 if (!required && zio_injection_enabled) 2123 required = !!zio_handle_device_injection(vd, NULL, ECHILD); 2124 2125 return (required); 2126 } 2127 2128 /* 2129 * Determine if resilver is needed, and if so the txg range. 2130 */ 2131 boolean_t 2132 vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp) 2133 { 2134 boolean_t needed = B_FALSE; 2135 uint64_t thismin = UINT64_MAX; 2136 uint64_t thismax = 0; 2137 2138 if (vd->vdev_children == 0) { 2139 mutex_enter(&vd->vdev_dtl_lock); 2140 if (range_tree_space(vd->vdev_dtl[DTL_MISSING]) != 0 && 2141 vdev_writeable(vd)) { 2142 2143 thismin = vdev_dtl_min(vd); 2144 thismax = vdev_dtl_max(vd); 2145 needed = B_TRUE; 2146 } 2147 mutex_exit(&vd->vdev_dtl_lock); 2148 } else { 2149 for (int c = 0; c < vd->vdev_children; c++) { 2150 vdev_t *cvd = vd->vdev_child[c]; 2151 uint64_t cmin, cmax; 2152 2153 if (vdev_resilver_needed(cvd, &cmin, &cmax)) { 2154 thismin = MIN(thismin, cmin); 2155 thismax = MAX(thismax, cmax); 2156 needed = B_TRUE; 2157 } 2158 } 2159 } 2160 2161 if (needed && minp) { 2162 *minp = thismin; 2163 *maxp = thismax; 2164 } 2165 return (needed); 2166 } 2167 2168 void 2169 vdev_load(vdev_t *vd) 2170 { 2171 /* 2172 * Recursively load all children. 2173 */ 2174 for (int c = 0; c < vd->vdev_children; c++) 2175 vdev_load(vd->vdev_child[c]); 2176 2177 /* 2178 * If this is a top-level vdev, initialize its metaslabs. 2179 */ 2180 if (vd == vd->vdev_top && !vd->vdev_ishole && 2181 (vd->vdev_ashift == 0 || vd->vdev_asize == 0 || 2182 vdev_metaslab_init(vd, 0) != 0)) 2183 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 2184 VDEV_AUX_CORRUPT_DATA); 2185 2186 /* 2187 * If this is a leaf vdev, load its DTL. 2188 */ 2189 if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0) 2190 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 2191 VDEV_AUX_CORRUPT_DATA); 2192 } 2193 2194 /* 2195 * The special vdev case is used for hot spares and l2cache devices. Its 2196 * sole purpose it to set the vdev state for the associated vdev. To do this, 2197 * we make sure that we can open the underlying device, then try to read the 2198 * label, and make sure that the label is sane and that it hasn't been 2199 * repurposed to another pool. 2200 */ 2201 int 2202 vdev_validate_aux(vdev_t *vd) 2203 { 2204 nvlist_t *label; 2205 uint64_t guid, version; 2206 uint64_t state; 2207 2208 if (!vdev_readable(vd)) 2209 return (0); 2210 2211 if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) { 2212 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 2213 VDEV_AUX_CORRUPT_DATA); 2214 return (-1); 2215 } 2216 2217 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 || 2218 !SPA_VERSION_IS_SUPPORTED(version) || 2219 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 || 2220 guid != vd->vdev_guid || 2221 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) { 2222 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 2223 VDEV_AUX_CORRUPT_DATA); 2224 nvlist_free(label); 2225 return (-1); 2226 } 2227 2228 /* 2229 * We don't actually check the pool state here. If it's in fact in 2230 * use by another pool, we update this fact on the fly when requested. 2231 */ 2232 nvlist_free(label); 2233 return (0); 2234 } 2235 2236 void 2237 vdev_remove(vdev_t *vd, uint64_t txg) 2238 { 2239 spa_t *spa = vd->vdev_spa; 2240 objset_t *mos = spa->spa_meta_objset; 2241 dmu_tx_t *tx; 2242 2243 tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg); 2244 ASSERT(vd == vd->vdev_top); 2245 ASSERT3U(txg, ==, spa_syncing_txg(spa)); 2246 2247 if (vd->vdev_ms != NULL) { 2248 metaslab_group_t *mg = vd->vdev_mg; 2249 2250 metaslab_group_histogram_verify(mg); 2251 metaslab_class_histogram_verify(mg->mg_class); 2252 2253 for (int m = 0; m < vd->vdev_ms_count; m++) { 2254 metaslab_t *msp = vd->vdev_ms[m]; 2255 2256 if (msp == NULL || msp->ms_sm == NULL) 2257 continue; 2258 2259 mutex_enter(&msp->ms_lock); 2260 /* 2261 * If the metaslab was not loaded when the vdev 2262 * was removed then the histogram accounting may 2263 * not be accurate. Update the histogram information 2264 * here so that we ensure that the metaslab group 2265 * and metaslab class are up-to-date. 2266 */ 2267 metaslab_group_histogram_remove(mg, msp); 2268 2269 VERIFY0(space_map_allocated(msp->ms_sm)); 2270 space_map_free(msp->ms_sm, tx); 2271 space_map_close(msp->ms_sm); 2272 msp->ms_sm = NULL; 2273 mutex_exit(&msp->ms_lock); 2274 } 2275 2276 metaslab_group_histogram_verify(mg); 2277 metaslab_class_histogram_verify(mg->mg_class); 2278 for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++) 2279 ASSERT0(mg->mg_histogram[i]); 2280 2281 } 2282 2283 if (vd->vdev_ms_array) { 2284 (void) dmu_object_free(mos, vd->vdev_ms_array, tx); 2285 vd->vdev_ms_array = 0; 2286 } 2287 2288 if (vd->vdev_islog && vd->vdev_top_zap != 0) { 2289 vdev_destroy_unlink_zap(vd, vd->vdev_top_zap, tx); 2290 vd->vdev_top_zap = 0; 2291 } 2292 dmu_tx_commit(tx); 2293 } 2294 2295 void 2296 vdev_sync_done(vdev_t *vd, uint64_t txg) 2297 { 2298 metaslab_t *msp; 2299 boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg)); 2300 2301 ASSERT(!vd->vdev_ishole); 2302 2303 while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg))) 2304 metaslab_sync_done(msp, txg); 2305 2306 if (reassess) 2307 metaslab_sync_reassess(vd->vdev_mg); 2308 } 2309 2310 void 2311 vdev_sync(vdev_t *vd, uint64_t txg) 2312 { 2313 spa_t *spa = vd->vdev_spa; 2314 vdev_t *lvd; 2315 metaslab_t *msp; 2316 dmu_tx_t *tx; 2317 2318 ASSERT(!vd->vdev_ishole); 2319 2320 if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) { 2321 ASSERT(vd == vd->vdev_top); 2322 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 2323 vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset, 2324 DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx); 2325 ASSERT(vd->vdev_ms_array != 0); 2326 vdev_config_dirty(vd); 2327 dmu_tx_commit(tx); 2328 } 2329 2330 /* 2331 * Remove the metadata associated with this vdev once it's empty. 2332 */ 2333 if (vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing) 2334 vdev_remove(vd, txg); 2335 2336 while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) { 2337 metaslab_sync(msp, txg); 2338 (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg)); 2339 } 2340 2341 while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL) 2342 vdev_dtl_sync(lvd, txg); 2343 2344 (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)); 2345 } 2346 2347 uint64_t 2348 vdev_psize_to_asize(vdev_t *vd, uint64_t psize) 2349 { 2350 return (vd->vdev_ops->vdev_op_asize(vd, psize)); 2351 } 2352 2353 /* 2354 * Mark the given vdev faulted. A faulted vdev behaves as if the device could 2355 * not be opened, and no I/O is attempted. 2356 */ 2357 int 2358 vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux) 2359 { 2360 vdev_t *vd, *tvd; 2361 2362 spa_vdev_state_enter(spa, SCL_NONE); 2363 2364 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 2365 return (spa_vdev_state_exit(spa, NULL, ENODEV)); 2366 2367 if (!vd->vdev_ops->vdev_op_leaf) 2368 return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 2369 2370 tvd = vd->vdev_top; 2371 2372 /* 2373 * We don't directly use the aux state here, but if we do a 2374 * vdev_reopen(), we need this value to be present to remember why we 2375 * were faulted. 2376 */ 2377 vd->vdev_label_aux = aux; 2378 2379 /* 2380 * Faulted state takes precedence over degraded. 2381 */ 2382 vd->vdev_delayed_close = B_FALSE; 2383 vd->vdev_faulted = 1ULL; 2384 vd->vdev_degraded = 0ULL; 2385 vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux); 2386 2387 /* 2388 * If this device has the only valid copy of the data, then 2389 * back off and simply mark the vdev as degraded instead. 2390 */ 2391 if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) { 2392 vd->vdev_degraded = 1ULL; 2393 vd->vdev_faulted = 0ULL; 2394 2395 /* 2396 * If we reopen the device and it's not dead, only then do we 2397 * mark it degraded. 2398 */ 2399 vdev_reopen(tvd); 2400 2401 if (vdev_readable(vd)) 2402 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux); 2403 } 2404 2405 return (spa_vdev_state_exit(spa, vd, 0)); 2406 } 2407 2408 /* 2409 * Mark the given vdev degraded. A degraded vdev is purely an indication to the 2410 * user that something is wrong. The vdev continues to operate as normal as far 2411 * as I/O is concerned. 2412 */ 2413 int 2414 vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux) 2415 { 2416 vdev_t *vd; 2417 2418 spa_vdev_state_enter(spa, SCL_NONE); 2419 2420 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 2421 return (spa_vdev_state_exit(spa, NULL, ENODEV)); 2422 2423 if (!vd->vdev_ops->vdev_op_leaf) 2424 return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 2425 2426 /* 2427 * If the vdev is already faulted, then don't do anything. 2428 */ 2429 if (vd->vdev_faulted || vd->vdev_degraded) 2430 return (spa_vdev_state_exit(spa, NULL, 0)); 2431 2432 vd->vdev_degraded = 1ULL; 2433 if (!vdev_is_dead(vd)) 2434 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, 2435 aux); 2436 2437 return (spa_vdev_state_exit(spa, vd, 0)); 2438 } 2439 2440 /* 2441 * Online the given vdev. 2442 * 2443 * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached 2444 * spare device should be detached when the device finishes resilvering. 2445 * Second, the online should be treated like a 'test' online case, so no FMA 2446 * events are generated if the device fails to open. 2447 */ 2448 int 2449 vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate) 2450 { 2451 vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev; 2452 boolean_t wasoffline; 2453 vdev_state_t oldstate; 2454 2455 spa_vdev_state_enter(spa, SCL_NONE); 2456 2457 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 2458 return (spa_vdev_state_exit(spa, NULL, ENODEV)); 2459 2460 if (!vd->vdev_ops->vdev_op_leaf) 2461 return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 2462 2463 wasoffline = (vd->vdev_offline || vd->vdev_tmpoffline); 2464 oldstate = vd->vdev_state; 2465 2466 tvd = vd->vdev_top; 2467 vd->vdev_offline = B_FALSE; 2468 vd->vdev_tmpoffline = B_FALSE; 2469 vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE); 2470 vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT); 2471 2472 /* XXX - L2ARC 1.0 does not support expansion */ 2473 if (!vd->vdev_aux) { 2474 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 2475 pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND); 2476 } 2477 2478 vdev_reopen(tvd); 2479 vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE; 2480 2481 if (!vd->vdev_aux) { 2482 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 2483 pvd->vdev_expanding = B_FALSE; 2484 } 2485 2486 if (newstate) 2487 *newstate = vd->vdev_state; 2488 if ((flags & ZFS_ONLINE_UNSPARE) && 2489 !vdev_is_dead(vd) && vd->vdev_parent && 2490 vd->vdev_parent->vdev_ops == &vdev_spare_ops && 2491 vd->vdev_parent->vdev_child[0] == vd) 2492 vd->vdev_unspare = B_TRUE; 2493 2494 if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) { 2495 2496 /* XXX - L2ARC 1.0 does not support expansion */ 2497 if (vd->vdev_aux) 2498 return (spa_vdev_state_exit(spa, vd, ENOTSUP)); 2499 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 2500 } 2501 2502 if (wasoffline || 2503 (oldstate < VDEV_STATE_DEGRADED && 2504 vd->vdev_state >= VDEV_STATE_DEGRADED)) 2505 spa_event_notify(spa, vd, ESC_ZFS_VDEV_ONLINE); 2506 2507 return (spa_vdev_state_exit(spa, vd, 0)); 2508 } 2509 2510 static int 2511 vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags) 2512 { 2513 vdev_t *vd, *tvd; 2514 int error = 0; 2515 uint64_t generation; 2516 metaslab_group_t *mg; 2517 2518 top: 2519 spa_vdev_state_enter(spa, SCL_ALLOC); 2520 2521 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 2522 return (spa_vdev_state_exit(spa, NULL, ENODEV)); 2523 2524 if (!vd->vdev_ops->vdev_op_leaf) 2525 return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 2526 2527 tvd = vd->vdev_top; 2528 mg = tvd->vdev_mg; 2529 generation = spa->spa_config_generation + 1; 2530 2531 /* 2532 * If the device isn't already offline, try to offline it. 2533 */ 2534 if (!vd->vdev_offline) { 2535 /* 2536 * If this device has the only valid copy of some data, 2537 * don't allow it to be offlined. Log devices are always 2538 * expendable. 2539 */ 2540 if (!tvd->vdev_islog && vd->vdev_aux == NULL && 2541 vdev_dtl_required(vd)) 2542 return (spa_vdev_state_exit(spa, NULL, EBUSY)); 2543 2544 /* 2545 * If the top-level is a slog and it has had allocations 2546 * then proceed. We check that the vdev's metaslab group 2547 * is not NULL since it's possible that we may have just 2548 * added this vdev but not yet initialized its metaslabs. 2549 */ 2550 if (tvd->vdev_islog && mg != NULL) { 2551 /* 2552 * Prevent any future allocations. 2553 */ 2554 metaslab_group_passivate(mg); 2555 (void) spa_vdev_state_exit(spa, vd, 0); 2556 2557 error = spa_offline_log(spa); 2558 2559 spa_vdev_state_enter(spa, SCL_ALLOC); 2560 2561 /* 2562 * Check to see if the config has changed. 2563 */ 2564 if (error || generation != spa->spa_config_generation) { 2565 metaslab_group_activate(mg); 2566 if (error) 2567 return (spa_vdev_state_exit(spa, 2568 vd, error)); 2569 (void) spa_vdev_state_exit(spa, vd, 0); 2570 goto top; 2571 } 2572 ASSERT0(tvd->vdev_stat.vs_alloc); 2573 } 2574 2575 /* 2576 * Offline this device and reopen its top-level vdev. 2577 * If the top-level vdev is a log device then just offline 2578 * it. Otherwise, if this action results in the top-level 2579 * vdev becoming unusable, undo it and fail the request. 2580 */ 2581 vd->vdev_offline = B_TRUE; 2582 vdev_reopen(tvd); 2583 2584 if (!tvd->vdev_islog && vd->vdev_aux == NULL && 2585 vdev_is_dead(tvd)) { 2586 vd->vdev_offline = B_FALSE; 2587 vdev_reopen(tvd); 2588 return (spa_vdev_state_exit(spa, NULL, EBUSY)); 2589 } 2590 2591 /* 2592 * Add the device back into the metaslab rotor so that 2593 * once we online the device it's open for business. 2594 */ 2595 if (tvd->vdev_islog && mg != NULL) 2596 metaslab_group_activate(mg); 2597 } 2598 2599 vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY); 2600 2601 return (spa_vdev_state_exit(spa, vd, 0)); 2602 } 2603 2604 int 2605 vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags) 2606 { 2607 int error; 2608 2609 mutex_enter(&spa->spa_vdev_top_lock); 2610 error = vdev_offline_locked(spa, guid, flags); 2611 mutex_exit(&spa->spa_vdev_top_lock); 2612 2613 return (error); 2614 } 2615 2616 /* 2617 * Clear the error counts associated with this vdev. Unlike vdev_online() and 2618 * vdev_offline(), we assume the spa config is locked. We also clear all 2619 * children. If 'vd' is NULL, then the user wants to clear all vdevs. 2620 */ 2621 void 2622 vdev_clear(spa_t *spa, vdev_t *vd) 2623 { 2624 vdev_t *rvd = spa->spa_root_vdev; 2625 2626 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 2627 2628 if (vd == NULL) 2629 vd = rvd; 2630 2631 vd->vdev_stat.vs_read_errors = 0; 2632 vd->vdev_stat.vs_write_errors = 0; 2633 vd->vdev_stat.vs_checksum_errors = 0; 2634 2635 for (int c = 0; c < vd->vdev_children; c++) 2636 vdev_clear(spa, vd->vdev_child[c]); 2637 2638 /* 2639 * If we're in the FAULTED state or have experienced failed I/O, then 2640 * clear the persistent state and attempt to reopen the device. We 2641 * also mark the vdev config dirty, so that the new faulted state is 2642 * written out to disk. 2643 */ 2644 if (vd->vdev_faulted || vd->vdev_degraded || 2645 !vdev_readable(vd) || !vdev_writeable(vd)) { 2646 2647 /* 2648 * When reopening in reponse to a clear event, it may be due to 2649 * a fmadm repair request. In this case, if the device is 2650 * still broken, we want to still post the ereport again. 2651 */ 2652 vd->vdev_forcefault = B_TRUE; 2653 2654 vd->vdev_faulted = vd->vdev_degraded = 0ULL; 2655 vd->vdev_cant_read = B_FALSE; 2656 vd->vdev_cant_write = B_FALSE; 2657 2658 vdev_reopen(vd == rvd ? rvd : vd->vdev_top); 2659 2660 vd->vdev_forcefault = B_FALSE; 2661 2662 if (vd != rvd && vdev_writeable(vd->vdev_top)) 2663 vdev_state_dirty(vd->vdev_top); 2664 2665 if (vd->vdev_aux == NULL && !vdev_is_dead(vd)) 2666 spa_async_request(spa, SPA_ASYNC_RESILVER); 2667 2668 spa_event_notify(spa, vd, ESC_ZFS_VDEV_CLEAR); 2669 } 2670 2671 /* 2672 * When clearing a FMA-diagnosed fault, we always want to 2673 * unspare the device, as we assume that the original spare was 2674 * done in response to the FMA fault. 2675 */ 2676 if (!vdev_is_dead(vd) && vd->vdev_parent != NULL && 2677 vd->vdev_parent->vdev_ops == &vdev_spare_ops && 2678 vd->vdev_parent->vdev_child[0] == vd) 2679 vd->vdev_unspare = B_TRUE; 2680 } 2681 2682 boolean_t 2683 vdev_is_dead(vdev_t *vd) 2684 { 2685 /* 2686 * Holes and missing devices are always considered "dead". 2687 * This simplifies the code since we don't have to check for 2688 * these types of devices in the various code paths. 2689 * Instead we rely on the fact that we skip over dead devices 2690 * before issuing I/O to them. 2691 */ 2692 return (vd->vdev_state < VDEV_STATE_DEGRADED || vd->vdev_ishole || 2693 vd->vdev_ops == &vdev_missing_ops); 2694 } 2695 2696 boolean_t 2697 vdev_readable(vdev_t *vd) 2698 { 2699 return (!vdev_is_dead(vd) && !vd->vdev_cant_read); 2700 } 2701 2702 boolean_t 2703 vdev_writeable(vdev_t *vd) 2704 { 2705 return (!vdev_is_dead(vd) && !vd->vdev_cant_write); 2706 } 2707 2708 boolean_t 2709 vdev_allocatable(vdev_t *vd) 2710 { 2711 uint64_t state = vd->vdev_state; 2712 2713 /* 2714 * We currently allow allocations from vdevs which may be in the 2715 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device 2716 * fails to reopen then we'll catch it later when we're holding 2717 * the proper locks. Note that we have to get the vdev state 2718 * in a local variable because although it changes atomically, 2719 * we're asking two separate questions about it. 2720 */ 2721 return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) && 2722 !vd->vdev_cant_write && !vd->vdev_ishole && 2723 vd->vdev_mg->mg_initialized); 2724 } 2725 2726 boolean_t 2727 vdev_accessible(vdev_t *vd, zio_t *zio) 2728 { 2729 ASSERT(zio->io_vd == vd); 2730 2731 if (vdev_is_dead(vd) || vd->vdev_remove_wanted) 2732 return (B_FALSE); 2733 2734 if (zio->io_type == ZIO_TYPE_READ) 2735 return (!vd->vdev_cant_read); 2736 2737 if (zio->io_type == ZIO_TYPE_WRITE) 2738 return (!vd->vdev_cant_write); 2739 2740 return (B_TRUE); 2741 } 2742 2743 /* 2744 * Get statistics for the given vdev. 2745 */ 2746 void 2747 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs) 2748 { 2749 spa_t *spa = vd->vdev_spa; 2750 vdev_t *rvd = spa->spa_root_vdev; 2751 vdev_t *tvd = vd->vdev_top; 2752 2753 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 2754 2755 mutex_enter(&vd->vdev_stat_lock); 2756 bcopy(&vd->vdev_stat, vs, sizeof (*vs)); 2757 vs->vs_timestamp = gethrtime() - vs->vs_timestamp; 2758 vs->vs_state = vd->vdev_state; 2759 vs->vs_rsize = vdev_get_min_asize(vd); 2760 if (vd->vdev_ops->vdev_op_leaf) 2761 vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE; 2762 /* 2763 * Report expandable space on top-level, non-auxillary devices only. 2764 * The expandable space is reported in terms of metaslab sized units 2765 * since that determines how much space the pool can expand. 2766 */ 2767 if (vd->vdev_aux == NULL && tvd != NULL) { 2768 vs->vs_esize = P2ALIGN(vd->vdev_max_asize - vd->vdev_asize, 2769 1ULL << tvd->vdev_ms_shift); 2770 } 2771 if (vd->vdev_aux == NULL && vd == vd->vdev_top && !vd->vdev_ishole) { 2772 vs->vs_fragmentation = vd->vdev_mg->mg_fragmentation; 2773 } 2774 2775 /* 2776 * If we're getting stats on the root vdev, aggregate the I/O counts 2777 * over all top-level vdevs (i.e. the direct children of the root). 2778 */ 2779 if (vd == rvd) { 2780 for (int c = 0; c < rvd->vdev_children; c++) { 2781 vdev_t *cvd = rvd->vdev_child[c]; 2782 vdev_stat_t *cvs = &cvd->vdev_stat; 2783 2784 for (int t = 0; t < ZIO_TYPES; t++) { 2785 vs->vs_ops[t] += cvs->vs_ops[t]; 2786 vs->vs_bytes[t] += cvs->vs_bytes[t]; 2787 } 2788 cvs->vs_scan_removing = cvd->vdev_removing; 2789 } 2790 } 2791 mutex_exit(&vd->vdev_stat_lock); 2792 } 2793 2794 void 2795 vdev_clear_stats(vdev_t *vd) 2796 { 2797 mutex_enter(&vd->vdev_stat_lock); 2798 vd->vdev_stat.vs_space = 0; 2799 vd->vdev_stat.vs_dspace = 0; 2800 vd->vdev_stat.vs_alloc = 0; 2801 mutex_exit(&vd->vdev_stat_lock); 2802 } 2803 2804 void 2805 vdev_scan_stat_init(vdev_t *vd) 2806 { 2807 vdev_stat_t *vs = &vd->vdev_stat; 2808 2809 for (int c = 0; c < vd->vdev_children; c++) 2810 vdev_scan_stat_init(vd->vdev_child[c]); 2811 2812 mutex_enter(&vd->vdev_stat_lock); 2813 vs->vs_scan_processed = 0; 2814 mutex_exit(&vd->vdev_stat_lock); 2815 } 2816 2817 void 2818 vdev_stat_update(zio_t *zio, uint64_t psize) 2819 { 2820 spa_t *spa = zio->io_spa; 2821 vdev_t *rvd = spa->spa_root_vdev; 2822 vdev_t *vd = zio->io_vd ? zio->io_vd : rvd; 2823 vdev_t *pvd; 2824 uint64_t txg = zio->io_txg; 2825 vdev_stat_t *vs = &vd->vdev_stat; 2826 zio_type_t type = zio->io_type; 2827 int flags = zio->io_flags; 2828 2829 /* 2830 * If this i/o is a gang leader, it didn't do any actual work. 2831 */ 2832 if (zio->io_gang_tree) 2833 return; 2834 2835 if (zio->io_error == 0) { 2836 /* 2837 * If this is a root i/o, don't count it -- we've already 2838 * counted the top-level vdevs, and vdev_get_stats() will 2839 * aggregate them when asked. This reduces contention on 2840 * the root vdev_stat_lock and implicitly handles blocks 2841 * that compress away to holes, for which there is no i/o. 2842 * (Holes never create vdev children, so all the counters 2843 * remain zero, which is what we want.) 2844 * 2845 * Note: this only applies to successful i/o (io_error == 0) 2846 * because unlike i/o counts, errors are not additive. 2847 * When reading a ditto block, for example, failure of 2848 * one top-level vdev does not imply a root-level error. 2849 */ 2850 if (vd == rvd) 2851 return; 2852 2853 ASSERT(vd == zio->io_vd); 2854 2855 if (flags & ZIO_FLAG_IO_BYPASS) 2856 return; 2857 2858 mutex_enter(&vd->vdev_stat_lock); 2859 2860 if (flags & ZIO_FLAG_IO_REPAIR) { 2861 if (flags & ZIO_FLAG_SCAN_THREAD) { 2862 dsl_scan_phys_t *scn_phys = 2863 &spa->spa_dsl_pool->dp_scan->scn_phys; 2864 uint64_t *processed = &scn_phys->scn_processed; 2865 2866 /* XXX cleanup? */ 2867 if (vd->vdev_ops->vdev_op_leaf) 2868 atomic_add_64(processed, psize); 2869 vs->vs_scan_processed += psize; 2870 } 2871 2872 if (flags & ZIO_FLAG_SELF_HEAL) 2873 vs->vs_self_healed += psize; 2874 } 2875 2876 vs->vs_ops[type]++; 2877 vs->vs_bytes[type] += psize; 2878 2879 mutex_exit(&vd->vdev_stat_lock); 2880 return; 2881 } 2882 2883 if (flags & ZIO_FLAG_SPECULATIVE) 2884 return; 2885 2886 /* 2887 * If this is an I/O error that is going to be retried, then ignore the 2888 * error. Otherwise, the user may interpret B_FAILFAST I/O errors as 2889 * hard errors, when in reality they can happen for any number of 2890 * innocuous reasons (bus resets, MPxIO link failure, etc). 2891 */ 2892 if (zio->io_error == EIO && 2893 !(zio->io_flags & ZIO_FLAG_IO_RETRY)) 2894 return; 2895 2896 /* 2897 * Intent logs writes won't propagate their error to the root 2898 * I/O so don't mark these types of failures as pool-level 2899 * errors. 2900 */ 2901 if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE)) 2902 return; 2903 2904 mutex_enter(&vd->vdev_stat_lock); 2905 if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) { 2906 if (zio->io_error == ECKSUM) 2907 vs->vs_checksum_errors++; 2908 else 2909 vs->vs_read_errors++; 2910 } 2911 if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd)) 2912 vs->vs_write_errors++; 2913 mutex_exit(&vd->vdev_stat_lock); 2914 2915 if (type == ZIO_TYPE_WRITE && txg != 0 && 2916 (!(flags & ZIO_FLAG_IO_REPAIR) || 2917 (flags & ZIO_FLAG_SCAN_THREAD) || 2918 spa->spa_claiming)) { 2919 /* 2920 * This is either a normal write (not a repair), or it's 2921 * a repair induced by the scrub thread, or it's a repair 2922 * made by zil_claim() during spa_load() in the first txg. 2923 * In the normal case, we commit the DTL change in the same 2924 * txg as the block was born. In the scrub-induced repair 2925 * case, we know that scrubs run in first-pass syncing context, 2926 * so we commit the DTL change in spa_syncing_txg(spa). 2927 * In the zil_claim() case, we commit in spa_first_txg(spa). 2928 * 2929 * We currently do not make DTL entries for failed spontaneous 2930 * self-healing writes triggered by normal (non-scrubbing) 2931 * reads, because we have no transactional context in which to 2932 * do so -- and it's not clear that it'd be desirable anyway. 2933 */ 2934 if (vd->vdev_ops->vdev_op_leaf) { 2935 uint64_t commit_txg = txg; 2936 if (flags & ZIO_FLAG_SCAN_THREAD) { 2937 ASSERT(flags & ZIO_FLAG_IO_REPAIR); 2938 ASSERT(spa_sync_pass(spa) == 1); 2939 vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1); 2940 commit_txg = spa_syncing_txg(spa); 2941 } else if (spa->spa_claiming) { 2942 ASSERT(flags & ZIO_FLAG_IO_REPAIR); 2943 commit_txg = spa_first_txg(spa); 2944 } 2945 ASSERT(commit_txg >= spa_syncing_txg(spa)); 2946 if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1)) 2947 return; 2948 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 2949 vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1); 2950 vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg); 2951 } 2952 if (vd != rvd) 2953 vdev_dtl_dirty(vd, DTL_MISSING, txg, 1); 2954 } 2955 } 2956 2957 /* 2958 * Update the in-core space usage stats for this vdev, its metaslab class, 2959 * and the root vdev. 2960 */ 2961 void 2962 vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta, 2963 int64_t space_delta) 2964 { 2965 int64_t dspace_delta = space_delta; 2966 spa_t *spa = vd->vdev_spa; 2967 vdev_t *rvd = spa->spa_root_vdev; 2968 metaslab_group_t *mg = vd->vdev_mg; 2969 metaslab_class_t *mc = mg ? mg->mg_class : NULL; 2970 2971 ASSERT(vd == vd->vdev_top); 2972 2973 /* 2974 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion 2975 * factor. We must calculate this here and not at the root vdev 2976 * because the root vdev's psize-to-asize is simply the max of its 2977 * childrens', thus not accurate enough for us. 2978 */ 2979 ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0); 2980 ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache); 2981 dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) * 2982 vd->vdev_deflate_ratio; 2983 2984 mutex_enter(&vd->vdev_stat_lock); 2985 vd->vdev_stat.vs_alloc += alloc_delta; 2986 vd->vdev_stat.vs_space += space_delta; 2987 vd->vdev_stat.vs_dspace += dspace_delta; 2988 mutex_exit(&vd->vdev_stat_lock); 2989 2990 if (mc == spa_normal_class(spa)) { 2991 mutex_enter(&rvd->vdev_stat_lock); 2992 rvd->vdev_stat.vs_alloc += alloc_delta; 2993 rvd->vdev_stat.vs_space += space_delta; 2994 rvd->vdev_stat.vs_dspace += dspace_delta; 2995 mutex_exit(&rvd->vdev_stat_lock); 2996 } 2997 2998 if (mc != NULL) { 2999 ASSERT(rvd == vd->vdev_parent); 3000 ASSERT(vd->vdev_ms_count != 0); 3001 3002 metaslab_class_space_update(mc, 3003 alloc_delta, defer_delta, space_delta, dspace_delta); 3004 } 3005 } 3006 3007 /* 3008 * Mark a top-level vdev's config as dirty, placing it on the dirty list 3009 * so that it will be written out next time the vdev configuration is synced. 3010 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs. 3011 */ 3012 void 3013 vdev_config_dirty(vdev_t *vd) 3014 { 3015 spa_t *spa = vd->vdev_spa; 3016 vdev_t *rvd = spa->spa_root_vdev; 3017 int c; 3018 3019 ASSERT(spa_writeable(spa)); 3020 3021 /* 3022 * If this is an aux vdev (as with l2cache and spare devices), then we 3023 * update the vdev config manually and set the sync flag. 3024 */ 3025 if (vd->vdev_aux != NULL) { 3026 spa_aux_vdev_t *sav = vd->vdev_aux; 3027 nvlist_t **aux; 3028 uint_t naux; 3029 3030 for (c = 0; c < sav->sav_count; c++) { 3031 if (sav->sav_vdevs[c] == vd) 3032 break; 3033 } 3034 3035 if (c == sav->sav_count) { 3036 /* 3037 * We're being removed. There's nothing more to do. 3038 */ 3039 ASSERT(sav->sav_sync == B_TRUE); 3040 return; 3041 } 3042 3043 sav->sav_sync = B_TRUE; 3044 3045 if (nvlist_lookup_nvlist_array(sav->sav_config, 3046 ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) { 3047 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 3048 ZPOOL_CONFIG_SPARES, &aux, &naux) == 0); 3049 } 3050 3051 ASSERT(c < naux); 3052 3053 /* 3054 * Setting the nvlist in the middle if the array is a little 3055 * sketchy, but it will work. 3056 */ 3057 nvlist_free(aux[c]); 3058 aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0); 3059 3060 return; 3061 } 3062 3063 /* 3064 * The dirty list is protected by the SCL_CONFIG lock. The caller 3065 * must either hold SCL_CONFIG as writer, or must be the sync thread 3066 * (which holds SCL_CONFIG as reader). There's only one sync thread, 3067 * so this is sufficient to ensure mutual exclusion. 3068 */ 3069 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 3070 (dsl_pool_sync_context(spa_get_dsl(spa)) && 3071 spa_config_held(spa, SCL_CONFIG, RW_READER))); 3072 3073 if (vd == rvd) { 3074 for (c = 0; c < rvd->vdev_children; c++) 3075 vdev_config_dirty(rvd->vdev_child[c]); 3076 } else { 3077 ASSERT(vd == vd->vdev_top); 3078 3079 if (!list_link_active(&vd->vdev_config_dirty_node) && 3080 !vd->vdev_ishole) 3081 list_insert_head(&spa->spa_config_dirty_list, vd); 3082 } 3083 } 3084 3085 void 3086 vdev_config_clean(vdev_t *vd) 3087 { 3088 spa_t *spa = vd->vdev_spa; 3089 3090 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 3091 (dsl_pool_sync_context(spa_get_dsl(spa)) && 3092 spa_config_held(spa, SCL_CONFIG, RW_READER))); 3093 3094 ASSERT(list_link_active(&vd->vdev_config_dirty_node)); 3095 list_remove(&spa->spa_config_dirty_list, vd); 3096 } 3097 3098 /* 3099 * Mark a top-level vdev's state as dirty, so that the next pass of 3100 * spa_sync() can convert this into vdev_config_dirty(). We distinguish 3101 * the state changes from larger config changes because they require 3102 * much less locking, and are often needed for administrative actions. 3103 */ 3104 void 3105 vdev_state_dirty(vdev_t *vd) 3106 { 3107 spa_t *spa = vd->vdev_spa; 3108 3109 ASSERT(spa_writeable(spa)); 3110 ASSERT(vd == vd->vdev_top); 3111 3112 /* 3113 * The state list is protected by the SCL_STATE lock. The caller 3114 * must either hold SCL_STATE as writer, or must be the sync thread 3115 * (which holds SCL_STATE as reader). There's only one sync thread, 3116 * so this is sufficient to ensure mutual exclusion. 3117 */ 3118 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 3119 (dsl_pool_sync_context(spa_get_dsl(spa)) && 3120 spa_config_held(spa, SCL_STATE, RW_READER))); 3121 3122 if (!list_link_active(&vd->vdev_state_dirty_node) && !vd->vdev_ishole) 3123 list_insert_head(&spa->spa_state_dirty_list, vd); 3124 } 3125 3126 void 3127 vdev_state_clean(vdev_t *vd) 3128 { 3129 spa_t *spa = vd->vdev_spa; 3130 3131 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 3132 (dsl_pool_sync_context(spa_get_dsl(spa)) && 3133 spa_config_held(spa, SCL_STATE, RW_READER))); 3134 3135 ASSERT(list_link_active(&vd->vdev_state_dirty_node)); 3136 list_remove(&spa->spa_state_dirty_list, vd); 3137 } 3138 3139 /* 3140 * Propagate vdev state up from children to parent. 3141 */ 3142 void 3143 vdev_propagate_state(vdev_t *vd) 3144 { 3145 spa_t *spa = vd->vdev_spa; 3146 vdev_t *rvd = spa->spa_root_vdev; 3147 int degraded = 0, faulted = 0; 3148 int corrupted = 0; 3149 vdev_t *child; 3150 3151 if (vd->vdev_children > 0) { 3152 for (int c = 0; c < vd->vdev_children; c++) { 3153 child = vd->vdev_child[c]; 3154 3155 /* 3156 * Don't factor holes into the decision. 3157 */ 3158 if (child->vdev_ishole) 3159 continue; 3160 3161 if (!vdev_readable(child) || 3162 (!vdev_writeable(child) && spa_writeable(spa))) { 3163 /* 3164 * Root special: if there is a top-level log 3165 * device, treat the root vdev as if it were 3166 * degraded. 3167 */ 3168 if (child->vdev_islog && vd == rvd) 3169 degraded++; 3170 else 3171 faulted++; 3172 } else if (child->vdev_state <= VDEV_STATE_DEGRADED) { 3173 degraded++; 3174 } 3175 3176 if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA) 3177 corrupted++; 3178 } 3179 3180 vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded); 3181 3182 /* 3183 * Root special: if there is a top-level vdev that cannot be 3184 * opened due to corrupted metadata, then propagate the root 3185 * vdev's aux state as 'corrupt' rather than 'insufficient 3186 * replicas'. 3187 */ 3188 if (corrupted && vd == rvd && 3189 rvd->vdev_state == VDEV_STATE_CANT_OPEN) 3190 vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN, 3191 VDEV_AUX_CORRUPT_DATA); 3192 } 3193 3194 if (vd->vdev_parent) 3195 vdev_propagate_state(vd->vdev_parent); 3196 } 3197 3198 /* 3199 * Set a vdev's state. If this is during an open, we don't update the parent 3200 * state, because we're in the process of opening children depth-first. 3201 * Otherwise, we propagate the change to the parent. 3202 * 3203 * If this routine places a device in a faulted state, an appropriate ereport is 3204 * generated. 3205 */ 3206 void 3207 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux) 3208 { 3209 uint64_t save_state; 3210 spa_t *spa = vd->vdev_spa; 3211 3212 if (state == vd->vdev_state) { 3213 vd->vdev_stat.vs_aux = aux; 3214 return; 3215 } 3216 3217 save_state = vd->vdev_state; 3218 3219 vd->vdev_state = state; 3220 vd->vdev_stat.vs_aux = aux; 3221 3222 /* 3223 * If we are setting the vdev state to anything but an open state, then 3224 * always close the underlying device unless the device has requested 3225 * a delayed close (i.e. we're about to remove or fault the device). 3226 * Otherwise, we keep accessible but invalid devices open forever. 3227 * We don't call vdev_close() itself, because that implies some extra 3228 * checks (offline, etc) that we don't want here. This is limited to 3229 * leaf devices, because otherwise closing the device will affect other 3230 * children. 3231 */ 3232 if (!vd->vdev_delayed_close && vdev_is_dead(vd) && 3233 vd->vdev_ops->vdev_op_leaf) 3234 vd->vdev_ops->vdev_op_close(vd); 3235 3236 /* 3237 * If we have brought this vdev back into service, we need 3238 * to notify fmd so that it can gracefully repair any outstanding 3239 * cases due to a missing device. We do this in all cases, even those 3240 * that probably don't correlate to a repaired fault. This is sure to 3241 * catch all cases, and we let the zfs-retire agent sort it out. If 3242 * this is a transient state it's OK, as the retire agent will 3243 * double-check the state of the vdev before repairing it. 3244 */ 3245 if (state == VDEV_STATE_HEALTHY && vd->vdev_ops->vdev_op_leaf && 3246 vd->vdev_prevstate != state) 3247 zfs_post_state_change(spa, vd); 3248 3249 if (vd->vdev_removed && 3250 state == VDEV_STATE_CANT_OPEN && 3251 (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) { 3252 /* 3253 * If the previous state is set to VDEV_STATE_REMOVED, then this 3254 * device was previously marked removed and someone attempted to 3255 * reopen it. If this failed due to a nonexistent device, then 3256 * keep the device in the REMOVED state. We also let this be if 3257 * it is one of our special test online cases, which is only 3258 * attempting to online the device and shouldn't generate an FMA 3259 * fault. 3260 */ 3261 vd->vdev_state = VDEV_STATE_REMOVED; 3262 vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 3263 } else if (state == VDEV_STATE_REMOVED) { 3264 vd->vdev_removed = B_TRUE; 3265 } else if (state == VDEV_STATE_CANT_OPEN) { 3266 /* 3267 * If we fail to open a vdev during an import or recovery, we 3268 * mark it as "not available", which signifies that it was 3269 * never there to begin with. Failure to open such a device 3270 * is not considered an error. 3271 */ 3272 if ((spa_load_state(spa) == SPA_LOAD_IMPORT || 3273 spa_load_state(spa) == SPA_LOAD_RECOVER) && 3274 vd->vdev_ops->vdev_op_leaf) 3275 vd->vdev_not_present = 1; 3276 3277 /* 3278 * Post the appropriate ereport. If the 'prevstate' field is 3279 * set to something other than VDEV_STATE_UNKNOWN, it indicates 3280 * that this is part of a vdev_reopen(). In this case, we don't 3281 * want to post the ereport if the device was already in the 3282 * CANT_OPEN state beforehand. 3283 * 3284 * If the 'checkremove' flag is set, then this is an attempt to 3285 * online the device in response to an insertion event. If we 3286 * hit this case, then we have detected an insertion event for a 3287 * faulted or offline device that wasn't in the removed state. 3288 * In this scenario, we don't post an ereport because we are 3289 * about to replace the device, or attempt an online with 3290 * vdev_forcefault, which will generate the fault for us. 3291 */ 3292 if ((vd->vdev_prevstate != state || vd->vdev_forcefault) && 3293 !vd->vdev_not_present && !vd->vdev_checkremove && 3294 vd != spa->spa_root_vdev) { 3295 const char *class; 3296 3297 switch (aux) { 3298 case VDEV_AUX_OPEN_FAILED: 3299 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED; 3300 break; 3301 case VDEV_AUX_CORRUPT_DATA: 3302 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA; 3303 break; 3304 case VDEV_AUX_NO_REPLICAS: 3305 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS; 3306 break; 3307 case VDEV_AUX_BAD_GUID_SUM: 3308 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM; 3309 break; 3310 case VDEV_AUX_TOO_SMALL: 3311 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL; 3312 break; 3313 case VDEV_AUX_BAD_LABEL: 3314 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL; 3315 break; 3316 default: 3317 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN; 3318 } 3319 3320 zfs_ereport_post(class, spa, vd, NULL, save_state, 0); 3321 } 3322 3323 /* Erase any notion of persistent removed state */ 3324 vd->vdev_removed = B_FALSE; 3325 } else { 3326 vd->vdev_removed = B_FALSE; 3327 } 3328 3329 if (!isopen && vd->vdev_parent) 3330 vdev_propagate_state(vd->vdev_parent); 3331 } 3332 3333 /* 3334 * Check the vdev configuration to ensure that it's capable of supporting 3335 * a root pool. We do not support partial configuration. 3336 * In addition, only a single top-level vdev is allowed. 3337 */ 3338 boolean_t 3339 vdev_is_bootable(vdev_t *vd) 3340 { 3341 if (!vd->vdev_ops->vdev_op_leaf) { 3342 char *vdev_type = vd->vdev_ops->vdev_op_type; 3343 3344 if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 && 3345 vd->vdev_children > 1) { 3346 return (B_FALSE); 3347 } else if (strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) { 3348 return (B_FALSE); 3349 } 3350 } 3351 3352 for (int c = 0; c < vd->vdev_children; c++) { 3353 if (!vdev_is_bootable(vd->vdev_child[c])) 3354 return (B_FALSE); 3355 } 3356 return (B_TRUE); 3357 } 3358 3359 /* 3360 * Load the state from the original vdev tree (ovd) which 3361 * we've retrieved from the MOS config object. If the original 3362 * vdev was offline or faulted then we transfer that state to the 3363 * device in the current vdev tree (nvd). 3364 */ 3365 void 3366 vdev_load_log_state(vdev_t *nvd, vdev_t *ovd) 3367 { 3368 spa_t *spa = nvd->vdev_spa; 3369 3370 ASSERT(nvd->vdev_top->vdev_islog); 3371 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 3372 ASSERT3U(nvd->vdev_guid, ==, ovd->vdev_guid); 3373 3374 for (int c = 0; c < nvd->vdev_children; c++) 3375 vdev_load_log_state(nvd->vdev_child[c], ovd->vdev_child[c]); 3376 3377 if (nvd->vdev_ops->vdev_op_leaf) { 3378 /* 3379 * Restore the persistent vdev state 3380 */ 3381 nvd->vdev_offline = ovd->vdev_offline; 3382 nvd->vdev_faulted = ovd->vdev_faulted; 3383 nvd->vdev_degraded = ovd->vdev_degraded; 3384 nvd->vdev_removed = ovd->vdev_removed; 3385 } 3386 } 3387 3388 /* 3389 * Determine if a log device has valid content. If the vdev was 3390 * removed or faulted in the MOS config then we know that 3391 * the content on the log device has already been written to the pool. 3392 */ 3393 boolean_t 3394 vdev_log_state_valid(vdev_t *vd) 3395 { 3396 if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted && 3397 !vd->vdev_removed) 3398 return (B_TRUE); 3399 3400 for (int c = 0; c < vd->vdev_children; c++) 3401 if (vdev_log_state_valid(vd->vdev_child[c])) 3402 return (B_TRUE); 3403 3404 return (B_FALSE); 3405 } 3406 3407 /* 3408 * Expand a vdev if possible. 3409 */ 3410 void 3411 vdev_expand(vdev_t *vd, uint64_t txg) 3412 { 3413 ASSERT(vd->vdev_top == vd); 3414 ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 3415 3416 if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count) { 3417 VERIFY(vdev_metaslab_init(vd, txg) == 0); 3418 vdev_config_dirty(vd); 3419 } 3420 } 3421 3422 /* 3423 * Split a vdev. 3424 */ 3425 void 3426 vdev_split(vdev_t *vd) 3427 { 3428 vdev_t *cvd, *pvd = vd->vdev_parent; 3429 3430 vdev_remove_child(pvd, vd); 3431 vdev_compact_children(pvd); 3432 3433 cvd = pvd->vdev_child[0]; 3434 if (pvd->vdev_children == 1) { 3435 vdev_remove_parent(cvd); 3436 cvd->vdev_splitting = B_TRUE; 3437 } 3438 vdev_propagate_state(cvd); 3439 } 3440 3441 void 3442 vdev_deadman(vdev_t *vd) 3443 { 3444 for (int c = 0; c < vd->vdev_children; c++) { 3445 vdev_t *cvd = vd->vdev_child[c]; 3446 3447 vdev_deadman(cvd); 3448 } 3449 3450 if (vd->vdev_ops->vdev_op_leaf) { 3451 vdev_queue_t *vq = &vd->vdev_queue; 3452 3453 mutex_enter(&vq->vq_lock); 3454 if (avl_numnodes(&vq->vq_active_tree) > 0) { 3455 spa_t *spa = vd->vdev_spa; 3456 zio_t *fio; 3457 uint64_t delta; 3458 3459 /* 3460 * Look at the head of all the pending queues, 3461 * if any I/O has been outstanding for longer than 3462 * the spa_deadman_synctime we panic the system. 3463 */ 3464 fio = avl_first(&vq->vq_active_tree); 3465 delta = gethrtime() - fio->io_timestamp; 3466 if (delta > spa_deadman_synctime(spa)) { 3467 zfs_dbgmsg("SLOW IO: zio timestamp %lluns, " 3468 "delta %lluns, last io %lluns", 3469 fio->io_timestamp, delta, 3470 vq->vq_io_complete_ts); 3471 fm_panic("I/O to pool '%s' appears to be " 3472 "hung.", spa_name(spa)); 3473 } 3474 } 3475 mutex_exit(&vq->vq_lock); 3476 } 3477 } 3478