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