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