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