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 /*PRINTFLIKE2*/ 141 void 142 vdev_dbgmsg(vdev_t *vd, const char *fmt, ...) 143 { 144 va_list adx; 145 char buf[256]; 146 147 va_start(adx, fmt); 148 (void) vsnprintf(buf, sizeof (buf), fmt, adx); 149 va_end(adx); 150 151 if (vd->vdev_path != NULL) { 152 zfs_dbgmsg("%s vdev '%s': %s", vd->vdev_ops->vdev_op_type, 153 vd->vdev_path, buf); 154 } else { 155 zfs_dbgmsg("%s-%llu vdev (guid %llu): %s", 156 vd->vdev_ops->vdev_op_type, 157 (u_longlong_t)vd->vdev_id, 158 (u_longlong_t)vd->vdev_guid, buf); 159 } 160 } 161 162 void 163 vdev_dbgmsg_print_tree(vdev_t *vd, int indent) 164 { 165 char state[20]; 166 167 if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) { 168 zfs_dbgmsg("%*svdev %u: %s", indent, "", 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 if (svd->vdev_path != NULL && dvd->vdev_path != NULL) { 2377 if (strcmp(svd->vdev_path, dvd->vdev_path) != 0) { 2378 zfs_dbgmsg("vdev_copy_path: vdev %llu: path changed " 2379 "from '%s' to '%s'", (u_longlong_t)dvd->vdev_guid, 2380 dvd->vdev_path, svd->vdev_path); 2381 spa_strfree(dvd->vdev_path); 2382 dvd->vdev_path = spa_strdup(svd->vdev_path); 2383 } 2384 } else if (svd->vdev_path != NULL) { 2385 dvd->vdev_path = spa_strdup(svd->vdev_path); 2386 zfs_dbgmsg("vdev_copy_path: vdev %llu: path set to '%s'", 2387 (u_longlong_t)dvd->vdev_guid, dvd->vdev_path); 2388 } 2389 } 2390 2391 /* 2392 * Recursively copy vdev paths from one vdev to another. Source and destination 2393 * vdev trees must have same geometry otherwise return error. Intended to copy 2394 * paths from userland config into MOS config. 2395 */ 2396 int 2397 vdev_copy_path_strict(vdev_t *svd, vdev_t *dvd) 2398 { 2399 if ((svd->vdev_ops == &vdev_missing_ops) || 2400 (svd->vdev_ishole && dvd->vdev_ishole) || 2401 (dvd->vdev_ops == &vdev_indirect_ops)) 2402 return (0); 2403 2404 if (svd->vdev_ops != dvd->vdev_ops) { 2405 vdev_dbgmsg(svd, "vdev_copy_path: vdev type mismatch: %s != %s", 2406 svd->vdev_ops->vdev_op_type, dvd->vdev_ops->vdev_op_type); 2407 return (SET_ERROR(EINVAL)); 2408 } 2409 2410 if (svd->vdev_guid != dvd->vdev_guid) { 2411 vdev_dbgmsg(svd, "vdev_copy_path: guids mismatch (%llu != " 2412 "%llu)", (u_longlong_t)svd->vdev_guid, 2413 (u_longlong_t)dvd->vdev_guid); 2414 return (SET_ERROR(EINVAL)); 2415 } 2416 2417 if (svd->vdev_children != dvd->vdev_children) { 2418 vdev_dbgmsg(svd, "vdev_copy_path: children count mismatch: " 2419 "%llu != %llu", (u_longlong_t)svd->vdev_children, 2420 (u_longlong_t)dvd->vdev_children); 2421 return (SET_ERROR(EINVAL)); 2422 } 2423 2424 for (uint64_t i = 0; i < svd->vdev_children; i++) { 2425 int error = vdev_copy_path_strict(svd->vdev_child[i], 2426 dvd->vdev_child[i]); 2427 if (error != 0) 2428 return (error); 2429 } 2430 2431 if (svd->vdev_ops->vdev_op_leaf) 2432 vdev_copy_path_impl(svd, dvd); 2433 2434 return (0); 2435 } 2436 2437 static void 2438 vdev_copy_path_search(vdev_t *stvd, vdev_t *dvd) 2439 { 2440 ASSERT(stvd->vdev_top == stvd); 2441 ASSERT3U(stvd->vdev_id, ==, dvd->vdev_top->vdev_id); 2442 2443 for (uint64_t i = 0; i < dvd->vdev_children; i++) { 2444 vdev_copy_path_search(stvd, dvd->vdev_child[i]); 2445 } 2446 2447 if (!dvd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(dvd)) 2448 return; 2449 2450 /* 2451 * The idea here is that while a vdev can shift positions within 2452 * a top vdev (when replacing, attaching mirror, etc.) it cannot 2453 * step outside of it. 2454 */ 2455 vdev_t *vd = vdev_lookup_by_guid(stvd, dvd->vdev_guid); 2456 2457 if (vd == NULL || vd->vdev_ops != dvd->vdev_ops) 2458 return; 2459 2460 ASSERT(vd->vdev_ops->vdev_op_leaf); 2461 2462 vdev_copy_path_impl(vd, dvd); 2463 } 2464 2465 /* 2466 * Recursively copy vdev paths from one root vdev to another. Source and 2467 * destination vdev trees may differ in geometry. For each destination leaf 2468 * vdev, search a vdev with the same guid and top vdev id in the source. 2469 * Intended to copy paths from userland config into MOS config. 2470 */ 2471 void 2472 vdev_copy_path_relaxed(vdev_t *srvd, vdev_t *drvd) 2473 { 2474 uint64_t children = MIN(srvd->vdev_children, drvd->vdev_children); 2475 ASSERT(srvd->vdev_ops == &vdev_root_ops); 2476 ASSERT(drvd->vdev_ops == &vdev_root_ops); 2477 2478 for (uint64_t i = 0; i < children; i++) { 2479 vdev_copy_path_search(srvd->vdev_child[i], 2480 drvd->vdev_child[i]); 2481 } 2482 } 2483 2484 /* 2485 * Close a virtual device. 2486 */ 2487 void 2488 vdev_close(vdev_t *vd) 2489 { 2490 vdev_t *pvd = vd->vdev_parent; 2491 spa_t *spa __maybe_unused = vd->vdev_spa; 2492 2493 ASSERT(vd != NULL); 2494 ASSERT(vd->vdev_open_thread == curthread || 2495 spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 2496 2497 /* 2498 * If our parent is reopening, then we are as well, unless we are 2499 * going offline. 2500 */ 2501 if (pvd != NULL && pvd->vdev_reopening) 2502 vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline); 2503 2504 vd->vdev_ops->vdev_op_close(vd); 2505 2506 vdev_cache_purge(vd); 2507 2508 /* 2509 * We record the previous state before we close it, so that if we are 2510 * doing a reopen(), we don't generate FMA ereports if we notice that 2511 * it's still faulted. 2512 */ 2513 vd->vdev_prevstate = vd->vdev_state; 2514 2515 if (vd->vdev_offline) 2516 vd->vdev_state = VDEV_STATE_OFFLINE; 2517 else 2518 vd->vdev_state = VDEV_STATE_CLOSED; 2519 vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 2520 } 2521 2522 void 2523 vdev_hold(vdev_t *vd) 2524 { 2525 spa_t *spa = vd->vdev_spa; 2526 2527 ASSERT(spa_is_root(spa)); 2528 if (spa->spa_state == POOL_STATE_UNINITIALIZED) 2529 return; 2530 2531 for (int c = 0; c < vd->vdev_children; c++) 2532 vdev_hold(vd->vdev_child[c]); 2533 2534 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_ops->vdev_op_hold != NULL) 2535 vd->vdev_ops->vdev_op_hold(vd); 2536 } 2537 2538 void 2539 vdev_rele(vdev_t *vd) 2540 { 2541 ASSERT(spa_is_root(vd->vdev_spa)); 2542 for (int c = 0; c < vd->vdev_children; c++) 2543 vdev_rele(vd->vdev_child[c]); 2544 2545 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_ops->vdev_op_rele != NULL) 2546 vd->vdev_ops->vdev_op_rele(vd); 2547 } 2548 2549 /* 2550 * Reopen all interior vdevs and any unopened leaves. We don't actually 2551 * reopen leaf vdevs which had previously been opened as they might deadlock 2552 * on the spa_config_lock. Instead we only obtain the leaf's physical size. 2553 * If the leaf has never been opened then open it, as usual. 2554 */ 2555 void 2556 vdev_reopen(vdev_t *vd) 2557 { 2558 spa_t *spa = vd->vdev_spa; 2559 2560 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 2561 2562 /* set the reopening flag unless we're taking the vdev offline */ 2563 vd->vdev_reopening = !vd->vdev_offline; 2564 vdev_close(vd); 2565 (void) vdev_open(vd); 2566 2567 /* 2568 * Call vdev_validate() here to make sure we have the same device. 2569 * Otherwise, a device with an invalid label could be successfully 2570 * opened in response to vdev_reopen(). 2571 */ 2572 if (vd->vdev_aux) { 2573 (void) vdev_validate_aux(vd); 2574 if (vdev_readable(vd) && vdev_writeable(vd) && 2575 vd->vdev_aux == &spa->spa_l2cache) { 2576 /* 2577 * In case the vdev is present we should evict all ARC 2578 * buffers and pointers to log blocks and reclaim their 2579 * space before restoring its contents to L2ARC. 2580 */ 2581 if (l2arc_vdev_present(vd)) { 2582 l2arc_rebuild_vdev(vd, B_TRUE); 2583 } else { 2584 l2arc_add_vdev(spa, vd); 2585 } 2586 spa_async_request(spa, SPA_ASYNC_L2CACHE_REBUILD); 2587 spa_async_request(spa, SPA_ASYNC_L2CACHE_TRIM); 2588 } 2589 } else { 2590 (void) vdev_validate(vd); 2591 } 2592 2593 /* 2594 * Reassess parent vdev's health. 2595 */ 2596 vdev_propagate_state(vd); 2597 } 2598 2599 int 2600 vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing) 2601 { 2602 int error; 2603 2604 /* 2605 * Normally, partial opens (e.g. of a mirror) are allowed. 2606 * For a create, however, we want to fail the request if 2607 * there are any components we can't open. 2608 */ 2609 error = vdev_open(vd); 2610 2611 if (error || vd->vdev_state != VDEV_STATE_HEALTHY) { 2612 vdev_close(vd); 2613 return (error ? error : SET_ERROR(ENXIO)); 2614 } 2615 2616 /* 2617 * Recursively load DTLs and initialize all labels. 2618 */ 2619 if ((error = vdev_dtl_load(vd)) != 0 || 2620 (error = vdev_label_init(vd, txg, isreplacing ? 2621 VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) { 2622 vdev_close(vd); 2623 return (error); 2624 } 2625 2626 return (0); 2627 } 2628 2629 void 2630 vdev_metaslab_set_size(vdev_t *vd) 2631 { 2632 uint64_t asize = vd->vdev_asize; 2633 uint64_t ms_count = asize >> zfs_vdev_default_ms_shift; 2634 uint64_t ms_shift; 2635 2636 /* 2637 * There are two dimensions to the metaslab sizing calculation: 2638 * the size of the metaslab and the count of metaslabs per vdev. 2639 * 2640 * The default values used below are a good balance between memory 2641 * usage (larger metaslab size means more memory needed for loaded 2642 * metaslabs; more metaslabs means more memory needed for the 2643 * metaslab_t structs), metaslab load time (larger metaslabs take 2644 * longer to load), and metaslab sync time (more metaslabs means 2645 * more time spent syncing all of them). 2646 * 2647 * In general, we aim for zfs_vdev_default_ms_count (200) metaslabs. 2648 * The range of the dimensions are as follows: 2649 * 2650 * 2^29 <= ms_size <= 2^34 2651 * 16 <= ms_count <= 131,072 2652 * 2653 * On the lower end of vdev sizes, we aim for metaslabs sizes of 2654 * at least 512MB (2^29) to minimize fragmentation effects when 2655 * testing with smaller devices. However, the count constraint 2656 * of at least 16 metaslabs will override this minimum size goal. 2657 * 2658 * On the upper end of vdev sizes, we aim for a maximum metaslab 2659 * size of 16GB. However, we will cap the total count to 2^17 2660 * metaslabs to keep our memory footprint in check and let the 2661 * metaslab size grow from there if that limit is hit. 2662 * 2663 * The net effect of applying above constrains is summarized below. 2664 * 2665 * vdev size metaslab count 2666 * --------------|----------------- 2667 * < 8GB ~16 2668 * 8GB - 100GB one per 512MB 2669 * 100GB - 3TB ~200 2670 * 3TB - 2PB one per 16GB 2671 * > 2PB ~131,072 2672 * -------------------------------- 2673 * 2674 * Finally, note that all of the above calculate the initial 2675 * number of metaslabs. Expanding a top-level vdev will result 2676 * in additional metaslabs being allocated making it possible 2677 * to exceed the zfs_vdev_ms_count_limit. 2678 */ 2679 2680 if (ms_count < zfs_vdev_min_ms_count) 2681 ms_shift = highbit64(asize / zfs_vdev_min_ms_count); 2682 else if (ms_count > zfs_vdev_default_ms_count) 2683 ms_shift = highbit64(asize / zfs_vdev_default_ms_count); 2684 else 2685 ms_shift = zfs_vdev_default_ms_shift; 2686 2687 if (ms_shift < SPA_MAXBLOCKSHIFT) { 2688 ms_shift = SPA_MAXBLOCKSHIFT; 2689 } else if (ms_shift > zfs_vdev_max_ms_shift) { 2690 ms_shift = zfs_vdev_max_ms_shift; 2691 /* cap the total count to constrain memory footprint */ 2692 if ((asize >> ms_shift) > zfs_vdev_ms_count_limit) 2693 ms_shift = highbit64(asize / zfs_vdev_ms_count_limit); 2694 } 2695 2696 vd->vdev_ms_shift = ms_shift; 2697 ASSERT3U(vd->vdev_ms_shift, >=, SPA_MAXBLOCKSHIFT); 2698 } 2699 2700 void 2701 vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg) 2702 { 2703 ASSERT(vd == vd->vdev_top); 2704 /* indirect vdevs don't have metaslabs or dtls */ 2705 ASSERT(vdev_is_concrete(vd) || flags == 0); 2706 ASSERT(ISP2(flags)); 2707 ASSERT(spa_writeable(vd->vdev_spa)); 2708 2709 if (flags & VDD_METASLAB) 2710 (void) txg_list_add(&vd->vdev_ms_list, arg, txg); 2711 2712 if (flags & VDD_DTL) 2713 (void) txg_list_add(&vd->vdev_dtl_list, arg, txg); 2714 2715 (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg); 2716 } 2717 2718 void 2719 vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg) 2720 { 2721 for (int c = 0; c < vd->vdev_children; c++) 2722 vdev_dirty_leaves(vd->vdev_child[c], flags, txg); 2723 2724 if (vd->vdev_ops->vdev_op_leaf) 2725 vdev_dirty(vd->vdev_top, flags, vd, txg); 2726 } 2727 2728 /* 2729 * DTLs. 2730 * 2731 * A vdev's DTL (dirty time log) is the set of transaction groups for which 2732 * the vdev has less than perfect replication. There are four kinds of DTL: 2733 * 2734 * DTL_MISSING: txgs for which the vdev has no valid copies of the data 2735 * 2736 * DTL_PARTIAL: txgs for which data is available, but not fully replicated 2737 * 2738 * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon 2739 * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of 2740 * txgs that was scrubbed. 2741 * 2742 * DTL_OUTAGE: txgs which cannot currently be read, whether due to 2743 * persistent errors or just some device being offline. 2744 * Unlike the other three, the DTL_OUTAGE map is not generally 2745 * maintained; it's only computed when needed, typically to 2746 * determine whether a device can be detached. 2747 * 2748 * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device 2749 * either has the data or it doesn't. 2750 * 2751 * For interior vdevs such as mirror and RAID-Z the picture is more complex. 2752 * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because 2753 * if any child is less than fully replicated, then so is its parent. 2754 * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs, 2755 * comprising only those txgs which appear in 'maxfaults' or more children; 2756 * those are the txgs we don't have enough replication to read. For example, 2757 * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2); 2758 * thus, its DTL_MISSING consists of the set of txgs that appear in more than 2759 * two child DTL_MISSING maps. 2760 * 2761 * It should be clear from the above that to compute the DTLs and outage maps 2762 * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps. 2763 * Therefore, that is all we keep on disk. When loading the pool, or after 2764 * a configuration change, we generate all other DTLs from first principles. 2765 */ 2766 void 2767 vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 2768 { 2769 range_tree_t *rt = vd->vdev_dtl[t]; 2770 2771 ASSERT(t < DTL_TYPES); 2772 ASSERT(vd != vd->vdev_spa->spa_root_vdev); 2773 ASSERT(spa_writeable(vd->vdev_spa)); 2774 2775 mutex_enter(&vd->vdev_dtl_lock); 2776 if (!range_tree_contains(rt, txg, size)) 2777 range_tree_add(rt, txg, size); 2778 mutex_exit(&vd->vdev_dtl_lock); 2779 } 2780 2781 boolean_t 2782 vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 2783 { 2784 range_tree_t *rt = vd->vdev_dtl[t]; 2785 boolean_t dirty = B_FALSE; 2786 2787 ASSERT(t < DTL_TYPES); 2788 ASSERT(vd != vd->vdev_spa->spa_root_vdev); 2789 2790 /* 2791 * While we are loading the pool, the DTLs have not been loaded yet. 2792 * This isn't a problem but it can result in devices being tried 2793 * which are known to not have the data. In which case, the import 2794 * is relying on the checksum to ensure that we get the right data. 2795 * Note that while importing we are only reading the MOS, which is 2796 * always checksummed. 2797 */ 2798 mutex_enter(&vd->vdev_dtl_lock); 2799 if (!range_tree_is_empty(rt)) 2800 dirty = range_tree_contains(rt, txg, size); 2801 mutex_exit(&vd->vdev_dtl_lock); 2802 2803 return (dirty); 2804 } 2805 2806 boolean_t 2807 vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t) 2808 { 2809 range_tree_t *rt = vd->vdev_dtl[t]; 2810 boolean_t empty; 2811 2812 mutex_enter(&vd->vdev_dtl_lock); 2813 empty = range_tree_is_empty(rt); 2814 mutex_exit(&vd->vdev_dtl_lock); 2815 2816 return (empty); 2817 } 2818 2819 /* 2820 * Check if the txg falls within the range which must be 2821 * resilvered. DVAs outside this range can always be skipped. 2822 */ 2823 boolean_t 2824 vdev_default_need_resilver(vdev_t *vd, const dva_t *dva, size_t psize, 2825 uint64_t phys_birth) 2826 { 2827 /* Set by sequential resilver. */ 2828 if (phys_birth == TXG_UNKNOWN) 2829 return (B_TRUE); 2830 2831 return (vdev_dtl_contains(vd, DTL_PARTIAL, phys_birth, 1)); 2832 } 2833 2834 /* 2835 * Returns B_TRUE if the vdev determines the DVA needs to be resilvered. 2836 */ 2837 boolean_t 2838 vdev_dtl_need_resilver(vdev_t *vd, const dva_t *dva, size_t psize, 2839 uint64_t phys_birth) 2840 { 2841 ASSERT(vd != vd->vdev_spa->spa_root_vdev); 2842 2843 if (vd->vdev_ops->vdev_op_need_resilver == NULL || 2844 vd->vdev_ops->vdev_op_leaf) 2845 return (B_TRUE); 2846 2847 return (vd->vdev_ops->vdev_op_need_resilver(vd, dva, psize, 2848 phys_birth)); 2849 } 2850 2851 /* 2852 * Returns the lowest txg in the DTL range. 2853 */ 2854 static uint64_t 2855 vdev_dtl_min(vdev_t *vd) 2856 { 2857 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock)); 2858 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0); 2859 ASSERT0(vd->vdev_children); 2860 2861 return (range_tree_min(vd->vdev_dtl[DTL_MISSING]) - 1); 2862 } 2863 2864 /* 2865 * Returns the highest txg in the DTL. 2866 */ 2867 static uint64_t 2868 vdev_dtl_max(vdev_t *vd) 2869 { 2870 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock)); 2871 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0); 2872 ASSERT0(vd->vdev_children); 2873 2874 return (range_tree_max(vd->vdev_dtl[DTL_MISSING])); 2875 } 2876 2877 /* 2878 * Determine if a resilvering vdev should remove any DTL entries from 2879 * its range. If the vdev was resilvering for the entire duration of the 2880 * scan then it should excise that range from its DTLs. Otherwise, this 2881 * vdev is considered partially resilvered and should leave its DTL 2882 * entries intact. The comment in vdev_dtl_reassess() describes how we 2883 * excise the DTLs. 2884 */ 2885 static boolean_t 2886 vdev_dtl_should_excise(vdev_t *vd, boolean_t rebuild_done) 2887 { 2888 ASSERT0(vd->vdev_children); 2889 2890 if (vd->vdev_state < VDEV_STATE_DEGRADED) 2891 return (B_FALSE); 2892 2893 if (vd->vdev_resilver_deferred) 2894 return (B_FALSE); 2895 2896 if (range_tree_is_empty(vd->vdev_dtl[DTL_MISSING])) 2897 return (B_TRUE); 2898 2899 if (rebuild_done) { 2900 vdev_rebuild_t *vr = &vd->vdev_top->vdev_rebuild_config; 2901 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys; 2902 2903 /* Rebuild not initiated by attach */ 2904 if (vd->vdev_rebuild_txg == 0) 2905 return (B_TRUE); 2906 2907 /* 2908 * When a rebuild completes without error then all missing data 2909 * up to the rebuild max txg has been reconstructed and the DTL 2910 * is eligible for excision. 2911 */ 2912 if (vrp->vrp_rebuild_state == VDEV_REBUILD_COMPLETE && 2913 vdev_dtl_max(vd) <= vrp->vrp_max_txg) { 2914 ASSERT3U(vrp->vrp_min_txg, <=, vdev_dtl_min(vd)); 2915 ASSERT3U(vrp->vrp_min_txg, <, vd->vdev_rebuild_txg); 2916 ASSERT3U(vd->vdev_rebuild_txg, <=, vrp->vrp_max_txg); 2917 return (B_TRUE); 2918 } 2919 } else { 2920 dsl_scan_t *scn = vd->vdev_spa->spa_dsl_pool->dp_scan; 2921 dsl_scan_phys_t *scnp __maybe_unused = &scn->scn_phys; 2922 2923 /* Resilver not initiated by attach */ 2924 if (vd->vdev_resilver_txg == 0) 2925 return (B_TRUE); 2926 2927 /* 2928 * When a resilver is initiated the scan will assign the 2929 * scn_max_txg value to the highest txg value that exists 2930 * in all DTLs. If this device's max DTL is not part of this 2931 * scan (i.e. it is not in the range (scn_min_txg, scn_max_txg] 2932 * then it is not eligible for excision. 2933 */ 2934 if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) { 2935 ASSERT3U(scnp->scn_min_txg, <=, vdev_dtl_min(vd)); 2936 ASSERT3U(scnp->scn_min_txg, <, vd->vdev_resilver_txg); 2937 ASSERT3U(vd->vdev_resilver_txg, <=, scnp->scn_max_txg); 2938 return (B_TRUE); 2939 } 2940 } 2941 2942 return (B_FALSE); 2943 } 2944 2945 /* 2946 * Reassess DTLs after a config change or scrub completion. If txg == 0 no 2947 * write operations will be issued to the pool. 2948 */ 2949 void 2950 vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, 2951 boolean_t scrub_done, boolean_t rebuild_done) 2952 { 2953 spa_t *spa = vd->vdev_spa; 2954 avl_tree_t reftree; 2955 int minref; 2956 2957 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 2958 2959 for (int c = 0; c < vd->vdev_children; c++) 2960 vdev_dtl_reassess(vd->vdev_child[c], txg, 2961 scrub_txg, scrub_done, rebuild_done); 2962 2963 if (vd == spa->spa_root_vdev || !vdev_is_concrete(vd) || vd->vdev_aux) 2964 return; 2965 2966 if (vd->vdev_ops->vdev_op_leaf) { 2967 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; 2968 vdev_rebuild_t *vr = &vd->vdev_top->vdev_rebuild_config; 2969 boolean_t check_excise = B_FALSE; 2970 boolean_t wasempty = B_TRUE; 2971 2972 mutex_enter(&vd->vdev_dtl_lock); 2973 2974 /* 2975 * If requested, pretend the scan or rebuild completed cleanly. 2976 */ 2977 if (zfs_scan_ignore_errors) { 2978 if (scn != NULL) 2979 scn->scn_phys.scn_errors = 0; 2980 if (vr != NULL) 2981 vr->vr_rebuild_phys.vrp_errors = 0; 2982 } 2983 2984 if (scrub_txg != 0 && 2985 !range_tree_is_empty(vd->vdev_dtl[DTL_MISSING])) { 2986 wasempty = B_FALSE; 2987 zfs_dbgmsg("guid:%llu txg:%llu scrub:%llu started:%d " 2988 "dtl:%llu/%llu errors:%llu", 2989 (u_longlong_t)vd->vdev_guid, (u_longlong_t)txg, 2990 (u_longlong_t)scrub_txg, spa->spa_scrub_started, 2991 (u_longlong_t)vdev_dtl_min(vd), 2992 (u_longlong_t)vdev_dtl_max(vd), 2993 (u_longlong_t)(scn ? scn->scn_phys.scn_errors : 0)); 2994 } 2995 2996 /* 2997 * If we've completed a scrub/resilver or a rebuild cleanly 2998 * then determine if this vdev should remove any DTLs. We 2999 * only want to excise regions on vdevs that were available 3000 * during the entire duration of this scan. 3001 */ 3002 if (rebuild_done && 3003 vr != NULL && vr->vr_rebuild_phys.vrp_errors == 0) { 3004 check_excise = B_TRUE; 3005 } else { 3006 if (spa->spa_scrub_started || 3007 (scn != NULL && scn->scn_phys.scn_errors == 0)) { 3008 check_excise = B_TRUE; 3009 } 3010 } 3011 3012 if (scrub_txg && check_excise && 3013 vdev_dtl_should_excise(vd, rebuild_done)) { 3014 /* 3015 * We completed a scrub, resilver or rebuild up to 3016 * scrub_txg. If we did it without rebooting, then 3017 * the scrub dtl will be valid, so excise the old 3018 * region and fold in the scrub dtl. Otherwise, 3019 * leave the dtl as-is if there was an error. 3020 * 3021 * There's little trick here: to excise the beginning 3022 * of the DTL_MISSING map, we put it into a reference 3023 * tree and then add a segment with refcnt -1 that 3024 * covers the range [0, scrub_txg). This means 3025 * that each txg in that range has refcnt -1 or 0. 3026 * We then add DTL_SCRUB with a refcnt of 2, so that 3027 * entries in the range [0, scrub_txg) will have a 3028 * positive refcnt -- either 1 or 2. We then convert 3029 * the reference tree into the new DTL_MISSING map. 3030 */ 3031 space_reftree_create(&reftree); 3032 space_reftree_add_map(&reftree, 3033 vd->vdev_dtl[DTL_MISSING], 1); 3034 space_reftree_add_seg(&reftree, 0, scrub_txg, -1); 3035 space_reftree_add_map(&reftree, 3036 vd->vdev_dtl[DTL_SCRUB], 2); 3037 space_reftree_generate_map(&reftree, 3038 vd->vdev_dtl[DTL_MISSING], 1); 3039 space_reftree_destroy(&reftree); 3040 3041 if (!range_tree_is_empty(vd->vdev_dtl[DTL_MISSING])) { 3042 zfs_dbgmsg("update DTL_MISSING:%llu/%llu", 3043 (u_longlong_t)vdev_dtl_min(vd), 3044 (u_longlong_t)vdev_dtl_max(vd)); 3045 } else if (!wasempty) { 3046 zfs_dbgmsg("DTL_MISSING is now empty"); 3047 } 3048 } 3049 range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL); 3050 range_tree_walk(vd->vdev_dtl[DTL_MISSING], 3051 range_tree_add, vd->vdev_dtl[DTL_PARTIAL]); 3052 if (scrub_done) 3053 range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL); 3054 range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL); 3055 if (!vdev_readable(vd)) 3056 range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL); 3057 else 3058 range_tree_walk(vd->vdev_dtl[DTL_MISSING], 3059 range_tree_add, vd->vdev_dtl[DTL_OUTAGE]); 3060 3061 /* 3062 * If the vdev was resilvering or rebuilding and no longer 3063 * has any DTLs then reset the appropriate flag and dirty 3064 * the top level so that we persist the change. 3065 */ 3066 if (txg != 0 && 3067 range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) && 3068 range_tree_is_empty(vd->vdev_dtl[DTL_OUTAGE])) { 3069 if (vd->vdev_rebuild_txg != 0) { 3070 vd->vdev_rebuild_txg = 0; 3071 vdev_config_dirty(vd->vdev_top); 3072 } else if (vd->vdev_resilver_txg != 0) { 3073 vd->vdev_resilver_txg = 0; 3074 vdev_config_dirty(vd->vdev_top); 3075 } 3076 } 3077 3078 mutex_exit(&vd->vdev_dtl_lock); 3079 3080 if (txg != 0) 3081 vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg); 3082 return; 3083 } 3084 3085 mutex_enter(&vd->vdev_dtl_lock); 3086 for (int t = 0; t < DTL_TYPES; t++) { 3087 /* account for child's outage in parent's missing map */ 3088 int s = (t == DTL_MISSING) ? DTL_OUTAGE: t; 3089 if (t == DTL_SCRUB) 3090 continue; /* leaf vdevs only */ 3091 if (t == DTL_PARTIAL) 3092 minref = 1; /* i.e. non-zero */ 3093 else if (vdev_get_nparity(vd) != 0) 3094 minref = vdev_get_nparity(vd) + 1; /* RAID-Z, dRAID */ 3095 else 3096 minref = vd->vdev_children; /* any kind of mirror */ 3097 space_reftree_create(&reftree); 3098 for (int c = 0; c < vd->vdev_children; c++) { 3099 vdev_t *cvd = vd->vdev_child[c]; 3100 mutex_enter(&cvd->vdev_dtl_lock); 3101 space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1); 3102 mutex_exit(&cvd->vdev_dtl_lock); 3103 } 3104 space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref); 3105 space_reftree_destroy(&reftree); 3106 } 3107 mutex_exit(&vd->vdev_dtl_lock); 3108 } 3109 3110 int 3111 vdev_dtl_load(vdev_t *vd) 3112 { 3113 spa_t *spa = vd->vdev_spa; 3114 objset_t *mos = spa->spa_meta_objset; 3115 range_tree_t *rt; 3116 int error = 0; 3117 3118 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) { 3119 ASSERT(vdev_is_concrete(vd)); 3120 3121 error = space_map_open(&vd->vdev_dtl_sm, mos, 3122 vd->vdev_dtl_object, 0, -1ULL, 0); 3123 if (error) 3124 return (error); 3125 ASSERT(vd->vdev_dtl_sm != NULL); 3126 3127 rt = range_tree_create(NULL, RANGE_SEG64, NULL, 0, 0); 3128 error = space_map_load(vd->vdev_dtl_sm, rt, SM_ALLOC); 3129 if (error == 0) { 3130 mutex_enter(&vd->vdev_dtl_lock); 3131 range_tree_walk(rt, range_tree_add, 3132 vd->vdev_dtl[DTL_MISSING]); 3133 mutex_exit(&vd->vdev_dtl_lock); 3134 } 3135 3136 range_tree_vacate(rt, NULL, NULL); 3137 range_tree_destroy(rt); 3138 3139 return (error); 3140 } 3141 3142 for (int c = 0; c < vd->vdev_children; c++) { 3143 error = vdev_dtl_load(vd->vdev_child[c]); 3144 if (error != 0) 3145 break; 3146 } 3147 3148 return (error); 3149 } 3150 3151 static void 3152 vdev_zap_allocation_data(vdev_t *vd, dmu_tx_t *tx) 3153 { 3154 spa_t *spa = vd->vdev_spa; 3155 objset_t *mos = spa->spa_meta_objset; 3156 vdev_alloc_bias_t alloc_bias = vd->vdev_alloc_bias; 3157 const char *string; 3158 3159 ASSERT(alloc_bias != VDEV_BIAS_NONE); 3160 3161 string = 3162 (alloc_bias == VDEV_BIAS_LOG) ? VDEV_ALLOC_BIAS_LOG : 3163 (alloc_bias == VDEV_BIAS_SPECIAL) ? VDEV_ALLOC_BIAS_SPECIAL : 3164 (alloc_bias == VDEV_BIAS_DEDUP) ? VDEV_ALLOC_BIAS_DEDUP : NULL; 3165 3166 ASSERT(string != NULL); 3167 VERIFY0(zap_add(mos, vd->vdev_top_zap, VDEV_TOP_ZAP_ALLOCATION_BIAS, 3168 1, strlen(string) + 1, string, tx)); 3169 3170 if (alloc_bias == VDEV_BIAS_SPECIAL || alloc_bias == VDEV_BIAS_DEDUP) { 3171 spa_activate_allocation_classes(spa, tx); 3172 } 3173 } 3174 3175 void 3176 vdev_destroy_unlink_zap(vdev_t *vd, uint64_t zapobj, dmu_tx_t *tx) 3177 { 3178 spa_t *spa = vd->vdev_spa; 3179 3180 VERIFY0(zap_destroy(spa->spa_meta_objset, zapobj, tx)); 3181 VERIFY0(zap_remove_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps, 3182 zapobj, tx)); 3183 } 3184 3185 uint64_t 3186 vdev_create_link_zap(vdev_t *vd, dmu_tx_t *tx) 3187 { 3188 spa_t *spa = vd->vdev_spa; 3189 uint64_t zap = zap_create(spa->spa_meta_objset, DMU_OTN_ZAP_METADATA, 3190 DMU_OT_NONE, 0, tx); 3191 3192 ASSERT(zap != 0); 3193 VERIFY0(zap_add_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps, 3194 zap, tx)); 3195 3196 return (zap); 3197 } 3198 3199 void 3200 vdev_construct_zaps(vdev_t *vd, dmu_tx_t *tx) 3201 { 3202 if (vd->vdev_ops != &vdev_hole_ops && 3203 vd->vdev_ops != &vdev_missing_ops && 3204 vd->vdev_ops != &vdev_root_ops && 3205 !vd->vdev_top->vdev_removing) { 3206 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_leaf_zap == 0) { 3207 vd->vdev_leaf_zap = vdev_create_link_zap(vd, tx); 3208 } 3209 if (vd == vd->vdev_top && vd->vdev_top_zap == 0) { 3210 vd->vdev_top_zap = vdev_create_link_zap(vd, tx); 3211 if (vd->vdev_alloc_bias != VDEV_BIAS_NONE) 3212 vdev_zap_allocation_data(vd, tx); 3213 } 3214 } 3215 3216 for (uint64_t i = 0; i < vd->vdev_children; i++) { 3217 vdev_construct_zaps(vd->vdev_child[i], tx); 3218 } 3219 } 3220 3221 static void 3222 vdev_dtl_sync(vdev_t *vd, uint64_t txg) 3223 { 3224 spa_t *spa = vd->vdev_spa; 3225 range_tree_t *rt = vd->vdev_dtl[DTL_MISSING]; 3226 objset_t *mos = spa->spa_meta_objset; 3227 range_tree_t *rtsync; 3228 dmu_tx_t *tx; 3229 uint64_t object = space_map_object(vd->vdev_dtl_sm); 3230 3231 ASSERT(vdev_is_concrete(vd)); 3232 ASSERT(vd->vdev_ops->vdev_op_leaf); 3233 3234 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 3235 3236 if (vd->vdev_detached || vd->vdev_top->vdev_removing) { 3237 mutex_enter(&vd->vdev_dtl_lock); 3238 space_map_free(vd->vdev_dtl_sm, tx); 3239 space_map_close(vd->vdev_dtl_sm); 3240 vd->vdev_dtl_sm = NULL; 3241 mutex_exit(&vd->vdev_dtl_lock); 3242 3243 /* 3244 * We only destroy the leaf ZAP for detached leaves or for 3245 * removed log devices. Removed data devices handle leaf ZAP 3246 * cleanup later, once cancellation is no longer possible. 3247 */ 3248 if (vd->vdev_leaf_zap != 0 && (vd->vdev_detached || 3249 vd->vdev_top->vdev_islog)) { 3250 vdev_destroy_unlink_zap(vd, vd->vdev_leaf_zap, tx); 3251 vd->vdev_leaf_zap = 0; 3252 } 3253 3254 dmu_tx_commit(tx); 3255 return; 3256 } 3257 3258 if (vd->vdev_dtl_sm == NULL) { 3259 uint64_t new_object; 3260 3261 new_object = space_map_alloc(mos, zfs_vdev_dtl_sm_blksz, tx); 3262 VERIFY3U(new_object, !=, 0); 3263 3264 VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object, 3265 0, -1ULL, 0)); 3266 ASSERT(vd->vdev_dtl_sm != NULL); 3267 } 3268 3269 rtsync = range_tree_create(NULL, RANGE_SEG64, NULL, 0, 0); 3270 3271 mutex_enter(&vd->vdev_dtl_lock); 3272 range_tree_walk(rt, range_tree_add, rtsync); 3273 mutex_exit(&vd->vdev_dtl_lock); 3274 3275 space_map_truncate(vd->vdev_dtl_sm, zfs_vdev_dtl_sm_blksz, tx); 3276 space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, SM_NO_VDEVID, tx); 3277 range_tree_vacate(rtsync, NULL, NULL); 3278 3279 range_tree_destroy(rtsync); 3280 3281 /* 3282 * If the object for the space map has changed then dirty 3283 * the top level so that we update the config. 3284 */ 3285 if (object != space_map_object(vd->vdev_dtl_sm)) { 3286 vdev_dbgmsg(vd, "txg %llu, spa %s, DTL old object %llu, " 3287 "new object %llu", (u_longlong_t)txg, spa_name(spa), 3288 (u_longlong_t)object, 3289 (u_longlong_t)space_map_object(vd->vdev_dtl_sm)); 3290 vdev_config_dirty(vd->vdev_top); 3291 } 3292 3293 dmu_tx_commit(tx); 3294 } 3295 3296 /* 3297 * Determine whether the specified vdev can be offlined/detached/removed 3298 * without losing data. 3299 */ 3300 boolean_t 3301 vdev_dtl_required(vdev_t *vd) 3302 { 3303 spa_t *spa = vd->vdev_spa; 3304 vdev_t *tvd = vd->vdev_top; 3305 uint8_t cant_read = vd->vdev_cant_read; 3306 boolean_t required; 3307 3308 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 3309 3310 if (vd == spa->spa_root_vdev || vd == tvd) 3311 return (B_TRUE); 3312 3313 /* 3314 * Temporarily mark the device as unreadable, and then determine 3315 * whether this results in any DTL outages in the top-level vdev. 3316 * If not, we can safely offline/detach/remove the device. 3317 */ 3318 vd->vdev_cant_read = B_TRUE; 3319 vdev_dtl_reassess(tvd, 0, 0, B_FALSE, B_FALSE); 3320 required = !vdev_dtl_empty(tvd, DTL_OUTAGE); 3321 vd->vdev_cant_read = cant_read; 3322 vdev_dtl_reassess(tvd, 0, 0, B_FALSE, B_FALSE); 3323 3324 if (!required && zio_injection_enabled) { 3325 required = !!zio_handle_device_injection(vd, NULL, 3326 SET_ERROR(ECHILD)); 3327 } 3328 3329 return (required); 3330 } 3331 3332 /* 3333 * Determine if resilver is needed, and if so the txg range. 3334 */ 3335 boolean_t 3336 vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp) 3337 { 3338 boolean_t needed = B_FALSE; 3339 uint64_t thismin = UINT64_MAX; 3340 uint64_t thismax = 0; 3341 3342 if (vd->vdev_children == 0) { 3343 mutex_enter(&vd->vdev_dtl_lock); 3344 if (!range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) && 3345 vdev_writeable(vd)) { 3346 3347 thismin = vdev_dtl_min(vd); 3348 thismax = vdev_dtl_max(vd); 3349 needed = B_TRUE; 3350 } 3351 mutex_exit(&vd->vdev_dtl_lock); 3352 } else { 3353 for (int c = 0; c < vd->vdev_children; c++) { 3354 vdev_t *cvd = vd->vdev_child[c]; 3355 uint64_t cmin, cmax; 3356 3357 if (vdev_resilver_needed(cvd, &cmin, &cmax)) { 3358 thismin = MIN(thismin, cmin); 3359 thismax = MAX(thismax, cmax); 3360 needed = B_TRUE; 3361 } 3362 } 3363 } 3364 3365 if (needed && minp) { 3366 *minp = thismin; 3367 *maxp = thismax; 3368 } 3369 return (needed); 3370 } 3371 3372 /* 3373 * Gets the checkpoint space map object from the vdev's ZAP. On success sm_obj 3374 * will contain either the checkpoint spacemap object or zero if none exists. 3375 * All other errors are returned to the caller. 3376 */ 3377 int 3378 vdev_checkpoint_sm_object(vdev_t *vd, uint64_t *sm_obj) 3379 { 3380 ASSERT0(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER)); 3381 3382 if (vd->vdev_top_zap == 0) { 3383 *sm_obj = 0; 3384 return (0); 3385 } 3386 3387 int error = zap_lookup(spa_meta_objset(vd->vdev_spa), vd->vdev_top_zap, 3388 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, sizeof (uint64_t), 1, sm_obj); 3389 if (error == ENOENT) { 3390 *sm_obj = 0; 3391 error = 0; 3392 } 3393 3394 return (error); 3395 } 3396 3397 int 3398 vdev_load(vdev_t *vd) 3399 { 3400 int children = vd->vdev_children; 3401 int error = 0; 3402 taskq_t *tq = NULL; 3403 3404 /* 3405 * It's only worthwhile to use the taskq for the root vdev, because the 3406 * slow part is metaslab_init, and that only happens for top-level 3407 * vdevs. 3408 */ 3409 if (vd->vdev_ops == &vdev_root_ops && vd->vdev_children > 0) { 3410 tq = taskq_create("vdev_load", children, minclsyspri, 3411 children, children, TASKQ_PREPOPULATE); 3412 } 3413 3414 /* 3415 * Recursively load all children. 3416 */ 3417 for (int c = 0; c < vd->vdev_children; c++) { 3418 vdev_t *cvd = vd->vdev_child[c]; 3419 3420 if (tq == NULL || vdev_uses_zvols(cvd)) { 3421 cvd->vdev_load_error = vdev_load(cvd); 3422 } else { 3423 VERIFY(taskq_dispatch(tq, vdev_load_child, 3424 cvd, TQ_SLEEP) != TASKQID_INVALID); 3425 } 3426 } 3427 3428 if (tq != NULL) { 3429 taskq_wait(tq); 3430 taskq_destroy(tq); 3431 } 3432 3433 for (int c = 0; c < vd->vdev_children; c++) { 3434 int error = vd->vdev_child[c]->vdev_load_error; 3435 3436 if (error != 0) 3437 return (error); 3438 } 3439 3440 vdev_set_deflate_ratio(vd); 3441 3442 /* 3443 * On spa_load path, grab the allocation bias from our zap 3444 */ 3445 if (vd == vd->vdev_top && vd->vdev_top_zap != 0) { 3446 spa_t *spa = vd->vdev_spa; 3447 char bias_str[64]; 3448 3449 error = zap_lookup(spa->spa_meta_objset, vd->vdev_top_zap, 3450 VDEV_TOP_ZAP_ALLOCATION_BIAS, 1, sizeof (bias_str), 3451 bias_str); 3452 if (error == 0) { 3453 ASSERT(vd->vdev_alloc_bias == VDEV_BIAS_NONE); 3454 vd->vdev_alloc_bias = vdev_derive_alloc_bias(bias_str); 3455 } else if (error != ENOENT) { 3456 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 3457 VDEV_AUX_CORRUPT_DATA); 3458 vdev_dbgmsg(vd, "vdev_load: zap_lookup(top_zap=%llu) " 3459 "failed [error=%d]", vd->vdev_top_zap, error); 3460 return (error); 3461 } 3462 } 3463 3464 /* 3465 * Load any rebuild state from the top-level vdev zap. 3466 */ 3467 if (vd == vd->vdev_top && vd->vdev_top_zap != 0) { 3468 error = vdev_rebuild_load(vd); 3469 if (error && error != ENOTSUP) { 3470 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 3471 VDEV_AUX_CORRUPT_DATA); 3472 vdev_dbgmsg(vd, "vdev_load: vdev_rebuild_load " 3473 "failed [error=%d]", error); 3474 return (error); 3475 } 3476 } 3477 3478 /* 3479 * If this is a top-level vdev, initialize its metaslabs. 3480 */ 3481 if (vd == vd->vdev_top && vdev_is_concrete(vd)) { 3482 vdev_metaslab_group_create(vd); 3483 3484 if (vd->vdev_ashift == 0 || vd->vdev_asize == 0) { 3485 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 3486 VDEV_AUX_CORRUPT_DATA); 3487 vdev_dbgmsg(vd, "vdev_load: invalid size. ashift=%llu, " 3488 "asize=%llu", (u_longlong_t)vd->vdev_ashift, 3489 (u_longlong_t)vd->vdev_asize); 3490 return (SET_ERROR(ENXIO)); 3491 } 3492 3493 error = vdev_metaslab_init(vd, 0); 3494 if (error != 0) { 3495 vdev_dbgmsg(vd, "vdev_load: metaslab_init failed " 3496 "[error=%d]", error); 3497 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 3498 VDEV_AUX_CORRUPT_DATA); 3499 return (error); 3500 } 3501 3502 uint64_t checkpoint_sm_obj; 3503 error = vdev_checkpoint_sm_object(vd, &checkpoint_sm_obj); 3504 if (error == 0 && checkpoint_sm_obj != 0) { 3505 objset_t *mos = spa_meta_objset(vd->vdev_spa); 3506 ASSERT(vd->vdev_asize != 0); 3507 ASSERT3P(vd->vdev_checkpoint_sm, ==, NULL); 3508 3509 error = space_map_open(&vd->vdev_checkpoint_sm, 3510 mos, checkpoint_sm_obj, 0, vd->vdev_asize, 3511 vd->vdev_ashift); 3512 if (error != 0) { 3513 vdev_dbgmsg(vd, "vdev_load: space_map_open " 3514 "failed for checkpoint spacemap (obj %llu) " 3515 "[error=%d]", 3516 (u_longlong_t)checkpoint_sm_obj, error); 3517 return (error); 3518 } 3519 ASSERT3P(vd->vdev_checkpoint_sm, !=, NULL); 3520 3521 /* 3522 * Since the checkpoint_sm contains free entries 3523 * exclusively we can use space_map_allocated() to 3524 * indicate the cumulative checkpointed space that 3525 * has been freed. 3526 */ 3527 vd->vdev_stat.vs_checkpoint_space = 3528 -space_map_allocated(vd->vdev_checkpoint_sm); 3529 vd->vdev_spa->spa_checkpoint_info.sci_dspace += 3530 vd->vdev_stat.vs_checkpoint_space; 3531 } else if (error != 0) { 3532 vdev_dbgmsg(vd, "vdev_load: failed to retrieve " 3533 "checkpoint space map object from vdev ZAP " 3534 "[error=%d]", error); 3535 return (error); 3536 } 3537 } 3538 3539 /* 3540 * If this is a leaf vdev, load its DTL. 3541 */ 3542 if (vd->vdev_ops->vdev_op_leaf && (error = vdev_dtl_load(vd)) != 0) { 3543 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 3544 VDEV_AUX_CORRUPT_DATA); 3545 vdev_dbgmsg(vd, "vdev_load: vdev_dtl_load failed " 3546 "[error=%d]", error); 3547 return (error); 3548 } 3549 3550 uint64_t obsolete_sm_object; 3551 error = vdev_obsolete_sm_object(vd, &obsolete_sm_object); 3552 if (error == 0 && obsolete_sm_object != 0) { 3553 objset_t *mos = vd->vdev_spa->spa_meta_objset; 3554 ASSERT(vd->vdev_asize != 0); 3555 ASSERT3P(vd->vdev_obsolete_sm, ==, NULL); 3556 3557 if ((error = space_map_open(&vd->vdev_obsolete_sm, mos, 3558 obsolete_sm_object, 0, vd->vdev_asize, 0))) { 3559 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 3560 VDEV_AUX_CORRUPT_DATA); 3561 vdev_dbgmsg(vd, "vdev_load: space_map_open failed for " 3562 "obsolete spacemap (obj %llu) [error=%d]", 3563 (u_longlong_t)obsolete_sm_object, error); 3564 return (error); 3565 } 3566 } else if (error != 0) { 3567 vdev_dbgmsg(vd, "vdev_load: failed to retrieve obsolete " 3568 "space map object from vdev ZAP [error=%d]", error); 3569 return (error); 3570 } 3571 3572 return (0); 3573 } 3574 3575 /* 3576 * The special vdev case is used for hot spares and l2cache devices. Its 3577 * sole purpose it to set the vdev state for the associated vdev. To do this, 3578 * we make sure that we can open the underlying device, then try to read the 3579 * label, and make sure that the label is sane and that it hasn't been 3580 * repurposed to another pool. 3581 */ 3582 int 3583 vdev_validate_aux(vdev_t *vd) 3584 { 3585 nvlist_t *label; 3586 uint64_t guid, version; 3587 uint64_t state; 3588 3589 if (!vdev_readable(vd)) 3590 return (0); 3591 3592 if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) { 3593 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 3594 VDEV_AUX_CORRUPT_DATA); 3595 return (-1); 3596 } 3597 3598 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 || 3599 !SPA_VERSION_IS_SUPPORTED(version) || 3600 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 || 3601 guid != vd->vdev_guid || 3602 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) { 3603 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 3604 VDEV_AUX_CORRUPT_DATA); 3605 nvlist_free(label); 3606 return (-1); 3607 } 3608 3609 /* 3610 * We don't actually check the pool state here. If it's in fact in 3611 * use by another pool, we update this fact on the fly when requested. 3612 */ 3613 nvlist_free(label); 3614 return (0); 3615 } 3616 3617 static void 3618 vdev_destroy_ms_flush_data(vdev_t *vd, dmu_tx_t *tx) 3619 { 3620 objset_t *mos = spa_meta_objset(vd->vdev_spa); 3621 3622 if (vd->vdev_top_zap == 0) 3623 return; 3624 3625 uint64_t object = 0; 3626 int err = zap_lookup(mos, vd->vdev_top_zap, 3627 VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS, sizeof (uint64_t), 1, &object); 3628 if (err == ENOENT) 3629 return; 3630 VERIFY0(err); 3631 3632 VERIFY0(dmu_object_free(mos, object, tx)); 3633 VERIFY0(zap_remove(mos, vd->vdev_top_zap, 3634 VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS, tx)); 3635 } 3636 3637 /* 3638 * Free the objects used to store this vdev's spacemaps, and the array 3639 * that points to them. 3640 */ 3641 void 3642 vdev_destroy_spacemaps(vdev_t *vd, dmu_tx_t *tx) 3643 { 3644 if (vd->vdev_ms_array == 0) 3645 return; 3646 3647 objset_t *mos = vd->vdev_spa->spa_meta_objset; 3648 uint64_t array_count = vd->vdev_asize >> vd->vdev_ms_shift; 3649 size_t array_bytes = array_count * sizeof (uint64_t); 3650 uint64_t *smobj_array = kmem_alloc(array_bytes, KM_SLEEP); 3651 VERIFY0(dmu_read(mos, vd->vdev_ms_array, 0, 3652 array_bytes, smobj_array, 0)); 3653 3654 for (uint64_t i = 0; i < array_count; i++) { 3655 uint64_t smobj = smobj_array[i]; 3656 if (smobj == 0) 3657 continue; 3658 3659 space_map_free_obj(mos, smobj, tx); 3660 } 3661 3662 kmem_free(smobj_array, array_bytes); 3663 VERIFY0(dmu_object_free(mos, vd->vdev_ms_array, tx)); 3664 vdev_destroy_ms_flush_data(vd, tx); 3665 vd->vdev_ms_array = 0; 3666 } 3667 3668 static void 3669 vdev_remove_empty_log(vdev_t *vd, uint64_t txg) 3670 { 3671 spa_t *spa = vd->vdev_spa; 3672 3673 ASSERT(vd->vdev_islog); 3674 ASSERT(vd == vd->vdev_top); 3675 ASSERT3U(txg, ==, spa_syncing_txg(spa)); 3676 3677 dmu_tx_t *tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg); 3678 3679 vdev_destroy_spacemaps(vd, tx); 3680 if (vd->vdev_top_zap != 0) { 3681 vdev_destroy_unlink_zap(vd, vd->vdev_top_zap, tx); 3682 vd->vdev_top_zap = 0; 3683 } 3684 3685 dmu_tx_commit(tx); 3686 } 3687 3688 void 3689 vdev_sync_done(vdev_t *vd, uint64_t txg) 3690 { 3691 metaslab_t *msp; 3692 boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg)); 3693 3694 ASSERT(vdev_is_concrete(vd)); 3695 3696 while ((msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg))) 3697 != NULL) 3698 metaslab_sync_done(msp, txg); 3699 3700 if (reassess) { 3701 metaslab_sync_reassess(vd->vdev_mg); 3702 if (vd->vdev_log_mg != NULL) 3703 metaslab_sync_reassess(vd->vdev_log_mg); 3704 } 3705 } 3706 3707 void 3708 vdev_sync(vdev_t *vd, uint64_t txg) 3709 { 3710 spa_t *spa = vd->vdev_spa; 3711 vdev_t *lvd; 3712 metaslab_t *msp; 3713 3714 ASSERT3U(txg, ==, spa->spa_syncing_txg); 3715 dmu_tx_t *tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 3716 if (range_tree_space(vd->vdev_obsolete_segments) > 0) { 3717 ASSERT(vd->vdev_removing || 3718 vd->vdev_ops == &vdev_indirect_ops); 3719 3720 vdev_indirect_sync_obsolete(vd, tx); 3721 3722 /* 3723 * If the vdev is indirect, it can't have dirty 3724 * metaslabs or DTLs. 3725 */ 3726 if (vd->vdev_ops == &vdev_indirect_ops) { 3727 ASSERT(txg_list_empty(&vd->vdev_ms_list, txg)); 3728 ASSERT(txg_list_empty(&vd->vdev_dtl_list, txg)); 3729 dmu_tx_commit(tx); 3730 return; 3731 } 3732 } 3733 3734 ASSERT(vdev_is_concrete(vd)); 3735 3736 if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0 && 3737 !vd->vdev_removing) { 3738 ASSERT(vd == vd->vdev_top); 3739 ASSERT0(vd->vdev_indirect_config.vic_mapping_object); 3740 vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset, 3741 DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx); 3742 ASSERT(vd->vdev_ms_array != 0); 3743 vdev_config_dirty(vd); 3744 } 3745 3746 while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) { 3747 metaslab_sync(msp, txg); 3748 (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg)); 3749 } 3750 3751 while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL) 3752 vdev_dtl_sync(lvd, txg); 3753 3754 /* 3755 * If this is an empty log device being removed, destroy the 3756 * metadata associated with it. 3757 */ 3758 if (vd->vdev_islog && vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing) 3759 vdev_remove_empty_log(vd, txg); 3760 3761 (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)); 3762 dmu_tx_commit(tx); 3763 } 3764 3765 uint64_t 3766 vdev_psize_to_asize(vdev_t *vd, uint64_t psize) 3767 { 3768 return (vd->vdev_ops->vdev_op_asize(vd, psize)); 3769 } 3770 3771 /* 3772 * Mark the given vdev faulted. A faulted vdev behaves as if the device could 3773 * not be opened, and no I/O is attempted. 3774 */ 3775 int 3776 vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux) 3777 { 3778 vdev_t *vd, *tvd; 3779 3780 spa_vdev_state_enter(spa, SCL_NONE); 3781 3782 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3783 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV))); 3784 3785 if (!vd->vdev_ops->vdev_op_leaf) 3786 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENOTSUP))); 3787 3788 tvd = vd->vdev_top; 3789 3790 /* 3791 * If user did a 'zpool offline -f' then make the fault persist across 3792 * reboots. 3793 */ 3794 if (aux == VDEV_AUX_EXTERNAL_PERSIST) { 3795 /* 3796 * There are two kinds of forced faults: temporary and 3797 * persistent. Temporary faults go away at pool import, while 3798 * persistent faults stay set. Both types of faults can be 3799 * cleared with a zpool clear. 3800 * 3801 * We tell if a vdev is persistently faulted by looking at the 3802 * ZPOOL_CONFIG_AUX_STATE nvpair. If it's set to "external" at 3803 * import then it's a persistent fault. Otherwise, it's 3804 * temporary. We get ZPOOL_CONFIG_AUX_STATE set to "external" 3805 * by setting vd.vdev_stat.vs_aux to VDEV_AUX_EXTERNAL. This 3806 * tells vdev_config_generate() (which gets run later) to set 3807 * ZPOOL_CONFIG_AUX_STATE to "external" in the nvlist. 3808 */ 3809 vd->vdev_stat.vs_aux = VDEV_AUX_EXTERNAL; 3810 vd->vdev_tmpoffline = B_FALSE; 3811 aux = VDEV_AUX_EXTERNAL; 3812 } else { 3813 vd->vdev_tmpoffline = B_TRUE; 3814 } 3815 3816 /* 3817 * We don't directly use the aux state here, but if we do a 3818 * vdev_reopen(), we need this value to be present to remember why we 3819 * were faulted. 3820 */ 3821 vd->vdev_label_aux = aux; 3822 3823 /* 3824 * Faulted state takes precedence over degraded. 3825 */ 3826 vd->vdev_delayed_close = B_FALSE; 3827 vd->vdev_faulted = 1ULL; 3828 vd->vdev_degraded = 0ULL; 3829 vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux); 3830 3831 /* 3832 * If this device has the only valid copy of the data, then 3833 * back off and simply mark the vdev as degraded instead. 3834 */ 3835 if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) { 3836 vd->vdev_degraded = 1ULL; 3837 vd->vdev_faulted = 0ULL; 3838 3839 /* 3840 * If we reopen the device and it's not dead, only then do we 3841 * mark it degraded. 3842 */ 3843 vdev_reopen(tvd); 3844 3845 if (vdev_readable(vd)) 3846 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux); 3847 } 3848 3849 return (spa_vdev_state_exit(spa, vd, 0)); 3850 } 3851 3852 /* 3853 * Mark the given vdev degraded. A degraded vdev is purely an indication to the 3854 * user that something is wrong. The vdev continues to operate as normal as far 3855 * as I/O is concerned. 3856 */ 3857 int 3858 vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux) 3859 { 3860 vdev_t *vd; 3861 3862 spa_vdev_state_enter(spa, SCL_NONE); 3863 3864 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3865 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV))); 3866 3867 if (!vd->vdev_ops->vdev_op_leaf) 3868 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENOTSUP))); 3869 3870 /* 3871 * If the vdev is already faulted, then don't do anything. 3872 */ 3873 if (vd->vdev_faulted || vd->vdev_degraded) 3874 return (spa_vdev_state_exit(spa, NULL, 0)); 3875 3876 vd->vdev_degraded = 1ULL; 3877 if (!vdev_is_dead(vd)) 3878 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, 3879 aux); 3880 3881 return (spa_vdev_state_exit(spa, vd, 0)); 3882 } 3883 3884 /* 3885 * Online the given vdev. 3886 * 3887 * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached 3888 * spare device should be detached when the device finishes resilvering. 3889 * Second, the online should be treated like a 'test' online case, so no FMA 3890 * events are generated if the device fails to open. 3891 */ 3892 int 3893 vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate) 3894 { 3895 vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev; 3896 boolean_t wasoffline; 3897 vdev_state_t oldstate; 3898 3899 spa_vdev_state_enter(spa, SCL_NONE); 3900 3901 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3902 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV))); 3903 3904 if (!vd->vdev_ops->vdev_op_leaf) 3905 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENOTSUP))); 3906 3907 wasoffline = (vd->vdev_offline || vd->vdev_tmpoffline); 3908 oldstate = vd->vdev_state; 3909 3910 tvd = vd->vdev_top; 3911 vd->vdev_offline = B_FALSE; 3912 vd->vdev_tmpoffline = B_FALSE; 3913 vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE); 3914 vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT); 3915 3916 /* XXX - L2ARC 1.0 does not support expansion */ 3917 if (!vd->vdev_aux) { 3918 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 3919 pvd->vdev_expanding = !!((flags & ZFS_ONLINE_EXPAND) || 3920 spa->spa_autoexpand); 3921 vd->vdev_expansion_time = gethrestime_sec(); 3922 } 3923 3924 vdev_reopen(tvd); 3925 vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE; 3926 3927 if (!vd->vdev_aux) { 3928 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 3929 pvd->vdev_expanding = B_FALSE; 3930 } 3931 3932 if (newstate) 3933 *newstate = vd->vdev_state; 3934 if ((flags & ZFS_ONLINE_UNSPARE) && 3935 !vdev_is_dead(vd) && vd->vdev_parent && 3936 vd->vdev_parent->vdev_ops == &vdev_spare_ops && 3937 vd->vdev_parent->vdev_child[0] == vd) 3938 vd->vdev_unspare = B_TRUE; 3939 3940 if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) { 3941 3942 /* XXX - L2ARC 1.0 does not support expansion */ 3943 if (vd->vdev_aux) 3944 return (spa_vdev_state_exit(spa, vd, ENOTSUP)); 3945 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 3946 } 3947 3948 /* Restart initializing if necessary */ 3949 mutex_enter(&vd->vdev_initialize_lock); 3950 if (vdev_writeable(vd) && 3951 vd->vdev_initialize_thread == NULL && 3952 vd->vdev_initialize_state == VDEV_INITIALIZE_ACTIVE) { 3953 (void) vdev_initialize(vd); 3954 } 3955 mutex_exit(&vd->vdev_initialize_lock); 3956 3957 /* 3958 * Restart trimming if necessary. We do not restart trimming for cache 3959 * devices here. This is triggered by l2arc_rebuild_vdev() 3960 * asynchronously for the whole device or in l2arc_evict() as it evicts 3961 * space for upcoming writes. 3962 */ 3963 mutex_enter(&vd->vdev_trim_lock); 3964 if (vdev_writeable(vd) && !vd->vdev_isl2cache && 3965 vd->vdev_trim_thread == NULL && 3966 vd->vdev_trim_state == VDEV_TRIM_ACTIVE) { 3967 (void) vdev_trim(vd, vd->vdev_trim_rate, vd->vdev_trim_partial, 3968 vd->vdev_trim_secure); 3969 } 3970 mutex_exit(&vd->vdev_trim_lock); 3971 3972 if (wasoffline || 3973 (oldstate < VDEV_STATE_DEGRADED && 3974 vd->vdev_state >= VDEV_STATE_DEGRADED)) 3975 spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_ONLINE); 3976 3977 return (spa_vdev_state_exit(spa, vd, 0)); 3978 } 3979 3980 static int 3981 vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags) 3982 { 3983 vdev_t *vd, *tvd; 3984 int error = 0; 3985 uint64_t generation; 3986 metaslab_group_t *mg; 3987 3988 top: 3989 spa_vdev_state_enter(spa, SCL_ALLOC); 3990 3991 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3992 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV))); 3993 3994 if (!vd->vdev_ops->vdev_op_leaf) 3995 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENOTSUP))); 3996 3997 if (vd->vdev_ops == &vdev_draid_spare_ops) 3998 return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 3999 4000 tvd = vd->vdev_top; 4001 mg = tvd->vdev_mg; 4002 generation = spa->spa_config_generation + 1; 4003 4004 /* 4005 * If the device isn't already offline, try to offline it. 4006 */ 4007 if (!vd->vdev_offline) { 4008 /* 4009 * If this device has the only valid copy of some data, 4010 * don't allow it to be offlined. Log devices are always 4011 * expendable. 4012 */ 4013 if (!tvd->vdev_islog && vd->vdev_aux == NULL && 4014 vdev_dtl_required(vd)) 4015 return (spa_vdev_state_exit(spa, NULL, 4016 SET_ERROR(EBUSY))); 4017 4018 /* 4019 * If the top-level is a slog and it has had allocations 4020 * then proceed. We check that the vdev's metaslab group 4021 * is not NULL since it's possible that we may have just 4022 * added this vdev but not yet initialized its metaslabs. 4023 */ 4024 if (tvd->vdev_islog && mg != NULL) { 4025 /* 4026 * Prevent any future allocations. 4027 */ 4028 ASSERT3P(tvd->vdev_log_mg, ==, NULL); 4029 metaslab_group_passivate(mg); 4030 (void) spa_vdev_state_exit(spa, vd, 0); 4031 4032 error = spa_reset_logs(spa); 4033 4034 /* 4035 * If the log device was successfully reset but has 4036 * checkpointed data, do not offline it. 4037 */ 4038 if (error == 0 && 4039 tvd->vdev_checkpoint_sm != NULL) { 4040 ASSERT3U(space_map_allocated( 4041 tvd->vdev_checkpoint_sm), !=, 0); 4042 error = ZFS_ERR_CHECKPOINT_EXISTS; 4043 } 4044 4045 spa_vdev_state_enter(spa, SCL_ALLOC); 4046 4047 /* 4048 * Check to see if the config has changed. 4049 */ 4050 if (error || generation != spa->spa_config_generation) { 4051 metaslab_group_activate(mg); 4052 if (error) 4053 return (spa_vdev_state_exit(spa, 4054 vd, error)); 4055 (void) spa_vdev_state_exit(spa, vd, 0); 4056 goto top; 4057 } 4058 ASSERT0(tvd->vdev_stat.vs_alloc); 4059 } 4060 4061 /* 4062 * Offline this device and reopen its top-level vdev. 4063 * If the top-level vdev is a log device then just offline 4064 * it. Otherwise, if this action results in the top-level 4065 * vdev becoming unusable, undo it and fail the request. 4066 */ 4067 vd->vdev_offline = B_TRUE; 4068 vdev_reopen(tvd); 4069 4070 if (!tvd->vdev_islog && vd->vdev_aux == NULL && 4071 vdev_is_dead(tvd)) { 4072 vd->vdev_offline = B_FALSE; 4073 vdev_reopen(tvd); 4074 return (spa_vdev_state_exit(spa, NULL, 4075 SET_ERROR(EBUSY))); 4076 } 4077 4078 /* 4079 * Add the device back into the metaslab rotor so that 4080 * once we online the device it's open for business. 4081 */ 4082 if (tvd->vdev_islog && mg != NULL) 4083 metaslab_group_activate(mg); 4084 } 4085 4086 vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY); 4087 4088 return (spa_vdev_state_exit(spa, vd, 0)); 4089 } 4090 4091 int 4092 vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags) 4093 { 4094 int error; 4095 4096 mutex_enter(&spa->spa_vdev_top_lock); 4097 error = vdev_offline_locked(spa, guid, flags); 4098 mutex_exit(&spa->spa_vdev_top_lock); 4099 4100 return (error); 4101 } 4102 4103 /* 4104 * Clear the error counts associated with this vdev. Unlike vdev_online() and 4105 * vdev_offline(), we assume the spa config is locked. We also clear all 4106 * children. If 'vd' is NULL, then the user wants to clear all vdevs. 4107 */ 4108 void 4109 vdev_clear(spa_t *spa, vdev_t *vd) 4110 { 4111 vdev_t *rvd = spa->spa_root_vdev; 4112 4113 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 4114 4115 if (vd == NULL) 4116 vd = rvd; 4117 4118 vd->vdev_stat.vs_read_errors = 0; 4119 vd->vdev_stat.vs_write_errors = 0; 4120 vd->vdev_stat.vs_checksum_errors = 0; 4121 vd->vdev_stat.vs_slow_ios = 0; 4122 4123 for (int c = 0; c < vd->vdev_children; c++) 4124 vdev_clear(spa, vd->vdev_child[c]); 4125 4126 /* 4127 * It makes no sense to "clear" an indirect vdev. 4128 */ 4129 if (!vdev_is_concrete(vd)) 4130 return; 4131 4132 /* 4133 * If we're in the FAULTED state or have experienced failed I/O, then 4134 * clear the persistent state and attempt to reopen the device. We 4135 * also mark the vdev config dirty, so that the new faulted state is 4136 * written out to disk. 4137 */ 4138 if (vd->vdev_faulted || vd->vdev_degraded || 4139 !vdev_readable(vd) || !vdev_writeable(vd)) { 4140 /* 4141 * When reopening in response to a clear event, it may be due to 4142 * a fmadm repair request. In this case, if the device is 4143 * still broken, we want to still post the ereport again. 4144 */ 4145 vd->vdev_forcefault = B_TRUE; 4146 4147 vd->vdev_faulted = vd->vdev_degraded = 0ULL; 4148 vd->vdev_cant_read = B_FALSE; 4149 vd->vdev_cant_write = B_FALSE; 4150 vd->vdev_stat.vs_aux = 0; 4151 4152 vdev_reopen(vd == rvd ? rvd : vd->vdev_top); 4153 4154 vd->vdev_forcefault = B_FALSE; 4155 4156 if (vd != rvd && vdev_writeable(vd->vdev_top)) 4157 vdev_state_dirty(vd->vdev_top); 4158 4159 /* If a resilver isn't required, check if vdevs can be culled */ 4160 if (vd->vdev_aux == NULL && !vdev_is_dead(vd) && 4161 !dsl_scan_resilvering(spa->spa_dsl_pool) && 4162 !dsl_scan_resilver_scheduled(spa->spa_dsl_pool)) 4163 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 4164 4165 spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_CLEAR); 4166 } 4167 4168 /* 4169 * When clearing a FMA-diagnosed fault, we always want to 4170 * unspare the device, as we assume that the original spare was 4171 * done in response to the FMA fault. 4172 */ 4173 if (!vdev_is_dead(vd) && vd->vdev_parent != NULL && 4174 vd->vdev_parent->vdev_ops == &vdev_spare_ops && 4175 vd->vdev_parent->vdev_child[0] == vd) 4176 vd->vdev_unspare = B_TRUE; 4177 4178 /* Clear recent error events cache (i.e. duplicate events tracking) */ 4179 zfs_ereport_clear(spa, vd); 4180 } 4181 4182 boolean_t 4183 vdev_is_dead(vdev_t *vd) 4184 { 4185 /* 4186 * Holes and missing devices are always considered "dead". 4187 * This simplifies the code since we don't have to check for 4188 * these types of devices in the various code paths. 4189 * Instead we rely on the fact that we skip over dead devices 4190 * before issuing I/O to them. 4191 */ 4192 return (vd->vdev_state < VDEV_STATE_DEGRADED || 4193 vd->vdev_ops == &vdev_hole_ops || 4194 vd->vdev_ops == &vdev_missing_ops); 4195 } 4196 4197 boolean_t 4198 vdev_readable(vdev_t *vd) 4199 { 4200 return (!vdev_is_dead(vd) && !vd->vdev_cant_read); 4201 } 4202 4203 boolean_t 4204 vdev_writeable(vdev_t *vd) 4205 { 4206 return (!vdev_is_dead(vd) && !vd->vdev_cant_write && 4207 vdev_is_concrete(vd)); 4208 } 4209 4210 boolean_t 4211 vdev_allocatable(vdev_t *vd) 4212 { 4213 uint64_t state = vd->vdev_state; 4214 4215 /* 4216 * We currently allow allocations from vdevs which may be in the 4217 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device 4218 * fails to reopen then we'll catch it later when we're holding 4219 * the proper locks. Note that we have to get the vdev state 4220 * in a local variable because although it changes atomically, 4221 * we're asking two separate questions about it. 4222 */ 4223 return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) && 4224 !vd->vdev_cant_write && vdev_is_concrete(vd) && 4225 vd->vdev_mg->mg_initialized); 4226 } 4227 4228 boolean_t 4229 vdev_accessible(vdev_t *vd, zio_t *zio) 4230 { 4231 ASSERT(zio->io_vd == vd); 4232 4233 if (vdev_is_dead(vd) || vd->vdev_remove_wanted) 4234 return (B_FALSE); 4235 4236 if (zio->io_type == ZIO_TYPE_READ) 4237 return (!vd->vdev_cant_read); 4238 4239 if (zio->io_type == ZIO_TYPE_WRITE) 4240 return (!vd->vdev_cant_write); 4241 4242 return (B_TRUE); 4243 } 4244 4245 static void 4246 vdev_get_child_stat(vdev_t *cvd, vdev_stat_t *vs, vdev_stat_t *cvs) 4247 { 4248 /* 4249 * Exclude the dRAID spare when aggregating to avoid double counting 4250 * the ops and bytes. These IOs are counted by the physical leaves. 4251 */ 4252 if (cvd->vdev_ops == &vdev_draid_spare_ops) 4253 return; 4254 4255 for (int t = 0; t < VS_ZIO_TYPES; t++) { 4256 vs->vs_ops[t] += cvs->vs_ops[t]; 4257 vs->vs_bytes[t] += cvs->vs_bytes[t]; 4258 } 4259 4260 cvs->vs_scan_removing = cvd->vdev_removing; 4261 } 4262 4263 /* 4264 * Get extended stats 4265 */ 4266 static void 4267 vdev_get_child_stat_ex(vdev_t *cvd, vdev_stat_ex_t *vsx, vdev_stat_ex_t *cvsx) 4268 { 4269 int t, b; 4270 for (t = 0; t < ZIO_TYPES; t++) { 4271 for (b = 0; b < ARRAY_SIZE(vsx->vsx_disk_histo[0]); b++) 4272 vsx->vsx_disk_histo[t][b] += cvsx->vsx_disk_histo[t][b]; 4273 4274 for (b = 0; b < ARRAY_SIZE(vsx->vsx_total_histo[0]); b++) { 4275 vsx->vsx_total_histo[t][b] += 4276 cvsx->vsx_total_histo[t][b]; 4277 } 4278 } 4279 4280 for (t = 0; t < ZIO_PRIORITY_NUM_QUEUEABLE; t++) { 4281 for (b = 0; b < ARRAY_SIZE(vsx->vsx_queue_histo[0]); b++) { 4282 vsx->vsx_queue_histo[t][b] += 4283 cvsx->vsx_queue_histo[t][b]; 4284 } 4285 vsx->vsx_active_queue[t] += cvsx->vsx_active_queue[t]; 4286 vsx->vsx_pend_queue[t] += cvsx->vsx_pend_queue[t]; 4287 4288 for (b = 0; b < ARRAY_SIZE(vsx->vsx_ind_histo[0]); b++) 4289 vsx->vsx_ind_histo[t][b] += cvsx->vsx_ind_histo[t][b]; 4290 4291 for (b = 0; b < ARRAY_SIZE(vsx->vsx_agg_histo[0]); b++) 4292 vsx->vsx_agg_histo[t][b] += cvsx->vsx_agg_histo[t][b]; 4293 } 4294 4295 } 4296 4297 boolean_t 4298 vdev_is_spacemap_addressable(vdev_t *vd) 4299 { 4300 if (spa_feature_is_active(vd->vdev_spa, SPA_FEATURE_SPACEMAP_V2)) 4301 return (B_TRUE); 4302 4303 /* 4304 * If double-word space map entries are not enabled we assume 4305 * 47 bits of the space map entry are dedicated to the entry's 4306 * offset (see SM_OFFSET_BITS in space_map.h). We then use that 4307 * to calculate the maximum address that can be described by a 4308 * space map entry for the given device. 4309 */ 4310 uint64_t shift = vd->vdev_ashift + SM_OFFSET_BITS; 4311 4312 if (shift >= 63) /* detect potential overflow */ 4313 return (B_TRUE); 4314 4315 return (vd->vdev_asize < (1ULL << shift)); 4316 } 4317 4318 /* 4319 * Get statistics for the given vdev. 4320 */ 4321 static void 4322 vdev_get_stats_ex_impl(vdev_t *vd, vdev_stat_t *vs, vdev_stat_ex_t *vsx) 4323 { 4324 int t; 4325 /* 4326 * If we're getting stats on the root vdev, aggregate the I/O counts 4327 * over all top-level vdevs (i.e. the direct children of the root). 4328 */ 4329 if (!vd->vdev_ops->vdev_op_leaf) { 4330 if (vs) { 4331 memset(vs->vs_ops, 0, sizeof (vs->vs_ops)); 4332 memset(vs->vs_bytes, 0, sizeof (vs->vs_bytes)); 4333 } 4334 if (vsx) 4335 memset(vsx, 0, sizeof (*vsx)); 4336 4337 for (int c = 0; c < vd->vdev_children; c++) { 4338 vdev_t *cvd = vd->vdev_child[c]; 4339 vdev_stat_t *cvs = &cvd->vdev_stat; 4340 vdev_stat_ex_t *cvsx = &cvd->vdev_stat_ex; 4341 4342 vdev_get_stats_ex_impl(cvd, cvs, cvsx); 4343 if (vs) 4344 vdev_get_child_stat(cvd, vs, cvs); 4345 if (vsx) 4346 vdev_get_child_stat_ex(cvd, vsx, cvsx); 4347 } 4348 } else { 4349 /* 4350 * We're a leaf. Just copy our ZIO active queue stats in. The 4351 * other leaf stats are updated in vdev_stat_update(). 4352 */ 4353 if (!vsx) 4354 return; 4355 4356 memcpy(vsx, &vd->vdev_stat_ex, sizeof (vd->vdev_stat_ex)); 4357 4358 for (t = 0; t < ARRAY_SIZE(vd->vdev_queue.vq_class); t++) { 4359 vsx->vsx_active_queue[t] = 4360 vd->vdev_queue.vq_class[t].vqc_active; 4361 vsx->vsx_pend_queue[t] = avl_numnodes( 4362 &vd->vdev_queue.vq_class[t].vqc_queued_tree); 4363 } 4364 } 4365 } 4366 4367 void 4368 vdev_get_stats_ex(vdev_t *vd, vdev_stat_t *vs, vdev_stat_ex_t *vsx) 4369 { 4370 vdev_t *tvd = vd->vdev_top; 4371 mutex_enter(&vd->vdev_stat_lock); 4372 if (vs) { 4373 bcopy(&vd->vdev_stat, vs, sizeof (*vs)); 4374 vs->vs_timestamp = gethrtime() - vs->vs_timestamp; 4375 vs->vs_state = vd->vdev_state; 4376 vs->vs_rsize = vdev_get_min_asize(vd); 4377 4378 if (vd->vdev_ops->vdev_op_leaf) { 4379 vs->vs_rsize += VDEV_LABEL_START_SIZE + 4380 VDEV_LABEL_END_SIZE; 4381 /* 4382 * Report initializing progress. Since we don't 4383 * have the initializing locks held, this is only 4384 * an estimate (although a fairly accurate one). 4385 */ 4386 vs->vs_initialize_bytes_done = 4387 vd->vdev_initialize_bytes_done; 4388 vs->vs_initialize_bytes_est = 4389 vd->vdev_initialize_bytes_est; 4390 vs->vs_initialize_state = vd->vdev_initialize_state; 4391 vs->vs_initialize_action_time = 4392 vd->vdev_initialize_action_time; 4393 4394 /* 4395 * Report manual TRIM progress. Since we don't have 4396 * the manual TRIM locks held, this is only an 4397 * estimate (although fairly accurate one). 4398 */ 4399 vs->vs_trim_notsup = !vd->vdev_has_trim; 4400 vs->vs_trim_bytes_done = vd->vdev_trim_bytes_done; 4401 vs->vs_trim_bytes_est = vd->vdev_trim_bytes_est; 4402 vs->vs_trim_state = vd->vdev_trim_state; 4403 vs->vs_trim_action_time = vd->vdev_trim_action_time; 4404 4405 /* Set when there is a deferred resilver. */ 4406 vs->vs_resilver_deferred = vd->vdev_resilver_deferred; 4407 } 4408 4409 /* 4410 * Report expandable space on top-level, non-auxiliary devices 4411 * only. The expandable space is reported in terms of metaslab 4412 * sized units since that determines how much space the pool 4413 * can expand. 4414 */ 4415 if (vd->vdev_aux == NULL && tvd != NULL) { 4416 vs->vs_esize = P2ALIGN( 4417 vd->vdev_max_asize - vd->vdev_asize, 4418 1ULL << tvd->vdev_ms_shift); 4419 } 4420 4421 vs->vs_configured_ashift = vd->vdev_top != NULL 4422 ? vd->vdev_top->vdev_ashift : vd->vdev_ashift; 4423 vs->vs_logical_ashift = vd->vdev_logical_ashift; 4424 vs->vs_physical_ashift = vd->vdev_physical_ashift; 4425 4426 /* 4427 * Report fragmentation and rebuild progress for top-level, 4428 * non-auxiliary, concrete devices. 4429 */ 4430 if (vd->vdev_aux == NULL && vd == vd->vdev_top && 4431 vdev_is_concrete(vd)) { 4432 /* 4433 * The vdev fragmentation rating doesn't take into 4434 * account the embedded slog metaslab (vdev_log_mg). 4435 * Since it's only one metaslab, it would have a tiny 4436 * impact on the overall fragmentation. 4437 */ 4438 vs->vs_fragmentation = (vd->vdev_mg != NULL) ? 4439 vd->vdev_mg->mg_fragmentation : 0; 4440 } 4441 } 4442 4443 vdev_get_stats_ex_impl(vd, vs, vsx); 4444 mutex_exit(&vd->vdev_stat_lock); 4445 } 4446 4447 void 4448 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs) 4449 { 4450 return (vdev_get_stats_ex(vd, vs, NULL)); 4451 } 4452 4453 void 4454 vdev_clear_stats(vdev_t *vd) 4455 { 4456 mutex_enter(&vd->vdev_stat_lock); 4457 vd->vdev_stat.vs_space = 0; 4458 vd->vdev_stat.vs_dspace = 0; 4459 vd->vdev_stat.vs_alloc = 0; 4460 mutex_exit(&vd->vdev_stat_lock); 4461 } 4462 4463 void 4464 vdev_scan_stat_init(vdev_t *vd) 4465 { 4466 vdev_stat_t *vs = &vd->vdev_stat; 4467 4468 for (int c = 0; c < vd->vdev_children; c++) 4469 vdev_scan_stat_init(vd->vdev_child[c]); 4470 4471 mutex_enter(&vd->vdev_stat_lock); 4472 vs->vs_scan_processed = 0; 4473 mutex_exit(&vd->vdev_stat_lock); 4474 } 4475 4476 void 4477 vdev_stat_update(zio_t *zio, uint64_t psize) 4478 { 4479 spa_t *spa = zio->io_spa; 4480 vdev_t *rvd = spa->spa_root_vdev; 4481 vdev_t *vd = zio->io_vd ? zio->io_vd : rvd; 4482 vdev_t *pvd; 4483 uint64_t txg = zio->io_txg; 4484 vdev_stat_t *vs = &vd->vdev_stat; 4485 vdev_stat_ex_t *vsx = &vd->vdev_stat_ex; 4486 zio_type_t type = zio->io_type; 4487 int flags = zio->io_flags; 4488 4489 /* 4490 * If this i/o is a gang leader, it didn't do any actual work. 4491 */ 4492 if (zio->io_gang_tree) 4493 return; 4494 4495 if (zio->io_error == 0) { 4496 /* 4497 * If this is a root i/o, don't count it -- we've already 4498 * counted the top-level vdevs, and vdev_get_stats() will 4499 * aggregate them when asked. This reduces contention on 4500 * the root vdev_stat_lock and implicitly handles blocks 4501 * that compress away to holes, for which there is no i/o. 4502 * (Holes never create vdev children, so all the counters 4503 * remain zero, which is what we want.) 4504 * 4505 * Note: this only applies to successful i/o (io_error == 0) 4506 * because unlike i/o counts, errors are not additive. 4507 * When reading a ditto block, for example, failure of 4508 * one top-level vdev does not imply a root-level error. 4509 */ 4510 if (vd == rvd) 4511 return; 4512 4513 ASSERT(vd == zio->io_vd); 4514 4515 if (flags & ZIO_FLAG_IO_BYPASS) 4516 return; 4517 4518 mutex_enter(&vd->vdev_stat_lock); 4519 4520 if (flags & ZIO_FLAG_IO_REPAIR) { 4521 /* 4522 * Repair is the result of a resilver issued by the 4523 * scan thread (spa_sync). 4524 */ 4525 if (flags & ZIO_FLAG_SCAN_THREAD) { 4526 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; 4527 dsl_scan_phys_t *scn_phys = &scn->scn_phys; 4528 uint64_t *processed = &scn_phys->scn_processed; 4529 4530 if (vd->vdev_ops->vdev_op_leaf) 4531 atomic_add_64(processed, psize); 4532 vs->vs_scan_processed += psize; 4533 } 4534 4535 /* 4536 * Repair is the result of a rebuild issued by the 4537 * rebuild thread (vdev_rebuild_thread). To avoid 4538 * double counting repaired bytes the virtual dRAID 4539 * spare vdev is excluded from the processed bytes. 4540 */ 4541 if (zio->io_priority == ZIO_PRIORITY_REBUILD) { 4542 vdev_t *tvd = vd->vdev_top; 4543 vdev_rebuild_t *vr = &tvd->vdev_rebuild_config; 4544 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys; 4545 uint64_t *rebuilt = &vrp->vrp_bytes_rebuilt; 4546 4547 if (vd->vdev_ops->vdev_op_leaf && 4548 vd->vdev_ops != &vdev_draid_spare_ops) { 4549 atomic_add_64(rebuilt, psize); 4550 } 4551 vs->vs_rebuild_processed += psize; 4552 } 4553 4554 if (flags & ZIO_FLAG_SELF_HEAL) 4555 vs->vs_self_healed += psize; 4556 } 4557 4558 /* 4559 * The bytes/ops/histograms are recorded at the leaf level and 4560 * aggregated into the higher level vdevs in vdev_get_stats(). 4561 */ 4562 if (vd->vdev_ops->vdev_op_leaf && 4563 (zio->io_priority < ZIO_PRIORITY_NUM_QUEUEABLE)) { 4564 zio_type_t vs_type = type; 4565 zio_priority_t priority = zio->io_priority; 4566 4567 /* 4568 * TRIM ops and bytes are reported to user space as 4569 * ZIO_TYPE_IOCTL. This is done to preserve the 4570 * vdev_stat_t structure layout for user space. 4571 */ 4572 if (type == ZIO_TYPE_TRIM) 4573 vs_type = ZIO_TYPE_IOCTL; 4574 4575 /* 4576 * Solely for the purposes of 'zpool iostat -lqrw' 4577 * reporting use the priority to categorize the IO. 4578 * Only the following are reported to user space: 4579 * 4580 * ZIO_PRIORITY_SYNC_READ, 4581 * ZIO_PRIORITY_SYNC_WRITE, 4582 * ZIO_PRIORITY_ASYNC_READ, 4583 * ZIO_PRIORITY_ASYNC_WRITE, 4584 * ZIO_PRIORITY_SCRUB, 4585 * ZIO_PRIORITY_TRIM. 4586 */ 4587 if (priority == ZIO_PRIORITY_REBUILD) { 4588 priority = ((type == ZIO_TYPE_WRITE) ? 4589 ZIO_PRIORITY_ASYNC_WRITE : 4590 ZIO_PRIORITY_SCRUB); 4591 } else if (priority == ZIO_PRIORITY_INITIALIZING) { 4592 ASSERT3U(type, ==, ZIO_TYPE_WRITE); 4593 priority = ZIO_PRIORITY_ASYNC_WRITE; 4594 } else if (priority == ZIO_PRIORITY_REMOVAL) { 4595 priority = ((type == ZIO_TYPE_WRITE) ? 4596 ZIO_PRIORITY_ASYNC_WRITE : 4597 ZIO_PRIORITY_ASYNC_READ); 4598 } 4599 4600 vs->vs_ops[vs_type]++; 4601 vs->vs_bytes[vs_type] += psize; 4602 4603 if (flags & ZIO_FLAG_DELEGATED) { 4604 vsx->vsx_agg_histo[priority] 4605 [RQ_HISTO(zio->io_size)]++; 4606 } else { 4607 vsx->vsx_ind_histo[priority] 4608 [RQ_HISTO(zio->io_size)]++; 4609 } 4610 4611 if (zio->io_delta && zio->io_delay) { 4612 vsx->vsx_queue_histo[priority] 4613 [L_HISTO(zio->io_delta - zio->io_delay)]++; 4614 vsx->vsx_disk_histo[type] 4615 [L_HISTO(zio->io_delay)]++; 4616 vsx->vsx_total_histo[type] 4617 [L_HISTO(zio->io_delta)]++; 4618 } 4619 } 4620 4621 mutex_exit(&vd->vdev_stat_lock); 4622 return; 4623 } 4624 4625 if (flags & ZIO_FLAG_SPECULATIVE) 4626 return; 4627 4628 /* 4629 * If this is an I/O error that is going to be retried, then ignore the 4630 * error. Otherwise, the user may interpret B_FAILFAST I/O errors as 4631 * hard errors, when in reality they can happen for any number of 4632 * innocuous reasons (bus resets, MPxIO link failure, etc). 4633 */ 4634 if (zio->io_error == EIO && 4635 !(zio->io_flags & ZIO_FLAG_IO_RETRY)) 4636 return; 4637 4638 /* 4639 * Intent logs writes won't propagate their error to the root 4640 * I/O so don't mark these types of failures as pool-level 4641 * errors. 4642 */ 4643 if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE)) 4644 return; 4645 4646 if (type == ZIO_TYPE_WRITE && txg != 0 && 4647 (!(flags & ZIO_FLAG_IO_REPAIR) || 4648 (flags & ZIO_FLAG_SCAN_THREAD) || 4649 spa->spa_claiming)) { 4650 /* 4651 * This is either a normal write (not a repair), or it's 4652 * a repair induced by the scrub thread, or it's a repair 4653 * made by zil_claim() during spa_load() in the first txg. 4654 * In the normal case, we commit the DTL change in the same 4655 * txg as the block was born. In the scrub-induced repair 4656 * case, we know that scrubs run in first-pass syncing context, 4657 * so we commit the DTL change in spa_syncing_txg(spa). 4658 * In the zil_claim() case, we commit in spa_first_txg(spa). 4659 * 4660 * We currently do not make DTL entries for failed spontaneous 4661 * self-healing writes triggered by normal (non-scrubbing) 4662 * reads, because we have no transactional context in which to 4663 * do so -- and it's not clear that it'd be desirable anyway. 4664 */ 4665 if (vd->vdev_ops->vdev_op_leaf) { 4666 uint64_t commit_txg = txg; 4667 if (flags & ZIO_FLAG_SCAN_THREAD) { 4668 ASSERT(flags & ZIO_FLAG_IO_REPAIR); 4669 ASSERT(spa_sync_pass(spa) == 1); 4670 vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1); 4671 commit_txg = spa_syncing_txg(spa); 4672 } else if (spa->spa_claiming) { 4673 ASSERT(flags & ZIO_FLAG_IO_REPAIR); 4674 commit_txg = spa_first_txg(spa); 4675 } 4676 ASSERT(commit_txg >= spa_syncing_txg(spa)); 4677 if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1)) 4678 return; 4679 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 4680 vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1); 4681 vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg); 4682 } 4683 if (vd != rvd) 4684 vdev_dtl_dirty(vd, DTL_MISSING, txg, 1); 4685 } 4686 } 4687 4688 int64_t 4689 vdev_deflated_space(vdev_t *vd, int64_t space) 4690 { 4691 ASSERT((space & (SPA_MINBLOCKSIZE-1)) == 0); 4692 ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache); 4693 4694 return ((space >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio); 4695 } 4696 4697 /* 4698 * Update the in-core space usage stats for this vdev, its metaslab class, 4699 * and the root vdev. 4700 */ 4701 void 4702 vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta, 4703 int64_t space_delta) 4704 { 4705 int64_t dspace_delta; 4706 spa_t *spa = vd->vdev_spa; 4707 vdev_t *rvd = spa->spa_root_vdev; 4708 4709 ASSERT(vd == vd->vdev_top); 4710 4711 /* 4712 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion 4713 * factor. We must calculate this here and not at the root vdev 4714 * because the root vdev's psize-to-asize is simply the max of its 4715 * children's, thus not accurate enough for us. 4716 */ 4717 dspace_delta = vdev_deflated_space(vd, space_delta); 4718 4719 mutex_enter(&vd->vdev_stat_lock); 4720 /* ensure we won't underflow */ 4721 if (alloc_delta < 0) { 4722 ASSERT3U(vd->vdev_stat.vs_alloc, >=, -alloc_delta); 4723 } 4724 4725 vd->vdev_stat.vs_alloc += alloc_delta; 4726 vd->vdev_stat.vs_space += space_delta; 4727 vd->vdev_stat.vs_dspace += dspace_delta; 4728 mutex_exit(&vd->vdev_stat_lock); 4729 4730 /* every class but log contributes to root space stats */ 4731 if (vd->vdev_mg != NULL && !vd->vdev_islog) { 4732 ASSERT(!vd->vdev_isl2cache); 4733 mutex_enter(&rvd->vdev_stat_lock); 4734 rvd->vdev_stat.vs_alloc += alloc_delta; 4735 rvd->vdev_stat.vs_space += space_delta; 4736 rvd->vdev_stat.vs_dspace += dspace_delta; 4737 mutex_exit(&rvd->vdev_stat_lock); 4738 } 4739 /* Note: metaslab_class_space_update moved to metaslab_space_update */ 4740 } 4741 4742 /* 4743 * Mark a top-level vdev's config as dirty, placing it on the dirty list 4744 * so that it will be written out next time the vdev configuration is synced. 4745 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs. 4746 */ 4747 void 4748 vdev_config_dirty(vdev_t *vd) 4749 { 4750 spa_t *spa = vd->vdev_spa; 4751 vdev_t *rvd = spa->spa_root_vdev; 4752 int c; 4753 4754 ASSERT(spa_writeable(spa)); 4755 4756 /* 4757 * If this is an aux vdev (as with l2cache and spare devices), then we 4758 * update the vdev config manually and set the sync flag. 4759 */ 4760 if (vd->vdev_aux != NULL) { 4761 spa_aux_vdev_t *sav = vd->vdev_aux; 4762 nvlist_t **aux; 4763 uint_t naux; 4764 4765 for (c = 0; c < sav->sav_count; c++) { 4766 if (sav->sav_vdevs[c] == vd) 4767 break; 4768 } 4769 4770 if (c == sav->sav_count) { 4771 /* 4772 * We're being removed. There's nothing more to do. 4773 */ 4774 ASSERT(sav->sav_sync == B_TRUE); 4775 return; 4776 } 4777 4778 sav->sav_sync = B_TRUE; 4779 4780 if (nvlist_lookup_nvlist_array(sav->sav_config, 4781 ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) { 4782 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 4783 ZPOOL_CONFIG_SPARES, &aux, &naux) == 0); 4784 } 4785 4786 ASSERT(c < naux); 4787 4788 /* 4789 * Setting the nvlist in the middle if the array is a little 4790 * sketchy, but it will work. 4791 */ 4792 nvlist_free(aux[c]); 4793 aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0); 4794 4795 return; 4796 } 4797 4798 /* 4799 * The dirty list is protected by the SCL_CONFIG lock. The caller 4800 * must either hold SCL_CONFIG as writer, or must be the sync thread 4801 * (which holds SCL_CONFIG as reader). There's only one sync thread, 4802 * so this is sufficient to ensure mutual exclusion. 4803 */ 4804 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 4805 (dsl_pool_sync_context(spa_get_dsl(spa)) && 4806 spa_config_held(spa, SCL_CONFIG, RW_READER))); 4807 4808 if (vd == rvd) { 4809 for (c = 0; c < rvd->vdev_children; c++) 4810 vdev_config_dirty(rvd->vdev_child[c]); 4811 } else { 4812 ASSERT(vd == vd->vdev_top); 4813 4814 if (!list_link_active(&vd->vdev_config_dirty_node) && 4815 vdev_is_concrete(vd)) { 4816 list_insert_head(&spa->spa_config_dirty_list, vd); 4817 } 4818 } 4819 } 4820 4821 void 4822 vdev_config_clean(vdev_t *vd) 4823 { 4824 spa_t *spa = vd->vdev_spa; 4825 4826 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 4827 (dsl_pool_sync_context(spa_get_dsl(spa)) && 4828 spa_config_held(spa, SCL_CONFIG, RW_READER))); 4829 4830 ASSERT(list_link_active(&vd->vdev_config_dirty_node)); 4831 list_remove(&spa->spa_config_dirty_list, vd); 4832 } 4833 4834 /* 4835 * Mark a top-level vdev's state as dirty, so that the next pass of 4836 * spa_sync() can convert this into vdev_config_dirty(). We distinguish 4837 * the state changes from larger config changes because they require 4838 * much less locking, and are often needed for administrative actions. 4839 */ 4840 void 4841 vdev_state_dirty(vdev_t *vd) 4842 { 4843 spa_t *spa = vd->vdev_spa; 4844 4845 ASSERT(spa_writeable(spa)); 4846 ASSERT(vd == vd->vdev_top); 4847 4848 /* 4849 * The state list is protected by the SCL_STATE lock. The caller 4850 * must either hold SCL_STATE as writer, or must be the sync thread 4851 * (which holds SCL_STATE as reader). There's only one sync thread, 4852 * so this is sufficient to ensure mutual exclusion. 4853 */ 4854 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 4855 (dsl_pool_sync_context(spa_get_dsl(spa)) && 4856 spa_config_held(spa, SCL_STATE, RW_READER))); 4857 4858 if (!list_link_active(&vd->vdev_state_dirty_node) && 4859 vdev_is_concrete(vd)) 4860 list_insert_head(&spa->spa_state_dirty_list, vd); 4861 } 4862 4863 void 4864 vdev_state_clean(vdev_t *vd) 4865 { 4866 spa_t *spa = vd->vdev_spa; 4867 4868 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 4869 (dsl_pool_sync_context(spa_get_dsl(spa)) && 4870 spa_config_held(spa, SCL_STATE, RW_READER))); 4871 4872 ASSERT(list_link_active(&vd->vdev_state_dirty_node)); 4873 list_remove(&spa->spa_state_dirty_list, vd); 4874 } 4875 4876 /* 4877 * Propagate vdev state up from children to parent. 4878 */ 4879 void 4880 vdev_propagate_state(vdev_t *vd) 4881 { 4882 spa_t *spa = vd->vdev_spa; 4883 vdev_t *rvd = spa->spa_root_vdev; 4884 int degraded = 0, faulted = 0; 4885 int corrupted = 0; 4886 vdev_t *child; 4887 4888 if (vd->vdev_children > 0) { 4889 for (int c = 0; c < vd->vdev_children; c++) { 4890 child = vd->vdev_child[c]; 4891 4892 /* 4893 * Don't factor holes or indirect vdevs into the 4894 * decision. 4895 */ 4896 if (!vdev_is_concrete(child)) 4897 continue; 4898 4899 if (!vdev_readable(child) || 4900 (!vdev_writeable(child) && spa_writeable(spa))) { 4901 /* 4902 * Root special: if there is a top-level log 4903 * device, treat the root vdev as if it were 4904 * degraded. 4905 */ 4906 if (child->vdev_islog && vd == rvd) 4907 degraded++; 4908 else 4909 faulted++; 4910 } else if (child->vdev_state <= VDEV_STATE_DEGRADED) { 4911 degraded++; 4912 } 4913 4914 if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA) 4915 corrupted++; 4916 } 4917 4918 vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded); 4919 4920 /* 4921 * Root special: if there is a top-level vdev that cannot be 4922 * opened due to corrupted metadata, then propagate the root 4923 * vdev's aux state as 'corrupt' rather than 'insufficient 4924 * replicas'. 4925 */ 4926 if (corrupted && vd == rvd && 4927 rvd->vdev_state == VDEV_STATE_CANT_OPEN) 4928 vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN, 4929 VDEV_AUX_CORRUPT_DATA); 4930 } 4931 4932 if (vd->vdev_parent) 4933 vdev_propagate_state(vd->vdev_parent); 4934 } 4935 4936 /* 4937 * Set a vdev's state. If this is during an open, we don't update the parent 4938 * state, because we're in the process of opening children depth-first. 4939 * Otherwise, we propagate the change to the parent. 4940 * 4941 * If this routine places a device in a faulted state, an appropriate ereport is 4942 * generated. 4943 */ 4944 void 4945 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux) 4946 { 4947 uint64_t save_state; 4948 spa_t *spa = vd->vdev_spa; 4949 4950 if (state == vd->vdev_state) { 4951 /* 4952 * Since vdev_offline() code path is already in an offline 4953 * state we can miss a statechange event to OFFLINE. Check 4954 * the previous state to catch this condition. 4955 */ 4956 if (vd->vdev_ops->vdev_op_leaf && 4957 (state == VDEV_STATE_OFFLINE) && 4958 (vd->vdev_prevstate >= VDEV_STATE_FAULTED)) { 4959 /* post an offline state change */ 4960 zfs_post_state_change(spa, vd, vd->vdev_prevstate); 4961 } 4962 vd->vdev_stat.vs_aux = aux; 4963 return; 4964 } 4965 4966 save_state = vd->vdev_state; 4967 4968 vd->vdev_state = state; 4969 vd->vdev_stat.vs_aux = aux; 4970 4971 /* 4972 * If we are setting the vdev state to anything but an open state, then 4973 * always close the underlying device unless the device has requested 4974 * a delayed close (i.e. we're about to remove or fault the device). 4975 * Otherwise, we keep accessible but invalid devices open forever. 4976 * We don't call vdev_close() itself, because that implies some extra 4977 * checks (offline, etc) that we don't want here. This is limited to 4978 * leaf devices, because otherwise closing the device will affect other 4979 * children. 4980 */ 4981 if (!vd->vdev_delayed_close && vdev_is_dead(vd) && 4982 vd->vdev_ops->vdev_op_leaf) 4983 vd->vdev_ops->vdev_op_close(vd); 4984 4985 if (vd->vdev_removed && 4986 state == VDEV_STATE_CANT_OPEN && 4987 (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) { 4988 /* 4989 * If the previous state is set to VDEV_STATE_REMOVED, then this 4990 * device was previously marked removed and someone attempted to 4991 * reopen it. If this failed due to a nonexistent device, then 4992 * keep the device in the REMOVED state. We also let this be if 4993 * it is one of our special test online cases, which is only 4994 * attempting to online the device and shouldn't generate an FMA 4995 * fault. 4996 */ 4997 vd->vdev_state = VDEV_STATE_REMOVED; 4998 vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 4999 } else if (state == VDEV_STATE_REMOVED) { 5000 vd->vdev_removed = B_TRUE; 5001 } else if (state == VDEV_STATE_CANT_OPEN) { 5002 /* 5003 * If we fail to open a vdev during an import or recovery, we 5004 * mark it as "not available", which signifies that it was 5005 * never there to begin with. Failure to open such a device 5006 * is not considered an error. 5007 */ 5008 if ((spa_load_state(spa) == SPA_LOAD_IMPORT || 5009 spa_load_state(spa) == SPA_LOAD_RECOVER) && 5010 vd->vdev_ops->vdev_op_leaf) 5011 vd->vdev_not_present = 1; 5012 5013 /* 5014 * Post the appropriate ereport. If the 'prevstate' field is 5015 * set to something other than VDEV_STATE_UNKNOWN, it indicates 5016 * that this is part of a vdev_reopen(). In this case, we don't 5017 * want to post the ereport if the device was already in the 5018 * CANT_OPEN state beforehand. 5019 * 5020 * If the 'checkremove' flag is set, then this is an attempt to 5021 * online the device in response to an insertion event. If we 5022 * hit this case, then we have detected an insertion event for a 5023 * faulted or offline device that wasn't in the removed state. 5024 * In this scenario, we don't post an ereport because we are 5025 * about to replace the device, or attempt an online with 5026 * vdev_forcefault, which will generate the fault for us. 5027 */ 5028 if ((vd->vdev_prevstate != state || vd->vdev_forcefault) && 5029 !vd->vdev_not_present && !vd->vdev_checkremove && 5030 vd != spa->spa_root_vdev) { 5031 const char *class; 5032 5033 switch (aux) { 5034 case VDEV_AUX_OPEN_FAILED: 5035 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED; 5036 break; 5037 case VDEV_AUX_CORRUPT_DATA: 5038 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA; 5039 break; 5040 case VDEV_AUX_NO_REPLICAS: 5041 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS; 5042 break; 5043 case VDEV_AUX_BAD_GUID_SUM: 5044 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM; 5045 break; 5046 case VDEV_AUX_TOO_SMALL: 5047 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL; 5048 break; 5049 case VDEV_AUX_BAD_LABEL: 5050 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL; 5051 break; 5052 case VDEV_AUX_BAD_ASHIFT: 5053 class = FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT; 5054 break; 5055 default: 5056 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN; 5057 } 5058 5059 (void) zfs_ereport_post(class, spa, vd, NULL, NULL, 5060 save_state); 5061 } 5062 5063 /* Erase any notion of persistent removed state */ 5064 vd->vdev_removed = B_FALSE; 5065 } else { 5066 vd->vdev_removed = B_FALSE; 5067 } 5068 5069 /* 5070 * Notify ZED of any significant state-change on a leaf vdev. 5071 * 5072 */ 5073 if (vd->vdev_ops->vdev_op_leaf) { 5074 /* preserve original state from a vdev_reopen() */ 5075 if ((vd->vdev_prevstate != VDEV_STATE_UNKNOWN) && 5076 (vd->vdev_prevstate != vd->vdev_state) && 5077 (save_state <= VDEV_STATE_CLOSED)) 5078 save_state = vd->vdev_prevstate; 5079 5080 /* filter out state change due to initial vdev_open */ 5081 if (save_state > VDEV_STATE_CLOSED) 5082 zfs_post_state_change(spa, vd, save_state); 5083 } 5084 5085 if (!isopen && vd->vdev_parent) 5086 vdev_propagate_state(vd->vdev_parent); 5087 } 5088 5089 boolean_t 5090 vdev_children_are_offline(vdev_t *vd) 5091 { 5092 ASSERT(!vd->vdev_ops->vdev_op_leaf); 5093 5094 for (uint64_t i = 0; i < vd->vdev_children; i++) { 5095 if (vd->vdev_child[i]->vdev_state != VDEV_STATE_OFFLINE) 5096 return (B_FALSE); 5097 } 5098 5099 return (B_TRUE); 5100 } 5101 5102 /* 5103 * Check the vdev configuration to ensure that it's capable of supporting 5104 * a root pool. We do not support partial configuration. 5105 */ 5106 boolean_t 5107 vdev_is_bootable(vdev_t *vd) 5108 { 5109 if (!vd->vdev_ops->vdev_op_leaf) { 5110 const char *vdev_type = vd->vdev_ops->vdev_op_type; 5111 5112 if (strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) 5113 return (B_FALSE); 5114 } 5115 5116 for (int c = 0; c < vd->vdev_children; c++) { 5117 if (!vdev_is_bootable(vd->vdev_child[c])) 5118 return (B_FALSE); 5119 } 5120 return (B_TRUE); 5121 } 5122 5123 boolean_t 5124 vdev_is_concrete(vdev_t *vd) 5125 { 5126 vdev_ops_t *ops = vd->vdev_ops; 5127 if (ops == &vdev_indirect_ops || ops == &vdev_hole_ops || 5128 ops == &vdev_missing_ops || ops == &vdev_root_ops) { 5129 return (B_FALSE); 5130 } else { 5131 return (B_TRUE); 5132 } 5133 } 5134 5135 /* 5136 * Determine if a log device has valid content. If the vdev was 5137 * removed or faulted in the MOS config then we know that 5138 * the content on the log device has already been written to the pool. 5139 */ 5140 boolean_t 5141 vdev_log_state_valid(vdev_t *vd) 5142 { 5143 if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted && 5144 !vd->vdev_removed) 5145 return (B_TRUE); 5146 5147 for (int c = 0; c < vd->vdev_children; c++) 5148 if (vdev_log_state_valid(vd->vdev_child[c])) 5149 return (B_TRUE); 5150 5151 return (B_FALSE); 5152 } 5153 5154 /* 5155 * Expand a vdev if possible. 5156 */ 5157 void 5158 vdev_expand(vdev_t *vd, uint64_t txg) 5159 { 5160 ASSERT(vd->vdev_top == vd); 5161 ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 5162 ASSERT(vdev_is_concrete(vd)); 5163 5164 vdev_set_deflate_ratio(vd); 5165 5166 if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count && 5167 vdev_is_concrete(vd)) { 5168 vdev_metaslab_group_create(vd); 5169 VERIFY(vdev_metaslab_init(vd, txg) == 0); 5170 vdev_config_dirty(vd); 5171 } 5172 } 5173 5174 /* 5175 * Split a vdev. 5176 */ 5177 void 5178 vdev_split(vdev_t *vd) 5179 { 5180 vdev_t *cvd, *pvd = vd->vdev_parent; 5181 5182 vdev_remove_child(pvd, vd); 5183 vdev_compact_children(pvd); 5184 5185 cvd = pvd->vdev_child[0]; 5186 if (pvd->vdev_children == 1) { 5187 vdev_remove_parent(cvd); 5188 cvd->vdev_splitting = B_TRUE; 5189 } 5190 vdev_propagate_state(cvd); 5191 } 5192 5193 void 5194 vdev_deadman(vdev_t *vd, char *tag) 5195 { 5196 for (int c = 0; c < vd->vdev_children; c++) { 5197 vdev_t *cvd = vd->vdev_child[c]; 5198 5199 vdev_deadman(cvd, tag); 5200 } 5201 5202 if (vd->vdev_ops->vdev_op_leaf) { 5203 vdev_queue_t *vq = &vd->vdev_queue; 5204 5205 mutex_enter(&vq->vq_lock); 5206 if (avl_numnodes(&vq->vq_active_tree) > 0) { 5207 spa_t *spa = vd->vdev_spa; 5208 zio_t *fio; 5209 uint64_t delta; 5210 5211 zfs_dbgmsg("slow vdev: %s has %d active IOs", 5212 vd->vdev_path, avl_numnodes(&vq->vq_active_tree)); 5213 5214 /* 5215 * Look at the head of all the pending queues, 5216 * if any I/O has been outstanding for longer than 5217 * the spa_deadman_synctime invoke the deadman logic. 5218 */ 5219 fio = avl_first(&vq->vq_active_tree); 5220 delta = gethrtime() - fio->io_timestamp; 5221 if (delta > spa_deadman_synctime(spa)) 5222 zio_deadman(fio, tag); 5223 } 5224 mutex_exit(&vq->vq_lock); 5225 } 5226 } 5227 5228 void 5229 vdev_defer_resilver(vdev_t *vd) 5230 { 5231 ASSERT(vd->vdev_ops->vdev_op_leaf); 5232 5233 vd->vdev_resilver_deferred = B_TRUE; 5234 vd->vdev_spa->spa_resilver_deferred = B_TRUE; 5235 } 5236 5237 /* 5238 * Clears the resilver deferred flag on all leaf devs under vd. Returns 5239 * B_TRUE if we have devices that need to be resilvered and are available to 5240 * accept resilver I/Os. 5241 */ 5242 boolean_t 5243 vdev_clear_resilver_deferred(vdev_t *vd, dmu_tx_t *tx) 5244 { 5245 boolean_t resilver_needed = B_FALSE; 5246 spa_t *spa = vd->vdev_spa; 5247 5248 for (int c = 0; c < vd->vdev_children; c++) { 5249 vdev_t *cvd = vd->vdev_child[c]; 5250 resilver_needed |= vdev_clear_resilver_deferred(cvd, tx); 5251 } 5252 5253 if (vd == spa->spa_root_vdev && 5254 spa_feature_is_active(spa, SPA_FEATURE_RESILVER_DEFER)) { 5255 spa_feature_decr(spa, SPA_FEATURE_RESILVER_DEFER, tx); 5256 vdev_config_dirty(vd); 5257 spa->spa_resilver_deferred = B_FALSE; 5258 return (resilver_needed); 5259 } 5260 5261 if (!vdev_is_concrete(vd) || vd->vdev_aux || 5262 !vd->vdev_ops->vdev_op_leaf) 5263 return (resilver_needed); 5264 5265 vd->vdev_resilver_deferred = B_FALSE; 5266 5267 return (!vdev_is_dead(vd) && !vd->vdev_offline && 5268 vdev_resilver_needed(vd, NULL, NULL)); 5269 } 5270 5271 boolean_t 5272 vdev_xlate_is_empty(range_seg64_t *rs) 5273 { 5274 return (rs->rs_start == rs->rs_end); 5275 } 5276 5277 /* 5278 * Translate a logical range to the first contiguous physical range for the 5279 * specified vdev_t. This function is initially called with a leaf vdev and 5280 * will walk each parent vdev until it reaches a top-level vdev. Once the 5281 * top-level is reached the physical range is initialized and the recursive 5282 * function begins to unwind. As it unwinds it calls the parent's vdev 5283 * specific translation function to do the real conversion. 5284 */ 5285 void 5286 vdev_xlate(vdev_t *vd, const range_seg64_t *logical_rs, 5287 range_seg64_t *physical_rs, range_seg64_t *remain_rs) 5288 { 5289 /* 5290 * Walk up the vdev tree 5291 */ 5292 if (vd != vd->vdev_top) { 5293 vdev_xlate(vd->vdev_parent, logical_rs, physical_rs, 5294 remain_rs); 5295 } else { 5296 /* 5297 * We've reached the top-level vdev, initialize the physical 5298 * range to the logical range and set an empty remaining 5299 * range then start to unwind. 5300 */ 5301 physical_rs->rs_start = logical_rs->rs_start; 5302 physical_rs->rs_end = logical_rs->rs_end; 5303 5304 remain_rs->rs_start = logical_rs->rs_start; 5305 remain_rs->rs_end = logical_rs->rs_start; 5306 5307 return; 5308 } 5309 5310 vdev_t *pvd = vd->vdev_parent; 5311 ASSERT3P(pvd, !=, NULL); 5312 ASSERT3P(pvd->vdev_ops->vdev_op_xlate, !=, NULL); 5313 5314 /* 5315 * As this recursive function unwinds, translate the logical 5316 * range into its physical and any remaining components by calling 5317 * the vdev specific translate function. 5318 */ 5319 range_seg64_t intermediate = { 0 }; 5320 pvd->vdev_ops->vdev_op_xlate(vd, physical_rs, &intermediate, remain_rs); 5321 5322 physical_rs->rs_start = intermediate.rs_start; 5323 physical_rs->rs_end = intermediate.rs_end; 5324 } 5325 5326 void 5327 vdev_xlate_walk(vdev_t *vd, const range_seg64_t *logical_rs, 5328 vdev_xlate_func_t *func, void *arg) 5329 { 5330 range_seg64_t iter_rs = *logical_rs; 5331 range_seg64_t physical_rs; 5332 range_seg64_t remain_rs; 5333 5334 while (!vdev_xlate_is_empty(&iter_rs)) { 5335 5336 vdev_xlate(vd, &iter_rs, &physical_rs, &remain_rs); 5337 5338 /* 5339 * With raidz and dRAID, it's possible that the logical range 5340 * does not live on this leaf vdev. Only when there is a non- 5341 * zero physical size call the provided function. 5342 */ 5343 if (!vdev_xlate_is_empty(&physical_rs)) 5344 func(arg, &physical_rs); 5345 5346 iter_rs = remain_rs; 5347 } 5348 } 5349 5350 /* 5351 * Look at the vdev tree and determine whether any devices are currently being 5352 * replaced. 5353 */ 5354 boolean_t 5355 vdev_replace_in_progress(vdev_t *vdev) 5356 { 5357 ASSERT(spa_config_held(vdev->vdev_spa, SCL_ALL, RW_READER) != 0); 5358 5359 if (vdev->vdev_ops == &vdev_replacing_ops) 5360 return (B_TRUE); 5361 5362 /* 5363 * A 'spare' vdev indicates that we have a replace in progress, unless 5364 * it has exactly two children, and the second, the hot spare, has 5365 * finished being resilvered. 5366 */ 5367 if (vdev->vdev_ops == &vdev_spare_ops && (vdev->vdev_children > 2 || 5368 !vdev_dtl_empty(vdev->vdev_child[1], DTL_MISSING))) 5369 return (B_TRUE); 5370 5371 for (int i = 0; i < vdev->vdev_children; i++) { 5372 if (vdev_replace_in_progress(vdev->vdev_child[i])) 5373 return (B_TRUE); 5374 } 5375 5376 return (B_FALSE); 5377 } 5378 5379 EXPORT_SYMBOL(vdev_fault); 5380 EXPORT_SYMBOL(vdev_degrade); 5381 EXPORT_SYMBOL(vdev_online); 5382 EXPORT_SYMBOL(vdev_offline); 5383 EXPORT_SYMBOL(vdev_clear); 5384 5385 /* BEGIN CSTYLED */ 5386 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, default_ms_count, INT, ZMOD_RW, 5387 "Target number of metaslabs per top-level vdev"); 5388 5389 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, default_ms_shift, INT, ZMOD_RW, 5390 "Default limit for metaslab size"); 5391 5392 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, min_ms_count, INT, ZMOD_RW, 5393 "Minimum number of metaslabs per top-level vdev"); 5394 5395 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, ms_count_limit, INT, ZMOD_RW, 5396 "Practical upper limit of total metaslabs per top-level vdev"); 5397 5398 ZFS_MODULE_PARAM(zfs, zfs_, slow_io_events_per_second, UINT, ZMOD_RW, 5399 "Rate limit slow IO (delay) events to this many per second"); 5400 5401 ZFS_MODULE_PARAM(zfs, zfs_, checksum_events_per_second, UINT, ZMOD_RW, 5402 "Rate limit checksum events to this many checksum errors per second " 5403 "(do not set below zed threshold)."); 5404 5405 ZFS_MODULE_PARAM(zfs, zfs_, scan_ignore_errors, INT, ZMOD_RW, 5406 "Ignore errors during resilver/scrub"); 5407 5408 ZFS_MODULE_PARAM(zfs_vdev, vdev_, validate_skip, INT, ZMOD_RW, 5409 "Bypass vdev_validate()"); 5410 5411 ZFS_MODULE_PARAM(zfs, zfs_, nocacheflush, INT, ZMOD_RW, 5412 "Disable cache flushes"); 5413 5414 ZFS_MODULE_PARAM(zfs, zfs_, embedded_slog_min_ms, INT, ZMOD_RW, 5415 "Minimum number of metaslabs required to dedicate one for log blocks"); 5416 5417 ZFS_MODULE_PARAM_CALL(zfs_vdev, zfs_vdev_, min_auto_ashift, 5418 param_set_min_auto_ashift, param_get_ulong, ZMOD_RW, 5419 "Minimum ashift used when creating new top-level vdevs"); 5420 5421 ZFS_MODULE_PARAM_CALL(zfs_vdev, zfs_vdev_, max_auto_ashift, 5422 param_set_max_auto_ashift, param_get_ulong, ZMOD_RW, 5423 "Maximum ashift used when optimizing for logical -> physical sector " 5424 "size on new top-level vdevs"); 5425 /* END CSTYLED */ 5426