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