1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "@(#)vdev.c 1.33 07/11/27 SMI" 28 29 #include <sys/zfs_context.h> 30 #include <sys/fm/fs/zfs.h> 31 #include <sys/spa.h> 32 #include <sys/spa_impl.h> 33 #include <sys/dmu.h> 34 #include <sys/dmu_tx.h> 35 #include <sys/vdev_impl.h> 36 #include <sys/uberblock_impl.h> 37 #include <sys/metaslab.h> 38 #include <sys/metaslab_impl.h> 39 #include <sys/space_map.h> 40 #include <sys/zio.h> 41 #include <sys/zap.h> 42 #include <sys/fs/zfs.h> 43 44 /* 45 * Virtual device management. 46 */ 47 48 static vdev_ops_t *vdev_ops_table[] = { 49 &vdev_root_ops, 50 &vdev_raidz_ops, 51 &vdev_mirror_ops, 52 &vdev_replacing_ops, 53 &vdev_spare_ops, 54 &vdev_disk_ops, 55 &vdev_file_ops, 56 &vdev_missing_ops, 57 NULL 58 }; 59 60 /* maximum scrub/resilver I/O queue */ 61 int zfs_scrub_limit = 70; 62 63 /* 64 * Given a vdev type, return the appropriate ops vector. 65 */ 66 static vdev_ops_t * 67 vdev_getops(const char *type) 68 { 69 vdev_ops_t *ops, **opspp; 70 71 for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++) 72 if (strcmp(ops->vdev_op_type, type) == 0) 73 break; 74 75 return (ops); 76 } 77 78 /* 79 * Default asize function: return the MAX of psize with the asize of 80 * all children. This is what's used by anything other than RAID-Z. 81 */ 82 uint64_t 83 vdev_default_asize(vdev_t *vd, uint64_t psize) 84 { 85 uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift); 86 uint64_t csize; 87 uint64_t c; 88 89 for (c = 0; c < vd->vdev_children; c++) { 90 csize = vdev_psize_to_asize(vd->vdev_child[c], psize); 91 asize = MAX(asize, csize); 92 } 93 94 return (asize); 95 } 96 97 /* 98 * Get the replaceable or attachable device size. 99 * If the parent is a mirror or raidz, the replaceable size is the minimum 100 * psize of all its children. For the rest, just return our own psize. 101 * 102 * e.g. 103 * psize rsize 104 * root - - 105 * mirror/raidz - - 106 * disk1 20g 20g 107 * disk2 40g 20g 108 * disk3 80g 80g 109 */ 110 uint64_t 111 vdev_get_rsize(vdev_t *vd) 112 { 113 vdev_t *pvd, *cvd; 114 uint64_t c, rsize; 115 116 pvd = vd->vdev_parent; 117 118 /* 119 * If our parent is NULL or the root, just return our own psize. 120 */ 121 if (pvd == NULL || pvd->vdev_parent == NULL) 122 return (vd->vdev_psize); 123 124 rsize = 0; 125 126 for (c = 0; c < pvd->vdev_children; c++) { 127 cvd = pvd->vdev_child[c]; 128 rsize = MIN(rsize - 1, cvd->vdev_psize - 1) + 1; 129 } 130 131 return (rsize); 132 } 133 134 vdev_t * 135 vdev_lookup_top(spa_t *spa, uint64_t vdev) 136 { 137 vdev_t *rvd = spa->spa_root_vdev; 138 139 ASSERT(spa_config_held(spa, RW_READER) || 140 curthread == spa->spa_scrub_thread); 141 142 if (vdev < rvd->vdev_children) 143 return (rvd->vdev_child[vdev]); 144 145 return (NULL); 146 } 147 148 vdev_t * 149 vdev_lookup_by_guid(vdev_t *vd, uint64_t guid) 150 { 151 int c; 152 vdev_t *mvd; 153 154 if (vd->vdev_guid == guid) 155 return (vd); 156 157 for (c = 0; c < vd->vdev_children; c++) 158 if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) != 159 NULL) 160 return (mvd); 161 162 return (NULL); 163 } 164 165 void 166 vdev_add_child(vdev_t *pvd, vdev_t *cvd) 167 { 168 size_t oldsize, newsize; 169 uint64_t id = cvd->vdev_id; 170 vdev_t **newchild; 171 172 ASSERT(spa_config_held(cvd->vdev_spa, RW_WRITER)); 173 ASSERT(cvd->vdev_parent == NULL); 174 175 cvd->vdev_parent = pvd; 176 177 if (pvd == NULL) 178 return; 179 180 ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL); 181 182 oldsize = pvd->vdev_children * sizeof (vdev_t *); 183 pvd->vdev_children = MAX(pvd->vdev_children, id + 1); 184 newsize = pvd->vdev_children * sizeof (vdev_t *); 185 186 newchild = kmem_zalloc(newsize, KM_SLEEP); 187 if (pvd->vdev_child != NULL) { 188 bcopy(pvd->vdev_child, newchild, oldsize); 189 kmem_free(pvd->vdev_child, oldsize); 190 } 191 192 pvd->vdev_child = newchild; 193 pvd->vdev_child[id] = cvd; 194 195 cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd); 196 ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL); 197 198 /* 199 * Walk up all ancestors to update guid sum. 200 */ 201 for (; pvd != NULL; pvd = pvd->vdev_parent) 202 pvd->vdev_guid_sum += cvd->vdev_guid_sum; 203 204 if (cvd->vdev_ops->vdev_op_leaf) 205 cvd->vdev_spa->spa_scrub_maxinflight += zfs_scrub_limit; 206 } 207 208 void 209 vdev_remove_child(vdev_t *pvd, vdev_t *cvd) 210 { 211 int c; 212 uint_t id = cvd->vdev_id; 213 214 ASSERT(cvd->vdev_parent == pvd); 215 216 if (pvd == NULL) 217 return; 218 219 ASSERT(id < pvd->vdev_children); 220 ASSERT(pvd->vdev_child[id] == cvd); 221 222 pvd->vdev_child[id] = NULL; 223 cvd->vdev_parent = NULL; 224 225 for (c = 0; c < pvd->vdev_children; c++) 226 if (pvd->vdev_child[c]) 227 break; 228 229 if (c == pvd->vdev_children) { 230 kmem_free(pvd->vdev_child, c * sizeof (vdev_t *)); 231 pvd->vdev_child = NULL; 232 pvd->vdev_children = 0; 233 } 234 235 /* 236 * Walk up all ancestors to update guid sum. 237 */ 238 for (; pvd != NULL; pvd = pvd->vdev_parent) 239 pvd->vdev_guid_sum -= cvd->vdev_guid_sum; 240 241 if (cvd->vdev_ops->vdev_op_leaf) 242 cvd->vdev_spa->spa_scrub_maxinflight -= zfs_scrub_limit; 243 } 244 245 /* 246 * Remove any holes in the child array. 247 */ 248 void 249 vdev_compact_children(vdev_t *pvd) 250 { 251 vdev_t **newchild, *cvd; 252 int oldc = pvd->vdev_children; 253 int newc, c; 254 255 ASSERT(spa_config_held(pvd->vdev_spa, RW_WRITER)); 256 257 for (c = newc = 0; c < oldc; c++) 258 if (pvd->vdev_child[c]) 259 newc++; 260 261 newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP); 262 263 for (c = newc = 0; c < oldc; c++) { 264 if ((cvd = pvd->vdev_child[c]) != NULL) { 265 newchild[newc] = cvd; 266 cvd->vdev_id = newc++; 267 } 268 } 269 270 kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *)); 271 pvd->vdev_child = newchild; 272 pvd->vdev_children = newc; 273 } 274 275 /* 276 * Allocate and minimally initialize a vdev_t. 277 */ 278 static vdev_t * 279 vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops) 280 { 281 vdev_t *vd; 282 283 vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP); 284 285 if (spa->spa_root_vdev == NULL) { 286 ASSERT(ops == &vdev_root_ops); 287 spa->spa_root_vdev = vd; 288 } 289 290 if (guid == 0) { 291 if (spa->spa_root_vdev == vd) { 292 /* 293 * The root vdev's guid will also be the pool guid, 294 * which must be unique among all pools. 295 */ 296 while (guid == 0 || spa_guid_exists(guid, 0)) 297 guid = spa_get_random(-1ULL); 298 } else { 299 /* 300 * Any other vdev's guid must be unique within the pool. 301 */ 302 while (guid == 0 || 303 spa_guid_exists(spa_guid(spa), guid)) 304 guid = spa_get_random(-1ULL); 305 } 306 ASSERT(!spa_guid_exists(spa_guid(spa), guid)); 307 } 308 309 vd->vdev_spa = spa; 310 vd->vdev_id = id; 311 vd->vdev_guid = guid; 312 vd->vdev_guid_sum = guid; 313 vd->vdev_ops = ops; 314 vd->vdev_state = VDEV_STATE_CLOSED; 315 316 mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL); 317 mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL); 318 space_map_create(&vd->vdev_dtl_map, 0, -1ULL, 0, &vd->vdev_dtl_lock); 319 space_map_create(&vd->vdev_dtl_scrub, 0, -1ULL, 0, &vd->vdev_dtl_lock); 320 txg_list_create(&vd->vdev_ms_list, 321 offsetof(struct metaslab, ms_txg_node)); 322 txg_list_create(&vd->vdev_dtl_list, 323 offsetof(struct vdev, vdev_dtl_node)); 324 vd->vdev_stat.vs_timestamp = gethrtime(); 325 vdev_queue_init(vd); 326 vdev_cache_init(vd); 327 328 return (vd); 329 } 330 331 /* 332 * Allocate a new vdev. The 'alloctype' is used to control whether we are 333 * creating a new vdev or loading an existing one - the behavior is slightly 334 * different for each case. 335 */ 336 int 337 vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id, 338 int alloctype) 339 { 340 vdev_ops_t *ops; 341 char *type; 342 uint64_t guid = 0, islog, nparity; 343 vdev_t *vd; 344 345 ASSERT(spa_config_held(spa, RW_WRITER)); 346 347 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0) 348 return (EINVAL); 349 350 if ((ops = vdev_getops(type)) == NULL) 351 return (EINVAL); 352 353 /* 354 * If this is a load, get the vdev guid from the nvlist. 355 * Otherwise, vdev_alloc_common() will generate one for us. 356 */ 357 if (alloctype == VDEV_ALLOC_LOAD) { 358 uint64_t label_id; 359 360 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) || 361 label_id != id) 362 return (EINVAL); 363 364 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 365 return (EINVAL); 366 } else if (alloctype == VDEV_ALLOC_SPARE) { 367 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 368 return (EINVAL); 369 } else if (alloctype == VDEV_ALLOC_L2CACHE) { 370 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 371 return (EINVAL); 372 } 373 374 /* 375 * The first allocated vdev must be of type 'root'. 376 */ 377 if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL) 378 return (EINVAL); 379 380 /* 381 * Determine whether we're a log vdev. 382 */ 383 islog = 0; 384 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog); 385 if (islog && spa_version(spa) < SPA_VERSION_SLOGS) 386 return (ENOTSUP); 387 388 /* 389 * Set the nparity property for RAID-Z vdevs. 390 */ 391 nparity = -1ULL; 392 if (ops == &vdev_raidz_ops) { 393 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 394 &nparity) == 0) { 395 /* 396 * Currently, we can only support 2 parity devices. 397 */ 398 if (nparity == 0 || nparity > 2) 399 return (EINVAL); 400 /* 401 * Older versions can only support 1 parity device. 402 */ 403 if (nparity == 2 && 404 spa_version(spa) < SPA_VERSION_RAID6) 405 return (ENOTSUP); 406 } else { 407 /* 408 * We require the parity to be specified for SPAs that 409 * support multiple parity levels. 410 */ 411 if (spa_version(spa) >= SPA_VERSION_RAID6) 412 return (EINVAL); 413 /* 414 * Otherwise, we default to 1 parity device for RAID-Z. 415 */ 416 nparity = 1; 417 } 418 } else { 419 nparity = 0; 420 } 421 ASSERT(nparity != -1ULL); 422 423 vd = vdev_alloc_common(spa, id, guid, ops); 424 425 vd->vdev_islog = islog; 426 vd->vdev_nparity = nparity; 427 428 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0) 429 vd->vdev_path = spa_strdup(vd->vdev_path); 430 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0) 431 vd->vdev_devid = spa_strdup(vd->vdev_devid); 432 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH, 433 &vd->vdev_physpath) == 0) 434 vd->vdev_physpath = spa_strdup(vd->vdev_physpath); 435 436 /* 437 * Set the whole_disk property. If it's not specified, leave the value 438 * as -1. 439 */ 440 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 441 &vd->vdev_wholedisk) != 0) 442 vd->vdev_wholedisk = -1ULL; 443 444 /* 445 * Look for the 'not present' flag. This will only be set if the device 446 * was not present at the time of import. 447 */ 448 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 449 &vd->vdev_not_present); 450 451 /* 452 * Get the alignment requirement. 453 */ 454 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift); 455 456 /* 457 * If we're a top-level vdev, try to load the allocation parameters. 458 */ 459 if (parent && !parent->vdev_parent && alloctype == VDEV_ALLOC_LOAD) { 460 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY, 461 &vd->vdev_ms_array); 462 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT, 463 &vd->vdev_ms_shift); 464 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE, 465 &vd->vdev_asize); 466 } 467 468 /* 469 * If we're a leaf vdev, try to load the DTL object and other state. 470 */ 471 if (vd->vdev_ops->vdev_op_leaf && alloctype == VDEV_ALLOC_LOAD) { 472 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL, 473 &vd->vdev_dtl.smo_object); 474 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, 475 &vd->vdev_offline); 476 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE, 477 &vd->vdev_unspare); 478 /* 479 * When importing a pool, we want to ignore the persistent fault 480 * state, as the diagnosis made on another system may not be 481 * valid in the current context. 482 */ 483 if (spa->spa_load_state == SPA_LOAD_OPEN) { 484 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, 485 &vd->vdev_faulted); 486 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED, 487 &vd->vdev_degraded); 488 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, 489 &vd->vdev_removed); 490 } 491 } 492 493 /* 494 * Add ourselves to the parent's list of children. 495 */ 496 vdev_add_child(parent, vd); 497 498 *vdp = vd; 499 500 return (0); 501 } 502 503 void 504 vdev_free(vdev_t *vd) 505 { 506 int c; 507 spa_t *spa = vd->vdev_spa; 508 509 /* 510 * vdev_free() implies closing the vdev first. This is simpler than 511 * trying to ensure complicated semantics for all callers. 512 */ 513 vdev_close(vd); 514 515 516 ASSERT(!list_link_active(&vd->vdev_dirty_node)); 517 518 /* 519 * Free all children. 520 */ 521 for (c = 0; c < vd->vdev_children; c++) 522 vdev_free(vd->vdev_child[c]); 523 524 ASSERT(vd->vdev_child == NULL); 525 ASSERT(vd->vdev_guid_sum == vd->vdev_guid); 526 527 /* 528 * Discard allocation state. 529 */ 530 if (vd == vd->vdev_top) 531 vdev_metaslab_fini(vd); 532 533 ASSERT3U(vd->vdev_stat.vs_space, ==, 0); 534 ASSERT3U(vd->vdev_stat.vs_dspace, ==, 0); 535 ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0); 536 537 /* 538 * Remove this vdev from its parent's child list. 539 */ 540 vdev_remove_child(vd->vdev_parent, vd); 541 542 ASSERT(vd->vdev_parent == NULL); 543 544 /* 545 * Clean up vdev structure. 546 */ 547 vdev_queue_fini(vd); 548 vdev_cache_fini(vd); 549 550 if (vd->vdev_path) 551 spa_strfree(vd->vdev_path); 552 if (vd->vdev_devid) 553 spa_strfree(vd->vdev_devid); 554 if (vd->vdev_physpath) 555 spa_strfree(vd->vdev_physpath); 556 557 if (vd->vdev_isspare) 558 spa_spare_remove(vd); 559 if (vd->vdev_isl2cache) 560 spa_l2cache_remove(vd); 561 562 txg_list_destroy(&vd->vdev_ms_list); 563 txg_list_destroy(&vd->vdev_dtl_list); 564 mutex_enter(&vd->vdev_dtl_lock); 565 space_map_unload(&vd->vdev_dtl_map); 566 space_map_destroy(&vd->vdev_dtl_map); 567 space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL); 568 space_map_destroy(&vd->vdev_dtl_scrub); 569 mutex_exit(&vd->vdev_dtl_lock); 570 mutex_destroy(&vd->vdev_dtl_lock); 571 mutex_destroy(&vd->vdev_stat_lock); 572 573 if (vd == spa->spa_root_vdev) 574 spa->spa_root_vdev = NULL; 575 576 kmem_free(vd, sizeof (vdev_t)); 577 } 578 579 /* 580 * Transfer top-level vdev state from svd to tvd. 581 */ 582 static void 583 vdev_top_transfer(vdev_t *svd, vdev_t *tvd) 584 { 585 spa_t *spa = svd->vdev_spa; 586 metaslab_t *msp; 587 vdev_t *vd; 588 int t; 589 590 ASSERT(tvd == tvd->vdev_top); 591 592 tvd->vdev_ms_array = svd->vdev_ms_array; 593 tvd->vdev_ms_shift = svd->vdev_ms_shift; 594 tvd->vdev_ms_count = svd->vdev_ms_count; 595 596 svd->vdev_ms_array = 0; 597 svd->vdev_ms_shift = 0; 598 svd->vdev_ms_count = 0; 599 600 tvd->vdev_mg = svd->vdev_mg; 601 tvd->vdev_ms = svd->vdev_ms; 602 603 svd->vdev_mg = NULL; 604 svd->vdev_ms = NULL; 605 606 if (tvd->vdev_mg != NULL) 607 tvd->vdev_mg->mg_vd = tvd; 608 609 tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc; 610 tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space; 611 tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace; 612 613 svd->vdev_stat.vs_alloc = 0; 614 svd->vdev_stat.vs_space = 0; 615 svd->vdev_stat.vs_dspace = 0; 616 617 for (t = 0; t < TXG_SIZE; t++) { 618 while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL) 619 (void) txg_list_add(&tvd->vdev_ms_list, msp, t); 620 while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL) 621 (void) txg_list_add(&tvd->vdev_dtl_list, vd, t); 622 if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t)) 623 (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t); 624 } 625 626 if (list_link_active(&svd->vdev_dirty_node)) { 627 vdev_config_clean(svd); 628 vdev_config_dirty(tvd); 629 } 630 631 tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio; 632 svd->vdev_deflate_ratio = 0; 633 634 tvd->vdev_islog = svd->vdev_islog; 635 svd->vdev_islog = 0; 636 } 637 638 static void 639 vdev_top_update(vdev_t *tvd, vdev_t *vd) 640 { 641 int c; 642 643 if (vd == NULL) 644 return; 645 646 vd->vdev_top = tvd; 647 648 for (c = 0; c < vd->vdev_children; c++) 649 vdev_top_update(tvd, vd->vdev_child[c]); 650 } 651 652 /* 653 * Add a mirror/replacing vdev above an existing vdev. 654 */ 655 vdev_t * 656 vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops) 657 { 658 spa_t *spa = cvd->vdev_spa; 659 vdev_t *pvd = cvd->vdev_parent; 660 vdev_t *mvd; 661 662 ASSERT(spa_config_held(spa, RW_WRITER)); 663 664 mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops); 665 666 mvd->vdev_asize = cvd->vdev_asize; 667 mvd->vdev_ashift = cvd->vdev_ashift; 668 mvd->vdev_state = cvd->vdev_state; 669 670 vdev_remove_child(pvd, cvd); 671 vdev_add_child(pvd, mvd); 672 cvd->vdev_id = mvd->vdev_children; 673 vdev_add_child(mvd, cvd); 674 vdev_top_update(cvd->vdev_top, cvd->vdev_top); 675 676 if (mvd == mvd->vdev_top) 677 vdev_top_transfer(cvd, mvd); 678 679 return (mvd); 680 } 681 682 /* 683 * Remove a 1-way mirror/replacing vdev from the tree. 684 */ 685 void 686 vdev_remove_parent(vdev_t *cvd) 687 { 688 vdev_t *mvd = cvd->vdev_parent; 689 vdev_t *pvd = mvd->vdev_parent; 690 691 ASSERT(spa_config_held(cvd->vdev_spa, RW_WRITER)); 692 693 ASSERT(mvd->vdev_children == 1); 694 ASSERT(mvd->vdev_ops == &vdev_mirror_ops || 695 mvd->vdev_ops == &vdev_replacing_ops || 696 mvd->vdev_ops == &vdev_spare_ops); 697 cvd->vdev_ashift = mvd->vdev_ashift; 698 699 vdev_remove_child(mvd, cvd); 700 vdev_remove_child(pvd, mvd); 701 cvd->vdev_id = mvd->vdev_id; 702 vdev_add_child(pvd, cvd); 703 /* 704 * If we created a new toplevel vdev, then we need to change the child's 705 * vdev GUID to match the old toplevel vdev. Otherwise, we could have 706 * detached an offline device, and when we go to import the pool we'll 707 * think we have two toplevel vdevs, instead of a different version of 708 * the same toplevel vdev. 709 */ 710 if (cvd->vdev_top == cvd) { 711 pvd->vdev_guid_sum -= cvd->vdev_guid; 712 cvd->vdev_guid_sum -= cvd->vdev_guid; 713 cvd->vdev_guid = mvd->vdev_guid; 714 cvd->vdev_guid_sum += mvd->vdev_guid; 715 pvd->vdev_guid_sum += cvd->vdev_guid; 716 } 717 vdev_top_update(cvd->vdev_top, cvd->vdev_top); 718 719 if (cvd == cvd->vdev_top) 720 vdev_top_transfer(mvd, cvd); 721 722 ASSERT(mvd->vdev_children == 0); 723 vdev_free(mvd); 724 } 725 726 int 727 vdev_metaslab_init(vdev_t *vd, uint64_t txg) 728 { 729 spa_t *spa = vd->vdev_spa; 730 objset_t *mos = spa->spa_meta_objset; 731 metaslab_class_t *mc; 732 uint64_t m; 733 uint64_t oldc = vd->vdev_ms_count; 734 uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift; 735 metaslab_t **mspp; 736 int error; 737 738 if (vd->vdev_ms_shift == 0) /* not being allocated from yet */ 739 return (0); 740 741 dprintf("%s oldc %llu newc %llu\n", vdev_description(vd), oldc, newc); 742 743 ASSERT(oldc <= newc); 744 745 if (vd->vdev_islog) 746 mc = spa->spa_log_class; 747 else 748 mc = spa->spa_normal_class; 749 750 if (vd->vdev_mg == NULL) 751 vd->vdev_mg = metaslab_group_create(mc, vd); 752 753 mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP); 754 755 if (oldc != 0) { 756 bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp)); 757 kmem_free(vd->vdev_ms, oldc * sizeof (*mspp)); 758 } 759 760 vd->vdev_ms = mspp; 761 vd->vdev_ms_count = newc; 762 763 for (m = oldc; m < newc; m++) { 764 space_map_obj_t smo = { 0, 0, 0 }; 765 if (txg == 0) { 766 uint64_t object = 0; 767 error = dmu_read(mos, vd->vdev_ms_array, 768 m * sizeof (uint64_t), sizeof (uint64_t), &object); 769 if (error) 770 return (error); 771 if (object != 0) { 772 dmu_buf_t *db; 773 error = dmu_bonus_hold(mos, object, FTAG, &db); 774 if (error) 775 return (error); 776 ASSERT3U(db->db_size, >=, sizeof (smo)); 777 bcopy(db->db_data, &smo, sizeof (smo)); 778 ASSERT3U(smo.smo_object, ==, object); 779 dmu_buf_rele(db, FTAG); 780 } 781 } 782 vd->vdev_ms[m] = metaslab_init(vd->vdev_mg, &smo, 783 m << vd->vdev_ms_shift, 1ULL << vd->vdev_ms_shift, txg); 784 } 785 786 return (0); 787 } 788 789 void 790 vdev_metaslab_fini(vdev_t *vd) 791 { 792 uint64_t m; 793 uint64_t count = vd->vdev_ms_count; 794 795 if (vd->vdev_ms != NULL) { 796 for (m = 0; m < count; m++) 797 if (vd->vdev_ms[m] != NULL) 798 metaslab_fini(vd->vdev_ms[m]); 799 kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *)); 800 vd->vdev_ms = NULL; 801 } 802 } 803 804 int 805 vdev_probe(vdev_t *vd) 806 { 807 if (vd == NULL) 808 return (EINVAL); 809 810 /* 811 * Right now we only support status checks on the leaf vdevs. 812 */ 813 if (vd->vdev_ops->vdev_op_leaf) 814 return (vd->vdev_ops->vdev_op_probe(vd)); 815 816 return (0); 817 } 818 819 /* 820 * Prepare a virtual device for access. 821 */ 822 int 823 vdev_open(vdev_t *vd) 824 { 825 int error; 826 int c; 827 uint64_t osize = 0; 828 uint64_t asize, psize; 829 uint64_t ashift = 0; 830 831 ASSERT(vd->vdev_state == VDEV_STATE_CLOSED || 832 vd->vdev_state == VDEV_STATE_CANT_OPEN || 833 vd->vdev_state == VDEV_STATE_OFFLINE); 834 835 if (vd->vdev_fault_mode == VDEV_FAULT_COUNT) 836 vd->vdev_fault_arg >>= 1; 837 else 838 vd->vdev_fault_mode = VDEV_FAULT_NONE; 839 840 vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 841 842 if (!vd->vdev_removed && vd->vdev_faulted) { 843 ASSERT(vd->vdev_children == 0); 844 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 845 VDEV_AUX_ERR_EXCEEDED); 846 return (ENXIO); 847 } else if (vd->vdev_offline) { 848 ASSERT(vd->vdev_children == 0); 849 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE); 850 return (ENXIO); 851 } 852 853 error = vd->vdev_ops->vdev_op_open(vd, &osize, &ashift); 854 855 if (zio_injection_enabled && error == 0) 856 error = zio_handle_device_injection(vd, ENXIO); 857 858 if (error) { 859 if (vd->vdev_removed && 860 vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED) 861 vd->vdev_removed = B_FALSE; 862 863 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 864 vd->vdev_stat.vs_aux); 865 return (error); 866 } 867 868 vd->vdev_removed = B_FALSE; 869 870 if (vd->vdev_degraded) { 871 ASSERT(vd->vdev_children == 0); 872 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 873 VDEV_AUX_ERR_EXCEEDED); 874 } else { 875 vd->vdev_state = VDEV_STATE_HEALTHY; 876 } 877 878 for (c = 0; c < vd->vdev_children; c++) 879 if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) { 880 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 881 VDEV_AUX_NONE); 882 break; 883 } 884 885 osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t)); 886 887 if (vd->vdev_children == 0) { 888 if (osize < SPA_MINDEVSIZE) { 889 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 890 VDEV_AUX_TOO_SMALL); 891 return (EOVERFLOW); 892 } 893 psize = osize; 894 asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE); 895 } else { 896 if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE - 897 (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) { 898 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 899 VDEV_AUX_TOO_SMALL); 900 return (EOVERFLOW); 901 } 902 psize = 0; 903 asize = osize; 904 } 905 906 vd->vdev_psize = psize; 907 908 if (vd->vdev_asize == 0) { 909 /* 910 * This is the first-ever open, so use the computed values. 911 * For testing purposes, a higher ashift can be requested. 912 */ 913 vd->vdev_asize = asize; 914 vd->vdev_ashift = MAX(ashift, vd->vdev_ashift); 915 } else { 916 /* 917 * Make sure the alignment requirement hasn't increased. 918 */ 919 if (ashift > vd->vdev_top->vdev_ashift) { 920 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 921 VDEV_AUX_BAD_LABEL); 922 return (EINVAL); 923 } 924 925 /* 926 * Make sure the device hasn't shrunk. 927 */ 928 if (asize < vd->vdev_asize) { 929 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 930 VDEV_AUX_BAD_LABEL); 931 return (EINVAL); 932 } 933 934 /* 935 * If all children are healthy and the asize has increased, 936 * then we've experienced dynamic LUN growth. 937 */ 938 if (vd->vdev_state == VDEV_STATE_HEALTHY && 939 asize > vd->vdev_asize) { 940 vd->vdev_asize = asize; 941 } 942 } 943 944 /* 945 * Ensure we can issue some IO before declaring the 946 * vdev open for business. 947 */ 948 error = vdev_probe(vd); 949 if (error) { 950 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 951 VDEV_AUX_OPEN_FAILED); 952 return (error); 953 } 954 955 /* 956 * If this is a top-level vdev, compute the raidz-deflation 957 * ratio. Note, we hard-code in 128k (1<<17) because it is the 958 * current "typical" blocksize. Even if SPA_MAXBLOCKSIZE 959 * changes, this algorithm must never change, or we will 960 * inconsistently account for existing bp's. 961 */ 962 if (vd->vdev_top == vd) { 963 vd->vdev_deflate_ratio = (1<<17) / 964 (vdev_psize_to_asize(vd, 1<<17) >> SPA_MINBLOCKSHIFT); 965 } 966 967 /* 968 * This allows the ZFS DE to close cases appropriately. If a device 969 * goes away and later returns, we want to close the associated case. 970 * But it's not enough to simply post this only when a device goes from 971 * CANT_OPEN -> HEALTHY. If we reboot the system and the device is 972 * back, we also need to close the case (otherwise we will try to replay 973 * it). So we have to post this notifier every time. Since this only 974 * occurs during pool open or error recovery, this should not be an 975 * issue. 976 */ 977 zfs_post_ok(vd->vdev_spa, vd); 978 979 return (0); 980 } 981 982 /* 983 * Called once the vdevs are all opened, this routine validates the label 984 * contents. This needs to be done before vdev_load() so that we don't 985 * inadvertently do repair I/Os to the wrong device. 986 * 987 * This function will only return failure if one of the vdevs indicates that it 988 * has since been destroyed or exported. This is only possible if 989 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state 990 * will be updated but the function will return 0. 991 */ 992 int 993 vdev_validate(vdev_t *vd) 994 { 995 spa_t *spa = vd->vdev_spa; 996 int c; 997 nvlist_t *label; 998 uint64_t guid; 999 uint64_t state; 1000 1001 for (c = 0; c < vd->vdev_children; c++) 1002 if (vdev_validate(vd->vdev_child[c]) != 0) 1003 return (EBADF); 1004 1005 /* 1006 * If the device has already failed, or was marked offline, don't do 1007 * any further validation. Otherwise, label I/O will fail and we will 1008 * overwrite the previous state. 1009 */ 1010 if (vd->vdev_ops->vdev_op_leaf && !vdev_is_dead(vd)) { 1011 1012 if ((label = vdev_label_read_config(vd)) == NULL) { 1013 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1014 VDEV_AUX_BAD_LABEL); 1015 return (0); 1016 } 1017 1018 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, 1019 &guid) != 0 || guid != spa_guid(spa)) { 1020 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1021 VDEV_AUX_CORRUPT_DATA); 1022 nvlist_free(label); 1023 return (0); 1024 } 1025 1026 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, 1027 &guid) != 0 || guid != vd->vdev_guid) { 1028 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1029 VDEV_AUX_CORRUPT_DATA); 1030 nvlist_free(label); 1031 return (0); 1032 } 1033 1034 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, 1035 &state) != 0) { 1036 vdev_set_state