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, 2021 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 * Copyright (c) 2017, Intel Corporation. 30 * Copyright (c) 2019, Datto Inc. All rights reserved. 31 * Copyright [2021] Hewlett Packard Enterprise Development LP 32 */ 33 34 #include <sys/zfs_context.h> 35 #include <sys/fm/fs/zfs.h> 36 #include <sys/spa.h> 37 #include <sys/spa_impl.h> 38 #include <sys/bpobj.h> 39 #include <sys/dmu.h> 40 #include <sys/dmu_tx.h> 41 #include <sys/dsl_dir.h> 42 #include <sys/vdev_impl.h> 43 #include <sys/vdev_rebuild.h> 44 #include <sys/vdev_draid.h> 45 #include <sys/uberblock_impl.h> 46 #include <sys/metaslab.h> 47 #include <sys/metaslab_impl.h> 48 #include <sys/space_map.h> 49 #include <sys/space_reftree.h> 50 #include <sys/zio.h> 51 #include <sys/zap.h> 52 #include <sys/fs/zfs.h> 53 #include <sys/arc.h> 54 #include <sys/zil.h> 55 #include <sys/dsl_scan.h> 56 #include <sys/vdev_raidz.h> 57 #include <sys/abd.h> 58 #include <sys/vdev_initialize.h> 59 #include <sys/vdev_trim.h> 60 #include <sys/zvol.h> 61 #include <sys/zfs_ratelimit.h> 62 63 /* 64 * One metaslab from each (normal-class) vdev is used by the ZIL. These are 65 * called "embedded slog metaslabs", are referenced by vdev_log_mg, and are 66 * part of the spa_embedded_log_class. The metaslab with the most free space 67 * in each vdev is selected for this purpose when the pool is opened (or a 68 * vdev is added). See vdev_metaslab_init(). 69 * 70 * Log blocks can be allocated from the following locations. Each one is tried 71 * in order until the allocation succeeds: 72 * 1. dedicated log vdevs, aka "slog" (spa_log_class) 73 * 2. embedded slog metaslabs (spa_embedded_log_class) 74 * 3. other metaslabs in normal vdevs (spa_normal_class) 75 * 76 * zfs_embedded_slog_min_ms disables the embedded slog if there are fewer 77 * than this number of metaslabs in the vdev. This ensures that we don't set 78 * aside an unreasonable amount of space for the ZIL. If set to less than 79 * 1 << (spa_slop_shift + 1), on small pools the usable space may be reduced 80 * (by more than 1<<spa_slop_shift) due to the embedded slog metaslab. 81 */ 82 int zfs_embedded_slog_min_ms = 64; 83 84 /* default target for number of metaslabs per top-level vdev */ 85 int zfs_vdev_default_ms_count = 200; 86 87 /* minimum number of metaslabs per top-level vdev */ 88 int zfs_vdev_min_ms_count = 16; 89 90 /* practical upper limit of total metaslabs per top-level vdev */ 91 int zfs_vdev_ms_count_limit = 1ULL << 17; 92 93 /* lower limit for metaslab size (512M) */ 94 int zfs_vdev_default_ms_shift = 29; 95 96 /* upper limit for metaslab size (16G) */ 97 int zfs_vdev_max_ms_shift = 34; 98 99 int vdev_validate_skip = B_FALSE; 100 101 /* 102 * Since the DTL space map of a vdev is not expected to have a lot of 103 * entries, we default its block size to 4K. 104 */ 105 int zfs_vdev_dtl_sm_blksz = (1 << 12); 106 107 /* 108 * Rate limit slow IO (delay) events to this many per second. 109 */ 110 unsigned int zfs_slow_io_events_per_second = 20; 111 112 /* 113 * Rate limit checksum events after this many checksum errors per second. 114 */ 115 unsigned int zfs_checksum_events_per_second = 20; 116 117 /* 118 * Ignore errors during scrub/resilver. Allows to work around resilver 119 * upon import when there are pool errors. 120 */ 121 int zfs_scan_ignore_errors = 0; 122 123 /* 124 * vdev-wide space maps that have lots of entries written to them at 125 * the end of each transaction can benefit from a higher I/O bandwidth 126 * (e.g. vdev_obsolete_sm), thus we default their block size to 128K. 127 */ 128 int zfs_vdev_standard_sm_blksz = (1 << 17); 129 130 /* 131 * Tunable parameter for debugging or performance analysis. Setting this 132 * will cause pool corruption on power loss if a volatile out-of-order 133 * write cache is enabled. 134 */ 135 int zfs_nocacheflush = 0; 136 137 uint64_t zfs_vdev_max_auto_ashift = ASHIFT_MAX; 138 uint64_t zfs_vdev_min_auto_ashift = ASHIFT_MIN; 139 140 void 141 vdev_dbgmsg(vdev_t *vd, const char *fmt, ...) 142 { 143 va_list adx; 144 char buf[256]; 145 146 va_start(adx, fmt); 147 (void) vsnprintf(buf, sizeof (buf), fmt, adx); 148 va_end(adx); 149 150 if (vd->vdev_path != NULL) { 151 zfs_dbgmsg("%s vdev '%s': %s", vd->vdev_ops->vdev_op_type, 152 vd->vdev_path, buf); 153 } else { 154 zfs_dbgmsg("%s-%llu vdev (guid %llu): %s", 155 vd->vdev_ops->vdev_op_type, 156 (u_longlong_t)vd->vdev_id, 157 (u_longlong_t)vd->vdev_guid, buf); 158 } 159 } 160 161 void 162 vdev_dbgmsg_print_tree(vdev_t *vd, int indent) 163 { 164 char state[20]; 165 166 if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) { 167 zfs_dbgmsg("%*svdev %llu: %s", indent, "", 168 (u_longlong_t)vd->vdev_id, 169 vd->vdev_ops->vdev_op_type); 170 return; 171 } 172 173 switch (vd->vdev_state) { 174 case VDEV_STATE_UNKNOWN: 175 (void) snprintf(state, sizeof (state), "unknown"); 176 break; 177 case VDEV_STATE_CLOSED: 178 (void) snprintf(state, sizeof (state), "closed"); 179 break; 180 case VDEV_STATE_OFFLINE: 181 (void) snprintf(state, sizeof (state), "offline"); 182 break; 183 case VDEV_STATE_REMOVED: 184 (void) snprintf(state, sizeof (state), "removed"); 185 break; 186 case VDEV_STATE_CANT_OPEN: 187 (void) snprintf(state, sizeof (state), "can't open"); 188 break; 189 case VDEV_STATE_FAULTED: 190 (void) snprintf(state, sizeof (state), "faulted"); 191 break; 192 case VDEV_STATE_DEGRADED: 193 (void) snprintf(state, sizeof (state), "degraded"); 194 break; 195 case VDEV_STATE_HEALTHY: 196 (void) snprintf(state, sizeof (state), "healthy"); 197 break; 198 default: 199 (void) snprintf(state, sizeof (state), "<state %u>", 200 (uint_t)vd->vdev_state); 201 } 202 203 zfs_dbgmsg("%*svdev %u: %s%s, guid: %llu, path: %s, %s", indent, 204 "", (int)vd->vdev_id, vd->vdev_ops->vdev_op_type, 205 vd->vdev_islog ? " (log)" : "", 206 (u_longlong_t)vd->vdev_guid, 207 vd->vdev_path ? vd->vdev_path : "N/A", state); 208 209 for (uint64_t i = 0; i < vd->vdev_children; i++) 210 vdev_dbgmsg_print_tree(vd->vdev_child[i], indent + 2); 211 } 212 213 /* 214 * Virtual device management. 215 */ 216 217 static vdev_ops_t *vdev_ops_table[] = { 218 &vdev_root_ops, 219 &vdev_raidz_ops, 220 &vdev_draid_ops, 221 &vdev_draid_spare_ops, 222 &vdev_mirror_ops, 223 &vdev_replacing_ops, 224 &vdev_spare_ops, 225 &vdev_disk_ops, 226 &vdev_file_ops, 227 &vdev_missing_ops, 228 &vdev_hole_ops, 229 &vdev_indirect_ops, 230 NULL 231 }; 232 233 /* 234 * Given a vdev type, return the appropriate ops vector. 235 */ 236 static vdev_ops_t * 237 vdev_getops(const char *type) 238 { 239 vdev_ops_t *ops, **opspp; 240 241 for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++) 242 if (strcmp(ops->vdev_op_type, type) == 0) 243 break; 244 245 return (ops); 246 } 247 248 /* 249 * Given a vdev and a metaslab class, find which metaslab group we're 250 * interested in. All vdevs may belong to two different metaslab classes. 251 * Dedicated slog devices use only the primary metaslab group, rather than a 252 * separate log group. For embedded slogs, the vdev_log_mg will be non-NULL. 253 */ 254 metaslab_group_t * 255 vdev_get_mg(vdev_t *vd, metaslab_class_t *mc) 256 { 257 if (mc == spa_embedded_log_class(vd->vdev_spa) && 258 vd->vdev_log_mg != NULL) 259 return (vd->vdev_log_mg); 260 else 261 return (vd->vdev_mg); 262 } 263 264 /* ARGSUSED */ 265 void 266 vdev_default_xlate(vdev_t *vd, const range_seg64_t *logical_rs, 267 range_seg64_t *physical_rs, range_seg64_t *remain_rs) 268 { 269 physical_rs->rs_start = logical_rs->rs_start; 270 physical_rs->rs_end = logical_rs->rs_end; 271 } 272 273 /* 274 * Derive the enumerated allocation bias from string input. 275 * String origin is either the per-vdev zap or zpool(8). 276 */ 277 static vdev_alloc_bias_t 278 vdev_derive_alloc_bias(const char *bias) 279 { 280 vdev_alloc_bias_t alloc_bias = VDEV_BIAS_NONE; 281 282 if (strcmp(bias, VDEV_ALLOC_BIAS_LOG) == 0) 283 alloc_bias = VDEV_BIAS_LOG; 284 else if (strcmp(bias, VDEV_ALLOC_BIAS_SPECIAL) == 0) 285 alloc_bias = VDEV_BIAS_SPECIAL; 286 else if (strcmp(bias, VDEV_ALLOC_BIAS_DEDUP) == 0) 287 alloc_bias = VDEV_BIAS_DEDUP; 288 289 return (alloc_bias); 290 } 291 292 /* 293 * Default asize function: return the MAX of psize with the asize of 294 * all children. This is what's used by anything other than RAID-Z. 295 */ 296 uint64_t 297 vdev_default_asize(vdev_t *vd, uint64_t psize) 298 { 299 uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift); 300 uint64_t csize; 301 302 for (int c = 0; c < vd->vdev_children; c++) { 303 csize = vdev_psize_to_asize(vd->vdev_child[c], psize); 304 asize = MAX(asize, csize); 305 } 306 307 return (asize); 308 } 309 310 uint64_t 311 vdev_default_min_asize(vdev_t *vd) 312 { 313 return (vd->vdev_min_asize); 314 } 315 316 /* 317 * Get the minimum allocatable size. We define the allocatable size as 318 * the vdev's asize rounded to the nearest metaslab. This allows us to 319 * replace or attach devices which don't have the same physical size but 320 * can still satisfy the same number of allocations. 321 */ 322 uint64_t 323 vdev_get_min_asize(vdev_t *vd) 324 { 325 vdev_t *pvd = vd->vdev_parent; 326 327 /* 328 * If our parent is NULL (inactive spare or cache) or is the root, 329 * just return our own asize. 330 */ 331 if (pvd == NULL) 332 return (vd->vdev_asize); 333 334 /* 335 * The top-level vdev just returns the allocatable size rounded 336 * to the nearest metaslab. 337 */ 338 if (vd == vd->vdev_top) 339 return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift)); 340 341 return (pvd->vdev_ops->vdev_op_min_asize(pvd)); 342 } 343 344 void 345 vdev_set_min_asize(vdev_t *vd) 346 { 347 vd->vdev_min_asize = vdev_get_min_asize(vd); 348 349 for (int c = 0; c < vd->vdev_children; c++) 350 vdev_set_min_asize(vd->vdev_child[c]); 351 } 352 353 /* 354 * Get the minimal allocation size for the top-level vdev. 355 */ 356 uint64_t 357 vdev_get_min_alloc(vdev_t *vd) 358 { 359 uint64_t min_alloc = 1ULL << vd->vdev_ashift; 360 361 if (vd->vdev_ops->vdev_op_min_alloc != NULL) 362 min_alloc = vd->vdev_ops->vdev_op_min_alloc(vd); 363 364 return (min_alloc); 365 } 366 367 /* 368 * Get the parity level for a top-level vdev. 369 */ 370 uint64_t 371 vdev_get_nparity(vdev_t *vd) 372 { 373 uint64_t nparity = 0; 374 375 if (vd->vdev_ops->vdev_op_nparity != NULL) 376 nparity = vd->vdev_ops->vdev_op_nparity(vd); 377 378 return (nparity); 379 } 380 381 /* 382 * Get the number of data disks for a top-level vdev. 383 */ 384 uint64_t 385 vdev_get_ndisks(vdev_t *vd) 386 { 387 uint64_t ndisks = 1; 388 389 if (vd->vdev_ops->vdev_op_ndisks != NULL) 390 ndisks = vd->vdev_ops->vdev_op_ndisks(vd); 391 392 return (ndisks); 393 } 394 395 vdev_t * 396 vdev_lookup_top(spa_t *spa, uint64_t vdev) 397 { 398 vdev_t *rvd = spa->spa_root_vdev; 399 400 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 401 402 if (vdev < rvd->vdev_children) { 403 ASSERT(rvd->vdev_child[vdev] != NULL); 404 return (rvd->vdev_child[vdev]); 405 } 406 407 return (NULL); 408 } 409 410 vdev_t * 411 vdev_lookup_by_guid(vdev_t *vd, uint64_t guid) 412 { 413 vdev_t *mvd; 414 415 if (vd->vdev_guid == guid) 416 return (vd); 417 418 for (int c = 0; c < vd->vdev_children; c++) 419 if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) != 420 NULL) 421 return (mvd); 422 423 return (NULL); 424 } 425 426 static int 427 vdev_count_leaves_impl(vdev_t *vd) 428 { 429 int n = 0; 430 431 if (vd->vdev_ops->vdev_op_leaf) 432 return (1); 433 434 for (int c = 0; c < vd->vdev_children; c++) 435 n += vdev_count_leaves_impl(vd->vdev_child[c]); 436 437 return (n); 438 } 439 440 int 441 vdev_count_leaves(spa_t *spa) 442 { 443 int rc; 444 445 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 446 rc = vdev_count_leaves_impl(spa->spa_root_vdev); 447 spa_config_exit(spa, SCL_VDEV, FTAG); 448 449 return (rc); 450 } 451 452 void 453 vdev_add_child(vdev_t *pvd, vdev_t *cvd) 454 { 455 size_t oldsize, newsize; 456 uint64_t id = cvd->vdev_id; 457 vdev_t **newchild; 458 459 ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 460 ASSERT(cvd->vdev_parent == NULL); 461 462 cvd->vdev_parent = pvd; 463 464 if (pvd == NULL) 465 return; 466 467 ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL); 468 469 oldsize = pvd->vdev_children * sizeof (vdev_t *); 470 pvd->vdev_children = MAX(pvd->vdev_children, id + 1); 471 newsize = pvd->vdev_children * sizeof (vdev_t *); 472 473 newchild = kmem_alloc(newsize, KM_SLEEP); 474 if (pvd->vdev_child != NULL) { 475 bcopy(pvd->vdev_child, newchild, oldsize); 476 kmem_free(pvd->vdev_child, oldsize); 477 } 478 479 pvd->vdev_child = newchild; 480 pvd->vdev_child[id] = cvd; 481 482 cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd); 483 ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL); 484 485 /* 486 * Walk up all ancestors to update guid sum. 487 */ 488 for (; pvd != NULL; pvd = pvd->vdev_parent) 489 pvd->vdev_guid_sum += cvd->vdev_guid_sum; 490 491 if (cvd->vdev_ops->vdev_op_leaf) { 492 list_insert_head(&cvd->vdev_spa->spa_leaf_list, cvd); 493 cvd->vdev_spa->spa_leaf_list_gen++; 494 } 495 } 496 497 void 498 vdev_remove_child(vdev_t *pvd, vdev_t *cvd) 499 { 500 int c; 501 uint_t id = cvd->vdev_id; 502 503 ASSERT(cvd->vdev_parent == pvd); 504 505 if (pvd == NULL) 506 return; 507 508 ASSERT(id < pvd->vdev_children); 509 ASSERT(pvd->vdev_child[id] == cvd); 510 511 pvd->vdev_child[id] = NULL; 512 cvd->vdev_parent = NULL; 513 514 for (c = 0; c < pvd->vdev_children; c++) 515 if (pvd->vdev_child[c]) 516 break; 517 518 if (c == pvd->vdev_children) { 519 kmem_free(pvd->vdev_child, c * sizeof (vdev_t *)); 520 pvd->vdev_child = NULL; 521 pvd->vdev_children = 0; 522 } 523 524 if (cvd->vdev_ops->vdev_op_leaf) { 525 spa_t *spa = cvd->vdev_spa; 526 list_remove(&spa->spa_leaf_list, cvd); 527 spa->spa_leaf_list_gen++; 528 } 529 530 /* 531 * Walk up all ancestors to update guid sum. 532 */ 533 for (; pvd != NULL; pvd = pvd->vdev_parent) 534 pvd->vdev_guid_sum -= cvd->vdev_guid_sum; 535 } 536 537 /* 538 * Remove any holes in the child array. 539 */ 540 void 541 vdev_compact_children(vdev_t *pvd) 542 { 543 vdev_t **newchild, *cvd; 544 int oldc = pvd->vdev_children; 545 int newc; 546 547 ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 548 549 if (oldc == 0) 550 return; 551 552 for (int c = newc = 0; c < oldc; c++) 553 if (pvd->vdev_child[c]) 554 newc++; 555 556 if (newc > 0) { 557 newchild = kmem_zalloc(newc * sizeof (vdev_t *), KM_SLEEP); 558 559 for (int c = newc = 0; c < oldc; c++) { 560 if ((cvd = pvd->vdev_child[c]) != NULL) { 561 newchild[newc] = cvd; 562 cvd->vdev_id = newc++; 563 } 564 } 565 } else { 566 newchild = NULL; 567 } 568 569 kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *)); 570 pvd->vdev_child = newchild; 571 pvd->vdev_children = newc; 572 } 573 574 /* 575 * Allocate and minimally initialize a vdev_t. 576 */ 577 vdev_t * 578 vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops) 579 { 580 vdev_t *vd; 581 vdev_indirect_config_t *vic; 582 583 vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP); 584 vic = &vd->vdev_indirect_config; 585 586 if (spa->spa_root_vdev == NULL) { 587 ASSERT(ops == &vdev_root_ops); 588 spa->spa_root_vdev = vd; 589 spa->spa_load_guid = spa_generate_guid(NULL); 590 } 591 592 if (guid == 0 && ops != &vdev_hole_ops) { 593 if (spa->spa_root_vdev == vd) { 594 /* 595 * The root vdev's guid will also be the pool guid, 596 * which must be unique among all pools. 597 */ 598 guid = spa_generate_guid(NULL); 599 } else { 600 /* 601 * Any other vdev's guid must be unique within the pool. 602 */ 603 guid = spa_generate_guid(spa); 604 } 605 ASSERT(!spa_guid_exists(spa_guid(spa), guid)); 606 } 607 608 vd->vdev_spa = spa; 609 vd->vdev_id = id; 610 vd->vdev_guid = guid; 611 vd->vdev_guid_sum = guid; 612 vd->vdev_ops = ops; 613 vd->vdev_state = VDEV_STATE_CLOSED; 614 vd->vdev_ishole = (ops == &vdev_hole_ops); 615 vic->vic_prev_indirect_vdev = UINT64_MAX; 616 617 rw_init(&vd->vdev_indirect_rwlock, NULL, RW_DEFAULT, NULL); 618 mutex_init(&vd->vdev_obsolete_lock, NULL, MUTEX_DEFAULT, NULL); 619 vd->vdev_obsolete_segments = range_tree_create(NULL, RANGE_SEG64, NULL, 620 0, 0); 621 622 /* 623 * Initialize rate limit structs for events. We rate limit ZIO delay 624 * and checksum events so that we don't overwhelm ZED with thousands 625 * of events when a disk is acting up. 626 */ 627 zfs_ratelimit_init(&vd->vdev_delay_rl, &zfs_slow_io_events_per_second, 628 1); 629 zfs_ratelimit_init(&vd->vdev_deadman_rl, &zfs_slow_io_events_per_second, 630 1); 631 zfs_ratelimit_init(&vd->vdev_checksum_rl, 632 &zfs_checksum_events_per_second, 1); 633 634 list_link_init(&vd->vdev_config_dirty_node); 635 list_link_init(&vd->vdev_state_dirty_node); 636 list_link_init(&vd->vdev_initialize_node); 637 list_link_init(&vd->vdev_leaf_node); 638 list_link_init(&vd->vdev_trim_node); 639 640 mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_NOLOCKDEP, NULL); 641 mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL); 642 mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL); 643 mutex_init(&vd->vdev_scan_io_queue_lock, NULL, MUTEX_DEFAULT, NULL); 644 645 mutex_init(&vd->vdev_initialize_lock, NULL, MUTEX_DEFAULT, NULL); 646 mutex_init(&vd->vdev_initialize_io_lock, NULL, MUTEX_DEFAULT, NULL); 647 cv_init(&vd->vdev_initialize_cv, NULL, CV_DEFAULT, NULL); 648 cv_init(&vd->vdev_initialize_io_cv, NULL, CV_DEFAULT, NULL); 649 650 mutex_init(&vd->vdev_trim_lock, NULL, MUTEX_DEFAULT, NULL); 651 mutex_init(&vd->vdev_autotrim_lock, NULL, MUTEX_DEFAULT, NULL); 652 mutex_init(&vd->vdev_trim_io_lock, NULL, MUTEX_DEFAULT, NULL); 653 cv_init(&vd->vdev_trim_cv, NULL, CV_DEFAULT, NULL); 654 cv_init(&vd->vdev_autotrim_cv, NULL, CV_DEFAULT, NULL); 655 cv_init(&vd->vdev_trim_io_cv, NULL, CV_DEFAULT, NULL); 656 657 mutex_init(&vd->vdev_rebuild_lock, NULL, MUTEX_DEFAULT, NULL); 658 cv_init(&vd->vdev_rebuild_cv, NULL, CV_DEFAULT, NULL); 659 660 for (int t = 0; t < DTL_TYPES; t++) { 661 vd->vdev_dtl[t] = range_tree_create(NULL, RANGE_SEG64, NULL, 0, 662 0); 663 } 664 665 txg_list_create(&vd->vdev_ms_list, spa, 666 offsetof(struct metaslab, ms_txg_node)); 667 txg_list_create(&vd->vdev_dtl_list, spa, 668 offsetof(struct vdev, vdev_dtl_node)); 669 vd->vdev_stat.vs_timestamp = gethrtime(); 670 vdev_queue_init(vd); 671 vdev_cache_init(vd); 672 673 return (vd); 674 } 675 676 /* 677 * Allocate a new vdev. The 'alloctype' is used to control whether we are 678 * creating a new vdev or loading an existing one - the behavior is slightly 679 * different for each case. 680 */ 681 int 682 vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id, 683 int alloctype) 684 { 685 vdev_ops_t *ops; 686 char *type; 687 uint64_t guid = 0, islog; 688 vdev_t *vd; 689 vdev_indirect_config_t *vic; 690 char *tmp = NULL; 691 int rc; 692 vdev_alloc_bias_t alloc_bias = VDEV_BIAS_NONE; 693 boolean_t top_level = (parent && !parent->vdev_parent); 694 695 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 696 697 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0) 698 return (SET_ERROR(EINVAL)); 699 700 if ((ops = vdev_getops(type)) == NULL) 701 return (SET_ERROR(EINVAL)); 702 703 /* 704 * If this is a load, get the vdev guid from the nvlist. 705 * Otherwise, vdev_alloc_common() will generate one for us. 706 */ 707 if (alloctype == VDEV_ALLOC_LOAD) { 708 uint64_t label_id; 709 710 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) || 711 label_id != id) 712 return (SET_ERROR(EINVAL)); 713 714 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 715 return (SET_ERROR(EINVAL)); 716 } else if (alloctype == VDEV_ALLOC_SPARE) { 717 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 718 return (SET_ERROR(EINVAL)); 719 } else if (alloctype == VDEV_ALLOC_L2CACHE) { 720 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 721 return (SET_ERROR(EINVAL)); 722 } else if (alloctype == VDEV_ALLOC_ROOTPOOL) { 723 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 724 return (SET_ERROR(EINVAL)); 725 } 726 727 /* 728 * The first allocated vdev must be of type 'root'. 729 */ 730 if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL) 731 return (SET_ERROR(EINVAL)); 732 733 /* 734 * Determine whether we're a log vdev. 735 */ 736 islog = 0; 737 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog); 738 if (islog && spa_version(spa) < SPA_VERSION_SLOGS) 739 return (SET_ERROR(ENOTSUP)); 740 741 if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES) 742 return (SET_ERROR(ENOTSUP)); 743 744 if (top_level && alloctype == VDEV_ALLOC_ADD) { 745 char *bias; 746 747 /* 748 * If creating a top-level vdev, check for allocation 749 * classes input. 750 */ 751 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_ALLOCATION_BIAS, 752 &bias) == 0) { 753 alloc_bias = vdev_derive_alloc_bias(bias); 754 755 /* spa_vdev_add() expects feature to be enabled */ 756 if (spa->spa_load_state != SPA_LOAD_CREATE && 757 !spa_feature_is_enabled(spa, 758 SPA_FEATURE_ALLOCATION_CLASSES)) { 759 return (SET_ERROR(ENOTSUP)); 760 } 761 } 762 763 /* spa_vdev_add() expects feature to be enabled */ 764 if (ops == &vdev_draid_ops && 765 spa->spa_load_state != SPA_LOAD_CREATE && 766 !spa_feature_is_enabled(spa, SPA_FEATURE_DRAID)) { 767 return (SET_ERROR(ENOTSUP)); 768 } 769 } 770 771 /* 772 * Initialize the vdev specific data. This is done before calling 773 * vdev_alloc_common() since it may fail and this simplifies the 774 * error reporting and cleanup code paths. 775 */ 776 void *tsd = NULL; 777 if (ops->vdev_op_init != NULL) { 778 rc = ops->vdev_op_init(spa, nv, &tsd); 779 if (rc != 0) { 780 return (rc); 781 } 782 } 783 784 vd = vdev_alloc_common(spa, id, guid, ops); 785 vd->vdev_tsd = tsd; 786 vd->vdev_islog = islog; 787 788 if (top_level && alloc_bias != VDEV_BIAS_NONE) 789 vd->vdev_alloc_bias = alloc_bias; 790 791 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0) 792 vd->vdev_path = spa_strdup(vd->vdev_path); 793 794 /* 795 * ZPOOL_CONFIG_AUX_STATE = "external" means we previously forced a 796 * fault on a vdev and want it to persist across imports (like with 797 * zpool offline -f). 798 */ 799 rc = nvlist_lookup_string(nv, ZPOOL_CONFIG_AUX_STATE, &tmp); 800 if (rc == 0 && tmp != NULL && strcmp(tmp, "external") == 0) { 801 vd->vdev_stat.vs_aux = VDEV_AUX_EXTERNAL; 802 vd->vdev_faulted = 1; 803 vd->vdev_label_aux = VDEV_AUX_EXTERNAL; 804 } 805 806 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0) 807 vd->vdev_devid = spa_strdup(vd->vdev_devid); 808 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH, 809 &vd->vdev_physpath) == 0) 810 vd->vdev_physpath = spa_strdup(vd->vdev_physpath); 811 812 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH, 813 &vd->vdev_enc_sysfs_path) == 0) 814 vd->vdev_enc_sysfs_path = spa_strdup(vd->vdev_enc_sysfs_path); 815 816 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0) 817 vd->vdev_fru = spa_strdup(vd->vdev_fru); 818 819 /* 820 * Set the whole_disk property. If it's not specified, leave the value 821 * as -1. 822 */ 823 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 824 &vd->vdev_wholedisk) != 0) 825 vd->vdev_wholedisk = -1ULL; 826 827 vic = &vd->vdev_indirect_config; 828 829 ASSERT0(vic->vic_mapping_object); 830 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_OBJECT, 831 &vic->vic_mapping_object); 832 ASSERT0(vic->vic_births_object); 833 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_BIRTHS, 834 &vic->vic_births_object); 835 ASSERT3U(vic->vic_prev_indirect_vdev, ==, UINT64_MAX); 836 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_PREV_INDIRECT_VDEV, 837 &vic->vic_prev_indirect_vdev); 838 839 /* 840 * Look for the 'not present' flag. This will only be set if the device 841 * was not present at the time of import. 842 */ 843 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 844 &vd->vdev_not_present); 845 846 /* 847 * Get the alignment requirement. 848 */ 849 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift); 850 851 /* 852 * Retrieve the vdev creation time. 853 */ 854 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG, 855 &vd->vdev_crtxg); 856 857 /* 858 * If we're a top-level vdev, try to load the allocation parameters. 859 */ 860 if (top_level && 861 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) { 862 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY, 863 &vd->vdev_ms_array); 864 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT, 865 &vd->vdev_ms_shift); 866 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE, 867 &vd->vdev_asize); 868 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING, 869 &vd->vdev_removing); 870 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP, 871 &vd->vdev_top_zap); 872 } else { 873 ASSERT0(vd->vdev_top_zap); 874 } 875 876 if (top_level && alloctype != VDEV_ALLOC_ATTACH) { 877 ASSERT(alloctype == VDEV_ALLOC_LOAD || 878 alloctype == VDEV_ALLOC_ADD || 879 alloctype == VDEV_ALLOC_SPLIT || 880 alloctype == VDEV_ALLOC_ROOTPOOL); 881 /* Note: metaslab_group_create() is now deferred */ 882 } 883 884 if (vd->vdev_ops->vdev_op_leaf && 885 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) { 886 (void) nvlist_lookup_uint64(nv, 887 ZPOOL_CONFIG_VDEV_LEAF_ZAP, &vd->vdev_leaf_zap); 888 } else { 889 ASSERT0(vd->vdev_leaf_zap); 890 } 891 892 /* 893 * If we're a leaf vdev, try to load the DTL object and other state. 894 */ 895 896 if (vd->vdev_ops->vdev_op_leaf && 897 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE || 898 alloctype == VDEV_ALLOC_ROOTPOOL)) { 899 if (alloctype == VDEV_ALLOC_LOAD) { 900 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL, 901 &vd->vdev_dtl_object); 902 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE, 903 &vd->vdev_unspare); 904 } 905 906 if (alloctype == VDEV_ALLOC_ROOTPOOL) { 907 uint64_t spare = 0; 908 909 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 910 &spare) == 0 && spare) 911 spa_spare_add(vd); 912 } 913 914 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, 915 &vd->vdev_offline); 916 917 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG, 918 &vd->vdev_resilver_txg); 919 920 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REBUILD_TXG, 921 &vd->vdev_rebuild_txg); 922 923 if (nvlist_exists(nv, ZPOOL_CONFIG_RESILVER_DEFER)) 924 vdev_defer_resilver(vd); 925 926 /* 927 * In general, when importing a pool we want to ignore the 928 * persistent fault state, as the diagnosis made on another 929 * system may not be valid in the current context. The only 930 * exception is if we forced a vdev to a persistently faulted 931 * state with 'zpool offline -f'. The persistent fault will 932 * remain across imports until cleared. 933 * 934 * Local vdevs will remain in the faulted state. 935 */ 936 if (spa_load_state(spa) == SPA_LOAD_OPEN || 937 spa_load_state(spa) == SPA_LOAD_IMPORT) { 938 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, 939 &vd->vdev_faulted); 940 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED, 941 &vd->vdev_degraded); 942 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, 943 &vd->vdev_removed); 944 945 if (vd->vdev_faulted || vd->vdev_degraded) { 946 char *aux; 947 948 vd->vdev_label_aux = 949 VDEV_AUX_ERR_EXCEEDED; 950 if (nvlist_lookup_string(nv, 951 ZPOOL_CONFIG_AUX_STATE, &aux) == 0 && 952 strcmp(aux, "external") == 0) 953 vd->vdev_label_aux = VDEV_AUX_EXTERNAL; 954 else 955 vd->vdev_faulted = 0ULL; 956 } 957 } 958 } 959 960 /* 961 * Add ourselves to the parent's list of children. 962 */ 963 vdev_add_child(parent, vd); 964 965 *vdp = vd; 966 967 return (0); 968 } 969 970 void 971 vdev_free(vdev_t *vd) 972 { 973 spa_t *spa = vd->vdev_spa; 974 975 ASSERT3P(vd->vdev_initialize_thread, ==, NULL); 976 ASSERT3P(vd->vdev_trim_thread, ==, NULL); 977 ASSERT3P(vd->vdev_autotrim_thread, ==, NULL); 978 ASSERT3P(vd->vdev_rebuild_thread, ==, NULL); 979 980 /* 981 * Scan queues are normally destroyed at the end of a scan. If the 982 * queue exists here, that implies the vdev is being removed while 983 * the scan is still running. 984 */ 985 if (vd->vdev_scan_io_queue != NULL) { 986 mutex_enter(&vd->vdev_scan_io_queue_lock); 987 dsl_scan_io_queue_destroy(vd->vdev_scan_io_queue); 988 vd->vdev_scan_io_queue = NULL; 989 mutex_exit(&vd->vdev_scan_io_queue_lock); 990 } 991 992 /* 993 * vdev_free() implies closing the vdev first. This is simpler than 994 * trying to ensure complicated semantics for all callers. 995 */ 996 vdev_close(vd); 997 998 ASSERT(!list_link_active(&vd->vdev_config_dirty_node)); 999 ASSERT(!list_link_active(&vd->vdev_state_dirty_node)); 1000 1001 /* 1002 * Free all children. 1003 */ 1004 for (int c = 0; c < vd->vdev_children; c++) 1005 vdev_free(vd->vdev_child[c]); 1006 1007 ASSERT(vd->vdev_child == NULL); 1008 ASSERT(vd->vdev_guid_sum == vd->vdev_guid); 1009 1010 if (vd->vdev_ops->vdev_op_fini != NULL) 1011 vd->vdev_ops->vdev_op_fini(vd); 1012 1013 /* 1014 * Discard allocation state. 1015 */ 1016 if (vd->vdev_mg != NULL) { 1017 vdev_metaslab_fini(vd); 1018 metaslab_group_destroy(vd->vdev_mg); 1019 vd->vdev_mg = NULL; 1020 } 1021 if (vd->vdev_log_mg != NULL) { 1022 ASSERT0(vd->vdev_ms_count); 1023 metaslab_group_destroy(vd->vdev_log_mg); 1024 vd->vdev_log_mg = NULL; 1025 } 1026 1027 ASSERT0(vd->vdev_stat.vs_space); 1028 ASSERT0(vd->vdev_stat.vs_dspace); 1029 ASSERT0(vd->vdev_stat.vs_alloc); 1030 1031 /* 1032 * Remove this vdev from its parent's child list. 1033 */ 1034 vdev_remove_child(vd->vdev_parent, vd); 1035 1036 ASSERT(vd->vdev_parent == NULL); 1037 ASSERT(!list_link_active(&vd->vdev_leaf_node)); 1038 1039 /* 1040 * Clean up vdev structure. 1041 */ 1042 vdev_queue_fini(vd); 1043 vdev_cache_fini(vd); 1044 1045 if (vd->vdev_path) 1046 spa_strfree(vd->vdev_path); 1047 if (vd->vdev_devid) 1048 spa_strfree(vd->vdev_devid); 1049 if (vd->vdev_physpath) 1050 spa_strfree(vd->vdev_physpath); 1051 1052 if (vd->vdev_enc_sysfs_path) 1053 spa_strfree(vd->vdev_enc_sysfs_path); 1054 1055 if (vd->vdev_fru) 1056 spa_strfree(vd->vdev_fru); 1057 1058 if (vd->vdev_isspare) 1059 spa_spare_remove(vd); 1060 if (vd->vdev_isl2cache) 1061 spa_l2cache_remove(vd); 1062 1063 txg_list_destroy(&vd->vdev_ms_list); 1064 txg_list_destroy(&vd->vdev_dtl_list); 1065 1066 mutex_enter(&vd->vdev_dtl_lock); 1067 space_map_close(vd->vdev_dtl_sm); 1068 for (int t = 0; t < DTL_TYPES; t++) { 1069 range_tree_vacate(vd->vdev_dtl[t], NULL, NULL); 1070 range_tree_destroy(vd->vdev_dtl[t]); 1071 } 1072 mutex_exit(&vd->vdev_dtl_lock); 1073 1074 EQUIV(vd->vdev_indirect_births != NULL, 1075 vd->vdev_indirect_mapping != NULL); 1076 if (vd->vdev_indirect_births != NULL) { 1077 vdev_indirect_mapping_close(vd->vdev_indirect_mapping); 1078 vdev_indirect_births_close(vd->vdev_indirect_births); 1079 } 1080 1081 if (vd->vdev_obsolete_sm != NULL) { 1082 ASSERT(vd->vdev_removing || 1083 vd->vdev_ops == &vdev_indirect_ops); 1084 space_map_close(vd->vdev_obsolete_sm); 1085 vd->vdev_obsolete_sm = NULL; 1086 } 1087 range_tree_destroy(vd->vdev_obsolete_segments); 1088 rw_destroy(&vd->vdev_indirect_rwlock); 1089 mutex_destroy(&vd->vdev_obsolete_lock); 1090 1091 mutex_destroy(&vd->vdev_dtl_lock); 1092 mutex_destroy(&vd->vdev_stat_lock); 1093 mutex_destroy(&vd->vdev_probe_lock); 1094 mutex_destroy(&vd->vdev_scan_io_queue_lock); 1095 1096 mutex_destroy(&vd->vdev_initialize_lock); 1097 mutex_destroy(&vd->vdev_initialize_io_lock); 1098 cv_destroy(&vd->vdev_initialize_io_cv); 1099 cv_destroy(&vd->vdev_initialize_cv); 1100 1101 mutex_destroy(&vd->vdev_trim_lock); 1102 mutex_destroy(&vd->vdev_autotrim_lock); 1103 mutex_destroy(&vd->vdev_trim_io_lock); 1104 cv_destroy(&vd->vdev_trim_cv); 1105 cv_destroy(&vd->vdev_autotrim_cv); 1106 cv_destroy(&vd->vdev_trim_io_cv); 1107 1108 mutex_destroy(&vd->vdev_rebuild_lock); 1109 cv_destroy(&vd->vdev_rebuild_cv); 1110 1111 zfs_ratelimit_fini(&vd->vdev_delay_rl); 1112 zfs_ratelimit_fini(&vd->vdev_deadman_rl); 1113 zfs_ratelimit_fini(&vd->vdev_checksum_rl); 1114 1115 if (vd == spa->spa_root_vdev) 1116 spa->spa_root_vdev = NULL; 1117 1118 kmem_free(vd, sizeof (vdev_t)); 1119 } 1120 1121 /* 1122 * Transfer top-level vdev state from svd to tvd. 1123 */ 1124 static void 1125 vdev_top_transfer(vdev_t *svd, vdev_t *tvd) 1126 { 1127 spa_t *spa = svd->vdev_spa; 1128 metaslab_t *msp; 1129 vdev_t *vd; 1130 int t; 1131 1132 ASSERT(tvd == tvd->vdev_top); 1133 1134 tvd->vdev_pending_fastwrite = svd->vdev_pending_fastwrite; 1135 tvd->vdev_ms_array = svd->vdev_ms_array; 1136 tvd->vdev_ms_shift = svd->vdev_ms_shift; 1137 tvd->vdev_ms_count = svd->vdev_ms_count; 1138 tvd->vdev_top_zap = svd->vdev_top_zap; 1139 1140 svd->vdev_ms_array = 0; 1141 svd->vdev_ms_shift = 0; 1142 svd->vdev_ms_count = 0; 1143 svd->vdev_top_zap = 0; 1144 1145 if (tvd->vdev_mg) 1146 ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg); 1147 if (tvd->vdev_log_mg) 1148 ASSERT3P(tvd->vdev_log_mg, ==, svd->vdev_log_mg); 1149 tvd->vdev_mg = svd->vdev_mg; 1150 tvd->vdev_log_mg = svd->vdev_log_mg; 1151 tvd->vdev_ms = svd->vdev_ms; 1152 1153 svd->vdev_mg = NULL; 1154 svd->vdev_log_mg = NULL; 1155 svd->vdev_ms = NULL; 1156 1157 if (tvd->vdev_mg != NULL) 1158 tvd->vdev_mg->mg_vd = tvd; 1159 if (tvd->vdev_log_mg != NULL) 1160 tvd->vdev_log_mg->mg_vd = tvd; 1161 1162 tvd->vdev_checkpoint_sm = svd->vdev_checkpoint_sm; 1163 svd->vdev_checkpoint_sm = NULL; 1164 1165 tvd->vdev_alloc_bias = svd->vdev_alloc_bias; 1166 svd->vdev_alloc_bias = VDEV_BIAS_NONE; 1167 1168 tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc; 1169 tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space; 1170 tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace; 1171 1172 svd->vdev_stat.vs_alloc = 0; 1173 svd->vdev_stat.vs_space = 0; 1174 svd->vdev_stat.vs_dspace = 0; 1175 1176 /* 1177 * State which may be set on a top-level vdev that's in the 1178 * process of being removed. 1179 */ 1180 ASSERT0(tvd->vdev_indirect_config.vic_births_object); 1181 ASSERT0(tvd->vdev_indirect_config.vic_mapping_object); 1182 ASSERT3U(tvd->vdev_indirect_config.vic_prev_indirect_vdev, ==, -1ULL); 1183 ASSERT3P(tvd->vdev_indirect_mapping, ==, NULL); 1184 ASSERT3P(tvd->vdev_indirect_births, ==, NULL); 1185 ASSERT3P(tvd->vdev_obsolete_sm, ==, NULL); 1186 ASSERT0(tvd->vdev_removing); 1187 ASSERT0(tvd->vdev_rebuilding); 1188 tvd->vdev_removing = svd->vdev_removing; 1189 tvd->vdev_rebuilding = svd->vdev_rebuilding; 1190 tvd->vdev_rebuild_config = svd->vdev_rebuild_config; 1191 tvd->vdev_indirect_config = svd->vdev_indirect_config; 1192 tvd->vdev_indirect_mapping = svd->vdev_indirect_mapping; 1193 tvd->vdev_indirect_births = svd->vdev_indirect_births; 1194 range_tree_swap(&svd->vdev_obsolete_segments, 1195 &tvd->vdev_obsolete_segments); 1196 tvd->vdev_obsolete_sm = svd->vdev_obsolete_sm; 1197 svd->vdev_indirect_config.vic_mapping_object = 0; 1198 svd->vdev_indirect_config.vic_births_object = 0; 1199 svd->vdev_indirect_config.vic_prev_indirect_vdev = -1ULL; 1200 svd->vdev_indirect_mapping = NULL; 1201 svd->vdev_indirect_births = NULL; 1202 svd->vdev_obsolete_sm = NULL; 1203 svd->vdev_removing = 0; 1204 svd->vdev_rebuilding = 0; 1205 1206 for (t = 0; t < TXG_SIZE; t++) { 1207 while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL) 1208 (void) txg_list_add(&tvd->vdev_ms_list, msp, t); 1209 while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL) 1210 (void) txg_list_add(&tvd->vdev_dtl_list, vd, t); 1211 if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t)) 1212 (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t); 1213 } 1214 1215 if (list_link_active(&svd->vdev_config_dirty_node)) { 1216 vdev_config_clean(svd); 1217 vdev_config_dirty(tvd); 1218 } 1219 1220 if (list_link_active(&svd->vdev_state_dirty_node)) { 1221 vdev_state_clean(svd); 1222 vdev_state_dirty(tvd); 1223 } 1224 1225 tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio; 1226 svd->vdev_deflate_ratio = 0; 1227 1228 tvd->vdev_islog = svd->vdev_islog; 1229 svd->vdev_islog = 0; 1230 1231 dsl_scan_io_queue_vdev_xfer(svd, tvd); 1232 } 1233 1234 static void 1235 vdev_top_update(vdev_t *tvd, vdev_t *vd) 1236 { 1237 if (vd == NULL) 1238 return; 1239 1240 vd->vdev_top = tvd; 1241 1242 for (int c = 0; c < vd->vdev_children; c++) 1243 vdev_top_update(tvd, vd->vdev_child[c]); 1244 } 1245 1246 /* 1247 * Add a mirror/replacing vdev above an existing vdev. There is no need to 1248 * call .vdev_op_init() since mirror/replacing vdevs do not have private state. 1249 */ 1250 vdev_t * 1251 vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops) 1252 { 1253 spa_t *spa = cvd->vdev_spa; 1254 vdev_t *pvd = cvd->vdev_parent; 1255 vdev_t *mvd; 1256 1257 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1258 1259 mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops); 1260 1261 mvd->vdev_asize = cvd->vdev_asize; 1262 mvd->vdev_min_asize = cvd->vdev_min_asize; 1263 mvd->vdev_max_asize = cvd->vdev_max_asize; 1264 mvd->vdev_psize = cvd->vdev_psize; 1265 mvd->vdev_ashift = cvd->vdev_ashift; 1266 mvd->vdev_logical_ashift = cvd->vdev_logical_ashift; 1267 mvd->vdev_physical_ashift = cvd->vdev_physical_ashift; 1268 mvd->vdev_state = cvd->vdev_state; 1269 mvd->vdev_crtxg = cvd->vdev_crtxg; 1270 1271 vdev_remove_child(pvd, cvd); 1272 vdev_add_child(pvd, mvd); 1273 cvd->vdev_id = mvd->vdev_children; 1274 vdev_add_child(mvd, cvd); 1275 vdev_top_update(cvd->vdev_top, cvd->vdev_top); 1276 1277 if (mvd == mvd->vdev_top) 1278 vdev_top_transfer(cvd, mvd); 1279 1280 return (mvd); 1281 } 1282 1283 /* 1284 * Remove a 1-way mirror/replacing vdev from the tree. 1285 */ 1286 void 1287 vdev_remove_parent(vdev_t *cvd) 1288 { 1289 vdev_t *mvd = cvd->vdev_parent; 1290 vdev_t *pvd = mvd->vdev_parent; 1291 1292 ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1293 1294 ASSERT(mvd->vdev_children == 1); 1295 ASSERT(mvd->vdev_ops == &vdev_mirror_ops || 1296 mvd->vdev_ops == &vdev_replacing_ops || 1297 mvd->vdev_ops == &vdev_spare_ops); 1298 cvd->vdev_ashift = mvd->vdev_ashift; 1299 cvd->vdev_logical_ashift = mvd->vdev_logical_ashift; 1300 cvd->vdev_physical_ashift = mvd->vdev_physical_ashift; 1301 vdev_remove_child(mvd, cvd); 1302 vdev_remove_child(pvd, mvd); 1303 1304 /* 1305 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid. 1306 * Otherwise, we could have detached an offline device, and when we 1307 * go to import the pool we'll think we have two top-level vdevs, 1308 * instead of a different version of the same top-level vdev. 1309 */ 1310 if (mvd->vdev_top == mvd) { 1311 uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid; 1312 cvd->vdev_orig_guid = cvd->vdev_guid; 1313 cvd->vdev_guid += guid_delta; 1314 cvd->vdev_guid_sum += guid_delta; 1315 1316 /* 1317 * If pool not set for autoexpand, we need to also preserve 1318 * mvd's asize to prevent automatic expansion of cvd. 1319 * Otherwise if we are adjusting the mirror by attaching and 1320 * detaching children of non-uniform sizes, the mirror could 1321 * autoexpand, unexpectedly requiring larger devices to 1322 * re-establish the mirror. 1323 */ 1324 if (!cvd->vdev_spa->spa_autoexpand) 1325 cvd->vdev_asize = mvd->vdev_asize; 1326 } 1327 cvd->vdev_id = mvd->vdev_id; 1328 vdev_add_child(pvd, cvd); 1329 vdev_top_update(cvd->vdev_top, cvd->vdev_top); 1330 1331 if (cvd == cvd->vdev_top) 1332 vdev_top_transfer(mvd, cvd); 1333 1334 ASSERT(mvd->vdev_children == 0); 1335 vdev_free(mvd); 1336 } 1337 1338 void 1339 vdev_metaslab_group_create(vdev_t *vd) 1340 { 1341 spa_t *spa = vd->vdev_spa; 1342 1343 /* 1344 * metaslab_group_create was delayed until allocation bias was available 1345 */ 1346 if (vd->vdev_mg == NULL) { 1347 metaslab_class_t *mc; 1348 1349 if (vd->vdev_islog && vd->vdev_alloc_bias == VDEV_BIAS_NONE) 1350 vd->vdev_alloc_bias = VDEV_BIAS_LOG; 1351 1352 ASSERT3U(vd->vdev_islog, ==, 1353 (vd->vdev_alloc_bias == VDEV_BIAS_LOG)); 1354 1355 switch (vd->vdev_alloc_bias) { 1356 case VDEV_BIAS_LOG: 1357 mc = spa_log_class(spa); 1358 break; 1359 case VDEV_BIAS_SPECIAL: 1360 mc = spa_special_class(spa); 1361 break; 1362 case VDEV_BIAS_DEDUP: 1363 mc = spa_dedup_class(spa); 1364 break; 1365 default: 1366 mc = spa_normal_class(spa); 1367 } 1368 1369 vd->vdev_mg = metaslab_group_create(mc, vd, 1370 spa->spa_alloc_count); 1371 1372 if (!vd->vdev_islog) { 1373 vd->vdev_log_mg = metaslab_group_create( 1374 spa_embedded_log_class(spa), vd, 1); 1375 } 1376 1377 /* 1378 * The spa ashift min/max only apply for the normal metaslab 1379 * class. Class destination is late binding so ashift boundary 1380 * setting had to wait until now. 1381 */ 1382 if (vd->vdev_top == vd && vd->vdev_ashift != 0 && 1383 mc == spa_normal_class(spa) && vd->vdev_aux == NULL) { 1384 if (vd->vdev_ashift > spa->spa_max_ashift) 1385 spa->spa_max_ashift = vd->vdev_ashift; 1386 if (vd->vdev_ashift < spa->spa_min_ashift) 1387 spa->spa_min_ashift = vd->vdev_ashift; 1388 1389 uint64_t min_alloc = vdev_get_min_alloc(vd); 1390 if (min_alloc < spa->spa_min_alloc) 1391 spa->spa_min_alloc = min_alloc; 1392 } 1393 } 1394 } 1395 1396 int 1397 vdev_metaslab_init(vdev_t *vd, uint64_t txg) 1398 { 1399 spa_t *spa = vd->vdev_spa; 1400 uint64_t oldc = vd->vdev_ms_count; 1401 uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift; 1402 metaslab_t **mspp; 1403 int error; 1404 boolean_t expanding = (oldc != 0); 1405 1406 ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 1407 1408 /* 1409 * This vdev is not being allocated from yet or is a hole. 1410 */ 1411 if (vd->vdev_ms_shift == 0) 1412 return (0); 1413 1414 ASSERT(!vd->vdev_ishole); 1415 1416 ASSERT(oldc <= newc); 1417 1418 mspp = vmem_zalloc(newc * sizeof (*mspp), KM_SLEEP); 1419 1420 if (expanding) { 1421 bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp)); 1422 vmem_free(vd->vdev_ms, oldc * sizeof (*mspp)); 1423 } 1424 1425 vd->vdev_ms = mspp; 1426 vd->vdev_ms_count = newc; 1427 1428 for (uint64_t m = oldc; m < newc; m++) { 1429 uint64_t object = 0; 1430 /* 1431 * vdev_ms_array may be 0 if we are creating the "fake" 1432 * metaslabs for an indirect vdev for zdb's leak detection. 1433 * See zdb_leak_init(). 1434 */ 1435 if (txg == 0 && vd->vdev_ms_array != 0) { 1436 error = dmu_read(spa->spa_meta_objset, 1437 vd->vdev_ms_array, 1438 m * sizeof (uint64_t), sizeof (uint64_t), &object, 1439 DMU_READ_PREFETCH); 1440 if (error != 0) { 1441 vdev_dbgmsg(vd, "unable to read the metaslab " 1442 "array [error=%d]", error); 1443 return (error); 1444 } 1445 } 1446 1447 error = metaslab_init(vd->vdev_mg, m, object, txg, 1448 &(vd->vdev_ms[m])); 1449 if (error != 0) { 1450 vdev_dbgmsg(vd, "metaslab_init failed [error=%d]", 1451 error); 1452 return (error); 1453 } 1454 } 1455 1456 /* 1457 * Find the emptiest metaslab on the vdev and mark it for use for 1458 * embedded slog by moving it from the regular to the log metaslab 1459 * group. 1460 */ 1461 if (vd->vdev_mg->mg_class == spa_normal_class(spa) && 1462 vd->vdev_ms_count > zfs_embedded_slog_min_ms && 1463 avl_is_empty(&vd->vdev_log_mg->mg_metaslab_tree)) { 1464 uint64_t slog_msid = 0; 1465 uint64_t smallest = UINT64_MAX; 1466 1467 /* 1468 * Note, we only search the new metaslabs, because the old 1469 * (pre-existing) ones may be active (e.g. have non-empty 1470 * range_tree's), and we don't move them to the new 1471 * metaslab_t. 1472 */ 1473 for (uint64_t m = oldc; m < newc; m++) { 1474 uint64_t alloc = 1475 space_map_allocated(vd->vdev_ms[m]->ms_sm); 1476 if (alloc < smallest) { 1477 slog_msid = m; 1478 smallest = alloc; 1479 } 1480 } 1481 metaslab_t *slog_ms = vd->vdev_ms[slog_msid]; 1482 /* 1483 * The metaslab was marked as dirty at the end of 1484 * metaslab_init(). Remove it from the dirty list so that we 1485 * can uninitialize and reinitialize it to the new class. 1486 */ 1487 if (txg != 0) { 1488 (void) txg_list_remove_this(&vd->vdev_ms_list, 1489 slog_ms, txg); 1490 } 1491 uint64_t sm_obj = space_map_object(slog_ms->ms_sm); 1492 metaslab_fini(slog_ms); 1493 VERIFY0(metaslab_init(vd->vdev_log_mg, slog_msid, sm_obj, txg, 1494 &vd->vdev_ms[slog_msid])); 1495 } 1496 1497 if (txg == 0) 1498 spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER); 1499 1500 /* 1501 * If the vdev is being removed we don't activate 1502 * the metaslabs since we want to ensure that no new 1503 * allocations are performed on this device. 1504 */ 1505 if (!expanding && !vd->vdev_removing) { 1506 metaslab_group_activate(vd->vdev_mg); 1507 if (vd->vdev_log_mg != NULL) 1508 metaslab_group_activate(vd->vdev_log_mg); 1509 } 1510 1511 if (txg == 0) 1512 spa_config_exit(spa, SCL_ALLOC, FTAG); 1513 1514 /* 1515 * Regardless whether this vdev was just added or it is being 1516 * expanded, the metaslab count has changed. Recalculate the 1517 * block limit. 1518 */ 1519 spa_log_sm_set_blocklimit(spa); 1520 1521 return (0); 1522 } 1523 1524 void 1525 vdev_metaslab_fini(vdev_t *vd) 1526 { 1527 if (vd->vdev_checkpoint_sm != NULL) { 1528 ASSERT(spa_feature_is_active(vd->vdev_spa, 1529 SPA_FEATURE_POOL_CHECKPOINT)); 1530 space_map_close(vd->vdev_checkpoint_sm); 1531 /* 1532 * Even though we close the space map, we need to set its 1533 * pointer to NULL. The reason is that vdev_metaslab_fini() 1534 * may be called multiple times for certain operations 1535 * (i.e. when destroying a pool) so we need to ensure that 1536 * this clause never executes twice. This logic is similar 1537 * to the one used for the vdev_ms clause below. 1538 */ 1539 vd->vdev_checkpoint_sm = NULL; 1540 } 1541 1542 if (vd->vdev_ms != NULL) { 1543 metaslab_group_t *mg = vd->vdev_mg; 1544 1545 metaslab_group_passivate(mg); 1546 if (vd->vdev_log_mg != NULL) { 1547 ASSERT(!vd->vdev_islog); 1548 metaslab_group_passivate(vd->vdev_log_mg); 1549 } 1550 1551 uint64_t count = vd->vdev_ms_count; 1552 for (uint64_t m = 0; m < count; m++) { 1553 metaslab_t *msp = vd->vdev_ms[m]; 1554 if (msp != NULL) 1555 metaslab_fini(msp); 1556 } 1557 vmem_free(vd->vdev_ms, count * sizeof (metaslab_t *)); 1558 vd->vdev_ms = NULL; 1559 vd->vdev_ms_count = 0; 1560 1561 for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++) { 1562 ASSERT0(mg->mg_histogram[i]); 1563 if (vd->vdev_log_mg != NULL) 1564 ASSERT0(vd->vdev_log_mg->mg_histogram[i]); 1565 } 1566 } 1567 ASSERT0(vd->vdev_ms_count); 1568 ASSERT3U(vd->vdev_pending_fastwrite, ==, 0); 1569 } 1570 1571 typedef struct vdev_probe_stats { 1572 boolean_t vps_readable; 1573 boolean_t vps_writeable; 1574 int vps_flags; 1575 } vdev_probe_stats_t; 1576 1577 static void 1578 vdev_probe_done(zio_t *zio) 1579 { 1580 spa_t *spa = zio->io_spa; 1581 vdev_t *vd = zio->io_vd; 1582 vdev_probe_stats_t *vps = zio->io_private; 1583 1584 ASSERT(vd->vdev_probe_zio != NULL); 1585 1586 if (zio->io_type == ZIO_TYPE_READ) { 1587 if (zio->io_error == 0) 1588 vps->vps_readable = 1; 1589 if (zio->io_error == 0 && spa_writeable(spa)) { 1590 zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd, 1591 zio->io_offset, zio->io_size, zio->io_abd, 1592 ZIO_CHECKSUM_OFF, vdev_probe_done, vps, 1593 ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE)); 1594 } else { 1595 abd_free(zio->io_abd); 1596 } 1597 } else if (zio->io_type == ZIO_TYPE_WRITE) { 1598 if (zio->io_error == 0) 1599 vps->vps_writeable = 1; 1600 abd_free(zio->io_abd); 1601 } else if (zio->io_type == ZIO_TYPE_NULL) { 1602 zio_t *pio; 1603 zio_link_t *zl; 1604 1605 vd->vdev_cant_read |= !vps->vps_readable; 1606 vd->vdev_cant_write |= !vps->vps_writeable; 1607 1608 if (vdev_readable(vd) && 1609 (vdev_writeable(vd) || !spa_writeable(spa))) { 1610 zio->io_error = 0; 1611 } else { 1612 ASSERT(zio->io_error != 0); 1613 vdev_dbgmsg(vd, "failed probe"); 1614 (void) zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE, 1615 spa, vd, NULL, NULL, 0); 1616 zio->io_error = SET_ERROR(ENXIO); 1617 } 1618 1619 mutex_enter(&vd->vdev_probe_lock); 1620 ASSERT(vd->vdev_probe_zio == zio); 1621 vd->vdev_probe_zio = NULL; 1622 mutex_exit(&vd->vdev_probe_lock); 1623 1624 zl = NULL; 1625 while ((pio = zio_walk_parents(zio, &zl)) != NULL) 1626 if (!vdev_accessible(vd, pio)) 1627 pio->io_error = SET_ERROR(ENXIO); 1628 1629 kmem_free(vps, sizeof (*vps)); 1630 } 1631 } 1632 1633 /* 1634 * Determine whether this device is accessible. 1635 * 1636 * Read and write to several known locations: the pad regions of each 1637 * vdev label but the first, which we leave alone in case it contains 1638 * a VTOC. 1639 */ 1640 zio_t * 1641 vdev_probe(vdev_t *vd, zio_t *zio) 1642 { 1643 spa_t *spa = vd->vdev_spa; 1644 vdev_probe_stats_t *vps = NULL; 1645 zio_t *pio; 1646 1647 ASSERT(vd->vdev_ops->vdev_op_leaf); 1648 1649 /* 1650 * Don't probe the probe. 1651 */ 1652 if (zio && (zio->io_flags & ZIO_FLAG_PROBE)) 1653 return (NULL); 1654 1655 /* 1656 * To prevent 'probe storms' when a device fails, we create 1657 * just one probe i/o at a time. All zios that want to probe 1658 * this vdev will become parents of the probe io. 1659 */ 1660 mutex_enter(&vd->vdev_probe_lock); 1661 1662 if ((pio = vd->vdev_probe_zio) == NULL) { 1663 vps = kmem_zalloc(sizeof (*vps), KM_SLEEP); 1664 1665 vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE | 1666 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE | 1667 ZIO_FLAG_TRYHARD; 1668 1669 if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) { 1670 /* 1671 * vdev_cant_read and vdev_cant_write can only 1672 * transition from TRUE to FALSE when we have the 1673 * SCL_ZIO lock as writer; otherwise they can only 1674 * transition from FALSE to TRUE. This ensures that 1675 * any zio looking at these values can assume that 1676 * failures persist for the life of the I/O. That's 1677 * important because when a device has intermittent 1678 * connectivity problems, we want to ensure that 1679 * they're ascribed to the device (ENXIO) and not 1680 * the zio (EIO). 1681 * 1682 * Since we hold SCL_ZIO as writer here, clear both 1683 * values so the probe can reevaluate from first 1684 * principles. 1685 */ 1686 vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER; 1687 vd->vdev_cant_read = B_FALSE; 1688 vd->vdev_cant_write = B_FALSE; 1689 } 1690 1691 vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd, 1692 vdev_probe_done, vps, 1693 vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE); 1694 1695 /* 1696 * We can't change the vdev state in this context, so we 1697 * kick off an async task to do it on our behalf. 1698 */ 1699 if (zio != NULL) { 1700 vd->vdev_probe_wanted = B_TRUE; 1701 spa_async_request(spa, SPA_ASYNC_PROBE); 1702 } 1703 } 1704 1705 if (zio != NULL) 1706 zio_add_child(zio, pio); 1707 1708 mutex_exit(&vd->vdev_probe_lock); 1709 1710 if (vps == NULL) { 1711 ASSERT(zio != NULL); 1712 return (NULL); 1713 } 1714 1715 for (int l = 1; l < VDEV_LABELS; l++) { 1716 zio_nowait(zio_read_phys(pio, vd, 1717 vdev_label_offset(vd->vdev_psize, l, 1718 offsetof(vdev_label_t, vl_be)), VDEV_PAD_SIZE, 1719 abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE), 1720 ZIO_CHECKSUM_OFF, vdev_probe_done, vps, 1721 ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE)); 1722 } 1723 1724 if (zio == NULL) 1725 return (pio); 1726 1727 zio_nowait(pio); 1728 return (NULL); 1729 } 1730 1731 static void 1732 vdev_load_child(void *arg) 1733 { 1734 vdev_t *vd = arg; 1735 1736 vd->vdev_load_error = vdev_load(vd); 1737 } 1738 1739 static void 1740 vdev_open_child(void *arg) 1741 { 1742 vdev_t *vd = arg; 1743 1744 vd->vdev_open_thread = curthread; 1745 vd->vdev_open_error = vdev_open(vd); 1746 vd->vdev_open_thread = NULL; 1747 } 1748 1749 static boolean_t 1750 vdev_uses_zvols(vdev_t *vd) 1751 { 1752 #ifdef _KERNEL 1753 if (zvol_is_zvol(vd->vdev_path)) 1754 return (B_TRUE); 1755 #endif 1756 1757 for (int c = 0; c < vd->vdev_children; c++) 1758 if (vdev_uses_zvols(vd->vdev_child[c])) 1759 return (B_TRUE); 1760 1761 return (B_FALSE); 1762 } 1763 1764 /* 1765 * Returns B_TRUE if the passed child should be opened. 1766 */ 1767 static boolean_t 1768 vdev_default_open_children_func(vdev_t *vd) 1769 { 1770 return (B_TRUE); 1771 } 1772 1773 /* 1774 * Open the requested child vdevs. If any of the leaf vdevs are using 1775 * a ZFS volume then do the opens in a single thread. This avoids a 1776 * deadlock when the current thread is holding the spa_namespace_lock. 1777 */ 1778 static void 1779 vdev_open_children_impl(vdev_t *vd, vdev_open_children_func_t *open_func) 1780 { 1781 int children = vd->vdev_children; 1782 1783 taskq_t *tq = taskq_create("vdev_open", children, minclsyspri, 1784 children, children, TASKQ_PREPOPULATE); 1785 vd->vdev_nonrot = B_TRUE; 1786 1787 for (int c = 0; c < children; c++) { 1788 vdev_t *cvd = vd->vdev_child[c]; 1789 1790 if (open_func(cvd) == B_FALSE) 1791 continue; 1792 1793 if (tq == NULL || vdev_uses_zvols(vd)) { 1794 cvd->vdev_open_error = vdev_open(cvd); 1795 } else { 1796 VERIFY(taskq_dispatch(tq, vdev_open_child, 1797 cvd, TQ_SLEEP) != TASKQID_INVALID); 1798 } 1799 1800 vd->vdev_nonrot &= cvd->vdev_nonrot; 1801 } 1802 1803 if (tq != NULL) { 1804 taskq_wait(tq); 1805 taskq_destroy(tq); 1806 } 1807 } 1808 1809 /* 1810 * Open all child vdevs. 1811 */ 1812 void 1813 vdev_open_children(vdev_t *vd) 1814 { 1815 vdev_open_children_impl(vd, vdev_default_open_children_func); 1816 } 1817 1818 /* 1819 * Conditionally open a subset of child vdevs. 1820 */ 1821 void 1822 vdev_open_children_subset(vdev_t *vd, vdev_open_children_func_t *open_func) 1823 { 1824 vdev_open_children_impl(vd, open_func); 1825 } 1826 1827 /* 1828 * Compute the raidz-deflation ratio. Note, we hard-code 1829 * in 128k (1 << 17) because it is the "typical" blocksize. 1830 * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change, 1831 * otherwise it would inconsistently account for existing bp's. 1832 */ 1833 static void 1834 vdev_set_deflate_ratio(vdev_t *vd) 1835 { 1836 if (vd == vd->vdev_top && !vd->vdev_ishole && vd->vdev_ashift != 0) { 1837 vd->vdev_deflate_ratio = (1 << 17) / 1838 (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT); 1839 } 1840 } 1841 1842 /* 1843 * Maximize performance by inflating the configured ashift for top level 1844 * vdevs to be as close to the physical ashift as possible while maintaining 1845 * administrator defined limits and ensuring it doesn't go below the 1846 * logical ashift. 1847 */ 1848 static void 1849 vdev_ashift_optimize(vdev_t *vd) 1850 { 1851 ASSERT(vd == vd->vdev_top); 1852 1853 if (vd->vdev_ashift < vd->vdev_physical_ashift) { 1854 vd->vdev_ashift = MIN( 1855 MAX(zfs_vdev_max_auto_ashift, vd->vdev_ashift), 1856 MAX(zfs_vdev_min_auto_ashift, 1857 vd->vdev_physical_ashift)); 1858 } else { 1859 /* 1860 * If the logical and physical ashifts are the same, then 1861 * we ensure that the top-level vdev's ashift is not smaller 1862 * than our minimum ashift value. For the unusual case 1863 * where logical ashift > physical ashift, we can't cap 1864 * the calculated ashift based on max ashift as that 1865 * would cause failures. 1866 * We still check if we need to increase it to match 1867 * the min ashift. 1868 */ 1869 vd->vdev_ashift = MAX(zfs_vdev_min_auto_ashift, 1870 vd->vdev_ashift); 1871 } 1872 } 1873 1874 /* 1875 * Prepare a virtual device for access. 1876 */ 1877 int 1878 vdev_open(vdev_t *vd) 1879 { 1880 spa_t *spa = vd->vdev_spa; 1881 int error; 1882 uint64_t osize = 0; 1883 uint64_t max_osize = 0; 1884 uint64_t asize, max_asize, psize; 1885 uint64_t logical_ashift = 0; 1886 uint64_t physical_ashift = 0; 1887 1888 ASSERT(vd->vdev_open_thread == curthread || 1889 spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 1890 ASSERT(vd->vdev_state == VDEV_STATE_CLOSED || 1891 vd->vdev_state == VDEV_STATE_CANT_OPEN || 1892 vd->vdev_state == VDEV_STATE_OFFLINE); 1893 1894 vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 1895 vd->vdev_cant_read = B_FALSE; 1896 vd->vdev_cant_write = B_FALSE; 1897 vd->vdev_min_asize = vdev_get_min_asize(vd); 1898 1899 /* 1900 * If this vdev is not removed, check its fault status. If it's 1901 * faulted, bail out of the open. 1902 */ 1903 if (!vd->vdev_removed && vd->vdev_faulted) { 1904 ASSERT(vd->vdev_children == 0); 1905 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED || 1906 vd->vdev_label_aux == VDEV_AUX_EXTERNAL); 1907 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 1908 vd->vdev_label_aux); 1909 return (SET_ERROR(ENXIO)); 1910 } else if (vd->vdev_offline) { 1911 ASSERT(vd->vdev_children == 0); 1912 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE); 1913 return (SET_ERROR(ENXIO)); 1914 } 1915 1916 error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize, 1917 &logical_ashift, &physical_ashift); 1918 /* 1919 * Physical volume size should never be larger than its max size, unless 1920 * the disk has shrunk while we were reading it or the device is buggy 1921 * or damaged: either way it's not safe for use, bail out of the open. 1922 */ 1923 if (osize > max_osize) { 1924 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1925 VDEV_AUX_OPEN_FAILED); 1926 return (SET_ERROR(ENXIO)); 1927 } 1928 1929 /* 1930 * Reset the vdev_reopening flag so that we actually close 1931 * the vdev on error. 1932 */ 1933 vd->vdev_reopening = B_FALSE; 1934 if (zio_injection_enabled && error == 0) 1935 error = zio_handle_device_injection(vd, NULL, SET_ERROR(ENXIO)); 1936 1937 if (error) { 1938 if (vd->vdev_removed && 1939 vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED) 1940 vd->vdev_removed = B_FALSE; 1941 1942 if (vd->vdev_stat.vs_aux == VDEV_AUX_CHILDREN_OFFLINE) { 1943 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, 1944 vd->vdev_stat.vs_aux); 1945 } else { 1946 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1947 vd->vdev_stat.vs_aux); 1948 } 1949 return (error); 1950 } 1951 1952 vd->vdev_removed = B_FALSE; 1953 1954 /* 1955 * Recheck the faulted flag now that we have confirmed that 1956 * the vdev is accessible. If we're faulted, bail. 1957 */ 1958 if (vd->vdev_faulted) { 1959 ASSERT(vd->vdev_children == 0); 1960 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED || 1961 vd->vdev_label_aux == VDEV_AUX_EXTERNAL); 1962 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 1963 vd->vdev_label_aux); 1964 return (SET_ERROR(ENXIO)); 1965 } 1966 1967 if (vd->vdev_degraded) { 1968 ASSERT(vd->vdev_children == 0); 1969 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 1970 VDEV_AUX_ERR_EXCEEDED); 1971 } else { 1972 vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0); 1973 } 1974 1975 /* 1976 * For hole or missing vdevs we just return success. 1977 */ 1978 if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) 1979 return (0); 1980 1981 for (int c = 0; c < vd->vdev_children; c++) { 1982 if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) { 1983 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 1984 VDEV_AUX_NONE); 1985 break; 1986 } 1987 } 1988 1989 osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t)); 1990 max_osize = P2ALIGN(max_osize, (uint64_t)sizeof (vdev_label_t)); 1991 1992 if (vd->vdev_children == 0) { 1993 if (osize < SPA_MINDEVSIZE) { 1994 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1995 VDEV_AUX_TOO_SMALL); 1996 return (SET_ERROR(EOVERFLOW)); 1997 } 1998 psize = osize; 1999 asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE); 2000 max_asize = max_osize - (VDEV_LABEL_START_SIZE + 2001 VDEV_LABEL_END_SIZE); 2002 } else { 2003 if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE - 2004 (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) { 2005 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 2006 VDEV_AUX_TOO_SMALL); 2007 return (SET_ERROR(EOVERFLOW)); 2008 } 2009 psize = 0; 2010 asize = osize; 2011 max_asize = max_osize; 2012 } 2013 2014 /* 2015 * If the vdev was expanded, record this so that we can re-create the 2016 * uberblock rings in labels {2,3}, during the next sync. 2017 */ 2018 if ((psize > vd->vdev_psize) && (vd->vdev_psize != 0)) 2019 vd->vdev_copy_uberblocks = B_TRUE; 2020 2021 vd->vdev_psize = psize; 2022 2023 /* 2024 * Make sure the allocatable size hasn't shrunk too much. 2025 */ 2026 if (asize < vd->vdev_min_asize) { 2027 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 2028 VDEV_AUX_BAD_LABEL); 2029 return (SET_ERROR(EINVAL)); 2030 } 2031 2032 /* 2033 * We can always set the logical/physical ashift members since 2034 * their values are only used to calculate the vdev_ashift when 2035 * the device is first added to the config. These values should 2036 * not be used for anything else since they may change whenever 2037 * the device is reopened and we don't store them in the label. 2038 */ 2039 vd->vdev_physical_ashift = 2040 MAX(physical_ashift, vd->vdev_physical_ashift); 2041 vd->vdev_logical_ashift = MAX(logical_ashift, 2042 vd->vdev_logical_ashift); 2043 2044 if (vd->vdev_asize == 0) { 2045 /* 2046 * This is the first-ever open, so use the computed values. 2047 * For compatibility, a different ashift can be requested. 2048 */ 2049 vd->vdev_asize = asize; 2050 vd->vdev_max_asize = max_asize; 2051 2052 /* 2053 * If the vdev_ashift was not overridden at creation time, 2054 * then set it the logical ashift and optimize the ashift. 2055 */ 2056 if (vd->vdev_ashift == 0) { 2057 vd->vdev_ashift = vd->vdev_logical_ashift; 2058 2059 if (vd->vdev_logical_ashift > ASHIFT_MAX) { 2060 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 2061 VDEV_AUX_ASHIFT_TOO_BIG); 2062 return (SET_ERROR(EDOM)); 2063 } 2064 2065 if (vd->vdev_top == vd) { 2066 vdev_ashift_optimize(vd); 2067 } 2068 } 2069 if (vd->vdev_ashift != 0 && (vd->vdev_ashift < ASHIFT_MIN || 2070 vd->vdev_ashift > ASHIFT_MAX)) { 2071 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 2072 VDEV_AUX_BAD_ASHIFT); 2073 return (SET_ERROR(EDOM)); 2074 } 2075 } else { 2076 /* 2077 * Make sure the alignment required hasn't increased. 2078 */ 2079 if (vd->vdev_ashift > vd->vdev_top->vdev_ashift && 2080 vd->vdev_ops->vdev_op_leaf) { 2081 (void) zfs_ereport_post( 2082 FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT, 2083 spa, vd, NULL, NULL, 0); 2084 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 2085 VDEV_AUX_BAD_LABEL); 2086 return (SET_ERROR(EDOM)); 2087 } 2088 vd->vdev_max_asize = max_asize; 2089 } 2090 2091 /* 2092 * If all children are healthy we update asize if either: 2093 * The asize has increased, due to a device expansion caused by dynamic 2094 * LUN growth or vdev replacement, and automatic expansion is enabled; 2095 * making the additional space available. 2096 * 2097 * The asize has decreased, due to a device shrink usually caused by a 2098 * vdev replace with a smaller device. This ensures that calculations 2099 * based of max_asize and asize e.g. esize are always valid. It's safe 2100 * to do this as we've already validated that asize is greater than 2101 * vdev_min_asize. 2102 */ 2103 if (vd->vdev_state == VDEV_STATE_HEALTHY && 2104 ((asize > vd->vdev_asize && 2105 (vd->vdev_expanding || spa->spa_autoexpand)) || 2106 (asize < vd->vdev_asize))) 2107 vd->vdev_asize = asize; 2108 2109 vdev_set_min_asize(vd); 2110 2111 /* 2112 * Ensure we can issue some IO before declaring the 2113 * vdev open for business. 2114 */ 2115 if (vd->vdev_ops->vdev_op_leaf && 2116 (error = zio_wait(vdev_probe(vd, NULL))) != 0) { 2117 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 2118 VDEV_AUX_ERR_EXCEEDED); 2119 return (error); 2120 } 2121 2122 /* 2123 * Track the minimum allocation size. 2124 */ 2125 if (vd->vdev_top == vd && vd->vdev_ashift != 0 && 2126 vd->vdev_islog == 0 && vd->vdev_aux == NULL) { 2127 uint64_t min_alloc = vdev_get_min_alloc(vd); 2128 if (min_alloc < spa->spa_min_alloc) 2129 spa->spa_min_alloc = min_alloc; 2130 } 2131 2132 /* 2133 * If this is a leaf vdev, assess whether a resilver is needed. 2134 * But don't do this if we are doing a reopen for a scrub, since 2135 * this would just restart the scrub we are already doing. 2136 */ 2137 if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen) 2138 dsl_scan_assess_vdev(spa->spa_dsl_pool, vd); 2139 2140 return (0); 2141 } 2142 2143 static void 2144 vdev_validate_child(void *arg) 2145 { 2146 vdev_t *vd = arg; 2147 2148 vd->vdev_validate_thread = curthread; 2149 vd->vdev_validate_error = vdev_validate(vd); 2150 vd->vdev_validate_thread = NULL; 2151 } 2152 2153 /* 2154 * Called once the vdevs are all opened, this routine validates the label 2155 * contents. This needs to be done before vdev_load() so that we don't 2156 * inadvertently do repair I/Os to the wrong device. 2157 * 2158 * This function will only return failure if one of the vdevs indicates that it 2159 * has since been destroyed or exported. This is only possible if 2160 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state 2161 * will be updated but the function will return 0. 2162 */ 2163 int 2164 vdev_validate(vdev_t *vd) 2165 { 2166 spa_t *spa = vd->vdev_spa; 2167 taskq_t *tq = NULL; 2168 nvlist_t *label; 2169 uint64_t guid = 0, aux_guid = 0, top_guid; 2170 uint64_t state; 2171 nvlist_t *nvl; 2172 uint64_t txg; 2173 int children = vd->vdev_children; 2174 2175 if (vdev_validate_skip) 2176 return (0); 2177 2178 if (children > 0) { 2179 tq = taskq_create("vdev_validate", children, minclsyspri, 2180 children, children, TASKQ_PREPOPULATE); 2181 } 2182 2183 for (uint64_t c = 0; c < children; c++) { 2184 vdev_t *cvd = vd->vdev_child[c]; 2185 2186 if (tq == NULL || vdev_uses_zvols(cvd)) { 2187 vdev_validate_child(cvd); 2188 } else { 2189 VERIFY(taskq_dispatch(tq, vdev_validate_child, cvd, 2190 TQ_SLEEP) != TASKQID_INVALID); 2191 } 2192 } 2193 if (tq != NULL) { 2194 taskq_wait(tq); 2195 taskq_destroy(tq); 2196 } 2197 for (int c = 0; c < children; c++) { 2198 int error = vd->vdev_child[c]->vdev_validate_error; 2199 2200 if (error != 0) 2201 return (SET_ERROR(EBADF)); 2202 } 2203 2204 2205 /* 2206 * If the device has already failed, or was marked offline, don't do 2207 * any further validation. Otherwise, label I/O will fail and we will 2208 * overwrite the previous state. 2209 */ 2210 if (!vd->vdev_ops->vdev_op_leaf || !vdev_readable(vd)) 2211 return (0); 2212 2213 /* 2214 * If we are performing an extreme rewind, we allow for a label that 2215 * was modified at a point after the current txg. 2216 * If config lock is not held do not check for the txg. spa_sync could 2217 * be updating the vdev's label before updating spa_last_synced_txg. 2218 */ 2219 if (spa->spa_extreme_rewind || spa_last_synced_txg(spa) == 0 || 2220 spa_config_held(spa, SCL_CONFIG, RW_WRITER) != SCL_CONFIG) 2221 txg = UINT64_MAX; 2222 else 2223 txg = spa_last_synced_txg(spa); 2224 2225 if ((label = vdev_label_read_config(vd, txg)) == NULL) { 2226 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 2227 VDEV_AUX_BAD_LABEL); 2228 vdev_dbgmsg(vd, "vdev_validate: failed reading config for " 2229 "txg %llu", (u_longlong_t)txg); 2230 return (0); 2231 } 2232 2233 /* 2234 * Determine if this vdev has been split off into another 2235 * pool. If so, then refuse to open it. 2236 */ 2237 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID, 2238 &aux_guid) == 0 && aux_guid == spa_guid(spa)) { 2239 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 2240 VDEV_AUX_SPLIT_POOL); 2241 nvlist_free(label); 2242 vdev_dbgmsg(vd, "vdev_validate: vdev split into other pool"); 2243 return (0); 2244 } 2245 2246 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &guid) != 0) { 2247 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 2248 VDEV_AUX_CORRUPT_DATA); 2249 nvlist_free(label); 2250 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 2251 ZPOOL_CONFIG_POOL_GUID); 2252 return (0); 2253 } 2254 2255 /* 2256 * If config is not trusted then ignore the spa guid check. This is 2257 * necessary because if the machine crashed during a re-guid the new 2258 * guid might have been written to all of the vdev labels, but not the 2259 * cached config. The check will be performed again once we have the 2260 * trusted config from the MOS. 2261 */ 2262 if (spa->spa_trust_config && guid != spa_guid(spa)) { 2263 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 2264 VDEV_AUX_CORRUPT_DATA); 2265 nvlist_free(label); 2266 vdev_dbgmsg(vd, "vdev_validate: vdev label pool_guid doesn't " 2267 "match config (%llu != %llu)", (u_longlong_t)guid, 2268 (u_longlong_t)spa_guid(spa)); 2269 return (0); 2270 } 2271 2272 if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl) 2273 != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID, 2274 &aux_guid) != 0) 2275 aux_guid = 0; 2276 2277 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0) { 2278 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 2279 VDEV_AUX_CORRUPT_DATA); 2280 nvlist_free(label); 2281 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 2282 ZPOOL_CONFIG_GUID); 2283 return (0); 2284 } 2285 2286 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID, &top_guid) 2287 != 0) { 2288 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 2289 VDEV_AUX_CORRUPT_DATA); 2290 nvlist_free(label); 2291 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 2292 ZPOOL_CONFIG_TOP_GUID); 2293 return (0); 2294 } 2295 2296 /* 2297 * If this vdev just became a top-level vdev because its sibling was 2298 * detached, it will have adopted the parent's vdev guid -- but the 2299 * label may or may not be on disk yet. Fortunately, either version 2300 * of the label will have the same top guid, so if we're a top-level 2301 * vdev, we can safely compare to that instead. 2302 * However, if the config comes from a cachefile that failed to update 2303 * after the detach, a top-level vdev will appear as a non top-level 2304 * vdev in the config. Also relax the constraints if we perform an 2305 * extreme rewind. 2306 * 2307 * If we split this vdev off instead, then we also check the 2308 * original pool's guid. We don't want to consider the vdev 2309 * corrupt if it is partway through a split operation. 2310 */ 2311 if (vd->vdev_guid != guid && vd->vdev_guid != aux_guid) { 2312 boolean_t mismatch = B_FALSE; 2313 if (spa->spa_trust_config && !spa->spa_extreme_rewind) { 2314 if (vd != vd->vdev_top || vd->vdev_guid != top_guid) 2315 mismatch = B_TRUE; 2316 } else { 2317 if (vd->vdev_guid != top_guid && 2318 vd->vdev_top->vdev_guid != guid) 2319 mismatch = B_TRUE; 2320 } 2321 2322 if (mismatch) { 2323 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 2324 VDEV_AUX_CORRUPT_DATA); 2325 nvlist_free(label); 2326 vdev_dbgmsg(vd, "vdev_validate: config guid " 2327 "doesn't match label guid"); 2328 vdev_dbgmsg(vd, "CONFIG: guid %llu, top_guid %llu", 2329 (u_longlong_t)vd->vdev_guid, 2330 (u_longlong_t)vd->vdev_top->vdev_guid); 2331 vdev_dbgmsg(vd, "LABEL: guid %llu, top_guid %llu, " 2332 "aux_guid %llu", (u_longlong_t)guid, 2333 (u_longlong_t)top_guid, (u_longlong_t)aux_guid); 2334 return (0); 2335 } 2336 } 2337 2338 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, 2339 &state) != 0) { 2340 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 2341 VDEV_AUX_CORRUPT_DATA); 2342 nvlist_free(label); 2343 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 2344 ZPOOL_CONFIG_POOL_STATE); 2345 return (0); 2346 } 2347 2348 nvlist_free(label); 2349 2350 /* 2351 * If this is a verbatim import, no need to check the 2352 * state of the pool. 2353 */ 2354 if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) && 2355 spa_load_state(spa) == SPA_LOAD_OPEN && 2356 state != POOL_STATE_ACTIVE) { 2357 vdev_dbgmsg(vd, "vdev_validate: invalid pool state (%llu) " 2358 "for spa %s", (u_longlong_t)state, spa->spa_name); 2359 return (SET_ERROR(EBADF)); 2360 } 2361 2362 /* 2363 * If we were able to open and validate a vdev that was 2364 * previously marked permanently unavailable, clear that state 2365 * now. 2366 */ 2367 if (vd->vdev_not_present) 2368 vd->vdev_not_present = 0; 2369 2370 return (0); 2371 } 2372 2373 static void 2374 vdev_copy_path_impl(vdev_t *svd, vdev_t *dvd) 2375 { 2376 char *old, *new; 2377 if (svd->vdev_path != NULL && dvd->vdev_path != NULL) { 2378 if (strcmp(svd->vdev_path, dvd->vdev_path) != 0) { 2379 zfs_dbgmsg("vdev_copy_path: vdev %llu: path changed " 2380 "from '%s' to '%s'", (u_longlong_t)dvd->vdev_guid, 2381 dvd->vdev_path, svd->vdev_path); 2382 spa_strfree(dvd->vdev_path); 2383 dvd->vdev_path = spa_strdup(svd->vdev_path); 2384 } 2385 } else if (svd->vdev_path != NULL) { 2386 dvd->vdev_path = spa_strdup(svd->vdev_path); 2387 zfs_dbgmsg("vdev_copy_path: vdev %llu: path set to '%s'", 2388 (u_longlong_t)dvd->vdev_guid, dvd->vdev_path); 2389 } 2390 2391 /* 2392 * Our enclosure sysfs path may have changed between imports 2393 */ 2394 old = dvd->vdev_enc_sysfs_path; 2395 new = svd->vdev_enc_sysfs_path; 2396 if ((old != NULL && new == NULL) || 2397 (old == NULL && new != NULL) || 2398 ((old != NULL && new != NULL) && strcmp(new, old) != 0)) { 2399 zfs_dbgmsg("vdev_copy_path: vdev %llu: vdev_enc_sysfs_path " 2400 "changed from '%s' to '%s'", (u_longlong_t)dvd->vdev_guid, 2401 old, new); 2402 2403 if (dvd->vdev_enc_sysfs_path) 2404 spa_strfree(dvd->vdev_enc_sysfs_path); 2405 2406 if (svd->vdev_enc_sysfs_path) { 2407 dvd->vdev_enc_sysfs_path = spa_strdup( 2408 svd->vdev_enc_sysfs_path); 2409 } else { 2410 dvd->vdev_enc_sysfs_path = NULL; 2411 } 2412 } 2413 } 2414 2415 /* 2416 * Recursively copy vdev paths from one vdev to another. Source and destination 2417 * vdev trees must have same geometry otherwise return error. Intended to copy 2418 * paths from userland config into MOS config. 2419 */ 2420 int 2421 vdev_copy_path_strict(vdev_t *svd, vdev_t *dvd) 2422 { 2423 if ((svd->vdev_ops == &vdev_missing_ops) || 2424 (svd->vdev_ishole && dvd->vdev_ishole) || 2425 (dvd->vdev_ops == &vdev_indirect_ops)) 2426 return (0); 2427 2428 if (svd->vdev_ops != dvd->vdev_ops) { 2429 vdev_dbgmsg(svd, "vdev_copy_path: vdev type mismatch: %s != %s", 2430 svd->vdev_ops->vdev_op_type, dvd->vdev_ops->vdev_op_type); 2431 return (SET_ERROR(EINVAL)); 2432 } 2433 2434 if (svd->vdev_guid != dvd->vdev_guid) { 2435 vdev_dbgmsg(svd, "vdev_copy_path: guids mismatch (%llu != " 2436 "%llu)", (u_longlong_t)svd->vdev_guid, 2437 (u_longlong_t)dvd->vdev_guid); 2438 return (SET_ERROR(EINVAL)); 2439 } 2440 2441 if (svd->vdev_children != dvd->vdev_children) { 2442 vdev_dbgmsg(svd, "vdev_copy_path: children count mismatch: " 2443 "%llu != %llu", (u_longlong_t)svd->vdev_children, 2444 (u_longlong_t)dvd->vdev_children); 2445 return (SET_ERROR(EINVAL)); 2446 } 2447 2448 for (uint64_t i = 0; i < svd->vdev_children; i++) { 2449 int error = vdev_copy_path_strict(svd->vdev_child[i], 2450 dvd->vdev_child[i]); 2451 if (error != 0) 2452 return (error); 2453 } 2454 2455 if (svd->vdev_ops->vdev_op_leaf) 2456 vdev_copy_path_impl(svd, dvd); 2457 2458 return (0); 2459 } 2460 2461 static void 2462 vdev_copy_path_search(vdev_t *stvd, vdev_t *dvd) 2463 { 2464 ASSERT(stvd->vdev_top == stvd); 2465 ASSERT3U(stvd->vdev_id, ==, dvd->vdev_top->vdev_id); 2466 2467 for (uint64_t i = 0; i < dvd->vdev_children; i++) { 2468 vdev_copy_path_search(stvd, dvd->vdev_child[i]); 2469 } 2470 2471 if (!dvd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(dvd)) 2472 return; 2473 2474 /* 2475 * The idea here is that while a vdev can shift positions within 2476 * a top vdev (when replacing, attaching mirror, etc.) it cannot 2477 * step outside of it. 2478 */ 2479 vdev_t *vd = vdev_lookup_by_guid(stvd, dvd->vdev_guid); 2480 2481 if (vd == NULL || vd->vdev_ops != dvd->vdev_ops) 2482 return; 2483 2484 ASSERT(vd->vdev_ops->vdev_op_leaf); 2485 2486 vdev_copy_path_impl(vd, dvd); 2487 } 2488 2489 /* 2490 * Recursively copy vdev paths from one root vdev to another. Source and 2491 * destination vdev trees may differ in geometry. For each destination leaf 2492 * vdev, search a vdev with the same guid and top vdev id in the source. 2493 * Intended to copy paths from userland config into MOS config. 2494 */ 2495 void 2496 vdev_copy_path_relaxed(vdev_t *srvd, vdev_t *drvd) 2497 { 2498 uint64_t children = MIN(srvd->vdev_children, drvd->vdev_children); 2499 ASSERT(srvd->vdev_ops == &vdev_root_ops); 2500 ASSERT(drvd->vdev_ops == &vdev_root_ops); 2501 2502 for (uint64_t i = 0; i < children; i++) { 2503 vdev_copy_path_search(srvd->vdev_child[i], 2504 drvd->vdev_child[i]); 2505 } 2506 } 2507 2508 /* 2509 * Close a virtual device. 2510 */ 2511 void 2512 vdev_close(vdev_t *vd) 2513 { 2514 vdev_t *pvd = vd->vdev_parent; 2515 spa_t *spa __maybe_unused = vd->vdev_spa; 2516 2517 ASSERT(vd != NULL); 2518 ASSERT(vd->vdev_open_thread == curthread || 2519 spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 2520 2521 /* 2522 * If our parent is reopening, then we are as well, unless we are 2523 * going offline. 2524 */ 2525 if (pvd != NULL && pvd->vdev_reopening) 2526 vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline); 2527 2528 vd->vdev_ops->vdev_op_close(vd); 2529 2530 vdev_cache_purge(vd); 2531 2532 /* 2533 * We record the previous state before we close it, so that if we are 2534 * doing a reopen(), we don't generate FMA ereports if we notice that 2535 * it's still faulted. 2536 */ 2537 vd->vdev_prevstate = vd->vdev_state; 2538 2539 if (vd->vdev_offline) 2540 vd->vdev_state = VDEV_STATE_OFFLINE; 2541 else 2542 vd->vdev_state = VDEV_STATE_CLOSED; 2543 vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 2544 } 2545 2546 void 2547 vdev_hold(vdev_t *vd) 2548 { 2549 spa_t *spa = vd->vdev_spa; 2550 2551 ASSERT(spa_is_root(spa)); 2552 if (spa->spa_state == POOL_STATE_UNINITIALIZED) 2553 return; 2554 2555 for (int c = 0; c < vd->vdev_children; c++) 2556 vdev_hold(vd->vdev_child[c]); 2557 2558 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_ops->vdev_op_hold != NULL) 2559 vd->vdev_ops->vdev_op_hold(vd); 2560 } 2561 2562 void 2563 vdev_rele(vdev_t *vd) 2564 { 2565 ASSERT(spa_is_root(vd->vdev_spa)); 2566 for (int c = 0; c < vd->vdev_children; c++) 2567 vdev_rele(vd->vdev_child[c]); 2568 2569 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_ops->vdev_op_rele != NULL) 2570 vd->vdev_ops->vdev_op_rele(vd); 2571 } 2572 2573 /* 2574 * Reopen all interior vdevs and any unopened leaves. We don't actually 2575 * reopen leaf vdevs which had previously been opened as they might deadlock 2576 * on the spa_config_lock. Instead we only obtain the leaf's physical size. 2577 * If the leaf has never been opened then open it, as usual. 2578 */ 2579 void 2580 vdev_reopen(vdev_t *vd) 2581 { 2582 spa_t *spa = vd->vdev_spa; 2583 2584 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 2585 2586 /* set the reopening flag unless we're taking the vdev offline */ 2587 vd->vdev_reopening = !vd->vdev_offline; 2588 vdev_close(vd); 2589 (void) vdev_open(vd); 2590 2591 /* 2592 * Call vdev_validate() here to make sure we have the same device. 2593 * Otherwise, a device with an invalid label could be successfully 2594 * opened in response to vdev_reopen(). 2595 */ 2596 if (vd->vdev_aux) { 2597 (void) vdev_validate_aux(vd); 2598 if (vdev_readable(vd) && vdev_writeable(vd) && 2599 vd->vdev_aux == &spa->spa_l2cache) { 2600 /* 2601 * In case the vdev is present we should evict all ARC 2602 * buffers and pointers to log blocks and reclaim their 2603 * space before restoring its contents to L2ARC. 2604 */ 2605 if (l2arc_vdev_present(vd)) { 2606 l2arc_rebuild_vdev(vd, B_TRUE); 2607 } else { 2608 l2arc_add_vdev(spa, vd); 2609 } 2610 spa_async_request(spa, SPA_ASYNC_L2CACHE_REBUILD); 2611 spa_async_request(spa, SPA_ASYNC_L2CACHE_TRIM); 2612 } 2613 } else { 2614 (void) vdev_validate(vd); 2615 } 2616 2617 /* 2618 * Reassess parent vdev's health. 2619 */ 2620 vdev_propagate_state(vd); 2621 } 2622 2623 int 2624 vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing) 2625 { 2626 int error; 2627 2628 /* 2629 * Normally, partial opens (e.g. of a mirror) are allowed. 2630 * For a create, however, we want to fail the request if 2631 * there are any components we can't open. 2632 */ 2633 error = vdev_open(vd); 2634 2635 if (error || vd->vdev_state != VDEV_STATE_HEALTHY) { 2636 vdev_close(vd); 2637 return (error ? error : SET_ERROR(ENXIO)); 2638 } 2639 2640 /* 2641 * Recursively load DTLs and initialize all labels. 2642 */ 2643 if ((error = vdev_dtl_load(vd)) != 0 || 2644 (error = vdev_label_init(vd, txg, isreplacing ? 2645 VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) { 2646 vdev_close(vd); 2647 return (error); 2648 } 2649 2650 return (0); 2651 } 2652 2653 void 2654 vdev_metaslab_set_size(vdev_t *vd) 2655 { 2656 uint64_t asize = vd->vdev_asize; 2657 uint64_t ms_count = asize >> zfs_vdev_default_ms_shift; 2658 uint64_t ms_shift; 2659 2660 /* 2661 * There are two dimensions to the metaslab sizing calculation: 2662 * the size of the metaslab and the count of metaslabs per vdev. 2663 * 2664 * The default values used below are a good balance between memory 2665 * usage (larger metaslab size means more memory needed for loaded 2666 * metaslabs; more metaslabs means more memory needed for the 2667 * metaslab_t structs), metaslab load time (larger metaslabs take 2668 * longer to load), and metaslab sync time (more metaslabs means 2669 * more time spent syncing all of them). 2670 * 2671 * In general, we aim for zfs_vdev_default_ms_count (200) metaslabs. 2672 * The range of the dimensions are as follows: 2673 * 2674 * 2^29 <= ms_size <= 2^34 2675 * 16 <= ms_count <= 131,072 2676 * 2677 * On the lower end of vdev sizes, we aim for metaslabs sizes of 2678 * at least 512MB (2^29) to minimize fragmentation effects when 2679 * testing with smaller devices. However, the count constraint 2680 * of at least 16 metaslabs will override this minimum size goal. 2681 * 2682 * On the upper end of vdev sizes, we aim for a maximum metaslab 2683 * size of 16GB. However, we will cap the total count to 2^17 2684 * metaslabs to keep our memory footprint in check and let the 2685 * metaslab size grow from there if that limit is hit. 2686 * 2687 * The net effect of applying above constrains is summarized below. 2688 * 2689 * vdev size metaslab count 2690 * --------------|----------------- 2691 * < 8GB ~16 2692 * 8GB - 100GB one per 512MB 2693 * 100GB - 3TB ~200 2694 * 3TB - 2PB one per 16GB 2695 * > 2PB ~131,072 2696 * -------------------------------- 2697 * 2698 * Finally, note that all of the above calculate the initial 2699 * number of metaslabs. Expanding a top-level vdev will result 2700 * in additional metaslabs being allocated making it possible 2701 * to exceed the zfs_vdev_ms_count_limit. 2702 */ 2703 2704 if (ms_count < zfs_vdev_min_ms_count) 2705 ms_shift = highbit64(asize / zfs_vdev_min_ms_count); 2706 else if (ms_count > zfs_vdev_default_ms_count) 2707 ms_shift = highbit64(asize / zfs_vdev_default_ms_count); 2708 else 2709 ms_shift = zfs_vdev_default_ms_shift; 2710 2711 if (ms_shift < SPA_MAXBLOCKSHIFT) { 2712 ms_shift = SPA_MAXBLOCKSHIFT; 2713 } else if (ms_shift > zfs_vdev_max_ms_shift) { 2714 ms_shift = zfs_vdev_max_ms_shift; 2715 /* cap the total count to constrain memory footprint */ 2716 if ((asize >> ms_shift) > zfs_vdev_ms_count_limit) 2717 ms_shift = highbit64(asize / zfs_vdev_ms_count_limit); 2718 } 2719 2720 vd->vdev_ms_shift = ms_shift; 2721 ASSERT3U(vd->vdev_ms_shift, >=, SPA_MAXBLOCKSHIFT); 2722 } 2723 2724 void 2725 vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg) 2726 { 2727 ASSERT(vd == vd->vdev_top); 2728 /* indirect vdevs don't have metaslabs or dtls */ 2729 ASSERT(vdev_is_concrete(vd) || flags == 0); 2730 ASSERT(ISP2(flags)); 2731 ASSERT(spa_writeable(vd->vdev_spa)); 2732 2733 if (flags & VDD_METASLAB) 2734 (void) txg_list_add(&vd->vdev_ms_list, arg, txg); 2735 2736 if (flags & VDD_DTL) 2737 (void) txg_list_add(&vd->vdev_dtl_list, arg, txg); 2738 2739 (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg); 2740 } 2741 2742 void 2743 vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg) 2744 { 2745 for (int c = 0; c < vd->vdev_children; c++) 2746 vdev_dirty_leaves(vd->vdev_child[c], flags, txg); 2747 2748 if (vd->vdev_ops->vdev_op_leaf) 2749 vdev_dirty(vd->vdev_top, flags, vd, txg); 2750 } 2751 2752 /* 2753 * DTLs. 2754 * 2755 * A vdev's DTL (dirty time log) is the set of transaction groups for which 2756 * the vdev has less than perfect replication. There are four kinds of DTL: 2757 * 2758 * DTL_MISSING: txgs for which the vdev has no valid copies of the data 2759 * 2760 * DTL_PARTIAL: txgs for which data is available, but not fully replicated 2761 * 2762 * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon 2763 * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of 2764 * txgs that was scrubbed. 2765 * 2766 * DTL_OUTAGE: txgs which cannot currently be read, whether due to 2767 * persistent errors or just some device being offline. 2768 * Unlike the other three, the DTL_OUTAGE map is not generally 2769 * maintained; it's only computed when needed, typically to 2770 * determine whether a device can be detached. 2771 * 2772 * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device 2773 * either has the data or it doesn't. 2774 * 2775 * For interior vdevs such as mirror and RAID-Z the picture is more complex. 2776 * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because 2777 * if any child is less than fully replicated, then so is its parent. 2778 * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs, 2779 * comprising only those txgs which appear in 'maxfaults' or more children; 2780 * those are the txgs we don't have enough replication to read. For example, 2781 * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2); 2782 * thus, its DTL_MISSING consists of the set of txgs that appear in more than 2783 * two child DTL_MISSING maps. 2784 * 2785 * It should be clear from the above that to compute the DTLs and outage maps 2786 * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps. 2787 * Therefore, that is all we keep on disk. When loading the pool, or after 2788 * a configuration change, we generate all other DTLs from first principles. 2789 */ 2790 void 2791 vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 2792 { 2793 range_tree_t *rt = vd->vdev_dtl[t]; 2794 2795 ASSERT(t < DTL_TYPES); 2796 ASSERT(vd != vd->vdev_spa->spa_root_vdev); 2797 ASSERT(spa_writeable(vd->vdev_spa)); 2798 2799 mutex_enter(&vd->vdev_dtl_lock); 2800 if (!range_tree_contains(rt, txg, size)) 2801 range_tree_add(rt, txg, size); 2802 mutex_exit(&vd->vdev_dtl_lock); 2803 } 2804 2805 boolean_t 2806 vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 2807 { 2808 range_tree_t *rt = vd->vdev_dtl[t]; 2809 boolean_t dirty = B_FALSE; 2810 2811 ASSERT(t < DTL_TYPES); 2812 ASSERT(vd != vd->vdev_spa->spa_root_vdev); 2813 2814 /* 2815 * While we are loading the pool, the DTLs have not been loaded yet. 2816 * This isn't a problem but it can result in devices being tried 2817 * which are known to not have the data. In which case, the import 2818 * is relying on the checksum to ensure that we get the right data. 2819 * Note that while importing we are only reading the MOS, which is 2820 * always checksummed. 2821 */ 2822 mutex_enter(&vd->vdev_dtl_lock); 2823 if (!range_tree_is_empty(rt)) 2824 dirty = range_tree_contains(rt, txg, size); 2825 mutex_exit(&vd->vdev_dtl_lock); 2826 2827 return (dirty); 2828 } 2829 2830 boolean_t 2831 vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t) 2832 { 2833 range_tree_t *rt = vd->vdev_dtl[t]; 2834 boolean_t empty; 2835 2836 mutex_enter(&vd->vdev_dtl_lock); 2837 empty = range_tree_is_empty(rt); 2838 mutex_exit(&vd->vdev_dtl_lock); 2839 2840 return (empty); 2841 } 2842 2843 /* 2844 * Check if the txg falls within the range which must be 2845 * resilvered. DVAs outside this range can always be skipped. 2846 */ 2847 boolean_t 2848 vdev_default_need_resilver(vdev_t *vd, const dva_t *dva, size_t psize, 2849 uint64_t phys_birth) 2850 { 2851 /* Set by sequential resilver. */ 2852 if (phys_birth == TXG_UNKNOWN) 2853 return (B_TRUE); 2854 2855 return (vdev_dtl_contains(vd, DTL_PARTIAL, phys_birth, 1)); 2856 } 2857 2858 /* 2859 * Returns B_TRUE if the vdev determines the DVA needs to be resilvered. 2860 */ 2861 boolean_t 2862 vdev_dtl_need_resilver(vdev_t *vd, const dva_t *dva, size_t psize, 2863 uint64_t phys_birth) 2864 { 2865 ASSERT(vd != vd->vdev_spa->spa_root_vdev); 2866 2867 if (vd->vdev_ops->vdev_op_need_resilver == NULL || 2868 vd->vdev_ops->vdev_op_leaf) 2869 return (B_TRUE); 2870 2871 return (vd->vdev_ops->vdev_op_need_resilver(vd, dva, psize, 2872 phys_birth)); 2873 } 2874 2875 /* 2876 * Returns the lowest txg in the DTL range. 2877 */ 2878 static uint64_t 2879 vdev_dtl_min(vdev_t *vd) 2880 { 2881 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock)); 2882 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0); 2883 ASSERT0(vd->vdev_children); 2884 2885 return (range_tree_min(vd->vdev_dtl[DTL_MISSING]) - 1); 2886 } 2887 2888 /* 2889 * Returns the highest txg in the DTL. 2890 */ 2891 static uint64_t 2892 vdev_dtl_max(vdev_t *vd) 2893 { 2894 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock)); 2895 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0); 2896 ASSERT0(vd->vdev_children); 2897 2898 return (range_tree_max(vd->vdev_dtl[DTL_MISSING])); 2899 } 2900 2901 /* 2902 * Determine if a resilvering vdev should remove any DTL entries from 2903 * its range. If the vdev was resilvering for the entire duration of the 2904 * scan then it should excise that range from its DTLs. Otherwise, this 2905 * vdev is considered partially resilvered and should leave its DTL 2906 * entries intact. The comment in vdev_dtl_reassess() describes how we 2907 * excise the DTLs. 2908 */ 2909 static boolean_t 2910 vdev_dtl_should_excise(vdev_t *vd, boolean_t rebuild_done) 2911 { 2912 ASSERT0(vd->vdev_children); 2913 2914 if (vd->vdev_state < VDEV_STATE_DEGRADED) 2915 return (B_FALSE); 2916 2917 if (vd->vdev_resilver_deferred) 2918 return (B_FALSE); 2919 2920 if (range_tree_is_empty(vd->vdev_dtl[DTL_MISSING])) 2921 return (B_TRUE); 2922 2923 if (rebuild_done) { 2924 vdev_rebuild_t *vr = &vd->vdev_top->vdev_rebuild_config; 2925 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys; 2926 2927 /* Rebuild not initiated by attach */ 2928 if (vd->vdev_rebuild_txg == 0) 2929 return (B_TRUE); 2930 2931 /* 2932 * When a rebuild completes without error then all missing data 2933 * up to the rebuild max txg has been reconstructed and the DTL 2934 * is eligible for excision. 2935 */ 2936 if (vrp->vrp_rebuild_state == VDEV_REBUILD_COMPLETE && 2937 vdev_dtl_max(vd) <= vrp->vrp_max_txg) { 2938 ASSERT3U(vrp->vrp_min_txg, <=, vdev_dtl_min(vd)); 2939 ASSERT3U(vrp->vrp_min_txg, <, vd->vdev_rebuild_txg); 2940 ASSERT3U(vd->vdev_rebuild_txg, <=, vrp->vrp_max_txg); 2941 return (B_TRUE); 2942 } 2943 } else { 2944 dsl_scan_t *scn = vd->vdev_spa->spa_dsl_pool->dp_scan; 2945 dsl_scan_phys_t *scnp __maybe_unused = &scn->scn_phys; 2946 2947 /* Resilver not initiated by attach */ 2948 if (vd->vdev_resilver_txg == 0) 2949 return (B_TRUE); 2950 2951 /* 2952 * When a resilver is initiated the scan will assign the 2953 * scn_max_txg value to the highest txg value that exists 2954 * in all DTLs. If this device's max DTL is not part of this 2955 * scan (i.e. it is not in the range (scn_min_txg, scn_max_txg] 2956 * then it is not eligible for excision. 2957 */ 2958 if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) { 2959 ASSERT3U(scnp->scn_min_txg, <=, vdev_dtl_min(vd)); 2960 ASSERT3U(scnp->scn_min_txg, <, vd->vdev_resilver_txg); 2961 ASSERT3U(vd->vdev_resilver_txg, <=, scnp->scn_max_txg); 2962 return (B_TRUE); 2963 } 2964 } 2965 2966 return (B_FALSE); 2967 } 2968 2969 /* 2970 * Reassess DTLs after a config change or scrub completion. If txg == 0 no 2971 * write operations will be issued to the pool. 2972 */ 2973 void 2974 vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, 2975 boolean_t scrub_done, boolean_t rebuild_done) 2976 { 2977 spa_t *spa = vd->vdev_spa; 2978 avl_tree_t reftree; 2979 int minref; 2980 2981 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 2982 2983 for (int c = 0; c < vd->vdev_children; c++) 2984 vdev_dtl_reassess(vd->vdev_child[c], txg, 2985 scrub_txg, scrub_done, rebuild_done); 2986 2987 if (vd == spa->spa_root_vdev || !vdev_is_concrete(vd) || vd->vdev_aux) 2988 return; 2989 2990 if (vd->vdev_ops->vdev_op_leaf) { 2991 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; 2992 vdev_rebuild_t *vr = &vd->vdev_top->vdev_rebuild_config; 2993 boolean_t check_excise = B_FALSE; 2994 boolean_t wasempty = B_TRUE; 2995 2996 mutex_enter(&vd->vdev_dtl_lock); 2997 2998 /* 2999 * If requested, pretend the scan or rebuild completed cleanly. 3000 */ 3001 if (zfs_scan_ignore_errors) { 3002 if (scn != NULL) 3003 scn->scn_phys.scn_errors = 0; 3004 if (vr != NULL) 3005 vr->vr_rebuild_phys.vrp_errors = 0; 3006 } 3007 3008 if (scrub_txg != 0 && 3009 !range_tree_is_empty(vd->vdev_dtl[DTL_MISSING])) { 3010 wasempty = B_FALSE; 3011 zfs_dbgmsg("guid:%llu txg:%llu scrub:%llu started:%d " 3012 "dtl:%llu/%llu errors:%llu", 3013 (u_longlong_t)vd->vdev_guid, (u_longlong_t)txg, 3014 (u_longlong_t)scrub_txg, spa->spa_scrub_started, 3015 (u_longlong_t)vdev_dtl_min(vd), 3016 (u_longlong_t)vdev_dtl_max(vd), 3017 (u_longlong_t)(scn ? scn->scn_phys.scn_errors : 0)); 3018 } 3019 3020 /* 3021 * If we've completed a scrub/resilver or a rebuild cleanly 3022 * then determine if this vdev should remove any DTLs. We 3023 * only want to excise regions on vdevs that were available 3024 * during the entire duration of this scan. 3025 */ 3026 if (rebuild_done && 3027 vr != NULL && vr->vr_rebuild_phys.vrp_errors == 0) { 3028 check_excise = B_TRUE; 3029 } else { 3030 if (spa->spa_scrub_started || 3031 (scn != NULL && scn->scn_phys.scn_errors == 0)) { 3032 check_excise = B_TRUE; 3033 } 3034 } 3035 3036 if (scrub_txg && check_excise && 3037 vdev_dtl_should_excise(vd, rebuild_done)) { 3038 /* 3039 * We completed a scrub, resilver or rebuild up to 3040 * scrub_txg. If we did it without rebooting, then 3041 * the scrub dtl will be valid, so excise the old 3042 * region and fold in the scrub dtl. Otherwise, 3043 * leave the dtl as-is if there was an error. 3044 * 3045 * There's little trick here: to excise the beginning 3046 * of the DTL_MISSING map, we put it into a reference 3047 * tree and then add a segment with refcnt -1 that 3048 * covers the range [0, scrub_txg). This means 3049 * that each txg in that range has refcnt -1 or 0. 3050 * We then add DTL_SCRUB with a refcnt of 2, so that 3051 * entries in the range [0, scrub_txg) will have a 3052 * positive refcnt -- either 1 or 2. We then convert 3053 * the reference tree into the new DTL_MISSING map. 3054 */ 3055 space_reftree_create(&reftree); 3056 space_reftree_add_map(&reftree, 3057 vd->vdev_dtl[DTL_MISSING], 1); 3058 space_reftree_add_seg(&reftree, 0, scrub_txg, -1); 3059 space_reftree_add_map(&reftree, 3060 vd->vdev_dtl[DTL_SCRUB], 2); 3061 space_reftree_generate_map(&reftree, 3062 vd->vdev_dtl[DTL_MISSING], 1); 3063 space_reftree_destroy(&reftree); 3064 3065 if (!range_tree_is_empty(vd->vdev_dtl[DTL_MISSING])) { 3066 zfs_dbgmsg("update DTL_MISSING:%llu/%llu", 3067 (u_longlong_t)vdev_dtl_min(vd), 3068 (u_longlong_t)vdev_dtl_max(vd)); 3069 } else if (!wasempty) { 3070 zfs_dbgmsg("DTL_MISSING is now empty"); 3071 } 3072 } 3073 range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL); 3074 range_tree_walk(vd->vdev_dtl[DTL_MISSING], 3075 range_tree_add, vd->vdev_dtl[DTL_PARTIAL]); 3076 if (scrub_done) 3077 range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL); 3078 range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL); 3079 if (!vdev_readable(vd)) 3080 range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL); 3081 else 3082 range_tree_walk(vd->vdev_dtl[DTL_MISSING], 3083 range_tree_add, vd->vdev_dtl[DTL_OUTAGE]); 3084 3085 /* 3086 * If the vdev was resilvering or rebuilding and no longer 3087 * has any DTLs then reset the appropriate flag and dirty 3088 * the top level so that we persist the change. 3089 */ 3090 if (txg != 0 && 3091 range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) && 3092 range_tree_is_empty(vd->vdev_dtl[DTL_OUTAGE])) { 3093 if (vd->vdev_rebuild_txg != 0) { 3094 vd->vdev_rebuild_txg = 0; 3095 vdev_config_dirty(vd->vdev_top); 3096 } else if (vd->vdev_resilver_txg != 0) { 3097 vd->vdev_resilver_txg = 0; 3098 vdev_config_dirty(vd->vdev_top); 3099 } 3100 } 3101 3102 mutex_exit(&vd->vdev_dtl_lock); 3103 3104 if (txg != 0) 3105 vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg); 3106 return; 3107 } 3108 3109 mutex_enter(&vd->vdev_dtl_lock); 3110 for (int t = 0; t < DTL_TYPES; t++) { 3111 /* account for child's outage in parent's missing map */ 3112 int s = (t == DTL_MISSING) ? DTL_OUTAGE: t; 3113 if (t == DTL_SCRUB) 3114 continue; /* leaf vdevs only */ 3115 if (t == DTL_PARTIAL) 3116 minref = 1; /* i.e. non-zero */ 3117 else if (vdev_get_nparity(vd) != 0) 3118 minref = vdev_get_nparity(vd) + 1; /* RAID-Z, dRAID */ 3119 else 3120 minref = vd->vdev_children; /* any kind of mirror */ 3121 space_reftree_create(&reftree); 3122 for (int c = 0; c < vd->vdev_children; c++) { 3123 vdev_t *cvd = vd->vdev_child[c]; 3124 mutex_enter(&cvd->vdev_dtl_lock); 3125 space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1); 3126 mutex_exit(&cvd->vdev_dtl_lock); 3127 } 3128 space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref); 3129 space_reftree_destroy(&reftree); 3130 } 3131 mutex_exit(&vd->vdev_dtl_lock); 3132 } 3133 3134 int 3135 vdev_dtl_load(vdev_t *vd) 3136 { 3137 spa_t *spa = vd->vdev_spa; 3138 objset_t *mos = spa->spa_meta_objset; 3139 range_tree_t *rt; 3140 int error = 0; 3141 3142 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) { 3143 ASSERT(vdev_is_concrete(vd)); 3144 3145 error = space_map_open(&vd->vdev_dtl_sm, mos, 3146 vd->vdev_dtl_object, 0, -1ULL, 0); 3147 if (error) 3148 return (error); 3149 ASSERT(vd->vdev_dtl_sm != NULL); 3150 3151 rt = range_tree_create(NULL, RANGE_SEG64, NULL, 0, 0); 3152 error = space_map_load(vd->vdev_dtl_sm, rt, SM_ALLOC); 3153 if (error == 0) { 3154 mutex_enter(&vd->vdev_dtl_lock); 3155 range_tree_walk(rt, range_tree_add, 3156 vd->vdev_dtl[DTL_MISSING]); 3157 mutex_exit(&vd->vdev_dtl_lock); 3158 } 3159 3160 range_tree_vacate(rt, NULL, NULL); 3161 range_tree_destroy(rt); 3162 3163 return (error); 3164 } 3165 3166 for (int c = 0; c < vd->vdev_children; c++) { 3167 error = vdev_dtl_load(vd->vdev_child[c]); 3168 if (error != 0) 3169 break; 3170 } 3171 3172 return (error); 3173 } 3174 3175 static void 3176 vdev_zap_allocation_data(vdev_t *vd, dmu_tx_t *tx) 3177 { 3178 spa_t *spa = vd->vdev_spa; 3179 objset_t *mos = spa->spa_meta_objset; 3180 vdev_alloc_bias_t alloc_bias = vd->vdev_alloc_bias; 3181 const char *string; 3182 3183 ASSERT(alloc_bias != VDEV_BIAS_NONE); 3184 3185 string = 3186 (alloc_bias == VDEV_BIAS_LOG) ? VDEV_ALLOC_BIAS_LOG : 3187 (alloc_bias == VDEV_BIAS_SPECIAL) ? VDEV_ALLOC_BIAS_SPECIAL : 3188 (alloc_bias == VDEV_BIAS_DEDUP) ? VDEV_ALLOC_BIAS_DEDUP : NULL; 3189 3190 ASSERT(string != NULL); 3191 VERIFY0(zap_add(mos, vd->vdev_top_zap, VDEV_TOP_ZAP_ALLOCATION_BIAS, 3192 1, strlen(string) + 1, string, tx)); 3193 3194 if (alloc_bias == VDEV_BIAS_SPECIAL || alloc_bias == VDEV_BIAS_DEDUP) { 3195 spa_activate_allocation_classes(spa, tx); 3196 } 3197 } 3198 3199 void 3200 vdev_destroy_unlink_zap(vdev_t *vd, uint64_t zapobj, dmu_tx_t *tx) 3201 { 3202 spa_t *spa = vd->vdev_spa; 3203 3204 VERIFY0(zap_destroy(spa->spa_meta_objset, zapobj, tx)); 3205 VERIFY0(zap_remove_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps, 3206 zapobj, tx)); 3207 } 3208 3209 uint64_t 3210 vdev_create_link_zap(vdev_t *vd, dmu_tx_t *tx) 3211 { 3212 spa_t *spa = vd->vdev_spa; 3213 uint64_t zap = zap_create(spa->spa_meta_objset, DMU_OTN_ZAP_METADATA, 3214 DMU_OT_NONE, 0, tx); 3215 3216 ASSERT(zap != 0); 3217 VERIFY0(zap_add_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps, 3218 zap, tx)); 3219 3220 return (zap); 3221 } 3222 3223 void 3224 vdev_construct_zaps(vdev_t *vd, dmu_tx_t *tx) 3225 { 3226 if (vd->vdev_ops != &vdev_hole_ops && 3227 vd->vdev_ops != &vdev_missing_ops && 3228 vd->vdev_ops != &vdev_root_ops && 3229 !vd->vdev_top->vdev_removing) { 3230 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_leaf_zap == 0) { 3231 vd->vdev_leaf_zap = vdev_create_link_zap(vd, tx); 3232 } 3233 if (vd == vd->vdev_top && vd->vdev_top_zap == 0) { 3234 vd->vdev_top_zap = vdev_create_link_zap(vd, tx); 3235 if (vd->vdev_alloc_bias != VDEV_BIAS_NONE) 3236 vdev_zap_allocation_data(vd, tx); 3237 } 3238 } 3239 3240 for (uint64_t i = 0; i < vd->vdev_children; i++) { 3241 vdev_construct_zaps(vd->vdev_child[i], tx); 3242 } 3243 } 3244 3245 static void 3246 vdev_dtl_sync(vdev_t *vd, uint64_t txg) 3247 { 3248 spa_t *spa = vd->vdev_spa; 3249 range_tree_t *rt = vd->vdev_dtl[DTL_MISSING]; 3250 objset_t *mos = spa->spa_meta_objset; 3251 range_tree_t *rtsync; 3252 dmu_tx_t *tx; 3253 uint64_t object = space_map_object(vd->vdev_dtl_sm); 3254 3255 ASSERT(vdev_is_concrete(vd)); 3256 ASSERT(vd->vdev_ops->vdev_op_leaf); 3257 3258 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 3259 3260 if (vd->vdev_detached || vd->vdev_top->vdev_removing) { 3261 mutex_enter(&vd->vdev_dtl_lock); 3262 space_map_free(vd->vdev_dtl_sm, tx); 3263 space_map_close(vd->vdev_dtl_sm); 3264 vd->vdev_dtl_sm = NULL; 3265 mutex_exit(&vd->vdev_dtl_lock); 3266 3267 /* 3268 * We only destroy the leaf ZAP for detached leaves or for 3269 * removed log devices. Removed data devices handle leaf ZAP 3270 * cleanup later, once cancellation is no longer possible. 3271 */ 3272 if (vd->vdev_leaf_zap != 0 && (vd->vdev_detached || 3273 vd->vdev_top->vdev_islog)) { 3274 vdev_destroy_unlink_zap(vd, vd->vdev_leaf_zap, tx); 3275 vd->vdev_leaf_zap = 0; 3276 } 3277 3278 dmu_tx_commit(tx); 3279 return; 3280 } 3281 3282 if (vd->vdev_dtl_sm == NULL) { 3283 uint64_t new_object; 3284 3285 new_object = space_map_alloc(mos, zfs_vdev_dtl_sm_blksz, tx); 3286 VERIFY3U(new_object, !=, 0); 3287 3288 VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object, 3289 0, -1ULL, 0)); 3290 ASSERT(vd->vdev_dtl_sm != NULL); 3291 } 3292 3293 rtsync = range_tree_create(NULL, RANGE_SEG64, NULL, 0, 0); 3294 3295 mutex_enter(&vd->vdev_dtl_lock); 3296 range_tree_walk(rt, range_tree_add, rtsync); 3297 mutex_exit(&vd->vdev_dtl_lock); 3298 3299 space_map_truncate(vd->vdev_dtl_sm, zfs_vdev_dtl_sm_blksz, tx); 3300 space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, SM_NO_VDEVID, tx); 3301 range_tree_vacate(rtsync, NULL, NULL); 3302 3303 range_tree_destroy(rtsync); 3304 3305 /* 3306 * If the object for the space map has changed then dirty 3307 * the top level so that we update the config. 3308 */ 3309 if (object != space_map_object(vd->vdev_dtl_sm)) { 3310 vdev_dbgmsg(vd, "txg %llu, spa %s, DTL old object %llu, " 3311 "new object %llu", (u_longlong_t)txg, spa_name(spa), 3312 (u_longlong_t)object, 3313 (u_longlong_t)space_map_object(vd->vdev_dtl_sm)); 3314 vdev_config_dirty(vd->vdev_top); 3315 } 3316 3317 dmu_tx_commit(tx); 3318 } 3319 3320 /* 3321 * Determine whether the specified vdev can be offlined/detached/removed 3322 * without losing data. 3323 */ 3324 boolean_t 3325 vdev_dtl_required(vdev_t *vd) 3326 { 3327 spa_t *spa = vd->vdev_spa; 3328 vdev_t *tvd = vd->vdev_top; 3329 uint8_t cant_read = vd->vdev_cant_read; 3330 boolean_t required; 3331 3332 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 3333 3334 if (vd == spa->spa_root_vdev || vd == tvd) 3335 return (B_TRUE); 3336 3337 /* 3338 * Temporarily mark the device as unreadable, and then determine 3339 * whether this results in any DTL outages in the top-level vdev. 3340 * If not, we can safely offline/detach/remove the device. 3341 */ 3342 vd->vdev_cant_read = B_TRUE; 3343 vdev_dtl_reassess(tvd, 0, 0, B_FALSE, B_FALSE); 3344 required = !vdev_dtl_empty(tvd, DTL_OUTAGE); 3345 vd->vdev_cant_read = cant_read; 3346 vdev_dtl_reassess(tvd, 0, 0, B_FALSE, B_FALSE); 3347 3348 if (!required && zio_injection_enabled) { 3349 required = !!zio_handle_device_injection(vd, NULL, 3350 SET_ERROR(ECHILD)); 3351 } 3352 3353 return (required); 3354 } 3355 3356 /* 3357 * Determine if resilver is needed, and if so the txg range. 3358 */ 3359 boolean_t 3360 vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp) 3361 { 3362 boolean_t needed = B_FALSE; 3363 uint64_t thismin = UINT64_MAX; 3364 uint64_t thismax = 0; 3365 3366 if (vd->vdev_children == 0) { 3367 mutex_enter(&vd->vdev_dtl_lock); 3368 if (!range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) && 3369 vdev_writeable(vd)) { 3370 3371 thismin = vdev_dtl_min(vd); 3372 thismax = vdev_dtl_max(vd); 3373 needed = B_TRUE; 3374 } 3375 mutex_exit(&vd->vdev_dtl_lock); 3376 } else { 3377 for (int c = 0; c < vd->vdev_children; c++) { 3378 vdev_t *cvd = vd->vdev_child[c]; 3379 uint64_t cmin, cmax; 3380 3381 if (vdev_resilver_needed(cvd, &cmin, &cmax)) { 3382 thismin = MIN(thismin, cmin); 3383 thismax = MAX(thismax, cmax); 3384 needed = B_TRUE; 3385 } 3386 } 3387 } 3388 3389 if (needed && minp) { 3390 *minp = thismin; 3391 *maxp = thismax; 3392 } 3393 return (needed); 3394 } 3395 3396 /* 3397 * Gets the checkpoint space map object from the vdev's ZAP. On success sm_obj 3398 * will contain either the checkpoint spacemap object or zero if none exists. 3399 * All other errors are returned to the caller. 3400 */ 3401 int 3402 vdev_checkpoint_sm_object(vdev_t *vd, uint64_t *sm_obj) 3403 { 3404 ASSERT0(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER)); 3405 3406 if (vd->vdev_top_zap == 0) { 3407 *sm_obj = 0; 3408 return (0); 3409 } 3410 3411 int error = zap_lookup(spa_meta_objset(vd->vdev_spa), vd->vdev_top_zap, 3412 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, sizeof (uint64_t), 1, sm_obj); 3413 if (error == ENOENT) { 3414 *sm_obj = 0; 3415 error = 0; 3416 } 3417 3418 return (error); 3419 } 3420 3421 int 3422 vdev_load(vdev_t *vd) 3423 { 3424 int children = vd->vdev_children; 3425 int error = 0; 3426 taskq_t *tq = NULL; 3427 3428 /* 3429 * It's only worthwhile to use the taskq for the root vdev, because the 3430 * slow part is metaslab_init, and that only happens for top-level 3431 * vdevs. 3432 */ 3433 if (vd->vdev_ops == &vdev_root_ops && vd->vdev_children > 0) { 3434 tq = taskq_create("vdev_load", children, minclsyspri, 3435 children, children, TASKQ_PREPOPULATE); 3436 } 3437 3438 /* 3439 * Recursively load all children. 3440 */ 3441 for (int c = 0; c < vd->vdev_children; c++) { 3442 vdev_t *cvd = vd->vdev_child[c]; 3443 3444 if (tq == NULL || vdev_uses_zvols(cvd)) { 3445 cvd->vdev_load_error = vdev_load(cvd); 3446 } else { 3447 VERIFY(taskq_dispatch(tq, vdev_load_child, 3448 cvd, TQ_SLEEP) != TASKQID_INVALID); 3449 } 3450 } 3451 3452 if (tq != NULL) { 3453 taskq_wait(tq); 3454 taskq_destroy(tq); 3455 } 3456 3457 for (int c = 0; c < vd->vdev_children; c++) { 3458 int error = vd->vdev_child[c]->vdev_load_error; 3459 3460 if (error != 0) 3461 return (error); 3462 } 3463 3464 vdev_set_deflate_ratio(vd); 3465 3466 /* 3467 * On spa_load path, grab the allocation bias from our zap 3468 */ 3469 if (vd == vd->vdev_top && vd->vdev_top_zap != 0) { 3470 spa_t *spa = vd->vdev_spa; 3471 char bias_str[64]; 3472 3473 error = zap_lookup(spa->spa_meta_objset, vd->vdev_top_zap, 3474 VDEV_TOP_ZAP_ALLOCATION_BIAS, 1, sizeof (bias_str), 3475 bias_str); 3476 if (error == 0) { 3477 ASSERT(vd->vdev_alloc_bias == VDEV_BIAS_NONE); 3478 vd->vdev_alloc_bias = vdev_derive_alloc_bias(bias_str); 3479 } else if (error != ENOENT) { 3480 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 3481 VDEV_AUX_CORRUPT_DATA); 3482 vdev_dbgmsg(vd, "vdev_load: zap_lookup(top_zap=%llu) " 3483 "failed [error=%d]", 3484 (u_longlong_t)vd->vdev_top_zap, error); 3485 return (error); 3486 } 3487 } 3488 3489 /* 3490 * Load any rebuild state from the top-level vdev zap. 3491 */ 3492 if (vd == vd->vdev_top && vd->vdev_top_zap != 0) { 3493 error = vdev_rebuild_load(vd); 3494 if (error && error != ENOTSUP) { 3495 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 3496 VDEV_AUX_CORRUPT_DATA); 3497 vdev_dbgmsg(vd, "vdev_load: vdev_rebuild_load " 3498 "failed [error=%d]", error); 3499 return (error); 3500 } 3501 } 3502 3503 /* 3504 * If this is a top-level vdev, initialize its metaslabs. 3505 */ 3506 if (vd == vd->vdev_top && vdev_is_concrete(vd)) { 3507 vdev_metaslab_group_create(vd); 3508 3509 if (vd->vdev_ashift == 0 || vd->vdev_asize == 0) { 3510 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 3511 VDEV_AUX_CORRUPT_DATA); 3512 vdev_dbgmsg(vd, "vdev_load: invalid size. ashift=%llu, " 3513 "asize=%llu", (u_longlong_t)vd->vdev_ashift, 3514 (u_longlong_t)vd->vdev_asize); 3515 return (SET_ERROR(ENXIO)); 3516 } 3517 3518 error = vdev_metaslab_init(vd, 0); 3519 if (error != 0) { 3520 vdev_dbgmsg(vd, "vdev_load: metaslab_init failed " 3521 "[error=%d]", error); 3522 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 3523 VDEV_AUX_CORRUPT_DATA); 3524 return (error); 3525 } 3526 3527 uint64_t checkpoint_sm_obj; 3528 error = vdev_checkpoint_sm_object(vd, &checkpoint_sm_obj); 3529 if (error == 0 && checkpoint_sm_obj != 0) { 3530 objset_t *mos = spa_meta_objset(vd->vdev_spa); 3531 ASSERT(vd->vdev_asize != 0); 3532 ASSERT3P(vd->vdev_checkpoint_sm, ==, NULL); 3533 3534 error = space_map_open(&vd->vdev_checkpoint_sm, 3535 mos, checkpoint_sm_obj, 0, vd->vdev_asize, 3536 vd->vdev_ashift); 3537 if (error != 0) { 3538 vdev_dbgmsg(vd, "vdev_load: space_map_open " 3539 "failed for checkpoint spacemap (obj %llu) " 3540 "[error=%d]", 3541 (u_longlong_t)checkpoint_sm_obj, error); 3542 return (error); 3543 } 3544 ASSERT3P(vd->vdev_checkpoint_sm, !=, NULL); 3545 3546 /* 3547 * Since the checkpoint_sm contains free entries 3548 * exclusively we can use space_map_allocated() to 3549 * indicate the cumulative checkpointed space that 3550 * has been freed. 3551 */ 3552 vd->vdev_stat.vs_checkpoint_space = 3553 -space_map_allocated(vd->vdev_checkpoint_sm); 3554 vd->vdev_spa->spa_checkpoint_info.sci_dspace += 3555 vd->vdev_stat.vs_checkpoint_space; 3556 } else if (error != 0) { 3557 vdev_dbgmsg(vd, "vdev_load: failed to retrieve " 3558 "checkpoint space map object from vdev ZAP " 3559 "[error=%d]", error); 3560 return (error); 3561 } 3562 } 3563 3564 /* 3565 * If this is a leaf vdev, load its DTL. 3566 */ 3567 if (vd->vdev_ops->vdev_op_leaf && (error = vdev_dtl_load(vd)) != 0) { 3568 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 3569 VDEV_AUX_CORRUPT_DATA); 3570 vdev_dbgmsg(vd, "vdev_load: vdev_dtl_load failed " 3571 "[error=%d]", error); 3572 return (error); 3573 } 3574 3575 uint64_t obsolete_sm_object; 3576 error = vdev_obsolete_sm_object(vd, &obsolete_sm_object); 3577 if (error == 0 && obsolete_sm_object != 0) { 3578 objset_t *mos = vd->vdev_spa->spa_meta_objset; 3579 ASSERT(vd->vdev_asize != 0); 3580 ASSERT3P(vd->vdev_obsolete_sm, ==, NULL); 3581 3582 if ((error = space_map_open(&vd->vdev_obsolete_sm, mos, 3583 obsolete_sm_object, 0, vd->vdev_asize, 0))) { 3584 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 3585 VDEV_AUX_CORRUPT_DATA); 3586 vdev_dbgmsg(vd, "vdev_load: space_map_open failed for " 3587 "obsolete spacemap (obj %llu) [error=%d]", 3588 (u_longlong_t)obsolete_sm_object, error); 3589 return (error); 3590 } 3591 } else if (error != 0) { 3592 vdev_dbgmsg(vd, "vdev_load: failed to retrieve obsolete " 3593 "space map object from vdev ZAP [error=%d]", error); 3594 return (error); 3595 } 3596 3597 return (0); 3598 } 3599 3600 /* 3601 * The special vdev case is used for hot spares and l2cache devices. Its 3602 * sole purpose it to set the vdev state for the associated vdev. To do this, 3603 * we make sure that we can open the underlying device, then try to read the 3604 * label, and make sure that the label is sane and that it hasn't been 3605 * repurposed to another pool. 3606 */ 3607 int 3608 vdev_validate_aux(vdev_t *vd) 3609 { 3610 nvlist_t *label; 3611 uint64_t guid, version; 3612 uint64_t state; 3613 3614 if (!vdev_readable(vd)) 3615 return (0); 3616 3617 if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) { 3618 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 3619 VDEV_AUX_CORRUPT_DATA); 3620 return (-1); 3621 } 3622 3623 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 || 3624 !SPA_VERSION_IS_SUPPORTED(version) || 3625 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 || 3626 guid != vd->vdev_guid || 3627 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) { 3628 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 3629 VDEV_AUX_CORRUPT_DATA); 3630 nvlist_free(label); 3631 return (-1); 3632 } 3633 3634 /* 3635 * We don't actually check the pool state here. If it's in fact in 3636 * use by another pool, we update this fact on the fly when requested. 3637 */ 3638 nvlist_free(label); 3639 return (0); 3640 } 3641 3642 static void 3643 vdev_destroy_ms_flush_data(vdev_t *vd, dmu_tx_t *tx) 3644 { 3645 objset_t *mos = spa_meta_objset(vd->vdev_spa); 3646 3647 if (vd->vdev_top_zap == 0) 3648 return; 3649 3650 uint64_t object = 0; 3651 int err = zap_lookup(mos, vd->vdev_top_zap, 3652 VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS, sizeof (uint64_t), 1, &object); 3653 if (err == ENOENT) 3654 return; 3655 VERIFY0(err); 3656 3657 VERIFY0(dmu_object_free(mos, object, tx)); 3658 VERIFY0(zap_remove(mos, vd->vdev_top_zap, 3659 VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS, tx)); 3660 } 3661 3662 /* 3663 * Free the objects used to store this vdev's spacemaps, and the array 3664 * that points to them. 3665 */ 3666 void 3667 vdev_destroy_spacemaps(vdev_t *vd, dmu_tx_t *tx) 3668 { 3669 if (vd->vdev_ms_array == 0) 3670 return; 3671 3672 objset_t *mos = vd->vdev_spa->spa_meta_objset; 3673 uint64_t array_count = vd->vdev_asize >> vd->vdev_ms_shift; 3674 size_t array_bytes = array_count * sizeof (uint64_t); 3675 uint64_t *smobj_array = kmem_alloc(array_bytes, KM_SLEEP); 3676 VERIFY0(dmu_read(mos, vd->vdev_ms_array, 0, 3677 array_bytes, smobj_array, 0)); 3678 3679 for (uint64_t i = 0; i < array_count; i++) { 3680 uint64_t smobj = smobj_array[i]; 3681 if (smobj == 0) 3682 continue; 3683 3684 space_map_free_obj(mos, smobj, tx); 3685 } 3686 3687 kmem_free(smobj_array, array_bytes); 3688 VERIFY0(dmu_object_free(mos, vd->vdev_ms_array, tx)); 3689 vdev_destroy_ms_flush_data(vd, tx); 3690 vd->vdev_ms_array = 0; 3691 } 3692 3693 static void 3694 vdev_remove_empty_log(vdev_t *vd, uint64_t txg) 3695 { 3696 spa_t *spa = vd->vdev_spa; 3697 3698 ASSERT(vd->vdev_islog); 3699 ASSERT(vd == vd->vdev_top); 3700 ASSERT3U(txg, ==, spa_syncing_txg(spa)); 3701 3702 dmu_tx_t *tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg); 3703 3704 vdev_destroy_spacemaps(vd, tx); 3705 if (vd->vdev_top_zap != 0) { 3706 vdev_destroy_unlink_zap(vd, vd->vdev_top_zap, tx); 3707 vd->vdev_top_zap = 0; 3708 } 3709 3710 dmu_tx_commit(tx); 3711 } 3712 3713 void 3714 vdev_sync_done(vdev_t *vd, uint64_t txg) 3715 { 3716 metaslab_t *msp; 3717 boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg)); 3718 3719 ASSERT(vdev_is_concrete(vd)); 3720 3721 while ((msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg))) 3722 != NULL) 3723 metaslab_sync_done(msp, txg); 3724 3725 if (reassess) { 3726 metaslab_sync_reassess(vd->vdev_mg); 3727 if (vd->vdev_log_mg != NULL) 3728 metaslab_sync_reassess(vd->vdev_log_mg); 3729 } 3730 } 3731 3732 void 3733 vdev_sync(vdev_t *vd, uint64_t txg) 3734 { 3735 spa_t *spa = vd->vdev_spa; 3736 vdev_t *lvd; 3737 metaslab_t *msp; 3738 3739 ASSERT3U(txg, ==, spa->spa_syncing_txg); 3740 dmu_tx_t *tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 3741 if (range_tree_space(vd->vdev_obsolete_segments) > 0) { 3742 ASSERT(vd->vdev_removing || 3743 vd->vdev_ops == &vdev_indirect_ops); 3744 3745 vdev_indirect_sync_obsolete(vd, tx); 3746 3747 /* 3748 * If the vdev is indirect, it can't have dirty 3749 * metaslabs or DTLs. 3750 */ 3751 if (vd->vdev_ops == &vdev_indirect_ops) { 3752 ASSERT(txg_list_empty(&vd->vdev_ms_list, txg)); 3753 ASSERT(txg_list_empty(&vd->vdev_dtl_list, txg)); 3754 dmu_tx_commit(tx); 3755 return; 3756 } 3757 } 3758 3759 ASSERT(vdev_is_concrete(vd)); 3760 3761 if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0 && 3762 !vd->vdev_removing) { 3763 ASSERT(vd == vd->vdev_top); 3764 ASSERT0(vd->vdev_indirect_config.vic_mapping_object); 3765 vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset, 3766 DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx); 3767 ASSERT(vd->vdev_ms_array != 0); 3768 vdev_config_dirty(vd); 3769 } 3770 3771 while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) { 3772 metaslab_sync(msp, txg); 3773 (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg)); 3774 } 3775 3776 while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL) 3777 vdev_dtl_sync(lvd, txg); 3778 3779 /* 3780 * If this is an empty log device being removed, destroy the 3781 * metadata associated with it. 3782 */ 3783 if (vd->vdev_islog && vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing) 3784 vdev_remove_empty_log(vd, txg); 3785 3786 (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)); 3787 dmu_tx_commit(tx); 3788 } 3789 3790 uint64_t 3791 vdev_psize_to_asize(vdev_t *vd, uint64_t psize) 3792 { 3793 return (vd->vdev_ops->vdev_op_asize(vd, psize)); 3794 } 3795 3796 /* 3797 * Mark the given vdev faulted. A faulted vdev behaves as if the device could 3798 * not be opened, and no I/O is attempted. 3799 */ 3800 int 3801 vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux) 3802 { 3803 vdev_t *vd, *tvd; 3804 3805 spa_vdev_state_enter(spa, SCL_NONE); 3806 3807 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3808 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV))); 3809 3810 if (!vd->vdev_ops->vdev_op_leaf) 3811 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENOTSUP))); 3812 3813 tvd = vd->vdev_top; 3814 3815 /* 3816 * If user did a 'zpool offline -f' then make the fault persist across 3817 * reboots. 3818 */ 3819 if (aux == VDEV_AUX_EXTERNAL_PERSIST) { 3820 /* 3821 * There are two kinds of forced faults: temporary and 3822 * persistent. Temporary faults go away at pool import, while 3823 * persistent faults stay set. Both types of faults can be 3824 * cleared with a zpool clear. 3825 * 3826 * We tell if a vdev is persistently faulted by looking at the 3827 * ZPOOL_CONFIG_AUX_STATE nvpair. If it's set to "external" at 3828 * import then it's a persistent fault. Otherwise, it's 3829 * temporary. We get ZPOOL_CONFIG_AUX_STATE set to "external" 3830 * by setting vd.vdev_stat.vs_aux to VDEV_AUX_EXTERNAL. This 3831 * tells vdev_config_generate() (which gets run later) to set 3832 * ZPOOL_CONFIG_AUX_STATE to "external" in the nvlist. 3833 */ 3834 vd->vdev_stat.vs_aux = VDEV_AUX_EXTERNAL; 3835 vd->vdev_tmpoffline = B_FALSE; 3836 aux = VDEV_AUX_EXTERNAL; 3837 } else { 3838 vd->vdev_tmpoffline = B_TRUE; 3839 } 3840 3841 /* 3842 * We don't directly use the aux state here, but if we do a 3843 * vdev_reopen(), we need this value to be present to remember why we 3844 * were faulted. 3845 */ 3846 vd->vdev_label_aux = aux; 3847 3848 /* 3849 * Faulted state takes precedence over degraded. 3850 */ 3851 vd->vdev_delayed_close = B_FALSE; 3852 vd->vdev_faulted = 1ULL; 3853 vd->vdev_degraded = 0ULL; 3854 vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux); 3855 3856 /* 3857 * If this device has the only valid copy of the data, then 3858 * back off and simply mark the vdev as degraded instead. 3859 */ 3860 if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) { 3861 vd->vdev_degraded = 1ULL; 3862 vd->vdev_faulted = 0ULL; 3863 3864 /* 3865 * If we reopen the device and it's not dead, only then do we 3866 * mark it degraded. 3867 */ 3868 vdev_reopen(tvd); 3869 3870 if (vdev_readable(vd)) 3871 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux); 3872 } 3873 3874 return (spa_vdev_state_exit(spa, vd, 0)); 3875 } 3876 3877 /* 3878 * Mark the given vdev degraded. A degraded vdev is purely an indication to the 3879 * user that something is wrong. The vdev continues to operate as normal as far 3880 * as I/O is concerned. 3881 */ 3882 int 3883 vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux) 3884 { 3885 vdev_t *vd; 3886 3887 spa_vdev_state_enter(spa, SCL_NONE); 3888 3889 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3890 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV))); 3891 3892 if (!vd->vdev_ops->vdev_op_leaf) 3893 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENOTSUP))); 3894 3895 /* 3896 * If the vdev is already faulted, then don't do anything. 3897 */ 3898 if (vd->vdev_faulted || vd->vdev_degraded) 3899 return (spa_vdev_state_exit(spa, NULL, 0)); 3900 3901 vd->vdev_degraded = 1ULL; 3902 if (!vdev_is_dead(vd)) 3903 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, 3904 aux); 3905 3906 return (spa_vdev_state_exit(spa, vd, 0)); 3907 } 3908 3909 /* 3910 * Online the given vdev. 3911 * 3912 * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached 3913 * spare device should be detached when the device finishes resilvering. 3914 * Second, the online should be treated like a 'test' online case, so no FMA 3915 * events are generated if the device fails to open. 3916 */ 3917 int 3918 vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate) 3919 { 3920 vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev; 3921 boolean_t wasoffline; 3922 vdev_state_t oldstate; 3923 3924 spa_vdev_state_enter(spa, SCL_NONE); 3925 3926 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3927 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV))); 3928 3929 if (!vd->vdev_ops->vdev_op_leaf) 3930 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENOTSUP))); 3931 3932 wasoffline = (vd->vdev_offline || vd->vdev_tmpoffline); 3933 oldstate = vd->vdev_state; 3934 3935 tvd = vd->vdev_top; 3936 vd->vdev_offline = B_FALSE; 3937 vd->vdev_tmpoffline = B_FALSE; 3938 vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE); 3939 vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT); 3940 3941 /* XXX - L2ARC 1.0 does not support expansion */ 3942 if (!vd->vdev_aux) { 3943 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 3944 pvd->vdev_expanding = !!((flags & ZFS_ONLINE_EXPAND) || 3945 spa->spa_autoexpand); 3946 vd->vdev_expansion_time = gethrestime_sec(); 3947 } 3948 3949 vdev_reopen(tvd); 3950 vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE; 3951 3952 if (!vd->vdev_aux) { 3953 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 3954 pvd->vdev_expanding = B_FALSE; 3955 } 3956 3957 if (newstate) 3958 *newstate = vd->vdev_state; 3959 if ((flags & ZFS_ONLINE_UNSPARE) && 3960 !vdev_is_dead(vd) && vd->vdev_parent && 3961 vd->vdev_parent->vdev_ops == &vdev_spare_ops && 3962 vd->vdev_parent->vdev_child[0] == vd) 3963 vd->vdev_unspare = B_TRUE; 3964 3965 if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) { 3966 3967 /* XXX - L2ARC 1.0 does not support expansion */ 3968 if (vd->vdev_aux) 3969 return (spa_vdev_state_exit(spa, vd, ENOTSUP)); 3970 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 3971 } 3972 3973 /* Restart initializing if necessary */ 3974 mutex_enter(&vd->vdev_initialize_lock); 3975 if (vdev_writeable(vd) && 3976 vd->vdev_initialize_thread == NULL && 3977 vd->vdev_initialize_state == VDEV_INITIALIZE_ACTIVE) { 3978 (void) vdev_initialize(vd); 3979 } 3980 mutex_exit(&vd->vdev_initialize_lock); 3981 3982 /* 3983 * Restart trimming if necessary. We do not restart trimming for cache 3984 * devices here. This is triggered by l2arc_rebuild_vdev() 3985 * asynchronously for the whole device or in l2arc_evict() as it evicts 3986 * space for upcoming writes. 3987 */ 3988 mutex_enter(&vd->vdev_trim_lock); 3989 if (vdev_writeable(vd) && !vd->vdev_isl2cache && 3990 vd->vdev_trim_thread == NULL && 3991 vd->vdev_trim_state == VDEV_TRIM_ACTIVE) { 3992 (void) vdev_trim(vd, vd->vdev_trim_rate, vd->vdev_trim_partial, 3993 vd->vdev_trim_secure); 3994 } 3995 mutex_exit(&vd->vdev_trim_lock); 3996 3997 if (wasoffline || 3998 (oldstate < VDEV_STATE_DEGRADED && 3999 vd->vdev_state >= VDEV_STATE_DEGRADED)) 4000 spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_ONLINE); 4001 4002 return (spa_vdev_state_exit(spa, vd, 0)); 4003 } 4004 4005 static int 4006 vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags) 4007 { 4008 vdev_t *vd, *tvd; 4009 int error = 0; 4010 uint64_t generation; 4011 metaslab_group_t *mg; 4012 4013 top: 4014 spa_vdev_state_enter(spa, SCL_ALLOC); 4015 4016 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 4017 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV))); 4018 4019 if (!vd->vdev_ops->vdev_op_leaf) 4020 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENOTSUP))); 4021 4022 if (vd->vdev_ops == &vdev_draid_spare_ops) 4023 return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 4024 4025 tvd = vd->vdev_top; 4026 mg = tvd->vdev_mg; 4027 generation = spa->spa_config_generation + 1; 4028 4029 /* 4030 * If the device isn't already offline, try to offline it. 4031 */ 4032 if (!vd->vdev_offline) { 4033 /* 4034 * If this device has the only valid copy of some data, 4035 * don't allow it to be offlined. Log devices are always 4036 * expendable. 4037 */ 4038 if (!tvd->vdev_islog && vd->vdev_aux == NULL && 4039 vdev_dtl_required(vd)) 4040 return (spa_vdev_state_exit(spa, NULL, 4041 SET_ERROR(EBUSY))); 4042 4043 /* 4044 * If the top-level is a slog and it has had allocations 4045 * then proceed. We check that the vdev's metaslab group 4046 * is not NULL since it's possible that we may have just 4047 * added this vdev but not yet initialized its metaslabs. 4048 */ 4049 if (tvd->vdev_islog && mg != NULL) { 4050 /* 4051 * Prevent any future allocations. 4052 */ 4053 ASSERT3P(tvd->vdev_log_mg, ==, NULL); 4054 metaslab_group_passivate(mg); 4055 (void) spa_vdev_state_exit(spa, vd, 0); 4056 4057 error = spa_reset_logs(spa); 4058 4059 /* 4060 * If the log device was successfully reset but has 4061 * checkpointed data, do not offline it. 4062 */ 4063 if (error == 0 && 4064 tvd->vdev_checkpoint_sm != NULL) { 4065 ASSERT3U(space_map_allocated( 4066 tvd->vdev_checkpoint_sm), !=, 0); 4067 error = ZFS_ERR_CHECKPOINT_EXISTS; 4068 } 4069 4070 spa_vdev_state_enter(spa, SCL_ALLOC); 4071 4072 /* 4073 * Check to see if the config has changed. 4074 */ 4075 if (error || generation != spa->spa_config_generation) { 4076 metaslab_group_activate(mg); 4077 if (error) 4078 return (spa_vdev_state_exit(spa, 4079 vd, error)); 4080 (void) spa_vdev_state_exit(spa, vd, 0); 4081 goto top; 4082 } 4083 ASSERT0(tvd->vdev_stat.vs_alloc); 4084 } 4085 4086 /* 4087 * Offline this device and reopen its top-level vdev. 4088 * If the top-level vdev is a log device then just offline 4089 * it. Otherwise, if this action results in the top-level 4090 * vdev becoming unusable, undo it and fail the request. 4091 */ 4092 vd->vdev_offline = B_TRUE; 4093 vdev_reopen(tvd); 4094 4095 if (!tvd->vdev_islog && vd->vdev_aux == NULL && 4096 vdev_is_dead(tvd)) { 4097 vd->vdev_offline = B_FALSE; 4098 vdev_reopen(tvd); 4099 return (spa_vdev_state_exit(spa, NULL, 4100 SET_ERROR(EBUSY))); 4101 } 4102 4103 /* 4104 * Add the device back into the metaslab rotor so that 4105 * once we online the device it's open for business. 4106 */ 4107 if (tvd->vdev_islog && mg != NULL) 4108 metaslab_group_activate(mg); 4109 } 4110 4111 vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY); 4112 4113 return (spa_vdev_state_exit(spa, vd, 0)); 4114 } 4115 4116 int 4117 vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags) 4118 { 4119 int error; 4120 4121 mutex_enter(&spa->spa_vdev_top_lock); 4122 error = vdev_offline_locked(spa, guid, flags); 4123 mutex_exit(&spa->spa_vdev_top_lock); 4124 4125 return (error); 4126 } 4127 4128 /* 4129 * Clear the error counts associated with this vdev. Unlike vdev_online() and 4130 * vdev_offline(), we assume the spa config is locked. We also clear all 4131 * children. If 'vd' is NULL, then the user wants to clear all vdevs. 4132 */ 4133 void 4134 vdev_clear(spa_t *spa, vdev_t *vd) 4135 { 4136 vdev_t *rvd = spa->spa_root_vdev; 4137 4138 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 4139 4140 if (vd == NULL) 4141 vd = rvd; 4142 4143 vd->vdev_stat.vs_read_errors = 0; 4144 vd->vdev_stat.vs_write_errors = 0; 4145 vd->vdev_stat.vs_checksum_errors = 0; 4146 vd->vdev_stat.vs_slow_ios = 0; 4147 4148 for (int c = 0; c < vd->vdev_children; c++) 4149 vdev_clear(spa, vd->vdev_child[c]); 4150 4151 /* 4152 * It makes no sense to "clear" an indirect vdev. 4153 */ 4154 if (!vdev_is_concrete(vd)) 4155 return; 4156 4157 /* 4158 * If we're in the FAULTED state or have experienced failed I/O, then 4159 * clear the persistent state and attempt to reopen the device. We 4160 * also mark the vdev config dirty, so that the new faulted state is 4161 * written out to disk. 4162 */ 4163 if (vd->vdev_faulted || vd->vdev_degraded || 4164 !vdev_readable(vd) || !vdev_writeable(vd)) { 4165 /* 4166 * When reopening in response to a clear event, it may be due to 4167 * a fmadm repair request. In this case, if the device is 4168 * still broken, we want to still post the ereport again. 4169 */ 4170 vd->vdev_forcefault = B_TRUE; 4171 4172 vd->vdev_faulted = vd->vdev_degraded = 0ULL; 4173 vd->vdev_cant_read = B_FALSE; 4174 vd->vdev_cant_write = B_FALSE; 4175 vd->vdev_stat.vs_aux = 0; 4176 4177 vdev_reopen(vd == rvd ? rvd : vd->vdev_top); 4178 4179 vd->vdev_forcefault = B_FALSE; 4180 4181 if (vd != rvd && vdev_writeable(vd->vdev_top)) 4182 vdev_state_dirty(vd->vdev_top); 4183 4184 /* If a resilver isn't required, check if vdevs can be culled */ 4185 if (vd->vdev_aux == NULL && !vdev_is_dead(vd) && 4186 !dsl_scan_resilvering(spa->spa_dsl_pool) && 4187 !dsl_scan_resilver_scheduled(spa->spa_dsl_pool)) 4188 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 4189 4190 spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_CLEAR); 4191 } 4192 4193 /* 4194 * When clearing a FMA-diagnosed fault, we always want to 4195 * unspare the device, as we assume that the original spare was 4196 * done in response to the FMA fault. 4197 */ 4198 if (!vdev_is_dead(vd) && vd->vdev_parent != NULL && 4199 vd->vdev_parent->vdev_ops == &vdev_spare_ops && 4200 vd->vdev_parent->vdev_child[0] == vd) 4201 vd->vdev_unspare = B_TRUE; 4202 4203 /* Clear recent error events cache (i.e. duplicate events tracking) */ 4204 zfs_ereport_clear(spa, vd); 4205 } 4206 4207 boolean_t 4208 vdev_is_dead(vdev_t *vd) 4209 { 4210 /* 4211 * Holes and missing devices are always considered "dead". 4212 * This simplifies the code since we don't have to check for 4213 * these types of devices in the various code paths. 4214 * Instead we rely on the fact that we skip over dead devices 4215 * before issuing I/O to them. 4216 */ 4217 return (vd->vdev_state < VDEV_STATE_DEGRADED || 4218 vd->vdev_ops == &vdev_hole_ops || 4219 vd->vdev_ops == &vdev_missing_ops); 4220 } 4221 4222 boolean_t 4223 vdev_readable(vdev_t *vd) 4224 { 4225 return (!vdev_is_dead(vd) && !vd->vdev_cant_read); 4226 } 4227 4228 boolean_t 4229 vdev_writeable(vdev_t *vd) 4230 { 4231 return (!vdev_is_dead(vd) && !vd->vdev_cant_write && 4232 vdev_is_concrete(vd)); 4233 } 4234 4235 boolean_t 4236 vdev_allocatable(vdev_t *vd) 4237 { 4238 uint64_t state = vd->vdev_state; 4239 4240 /* 4241 * We currently allow allocations from vdevs which may be in the 4242 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device 4243 * fails to reopen then we'll catch it later when we're holding 4244 * the proper locks. Note that we have to get the vdev state 4245 * in a local variable because although it changes atomically, 4246 * we're asking two separate questions about it. 4247 */ 4248 return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) && 4249 !vd->vdev_cant_write && vdev_is_concrete(vd) && 4250 vd->vdev_mg->mg_initialized); 4251 } 4252 4253 boolean_t 4254 vdev_accessible(vdev_t *vd, zio_t *zio) 4255 { 4256 ASSERT(zio->io_vd == vd); 4257 4258 if (vdev_is_dead(vd) || vd->vdev_remove_wanted) 4259 return (B_FALSE); 4260 4261 if (zio->io_type == ZIO_TYPE_READ) 4262 return (!vd->vdev_cant_read); 4263 4264 if (zio->io_type == ZIO_TYPE_WRITE) 4265 return (!vd->vdev_cant_write); 4266 4267 return (B_TRUE); 4268 } 4269 4270 static void 4271 vdev_get_child_stat(vdev_t *cvd, vdev_stat_t *vs, vdev_stat_t *cvs) 4272 { 4273 /* 4274 * Exclude the dRAID spare when aggregating to avoid double counting 4275 * the ops and bytes. These IOs are counted by the physical leaves. 4276 */ 4277 if (cvd->vdev_ops == &vdev_draid_spare_ops) 4278 return; 4279 4280 for (int t = 0; t < VS_ZIO_TYPES; t++) { 4281 vs->vs_ops[t] += cvs->vs_ops[t]; 4282 vs->vs_bytes[t] += cvs->vs_bytes[t]; 4283 } 4284 4285 cvs->vs_scan_removing = cvd->vdev_removing; 4286 } 4287 4288 /* 4289 * Get extended stats 4290 */ 4291 static void 4292 vdev_get_child_stat_ex(vdev_t *cvd, vdev_stat_ex_t *vsx, vdev_stat_ex_t *cvsx) 4293 { 4294 int t, b; 4295 for (t = 0; t < ZIO_TYPES; t++) { 4296 for (b = 0; b < ARRAY_SIZE(vsx->vsx_disk_histo[0]); b++) 4297 vsx->vsx_disk_histo[t][b] += cvsx->vsx_disk_histo[t][b]; 4298 4299 for (b = 0; b < ARRAY_SIZE(vsx->vsx_total_histo[0]); b++) { 4300 vsx->vsx_total_histo[t][b] += 4301 cvsx->vsx_total_histo[t][b]; 4302 } 4303 } 4304 4305 for (t = 0; t < ZIO_PRIORITY_NUM_QUEUEABLE; t++) { 4306 for (b = 0; b < ARRAY_SIZE(vsx->vsx_queue_histo[0]); b++) { 4307 vsx->vsx_queue_histo[t][b] += 4308 cvsx->vsx_queue_histo[t][b]; 4309 } 4310 vsx->vsx_active_queue[t] += cvsx->vsx_active_queue[t]; 4311 vsx->vsx_pend_queue[t] += cvsx->vsx_pend_queue[t]; 4312 4313 for (b = 0; b < ARRAY_SIZE(vsx->vsx_ind_histo[0]); b++) 4314 vsx->vsx_ind_histo[t][b] += cvsx->vsx_ind_histo[t][b]; 4315 4316 for (b = 0; b < ARRAY_SIZE(vsx->vsx_agg_histo[0]); b++) 4317 vsx->vsx_agg_histo[t][b] += cvsx->vsx_agg_histo[t][b]; 4318 } 4319 4320 } 4321 4322 boolean_t 4323 vdev_is_spacemap_addressable(vdev_t *vd) 4324 { 4325 if (spa_feature_is_active(vd->vdev_spa, SPA_FEATURE_SPACEMAP_V2)) 4326 return (B_TRUE); 4327 4328 /* 4329 * If double-word space map entries are not enabled we assume 4330 * 47 bits of the space map entry are dedicated to the entry's 4331 * offset (see SM_OFFSET_BITS in space_map.h). We then use that 4332 * to calculate the maximum address that can be described by a 4333 * space map entry for the given device. 4334 */ 4335 uint64_t shift = vd->vdev_ashift + SM_OFFSET_BITS; 4336 4337 if (shift >= 63) /* detect potential overflow */ 4338 return (B_TRUE); 4339 4340 return (vd->vdev_asize < (1ULL << shift)); 4341 } 4342 4343 /* 4344 * Get statistics for the given vdev. 4345 */ 4346 static void 4347 vdev_get_stats_ex_impl(vdev_t *vd, vdev_stat_t *vs, vdev_stat_ex_t *vsx) 4348 { 4349 int t; 4350 /* 4351 * If we're getting stats on the root vdev, aggregate the I/O counts 4352 * over all top-level vdevs (i.e. the direct children of the root). 4353 */ 4354 if (!vd->vdev_ops->vdev_op_leaf) { 4355 if (vs) { 4356 memset(vs->vs_ops, 0, sizeof (vs->vs_ops)); 4357 memset(vs->vs_bytes, 0, sizeof (vs->vs_bytes)); 4358 } 4359 if (vsx) 4360 memset(vsx, 0, sizeof (*vsx)); 4361 4362 for (int c = 0; c < vd->vdev_children; c++) { 4363 vdev_t *cvd = vd->vdev_child[c]; 4364 vdev_stat_t *cvs = &cvd->vdev_stat; 4365 vdev_stat_ex_t *cvsx = &cvd->vdev_stat_ex; 4366 4367 vdev_get_stats_ex_impl(cvd, cvs, cvsx); 4368 if (vs) 4369 vdev_get_child_stat(cvd, vs, cvs); 4370 if (vsx) 4371 vdev_get_child_stat_ex(cvd, vsx, cvsx); 4372 } 4373 } else { 4374 /* 4375 * We're a leaf. Just copy our ZIO active queue stats in. The 4376 * other leaf stats are updated in vdev_stat_update(). 4377 */ 4378 if (!vsx) 4379 return; 4380 4381 memcpy(vsx, &vd->vdev_stat_ex, sizeof (vd->vdev_stat_ex)); 4382 4383 for (t = 0; t < ARRAY_SIZE(vd->vdev_queue.vq_class); t++) { 4384 vsx->vsx_active_queue[t] = 4385 vd->vdev_queue.vq_class[t].vqc_active; 4386 vsx->vsx_pend_queue[t] = avl_numnodes( 4387 &vd->vdev_queue.vq_class[t].vqc_queued_tree); 4388 } 4389 } 4390 } 4391 4392 void 4393 vdev_get_stats_ex(vdev_t *vd, vdev_stat_t *vs, vdev_stat_ex_t *vsx) 4394 { 4395 vdev_t *tvd = vd->vdev_top; 4396 mutex_enter(&vd->vdev_stat_lock); 4397 if (vs) { 4398 bcopy(&vd->vdev_stat, vs, sizeof (*vs)); 4399 vs->vs_timestamp = gethrtime() - vs->vs_timestamp; 4400 vs->vs_state = vd->vdev_state; 4401 vs->vs_rsize = vdev_get_min_asize(vd); 4402 4403 if (vd->vdev_ops->vdev_op_leaf) { 4404 vs->vs_rsize += VDEV_LABEL_START_SIZE + 4405 VDEV_LABEL_END_SIZE; 4406 /* 4407 * Report initializing progress. Since we don't 4408 * have the initializing locks held, this is only 4409 * an estimate (although a fairly accurate one). 4410 */ 4411 vs->vs_initialize_bytes_done = 4412 vd->vdev_initialize_bytes_done; 4413 vs->vs_initialize_bytes_est = 4414 vd->vdev_initialize_bytes_est; 4415 vs->vs_initialize_state = vd->vdev_initialize_state; 4416 vs->vs_initialize_action_time = 4417 vd->vdev_initialize_action_time; 4418 4419 /* 4420 * Report manual TRIM progress. Since we don't have 4421 * the manual TRIM locks held, this is only an 4422 * estimate (although fairly accurate one). 4423 */ 4424 vs->vs_trim_notsup = !vd->vdev_has_trim; 4425 vs->vs_trim_bytes_done = vd->vdev_trim_bytes_done; 4426 vs->vs_trim_bytes_est = vd->vdev_trim_bytes_est; 4427 vs->vs_trim_state = vd->vdev_trim_state; 4428 vs->vs_trim_action_time = vd->vdev_trim_action_time; 4429 4430 /* Set when there is a deferred resilver. */ 4431 vs->vs_resilver_deferred = vd->vdev_resilver_deferred; 4432 } 4433 4434 /* 4435 * Report expandable space on top-level, non-auxiliary devices 4436 * only. The expandable space is reported in terms of metaslab 4437 * sized units since that determines how much space the pool 4438 * can expand. 4439 */ 4440 if (vd->vdev_aux == NULL && tvd != NULL) { 4441 vs->vs_esize = P2ALIGN( 4442 vd->vdev_max_asize - vd->vdev_asize, 4443 1ULL << tvd->vdev_ms_shift); 4444 } 4445 4446 vs->vs_configured_ashift = vd->vdev_top != NULL 4447 ? vd->vdev_top->vdev_ashift : vd->vdev_ashift; 4448 vs->vs_logical_ashift = vd->vdev_logical_ashift; 4449 vs->vs_physical_ashift = vd->vdev_physical_ashift; 4450 4451 /* 4452 * Report fragmentation and rebuild progress for top-level, 4453 * non-auxiliary, concrete devices. 4454 */ 4455 if (vd->vdev_aux == NULL && vd == vd->vdev_top && 4456 vdev_is_concrete(vd)) { 4457 /* 4458 * The vdev fragmentation rating doesn't take into 4459 * account the embedded slog metaslab (vdev_log_mg). 4460 * Since it's only one metaslab, it would have a tiny 4461 * impact on the overall fragmentation. 4462 */ 4463 vs->vs_fragmentation = (vd->vdev_mg != NULL) ? 4464 vd->vdev_mg->mg_fragmentation : 0; 4465 } 4466 } 4467 4468 vdev_get_stats_ex_impl(vd, vs, vsx); 4469 mutex_exit(&vd->vdev_stat_lock); 4470 } 4471 4472 void 4473 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs) 4474 { 4475 return (vdev_get_stats_ex(vd, vs, NULL)); 4476 } 4477 4478 void 4479 vdev_clear_stats(vdev_t *vd) 4480 { 4481 mutex_enter(&vd->vdev_stat_lock); 4482 vd->vdev_stat.vs_space = 0; 4483 vd->vdev_stat.vs_dspace = 0; 4484 vd->vdev_stat.vs_alloc = 0; 4485 mutex_exit(&vd->vdev_stat_lock); 4486 } 4487 4488 void 4489 vdev_scan_stat_init(vdev_t *vd) 4490 { 4491 vdev_stat_t *vs = &vd->vdev_stat; 4492 4493 for (int c = 0; c < vd->vdev_children; c++) 4494 vdev_scan_stat_init(vd->vdev_child[c]); 4495 4496 mutex_enter(&vd->vdev_stat_lock); 4497 vs->vs_scan_processed = 0; 4498 mutex_exit(&vd->vdev_stat_lock); 4499 } 4500 4501 void 4502 vdev_stat_update(zio_t *zio, uint64_t psize) 4503 { 4504 spa_t *spa = zio->io_spa; 4505 vdev_t *rvd = spa->spa_root_vdev; 4506 vdev_t *vd = zio->io_vd ? zio->io_vd : rvd; 4507 vdev_t *pvd; 4508 uint64_t txg = zio->io_txg; 4509 vdev_stat_t *vs = &vd->vdev_stat; 4510 vdev_stat_ex_t *vsx = &vd->vdev_stat_ex; 4511 zio_type_t type = zio->io_type; 4512 int flags = zio->io_flags; 4513 4514 /* 4515 * If this i/o is a gang leader, it didn't do any actual work. 4516 */ 4517 if (zio->io_gang_tree) 4518 return; 4519 4520 if (zio->io_error == 0) { 4521 /* 4522 * If this is a root i/o, don't count it -- we've already 4523 * counted the top-level vdevs, and vdev_get_stats() will 4524 * aggregate them when asked. This reduces contention on 4525 * the root vdev_stat_lock and implicitly handles blocks 4526 * that compress away to holes, for which there is no i/o. 4527 * (Holes never create vdev children, so all the counters 4528 * remain zero, which is what we want.) 4529 * 4530 * Note: this only applies to successful i/o (io_error == 0) 4531 * because unlike i/o counts, errors are not additive. 4532 * When reading a ditto block, for example, failure of 4533 * one top-level vdev does not imply a root-level error. 4534 */ 4535 if (vd == rvd) 4536 return; 4537 4538 ASSERT(vd == zio->io_vd); 4539 4540 if (flags & ZIO_FLAG_IO_BYPASS) 4541 return; 4542 4543 mutex_enter(&vd->vdev_stat_lock); 4544 4545 if (flags & ZIO_FLAG_IO_REPAIR) { 4546 /* 4547 * Repair is the result of a resilver issued by the 4548 * scan thread (spa_sync). 4549 */ 4550 if (flags & ZIO_FLAG_SCAN_THREAD) { 4551 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; 4552 dsl_scan_phys_t *scn_phys = &scn->scn_phys; 4553 uint64_t *processed = &scn_phys->scn_processed; 4554 4555 if (vd->vdev_ops->vdev_op_leaf) 4556 atomic_add_64(processed, psize); 4557 vs->vs_scan_processed += psize; 4558 } 4559 4560 /* 4561 * Repair is the result of a rebuild issued by the 4562 * rebuild thread (vdev_rebuild_thread). To avoid 4563 * double counting repaired bytes the virtual dRAID 4564 * spare vdev is excluded from the processed bytes. 4565 */ 4566 if (zio->io_priority == ZIO_PRIORITY_REBUILD) { 4567 vdev_t *tvd = vd->vdev_top; 4568 vdev_rebuild_t *vr = &tvd->vdev_rebuild_config; 4569 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys; 4570 uint64_t *rebuilt = &vrp->vrp_bytes_rebuilt; 4571 4572 if (vd->vdev_ops->vdev_op_leaf && 4573 vd->vdev_ops != &vdev_draid_spare_ops) { 4574 atomic_add_64(rebuilt, psize); 4575 } 4576 vs->vs_rebuild_processed += psize; 4577 } 4578 4579 if (flags & ZIO_FLAG_SELF_HEAL) 4580 vs->vs_self_healed += psize; 4581 } 4582 4583 /* 4584 * The bytes/ops/histograms are recorded at the leaf level and 4585 * aggregated into the higher level vdevs in vdev_get_stats(). 4586 */ 4587 if (vd->vdev_ops->vdev_op_leaf && 4588 (zio->io_priority < ZIO_PRIORITY_NUM_QUEUEABLE)) { 4589 zio_type_t vs_type = type; 4590 zio_priority_t priority = zio->io_priority; 4591 4592 /* 4593 * TRIM ops and bytes are reported to user space as 4594 * ZIO_TYPE_IOCTL. This is done to preserve the 4595 * vdev_stat_t structure layout for user space. 4596 */ 4597 if (type == ZIO_TYPE_TRIM) 4598 vs_type = ZIO_TYPE_IOCTL; 4599 4600 /* 4601 * Solely for the purposes of 'zpool iostat -lqrw' 4602 * reporting use the priority to categorize the IO. 4603 * Only the following are reported to user space: 4604 * 4605 * ZIO_PRIORITY_SYNC_READ, 4606 * ZIO_PRIORITY_SYNC_WRITE, 4607 * ZIO_PRIORITY_ASYNC_READ, 4608 * ZIO_PRIORITY_ASYNC_WRITE, 4609 * ZIO_PRIORITY_SCRUB, 4610 * ZIO_PRIORITY_TRIM, 4611 * ZIO_PRIORITY_REBUILD. 4612 */ 4613 if (priority == ZIO_PRIORITY_INITIALIZING) { 4614 ASSERT3U(type, ==, ZIO_TYPE_WRITE); 4615 priority = ZIO_PRIORITY_ASYNC_WRITE; 4616 } else if (priority == ZIO_PRIORITY_REMOVAL) { 4617 priority = ((type == ZIO_TYPE_WRITE) ? 4618 ZIO_PRIORITY_ASYNC_WRITE : 4619 ZIO_PRIORITY_ASYNC_READ); 4620 } 4621 4622 vs->vs_ops[vs_type]++; 4623 vs->vs_bytes[vs_type] += psize; 4624 4625 if (flags & ZIO_FLAG_DELEGATED) { 4626 vsx->vsx_agg_histo[priority] 4627 [RQ_HISTO(zio->io_size)]++; 4628 } else { 4629 vsx->vsx_ind_histo[priority] 4630 [RQ_HISTO(zio->io_size)]++; 4631 } 4632 4633 if (zio->io_delta && zio->io_delay) { 4634 vsx->vsx_queue_histo[priority] 4635 [L_HISTO(zio->io_delta - zio->io_delay)]++; 4636 vsx->vsx_disk_histo[type] 4637 [L_HISTO(zio->io_delay)]++; 4638 vsx->vsx_total_histo[type] 4639 [L_HISTO(zio->io_delta)]++; 4640 } 4641 } 4642 4643 mutex_exit(&vd->vdev_stat_lock); 4644 return; 4645 } 4646 4647 if (flags & ZIO_FLAG_SPECULATIVE) 4648 return; 4649 4650 /* 4651 * If this is an I/O error that is going to be retried, then ignore the 4652 * error. Otherwise, the user may interpret B_FAILFAST I/O errors as 4653 * hard errors, when in reality they can happen for any number of 4654 * innocuous reasons (bus resets, MPxIO link failure, etc). 4655 */ 4656 if (zio->io_error == EIO && 4657 !(zio->io_flags & ZIO_FLAG_IO_RETRY)) 4658 return; 4659 4660 /* 4661 * Intent logs writes won't propagate their error to the root 4662 * I/O so don't mark these types of failures as pool-level 4663 * errors. 4664 */ 4665 if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE)) 4666 return; 4667 4668 if (type == ZIO_TYPE_WRITE && txg != 0 && 4669 (!(flags & ZIO_FLAG_IO_REPAIR) || 4670 (flags & ZIO_FLAG_SCAN_THREAD) || 4671 spa->spa_claiming)) { 4672 /* 4673 * This is either a normal write (not a repair), or it's 4674 * a repair induced by the scrub thread, or it's a repair 4675 * made by zil_claim() during spa_load() in the first txg. 4676 * In the normal case, we commit the DTL change in the same 4677 * txg as the block was born. In the scrub-induced repair 4678 * case, we know that scrubs run in first-pass syncing context, 4679 * so we commit the DTL change in spa_syncing_txg(spa). 4680 * In the zil_claim() case, we commit in spa_first_txg(spa). 4681 * 4682 * We currently do not make DTL entries for failed spontaneous 4683 * self-healing writes triggered by normal (non-scrubbing) 4684 * reads, because we have no transactional context in which to 4685 * do so -- and it's not clear that it'd be desirable anyway. 4686 */ 4687 if (vd->vdev_ops->vdev_op_leaf) { 4688 uint64_t commit_txg = txg; 4689 if (flags & ZIO_FLAG_SCAN_THREAD) { 4690 ASSERT(flags & ZIO_FLAG_IO_REPAIR); 4691 ASSERT(spa_sync_pass(spa) == 1); 4692 vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1); 4693 commit_txg = spa_syncing_txg(spa); 4694 } else if (spa->spa_claiming) { 4695 ASSERT(flags & ZIO_FLAG_IO_REPAIR); 4696 commit_txg = spa_first_txg(spa); 4697 } 4698 ASSERT(commit_txg >= spa_syncing_txg(spa)); 4699 if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1)) 4700 return; 4701 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 4702 vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1); 4703 vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg); 4704 } 4705 if (vd != rvd) 4706 vdev_dtl_dirty(vd, DTL_MISSING, txg, 1); 4707 } 4708 } 4709 4710 int64_t 4711 vdev_deflated_space(vdev_t *vd, int64_t space) 4712 { 4713 ASSERT((space & (SPA_MINBLOCKSIZE-1)) == 0); 4714 ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache); 4715 4716 return ((space >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio); 4717 } 4718 4719 /* 4720 * Update the in-core space usage stats for this vdev, its metaslab class, 4721 * and the root vdev. 4722 */ 4723 void 4724 vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta, 4725 int64_t space_delta) 4726 { 4727 int64_t dspace_delta; 4728 spa_t *spa = vd->vdev_spa; 4729 vdev_t *rvd = spa->spa_root_vdev; 4730 4731 ASSERT(vd == vd->vdev_top); 4732 4733 /* 4734 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion 4735 * factor. We must calculate this here and not at the root vdev 4736 * because the root vdev's psize-to-asize is simply the max of its 4737 * children's, thus not accurate enough for us. 4738 */ 4739 dspace_delta = vdev_deflated_space(vd, space_delta); 4740 4741 mutex_enter(&vd->vdev_stat_lock); 4742 /* ensure we won't underflow */ 4743 if (alloc_delta < 0) { 4744 ASSERT3U(vd->vdev_stat.vs_alloc, >=, -alloc_delta); 4745 } 4746 4747 vd->vdev_stat.vs_alloc += alloc_delta; 4748 vd->vdev_stat.vs_space += space_delta; 4749 vd->vdev_stat.vs_dspace += dspace_delta; 4750 mutex_exit(&vd->vdev_stat_lock); 4751 4752 /* every class but log contributes to root space stats */ 4753 if (vd->vdev_mg != NULL && !vd->vdev_islog) { 4754 ASSERT(!vd->vdev_isl2cache); 4755 mutex_enter(&rvd->vdev_stat_lock); 4756 rvd->vdev_stat.vs_alloc += alloc_delta; 4757 rvd->vdev_stat.vs_space += space_delta; 4758 rvd->vdev_stat.vs_dspace += dspace_delta; 4759 mutex_exit(&rvd->vdev_stat_lock); 4760 } 4761 /* Note: metaslab_class_space_update moved to metaslab_space_update */ 4762 } 4763 4764 /* 4765 * Mark a top-level vdev's config as dirty, placing it on the dirty list 4766 * so that it will be written out next time the vdev configuration is synced. 4767 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs. 4768 */ 4769 void 4770 vdev_config_dirty(vdev_t *vd) 4771 { 4772 spa_t *spa = vd->vdev_spa; 4773 vdev_t *rvd = spa->spa_root_vdev; 4774 int c; 4775 4776 ASSERT(spa_writeable(spa)); 4777 4778 /* 4779 * If this is an aux vdev (as with l2cache and spare devices), then we 4780 * update the vdev config manually and set the sync flag. 4781 */ 4782 if (vd->vdev_aux != NULL) { 4783 spa_aux_vdev_t *sav = vd->vdev_aux; 4784 nvlist_t **aux; 4785 uint_t naux; 4786 4787 for (c = 0; c < sav->sav_count; c++) { 4788 if (sav->sav_vdevs[c] == vd) 4789 break; 4790 } 4791 4792 if (c == sav->sav_count) { 4793 /* 4794 * We're being removed. There's nothing more to do. 4795 */ 4796 ASSERT(sav->sav_sync == B_TRUE); 4797 return; 4798 } 4799 4800 sav->sav_sync = B_TRUE; 4801 4802 if (nvlist_lookup_nvlist_array(sav->sav_config, 4803 ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) { 4804 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 4805 ZPOOL_CONFIG_SPARES, &aux, &naux) == 0); 4806 } 4807 4808 ASSERT(c < naux); 4809 4810 /* 4811 * Setting the nvlist in the middle if the array is a little 4812 * sketchy, but it will work. 4813 */ 4814 nvlist_free(aux[c]); 4815 aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0); 4816 4817 return; 4818 } 4819 4820 /* 4821 * The dirty list is protected by the SCL_CONFIG lock. The caller 4822 * must either hold SCL_CONFIG as writer, or must be the sync thread 4823 * (which holds SCL_CONFIG as reader). There's only one sync thread, 4824 * so this is sufficient to ensure mutual exclusion. 4825 */ 4826 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 4827 (dsl_pool_sync_context(spa_get_dsl(spa)) && 4828 spa_config_held(spa, SCL_CONFIG, RW_READER))); 4829 4830 if (vd == rvd) { 4831 for (c = 0; c < rvd->vdev_children; c++) 4832 vdev_config_dirty(rvd->vdev_child[c]); 4833 } else { 4834 ASSERT(vd == vd->vdev_top); 4835 4836 if (!list_link_active(&vd->vdev_config_dirty_node) && 4837 vdev_is_concrete(vd)) { 4838 list_insert_head(&spa->spa_config_dirty_list, vd); 4839 } 4840 } 4841 } 4842 4843 void 4844 vdev_config_clean(vdev_t *vd) 4845 { 4846 spa_t *spa = vd->vdev_spa; 4847 4848 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 4849 (dsl_pool_sync_context(spa_get_dsl(spa)) && 4850 spa_config_held(spa, SCL_CONFIG, RW_READER))); 4851 4852 ASSERT(list_link_active(&vd->vdev_config_dirty_node)); 4853 list_remove(&spa->spa_config_dirty_list, vd); 4854 } 4855 4856 /* 4857 * Mark a top-level vdev's state as dirty, so that the next pass of 4858 * spa_sync() can convert this into vdev_config_dirty(). We distinguish 4859 * the state changes from larger config changes because they require 4860 * much less locking, and are often needed for administrative actions. 4861 */ 4862 void 4863 vdev_state_dirty(vdev_t *vd) 4864 { 4865 spa_t *spa = vd->vdev_spa; 4866 4867 ASSERT(spa_writeable(spa)); 4868 ASSERT(vd == vd->vdev_top); 4869 4870 /* 4871 * The state list is protected by the SCL_STATE lock. The caller 4872 * must either hold SCL_STATE as writer, or must be the sync thread 4873 * (which holds SCL_STATE as reader). There's only one sync thread, 4874 * so this is sufficient to ensure mutual exclusion. 4875 */ 4876 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 4877 (dsl_pool_sync_context(spa_get_dsl(spa)) && 4878 spa_config_held(spa, SCL_STATE, RW_READER))); 4879 4880 if (!list_link_active(&vd->vdev_state_dirty_node) && 4881 vdev_is_concrete(vd)) 4882 list_insert_head(&spa->spa_state_dirty_list, vd); 4883 } 4884 4885 void 4886 vdev_state_clean(vdev_t *vd) 4887 { 4888 spa_t *spa = vd->vdev_spa; 4889 4890 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 4891 (dsl_pool_sync_context(spa_get_dsl(spa)) && 4892 spa_config_held(spa, SCL_STATE, RW_READER))); 4893 4894 ASSERT(list_link_active(&vd->vdev_state_dirty_node)); 4895 list_remove(&spa->spa_state_dirty_list, vd); 4896 } 4897 4898 /* 4899 * Propagate vdev state up from children to parent. 4900 */ 4901 void 4902 vdev_propagate_state(vdev_t *vd) 4903 { 4904 spa_t *spa = vd->vdev_spa; 4905 vdev_t *rvd = spa->spa_root_vdev; 4906 int degraded = 0, faulted = 0; 4907 int corrupted = 0; 4908 vdev_t *child; 4909 4910 if (vd->vdev_children > 0) { 4911 for (int c = 0; c < vd->vdev_children; c++) { 4912 child = vd->vdev_child[c]; 4913 4914 /* 4915 * Don't factor holes or indirect vdevs into the 4916 * decision. 4917 */ 4918 if (!vdev_is_concrete(child)) 4919 continue; 4920 4921 if (!vdev_readable(child) || 4922 (!vdev_writeable(child) && spa_writeable(spa))) { 4923 /* 4924 * Root special: if there is a top-level log 4925 * device, treat the root vdev as if it were 4926 * degraded. 4927 */ 4928 if (child->vdev_islog && vd == rvd) 4929 degraded++; 4930 else 4931 faulted++; 4932 } else if (child->vdev_state <= VDEV_STATE_DEGRADED) { 4933 degraded++; 4934 } 4935 4936 if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA) 4937 corrupted++; 4938 } 4939 4940 vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded); 4941 4942 /* 4943 * Root special: if there is a top-level vdev that cannot be 4944 * opened due to corrupted metadata, then propagate the root 4945 * vdev's aux state as 'corrupt' rather than 'insufficient 4946 * replicas'. 4947 */ 4948 if (corrupted && vd == rvd && 4949 rvd->vdev_state == VDEV_STATE_CANT_OPEN) 4950 vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN, 4951 VDEV_AUX_CORRUPT_DATA); 4952 } 4953 4954 if (vd->vdev_parent) 4955 vdev_propagate_state(vd->vdev_parent); 4956 } 4957 4958 /* 4959 * Set a vdev's state. If this is during an open, we don't update the parent 4960 * state, because we're in the process of opening children depth-first. 4961 * Otherwise, we propagate the change to the parent. 4962 * 4963 * If this routine places a device in a faulted state, an appropriate ereport is 4964 * generated. 4965 */ 4966 void 4967 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux) 4968 { 4969 uint64_t save_state; 4970 spa_t *spa = vd->vdev_spa; 4971 4972 if (state == vd->vdev_state) { 4973 /* 4974 * Since vdev_offline() code path is already in an offline 4975 * state we can miss a statechange event to OFFLINE. Check 4976 * the previous state to catch this condition. 4977 */ 4978 if (vd->vdev_ops->vdev_op_leaf && 4979 (state == VDEV_STATE_OFFLINE) && 4980 (vd->vdev_prevstate >= VDEV_STATE_FAULTED)) { 4981 /* post an offline state change */ 4982 zfs_post_state_change(spa, vd, vd->vdev_prevstate); 4983 } 4984 vd->vdev_stat.vs_aux = aux; 4985 return; 4986 } 4987 4988 save_state = vd->vdev_state; 4989 4990 vd->vdev_state = state; 4991 vd->vdev_stat.vs_aux = aux; 4992 4993 /* 4994 * If we are setting the vdev state to anything but an open state, then 4995 * always close the underlying device unless the device has requested 4996 * a delayed close (i.e. we're about to remove or fault the device). 4997 * Otherwise, we keep accessible but invalid devices open forever. 4998 * We don't call vdev_close() itself, because that implies some extra 4999 * checks (offline, etc) that we don't want here. This is limited to 5000 * leaf devices, because otherwise closing the device will affect other 5001 * children. 5002 */ 5003 if (!vd->vdev_delayed_close && vdev_is_dead(vd) && 5004 vd->vdev_ops->vdev_op_leaf) 5005 vd->vdev_ops->vdev_op_close(vd); 5006 5007 if (vd->vdev_removed && 5008 state == VDEV_STATE_CANT_OPEN && 5009 (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) { 5010 /* 5011 * If the previous state is set to VDEV_STATE_REMOVED, then this 5012 * device was previously marked removed and someone attempted to 5013 * reopen it. If this failed due to a nonexistent device, then 5014 * keep the device in the REMOVED state. We also let this be if 5015 * it is one of our special test online cases, which is only 5016 * attempting to online the device and shouldn't generate an FMA 5017 * fault. 5018 */ 5019 vd->vdev_state = VDEV_STATE_REMOVED; 5020 vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 5021 } else if (state == VDEV_STATE_REMOVED) { 5022 vd->vdev_removed = B_TRUE; 5023 } else if (state == VDEV_STATE_CANT_OPEN) { 5024 /* 5025 * If we fail to open a vdev during an import or recovery, we 5026 * mark it as "not available", which signifies that it was 5027 * never there to begin with. Failure to open such a device 5028 * is not considered an error. 5029 */ 5030 if ((spa_load_state(spa) == SPA_LOAD_IMPORT || 5031 spa_load_state(spa) == SPA_LOAD_RECOVER) && 5032 vd->vdev_ops->vdev_op_leaf) 5033 vd->vdev_not_present = 1; 5034 5035 /* 5036 * Post the appropriate ereport. If the 'prevstate' field is 5037 * set to something other than VDEV_STATE_UNKNOWN, it indicates 5038 * that this is part of a vdev_reopen(). In this case, we don't 5039 * want to post the ereport if the device was already in the 5040 * CANT_OPEN state beforehand. 5041 * 5042 * If the 'checkremove' flag is set, then this is an attempt to 5043 * online the device in response to an insertion event. If we 5044 * hit this case, then we have detected an insertion event for a 5045 * faulted or offline device that wasn't in the removed state. 5046 * In this scenario, we don't post an ereport because we are 5047 * about to replace the device, or attempt an online with 5048 * vdev_forcefault, which will generate the fault for us. 5049 */ 5050 if ((vd->vdev_prevstate != state || vd->vdev_forcefault) && 5051 !vd->vdev_not_present && !vd->vdev_checkremove && 5052 vd != spa->spa_root_vdev) { 5053 const char *class; 5054 5055 switch (aux) { 5056 case VDEV_AUX_OPEN_FAILED: 5057 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED; 5058 break; 5059 case VDEV_AUX_CORRUPT_DATA: 5060 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA; 5061 break; 5062 case VDEV_AUX_NO_REPLICAS: 5063 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS; 5064 break; 5065 case VDEV_AUX_BAD_GUID_SUM: 5066 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM; 5067 break; 5068 case VDEV_AUX_TOO_SMALL: 5069 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL; 5070 break; 5071 case VDEV_AUX_BAD_LABEL: 5072 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL; 5073 break; 5074 case VDEV_AUX_BAD_ASHIFT: 5075 class = FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT; 5076 break; 5077 default: 5078 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN; 5079 } 5080 5081 (void) zfs_ereport_post(class, spa, vd, NULL, NULL, 5082 save_state); 5083 } 5084 5085 /* Erase any notion of persistent removed state */ 5086 vd->vdev_removed = B_FALSE; 5087 } else { 5088 vd->vdev_removed = B_FALSE; 5089 } 5090 5091 /* 5092 * Notify ZED of any significant state-change on a leaf vdev. 5093 * 5094 */ 5095 if (vd->vdev_ops->vdev_op_leaf) { 5096 /* preserve original state from a vdev_reopen() */ 5097 if ((vd->vdev_prevstate != VDEV_STATE_UNKNOWN) && 5098 (vd->vdev_prevstate != vd->vdev_state) && 5099 (save_state <= VDEV_STATE_CLOSED)) 5100 save_state = vd->vdev_prevstate; 5101 5102 /* filter out state change due to initial vdev_open */ 5103 if (save_state > VDEV_STATE_CLOSED) 5104 zfs_post_state_change(spa, vd, save_state); 5105 } 5106 5107 if (!isopen && vd->vdev_parent) 5108 vdev_propagate_state(vd->vdev_parent); 5109 } 5110 5111 boolean_t 5112 vdev_children_are_offline(vdev_t *vd) 5113 { 5114 ASSERT(!vd->vdev_ops->vdev_op_leaf); 5115 5116 for (uint64_t i = 0; i < vd->vdev_children; i++) { 5117 if (vd->vdev_child[i]->vdev_state != VDEV_STATE_OFFLINE) 5118 return (B_FALSE); 5119 } 5120 5121 return (B_TRUE); 5122 } 5123 5124 /* 5125 * Check the vdev configuration to ensure that it's capable of supporting 5126 * a root pool. We do not support partial configuration. 5127 */ 5128 boolean_t 5129 vdev_is_bootable(vdev_t *vd) 5130 { 5131 if (!vd->vdev_ops->vdev_op_leaf) { 5132 const char *vdev_type = vd->vdev_ops->vdev_op_type; 5133 5134 if (strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) 5135 return (B_FALSE); 5136 } 5137 5138 for (int c = 0; c < vd->vdev_children; c++) { 5139 if (!vdev_is_bootable(vd->vdev_child[c])) 5140 return (B_FALSE); 5141 } 5142 return (B_TRUE); 5143 } 5144 5145 boolean_t 5146 vdev_is_concrete(vdev_t *vd) 5147 { 5148 vdev_ops_t *ops = vd->vdev_ops; 5149 if (ops == &vdev_indirect_ops || ops == &vdev_hole_ops || 5150 ops == &vdev_missing_ops || ops == &vdev_root_ops) { 5151 return (B_FALSE); 5152 } else { 5153 return (B_TRUE); 5154 } 5155 } 5156 5157 /* 5158 * Determine if a log device has valid content. If the vdev was 5159 * removed or faulted in the MOS config then we know that 5160 * the content on the log device has already been written to the pool. 5161 */ 5162 boolean_t 5163 vdev_log_state_valid(vdev_t *vd) 5164 { 5165 if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted && 5166 !vd->vdev_removed) 5167 return (B_TRUE); 5168 5169 for (int c = 0; c < vd->vdev_children; c++) 5170 if (vdev_log_state_valid(vd->vdev_child[c])) 5171 return (B_TRUE); 5172 5173 return (B_FALSE); 5174 } 5175 5176 /* 5177 * Expand a vdev if possible. 5178 */ 5179 void 5180 vdev_expand(vdev_t *vd, uint64_t txg) 5181 { 5182 ASSERT(vd->vdev_top == vd); 5183 ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 5184 ASSERT(vdev_is_concrete(vd)); 5185 5186 vdev_set_deflate_ratio(vd); 5187 5188 if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count && 5189 vdev_is_concrete(vd)) { 5190 vdev_metaslab_group_create(vd); 5191 VERIFY(vdev_metaslab_init(vd, txg) == 0); 5192 vdev_config_dirty(vd); 5193 } 5194 } 5195 5196 /* 5197 * Split a vdev. 5198 */ 5199 void 5200 vdev_split(vdev_t *vd) 5201 { 5202 vdev_t *cvd, *pvd = vd->vdev_parent; 5203 5204 vdev_remove_child(pvd, vd); 5205 vdev_compact_children(pvd); 5206 5207 cvd = pvd->vdev_child[0]; 5208 if (pvd->vdev_children == 1) { 5209 vdev_remove_parent(cvd); 5210 cvd->vdev_splitting = B_TRUE; 5211 } 5212 vdev_propagate_state(cvd); 5213 } 5214 5215 void 5216 vdev_deadman(vdev_t *vd, char *tag) 5217 { 5218 for (int c = 0; c < vd->vdev_children; c++) { 5219 vdev_t *cvd = vd->vdev_child[c]; 5220 5221 vdev_deadman(cvd, tag); 5222 } 5223 5224 if (vd->vdev_ops->vdev_op_leaf) { 5225 vdev_queue_t *vq = &vd->vdev_queue; 5226 5227 mutex_enter(&vq->vq_lock); 5228 if (avl_numnodes(&vq->vq_active_tree) > 0) { 5229 spa_t *spa = vd->vdev_spa; 5230 zio_t *fio; 5231 uint64_t delta; 5232 5233 zfs_dbgmsg("slow vdev: %s has %lu active IOs", 5234 vd->vdev_path, avl_numnodes(&vq->vq_active_tree)); 5235 5236 /* 5237 * Look at the head of all the pending queues, 5238 * if any I/O has been outstanding for longer than 5239 * the spa_deadman_synctime invoke the deadman logic. 5240 */ 5241 fio = avl_first(&vq->vq_active_tree); 5242 delta = gethrtime() - fio->io_timestamp; 5243 if (delta > spa_deadman_synctime(spa)) 5244 zio_deadman(fio, tag); 5245 } 5246 mutex_exit(&vq->vq_lock); 5247 } 5248 } 5249 5250 void 5251 vdev_defer_resilver(vdev_t *vd) 5252 { 5253 ASSERT(vd->vdev_ops->vdev_op_leaf); 5254 5255 vd->vdev_resilver_deferred = B_TRUE; 5256 vd->vdev_spa->spa_resilver_deferred = B_TRUE; 5257 } 5258 5259 /* 5260 * Clears the resilver deferred flag on all leaf devs under vd. Returns 5261 * B_TRUE if we have devices that need to be resilvered and are available to 5262 * accept resilver I/Os. 5263 */ 5264 boolean_t 5265 vdev_clear_resilver_deferred(vdev_t *vd, dmu_tx_t *tx) 5266 { 5267 boolean_t resilver_needed = B_FALSE; 5268 spa_t *spa = vd->vdev_spa; 5269 5270 for (int c = 0; c < vd->vdev_children; c++) { 5271 vdev_t *cvd = vd->vdev_child[c]; 5272 resilver_needed |= vdev_clear_resilver_deferred(cvd, tx); 5273 } 5274 5275 if (vd == spa->spa_root_vdev && 5276 spa_feature_is_active(spa, SPA_FEATURE_RESILVER_DEFER)) { 5277 spa_feature_decr(spa, SPA_FEATURE_RESILVER_DEFER, tx); 5278 vdev_config_dirty(vd); 5279 spa->spa_resilver_deferred = B_FALSE; 5280 return (resilver_needed); 5281 } 5282 5283 if (!vdev_is_concrete(vd) || vd->vdev_aux || 5284 !vd->vdev_ops->vdev_op_leaf) 5285 return (resilver_needed); 5286 5287 vd->vdev_resilver_deferred = B_FALSE; 5288 5289 return (!vdev_is_dead(vd) && !vd->vdev_offline && 5290 vdev_resilver_needed(vd, NULL, NULL)); 5291 } 5292 5293 boolean_t 5294 vdev_xlate_is_empty(range_seg64_t *rs) 5295 { 5296 return (rs->rs_start == rs->rs_end); 5297 } 5298 5299 /* 5300 * Translate a logical range to the first contiguous physical range for the 5301 * specified vdev_t. This function is initially called with a leaf vdev and 5302 * will walk each parent vdev until it reaches a top-level vdev. Once the 5303 * top-level is reached the physical range is initialized and the recursive 5304 * function begins to unwind. As it unwinds it calls the parent's vdev 5305 * specific translation function to do the real conversion. 5306 */ 5307 void 5308 vdev_xlate(vdev_t *vd, const range_seg64_t *logical_rs, 5309 range_seg64_t *physical_rs, range_seg64_t *remain_rs) 5310 { 5311 /* 5312 * Walk up the vdev tree 5313 */ 5314 if (vd != vd->vdev_top) { 5315 vdev_xlate(vd->vdev_parent, logical_rs, physical_rs, 5316 remain_rs); 5317 } else { 5318 /* 5319 * We've reached the top-level vdev, initialize the physical 5320 * range to the logical range and set an empty remaining 5321 * range then start to unwind. 5322 */ 5323 physical_rs->rs_start = logical_rs->rs_start; 5324 physical_rs->rs_end = logical_rs->rs_end; 5325 5326 remain_rs->rs_start = logical_rs->rs_start; 5327 remain_rs->rs_end = logical_rs->rs_start; 5328 5329 return; 5330 } 5331 5332 vdev_t *pvd = vd->vdev_parent; 5333 ASSERT3P(pvd, !=, NULL); 5334 ASSERT3P(pvd->vdev_ops->vdev_op_xlate, !=, NULL); 5335 5336 /* 5337 * As this recursive function unwinds, translate the logical 5338 * range into its physical and any remaining components by calling 5339 * the vdev specific translate function. 5340 */ 5341 range_seg64_t intermediate = { 0 }; 5342 pvd->vdev_ops->vdev_op_xlate(vd, physical_rs, &intermediate, remain_rs); 5343 5344 physical_rs->rs_start = intermediate.rs_start; 5345 physical_rs->rs_end = intermediate.rs_end; 5346 } 5347 5348 void 5349 vdev_xlate_walk(vdev_t *vd, const range_seg64_t *logical_rs, 5350 vdev_xlate_func_t *func, void *arg) 5351 { 5352 range_seg64_t iter_rs = *logical_rs; 5353 range_seg64_t physical_rs; 5354 range_seg64_t remain_rs; 5355 5356 while (!vdev_xlate_is_empty(&iter_rs)) { 5357 5358 vdev_xlate(vd, &iter_rs, &physical_rs, &remain_rs); 5359 5360 /* 5361 * With raidz and dRAID, it's possible that the logical range 5362 * does not live on this leaf vdev. Only when there is a non- 5363 * zero physical size call the provided function. 5364 */ 5365 if (!vdev_xlate_is_empty(&physical_rs)) 5366 func(arg, &physical_rs); 5367 5368 iter_rs = remain_rs; 5369 } 5370 } 5371 5372 /* 5373 * Look at the vdev tree and determine whether any devices are currently being 5374 * replaced. 5375 */ 5376 boolean_t 5377 vdev_replace_in_progress(vdev_t *vdev) 5378 { 5379 ASSERT(spa_config_held(vdev->vdev_spa, SCL_ALL, RW_READER) != 0); 5380 5381 if (vdev->vdev_ops == &vdev_replacing_ops) 5382 return (B_TRUE); 5383 5384 /* 5385 * A 'spare' vdev indicates that we have a replace in progress, unless 5386 * it has exactly two children, and the second, the hot spare, has 5387 * finished being resilvered. 5388 */ 5389 if (vdev->vdev_ops == &vdev_spare_ops && (vdev->vdev_children > 2 || 5390 !vdev_dtl_empty(vdev->vdev_child[1], DTL_MISSING))) 5391 return (B_TRUE); 5392 5393 for (int i = 0; i < vdev->vdev_children; i++) { 5394 if (vdev_replace_in_progress(vdev->vdev_child[i])) 5395 return (B_TRUE); 5396 } 5397 5398 return (B_FALSE); 5399 } 5400 5401 EXPORT_SYMBOL(vdev_fault); 5402 EXPORT_SYMBOL(vdev_degrade); 5403 EXPORT_SYMBOL(vdev_online); 5404 EXPORT_SYMBOL(vdev_offline); 5405 EXPORT_SYMBOL(vdev_clear); 5406 5407 /* BEGIN CSTYLED */ 5408 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, default_ms_count, INT, ZMOD_RW, 5409 "Target number of metaslabs per top-level vdev"); 5410 5411 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, default_ms_shift, INT, ZMOD_RW, 5412 "Default limit for metaslab size"); 5413 5414 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, min_ms_count, INT, ZMOD_RW, 5415 "Minimum number of metaslabs per top-level vdev"); 5416 5417 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, ms_count_limit, INT, ZMOD_RW, 5418 "Practical upper limit of total metaslabs per top-level vdev"); 5419 5420 ZFS_MODULE_PARAM(zfs, zfs_, slow_io_events_per_second, UINT, ZMOD_RW, 5421 "Rate limit slow IO (delay) events to this many per second"); 5422 5423 ZFS_MODULE_PARAM(zfs, zfs_, checksum_events_per_second, UINT, ZMOD_RW, 5424 "Rate limit checksum events to this many checksum errors per second " 5425 "(do not set below zed threshold)."); 5426 5427 ZFS_MODULE_PARAM(zfs, zfs_, scan_ignore_errors, INT, ZMOD_RW, 5428 "Ignore errors during resilver/scrub"); 5429 5430 ZFS_MODULE_PARAM(zfs_vdev, vdev_, validate_skip, INT, ZMOD_RW, 5431 "Bypass vdev_validate()"); 5432 5433 ZFS_MODULE_PARAM(zfs, zfs_, nocacheflush, INT, ZMOD_RW, 5434 "Disable cache flushes"); 5435 5436 ZFS_MODULE_PARAM(zfs, zfs_, embedded_slog_min_ms, INT, ZMOD_RW, 5437 "Minimum number of metaslabs required to dedicate one for log blocks"); 5438 5439 ZFS_MODULE_PARAM_CALL(zfs_vdev, zfs_vdev_, min_auto_ashift, 5440 param_set_min_auto_ashift, param_get_ulong, ZMOD_RW, 5441 "Minimum ashift used when creating new top-level vdevs"); 5442 5443 ZFS_MODULE_PARAM_CALL(zfs_vdev, zfs_vdev_, max_auto_ashift, 5444 param_set_max_auto_ashift, param_get_ulong, ZMOD_RW, 5445 "Maximum ashift used when optimizing for logical -> physical sector " 5446 "size on new top-level vdevs"); 5447 /* END CSTYLED */ 5448