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