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