1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or https://opensource.org/licenses/CDDL-1.0. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright (c) 2011, 2020 by Delphix. All rights reserved. 25 * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>. All rights reserved. 26 */ 27 28 #include <sys/zfs_context.h> 29 #include <sys/spa_impl.h> 30 #include <sys/dmu.h> 31 #include <sys/dmu_tx.h> 32 #include <sys/zap.h> 33 #include <sys/vdev_impl.h> 34 #include <sys/metaslab.h> 35 #include <sys/metaslab_impl.h> 36 #include <sys/uberblock_impl.h> 37 #include <sys/txg.h> 38 #include <sys/avl.h> 39 #include <sys/bpobj.h> 40 #include <sys/dsl_pool.h> 41 #include <sys/dsl_synctask.h> 42 #include <sys/dsl_dir.h> 43 #include <sys/arc.h> 44 #include <sys/zfeature.h> 45 #include <sys/vdev_indirect_births.h> 46 #include <sys/vdev_indirect_mapping.h> 47 #include <sys/abd.h> 48 #include <sys/vdev_initialize.h> 49 #include <sys/vdev_trim.h> 50 #include <sys/trace_zfs.h> 51 52 /* 53 * This file contains the necessary logic to remove vdevs from a 54 * storage pool. Currently, the only devices that can be removed 55 * are log, cache, and spare devices; and top level vdevs from a pool 56 * w/o raidz or mirrors. (Note that members of a mirror can be removed 57 * by the detach operation.) 58 * 59 * Log vdevs are removed by evacuating them and then turning the vdev 60 * into a hole vdev while holding spa config locks. 61 * 62 * Top level vdevs are removed and converted into an indirect vdev via 63 * a multi-step process: 64 * 65 * - Disable allocations from this device (spa_vdev_remove_top). 66 * 67 * - From a new thread (spa_vdev_remove_thread), copy data from 68 * the removing vdev to a different vdev. The copy happens in open 69 * context (spa_vdev_copy_impl) and issues a sync task 70 * (vdev_mapping_sync) so the sync thread can update the partial 71 * indirect mappings in core and on disk. 72 * 73 * - If a free happens during a removal, it is freed from the 74 * removing vdev, and if it has already been copied, from the new 75 * location as well (free_from_removing_vdev). 76 * 77 * - After the removal is completed, the copy thread converts the vdev 78 * into an indirect vdev (vdev_remove_complete) before instructing 79 * the sync thread to destroy the space maps and finish the removal 80 * (spa_finish_removal). 81 */ 82 83 typedef struct vdev_copy_arg { 84 metaslab_t *vca_msp; 85 uint64_t vca_outstanding_bytes; 86 uint64_t vca_read_error_bytes; 87 uint64_t vca_write_error_bytes; 88 kcondvar_t vca_cv; 89 kmutex_t vca_lock; 90 } vdev_copy_arg_t; 91 92 /* 93 * The maximum amount of memory we can use for outstanding i/o while 94 * doing a device removal. This determines how much i/o we can have 95 * in flight concurrently. 96 */ 97 static const uint_t zfs_remove_max_copy_bytes = 64 * 1024 * 1024; 98 99 /* 100 * The largest contiguous segment that we will attempt to allocate when 101 * removing a device. This can be no larger than SPA_MAXBLOCKSIZE. If 102 * there is a performance problem with attempting to allocate large blocks, 103 * consider decreasing this. 104 * 105 * See also the accessor function spa_remove_max_segment(). 106 */ 107 uint_t zfs_remove_max_segment = SPA_MAXBLOCKSIZE; 108 109 /* 110 * Ignore hard IO errors during device removal. When set if a device 111 * encounters hard IO error during the removal process the removal will 112 * not be cancelled. This can result in a normally recoverable block 113 * becoming permanently damaged and is not recommended. 114 */ 115 static int zfs_removal_ignore_errors = 0; 116 117 /* 118 * Allow a remap segment to span free chunks of at most this size. The main 119 * impact of a larger span is that we will read and write larger, more 120 * contiguous chunks, with more "unnecessary" data -- trading off bandwidth 121 * for iops. The value here was chosen to align with 122 * zfs_vdev_read_gap_limit, which is a similar concept when doing regular 123 * reads (but there's no reason it has to be the same). 124 * 125 * Additionally, a higher span will have the following relatively minor 126 * effects: 127 * - the mapping will be smaller, since one entry can cover more allocated 128 * segments 129 * - more of the fragmentation in the removing device will be preserved 130 * - we'll do larger allocations, which may fail and fall back on smaller 131 * allocations 132 */ 133 uint_t vdev_removal_max_span = 32 * 1024; 134 135 /* 136 * This is used by the test suite so that it can ensure that certain 137 * actions happen while in the middle of a removal. 138 */ 139 int zfs_removal_suspend_progress = 0; 140 141 #define VDEV_REMOVAL_ZAP_OBJS "lzap" 142 143 static __attribute__((noreturn)) void spa_vdev_remove_thread(void *arg); 144 static int spa_vdev_remove_cancel_impl(spa_t *spa); 145 146 static void 147 spa_sync_removing_state(spa_t *spa, dmu_tx_t *tx) 148 { 149 VERIFY0(zap_update(spa->spa_dsl_pool->dp_meta_objset, 150 DMU_POOL_DIRECTORY_OBJECT, 151 DMU_POOL_REMOVING, sizeof (uint64_t), 152 sizeof (spa->spa_removing_phys) / sizeof (uint64_t), 153 &spa->spa_removing_phys, tx)); 154 } 155 156 static nvlist_t * 157 spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid) 158 { 159 for (int i = 0; i < count; i++) { 160 uint64_t guid = 161 fnvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID); 162 163 if (guid == target_guid) 164 return (nvpp[i]); 165 } 166 167 return (NULL); 168 } 169 170 static void 171 vdev_activate(vdev_t *vd) 172 { 173 metaslab_group_t *mg = vd->vdev_mg; 174 spa_t *spa = vd->vdev_spa; 175 uint64_t vdev_space = spa_deflate(spa) ? 176 vd->vdev_stat.vs_dspace : vd->vdev_stat.vs_space; 177 178 ASSERT(!vd->vdev_islog); 179 ASSERT(vd->vdev_noalloc); 180 181 metaslab_group_activate(mg); 182 metaslab_group_activate(vd->vdev_log_mg); 183 184 ASSERT3U(spa->spa_nonallocating_dspace, >=, vdev_space); 185 186 spa->spa_nonallocating_dspace -= vdev_space; 187 188 vd->vdev_noalloc = B_FALSE; 189 } 190 191 static int 192 vdev_passivate(vdev_t *vd, uint64_t *txg) 193 { 194 spa_t *spa = vd->vdev_spa; 195 int error; 196 197 ASSERT(!vd->vdev_noalloc); 198 199 vdev_t *rvd = spa->spa_root_vdev; 200 metaslab_group_t *mg = vd->vdev_mg; 201 metaslab_class_t *normal = spa_normal_class(spa); 202 if (mg->mg_class == normal) { 203 /* 204 * We must check that this is not the only allocating device in 205 * the pool before passivating, otherwise we will not be able 206 * to make progress because we can't allocate from any vdevs. 207 */ 208 boolean_t last = B_TRUE; 209 for (uint64_t id = 0; id < rvd->vdev_children; id++) { 210 vdev_t *cvd = rvd->vdev_child[id]; 211 212 if (cvd == vd || !vdev_is_concrete(cvd) || 213 vdev_is_dead(cvd)) 214 continue; 215 216 metaslab_class_t *mc = cvd->vdev_mg->mg_class; 217 if (mc != normal) 218 continue; 219 220 if (!cvd->vdev_noalloc) { 221 last = B_FALSE; 222 break; 223 } 224 } 225 if (last) 226 return (SET_ERROR(EINVAL)); 227 } 228 229 metaslab_group_passivate(mg); 230 ASSERT(!vd->vdev_islog); 231 metaslab_group_passivate(vd->vdev_log_mg); 232 233 /* 234 * Wait for the youngest allocations and frees to sync, 235 * and then wait for the deferral of those frees to finish. 236 */ 237 spa_vdev_config_exit(spa, NULL, 238 *txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG); 239 240 /* 241 * We must ensure that no "stubby" log blocks are allocated 242 * on the device to be removed. These blocks could be 243 * written at any time, including while we are in the middle 244 * of copying them. 245 */ 246 error = spa_reset_logs(spa); 247 248 *txg = spa_vdev_config_enter(spa); 249 250 if (error != 0) { 251 metaslab_group_activate(mg); 252 ASSERT(!vd->vdev_islog); 253 if (vd->vdev_log_mg != NULL) 254 metaslab_group_activate(vd->vdev_log_mg); 255 return (error); 256 } 257 258 spa->spa_nonallocating_dspace += spa_deflate(spa) ? 259 vd->vdev_stat.vs_dspace : vd->vdev_stat.vs_space; 260 vd->vdev_noalloc = B_TRUE; 261 262 return (0); 263 } 264 265 /* 266 * Turn off allocations for a top-level device from the pool. 267 * 268 * Turning off allocations for a top-level device can take a significant 269 * amount of time. As a result we use the spa_vdev_config_[enter/exit] 270 * functions which allow us to grab and release the spa_config_lock while 271 * still holding the namespace lock. During each step the configuration 272 * is synced out. 273 */ 274 int 275 spa_vdev_noalloc(spa_t *spa, uint64_t guid) 276 { 277 vdev_t *vd; 278 uint64_t txg; 279 int error = 0; 280 281 ASSERT(!MUTEX_HELD(&spa_namespace_lock)); 282 ASSERT(spa_writeable(spa)); 283 284 txg = spa_vdev_enter(spa); 285 286 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 287 288 vd = spa_lookup_by_guid(spa, guid, B_FALSE); 289 290 if (vd == NULL) 291 error = SET_ERROR(ENOENT); 292 else if (vd->vdev_mg == NULL) 293 error = SET_ERROR(ZFS_ERR_VDEV_NOTSUP); 294 else if (!vd->vdev_noalloc) 295 error = vdev_passivate(vd, &txg); 296 297 if (error == 0) { 298 vdev_dirty_leaves(vd, VDD_DTL, txg); 299 vdev_config_dirty(vd); 300 } 301 302 error = spa_vdev_exit(spa, NULL, txg, error); 303 304 return (error); 305 } 306 307 int 308 spa_vdev_alloc(spa_t *spa, uint64_t guid) 309 { 310 vdev_t *vd; 311 uint64_t txg; 312 int error = 0; 313 314 ASSERT(!MUTEX_HELD(&spa_namespace_lock)); 315 ASSERT(spa_writeable(spa)); 316 317 txg = spa_vdev_enter(spa); 318 319 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 320 321 vd = spa_lookup_by_guid(spa, guid, B_FALSE); 322 323 if (vd == NULL) 324 error = SET_ERROR(ENOENT); 325 else if (vd->vdev_mg == NULL) 326 error = SET_ERROR(ZFS_ERR_VDEV_NOTSUP); 327 else if (!vd->vdev_removing) 328 vdev_activate(vd); 329 330 if (error == 0) { 331 vdev_dirty_leaves(vd, VDD_DTL, txg); 332 vdev_config_dirty(vd); 333 } 334 335 (void) spa_vdev_exit(spa, NULL, txg, error); 336 337 return (error); 338 } 339 340 static void 341 spa_vdev_remove_aux(nvlist_t *config, const char *name, nvlist_t **dev, 342 int count, nvlist_t *dev_to_remove) 343 { 344 nvlist_t **newdev = NULL; 345 346 if (count > 1) 347 newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP); 348 349 for (int i = 0, j = 0; i < count; i++) { 350 if (dev[i] == dev_to_remove) 351 continue; 352 VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0); 353 } 354 355 VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0); 356 fnvlist_add_nvlist_array(config, name, (const nvlist_t * const *)newdev, 357 count - 1); 358 359 for (int i = 0; i < count - 1; i++) 360 nvlist_free(newdev[i]); 361 362 if (count > 1) 363 kmem_free(newdev, (count - 1) * sizeof (void *)); 364 } 365 366 static spa_vdev_removal_t * 367 spa_vdev_removal_create(vdev_t *vd) 368 { 369 spa_vdev_removal_t *svr = kmem_zalloc(sizeof (*svr), KM_SLEEP); 370 mutex_init(&svr->svr_lock, NULL, MUTEX_DEFAULT, NULL); 371 cv_init(&svr->svr_cv, NULL, CV_DEFAULT, NULL); 372 svr->svr_allocd_segs = zfs_range_tree_create(NULL, ZFS_RANGE_SEG64, 373 NULL, 0, 0); 374 svr->svr_vdev_id = vd->vdev_id; 375 376 for (int i = 0; i < TXG_SIZE; i++) { 377 svr->svr_frees[i] = zfs_range_tree_create(NULL, ZFS_RANGE_SEG64, 378 NULL, 0, 0); 379 list_create(&svr->svr_new_segments[i], 380 sizeof (vdev_indirect_mapping_entry_t), 381 offsetof(vdev_indirect_mapping_entry_t, vime_node)); 382 } 383 384 return (svr); 385 } 386 387 void 388 spa_vdev_removal_destroy(spa_vdev_removal_t *svr) 389 { 390 for (int i = 0; i < TXG_SIZE; i++) { 391 ASSERT0(svr->svr_bytes_done[i]); 392 ASSERT0(svr->svr_max_offset_to_sync[i]); 393 zfs_range_tree_destroy(svr->svr_frees[i]); 394 list_destroy(&svr->svr_new_segments[i]); 395 } 396 397 zfs_range_tree_destroy(svr->svr_allocd_segs); 398 mutex_destroy(&svr->svr_lock); 399 cv_destroy(&svr->svr_cv); 400 kmem_free(svr, sizeof (*svr)); 401 } 402 403 /* 404 * This is called as a synctask in the txg in which we will mark this vdev 405 * as removing (in the config stored in the MOS). 406 * 407 * It begins the evacuation of a toplevel vdev by: 408 * - initializing the spa_removing_phys which tracks this removal 409 * - computing the amount of space to remove for accounting purposes 410 * - dirtying all dbufs in the spa_config_object 411 * - creating the spa_vdev_removal 412 * - starting the spa_vdev_remove_thread 413 */ 414 static void 415 vdev_remove_initiate_sync(void *arg, dmu_tx_t *tx) 416 { 417 int vdev_id = (uintptr_t)arg; 418 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 419 vdev_t *vd = vdev_lookup_top(spa, vdev_id); 420 vdev_indirect_config_t *vic = &vd->vdev_indirect_config; 421 objset_t *mos = spa->spa_dsl_pool->dp_meta_objset; 422 spa_vdev_removal_t *svr = NULL; 423 uint64_t txg __maybe_unused = dmu_tx_get_txg(tx); 424 425 ASSERT0(vdev_get_nparity(vd)); 426 svr = spa_vdev_removal_create(vd); 427 428 ASSERT(vd->vdev_removing); 429 ASSERT3P(vd->vdev_indirect_mapping, ==, NULL); 430 431 spa_feature_incr(spa, SPA_FEATURE_DEVICE_REMOVAL, tx); 432 if (spa_feature_is_enabled(spa, SPA_FEATURE_OBSOLETE_COUNTS)) { 433 /* 434 * By activating the OBSOLETE_COUNTS feature, we prevent 435 * the pool from being downgraded and ensure that the 436 * refcounts are precise. 437 */ 438 spa_feature_incr(spa, SPA_FEATURE_OBSOLETE_COUNTS, tx); 439 uint64_t one = 1; 440 VERIFY0(zap_add(spa->spa_meta_objset, vd->vdev_top_zap, 441 VDEV_TOP_ZAP_OBSOLETE_COUNTS_ARE_PRECISE, sizeof (one), 1, 442 &one, tx)); 443 boolean_t are_precise __maybe_unused; 444 ASSERT0(vdev_obsolete_counts_are_precise(vd, &are_precise)); 445 ASSERT3B(are_precise, ==, B_TRUE); 446 } 447 448 vic->vic_mapping_object = vdev_indirect_mapping_alloc(mos, tx); 449 vd->vdev_indirect_mapping = 450 vdev_indirect_mapping_open(mos, vic->vic_mapping_object); 451 vic->vic_births_object = vdev_indirect_births_alloc(mos, tx); 452 vd->vdev_indirect_births = 453 vdev_indirect_births_open(mos, vic->vic_births_object); 454 spa->spa_removing_phys.sr_removing_vdev = vd->vdev_id; 455 spa->spa_removing_phys.sr_start_time = gethrestime_sec(); 456 spa->spa_removing_phys.sr_end_time = 0; 457 spa->spa_removing_phys.sr_state = DSS_SCANNING; 458 spa->spa_removing_phys.sr_to_copy = 0; 459 spa->spa_removing_phys.sr_copied = 0; 460 461 /* 462 * Note: We can't use vdev_stat's vs_alloc for sr_to_copy, because 463 * there may be space in the defer tree, which is free, but still 464 * counted in vs_alloc. 465 */ 466 for (uint64_t i = 0; i < vd->vdev_ms_count; i++) { 467 metaslab_t *ms = vd->vdev_ms[i]; 468 if (ms->ms_sm == NULL) 469 continue; 470 471 spa->spa_removing_phys.sr_to_copy += 472 metaslab_allocated_space(ms); 473 474 /* 475 * Space which we are freeing this txg does not need to 476 * be copied. 477 */ 478 spa->spa_removing_phys.sr_to_copy -= 479 zfs_range_tree_space(ms->ms_freeing); 480 481 ASSERT0(zfs_range_tree_space(ms->ms_freed)); 482 for (int t = 0; t < TXG_SIZE; t++) 483 ASSERT0(zfs_range_tree_space(ms->ms_allocating[t])); 484 } 485 486 /* 487 * Sync tasks are called before metaslab_sync(), so there should 488 * be no already-synced metaslabs in the TXG_CLEAN list. 489 */ 490 ASSERT3P(txg_list_head(&vd->vdev_ms_list, TXG_CLEAN(txg)), ==, NULL); 491 492 spa_sync_removing_state(spa, tx); 493 494 /* 495 * All blocks that we need to read the most recent mapping must be 496 * stored on concrete vdevs. Therefore, we must dirty anything that 497 * is read before spa_remove_init(). Specifically, the 498 * spa_config_object. (Note that although we already modified the 499 * spa_config_object in spa_sync_removing_state, that may not have 500 * modified all blocks of the object.) 501 */ 502 dmu_object_info_t doi; 503 VERIFY0(dmu_object_info(mos, DMU_POOL_DIRECTORY_OBJECT, &doi)); 504 for (uint64_t offset = 0; offset < doi.doi_max_offset; ) { 505 dmu_buf_t *dbuf; 506 VERIFY0(dmu_buf_hold(mos, DMU_POOL_DIRECTORY_OBJECT, 507 offset, FTAG, &dbuf, 0)); 508 dmu_buf_will_dirty(dbuf, tx); 509 offset += dbuf->db_size; 510 dmu_buf_rele(dbuf, FTAG); 511 } 512 513 /* 514 * Now that we've allocated the im_object, dirty the vdev to ensure 515 * that the object gets written to the config on disk. 516 */ 517 vdev_config_dirty(vd); 518 519 zfs_dbgmsg("starting removal thread for vdev %llu (%px) in txg %llu " 520 "im_obj=%llu", (u_longlong_t)vd->vdev_id, vd, 521 (u_longlong_t)dmu_tx_get_txg(tx), 522 (u_longlong_t)vic->vic_mapping_object); 523 524 spa_history_log_internal(spa, "vdev remove started", tx, 525 "%s vdev %llu %s", spa_name(spa), (u_longlong_t)vd->vdev_id, 526 (vd->vdev_path != NULL) ? vd->vdev_path : "-"); 527 /* 528 * Setting spa_vdev_removal causes subsequent frees to call 529 * free_from_removing_vdev(). Note that we don't need any locking 530 * because we are the sync thread, and metaslab_free_impl() is only 531 * called from syncing context (potentially from a zio taskq thread, 532 * but in any case only when there are outstanding free i/os, which 533 * there are not). 534 */ 535 ASSERT3P(spa->spa_vdev_removal, ==, NULL); 536 spa->spa_vdev_removal = svr; 537 svr->svr_thread = thread_create(NULL, 0, 538 spa_vdev_remove_thread, spa, 0, &p0, TS_RUN, minclsyspri); 539 } 540 541 /* 542 * When we are opening a pool, we must read the mapping for each 543 * indirect vdev in order from most recently removed to least 544 * recently removed. We do this because the blocks for the mapping 545 * of older indirect vdevs may be stored on more recently removed vdevs. 546 * In order to read each indirect mapping object, we must have 547 * initialized all more recently removed vdevs. 548 */ 549 int 550 spa_remove_init(spa_t *spa) 551 { 552 int error; 553 554 error = zap_lookup(spa->spa_dsl_pool->dp_meta_objset, 555 DMU_POOL_DIRECTORY_OBJECT, 556 DMU_POOL_REMOVING, sizeof (uint64_t), 557 sizeof (spa->spa_removing_phys) / sizeof (uint64_t), 558 &spa->spa_removing_phys); 559 560 if (error == ENOENT) { 561 spa->spa_removing_phys.sr_state = DSS_NONE; 562 spa->spa_removing_phys.sr_removing_vdev = -1; 563 spa->spa_removing_phys.sr_prev_indirect_vdev = -1; 564 spa->spa_indirect_vdevs_loaded = B_TRUE; 565 return (0); 566 } else if (error != 0) { 567 return (error); 568 } 569 570 if (spa->spa_removing_phys.sr_state == DSS_SCANNING) { 571 /* 572 * We are currently removing a vdev. Create and 573 * initialize a spa_vdev_removal_t from the bonus 574 * buffer of the removing vdevs vdev_im_object, and 575 * initialize its partial mapping. 576 */ 577 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 578 vdev_t *vd = vdev_lookup_top(spa, 579 spa->spa_removing_phys.sr_removing_vdev); 580 581 if (vd == NULL) { 582 spa_config_exit(spa, SCL_STATE, FTAG); 583 return (EINVAL); 584 } 585 586 vdev_indirect_config_t *vic = &vd->vdev_indirect_config; 587 588 ASSERT(vdev_is_concrete(vd)); 589 spa_vdev_removal_t *svr = spa_vdev_removal_create(vd); 590 ASSERT3U(svr->svr_vdev_id, ==, vd->vdev_id); 591 ASSERT(vd->vdev_removing); 592 593 vd->vdev_indirect_mapping = vdev_indirect_mapping_open( 594 spa->spa_meta_objset, vic->vic_mapping_object); 595 vd->vdev_indirect_births = vdev_indirect_births_open( 596 spa->spa_meta_objset, vic->vic_births_object); 597 spa_config_exit(spa, SCL_STATE, FTAG); 598 599 spa->spa_vdev_removal = svr; 600 } 601 602 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 603 uint64_t indirect_vdev_id = 604 spa->spa_removing_phys.sr_prev_indirect_vdev; 605 while (indirect_vdev_id != UINT64_MAX) { 606 vdev_t *vd = vdev_lookup_top(spa, indirect_vdev_id); 607 vdev_indirect_config_t *vic = &vd->vdev_indirect_config; 608 609 ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops); 610 vd->vdev_indirect_mapping = vdev_indirect_mapping_open( 611 spa->spa_meta_objset, vic->vic_mapping_object); 612 vd->vdev_indirect_births = vdev_indirect_births_open( 613 spa->spa_meta_objset, vic->vic_births_object); 614 615 indirect_vdev_id = vic->vic_prev_indirect_vdev; 616 } 617 spa_config_exit(spa, SCL_STATE, FTAG); 618 619 /* 620 * Now that we've loaded all the indirect mappings, we can allow 621 * reads from other blocks (e.g. via predictive prefetch). 622 */ 623 spa->spa_indirect_vdevs_loaded = B_TRUE; 624 return (0); 625 } 626 627 void 628 spa_restart_removal(spa_t *spa) 629 { 630 spa_vdev_removal_t *svr = spa->spa_vdev_removal; 631 632 if (svr == NULL) 633 return; 634 635 /* 636 * In general when this function is called there is no 637 * removal thread running. The only scenario where this 638 * is not true is during spa_import() where this function 639 * is called twice [once from spa_import_impl() and 640 * spa_async_resume()]. Thus, in the scenario where we 641 * import a pool that has an ongoing removal we don't 642 * want to spawn a second thread. 643 */ 644 if (svr->svr_thread != NULL) 645 return; 646 647 if (!spa_writeable(spa)) 648 return; 649 650 zfs_dbgmsg("restarting removal of %llu", 651 (u_longlong_t)svr->svr_vdev_id); 652 svr->svr_thread = thread_create(NULL, 0, spa_vdev_remove_thread, spa, 653 0, &p0, TS_RUN, minclsyspri); 654 } 655 656 /* 657 * Process freeing from a device which is in the middle of being removed. 658 * We must handle this carefully so that we attempt to copy freed data, 659 * and we correctly free already-copied data. 660 */ 661 void 662 free_from_removing_vdev(vdev_t *vd, uint64_t offset, uint64_t size) 663 { 664 spa_t *spa = vd->vdev_spa; 665 spa_vdev_removal_t *svr = spa->spa_vdev_removal; 666 vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping; 667 uint64_t txg = spa_syncing_txg(spa); 668 uint64_t max_offset_yet = 0; 669 670 ASSERT(vd->vdev_indirect_config.vic_mapping_object != 0); 671 ASSERT3U(vd->vdev_indirect_config.vic_mapping_object, ==, 672 vdev_indirect_mapping_object(vim)); 673 ASSERT3U(vd->vdev_id, ==, svr->svr_vdev_id); 674 675 mutex_enter(&svr->svr_lock); 676 677 /* 678 * Remove the segment from the removing vdev's spacemap. This 679 * ensures that we will not attempt to copy this space (if the 680 * removal thread has not yet visited it), and also ensures 681 * that we know what is actually allocated on the new vdevs 682 * (needed if we cancel the removal). 683 * 684 * Note: we must do the metaslab_free_concrete() with the svr_lock 685 * held, so that the remove_thread can not load this metaslab and then 686 * visit this offset between the time that we metaslab_free_concrete() 687 * and when we check to see if it has been visited. 688 * 689 * Note: The checkpoint flag is set to false as having/taking 690 * a checkpoint and removing a device can't happen at the same 691 * time. 692 */ 693 ASSERT(!spa_has_checkpoint(spa)); 694 metaslab_free_concrete(vd, offset, size, B_FALSE); 695 696 uint64_t synced_size = 0; 697 uint64_t synced_offset = 0; 698 uint64_t max_offset_synced = vdev_indirect_mapping_max_offset(vim); 699 if (offset < max_offset_synced) { 700 /* 701 * The mapping for this offset is already on disk. 702 * Free from the new location. 703 * 704 * Note that we use svr_max_synced_offset because it is 705 * updated atomically with respect to the in-core mapping. 706 * By contrast, vim_max_offset is not. 707 * 708 * This block may be split between a synced entry and an 709 * in-flight or unvisited entry. Only process the synced 710 * portion of it here. 711 */ 712 synced_size = MIN(size, max_offset_synced - offset); 713 synced_offset = offset; 714 715 ASSERT3U(max_offset_yet, <=, max_offset_synced); 716 max_offset_yet = max_offset_synced; 717 718 DTRACE_PROBE3(remove__free__synced, 719 spa_t *, spa, 720 uint64_t, offset, 721 uint64_t, synced_size); 722 723 size -= synced_size; 724 offset += synced_size; 725 } 726 727 /* 728 * Look at all in-flight txgs starting from the currently syncing one 729 * and see if a section of this free is being copied. By starting from 730 * this txg and iterating forward, we might find that this region 731 * was copied in two different txgs and handle it appropriately. 732 */ 733 for (int i = 0; i < TXG_CONCURRENT_STATES; i++) { 734 int txgoff = (txg + i) & TXG_MASK; 735 if (size > 0 && offset < svr->svr_max_offset_to_sync[txgoff]) { 736 /* 737 * The mapping for this offset is in flight, and 738 * will be synced in txg+i. 739 */ 740 uint64_t inflight_size = MIN(size, 741 svr->svr_max_offset_to_sync[txgoff] - offset); 742 743 DTRACE_PROBE4(remove__free__inflight, 744 spa_t *, spa, 745 uint64_t, offset, 746 uint64_t, inflight_size, 747 uint64_t, txg + i); 748 749 /* 750 * We copy data in order of increasing offset. 751 * Therefore the max_offset_to_sync[] must increase 752 * (or be zero, indicating that nothing is being 753 * copied in that txg). 754 */ 755 if (svr->svr_max_offset_to_sync[txgoff] != 0) { 756 ASSERT3U(svr->svr_max_offset_to_sync[txgoff], 757 >=, max_offset_yet); 758 max_offset_yet = 759 svr->svr_max_offset_to_sync[txgoff]; 760 } 761 762 /* 763 * We've already committed to copying this segment: 764 * we have allocated space elsewhere in the pool for 765 * it and have an IO outstanding to copy the data. We 766 * cannot free the space before the copy has 767 * completed, or else the copy IO might overwrite any 768 * new data. To free that space, we record the 769 * segment in the appropriate svr_frees tree and free 770 * the mapped space later, in the txg where we have 771 * completed the copy and synced the mapping (see 772 * vdev_mapping_sync). 773 */ 774 zfs_range_tree_add(svr->svr_frees[txgoff], 775 offset, inflight_size); 776 size -= inflight_size; 777 offset += inflight_size; 778 779 /* 780 * This space is already accounted for as being 781 * done, because it is being copied in txg+i. 782 * However, if i!=0, then it is being copied in 783 * a future txg. If we crash after this txg 784 * syncs but before txg+i syncs, then the space 785 * will be free. Therefore we must account 786 * for the space being done in *this* txg 787 * (when it is freed) rather than the future txg 788 * (when it will be copied). 789 */ 790 ASSERT3U(svr->svr_bytes_done[txgoff], >=, 791 inflight_size); 792 svr->svr_bytes_done[txgoff] -= inflight_size; 793 svr->svr_bytes_done[txg & TXG_MASK] += inflight_size; 794 } 795 } 796 ASSERT0(svr->svr_max_offset_to_sync[TXG_CLEAN(txg) & TXG_MASK]); 797 798 if (size > 0) { 799 /* 800 * The copy thread has not yet visited this offset. Ensure 801 * that it doesn't. 802 */ 803 804 DTRACE_PROBE3(remove__free__unvisited, 805 spa_t *, spa, 806 uint64_t, offset, 807 uint64_t, size); 808 809 if (svr->svr_allocd_segs != NULL) 810 zfs_range_tree_clear(svr->svr_allocd_segs, offset, 811 size); 812 813 /* 814 * Since we now do not need to copy this data, for 815 * accounting purposes we have done our job and can count 816 * it as completed. 817 */ 818 svr->svr_bytes_done[txg & TXG_MASK] += size; 819 } 820 mutex_exit(&svr->svr_lock); 821 822 /* 823 * Now that we have dropped svr_lock, process the synced portion 824 * of this free. 825 */ 826 if (synced_size > 0) { 827 vdev_indirect_mark_obsolete(vd, synced_offset, synced_size); 828 829 /* 830 * Note: this can only be called from syncing context, 831 * and the vdev_indirect_mapping is only changed from the 832 * sync thread, so we don't need svr_lock while doing 833 * metaslab_free_impl_cb. 834 */ 835 boolean_t checkpoint = B_FALSE; 836 vdev_indirect_ops.vdev_op_remap(vd, synced_offset, synced_size, 837 metaslab_free_impl_cb, &checkpoint); 838 } 839 } 840 841 /* 842 * Stop an active removal and update the spa_removing phys. 843 */ 844 static void 845 spa_finish_removal(spa_t *spa, dsl_scan_state_t state, dmu_tx_t *tx) 846 { 847 spa_vdev_removal_t *svr = spa->spa_vdev_removal; 848 ASSERT3U(dmu_tx_get_txg(tx), ==, spa_syncing_txg(spa)); 849 850 /* Ensure the removal thread has completed before we free the svr. */ 851 spa_vdev_remove_suspend(spa); 852 853 ASSERT(state == DSS_FINISHED || state == DSS_CANCELED); 854 855 if (state == DSS_FINISHED) { 856 spa_removing_phys_t *srp = &spa->spa_removing_phys; 857 vdev_t *vd = vdev_lookup_top(spa, svr->svr_vdev_id); 858 vdev_indirect_config_t *vic = &vd->vdev_indirect_config; 859 860 if (srp->sr_prev_indirect_vdev != -1) { 861 vdev_t *pvd; 862 pvd = vdev_lookup_top(spa, 863 srp->sr_prev_indirect_vdev); 864 ASSERT3P(pvd->vdev_ops, ==, &vdev_indirect_ops); 865 } 866 867 vic->vic_prev_indirect_vdev = srp->sr_prev_indirect_vdev; 868 srp->sr_prev_indirect_vdev = vd->vdev_id; 869 } 870 spa->spa_removing_phys.sr_state = state; 871 spa->spa_removing_phys.sr_end_time = gethrestime_sec(); 872 873 spa->spa_vdev_removal = NULL; 874 spa_vdev_removal_destroy(svr); 875 876 spa_sync_removing_state(spa, tx); 877 spa_notify_waiters(spa); 878 879 vdev_config_dirty(spa->spa_root_vdev); 880 } 881 882 static void 883 free_mapped_segment_cb(void *arg, uint64_t offset, uint64_t size) 884 { 885 vdev_t *vd = arg; 886 vdev_indirect_mark_obsolete(vd, offset, size); 887 boolean_t checkpoint = B_FALSE; 888 vdev_indirect_ops.vdev_op_remap(vd, offset, size, 889 metaslab_free_impl_cb, &checkpoint); 890 } 891 892 /* 893 * On behalf of the removal thread, syncs an incremental bit more of 894 * the indirect mapping to disk and updates the in-memory mapping. 895 * Called as a sync task in every txg that the removal thread makes progress. 896 */ 897 static void 898 vdev_mapping_sync(void *arg, dmu_tx_t *tx) 899 { 900 spa_vdev_removal_t *svr = arg; 901 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 902 vdev_t *vd = vdev_lookup_top(spa, svr->svr_vdev_id); 903 vdev_indirect_config_t *vic __maybe_unused = &vd->vdev_indirect_config; 904 uint64_t txg = dmu_tx_get_txg(tx); 905 vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping; 906 907 ASSERT(vic->vic_mapping_object != 0); 908 ASSERT3U(txg, ==, spa_syncing_txg(spa)); 909 910 vdev_indirect_mapping_add_entries(vim, 911 &svr->svr_new_segments[txg & TXG_MASK], tx); 912 vdev_indirect_births_add_entry(vd->vdev_indirect_births, 913 vdev_indirect_mapping_max_offset(vim), dmu_tx_get_txg(tx), tx); 914 915 /* 916 * Free the copied data for anything that was freed while the 917 * mapping entries were in flight. 918 */ 919 mutex_enter(&svr->svr_lock); 920 zfs_range_tree_vacate(svr->svr_frees[txg & TXG_MASK], 921 free_mapped_segment_cb, vd); 922 ASSERT3U(svr->svr_max_offset_to_sync[txg & TXG_MASK], >=, 923 vdev_indirect_mapping_max_offset(vim)); 924 svr->svr_max_offset_to_sync[txg & TXG_MASK] = 0; 925 mutex_exit(&svr->svr_lock); 926 927 spa_sync_removing_state(spa, tx); 928 } 929 930 typedef struct vdev_copy_segment_arg { 931 spa_t *vcsa_spa; 932 dva_t *vcsa_dest_dva; 933 uint64_t vcsa_txg; 934 zfs_range_tree_t *vcsa_obsolete_segs; 935 } vdev_copy_segment_arg_t; 936 937 static void 938 unalloc_seg(void *arg, uint64_t start, uint64_t size) 939 { 940 vdev_copy_segment_arg_t *vcsa = arg; 941 spa_t *spa = vcsa->vcsa_spa; 942 blkptr_t bp = { { { {0} } } }; 943 944 BP_SET_BIRTH(&bp, TXG_INITIAL, TXG_INITIAL); 945 BP_SET_LSIZE(&bp, size); 946 BP_SET_PSIZE(&bp, size); 947 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF); 948 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_OFF); 949 BP_SET_TYPE(&bp, DMU_OT_NONE); 950 BP_SET_LEVEL(&bp, 0); 951 BP_SET_DEDUP(&bp, 0); 952 BP_SET_BYTEORDER(&bp, ZFS_HOST_BYTEORDER); 953 954 DVA_SET_VDEV(&bp.blk_dva[0], DVA_GET_VDEV(vcsa->vcsa_dest_dva)); 955 DVA_SET_OFFSET(&bp.blk_dva[0], 956 DVA_GET_OFFSET(vcsa->vcsa_dest_dva) + start); 957 DVA_SET_ASIZE(&bp.blk_dva[0], size); 958 959 zio_free(spa, vcsa->vcsa_txg, &bp); 960 } 961 962 /* 963 * All reads and writes associated with a call to spa_vdev_copy_segment() 964 * are done. 965 */ 966 static void 967 spa_vdev_copy_segment_done(zio_t *zio) 968 { 969 vdev_copy_segment_arg_t *vcsa = zio->io_private; 970 971 zfs_range_tree_vacate(vcsa->vcsa_obsolete_segs, 972 unalloc_seg, vcsa); 973 zfs_range_tree_destroy(vcsa->vcsa_obsolete_segs); 974 kmem_free(vcsa, sizeof (*vcsa)); 975 976 spa_config_exit(zio->io_spa, SCL_STATE, zio->io_spa); 977 } 978 979 /* 980 * The write of the new location is done. 981 */ 982 static void 983 spa_vdev_copy_segment_write_done(zio_t *zio) 984 { 985 vdev_copy_arg_t *vca = zio->io_private; 986 987 abd_free(zio->io_abd); 988 989 mutex_enter(&vca->vca_lock); 990 vca->vca_outstanding_bytes -= zio->io_size; 991 992 if (zio->io_error != 0) 993 vca->vca_write_error_bytes += zio->io_size; 994 995 cv_signal(&vca->vca_cv); 996 mutex_exit(&vca->vca_lock); 997 } 998 999 /* 1000 * The read of the old location is done. The parent zio is the write to 1001 * the new location. Allow it to start. 1002 */ 1003 static void 1004 spa_vdev_copy_segment_read_done(zio_t *zio) 1005 { 1006 vdev_copy_arg_t *vca = zio->io_private; 1007 1008 if (zio->io_error != 0) { 1009 mutex_enter(&vca->vca_lock); 1010 vca->vca_read_error_bytes += zio->io_size; 1011 mutex_exit(&vca->vca_lock); 1012 } 1013 1014 zio_nowait(zio_unique_parent(zio)); 1015 } 1016 1017 /* 1018 * If the old and new vdevs are mirrors, we will read both sides of the old 1019 * mirror, and write each copy to the corresponding side of the new mirror. 1020 * If the old and new vdevs have a different number of children, we will do 1021 * this as best as possible. Since we aren't verifying checksums, this 1022 * ensures that as long as there's a good copy of the data, we'll have a 1023 * good copy after the removal, even if there's silent damage to one side 1024 * of the mirror. If we're removing a mirror that has some silent damage, 1025 * we'll have exactly the same damage in the new location (assuming that 1026 * the new location is also a mirror). 1027 * 1028 * We accomplish this by creating a tree of zio_t's, with as many writes as 1029 * there are "children" of the new vdev (a non-redundant vdev counts as one 1030 * child, a 2-way mirror has 2 children, etc). Each write has an associated 1031 * read from a child of the old vdev. Typically there will be the same 1032 * number of children of the old and new vdevs. However, if there are more 1033 * children of the new vdev, some child(ren) of the old vdev will be issued 1034 * multiple reads. If there are more children of the old vdev, some copies 1035 * will be dropped. 1036 * 1037 * For example, the tree of zio_t's for a 2-way mirror is: 1038 * 1039 * null 1040 * / \ 1041 * write(new vdev, child 0) write(new vdev, child 1) 1042 * | | 1043 * read(old vdev, child 0) read(old vdev, child 1) 1044 * 1045 * Child zio's complete before their parents complete. However, zio's 1046 * created with zio_vdev_child_io() may be issued before their children 1047 * complete. In this case we need to make sure that the children (reads) 1048 * complete before the parents (writes) are *issued*. We do this by not 1049 * calling zio_nowait() on each write until its corresponding read has 1050 * completed. 1051 * 1052 * The spa_config_lock must be held while zio's created by 1053 * zio_vdev_child_io() are in progress, to ensure that the vdev tree does 1054 * not change (e.g. due to a concurrent "zpool attach/detach"). The "null" 1055 * zio is needed to release the spa_config_lock after all the reads and 1056 * writes complete. (Note that we can't grab the config lock for each read, 1057 * because it is not reentrant - we could deadlock with a thread waiting 1058 * for a write lock.) 1059 */ 1060 static void 1061 spa_vdev_copy_one_child(vdev_copy_arg_t *vca, zio_t *nzio, 1062 vdev_t *source_vd, uint64_t source_offset, 1063 vdev_t *dest_child_vd, uint64_t dest_offset, int dest_id, uint64_t size) 1064 { 1065 ASSERT3U(spa_config_held(nzio->io_spa, SCL_ALL, RW_READER), !=, 0); 1066 1067 /* 1068 * If the destination child in unwritable then there is no point 1069 * in issuing the source reads which cannot be written. 1070 */ 1071 if (!vdev_writeable(dest_child_vd)) 1072 return; 1073 1074 mutex_enter(&vca->vca_lock); 1075 vca->vca_outstanding_bytes += size; 1076 mutex_exit(&vca->vca_lock); 1077 1078 abd_t *abd = abd_alloc_for_io(size, B_FALSE); 1079 1080 vdev_t *source_child_vd = NULL; 1081 if (source_vd->vdev_ops == &vdev_mirror_ops && dest_id != -1) { 1082 /* 1083 * Source and dest are both mirrors. Copy from the same 1084 * child id as we are copying to (wrapping around if there 1085 * are more dest children than source children). If the 1086 * preferred source child is unreadable select another. 1087 */ 1088 for (int i = 0; i < source_vd->vdev_children; i++) { 1089 source_child_vd = source_vd->vdev_child[ 1090 (dest_id + i) % source_vd->vdev_children]; 1091 if (vdev_readable(source_child_vd)) 1092 break; 1093 } 1094 } else { 1095 source_child_vd = source_vd; 1096 } 1097 1098 /* 1099 * There should always be at least one readable source child or 1100 * the pool would be in a suspended state. Somehow selecting an 1101 * unreadable child would result in IO errors, the removal process 1102 * being cancelled, and the pool reverting to its pre-removal state. 1103 */ 1104 ASSERT3P(source_child_vd, !=, NULL); 1105 1106 zio_t *write_zio = zio_vdev_child_io(nzio, NULL, 1107 dest_child_vd, dest_offset, abd, size, 1108 ZIO_TYPE_WRITE, ZIO_PRIORITY_REMOVAL, 1109 ZIO_FLAG_CANFAIL, 1110 spa_vdev_copy_segment_write_done, vca); 1111 1112 zio_nowait(zio_vdev_child_io(write_zio, NULL, 1113 source_child_vd, source_offset, abd, size, 1114 ZIO_TYPE_READ, ZIO_PRIORITY_REMOVAL, 1115 ZIO_FLAG_CANFAIL, 1116 spa_vdev_copy_segment_read_done, vca)); 1117 } 1118 1119 /* 1120 * Allocate a new location for this segment, and create the zio_t's to 1121 * read from the old location and write to the new location. 1122 */ 1123 static int 1124 spa_vdev_copy_segment(vdev_t *vd, zfs_range_tree_t *segs, 1125 uint64_t maxalloc, uint64_t txg, 1126 vdev_copy_arg_t *vca, zio_alloc_list_t *zal) 1127 { 1128 metaslab_group_t *mg = vd->vdev_mg; 1129 spa_t *spa = vd->vdev_spa; 1130 spa_vdev_removal_t *svr = spa->spa_vdev_removal; 1131 vdev_indirect_mapping_entry_t *entry; 1132 dva_t dst = {{ 0 }}; 1133 uint64_t start = zfs_range_tree_min(segs); 1134 ASSERT0(P2PHASE(start, 1 << spa->spa_min_ashift)); 1135 1136 ASSERT3U(maxalloc, <=, SPA_MAXBLOCKSIZE); 1137 ASSERT0(P2PHASE(maxalloc, 1 << spa->spa_min_ashift)); 1138 1139 uint64_t size = zfs_range_tree_span(segs); 1140 if (zfs_range_tree_span(segs) > maxalloc) { 1141 /* 1142 * We can't allocate all the segments. Prefer to end 1143 * the allocation at the end of a segment, thus avoiding 1144 * additional split blocks. 1145 */ 1146 zfs_range_seg_max_t search; 1147 zfs_btree_index_t where; 1148 zfs_rs_set_start(&search, segs, start + maxalloc); 1149 zfs_rs_set_end(&search, segs, start + maxalloc); 1150 (void) zfs_btree_find(&segs->rt_root, &search, &where); 1151 zfs_range_seg_t *rs = zfs_btree_prev(&segs->rt_root, &where, 1152 &where); 1153 if (rs != NULL) { 1154 size = zfs_rs_get_end(rs, segs) - start; 1155 } else { 1156 /* 1157 * There are no segments that end before maxalloc. 1158 * I.e. the first segment is larger than maxalloc, 1159 * so we must split it. 1160 */ 1161 size = maxalloc; 1162 } 1163 } 1164 ASSERT3U(size, <=, maxalloc); 1165 ASSERT0(P2PHASE(size, 1 << spa->spa_min_ashift)); 1166 1167 /* 1168 * An allocation class might not have any remaining vdevs or space 1169 */ 1170 metaslab_class_t *mc = mg->mg_class; 1171 if (mc->mc_groups == 0) 1172 mc = spa_normal_class(spa); 1173 int error = metaslab_alloc_dva(spa, mc, size, &dst, 0, NULL, txg, 1174 METASLAB_DONT_THROTTLE, zal, 0); 1175 if (error == ENOSPC && mc != spa_normal_class(spa)) { 1176 error = metaslab_alloc_dva(spa, spa_normal_class(spa), size, 1177 &dst, 0, NULL, txg, METASLAB_DONT_THROTTLE, zal, 0); 1178 } 1179 if (error != 0) 1180 return (error); 1181 1182 /* 1183 * Determine the ranges that are not actually needed. Offsets are 1184 * relative to the start of the range to be copied (i.e. relative to the 1185 * local variable "start"). 1186 */ 1187 zfs_range_tree_t *obsolete_segs = zfs_range_tree_create(NULL, 1188 ZFS_RANGE_SEG64, NULL, 0, 0); 1189 1190 zfs_btree_index_t where; 1191 zfs_range_seg_t *rs = zfs_btree_first(&segs->rt_root, &where); 1192 ASSERT3U(zfs_rs_get_start(rs, segs), ==, start); 1193 uint64_t prev_seg_end = zfs_rs_get_end(rs, segs); 1194 while ((rs = zfs_btree_next(&segs->rt_root, &where, &where)) != NULL) { 1195 if (zfs_rs_get_start(rs, segs) >= start + size) { 1196 break; 1197 } else { 1198 zfs_range_tree_add(obsolete_segs, 1199 prev_seg_end - start, 1200 zfs_rs_get_start(rs, segs) - prev_seg_end); 1201 } 1202 prev_seg_end = zfs_rs_get_end(rs, segs); 1203 } 1204 /* We don't end in the middle of an obsolete range */ 1205 ASSERT3U(start + size, <=, prev_seg_end); 1206 1207 zfs_range_tree_clear(segs, start, size); 1208 1209 /* 1210 * We can't have any padding of the allocated size, otherwise we will 1211 * misunderstand what's allocated, and the size of the mapping. We 1212 * prevent padding by ensuring that all devices in the pool have the 1213 * same ashift, and the allocation size is a multiple of the ashift. 1214 */ 1215 VERIFY3U(DVA_GET_ASIZE(&dst), ==, size); 1216 1217 entry = kmem_zalloc(sizeof (vdev_indirect_mapping_entry_t), KM_SLEEP); 1218 DVA_MAPPING_SET_SRC_OFFSET(&entry->vime_mapping, start); 1219 entry->vime_mapping.vimep_dst = dst; 1220 if (spa_feature_is_enabled(spa, SPA_FEATURE_OBSOLETE_COUNTS)) { 1221 entry->vime_obsolete_count = 1222 zfs_range_tree_space(obsolete_segs); 1223 } 1224 1225 vdev_copy_segment_arg_t *vcsa = kmem_zalloc(sizeof (*vcsa), KM_SLEEP); 1226 vcsa->vcsa_dest_dva = &entry->vime_mapping.vimep_dst; 1227 vcsa->vcsa_obsolete_segs = obsolete_segs; 1228 vcsa->vcsa_spa = spa; 1229 vcsa->vcsa_txg = txg; 1230 1231 /* 1232 * See comment before spa_vdev_copy_one_child(). 1233 */ 1234 spa_config_enter(spa, SCL_STATE, spa, RW_READER); 1235 zio_t *nzio = zio_null(spa->spa_txg_zio[txg & TXG_MASK], spa, NULL, 1236 spa_vdev_copy_segment_done, vcsa, 0); 1237 vdev_t *dest_vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dst)); 1238 if (dest_vd->vdev_ops == &vdev_mirror_ops) { 1239 for (int i = 0; i < dest_vd->vdev_children; i++) { 1240 vdev_t *child = dest_vd->vdev_child[i]; 1241 spa_vdev_copy_one_child(vca, nzio, vd, start, 1242 child, DVA_GET_OFFSET(&dst), i, size); 1243 } 1244 } else { 1245 spa_vdev_copy_one_child(vca, nzio, vd, start, 1246 dest_vd, DVA_GET_OFFSET(&dst), -1, size); 1247 } 1248 zio_nowait(nzio); 1249 1250 list_insert_tail(&svr->svr_new_segments[txg & TXG_MASK], entry); 1251 ASSERT3U(start + size, <=, vd->vdev_ms_count << vd->vdev_ms_shift); 1252 vdev_dirty(vd, 0, NULL, txg); 1253 1254 return (0); 1255 } 1256 1257 /* 1258 * Complete the removal of a toplevel vdev. This is called as a 1259 * synctask in the same txg that we will sync out the new config (to the 1260 * MOS object) which indicates that this vdev is indirect. 1261 */ 1262 static void 1263 vdev_remove_complete_sync(void *arg, dmu_tx_t *tx) 1264 { 1265 spa_vdev_removal_t *svr = arg; 1266 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 1267 vdev_t *vd = vdev_lookup_top(spa, svr->svr_vdev_id); 1268 1269 ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops); 1270 1271 for (int i = 0; i < TXG_SIZE; i++) { 1272 ASSERT0(svr->svr_bytes_done[i]); 1273 } 1274 1275 ASSERT3U(spa->spa_removing_phys.sr_copied, ==, 1276 spa->spa_removing_phys.sr_to_copy); 1277 1278 vdev_destroy_spacemaps(vd, tx); 1279 1280 /* destroy leaf zaps, if any */ 1281 ASSERT3P(svr->svr_zaplist, !=, NULL); 1282 for (nvpair_t *pair = nvlist_next_nvpair(svr->svr_zaplist, NULL); 1283 pair != NULL; 1284 pair = nvlist_next_nvpair(svr->svr_zaplist, pair)) { 1285 vdev_destroy_unlink_zap(vd, fnvpair_value_uint64(pair), tx); 1286 } 1287 fnvlist_free(svr->svr_zaplist); 1288 1289 spa_finish_removal(dmu_tx_pool(tx)->dp_spa, DSS_FINISHED, tx); 1290 /* vd->vdev_path is not available here */ 1291 spa_history_log_internal(spa, "vdev remove completed", tx, 1292 "%s vdev %llu", spa_name(spa), (u_longlong_t)vd->vdev_id); 1293 } 1294 1295 static void 1296 vdev_remove_enlist_zaps(vdev_t *vd, nvlist_t *zlist) 1297 { 1298 ASSERT3P(zlist, !=, NULL); 1299 ASSERT0(vdev_get_nparity(vd)); 1300 1301 if (vd->vdev_leaf_zap != 0) { 1302 char zkey[32]; 1303 (void) snprintf(zkey, sizeof (zkey), "%s-%llu", 1304 VDEV_REMOVAL_ZAP_OBJS, (u_longlong_t)vd->vdev_leaf_zap); 1305 fnvlist_add_uint64(zlist, zkey, vd->vdev_leaf_zap); 1306 } 1307 1308 for (uint64_t id = 0; id < vd->vdev_children; id++) { 1309 vdev_remove_enlist_zaps(vd->vdev_child[id], zlist); 1310 } 1311 } 1312 1313 static void 1314 vdev_remove_replace_with_indirect(vdev_t *vd, uint64_t txg) 1315 { 1316 vdev_t *ivd; 1317 dmu_tx_t *tx; 1318 spa_t *spa = vd->vdev_spa; 1319 spa_vdev_removal_t *svr = spa->spa_vdev_removal; 1320 1321 /* 1322 * First, build a list of leaf zaps to be destroyed. 1323 * This is passed to the sync context thread, 1324 * which does the actual unlinking. 1325 */ 1326 svr->svr_zaplist = fnvlist_alloc(); 1327 vdev_remove_enlist_zaps(vd, svr->svr_zaplist); 1328 1329 ivd = vdev_add_parent(vd, &vdev_indirect_ops); 1330 ivd->vdev_removing = 0; 1331 1332 vd->vdev_leaf_zap = 0; 1333 1334 vdev_remove_child(ivd, vd); 1335 vdev_compact_children(ivd); 1336 1337 ASSERT(!list_link_active(&vd->vdev_state_dirty_node)); 1338 1339 mutex_enter(&svr->svr_lock); 1340 svr->svr_thread = NULL; 1341 cv_broadcast(&svr->svr_cv); 1342 mutex_exit(&svr->svr_lock); 1343 1344 /* After this, we can not use svr. */ 1345 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 1346 dsl_sync_task_nowait(spa->spa_dsl_pool, 1347 vdev_remove_complete_sync, svr, tx); 1348 dmu_tx_commit(tx); 1349 } 1350 1351 /* 1352 * Complete the removal of a toplevel vdev. This is called in open 1353 * context by the removal thread after we have copied all vdev's data. 1354 */ 1355 static void 1356 vdev_remove_complete(spa_t *spa) 1357 { 1358 uint64_t txg; 1359 1360 /* 1361 * Wait for any deferred frees to be synced before we call 1362 * vdev_metaslab_fini() 1363 */ 1364 txg_wait_synced(spa->spa_dsl_pool, 0); 1365 txg = spa_vdev_enter(spa); 1366 vdev_t *vd = vdev_lookup_top(spa, spa->spa_vdev_removal->svr_vdev_id); 1367 ASSERT3P(vd->vdev_initialize_thread, ==, NULL); 1368 ASSERT3P(vd->vdev_trim_thread, ==, NULL); 1369 ASSERT3P(vd->vdev_autotrim_thread, ==, NULL); 1370 vdev_rebuild_stop_wait(vd); 1371 ASSERT3P(vd->vdev_rebuild_thread, ==, NULL); 1372 uint64_t vdev_space = spa_deflate(spa) ? 1373 vd->vdev_stat.vs_dspace : vd->vdev_stat.vs_space; 1374 1375 sysevent_t *ev = spa_event_create(spa, vd, NULL, 1376 ESC_ZFS_VDEV_REMOVE_DEV); 1377 1378 zfs_dbgmsg("finishing device removal for vdev %llu in txg %llu", 1379 (u_longlong_t)vd->vdev_id, (u_longlong_t)txg); 1380 1381 ASSERT3U(0, !=, vdev_space); 1382 ASSERT3U(spa->spa_nonallocating_dspace, >=, vdev_space); 1383 1384 /* the vdev is no longer part of the dspace */ 1385 spa->spa_nonallocating_dspace -= vdev_space; 1386 1387 /* 1388 * Discard allocation state. 1389 */ 1390 if (vd->vdev_mg != NULL) { 1391 vdev_metaslab_fini(vd); 1392 metaslab_group_destroy(vd->vdev_mg); 1393 vd->vdev_mg = NULL; 1394 } 1395 if (vd->vdev_log_mg != NULL) { 1396 ASSERT0(vd->vdev_ms_count); 1397 metaslab_group_destroy(vd->vdev_log_mg); 1398 vd->vdev_log_mg = NULL; 1399 } 1400 ASSERT0(vd->vdev_stat.vs_space); 1401 ASSERT0(vd->vdev_stat.vs_dspace); 1402 1403 vdev_remove_replace_with_indirect(vd, txg); 1404 1405 /* 1406 * We now release the locks, allowing spa_sync to run and finish the 1407 * removal via vdev_remove_complete_sync in syncing context. 1408 * 1409 * Note that we hold on to the vdev_t that has been replaced. Since 1410 * it isn't part of the vdev tree any longer, it can't be concurrently 1411 * manipulated, even while we don't have the config lock. 1412 */ 1413 (void) spa_vdev_exit(spa, NULL, txg, 0); 1414 1415 /* 1416 * Top ZAP should have been transferred to the indirect vdev in 1417 * vdev_remove_replace_with_indirect. 1418 */ 1419 ASSERT0(vd->vdev_top_zap); 1420 1421 /* 1422 * Leaf ZAP should have been moved in vdev_remove_replace_with_indirect. 1423 */ 1424 ASSERT0(vd->vdev_leaf_zap); 1425 1426 txg = spa_vdev_enter(spa); 1427 (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 1428 /* 1429 * Request to update the config and the config cachefile. 1430 */ 1431 vdev_config_dirty(spa->spa_root_vdev); 1432 (void) spa_vdev_exit(spa, vd, txg, 0); 1433 1434 if (ev != NULL) 1435 spa_event_post(ev); 1436 } 1437 1438 /* 1439 * Evacuates a segment of size at most max_alloc from the vdev 1440 * via repeated calls to spa_vdev_copy_segment. If an allocation 1441 * fails, the pool is probably too fragmented to handle such a 1442 * large size, so decrease max_alloc so that the caller will not try 1443 * this size again this txg. 1444 */ 1445 static void 1446 spa_vdev_copy_impl(vdev_t *vd, spa_vdev_removal_t *svr, vdev_copy_arg_t *vca, 1447 uint64_t *max_alloc, dmu_tx_t *tx) 1448 { 1449 uint64_t txg = dmu_tx_get_txg(tx); 1450 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 1451 1452 mutex_enter(&svr->svr_lock); 1453 1454 /* 1455 * Determine how big of a chunk to copy. We can allocate up 1456 * to max_alloc bytes, and we can span up to vdev_removal_max_span 1457 * bytes of unallocated space at a time. "segs" will track the 1458 * allocated segments that we are copying. We may also be copying 1459 * free segments (of up to vdev_removal_max_span bytes). 1460 */ 1461 zfs_range_tree_t *segs = zfs_range_tree_create(NULL, ZFS_RANGE_SEG64, 1462 NULL, 0, 0); 1463 for (;;) { 1464 zfs_range_tree_t *rt = svr->svr_allocd_segs; 1465 zfs_range_seg_t *rs = zfs_range_tree_first(rt); 1466 1467 if (rs == NULL) 1468 break; 1469 1470 uint64_t seg_length; 1471 1472 if (zfs_range_tree_is_empty(segs)) { 1473 /* need to truncate the first seg based on max_alloc */ 1474 seg_length = MIN(zfs_rs_get_end(rs, rt) - 1475 zfs_rs_get_start(rs, rt), *max_alloc); 1476 } else { 1477 if (zfs_rs_get_start(rs, rt) - zfs_range_tree_max(segs) 1478 > vdev_removal_max_span) { 1479 /* 1480 * Including this segment would cause us to 1481 * copy a larger unneeded chunk than is allowed. 1482 */ 1483 break; 1484 } else if (zfs_rs_get_end(rs, rt) - 1485 zfs_range_tree_min(segs) > *max_alloc) { 1486 /* 1487 * This additional segment would extend past 1488 * max_alloc. Rather than splitting this 1489 * segment, leave it for the next mapping. 1490 */ 1491 break; 1492 } else { 1493 seg_length = zfs_rs_get_end(rs, rt) - 1494 zfs_rs_get_start(rs, rt); 1495 } 1496 } 1497 1498 zfs_range_tree_add(segs, zfs_rs_get_start(rs, rt), seg_length); 1499 zfs_range_tree_remove(svr->svr_allocd_segs, 1500 zfs_rs_get_start(rs, rt), seg_length); 1501 } 1502 1503 if (zfs_range_tree_is_empty(segs)) { 1504 mutex_exit(&svr->svr_lock); 1505 zfs_range_tree_destroy(segs); 1506 return; 1507 } 1508 1509 if (svr->svr_max_offset_to_sync[txg & TXG_MASK] == 0) { 1510 dsl_sync_task_nowait(dmu_tx_pool(tx), vdev_mapping_sync, 1511 svr, tx); 1512 } 1513 1514 svr->svr_max_offset_to_sync[txg & TXG_MASK] = zfs_range_tree_max(segs); 1515 1516 /* 1517 * Note: this is the amount of *allocated* space 1518 * that we are taking care of each txg. 1519 */ 1520 svr->svr_bytes_done[txg & TXG_MASK] += zfs_range_tree_space(segs); 1521 1522 mutex_exit(&svr->svr_lock); 1523 1524 zio_alloc_list_t zal; 1525 metaslab_trace_init(&zal); 1526 uint64_t thismax = SPA_MAXBLOCKSIZE; 1527 while (!zfs_range_tree_is_empty(segs)) { 1528 int error = spa_vdev_copy_segment(vd, 1529 segs, thismax, txg, vca, &zal); 1530 1531 if (error == ENOSPC) { 1532 /* 1533 * Cut our segment in half, and don't try this 1534 * segment size again this txg. Note that the 1535 * allocation size must be aligned to the highest 1536 * ashift in the pool, so that the allocation will 1537 * not be padded out to a multiple of the ashift, 1538 * which could cause us to think that this mapping 1539 * is larger than we intended. 1540 */ 1541 ASSERT3U(spa->spa_max_ashift, >=, SPA_MINBLOCKSHIFT); 1542 ASSERT3U(spa->spa_max_ashift, ==, spa->spa_min_ashift); 1543 uint64_t attempted = 1544 MIN(zfs_range_tree_span(segs), thismax); 1545 thismax = P2ROUNDUP(attempted / 2, 1546 1 << spa->spa_max_ashift); 1547 /* 1548 * The minimum-size allocation can not fail. 1549 */ 1550 ASSERT3U(attempted, >, 1 << spa->spa_max_ashift); 1551 *max_alloc = attempted - (1 << spa->spa_max_ashift); 1552 } else { 1553 ASSERT0(error); 1554 1555 /* 1556 * We've performed an allocation, so reset the 1557 * alloc trace list. 1558 */ 1559 metaslab_trace_fini(&zal); 1560 metaslab_trace_init(&zal); 1561 } 1562 } 1563 metaslab_trace_fini(&zal); 1564 zfs_range_tree_destroy(segs); 1565 } 1566 1567 /* 1568 * The size of each removal mapping is limited by the tunable 1569 * zfs_remove_max_segment, but we must adjust this to be a multiple of the 1570 * pool's ashift, so that we don't try to split individual sectors regardless 1571 * of the tunable value. (Note that device removal requires that all devices 1572 * have the same ashift, so there's no difference between spa_min_ashift and 1573 * spa_max_ashift.) The raw tunable should not be used elsewhere. 1574 */ 1575 uint64_t 1576 spa_remove_max_segment(spa_t *spa) 1577 { 1578 return (P2ROUNDUP(zfs_remove_max_segment, 1 << spa->spa_max_ashift)); 1579 } 1580 1581 /* 1582 * The removal thread operates in open context. It iterates over all 1583 * allocated space in the vdev, by loading each metaslab's spacemap. 1584 * For each contiguous segment of allocated space (capping the segment 1585 * size at SPA_MAXBLOCKSIZE), we: 1586 * - Allocate space for it on another vdev. 1587 * - Create a new mapping from the old location to the new location 1588 * (as a record in svr_new_segments). 1589 * - Initiate a physical read zio to get the data off the removing disk. 1590 * - In the read zio's done callback, initiate a physical write zio to 1591 * write it to the new vdev. 1592 * Note that all of this will take effect when a particular TXG syncs. 1593 * The sync thread ensures that all the phys reads and writes for the syncing 1594 * TXG have completed (see spa_txg_zio) and writes the new mappings to disk 1595 * (see vdev_mapping_sync()). 1596 */ 1597 static __attribute__((noreturn)) void 1598 spa_vdev_remove_thread(void *arg) 1599 { 1600 spa_t *spa = arg; 1601 spa_vdev_removal_t *svr = spa->spa_vdev_removal; 1602 vdev_copy_arg_t vca; 1603 uint64_t max_alloc = spa_remove_max_segment(spa); 1604 uint64_t last_txg = 0; 1605 1606 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 1607 vdev_t *vd = vdev_lookup_top(spa, svr->svr_vdev_id); 1608 vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping; 1609 uint64_t start_offset = vdev_indirect_mapping_max_offset(vim); 1610 1611 ASSERT3P(vd->vdev_ops, !=, &vdev_indirect_ops); 1612 ASSERT(vdev_is_concrete(vd)); 1613 ASSERT(vd->vdev_removing); 1614 ASSERT(vd->vdev_indirect_config.vic_mapping_object != 0); 1615 ASSERT(vim != NULL); 1616 1617 mutex_init(&vca.vca_lock, NULL, MUTEX_DEFAULT, NULL); 1618 cv_init(&vca.vca_cv, NULL, CV_DEFAULT, NULL); 1619 vca.vca_outstanding_bytes = 0; 1620 vca.vca_read_error_bytes = 0; 1621 vca.vca_write_error_bytes = 0; 1622 1623 mutex_enter(&svr->svr_lock); 1624 1625 /* 1626 * Start from vim_max_offset so we pick up where we left off 1627 * if we are restarting the removal after opening the pool. 1628 */ 1629 uint64_t msi; 1630 for (msi = start_offset >> vd->vdev_ms_shift; 1631 msi < vd->vdev_ms_count && !svr->svr_thread_exit; msi++) { 1632 metaslab_t *msp = vd->vdev_ms[msi]; 1633 ASSERT3U(msi, <=, vd->vdev_ms_count); 1634 1635 ASSERT0(zfs_range_tree_space(svr->svr_allocd_segs)); 1636 1637 mutex_enter(&msp->ms_sync_lock); 1638 mutex_enter(&msp->ms_lock); 1639 1640 /* 1641 * Assert nothing in flight -- ms_*tree is empty. 1642 */ 1643 for (int i = 0; i < TXG_SIZE; i++) { 1644 ASSERT0(zfs_range_tree_space(msp->ms_allocating[i])); 1645 } 1646 1647 /* 1648 * If the metaslab has ever been allocated from (ms_sm!=NULL), 1649 * read the allocated segments from the space map object 1650 * into svr_allocd_segs. Since we do this while holding 1651 * svr_lock and ms_sync_lock, concurrent frees (which 1652 * would have modified the space map) will wait for us 1653 * to finish loading the spacemap, and then take the 1654 * appropriate action (see free_from_removing_vdev()). 1655 */ 1656 if (msp->ms_sm != NULL) { 1657 VERIFY0(space_map_load(msp->ms_sm, 1658 svr->svr_allocd_segs, SM_ALLOC)); 1659 1660 zfs_range_tree_walk(msp->ms_unflushed_allocs, 1661 zfs_range_tree_add, svr->svr_allocd_segs); 1662 zfs_range_tree_walk(msp->ms_unflushed_frees, 1663 zfs_range_tree_remove, svr->svr_allocd_segs); 1664 zfs_range_tree_walk(msp->ms_freeing, 1665 zfs_range_tree_remove, svr->svr_allocd_segs); 1666 1667 /* 1668 * When we are resuming from a paused removal (i.e. 1669 * when importing a pool with a removal in progress), 1670 * discard any state that we have already processed. 1671 */ 1672 zfs_range_tree_clear(svr->svr_allocd_segs, 0, 1673 start_offset); 1674 } 1675 mutex_exit(&msp->ms_lock); 1676 mutex_exit(&msp->ms_sync_lock); 1677 1678 vca.vca_msp = msp; 1679 zfs_dbgmsg("copying %llu segments for metaslab %llu", 1680 (u_longlong_t)zfs_btree_numnodes( 1681 &svr->svr_allocd_segs->rt_root), 1682 (u_longlong_t)msp->ms_id); 1683 1684 while (!svr->svr_thread_exit && 1685 !zfs_range_tree_is_empty(svr->svr_allocd_segs)) { 1686 1687 mutex_exit(&svr->svr_lock); 1688 1689 /* 1690 * We need to periodically drop the config lock so that 1691 * writers can get in. Additionally, we can't wait 1692 * for a txg to sync while holding a config lock 1693 * (since a waiting writer could cause a 3-way deadlock 1694 * with the sync thread, which also gets a config 1695 * lock for reader). So we can't hold the config lock 1696 * while calling dmu_tx_assign(). 1697 */ 1698 spa_config_exit(spa, SCL_CONFIG, FTAG); 1699 1700 /* 1701 * This delay will pause the removal around the point 1702 * specified by zfs_removal_suspend_progress. We do this 1703 * solely from the test suite or during debugging. 1704 */ 1705 while (zfs_removal_suspend_progress && 1706 !svr->svr_thread_exit) 1707 delay(hz); 1708 1709 mutex_enter(&vca.vca_lock); 1710 while (vca.vca_outstanding_bytes > 1711 zfs_remove_max_copy_bytes) { 1712 cv_wait(&vca.vca_cv, &vca.vca_lock); 1713 } 1714 mutex_exit(&vca.vca_lock); 1715 1716 dmu_tx_t *tx = 1717 dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); 1718 1719 VERIFY0(dmu_tx_assign(tx, TXG_WAIT)); 1720 uint64_t txg = dmu_tx_get_txg(tx); 1721 1722 /* 1723 * Reacquire the vdev_config lock. The vdev_t 1724 * that we're removing may have changed, e.g. due 1725 * to a vdev_attach or vdev_detach. 1726 */ 1727 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 1728 vd = vdev_lookup_top(spa, svr->svr_vdev_id); 1729 1730 if (txg != last_txg) 1731 max_alloc = spa_remove_max_segment(spa); 1732 last_txg = txg; 1733 1734 spa_vdev_copy_impl(vd, svr, &vca, &max_alloc, tx); 1735 1736 dmu_tx_commit(tx); 1737 mutex_enter(&svr->svr_lock); 1738 } 1739 1740 mutex_enter(&vca.vca_lock); 1741 if (zfs_removal_ignore_errors == 0 && 1742 (vca.vca_read_error_bytes > 0 || 1743 vca.vca_write_error_bytes > 0)) { 1744 svr->svr_thread_exit = B_TRUE; 1745 } 1746 mutex_exit(&vca.vca_lock); 1747 } 1748 1749 mutex_exit(&svr->svr_lock); 1750 1751 spa_config_exit(spa, SCL_CONFIG, FTAG); 1752 1753 /* 1754 * Wait for all copies to finish before cleaning up the vca. 1755 */ 1756 txg_wait_synced(spa->spa_dsl_pool, 0); 1757 ASSERT0(vca.vca_outstanding_bytes); 1758 1759 mutex_destroy(&vca.vca_lock); 1760 cv_destroy(&vca.vca_cv); 1761 1762 if (svr->svr_thread_exit) { 1763 mutex_enter(&svr->svr_lock); 1764 zfs_range_tree_vacate(svr->svr_allocd_segs, NULL, NULL); 1765 svr->svr_thread = NULL; 1766 cv_broadcast(&svr->svr_cv); 1767 mutex_exit(&svr->svr_lock); 1768 1769 /* 1770 * During the removal process an unrecoverable read or write 1771 * error was encountered. The removal process must be 1772 * cancelled or this damage may become permanent. 1773 */ 1774 if (zfs_removal_ignore_errors == 0 && 1775 (vca.vca_read_error_bytes > 0 || 1776 vca.vca_write_error_bytes > 0)) { 1777 zfs_dbgmsg("canceling removal due to IO errors: " 1778 "[read_error_bytes=%llu] [write_error_bytes=%llu]", 1779 (u_longlong_t)vca.vca_read_error_bytes, 1780 (u_longlong_t)vca.vca_write_error_bytes); 1781 spa_vdev_remove_cancel_impl(spa); 1782 } 1783 } else { 1784 ASSERT0(zfs_range_tree_space(svr->svr_allocd_segs)); 1785 vdev_remove_complete(spa); 1786 } 1787 1788 thread_exit(); 1789 } 1790 1791 void 1792 spa_vdev_remove_suspend(spa_t *spa) 1793 { 1794 spa_vdev_removal_t *svr = spa->spa_vdev_removal; 1795 1796 if (svr == NULL) 1797 return; 1798 1799 mutex_enter(&svr->svr_lock); 1800 svr->svr_thread_exit = B_TRUE; 1801 while (svr->svr_thread != NULL) 1802 cv_wait(&svr->svr_cv, &svr->svr_lock); 1803 svr->svr_thread_exit = B_FALSE; 1804 mutex_exit(&svr->svr_lock); 1805 } 1806 1807 /* 1808 * Return true if the "allocating" property has been set to "off" 1809 */ 1810 static boolean_t 1811 vdev_prop_allocating_off(vdev_t *vd) 1812 { 1813 uint64_t objid = vd->vdev_top_zap; 1814 uint64_t allocating = 1; 1815 1816 /* no vdev property object => no props */ 1817 if (objid != 0) { 1818 spa_t *spa = vd->vdev_spa; 1819 objset_t *mos = spa->spa_meta_objset; 1820 1821 mutex_enter(&spa->spa_props_lock); 1822 (void) zap_lookup(mos, objid, "allocating", sizeof (uint64_t), 1823 1, &allocating); 1824 mutex_exit(&spa->spa_props_lock); 1825 } 1826 return (allocating == 0); 1827 } 1828 1829 static int 1830 spa_vdev_remove_cancel_check(void *arg, dmu_tx_t *tx) 1831 { 1832 (void) arg; 1833 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 1834 1835 if (spa->spa_vdev_removal == NULL) 1836 return (ENOTACTIVE); 1837 return (0); 1838 } 1839 1840 /* 1841 * Cancel a removal by freeing all entries from the partial mapping 1842 * and marking the vdev as no longer being removing. 1843 */ 1844 static void 1845 spa_vdev_remove_cancel_sync(void *arg, dmu_tx_t *tx) 1846 { 1847 (void) arg; 1848 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 1849 spa_vdev_removal_t *svr = spa->spa_vdev_removal; 1850 vdev_t *vd = vdev_lookup_top(spa, svr->svr_vdev_id); 1851 vdev_indirect_config_t *vic = &vd->vdev_indirect_config; 1852 vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping; 1853 objset_t *mos = spa->spa_meta_objset; 1854 1855 ASSERT3P(svr->svr_thread, ==, NULL); 1856 1857 spa_feature_decr(spa, SPA_FEATURE_DEVICE_REMOVAL, tx); 1858 1859 boolean_t are_precise; 1860 VERIFY0(vdev_obsolete_counts_are_precise(vd, &are_precise)); 1861 if (are_precise) { 1862 spa_feature_decr(spa, SPA_FEATURE_OBSOLETE_COUNTS, tx); 1863 VERIFY0(zap_remove(spa->spa_meta_objset, vd->vdev_top_zap, 1864 VDEV_TOP_ZAP_OBSOLETE_COUNTS_ARE_PRECISE, tx)); 1865 } 1866 1867 uint64_t obsolete_sm_object; 1868 VERIFY0(vdev_obsolete_sm_object(vd, &obsolete_sm_object)); 1869 if (obsolete_sm_object != 0) { 1870 ASSERT(vd->vdev_obsolete_sm != NULL); 1871 ASSERT3U(obsolete_sm_object, ==, 1872 space_map_object(vd->vdev_obsolete_sm)); 1873 1874 space_map_free(vd->vdev_obsolete_sm, tx); 1875 VERIFY0(zap_remove(spa->spa_meta_objset, vd->vdev_top_zap, 1876 VDEV_TOP_ZAP_INDIRECT_OBSOLETE_SM, tx)); 1877 space_map_close(vd->vdev_obsolete_sm); 1878 vd->vdev_obsolete_sm = NULL; 1879 spa_feature_decr(spa, SPA_FEATURE_OBSOLETE_COUNTS, tx); 1880 } 1881 for (int i = 0; i < TXG_SIZE; i++) { 1882 ASSERT(list_is_empty(&svr->svr_new_segments[i])); 1883 ASSERT3U(svr->svr_max_offset_to_sync[i], <=, 1884 vdev_indirect_mapping_max_offset(vim)); 1885 } 1886 1887 for (uint64_t msi = 0; msi < vd->vdev_ms_count; msi++) { 1888 metaslab_t *msp = vd->vdev_ms[msi]; 1889 1890 if (msp->ms_start >= vdev_indirect_mapping_max_offset(vim)) 1891 break; 1892 1893 ASSERT0(zfs_range_tree_space(svr->svr_allocd_segs)); 1894 1895 mutex_enter(&msp->ms_lock); 1896 1897 /* 1898 * Assert nothing in flight -- ms_*tree is empty. 1899 */ 1900 for (int i = 0; i < TXG_SIZE; i++) 1901 ASSERT0(zfs_range_tree_space(msp->ms_allocating[i])); 1902 for (int i = 0; i < TXG_DEFER_SIZE; i++) 1903 ASSERT0(zfs_range_tree_space(msp->ms_defer[i])); 1904 ASSERT0(zfs_range_tree_space(msp->ms_freed)); 1905 1906 if (msp->ms_sm != NULL) { 1907 mutex_enter(&svr->svr_lock); 1908 VERIFY0(space_map_load(msp->ms_sm, 1909 svr->svr_allocd_segs, SM_ALLOC)); 1910 1911 zfs_range_tree_walk(msp->ms_unflushed_allocs, 1912 zfs_range_tree_add, svr->svr_allocd_segs); 1913 zfs_range_tree_walk(msp->ms_unflushed_frees, 1914 zfs_range_tree_remove, svr->svr_allocd_segs); 1915 zfs_range_tree_walk(msp->ms_freeing, 1916 zfs_range_tree_remove, svr->svr_allocd_segs); 1917 1918 /* 1919 * Clear everything past what has been synced, 1920 * because we have not allocated mappings for it yet. 1921 */ 1922 uint64_t syncd = vdev_indirect_mapping_max_offset(vim); 1923 uint64_t sm_end = msp->ms_sm->sm_start + 1924 msp->ms_sm->sm_size; 1925 if (sm_end > syncd) 1926 zfs_range_tree_clear(svr->svr_allocd_segs, 1927 syncd, sm_end - syncd); 1928 1929 mutex_exit(&svr->svr_lock); 1930 } 1931 mutex_exit(&msp->ms_lock); 1932 1933 mutex_enter(&svr->svr_lock); 1934 zfs_range_tree_vacate(svr->svr_allocd_segs, 1935 free_mapped_segment_cb, vd); 1936 mutex_exit(&svr->svr_lock); 1937 } 1938 1939 /* 1940 * Note: this must happen after we invoke free_mapped_segment_cb, 1941 * because it adds to the obsolete_segments. 1942 */ 1943 zfs_range_tree_vacate(vd->vdev_obsolete_segments, NULL, NULL); 1944 1945 ASSERT3U(vic->vic_mapping_object, ==, 1946 vdev_indirect_mapping_object(vd->vdev_indirect_mapping)); 1947 vdev_indirect_mapping_close(vd->vdev_indirect_mapping); 1948 vd->vdev_indirect_mapping = NULL; 1949 vdev_indirect_mapping_free(mos, vic->vic_mapping_object, tx); 1950 vic->vic_mapping_object = 0; 1951 1952 ASSERT3U(vic->vic_births_object, ==, 1953 vdev_indirect_births_object(vd->vdev_indirect_births)); 1954 vdev_indirect_births_close(vd->vdev_indirect_births); 1955 vd->vdev_indirect_births = NULL; 1956 vdev_indirect_births_free(mos, vic->vic_births_object, tx); 1957 vic->vic_births_object = 0; 1958 1959 /* 1960 * We may have processed some frees from the removing vdev in this 1961 * txg, thus increasing svr_bytes_done; discard that here to 1962 * satisfy the assertions in spa_vdev_removal_destroy(). 1963 * Note that future txg's can not have any bytes_done, because 1964 * future TXG's are only modified from open context, and we have 1965 * already shut down the copying thread. 1966 */ 1967 svr->svr_bytes_done[dmu_tx_get_txg(tx) & TXG_MASK] = 0; 1968 spa_finish_removal(spa, DSS_CANCELED, tx); 1969 1970 vd->vdev_removing = B_FALSE; 1971 1972 if (!vdev_prop_allocating_off(vd)) { 1973 spa_config_enter(spa, SCL_ALLOC | SCL_VDEV, FTAG, RW_WRITER); 1974 vdev_activate(vd); 1975 spa_config_exit(spa, SCL_ALLOC | SCL_VDEV, FTAG); 1976 } 1977 1978 vdev_config_dirty(vd); 1979 1980 zfs_dbgmsg("canceled device removal for vdev %llu in %llu", 1981 (u_longlong_t)vd->vdev_id, (u_longlong_t)dmu_tx_get_txg(tx)); 1982 spa_history_log_internal(spa, "vdev remove canceled", tx, 1983 "%s vdev %llu %s", spa_name(spa), 1984 (u_longlong_t)vd->vdev_id, 1985 (vd->vdev_path != NULL) ? vd->vdev_path : "-"); 1986 } 1987 1988 static int 1989 spa_vdev_remove_cancel_impl(spa_t *spa) 1990 { 1991 int error = dsl_sync_task(spa->spa_name, spa_vdev_remove_cancel_check, 1992 spa_vdev_remove_cancel_sync, NULL, 0, 1993 ZFS_SPACE_CHECK_EXTRA_RESERVED); 1994 return (error); 1995 } 1996 1997 int 1998 spa_vdev_remove_cancel(spa_t *spa) 1999 { 2000 spa_vdev_remove_suspend(spa); 2001 2002 if (spa->spa_vdev_removal == NULL) 2003 return (ENOTACTIVE); 2004 2005 return (spa_vdev_remove_cancel_impl(spa)); 2006 } 2007 2008 void 2009 svr_sync(spa_t *spa, dmu_tx_t *tx) 2010 { 2011 spa_vdev_removal_t *svr = spa->spa_vdev_removal; 2012 int txgoff = dmu_tx_get_txg(tx) & TXG_MASK; 2013 2014 if (svr == NULL) 2015 return; 2016 2017 /* 2018 * This check is necessary so that we do not dirty the 2019 * DIRECTORY_OBJECT via spa_sync_removing_state() when there 2020 * is nothing to do. Dirtying it every time would prevent us 2021 * from syncing-to-convergence. 2022 */ 2023 if (svr->svr_bytes_done[txgoff] == 0) 2024 return; 2025 2026 /* 2027 * Update progress accounting. 2028 */ 2029 spa->spa_removing_phys.sr_copied += svr->svr_bytes_done[txgoff]; 2030 svr->svr_bytes_done[txgoff] = 0; 2031 2032 spa_sync_removing_state(spa, tx); 2033 } 2034 2035 static void 2036 vdev_remove_make_hole_and_free(vdev_t *vd) 2037 { 2038 uint64_t id = vd->vdev_id; 2039 spa_t *spa = vd->vdev_spa; 2040 vdev_t *rvd = spa->spa_root_vdev; 2041 2042 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 2043 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 2044 2045 vdev_free(vd); 2046 2047 vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops); 2048 vdev_add_child(rvd, vd); 2049 vdev_config_dirty(rvd); 2050 2051 /* 2052 * Reassess the health of our root vdev. 2053 */ 2054 vdev_reopen(rvd); 2055 } 2056 2057 /* 2058 * Remove a log device. The config lock is held for the specified TXG. 2059 */ 2060 static int 2061 spa_vdev_remove_log(vdev_t *vd, uint64_t *txg) 2062 { 2063 metaslab_group_t *mg = vd->vdev_mg; 2064 spa_t *spa = vd->vdev_spa; 2065 int error = 0; 2066 2067 ASSERT(vd->vdev_islog); 2068 ASSERT(vd == vd->vdev_top); 2069 ASSERT3P(vd->vdev_log_mg, ==, NULL); 2070 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 2071 2072 /* 2073 * Stop allocating from this vdev. 2074 */ 2075 metaslab_group_passivate(mg); 2076 2077 /* 2078 * Wait for the youngest allocations and frees to sync, 2079 * and then wait for the deferral of those frees to finish. 2080 */ 2081 spa_vdev_config_exit(spa, NULL, 2082 *txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG); 2083 2084 /* 2085 * Cancel any initialize or TRIM which was in progress. 2086 */ 2087 vdev_initialize_stop_all(vd, VDEV_INITIALIZE_CANCELED); 2088 vdev_trim_stop_all(vd, VDEV_TRIM_CANCELED); 2089 vdev_autotrim_stop_wait(vd); 2090 2091 /* 2092 * Evacuate the device. We don't hold the config lock as 2093 * writer since we need to do I/O but we do keep the 2094 * spa_namespace_lock held. Once this completes the device 2095 * should no longer have any blocks allocated on it. 2096 */ 2097 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 2098 if (vd->vdev_stat.vs_alloc != 0) 2099 error = spa_reset_logs(spa); 2100 2101 *txg = spa_vdev_config_enter(spa); 2102 2103 if (error != 0) { 2104 metaslab_group_activate(mg); 2105 ASSERT3P(vd->vdev_log_mg, ==, NULL); 2106 return (error); 2107 } 2108 ASSERT0(vd->vdev_stat.vs_alloc); 2109 2110 /* 2111 * The evacuation succeeded. Remove any remaining MOS metadata 2112 * associated with this vdev, and wait for these changes to sync. 2113 */ 2114 vd->vdev_removing = B_TRUE; 2115 2116 vdev_dirty_leaves(vd, VDD_DTL, *txg); 2117 vdev_config_dirty(vd); 2118 2119 /* 2120 * When the log space map feature is enabled we look at 2121 * the vdev's top_zap to find the on-disk flush data of 2122 * the metaslab we just flushed. Thus, while removing a 2123 * log vdev we make sure to call vdev_metaslab_fini() 2124 * first, which removes all metaslabs of this vdev from 2125 * spa_metaslabs_by_flushed before vdev_remove_empty() 2126 * destroys the top_zap of this log vdev. 2127 * 2128 * This avoids the scenario where we flush a metaslab 2129 * from the log vdev being removed that doesn't have a 2130 * top_zap and end up failing to lookup its on-disk flush 2131 * data. 2132 * 2133 * We don't call metaslab_group_destroy() right away 2134 * though (it will be called in vdev_free() later) as 2135 * during metaslab_sync() of metaslabs from other vdevs 2136 * we may touch the metaslab group of this vdev through 2137 * metaslab_class_histogram_verify() 2138 */ 2139 vdev_metaslab_fini(vd); 2140 2141 spa_vdev_config_exit(spa, NULL, *txg, 0, FTAG); 2142 *txg = spa_vdev_config_enter(spa); 2143 2144 sysevent_t *ev = spa_event_create(spa, vd, NULL, 2145 ESC_ZFS_VDEV_REMOVE_DEV); 2146 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 2147 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); 2148 2149 /* The top ZAP should have been destroyed by vdev_remove_empty. */ 2150 ASSERT0(vd->vdev_top_zap); 2151 /* The leaf ZAP should have been destroyed by vdev_dtl_sync. */ 2152 ASSERT0(vd->vdev_leaf_zap); 2153 2154 (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); 2155 2156 if (list_link_active(&vd->vdev_state_dirty_node)) 2157 vdev_state_clean(vd); 2158 if (list_link_active(&vd->vdev_config_dirty_node)) 2159 vdev_config_clean(vd); 2160 2161 ASSERT0(vd->vdev_stat.vs_alloc); 2162 2163 /* 2164 * Clean up the vdev namespace. 2165 */ 2166 vdev_remove_make_hole_and_free(vd); 2167 2168 if (ev != NULL) 2169 spa_event_post(ev); 2170 2171 return (0); 2172 } 2173 2174 static int 2175 spa_vdev_remove_top_check(vdev_t *vd) 2176 { 2177 spa_t *spa = vd->vdev_spa; 2178 2179 if (vd != vd->vdev_top) 2180 return (SET_ERROR(ENOTSUP)); 2181 2182 if (!vdev_is_concrete(vd)) 2183 return (SET_ERROR(ENOTSUP)); 2184 2185 if (!spa_feature_is_enabled(spa, SPA_FEATURE_DEVICE_REMOVAL)) 2186 return (SET_ERROR(ENOTSUP)); 2187 2188 /* 2189 * This device is already being removed 2190 */ 2191 if (vd->vdev_removing) 2192 return (SET_ERROR(EALREADY)); 2193 2194 metaslab_class_t *mc = vd->vdev_mg->mg_class; 2195 metaslab_class_t *normal = spa_normal_class(spa); 2196 if (mc != normal) { 2197 /* 2198 * Space allocated from the special (or dedup) class is 2199 * included in the DMU's space usage, but it's not included 2200 * in spa_dspace (or dsl_pool_adjustedsize()). Therefore 2201 * there is always at least as much free space in the normal 2202 * class, as is allocated from the special (and dedup) class. 2203 * As a backup check, we will return ENOSPC if this is 2204 * violated. See also spa_update_dspace(). 2205 */ 2206 uint64_t available = metaslab_class_get_space(normal) - 2207 metaslab_class_get_alloc(normal); 2208 ASSERT3U(available, >=, vd->vdev_stat.vs_alloc); 2209 if (available < vd->vdev_stat.vs_alloc) 2210 return (SET_ERROR(ENOSPC)); 2211 } else if (!vd->vdev_noalloc) { 2212 /* available space in the pool's normal class */ 2213 uint64_t available = dsl_dir_space_available( 2214 spa->spa_dsl_pool->dp_root_dir, NULL, 0, B_TRUE); 2215 if (available < vd->vdev_stat.vs_dspace) 2216 return (SET_ERROR(ENOSPC)); 2217 } 2218 2219 /* 2220 * There can not be a removal in progress. 2221 */ 2222 if (spa->spa_removing_phys.sr_state == DSS_SCANNING) 2223 return (SET_ERROR(EBUSY)); 2224 2225 /* 2226 * The device must have all its data. 2227 */ 2228 if (!vdev_dtl_empty(vd, DTL_MISSING) || 2229 !vdev_dtl_empty(vd, DTL_OUTAGE)) 2230 return (SET_ERROR(EBUSY)); 2231 2232 /* 2233 * The device must be healthy. 2234 */ 2235 if (!vdev_readable(vd)) 2236 return (SET_ERROR(EIO)); 2237 2238 /* 2239 * All vdevs in normal class must have the same ashift. 2240 */ 2241 if (spa->spa_max_ashift != spa->spa_min_ashift) { 2242 return (SET_ERROR(EINVAL)); 2243 } 2244 2245 /* 2246 * A removed special/dedup vdev must have same ashift as normal class. 2247 */ 2248 ASSERT(!vd->vdev_islog); 2249 if (vd->vdev_alloc_bias != VDEV_BIAS_NONE && 2250 vd->vdev_ashift != spa->spa_max_ashift) { 2251 return (SET_ERROR(EINVAL)); 2252 } 2253 2254 /* 2255 * All vdevs in normal class must have the same ashift 2256 * and not be raidz or draid. 2257 */ 2258 vdev_t *rvd = spa->spa_root_vdev; 2259 for (uint64_t id = 0; id < rvd->vdev_children; id++) { 2260 vdev_t *cvd = rvd->vdev_child[id]; 2261 2262 /* 2263 * A removed special/dedup vdev must have the same ashift 2264 * across all vdevs in its class. 2265 */ 2266 if (vd->vdev_alloc_bias != VDEV_BIAS_NONE && 2267 cvd->vdev_alloc_bias == vd->vdev_alloc_bias && 2268 cvd->vdev_ashift != vd->vdev_ashift) { 2269 return (SET_ERROR(EINVAL)); 2270 } 2271 if (cvd->vdev_ashift != 0 && 2272 cvd->vdev_alloc_bias == VDEV_BIAS_NONE) 2273 ASSERT3U(cvd->vdev_ashift, ==, spa->spa_max_ashift); 2274 if (!vdev_is_concrete(cvd)) 2275 continue; 2276 if (vdev_get_nparity(cvd) != 0) 2277 return (SET_ERROR(EINVAL)); 2278 /* 2279 * Need the mirror to be mirror of leaf vdevs only 2280 */ 2281 if (cvd->vdev_ops == &vdev_mirror_ops) { 2282 for (uint64_t cid = 0; 2283 cid < cvd->vdev_children; cid++) { 2284 if (!cvd->vdev_child[cid]->vdev_ops-> 2285 vdev_op_leaf) 2286 return (SET_ERROR(EINVAL)); 2287 } 2288 } 2289 } 2290 2291 return (0); 2292 } 2293 2294 /* 2295 * Initiate removal of a top-level vdev, reducing the total space in the pool. 2296 * The config lock is held for the specified TXG. Once initiated, 2297 * evacuation of all allocated space (copying it to other vdevs) happens 2298 * in the background (see spa_vdev_remove_thread()), and can be canceled 2299 * (see spa_vdev_remove_cancel()). If successful, the vdev will 2300 * be transformed to an indirect vdev (see spa_vdev_remove_complete()). 2301 */ 2302 static int 2303 spa_vdev_remove_top(vdev_t *vd, uint64_t *txg) 2304 { 2305 spa_t *spa = vd->vdev_spa; 2306 boolean_t set_noalloc = B_FALSE; 2307 int error; 2308 2309 /* 2310 * Check for errors up-front, so that we don't waste time 2311 * passivating the metaslab group and clearing the ZIL if there 2312 * are errors. 2313 */ 2314 error = spa_vdev_remove_top_check(vd); 2315 2316 /* 2317 * Stop allocating from this vdev. Note that we must check 2318 * that this is not the only device in the pool before 2319 * passivating, otherwise we will not be able to make 2320 * progress because we can't allocate from any vdevs. 2321 * The above check for sufficient free space serves this 2322 * purpose. 2323 */ 2324 if (error == 0 && !vd->vdev_noalloc) { 2325 set_noalloc = B_TRUE; 2326 error = vdev_passivate(vd, txg); 2327 } 2328 2329 if (error != 0) 2330 return (error); 2331 2332 /* 2333 * We stop any initializing and TRIM that is currently in progress 2334 * but leave the state as "active". This will allow the process to 2335 * resume if the removal is canceled sometime later. 2336 */ 2337 2338 spa_vdev_config_exit(spa, NULL, *txg, 0, FTAG); 2339 2340 vdev_initialize_stop_all(vd, VDEV_INITIALIZE_ACTIVE); 2341 vdev_trim_stop_all(vd, VDEV_TRIM_ACTIVE); 2342 vdev_autotrim_stop_wait(vd); 2343 2344 *txg = spa_vdev_config_enter(spa); 2345 2346 /* 2347 * Things might have changed while the config lock was dropped 2348 * (e.g. space usage). Check for errors again. 2349 */ 2350 error = spa_vdev_remove_top_check(vd); 2351 2352 if (error != 0) { 2353 if (set_noalloc) 2354 vdev_activate(vd); 2355 spa_async_request(spa, SPA_ASYNC_INITIALIZE_RESTART); 2356 spa_async_request(spa, SPA_ASYNC_TRIM_RESTART); 2357 spa_async_request(spa, SPA_ASYNC_AUTOTRIM_RESTART); 2358 return (error); 2359 } 2360 2361 vd->vdev_removing = B_TRUE; 2362 2363 vdev_dirty_leaves(vd, VDD_DTL, *txg); 2364 vdev_config_dirty(vd); 2365 dmu_tx_t *tx = dmu_tx_create_assigned(spa->spa_dsl_pool, *txg); 2366 dsl_sync_task_nowait(spa->spa_dsl_pool, 2367 vdev_remove_initiate_sync, (void *)(uintptr_t)vd->vdev_id, tx); 2368 dmu_tx_commit(tx); 2369 2370 return (0); 2371 } 2372 2373 /* 2374 * Remove a device from the pool. 2375 * 2376 * Removing a device from the vdev namespace requires several steps 2377 * and can take a significant amount of time. As a result we use 2378 * the spa_vdev_config_[enter/exit] functions which allow us to 2379 * grab and release the spa_config_lock while still holding the namespace 2380 * lock. During each step the configuration is synced out. 2381 */ 2382 int 2383 spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) 2384 { 2385 vdev_t *vd; 2386 nvlist_t **spares, **l2cache, *nv; 2387 uint64_t txg = 0; 2388 uint_t nspares, nl2cache; 2389 int error = 0, error_log; 2390 boolean_t locked = MUTEX_HELD(&spa_namespace_lock); 2391 sysevent_t *ev = NULL; 2392 const char *vd_type = NULL; 2393 char *vd_path = NULL; 2394 2395 ASSERT(spa_writeable(spa)); 2396 2397 if (!locked) 2398 txg = spa_vdev_enter(spa); 2399 2400 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 2401 if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { 2402 error = (spa_has_checkpoint(spa)) ? 2403 ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; 2404 2405 if (!locked) 2406 return (spa_vdev_exit(spa, NULL, txg, error)); 2407 2408 return (error); 2409 } 2410 2411 vd = spa_lookup_by_guid(spa, guid, B_FALSE); 2412 2413 if (spa->spa_spares.sav_vdevs != NULL && 2414 nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, 2415 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 && 2416 (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) { 2417 /* 2418 * Only remove the hot spare if it's not currently in use 2419 * in this pool. 2420 */ 2421 if (vd == NULL || unspare) { 2422 const char *type; 2423 boolean_t draid_spare = B_FALSE; 2424 2425 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) 2426 == 0 && strcmp(type, VDEV_TYPE_DRAID_SPARE) == 0) 2427 draid_spare = B_TRUE; 2428 2429 if (vd == NULL && draid_spare) { 2430 error = SET_ERROR(ENOTSUP); 2431 } else { 2432 if (vd == NULL) 2433 vd = spa_lookup_by_guid(spa, 2434 guid, B_TRUE); 2435 ev = spa_event_create(spa, vd, NULL, 2436 ESC_ZFS_VDEV_REMOVE_AUX); 2437 2438 vd_type = VDEV_TYPE_SPARE; 2439 vd_path = spa_strdup(fnvlist_lookup_string( 2440 nv, ZPOOL_CONFIG_PATH)); 2441 spa_vdev_remove_aux(spa->spa_spares.sav_config, 2442 ZPOOL_CONFIG_SPARES, spares, nspares, nv); 2443 spa_load_spares(spa); 2444 spa->spa_spares.sav_sync = B_TRUE; 2445 } 2446 } else { 2447 error = SET_ERROR(EBUSY); 2448 } 2449 } else if (spa->spa_l2cache.sav_vdevs != NULL && 2450 nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, 2451 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 && 2452 (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) { 2453 vd_type = VDEV_TYPE_L2CACHE; 2454 vd_path = spa_strdup(fnvlist_lookup_string( 2455 nv, ZPOOL_CONFIG_PATH)); 2456 /* 2457 * Cache devices can always be removed. 2458 */ 2459 vd = spa_lookup_by_guid(spa, guid, B_TRUE); 2460 2461 /* 2462 * Stop trimming the cache device. We need to release the 2463 * config lock to allow the syncing of TRIM transactions 2464 * without releasing the spa_namespace_lock. The same 2465 * strategy is employed in spa_vdev_remove_top(). 2466 */ 2467 spa_vdev_config_exit(spa, NULL, 2468 txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG); 2469 mutex_enter(&vd->vdev_trim_lock); 2470 vdev_trim_stop(vd, VDEV_TRIM_CANCELED, NULL); 2471 mutex_exit(&vd->vdev_trim_lock); 2472 txg = spa_vdev_config_enter(spa); 2473 2474 ev = spa_event_create(spa, vd, NULL, ESC_ZFS_VDEV_REMOVE_AUX); 2475 spa_vdev_remove_aux(spa->spa_l2cache.sav_config, 2476 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv); 2477 spa_load_l2cache(spa); 2478 spa->spa_l2cache.sav_sync = B_TRUE; 2479 } else if (vd != NULL && vd->vdev_islog) { 2480 ASSERT(!locked); 2481 vd_type = VDEV_TYPE_LOG; 2482 vd_path = spa_strdup((vd->vdev_path != NULL) ? 2483 vd->vdev_path : "-"); 2484 error = spa_vdev_remove_log(vd, &txg); 2485 } else if (vd != NULL) { 2486 ASSERT(!locked); 2487 error = spa_vdev_remove_top(vd, &txg); 2488 } else { 2489 /* 2490 * There is no vdev of any kind with the specified guid. 2491 */ 2492 error = SET_ERROR(ENOENT); 2493 } 2494 2495 error_log = error; 2496 2497 if (!locked) 2498 error = spa_vdev_exit(spa, NULL, txg, error); 2499 2500 /* 2501 * Logging must be done outside the spa config lock. Otherwise, 2502 * this code path could end up holding the spa config lock while 2503 * waiting for a txg_sync so it can write to the internal log. 2504 * Doing that would prevent the txg sync from actually happening, 2505 * causing a deadlock. 2506 */ 2507 if (error_log == 0 && vd_type != NULL && vd_path != NULL) { 2508 spa_history_log_internal(spa, "vdev remove", NULL, 2509 "%s vdev (%s) %s", spa_name(spa), vd_type, vd_path); 2510 } 2511 if (vd_path != NULL) 2512 spa_strfree(vd_path); 2513 2514 if (ev != NULL) 2515 spa_event_post(ev); 2516 2517 return (error); 2518 } 2519 2520 int 2521 spa_removal_get_stats(spa_t *spa, pool_removal_stat_t *prs) 2522 { 2523 prs->prs_state = spa->spa_removing_phys.sr_state; 2524 2525 if (prs->prs_state == DSS_NONE) 2526 return (SET_ERROR(ENOENT)); 2527 2528 prs->prs_removing_vdev = spa->spa_removing_phys.sr_removing_vdev; 2529 prs->prs_start_time = spa->spa_removing_phys.sr_start_time; 2530 prs->prs_end_time = spa->spa_removing_phys.sr_end_time; 2531 prs->prs_to_copy = spa->spa_removing_phys.sr_to_copy; 2532 prs->prs_copied = spa->spa_removing_phys.sr_copied; 2533 2534 prs->prs_mapping_memory = 0; 2535 uint64_t indirect_vdev_id = 2536 spa->spa_removing_phys.sr_prev_indirect_vdev; 2537 while (indirect_vdev_id != -1) { 2538 vdev_t *vd = spa->spa_root_vdev->vdev_child[indirect_vdev_id]; 2539 vdev_indirect_config_t *vic = &vd->vdev_indirect_config; 2540 vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping; 2541 2542 ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops); 2543 prs->prs_mapping_memory += vdev_indirect_mapping_size(vim); 2544 indirect_vdev_id = vic->vic_prev_indirect_vdev; 2545 } 2546 2547 return (0); 2548 } 2549 2550 ZFS_MODULE_PARAM(zfs_vdev, zfs_, removal_ignore_errors, INT, ZMOD_RW, 2551 "Ignore hard IO errors when removing device"); 2552 2553 ZFS_MODULE_PARAM(zfs_vdev, zfs_, remove_max_segment, UINT, ZMOD_RW, 2554 "Largest contiguous segment to allocate when removing device"); 2555 2556 ZFS_MODULE_PARAM(zfs_vdev, vdev_, removal_max_span, UINT, ZMOD_RW, 2557 "Largest span of free chunks a remap segment can span"); 2558 2559 ZFS_MODULE_PARAM(zfs_vdev, zfs_, removal_suspend_progress, UINT, ZMOD_RW, 2560 "Pause device removal after this many bytes are copied " 2561 "(debug use only - causes removal to hang)"); 2562 2563 EXPORT_SYMBOL(free_from_removing_vdev); 2564 EXPORT_SYMBOL(spa_removal_get_stats); 2565 EXPORT_SYMBOL(spa_remove_init); 2566 EXPORT_SYMBOL(spa_restart_removal); 2567 EXPORT_SYMBOL(spa_vdev_removal_destroy); 2568 EXPORT_SYMBOL(spa_vdev_remove); 2569 EXPORT_SYMBOL(spa_vdev_remove_cancel); 2570 EXPORT_SYMBOL(spa_vdev_remove_suspend); 2571 EXPORT_SYMBOL(svr_sync); 2572