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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright (c) 2011, 2018 by Delphix. All rights reserved. 25 * Copyright 2017 Nexenta Systems, Inc. 26 * Copyright (c) 2014 Integros [integros.com] 27 * Copyright 2016 Toomas Soome <tsoome@me.com> 28 * Copyright 2017 Joyent, Inc. 29 */ 30 31 #include <sys/zfs_context.h> 32 #include <sys/fm/fs/zfs.h> 33 #include <sys/spa.h> 34 #include <sys/spa_impl.h> 35 #include <sys/bpobj.h> 36 #include <sys/dmu.h> 37 #include <sys/dmu_tx.h> 38 #include <sys/dsl_dir.h> 39 #include <sys/vdev_impl.h> 40 #include <sys/uberblock_impl.h> 41 #include <sys/metaslab.h> 42 #include <sys/metaslab_impl.h> 43 #include <sys/space_map.h> 44 #include <sys/space_reftree.h> 45 #include <sys/zio.h> 46 #include <sys/zap.h> 47 #include <sys/fs/zfs.h> 48 #include <sys/arc.h> 49 #include <sys/zil.h> 50 #include <sys/dsl_scan.h> 51 #include <sys/abd.h> 52 53 /* 54 * Virtual device management. 55 */ 56 57 static vdev_ops_t *vdev_ops_table[] = { 58 &vdev_root_ops, 59 &vdev_raidz_ops, 60 &vdev_mirror_ops, 61 &vdev_replacing_ops, 62 &vdev_spare_ops, 63 &vdev_disk_ops, 64 &vdev_file_ops, 65 &vdev_missing_ops, 66 &vdev_hole_ops, 67 &vdev_indirect_ops, 68 NULL 69 }; 70 71 /* maximum scrub/resilver I/O queue per leaf vdev */ 72 int zfs_scrub_limit = 10; 73 74 /* 75 * When a vdev is added, it will be divided into approximately (but no 76 * more than) this number of metaslabs. 77 */ 78 int metaslabs_per_vdev = 200; 79 80 boolean_t vdev_validate_skip = B_FALSE; 81 82 /*PRINTFLIKE2*/ 83 void 84 vdev_dbgmsg(vdev_t *vd, const char *fmt, ...) 85 { 86 va_list adx; 87 char buf[256]; 88 89 va_start(adx, fmt); 90 (void) vsnprintf(buf, sizeof (buf), fmt, adx); 91 va_end(adx); 92 93 if (vd->vdev_path != NULL) { 94 zfs_dbgmsg("%s vdev '%s': %s", vd->vdev_ops->vdev_op_type, 95 vd->vdev_path, buf); 96 } else { 97 zfs_dbgmsg("%s-%llu vdev (guid %llu): %s", 98 vd->vdev_ops->vdev_op_type, 99 (u_longlong_t)vd->vdev_id, 100 (u_longlong_t)vd->vdev_guid, buf); 101 } 102 } 103 104 void 105 vdev_dbgmsg_print_tree(vdev_t *vd, int indent) 106 { 107 char state[20]; 108 109 if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) { 110 zfs_dbgmsg("%*svdev %u: %s", indent, "", vd->vdev_id, 111 vd->vdev_ops->vdev_op_type); 112 return; 113 } 114 115 switch (vd->vdev_state) { 116 case VDEV_STATE_UNKNOWN: 117 (void) snprintf(state, sizeof (state), "unknown"); 118 break; 119 case VDEV_STATE_CLOSED: 120 (void) snprintf(state, sizeof (state), "closed"); 121 break; 122 case VDEV_STATE_OFFLINE: 123 (void) snprintf(state, sizeof (state), "offline"); 124 break; 125 case VDEV_STATE_REMOVED: 126 (void) snprintf(state, sizeof (state), "removed"); 127 break; 128 case VDEV_STATE_CANT_OPEN: 129 (void) snprintf(state, sizeof (state), "can't open"); 130 break; 131 case VDEV_STATE_FAULTED: 132 (void) snprintf(state, sizeof (state), "faulted"); 133 break; 134 case VDEV_STATE_DEGRADED: 135 (void) snprintf(state, sizeof (state), "degraded"); 136 break; 137 case VDEV_STATE_HEALTHY: 138 (void) snprintf(state, sizeof (state), "healthy"); 139 break; 140 default: 141 (void) snprintf(state, sizeof (state), "<state %u>", 142 (uint_t)vd->vdev_state); 143 } 144 145 zfs_dbgmsg("%*svdev %u: %s%s, guid: %llu, path: %s, %s", indent, 146 "", vd->vdev_id, vd->vdev_ops->vdev_op_type, 147 vd->vdev_islog ? " (log)" : "", 148 (u_longlong_t)vd->vdev_guid, 149 vd->vdev_path ? vd->vdev_path : "N/A", state); 150 151 for (uint64_t i = 0; i < vd->vdev_children; i++) 152 vdev_dbgmsg_print_tree(vd->vdev_child[i], indent + 2); 153 } 154 155 /* 156 * Given a vdev type, return the appropriate ops vector. 157 */ 158 static vdev_ops_t * 159 vdev_getops(const char *type) 160 { 161 vdev_ops_t *ops, **opspp; 162 163 for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++) 164 if (strcmp(ops->vdev_op_type, type) == 0) 165 break; 166 167 return (ops); 168 } 169 170 /* 171 * Default asize function: return the MAX of psize with the asize of 172 * all children. This is what's used by anything other than RAID-Z. 173 */ 174 uint64_t 175 vdev_default_asize(vdev_t *vd, uint64_t psize) 176 { 177 uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift); 178 uint64_t csize; 179 180 for (int c = 0; c < vd->vdev_children; c++) { 181 csize = vdev_psize_to_asize(vd->vdev_child[c], psize); 182 asize = MAX(asize, csize); 183 } 184 185 return (asize); 186 } 187 188 /* 189 * Get the minimum allocatable size. We define the allocatable size as 190 * the vdev's asize rounded to the nearest metaslab. This allows us to 191 * replace or attach devices which don't have the same physical size but 192 * can still satisfy the same number of allocations. 193 */ 194 uint64_t 195 vdev_get_min_asize(vdev_t *vd) 196 { 197 vdev_t *pvd = vd->vdev_parent; 198 199 /* 200 * If our parent is NULL (inactive spare or cache) or is the root, 201 * just return our own asize. 202 */ 203 if (pvd == NULL) 204 return (vd->vdev_asize); 205 206 /* 207 * The top-level vdev just returns the allocatable size rounded 208 * to the nearest metaslab. 209 */ 210 if (vd == vd->vdev_top) 211 return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift)); 212 213 /* 214 * The allocatable space for a raidz vdev is N * sizeof(smallest child), 215 * so each child must provide at least 1/Nth of its asize. 216 */ 217 if (pvd->vdev_ops == &vdev_raidz_ops) 218 return ((pvd->vdev_min_asize + pvd->vdev_children - 1) / 219 pvd->vdev_children); 220 221 return (pvd->vdev_min_asize); 222 } 223 224 void 225 vdev_set_min_asize(vdev_t *vd) 226 { 227 vd->vdev_min_asize = vdev_get_min_asize(vd); 228 229 for (int c = 0; c < vd->vdev_children; c++) 230 vdev_set_min_asize(vd->vdev_child[c]); 231 } 232 233 vdev_t * 234 vdev_lookup_top(spa_t *spa, uint64_t vdev) 235 { 236 vdev_t *rvd = spa->spa_root_vdev; 237 238 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 239 240 if (vdev < rvd->vdev_children) { 241 ASSERT(rvd->vdev_child[vdev] != NULL); 242 return (rvd->vdev_child[vdev]); 243 } 244 245 return (NULL); 246 } 247 248 vdev_t * 249 vdev_lookup_by_guid(vdev_t *vd, uint64_t guid) 250 { 251 vdev_t *mvd; 252 253 if (vd->vdev_guid == guid) 254 return (vd); 255 256 for (int c = 0; c < vd->vdev_children; c++) 257 if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) != 258 NULL) 259 return (mvd); 260 261 return (NULL); 262 } 263 264 static int 265 vdev_count_leaves_impl(vdev_t *vd) 266 { 267 int n = 0; 268 269 if (vd->vdev_ops->vdev_op_leaf) 270 return (1); 271 272 for (int c = 0; c < vd->vdev_children; c++) 273 n += vdev_count_leaves_impl(vd->vdev_child[c]); 274 275 return (n); 276 } 277 278 int 279 vdev_count_leaves(spa_t *spa) 280 { 281 return (vdev_count_leaves_impl(spa->spa_root_vdev)); 282 } 283 284 void 285 vdev_add_child(vdev_t *pvd, vdev_t *cvd) 286 { 287 size_t oldsize, newsize; 288 uint64_t id = cvd->vdev_id; 289 vdev_t **newchild; 290 spa_t *spa = cvd->vdev_spa; 291 292 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 293 ASSERT(cvd->vdev_parent == NULL); 294 295 cvd->vdev_parent = pvd; 296 297 if (pvd == NULL) 298 return; 299 300 ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL); 301 302 oldsize = pvd->vdev_children * sizeof (vdev_t *); 303 pvd->vdev_children = MAX(pvd->vdev_children, id + 1); 304 newsize = pvd->vdev_children * sizeof (vdev_t *); 305 306 newchild = kmem_zalloc(newsize, KM_SLEEP); 307 if (pvd->vdev_child != NULL) { 308 bcopy(pvd->vdev_child, newchild, oldsize); 309 kmem_free(pvd->vdev_child, oldsize); 310 } 311 312 pvd->vdev_child = newchild; 313 pvd->vdev_child[id] = cvd; 314 315 cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd); 316 ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL); 317 318 /* 319 * Walk up all ancestors to update guid sum. 320 */ 321 for (; pvd != NULL; pvd = pvd->vdev_parent) 322 pvd->vdev_guid_sum += cvd->vdev_guid_sum; 323 } 324 325 void 326 vdev_remove_child(vdev_t *pvd, vdev_t *cvd) 327 { 328 int c; 329 uint_t id = cvd->vdev_id; 330 331 ASSERT(cvd->vdev_parent == pvd); 332 333 if (pvd == NULL) 334 return; 335 336 ASSERT(id < pvd->vdev_children); 337 ASSERT(pvd->vdev_child[id] == cvd); 338 339 pvd->vdev_child[id] = NULL; 340 cvd->vdev_parent = NULL; 341 342 for (c = 0; c < pvd->vdev_children; c++) 343 if (pvd->vdev_child[c]) 344 break; 345 346 if (c == pvd->vdev_children) { 347 kmem_free(pvd->vdev_child, c * sizeof (vdev_t *)); 348 pvd->vdev_child = NULL; 349 pvd->vdev_children = 0; 350 } 351 352 /* 353 * Walk up all ancestors to update guid sum. 354 */ 355 for (; pvd != NULL; pvd = pvd->vdev_parent) 356 pvd->vdev_guid_sum -= cvd->vdev_guid_sum; 357 } 358 359 /* 360 * Remove any holes in the child array. 361 */ 362 void 363 vdev_compact_children(vdev_t *pvd) 364 { 365 vdev_t **newchild, *cvd; 366 int oldc = pvd->vdev_children; 367 int newc; 368 369 ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 370 371 for (int c = newc = 0; c < oldc; c++) 372 if (pvd->vdev_child[c]) 373 newc++; 374 375 newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP); 376 377 for (int c = newc = 0; c < oldc; c++) { 378 if ((cvd = pvd->vdev_child[c]) != NULL) { 379 newchild[newc] = cvd; 380 cvd->vdev_id = newc++; 381 } 382 } 383 384 kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *)); 385 pvd->vdev_child = newchild; 386 pvd->vdev_children = newc; 387 } 388 389 /* 390 * Allocate and minimally initialize a vdev_t. 391 */ 392 vdev_t * 393 vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops) 394 { 395 vdev_t *vd; 396 vdev_indirect_config_t *vic; 397 398 vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP); 399 vic = &vd->vdev_indirect_config; 400 401 if (spa->spa_root_vdev == NULL) { 402 ASSERT(ops == &vdev_root_ops); 403 spa->spa_root_vdev = vd; 404 spa->spa_load_guid = spa_generate_guid(NULL); 405 } 406 407 if (guid == 0 && ops != &vdev_hole_ops) { 408 if (spa->spa_root_vdev == vd) { 409 /* 410 * The root vdev's guid will also be the pool guid, 411 * which must be unique among all pools. 412 */ 413 guid = spa_generate_guid(NULL); 414 } else { 415 /* 416 * Any other vdev's guid must be unique within the pool. 417 */ 418 guid = spa_generate_guid(spa); 419 } 420 ASSERT(!spa_guid_exists(spa_guid(spa), guid)); 421 } 422 423 vd->vdev_spa = spa; 424 vd->vdev_id = id; 425 vd->vdev_guid = guid; 426 vd->vdev_guid_sum = guid; 427 vd->vdev_ops = ops; 428 vd->vdev_state = VDEV_STATE_CLOSED; 429 vd->vdev_ishole = (ops == &vdev_hole_ops); 430 vic->vic_prev_indirect_vdev = UINT64_MAX; 431 432 rw_init(&vd->vdev_indirect_rwlock, NULL, RW_DEFAULT, NULL); 433 mutex_init(&vd->vdev_obsolete_lock, NULL, MUTEX_DEFAULT, NULL); 434 vd->vdev_obsolete_segments = range_tree_create(NULL, NULL); 435 436 mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL); 437 mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL); 438 mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL); 439 mutex_init(&vd->vdev_queue_lock, NULL, MUTEX_DEFAULT, NULL); 440 for (int t = 0; t < DTL_TYPES; t++) { 441 vd->vdev_dtl[t] = range_tree_create(NULL, NULL); 442 } 443 txg_list_create(&vd->vdev_ms_list, spa, 444 offsetof(struct metaslab, ms_txg_node)); 445 txg_list_create(&vd->vdev_dtl_list, spa, 446 offsetof(struct vdev, vdev_dtl_node)); 447 vd->vdev_stat.vs_timestamp = gethrtime(); 448 vdev_queue_init(vd); 449 vdev_cache_init(vd); 450 451 return (vd); 452 } 453 454 /* 455 * Allocate a new vdev. The 'alloctype' is used to control whether we are 456 * creating a new vdev or loading an existing one - the behavior is slightly 457 * different for each case. 458 */ 459 int 460 vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id, 461 int alloctype) 462 { 463 vdev_ops_t *ops; 464 char *type; 465 uint64_t guid = 0, islog, nparity; 466 vdev_t *vd; 467 vdev_indirect_config_t *vic; 468 469 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 470 471 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0) 472 return (SET_ERROR(EINVAL)); 473 474 if ((ops = vdev_getops(type)) == NULL) 475 return (SET_ERROR(EINVAL)); 476 477 /* 478 * If this is a load, get the vdev guid from the nvlist. 479 * Otherwise, vdev_alloc_common() will generate one for us. 480 */ 481 if (alloctype == VDEV_ALLOC_LOAD) { 482 uint64_t label_id; 483 484 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) || 485 label_id != id) 486 return (SET_ERROR(EINVAL)); 487 488 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 489 return (SET_ERROR(EINVAL)); 490 } else if (alloctype == VDEV_ALLOC_SPARE) { 491 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 492 return (SET_ERROR(EINVAL)); 493 } else if (alloctype == VDEV_ALLOC_L2CACHE) { 494 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 495 return (SET_ERROR(EINVAL)); 496 } else if (alloctype == VDEV_ALLOC_ROOTPOOL) { 497 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 498 return (SET_ERROR(EINVAL)); 499 } 500 501 /* 502 * The first allocated vdev must be of type 'root'. 503 */ 504 if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL) 505 return (SET_ERROR(EINVAL)); 506 507 /* 508 * Determine whether we're a log vdev. 509 */ 510 islog = 0; 511 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog); 512 if (islog && spa_version(spa) < SPA_VERSION_SLOGS) 513 return (SET_ERROR(ENOTSUP)); 514 515 if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES) 516 return (SET_ERROR(ENOTSUP)); 517 518 /* 519 * Set the nparity property for RAID-Z vdevs. 520 */ 521 nparity = -1ULL; 522 if (ops == &vdev_raidz_ops) { 523 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 524 &nparity) == 0) { 525 if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY) 526 return (SET_ERROR(EINVAL)); 527 /* 528 * Previous versions could only support 1 or 2 parity 529 * device. 530 */ 531 if (nparity > 1 && 532 spa_version(spa) < SPA_VERSION_RAIDZ2) 533 return (SET_ERROR(ENOTSUP)); 534 if (nparity > 2 && 535 spa_version(spa) < SPA_VERSION_RAIDZ3) 536 return (SET_ERROR(ENOTSUP)); 537 } else { 538 /* 539 * We require the parity to be specified for SPAs that 540 * support multiple parity levels. 541 */ 542 if (spa_version(spa) >= SPA_VERSION_RAIDZ2) 543 return (SET_ERROR(EINVAL)); 544 /* 545 * Otherwise, we default to 1 parity device for RAID-Z. 546 */ 547 nparity = 1; 548 } 549 } else { 550 nparity = 0; 551 } 552 ASSERT(nparity != -1ULL); 553 554 vd = vdev_alloc_common(spa, id, guid, ops); 555 vic = &vd->vdev_indirect_config; 556 557 vd->vdev_islog = islog; 558 vd->vdev_nparity = nparity; 559 560 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0) 561 vd->vdev_path = spa_strdup(vd->vdev_path); 562 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0) 563 vd->vdev_devid = spa_strdup(vd->vdev_devid); 564 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH, 565 &vd->vdev_physpath) == 0) 566 vd->vdev_physpath = spa_strdup(vd->vdev_physpath); 567 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0) 568 vd->vdev_fru = spa_strdup(vd->vdev_fru); 569 570 /* 571 * Set the whole_disk property. If it's not specified, leave the value 572 * as -1. 573 */ 574 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 575 &vd->vdev_wholedisk) != 0) 576 vd->vdev_wholedisk = -1ULL; 577 578 ASSERT0(vic->vic_mapping_object); 579 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_OBJECT, 580 &vic->vic_mapping_object); 581 ASSERT0(vic->vic_births_object); 582 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_BIRTHS, 583 &vic->vic_births_object); 584 ASSERT3U(vic->vic_prev_indirect_vdev, ==, UINT64_MAX); 585 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_PREV_INDIRECT_VDEV, 586 &vic->vic_prev_indirect_vdev); 587 588 /* 589 * Look for the 'not present' flag. This will only be set if the device 590 * was not present at the time of import. 591 */ 592 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 593 &vd->vdev_not_present); 594 595 /* 596 * Get the alignment requirement. 597 */ 598 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift); 599 600 /* 601 * Retrieve the vdev creation time. 602 */ 603 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG, 604 &vd->vdev_crtxg); 605 606 /* 607 * If we're a top-level vdev, try to load the allocation parameters. 608 */ 609 if (parent && !parent->vdev_parent && 610 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) { 611 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY, 612 &vd->vdev_ms_array); 613 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT, 614 &vd->vdev_ms_shift); 615 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE, 616 &vd->vdev_asize); 617 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING, 618 &vd->vdev_removing); 619 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP, 620 &vd->vdev_top_zap); 621 } else { 622 ASSERT0(vd->vdev_top_zap); 623 } 624 625 if (parent && !parent->vdev_parent && alloctype != VDEV_ALLOC_ATTACH) { 626 ASSERT(alloctype == VDEV_ALLOC_LOAD || 627 alloctype == VDEV_ALLOC_ADD || 628 alloctype == VDEV_ALLOC_SPLIT || 629 alloctype == VDEV_ALLOC_ROOTPOOL); 630 vd->vdev_mg = metaslab_group_create(islog ? 631 spa_log_class(spa) : spa_normal_class(spa), vd); 632 } 633 634 if (vd->vdev_ops->vdev_op_leaf && 635 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) { 636 (void) nvlist_lookup_uint64(nv, 637 ZPOOL_CONFIG_VDEV_LEAF_ZAP, &vd->vdev_leaf_zap); 638 } else { 639 ASSERT0(vd->vdev_leaf_zap); 640 } 641 642 /* 643 * If we're a leaf vdev, try to load the DTL object and other state. 644 */ 645 646 if (vd->vdev_ops->vdev_op_leaf && 647 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE || 648 alloctype == VDEV_ALLOC_ROOTPOOL)) { 649 if (alloctype == VDEV_ALLOC_LOAD) { 650 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL, 651 &vd->vdev_dtl_object); 652 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE, 653 &vd->vdev_unspare); 654 } 655 656 if (alloctype == VDEV_ALLOC_ROOTPOOL) { 657 uint64_t spare = 0; 658 659 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 660 &spare) == 0 && spare) 661 spa_spare_add(vd); 662 } 663 664 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, 665 &vd->vdev_offline); 666 667 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG, 668 &vd->vdev_resilver_txg); 669 670 /* 671 * When importing a pool, we want to ignore the persistent fault 672 * state, as the diagnosis made on another system may not be 673 * valid in the current context. Local vdevs will 674 * remain in the faulted state. 675 */ 676 if (spa_load_state(spa) == SPA_LOAD_OPEN) { 677 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, 678 &vd->vdev_faulted); 679 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED, 680 &vd->vdev_degraded); 681 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, 682 &vd->vdev_removed); 683 684 if (vd->vdev_faulted || vd->vdev_degraded) { 685 char *aux; 686 687 vd->vdev_label_aux = 688 VDEV_AUX_ERR_EXCEEDED; 689 if (nvlist_lookup_string(nv, 690 ZPOOL_CONFIG_AUX_STATE, &aux) == 0 && 691 strcmp(aux, "external") == 0) 692 vd->vdev_label_aux = VDEV_AUX_EXTERNAL; 693 } 694 } 695 } 696 697 /* 698 * Add ourselves to the parent's list of children. 699 */ 700 vdev_add_child(parent, vd); 701 702 *vdp = vd; 703 704 return (0); 705 } 706 707 void 708 vdev_free(vdev_t *vd) 709 { 710 spa_t *spa = vd->vdev_spa; 711 712 /* 713 * vdev_free() implies closing the vdev first. This is simpler than 714 * trying to ensure complicated semantics for all callers. 715 */ 716 vdev_close(vd); 717 718 ASSERT(!list_link_active(&vd->vdev_config_dirty_node)); 719 ASSERT(!list_link_active(&vd->vdev_state_dirty_node)); 720 721 /* 722 * Free all children. 723 */ 724 for (int c = 0; c < vd->vdev_children; c++) 725 vdev_free(vd->vdev_child[c]); 726 727 ASSERT(vd->vdev_child == NULL); 728 ASSERT(vd->vdev_guid_sum == vd->vdev_guid); 729 730 /* 731 * Discard allocation state. 732 */ 733 if (vd->vdev_mg != NULL) { 734 vdev_metaslab_fini(vd); 735 metaslab_group_destroy(vd->vdev_mg); 736 } 737 738 ASSERT0(vd->vdev_stat.vs_space); 739 ASSERT0(vd->vdev_stat.vs_dspace); 740 ASSERT0(vd->vdev_stat.vs_alloc); 741 742 /* 743 * Remove this vdev from its parent's child list. 744 */ 745 vdev_remove_child(vd->vdev_parent, vd); 746 747 ASSERT(vd->vdev_parent == NULL); 748 749 /* 750 * Clean up vdev structure. 751 */ 752 vdev_queue_fini(vd); 753 vdev_cache_fini(vd); 754 755 if (vd->vdev_path) 756 spa_strfree(vd->vdev_path); 757 if (vd->vdev_devid) 758 spa_strfree(vd->vdev_devid); 759 if (vd->vdev_physpath) 760 spa_strfree(vd->vdev_physpath); 761 if (vd->vdev_fru) 762 spa_strfree(vd->vdev_fru); 763 764 if (vd->vdev_isspare) 765 spa_spare_remove(vd); 766 if (vd->vdev_isl2cache) 767 spa_l2cache_remove(vd); 768 769 txg_list_destroy(&vd->vdev_ms_list); 770 txg_list_destroy(&vd->vdev_dtl_list); 771 772 mutex_enter(&vd->vdev_dtl_lock); 773 space_map_close(vd->vdev_dtl_sm); 774 for (int t = 0; t < DTL_TYPES; t++) { 775 range_tree_vacate(vd->vdev_dtl[t], NULL, NULL); 776 range_tree_destroy(vd->vdev_dtl[t]); 777 } 778 mutex_exit(&vd->vdev_dtl_lock); 779 780 EQUIV(vd->vdev_indirect_births != NULL, 781 vd->vdev_indirect_mapping != NULL); 782 if (vd->vdev_indirect_births != NULL) { 783 vdev_indirect_mapping_close(vd->vdev_indirect_mapping); 784 vdev_indirect_births_close(vd->vdev_indirect_births); 785 } 786 787 if (vd->vdev_obsolete_sm != NULL) { 788 ASSERT(vd->vdev_removing || 789 vd->vdev_ops == &vdev_indirect_ops); 790 space_map_close(vd->vdev_obsolete_sm); 791 vd->vdev_obsolete_sm = NULL; 792 } 793 range_tree_destroy(vd->vdev_obsolete_segments); 794 rw_destroy(&vd->vdev_indirect_rwlock); 795 mutex_destroy(&vd->vdev_obsolete_lock); 796 797 mutex_destroy(&vd->vdev_queue_lock); 798 mutex_destroy(&vd->vdev_dtl_lock); 799 mutex_destroy(&vd->vdev_stat_lock); 800 mutex_destroy(&vd->vdev_probe_lock); 801 802 if (vd == spa->spa_root_vdev) 803 spa->spa_root_vdev = NULL; 804 805 kmem_free(vd, sizeof (vdev_t)); 806 } 807 808 /* 809 * Transfer top-level vdev state from svd to tvd. 810 */ 811 static void 812 vdev_top_transfer(vdev_t *svd, vdev_t *tvd) 813 { 814 spa_t *spa = svd->vdev_spa; 815 metaslab_t *msp; 816 vdev_t *vd; 817 int t; 818 819 ASSERT(tvd == tvd->vdev_top); 820 821 tvd->vdev_ms_array = svd->vdev_ms_array; 822 tvd->vdev_ms_shift = svd->vdev_ms_shift; 823 tvd->vdev_ms_count = svd->vdev_ms_count; 824 tvd->vdev_top_zap = svd->vdev_top_zap; 825 826 svd->vdev_ms_array = 0; 827 svd->vdev_ms_shift = 0; 828 svd->vdev_ms_count = 0; 829 svd->vdev_top_zap = 0; 830 831 if (tvd->vdev_mg) 832 ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg); 833 tvd->vdev_mg = svd->vdev_mg; 834 tvd->vdev_ms = svd->vdev_ms; 835 836 svd->vdev_mg = NULL; 837 svd->vdev_ms = NULL; 838 839 if (tvd->vdev_mg != NULL) 840 tvd->vdev_mg->mg_vd = tvd; 841 842 tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc; 843 tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space; 844 tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace; 845 846 svd->vdev_stat.vs_alloc = 0; 847 svd->vdev_stat.vs_space = 0; 848 svd->vdev_stat.vs_dspace = 0; 849 850 for (t = 0; t < TXG_SIZE; t++) { 851 while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL) 852 (void) txg_list_add(&tvd->vdev_ms_list, msp, t); 853 while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL) 854 (void) txg_list_add(&tvd->vdev_dtl_list, vd, t); 855 if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t)) 856 (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t); 857 } 858 859 if (list_link_active(&svd->vdev_config_dirty_node)) { 860 vdev_config_clean(svd); 861 vdev_config_dirty(tvd); 862 } 863 864 if (list_link_active(&svd->vdev_state_dirty_node)) { 865 vdev_state_clean(svd); 866 vdev_state_dirty(tvd); 867 } 868 869 tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio; 870 svd->vdev_deflate_ratio = 0; 871 872 tvd->vdev_islog = svd->vdev_islog; 873 svd->vdev_islog = 0; 874 } 875 876 static void 877 vdev_top_update(vdev_t *tvd, vdev_t *vd) 878 { 879 if (vd == NULL) 880 return; 881 882 vd->vdev_top = tvd; 883 884 for (int c = 0; c < vd->vdev_children; c++) 885 vdev_top_update(tvd, vd->vdev_child[c]); 886 } 887 888 /* 889 * Add a mirror/replacing vdev above an existing vdev. 890 */ 891 vdev_t * 892 vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops) 893 { 894 spa_t *spa = cvd->vdev_spa; 895 vdev_t *pvd = cvd->vdev_parent; 896 vdev_t *mvd; 897 898 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 899 900 mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops); 901 902 mvd->vdev_asize = cvd->vdev_asize; 903 mvd->vdev_min_asize = cvd->vdev_min_asize; 904 mvd->vdev_max_asize = cvd->vdev_max_asize; 905 mvd->vdev_psize = cvd->vdev_psize; 906 mvd->vdev_ashift = cvd->vdev_ashift; 907 mvd->vdev_state = cvd->vdev_state; 908 mvd->vdev_crtxg = cvd->vdev_crtxg; 909 910 vdev_remove_child(pvd, cvd); 911 vdev_add_child(pvd, mvd); 912 cvd->vdev_id = mvd->vdev_children; 913 vdev_add_child(mvd, cvd); 914 vdev_top_update(cvd->vdev_top, cvd->vdev_top); 915 916 if (mvd == mvd->vdev_top) 917 vdev_top_transfer(cvd, mvd); 918 919 return (mvd); 920 } 921 922 /* 923 * Remove a 1-way mirror/replacing vdev from the tree. 924 */ 925 void 926 vdev_remove_parent(vdev_t *cvd) 927 { 928 vdev_t *mvd = cvd->vdev_parent; 929 vdev_t *pvd = mvd->vdev_parent; 930 931 ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 932 933 ASSERT(mvd->vdev_children == 1); 934 ASSERT(mvd->vdev_ops == &vdev_mirror_ops || 935 mvd->vdev_ops == &vdev_replacing_ops || 936 mvd->vdev_ops == &vdev_spare_ops); 937 cvd->vdev_ashift = mvd->vdev_ashift; 938 939 vdev_remove_child(mvd, cvd); 940 vdev_remove_child(pvd, mvd); 941 942 /* 943 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid. 944 * Otherwise, we could have detached an offline device, and when we 945 * go to import the pool we'll think we have two top-level vdevs, 946 * instead of a different version of the same top-level vdev. 947 */ 948 if (mvd->vdev_top == mvd) { 949 uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid; 950 cvd->vdev_orig_guid = cvd->vdev_guid; 951 cvd->vdev_guid += guid_delta; 952 cvd->vdev_guid_sum += guid_delta; 953 } 954 cvd->vdev_id = mvd->vdev_id; 955 vdev_add_child(pvd, cvd); 956 vdev_top_update(cvd->vdev_top, cvd->vdev_top); 957 958 if (cvd == cvd->vdev_top) 959 vdev_top_transfer(mvd, cvd); 960 961 ASSERT(mvd->vdev_children == 0); 962 vdev_free(mvd); 963 } 964 965 int 966 vdev_metaslab_init(vdev_t *vd, uint64_t txg) 967 { 968 spa_t *spa = vd->vdev_spa; 969 objset_t *mos = spa->spa_meta_objset; 970 uint64_t m; 971 uint64_t oldc = vd->vdev_ms_count; 972 uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift; 973 metaslab_t **mspp; 974 int error; 975 976 ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 977 978 /* 979 * This vdev is not being allocated from yet or is a hole. 980 */ 981 if (vd->vdev_ms_shift == 0) 982 return (0); 983 984 ASSERT(!vd->vdev_ishole); 985 986 ASSERT(oldc <= newc); 987 988 mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP); 989 990 if (oldc != 0) { 991 bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp)); 992 kmem_free(vd->vdev_ms, oldc * sizeof (*mspp)); 993 } 994 995 vd->vdev_ms = mspp; 996 vd->vdev_ms_count = newc; 997 998 for (m = oldc; m < newc; m++) { 999 uint64_t object = 0; 1000 1001 /* 1002 * vdev_ms_array may be 0 if we are creating the "fake" 1003 * metaslabs for an indirect vdev for zdb's leak detection. 1004 * See zdb_leak_init(). 1005 */ 1006 if (txg == 0 && vd->vdev_ms_array != 0) { 1007 error = dmu_read(mos, vd->vdev_ms_array, 1008 m * sizeof (uint64_t), sizeof (uint64_t), &object, 1009 DMU_READ_PREFETCH); 1010 if (error != 0) { 1011 vdev_dbgmsg(vd, "unable to read the metaslab " 1012 "array [error=%d]", error); 1013 return (error); 1014 } 1015 } 1016 1017 error = metaslab_init(vd->vdev_mg, m, object, txg, 1018 &(vd->vdev_ms[m])); 1019 if (error != 0) { 1020 vdev_dbgmsg(vd, "metaslab_init failed [error=%d]", 1021 error); 1022 return (error); 1023 } 1024 } 1025 1026 if (txg == 0) 1027 spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER); 1028 1029 /* 1030 * If the vdev is being removed we don't activate 1031 * the metaslabs since we want to ensure that no new 1032 * allocations are performed on this device. 1033 */ 1034 if (oldc == 0 && !vd->vdev_removing) 1035 metaslab_group_activate(vd->vdev_mg); 1036 1037 if (txg == 0) 1038 spa_config_exit(spa, SCL_ALLOC, FTAG); 1039 1040 return (0); 1041 } 1042 1043 void 1044 vdev_metaslab_fini(vdev_t *vd) 1045 { 1046 if (vd->vdev_ms != NULL) { 1047 uint64_t count = vd->vdev_ms_count; 1048 1049 metaslab_group_passivate(vd->vdev_mg); 1050 for (uint64_t m = 0; m < count; m++) { 1051 metaslab_t *msp = vd->vdev_ms[m]; 1052 1053 if (msp != NULL) 1054 metaslab_fini(msp); 1055 } 1056 kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *)); 1057 vd->vdev_ms = NULL; 1058 1059 vd->vdev_ms_count = 0; 1060 } 1061 ASSERT0(vd->vdev_ms_count); 1062 } 1063 1064 typedef struct vdev_probe_stats { 1065 boolean_t vps_readable; 1066 boolean_t vps_writeable; 1067 int vps_flags; 1068 } vdev_probe_stats_t; 1069 1070 static void 1071 vdev_probe_done(zio_t *zio) 1072 { 1073 spa_t *spa = zio->io_spa; 1074 vdev_t *vd = zio->io_vd; 1075 vdev_probe_stats_t *vps = zio->io_private; 1076 1077 ASSERT(vd->vdev_probe_zio != NULL); 1078 1079 if (zio->io_type == ZIO_TYPE_READ) { 1080 if (zio->io_error == 0) 1081 vps->vps_readable = 1; 1082 if (zio->io_error == 0 && spa_writeable(spa)) { 1083 zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd, 1084 zio->io_offset, zio->io_size, zio->io_abd, 1085 ZIO_CHECKSUM_OFF, vdev_probe_done, vps, 1086 ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE)); 1087 } else { 1088 abd_free(zio->io_abd); 1089 } 1090 } else if (zio->io_type == ZIO_TYPE_WRITE) { 1091 if (zio->io_error == 0) 1092 vps->vps_writeable = 1; 1093 abd_free(zio->io_abd); 1094 } else if (zio->io_type == ZIO_TYPE_NULL) { 1095 zio_t *pio; 1096 1097 vd->vdev_cant_read |= !vps->vps_readable; 1098 vd->vdev_cant_write |= !vps->vps_writeable; 1099 1100 if (vdev_readable(vd) && 1101 (vdev_writeable(vd) || !spa_writeable(spa))) { 1102 zio->io_error = 0; 1103 } else { 1104 ASSERT(zio->io_error != 0); 1105 vdev_dbgmsg(vd, "failed probe"); 1106 zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE, 1107 spa, vd, NULL, 0, 0); 1108 zio->io_error = SET_ERROR(ENXIO); 1109 } 1110 1111 mutex_enter(&vd->vdev_probe_lock); 1112 ASSERT(vd->vdev_probe_zio == zio); 1113 vd->vdev_probe_zio = NULL; 1114 mutex_exit(&vd->vdev_probe_lock); 1115 1116 zio_link_t *zl = NULL; 1117 while ((pio = zio_walk_parents(zio, &zl)) != NULL) 1118 if (!vdev_accessible(vd, pio)) 1119 pio->io_error = SET_ERROR(ENXIO); 1120 1121 kmem_free(vps, sizeof (*vps)); 1122 } 1123 } 1124 1125 /* 1126 * Determine whether this device is accessible. 1127 * 1128 * Read and write to several known locations: the pad regions of each 1129 * vdev label but the first, which we leave alone in case it contains 1130 * a VTOC. 1131 */ 1132 zio_t * 1133 vdev_probe(vdev_t *vd, zio_t *zio) 1134 { 1135 spa_t *spa = vd->vdev_spa; 1136 vdev_probe_stats_t *vps = NULL; 1137 zio_t *pio; 1138 1139 ASSERT(vd->vdev_ops->vdev_op_leaf); 1140 1141 /* 1142 * Don't probe the probe. 1143 */ 1144 if (zio && (zio->io_flags & ZIO_FLAG_PROBE)) 1145 return (NULL); 1146 1147 /* 1148 * To prevent 'probe storms' when a device fails, we create 1149 * just one probe i/o at a time. All zios that want to probe 1150 * this vdev will become parents of the probe io. 1151 */ 1152 mutex_enter(&vd->vdev_probe_lock); 1153 1154 if ((pio = vd->vdev_probe_zio) == NULL) { 1155 vps = kmem_zalloc(sizeof (*vps), KM_SLEEP); 1156 1157 vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE | 1158 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE | 1159 ZIO_FLAG_TRYHARD; 1160 1161 if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) { 1162 /* 1163 * vdev_cant_read and vdev_cant_write can only 1164 * transition from TRUE to FALSE when we have the 1165 * SCL_ZIO lock as writer; otherwise they can only 1166 * transition from FALSE to TRUE. This ensures that 1167 * any zio looking at these values can assume that 1168 * failures persist for the life of the I/O. That's 1169 * important because when a device has intermittent 1170 * connectivity problems, we want to ensure that 1171 * they're ascribed to the device (ENXIO) and not 1172 * the zio (EIO). 1173 * 1174 * Since we hold SCL_ZIO as writer here, clear both 1175 * values so the probe can reevaluate from first 1176 * principles. 1177 */ 1178 vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER; 1179 vd->vdev_cant_read = B_FALSE; 1180 vd->vdev_cant_write = B_FALSE; 1181 } 1182 1183 vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd, 1184 vdev_probe_done, vps, 1185 vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE); 1186 1187 /* 1188 * We can't change the vdev state in this context, so we 1189 * kick off an async task to do it on our behalf. 1190 */ 1191 if (zio != NULL) { 1192 vd->vdev_probe_wanted = B_TRUE; 1193 spa_async_request(spa, SPA_ASYNC_PROBE); 1194 } 1195 } 1196 1197 if (zio != NULL) 1198 zio_add_child(zio, pio); 1199 1200 mutex_exit(&vd->vdev_probe_lock); 1201 1202 if (vps == NULL) { 1203 ASSERT(zio != NULL); 1204 return (NULL); 1205 } 1206 1207 for (int l = 1; l < VDEV_LABELS; l++) { 1208 zio_nowait(zio_read_phys(pio, vd, 1209 vdev_label_offset(vd->vdev_psize, l, 1210 offsetof(vdev_label_t, vl_pad2)), VDEV_PAD_SIZE, 1211 abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE), 1212 ZIO_CHECKSUM_OFF, vdev_probe_done, vps, 1213 ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE)); 1214 } 1215 1216 if (zio == NULL) 1217 return (pio); 1218 1219 zio_nowait(pio); 1220 return (NULL); 1221 } 1222 1223 static void 1224 vdev_open_child(void *arg) 1225 { 1226 vdev_t *vd = arg; 1227 1228 vd->vdev_open_thread = curthread; 1229 vd->vdev_open_error = vdev_open(vd); 1230 vd->vdev_open_thread = NULL; 1231 } 1232 1233 boolean_t 1234 vdev_uses_zvols(vdev_t *vd) 1235 { 1236 if (vd->vdev_path && strncmp(vd->vdev_path, ZVOL_DIR, 1237 strlen(ZVOL_DIR)) == 0) 1238 return (B_TRUE); 1239 for (int c = 0; c < vd->vdev_children; c++) 1240 if (vdev_uses_zvols(vd->vdev_child[c])) 1241 return (B_TRUE); 1242 return (B_FALSE); 1243 } 1244 1245 void 1246 vdev_open_children(vdev_t *vd) 1247 { 1248 taskq_t *tq; 1249 int children = vd->vdev_children; 1250 1251 /* 1252 * in order to handle pools on top of zvols, do the opens 1253 * in a single thread so that the same thread holds the 1254 * spa_namespace_lock 1255 */ 1256 if (vdev_uses_zvols(vd)) { 1257 for (int c = 0; c < children; c++) 1258 vd->vdev_child[c]->vdev_open_error = 1259 vdev_open(vd->vdev_child[c]); 1260 return; 1261 } 1262 tq = taskq_create("vdev_open", children, minclsyspri, 1263 children, children, TASKQ_PREPOPULATE); 1264 1265 for (int c = 0; c < children; c++) 1266 VERIFY(taskq_dispatch(tq, vdev_open_child, vd->vdev_child[c], 1267 TQ_SLEEP) != NULL); 1268 1269 taskq_destroy(tq); 1270 } 1271 1272 /* 1273 * Compute the raidz-deflation ratio. Note, we hard-code 1274 * in 128k (1 << 17) because it is the "typical" blocksize. 1275 * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change, 1276 * otherwise it would inconsistently account for existing bp's. 1277 */ 1278 static void 1279 vdev_set_deflate_ratio(vdev_t *vd) 1280 { 1281 if (vd == vd->vdev_top && !vd->vdev_ishole && vd->vdev_ashift != 0) { 1282 vd->vdev_deflate_ratio = (1 << 17) / 1283 (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT); 1284 } 1285 } 1286 1287 /* 1288 * Prepare a virtual device for access. 1289 */ 1290 int 1291 vdev_open(vdev_t *vd) 1292 { 1293 spa_t *spa = vd->vdev_spa; 1294 int error; 1295 uint64_t osize = 0; 1296 uint64_t max_osize = 0; 1297 uint64_t asize, max_asize, psize; 1298 uint64_t ashift = 0; 1299 1300 ASSERT(vd->vdev_open_thread == curthread || 1301 spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 1302 ASSERT(vd->vdev_state == VDEV_STATE_CLOSED || 1303 vd->vdev_state == VDEV_STATE_CANT_OPEN || 1304 vd->vdev_state == VDEV_STATE_OFFLINE); 1305 1306 vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 1307 vd->vdev_cant_read = B_FALSE; 1308 vd->vdev_cant_write = B_FALSE; 1309 vd->vdev_min_asize = vdev_get_min_asize(vd); 1310 1311 /* 1312 * If this vdev is not removed, check its fault status. If it's 1313 * faulted, bail out of the open. 1314 */ 1315 if (!vd->vdev_removed && vd->vdev_faulted) { 1316 ASSERT(vd->vdev_children == 0); 1317 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED || 1318 vd->vdev_label_aux == VDEV_AUX_EXTERNAL); 1319 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 1320 vd->vdev_label_aux); 1321 return (SET_ERROR(ENXIO)); 1322 } else if (vd->vdev_offline) { 1323 ASSERT(vd->vdev_children == 0); 1324 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE); 1325 return (SET_ERROR(ENXIO)); 1326 } 1327 1328 error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize, &ashift); 1329 1330 /* 1331 * Reset the vdev_reopening flag so that we actually close 1332 * the vdev on error. 1333 */ 1334 vd->vdev_reopening = B_FALSE; 1335 if (zio_injection_enabled && error == 0) 1336 error = zio_handle_device_injection(vd, NULL, ENXIO); 1337 1338 if (error) { 1339 if (vd->vdev_removed && 1340 vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED) 1341 vd->vdev_removed = B_FALSE; 1342 1343 if (vd->vdev_stat.vs_aux == VDEV_AUX_CHILDREN_OFFLINE) { 1344 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, 1345 vd->vdev_stat.vs_aux); 1346 } else { 1347 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1348 vd->vdev_stat.vs_aux); 1349 } 1350 return (error); 1351 } 1352 1353 vd->vdev_removed = B_FALSE; 1354 1355 /* 1356 * Recheck the faulted flag now that we have confirmed that 1357 * the vdev is accessible. If we're faulted, bail. 1358 */ 1359 if (vd->vdev_faulted) { 1360 ASSERT(vd->vdev_children == 0); 1361 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED || 1362 vd->vdev_label_aux == VDEV_AUX_EXTERNAL); 1363 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 1364 vd->vdev_label_aux); 1365 return (SET_ERROR(ENXIO)); 1366 } 1367 1368 if (vd->vdev_degraded) { 1369 ASSERT(vd->vdev_children == 0); 1370 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 1371 VDEV_AUX_ERR_EXCEEDED); 1372 } else { 1373 vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0); 1374 } 1375 1376 /* 1377 * For hole or missing vdevs we just return success. 1378 */ 1379 if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) 1380 return (0); 1381 1382 for (int c = 0; c < vd->vdev_children; c++) { 1383 if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) { 1384 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 1385 VDEV_AUX_NONE); 1386 break; 1387 } 1388 } 1389 1390 osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t)); 1391 max_osize = P2ALIGN(max_osize, (uint64_t)sizeof (vdev_label_t)); 1392 1393 if (vd->vdev_children == 0) { 1394 if (osize < SPA_MINDEVSIZE) { 1395 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1396 VDEV_AUX_TOO_SMALL); 1397 return (SET_ERROR(EOVERFLOW)); 1398 } 1399 psize = osize; 1400 asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE); 1401 max_asize = max_osize - (VDEV_LABEL_START_SIZE + 1402 VDEV_LABEL_END_SIZE); 1403 } else { 1404 if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE - 1405 (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) { 1406 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1407 VDEV_AUX_TOO_SMALL); 1408 return (SET_ERROR(EOVERFLOW)); 1409 } 1410 psize = 0; 1411 asize = osize; 1412 max_asize = max_osize; 1413 } 1414 1415 vd->vdev_psize = psize; 1416 1417 /* 1418 * Make sure the allocatable size hasn't shrunk too much. 1419 */ 1420 if (asize < vd->vdev_min_asize) { 1421 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1422 VDEV_AUX_BAD_LABEL); 1423 return (SET_ERROR(EINVAL)); 1424 } 1425 1426 if (vd->vdev_asize == 0) { 1427 /* 1428 * This is the first-ever open, so use the computed values. 1429 * For testing purposes, a higher ashift can be requested. 1430 */ 1431 vd->vdev_asize = asize; 1432 vd->vdev_max_asize = max_asize; 1433 vd->vdev_ashift = MAX(ashift, vd->vdev_ashift); 1434 } else { 1435 /* 1436 * Detect if the alignment requirement has increased. 1437 * We don't want to make the pool unavailable, just 1438 * issue a warning instead. 1439 */ 1440 if (ashift > vd->vdev_top->vdev_ashift && 1441 vd->vdev_ops->vdev_op_leaf) { 1442 cmn_err(CE_WARN, 1443 "Disk, '%s', has a block alignment that is " 1444 "larger than the pool's alignment\n", 1445 vd->vdev_path); 1446 } 1447 vd->vdev_max_asize = max_asize; 1448 } 1449 1450 /* 1451 * If all children are healthy we update asize if either: 1452 * The asize has increased, due to a device expansion caused by dynamic 1453 * LUN growth or vdev replacement, and automatic expansion is enabled; 1454 * making the additional space available. 1455 * 1456 * The asize has decreased, due to a device shrink usually caused by a 1457 * vdev replace with a smaller device. This ensures that calculations 1458 * based of max_asize and asize e.g. esize are always valid. It's safe 1459 * to do this as we've already validated that asize is greater than 1460 * vdev_min_asize. 1461 */ 1462 if (vd->vdev_state == VDEV_STATE_HEALTHY && 1463 ((asize > vd->vdev_asize && 1464 (vd->vdev_expanding || spa->spa_autoexpand)) || 1465 (asize < vd->vdev_asize))) 1466 vd->vdev_asize = asize; 1467 1468 vdev_set_min_asize(vd); 1469 1470 /* 1471 * Ensure we can issue some IO before declaring the 1472 * vdev open for business. 1473 */ 1474 if (vd->vdev_ops->vdev_op_leaf && 1475 (error = zio_wait(vdev_probe(vd, NULL))) != 0) { 1476 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 1477 VDEV_AUX_ERR_EXCEEDED); 1478 return (error); 1479 } 1480 1481 if (vd->vdev_top == vd && vd->vdev_ashift != 0 && 1482 !vd->vdev_isl2cache && !vd->vdev_islog) { 1483 if (vd->vdev_ashift > spa->spa_max_ashift) 1484 spa->spa_max_ashift = vd->vdev_ashift; 1485 if (vd->vdev_ashift < spa->spa_min_ashift) 1486 spa->spa_min_ashift = vd->vdev_ashift; 1487 } 1488 1489 /* 1490 * Track the min and max ashift values for normal data devices. 1491 */ 1492 if (vd->vdev_top == vd && vd->vdev_ashift != 0 && 1493 !vd->vdev_islog && vd->vdev_aux == NULL) { 1494 if (vd->vdev_ashift > spa->spa_max_ashift) 1495 spa->spa_max_ashift = vd->vdev_ashift; 1496 if (vd->vdev_ashift < spa->spa_min_ashift) 1497 spa->spa_min_ashift = vd->vdev_ashift; 1498 } 1499 1500 /* 1501 * If a leaf vdev has a DTL, and seems healthy, then kick off a 1502 * resilver. But don't do this if we are doing a reopen for a scrub, 1503 * since this would just restart the scrub we are already doing. 1504 */ 1505 if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen && 1506 vdev_resilver_needed(vd, NULL, NULL)) 1507 spa_async_request(spa, SPA_ASYNC_RESILVER); 1508 1509 return (0); 1510 } 1511 1512 /* 1513 * Called once the vdevs are all opened, this routine validates the label 1514 * contents. This needs to be done before vdev_load() so that we don't 1515 * inadvertently do repair I/Os to the wrong device. 1516 * 1517 * This function will only return failure if one of the vdevs indicates that it 1518 * has since been destroyed or exported. This is only possible if 1519 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state 1520 * will be updated but the function will return 0. 1521 */ 1522 int 1523 vdev_validate(vdev_t *vd) 1524 { 1525 spa_t *spa = vd->vdev_spa; 1526 nvlist_t *label; 1527 uint64_t guid = 0, aux_guid = 0, top_guid; 1528 uint64_t state; 1529 nvlist_t *nvl; 1530 uint64_t txg; 1531 1532 if (vdev_validate_skip) 1533 return (0); 1534 1535 for (uint64_t c = 0; c < vd->vdev_children; c++) 1536 if (vdev_validate(vd->vdev_child[c]) != 0) 1537 return (SET_ERROR(EBADF)); 1538 1539 /* 1540 * If the device has already failed, or was marked offline, don't do 1541 * any further validation. Otherwise, label I/O will fail and we will 1542 * overwrite the previous state. 1543 */ 1544 if (!vd->vdev_ops->vdev_op_leaf || !vdev_readable(vd)) 1545 return (0); 1546 1547 /* 1548 * If we are performing an extreme rewind, we allow for a label that 1549 * was modified at a point after the current txg. 1550 */ 1551 if (spa->spa_extreme_rewind || spa_last_synced_txg(spa) == 0) 1552 txg = UINT64_MAX; 1553 else 1554 txg = spa_last_synced_txg(spa); 1555 1556 if ((label = vdev_label_read_config(vd, txg)) == NULL) { 1557 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1558 VDEV_AUX_BAD_LABEL); 1559 vdev_dbgmsg(vd, "vdev_validate: failed reading config"); 1560 return (0); 1561 } 1562 1563 /* 1564 * Determine if this vdev has been split off into another 1565 * pool. If so, then refuse to open it. 1566 */ 1567 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID, 1568 &aux_guid) == 0 && aux_guid == spa_guid(spa)) { 1569 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1570 VDEV_AUX_SPLIT_POOL); 1571 nvlist_free(label); 1572 vdev_dbgmsg(vd, "vdev_validate: vdev split into other pool"); 1573 return (0); 1574 } 1575 1576 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &guid) != 0) { 1577 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1578 VDEV_AUX_CORRUPT_DATA); 1579 nvlist_free(label); 1580 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 1581 ZPOOL_CONFIG_POOL_GUID); 1582 return (0); 1583 } 1584 1585 /* 1586 * If config is not trusted then ignore the spa guid check. This is 1587 * necessary because if the machine crashed during a re-guid the new 1588 * guid might have been written to all of the vdev labels, but not the 1589 * cached config. The check will be performed again once we have the 1590 * trusted config from the MOS. 1591 */ 1592 if (spa->spa_trust_config && guid != spa_guid(spa)) { 1593 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1594 VDEV_AUX_CORRUPT_DATA); 1595 nvlist_free(label); 1596 vdev_dbgmsg(vd, "vdev_validate: vdev label pool_guid doesn't " 1597 "match config (%llu != %llu)", (u_longlong_t)guid, 1598 (u_longlong_t)spa_guid(spa)); 1599 return (0); 1600 } 1601 1602 if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl) 1603 != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID, 1604 &aux_guid) != 0) 1605 aux_guid = 0; 1606 1607 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0) { 1608 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1609 VDEV_AUX_CORRUPT_DATA); 1610 nvlist_free(label); 1611 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 1612 ZPOOL_CONFIG_GUID); 1613 return (0); 1614 } 1615 1616 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID, &top_guid) 1617 != 0) { 1618 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1619 VDEV_AUX_CORRUPT_DATA); 1620 nvlist_free(label); 1621 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 1622 ZPOOL_CONFIG_TOP_GUID); 1623 return (0); 1624 } 1625 1626 /* 1627 * If this vdev just became a top-level vdev because its sibling was 1628 * detached, it will have adopted the parent's vdev guid -- but the 1629 * label may or may not be on disk yet. Fortunately, either version 1630 * of the label will have the same top guid, so if we're a top-level 1631 * vdev, we can safely compare to that instead. 1632 * However, if the config comes from a cachefile that failed to update 1633 * after the detach, a top-level vdev will appear as a non top-level 1634 * vdev in the config. Also relax the constraints if we perform an 1635 * extreme rewind. 1636 * 1637 * If we split this vdev off instead, then we also check the 1638 * original pool's guid. We don't want to consider the vdev 1639 * corrupt if it is partway through a split operation. 1640 */ 1641 if (vd->vdev_guid != guid && vd->vdev_guid != aux_guid) { 1642 boolean_t mismatch = B_FALSE; 1643 if (spa->spa_trust_config && !spa->spa_extreme_rewind) { 1644 if (vd != vd->vdev_top || vd->vdev_guid != top_guid) 1645 mismatch = B_TRUE; 1646 } else { 1647 if (vd->vdev_guid != top_guid && 1648 vd->vdev_top->vdev_guid != guid) 1649 mismatch = B_TRUE; 1650 } 1651 1652 if (mismatch) { 1653 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1654 VDEV_AUX_CORRUPT_DATA); 1655 nvlist_free(label); 1656 vdev_dbgmsg(vd, "vdev_validate: config guid " 1657 "doesn't match label guid"); 1658 vdev_dbgmsg(vd, "CONFIG: guid %llu, top_guid %llu", 1659 (u_longlong_t)vd->vdev_guid, 1660 (u_longlong_t)vd->vdev_top->vdev_guid); 1661 vdev_dbgmsg(vd, "LABEL: guid %llu, top_guid %llu, " 1662 "aux_guid %llu", (u_longlong_t)guid, 1663 (u_longlong_t)top_guid, (u_longlong_t)aux_guid); 1664 return (0); 1665 } 1666 } 1667 1668 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, 1669 &state) != 0) { 1670 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1671 VDEV_AUX_CORRUPT_DATA); 1672 nvlist_free(label); 1673 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 1674 ZPOOL_CONFIG_POOL_STATE); 1675 return (0); 1676 } 1677 1678 nvlist_free(label); 1679 1680 /* 1681 * If this is a verbatim import, no need to check the 1682 * state of the pool. 1683 */ 1684 if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) && 1685 spa_load_state(spa) == SPA_LOAD_OPEN && 1686 state != POOL_STATE_ACTIVE) { 1687 vdev_dbgmsg(vd, "vdev_validate: invalid pool state (%llu) " 1688 "for spa %s", (u_longlong_t)state, spa->spa_name); 1689 return (SET_ERROR(EBADF)); 1690 } 1691 1692 /* 1693 * If we were able to open and validate a vdev that was 1694 * previously marked permanently unavailable, clear that state 1695 * now. 1696 */ 1697 if (vd->vdev_not_present) 1698 vd->vdev_not_present = 0; 1699 1700 return (0); 1701 } 1702 1703 static void 1704 vdev_copy_path_impl(vdev_t *svd, vdev_t *dvd) 1705 { 1706 if (svd->vdev_path != NULL && dvd->vdev_path != NULL) { 1707 if (strcmp(svd->vdev_path, dvd->vdev_path) != 0) { 1708 zfs_dbgmsg("vdev_copy_path: vdev %llu: path changed " 1709 "from '%s' to '%s'", (u_longlong_t)dvd->vdev_guid, 1710 dvd->vdev_path, svd->vdev_path); 1711 spa_strfree(dvd->vdev_path); 1712 dvd->vdev_path = spa_strdup(svd->vdev_path); 1713 } 1714 } else if (svd->vdev_path != NULL) { 1715 dvd->vdev_path = spa_strdup(svd->vdev_path); 1716 zfs_dbgmsg("vdev_copy_path: vdev %llu: path set to '%s'", 1717 (u_longlong_t)dvd->vdev_guid, dvd->vdev_path); 1718 } 1719 } 1720 1721 /* 1722 * Recursively copy vdev paths from one vdev to another. Source and destination 1723 * vdev trees must have same geometry otherwise return error. Intended to copy 1724 * paths from userland config into MOS config. 1725 */ 1726 int 1727 vdev_copy_path_strict(vdev_t *svd, vdev_t *dvd) 1728 { 1729 if ((svd->vdev_ops == &vdev_missing_ops) || 1730 (svd->vdev_ishole && dvd->vdev_ishole) || 1731 (dvd->vdev_ops == &vdev_indirect_ops)) 1732 return (0); 1733 1734 if (svd->vdev_ops != dvd->vdev_ops) { 1735 vdev_dbgmsg(svd, "vdev_copy_path: vdev type mismatch: %s != %s", 1736 svd->vdev_ops->vdev_op_type, dvd->vdev_ops->vdev_op_type); 1737 return (SET_ERROR(EINVAL)); 1738 } 1739 1740 if (svd->vdev_guid != dvd->vdev_guid) { 1741 vdev_dbgmsg(svd, "vdev_copy_path: guids mismatch (%llu != " 1742 "%llu)", (u_longlong_t)svd->vdev_guid, 1743 (u_longlong_t)dvd->vdev_guid); 1744 return (SET_ERROR(EINVAL)); 1745 } 1746 1747 if (svd->vdev_children != dvd->vdev_children) { 1748 vdev_dbgmsg(svd, "vdev_copy_path: children count mismatch: " 1749 "%llu != %llu", (u_longlong_t)svd->vdev_children, 1750 (u_longlong_t)dvd->vdev_children); 1751 return (SET_ERROR(EINVAL)); 1752 } 1753 1754 for (uint64_t i = 0; i < svd->vdev_children; i++) { 1755 int error = vdev_copy_path_strict(svd->vdev_child[i], 1756 dvd->vdev_child[i]); 1757 if (error != 0) 1758 return (error); 1759 } 1760 1761 if (svd->vdev_ops->vdev_op_leaf) 1762 vdev_copy_path_impl(svd, dvd); 1763 1764 return (0); 1765 } 1766 1767 static void 1768 vdev_copy_path_search(vdev_t *stvd, vdev_t *dvd) 1769 { 1770 ASSERT(stvd->vdev_top == stvd); 1771 ASSERT3U(stvd->vdev_id, ==, dvd->vdev_top->vdev_id); 1772 1773 for (uint64_t i = 0; i < dvd->vdev_children; i++) { 1774 vdev_copy_path_search(stvd, dvd->vdev_child[i]); 1775 } 1776 1777 if (!dvd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(dvd)) 1778 return; 1779 1780 /* 1781 * The idea here is that while a vdev can shift positions within 1782 * a top vdev (when replacing, attaching mirror, etc.) it cannot 1783 * step outside of it. 1784 */ 1785 vdev_t *vd = vdev_lookup_by_guid(stvd, dvd->vdev_guid); 1786 1787 if (vd == NULL || vd->vdev_ops != dvd->vdev_ops) 1788 return; 1789 1790 ASSERT(vd->vdev_ops->vdev_op_leaf); 1791 1792 vdev_copy_path_impl(vd, dvd); 1793 } 1794 1795 /* 1796 * Recursively copy vdev paths from one root vdev to another. Source and 1797 * destination vdev trees may differ in geometry. For each destination leaf 1798 * vdev, search a vdev with the same guid and top vdev id in the source. 1799 * Intended to copy paths from userland config into MOS config. 1800 */ 1801 void 1802 vdev_copy_path_relaxed(vdev_t *srvd, vdev_t *drvd) 1803 { 1804 uint64_t children = MIN(srvd->vdev_children, drvd->vdev_children); 1805 ASSERT(srvd->vdev_ops == &vdev_root_ops); 1806 ASSERT(drvd->vdev_ops == &vdev_root_ops); 1807 1808 for (uint64_t i = 0; i < children; i++) { 1809 vdev_copy_path_search(srvd->vdev_child[i], 1810 drvd->vdev_child[i]); 1811 } 1812 } 1813 1814 /* 1815 * Close a virtual device. 1816 */ 1817 void 1818 vdev_close(vdev_t *vd) 1819 { 1820 spa_t *spa = vd->vdev_spa; 1821 vdev_t *pvd = vd->vdev_parent; 1822 1823 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 1824 1825 /* 1826 * If our parent is reopening, then we are as well, unless we are 1827 * going offline. 1828 */ 1829 if (pvd != NULL && pvd->vdev_reopening) 1830 vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline); 1831 1832 vd->vdev_ops->vdev_op_close(vd); 1833 1834 vdev_cache_purge(vd); 1835 1836 /* 1837 * We record the previous state before we close it, so that if we are 1838 * doing a reopen(), we don't generate FMA ereports if we notice that 1839 * it's still faulted. 1840 */ 1841 vd->vdev_prevstate = vd->vdev_state; 1842 1843 if (vd->vdev_offline) 1844 vd->vdev_state = VDEV_STATE_OFFLINE; 1845 else 1846 vd->vdev_state = VDEV_STATE_CLOSED; 1847 vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 1848 } 1849 1850 void 1851 vdev_hold(vdev_t *vd) 1852 { 1853 spa_t *spa = vd->vdev_spa; 1854 1855 ASSERT(spa_is_root(spa)); 1856 if (spa->spa_state == POOL_STATE_UNINITIALIZED) 1857 return; 1858 1859 for (int c = 0; c < vd->vdev_children; c++) 1860 vdev_hold(vd->vdev_child[c]); 1861 1862 if (vd->vdev_ops->vdev_op_leaf) 1863 vd->vdev_ops->vdev_op_hold(vd); 1864 } 1865 1866 void 1867 vdev_rele(vdev_t *vd) 1868 { 1869 spa_t *spa = vd->vdev_spa; 1870 1871 ASSERT(spa_is_root(spa)); 1872 for (int c = 0; c < vd->vdev_children; c++) 1873 vdev_rele(vd->vdev_child[c]); 1874 1875 if (vd->vdev_ops->vdev_op_leaf) 1876 vd->vdev_ops->vdev_op_rele(vd); 1877 } 1878 1879 /* 1880 * Reopen all interior vdevs and any unopened leaves. We don't actually 1881 * reopen leaf vdevs which had previously been opened as they might deadlock 1882 * on the spa_config_lock. Instead we only obtain the leaf's physical size. 1883 * If the leaf has never been opened then open it, as usual. 1884 */ 1885 void 1886 vdev_reopen(vdev_t *vd) 1887 { 1888 spa_t *spa = vd->vdev_spa; 1889 1890 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 1891 1892 /* set the reopening flag unless we're taking the vdev offline */ 1893 vd->vdev_reopening = !vd->vdev_offline; 1894 vdev_close(vd); 1895 (void) vdev_open(vd); 1896 1897 /* 1898 * Call vdev_validate() here to make sure we have the same device. 1899 * Otherwise, a device with an invalid label could be successfully 1900 * opened in response to vdev_reopen(). 1901 */ 1902 if (vd->vdev_aux) { 1903 (void) vdev_validate_aux(vd); 1904 if (vdev_readable(vd) && vdev_writeable(vd) && 1905 vd->vdev_aux == &spa->spa_l2cache && 1906 !l2arc_vdev_present(vd)) 1907 l2arc_add_vdev(spa, vd); 1908 } else { 1909 (void) vdev_validate(vd); 1910 } 1911 1912 /* 1913 * Reassess parent vdev's health. 1914 */ 1915 vdev_propagate_state(vd); 1916 } 1917 1918 int 1919 vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing) 1920 { 1921 int error; 1922 1923 /* 1924 * Normally, partial opens (e.g. of a mirror) are allowed. 1925 * For a create, however, we want to fail the request if 1926 * there are any components we can't open. 1927 */ 1928 error = vdev_open(vd); 1929 1930 if (error || vd->vdev_state != VDEV_STATE_HEALTHY) { 1931 vdev_close(vd); 1932 return (error ? error : ENXIO); 1933 } 1934 1935 /* 1936 * Recursively load DTLs and initialize all labels. 1937 */ 1938 if ((error = vdev_dtl_load(vd)) != 0 || 1939 (error = vdev_label_init(vd, txg, isreplacing ? 1940 VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) { 1941 vdev_close(vd); 1942 return (error); 1943 } 1944 1945 return (0); 1946 } 1947 1948 void 1949 vdev_metaslab_set_size(vdev_t *vd) 1950 { 1951 /* 1952 * Aim for roughly metaslabs_per_vdev (default 200) metaslabs per vdev. 1953 */ 1954 vd->vdev_ms_shift = highbit64(vd->vdev_asize / metaslabs_per_vdev); 1955 vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT); 1956 } 1957 1958 void 1959 vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg) 1960 { 1961 ASSERT(vd == vd->vdev_top); 1962 /* indirect vdevs don't have metaslabs or dtls */ 1963 ASSERT(vdev_is_concrete(vd) || flags == 0); 1964 ASSERT(ISP2(flags)); 1965 ASSERT(spa_writeable(vd->vdev_spa)); 1966 1967 if (flags & VDD_METASLAB) 1968 (void) txg_list_add(&vd->vdev_ms_list, arg, txg); 1969 1970 if (flags & VDD_DTL) 1971 (void) txg_list_add(&vd->vdev_dtl_list, arg, txg); 1972 1973 (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg); 1974 } 1975 1976 void 1977 vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg) 1978 { 1979 for (int c = 0; c < vd->vdev_children; c++) 1980 vdev_dirty_leaves(vd->vdev_child[c], flags, txg); 1981 1982 if (vd->vdev_ops->vdev_op_leaf) 1983 vdev_dirty(vd->vdev_top, flags, vd, txg); 1984 } 1985 1986 /* 1987 * DTLs. 1988 * 1989 * A vdev's DTL (dirty time log) is the set of transaction groups for which 1990 * the vdev has less than perfect replication. There are four kinds of DTL: 1991 * 1992 * DTL_MISSING: txgs for which the vdev has no valid copies of the data 1993 * 1994 * DTL_PARTIAL: txgs for which data is available, but not fully replicated 1995 * 1996 * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon 1997 * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of 1998 * txgs that was scrubbed. 1999 * 2000 * DTL_OUTAGE: txgs which cannot currently be read, whether due to 2001 * persistent errors or just some device being offline. 2002 * Unlike the other three, the DTL_OUTAGE map is not generally 2003 * maintained; it's only computed when needed, typically to 2004 * determine whether a device can be detached. 2005 * 2006 * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device 2007 * either has the data or it doesn't. 2008 * 2009 * For interior vdevs such as mirror and RAID-Z the picture is more complex. 2010 * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because 2011 * if any child is less than fully replicated, then so is its parent. 2012 * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs, 2013 * comprising only those txgs which appear in 'maxfaults' or more children; 2014 * those are the txgs we don't have enough replication to read. For example, 2015 * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2); 2016 * thus, its DTL_MISSING consists of the set of txgs that appear in more than 2017 * two child DTL_MISSING maps. 2018 * 2019 * It should be clear from the above that to compute the DTLs and outage maps 2020 * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps. 2021 * Therefore, that is all we keep on disk. When loading the pool, or after 2022 * a configuration change, we generate all other DTLs from first principles. 2023 */ 2024 void 2025 vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 2026 { 2027 range_tree_t *rt = vd->vdev_dtl[t]; 2028 2029 ASSERT(t < DTL_TYPES); 2030 ASSERT(vd != vd->vdev_spa->spa_root_vdev); 2031 ASSERT(spa_writeable(vd->vdev_spa)); 2032 2033 mutex_enter(&vd->vdev_dtl_lock); 2034 if (!range_tree_contains(rt, txg, size)) 2035 range_tree_add(rt, txg, size); 2036 mutex_exit(&vd->vdev_dtl_lock); 2037 } 2038 2039 boolean_t 2040 vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 2041 { 2042 range_tree_t *rt = vd->vdev_dtl[t]; 2043 boolean_t dirty = B_FALSE; 2044 2045 ASSERT(t < DTL_TYPES); 2046 ASSERT(vd != vd->vdev_spa->spa_root_vdev); 2047 2048 /* 2049 * While we are loading the pool, the DTLs have not been loaded yet. 2050 * Ignore the DTLs and try all devices. This avoids a recursive 2051 * mutex enter on the vdev_dtl_lock, and also makes us try hard 2052 * when loading the pool (relying on the checksum to ensure that 2053 * we get the right data -- note that we while loading, we are 2054 * only reading the MOS, which is always checksummed). 2055 */ 2056 if (vd->vdev_spa->spa_load_state != SPA_LOAD_NONE) 2057 return (B_FALSE); 2058 2059 mutex_enter(&vd->vdev_dtl_lock); 2060 if (range_tree_space(rt) != 0) 2061 dirty = range_tree_contains(rt, txg, size); 2062 mutex_exit(&vd->vdev_dtl_lock); 2063 2064 return (dirty); 2065 } 2066 2067 boolean_t 2068 vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t) 2069 { 2070 range_tree_t *rt = vd->vdev_dtl[t]; 2071 boolean_t empty; 2072 2073 mutex_enter(&vd->vdev_dtl_lock); 2074 empty = (range_tree_space(rt) == 0); 2075 mutex_exit(&vd->vdev_dtl_lock); 2076 2077 return (empty); 2078 } 2079 2080 /* 2081 * Returns the lowest txg in the DTL range. 2082 */ 2083 static uint64_t 2084 vdev_dtl_min(vdev_t *vd) 2085 { 2086 range_seg_t *rs; 2087 2088 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock)); 2089 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0); 2090 ASSERT0(vd->vdev_children); 2091 2092 rs = avl_first(&vd->vdev_dtl[DTL_MISSING]->rt_root); 2093 return (rs->rs_start - 1); 2094 } 2095 2096 /* 2097 * Returns the highest txg in the DTL. 2098 */ 2099 static uint64_t 2100 vdev_dtl_max(vdev_t *vd) 2101 { 2102 range_seg_t *rs; 2103 2104 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock)); 2105 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0); 2106 ASSERT0(vd->vdev_children); 2107 2108 rs = avl_last(&vd->vdev_dtl[DTL_MISSING]->rt_root); 2109 return (rs->rs_end); 2110 } 2111 2112 /* 2113 * Determine if a resilvering vdev should remove any DTL entries from 2114 * its range. If the vdev was resilvering for the entire duration of the 2115 * scan then it should excise that range from its DTLs. Otherwise, this 2116 * vdev is considered partially resilvered and should leave its DTL 2117 * entries intact. The comment in vdev_dtl_reassess() describes how we 2118 * excise the DTLs. 2119 */ 2120 static boolean_t 2121 vdev_dtl_should_excise(vdev_t *vd) 2122 { 2123 spa_t *spa = vd->vdev_spa; 2124 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; 2125 2126 ASSERT0(scn->scn_phys.scn_errors); 2127 ASSERT0(vd->vdev_children); 2128 2129 if (vd->vdev_state < VDEV_STATE_DEGRADED) 2130 return (B_FALSE); 2131 2132 if (vd->vdev_resilver_txg == 0 || 2133 range_tree_space(vd->vdev_dtl[DTL_MISSING]) == 0) 2134 return (B_TRUE); 2135 2136 /* 2137 * When a resilver is initiated the scan will assign the scn_max_txg 2138 * value to the highest txg value that exists in all DTLs. If this 2139 * device's max DTL is not part of this scan (i.e. it is not in 2140 * the range (scn_min_txg, scn_max_txg] then it is not eligible 2141 * for excision. 2142 */ 2143 if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) { 2144 ASSERT3U(scn->scn_phys.scn_min_txg, <=, vdev_dtl_min(vd)); 2145 ASSERT3U(scn->scn_phys.scn_min_txg, <, vd->vdev_resilver_txg); 2146 ASSERT3U(vd->vdev_resilver_txg, <=, scn->scn_phys.scn_max_txg); 2147 return (B_TRUE); 2148 } 2149 return (B_FALSE); 2150 } 2151 2152 /* 2153 * Reassess DTLs after a config change or scrub completion. 2154 */ 2155 void 2156 vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done) 2157 { 2158 spa_t *spa = vd->vdev_spa; 2159 avl_tree_t reftree; 2160 int minref; 2161 2162 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 2163 2164 for (int c = 0; c < vd->vdev_children; c++) 2165 vdev_dtl_reassess(vd->vdev_child[c], txg, 2166 scrub_txg, scrub_done); 2167 2168 if (vd == spa->spa_root_vdev || !vdev_is_concrete(vd) || vd->vdev_aux) 2169 return; 2170 2171 if (vd->vdev_ops->vdev_op_leaf) { 2172 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; 2173 2174 mutex_enter(&vd->vdev_dtl_lock); 2175 2176 /* 2177 * If we've completed a scan cleanly then determine 2178 * if this vdev should remove any DTLs. We only want to 2179 * excise regions on vdevs that were available during 2180 * the entire duration of this scan. 2181 */ 2182 if (scrub_txg != 0 && 2183 (spa->spa_scrub_started || 2184 (scn != NULL && scn->scn_phys.scn_errors == 0)) && 2185 vdev_dtl_should_excise(vd)) { 2186 /* 2187 * We completed a scrub up to scrub_txg. If we 2188 * did it without rebooting, then the scrub dtl 2189 * will be valid, so excise the old region and 2190 * fold in the scrub dtl. Otherwise, leave the 2191 * dtl as-is if there was an error. 2192 * 2193 * There's little trick here: to excise the beginning 2194 * of the DTL_MISSING map, we put it into a reference 2195 * tree and then add a segment with refcnt -1 that 2196 * covers the range [0, scrub_txg). This means 2197 * that each txg in that range has refcnt -1 or 0. 2198 * We then add DTL_SCRUB with a refcnt of 2, so that 2199 * entries in the range [0, scrub_txg) will have a 2200 * positive refcnt -- either 1 or 2. We then convert 2201 * the reference tree into the new DTL_MISSING map. 2202 */ 2203 space_reftree_create(&reftree); 2204 space_reftree_add_map(&reftree, 2205 vd->vdev_dtl[DTL_MISSING], 1); 2206 space_reftree_add_seg(&reftree, 0, scrub_txg, -1); 2207 space_reftree_add_map(&reftree, 2208 vd->vdev_dtl[DTL_SCRUB], 2); 2209 space_reftree_generate_map(&reftree, 2210 vd->vdev_dtl[DTL_MISSING], 1); 2211 space_reftree_destroy(&reftree); 2212 } 2213 range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL); 2214 range_tree_walk(vd->vdev_dtl[DTL_MISSING], 2215 range_tree_add, vd->vdev_dtl[DTL_PARTIAL]); 2216 if (scrub_done) 2217 range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL); 2218 range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL); 2219 if (!vdev_readable(vd)) 2220 range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL); 2221 else 2222 range_tree_walk(vd->vdev_dtl[DTL_MISSING], 2223 range_tree_add, vd->vdev_dtl[DTL_OUTAGE]); 2224 2225 /* 2226 * If the vdev was resilvering and no longer has any 2227 * DTLs then reset its resilvering flag. 2228 */ 2229 if (vd->vdev_resilver_txg != 0 && 2230 range_tree_space(vd->vdev_dtl[DTL_MISSING]) == 0 && 2231 range_tree_space(vd->vdev_dtl[DTL_OUTAGE]) == 0) 2232 vd->vdev_resilver_txg = 0; 2233 2234 mutex_exit(&vd->vdev_dtl_lock); 2235 2236 if (txg != 0) 2237 vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg); 2238 return; 2239 } 2240 2241 mutex_enter(&vd->vdev_dtl_lock); 2242 for (int t = 0; t < DTL_TYPES; t++) { 2243 /* account for child's outage in parent's missing map */ 2244 int s = (t == DTL_MISSING) ? DTL_OUTAGE: t; 2245 if (t == DTL_SCRUB) 2246 continue; /* leaf vdevs only */ 2247 if (t == DTL_PARTIAL) 2248 minref = 1; /* i.e. non-zero */ 2249 else if (vd->vdev_nparity != 0) 2250 minref = vd->vdev_nparity + 1; /* RAID-Z */ 2251 else 2252 minref = vd->vdev_children; /* any kind of mirror */ 2253 space_reftree_create(&reftree); 2254 for (int c = 0; c < vd->vdev_children; c++) { 2255 vdev_t *cvd = vd->vdev_child[c]; 2256 mutex_enter(&cvd->vdev_dtl_lock); 2257 space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1); 2258 mutex_exit(&cvd->vdev_dtl_lock); 2259 } 2260 space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref); 2261 space_reftree_destroy(&reftree); 2262 } 2263 mutex_exit(&vd->vdev_dtl_lock); 2264 } 2265 2266 int 2267 vdev_dtl_load(vdev_t *vd) 2268 { 2269 spa_t *spa = vd->vdev_spa; 2270 objset_t *mos = spa->spa_meta_objset; 2271 int error = 0; 2272 2273 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) { 2274 ASSERT(vdev_is_concrete(vd)); 2275 2276 error = space_map_open(&vd->vdev_dtl_sm, mos, 2277 vd->vdev_dtl_object, 0, -1ULL, 0); 2278 if (error) 2279 return (error); 2280 ASSERT(vd->vdev_dtl_sm != NULL); 2281 2282 mutex_enter(&vd->vdev_dtl_lock); 2283 2284 /* 2285 * Now that we've opened the space_map we need to update 2286 * the in-core DTL. 2287 */ 2288 space_map_update(vd->vdev_dtl_sm); 2289 2290 error = space_map_load(vd->vdev_dtl_sm, 2291 vd->vdev_dtl[DTL_MISSING], SM_ALLOC); 2292 mutex_exit(&vd->vdev_dtl_lock); 2293 2294 return (error); 2295 } 2296 2297 for (int c = 0; c < vd->vdev_children; c++) { 2298 error = vdev_dtl_load(vd->vdev_child[c]); 2299 if (error != 0) 2300 break; 2301 } 2302 2303 return (error); 2304 } 2305 2306 void 2307 vdev_destroy_unlink_zap(vdev_t *vd, uint64_t zapobj, dmu_tx_t *tx) 2308 { 2309 spa_t *spa = vd->vdev_spa; 2310 2311 VERIFY0(zap_destroy(spa->spa_meta_objset, zapobj, tx)); 2312 VERIFY0(zap_remove_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps, 2313 zapobj, tx)); 2314 } 2315 2316 uint64_t 2317 vdev_create_link_zap(vdev_t *vd, dmu_tx_t *tx) 2318 { 2319 spa_t *spa = vd->vdev_spa; 2320 uint64_t zap = zap_create(spa->spa_meta_objset, DMU_OTN_ZAP_METADATA, 2321 DMU_OT_NONE, 0, tx); 2322 2323 ASSERT(zap != 0); 2324 VERIFY0(zap_add_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps, 2325 zap, tx)); 2326 2327 return (zap); 2328 } 2329 2330 void 2331 vdev_construct_zaps(vdev_t *vd, dmu_tx_t *tx) 2332 { 2333 if (vd->vdev_ops != &vdev_hole_ops && 2334 vd->vdev_ops != &vdev_missing_ops && 2335 vd->vdev_ops != &vdev_root_ops && 2336 !vd->vdev_top->vdev_removing) { 2337 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_leaf_zap == 0) { 2338 vd->vdev_leaf_zap = vdev_create_link_zap(vd, tx); 2339 } 2340 if (vd == vd->vdev_top && vd->vdev_top_zap == 0) { 2341 vd->vdev_top_zap = vdev_create_link_zap(vd, tx); 2342 } 2343 } 2344 for (uint64_t i = 0; i < vd->vdev_children; i++) { 2345 vdev_construct_zaps(vd->vdev_child[i], tx); 2346 } 2347 } 2348 2349 void 2350 vdev_dtl_sync(vdev_t *vd, uint64_t txg) 2351 { 2352 spa_t *spa = vd->vdev_spa; 2353 range_tree_t *rt = vd->vdev_dtl[DTL_MISSING]; 2354 objset_t *mos = spa->spa_meta_objset; 2355 range_tree_t *rtsync; 2356 dmu_tx_t *tx; 2357 uint64_t object = space_map_object(vd->vdev_dtl_sm); 2358 2359 ASSERT(vdev_is_concrete(vd)); 2360 ASSERT(vd->vdev_ops->vdev_op_leaf); 2361 2362 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 2363 2364 if (vd->vdev_detached || vd->vdev_top->vdev_removing) { 2365 mutex_enter(&vd->vdev_dtl_lock); 2366 space_map_free(vd->vdev_dtl_sm, tx); 2367 space_map_close(vd->vdev_dtl_sm); 2368 vd->vdev_dtl_sm = NULL; 2369 mutex_exit(&vd->vdev_dtl_lock); 2370 2371 /* 2372 * We only destroy the leaf ZAP for detached leaves or for 2373 * removed log devices. Removed data devices handle leaf ZAP 2374 * cleanup later, once cancellation is no longer possible. 2375 */ 2376 if (vd->vdev_leaf_zap != 0 && (vd->vdev_detached || 2377 vd->vdev_top->vdev_islog)) { 2378 vdev_destroy_unlink_zap(vd, vd->vdev_leaf_zap, tx); 2379 vd->vdev_leaf_zap = 0; 2380 } 2381 2382 dmu_tx_commit(tx); 2383 return; 2384 } 2385 2386 if (vd->vdev_dtl_sm == NULL) { 2387 uint64_t new_object; 2388 2389 new_object = space_map_alloc(mos, tx); 2390 VERIFY3U(new_object, !=, 0); 2391 2392 VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object, 2393 0, -1ULL, 0)); 2394 ASSERT(vd->vdev_dtl_sm != NULL); 2395 } 2396 2397 rtsync = range_tree_create(NULL, NULL); 2398 2399 mutex_enter(&vd->vdev_dtl_lock); 2400 range_tree_walk(rt, range_tree_add, rtsync); 2401 mutex_exit(&vd->vdev_dtl_lock); 2402 2403 space_map_truncate(vd->vdev_dtl_sm, tx); 2404 space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, tx); 2405 range_tree_vacate(rtsync, NULL, NULL); 2406 2407 range_tree_destroy(rtsync); 2408 2409 /* 2410 * If the object for the space map has changed then dirty 2411 * the top level so that we update the config. 2412 */ 2413 if (object != space_map_object(vd->vdev_dtl_sm)) { 2414 vdev_dbgmsg(vd, "txg %llu, spa %s, DTL old object %llu, " 2415 "new object %llu", (u_longlong_t)txg, spa_name(spa), 2416 (u_longlong_t)object, 2417 (u_longlong_t)space_map_object(vd->vdev_dtl_sm)); 2418 vdev_config_dirty(vd->vdev_top); 2419 } 2420 2421 dmu_tx_commit(tx); 2422 2423 mutex_enter(&vd->vdev_dtl_lock); 2424 space_map_update(vd->vdev_dtl_sm); 2425 mutex_exit(&vd->vdev_dtl_lock); 2426 } 2427 2428 /* 2429 * Determine whether the specified vdev can be offlined/detached/removed 2430 * without losing data. 2431 */ 2432 boolean_t 2433 vdev_dtl_required(vdev_t *vd) 2434 { 2435 spa_t *spa = vd->vdev_spa; 2436 vdev_t *tvd = vd->vdev_top; 2437 uint8_t cant_read = vd->vdev_cant_read; 2438 boolean_t required; 2439 2440 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 2441 2442 if (vd == spa->spa_root_vdev || vd == tvd) 2443 return (B_TRUE); 2444 2445 /* 2446 * Temporarily mark the device as unreadable, and then determine 2447 * whether this results in any DTL outages in the top-level vdev. 2448 * If not, we can safely offline/detach/remove the device. 2449 */ 2450 vd->vdev_cant_read = B_TRUE; 2451 vdev_dtl_reassess(tvd, 0, 0, B_FALSE); 2452 required = !vdev_dtl_empty(tvd, DTL_OUTAGE); 2453 vd->vdev_cant_read = cant_read; 2454 vdev_dtl_reassess(tvd, 0, 0, B_FALSE); 2455 2456 if (!required && zio_injection_enabled) 2457 required = !!zio_handle_device_injection(vd, NULL, ECHILD); 2458 2459 return (required); 2460 } 2461 2462 /* 2463 * Determine if resilver is needed, and if so the txg range. 2464 */ 2465 boolean_t 2466 vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp) 2467 { 2468 boolean_t needed = B_FALSE; 2469 uint64_t thismin = UINT64_MAX; 2470 uint64_t thismax = 0; 2471 2472 if (vd->vdev_children == 0) { 2473 mutex_enter(&vd->vdev_dtl_lock); 2474 if (range_tree_space(vd->vdev_dtl[DTL_MISSING]) != 0 && 2475 vdev_writeable(vd)) { 2476 2477 thismin = vdev_dtl_min(vd); 2478 thismax = vdev_dtl_max(vd); 2479 needed = B_TRUE; 2480 } 2481 mutex_exit(&vd->vdev_dtl_lock); 2482 } else { 2483 for (int c = 0; c < vd->vdev_children; c++) { 2484 vdev_t *cvd = vd->vdev_child[c]; 2485 uint64_t cmin, cmax; 2486 2487 if (vdev_resilver_needed(cvd, &cmin, &cmax)) { 2488 thismin = MIN(thismin, cmin); 2489 thismax = MAX(thismax, cmax); 2490 needed = B_TRUE; 2491 } 2492 } 2493 } 2494 2495 if (needed && minp) { 2496 *minp = thismin; 2497 *maxp = thismax; 2498 } 2499 return (needed); 2500 } 2501 2502 int 2503 vdev_load(vdev_t *vd) 2504 { 2505 int error = 0; 2506 /* 2507 * Recursively load all children. 2508 */ 2509 for (int c = 0; c < vd->vdev_children; c++) { 2510 error = vdev_load(vd->vdev_child[c]); 2511 if (error != 0) { 2512 return (error); 2513 } 2514 } 2515 2516 vdev_set_deflate_ratio(vd); 2517 2518 /* 2519 * If this is a top-level vdev, initialize its metaslabs. 2520 */ 2521 if (vd == vd->vdev_top && vdev_is_concrete(vd)) { 2522 if (vd->vdev_ashift == 0 || vd->vdev_asize == 0) { 2523 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 2524 VDEV_AUX_CORRUPT_DATA); 2525 vdev_dbgmsg(vd, "vdev_load: invalid size. ashift=%llu, " 2526 "asize=%llu", (u_longlong_t)vd->vdev_ashift, 2527 (u_longlong_t)vd->vdev_asize); 2528 return (SET_ERROR(ENXIO)); 2529 } else if ((error = vdev_metaslab_init(vd, 0)) != 0) { 2530 vdev_dbgmsg(vd, "vdev_load: metaslab_init failed " 2531 "[error=%d]", error); 2532 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 2533 VDEV_AUX_CORRUPT_DATA); 2534 return (error); 2535 } 2536 } 2537 2538 /* 2539 * If this is a leaf vdev, load its DTL. 2540 */ 2541 if (vd->vdev_ops->vdev_op_leaf && (error = vdev_dtl_load(vd)) != 0) { 2542 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 2543 VDEV_AUX_CORRUPT_DATA); 2544 vdev_dbgmsg(vd, "vdev_load: vdev_dtl_load failed " 2545 "[error=%d]", error); 2546 return (error); 2547 } 2548 2549 uint64_t obsolete_sm_object = vdev_obsolete_sm_object(vd); 2550 if (obsolete_sm_object != 0) { 2551 objset_t *mos = vd->vdev_spa->spa_meta_objset; 2552 ASSERT(vd->vdev_asize != 0); 2553 ASSERT(vd->vdev_obsolete_sm == NULL); 2554 2555 if ((error = space_map_open(&vd->vdev_obsolete_sm, mos, 2556 obsolete_sm_object, 0, vd->vdev_asize, 0))) { 2557 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 2558 VDEV_AUX_CORRUPT_DATA); 2559 vdev_dbgmsg(vd, "vdev_load: space_map_open failed for " 2560 "obsolete spacemap (obj %llu) [error=%d]", 2561 (u_longlong_t)obsolete_sm_object, error); 2562 return (error); 2563 } 2564 space_map_update(vd->vdev_obsolete_sm); 2565 } 2566 2567 return (0); 2568 } 2569 2570 /* 2571 * The special vdev case is used for hot spares and l2cache devices. Its 2572 * sole purpose it to set the vdev state for the associated vdev. To do this, 2573 * we make sure that we can open the underlying device, then try to read the 2574 * label, and make sure that the label is sane and that it hasn't been 2575 * repurposed to another pool. 2576 */ 2577 int 2578 vdev_validate_aux(vdev_t *vd) 2579 { 2580 nvlist_t *label; 2581 uint64_t guid, version; 2582 uint64_t state; 2583 2584 if (!vdev_readable(vd)) 2585 return (0); 2586 2587 if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) { 2588 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 2589 VDEV_AUX_CORRUPT_DATA); 2590 return (-1); 2591 } 2592 2593 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 || 2594 !SPA_VERSION_IS_SUPPORTED(version) || 2595 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 || 2596 guid != vd->vdev_guid || 2597 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) { 2598 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 2599 VDEV_AUX_CORRUPT_DATA); 2600 nvlist_free(label); 2601 return (-1); 2602 } 2603 2604 /* 2605 * We don't actually check the pool state here. If it's in fact in 2606 * use by another pool, we update this fact on the fly when requested. 2607 */ 2608 nvlist_free(label); 2609 return (0); 2610 } 2611 2612 /* 2613 * Free the objects used to store this vdev's spacemaps, and the array 2614 * that points to them. 2615 */ 2616 void 2617 vdev_destroy_spacemaps(vdev_t *vd, dmu_tx_t *tx) 2618 { 2619 if (vd->vdev_ms_array == 0) 2620 return; 2621 2622 objset_t *mos = vd->vdev_spa->spa_meta_objset; 2623 uint64_t array_count = vd->vdev_asize >> vd->vdev_ms_shift; 2624 size_t array_bytes = array_count * sizeof (uint64_t); 2625 uint64_t *smobj_array = kmem_alloc(array_bytes, KM_SLEEP); 2626 VERIFY0(dmu_read(mos, vd->vdev_ms_array, 0, 2627 array_bytes, smobj_array, 0)); 2628 2629 for (uint64_t i = 0; i < array_count; i++) { 2630 uint64_t smobj = smobj_array[i]; 2631 if (smobj == 0) 2632 continue; 2633 2634 space_map_free_obj(mos, smobj, tx); 2635 } 2636 2637 kmem_free(smobj_array, array_bytes); 2638 VERIFY0(dmu_object_free(mos, vd->vdev_ms_array, tx)); 2639 vd->vdev_ms_array = 0; 2640 } 2641 2642 static void 2643 vdev_remove_empty(vdev_t *vd, uint64_t txg) 2644 { 2645 spa_t *spa = vd->vdev_spa; 2646 dmu_tx_t *tx; 2647 2648 ASSERT(vd == vd->vdev_top); 2649 ASSERT3U(txg, ==, spa_syncing_txg(spa)); 2650 2651 if (vd->vdev_ms != NULL) { 2652 metaslab_group_t *mg = vd->vdev_mg; 2653 2654 metaslab_group_histogram_verify(mg); 2655 metaslab_class_histogram_verify(mg->mg_class); 2656 2657 for (int m = 0; m < vd->vdev_ms_count; m++) { 2658 metaslab_t *msp = vd->vdev_ms[m]; 2659 2660 if (msp == NULL || msp->ms_sm == NULL) 2661 continue; 2662 2663 mutex_enter(&msp->ms_lock); 2664 /* 2665 * If the metaslab was not loaded when the vdev 2666 * was removed then the histogram accounting may 2667 * not be accurate. Update the histogram information 2668 * here so that we ensure that the metaslab group 2669 * and metaslab class are up-to-date. 2670 */ 2671 metaslab_group_histogram_remove(mg, msp); 2672 2673 VERIFY0(space_map_allocated(msp->ms_sm)); 2674 space_map_close(msp->ms_sm); 2675 msp->ms_sm = NULL; 2676 mutex_exit(&msp->ms_lock); 2677 } 2678 2679 metaslab_group_histogram_verify(mg); 2680 metaslab_class_histogram_verify(mg->mg_class); 2681 for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++) 2682 ASSERT0(mg->mg_histogram[i]); 2683 } 2684 2685 tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg); 2686 vdev_destroy_spacemaps(vd, tx); 2687 2688 if (vd->vdev_islog && vd->vdev_top_zap != 0) { 2689 vdev_destroy_unlink_zap(vd, vd->vdev_top_zap, tx); 2690 vd->vdev_top_zap = 0; 2691 } 2692 dmu_tx_commit(tx); 2693 } 2694 2695 void 2696 vdev_sync_done(vdev_t *vd, uint64_t txg) 2697 { 2698 metaslab_t *msp; 2699 boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg)); 2700 2701 ASSERT(vdev_is_concrete(vd)); 2702 2703 while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg))) 2704 metaslab_sync_done(msp, txg); 2705 2706 if (reassess) 2707 metaslab_sync_reassess(vd->vdev_mg); 2708 } 2709 2710 void 2711 vdev_sync(vdev_t *vd, uint64_t txg) 2712 { 2713 spa_t *spa = vd->vdev_spa; 2714 vdev_t *lvd; 2715 metaslab_t *msp; 2716 dmu_tx_t *tx; 2717 2718 if (range_tree_space(vd->vdev_obsolete_segments) > 0) { 2719 dmu_tx_t *tx; 2720 2721 ASSERT(vd->vdev_removing || 2722 vd->vdev_ops == &vdev_indirect_ops); 2723 2724 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 2725 vdev_indirect_sync_obsolete(vd, tx); 2726 dmu_tx_commit(tx); 2727 2728 /* 2729 * If the vdev is indirect, it can't have dirty 2730 * metaslabs or DTLs. 2731 */ 2732 if (vd->vdev_ops == &vdev_indirect_ops) { 2733 ASSERT(txg_list_empty(&vd->vdev_ms_list, txg)); 2734 ASSERT(txg_list_empty(&vd->vdev_dtl_list, txg)); 2735 return; 2736 } 2737 } 2738 2739 ASSERT(vdev_is_concrete(vd)); 2740 2741 if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0 && 2742 !vd->vdev_removing) { 2743 ASSERT(vd == vd->vdev_top); 2744 ASSERT0(vd->vdev_indirect_config.vic_mapping_object); 2745 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 2746 vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset, 2747 DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx); 2748 ASSERT(vd->vdev_ms_array != 0); 2749 vdev_config_dirty(vd); 2750 dmu_tx_commit(tx); 2751 } 2752 2753 while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) { 2754 metaslab_sync(msp, txg); 2755 (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg)); 2756 } 2757 2758 while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL) 2759 vdev_dtl_sync(lvd, txg); 2760 2761 /* 2762 * Remove the metadata associated with this vdev once it's empty. 2763 * Note that this is typically used for log/cache device removal; 2764 * we don't empty toplevel vdevs when removing them. But if 2765 * a toplevel happens to be emptied, this is not harmful. 2766 */ 2767 if (vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing) { 2768 vdev_remove_empty(vd, txg); 2769 } 2770 2771 (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)); 2772 } 2773 2774 uint64_t 2775 vdev_psize_to_asize(vdev_t *vd, uint64_t psize) 2776 { 2777 return (vd->vdev_ops->vdev_op_asize(vd, psize)); 2778 } 2779 2780 /* 2781 * Mark the given vdev faulted. A faulted vdev behaves as if the device could 2782 * not be opened, and no I/O is attempted. 2783 */ 2784 int 2785 vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux) 2786 { 2787 vdev_t *vd, *tvd; 2788 2789 spa_vdev_state_enter(spa, SCL_NONE); 2790 2791 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 2792 return (spa_vdev_state_exit(spa, NULL, ENODEV)); 2793 2794 if (!vd->vdev_ops->vdev_op_leaf) 2795 return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 2796 2797 tvd = vd->vdev_top; 2798 2799 /* 2800 * We don't directly use the aux state here, but if we do a 2801 * vdev_reopen(), we need this value to be present to remember why we 2802 * were faulted. 2803 */ 2804 vd->vdev_label_aux = aux; 2805 2806 /* 2807 * Faulted state takes precedence over degraded. 2808 */ 2809 vd->vdev_delayed_close = B_FALSE; 2810 vd->vdev_faulted = 1ULL; 2811 vd->vdev_degraded = 0ULL; 2812 vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux); 2813 2814 /* 2815 * If this device has the only valid copy of the data, then 2816 * back off and simply mark the vdev as degraded instead. 2817 */ 2818 if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) { 2819 vd->vdev_degraded = 1ULL; 2820 vd->vdev_faulted = 0ULL; 2821 2822 /* 2823 * If we reopen the device and it's not dead, only then do we 2824 * mark it degraded. 2825 */ 2826 vdev_reopen(tvd); 2827 2828 if (vdev_readable(vd)) 2829 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux); 2830 } 2831 2832 return (spa_vdev_state_exit(spa, vd, 0)); 2833 } 2834 2835 /* 2836 * Mark the given vdev degraded. A degraded vdev is purely an indication to the 2837 * user that something is wrong. The vdev continues to operate as normal as far 2838 * as I/O is concerned. 2839 */ 2840 int 2841 vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux) 2842 { 2843 vdev_t *vd; 2844 2845 spa_vdev_state_enter(spa, SCL_NONE); 2846 2847 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 2848 return (spa_vdev_state_exit(spa, NULL, ENODEV)); 2849 2850 if (!vd->vdev_ops->vdev_op_leaf) 2851 return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 2852 2853 /* 2854 * If the vdev is already faulted, then don't do anything. 2855 */ 2856 if (vd->vdev_faulted || vd->vdev_degraded) 2857 return (spa_vdev_state_exit(spa, NULL, 0)); 2858 2859 vd->vdev_degraded = 1ULL; 2860 if (!vdev_is_dead(vd)) 2861 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, 2862 aux); 2863 2864 return (spa_vdev_state_exit(spa, vd, 0)); 2865 } 2866 2867 /* 2868 * Online the given vdev. 2869 * 2870 * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached 2871 * spare device should be detached when the device finishes resilvering. 2872 * Second, the online should be treated like a 'test' online case, so no FMA 2873 * events are generated if the device fails to open. 2874 */ 2875 int 2876 vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate) 2877 { 2878 vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev; 2879 boolean_t wasoffline; 2880 vdev_state_t oldstate; 2881 2882 spa_vdev_state_enter(spa, SCL_NONE); 2883 2884 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 2885 return (spa_vdev_state_exit(spa, NULL, ENODEV)); 2886 2887 if (!vd->vdev_ops->vdev_op_leaf) 2888 return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 2889 2890 wasoffline = (vd->vdev_offline || vd->vdev_tmpoffline); 2891 oldstate = vd->vdev_state; 2892 2893 tvd = vd->vdev_top; 2894 vd->vdev_offline = B_FALSE; 2895 vd->vdev_tmpoffline = B_FALSE; 2896 vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE); 2897 vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT); 2898 2899 /* XXX - L2ARC 1.0 does not support expansion */ 2900 if (!vd->vdev_aux) { 2901 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 2902 pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND); 2903 } 2904 2905 vdev_reopen(tvd); 2906 vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE; 2907 2908 if (!vd->vdev_aux) { 2909 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 2910 pvd->vdev_expanding = B_FALSE; 2911 } 2912 2913 if (newstate) 2914 *newstate = vd->vdev_state; 2915 if ((flags & ZFS_ONLINE_UNSPARE) && 2916 !vdev_is_dead(vd) && vd->vdev_parent && 2917 vd->vdev_parent->vdev_ops == &vdev_spare_ops && 2918 vd->vdev_parent->vdev_child[0] == vd) 2919 vd->vdev_unspare = B_TRUE; 2920 2921 if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) { 2922 2923 /* XXX - L2ARC 1.0 does not support expansion */ 2924 if (vd->vdev_aux) 2925 return (spa_vdev_state_exit(spa, vd, ENOTSUP)); 2926 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 2927 } 2928 2929 if (wasoffline || 2930 (oldstate < VDEV_STATE_DEGRADED && 2931 vd->vdev_state >= VDEV_STATE_DEGRADED)) 2932 spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_ONLINE); 2933 2934 return (spa_vdev_state_exit(spa, vd, 0)); 2935 } 2936 2937 static int 2938 vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags) 2939 { 2940 vdev_t *vd, *tvd; 2941 int error = 0; 2942 uint64_t generation; 2943 metaslab_group_t *mg; 2944 2945 top: 2946 spa_vdev_state_enter(spa, SCL_ALLOC); 2947 2948 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 2949 return (spa_vdev_state_exit(spa, NULL, ENODEV)); 2950 2951 if (!vd->vdev_ops->vdev_op_leaf) 2952 return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 2953 2954 tvd = vd->vdev_top; 2955 mg = tvd->vdev_mg; 2956 generation = spa->spa_config_generation + 1; 2957 2958 /* 2959 * If the device isn't already offline, try to offline it. 2960 */ 2961 if (!vd->vdev_offline) { 2962 /* 2963 * If this device has the only valid copy of some data, 2964 * don't allow it to be offlined. Log devices are always 2965 * expendable. 2966 */ 2967 if (!tvd->vdev_islog && vd->vdev_aux == NULL && 2968 vdev_dtl_required(vd)) 2969 return (spa_vdev_state_exit(spa, NULL, EBUSY)); 2970 2971 /* 2972 * If the top-level is a slog and it has had allocations 2973 * then proceed. We check that the vdev's metaslab group 2974 * is not NULL since it's possible that we may have just 2975 * added this vdev but not yet initialized its metaslabs. 2976 */ 2977 if (tvd->vdev_islog && mg != NULL) { 2978 /* 2979 * Prevent any future allocations. 2980 */ 2981 metaslab_group_passivate(mg); 2982 (void) spa_vdev_state_exit(spa, vd, 0); 2983 2984 error = spa_reset_logs(spa); 2985 2986 spa_vdev_state_enter(spa, SCL_ALLOC); 2987 2988 /* 2989 * Check to see if the config has changed. 2990 */ 2991 if (error || generation != spa->spa_config_generation) { 2992 metaslab_group_activate(mg); 2993 if (error) 2994 return (spa_vdev_state_exit(spa, 2995 vd, error)); 2996 (void) spa_vdev_state_exit(spa, vd, 0); 2997 goto top; 2998 } 2999 ASSERT0(tvd->vdev_stat.vs_alloc); 3000 } 3001 3002 /* 3003 * Offline this device and reopen its top-level vdev. 3004 * If the top-level vdev is a log device then just offline 3005 * it. Otherwise, if this action results in the top-level 3006 * vdev becoming unusable, undo it and fail the request. 3007 */ 3008 vd->vdev_offline = B_TRUE; 3009 vdev_reopen(tvd); 3010 3011 if (!tvd->vdev_islog && vd->vdev_aux == NULL && 3012 vdev_is_dead(tvd)) { 3013 vd->vdev_offline = B_FALSE; 3014 vdev_reopen(tvd); 3015 return (spa_vdev_state_exit(spa, NULL, EBUSY)); 3016 } 3017 3018 /* 3019 * Add the device back into the metaslab rotor so that 3020 * once we online the device it's open for business. 3021 */ 3022 if (tvd->vdev_islog && mg != NULL) 3023 metaslab_group_activate(mg); 3024 } 3025 3026 vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY); 3027 3028 return (spa_vdev_state_exit(spa, vd, 0)); 3029 } 3030 3031 int 3032 vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags) 3033 { 3034 int error; 3035 3036 mutex_enter(&spa->spa_vdev_top_lock); 3037 error = vdev_offline_locked(spa, guid, flags); 3038 mutex_exit(&spa->spa_vdev_top_lock); 3039 3040 return (error); 3041 } 3042 3043 /* 3044 * Clear the error counts associated with this vdev. Unlike vdev_online() and 3045 * vdev_offline(), we assume the spa config is locked. We also clear all 3046 * children. If 'vd' is NULL, then the user wants to clear all vdevs. 3047 */ 3048 void 3049 vdev_clear(spa_t *spa, vdev_t *vd) 3050 { 3051 vdev_t *rvd = spa->spa_root_vdev; 3052 3053 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 3054 3055 if (vd == NULL) 3056 vd = rvd; 3057 3058 vd->vdev_stat.vs_read_errors = 0; 3059 vd->vdev_stat.vs_write_errors = 0; 3060 vd->vdev_stat.vs_checksum_errors = 0; 3061 3062 for (int c = 0; c < vd->vdev_children; c++) 3063 vdev_clear(spa, vd->vdev_child[c]); 3064 3065 /* 3066 * It makes no sense to "clear" an indirect vdev. 3067 */ 3068 if (!vdev_is_concrete(vd)) 3069 return; 3070 3071 /* 3072 * If we're in the FAULTED state or have experienced failed I/O, then 3073 * clear the persistent state and attempt to reopen the device. We 3074 * also mark the vdev config dirty, so that the new faulted state is 3075 * written out to disk. 3076 */ 3077 if (vd->vdev_faulted || vd->vdev_degraded || 3078 !vdev_readable(vd) || !vdev_writeable(vd)) { 3079 3080 /* 3081 * When reopening in reponse to a clear event, it may be due to 3082 * a fmadm repair request. In this case, if the device is 3083 * still broken, we want to still post the ereport again. 3084 */ 3085 vd->vdev_forcefault = B_TRUE; 3086 3087 vd->vdev_faulted = vd->vdev_degraded = 0ULL; 3088 vd->vdev_cant_read = B_FALSE; 3089 vd->vdev_cant_write = B_FALSE; 3090 3091 vdev_reopen(vd == rvd ? rvd : vd->vdev_top); 3092 3093 vd->vdev_forcefault = B_FALSE; 3094 3095 if (vd != rvd && vdev_writeable(vd->vdev_top)) 3096 vdev_state_dirty(vd->vdev_top); 3097 3098 if (vd->vdev_aux == NULL && !vdev_is_dead(vd)) 3099 spa_async_request(spa, SPA_ASYNC_RESILVER); 3100 3101 spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_CLEAR); 3102 } 3103 3104 /* 3105 * When clearing a FMA-diagnosed fault, we always want to 3106 * unspare the device, as we assume that the original spare was 3107 * done in response to the FMA fault. 3108 */ 3109 if (!vdev_is_dead(vd) && vd->vdev_parent != NULL && 3110 vd->vdev_parent->vdev_ops == &vdev_spare_ops && 3111 vd->vdev_parent->vdev_child[0] == vd) 3112 vd->vdev_unspare = B_TRUE; 3113 } 3114 3115 boolean_t 3116 vdev_is_dead(vdev_t *vd) 3117 { 3118 /* 3119 * Holes and missing devices are always considered "dead". 3120 * This simplifies the code since we don't have to check for 3121 * these types of devices in the various code paths. 3122 * Instead we rely on the fact that we skip over dead devices 3123 * before issuing I/O to them. 3124 */ 3125 return (vd->vdev_state < VDEV_STATE_DEGRADED || 3126 vd->vdev_ops == &vdev_hole_ops || 3127 vd->vdev_ops == &vdev_missing_ops); 3128 } 3129 3130 boolean_t 3131 vdev_readable(vdev_t *vd) 3132 { 3133 return (!vdev_is_dead(vd) && !vd->vdev_cant_read); 3134 } 3135 3136 boolean_t 3137 vdev_writeable(vdev_t *vd) 3138 { 3139 return (!vdev_is_dead(vd) && !vd->vdev_cant_write && 3140 vdev_is_concrete(vd)); 3141 } 3142 3143 boolean_t 3144 vdev_allocatable(vdev_t *vd) 3145 { 3146 uint64_t state = vd->vdev_state; 3147 3148 /* 3149 * We currently allow allocations from vdevs which may be in the 3150 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device 3151 * fails to reopen then we'll catch it later when we're holding 3152 * the proper locks. Note that we have to get the vdev state 3153 * in a local variable because although it changes atomically, 3154 * we're asking two separate questions about it. 3155 */ 3156 return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) && 3157 !vd->vdev_cant_write && vdev_is_concrete(vd) && 3158 vd->vdev_mg->mg_initialized); 3159 } 3160 3161 boolean_t 3162 vdev_accessible(vdev_t *vd, zio_t *zio) 3163 { 3164 ASSERT(zio->io_vd == vd); 3165 3166 if (vdev_is_dead(vd) || vd->vdev_remove_wanted) 3167 return (B_FALSE); 3168 3169 if (zio->io_type == ZIO_TYPE_READ) 3170 return (!vd->vdev_cant_read); 3171 3172 if (zio->io_type == ZIO_TYPE_WRITE) 3173 return (!vd->vdev_cant_write); 3174 3175 return (B_TRUE); 3176 } 3177 3178 /* 3179 * Get statistics for the given vdev. 3180 */ 3181 void 3182 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs) 3183 { 3184 spa_t *spa = vd->vdev_spa; 3185 vdev_t *rvd = spa->spa_root_vdev; 3186 vdev_t *tvd = vd->vdev_top; 3187 3188 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 3189 3190 mutex_enter(&vd->vdev_stat_lock); 3191 bcopy(&vd->vdev_stat, vs, sizeof (*vs)); 3192 vs->vs_timestamp = gethrtime() - vs->vs_timestamp; 3193 vs->vs_state = vd->vdev_state; 3194 vs->vs_rsize = vdev_get_min_asize(vd); 3195 if (vd->vdev_ops->vdev_op_leaf) 3196 vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE; 3197 /* 3198 * Report expandable space on top-level, non-auxillary devices only. 3199 * The expandable space is reported in terms of metaslab sized units 3200 * since that determines how much space the pool can expand. 3201 */ 3202 if (vd->vdev_aux == NULL && tvd != NULL) { 3203 vs->vs_esize = P2ALIGN(vd->vdev_max_asize - vd->vdev_asize - 3204 spa->spa_bootsize, 1ULL << tvd->vdev_ms_shift); 3205 } 3206 if (vd->vdev_aux == NULL && vd == vd->vdev_top && 3207 vdev_is_concrete(vd)) { 3208 vs->vs_fragmentation = vd->vdev_mg->mg_fragmentation; 3209 } 3210 3211 /* 3212 * If we're getting stats on the root vdev, aggregate the I/O counts 3213 * over all top-level vdevs (i.e. the direct children of the root). 3214 */ 3215 if (vd == rvd) { 3216 for (int c = 0; c < rvd->vdev_children; c++) { 3217 vdev_t *cvd = rvd->vdev_child[c]; 3218 vdev_stat_t *cvs = &cvd->vdev_stat; 3219 3220 for (int t = 0; t < ZIO_TYPES; t++) { 3221 vs->vs_ops[t] += cvs->vs_ops[t]; 3222 vs->vs_bytes[t] += cvs->vs_bytes[t]; 3223 } 3224 cvs->vs_scan_removing = cvd->vdev_removing; 3225 } 3226 } 3227 mutex_exit(&vd->vdev_stat_lock); 3228 } 3229 3230 void 3231 vdev_clear_stats(vdev_t *vd) 3232 { 3233 mutex_enter(&vd->vdev_stat_lock); 3234 vd->vdev_stat.vs_space = 0; 3235 vd->vdev_stat.vs_dspace = 0; 3236 vd->vdev_stat.vs_alloc = 0; 3237 mutex_exit(&vd->vdev_stat_lock); 3238 } 3239 3240 void 3241 vdev_scan_stat_init(vdev_t *vd) 3242 { 3243 vdev_stat_t *vs = &vd->vdev_stat; 3244 3245 for (int c = 0; c < vd->vdev_children; c++) 3246 vdev_scan_stat_init(vd->vdev_child[c]); 3247 3248 mutex_enter(&vd->vdev_stat_lock); 3249 vs->vs_scan_processed = 0; 3250 mutex_exit(&vd->vdev_stat_lock); 3251 } 3252 3253 void 3254 vdev_stat_update(zio_t *zio, uint64_t psize) 3255 { 3256 spa_t *spa = zio->io_spa; 3257 vdev_t *rvd = spa->spa_root_vdev; 3258 vdev_t *vd = zio->io_vd ? zio->io_vd : rvd; 3259 vdev_t *pvd; 3260 uint64_t txg = zio->io_txg; 3261 vdev_stat_t *vs = &vd->vdev_stat; 3262 zio_type_t type = zio->io_type; 3263 int flags = zio->io_flags; 3264 3265 /* 3266 * If this i/o is a gang leader, it didn't do any actual work. 3267 */ 3268 if (zio->io_gang_tree) 3269 return; 3270 3271 if (zio->io_error == 0) { 3272 /* 3273 * If this is a root i/o, don't count it -- we've already 3274 * counted the top-level vdevs, and vdev_get_stats() will 3275 * aggregate them when asked. This reduces contention on 3276 * the root vdev_stat_lock and implicitly handles blocks 3277 * that compress away to holes, for which there is no i/o. 3278 * (Holes never create vdev children, so all the counters 3279 * remain zero, which is what we want.) 3280 * 3281 * Note: this only applies to successful i/o (io_error == 0) 3282 * because unlike i/o counts, errors are not additive. 3283 * When reading a ditto block, for example, failure of 3284 * one top-level vdev does not imply a root-level error. 3285 */ 3286 if (vd == rvd) 3287 return; 3288 3289 ASSERT(vd == zio->io_vd); 3290 3291 if (flags & ZIO_FLAG_IO_BYPASS) 3292 return; 3293 3294 mutex_enter(&vd->vdev_stat_lock); 3295 3296 if (flags & ZIO_FLAG_IO_REPAIR) { 3297 if (flags & ZIO_FLAG_SCAN_THREAD) { 3298 dsl_scan_phys_t *scn_phys = 3299 &spa->spa_dsl_pool->dp_scan->scn_phys; 3300 uint64_t *processed = &scn_phys->scn_processed; 3301 3302 /* XXX cleanup? */ 3303 if (vd->vdev_ops->vdev_op_leaf) 3304 atomic_add_64(processed, psize); 3305 vs->vs_scan_processed += psize; 3306 } 3307 3308 if (flags & ZIO_FLAG_SELF_HEAL) 3309 vs->vs_self_healed += psize; 3310 } 3311 3312 vs->vs_ops[type]++; 3313 vs->vs_bytes[type] += psize; 3314 3315 mutex_exit(&vd->vdev_stat_lock); 3316 return; 3317 } 3318 3319 if (flags & ZIO_FLAG_SPECULATIVE) 3320 return; 3321 3322 /* 3323 * If this is an I/O error that is going to be retried, then ignore the 3324 * error. Otherwise, the user may interpret B_FAILFAST I/O errors as 3325 * hard errors, when in reality they can happen for any number of 3326 * innocuous reasons (bus resets, MPxIO link failure, etc). 3327 */ 3328 if (zio->io_error == EIO && 3329 !(zio->io_flags & ZIO_FLAG_IO_RETRY)) 3330 return; 3331 3332 /* 3333 * Intent logs writes won't propagate their error to the root 3334 * I/O so don't mark these types of failures as pool-level 3335 * errors. 3336 */ 3337 if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE)) 3338 return; 3339 3340 mutex_enter(&vd->vdev_stat_lock); 3341 if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) { 3342 if (zio->io_error == ECKSUM) 3343 vs->vs_checksum_errors++; 3344 else 3345 vs->vs_read_errors++; 3346 } 3347 if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd)) 3348 vs->vs_write_errors++; 3349 mutex_exit(&vd->vdev_stat_lock); 3350 3351 if (spa->spa_load_state == SPA_LOAD_NONE && 3352 type == ZIO_TYPE_WRITE && txg != 0 && 3353 (!(flags & ZIO_FLAG_IO_REPAIR) || 3354 (flags & ZIO_FLAG_SCAN_THREAD) || 3355 spa->spa_claiming)) { 3356 /* 3357 * This is either a normal write (not a repair), or it's 3358 * a repair induced by the scrub thread, or it's a repair 3359 * made by zil_claim() during spa_load() in the first txg. 3360 * In the normal case, we commit the DTL change in the same 3361 * txg as the block was born. In the scrub-induced repair 3362 * case, we know that scrubs run in first-pass syncing context, 3363 * so we commit the DTL change in spa_syncing_txg(spa). 3364 * In the zil_claim() case, we commit in spa_first_txg(spa). 3365 * 3366 * We currently do not make DTL entries for failed spontaneous 3367 * self-healing writes triggered by normal (non-scrubbing) 3368 * reads, because we have no transactional context in which to 3369 * do so -- and it's not clear that it'd be desirable anyway. 3370 */ 3371 if (vd->vdev_ops->vdev_op_leaf) { 3372 uint64_t commit_txg = txg; 3373 if (flags & ZIO_FLAG_SCAN_THREAD) { 3374 ASSERT(flags & ZIO_FLAG_IO_REPAIR); 3375 ASSERT(spa_sync_pass(spa) == 1); 3376 vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1); 3377 commit_txg = spa_syncing_txg(spa); 3378 } else if (spa->spa_claiming) { 3379 ASSERT(flags & ZIO_FLAG_IO_REPAIR); 3380 commit_txg = spa_first_txg(spa); 3381 } 3382 ASSERT(commit_txg >= spa_syncing_txg(spa)); 3383 if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1)) 3384 return; 3385 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 3386 vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1); 3387 vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg); 3388 } 3389 if (vd != rvd) 3390 vdev_dtl_dirty(vd, DTL_MISSING, txg, 1); 3391 } 3392 } 3393 3394 /* 3395 * Update the in-core space usage stats for this vdev, its metaslab class, 3396 * and the root vdev. 3397 */ 3398 void 3399 vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta, 3400 int64_t space_delta) 3401 { 3402 int64_t dspace_delta = space_delta; 3403 spa_t *spa = vd->vdev_spa; 3404 vdev_t *rvd = spa->spa_root_vdev; 3405 metaslab_group_t *mg = vd->vdev_mg; 3406 metaslab_class_t *mc = mg ? mg->mg_class : NULL; 3407 3408 ASSERT(vd == vd->vdev_top); 3409 3410 /* 3411 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion 3412 * factor. We must calculate this here and not at the root vdev 3413 * because the root vdev's psize-to-asize is simply the max of its 3414 * childrens', thus not accurate enough for us. 3415 */ 3416 ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0); 3417 ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache); 3418 dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) * 3419 vd->vdev_deflate_ratio; 3420 3421 mutex_enter(&vd->vdev_stat_lock); 3422 vd->vdev_stat.vs_alloc += alloc_delta; 3423 vd->vdev_stat.vs_space += space_delta; 3424 vd->vdev_stat.vs_dspace += dspace_delta; 3425 mutex_exit(&vd->vdev_stat_lock); 3426 3427 if (mc == spa_normal_class(spa)) { 3428 mutex_enter(&rvd->vdev_stat_lock); 3429 rvd->vdev_stat.vs_alloc += alloc_delta; 3430 rvd->vdev_stat.vs_space += space_delta; 3431 rvd->vdev_stat.vs_dspace += dspace_delta; 3432 mutex_exit(&rvd->vdev_stat_lock); 3433 } 3434 3435 if (mc != NULL) { 3436 ASSERT(rvd == vd->vdev_parent); 3437 ASSERT(vd->vdev_ms_count != 0); 3438 3439 metaslab_class_space_update(mc, 3440 alloc_delta, defer_delta, space_delta, dspace_delta); 3441 } 3442 } 3443 3444 /* 3445 * Mark a top-level vdev's config as dirty, placing it on the dirty list 3446 * so that it will be written out next time the vdev configuration is synced. 3447 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs. 3448 */ 3449 void 3450 vdev_config_dirty(vdev_t *vd) 3451 { 3452 spa_t *spa = vd->vdev_spa; 3453 vdev_t *rvd = spa->spa_root_vdev; 3454 int c; 3455 3456 ASSERT(spa_writeable(spa)); 3457 3458 /* 3459 * If this is an aux vdev (as with l2cache and spare devices), then we 3460 * update the vdev config manually and set the sync flag. 3461 */ 3462 if (vd->vdev_aux != NULL) { 3463 spa_aux_vdev_t *sav = vd->vdev_aux; 3464 nvlist_t **aux; 3465 uint_t naux; 3466 3467 for (c = 0; c < sav->sav_count; c++) { 3468 if (sav->sav_vdevs[c] == vd) 3469 break; 3470 } 3471 3472 if (c == sav->sav_count) { 3473 /* 3474 * We're being removed. There's nothing more to do. 3475 */ 3476 ASSERT(sav->sav_sync == B_TRUE); 3477 return; 3478 } 3479 3480 sav->sav_sync = B_TRUE; 3481 3482 if (nvlist_lookup_nvlist_array(sav->sav_config, 3483 ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) { 3484 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 3485 ZPOOL_CONFIG_SPARES, &aux, &naux) == 0); 3486 } 3487 3488 ASSERT(c < naux); 3489 3490 /* 3491 * Setting the nvlist in the middle if the array is a little 3492 * sketchy, but it will work. 3493 */ 3494 nvlist_free(aux[c]); 3495 aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0); 3496 3497 return; 3498 } 3499 3500 /* 3501 * The dirty list is protected by the SCL_CONFIG lock. The caller 3502 * must either hold SCL_CONFIG as writer, or must be the sync thread 3503 * (which holds SCL_CONFIG as reader). There's only one sync thread, 3504 * so this is sufficient to ensure mutual exclusion. 3505 */ 3506 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 3507 (dsl_pool_sync_context(spa_get_dsl(spa)) && 3508 spa_config_held(spa, SCL_CONFIG, RW_READER))); 3509 3510 if (vd == rvd) { 3511 for (c = 0; c < rvd->vdev_children; c++) 3512 vdev_config_dirty(rvd->vdev_child[c]); 3513 } else { 3514 ASSERT(vd == vd->vdev_top); 3515 3516 if (!list_link_active(&vd->vdev_config_dirty_node) && 3517 vdev_is_concrete(vd)) { 3518 list_insert_head(&spa->spa_config_dirty_list, vd); 3519 } 3520 } 3521 } 3522 3523 void 3524 vdev_config_clean(vdev_t *vd) 3525 { 3526 spa_t *spa = vd->vdev_spa; 3527 3528 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 3529 (dsl_pool_sync_context(spa_get_dsl(spa)) && 3530 spa_config_held(spa, SCL_CONFIG, RW_READER))); 3531 3532 ASSERT(list_link_active(&vd->vdev_config_dirty_node)); 3533 list_remove(&spa->spa_config_dirty_list, vd); 3534 } 3535 3536 /* 3537 * Mark a top-level vdev's state as dirty, so that the next pass of 3538 * spa_sync() can convert this into vdev_config_dirty(). We distinguish 3539 * the state changes from larger config changes because they require 3540 * much less locking, and are often needed for administrative actions. 3541 */ 3542 void 3543 vdev_state_dirty(vdev_t *vd) 3544 { 3545 spa_t *spa = vd->vdev_spa; 3546 3547 ASSERT(spa_writeable(spa)); 3548 ASSERT(vd == vd->vdev_top); 3549 3550 /* 3551 * The state list is protected by the SCL_STATE lock. The caller 3552 * must either hold SCL_STATE as writer, or must be the sync thread 3553 * (which holds SCL_STATE as reader). There's only one sync thread, 3554 * so this is sufficient to ensure mutual exclusion. 3555 */ 3556 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 3557 (dsl_pool_sync_context(spa_get_dsl(spa)) && 3558 spa_config_held(spa, SCL_STATE, RW_READER))); 3559 3560 if (!list_link_active(&vd->vdev_state_dirty_node) && 3561 vdev_is_concrete(vd)) 3562 list_insert_head(&spa->spa_state_dirty_list, vd); 3563 } 3564 3565 void 3566 vdev_state_clean(vdev_t *vd) 3567 { 3568 spa_t *spa = vd->vdev_spa; 3569 3570 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 3571 (dsl_pool_sync_context(spa_get_dsl(spa)) && 3572 spa_config_held(spa, SCL_STATE, RW_READER))); 3573 3574 ASSERT(list_link_active(&vd->vdev_state_dirty_node)); 3575 list_remove(&spa->spa_state_dirty_list, vd); 3576 } 3577 3578 /* 3579 * Propagate vdev state up from children to parent. 3580 */ 3581 void 3582 vdev_propagate_state(vdev_t *vd) 3583 { 3584 spa_t *spa = vd->vdev_spa; 3585 vdev_t *rvd = spa->spa_root_vdev; 3586 int degraded = 0, faulted = 0; 3587 int corrupted = 0; 3588 vdev_t *child; 3589 3590 if (vd->vdev_children > 0) { 3591 for (int c = 0; c < vd->vdev_children; c++) { 3592 child = vd->vdev_child[c]; 3593 3594 /* 3595 * Don't factor holes or indirect vdevs into the 3596 * decision. 3597 */ 3598 if (!vdev_is_concrete(child)) 3599 continue; 3600 3601 if (!vdev_readable(child) || 3602 (!vdev_writeable(child) && spa_writeable(spa))) { 3603 /* 3604 * Root special: if there is a top-level log 3605 * device, treat the root vdev as if it were 3606 * degraded. 3607 */ 3608 if (child->vdev_islog && vd == rvd) 3609 degraded++; 3610 else 3611 faulted++; 3612 } else if (child->vdev_state <= VDEV_STATE_DEGRADED) { 3613 degraded++; 3614 } 3615 3616 if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA) 3617 corrupted++; 3618 } 3619 3620 vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded); 3621 3622 /* 3623 * Root special: if there is a top-level vdev that cannot be 3624 * opened due to corrupted metadata, then propagate the root 3625 * vdev's aux state as 'corrupt' rather than 'insufficient 3626 * replicas'. 3627 */ 3628 if (corrupted && vd == rvd && 3629 rvd->vdev_state == VDEV_STATE_CANT_OPEN) 3630 vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN, 3631 VDEV_AUX_CORRUPT_DATA); 3632 } 3633 3634 if (vd->vdev_parent) 3635 vdev_propagate_state(vd->vdev_parent); 3636 } 3637 3638 /* 3639 * Set a vdev's state. If this is during an open, we don't update the parent 3640 * state, because we're in the process of opening children depth-first. 3641 * Otherwise, we propagate the change to the parent. 3642 * 3643 * If this routine places a device in a faulted state, an appropriate ereport is 3644 * generated. 3645 */ 3646 void 3647 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux) 3648 { 3649 uint64_t save_state; 3650 spa_t *spa = vd->vdev_spa; 3651 3652 if (state == vd->vdev_state) { 3653 vd->vdev_stat.vs_aux = aux; 3654 return; 3655 } 3656 3657 save_state = vd->vdev_state; 3658 3659 vd->vdev_state = state; 3660 vd->vdev_stat.vs_aux = aux; 3661 3662 /* 3663 * If we are setting the vdev state to anything but an open state, then 3664 * always close the underlying device unless the device has requested 3665 * a delayed close (i.e. we're about to remove or fault the device). 3666 * Otherwise, we keep accessible but invalid devices open forever. 3667 * We don't call vdev_close() itself, because that implies some extra 3668 * checks (offline, etc) that we don't want here. This is limited to 3669 * leaf devices, because otherwise closing the device will affect other 3670 * children. 3671 */ 3672 if (!vd->vdev_delayed_close && vdev_is_dead(vd) && 3673 vd->vdev_ops->vdev_op_leaf) 3674 vd->vdev_ops->vdev_op_close(vd); 3675 3676 /* 3677 * If we have brought this vdev back into service, we need 3678 * to notify fmd so that it can gracefully repair any outstanding 3679 * cases due to a missing device. We do this in all cases, even those 3680 * that probably don't correlate to a repaired fault. This is sure to 3681 * catch all cases, and we let the zfs-retire agent sort it out. If 3682 * this is a transient state it's OK, as the retire agent will 3683 * double-check the state of the vdev before repairing it. 3684 */ 3685 if (state == VDEV_STATE_HEALTHY && vd->vdev_ops->vdev_op_leaf && 3686 vd->vdev_prevstate != state) 3687 zfs_post_state_change(spa, vd); 3688 3689 if (vd->vdev_removed && 3690 state == VDEV_STATE_CANT_OPEN && 3691 (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) { 3692 /* 3693 * If the previous state is set to VDEV_STATE_REMOVED, then this 3694 * device was previously marked removed and someone attempted to 3695 * reopen it. If this failed due to a nonexistent device, then 3696 * keep the device in the REMOVED state. We also let this be if 3697 * it is one of our special test online cases, which is only 3698 * attempting to online the device and shouldn't generate an FMA 3699 * fault. 3700 */ 3701 vd->vdev_state = VDEV_STATE_REMOVED; 3702 vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 3703 } else if (state == VDEV_STATE_REMOVED) { 3704 vd->vdev_removed = B_TRUE; 3705 } else if (state == VDEV_STATE_CANT_OPEN) { 3706 /* 3707 * If we fail to open a vdev during an import or recovery, we 3708 * mark it as "not available", which signifies that it was 3709 * never there to begin with. Failure to open such a device 3710 * is not considered an error. 3711 */ 3712 if ((spa_load_state(spa) == SPA_LOAD_IMPORT || 3713 spa_load_state(spa) == SPA_LOAD_RECOVER) && 3714 vd->vdev_ops->vdev_op_leaf) 3715 vd->vdev_not_present = 1; 3716 3717 /* 3718 * Post the appropriate ereport. If the 'prevstate' field is 3719 * set to something other than VDEV_STATE_UNKNOWN, it indicates 3720 * that this is part of a vdev_reopen(). In this case, we don't 3721 * want to post the ereport if the device was already in the 3722 * CANT_OPEN state beforehand. 3723 * 3724 * If the 'checkremove' flag is set, then this is an attempt to 3725 * online the device in response to an insertion event. If we 3726 * hit this case, then we have detected an insertion event for a 3727 * faulted or offline device that wasn't in the removed state. 3728 * In this scenario, we don't post an ereport because we are 3729 * about to replace the device, or attempt an online with 3730 * vdev_forcefault, which will generate the fault for us. 3731 */ 3732 if ((vd->vdev_prevstate != state || vd->vdev_forcefault) && 3733 !vd->vdev_not_present && !vd->vdev_checkremove && 3734 vd != spa->spa_root_vdev) { 3735 const char *class; 3736 3737 switch (aux) { 3738 case VDEV_AUX_OPEN_FAILED: 3739 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED; 3740 break; 3741 case VDEV_AUX_CORRUPT_DATA: 3742 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA; 3743 break; 3744 case VDEV_AUX_NO_REPLICAS: 3745 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS; 3746 break; 3747 case VDEV_AUX_BAD_GUID_SUM: 3748 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM; 3749 break; 3750 case VDEV_AUX_TOO_SMALL: 3751 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL; 3752 break; 3753 case VDEV_AUX_BAD_LABEL: 3754 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL; 3755 break; 3756 default: 3757 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN; 3758 } 3759 3760 zfs_ereport_post(class, spa, vd, NULL, save_state, 0); 3761 } 3762 3763 /* Erase any notion of persistent removed state */ 3764 vd->vdev_removed = B_FALSE; 3765 } else { 3766 vd->vdev_removed = B_FALSE; 3767 } 3768 3769 if (!isopen && vd->vdev_parent) 3770 vdev_propagate_state(vd->vdev_parent); 3771 } 3772 3773 boolean_t 3774 vdev_children_are_offline(vdev_t *vd) 3775 { 3776 ASSERT(!vd->vdev_ops->vdev_op_leaf); 3777 3778 for (uint64_t i = 0; i < vd->vdev_children; i++) { 3779 if (vd->vdev_child[i]->vdev_state != VDEV_STATE_OFFLINE) 3780 return (B_FALSE); 3781 } 3782 3783 return (B_TRUE); 3784 } 3785 3786 /* 3787 * Check the vdev configuration to ensure that it's capable of supporting 3788 * a root pool. We do not support partial configuration. 3789 * In addition, only a single top-level vdev is allowed. 3790 */ 3791 boolean_t 3792 vdev_is_bootable(vdev_t *vd) 3793 { 3794 if (!vd->vdev_ops->vdev_op_leaf) { 3795 char *vdev_type = vd->vdev_ops->vdev_op_type; 3796 3797 if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 && 3798 vd->vdev_children > 1) { 3799 return (B_FALSE); 3800 } else if (strcmp(vdev_type, VDEV_TYPE_MISSING) == 0 || 3801 strcmp(vdev_type, VDEV_TYPE_INDIRECT) == 0) { 3802 return (B_FALSE); 3803 } 3804 } 3805 3806 for (int c = 0; c < vd->vdev_children; c++) { 3807 if (!vdev_is_bootable(vd->vdev_child[c])) 3808 return (B_FALSE); 3809 } 3810 return (B_TRUE); 3811 } 3812 3813 boolean_t 3814 vdev_is_concrete(vdev_t *vd) 3815 { 3816 vdev_ops_t *ops = vd->vdev_ops; 3817 if (ops == &vdev_indirect_ops || ops == &vdev_hole_ops || 3818 ops == &vdev_missing_ops || ops == &vdev_root_ops) { 3819 return (B_FALSE); 3820 } else { 3821 return (B_TRUE); 3822 } 3823 } 3824 3825 /* 3826 * Determine if a log device has valid content. If the vdev was 3827 * removed or faulted in the MOS config then we know that 3828 * the content on the log device has already been written to the pool. 3829 */ 3830 boolean_t 3831 vdev_log_state_valid(vdev_t *vd) 3832 { 3833 if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted && 3834 !vd->vdev_removed) 3835 return (B_TRUE); 3836 3837 for (int c = 0; c < vd->vdev_children; c++) 3838 if (vdev_log_state_valid(vd->vdev_child[c])) 3839 return (B_TRUE); 3840 3841 return (B_FALSE); 3842 } 3843 3844 /* 3845 * Expand a vdev if possible. 3846 */ 3847 void 3848 vdev_expand(vdev_t *vd, uint64_t txg) 3849 { 3850 ASSERT(vd->vdev_top == vd); 3851 ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 3852 3853 vdev_set_deflate_ratio(vd); 3854 3855 if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count && 3856 vdev_is_concrete(vd)) { 3857 VERIFY(vdev_metaslab_init(vd, txg) == 0); 3858 vdev_config_dirty(vd); 3859 } 3860 } 3861 3862 /* 3863 * Split a vdev. 3864 */ 3865 void 3866 vdev_split(vdev_t *vd) 3867 { 3868 vdev_t *cvd, *pvd = vd->vdev_parent; 3869 3870 vdev_remove_child(pvd, vd); 3871 vdev_compact_children(pvd); 3872 3873 cvd = pvd->vdev_child[0]; 3874 if (pvd->vdev_children == 1) { 3875 vdev_remove_parent(cvd); 3876 cvd->vdev_splitting = B_TRUE; 3877 } 3878 vdev_propagate_state(cvd); 3879 } 3880 3881 void 3882 vdev_deadman(vdev_t *vd) 3883 { 3884 for (int c = 0; c < vd->vdev_children; c++) { 3885 vdev_t *cvd = vd->vdev_child[c]; 3886 3887 vdev_deadman(cvd); 3888 } 3889 3890 if (vd->vdev_ops->vdev_op_leaf) { 3891 vdev_queue_t *vq = &vd->vdev_queue; 3892 3893 mutex_enter(&vq->vq_lock); 3894 if (avl_numnodes(&vq->vq_active_tree) > 0) { 3895 spa_t *spa = vd->vdev_spa; 3896 zio_t *fio; 3897 uint64_t delta; 3898 3899 /* 3900 * Look at the head of all the pending queues, 3901 * if any I/O has been outstanding for longer than 3902 * the spa_deadman_synctime we panic the system. 3903 */ 3904 fio = avl_first(&vq->vq_active_tree); 3905 delta = gethrtime() - fio->io_timestamp; 3906 if (delta > spa_deadman_synctime(spa)) { 3907 vdev_dbgmsg(vd, "SLOW IO: zio timestamp " 3908 "%lluns, delta %lluns, last io %lluns", 3909 fio->io_timestamp, (u_longlong_t)delta, 3910 vq->vq_io_complete_ts); 3911 fm_panic("I/O to pool '%s' appears to be " 3912 "hung.", spa_name(spa)); 3913 } 3914 } 3915 mutex_exit(&vq->vq_lock); 3916 } 3917 } 3918