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 2009 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #include <sys/zfs_context.h> 28 #include <sys/fm/fs/zfs.h> 29 #include <sys/spa.h> 30 #include <sys/spa_impl.h> 31 #include <sys/dmu.h> 32 #include <sys/dmu_tx.h> 33 #include <sys/vdev_impl.h> 34 #include <sys/uberblock_impl.h> 35 #include <sys/metaslab.h> 36 #include <sys/metaslab_impl.h> 37 #include <sys/space_map.h> 38 #include <sys/zio.h> 39 #include <sys/zap.h> 40 #include <sys/fs/zfs.h> 41 #include <sys/arc.h> 42 43 /* 44 * Virtual device management. 45 */ 46 47 static vdev_ops_t *vdev_ops_table[] = { 48 &vdev_root_ops, 49 &vdev_raidz_ops, 50 &vdev_mirror_ops, 51 &vdev_replacing_ops, 52 &vdev_spare_ops, 53 &vdev_disk_ops, 54 &vdev_file_ops, 55 &vdev_missing_ops, 56 NULL 57 }; 58 59 /* maximum scrub/resilver I/O queue per leaf vdev */ 60 int zfs_scrub_limit = 10; 61 62 /* 63 * Given a vdev type, return the appropriate ops vector. 64 */ 65 static vdev_ops_t * 66 vdev_getops(const char *type) 67 { 68 vdev_ops_t *ops, **opspp; 69 70 for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++) 71 if (strcmp(ops->vdev_op_type, type) == 0) 72 break; 73 74 return (ops); 75 } 76 77 /* 78 * Default asize function: return the MAX of psize with the asize of 79 * all children. This is what's used by anything other than RAID-Z. 80 */ 81 uint64_t 82 vdev_default_asize(vdev_t *vd, uint64_t psize) 83 { 84 uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift); 85 uint64_t csize; 86 uint64_t c; 87 88 for (c = 0; c < vd->vdev_children; c++) { 89 csize = vdev_psize_to_asize(vd->vdev_child[c], psize); 90 asize = MAX(asize, csize); 91 } 92 93 return (asize); 94 } 95 96 /* 97 * Get the replaceable or attachable device size. 98 * If the parent is a mirror or raidz, the replaceable size is the minimum 99 * psize of all its children. For the rest, just return our own psize. 100 * 101 * e.g. 102 * psize rsize 103 * root - - 104 * mirror/raidz - - 105 * disk1 20g 20g 106 * disk2 40g 20g 107 * disk3 80g 80g 108 */ 109 uint64_t 110 vdev_get_rsize(vdev_t *vd) 111 { 112 vdev_t *pvd, *cvd; 113 uint64_t c, rsize; 114 115 pvd = vd->vdev_parent; 116 117 /* 118 * If our parent is NULL or the root, just return our own psize. 119 */ 120 if (pvd == NULL || pvd->vdev_parent == NULL) 121 return (vd->vdev_psize); 122 123 rsize = 0; 124 125 for (c = 0; c < pvd->vdev_children; c++) { 126 cvd = pvd->vdev_child[c]; 127 rsize = MIN(rsize - 1, cvd->vdev_psize - 1) + 1; 128 } 129 130 return (rsize); 131 } 132 133 vdev_t * 134 vdev_lookup_top(spa_t *spa, uint64_t vdev) 135 { 136 vdev_t *rvd = spa->spa_root_vdev; 137 138 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 139 140 if (vdev < rvd->vdev_children) { 141 ASSERT(rvd->vdev_child[vdev] != NULL); 142 return (rvd->vdev_child[vdev]); 143 } 144 145 return (NULL); 146 } 147 148 vdev_t * 149 vdev_lookup_by_guid(vdev_t *vd, uint64_t guid) 150 { 151 int c; 152 vdev_t *mvd; 153 154 if (vd->vdev_guid == guid) 155 return (vd); 156 157 for (c = 0; c < vd->vdev_children; c++) 158 if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) != 159 NULL) 160 return (mvd); 161 162 return (NULL); 163 } 164 165 void 166 vdev_add_child(vdev_t *pvd, vdev_t *cvd) 167 { 168 size_t oldsize, newsize; 169 uint64_t id = cvd->vdev_id; 170 vdev_t **newchild; 171 172 ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 173 ASSERT(cvd->vdev_parent == NULL); 174 175 cvd->vdev_parent = pvd; 176 177 if (pvd == NULL) 178 return; 179 180 ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL); 181 182 oldsize = pvd->vdev_children * sizeof (vdev_t *); 183 pvd->vdev_children = MAX(pvd->vdev_children, id + 1); 184 newsize = pvd->vdev_children * sizeof (vdev_t *); 185 186 newchild = kmem_zalloc(newsize, KM_SLEEP); 187 if (pvd->vdev_child != NULL) { 188 bcopy(pvd->vdev_child, newchild, oldsize); 189 kmem_free(pvd->vdev_child, oldsize); 190 } 191 192 pvd->vdev_child = newchild; 193 pvd->vdev_child[id] = cvd; 194 195 cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd); 196 ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL); 197 198 /* 199 * Walk up all ancestors to update guid sum. 200 */ 201 for (; pvd != NULL; pvd = pvd->vdev_parent) 202 pvd->vdev_guid_sum += cvd->vdev_guid_sum; 203 204 if (cvd->vdev_ops->vdev_op_leaf) 205 cvd->vdev_spa->spa_scrub_maxinflight += zfs_scrub_limit; 206 } 207 208 void 209 vdev_remove_child(vdev_t *pvd, vdev_t *cvd) 210 { 211 int c; 212 uint_t id = cvd->vdev_id; 213 214 ASSERT(cvd->vdev_parent == pvd); 215 216 if (pvd == NULL) 217 return; 218 219 ASSERT(id < pvd->vdev_children); 220 ASSERT(pvd->vdev_child[id] == cvd); 221 222 pvd->vdev_child[id] = NULL; 223 cvd->vdev_parent = NULL; 224 225 for (c = 0; c < pvd->vdev_children; c++) 226 if (pvd->vdev_child[c]) 227 break; 228 229 if (c == pvd->vdev_children) { 230 kmem_free(pvd->vdev_child, c * sizeof (vdev_t *)); 231 pvd->vdev_child = NULL; 232 pvd->vdev_children = 0; 233 } 234 235 /* 236 * Walk up all ancestors to update guid sum. 237 */ 238 for (; pvd != NULL; pvd = pvd->vdev_parent) 239 pvd->vdev_guid_sum -= cvd->vdev_guid_sum; 240 241 if (cvd->vdev_ops->vdev_op_leaf) 242 cvd->vdev_spa->spa_scrub_maxinflight -= zfs_scrub_limit; 243 } 244 245 /* 246 * Remove any holes in the child array. 247 */ 248 void 249 vdev_compact_children(vdev_t *pvd) 250 { 251 vdev_t **newchild, *cvd; 252 int oldc = pvd->vdev_children; 253 int newc, c; 254 255 ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 256 257 for (c = newc = 0; c < oldc; c++) 258 if (pvd->vdev_child[c]) 259 newc++; 260 261 newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP); 262 263 for (c = newc = 0; c < oldc; c++) { 264 if ((cvd = pvd->vdev_child[c]) != NULL) { 265 newchild[newc] = cvd; 266 cvd->vdev_id = newc++; 267 } 268 } 269 270 kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *)); 271 pvd->vdev_child = newchild; 272 pvd->vdev_children = newc; 273 } 274 275 /* 276 * Allocate and minimally initialize a vdev_t. 277 */ 278 static vdev_t * 279 vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops) 280 { 281 vdev_t *vd; 282 283 vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP); 284 285 if (spa->spa_root_vdev == NULL) { 286 ASSERT(ops == &vdev_root_ops); 287 spa->spa_root_vdev = vd; 288 } 289 290 if (guid == 0) { 291 if (spa->spa_root_vdev == vd) { 292 /* 293 * The root vdev's guid will also be the pool guid, 294 * which must be unique among all pools. 295 */ 296 while (guid == 0 || spa_guid_exists(guid, 0)) 297 guid = spa_get_random(-1ULL); 298 } else { 299 /* 300 * Any other vdev's guid must be unique within the pool. 301 */ 302 while (guid == 0 || 303 spa_guid_exists(spa_guid(spa), guid)) 304 guid = spa_get_random(-1ULL); 305 } 306 ASSERT(!spa_guid_exists(spa_guid(spa), guid)); 307 } 308 309 vd->vdev_spa = spa; 310 vd->vdev_id = id; 311 vd->vdev_guid = guid; 312 vd->vdev_guid_sum = guid; 313 vd->vdev_ops = ops; 314 vd->vdev_state = VDEV_STATE_CLOSED; 315 316 mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL); 317 mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL); 318 mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL); 319 for (int t = 0; t < DTL_TYPES; t++) { 320 space_map_create(&vd->vdev_dtl[t], 0, -1ULL, 0, 321 &vd->vdev_dtl_lock); 322 } 323 txg_list_create(&vd->vdev_ms_list, 324 offsetof(struct metaslab, ms_txg_node)); 325 txg_list_create(&vd->vdev_dtl_list, 326 offsetof(struct vdev, vdev_dtl_node)); 327 vd->vdev_stat.vs_timestamp = gethrtime(); 328 vdev_queue_init(vd); 329 vdev_cache_init(vd); 330 331 return (vd); 332 } 333 334 /* 335 * Allocate a new vdev. The 'alloctype' is used to control whether we are 336 * creating a new vdev or loading an existing one - the behavior is slightly 337 * different for each case. 338 */ 339 int 340 vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id, 341 int alloctype) 342 { 343 vdev_ops_t *ops; 344 char *type; 345 uint64_t guid = 0, islog, nparity; 346 vdev_t *vd; 347 348 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 349 350 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0) 351 return (EINVAL); 352 353 if ((ops = vdev_getops(type)) == NULL) 354 return (EINVAL); 355 356 /* 357 * If this is a load, get the vdev guid from the nvlist. 358 * Otherwise, vdev_alloc_common() will generate one for us. 359 */ 360 if (alloctype == VDEV_ALLOC_LOAD) { 361 uint64_t label_id; 362 363 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) || 364 label_id != id) 365 return (EINVAL); 366 367 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 368 return (EINVAL); 369 } else if (alloctype == VDEV_ALLOC_SPARE) { 370 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 371 return (EINVAL); 372 } else if (alloctype == VDEV_ALLOC_L2CACHE) { 373 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 374 return (EINVAL); 375 } 376 377 /* 378 * The first allocated vdev must be of type 'root'. 379 */ 380 if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL) 381 return (EINVAL); 382 383 /* 384 * Determine whether we're a log vdev. 385 */ 386 islog = 0; 387 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog); 388 if (islog && spa_version(spa) < SPA_VERSION_SLOGS) 389 return (ENOTSUP); 390 391 /* 392 * Set the nparity property for RAID-Z vdevs. 393 */ 394 nparity = -1ULL; 395 if (ops == &vdev_raidz_ops) { 396 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 397 &nparity) == 0) { 398 /* 399 * Currently, we can only support 2 parity devices. 400 */ 401 if (nparity == 0 || nparity > 2) 402 return (EINVAL); 403 /* 404 * Older versions can only support 1 parity device. 405 */ 406 if (nparity == 2 && 407 spa_version(spa) < SPA_VERSION_RAID6) 408 return (ENOTSUP); 409 } else { 410 /* 411 * We require the parity to be specified for SPAs that 412 * support multiple parity levels. 413 */ 414 if (spa_version(spa) >= SPA_VERSION_RAID6) 415 return (EINVAL); 416 /* 417 * Otherwise, we default to 1 parity device for RAID-Z. 418 */ 419 nparity = 1; 420 } 421 } else { 422 nparity = 0; 423 } 424 ASSERT(nparity != -1ULL); 425 426 vd = vdev_alloc_common(spa, id, guid, ops); 427 428 vd->vdev_islog = islog; 429 vd->vdev_nparity = nparity; 430 431 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0) 432 vd->vdev_path = spa_strdup(vd->vdev_path); 433 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0) 434 vd->vdev_devid = spa_strdup(vd->vdev_devid); 435 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH, 436 &vd->vdev_physpath) == 0) 437 vd->vdev_physpath = spa_strdup(vd->vdev_physpath); 438 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0) 439 vd->vdev_fru = spa_strdup(vd->vdev_fru); 440 441 /* 442 * Set the whole_disk property. If it's not specified, leave the value 443 * as -1. 444 */ 445 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 446 &vd->vdev_wholedisk) != 0) 447 vd->vdev_wholedisk = -1ULL; 448 449 /* 450 * Look for the 'not present' flag. This will only be set if the device 451 * was not present at the time of import. 452 */ 453 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 454 &vd->vdev_not_present); 455 456 /* 457 * Get the alignment requirement. 458 */ 459 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift); 460 461 /* 462 * If we're a top-level vdev, try to load the allocation parameters. 463 */ 464 if (parent && !parent->vdev_parent && alloctype == VDEV_ALLOC_LOAD) { 465 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY, 466 &vd->vdev_ms_array); 467 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT, 468 &vd->vdev_ms_shift); 469 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE, 470 &vd->vdev_asize); 471 } 472 473 /* 474 * If we're a leaf vdev, try to load the DTL object and other state. 475 */ 476 if (vd->vdev_ops->vdev_op_leaf && 477 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE)) { 478 if (alloctype == VDEV_ALLOC_LOAD) { 479 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL, 480 &vd->vdev_dtl_smo.smo_object); 481 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE, 482 &vd->vdev_unspare); 483 } 484 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, 485 &vd->vdev_offline); 486 487 /* 488 * When importing a pool, we want to ignore the persistent fault 489 * state, as the diagnosis made on another system may not be 490 * valid in the current context. 491 */ 492 if (spa->spa_load_state == SPA_LOAD_OPEN) { 493 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, 494 &vd->vdev_faulted); 495 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED, 496 &vd->vdev_degraded); 497 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, 498 &vd->vdev_removed); 499 } 500 } 501 502 /* 503 * Add ourselves to the parent's list of children. 504 */ 505 vdev_add_child(parent, vd); 506 507 *vdp = vd; 508 509 return (0); 510 } 511 512 void 513 vdev_free(vdev_t *vd) 514 { 515 int c; 516 spa_t *spa = vd->vdev_spa; 517 518 /* 519 * vdev_free() implies closing the vdev first. This is simpler than 520 * trying to ensure complicated semantics for all callers. 521 */ 522 vdev_close(vd); 523 524 ASSERT(!list_link_active(&vd->vdev_config_dirty_node)); 525 526 /* 527 * Free all children. 528 */ 529 for (c = 0; c < vd->vdev_children; c++) 530 vdev_free(vd->vdev_child[c]); 531 532 ASSERT(vd->vdev_child == NULL); 533 ASSERT(vd->vdev_guid_sum == vd->vdev_guid); 534 535 /* 536 * Discard allocation state. 537 */ 538 if (vd == vd->vdev_top) 539 vdev_metaslab_fini(vd); 540 541 ASSERT3U(vd->vdev_stat.vs_space, ==, 0); 542 ASSERT3U(vd->vdev_stat.vs_dspace, ==, 0); 543 ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0); 544 545 /* 546 * Remove this vdev from its parent's child list. 547 */ 548 vdev_remove_child(vd->vdev_parent, vd); 549 550 ASSERT(vd->vdev_parent == NULL); 551 552 /* 553 * Clean up vdev structure. 554 */ 555 vdev_queue_fini(vd); 556 vdev_cache_fini(vd); 557 558 if (vd->vdev_path) 559 spa_strfree(vd->vdev_path); 560 if (vd->vdev_devid) 561 spa_strfree(vd->vdev_devid); 562 if (vd->vdev_physpath) 563 spa_strfree(vd->vdev_physpath); 564 if (vd->vdev_fru) 565 spa_strfree(vd->vdev_fru); 566 567 if (vd->vdev_isspare) 568 spa_spare_remove(vd); 569 if (vd->vdev_isl2cache) 570 spa_l2cache_remove(vd); 571 572 txg_list_destroy(&vd->vdev_ms_list); 573 txg_list_destroy(&vd->vdev_dtl_list); 574 575 mutex_enter(&vd->vdev_dtl_lock); 576 for (int t = 0; t < DTL_TYPES; t++) { 577 space_map_unload(&vd->vdev_dtl[t]); 578 space_map_destroy(&vd->vdev_dtl[t]); 579 } 580 mutex_exit(&vd->vdev_dtl_lock); 581 582 mutex_destroy(&vd->vdev_dtl_lock); 583 mutex_destroy(&vd->vdev_stat_lock); 584 mutex_destroy(&vd->vdev_probe_lock); 585 586 if (vd == spa->spa_root_vdev) 587 spa->spa_root_vdev = NULL; 588 589 kmem_free(vd, sizeof (vdev_t)); 590 } 591 592 /* 593 * Transfer top-level vdev state from svd to tvd. 594 */ 595 static void 596 vdev_top_transfer(vdev_t *svd, vdev_t *tvd) 597 { 598 spa_t *spa = svd->vdev_spa; 599 metaslab_t *msp; 600 vdev_t *vd; 601 int t; 602 603 ASSERT(tvd == tvd->vdev_top); 604 605 tvd->vdev_ms_array = svd->vdev_ms_array; 606 tvd->vdev_ms_shift = svd->vdev_ms_shift; 607 tvd->vdev_ms_count = svd->vdev_ms_count; 608 609 svd->vdev_ms_array = 0; 610 svd->vdev_ms_shift = 0; 611 svd->vdev_ms_count = 0; 612 613 tvd->vdev_mg = svd->vdev_mg; 614 tvd->vdev_ms = svd->vdev_ms; 615 616 svd->vdev_mg = NULL; 617 svd->vdev_ms = NULL; 618 619 if (tvd->vdev_mg != NULL) 620 tvd->vdev_mg->mg_vd = tvd; 621 622 tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc; 623 tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space; 624 tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace; 625 626 svd->vdev_stat.vs_alloc = 0; 627 svd->vdev_stat.vs_space = 0; 628 svd->vdev_stat.vs_dspace = 0; 629 630 for (t = 0; t < TXG_SIZE; t++) { 631 while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL) 632 (void) txg_list_add(&tvd->vdev_ms_list, msp, t); 633 while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL) 634 (void) txg_list_add(&tvd->vdev_dtl_list, vd, t); 635 if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t)) 636 (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t); 637 } 638 639 if (list_link_active(&svd->vdev_config_dirty_node)) { 640 vdev_config_clean(svd); 641 vdev_config_dirty(tvd); 642 } 643 644 if (list_link_active(&svd->vdev_state_dirty_node)) { 645 vdev_state_clean(svd); 646 vdev_state_dirty(tvd); 647 } 648 649 tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio; 650 svd->vdev_deflate_ratio = 0; 651 652 tvd->vdev_islog = svd->vdev_islog; 653 svd->vdev_islog = 0; 654 } 655 656 static void 657 vdev_top_update(vdev_t *tvd, vdev_t *vd) 658 { 659 int c; 660 661 if (vd == NULL) 662 return; 663 664 vd->vdev_top = tvd; 665 666 for (c = 0; c < vd->vdev_children; c++) 667 vdev_top_update(tvd, vd->vdev_child[c]); 668 } 669 670 /* 671 * Add a mirror/replacing vdev above an existing vdev. 672 */ 673 vdev_t * 674 vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops) 675 { 676 spa_t *spa = cvd->vdev_spa; 677 vdev_t *pvd = cvd->vdev_parent; 678 vdev_t *mvd; 679 680 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 681 682 mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops); 683 684 mvd->vdev_asize = cvd->vdev_asize; 685 mvd->vdev_ashift = cvd->vdev_ashift; 686 mvd->vdev_state = cvd->vdev_state; 687 688 vdev_remove_child(pvd, cvd); 689 vdev_add_child(pvd, mvd); 690 cvd->vdev_id = mvd->vdev_children; 691 vdev_add_child(mvd, cvd); 692 vdev_top_update(cvd->vdev_top, cvd->vdev_top); 693 694 if (mvd == mvd->vdev_top) 695 vdev_top_transfer(cvd, mvd); 696 697 return (mvd); 698 } 699 700 /* 701 * Remove a 1-way mirror/replacing vdev from the tree. 702 */ 703 void 704 vdev_remove_parent(vdev_t *cvd) 705 { 706 vdev_t *mvd = cvd->vdev_parent; 707 vdev_t *pvd = mvd->vdev_parent; 708 709 ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 710 711 ASSERT(mvd->vdev_children == 1); 712 ASSERT(mvd->vdev_ops == &vdev_mirror_ops || 713 mvd->vdev_ops == &vdev_replacing_ops || 714 mvd->vdev_ops == &vdev_spare_ops); 715 cvd->vdev_ashift = mvd->vdev_ashift; 716 717 vdev_remove_child(mvd, cvd); 718 vdev_remove_child(pvd, mvd); 719 720 /* 721 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid. 722 * Otherwise, we could have detached an offline device, and when we 723 * go to import the pool we'll think we have two top-level vdevs, 724 * instead of a different version of the same top-level vdev. 725 */ 726 if (mvd->vdev_top == mvd) { 727 uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid; 728 cvd->vdev_guid += guid_delta; 729 cvd->vdev_guid_sum += guid_delta; 730 } 731 cvd->vdev_id = mvd->vdev_id; 732 vdev_add_child(pvd, cvd); 733 vdev_top_update(cvd->vdev_top, cvd->vdev_top); 734 735 if (cvd == cvd->vdev_top) 736 vdev_top_transfer(mvd, cvd); 737 738 ASSERT(mvd->vdev_children == 0); 739 vdev_free(mvd); 740 } 741 742 int 743 vdev_metaslab_init(vdev_t *vd, uint64_t txg) 744 { 745 spa_t *spa = vd->vdev_spa; 746 objset_t *mos = spa->spa_meta_objset; 747 metaslab_class_t *mc; 748 uint64_t m; 749 uint64_t oldc = vd->vdev_ms_count; 750 uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift; 751 metaslab_t **mspp; 752 int error; 753 754 if (vd->vdev_ms_shift == 0) /* not being allocated from yet */ 755 return (0); 756 757 ASSERT(oldc <= newc); 758 759 if (vd->vdev_islog) 760 mc = spa->spa_log_class; 761 else 762 mc = spa->spa_normal_class; 763 764 if (vd->vdev_mg == NULL) 765 vd->vdev_mg = metaslab_group_create(mc, vd); 766 767 mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP); 768 769 if (oldc != 0) { 770 bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp)); 771 kmem_free(vd->vdev_ms, oldc * sizeof (*mspp)); 772 } 773 774 vd->vdev_ms = mspp; 775 vd->vdev_ms_count = newc; 776 777 for (m = oldc; m < newc; m++) { 778 space_map_obj_t smo = { 0, 0, 0 }; 779 if (txg == 0) { 780 uint64_t object = 0; 781 error = dmu_read(mos, vd->vdev_ms_array, 782 m * sizeof (uint64_t), sizeof (uint64_t), &object); 783 if (error) 784 return (error); 785 if (object != 0) { 786 dmu_buf_t *db; 787 error = dmu_bonus_hold(mos, object, FTAG, &db); 788 if (error) 789 return (error); 790 ASSERT3U(db->db_size, >=, sizeof (smo)); 791 bcopy(db->db_data, &smo, sizeof (smo)); 792 ASSERT3U(smo.smo_object, ==, object); 793 dmu_buf_rele(db, FTAG); 794 } 795 } 796 vd->vdev_ms[m] = metaslab_init(vd->vdev_mg, &smo, 797 m << vd->vdev_ms_shift, 1ULL << vd->vdev_ms_shift, txg); 798 } 799 800 return (0); 801 } 802 803 void 804 vdev_metaslab_fini(vdev_t *vd) 805 { 806 uint64_t m; 807 uint64_t count = vd->vdev_ms_count; 808 809 if (vd->vdev_ms != NULL) { 810 for (m = 0; m < count; m++) 811 if (vd->vdev_ms[m] != NULL) 812 metaslab_fini(vd->vdev_ms[m]); 813 kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *)); 814 vd->vdev_ms = NULL; 815 } 816 } 817 818 typedef struct vdev_probe_stats { 819 boolean_t vps_readable; 820 boolean_t vps_writeable; 821 int vps_flags; 822 } vdev_probe_stats_t; 823 824 static void 825 vdev_probe_done(zio_t *zio) 826 { 827 spa_t *spa = zio->io_spa; 828 vdev_t *vd = zio->io_vd; 829 vdev_probe_stats_t *vps = zio->io_private; 830 831 ASSERT(vd->vdev_probe_zio != NULL); 832 833 if (zio->io_type == ZIO_TYPE_READ) { 834 if (zio->io_error == 0) 835 vps->vps_readable = 1; 836 if (zio->io_error == 0 && spa_writeable(spa)) { 837 zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd, 838 zio->io_offset, zio->io_size, zio->io_data, 839 ZIO_CHECKSUM_OFF, vdev_probe_done, vps, 840 ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE)); 841 } else { 842 zio_buf_free(zio->io_data, zio->io_size); 843 } 844 } else if (zio->io_type == ZIO_TYPE_WRITE) { 845 if (zio->io_error == 0) 846 vps->vps_writeable = 1; 847 zio_buf_free(zio->io_data, zio->io_size); 848 } else if (zio->io_type == ZIO_TYPE_NULL) { 849 zio_t *pio; 850 851 vd->vdev_cant_read |= !vps->vps_readable; 852 vd->vdev_cant_write |= !vps->vps_writeable; 853 854 if (vdev_readable(vd) && 855 (vdev_writeable(vd) || !spa_writeable(spa))) { 856 zio->io_error = 0; 857 } else { 858 ASSERT(zio->io_error != 0); 859 zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE, 860 spa, vd, NULL, 0, 0); 861 zio->io_error = ENXIO; 862 } 863 864 mutex_enter(&vd->vdev_probe_lock); 865 ASSERT(vd->vdev_probe_zio == zio); 866 vd->vdev_probe_zio = NULL; 867 mutex_exit(&vd->vdev_probe_lock); 868 869 while ((pio = zio_walk_parents(zio)) != NULL) 870 if (!vdev_accessible(vd, pio)) 871 pio->io_error = ENXIO; 872 873 kmem_free(vps, sizeof (*vps)); 874 } 875 } 876 877 /* 878 * Determine whether this device is accessible by reading and writing 879 * to several known locations: the pad regions of each vdev label 880 * but the first (which we leave alone in case it contains a VTOC). 881 */ 882 zio_t * 883 vdev_probe(vdev_t *vd, zio_t *zio) 884 { 885 spa_t *spa = vd->vdev_spa; 886 vdev_probe_stats_t *vps = NULL; 887 zio_t *pio; 888 889 ASSERT(vd->vdev_ops->vdev_op_leaf); 890 891 /* 892 * Don't probe the probe. 893 */ 894 if (zio && (zio->io_flags & ZIO_FLAG_PROBE)) 895 return (NULL); 896 897 /* 898 * To prevent 'probe storms' when a device fails, we create 899 * just one probe i/o at a time. All zios that want to probe 900 * this vdev will become parents of the probe io. 901 */ 902 mutex_enter(&vd->vdev_probe_lock); 903 904 if ((pio = vd->vdev_probe_zio) == NULL) { 905 vps = kmem_zalloc(sizeof (*vps), KM_SLEEP); 906 907 vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE | 908 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE | 909 ZIO_FLAG_DONT_RETRY; 910 911 if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) { 912 /* 913 * vdev_cant_read and vdev_cant_write can only 914 * transition from TRUE to FALSE when we have the 915 * SCL_ZIO lock as writer; otherwise they can only 916 * transition from FALSE to TRUE. This ensures that 917 * any zio looking at these values can assume that 918 * failures persist for the life of the I/O. That's 919 * important because when a device has intermittent 920 * connectivity problems, we want to ensure that 921 * they're ascribed to the device (ENXIO) and not 922 * the zio (EIO). 923 * 924 * Since we hold SCL_ZIO as writer here, clear both 925 * values so the probe can reevaluate from first 926 * principles. 927 */ 928 vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER; 929 vd->vdev_cant_read = B_FALSE; 930 vd->vdev_cant_write = B_FALSE; 931 } 932 933 vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd, 934 vdev_probe_done, vps, 935 vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE); 936 937 if (zio != NULL) { 938 vd->vdev_probe_wanted = B_TRUE; 939 spa_async_request(spa, SPA_ASYNC_PROBE); 940 } 941 } 942 943 if (zio != NULL) 944 zio_add_child(zio, pio); 945 946 mutex_exit(&vd->vdev_probe_lock); 947 948 if (vps == NULL) { 949 ASSERT(zio != NULL); 950 return (NULL); 951 } 952 953 for (int l = 1; l < VDEV_LABELS; l++) { 954 zio_nowait(zio_read_phys(pio, vd, 955 vdev_label_offset(vd->vdev_psize, l, 956 offsetof(vdev_label_t, vl_pad2)), 957 VDEV_PAD_SIZE, zio_buf_alloc(VDEV_PAD_SIZE), 958 ZIO_CHECKSUM_OFF, vdev_probe_done, vps, 959 ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE)); 960 } 961 962 if (zio == NULL) 963 return (pio); 964 965 zio_nowait(pio); 966 return (NULL); 967 } 968 969 /* 970 * Prepare a virtual device for access. 971 */ 972 int 973 vdev_open(vdev_t *vd) 974 { 975 spa_t *spa = vd->vdev_spa; 976 int error; 977 int c; 978 uint64_t osize = 0; 979 uint64_t asize, psize; 980 uint64_t ashift = 0; 981 982 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 983 984 ASSERT(vd->vdev_state == VDEV_STATE_CLOSED || 985 vd->vdev_state == VDEV_STATE_CANT_OPEN || 986 vd->vdev_state == VDEV_STATE_OFFLINE); 987 988 vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 989 990 if (!vd->vdev_removed && vd->vdev_faulted) { 991 ASSERT(vd->vdev_children == 0); 992 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 993 VDEV_AUX_ERR_EXCEEDED); 994 return (ENXIO); 995 } else if (vd->vdev_offline) { 996 ASSERT(vd->vdev_children == 0); 997 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE); 998 return (ENXIO); 999 } 1000 1001 error = vd->vdev_ops->vdev_op_open(vd, &osize, &ashift); 1002 1003 if (zio_injection_enabled && error == 0) 1004 error = zio_handle_device_injection(vd, ENXIO); 1005 1006 if (error) { 1007 if (vd->vdev_removed && 1008 vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED) 1009 vd->vdev_removed = B_FALSE; 1010 1011 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1012 vd->vdev_stat.vs_aux); 1013 return (error); 1014 } 1015 1016 vd->vdev_removed = B_FALSE; 1017 1018 if (vd->vdev_degraded) { 1019 ASSERT(vd->vdev_children == 0); 1020 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 1021 VDEV_AUX_ERR_EXCEEDED); 1022 } else { 1023 vd->vdev_state = VDEV_STATE_HEALTHY; 1024 } 1025 1026 for (c = 0; c < vd->vdev_children; c++) 1027 if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) { 1028 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 1029 VDEV_AUX_NONE); 1030 break; 1031 } 1032 1033 osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t)); 1034 1035 if (vd->vdev_children == 0) { 1036 if (osize < SPA_MINDEVSIZE) { 1037 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1038 VDEV_AUX_TOO_SMALL); 1039 return (EOVERFLOW); 1040 } 1041 psize = osize; 1042 asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE); 1043 } else { 1044 if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE - 1045 (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) { 1046 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1047 VDEV_AUX_TOO_SMALL); 1048 return (EOVERFLOW); 1049 } 1050 psize = 0; 1051 asize = osize; 1052 } 1053 1054 vd->vdev_psize = psize; 1055 1056 if (vd->vdev_asize == 0) { 1057 /* 1058 * This is the first-ever open, so use the computed values. 1059 * For testing purposes, a higher ashift can be requested. 1060 */ 1061 vd->vdev_asize = asize; 1062 vd->vdev_ashift = MAX(ashift, vd->vdev_ashift); 1063 } else { 1064 /* 1065 * Make sure the alignment requirement hasn't increased. 1066 */ 1067 if (ashift > vd->vdev_top->vdev_ashift) { 1068 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1069 VDEV_AUX_BAD_LABEL); 1070 return (EINVAL); 1071 } 1072 1073 /* 1074 * Make sure the device hasn't shrunk. 1075 */ 1076 if (asize < vd->vdev_asize) { 1077 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1078 VDEV_AUX_BAD_LABEL); 1079 return (EINVAL); 1080 } 1081 1082 /* 1083 * If all children are healthy and the asize has increased, 1084 * then we've experienced dynamic LUN growth. 1085 */ 1086 if (vd->vdev_state == VDEV_STATE_HEALTHY && 1087 asize > vd->vdev_asize) { 1088 vd->vdev_asize = asize; 1089 } 1090 } 1091 1092 /* 1093 * Ensure we can issue some IO before declaring the 1094 * vdev open for business. 1095 */ 1096 if (vd->vdev_ops->vdev_op_leaf && 1097 (error = zio_wait(vdev_probe(vd, NULL))) != 0) { 1098 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1099 VDEV_AUX_IO_FAILURE); 1100 return (error); 1101 } 1102 1103 /* 1104 * If this is a top-level vdev, compute the raidz-deflation 1105 * ratio. Note, we hard-code in 128k (1<<17) because it is the 1106 * current "typical" blocksize. Even if SPA_MAXBLOCKSIZE 1107 * changes, this algorithm must never change, or we will 1108 * inconsistently account for existing bp's. 1109 */ 1110 if (vd->vdev_top == vd) { 1111 vd->vdev_deflate_ratio = (1<<17) / 1112 (vdev_psize_to_asize(vd, 1<<17) >> SPA_MINBLOCKSHIFT); 1113 } 1114 1115 /* 1116 * If a leaf vdev has a DTL, and seems healthy, then kick off a 1117 * resilver. But don't do this if we are doing a reopen for a scrub, 1118 * since this would just restart the scrub we are already doing. 1119 */ 1120 if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen && 1121 vdev_resilver_needed(vd, NULL, NULL)) 1122 spa_async_request(spa, SPA_ASYNC_RESILVER); 1123 1124 return (0); 1125 } 1126 1127 /* 1128 * Called once the vdevs are all opened, this routine validates the label 1129 * contents. This needs to be done before vdev_load() so that we don't 1130 * inadvertently do repair I/Os to the wrong device. 1131 * 1132 * This function will only return failure if one of the vdevs indicates that it 1133 * has since been destroyed or exported. This is only possible if 1134 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state 1135 * will be updated but the function will return 0. 1136 */ 1137 int 1138 vdev_validate(vdev_t *vd) 1139 { 1140 spa_t *spa = vd->vdev_spa; 1141 int c; 1142 nvlist_t *label; 1143 uint64_t guid, top_guid; 1144 uint64_t state; 1145 1146 for (c = 0; c < vd->vdev_children; c++) 1147 if (vdev_validate(vd->vdev_child[c]) != 0) 1148 return (EBADF); 1149 1150 /* 1151 * If the device has already failed, or was marked offline, don't do 1152 * any further validation. Otherwise, label I/O will fail and we will 1153 * overwrite the previous state. 1154 */ 1155 if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) { 1156 1157 if ((label = vdev_label_read_config(vd)) == NULL) { 1158 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1159 VDEV_AUX_BAD_LABEL); 1160 return (0); 1161 } 1162 1163 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, 1164 &guid) != 0 || guid != spa_guid(spa)) { 1165 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1166 VDEV_AUX_CORRUPT_DATA); 1167 nvlist_free(label); 1168 return (0); 1169 } 1170 1171 /* 1172 * If this vdev just became a top-level vdev because its 1173 * sibling was detached, it will have adopted the parent's 1174 * vdev guid -- but the label may or may not be on disk yet. 1175 * Fortunately, either version of the label will have the 1176 * same top guid, so if we're a top-level vdev, we can 1177 * safely compare to that instead. 1178 */ 1179 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, 1180 &guid) != 0 || 1181 nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID, 1182 &top_guid) != 0 || 1183 (vd->vdev_guid != guid && 1184 (vd->vdev_guid != top_guid || vd != vd->vdev_top))) { 1185 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1186 VDEV_AUX_CORRUPT_DATA); 1187 nvlist_free(label); 1188 return (0); 1189 } 1190 1191 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, 1192 &state) != 0) { 1193 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1194 VDEV_AUX_CORRUPT_DATA); 1195 nvlist_free(label); 1196 return (0); 1197 } 1198 1199 nvlist_free(label); 1200 1201 if (spa->spa_load_state == SPA_LOAD_OPEN && 1202 state != POOL_STATE_ACTIVE) 1203 return (EBADF); 1204 1205 /* 1206 * If we were able to open and validate a vdev that was 1207 * previously marked permanently unavailable, clear that state 1208 * now. 1209 */ 1210 if (vd->vdev_not_present) 1211 vd->vdev_not_present = 0; 1212 } 1213 1214 return (0); 1215 } 1216 1217 /* 1218 * Close a virtual device. 1219 */ 1220 void 1221 vdev_close(vdev_t *vd) 1222 { 1223 spa_t *spa = vd->vdev_spa; 1224 1225 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 1226 1227 vd->vdev_ops->vdev_op_close(vd); 1228 1229 vdev_cache_purge(vd); 1230 1231 /* 1232 * We record the previous state before we close it, so that if we are 1233 * doing a reopen(), we don't generate FMA ereports if we notice that 1234 * it's still faulted. 1235 */ 1236 vd->vdev_prevstate = vd->vdev_state; 1237 1238 if (vd->vdev_offline) 1239 vd->vdev_state = VDEV_STATE_OFFLINE; 1240 else 1241 vd->vdev_state = VDEV_STATE_CLOSED; 1242 vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 1243 } 1244 1245 void 1246 vdev_reopen(vdev_t *vd) 1247 { 1248 spa_t *spa = vd->vdev_spa; 1249 1250 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 1251 1252 vdev_close(vd); 1253 (void) vdev_open(vd); 1254 1255 /* 1256 * Call vdev_validate() here to make sure we have the same device. 1257 * Otherwise, a device with an invalid label could be successfully 1258 * opened in response to vdev_reopen(). 1259 */ 1260 if (vd->vdev_aux) { 1261 (void) vdev_validate_aux(vd); 1262 if (vdev_readable(vd) && vdev_writeable(vd) && 1263 vd->vdev_aux == &spa->spa_l2cache && 1264 !l2arc_vdev_present(vd)) { 1265 uint64_t size = vdev_get_rsize(vd); 1266 l2arc_add_vdev(spa, vd, 1267 VDEV_LABEL_START_SIZE, 1268 size - VDEV_LABEL_START_SIZE); 1269 } 1270 } else { 1271 (void) vdev_validate(vd); 1272 } 1273 1274 /* 1275 * Reassess parent vdev's health. 1276 */ 1277 vdev_propagate_state(vd); 1278 } 1279 1280 int 1281 vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing) 1282 { 1283 int error; 1284 1285 /* 1286 * Normally, partial opens (e.g. of a mirror) are allowed. 1287 * For a create, however, we want to fail the request if 1288 * there are any components we can't open. 1289 */ 1290 error = vdev_open(vd); 1291 1292 if (error || vd->vdev_state != VDEV_STATE_HEALTHY) { 1293 vdev_close(vd); 1294 return (error ? error : ENXIO); 1295 } 1296 1297 /* 1298 * Recursively initialize all labels. 1299 */ 1300 if ((error = vdev_label_init(vd, txg, isreplacing ? 1301 VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) { 1302 vdev_close(vd); 1303 return (error); 1304 } 1305 1306 return (0); 1307 } 1308 1309 /* 1310 * The is the latter half of vdev_create(). It is distinct because it 1311 * involves initiating transactions in order to do metaslab creation. 1312 * For creation, we want to try to create all vdevs at once and then undo it 1313 * if anything fails; this is much harder if we have pending transactions. 1314 */ 1315 void 1316 vdev_init(vdev_t *vd, uint64_t txg) 1317 { 1318 /* 1319 * Aim for roughly 200 metaslabs per vdev. 1320 */ 1321 vd->vdev_ms_shift = highbit(vd->vdev_asize / 200); 1322 vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT); 1323 1324 /* 1325 * Initialize the vdev's metaslabs. This can't fail because 1326 * there's nothing to read when creating all new metaslabs. 1327 */ 1328 VERIFY(vdev_metaslab_init(vd, txg) == 0); 1329 } 1330 1331 void 1332 vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg) 1333 { 1334 ASSERT(vd == vd->vdev_top); 1335 ASSERT(ISP2(flags)); 1336 1337 if (flags & VDD_METASLAB) 1338 (void) txg_list_add(&vd->vdev_ms_list, arg, txg); 1339 1340 if (flags & VDD_DTL) 1341 (void) txg_list_add(&vd->vdev_dtl_list, arg, txg); 1342 1343 (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg); 1344 } 1345 1346 /* 1347 * DTLs. 1348 * 1349 * A vdev's DTL (dirty time log) is the set of transaction groups for which 1350 * the vdev has less than perfect replication. There are three kinds of DTL: 1351 * 1352 * DTL_MISSING: txgs for which the vdev has no valid copies of the data 1353 * 1354 * DTL_PARTIAL: txgs for which data is available, but not fully replicated 1355 * 1356 * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon 1357 * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of 1358 * txgs that was scrubbed. 1359 * 1360 * DTL_OUTAGE: txgs which cannot currently be read, whether due to 1361 * persistent errors or just some device being offline. 1362 * Unlike the other three, the DTL_OUTAGE map is not generally 1363 * maintained; it's only computed when needed, typically to 1364 * determine whether a device can be detached. 1365 * 1366 * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device 1367 * either has the data or it doesn't. 1368 * 1369 * For interior vdevs such as mirror and RAID-Z the picture is more complex. 1370 * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because 1371 * if any child is less than fully replicated, then so is its parent. 1372 * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs, 1373 * comprising only those txgs which appear in 'maxfaults' or more children; 1374 * those are the txgs we don't have enough replication to read. For example, 1375 * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2); 1376 * thus, its DTL_MISSING consists of the set of txgs that appear in more than 1377 * two child DTL_MISSING maps. 1378 * 1379 * It should be clear from the above that to compute the DTLs and outage maps 1380 * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps. 1381 * Therefore, that is all we keep on disk. When loading the pool, or after 1382 * a configuration change, we generate all other DTLs from first principles. 1383 */ 1384 void 1385 vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 1386 { 1387 space_map_t *sm = &vd->vdev_dtl[t]; 1388 1389 ASSERT(t < DTL_TYPES); 1390 ASSERT(vd != vd->vdev_spa->spa_root_vdev); 1391 1392 mutex_enter(sm->sm_lock); 1393 if (!space_map_contains(sm, txg, size)) 1394 space_map_add(sm, txg, size); 1395 mutex_exit(sm->sm_lock); 1396 } 1397 1398 boolean_t 1399 vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 1400 { 1401 space_map_t *sm = &vd->vdev_dtl[t]; 1402 boolean_t dirty = B_FALSE; 1403 1404 ASSERT(t < DTL_TYPES); 1405 ASSERT(vd != vd->vdev_spa->spa_root_vdev); 1406 1407 mutex_enter(sm->sm_lock); 1408 if (sm->sm_space != 0) 1409 dirty = space_map_contains(sm, txg, size); 1410 mutex_exit(sm->sm_lock); 1411 1412 return (dirty); 1413 } 1414 1415 boolean_t 1416 vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t) 1417 { 1418 space_map_t *sm = &vd->vdev_dtl[t]; 1419 boolean_t empty; 1420 1421 mutex_enter(sm->sm_lock); 1422 empty = (sm->sm_space == 0); 1423 mutex_exit(sm->sm_lock); 1424 1425 return (empty); 1426 } 1427 1428 /* 1429 * Reassess DTLs after a config change or scrub completion. 1430 */ 1431 void 1432 vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done) 1433 { 1434 spa_t *spa = vd->vdev_spa; 1435 avl_tree_t reftree; 1436 int minref; 1437 1438 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 1439 1440 for (int c = 0; c < vd->vdev_children; c++) 1441 vdev_dtl_reassess(vd->vdev_child[c], txg, 1442 scrub_txg, scrub_done); 1443 1444 if (vd == spa->spa_root_vdev) 1445 return; 1446 1447 if (vd->vdev_ops->vdev_op_leaf) { 1448 mutex_enter(&vd->vdev_dtl_lock); 1449 if (scrub_txg != 0 && 1450 (spa->spa_scrub_started || spa->spa_scrub_errors == 0)) { 1451 /* XXX should check scrub_done? */ 1452 /* 1453 * We completed a scrub up to scrub_txg. If we 1454 * did it without rebooting, then the scrub dtl 1455 * will be valid, so excise the old region and 1456 * fold in the scrub dtl. Otherwise, leave the 1457 * dtl as-is if there was an error. 1458 * 1459 * There's little trick here: to excise the beginning 1460 * of the DTL_MISSING map, we put it into a reference 1461 * tree and then add a segment with refcnt -1 that 1462 * covers the range [0, scrub_txg). This means 1463 * that each txg in that range has refcnt -1 or 0. 1464 * We then add DTL_SCRUB with a refcnt of 2, so that 1465 * entries in the range [0, scrub_txg) will have a 1466 * positive refcnt -- either 1 or 2. We then convert 1467 * the reference tree into the new DTL_MISSING map. 1468 */ 1469 space_map_ref_create(&reftree); 1470 space_map_ref_add_map(&reftree, 1471 &vd->vdev_dtl[DTL_MISSING], 1); 1472 space_map_ref_add_seg(&reftree, 0, scrub_txg, -1); 1473 space_map_ref_add_map(&reftree, 1474 &vd->vdev_dtl[DTL_SCRUB], 2); 1475 space_map_ref_generate_map(&reftree, 1476 &vd->vdev_dtl[DTL_MISSING], 1); 1477 space_map_ref_destroy(&reftree); 1478 } 1479 space_map_vacate(&vd->vdev_dtl[DTL_PARTIAL], NULL, NULL); 1480 space_map_walk(&vd->vdev_dtl[DTL_MISSING], 1481 space_map_add, &vd->vdev_dtl[DTL_PARTIAL]); 1482 if (scrub_done) 1483 space_map_vacate(&vd->vdev_dtl[DTL_SCRUB], NULL, NULL); 1484 space_map_vacate(&vd->vdev_dtl[DTL_OUTAGE], NULL, NULL); 1485 if (!vdev_readable(vd)) 1486 space_map_add(&vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL); 1487 else 1488 space_map_walk(&vd->vdev_dtl[DTL_MISSING], 1489 space_map_add, &vd->vdev_dtl[DTL_OUTAGE]); 1490 mutex_exit(&vd->vdev_dtl_lock); 1491 1492 if (txg != 0) 1493 vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg); 1494 return; 1495 } 1496 1497 mutex_enter(&vd->vdev_dtl_lock); 1498 for (int t = 0; t < DTL_TYPES; t++) { 1499 if (t == DTL_SCRUB) 1500 continue; /* leaf vdevs only */ 1501 if (t == DTL_PARTIAL) 1502 minref = 1; /* i.e. non-zero */ 1503 else if (vd->vdev_nparity != 0) 1504 minref = vd->vdev_nparity + 1; /* RAID-Z */ 1505 else 1506 minref = vd->vdev_children; /* any kind of mirror */ 1507 space_map_ref_create(&reftree); 1508 for (int c = 0; c < vd->vdev_children; c++) { 1509 vdev_t *cvd = vd->vdev_child[c]; 1510 mutex_enter(&cvd->vdev_dtl_lock); 1511 space_map_ref_add_map(&reftree, &cvd->vdev_dtl[t], 1); 1512 mutex_exit(&cvd->vdev_dtl_lock); 1513 } 1514 space_map_ref_generate_map(&reftree, &vd->vdev_dtl[t], minref); 1515 space_map_ref_destroy(&reftree); 1516 } 1517 mutex_exit(&vd->vdev_dtl_lock); 1518 } 1519 1520 static int 1521 vdev_dtl_load(vdev_t *vd) 1522 { 1523 spa_t *spa = vd->vdev_spa; 1524 space_map_obj_t *smo = &vd->vdev_dtl_smo; 1525 objset_t *mos = spa->spa_meta_objset; 1526 dmu_buf_t *db; 1527 int error; 1528 1529 ASSERT(vd->vdev_children == 0); 1530 1531 if (smo->smo_object == 0) 1532 return (0); 1533 1534 if ((error = dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)) != 0) 1535 return (error); 1536 1537 ASSERT3U(db->db_size, >=, sizeof (*smo)); 1538 bcopy(db->db_data, smo, sizeof (*smo)); 1539 dmu_buf_rele(db, FTAG); 1540 1541 mutex_enter(&vd->vdev_dtl_lock); 1542 error = space_map_load(&vd->vdev_dtl[DTL_MISSING], 1543 NULL, SM_ALLOC, smo, mos); 1544 mutex_exit(&vd->vdev_dtl_lock); 1545 1546 return (error); 1547 } 1548 1549 void 1550 vdev_dtl_sync(vdev_t *vd, uint64_t txg) 1551 { 1552 spa_t *spa = vd->vdev_spa; 1553 space_map_obj_t *smo = &vd->vdev_dtl_smo; 1554 space_map_t *sm = &vd->vdev_dtl[DTL_MISSING]; 1555 objset_t *mos = spa->spa_meta_objset; 1556 space_map_t smsync; 1557 kmutex_t smlock; 1558 dmu_buf_t *db; 1559 dmu_tx_t *tx; 1560 1561 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 1562 1563 if (vd->vdev_detached) { 1564 if (smo->smo_object != 0) { 1565 int err = dmu_object_free(mos, smo->smo_object, tx); 1566 ASSERT3U(err, ==, 0); 1567 smo->smo_object = 0; 1568 } 1569 dmu_tx_commit(tx); 1570 return; 1571 } 1572 1573 if (smo->smo_object == 0) { 1574 ASSERT(smo->smo_objsize == 0); 1575 ASSERT(smo->smo_alloc == 0); 1576 smo->smo_object = dmu_object_alloc(mos, 1577 DMU_OT_SPACE_MAP, 1 << SPACE_MAP_BLOCKSHIFT, 1578 DMU_OT_SPACE_MAP_HEADER, sizeof (*smo), tx); 1579 ASSERT(smo->smo_object != 0); 1580 vdev_config_dirty(vd->vdev_top); 1581 } 1582 1583 mutex_init(&smlock, NULL, MUTEX_DEFAULT, NULL); 1584 1585 space_map_create(&smsync, sm->sm_start, sm->sm_size, sm->sm_shift, 1586 &smlock); 1587 1588 mutex_enter(&smlock); 1589 1590 mutex_enter(&vd->vdev_dtl_lock); 1591 space_map_walk(sm, space_map_add, &smsync); 1592 mutex_exit(&vd->vdev_dtl_lock); 1593 1594 space_map_truncate(smo, mos, tx); 1595 space_map_sync(&smsync, SM_ALLOC, smo, mos, tx); 1596 1597 space_map_destroy(&smsync); 1598 1599 mutex_exit(&smlock); 1600 mutex_destroy(&smlock); 1601 1602 VERIFY(0 == dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)); 1603 dmu_buf_will_dirty(db, tx); 1604 ASSERT3U(db->db_size, >=, sizeof (*smo)); 1605 bcopy(smo, db->db_data, sizeof (*smo)); 1606 dmu_buf_rele(db, FTAG); 1607 1608 dmu_tx_commit(tx); 1609 } 1610 1611 /* 1612 * Determine whether the specified vdev can be offlined/detached/removed 1613 * without losing data. 1614 */ 1615 boolean_t 1616 vdev_dtl_required(vdev_t *vd) 1617 { 1618 spa_t *spa = vd->vdev_spa; 1619 vdev_t *tvd = vd->vdev_top; 1620 uint8_t cant_read = vd->vdev_cant_read; 1621 boolean_t required; 1622 1623 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 1624 1625 if (vd == spa->spa_root_vdev || vd == tvd) 1626 return (B_TRUE); 1627 1628 /* 1629 * Temporarily mark the device as unreadable, and then determine 1630 * whether this results in any DTL outages in the top-level vdev. 1631 * If not, we can safely offline/detach/remove the device. 1632 */ 1633 vd->vdev_cant_read = B_TRUE; 1634 vdev_dtl_reassess(tvd, 0, 0, B_FALSE); 1635 required = !vdev_dtl_empty(tvd, DTL_OUTAGE); 1636 vd->vdev_cant_read = cant_read; 1637 vdev_dtl_reassess(tvd, 0, 0, B_FALSE); 1638 1639 return (required); 1640 } 1641 1642 /* 1643 * Determine if resilver is needed, and if so the txg range. 1644 */ 1645 boolean_t 1646 vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp) 1647 { 1648 boolean_t needed = B_FALSE; 1649 uint64_t thismin = UINT64_MAX; 1650 uint64_t thismax = 0; 1651 1652 if (vd->vdev_children == 0) { 1653 mutex_enter(&vd->vdev_dtl_lock); 1654 if (vd->vdev_dtl[DTL_MISSING].sm_space != 0 && 1655 vdev_writeable(vd)) { 1656 space_seg_t *ss; 1657 1658 ss = avl_first(&vd->vdev_dtl[DTL_MISSING].sm_root); 1659 thismin = ss->ss_start - 1; 1660 ss = avl_last(&vd->vdev_dtl[DTL_MISSING].sm_root); 1661 thismax = ss->ss_end; 1662 needed = B_TRUE; 1663 } 1664 mutex_exit(&vd->vdev_dtl_lock); 1665 } else { 1666 for (int c = 0; c < vd->vdev_children; c++) { 1667 vdev_t *cvd = vd->vdev_child[c]; 1668 uint64_t cmin, cmax; 1669 1670 if (vdev_resilver_needed(cvd, &cmin, &cmax)) { 1671 thismin = MIN(thismin, cmin); 1672 thismax = MAX(thismax, cmax); 1673 needed = B_TRUE; 1674 } 1675 } 1676 } 1677 1678 if (needed && minp) { 1679 *minp = thismin; 1680 *maxp = thismax; 1681 } 1682 return (needed); 1683 } 1684 1685 void 1686 vdev_load(vdev_t *vd) 1687 { 1688 /* 1689 * Recursively load all children. 1690 */ 1691 for (int c = 0; c < vd->vdev_children; c++) 1692 vdev_load(vd->vdev_child[c]); 1693 1694 /* 1695 * If this is a top-level vdev, initialize its metaslabs. 1696 */ 1697 if (vd == vd->vdev_top && 1698 (vd->vdev_ashift == 0 || vd->vdev_asize == 0 || 1699 vdev_metaslab_init(vd, 0) != 0)) 1700 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1701 VDEV_AUX_CORRUPT_DATA); 1702 1703 /* 1704 * If this is a leaf vdev, load its DTL. 1705 */ 1706 if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0) 1707 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1708 VDEV_AUX_CORRUPT_DATA); 1709 } 1710 1711 /* 1712 * The special vdev case is used for hot spares and l2cache devices. Its 1713 * sole purpose it to set the vdev state for the associated vdev. To do this, 1714 * we make sure that we can open the underlying device, then try to read the 1715 * label, and make sure that the label is sane and that it hasn't been 1716 * repurposed to another pool. 1717 */ 1718 int 1719 vdev_validate_aux(vdev_t *vd) 1720 { 1721 nvlist_t *label; 1722 uint64_t guid, version; 1723 uint64_t state; 1724 1725 if (!vdev_readable(vd)) 1726 return (0); 1727 1728 if ((label = vdev_label_read_config(vd)) == NULL) { 1729 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1730 VDEV_AUX_CORRUPT_DATA); 1731 return (-1); 1732 } 1733 1734 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 || 1735 version > SPA_VERSION || 1736 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 || 1737 guid != vd->vdev_guid || 1738 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) { 1739 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1740 VDEV_AUX_CORRUPT_DATA); 1741 nvlist_free(label); 1742 return (-1); 1743 } 1744 1745 /* 1746 * We don't actually check the pool state here. If it's in fact in 1747 * use by another pool, we update this fact on the fly when requested. 1748 */ 1749 nvlist_free(label); 1750 return (0); 1751 } 1752 1753 void 1754 vdev_sync_done(vdev_t *vd, uint64_t txg) 1755 { 1756 metaslab_t *msp; 1757 1758 while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg))) 1759 metaslab_sync_done(msp, txg); 1760 } 1761 1762 void 1763 vdev_sync(vdev_t *vd, uint64_t txg) 1764 { 1765 spa_t *spa = vd->vdev_spa; 1766 vdev_t *lvd; 1767 metaslab_t *msp; 1768 dmu_tx_t *tx; 1769 1770 if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) { 1771 ASSERT(vd == vd->vdev_top); 1772 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 1773 vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset, 1774 DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx); 1775 ASSERT(vd->vdev_ms_array != 0); 1776 vdev_config_dirty(vd); 1777 dmu_tx_commit(tx); 1778 } 1779 1780 while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) { 1781 metaslab_sync(msp, txg); 1782 (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg)); 1783 } 1784 1785 while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL) 1786 vdev_dtl_sync(lvd, txg); 1787 1788 (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)); 1789 } 1790 1791 uint64_t 1792 vdev_psize_to_asize(vdev_t *vd, uint64_t psize) 1793 { 1794 return (vd->vdev_ops->vdev_op_asize(vd, psize)); 1795 } 1796 1797 /* 1798 * Mark the given vdev faulted. A faulted vdev behaves as if the device could 1799 * not be opened, and no I/O is attempted. 1800 */ 1801 int 1802 vdev_fault(spa_t *spa, uint64_t guid) 1803 { 1804 vdev_t *vd; 1805 1806 spa_vdev_state_enter(spa); 1807 1808 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 1809 return (spa_vdev_state_exit(spa, NULL, ENODEV)); 1810 1811 if (!vd->vdev_ops->vdev_op_leaf) 1812 return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 1813 1814 /* 1815 * Faulted state takes precedence over degraded. 1816 */ 1817 vd->vdev_faulted = 1ULL; 1818 vd->vdev_degraded = 0ULL; 1819 vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, VDEV_AUX_ERR_EXCEEDED); 1820 1821 /* 1822 * If marking the vdev as faulted cause the top-level vdev to become 1823 * unavailable, then back off and simply mark the vdev as degraded 1824 * instead. 1825 */ 1826 if (vdev_is_dead(vd->vdev_top) && vd->vdev_aux == NULL) { 1827 vd->vdev_degraded = 1ULL; 1828 vd->vdev_faulted = 0ULL; 1829 1830 /* 1831 * If we reopen the device and it's not dead, only then do we 1832 * mark it degraded. 1833 */ 1834 vdev_reopen(vd); 1835 1836 if (vdev_readable(vd)) { 1837 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, 1838 VDEV_AUX_ERR_EXCEEDED); 1839 } 1840 } 1841 1842 return (spa_vdev_state_exit(spa, vd, 0)); 1843 } 1844 1845 /* 1846 * Mark the given vdev degraded. A degraded vdev is purely an indication to the 1847 * user that something is wrong. The vdev continues to operate as normal as far 1848 * as I/O is concerned. 1849 */ 1850 int 1851 vdev_degrade(spa_t *spa, uint64_t guid) 1852 { 1853 vdev_t *vd; 1854 1855 spa_vdev_state_enter(spa); 1856 1857 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 1858 return (spa_vdev_state_exit(spa, NULL, ENODEV)); 1859 1860 if (!vd->vdev_ops->vdev_op_leaf) 1861 return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 1862 1863 /* 1864 * If the vdev is already faulted, then don't do anything. 1865 */ 1866 if (vd->vdev_faulted || vd->vdev_degraded) 1867 return (spa_vdev_state_exit(spa, NULL, 0)); 1868 1869 vd->vdev_degraded = 1ULL; 1870 if (!vdev_is_dead(vd)) 1871 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, 1872 VDEV_AUX_ERR_EXCEEDED); 1873 1874 return (spa_vdev_state_exit(spa, vd, 0)); 1875 } 1876 1877 /* 1878 * Online the given vdev. If 'unspare' is set, it implies two things. First, 1879 * any attached spare device should be detached when the device finishes 1880 * resilvering. Second, the online should be treated like a 'test' online case, 1881 * so no FMA events are generated if the device fails to open. 1882 */ 1883 int 1884 vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate) 1885 { 1886 vdev_t *vd; 1887 1888 spa_vdev_state_enter(spa); 1889 1890 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 1891 return (spa_vdev_state_exit(spa, NULL, ENODEV)); 1892 1893 if (!vd->vdev_ops->vdev_op_leaf) 1894 return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 1895 1896 vd->vdev_offline = B_FALSE; 1897 vd->vdev_tmpoffline = B_FALSE; 1898 vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE); 1899 vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT); 1900 vdev_reopen(vd->vdev_top); 1901 vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE; 1902 1903 if (newstate) 1904 *newstate = vd->vdev_state; 1905 if ((flags & ZFS_ONLINE_UNSPARE) && 1906 !vdev_is_dead(vd) && vd->vdev_parent && 1907 vd->vdev_parent->vdev_ops == &vdev_spare_ops && 1908 vd->vdev_parent->vdev_child[0] == vd) 1909 vd->vdev_unspare = B_TRUE; 1910 1911 return (spa_vdev_state_exit(spa, vd, 0)); 1912 } 1913 1914 int 1915 vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags) 1916 { 1917 vdev_t *vd; 1918 1919 spa_vdev_state_enter(spa); 1920 1921 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 1922 return (spa_vdev_state_exit(spa, NULL, ENODEV)); 1923 1924 if (!vd->vdev_ops->vdev_op_leaf) 1925 return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 1926 1927 /* 1928 * If the device isn't already offline, try to offline it. 1929 */ 1930 if (!vd->vdev_offline) { 1931 /* 1932 * If this device has the only valid copy of some data, 1933 * don't allow it to be offlined. 1934 */ 1935 if (vd->vdev_aux == NULL && vdev_dtl_required(vd)) 1936 return (spa_vdev_state_exit(spa, NULL, EBUSY)); 1937 1938 /* 1939 * Offline this device and reopen its top-level vdev. 1940 * If this action results in the top-level vdev becoming 1941 * unusable, undo it and fail the request. 1942 */ 1943 vd->vdev_offline = B_TRUE; 1944 vdev_reopen(vd->vdev_top); 1945 if (vd->vdev_aux == NULL && vdev_is_dead(vd->vdev_top)) { 1946 vd->vdev_offline = B_FALSE; 1947 vdev_reopen(vd->vdev_top); 1948 return (spa_vdev_state_exit(spa, NULL, EBUSY)); 1949 } 1950 } 1951 1952 vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY); 1953 1954 return (spa_vdev_state_exit(spa, vd, 0)); 1955 } 1956 1957 /* 1958 * Clear the error counts associated with this vdev. Unlike vdev_online() and 1959 * vdev_offline(), we assume the spa config is locked. We also clear all 1960 * children. If 'vd' is NULL, then the user wants to clear all vdevs. 1961 */ 1962 void 1963 vdev_clear(spa_t *spa, vdev_t *vd) 1964 { 1965 vdev_t *rvd = spa->spa_root_vdev; 1966 1967 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 1968 1969 if (vd == NULL) 1970 vd = rvd; 1971 1972 vd->vdev_stat.vs_read_errors = 0; 1973 vd->vdev_stat.vs_write_errors = 0; 1974 vd->vdev_stat.vs_checksum_errors = 0; 1975 1976 for (int c = 0; c < vd->vdev_children; c++) 1977 vdev_clear(spa, vd->vdev_child[c]); 1978 1979 /* 1980 * If we're in the FAULTED state or have experienced failed I/O, then 1981 * clear the persistent state and attempt to reopen the device. We 1982 * also mark the vdev config dirty, so that the new faulted state is 1983 * written out to disk. 1984 */ 1985 if (vd->vdev_faulted || vd->vdev_degraded || 1986 !vdev_readable(vd) || !vdev_writeable(vd)) { 1987 1988 vd->vdev_faulted = vd->vdev_degraded = 0; 1989 vd->vdev_cant_read = B_FALSE; 1990 vd->vdev_cant_write = B_FALSE; 1991 1992 vdev_reopen(vd); 1993 1994 if (vd != rvd) 1995 vdev_state_dirty(vd->vdev_top); 1996 1997 if (vd->vdev_aux == NULL && !vdev_is_dead(vd)) 1998 spa_async_request(spa, SPA_ASYNC_RESILVER); 1999 2000 spa_event_notify(spa, vd, ESC_ZFS_VDEV_CLEAR); 2001 } 2002 } 2003 2004 boolean_t 2005 vdev_is_dead(vdev_t *vd) 2006 { 2007 return (vd->vdev_state < VDEV_STATE_DEGRADED); 2008 } 2009 2010 boolean_t 2011 vdev_readable(vdev_t *vd) 2012 { 2013 return (!vdev_is_dead(vd) && !vd->vdev_cant_read); 2014 } 2015 2016 boolean_t 2017 vdev_writeable(vdev_t *vd) 2018 { 2019 return (!vdev_is_dead(vd) && !vd->vdev_cant_write); 2020 } 2021 2022 boolean_t 2023 vdev_allocatable(vdev_t *vd) 2024 { 2025 uint64_t state = vd->vdev_state; 2026 2027 /* 2028 * We currently allow allocations from vdevs which may be in the 2029 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device 2030 * fails to reopen then we'll catch it later when we're holding 2031 * the proper locks. Note that we have to get the vdev state 2032 * in a local variable because although it changes atomically, 2033 * we're asking two separate questions about it. 2034 */ 2035 return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) && 2036 !vd->vdev_cant_write); 2037 } 2038 2039 boolean_t 2040 vdev_accessible(vdev_t *vd, zio_t *zio) 2041 { 2042 ASSERT(zio->io_vd == vd); 2043 2044 if (vdev_is_dead(vd) || vd->vdev_remove_wanted) 2045 return (B_FALSE); 2046 2047 if (zio->io_type == ZIO_TYPE_READ) 2048 return (!vd->vdev_cant_read); 2049 2050 if (zio->io_type == ZIO_TYPE_WRITE) 2051 return (!vd->vdev_cant_write); 2052 2053 return (B_TRUE); 2054 } 2055 2056 /* 2057 * Get statistics for the given vdev. 2058 */ 2059 void 2060 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs) 2061 { 2062 vdev_t *rvd = vd->vdev_spa->spa_root_vdev; 2063 2064 mutex_enter(&vd->vdev_stat_lock); 2065 bcopy(&vd->vdev_stat, vs, sizeof (*vs)); 2066 vs->vs_scrub_errors = vd->vdev_spa->spa_scrub_errors; 2067 vs->vs_timestamp = gethrtime() - vs->vs_timestamp; 2068 vs->vs_state = vd->vdev_state; 2069 vs->vs_rsize = vdev_get_rsize(vd); 2070 mutex_exit(&vd->vdev_stat_lock); 2071 2072 /* 2073 * If we're getting stats on the root vdev, aggregate the I/O counts 2074 * over all top-level vdevs (i.e. the direct children of the root). 2075 */ 2076 if (vd == rvd) { 2077 for (int c = 0; c < rvd->vdev_children; c++) { 2078 vdev_t *cvd = rvd->vdev_child[c]; 2079 vdev_stat_t *cvs = &cvd->vdev_stat; 2080 2081 mutex_enter(&vd->vdev_stat_lock); 2082 for (int t = 0; t < ZIO_TYPES; t++) { 2083 vs->vs_ops[t] += cvs->vs_ops[t]; 2084 vs->vs_bytes[t] += cvs->vs_bytes[t]; 2085 } 2086 vs->vs_scrub_examined += cvs->vs_scrub_examined; 2087 mutex_exit(&vd->vdev_stat_lock); 2088 } 2089 } 2090 } 2091 2092 void 2093 vdev_clear_stats(vdev_t *vd) 2094 { 2095 mutex_enter(&vd->vdev_stat_lock); 2096 vd->vdev_stat.vs_space = 0; 2097 vd->vdev_stat.vs_dspace = 0; 2098 vd->vdev_stat.vs_alloc = 0; 2099 mutex_exit(&vd->vdev_stat_lock); 2100 } 2101 2102 void 2103 vdev_stat_update(zio_t *zio, uint64_t psize) 2104 { 2105 spa_t *spa = zio->io_spa; 2106 vdev_t *rvd = spa->spa_root_vdev; 2107 vdev_t *vd = zio->io_vd ? zio->io_vd : rvd; 2108 vdev_t *pvd; 2109 uint64_t txg = zio->io_txg; 2110 vdev_stat_t *vs = &vd->vdev_stat; 2111 zio_type_t type = zio->io_type; 2112 int flags = zio->io_flags; 2113 2114 /* 2115 * If this i/o is a gang leader, it didn't do any actual work. 2116 */ 2117 if (zio->io_gang_tree) 2118 return; 2119 2120 if (zio->io_error == 0) { 2121 /* 2122 * If this is a root i/o, don't count it -- we've already 2123 * counted the top-level vdevs, and vdev_get_stats() will 2124 * aggregate them when asked. This reduces contention on 2125 * the root vdev_stat_lock and implicitly handles blocks 2126 * that compress away to holes, for which there is no i/o. 2127 * (Holes never create vdev children, so all the counters 2128 * remain zero, which is what we want.) 2129 * 2130 * Note: this only applies to successful i/o (io_error == 0) 2131 * because unlike i/o counts, errors are not additive. 2132 * When reading a ditto block, for example, failure of 2133 * one top-level vdev does not imply a root-level error. 2134 */ 2135 if (vd == rvd) 2136 return; 2137 2138 ASSERT(vd == zio->io_vd); 2139 2140 if (flags & ZIO_FLAG_IO_BYPASS) 2141 return; 2142 2143 mutex_enter(&vd->vdev_stat_lock); 2144 2145 if (flags & ZIO_FLAG_IO_REPAIR) { 2146 if (flags & ZIO_FLAG_SCRUB_THREAD) 2147 vs->vs_scrub_repaired += psize; 2148 if (flags & ZIO_FLAG_SELF_HEAL) 2149 vs->vs_self_healed += psize; 2150 } 2151 2152 vs->vs_ops[type]++; 2153 vs->vs_bytes[type] += psize; 2154 2155 mutex_exit(&vd->vdev_stat_lock); 2156 return; 2157 } 2158 2159 if (flags & ZIO_FLAG_SPECULATIVE) 2160 return; 2161 2162 mutex_enter(&vd->vdev_stat_lock); 2163 if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) { 2164 if (zio->io_error == ECKSUM) 2165 vs->vs_checksum_errors++; 2166 else 2167 vs->vs_read_errors++; 2168 } 2169 if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd)) 2170 vs->vs_write_errors++; 2171 mutex_exit(&vd->vdev_stat_lock); 2172 2173 if (type == ZIO_TYPE_WRITE && txg != 0 && 2174 (!(flags & ZIO_FLAG_IO_REPAIR) || 2175 (flags & ZIO_FLAG_SCRUB_THREAD))) { 2176 /* 2177 * This is either a normal write (not a repair), or it's a 2178 * repair induced by the scrub thread. In the normal case, 2179 * we commit the DTL change in the same txg as the block 2180 * was born. In the scrub-induced repair case, we know that 2181 * scrubs run in first-pass syncing context, so we commit 2182 * the DTL change in spa->spa_syncing_txg. 2183 * 2184 * We currently do not make DTL entries for failed spontaneous 2185 * self-healing writes triggered by normal (non-scrubbing) 2186 * reads, because we have no transactional context in which to 2187 * do so -- and it's not clear that it'd be desirable anyway. 2188 */ 2189 if (vd->vdev_ops->vdev_op_leaf) { 2190 uint64_t commit_txg = txg; 2191 if (flags & ZIO_FLAG_SCRUB_THREAD) { 2192 ASSERT(flags & ZIO_FLAG_IO_REPAIR); 2193 ASSERT(spa_sync_pass(spa) == 1); 2194 vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1); 2195 commit_txg = spa->spa_syncing_txg; 2196 } 2197 ASSERT(commit_txg >= spa->spa_syncing_txg); 2198 if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1)) 2199 return; 2200 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 2201 vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1); 2202 vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg); 2203 } 2204 if (vd != rvd) 2205 vdev_dtl_dirty(vd, DTL_MISSING, txg, 1); 2206 } 2207 } 2208 2209 void 2210 vdev_scrub_stat_update(vdev_t *vd, pool_scrub_type_t type, boolean_t complete) 2211 { 2212 int c; 2213 vdev_stat_t *vs = &vd->vdev_stat; 2214 2215 for (c = 0; c < vd->vdev_children; c++) 2216 vdev_scrub_stat_update(vd->vdev_child[c], type, complete); 2217 2218 mutex_enter(&vd->vdev_stat_lock); 2219 2220 if (type == POOL_SCRUB_NONE) { 2221 /* 2222 * Update completion and end time. Leave everything else alone 2223 * so we can report what happened during the previous scrub. 2224 */ 2225 vs->vs_scrub_complete = complete; 2226 vs->vs_scrub_end = gethrestime_sec(); 2227 } else { 2228 vs->vs_scrub_type = type; 2229 vs->vs_scrub_complete = 0; 2230 vs->vs_scrub_examined = 0; 2231 vs->vs_scrub_repaired = 0; 2232 vs->vs_scrub_start = gethrestime_sec(); 2233 vs->vs_scrub_end = 0; 2234 } 2235 2236 mutex_exit(&vd->vdev_stat_lock); 2237 } 2238 2239 /* 2240 * Update the in-core space usage stats for this vdev and the root vdev. 2241 */ 2242 void 2243 vdev_space_update(vdev_t *vd, int64_t space_delta, int64_t alloc_delta, 2244 boolean_t update_root) 2245 { 2246 int64_t dspace_delta = space_delta; 2247 spa_t *spa = vd->vdev_spa; 2248 vdev_t *rvd = spa->spa_root_vdev; 2249 2250 ASSERT(vd == vd->vdev_top); 2251 2252 /* 2253 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion 2254 * factor. We must calculate this here and not at the root vdev 2255 * because the root vdev's psize-to-asize is simply the max of its 2256 * childrens', thus not accurate enough for us. 2257 */ 2258 ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0); 2259 dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) * 2260 vd->vdev_deflate_ratio; 2261 2262 mutex_enter(&vd->vdev_stat_lock); 2263 vd->vdev_stat.vs_space += space_delta; 2264 vd->vdev_stat.vs_alloc += alloc_delta; 2265 vd->vdev_stat.vs_dspace += dspace_delta; 2266 mutex_exit(&vd->vdev_stat_lock); 2267 2268 if (update_root) { 2269 ASSERT(rvd == vd->vdev_parent); 2270 ASSERT(vd->vdev_ms_count != 0); 2271 2272 /* 2273 * Don't count non-normal (e.g. intent log) space as part of 2274 * the pool's capacity. 2275 */ 2276 if (vd->vdev_mg->mg_class != spa->spa_normal_class) 2277 return; 2278 2279 mutex_enter(&rvd->vdev_stat_lock); 2280 rvd->vdev_stat.vs_space += space_delta; 2281 rvd->vdev_stat.vs_alloc += alloc_delta; 2282 rvd->vdev_stat.vs_dspace += dspace_delta; 2283 mutex_exit(&rvd->vdev_stat_lock); 2284 } 2285 } 2286 2287 /* 2288 * Mark a top-level vdev's config as dirty, placing it on the dirty list 2289 * so that it will be written out next time the vdev configuration is synced. 2290 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs. 2291 */ 2292 void 2293 vdev_config_dirty(vdev_t *vd) 2294 { 2295 spa_t *spa = vd->vdev_spa; 2296 vdev_t *rvd = spa->spa_root_vdev; 2297 int c; 2298 2299 /* 2300 * If this is an aux vdev (as with l2cache and spare devices), then we 2301 * update the vdev config manually and set the sync flag. 2302 */ 2303 if (vd->vdev_aux != NULL) { 2304 spa_aux_vdev_t *sav = vd->vdev_aux; 2305 nvlist_t **aux; 2306 uint_t naux; 2307 2308 for (c = 0; c < sav->sav_count; c++) { 2309 if (sav->sav_vdevs[c] == vd) 2310 break; 2311 } 2312 2313 if (c == sav->sav_count) { 2314 /* 2315 * We're being removed. There's nothing more to do. 2316 */ 2317 ASSERT(sav->sav_sync == B_TRUE); 2318 return; 2319 } 2320 2321 sav->sav_sync = B_TRUE; 2322 2323 if (nvlist_lookup_nvlist_array(sav->sav_config, 2324 ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) { 2325 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 2326 ZPOOL_CONFIG_SPARES, &aux, &naux) == 0); 2327 } 2328 2329 ASSERT(c < naux); 2330 2331 /* 2332 * Setting the nvlist in the middle if the array is a little 2333 * sketchy, but it will work. 2334 */ 2335 nvlist_free(aux[c]); 2336 aux[c] = vdev_config_generate(spa, vd, B_TRUE, B_FALSE, B_TRUE); 2337 2338 return; 2339 } 2340 2341 /* 2342 * The dirty list is protected by the SCL_CONFIG lock. The caller 2343 * must either hold SCL_CONFIG as writer, or must be the sync thread 2344 * (which holds SCL_CONFIG as reader). There's only one sync thread, 2345 * so this is sufficient to ensure mutual exclusion. 2346 */ 2347 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 2348 (dsl_pool_sync_context(spa_get_dsl(spa)) && 2349 spa_config_held(spa, SCL_CONFIG, RW_READER))); 2350 2351 if (vd == rvd) { 2352 for (c = 0; c < rvd->vdev_children; c++) 2353 vdev_config_dirty(rvd->vdev_child[c]); 2354 } else { 2355 ASSERT(vd == vd->vdev_top); 2356 2357 if (!list_link_active(&vd->vdev_config_dirty_node)) 2358 list_insert_head(&spa->spa_config_dirty_list, vd); 2359 } 2360 } 2361 2362 void 2363 vdev_config_clean(vdev_t *vd) 2364 { 2365 spa_t *spa = vd->vdev_spa; 2366 2367 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 2368 (dsl_pool_sync_context(spa_get_dsl(spa)) && 2369 spa_config_held(spa, SCL_CONFIG, RW_READER))); 2370 2371 ASSERT(list_link_active(&vd->vdev_config_dirty_node)); 2372 list_remove(&spa->spa_config_dirty_list, vd); 2373 } 2374 2375 /* 2376 * Mark a top-level vdev's state as dirty, so that the next pass of 2377 * spa_sync() can convert this into vdev_config_dirty(). We distinguish 2378 * the state changes from larger config changes because they require 2379 * much less locking, and are often needed for administrative actions. 2380 */ 2381 void 2382 vdev_state_dirty(vdev_t *vd) 2383 { 2384 spa_t *spa = vd->vdev_spa; 2385 2386 ASSERT(vd == vd->vdev_top); 2387 2388 /* 2389 * The state list is protected by the SCL_STATE lock. The caller 2390 * must either hold SCL_STATE as writer, or must be the sync thread 2391 * (which holds SCL_STATE as reader). There's only one sync thread, 2392 * so this is sufficient to ensure mutual exclusion. 2393 */ 2394 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 2395 (dsl_pool_sync_context(spa_get_dsl(spa)) && 2396 spa_config_held(spa, SCL_STATE, RW_READER))); 2397 2398 if (!list_link_active(&vd->vdev_state_dirty_node)) 2399 list_insert_head(&spa->spa_state_dirty_list, vd); 2400 } 2401 2402 void 2403 vdev_state_clean(vdev_t *vd) 2404 { 2405 spa_t *spa = vd->vdev_spa; 2406 2407 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 2408 (dsl_pool_sync_context(spa_get_dsl(spa)) && 2409 spa_config_held(spa, SCL_STATE, RW_READER))); 2410 2411 ASSERT(list_link_active(&vd->vdev_state_dirty_node)); 2412 list_remove(&spa->spa_state_dirty_list, vd); 2413 } 2414 2415 /* 2416 * Propagate vdev state up from children to parent. 2417 */ 2418 void 2419 vdev_propagate_state(vdev_t *vd) 2420 { 2421 spa_t *spa = vd->vdev_spa; 2422 vdev_t *rvd = spa->spa_root_vdev; 2423 int degraded = 0, faulted = 0; 2424 int corrupted = 0; 2425 int c; 2426 vdev_t *child; 2427 2428 if (vd->vdev_children > 0) { 2429 for (c = 0; c < vd->vdev_children; c++) { 2430 child = vd->vdev_child[c]; 2431 2432 if (!vdev_readable(child) || 2433 (!vdev_writeable(child) && spa_writeable(spa))) { 2434 /* 2435 * Root special: if there is a top-level log 2436 * device, treat the root vdev as if it were 2437 * degraded. 2438 */ 2439 if (child->vdev_islog && vd == rvd) 2440 degraded++; 2441 else 2442 faulted++; 2443 } else if (child->vdev_state <= VDEV_STATE_DEGRADED) { 2444 degraded++; 2445 } 2446 2447 if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA) 2448 corrupted++; 2449 } 2450 2451 vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded); 2452 2453 /* 2454 * Root special: if there is a top-level vdev that cannot be 2455 * opened due to corrupted metadata, then propagate the root 2456 * vdev's aux state as 'corrupt' rather than 'insufficient 2457 * replicas'. 2458 */ 2459 if (corrupted && vd == rvd && 2460 rvd->vdev_state == VDEV_STATE_CANT_OPEN) 2461 vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN, 2462 VDEV_AUX_CORRUPT_DATA); 2463 } 2464 2465 if (vd->vdev_parent) 2466 vdev_propagate_state(vd->vdev_parent); 2467 } 2468 2469 /* 2470 * Set a vdev's state. If this is during an open, we don't update the parent 2471 * state, because we're in the process of opening children depth-first. 2472 * Otherwise, we propagate the change to the parent. 2473 * 2474 * If this routine places a device in a faulted state, an appropriate ereport is 2475 * generated. 2476 */ 2477 void 2478 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux) 2479 { 2480 uint64_t save_state; 2481 spa_t *spa = vd->vdev_spa; 2482 2483 if (state == vd->vdev_state) { 2484 vd->vdev_stat.vs_aux = aux; 2485 return; 2486 } 2487 2488 save_state = vd->vdev_state; 2489 2490 vd->vdev_state = state; 2491 vd->vdev_stat.vs_aux = aux; 2492 2493 /* 2494 * If we are setting the vdev state to anything but an open state, then 2495 * always close the underlying device. Otherwise, we keep accessible 2496 * but invalid devices open forever. We don't call vdev_close() itself, 2497 * because that implies some extra checks (offline, etc) that we don't 2498 * want here. This is limited to leaf devices, because otherwise 2499 * closing the device will affect other children. 2500 */ 2501 if (vdev_is_dead(vd) && vd->vdev_ops->vdev_op_leaf) 2502 vd->vdev_ops->vdev_op_close(vd); 2503 2504 if (vd->vdev_removed && 2505 state == VDEV_STATE_CANT_OPEN && 2506 (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) { 2507 /* 2508 * If the previous state is set to VDEV_STATE_REMOVED, then this 2509 * device was previously marked removed and someone attempted to 2510 * reopen it. If this failed due to a nonexistent device, then 2511 * keep the device in the REMOVED state. We also let this be if 2512 * it is one of our special test online cases, which is only 2513 * attempting to online the device and shouldn't generate an FMA 2514 * fault. 2515 */ 2516 vd->vdev_state = VDEV_STATE_REMOVED; 2517 vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 2518 } else if (state == VDEV_STATE_REMOVED) { 2519 /* 2520 * Indicate to the ZFS DE that this device has been removed, and 2521 * any recent errors should be ignored. 2522 */ 2523 zfs_post_remove(spa, vd); 2524 vd->vdev_removed = B_TRUE; 2525 } else if (state == VDEV_STATE_CANT_OPEN) { 2526 /* 2527 * If we fail to open a vdev during an import, we mark it as 2528 * "not available", which signifies that it was never there to 2529 * begin with. Failure to open such a device is not considered 2530 * an error. 2531 */ 2532 if (spa->spa_load_state == SPA_LOAD_IMPORT && 2533 vd->vdev_ops->vdev_op_leaf) 2534 vd->vdev_not_present = 1; 2535 2536 /* 2537 * Post the appropriate ereport. If the 'prevstate' field is 2538 * set to something other than VDEV_STATE_UNKNOWN, it indicates 2539 * that this is part of a vdev_reopen(). In this case, we don't 2540 * want to post the ereport if the device was already in the 2541 * CANT_OPEN state beforehand. 2542 * 2543 * If the 'checkremove' flag is set, then this is an attempt to 2544 * online the device in response to an insertion event. If we 2545 * hit this case, then we have detected an insertion event for a 2546 * faulted or offline device that wasn't in the removed state. 2547 * In this scenario, we don't post an ereport because we are 2548 * about to replace the device, or attempt an online with 2549 * vdev_forcefault, which will generate the fault for us. 2550 */ 2551 if ((vd->vdev_prevstate != state || vd->vdev_forcefault) && 2552 !vd->vdev_not_present && !vd->vdev_checkremove && 2553 vd != spa->spa_root_vdev) { 2554 const char *class; 2555 2556 switch (aux) { 2557 case VDEV_AUX_OPEN_FAILED: 2558 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED; 2559 break; 2560 case VDEV_AUX_CORRUPT_DATA: 2561 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA; 2562 break; 2563 case VDEV_AUX_NO_REPLICAS: 2564 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS; 2565 break; 2566 case VDEV_AUX_BAD_GUID_SUM: 2567 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM; 2568 break; 2569 case VDEV_AUX_TOO_SMALL: 2570 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL; 2571 break; 2572 case VDEV_AUX_BAD_LABEL: 2573 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL; 2574 break; 2575 case VDEV_AUX_IO_FAILURE: 2576 class = FM_EREPORT_ZFS_IO_FAILURE; 2577 break; 2578 default: 2579 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN; 2580 } 2581 2582 zfs_ereport_post(class, spa, vd, NULL, save_state, 0); 2583 } 2584 2585 /* Erase any notion of persistent removed state */ 2586 vd->vdev_removed = B_FALSE; 2587 } else { 2588 vd->vdev_removed = B_FALSE; 2589 } 2590 2591 if (!isopen) 2592 vdev_propagate_state(vd); 2593 } 2594 2595 /* 2596 * Check the vdev configuration to ensure that it's capable of supporting 2597 * a root pool. Currently, we do not support RAID-Z or partial configuration. 2598 * In addition, only a single top-level vdev is allowed and none of the leaves 2599 * can be wholedisks. 2600 */ 2601 boolean_t 2602 vdev_is_bootable(vdev_t *vd) 2603 { 2604 int c; 2605 2606 if (!vd->vdev_ops->vdev_op_leaf) { 2607 char *vdev_type = vd->vdev_ops->vdev_op_type; 2608 2609 if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 && 2610 vd->vdev_children > 1) { 2611 return (B_FALSE); 2612 } else if (strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 || 2613 strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) { 2614 return (B_FALSE); 2615 } 2616 } else if (vd->vdev_wholedisk == 1) { 2617 return (B_FALSE); 2618 } 2619 2620 for (c = 0; c < vd->vdev_children; c++) { 2621 if (!vdev_is_bootable(vd->vdev_child[c])) 2622 return (B_FALSE); 2623 } 2624 return (B_TRUE); 2625 } 2626