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