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