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