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