xref: /illumos-gate/usr/src/uts/common/fs/zfs/vdev_removal.c (revision 555d674d5d4b8191dc83723188349d28278b2431)
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 
108cfd63e1bSMatthew Ahrens /*
109cfd63e1bSMatthew Ahrens  * Allow a remap segment to span free chunks of at most this size. The main
110cfd63e1bSMatthew Ahrens  * impact of a larger span is that we will read and write larger, more
111cfd63e1bSMatthew Ahrens  * contiguous chunks, with more "unnecessary" data -- trading off bandwidth
112cfd63e1bSMatthew Ahrens  * for iops.  The value here was chosen to align with
113cfd63e1bSMatthew Ahrens  * zfs_vdev_read_gap_limit, which is a similar concept when doing regular
114cfd63e1bSMatthew Ahrens  * reads (but there's no reason it has to be the same).
115cfd63e1bSMatthew Ahrens  *
116cfd63e1bSMatthew Ahrens  * Additionally, a higher span will have the following relatively minor
117cfd63e1bSMatthew Ahrens  * effects:
118cfd63e1bSMatthew Ahrens  *  - the mapping will be smaller, since one entry can cover more allocated
119cfd63e1bSMatthew Ahrens  *    segments
120cfd63e1bSMatthew Ahrens  *  - more of the fragmentation in the removing device will be preserved
121cfd63e1bSMatthew Ahrens  *  - we'll do larger allocations, which may fail and fall back on smaller
122cfd63e1bSMatthew Ahrens  *    allocations
123cfd63e1bSMatthew Ahrens  */
124cfd63e1bSMatthew Ahrens int vdev_removal_max_span = 32 * 1024;
125cfd63e1bSMatthew 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 		spa->spa_removing_phys.sr_to_copy +=
287*555d674dSSerapheim Dimitropoulos 		    metaslab_allocated_space(ms);
2885cabbc6bSPrashanth Sreenivasa 
2895cabbc6bSPrashanth Sreenivasa 		/*
2905cabbc6bSPrashanth Sreenivasa 		 * Space which we are freeing this txg does not need to
2915cabbc6bSPrashanth Sreenivasa 		 * be copied.
2925cabbc6bSPrashanth Sreenivasa 		 */
2935cabbc6bSPrashanth Sreenivasa 		spa->spa_removing_phys.sr_to_copy -=
29486714001SSerapheim Dimitropoulos 		    range_tree_space(ms->ms_freeing);
2955cabbc6bSPrashanth Sreenivasa 
29686714001SSerapheim Dimitropoulos 		ASSERT0(range_tree_space(ms->ms_freed));
2975cabbc6bSPrashanth Sreenivasa 		for (int t = 0; t < TXG_SIZE; t++)
29886714001SSerapheim Dimitropoulos 			ASSERT0(range_tree_space(ms->ms_allocating[t]));
2995cabbc6bSPrashanth Sreenivasa 	}
3005cabbc6bSPrashanth Sreenivasa 
3015cabbc6bSPrashanth Sreenivasa 	/*
3025cabbc6bSPrashanth Sreenivasa 	 * Sync tasks are called before metaslab_sync(), so there should
3035cabbc6bSPrashanth Sreenivasa 	 * be no already-synced metaslabs in the TXG_CLEAN list.
3045cabbc6bSPrashanth Sreenivasa 	 */
3055cabbc6bSPrashanth Sreenivasa 	ASSERT3P(txg_list_head(&vd->vdev_ms_list, TXG_CLEAN(txg)), ==, NULL);
3065cabbc6bSPrashanth Sreenivasa 
3075cabbc6bSPrashanth Sreenivasa 	spa_sync_removing_state(spa, tx);
3085cabbc6bSPrashanth Sreenivasa 
3095cabbc6bSPrashanth Sreenivasa 	/*
3105cabbc6bSPrashanth Sreenivasa 	 * All blocks that we need to read the most recent mapping must be
3115cabbc6bSPrashanth Sreenivasa 	 * stored on concrete vdevs.  Therefore, we must dirty anything that
3125cabbc6bSPrashanth Sreenivasa 	 * is read before spa_remove_init().  Specifically, the
3135cabbc6bSPrashanth Sreenivasa 	 * spa_config_object.  (Note that although we already modified the
3145cabbc6bSPrashanth Sreenivasa 	 * spa_config_object in spa_sync_removing_state, that may not have
3155cabbc6bSPrashanth Sreenivasa 	 * modified all blocks of the object.)
3165cabbc6bSPrashanth Sreenivasa 	 */
3175cabbc6bSPrashanth Sreenivasa 	dmu_object_info_t doi;
3185cabbc6bSPrashanth Sreenivasa 	VERIFY0(dmu_object_info(mos, DMU_POOL_DIRECTORY_OBJECT, &doi));
3195cabbc6bSPrashanth Sreenivasa 	for (uint64_t offset = 0; offset < doi.doi_max_offset; ) {
3205cabbc6bSPrashanth Sreenivasa 		dmu_buf_t *dbuf;
3215cabbc6bSPrashanth Sreenivasa 		VERIFY0(dmu_buf_hold(mos, DMU_POOL_DIRECTORY_OBJECT,
3225cabbc6bSPrashanth Sreenivasa 		    offset, FTAG, &dbuf, 0));
3235cabbc6bSPrashanth Sreenivasa 		dmu_buf_will_dirty(dbuf, tx);
3245cabbc6bSPrashanth Sreenivasa 		offset += dbuf->db_size;
3255cabbc6bSPrashanth Sreenivasa 		dmu_buf_rele(dbuf, FTAG);
3265cabbc6bSPrashanth Sreenivasa 	}
3275cabbc6bSPrashanth Sreenivasa 
3285cabbc6bSPrashanth Sreenivasa 	/*
3295cabbc6bSPrashanth Sreenivasa 	 * Now that we've allocated the im_object, dirty the vdev to ensure
3305cabbc6bSPrashanth Sreenivasa 	 * that the object gets written to the config on disk.
3315cabbc6bSPrashanth Sreenivasa 	 */
3325cabbc6bSPrashanth Sreenivasa 	vdev_config_dirty(vd);
3335cabbc6bSPrashanth Sreenivasa 
3345cabbc6bSPrashanth Sreenivasa 	zfs_dbgmsg("starting removal thread for vdev %llu (%p) in txg %llu "
3355cabbc6bSPrashanth Sreenivasa 	    "im_obj=%llu", vd->vdev_id, vd, dmu_tx_get_txg(tx),
3365cabbc6bSPrashanth Sreenivasa 	    vic->vic_mapping_object);
3375cabbc6bSPrashanth Sreenivasa 
3385cabbc6bSPrashanth Sreenivasa 	spa_history_log_internal(spa, "vdev remove started", tx,
3395cabbc6bSPrashanth Sreenivasa 	    "%s vdev %llu %s", spa_name(spa), vd->vdev_id,
3405cabbc6bSPrashanth Sreenivasa 	    (vd->vdev_path != NULL) ? vd->vdev_path : "-");
3415cabbc6bSPrashanth Sreenivasa 	/*
3425cabbc6bSPrashanth Sreenivasa 	 * Setting spa_vdev_removal causes subsequent frees to call
3435cabbc6bSPrashanth Sreenivasa 	 * free_from_removing_vdev().  Note that we don't need any locking
3445cabbc6bSPrashanth Sreenivasa 	 * because we are the sync thread, and metaslab_free_impl() is only
3455cabbc6bSPrashanth Sreenivasa 	 * called from syncing context (potentially from a zio taskq thread,
3465cabbc6bSPrashanth Sreenivasa 	 * but in any case only when there are outstanding free i/os, which
3475cabbc6bSPrashanth Sreenivasa 	 * there are not).
3485cabbc6bSPrashanth Sreenivasa 	 */
3495cabbc6bSPrashanth Sreenivasa 	ASSERT3P(spa->spa_vdev_removal, ==, NULL);
3505cabbc6bSPrashanth Sreenivasa 	spa->spa_vdev_removal = svr;
3515cabbc6bSPrashanth Sreenivasa 	svr->svr_thread = thread_create(NULL, 0,
3523a4b1be9SMatthew Ahrens 	    spa_vdev_remove_thread, spa, 0, &p0, TS_RUN, minclsyspri);
3535cabbc6bSPrashanth Sreenivasa }
3545cabbc6bSPrashanth Sreenivasa 
3555cabbc6bSPrashanth Sreenivasa /*
3565cabbc6bSPrashanth Sreenivasa  * When we are opening a pool, we must read the mapping for each
3575cabbc6bSPrashanth Sreenivasa  * indirect vdev in order from most recently removed to least
3585cabbc6bSPrashanth Sreenivasa  * recently removed.  We do this because the blocks for the mapping
3595cabbc6bSPrashanth Sreenivasa  * of older indirect vdevs may be stored on more recently removed vdevs.
3605cabbc6bSPrashanth Sreenivasa  * In order to read each indirect mapping object, we must have
3615cabbc6bSPrashanth Sreenivasa  * initialized all more recently removed vdevs.
3625cabbc6bSPrashanth Sreenivasa  */
3635cabbc6bSPrashanth Sreenivasa int
3645cabbc6bSPrashanth Sreenivasa spa_remove_init(spa_t *spa)
3655cabbc6bSPrashanth Sreenivasa {
3665cabbc6bSPrashanth Sreenivasa 	int error;
3675cabbc6bSPrashanth Sreenivasa 
3685cabbc6bSPrashanth Sreenivasa 	error = zap_lookup(spa->spa_dsl_pool->dp_meta_objset,
3695cabbc6bSPrashanth Sreenivasa 	    DMU_POOL_DIRECTORY_OBJECT,
3705cabbc6bSPrashanth Sreenivasa 	    DMU_POOL_REMOVING, sizeof (uint64_t),
3715cabbc6bSPrashanth Sreenivasa 	    sizeof (spa->spa_removing_phys) / sizeof (uint64_t),
3725cabbc6bSPrashanth Sreenivasa 	    &spa->spa_removing_phys);
3735cabbc6bSPrashanth Sreenivasa 
3745cabbc6bSPrashanth Sreenivasa 	if (error == ENOENT) {
3755cabbc6bSPrashanth Sreenivasa 		spa->spa_removing_phys.sr_state = DSS_NONE;
3765cabbc6bSPrashanth Sreenivasa 		spa->spa_removing_phys.sr_removing_vdev = -1;
3775cabbc6bSPrashanth Sreenivasa 		spa->spa_removing_phys.sr_prev_indirect_vdev = -1;
37847b8d4b8SAlexander Motin 		spa->spa_indirect_vdevs_loaded = B_TRUE;
3795cabbc6bSPrashanth Sreenivasa 		return (0);
3805cabbc6bSPrashanth Sreenivasa 	} else if (error != 0) {
3815cabbc6bSPrashanth Sreenivasa 		return (error);
3825cabbc6bSPrashanth Sreenivasa 	}
3835cabbc6bSPrashanth Sreenivasa 
3845cabbc6bSPrashanth Sreenivasa 	if (spa->spa_removing_phys.sr_state == DSS_SCANNING) {
3855cabbc6bSPrashanth Sreenivasa 		/*
3865cabbc6bSPrashanth Sreenivasa 		 * We are currently removing a vdev.  Create and
3875cabbc6bSPrashanth Sreenivasa 		 * initialize a spa_vdev_removal_t from the bonus
3885cabbc6bSPrashanth Sreenivasa 		 * buffer of the removing vdevs vdev_im_object, and
3895cabbc6bSPrashanth Sreenivasa 		 * initialize its partial mapping.
3905cabbc6bSPrashanth Sreenivasa 		 */
3915cabbc6bSPrashanth Sreenivasa 		spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
3925cabbc6bSPrashanth Sreenivasa 		vdev_t *vd = vdev_lookup_top(spa,
3935cabbc6bSPrashanth Sreenivasa 		    spa->spa_removing_phys.sr_removing_vdev);
3945cabbc6bSPrashanth Sreenivasa 
3953a4b1be9SMatthew Ahrens 		if (vd == NULL) {
3963a4b1be9SMatthew Ahrens 			spa_config_exit(spa, SCL_STATE, FTAG);
3975cabbc6bSPrashanth Sreenivasa 			return (EINVAL);
3983a4b1be9SMatthew Ahrens 		}
3995cabbc6bSPrashanth Sreenivasa 
4005cabbc6bSPrashanth Sreenivasa 		vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
4015cabbc6bSPrashanth Sreenivasa 
4025cabbc6bSPrashanth Sreenivasa 		ASSERT(vdev_is_concrete(vd));
4035cabbc6bSPrashanth Sreenivasa 		spa_vdev_removal_t *svr = spa_vdev_removal_create(vd);
4043a4b1be9SMatthew Ahrens 		ASSERT3U(svr->svr_vdev_id, ==, vd->vdev_id);
4053a4b1be9SMatthew Ahrens 		ASSERT(vd->vdev_removing);
4065cabbc6bSPrashanth Sreenivasa 
4075cabbc6bSPrashanth Sreenivasa 		vd->vdev_indirect_mapping = vdev_indirect_mapping_open(
4085cabbc6bSPrashanth Sreenivasa 		    spa->spa_meta_objset, vic->vic_mapping_object);
4095cabbc6bSPrashanth Sreenivasa 		vd->vdev_indirect_births = vdev_indirect_births_open(
4105cabbc6bSPrashanth Sreenivasa 		    spa->spa_meta_objset, vic->vic_births_object);
4113a4b1be9SMatthew Ahrens 		spa_config_exit(spa, SCL_STATE, FTAG);
4125cabbc6bSPrashanth Sreenivasa 
4135cabbc6bSPrashanth Sreenivasa 		spa->spa_vdev_removal = svr;
4145cabbc6bSPrashanth Sreenivasa 	}
4155cabbc6bSPrashanth Sreenivasa 
4165cabbc6bSPrashanth Sreenivasa 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
4175cabbc6bSPrashanth Sreenivasa 	uint64_t indirect_vdev_id =
4185cabbc6bSPrashanth Sreenivasa 	    spa->spa_removing_phys.sr_prev_indirect_vdev;
4195cabbc6bSPrashanth Sreenivasa 	while (indirect_vdev_id != UINT64_MAX) {
4205cabbc6bSPrashanth Sreenivasa 		vdev_t *vd = vdev_lookup_top(spa, indirect_vdev_id);
4215cabbc6bSPrashanth Sreenivasa 		vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
4225cabbc6bSPrashanth Sreenivasa 
4235cabbc6bSPrashanth Sreenivasa 		ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops);
4245cabbc6bSPrashanth Sreenivasa 		vd->vdev_indirect_mapping = vdev_indirect_mapping_open(
4255cabbc6bSPrashanth Sreenivasa 		    spa->spa_meta_objset, vic->vic_mapping_object);
4265cabbc6bSPrashanth Sreenivasa 		vd->vdev_indirect_births = vdev_indirect_births_open(
4275cabbc6bSPrashanth Sreenivasa 		    spa->spa_meta_objset, vic->vic_births_object);
4285cabbc6bSPrashanth Sreenivasa 
4295cabbc6bSPrashanth Sreenivasa 		indirect_vdev_id = vic->vic_prev_indirect_vdev;
4305cabbc6bSPrashanth Sreenivasa 	}
4315cabbc6bSPrashanth Sreenivasa 	spa_config_exit(spa, SCL_STATE, FTAG);
4325cabbc6bSPrashanth Sreenivasa 
4335cabbc6bSPrashanth Sreenivasa 	/*
4345cabbc6bSPrashanth Sreenivasa 	 * Now that we've loaded all the indirect mappings, we can allow
4355cabbc6bSPrashanth Sreenivasa 	 * reads from other blocks (e.g. via predictive prefetch).
4365cabbc6bSPrashanth Sreenivasa 	 */
4375cabbc6bSPrashanth Sreenivasa 	spa->spa_indirect_vdevs_loaded = B_TRUE;
4385cabbc6bSPrashanth Sreenivasa 	return (0);
4395cabbc6bSPrashanth Sreenivasa }
4405cabbc6bSPrashanth Sreenivasa 
4415cabbc6bSPrashanth Sreenivasa void
4425cabbc6bSPrashanth Sreenivasa spa_restart_removal(spa_t *spa)
4435cabbc6bSPrashanth Sreenivasa {
4445cabbc6bSPrashanth Sreenivasa 	spa_vdev_removal_t *svr = spa->spa_vdev_removal;
4455cabbc6bSPrashanth Sreenivasa 
4465cabbc6bSPrashanth Sreenivasa 	if (svr == NULL)
4475cabbc6bSPrashanth Sreenivasa 		return;
4485cabbc6bSPrashanth Sreenivasa 
4495cabbc6bSPrashanth Sreenivasa 	/*
4505cabbc6bSPrashanth Sreenivasa 	 * In general when this function is called there is no
4515cabbc6bSPrashanth Sreenivasa 	 * removal thread running. The only scenario where this
4525cabbc6bSPrashanth Sreenivasa 	 * is not true is during spa_import() where this function
4535cabbc6bSPrashanth Sreenivasa 	 * is called twice [once from spa_import_impl() and
4545cabbc6bSPrashanth Sreenivasa 	 * spa_async_resume()]. Thus, in the scenario where we
4555cabbc6bSPrashanth Sreenivasa 	 * import a pool that has an ongoing removal we don't
4565cabbc6bSPrashanth Sreenivasa 	 * want to spawn a second thread.
4575cabbc6bSPrashanth Sreenivasa 	 */
4585cabbc6bSPrashanth Sreenivasa 	if (svr->svr_thread != NULL)
4595cabbc6bSPrashanth Sreenivasa 		return;
4605cabbc6bSPrashanth Sreenivasa 
4615cabbc6bSPrashanth Sreenivasa 	if (!spa_writeable(spa))
4625cabbc6bSPrashanth Sreenivasa 		return;
4635cabbc6bSPrashanth Sreenivasa 
4643a4b1be9SMatthew Ahrens 	zfs_dbgmsg("restarting removal of %llu", svr->svr_vdev_id);
4653a4b1be9SMatthew Ahrens 	svr->svr_thread = thread_create(NULL, 0, spa_vdev_remove_thread, spa,
4665cabbc6bSPrashanth Sreenivasa 	    0, &p0, TS_RUN, minclsyspri);
4675cabbc6bSPrashanth Sreenivasa }
4685cabbc6bSPrashanth Sreenivasa 
4695cabbc6bSPrashanth Sreenivasa /*
4705cabbc6bSPrashanth Sreenivasa  * Process freeing from a device which is in the middle of being removed.
4715cabbc6bSPrashanth Sreenivasa  * We must handle this carefully so that we attempt to copy freed data,
4725cabbc6bSPrashanth Sreenivasa  * and we correctly free already-copied data.
4735cabbc6bSPrashanth Sreenivasa  */
4745cabbc6bSPrashanth Sreenivasa void
47586714001SSerapheim Dimitropoulos free_from_removing_vdev(vdev_t *vd, uint64_t offset, uint64_t size)
4765cabbc6bSPrashanth Sreenivasa {
4775cabbc6bSPrashanth Sreenivasa 	spa_t *spa = vd->vdev_spa;
4785cabbc6bSPrashanth Sreenivasa 	spa_vdev_removal_t *svr = spa->spa_vdev_removal;
4795cabbc6bSPrashanth Sreenivasa 	vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
48086714001SSerapheim Dimitropoulos 	uint64_t txg = spa_syncing_txg(spa);
4815cabbc6bSPrashanth Sreenivasa 	uint64_t max_offset_yet = 0;
4825cabbc6bSPrashanth Sreenivasa 
4835cabbc6bSPrashanth Sreenivasa 	ASSERT(vd->vdev_indirect_config.vic_mapping_object != 0);
4845cabbc6bSPrashanth Sreenivasa 	ASSERT3U(vd->vdev_indirect_config.vic_mapping_object, ==,
4855cabbc6bSPrashanth Sreenivasa 	    vdev_indirect_mapping_object(vim));
4863a4b1be9SMatthew Ahrens 	ASSERT3U(vd->vdev_id, ==, svr->svr_vdev_id);
4875cabbc6bSPrashanth Sreenivasa 
4885cabbc6bSPrashanth Sreenivasa 	mutex_enter(&svr->svr_lock);
4895cabbc6bSPrashanth Sreenivasa 
4905cabbc6bSPrashanth Sreenivasa 	/*
4915cabbc6bSPrashanth Sreenivasa 	 * Remove the segment from the removing vdev's spacemap.  This
4925cabbc6bSPrashanth Sreenivasa 	 * ensures that we will not attempt to copy this space (if the
4935cabbc6bSPrashanth Sreenivasa 	 * removal thread has not yet visited it), and also ensures
4945cabbc6bSPrashanth Sreenivasa 	 * that we know what is actually allocated on the new vdevs
4955cabbc6bSPrashanth Sreenivasa 	 * (needed if we cancel the removal).
4965cabbc6bSPrashanth Sreenivasa 	 *
4975cabbc6bSPrashanth Sreenivasa 	 * Note: we must do the metaslab_free_concrete() with the svr_lock
4985cabbc6bSPrashanth Sreenivasa 	 * held, so that the remove_thread can not load this metaslab and then
4995cabbc6bSPrashanth Sreenivasa 	 * visit this offset between the time that we metaslab_free_concrete()
5005cabbc6bSPrashanth Sreenivasa 	 * and when we check to see if it has been visited.
50186714001SSerapheim Dimitropoulos 	 *
50286714001SSerapheim Dimitropoulos 	 * Note: The checkpoint flag is set to false as having/taking
50386714001SSerapheim Dimitropoulos 	 * a checkpoint and removing a device can't happen at the same
50486714001SSerapheim Dimitropoulos 	 * time.
5055cabbc6bSPrashanth Sreenivasa 	 */
50686714001SSerapheim Dimitropoulos 	ASSERT(!spa_has_checkpoint(spa));
50786714001SSerapheim Dimitropoulos 	metaslab_free_concrete(vd, offset, size, B_FALSE);
5085cabbc6bSPrashanth Sreenivasa 
5095cabbc6bSPrashanth Sreenivasa 	uint64_t synced_size = 0;
5105cabbc6bSPrashanth Sreenivasa 	uint64_t synced_offset = 0;
5115cabbc6bSPrashanth Sreenivasa 	uint64_t max_offset_synced = vdev_indirect_mapping_max_offset(vim);
5125cabbc6bSPrashanth Sreenivasa 	if (offset < max_offset_synced) {
5135cabbc6bSPrashanth Sreenivasa 		/*
5145cabbc6bSPrashanth Sreenivasa 		 * The mapping for this offset is already on disk.
5155cabbc6bSPrashanth Sreenivasa 		 * Free from the new location.
5165cabbc6bSPrashanth Sreenivasa 		 *
5175cabbc6bSPrashanth Sreenivasa 		 * Note that we use svr_max_synced_offset because it is
5185cabbc6bSPrashanth Sreenivasa 		 * updated atomically with respect to the in-core mapping.
5195cabbc6bSPrashanth Sreenivasa 		 * By contrast, vim_max_offset is not.
5205cabbc6bSPrashanth Sreenivasa 		 *
5215cabbc6bSPrashanth Sreenivasa 		 * This block may be split between a synced entry and an
5225cabbc6bSPrashanth Sreenivasa 		 * in-flight or unvisited entry.  Only process the synced
5235cabbc6bSPrashanth Sreenivasa 		 * portion of it here.
5245cabbc6bSPrashanth Sreenivasa 		 */
5255cabbc6bSPrashanth Sreenivasa 		synced_size = MIN(size, max_offset_synced - offset);
5265cabbc6bSPrashanth Sreenivasa 		synced_offset = offset;
5275cabbc6bSPrashanth Sreenivasa 
5285cabbc6bSPrashanth Sreenivasa 		ASSERT3U(max_offset_yet, <=, max_offset_synced);
5295cabbc6bSPrashanth Sreenivasa 		max_offset_yet = max_offset_synced;
5305cabbc6bSPrashanth Sreenivasa 
5315cabbc6bSPrashanth Sreenivasa 		DTRACE_PROBE3(remove__free__synced,
5325cabbc6bSPrashanth Sreenivasa 		    spa_t *, spa,
5335cabbc6bSPrashanth Sreenivasa 		    uint64_t, offset,
5345cabbc6bSPrashanth Sreenivasa 		    uint64_t, synced_size);
5355cabbc6bSPrashanth Sreenivasa 
5365cabbc6bSPrashanth Sreenivasa 		size -= synced_size;
5375cabbc6bSPrashanth Sreenivasa 		offset += synced_size;
5385cabbc6bSPrashanth Sreenivasa 	}
5395cabbc6bSPrashanth Sreenivasa 
5405cabbc6bSPrashanth Sreenivasa 	/*
5415cabbc6bSPrashanth Sreenivasa 	 * Look at all in-flight txgs starting from the currently syncing one
5425cabbc6bSPrashanth Sreenivasa 	 * and see if a section of this free is being copied. By starting from
5435cabbc6bSPrashanth Sreenivasa 	 * this txg and iterating forward, we might find that this region
5445cabbc6bSPrashanth Sreenivasa 	 * was copied in two different txgs and handle it appropriately.
5455cabbc6bSPrashanth Sreenivasa 	 */
5465cabbc6bSPrashanth Sreenivasa 	for (int i = 0; i < TXG_CONCURRENT_STATES; i++) {
5475cabbc6bSPrashanth Sreenivasa 		int txgoff = (txg + i) & TXG_MASK;
5485cabbc6bSPrashanth Sreenivasa 		if (size > 0 && offset < svr->svr_max_offset_to_sync[txgoff]) {
5495cabbc6bSPrashanth Sreenivasa 			/*
5505cabbc6bSPrashanth Sreenivasa 			 * The mapping for this offset is in flight, and
5515cabbc6bSPrashanth Sreenivasa 			 * will be synced in txg+i.
5525cabbc6bSPrashanth Sreenivasa 			 */
5535cabbc6bSPrashanth Sreenivasa 			uint64_t inflight_size = MIN(size,
5545cabbc6bSPrashanth Sreenivasa 			    svr->svr_max_offset_to_sync[txgoff] - offset);
5555cabbc6bSPrashanth Sreenivasa 
5565cabbc6bSPrashanth Sreenivasa 			DTRACE_PROBE4(remove__free__inflight,
5575cabbc6bSPrashanth Sreenivasa 			    spa_t *, spa,
5585cabbc6bSPrashanth Sreenivasa 			    uint64_t, offset,
5595cabbc6bSPrashanth Sreenivasa 			    uint64_t, inflight_size,
5605cabbc6bSPrashanth Sreenivasa 			    uint64_t, txg + i);
5615cabbc6bSPrashanth Sreenivasa 
5625cabbc6bSPrashanth Sreenivasa 			/*
5635cabbc6bSPrashanth Sreenivasa 			 * We copy data in order of increasing offset.
5645cabbc6bSPrashanth Sreenivasa 			 * Therefore the max_offset_to_sync[] must increase
5655cabbc6bSPrashanth Sreenivasa 			 * (or be zero, indicating that nothing is being
5665cabbc6bSPrashanth Sreenivasa 			 * copied in that txg).
5675cabbc6bSPrashanth Sreenivasa 			 */
5685cabbc6bSPrashanth Sreenivasa 			if (svr->svr_max_offset_to_sync[txgoff] != 0) {
5695cabbc6bSPrashanth Sreenivasa 				ASSERT3U(svr->svr_max_offset_to_sync[txgoff],
5705cabbc6bSPrashanth Sreenivasa 				    >=, max_offset_yet);
5715cabbc6bSPrashanth Sreenivasa 				max_offset_yet =
5725cabbc6bSPrashanth Sreenivasa 				    svr->svr_max_offset_to_sync[txgoff];
5735cabbc6bSPrashanth Sreenivasa 			}
5745cabbc6bSPrashanth Sreenivasa 
5755cabbc6bSPrashanth Sreenivasa 			/*
5765cabbc6bSPrashanth Sreenivasa 			 * We've already committed to copying this segment:
5775cabbc6bSPrashanth Sreenivasa 			 * we have allocated space elsewhere in the pool for
5785cabbc6bSPrashanth Sreenivasa 			 * it and have an IO outstanding to copy the data. We
5795cabbc6bSPrashanth Sreenivasa 			 * cannot free the space before the copy has
5805cabbc6bSPrashanth Sreenivasa 			 * completed, or else the copy IO might overwrite any
5815cabbc6bSPrashanth Sreenivasa 			 * new data. To free that space, we record the
5825cabbc6bSPrashanth Sreenivasa 			 * segment in the appropriate svr_frees tree and free
5835cabbc6bSPrashanth Sreenivasa 			 * the mapped space later, in the txg where we have
5845cabbc6bSPrashanth Sreenivasa 			 * completed the copy and synced the mapping (see
5855cabbc6bSPrashanth Sreenivasa 			 * vdev_mapping_sync).
5865cabbc6bSPrashanth Sreenivasa 			 */
5875cabbc6bSPrashanth Sreenivasa 			range_tree_add(svr->svr_frees[txgoff],
5885cabbc6bSPrashanth Sreenivasa 			    offset, inflight_size);
5895cabbc6bSPrashanth Sreenivasa 			size -= inflight_size;
5905cabbc6bSPrashanth Sreenivasa 			offset += inflight_size;
5915cabbc6bSPrashanth Sreenivasa 
5925cabbc6bSPrashanth Sreenivasa 			/*
5935cabbc6bSPrashanth Sreenivasa 			 * This space is already accounted for as being
5945cabbc6bSPrashanth Sreenivasa 			 * done, because it is being copied in txg+i.
5955cabbc6bSPrashanth Sreenivasa 			 * However, if i!=0, then it is being copied in
5965cabbc6bSPrashanth Sreenivasa 			 * a future txg.  If we crash after this txg
5975cabbc6bSPrashanth Sreenivasa 			 * syncs but before txg+i syncs, then the space
5985cabbc6bSPrashanth Sreenivasa 			 * will be free.  Therefore we must account
5995cabbc6bSPrashanth Sreenivasa 			 * for the space being done in *this* txg
6005cabbc6bSPrashanth Sreenivasa 			 * (when it is freed) rather than the future txg
6015cabbc6bSPrashanth Sreenivasa 			 * (when it will be copied).
6025cabbc6bSPrashanth Sreenivasa 			 */
6035cabbc6bSPrashanth Sreenivasa 			ASSERT3U(svr->svr_bytes_done[txgoff], >=,
6045cabbc6bSPrashanth Sreenivasa 			    inflight_size);
6055cabbc6bSPrashanth Sreenivasa 			svr->svr_bytes_done[txgoff] -= inflight_size;
6065cabbc6bSPrashanth Sreenivasa 			svr->svr_bytes_done[txg & TXG_MASK] += inflight_size;
6075cabbc6bSPrashanth Sreenivasa 		}
6085cabbc6bSPrashanth Sreenivasa 	}
6095cabbc6bSPrashanth Sreenivasa 	ASSERT0(svr->svr_max_offset_to_sync[TXG_CLEAN(txg) & TXG_MASK]);
6105cabbc6bSPrashanth Sreenivasa 
6115cabbc6bSPrashanth Sreenivasa 	if (size > 0) {
6125cabbc6bSPrashanth Sreenivasa 		/*
6135cabbc6bSPrashanth Sreenivasa 		 * The copy thread has not yet visited this offset.  Ensure
6145cabbc6bSPrashanth Sreenivasa 		 * that it doesn't.
6155cabbc6bSPrashanth Sreenivasa 		 */
6165cabbc6bSPrashanth Sreenivasa 
6175cabbc6bSPrashanth Sreenivasa 		DTRACE_PROBE3(remove__free__unvisited,
6185cabbc6bSPrashanth Sreenivasa 		    spa_t *, spa,
6195cabbc6bSPrashanth Sreenivasa 		    uint64_t, offset,
6205cabbc6bSPrashanth Sreenivasa 		    uint64_t, size);
6215cabbc6bSPrashanth Sreenivasa 
6225cabbc6bSPrashanth Sreenivasa 		if (svr->svr_allocd_segs != NULL)
6235cabbc6bSPrashanth Sreenivasa 			range_tree_clear(svr->svr_allocd_segs, offset, size);
6245cabbc6bSPrashanth Sreenivasa 
6255cabbc6bSPrashanth Sreenivasa 		/*
6265cabbc6bSPrashanth Sreenivasa 		 * Since we now do not need to copy this data, for
6275cabbc6bSPrashanth Sreenivasa 		 * accounting purposes we have done our job and can count
6285cabbc6bSPrashanth Sreenivasa 		 * it as completed.
6295cabbc6bSPrashanth Sreenivasa 		 */
6305cabbc6bSPrashanth Sreenivasa 		svr->svr_bytes_done[txg & TXG_MASK] += size;
6315cabbc6bSPrashanth Sreenivasa 	}
6325cabbc6bSPrashanth Sreenivasa 	mutex_exit(&svr->svr_lock);
6335cabbc6bSPrashanth Sreenivasa 
6345cabbc6bSPrashanth Sreenivasa 	/*
6355cabbc6bSPrashanth Sreenivasa 	 * Now that we have dropped svr_lock, process the synced portion
6365cabbc6bSPrashanth Sreenivasa 	 * of this free.
6375cabbc6bSPrashanth Sreenivasa 	 */
6385cabbc6bSPrashanth Sreenivasa 	if (synced_size > 0) {
63986714001SSerapheim Dimitropoulos 		vdev_indirect_mark_obsolete(vd, synced_offset, synced_size);
64086714001SSerapheim Dimitropoulos 
6415cabbc6bSPrashanth Sreenivasa 		/*
6425cabbc6bSPrashanth Sreenivasa 		 * Note: this can only be called from syncing context,
6435cabbc6bSPrashanth Sreenivasa 		 * and the vdev_indirect_mapping is only changed from the
6445cabbc6bSPrashanth Sreenivasa 		 * sync thread, so we don't need svr_lock while doing
6455cabbc6bSPrashanth Sreenivasa 		 * metaslab_free_impl_cb.
6465cabbc6bSPrashanth Sreenivasa 		 */
64786714001SSerapheim Dimitropoulos 		boolean_t checkpoint = B_FALSE;
6485cabbc6bSPrashanth Sreenivasa 		vdev_indirect_ops.vdev_op_remap(vd, synced_offset, synced_size,
64986714001SSerapheim Dimitropoulos 		    metaslab_free_impl_cb, &checkpoint);
6505cabbc6bSPrashanth Sreenivasa 	}
6515cabbc6bSPrashanth Sreenivasa }
6525cabbc6bSPrashanth Sreenivasa 
6535cabbc6bSPrashanth Sreenivasa /*
6545cabbc6bSPrashanth Sreenivasa  * Stop an active removal and update the spa_removing phys.
6555cabbc6bSPrashanth Sreenivasa  */
6565cabbc6bSPrashanth Sreenivasa static void
6575cabbc6bSPrashanth Sreenivasa spa_finish_removal(spa_t *spa, dsl_scan_state_t state, dmu_tx_t *tx)
6585cabbc6bSPrashanth Sreenivasa {
6595cabbc6bSPrashanth Sreenivasa 	spa_vdev_removal_t *svr = spa->spa_vdev_removal;
6605cabbc6bSPrashanth Sreenivasa 	ASSERT3U(dmu_tx_get_txg(tx), ==, spa_syncing_txg(spa));
6615cabbc6bSPrashanth Sreenivasa 
6625cabbc6bSPrashanth Sreenivasa 	/* Ensure the removal thread has completed before we free the svr. */
6635cabbc6bSPrashanth Sreenivasa 	spa_vdev_remove_suspend(spa);
6645cabbc6bSPrashanth Sreenivasa 
6655cabbc6bSPrashanth Sreenivasa 	ASSERT(state == DSS_FINISHED || state == DSS_CANCELED);
6665cabbc6bSPrashanth Sreenivasa 
6675cabbc6bSPrashanth Sreenivasa 	if (state == DSS_FINISHED) {
6685cabbc6bSPrashanth Sreenivasa 		spa_removing_phys_t *srp = &spa->spa_removing_phys;
6693a4b1be9SMatthew Ahrens 		vdev_t *vd = vdev_lookup_top(spa, svr->svr_vdev_id);
6705cabbc6bSPrashanth Sreenivasa 		vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
6715cabbc6bSPrashanth Sreenivasa 
6725cabbc6bSPrashanth Sreenivasa 		if (srp->sr_prev_indirect_vdev != UINT64_MAX) {
6735cabbc6bSPrashanth Sreenivasa 			vdev_t *pvd = vdev_lookup_top(spa,
6745cabbc6bSPrashanth Sreenivasa 			    srp->sr_prev_indirect_vdev);
6755cabbc6bSPrashanth Sreenivasa 			ASSERT3P(pvd->vdev_ops, ==, &vdev_indirect_ops);
6765cabbc6bSPrashanth Sreenivasa 		}
6775cabbc6bSPrashanth Sreenivasa 
6785cabbc6bSPrashanth Sreenivasa 		vic->vic_prev_indirect_vdev = srp->sr_prev_indirect_vdev;
6795cabbc6bSPrashanth Sreenivasa 		srp->sr_prev_indirect_vdev = vd->vdev_id;
6805cabbc6bSPrashanth Sreenivasa 	}
6815cabbc6bSPrashanth Sreenivasa 	spa->spa_removing_phys.sr_state = state;
6825cabbc6bSPrashanth Sreenivasa 	spa->spa_removing_phys.sr_end_time = gethrestime_sec();
6835cabbc6bSPrashanth Sreenivasa 
6845cabbc6bSPrashanth Sreenivasa 	spa->spa_vdev_removal = NULL;
6855cabbc6bSPrashanth Sreenivasa 	spa_vdev_removal_destroy(svr);
6865cabbc6bSPrashanth Sreenivasa 
6875cabbc6bSPrashanth Sreenivasa 	spa_sync_removing_state(spa, tx);
6885cabbc6bSPrashanth Sreenivasa 
6895cabbc6bSPrashanth Sreenivasa 	vdev_config_dirty(spa->spa_root_vdev);
6905cabbc6bSPrashanth Sreenivasa }
6915cabbc6bSPrashanth Sreenivasa 
6925cabbc6bSPrashanth Sreenivasa static void
6935cabbc6bSPrashanth Sreenivasa free_mapped_segment_cb(void *arg, uint64_t offset, uint64_t size)
6945cabbc6bSPrashanth Sreenivasa {
6955cabbc6bSPrashanth Sreenivasa 	vdev_t *vd = arg;
69686714001SSerapheim Dimitropoulos 	vdev_indirect_mark_obsolete(vd, offset, size);
69786714001SSerapheim Dimitropoulos 	boolean_t checkpoint = B_FALSE;
6985cabbc6bSPrashanth Sreenivasa 	vdev_indirect_ops.vdev_op_remap(vd, offset, size,
69986714001SSerapheim Dimitropoulos 	    metaslab_free_impl_cb, &checkpoint);
7005cabbc6bSPrashanth Sreenivasa }
7015cabbc6bSPrashanth Sreenivasa 
7025cabbc6bSPrashanth Sreenivasa /*
7035cabbc6bSPrashanth Sreenivasa  * On behalf of the removal thread, syncs an incremental bit more of
7045cabbc6bSPrashanth Sreenivasa  * the indirect mapping to disk and updates the in-memory mapping.
7055cabbc6bSPrashanth Sreenivasa  * Called as a sync task in every txg that the removal thread makes progress.
7065cabbc6bSPrashanth Sreenivasa  */
7075cabbc6bSPrashanth Sreenivasa static void
7085cabbc6bSPrashanth Sreenivasa vdev_mapping_sync(void *arg, dmu_tx_t *tx)
7095cabbc6bSPrashanth Sreenivasa {
7105cabbc6bSPrashanth Sreenivasa 	spa_vdev_removal_t *svr = arg;
7115cabbc6bSPrashanth Sreenivasa 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
7123a4b1be9SMatthew Ahrens 	vdev_t *vd = vdev_lookup_top(spa, svr->svr_vdev_id);
7135cabbc6bSPrashanth Sreenivasa 	vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
7145cabbc6bSPrashanth Sreenivasa 	uint64_t txg = dmu_tx_get_txg(tx);
7155cabbc6bSPrashanth Sreenivasa 	vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
7165cabbc6bSPrashanth Sreenivasa 
7175cabbc6bSPrashanth Sreenivasa 	ASSERT(vic->vic_mapping_object != 0);
7185cabbc6bSPrashanth Sreenivasa 	ASSERT3U(txg, ==, spa_syncing_txg(spa));
7195cabbc6bSPrashanth Sreenivasa 
7205cabbc6bSPrashanth Sreenivasa 	vdev_indirect_mapping_add_entries(vim,
7215cabbc6bSPrashanth Sreenivasa 	    &svr->svr_new_segments[txg & TXG_MASK], tx);
7225cabbc6bSPrashanth Sreenivasa 	vdev_indirect_births_add_entry(vd->vdev_indirect_births,
7235cabbc6bSPrashanth Sreenivasa 	    vdev_indirect_mapping_max_offset(vim), dmu_tx_get_txg(tx), tx);
7245cabbc6bSPrashanth Sreenivasa 
7255cabbc6bSPrashanth Sreenivasa 	/*
7265cabbc6bSPrashanth Sreenivasa 	 * Free the copied data for anything that was freed while the
7275cabbc6bSPrashanth Sreenivasa 	 * mapping entries were in flight.
7285cabbc6bSPrashanth Sreenivasa 	 */
7295cabbc6bSPrashanth Sreenivasa 	mutex_enter(&svr->svr_lock);
7305cabbc6bSPrashanth Sreenivasa 	range_tree_vacate(svr->svr_frees[txg & TXG_MASK],
7315cabbc6bSPrashanth Sreenivasa 	    free_mapped_segment_cb, vd);
7325cabbc6bSPrashanth Sreenivasa 	ASSERT3U(svr->svr_max_offset_to_sync[txg & TXG_MASK], >=,
7335cabbc6bSPrashanth Sreenivasa 	    vdev_indirect_mapping_max_offset(vim));
7345cabbc6bSPrashanth Sreenivasa 	svr->svr_max_offset_to_sync[txg & TXG_MASK] = 0;
7355cabbc6bSPrashanth Sreenivasa 	mutex_exit(&svr->svr_lock);
7365cabbc6bSPrashanth Sreenivasa 
7375cabbc6bSPrashanth Sreenivasa 	spa_sync_removing_state(spa, tx);
7385cabbc6bSPrashanth Sreenivasa }
7395cabbc6bSPrashanth Sreenivasa 
740cfd63e1bSMatthew Ahrens typedef struct vdev_copy_segment_arg {
741cfd63e1bSMatthew Ahrens 	spa_t *vcsa_spa;
742cfd63e1bSMatthew Ahrens 	dva_t *vcsa_dest_dva;
743cfd63e1bSMatthew Ahrens 	uint64_t vcsa_txg;
744cfd63e1bSMatthew Ahrens 	range_tree_t *vcsa_obsolete_segs;
745cfd63e1bSMatthew Ahrens } vdev_copy_segment_arg_t;
746cfd63e1bSMatthew Ahrens 
747cfd63e1bSMatthew Ahrens static void
748cfd63e1bSMatthew Ahrens unalloc_seg(void *arg, uint64_t start, uint64_t size)
749cfd63e1bSMatthew Ahrens {
750cfd63e1bSMatthew Ahrens 	vdev_copy_segment_arg_t *vcsa = arg;
751cfd63e1bSMatthew Ahrens 	spa_t *spa = vcsa->vcsa_spa;
752cfd63e1bSMatthew Ahrens 	blkptr_t bp = { 0 };
753cfd63e1bSMatthew Ahrens 
754cfd63e1bSMatthew Ahrens 	BP_SET_BIRTH(&bp, TXG_INITIAL, TXG_INITIAL);
755cfd63e1bSMatthew Ahrens 	BP_SET_LSIZE(&bp, size);
756cfd63e1bSMatthew Ahrens 	BP_SET_PSIZE(&bp, size);
757cfd63e1bSMatthew Ahrens 	BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
758cfd63e1bSMatthew Ahrens 	BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_OFF);
759cfd63e1bSMatthew Ahrens 	BP_SET_TYPE(&bp, DMU_OT_NONE);
760cfd63e1bSMatthew Ahrens 	BP_SET_LEVEL(&bp, 0);
761cfd63e1bSMatthew Ahrens 	BP_SET_DEDUP(&bp, 0);
762cfd63e1bSMatthew Ahrens 	BP_SET_BYTEORDER(&bp, ZFS_HOST_BYTEORDER);
763cfd63e1bSMatthew Ahrens 
764cfd63e1bSMatthew Ahrens 	DVA_SET_VDEV(&bp.blk_dva[0], DVA_GET_VDEV(vcsa->vcsa_dest_dva));
765cfd63e1bSMatthew Ahrens 	DVA_SET_OFFSET(&bp.blk_dva[0],
766cfd63e1bSMatthew Ahrens 	    DVA_GET_OFFSET(vcsa->vcsa_dest_dva) + start);
767cfd63e1bSMatthew Ahrens 	DVA_SET_ASIZE(&bp.blk_dva[0], size);
768cfd63e1bSMatthew Ahrens 
769cfd63e1bSMatthew Ahrens 	zio_free(spa, vcsa->vcsa_txg, &bp);
770cfd63e1bSMatthew Ahrens }
771cfd63e1bSMatthew Ahrens 
7723a4b1be9SMatthew Ahrens /*
7733a4b1be9SMatthew Ahrens  * All reads and writes associated with a call to spa_vdev_copy_segment()
7743a4b1be9SMatthew Ahrens  * are done.
7753a4b1be9SMatthew Ahrens  */
7763a4b1be9SMatthew Ahrens static void
777cfd63e1bSMatthew Ahrens spa_vdev_copy_segment_done(zio_t *zio)
7783a4b1be9SMatthew Ahrens {
779cfd63e1bSMatthew Ahrens 	vdev_copy_segment_arg_t *vcsa = zio->io_private;
780cfd63e1bSMatthew Ahrens 
781cfd63e1bSMatthew Ahrens 	range_tree_vacate(vcsa->vcsa_obsolete_segs,
782cfd63e1bSMatthew Ahrens 	    unalloc_seg, vcsa);
783cfd63e1bSMatthew Ahrens 	range_tree_destroy(vcsa->vcsa_obsolete_segs);
784cfd63e1bSMatthew Ahrens 	kmem_free(vcsa, sizeof (*vcsa));
785cfd63e1bSMatthew Ahrens 
7863a4b1be9SMatthew Ahrens 	spa_config_exit(zio->io_spa, SCL_STATE, zio->io_spa);
7873a4b1be9SMatthew Ahrens }
7883a4b1be9SMatthew Ahrens 
7893a4b1be9SMatthew Ahrens /*
7903a4b1be9SMatthew Ahrens  * The write of the new location is done.
7913a4b1be9SMatthew Ahrens  */
7925cabbc6bSPrashanth Sreenivasa static void
7935cabbc6bSPrashanth Sreenivasa spa_vdev_copy_segment_write_done(zio_t *zio)
7945cabbc6bSPrashanth Sreenivasa {
7953a4b1be9SMatthew Ahrens 	vdev_copy_arg_t *vca = zio->io_private;
7963a4b1be9SMatthew Ahrens 
7975cabbc6bSPrashanth Sreenivasa 	abd_free(zio->io_abd);
7985cabbc6bSPrashanth Sreenivasa 
7995cabbc6bSPrashanth Sreenivasa 	mutex_enter(&vca->vca_lock);
8005cabbc6bSPrashanth Sreenivasa 	vca->vca_outstanding_bytes -= zio->io_size;
8015cabbc6bSPrashanth Sreenivasa 	cv_signal(&vca->vca_cv);
8025cabbc6bSPrashanth Sreenivasa 	mutex_exit(&vca->vca_lock);
8035cabbc6bSPrashanth Sreenivasa }
8045cabbc6bSPrashanth Sreenivasa 
8053a4b1be9SMatthew Ahrens /*
8063a4b1be9SMatthew Ahrens  * The read of the old location is done.  The parent zio is the write to
8073a4b1be9SMatthew Ahrens  * the new location.  Allow it to start.
8083a4b1be9SMatthew Ahrens  */
8095cabbc6bSPrashanth Sreenivasa static void
8105cabbc6bSPrashanth Sreenivasa spa_vdev_copy_segment_read_done(zio_t *zio)
8115cabbc6bSPrashanth Sreenivasa {
8123a4b1be9SMatthew Ahrens 	zio_nowait(zio_unique_parent(zio));
8135cabbc6bSPrashanth Sreenivasa }
8145cabbc6bSPrashanth Sreenivasa 
8153a4b1be9SMatthew Ahrens /*
8163a4b1be9SMatthew Ahrens  * If the old and new vdevs are mirrors, we will read both sides of the old
8173a4b1be9SMatthew Ahrens  * mirror, and write each copy to the corresponding side of the new mirror.
8183a4b1be9SMatthew Ahrens  * If the old and new vdevs have a different number of children, we will do
8193a4b1be9SMatthew Ahrens  * this as best as possible.  Since we aren't verifying checksums, this
8203a4b1be9SMatthew Ahrens  * ensures that as long as there's a good copy of the data, we'll have a
8213a4b1be9SMatthew Ahrens  * good copy after the removal, even if there's silent damage to one side
8223a4b1be9SMatthew Ahrens  * of the mirror. If we're removing a mirror that has some silent damage,
8233a4b1be9SMatthew Ahrens  * we'll have exactly the same damage in the new location (assuming that
8243a4b1be9SMatthew Ahrens  * the new location is also a mirror).
8253a4b1be9SMatthew Ahrens  *
8263a4b1be9SMatthew Ahrens  * We accomplish this by creating a tree of zio_t's, with as many writes as
8273a4b1be9SMatthew Ahrens  * there are "children" of the new vdev (a non-redundant vdev counts as one
8283a4b1be9SMatthew Ahrens  * child, a 2-way mirror has 2 children, etc). Each write has an associated
8293a4b1be9SMatthew Ahrens  * read from a child of the old vdev. Typically there will be the same
8303a4b1be9SMatthew Ahrens  * number of children of the old and new vdevs.  However, if there are more
8313a4b1be9SMatthew Ahrens  * children of the new vdev, some child(ren) of the old vdev will be issued
8323a4b1be9SMatthew Ahrens  * multiple reads.  If there are more children of the old vdev, some copies
8333a4b1be9SMatthew Ahrens  * will be dropped.
8343a4b1be9SMatthew Ahrens  *
8353a4b1be9SMatthew Ahrens  * For example, the tree of zio_t's for a 2-way mirror is:
8363a4b1be9SMatthew Ahrens  *
8373a4b1be9SMatthew Ahrens  *                            null
8383a4b1be9SMatthew Ahrens  *                           /    \
8393a4b1be9SMatthew Ahrens  *    write(new vdev, child 0)      write(new vdev, child 1)
8403a4b1be9SMatthew Ahrens  *      |                             |
8413a4b1be9SMatthew Ahrens  *    read(old vdev, child 0)       read(old vdev, child 1)
8423a4b1be9SMatthew Ahrens  *
8433a4b1be9SMatthew Ahrens  * Child zio's complete before their parents complete.  However, zio's
8443a4b1be9SMatthew Ahrens  * created with zio_vdev_child_io() may be issued before their children
8453a4b1be9SMatthew Ahrens  * complete.  In this case we need to make sure that the children (reads)
8463a4b1be9SMatthew Ahrens  * complete before the parents (writes) are *issued*.  We do this by not
8473a4b1be9SMatthew Ahrens  * calling zio_nowait() on each write until its corresponding read has
8483a4b1be9SMatthew Ahrens  * completed.
8493a4b1be9SMatthew Ahrens  *
8503a4b1be9SMatthew Ahrens  * The spa_config_lock must be held while zio's created by
8513a4b1be9SMatthew Ahrens  * zio_vdev_child_io() are in progress, to ensure that the vdev tree does
8523a4b1be9SMatthew Ahrens  * not change (e.g. due to a concurrent "zpool attach/detach"). The "null"
8533a4b1be9SMatthew Ahrens  * zio is needed to release the spa_config_lock after all the reads and
8543a4b1be9SMatthew Ahrens  * writes complete. (Note that we can't grab the config lock for each read,
8553a4b1be9SMatthew Ahrens  * because it is not reentrant - we could deadlock with a thread waiting
8563a4b1be9SMatthew Ahrens  * for a write lock.)
8573a4b1be9SMatthew Ahrens  */
8583a4b1be9SMatthew Ahrens static void
8593a4b1be9SMatthew Ahrens spa_vdev_copy_one_child(vdev_copy_arg_t *vca, zio_t *nzio,
8603a4b1be9SMatthew Ahrens     vdev_t *source_vd, uint64_t source_offset,
8613a4b1be9SMatthew Ahrens     vdev_t *dest_child_vd, uint64_t dest_offset, int dest_id, uint64_t size)
8623a4b1be9SMatthew Ahrens {
8633a4b1be9SMatthew Ahrens 	ASSERT3U(spa_config_held(nzio->io_spa, SCL_ALL, RW_READER), !=, 0);
8643a4b1be9SMatthew Ahrens 
8653a4b1be9SMatthew Ahrens 	mutex_enter(&vca->vca_lock);
8663a4b1be9SMatthew Ahrens 	vca->vca_outstanding_bytes += size;
8673a4b1be9SMatthew Ahrens 	mutex_exit(&vca->vca_lock);
8683a4b1be9SMatthew Ahrens 
8693a4b1be9SMatthew Ahrens 	abd_t *abd = abd_alloc_for_io(size, B_FALSE);
8703a4b1be9SMatthew Ahrens 
8713a4b1be9SMatthew Ahrens 	vdev_t *source_child_vd;
8723a4b1be9SMatthew Ahrens 	if (source_vd->vdev_ops == &vdev_mirror_ops && dest_id != -1) {
8733a4b1be9SMatthew Ahrens 		/*
8743a4b1be9SMatthew Ahrens 		 * Source and dest are both mirrors.  Copy from the same
8753a4b1be9SMatthew Ahrens 		 * child id as we are copying to (wrapping around if there
8763a4b1be9SMatthew Ahrens 		 * are more dest children than source children).
8773a4b1be9SMatthew Ahrens 		 */
8783a4b1be9SMatthew Ahrens 		source_child_vd =
8793a4b1be9SMatthew Ahrens 		    source_vd->vdev_child[dest_id % source_vd->vdev_children];
8803a4b1be9SMatthew Ahrens 	} else {
8813a4b1be9SMatthew Ahrens 		source_child_vd = source_vd;
8823a4b1be9SMatthew Ahrens 	}
8833a4b1be9SMatthew Ahrens 
8843a4b1be9SMatthew Ahrens 	zio_t *write_zio = zio_vdev_child_io(nzio, NULL,
8853a4b1be9SMatthew Ahrens 	    dest_child_vd, dest_offset, abd, size,
8863a4b1be9SMatthew Ahrens 	    ZIO_TYPE_WRITE, ZIO_PRIORITY_REMOVAL,
8873a4b1be9SMatthew Ahrens 	    ZIO_FLAG_CANFAIL,
8883a4b1be9SMatthew Ahrens 	    spa_vdev_copy_segment_write_done, vca);
8893a4b1be9SMatthew Ahrens 
8903a4b1be9SMatthew Ahrens 	zio_nowait(zio_vdev_child_io(write_zio, NULL,
8913a4b1be9SMatthew Ahrens 	    source_child_vd, source_offset, abd, size,
8923a4b1be9SMatthew Ahrens 	    ZIO_TYPE_READ, ZIO_PRIORITY_REMOVAL,
8933a4b1be9SMatthew Ahrens 	    ZIO_FLAG_CANFAIL,
8943a4b1be9SMatthew Ahrens 	    spa_vdev_copy_segment_read_done, vca));
8953a4b1be9SMatthew Ahrens }
8963a4b1be9SMatthew Ahrens 
8973a4b1be9SMatthew Ahrens /*
8983a4b1be9SMatthew Ahrens  * Allocate a new location for this segment, and create the zio_t's to
8993a4b1be9SMatthew Ahrens  * read from the old location and write to the new location.
9003a4b1be9SMatthew Ahrens  */
9015cabbc6bSPrashanth Sreenivasa static int
902cfd63e1bSMatthew Ahrens spa_vdev_copy_segment(vdev_t *vd, range_tree_t *segs,
903cfd63e1bSMatthew Ahrens     uint64_t maxalloc, uint64_t txg,
9045cabbc6bSPrashanth Sreenivasa     vdev_copy_arg_t *vca, zio_alloc_list_t *zal)
9055cabbc6bSPrashanth Sreenivasa {
9065cabbc6bSPrashanth Sreenivasa 	metaslab_group_t *mg = vd->vdev_mg;
9075cabbc6bSPrashanth Sreenivasa 	spa_t *spa = vd->vdev_spa;
9085cabbc6bSPrashanth Sreenivasa 	spa_vdev_removal_t *svr = spa->spa_vdev_removal;
9095cabbc6bSPrashanth Sreenivasa 	vdev_indirect_mapping_entry_t *entry;
9105cabbc6bSPrashanth Sreenivasa 	dva_t dst = { 0 };
911cfd63e1bSMatthew Ahrens 	uint64_t start = range_tree_min(segs);
9125cabbc6bSPrashanth Sreenivasa 
913cfd63e1bSMatthew Ahrens 	ASSERT3U(maxalloc, <=, SPA_MAXBLOCKSIZE);
914cfd63e1bSMatthew Ahrens 
915cfd63e1bSMatthew Ahrens 	uint64_t size = range_tree_span(segs);
916cfd63e1bSMatthew Ahrens 	if (range_tree_span(segs) > maxalloc) {
917cfd63e1bSMatthew Ahrens 		/*
918cfd63e1bSMatthew Ahrens 		 * We can't allocate all the segments.  Prefer to end
919cfd63e1bSMatthew Ahrens 		 * the allocation at the end of a segment, thus avoiding
920cfd63e1bSMatthew Ahrens 		 * additional split blocks.
921cfd63e1bSMatthew Ahrens 		 */
922cfd63e1bSMatthew Ahrens 		range_seg_t search;
923cfd63e1bSMatthew Ahrens 		avl_index_t where;
924cfd63e1bSMatthew Ahrens 		search.rs_start = start + maxalloc;
925cfd63e1bSMatthew Ahrens 		search.rs_end = search.rs_start;
926cfd63e1bSMatthew Ahrens 		range_seg_t *rs = avl_find(&segs->rt_root, &search, &where);
927cfd63e1bSMatthew Ahrens 		if (rs == NULL) {
928cfd63e1bSMatthew Ahrens 			rs = avl_nearest(&segs->rt_root, where, AVL_BEFORE);
929cfd63e1bSMatthew Ahrens 		} else {
930cfd63e1bSMatthew Ahrens 			rs = AVL_PREV(&segs->rt_root, rs);
931cfd63e1bSMatthew Ahrens 		}
932cfd63e1bSMatthew Ahrens 		if (rs != NULL) {
933cfd63e1bSMatthew Ahrens 			size = rs->rs_end - start;
934cfd63e1bSMatthew Ahrens 		} else {
935cfd63e1bSMatthew Ahrens 			/*
936cfd63e1bSMatthew Ahrens 			 * There are no segments that end before maxalloc.
937cfd63e1bSMatthew Ahrens 			 * I.e. the first segment is larger than maxalloc,
938cfd63e1bSMatthew Ahrens 			 * so we must split it.
939cfd63e1bSMatthew Ahrens 			 */
940cfd63e1bSMatthew Ahrens 			size = maxalloc;
941cfd63e1bSMatthew Ahrens 		}
942cfd63e1bSMatthew Ahrens 	}
943cfd63e1bSMatthew Ahrens 	ASSERT3U(size, <=, maxalloc);
9445cabbc6bSPrashanth Sreenivasa 
945f78cdc34SPaul Dagnelie 	/*
946663207adSDon Brady 	 * An allocation class might not have any remaining vdevs or space
947f78cdc34SPaul Dagnelie 	 */
948663207adSDon Brady 	metaslab_class_t *mc = mg->mg_class;
949663207adSDon Brady 	if (mc != spa_normal_class(spa) && mc->mc_groups <= 1)
950663207adSDon Brady 		mc = spa_normal_class(spa);
951663207adSDon Brady 	int error = metaslab_alloc_dva(spa, mc, size, &dst, 0, NULL, txg, 0,
952663207adSDon Brady 	    zal, 0);
953663207adSDon Brady 	if (error == ENOSPC && mc != spa_normal_class(spa)) {
954663207adSDon Brady 		error = metaslab_alloc_dva(spa, spa_normal_class(spa), size,
955663207adSDon Brady 		    &dst, 0, NULL, txg, 0, zal, 0);
956663207adSDon Brady 	}
9575cabbc6bSPrashanth Sreenivasa 	if (error != 0)
9585cabbc6bSPrashanth Sreenivasa 		return (error);
9595cabbc6bSPrashanth Sreenivasa 
960cfd63e1bSMatthew Ahrens 	/*
961cfd63e1bSMatthew Ahrens 	 * Determine the ranges that are not actually needed.  Offsets are
962cfd63e1bSMatthew Ahrens 	 * relative to the start of the range to be copied (i.e. relative to the
963cfd63e1bSMatthew Ahrens 	 * local variable "start").
964cfd63e1bSMatthew Ahrens 	 */
965cfd63e1bSMatthew Ahrens 	range_tree_t *obsolete_segs = range_tree_create(NULL, NULL);
966cfd63e1bSMatthew Ahrens 
967cfd63e1bSMatthew Ahrens 	range_seg_t *rs = avl_first(&segs->rt_root);
968cfd63e1bSMatthew Ahrens 	ASSERT3U(rs->rs_start, ==, start);
969cfd63e1bSMatthew Ahrens 	uint64_t prev_seg_end = rs->rs_end;
970cfd63e1bSMatthew Ahrens 	while ((rs = AVL_NEXT(&segs->rt_root, rs)) != NULL) {
971cfd63e1bSMatthew Ahrens 		if (rs->rs_start >= start + size) {
972cfd63e1bSMatthew Ahrens 			break;
973cfd63e1bSMatthew Ahrens 		} else {
974cfd63e1bSMatthew Ahrens 			range_tree_add(obsolete_segs,
975cfd63e1bSMatthew Ahrens 			    prev_seg_end - start,
976cfd63e1bSMatthew Ahrens 			    rs->rs_start - prev_seg_end);
977cfd63e1bSMatthew Ahrens 		}
978cfd63e1bSMatthew Ahrens 		prev_seg_end = rs->rs_end;
979cfd63e1bSMatthew Ahrens 	}
980cfd63e1bSMatthew Ahrens 	/* We don't end in the middle of an obsolete range */
981cfd63e1bSMatthew Ahrens 	ASSERT3U(start + size, <=, prev_seg_end);
982cfd63e1bSMatthew Ahrens 
983cfd63e1bSMatthew Ahrens 	range_tree_clear(segs, start, size);
984cfd63e1bSMatthew Ahrens 
9855cabbc6bSPrashanth Sreenivasa 	/*
9865cabbc6bSPrashanth Sreenivasa 	 * We can't have any padding of the allocated size, otherwise we will
9875cabbc6bSPrashanth Sreenivasa 	 * misunderstand what's allocated, and the size of the mapping.
9885cabbc6bSPrashanth Sreenivasa 	 * The caller ensures this will be true by passing in a size that is
9895cabbc6bSPrashanth Sreenivasa 	 * aligned to the worst (highest) ashift in the pool.
9905cabbc6bSPrashanth Sreenivasa 	 */
9915cabbc6bSPrashanth Sreenivasa 	ASSERT3U(DVA_GET_ASIZE(&dst), ==, size);
9925cabbc6bSPrashanth Sreenivasa 
9935cabbc6bSPrashanth Sreenivasa 	entry = kmem_zalloc(sizeof (vdev_indirect_mapping_entry_t), KM_SLEEP);
9945cabbc6bSPrashanth Sreenivasa 	DVA_MAPPING_SET_SRC_OFFSET(&entry->vime_mapping, start);
9955cabbc6bSPrashanth Sreenivasa 	entry->vime_mapping.vimep_dst = dst;
996cfd63e1bSMatthew Ahrens 	if (spa_feature_is_enabled(spa, SPA_FEATURE_OBSOLETE_COUNTS)) {
997cfd63e1bSMatthew Ahrens 		entry->vime_obsolete_count = range_tree_space(obsolete_segs);
998cfd63e1bSMatthew Ahrens 	}
999cfd63e1bSMatthew Ahrens 
1000cfd63e1bSMatthew Ahrens 	vdev_copy_segment_arg_t *vcsa = kmem_zalloc(sizeof (*vcsa), KM_SLEEP);
1001cfd63e1bSMatthew Ahrens 	vcsa->vcsa_dest_dva = &entry->vime_mapping.vimep_dst;
1002cfd63e1bSMatthew Ahrens 	vcsa->vcsa_obsolete_segs = obsolete_segs;
1003cfd63e1bSMatthew Ahrens 	vcsa->vcsa_spa = spa;
1004cfd63e1bSMatthew Ahrens 	vcsa->vcsa_txg = txg;
10055cabbc6bSPrashanth Sreenivasa 
10065cabbc6bSPrashanth Sreenivasa 	/*
10073a4b1be9SMatthew Ahrens 	 * See comment before spa_vdev_copy_one_child().
10085cabbc6bSPrashanth Sreenivasa 	 */
10093a4b1be9SMatthew Ahrens 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
10103a4b1be9SMatthew Ahrens 	zio_t *nzio = zio_null(spa->spa_txg_zio[txg & TXG_MASK], spa, NULL,
1011cfd63e1bSMatthew Ahrens 	    spa_vdev_copy_segment_done, vcsa, 0);
10123a4b1be9SMatthew Ahrens 	vdev_t *dest_vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dst));
10133a4b1be9SMatthew Ahrens 	if (dest_vd->vdev_ops == &vdev_mirror_ops) {
10143a4b1be9SMatthew Ahrens 		for (int i = 0; i < dest_vd->vdev_children; i++) {
10153a4b1be9SMatthew Ahrens 			vdev_t *child = dest_vd->vdev_child[i];
10163a4b1be9SMatthew Ahrens 			spa_vdev_copy_one_child(vca, nzio, vd, start,
10173a4b1be9SMatthew Ahrens 			    child, DVA_GET_OFFSET(&dst), i, size);
10183a4b1be9SMatthew Ahrens 		}
10193a4b1be9SMatthew Ahrens 	} else {
10203a4b1be9SMatthew Ahrens 		spa_vdev_copy_one_child(vca, nzio, vd, start,
10213a4b1be9SMatthew Ahrens 		    dest_vd, DVA_GET_OFFSET(&dst), -1, size);
10223a4b1be9SMatthew Ahrens 	}
10233a4b1be9SMatthew Ahrens 	zio_nowait(nzio);
10245cabbc6bSPrashanth Sreenivasa 
10255cabbc6bSPrashanth Sreenivasa 	list_insert_tail(&svr->svr_new_segments[txg & TXG_MASK], entry);
10265cabbc6bSPrashanth Sreenivasa 	ASSERT3U(start + size, <=, vd->vdev_ms_count << vd->vdev_ms_shift);
10275cabbc6bSPrashanth Sreenivasa 	vdev_dirty(vd, 0, NULL, txg);
10285cabbc6bSPrashanth Sreenivasa 
10295cabbc6bSPrashanth Sreenivasa 	return (0);
10305cabbc6bSPrashanth Sreenivasa }
10315cabbc6bSPrashanth Sreenivasa 
10325cabbc6bSPrashanth Sreenivasa /*
10335cabbc6bSPrashanth Sreenivasa  * Complete the removal of a toplevel vdev. This is called as a
10345cabbc6bSPrashanth Sreenivasa  * synctask in the same txg that we will sync out the new config (to the
10355cabbc6bSPrashanth Sreenivasa  * MOS object) which indicates that this vdev is indirect.
10365cabbc6bSPrashanth Sreenivasa  */
10375cabbc6bSPrashanth Sreenivasa static void
10385cabbc6bSPrashanth Sreenivasa vdev_remove_complete_sync(void *arg, dmu_tx_t *tx)
10395cabbc6bSPrashanth Sreenivasa {
10405cabbc6bSPrashanth Sreenivasa 	spa_vdev_removal_t *svr = arg;
10413a4b1be9SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
10423a4b1be9SMatthew Ahrens 	vdev_t *vd = vdev_lookup_top(spa, svr->svr_vdev_id);
10435cabbc6bSPrashanth Sreenivasa 
10445cabbc6bSPrashanth Sreenivasa 	ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops);
10455cabbc6bSPrashanth Sreenivasa 
10465cabbc6bSPrashanth Sreenivasa 	for (int i = 0; i < TXG_SIZE; i++) {
10475cabbc6bSPrashanth Sreenivasa 		ASSERT0(svr->svr_bytes_done[i]);
10485cabbc6bSPrashanth Sreenivasa 	}
10495cabbc6bSPrashanth Sreenivasa 
10505cabbc6bSPrashanth Sreenivasa 	ASSERT3U(spa->spa_removing_phys.sr_copied, ==,
10515cabbc6bSPrashanth Sreenivasa 	    spa->spa_removing_phys.sr_to_copy);
10525cabbc6bSPrashanth Sreenivasa 
10535cabbc6bSPrashanth Sreenivasa 	vdev_destroy_spacemaps(vd, tx);
10545cabbc6bSPrashanth Sreenivasa 
10555cabbc6bSPrashanth Sreenivasa 	/* destroy leaf zaps, if any */
10565cabbc6bSPrashanth Sreenivasa 	ASSERT3P(svr->svr_zaplist, !=, NULL);
10575cabbc6bSPrashanth Sreenivasa 	for (nvpair_t *pair = nvlist_next_nvpair(svr->svr_zaplist, NULL);
10585cabbc6bSPrashanth Sreenivasa 	    pair != NULL;
10595cabbc6bSPrashanth Sreenivasa 	    pair = nvlist_next_nvpair(svr->svr_zaplist, pair)) {
10605cabbc6bSPrashanth Sreenivasa 		vdev_destroy_unlink_zap(vd, fnvpair_value_uint64(pair), tx);
10615cabbc6bSPrashanth Sreenivasa 	}
10625cabbc6bSPrashanth Sreenivasa 	fnvlist_free(svr->svr_zaplist);
10635cabbc6bSPrashanth Sreenivasa 
10645cabbc6bSPrashanth Sreenivasa 	spa_finish_removal(dmu_tx_pool(tx)->dp_spa, DSS_FINISHED, tx);
10655cabbc6bSPrashanth Sreenivasa 	/* vd->vdev_path is not available here */
10665cabbc6bSPrashanth Sreenivasa 	spa_history_log_internal(spa, "vdev remove completed",  tx,
10675cabbc6bSPrashanth Sreenivasa 	    "%s vdev %llu", spa_name(spa), vd->vdev_id);
10685cabbc6bSPrashanth Sreenivasa }
10695cabbc6bSPrashanth Sreenivasa 
10705cabbc6bSPrashanth Sreenivasa static void
10715cabbc6bSPrashanth Sreenivasa vdev_remove_enlist_zaps(vdev_t *vd, nvlist_t *zlist)
10725cabbc6bSPrashanth Sreenivasa {
10735cabbc6bSPrashanth Sreenivasa 	ASSERT3P(zlist, !=, NULL);
10745cabbc6bSPrashanth Sreenivasa 	ASSERT3P(vd->vdev_ops, !=, &vdev_raidz_ops);
10755cabbc6bSPrashanth Sreenivasa 
10765cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_leaf_zap != 0) {
10775cabbc6bSPrashanth Sreenivasa 		char zkey[32];
10785cabbc6bSPrashanth Sreenivasa 		(void) snprintf(zkey, sizeof (zkey), "%s-%"PRIu64,
10795cabbc6bSPrashanth Sreenivasa 		    VDEV_REMOVAL_ZAP_OBJS, vd->vdev_leaf_zap);
10805cabbc6bSPrashanth Sreenivasa 		fnvlist_add_uint64(zlist, zkey, vd->vdev_leaf_zap);
10815cabbc6bSPrashanth Sreenivasa 	}
10825cabbc6bSPrashanth Sreenivasa 
10835cabbc6bSPrashanth Sreenivasa 	for (uint64_t id = 0; id < vd->vdev_children; id++) {
10845cabbc6bSPrashanth Sreenivasa 		vdev_remove_enlist_zaps(vd->vdev_child[id], zlist);
10855cabbc6bSPrashanth Sreenivasa 	}
10865cabbc6bSPrashanth Sreenivasa }
10875cabbc6bSPrashanth Sreenivasa 
10885cabbc6bSPrashanth Sreenivasa static void
10895cabbc6bSPrashanth Sreenivasa vdev_remove_replace_with_indirect(vdev_t *vd, uint64_t txg)
10905cabbc6bSPrashanth Sreenivasa {
10915cabbc6bSPrashanth Sreenivasa 	vdev_t *ivd;
10925cabbc6bSPrashanth Sreenivasa 	dmu_tx_t *tx;
10935cabbc6bSPrashanth Sreenivasa 	spa_t *spa = vd->vdev_spa;
10945cabbc6bSPrashanth Sreenivasa 	spa_vdev_removal_t *svr = spa->spa_vdev_removal;
10955cabbc6bSPrashanth Sreenivasa 
10965cabbc6bSPrashanth Sreenivasa 	/*
10975cabbc6bSPrashanth Sreenivasa 	 * First, build a list of leaf zaps to be destroyed.
10985cabbc6bSPrashanth Sreenivasa 	 * This is passed to the sync context thread,
10995cabbc6bSPrashanth Sreenivasa 	 * which does the actual unlinking.
11005cabbc6bSPrashanth Sreenivasa 	 */
11015cabbc6bSPrashanth Sreenivasa 	svr->svr_zaplist = fnvlist_alloc();
11025cabbc6bSPrashanth Sreenivasa 	vdev_remove_enlist_zaps(vd, svr->svr_zaplist);
11035cabbc6bSPrashanth Sreenivasa 
11045cabbc6bSPrashanth Sreenivasa 	ivd = vdev_add_parent(vd, &vdev_indirect_ops);
11053a4b1be9SMatthew Ahrens 	ivd->vdev_removing = 0;
11065cabbc6bSPrashanth Sreenivasa 
11075cabbc6bSPrashanth Sreenivasa 	vd->vdev_leaf_zap = 0;
11085cabbc6bSPrashanth Sreenivasa 
11095cabbc6bSPrashanth Sreenivasa 	vdev_remove_child(ivd, vd);
11105cabbc6bSPrashanth Sreenivasa 	vdev_compact_children(ivd);
11115cabbc6bSPrashanth Sreenivasa 
11125cabbc6bSPrashanth Sreenivasa 	ASSERT(!list_link_active(&vd->vdev_state_dirty_node));
11135cabbc6bSPrashanth Sreenivasa 
11145cabbc6bSPrashanth Sreenivasa 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
11155cabbc6bSPrashanth Sreenivasa 	dsl_sync_task_nowait(spa->spa_dsl_pool, vdev_remove_complete_sync, svr,
11165cabbc6bSPrashanth Sreenivasa 	    0, ZFS_SPACE_CHECK_NONE, tx);
11175cabbc6bSPrashanth Sreenivasa 	dmu_tx_commit(tx);
11185cabbc6bSPrashanth Sreenivasa 
11195cabbc6bSPrashanth Sreenivasa 	/*
11205cabbc6bSPrashanth Sreenivasa 	 * Indicate that this thread has exited.
11215cabbc6bSPrashanth Sreenivasa 	 * After this, we can not use svr.
11225cabbc6bSPrashanth Sreenivasa 	 */
11235cabbc6bSPrashanth Sreenivasa 	mutex_enter(&svr->svr_lock);
11245cabbc6bSPrashanth Sreenivasa 	svr->svr_thread = NULL;
11255cabbc6bSPrashanth Sreenivasa 	cv_broadcast(&svr->svr_cv);
11265cabbc6bSPrashanth Sreenivasa 	mutex_exit(&svr->svr_lock);
11275cabbc6bSPrashanth Sreenivasa }
11285cabbc6bSPrashanth Sreenivasa 
11295cabbc6bSPrashanth Sreenivasa /*
11305cabbc6bSPrashanth Sreenivasa  * Complete the removal of a toplevel vdev. This is called in open
11315cabbc6bSPrashanth Sreenivasa  * context by the removal thread after we have copied all vdev's data.
11325cabbc6bSPrashanth Sreenivasa  */
11335cabbc6bSPrashanth Sreenivasa static void
11343a4b1be9SMatthew Ahrens vdev_remove_complete(spa_t *spa)
11355cabbc6bSPrashanth Sreenivasa {
11365cabbc6bSPrashanth Sreenivasa 	uint64_t txg;
11375cabbc6bSPrashanth Sreenivasa 
11385cabbc6bSPrashanth Sreenivasa 	/*
11395cabbc6bSPrashanth Sreenivasa 	 * Wait for any deferred frees to be synced before we call
11405cabbc6bSPrashanth Sreenivasa 	 * vdev_metaslab_fini()
11415cabbc6bSPrashanth Sreenivasa 	 */
11425cabbc6bSPrashanth Sreenivasa 	txg_wait_synced(spa->spa_dsl_pool, 0);
11435cabbc6bSPrashanth Sreenivasa 	txg = spa_vdev_enter(spa);
11443a4b1be9SMatthew Ahrens 	vdev_t *vd = vdev_lookup_top(spa, spa->spa_vdev_removal->svr_vdev_id);
1145094e47e9SGeorge Wilson 	ASSERT3P(vd->vdev_initialize_thread, ==, NULL);
11463a4b1be9SMatthew Ahrens 
11473a4b1be9SMatthew Ahrens 	sysevent_t *ev = spa_event_create(spa, vd, NULL,
11483a4b1be9SMatthew Ahrens 	    ESC_ZFS_VDEV_REMOVE_DEV);
11493a4b1be9SMatthew Ahrens 
11505cabbc6bSPrashanth Sreenivasa 	zfs_dbgmsg("finishing device removal for vdev %llu in txg %llu",
11515cabbc6bSPrashanth Sreenivasa 	    vd->vdev_id, txg);
11525cabbc6bSPrashanth Sreenivasa 
11535cabbc6bSPrashanth Sreenivasa 	/*
11545cabbc6bSPrashanth Sreenivasa 	 * Discard allocation state.
11555cabbc6bSPrashanth Sreenivasa 	 */
11565cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_mg != NULL) {
11575cabbc6bSPrashanth Sreenivasa 		vdev_metaslab_fini(vd);
11585cabbc6bSPrashanth Sreenivasa 		metaslab_group_destroy(vd->vdev_mg);
11595cabbc6bSPrashanth Sreenivasa 		vd->vdev_mg = NULL;
11605cabbc6bSPrashanth Sreenivasa 	}
11615cabbc6bSPrashanth Sreenivasa 	ASSERT0(vd->vdev_stat.vs_space);
11625cabbc6bSPrashanth Sreenivasa 	ASSERT0(vd->vdev_stat.vs_dspace);
11635cabbc6bSPrashanth Sreenivasa 
11645cabbc6bSPrashanth Sreenivasa 	vdev_remove_replace_with_indirect(vd, txg);
11655cabbc6bSPrashanth Sreenivasa 
11665cabbc6bSPrashanth Sreenivasa 	/*
11675cabbc6bSPrashanth Sreenivasa 	 * We now release the locks, allowing spa_sync to run and finish the
11685cabbc6bSPrashanth Sreenivasa 	 * removal via vdev_remove_complete_sync in syncing context.
11693a4b1be9SMatthew Ahrens 	 *
11703a4b1be9SMatthew Ahrens 	 * Note that we hold on to the vdev_t that has been replaced.  Since
11713a4b1be9SMatthew Ahrens 	 * it isn't part of the vdev tree any longer, it can't be concurrently
11723a4b1be9SMatthew Ahrens 	 * manipulated, even while we don't have the config lock.
11735cabbc6bSPrashanth Sreenivasa 	 */
11745cabbc6bSPrashanth Sreenivasa 	(void) spa_vdev_exit(spa, NULL, txg, 0);
11755cabbc6bSPrashanth Sreenivasa 
11765cabbc6bSPrashanth Sreenivasa 	/*
11775cabbc6bSPrashanth Sreenivasa 	 * Top ZAP should have been transferred to the indirect vdev in
11785cabbc6bSPrashanth Sreenivasa 	 * vdev_remove_replace_with_indirect.
11795cabbc6bSPrashanth Sreenivasa 	 */
11805cabbc6bSPrashanth Sreenivasa 	ASSERT0(vd->vdev_top_zap);
11815cabbc6bSPrashanth Sreenivasa 
11825cabbc6bSPrashanth Sreenivasa 	/*
11835cabbc6bSPrashanth Sreenivasa 	 * Leaf ZAP should have been moved in vdev_remove_replace_with_indirect.
11845cabbc6bSPrashanth Sreenivasa 	 */
11855cabbc6bSPrashanth Sreenivasa 	ASSERT0(vd->vdev_leaf_zap);
11865cabbc6bSPrashanth Sreenivasa 
11875cabbc6bSPrashanth Sreenivasa 	txg = spa_vdev_enter(spa);
11885cabbc6bSPrashanth Sreenivasa 	(void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
11895cabbc6bSPrashanth Sreenivasa 	/*
11905cabbc6bSPrashanth Sreenivasa 	 * Request to update the config and the config cachefile.
11915cabbc6bSPrashanth Sreenivasa 	 */
11925cabbc6bSPrashanth Sreenivasa 	vdev_config_dirty(spa->spa_root_vdev);
11935cabbc6bSPrashanth Sreenivasa 	(void) spa_vdev_exit(spa, vd, txg, 0);
11943a4b1be9SMatthew Ahrens 
11953a4b1be9SMatthew Ahrens 	spa_event_post(ev);
11965cabbc6bSPrashanth Sreenivasa }
11975cabbc6bSPrashanth Sreenivasa 
11985cabbc6bSPrashanth Sreenivasa /*
11995cabbc6bSPrashanth Sreenivasa  * Evacuates a segment of size at most max_alloc from the vdev
12005cabbc6bSPrashanth Sreenivasa  * via repeated calls to spa_vdev_copy_segment. If an allocation
12015cabbc6bSPrashanth Sreenivasa  * fails, the pool is probably too fragmented to handle such a
12025cabbc6bSPrashanth Sreenivasa  * large size, so decrease max_alloc so that the caller will not try
12035cabbc6bSPrashanth Sreenivasa  * this size again this txg.
12045cabbc6bSPrashanth Sreenivasa  */
12055cabbc6bSPrashanth Sreenivasa static void
12063a4b1be9SMatthew Ahrens spa_vdev_copy_impl(vdev_t *vd, spa_vdev_removal_t *svr, vdev_copy_arg_t *vca,
12075cabbc6bSPrashanth Sreenivasa     uint64_t *max_alloc, dmu_tx_t *tx)
12085cabbc6bSPrashanth Sreenivasa {
12095cabbc6bSPrashanth Sreenivasa 	uint64_t txg = dmu_tx_get_txg(tx);
12105cabbc6bSPrashanth Sreenivasa 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
12115cabbc6bSPrashanth Sreenivasa 
12125cabbc6bSPrashanth Sreenivasa 	mutex_enter(&svr->svr_lock);
12135cabbc6bSPrashanth Sreenivasa 
1214cfd63e1bSMatthew Ahrens 	/*
1215cfd63e1bSMatthew Ahrens 	 * Determine how big of a chunk to copy.  We can allocate up
1216cfd63e1bSMatthew Ahrens 	 * to max_alloc bytes, and we can span up to vdev_removal_max_span
1217cfd63e1bSMatthew Ahrens 	 * bytes of unallocated space at a time.  "segs" will track the
1218cfd63e1bSMatthew Ahrens 	 * allocated segments that we are copying.  We may also be copying
1219cfd63e1bSMatthew Ahrens 	 * free segments (of up to vdev_removal_max_span bytes).
1220cfd63e1bSMatthew Ahrens 	 */
1221cfd63e1bSMatthew Ahrens 	range_tree_t *segs = range_tree_create(NULL, NULL);
1222cfd63e1bSMatthew Ahrens 	for (;;) {
1223cfd63e1bSMatthew Ahrens 		range_seg_t *rs = avl_first(&svr->svr_allocd_segs->rt_root);
1224cfd63e1bSMatthew Ahrens 		if (rs == NULL)
1225cfd63e1bSMatthew Ahrens 			break;
1226cfd63e1bSMatthew Ahrens 
1227cfd63e1bSMatthew Ahrens 		uint64_t seg_length;
1228cfd63e1bSMatthew Ahrens 
1229cfd63e1bSMatthew Ahrens 		if (range_tree_is_empty(segs)) {
1230cfd63e1bSMatthew Ahrens 			/* need to truncate the first seg based on max_alloc */
1231cfd63e1bSMatthew Ahrens 			seg_length =
1232cfd63e1bSMatthew Ahrens 			    MIN(rs->rs_end - rs->rs_start, *max_alloc);
1233cfd63e1bSMatthew Ahrens 		} else {
1234cfd63e1bSMatthew Ahrens 			if (rs->rs_start - range_tree_max(segs) >
1235cfd63e1bSMatthew Ahrens 			    vdev_removal_max_span) {
1236cfd63e1bSMatthew Ahrens 				/*
1237cfd63e1bSMatthew Ahrens 				 * Including this segment would cause us to
1238cfd63e1bSMatthew Ahrens 				 * copy a larger unneeded chunk than is allowed.
1239cfd63e1bSMatthew Ahrens 				 */
1240cfd63e1bSMatthew Ahrens 				break;
1241cfd63e1bSMatthew Ahrens 			} else if (rs->rs_end - range_tree_min(segs) >
1242cfd63e1bSMatthew Ahrens 			    *max_alloc) {
1243cfd63e1bSMatthew Ahrens 				/*
1244cfd63e1bSMatthew Ahrens 				 * This additional segment would extend past
1245cfd63e1bSMatthew Ahrens 				 * max_alloc. Rather than splitting this
1246cfd63e1bSMatthew Ahrens 				 * segment, leave it for the next mapping.
1247cfd63e1bSMatthew Ahrens 				 */
1248cfd63e1bSMatthew Ahrens 				break;
1249cfd63e1bSMatthew Ahrens 			} else {
1250cfd63e1bSMatthew Ahrens 				seg_length = rs->rs_end - rs->rs_start;
1251cfd63e1bSMatthew Ahrens 			}
1252cfd63e1bSMatthew Ahrens 		}
1253cfd63e1bSMatthew Ahrens 
1254cfd63e1bSMatthew Ahrens 		range_tree_add(segs, rs->rs_start, seg_length);
1255cfd63e1bSMatthew Ahrens 		range_tree_remove(svr->svr_allocd_segs,
1256cfd63e1bSMatthew Ahrens 		    rs->rs_start, seg_length);
1257cfd63e1bSMatthew Ahrens 	}
1258cfd63e1bSMatthew Ahrens 
1259cfd63e1bSMatthew Ahrens 	if (range_tree_is_empty(segs)) {
12605cabbc6bSPrashanth Sreenivasa 		mutex_exit(&svr->svr_lock);
1261cfd63e1bSMatthew Ahrens 		range_tree_destroy(segs);
12625cabbc6bSPrashanth Sreenivasa 		return;
12635cabbc6bSPrashanth Sreenivasa 	}
12645cabbc6bSPrashanth Sreenivasa 
12655cabbc6bSPrashanth Sreenivasa 	if (svr->svr_max_offset_to_sync[txg & TXG_MASK] == 0) {
12665cabbc6bSPrashanth Sreenivasa 		dsl_sync_task_nowait(dmu_tx_pool(tx), vdev_mapping_sync,
12675cabbc6bSPrashanth Sreenivasa 		    svr, 0, ZFS_SPACE_CHECK_NONE, tx);
12685cabbc6bSPrashanth Sreenivasa 	}
12695cabbc6bSPrashanth Sreenivasa 
1270cfd63e1bSMatthew Ahrens 	svr->svr_max_offset_to_sync[txg & TXG_MASK] = range_tree_max(segs);
12715cabbc6bSPrashanth Sreenivasa 
12725cabbc6bSPrashanth Sreenivasa 	/*
12735cabbc6bSPrashanth Sreenivasa 	 * Note: this is the amount of *allocated* space
12745cabbc6bSPrashanth Sreenivasa 	 * that we are taking care of each txg.
12755cabbc6bSPrashanth Sreenivasa 	 */
1276cfd63e1bSMatthew Ahrens 	svr->svr_bytes_done[txg & TXG_MASK] += range_tree_space(segs);
12775cabbc6bSPrashanth Sreenivasa 
12785cabbc6bSPrashanth Sreenivasa 	mutex_exit(&svr->svr_lock);
12795cabbc6bSPrashanth Sreenivasa 
12805cabbc6bSPrashanth Sreenivasa 	zio_alloc_list_t zal;
12815cabbc6bSPrashanth Sreenivasa 	metaslab_trace_init(&zal);
1282cfd63e1bSMatthew Ahrens 	uint64_t thismax = SPA_MAXBLOCKSIZE;
1283cfd63e1bSMatthew Ahrens 	while (!range_tree_is_empty(segs)) {
12843a4b1be9SMatthew Ahrens 		int error = spa_vdev_copy_segment(vd,
1285cfd63e1bSMatthew Ahrens 		    segs, thismax, txg, vca, &zal);
12865cabbc6bSPrashanth Sreenivasa 
12875cabbc6bSPrashanth Sreenivasa 		if (error == ENOSPC) {
12885cabbc6bSPrashanth Sreenivasa 			/*
12895cabbc6bSPrashanth Sreenivasa 			 * Cut our segment in half, and don't try this
12905cabbc6bSPrashanth Sreenivasa 			 * segment size again this txg.  Note that the
12915cabbc6bSPrashanth Sreenivasa 			 * allocation size must be aligned to the highest
12925cabbc6bSPrashanth Sreenivasa 			 * ashift in the pool, so that the allocation will
12935cabbc6bSPrashanth Sreenivasa 			 * not be padded out to a multiple of the ashift,
12945cabbc6bSPrashanth Sreenivasa 			 * which could cause us to think that this mapping
12955cabbc6bSPrashanth Sreenivasa 			 * is larger than we intended.
12965cabbc6bSPrashanth Sreenivasa 			 */
12975cabbc6bSPrashanth Sreenivasa 			ASSERT3U(spa->spa_max_ashift, >=, SPA_MINBLOCKSHIFT);
12985cabbc6bSPrashanth Sreenivasa 			ASSERT3U(spa->spa_max_ashift, ==, spa->spa_min_ashift);
1299cfd63e1bSMatthew Ahrens 			uint64_t attempted =
1300cfd63e1bSMatthew Ahrens 			    MIN(range_tree_span(segs), thismax);
1301cfd63e1bSMatthew Ahrens 			thismax = P2ROUNDUP(attempted / 2,
13025cabbc6bSPrashanth Sreenivasa 			    1 << spa->spa_max_ashift);
13035cabbc6bSPrashanth Sreenivasa 			/*
13045cabbc6bSPrashanth Sreenivasa 			 * The minimum-size allocation can not fail.
13055cabbc6bSPrashanth Sreenivasa 			 */
1306cfd63e1bSMatthew Ahrens 			ASSERT3U(attempted, >, 1 << spa->spa_max_ashift);
1307cfd63e1bSMatthew Ahrens 			*max_alloc = attempted - (1 << spa->spa_max_ashift);
13085cabbc6bSPrashanth Sreenivasa 		} else {
13095cabbc6bSPrashanth Sreenivasa 			ASSERT0(error);
13105cabbc6bSPrashanth Sreenivasa 
13115cabbc6bSPrashanth Sreenivasa 			/*
13125cabbc6bSPrashanth Sreenivasa 			 * We've performed an allocation, so reset the
13135cabbc6bSPrashanth Sreenivasa 			 * alloc trace list.
13145cabbc6bSPrashanth Sreenivasa 			 */
13155cabbc6bSPrashanth Sreenivasa 			metaslab_trace_fini(&zal);
13165cabbc6bSPrashanth Sreenivasa 			metaslab_trace_init(&zal);
13175cabbc6bSPrashanth Sreenivasa 		}
13185cabbc6bSPrashanth Sreenivasa 	}
13195cabbc6bSPrashanth Sreenivasa 	metaslab_trace_fini(&zal);
1320cfd63e1bSMatthew Ahrens 	range_tree_destroy(segs);
13215cabbc6bSPrashanth Sreenivasa }
13225cabbc6bSPrashanth Sreenivasa 
13235cabbc6bSPrashanth Sreenivasa /*
13245cabbc6bSPrashanth Sreenivasa  * The removal thread operates in open context.  It iterates over all
13255cabbc6bSPrashanth Sreenivasa  * allocated space in the vdev, by loading each metaslab's spacemap.
13265cabbc6bSPrashanth Sreenivasa  * For each contiguous segment of allocated space (capping the segment
13275cabbc6bSPrashanth Sreenivasa  * size at SPA_MAXBLOCKSIZE), we:
13285cabbc6bSPrashanth Sreenivasa  *    - Allocate space for it on another vdev.
13295cabbc6bSPrashanth Sreenivasa  *    - Create a new mapping from the old location to the new location
13305cabbc6bSPrashanth Sreenivasa  *      (as a record in svr_new_segments).
13315cabbc6bSPrashanth Sreenivasa  *    - Initiate a logical read zio to get the data off the removing disk.
13325cabbc6bSPrashanth Sreenivasa  *    - In the read zio's done callback, initiate a logical write zio to
13335cabbc6bSPrashanth Sreenivasa  *      write it to the new vdev.
13345cabbc6bSPrashanth Sreenivasa  * Note that all of this will take effect when a particular TXG syncs.
13355cabbc6bSPrashanth Sreenivasa  * The sync thread ensures that all the phys reads and writes for the syncing
13365cabbc6bSPrashanth Sreenivasa  * TXG have completed (see spa_txg_zio) and writes the new mappings to disk
13375cabbc6bSPrashanth Sreenivasa  * (see vdev_mapping_sync()).
13385cabbc6bSPrashanth Sreenivasa  */
13395cabbc6bSPrashanth Sreenivasa static void
13405cabbc6bSPrashanth Sreenivasa spa_vdev_remove_thread(void *arg)
13415cabbc6bSPrashanth Sreenivasa {
13423a4b1be9SMatthew Ahrens 	spa_t *spa = arg;
13435cabbc6bSPrashanth Sreenivasa 	spa_vdev_removal_t *svr = spa->spa_vdev_removal;
13445cabbc6bSPrashanth Sreenivasa 	vdev_copy_arg_t vca;
13455cabbc6bSPrashanth Sreenivasa 	uint64_t max_alloc = zfs_remove_max_segment;
13465cabbc6bSPrashanth Sreenivasa 	uint64_t last_txg = 0;
13473a4b1be9SMatthew Ahrens 
13483a4b1be9SMatthew Ahrens 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
13493a4b1be9SMatthew Ahrens 	vdev_t *vd = vdev_lookup_top(spa, svr->svr_vdev_id);
13505cabbc6bSPrashanth Sreenivasa 	vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
13515cabbc6bSPrashanth Sreenivasa 	uint64_t start_offset = vdev_indirect_mapping_max_offset(vim);
13525cabbc6bSPrashanth Sreenivasa 
13535cabbc6bSPrashanth Sreenivasa 	ASSERT3P(vd->vdev_ops, !=, &vdev_indirect_ops);
13545cabbc6bSPrashanth Sreenivasa 	ASSERT(vdev_is_concrete(vd));
13555cabbc6bSPrashanth Sreenivasa 	ASSERT(vd->vdev_removing);
13565cabbc6bSPrashanth Sreenivasa 	ASSERT(vd->vdev_indirect_config.vic_mapping_object != 0);
13575cabbc6bSPrashanth Sreenivasa 	ASSERT(vim != NULL);
13585cabbc6bSPrashanth Sreenivasa 
13595cabbc6bSPrashanth Sreenivasa 	mutex_init(&vca.vca_lock, NULL, MUTEX_DEFAULT, NULL);
13605cabbc6bSPrashanth Sreenivasa 	cv_init(&vca.vca_cv, NULL, CV_DEFAULT, NULL);
13615cabbc6bSPrashanth Sreenivasa 	vca.vca_outstanding_bytes = 0;
13625cabbc6bSPrashanth Sreenivasa 
13635cabbc6bSPrashanth Sreenivasa 	mutex_enter(&svr->svr_lock);
13645cabbc6bSPrashanth Sreenivasa 
13655cabbc6bSPrashanth Sreenivasa 	/*
13665cabbc6bSPrashanth Sreenivasa 	 * Start from vim_max_offset so we pick up where we left off
13675cabbc6bSPrashanth Sreenivasa 	 * if we are restarting the removal after opening the pool.
13685cabbc6bSPrashanth Sreenivasa 	 */
13695cabbc6bSPrashanth Sreenivasa 	uint64_t msi;
13705cabbc6bSPrashanth Sreenivasa 	for (msi = start_offset >> vd->vdev_ms_shift;
13715cabbc6bSPrashanth Sreenivasa 	    msi < vd->vdev_ms_count && !svr->svr_thread_exit; msi++) {
13725cabbc6bSPrashanth Sreenivasa 		metaslab_t *msp = vd->vdev_ms[msi];
13735cabbc6bSPrashanth Sreenivasa 		ASSERT3U(msi, <=, vd->vdev_ms_count);
13745cabbc6bSPrashanth Sreenivasa 
13755cabbc6bSPrashanth Sreenivasa 		ASSERT0(range_tree_space(svr->svr_allocd_segs));
13765cabbc6bSPrashanth Sreenivasa 
13775cabbc6bSPrashanth Sreenivasa 		mutex_enter(&msp->ms_sync_lock);
13785cabbc6bSPrashanth Sreenivasa 		mutex_enter(&msp->ms_lock);
13795cabbc6bSPrashanth Sreenivasa 
13805cabbc6bSPrashanth Sreenivasa 		/*
13815cabbc6bSPrashanth Sreenivasa 		 * Assert nothing in flight -- ms_*tree is empty.
13825cabbc6bSPrashanth Sreenivasa 		 */
13835cabbc6bSPrashanth Sreenivasa 		for (int i = 0; i < TXG_SIZE; i++) {
138486714001SSerapheim Dimitropoulos 			ASSERT0(range_tree_space(msp->ms_allocating[i]));
13855cabbc6bSPrashanth Sreenivasa 		}
13865cabbc6bSPrashanth Sreenivasa 
13875cabbc6bSPrashanth Sreenivasa 		/*
13885cabbc6bSPrashanth Sreenivasa 		 * If the metaslab has ever been allocated from (ms_sm!=NULL),
13895cabbc6bSPrashanth Sreenivasa 		 * read the allocated segments from the space map object
13905cabbc6bSPrashanth Sreenivasa 		 * into svr_allocd_segs. Since we do this while holding
13915cabbc6bSPrashanth Sreenivasa 		 * svr_lock and ms_sync_lock, concurrent frees (which
13925cabbc6bSPrashanth Sreenivasa 		 * would have modified the space map) will wait for us
13935cabbc6bSPrashanth Sreenivasa 		 * to finish loading the spacemap, and then take the
13945cabbc6bSPrashanth Sreenivasa 		 * appropriate action (see free_from_removing_vdev()).
13955cabbc6bSPrashanth Sreenivasa 		 */
13965cabbc6bSPrashanth Sreenivasa 		if (msp->ms_sm != NULL) {
1397*555d674dSSerapheim Dimitropoulos 			VERIFY0(space_map_load(msp->ms_sm,
1398*555d674dSSerapheim Dimitropoulos 			    svr->svr_allocd_segs, SM_ALLOC));
13995cabbc6bSPrashanth Sreenivasa 
140086714001SSerapheim Dimitropoulos 			range_tree_walk(msp->ms_freeing,
14015cabbc6bSPrashanth Sreenivasa 			    range_tree_remove, svr->svr_allocd_segs);
14025cabbc6bSPrashanth Sreenivasa 
14035cabbc6bSPrashanth Sreenivasa 			/*
14045cabbc6bSPrashanth Sreenivasa 			 * When we are resuming from a paused removal (i.e.
14055cabbc6bSPrashanth Sreenivasa 			 * when importing a pool with a removal in progress),
14065cabbc6bSPrashanth Sreenivasa 			 * discard any state that we have already processed.
14075cabbc6bSPrashanth Sreenivasa 			 */
14085cabbc6bSPrashanth Sreenivasa 			range_tree_clear(svr->svr_allocd_segs, 0, start_offset);
14095cabbc6bSPrashanth Sreenivasa 		}
14105cabbc6bSPrashanth Sreenivasa 		mutex_exit(&msp->ms_lock);
14115cabbc6bSPrashanth Sreenivasa 		mutex_exit(&msp->ms_sync_lock);
14125cabbc6bSPrashanth Sreenivasa 
14135cabbc6bSPrashanth Sreenivasa 		vca.vca_msp = msp;
14145cabbc6bSPrashanth Sreenivasa 		zfs_dbgmsg("copying %llu segments for metaslab %llu",
14155cabbc6bSPrashanth Sreenivasa 		    avl_numnodes(&svr->svr_allocd_segs->rt_root),
14165cabbc6bSPrashanth Sreenivasa 		    msp->ms_id);
14175cabbc6bSPrashanth Sreenivasa 
14185cabbc6bSPrashanth Sreenivasa 		while (!svr->svr_thread_exit &&
141986714001SSerapheim Dimitropoulos 		    !range_tree_is_empty(svr->svr_allocd_segs)) {
14205cabbc6bSPrashanth Sreenivasa 
14215cabbc6bSPrashanth Sreenivasa 			mutex_exit(&svr->svr_lock);
14225cabbc6bSPrashanth Sreenivasa 
14233a4b1be9SMatthew Ahrens 			/*
14243a4b1be9SMatthew Ahrens 			 * We need to periodically drop the config lock so that
14253a4b1be9SMatthew Ahrens 			 * writers can get in.  Additionally, we can't wait
14263a4b1be9SMatthew Ahrens 			 * for a txg to sync while holding a config lock
14273a4b1be9SMatthew Ahrens 			 * (since a waiting writer could cause a 3-way deadlock
14283a4b1be9SMatthew Ahrens 			 * with the sync thread, which also gets a config
14293a4b1be9SMatthew Ahrens 			 * lock for reader).  So we can't hold the config lock
14303a4b1be9SMatthew Ahrens 			 * while calling dmu_tx_assign().
14313a4b1be9SMatthew Ahrens 			 */
14323a4b1be9SMatthew Ahrens 			spa_config_exit(spa, SCL_CONFIG, FTAG);
14333a4b1be9SMatthew Ahrens 
143486714001SSerapheim Dimitropoulos 			/*
143586714001SSerapheim Dimitropoulos 			 * This delay will pause the removal around the point
143686714001SSerapheim Dimitropoulos 			 * specified by zfs_remove_max_bytes_pause. We do this
143786714001SSerapheim Dimitropoulos 			 * solely from the test suite or during debugging.
143886714001SSerapheim Dimitropoulos 			 */
143986714001SSerapheim Dimitropoulos 			uint64_t bytes_copied =
144086714001SSerapheim Dimitropoulos 			    spa->spa_removing_phys.sr_copied;
144186714001SSerapheim Dimitropoulos 			for (int i = 0; i < TXG_SIZE; i++)
144286714001SSerapheim Dimitropoulos 				bytes_copied += svr->svr_bytes_done[i];
144386714001SSerapheim Dimitropoulos 			while (zfs_remove_max_bytes_pause <= bytes_copied &&
144486714001SSerapheim Dimitropoulos 			    !svr->svr_thread_exit)
144586714001SSerapheim Dimitropoulos 				delay(hz);
144686714001SSerapheim Dimitropoulos 
14475cabbc6bSPrashanth Sreenivasa 			mutex_enter(&vca.vca_lock);
14485cabbc6bSPrashanth Sreenivasa 			while (vca.vca_outstanding_bytes >
14495cabbc6bSPrashanth Sreenivasa 			    zfs_remove_max_copy_bytes) {
14505cabbc6bSPrashanth Sreenivasa 				cv_wait(&vca.vca_cv, &vca.vca_lock);
14515cabbc6bSPrashanth Sreenivasa 			}
14525cabbc6bSPrashanth Sreenivasa 			mutex_exit(&vca.vca_lock);
14535cabbc6bSPrashanth Sreenivasa 
14545cabbc6bSPrashanth Sreenivasa 			dmu_tx_t *tx =
14555cabbc6bSPrashanth Sreenivasa 			    dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
14565cabbc6bSPrashanth Sreenivasa 
14575cabbc6bSPrashanth Sreenivasa 			VERIFY0(dmu_tx_assign(tx, TXG_WAIT));
14585cabbc6bSPrashanth Sreenivasa 			uint64_t txg = dmu_tx_get_txg(tx);
14595cabbc6bSPrashanth Sreenivasa 
14603a4b1be9SMatthew Ahrens 			/*
14613a4b1be9SMatthew Ahrens 			 * Reacquire the vdev_config lock.  The vdev_t
14623a4b1be9SMatthew Ahrens 			 * that we're removing may have changed, e.g. due
14633a4b1be9SMatthew Ahrens 			 * to a vdev_attach or vdev_detach.
14643a4b1be9SMatthew Ahrens 			 */
14653a4b1be9SMatthew Ahrens 			spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
14663a4b1be9SMatthew Ahrens 			vd = vdev_lookup_top(spa, svr->svr_vdev_id);
14673a4b1be9SMatthew Ahrens 
14685cabbc6bSPrashanth Sreenivasa 			if (txg != last_txg)
14695cabbc6bSPrashanth Sreenivasa 				max_alloc = zfs_remove_max_segment;
14705cabbc6bSPrashanth Sreenivasa 			last_txg = txg;
14715cabbc6bSPrashanth Sreenivasa 
14723a4b1be9SMatthew Ahrens 			spa_vdev_copy_impl(vd, svr, &vca, &max_alloc, tx);
14735cabbc6bSPrashanth Sreenivasa 
14745cabbc6bSPrashanth Sreenivasa 			dmu_tx_commit(tx);
14755cabbc6bSPrashanth Sreenivasa 			mutex_enter(&svr->svr_lock);
14765cabbc6bSPrashanth Sreenivasa 		}
14775cabbc6bSPrashanth Sreenivasa 	}
14785cabbc6bSPrashanth Sreenivasa 
14795cabbc6bSPrashanth Sreenivasa 	mutex_exit(&svr->svr_lock);
14803a4b1be9SMatthew Ahrens 
14813a4b1be9SMatthew Ahrens 	spa_config_exit(spa, SCL_CONFIG, FTAG);
14823a4b1be9SMatthew Ahrens 
14835cabbc6bSPrashanth Sreenivasa 	/*
14845cabbc6bSPrashanth Sreenivasa 	 * Wait for all copies to finish before cleaning up the vca.
14855cabbc6bSPrashanth Sreenivasa 	 */
14865cabbc6bSPrashanth Sreenivasa 	txg_wait_synced(spa->spa_dsl_pool, 0);
14875cabbc6bSPrashanth Sreenivasa 	ASSERT0(vca.vca_outstanding_bytes);
14885cabbc6bSPrashanth Sreenivasa 
14895cabbc6bSPrashanth Sreenivasa 	mutex_destroy(&vca.vca_lock);
14905cabbc6bSPrashanth Sreenivasa 	cv_destroy(&vca.vca_cv);
14915cabbc6bSPrashanth Sreenivasa 
14925cabbc6bSPrashanth Sreenivasa 	if (svr->svr_thread_exit) {
14935cabbc6bSPrashanth Sreenivasa 		mutex_enter(&svr->svr_lock);
14945cabbc6bSPrashanth Sreenivasa 		range_tree_vacate(svr->svr_allocd_segs, NULL, NULL);
14955cabbc6bSPrashanth Sreenivasa 		svr->svr_thread = NULL;
14965cabbc6bSPrashanth Sreenivasa 		cv_broadcast(&svr->svr_cv);
14975cabbc6bSPrashanth Sreenivasa 		mutex_exit(&svr->svr_lock);
14985cabbc6bSPrashanth Sreenivasa 	} else {
14995cabbc6bSPrashanth Sreenivasa 		ASSERT0(range_tree_space(svr->svr_allocd_segs));
15003a4b1be9SMatthew Ahrens 		vdev_remove_complete(spa);
15015cabbc6bSPrashanth Sreenivasa 	}
15025cabbc6bSPrashanth Sreenivasa }
15035cabbc6bSPrashanth Sreenivasa 
15045cabbc6bSPrashanth Sreenivasa void
15055cabbc6bSPrashanth Sreenivasa spa_vdev_remove_suspend(spa_t *spa)
15065cabbc6bSPrashanth Sreenivasa {
15075cabbc6bSPrashanth Sreenivasa 	spa_vdev_removal_t *svr = spa->spa_vdev_removal;
15085cabbc6bSPrashanth Sreenivasa 
15095cabbc6bSPrashanth Sreenivasa 	if (svr == NULL)
15105cabbc6bSPrashanth Sreenivasa 		return;
15115cabbc6bSPrashanth Sreenivasa 
15125cabbc6bSPrashanth Sreenivasa 	mutex_enter(&svr->svr_lock);
15135cabbc6bSPrashanth Sreenivasa 	svr->svr_thread_exit = B_TRUE;
15145cabbc6bSPrashanth Sreenivasa 	while (svr->svr_thread != NULL)
15155cabbc6bSPrashanth Sreenivasa 		cv_wait(&svr->svr_cv, &svr->svr_lock);
15165cabbc6bSPrashanth Sreenivasa 	svr->svr_thread_exit = B_FALSE;
15175cabbc6bSPrashanth Sreenivasa 	mutex_exit(&svr->svr_lock);
15185cabbc6bSPrashanth Sreenivasa }
15195cabbc6bSPrashanth Sreenivasa 
15205cabbc6bSPrashanth Sreenivasa /* ARGSUSED */
15215cabbc6bSPrashanth Sreenivasa static int
15225cabbc6bSPrashanth Sreenivasa spa_vdev_remove_cancel_check(void *arg, dmu_tx_t *tx)
15235cabbc6bSPrashanth Sreenivasa {
15245cabbc6bSPrashanth Sreenivasa 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
15255cabbc6bSPrashanth Sreenivasa 
15265cabbc6bSPrashanth Sreenivasa 	if (spa->spa_vdev_removal == NULL)
15275cabbc6bSPrashanth Sreenivasa 		return (ENOTACTIVE);
15285cabbc6bSPrashanth Sreenivasa 	return (0);
15295cabbc6bSPrashanth Sreenivasa }
15305cabbc6bSPrashanth Sreenivasa 
15315cabbc6bSPrashanth Sreenivasa /*
15325cabbc6bSPrashanth Sreenivasa  * Cancel a removal by freeing all entries from the partial mapping
15335cabbc6bSPrashanth Sreenivasa  * and marking the vdev as no longer being removing.
15345cabbc6bSPrashanth Sreenivasa  */
15355cabbc6bSPrashanth Sreenivasa /* ARGSUSED */
15365cabbc6bSPrashanth Sreenivasa static void
15375cabbc6bSPrashanth Sreenivasa spa_vdev_remove_cancel_sync(void *arg, dmu_tx_t *tx)
15385cabbc6bSPrashanth Sreenivasa {
15395cabbc6bSPrashanth Sreenivasa 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
15405cabbc6bSPrashanth Sreenivasa 	spa_vdev_removal_t *svr = spa->spa_vdev_removal;
15413a4b1be9SMatthew Ahrens 	vdev_t *vd = vdev_lookup_top(spa, svr->svr_vdev_id);
15425cabbc6bSPrashanth Sreenivasa 	vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
15435cabbc6bSPrashanth Sreenivasa 	vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
15445cabbc6bSPrashanth Sreenivasa 	objset_t *mos = spa->spa_meta_objset;
15455cabbc6bSPrashanth Sreenivasa 
15465cabbc6bSPrashanth Sreenivasa 	ASSERT3P(svr->svr_thread, ==, NULL);
15475cabbc6bSPrashanth Sreenivasa 
15485cabbc6bSPrashanth Sreenivasa 	spa_feature_decr(spa, SPA_FEATURE_DEVICE_REMOVAL, tx);
15495cabbc6bSPrashanth Sreenivasa 	if (vdev_obsolete_counts_are_precise(vd)) {
15505cabbc6bSPrashanth Sreenivasa 		spa_feature_decr(spa, SPA_FEATURE_OBSOLETE_COUNTS, tx);
15515cabbc6bSPrashanth Sreenivasa 		VERIFY0(zap_remove(spa->spa_meta_objset, vd->vdev_top_zap,
15525cabbc6bSPrashanth Sreenivasa 		    VDEV_TOP_ZAP_OBSOLETE_COUNTS_ARE_PRECISE, tx));
15535cabbc6bSPrashanth Sreenivasa 	}
15545cabbc6bSPrashanth Sreenivasa 
15555cabbc6bSPrashanth Sreenivasa 	if (vdev_obsolete_sm_object(vd) != 0) {
15565cabbc6bSPrashanth Sreenivasa 		ASSERT(vd->vdev_obsolete_sm != NULL);
15575cabbc6bSPrashanth Sreenivasa 		ASSERT3U(vdev_obsolete_sm_object(vd), ==,
15585cabbc6bSPrashanth Sreenivasa 		    space_map_object(vd->vdev_obsolete_sm));
15595cabbc6bSPrashanth Sreenivasa 
15605cabbc6bSPrashanth Sreenivasa 		space_map_free(vd->vdev_obsolete_sm, tx);
15615cabbc6bSPrashanth Sreenivasa 		VERIFY0(zap_remove(spa->spa_meta_objset, vd->vdev_top_zap,
15625cabbc6bSPrashanth Sreenivasa 		    VDEV_TOP_ZAP_INDIRECT_OBSOLETE_SM, tx));
15635cabbc6bSPrashanth Sreenivasa 		space_map_close(vd->vdev_obsolete_sm);
15645cabbc6bSPrashanth Sreenivasa 		vd->vdev_obsolete_sm = NULL;
15655cabbc6bSPrashanth Sreenivasa 		spa_feature_decr(spa, SPA_FEATURE_OBSOLETE_COUNTS, tx);
15665cabbc6bSPrashanth Sreenivasa 	}
15675cabbc6bSPrashanth Sreenivasa 	for (int i = 0; i < TXG_SIZE; i++) {
15685cabbc6bSPrashanth Sreenivasa 		ASSERT(list_is_empty(&svr->svr_new_segments[i]));
15695cabbc6bSPrashanth Sreenivasa 		ASSERT3U(svr->svr_max_offset_to_sync[i], <=,
15705cabbc6bSPrashanth Sreenivasa 		    vdev_indirect_mapping_max_offset(vim));
15715cabbc6bSPrashanth Sreenivasa 	}
15725cabbc6bSPrashanth Sreenivasa 
15735cabbc6bSPrashanth Sreenivasa 	for (uint64_t msi = 0; msi < vd->vdev_ms_count; msi++) {
15745cabbc6bSPrashanth Sreenivasa 		metaslab_t *msp = vd->vdev_ms[msi];
15755cabbc6bSPrashanth Sreenivasa 
15765cabbc6bSPrashanth Sreenivasa 		if (msp->ms_start >= vdev_indirect_mapping_max_offset(vim))
15775cabbc6bSPrashanth Sreenivasa 			break;
15785cabbc6bSPrashanth Sreenivasa 
15795cabbc6bSPrashanth Sreenivasa 		ASSERT0(range_tree_space(svr->svr_allocd_segs));
15805cabbc6bSPrashanth Sreenivasa 
15815cabbc6bSPrashanth Sreenivasa 		mutex_enter(&msp->ms_lock);
15825cabbc6bSPrashanth Sreenivasa 
15835cabbc6bSPrashanth Sreenivasa 		/*
15845cabbc6bSPrashanth Sreenivasa 		 * Assert nothing in flight -- ms_*tree is empty.
15855cabbc6bSPrashanth Sreenivasa 		 */
15865cabbc6bSPrashanth Sreenivasa 		for (int i = 0; i < TXG_SIZE; i++)
158786714001SSerapheim Dimitropoulos 			ASSERT0(range_tree_space(msp->ms_allocating[i]));
15885cabbc6bSPrashanth Sreenivasa 		for (int i = 0; i < TXG_DEFER_SIZE; i++)
158986714001SSerapheim Dimitropoulos 			ASSERT0(range_tree_space(msp->ms_defer[i]));
159086714001SSerapheim Dimitropoulos 		ASSERT0(range_tree_space(msp->ms_freed));
15915cabbc6bSPrashanth Sreenivasa 
15925cabbc6bSPrashanth Sreenivasa 		if (msp->ms_sm != NULL) {
15935cabbc6bSPrashanth Sreenivasa 			mutex_enter(&svr->svr_lock);
15945cabbc6bSPrashanth Sreenivasa 			VERIFY0(space_map_load(msp->ms_sm,
15955cabbc6bSPrashanth Sreenivasa 			    svr->svr_allocd_segs, SM_ALLOC));
159686714001SSerapheim Dimitropoulos 			range_tree_walk(msp->ms_freeing,
15975cabbc6bSPrashanth Sreenivasa 			    range_tree_remove, svr->svr_allocd_segs);
15985cabbc6bSPrashanth Sreenivasa 
15995cabbc6bSPrashanth Sreenivasa 			/*
16005cabbc6bSPrashanth Sreenivasa 			 * Clear everything past what has been synced,
16015cabbc6bSPrashanth Sreenivasa 			 * because we have not allocated mappings for it yet.
16025cabbc6bSPrashanth Sreenivasa 			 */
16035cabbc6bSPrashanth Sreenivasa 			uint64_t syncd = vdev_indirect_mapping_max_offset(vim);
16043a4b1be9SMatthew Ahrens 			uint64_t sm_end = msp->ms_sm->sm_start +
16053a4b1be9SMatthew Ahrens 			    msp->ms_sm->sm_size;
16063a4b1be9SMatthew Ahrens 			if (sm_end > syncd)
16073a4b1be9SMatthew Ahrens 				range_tree_clear(svr->svr_allocd_segs,
16083a4b1be9SMatthew Ahrens 				    syncd, sm_end - syncd);
16095cabbc6bSPrashanth Sreenivasa 
16105cabbc6bSPrashanth Sreenivasa 			mutex_exit(&svr->svr_lock);
16115cabbc6bSPrashanth Sreenivasa 		}
16125cabbc6bSPrashanth Sreenivasa 		mutex_exit(&msp->ms_lock);
16135cabbc6bSPrashanth Sreenivasa 
16145cabbc6bSPrashanth Sreenivasa 		mutex_enter(&svr->svr_lock);
16155cabbc6bSPrashanth Sreenivasa 		range_tree_vacate(svr->svr_allocd_segs,
16165cabbc6bSPrashanth Sreenivasa 		    free_mapped_segment_cb, vd);
16175cabbc6bSPrashanth Sreenivasa 		mutex_exit(&svr->svr_lock);
16185cabbc6bSPrashanth Sreenivasa 	}
16195cabbc6bSPrashanth Sreenivasa 
16205cabbc6bSPrashanth Sreenivasa 	/*
16215cabbc6bSPrashanth Sreenivasa 	 * Note: this must happen after we invoke free_mapped_segment_cb,
16225cabbc6bSPrashanth Sreenivasa 	 * because it adds to the obsolete_segments.
16235cabbc6bSPrashanth Sreenivasa 	 */
16245cabbc6bSPrashanth Sreenivasa 	range_tree_vacate(vd->vdev_obsolete_segments, NULL, NULL);
16255cabbc6bSPrashanth Sreenivasa 
16265cabbc6bSPrashanth Sreenivasa 	ASSERT3U(vic->vic_mapping_object, ==,
16275cabbc6bSPrashanth Sreenivasa 	    vdev_indirect_mapping_object(vd->vdev_indirect_mapping));
16285cabbc6bSPrashanth Sreenivasa 	vdev_indirect_mapping_close(vd->vdev_indirect_mapping);
16295cabbc6bSPrashanth Sreenivasa 	vd->vdev_indirect_mapping = NULL;
16305cabbc6bSPrashanth Sreenivasa 	vdev_indirect_mapping_free(mos, vic->vic_mapping_object, tx);
16315cabbc6bSPrashanth Sreenivasa 	vic->vic_mapping_object = 0;
16325cabbc6bSPrashanth Sreenivasa 
16335cabbc6bSPrashanth Sreenivasa 	ASSERT3U(vic->vic_births_object, ==,
16345cabbc6bSPrashanth Sreenivasa 	    vdev_indirect_births_object(vd->vdev_indirect_births));
16355cabbc6bSPrashanth Sreenivasa 	vdev_indirect_births_close(vd->vdev_indirect_births);
16365cabbc6bSPrashanth Sreenivasa 	vd->vdev_indirect_births = NULL;
16375cabbc6bSPrashanth Sreenivasa 	vdev_indirect_births_free(mos, vic->vic_births_object, tx);
16385cabbc6bSPrashanth Sreenivasa 	vic->vic_births_object = 0;
16395cabbc6bSPrashanth Sreenivasa 
16405cabbc6bSPrashanth Sreenivasa 	/*
16415cabbc6bSPrashanth Sreenivasa 	 * We may have processed some frees from the removing vdev in this
16425cabbc6bSPrashanth Sreenivasa 	 * txg, thus increasing svr_bytes_done; discard that here to
16435cabbc6bSPrashanth Sreenivasa 	 * satisfy the assertions in spa_vdev_removal_destroy().
16445cabbc6bSPrashanth Sreenivasa 	 * Note that future txg's can not have any bytes_done, because
16455cabbc6bSPrashanth Sreenivasa 	 * future TXG's are only modified from open context, and we have
16465cabbc6bSPrashanth Sreenivasa 	 * already shut down the copying thread.
16475cabbc6bSPrashanth Sreenivasa 	 */
16485cabbc6bSPrashanth Sreenivasa 	svr->svr_bytes_done[dmu_tx_get_txg(tx) & TXG_MASK] = 0;
16495cabbc6bSPrashanth Sreenivasa 	spa_finish_removal(spa, DSS_CANCELED, tx);
16505cabbc6bSPrashanth Sreenivasa 
16515cabbc6bSPrashanth Sreenivasa 	vd->vdev_removing = B_FALSE;
16525cabbc6bSPrashanth Sreenivasa 	vdev_config_dirty(vd);
16535cabbc6bSPrashanth Sreenivasa 
16545cabbc6bSPrashanth Sreenivasa 	zfs_dbgmsg("canceled device removal for vdev %llu in %llu",
16555cabbc6bSPrashanth Sreenivasa 	    vd->vdev_id, dmu_tx_get_txg(tx));
16565cabbc6bSPrashanth Sreenivasa 	spa_history_log_internal(spa, "vdev remove canceled", tx,
16575cabbc6bSPrashanth Sreenivasa 	    "%s vdev %llu %s", spa_name(spa),
16585cabbc6bSPrashanth Sreenivasa 	    vd->vdev_id, (vd->vdev_path != NULL) ? vd->vdev_path : "-");
16595cabbc6bSPrashanth Sreenivasa }
16605cabbc6bSPrashanth Sreenivasa 
16615cabbc6bSPrashanth Sreenivasa int
16625cabbc6bSPrashanth Sreenivasa spa_vdev_remove_cancel(spa_t *spa)
16635cabbc6bSPrashanth Sreenivasa {
16645cabbc6bSPrashanth Sreenivasa 	spa_vdev_remove_suspend(spa);
16655cabbc6bSPrashanth Sreenivasa 
16665cabbc6bSPrashanth Sreenivasa 	if (spa->spa_vdev_removal == NULL)
16675cabbc6bSPrashanth Sreenivasa 		return (ENOTACTIVE);
16685cabbc6bSPrashanth Sreenivasa 
16693a4b1be9SMatthew Ahrens 	uint64_t vdid = spa->spa_vdev_removal->svr_vdev_id;
16705cabbc6bSPrashanth Sreenivasa 
16715cabbc6bSPrashanth Sreenivasa 	int error = dsl_sync_task(spa->spa_name, spa_vdev_remove_cancel_check,
167286714001SSerapheim Dimitropoulos 	    spa_vdev_remove_cancel_sync, NULL, 0,
167386714001SSerapheim Dimitropoulos 	    ZFS_SPACE_CHECK_EXTRA_RESERVED);
16745cabbc6bSPrashanth Sreenivasa 
16755cabbc6bSPrashanth Sreenivasa 	if (error == 0) {
16765cabbc6bSPrashanth Sreenivasa 		spa_config_enter(spa, SCL_ALLOC | SCL_VDEV, FTAG, RW_WRITER);
16775cabbc6bSPrashanth Sreenivasa 		vdev_t *vd = vdev_lookup_top(spa, vdid);
16785cabbc6bSPrashanth Sreenivasa 		metaslab_group_activate(vd->vdev_mg);
16795cabbc6bSPrashanth Sreenivasa 		spa_config_exit(spa, SCL_ALLOC | SCL_VDEV, FTAG);
16805cabbc6bSPrashanth Sreenivasa 	}
16815cabbc6bSPrashanth Sreenivasa 
16825cabbc6bSPrashanth Sreenivasa 	return (error);
16835cabbc6bSPrashanth Sreenivasa }
16845cabbc6bSPrashanth Sreenivasa 
16855cabbc6bSPrashanth Sreenivasa void
16865cabbc6bSPrashanth Sreenivasa svr_sync(spa_t *spa, dmu_tx_t *tx)
16875cabbc6bSPrashanth Sreenivasa {
16885cabbc6bSPrashanth Sreenivasa 	spa_vdev_removal_t *svr = spa->spa_vdev_removal;
16895cabbc6bSPrashanth Sreenivasa 	int txgoff = dmu_tx_get_txg(tx) & TXG_MASK;
16905cabbc6bSPrashanth Sreenivasa 
16915cabbc6bSPrashanth Sreenivasa 	/*
16925cabbc6bSPrashanth Sreenivasa 	 * This check is necessary so that we do not dirty the
16935cabbc6bSPrashanth Sreenivasa 	 * DIRECTORY_OBJECT via spa_sync_removing_state() when there
16945cabbc6bSPrashanth Sreenivasa 	 * is nothing to do.  Dirtying it every time would prevent us
16955cabbc6bSPrashanth Sreenivasa 	 * from syncing-to-convergence.
16965cabbc6bSPrashanth Sreenivasa 	 */
16975cabbc6bSPrashanth Sreenivasa 	if (svr->svr_bytes_done[txgoff] == 0)
16985cabbc6bSPrashanth Sreenivasa 		return;
16995cabbc6bSPrashanth Sreenivasa 
17005cabbc6bSPrashanth Sreenivasa 	/*
17015cabbc6bSPrashanth Sreenivasa 	 * Update progress accounting.
17025cabbc6bSPrashanth Sreenivasa 	 */
17035cabbc6bSPrashanth Sreenivasa 	spa->spa_removing_phys.sr_copied += svr->svr_bytes_done[txgoff];
17045cabbc6bSPrashanth Sreenivasa 	svr->svr_bytes_done[txgoff] = 0;
17055cabbc6bSPrashanth Sreenivasa 
17065cabbc6bSPrashanth Sreenivasa 	spa_sync_removing_state(spa, tx);
17075cabbc6bSPrashanth Sreenivasa }
17085cabbc6bSPrashanth Sreenivasa 
17095cabbc6bSPrashanth Sreenivasa static void
17105cabbc6bSPrashanth Sreenivasa vdev_remove_make_hole_and_free(vdev_t *vd)
17115cabbc6bSPrashanth Sreenivasa {
17125cabbc6bSPrashanth Sreenivasa 	uint64_t id = vd->vdev_id;
17135cabbc6bSPrashanth Sreenivasa 	spa_t *spa = vd->vdev_spa;
17145cabbc6bSPrashanth Sreenivasa 	vdev_t *rvd = spa->spa_root_vdev;
17155cabbc6bSPrashanth Sreenivasa 	boolean_t last_vdev = (id == (rvd->vdev_children - 1));
17165cabbc6bSPrashanth Sreenivasa 
17175cabbc6bSPrashanth Sreenivasa 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
17185cabbc6bSPrashanth Sreenivasa 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
17195cabbc6bSPrashanth Sreenivasa 
17205cabbc6bSPrashanth Sreenivasa 	vdev_free(vd);
17215cabbc6bSPrashanth Sreenivasa 
17225cabbc6bSPrashanth Sreenivasa 	if (last_vdev) {
17235cabbc6bSPrashanth Sreenivasa 		vdev_compact_children(rvd);
17245cabbc6bSPrashanth Sreenivasa 	} else {
17255cabbc6bSPrashanth Sreenivasa 		vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
17265cabbc6bSPrashanth Sreenivasa 		vdev_add_child(rvd, vd);
17275cabbc6bSPrashanth Sreenivasa 	}
17285cabbc6bSPrashanth Sreenivasa 	vdev_config_dirty(rvd);
17295cabbc6bSPrashanth Sreenivasa 
17305cabbc6bSPrashanth Sreenivasa 	/*
17315cabbc6bSPrashanth Sreenivasa 	 * Reassess the health of our root vdev.
17325cabbc6bSPrashanth Sreenivasa 	 */
17335cabbc6bSPrashanth Sreenivasa 	vdev_reopen(rvd);
17345cabbc6bSPrashanth Sreenivasa }
17355cabbc6bSPrashanth Sreenivasa 
17365cabbc6bSPrashanth Sreenivasa /*
17375cabbc6bSPrashanth Sreenivasa  * Remove a log device.  The config lock is held for the specified TXG.
17385cabbc6bSPrashanth Sreenivasa  */
17395cabbc6bSPrashanth Sreenivasa static int
17405cabbc6bSPrashanth Sreenivasa spa_vdev_remove_log(vdev_t *vd, uint64_t *txg)
17415cabbc6bSPrashanth Sreenivasa {
17425cabbc6bSPrashanth Sreenivasa 	metaslab_group_t *mg = vd->vdev_mg;
17435cabbc6bSPrashanth Sreenivasa 	spa_t *spa = vd->vdev_spa;
17445cabbc6bSPrashanth Sreenivasa 	int error = 0;
17455cabbc6bSPrashanth Sreenivasa 
17465cabbc6bSPrashanth Sreenivasa 	ASSERT(vd->vdev_islog);
17475cabbc6bSPrashanth Sreenivasa 	ASSERT(vd == vd->vdev_top);
1748*555d674dSSerapheim Dimitropoulos 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
17495cabbc6bSPrashanth Sreenivasa 
17505cabbc6bSPrashanth Sreenivasa 	/*
17515cabbc6bSPrashanth Sreenivasa 	 * Stop allocating from this vdev.
17525cabbc6bSPrashanth Sreenivasa 	 */
17535cabbc6bSPrashanth Sreenivasa 	metaslab_group_passivate(mg);
17545cabbc6bSPrashanth Sreenivasa 
17555cabbc6bSPrashanth Sreenivasa 	/*
17565cabbc6bSPrashanth Sreenivasa 	 * Wait for the youngest allocations and frees to sync,
17575cabbc6bSPrashanth Sreenivasa 	 * and then wait for the deferral of those frees to finish.
17585cabbc6bSPrashanth Sreenivasa 	 */
17595cabbc6bSPrashanth Sreenivasa 	spa_vdev_config_exit(spa, NULL,
17605cabbc6bSPrashanth Sreenivasa 	    *txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
17615cabbc6bSPrashanth Sreenivasa 
17625cabbc6bSPrashanth Sreenivasa 	/*
1763*555d674dSSerapheim Dimitropoulos 	 * Evacuate the device.  We don't hold the config lock as
1764*555d674dSSerapheim Dimitropoulos 	 * writer since we need to do I/O but we do keep the
17655cabbc6bSPrashanth Sreenivasa 	 * spa_namespace_lock held.  Once this completes the device
17665cabbc6bSPrashanth Sreenivasa 	 * should no longer have any blocks allocated on it.
17675cabbc6bSPrashanth Sreenivasa 	 */
1768*555d674dSSerapheim Dimitropoulos 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
1769*555d674dSSerapheim Dimitropoulos 	if (vd->vdev_stat.vs_alloc != 0)
1770*555d674dSSerapheim Dimitropoulos 		error = spa_reset_logs(spa);
17715cabbc6bSPrashanth Sreenivasa 
17725cabbc6bSPrashanth Sreenivasa 	*txg = spa_vdev_config_enter(spa);
17735cabbc6bSPrashanth Sreenivasa 
17745cabbc6bSPrashanth Sreenivasa 	if (error != 0) {
17755cabbc6bSPrashanth Sreenivasa 		metaslab_group_activate(mg);
17765cabbc6bSPrashanth Sreenivasa 		return (error);
17775cabbc6bSPrashanth Sreenivasa 	}
17785cabbc6bSPrashanth Sreenivasa 	ASSERT0(vd->vdev_stat.vs_alloc);
17795cabbc6bSPrashanth Sreenivasa 
17805cabbc6bSPrashanth Sreenivasa 	/*
17815cabbc6bSPrashanth Sreenivasa 	 * The evacuation succeeded.  Remove any remaining MOS metadata
17825cabbc6bSPrashanth Sreenivasa 	 * associated with this vdev, and wait for these changes to sync.
17835cabbc6bSPrashanth Sreenivasa 	 */
17845cabbc6bSPrashanth Sreenivasa 	vd->vdev_removing = B_TRUE;
17855cabbc6bSPrashanth Sreenivasa 
17865cabbc6bSPrashanth Sreenivasa 	vdev_dirty_leaves(vd, VDD_DTL, *txg);
17875cabbc6bSPrashanth Sreenivasa 	vdev_config_dirty(vd);
17885cabbc6bSPrashanth Sreenivasa 
1789*555d674dSSerapheim Dimitropoulos 	vdev_metaslab_fini(vd);
1790*555d674dSSerapheim Dimitropoulos 
17915cabbc6bSPrashanth Sreenivasa 	spa_history_log_internal(spa, "vdev remove", NULL,
17925cabbc6bSPrashanth Sreenivasa 	    "%s vdev %llu (log) %s", spa_name(spa), vd->vdev_id,
17935cabbc6bSPrashanth Sreenivasa 	    (vd->vdev_path != NULL) ? vd->vdev_path : "-");
17945cabbc6bSPrashanth Sreenivasa 
17955cabbc6bSPrashanth Sreenivasa 	/* Make sure these changes are sync'ed */
17965cabbc6bSPrashanth Sreenivasa 	spa_vdev_config_exit(spa, NULL, *txg, 0, FTAG);
17975cabbc6bSPrashanth Sreenivasa 
1798094e47e9SGeorge Wilson 	/* Stop initializing */
1799094e47e9SGeorge Wilson 	(void) vdev_initialize_stop_all(vd, VDEV_INITIALIZE_CANCELED);
1800094e47e9SGeorge Wilson 
18015cabbc6bSPrashanth Sreenivasa 	*txg = spa_vdev_config_enter(spa);
18025cabbc6bSPrashanth Sreenivasa 
18035cabbc6bSPrashanth Sreenivasa 	sysevent_t *ev = spa_event_create(spa, vd, NULL,
18045cabbc6bSPrashanth Sreenivasa 	    ESC_ZFS_VDEV_REMOVE_DEV);
18055cabbc6bSPrashanth Sreenivasa 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
18065cabbc6bSPrashanth Sreenivasa 	ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
18075cabbc6bSPrashanth Sreenivasa 
18085cabbc6bSPrashanth Sreenivasa 	/* The top ZAP should have been destroyed by vdev_remove_empty. */
18095cabbc6bSPrashanth Sreenivasa 	ASSERT0(vd->vdev_top_zap);
18105cabbc6bSPrashanth Sreenivasa 	/* The leaf ZAP should have been destroyed by vdev_dtl_sync. */
18115cabbc6bSPrashanth Sreenivasa 	ASSERT0(vd->vdev_leaf_zap);
18125cabbc6bSPrashanth Sreenivasa 
18135cabbc6bSPrashanth Sreenivasa 	(void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
18145cabbc6bSPrashanth Sreenivasa 
18155cabbc6bSPrashanth Sreenivasa 	if (list_link_active(&vd->vdev_state_dirty_node))
18165cabbc6bSPrashanth Sreenivasa 		vdev_state_clean(vd);
18175cabbc6bSPrashanth Sreenivasa 	if (list_link_active(&vd->vdev_config_dirty_node))
18185cabbc6bSPrashanth Sreenivasa 		vdev_config_clean(vd);
18195cabbc6bSPrashanth Sreenivasa 
1820*555d674dSSerapheim Dimitropoulos 	ASSERT0(vd->vdev_stat.vs_alloc);
1821*555d674dSSerapheim Dimitropoulos 
18225cabbc6bSPrashanth Sreenivasa 	/*
18235cabbc6bSPrashanth Sreenivasa 	 * Clean up the vdev namespace.
18245cabbc6bSPrashanth Sreenivasa 	 */
18255cabbc6bSPrashanth Sreenivasa 	vdev_remove_make_hole_and_free(vd);
18265cabbc6bSPrashanth Sreenivasa 
18275cabbc6bSPrashanth Sreenivasa 	if (ev != NULL)
18285cabbc6bSPrashanth Sreenivasa 		spa_event_post(ev);
18295cabbc6bSPrashanth Sreenivasa 
18305cabbc6bSPrashanth Sreenivasa 	return (0);
18315cabbc6bSPrashanth Sreenivasa }
18325cabbc6bSPrashanth Sreenivasa 
18335cabbc6bSPrashanth Sreenivasa static int
18345cabbc6bSPrashanth Sreenivasa spa_vdev_remove_top_check(vdev_t *vd)
18355cabbc6bSPrashanth Sreenivasa {
18365cabbc6bSPrashanth Sreenivasa 	spa_t *spa = vd->vdev_spa;
18375cabbc6bSPrashanth Sreenivasa 
18385cabbc6bSPrashanth Sreenivasa 	if (vd != vd->vdev_top)
18395cabbc6bSPrashanth Sreenivasa 		return (SET_ERROR(ENOTSUP));
18405cabbc6bSPrashanth Sreenivasa 
18415cabbc6bSPrashanth Sreenivasa 	if (!spa_feature_is_enabled(spa, SPA_FEATURE_DEVICE_REMOVAL))
18425cabbc6bSPrashanth Sreenivasa 		return (SET_ERROR(ENOTSUP));
18435cabbc6bSPrashanth Sreenivasa 
1844663207adSDon Brady 	/* available space in the pool's normal class */
1845663207adSDon Brady 	uint64_t available = dsl_dir_space_available(
1846663207adSDon Brady 	    spa->spa_dsl_pool->dp_root_dir, NULL, 0, B_TRUE);
1847663207adSDon Brady 
1848663207adSDon Brady 	metaslab_class_t *mc = vd->vdev_mg->mg_class;
1849663207adSDon Brady 
1850663207adSDon Brady 	/*
1851663207adSDon Brady 	 * When removing a vdev from an allocation class that has
1852663207adSDon Brady 	 * remaining vdevs, include available space from the class.
1853663207adSDon Brady 	 */
1854663207adSDon Brady 	if (mc != spa_normal_class(spa) && mc->mc_groups > 1) {
1855663207adSDon Brady 		uint64_t class_avail = metaslab_class_get_space(mc) -
1856663207adSDon Brady 		    metaslab_class_get_alloc(mc);
1857663207adSDon Brady 
1858663207adSDon Brady 		/* add class space, adjusted for overhead */
1859663207adSDon Brady 		available += (class_avail * 94) / 100;
1860663207adSDon Brady 	}
1861663207adSDon Brady 
18625cabbc6bSPrashanth Sreenivasa 	/*
18635cabbc6bSPrashanth Sreenivasa 	 * There has to be enough free space to remove the
18645cabbc6bSPrashanth Sreenivasa 	 * device and leave double the "slop" space (i.e. we
18655cabbc6bSPrashanth Sreenivasa 	 * must leave at least 3% of the pool free, in addition to
18665cabbc6bSPrashanth Sreenivasa 	 * the normal slop space).
18675cabbc6bSPrashanth Sreenivasa 	 */
1868663207adSDon Brady 	if (available < vd->vdev_stat.vs_dspace + spa_get_slop_space(spa)) {
18695cabbc6bSPrashanth Sreenivasa 		return (SET_ERROR(ENOSPC));
18705cabbc6bSPrashanth Sreenivasa 	}
18715cabbc6bSPrashanth Sreenivasa 
18725cabbc6bSPrashanth Sreenivasa 	/*
18735cabbc6bSPrashanth Sreenivasa 	 * There can not be a removal in progress.
18745cabbc6bSPrashanth Sreenivasa 	 */
18755cabbc6bSPrashanth Sreenivasa 	if (spa->spa_removing_phys.sr_state == DSS_SCANNING)
18765cabbc6bSPrashanth Sreenivasa 		return (SET_ERROR(EBUSY));
18775cabbc6bSPrashanth Sreenivasa 
18785cabbc6bSPrashanth Sreenivasa 	/*
18795cabbc6bSPrashanth Sreenivasa 	 * The device must have all its data.
18805cabbc6bSPrashanth Sreenivasa 	 */
18815cabbc6bSPrashanth Sreenivasa 	if (!vdev_dtl_empty(vd, DTL_MISSING) ||
18825cabbc6bSPrashanth Sreenivasa 	    !vdev_dtl_empty(vd, DTL_OUTAGE))
18835cabbc6bSPrashanth Sreenivasa 		return (SET_ERROR(EBUSY));
18845cabbc6bSPrashanth Sreenivasa 
18855cabbc6bSPrashanth Sreenivasa 	/*
18865cabbc6bSPrashanth Sreenivasa 	 * The device must be healthy.
18875cabbc6bSPrashanth Sreenivasa 	 */
18885cabbc6bSPrashanth Sreenivasa 	if (!vdev_readable(vd))
18895cabbc6bSPrashanth Sreenivasa 		return (SET_ERROR(EIO));
18905cabbc6bSPrashanth Sreenivasa 
18915cabbc6bSPrashanth Sreenivasa 	/*
18925cabbc6bSPrashanth Sreenivasa 	 * All vdevs in normal class must have the same ashift.
18935cabbc6bSPrashanth Sreenivasa 	 */
18945cabbc6bSPrashanth Sreenivasa 	if (spa->spa_max_ashift != spa->spa_min_ashift) {
18955cabbc6bSPrashanth Sreenivasa 		return (SET_ERROR(EINVAL));
18965cabbc6bSPrashanth Sreenivasa 	}
18975cabbc6bSPrashanth Sreenivasa 
18985cabbc6bSPrashanth Sreenivasa 	/*
18995cabbc6bSPrashanth Sreenivasa 	 * All vdevs in normal class must have the same ashift
19005cabbc6bSPrashanth Sreenivasa 	 * and not be raidz.
19015cabbc6bSPrashanth Sreenivasa 	 */
19025cabbc6bSPrashanth Sreenivasa 	vdev_t *rvd = spa->spa_root_vdev;
19035cabbc6bSPrashanth Sreenivasa 	int num_indirect = 0;
19045cabbc6bSPrashanth Sreenivasa 	for (uint64_t id = 0; id < rvd->vdev_children; id++) {
19055cabbc6bSPrashanth Sreenivasa 		vdev_t *cvd = rvd->vdev_child[id];
19065cabbc6bSPrashanth Sreenivasa 		if (cvd->vdev_ashift != 0 && !cvd->vdev_islog)
19075cabbc6bSPrashanth Sreenivasa 			ASSERT3U(cvd->vdev_ashift, ==, spa->spa_max_ashift);
19085cabbc6bSPrashanth Sreenivasa 		if (cvd->vdev_ops == &vdev_indirect_ops)
19095cabbc6bSPrashanth Sreenivasa 			num_indirect++;
19105cabbc6bSPrashanth Sreenivasa 		if (!vdev_is_concrete(cvd))
19115cabbc6bSPrashanth Sreenivasa 			continue;
19125cabbc6bSPrashanth Sreenivasa 		if (cvd->vdev_ops == &vdev_raidz_ops)
19135cabbc6bSPrashanth Sreenivasa 			return (SET_ERROR(EINVAL));
19145cabbc6bSPrashanth Sreenivasa 		/*
19155cabbc6bSPrashanth Sreenivasa 		 * Need the mirror to be mirror of leaf vdevs only
19165cabbc6bSPrashanth Sreenivasa 		 */
19175cabbc6bSPrashanth Sreenivasa 		if (cvd->vdev_ops == &vdev_mirror_ops) {
19185cabbc6bSPrashanth Sreenivasa 			for (uint64_t cid = 0;
19195cabbc6bSPrashanth Sreenivasa 			    cid < cvd->vdev_children; cid++) {
19205cabbc6bSPrashanth Sreenivasa 				vdev_t *tmp = cvd->vdev_child[cid];
19215cabbc6bSPrashanth Sreenivasa 				if (!tmp->vdev_ops->vdev_op_leaf)
19225cabbc6bSPrashanth Sreenivasa 					return (SET_ERROR(EINVAL));
19235cabbc6bSPrashanth Sreenivasa 			}
19245cabbc6bSPrashanth Sreenivasa 		}
19255cabbc6bSPrashanth Sreenivasa 	}
19265cabbc6bSPrashanth Sreenivasa 
19275cabbc6bSPrashanth Sreenivasa 	return (0);
19285cabbc6bSPrashanth Sreenivasa }
19295cabbc6bSPrashanth Sreenivasa 
19305cabbc6bSPrashanth Sreenivasa /*
19315cabbc6bSPrashanth Sreenivasa  * Initiate removal of a top-level vdev, reducing the total space in the pool.
19325cabbc6bSPrashanth Sreenivasa  * The config lock is held for the specified TXG.  Once initiated,
19335cabbc6bSPrashanth Sreenivasa  * evacuation of all allocated space (copying it to other vdevs) happens
19345cabbc6bSPrashanth Sreenivasa  * in the background (see spa_vdev_remove_thread()), and can be canceled
19355cabbc6bSPrashanth Sreenivasa  * (see spa_vdev_remove_cancel()).  If successful, the vdev will
19365cabbc6bSPrashanth Sreenivasa  * be transformed to an indirect vdev (see spa_vdev_remove_complete()).
19375cabbc6bSPrashanth Sreenivasa  */
19385cabbc6bSPrashanth Sreenivasa static int
19395cabbc6bSPrashanth Sreenivasa spa_vdev_remove_top(vdev_t *vd, uint64_t *txg)
19405cabbc6bSPrashanth Sreenivasa {
19415cabbc6bSPrashanth Sreenivasa 	spa_t *spa = vd->vdev_spa;
19425cabbc6bSPrashanth Sreenivasa 	int error;
19435cabbc6bSPrashanth Sreenivasa 
19445cabbc6bSPrashanth Sreenivasa 	/*
19455cabbc6bSPrashanth Sreenivasa 	 * Check for errors up-front, so that we don't waste time
19465cabbc6bSPrashanth Sreenivasa 	 * passivating the metaslab group and clearing the ZIL if there
19475cabbc6bSPrashanth Sreenivasa 	 * are errors.
19485cabbc6bSPrashanth Sreenivasa 	 */
19495cabbc6bSPrashanth Sreenivasa 	error = spa_vdev_remove_top_check(vd);
19505cabbc6bSPrashanth Sreenivasa 	if (error != 0)
19515cabbc6bSPrashanth Sreenivasa 		return (error);
19525cabbc6bSPrashanth Sreenivasa 
19535cabbc6bSPrashanth Sreenivasa 	/*
19545cabbc6bSPrashanth Sreenivasa 	 * Stop allocating from this vdev.  Note that we must check
19555cabbc6bSPrashanth Sreenivasa 	 * that this is not the only device in the pool before
19565cabbc6bSPrashanth Sreenivasa 	 * passivating, otherwise we will not be able to make
19575cabbc6bSPrashanth Sreenivasa 	 * progress because we can't allocate from any vdevs.
19585cabbc6bSPrashanth Sreenivasa 	 * The above check for sufficient free space serves this
19595cabbc6bSPrashanth Sreenivasa 	 * purpose.
19605cabbc6bSPrashanth Sreenivasa 	 */
19615cabbc6bSPrashanth Sreenivasa 	metaslab_group_t *mg = vd->vdev_mg;
19625cabbc6bSPrashanth Sreenivasa 	metaslab_group_passivate(mg);
19635cabbc6bSPrashanth Sreenivasa 
19645cabbc6bSPrashanth Sreenivasa 	/*
19655cabbc6bSPrashanth Sreenivasa 	 * Wait for the youngest allocations and frees to sync,
19665cabbc6bSPrashanth Sreenivasa 	 * and then wait for the deferral of those frees to finish.
19675cabbc6bSPrashanth Sreenivasa 	 */
19685cabbc6bSPrashanth Sreenivasa 	spa_vdev_config_exit(spa, NULL,
19695cabbc6bSPrashanth Sreenivasa 	    *txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
19705cabbc6bSPrashanth Sreenivasa 
19715cabbc6bSPrashanth Sreenivasa 	/*
19725cabbc6bSPrashanth Sreenivasa 	 * We must ensure that no "stubby" log blocks are allocated
19735cabbc6bSPrashanth Sreenivasa 	 * on the device to be removed.  These blocks could be
19745cabbc6bSPrashanth Sreenivasa 	 * written at any time, including while we are in the middle
19755cabbc6bSPrashanth Sreenivasa 	 * of copying them.
19765cabbc6bSPrashanth Sreenivasa 	 */
19775cabbc6bSPrashanth Sreenivasa 	error = spa_reset_logs(spa);
19785cabbc6bSPrashanth Sreenivasa 
1979094e47e9SGeorge Wilson 	/*
1980094e47e9SGeorge Wilson 	 * We stop any initializing that is currently in progress but leave
1981094e47e9SGeorge Wilson 	 * the state as "active". This will allow the initializing to resume
1982094e47e9SGeorge Wilson 	 * if the removal is canceled sometime later.
1983094e47e9SGeorge Wilson 	 */
1984094e47e9SGeorge Wilson 	vdev_initialize_stop_all(vd, VDEV_INITIALIZE_ACTIVE);
1985094e47e9SGeorge Wilson 
19865cabbc6bSPrashanth Sreenivasa 	*txg = spa_vdev_config_enter(spa);
19875cabbc6bSPrashanth Sreenivasa 
19885cabbc6bSPrashanth Sreenivasa 	/*
19895cabbc6bSPrashanth Sreenivasa 	 * Things might have changed while the config lock was dropped
19905cabbc6bSPrashanth Sreenivasa 	 * (e.g. space usage).  Check for errors again.
19915cabbc6bSPrashanth Sreenivasa 	 */
19925cabbc6bSPrashanth Sreenivasa 	if (error == 0)
19935cabbc6bSPrashanth Sreenivasa 		error = spa_vdev_remove_top_check(vd);
19945cabbc6bSPrashanth Sreenivasa 
19955cabbc6bSPrashanth Sreenivasa 	if (error != 0) {
19965cabbc6bSPrashanth Sreenivasa 		metaslab_group_activate(mg);
1997094e47e9SGeorge Wilson 		spa_async_request(spa, SPA_ASYNC_INITIALIZE_RESTART);
19985cabbc6bSPrashanth Sreenivasa 		return (error);
19995cabbc6bSPrashanth Sreenivasa 	}
20005cabbc6bSPrashanth Sreenivasa 
20015cabbc6bSPrashanth Sreenivasa 	vd->vdev_removing = B_TRUE;
20025cabbc6bSPrashanth Sreenivasa 
20035cabbc6bSPrashanth Sreenivasa 	vdev_dirty_leaves(vd, VDD_DTL, *txg);
20045cabbc6bSPrashanth Sreenivasa 	vdev_config_dirty(vd);
20055cabbc6bSPrashanth Sreenivasa 	dmu_tx_t *tx = dmu_tx_create_assigned(spa->spa_dsl_pool, *txg);
20065cabbc6bSPrashanth Sreenivasa 	dsl_sync_task_nowait(spa->spa_dsl_pool,
20075cabbc6bSPrashanth Sreenivasa 	    vdev_remove_initiate_sync,
20083a4b1be9SMatthew Ahrens 	    (void *)(uintptr_t)vd->vdev_id, 0, ZFS_SPACE_CHECK_NONE, tx);
20095cabbc6bSPrashanth Sreenivasa 	dmu_tx_commit(tx);
20105cabbc6bSPrashanth Sreenivasa 
20115cabbc6bSPrashanth Sreenivasa 	return (0);
20125cabbc6bSPrashanth Sreenivasa }
20135cabbc6bSPrashanth Sreenivasa 
20145cabbc6bSPrashanth Sreenivasa /*
20155cabbc6bSPrashanth Sreenivasa  * Remove a device from the pool.
20165cabbc6bSPrashanth Sreenivasa  *
20175cabbc6bSPrashanth Sreenivasa  * Removing a device from the vdev namespace requires several steps
20185cabbc6bSPrashanth Sreenivasa  * and can take a significant amount of time.  As a result we use
20195cabbc6bSPrashanth Sreenivasa  * the spa_vdev_config_[enter/exit] functions which allow us to
20205cabbc6bSPrashanth Sreenivasa  * grab and release the spa_config_lock while still holding the namespace
20215cabbc6bSPrashanth Sreenivasa  * lock.  During each step the configuration is synced out.
20225cabbc6bSPrashanth Sreenivasa  */
20235cabbc6bSPrashanth Sreenivasa int
20245cabbc6bSPrashanth Sreenivasa spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
20255cabbc6bSPrashanth Sreenivasa {
20265cabbc6bSPrashanth Sreenivasa 	vdev_t *vd;
20275cabbc6bSPrashanth Sreenivasa 	nvlist_t **spares, **l2cache, *nv;
20285cabbc6bSPrashanth Sreenivasa 	uint64_t txg = 0;
20295cabbc6bSPrashanth Sreenivasa 	uint_t nspares, nl2cache;
20305cabbc6bSPrashanth Sreenivasa 	int error = 0;
20315cabbc6bSPrashanth Sreenivasa 	boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
20325cabbc6bSPrashanth Sreenivasa 	sysevent_t *ev = NULL;
20335cabbc6bSPrashanth Sreenivasa 
20345cabbc6bSPrashanth Sreenivasa 	ASSERT(spa_writeable(spa));
20355cabbc6bSPrashanth Sreenivasa 
20365cabbc6bSPrashanth Sreenivasa 	if (!locked)
20375cabbc6bSPrashanth Sreenivasa 		txg = spa_vdev_enter(spa);
20385cabbc6bSPrashanth Sreenivasa 
203986714001SSerapheim Dimitropoulos 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
204086714001SSerapheim Dimitropoulos 	if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) {
204186714001SSerapheim Dimitropoulos 		error = (spa_has_checkpoint(spa)) ?
204286714001SSerapheim Dimitropoulos 		    ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT;
204386714001SSerapheim Dimitropoulos 
204486714001SSerapheim Dimitropoulos 		if (!locked)
204586714001SSerapheim Dimitropoulos 			return (spa_vdev_exit(spa, NULL, txg, error));
204686714001SSerapheim Dimitropoulos 
204786714001SSerapheim Dimitropoulos 		return (error);
204886714001SSerapheim Dimitropoulos 	}
204986714001SSerapheim Dimitropoulos 
20505cabbc6bSPrashanth Sreenivasa 	vd = spa_lookup_by_guid(spa, guid, B_FALSE);
20515cabbc6bSPrashanth Sreenivasa 
20525cabbc6bSPrashanth Sreenivasa 	if (spa->spa_spares.sav_vdevs != NULL &&
20535cabbc6bSPrashanth Sreenivasa 	    nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
20545cabbc6bSPrashanth Sreenivasa 	    ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
20555cabbc6bSPrashanth Sreenivasa 	    (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
20565cabbc6bSPrashanth Sreenivasa 		/*
20575cabbc6bSPrashanth Sreenivasa 		 * Only remove the hot spare if it's not currently in use
20585cabbc6bSPrashanth Sreenivasa 		 * in this pool.
20595cabbc6bSPrashanth Sreenivasa 		 */
20605cabbc6bSPrashanth Sreenivasa 		if (vd == NULL || unspare) {
20615cabbc6bSPrashanth Sreenivasa 			char *nvstr = fnvlist_lookup_string(nv,
20625cabbc6bSPrashanth Sreenivasa 			    ZPOOL_CONFIG_PATH);
20635cabbc6bSPrashanth Sreenivasa 			spa_history_log_internal(spa, "vdev remove", NULL,
20645cabbc6bSPrashanth Sreenivasa 			    "%s vdev (%s) %s", spa_name(spa),
20655cabbc6bSPrashanth Sreenivasa 			    VDEV_TYPE_SPARE, nvstr);
20665cabbc6bSPrashanth Sreenivasa 			if (vd == NULL)
20675cabbc6bSPrashanth Sreenivasa 				vd = spa_lookup_by_guid(spa, guid, B_TRUE);
20685cabbc6bSPrashanth Sreenivasa 			ev = spa_event_create(spa, vd, NULL,
20695cabbc6bSPrashanth Sreenivasa 			    ESC_ZFS_VDEV_REMOVE_AUX);
20705cabbc6bSPrashanth Sreenivasa 			spa_vdev_remove_aux(spa->spa_spares.sav_config,
20715cabbc6bSPrashanth Sreenivasa 			    ZPOOL_CONFIG_SPARES, spares, nspares, nv);
20725cabbc6bSPrashanth Sreenivasa 			spa_load_spares(spa);
20735cabbc6bSPrashanth Sreenivasa 			spa->spa_spares.sav_sync = B_TRUE;
20745cabbc6bSPrashanth Sreenivasa 		} else {
20755cabbc6bSPrashanth Sreenivasa 			error = SET_ERROR(EBUSY);
20765cabbc6bSPrashanth Sreenivasa 		}
20775cabbc6bSPrashanth Sreenivasa 	} else if (spa->spa_l2cache.sav_vdevs != NULL &&
20785cabbc6bSPrashanth Sreenivasa 	    nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
20795cabbc6bSPrashanth Sreenivasa 	    ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
20805cabbc6bSPrashanth Sreenivasa 	    (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
20815cabbc6bSPrashanth Sreenivasa 		char *nvstr = fnvlist_lookup_string(nv, ZPOOL_CONFIG_PATH);
20825cabbc6bSPrashanth Sreenivasa 		spa_history_log_internal(spa, "vdev remove", NULL,
20835cabbc6bSPrashanth Sreenivasa 		    "%s vdev (%s) %s", spa_name(spa), VDEV_TYPE_L2CACHE, nvstr);
20845cabbc6bSPrashanth Sreenivasa 		/*
20855cabbc6bSPrashanth Sreenivasa 		 * Cache devices can always be removed.
20865cabbc6bSPrashanth Sreenivasa 		 */
20875cabbc6bSPrashanth Sreenivasa 		vd = spa_lookup_by_guid(spa, guid, B_TRUE);
20885cabbc6bSPrashanth Sreenivasa 		ev = spa_event_create(spa, vd, NULL, ESC_ZFS_VDEV_REMOVE_AUX);
20895cabbc6bSPrashanth Sreenivasa 		spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
20905cabbc6bSPrashanth Sreenivasa 		    ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
20915cabbc6bSPrashanth Sreenivasa 		spa_load_l2cache(spa);
20925cabbc6bSPrashanth Sreenivasa 		spa->spa_l2cache.sav_sync = B_TRUE;
20935cabbc6bSPrashanth Sreenivasa 	} else if (vd != NULL && vd->vdev_islog) {
20945cabbc6bSPrashanth Sreenivasa 		ASSERT(!locked);
20955cabbc6bSPrashanth Sreenivasa 		error = spa_vdev_remove_log(vd, &txg);
20965cabbc6bSPrashanth Sreenivasa 	} else if (vd != NULL) {
20975cabbc6bSPrashanth Sreenivasa 		ASSERT(!locked);
20985cabbc6bSPrashanth Sreenivasa 		error = spa_vdev_remove_top(vd, &txg);
20995cabbc6bSPrashanth Sreenivasa 	} else {
21005cabbc6bSPrashanth Sreenivasa 		/*
21015cabbc6bSPrashanth Sreenivasa 		 * There is no vdev of any kind with the specified guid.
21025cabbc6bSPrashanth Sreenivasa 		 */
21035cabbc6bSPrashanth Sreenivasa 		error = SET_ERROR(ENOENT);
21045cabbc6bSPrashanth Sreenivasa 	}
21055cabbc6bSPrashanth Sreenivasa 
21065cabbc6bSPrashanth Sreenivasa 	if (!locked)
21075cabbc6bSPrashanth Sreenivasa 		error = spa_vdev_exit(spa, NULL, txg, error);
21085cabbc6bSPrashanth Sreenivasa 
21095cabbc6bSPrashanth Sreenivasa 	if (ev != NULL) {
21105cabbc6bSPrashanth Sreenivasa 		if (error != 0) {
21115cabbc6bSPrashanth Sreenivasa 			spa_event_discard(ev);
21125cabbc6bSPrashanth Sreenivasa 		} else {
21135cabbc6bSPrashanth Sreenivasa 			spa_event_post(ev);
21145cabbc6bSPrashanth Sreenivasa 		}
21155cabbc6bSPrashanth Sreenivasa 	}
21165cabbc6bSPrashanth Sreenivasa 
21175cabbc6bSPrashanth Sreenivasa 	return (error);
21185cabbc6bSPrashanth Sreenivasa }
21195cabbc6bSPrashanth Sreenivasa 
21205cabbc6bSPrashanth Sreenivasa int
21215cabbc6bSPrashanth Sreenivasa spa_removal_get_stats(spa_t *spa, pool_removal_stat_t *prs)
21225cabbc6bSPrashanth Sreenivasa {
21235cabbc6bSPrashanth Sreenivasa 	prs->prs_state = spa->spa_removing_phys.sr_state;
21245cabbc6bSPrashanth Sreenivasa 
21255cabbc6bSPrashanth Sreenivasa 	if (prs->prs_state == DSS_NONE)
21265cabbc6bSPrashanth Sreenivasa 		return (SET_ERROR(ENOENT));
21275cabbc6bSPrashanth Sreenivasa 
21285cabbc6bSPrashanth Sreenivasa 	prs->prs_removing_vdev = spa->spa_removing_phys.sr_removing_vdev;
21295cabbc6bSPrashanth Sreenivasa 	prs->prs_start_time = spa->spa_removing_phys.sr_start_time;
21305cabbc6bSPrashanth Sreenivasa 	prs->prs_end_time = spa->spa_removing_phys.sr_end_time;
21315cabbc6bSPrashanth Sreenivasa 	prs->prs_to_copy = spa->spa_removing_phys.sr_to_copy;
21325cabbc6bSPrashanth Sreenivasa 	prs->prs_copied = spa->spa_removing_phys.sr_copied;
21335cabbc6bSPrashanth Sreenivasa 
21345cabbc6bSPrashanth Sreenivasa 	if (spa->spa_vdev_removal != NULL) {
21355cabbc6bSPrashanth Sreenivasa 		for (int i = 0; i < TXG_SIZE; i++) {
21365cabbc6bSPrashanth Sreenivasa 			prs->prs_copied +=
21375cabbc6bSPrashanth Sreenivasa 			    spa->spa_vdev_removal->svr_bytes_done[i];
21385cabbc6bSPrashanth Sreenivasa 		}
21395cabbc6bSPrashanth Sreenivasa 	}
21405cabbc6bSPrashanth Sreenivasa 
21415cabbc6bSPrashanth Sreenivasa 	prs->prs_mapping_memory = 0;
21425cabbc6bSPrashanth Sreenivasa 	uint64_t indirect_vdev_id =
21435cabbc6bSPrashanth Sreenivasa 	    spa->spa_removing_phys.sr_prev_indirect_vdev;
21445cabbc6bSPrashanth Sreenivasa 	while (indirect_vdev_id != -1) {
21455cabbc6bSPrashanth Sreenivasa 		vdev_t *vd = spa->spa_root_vdev->vdev_child[indirect_vdev_id];
21465cabbc6bSPrashanth Sreenivasa 		vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
21475cabbc6bSPrashanth Sreenivasa 		vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
21485cabbc6bSPrashanth Sreenivasa 
21495cabbc6bSPrashanth Sreenivasa 		ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops);
21505cabbc6bSPrashanth Sreenivasa 		prs->prs_mapping_memory += vdev_indirect_mapping_size(vim);
21515cabbc6bSPrashanth Sreenivasa 		indirect_vdev_id = vic->vic_prev_indirect_vdev;
21525cabbc6bSPrashanth Sreenivasa 	}
21535cabbc6bSPrashanth Sreenivasa 
21545cabbc6bSPrashanth Sreenivasa 	return (0);
21555cabbc6bSPrashanth Sreenivasa }
2156