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