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