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