xref: /illumos-gate/usr/src/uts/common/fs/zfs/vdev_removal.c (revision cfd63e1b1bcf7ba4bf72f55ddbd87ce008d2986d)
15cabbc6bSPrashanth Sreenivasa /*
25cabbc6bSPrashanth Sreenivasa  * CDDL HEADER START
35cabbc6bSPrashanth Sreenivasa  *
45cabbc6bSPrashanth Sreenivasa  * The contents of this file are subject to the terms of the
55cabbc6bSPrashanth Sreenivasa  * Common Development and Distribution License (the "License").
65cabbc6bSPrashanth Sreenivasa  * You may not use this file except in compliance with the License.
75cabbc6bSPrashanth Sreenivasa  *
85cabbc6bSPrashanth Sreenivasa  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95cabbc6bSPrashanth Sreenivasa  * or http://www.opensolaris.org/os/licensing.
105cabbc6bSPrashanth Sreenivasa  * See the License for the specific language governing permissions
115cabbc6bSPrashanth Sreenivasa  * and limitations under the License.
125cabbc6bSPrashanth Sreenivasa  *
135cabbc6bSPrashanth Sreenivasa  * When distributing Covered Code, include this CDDL HEADER in each
145cabbc6bSPrashanth Sreenivasa  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155cabbc6bSPrashanth Sreenivasa  * If applicable, add the following below this CDDL HEADER, with the
165cabbc6bSPrashanth Sreenivasa  * fields enclosed by brackets "[]" replaced with your own identifying
175cabbc6bSPrashanth Sreenivasa  * information: Portions Copyright [yyyy] [name of copyright owner]
185cabbc6bSPrashanth Sreenivasa  *
195cabbc6bSPrashanth Sreenivasa  * CDDL HEADER END
205cabbc6bSPrashanth Sreenivasa  */
215cabbc6bSPrashanth Sreenivasa 
225cabbc6bSPrashanth Sreenivasa /*
235cabbc6bSPrashanth Sreenivasa  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
245cabbc6bSPrashanth Sreenivasa  * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
255cabbc6bSPrashanth Sreenivasa  */
265cabbc6bSPrashanth Sreenivasa 
275cabbc6bSPrashanth Sreenivasa #include <sys/zfs_context.h>
285cabbc6bSPrashanth Sreenivasa #include <sys/spa_impl.h>
295cabbc6bSPrashanth Sreenivasa #include <sys/dmu.h>
305cabbc6bSPrashanth Sreenivasa #include <sys/dmu_tx.h>
315cabbc6bSPrashanth Sreenivasa #include <sys/zap.h>
325cabbc6bSPrashanth Sreenivasa #include <sys/vdev_impl.h>
335cabbc6bSPrashanth Sreenivasa #include <sys/metaslab.h>
345cabbc6bSPrashanth Sreenivasa #include <sys/metaslab_impl.h>
355cabbc6bSPrashanth Sreenivasa #include <sys/uberblock_impl.h>
365cabbc6bSPrashanth Sreenivasa #include <sys/txg.h>
375cabbc6bSPrashanth Sreenivasa #include <sys/avl.h>
385cabbc6bSPrashanth Sreenivasa #include <sys/bpobj.h>
395cabbc6bSPrashanth Sreenivasa #include <sys/dsl_pool.h>
405cabbc6bSPrashanth Sreenivasa #include <sys/dsl_synctask.h>
415cabbc6bSPrashanth Sreenivasa #include <sys/dsl_dir.h>
425cabbc6bSPrashanth Sreenivasa #include <sys/arc.h>
435cabbc6bSPrashanth Sreenivasa #include <sys/zfeature.h>
445cabbc6bSPrashanth Sreenivasa #include <sys/vdev_indirect_births.h>
455cabbc6bSPrashanth Sreenivasa #include <sys/vdev_indirect_mapping.h>
465cabbc6bSPrashanth Sreenivasa #include <sys/abd.h>
47094e47e9SGeorge Wilson #include <sys/vdev_initialize.h>
485cabbc6bSPrashanth Sreenivasa 
495cabbc6bSPrashanth Sreenivasa /*
505cabbc6bSPrashanth Sreenivasa  * This file contains the necessary logic to remove vdevs from a
515cabbc6bSPrashanth Sreenivasa  * storage pool.  Currently, the only devices that can be removed
525cabbc6bSPrashanth Sreenivasa  * are log, cache, and spare devices; and top level vdevs from a pool
535cabbc6bSPrashanth Sreenivasa  * w/o raidz.  (Note that members of a mirror can also be removed
545cabbc6bSPrashanth Sreenivasa  * by the detach operation.)
555cabbc6bSPrashanth Sreenivasa  *
565cabbc6bSPrashanth Sreenivasa  * Log vdevs are removed by evacuating them and then turning the vdev
575cabbc6bSPrashanth Sreenivasa  * into a hole vdev while holding spa config locks.
585cabbc6bSPrashanth Sreenivasa  *
595cabbc6bSPrashanth Sreenivasa  * Top level vdevs are removed and converted into an indirect vdev via
605cabbc6bSPrashanth Sreenivasa  * a multi-step process:
615cabbc6bSPrashanth Sreenivasa  *
625cabbc6bSPrashanth Sreenivasa  *  - Disable allocations from this device (spa_vdev_remove_top).
635cabbc6bSPrashanth Sreenivasa  *
645cabbc6bSPrashanth Sreenivasa  *  - From a new thread (spa_vdev_remove_thread), copy data from
655cabbc6bSPrashanth Sreenivasa  *    the removing vdev to a different vdev.  The copy happens in open
665cabbc6bSPrashanth Sreenivasa  *    context (spa_vdev_copy_impl) and issues a sync task
675cabbc6bSPrashanth Sreenivasa  *    (vdev_mapping_sync) so the sync thread can update the partial
685cabbc6bSPrashanth Sreenivasa  *    indirect mappings in core and on disk.
695cabbc6bSPrashanth Sreenivasa  *
705cabbc6bSPrashanth Sreenivasa  *  - If a free happens during a removal, it is freed from the
715cabbc6bSPrashanth Sreenivasa  *    removing vdev, and if it has already been copied, from the new
725cabbc6bSPrashanth Sreenivasa  *    location as well (free_from_removing_vdev).
735cabbc6bSPrashanth Sreenivasa  *
745cabbc6bSPrashanth Sreenivasa  *  - After the removal is completed, the copy thread converts the vdev
755cabbc6bSPrashanth Sreenivasa  *    into an indirect vdev (vdev_remove_complete) before instructing
765cabbc6bSPrashanth Sreenivasa  *    the sync thread to destroy the space maps and finish the removal
775cabbc6bSPrashanth Sreenivasa  *    (spa_finish_removal).
785cabbc6bSPrashanth Sreenivasa  */
795cabbc6bSPrashanth Sreenivasa 
805cabbc6bSPrashanth Sreenivasa typedef struct vdev_copy_arg {
815cabbc6bSPrashanth Sreenivasa 	metaslab_t	*vca_msp;
825cabbc6bSPrashanth Sreenivasa 	uint64_t	vca_outstanding_bytes;
835cabbc6bSPrashanth Sreenivasa 	kcondvar_t	vca_cv;
845cabbc6bSPrashanth Sreenivasa 	kmutex_t	vca_lock;
855cabbc6bSPrashanth Sreenivasa } vdev_copy_arg_t;
865cabbc6bSPrashanth Sreenivasa 
875cabbc6bSPrashanth Sreenivasa /*
883a4b1be9SMatthew Ahrens  * The maximum amount of memory we can use for outstanding i/o while
893a4b1be9SMatthew Ahrens  * doing a device removal.  This determines how much i/o we can have
903a4b1be9SMatthew Ahrens  * in flight concurrently.
915cabbc6bSPrashanth Sreenivasa  */
923a4b1be9SMatthew Ahrens int zfs_remove_max_copy_bytes = 64 * 1024 * 1024;
935cabbc6bSPrashanth Sreenivasa 
945cabbc6bSPrashanth Sreenivasa /*
955cabbc6bSPrashanth Sreenivasa  * The largest contiguous segment that we will attempt to allocate when
965cabbc6bSPrashanth Sreenivasa  * removing a device.  This can be no larger than SPA_MAXBLOCKSIZE.  If
975cabbc6bSPrashanth Sreenivasa  * there is a performance problem with attempting to allocate large blocks,
985cabbc6bSPrashanth Sreenivasa  * consider decreasing this.
995cabbc6bSPrashanth Sreenivasa  *
1005cabbc6bSPrashanth Sreenivasa  * Note: we will issue I/Os of up to this size.  The mpt driver does not
1015cabbc6bSPrashanth Sreenivasa  * respond well to I/Os larger than 1MB, so we set this to 1MB.  (When
1025cabbc6bSPrashanth Sreenivasa  * mpt processes an I/O larger than 1MB, it needs to do an allocation of
1035cabbc6bSPrashanth Sreenivasa  * 2 physically contiguous pages; if this allocation fails, mpt will drop
1045cabbc6bSPrashanth Sreenivasa  * the I/O and hang the device.)
1055cabbc6bSPrashanth Sreenivasa  */
1065cabbc6bSPrashanth Sreenivasa int zfs_remove_max_segment = 1024 * 1024;
1075cabbc6bSPrashanth Sreenivasa 
108*cfd63e1bSMatthew Ahrens /*
109*cfd63e1bSMatthew Ahrens  * Allow a remap segment to span free chunks of at most this size. The main
110*cfd63e1bSMatthew Ahrens  * impact of a larger span is that we will read and write larger, more
111*cfd63e1bSMatthew Ahrens  * contiguous chunks, with more "unnecessary" data -- trading off bandwidth
112*cfd63e1bSMatthew Ahrens  * for iops.  The value here was chosen to align with
113*cfd63e1bSMatthew Ahrens  * zfs_vdev_read_gap_limit, which is a similar concept when doing regular
114*cfd63e1bSMatthew Ahrens  * reads (but there's no reason it has to be the same).
115*cfd63e1bSMatthew Ahrens  *
116*cfd63e1bSMatthew Ahrens  * Additionally, a higher span will have the following relatively minor
117*cfd63e1bSMatthew Ahrens  * effects:
118*cfd63e1bSMatthew Ahrens  *  - the mapping will be smaller, since one entry can cover more allocated
119*cfd63e1bSMatthew Ahrens  *    segments
120*cfd63e1bSMatthew Ahrens  *  - more of the fragmentation in the removing device will be preserved
121*cfd63e1bSMatthew Ahrens  *  - we'll do larger allocations, which may fail and fall back on smaller
122*cfd63e1bSMatthew Ahrens  *    allocations
123*cfd63e1bSMatthew Ahrens  */
124*cfd63e1bSMatthew Ahrens int vdev_removal_max_span = 32 * 1024;
125*cfd63e1bSMatthew Ahrens 
12686714001SSerapheim Dimitropoulos /*
12786714001SSerapheim Dimitropoulos  * This is used by the test suite so that it can ensure that certain
12886714001SSerapheim Dimitropoulos  * actions happen while in the middle of a removal.
12986714001SSerapheim Dimitropoulos  */
13086714001SSerapheim Dimitropoulos uint64_t zfs_remove_max_bytes_pause = UINT64_MAX;
13186714001SSerapheim Dimitropoulos 
1325cabbc6bSPrashanth Sreenivasa #define	VDEV_REMOVAL_ZAP_OBJS	"lzap"
1335cabbc6bSPrashanth Sreenivasa 
1345cabbc6bSPrashanth Sreenivasa static void spa_vdev_remove_thread(void *arg);
1355cabbc6bSPrashanth Sreenivasa 
1365cabbc6bSPrashanth Sreenivasa static void
1375cabbc6bSPrashanth Sreenivasa spa_sync_removing_state(spa_t *spa, dmu_tx_t *tx)
1385cabbc6bSPrashanth Sreenivasa {
1395cabbc6bSPrashanth Sreenivasa 	VERIFY0(zap_update(spa->spa_dsl_pool->dp_meta_objset,
1405cabbc6bSPrashanth Sreenivasa 	    DMU_POOL_DIRECTORY_OBJECT,
1415cabbc6bSPrashanth Sreenivasa 	    DMU_POOL_REMOVING, sizeof (uint64_t),
1425cabbc6bSPrashanth Sreenivasa 	    sizeof (spa->spa_removing_phys) / sizeof (uint64_t),
1435cabbc6bSPrashanth Sreenivasa 	    &spa->spa_removing_phys, tx));
1445cabbc6bSPrashanth Sreenivasa }
1455cabbc6bSPrashanth Sreenivasa 
1465cabbc6bSPrashanth Sreenivasa static nvlist_t *
1475cabbc6bSPrashanth Sreenivasa spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
1485cabbc6bSPrashanth Sreenivasa {
1495cabbc6bSPrashanth Sreenivasa 	for (int i = 0; i < count; i++) {
1505cabbc6bSPrashanth Sreenivasa 		uint64_t guid =
1515cabbc6bSPrashanth Sreenivasa 		    fnvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID);
1525cabbc6bSPrashanth Sreenivasa 
1535cabbc6bSPrashanth Sreenivasa 		if (guid == target_guid)
1545cabbc6bSPrashanth Sreenivasa 			return (nvpp[i]);
1555cabbc6bSPrashanth Sreenivasa 	}
1565cabbc6bSPrashanth Sreenivasa 
1575cabbc6bSPrashanth Sreenivasa 	return (NULL);
1585cabbc6bSPrashanth Sreenivasa }
1595cabbc6bSPrashanth Sreenivasa 
1605cabbc6bSPrashanth Sreenivasa static void
1615cabbc6bSPrashanth Sreenivasa spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
1625cabbc6bSPrashanth Sreenivasa     nvlist_t *dev_to_remove)
1635cabbc6bSPrashanth Sreenivasa {
1645cabbc6bSPrashanth Sreenivasa 	nvlist_t **newdev = NULL;
1655cabbc6bSPrashanth Sreenivasa 
1665cabbc6bSPrashanth Sreenivasa 	if (count > 1)
1675cabbc6bSPrashanth Sreenivasa 		newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
1685cabbc6bSPrashanth Sreenivasa 
1695cabbc6bSPrashanth Sreenivasa 	for (int i = 0, j = 0; i < count; i++) {
1705cabbc6bSPrashanth Sreenivasa 		if (dev[i] == dev_to_remove)
1715cabbc6bSPrashanth Sreenivasa 			continue;
1725cabbc6bSPrashanth Sreenivasa 		VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
1735cabbc6bSPrashanth Sreenivasa 	}
1745cabbc6bSPrashanth Sreenivasa 
1755cabbc6bSPrashanth Sreenivasa 	VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
1765cabbc6bSPrashanth Sreenivasa 	VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
1775cabbc6bSPrashanth Sreenivasa 
1785cabbc6bSPrashanth Sreenivasa 	for (int i = 0; i < count - 1; i++)
1795cabbc6bSPrashanth Sreenivasa 		nvlist_free(newdev[i]);
1805cabbc6bSPrashanth Sreenivasa 
1815cabbc6bSPrashanth Sreenivasa 	if (count > 1)
1825cabbc6bSPrashanth Sreenivasa 		kmem_free(newdev, (count - 1) * sizeof (void *));
1835cabbc6bSPrashanth Sreenivasa }
1845cabbc6bSPrashanth Sreenivasa 
1855cabbc6bSPrashanth Sreenivasa static spa_vdev_removal_t *
1865cabbc6bSPrashanth Sreenivasa spa_vdev_removal_create(vdev_t *vd)
1875cabbc6bSPrashanth Sreenivasa {
1885cabbc6bSPrashanth Sreenivasa 	spa_vdev_removal_t *svr = kmem_zalloc(sizeof (*svr), KM_SLEEP);
1895cabbc6bSPrashanth Sreenivasa 	mutex_init(&svr->svr_lock, NULL, MUTEX_DEFAULT, NULL);
1905cabbc6bSPrashanth Sreenivasa 	cv_init(&svr->svr_cv, NULL, CV_DEFAULT, NULL);
1915cabbc6bSPrashanth Sreenivasa 	svr->svr_allocd_segs = range_tree_create(NULL, NULL);
1923a4b1be9SMatthew Ahrens 	svr->svr_vdev_id = vd->vdev_id;
1935cabbc6bSPrashanth Sreenivasa 
1945cabbc6bSPrashanth Sreenivasa 	for (int i = 0; i < TXG_SIZE; i++) {
1955cabbc6bSPrashanth Sreenivasa 		svr->svr_frees[i] = range_tree_create(NULL, NULL);
1965cabbc6bSPrashanth Sreenivasa 		list_create(&svr->svr_new_segments[i],
1975cabbc6bSPrashanth Sreenivasa 		    sizeof (vdev_indirect_mapping_entry_t),
1985cabbc6bSPrashanth Sreenivasa 		    offsetof(vdev_indirect_mapping_entry_t, vime_node));
1995cabbc6bSPrashanth Sreenivasa 	}
2005cabbc6bSPrashanth Sreenivasa 
2015cabbc6bSPrashanth Sreenivasa 	return (svr);
2025cabbc6bSPrashanth Sreenivasa }
2035cabbc6bSPrashanth Sreenivasa 
2045cabbc6bSPrashanth Sreenivasa void
2055cabbc6bSPrashanth Sreenivasa spa_vdev_removal_destroy(spa_vdev_removal_t *svr)
2065cabbc6bSPrashanth Sreenivasa {
2075cabbc6bSPrashanth Sreenivasa 	for (int i = 0; i < TXG_SIZE; i++) {
2085cabbc6bSPrashanth Sreenivasa 		ASSERT0(svr->svr_bytes_done[i]);
2095cabbc6bSPrashanth Sreenivasa 		ASSERT0(svr->svr_max_offset_to_sync[i]);
2105cabbc6bSPrashanth Sreenivasa 		range_tree_destroy(svr->svr_frees[i]);
2115cabbc6bSPrashanth Sreenivasa 		list_destroy(&svr->svr_new_segments[i]);
2125cabbc6bSPrashanth Sreenivasa 	}
2135cabbc6bSPrashanth Sreenivasa 
2145cabbc6bSPrashanth Sreenivasa 	range_tree_destroy(svr->svr_allocd_segs);
2155cabbc6bSPrashanth Sreenivasa 	mutex_destroy(&svr->svr_lock);
2165cabbc6bSPrashanth Sreenivasa 	cv_destroy(&svr->svr_cv);
2175cabbc6bSPrashanth Sreenivasa 	kmem_free(svr, sizeof (*svr));
2185cabbc6bSPrashanth Sreenivasa }
2195cabbc6bSPrashanth Sreenivasa 
2205cabbc6bSPrashanth Sreenivasa /*
2215cabbc6bSPrashanth Sreenivasa  * This is called as a synctask in the txg in which we will mark this vdev
2225cabbc6bSPrashanth Sreenivasa  * as removing (in the config stored in the MOS).
2235cabbc6bSPrashanth Sreenivasa  *
2245cabbc6bSPrashanth Sreenivasa  * It begins the evacuation of a toplevel vdev by:
2255cabbc6bSPrashanth Sreenivasa  * - initializing the spa_removing_phys which tracks this removal
2265cabbc6bSPrashanth Sreenivasa  * - computing the amount of space to remove for accounting purposes
2275cabbc6bSPrashanth Sreenivasa  * - dirtying all dbufs in the spa_config_object
2285cabbc6bSPrashanth Sreenivasa  * - creating the spa_vdev_removal
2295cabbc6bSPrashanth Sreenivasa  * - starting the spa_vdev_remove_thread
2305cabbc6bSPrashanth Sreenivasa  */
2315cabbc6bSPrashanth Sreenivasa static void
2325cabbc6bSPrashanth Sreenivasa vdev_remove_initiate_sync(void *arg, dmu_tx_t *tx)
2335cabbc6bSPrashanth Sreenivasa {
2343a4b1be9SMatthew Ahrens 	int vdev_id = (uintptr_t)arg;
2353a4b1be9SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
2363a4b1be9SMatthew Ahrens 	vdev_t *vd = vdev_lookup_top(spa, vdev_id);
2375cabbc6bSPrashanth Sreenivasa 	vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
2385cabbc6bSPrashanth Sreenivasa 	objset_t *mos = spa->spa_dsl_pool->dp_meta_objset;
2395cabbc6bSPrashanth Sreenivasa 	spa_vdev_removal_t *svr = NULL;
2405cabbc6bSPrashanth Sreenivasa 	uint64_t txg = dmu_tx_get_txg(tx);
2415cabbc6bSPrashanth Sreenivasa 
2425cabbc6bSPrashanth Sreenivasa 	ASSERT3P(vd->vdev_ops, !=, &vdev_raidz_ops);
2435cabbc6bSPrashanth Sreenivasa 	svr = spa_vdev_removal_create(vd);
2445cabbc6bSPrashanth Sreenivasa 
2455cabbc6bSPrashanth Sreenivasa 	ASSERT(vd->vdev_removing);
2465cabbc6bSPrashanth Sreenivasa 	ASSERT3P(vd->vdev_indirect_mapping, ==, NULL);
2475cabbc6bSPrashanth Sreenivasa 
2485cabbc6bSPrashanth Sreenivasa 	spa_feature_incr(spa, SPA_FEATURE_DEVICE_REMOVAL, tx);
2495cabbc6bSPrashanth Sreenivasa 	if (spa_feature_is_enabled(spa, SPA_FEATURE_OBSOLETE_COUNTS)) {
2505cabbc6bSPrashanth Sreenivasa 		/*
2515cabbc6bSPrashanth Sreenivasa 		 * By activating the OBSOLETE_COUNTS feature, we prevent
2525cabbc6bSPrashanth Sreenivasa 		 * the pool from being downgraded and ensure that the
2535cabbc6bSPrashanth Sreenivasa 		 * refcounts are precise.
2545cabbc6bSPrashanth Sreenivasa 		 */
2555cabbc6bSPrashanth Sreenivasa 		spa_feature_incr(spa, SPA_FEATURE_OBSOLETE_COUNTS, tx);
2565cabbc6bSPrashanth Sreenivasa 		uint64_t one = 1;
2575cabbc6bSPrashanth Sreenivasa 		VERIFY0(zap_add(spa->spa_meta_objset, vd->vdev_top_zap,
2585cabbc6bSPrashanth Sreenivasa 		    VDEV_TOP_ZAP_OBSOLETE_COUNTS_ARE_PRECISE, sizeof (one), 1,
2595cabbc6bSPrashanth Sreenivasa 		    &one, tx));
2605cabbc6bSPrashanth Sreenivasa 		ASSERT3U(vdev_obsolete_counts_are_precise(vd), !=, 0);
2615cabbc6bSPrashanth Sreenivasa 	}
2625cabbc6bSPrashanth Sreenivasa 
2635cabbc6bSPrashanth Sreenivasa 	vic->vic_mapping_object = vdev_indirect_mapping_alloc(mos, tx);
2645cabbc6bSPrashanth Sreenivasa 	vd->vdev_indirect_mapping =
2655cabbc6bSPrashanth Sreenivasa 	    vdev_indirect_mapping_open(mos, vic->vic_mapping_object);
2665cabbc6bSPrashanth Sreenivasa 	vic->vic_births_object = vdev_indirect_births_alloc(mos, tx);
2675cabbc6bSPrashanth Sreenivasa 	vd->vdev_indirect_births =
2685cabbc6bSPrashanth Sreenivasa 	    vdev_indirect_births_open(mos, vic->vic_births_object);
2695cabbc6bSPrashanth Sreenivasa 	spa->spa_removing_phys.sr_removing_vdev = vd->vdev_id;
2705cabbc6bSPrashanth Sreenivasa 	spa->spa_removing_phys.sr_start_time = gethrestime_sec();
2715cabbc6bSPrashanth Sreenivasa 	spa->spa_removing_phys.sr_end_time = 0;
2725cabbc6bSPrashanth Sreenivasa 	spa->spa_removing_phys.sr_state = DSS_SCANNING;
2735cabbc6bSPrashanth Sreenivasa 	spa->spa_removing_phys.sr_to_copy = 0;
2745cabbc6bSPrashanth Sreenivasa 	spa->spa_removing_phys.sr_copied = 0;
2755cabbc6bSPrashanth Sreenivasa 
2765cabbc6bSPrashanth Sreenivasa 	/*
2775cabbc6bSPrashanth Sreenivasa 	 * Note: We can't use vdev_stat's vs_alloc for sr_to_copy, because
2785cabbc6bSPrashanth Sreenivasa 	 * there may be space in the defer tree, which is free, but still
2795cabbc6bSPrashanth Sreenivasa 	 * counted in vs_alloc.
2805cabbc6bSPrashanth Sreenivasa 	 */
2815cabbc6bSPrashanth Sreenivasa 	for (uint64_t i = 0; i < vd->vdev_ms_count; i++) {
2825cabbc6bSPrashanth Sreenivasa 		metaslab_t *ms = vd->vdev_ms[i];
2835cabbc6bSPrashanth Sreenivasa 		if (ms->ms_sm == NULL)
2845cabbc6bSPrashanth Sreenivasa 			continue;
2855cabbc6bSPrashanth Sreenivasa 
2865cabbc6bSPrashanth Sreenivasa 		/*
2875cabbc6bSPrashanth Sreenivasa 		 * Sync tasks happen before metaslab_sync(), therefore
2885cabbc6bSPrashanth Sreenivasa 		 * smp_alloc and sm_alloc must be the same.
2895cabbc6bSPrashanth Sreenivasa 		 */
2905cabbc6bSPrashanth Sreenivasa 		ASSERT3U(space_map_allocated(ms->ms_sm), ==,
2915cabbc6bSPrashanth Sreenivasa 		    ms->ms_sm->sm_phys->smp_alloc);
2925cabbc6bSPrashanth Sreenivasa 
2935cabbc6bSPrashanth Sreenivasa 		spa->spa_removing_phys.sr_to_copy +=
2945cabbc6bSPrashanth Sreenivasa 		    space_map_allocated(ms->ms_sm);
2955cabbc6bSPrashanth Sreenivasa 
2965cabbc6bSPrashanth Sreenivasa 		/*
2975cabbc6bSPrashanth Sreenivasa 		 * Space which we are freeing this txg does not need to
2985cabbc6bSPrashanth Sreenivasa 		 * be copied.
2995cabbc6bSPrashanth Sreenivasa 		 */
3005cabbc6bSPrashanth Sreenivasa 		spa->spa_removing_phys.sr_to_copy -=
30186714001SSerapheim Dimitropoulos 		    range_tree_space(ms->ms_freeing);
3025cabbc6bSPrashanth Sreenivasa 
30386714001SSerapheim Dimitropoulos 		ASSERT0(range_tree_space(ms->ms_freed));
3045cabbc6bSPrashanth Sreenivasa 		for (int t = 0; t < TXG_SIZE; t++)
30586714001SSerapheim Dimitropoulos 			ASSERT0(range_tree_space(ms->ms_allocating[t]));
3065cabbc6bSPrashanth Sreenivasa 	}
3075cabbc6bSPrashanth Sreenivasa 
3085cabbc6bSPrashanth Sreenivasa 	/*
3095cabbc6bSPrashanth Sreenivasa 	 * Sync tasks are called before metaslab_sync(), so there should
3105cabbc6bSPrashanth Sreenivasa 	 * be no already-synced metaslabs in the TXG_CLEAN list.
3115cabbc6bSPrashanth Sreenivasa 	 */
3125cabbc6bSPrashanth Sreenivasa 	ASSERT3P(txg_list_head(&vd->vdev_ms_list, TXG_CLEAN(txg)), ==, NULL);
3135cabbc6bSPrashanth Sreenivasa 
3145cabbc6bSPrashanth Sreenivasa 	spa_sync_removing_state(spa, tx);
3155cabbc6bSPrashanth Sreenivasa 
3165cabbc6bSPrashanth Sreenivasa 	/*
3175cabbc6bSPrashanth Sreenivasa 	 * All blocks that we need to read the most recent mapping must be
3185cabbc6bSPrashanth Sreenivasa 	 * stored on concrete vdevs.  Therefore, we must dirty anything that
3195cabbc6bSPrashanth Sreenivasa 	 * is read before spa_remove_init().  Specifically, the
3205cabbc6bSPrashanth Sreenivasa 	 * spa_config_object.  (Note that although we already modified the
3215cabbc6bSPrashanth Sreenivasa 	 * spa_config_object in spa_sync_removing_state, that may not have
3225cabbc6bSPrashanth Sreenivasa 	 * modified all blocks of the object.)
3235cabbc6bSPrashanth Sreenivasa 	 */
3245cabbc6bSPrashanth Sreenivasa 	dmu_object_info_t doi;
3255cabbc6bSPrashanth Sreenivasa 	VERIFY0(dmu_object_info(mos, DMU_POOL_DIRECTORY_OBJECT, &doi));
3265cabbc6bSPrashanth Sreenivasa 	for (uint64_t offset = 0; offset < doi.doi_max_offset; ) {
3275cabbc6bSPrashanth Sreenivasa 		dmu_buf_t *dbuf;
3285cabbc6bSPrashanth Sreenivasa 		VERIFY0(dmu_buf_hold(mos, DMU_POOL_DIRECTORY_OBJECT,
3295cabbc6bSPrashanth Sreenivasa 		    offset, FTAG, &dbuf, 0));
3305cabbc6bSPrashanth Sreenivasa 		dmu_buf_will_dirty(dbuf, tx);
3315cabbc6bSPrashanth Sreenivasa 		offset += dbuf->db_size;
3325cabbc6bSPrashanth Sreenivasa 		dmu_buf_rele(dbuf, FTAG);
3335cabbc6bSPrashanth Sreenivasa 	}
3345cabbc6bSPrashanth Sreenivasa 
3355cabbc6bSPrashanth Sreenivasa 	/*
3365cabbc6bSPrashanth Sreenivasa 	 * Now that we've allocated the im_object, dirty the vdev to ensure
3375cabbc6bSPrashanth Sreenivasa 	 * that the object gets written to the config on disk.
3385cabbc6bSPrashanth Sreenivasa 	 */
3395cabbc6bSPrashanth Sreenivasa 	vdev_config_dirty(vd);
3405cabbc6bSPrashanth Sreenivasa 
3415cabbc6bSPrashanth Sreenivasa 	zfs_dbgmsg("starting removal thread for vdev %llu (%p) in txg %llu "
3425cabbc6bSPrashanth Sreenivasa 	    "im_obj=%llu", vd->vdev_id, vd, dmu_tx_get_txg(tx),
3435cabbc6bSPrashanth Sreenivasa 	    vic->vic_mapping_object);
3445cabbc6bSPrashanth Sreenivasa 
3455cabbc6bSPrashanth Sreenivasa 	spa_history_log_internal(spa, "vdev remove started", tx,
3465cabbc6bSPrashanth Sreenivasa 	    "%s vdev %llu %s", spa_name(spa), vd->vdev_id,
3475cabbc6bSPrashanth Sreenivasa 	    (vd->vdev_path != NULL) ? vd->vdev_path : "-");
3485cabbc6bSPrashanth Sreenivasa 	/*
3495cabbc6bSPrashanth Sreenivasa 	 * Setting spa_vdev_removal causes subsequent frees to call
3505cabbc6bSPrashanth Sreenivasa 	 * free_from_removing_vdev().  Note that we don't need any locking
3515cabbc6bSPrashanth Sreenivasa 	 * because we are the sync thread, and metaslab_free_impl() is only
3525cabbc6bSPrashanth Sreenivasa 	 * called from syncing context (potentially from a zio taskq thread,
3535cabbc6bSPrashanth Sreenivasa 	 * but in any case only when there are outstanding free i/os, which
3545cabbc6bSPrashanth Sreenivasa 	 * there are not).
3555cabbc6bSPrashanth Sreenivasa 	 */
3565cabbc6bSPrashanth Sreenivasa 	ASSERT3P(spa->spa_vdev_removal, ==, NULL);
3575cabbc6bSPrashanth Sreenivasa 	spa->spa_vdev_removal = svr;
3585cabbc6bSPrashanth Sreenivasa 	svr->svr_thread = thread_create(NULL, 0,
3593a4b1be9SMatthew Ahrens 	    spa_vdev_remove_thread, spa, 0, &p0, TS_RUN, minclsyspri);
3605cabbc6bSPrashanth Sreenivasa }
3615cabbc6bSPrashanth Sreenivasa 
3625cabbc6bSPrashanth Sreenivasa /*
3635cabbc6bSPrashanth Sreenivasa  * When we are opening a pool, we must read the mapping for each
3645cabbc6bSPrashanth Sreenivasa  * indirect vdev in order from most recently removed to least
3655cabbc6bSPrashanth Sreenivasa  * recently removed.  We do this because the blocks for the mapping
3665cabbc6bSPrashanth Sreenivasa  * of older indirect vdevs may be stored on more recently removed vdevs.
3675cabbc6bSPrashanth Sreenivasa  * In order to read each indirect mapping object, we must have
3685cabbc6bSPrashanth Sreenivasa  * initialized all more recently removed vdevs.
3695cabbc6bSPrashanth Sreenivasa  */
3705cabbc6bSPrashanth Sreenivasa int
3715cabbc6bSPrashanth Sreenivasa spa_remove_init(spa_t *spa)
3725cabbc6bSPrashanth Sreenivasa {
3735cabbc6bSPrashanth Sreenivasa 	int error;
3745cabbc6bSPrashanth Sreenivasa 
3755cabbc6bSPrashanth Sreenivasa 	error = zap_lookup(spa->spa_dsl_pool->dp_meta_objset,
3765cabbc6bSPrashanth Sreenivasa 	    DMU_POOL_DIRECTORY_OBJECT,
3775cabbc6bSPrashanth Sreenivasa 	    DMU_POOL_REMOVING, sizeof (uint64_t),
3785cabbc6bSPrashanth Sreenivasa 	    sizeof (spa->spa_removing_phys) / sizeof (uint64_t),
3795cabbc6bSPrashanth Sreenivasa 	    &spa->spa_removing_phys);
3805cabbc6bSPrashanth Sreenivasa 
3815cabbc6bSPrashanth Sreenivasa 	if (error == ENOENT) {
3825cabbc6bSPrashanth Sreenivasa 		spa->spa_removing_phys.sr_state = DSS_NONE;
3835cabbc6bSPrashanth Sreenivasa 		spa->spa_removing_phys.sr_removing_vdev = -1;
3845cabbc6bSPrashanth Sreenivasa 		spa->spa_removing_phys.sr_prev_indirect_vdev = -1;
38547b8d4b8SAlexander Motin 		spa->spa_indirect_vdevs_loaded = B_TRUE;
3865cabbc6bSPrashanth Sreenivasa 		return (0);
3875cabbc6bSPrashanth Sreenivasa 	} else if (error != 0) {
3885cabbc6bSPrashanth Sreenivasa 		return (error);
3895cabbc6bSPrashanth Sreenivasa 	}
3905cabbc6bSPrashanth Sreenivasa 
3915cabbc6bSPrashanth Sreenivasa 	if (spa->spa_removing_phys.sr_state == DSS_SCANNING) {
3925cabbc6bSPrashanth Sreenivasa 		/*
3935cabbc6bSPrashanth Sreenivasa 		 * We are currently removing a vdev.  Create and
3945cabbc6bSPrashanth Sreenivasa 		 * initialize a spa_vdev_removal_t from the bonus
3955cabbc6bSPrashanth Sreenivasa 		 * buffer of the removing vdevs vdev_im_object, and
3965cabbc6bSPrashanth Sreenivasa 		 * initialize its partial mapping.
3975cabbc6bSPrashanth Sreenivasa 		 */
3985cabbc6bSPrashanth Sreenivasa 		spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
3995cabbc6bSPrashanth Sreenivasa 		vdev_t *vd = vdev_lookup_top(spa,
4005cabbc6bSPrashanth Sreenivasa 		    spa->spa_removing_phys.sr_removing_vdev);
4015cabbc6bSPrashanth Sreenivasa 
4023a4b1be9SMatthew Ahrens 		if (vd == NULL) {
4033a4b1be9SMatthew Ahrens 			spa_config_exit(spa, SCL_STATE, FTAG);
4045cabbc6bSPrashanth Sreenivasa 			return (EINVAL);
4053a4b1be9SMatthew Ahrens 		}
4065cabbc6bSPrashanth Sreenivasa 
4075cabbc6bSPrashanth Sreenivasa 		vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
4085cabbc6bSPrashanth Sreenivasa 
4095cabbc6bSPrashanth Sreenivasa 		ASSERT(vdev_is_concrete(vd));
4105cabbc6bSPrashanth Sreenivasa 		spa_vdev_removal_t *svr = spa_vdev_removal_create(vd);
4113a4b1be9SMatthew Ahrens 		ASSERT3U(svr->svr_vdev_id, ==, vd->vdev_id);
4123a4b1be9SMatthew Ahrens 		ASSERT(vd->vdev_removing);
4135cabbc6bSPrashanth Sreenivasa 
4145cabbc6bSPrashanth Sreenivasa 		vd->vdev_indirect_mapping = vdev_indirect_mapping_open(
4155cabbc6bSPrashanth Sreenivasa 		    spa->spa_meta_objset, vic->vic_mapping_object);
4165cabbc6bSPrashanth Sreenivasa 		vd->vdev_indirect_births = vdev_indirect_births_open(
4175cabbc6bSPrashanth Sreenivasa 		    spa->spa_meta_objset, vic->vic_births_object);
4183a4b1be9SMatthew Ahrens 		spa_config_exit(spa, SCL_STATE, FTAG);
4195cabbc6bSPrashanth Sreenivasa 
4205cabbc6bSPrashanth Sreenivasa 		spa->spa_vdev_removal = svr;
4215cabbc6bSPrashanth Sreenivasa 	}
4225cabbc6bSPrashanth Sreenivasa 
4235cabbc6bSPrashanth Sreenivasa 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
4245cabbc6bSPrashanth Sreenivasa 	uint64_t indirect_vdev_id =
4255cabbc6bSPrashanth Sreenivasa 	    spa->spa_removing_phys.sr_prev_indirect_vdev;
4265cabbc6bSPrashanth Sreenivasa 	while (indirect_vdev_id != UINT64_MAX) {
4275cabbc6bSPrashanth Sreenivasa 		vdev_t *vd = vdev_lookup_top(spa, indirect_vdev_id);
4285cabbc6bSPrashanth Sreenivasa 		vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
4295cabbc6bSPrashanth Sreenivasa 
4305cabbc6bSPrashanth Sreenivasa 		ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops);
4315cabbc6bSPrashanth Sreenivasa 		vd->vdev_indirect_mapping = vdev_indirect_mapping_open(
4325cabbc6bSPrashanth Sreenivasa 		    spa->spa_meta_objset, vic->vic_mapping_object);
4335cabbc6bSPrashanth Sreenivasa 		vd->vdev_indirect_births = vdev_indirect_births_open(
4345cabbc6bSPrashanth Sreenivasa 		    spa->spa_meta_objset, vic->vic_births_object);
4355cabbc6bSPrashanth Sreenivasa 
4365cabbc6bSPrashanth Sreenivasa 		indirect_vdev_id = vic->vic_prev_indirect_vdev;
4375cabbc6bSPrashanth Sreenivasa 	}
4385cabbc6bSPrashanth Sreenivasa 	spa_config_exit(spa, SCL_STATE, FTAG);
4395cabbc6bSPrashanth Sreenivasa 
4405cabbc6bSPrashanth Sreenivasa 	/*
4415cabbc6bSPrashanth Sreenivasa 	 * Now that we've loaded all the indirect mappings, we can allow
4425cabbc6bSPrashanth Sreenivasa 	 * reads from other blocks (e.g. via predictive prefetch).
4435cabbc6bSPrashanth Sreenivasa 	 */
4445cabbc6bSPrashanth Sreenivasa 	spa->spa_indirect_vdevs_loaded = B_TRUE;
4455cabbc6bSPrashanth Sreenivasa 	return (0);
4465cabbc6bSPrashanth Sreenivasa }
4475cabbc6bSPrashanth Sreenivasa 
4485cabbc6bSPrashanth Sreenivasa void
4495cabbc6bSPrashanth Sreenivasa spa_restart_removal(spa_t *spa)
4505cabbc6bSPrashanth Sreenivasa {
4515cabbc6bSPrashanth Sreenivasa 	spa_vdev_removal_t *svr = spa->spa_vdev_removal;
4525cabbc6bSPrashanth Sreenivasa 
4535cabbc6bSPrashanth Sreenivasa 	if (svr == NULL)
4545cabbc6bSPrashanth Sreenivasa 		return;
4555cabbc6bSPrashanth Sreenivasa 
4565cabbc6bSPrashanth Sreenivasa 	/*
4575cabbc6bSPrashanth Sreenivasa 	 * In general when this function is called there is no
4585cabbc6bSPrashanth Sreenivasa 	 * removal thread running. The only scenario where this
4595cabbc6bSPrashanth Sreenivasa 	 * is not true is during spa_import() where this function
4605cabbc6bSPrashanth Sreenivasa 	 * is called twice [once from spa_import_impl() and
4615cabbc6bSPrashanth Sreenivasa 	 * spa_async_resume()]. Thus, in the scenario where we
4625cabbc6bSPrashanth Sreenivasa 	 * import a pool that has an ongoing removal we don't
4635cabbc6bSPrashanth Sreenivasa 	 * want to spawn a second thread.
4645cabbc6bSPrashanth Sreenivasa 	 */
4655cabbc6bSPrashanth Sreenivasa 	if (svr->svr_thread != NULL)
4665cabbc6bSPrashanth Sreenivasa 		return;
4675cabbc6bSPrashanth Sreenivasa 
4685cabbc6bSPrashanth Sreenivasa 	if (!spa_writeable(spa))
4695cabbc6bSPrashanth Sreenivasa 		return;
4705cabbc6bSPrashanth Sreenivasa 
4713a4b1be9SMatthew Ahrens 	zfs_dbgmsg("restarting removal of %llu", svr->svr_vdev_id);
4723a4b1be9SMatthew Ahrens 	svr->svr_thread = thread_create(NULL, 0, spa_vdev_remove_thread, spa,
4735cabbc6bSPrashanth Sreenivasa 	    0, &p0, TS_RUN, minclsyspri);
4745cabbc6bSPrashanth Sreenivasa }
4755cabbc6bSPrashanth Sreenivasa 
4765cabbc6bSPrashanth Sreenivasa /*
4775cabbc6bSPrashanth Sreenivasa  * Process freeing from a device which is in the middle of being removed.
4785cabbc6bSPrashanth Sreenivasa  * We must handle this carefully so that we attempt to copy freed data,
4795cabbc6bSPrashanth Sreenivasa  * and we correctly free already-copied data.
4805cabbc6bSPrashanth Sreenivasa  */
4815cabbc6bSPrashanth Sreenivasa void
48286714001SSerapheim Dimitropoulos free_from_removing_vdev(vdev_t *vd, uint64_t offset, uint64_t size)
4835cabbc6bSPrashanth Sreenivasa {
4845cabbc6bSPrashanth Sreenivasa 	spa_t *spa = vd->vdev_spa;
4855cabbc6bSPrashanth Sreenivasa 	spa_vdev_removal_t *svr = spa->spa_vdev_removal;
4865cabbc6bSPrashanth Sreenivasa 	vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
48786714001SSerapheim Dimitropoulos 	uint64_t txg = spa_syncing_txg(spa);
4885cabbc6bSPrashanth Sreenivasa 	uint64_t max_offset_yet = 0;
4895cabbc6bSPrashanth Sreenivasa 
4905cabbc6bSPrashanth Sreenivasa 	ASSERT(vd->vdev_indirect_config.vic_mapping_object != 0);
4915cabbc6bSPrashanth Sreenivasa 	ASSERT3U(vd->vdev_indirect_config.vic_mapping_object, ==,
4925cabbc6bSPrashanth Sreenivasa 	    vdev_indirect_mapping_object(vim));
4933a4b1be9SMatthew Ahrens 	ASSERT3U(vd->vdev_id, ==, svr->svr_vdev_id);
4945cabbc6bSPrashanth Sreenivasa 
4955cabbc6bSPrashanth Sreenivasa 	mutex_enter(&svr->svr_lock);
4965cabbc6bSPrashanth Sreenivasa 
4975cabbc6bSPrashanth Sreenivasa 	/*
4985cabbc6bSPrashanth Sreenivasa 	 * Remove the segment from the removing vdev's spacemap.  This
4995cabbc6bSPrashanth Sreenivasa 	 * ensures that we will not attempt to copy this space (if the
5005cabbc6bSPrashanth Sreenivasa 	 * removal thread has not yet visited it), and also ensures
5015cabbc6bSPrashanth Sreenivasa 	 * that we know what is actually allocated on the new vdevs
5025cabbc6bSPrashanth Sreenivasa 	 * (needed if we cancel the removal).
5035cabbc6bSPrashanth Sreenivasa 	 *
5045cabbc6bSPrashanth Sreenivasa 	 * Note: we must do the metaslab_free_concrete() with the svr_lock
5055cabbc6bSPrashanth Sreenivasa 	 * held, so that the remove_thread can not load this metaslab and then
5065cabbc6bSPrashanth Sreenivasa 	 * visit this offset between the time that we metaslab_free_concrete()
5075cabbc6bSPrashanth Sreenivasa 	 * and when we check to see if it has been visited.
50886714001SSerapheim Dimitropoulos 	 *
50986714001SSerapheim Dimitropoulos 	 * Note: The checkpoint flag is set to false as having/taking
51086714001SSerapheim Dimitropoulos 	 * a checkpoint and removing a device can't happen at the same
51186714001SSerapheim Dimitropoulos 	 * time.
5125cabbc6bSPrashanth Sreenivasa 	 */
51386714001SSerapheim Dimitropoulos 	ASSERT(!spa_has_checkpoint(spa));
51486714001SSerapheim Dimitropoulos 	metaslab_free_concrete(vd, offset, size, B_FALSE);
5155cabbc6bSPrashanth Sreenivasa 
5165cabbc6bSPrashanth Sreenivasa 	uint64_t synced_size = 0;
5175cabbc6bSPrashanth Sreenivasa 	uint64_t synced_offset = 0;
5185cabbc6bSPrashanth Sreenivasa 	uint64_t max_offset_synced = vdev_indirect_mapping_max_offset(vim);
5195cabbc6bSPrashanth Sreenivasa 	if (offset < max_offset_synced) {
5205cabbc6bSPrashanth Sreenivasa 		/*
5215cabbc6bSPrashanth Sreenivasa 		 * The mapping for this offset is already on disk.
5225cabbc6bSPrashanth Sreenivasa 		 * Free from the new location.
5235cabbc6bSPrashanth Sreenivasa 		 *
5245cabbc6bSPrashanth Sreenivasa 		 * Note that we use svr_max_synced_offset because it is
5255cabbc6bSPrashanth Sreenivasa 		 * updated atomically with respect to the in-core mapping.
5265cabbc6bSPrashanth Sreenivasa 		 * By contrast, vim_max_offset is not.
5275cabbc6bSPrashanth Sreenivasa 		 *
5285cabbc6bSPrashanth Sreenivasa 		 * This block may be split between a synced entry and an
5295cabbc6bSPrashanth Sreenivasa 		 * in-flight or unvisited entry.  Only process the synced
5305cabbc6bSPrashanth Sreenivasa 		 * portion of it here.
5315cabbc6bSPrashanth Sreenivasa 		 */
5325cabbc6bSPrashanth Sreenivasa 		synced_size = MIN(size, max_offset_synced - offset);
5335cabbc6bSPrashanth Sreenivasa 		synced_offset = offset;
5345cabbc6bSPrashanth Sreenivasa 
5355cabbc6bSPrashanth Sreenivasa 		ASSERT3U(max_offset_yet, <=, max_offset_synced);
5365cabbc6bSPrashanth Sreenivasa 		max_offset_yet = max_offset_synced;
5375cabbc6bSPrashanth Sreenivasa 
5385cabbc6bSPrashanth Sreenivasa 		DTRACE_PROBE3(remove__free__synced,
5395cabbc6bSPrashanth Sreenivasa 		    spa_t *, spa,
5405cabbc6bSPrashanth Sreenivasa 		    uint64_t, offset,
5415cabbc6bSPrashanth Sreenivasa 		    uint64_t, synced_size);
5425cabbc6bSPrashanth Sreenivasa 
5435cabbc6bSPrashanth Sreenivasa 		size -= synced_size;
5445cabbc6bSPrashanth Sreenivasa 		offset += synced_size;
5455cabbc6bSPrashanth Sreenivasa 	}
5465cabbc6bSPrashanth Sreenivasa 
5475cabbc6bSPrashanth Sreenivasa 	/*
5485cabbc6bSPrashanth Sreenivasa 	 * Look at all in-flight txgs starting from the currently syncing one
5495cabbc6bSPrashanth Sreenivasa 	 * and see if a section of this free is being copied. By starting from
5505cabbc6bSPrashanth Sreenivasa 	 * this txg and iterating forward, we might find that this region
5515cabbc6bSPrashanth Sreenivasa 	 * was copied in two different txgs and handle it appropriately.
5525cabbc6bSPrashanth Sreenivasa 	 */
5535cabbc6bSPrashanth Sreenivasa 	for (int i = 0; i < TXG_CONCURRENT_STATES; i++) {
5545cabbc6bSPrashanth Sreenivasa 		int txgoff = (txg + i) & TXG_MASK;
5555cabbc6bSPrashanth Sreenivasa 		if (size > 0 && offset < svr->svr_max_offset_to_sync[txgoff]) {
5565cabbc6bSPrashanth Sreenivasa 			/*
5575cabbc6bSPrashanth Sreenivasa 			 * The mapping for this offset is in flight, and
5585cabbc6bSPrashanth Sreenivasa 			 * will be synced in txg+i.
5595cabbc6bSPrashanth Sreenivasa 			 */
5605cabbc6bSPrashanth Sreenivasa 			uint64_t inflight_size = MIN(size,
5615cabbc6bSPrashanth Sreenivasa 			    svr->svr_max_offset_to_sync[txgoff] - offset);
5625cabbc6bSPrashanth Sreenivasa 
5635cabbc6bSPrashanth Sreenivasa 			DTRACE_PROBE4(remove__free__inflight,
5645cabbc6bSPrashanth Sreenivasa 			    spa_t *, spa,
5655cabbc6bSPrashanth Sreenivasa 			    uint64_t, offset,
5665cabbc6bSPrashanth Sreenivasa 			    uint64_t, inflight_size,
5675cabbc6bSPrashanth Sreenivasa 			    uint64_t, txg + i);
5685cabbc6bSPrashanth Sreenivasa 
5695cabbc6bSPrashanth Sreenivasa 			/*
5705cabbc6bSPrashanth Sreenivasa 			 * We copy data in order of increasing offset.
5715cabbc6bSPrashanth Sreenivasa 			 * Therefore the max_offset_to_sync[] must increase
5725cabbc6bSPrashanth Sreenivasa 			 * (or be zero, indicating that nothing is being
5735cabbc6bSPrashanth Sreenivasa 			 * copied in that txg).
5745cabbc6bSPrashanth Sreenivasa 			 */
5755cabbc6bSPrashanth Sreenivasa 			if (svr->svr_max_offset_to_sync[txgoff] != 0) {
5765cabbc6bSPrashanth Sreenivasa 				ASSERT3U(svr->svr_max_offset_to_sync[txgoff],
5775cabbc6bSPrashanth Sreenivasa 				    >=, max_offset_yet);
5785cabbc6bSPrashanth Sreenivasa 				max_offset_yet =
5795cabbc6bSPrashanth Sreenivasa 				    svr->svr_max_offset_to_sync[txgoff];
5805cabbc6bSPrashanth Sreenivasa 			}
5815cabbc6bSPrashanth Sreenivasa 
5825cabbc6bSPrashanth Sreenivasa 			/*
5835cabbc6bSPrashanth Sreenivasa 			 * We've already committed to copying this segment:
5845cabbc6bSPrashanth Sreenivasa 			 * we have allocated space elsewhere in the pool for
5855cabbc6bSPrashanth Sreenivasa 			 * it and have an IO outstanding to copy the data. We
5865cabbc6bSPrashanth Sreenivasa 			 * cannot free the space before the copy has
5875cabbc6bSPrashanth Sreenivasa 			 * completed, or else the copy IO might overwrite any
5885cabbc6bSPrashanth Sreenivasa 			 * new data. To free that space, we record the
5895cabbc6bSPrashanth Sreenivasa 			 * segment in the appropriate svr_frees tree and free
5905cabbc6bSPrashanth Sreenivasa 			 * the mapped space later, in the txg where we have
5915cabbc6bSPrashanth Sreenivasa 			 * completed the copy and synced the mapping (see
5925cabbc6bSPrashanth Sreenivasa 			 * vdev_mapping_sync).
5935cabbc6bSPrashanth Sreenivasa 			 */
5945cabbc6bSPrashanth Sreenivasa 			range_tree_add(svr->svr_frees[txgoff],
5955cabbc6bSPrashanth Sreenivasa 			    offset, inflight_size);
5965cabbc6bSPrashanth Sreenivasa 			size -= inflight_size;
5975cabbc6bSPrashanth Sreenivasa 			offset += inflight_size;
5985cabbc6bSPrashanth Sreenivasa 
5995cabbc6bSPrashanth Sreenivasa 			/*
6005cabbc6bSPrashanth Sreenivasa 			 * This space is already accounted for as being
6015cabbc6bSPrashanth Sreenivasa 			 * done, because it is being copied in txg+i.
6025cabbc6bSPrashanth Sreenivasa 			 * However, if i!=0, then it is being copied in
6035cabbc6bSPrashanth Sreenivasa 			 * a future txg.  If we crash after this txg
6045cabbc6bSPrashanth Sreenivasa 			 * syncs but before txg+i syncs, then the space
6055cabbc6bSPrashanth Sreenivasa 			 * will be free.  Therefore we must account
6065cabbc6bSPrashanth Sreenivasa 			 * for the space being done in *this* txg
6075cabbc6bSPrashanth Sreenivasa 			 * (when it is freed) rather than the future txg
6085cabbc6bSPrashanth Sreenivasa 			 * (when it will be copied).
6095cabbc6bSPrashanth Sreenivasa 			 */
6105cabbc6bSPrashanth Sreenivasa 			ASSERT3U(svr->svr_bytes_done[txgoff], >=,
6115cabbc6bSPrashanth Sreenivasa 			    inflight_size);
6125cabbc6bSPrashanth Sreenivasa 			svr->svr_bytes_done[txgoff] -= inflight_size;
6135cabbc6bSPrashanth Sreenivasa 			svr->svr_bytes_done[txg & TXG_MASK] += inflight_size;
6145cabbc6bSPrashanth Sreenivasa 		}
6155cabbc6bSPrashanth Sreenivasa 	}
6165cabbc6bSPrashanth Sreenivasa 	ASSERT0(svr->svr_max_offset_to_sync[TXG_CLEAN(txg) & TXG_MASK]);
6175cabbc6bSPrashanth Sreenivasa 
6185cabbc6bSPrashanth Sreenivasa 	if (size > 0) {
6195cabbc6bSPrashanth Sreenivasa 		/*
6205cabbc6bSPrashanth Sreenivasa 		 * The copy thread has not yet visited this offset.  Ensure
6215cabbc6bSPrashanth Sreenivasa 		 * that it doesn't.
6225cabbc6bSPrashanth Sreenivasa 		 */
6235cabbc6bSPrashanth Sreenivasa 
6245cabbc6bSPrashanth Sreenivasa 		DTRACE_PROBE3(remove__free__unvisited,
6255cabbc6bSPrashanth Sreenivasa 		    spa_t *, spa,
6265cabbc6bSPrashanth Sreenivasa 		    uint64_t, offset,
6275cabbc6bSPrashanth Sreenivasa 		    uint64_t, size);
6285cabbc6bSPrashanth Sreenivasa 
6295cabbc6bSPrashanth Sreenivasa 		if (svr->svr_allocd_segs != NULL)
6305cabbc6bSPrashanth Sreenivasa 			range_tree_clear(svr->svr_allocd_segs, offset, size);
6315cabbc6bSPrashanth Sreenivasa 
6325cabbc6bSPrashanth Sreenivasa 		/*
6335cabbc6bSPrashanth Sreenivasa 		 * Since we now do not need to copy this data, for
6345cabbc6bSPrashanth Sreenivasa 		 * accounting purposes we have done our job and can count
6355cabbc6bSPrashanth Sreenivasa 		 * it as completed.
6365cabbc6bSPrashanth Sreenivasa 		 */
6375cabbc6bSPrashanth Sreenivasa 		svr->svr_bytes_done[txg & TXG_MASK] += size;
6385cabbc6bSPrashanth Sreenivasa 	}
6395cabbc6bSPrashanth Sreenivasa 	mutex_exit(&svr->svr_lock);
6405cabbc6bSPrashanth Sreenivasa 
6415cabbc6bSPrashanth Sreenivasa 	/*
6425cabbc6bSPrashanth Sreenivasa 	 * Now that we have dropped svr_lock, process the synced portion
6435cabbc6bSPrashanth Sreenivasa 	 * of this free.
6445cabbc6bSPrashanth Sreenivasa 	 */
6455cabbc6bSPrashanth Sreenivasa 	if (synced_size > 0) {
64686714001SSerapheim Dimitropoulos 		vdev_indirect_mark_obsolete(vd, synced_offset, synced_size);
64786714001SSerapheim Dimitropoulos 
6485cabbc6bSPrashanth Sreenivasa 		/*
6495cabbc6bSPrashanth Sreenivasa 		 * Note: this can only be called from syncing context,
6505cabbc6bSPrashanth Sreenivasa 		 * and the vdev_indirect_mapping is only changed from the
6515cabbc6bSPrashanth Sreenivasa 		 * sync thread, so we don't need svr_lock while doing
6525cabbc6bSPrashanth Sreenivasa 		 * metaslab_free_impl_cb.
6535cabbc6bSPrashanth Sreenivasa 		 */
65486714001SSerapheim Dimitropoulos 		boolean_t checkpoint = B_FALSE;
6555cabbc6bSPrashanth Sreenivasa 		vdev_indirect_ops.vdev_op_remap(vd, synced_offset, synced_size,
65686714001SSerapheim Dimitropoulos 		    metaslab_free_impl_cb, &checkpoint);
6575cabbc6bSPrashanth Sreenivasa 	}
6585cabbc6bSPrashanth Sreenivasa }
6595cabbc6bSPrashanth Sreenivasa 
6605cabbc6bSPrashanth Sreenivasa /*
6615cabbc6bSPrashanth Sreenivasa  * Stop an active removal and update the spa_removing phys.
6625cabbc6bSPrashanth Sreenivasa  */
6635cabbc6bSPrashanth Sreenivasa static void
6645cabbc6bSPrashanth Sreenivasa spa_finish_removal(spa_t *spa, dsl_scan_state_t state, dmu_tx_t *tx)
6655cabbc6bSPrashanth Sreenivasa {
6665cabbc6bSPrashanth Sreenivasa 	spa_vdev_removal_t *svr = spa->spa_vdev_removal;
6675cabbc6bSPrashanth Sreenivasa 	ASSERT3U(dmu_tx_get_txg(tx), ==, spa_syncing_txg(spa));
6685cabbc6bSPrashanth Sreenivasa 
6695cabbc6bSPrashanth Sreenivasa 	/* Ensure the removal thread has completed before we free the svr. */
6705cabbc6bSPrashanth Sreenivasa 	spa_vdev_remove_suspend(spa);
6715cabbc6bSPrashanth Sreenivasa 
6725cabbc6bSPrashanth Sreenivasa 	ASSERT(state == DSS_FINISHED || state == DSS_CANCELED);
6735cabbc6bSPrashanth Sreenivasa 
6745cabbc6bSPrashanth Sreenivasa 	if (state == DSS_FINISHED) {
6755cabbc6bSPrashanth Sreenivasa 		spa_removing_phys_t *srp = &spa->spa_removing_phys;
6763a4b1be9SMatthew Ahrens 		vdev_t *vd = vdev_lookup_top(spa, svr->svr_vdev_id);
6775cabbc6bSPrashanth Sreenivasa 		vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
6785cabbc6bSPrashanth Sreenivasa 
6795cabbc6bSPrashanth Sreenivasa 		if (srp->sr_prev_indirect_vdev != UINT64_MAX) {
6805cabbc6bSPrashanth Sreenivasa 			vdev_t *pvd = vdev_lookup_top(spa,
6815cabbc6bSPrashanth Sreenivasa 			    srp->sr_prev_indirect_vdev);
6825cabbc6bSPrashanth Sreenivasa 			ASSERT3P(pvd->vdev_ops, ==, &vdev_indirect_ops);
6835cabbc6bSPrashanth Sreenivasa 		}
6845cabbc6bSPrashanth Sreenivasa 
6855cabbc6bSPrashanth Sreenivasa 		vic->vic_prev_indirect_vdev = srp->sr_prev_indirect_vdev;
6865cabbc6bSPrashanth Sreenivasa 		srp->sr_prev_indirect_vdev = vd->vdev_id;
6875cabbc6bSPrashanth Sreenivasa 	}
6885cabbc6bSPrashanth Sreenivasa 	spa->spa_removing_phys.sr_state = state;
6895cabbc6bSPrashanth Sreenivasa 	spa->spa_removing_phys.sr_end_time = gethrestime_sec();
6905cabbc6bSPrashanth Sreenivasa 
6915cabbc6bSPrashanth Sreenivasa 	spa->spa_vdev_removal = NULL;
6925cabbc6bSPrashanth Sreenivasa 	spa_vdev_removal_destroy(svr);
6935cabbc6bSPrashanth Sreenivasa 
6945cabbc6bSPrashanth Sreenivasa 	spa_sync_removing_state(spa, tx);
6955cabbc6bSPrashanth Sreenivasa 
6965cabbc6bSPrashanth Sreenivasa 	vdev_config_dirty(spa->spa_root_vdev);
6975cabbc6bSPrashanth Sreenivasa }
6985cabbc6bSPrashanth Sreenivasa 
6995cabbc6bSPrashanth Sreenivasa static void
7005cabbc6bSPrashanth Sreenivasa free_mapped_segment_cb(void *arg, uint64_t offset, uint64_t size)
7015cabbc6bSPrashanth Sreenivasa {
7025cabbc6bSPrashanth Sreenivasa 	vdev_t *vd = arg;
70386714001SSerapheim Dimitropoulos 	vdev_indirect_mark_obsolete(vd, offset, size);
70486714001SSerapheim Dimitropoulos 	boolean_t checkpoint = B_FALSE;
7055cabbc6bSPrashanth Sreenivasa 	vdev_indirect_ops.vdev_op_remap(vd, offset, size,
70686714001SSerapheim Dimitropoulos 	    metaslab_free_impl_cb, &checkpoint);
7075cabbc6bSPrashanth Sreenivasa }
7085cabbc6bSPrashanth Sreenivasa 
7095cabbc6bSPrashanth Sreenivasa /*
7105cabbc6bSPrashanth Sreenivasa  * On behalf of the removal thread, syncs an incremental bit more of
7115cabbc6bSPrashanth Sreenivasa  * the indirect mapping to disk and updates the in-memory mapping.
7125cabbc6bSPrashanth Sreenivasa  * Called as a sync task in every txg that the removal thread makes progress.
7135cabbc6bSPrashanth Sreenivasa  */
7145cabbc6bSPrashanth Sreenivasa static void
7155cabbc6bSPrashanth Sreenivasa vdev_mapping_sync(void *arg, dmu_tx_t *tx)
7165cabbc6bSPrashanth Sreenivasa {
7175cabbc6bSPrashanth Sreenivasa 	spa_vdev_removal_t *svr = arg;
7185cabbc6bSPrashanth Sreenivasa 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
7193a4b1be9SMatthew Ahrens 	vdev_t *vd = vdev_lookup_top(spa, svr->svr_vdev_id);
7205cabbc6bSPrashanth Sreenivasa 	vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
7215cabbc6bSPrashanth Sreenivasa 	uint64_t txg = dmu_tx_get_txg(tx);
7225cabbc6bSPrashanth Sreenivasa 	vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
7235cabbc6bSPrashanth Sreenivasa 
7245cabbc6bSPrashanth Sreenivasa 	ASSERT(vic->vic_mapping_object != 0);
7255cabbc6bSPrashanth Sreenivasa 	ASSERT3U(txg, ==, spa_syncing_txg(spa));
7265cabbc6bSPrashanth Sreenivasa 
7275cabbc6bSPrashanth Sreenivasa 	vdev_indirect_mapping_add_entries(vim,
7285cabbc6bSPrashanth Sreenivasa 	    &svr->svr_new_segments[txg & TXG_MASK], tx);
7295cabbc6bSPrashanth Sreenivasa 	vdev_indirect_births_add_entry(vd->vdev_indirect_births,
7305cabbc6bSPrashanth Sreenivasa 	    vdev_indirect_mapping_max_offset(vim), dmu_tx_get_txg(tx), tx);
7315cabbc6bSPrashanth Sreenivasa 
7325cabbc6bSPrashanth Sreenivasa 	/*
7335cabbc6bSPrashanth Sreenivasa 	 * Free the copied data for anything that was freed while the
7345cabbc6bSPrashanth Sreenivasa 	 * mapping entries were in flight.
7355cabbc6bSPrashanth Sreenivasa 	 */
7365cabbc6bSPrashanth Sreenivasa 	mutex_enter(&svr->svr_lock);
7375cabbc6bSPrashanth Sreenivasa 	range_tree_vacate(svr->svr_frees[txg & TXG_MASK],
7385cabbc6bSPrashanth Sreenivasa 	    free_mapped_segment_cb, vd);
7395cabbc6bSPrashanth Sreenivasa 	ASSERT3U(svr->svr_max_offset_to_sync[txg & TXG_MASK], >=,
7405cabbc6bSPrashanth Sreenivasa 	    vdev_indirect_mapping_max_offset(vim));
7415cabbc6bSPrashanth Sreenivasa 	svr->svr_max_offset_to_sync[txg & TXG_MASK] = 0;
7425cabbc6bSPrashanth Sreenivasa 	mutex_exit(&svr->svr_lock);
7435cabbc6bSPrashanth Sreenivasa 
7445cabbc6bSPrashanth Sreenivasa 	spa_sync_removing_state(spa, tx);
7455cabbc6bSPrashanth Sreenivasa }
7465cabbc6bSPrashanth Sreenivasa 
747*cfd63e1bSMatthew Ahrens typedef struct vdev_copy_segment_arg {
748*cfd63e1bSMatthew Ahrens 	spa_t *vcsa_spa;
749*cfd63e1bSMatthew Ahrens 	dva_t *vcsa_dest_dva;
750*cfd63e1bSMatthew Ahrens 	uint64_t vcsa_txg;
751*cfd63e1bSMatthew Ahrens 	range_tree_t *vcsa_obsolete_segs;
752*cfd63e1bSMatthew Ahrens } vdev_copy_segment_arg_t;
753*cfd63e1bSMatthew Ahrens 
754*cfd63e1bSMatthew Ahrens static void
755*cfd63e1bSMatthew Ahrens unalloc_seg(void *arg, uint64_t start, uint64_t size)
756*cfd63e1bSMatthew Ahrens {
757*cfd63e1bSMatthew Ahrens 	vdev_copy_segment_arg_t *vcsa = arg;
758*cfd63e1bSMatthew Ahrens 	spa_t *spa = vcsa->vcsa_spa;
759*cfd63e1bSMatthew Ahrens 	blkptr_t bp = { 0 };
760*cfd63e1bSMatthew Ahrens 
761*cfd63e1bSMatthew Ahrens 	BP_SET_BIRTH(&bp, TXG_INITIAL, TXG_INITIAL);
762*cfd63e1bSMatthew Ahrens 	BP_SET_LSIZE(&bp, size);
763*cfd63e1bSMatthew Ahrens 	BP_SET_PSIZE(&bp, size);
764*cfd63e1bSMatthew Ahrens 	BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
765*cfd63e1bSMatthew Ahrens 	BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_OFF);
766*cfd63e1bSMatthew Ahrens 	BP_SET_TYPE(&bp, DMU_OT_NONE);
767*cfd63e1bSMatthew Ahrens 	BP_SET_LEVEL(&bp, 0);
768*cfd63e1bSMatthew Ahrens 	BP_SET_DEDUP(&bp, 0);
769*cfd63e1bSMatthew Ahrens 	BP_SET_BYTEORDER(&bp, ZFS_HOST_BYTEORDER);
770*cfd63e1bSMatthew Ahrens 
771*cfd63e1bSMatthew Ahrens 	DVA_SET_VDEV(&bp.blk_dva[0], DVA_GET_VDEV(vcsa->vcsa_dest_dva));
772*cfd63e1bSMatthew Ahrens 	DVA_SET_OFFSET(&bp.blk_dva[0],
773*cfd63e1bSMatthew Ahrens 	    DVA_GET_OFFSET(vcsa->vcsa_dest_dva) + start);
774*cfd63e1bSMatthew Ahrens 	DVA_SET_ASIZE(&bp.blk_dva[0], size);
775*cfd63e1bSMatthew Ahrens 
776*cfd63e1bSMatthew Ahrens 	zio_free(spa, vcsa->vcsa_txg, &bp);
777*cfd63e1bSMatthew Ahrens }
778*cfd63e1bSMatthew Ahrens 
7793a4b1be9SMatthew Ahrens /*
7803a4b1be9SMatthew Ahrens  * All reads and writes associated with a call to spa_vdev_copy_segment()
7813a4b1be9SMatthew Ahrens  * are done.
7823a4b1be9SMatthew Ahrens  */
7833a4b1be9SMatthew Ahrens static void
784*cfd63e1bSMatthew Ahrens spa_vdev_copy_segment_done(zio_t *zio)
7853a4b1be9SMatthew Ahrens {
786*cfd63e1bSMatthew Ahrens 	vdev_copy_segment_arg_t *vcsa = zio->io_private;
787*cfd63e1bSMatthew Ahrens 
788*cfd63e1bSMatthew Ahrens 	range_tree_vacate(vcsa->vcsa_obsolete_segs,
789*cfd63e1bSMatthew Ahrens 	    unalloc_seg, vcsa);
790*cfd63e1bSMatthew Ahrens 	range_tree_destroy(vcsa->vcsa_obsolete_segs);
791*cfd63e1bSMatthew Ahrens 	kmem_free(vcsa, sizeof (*vcsa));
792*cfd63e1bSMatthew Ahrens 
7933a4b1be9SMatthew Ahrens 	spa_config_exit(zio->io_spa, SCL_STATE, zio->io_spa);
7943a4b1be9SMatthew Ahrens }
7953a4b1be9SMatthew Ahrens 
7963a4b1be9SMatthew Ahrens /*
7973a4b1be9SMatthew Ahrens  * The write of the new location is done.
7983a4b1be9SMatthew Ahrens  */
7995cabbc6bSPrashanth Sreenivasa static void
8005cabbc6bSPrashanth Sreenivasa spa_vdev_copy_segment_write_done(zio_t *zio)
8015cabbc6bSPrashanth Sreenivasa {
8023a4b1be9SMatthew Ahrens 	vdev_copy_arg_t *vca = zio->io_private;
8033a4b1be9SMatthew Ahrens 
8045cabbc6bSPrashanth Sreenivasa 	abd_free(zio->io_abd);
8055cabbc6bSPrashanth Sreenivasa 
8065cabbc6bSPrashanth Sreenivasa 	mutex_enter(&vca->vca_lock);
8075cabbc6bSPrashanth Sreenivasa 	vca->vca_outstanding_bytes -= zio->io_size;
8085cabbc6bSPrashanth Sreenivasa 	cv_signal(&vca->vca_cv);
8095cabbc6bSPrashanth Sreenivasa 	mutex_exit(&vca->vca_lock);
8105cabbc6bSPrashanth Sreenivasa }
8115cabbc6bSPrashanth Sreenivasa 
8123a4b1be9SMatthew Ahrens /*
8133a4b1be9SMatthew Ahrens  * The read of the old location is done.  The parent zio is the write to
8143a4b1be9SMatthew Ahrens  * the new location.  Allow it to start.
8153a4b1be9SMatthew Ahrens  */
8165cabbc6bSPrashanth Sreenivasa static void
8175cabbc6bSPrashanth Sreenivasa spa_vdev_copy_segment_read_done(zio_t *zio)
8185cabbc6bSPrashanth Sreenivasa {
8193a4b1be9SMatthew Ahrens 	zio_nowait(zio_unique_parent(zio));
8205cabbc6bSPrashanth Sreenivasa }
8215cabbc6bSPrashanth Sreenivasa 
8223a4b1be9SMatthew Ahrens /*
8233a4b1be9SMatthew Ahrens  * If the old and new vdevs are mirrors, we will read both sides of the old
8243a4b1be9SMatthew Ahrens  * mirror, and write each copy to the corresponding side of the new mirror.
8253a4b1be9SMatthew Ahrens  * If the old and new vdevs have a different number of children, we will do
8263a4b1be9SMatthew Ahrens  * this as best as possible.  Since we aren't verifying checksums, this
8273a4b1be9SMatthew Ahrens  * ensures that as long as there's a good copy of the data, we'll have a
8283a4b1be9SMatthew Ahrens  * good copy after the removal, even if there's silent damage to one side
8293a4b1be9SMatthew Ahrens  * of the mirror. If we're removing a mirror that has some silent damage,
8303a4b1be9SMatthew Ahrens  * we'll have exactly the same damage in the new location (assuming that
8313a4b1be9SMatthew Ahrens  * the new location is also a mirror).
8323a4b1be9SMatthew Ahrens  *
8333a4b1be9SMatthew Ahrens  * We accomplish this by creating a tree of zio_t's, with as many writes as
8343a4b1be9SMatthew Ahrens  * there are "children" of the new vdev (a non-redundant vdev counts as one
8353a4b1be9SMatthew Ahrens  * child, a 2-way mirror has 2 children, etc). Each write has an associated
8363a4b1be9SMatthew Ahrens  * read from a child of the old vdev. Typically there will be the same
8373a4b1be9SMatthew Ahrens  * number of children of the old and new vdevs.  However, if there are more
8383a4b1be9SMatthew Ahrens  * children of the new vdev, some child(ren) of the old vdev will be issued
8393a4b1be9SMatthew Ahrens  * multiple reads.  If there are more children of the old vdev, some copies
8403a4b1be9SMatthew Ahrens  * will be dropped.
8413a4b1be9SMatthew Ahrens  *
8423a4b1be9SMatthew Ahrens  * For example, the tree of zio_t's for a 2-way mirror is:
8433a4b1be9SMatthew Ahrens  *
8443a4b1be9SMatthew Ahrens  *                            null
8453a4b1be9SMatthew Ahrens  *                           /    \
8463a4b1be9SMatthew Ahrens  *    write(new vdev, child 0)      write(new vdev, child 1)
8473a4b1be9SMatthew Ahrens  *      |                             |
8483a4b1be9SMatthew Ahrens  *    read(old vdev, child 0)       read(old vdev, child 1)
8493a4b1be9SMatthew Ahrens  *
8503a4b1be9SMatthew Ahrens  * Child zio's complete before their parents complete.  However, zio's
8513a4b1be9SMatthew Ahrens  * created with zio_vdev_child_io() may be issued before their children
8523a4b1be9SMatthew Ahrens  * complete.  In this case we need to make sure that the children (reads)
8533a4b1be9SMatthew Ahrens  * complete before the parents (writes) are *issued*.  We do this by not
8543a4b1be9SMatthew Ahrens  * calling zio_nowait() on each write until its corresponding read has
8553a4b1be9SMatthew Ahrens  * completed.
8563a4b1be9SMatthew Ahrens  *
8573a4b1be9SMatthew Ahrens  * The spa_config_lock must be held while zio's created by
8583a4b1be9SMatthew Ahrens  * zio_vdev_child_io() are in progress, to ensure that the vdev tree does
8593a4b1be9SMatthew Ahrens  * not change (e.g. due to a concurrent "zpool attach/detach"). The "null"
8603a4b1be9SMatthew Ahrens  * zio is needed to release the spa_config_lock after all the reads and
8613a4b1be9SMatthew Ahrens  * writes complete. (Note that we can't grab the config lock for each read,
8623a4b1be9SMatthew Ahrens  * because it is not reentrant - we could deadlock with a thread waiting
8633a4b1be9SMatthew Ahrens  * for a write lock.)
8643a4b1be9SMatthew Ahrens  */
8653a4b1be9SMatthew Ahrens static void
8663a4b1be9SMatthew Ahrens spa_vdev_copy_one_child(vdev_copy_arg_t *vca, zio_t *nzio,
8673a4b1be9SMatthew Ahrens     vdev_t *source_vd, uint64_t source_offset,
8683a4b1be9SMatthew Ahrens     vdev_t *dest_child_vd, uint64_t dest_offset, int dest_id, uint64_t size)
8693a4b1be9SMatthew Ahrens {
8703a4b1be9SMatthew Ahrens 	ASSERT3U(spa_config_held(nzio->io_spa, SCL_ALL, RW_READER), !=, 0);
8713a4b1be9SMatthew Ahrens 
8723a4b1be9SMatthew Ahrens 	mutex_enter(&vca->vca_lock);
8733a4b1be9SMatthew Ahrens 	vca->vca_outstanding_bytes += size;
8743a4b1be9SMatthew Ahrens 	mutex_exit(&vca->vca_lock);
8753a4b1be9SMatthew Ahrens 
8763a4b1be9SMatthew Ahrens 	abd_t *abd = abd_alloc_for_io(size, B_FALSE);
8773a4b1be9SMatthew Ahrens 
8783a4b1be9SMatthew Ahrens 	vdev_t *source_child_vd;
8793a4b1be9SMatthew Ahrens 	if (source_vd->vdev_ops == &vdev_mirror_ops && dest_id != -1) {
8803a4b1be9SMatthew Ahrens 		/*
8813a4b1be9SMatthew Ahrens 		 * Source and dest are both mirrors.  Copy from the same
8823a4b1be9SMatthew Ahrens 		 * child id as we are copying to (wrapping around if there
8833a4b1be9SMatthew Ahrens 		 * are more dest children than source children).
8843a4b1be9SMatthew Ahrens 		 */
8853a4b1be9SMatthew Ahrens 		source_child_vd =
8863a4b1be9SMatthew Ahrens 		    source_vd->vdev_child[dest_id % source_vd->vdev_children];
8873a4b1be9SMatthew Ahrens 	} else {
8883a4b1be9SMatthew Ahrens 		source_child_vd = source_vd;
8893a4b1be9SMatthew Ahrens 	}
8903a4b1be9SMatthew Ahrens 
8913a4b1be9SMatthew Ahrens 	zio_t *write_zio = zio_vdev_child_io(nzio, NULL,
8923a4b1be9SMatthew Ahrens 	    dest_child_vd, dest_offset, abd, size,
8933a4b1be9SMatthew Ahrens 	    ZIO_TYPE_WRITE, ZIO_PRIORITY_REMOVAL,
8943a4b1be9SMatthew Ahrens 	    ZIO_FLAG_CANFAIL,
8953a4b1be9SMatthew Ahrens 	    spa_vdev_copy_segment_write_done, vca);
8963a4b1be9SMatthew Ahrens 
8973a4b1be9SMatthew Ahrens 	zio_nowait(zio_vdev_child_io(write_zio, NULL,
8983a4b1be9SMatthew Ahrens 	    source_child_vd, source_offset, abd, size,
8993a4b1be9SMatthew Ahrens 	    ZIO_TYPE_READ, ZIO_PRIORITY_REMOVAL,
9003a4b1be9SMatthew Ahrens 	    ZIO_FLAG_CANFAIL,
9013a4b1be9SMatthew Ahrens 	    spa_vdev_copy_segment_read_done, vca));
9023a4b1be9SMatthew Ahrens }
9033a4b1be9SMatthew Ahrens 
9043a4b1be9SMatthew Ahrens /*
9053a4b1be9SMatthew Ahrens  * Allocate a new location for this segment, and create the zio_t's to
9063a4b1be9SMatthew Ahrens  * read from the old location and write to the new location.
9073a4b1be9SMatthew Ahrens  */
9085cabbc6bSPrashanth Sreenivasa static int
909*cfd63e1bSMatthew Ahrens spa_vdev_copy_segment(vdev_t *vd, range_tree_t *segs,
910*cfd63e1bSMatthew Ahrens     uint64_t maxalloc, uint64_t txg,
9115cabbc6bSPrashanth Sreenivasa     vdev_copy_arg_t *vca, zio_alloc_list_t *zal)
9125cabbc6bSPrashanth Sreenivasa {
9135cabbc6bSPrashanth Sreenivasa 	metaslab_group_t *mg = vd->vdev_mg;
9145cabbc6bSPrashanth Sreenivasa 	spa_t *spa = vd->vdev_spa;
9155cabbc6bSPrashanth Sreenivasa 	spa_vdev_removal_t *svr = spa->spa_vdev_removal;
9165cabbc6bSPrashanth Sreenivasa 	vdev_indirect_mapping_entry_t *entry;
9175cabbc6bSPrashanth Sreenivasa 	dva_t dst = { 0 };
918*cfd63e1bSMatthew Ahrens 	uint64_t start = range_tree_min(segs);
9195cabbc6bSPrashanth Sreenivasa 
920*cfd63e1bSMatthew Ahrens 	ASSERT3U(maxalloc, <=, SPA_MAXBLOCKSIZE);
921*cfd63e1bSMatthew Ahrens 
922*cfd63e1bSMatthew Ahrens 	uint64_t size = range_tree_span(segs);
923*cfd63e1bSMatthew Ahrens 	if (range_tree_span(segs) > maxalloc) {
924*cfd63e1bSMatthew Ahrens 		/*
925*cfd63e1bSMatthew Ahrens 		 * We can't allocate all the segments.  Prefer to end
926*cfd63e1bSMatthew Ahrens 		 * the allocation at the end of a segment, thus avoiding
927*cfd63e1bSMatthew Ahrens 		 * additional split blocks.
928*cfd63e1bSMatthew Ahrens 		 */
929*cfd63e1bSMatthew Ahrens 		range_seg_t search;
930*cfd63e1bSMatthew Ahrens 		avl_index_t where;
931*cfd63e1bSMatthew Ahrens 		search.rs_start = start + maxalloc;
932*cfd63e1bSMatthew Ahrens 		search.rs_end = search.rs_start;
933*cfd63e1bSMatthew Ahrens 		range_seg_t *rs = avl_find(&segs->rt_root, &search, &where);
934*cfd63e1bSMatthew Ahrens 		if (rs == NULL) {
935*cfd63e1bSMatthew Ahrens 			rs = avl_nearest(&segs->rt_root, where, AVL_BEFORE);
936*cfd63e1bSMatthew Ahrens 		} else {
937*cfd63e1bSMatthew Ahrens 			rs = AVL_PREV(&segs->rt_root, rs);
938*cfd63e1bSMatthew Ahrens 		}
939*cfd63e1bSMatthew Ahrens 		if (rs != NULL) {
940*cfd63e1bSMatthew Ahrens 			size = rs->rs_end - start;
941*cfd63e1bSMatthew Ahrens 		} else {
942*cfd63e1bSMatthew Ahrens 			/*
943*cfd63e1bSMatthew Ahrens 			 * There are no segments that end before maxalloc.
944*cfd63e1bSMatthew Ahrens 			 * I.e. the first segment is larger than maxalloc,
945*cfd63e1bSMatthew Ahrens 			 * so we must split it.
946*cfd63e1bSMatthew Ahrens 			 */
947*cfd63e1bSMatthew Ahrens 			size = maxalloc;
948*cfd63e1bSMatthew Ahrens 		}
949*cfd63e1bSMatthew Ahrens 	}
950*cfd63e1bSMatthew Ahrens 	ASSERT3U(size, <=, maxalloc);
9515cabbc6bSPrashanth Sreenivasa 
952f78cdc34SPaul Dagnelie 	/*
953f78cdc34SPaul Dagnelie 	 * We use allocator 0 for this I/O because we don't expect device remap
954f78cdc34SPaul Dagnelie 	 * to be the steady state of the system, so parallelizing is not as
955f78cdc34SPaul Dagnelie 	 * critical as it is for other allocation types. We also want to ensure
956f78cdc34SPaul Dagnelie 	 * that the IOs are allocated together as much as possible, to reduce
957f78cdc34SPaul Dagnelie 	 * mapping sizes.
958f78cdc34SPaul Dagnelie 	 */
9595cabbc6bSPrashanth Sreenivasa 	int error = metaslab_alloc_dva(spa, mg->mg_class, size,
960f78cdc34SPaul Dagnelie 	    &dst, 0, NULL, txg, 0, zal, 0);
9615cabbc6bSPrashanth Sreenivasa 	if (error != 0)
9625cabbc6bSPrashanth Sreenivasa 		return (error);
9635cabbc6bSPrashanth Sreenivasa 
964*cfd63e1bSMatthew Ahrens 	/*
965*cfd63e1bSMatthew Ahrens 	 * Determine the ranges that are not actually needed.  Offsets are
966*cfd63e1bSMatthew Ahrens 	 * relative to the start of the range to be copied (i.e. relative to the
967*cfd63e1bSMatthew Ahrens 	 * local variable "start").
968*cfd63e1bSMatthew Ahrens 	 */
969*cfd63e1bSMatthew Ahrens 	range_tree_t *obsolete_segs = range_tree_create(NULL, NULL);
970*cfd63e1bSMatthew Ahrens 
971*cfd63e1bSMatthew Ahrens 	range_seg_t *rs = avl_first(&segs->rt_root);
972*cfd63e1bSMatthew Ahrens 	ASSERT3U(rs->rs_start, ==, start);
973*cfd63e1bSMatthew Ahrens 	uint64_t prev_seg_end = rs->rs_end;
974*cfd63e1bSMatthew Ahrens 	while ((rs = AVL_NEXT(&segs->rt_root, rs)) != NULL) {
975*cfd63e1bSMatthew Ahrens 		if (rs->rs_start >= start + size) {
976*cfd63e1bSMatthew Ahrens 			break;
977*cfd63e1bSMatthew Ahrens 		} else {
978*cfd63e1bSMatthew Ahrens 			range_tree_add(obsolete_segs,
979*cfd63e1bSMatthew Ahrens 			    prev_seg_end - start,
980*cfd63e1bSMatthew Ahrens 			    rs->rs_start - prev_seg_end);
981*cfd63e1bSMatthew Ahrens 		}
982*cfd63e1bSMatthew Ahrens 		prev_seg_end = rs->rs_end;
983*cfd63e1bSMatthew Ahrens 	}
984*cfd63e1bSMatthew Ahrens 	/* We don't end in the middle of an obsolete range */
985*cfd63e1bSMatthew Ahrens 	ASSERT3U(start + size, <=, prev_seg_end);
986*cfd63e1bSMatthew Ahrens 
987*cfd63e1bSMatthew Ahrens 	range_tree_clear(segs, start, size);
988*cfd63e1bSMatthew Ahrens 
9895cabbc6bSPrashanth Sreenivasa 	/*
9905cabbc6bSPrashanth Sreenivasa 	 * We can't have any padding of the allocated size, otherwise we will
9915cabbc6bSPrashanth Sreenivasa 	 * misunderstand what's allocated, and the size of the mapping.
9925cabbc6bSPrashanth Sreenivasa 	 * The caller ensures this will be true by passing in a size that is
9935cabbc6bSPrashanth Sreenivasa 	 * aligned to the worst (highest) ashift in the pool.
9945cabbc6bSPrashanth Sreenivasa 	 */
9955cabbc6bSPrashanth Sreenivasa 	ASSERT3U(DVA_GET_ASIZE(&dst), ==, size);
9965cabbc6bSPrashanth Sreenivasa 
9975cabbc6bSPrashanth Sreenivasa 	entry = kmem_zalloc(sizeof (vdev_indirect_mapping_entry_t), KM_SLEEP);
9985cabbc6bSPrashanth Sreenivasa 	DVA_MAPPING_SET_SRC_OFFSET(&entry->vime_mapping, start);
9995cabbc6bSPrashanth Sreenivasa 	entry->vime_mapping.vimep_dst = dst;
1000*cfd63e1bSMatthew Ahrens 	if (spa_feature_is_enabled(spa, SPA_FEATURE_OBSOLETE_COUNTS)) {
1001*cfd63e1bSMatthew Ahrens 		entry->vime_obsolete_count = range_tree_space(obsolete_segs);
1002*cfd63e1bSMatthew Ahrens 	}
1003*cfd63e1bSMatthew Ahrens 
1004*cfd63e1bSMatthew Ahrens 	vdev_copy_segment_arg_t *vcsa = kmem_zalloc(sizeof (*vcsa), KM_SLEEP);
1005*cfd63e1bSMatthew Ahrens 	vcsa->vcsa_dest_dva = &entry->vime_mapping.vimep_dst;
1006*cfd63e1bSMatthew Ahrens 	vcsa->vcsa_obsolete_segs = obsolete_segs;
1007*cfd63e1bSMatthew Ahrens 	vcsa->vcsa_spa = spa;
1008*cfd63e1bSMatthew Ahrens 	vcsa->vcsa_txg = txg;
10095cabbc6bSPrashanth Sreenivasa 
10105cabbc6bSPrashanth Sreenivasa 	/*
10113a4b1be9SMatthew Ahrens 	 * See comment before spa_vdev_copy_one_child().
10125cabbc6bSPrashanth Sreenivasa 	 */
10133a4b1be9SMatthew Ahrens 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
10143a4b1be9SMatthew Ahrens 	zio_t *nzio = zio_null(spa->spa_txg_zio[txg & TXG_MASK], spa, NULL,
1015*cfd63e1bSMatthew Ahrens 	    spa_vdev_copy_segment_done, vcsa, 0);
10163a4b1be9SMatthew Ahrens 	vdev_t *dest_vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dst));
10173a4b1be9SMatthew Ahrens 	if (dest_vd->vdev_ops == &vdev_mirror_ops) {
10183a4b1be9SMatthew Ahrens 		for (int i = 0; i < dest_vd->vdev_children; i++) {
10193a4b1be9SMatthew Ahrens 			vdev_t *child = dest_vd->vdev_child[i];
10203a4b1be9SMatthew Ahrens 			spa_vdev_copy_one_child(vca, nzio, vd, start,
10213a4b1be9SMatthew Ahrens 			    child, DVA_GET_OFFSET(&dst), i, size);
10223a4b1be9SMatthew Ahrens 		}
10233a4b1be9SMatthew Ahrens 	} else {
10243a4b1be9SMatthew Ahrens 		spa_vdev_copy_one_child(vca, nzio, vd, start,
10253a4b1be9SMatthew Ahrens 		    dest_vd, DVA_GET_OFFSET(&dst), -1, size);
10263a4b1be9SMatthew Ahrens 	}
10273a4b1be9SMatthew Ahrens 	zio_nowait(nzio);
10285cabbc6bSPrashanth Sreenivasa 
10295cabbc6bSPrashanth Sreenivasa 	list_insert_tail(&svr->svr_new_segments[txg & TXG_MASK], entry);
10305cabbc6bSPrashanth Sreenivasa 	ASSERT3U(start + size, <=, vd->vdev_ms_count << vd->vdev_ms_shift);
10315cabbc6bSPrashanth Sreenivasa 	vdev_dirty(vd, 0, NULL, txg);
10325cabbc6bSPrashanth Sreenivasa 
10335cabbc6bSPrashanth Sreenivasa 	return (0);
10345cabbc6bSPrashanth Sreenivasa }
10355cabbc6bSPrashanth Sreenivasa 
10365cabbc6bSPrashanth Sreenivasa /*
10375cabbc6bSPrashanth Sreenivasa  * Complete the removal of a toplevel vdev. This is called as a
10385cabbc6bSPrashanth Sreenivasa  * synctask in the same txg that we will sync out the new config (to the
10395cabbc6bSPrashanth Sreenivasa  * MOS object) which indicates that this vdev is indirect.
10405cabbc6bSPrashanth Sreenivasa  */
10415cabbc6bSPrashanth Sreenivasa static void
10425cabbc6bSPrashanth Sreenivasa vdev_remove_complete_sync(void *arg, dmu_tx_t *tx)
10435cabbc6bSPrashanth Sreenivasa {
10445cabbc6bSPrashanth Sreenivasa 	spa_vdev_removal_t *svr = arg;
10453a4b1be9SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
10463a4b1be9SMatthew Ahrens 	vdev_t *vd = vdev_lookup_top(spa, svr->svr_vdev_id);
10475cabbc6bSPrashanth Sreenivasa 
10485cabbc6bSPrashanth Sreenivasa 	ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops);
10495cabbc6bSPrashanth Sreenivasa 
10505cabbc6bSPrashanth Sreenivasa 	for (int i = 0; i < TXG_SIZE; i++) {
10515cabbc6bSPrashanth Sreenivasa 		ASSERT0(svr->svr_bytes_done[i]);
10525cabbc6bSPrashanth Sreenivasa 	}
10535cabbc6bSPrashanth Sreenivasa 
10545cabbc6bSPrashanth Sreenivasa 	ASSERT3U(spa->spa_removing_phys.sr_copied, ==,
10555cabbc6bSPrashanth Sreenivasa 	    spa->spa_removing_phys.sr_to_copy);
10565cabbc6bSPrashanth Sreenivasa 
10575cabbc6bSPrashanth Sreenivasa 	vdev_destroy_spacemaps(vd, tx);
10585cabbc6bSPrashanth Sreenivasa 
10595cabbc6bSPrashanth Sreenivasa 	/* destroy leaf zaps, if any */
10605cabbc6bSPrashanth Sreenivasa 	ASSERT3P(svr->svr_zaplist, !=, NULL);
10615cabbc6bSPrashanth Sreenivasa 	for (nvpair_t *pair = nvlist_next_nvpair(svr->svr_zaplist, NULL);
10625cabbc6bSPrashanth Sreenivasa 	    pair != NULL;
10635cabbc6bSPrashanth Sreenivasa 	    pair = nvlist_next_nvpair(svr->svr_zaplist, pair)) {
10645cabbc6bSPrashanth Sreenivasa 		vdev_destroy_unlink_zap(vd, fnvpair_value_uint64(pair), tx);
10655cabbc6bSPrashanth Sreenivasa 	}
10665cabbc6bSPrashanth Sreenivasa 	fnvlist_free(svr->svr_zaplist);
10675cabbc6bSPrashanth Sreenivasa 
10685cabbc6bSPrashanth Sreenivasa 	spa_finish_removal(dmu_tx_pool(tx)->dp_spa, DSS_FINISHED, tx);
10695cabbc6bSPrashanth Sreenivasa 	/* vd->vdev_path is not available here */
10705cabbc6bSPrashanth Sreenivasa 	spa_history_log_internal(spa, "vdev remove completed",  tx,
10715cabbc6bSPrashanth Sreenivasa 	    "%s vdev %llu", spa_name(spa), vd->vdev_id);
10725cabbc6bSPrashanth Sreenivasa }
10735cabbc6bSPrashanth Sreenivasa 
10745cabbc6bSPrashanth Sreenivasa static void
10755cabbc6bSPrashanth Sreenivasa vdev_remove_enlist_zaps(vdev_t *vd, nvlist_t *zlist)
10765cabbc6bSPrashanth Sreenivasa {
10775cabbc6bSPrashanth Sreenivasa 	ASSERT3P(zlist, !=, NULL);
10785cabbc6bSPrashanth Sreenivasa 	ASSERT3P(vd->vdev_ops, !=, &vdev_raidz_ops);
10795cabbc6bSPrashanth Sreenivasa 
10805cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_leaf_zap != 0) {
10815cabbc6bSPrashanth Sreenivasa 		char zkey[32];
10825cabbc6bSPrashanth Sreenivasa 		(void) snprintf(zkey, sizeof (zkey), "%s-%"PRIu64,
10835cabbc6bSPrashanth Sreenivasa 		    VDEV_REMOVAL_ZAP_OBJS, vd->vdev_leaf_zap);
10845cabbc6bSPrashanth Sreenivasa 		fnvlist_add_uint64(zlist, zkey, vd->vdev_leaf_zap);
10855cabbc6bSPrashanth Sreenivasa 	}
10865cabbc6bSPrashanth Sreenivasa 
10875cabbc6bSPrashanth Sreenivasa 	for (uint64_t id = 0; id < vd->vdev_children; id++) {
10885cabbc6bSPrashanth Sreenivasa 		vdev_remove_enlist_zaps(vd->vdev_child[id], zlist);
10895cabbc6bSPrashanth Sreenivasa 	}
10905cabbc6bSPrashanth Sreenivasa }
10915cabbc6bSPrashanth Sreenivasa 
10925cabbc6bSPrashanth Sreenivasa static void
10935cabbc6bSPrashanth Sreenivasa vdev_remove_replace_with_indirect(vdev_t *vd, uint64_t txg)
10945cabbc6bSPrashanth Sreenivasa {
10955cabbc6bSPrashanth Sreenivasa 	vdev_t *ivd;
10965cabbc6bSPrashanth Sreenivasa 	dmu_tx_t *tx;
10975cabbc6bSPrashanth Sreenivasa 	spa_t *spa = vd->vdev_spa;
10985cabbc6bSPrashanth Sreenivasa 	spa_vdev_removal_t *svr = spa->spa_vdev_removal;
10995cabbc6bSPrashanth Sreenivasa 
11005cabbc6bSPrashanth Sreenivasa 	/*
11015cabbc6bSPrashanth Sreenivasa 	 * First, build a list of leaf zaps to be destroyed.
11025cabbc6bSPrashanth Sreenivasa 	 * This is passed to the sync context thread,
11035cabbc6bSPrashanth Sreenivasa 	 * which does the actual unlinking.
11045cabbc6bSPrashanth Sreenivasa 	 */
11055cabbc6bSPrashanth Sreenivasa 	svr->svr_zaplist = fnvlist_alloc();
11065cabbc6bSPrashanth Sreenivasa 	vdev_remove_enlist_zaps(vd, svr->svr_zaplist);
11075cabbc6bSPrashanth Sreenivasa 
11085cabbc6bSPrashanth Sreenivasa 	ivd = vdev_add_parent(vd, &vdev_indirect_ops);
11093a4b1be9SMatthew Ahrens 	ivd->vdev_removing = 0;
11105cabbc6bSPrashanth Sreenivasa 
11115cabbc6bSPrashanth Sreenivasa 	vd->vdev_leaf_zap = 0;
11125cabbc6bSPrashanth Sreenivasa 
11135cabbc6bSPrashanth Sreenivasa 	vdev_remove_child(ivd, vd);
11145cabbc6bSPrashanth Sreenivasa 	vdev_compact_children(ivd);
11155cabbc6bSPrashanth Sreenivasa 
11165cabbc6bSPrashanth Sreenivasa 	ASSERT(!list_link_active(&vd->vdev_state_dirty_node));
11175cabbc6bSPrashanth Sreenivasa 
11185cabbc6bSPrashanth Sreenivasa 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
11195cabbc6bSPrashanth Sreenivasa 	dsl_sync_task_nowait(spa->spa_dsl_pool, vdev_remove_complete_sync, svr,
11205cabbc6bSPrashanth Sreenivasa 	    0, ZFS_SPACE_CHECK_NONE, tx);
11215cabbc6bSPrashanth Sreenivasa 	dmu_tx_commit(tx);
11225cabbc6bSPrashanth Sreenivasa 
11235cabbc6bSPrashanth Sreenivasa 	/*
11245cabbc6bSPrashanth Sreenivasa 	 * Indicate that this thread has exited.
11255cabbc6bSPrashanth Sreenivasa 	 * After this, we can not use svr.
11265cabbc6bSPrashanth Sreenivasa 	 */
11275cabbc6bSPrashanth Sreenivasa 	mutex_enter(&svr->svr_lock);
11285cabbc6bSPrashanth Sreenivasa 	svr->svr_thread = NULL;
11295cabbc6bSPrashanth Sreenivasa 	cv_broadcast(&svr->svr_cv);
11305cabbc6bSPrashanth Sreenivasa 	mutex_exit(&svr->svr_lock);
11315cabbc6bSPrashanth Sreenivasa }
11325cabbc6bSPrashanth Sreenivasa 
11335cabbc6bSPrashanth Sreenivasa /*
11345cabbc6bSPrashanth Sreenivasa  * Complete the removal of a toplevel vdev. This is called in open
11355cabbc6bSPrashanth Sreenivasa  * context by the removal thread after we have copied all vdev's data.
11365cabbc6bSPrashanth Sreenivasa  */
11375cabbc6bSPrashanth Sreenivasa static void
11383a4b1be9SMatthew Ahrens vdev_remove_complete(spa_t *spa)
11395cabbc6bSPrashanth Sreenivasa {
11405cabbc6bSPrashanth Sreenivasa 	uint64_t txg;
11415cabbc6bSPrashanth Sreenivasa 
11425cabbc6bSPrashanth Sreenivasa 	/*
11435cabbc6bSPrashanth Sreenivasa 	 * Wait for any deferred frees to be synced before we call
11445cabbc6bSPrashanth Sreenivasa 	 * vdev_metaslab_fini()
11455cabbc6bSPrashanth Sreenivasa 	 */
11465cabbc6bSPrashanth Sreenivasa 	txg_wait_synced(spa->spa_dsl_pool, 0);
11475cabbc6bSPrashanth Sreenivasa 	txg = spa_vdev_enter(spa);
11483a4b1be9SMatthew Ahrens 	vdev_t *vd = vdev_lookup_top(spa, spa->spa_vdev_removal->svr_vdev_id);
1149094e47e9SGeorge Wilson 	ASSERT3P(vd->vdev_initialize_thread, ==, NULL);
11503a4b1be9SMatthew Ahrens 
11513a4b1be9SMatthew Ahrens 	sysevent_t *ev = spa_event_create(spa, vd, NULL,
11523a4b1be9SMatthew Ahrens 	    ESC_ZFS_VDEV_REMOVE_DEV);
11533a4b1be9SMatthew Ahrens 
11545cabbc6bSPrashanth Sreenivasa 	zfs_dbgmsg("finishing device removal for vdev %llu in txg %llu",
11555cabbc6bSPrashanth Sreenivasa 	    vd->vdev_id, txg);
11565cabbc6bSPrashanth Sreenivasa 
11575cabbc6bSPrashanth Sreenivasa 	/*
11585cabbc6bSPrashanth Sreenivasa 	 * Discard allocation state.
11595cabbc6bSPrashanth Sreenivasa 	 */
11605cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_mg != NULL) {
11615cabbc6bSPrashanth Sreenivasa 		vdev_metaslab_fini(vd);
11625cabbc6bSPrashanth Sreenivasa 		metaslab_group_destroy(vd->vdev_mg);
11635cabbc6bSPrashanth Sreenivasa 		vd->vdev_mg = NULL;
11645cabbc6bSPrashanth Sreenivasa 	}
11655cabbc6bSPrashanth Sreenivasa 	ASSERT0(vd->vdev_stat.vs_space);
11665cabbc6bSPrashanth Sreenivasa 	ASSERT0(vd->vdev_stat.vs_dspace);
11675cabbc6bSPrashanth Sreenivasa 
11685cabbc6bSPrashanth Sreenivasa 	vdev_remove_replace_with_indirect(vd, txg);
11695cabbc6bSPrashanth Sreenivasa 
11705cabbc6bSPrashanth Sreenivasa 	/*
11715cabbc6bSPrashanth Sreenivasa 	 * We now release the locks, allowing spa_sync to run and finish the
11725cabbc6bSPrashanth Sreenivasa 	 * removal via vdev_remove_complete_sync in syncing context.
11733a4b1be9SMatthew Ahrens 	 *
11743a4b1be9SMatthew Ahrens 	 * Note that we hold on to the vdev_t that has been replaced.  Since
11753a4b1be9SMatthew Ahrens 	 * it isn't part of the vdev tree any longer, it can't be concurrently
11763a4b1be9SMatthew Ahrens 	 * manipulated, even while we don't have the config lock.
11775cabbc6bSPrashanth Sreenivasa 	 */
11785cabbc6bSPrashanth Sreenivasa 	(void) spa_vdev_exit(spa, NULL, txg, 0);
11795cabbc6bSPrashanth Sreenivasa 
11805cabbc6bSPrashanth Sreenivasa 	/*
11815cabbc6bSPrashanth Sreenivasa 	 * Top ZAP should have been transferred to the indirect vdev in
11825cabbc6bSPrashanth Sreenivasa 	 * vdev_remove_replace_with_indirect.
11835cabbc6bSPrashanth Sreenivasa 	 */
11845cabbc6bSPrashanth Sreenivasa 	ASSERT0(vd->vdev_top_zap);
11855cabbc6bSPrashanth Sreenivasa 
11865cabbc6bSPrashanth Sreenivasa 	/*
11875cabbc6bSPrashanth Sreenivasa 	 * Leaf ZAP should have been moved in vdev_remove_replace_with_indirect.
11885cabbc6bSPrashanth Sreenivasa 	 */
11895cabbc6bSPrashanth Sreenivasa 	ASSERT0(vd->vdev_leaf_zap);
11905cabbc6bSPrashanth Sreenivasa 
11915cabbc6bSPrashanth Sreenivasa 	txg = spa_vdev_enter(spa);
11925cabbc6bSPrashanth Sreenivasa 	(void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
11935cabbc6bSPrashanth Sreenivasa 	/*
11945cabbc6bSPrashanth Sreenivasa 	 * Request to update the config and the config cachefile.
11955cabbc6bSPrashanth Sreenivasa 	 */
11965cabbc6bSPrashanth Sreenivasa 	vdev_config_dirty(spa->spa_root_vdev);
11975cabbc6bSPrashanth Sreenivasa 	(void) spa_vdev_exit(spa, vd, txg, 0);
11983a4b1be9SMatthew Ahrens 
11993a4b1be9SMatthew Ahrens 	spa_event_post(ev);
12005cabbc6bSPrashanth Sreenivasa }
12015cabbc6bSPrashanth Sreenivasa 
12025cabbc6bSPrashanth Sreenivasa /*
12035cabbc6bSPrashanth Sreenivasa  * Evacuates a segment of size at most max_alloc from the vdev
12045cabbc6bSPrashanth Sreenivasa  * via repeated calls to spa_vdev_copy_segment. If an allocation
12055cabbc6bSPrashanth Sreenivasa  * fails, the pool is probably too fragmented to handle such a
12065cabbc6bSPrashanth Sreenivasa  * large size, so decrease max_alloc so that the caller will not try
12075cabbc6bSPrashanth Sreenivasa  * this size again this txg.
12085cabbc6bSPrashanth Sreenivasa  */
12095cabbc6bSPrashanth Sreenivasa static void
12103a4b1be9SMatthew Ahrens spa_vdev_copy_impl(vdev_t *vd, spa_vdev_removal_t *svr, vdev_copy_arg_t *vca,
12115cabbc6bSPrashanth Sreenivasa     uint64_t *max_alloc, dmu_tx_t *tx)
12125cabbc6bSPrashanth Sreenivasa {
12135cabbc6bSPrashanth Sreenivasa 	uint64_t txg = dmu_tx_get_txg(tx);
12145cabbc6bSPrashanth Sreenivasa 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
12155cabbc6bSPrashanth Sreenivasa 
12165cabbc6bSPrashanth Sreenivasa 	mutex_enter(&svr->svr_lock);
12175cabbc6bSPrashanth Sreenivasa 
1218*cfd63e1bSMatthew Ahrens 	/*
1219*cfd63e1bSMatthew Ahrens 	 * Determine how big of a chunk to copy.  We can allocate up
1220*cfd63e1bSMatthew Ahrens 	 * to max_alloc bytes, and we can span up to vdev_removal_max_span
1221*cfd63e1bSMatthew Ahrens 	 * bytes of unallocated space at a time.  "segs" will track the
1222*cfd63e1bSMatthew Ahrens 	 * allocated segments that we are copying.  We may also be copying
1223*cfd63e1bSMatthew Ahrens 	 * free segments (of up to vdev_removal_max_span bytes).
1224*cfd63e1bSMatthew Ahrens 	 */
1225*cfd63e1bSMatthew Ahrens 	range_tree_t *segs = range_tree_create(NULL, NULL);
1226*cfd63e1bSMatthew Ahrens 	for (;;) {
1227*cfd63e1bSMatthew Ahrens 		range_seg_t *rs = avl_first(&svr->svr_allocd_segs->rt_root);
1228*cfd63e1bSMatthew Ahrens 		if (rs == NULL)
1229*cfd63e1bSMatthew Ahrens 			break;
1230*cfd63e1bSMatthew Ahrens 
1231*cfd63e1bSMatthew Ahrens 		uint64_t seg_length;
1232*cfd63e1bSMatthew Ahrens 
1233*cfd63e1bSMatthew Ahrens 		if (range_tree_is_empty(segs)) {
1234*cfd63e1bSMatthew Ahrens 			/* need to truncate the first seg based on max_alloc */
1235*cfd63e1bSMatthew Ahrens 			seg_length =
1236*cfd63e1bSMatthew Ahrens 			    MIN(rs->rs_end - rs->rs_start, *max_alloc);
1237*cfd63e1bSMatthew Ahrens 		} else {
1238*cfd63e1bSMatthew Ahrens 			if (rs->rs_start - range_tree_max(segs) >
1239*cfd63e1bSMatthew Ahrens 			    vdev_removal_max_span) {
1240*cfd63e1bSMatthew Ahrens 				/*
1241*cfd63e1bSMatthew Ahrens 				 * Including this segment would cause us to
1242*cfd63e1bSMatthew Ahrens 				 * copy a larger unneeded chunk than is allowed.
1243*cfd63e1bSMatthew Ahrens 				 */
1244*cfd63e1bSMatthew Ahrens 				break;
1245*cfd63e1bSMatthew Ahrens 			} else if (rs->rs_end - range_tree_min(segs) >
1246*cfd63e1bSMatthew Ahrens 			    *max_alloc) {
1247*cfd63e1bSMatthew Ahrens 				/*
1248*cfd63e1bSMatthew Ahrens 				 * This additional segment would extend past
1249*cfd63e1bSMatthew Ahrens 				 * max_alloc. Rather than splitting this
1250*cfd63e1bSMatthew Ahrens 				 * segment, leave it for the next mapping.
1251*cfd63e1bSMatthew Ahrens 				 */
1252*cfd63e1bSMatthew Ahrens 				break;
1253*cfd63e1bSMatthew Ahrens 			} else {
1254*cfd63e1bSMatthew Ahrens 				seg_length = rs->rs_end - rs->rs_start;
1255*cfd63e1bSMatthew Ahrens 			}
1256*cfd63e1bSMatthew Ahrens 		}
1257*cfd63e1bSMatthew Ahrens 
1258*cfd63e1bSMatthew Ahrens 		range_tree_add(segs, rs->rs_start, seg_length);
1259*cfd63e1bSMatthew Ahrens 		range_tree_remove(svr->svr_allocd_segs,
1260*cfd63e1bSMatthew Ahrens 		    rs->rs_start, seg_length);
1261*cfd63e1bSMatthew Ahrens 	}
1262*cfd63e1bSMatthew Ahrens 
1263*cfd63e1bSMatthew Ahrens 	if (range_tree_is_empty(segs)) {
12645cabbc6bSPrashanth Sreenivasa 		mutex_exit(&svr->svr_lock);
1265*cfd63e1bSMatthew Ahrens 		range_tree_destroy(segs);
12665cabbc6bSPrashanth Sreenivasa 		return;
12675cabbc6bSPrashanth Sreenivasa 	}
12685cabbc6bSPrashanth Sreenivasa 
12695cabbc6bSPrashanth Sreenivasa 	if (svr->svr_max_offset_to_sync[txg & TXG_MASK] == 0) {
12705cabbc6bSPrashanth Sreenivasa 		dsl_sync_task_nowait(dmu_tx_pool(tx), vdev_mapping_sync,
12715cabbc6bSPrashanth Sreenivasa 		    svr, 0, ZFS_SPACE_CHECK_NONE, tx);
12725cabbc6bSPrashanth Sreenivasa 	}
12735cabbc6bSPrashanth Sreenivasa 
1274*cfd63e1bSMatthew Ahrens 	svr->svr_max_offset_to_sync[txg & TXG_MASK] = range_tree_max(segs);
12755cabbc6bSPrashanth Sreenivasa 
12765cabbc6bSPrashanth Sreenivasa 	/*
12775cabbc6bSPrashanth Sreenivasa 	 * Note: this is the amount of *allocated* space
12785cabbc6bSPrashanth Sreenivasa 	 * that we are taking care of each txg.
12795cabbc6bSPrashanth Sreenivasa 	 */
1280*cfd63e1bSMatthew Ahrens 	svr->svr_bytes_done[txg & TXG_MASK] += range_tree_space(segs);
12815cabbc6bSPrashanth Sreenivasa 
12825cabbc6bSPrashanth Sreenivasa 	mutex_exit(&svr->svr_lock);
12835cabbc6bSPrashanth Sreenivasa 
12845cabbc6bSPrashanth Sreenivasa 	zio_alloc_list_t zal;
12855cabbc6bSPrashanth Sreenivasa 	metaslab_trace_init(&zal);
1286*cfd63e1bSMatthew Ahrens 	uint64_t thismax = SPA_MAXBLOCKSIZE;
1287*cfd63e1bSMatthew Ahrens 	while (!range_tree_is_empty(segs)) {
12883a4b1be9SMatthew Ahrens 		int error = spa_vdev_copy_segment(vd,
1289*cfd63e1bSMatthew Ahrens 		    segs, thismax, txg, vca, &zal);
12905cabbc6bSPrashanth Sreenivasa 
12915cabbc6bSPrashanth Sreenivasa 		if (error == ENOSPC) {
12925cabbc6bSPrashanth Sreenivasa 			/*
12935cabbc6bSPrashanth Sreenivasa 			 * Cut our segment in half, and don't try this
12945cabbc6bSPrashanth Sreenivasa 			 * segment size again this txg.  Note that the
12955cabbc6bSPrashanth Sreenivasa 			 * allocation size must be aligned to the highest
12965cabbc6bSPrashanth Sreenivasa 			 * ashift in the pool, so that the allocation will
12975cabbc6bSPrashanth Sreenivasa 			 * not be padded out to a multiple of the ashift,
12985cabbc6bSPrashanth Sreenivasa 			 * which could cause us to think that this mapping
12995cabbc6bSPrashanth Sreenivasa 			 * is larger than we intended.
13005cabbc6bSPrashanth Sreenivasa 			 */
13015cabbc6bSPrashanth Sreenivasa 			ASSERT3U(spa->spa_max_ashift, >=, SPA_MINBLOCKSHIFT);
13025cabbc6bSPrashanth Sreenivasa 			ASSERT3U(spa->spa_max_ashift, ==, spa->spa_min_ashift);
1303*cfd63e1bSMatthew Ahrens 			uint64_t attempted =
1304*cfd63e1bSMatthew Ahrens 			    MIN(range_tree_span(segs), thismax);
1305*cfd63e1bSMatthew Ahrens 			thismax = P2ROUNDUP(attempted / 2,
13065cabbc6bSPrashanth Sreenivasa 			    1 << spa->spa_max_ashift);
13075cabbc6bSPrashanth Sreenivasa 			/*
13085cabbc6bSPrashanth Sreenivasa 			 * The minimum-size allocation can not fail.
13095cabbc6bSPrashanth Sreenivasa 			 */
1310*cfd63e1bSMatthew Ahrens 			ASSERT3U(attempted, >, 1 << spa->spa_max_ashift);
1311*cfd63e1bSMatthew Ahrens 			*max_alloc = attempted - (1 << spa->spa_max_ashift);
13125cabbc6bSPrashanth Sreenivasa 		} else {
13135cabbc6bSPrashanth Sreenivasa 			ASSERT0(error);
13145cabbc6bSPrashanth Sreenivasa 
13155cabbc6bSPrashanth Sreenivasa 			/*
13165cabbc6bSPrashanth Sreenivasa 			 * We've performed an allocation, so reset the
13175cabbc6bSPrashanth Sreenivasa 			 * alloc trace list.
13185cabbc6bSPrashanth Sreenivasa 			 */
13195cabbc6bSPrashanth Sreenivasa 			metaslab_trace_fini(&zal);
13205cabbc6bSPrashanth Sreenivasa 			metaslab_trace_init(&zal);
13215cabbc6bSPrashanth Sreenivasa 		}
13225cabbc6bSPrashanth Sreenivasa 	}
13235cabbc6bSPrashanth Sreenivasa 	metaslab_trace_fini(&zal);
1324*cfd63e1bSMatthew Ahrens 	range_tree_destroy(segs);
13255cabbc6bSPrashanth Sreenivasa }
13265cabbc6bSPrashanth Sreenivasa 
13275cabbc6bSPrashanth Sreenivasa /*
13285cabbc6bSPrashanth Sreenivasa  * The removal thread operates in open context.  It iterates over all
13295cabbc6bSPrashanth Sreenivasa  * allocated space in the vdev, by loading each metaslab's spacemap.
13305cabbc6bSPrashanth Sreenivasa  * For each contiguous segment of allocated space (capping the segment
13315cabbc6bSPrashanth Sreenivasa  * size at SPA_MAXBLOCKSIZE), we:
13325cabbc6bSPrashanth Sreenivasa  *    - Allocate space for it on another vdev.
13335cabbc6bSPrashanth Sreenivasa  *    - Create a new mapping from the old location to the new location
13345cabbc6bSPrashanth Sreenivasa  *      (as a record in svr_new_segments).
13355cabbc6bSPrashanth Sreenivasa  *    - Initiate a logical read zio to get the data off the removing disk.
13365cabbc6bSPrashanth Sreenivasa  *    - In the read zio's done callback, initiate a logical write zio to
13375cabbc6bSPrashanth Sreenivasa  *      write it to the new vdev.
13385cabbc6bSPrashanth Sreenivasa  * Note that all of this will take effect when a particular TXG syncs.
13395cabbc6bSPrashanth Sreenivasa  * The sync thread ensures that all the phys reads and writes for the syncing
13405cabbc6bSPrashanth Sreenivasa  * TXG have completed (see spa_txg_zio) and writes the new mappings to disk
13415cabbc6bSPrashanth Sreenivasa  * (see vdev_mapping_sync()).
13425cabbc6bSPrashanth Sreenivasa  */
13435cabbc6bSPrashanth Sreenivasa static void
13445cabbc6bSPrashanth Sreenivasa spa_vdev_remove_thread(void *arg)
13455cabbc6bSPrashanth Sreenivasa {
13463a4b1be9SMatthew Ahrens 	spa_t *spa = arg;
13475cabbc6bSPrashanth Sreenivasa 	spa_vdev_removal_t *svr = spa->spa_vdev_removal;
13485cabbc6bSPrashanth Sreenivasa 	vdev_copy_arg_t vca;
13495cabbc6bSPrashanth Sreenivasa 	uint64_t max_alloc = zfs_remove_max_segment;
13505cabbc6bSPrashanth Sreenivasa 	uint64_t last_txg = 0;
13513a4b1be9SMatthew Ahrens 
13523a4b1be9SMatthew Ahrens 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
13533a4b1be9SMatthew Ahrens 	vdev_t *vd = vdev_lookup_top(spa, svr->svr_vdev_id);
13545cabbc6bSPrashanth Sreenivasa 	vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
13555cabbc6bSPrashanth Sreenivasa 	uint64_t start_offset = vdev_indirect_mapping_max_offset(vim);
13565cabbc6bSPrashanth Sreenivasa 
13575cabbc6bSPrashanth Sreenivasa 	ASSERT3P(vd->vdev_ops, !=, &vdev_indirect_ops);
13585cabbc6bSPrashanth Sreenivasa 	ASSERT(vdev_is_concrete(vd));
13595cabbc6bSPrashanth Sreenivasa 	ASSERT(vd->vdev_removing);
13605cabbc6bSPrashanth Sreenivasa 	ASSERT(vd->vdev_indirect_config.vic_mapping_object != 0);
13615cabbc6bSPrashanth Sreenivasa 	ASSERT(vim != NULL);
13625cabbc6bSPrashanth Sreenivasa 
13635cabbc6bSPrashanth Sreenivasa 	mutex_init(&vca.vca_lock, NULL, MUTEX_DEFAULT, NULL);
13645cabbc6bSPrashanth Sreenivasa 	cv_init(&vca.vca_cv, NULL, CV_DEFAULT, NULL);
13655cabbc6bSPrashanth Sreenivasa 	vca.vca_outstanding_bytes = 0;
13665cabbc6bSPrashanth Sreenivasa 
13675cabbc6bSPrashanth Sreenivasa 	mutex_enter(&svr->svr_lock);
13685cabbc6bSPrashanth Sreenivasa 
13695cabbc6bSPrashanth Sreenivasa 	/*
13705cabbc6bSPrashanth Sreenivasa 	 * Start from vim_max_offset so we pick up where we left off
13715cabbc6bSPrashanth Sreenivasa 	 * if we are restarting the removal after opening the pool.
13725cabbc6bSPrashanth Sreenivasa 	 */
13735cabbc6bSPrashanth Sreenivasa 	uint64_t msi;
13745cabbc6bSPrashanth Sreenivasa 	for (msi = start_offset >> vd->vdev_ms_shift;
13755cabbc6bSPrashanth Sreenivasa 	    msi < vd->vdev_ms_count && !svr->svr_thread_exit; msi++) {
13765cabbc6bSPrashanth Sreenivasa 		metaslab_t *msp = vd->vdev_ms[msi];
13775cabbc6bSPrashanth Sreenivasa 		ASSERT3U(msi, <=, vd->vdev_ms_count);
13785cabbc6bSPrashanth Sreenivasa 
13795cabbc6bSPrashanth Sreenivasa 		ASSERT0(range_tree_space(svr->svr_allocd_segs));
13805cabbc6bSPrashanth Sreenivasa 
13815cabbc6bSPrashanth Sreenivasa 		mutex_enter(&msp->ms_sync_lock);
13825cabbc6bSPrashanth Sreenivasa 		mutex_enter(&msp->ms_lock);
13835cabbc6bSPrashanth Sreenivasa 
13845cabbc6bSPrashanth Sreenivasa 		/*
13855cabbc6bSPrashanth Sreenivasa 		 * Assert nothing in flight -- ms_*tree is empty.
13865cabbc6bSPrashanth Sreenivasa 		 */
13875cabbc6bSPrashanth Sreenivasa 		for (int i = 0; i < TXG_SIZE; i++) {
138886714001SSerapheim Dimitropoulos 			ASSERT0(range_tree_space(msp->ms_allocating[i]));
13895cabbc6bSPrashanth Sreenivasa 		}
13905cabbc6bSPrashanth Sreenivasa 
13915cabbc6bSPrashanth Sreenivasa 		/*
13925cabbc6bSPrashanth Sreenivasa 		 * If the metaslab has ever been allocated from (ms_sm!=NULL),
13935cabbc6bSPrashanth Sreenivasa 		 * read the allocated segments from the space map object
13945cabbc6bSPrashanth Sreenivasa 		 * into svr_allocd_segs. Since we do this while holding
13955cabbc6bSPrashanth Sreenivasa 		 * svr_lock and ms_sync_lock, concurrent frees (which
13965cabbc6bSPrashanth Sreenivasa 		 * would have modified the space map) will wait for us
13975cabbc6bSPrashanth Sreenivasa 		 * to finish loading the spacemap, and then take the
13985cabbc6bSPrashanth Sreenivasa 		 * appropriate action (see free_from_removing_vdev()).
13995cabbc6bSPrashanth Sreenivasa 		 */
14005cabbc6bSPrashanth Sreenivasa 		if (msp->ms_sm != NULL) {
14015cabbc6bSPrashanth Sreenivasa 			space_map_t *sm = NULL;
14025cabbc6bSPrashanth Sreenivasa 
14035cabbc6bSPrashanth Sreenivasa 			/*
14045cabbc6bSPrashanth Sreenivasa 			 * We have to open a new space map here, because
14055cabbc6bSPrashanth Sreenivasa 			 * ms_sm's sm_length and sm_alloc may not reflect
14065cabbc6bSPrashanth Sreenivasa 			 * what's in the object contents, if we are in between
14075cabbc6bSPrashanth Sreenivasa 			 * metaslab_sync() and metaslab_sync_done().
14085cabbc6bSPrashanth Sreenivasa 			 */
14095cabbc6bSPrashanth Sreenivasa 			VERIFY0(space_map_open(&sm,
14105cabbc6bSPrashanth Sreenivasa 			    spa->spa_dsl_pool->dp_meta_objset,
14115cabbc6bSPrashanth Sreenivasa 			    msp->ms_sm->sm_object, msp->ms_sm->sm_start,
14125cabbc6bSPrashanth Sreenivasa 			    msp->ms_sm->sm_size, msp->ms_sm->sm_shift));
14135cabbc6bSPrashanth Sreenivasa 			space_map_update(sm);
14145cabbc6bSPrashanth Sreenivasa 			VERIFY0(space_map_load(sm, svr->svr_allocd_segs,
14155cabbc6bSPrashanth Sreenivasa 			    SM_ALLOC));
14165cabbc6bSPrashanth Sreenivasa 			space_map_close(sm);
14175cabbc6bSPrashanth Sreenivasa 
141886714001SSerapheim Dimitropoulos 			range_tree_walk(msp->ms_freeing,
14195cabbc6bSPrashanth Sreenivasa 			    range_tree_remove, svr->svr_allocd_segs);
14205cabbc6bSPrashanth Sreenivasa 
14215cabbc6bSPrashanth Sreenivasa 			/*
14225cabbc6bSPrashanth Sreenivasa 			 * When we are resuming from a paused removal (i.e.
14235cabbc6bSPrashanth Sreenivasa 			 * when importing a pool with a removal in progress),
14245cabbc6bSPrashanth Sreenivasa 			 * discard any state that we have already processed.
14255cabbc6bSPrashanth Sreenivasa 			 */
14265cabbc6bSPrashanth Sreenivasa 			range_tree_clear(svr->svr_allocd_segs, 0, start_offset);
14275cabbc6bSPrashanth Sreenivasa 		}
14285cabbc6bSPrashanth Sreenivasa 		mutex_exit(&msp->ms_lock);
14295cabbc6bSPrashanth Sreenivasa 		mutex_exit(&msp->ms_sync_lock);
14305cabbc6bSPrashanth Sreenivasa 
14315cabbc6bSPrashanth Sreenivasa 		vca.vca_msp = msp;
14325cabbc6bSPrashanth Sreenivasa 		zfs_dbgmsg("copying %llu segments for metaslab %llu",
14335cabbc6bSPrashanth Sreenivasa 		    avl_numnodes(&svr->svr_allocd_segs->rt_root),
14345cabbc6bSPrashanth Sreenivasa 		    msp->ms_id);
14355cabbc6bSPrashanth Sreenivasa 
14365cabbc6bSPrashanth Sreenivasa 		while (!svr->svr_thread_exit &&
143786714001SSerapheim Dimitropoulos 		    !range_tree_is_empty(svr->svr_allocd_segs)) {
14385cabbc6bSPrashanth Sreenivasa 
14395cabbc6bSPrashanth Sreenivasa 			mutex_exit(&svr->svr_lock);
14405cabbc6bSPrashanth Sreenivasa 
14413a4b1be9SMatthew Ahrens 			/*
14423a4b1be9SMatthew Ahrens 			 * We need to periodically drop the config lock so that
14433a4b1be9SMatthew Ahrens 			 * writers can get in.  Additionally, we can't wait
14443a4b1be9SMatthew Ahrens 			 * for a txg to sync while holding a config lock
14453a4b1be9SMatthew Ahrens 			 * (since a waiting writer could cause a 3-way deadlock
14463a4b1be9SMatthew Ahrens 			 * with the sync thread, which also gets a config
14473a4b1be9SMatthew Ahrens 			 * lock for reader).  So we can't hold the config lock
14483a4b1be9SMatthew Ahrens 			 * while calling dmu_tx_assign().
14493a4b1be9SMatthew Ahrens 			 */
14503a4b1be9SMatthew Ahrens 			spa_config_exit(spa, SCL_CONFIG, FTAG);
14513a4b1be9SMatthew Ahrens 
145286714001SSerapheim Dimitropoulos 			/*
145386714001SSerapheim Dimitropoulos 			 * This delay will pause the removal around the point
145486714001SSerapheim Dimitropoulos 			 * specified by zfs_remove_max_bytes_pause. We do this
145586714001SSerapheim Dimitropoulos 			 * solely from the test suite or during debugging.
145686714001SSerapheim Dimitropoulos 			 */
145786714001SSerapheim Dimitropoulos 			uint64_t bytes_copied =
145886714001SSerapheim Dimitropoulos 			    spa->spa_removing_phys.sr_copied;
145986714001SSerapheim Dimitropoulos 			for (int i = 0; i < TXG_SIZE; i++)
146086714001SSerapheim Dimitropoulos 				bytes_copied += svr->svr_bytes_done[i];
146186714001SSerapheim Dimitropoulos 			while (zfs_remove_max_bytes_pause <= bytes_copied &&
146286714001SSerapheim Dimitropoulos 			    !svr->svr_thread_exit)
146386714001SSerapheim Dimitropoulos 				delay(hz);
146486714001SSerapheim Dimitropoulos 
14655cabbc6bSPrashanth Sreenivasa 			mutex_enter(&vca.vca_lock);
14665cabbc6bSPrashanth Sreenivasa 			while (vca.vca_outstanding_bytes >
14675cabbc6bSPrashanth Sreenivasa 			    zfs_remove_max_copy_bytes) {
14685cabbc6bSPrashanth Sreenivasa 				cv_wait(&vca.vca_cv, &vca.vca_lock);
14695cabbc6bSPrashanth Sreenivasa 			}
14705cabbc6bSPrashanth Sreenivasa 			mutex_exit(&vca.vca_lock);
14715cabbc6bSPrashanth Sreenivasa 
14725cabbc6bSPrashanth Sreenivasa 			dmu_tx_t *tx =
14735cabbc6bSPrashanth Sreenivasa 			    dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
14745cabbc6bSPrashanth Sreenivasa 
14755cabbc6bSPrashanth Sreenivasa 			VERIFY0(dmu_tx_assign(tx, TXG_WAIT));
14765cabbc6bSPrashanth Sreenivasa 			uint64_t txg = dmu_tx_get_txg(tx);
14775cabbc6bSPrashanth Sreenivasa 
14783a4b1be9SMatthew Ahrens 			/*
14793a4b1be9SMatthew Ahrens 			 * Reacquire the vdev_config lock.  The vdev_t
14803a4b1be9SMatthew Ahrens 			 * that we're removing may have changed, e.g. due
14813a4b1be9SMatthew Ahrens 			 * to a vdev_attach or vdev_detach.
14823a4b1be9SMatthew Ahrens 			 */
14833a4b1be9SMatthew Ahrens 			spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
14843a4b1be9SMatthew Ahrens 			vd = vdev_lookup_top(spa, svr->svr_vdev_id);
14853a4b1be9SMatthew Ahrens 
14865cabbc6bSPrashanth Sreenivasa 			if (txg != last_txg)
14875cabbc6bSPrashanth Sreenivasa 				max_alloc = zfs_remove_max_segment;
14885cabbc6bSPrashanth Sreenivasa 			last_txg = txg;
14895cabbc6bSPrashanth Sreenivasa 
14903a4b1be9SMatthew Ahrens 			spa_vdev_copy_impl(vd, svr, &vca, &max_alloc, tx);
14915cabbc6bSPrashanth Sreenivasa 
14925cabbc6bSPrashanth Sreenivasa 			dmu_tx_commit(tx);
14935cabbc6bSPrashanth Sreenivasa 			mutex_enter(&svr->svr_lock);
14945cabbc6bSPrashanth Sreenivasa 		}
14955cabbc6bSPrashanth Sreenivasa 	}
14965cabbc6bSPrashanth Sreenivasa 
14975cabbc6bSPrashanth Sreenivasa 	mutex_exit(&svr->svr_lock);
14983a4b1be9SMatthew Ahrens 
14993a4b1be9SMatthew Ahrens 	spa_config_exit(spa, SCL_CONFIG, FTAG);
15003a4b1be9SMatthew Ahrens 
15015cabbc6bSPrashanth Sreenivasa 	/*
15025cabbc6bSPrashanth Sreenivasa 	 * Wait for all copies to finish before cleaning up the vca.
15035cabbc6bSPrashanth Sreenivasa 	 */
15045cabbc6bSPrashanth Sreenivasa 	txg_wait_synced(spa->spa_dsl_pool, 0);
15055cabbc6bSPrashanth Sreenivasa 	ASSERT0(vca.vca_outstanding_bytes);
15065cabbc6bSPrashanth Sreenivasa 
15075cabbc6bSPrashanth Sreenivasa 	mutex_destroy(&vca.vca_lock);
15085cabbc6bSPrashanth Sreenivasa 	cv_destroy(&vca.vca_cv);
15095cabbc6bSPrashanth Sreenivasa 
15105cabbc6bSPrashanth Sreenivasa 	if (svr->svr_thread_exit) {
15115cabbc6bSPrashanth Sreenivasa 		mutex_enter(&svr->svr_lock);
15125cabbc6bSPrashanth Sreenivasa 		range_tree_vacate(svr->svr_allocd_segs, NULL, NULL);
15135cabbc6bSPrashanth Sreenivasa 		svr->svr_thread = NULL;
15145cabbc6bSPrashanth Sreenivasa 		cv_broadcast(&svr->svr_cv);
15155cabbc6bSPrashanth Sreenivasa 		mutex_exit(&svr->svr_lock);
15165cabbc6bSPrashanth Sreenivasa 	} else {
15175cabbc6bSPrashanth Sreenivasa 		ASSERT0(range_tree_space(svr->svr_allocd_segs));
15183a4b1be9SMatthew Ahrens 		vdev_remove_complete(spa);
15195cabbc6bSPrashanth Sreenivasa 	}
15205cabbc6bSPrashanth Sreenivasa }
15215cabbc6bSPrashanth Sreenivasa 
15225cabbc6bSPrashanth Sreenivasa void
15235cabbc6bSPrashanth Sreenivasa spa_vdev_remove_suspend(spa_t *spa)
15245cabbc6bSPrashanth Sreenivasa {
15255cabbc6bSPrashanth Sreenivasa 	spa_vdev_removal_t *svr = spa->spa_vdev_removal;
15265cabbc6bSPrashanth Sreenivasa 
15275cabbc6bSPrashanth Sreenivasa 	if (svr == NULL)
15285cabbc6bSPrashanth Sreenivasa 		return;
15295cabbc6bSPrashanth Sreenivasa 
15305cabbc6bSPrashanth Sreenivasa 	mutex_enter(&svr->svr_lock);
15315cabbc6bSPrashanth Sreenivasa 	svr->svr_thread_exit = B_TRUE;
15325cabbc6bSPrashanth Sreenivasa 	while (svr->svr_thread != NULL)
15335cabbc6bSPrashanth Sreenivasa 		cv_wait(&svr->svr_cv, &svr->svr_lock);
15345cabbc6bSPrashanth Sreenivasa 	svr->svr_thread_exit = B_FALSE;
15355cabbc6bSPrashanth Sreenivasa 	mutex_exit(&svr->svr_lock);
15365cabbc6bSPrashanth Sreenivasa }
15375cabbc6bSPrashanth Sreenivasa 
15385cabbc6bSPrashanth Sreenivasa /* ARGSUSED */
15395cabbc6bSPrashanth Sreenivasa static int
15405cabbc6bSPrashanth Sreenivasa spa_vdev_remove_cancel_check(void *arg, dmu_tx_t *tx)
15415cabbc6bSPrashanth Sreenivasa {
15425cabbc6bSPrashanth Sreenivasa 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
15435cabbc6bSPrashanth Sreenivasa 
15445cabbc6bSPrashanth Sreenivasa 	if (spa->spa_vdev_removal == NULL)
15455cabbc6bSPrashanth Sreenivasa 		return (ENOTACTIVE);
15465cabbc6bSPrashanth Sreenivasa 	return (0);
15475cabbc6bSPrashanth Sreenivasa }
15485cabbc6bSPrashanth Sreenivasa 
15495cabbc6bSPrashanth Sreenivasa /*
15505cabbc6bSPrashanth Sreenivasa  * Cancel a removal by freeing all entries from the partial mapping
15515cabbc6bSPrashanth Sreenivasa  * and marking the vdev as no longer being removing.
15525cabbc6bSPrashanth Sreenivasa  */
15535cabbc6bSPrashanth Sreenivasa /* ARGSUSED */
15545cabbc6bSPrashanth Sreenivasa static void
15555cabbc6bSPrashanth Sreenivasa spa_vdev_remove_cancel_sync(void *arg, dmu_tx_t *tx)
15565cabbc6bSPrashanth Sreenivasa {
15575cabbc6bSPrashanth Sreenivasa 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
15585cabbc6bSPrashanth Sreenivasa 	spa_vdev_removal_t *svr = spa->spa_vdev_removal;
15593a4b1be9SMatthew Ahrens 	vdev_t *vd = vdev_lookup_top(spa, svr->svr_vdev_id);
15605cabbc6bSPrashanth Sreenivasa 	vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
15615cabbc6bSPrashanth Sreenivasa 	vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
15625cabbc6bSPrashanth Sreenivasa 	objset_t *mos = spa->spa_meta_objset;
15635cabbc6bSPrashanth Sreenivasa 
15645cabbc6bSPrashanth Sreenivasa 	ASSERT3P(svr->svr_thread, ==, NULL);
15655cabbc6bSPrashanth Sreenivasa 
15665cabbc6bSPrashanth Sreenivasa 	spa_feature_decr(spa, SPA_FEATURE_DEVICE_REMOVAL, tx);
15675cabbc6bSPrashanth Sreenivasa 	if (vdev_obsolete_counts_are_precise(vd)) {
15685cabbc6bSPrashanth Sreenivasa 		spa_feature_decr(spa, SPA_FEATURE_OBSOLETE_COUNTS, tx);
15695cabbc6bSPrashanth Sreenivasa 		VERIFY0(zap_remove(spa->spa_meta_objset, vd->vdev_top_zap,
15705cabbc6bSPrashanth Sreenivasa 		    VDEV_TOP_ZAP_OBSOLETE_COUNTS_ARE_PRECISE, tx));
15715cabbc6bSPrashanth Sreenivasa 	}
15725cabbc6bSPrashanth Sreenivasa 
15735cabbc6bSPrashanth Sreenivasa 	if (vdev_obsolete_sm_object(vd) != 0) {
15745cabbc6bSPrashanth Sreenivasa 		ASSERT(vd->vdev_obsolete_sm != NULL);
15755cabbc6bSPrashanth Sreenivasa 		ASSERT3U(vdev_obsolete_sm_object(vd), ==,
15765cabbc6bSPrashanth Sreenivasa 		    space_map_object(vd->vdev_obsolete_sm));
15775cabbc6bSPrashanth Sreenivasa 
15785cabbc6bSPrashanth Sreenivasa 		space_map_free(vd->vdev_obsolete_sm, tx);
15795cabbc6bSPrashanth Sreenivasa 		VERIFY0(zap_remove(spa->spa_meta_objset, vd->vdev_top_zap,
15805cabbc6bSPrashanth Sreenivasa 		    VDEV_TOP_ZAP_INDIRECT_OBSOLETE_SM, tx));
15815cabbc6bSPrashanth Sreenivasa 		space_map_close(vd->vdev_obsolete_sm);
15825cabbc6bSPrashanth Sreenivasa 		vd->vdev_obsolete_sm = NULL;
15835cabbc6bSPrashanth Sreenivasa 		spa_feature_decr(spa, SPA_FEATURE_OBSOLETE_COUNTS, tx);
15845cabbc6bSPrashanth Sreenivasa 	}
15855cabbc6bSPrashanth Sreenivasa 	for (int i = 0; i < TXG_SIZE; i++) {
15865cabbc6bSPrashanth Sreenivasa 		ASSERT(list_is_empty(&svr->svr_new_segments[i]));
15875cabbc6bSPrashanth Sreenivasa 		ASSERT3U(svr->svr_max_offset_to_sync[i], <=,
15885cabbc6bSPrashanth Sreenivasa 		    vdev_indirect_mapping_max_offset(vim));
15895cabbc6bSPrashanth Sreenivasa 	}
15905cabbc6bSPrashanth Sreenivasa 
15915cabbc6bSPrashanth Sreenivasa 	for (uint64_t msi = 0; msi < vd->vdev_ms_count; msi++) {
15925cabbc6bSPrashanth Sreenivasa 		metaslab_t *msp = vd->vdev_ms[msi];
15935cabbc6bSPrashanth Sreenivasa 
15945cabbc6bSPrashanth Sreenivasa 		if (msp->ms_start >= vdev_indirect_mapping_max_offset(vim))
15955cabbc6bSPrashanth Sreenivasa 			break;
15965cabbc6bSPrashanth Sreenivasa 
15975cabbc6bSPrashanth Sreenivasa 		ASSERT0(range_tree_space(svr->svr_allocd_segs));
15985cabbc6bSPrashanth Sreenivasa 
15995cabbc6bSPrashanth Sreenivasa 		mutex_enter(&msp->ms_lock);
16005cabbc6bSPrashanth Sreenivasa 
16015cabbc6bSPrashanth Sreenivasa 		/*
16025cabbc6bSPrashanth Sreenivasa 		 * Assert nothing in flight -- ms_*tree is empty.
16035cabbc6bSPrashanth Sreenivasa 		 */
16045cabbc6bSPrashanth Sreenivasa 		for (int i = 0; i < TXG_SIZE; i++)
160586714001SSerapheim Dimitropoulos 			ASSERT0(range_tree_space(msp->ms_allocating[i]));
16065cabbc6bSPrashanth Sreenivasa 		for (int i = 0; i < TXG_DEFER_SIZE; i++)
160786714001SSerapheim Dimitropoulos 			ASSERT0(range_tree_space(msp->ms_defer[i]));
160886714001SSerapheim Dimitropoulos 		ASSERT0(range_tree_space(msp->ms_freed));
16095cabbc6bSPrashanth Sreenivasa 
16105cabbc6bSPrashanth Sreenivasa 		if (msp->ms_sm != NULL) {
16115cabbc6bSPrashanth Sreenivasa 			/*
16125cabbc6bSPrashanth Sreenivasa 			 * Assert that the in-core spacemap has the same
16135cabbc6bSPrashanth Sreenivasa 			 * length as the on-disk one, so we can use the
16145cabbc6bSPrashanth Sreenivasa 			 * existing in-core spacemap to load it from disk.
16155cabbc6bSPrashanth Sreenivasa 			 */
16165cabbc6bSPrashanth Sreenivasa 			ASSERT3U(msp->ms_sm->sm_alloc, ==,
16175cabbc6bSPrashanth Sreenivasa 			    msp->ms_sm->sm_phys->smp_alloc);
16185cabbc6bSPrashanth Sreenivasa 			ASSERT3U(msp->ms_sm->sm_length, ==,
16195cabbc6bSPrashanth Sreenivasa 			    msp->ms_sm->sm_phys->smp_objsize);
16205cabbc6bSPrashanth Sreenivasa 
16215cabbc6bSPrashanth Sreenivasa 			mutex_enter(&svr->svr_lock);
16225cabbc6bSPrashanth Sreenivasa 			VERIFY0(space_map_load(msp->ms_sm,
16235cabbc6bSPrashanth Sreenivasa 			    svr->svr_allocd_segs, SM_ALLOC));
162486714001SSerapheim Dimitropoulos 			range_tree_walk(msp->ms_freeing,
16255cabbc6bSPrashanth Sreenivasa 			    range_tree_remove, svr->svr_allocd_segs);
16265cabbc6bSPrashanth Sreenivasa 
16275cabbc6bSPrashanth Sreenivasa 			/*
16285cabbc6bSPrashanth Sreenivasa 			 * Clear everything past what has been synced,
16295cabbc6bSPrashanth Sreenivasa 			 * because we have not allocated mappings for it yet.
16305cabbc6bSPrashanth Sreenivasa 			 */
16315cabbc6bSPrashanth Sreenivasa 			uint64_t syncd = vdev_indirect_mapping_max_offset(vim);
16323a4b1be9SMatthew Ahrens 			uint64_t sm_end = msp->ms_sm->sm_start +
16333a4b1be9SMatthew Ahrens 			    msp->ms_sm->sm_size;
16343a4b1be9SMatthew Ahrens 			if (sm_end > syncd)
16353a4b1be9SMatthew Ahrens 				range_tree_clear(svr->svr_allocd_segs,
16363a4b1be9SMatthew Ahrens 				    syncd, sm_end - syncd);
16375cabbc6bSPrashanth Sreenivasa 
16385cabbc6bSPrashanth Sreenivasa 			mutex_exit(&svr->svr_lock);
16395cabbc6bSPrashanth Sreenivasa 		}
16405cabbc6bSPrashanth Sreenivasa 		mutex_exit(&msp->ms_lock);
16415cabbc6bSPrashanth Sreenivasa 
16425cabbc6bSPrashanth Sreenivasa 		mutex_enter(&svr->svr_lock);
16435cabbc6bSPrashanth Sreenivasa 		range_tree_vacate(svr->svr_allocd_segs,
16445cabbc6bSPrashanth Sreenivasa 		    free_mapped_segment_cb, vd);
16455cabbc6bSPrashanth Sreenivasa 		mutex_exit(&svr->svr_lock);
16465cabbc6bSPrashanth Sreenivasa 	}
16475cabbc6bSPrashanth Sreenivasa 
16485cabbc6bSPrashanth Sreenivasa 	/*
16495cabbc6bSPrashanth Sreenivasa 	 * Note: this must happen after we invoke free_mapped_segment_cb,
16505cabbc6bSPrashanth Sreenivasa 	 * because it adds to the obsolete_segments.
16515cabbc6bSPrashanth Sreenivasa 	 */
16525cabbc6bSPrashanth Sreenivasa 	range_tree_vacate(vd->vdev_obsolete_segments, NULL, NULL);
16535cabbc6bSPrashanth Sreenivasa 
16545cabbc6bSPrashanth Sreenivasa 	ASSERT3U(vic->vic_mapping_object, ==,
16555cabbc6bSPrashanth Sreenivasa 	    vdev_indirect_mapping_object(vd->vdev_indirect_mapping));
16565cabbc6bSPrashanth Sreenivasa 	vdev_indirect_mapping_close(vd->vdev_indirect_mapping);
16575cabbc6bSPrashanth Sreenivasa 	vd->vdev_indirect_mapping = NULL;
16585cabbc6bSPrashanth Sreenivasa 	vdev_indirect_mapping_free(mos, vic->vic_mapping_object, tx);
16595cabbc6bSPrashanth Sreenivasa 	vic->vic_mapping_object = 0;
16605cabbc6bSPrashanth Sreenivasa 
16615cabbc6bSPrashanth Sreenivasa 	ASSERT3U(vic->vic_births_object, ==,
16625cabbc6bSPrashanth Sreenivasa 	    vdev_indirect_births_object(vd->vdev_indirect_births));
16635cabbc6bSPrashanth Sreenivasa 	vdev_indirect_births_close(vd->vdev_indirect_births);
16645cabbc6bSPrashanth Sreenivasa 	vd->vdev_indirect_births = NULL;
16655cabbc6bSPrashanth Sreenivasa 	vdev_indirect_births_free(mos, vic->vic_births_object, tx);
16665cabbc6bSPrashanth Sreenivasa 	vic->vic_births_object = 0;
16675cabbc6bSPrashanth Sreenivasa 
16685cabbc6bSPrashanth Sreenivasa 	/*
16695cabbc6bSPrashanth Sreenivasa 	 * We may have processed some frees from the removing vdev in this
16705cabbc6bSPrashanth Sreenivasa 	 * txg, thus increasing svr_bytes_done; discard that here to
16715cabbc6bSPrashanth Sreenivasa 	 * satisfy the assertions in spa_vdev_removal_destroy().
16725cabbc6bSPrashanth Sreenivasa 	 * Note that future txg's can not have any bytes_done, because
16735cabbc6bSPrashanth Sreenivasa 	 * future TXG's are only modified from open context, and we have
16745cabbc6bSPrashanth Sreenivasa 	 * already shut down the copying thread.
16755cabbc6bSPrashanth Sreenivasa 	 */
16765cabbc6bSPrashanth Sreenivasa 	svr->svr_bytes_done[dmu_tx_get_txg(tx) & TXG_MASK] = 0;
16775cabbc6bSPrashanth Sreenivasa 	spa_finish_removal(spa, DSS_CANCELED, tx);
16785cabbc6bSPrashanth Sreenivasa 
16795cabbc6bSPrashanth Sreenivasa 	vd->vdev_removing = B_FALSE;
16805cabbc6bSPrashanth Sreenivasa 	vdev_config_dirty(vd);
16815cabbc6bSPrashanth Sreenivasa 
16825cabbc6bSPrashanth Sreenivasa 	zfs_dbgmsg("canceled device removal for vdev %llu in %llu",
16835cabbc6bSPrashanth Sreenivasa 	    vd->vdev_id, dmu_tx_get_txg(tx));
16845cabbc6bSPrashanth Sreenivasa 	spa_history_log_internal(spa, "vdev remove canceled", tx,
16855cabbc6bSPrashanth Sreenivasa 	    "%s vdev %llu %s", spa_name(spa),
16865cabbc6bSPrashanth Sreenivasa 	    vd->vdev_id, (vd->vdev_path != NULL) ? vd->vdev_path : "-");
16875cabbc6bSPrashanth Sreenivasa }
16885cabbc6bSPrashanth Sreenivasa 
16895cabbc6bSPrashanth Sreenivasa int
16905cabbc6bSPrashanth Sreenivasa spa_vdev_remove_cancel(spa_t *spa)
16915cabbc6bSPrashanth Sreenivasa {
16925cabbc6bSPrashanth Sreenivasa 	spa_vdev_remove_suspend(spa);
16935cabbc6bSPrashanth Sreenivasa 
16945cabbc6bSPrashanth Sreenivasa 	if (spa->spa_vdev_removal == NULL)
16955cabbc6bSPrashanth Sreenivasa 		return (ENOTACTIVE);
16965cabbc6bSPrashanth Sreenivasa 
16973a4b1be9SMatthew Ahrens 	uint64_t vdid = spa->spa_vdev_removal->svr_vdev_id;
16985cabbc6bSPrashanth Sreenivasa 
16995cabbc6bSPrashanth Sreenivasa 	int error = dsl_sync_task(spa->spa_name, spa_vdev_remove_cancel_check,
170086714001SSerapheim Dimitropoulos 	    spa_vdev_remove_cancel_sync, NULL, 0,
170186714001SSerapheim Dimitropoulos 	    ZFS_SPACE_CHECK_EXTRA_RESERVED);
17025cabbc6bSPrashanth Sreenivasa 
17035cabbc6bSPrashanth Sreenivasa 	if (error == 0) {
17045cabbc6bSPrashanth Sreenivasa 		spa_config_enter(spa, SCL_ALLOC | SCL_VDEV, FTAG, RW_WRITER);
17055cabbc6bSPrashanth Sreenivasa 		vdev_t *vd = vdev_lookup_top(spa, vdid);
17065cabbc6bSPrashanth Sreenivasa 		metaslab_group_activate(vd->vdev_mg);
17075cabbc6bSPrashanth Sreenivasa 		spa_config_exit(spa, SCL_ALLOC | SCL_VDEV, FTAG);
17085cabbc6bSPrashanth Sreenivasa 	}
17095cabbc6bSPrashanth Sreenivasa 
17105cabbc6bSPrashanth Sreenivasa 	return (error);
17115cabbc6bSPrashanth Sreenivasa }
17125cabbc6bSPrashanth Sreenivasa 
17135cabbc6bSPrashanth Sreenivasa /*
17145cabbc6bSPrashanth Sreenivasa  * Called every sync pass of every txg if there's a svr.
17155cabbc6bSPrashanth Sreenivasa  */
17165cabbc6bSPrashanth Sreenivasa void
17175cabbc6bSPrashanth Sreenivasa svr_sync(spa_t *spa, dmu_tx_t *tx)
17185cabbc6bSPrashanth Sreenivasa {
17195cabbc6bSPrashanth Sreenivasa 	spa_vdev_removal_t *svr = spa->spa_vdev_removal;
17205cabbc6bSPrashanth Sreenivasa 	int txgoff = dmu_tx_get_txg(tx) & TXG_MASK;
17215cabbc6bSPrashanth Sreenivasa 
17225cabbc6bSPrashanth Sreenivasa 	/*
17235cabbc6bSPrashanth Sreenivasa 	 * This check is necessary so that we do not dirty the
17245cabbc6bSPrashanth Sreenivasa 	 * DIRECTORY_OBJECT via spa_sync_removing_state() when there
17255cabbc6bSPrashanth Sreenivasa 	 * is nothing to do.  Dirtying it every time would prevent us
17265cabbc6bSPrashanth Sreenivasa 	 * from syncing-to-convergence.
17275cabbc6bSPrashanth Sreenivasa 	 */
17285cabbc6bSPrashanth Sreenivasa 	if (svr->svr_bytes_done[txgoff] == 0)
17295cabbc6bSPrashanth Sreenivasa 		return;
17305cabbc6bSPrashanth Sreenivasa 
17315cabbc6bSPrashanth Sreenivasa 	/*
17325cabbc6bSPrashanth Sreenivasa 	 * Update progress accounting.
17335cabbc6bSPrashanth Sreenivasa 	 */
17345cabbc6bSPrashanth Sreenivasa 	spa->spa_removing_phys.sr_copied += svr->svr_bytes_done[txgoff];
17355cabbc6bSPrashanth Sreenivasa 	svr->svr_bytes_done[txgoff] = 0;
17365cabbc6bSPrashanth Sreenivasa 
17375cabbc6bSPrashanth Sreenivasa 	spa_sync_removing_state(spa, tx);
17385cabbc6bSPrashanth Sreenivasa }
17395cabbc6bSPrashanth Sreenivasa 
17405cabbc6bSPrashanth Sreenivasa static void
17415cabbc6bSPrashanth Sreenivasa vdev_remove_make_hole_and_free(vdev_t *vd)
17425cabbc6bSPrashanth Sreenivasa {
17435cabbc6bSPrashanth Sreenivasa 	uint64_t id = vd->vdev_id;
17445cabbc6bSPrashanth Sreenivasa 	spa_t *spa = vd->vdev_spa;
17455cabbc6bSPrashanth Sreenivasa 	vdev_t *rvd = spa->spa_root_vdev;
17465cabbc6bSPrashanth Sreenivasa 	boolean_t last_vdev = (id == (rvd->vdev_children - 1));
17475cabbc6bSPrashanth Sreenivasa 
17485cabbc6bSPrashanth Sreenivasa 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
17495cabbc6bSPrashanth Sreenivasa 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
17505cabbc6bSPrashanth Sreenivasa 
17515cabbc6bSPrashanth Sreenivasa 	vdev_free(vd);
17525cabbc6bSPrashanth Sreenivasa 
17535cabbc6bSPrashanth Sreenivasa 	if (last_vdev) {
17545cabbc6bSPrashanth Sreenivasa 		vdev_compact_children(rvd);
17555cabbc6bSPrashanth Sreenivasa 	} else {
17565cabbc6bSPrashanth Sreenivasa 		vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
17575cabbc6bSPrashanth Sreenivasa 		vdev_add_child(rvd, vd);
17585cabbc6bSPrashanth Sreenivasa 	}
17595cabbc6bSPrashanth Sreenivasa 	vdev_config_dirty(rvd);
17605cabbc6bSPrashanth Sreenivasa 
17615cabbc6bSPrashanth Sreenivasa 	/*
17625cabbc6bSPrashanth Sreenivasa 	 * Reassess the health of our root vdev.
17635cabbc6bSPrashanth Sreenivasa 	 */
17645cabbc6bSPrashanth Sreenivasa 	vdev_reopen(rvd);
17655cabbc6bSPrashanth Sreenivasa }
17665cabbc6bSPrashanth Sreenivasa 
17675cabbc6bSPrashanth Sreenivasa /*
17685cabbc6bSPrashanth Sreenivasa  * Remove a log device.  The config lock is held for the specified TXG.
17695cabbc6bSPrashanth Sreenivasa  */
17705cabbc6bSPrashanth Sreenivasa static int
17715cabbc6bSPrashanth Sreenivasa spa_vdev_remove_log(vdev_t *vd, uint64_t *txg)
17725cabbc6bSPrashanth Sreenivasa {
17735cabbc6bSPrashanth Sreenivasa 	metaslab_group_t *mg = vd->vdev_mg;
17745cabbc6bSPrashanth Sreenivasa 	spa_t *spa = vd->vdev_spa;
17755cabbc6bSPrashanth Sreenivasa 	int error = 0;
17765cabbc6bSPrashanth Sreenivasa 
17775cabbc6bSPrashanth Sreenivasa 	ASSERT(vd->vdev_islog);
17785cabbc6bSPrashanth Sreenivasa 	ASSERT(vd == vd->vdev_top);
17795cabbc6bSPrashanth Sreenivasa 
17805cabbc6bSPrashanth Sreenivasa 	/*
17815cabbc6bSPrashanth Sreenivasa 	 * Stop allocating from this vdev.
17825cabbc6bSPrashanth Sreenivasa 	 */
17835cabbc6bSPrashanth Sreenivasa 	metaslab_group_passivate(mg);
17845cabbc6bSPrashanth Sreenivasa 
17855cabbc6bSPrashanth Sreenivasa 	/*
17865cabbc6bSPrashanth Sreenivasa 	 * Wait for the youngest allocations and frees to sync,
17875cabbc6bSPrashanth Sreenivasa 	 * and then wait for the deferral of those frees to finish.
17885cabbc6bSPrashanth Sreenivasa 	 */
17895cabbc6bSPrashanth Sreenivasa 	spa_vdev_config_exit(spa, NULL,
17905cabbc6bSPrashanth Sreenivasa 	    *txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
17915cabbc6bSPrashanth Sreenivasa 
17925cabbc6bSPrashanth Sreenivasa 	/*
17935cabbc6bSPrashanth Sreenivasa 	 * Evacuate the device.  We don't hold the config lock as writer
17945cabbc6bSPrashanth Sreenivasa 	 * since we need to do I/O but we do keep the
17955cabbc6bSPrashanth Sreenivasa 	 * spa_namespace_lock held.  Once this completes the device
17965cabbc6bSPrashanth Sreenivasa 	 * should no longer have any blocks allocated on it.
17975cabbc6bSPrashanth Sreenivasa 	 */
17985cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_islog) {
17995cabbc6bSPrashanth Sreenivasa 		if (vd->vdev_stat.vs_alloc != 0)
18005cabbc6bSPrashanth Sreenivasa 			error = spa_reset_logs(spa);
18015cabbc6bSPrashanth Sreenivasa 	}
18025cabbc6bSPrashanth Sreenivasa 
18035cabbc6bSPrashanth Sreenivasa 	*txg = spa_vdev_config_enter(spa);
18045cabbc6bSPrashanth Sreenivasa 
18055cabbc6bSPrashanth Sreenivasa 	if (error != 0) {
18065cabbc6bSPrashanth Sreenivasa 		metaslab_group_activate(mg);
18075cabbc6bSPrashanth Sreenivasa 		return (error);
18085cabbc6bSPrashanth Sreenivasa 	}
18095cabbc6bSPrashanth Sreenivasa 	ASSERT0(vd->vdev_stat.vs_alloc);
18105cabbc6bSPrashanth Sreenivasa 
18115cabbc6bSPrashanth Sreenivasa 	/*
18125cabbc6bSPrashanth Sreenivasa 	 * The evacuation succeeded.  Remove any remaining MOS metadata
18135cabbc6bSPrashanth Sreenivasa 	 * associated with this vdev, and wait for these changes to sync.
18145cabbc6bSPrashanth Sreenivasa 	 */
18155cabbc6bSPrashanth Sreenivasa 	vd->vdev_removing = B_TRUE;
18165cabbc6bSPrashanth Sreenivasa 
18175cabbc6bSPrashanth Sreenivasa 	vdev_dirty_leaves(vd, VDD_DTL, *txg);
18185cabbc6bSPrashanth Sreenivasa 	vdev_config_dirty(vd);
18195cabbc6bSPrashanth Sreenivasa 
18205cabbc6bSPrashanth Sreenivasa 	spa_history_log_internal(spa, "vdev remove", NULL,
18215cabbc6bSPrashanth Sreenivasa 	    "%s vdev %llu (log) %s", spa_name(spa), vd->vdev_id,
18225cabbc6bSPrashanth Sreenivasa 	    (vd->vdev_path != NULL) ? vd->vdev_path : "-");
18235cabbc6bSPrashanth Sreenivasa 
18245cabbc6bSPrashanth Sreenivasa 	/* Make sure these changes are sync'ed */
18255cabbc6bSPrashanth Sreenivasa 	spa_vdev_config_exit(spa, NULL, *txg, 0, FTAG);
18265cabbc6bSPrashanth Sreenivasa 
1827094e47e9SGeorge Wilson 	/* Stop initializing */
1828094e47e9SGeorge Wilson 	(void) vdev_initialize_stop_all(vd, VDEV_INITIALIZE_CANCELED);
1829094e47e9SGeorge Wilson 
18305cabbc6bSPrashanth Sreenivasa 	*txg = spa_vdev_config_enter(spa);
18315cabbc6bSPrashanth Sreenivasa 
18325cabbc6bSPrashanth Sreenivasa 	sysevent_t *ev = spa_event_create(spa, vd, NULL,
18335cabbc6bSPrashanth Sreenivasa 	    ESC_ZFS_VDEV_REMOVE_DEV);
18345cabbc6bSPrashanth Sreenivasa 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
18355cabbc6bSPrashanth Sreenivasa 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
18365cabbc6bSPrashanth Sreenivasa 
18375cabbc6bSPrashanth Sreenivasa 	/* The top ZAP should have been destroyed by vdev_remove_empty. */
18385cabbc6bSPrashanth Sreenivasa 	ASSERT0(vd->vdev_top_zap);
18395cabbc6bSPrashanth Sreenivasa 	/* The leaf ZAP should have been destroyed by vdev_dtl_sync. */
18405cabbc6bSPrashanth Sreenivasa 	ASSERT0(vd->vdev_leaf_zap);
18415cabbc6bSPrashanth Sreenivasa 
18425cabbc6bSPrashanth Sreenivasa 	(void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
18435cabbc6bSPrashanth Sreenivasa 
18445cabbc6bSPrashanth Sreenivasa 	if (list_link_active(&vd->vdev_state_dirty_node))
18455cabbc6bSPrashanth Sreenivasa 		vdev_state_clean(vd);
18465cabbc6bSPrashanth Sreenivasa 	if (list_link_active(&vd->vdev_config_dirty_node))
18475cabbc6bSPrashanth Sreenivasa 		vdev_config_clean(vd);
18485cabbc6bSPrashanth Sreenivasa 
18495cabbc6bSPrashanth Sreenivasa 	/*
18505cabbc6bSPrashanth Sreenivasa 	 * Clean up the vdev namespace.
18515cabbc6bSPrashanth Sreenivasa 	 */
18525cabbc6bSPrashanth Sreenivasa 	vdev_remove_make_hole_and_free(vd);
18535cabbc6bSPrashanth Sreenivasa 
18545cabbc6bSPrashanth Sreenivasa 	if (ev != NULL)
18555cabbc6bSPrashanth Sreenivasa 		spa_event_post(ev);
18565cabbc6bSPrashanth Sreenivasa 
18575cabbc6bSPrashanth Sreenivasa 	return (0);
18585cabbc6bSPrashanth Sreenivasa }
18595cabbc6bSPrashanth Sreenivasa 
18605cabbc6bSPrashanth Sreenivasa static int
18615cabbc6bSPrashanth Sreenivasa spa_vdev_remove_top_check(vdev_t *vd)
18625cabbc6bSPrashanth Sreenivasa {
18635cabbc6bSPrashanth Sreenivasa 	spa_t *spa = vd->vdev_spa;
18645cabbc6bSPrashanth Sreenivasa 
18655cabbc6bSPrashanth Sreenivasa 	if (vd != vd->vdev_top)
18665cabbc6bSPrashanth Sreenivasa 		return (SET_ERROR(ENOTSUP));
18675cabbc6bSPrashanth Sreenivasa 
18685cabbc6bSPrashanth Sreenivasa 	if (!spa_feature_is_enabled(spa, SPA_FEATURE_DEVICE_REMOVAL))
18695cabbc6bSPrashanth Sreenivasa 		return (SET_ERROR(ENOTSUP));
18705cabbc6bSPrashanth Sreenivasa 
18715cabbc6bSPrashanth Sreenivasa 	/*
18725cabbc6bSPrashanth Sreenivasa 	 * There has to be enough free space to remove the
18735cabbc6bSPrashanth Sreenivasa 	 * device and leave double the "slop" space (i.e. we
18745cabbc6bSPrashanth Sreenivasa 	 * must leave at least 3% of the pool free, in addition to
18755cabbc6bSPrashanth Sreenivasa 	 * the normal slop space).
18765cabbc6bSPrashanth Sreenivasa 	 */
18775cabbc6bSPrashanth Sreenivasa 	if (dsl_dir_space_available(spa->spa_dsl_pool->dp_root_dir,
18785cabbc6bSPrashanth Sreenivasa 	    NULL, 0, B_TRUE) <
18795cabbc6bSPrashanth Sreenivasa 	    vd->vdev_stat.vs_dspace + spa_get_slop_space(spa)) {
18805cabbc6bSPrashanth Sreenivasa 		return (SET_ERROR(ENOSPC));
18815cabbc6bSPrashanth Sreenivasa 	}
18825cabbc6bSPrashanth Sreenivasa 
18835cabbc6bSPrashanth Sreenivasa 	/*
18845cabbc6bSPrashanth Sreenivasa 	 * There can not be a removal in progress.
18855cabbc6bSPrashanth Sreenivasa 	 */
18865cabbc6bSPrashanth Sreenivasa 	if (spa->spa_removing_phys.sr_state == DSS_SCANNING)
18875cabbc6bSPrashanth Sreenivasa 		return (SET_ERROR(EBUSY));
18885cabbc6bSPrashanth Sreenivasa 
18895cabbc6bSPrashanth Sreenivasa 	/*
18905cabbc6bSPrashanth Sreenivasa 	 * The device must have all its data.
18915cabbc6bSPrashanth Sreenivasa 	 */
18925cabbc6bSPrashanth Sreenivasa 	if (!vdev_dtl_empty(vd, DTL_MISSING) ||
18935cabbc6bSPrashanth Sreenivasa 	    !vdev_dtl_empty(vd, DTL_OUTAGE))
18945cabbc6bSPrashanth Sreenivasa 		return (SET_ERROR(EBUSY));
18955cabbc6bSPrashanth Sreenivasa 
18965cabbc6bSPrashanth Sreenivasa 	/*
18975cabbc6bSPrashanth Sreenivasa 	 * The device must be healthy.
18985cabbc6bSPrashanth Sreenivasa 	 */
18995cabbc6bSPrashanth Sreenivasa 	if (!vdev_readable(vd))
19005cabbc6bSPrashanth Sreenivasa 		return (SET_ERROR(EIO));
19015cabbc6bSPrashanth Sreenivasa 
19025cabbc6bSPrashanth Sreenivasa 	/*
19035cabbc6bSPrashanth Sreenivasa 	 * All vdevs in normal class must have the same ashift.
19045cabbc6bSPrashanth Sreenivasa 	 */
19055cabbc6bSPrashanth Sreenivasa 	if (spa->spa_max_ashift != spa->spa_min_ashift) {
19065cabbc6bSPrashanth Sreenivasa 		return (SET_ERROR(EINVAL));
19075cabbc6bSPrashanth Sreenivasa 	}
19085cabbc6bSPrashanth Sreenivasa 
19095cabbc6bSPrashanth Sreenivasa 	/*
19105cabbc6bSPrashanth Sreenivasa 	 * All vdevs in normal class must have the same ashift
19115cabbc6bSPrashanth Sreenivasa 	 * and not be raidz.
19125cabbc6bSPrashanth Sreenivasa 	 */
19135cabbc6bSPrashanth Sreenivasa 	vdev_t *rvd = spa->spa_root_vdev;
19145cabbc6bSPrashanth Sreenivasa 	int num_indirect = 0;
19155cabbc6bSPrashanth Sreenivasa 	for (uint64_t id = 0; id < rvd->vdev_children; id++) {
19165cabbc6bSPrashanth Sreenivasa 		vdev_t *cvd = rvd->vdev_child[id];
19175cabbc6bSPrashanth Sreenivasa 		if (cvd->vdev_ashift != 0 && !cvd->vdev_islog)
19185cabbc6bSPrashanth Sreenivasa 			ASSERT3U(cvd->vdev_ashift, ==, spa->spa_max_ashift);
19195cabbc6bSPrashanth Sreenivasa 		if (cvd->vdev_ops == &vdev_indirect_ops)
19205cabbc6bSPrashanth Sreenivasa 			num_indirect++;
19215cabbc6bSPrashanth Sreenivasa 		if (!vdev_is_concrete(cvd))
19225cabbc6bSPrashanth Sreenivasa 			continue;
19235cabbc6bSPrashanth Sreenivasa 		if (cvd->vdev_ops == &vdev_raidz_ops)
19245cabbc6bSPrashanth Sreenivasa 			return (SET_ERROR(EINVAL));
19255cabbc6bSPrashanth Sreenivasa 		/*
19265cabbc6bSPrashanth Sreenivasa 		 * Need the mirror to be mirror of leaf vdevs only
19275cabbc6bSPrashanth Sreenivasa 		 */
19285cabbc6bSPrashanth Sreenivasa 		if (cvd->vdev_ops == &vdev_mirror_ops) {
19295cabbc6bSPrashanth Sreenivasa 			for (uint64_t cid = 0;
19305cabbc6bSPrashanth Sreenivasa 			    cid < cvd->vdev_children; cid++) {
19315cabbc6bSPrashanth Sreenivasa 				vdev_t *tmp = cvd->vdev_child[cid];
19325cabbc6bSPrashanth Sreenivasa 				if (!tmp->vdev_ops->vdev_op_leaf)
19335cabbc6bSPrashanth Sreenivasa 					return (SET_ERROR(EINVAL));
19345cabbc6bSPrashanth Sreenivasa 			}
19355cabbc6bSPrashanth Sreenivasa 		}
19365cabbc6bSPrashanth Sreenivasa 	}
19375cabbc6bSPrashanth Sreenivasa 
19385cabbc6bSPrashanth Sreenivasa 	return (0);
19395cabbc6bSPrashanth Sreenivasa }
19405cabbc6bSPrashanth Sreenivasa 
19415cabbc6bSPrashanth Sreenivasa /*
19425cabbc6bSPrashanth Sreenivasa  * Initiate removal of a top-level vdev, reducing the total space in the pool.
19435cabbc6bSPrashanth Sreenivasa  * The config lock is held for the specified TXG.  Once initiated,
19445cabbc6bSPrashanth Sreenivasa  * evacuation of all allocated space (copying it to other vdevs) happens
19455cabbc6bSPrashanth Sreenivasa  * in the background (see spa_vdev_remove_thread()), and can be canceled
19465cabbc6bSPrashanth Sreenivasa  * (see spa_vdev_remove_cancel()).  If successful, the vdev will
19475cabbc6bSPrashanth Sreenivasa  * be transformed to an indirect vdev (see spa_vdev_remove_complete()).
19485cabbc6bSPrashanth Sreenivasa  */
19495cabbc6bSPrashanth Sreenivasa static int
19505cabbc6bSPrashanth Sreenivasa spa_vdev_remove_top(vdev_t *vd, uint64_t *txg)
19515cabbc6bSPrashanth Sreenivasa {
19525cabbc6bSPrashanth Sreenivasa 	spa_t *spa = vd->vdev_spa;
19535cabbc6bSPrashanth Sreenivasa 	int error;
19545cabbc6bSPrashanth Sreenivasa 
19555cabbc6bSPrashanth Sreenivasa 	/*
19565cabbc6bSPrashanth Sreenivasa 	 * Check for errors up-front, so that we don't waste time
19575cabbc6bSPrashanth Sreenivasa 	 * passivating the metaslab group and clearing the ZIL if there
19585cabbc6bSPrashanth Sreenivasa 	 * are errors.
19595cabbc6bSPrashanth Sreenivasa 	 */
19605cabbc6bSPrashanth Sreenivasa 	error = spa_vdev_remove_top_check(vd);
19615cabbc6bSPrashanth Sreenivasa 	if (error != 0)
19625cabbc6bSPrashanth Sreenivasa 		return (error);
19635cabbc6bSPrashanth Sreenivasa 
19645cabbc6bSPrashanth Sreenivasa 	/*
19655cabbc6bSPrashanth Sreenivasa 	 * Stop allocating from this vdev.  Note that we must check
19665cabbc6bSPrashanth Sreenivasa 	 * that this is not the only device in the pool before
19675cabbc6bSPrashanth Sreenivasa 	 * passivating, otherwise we will not be able to make
19685cabbc6bSPrashanth Sreenivasa 	 * progress because we can't allocate from any vdevs.
19695cabbc6bSPrashanth Sreenivasa 	 * The above check for sufficient free space serves this
19705cabbc6bSPrashanth Sreenivasa 	 * purpose.
19715cabbc6bSPrashanth Sreenivasa 	 */
19725cabbc6bSPrashanth Sreenivasa 	metaslab_group_t *mg = vd->vdev_mg;
19735cabbc6bSPrashanth Sreenivasa 	metaslab_group_passivate(mg);
19745cabbc6bSPrashanth Sreenivasa 
19755cabbc6bSPrashanth Sreenivasa 	/*
19765cabbc6bSPrashanth Sreenivasa 	 * Wait for the youngest allocations and frees to sync,
19775cabbc6bSPrashanth Sreenivasa 	 * and then wait for the deferral of those frees to finish.
19785cabbc6bSPrashanth Sreenivasa 	 */
19795cabbc6bSPrashanth Sreenivasa 	spa_vdev_config_exit(spa, NULL,
19805cabbc6bSPrashanth Sreenivasa 	    *txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
19815cabbc6bSPrashanth Sreenivasa 
19825cabbc6bSPrashanth Sreenivasa 	/*
19835cabbc6bSPrashanth Sreenivasa 	 * We must ensure that no "stubby" log blocks are allocated
19845cabbc6bSPrashanth Sreenivasa 	 * on the device to be removed.  These blocks could be
19855cabbc6bSPrashanth Sreenivasa 	 * written at any time, including while we are in the middle
19865cabbc6bSPrashanth Sreenivasa 	 * of copying them.
19875cabbc6bSPrashanth Sreenivasa 	 */
19885cabbc6bSPrashanth Sreenivasa 	error = spa_reset_logs(spa);
19895cabbc6bSPrashanth Sreenivasa 
1990094e47e9SGeorge Wilson 	/*
1991094e47e9SGeorge Wilson 	 * We stop any initializing that is currently in progress but leave
1992094e47e9SGeorge Wilson 	 * the state as "active". This will allow the initializing to resume
1993094e47e9SGeorge Wilson 	 * if the removal is canceled sometime later.
1994094e47e9SGeorge Wilson 	 */
1995094e47e9SGeorge Wilson 	vdev_initialize_stop_all(vd, VDEV_INITIALIZE_ACTIVE);
1996094e47e9SGeorge Wilson 
19975cabbc6bSPrashanth Sreenivasa 	*txg = spa_vdev_config_enter(spa);
19985cabbc6bSPrashanth Sreenivasa 
19995cabbc6bSPrashanth Sreenivasa 	/*
20005cabbc6bSPrashanth Sreenivasa 	 * Things might have changed while the config lock was dropped
20015cabbc6bSPrashanth Sreenivasa 	 * (e.g. space usage).  Check for errors again.
20025cabbc6bSPrashanth Sreenivasa 	 */
20035cabbc6bSPrashanth Sreenivasa 	if (error == 0)
20045cabbc6bSPrashanth Sreenivasa 		error = spa_vdev_remove_top_check(vd);
20055cabbc6bSPrashanth Sreenivasa 
20065cabbc6bSPrashanth Sreenivasa 	if (error != 0) {
20075cabbc6bSPrashanth Sreenivasa 		metaslab_group_activate(mg);
2008094e47e9SGeorge Wilson 		spa_async_request(spa, SPA_ASYNC_INITIALIZE_RESTART);
20095cabbc6bSPrashanth Sreenivasa 		return (error);
20105cabbc6bSPrashanth Sreenivasa 	}
20115cabbc6bSPrashanth Sreenivasa 
20125cabbc6bSPrashanth Sreenivasa 	vd->vdev_removing = B_TRUE;
20135cabbc6bSPrashanth Sreenivasa 
20145cabbc6bSPrashanth Sreenivasa 	vdev_dirty_leaves(vd, VDD_DTL, *txg);
20155cabbc6bSPrashanth Sreenivasa 	vdev_config_dirty(vd);
20165cabbc6bSPrashanth Sreenivasa 	dmu_tx_t *tx = dmu_tx_create_assigned(spa->spa_dsl_pool, *txg);
20175cabbc6bSPrashanth Sreenivasa 	dsl_sync_task_nowait(spa->spa_dsl_pool,
20185cabbc6bSPrashanth Sreenivasa 	    vdev_remove_initiate_sync,
20193a4b1be9SMatthew Ahrens 	    (void *)(uintptr_t)vd->vdev_id, 0, ZFS_SPACE_CHECK_NONE, tx);
20205cabbc6bSPrashanth Sreenivasa 	dmu_tx_commit(tx);
20215cabbc6bSPrashanth Sreenivasa 
20225cabbc6bSPrashanth Sreenivasa 	return (0);
20235cabbc6bSPrashanth Sreenivasa }
20245cabbc6bSPrashanth Sreenivasa 
20255cabbc6bSPrashanth Sreenivasa /*
20265cabbc6bSPrashanth Sreenivasa  * Remove a device from the pool.
20275cabbc6bSPrashanth Sreenivasa  *
20285cabbc6bSPrashanth Sreenivasa  * Removing a device from the vdev namespace requires several steps
20295cabbc6bSPrashanth Sreenivasa  * and can take a significant amount of time.  As a result we use
20305cabbc6bSPrashanth Sreenivasa  * the spa_vdev_config_[enter/exit] functions which allow us to
20315cabbc6bSPrashanth Sreenivasa  * grab and release the spa_config_lock while still holding the namespace
20325cabbc6bSPrashanth Sreenivasa  * lock.  During each step the configuration is synced out.
20335cabbc6bSPrashanth Sreenivasa  */
20345cabbc6bSPrashanth Sreenivasa int
20355cabbc6bSPrashanth Sreenivasa spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
20365cabbc6bSPrashanth Sreenivasa {
20375cabbc6bSPrashanth Sreenivasa 	vdev_t *vd;
20385cabbc6bSPrashanth Sreenivasa 	nvlist_t **spares, **l2cache, *nv;
20395cabbc6bSPrashanth Sreenivasa 	uint64_t txg = 0;
20405cabbc6bSPrashanth Sreenivasa 	uint_t nspares, nl2cache;
20415cabbc6bSPrashanth Sreenivasa 	int error = 0;
20425cabbc6bSPrashanth Sreenivasa 	boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
20435cabbc6bSPrashanth Sreenivasa 	sysevent_t *ev = NULL;
20445cabbc6bSPrashanth Sreenivasa 
20455cabbc6bSPrashanth Sreenivasa 	ASSERT(spa_writeable(spa));
20465cabbc6bSPrashanth Sreenivasa 
20475cabbc6bSPrashanth Sreenivasa 	if (!locked)
20485cabbc6bSPrashanth Sreenivasa 		txg = spa_vdev_enter(spa);
20495cabbc6bSPrashanth Sreenivasa 
205086714001SSerapheim Dimitropoulos 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
205186714001SSerapheim Dimitropoulos 	if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) {
205286714001SSerapheim Dimitropoulos 		error = (spa_has_checkpoint(spa)) ?
205386714001SSerapheim Dimitropoulos 		    ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT;
205486714001SSerapheim Dimitropoulos 
205586714001SSerapheim Dimitropoulos 		if (!locked)
205686714001SSerapheim Dimitropoulos 			return (spa_vdev_exit(spa, NULL, txg, error));
205786714001SSerapheim Dimitropoulos 
205886714001SSerapheim Dimitropoulos 		return (error);
205986714001SSerapheim Dimitropoulos 	}
206086714001SSerapheim Dimitropoulos 
20615cabbc6bSPrashanth Sreenivasa 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
20625cabbc6bSPrashanth Sreenivasa 
20635cabbc6bSPrashanth Sreenivasa 	if (spa->spa_spares.sav_vdevs != NULL &&
20645cabbc6bSPrashanth Sreenivasa 	    nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
20655cabbc6bSPrashanth Sreenivasa 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
20665cabbc6bSPrashanth Sreenivasa 	    (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
20675cabbc6bSPrashanth Sreenivasa 		/*
20685cabbc6bSPrashanth Sreenivasa 		 * Only remove the hot spare if it's not currently in use
20695cabbc6bSPrashanth Sreenivasa 		 * in this pool.
20705cabbc6bSPrashanth Sreenivasa 		 */
20715cabbc6bSPrashanth Sreenivasa 		if (vd == NULL || unspare) {
20725cabbc6bSPrashanth Sreenivasa 			char *nvstr = fnvlist_lookup_string(nv,
20735cabbc6bSPrashanth Sreenivasa 			    ZPOOL_CONFIG_PATH);
20745cabbc6bSPrashanth Sreenivasa 			spa_history_log_internal(spa, "vdev remove", NULL,
20755cabbc6bSPrashanth Sreenivasa 			    "%s vdev (%s) %s", spa_name(spa),
20765cabbc6bSPrashanth Sreenivasa 			    VDEV_TYPE_SPARE, nvstr);
20775cabbc6bSPrashanth Sreenivasa 			if (vd == NULL)
20785cabbc6bSPrashanth Sreenivasa 				vd = spa_lookup_by_guid(spa, guid, B_TRUE);
20795cabbc6bSPrashanth Sreenivasa 			ev = spa_event_create(spa, vd, NULL,
20805cabbc6bSPrashanth Sreenivasa 			    ESC_ZFS_VDEV_REMOVE_AUX);
20815cabbc6bSPrashanth Sreenivasa 			spa_vdev_remove_aux(spa->spa_spares.sav_config,
20825cabbc6bSPrashanth Sreenivasa 			    ZPOOL_CONFIG_SPARES, spares, nspares, nv);
20835cabbc6bSPrashanth Sreenivasa 			spa_load_spares(spa);
20845cabbc6bSPrashanth Sreenivasa 			spa->spa_spares.sav_sync = B_TRUE;
20855cabbc6bSPrashanth Sreenivasa 		} else {
20865cabbc6bSPrashanth Sreenivasa 			error = SET_ERROR(EBUSY);
20875cabbc6bSPrashanth Sreenivasa 		}
20885cabbc6bSPrashanth Sreenivasa 	} else if (spa->spa_l2cache.sav_vdevs != NULL &&
20895cabbc6bSPrashanth Sreenivasa 	    nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
20905cabbc6bSPrashanth Sreenivasa 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
20915cabbc6bSPrashanth Sreenivasa 	    (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
20925cabbc6bSPrashanth Sreenivasa 		char *nvstr = fnvlist_lookup_string(nv, ZPOOL_CONFIG_PATH);
20935cabbc6bSPrashanth Sreenivasa 		spa_history_log_internal(spa, "vdev remove", NULL,
20945cabbc6bSPrashanth Sreenivasa 		    "%s vdev (%s) %s", spa_name(spa), VDEV_TYPE_L2CACHE, nvstr);
20955cabbc6bSPrashanth Sreenivasa 		/*
20965cabbc6bSPrashanth Sreenivasa 		 * Cache devices can always be removed.
20975cabbc6bSPrashanth Sreenivasa 		 */
20985cabbc6bSPrashanth Sreenivasa 		vd = spa_lookup_by_guid(spa, guid, B_TRUE);
20995cabbc6bSPrashanth Sreenivasa 		ev = spa_event_create(spa, vd, NULL, ESC_ZFS_VDEV_REMOVE_AUX);
21005cabbc6bSPrashanth Sreenivasa 		spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
21015cabbc6bSPrashanth Sreenivasa 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
21025cabbc6bSPrashanth Sreenivasa 		spa_load_l2cache(spa);
21035cabbc6bSPrashanth Sreenivasa 		spa->spa_l2cache.sav_sync = B_TRUE;
21045cabbc6bSPrashanth Sreenivasa 	} else if (vd != NULL && vd->vdev_islog) {
21055cabbc6bSPrashanth Sreenivasa 		ASSERT(!locked);
21065cabbc6bSPrashanth Sreenivasa 		error = spa_vdev_remove_log(vd, &txg);
21075cabbc6bSPrashanth Sreenivasa 	} else if (vd != NULL) {
21085cabbc6bSPrashanth Sreenivasa 		ASSERT(!locked);
21095cabbc6bSPrashanth Sreenivasa 		error = spa_vdev_remove_top(vd, &txg);
21105cabbc6bSPrashanth Sreenivasa 	} else {
21115cabbc6bSPrashanth Sreenivasa 		/*
21125cabbc6bSPrashanth Sreenivasa 		 * There is no vdev of any kind with the specified guid.
21135cabbc6bSPrashanth Sreenivasa 		 */
21145cabbc6bSPrashanth Sreenivasa 		error = SET_ERROR(ENOENT);
21155cabbc6bSPrashanth Sreenivasa 	}
21165cabbc6bSPrashanth Sreenivasa 
21175cabbc6bSPrashanth Sreenivasa 	if (!locked)
21185cabbc6bSPrashanth Sreenivasa 		error = spa_vdev_exit(spa, NULL, txg, error);
21195cabbc6bSPrashanth Sreenivasa 
21205cabbc6bSPrashanth Sreenivasa 	if (ev != NULL) {
21215cabbc6bSPrashanth Sreenivasa 		if (error != 0) {
21225cabbc6bSPrashanth Sreenivasa 			spa_event_discard(ev);
21235cabbc6bSPrashanth Sreenivasa 		} else {
21245cabbc6bSPrashanth Sreenivasa 			spa_event_post(ev);
21255cabbc6bSPrashanth Sreenivasa 		}
21265cabbc6bSPrashanth Sreenivasa 	}
21275cabbc6bSPrashanth Sreenivasa 
21285cabbc6bSPrashanth Sreenivasa 	return (error);
21295cabbc6bSPrashanth Sreenivasa }
21305cabbc6bSPrashanth Sreenivasa 
21315cabbc6bSPrashanth Sreenivasa int
21325cabbc6bSPrashanth Sreenivasa spa_removal_get_stats(spa_t *spa, pool_removal_stat_t *prs)
21335cabbc6bSPrashanth Sreenivasa {
21345cabbc6bSPrashanth Sreenivasa 	prs->prs_state = spa->spa_removing_phys.sr_state;
21355cabbc6bSPrashanth Sreenivasa 
21365cabbc6bSPrashanth Sreenivasa 	if (prs->prs_state == DSS_NONE)
21375cabbc6bSPrashanth Sreenivasa 		return (SET_ERROR(ENOENT));
21385cabbc6bSPrashanth Sreenivasa 
21395cabbc6bSPrashanth Sreenivasa 	prs->prs_removing_vdev = spa->spa_removing_phys.sr_removing_vdev;
21405cabbc6bSPrashanth Sreenivasa 	prs->prs_start_time = spa->spa_removing_phys.sr_start_time;
21415cabbc6bSPrashanth Sreenivasa 	prs->prs_end_time = spa->spa_removing_phys.sr_end_time;
21425cabbc6bSPrashanth Sreenivasa 	prs->prs_to_copy = spa->spa_removing_phys.sr_to_copy;
21435cabbc6bSPrashanth Sreenivasa 	prs->prs_copied = spa->spa_removing_phys.sr_copied;
21445cabbc6bSPrashanth Sreenivasa 
21455cabbc6bSPrashanth Sreenivasa 	if (spa->spa_vdev_removal != NULL) {
21465cabbc6bSPrashanth Sreenivasa 		for (int i = 0; i < TXG_SIZE; i++) {
21475cabbc6bSPrashanth Sreenivasa 			prs->prs_copied +=
21485cabbc6bSPrashanth Sreenivasa 			    spa->spa_vdev_removal->svr_bytes_done[i];
21495cabbc6bSPrashanth Sreenivasa 		}
21505cabbc6bSPrashanth Sreenivasa 	}
21515cabbc6bSPrashanth Sreenivasa 
21525cabbc6bSPrashanth Sreenivasa 	prs->prs_mapping_memory = 0;
21535cabbc6bSPrashanth Sreenivasa 	uint64_t indirect_vdev_id =
21545cabbc6bSPrashanth Sreenivasa 	    spa->spa_removing_phys.sr_prev_indirect_vdev;
21555cabbc6bSPrashanth Sreenivasa 	while (indirect_vdev_id != -1) {
21565cabbc6bSPrashanth Sreenivasa 		vdev_t *vd = spa->spa_root_vdev->vdev_child[indirect_vdev_id];
21575cabbc6bSPrashanth Sreenivasa 		vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
21585cabbc6bSPrashanth Sreenivasa 		vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
21595cabbc6bSPrashanth Sreenivasa 
21605cabbc6bSPrashanth Sreenivasa 		ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops);
21615cabbc6bSPrashanth Sreenivasa 		prs->prs_mapping_memory += vdev_indirect_mapping_size(vim);
21625cabbc6bSPrashanth Sreenivasa 		indirect_vdev_id = vic->vic_prev_indirect_vdev;
21635cabbc6bSPrashanth Sreenivasa 	}
21645cabbc6bSPrashanth Sreenivasa 
21655cabbc6bSPrashanth Sreenivasa 	return (0);
21665cabbc6bSPrashanth Sreenivasa }
2167