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