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