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 /* 3146 * If the dtl cannot be sync'd there is no need to open it. 3147 */ 3148 if (spa->spa_mode == SPA_MODE_READ && !spa->spa_read_spacemaps) 3149 return (0); 3150 3151 error = space_map_open(&vd->vdev_dtl_sm, mos, 3152 vd->vdev_dtl_object, 0, -1ULL, 0); 3153 if (error) 3154 return (error); 3155 ASSERT(vd->vdev_dtl_sm != NULL); 3156 3157 rt = range_tree_create(NULL, RANGE_SEG64, NULL, 0, 0); 3158 error = space_map_load(vd->vdev_dtl_sm, rt, SM_ALLOC); 3159 if (error == 0) { 3160 mutex_enter(&vd->vdev_dtl_lock); 3161 range_tree_walk(rt, range_tree_add, 3162 vd->vdev_dtl[DTL_MISSING]); 3163 mutex_exit(&vd->vdev_dtl_lock); 3164 } 3165 3166 range_tree_vacate(rt, NULL, NULL); 3167 range_tree_destroy(rt); 3168 3169 return (error); 3170 } 3171 3172 for (int c = 0; c < vd->vdev_children; c++) { 3173 error = vdev_dtl_load(vd->vdev_child[c]); 3174 if (error != 0) 3175 break; 3176 } 3177 3178 return (error); 3179 } 3180 3181 static void 3182 vdev_zap_allocation_data(vdev_t *vd, dmu_tx_t *tx) 3183 { 3184 spa_t *spa = vd->vdev_spa; 3185 objset_t *mos = spa->spa_meta_objset; 3186 vdev_alloc_bias_t alloc_bias = vd->vdev_alloc_bias; 3187 const char *string; 3188 3189 ASSERT(alloc_bias != VDEV_BIAS_NONE); 3190 3191 string = 3192 (alloc_bias == VDEV_BIAS_LOG) ? VDEV_ALLOC_BIAS_LOG : 3193 (alloc_bias == VDEV_BIAS_SPECIAL) ? VDEV_ALLOC_BIAS_SPECIAL : 3194 (alloc_bias == VDEV_BIAS_DEDUP) ? VDEV_ALLOC_BIAS_DEDUP : NULL; 3195 3196 ASSERT(string != NULL); 3197 VERIFY0(zap_add(mos, vd->vdev_top_zap, VDEV_TOP_ZAP_ALLOCATION_BIAS, 3198 1, strlen(string) + 1, string, tx)); 3199 3200 if (alloc_bias == VDEV_BIAS_SPECIAL || alloc_bias == VDEV_BIAS_DEDUP) { 3201 spa_activate_allocation_classes(spa, tx); 3202 } 3203 } 3204 3205 void 3206 vdev_destroy_unlink_zap(vdev_t *vd, uint64_t zapobj, dmu_tx_t *tx) 3207 { 3208 spa_t *spa = vd->vdev_spa; 3209 3210 VERIFY0(zap_destroy(spa->spa_meta_objset, zapobj, tx)); 3211 VERIFY0(zap_remove_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps, 3212 zapobj, tx)); 3213 } 3214 3215 uint64_t 3216 vdev_create_link_zap(vdev_t *vd, dmu_tx_t *tx) 3217 { 3218 spa_t *spa = vd->vdev_spa; 3219 uint64_t zap = zap_create(spa->spa_meta_objset, DMU_OTN_ZAP_METADATA, 3220 DMU_OT_NONE, 0, tx); 3221 3222 ASSERT(zap != 0); 3223 VERIFY0(zap_add_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps, 3224 zap, tx)); 3225 3226 return (zap); 3227 } 3228 3229 void 3230 vdev_construct_zaps(vdev_t *vd, dmu_tx_t *tx) 3231 { 3232 if (vd->vdev_ops != &vdev_hole_ops && 3233 vd->vdev_ops != &vdev_missing_ops && 3234 vd->vdev_ops != &vdev_root_ops && 3235 !vd->vdev_top->vdev_removing) { 3236 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_leaf_zap == 0) { 3237 vd->vdev_leaf_zap = vdev_create_link_zap(vd, tx); 3238 } 3239 if (vd == vd->vdev_top && vd->vdev_top_zap == 0) { 3240 vd->vdev_top_zap = vdev_create_link_zap(vd, tx); 3241 if (vd->vdev_alloc_bias != VDEV_BIAS_NONE) 3242 vdev_zap_allocation_data(vd, tx); 3243 } 3244 } 3245 3246 for (uint64_t i = 0; i < vd->vdev_children; i++) { 3247 vdev_construct_zaps(vd->vdev_child[i], tx); 3248 } 3249 } 3250 3251 static void 3252 vdev_dtl_sync(vdev_t *vd, uint64_t txg) 3253 { 3254 spa_t *spa = vd->vdev_spa; 3255 range_tree_t *rt = vd->vdev_dtl[DTL_MISSING]; 3256 objset_t *mos = spa->spa_meta_objset; 3257 range_tree_t *rtsync; 3258 dmu_tx_t *tx; 3259 uint64_t object = space_map_object(vd->vdev_dtl_sm); 3260 3261 ASSERT(vdev_is_concrete(vd)); 3262 ASSERT(vd->vdev_ops->vdev_op_leaf); 3263 3264 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 3265 3266 if (vd->vdev_detached || vd->vdev_top->vdev_removing) { 3267 mutex_enter(&vd->vdev_dtl_lock); 3268 space_map_free(vd->vdev_dtl_sm, tx); 3269 space_map_close(vd->vdev_dtl_sm); 3270 vd->vdev_dtl_sm = NULL; 3271 mutex_exit(&vd->vdev_dtl_lock); 3272 3273 /* 3274 * We only destroy the leaf ZAP for detached leaves or for 3275 * removed log devices. Removed data devices handle leaf ZAP 3276 * cleanup later, once cancellation is no longer possible. 3277 */ 3278 if (vd->vdev_leaf_zap != 0 && (vd->vdev_detached || 3279 vd->vdev_top->vdev_islog)) { 3280 vdev_destroy_unlink_zap(vd, vd->vdev_leaf_zap, tx); 3281 vd->vdev_leaf_zap = 0; 3282 } 3283 3284 dmu_tx_commit(tx); 3285 return; 3286 } 3287 3288 if (vd->vdev_dtl_sm == NULL) { 3289 uint64_t new_object; 3290 3291 new_object = space_map_alloc(mos, zfs_vdev_dtl_sm_blksz, tx); 3292 VERIFY3U(new_object, !=, 0); 3293 3294 VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object, 3295 0, -1ULL, 0)); 3296 ASSERT(vd->vdev_dtl_sm != NULL); 3297 } 3298 3299 rtsync = range_tree_create(NULL, RANGE_SEG64, NULL, 0, 0); 3300 3301 mutex_enter(&vd->vdev_dtl_lock); 3302 range_tree_walk(rt, range_tree_add, rtsync); 3303 mutex_exit(&vd->vdev_dtl_lock); 3304 3305 space_map_truncate(vd->vdev_dtl_sm, zfs_vdev_dtl_sm_blksz, tx); 3306 space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, SM_NO_VDEVID, tx); 3307 range_tree_vacate(rtsync, NULL, NULL); 3308 3309 range_tree_destroy(rtsync); 3310 3311 /* 3312 * If the object for the space map has changed then dirty 3313 * the top level so that we update the config. 3314 */ 3315 if (object != space_map_object(vd->vdev_dtl_sm)) { 3316 vdev_dbgmsg(vd, "txg %llu, spa %s, DTL old object %llu, " 3317 "new object %llu", (u_longlong_t)txg, spa_name(spa), 3318 (u_longlong_t)object, 3319 (u_longlong_t)space_map_object(vd->vdev_dtl_sm)); 3320 vdev_config_dirty(vd->vdev_top); 3321 } 3322 3323 dmu_tx_commit(tx); 3324 } 3325 3326 /* 3327 * Determine whether the specified vdev can be offlined/detached/removed 3328 * without losing data. 3329 */ 3330 boolean_t 3331 vdev_dtl_required(vdev_t *vd) 3332 { 3333 spa_t *spa = vd->vdev_spa; 3334 vdev_t *tvd = vd->vdev_top; 3335 uint8_t cant_read = vd->vdev_cant_read; 3336 boolean_t required; 3337 3338 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 3339 3340 if (vd == spa->spa_root_vdev || vd == tvd) 3341 return (B_TRUE); 3342 3343 /* 3344 * Temporarily mark the device as unreadable, and then determine 3345 * whether this results in any DTL outages in the top-level vdev. 3346 * If not, we can safely offline/detach/remove the device. 3347 */ 3348 vd->vdev_cant_read = B_TRUE; 3349 vdev_dtl_reassess(tvd, 0, 0, B_FALSE, B_FALSE); 3350 required = !vdev_dtl_empty(tvd, DTL_OUTAGE); 3351 vd->vdev_cant_read = cant_read; 3352 vdev_dtl_reassess(tvd, 0, 0, B_FALSE, B_FALSE); 3353 3354 if (!required && zio_injection_enabled) { 3355 required = !!zio_handle_device_injection(vd, NULL, 3356 SET_ERROR(ECHILD)); 3357 } 3358 3359 return (required); 3360 } 3361 3362 /* 3363 * Determine if resilver is needed, and if so the txg range. 3364 */ 3365 boolean_t 3366 vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp) 3367 { 3368 boolean_t needed = B_FALSE; 3369 uint64_t thismin = UINT64_MAX; 3370 uint64_t thismax = 0; 3371 3372 if (vd->vdev_children == 0) { 3373 mutex_enter(&vd->vdev_dtl_lock); 3374 if (!range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) && 3375 vdev_writeable(vd)) { 3376 3377 thismin = vdev_dtl_min(vd); 3378 thismax = vdev_dtl_max(vd); 3379 needed = B_TRUE; 3380 } 3381 mutex_exit(&vd->vdev_dtl_lock); 3382 } else { 3383 for (int c = 0; c < vd->vdev_children; c++) { 3384 vdev_t *cvd = vd->vdev_child[c]; 3385 uint64_t cmin, cmax; 3386 3387 if (vdev_resilver_needed(cvd, &cmin, &cmax)) { 3388 thismin = MIN(thismin, cmin); 3389 thismax = MAX(thismax, cmax); 3390 needed = B_TRUE; 3391 } 3392 } 3393 } 3394 3395 if (needed && minp) { 3396 *minp = thismin; 3397 *maxp = thismax; 3398 } 3399 return (needed); 3400 } 3401 3402 /* 3403 * Gets the checkpoint space map object from the vdev's ZAP. On success sm_obj 3404 * will contain either the checkpoint spacemap object or zero if none exists. 3405 * All other errors are returned to the caller. 3406 */ 3407 int 3408 vdev_checkpoint_sm_object(vdev_t *vd, uint64_t *sm_obj) 3409 { 3410 ASSERT0(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER)); 3411 3412 if (vd->vdev_top_zap == 0) { 3413 *sm_obj = 0; 3414 return (0); 3415 } 3416 3417 int error = zap_lookup(spa_meta_objset(vd->vdev_spa), vd->vdev_top_zap, 3418 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, sizeof (uint64_t), 1, sm_obj); 3419 if (error == ENOENT) { 3420 *sm_obj = 0; 3421 error = 0; 3422 } 3423 3424 return (error); 3425 } 3426 3427 int 3428 vdev_load(vdev_t *vd) 3429 { 3430 int children = vd->vdev_children; 3431 int error = 0; 3432 taskq_t *tq = NULL; 3433 3434 /* 3435 * It's only worthwhile to use the taskq for the root vdev, because the 3436 * slow part is metaslab_init, and that only happens for top-level 3437 * vdevs. 3438 */ 3439 if (vd->vdev_ops == &vdev_root_ops && vd->vdev_children > 0) { 3440 tq = taskq_create("vdev_load", children, minclsyspri, 3441 children, children, TASKQ_PREPOPULATE); 3442 } 3443 3444 /* 3445 * Recursively load all children. 3446 */ 3447 for (int c = 0; c < vd->vdev_children; c++) { 3448 vdev_t *cvd = vd->vdev_child[c]; 3449 3450 if (tq == NULL || vdev_uses_zvols(cvd)) { 3451 cvd->vdev_load_error = vdev_load(cvd); 3452 } else { 3453 VERIFY(taskq_dispatch(tq, vdev_load_child, 3454 cvd, TQ_SLEEP) != TASKQID_INVALID); 3455 } 3456 } 3457 3458 if (tq != NULL) { 3459 taskq_wait(tq); 3460 taskq_destroy(tq); 3461 } 3462 3463 for (int c = 0; c < vd->vdev_children; c++) { 3464 int error = vd->vdev_child[c]->vdev_load_error; 3465 3466 if (error != 0) 3467 return (error); 3468 } 3469 3470 vdev_set_deflate_ratio(vd); 3471 3472 /* 3473 * On spa_load path, grab the allocation bias from our zap 3474 */ 3475 if (vd == vd->vdev_top && vd->vdev_top_zap != 0) { 3476 spa_t *spa = vd->vdev_spa; 3477 char bias_str[64]; 3478 3479 error = zap_lookup(spa->spa_meta_objset, vd->vdev_top_zap, 3480 VDEV_TOP_ZAP_ALLOCATION_BIAS, 1, sizeof (bias_str), 3481 bias_str); 3482 if (error == 0) { 3483 ASSERT(vd->vdev_alloc_bias == VDEV_BIAS_NONE); 3484 vd->vdev_alloc_bias = vdev_derive_alloc_bias(bias_str); 3485 } else if (error != ENOENT) { 3486 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 3487 VDEV_AUX_CORRUPT_DATA); 3488 vdev_dbgmsg(vd, "vdev_load: zap_lookup(top_zap=%llu) " 3489 "failed [error=%d]", 3490 (u_longlong_t)vd->vdev_top_zap, error); 3491 return (error); 3492 } 3493 } 3494 3495 /* 3496 * Load any rebuild state from the top-level vdev zap. 3497 */ 3498 if (vd == vd->vdev_top && vd->vdev_top_zap != 0) { 3499 error = vdev_rebuild_load(vd); 3500 if (error && error != ENOTSUP) { 3501 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 3502 VDEV_AUX_CORRUPT_DATA); 3503 vdev_dbgmsg(vd, "vdev_load: vdev_rebuild_load " 3504 "failed [error=%d]", error); 3505 return (error); 3506 } 3507 } 3508 3509 /* 3510 * If this is a top-level vdev, initialize its metaslabs. 3511 */ 3512 if (vd == vd->vdev_top && vdev_is_concrete(vd)) { 3513 vdev_metaslab_group_create(vd); 3514 3515 if (vd->vdev_ashift == 0 || vd->vdev_asize == 0) { 3516 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 3517 VDEV_AUX_CORRUPT_DATA); 3518 vdev_dbgmsg(vd, "vdev_load: invalid size. ashift=%llu, " 3519 "asize=%llu", (u_longlong_t)vd->vdev_ashift, 3520 (u_longlong_t)vd->vdev_asize); 3521 return (SET_ERROR(ENXIO)); 3522 } 3523 3524 error = vdev_metaslab_init(vd, 0); 3525 if (error != 0) { 3526 vdev_dbgmsg(vd, "vdev_load: metaslab_init failed " 3527 "[error=%d]", error); 3528 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 3529 VDEV_AUX_CORRUPT_DATA); 3530 return (error); 3531 } 3532 3533 uint64_t checkpoint_sm_obj; 3534 error = vdev_checkpoint_sm_object(vd, &checkpoint_sm_obj); 3535 if (error == 0 && checkpoint_sm_obj != 0) { 3536 objset_t *mos = spa_meta_objset(vd->vdev_spa); 3537 ASSERT(vd->vdev_asize != 0); 3538 ASSERT3P(vd->vdev_checkpoint_sm, ==, NULL); 3539 3540 error = space_map_open(&vd->vdev_checkpoint_sm, 3541 mos, checkpoint_sm_obj, 0, vd->vdev_asize, 3542 vd->vdev_ashift); 3543 if (error != 0) { 3544 vdev_dbgmsg(vd, "vdev_load: space_map_open " 3545 "failed for checkpoint spacemap (obj %llu) " 3546 "[error=%d]", 3547 (u_longlong_t)checkpoint_sm_obj, error); 3548 return (error); 3549 } 3550 ASSERT3P(vd->vdev_checkpoint_sm, !=, NULL); 3551 3552 /* 3553 * Since the checkpoint_sm contains free entries 3554 * exclusively we can use space_map_allocated() to 3555 * indicate the cumulative checkpointed space that 3556 * has been freed. 3557 */ 3558 vd->vdev_stat.vs_checkpoint_space = 3559 -space_map_allocated(vd->vdev_checkpoint_sm); 3560 vd->vdev_spa->spa_checkpoint_info.sci_dspace += 3561 vd->vdev_stat.vs_checkpoint_space; 3562 } else if (error != 0) { 3563 vdev_dbgmsg(vd, "vdev_load: failed to retrieve " 3564 "checkpoint space map object from vdev ZAP " 3565 "[error=%d]", error); 3566 return (error); 3567 } 3568 } 3569 3570 /* 3571 * If this is a leaf vdev, load its DTL. 3572 */ 3573 if (vd->vdev_ops->vdev_op_leaf && (error = vdev_dtl_load(vd)) != 0) { 3574 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 3575 VDEV_AUX_CORRUPT_DATA); 3576 vdev_dbgmsg(vd, "vdev_load: vdev_dtl_load failed " 3577 "[error=%d]", error); 3578 return (error); 3579 } 3580 3581 uint64_t obsolete_sm_object; 3582 error = vdev_obsolete_sm_object(vd, &obsolete_sm_object); 3583 if (error == 0 && obsolete_sm_object != 0) { 3584 objset_t *mos = vd->vdev_spa->spa_meta_objset; 3585 ASSERT(vd->vdev_asize != 0); 3586 ASSERT3P(vd->vdev_obsolete_sm, ==, NULL); 3587 3588 if ((error = space_map_open(&vd->vdev_obsolete_sm, mos, 3589 obsolete_sm_object, 0, vd->vdev_asize, 0))) { 3590 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 3591 VDEV_AUX_CORRUPT_DATA); 3592 vdev_dbgmsg(vd, "vdev_load: space_map_open failed for " 3593 "obsolete spacemap (obj %llu) [error=%d]", 3594 (u_longlong_t)obsolete_sm_object, error); 3595 return (error); 3596 } 3597 } else if (error != 0) { 3598 vdev_dbgmsg(vd, "vdev_load: failed to retrieve obsolete " 3599 "space map object from vdev ZAP [error=%d]", error); 3600 return (error); 3601 } 3602 3603 return (0); 3604 } 3605 3606 /* 3607 * The special vdev case is used for hot spares and l2cache devices. Its 3608 * sole purpose it to set the vdev state for the associated vdev. To do this, 3609 * we make sure that we can open the underlying device, then try to read the 3610 * label, and make sure that the label is sane and that it hasn't been 3611 * repurposed to another pool. 3612 */ 3613 int 3614 vdev_validate_aux(vdev_t *vd) 3615 { 3616 nvlist_t *label; 3617 uint64_t guid, version; 3618 uint64_t state; 3619 3620 if (!vdev_readable(vd)) 3621 return (0); 3622 3623 if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) { 3624 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 3625 VDEV_AUX_CORRUPT_DATA); 3626 return (-1); 3627 } 3628 3629 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 || 3630 !SPA_VERSION_IS_SUPPORTED(version) || 3631 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 || 3632 guid != vd->vdev_guid || 3633 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) { 3634 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 3635 VDEV_AUX_CORRUPT_DATA); 3636 nvlist_free(label); 3637 return (-1); 3638 } 3639 3640 /* 3641 * We don't actually check the pool state here. If it's in fact in 3642 * use by another pool, we update this fact on the fly when requested. 3643 */ 3644 nvlist_free(label); 3645 return (0); 3646 } 3647 3648 static void 3649 vdev_destroy_ms_flush_data(vdev_t *vd, dmu_tx_t *tx) 3650 { 3651 objset_t *mos = spa_meta_objset(vd->vdev_spa); 3652 3653 if (vd->vdev_top_zap == 0) 3654 return; 3655 3656 uint64_t object = 0; 3657 int err = zap_lookup(mos, vd->vdev_top_zap, 3658 VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS, sizeof (uint64_t), 1, &object); 3659 if (err == ENOENT) 3660 return; 3661 VERIFY0(err); 3662 3663 VERIFY0(dmu_object_free(mos, object, tx)); 3664 VERIFY0(zap_remove(mos, vd->vdev_top_zap, 3665 VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS, tx)); 3666 } 3667 3668 /* 3669 * Free the objects used to store this vdev's spacemaps, and the array 3670 * that points to them. 3671 */ 3672 void 3673 vdev_destroy_spacemaps(vdev_t *vd, dmu_tx_t *tx) 3674 { 3675 if (vd->vdev_ms_array == 0) 3676 return; 3677 3678 objset_t *mos = vd->vdev_spa->spa_meta_objset; 3679 uint64_t array_count = vd->vdev_asize >> vd->vdev_ms_shift; 3680 size_t array_bytes = array_count * sizeof (uint64_t); 3681 uint64_t *smobj_array = kmem_alloc(array_bytes, KM_SLEEP); 3682 VERIFY0(dmu_read(mos, vd->vdev_ms_array, 0, 3683 array_bytes, smobj_array, 0)); 3684 3685 for (uint64_t i = 0; i < array_count; i++) { 3686 uint64_t smobj = smobj_array[i]; 3687 if (smobj == 0) 3688 continue; 3689 3690 space_map_free_obj(mos, smobj, tx); 3691 } 3692 3693 kmem_free(smobj_array, array_bytes); 3694 VERIFY0(dmu_object_free(mos, vd->vdev_ms_array, tx)); 3695 vdev_destroy_ms_flush_data(vd, tx); 3696 vd->vdev_ms_array = 0; 3697 } 3698 3699 static void 3700 vdev_remove_empty_log(vdev_t *vd, uint64_t txg) 3701 { 3702 spa_t *spa = vd->vdev_spa; 3703 3704 ASSERT(vd->vdev_islog); 3705 ASSERT(vd == vd->vdev_top); 3706 ASSERT3U(txg, ==, spa_syncing_txg(spa)); 3707 3708 dmu_tx_t *tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg); 3709 3710 vdev_destroy_spacemaps(vd, tx); 3711 if (vd->vdev_top_zap != 0) { 3712 vdev_destroy_unlink_zap(vd, vd->vdev_top_zap, tx); 3713 vd->vdev_top_zap = 0; 3714 } 3715 3716 dmu_tx_commit(tx); 3717 } 3718 3719 void 3720 vdev_sync_done(vdev_t *vd, uint64_t txg) 3721 { 3722 metaslab_t *msp; 3723 boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg)); 3724 3725 ASSERT(vdev_is_concrete(vd)); 3726 3727 while ((msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg))) 3728 != NULL) 3729 metaslab_sync_done(msp, txg); 3730 3731 if (reassess) { 3732 metaslab_sync_reassess(vd->vdev_mg); 3733 if (vd->vdev_log_mg != NULL) 3734 metaslab_sync_reassess(vd->vdev_log_mg); 3735 } 3736 } 3737 3738 void 3739 vdev_sync(vdev_t *vd, uint64_t txg) 3740 { 3741 spa_t *spa = vd->vdev_spa; 3742 vdev_t *lvd; 3743 metaslab_t *msp; 3744 3745 ASSERT3U(txg, ==, spa->spa_syncing_txg); 3746 dmu_tx_t *tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 3747 if (range_tree_space(vd->vdev_obsolete_segments) > 0) { 3748 ASSERT(vd->vdev_removing || 3749 vd->vdev_ops == &vdev_indirect_ops); 3750 3751 vdev_indirect_sync_obsolete(vd, tx); 3752 3753 /* 3754 * If the vdev is indirect, it can't have dirty 3755 * metaslabs or DTLs. 3756 */ 3757 if (vd->vdev_ops == &vdev_indirect_ops) { 3758 ASSERT(txg_list_empty(&vd->vdev_ms_list, txg)); 3759 ASSERT(txg_list_empty(&vd->vdev_dtl_list, txg)); 3760 dmu_tx_commit(tx); 3761 return; 3762 } 3763 } 3764 3765 ASSERT(vdev_is_concrete(vd)); 3766 3767 if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0 && 3768 !vd->vdev_removing) { 3769 ASSERT(vd == vd->vdev_top); 3770 ASSERT0(vd->vdev_indirect_config.vic_mapping_object); 3771 vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset, 3772 DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx); 3773 ASSERT(vd->vdev_ms_array != 0); 3774 vdev_config_dirty(vd); 3775 } 3776 3777 while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) { 3778 metaslab_sync(msp, txg); 3779 (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg)); 3780 } 3781 3782 while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL) 3783 vdev_dtl_sync(lvd, txg); 3784 3785 /* 3786 * If this is an empty log device being removed, destroy the 3787 * metadata associated with it. 3788 */ 3789 if (vd->vdev_islog && vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing) 3790 vdev_remove_empty_log(vd, txg); 3791 3792 (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)); 3793 dmu_tx_commit(tx); 3794 } 3795 3796 uint64_t 3797 vdev_psize_to_asize(vdev_t *vd, uint64_t psize) 3798 { 3799 return (vd->vdev_ops->vdev_op_asize(vd, psize)); 3800 } 3801 3802 /* 3803 * Mark the given vdev faulted. A faulted vdev behaves as if the device could 3804 * not be opened, and no I/O is attempted. 3805 */ 3806 int 3807 vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux) 3808 { 3809 vdev_t *vd, *tvd; 3810 3811 spa_vdev_state_enter(spa, SCL_NONE); 3812 3813 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3814 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV))); 3815 3816 if (!vd->vdev_ops->vdev_op_leaf) 3817 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENOTSUP))); 3818 3819 tvd = vd->vdev_top; 3820 3821 /* 3822 * If user did a 'zpool offline -f' then make the fault persist across 3823 * reboots. 3824 */ 3825 if (aux == VDEV_AUX_EXTERNAL_PERSIST) { 3826 /* 3827 * There are two kinds of forced faults: temporary and 3828 * persistent. Temporary faults go away at pool import, while 3829 * persistent faults stay set. Both types of faults can be 3830 * cleared with a zpool clear. 3831 * 3832 * We tell if a vdev is persistently faulted by looking at the 3833 * ZPOOL_CONFIG_AUX_STATE nvpair. If it's set to "external" at 3834 * import then it's a persistent fault. Otherwise, it's 3835 * temporary. We get ZPOOL_CONFIG_AUX_STATE set to "external" 3836 * by setting vd.vdev_stat.vs_aux to VDEV_AUX_EXTERNAL. This 3837 * tells vdev_config_generate() (which gets run later) to set 3838 * ZPOOL_CONFIG_AUX_STATE to "external" in the nvlist. 3839 */ 3840 vd->vdev_stat.vs_aux = VDEV_AUX_EXTERNAL; 3841 vd->vdev_tmpoffline = B_FALSE; 3842 aux = VDEV_AUX_EXTERNAL; 3843 } else { 3844 vd->vdev_tmpoffline = B_TRUE; 3845 } 3846 3847 /* 3848 * We don't directly use the aux state here, but if we do a 3849 * vdev_reopen(), we need this value to be present to remember why we 3850 * were faulted. 3851 */ 3852 vd->vdev_label_aux = aux; 3853 3854 /* 3855 * Faulted state takes precedence over degraded. 3856 */ 3857 vd->vdev_delayed_close = B_FALSE; 3858 vd->vdev_faulted = 1ULL; 3859 vd->vdev_degraded = 0ULL; 3860 vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux); 3861 3862 /* 3863 * If this device has the only valid copy of the data, then 3864 * back off and simply mark the vdev as degraded instead. 3865 */ 3866 if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) { 3867 vd->vdev_degraded = 1ULL; 3868 vd->vdev_faulted = 0ULL; 3869 3870 /* 3871 * If we reopen the device and it's not dead, only then do we 3872 * mark it degraded. 3873 */ 3874 vdev_reopen(tvd); 3875 3876 if (vdev_readable(vd)) 3877 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux); 3878 } 3879 3880 return (spa_vdev_state_exit(spa, vd, 0)); 3881 } 3882 3883 /* 3884 * Mark the given vdev degraded. A degraded vdev is purely an indication to the 3885 * user that something is wrong. The vdev continues to operate as normal as far 3886 * as I/O is concerned. 3887 */ 3888 int 3889 vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux) 3890 { 3891 vdev_t *vd; 3892 3893 spa_vdev_state_enter(spa, SCL_NONE); 3894 3895 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3896 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV))); 3897 3898 if (!vd->vdev_ops->vdev_op_leaf) 3899 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENOTSUP))); 3900 3901 /* 3902 * If the vdev is already faulted, then don't do anything. 3903 */ 3904 if (vd->vdev_faulted || vd->vdev_degraded) 3905 return (spa_vdev_state_exit(spa, NULL, 0)); 3906 3907 vd->vdev_degraded = 1ULL; 3908 if (!vdev_is_dead(vd)) 3909 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, 3910 aux); 3911 3912 return (spa_vdev_state_exit(spa, vd, 0)); 3913 } 3914 3915 /* 3916 * Online the given vdev. 3917 * 3918 * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached 3919 * spare device should be detached when the device finishes resilvering. 3920 * Second, the online should be treated like a 'test' online case, so no FMA 3921 * events are generated if the device fails to open. 3922 */ 3923 int 3924 vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate) 3925 { 3926 vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev; 3927 boolean_t wasoffline; 3928 vdev_state_t oldstate; 3929 3930 spa_vdev_state_enter(spa, SCL_NONE); 3931 3932 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3933 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV))); 3934 3935 if (!vd->vdev_ops->vdev_op_leaf) 3936 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENOTSUP))); 3937 3938 wasoffline = (vd->vdev_offline || vd->vdev_tmpoffline); 3939 oldstate = vd->vdev_state; 3940 3941 tvd = vd->vdev_top; 3942 vd->vdev_offline = B_FALSE; 3943 vd->vdev_tmpoffline = B_FALSE; 3944 vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE); 3945 vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT); 3946 3947 /* XXX - L2ARC 1.0 does not support expansion */ 3948 if (!vd->vdev_aux) { 3949 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 3950 pvd->vdev_expanding = !!((flags & ZFS_ONLINE_EXPAND) || 3951 spa->spa_autoexpand); 3952 vd->vdev_expansion_time = gethrestime_sec(); 3953 } 3954 3955 vdev_reopen(tvd); 3956 vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE; 3957 3958 if (!vd->vdev_aux) { 3959 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 3960 pvd->vdev_expanding = B_FALSE; 3961 } 3962 3963 if (newstate) 3964 *newstate = vd->vdev_state; 3965 if ((flags & ZFS_ONLINE_UNSPARE) && 3966 !vdev_is_dead(vd) && vd->vdev_parent && 3967 vd->vdev_parent->vdev_ops == &vdev_spare_ops && 3968 vd->vdev_parent->vdev_child[0] == vd) 3969 vd->vdev_unspare = B_TRUE; 3970 3971 if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) { 3972 3973 /* XXX - L2ARC 1.0 does not support expansion */ 3974 if (vd->vdev_aux) 3975 return (spa_vdev_state_exit(spa, vd, ENOTSUP)); 3976 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 3977 } 3978 3979 /* Restart initializing if necessary */ 3980 mutex_enter(&vd->vdev_initialize_lock); 3981 if (vdev_writeable(vd) && 3982 vd->vdev_initialize_thread == NULL && 3983 vd->vdev_initialize_state == VDEV_INITIALIZE_ACTIVE) { 3984 (void) vdev_initialize(vd); 3985 } 3986 mutex_exit(&vd->vdev_initialize_lock); 3987 3988 /* 3989 * Restart trimming if necessary. We do not restart trimming for cache 3990 * devices here. This is triggered by l2arc_rebuild_vdev() 3991 * asynchronously for the whole device or in l2arc_evict() as it evicts 3992 * space for upcoming writes. 3993 */ 3994 mutex_enter(&vd->vdev_trim_lock); 3995 if (vdev_writeable(vd) && !vd->vdev_isl2cache && 3996 vd->vdev_trim_thread == NULL && 3997 vd->vdev_trim_state == VDEV_TRIM_ACTIVE) { 3998 (void) vdev_trim(vd, vd->vdev_trim_rate, vd->vdev_trim_partial, 3999 vd->vdev_trim_secure); 4000 } 4001 mutex_exit(&vd->vdev_trim_lock); 4002 4003 if (wasoffline || 4004 (oldstate < VDEV_STATE_DEGRADED && 4005 vd->vdev_state >= VDEV_STATE_DEGRADED)) 4006 spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_ONLINE); 4007 4008 return (spa_vdev_state_exit(spa, vd, 0)); 4009 } 4010 4011 static int 4012 vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags) 4013 { 4014 vdev_t *vd, *tvd; 4015 int error = 0; 4016 uint64_t generation; 4017 metaslab_group_t *mg; 4018 4019 top: 4020 spa_vdev_state_enter(spa, SCL_ALLOC); 4021 4022 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 4023 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV))); 4024 4025 if (!vd->vdev_ops->vdev_op_leaf) 4026 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENOTSUP))); 4027 4028 if (vd->vdev_ops == &vdev_draid_spare_ops) 4029 return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 4030 4031 tvd = vd->vdev_top; 4032 mg = tvd->vdev_mg; 4033 generation = spa->spa_config_generation + 1; 4034 4035 /* 4036 * If the device isn't already offline, try to offline it. 4037 */ 4038 if (!vd->vdev_offline) { 4039 /* 4040 * If this device has the only valid copy of some data, 4041 * don't allow it to be offlined. Log devices are always 4042 * expendable. 4043 */ 4044 if (!tvd->vdev_islog && vd->vdev_aux == NULL && 4045 vdev_dtl_required(vd)) 4046 return (spa_vdev_state_exit(spa, NULL, 4047 SET_ERROR(EBUSY))); 4048 4049 /* 4050 * If the top-level is a slog and it has had allocations 4051 * then proceed. We check that the vdev's metaslab group 4052 * is not NULL since it's possible that we may have just 4053 * added this vdev but not yet initialized its metaslabs. 4054 */ 4055 if (tvd->vdev_islog && mg != NULL) { 4056 /* 4057 * Prevent any future allocations. 4058 */ 4059 ASSERT3P(tvd->vdev_log_mg, ==, NULL); 4060 metaslab_group_passivate(mg); 4061 (void) spa_vdev_state_exit(spa, vd, 0); 4062 4063 error = spa_reset_logs(spa); 4064 4065 /* 4066 * If the log device was successfully reset but has 4067 * checkpointed data, do not offline it. 4068 */ 4069 if (error == 0 && 4070 tvd->vdev_checkpoint_sm != NULL) { 4071 ASSERT3U(space_map_allocated( 4072 tvd->vdev_checkpoint_sm), !=, 0); 4073 error = ZFS_ERR_CHECKPOINT_EXISTS; 4074 } 4075 4076 spa_vdev_state_enter(spa, SCL_ALLOC); 4077 4078 /* 4079 * Check to see if the config has changed. 4080 */ 4081 if (error || generation != spa->spa_config_generation) { 4082 metaslab_group_activate(mg); 4083 if (error) 4084 return (spa_vdev_state_exit(spa, 4085 vd, error)); 4086 (void) spa_vdev_state_exit(spa, vd, 0); 4087 goto top; 4088 } 4089 ASSERT0(tvd->vdev_stat.vs_alloc); 4090 } 4091 4092 /* 4093 * Offline this device and reopen its top-level vdev. 4094 * If the top-level vdev is a log device then just offline 4095 * it. Otherwise, if this action results in the top-level 4096 * vdev becoming unusable, undo it and fail the request. 4097 */ 4098 vd->vdev_offline = B_TRUE; 4099 vdev_reopen(tvd); 4100 4101 if (!tvd->vdev_islog && vd->vdev_aux == NULL && 4102 vdev_is_dead(tvd)) { 4103 vd->vdev_offline = B_FALSE; 4104 vdev_reopen(tvd); 4105 return (spa_vdev_state_exit(spa, NULL, 4106 SET_ERROR(EBUSY))); 4107 } 4108 4109 /* 4110 * Add the device back into the metaslab rotor so that 4111 * once we online the device it's open for business. 4112 */ 4113 if (tvd->vdev_islog && mg != NULL) 4114 metaslab_group_activate(mg); 4115 } 4116 4117 vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY); 4118 4119 return (spa_vdev_state_exit(spa, vd, 0)); 4120 } 4121 4122 int 4123 vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags) 4124 { 4125 int error; 4126 4127 mutex_enter(&spa->spa_vdev_top_lock); 4128 error = vdev_offline_locked(spa, guid, flags); 4129 mutex_exit(&spa->spa_vdev_top_lock); 4130 4131 return (error); 4132 } 4133 4134 /* 4135 * Clear the error counts associated with this vdev. Unlike vdev_online() and 4136 * vdev_offline(), we assume the spa config is locked. We also clear all 4137 * children. If 'vd' is NULL, then the user wants to clear all vdevs. 4138 */ 4139 void 4140 vdev_clear(spa_t *spa, vdev_t *vd) 4141 { 4142 vdev_t *rvd = spa->spa_root_vdev; 4143 4144 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 4145 4146 if (vd == NULL) 4147 vd = rvd; 4148 4149 vd->vdev_stat.vs_read_errors = 0; 4150 vd->vdev_stat.vs_write_errors = 0; 4151 vd->vdev_stat.vs_checksum_errors = 0; 4152 vd->vdev_stat.vs_slow_ios = 0; 4153 4154 for (int c = 0; c < vd->vdev_children; c++) 4155 vdev_clear(spa, vd->vdev_child[c]); 4156 4157 /* 4158 * It makes no sense to "clear" an indirect vdev. 4159 */ 4160 if (!vdev_is_concrete(vd)) 4161 return; 4162 4163 /* 4164 * If we're in the FAULTED state or have experienced failed I/O, then 4165 * clear the persistent state and attempt to reopen the device. We 4166 * also mark the vdev config dirty, so that the new faulted state is 4167 * written out to disk. 4168 */ 4169 if (vd->vdev_faulted || vd->vdev_degraded || 4170 !vdev_readable(vd) || !vdev_writeable(vd)) { 4171 /* 4172 * When reopening in response to a clear event, it may be due to 4173 * a fmadm repair request. In this case, if the device is 4174 * still broken, we want to still post the ereport again. 4175 */ 4176 vd->vdev_forcefault = B_TRUE; 4177 4178 vd->vdev_faulted = vd->vdev_degraded = 0ULL; 4179 vd->vdev_cant_read = B_FALSE; 4180 vd->vdev_cant_write = B_FALSE; 4181 vd->vdev_stat.vs_aux = 0; 4182 4183 vdev_reopen(vd == rvd ? rvd : vd->vdev_top); 4184 4185 vd->vdev_forcefault = B_FALSE; 4186 4187 if (vd != rvd && vdev_writeable(vd->vdev_top)) 4188 vdev_state_dirty(vd->vdev_top); 4189 4190 /* If a resilver isn't required, check if vdevs can be culled */ 4191 if (vd->vdev_aux == NULL && !vdev_is_dead(vd) && 4192 !dsl_scan_resilvering(spa->spa_dsl_pool) && 4193 !dsl_scan_resilver_scheduled(spa->spa_dsl_pool)) 4194 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 4195 4196 spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_CLEAR); 4197 } 4198 4199 /* 4200 * When clearing a FMA-diagnosed fault, we always want to 4201 * unspare the device, as we assume that the original spare was 4202 * done in response to the FMA fault. 4203 */ 4204 if (!vdev_is_dead(vd) && vd->vdev_parent != NULL && 4205 vd->vdev_parent->vdev_ops == &vdev_spare_ops && 4206 vd->vdev_parent->vdev_child[0] == vd) 4207 vd->vdev_unspare = B_TRUE; 4208 4209 /* Clear recent error events cache (i.e. duplicate events tracking) */ 4210 zfs_ereport_clear(spa, vd); 4211 } 4212 4213 boolean_t 4214 vdev_is_dead(vdev_t *vd) 4215 { 4216 /* 4217 * Holes and missing devices are always considered "dead". 4218 * This simplifies the code since we don't have to check for 4219 * these types of devices in the various code paths. 4220 * Instead we rely on the fact that we skip over dead devices 4221 * before issuing I/O to them. 4222 */ 4223 return (vd->vdev_state < VDEV_STATE_DEGRADED || 4224 vd->vdev_ops == &vdev_hole_ops || 4225 vd->vdev_ops == &vdev_missing_ops); 4226 } 4227 4228 boolean_t 4229 vdev_readable(vdev_t *vd) 4230 { 4231 return (!vdev_is_dead(vd) && !vd->vdev_cant_read); 4232 } 4233 4234 boolean_t 4235 vdev_writeable(vdev_t *vd) 4236 { 4237 return (!vdev_is_dead(vd) && !vd->vdev_cant_write && 4238 vdev_is_concrete(vd)); 4239 } 4240 4241 boolean_t 4242 vdev_allocatable(vdev_t *vd) 4243 { 4244 uint64_t state = vd->vdev_state; 4245 4246 /* 4247 * We currently allow allocations from vdevs which may be in the 4248 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device 4249 * fails to reopen then we'll catch it later when we're holding 4250 * the proper locks. Note that we have to get the vdev state 4251 * in a local variable because although it changes atomically, 4252 * we're asking two separate questions about it. 4253 */ 4254 return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) && 4255 !vd->vdev_cant_write && vdev_is_concrete(vd) && 4256 vd->vdev_mg->mg_initialized); 4257 } 4258 4259 boolean_t 4260 vdev_accessible(vdev_t *vd, zio_t *zio) 4261 { 4262 ASSERT(zio->io_vd == vd); 4263 4264 if (vdev_is_dead(vd) || vd->vdev_remove_wanted) 4265 return (B_FALSE); 4266 4267 if (zio->io_type == ZIO_TYPE_READ) 4268 return (!vd->vdev_cant_read); 4269 4270 if (zio->io_type == ZIO_TYPE_WRITE) 4271 return (!vd->vdev_cant_write); 4272 4273 return (B_TRUE); 4274 } 4275 4276 static void 4277 vdev_get_child_stat(vdev_t *cvd, vdev_stat_t *vs, vdev_stat_t *cvs) 4278 { 4279 /* 4280 * Exclude the dRAID spare when aggregating to avoid double counting 4281 * the ops and bytes. These IOs are counted by the physical leaves. 4282 */ 4283 if (cvd->vdev_ops == &vdev_draid_spare_ops) 4284 return; 4285 4286 for (int t = 0; t < VS_ZIO_TYPES; t++) { 4287 vs->vs_ops[t] += cvs->vs_ops[t]; 4288 vs->vs_bytes[t] += cvs->vs_bytes[t]; 4289 } 4290 4291 cvs->vs_scan_removing = cvd->vdev_removing; 4292 } 4293 4294 /* 4295 * Get extended stats 4296 */ 4297 static void 4298 vdev_get_child_stat_ex(vdev_t *cvd, vdev_stat_ex_t *vsx, vdev_stat_ex_t *cvsx) 4299 { 4300 int t, b; 4301 for (t = 0; t < ZIO_TYPES; t++) { 4302 for (b = 0; b < ARRAY_SIZE(vsx->vsx_disk_histo[0]); b++) 4303 vsx->vsx_disk_histo[t][b] += cvsx->vsx_disk_histo[t][b]; 4304 4305 for (b = 0; b < ARRAY_SIZE(vsx->vsx_total_histo[0]); b++) { 4306 vsx->vsx_total_histo[t][b] += 4307 cvsx->vsx_total_histo[t][b]; 4308 } 4309 } 4310 4311 for (t = 0; t < ZIO_PRIORITY_NUM_QUEUEABLE; t++) { 4312 for (b = 0; b < ARRAY_SIZE(vsx->vsx_queue_histo[0]); b++) { 4313 vsx->vsx_queue_histo[t][b] += 4314 cvsx->vsx_queue_histo[t][b]; 4315 } 4316 vsx->vsx_active_queue[t] += cvsx->vsx_active_queue[t]; 4317 vsx->vsx_pend_queue[t] += cvsx->vsx_pend_queue[t]; 4318 4319 for (b = 0; b < ARRAY_SIZE(vsx->vsx_ind_histo[0]); b++) 4320 vsx->vsx_ind_histo[t][b] += cvsx->vsx_ind_histo[t][b]; 4321 4322 for (b = 0; b < ARRAY_SIZE(vsx->vsx_agg_histo[0]); b++) 4323 vsx->vsx_agg_histo[t][b] += cvsx->vsx_agg_histo[t][b]; 4324 } 4325 4326 } 4327 4328 boolean_t 4329 vdev_is_spacemap_addressable(vdev_t *vd) 4330 { 4331 if (spa_feature_is_active(vd->vdev_spa, SPA_FEATURE_SPACEMAP_V2)) 4332 return (B_TRUE); 4333 4334 /* 4335 * If double-word space map entries are not enabled we assume 4336 * 47 bits of the space map entry are dedicated to the entry's 4337 * offset (see SM_OFFSET_BITS in space_map.h). We then use that 4338 * to calculate the maximum address that can be described by a 4339 * space map entry for the given device. 4340 */ 4341 uint64_t shift = vd->vdev_ashift + SM_OFFSET_BITS; 4342 4343 if (shift >= 63) /* detect potential overflow */ 4344 return (B_TRUE); 4345 4346 return (vd->vdev_asize < (1ULL << shift)); 4347 } 4348 4349 /* 4350 * Get statistics for the given vdev. 4351 */ 4352 static void 4353 vdev_get_stats_ex_impl(vdev_t *vd, vdev_stat_t *vs, vdev_stat_ex_t *vsx) 4354 { 4355 int t; 4356 /* 4357 * If we're getting stats on the root vdev, aggregate the I/O counts 4358 * over all top-level vdevs (i.e. the direct children of the root). 4359 */ 4360 if (!vd->vdev_ops->vdev_op_leaf) { 4361 if (vs) { 4362 memset(vs->vs_ops, 0, sizeof (vs->vs_ops)); 4363 memset(vs->vs_bytes, 0, sizeof (vs->vs_bytes)); 4364 } 4365 if (vsx) 4366 memset(vsx, 0, sizeof (*vsx)); 4367 4368 for (int c = 0; c < vd->vdev_children; c++) { 4369 vdev_t *cvd = vd->vdev_child[c]; 4370 vdev_stat_t *cvs = &cvd->vdev_stat; 4371 vdev_stat_ex_t *cvsx = &cvd->vdev_stat_ex; 4372 4373 vdev_get_stats_ex_impl(cvd, cvs, cvsx); 4374 if (vs) 4375 vdev_get_child_stat(cvd, vs, cvs); 4376 if (vsx) 4377 vdev_get_child_stat_ex(cvd, vsx, cvsx); 4378 } 4379 } else { 4380 /* 4381 * We're a leaf. Just copy our ZIO active queue stats in. The 4382 * other leaf stats are updated in vdev_stat_update(). 4383 */ 4384 if (!vsx) 4385 return; 4386 4387 memcpy(vsx, &vd->vdev_stat_ex, sizeof (vd->vdev_stat_ex)); 4388 4389 for (t = 0; t < ARRAY_SIZE(vd->vdev_queue.vq_class); t++) { 4390 vsx->vsx_active_queue[t] = 4391 vd->vdev_queue.vq_class[t].vqc_active; 4392 vsx->vsx_pend_queue[t] = avl_numnodes( 4393 &vd->vdev_queue.vq_class[t].vqc_queued_tree); 4394 } 4395 } 4396 } 4397 4398 void 4399 vdev_get_stats_ex(vdev_t *vd, vdev_stat_t *vs, vdev_stat_ex_t *vsx) 4400 { 4401 vdev_t *tvd = vd->vdev_top; 4402 mutex_enter(&vd->vdev_stat_lock); 4403 if (vs) { 4404 bcopy(&vd->vdev_stat, vs, sizeof (*vs)); 4405 vs->vs_timestamp = gethrtime() - vs->vs_timestamp; 4406 vs->vs_state = vd->vdev_state; 4407 vs->vs_rsize = vdev_get_min_asize(vd); 4408 4409 if (vd->vdev_ops->vdev_op_leaf) { 4410 vs->vs_rsize += VDEV_LABEL_START_SIZE + 4411 VDEV_LABEL_END_SIZE; 4412 /* 4413 * Report initializing progress. Since we don't 4414 * have the initializing locks held, this is only 4415 * an estimate (although a fairly accurate one). 4416 */ 4417 vs->vs_initialize_bytes_done = 4418 vd->vdev_initialize_bytes_done; 4419 vs->vs_initialize_bytes_est = 4420 vd->vdev_initialize_bytes_est; 4421 vs->vs_initialize_state = vd->vdev_initialize_state; 4422 vs->vs_initialize_action_time = 4423 vd->vdev_initialize_action_time; 4424 4425 /* 4426 * Report manual TRIM progress. Since we don't have 4427 * the manual TRIM locks held, this is only an 4428 * estimate (although fairly accurate one). 4429 */ 4430 vs->vs_trim_notsup = !vd->vdev_has_trim; 4431 vs->vs_trim_bytes_done = vd->vdev_trim_bytes_done; 4432 vs->vs_trim_bytes_est = vd->vdev_trim_bytes_est; 4433 vs->vs_trim_state = vd->vdev_trim_state; 4434 vs->vs_trim_action_time = vd->vdev_trim_action_time; 4435 4436 /* Set when there is a deferred resilver. */ 4437 vs->vs_resilver_deferred = vd->vdev_resilver_deferred; 4438 } 4439 4440 /* 4441 * Report expandable space on top-level, non-auxiliary devices 4442 * only. The expandable space is reported in terms of metaslab 4443 * sized units since that determines how much space the pool 4444 * can expand. 4445 */ 4446 if (vd->vdev_aux == NULL && tvd != NULL) { 4447 vs->vs_esize = P2ALIGN( 4448 vd->vdev_max_asize - vd->vdev_asize, 4449 1ULL << tvd->vdev_ms_shift); 4450 } 4451 4452 vs->vs_configured_ashift = vd->vdev_top != NULL 4453 ? vd->vdev_top->vdev_ashift : vd->vdev_ashift; 4454 vs->vs_logical_ashift = vd->vdev_logical_ashift; 4455 vs->vs_physical_ashift = vd->vdev_physical_ashift; 4456 4457 /* 4458 * Report fragmentation and rebuild progress for top-level, 4459 * non-auxiliary, concrete devices. 4460 */ 4461 if (vd->vdev_aux == NULL && vd == vd->vdev_top && 4462 vdev_is_concrete(vd)) { 4463 /* 4464 * The vdev fragmentation rating doesn't take into 4465 * account the embedded slog metaslab (vdev_log_mg). 4466 * Since it's only one metaslab, it would have a tiny 4467 * impact on the overall fragmentation. 4468 */ 4469 vs->vs_fragmentation = (vd->vdev_mg != NULL) ? 4470 vd->vdev_mg->mg_fragmentation : 0; 4471 } 4472 } 4473 4474 vdev_get_stats_ex_impl(vd, vs, vsx); 4475 mutex_exit(&vd->vdev_stat_lock); 4476 } 4477 4478 void 4479 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs) 4480 { 4481 return (vdev_get_stats_ex(vd, vs, NULL)); 4482 } 4483 4484 void 4485 vdev_clear_stats(vdev_t *vd) 4486 { 4487 mutex_enter(&vd->vdev_stat_lock); 4488 vd->vdev_stat.vs_space = 0; 4489 vd->vdev_stat.vs_dspace = 0; 4490 vd->vdev_stat.vs_alloc = 0; 4491 mutex_exit(&vd->vdev_stat_lock); 4492 } 4493 4494 void 4495 vdev_scan_stat_init(vdev_t *vd) 4496 { 4497 vdev_stat_t *vs = &vd->vdev_stat; 4498 4499 for (int c = 0; c < vd->vdev_children; c++) 4500 vdev_scan_stat_init(vd->vdev_child[c]); 4501 4502 mutex_enter(&vd->vdev_stat_lock); 4503 vs->vs_scan_processed = 0; 4504 mutex_exit(&vd->vdev_stat_lock); 4505 } 4506 4507 void 4508 vdev_stat_update(zio_t *zio, uint64_t psize) 4509 { 4510 spa_t *spa = zio->io_spa; 4511 vdev_t *rvd = spa->spa_root_vdev; 4512 vdev_t *vd = zio->io_vd ? zio->io_vd : rvd; 4513 vdev_t *pvd; 4514 uint64_t txg = zio->io_txg; 4515 vdev_stat_t *vs = &vd->vdev_stat; 4516 vdev_stat_ex_t *vsx = &vd->vdev_stat_ex; 4517 zio_type_t type = zio->io_type; 4518 int flags = zio->io_flags; 4519 4520 /* 4521 * If this i/o is a gang leader, it didn't do any actual work. 4522 */ 4523 if (zio->io_gang_tree) 4524 return; 4525 4526 if (zio->io_error == 0) { 4527 /* 4528 * If this is a root i/o, don't count it -- we've already 4529 * counted the top-level vdevs, and vdev_get_stats() will 4530 * aggregate them when asked. This reduces contention on 4531 * the root vdev_stat_lock and implicitly handles blocks 4532 * that compress away to holes, for which there is no i/o. 4533 * (Holes never create vdev children, so all the counters 4534 * remain zero, which is what we want.) 4535 * 4536 * Note: this only applies to successful i/o (io_error == 0) 4537 * because unlike i/o counts, errors are not additive. 4538 * When reading a ditto block, for example, failure of 4539 * one top-level vdev does not imply a root-level error. 4540 */ 4541 if (vd == rvd) 4542 return; 4543 4544 ASSERT(vd == zio->io_vd); 4545 4546 if (flags & ZIO_FLAG_IO_BYPASS) 4547 return; 4548 4549 mutex_enter(&vd->vdev_stat_lock); 4550 4551 if (flags & ZIO_FLAG_IO_REPAIR) { 4552 /* 4553 * Repair is the result of a resilver issued by the 4554 * scan thread (spa_sync). 4555 */ 4556 if (flags & ZIO_FLAG_SCAN_THREAD) { 4557 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; 4558 dsl_scan_phys_t *scn_phys = &scn->scn_phys; 4559 uint64_t *processed = &scn_phys->scn_processed; 4560 4561 if (vd->vdev_ops->vdev_op_leaf) 4562 atomic_add_64(processed, psize); 4563 vs->vs_scan_processed += psize; 4564 } 4565 4566 /* 4567 * Repair is the result of a rebuild issued by the 4568 * rebuild thread (vdev_rebuild_thread). To avoid 4569 * double counting repaired bytes the virtual dRAID 4570 * spare vdev is excluded from the processed bytes. 4571 */ 4572 if (zio->io_priority == ZIO_PRIORITY_REBUILD) { 4573 vdev_t *tvd = vd->vdev_top; 4574 vdev_rebuild_t *vr = &tvd->vdev_rebuild_config; 4575 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys; 4576 uint64_t *rebuilt = &vrp->vrp_bytes_rebuilt; 4577 4578 if (vd->vdev_ops->vdev_op_leaf && 4579 vd->vdev_ops != &vdev_draid_spare_ops) { 4580 atomic_add_64(rebuilt, psize); 4581 } 4582 vs->vs_rebuild_processed += psize; 4583 } 4584 4585 if (flags & ZIO_FLAG_SELF_HEAL) 4586 vs->vs_self_healed += psize; 4587 } 4588 4589 /* 4590 * The bytes/ops/histograms are recorded at the leaf level and 4591 * aggregated into the higher level vdevs in vdev_get_stats(). 4592 */ 4593 if (vd->vdev_ops->vdev_op_leaf && 4594 (zio->io_priority < ZIO_PRIORITY_NUM_QUEUEABLE)) { 4595 zio_type_t vs_type = type; 4596 zio_priority_t priority = zio->io_priority; 4597 4598 /* 4599 * TRIM ops and bytes are reported to user space as 4600 * ZIO_TYPE_IOCTL. This is done to preserve the 4601 * vdev_stat_t structure layout for user space. 4602 */ 4603 if (type == ZIO_TYPE_TRIM) 4604 vs_type = ZIO_TYPE_IOCTL; 4605 4606 /* 4607 * Solely for the purposes of 'zpool iostat -lqrw' 4608 * reporting use the priority to categorize the IO. 4609 * Only the following are reported to user space: 4610 * 4611 * ZIO_PRIORITY_SYNC_READ, 4612 * ZIO_PRIORITY_SYNC_WRITE, 4613 * ZIO_PRIORITY_ASYNC_READ, 4614 * ZIO_PRIORITY_ASYNC_WRITE, 4615 * ZIO_PRIORITY_SCRUB, 4616 * ZIO_PRIORITY_TRIM, 4617 * ZIO_PRIORITY_REBUILD. 4618 */ 4619 if (priority == ZIO_PRIORITY_INITIALIZING) { 4620 ASSERT3U(type, ==, ZIO_TYPE_WRITE); 4621 priority = ZIO_PRIORITY_ASYNC_WRITE; 4622 } else if (priority == ZIO_PRIORITY_REMOVAL) { 4623 priority = ((type == ZIO_TYPE_WRITE) ? 4624 ZIO_PRIORITY_ASYNC_WRITE : 4625 ZIO_PRIORITY_ASYNC_READ); 4626 } 4627 4628 vs->vs_ops[vs_type]++; 4629 vs->vs_bytes[vs_type] += psize; 4630 4631 if (flags & ZIO_FLAG_DELEGATED) { 4632 vsx->vsx_agg_histo[priority] 4633 [RQ_HISTO(zio->io_size)]++; 4634 } else { 4635 vsx->vsx_ind_histo[priority] 4636 [RQ_HISTO(zio->io_size)]++; 4637 } 4638 4639 if (zio->io_delta && zio->io_delay) { 4640 vsx->vsx_queue_histo[priority] 4641 [L_HISTO(zio->io_delta - zio->io_delay)]++; 4642 vsx->vsx_disk_histo[type] 4643 [L_HISTO(zio->io_delay)]++; 4644 vsx->vsx_total_histo[type] 4645 [L_HISTO(zio->io_delta)]++; 4646 } 4647 } 4648 4649 mutex_exit(&vd->vdev_stat_lock); 4650 return; 4651 } 4652 4653 if (flags & ZIO_FLAG_SPECULATIVE) 4654 return; 4655 4656 /* 4657 * If this is an I/O error that is going to be retried, then ignore the 4658 * error. Otherwise, the user may interpret B_FAILFAST I/O errors as 4659 * hard errors, when in reality they can happen for any number of 4660 * innocuous reasons (bus resets, MPxIO link failure, etc). 4661 */ 4662 if (zio->io_error == EIO && 4663 !(zio->io_flags & ZIO_FLAG_IO_RETRY)) 4664 return; 4665 4666 /* 4667 * Intent logs writes won't propagate their error to the root 4668 * I/O so don't mark these types of failures as pool-level 4669 * errors. 4670 */ 4671 if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE)) 4672 return; 4673 4674 if (type == ZIO_TYPE_WRITE && txg != 0 && 4675 (!(flags & ZIO_FLAG_IO_REPAIR) || 4676 (flags & ZIO_FLAG_SCAN_THREAD) || 4677 spa->spa_claiming)) { 4678 /* 4679 * This is either a normal write (not a repair), or it's 4680 * a repair induced by the scrub thread, or it's a repair 4681 * made by zil_claim() during spa_load() in the first txg. 4682 * In the normal case, we commit the DTL change in the same 4683 * txg as the block was born. In the scrub-induced repair 4684 * case, we know that scrubs run in first-pass syncing context, 4685 * so we commit the DTL change in spa_syncing_txg(spa). 4686 * In the zil_claim() case, we commit in spa_first_txg(spa). 4687 * 4688 * We currently do not make DTL entries for failed spontaneous 4689 * self-healing writes triggered by normal (non-scrubbing) 4690 * reads, because we have no transactional context in which to 4691 * do so -- and it's not clear that it'd be desirable anyway. 4692 */ 4693 if (vd->vdev_ops->vdev_op_leaf) { 4694 uint64_t commit_txg = txg; 4695 if (flags & ZIO_FLAG_SCAN_THREAD) { 4696 ASSERT(flags & ZIO_FLAG_IO_REPAIR); 4697 ASSERT(spa_sync_pass(spa) == 1); 4698 vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1); 4699 commit_txg = spa_syncing_txg(spa); 4700 } else if (spa->spa_claiming) { 4701 ASSERT(flags & ZIO_FLAG_IO_REPAIR); 4702 commit_txg = spa_first_txg(spa); 4703 } 4704 ASSERT(commit_txg >= spa_syncing_txg(spa)); 4705 if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1)) 4706 return; 4707 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 4708 vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1); 4709 vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg); 4710 } 4711 if (vd != rvd) 4712 vdev_dtl_dirty(vd, DTL_MISSING, txg, 1); 4713 } 4714 } 4715 4716 int64_t 4717 vdev_deflated_space(vdev_t *vd, int64_t space) 4718 { 4719 ASSERT((space & (SPA_MINBLOCKSIZE-1)) == 0); 4720 ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache); 4721 4722 return ((space >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio); 4723 } 4724 4725 /* 4726 * Update the in-core space usage stats for this vdev, its metaslab class, 4727 * and the root vdev. 4728 */ 4729 void 4730 vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta, 4731 int64_t space_delta) 4732 { 4733 int64_t dspace_delta; 4734 spa_t *spa = vd->vdev_spa; 4735 vdev_t *rvd = spa->spa_root_vdev; 4736 4737 ASSERT(vd == vd->vdev_top); 4738 4739 /* 4740 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion 4741 * factor. We must calculate this here and not at the root vdev 4742 * because the root vdev's psize-to-asize is simply the max of its 4743 * children's, thus not accurate enough for us. 4744 */ 4745 dspace_delta = vdev_deflated_space(vd, space_delta); 4746 4747 mutex_enter(&vd->vdev_stat_lock); 4748 /* ensure we won't underflow */ 4749 if (alloc_delta < 0) { 4750 ASSERT3U(vd->vdev_stat.vs_alloc, >=, -alloc_delta); 4751 } 4752 4753 vd->vdev_stat.vs_alloc += alloc_delta; 4754 vd->vdev_stat.vs_space += space_delta; 4755 vd->vdev_stat.vs_dspace += dspace_delta; 4756 mutex_exit(&vd->vdev_stat_lock); 4757 4758 /* every class but log contributes to root space stats */ 4759 if (vd->vdev_mg != NULL && !vd->vdev_islog) { 4760 ASSERT(!vd->vdev_isl2cache); 4761 mutex_enter(&rvd->vdev_stat_lock); 4762 rvd->vdev_stat.vs_alloc += alloc_delta; 4763 rvd->vdev_stat.vs_space += space_delta; 4764 rvd->vdev_stat.vs_dspace += dspace_delta; 4765 mutex_exit(&rvd->vdev_stat_lock); 4766 } 4767 /* Note: metaslab_class_space_update moved to metaslab_space_update */ 4768 } 4769 4770 /* 4771 * Mark a top-level vdev's config as dirty, placing it on the dirty list 4772 * so that it will be written out next time the vdev configuration is synced. 4773 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs. 4774 */ 4775 void 4776 vdev_config_dirty(vdev_t *vd) 4777 { 4778 spa_t *spa = vd->vdev_spa; 4779 vdev_t *rvd = spa->spa_root_vdev; 4780 int c; 4781 4782 ASSERT(spa_writeable(spa)); 4783 4784 /* 4785 * If this is an aux vdev (as with l2cache and spare devices), then we 4786 * update the vdev config manually and set the sync flag. 4787 */ 4788 if (vd->vdev_aux != NULL) { 4789 spa_aux_vdev_t *sav = vd->vdev_aux; 4790 nvlist_t **aux; 4791 uint_t naux; 4792 4793 for (c = 0; c < sav->sav_count; c++) { 4794 if (sav->sav_vdevs[c] == vd) 4795 break; 4796 } 4797 4798 if (c == sav->sav_count) { 4799 /* 4800 * We're being removed. There's nothing more to do. 4801 */ 4802 ASSERT(sav->sav_sync == B_TRUE); 4803 return; 4804 } 4805 4806 sav->sav_sync = B_TRUE; 4807 4808 if (nvlist_lookup_nvlist_array(sav->sav_config, 4809 ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) { 4810 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 4811 ZPOOL_CONFIG_SPARES, &aux, &naux) == 0); 4812 } 4813 4814 ASSERT(c < naux); 4815 4816 /* 4817 * Setting the nvlist in the middle if the array is a little 4818 * sketchy, but it will work. 4819 */ 4820 nvlist_free(aux[c]); 4821 aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0); 4822 4823 return; 4824 } 4825 4826 /* 4827 * The dirty list is protected by the SCL_CONFIG lock. The caller 4828 * must either hold SCL_CONFIG as writer, or must be the sync thread 4829 * (which holds SCL_CONFIG as reader). There's only one sync thread, 4830 * so this is sufficient to ensure mutual exclusion. 4831 */ 4832 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 4833 (dsl_pool_sync_context(spa_get_dsl(spa)) && 4834 spa_config_held(spa, SCL_CONFIG, RW_READER))); 4835 4836 if (vd == rvd) { 4837 for (c = 0; c < rvd->vdev_children; c++) 4838 vdev_config_dirty(rvd->vdev_child[c]); 4839 } else { 4840 ASSERT(vd == vd->vdev_top); 4841 4842 if (!list_link_active(&vd->vdev_config_dirty_node) && 4843 vdev_is_concrete(vd)) { 4844 list_insert_head(&spa->spa_config_dirty_list, vd); 4845 } 4846 } 4847 } 4848 4849 void 4850 vdev_config_clean(vdev_t *vd) 4851 { 4852 spa_t *spa = vd->vdev_spa; 4853 4854 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 4855 (dsl_pool_sync_context(spa_get_dsl(spa)) && 4856 spa_config_held(spa, SCL_CONFIG, RW_READER))); 4857 4858 ASSERT(list_link_active(&vd->vdev_config_dirty_node)); 4859 list_remove(&spa->spa_config_dirty_list, vd); 4860 } 4861 4862 /* 4863 * Mark a top-level vdev's state as dirty, so that the next pass of 4864 * spa_sync() can convert this into vdev_config_dirty(). We distinguish 4865 * the state changes from larger config changes because they require 4866 * much less locking, and are often needed for administrative actions. 4867 */ 4868 void 4869 vdev_state_dirty(vdev_t *vd) 4870 { 4871 spa_t *spa = vd->vdev_spa; 4872 4873 ASSERT(spa_writeable(spa)); 4874 ASSERT(vd == vd->vdev_top); 4875 4876 /* 4877 * The state list is protected by the SCL_STATE lock. The caller 4878 * must either hold SCL_STATE as writer, or must be the sync thread 4879 * (which holds SCL_STATE as reader). There's only one sync thread, 4880 * so this is sufficient to ensure mutual exclusion. 4881 */ 4882 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 4883 (dsl_pool_sync_context(spa_get_dsl(spa)) && 4884 spa_config_held(spa, SCL_STATE, RW_READER))); 4885 4886 if (!list_link_active(&vd->vdev_state_dirty_node) && 4887 vdev_is_concrete(vd)) 4888 list_insert_head(&spa->spa_state_dirty_list, vd); 4889 } 4890 4891 void 4892 vdev_state_clean(vdev_t *vd) 4893 { 4894 spa_t *spa = vd->vdev_spa; 4895 4896 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 4897 (dsl_pool_sync_context(spa_get_dsl(spa)) && 4898 spa_config_held(spa, SCL_STATE, RW_READER))); 4899 4900 ASSERT(list_link_active(&vd->vdev_state_dirty_node)); 4901 list_remove(&spa->spa_state_dirty_list, vd); 4902 } 4903 4904 /* 4905 * Propagate vdev state up from children to parent. 4906 */ 4907 void 4908 vdev_propagate_state(vdev_t *vd) 4909 { 4910 spa_t *spa = vd->vdev_spa; 4911 vdev_t *rvd = spa->spa_root_vdev; 4912 int degraded = 0, faulted = 0; 4913 int corrupted = 0; 4914 vdev_t *child; 4915 4916 if (vd->vdev_children > 0) { 4917 for (int c = 0; c < vd->vdev_children; c++) { 4918 child = vd->vdev_child[c]; 4919 4920 /* 4921 * Don't factor holes or indirect vdevs into the 4922 * decision. 4923 */ 4924 if (!vdev_is_concrete(child)) 4925 continue; 4926 4927 if (!vdev_readable(child) || 4928 (!vdev_writeable(child) && spa_writeable(spa))) { 4929 /* 4930 * Root special: if there is a top-level log 4931 * device, treat the root vdev as if it were 4932 * degraded. 4933 */ 4934 if (child->vdev_islog && vd == rvd) 4935 degraded++; 4936 else 4937 faulted++; 4938 } else if (child->vdev_state <= VDEV_STATE_DEGRADED) { 4939 degraded++; 4940 } 4941 4942 if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA) 4943 corrupted++; 4944 } 4945 4946 vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded); 4947 4948 /* 4949 * Root special: if there is a top-level vdev that cannot be 4950 * opened due to corrupted metadata, then propagate the root 4951 * vdev's aux state as 'corrupt' rather than 'insufficient 4952 * replicas'. 4953 */ 4954 if (corrupted && vd == rvd && 4955 rvd->vdev_state == VDEV_STATE_CANT_OPEN) 4956 vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN, 4957 VDEV_AUX_CORRUPT_DATA); 4958 } 4959 4960 if (vd->vdev_parent) 4961 vdev_propagate_state(vd->vdev_parent); 4962 } 4963 4964 /* 4965 * Set a vdev's state. If this is during an open, we don't update the parent 4966 * state, because we're in the process of opening children depth-first. 4967 * Otherwise, we propagate the change to the parent. 4968 * 4969 * If this routine places a device in a faulted state, an appropriate ereport is 4970 * generated. 4971 */ 4972 void 4973 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux) 4974 { 4975 uint64_t save_state; 4976 spa_t *spa = vd->vdev_spa; 4977 4978 if (state == vd->vdev_state) { 4979 /* 4980 * Since vdev_offline() code path is already in an offline 4981 * state we can miss a statechange event to OFFLINE. Check 4982 * the previous state to catch this condition. 4983 */ 4984 if (vd->vdev_ops->vdev_op_leaf && 4985 (state == VDEV_STATE_OFFLINE) && 4986 (vd->vdev_prevstate >= VDEV_STATE_FAULTED)) { 4987 /* post an offline state change */ 4988 zfs_post_state_change(spa, vd, vd->vdev_prevstate); 4989 } 4990 vd->vdev_stat.vs_aux = aux; 4991 return; 4992 } 4993 4994 save_state = vd->vdev_state; 4995 4996 vd->vdev_state = state; 4997 vd->vdev_stat.vs_aux = aux; 4998 4999 /* 5000 * If we are setting the vdev state to anything but an open state, then 5001 * always close the underlying device unless the device has requested 5002 * a delayed close (i.e. we're about to remove or fault the device). 5003 * Otherwise, we keep accessible but invalid devices open forever. 5004 * We don't call vdev_close() itself, because that implies some extra 5005 * checks (offline, etc) that we don't want here. This is limited to 5006 * leaf devices, because otherwise closing the device will affect other 5007 * children. 5008 */ 5009 if (!vd->vdev_delayed_close && vdev_is_dead(vd) && 5010 vd->vdev_ops->vdev_op_leaf) 5011 vd->vdev_ops->vdev_op_close(vd); 5012 5013 if (vd->vdev_removed && 5014 state == VDEV_STATE_CANT_OPEN && 5015 (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) { 5016 /* 5017 * If the previous state is set to VDEV_STATE_REMOVED, then this 5018 * device was previously marked removed and someone attempted to 5019 * reopen it. If this failed due to a nonexistent device, then 5020 * keep the device in the REMOVED state. We also let this be if 5021 * it is one of our special test online cases, which is only 5022 * attempting to online the device and shouldn't generate an FMA 5023 * fault. 5024 */ 5025 vd->vdev_state = VDEV_STATE_REMOVED; 5026 vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 5027 } else if (state == VDEV_STATE_REMOVED) { 5028 vd->vdev_removed = B_TRUE; 5029 } else if (state == VDEV_STATE_CANT_OPEN) { 5030 /* 5031 * If we fail to open a vdev during an import or recovery, we 5032 * mark it as "not available", which signifies that it was 5033 * never there to begin with. Failure to open such a device 5034 * is not considered an error. 5035 */ 5036 if ((spa_load_state(spa) == SPA_LOAD_IMPORT || 5037 spa_load_state(spa) == SPA_LOAD_RECOVER) && 5038 vd->vdev_ops->vdev_op_leaf) 5039 vd->vdev_not_present = 1; 5040 5041 /* 5042 * Post the appropriate ereport. If the 'prevstate' field is 5043 * set to something other than VDEV_STATE_UNKNOWN, it indicates 5044 * that this is part of a vdev_reopen(). In this case, we don't 5045 * want to post the ereport if the device was already in the 5046 * CANT_OPEN state beforehand. 5047 * 5048 * If the 'checkremove' flag is set, then this is an attempt to 5049 * online the device in response to an insertion event. If we 5050 * hit this case, then we have detected an insertion event for a 5051 * faulted or offline device that wasn't in the removed state. 5052 * In this scenario, we don't post an ereport because we are 5053 * about to replace the device, or attempt an online with 5054 * vdev_forcefault, which will generate the fault for us. 5055 */ 5056 if ((vd->vdev_prevstate != state || vd->vdev_forcefault) && 5057 !vd->vdev_not_present && !vd->vdev_checkremove && 5058 vd != spa->spa_root_vdev) { 5059 const char *class; 5060 5061 switch (aux) { 5062 case VDEV_AUX_OPEN_FAILED: 5063 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED; 5064 break; 5065 case VDEV_AUX_CORRUPT_DATA: 5066 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA; 5067 break; 5068 case VDEV_AUX_NO_REPLICAS: 5069 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS; 5070 break; 5071 case VDEV_AUX_BAD_GUID_SUM: 5072 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM; 5073 break; 5074 case VDEV_AUX_TOO_SMALL: 5075 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL; 5076 break; 5077 case VDEV_AUX_BAD_LABEL: 5078 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL; 5079 break; 5080 case VDEV_AUX_BAD_ASHIFT: 5081 class = FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT; 5082 break; 5083 default: 5084 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN; 5085 } 5086 5087 (void) zfs_ereport_post(class, spa, vd, NULL, NULL, 5088 save_state); 5089 } 5090 5091 /* Erase any notion of persistent removed state */ 5092 vd->vdev_removed = B_FALSE; 5093 } else { 5094 vd->vdev_removed = B_FALSE; 5095 } 5096 5097 /* 5098 * Notify ZED of any significant state-change on a leaf vdev. 5099 * 5100 */ 5101 if (vd->vdev_ops->vdev_op_leaf) { 5102 /* preserve original state from a vdev_reopen() */ 5103 if ((vd->vdev_prevstate != VDEV_STATE_UNKNOWN) && 5104 (vd->vdev_prevstate != vd->vdev_state) && 5105 (save_state <= VDEV_STATE_CLOSED)) 5106 save_state = vd->vdev_prevstate; 5107 5108 /* filter out state change due to initial vdev_open */ 5109 if (save_state > VDEV_STATE_CLOSED) 5110 zfs_post_state_change(spa, vd, save_state); 5111 } 5112 5113 if (!isopen && vd->vdev_parent) 5114 vdev_propagate_state(vd->vdev_parent); 5115 } 5116 5117 boolean_t 5118 vdev_children_are_offline(vdev_t *vd) 5119 { 5120 ASSERT(!vd->vdev_ops->vdev_op_leaf); 5121 5122 for (uint64_t i = 0; i < vd->vdev_children; i++) { 5123 if (vd->vdev_child[i]->vdev_state != VDEV_STATE_OFFLINE) 5124 return (B_FALSE); 5125 } 5126 5127 return (B_TRUE); 5128 } 5129 5130 /* 5131 * Check the vdev configuration to ensure that it's capable of supporting 5132 * a root pool. We do not support partial configuration. 5133 */ 5134 boolean_t 5135 vdev_is_bootable(vdev_t *vd) 5136 { 5137 if (!vd->vdev_ops->vdev_op_leaf) { 5138 const char *vdev_type = vd->vdev_ops->vdev_op_type; 5139 5140 if (strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) 5141 return (B_FALSE); 5142 } 5143 5144 for (int c = 0; c < vd->vdev_children; c++) { 5145 if (!vdev_is_bootable(vd->vdev_child[c])) 5146 return (B_FALSE); 5147 } 5148 return (B_TRUE); 5149 } 5150 5151 boolean_t 5152 vdev_is_concrete(vdev_t *vd) 5153 { 5154 vdev_ops_t *ops = vd->vdev_ops; 5155 if (ops == &vdev_indirect_ops || ops == &vdev_hole_ops || 5156 ops == &vdev_missing_ops || ops == &vdev_root_ops) { 5157 return (B_FALSE); 5158 } else { 5159 return (B_TRUE); 5160 } 5161 } 5162 5163 /* 5164 * Determine if a log device has valid content. If the vdev was 5165 * removed or faulted in the MOS config then we know that 5166 * the content on the log device has already been written to the pool. 5167 */ 5168 boolean_t 5169 vdev_log_state_valid(vdev_t *vd) 5170 { 5171 if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted && 5172 !vd->vdev_removed) 5173 return (B_TRUE); 5174 5175 for (int c = 0; c < vd->vdev_children; c++) 5176 if (vdev_log_state_valid(vd->vdev_child[c])) 5177 return (B_TRUE); 5178 5179 return (B_FALSE); 5180 } 5181 5182 /* 5183 * Expand a vdev if possible. 5184 */ 5185 void 5186 vdev_expand(vdev_t *vd, uint64_t txg) 5187 { 5188 ASSERT(vd->vdev_top == vd); 5189 ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 5190 ASSERT(vdev_is_concrete(vd)); 5191 5192 vdev_set_deflate_ratio(vd); 5193 5194 if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count && 5195 vdev_is_concrete(vd)) { 5196 vdev_metaslab_group_create(vd); 5197 VERIFY(vdev_metaslab_init(vd, txg) == 0); 5198 vdev_config_dirty(vd); 5199 } 5200 } 5201 5202 /* 5203 * Split a vdev. 5204 */ 5205 void 5206 vdev_split(vdev_t *vd) 5207 { 5208 vdev_t *cvd, *pvd = vd->vdev_parent; 5209 5210 vdev_remove_child(pvd, vd); 5211 vdev_compact_children(pvd); 5212 5213 cvd = pvd->vdev_child[0]; 5214 if (pvd->vdev_children == 1) { 5215 vdev_remove_parent(cvd); 5216 cvd->vdev_splitting = B_TRUE; 5217 } 5218 vdev_propagate_state(cvd); 5219 } 5220 5221 void 5222 vdev_deadman(vdev_t *vd, char *tag) 5223 { 5224 for (int c = 0; c < vd->vdev_children; c++) { 5225 vdev_t *cvd = vd->vdev_child[c]; 5226 5227 vdev_deadman(cvd, tag); 5228 } 5229 5230 if (vd->vdev_ops->vdev_op_leaf) { 5231 vdev_queue_t *vq = &vd->vdev_queue; 5232 5233 mutex_enter(&vq->vq_lock); 5234 if (avl_numnodes(&vq->vq_active_tree) > 0) { 5235 spa_t *spa = vd->vdev_spa; 5236 zio_t *fio; 5237 uint64_t delta; 5238 5239 zfs_dbgmsg("slow vdev: %s has %lu active IOs", 5240 vd->vdev_path, avl_numnodes(&vq->vq_active_tree)); 5241 5242 /* 5243 * Look at the head of all the pending queues, 5244 * if any I/O has been outstanding for longer than 5245 * the spa_deadman_synctime invoke the deadman logic. 5246 */ 5247 fio = avl_first(&vq->vq_active_tree); 5248 delta = gethrtime() - fio->io_timestamp; 5249 if (delta > spa_deadman_synctime(spa)) 5250 zio_deadman(fio, tag); 5251 } 5252 mutex_exit(&vq->vq_lock); 5253 } 5254 } 5255 5256 void 5257 vdev_defer_resilver(vdev_t *vd) 5258 { 5259 ASSERT(vd->vdev_ops->vdev_op_leaf); 5260 5261 vd->vdev_resilver_deferred = B_TRUE; 5262 vd->vdev_spa->spa_resilver_deferred = B_TRUE; 5263 } 5264 5265 /* 5266 * Clears the resilver deferred flag on all leaf devs under vd. Returns 5267 * B_TRUE if we have devices that need to be resilvered and are available to 5268 * accept resilver I/Os. 5269 */ 5270 boolean_t 5271 vdev_clear_resilver_deferred(vdev_t *vd, dmu_tx_t *tx) 5272 { 5273 boolean_t resilver_needed = B_FALSE; 5274 spa_t *spa = vd->vdev_spa; 5275 5276 for (int c = 0; c < vd->vdev_children; c++) { 5277 vdev_t *cvd = vd->vdev_child[c]; 5278 resilver_needed |= vdev_clear_resilver_deferred(cvd, tx); 5279 } 5280 5281 if (vd == spa->spa_root_vdev && 5282 spa_feature_is_active(spa, SPA_FEATURE_RESILVER_DEFER)) { 5283 spa_feature_decr(spa, SPA_FEATURE_RESILVER_DEFER, tx); 5284 vdev_config_dirty(vd); 5285 spa->spa_resilver_deferred = B_FALSE; 5286 return (resilver_needed); 5287 } 5288 5289 if (!vdev_is_concrete(vd) || vd->vdev_aux || 5290 !vd->vdev_ops->vdev_op_leaf) 5291 return (resilver_needed); 5292 5293 vd->vdev_resilver_deferred = B_FALSE; 5294 5295 return (!vdev_is_dead(vd) && !vd->vdev_offline && 5296 vdev_resilver_needed(vd, NULL, NULL)); 5297 } 5298 5299 boolean_t 5300 vdev_xlate_is_empty(range_seg64_t *rs) 5301 { 5302 return (rs->rs_start == rs->rs_end); 5303 } 5304 5305 /* 5306 * Translate a logical range to the first contiguous physical range for the 5307 * specified vdev_t. This function is initially called with a leaf vdev and 5308 * will walk each parent vdev until it reaches a top-level vdev. Once the 5309 * top-level is reached the physical range is initialized and the recursive 5310 * function begins to unwind. As it unwinds it calls the parent's vdev 5311 * specific translation function to do the real conversion. 5312 */ 5313 void 5314 vdev_xlate(vdev_t *vd, const range_seg64_t *logical_rs, 5315 range_seg64_t *physical_rs, range_seg64_t *remain_rs) 5316 { 5317 /* 5318 * Walk up the vdev tree 5319 */ 5320 if (vd != vd->vdev_top) { 5321 vdev_xlate(vd->vdev_parent, logical_rs, physical_rs, 5322 remain_rs); 5323 } else { 5324 /* 5325 * We've reached the top-level vdev, initialize the physical 5326 * range to the logical range and set an empty remaining 5327 * range then start to unwind. 5328 */ 5329 physical_rs->rs_start = logical_rs->rs_start; 5330 physical_rs->rs_end = logical_rs->rs_end; 5331 5332 remain_rs->rs_start = logical_rs->rs_start; 5333 remain_rs->rs_end = logical_rs->rs_start; 5334 5335 return; 5336 } 5337 5338 vdev_t *pvd = vd->vdev_parent; 5339 ASSERT3P(pvd, !=, NULL); 5340 ASSERT3P(pvd->vdev_ops->vdev_op_xlate, !=, NULL); 5341 5342 /* 5343 * As this recursive function unwinds, translate the logical 5344 * range into its physical and any remaining components by calling 5345 * the vdev specific translate function. 5346 */ 5347 range_seg64_t intermediate = { 0 }; 5348 pvd->vdev_ops->vdev_op_xlate(vd, physical_rs, &intermediate, remain_rs); 5349 5350 physical_rs->rs_start = intermediate.rs_start; 5351 physical_rs->rs_end = intermediate.rs_end; 5352 } 5353 5354 void 5355 vdev_xlate_walk(vdev_t *vd, const range_seg64_t *logical_rs, 5356 vdev_xlate_func_t *func, void *arg) 5357 { 5358 range_seg64_t iter_rs = *logical_rs; 5359 range_seg64_t physical_rs; 5360 range_seg64_t remain_rs; 5361 5362 while (!vdev_xlate_is_empty(&iter_rs)) { 5363 5364 vdev_xlate(vd, &iter_rs, &physical_rs, &remain_rs); 5365 5366 /* 5367 * With raidz and dRAID, it's possible that the logical range 5368 * does not live on this leaf vdev. Only when there is a non- 5369 * zero physical size call the provided function. 5370 */ 5371 if (!vdev_xlate_is_empty(&physical_rs)) 5372 func(arg, &physical_rs); 5373 5374 iter_rs = remain_rs; 5375 } 5376 } 5377 5378 /* 5379 * Look at the vdev tree and determine whether any devices are currently being 5380 * replaced. 5381 */ 5382 boolean_t 5383 vdev_replace_in_progress(vdev_t *vdev) 5384 { 5385 ASSERT(spa_config_held(vdev->vdev_spa, SCL_ALL, RW_READER) != 0); 5386 5387 if (vdev->vdev_ops == &vdev_replacing_ops) 5388 return (B_TRUE); 5389 5390 /* 5391 * A 'spare' vdev indicates that we have a replace in progress, unless 5392 * it has exactly two children, and the second, the hot spare, has 5393 * finished being resilvered. 5394 */ 5395 if (vdev->vdev_ops == &vdev_spare_ops && (vdev->vdev_children > 2 || 5396 !vdev_dtl_empty(vdev->vdev_child[1], DTL_MISSING))) 5397 return (B_TRUE); 5398 5399 for (int i = 0; i < vdev->vdev_children; i++) { 5400 if (vdev_replace_in_progress(vdev->vdev_child[i])) 5401 return (B_TRUE); 5402 } 5403 5404 return (B_FALSE); 5405 } 5406 5407 EXPORT_SYMBOL(vdev_fault); 5408 EXPORT_SYMBOL(vdev_degrade); 5409 EXPORT_SYMBOL(vdev_online); 5410 EXPORT_SYMBOL(vdev_offline); 5411 EXPORT_SYMBOL(vdev_clear); 5412 5413 /* BEGIN CSTYLED */ 5414 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, default_ms_count, INT, ZMOD_RW, 5415 "Target number of metaslabs per top-level vdev"); 5416 5417 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, default_ms_shift, INT, ZMOD_RW, 5418 "Default limit for metaslab size"); 5419 5420 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, min_ms_count, INT, ZMOD_RW, 5421 "Minimum number of metaslabs per top-level vdev"); 5422 5423 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, ms_count_limit, INT, ZMOD_RW, 5424 "Practical upper limit of total metaslabs per top-level vdev"); 5425 5426 ZFS_MODULE_PARAM(zfs, zfs_, slow_io_events_per_second, UINT, ZMOD_RW, 5427 "Rate limit slow IO (delay) events to this many per second"); 5428 5429 ZFS_MODULE_PARAM(zfs, zfs_, checksum_events_per_second, UINT, ZMOD_RW, 5430 "Rate limit checksum events to this many checksum errors per second " 5431 "(do not set below zed threshold)."); 5432 5433 ZFS_MODULE_PARAM(zfs, zfs_, scan_ignore_errors, INT, ZMOD_RW, 5434 "Ignore errors during resilver/scrub"); 5435 5436 ZFS_MODULE_PARAM(zfs_vdev, vdev_, validate_skip, INT, ZMOD_RW, 5437 "Bypass vdev_validate()"); 5438 5439 ZFS_MODULE_PARAM(zfs, zfs_, nocacheflush, INT, ZMOD_RW, 5440 "Disable cache flushes"); 5441 5442 ZFS_MODULE_PARAM(zfs, zfs_, embedded_slog_min_ms, INT, ZMOD_RW, 5443 "Minimum number of metaslabs required to dedicate one for log blocks"); 5444 5445 ZFS_MODULE_PARAM_CALL(zfs_vdev, zfs_vdev_, min_auto_ashift, 5446 param_set_min_auto_ashift, param_get_ulong, ZMOD_RW, 5447 "Minimum ashift used when creating new top-level vdevs"); 5448 5449 ZFS_MODULE_PARAM_CALL(zfs_vdev, zfs_vdev_, max_auto_ashift, 5450 param_set_max_auto_ashift, param_get_ulong, ZMOD_RW, 5451 "Maximum ashift used when optimizing for logical -> physical sector " 5452 "size on new top-level vdevs"); 5453 /* END CSTYLED */ 5454