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