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