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