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