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) < SPA_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) < SPA_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) >= SPA_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, sizeof (smo)); 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 int 797 vdev_probe(vdev_t *vd) 798 { 799 if (vd == NULL) 800 return (EINVAL); 801 802 /* 803 * Right now we only support status checks on the leaf vdevs. 804 */ 805 if (vd->vdev_ops->vdev_op_leaf) 806 return (vd->vdev_ops->vdev_op_probe(vd)); 807 808 return (0); 809 } 810 811 /* 812 * Prepare a virtual device for access. 813 */ 814 int 815 vdev_open(vdev_t *vd) 816 { 817 int error; 818 int c; 819 uint64_t osize = 0; 820 uint64_t asize, psize; 821 uint64_t ashift = 0; 822 823 ASSERT(vd->vdev_state == VDEV_STATE_CLOSED || 824 vd->vdev_state == VDEV_STATE_CANT_OPEN || 825 vd->vdev_state == VDEV_STATE_OFFLINE); 826 827 if (vd->vdev_fault_mode == VDEV_FAULT_COUNT) 828 vd->vdev_fault_arg >>= 1; 829 else 830 vd->vdev_fault_mode = VDEV_FAULT_NONE; 831 832 vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 833 834 if (!vd->vdev_removed && vd->vdev_faulted) { 835 ASSERT(vd->vdev_children == 0); 836 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 837 VDEV_AUX_ERR_EXCEEDED); 838 return (ENXIO); 839 } else if (vd->vdev_offline) { 840 ASSERT(vd->vdev_children == 0); 841 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE); 842 return (ENXIO); 843 } 844 845 error = vd->vdev_ops->vdev_op_open(vd, &osize, &ashift); 846 847 if (zio_injection_enabled && error == 0) 848 error = zio_handle_device_injection(vd, ENXIO); 849 850 if (error) { 851 if (vd->vdev_removed && 852 vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED) 853 vd->vdev_removed = B_FALSE; 854 855 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 856 vd->vdev_stat.vs_aux); 857 return (error); 858 } 859 860 vd->vdev_removed = B_FALSE; 861 862 if (vd->vdev_degraded) { 863 ASSERT(vd->vdev_children == 0); 864 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 865 VDEV_AUX_ERR_EXCEEDED); 866 } else { 867 vd->vdev_state = VDEV_STATE_HEALTHY; 868 } 869 870 for (c = 0; c < vd->vdev_children; c++) 871 if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) { 872 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 873 VDEV_AUX_NONE); 874 break; 875 } 876 877 osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t)); 878 879 if (vd->vdev_children == 0) { 880 if (osize < SPA_MINDEVSIZE) { 881 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 882 VDEV_AUX_TOO_SMALL); 883 return (EOVERFLOW); 884 } 885 psize = osize; 886 asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE); 887 } else { 888 if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE - 889 (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) { 890 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 891 VDEV_AUX_TOO_SMALL); 892 return (EOVERFLOW); 893 } 894 psize = 0; 895 asize = osize; 896 } 897 898 vd->vdev_psize = psize; 899 900 if (vd->vdev_asize == 0) { 901 /* 902 * This is the first-ever open, so use the computed values. 903 * For testing purposes, a higher ashift can be requested. 904 */ 905 vd->vdev_asize = asize; 906 vd->vdev_ashift = MAX(ashift, vd->vdev_ashift); 907 } else { 908 /* 909 * Make sure the alignment requirement hasn't increased. 910 */ 911 if (ashift > vd->vdev_top->vdev_ashift) { 912 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 913 VDEV_AUX_BAD_LABEL); 914 return (EINVAL); 915 } 916 917 /* 918 * Make sure the device hasn't shrunk. 919 */ 920 if (asize < vd->vdev_asize) { 921 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 922 VDEV_AUX_BAD_LABEL); 923 return (EINVAL); 924 } 925 926 /* 927 * If all children are healthy and the asize has increased, 928 * then we've experienced dynamic LUN growth. 929 */ 930 if (vd->vdev_state == VDEV_STATE_HEALTHY && 931 asize > vd->vdev_asize) { 932 vd->vdev_asize = asize; 933 } 934 } 935 936 /* 937 * Ensure we can issue some IO before declaring the 938 * vdev open for business. 939 */ 940 error = vdev_probe(vd); 941 if (error) { 942 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 943 VDEV_AUX_OPEN_FAILED); 944 return (error); 945 } 946 947 /* 948 * If this is a top-level vdev, compute the raidz-deflation 949 * ratio. Note, we hard-code in 128k (1<<17) because it is the 950 * current "typical" blocksize. Even if SPA_MAXBLOCKSIZE 951 * changes, this algorithm must never change, or we will 952 * inconsistently account for existing bp's. 953 */ 954 if (vd->vdev_top == vd) { 955 vd->vdev_deflate_ratio = (1<<17) / 956 (vdev_psize_to_asize(vd, 1<<17) >> SPA_MINBLOCKSHIFT); 957 } 958 959 /* 960 * This allows the ZFS DE to close cases appropriately. If a device 961 * goes away and later returns, we want to close the associated case. 962 * But it's not enough to simply post this only when a device goes from 963 * CANT_OPEN -> HEALTHY. If we reboot the system and the device is 964 * back, we also need to close the case (otherwise we will try to replay 965 * it). So we have to post this notifier every time. Since this only 966 * occurs during pool open or error recovery, this should not be an 967 * issue. 968 */ 969 zfs_post_ok(vd->vdev_spa, vd); 970 971 return (0); 972 } 973 974 /* 975 * Called once the vdevs are all opened, this routine validates the label 976 * contents. This needs to be done before vdev_load() so that we don't 977 * inadvertently do repair I/Os to the wrong device. 978 * 979 * This function will only return failure if one of the vdevs indicates that it 980 * has since been destroyed or exported. This is only possible if 981 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state 982 * will be updated but the function will return 0. 983 */ 984 int 985 vdev_validate(vdev_t *vd) 986 { 987 spa_t *spa = vd->vdev_spa; 988 int c; 989 nvlist_t *label; 990 uint64_t guid; 991 uint64_t state; 992 993 for (c = 0; c < vd->vdev_children; c++) 994 if (vdev_validate(vd->vdev_child[c]) != 0) 995 return (EBADF); 996 997 /* 998 * If the device has already failed, or was marked offline, don't do 999 * any further validation. Otherwise, label I/O will fail and we will 1000 * overwrite the previous state. 1001 */ 1002 if (vd->vdev_ops->vdev_op_leaf && !vdev_is_dead(vd)) { 1003 1004 if ((label = vdev_label_read_config(vd)) == NULL) { 1005 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1006 VDEV_AUX_BAD_LABEL); 1007 return (0); 1008 } 1009 1010 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, 1011 &guid) != 0 || guid != spa_guid(spa)) { 1012 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1013 VDEV_AUX_CORRUPT_DATA); 1014 nvlist_free(label); 1015 return (0); 1016 } 1017 1018 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, 1019 &guid) != 0 || guid != vd->vdev_guid) { 1020 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1021 VDEV_AUX_CORRUPT_DATA); 1022 nvlist_free(label); 1023 return (0); 1024 } 1025 1026 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, 1027 &state) != 0) { 1028 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1029 VDEV_AUX_CORRUPT_DATA); 1030 nvlist_free(label); 1031 return (0); 1032 } 1033 1034 nvlist_free(label); 1035 1036 if (spa->spa_load_state == SPA_LOAD_OPEN && 1037 state != POOL_STATE_ACTIVE) 1038 return (EBADF); 1039 } 1040 1041 /* 1042 * If we were able to open and validate a vdev that was previously 1043 * marked permanently unavailable, clear that state now. 1044 */ 1045 if (vd->vdev_not_present) 1046 vd->vdev_not_present = 0; 1047 1048 return (0); 1049 } 1050 1051 /* 1052 * Close a virtual device. 1053 */ 1054 void 1055 vdev_close(vdev_t *vd) 1056 { 1057 vd->vdev_ops->vdev_op_close(vd); 1058 1059 vdev_cache_purge(vd); 1060 1061 /* 1062 * We record the previous state before we close it, so that if we are 1063 * doing a reopen(), we don't generate FMA ereports if we notice that 1064 * it's still faulted. 1065 */ 1066 vd->vdev_prevstate = vd->vdev_state; 1067 1068 if (vd->vdev_offline) 1069 vd->vdev_state = VDEV_STATE_OFFLINE; 1070 else 1071 vd->vdev_state = VDEV_STATE_CLOSED; 1072 vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 1073 } 1074 1075 void 1076 vdev_reopen(vdev_t *vd) 1077 { 1078 spa_t *spa = vd->vdev_spa; 1079 1080 ASSERT(spa_config_held(spa, RW_WRITER)); 1081 1082 vdev_close(vd); 1083 (void) vdev_open(vd); 1084 1085 /* 1086 * Call vdev_validate() here to make sure we have the same device. 1087 * Otherwise, a device with an invalid label could be successfully 1088 * opened in response to vdev_reopen(). 1089 */ 1090 (void) vdev_validate(vd); 1091 1092 /* 1093 * Reassess parent vdev's health. 1094 */ 1095 vdev_propagate_state(vd); 1096 } 1097 1098 int 1099 vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing) 1100 { 1101 int error; 1102 1103 /* 1104 * Normally, partial opens (e.g. of a mirror) are allowed. 1105 * For a create, however, we want to fail the request if 1106 * there are any components we can't open. 1107 */ 1108 error = vdev_open(vd); 1109 1110 if (error || vd->vdev_state != VDEV_STATE_HEALTHY) { 1111 vdev_close(vd); 1112 return (error ? error : ENXIO); 1113 } 1114 1115 /* 1116 * Recursively initialize all labels. 1117 */ 1118 if ((error = vdev_label_init(vd, txg, isreplacing ? 1119 VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) { 1120 vdev_close(vd); 1121 return (error); 1122 } 1123 1124 return (0); 1125 } 1126 1127 /* 1128 * The is the latter half of vdev_create(). It is distinct because it 1129 * involves initiating transactions in order to do metaslab creation. 1130 * For creation, we want to try to create all vdevs at once and then undo it 1131 * if anything fails; this is much harder if we have pending transactions. 1132 */ 1133 void 1134 vdev_init(vdev_t *vd, uint64_t txg) 1135 { 1136 /* 1137 * Aim for roughly 200 metaslabs per vdev. 1138 */ 1139 vd->vdev_ms_shift = highbit(vd->vdev_asize / 200); 1140 vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT); 1141 1142 /* 1143 * Initialize the vdev's metaslabs. This can't fail because 1144 * there's nothing to read when creating all new metaslabs. 1145 */ 1146 VERIFY(vdev_metaslab_init(vd, txg) == 0); 1147 } 1148 1149 void 1150 vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg) 1151 { 1152 ASSERT(vd == vd->vdev_top); 1153 ASSERT(ISP2(flags)); 1154 1155 if (flags & VDD_METASLAB) 1156 (void) txg_list_add(&vd->vdev_ms_list, arg, txg); 1157 1158 if (flags & VDD_DTL) 1159 (void) txg_list_add(&vd->vdev_dtl_list, arg, txg); 1160 1161 (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg); 1162 } 1163 1164 void 1165 vdev_dtl_dirty(space_map_t *sm, uint64_t txg, uint64_t size) 1166 { 1167 mutex_enter(sm->sm_lock); 1168 if (!space_map_contains(sm, txg, size)) 1169 space_map_add(sm, txg, size); 1170 mutex_exit(sm->sm_lock); 1171 } 1172 1173 int 1174 vdev_dtl_contains(space_map_t *sm, uint64_t txg, uint64_t size) 1175 { 1176 int dirty; 1177 1178 /* 1179 * Quick test without the lock -- covers the common case that 1180 * there are no dirty time segments. 1181 */ 1182 if (sm->sm_space == 0) 1183 return (0); 1184 1185 mutex_enter(sm->sm_lock); 1186 dirty = space_map_contains(sm, txg, size); 1187 mutex_exit(sm->sm_lock); 1188 1189 return (dirty); 1190 } 1191 1192 /* 1193 * Reassess DTLs after a config change or scrub completion. 1194 */ 1195 void 1196 vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done) 1197 { 1198 spa_t *spa = vd->vdev_spa; 1199 int c; 1200 1201 ASSERT(spa_config_held(spa, RW_WRITER)); 1202 1203 if (vd->vdev_children == 0) { 1204 mutex_enter(&vd->vdev_dtl_lock); 1205 /* 1206 * We're successfully scrubbed everything up to scrub_txg. 1207 * Therefore, excise all old DTLs up to that point, then 1208 * fold in the DTLs for everything we couldn't scrub. 1209 */ 1210 if (scrub_txg != 0) { 1211 space_map_excise(&vd->vdev_dtl_map, 0, scrub_txg); 1212 space_map_union(&vd->vdev_dtl_map, &vd->vdev_dtl_scrub); 1213 } 1214 if (scrub_done) 1215 space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL); 1216 mutex_exit(&vd->vdev_dtl_lock); 1217 if (txg != 0) 1218 vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg); 1219 return; 1220 } 1221 1222 /* 1223 * Make sure the DTLs are always correct under the scrub lock. 1224 */ 1225 if (vd == spa->spa_root_vdev) 1226 mutex_enter(&spa->spa_scrub_lock); 1227 1228 mutex_enter(&vd->vdev_dtl_lock); 1229 space_map_vacate(&vd->vdev_dtl_map, NULL, NULL); 1230 space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL); 1231 mutex_exit(&vd->vdev_dtl_lock); 1232 1233 for (c = 0; c < vd->vdev_children; c++) { 1234 vdev_t *cvd = vd->vdev_child[c]; 1235 vdev_dtl_reassess(cvd, txg, scrub_txg, scrub_done); 1236 mutex_enter(&vd->vdev_dtl_lock); 1237 space_map_union(&vd->vdev_dtl_map, &cvd->vdev_dtl_map); 1238 space_map_union(&vd->vdev_dtl_scrub, &cvd->vdev_dtl_scrub); 1239 mutex_exit(&vd->vdev_dtl_lock); 1240 } 1241 1242 if (vd == spa->spa_root_vdev) 1243 mutex_exit(&spa->spa_scrub_lock); 1244 } 1245 1246 static int 1247 vdev_dtl_load(vdev_t *vd) 1248 { 1249 spa_t *spa = vd->vdev_spa; 1250 space_map_obj_t *smo = &vd->vdev_dtl; 1251 objset_t *mos = spa->spa_meta_objset; 1252 dmu_buf_t *db; 1253 int error; 1254 1255 ASSERT(vd->vdev_children == 0); 1256 1257 if (smo->smo_object == 0) 1258 return (0); 1259 1260 if ((error = dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)) != 0) 1261 return (error); 1262 1263 ASSERT3U(db->db_size, >=, sizeof (*smo)); 1264 bcopy(db->db_data, smo, sizeof (*smo)); 1265 dmu_buf_rele(db, FTAG); 1266 1267 mutex_enter(&vd->vdev_dtl_lock); 1268 error = space_map_load(&vd->vdev_dtl_map, NULL, SM_ALLOC, smo, mos); 1269 mutex_exit(&vd->vdev_dtl_lock); 1270 1271 return (error); 1272 } 1273 1274 void 1275 vdev_dtl_sync(vdev_t *vd, uint64_t txg) 1276 { 1277 spa_t *spa = vd->vdev_spa; 1278 space_map_obj_t *smo = &vd->vdev_dtl; 1279 space_map_t *sm = &vd->vdev_dtl_map; 1280 objset_t *mos = spa->spa_meta_objset; 1281 space_map_t smsync; 1282 kmutex_t smlock; 1283 dmu_buf_t *db; 1284 dmu_tx_t *tx; 1285 1286 dprintf("%s in txg %llu pass %d\n", 1287 vdev_description(vd), (u_longlong_t)txg, spa_sync_pass(spa)); 1288 1289 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 1290 1291 if (vd->vdev_detached) { 1292 if (smo->smo_object != 0) { 1293 int err = dmu_object_free(mos, smo->smo_object, tx); 1294 ASSERT3U(err, ==, 0); 1295 smo->smo_object = 0; 1296 } 1297 dmu_tx_commit(tx); 1298 dprintf("detach %s committed in txg %llu\n", 1299 vdev_description(vd), txg); 1300 return; 1301 } 1302 1303 if (smo->smo_object == 0) { 1304 ASSERT(smo->smo_objsize == 0); 1305 ASSERT(smo->smo_alloc == 0); 1306 smo->smo_object = dmu_object_alloc(mos, 1307 DMU_OT_SPACE_MAP, 1 << SPACE_MAP_BLOCKSHIFT, 1308 DMU_OT_SPACE_MAP_HEADER, sizeof (*smo), tx); 1309 ASSERT(smo->smo_object != 0); 1310 vdev_config_dirty(vd->vdev_top); 1311 } 1312 1313 mutex_init(&smlock, NULL, MUTEX_DEFAULT, NULL); 1314 1315 space_map_create(&smsync, sm->sm_start, sm->sm_size, sm->sm_shift, 1316 &smlock); 1317 1318 mutex_enter(&smlock); 1319 1320 mutex_enter(&vd->vdev_dtl_lock); 1321 space_map_walk(sm, space_map_add, &smsync); 1322 mutex_exit(&vd->vdev_dtl_lock); 1323 1324 space_map_truncate(smo, mos, tx); 1325 space_map_sync(&smsync, SM_ALLOC, smo, mos, tx); 1326 1327 space_map_destroy(&smsync); 1328 1329 mutex_exit(&smlock); 1330 mutex_destroy(&smlock); 1331 1332 VERIFY(0 == dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)); 1333 dmu_buf_will_dirty(db, tx); 1334 ASSERT3U(db->db_size, >=, sizeof (*smo)); 1335 bcopy(smo, db->db_data, sizeof (*smo)); 1336 dmu_buf_rele(db, FTAG); 1337 1338 dmu_tx_commit(tx); 1339 } 1340 1341 void 1342 vdev_load(vdev_t *vd) 1343 { 1344 int c; 1345 1346 /* 1347 * Recursively load all children. 1348 */ 1349 for (c = 0; c < vd->vdev_children; c++) 1350 vdev_load(vd->vdev_child[c]); 1351 1352 /* 1353 * If this is a top-level vdev, initialize its metaslabs. 1354 */ 1355 if (vd == vd->vdev_top && 1356 (vd->vdev_ashift == 0 || vd->vdev_asize == 0 || 1357 vdev_metaslab_init(vd, 0) != 0)) 1358 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1359 VDEV_AUX_CORRUPT_DATA); 1360 1361 /* 1362 * If this is a leaf vdev, load its DTL. 1363 */ 1364 if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0) 1365 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1366 VDEV_AUX_CORRUPT_DATA); 1367 } 1368 1369 /* 1370 * This special case of vdev_spare() is used for hot spares. It's sole purpose 1371 * it to set the vdev state for the associated vdev. To do this, we make sure 1372 * that we can open the underlying device, then try to read the label, and make 1373 * sure that the label is sane and that it hasn't been repurposed to another 1374 * pool. 1375 */ 1376 int 1377 vdev_validate_spare(vdev_t *vd) 1378 { 1379 nvlist_t *label; 1380 uint64_t guid, version; 1381 uint64_t state; 1382 1383 if ((label = vdev_label_read_config(vd)) == NULL) { 1384 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1385 VDEV_AUX_CORRUPT_DATA); 1386 return (-1); 1387 } 1388 1389 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 || 1390 version > SPA_VERSION || 1391 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 || 1392 guid != vd->vdev_guid || 1393 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) { 1394 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1395 VDEV_AUX_CORRUPT_DATA); 1396 nvlist_free(label); 1397 return (-1); 1398 } 1399 1400 spa_spare_add(vd); 1401 1402 /* 1403 * We don't actually check the pool state here. If it's in fact in 1404 * use by another pool, we update this fact on the fly when requested. 1405 */ 1406 nvlist_free(label); 1407 return (0); 1408 } 1409 1410 void 1411 vdev_sync_done(vdev_t *vd, uint64_t txg) 1412 { 1413 metaslab_t *msp; 1414 1415 dprintf("%s txg %llu\n", vdev_description(vd), txg); 1416 1417 while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg))) 1418 metaslab_sync_done(msp, txg); 1419 } 1420 1421 void 1422 vdev_sync(vdev_t *vd, uint64_t txg) 1423 { 1424 spa_t *spa = vd->vdev_spa; 1425 vdev_t *lvd; 1426 metaslab_t *msp; 1427 dmu_tx_t *tx; 1428 1429 dprintf("%s txg %llu pass %d\n", 1430 vdev_description(vd), (u_longlong_t)txg, spa_sync_pass(spa)); 1431 1432 if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) { 1433 ASSERT(vd == vd->vdev_top); 1434 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 1435 vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset, 1436 DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx); 1437 ASSERT(vd->vdev_ms_array != 0); 1438 vdev_config_dirty(vd); 1439 dmu_tx_commit(tx); 1440 } 1441 1442 while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) { 1443 metaslab_sync(msp, txg); 1444 (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg)); 1445 } 1446 1447 while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL) 1448 vdev_dtl_sync(lvd, txg); 1449 1450 (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)); 1451 } 1452 1453 uint64_t 1454 vdev_psize_to_asize(vdev_t *vd, uint64_t psize) 1455 { 1456 return (vd->vdev_ops->vdev_op_asize(vd, psize)); 1457 } 1458 1459 void 1460 vdev_io_start(zio_t *zio) 1461 { 1462 zio->io_vd->vdev_ops->vdev_op_io_start(zio); 1463 } 1464 1465 void 1466 vdev_io_done(zio_t *zio) 1467 { 1468 zio->io_vd->vdev_ops->vdev_op_io_done(zio); 1469 } 1470 1471 const char * 1472 vdev_description(vdev_t *vd) 1473 { 1474 if (vd == NULL || vd->vdev_ops == NULL) 1475 return ("<unknown>"); 1476 1477 if (vd->vdev_path != NULL) 1478 return (vd->vdev_path); 1479 1480 if (vd->vdev_parent == NULL) 1481 return (spa_name(vd->vdev_spa)); 1482 1483 return (vd->vdev_ops->vdev_op_type); 1484 } 1485 1486 /* 1487 * Mark the given vdev faulted. A faulted vdev behaves as if the device could 1488 * not be opened, and no I/O is attempted. 1489 */ 1490 int 1491 vdev_fault(spa_t *spa, uint64_t guid) 1492 { 1493 vdev_t *rvd, *vd; 1494 uint64_t txg; 1495 1496 /* 1497 * Disregard a vdev fault request if the pool has 1498 * experienced a complete failure. 1499 * 1500 * XXX - We do this here so that we don't hold the 1501 * spa_namespace_lock in the event that we can't get 1502 * the RW_WRITER spa_config_lock. 1503 */ 1504 if (spa_state(spa) == POOL_STATE_IO_FAILURE) 1505 return (EIO); 1506 1507 txg = spa_vdev_enter(spa); 1508 1509 rvd = spa->spa_root_vdev; 1510 1511 if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL) 1512 return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 1513 if (!vd->vdev_ops->vdev_op_leaf) 1514 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 1515 1516 /* 1517 * Faulted state takes precedence over degraded. 1518 */ 1519 vd->vdev_faulted = 1ULL; 1520 vd->vdev_degraded = 0ULL; 1521 vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, 1522 VDEV_AUX_ERR_EXCEEDED); 1523 1524 /* 1525 * If marking the vdev as faulted cause the toplevel vdev to become 1526 * unavailable, then back off and simply mark the vdev as degraded 1527 * instead. 1528 */ 1529 if (vdev_is_dead(vd->vdev_top)) { 1530 vd->vdev_degraded = 1ULL; 1531 vd->vdev_faulted = 0ULL; 1532 1533 /* 1534 * If we reopen the device and it's not dead, only then do we 1535 * mark it degraded. 1536 */ 1537 vdev_reopen(vd); 1538 1539 if (vdev_readable(vd)) { 1540 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, 1541 VDEV_AUX_ERR_EXCEEDED); 1542 } 1543 } 1544 1545 vdev_config_dirty(vd->vdev_top); 1546 1547 (void) spa_vdev_exit(spa, NULL, txg, 0); 1548 1549 return (0); 1550 } 1551 1552 /* 1553 * Mark the given vdev degraded. A degraded vdev is purely an indication to the 1554 * user that something is wrong. The vdev continues to operate as normal as far 1555 * as I/O is concerned. 1556 */ 1557 int 1558 vdev_degrade(spa_t *spa, uint64_t guid) 1559 { 1560 vdev_t *rvd, *vd; 1561 uint64_t txg; 1562 1563 /* 1564 * Disregard a vdev fault request if the pool has 1565 * experienced a complete failure. 1566 * 1567 * XXX - We do this here so that we don't hold the 1568 * spa_namespace_lock in the event that we can't get 1569 * the RW_WRITER spa_config_lock. 1570 */ 1571 if (spa_state(spa) == POOL_STATE_IO_FAILURE) 1572 return (EIO); 1573 1574 txg = spa_vdev_enter(spa); 1575 1576 rvd = spa->spa_root_vdev; 1577 1578 if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL) 1579 return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 1580 if (!vd->vdev_ops->vdev_op_leaf) 1581 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 1582 1583 /* 1584 * If the vdev is already faulted, then don't do anything. 1585 */ 1586 if (vd->vdev_faulted || vd->vdev_degraded) { 1587 (void) spa_vdev_exit(spa, NULL, txg, 0); 1588 return (0); 1589 } 1590 1591 vd->vdev_degraded = 1ULL; 1592 if (!vdev_is_dead(vd)) 1593 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, 1594 VDEV_AUX_ERR_EXCEEDED); 1595 vdev_config_dirty(vd->vdev_top); 1596 1597 (void) spa_vdev_exit(spa, NULL, txg, 0); 1598 1599 return (0); 1600 } 1601 1602 /* 1603 * Online the given vdev. If 'unspare' is set, it implies two things. First, 1604 * any attached spare device should be detached when the device finishes 1605 * resilvering. Second, the online should be treated like a 'test' online case, 1606 * so no FMA events are generated if the device fails to open. 1607 */ 1608 int 1609 vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, 1610 vdev_state_t *newstate) 1611 { 1612 vdev_t *rvd, *vd; 1613 uint64_t txg; 1614 1615 /* 1616 * Disregard a vdev fault request if the pool has 1617 * experienced a complete failure. 1618 * 1619 * XXX - We do this here so that we don't hold the 1620 * spa_namespace_lock in the event that we can't get 1621 * the RW_WRITER spa_config_lock. 1622 */ 1623 if (spa_state(spa) == POOL_STATE_IO_FAILURE) 1624 return (EIO); 1625 1626 txg = spa_vdev_enter(spa); 1627 1628 rvd = spa->spa_root_vdev; 1629 1630 if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL) 1631 return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 1632 1633 if (!vd->vdev_ops->vdev_op_leaf) 1634 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 1635 1636 vd->vdev_offline = B_FALSE; 1637 vd->vdev_tmpoffline = B_FALSE; 1638 vd->vdev_checkremove = (flags & ZFS_ONLINE_CHECKREMOVE) ? 1639 B_TRUE : B_FALSE; 1640 vd->vdev_forcefault = (flags & ZFS_ONLINE_FORCEFAULT) ? 1641 B_TRUE : B_FALSE; 1642 vdev_reopen(vd->vdev_top); 1643 vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE; 1644 1645 if (newstate) 1646 *newstate = vd->vdev_state; 1647 if ((flags & ZFS_ONLINE_UNSPARE) && 1648 !vdev_is_dead(vd) && vd->vdev_parent && 1649 vd->vdev_parent->vdev_ops == &vdev_spare_ops && 1650 vd->vdev_parent->vdev_child[0] == vd) 1651 vd->vdev_unspare = B_TRUE; 1652 1653 vdev_config_dirty(vd->vdev_top); 1654 1655 (void) spa_vdev_exit(spa, NULL, txg, 0); 1656 1657 /* 1658 * Must hold spa_namespace_lock in order to post resilver sysevent 1659 * w/pool name. 1660 */ 1661 mutex_enter(&spa_namespace_lock); 1662 VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 1663 mutex_exit(&spa_namespace_lock); 1664 1665 return (0); 1666 } 1667 1668 int 1669 vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags) 1670 { 1671 vdev_t *rvd, *vd; 1672 uint64_t txg; 1673 1674 /* 1675 * Disregard a vdev fault request if the pool has 1676 * experienced a complete failure. 1677 * 1678 * XXX - We do this here so that we don't hold the 1679 * spa_namespace_lock in the event that we can't get 1680 * the RW_WRITER spa_config_lock. 1681 */ 1682 if (spa_state(spa) == POOL_STATE_IO_FAILURE) 1683 return (EIO); 1684 1685 txg = spa_vdev_enter(spa); 1686 1687 rvd = spa->spa_root_vdev; 1688 1689 if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL) 1690 return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 1691 1692 if (!vd->vdev_ops->vdev_op_leaf) 1693 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 1694 1695 /* 1696 * If the device isn't already offline, try to offline it. 1697 */ 1698 if (!vd->vdev_offline) { 1699 /* 1700 * If this device's top-level vdev has a non-empty DTL, 1701 * don't allow the device to be offlined. 1702 * 1703 * XXX -- make this more precise by allowing the offline 1704 * as long as the remaining devices don't have any DTL holes. 1705 */ 1706 if (vd->vdev_top->vdev_dtl_map.sm_space != 0) 1707 return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 1708 1709 /* 1710 * Offline this device and reopen its top-level vdev. 1711 * If this action results in the top-level vdev becoming 1712 * unusable, undo it and fail the request. 1713 */ 1714 vd->vdev_offline = B_TRUE; 1715 vdev_reopen(vd->vdev_top); 1716 if (vdev_is_dead(vd->vdev_top)) { 1717 vd->vdev_offline = B_FALSE; 1718 vdev_reopen(vd->vdev_top); 1719 return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 1720 } 1721 } 1722 1723 vd->vdev_tmpoffline = (flags & ZFS_OFFLINE_TEMPORARY) ? 1724 B_TRUE : B_FALSE; 1725 1726 vdev_config_dirty(vd->vdev_top); 1727 1728 return (spa_vdev_exit(spa, NULL, txg, 0)); 1729 } 1730 1731 /* 1732 * Clear the error counts associated with this vdev. Unlike vdev_online() and 1733 * vdev_offline(), we assume the spa config is locked. We also clear all 1734 * children. If 'vd' is NULL, then the user wants to clear all vdevs. 1735 * If reopen is specified then attempt to reopen the vdev if the vdev is 1736 * faulted or degraded. 1737 */ 1738 void 1739 vdev_clear(spa_t *spa, vdev_t *vd, boolean_t reopen_wanted) 1740 { 1741 int c; 1742 1743 if (vd == NULL) 1744 vd = spa->spa_root_vdev; 1745 1746 vd->vdev_stat.vs_read_errors = 0; 1747 vd->vdev_stat.vs_write_errors = 0; 1748 vd->vdev_stat.vs_checksum_errors = 0; 1749 vd->vdev_is_failing = B_FALSE; 1750 1751 for (c = 0; c < vd->vdev_children; c++) 1752 vdev_clear(spa, vd->vdev_child[c], reopen_wanted); 1753 1754 /* 1755 * If we're in the FAULTED state, then clear the persistent state and 1756 * attempt to reopen the device. We also mark the vdev config dirty, so 1757 * that the new faulted state is written out to disk. 1758 */ 1759 if (reopen_wanted && (vd->vdev_faulted || vd->vdev_degraded)) { 1760 vd->vdev_faulted = vd->vdev_degraded = 0; 1761 vdev_reopen(vd); 1762 vdev_config_dirty(vd->vdev_top); 1763 1764 if (vd->vdev_faulted) 1765 spa_async_request(spa, SPA_ASYNC_RESILVER); 1766 1767 spa_event_notify(spa, vd, ESC_ZFS_VDEV_CLEAR); 1768 } 1769 } 1770 1771 int 1772 vdev_readable(vdev_t *vd) 1773 { 1774 /* XXPOLICY */ 1775 return (!vdev_is_dead(vd)); 1776 } 1777 1778 int 1779 vdev_writeable(vdev_t *vd) 1780 { 1781 return (vd->vdev_ops->vdev_op_leaf ? 1782 !vd->vdev_is_failing : !vdev_is_dead(vd)); 1783 } 1784 1785 int 1786 vdev_is_dead(vdev_t *vd) 1787 { 1788 return (vd->vdev_state < VDEV_STATE_DEGRADED); 1789 } 1790 1791 int 1792 vdev_error_inject(vdev_t *vd, zio_t *zio) 1793 { 1794 int error = 0; 1795 1796 if (vd->vdev_fault_mode == VDEV_FAULT_NONE) 1797 return (0); 1798 1799 if (((1ULL << zio->io_type) & vd->vdev_fault_mask) == 0) 1800 return (0); 1801 1802 switch (vd->vdev_fault_mode) { 1803 case VDEV_FAULT_RANDOM: 1804 if (spa_get_random(vd->vdev_fault_arg) == 0) 1805 error = EIO; 1806 break; 1807 1808 case VDEV_FAULT_COUNT: 1809 if ((int64_t)--vd->vdev_fault_arg <= 0) 1810 vd->vdev_fault_mode = VDEV_FAULT_NONE; 1811 error = EIO; 1812 break; 1813 } 1814 1815 return (error); 1816 } 1817 1818 /* 1819 * Get statistics for the given vdev. 1820 */ 1821 void 1822 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs) 1823 { 1824 vdev_t *rvd = vd->vdev_spa->spa_root_vdev; 1825 int c, t; 1826 1827 mutex_enter(&vd->vdev_stat_lock); 1828 bcopy(&vd->vdev_stat, vs, sizeof (*vs)); 1829 vs->vs_timestamp = gethrtime() - vs->vs_timestamp; 1830 vs->vs_state = vd->vdev_state; 1831 vs->vs_rsize = vdev_get_rsize(vd); 1832 mutex_exit(&vd->vdev_stat_lock); 1833 1834 /* 1835 * If we're getting stats on the root vdev, aggregate the I/O counts 1836 * over all top-level vdevs (i.e. the direct children of the root). 1837 */ 1838 if (vd == rvd) { 1839 for (c = 0; c < rvd->vdev_children; c++) { 1840 vdev_t *cvd = rvd->vdev_child[c]; 1841 vdev_stat_t *cvs = &cvd->vdev_stat; 1842 1843 mutex_enter(&vd->vdev_stat_lock); 1844 for (t = 0; t < ZIO_TYPES; t++) { 1845 vs->vs_ops[t] += cvs->vs_ops[t]; 1846 vs->vs_bytes[t] += cvs->vs_bytes[t]; 1847 } 1848 vs->vs_read_errors += cvs->vs_read_errors; 1849 vs->vs_write_errors += cvs->vs_write_errors; 1850 vs->vs_checksum_errors += cvs->vs_checksum_errors; 1851 vs->vs_scrub_examined += cvs->vs_scrub_examined; 1852 vs->vs_scrub_errors += cvs->vs_scrub_errors; 1853 mutex_exit(&vd->vdev_stat_lock); 1854 } 1855 } 1856 } 1857 1858 void 1859 vdev_stat_update(zio_t *zio) 1860 { 1861 vdev_t *vd = zio->io_vd; 1862 vdev_t *pvd; 1863 uint64_t txg = zio->io_txg; 1864 vdev_stat_t *vs = &vd->vdev_stat; 1865 zio_type_t type = zio->io_type; 1866 int flags = zio->io_flags; 1867 1868 if (zio->io_error == 0) { 1869 if (!(flags & ZIO_FLAG_IO_BYPASS)) { 1870 mutex_enter(&vd->vdev_stat_lock); 1871 vs->vs_ops[type]++; 1872 vs->vs_bytes[type] += zio->io_size; 1873 mutex_exit(&vd->vdev_stat_lock); 1874 } 1875 if ((flags & ZIO_FLAG_IO_REPAIR) && 1876 zio->io_delegate_list == NULL) { 1877 mutex_enter(&vd->vdev_stat_lock); 1878 if (flags & ZIO_FLAG_SCRUB_THREAD) 1879 vs->vs_scrub_repaired += zio->io_size; 1880 else 1881 vs->vs_self_healed += zio->io_size; 1882 mutex_exit(&vd->vdev_stat_lock); 1883 } 1884 return; 1885 } 1886 1887 if (flags & ZIO_FLAG_SPECULATIVE) 1888 return; 1889 1890 if (vdev_readable(vd)) { 1891 mutex_enter(&vd->vdev_stat_lock); 1892 if (type == ZIO_TYPE_READ) { 1893 if (zio->io_error == ECKSUM) 1894 vs->vs_checksum_errors++; 1895 else 1896 vs->vs_read_errors++; 1897 } 1898 if (type == ZIO_TYPE_WRITE) 1899 vs->vs_write_errors++; 1900 mutex_exit(&vd->vdev_stat_lock); 1901 } 1902 1903 if (type == ZIO_TYPE_WRITE) { 1904 if (txg == 0 || vd->vdev_children != 0) 1905 return; 1906 if (flags & ZIO_FLAG_SCRUB_THREAD) { 1907 ASSERT(flags & ZIO_FLAG_IO_REPAIR); 1908 for (pvd = vd; pvd != NULL; pvd = pvd->vdev_parent) 1909 vdev_dtl_dirty(&pvd->vdev_dtl_scrub, txg, 1); 1910 } 1911 if (!(flags & ZIO_FLAG_IO_REPAIR)) { 1912 if (vdev_dtl_contains(&vd->vdev_dtl_map, txg, 1)) 1913 return; 1914 vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg); 1915 for (pvd = vd; pvd != NULL; pvd = pvd->vdev_parent) 1916 vdev_dtl_dirty(&pvd->vdev_dtl_map, txg, 1); 1917 } 1918 } 1919 } 1920 1921 void 1922 vdev_scrub_stat_update(vdev_t *vd, pool_scrub_type_t type, boolean_t complete) 1923 { 1924 int c; 1925 vdev_stat_t *vs = &vd->vdev_stat; 1926 1927 for (c = 0; c < vd->vdev_children; c++) 1928 vdev_scrub_stat_update(vd->vdev_child[c], type, complete); 1929 1930 mutex_enter(&vd->vdev_stat_lock); 1931 1932 if (type == POOL_SCRUB_NONE) { 1933 /* 1934 * Update completion and end time. Leave everything else alone 1935 * so we can report what happened during the previous scrub. 1936 */ 1937 vs->vs_scrub_complete = complete; 1938 vs->vs_scrub_end = gethrestime_sec(); 1939 } else { 1940 vs->vs_scrub_type = type; 1941 vs->vs_scrub_complete = 0; 1942 vs->vs_scrub_examined = 0; 1943 vs->vs_scrub_repaired = 0; 1944 vs->vs_scrub_errors = 0; 1945 vs->vs_scrub_start = gethrestime_sec(); 1946 vs->vs_scrub_end = 0; 1947 } 1948 1949 mutex_exit(&vd->vdev_stat_lock); 1950 } 1951 1952 /* 1953 * Update the in-core space usage stats for this vdev and the root vdev. 1954 */ 1955 void 1956 vdev_space_update(vdev_t *vd, int64_t space_delta, int64_t alloc_delta) 1957 { 1958 int64_t dspace_delta = space_delta; 1959 spa_t *spa = vd->vdev_spa; 1960 vdev_t *rvd = spa->spa_root_vdev; 1961 1962 ASSERT(vd == vd->vdev_top); 1963 ASSERT(rvd == vd->vdev_parent); 1964 ASSERT(vd->vdev_ms_count != 0); 1965 1966 /* 1967 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion 1968 * factor. We must calculate this here and not at the root vdev 1969 * because the root vdev's psize-to-asize is simply the max of its 1970 * childrens', thus not accurate enough for us. 1971 */ 1972 ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0); 1973 dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) * 1974 vd->vdev_deflate_ratio; 1975 1976 mutex_enter(&vd->vdev_stat_lock); 1977 vd->vdev_stat.vs_space += space_delta; 1978 vd->vdev_stat.vs_alloc += alloc_delta; 1979 vd->vdev_stat.vs_dspace += dspace_delta; 1980 mutex_exit(&vd->vdev_stat_lock); 1981 1982 /* 1983 * Don't count non-normal (e.g. intent log) space as part of 1984 * the pool's capacity. 1985 */ 1986 if (vd->vdev_mg->mg_class != spa->spa_normal_class) 1987 return; 1988 1989 mutex_enter(&rvd->vdev_stat_lock); 1990 rvd->vdev_stat.vs_space += space_delta; 1991 rvd->vdev_stat.vs_alloc += alloc_delta; 1992 rvd->vdev_stat.vs_dspace += dspace_delta; 1993 mutex_exit(&rvd->vdev_stat_lock); 1994 } 1995 1996 /* 1997 * Mark a top-level vdev's config as dirty, placing it on the dirty list 1998 * so that it will be written out next time the vdev configuration is synced. 1999 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs. 2000 */ 2001 void 2002 vdev_config_dirty(vdev_t *vd) 2003 { 2004 spa_t *spa = vd->vdev_spa; 2005 vdev_t *rvd = spa->spa_root_vdev; 2006 int c; 2007 2008 /* 2009 * The dirty list is protected by the config lock. The caller must 2010 * either hold the config lock as writer, or must be the sync thread 2011 * (which holds the lock as reader). There's only one sync thread, 2012 * so this is sufficient to ensure mutual exclusion. 2013 */ 2014 ASSERT(spa_config_held(spa, RW_WRITER) || 2015 dsl_pool_sync_context(spa_get_dsl(spa))); 2016 2017 if (vd == rvd) { 2018 for (c = 0; c < rvd->vdev_children; c++) 2019 vdev_config_dirty(rvd->vdev_child[c]); 2020 } else { 2021 ASSERT(vd == vd->vdev_top); 2022 2023 if (!list_link_active(&vd->vdev_dirty_node)) 2024 list_insert_head(&spa->spa_dirty_list, vd); 2025 } 2026 } 2027 2028 void 2029 vdev_config_clean(vdev_t *vd) 2030 { 2031 spa_t *spa = vd->vdev_spa; 2032 2033 ASSERT(spa_config_held(spa, RW_WRITER) || 2034 dsl_pool_sync_context(spa_get_dsl(spa))); 2035 2036 ASSERT(list_link_active(&vd->vdev_dirty_node)); 2037 list_remove(&spa->spa_dirty_list, vd); 2038 } 2039 2040 void 2041 vdev_propagate_state(vdev_t *vd) 2042 { 2043 vdev_t *rvd = vd->vdev_spa->spa_root_vdev; 2044 int degraded = 0, faulted = 0; 2045 int corrupted = 0; 2046 int c; 2047 vdev_t *child; 2048 2049 if (vd->vdev_children > 0) { 2050 for (c = 0; c < vd->vdev_children; c++) { 2051 child = vd->vdev_child[c]; 2052 if (vdev_is_dead(child) && !vdev_readable(child)) 2053 faulted++; 2054 else if (child->vdev_state <= VDEV_STATE_DEGRADED) 2055 degraded++; 2056 2057 if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA) 2058 corrupted++; 2059 } 2060 2061 vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded); 2062 2063 /* 2064 * Root special: if there is a toplevel vdev that cannot be 2065 * opened due to corrupted metadata, then propagate the root 2066 * vdev's aux state as 'corrupt' rather than 'insufficient 2067 * replicas'. 2068 */ 2069 if (corrupted && vd == rvd && 2070 rvd->vdev_state == VDEV_STATE_CANT_OPEN) 2071 vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN, 2072 VDEV_AUX_CORRUPT_DATA); 2073 } 2074 2075 if (vd->vdev_parent && !vd->vdev_islog) 2076 vdev_propagate_state(vd->vdev_parent); 2077 } 2078 2079 /* 2080 * Set a vdev's state. If this is during an open, we don't update the parent 2081 * state, because we're in the process of opening children depth-first. 2082 * Otherwise, we propagate the change to the parent. 2083 * 2084 * If this routine places a device in a faulted state, an appropriate ereport is 2085 * generated. 2086 */ 2087 void 2088 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux) 2089 { 2090 uint64_t save_state; 2091 2092 if (state == vd->vdev_state) { 2093 vd->vdev_stat.vs_aux = aux; 2094 return; 2095 } 2096 2097 save_state = vd->vdev_state; 2098 2099 vd->vdev_state = state; 2100 vd->vdev_stat.vs_aux = aux; 2101 2102 /* 2103 * If we are setting the vdev state to anything but an open state, then 2104 * always close the underlying device. Otherwise, we keep accessible 2105 * but invalid devices open forever. We don't call vdev_close() itself, 2106 * because that implies some extra checks (offline, etc) that we don't 2107 * want here. This is limited to leaf devices, because otherwise 2108 * closing the device will affect other children. 2109 */ 2110 if (!vdev_readable(vd) && vd->vdev_ops->vdev_op_leaf) 2111 vd->vdev_ops->vdev_op_close(vd); 2112 2113 if (vd->vdev_removed && 2114 state == VDEV_STATE_CANT_OPEN && 2115 (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) { 2116 /* 2117 * If the previous state is set to VDEV_STATE_REMOVED, then this 2118 * device was previously marked removed and someone attempted to 2119 * reopen it. If this failed due to a nonexistent device, then 2120 * keep the device in the REMOVED state. We also let this be if 2121 * it is one of our special test online cases, which is only 2122 * attempting to online the device and shouldn't generate an FMA 2123 * fault. 2124 */ 2125 vd->vdev_state = VDEV_STATE_REMOVED; 2126 vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 2127 } else if (state == VDEV_STATE_REMOVED) { 2128 /* 2129 * Indicate to the ZFS DE that this device has been removed, and 2130 * any recent errors should be ignored. 2131 */ 2132 zfs_post_remove(vd->vdev_spa, vd); 2133 vd->vdev_removed = B_TRUE; 2134 } else if (state == VDEV_STATE_CANT_OPEN) { 2135 /* 2136 * If we fail to open a vdev during an import, we mark it as 2137 * "not available", which signifies that it was never there to 2138 * begin with. Failure to open such a device is not considered 2139 * an error. 2140 */ 2141 if (vd->vdev_spa->spa_load_state == SPA_LOAD_IMPORT && 2142 vd->vdev_ops->vdev_op_leaf) 2143 vd->vdev_not_present = 1; 2144 2145 /* 2146 * Post the appropriate ereport. If the 'prevstate' field is 2147 * set to something other than VDEV_STATE_UNKNOWN, it indicates 2148 * that this is part of a vdev_reopen(). In this case, we don't 2149 * want to post the ereport if the device was already in the 2150 * CANT_OPEN state beforehand. 2151 * 2152 * If the 'checkremove' flag is set, then this is an attempt to 2153 * online the device in response to an insertion event. If we 2154 * hit this case, then we have detected an insertion event for a 2155 * faulted or offline device that wasn't in the removed state. 2156 * In this scenario, we don't post an ereport because we are 2157 * about to replace the device, or attempt an online with 2158 * vdev_forcefault, which will generate the fault for us. 2159 */ 2160 if ((vd->vdev_prevstate != state || vd->vdev_forcefault) && 2161 !vd->vdev_not_present && !vd->vdev_checkremove && 2162 vd != vd->vdev_spa->spa_root_vdev) { 2163 const char *class; 2164 2165 switch (aux) { 2166 case VDEV_AUX_OPEN_FAILED: 2167 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED; 2168 break; 2169 case VDEV_AUX_CORRUPT_DATA: 2170 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA; 2171 break; 2172 case VDEV_AUX_NO_REPLICAS: 2173 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS; 2174 break; 2175 case VDEV_AUX_BAD_GUID_SUM: 2176 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM; 2177 break; 2178 case VDEV_AUX_TOO_SMALL: 2179 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL; 2180 break; 2181 case VDEV_AUX_BAD_LABEL: 2182 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL; 2183 break; 2184 default: 2185 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN; 2186 } 2187 2188 zfs_ereport_post(class, vd->vdev_spa, 2189 vd, NULL, save_state, 0); 2190 } 2191 2192 /* Erase any notion of persistent removed state */ 2193 vd->vdev_removed = B_FALSE; 2194 } else { 2195 vd->vdev_removed = B_FALSE; 2196 } 2197 2198 if (!isopen) 2199 vdev_propagate_state(vd); 2200 } 2201