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 http://www.opensolaris.org/os/licensing. 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, 2018 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 2019 Joyent, Inc. 29 * Copyright (c) 2017, Intel Corporation. 30 * Copyright (c) 2019, Datto Inc. All rights reserved. 31 */ 32 33 #include <sys/zfs_context.h> 34 #include <sys/fm/fs/zfs.h> 35 #include <sys/spa.h> 36 #include <sys/spa_impl.h> 37 #include <sys/bpobj.h> 38 #include <sys/dmu.h> 39 #include <sys/dmu_tx.h> 40 #include <sys/dsl_dir.h> 41 #include <sys/vdev_impl.h> 42 #include <sys/uberblock_impl.h> 43 #include <sys/metaslab.h> 44 #include <sys/metaslab_impl.h> 45 #include <sys/space_map.h> 46 #include <sys/space_reftree.h> 47 #include <sys/zio.h> 48 #include <sys/zap.h> 49 #include <sys/fs/zfs.h> 50 #include <sys/arc.h> 51 #include <sys/zil.h> 52 #include <sys/dsl_scan.h> 53 #include <sys/abd.h> 54 #include <sys/vdev_initialize.h> 55 #include <sys/vdev_trim.h> 56 57 /* 58 * Virtual device management. 59 */ 60 61 static vdev_ops_t *vdev_ops_table[] = { 62 &vdev_root_ops, 63 &vdev_raidz_ops, 64 &vdev_mirror_ops, 65 &vdev_replacing_ops, 66 &vdev_spare_ops, 67 &vdev_disk_ops, 68 &vdev_file_ops, 69 &vdev_missing_ops, 70 &vdev_hole_ops, 71 &vdev_indirect_ops, 72 NULL 73 }; 74 75 /* maximum scrub/resilver I/O queue per leaf vdev */ 76 int zfs_scrub_limit = 10; 77 78 /* default target for number of metaslabs per top-level vdev */ 79 int zfs_vdev_default_ms_count = 200; 80 81 /* minimum number of metaslabs per top-level vdev */ 82 int zfs_vdev_min_ms_count = 16; 83 84 /* practical upper limit of total metaslabs per top-level vdev */ 85 int zfs_vdev_ms_count_limit = 1ULL << 17; 86 87 /* lower limit for metaslab size (512M) */ 88 int zfs_vdev_default_ms_shift = 29; 89 90 /* upper limit for metaslab size (16G) */ 91 int zfs_vdev_max_ms_shift = 34; 92 93 boolean_t vdev_validate_skip = B_FALSE; 94 95 /* 96 * Since the DTL space map of a vdev is not expected to have a lot of 97 * entries, we default its block size to 4K. 98 */ 99 int zfs_vdev_dtl_sm_blksz = (1 << 12); 100 101 /* 102 * Ignore errors during scrub/resilver. Allows to work around resilver 103 * upon import when there are pool errors. 104 */ 105 int zfs_scan_ignore_errors = 0; 106 107 /* 108 * vdev-wide space maps that have lots of entries written to them at 109 * the end of each transaction can benefit from a higher I/O bandwidth 110 * (e.g. vdev_obsolete_sm), thus we default their block size to 128K. 111 */ 112 int zfs_vdev_standard_sm_blksz = (1 << 17); 113 114 int zfs_ashift_min; 115 116 /*PRINTFLIKE2*/ 117 void 118 vdev_dbgmsg(vdev_t *vd, const char *fmt, ...) 119 { 120 va_list adx; 121 char buf[256]; 122 123 va_start(adx, fmt); 124 (void) vsnprintf(buf, sizeof (buf), fmt, adx); 125 va_end(adx); 126 127 if (vd->vdev_path != NULL) { 128 zfs_dbgmsg("%s vdev '%s': %s", vd->vdev_ops->vdev_op_type, 129 vd->vdev_path, buf); 130 } else { 131 zfs_dbgmsg("%s-%llu vdev (guid %llu): %s", 132 vd->vdev_ops->vdev_op_type, 133 (u_longlong_t)vd->vdev_id, 134 (u_longlong_t)vd->vdev_guid, buf); 135 } 136 } 137 138 void 139 vdev_dbgmsg_print_tree(vdev_t *vd, int indent) 140 { 141 char state[20]; 142 143 if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) { 144 zfs_dbgmsg("%*svdev %u: %s", indent, "", vd->vdev_id, 145 vd->vdev_ops->vdev_op_type); 146 return; 147 } 148 149 switch (vd->vdev_state) { 150 case VDEV_STATE_UNKNOWN: 151 (void) snprintf(state, sizeof (state), "unknown"); 152 break; 153 case VDEV_STATE_CLOSED: 154 (void) snprintf(state, sizeof (state), "closed"); 155 break; 156 case VDEV_STATE_OFFLINE: 157 (void) snprintf(state, sizeof (state), "offline"); 158 break; 159 case VDEV_STATE_REMOVED: 160 (void) snprintf(state, sizeof (state), "removed"); 161 break; 162 case VDEV_STATE_CANT_OPEN: 163 (void) snprintf(state, sizeof (state), "can't open"); 164 break; 165 case VDEV_STATE_FAULTED: 166 (void) snprintf(state, sizeof (state), "faulted"); 167 break; 168 case VDEV_STATE_DEGRADED: 169 (void) snprintf(state, sizeof (state), "degraded"); 170 break; 171 case VDEV_STATE_HEALTHY: 172 (void) snprintf(state, sizeof (state), "healthy"); 173 break; 174 default: 175 (void) snprintf(state, sizeof (state), "<state %u>", 176 (uint_t)vd->vdev_state); 177 } 178 179 zfs_dbgmsg("%*svdev %u: %s%s, guid: %llu, path: %s, %s", indent, 180 "", (int)vd->vdev_id, vd->vdev_ops->vdev_op_type, 181 vd->vdev_islog ? " (log)" : "", 182 (u_longlong_t)vd->vdev_guid, 183 vd->vdev_path ? vd->vdev_path : "N/A", state); 184 185 for (uint64_t i = 0; i < vd->vdev_children; i++) 186 vdev_dbgmsg_print_tree(vd->vdev_child[i], indent + 2); 187 } 188 189 /* 190 * Given a vdev type, return the appropriate ops vector. 191 */ 192 static vdev_ops_t * 193 vdev_getops(const char *type) 194 { 195 vdev_ops_t *ops, **opspp; 196 197 for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++) 198 if (strcmp(ops->vdev_op_type, type) == 0) 199 break; 200 201 return (ops); 202 } 203 204 /* 205 * Derive the enumerated alloction bias from string input. 206 * String origin is either the per-vdev zap or zpool(1M). 207 */ 208 static vdev_alloc_bias_t 209 vdev_derive_alloc_bias(const char *bias) 210 { 211 vdev_alloc_bias_t alloc_bias = VDEV_BIAS_NONE; 212 213 if (strcmp(bias, VDEV_ALLOC_BIAS_LOG) == 0) 214 alloc_bias = VDEV_BIAS_LOG; 215 else if (strcmp(bias, VDEV_ALLOC_BIAS_SPECIAL) == 0) 216 alloc_bias = VDEV_BIAS_SPECIAL; 217 else if (strcmp(bias, VDEV_ALLOC_BIAS_DEDUP) == 0) 218 alloc_bias = VDEV_BIAS_DEDUP; 219 220 return (alloc_bias); 221 } 222 223 /* ARGSUSED */ 224 void 225 vdev_default_xlate(vdev_t *vd, const range_seg64_t *in, range_seg64_t *res) 226 { 227 res->rs_start = in->rs_start; 228 res->rs_end = in->rs_end; 229 } 230 231 /* 232 * Default asize function: return the MAX of psize with the asize of 233 * all children. This is what's used by anything other than RAID-Z. 234 */ 235 uint64_t 236 vdev_default_asize(vdev_t *vd, uint64_t psize) 237 { 238 uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift); 239 uint64_t csize; 240 241 for (int c = 0; c < vd->vdev_children; c++) { 242 csize = vdev_psize_to_asize(vd->vdev_child[c], psize); 243 asize = MAX(asize, csize); 244 } 245 246 return (asize); 247 } 248 249 /* 250 * Get the minimum allocatable size. We define the allocatable size as 251 * the vdev's asize rounded to the nearest metaslab. This allows us to 252 * replace or attach devices which don't have the same physical size but 253 * can still satisfy the same number of allocations. 254 */ 255 uint64_t 256 vdev_get_min_asize(vdev_t *vd) 257 { 258 vdev_t *pvd = vd->vdev_parent; 259 260 /* 261 * If our parent is NULL (inactive spare or cache) or is the root, 262 * just return our own asize. 263 */ 264 if (pvd == NULL) 265 return (vd->vdev_asize); 266 267 /* 268 * The top-level vdev just returns the allocatable size rounded 269 * to the nearest metaslab. 270 */ 271 if (vd == vd->vdev_top) 272 return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift)); 273 274 /* 275 * The allocatable space for a raidz vdev is N * sizeof(smallest child), 276 * so each child must provide at least 1/Nth of its asize. 277 */ 278 if (pvd->vdev_ops == &vdev_raidz_ops) 279 return ((pvd->vdev_min_asize + pvd->vdev_children - 1) / 280 pvd->vdev_children); 281 282 return (pvd->vdev_min_asize); 283 } 284 285 void 286 vdev_set_min_asize(vdev_t *vd) 287 { 288 vd->vdev_min_asize = vdev_get_min_asize(vd); 289 290 for (int c = 0; c < vd->vdev_children; c++) 291 vdev_set_min_asize(vd->vdev_child[c]); 292 } 293 294 vdev_t * 295 vdev_lookup_top(spa_t *spa, uint64_t vdev) 296 { 297 vdev_t *rvd = spa->spa_root_vdev; 298 299 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 300 301 if (vdev < rvd->vdev_children) { 302 ASSERT(rvd->vdev_child[vdev] != NULL); 303 return (rvd->vdev_child[vdev]); 304 } 305 306 return (NULL); 307 } 308 309 vdev_t * 310 vdev_lookup_by_guid(vdev_t *vd, uint64_t guid) 311 { 312 vdev_t *mvd; 313 314 if (vd->vdev_guid == guid) 315 return (vd); 316 317 for (int c = 0; c < vd->vdev_children; c++) 318 if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) != 319 NULL) 320 return (mvd); 321 322 return (NULL); 323 } 324 325 static int 326 vdev_count_leaves_impl(vdev_t *vd) 327 { 328 int n = 0; 329 330 if (vd->vdev_ops->vdev_op_leaf) 331 return (1); 332 333 for (int c = 0; c < vd->vdev_children; c++) 334 n += vdev_count_leaves_impl(vd->vdev_child[c]); 335 336 return (n); 337 } 338 339 int 340 vdev_count_leaves(spa_t *spa) 341 { 342 return (vdev_count_leaves_impl(spa->spa_root_vdev)); 343 } 344 345 void 346 vdev_add_child(vdev_t *pvd, vdev_t *cvd) 347 { 348 size_t oldsize, newsize; 349 uint64_t id = cvd->vdev_id; 350 vdev_t **newchild; 351 spa_t *spa = cvd->vdev_spa; 352 353 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 354 ASSERT(cvd->vdev_parent == NULL); 355 356 cvd->vdev_parent = pvd; 357 358 if (pvd == NULL) 359 return; 360 361 ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL); 362 363 oldsize = pvd->vdev_children * sizeof (vdev_t *); 364 pvd->vdev_children = MAX(pvd->vdev_children, id + 1); 365 newsize = pvd->vdev_children * sizeof (vdev_t *); 366 367 newchild = kmem_zalloc(newsize, KM_SLEEP); 368 if (pvd->vdev_child != NULL) { 369 bcopy(pvd->vdev_child, newchild, oldsize); 370 kmem_free(pvd->vdev_child, oldsize); 371 } 372 373 pvd->vdev_child = newchild; 374 pvd->vdev_child[id] = cvd; 375 376 cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd); 377 ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL); 378 379 /* 380 * Walk up all ancestors to update guid sum. 381 */ 382 for (; pvd != NULL; pvd = pvd->vdev_parent) 383 pvd->vdev_guid_sum += cvd->vdev_guid_sum; 384 385 if (cvd->vdev_ops->vdev_op_leaf) { 386 list_insert_head(&cvd->vdev_spa->spa_leaf_list, cvd); 387 cvd->vdev_spa->spa_leaf_list_gen++; 388 } 389 } 390 391 void 392 vdev_remove_child(vdev_t *pvd, vdev_t *cvd) 393 { 394 int c; 395 uint_t id = cvd->vdev_id; 396 397 ASSERT(cvd->vdev_parent == pvd); 398 399 if (pvd == NULL) 400 return; 401 402 ASSERT(id < pvd->vdev_children); 403 ASSERT(pvd->vdev_child[id] == cvd); 404 405 pvd->vdev_child[id] = NULL; 406 cvd->vdev_parent = NULL; 407 408 for (c = 0; c < pvd->vdev_children; c++) 409 if (pvd->vdev_child[c]) 410 break; 411 412 if (c == pvd->vdev_children) { 413 kmem_free(pvd->vdev_child, c * sizeof (vdev_t *)); 414 pvd->vdev_child = NULL; 415 pvd->vdev_children = 0; 416 } 417 418 if (cvd->vdev_ops->vdev_op_leaf) { 419 spa_t *spa = cvd->vdev_spa; 420 list_remove(&spa->spa_leaf_list, cvd); 421 spa->spa_leaf_list_gen++; 422 } 423 424 /* 425 * Walk up all ancestors to update guid sum. 426 */ 427 for (; pvd != NULL; pvd = pvd->vdev_parent) 428 pvd->vdev_guid_sum -= cvd->vdev_guid_sum; 429 } 430 431 /* 432 * Remove any holes in the child array. 433 */ 434 void 435 vdev_compact_children(vdev_t *pvd) 436 { 437 vdev_t **newchild, *cvd; 438 int oldc = pvd->vdev_children; 439 int newc; 440 441 ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 442 443 for (int c = newc = 0; c < oldc; c++) 444 if (pvd->vdev_child[c]) 445 newc++; 446 447 newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP); 448 449 for (int c = newc = 0; c < oldc; c++) { 450 if ((cvd = pvd->vdev_child[c]) != NULL) { 451 newchild[newc] = cvd; 452 cvd->vdev_id = newc++; 453 } 454 } 455 456 kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *)); 457 pvd->vdev_child = newchild; 458 pvd->vdev_children = newc; 459 } 460 461 /* 462 * Allocate and minimally initialize a vdev_t. 463 */ 464 vdev_t * 465 vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops) 466 { 467 vdev_t *vd; 468 vdev_indirect_config_t *vic; 469 470 vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP); 471 vic = &vd->vdev_indirect_config; 472 473 if (spa->spa_root_vdev == NULL) { 474 ASSERT(ops == &vdev_root_ops); 475 spa->spa_root_vdev = vd; 476 spa->spa_load_guid = spa_generate_guid(NULL); 477 } 478 479 if (guid == 0 && ops != &vdev_hole_ops) { 480 if (spa->spa_root_vdev == vd) { 481 /* 482 * The root vdev's guid will also be the pool guid, 483 * which must be unique among all pools. 484 */ 485 guid = spa_generate_guid(NULL); 486 } else { 487 /* 488 * Any other vdev's guid must be unique within the pool. 489 */ 490 guid = spa_generate_guid(spa); 491 } 492 ASSERT(!spa_guid_exists(spa_guid(spa), guid)); 493 } 494 495 vd->vdev_spa = spa; 496 vd->vdev_id = id; 497 vd->vdev_guid = guid; 498 vd->vdev_guid_sum = guid; 499 vd->vdev_ops = ops; 500 vd->vdev_state = VDEV_STATE_CLOSED; 501 vd->vdev_ishole = (ops == &vdev_hole_ops); 502 vic->vic_prev_indirect_vdev = UINT64_MAX; 503 504 rw_init(&vd->vdev_indirect_rwlock, NULL, RW_DEFAULT, NULL); 505 mutex_init(&vd->vdev_obsolete_lock, NULL, MUTEX_DEFAULT, NULL); 506 vd->vdev_obsolete_segments = range_tree_create(NULL, RANGE_SEG64, NULL, 507 0, 0); 508 509 list_link_init(&vd->vdev_initialize_node); 510 list_link_init(&vd->vdev_leaf_node); 511 list_link_init(&vd->vdev_trim_node); 512 mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL); 513 mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL); 514 mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL); 515 mutex_init(&vd->vdev_scan_io_queue_lock, NULL, MUTEX_DEFAULT, NULL); 516 mutex_init(&vd->vdev_initialize_lock, NULL, MUTEX_DEFAULT, NULL); 517 mutex_init(&vd->vdev_initialize_io_lock, NULL, MUTEX_DEFAULT, NULL); 518 cv_init(&vd->vdev_initialize_cv, NULL, CV_DEFAULT, NULL); 519 cv_init(&vd->vdev_initialize_io_cv, NULL, CV_DEFAULT, NULL); 520 mutex_init(&vd->vdev_trim_lock, NULL, MUTEX_DEFAULT, NULL); 521 mutex_init(&vd->vdev_autotrim_lock, NULL, MUTEX_DEFAULT, NULL); 522 mutex_init(&vd->vdev_trim_io_lock, NULL, MUTEX_DEFAULT, NULL); 523 cv_init(&vd->vdev_trim_cv, NULL, CV_DEFAULT, NULL); 524 cv_init(&vd->vdev_autotrim_cv, NULL, CV_DEFAULT, NULL); 525 cv_init(&vd->vdev_trim_io_cv, NULL, CV_DEFAULT, NULL); 526 527 for (int t = 0; t < DTL_TYPES; t++) { 528 vd->vdev_dtl[t] = range_tree_create(NULL, RANGE_SEG64, NULL, 0, 529 0); 530 } 531 txg_list_create(&vd->vdev_ms_list, spa, 532 offsetof(struct metaslab, ms_txg_node)); 533 txg_list_create(&vd->vdev_dtl_list, spa, 534 offsetof(struct vdev, vdev_dtl_node)); 535 vd->vdev_stat.vs_timestamp = gethrtime(); 536 vdev_queue_init(vd); 537 vdev_cache_init(vd); 538 539 return (vd); 540 } 541 542 /* 543 * Allocate a new vdev. The 'alloctype' is used to control whether we are 544 * creating a new vdev or loading an existing one - the behavior is slightly 545 * different for each case. 546 */ 547 int 548 vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id, 549 int alloctype) 550 { 551 vdev_ops_t *ops; 552 char *type; 553 uint64_t guid = 0, islog, nparity; 554 vdev_t *vd; 555 vdev_indirect_config_t *vic; 556 vdev_alloc_bias_t alloc_bias = VDEV_BIAS_NONE; 557 boolean_t top_level = (parent && !parent->vdev_parent); 558 559 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 560 561 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0) 562 return (SET_ERROR(EINVAL)); 563 564 if ((ops = vdev_getops(type)) == NULL) 565 return (SET_ERROR(EINVAL)); 566 567 /* 568 * If this is a load, get the vdev guid from the nvlist. 569 * Otherwise, vdev_alloc_common() will generate one for us. 570 */ 571 if (alloctype == VDEV_ALLOC_LOAD) { 572 uint64_t label_id; 573 574 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) || 575 label_id != id) 576 return (SET_ERROR(EINVAL)); 577 578 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 579 return (SET_ERROR(EINVAL)); 580 } else if (alloctype == VDEV_ALLOC_SPARE) { 581 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 582 return (SET_ERROR(EINVAL)); 583 } else if (alloctype == VDEV_ALLOC_L2CACHE) { 584 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 585 return (SET_ERROR(EINVAL)); 586 } else if (alloctype == VDEV_ALLOC_ROOTPOOL) { 587 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) 588 return (SET_ERROR(EINVAL)); 589 } 590 591 /* 592 * The first allocated vdev must be of type 'root'. 593 */ 594 if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL) 595 return (SET_ERROR(EINVAL)); 596 597 /* 598 * Determine whether we're a log vdev. 599 */ 600 islog = 0; 601 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog); 602 if (islog && spa_version(spa) < SPA_VERSION_SLOGS) 603 return (SET_ERROR(ENOTSUP)); 604 605 if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES) 606 return (SET_ERROR(ENOTSUP)); 607 608 /* 609 * Set the nparity property for RAID-Z vdevs. 610 */ 611 nparity = -1ULL; 612 if (ops == &vdev_raidz_ops) { 613 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 614 &nparity) == 0) { 615 if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY) 616 return (SET_ERROR(EINVAL)); 617 /* 618 * Previous versions could only support 1 or 2 parity 619 * device. 620 */ 621 if (nparity > 1 && 622 spa_version(spa) < SPA_VERSION_RAIDZ2) 623 return (SET_ERROR(ENOTSUP)); 624 if (nparity > 2 && 625 spa_version(spa) < SPA_VERSION_RAIDZ3) 626 return (SET_ERROR(ENOTSUP)); 627 } else { 628 /* 629 * We require the parity to be specified for SPAs that 630 * support multiple parity levels. 631 */ 632 if (spa_version(spa) >= SPA_VERSION_RAIDZ2) 633 return (SET_ERROR(EINVAL)); 634 /* 635 * Otherwise, we default to 1 parity device for RAID-Z. 636 */ 637 nparity = 1; 638 } 639 } else { 640 nparity = 0; 641 } 642 ASSERT(nparity != -1ULL); 643 644 /* 645 * If creating a top-level vdev, check for allocation classes input 646 */ 647 if (top_level && alloctype == VDEV_ALLOC_ADD) { 648 char *bias; 649 650 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_ALLOCATION_BIAS, 651 &bias) == 0) { 652 alloc_bias = vdev_derive_alloc_bias(bias); 653 654 /* spa_vdev_add() expects feature to be enabled */ 655 if (alloc_bias != VDEV_BIAS_LOG && 656 spa->spa_load_state != SPA_LOAD_CREATE && 657 !spa_feature_is_enabled(spa, 658 SPA_FEATURE_ALLOCATION_CLASSES)) { 659 return (SET_ERROR(ENOTSUP)); 660 } 661 } 662 } 663 664 vd = vdev_alloc_common(spa, id, guid, ops); 665 vic = &vd->vdev_indirect_config; 666 667 vd->vdev_islog = islog; 668 vd->vdev_nparity = nparity; 669 if (top_level && alloc_bias != VDEV_BIAS_NONE) 670 vd->vdev_alloc_bias = alloc_bias; 671 672 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0) 673 vd->vdev_path = spa_strdup(vd->vdev_path); 674 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0) 675 vd->vdev_devid = spa_strdup(vd->vdev_devid); 676 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH, 677 &vd->vdev_physpath) == 0) 678 vd->vdev_physpath = spa_strdup(vd->vdev_physpath); 679 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0) 680 vd->vdev_fru = spa_strdup(vd->vdev_fru); 681 682 /* 683 * Set the whole_disk property. If it's not specified, leave the value 684 * as -1. 685 */ 686 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 687 &vd->vdev_wholedisk) != 0) 688 vd->vdev_wholedisk = -1ULL; 689 690 ASSERT0(vic->vic_mapping_object); 691 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_OBJECT, 692 &vic->vic_mapping_object); 693 ASSERT0(vic->vic_births_object); 694 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_BIRTHS, 695 &vic->vic_births_object); 696 ASSERT3U(vic->vic_prev_indirect_vdev, ==, UINT64_MAX); 697 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_PREV_INDIRECT_VDEV, 698 &vic->vic_prev_indirect_vdev); 699 700 /* 701 * Look for the 'not present' flag. This will only be set if the device 702 * was not present at the time of import. 703 */ 704 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 705 &vd->vdev_not_present); 706 707 /* 708 * Get the alignment requirement. 709 */ 710 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift); 711 712 /* 713 * Retrieve the vdev creation time. 714 */ 715 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG, 716 &vd->vdev_crtxg); 717 718 /* 719 * If we're a top-level vdev, try to load the allocation parameters. 720 */ 721 if (top_level && 722 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) { 723 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY, 724 &vd->vdev_ms_array); 725 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT, 726 &vd->vdev_ms_shift); 727 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE, 728 &vd->vdev_asize); 729 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING, 730 &vd->vdev_removing); 731 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP, 732 &vd->vdev_top_zap); 733 } else { 734 ASSERT0(vd->vdev_top_zap); 735 } 736 737 if (top_level && alloctype != VDEV_ALLOC_ATTACH) { 738 ASSERT(alloctype == VDEV_ALLOC_LOAD || 739 alloctype == VDEV_ALLOC_ADD || 740 alloctype == VDEV_ALLOC_SPLIT || 741 alloctype == VDEV_ALLOC_ROOTPOOL); 742 /* Note: metaslab_group_create() is now deferred */ 743 } 744 745 if (vd->vdev_ops->vdev_op_leaf && 746 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) { 747 (void) nvlist_lookup_uint64(nv, 748 ZPOOL_CONFIG_VDEV_LEAF_ZAP, &vd->vdev_leaf_zap); 749 } else { 750 ASSERT0(vd->vdev_leaf_zap); 751 } 752 753 /* 754 * If we're a leaf vdev, try to load the DTL object and other state. 755 */ 756 757 if (vd->vdev_ops->vdev_op_leaf && 758 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE || 759 alloctype == VDEV_ALLOC_ROOTPOOL)) { 760 if (alloctype == VDEV_ALLOC_LOAD) { 761 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL, 762 &vd->vdev_dtl_object); 763 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE, 764 &vd->vdev_unspare); 765 } 766 767 if (alloctype == VDEV_ALLOC_ROOTPOOL) { 768 uint64_t spare = 0; 769 770 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 771 &spare) == 0 && spare) 772 spa_spare_add(vd); 773 } 774 775 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, 776 &vd->vdev_offline); 777 778 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG, 779 &vd->vdev_resilver_txg); 780 781 if (nvlist_exists(nv, ZPOOL_CONFIG_RESILVER_DEFER)) 782 vdev_defer_resilver(vd); 783 784 /* 785 * When importing a pool, we want to ignore the persistent fault 786 * state, as the diagnosis made on another system may not be 787 * valid in the current context. Local vdevs will 788 * remain in the faulted state. 789 */ 790 if (spa_load_state(spa) == SPA_LOAD_OPEN) { 791 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, 792 &vd->vdev_faulted); 793 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED, 794 &vd->vdev_degraded); 795 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, 796 &vd->vdev_removed); 797 798 if (vd->vdev_faulted || vd->vdev_degraded) { 799 char *aux; 800 801 vd->vdev_label_aux = 802 VDEV_AUX_ERR_EXCEEDED; 803 if (nvlist_lookup_string(nv, 804 ZPOOL_CONFIG_AUX_STATE, &aux) == 0 && 805 strcmp(aux, "external") == 0) 806 vd->vdev_label_aux = VDEV_AUX_EXTERNAL; 807 } 808 } 809 } 810 811 /* 812 * Add ourselves to the parent's list of children. 813 */ 814 vdev_add_child(parent, vd); 815 816 *vdp = vd; 817 818 return (0); 819 } 820 821 void 822 vdev_free(vdev_t *vd) 823 { 824 spa_t *spa = vd->vdev_spa; 825 826 ASSERT3P(vd->vdev_initialize_thread, ==, NULL); 827 ASSERT3P(vd->vdev_trim_thread, ==, NULL); 828 ASSERT3P(vd->vdev_autotrim_thread, ==, NULL); 829 830 /* 831 * Scan queues are normally destroyed at the end of a scan. If the 832 * queue exists here, that implies the vdev is being removed while 833 * the scan is still running. 834 */ 835 if (vd->vdev_scan_io_queue != NULL) { 836 mutex_enter(&vd->vdev_scan_io_queue_lock); 837 dsl_scan_io_queue_destroy(vd->vdev_scan_io_queue); 838 vd->vdev_scan_io_queue = NULL; 839 mutex_exit(&vd->vdev_scan_io_queue_lock); 840 } 841 842 /* 843 * vdev_free() implies closing the vdev first. This is simpler than 844 * trying to ensure complicated semantics for all callers. 845 */ 846 vdev_close(vd); 847 848 ASSERT(!list_link_active(&vd->vdev_config_dirty_node)); 849 ASSERT(!list_link_active(&vd->vdev_state_dirty_node)); 850 851 /* 852 * Free all children. 853 */ 854 for (int c = 0; c < vd->vdev_children; c++) 855 vdev_free(vd->vdev_child[c]); 856 857 ASSERT(vd->vdev_child == NULL); 858 ASSERT(vd->vdev_guid_sum == vd->vdev_guid); 859 860 /* 861 * Discard allocation state. 862 */ 863 if (vd->vdev_mg != NULL) { 864 vdev_metaslab_fini(vd); 865 metaslab_group_destroy(vd->vdev_mg); 866 vd->vdev_mg = NULL; 867 } 868 869 ASSERT0(vd->vdev_stat.vs_space); 870 ASSERT0(vd->vdev_stat.vs_dspace); 871 ASSERT0(vd->vdev_stat.vs_alloc); 872 873 /* 874 * Remove this vdev from its parent's child list. 875 */ 876 vdev_remove_child(vd->vdev_parent, vd); 877 878 ASSERT(vd->vdev_parent == NULL); 879 ASSERT(!list_link_active(&vd->vdev_leaf_node)); 880 881 /* 882 * Clean up vdev structure. 883 */ 884 vdev_queue_fini(vd); 885 vdev_cache_fini(vd); 886 887 if (vd->vdev_path) 888 spa_strfree(vd->vdev_path); 889 if (vd->vdev_devid) 890 spa_strfree(vd->vdev_devid); 891 if (vd->vdev_physpath) 892 spa_strfree(vd->vdev_physpath); 893 if (vd->vdev_fru) 894 spa_strfree(vd->vdev_fru); 895 896 if (vd->vdev_isspare) 897 spa_spare_remove(vd); 898 if (vd->vdev_isl2cache) 899 spa_l2cache_remove(vd); 900 901 txg_list_destroy(&vd->vdev_ms_list); 902 txg_list_destroy(&vd->vdev_dtl_list); 903 904 mutex_enter(&vd->vdev_dtl_lock); 905 space_map_close(vd->vdev_dtl_sm); 906 for (int t = 0; t < DTL_TYPES; t++) { 907 range_tree_vacate(vd->vdev_dtl[t], NULL, NULL); 908 range_tree_destroy(vd->vdev_dtl[t]); 909 } 910 mutex_exit(&vd->vdev_dtl_lock); 911 912 EQUIV(vd->vdev_indirect_births != NULL, 913 vd->vdev_indirect_mapping != NULL); 914 if (vd->vdev_indirect_births != NULL) { 915 vdev_indirect_mapping_close(vd->vdev_indirect_mapping); 916 vdev_indirect_births_close(vd->vdev_indirect_births); 917 } 918 919 if (vd->vdev_obsolete_sm != NULL) { 920 ASSERT(vd->vdev_removing || 921 vd->vdev_ops == &vdev_indirect_ops); 922 space_map_close(vd->vdev_obsolete_sm); 923 vd->vdev_obsolete_sm = NULL; 924 } 925 range_tree_destroy(vd->vdev_obsolete_segments); 926 rw_destroy(&vd->vdev_indirect_rwlock); 927 mutex_destroy(&vd->vdev_obsolete_lock); 928 929 mutex_destroy(&vd->vdev_dtl_lock); 930 mutex_destroy(&vd->vdev_stat_lock); 931 mutex_destroy(&vd->vdev_probe_lock); 932 mutex_destroy(&vd->vdev_scan_io_queue_lock); 933 mutex_destroy(&vd->vdev_initialize_lock); 934 mutex_destroy(&vd->vdev_initialize_io_lock); 935 cv_destroy(&vd->vdev_initialize_io_cv); 936 cv_destroy(&vd->vdev_initialize_cv); 937 mutex_destroy(&vd->vdev_trim_lock); 938 mutex_destroy(&vd->vdev_autotrim_lock); 939 mutex_destroy(&vd->vdev_trim_io_lock); 940 cv_destroy(&vd->vdev_trim_cv); 941 cv_destroy(&vd->vdev_autotrim_cv); 942 cv_destroy(&vd->vdev_trim_io_cv); 943 944 if (vd == spa->spa_root_vdev) 945 spa->spa_root_vdev = NULL; 946 947 kmem_free(vd, sizeof (vdev_t)); 948 } 949 950 /* 951 * Transfer top-level vdev state from svd to tvd. 952 */ 953 static void 954 vdev_top_transfer(vdev_t *svd, vdev_t *tvd) 955 { 956 spa_t *spa = svd->vdev_spa; 957 metaslab_t *msp; 958 vdev_t *vd; 959 int t; 960 961 ASSERT(tvd == tvd->vdev_top); 962 963 tvd->vdev_ms_array = svd->vdev_ms_array; 964 tvd->vdev_ms_shift = svd->vdev_ms_shift; 965 tvd->vdev_ms_count = svd->vdev_ms_count; 966 tvd->vdev_top_zap = svd->vdev_top_zap; 967 968 svd->vdev_ms_array = 0; 969 svd->vdev_ms_shift = 0; 970 svd->vdev_ms_count = 0; 971 svd->vdev_top_zap = 0; 972 973 if (tvd->vdev_mg) 974 ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg); 975 tvd->vdev_mg = svd->vdev_mg; 976 tvd->vdev_ms = svd->vdev_ms; 977 978 svd->vdev_mg = NULL; 979 svd->vdev_ms = NULL; 980 981 if (tvd->vdev_mg != NULL) 982 tvd->vdev_mg->mg_vd = tvd; 983 984 tvd->vdev_checkpoint_sm = svd->vdev_checkpoint_sm; 985 svd->vdev_checkpoint_sm = NULL; 986 987 tvd->vdev_alloc_bias = svd->vdev_alloc_bias; 988 svd->vdev_alloc_bias = VDEV_BIAS_NONE; 989 990 tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc; 991 tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space; 992 tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace; 993 994 svd->vdev_stat.vs_alloc = 0; 995 svd->vdev_stat.vs_space = 0; 996 svd->vdev_stat.vs_dspace = 0; 997 998 /* 999 * State which may be set on a top-level vdev that's in the 1000 * process of being removed. 1001 */ 1002 ASSERT0(tvd->vdev_indirect_config.vic_births_object); 1003 ASSERT0(tvd->vdev_indirect_config.vic_mapping_object); 1004 ASSERT3U(tvd->vdev_indirect_config.vic_prev_indirect_vdev, ==, -1ULL); 1005 ASSERT3P(tvd->vdev_indirect_mapping, ==, NULL); 1006 ASSERT3P(tvd->vdev_indirect_births, ==, NULL); 1007 ASSERT3P(tvd->vdev_obsolete_sm, ==, NULL); 1008 ASSERT0(tvd->vdev_removing); 1009 tvd->vdev_removing = svd->vdev_removing; 1010 tvd->vdev_indirect_config = svd->vdev_indirect_config; 1011 tvd->vdev_indirect_mapping = svd->vdev_indirect_mapping; 1012 tvd->vdev_indirect_births = svd->vdev_indirect_births; 1013 range_tree_swap(&svd->vdev_obsolete_segments, 1014 &tvd->vdev_obsolete_segments); 1015 tvd->vdev_obsolete_sm = svd->vdev_obsolete_sm; 1016 svd->vdev_indirect_config.vic_mapping_object = 0; 1017 svd->vdev_indirect_config.vic_births_object = 0; 1018 svd->vdev_indirect_config.vic_prev_indirect_vdev = -1ULL; 1019 svd->vdev_indirect_mapping = NULL; 1020 svd->vdev_indirect_births = NULL; 1021 svd->vdev_obsolete_sm = NULL; 1022 svd->vdev_removing = 0; 1023 1024 for (t = 0; t < TXG_SIZE; t++) { 1025 while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL) 1026 (void) txg_list_add(&tvd->vdev_ms_list, msp, t); 1027 while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL) 1028 (void) txg_list_add(&tvd->vdev_dtl_list, vd, t); 1029 if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t)) 1030 (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t); 1031 } 1032 1033 if (list_link_active(&svd->vdev_config_dirty_node)) { 1034 vdev_config_clean(svd); 1035 vdev_config_dirty(tvd); 1036 } 1037 1038 if (list_link_active(&svd->vdev_state_dirty_node)) { 1039 vdev_state_clean(svd); 1040 vdev_state_dirty(tvd); 1041 } 1042 1043 tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio; 1044 svd->vdev_deflate_ratio = 0; 1045 1046 tvd->vdev_islog = svd->vdev_islog; 1047 svd->vdev_islog = 0; 1048 1049 dsl_scan_io_queue_vdev_xfer(svd, tvd); 1050 } 1051 1052 static void 1053 vdev_top_update(vdev_t *tvd, vdev_t *vd) 1054 { 1055 if (vd == NULL) 1056 return; 1057 1058 vd->vdev_top = tvd; 1059 1060 for (int c = 0; c < vd->vdev_children; c++) 1061 vdev_top_update(tvd, vd->vdev_child[c]); 1062 } 1063 1064 /* 1065 * Add a mirror/replacing vdev above an existing vdev. 1066 */ 1067 vdev_t * 1068 vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops) 1069 { 1070 spa_t *spa = cvd->vdev_spa; 1071 vdev_t *pvd = cvd->vdev_parent; 1072 vdev_t *mvd; 1073 1074 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1075 1076 mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops); 1077 1078 mvd->vdev_asize = cvd->vdev_asize; 1079 mvd->vdev_min_asize = cvd->vdev_min_asize; 1080 mvd->vdev_max_asize = cvd->vdev_max_asize; 1081 mvd->vdev_psize = cvd->vdev_psize; 1082 mvd->vdev_ashift = cvd->vdev_ashift; 1083 mvd->vdev_state = cvd->vdev_state; 1084 mvd->vdev_crtxg = cvd->vdev_crtxg; 1085 1086 vdev_remove_child(pvd, cvd); 1087 vdev_add_child(pvd, mvd); 1088 cvd->vdev_id = mvd->vdev_children; 1089 vdev_add_child(mvd, cvd); 1090 vdev_top_update(cvd->vdev_top, cvd->vdev_top); 1091 1092 if (mvd == mvd->vdev_top) 1093 vdev_top_transfer(cvd, mvd); 1094 1095 return (mvd); 1096 } 1097 1098 /* 1099 * Remove a 1-way mirror/replacing vdev from the tree. 1100 */ 1101 void 1102 vdev_remove_parent(vdev_t *cvd) 1103 { 1104 vdev_t *mvd = cvd->vdev_parent; 1105 vdev_t *pvd = mvd->vdev_parent; 1106 1107 ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 1108 1109 ASSERT(mvd->vdev_children == 1); 1110 ASSERT(mvd->vdev_ops == &vdev_mirror_ops || 1111 mvd->vdev_ops == &vdev_replacing_ops || 1112 mvd->vdev_ops == &vdev_spare_ops); 1113 cvd->vdev_ashift = mvd->vdev_ashift; 1114 1115 vdev_remove_child(mvd, cvd); 1116 vdev_remove_child(pvd, mvd); 1117 1118 /* 1119 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid. 1120 * Otherwise, we could have detached an offline device, and when we 1121 * go to import the pool we'll think we have two top-level vdevs, 1122 * instead of a different version of the same top-level vdev. 1123 */ 1124 if (mvd->vdev_top == mvd) { 1125 uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid; 1126 cvd->vdev_orig_guid = cvd->vdev_guid; 1127 cvd->vdev_guid += guid_delta; 1128 cvd->vdev_guid_sum += guid_delta; 1129 } 1130 cvd->vdev_id = mvd->vdev_id; 1131 vdev_add_child(pvd, cvd); 1132 vdev_top_update(cvd->vdev_top, cvd->vdev_top); 1133 1134 if (cvd == cvd->vdev_top) 1135 vdev_top_transfer(mvd, cvd); 1136 1137 ASSERT(mvd->vdev_children == 0); 1138 vdev_free(mvd); 1139 } 1140 1141 static void 1142 vdev_metaslab_group_create(vdev_t *vd) 1143 { 1144 spa_t *spa = vd->vdev_spa; 1145 1146 /* 1147 * metaslab_group_create was delayed until allocation bias was available 1148 */ 1149 if (vd->vdev_mg == NULL) { 1150 metaslab_class_t *mc; 1151 1152 if (vd->vdev_islog && vd->vdev_alloc_bias == VDEV_BIAS_NONE) 1153 vd->vdev_alloc_bias = VDEV_BIAS_LOG; 1154 1155 ASSERT3U(vd->vdev_islog, ==, 1156 (vd->vdev_alloc_bias == VDEV_BIAS_LOG)); 1157 1158 switch (vd->vdev_alloc_bias) { 1159 case VDEV_BIAS_LOG: 1160 mc = spa_log_class(spa); 1161 break; 1162 case VDEV_BIAS_SPECIAL: 1163 mc = spa_special_class(spa); 1164 break; 1165 case VDEV_BIAS_DEDUP: 1166 mc = spa_dedup_class(spa); 1167 break; 1168 default: 1169 mc = spa_normal_class(spa); 1170 } 1171 1172 vd->vdev_mg = metaslab_group_create(mc, vd, 1173 spa->spa_alloc_count); 1174 1175 /* 1176 * The spa ashift values currently only reflect the 1177 * general vdev classes. Class destination is late 1178 * binding so ashift checking had to wait until now 1179 */ 1180 if (vd->vdev_top == vd && vd->vdev_ashift != 0 && 1181 mc == spa_normal_class(spa) && vd->vdev_aux == NULL) { 1182 if (vd->vdev_ashift > spa->spa_max_ashift) 1183 spa->spa_max_ashift = vd->vdev_ashift; 1184 if (vd->vdev_ashift < spa->spa_min_ashift) 1185 spa->spa_min_ashift = vd->vdev_ashift; 1186 } 1187 } 1188 } 1189 1190 int 1191 vdev_metaslab_init(vdev_t *vd, uint64_t txg) 1192 { 1193 spa_t *spa = vd->vdev_spa; 1194 objset_t *mos = spa->spa_meta_objset; 1195 uint64_t m; 1196 uint64_t oldc = vd->vdev_ms_count; 1197 uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift; 1198 metaslab_t **mspp; 1199 int error; 1200 boolean_t expanding = (oldc != 0); 1201 1202 ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER)); 1203 1204 /* 1205 * This vdev is not being allocated from yet or is a hole. 1206 */ 1207 if (vd->vdev_ms_shift == 0) 1208 return (0); 1209 1210 ASSERT(!vd->vdev_ishole); 1211 1212 ASSERT(oldc <= newc); 1213 1214 mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP); 1215 1216 if (expanding) { 1217 bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp)); 1218 kmem_free(vd->vdev_ms, oldc * sizeof (*mspp)); 1219 } 1220 1221 vd->vdev_ms = mspp; 1222 vd->vdev_ms_count = newc; 1223 for (m = oldc; m < newc; m++) { 1224 uint64_t object = 0; 1225 1226 /* 1227 * vdev_ms_array may be 0 if we are creating the "fake" 1228 * metaslabs for an indirect vdev for zdb's leak detection. 1229 * See zdb_leak_init(). 1230 */ 1231 if (txg == 0 && vd->vdev_ms_array != 0) { 1232 error = dmu_read(mos, vd->vdev_ms_array, 1233 m * sizeof (uint64_t), sizeof (uint64_t), &object, 1234 DMU_READ_PREFETCH); 1235 if (error != 0) { 1236 vdev_dbgmsg(vd, "unable to read the metaslab " 1237 "array [error=%d]", error); 1238 return (error); 1239 } 1240 } 1241 1242 #ifndef _KERNEL 1243 /* 1244 * To accomodate zdb_leak_init() fake indirect 1245 * metaslabs, we allocate a metaslab group for 1246 * indirect vdevs which normally don't have one. 1247 */ 1248 if (vd->vdev_mg == NULL) { 1249 ASSERT0(vdev_is_concrete(vd)); 1250 vdev_metaslab_group_create(vd); 1251 } 1252 #endif 1253 error = metaslab_init(vd->vdev_mg, m, object, txg, 1254 &(vd->vdev_ms[m])); 1255 if (error != 0) { 1256 vdev_dbgmsg(vd, "metaslab_init failed [error=%d]", 1257 error); 1258 return (error); 1259 } 1260 } 1261 1262 if (txg == 0) 1263 spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER); 1264 1265 /* 1266 * If the vdev is being removed we don't activate 1267 * the metaslabs since we want to ensure that no new 1268 * allocations are performed on this device. 1269 */ 1270 if (!expanding && !vd->vdev_removing) { 1271 metaslab_group_activate(vd->vdev_mg); 1272 } 1273 1274 if (txg == 0) 1275 spa_config_exit(spa, SCL_ALLOC, FTAG); 1276 1277 /* 1278 * Regardless whether this vdev was just added or it is being 1279 * expanded, the metaslab count has changed. Recalculate the 1280 * block limit. 1281 */ 1282 spa_log_sm_set_blocklimit(spa); 1283 1284 return (0); 1285 } 1286 1287 void 1288 vdev_metaslab_fini(vdev_t *vd) 1289 { 1290 if (vd->vdev_checkpoint_sm != NULL) { 1291 ASSERT(spa_feature_is_active(vd->vdev_spa, 1292 SPA_FEATURE_POOL_CHECKPOINT)); 1293 space_map_close(vd->vdev_checkpoint_sm); 1294 /* 1295 * Even though we close the space map, we need to set its 1296 * pointer to NULL. The reason is that vdev_metaslab_fini() 1297 * may be called multiple times for certain operations 1298 * (i.e. when destroying a pool) so we need to ensure that 1299 * this clause never executes twice. This logic is similar 1300 * to the one used for the vdev_ms clause below. 1301 */ 1302 vd->vdev_checkpoint_sm = NULL; 1303 } 1304 1305 if (vd->vdev_ms != NULL) { 1306 metaslab_group_t *mg = vd->vdev_mg; 1307 metaslab_group_passivate(mg); 1308 1309 uint64_t count = vd->vdev_ms_count; 1310 for (uint64_t m = 0; m < count; m++) { 1311 metaslab_t *msp = vd->vdev_ms[m]; 1312 if (msp != NULL) 1313 metaslab_fini(msp); 1314 } 1315 kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *)); 1316 vd->vdev_ms = NULL; 1317 1318 vd->vdev_ms_count = 0; 1319 1320 for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++) 1321 ASSERT0(mg->mg_histogram[i]); 1322 } 1323 ASSERT0(vd->vdev_ms_count); 1324 } 1325 1326 typedef struct vdev_probe_stats { 1327 boolean_t vps_readable; 1328 boolean_t vps_writeable; 1329 int vps_flags; 1330 } vdev_probe_stats_t; 1331 1332 static void 1333 vdev_probe_done(zio_t *zio) 1334 { 1335 spa_t *spa = zio->io_spa; 1336 vdev_t *vd = zio->io_vd; 1337 vdev_probe_stats_t *vps = zio->io_private; 1338 1339 ASSERT(vd->vdev_probe_zio != NULL); 1340 1341 if (zio->io_type == ZIO_TYPE_READ) { 1342 if (zio->io_error == 0) 1343 vps->vps_readable = 1; 1344 if (zio->io_error == 0 && spa_writeable(spa)) { 1345 zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd, 1346 zio->io_offset, zio->io_size, zio->io_abd, 1347 ZIO_CHECKSUM_OFF, vdev_probe_done, vps, 1348 ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE)); 1349 } else { 1350 abd_free(zio->io_abd); 1351 } 1352 } else if (zio->io_type == ZIO_TYPE_WRITE) { 1353 if (zio->io_error == 0) 1354 vps->vps_writeable = 1; 1355 abd_free(zio->io_abd); 1356 } else if (zio->io_type == ZIO_TYPE_NULL) { 1357 zio_t *pio; 1358 1359 vd->vdev_cant_read |= !vps->vps_readable; 1360 vd->vdev_cant_write |= !vps->vps_writeable; 1361 1362 if (vdev_readable(vd) && 1363 (vdev_writeable(vd) || !spa_writeable(spa))) { 1364 zio->io_error = 0; 1365 } else { 1366 ASSERT(zio->io_error != 0); 1367 vdev_dbgmsg(vd, "failed probe"); 1368 (void) zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE, 1369 spa, vd, NULL, NULL, 0, 0); 1370 zio->io_error = SET_ERROR(ENXIO); 1371 } 1372 1373 mutex_enter(&vd->vdev_probe_lock); 1374 ASSERT(vd->vdev_probe_zio == zio); 1375 vd->vdev_probe_zio = NULL; 1376 mutex_exit(&vd->vdev_probe_lock); 1377 1378 zio_link_t *zl = NULL; 1379 while ((pio = zio_walk_parents(zio, &zl)) != NULL) 1380 if (!vdev_accessible(vd, pio)) 1381 pio->io_error = SET_ERROR(ENXIO); 1382 1383 kmem_free(vps, sizeof (*vps)); 1384 } 1385 } 1386 1387 /* 1388 * Determine whether this device is accessible. 1389 * 1390 * Read and write to several known locations: the pad regions of each 1391 * vdev label but the first, which we leave alone in case it contains 1392 * a VTOC. 1393 */ 1394 zio_t * 1395 vdev_probe(vdev_t *vd, zio_t *zio) 1396 { 1397 spa_t *spa = vd->vdev_spa; 1398 vdev_probe_stats_t *vps = NULL; 1399 zio_t *pio; 1400 1401 ASSERT(vd->vdev_ops->vdev_op_leaf); 1402 1403 /* 1404 * Don't probe the probe. 1405 */ 1406 if (zio && (zio->io_flags & ZIO_FLAG_PROBE)) 1407 return (NULL); 1408 1409 /* 1410 * To prevent 'probe storms' when a device fails, we create 1411 * just one probe i/o at a time. All zios that want to probe 1412 * this vdev will become parents of the probe io. 1413 */ 1414 mutex_enter(&vd->vdev_probe_lock); 1415 1416 if ((pio = vd->vdev_probe_zio) == NULL) { 1417 vps = kmem_zalloc(sizeof (*vps), KM_SLEEP); 1418 1419 vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE | 1420 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE | 1421 ZIO_FLAG_TRYHARD; 1422 1423 if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) { 1424 /* 1425 * vdev_cant_read and vdev_cant_write can only 1426 * transition from TRUE to FALSE when we have the 1427 * SCL_ZIO lock as writer; otherwise they can only 1428 * transition from FALSE to TRUE. This ensures that 1429 * any zio looking at these values can assume that 1430 * failures persist for the life of the I/O. That's 1431 * important because when a device has intermittent 1432 * connectivity problems, we want to ensure that 1433 * they're ascribed to the device (ENXIO) and not 1434 * the zio (EIO). 1435 * 1436 * Since we hold SCL_ZIO as writer here, clear both 1437 * values so the probe can reevaluate from first 1438 * principles. 1439 */ 1440 vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER; 1441 vd->vdev_cant_read = B_FALSE; 1442 vd->vdev_cant_write = B_FALSE; 1443 } 1444 1445 vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd, 1446 vdev_probe_done, vps, 1447 vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE); 1448 1449 /* 1450 * We can't change the vdev state in this context, so we 1451 * kick off an async task to do it on our behalf. 1452 */ 1453 if (zio != NULL) { 1454 vd->vdev_probe_wanted = B_TRUE; 1455 spa_async_request(spa, SPA_ASYNC_PROBE); 1456 } 1457 } 1458 1459 if (zio != NULL) 1460 zio_add_child(zio, pio); 1461 1462 mutex_exit(&vd->vdev_probe_lock); 1463 1464 if (vps == NULL) { 1465 ASSERT(zio != NULL); 1466 return (NULL); 1467 } 1468 1469 for (int l = 1; l < VDEV_LABELS; l++) { 1470 zio_nowait(zio_read_phys(pio, vd, 1471 vdev_label_offset(vd->vdev_psize, l, 1472 offsetof(vdev_label_t, vl_pad2)), VDEV_PAD_SIZE, 1473 abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE), 1474 ZIO_CHECKSUM_OFF, vdev_probe_done, vps, 1475 ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE)); 1476 } 1477 1478 if (zio == NULL) 1479 return (pio); 1480 1481 zio_nowait(pio); 1482 return (NULL); 1483 } 1484 1485 static void 1486 vdev_open_child(void *arg) 1487 { 1488 vdev_t *vd = arg; 1489 1490 vd->vdev_open_thread = curthread; 1491 vd->vdev_open_error = vdev_open(vd); 1492 vd->vdev_open_thread = NULL; 1493 } 1494 1495 boolean_t 1496 vdev_uses_zvols(vdev_t *vd) 1497 { 1498 if (vd->vdev_path && strncmp(vd->vdev_path, ZVOL_DIR, 1499 strlen(ZVOL_DIR)) == 0) 1500 return (B_TRUE); 1501 for (int c = 0; c < vd->vdev_children; c++) 1502 if (vdev_uses_zvols(vd->vdev_child[c])) 1503 return (B_TRUE); 1504 return (B_FALSE); 1505 } 1506 1507 void 1508 vdev_open_children(vdev_t *vd) 1509 { 1510 taskq_t *tq; 1511 int children = vd->vdev_children; 1512 1513 /* 1514 * in order to handle pools on top of zvols, do the opens 1515 * in a single thread so that the same thread holds the 1516 * spa_namespace_lock 1517 */ 1518 if (vdev_uses_zvols(vd)) { 1519 retry_sync: 1520 for (int c = 0; c < children; c++) 1521 vd->vdev_child[c]->vdev_open_error = 1522 vdev_open(vd->vdev_child[c]); 1523 } else { 1524 tq = taskq_create("vdev_open", children, minclsyspri, 1525 children, children, TASKQ_PREPOPULATE); 1526 if (tq == NULL) 1527 goto retry_sync; 1528 1529 for (int c = 0; c < children; c++) 1530 VERIFY(taskq_dispatch(tq, vdev_open_child, 1531 vd->vdev_child[c], TQ_SLEEP) != TASKQID_INVALID); 1532 1533 taskq_destroy(tq); 1534 } 1535 1536 vd->vdev_nonrot = B_TRUE; 1537 1538 for (int c = 0; c < children; c++) 1539 vd->vdev_nonrot &= vd->vdev_child[c]->vdev_nonrot; 1540 } 1541 1542 /* 1543 * Compute the raidz-deflation ratio. Note, we hard-code 1544 * in 128k (1 << 17) because it is the "typical" blocksize. 1545 * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change, 1546 * otherwise it would inconsistently account for existing bp's. 1547 */ 1548 static void 1549 vdev_set_deflate_ratio(vdev_t *vd) 1550 { 1551 if (vd == vd->vdev_top && !vd->vdev_ishole && vd->vdev_ashift != 0) { 1552 vd->vdev_deflate_ratio = (1 << 17) / 1553 (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT); 1554 } 1555 } 1556 1557 /* 1558 * Prepare a virtual device for access. 1559 */ 1560 int 1561 vdev_open(vdev_t *vd) 1562 { 1563 spa_t *spa = vd->vdev_spa; 1564 int error; 1565 uint64_t osize = 0; 1566 uint64_t max_osize = 0; 1567 uint64_t asize, max_asize, psize; 1568 uint64_t ashift = 0; 1569 1570 ASSERT(vd->vdev_open_thread == curthread || 1571 spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 1572 ASSERT(vd->vdev_state == VDEV_STATE_CLOSED || 1573 vd->vdev_state == VDEV_STATE_CANT_OPEN || 1574 vd->vdev_state == VDEV_STATE_OFFLINE); 1575 1576 vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 1577 vd->vdev_cant_read = B_FALSE; 1578 vd->vdev_cant_write = B_FALSE; 1579 vd->vdev_min_asize = vdev_get_min_asize(vd); 1580 1581 /* 1582 * If this vdev is not removed, check its fault status. If it's 1583 * faulted, bail out of the open. 1584 */ 1585 if (!vd->vdev_removed && vd->vdev_faulted) { 1586 ASSERT(vd->vdev_children == 0); 1587 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED || 1588 vd->vdev_label_aux == VDEV_AUX_EXTERNAL); 1589 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 1590 vd->vdev_label_aux); 1591 return (SET_ERROR(ENXIO)); 1592 } else if (vd->vdev_offline) { 1593 ASSERT(vd->vdev_children == 0); 1594 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE); 1595 return (SET_ERROR(ENXIO)); 1596 } 1597 1598 error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize, &ashift); 1599 1600 /* 1601 * Reset the vdev_reopening flag so that we actually close 1602 * the vdev on error. 1603 */ 1604 vd->vdev_reopening = B_FALSE; 1605 if (zio_injection_enabled && error == 0) 1606 error = zio_handle_device_injection(vd, NULL, ENXIO); 1607 1608 if (error) { 1609 if (vd->vdev_removed && 1610 vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED) 1611 vd->vdev_removed = B_FALSE; 1612 1613 if (vd->vdev_stat.vs_aux == VDEV_AUX_CHILDREN_OFFLINE) { 1614 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, 1615 vd->vdev_stat.vs_aux); 1616 } else { 1617 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1618 vd->vdev_stat.vs_aux); 1619 } 1620 return (error); 1621 } 1622 1623 vd->vdev_removed = B_FALSE; 1624 1625 /* 1626 * Recheck the faulted flag now that we have confirmed that 1627 * the vdev is accessible. If we're faulted, bail. 1628 */ 1629 if (vd->vdev_faulted) { 1630 ASSERT(vd->vdev_children == 0); 1631 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED || 1632 vd->vdev_label_aux == VDEV_AUX_EXTERNAL); 1633 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 1634 vd->vdev_label_aux); 1635 return (SET_ERROR(ENXIO)); 1636 } 1637 1638 if (vd->vdev_degraded) { 1639 ASSERT(vd->vdev_children == 0); 1640 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 1641 VDEV_AUX_ERR_EXCEEDED); 1642 } else { 1643 vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0); 1644 } 1645 1646 /* 1647 * For hole or missing vdevs we just return success. 1648 */ 1649 if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) 1650 return (0); 1651 1652 for (int c = 0; c < vd->vdev_children; c++) { 1653 if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) { 1654 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED, 1655 VDEV_AUX_NONE); 1656 break; 1657 } 1658 } 1659 1660 osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t)); 1661 max_osize = P2ALIGN(max_osize, (uint64_t)sizeof (vdev_label_t)); 1662 1663 if (vd->vdev_children == 0) { 1664 if (osize < SPA_MINDEVSIZE) { 1665 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1666 VDEV_AUX_TOO_SMALL); 1667 return (SET_ERROR(EOVERFLOW)); 1668 } 1669 psize = osize; 1670 asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE); 1671 max_asize = max_osize - (VDEV_LABEL_START_SIZE + 1672 VDEV_LABEL_END_SIZE); 1673 } else { 1674 if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE - 1675 (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) { 1676 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1677 VDEV_AUX_TOO_SMALL); 1678 return (SET_ERROR(EOVERFLOW)); 1679 } 1680 psize = 0; 1681 asize = osize; 1682 max_asize = max_osize; 1683 } 1684 1685 vd->vdev_psize = psize; 1686 1687 /* 1688 * Make sure the allocatable size hasn't shrunk too much. 1689 */ 1690 if (asize < vd->vdev_min_asize) { 1691 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1692 VDEV_AUX_BAD_LABEL); 1693 return (SET_ERROR(EINVAL)); 1694 } 1695 1696 if (vd->vdev_asize == 0) { 1697 /* 1698 * This is the first-ever open, so use the computed values. 1699 * For compatibility, a different ashift can be requested. 1700 */ 1701 vd->vdev_asize = asize; 1702 vd->vdev_max_asize = max_asize; 1703 if (vd->vdev_ashift == 0) { 1704 vd->vdev_ashift = ashift; /* use detected value */ 1705 } 1706 if (vd->vdev_ashift != 0 && (vd->vdev_ashift < ASHIFT_MIN || 1707 vd->vdev_ashift > ASHIFT_MAX)) { 1708 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1709 VDEV_AUX_BAD_ASHIFT); 1710 return (SET_ERROR(EDOM)); 1711 } 1712 } else { 1713 /* 1714 * Detect if the alignment requirement has increased. 1715 * We don't want to make the pool unavailable, just 1716 * post an event instead. 1717 */ 1718 if (ashift > vd->vdev_top->vdev_ashift && 1719 vd->vdev_ops->vdev_op_leaf) { 1720 (void) zfs_ereport_post( 1721 FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT, 1722 spa, vd, NULL, NULL, 0, 0); 1723 } 1724 1725 vd->vdev_max_asize = max_asize; 1726 } 1727 1728 /* 1729 * If all children are healthy we update asize if either: 1730 * The asize has increased, due to a device expansion caused by dynamic 1731 * LUN growth or vdev replacement, and automatic expansion is enabled; 1732 * making the additional space available. 1733 * 1734 * The asize has decreased, due to a device shrink usually caused by a 1735 * vdev replace with a smaller device. This ensures that calculations 1736 * based of max_asize and asize e.g. esize are always valid. It's safe 1737 * to do this as we've already validated that asize is greater than 1738 * vdev_min_asize. 1739 */ 1740 if (vd->vdev_state == VDEV_STATE_HEALTHY && 1741 ((asize > vd->vdev_asize && 1742 (vd->vdev_expanding || spa->spa_autoexpand)) || 1743 (asize < vd->vdev_asize))) 1744 vd->vdev_asize = asize; 1745 1746 vdev_set_min_asize(vd); 1747 1748 /* 1749 * Ensure we can issue some IO before declaring the 1750 * vdev open for business. 1751 */ 1752 if (vd->vdev_ops->vdev_op_leaf && 1753 (error = zio_wait(vdev_probe(vd, NULL))) != 0) { 1754 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED, 1755 VDEV_AUX_ERR_EXCEEDED); 1756 return (error); 1757 } 1758 1759 /* 1760 * Track the min and max ashift values for normal data devices. 1761 * 1762 * DJB - TBD these should perhaps be tracked per allocation class 1763 * (e.g. spa_min_ashift is used to round up post compression buffers) 1764 */ 1765 if (vd->vdev_top == vd && vd->vdev_ashift != 0 && 1766 vd->vdev_alloc_bias == VDEV_BIAS_NONE && 1767 vd->vdev_aux == NULL) { 1768 if (vd->vdev_ashift > spa->spa_max_ashift) 1769 spa->spa_max_ashift = vd->vdev_ashift; 1770 if (vd->vdev_ashift < spa->spa_min_ashift) 1771 spa->spa_min_ashift = vd->vdev_ashift; 1772 } 1773 1774 /* 1775 * If this is a leaf vdev, assess whether a resilver is needed. 1776 * But don't do this if we are doing a reopen for a scrub, since 1777 * this would just restart the scrub we are already doing. 1778 */ 1779 if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen) 1780 dsl_scan_assess_vdev(spa->spa_dsl_pool, vd); 1781 1782 return (0); 1783 } 1784 1785 /* 1786 * Called once the vdevs are all opened, this routine validates the label 1787 * contents. This needs to be done before vdev_load() so that we don't 1788 * inadvertently do repair I/Os to the wrong device. 1789 * 1790 * This function will only return failure if one of the vdevs indicates that it 1791 * has since been destroyed or exported. This is only possible if 1792 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state 1793 * will be updated but the function will return 0. 1794 */ 1795 int 1796 vdev_validate(vdev_t *vd) 1797 { 1798 spa_t *spa = vd->vdev_spa; 1799 nvlist_t *label; 1800 uint64_t guid = 0, aux_guid = 0, top_guid; 1801 uint64_t state; 1802 nvlist_t *nvl; 1803 uint64_t txg; 1804 1805 if (vdev_validate_skip) 1806 return (0); 1807 1808 for (uint64_t c = 0; c < vd->vdev_children; c++) 1809 if (vdev_validate(vd->vdev_child[c]) != 0) 1810 return (SET_ERROR(EBADF)); 1811 1812 /* 1813 * If the device has already failed, or was marked offline, don't do 1814 * any further validation. Otherwise, label I/O will fail and we will 1815 * overwrite the previous state. 1816 */ 1817 if (!vd->vdev_ops->vdev_op_leaf || !vdev_readable(vd)) 1818 return (0); 1819 1820 /* 1821 * If we are performing an extreme rewind, we allow for a label that 1822 * was modified at a point after the current txg. 1823 * If config lock is not held do not check for the txg. spa_sync could 1824 * be updating the vdev's label before updating spa_last_synced_txg. 1825 */ 1826 if (spa->spa_extreme_rewind || spa_last_synced_txg(spa) == 0 || 1827 spa_config_held(spa, SCL_CONFIG, RW_WRITER) != SCL_CONFIG) 1828 txg = UINT64_MAX; 1829 else 1830 txg = spa_last_synced_txg(spa); 1831 1832 if ((label = vdev_label_read_config(vd, txg)) == NULL) { 1833 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 1834 VDEV_AUX_BAD_LABEL); 1835 vdev_dbgmsg(vd, "vdev_validate: failed reading config for " 1836 "txg %llu", (u_longlong_t)txg); 1837 return (0); 1838 } 1839 1840 /* 1841 * Determine if this vdev has been split off into another 1842 * pool. If so, then refuse to open it. 1843 */ 1844 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID, 1845 &aux_guid) == 0 && aux_guid == spa_guid(spa)) { 1846 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1847 VDEV_AUX_SPLIT_POOL); 1848 nvlist_free(label); 1849 vdev_dbgmsg(vd, "vdev_validate: vdev split into other pool"); 1850 return (0); 1851 } 1852 1853 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &guid) != 0) { 1854 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1855 VDEV_AUX_CORRUPT_DATA); 1856 nvlist_free(label); 1857 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 1858 ZPOOL_CONFIG_POOL_GUID); 1859 return (0); 1860 } 1861 1862 /* 1863 * If config is not trusted then ignore the spa guid check. This is 1864 * necessary because if the machine crashed during a re-guid the new 1865 * guid might have been written to all of the vdev labels, but not the 1866 * cached config. The check will be performed again once we have the 1867 * trusted config from the MOS. 1868 */ 1869 if (spa->spa_trust_config && guid != spa_guid(spa)) { 1870 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1871 VDEV_AUX_CORRUPT_DATA); 1872 nvlist_free(label); 1873 vdev_dbgmsg(vd, "vdev_validate: vdev label pool_guid doesn't " 1874 "match config (%llu != %llu)", (u_longlong_t)guid, 1875 (u_longlong_t)spa_guid(spa)); 1876 return (0); 1877 } 1878 1879 if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl) 1880 != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID, 1881 &aux_guid) != 0) 1882 aux_guid = 0; 1883 1884 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0) { 1885 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1886 VDEV_AUX_CORRUPT_DATA); 1887 nvlist_free(label); 1888 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 1889 ZPOOL_CONFIG_GUID); 1890 return (0); 1891 } 1892 1893 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID, &top_guid) 1894 != 0) { 1895 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1896 VDEV_AUX_CORRUPT_DATA); 1897 nvlist_free(label); 1898 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 1899 ZPOOL_CONFIG_TOP_GUID); 1900 return (0); 1901 } 1902 1903 /* 1904 * If this vdev just became a top-level vdev because its sibling was 1905 * detached, it will have adopted the parent's vdev guid -- but the 1906 * label may or may not be on disk yet. Fortunately, either version 1907 * of the label will have the same top guid, so if we're a top-level 1908 * vdev, we can safely compare to that instead. 1909 * However, if the config comes from a cachefile that failed to update 1910 * after the detach, a top-level vdev will appear as a non top-level 1911 * vdev in the config. Also relax the constraints if we perform an 1912 * extreme rewind. 1913 * 1914 * If we split this vdev off instead, then we also check the 1915 * original pool's guid. We don't want to consider the vdev 1916 * corrupt if it is partway through a split operation. 1917 */ 1918 if (vd->vdev_guid != guid && vd->vdev_guid != aux_guid) { 1919 boolean_t mismatch = B_FALSE; 1920 if (spa->spa_trust_config && !spa->spa_extreme_rewind) { 1921 if (vd != vd->vdev_top || vd->vdev_guid != top_guid) 1922 mismatch = B_TRUE; 1923 } else { 1924 if (vd->vdev_guid != top_guid && 1925 vd->vdev_top->vdev_guid != guid) 1926 mismatch = B_TRUE; 1927 } 1928 1929 if (mismatch) { 1930 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1931 VDEV_AUX_CORRUPT_DATA); 1932 nvlist_free(label); 1933 vdev_dbgmsg(vd, "vdev_validate: config guid " 1934 "doesn't match label guid"); 1935 vdev_dbgmsg(vd, "CONFIG: guid %llu, top_guid %llu", 1936 (u_longlong_t)vd->vdev_guid, 1937 (u_longlong_t)vd->vdev_top->vdev_guid); 1938 vdev_dbgmsg(vd, "LABEL: guid %llu, top_guid %llu, " 1939 "aux_guid %llu", (u_longlong_t)guid, 1940 (u_longlong_t)top_guid, (u_longlong_t)aux_guid); 1941 return (0); 1942 } 1943 } 1944 1945 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, 1946 &state) != 0) { 1947 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 1948 VDEV_AUX_CORRUPT_DATA); 1949 nvlist_free(label); 1950 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label", 1951 ZPOOL_CONFIG_POOL_STATE); 1952 return (0); 1953 } 1954 1955 nvlist_free(label); 1956 1957 /* 1958 * If this is a verbatim import, no need to check the 1959 * state of the pool. 1960 */ 1961 if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) && 1962 spa_load_state(spa) == SPA_LOAD_OPEN && 1963 state != POOL_STATE_ACTIVE) { 1964 vdev_dbgmsg(vd, "vdev_validate: invalid pool state (%llu) " 1965 "for spa %s", (u_longlong_t)state, spa->spa_name); 1966 return (SET_ERROR(EBADF)); 1967 } 1968 1969 /* 1970 * If we were able to open and validate a vdev that was 1971 * previously marked permanently unavailable, clear that state 1972 * now. 1973 */ 1974 if (vd->vdev_not_present) 1975 vd->vdev_not_present = 0; 1976 1977 return (0); 1978 } 1979 1980 static void 1981 vdev_copy_path_impl(vdev_t *svd, vdev_t *dvd) 1982 { 1983 if (svd->vdev_path != NULL && dvd->vdev_path != NULL) { 1984 if (strcmp(svd->vdev_path, dvd->vdev_path) != 0) { 1985 zfs_dbgmsg("vdev_copy_path: vdev %llu: path changed " 1986 "from '%s' to '%s'", (u_longlong_t)dvd->vdev_guid, 1987 dvd->vdev_path, svd->vdev_path); 1988 spa_strfree(dvd->vdev_path); 1989 dvd->vdev_path = spa_strdup(svd->vdev_path); 1990 } 1991 } else if (svd->vdev_path != NULL) { 1992 dvd->vdev_path = spa_strdup(svd->vdev_path); 1993 zfs_dbgmsg("vdev_copy_path: vdev %llu: path set to '%s'", 1994 (u_longlong_t)dvd->vdev_guid, dvd->vdev_path); 1995 } 1996 } 1997 1998 /* 1999 * Recursively copy vdev paths from one vdev to another. Source and destination 2000 * vdev trees must have same geometry otherwise return error. Intended to copy 2001 * paths from userland config into MOS config. 2002 */ 2003 int 2004 vdev_copy_path_strict(vdev_t *svd, vdev_t *dvd) 2005 { 2006 if ((svd->vdev_ops == &vdev_missing_ops) || 2007 (svd->vdev_ishole && dvd->vdev_ishole) || 2008 (dvd->vdev_ops == &vdev_indirect_ops)) 2009 return (0); 2010 2011 if (svd->vdev_ops != dvd->vdev_ops) { 2012 vdev_dbgmsg(svd, "vdev_copy_path: vdev type mismatch: %s != %s", 2013 svd->vdev_ops->vdev_op_type, dvd->vdev_ops->vdev_op_type); 2014 return (SET_ERROR(EINVAL)); 2015 } 2016 2017 if (svd->vdev_guid != dvd->vdev_guid) { 2018 vdev_dbgmsg(svd, "vdev_copy_path: guids mismatch (%llu != " 2019 "%llu)", (u_longlong_t)svd->vdev_guid, 2020 (u_longlong_t)dvd->vdev_guid); 2021 return (SET_ERROR(EINVAL)); 2022 } 2023 2024 if (svd->vdev_children != dvd->vdev_children) { 2025 vdev_dbgmsg(svd, "vdev_copy_path: children count mismatch: " 2026 "%llu != %llu", (u_longlong_t)svd->vdev_children, 2027 (u_longlong_t)dvd->vdev_children); 2028 return (SET_ERROR(EINVAL)); 2029 } 2030 2031 for (uint64_t i = 0; i < svd->vdev_children; i++) { 2032 int error = vdev_copy_path_strict(svd->vdev_child[i], 2033 dvd->vdev_child[i]); 2034 if (error != 0) 2035 return (error); 2036 } 2037 2038 if (svd->vdev_ops->vdev_op_leaf) 2039 vdev_copy_path_impl(svd, dvd); 2040 2041 return (0); 2042 } 2043 2044 static void 2045 vdev_copy_path_search(vdev_t *stvd, vdev_t *dvd) 2046 { 2047 ASSERT(stvd->vdev_top == stvd); 2048 ASSERT3U(stvd->vdev_id, ==, dvd->vdev_top->vdev_id); 2049 2050 for (uint64_t i = 0; i < dvd->vdev_children; i++) { 2051 vdev_copy_path_search(stvd, dvd->vdev_child[i]); 2052 } 2053 2054 if (!dvd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(dvd)) 2055 return; 2056 2057 /* 2058 * The idea here is that while a vdev can shift positions within 2059 * a top vdev (when replacing, attaching mirror, etc.) it cannot 2060 * step outside of it. 2061 */ 2062 vdev_t *vd = vdev_lookup_by_guid(stvd, dvd->vdev_guid); 2063 2064 if (vd == NULL || vd->vdev_ops != dvd->vdev_ops) 2065 return; 2066 2067 ASSERT(vd->vdev_ops->vdev_op_leaf); 2068 2069 vdev_copy_path_impl(vd, dvd); 2070 } 2071 2072 /* 2073 * Recursively copy vdev paths from one root vdev to another. Source and 2074 * destination vdev trees may differ in geometry. For each destination leaf 2075 * vdev, search a vdev with the same guid and top vdev id in the source. 2076 * Intended to copy paths from userland config into MOS config. 2077 */ 2078 void 2079 vdev_copy_path_relaxed(vdev_t *srvd, vdev_t *drvd) 2080 { 2081 uint64_t children = MIN(srvd->vdev_children, drvd->vdev_children); 2082 ASSERT(srvd->vdev_ops == &vdev_root_ops); 2083 ASSERT(drvd->vdev_ops == &vdev_root_ops); 2084 2085 for (uint64_t i = 0; i < children; i++) { 2086 vdev_copy_path_search(srvd->vdev_child[i], 2087 drvd->vdev_child[i]); 2088 } 2089 } 2090 2091 /* 2092 * Close a virtual device. 2093 */ 2094 void 2095 vdev_close(vdev_t *vd) 2096 { 2097 spa_t *spa = vd->vdev_spa; 2098 vdev_t *pvd = vd->vdev_parent; 2099 2100 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 2101 2102 /* 2103 * If our parent is reopening, then we are as well, unless we are 2104 * going offline. 2105 */ 2106 if (pvd != NULL && pvd->vdev_reopening) 2107 vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline); 2108 2109 vd->vdev_ops->vdev_op_close(vd); 2110 2111 vdev_cache_purge(vd); 2112 2113 /* 2114 * We record the previous state before we close it, so that if we are 2115 * doing a reopen(), we don't generate FMA ereports if we notice that 2116 * it's still faulted. 2117 */ 2118 vd->vdev_prevstate = vd->vdev_state; 2119 2120 if (vd->vdev_offline) 2121 vd->vdev_state = VDEV_STATE_OFFLINE; 2122 else 2123 vd->vdev_state = VDEV_STATE_CLOSED; 2124 vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 2125 } 2126 2127 void 2128 vdev_hold(vdev_t *vd) 2129 { 2130 spa_t *spa = vd->vdev_spa; 2131 2132 ASSERT(spa_is_root(spa)); 2133 if (spa->spa_state == POOL_STATE_UNINITIALIZED) 2134 return; 2135 2136 for (int c = 0; c < vd->vdev_children; c++) 2137 vdev_hold(vd->vdev_child[c]); 2138 2139 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_ops->vdev_op_hold != NULL) 2140 vd->vdev_ops->vdev_op_hold(vd); 2141 } 2142 2143 void 2144 vdev_rele(vdev_t *vd) 2145 { 2146 spa_t *spa = vd->vdev_spa; 2147 2148 ASSERT(spa_is_root(spa)); 2149 for (int c = 0; c < vd->vdev_children; c++) 2150 vdev_rele(vd->vdev_child[c]); 2151 2152 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_ops->vdev_op_rele != NULL) 2153 vd->vdev_ops->vdev_op_rele(vd); 2154 } 2155 2156 /* 2157 * Reopen all interior vdevs and any unopened leaves. We don't actually 2158 * reopen leaf vdevs which had previously been opened as they might deadlock 2159 * on the spa_config_lock. Instead we only obtain the leaf's physical size. 2160 * If the leaf has never been opened then open it, as usual. 2161 */ 2162 void 2163 vdev_reopen(vdev_t *vd) 2164 { 2165 spa_t *spa = vd->vdev_spa; 2166 2167 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 2168 2169 /* set the reopening flag unless we're taking the vdev offline */ 2170 vd->vdev_reopening = !vd->vdev_offline; 2171 vdev_close(vd); 2172 (void) vdev_open(vd); 2173 2174 /* 2175 * Call vdev_validate() here to make sure we have the same device. 2176 * Otherwise, a device with an invalid label could be successfully 2177 * opened in response to vdev_reopen(). 2178 */ 2179 if (vd->vdev_aux) { 2180 (void) vdev_validate_aux(vd); 2181 if (vdev_readable(vd) && vdev_writeable(vd) && 2182 vd->vdev_aux == &spa->spa_l2cache && 2183 !l2arc_vdev_present(vd)) 2184 l2arc_add_vdev(spa, vd); 2185 } else { 2186 (void) vdev_validate(vd); 2187 } 2188 2189 /* 2190 * Reassess parent vdev's health. 2191 */ 2192 vdev_propagate_state(vd); 2193 } 2194 2195 int 2196 vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing) 2197 { 2198 int error; 2199 2200 /* 2201 * Normally, partial opens (e.g. of a mirror) are allowed. 2202 * For a create, however, we want to fail the request if 2203 * there are any components we can't open. 2204 */ 2205 error = vdev_open(vd); 2206 2207 if (error || vd->vdev_state != VDEV_STATE_HEALTHY) { 2208 vdev_close(vd); 2209 return (error ? error : ENXIO); 2210 } 2211 2212 /* 2213 * Recursively load DTLs and initialize all labels. 2214 */ 2215 if ((error = vdev_dtl_load(vd)) != 0 || 2216 (error = vdev_label_init(vd, txg, isreplacing ? 2217 VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) { 2218 vdev_close(vd); 2219 return (error); 2220 } 2221 2222 return (0); 2223 } 2224 2225 void 2226 vdev_metaslab_set_size(vdev_t *vd) 2227 { 2228 uint64_t asize = vd->vdev_asize; 2229 uint64_t ms_count = asize >> zfs_vdev_default_ms_shift; 2230 uint64_t ms_shift; 2231 2232 /* BEGIN CSTYLED */ 2233 /* 2234 * There are two dimensions to the metaslab sizing calculation: 2235 * the size of the metaslab and the count of metaslabs per vdev. 2236 * 2237 * The default values used below are a good balance between memory 2238 * usage (larger metaslab size means more memory needed for loaded 2239 * metaslabs; more metaslabs means more memory needed for the 2240 * metaslab_t structs), metaslab load time (larger metaslabs take 2241 * longer to load), and metaslab sync time (more metaslabs means 2242 * more time spent syncing all of them). 2243 * 2244 * In general, we aim for zfs_vdev_default_ms_count (200) metaslabs. 2245 * The range of the dimensions are as follows: 2246 * 2247 * 2^29 <= ms_size <= 2^34 2248 * 16 <= ms_count <= 131,072 2249 * 2250 * On the lower end of vdev sizes, we aim for metaslabs sizes of 2251 * at least 512MB (2^29) to minimize fragmentation effects when 2252 * testing with smaller devices. However, the count constraint 2253 * of at least 16 metaslabs will override this minimum size goal. 2254 * 2255 * On the upper end of vdev sizes, we aim for a maximum metaslab 2256 * size of 16GB. However, we will cap the total count to 2^17 2257 * metaslabs to keep our memory footprint in check and let the 2258 * metaslab size grow from there if that limit is hit. 2259 * 2260 * The net effect of applying above constrains is summarized below. 2261 * 2262 * vdev size metaslab count 2263 * --------------|----------------- 2264 * < 8GB ~16 2265 * 8GB - 100GB one per 512MB 2266 * 100GB - 3TB ~200 2267 * 3TB - 2PB one per 16GB 2268 * > 2PB ~131,072 2269 * -------------------------------- 2270 * 2271 * Finally, note that all of the above calculate the initial 2272 * number of metaslabs. Expanding a top-level vdev will result 2273 * in additional metaslabs being allocated making it possible 2274 * to exceed the zfs_vdev_ms_count_limit. 2275 */ 2276 /* END CSTYLED */ 2277 2278 if (ms_count < zfs_vdev_min_ms_count) 2279 ms_shift = highbit64(asize / zfs_vdev_min_ms_count); 2280 else if (ms_count > zfs_vdev_default_ms_count) 2281 ms_shift = highbit64(asize / zfs_vdev_default_ms_count); 2282 else 2283 ms_shift = zfs_vdev_default_ms_shift; 2284 2285 if (ms_shift < SPA_MAXBLOCKSHIFT) { 2286 ms_shift = SPA_MAXBLOCKSHIFT; 2287 } else if (ms_shift > zfs_vdev_max_ms_shift) { 2288 ms_shift = zfs_vdev_max_ms_shift; 2289 /* cap the total count to constrain memory footprint */ 2290 if ((asize >> ms_shift) > zfs_vdev_ms_count_limit) 2291 ms_shift = highbit64(asize / zfs_vdev_ms_count_limit); 2292 } 2293 2294 vd->vdev_ms_shift = ms_shift; 2295 ASSERT3U(vd->vdev_ms_shift, >=, SPA_MAXBLOCKSHIFT); 2296 } 2297 2298 void 2299 vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg) 2300 { 2301 ASSERT(vd == vd->vdev_top); 2302 /* indirect vdevs don't have metaslabs or dtls */ 2303 ASSERT(vdev_is_concrete(vd) || flags == 0); 2304 ASSERT(ISP2(flags)); 2305 ASSERT(spa_writeable(vd->vdev_spa)); 2306 2307 if (flags & VDD_METASLAB) 2308 (void) txg_list_add(&vd->vdev_ms_list, arg, txg); 2309 2310 if (flags & VDD_DTL) 2311 (void) txg_list_add(&vd->vdev_dtl_list, arg, txg); 2312 2313 (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg); 2314 } 2315 2316 void 2317 vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg) 2318 { 2319 for (int c = 0; c < vd->vdev_children; c++) 2320 vdev_dirty_leaves(vd->vdev_child[c], flags, txg); 2321 2322 if (vd->vdev_ops->vdev_op_leaf) 2323 vdev_dirty(vd->vdev_top, flags, vd, txg); 2324 } 2325 2326 /* 2327 * DTLs. 2328 * 2329 * A vdev's DTL (dirty time log) is the set of transaction groups for which 2330 * the vdev has less than perfect replication. There are four kinds of DTL: 2331 * 2332 * DTL_MISSING: txgs for which the vdev has no valid copies of the data 2333 * 2334 * DTL_PARTIAL: txgs for which data is available, but not fully replicated 2335 * 2336 * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon 2337 * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of 2338 * txgs that was scrubbed. 2339 * 2340 * DTL_OUTAGE: txgs which cannot currently be read, whether due to 2341 * persistent errors or just some device being offline. 2342 * Unlike the other three, the DTL_OUTAGE map is not generally 2343 * maintained; it's only computed when needed, typically to 2344 * determine whether a device can be detached. 2345 * 2346 * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device 2347 * either has the data or it doesn't. 2348 * 2349 * For interior vdevs such as mirror and RAID-Z the picture is more complex. 2350 * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because 2351 * if any child is less than fully replicated, then so is its parent. 2352 * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs, 2353 * comprising only those txgs which appear in 'maxfaults' or more children; 2354 * those are the txgs we don't have enough replication to read. For example, 2355 * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2); 2356 * thus, its DTL_MISSING consists of the set of txgs that appear in more than 2357 * two child DTL_MISSING maps. 2358 * 2359 * It should be clear from the above that to compute the DTLs and outage maps 2360 * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps. 2361 * Therefore, that is all we keep on disk. When loading the pool, or after 2362 * a configuration change, we generate all other DTLs from first principles. 2363 */ 2364 void 2365 vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 2366 { 2367 range_tree_t *rt = vd->vdev_dtl[t]; 2368 2369 ASSERT(t < DTL_TYPES); 2370 ASSERT(vd != vd->vdev_spa->spa_root_vdev); 2371 ASSERT(spa_writeable(vd->vdev_spa)); 2372 2373 mutex_enter(&vd->vdev_dtl_lock); 2374 if (!range_tree_contains(rt, txg, size)) 2375 range_tree_add(rt, txg, size); 2376 mutex_exit(&vd->vdev_dtl_lock); 2377 } 2378 2379 boolean_t 2380 vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size) 2381 { 2382 range_tree_t *rt = vd->vdev_dtl[t]; 2383 boolean_t dirty = B_FALSE; 2384 2385 ASSERT(t < DTL_TYPES); 2386 ASSERT(vd != vd->vdev_spa->spa_root_vdev); 2387 2388 /* 2389 * While we are loading the pool, the DTLs have not been loaded yet. 2390 * Ignore the DTLs and try all devices. This avoids a recursive 2391 * mutex enter on the vdev_dtl_lock, and also makes us try hard 2392 * when loading the pool (relying on the checksum to ensure that 2393 * we get the right data -- note that we while loading, we are 2394 * only reading the MOS, which is always checksummed). 2395 */ 2396 if (vd->vdev_spa->spa_load_state != SPA_LOAD_NONE) 2397 return (B_FALSE); 2398 2399 mutex_enter(&vd->vdev_dtl_lock); 2400 if (!range_tree_is_empty(rt)) 2401 dirty = range_tree_contains(rt, txg, size); 2402 mutex_exit(&vd->vdev_dtl_lock); 2403 2404 return (dirty); 2405 } 2406 2407 boolean_t 2408 vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t) 2409 { 2410 range_tree_t *rt = vd->vdev_dtl[t]; 2411 boolean_t empty; 2412 2413 mutex_enter(&vd->vdev_dtl_lock); 2414 empty = range_tree_is_empty(rt); 2415 mutex_exit(&vd->vdev_dtl_lock); 2416 2417 return (empty); 2418 } 2419 2420 /* 2421 * Returns B_TRUE if vdev determines offset needs to be resilvered. 2422 */ 2423 boolean_t 2424 vdev_dtl_need_resilver(vdev_t *vd, uint64_t offset, size_t psize) 2425 { 2426 ASSERT(vd != vd->vdev_spa->spa_root_vdev); 2427 2428 if (vd->vdev_ops->vdev_op_need_resilver == NULL || 2429 vd->vdev_ops->vdev_op_leaf) 2430 return (B_TRUE); 2431 2432 return (vd->vdev_ops->vdev_op_need_resilver(vd, offset, psize)); 2433 } 2434 2435 /* 2436 * Returns the lowest txg in the DTL range. 2437 */ 2438 static uint64_t 2439 vdev_dtl_min(vdev_t *vd) 2440 { 2441 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock)); 2442 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0); 2443 ASSERT0(vd->vdev_children); 2444 2445 return (range_tree_min(vd->vdev_dtl[DTL_MISSING]) - 1); 2446 } 2447 2448 /* 2449 * Returns the highest txg in the DTL. 2450 */ 2451 static uint64_t 2452 vdev_dtl_max(vdev_t *vd) 2453 { 2454 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock)); 2455 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0); 2456 ASSERT0(vd->vdev_children); 2457 2458 return (range_tree_max(vd->vdev_dtl[DTL_MISSING])); 2459 } 2460 2461 /* 2462 * Determine if a resilvering vdev should remove any DTL entries from 2463 * its range. If the vdev was resilvering for the entire duration of the 2464 * scan then it should excise that range from its DTLs. Otherwise, this 2465 * vdev is considered partially resilvered and should leave its DTL 2466 * entries intact. The comment in vdev_dtl_reassess() describes how we 2467 * excise the DTLs. 2468 */ 2469 static boolean_t 2470 vdev_dtl_should_excise(vdev_t *vd) 2471 { 2472 spa_t *spa = vd->vdev_spa; 2473 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; 2474 2475 ASSERT0(vd->vdev_children); 2476 2477 if (vd->vdev_state < VDEV_STATE_DEGRADED) 2478 return (B_FALSE); 2479 2480 if (vd->vdev_resilver_deferred) 2481 return (B_FALSE); 2482 2483 if (vd->vdev_resilver_txg == 0 || 2484 range_tree_is_empty(vd->vdev_dtl[DTL_MISSING])) 2485 return (B_TRUE); 2486 2487 /* 2488 * When a resilver is initiated the scan will assign the scn_max_txg 2489 * value to the highest txg value that exists in all DTLs. If this 2490 * device's max DTL is not part of this scan (i.e. it is not in 2491 * the range (scn_min_txg, scn_max_txg] then it is not eligible 2492 * for excision. 2493 */ 2494 if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) { 2495 ASSERT3U(scn->scn_phys.scn_min_txg, <=, vdev_dtl_min(vd)); 2496 ASSERT3U(scn->scn_phys.scn_min_txg, <, vd->vdev_resilver_txg); 2497 ASSERT3U(vd->vdev_resilver_txg, <=, scn->scn_phys.scn_max_txg); 2498 return (B_TRUE); 2499 } 2500 return (B_FALSE); 2501 } 2502 2503 /* 2504 * Reassess DTLs after a config change or scrub completion. 2505 */ 2506 void 2507 vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done) 2508 { 2509 spa_t *spa = vd->vdev_spa; 2510 avl_tree_t reftree; 2511 int minref; 2512 2513 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 2514 2515 for (int c = 0; c < vd->vdev_children; c++) 2516 vdev_dtl_reassess(vd->vdev_child[c], txg, 2517 scrub_txg, scrub_done); 2518 2519 if (vd == spa->spa_root_vdev || !vdev_is_concrete(vd) || vd->vdev_aux) 2520 return; 2521 2522 if (vd->vdev_ops->vdev_op_leaf) { 2523 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; 2524 boolean_t wasempty = B_TRUE; 2525 2526 mutex_enter(&vd->vdev_dtl_lock); 2527 2528 /* 2529 * If requested, pretend the scan completed cleanly. 2530 */ 2531 if (zfs_scan_ignore_errors && scn) 2532 scn->scn_phys.scn_errors = 0; 2533 2534 if (scrub_txg != 0 && 2535 !range_tree_is_empty(vd->vdev_dtl[DTL_MISSING])) { 2536 wasempty = B_FALSE; 2537 zfs_dbgmsg("guid:%llu txg:%llu scrub:%llu started:%d " 2538 "dtl:%llu/%llu errors:%llu", 2539 (u_longlong_t)vd->vdev_guid, (u_longlong_t)txg, 2540 (u_longlong_t)scrub_txg, spa->spa_scrub_started, 2541 (u_longlong_t)vdev_dtl_min(vd), 2542 (u_longlong_t)vdev_dtl_max(vd), 2543 (u_longlong_t)(scn ? scn->scn_phys.scn_errors : 0)); 2544 } 2545 2546 /* 2547 * If we've completed a scan cleanly then determine 2548 * if this vdev should remove any DTLs. We only want to 2549 * excise regions on vdevs that were available during 2550 * the entire duration of this scan. 2551 */ 2552 if (scrub_txg != 0 && 2553 (spa->spa_scrub_started || 2554 (scn != NULL && scn->scn_phys.scn_errors == 0)) && 2555 vdev_dtl_should_excise(vd)) { 2556 /* 2557 * We completed a scrub up to scrub_txg. If we 2558 * did it without rebooting, then the scrub dtl 2559 * will be valid, so excise the old region and 2560 * fold in the scrub dtl. Otherwise, leave the 2561 * dtl as-is if there was an error. 2562 * 2563 * There's little trick here: to excise the beginning 2564 * of the DTL_MISSING map, we put it into a reference 2565 * tree and then add a segment with refcnt -1 that 2566 * covers the range [0, scrub_txg). This means 2567 * that each txg in that range has refcnt -1 or 0. 2568 * We then add DTL_SCRUB with a refcnt of 2, so that 2569 * entries in the range [0, scrub_txg) will have a 2570 * positive refcnt -- either 1 or 2. We then convert 2571 * the reference tree into the new DTL_MISSING map. 2572 */ 2573 space_reftree_create(&reftree); 2574 space_reftree_add_map(&reftree, 2575 vd->vdev_dtl[DTL_MISSING], 1); 2576 space_reftree_add_seg(&reftree, 0, scrub_txg, -1); 2577 space_reftree_add_map(&reftree, 2578 vd->vdev_dtl[DTL_SCRUB], 2); 2579 space_reftree_generate_map(&reftree, 2580 vd->vdev_dtl[DTL_MISSING], 1); 2581 space_reftree_destroy(&reftree); 2582 2583 if (!range_tree_is_empty(vd->vdev_dtl[DTL_MISSING])) { 2584 zfs_dbgmsg("update DTL_MISSING:%llu/%llu", 2585 (u_longlong_t)vdev_dtl_min(vd), 2586 (u_longlong_t)vdev_dtl_max(vd)); 2587 } else if (!wasempty) { 2588 zfs_dbgmsg("DTL_MISSING is now empty"); 2589 } 2590 } 2591 range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL); 2592 range_tree_walk(vd->vdev_dtl[DTL_MISSING], 2593 range_tree_add, vd->vdev_dtl[DTL_PARTIAL]); 2594 if (scrub_done) 2595 range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL); 2596 range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL); 2597 if (!vdev_readable(vd)) 2598 range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL); 2599 else 2600 range_tree_walk(vd->vdev_dtl[DTL_MISSING], 2601 range_tree_add, vd->vdev_dtl[DTL_OUTAGE]); 2602 2603 /* 2604 * If the vdev was resilvering and no longer has any 2605 * DTLs then reset its resilvering flag. 2606 */ 2607 if (vd->vdev_resilver_txg != 0 && 2608 range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) && 2609 range_tree_is_empty(vd->vdev_dtl[DTL_OUTAGE])) 2610 vd->vdev_resilver_txg = 0; 2611 2612 mutex_exit(&vd->vdev_dtl_lock); 2613 2614 if (txg != 0) 2615 vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg); 2616 return; 2617 } 2618 2619 mutex_enter(&vd->vdev_dtl_lock); 2620 for (int t = 0; t < DTL_TYPES; t++) { 2621 /* account for child's outage in parent's missing map */ 2622 int s = (t == DTL_MISSING) ? DTL_OUTAGE: t; 2623 if (t == DTL_SCRUB) 2624 continue; /* leaf vdevs only */ 2625 if (t == DTL_PARTIAL) 2626 minref = 1; /* i.e. non-zero */ 2627 else if (vd->vdev_nparity != 0) 2628 minref = vd->vdev_nparity + 1; /* RAID-Z */ 2629 else 2630 minref = vd->vdev_children; /* any kind of mirror */ 2631 space_reftree_create(&reftree); 2632 for (int c = 0; c < vd->vdev_children; c++) { 2633 vdev_t *cvd = vd->vdev_child[c]; 2634 mutex_enter(&cvd->vdev_dtl_lock); 2635 space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1); 2636 mutex_exit(&cvd->vdev_dtl_lock); 2637 } 2638 space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref); 2639 space_reftree_destroy(&reftree); 2640 } 2641 mutex_exit(&vd->vdev_dtl_lock); 2642 } 2643 2644 int 2645 vdev_dtl_load(vdev_t *vd) 2646 { 2647 spa_t *spa = vd->vdev_spa; 2648 objset_t *mos = spa->spa_meta_objset; 2649 int error = 0; 2650 2651 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) { 2652 ASSERT(vdev_is_concrete(vd)); 2653 2654 error = space_map_open(&vd->vdev_dtl_sm, mos, 2655 vd->vdev_dtl_object, 0, -1ULL, 0); 2656 if (error) 2657 return (error); 2658 ASSERT(vd->vdev_dtl_sm != NULL); 2659 2660 mutex_enter(&vd->vdev_dtl_lock); 2661 error = space_map_load(vd->vdev_dtl_sm, 2662 vd->vdev_dtl[DTL_MISSING], SM_ALLOC); 2663 mutex_exit(&vd->vdev_dtl_lock); 2664 2665 return (error); 2666 } 2667 2668 for (int c = 0; c < vd->vdev_children; c++) { 2669 error = vdev_dtl_load(vd->vdev_child[c]); 2670 if (error != 0) 2671 break; 2672 } 2673 2674 return (error); 2675 } 2676 2677 static void 2678 vdev_zap_allocation_data(vdev_t *vd, dmu_tx_t *tx) 2679 { 2680 spa_t *spa = vd->vdev_spa; 2681 objset_t *mos = spa->spa_meta_objset; 2682 vdev_alloc_bias_t alloc_bias = vd->vdev_alloc_bias; 2683 const char *string; 2684 2685 ASSERT(alloc_bias != VDEV_BIAS_NONE); 2686 2687 string = 2688 (alloc_bias == VDEV_BIAS_LOG) ? VDEV_ALLOC_BIAS_LOG : 2689 (alloc_bias == VDEV_BIAS_SPECIAL) ? VDEV_ALLOC_BIAS_SPECIAL : 2690 (alloc_bias == VDEV_BIAS_DEDUP) ? VDEV_ALLOC_BIAS_DEDUP : NULL; 2691 2692 ASSERT(string != NULL); 2693 VERIFY0(zap_add(mos, vd->vdev_top_zap, VDEV_TOP_ZAP_ALLOCATION_BIAS, 2694 1, strlen(string) + 1, string, tx)); 2695 2696 if (alloc_bias == VDEV_BIAS_SPECIAL || alloc_bias == VDEV_BIAS_DEDUP) { 2697 spa_activate_allocation_classes(spa, tx); 2698 } 2699 } 2700 2701 void 2702 vdev_destroy_unlink_zap(vdev_t *vd, uint64_t zapobj, dmu_tx_t *tx) 2703 { 2704 spa_t *spa = vd->vdev_spa; 2705 2706 VERIFY0(zap_destroy(spa->spa_meta_objset, zapobj, tx)); 2707 VERIFY0(zap_remove_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps, 2708 zapobj, tx)); 2709 } 2710 2711 uint64_t 2712 vdev_create_link_zap(vdev_t *vd, dmu_tx_t *tx) 2713 { 2714 spa_t *spa = vd->vdev_spa; 2715 uint64_t zap = zap_create(spa->spa_meta_objset, DMU_OTN_ZAP_METADATA, 2716 DMU_OT_NONE, 0, tx); 2717 2718 ASSERT(zap != 0); 2719 VERIFY0(zap_add_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps, 2720 zap, tx)); 2721 2722 return (zap); 2723 } 2724 2725 void 2726 vdev_construct_zaps(vdev_t *vd, dmu_tx_t *tx) 2727 { 2728 if (vd->vdev_ops != &vdev_hole_ops && 2729 vd->vdev_ops != &vdev_missing_ops && 2730 vd->vdev_ops != &vdev_root_ops && 2731 !vd->vdev_top->vdev_removing) { 2732 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_leaf_zap == 0) { 2733 vd->vdev_leaf_zap = vdev_create_link_zap(vd, tx); 2734 } 2735 if (vd == vd->vdev_top && vd->vdev_top_zap == 0) { 2736 vd->vdev_top_zap = vdev_create_link_zap(vd, tx); 2737 if (vd->vdev_alloc_bias != VDEV_BIAS_NONE) 2738 vdev_zap_allocation_data(vd, tx); 2739 } 2740 } 2741 2742 for (uint64_t i = 0; i < vd->vdev_children; i++) { 2743 vdev_construct_zaps(vd->vdev_child[i], tx); 2744 } 2745 } 2746 2747 void 2748 vdev_dtl_sync(vdev_t *vd, uint64_t txg) 2749 { 2750 spa_t *spa = vd->vdev_spa; 2751 range_tree_t *rt = vd->vdev_dtl[DTL_MISSING]; 2752 objset_t *mos = spa->spa_meta_objset; 2753 range_tree_t *rtsync; 2754 dmu_tx_t *tx; 2755 uint64_t object = space_map_object(vd->vdev_dtl_sm); 2756 2757 ASSERT(vdev_is_concrete(vd)); 2758 ASSERT(vd->vdev_ops->vdev_op_leaf); 2759 2760 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 2761 2762 if (vd->vdev_detached || vd->vdev_top->vdev_removing) { 2763 mutex_enter(&vd->vdev_dtl_lock); 2764 space_map_free(vd->vdev_dtl_sm, tx); 2765 space_map_close(vd->vdev_dtl_sm); 2766 vd->vdev_dtl_sm = NULL; 2767 mutex_exit(&vd->vdev_dtl_lock); 2768 2769 /* 2770 * We only destroy the leaf ZAP for detached leaves or for 2771 * removed log devices. Removed data devices handle leaf ZAP 2772 * cleanup later, once cancellation is no longer possible. 2773 */ 2774 if (vd->vdev_leaf_zap != 0 && (vd->vdev_detached || 2775 vd->vdev_top->vdev_islog)) { 2776 vdev_destroy_unlink_zap(vd, vd->vdev_leaf_zap, tx); 2777 vd->vdev_leaf_zap = 0; 2778 } 2779 2780 dmu_tx_commit(tx); 2781 return; 2782 } 2783 2784 if (vd->vdev_dtl_sm == NULL) { 2785 uint64_t new_object; 2786 2787 new_object = space_map_alloc(mos, zfs_vdev_dtl_sm_blksz, tx); 2788 VERIFY3U(new_object, !=, 0); 2789 2790 VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object, 2791 0, -1ULL, 0)); 2792 ASSERT(vd->vdev_dtl_sm != NULL); 2793 } 2794 2795 rtsync = range_tree_create(NULL, RANGE_SEG64, NULL, 0, 0); 2796 2797 mutex_enter(&vd->vdev_dtl_lock); 2798 range_tree_walk(rt, range_tree_add, rtsync); 2799 mutex_exit(&vd->vdev_dtl_lock); 2800 2801 space_map_truncate(vd->vdev_dtl_sm, zfs_vdev_dtl_sm_blksz, tx); 2802 space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, SM_NO_VDEVID, tx); 2803 range_tree_vacate(rtsync, NULL, NULL); 2804 2805 range_tree_destroy(rtsync); 2806 2807 /* 2808 * If the object for the space map has changed then dirty 2809 * the top level so that we update the config. 2810 */ 2811 if (object != space_map_object(vd->vdev_dtl_sm)) { 2812 vdev_dbgmsg(vd, "txg %llu, spa %s, DTL old object %llu, " 2813 "new object %llu", (u_longlong_t)txg, spa_name(spa), 2814 (u_longlong_t)object, 2815 (u_longlong_t)space_map_object(vd->vdev_dtl_sm)); 2816 vdev_config_dirty(vd->vdev_top); 2817 } 2818 2819 dmu_tx_commit(tx); 2820 } 2821 2822 /* 2823 * Determine whether the specified vdev can be offlined/detached/removed 2824 * without losing data. 2825 */ 2826 boolean_t 2827 vdev_dtl_required(vdev_t *vd) 2828 { 2829 spa_t *spa = vd->vdev_spa; 2830 vdev_t *tvd = vd->vdev_top; 2831 uint8_t cant_read = vd->vdev_cant_read; 2832 boolean_t required; 2833 2834 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 2835 2836 if (vd == spa->spa_root_vdev || vd == tvd) 2837 return (B_TRUE); 2838 2839 /* 2840 * Temporarily mark the device as unreadable, and then determine 2841 * whether this results in any DTL outages in the top-level vdev. 2842 * If not, we can safely offline/detach/remove the device. 2843 */ 2844 vd->vdev_cant_read = B_TRUE; 2845 vdev_dtl_reassess(tvd, 0, 0, B_FALSE); 2846 required = !vdev_dtl_empty(tvd, DTL_OUTAGE); 2847 vd->vdev_cant_read = cant_read; 2848 vdev_dtl_reassess(tvd, 0, 0, B_FALSE); 2849 2850 if (!required && zio_injection_enabled) 2851 required = !!zio_handle_device_injection(vd, NULL, ECHILD); 2852 2853 return (required); 2854 } 2855 2856 /* 2857 * Determine if resilver is needed, and if so the txg range. 2858 */ 2859 boolean_t 2860 vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp) 2861 { 2862 boolean_t needed = B_FALSE; 2863 uint64_t thismin = UINT64_MAX; 2864 uint64_t thismax = 0; 2865 2866 if (vd->vdev_children == 0) { 2867 mutex_enter(&vd->vdev_dtl_lock); 2868 if (!range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) && 2869 vdev_writeable(vd)) { 2870 2871 thismin = vdev_dtl_min(vd); 2872 thismax = vdev_dtl_max(vd); 2873 needed = B_TRUE; 2874 } 2875 mutex_exit(&vd->vdev_dtl_lock); 2876 } else { 2877 for (int c = 0; c < vd->vdev_children; c++) { 2878 vdev_t *cvd = vd->vdev_child[c]; 2879 uint64_t cmin, cmax; 2880 2881 if (vdev_resilver_needed(cvd, &cmin, &cmax)) { 2882 thismin = MIN(thismin, cmin); 2883 thismax = MAX(thismax, cmax); 2884 needed = B_TRUE; 2885 } 2886 } 2887 } 2888 2889 if (needed && minp) { 2890 *minp = thismin; 2891 *maxp = thismax; 2892 } 2893 return (needed); 2894 } 2895 2896 /* 2897 * Gets the checkpoint space map object from the vdev's ZAP. 2898 * Returns the spacemap object, or 0 if it wasn't in the ZAP 2899 * or the ZAP doesn't exist yet. 2900 */ 2901 int 2902 vdev_checkpoint_sm_object(vdev_t *vd) 2903 { 2904 ASSERT0(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER)); 2905 if (vd->vdev_top_zap == 0) { 2906 return (0); 2907 } 2908 2909 uint64_t sm_obj = 0; 2910 int err = zap_lookup(spa_meta_objset(vd->vdev_spa), vd->vdev_top_zap, 2911 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, sizeof (uint64_t), 1, &sm_obj); 2912 2913 ASSERT(err == 0 || err == ENOENT); 2914 2915 return (sm_obj); 2916 } 2917 2918 int 2919 vdev_load(vdev_t *vd) 2920 { 2921 int error = 0; 2922 /* 2923 * Recursively load all children. 2924 */ 2925 for (int c = 0; c < vd->vdev_children; c++) { 2926 error = vdev_load(vd->vdev_child[c]); 2927 if (error != 0) { 2928 return (error); 2929 } 2930 } 2931 2932 vdev_set_deflate_ratio(vd); 2933 2934 /* 2935 * On spa_load path, grab the allocation bias from our zap 2936 */ 2937 if (vd == vd->vdev_top && vd->vdev_top_zap != 0) { 2938 spa_t *spa = vd->vdev_spa; 2939 char bias_str[64]; 2940 2941 if (zap_lookup(spa->spa_meta_objset, vd->vdev_top_zap, 2942 VDEV_TOP_ZAP_ALLOCATION_BIAS, 1, sizeof (bias_str), 2943 bias_str) == 0) { 2944 ASSERT(vd->vdev_alloc_bias == VDEV_BIAS_NONE); 2945 vd->vdev_alloc_bias = vdev_derive_alloc_bias(bias_str); 2946 } 2947 } 2948 2949 /* 2950 * If this is a top-level vdev, initialize its metaslabs. 2951 */ 2952 if (vd == vd->vdev_top && vdev_is_concrete(vd)) { 2953 vdev_metaslab_group_create(vd); 2954 2955 if (vd->vdev_ashift == 0 || vd->vdev_asize == 0) { 2956 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 2957 VDEV_AUX_CORRUPT_DATA); 2958 vdev_dbgmsg(vd, "vdev_load: invalid size. ashift=%llu, " 2959 "asize=%llu", (u_longlong_t)vd->vdev_ashift, 2960 (u_longlong_t)vd->vdev_asize); 2961 return (SET_ERROR(ENXIO)); 2962 } 2963 2964 error = vdev_metaslab_init(vd, 0); 2965 if (error != 0) { 2966 vdev_dbgmsg(vd, "vdev_load: metaslab_init failed " 2967 "[error=%d]", error); 2968 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 2969 VDEV_AUX_CORRUPT_DATA); 2970 return (error); 2971 } 2972 2973 uint64_t checkpoint_sm_obj = vdev_checkpoint_sm_object(vd); 2974 if (checkpoint_sm_obj != 0) { 2975 objset_t *mos = spa_meta_objset(vd->vdev_spa); 2976 ASSERT(vd->vdev_asize != 0); 2977 ASSERT3P(vd->vdev_checkpoint_sm, ==, NULL); 2978 2979 error = space_map_open(&vd->vdev_checkpoint_sm, 2980 mos, checkpoint_sm_obj, 0, vd->vdev_asize, 2981 vd->vdev_ashift); 2982 if (error != 0) { 2983 vdev_dbgmsg(vd, "vdev_load: space_map_open " 2984 "failed for checkpoint spacemap (obj %llu) " 2985 "[error=%d]", 2986 (u_longlong_t)checkpoint_sm_obj, error); 2987 return (error); 2988 } 2989 ASSERT3P(vd->vdev_checkpoint_sm, !=, NULL); 2990 2991 /* 2992 * Since the checkpoint_sm contains free entries 2993 * exclusively we can use space_map_allocated() to 2994 * indicate the cumulative checkpointed space that 2995 * has been freed. 2996 */ 2997 vd->vdev_stat.vs_checkpoint_space = 2998 -space_map_allocated(vd->vdev_checkpoint_sm); 2999 vd->vdev_spa->spa_checkpoint_info.sci_dspace += 3000 vd->vdev_stat.vs_checkpoint_space; 3001 } 3002 } 3003 3004 /* 3005 * If this is a leaf vdev, load its DTL. 3006 */ 3007 if (vd->vdev_ops->vdev_op_leaf && (error = vdev_dtl_load(vd)) != 0) { 3008 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 3009 VDEV_AUX_CORRUPT_DATA); 3010 vdev_dbgmsg(vd, "vdev_load: vdev_dtl_load failed " 3011 "[error=%d]", error); 3012 return (error); 3013 } 3014 3015 uint64_t obsolete_sm_object = vdev_obsolete_sm_object(vd); 3016 if (obsolete_sm_object != 0) { 3017 objset_t *mos = vd->vdev_spa->spa_meta_objset; 3018 ASSERT(vd->vdev_asize != 0); 3019 ASSERT3P(vd->vdev_obsolete_sm, ==, NULL); 3020 3021 if ((error = space_map_open(&vd->vdev_obsolete_sm, mos, 3022 obsolete_sm_object, 0, vd->vdev_asize, 0))) { 3023 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 3024 VDEV_AUX_CORRUPT_DATA); 3025 vdev_dbgmsg(vd, "vdev_load: space_map_open failed for " 3026 "obsolete spacemap (obj %llu) [error=%d]", 3027 (u_longlong_t)obsolete_sm_object, error); 3028 return (error); 3029 } 3030 } 3031 3032 return (0); 3033 } 3034 3035 /* 3036 * The special vdev case is used for hot spares and l2cache devices. Its 3037 * sole purpose it to set the vdev state for the associated vdev. To do this, 3038 * we make sure that we can open the underlying device, then try to read the 3039 * label, and make sure that the label is sane and that it hasn't been 3040 * repurposed to another pool. 3041 */ 3042 int 3043 vdev_validate_aux(vdev_t *vd) 3044 { 3045 nvlist_t *label; 3046 uint64_t guid, version; 3047 uint64_t state; 3048 3049 if (!vdev_readable(vd)) 3050 return (0); 3051 3052 if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) { 3053 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 3054 VDEV_AUX_CORRUPT_DATA); 3055 return (-1); 3056 } 3057 3058 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 || 3059 !SPA_VERSION_IS_SUPPORTED(version) || 3060 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 || 3061 guid != vd->vdev_guid || 3062 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) { 3063 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN, 3064 VDEV_AUX_CORRUPT_DATA); 3065 nvlist_free(label); 3066 return (-1); 3067 } 3068 3069 /* 3070 * We don't actually check the pool state here. If it's in fact in 3071 * use by another pool, we update this fact on the fly when requested. 3072 */ 3073 nvlist_free(label); 3074 return (0); 3075 } 3076 3077 static void 3078 vdev_destroy_ms_flush_data(vdev_t *vd, dmu_tx_t *tx) 3079 { 3080 objset_t *mos = spa_meta_objset(vd->vdev_spa); 3081 3082 if (vd->vdev_top_zap == 0) 3083 return; 3084 3085 uint64_t object = 0; 3086 int err = zap_lookup(mos, vd->vdev_top_zap, 3087 VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS, sizeof (uint64_t), 1, &object); 3088 if (err == ENOENT) 3089 return; 3090 3091 VERIFY0(dmu_object_free(mos, object, tx)); 3092 VERIFY0(zap_remove(mos, vd->vdev_top_zap, 3093 VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS, tx)); 3094 } 3095 3096 /* 3097 * Free the objects used to store this vdev's spacemaps, and the array 3098 * that points to them. 3099 */ 3100 void 3101 vdev_destroy_spacemaps(vdev_t *vd, dmu_tx_t *tx) 3102 { 3103 if (vd->vdev_ms_array == 0) 3104 return; 3105 3106 objset_t *mos = vd->vdev_spa->spa_meta_objset; 3107 uint64_t array_count = vd->vdev_asize >> vd->vdev_ms_shift; 3108 size_t array_bytes = array_count * sizeof (uint64_t); 3109 uint64_t *smobj_array = kmem_alloc(array_bytes, KM_SLEEP); 3110 VERIFY0(dmu_read(mos, vd->vdev_ms_array, 0, 3111 array_bytes, smobj_array, 0)); 3112 3113 for (uint64_t i = 0; i < array_count; i++) { 3114 uint64_t smobj = smobj_array[i]; 3115 if (smobj == 0) 3116 continue; 3117 3118 space_map_free_obj(mos, smobj, tx); 3119 } 3120 3121 kmem_free(smobj_array, array_bytes); 3122 VERIFY0(dmu_object_free(mos, vd->vdev_ms_array, tx)); 3123 vdev_destroy_ms_flush_data(vd, tx); 3124 vd->vdev_ms_array = 0; 3125 } 3126 3127 static void 3128 vdev_remove_empty_log(vdev_t *vd, uint64_t txg) 3129 { 3130 spa_t *spa = vd->vdev_spa; 3131 3132 ASSERT(vd->vdev_islog); 3133 ASSERT(vd == vd->vdev_top); 3134 ASSERT3U(txg, ==, spa_syncing_txg(spa)); 3135 3136 dmu_tx_t *tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg); 3137 3138 vdev_destroy_spacemaps(vd, tx); 3139 if (vd->vdev_top_zap != 0) { 3140 vdev_destroy_unlink_zap(vd, vd->vdev_top_zap, tx); 3141 vd->vdev_top_zap = 0; 3142 } 3143 3144 dmu_tx_commit(tx); 3145 } 3146 3147 void 3148 vdev_sync_done(vdev_t *vd, uint64_t txg) 3149 { 3150 metaslab_t *msp; 3151 boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg)); 3152 3153 ASSERT(vdev_is_concrete(vd)); 3154 3155 while ((msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg))) 3156 != NULL) 3157 metaslab_sync_done(msp, txg); 3158 3159 if (reassess) 3160 metaslab_sync_reassess(vd->vdev_mg); 3161 } 3162 3163 void 3164 vdev_sync(vdev_t *vd, uint64_t txg) 3165 { 3166 spa_t *spa = vd->vdev_spa; 3167 vdev_t *lvd; 3168 metaslab_t *msp; 3169 3170 ASSERT3U(txg, ==, spa->spa_syncing_txg); 3171 dmu_tx_t *tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 3172 if (range_tree_space(vd->vdev_obsolete_segments) > 0) { 3173 ASSERT(vd->vdev_removing || 3174 vd->vdev_ops == &vdev_indirect_ops); 3175 3176 vdev_indirect_sync_obsolete(vd, tx); 3177 3178 /* 3179 * If the vdev is indirect, it can't have dirty 3180 * metaslabs or DTLs. 3181 */ 3182 if (vd->vdev_ops == &vdev_indirect_ops) { 3183 ASSERT(txg_list_empty(&vd->vdev_ms_list, txg)); 3184 ASSERT(txg_list_empty(&vd->vdev_dtl_list, txg)); 3185 dmu_tx_commit(tx); 3186 return; 3187 } 3188 } 3189 3190 ASSERT(vdev_is_concrete(vd)); 3191 3192 if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0 && 3193 !vd->vdev_removing) { 3194 ASSERT(vd == vd->vdev_top); 3195 ASSERT0(vd->vdev_indirect_config.vic_mapping_object); 3196 vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset, 3197 DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx); 3198 ASSERT(vd->vdev_ms_array != 0); 3199 vdev_config_dirty(vd); 3200 } 3201 3202 while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) { 3203 metaslab_sync(msp, txg); 3204 (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg)); 3205 } 3206 3207 while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL) 3208 vdev_dtl_sync(lvd, txg); 3209 3210 /* 3211 * If this is an empty log device being removed, destroy the 3212 * metadata associated with it. 3213 */ 3214 if (vd->vdev_islog && vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing) 3215 vdev_remove_empty_log(vd, txg); 3216 3217 (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)); 3218 dmu_tx_commit(tx); 3219 } 3220 3221 uint64_t 3222 vdev_psize_to_asize(vdev_t *vd, uint64_t psize) 3223 { 3224 return (vd->vdev_ops->vdev_op_asize(vd, psize)); 3225 } 3226 3227 /* 3228 * Mark the given vdev faulted. A faulted vdev behaves as if the device could 3229 * not be opened, and no I/O is attempted. 3230 */ 3231 int 3232 vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux) 3233 { 3234 vdev_t *vd, *tvd; 3235 3236 spa_vdev_state_enter(spa, SCL_NONE); 3237 3238 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3239 return (spa_vdev_state_exit(spa, NULL, ENODEV)); 3240 3241 if (!vd->vdev_ops->vdev_op_leaf) 3242 return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 3243 3244 tvd = vd->vdev_top; 3245 3246 /* 3247 * We don't directly use the aux state here, but if we do a 3248 * vdev_reopen(), we need this value to be present to remember why we 3249 * were faulted. 3250 */ 3251 vd->vdev_label_aux = aux; 3252 3253 /* 3254 * Faulted state takes precedence over degraded. 3255 */ 3256 vd->vdev_delayed_close = B_FALSE; 3257 vd->vdev_faulted = 1ULL; 3258 vd->vdev_degraded = 0ULL; 3259 vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux); 3260 3261 /* 3262 * If this device has the only valid copy of the data, then 3263 * back off and simply mark the vdev as degraded instead. 3264 */ 3265 if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) { 3266 vd->vdev_degraded = 1ULL; 3267 vd->vdev_faulted = 0ULL; 3268 3269 /* 3270 * If we reopen the device and it's not dead, only then do we 3271 * mark it degraded. 3272 */ 3273 vdev_reopen(tvd); 3274 3275 if (vdev_readable(vd)) 3276 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux); 3277 } 3278 3279 return (spa_vdev_state_exit(spa, vd, 0)); 3280 } 3281 3282 /* 3283 * Mark the given vdev degraded. A degraded vdev is purely an indication to the 3284 * user that something is wrong. The vdev continues to operate as normal as far 3285 * as I/O is concerned. 3286 */ 3287 int 3288 vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux) 3289 { 3290 vdev_t *vd; 3291 3292 spa_vdev_state_enter(spa, SCL_NONE); 3293 3294 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3295 return (spa_vdev_state_exit(spa, NULL, ENODEV)); 3296 3297 if (!vd->vdev_ops->vdev_op_leaf) 3298 return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 3299 3300 /* 3301 * If the vdev is already faulted, then don't do anything. 3302 */ 3303 if (vd->vdev_faulted || vd->vdev_degraded) 3304 return (spa_vdev_state_exit(spa, NULL, 0)); 3305 3306 vd->vdev_degraded = 1ULL; 3307 if (!vdev_is_dead(vd)) 3308 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, 3309 aux); 3310 3311 return (spa_vdev_state_exit(spa, vd, 0)); 3312 } 3313 3314 /* 3315 * Online the given vdev. 3316 * 3317 * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached 3318 * spare device should be detached when the device finishes resilvering. 3319 * Second, the online should be treated like a 'test' online case, so no FMA 3320 * events are generated if the device fails to open. 3321 */ 3322 int 3323 vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate) 3324 { 3325 vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev; 3326 boolean_t wasoffline; 3327 vdev_state_t oldstate; 3328 3329 spa_vdev_state_enter(spa, SCL_NONE); 3330 3331 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3332 return (spa_vdev_state_exit(spa, NULL, ENODEV)); 3333 3334 if (!vd->vdev_ops->vdev_op_leaf) 3335 return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 3336 3337 wasoffline = (vd->vdev_offline || vd->vdev_tmpoffline); 3338 oldstate = vd->vdev_state; 3339 3340 tvd = vd->vdev_top; 3341 vd->vdev_offline = B_FALSE; 3342 vd->vdev_tmpoffline = B_FALSE; 3343 vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE); 3344 vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT); 3345 3346 /* XXX - L2ARC 1.0 does not support expansion */ 3347 if (!vd->vdev_aux) { 3348 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 3349 pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND); 3350 } 3351 3352 vdev_reopen(tvd); 3353 vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE; 3354 3355 if (!vd->vdev_aux) { 3356 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 3357 pvd->vdev_expanding = B_FALSE; 3358 } 3359 3360 if (newstate) 3361 *newstate = vd->vdev_state; 3362 if ((flags & ZFS_ONLINE_UNSPARE) && 3363 !vdev_is_dead(vd) && vd->vdev_parent && 3364 vd->vdev_parent->vdev_ops == &vdev_spare_ops && 3365 vd->vdev_parent->vdev_child[0] == vd) 3366 vd->vdev_unspare = B_TRUE; 3367 3368 if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) { 3369 3370 /* XXX - L2ARC 1.0 does not support expansion */ 3371 if (vd->vdev_aux) 3372 return (spa_vdev_state_exit(spa, vd, ENOTSUP)); 3373 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); 3374 } 3375 3376 /* Restart initializing if necessary */ 3377 mutex_enter(&vd->vdev_initialize_lock); 3378 if (vdev_writeable(vd) && 3379 vd->vdev_initialize_thread == NULL && 3380 vd->vdev_initialize_state == VDEV_INITIALIZE_ACTIVE) { 3381 (void) vdev_initialize(vd); 3382 } 3383 mutex_exit(&vd->vdev_initialize_lock); 3384 3385 /* Restart trimming if necessary */ 3386 mutex_enter(&vd->vdev_trim_lock); 3387 if (vdev_writeable(vd) && 3388 vd->vdev_trim_thread == NULL && 3389 vd->vdev_trim_state == VDEV_TRIM_ACTIVE) { 3390 (void) vdev_trim(vd, vd->vdev_trim_rate, vd->vdev_trim_partial, 3391 vd->vdev_trim_secure); 3392 } 3393 mutex_exit(&vd->vdev_trim_lock); 3394 3395 if (wasoffline || 3396 (oldstate < VDEV_STATE_DEGRADED && 3397 vd->vdev_state >= VDEV_STATE_DEGRADED)) 3398 spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_ONLINE); 3399 3400 return (spa_vdev_state_exit(spa, vd, 0)); 3401 } 3402 3403 static int 3404 vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags) 3405 { 3406 vdev_t *vd, *tvd; 3407 int error = 0; 3408 uint64_t generation; 3409 metaslab_group_t *mg; 3410 3411 top: 3412 spa_vdev_state_enter(spa, SCL_ALLOC); 3413 3414 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) 3415 return (spa_vdev_state_exit(spa, NULL, ENODEV)); 3416 3417 if (!vd->vdev_ops->vdev_op_leaf) 3418 return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); 3419 3420 tvd = vd->vdev_top; 3421 mg = tvd->vdev_mg; 3422 generation = spa->spa_config_generation + 1; 3423 3424 /* 3425 * If the device isn't already offline, try to offline it. 3426 */ 3427 if (!vd->vdev_offline) { 3428 /* 3429 * If this device has the only valid copy of some data, 3430 * don't allow it to be offlined. Log devices are always 3431 * expendable. 3432 */ 3433 if (!tvd->vdev_islog && vd->vdev_aux == NULL && 3434 vdev_dtl_required(vd)) 3435 return (spa_vdev_state_exit(spa, NULL, EBUSY)); 3436 3437 /* 3438 * If the top-level is a slog and it has had allocations 3439 * then proceed. We check that the vdev's metaslab group 3440 * is not NULL since it's possible that we may have just 3441 * added this vdev but not yet initialized its metaslabs. 3442 */ 3443 if (tvd->vdev_islog && mg != NULL) { 3444 /* 3445 * Prevent any future allocations. 3446 */ 3447 metaslab_group_passivate(mg); 3448 (void) spa_vdev_state_exit(spa, vd, 0); 3449 3450 error = spa_reset_logs(spa); 3451 3452 /* 3453 * If the log device was successfully reset but has 3454 * checkpointed data, do not offline it. 3455 */ 3456 if (error == 0 && 3457 tvd->vdev_checkpoint_sm != NULL) { 3458 error = ZFS_ERR_CHECKPOINT_EXISTS; 3459 } 3460 3461 spa_vdev_state_enter(spa, SCL_ALLOC); 3462 3463 /* 3464 * Check to see if the config has changed. 3465 */ 3466 if (error || generation != spa->spa_config_generation) { 3467 metaslab_group_activate(mg); 3468 if (error) 3469 return (spa_vdev_state_exit(spa, 3470 vd, error)); 3471 (void) spa_vdev_state_exit(spa, vd, 0); 3472 goto top; 3473 } 3474 ASSERT0(tvd->vdev_stat.vs_alloc); 3475 } 3476 3477 /* 3478 * Offline this device and reopen its top-level vdev. 3479 * If the top-level vdev is a log device then just offline 3480 * it. Otherwise, if this action results in the top-level 3481 * vdev becoming unusable, undo it and fail the request. 3482 */ 3483 vd->vdev_offline = B_TRUE; 3484 vdev_reopen(tvd); 3485 3486 if (!tvd->vdev_islog && vd->vdev_aux == NULL && 3487 vdev_is_dead(tvd)) { 3488 vd->vdev_offline = B_FALSE; 3489 vdev_reopen(tvd); 3490 return (spa_vdev_state_exit(spa, NULL, EBUSY)); 3491 } 3492 3493 /* 3494 * Add the device back into the metaslab rotor so that 3495 * once we online the device it's open for business. 3496 */ 3497 if (tvd->vdev_islog && mg != NULL) 3498 metaslab_group_activate(mg); 3499 } 3500 3501 vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY); 3502 3503 return (spa_vdev_state_exit(spa, vd, 0)); 3504 } 3505 3506 int 3507 vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags) 3508 { 3509 int error; 3510 3511 mutex_enter(&spa->spa_vdev_top_lock); 3512 error = vdev_offline_locked(spa, guid, flags); 3513 mutex_exit(&spa->spa_vdev_top_lock); 3514 3515 return (error); 3516 } 3517 3518 /* 3519 * Clear the error counts associated with this vdev. Unlike vdev_online() and 3520 * vdev_offline(), we assume the spa config is locked. We also clear all 3521 * children. If 'vd' is NULL, then the user wants to clear all vdevs. 3522 */ 3523 void 3524 vdev_clear(spa_t *spa, vdev_t *vd) 3525 { 3526 vdev_t *rvd = spa->spa_root_vdev; 3527 3528 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 3529 3530 if (vd == NULL) 3531 vd = rvd; 3532 3533 vd->vdev_stat.vs_read_errors = 0; 3534 vd->vdev_stat.vs_write_errors = 0; 3535 vd->vdev_stat.vs_checksum_errors = 0; 3536 vd->vdev_stat.vs_slow_ios = 0; 3537 3538 for (int c = 0; c < vd->vdev_children; c++) 3539 vdev_clear(spa, vd->vdev_child[c]); 3540 3541 /* 3542 * It makes no sense to "clear" an indirect vdev. 3543 */ 3544 if (!vdev_is_concrete(vd)) 3545 return; 3546 3547 /* 3548 * If we're in the FAULTED state or have experienced failed I/O, then 3549 * clear the persistent state and attempt to reopen the device. We 3550 * also mark the vdev config dirty, so that the new faulted state is 3551 * written out to disk. 3552 */ 3553 if (vd->vdev_faulted || vd->vdev_degraded || 3554 !vdev_readable(vd) || !vdev_writeable(vd)) { 3555 3556 /* 3557 * When reopening in reponse to a clear event, it may be due to 3558 * a fmadm repair request. In this case, if the device is 3559 * still broken, we want to still post the ereport again. 3560 */ 3561 vd->vdev_forcefault = B_TRUE; 3562 3563 vd->vdev_faulted = vd->vdev_degraded = 0ULL; 3564 vd->vdev_cant_read = B_FALSE; 3565 vd->vdev_cant_write = B_FALSE; 3566 3567 vdev_reopen(vd == rvd ? rvd : vd->vdev_top); 3568 3569 vd->vdev_forcefault = B_FALSE; 3570 3571 if (vd != rvd && vdev_writeable(vd->vdev_top)) 3572 vdev_state_dirty(vd->vdev_top); 3573 3574 /* If a resilver isn't required, check if vdevs can be culled */ 3575 if (vd->vdev_aux == NULL && !vdev_is_dead(vd) && 3576 !dsl_scan_resilvering(spa->spa_dsl_pool) && 3577 !dsl_scan_resilver_scheduled(spa->spa_dsl_pool)) 3578 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); 3579 3580 spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_CLEAR); 3581 } 3582 3583 /* 3584 * When clearing a FMA-diagnosed fault, we always want to 3585 * unspare the device, as we assume that the original spare was 3586 * done in response to the FMA fault. 3587 */ 3588 if (!vdev_is_dead(vd) && vd->vdev_parent != NULL && 3589 vd->vdev_parent->vdev_ops == &vdev_spare_ops && 3590 vd->vdev_parent->vdev_child[0] == vd) 3591 vd->vdev_unspare = B_TRUE; 3592 } 3593 3594 boolean_t 3595 vdev_is_dead(vdev_t *vd) 3596 { 3597 /* 3598 * Holes and missing devices are always considered "dead". 3599 * This simplifies the code since we don't have to check for 3600 * these types of devices in the various code paths. 3601 * Instead we rely on the fact that we skip over dead devices 3602 * before issuing I/O to them. 3603 */ 3604 return (vd->vdev_state < VDEV_STATE_DEGRADED || 3605 vd->vdev_ops == &vdev_hole_ops || 3606 vd->vdev_ops == &vdev_missing_ops); 3607 } 3608 3609 boolean_t 3610 vdev_readable(vdev_t *vd) 3611 { 3612 return (!vdev_is_dead(vd) && !vd->vdev_cant_read); 3613 } 3614 3615 boolean_t 3616 vdev_writeable(vdev_t *vd) 3617 { 3618 return (!vdev_is_dead(vd) && !vd->vdev_cant_write && 3619 vdev_is_concrete(vd)); 3620 } 3621 3622 boolean_t 3623 vdev_allocatable(vdev_t *vd) 3624 { 3625 uint64_t state = vd->vdev_state; 3626 3627 /* 3628 * We currently allow allocations from vdevs which may be in the 3629 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device 3630 * fails to reopen then we'll catch it later when we're holding 3631 * the proper locks. Note that we have to get the vdev state 3632 * in a local variable because although it changes atomically, 3633 * we're asking two separate questions about it. 3634 */ 3635 return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) && 3636 !vd->vdev_cant_write && vdev_is_concrete(vd) && 3637 vd->vdev_mg->mg_initialized); 3638 } 3639 3640 boolean_t 3641 vdev_accessible(vdev_t *vd, zio_t *zio) 3642 { 3643 ASSERT(zio->io_vd == vd); 3644 3645 if (vdev_is_dead(vd) || vd->vdev_remove_wanted) 3646 return (B_FALSE); 3647 3648 if (zio->io_type == ZIO_TYPE_READ) 3649 return (!vd->vdev_cant_read); 3650 3651 if (zio->io_type == ZIO_TYPE_WRITE) 3652 return (!vd->vdev_cant_write); 3653 3654 return (B_TRUE); 3655 } 3656 3657 static void 3658 vdev_get_child_stat(vdev_t *cvd, vdev_stat_t *vs, vdev_stat_t *cvs) 3659 { 3660 for (int t = 0; t < VS_ZIO_TYPES; t++) { 3661 vs->vs_ops[t] += cvs->vs_ops[t]; 3662 vs->vs_bytes[t] += cvs->vs_bytes[t]; 3663 } 3664 3665 cvs->vs_scan_removing = cvd->vdev_removing; 3666 } 3667 3668 /* 3669 * Get extended stats 3670 */ 3671 static void 3672 vdev_get_child_stat_ex(vdev_t *cvd, vdev_stat_ex_t *vsx, vdev_stat_ex_t *cvsx) 3673 { 3674 int t, b; 3675 for (t = 0; t < ZIO_TYPES; t++) { 3676 for (b = 0; b < ARRAY_SIZE(vsx->vsx_disk_histo[0]); b++) 3677 vsx->vsx_disk_histo[t][b] += cvsx->vsx_disk_histo[t][b]; 3678 3679 for (b = 0; b < ARRAY_SIZE(vsx->vsx_total_histo[0]); b++) { 3680 vsx->vsx_total_histo[t][b] += 3681 cvsx->vsx_total_histo[t][b]; 3682 } 3683 } 3684 3685 for (t = 0; t < ZIO_PRIORITY_NUM_QUEUEABLE; t++) { 3686 for (b = 0; b < ARRAY_SIZE(vsx->vsx_queue_histo[0]); b++) { 3687 vsx->vsx_queue_histo[t][b] += 3688 cvsx->vsx_queue_histo[t][b]; 3689 } 3690 vsx->vsx_active_queue[t] += cvsx->vsx_active_queue[t]; 3691 vsx->vsx_pend_queue[t] += cvsx->vsx_pend_queue[t]; 3692 3693 for (b = 0; b < ARRAY_SIZE(vsx->vsx_ind_histo[0]); b++) 3694 vsx->vsx_ind_histo[t][b] += cvsx->vsx_ind_histo[t][b]; 3695 3696 for (b = 0; b < ARRAY_SIZE(vsx->vsx_agg_histo[0]); b++) 3697 vsx->vsx_agg_histo[t][b] += cvsx->vsx_agg_histo[t][b]; 3698 } 3699 3700 } 3701 3702 boolean_t 3703 vdev_is_spacemap_addressable(vdev_t *vd) 3704 { 3705 if (spa_feature_is_active(vd->vdev_spa, SPA_FEATURE_SPACEMAP_V2)) 3706 return (B_TRUE); 3707 3708 /* 3709 * If double-word space map entries are not enabled we assume 3710 * 47 bits of the space map entry are dedicated to the entry's 3711 * offset (see SM_OFFSET_BITS in space_map.h). We then use that 3712 * to calculate the maximum address that can be described by a 3713 * space map entry for the given device. 3714 */ 3715 uint64_t shift = vd->vdev_ashift + SM_OFFSET_BITS; 3716 3717 if (shift >= 63) /* detect potential overflow */ 3718 return (B_TRUE); 3719 3720 return (vd->vdev_asize < (1ULL << shift)); 3721 } 3722 3723 /* 3724 * Get statistics for the given vdev. 3725 */ 3726 static void 3727 vdev_get_stats_ex_impl(vdev_t *vd, vdev_stat_t *vs, vdev_stat_ex_t *vsx) 3728 { 3729 int t; 3730 /* 3731 * If we're getting stats on the root vdev, aggregate the I/O counts 3732 * over all top-level vdevs (i.e. the direct children of the root). 3733 */ 3734 if (!vd->vdev_ops->vdev_op_leaf) { 3735 if (vs) { 3736 memset(vs->vs_ops, 0, sizeof (vs->vs_ops)); 3737 memset(vs->vs_bytes, 0, sizeof (vs->vs_bytes)); 3738 } 3739 if (vsx) 3740 memset(vsx, 0, sizeof (*vsx)); 3741 3742 for (int c = 0; c < vd->vdev_children; c++) { 3743 vdev_t *cvd = vd->vdev_child[c]; 3744 vdev_stat_t *cvs = &cvd->vdev_stat; 3745 vdev_stat_ex_t *cvsx = &cvd->vdev_stat_ex; 3746 3747 vdev_get_stats_ex_impl(cvd, cvs, cvsx); 3748 if (vs) 3749 vdev_get_child_stat(cvd, vs, cvs); 3750 if (vsx) 3751 vdev_get_child_stat_ex(cvd, vsx, cvsx); 3752 3753 } 3754 } else { 3755 /* 3756 * We're a leaf. Just copy our ZIO active queue stats in. The 3757 * other leaf stats are updated in vdev_stat_update(). 3758 */ 3759 if (!vsx) 3760 return; 3761 3762 memcpy(vsx, &vd->vdev_stat_ex, sizeof (vd->vdev_stat_ex)); 3763 3764 for (t = 0; t < ARRAY_SIZE(vd->vdev_queue.vq_class); t++) { 3765 vsx->vsx_active_queue[t] = 3766 vd->vdev_queue.vq_class[t].vqc_active; 3767 vsx->vsx_pend_queue[t] = avl_numnodes( 3768 &vd->vdev_queue.vq_class[t].vqc_queued_tree); 3769 } 3770 } 3771 } 3772 3773 void 3774 vdev_get_stats_ex(vdev_t *vd, vdev_stat_t *vs, vdev_stat_ex_t *vsx) 3775 { 3776 vdev_t *tvd = vd->vdev_top; 3777 mutex_enter(&vd->vdev_stat_lock); 3778 if (vs) { 3779 bcopy(&vd->vdev_stat, vs, sizeof (*vs)); 3780 vs->vs_timestamp = gethrtime() - vs->vs_timestamp; 3781 vs->vs_state = vd->vdev_state; 3782 vs->vs_rsize = vdev_get_min_asize(vd); 3783 if (vd->vdev_ops->vdev_op_leaf) { 3784 vs->vs_rsize += VDEV_LABEL_START_SIZE + 3785 VDEV_LABEL_END_SIZE; 3786 /* 3787 * Report initializing progress. Since we don't 3788 * have the initializing locks held, this is only 3789 * an estimate (although a fairly accurate one). 3790 */ 3791 vs->vs_initialize_bytes_done = 3792 vd->vdev_initialize_bytes_done; 3793 vs->vs_initialize_bytes_est = 3794 vd->vdev_initialize_bytes_est; 3795 vs->vs_initialize_state = vd->vdev_initialize_state; 3796 vs->vs_initialize_action_time = 3797 vd->vdev_initialize_action_time; 3798 3799 /* 3800 * Report manual TRIM progress. Since we don't have 3801 * the manual TRIM locks held, this is only an 3802 * estimate (although fairly accurate one). 3803 */ 3804 vs->vs_trim_notsup = !vd->vdev_has_trim; 3805 vs->vs_trim_bytes_done = vd->vdev_trim_bytes_done; 3806 vs->vs_trim_bytes_est = vd->vdev_trim_bytes_est; 3807 vs->vs_trim_state = vd->vdev_trim_state; 3808 vs->vs_trim_action_time = vd->vdev_trim_action_time; 3809 } 3810 /* 3811 * Report expandable space on top-level, non-auxiliary devices 3812 * only. The expandable space is reported in terms of metaslab 3813 * sized units since that determines how much space the pool 3814 * can expand. 3815 */ 3816 if (vd->vdev_aux == NULL && tvd != NULL) { 3817 vs->vs_esize = P2ALIGN( 3818 vd->vdev_max_asize - vd->vdev_asize, 3819 1ULL << tvd->vdev_ms_shift); 3820 } 3821 if (vd->vdev_aux == NULL && vd == vd->vdev_top && 3822 vdev_is_concrete(vd)) { 3823 vs->vs_fragmentation = (vd->vdev_mg != NULL) ? 3824 vd->vdev_mg->mg_fragmentation : 0; 3825 } 3826 if (vd->vdev_ops->vdev_op_leaf) 3827 vs->vs_resilver_deferred = vd->vdev_resilver_deferred; 3828 } 3829 3830 vdev_get_stats_ex_impl(vd, vs, vsx); 3831 mutex_exit(&vd->vdev_stat_lock); 3832 } 3833 3834 void 3835 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs) 3836 { 3837 return (vdev_get_stats_ex(vd, vs, NULL)); 3838 } 3839 3840 void 3841 vdev_clear_stats(vdev_t *vd) 3842 { 3843 mutex_enter(&vd->vdev_stat_lock); 3844 vd->vdev_stat.vs_space = 0; 3845 vd->vdev_stat.vs_dspace = 0; 3846 vd->vdev_stat.vs_alloc = 0; 3847 mutex_exit(&vd->vdev_stat_lock); 3848 } 3849 3850 void 3851 vdev_scan_stat_init(vdev_t *vd) 3852 { 3853 vdev_stat_t *vs = &vd->vdev_stat; 3854 3855 for (int c = 0; c < vd->vdev_children; c++) 3856 vdev_scan_stat_init(vd->vdev_child[c]); 3857 3858 mutex_enter(&vd->vdev_stat_lock); 3859 vs->vs_scan_processed = 0; 3860 mutex_exit(&vd->vdev_stat_lock); 3861 } 3862 3863 void 3864 vdev_stat_update(zio_t *zio, uint64_t psize) 3865 { 3866 spa_t *spa = zio->io_spa; 3867 vdev_t *rvd = spa->spa_root_vdev; 3868 vdev_t *vd = zio->io_vd ? zio->io_vd : rvd; 3869 vdev_t *pvd; 3870 uint64_t txg = zio->io_txg; 3871 vdev_stat_t *vs = &vd->vdev_stat; 3872 vdev_stat_ex_t *vsx = &vd->vdev_stat_ex; 3873 zio_type_t type = zio->io_type; 3874 int flags = zio->io_flags; 3875 3876 /* 3877 * If this i/o is a gang leader, it didn't do any actual work. 3878 */ 3879 if (zio->io_gang_tree) 3880 return; 3881 3882 if (zio->io_error == 0) { 3883 /* 3884 * If this is a root i/o, don't count it -- we've already 3885 * counted the top-level vdevs, and vdev_get_stats() will 3886 * aggregate them when asked. This reduces contention on 3887 * the root vdev_stat_lock and implicitly handles blocks 3888 * that compress away to holes, for which there is no i/o. 3889 * (Holes never create vdev children, so all the counters 3890 * remain zero, which is what we want.) 3891 * 3892 * Note: this only applies to successful i/o (io_error == 0) 3893 * because unlike i/o counts, errors are not additive. 3894 * When reading a ditto block, for example, failure of 3895 * one top-level vdev does not imply a root-level error. 3896 */ 3897 if (vd == rvd) 3898 return; 3899 3900 ASSERT(vd == zio->io_vd); 3901 3902 if (flags & ZIO_FLAG_IO_BYPASS) 3903 return; 3904 3905 mutex_enter(&vd->vdev_stat_lock); 3906 3907 if (flags & ZIO_FLAG_IO_REPAIR) { 3908 if (flags & ZIO_FLAG_SCAN_THREAD) { 3909 dsl_scan_phys_t *scn_phys = 3910 &spa->spa_dsl_pool->dp_scan->scn_phys; 3911 uint64_t *processed = &scn_phys->scn_processed; 3912 3913 /* XXX cleanup? */ 3914 if (vd->vdev_ops->vdev_op_leaf) 3915 atomic_add_64(processed, psize); 3916 vs->vs_scan_processed += psize; 3917 } 3918 3919 if (flags & ZIO_FLAG_SELF_HEAL) 3920 vs->vs_self_healed += psize; 3921 } 3922 3923 /* 3924 * The bytes/ops/histograms are recorded at the leaf level and 3925 * aggregated into the higher level vdevs in vdev_get_stats(). 3926 */ 3927 if (vd->vdev_ops->vdev_op_leaf && 3928 (zio->io_priority < ZIO_PRIORITY_NUM_QUEUEABLE)) { 3929 zio_type_t vs_type = type; 3930 3931 /* 3932 * TRIM ops and bytes are reported to user space as 3933 * ZIO_TYPE_IOCTL. This is done to preserve the 3934 * vdev_stat_t structure layout for user space. 3935 */ 3936 if (type == ZIO_TYPE_TRIM) 3937 vs_type = ZIO_TYPE_IOCTL; 3938 3939 vs->vs_ops[vs_type]++; 3940 vs->vs_bytes[vs_type] += psize; 3941 3942 if (flags & ZIO_FLAG_DELEGATED) { 3943 vsx->vsx_agg_histo[zio->io_priority] 3944 [RQ_HISTO(zio->io_size)]++; 3945 } else { 3946 vsx->vsx_ind_histo[zio->io_priority] 3947 [RQ_HISTO(zio->io_size)]++; 3948 } 3949 3950 if (zio->io_delta && zio->io_delay) { 3951 vsx->vsx_queue_histo[zio->io_priority] 3952 [L_HISTO(zio->io_delta - zio->io_delay)]++; 3953 vsx->vsx_disk_histo[type] 3954 [L_HISTO(zio->io_delay)]++; 3955 vsx->vsx_total_histo[type] 3956 [L_HISTO(zio->io_delta)]++; 3957 } 3958 } 3959 3960 mutex_exit(&vd->vdev_stat_lock); 3961 return; 3962 } 3963 3964 if (flags & ZIO_FLAG_SPECULATIVE) 3965 return; 3966 3967 /* 3968 * If this is an I/O error that is going to be retried, then ignore the 3969 * error. Otherwise, the user may interpret B_FAILFAST I/O errors as 3970 * hard errors, when in reality they can happen for any number of 3971 * innocuous reasons (bus resets, MPxIO link failure, etc). 3972 */ 3973 if (zio->io_error == EIO && 3974 !(zio->io_flags & ZIO_FLAG_IO_RETRY)) 3975 return; 3976 3977 /* 3978 * Intent logs writes won't propagate their error to the root 3979 * I/O so don't mark these types of failures as pool-level 3980 * errors. 3981 */ 3982 if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE)) 3983 return; 3984 3985 mutex_enter(&vd->vdev_stat_lock); 3986 if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) { 3987 if (zio->io_error == ECKSUM) 3988 vs->vs_checksum_errors++; 3989 else 3990 vs->vs_read_errors++; 3991 } 3992 if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd)) 3993 vs->vs_write_errors++; 3994 mutex_exit(&vd->vdev_stat_lock); 3995 3996 if (spa->spa_load_state == SPA_LOAD_NONE && 3997 type == ZIO_TYPE_WRITE && txg != 0 && 3998 (!(flags & ZIO_FLAG_IO_REPAIR) || 3999 (flags & ZIO_FLAG_SCAN_THREAD) || 4000 spa->spa_claiming)) { 4001 /* 4002 * This is either a normal write (not a repair), or it's 4003 * a repair induced by the scrub thread, or it's a repair 4004 * made by zil_claim() during spa_load() in the first txg. 4005 * In the normal case, we commit the DTL change in the same 4006 * txg as the block was born. In the scrub-induced repair 4007 * case, we know that scrubs run in first-pass syncing context, 4008 * so we commit the DTL change in spa_syncing_txg(spa). 4009 * In the zil_claim() case, we commit in spa_first_txg(spa). 4010 * 4011 * We currently do not make DTL entries for failed spontaneous 4012 * self-healing writes triggered by normal (non-scrubbing) 4013 * reads, because we have no transactional context in which to 4014 * do so -- and it's not clear that it'd be desirable anyway. 4015 */ 4016 if (vd->vdev_ops->vdev_op_leaf) { 4017 uint64_t commit_txg = txg; 4018 if (flags & ZIO_FLAG_SCAN_THREAD) { 4019 ASSERT(flags & ZIO_FLAG_IO_REPAIR); 4020 ASSERT(spa_sync_pass(spa) == 1); 4021 vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1); 4022 commit_txg = spa_syncing_txg(spa); 4023 } else if (spa->spa_claiming) { 4024 ASSERT(flags & ZIO_FLAG_IO_REPAIR); 4025 commit_txg = spa_first_txg(spa); 4026 } 4027 ASSERT(commit_txg >= spa_syncing_txg(spa)); 4028 if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1)) 4029 return; 4030 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent) 4031 vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1); 4032 vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg); 4033 } 4034 if (vd != rvd) 4035 vdev_dtl_dirty(vd, DTL_MISSING, txg, 1); 4036 } 4037 } 4038 4039 int64_t 4040 vdev_deflated_space(vdev_t *vd, int64_t space) 4041 { 4042 ASSERT((space & (SPA_MINBLOCKSIZE-1)) == 0); 4043 ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache); 4044 4045 return ((space >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio); 4046 } 4047 4048 /* 4049 * Update the in-core space usage stats for this vdev, its metaslab class, 4050 * and the root vdev. 4051 */ 4052 void 4053 vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta, 4054 int64_t space_delta) 4055 { 4056 int64_t dspace_delta; 4057 spa_t *spa = vd->vdev_spa; 4058 vdev_t *rvd = spa->spa_root_vdev; 4059 4060 ASSERT(vd == vd->vdev_top); 4061 4062 /* 4063 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion 4064 * factor. We must calculate this here and not at the root vdev 4065 * because the root vdev's psize-to-asize is simply the max of its 4066 * childrens', thus not accurate enough for us. 4067 */ 4068 dspace_delta = vdev_deflated_space(vd, space_delta); 4069 4070 mutex_enter(&vd->vdev_stat_lock); 4071 /* ensure we won't underflow */ 4072 if (alloc_delta < 0) { 4073 ASSERT3U(vd->vdev_stat.vs_alloc, >=, -alloc_delta); 4074 } 4075 4076 vd->vdev_stat.vs_alloc += alloc_delta; 4077 vd->vdev_stat.vs_space += space_delta; 4078 vd->vdev_stat.vs_dspace += dspace_delta; 4079 mutex_exit(&vd->vdev_stat_lock); 4080 4081 /* every class but log contributes to root space stats */ 4082 if (vd->vdev_mg != NULL && !vd->vdev_islog) { 4083 ASSERT(!vd->vdev_isl2cache); 4084 mutex_enter(&rvd->vdev_stat_lock); 4085 rvd->vdev_stat.vs_alloc += alloc_delta; 4086 rvd->vdev_stat.vs_space += space_delta; 4087 rvd->vdev_stat.vs_dspace += dspace_delta; 4088 mutex_exit(&rvd->vdev_stat_lock); 4089 } 4090 /* Note: metaslab_class_space_update moved to metaslab_space_update */ 4091 } 4092 4093 /* 4094 * Mark a top-level vdev's config as dirty, placing it on the dirty list 4095 * so that it will be written out next time the vdev configuration is synced. 4096 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs. 4097 */ 4098 void 4099 vdev_config_dirty(vdev_t *vd) 4100 { 4101 spa_t *spa = vd->vdev_spa; 4102 vdev_t *rvd = spa->spa_root_vdev; 4103 int c; 4104 4105 ASSERT(spa_writeable(spa)); 4106 4107 /* 4108 * If this is an aux vdev (as with l2cache and spare devices), then we 4109 * update the vdev config manually and set the sync flag. 4110 */ 4111 if (vd->vdev_aux != NULL) { 4112 spa_aux_vdev_t *sav = vd->vdev_aux; 4113 nvlist_t **aux; 4114 uint_t naux; 4115 4116 for (c = 0; c < sav->sav_count; c++) { 4117 if (sav->sav_vdevs[c] == vd) 4118 break; 4119 } 4120 4121 if (c == sav->sav_count) { 4122 /* 4123 * We're being removed. There's nothing more to do. 4124 */ 4125 ASSERT(sav->sav_sync == B_TRUE); 4126 return; 4127 } 4128 4129 sav->sav_sync = B_TRUE; 4130 4131 if (nvlist_lookup_nvlist_array(sav->sav_config, 4132 ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) { 4133 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, 4134 ZPOOL_CONFIG_SPARES, &aux, &naux) == 0); 4135 } 4136 4137 ASSERT(c < naux); 4138 4139 /* 4140 * Setting the nvlist in the middle if the array is a little 4141 * sketchy, but it will work. 4142 */ 4143 nvlist_free(aux[c]); 4144 aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0); 4145 4146 return; 4147 } 4148 4149 /* 4150 * The dirty list is protected by the SCL_CONFIG lock. The caller 4151 * must either hold SCL_CONFIG as writer, or must be the sync thread 4152 * (which holds SCL_CONFIG as reader). There's only one sync thread, 4153 * so this is sufficient to ensure mutual exclusion. 4154 */ 4155 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 4156 (dsl_pool_sync_context(spa_get_dsl(spa)) && 4157 spa_config_held(spa, SCL_CONFIG, RW_READER))); 4158 4159 if (vd == rvd) { 4160 for (c = 0; c < rvd->vdev_children; c++) 4161 vdev_config_dirty(rvd->vdev_child[c]); 4162 } else { 4163 ASSERT(vd == vd->vdev_top); 4164 4165 if (!list_link_active(&vd->vdev_config_dirty_node) && 4166 vdev_is_concrete(vd)) { 4167 list_insert_head(&spa->spa_config_dirty_list, vd); 4168 } 4169 } 4170 } 4171 4172 void 4173 vdev_config_clean(vdev_t *vd) 4174 { 4175 spa_t *spa = vd->vdev_spa; 4176 4177 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) || 4178 (dsl_pool_sync_context(spa_get_dsl(spa)) && 4179 spa_config_held(spa, SCL_CONFIG, RW_READER))); 4180 4181 ASSERT(list_link_active(&vd->vdev_config_dirty_node)); 4182 list_remove(&spa->spa_config_dirty_list, vd); 4183 } 4184 4185 /* 4186 * Mark a top-level vdev's state as dirty, so that the next pass of 4187 * spa_sync() can convert this into vdev_config_dirty(). We distinguish 4188 * the state changes from larger config changes because they require 4189 * much less locking, and are often needed for administrative actions. 4190 */ 4191 void 4192 vdev_state_dirty(vdev_t *vd) 4193 { 4194 spa_t *spa = vd->vdev_spa; 4195 4196 ASSERT(spa_writeable(spa)); 4197 ASSERT(vd == vd->vdev_top); 4198 4199 /* 4200 * The state list is protected by the SCL_STATE lock. The caller 4201 * must either hold SCL_STATE as writer, or must be the sync thread 4202 * (which holds SCL_STATE as reader). There's only one sync thread, 4203 * so this is sufficient to ensure mutual exclusion. 4204 */ 4205 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 4206 (dsl_pool_sync_context(spa_get_dsl(spa)) && 4207 spa_config_held(spa, SCL_STATE, RW_READER))); 4208 4209 if (!list_link_active(&vd->vdev_state_dirty_node) && 4210 vdev_is_concrete(vd)) 4211 list_insert_head(&spa->spa_state_dirty_list, vd); 4212 } 4213 4214 void 4215 vdev_state_clean(vdev_t *vd) 4216 { 4217 spa_t *spa = vd->vdev_spa; 4218 4219 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) || 4220 (dsl_pool_sync_context(spa_get_dsl(spa)) && 4221 spa_config_held(spa, SCL_STATE, RW_READER))); 4222 4223 ASSERT(list_link_active(&vd->vdev_state_dirty_node)); 4224 list_remove(&spa->spa_state_dirty_list, vd); 4225 } 4226 4227 /* 4228 * Propagate vdev state up from children to parent. 4229 */ 4230 void 4231 vdev_propagate_state(vdev_t *vd) 4232 { 4233 spa_t *spa = vd->vdev_spa; 4234 vdev_t *rvd = spa->spa_root_vdev; 4235 int degraded = 0, faulted = 0; 4236 int corrupted = 0; 4237 vdev_t *child; 4238 4239 if (vd->vdev_children > 0) { 4240 for (int c = 0; c < vd->vdev_children; c++) { 4241 child = vd->vdev_child[c]; 4242 4243 /* 4244 * Don't factor holes or indirect vdevs into the 4245 * decision. 4246 */ 4247 if (!vdev_is_concrete(child)) 4248 continue; 4249 4250 if (!vdev_readable(child) || 4251 (!vdev_writeable(child) && spa_writeable(spa))) { 4252 /* 4253 * Root special: if there is a top-level log 4254 * device, treat the root vdev as if it were 4255 * degraded. 4256 */ 4257 if (child->vdev_islog && vd == rvd) 4258 degraded++; 4259 else 4260 faulted++; 4261 } else if (child->vdev_state <= VDEV_STATE_DEGRADED) { 4262 degraded++; 4263 } 4264 4265 if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA) 4266 corrupted++; 4267 } 4268 4269 vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded); 4270 4271 /* 4272 * Root special: if there is a top-level vdev that cannot be 4273 * opened due to corrupted metadata, then propagate the root 4274 * vdev's aux state as 'corrupt' rather than 'insufficient 4275 * replicas'. 4276 */ 4277 if (corrupted && vd == rvd && 4278 rvd->vdev_state == VDEV_STATE_CANT_OPEN) 4279 vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN, 4280 VDEV_AUX_CORRUPT_DATA); 4281 } 4282 4283 if (vd->vdev_parent) 4284 vdev_propagate_state(vd->vdev_parent); 4285 } 4286 4287 /* 4288 * Set a vdev's state. If this is during an open, we don't update the parent 4289 * state, because we're in the process of opening children depth-first. 4290 * Otherwise, we propagate the change to the parent. 4291 * 4292 * If this routine places a device in a faulted state, an appropriate ereport is 4293 * generated. 4294 */ 4295 void 4296 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux) 4297 { 4298 uint64_t save_state; 4299 spa_t *spa = vd->vdev_spa; 4300 4301 if (state == vd->vdev_state) { 4302 vd->vdev_stat.vs_aux = aux; 4303 return; 4304 } 4305 4306 save_state = vd->vdev_state; 4307 4308 vd->vdev_state = state; 4309 vd->vdev_stat.vs_aux = aux; 4310 4311 /* 4312 * If we are setting the vdev state to anything but an open state, then 4313 * always close the underlying device unless the device has requested 4314 * a delayed close (i.e. we're about to remove or fault the device). 4315 * Otherwise, we keep accessible but invalid devices open forever. 4316 * We don't call vdev_close() itself, because that implies some extra 4317 * checks (offline, etc) that we don't want here. This is limited to 4318 * leaf devices, because otherwise closing the device will affect other 4319 * children. 4320 */ 4321 if (!vd->vdev_delayed_close && vdev_is_dead(vd) && 4322 vd->vdev_ops->vdev_op_leaf) 4323 vd->vdev_ops->vdev_op_close(vd); 4324 4325 /* 4326 * If we have brought this vdev back into service, we need 4327 * to notify fmd so that it can gracefully repair any outstanding 4328 * cases due to a missing device. We do this in all cases, even those 4329 * that probably don't correlate to a repaired fault. This is sure to 4330 * catch all cases, and we let the zfs-retire agent sort it out. If 4331 * this is a transient state it's OK, as the retire agent will 4332 * double-check the state of the vdev before repairing it. 4333 */ 4334 if (state == VDEV_STATE_HEALTHY && vd->vdev_ops->vdev_op_leaf && 4335 vd->vdev_prevstate != state) 4336 zfs_post_state_change(spa, vd); 4337 4338 if (vd->vdev_removed && 4339 state == VDEV_STATE_CANT_OPEN && 4340 (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) { 4341 /* 4342 * If the previous state is set to VDEV_STATE_REMOVED, then this 4343 * device was previously marked removed and someone attempted to 4344 * reopen it. If this failed due to a nonexistent device, then 4345 * keep the device in the REMOVED state. We also let this be if 4346 * it is one of our special test online cases, which is only 4347 * attempting to online the device and shouldn't generate an FMA 4348 * fault. 4349 */ 4350 vd->vdev_state = VDEV_STATE_REMOVED; 4351 vd->vdev_stat.vs_aux = VDEV_AUX_NONE; 4352 } else if (state == VDEV_STATE_REMOVED) { 4353 vd->vdev_removed = B_TRUE; 4354 } else if (state == VDEV_STATE_CANT_OPEN) { 4355 /* 4356 * If we fail to open a vdev during an import or recovery, we 4357 * mark it as "not available", which signifies that it was 4358 * never there to begin with. Failure to open such a device 4359 * is not considered an error. 4360 */ 4361 if ((spa_load_state(spa) == SPA_LOAD_IMPORT || 4362 spa_load_state(spa) == SPA_LOAD_RECOVER) && 4363 vd->vdev_ops->vdev_op_leaf) 4364 vd->vdev_not_present = 1; 4365 4366 /* 4367 * Post the appropriate ereport. If the 'prevstate' field is 4368 * set to something other than VDEV_STATE_UNKNOWN, it indicates 4369 * that this is part of a vdev_reopen(). In this case, we don't 4370 * want to post the ereport if the device was already in the 4371 * CANT_OPEN state beforehand. 4372 * 4373 * If the 'checkremove' flag is set, then this is an attempt to 4374 * online the device in response to an insertion event. If we 4375 * hit this case, then we have detected an insertion event for a 4376 * faulted or offline device that wasn't in the removed state. 4377 * In this scenario, we don't post an ereport because we are 4378 * about to replace the device, or attempt an online with 4379 * vdev_forcefault, which will generate the fault for us. 4380 */ 4381 if ((vd->vdev_prevstate != state || vd->vdev_forcefault) && 4382 !vd->vdev_not_present && !vd->vdev_checkremove && 4383 vd != spa->spa_root_vdev) { 4384 const char *class; 4385 4386 switch (aux) { 4387 case VDEV_AUX_OPEN_FAILED: 4388 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED; 4389 break; 4390 case VDEV_AUX_CORRUPT_DATA: 4391 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA; 4392 break; 4393 case VDEV_AUX_NO_REPLICAS: 4394 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS; 4395 break; 4396 case VDEV_AUX_BAD_GUID_SUM: 4397 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM; 4398 break; 4399 case VDEV_AUX_TOO_SMALL: 4400 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL; 4401 break; 4402 case VDEV_AUX_BAD_LABEL: 4403 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL; 4404 break; 4405 case VDEV_AUX_BAD_ASHIFT: 4406 class = FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT; 4407 break; 4408 default: 4409 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN; 4410 } 4411 4412 (void) zfs_ereport_post(class, spa, vd, NULL, NULL, 4413 save_state, 0); 4414 } 4415 4416 /* Erase any notion of persistent removed state */ 4417 vd->vdev_removed = B_FALSE; 4418 } else { 4419 vd->vdev_removed = B_FALSE; 4420 } 4421 4422 if (!isopen && vd->vdev_parent) 4423 vdev_propagate_state(vd->vdev_parent); 4424 } 4425 4426 boolean_t 4427 vdev_children_are_offline(vdev_t *vd) 4428 { 4429 ASSERT(!vd->vdev_ops->vdev_op_leaf); 4430 4431 for (uint64_t i = 0; i < vd->vdev_children; i++) { 4432 if (vd->vdev_child[i]->vdev_state != VDEV_STATE_OFFLINE) 4433 return (B_FALSE); 4434 } 4435 4436 return (B_TRUE); 4437 } 4438 4439 /* 4440 * Check the vdev configuration to ensure that it's capable of supporting 4441 * a root pool. We do not support partial configuration. 4442 */ 4443 boolean_t 4444 vdev_is_bootable(vdev_t *vd) 4445 { 4446 if (!vd->vdev_ops->vdev_op_leaf) { 4447 char *vdev_type = vd->vdev_ops->vdev_op_type; 4448 4449 if (strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) { 4450 return (B_FALSE); 4451 } 4452 } 4453 4454 for (int c = 0; c < vd->vdev_children; c++) { 4455 if (!vdev_is_bootable(vd->vdev_child[c])) 4456 return (B_FALSE); 4457 } 4458 return (B_TRUE); 4459 } 4460 4461 boolean_t 4462 vdev_is_concrete(vdev_t *vd) 4463 { 4464 vdev_ops_t *ops = vd->vdev_ops; 4465 if (ops == &vdev_indirect_ops || ops == &vdev_hole_ops || 4466 ops == &vdev_missing_ops || ops == &vdev_root_ops) { 4467 return (B_FALSE); 4468 } else { 4469 return (B_TRUE); 4470 } 4471 } 4472 4473 /* 4474 * Determine if a log device has valid content. If the vdev was 4475 * removed or faulted in the MOS config then we know that 4476 * the content on the log device has already been written to the pool. 4477 */ 4478 boolean_t 4479 vdev_log_state_valid(vdev_t *vd) 4480 { 4481 if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted && 4482 !vd->vdev_removed) 4483 return (B_TRUE); 4484 4485 for (int c = 0; c < vd->vdev_children; c++) 4486 if (vdev_log_state_valid(vd->vdev_child[c])) 4487 return (B_TRUE); 4488 4489 return (B_FALSE); 4490 } 4491 4492 /* 4493 * Expand a vdev if possible. 4494 */ 4495 void 4496 vdev_expand(vdev_t *vd, uint64_t txg) 4497 { 4498 ASSERT(vd->vdev_top == vd); 4499 ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL); 4500 ASSERT(vdev_is_concrete(vd)); 4501 4502 vdev_set_deflate_ratio(vd); 4503 4504 if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count && 4505 vdev_is_concrete(vd)) { 4506 vdev_metaslab_group_create(vd); 4507 VERIFY(vdev_metaslab_init(vd, txg) == 0); 4508 vdev_config_dirty(vd); 4509 } 4510 } 4511 4512 /* 4513 * Split a vdev. 4514 */ 4515 void 4516 vdev_split(vdev_t *vd) 4517 { 4518 vdev_t *cvd, *pvd = vd->vdev_parent; 4519 4520 vdev_remove_child(pvd, vd); 4521 vdev_compact_children(pvd); 4522 4523 cvd = pvd->vdev_child[0]; 4524 if (pvd->vdev_children == 1) { 4525 vdev_remove_parent(cvd); 4526 cvd->vdev_splitting = B_TRUE; 4527 } 4528 vdev_propagate_state(cvd); 4529 } 4530 4531 void 4532 vdev_deadman(vdev_t *vd) 4533 { 4534 for (int c = 0; c < vd->vdev_children; c++) { 4535 vdev_t *cvd = vd->vdev_child[c]; 4536 4537 vdev_deadman(cvd); 4538 } 4539 4540 if (vd->vdev_ops->vdev_op_leaf) { 4541 vdev_queue_t *vq = &vd->vdev_queue; 4542 4543 mutex_enter(&vq->vq_lock); 4544 if (avl_numnodes(&vq->vq_active_tree) > 0) { 4545 spa_t *spa = vd->vdev_spa; 4546 zio_t *fio; 4547 uint64_t delta; 4548 4549 /* 4550 * Look at the head of all the pending queues, 4551 * if any I/O has been outstanding for longer than 4552 * the spa_deadman_synctime we panic the system. 4553 */ 4554 fio = avl_first(&vq->vq_active_tree); 4555 delta = gethrtime() - fio->io_timestamp; 4556 if (delta > spa_deadman_synctime(spa)) { 4557 vdev_dbgmsg(vd, "SLOW IO: zio timestamp " 4558 "%lluns, delta %lluns, last io %lluns", 4559 fio->io_timestamp, (u_longlong_t)delta, 4560 vq->vq_io_complete_ts); 4561 fm_panic("I/O to pool '%s' appears to be " 4562 "hung.", spa_name(spa)); 4563 } 4564 } 4565 mutex_exit(&vq->vq_lock); 4566 } 4567 } 4568 4569 void 4570 vdev_defer_resilver(vdev_t *vd) 4571 { 4572 ASSERT(vd->vdev_ops->vdev_op_leaf); 4573 4574 vd->vdev_resilver_deferred = B_TRUE; 4575 vd->vdev_spa->spa_resilver_deferred = B_TRUE; 4576 } 4577 4578 /* 4579 * Clears the resilver deferred flag on all leaf devs under vd. Returns 4580 * B_TRUE if we have devices that need to be resilvered and are available to 4581 * accept resilver I/Os. 4582 */ 4583 boolean_t 4584 vdev_clear_resilver_deferred(vdev_t *vd, dmu_tx_t *tx) 4585 { 4586 boolean_t resilver_needed = B_FALSE; 4587 spa_t *spa = vd->vdev_spa; 4588 4589 for (int c = 0; c < vd->vdev_children; c++) { 4590 vdev_t *cvd = vd->vdev_child[c]; 4591 resilver_needed |= vdev_clear_resilver_deferred(cvd, tx); 4592 } 4593 4594 if (vd == spa->spa_root_vdev && 4595 spa_feature_is_active(spa, SPA_FEATURE_RESILVER_DEFER)) { 4596 spa_feature_decr(spa, SPA_FEATURE_RESILVER_DEFER, tx); 4597 vdev_config_dirty(vd); 4598 spa->spa_resilver_deferred = B_FALSE; 4599 return (resilver_needed); 4600 } 4601 4602 if (!vdev_is_concrete(vd) || vd->vdev_aux || 4603 !vd->vdev_ops->vdev_op_leaf) 4604 return (resilver_needed); 4605 4606 vd->vdev_resilver_deferred = B_FALSE; 4607 4608 return (!vdev_is_dead(vd) && !vd->vdev_offline && 4609 vdev_resilver_needed(vd, NULL, NULL)); 4610 } 4611 4612 /* 4613 * Translate a logical range to the physical range for the specified vdev_t. 4614 * This function is initially called with a leaf vdev and will walk each 4615 * parent vdev until it reaches a top-level vdev. Once the top-level is 4616 * reached the physical range is initialized and the recursive function 4617 * begins to unwind. As it unwinds it calls the parent's vdev specific 4618 * translation function to do the real conversion. 4619 */ 4620 void 4621 vdev_xlate(vdev_t *vd, const range_seg64_t *logical_rs, 4622 range_seg64_t *physical_rs) 4623 { 4624 /* 4625 * Walk up the vdev tree 4626 */ 4627 if (vd != vd->vdev_top) { 4628 vdev_xlate(vd->vdev_parent, logical_rs, physical_rs); 4629 } else { 4630 /* 4631 * We've reached the top-level vdev, initialize the 4632 * physical range to the logical range and start to 4633 * unwind. 4634 */ 4635 physical_rs->rs_start = logical_rs->rs_start; 4636 physical_rs->rs_end = logical_rs->rs_end; 4637 return; 4638 } 4639 4640 vdev_t *pvd = vd->vdev_parent; 4641 ASSERT3P(pvd, !=, NULL); 4642 ASSERT3P(pvd->vdev_ops->vdev_op_xlate, !=, NULL); 4643 4644 /* 4645 * As this recursive function unwinds, translate the logical 4646 * range into its physical components by calling the 4647 * vdev specific translate function. 4648 */ 4649 range_seg64_t intermediate = { 0 }; 4650 pvd->vdev_ops->vdev_op_xlate(vd, physical_rs, &intermediate); 4651 4652 physical_rs->rs_start = intermediate.rs_start; 4653 physical_rs->rs_end = intermediate.rs_end; 4654 } 4655