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 2007 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include <sys/zfs_context.h> 30 #include <sys/fm/fs/zfs.h> 31 #include <sys/spa.h> 32 #include <sys/spa_impl.h> 33 #include <sys/dmu.h> 34 #include <sys/dmu_tx.h> 35 #include <sys/vdev_impl.h> 36 #include <sys/uberblock_impl.h> 37 #include <sys/metaslab.h> 38 #include <sys/metaslab_impl.h> 39 #include <sys/space_map.h> 40 #include <sys/zio.h> 41 #include <sys/zap.h> 42 #include <sys/fs/zfs.h> 43 44 /* 45 * Virtual device management. 46 */ 47 48 static vdev_ops_t *vdev_ops_table[] = { 49 &vdev_root_ops, 50 &vdev_raidz_ops, 51 &vdev_mirror_ops, 52 &vdev_replacing_ops, 53 &vdev_spare_ops, 54 &vdev_disk_ops, 55 &vdev_file_ops, 56 &vdev_missing_ops, 57 NULL 58 }; 59 60 /* maximum scrub/resilver I/O queue */ 61 int zfs_scrub_limit = 70; 62 63 /* 64 * Given a vdev type, return the appropriate ops vector. 65 */ 66 static vdev_ops_t * 67 vdev_getops(const char *type) 68 { 69 vdev_ops_t *ops, **opspp; 70 71 for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++) 72 if (strcmp(ops->vdev_op_type, type) == 0) 73 break; 74 75 return (ops); 76 } 77 78 /* 79 * Default asize function: return the MAX of psize with the asize of 80 * all children. This is what's used by anything other than RAID-Z. 81 */ 82 uint64_t 83 vdev_default_asize(vdev_t *vd, uint64_t psize) 84 { 85 uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift); 86 uint64_t csize; 87 uint64_t c; 88 89 for (c = 0; c < vd->vdev_children; c++) { 90 csize = vdev_psize_to_asize(vd->vdev_child[c], psize); 91 asize = MAX(asize, csize); 92 } 93 94 return (asize); 95 } 96 97 /* 98 * Get the replaceable or attachable device size. 99 * If the parent is a mirror or raidz, the replaceable size is the minimum 100 * psize of all its children. For the rest, just return our own psize. 101 * 102 * e.g. 103 * psize rsize 104 * root - - 105 * mirror/raidz - - 106 * disk1 20g 20g 107 * disk2 40g 20g 108 * disk3 80g 80g 109 */ 110 uint64_t 111 vdev_get_rsize(vdev_t *vd) 112 { 113 vdev_t *pvd, *cvd; 114 uint64_t c, rsize; 115 116 pvd = vd->vdev_parent; 117 118 /* 119 * If our parent is NULL or the root, just return our own psize. 120 */ 121 if (pvd == NULL || pvd->vdev_parent == NULL) 122 return (vd->vdev_psize); 123 124 rsize = 0; 125 126 for (c = 0; c < pvd->vdev_children; c++) { 127 cvd = pvd->vdev_child[c]; 128 rsize = MIN(rsize - 1, cvd->vdev_psize - 1) + 1; 129 } 130 131 return (rsize); 132 } 133 134 vdev_t * 135 vdev_lookup_top(spa_t *spa, uint64_t vdev) 136 { 137 vdev_t *rvd = spa->spa_root_vdev; 138 139 if (vdev < rvd->vdev_children) 140 return (rvd->vdev_child[vdev]); 141 142 return (NULL); 143 } 144 145 vdev_t * 146 vdev_lookup_by_guid(vdev_t *vd, uint64_t guid) 147 { 148 int c; 149 vdev_t *mvd; 150 151 if (vd->vdev_guid == guid) 152 return (vd); 153 154 for (c = 0; c < vd->vdev_children; c++) 155 if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) != 156 NULL) 157 return (mvd); 158 159 return (NULL); 160 } 161 162 void 163 vdev_add_child(vdev_t *pvd, vdev_t *cvd) 164 { 165 size_t oldsize, newsize; 166 uint64_t id = cvd->vdev_id; 167 vdev_t **newchild; 168 169 ASSERT(spa_config_held(cvd->vdev_spa, RW_WRITER)); 170 ASSERT(cvd->vdev_parent == NULL); 171 172 cvd->vdev_parent = pvd; 173 174 if (pvd == NULL) 175 return; 176 177 ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL); 178 179 oldsize = pvd->vdev_children * sizeof (vdev_t *); 180 pvd->vdev_children = MAX(pvd->vdev_children, id + 1); 181 newsize = pvd->vdev_children * sizeof (vdev_t *); 182 183 newchild = kmem_zalloc(newsize, KM_SLEEP); 184 if (pvd->vdev_child != NULL) { 185 bcopy(pvd->vdev_child, newchild, oldsize); 186 kmem_free(pvd->vdev_child, oldsize); 187 } 188 189 pvd->vdev_child = newchild; 190 pvd->vdev_child[id] = cvd; 191 192 cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd); 193 ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL); 194 195 /* 196 * Walk up all ancestors to update guid sum. 197 */ 198 for (; pvd != NULL; pvd = pvd->vdev_parent) 199 pvd->vdev_guid_sum += cvd->vdev_guid_sum; 200 201 if (cvd->vdev_ops->vdev_op_leaf) 202 cvd->vdev_spa->spa_scrub_maxinflight += zfs_scrub_limit; 203 } 204 205 void 206 vdev_remove_child(vdev_t *pvd, vdev_t *cvd) 207 { 208 int c; 209 uint_t id = cvd->vdev_id; 210 211 ASSERT(cvd->vdev_parent == pvd); 212 213 if (pvd == NULL) 214 return; 215 216 ASSERT(id < pvd->vdev_children); 217 ASSERT(pvd->vdev_child[id] == cvd); 218 219 pvd->vdev_child[id] = NULL; 220 cvd->vdev_parent = NULL; 221 222 for (c = 0; c < pvd->vdev_children; c++) 223 if (pvd->vdev_child[c]) 224 break; 225 226 if (c == pvd->vdev_children) { 227 kmem_free(pvd->vdev_child, c * sizeof (vdev_t *)); 228 pvd->vdev_child = NULL; 229 pvd->vdev_children = 0; 230 } 231 232 /* 233 * Walk up all ancestors to update guid sum. 234 */ 235 for (; pvd != NULL; pvd = pvd->vdev_parent) 236 pvd->vdev_guid_sum -= cvd->vdev_guid_sum; 237 238 if (cvd->vdev_ops->vdev_op_leaf) 239 cvd->vdev_spa->spa_scrub_maxinflight -= zfs_scrub_limit; 240 } 241 242 /* 243 * Remove any holes in the child array. 244 */ 245 void 246 vdev_compact_children(vdev_t *pvd) 247 { 248 vdev_t **newchild, *cvd; 249 int oldc = pvd->vdev_children; 250 int newc, c; 251 252 ASSERT(spa_config_held(pvd->vdev_spa, RW_WRITER)); 253 254 for (c = newc = 0; c < oldc; c++) 255 if (pvd->vdev_child[c]) 256 newc++; 257 258 newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP); 259 260 for (c = newc = 0; c < oldc; c++) { 261 if ((cvd = pvd->vdev_child[c]) != NULL) { 262 newchild[newc] = cvd; 263 cvd->vdev_id = newc++; 264 } 265 } 266 267 kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *)); 268 pvd->vdev_child = newchild; 269 pvd->vdev_children = newc; 270 } 271 272 /* 273 * Allocate and minimally initialize a vdev_t. 274 */ 275 static vdev_t * 276 vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops) 277 { 278 vdev_t *vd; 279 280 vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP); 281 282 if (spa->spa_root_vdev == NULL) { 283 ASSERT(ops == &vdev_root_ops); 284 spa->spa_root_vdev = vd; 285 } 286 287 if (guid == 0) { 288 if (spa->spa_root_vdev == vd) { 289 /* 290 * The root vdev's guid will also be the pool guid, 291 * which must be unique among all pools. 292 */ 293 while (guid == 0 || spa_guid_exists(guid, 0)) 294 guid = spa_get_random(-1ULL); 295 } else { 296 /* 297 * Any other vdev's guid must be unique within the pool. 298 */ 299 while (guid == 0 || 300 spa_guid_exists(spa_guid(spa), guid)) 301 guid = spa_get_random(-1ULL); 302 } 303 ASSERT(!spa_guid_exists(spa_guid(spa), guid)); 304 } 305 306 vd->vdev_spa = spa; 307 vd->vdev_id = id; 308 vd->vdev_guid = guid; 309 vd->vdev_guid_sum = guid; 310 vd->vdev_ops = ops; 311 vd->vdev_state = VDEV_STATE_CLOSED; 312 313 mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL); 314 mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL); 315 space_map_create(&vd->vdev_dtl_map, 0, -1ULL, 0, &vd->vdev_dtl_lock); 316 space_map_create(&vd->vdev_dtl_scrub, 0, -1ULL, 0, &vd->vdev_dtl_lock); 317 txg_list_create(&vd->vdev_ms_list, 318 offsetof(struct metaslab, ms_txg_node)); 319 txg_list_create(&vd->vdev_dtl_list, 320 offsetof(struct vdev, vdev_dtl_node)); 321 vd->vdev_stat.vs_timestamp = gethrtime(); 322 vdev_queue_init(vd); 323 vdev_cache_init(vd); 324 325 return (vd); 326 } 327 328 /* 329 * Allocate a new vdev. The 'alloctype' is used to control whether we are 330 * creating a new vdev or loading an existing one - the behavior is slightly 331 * different for each case. 332 */ 333 int 334 vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id, 335 int alloctype) 336 { 337 vdev_ops_t *ops; 338 char *type; 339 uint64_t guid = 0; 340 vdev_t *vd; 341 342 ASSERT(spa_config_held(spa, RW_WRITER)); 343 344 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0) 345 return (EINVAL); 346 347 if ((ops = vdev_getops(type)) == NULL) 348 return (EINVAL); 349 350 /* 351 * If this is a load, get the vdev guid from the nvlist. 352 * Otherwise, vdev_alloc_common() will generate one for us. 353 */ 354 if (alloctype == VDEV_ALLOC_LOAD) { 355 uint64_t label_id; 356 357 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) || 358 label_id != id) 359 return (EINVAL); 360 361 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 362 return (EINVAL); 363 } else if (alloctype == VDEV_ALLOC_SPARE) { 364 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 365 return (EINVAL); 366 } 367 368 /* 369 * The first allocated vdev must be of type 'root'. 370 */ 371 if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL) 372 return (EINVAL); 373 374 vd = vdev_alloc_common(spa, id, guid, ops); 375 376 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0) 377 vd->vdev_path = spa_strdup(vd->vdev_path); 378 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0) 379 vd->vdev_devid = spa_strdup(vd->vdev_devid); 380 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH, 381 &vd->vdev_physpath) == 0) 382 vd->vdev_physpath = spa_strdup(vd->vdev_physpath); 383 384 /* 385 * Set the nparity propery for RAID-Z vdevs. 386 */ 387 if (ops == &vdev_raidz_ops) { 388 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 389 &vd->vdev_nparity) == 0) { 390 /* 391 * Currently, we can only support 2 parity devices. 392 */ 393 if (vd->vdev_nparity > 2) 394 return (EINVAL); 395 /* 396 * Older versions can only support 1 parity device. 397 */ 398 if (vd->vdev_nparity == 2 && 399 spa_version(spa) < ZFS_VERSION_RAID6) 400 return (ENOTSUP); 401 402 } else { 403 /* 404 * We require the parity to be specified for SPAs that 405 * support multiple parity levels. 406 */ 407 if (spa_version(spa) >= ZFS_VERSION_RAID6) 408 return (EINVAL); 409 410 /* 411 * Otherwise, we default to 1 parity device for RAID-Z. 412 */ 413 vd->vdev_nparity = 1; 414 } 415 } else { 416 vd->vdev_nparity = 0; 417 } 418 419 /* 420 * Set the whole_disk property. If it's not specified, leave the value 421 * as -1. 422 */ 423 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 424 &vd->vdev_wholedisk) != 0) 425 vd->vdev_wholedisk = -1ULL; 426 427 /* 428 * Look for the 'not present' flag. This will only be set if the device 429 * was not present at the time of import. 430 */ 431 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 432 &vd->vdev_not_present); 433 434 /* 435 * Get the alignment requirement. 436 */ 437 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift); 438 439 /* 440 * If we're a top-level vdev, try to load the allocation parameters. 441 */ 442 if (parent && !parent->vdev_parent && alloctype == VDEV_ALLOC_LOAD) { 443 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY, 444 &vd->vdev_ms_array); 445 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT, 446 &vd->vdev_ms_shift); 447 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE, 448 &vd->vdev_asize); 449 } 450 451 /* 452 * If we're a leaf vdev, try to load the DTL object and other state. 453 */ 454 if (vd->vdev_ops->vdev_op_leaf && alloctype == VDEV_ALLOC_LOAD) { 455 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL, 456 &vd->vdev_dtl.smo_object); 457 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, 458 &vd->vdev_offline); 459 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE, 460 &vd->vdev_unspare); 461 /* 462 * When importing a pool, we want to ignore the persistent fault 463 * state, as the diagnosis made on another system may not be 464 * valid in the current context. 465 */ 466 if (spa->spa_load_state == SPA_LOAD_OPEN) { 467 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, 468 &vd->vdev_faulted); 469 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED, 470 &vd->vdev_degraded); 471 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, 472 &vd->vdev_removed); 473 } 474 } 475 476 /* 477 * Add ourselves to the parent's list of children. 478 */ 479 vdev_add_child(parent, vd); 480 481 *vdp = vd; 482 483 return (0); 484 } 485 486 void 487 vdev_free(vdev_t *vd) 488 { 489 int c; 490 spa_t *spa = vd->vdev_spa; 491 492 /* 493 * vdev_free() implies closing the vdev first. This is simpler than 494 * trying to ensure complicated semantics for all callers. 495 */ 496 vdev_close(vd); 497 498 499 ASSERT(!list_link_active(&vd->vdev_dirty_node)); 500 501 /* 502 * Free all children. 503 */ 504 for (c = 0; c < vd->vdev_children; c++) 505 vdev_free(vd->vdev_child[c]); 506 507 ASSERT(vd->vdev_child == NULL); 508 ASSERT(vd->vdev_guid_sum == vd->vdev_guid); 509 510 /* 511 * Discard allocation state. 512 */ 513 if (vd == vd->vdev_top) 514 vdev_metaslab_fini(vd); 515 516 ASSERT3U(vd->vdev_stat.vs_space, ==, 0); 517 ASSERT3U(vd->vdev_stat.vs_dspace, ==, 0); 518 ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0); 519 520 /* 521 * Remove this vdev from its parent's child list. 522 */ 523 vdev_remove_child(vd->vdev_parent, vd); 524 525 ASSERT(vd->vdev_parent == NULL); 526 527 /* 528 * Clean up vdev structure. 529 */ 530 vdev_queue_fini(vd); 531 vdev_cache_fini(vd); 532 533 if (vd->vdev_path) 534 spa_strfree(vd->vdev_path); 535 if (vd->vdev_devid) 536 spa_strfree(vd->vdev_devid); 537 if (vd->vdev_physpath) 538 spa_strfree(vd->vdev_physpath); 539 540 if (vd->vdev_isspare) 541 spa_spare_remove(vd); 542 543 txg_list_destroy(&vd->vdev_ms_list); 544 txg_list_destroy(&vd->vdev_dtl_list); 545 mutex_enter(&vd->vdev_dtl_lock); 546 space_map_unload(&vd->vdev_dtl_map); 547 space_map_destroy(&vd->vdev_dtl_map); 548 space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL); 549 space_map_destroy(&vd->vdev_dtl_scrub); 550 mutex_exit(&vd->vdev_dtl_lock); 551 mutex_destroy(&vd->vdev_dtl_lock); 552 mutex_destroy(&vd->vdev_stat_lock); 553 554 if (vd == spa->spa_root_vdev) 555 spa->spa_root_vdev = NULL; 556 557 kmem_free(vd, sizeof (vdev_t)); 558 } 559 560 /* 561 * Transfer top-level vdev state from svd to tvd. 562 */ 563 static void 564 vdev_top_transfer(vdev_t *svd, vdev_t *tvd) 565 { 566 spa_t *spa = svd->vdev_spa; 567 metaslab_t *msp; 568 vdev_t *vd; 569 int t; 570 571 ASSERT(tvd == tvd->vdev_top); 572 573 tvd->vdev_ms_array = svd->vdev_ms_array; 574 tvd->vdev_ms_shift = svd->vdev_ms_shift; 575 tvd->vdev_ms_count = svd->vdev_ms_count; 576 577 svd->vdev_ms_array = 0; 578 svd->vdev_ms_shift = 0; 579 svd->vdev_ms_count = 0; 580 581 tvd->vdev_mg = svd->vdev_mg; 582 tvd->vdev_ms = svd->vdev_ms; 583 584 svd->vdev_mg = NULL; 585 svd->vdev_ms = NULL; 586 587 if (tvd->vdev_mg != NULL) 588 tvd->vdev_mg->mg_vd = tvd; 589 590 tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc; 591 tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space; 592 tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace; 593 594 svd->vdev_stat.vs_alloc = 0; 595 svd->vdev_stat.vs_space = 0; 596 svd->vdev_stat.vs_dspace = 0; 597 598 for (t = 0; t < TXG_SIZE; t++) { 599 while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL) 600 (void) txg_list_add(&tvd->vdev_ms_list, msp, t); 601 while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL) 602 (void) txg_list_add(&tvd->vdev_dtl_list, vd, t); 603 if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t)) 604 (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t); 605 } 606 607 if (list_link_active(&svd->vdev_dirty_node)) { 608 vdev_config_clean(svd); 609 vdev_config_dirty(tvd); 610 } 611 612 tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio; 613 svd->vdev_deflate_ratio = 0; 614 } 615 616 static void 617 vdev_top_update(vdev_t *tvd, vdev_t *vd) 618 { 619 int c; 620 621 if (vd == NULL) 622 return; 623 624 vd->vdev_top = tvd; 625 626 for (c = 0; c < vd->vdev_children; c++) 627 vdev_top_update(tvd, vd->vdev_child[c]); 628 } 629 630 /* 631 * Add a mirror/replacing vdev above an existing vdev. 632 */ 633 vdev_t * 634 vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops) 635 { 636 spa_t *spa = cvd->vdev_spa; 637 vdev_t *pvd = cvd->vdev_parent; 638 vdev_t *mvd; 639 640 ASSERT(spa_config_held(spa, RW_WRITER)); 641 642 mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops); 643 644 mvd->vdev_asize = cvd->vdev_asize; 645 mvd->vdev_ashift = cvd->vdev_ashift; 646 mvd->vdev_state = cvd->vdev_state; 647 648 vdev_remove_child(pvd, cvd); 649 vdev_add_child(pvd, mvd); 650 cvd->vdev_id = mvd->vdev_children; 651 vdev_add_child(mvd, cvd); 652 vdev_top_update(cvd->vdev_top, cvd->vdev_top); 653 654 if (mvd == mvd->vdev_top) 655 vdev_top_transfer(cvd, mvd); 656 657 return (mvd); 658 } 659 660 /* 661 * Remove a 1-way mirror/replacing vdev from the tree. 662 */ 663 void 664 vdev_remove_parent(vdev_t *cvd) 665 { 666 vdev_t *mvd = cvd->vdev_parent; 667 vdev_t *pvd = mvd->vdev_parent; 668 669 ASSERT(spa_config_held(cvd->vdev_spa, RW_WRITER)); 670 671 ASSERT(mvd->vdev_children == 1); 672 ASSERT(mvd->vdev_ops == &vdev_mirror_ops || 673 mvd->vdev_ops == &vdev_replacing_ops || 674 mvd->vdev_ops == &vdev_spare_ops); 675 cvd->vdev_ashift = mvd->vdev_ashift; 676 677 vdev_remove_child(mvd, cvd); 678 vdev_remove_child(pvd, mvd); 679 cvd->vdev_id = mvd->vdev_id; 680 vdev_add_child(pvd, cvd); 681 /* 682 * If we created a new toplevel vdev, then we need to change the child's 683 * vdev GUID to match the old toplevel vdev. Otherwise, we could have 684 * detached an offline device, and when we go to import the pool we'll 685 * think we have two toplevel vdevs, instead of a different version of 686 * the same toplevel vdev. 687 */ 688 if (cvd->vdev_top == cvd) { 689 pvd->vdev_guid_sum -= cvd->vdev_guid; 690 cvd->vdev_guid_sum -= cvd->vdev_guid; 691 cvd->vdev_guid = mvd->vdev_guid; 692 cvd->vdev_guid_sum += mvd->vdev_guid; 693 pvd->vdev_guid_sum += cvd->vdev_guid; 694 } 695 vdev_top_update(cvd->vdev_top, cvd->vdev_top); 696 697 if (cvd == cvd->vdev_top) 698 vdev_top_transfer(mvd, cvd); 699 700 ASSERT(mvd->vdev_children == 0); 701 vdev_free(mvd); 702 } 703 704 int 705 vdev_metaslab_init(vdev_t *vd, uint64_t txg) 706 { 707 spa_t *spa = vd->vdev_spa; 708 objset_t *mos = spa->spa_meta_objset; 709 metaslab_class_t *mc = spa_metaslab_class_select(spa); 710 uint64_t m; 711 uint64_t oldc = vd->vdev_ms_count; 712 uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift; 713 metaslab_t **mspp; 714 int error; 715 716 if (vd->vdev_ms_shift == 0) /* not being allocated from yet */ 717 return (0); 718 719 dprintf("%s oldc %llu newc %llu\n", vdev_description(vd), oldc, newc); 720 721 ASSERT(oldc <= newc); 722 723 if (vd->vdev_mg == NULL) 724 vd->vdev_mg = metaslab_group_create(mc, vd); 725 726 mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP); 727 728 if (oldc != 0) { 729 bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp)); 730 kmem_free(vd->vdev_ms, oldc * sizeof (*mspp)); 731 } 732 733 vd->vdev_ms = mspp; 734 vd->vdev_ms_count = newc; 735 736 for (m = oldc; m < newc; m++) { 737 space_map_obj_t smo = { 0, 0, 0 }; 738 if (txg == 0) { 739 uint64_t object = 0; 740 error = dmu_read(mos, vd->vdev_ms_array, 741 m * sizeof (uint64_t), sizeof (uint64_t), &object); 742 if (error) 743 return (error); 744 if (object != 0) { 745 dmu_buf_t *db; 746 error = dmu_bonus_hold(mos, object, FTAG, &db); 747 if (error) 748 return (error); 749 ASSERT3U(db->db_size, ==, sizeof (smo)); 750 bcopy(db->db_data, &smo, db->db_size); 751 ASSERT3U(smo.smo_object, ==, object); 752 dmu_buf_rele(db, FTAG); 753 } 754 } 755 vd->vdev_ms[m] = metaslab_init(vd->vdev_mg, &smo, 756 m << vd->vdev_ms_shift, 1ULL << vd->vdev_ms_shift, txg); 757 } 758 759 return (0); 760 } 761 762 void 763 vdev_metaslab_fini(vdev_t *vd) 764 { 765 uint64_t m; 766 uint64_t count = vd->vdev_ms_count; 767 768 if (vd->vdev_ms != NULL) { 769 for (m = 0; m < count; m++) 770 if (vd->vdev_ms[m] != NULL) 771 metaslab_fini(vd->vdev_ms[m]); 772 kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *)); 773 vd->vdev_ms = NULL; 774 } 775 } 776 777 /* 778 * Prepare a virtual device for access. 779 */ 780 int 781 vdev_open(vdev_t *vd) 782 { 783 int error; 784 int c; 785 uint64_t osize = 0; 786 uint64_t asize, psize; 787 uint64_t ashift = 0; 788 789 ASSERT(vd->vdev_state == VDEV_STATE_CLOSED || 790 vd->vdev_state == VDEV_STATE_CANT_OPEN || 791 vd->vdev_state == VDEV_STATE_OFFLINE); 792 793 if (vd->vdev_fault_mode == VDEV_FAULT_COUNT) 794 vd->vdev_fault_arg >>= 1; 795 else 796 vd->vdev_fault_mode = VDEV_FAULT_NONE; 797 798 vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 799 800 if (!vd->vdev_removed && vd->vdev_faulted) { 801 ASSERT(vd->vdev_children == 0); 802 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 803 VDEV_AUX_ERR_EXCEEDED); 804 return (ENXIO); 805 } else if (vd->vdev_offline) { 806 ASSERT(vd->vdev_children == 0); 807 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE); 808 return (ENXIO); 809 } 810 811 error = vd->vdev_ops->vdev_op_open(vd, &osize, &ashift); 812 813 if (zio_injection_enabled && error == 0) 814 error = zio_handle_device_injection(vd, ENXIO); 815 816 if (error) { 817 if (vd->vdev_removed && 818 vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED) 819 vd->vdev_removed = B_FALSE; 820 821 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 822 vd->vdev_stat.vs_aux); 823 return (error); 824 } 825 826 vd->vdev_removed = B_FALSE; 827 828 if (vd->vdev_degraded) { 829 ASSERT(vd->vdev_children == 0); 830 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 831 VDEV_AUX_ERR_EXCEEDED); 832 } else { 833 vd->vdev_state = VDEV_STATE_HEALTHY; 834 } 835 836 for (c = 0; c < vd->vdev_children; c++) 837 if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) { 838 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 839 VDEV_AUX_NONE); 840 break; 841 } 842 843 osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t)); 844 845 if (vd->vdev_children == 0) { 846 if (osize < SPA_MINDEVSIZE) { 847 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 848 VDEV_AUX_TOO_SMALL); 849 return (EOVERFLOW); 850 } 851 psize = osize; 852 asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE); 853 } else { 854 if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE - 855 (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) { 856 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 857 VDEV_AUX_TOO_SMALL); 858 return (EOVERFLOW); 859 } 860 psize = 0; 861 asize = osize; 862 } 863 864 vd->vdev_psize = psize; 865 866 if (vd->vdev_asize == 0) { 867 /* 868 * This is the first-ever open, so use the computed values. 869 * For testing purposes, a higher ashift can be requested. 870 */ 871 vd->vdev_asize = asize; 872 vd->vdev_ashift = MAX(ashift, vd->vdev_ashift); 873 } else { 874 /* 875 * Make sure the alignment requirement hasn't increased. 876 */ 877 if (ashift > vd->vdev_top->vdev_ashift) { 878 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 879 VDEV_AUX_BAD_LABEL); 880 return (EINVAL); 881 } 882 883 /* 884 * Make sure the device hasn't shrunk. 885 */ 886 if (asize < vd->vdev_asize) { 887 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 888 VDEV_AUX_BAD_LABEL); 889 return (EINVAL); 890 } 891 892 /* 893 * If all children are healthy and the asize has increased, 894 * then we've experienced dynamic LUN growth. 895 */ 896 if (vd->vdev_state == VDEV_STATE_HEALTHY && 897 asize > vd->vdev_asize) { 898 vd->vdev_asize = asize; 899 } 900 } 901 902 /* 903 * If this is a top-level vdev, compute the raidz-deflation 904 * ratio. Note, we hard-code in 128k (1<<17) because it is the 905 * current "typical" blocksize. Even if SPA_MAXBLOCKSIZE 906 * changes, this algorithm must never change, or we will 907 * inconsistently account for existing bp's. 908 */ 909 if (vd->vdev_top == vd) { 910 vd->vdev_deflate_ratio = (1<<17) / 911 (vdev_psize_to_asize(vd, 1<<17) >> SPA_MINBLOCKSHIFT); 912 } 913 914 /* 915 * This allows the ZFS DE to close cases appropriately. If a device 916 * goes away and later returns, we want to close the associated case. 917 * But it's not enough to simply post this only when a device goes from 918 * CANT_OPEN -> HEALTHY. If we reboot the system and the device is 919 * back, we also need to close the case (otherwise we will try to replay 920 * it). So we have to post this notifier every time. Since this only 921 * occurs during pool open or error recovery, this should not be an 922 * issue. 923 */ 924 zfs_post_ok(vd->vdev_spa, vd); 925 926 return (0); 927 } 928 929 /* 930 * Called once the vdevs are all opened, this routine validates the label 931 * contents. This needs to be done before vdev_load() so that we don't 932 * inadvertently do repair I/Os to the wrong device. 933 * 934 * This function will only return failure if one of the vdevs indicates that it 935 * has since been destroyed or exported. This is only possible if 936 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state 937 * will be updated but the function will return 0. 938 */ 939 int 940 vdev_validate(vdev_t *vd) 941 { 942 spa_t *spa = vd->vdev_spa; 943 int c; 944 nvlist_t *label; 945 uint64_t guid; 946 uint64_t state; 947 948 for (c = 0; c < vd->vdev_children; c++) 949 if (vdev_validate(vd->vdev_child[c]) != 0) 950 return (EBADF); 951 952 /* 953 * If the device has already failed, or was marked offline, don't do 954 * any further validation. Otherwise, label I/O will fail and we will 955 * overwrite the previous state. 956 */ 957 if (vd->vdev_ops->vdev_op_leaf && !vdev_is_dead(vd)) { 958 959 if ((label = vdev_label_read_config(vd)) == NULL) { 960 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 961 VDEV_AUX_BAD_LABEL); 962 return (0); 963 } 964 965 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, 966 &guid) != 0 || guid != spa_guid(spa)) { 967 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 968 VDEV_AUX_CORRUPT_DATA); 969 nvlist_free(label); 970 return (0); 971 } 972 973 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, 974 &guid) != 0 || guid != vd->vdev_guid) { 975 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 976 VDEV_AUX_CORRUPT_DATA); 977 nvlist_free(label); 978 return (0); 979 } 980 981 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, 982 &state) != 0) { 983 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 984 VDEV_AUX_CORRUPT_DATA); 985 nvlist_free(label); 986 return (0); 987 } 988 989 nvlist_free(label); 990 991 if (spa->spa_load_state == SPA_LOAD_OPEN && 992 state != POOL_STATE_ACTIVE) 993 return (EBADF); 994 } 995 996 /* 997 * If we were able to open and validate a vdev that was previously 998 * marked permanently unavailable, clear that state now. 999 */ 1000 if (vd->vdev_not_present) 1001 vd->vdev_not_present = 0; 1002 1003 return (0); 1004 } 1005 1006 /* 1007 * Close a virtual device. 1008 */ 1009 void 1010 vdev_close(vdev_t *vd) 1011 { 1012 vd->vdev_ops->vdev_op_close(vd); 1013 1014 vdev_cache_purge(vd); 1015 1016 /* 1017 * We record the previous state before we close it, so that if we are 1018 * doing a reopen(), we don't generate FMA ereports if we notice that 1019 * it's still faulted. 1020 */ 1021 vd->vdev_prevstate = vd->vdev_state; 1022 1023 if (vd->vdev_offline) 1024 vd->vdev_state = VDEV_STATE_OFFLINE; 1025 else 1026 vd->vdev_state = VDEV_STATE_CLOSED; 1027 vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 1028 } 1029 1030 void 1031 vdev_reopen(vdev_t *vd) 1032 { 1033 spa_t *spa = vd->vdev_spa; 1034 1035 ASSERT(spa_config_held(spa, RW_WRITER)); 1036 1037 vdev_close(vd); 1038 (void) vdev_open(vd); 1039 1040 /* 1041 * Call vdev_validate() here to make sure we have the same device. 1042 * Otherwise, a device with an invalid label could be successfully 1043 * opened in response to vdev_reopen(). 1044 */ 1045 (void) vdev_validate(vd); 1046 1047 /* 1048 * Reassess parent vdev's health. 1049 */ 1050 vdev_propagate_state(vd); 1051 } 1052 1053 int 1054 vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing) 1055 { 1056 int error; 1057 1058 /* 1059 * Normally, partial opens (e.g. of a mirror) are allowed. 1060 * For a create, however, we want to fail the request if 1061 * there are any components we can't open. 1062 */ 1063 error = vdev_open(vd); 1064 1065 if (error || vd->vdev_state != VDEV_STATE_HEALTHY) { 1066 vdev_close(vd); 1067 return (error ? error : ENXIO); 1068 } 1069 1070 /* 1071 * Recursively initialize all labels. 1072 */ 1073 if ((error = vdev_label_init(vd, txg, isreplacing ? 1074 VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) { 1075 vdev_close(vd); 1076 return (error); 1077 } 1078 1079 return (0); 1080 } 1081 1082 /* 1083 * The is the latter half of vdev_create(). It is distinct because it 1084 * involves initiating transactions in order to do metaslab creation. 1085 * For creation, we want to try to create all vdevs at once and then undo it 1086 * if anything fails; this is much harder if we have pending transactions. 1087 */ 1088 void 1089 vdev_init(vdev_t *vd, uint64_t txg) 1090 { 1091 /* 1092 * Aim for roughly 200 metaslabs per vdev. 1093 */ 1094 vd->vdev_ms_shift = highbit(vd->vdev_asize / 200); 1095 vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT); 1096 1097 /* 1098 * Initialize the vdev's metaslabs. This can't fail because 1099 * there's nothing to read when creating all new metaslabs. 1100 */ 1101 VERIFY(vdev_metaslab_init(vd, txg) == 0); 1102 } 1103 1104 void 1105 vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg) 1106 { 1107 ASSERT(vd == vd->vdev_top); 1108 ASSERT(ISP2(flags)); 1109 1110 if (flags & VDD_METASLAB) 1111 (void) txg_list_add(&vd->vdev_ms_list, arg, txg); 1112 1113 if (flags & VDD_DTL) 1114 (void) txg_list_add(&vd->vdev_dtl_list, arg, txg); 1115 1116 (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg); 1117 } 1118 1119 void 1120 vdev_dtl_dirty(space_map_t *sm, uint64_t txg, uint64_t size) 1121 { 1122 mutex_enter(sm->sm_lock); 1123 if (!space_map_contains(sm, txg, size)) 1124 space_map_add(sm, txg, size); 1125 mutex_exit(sm->sm_lock); 1126 } 1127 1128 int 1129 vdev_dtl_contains(space_map_t *sm, uint64_t txg, uint64_t size) 1130 { 1131 int dirty; 1132 1133 /* 1134 * Quick test without the lock -- covers the common case that 1135 * there are no dirty time segments. 1136 */ 1137 if (sm->sm_space == 0) 1138 return (0); 1139 1140 mutex_enter(sm->sm_lock); 1141 dirty = space_map_contains(sm, txg, size); 1142 mutex_exit(sm->sm_lock); 1143 1144 return (dirty); 1145 } 1146 1147 /* 1148 * Reassess DTLs after a config change or scrub completion. 1149 */ 1150 void 1151 vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done) 1152 { 1153 spa_t *spa = vd->vdev_spa; 1154 int c; 1155 1156 ASSERT(spa_config_held(spa, RW_WRITER)); 1157 1158 if (vd->vdev_children == 0) { 1159 mutex_enter(&vd->vdev_dtl_lock); 1160 /* 1161 * We're successfully scrubbed everything up to scrub_txg. 1162 * Therefore, excise all old DTLs up to that point, then 1163 * fold in the DTLs for everything we couldn't scrub. 1164 */ 1165 if (scrub_txg != 0) { 1166 space_map_excise(&vd->vdev_dtl_map, 0, scrub_txg); 1167 space_map_union(&vd->vdev_dtl_map, &vd->vdev_dtl_scrub); 1168 } 1169 if (scrub_done) 1170 space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL); 1171 mutex_exit(&vd->vdev_dtl_lock); 1172 if (txg != 0) 1173 vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg); 1174 return; 1175 } 1176 1177 /* 1178 * Make sure the DTLs are always correct under the scrub lock. 1179 */ 1180 if (vd == spa->spa_root_vdev) 1181 mutex_enter(&spa->spa_scrub_lock); 1182 1183 mutex_enter(&vd->vdev_dtl_lock); 1184 space_map_vacate(&vd->vdev_dtl_map, NULL, NULL); 1185 space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL); 1186 mutex_exit(&vd->vdev_dtl_lock); 1187 1188 for (c = 0; c < vd->vdev_children; c++) { 1189 vdev_t *cvd = vd->vdev_child[c]; 1190 vdev_dtl_reassess(cvd, txg, scrub_txg, scrub_done); 1191 mutex_enter(&vd->vdev_dtl_lock); 1192 space_map_union(&vd->vdev_dtl_map, &cvd->vdev_dtl_map); 1193 space_map_union(&vd->vdev_dtl_scrub, &cvd->vdev_dtl_scrub); 1194 mutex_exit(&vd->vdev_dtl_lock); 1195 } 1196 1197 if (vd == spa->spa_root_vdev) 1198 mutex_exit(&spa->spa_scrub_lock); 1199 } 1200 1201 static int 1202 vdev_dtl_load(vdev_t *vd) 1203 { 1204 spa_t *spa = vd->vdev_spa; 1205 space_map_obj_t *smo = &vd->vdev_dtl; 1206 objset_t *mos = spa->spa_meta_objset; 1207 dmu_buf_t *db; 1208 int error; 1209 1210 ASSERT(vd->vdev_children == 0); 1211 1212 if (smo->smo_object == 0) 1213 return (0); 1214 1215 if ((error = dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)) != 0) 1216 return (error); 1217 1218 ASSERT3U(db->db_size, ==, sizeof (*smo)); 1219 bcopy(db->db_data, smo, db->db_size); 1220 dmu_buf_rele(db, FTAG); 1221 1222 mutex_enter(&vd->vdev_dtl_lock); 1223 error = space_map_load(&vd->vdev_dtl_map, NULL, SM_ALLOC, smo, mos); 1224 mutex_exit(&vd->vdev_dtl_lock); 1225 1226 return (error); 1227 } 1228 1229 void 1230 vdev_dtl_sync(vdev_t *vd, uint64_t txg) 1231 { 1232 spa_t *spa = vd->vdev_spa; 1233 space_map_obj_t *smo = &vd->vdev_dtl; 1234 space_map_t *sm = &vd->vdev_dtl_map; 1235 objset_t *mos = spa->spa_meta_objset; 1236 space_map_t smsync; 1237 kmutex_t smlock; 1238 dmu_buf_t *db; 1239 dmu_tx_t *tx; 1240 1241 dprintf("%s in txg %llu pass %d\n", 1242 vdev_description(vd), (u_longlong_t)txg, spa_sync_pass(spa)); 1243 1244 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 1245 1246 if (vd->vdev_detached) { 1247 if (smo->smo_object != 0) { 1248 int err = dmu_object_free(mos, smo->smo_object, tx); 1249 ASSERT3U(err, ==, 0); 1250 smo->smo_object = 0; 1251 } 1252 dmu_tx_commit(tx); 1253 dprintf("detach %s committed in txg %llu\n", 1254 vdev_description(vd), txg); 1255 return; 1256 } 1257 1258 if (smo->smo_object == 0) { 1259 ASSERT(smo->smo_objsize == 0); 1260 ASSERT(smo->smo_alloc == 0); 1261 smo->smo_object = dmu_object_alloc(mos, 1262 DMU_OT_SPACE_MAP, 1 << SPACE_MAP_BLOCKSHIFT, 1263 DMU_OT_SPACE_MAP_HEADER, sizeof (*smo), tx); 1264 ASSERT(smo->smo_object != 0); 1265 vdev_config_dirty(vd->vdev_top); 1266 } 1267 1268 mutex_init(&smlock, NULL, MUTEX_DEFAULT, NULL); 1269 1270 space_map_create(&smsync, sm->sm_start, sm->sm_size, sm->sm_shift, 1271 &smlock); 1272 1273 mutex_enter(&smlock); 1274 1275 mutex_enter(&vd->vdev_dtl_lock); 1276 space_map_walk(sm, space_map_add, &smsync); 1277 mutex_exit(&vd->vdev_dtl_lock); 1278 1279 space_map_truncate(smo, mos, tx); 1280 space_map_sync(&smsync, SM_ALLOC, smo, mos, tx); 1281 1282 space_map_destroy(&smsync); 1283 1284 mutex_exit(&smlock); 1285 mutex_destroy(&smlock); 1286 1287 VERIFY(0 == dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)); 1288 dmu_buf_will_dirty(db, tx); 1289 ASSERT3U(db->db_size, ==, sizeof (*smo)); 1290 bcopy(smo, db->db_data, db->db_size); 1291 dmu_buf_rele(db, FTAG); 1292 1293 dmu_tx_commit(tx); 1294 } 1295 1296 void 1297 vdev_load(vdev_t *vd) 1298 { 1299 int c; 1300 1301 /* 1302 * Recursively load all children. 1303 */ 1304 for (c = 0; c < vd->vdev_children; c++) 1305 vdev_load(vd->vdev_child[c]); 1306 1307 /* 1308 * If this is a top-level vdev, initialize its metaslabs. 1309 */ 1310 if (vd == vd->vdev_top && 1311 (vd->vdev_ashift == 0 || vd->vdev_asize == 0 || 1312 vdev_metaslab_init(vd, 0) != 0)) 1313 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1314 VDEV_AUX_CORRUPT_DATA); 1315 1316 /* 1317 * If this is a leaf vdev, load its DTL. 1318 */ 1319 if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0) 1320 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1321 VDEV_AUX_CORRUPT_DATA); 1322 } 1323 1324 /* 1325 * This special case of vdev_spare() is used for hot spares. It's sole purpose 1326 * it to set the vdev state for the associated vdev. To do this, we make sure 1327 * that we can open the underlying device, then try to read the label, and make 1328 * sure that the label is sane and that it hasn't been repurposed to another 1329 * pool. 1330 */ 1331 int 1332 vdev_validate_spare(vdev_t *vd) 1333 { 1334 nvlist_t *label; 1335 uint64_t guid, version; 1336 uint64_t state; 1337 1338 if ((label = vdev_label_read_config(vd)) == NULL) { 1339 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1340 VDEV_AUX_CORRUPT_DATA); 1341 return (-1); 1342 } 1343 1344 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 || 1345 version > ZFS_VERSION || 1346 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 || 1347 guid != vd->vdev_guid || 1348 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) { 1349 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1350 VDEV_AUX_CORRUPT_DATA); 1351 nvlist_free(label); 1352 return (-1); 1353 } 1354 1355 spa_spare_add(vd); 1356 1357 /* 1358 * We don't actually check the pool state here. If it's in fact in 1359 * use by another pool, we update this fact on the fly when requested. 1360 */ 1361 nvlist_free(label); 1362 return (0); 1363 } 1364 1365 void 1366 vdev_sync_done(vdev_t *vd, uint64_t txg) 1367 { 1368 metaslab_t *msp; 1369 1370 dprintf("%s txg %llu\n", vdev_description(vd), txg); 1371 1372 while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg))) 1373 metaslab_sync_done(msp, txg); 1374 } 1375 1376 void 1377 vdev_sync(vdev_t *vd, uint64_t txg) 1378 { 1379 spa_t *spa = vd->vdev_spa; 1380 vdev_t *lvd; 1381 metaslab_t *msp; 1382 dmu_tx_t *tx; 1383 1384 dprintf("%s txg %llu pass %d\n", 1385 vdev_description(vd), (u_longlong_t)txg, spa_sync_pass(spa)); 1386 1387 if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) { 1388 ASSERT(vd == vd->vdev_top); 1389 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 1390 vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset, 1391 DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx); 1392 ASSERT(vd->vdev_ms_array != 0); 1393 vdev_config_dirty(vd); 1394 dmu_tx_commit(tx); 1395 } 1396 1397 while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) { 1398 metaslab_sync(msp, txg); 1399 (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg)); 1400 } 1401 1402 while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL) 1403 vdev_dtl_sync(lvd, txg); 1404 1405 (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)); 1406 } 1407 1408 uint64_t 1409 vdev_psize_to_asize(vdev_t *vd, uint64_t psize) 1410 { 1411 return (vd->vdev_ops->vdev_op_asize(vd, psize)); 1412 } 1413 1414 void 1415 vdev_io_start(zio_t *zio) 1416 { 1417 zio->io_vd->vdev_ops->vdev_op_io_start(zio); 1418 } 1419 1420 void 1421 vdev_io_done(zio_t *zio) 1422 { 1423 zio->io_vd->vdev_ops->vdev_op_io_done(zio); 1424 } 1425 1426 const char * 1427 vdev_description(vdev_t *vd) 1428 { 1429 if (vd == NULL || vd->vdev_ops == NULL) 1430 return ("<unknown>"); 1431 1432 if (vd->vdev_path != NULL) 1433 return (vd->vdev_path); 1434 1435 if (vd->vdev_parent == NULL) 1436 return (spa_name(vd->vdev_spa)); 1437 1438 return (vd->vdev_ops->vdev_op_type); 1439 } 1440 1441 /* 1442 * Mark the given vdev faulted. A faulted vdev behaves as if the device could 1443 * not be opened, and no I/O is attempted. 1444 */ 1445 int 1446 vdev_fault(spa_t *spa, uint64_t guid) 1447 { 1448 vdev_t *rvd, *vd; 1449 uint64_t txg; 1450 1451 txg = spa_vdev_enter(spa); 1452 1453 rvd = spa->spa_root_vdev; 1454 1455 if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL) 1456 return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 1457 if (!vd->vdev_ops->vdev_op_leaf) 1458 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 1459 1460 /* 1461 * Faulted state takes precedence over degraded. 1462 */ 1463 vd->vdev_faulted = 1ULL; 1464 vd->vdev_degraded = 0ULL; 1465 vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, 1466 VDEV_AUX_ERR_EXCEEDED); 1467 1468 /* 1469 * If marking the vdev as faulted cause the toplevel vdev to become 1470 * unavailable, then back off and simply mark the vdev as degraded 1471 * instead. 1472 */ 1473 if (vdev_is_dead(vd->vdev_top)) { 1474 vd->vdev_degraded = 1ULL; 1475 vd->vdev_faulted = 0ULL; 1476 1477 /* 1478 * If we reopen the device and it's not dead, only then do we 1479 * mark it degraded. 1480 */ 1481 vdev_reopen(vd); 1482 1483 if (!vdev_is_dead(vd)) { 1484 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, 1485 VDEV_AUX_ERR_EXCEEDED); 1486 } 1487 } 1488 1489 vdev_config_dirty(vd->vdev_top); 1490 1491 (void) spa_vdev_exit(spa, NULL, txg, 0); 1492 1493 return (0); 1494 } 1495 1496 /* 1497 * Mark the given vdev degraded. A degraded vdev is purely an indication to the 1498 * user that something is wrong. The vdev continues to operate as normal as far 1499 * as I/O is concerned. 1500 */ 1501 int 1502 vdev_degrade(spa_t *spa, uint64_t guid) 1503 { 1504 vdev_t *rvd, *vd; 1505 uint64_t txg; 1506 1507 txg = spa_vdev_enter(spa); 1508 1509 rvd = spa->spa_root_vdev; 1510 1511 if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL) 1512 return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 1513 if (!vd->vdev_ops->vdev_op_leaf) 1514 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 1515 1516 /* 1517 * If the vdev is already faulted, then don't do anything. 1518 */ 1519 if (vd->vdev_faulted || vd->vdev_degraded) { 1520 (void) spa_vdev_exit(spa, NULL, txg, 0); 1521 return (0); 1522 } 1523 1524 vd->vdev_degraded = 1ULL; 1525 if (!vdev_is_dead(vd)) 1526 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, 1527 VDEV_AUX_ERR_EXCEEDED); 1528 vdev_config_dirty(vd->vdev_top); 1529 1530 (void) spa_vdev_exit(spa, NULL, txg, 0); 1531 1532 return (0); 1533 } 1534 1535 /* 1536 * Online the given vdev. If 'unspare' is set, it implies two things. First, 1537 * any attached spare device should be detached when the device finishes 1538 * resilvering. Second, the online should be treated like a 'test' online case, 1539 * so no FMA events are generated if the device fails to open. 1540 */ 1541 int 1542 vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, 1543 vdev_state_t *newstate) 1544 { 1545 vdev_t *rvd, *vd; 1546 uint64_t txg; 1547 1548 txg = spa_vdev_enter(spa); 1549 1550 rvd = spa->spa_root_vdev; 1551 1552 if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL) 1553 return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 1554 1555 if (!vd->vdev_ops->vdev_op_leaf) 1556 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 1557 1558 vd->vdev_offline = B_FALSE; 1559 vd->vdev_tmpoffline = B_FALSE; 1560 vd->vdev_checkremove = (flags & ZFS_ONLINE_CHECKREMOVE) ? 1561 B_TRUE : B_FALSE; 1562 vd->vdev_forcefault = (flags & ZFS_ONLINE_FORCEFAULT) ? 1563 B_TRUE : B_FALSE; 1564 vdev_reopen(vd->vdev_top); 1565 vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE; 1566 1567 if (newstate) 1568 *newstate = vd->vdev_state; 1569 if ((flags & ZFS_ONLINE_UNSPARE) && 1570 !vdev_is_dead(vd) && vd->vdev_parent && 1571 vd->vdev_parent->vdev_ops == &vdev_spare_ops && 1572 vd->vdev_parent->vdev_child[0] == vd) 1573 vd->vdev_unspare = B_TRUE; 1574 1575 vdev_config_dirty(vd->vdev_top); 1576 1577 (void) spa_vdev_exit(spa, NULL, txg, 0); 1578 1579 /* 1580 * Must hold spa_namespace_lock in order to post resilver sysevent 1581 * w/pool name. 1582 */ 1583 mutex_enter(&spa_namespace_lock); 1584 VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 1585 mutex_exit(&spa_namespace_lock); 1586 1587 return (0); 1588 } 1589 1590 int 1591 vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags) 1592 { 1593 vdev_t *rvd, *vd; 1594 uint64_t txg; 1595 1596 txg = spa_vdev_enter(spa); 1597 1598 rvd = spa->spa_root_vdev; 1599 1600 if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL) 1601 return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 1602 1603 if (!vd->vdev_ops->vdev_op_leaf) 1604 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 1605 1606 /* 1607 * If the device isn't already offline, try to offline it. 1608 */ 1609 if (!vd->vdev_offline) { 1610 /* 1611 * If this device's top-level vdev has a non-empty DTL, 1612 * don't allow the device to be offlined. 1613 * 1614 * XXX -- make this more precise by allowing the offline 1615 * as long as the remaining devices don't have any DTL holes. 1616 */ 1617 if (vd->vdev_top->vdev_dtl_map.sm_space != 0) 1618 return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 1619 1620 /* 1621 * Offline this device and reopen its top-level vdev. 1622 * If this action results in the top-level vdev becoming 1623 * unusable, undo it and fail the request. 1624 */ 1625 vd->vdev_offline = B_TRUE; 1626 vdev_reopen(vd->vdev_top); 1627 if (vdev_is_dead(vd->vdev_top)) { 1628 vd->vdev_offline = B_FALSE; 1629 vdev_reopen(vd->vdev_top); 1630 return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 1631 } 1632 } 1633 1634 vd->vdev_tmpoffline = (flags & ZFS_OFFLINE_TEMPORARY) ? 1635 B_TRUE : B_FALSE; 1636 1637 vdev_config_dirty(vd->vdev_top); 1638 1639 return (spa_vdev_exit(spa, NULL, txg, 0)); 1640 } 1641 1642 /* 1643 * Clear the error counts associated with this vdev. Unlike vdev_online() and 1644 * vdev_offline(), we assume the spa config is locked. We also clear all 1645 * children. If 'vd' is NULL, then the user wants to clear all vdevs. 1646 */ 1647 void 1648 vdev_clear(spa_t *spa, vdev_t *vd) 1649 { 1650 int c; 1651 1652 if (vd == NULL) 1653 vd = spa->spa_root_vdev; 1654 1655 vd->vdev_stat.vs_read_errors = 0; 1656 vd->vdev_stat.vs_write_errors = 0; 1657 vd->vdev_stat.vs_checksum_errors = 0; 1658 1659 for (c = 0; c < vd->vdev_children; c++) 1660 vdev_clear(spa, vd->vdev_child[c]); 1661 1662 /* 1663 * If we're in the FAULTED state, then clear the persistent state and 1664 * attempt to reopen the device. We also mark the vdev config dirty, so 1665 * that the new faulted state is written out to disk. 1666 */ 1667 if (vd->vdev_faulted || vd->vdev_degraded) { 1668 vd->vdev_faulted = vd->vdev_degraded = 0; 1669 vdev_reopen(vd); 1670 vdev_config_dirty(vd->vdev_top); 1671 1672 if (vd->vdev_faulted) 1673 VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, 1674 B_TRUE) == 0); 1675 1676 spa_event_notify(spa, vd, ESC_ZFS_VDEV_CLEAR); 1677 } 1678 } 1679 1680 int 1681 vdev_is_dead(vdev_t *vd) 1682 { 1683 return (vd->vdev_state < VDEV_STATE_DEGRADED); 1684 } 1685 1686 int 1687 vdev_error_inject(vdev_t *vd, zio_t *zio) 1688 { 1689 int error = 0; 1690 1691 if (vd->vdev_fault_mode == VDEV_FAULT_NONE) 1692 return (0); 1693 1694 if (((1ULL << zio->io_type) & vd->vdev_fault_mask) == 0) 1695 return (0); 1696 1697 switch (vd->vdev_fault_mode) { 1698 case VDEV_FAULT_RANDOM: 1699 if (spa_get_random(vd->vdev_fault_arg) == 0) 1700 error = EIO; 1701 break; 1702 1703 case VDEV_FAULT_COUNT: 1704 if ((int64_t)--vd->vdev_fault_arg <= 0) 1705 vd->vdev_fault_mode = VDEV_FAULT_NONE; 1706 error = EIO; 1707 break; 1708 } 1709 1710 return (error); 1711 } 1712 1713 /* 1714 * Get statistics for the given vdev. 1715 */ 1716 void 1717 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs) 1718 { 1719 vdev_t *rvd = vd->vdev_spa->spa_root_vdev; 1720 int c, t; 1721 1722 mutex_enter(&vd->vdev_stat_lock); 1723 bcopy(&vd->vdev_stat, vs, sizeof (*vs)); 1724 vs->vs_timestamp = gethrtime() - vs->vs_timestamp; 1725 vs->vs_state = vd->vdev_state; 1726 vs->vs_rsize = vdev_get_rsize(vd); 1727 mutex_exit(&vd->vdev_stat_lock); 1728 1729 /* 1730 * If we're getting stats on the root vdev, aggregate the I/O counts 1731 * over all top-level vdevs (i.e. the direct children of the root). 1732 */ 1733 if (vd == rvd) { 1734 for (c = 0; c < rvd->vdev_children; c++) { 1735 vdev_t *cvd = rvd->vdev_child[c]; 1736 vdev_stat_t *cvs = &cvd->vdev_stat; 1737 1738 mutex_enter(&vd->vdev_stat_lock); 1739 for (t = 0; t < ZIO_TYPES; t++) { 1740 vs->vs_ops[t] += cvs->vs_ops[t]; 1741 vs->vs_bytes[t] += cvs->vs_bytes[t]; 1742 } 1743 vs->vs_read_errors += cvs->vs_read_errors; 1744 vs->vs_write_errors += cvs->vs_write_errors; 1745 vs->vs_checksum_errors += cvs->vs_checksum_errors; 1746 vs->vs_scrub_examined += cvs->vs_scrub_examined; 1747 vs->vs_scrub_errors += cvs->vs_scrub_errors; 1748 mutex_exit(&vd->vdev_stat_lock); 1749 } 1750 } 1751 } 1752 1753 void 1754 vdev_stat_update(zio_t *zio) 1755 { 1756 vdev_t *vd = zio->io_vd; 1757 vdev_t *pvd; 1758 uint64_t txg = zio->io_txg; 1759 vdev_stat_t *vs = &vd->vdev_stat; 1760 zio_type_t type = zio->io_type; 1761 int flags = zio->io_flags; 1762 1763 if (zio->io_error == 0) { 1764 if (!(flags & ZIO_FLAG_IO_BYPASS)) { 1765 mutex_enter(&vd->vdev_stat_lock); 1766 vs->vs_ops[type]++; 1767 vs->vs_bytes[type] += zio->io_size; 1768 mutex_exit(&vd->vdev_stat_lock); 1769 } 1770 if ((flags & ZIO_FLAG_IO_REPAIR) && 1771 zio->io_delegate_list == NULL) { 1772 mutex_enter(&vd->vdev_stat_lock); 1773 if (flags & ZIO_FLAG_SCRUB_THREAD) 1774 vs->vs_scrub_repaired += zio->io_size; 1775 else 1776 vs->vs_self_healed += zio->io_size; 1777 mutex_exit(&vd->vdev_stat_lock); 1778 } 1779 return; 1780 } 1781 1782 if (flags & ZIO_FLAG_SPECULATIVE) 1783 return; 1784 1785 if (!vdev_is_dead(vd)) { 1786 mutex_enter(&vd->vdev_stat_lock); 1787 if (type == ZIO_TYPE_READ) { 1788 if (zio->io_error == ECKSUM) 1789 vs->vs_checksum_errors++; 1790 else 1791 vs->vs_read_errors++; 1792 } 1793 if (type == ZIO_TYPE_WRITE) 1794 vs->vs_write_errors++; 1795 mutex_exit(&vd->vdev_stat_lock); 1796 } 1797 1798 if (type == ZIO_TYPE_WRITE) { 1799 if (txg == 0 || vd->vdev_children != 0) 1800 return; 1801 if (flags & ZIO_FLAG_SCRUB_THREAD) { 1802 ASSERT(flags & ZIO_FLAG_IO_REPAIR); 1803 for (pvd = vd; pvd != NULL; pvd = pvd->vdev_parent) 1804 vdev_dtl_dirty(&pvd->vdev_dtl_scrub, txg, 1); 1805 } 1806 if (!(flags & ZIO_FLAG_IO_REPAIR)) { 1807 if (vdev_dtl_contains(&vd->vdev_dtl_map, txg, 1)) 1808 return; 1809 vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg); 1810 for (pvd = vd; pvd != NULL; pvd = pvd->vdev_parent) 1811 vdev_dtl_dirty(&pvd->vdev_dtl_map, txg, 1); 1812 } 1813 } 1814 } 1815 1816 void 1817 vdev_scrub_stat_update(vdev_t *vd, pool_scrub_type_t type, boolean_t complete) 1818 { 1819 int c; 1820 vdev_stat_t *vs = &vd->vdev_stat; 1821 1822 for (c = 0; c < vd->vdev_children; c++) 1823 vdev_scrub_stat_update(vd->vdev_child[c], type, complete); 1824 1825 mutex_enter(&vd->vdev_stat_lock); 1826 1827 if (type == POOL_SCRUB_NONE) { 1828 /* 1829 * Update completion and end time. Leave everything else alone 1830 * so we can report what happened during the previous scrub. 1831 */ 1832 vs->vs_scrub_complete = complete; 1833 vs->vs_scrub_end = gethrestime_sec(); 1834 } else { 1835 vs->vs_scrub_type = type; 1836 vs->vs_scrub_complete = 0; 1837 vs->vs_scrub_examined = 0; 1838 vs->vs_scrub_repaired = 0; 1839 vs->vs_scrub_errors = 0; 1840 vs->vs_scrub_start = gethrestime_sec(); 1841 vs->vs_scrub_end = 0; 1842 } 1843 1844 mutex_exit(&vd->vdev_stat_lock); 1845 } 1846 1847 /* 1848 * Update the in-core space usage stats for this vdev and the root vdev. 1849 */ 1850 void 1851 vdev_space_update(vdev_t *vd, int64_t space_delta, int64_t alloc_delta) 1852 { 1853 ASSERT(vd == vd->vdev_top); 1854 int64_t dspace_delta = space_delta; 1855 1856 do { 1857 if (vd->vdev_ms_count) { 1858 /* 1859 * If this is a top-level vdev, apply the 1860 * inverse of its psize-to-asize (ie. RAID-Z) 1861 * space-expansion factor. We must calculate 1862 * this here and not at the root vdev because 1863 * the root vdev's psize-to-asize is simply the 1864 * max of its childrens', thus not accurate 1865 * enough for us. 1866 */ 1867 ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0); 1868 dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) * 1869 vd->vdev_deflate_ratio; 1870 } 1871 1872 mutex_enter(&vd->vdev_stat_lock); 1873 vd->vdev_stat.vs_space += space_delta; 1874 vd->vdev_stat.vs_alloc += alloc_delta; 1875 vd->vdev_stat.vs_dspace += dspace_delta; 1876 mutex_exit(&vd->vdev_stat_lock); 1877 } while ((vd = vd->vdev_parent) != NULL); 1878 } 1879 1880 /* 1881 * Mark a top-level vdev's config as dirty, placing it on the dirty list 1882 * so that it will be written out next time the vdev configuration is synced. 1883 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs. 1884 */ 1885 void 1886 vdev_config_dirty(vdev_t *vd) 1887 { 1888 spa_t *spa = vd->vdev_spa; 1889 vdev_t *rvd = spa->spa_root_vdev; 1890 int c; 1891 1892 /* 1893 * The dirty list is protected by the config lock. The caller must 1894 * either hold the config lock as writer, or must be the sync thread 1895 * (which holds the lock as reader). There's only one sync thread, 1896 * so this is sufficient to ensure mutual exclusion. 1897 */ 1898 ASSERT(spa_config_held(spa, RW_WRITER) || 1899 dsl_pool_sync_context(spa_get_dsl(spa))); 1900 1901 if (vd == rvd) { 1902 for (c = 0; c < rvd->vdev_children; c++) 1903 vdev_config_dirty(rvd->vdev_child[c]); 1904 } else { 1905 ASSERT(vd == vd->vdev_top); 1906 1907 if (!list_link_active(&vd->vdev_dirty_node)) 1908 list_insert_head(&spa->spa_dirty_list, vd); 1909 } 1910 } 1911 1912 void 1913 vdev_config_clean(vdev_t *vd) 1914 { 1915 spa_t *spa = vd->vdev_spa; 1916 1917 ASSERT(spa_config_held(spa, RW_WRITER) || 1918 dsl_pool_sync_context(spa_get_dsl(spa))); 1919 1920 ASSERT(list_link_active(&vd->vdev_dirty_node)); 1921 list_remove(&spa->spa_dirty_list, vd); 1922 } 1923 1924 void 1925 vdev_propagate_state(vdev_t *vd) 1926 { 1927 vdev_t *rvd = vd->vdev_spa->spa_root_vdev; 1928 int degraded = 0, faulted = 0; 1929 int corrupted = 0; 1930 int c; 1931 vdev_t *child; 1932 1933 if (vd->vdev_children > 0) { 1934 for (c = 0; c < vd->vdev_children; c++) { 1935 child = vd->vdev_child[c]; 1936 if (vdev_is_dead(child)) 1937 faulted++; 1938 else if (child->vdev_state == VDEV_STATE_DEGRADED) 1939 degraded++; 1940 1941 if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA) 1942 corrupted++; 1943 } 1944 1945 vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded); 1946 1947 /* 1948 * Root special: if there is a toplevel vdev that cannot be 1949 * opened due to corrupted metadata, then propagate the root 1950 * vdev's aux state as 'corrupt' rather than 'insufficient 1951 * replicas'. 1952 */ 1953 if (corrupted && vd == rvd && 1954 rvd->vdev_state == VDEV_STATE_CANT_OPEN) 1955 vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN, 1956 VDEV_AUX_CORRUPT_DATA); 1957 } 1958 1959 if (vd->vdev_parent) 1960 vdev_propagate_state(vd->vdev_parent); 1961 } 1962 1963 /* 1964 * Set a vdev's state. If this is during an open, we don't update the parent 1965 * state, because we're in the process of opening children depth-first. 1966 * Otherwise, we propagate the change to the parent. 1967 * 1968 * If this routine places a device in a faulted state, an appropriate ereport is 1969 * generated. 1970 */ 1971 void 1972 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux) 1973 { 1974 uint64_t save_state; 1975 1976 if (state == vd->vdev_state) { 1977 vd->vdev_stat.vs_aux = aux; 1978 return; 1979 } 1980 1981 save_state = vd->vdev_state; 1982 1983 vd->vdev_state = state; 1984 vd->vdev_stat.vs_aux = aux; 1985 1986 /* 1987 * If we are setting the vdev state to anything but an open state, then 1988 * always close the underlying device. Otherwise, we keep accessible 1989 * but invalid devices open forever. We don't call vdev_close() itself, 1990 * because that implies some extra checks (offline, etc) that we don't 1991 * want here. This is limited to leaf devices, because otherwise 1992 * closing the device will affect other children. 1993 */ 1994 if (vdev_is_dead(vd) && vd->vdev_ops->vdev_op_leaf) 1995 vd->vdev_ops->vdev_op_close(vd); 1996 1997 if (vd->vdev_removed && 1998 state == VDEV_STATE_CANT_OPEN && 1999 (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) { 2000 /* 2001 * If the previous state is set to VDEV_STATE_REMOVED, then this 2002 * device was previously marked removed and someone attempted to 2003 * reopen it. If this failed due to a nonexistent device, then 2004 * keep the device in the REMOVED state. We also let this be if 2005 * it is one of our special test online cases, which is only 2006 * attempting to online the device and shouldn't generate an FMA 2007 * fault. 2008 */ 2009 vd->vdev_state = VDEV_STATE_REMOVED; 2010 vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 2011 } else if (state == VDEV_STATE_REMOVED) { 2012 /* 2013 * Indicate to the ZFS DE that this device has been removed, and 2014 * any recent errors should be ignored. 2015 */ 2016 zfs_post_remove(vd->vdev_spa, vd); 2017 vd->vdev_removed = B_TRUE; 2018 } else if (state == VDEV_STATE_CANT_OPEN) { 2019 /* 2020 * If we fail to open a vdev during an import, we mark it as 2021 * "not available", which signifies that it was never there to 2022 * begin with. Failure to open such a device is not considered 2023 * an error. 2024 */ 2025 if (vd->vdev_spa->spa_load_state == SPA_LOAD_IMPORT && 2026 vd->vdev_ops->vdev_op_leaf) 2027 vd->vdev_not_present = 1; 2028 2029 /* 2030 * Post the appropriate ereport. If the 'prevstate' field is 2031 * set to something other than VDEV_STATE_UNKNOWN, it indicates 2032 * that this is part of a vdev_reopen(). In this case, we don't 2033 * want to post the ereport if the device was already in the 2034 * CANT_OPEN state beforehand. 2035 * 2036 * If the 'checkremove' flag is set, then this is an attempt to 2037 * online the device in response to an insertion event. If we 2038 * hit this case, then we have detected an insertion event for a 2039 * faulted or offline device that wasn't in the removed state. 2040 * In this scenario, we don't post an ereport because we are 2041 * about to replace the device, or attempt an online with 2042 * vdev_forcefault, which will generate the fault for us. 2043 */ 2044 if ((vd->vdev_prevstate != state || vd->vdev_forcefault) && 2045 !vd->vdev_not_present && !vd->vdev_checkremove && 2046 vd != vd->vdev_spa->spa_root_vdev) { 2047 const char *class; 2048 2049 switch (aux) { 2050 case VDEV_AUX_OPEN_FAILED: 2051 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED; 2052 break; 2053 case VDEV_AUX_CORRUPT_DATA: 2054 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA; 2055 break; 2056 case VDEV_AUX_NO_REPLICAS: 2057 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS; 2058 break; 2059 case VDEV_AUX_BAD_GUID_SUM: 2060 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM; 2061 break; 2062 case VDEV_AUX_TOO_SMALL: 2063 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL; 2064 break; 2065 case VDEV_AUX_BAD_LABEL: 2066 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL; 2067 break; 2068 default: 2069 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN; 2070 } 2071 2072 zfs_ereport_post(class, vd->vdev_spa, 2073 vd, NULL, save_state, 0); 2074 } 2075 2076 /* Erase any notion of persistent removed state */ 2077 vd->vdev_removed = B_FALSE; 2078 } else { 2079 vd->vdev_removed = B_FALSE; 2080 } 2081 2082 if (!isopen) 2083 vdev_propagate_state(vd); 2084 } 2085