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 vdev_t *rvd = spa->spa_root_vdev; 851 int c; 852 853 ASSERT(spa_config_held(spa, RW_WRITER)); 854 855 if (vd == rvd) { 856 for (c = 0; c < rvd->vdev_children; c++) 857 vdev_reopen(rvd->vdev_child[c]); 858 return; 859 } 860 861 /* only valid for top-level vdevs */ 862 ASSERT3P(vd, ==, vd->vdev_top); 863 864 vdev_close(vd); 865 (void) vdev_open(vd); 866 867 /* 868 * Reassess root vdev's health. 869 */ 870 rvd->vdev_state = VDEV_STATE_HEALTHY; 871 for (c = 0; c < rvd->vdev_children; c++) { 872 uint64_t state = rvd->vdev_child[c]->vdev_state; 873 rvd->vdev_state = MIN(rvd->vdev_state, state); 874 } 875 } 876 877 int 878 vdev_create(vdev_t *vd, uint64_t txg) 879 { 880 int error; 881 882 /* 883 * Normally, partial opens (e.g. of a mirror) are allowed. 884 * For a create, however, we want to fail the request if 885 * there are any components we can't open. 886 */ 887 error = vdev_open(vd); 888 889 if (error || vd->vdev_state != VDEV_STATE_HEALTHY) { 890 vdev_close(vd); 891 return (error ? error : ENXIO); 892 } 893 894 /* 895 * Recursively initialize all labels. 896 */ 897 if ((error = vdev_label_init(vd, txg)) != 0) { 898 vdev_close(vd); 899 return (error); 900 } 901 902 return (0); 903 } 904 905 /* 906 * The is the latter half of vdev_create(). It is distinct because it 907 * involves initiating transactions in order to do metaslab creation. 908 * For creation, we want to try to create all vdevs at once and then undo it 909 * if anything fails; this is much harder if we have pending transactions. 910 */ 911 void 912 vdev_init(vdev_t *vd, uint64_t txg) 913 { 914 /* 915 * Aim for roughly 200 metaslabs per vdev. 916 */ 917 vd->vdev_ms_shift = highbit(vd->vdev_asize / 200); 918 vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT); 919 920 /* 921 * Initialize the vdev's metaslabs. This can't fail because 922 * there's nothing to read when creating all new metaslabs. 923 */ 924 VERIFY(vdev_metaslab_init(vd, txg) == 0); 925 } 926 927 void 928 vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg) 929 { 930 ASSERT(vd == vd->vdev_top); 931 ASSERT(ISP2(flags)); 932 933 if (flags & VDD_METASLAB) 934 (void) txg_list_add(&vd->vdev_ms_list, arg, txg); 935 936 if (flags & VDD_DTL) 937 (void) txg_list_add(&vd->vdev_dtl_list, arg, txg); 938 939 (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg); 940 } 941 942 void 943 vdev_dtl_dirty(space_map_t *sm, uint64_t txg, uint64_t size) 944 { 945 mutex_enter(sm->sm_lock); 946 if (!space_map_contains(sm, txg, size)) 947 space_map_add(sm, txg, size); 948 mutex_exit(sm->sm_lock); 949 } 950 951 int 952 vdev_dtl_contains(space_map_t *sm, uint64_t txg, uint64_t size) 953 { 954 int dirty; 955 956 /* 957 * Quick test without the lock -- covers the common case that 958 * there are no dirty time segments. 959 */ 960 if (sm->sm_space == 0) 961 return (0); 962 963 mutex_enter(sm->sm_lock); 964 dirty = space_map_contains(sm, txg, size); 965 mutex_exit(sm->sm_lock); 966 967 return (dirty); 968 } 969 970 /* 971 * Reassess DTLs after a config change or scrub completion. 972 */ 973 void 974 vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done) 975 { 976 spa_t *spa = vd->vdev_spa; 977 int c; 978 979 ASSERT(spa_config_held(spa, RW_WRITER)); 980 981 if (vd->vdev_children == 0) { 982 mutex_enter(&vd->vdev_dtl_lock); 983 /* 984 * We're successfully scrubbed everything up to scrub_txg. 985 * Therefore, excise all old DTLs up to that point, then 986 * fold in the DTLs for everything we couldn't scrub. 987 */ 988 if (scrub_txg != 0) { 989 space_map_excise(&vd->vdev_dtl_map, 0, scrub_txg); 990 space_map_union(&vd->vdev_dtl_map, &vd->vdev_dtl_scrub); 991 } 992 if (scrub_done) 993 space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL); 994 mutex_exit(&vd->vdev_dtl_lock); 995 if (txg != 0) 996 vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg); 997 return; 998 } 999 1000 /* 1001 * Make sure the DTLs are always correct under the scrub lock. 1002 */ 1003 if (vd == spa->spa_root_vdev) 1004 mutex_enter(&spa->spa_scrub_lock); 1005 1006 mutex_enter(&vd->vdev_dtl_lock); 1007 space_map_vacate(&vd->vdev_dtl_map, NULL, NULL); 1008 space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL); 1009 mutex_exit(&vd->vdev_dtl_lock); 1010 1011 for (c = 0; c < vd->vdev_children; c++) { 1012 vdev_t *cvd = vd->vdev_child[c]; 1013 vdev_dtl_reassess(cvd, txg, scrub_txg, scrub_done); 1014 mutex_enter(&vd->vdev_dtl_lock); 1015 space_map_union(&vd->vdev_dtl_map, &cvd->vdev_dtl_map); 1016 space_map_union(&vd->vdev_dtl_scrub, &cvd->vdev_dtl_scrub); 1017 mutex_exit(&vd->vdev_dtl_lock); 1018 } 1019 1020 if (vd == spa->spa_root_vdev) 1021 mutex_exit(&spa->spa_scrub_lock); 1022 } 1023 1024 static int 1025 vdev_dtl_load(vdev_t *vd) 1026 { 1027 spa_t *spa = vd->vdev_spa; 1028 space_map_obj_t *smo = &vd->vdev_dtl; 1029 objset_t *mos = spa->spa_meta_objset; 1030 dmu_buf_t *db; 1031 int error; 1032 1033 ASSERT(vd->vdev_children == 0); 1034 1035 if (smo->smo_object == 0) 1036 return (0); 1037 1038 if ((error = dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)) != 0) 1039 return (error); 1040 1041 ASSERT3U(db->db_size, ==, sizeof (*smo)); 1042 bcopy(db->db_data, smo, db->db_size); 1043 dmu_buf_rele(db, FTAG); 1044 1045 mutex_enter(&vd->vdev_dtl_lock); 1046 error = space_map_load(&vd->vdev_dtl_map, NULL, SM_ALLOC, smo, mos); 1047 mutex_exit(&vd->vdev_dtl_lock); 1048 1049 return (error); 1050 } 1051 1052 void 1053 vdev_dtl_sync(vdev_t *vd, uint64_t txg) 1054 { 1055 spa_t *spa = vd->vdev_spa; 1056 space_map_obj_t *smo = &vd->vdev_dtl; 1057 space_map_t *sm = &vd->vdev_dtl_map; 1058 objset_t *mos = spa->spa_meta_objset; 1059 space_map_t smsync; 1060 kmutex_t smlock; 1061 dmu_buf_t *db; 1062 dmu_tx_t *tx; 1063 1064 dprintf("%s in txg %llu pass %d\n", 1065 vdev_description(vd), (u_longlong_t)txg, spa_sync_pass(spa)); 1066 1067 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 1068 1069 if (vd->vdev_detached) { 1070 if (smo->smo_object != 0) { 1071 int err = dmu_object_free(mos, smo->smo_object, tx); 1072 ASSERT3U(err, ==, 0); 1073 smo->smo_object = 0; 1074 } 1075 dmu_tx_commit(tx); 1076 dprintf("detach %s committed in txg %llu\n", 1077 vdev_description(vd), txg); 1078 return; 1079 } 1080 1081 if (smo->smo_object == 0) { 1082 ASSERT(smo->smo_objsize == 0); 1083 ASSERT(smo->smo_alloc == 0); 1084 smo->smo_object = dmu_object_alloc(mos, 1085 DMU_OT_SPACE_MAP, 1 << SPACE_MAP_BLOCKSHIFT, 1086 DMU_OT_SPACE_MAP_HEADER, sizeof (*smo), tx); 1087 ASSERT(smo->smo_object != 0); 1088 vdev_config_dirty(vd->vdev_top); 1089 } 1090 1091 mutex_init(&smlock, NULL, MUTEX_DEFAULT, NULL); 1092 1093 space_map_create(&smsync, sm->sm_start, sm->sm_size, sm->sm_shift, 1094 &smlock); 1095 1096 mutex_enter(&smlock); 1097 1098 mutex_enter(&vd->vdev_dtl_lock); 1099 space_map_walk(sm, space_map_add, &smsync); 1100 mutex_exit(&vd->vdev_dtl_lock); 1101 1102 space_map_truncate(smo, mos, tx); 1103 space_map_sync(&smsync, SM_ALLOC, smo, mos, tx); 1104 1105 space_map_destroy(&smsync); 1106 1107 mutex_exit(&smlock); 1108 mutex_destroy(&smlock); 1109 1110 VERIFY(0 == dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)); 1111 dmu_buf_will_dirty(db, tx); 1112 ASSERT3U(db->db_size, ==, sizeof (*smo)); 1113 bcopy(smo, db->db_data, db->db_size); 1114 dmu_buf_rele(db, FTAG); 1115 1116 dmu_tx_commit(tx); 1117 } 1118 1119 int 1120 vdev_load(vdev_t *vd) 1121 { 1122 spa_t *spa = vd->vdev_spa; 1123 int c, error; 1124 nvlist_t *label; 1125 uint64_t guid, state; 1126 1127 dprintf("loading %s\n", vdev_description(vd)); 1128 1129 /* 1130 * Recursively load all children. 1131 */ 1132 for (c = 0; c < vd->vdev_children; c++) 1133 if ((error = vdev_load(vd->vdev_child[c])) != 0) 1134 return (error); 1135 1136 /* 1137 * If this is a leaf vdev, make sure its agrees with its disk labels. 1138 */ 1139 if (vd->vdev_ops->vdev_op_leaf) { 1140 1141 if (vdev_is_dead(vd)) 1142 return (0); 1143 1144 /* 1145 * XXX state transitions don't propagate to parent here. 1146 * Also, merely setting the state isn't sufficient because 1147 * it's not persistent; a vdev_reopen() would make us 1148 * forget all about it. 1149 */ 1150 if ((label = vdev_label_read_config(vd)) == NULL) { 1151 dprintf("can't load label config\n"); 1152 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1153 VDEV_AUX_CORRUPT_DATA); 1154 return (0); 1155 } 1156 1157 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, 1158 &guid) != 0 || guid != spa_guid(spa)) { 1159 dprintf("bad or missing pool GUID (%llu)\n", guid); 1160 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1161 VDEV_AUX_CORRUPT_DATA); 1162 nvlist_free(label); 1163 return (0); 1164 } 1165 1166 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) || 1167 guid != vd->vdev_guid) { 1168 dprintf("bad or missing vdev guid (%llu != %llu)\n", 1169 guid, vd->vdev_guid); 1170 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1171 VDEV_AUX_CORRUPT_DATA); 1172 nvlist_free(label); 1173 return (0); 1174 } 1175 1176 /* 1177 * If we find a vdev with a matching pool guid and vdev guid, 1178 * but the pool state is not active, it indicates that the user 1179 * exported or destroyed the pool without affecting the config 1180 * cache (if / was mounted readonly, for example). In this 1181 * case, immediately return EBADF so the caller can remove it 1182 * from the config. 1183 */ 1184 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, 1185 &state)) { 1186 dprintf("missing pool state\n"); 1187 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1188 VDEV_AUX_CORRUPT_DATA); 1189 nvlist_free(label); 1190 return (0); 1191 } 1192 1193 if (state != POOL_STATE_ACTIVE && 1194 (spa->spa_load_state == SPA_LOAD_OPEN || 1195 (state != POOL_STATE_EXPORTED && 1196 state != POOL_STATE_DESTROYED))) { 1197 dprintf("pool state not active (%llu)\n", state); 1198 nvlist_free(label); 1199 return (EBADF); 1200 } 1201 1202 nvlist_free(label); 1203 } 1204 1205 /* 1206 * If this is a top-level vdev, initialize its metaslabs. 1207 */ 1208 if (vd == vd->vdev_top) { 1209 1210 if (vd->vdev_ashift == 0 || vd->vdev_asize == 0) { 1211 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1212 VDEV_AUX_CORRUPT_DATA); 1213 return (0); 1214 } 1215 1216 if ((error = vdev_metaslab_init(vd, 0)) != 0) { 1217 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1218 VDEV_AUX_CORRUPT_DATA); 1219 return (0); 1220 } 1221 } 1222 1223 /* 1224 * If this is a leaf vdev, load its DTL. 1225 */ 1226 if (vd->vdev_ops->vdev_op_leaf) { 1227 error = vdev_dtl_load(vd); 1228 if (error) { 1229 dprintf("can't load DTL for %s, error %d\n", 1230 vdev_description(vd), error); 1231 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1232 VDEV_AUX_CORRUPT_DATA); 1233 return (0); 1234 } 1235 } 1236 1237 return (0); 1238 } 1239 1240 void 1241 vdev_sync_done(vdev_t *vd, uint64_t txg) 1242 { 1243 metaslab_t *msp; 1244 1245 dprintf("%s txg %llu\n", vdev_description(vd), txg); 1246 1247 while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg))) 1248 metaslab_sync_done(msp, txg); 1249 } 1250 1251 void 1252 vdev_sync(vdev_t *vd, uint64_t txg) 1253 { 1254 spa_t *spa = vd->vdev_spa; 1255 vdev_t *lvd; 1256 metaslab_t *msp; 1257 dmu_tx_t *tx; 1258 1259 dprintf("%s txg %llu pass %d\n", 1260 vdev_description(vd), (u_longlong_t)txg, spa_sync_pass(spa)); 1261 1262 if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) { 1263 ASSERT(vd == vd->vdev_top); 1264 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 1265 vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset, 1266 DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx); 1267 ASSERT(vd->vdev_ms_array != 0); 1268 vdev_config_dirty(vd); 1269 dmu_tx_commit(tx); 1270 } 1271 1272 while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) { 1273 metaslab_sync(msp, txg); 1274 (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg)); 1275 } 1276 1277 while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL) 1278 vdev_dtl_sync(lvd, txg); 1279 1280 (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)); 1281 } 1282 1283 uint64_t 1284 vdev_psize_to_asize(vdev_t *vd, uint64_t psize) 1285 { 1286 return (vd->vdev_ops->vdev_op_asize(vd, psize)); 1287 } 1288 1289 void 1290 vdev_io_start(zio_t *zio) 1291 { 1292 zio->io_vd->vdev_ops->vdev_op_io_start(zio); 1293 } 1294 1295 void 1296 vdev_io_done(zio_t *zio) 1297 { 1298 zio->io_vd->vdev_ops->vdev_op_io_done(zio); 1299 } 1300 1301 const char * 1302 vdev_description(vdev_t *vd) 1303 { 1304 if (vd == NULL || vd->vdev_ops == NULL) 1305 return ("<unknown>"); 1306 1307 if (vd->vdev_path != NULL) 1308 return (vd->vdev_path); 1309 1310 if (vd->vdev_parent == NULL) 1311 return (spa_name(vd->vdev_spa)); 1312 1313 return (vd->vdev_ops->vdev_op_type); 1314 } 1315 1316 int 1317 vdev_online(spa_t *spa, uint64_t guid) 1318 { 1319 vdev_t *rvd, *vd; 1320 uint64_t txg; 1321 1322 txg = spa_vdev_enter(spa); 1323 1324 rvd = spa->spa_root_vdev; 1325 1326 if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL) 1327 return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 1328 1329 if (!vd->vdev_ops->vdev_op_leaf) 1330 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 1331 1332 dprintf("ONLINE: %s\n", vdev_description(vd)); 1333 1334 vd->vdev_offline = B_FALSE; 1335 vd->vdev_tmpoffline = B_FALSE; 1336 vdev_reopen(vd->vdev_top); 1337 1338 vdev_config_dirty(vd->vdev_top); 1339 1340 (void) spa_vdev_exit(spa, NULL, txg, 0); 1341 1342 VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0); 1343 1344 return (0); 1345 } 1346 1347 int 1348 vdev_offline(spa_t *spa, uint64_t guid, int istmp) 1349 { 1350 vdev_t *rvd, *vd; 1351 uint64_t txg; 1352 1353 txg = spa_vdev_enter(spa); 1354 1355 rvd = spa->spa_root_vdev; 1356 1357 if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL) 1358 return (spa_vdev_exit(spa, NULL, txg, ENODEV)); 1359 1360 if (!vd->vdev_ops->vdev_op_leaf) 1361 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); 1362 1363 dprintf("OFFLINE: %s\n", vdev_description(vd)); 1364 1365 /* 1366 * If the device isn't already offline, try to offline it. 1367 */ 1368 if (!vd->vdev_offline) { 1369 /* 1370 * If this device's top-level vdev has a non-empty DTL, 1371 * don't allow the device to be offlined. 1372 * 1373 * XXX -- make this more precise by allowing the offline 1374 * as long as the remaining devices don't have any DTL holes. 1375 */ 1376 if (vd->vdev_top->vdev_dtl_map.sm_space != 0) 1377 return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 1378 1379 /* 1380 * Offline this device and reopen its top-level vdev. 1381 * If this action results in the top-level vdev becoming 1382 * unusable, undo it and fail the request. 1383 */ 1384 vd->vdev_offline = B_TRUE; 1385 vdev_reopen(vd->vdev_top); 1386 if (vdev_is_dead(vd->vdev_top)) { 1387 vd->vdev_offline = B_FALSE; 1388 vdev_reopen(vd->vdev_top); 1389 return (spa_vdev_exit(spa, NULL, txg, EBUSY)); 1390 } 1391 } 1392 1393 vd->vdev_tmpoffline = istmp; 1394 1395 vdev_config_dirty(vd->vdev_top); 1396 1397 return (spa_vdev_exit(spa, NULL, txg, 0)); 1398 } 1399 1400 /* 1401 * Clear the error counts associated with this vdev. Unlike vdev_online() and 1402 * vdev_offline(), we assume the spa config is locked. We also clear all 1403 * children. If 'vd' is NULL, then the user wants to clear all vdevs. 1404 */ 1405 void 1406 vdev_clear(spa_t *spa, vdev_t *vd) 1407 { 1408 int c; 1409 1410 if (vd == NULL) 1411 vd = spa->spa_root_vdev; 1412 1413 vd->vdev_stat.vs_read_errors = 0; 1414 vd->vdev_stat.vs_write_errors = 0; 1415 vd->vdev_stat.vs_checksum_errors = 0; 1416 1417 for (c = 0; c < vd->vdev_children; c++) 1418 vdev_clear(spa, vd->vdev_child[c]); 1419 } 1420 1421 int 1422 vdev_is_dead(vdev_t *vd) 1423 { 1424 return (vd->vdev_state <= VDEV_STATE_CANT_OPEN); 1425 } 1426 1427 int 1428 vdev_error_inject(vdev_t *vd, zio_t *zio) 1429 { 1430 int error = 0; 1431 1432 if (vd->vdev_fault_mode == VDEV_FAULT_NONE) 1433 return (0); 1434 1435 if (((1ULL << zio->io_type) & vd->vdev_fault_mask) == 0) 1436 return (0); 1437 1438 switch (vd->vdev_fault_mode) { 1439 case VDEV_FAULT_RANDOM: 1440 if (spa_get_random(vd->vdev_fault_arg) == 0) 1441 error = EIO; 1442 break; 1443 1444 case VDEV_FAULT_COUNT: 1445 if ((int64_t)--vd->vdev_fault_arg <= 0) 1446 vd->vdev_fault_mode = VDEV_FAULT_NONE; 1447 error = EIO; 1448 break; 1449 } 1450 1451 if (error != 0) { 1452 dprintf("returning %d for type %d on %s state %d offset %llx\n", 1453 error, zio->io_type, vdev_description(vd), 1454 vd->vdev_state, zio->io_offset); 1455 } 1456 1457 return (error); 1458 } 1459 1460 /* 1461 * Get statistics for the given vdev. 1462 */ 1463 void 1464 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs) 1465 { 1466 vdev_t *rvd = vd->vdev_spa->spa_root_vdev; 1467 int c, t; 1468 1469 mutex_enter(&vd->vdev_stat_lock); 1470 bcopy(&vd->vdev_stat, vs, sizeof (*vs)); 1471 vs->vs_timestamp = gethrtime() - vs->vs_timestamp; 1472 vs->vs_state = vd->vdev_state; 1473 vs->vs_rsize = vdev_get_rsize(vd); 1474 mutex_exit(&vd->vdev_stat_lock); 1475 1476 /* 1477 * If we're getting stats on the root vdev, aggregate the I/O counts 1478 * over all top-level vdevs (i.e. the direct children of the root). 1479 */ 1480 if (vd == rvd) { 1481 for (c = 0; c < rvd->vdev_children; c++) { 1482 vdev_t *cvd = rvd->vdev_child[c]; 1483 vdev_stat_t *cvs = &cvd->vdev_stat; 1484 1485 mutex_enter(&vd->vdev_stat_lock); 1486 for (t = 0; t < ZIO_TYPES; t++) { 1487 vs->vs_ops[t] += cvs->vs_ops[t]; 1488 vs->vs_bytes[t] += cvs->vs_bytes[t]; 1489 } 1490 vs->vs_read_errors += cvs->vs_read_errors; 1491 vs->vs_write_errors += cvs->vs_write_errors; 1492 vs->vs_checksum_errors += cvs->vs_checksum_errors; 1493 vs->vs_scrub_examined += cvs->vs_scrub_examined; 1494 vs->vs_scrub_errors += cvs->vs_scrub_errors; 1495 mutex_exit(&vd->vdev_stat_lock); 1496 } 1497 } 1498 } 1499 1500 void 1501 vdev_stat_update(zio_t *zio) 1502 { 1503 vdev_t *vd = zio->io_vd; 1504 vdev_t *pvd; 1505 uint64_t txg = zio->io_txg; 1506 vdev_stat_t *vs = &vd->vdev_stat; 1507 zio_type_t type = zio->io_type; 1508 int flags = zio->io_flags; 1509 1510 if (zio->io_error == 0) { 1511 if (!(flags & ZIO_FLAG_IO_BYPASS)) { 1512 mutex_enter(&vd->vdev_stat_lock); 1513 vs->vs_ops[type]++; 1514 vs->vs_bytes[type] += zio->io_size; 1515 mutex_exit(&vd->vdev_stat_lock); 1516 } 1517 if ((flags & ZIO_FLAG_IO_REPAIR) && 1518 zio->io_delegate_list == NULL) { 1519 mutex_enter(&vd->vdev_stat_lock); 1520 if (flags & (ZIO_FLAG_SCRUB | ZIO_FLAG_RESILVER)) 1521 vs->vs_scrub_repaired += zio->io_size; 1522 else 1523 vs->vs_self_healed += zio->io_size; 1524 mutex_exit(&vd->vdev_stat_lock); 1525 } 1526 return; 1527 } 1528 1529 if (flags & ZIO_FLAG_SPECULATIVE) 1530 return; 1531 1532 if (!vdev_is_dead(vd)) { 1533 mutex_enter(&vd->vdev_stat_lock); 1534 if (type == ZIO_TYPE_READ) { 1535 if (zio->io_error == ECKSUM) 1536 vs->vs_checksum_errors++; 1537 else 1538 vs->vs_read_errors++; 1539 } 1540 if (type == ZIO_TYPE_WRITE) 1541 vs->vs_write_errors++; 1542 mutex_exit(&vd->vdev_stat_lock); 1543 } 1544 1545 if (type == ZIO_TYPE_WRITE) { 1546 if (txg == 0 || vd->vdev_children != 0) 1547 return; 1548 if (flags & (ZIO_FLAG_SCRUB | ZIO_FLAG_RESILVER)) { 1549 ASSERT(flags & ZIO_FLAG_IO_REPAIR); 1550 for (pvd = vd; pvd != NULL; pvd = pvd->vdev_parent) 1551 vdev_dtl_dirty(&pvd->vdev_dtl_scrub, txg, 1); 1552 } 1553 if (!(flags & ZIO_FLAG_IO_REPAIR)) { 1554 if (vdev_dtl_contains(&vd->vdev_dtl_map, txg, 1)) 1555 return; 1556 vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg); 1557 for (pvd = vd; pvd != NULL; pvd = pvd->vdev_parent) 1558 vdev_dtl_dirty(&pvd->vdev_dtl_map, txg, 1); 1559 } 1560 } 1561 } 1562 1563 void 1564 vdev_scrub_stat_update(vdev_t *vd, pool_scrub_type_t type, boolean_t complete) 1565 { 1566 int c; 1567 vdev_stat_t *vs = &vd->vdev_stat; 1568 1569 for (c = 0; c < vd->vdev_children; c++) 1570 vdev_scrub_stat_update(vd->vdev_child[c], type, complete); 1571 1572 mutex_enter(&vd->vdev_stat_lock); 1573 1574 if (type == POOL_SCRUB_NONE) { 1575 /* 1576 * Update completion and end time. Leave everything else alone 1577 * so we can report what happened during the previous scrub. 1578 */ 1579 vs->vs_scrub_complete = complete; 1580 vs->vs_scrub_end = gethrestime_sec(); 1581 } else { 1582 vs->vs_scrub_type = type; 1583 vs->vs_scrub_complete = 0; 1584 vs->vs_scrub_examined = 0; 1585 vs->vs_scrub_repaired = 0; 1586 vs->vs_scrub_errors = 0; 1587 vs->vs_scrub_start = gethrestime_sec(); 1588 vs->vs_scrub_end = 0; 1589 } 1590 1591 mutex_exit(&vd->vdev_stat_lock); 1592 } 1593 1594 /* 1595 * Update the in-core space usage stats for this vdev and the root vdev. 1596 */ 1597 void 1598 vdev_space_update(vdev_t *vd, uint64_t space_delta, uint64_t alloc_delta) 1599 { 1600 ASSERT(vd == vd->vdev_top); 1601 1602 do { 1603 mutex_enter(&vd->vdev_stat_lock); 1604 vd->vdev_stat.vs_space += space_delta; 1605 vd->vdev_stat.vs_alloc += alloc_delta; 1606 mutex_exit(&vd->vdev_stat_lock); 1607 } while ((vd = vd->vdev_parent) != NULL); 1608 } 1609 1610 /* 1611 * Various knobs to tune a vdev. 1612 */ 1613 static vdev_knob_t vdev_knob[] = { 1614 { 1615 "cache_size", 1616 "size of the read-ahead cache", 1617 0, 1618 1ULL << 30, 1619 10ULL << 20, 1620 offsetof(struct vdev, vdev_cache.vc_size) 1621 }, 1622 { 1623 "cache_bshift", 1624 "log2 of cache blocksize", 1625 SPA_MINBLOCKSHIFT, 1626 SPA_MAXBLOCKSHIFT, 1627 16, 1628 offsetof(struct vdev, vdev_cache.vc_bshift) 1629 }, 1630 { 1631 "cache_max", 1632 "largest block size to cache", 1633 0, 1634 SPA_MAXBLOCKSIZE, 1635 1ULL << 14, 1636 offsetof(struct vdev, vdev_cache.vc_max) 1637 }, 1638 { 1639 "min_pending", 1640 "minimum pending I/Os to the disk", 1641 1, 1642 10000, 1643 2, 1644 offsetof(struct vdev, vdev_queue.vq_min_pending) 1645 }, 1646 { 1647 "max_pending", 1648 "maximum pending I/Os to the disk", 1649 1, 1650 10000, 1651 35, 1652 offsetof(struct vdev, vdev_queue.vq_max_pending) 1653 }, 1654 { 1655 "scrub_limit", 1656 "maximum scrub/resilver I/O queue", 1657 0, 1658 10000, 1659 70, 1660 offsetof(struct vdev, vdev_queue.vq_scrub_limit) 1661 }, 1662 { 1663 "agg_limit", 1664 "maximum size of aggregated I/Os", 1665 0, 1666 SPA_MAXBLOCKSIZE, 1667 SPA_MAXBLOCKSIZE, 1668 offsetof(struct vdev, vdev_queue.vq_agg_limit) 1669 }, 1670 { 1671 "time_shift", 1672 "deadline = pri + (lbolt >> time_shift)", 1673 0, 1674 63, 1675 4, 1676 offsetof(struct vdev, vdev_queue.vq_time_shift) 1677 }, 1678 { 1679 "ramp_rate", 1680 "exponential I/O issue ramp-up rate", 1681 1, 1682 10000, 1683 2, 1684 offsetof(struct vdev, vdev_queue.vq_ramp_rate) 1685 }, 1686 }; 1687 1688 vdev_knob_t * 1689 vdev_knob_next(vdev_knob_t *vk) 1690 { 1691 if (vk == NULL) 1692 return (vdev_knob); 1693 1694 if (++vk == vdev_knob + sizeof (vdev_knob) / sizeof (vdev_knob_t)) 1695 return (NULL); 1696 1697 return (vk); 1698 } 1699 1700 /* 1701 * Mark a top-level vdev's config as dirty, placing it on the dirty list 1702 * so that it will be written out next time the vdev configuration is synced. 1703 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs. 1704 */ 1705 void 1706 vdev_config_dirty(vdev_t *vd) 1707 { 1708 spa_t *spa = vd->vdev_spa; 1709 vdev_t *rvd = spa->spa_root_vdev; 1710 int c; 1711 1712 /* 1713 * The dirty list is protected by the config lock. The caller must 1714 * either hold the config lock as writer, or must be the sync thread 1715 * (which holds the lock as reader). There's only one sync thread, 1716 * so this is sufficient to ensure mutual exclusion. 1717 */ 1718 ASSERT(spa_config_held(spa, RW_WRITER) || 1719 dsl_pool_sync_context(spa_get_dsl(spa))); 1720 1721 if (vd == rvd) { 1722 for (c = 0; c < rvd->vdev_children; c++) 1723 vdev_config_dirty(rvd->vdev_child[c]); 1724 } else { 1725 ASSERT(vd == vd->vdev_top); 1726 1727 if (!list_link_active(&vd->vdev_dirty_node)) 1728 list_insert_head(&spa->spa_dirty_list, vd); 1729 } 1730 } 1731 1732 void 1733 vdev_config_clean(vdev_t *vd) 1734 { 1735 spa_t *spa = vd->vdev_spa; 1736 1737 ASSERT(spa_config_held(spa, RW_WRITER) || 1738 dsl_pool_sync_context(spa_get_dsl(spa))); 1739 1740 ASSERT(list_link_active(&vd->vdev_dirty_node)); 1741 list_remove(&spa->spa_dirty_list, vd); 1742 } 1743 1744 /* 1745 * Set a vdev's state. If this is during an open, we don't update the parent 1746 * state, because we're in the process of opening children depth-first. 1747 * Otherwise, we propagate the change to the parent. 1748 * 1749 * If this routine places a device in a faulted state, an appropriate ereport is 1750 * generated. 1751 */ 1752 void 1753 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux) 1754 { 1755 uint64_t prev_state; 1756 1757 if (state == vd->vdev_state) { 1758 vd->vdev_stat.vs_aux = aux; 1759 return; 1760 } 1761 1762 prev_state = vd->vdev_state; 1763 1764 vd->vdev_state = state; 1765 vd->vdev_stat.vs_aux = aux; 1766 1767 if (state == VDEV_STATE_CANT_OPEN) { 1768 /* 1769 * If we fail to open a vdev during an import, we mark it as 1770 * "not available", which signifies that it was never there to 1771 * begin with. Failure to open such a device is not considered 1772 * an error. 1773 */ 1774 if (!vd->vdev_not_present && 1775 vd != vd->vdev_spa->spa_root_vdev) { 1776 const char *class; 1777 1778 switch (aux) { 1779 case VDEV_AUX_OPEN_FAILED: 1780 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED; 1781 break; 1782 case VDEV_AUX_CORRUPT_DATA: 1783 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA; 1784 break; 1785 case VDEV_AUX_NO_REPLICAS: 1786 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS; 1787 break; 1788 case VDEV_AUX_BAD_GUID_SUM: 1789 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM; 1790 break; 1791 case VDEV_AUX_TOO_SMALL: 1792 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL; 1793 break; 1794 case VDEV_AUX_BAD_LABEL: 1795 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL; 1796 break; 1797 default: 1798 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN; 1799 } 1800 1801 zfs_ereport_post(class, vd->vdev_spa, 1802 vd, NULL, prev_state, 0); 1803 } 1804 1805 if (vd->vdev_spa->spa_load_state == SPA_LOAD_IMPORT && 1806 vd->vdev_ops->vdev_op_leaf) 1807 vd->vdev_not_present = 1; 1808 } 1809 1810 if (isopen) 1811 return; 1812 1813 if (vd->vdev_parent != NULL) { 1814 int c; 1815 int degraded = 0, faulted = 0; 1816 int corrupted = 0; 1817 vdev_t *parent, *child; 1818 1819 parent = vd->vdev_parent; 1820 for (c = 0; c < parent->vdev_children; c++) { 1821 child = parent->vdev_child[c]; 1822 if (child->vdev_state <= VDEV_STATE_CANT_OPEN) 1823 faulted++; 1824 else if (child->vdev_state == VDEV_STATE_DEGRADED) 1825 degraded++; 1826 1827 if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA) 1828 corrupted++; 1829 } 1830 1831 vd->vdev_parent->vdev_ops->vdev_op_state_change( 1832 vd->vdev_parent, faulted, degraded); 1833 1834 /* 1835 * Root special: if this is a toplevel vdev that cannot be 1836 * opened due to corrupted metadata, then propagate the root 1837 * vdev's aux state as 'corrupt' rather than 'insufficient 1838 * replicas'. 1839 */ 1840 if (corrupted && vd == vd->vdev_top) 1841 vdev_set_state(vd->vdev_spa->spa_root_vdev, 1842 B_FALSE, VDEV_STATE_CANT_OPEN, 1843 VDEV_AUX_CORRUPT_DATA); 1844 } 1845 } 1846