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