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.
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.
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]
23 * Copyright (c) 2018, Intel Corporation.
24 * Copyright (c) 2020 by Lawrence Livermore National Security, LLC.
27 #include <sys/vdev_impl.h>
28 #include <sys/vdev_draid.h>
29 #include <sys/dsl_scan.h>
30 #include <sys/spa_impl.h>
31 #include <sys/metaslab_impl.h>
32 #include <sys/vdev_rebuild.h>
34 #include <sys/dmu_tx.h>
39 * This file contains the sequential reconstruction implementation for
40 * resilvering. This form of resilvering is internally referred to as device
41 * rebuild to avoid conflating it with the traditional healing reconstruction
42 * performed by the dsl scan code.
44 * When replacing a device, or scrubbing the pool, ZFS has historically used
45 * a process called resilvering which is a form of healing reconstruction.
46 * This approach has the advantage that as blocks are read from disk their
47 * checksums can be immediately verified and the data repaired. Unfortunately,
48 * it also results in a random IO pattern to the disk even when extra care
49 * is taken to sequentialize the IO as much as possible. This substantially
50 * increases the time required to resilver the pool and restore redundancy.
52 * For mirrored devices it's possible to implement an alternate sequential
53 * reconstruction strategy when resilvering. Sequential reconstruction
54 * behaves like a traditional RAID rebuild and reconstructs a device in LBA
55 * order without verifying the checksum. After this phase completes a second
56 * scrub phase is started to verify all of the checksums. This two phase
57 * process will take longer than the healing reconstruction described above.
58 * However, it has that advantage that after the reconstruction first phase
59 * completes redundancy has been restored. At this point the pool can incur
60 * another device failure without risking data loss.
62 * There are a few noteworthy limitations and other advantages of resilvering
63 * using sequential reconstruction vs healing reconstruction.
67 * - Sequential reconstruction is not possible on RAIDZ due to its
68 * variable stripe width. Note dRAID uses a fixed stripe width which
69 * avoids this issue, but comes at the expense of some usable capacity.
71 * - Block checksums are not verified during sequential reconstruction.
72 * Similar to traditional RAID the parity/mirror data is reconstructed
73 * but cannot be immediately double checked. For this reason when the
74 * last active resilver completes the pool is automatically scrubbed
77 * - Deferred resilvers using sequential reconstruction are not currently
78 * supported. When adding another vdev to an active top-level resilver
79 * it must be restarted.
83 * - Sequential reconstruction is performed in LBA order which may be faster
84 * than healing reconstruction particularly when using using HDDs (or
85 * especially with SMR devices). Only allocated capacity is resilvered.
87 * - Sequential reconstruction is not constrained by ZFS block boundaries.
88 * This allows it to issue larger IOs to disk which span multiple blocks
89 * allowing all of these logical blocks to be repaired with a single IO.
91 * - Unlike a healing resilver or scrub which are pool wide operations,
92 * sequential reconstruction is handled by the top-level vdevs. This
93 * allows for it to be started or canceled on a top-level vdev without
94 * impacting any other top-level vdevs in the pool.
96 * - Data only referenced by a pool checkpoint will be repaired because
97 * that space is reflected in the space maps. This differs for a
98 * healing resilver or scrub which will not repair that data.
103 * Size of rebuild reads; defaults to 1MiB per data disk and is capped at
106 unsigned long zfs_rebuild_max_segment = 1024 * 1024;
109 * Maximum number of parallelly executed bytes per leaf vdev caused by a
110 * sequential resilver. We attempt to strike a balance here between keeping
111 * the vdev queues full of I/Os at all times and not overflowing the queues
112 * to cause long latency, which would cause long txg sync times.
114 * A large default value can be safely used here because the default target
115 * segment size is also large (zfs_rebuild_max_segment=1M). This helps keep
116 * the queue depth short.
118 * 32MB was selected as the default value to achieve good performance with
119 * a large 90-drive dRAID HDD configuration (draid2:8d:90c:2s). A sequential
120 * rebuild was unable to saturate all of the drives using smaller values.
121 * With a value of 32MB the sequential resilver write rate was measured at
122 * 800MB/s sustained while rebuilding to a distributed spare.
124 unsigned long zfs_rebuild_vdev_limit = 32 << 20;
127 * Automatically start a pool scrub when the last active sequential resilver
128 * completes in order to verify the checksums of all blocks which have been
129 * resilvered. This option is enabled by default and is strongly recommended.
131 int zfs_rebuild_scrub_enabled = 1;
134 * For vdev_rebuild_initiate_sync() and vdev_rebuild_reset_sync().
136 static void vdev_rebuild_thread(void *arg);
139 * Clear the per-vdev rebuild bytes value for a vdev tree.
142 clear_rebuild_bytes(vdev_t *vd)
144 vdev_stat_t *vs = &vd->vdev_stat;
146 for (uint64_t i = 0; i < vd->vdev_children; i++)
147 clear_rebuild_bytes(vd->vdev_child[i]);
149 mutex_enter(&vd->vdev_stat_lock);
150 vs->vs_rebuild_processed = 0;
151 mutex_exit(&vd->vdev_stat_lock);
155 * Determines whether a vdev_rebuild_thread() should be stopped.
158 vdev_rebuild_should_stop(vdev_t *vd)
160 return (!vdev_writeable(vd) || vd->vdev_removing ||
161 vd->vdev_rebuild_exit_wanted ||
162 vd->vdev_rebuild_cancel_wanted ||
163 vd->vdev_rebuild_reset_wanted);
167 * Determine if the rebuild should be canceled. This may happen when all
168 * vdevs with MISSING DTLs are detached.
171 vdev_rebuild_should_cancel(vdev_t *vd)
173 vdev_rebuild_t *vr = &vd->vdev_rebuild_config;
174 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
176 if (!vdev_resilver_needed(vd, &vrp->vrp_min_txg, &vrp->vrp_max_txg))
183 * The sync task for updating the on-disk state of a rebuild. This is
184 * scheduled by vdev_rebuild_range().
187 vdev_rebuild_update_sync(void *arg, dmu_tx_t *tx)
189 int vdev_id = (uintptr_t)arg;
190 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
191 vdev_t *vd = vdev_lookup_top(spa, vdev_id);
192 vdev_rebuild_t *vr = &vd->vdev_rebuild_config;
193 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
194 uint64_t txg = dmu_tx_get_txg(tx);
196 mutex_enter(&vd->vdev_rebuild_lock);
198 if (vr->vr_scan_offset[txg & TXG_MASK] > 0) {
199 vrp->vrp_last_offset = vr->vr_scan_offset[txg & TXG_MASK];
200 vr->vr_scan_offset[txg & TXG_MASK] = 0;
203 vrp->vrp_scan_time_ms = vr->vr_prev_scan_time_ms +
204 NSEC2MSEC(gethrtime() - vr->vr_pass_start_time);
206 VERIFY0(zap_update(vd->vdev_spa->spa_meta_objset, vd->vdev_top_zap,
207 VDEV_TOP_ZAP_VDEV_REBUILD_PHYS, sizeof (uint64_t),
208 REBUILD_PHYS_ENTRIES, vrp, tx));
210 mutex_exit(&vd->vdev_rebuild_lock);
214 * Initialize the on-disk state for a new rebuild, start the rebuild thread.
217 vdev_rebuild_initiate_sync(void *arg, dmu_tx_t *tx)
219 int vdev_id = (uintptr_t)arg;
220 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
221 vdev_t *vd = vdev_lookup_top(spa, vdev_id);
222 vdev_rebuild_t *vr = &vd->vdev_rebuild_config;
223 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
225 ASSERT(vd->vdev_rebuilding);
227 spa_feature_incr(vd->vdev_spa, SPA_FEATURE_DEVICE_REBUILD, tx);
229 mutex_enter(&vd->vdev_rebuild_lock);
230 bzero(vrp, sizeof (uint64_t) * REBUILD_PHYS_ENTRIES);
231 vrp->vrp_rebuild_state = VDEV_REBUILD_ACTIVE;
232 vrp->vrp_min_txg = 0;
233 vrp->vrp_max_txg = dmu_tx_get_txg(tx);
234 vrp->vrp_start_time = gethrestime_sec();
235 vrp->vrp_scan_time_ms = 0;
236 vr->vr_prev_scan_time_ms = 0;
239 * Rebuilds are currently only used when replacing a device, in which
240 * case there must be DTL_MISSING entries. In the future, we could
241 * allow rebuilds to be used in a way similar to a scrub. This would
242 * be useful because it would allow us to rebuild the space used by
245 VERIFY(vdev_resilver_needed(vd, &vrp->vrp_min_txg, &vrp->vrp_max_txg));
247 VERIFY0(zap_update(vd->vdev_spa->spa_meta_objset, vd->vdev_top_zap,
248 VDEV_TOP_ZAP_VDEV_REBUILD_PHYS, sizeof (uint64_t),
249 REBUILD_PHYS_ENTRIES, vrp, tx));
251 spa_history_log_internal(spa, "rebuild", tx,
252 "vdev_id=%llu vdev_guid=%llu started",
253 (u_longlong_t)vd->vdev_id, (u_longlong_t)vd->vdev_guid);
255 ASSERT3P(vd->vdev_rebuild_thread, ==, NULL);
256 vd->vdev_rebuild_thread = thread_create(NULL, 0,
257 vdev_rebuild_thread, vd, 0, &p0, TS_RUN, maxclsyspri);
259 mutex_exit(&vd->vdev_rebuild_lock);
263 vdev_rebuild_log_notify(spa_t *spa, vdev_t *vd, char *name)
265 nvlist_t *aux = fnvlist_alloc();
267 fnvlist_add_string(aux, ZFS_EV_RESILVER_TYPE, "sequential");
268 spa_event_notify(spa, vd, aux, name);
273 * Called to request that a new rebuild be started. The feature will remain
274 * active for the duration of the rebuild, then revert to the enabled state.
277 vdev_rebuild_initiate(vdev_t *vd)
279 spa_t *spa = vd->vdev_spa;
281 ASSERT(vd->vdev_top == vd);
282 ASSERT(MUTEX_HELD(&vd->vdev_rebuild_lock));
283 ASSERT(!vd->vdev_rebuilding);
285 dmu_tx_t *tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
286 VERIFY0(dmu_tx_assign(tx, TXG_WAIT));
288 vd->vdev_rebuilding = B_TRUE;
290 dsl_sync_task_nowait(spa_get_dsl(spa), vdev_rebuild_initiate_sync,
291 (void *)(uintptr_t)vd->vdev_id, tx);
294 vdev_rebuild_log_notify(spa, vd, ESC_ZFS_RESILVER_START);
298 * Update the on-disk state to completed when a rebuild finishes.
301 vdev_rebuild_complete_sync(void *arg, dmu_tx_t *tx)
303 int vdev_id = (uintptr_t)arg;
304 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
305 vdev_t *vd = vdev_lookup_top(spa, vdev_id);
306 vdev_rebuild_t *vr = &vd->vdev_rebuild_config;
307 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
309 mutex_enter(&vd->vdev_rebuild_lock);
310 vrp->vrp_rebuild_state = VDEV_REBUILD_COMPLETE;
311 vrp->vrp_end_time = gethrestime_sec();
313 VERIFY0(zap_update(vd->vdev_spa->spa_meta_objset, vd->vdev_top_zap,
314 VDEV_TOP_ZAP_VDEV_REBUILD_PHYS, sizeof (uint64_t),
315 REBUILD_PHYS_ENTRIES, vrp, tx));
317 vdev_dtl_reassess(vd, tx->tx_txg, vrp->vrp_max_txg, B_TRUE, B_TRUE);
318 spa_feature_decr(vd->vdev_spa, SPA_FEATURE_DEVICE_REBUILD, tx);
320 spa_history_log_internal(spa, "rebuild", tx,
321 "vdev_id=%llu vdev_guid=%llu complete",
322 (u_longlong_t)vd->vdev_id, (u_longlong_t)vd->vdev_guid);
323 vdev_rebuild_log_notify(spa, vd, ESC_ZFS_RESILVER_FINISH);
325 /* Handles detaching of spares */
326 spa_async_request(spa, SPA_ASYNC_REBUILD_DONE);
327 vd->vdev_rebuilding = B_FALSE;
328 mutex_exit(&vd->vdev_rebuild_lock);
331 * While we're in syncing context take the opportunity to
332 * setup the scrub when there are no more active rebuilds.
334 if (!vdev_rebuild_active(spa->spa_root_vdev) &&
335 zfs_rebuild_scrub_enabled) {
336 pool_scan_func_t func = POOL_SCAN_SCRUB;
337 dsl_scan_setup_sync(&func, tx);
340 cv_broadcast(&vd->vdev_rebuild_cv);
344 * Update the on-disk state to canceled when a rebuild finishes.
347 vdev_rebuild_cancel_sync(void *arg, dmu_tx_t *tx)
349 int vdev_id = (uintptr_t)arg;
350 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
351 vdev_t *vd = vdev_lookup_top(spa, vdev_id);
352 vdev_rebuild_t *vr = &vd->vdev_rebuild_config;
353 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
355 mutex_enter(&vd->vdev_rebuild_lock);
356 vrp->vrp_rebuild_state = VDEV_REBUILD_CANCELED;
357 vrp->vrp_end_time = gethrestime_sec();
359 VERIFY0(zap_update(vd->vdev_spa->spa_meta_objset, vd->vdev_top_zap,
360 VDEV_TOP_ZAP_VDEV_REBUILD_PHYS, sizeof (uint64_t),
361 REBUILD_PHYS_ENTRIES, vrp, tx));
363 spa_feature_decr(vd->vdev_spa, SPA_FEATURE_DEVICE_REBUILD, tx);
365 spa_history_log_internal(spa, "rebuild", tx,
366 "vdev_id=%llu vdev_guid=%llu canceled",
367 (u_longlong_t)vd->vdev_id, (u_longlong_t)vd->vdev_guid);
368 vdev_rebuild_log_notify(spa, vd, ESC_ZFS_RESILVER_FINISH);
370 vd->vdev_rebuild_cancel_wanted = B_FALSE;
371 vd->vdev_rebuilding = B_FALSE;
372 mutex_exit(&vd->vdev_rebuild_lock);
374 spa_notify_waiters(spa);
375 cv_broadcast(&vd->vdev_rebuild_cv);
379 * Resets the progress of a running rebuild. This will occur when a new
380 * vdev is added to rebuild.
383 vdev_rebuild_reset_sync(void *arg, dmu_tx_t *tx)
385 int vdev_id = (uintptr_t)arg;
386 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
387 vdev_t *vd = vdev_lookup_top(spa, vdev_id);
388 vdev_rebuild_t *vr = &vd->vdev_rebuild_config;
389 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
391 mutex_enter(&vd->vdev_rebuild_lock);
393 ASSERT(vrp->vrp_rebuild_state == VDEV_REBUILD_ACTIVE);
394 ASSERT3P(vd->vdev_rebuild_thread, ==, NULL);
396 vrp->vrp_last_offset = 0;
397 vrp->vrp_min_txg = 0;
398 vrp->vrp_max_txg = dmu_tx_get_txg(tx);
399 vrp->vrp_bytes_scanned = 0;
400 vrp->vrp_bytes_issued = 0;
401 vrp->vrp_bytes_rebuilt = 0;
402 vrp->vrp_bytes_est = 0;
403 vrp->vrp_scan_time_ms = 0;
404 vr->vr_prev_scan_time_ms = 0;
406 /* See vdev_rebuild_initiate_sync comment */
407 VERIFY(vdev_resilver_needed(vd, &vrp->vrp_min_txg, &vrp->vrp_max_txg));
409 VERIFY0(zap_update(vd->vdev_spa->spa_meta_objset, vd->vdev_top_zap,
410 VDEV_TOP_ZAP_VDEV_REBUILD_PHYS, sizeof (uint64_t),
411 REBUILD_PHYS_ENTRIES, vrp, tx));
413 spa_history_log_internal(spa, "rebuild", tx,
414 "vdev_id=%llu vdev_guid=%llu reset",
415 (u_longlong_t)vd->vdev_id, (u_longlong_t)vd->vdev_guid);
417 vd->vdev_rebuild_reset_wanted = B_FALSE;
418 ASSERT(vd->vdev_rebuilding);
420 vd->vdev_rebuild_thread = thread_create(NULL, 0,
421 vdev_rebuild_thread, vd, 0, &p0, TS_RUN, maxclsyspri);
423 mutex_exit(&vd->vdev_rebuild_lock);
427 * Clear the last rebuild status.
430 vdev_rebuild_clear_sync(void *arg, dmu_tx_t *tx)
432 int vdev_id = (uintptr_t)arg;
433 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
434 vdev_t *vd = vdev_lookup_top(spa, vdev_id);
435 vdev_rebuild_t *vr = &vd->vdev_rebuild_config;
436 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
437 objset_t *mos = spa_meta_objset(spa);
439 mutex_enter(&vd->vdev_rebuild_lock);
441 if (!spa_feature_is_enabled(spa, SPA_FEATURE_DEVICE_REBUILD) ||
442 vrp->vrp_rebuild_state == VDEV_REBUILD_ACTIVE) {
443 mutex_exit(&vd->vdev_rebuild_lock);
447 clear_rebuild_bytes(vd);
448 bzero(vrp, sizeof (uint64_t) * REBUILD_PHYS_ENTRIES);
450 if (vd->vdev_top_zap != 0 && zap_contains(mos, vd->vdev_top_zap,
451 VDEV_TOP_ZAP_VDEV_REBUILD_PHYS) == 0) {
452 VERIFY0(zap_update(mos, vd->vdev_top_zap,
453 VDEV_TOP_ZAP_VDEV_REBUILD_PHYS, sizeof (uint64_t),
454 REBUILD_PHYS_ENTRIES, vrp, tx));
457 mutex_exit(&vd->vdev_rebuild_lock);
461 * The zio_done_func_t callback for each rebuild I/O issued. It's responsible
462 * for updating the rebuild stats and limiting the number of in flight I/Os.
465 vdev_rebuild_cb(zio_t *zio)
467 vdev_rebuild_t *vr = zio->io_private;
468 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
469 vdev_t *vd = vr->vr_top_vdev;
471 mutex_enter(&vr->vr_io_lock);
472 if (zio->io_error == ENXIO && !vdev_writeable(vd)) {
474 * The I/O failed because the top-level vdev was unavailable.
475 * Attempt to roll back to the last completed offset, in order
476 * resume from the correct location if the pool is resumed.
477 * (This works because spa_sync waits on spa_txg_zio before
478 * it runs sync tasks.)
480 uint64_t *off = &vr->vr_scan_offset[zio->io_txg & TXG_MASK];
481 *off = MIN(*off, zio->io_offset);
482 } else if (zio->io_error) {
486 abd_free(zio->io_abd);
488 ASSERT3U(vr->vr_bytes_inflight, >, 0);
489 vr->vr_bytes_inflight -= zio->io_size;
490 cv_broadcast(&vr->vr_io_cv);
491 mutex_exit(&vr->vr_io_lock);
493 spa_config_exit(vd->vdev_spa, SCL_STATE_ALL, vd);
497 * Initialize a block pointer that can be used to read the given segment
498 * for sequential rebuild.
501 vdev_rebuild_blkptr_init(blkptr_t *bp, vdev_t *vd, uint64_t start,
504 ASSERT(vd->vdev_ops == &vdev_draid_ops ||
505 vd->vdev_ops == &vdev_mirror_ops ||
506 vd->vdev_ops == &vdev_replacing_ops ||
507 vd->vdev_ops == &vdev_spare_ops);
509 uint64_t psize = vd->vdev_ops == &vdev_draid_ops ?
510 vdev_draid_asize_to_psize(vd, asize) : asize;
514 DVA_SET_VDEV(&bp->blk_dva[0], vd->vdev_id);
515 DVA_SET_OFFSET(&bp->blk_dva[0], start);
516 DVA_SET_GANG(&bp->blk_dva[0], 0);
517 DVA_SET_ASIZE(&bp->blk_dva[0], asize);
519 BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL);
520 BP_SET_LSIZE(bp, psize);
521 BP_SET_PSIZE(bp, psize);
522 BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
523 BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF);
524 BP_SET_TYPE(bp, DMU_OT_NONE);
527 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
531 * Issues a rebuild I/O and takes care of rate limiting the number of queued
532 * rebuild I/Os. The provided start and size must be properly aligned for the
533 * top-level vdev type being rebuilt.
536 vdev_rebuild_range(vdev_rebuild_t *vr, uint64_t start, uint64_t size)
538 uint64_t ms_id __maybe_unused = vr->vr_scan_msp->ms_id;
539 vdev_t *vd = vr->vr_top_vdev;
540 spa_t *spa = vd->vdev_spa;
543 ASSERT3U(ms_id, ==, start >> vd->vdev_ms_shift);
544 ASSERT3U(ms_id, ==, (start + size - 1) >> vd->vdev_ms_shift);
546 vr->vr_pass_bytes_scanned += size;
547 vr->vr_rebuild_phys.vrp_bytes_scanned += size;
550 * Rebuild the data in this range by constructing a special block
551 * pointer. It has no relation to any existing blocks in the pool.
552 * However, by disabling checksum verification and issuing a scrub IO
553 * we can reconstruct and repair any children with missing data.
555 vdev_rebuild_blkptr_init(&blk, vd, start, size);
556 uint64_t psize = BP_GET_PSIZE(&blk);
558 if (!vdev_dtl_need_resilver(vd, &blk.blk_dva[0], psize, TXG_UNKNOWN))
561 mutex_enter(&vr->vr_io_lock);
563 /* Limit in flight rebuild I/Os */
564 while (vr->vr_bytes_inflight >= vr->vr_bytes_inflight_max)
565 cv_wait(&vr->vr_io_cv, &vr->vr_io_lock);
567 vr->vr_bytes_inflight += psize;
568 mutex_exit(&vr->vr_io_lock);
570 dmu_tx_t *tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
571 VERIFY0(dmu_tx_assign(tx, TXG_WAIT));
572 uint64_t txg = dmu_tx_get_txg(tx);
574 spa_config_enter(spa, SCL_STATE_ALL, vd, RW_READER);
575 mutex_enter(&vd->vdev_rebuild_lock);
577 /* This is the first I/O for this txg. */
578 if (vr->vr_scan_offset[txg & TXG_MASK] == 0) {
579 vr->vr_scan_offset[txg & TXG_MASK] = start;
580 dsl_sync_task_nowait(spa_get_dsl(spa),
581 vdev_rebuild_update_sync,
582 (void *)(uintptr_t)vd->vdev_id, tx);
585 /* When exiting write out our progress. */
586 if (vdev_rebuild_should_stop(vd)) {
587 mutex_enter(&vr->vr_io_lock);
588 vr->vr_bytes_inflight -= psize;
589 mutex_exit(&vr->vr_io_lock);
590 spa_config_exit(vd->vdev_spa, SCL_STATE_ALL, vd);
591 mutex_exit(&vd->vdev_rebuild_lock);
593 return (SET_ERROR(EINTR));
595 mutex_exit(&vd->vdev_rebuild_lock);
598 vr->vr_scan_offset[txg & TXG_MASK] = start + size;
599 vr->vr_pass_bytes_issued += size;
600 vr->vr_rebuild_phys.vrp_bytes_issued += size;
602 zio_nowait(zio_read(spa->spa_txg_zio[txg & TXG_MASK], spa, &blk,
603 abd_alloc(psize, B_FALSE), psize, vdev_rebuild_cb, vr,
604 ZIO_PRIORITY_REBUILD, ZIO_FLAG_RAW | ZIO_FLAG_CANFAIL |
605 ZIO_FLAG_RESILVER, NULL));
611 * Issues rebuild I/Os for all ranges in the provided vr->vr_tree range tree.
614 vdev_rebuild_ranges(vdev_rebuild_t *vr)
616 vdev_t *vd = vr->vr_top_vdev;
617 zfs_btree_t *t = &vr->vr_scan_tree->rt_root;
618 zfs_btree_index_t idx;
621 for (range_seg_t *rs = zfs_btree_first(t, &idx); rs != NULL;
622 rs = zfs_btree_next(t, &idx, &idx)) {
623 uint64_t start = rs_get_start(rs, vr->vr_scan_tree);
624 uint64_t size = rs_get_end(rs, vr->vr_scan_tree) - start;
627 * zfs_scan_suspend_progress can be set to disable rebuild
628 * progress for testing. See comment in dsl_scan_sync().
630 while (zfs_scan_suspend_progress &&
631 !vdev_rebuild_should_stop(vd)) {
639 * Split range into legally-sized logical chunks
640 * given the constraints of the top-level vdev
641 * being rebuilt (dRAID or mirror).
643 ASSERT3P(vd->vdev_ops, !=, NULL);
644 chunk_size = vd->vdev_ops->vdev_op_rebuild_asize(vd,
645 start, size, zfs_rebuild_max_segment);
647 error = vdev_rebuild_range(vr, start, chunk_size);
660 * Calculates the estimated capacity which remains to be scanned. Since
661 * we traverse the pool in metaslab order only allocated capacity beyond
662 * the vrp_last_offset need be considered. All lower offsets must have
663 * already been rebuilt and are thus already included in vrp_bytes_scanned.
666 vdev_rebuild_update_bytes_est(vdev_t *vd, uint64_t ms_id)
668 vdev_rebuild_t *vr = &vd->vdev_rebuild_config;
669 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
670 uint64_t bytes_est = vrp->vrp_bytes_scanned;
672 if (vrp->vrp_last_offset < vd->vdev_ms[ms_id]->ms_start)
675 for (uint64_t i = ms_id; i < vd->vdev_ms_count; i++) {
676 metaslab_t *msp = vd->vdev_ms[i];
678 mutex_enter(&msp->ms_lock);
679 bytes_est += metaslab_allocated_space(msp);
680 mutex_exit(&msp->ms_lock);
683 vrp->vrp_bytes_est = bytes_est;
687 * Load from disk the top-level vdev's rebuild information.
690 vdev_rebuild_load(vdev_t *vd)
692 vdev_rebuild_t *vr = &vd->vdev_rebuild_config;
693 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
694 spa_t *spa = vd->vdev_spa;
697 mutex_enter(&vd->vdev_rebuild_lock);
698 vd->vdev_rebuilding = B_FALSE;
700 if (!spa_feature_is_enabled(spa, SPA_FEATURE_DEVICE_REBUILD)) {
701 bzero(vrp, sizeof (uint64_t) * REBUILD_PHYS_ENTRIES);
702 mutex_exit(&vd->vdev_rebuild_lock);
703 return (SET_ERROR(ENOTSUP));
706 ASSERT(vd->vdev_top == vd);
708 err = zap_lookup(spa->spa_meta_objset, vd->vdev_top_zap,
709 VDEV_TOP_ZAP_VDEV_REBUILD_PHYS, sizeof (uint64_t),
710 REBUILD_PHYS_ENTRIES, vrp);
713 * A missing or damaged VDEV_TOP_ZAP_VDEV_REBUILD_PHYS should
714 * not prevent a pool from being imported. Clear the rebuild
715 * status allowing a new resilver/rebuild to be started.
717 if (err == ENOENT || err == EOVERFLOW || err == ECKSUM) {
718 bzero(vrp, sizeof (uint64_t) * REBUILD_PHYS_ENTRIES);
720 mutex_exit(&vd->vdev_rebuild_lock);
724 vr->vr_prev_scan_time_ms = vrp->vrp_scan_time_ms;
725 vr->vr_top_vdev = vd;
727 mutex_exit(&vd->vdev_rebuild_lock);
733 * Each scan thread is responsible for rebuilding a top-level vdev. The
734 * rebuild progress in tracked on-disk in VDEV_TOP_ZAP_VDEV_REBUILD_PHYS.
737 vdev_rebuild_thread(void *arg)
740 spa_t *spa = vd->vdev_spa;
744 * If there's a scrub in process request that it be stopped. This
745 * is not required for a correct rebuild, but we do want rebuilds to
746 * emulate the resilver behavior as much as possible.
748 dsl_pool_t *dsl = spa_get_dsl(spa);
749 if (dsl_scan_scrubbing(dsl))
750 dsl_scan_cancel(dsl);
752 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
753 mutex_enter(&vd->vdev_rebuild_lock);
755 ASSERT3P(vd->vdev_top, ==, vd);
756 ASSERT3P(vd->vdev_rebuild_thread, !=, NULL);
757 ASSERT(vd->vdev_rebuilding);
758 ASSERT(spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REBUILD));
759 ASSERT3B(vd->vdev_rebuild_cancel_wanted, ==, B_FALSE);
760 ASSERT3B(vd->vdev_rebuild_reset_wanted, ==, B_FALSE);
762 vdev_rebuild_t *vr = &vd->vdev_rebuild_config;
763 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
764 vr->vr_top_vdev = vd;
765 vr->vr_scan_msp = NULL;
766 vr->vr_scan_tree = range_tree_create(NULL, RANGE_SEG64, NULL, 0, 0);
767 mutex_init(&vr->vr_io_lock, NULL, MUTEX_DEFAULT, NULL);
768 cv_init(&vr->vr_io_cv, NULL, CV_DEFAULT, NULL);
770 vr->vr_pass_start_time = gethrtime();
771 vr->vr_pass_bytes_scanned = 0;
772 vr->vr_pass_bytes_issued = 0;
774 vr->vr_bytes_inflight_max = MAX(1ULL << 20,
775 zfs_rebuild_vdev_limit * vd->vdev_children);
777 uint64_t update_est_time = gethrtime();
778 vdev_rebuild_update_bytes_est(vd, 0);
780 clear_rebuild_bytes(vr->vr_top_vdev);
782 mutex_exit(&vd->vdev_rebuild_lock);
785 * Systematically walk the metaslabs and issue rebuild I/Os for
786 * all ranges in the allocated space map.
788 for (uint64_t i = 0; i < vd->vdev_ms_count; i++) {
789 metaslab_t *msp = vd->vdev_ms[i];
790 vr->vr_scan_msp = msp;
793 * Removal of vdevs from the vdev tree may eliminate the need
794 * for the rebuild, in which case it should be canceled. The
795 * vdev_rebuild_cancel_wanted flag is set until the sync task
796 * completes. This may be after the rebuild thread exits.
798 if (vdev_rebuild_should_cancel(vd)) {
799 vd->vdev_rebuild_cancel_wanted = B_TRUE;
804 ASSERT0(range_tree_space(vr->vr_scan_tree));
806 /* Disable any new allocations to this metaslab */
807 spa_config_exit(spa, SCL_CONFIG, FTAG);
808 metaslab_disable(msp);
810 mutex_enter(&msp->ms_sync_lock);
811 mutex_enter(&msp->ms_lock);
814 * If there are outstanding allocations wait for them to be
815 * synced. This is needed to ensure all allocated ranges are
816 * on disk and therefore will be rebuilt.
818 for (int j = 0; j < TXG_SIZE; j++) {
819 if (range_tree_space(msp->ms_allocating[j])) {
820 mutex_exit(&msp->ms_lock);
821 mutex_exit(&msp->ms_sync_lock);
822 txg_wait_synced(dsl, 0);
823 mutex_enter(&msp->ms_sync_lock);
824 mutex_enter(&msp->ms_lock);
830 * When a metaslab has been allocated from read its allocated
831 * ranges from the space map object into the vr_scan_tree.
832 * Then add inflight / unflushed ranges and remove inflight /
833 * unflushed frees. This is the minimum range to be rebuilt.
835 if (msp->ms_sm != NULL) {
836 VERIFY0(space_map_load(msp->ms_sm,
837 vr->vr_scan_tree, SM_ALLOC));
839 for (int i = 0; i < TXG_SIZE; i++) {
840 ASSERT0(range_tree_space(
841 msp->ms_allocating[i]));
844 range_tree_walk(msp->ms_unflushed_allocs,
845 range_tree_add, vr->vr_scan_tree);
846 range_tree_walk(msp->ms_unflushed_frees,
847 range_tree_remove, vr->vr_scan_tree);
850 * Remove ranges which have already been rebuilt based
851 * on the last offset. This can happen when restarting
852 * a scan after exporting and re-importing the pool.
854 range_tree_clear(vr->vr_scan_tree, 0,
855 vrp->vrp_last_offset);
858 mutex_exit(&msp->ms_lock);
859 mutex_exit(&msp->ms_sync_lock);
862 * To provide an accurate estimate re-calculate the estimated
863 * size every 5 minutes to account for recent allocations and
864 * frees made to space maps which have not yet been rebuilt.
866 if (gethrtime() > update_est_time + SEC2NSEC(300)) {
867 update_est_time = gethrtime();
868 vdev_rebuild_update_bytes_est(vd, i);
872 * Walk the allocated space map and issue the rebuild I/O.
874 error = vdev_rebuild_ranges(vr);
875 range_tree_vacate(vr->vr_scan_tree, NULL, NULL);
877 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
878 metaslab_enable(msp, B_FALSE, B_FALSE);
884 range_tree_destroy(vr->vr_scan_tree);
885 spa_config_exit(spa, SCL_CONFIG, FTAG);
887 /* Wait for any remaining rebuild I/O to complete */
888 mutex_enter(&vr->vr_io_lock);
889 while (vr->vr_bytes_inflight > 0)
890 cv_wait(&vr->vr_io_cv, &vr->vr_io_lock);
892 mutex_exit(&vr->vr_io_lock);
894 mutex_destroy(&vr->vr_io_lock);
895 cv_destroy(&vr->vr_io_cv);
897 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
899 dsl_pool_t *dp = spa_get_dsl(spa);
900 dmu_tx_t *tx = dmu_tx_create_dd(dp->dp_mos_dir);
901 VERIFY0(dmu_tx_assign(tx, TXG_WAIT));
903 mutex_enter(&vd->vdev_rebuild_lock);
906 * After a successful rebuild clear the DTLs of all ranges
907 * which were missing when the rebuild was started. These
908 * ranges must have been rebuilt as a consequence of rebuilding
909 * all allocated space. Note that unlike a scrub or resilver
910 * the rebuild operation will reconstruct data only referenced
911 * by a pool checkpoint. See the dsl_scan_done() comments.
913 dsl_sync_task_nowait(dp, vdev_rebuild_complete_sync,
914 (void *)(uintptr_t)vd->vdev_id, tx);
915 } else if (vd->vdev_rebuild_cancel_wanted) {
917 * The rebuild operation was canceled. This will occur when
918 * a device participating in the rebuild is detached.
920 dsl_sync_task_nowait(dp, vdev_rebuild_cancel_sync,
921 (void *)(uintptr_t)vd->vdev_id, tx);
922 } else if (vd->vdev_rebuild_reset_wanted) {
924 * Reset the running rebuild without canceling and restarting
925 * it. This will occur when a new device is attached and must
926 * participate in the rebuild.
928 dsl_sync_task_nowait(dp, vdev_rebuild_reset_sync,
929 (void *)(uintptr_t)vd->vdev_id, tx);
932 * The rebuild operation should be suspended. This may occur
933 * when detaching a child vdev or when exporting the pool. The
934 * rebuild is left in the active state so it will be resumed.
936 ASSERT(vrp->vrp_rebuild_state == VDEV_REBUILD_ACTIVE);
937 vd->vdev_rebuilding = B_FALSE;
942 vd->vdev_rebuild_thread = NULL;
943 mutex_exit(&vd->vdev_rebuild_lock);
944 spa_config_exit(spa, SCL_CONFIG, FTAG);
946 cv_broadcast(&vd->vdev_rebuild_cv);
952 * Returns B_TRUE if any top-level vdev are rebuilding.
955 vdev_rebuild_active(vdev_t *vd)
957 spa_t *spa = vd->vdev_spa;
958 boolean_t ret = B_FALSE;
960 if (vd == spa->spa_root_vdev) {
961 for (uint64_t i = 0; i < vd->vdev_children; i++) {
962 ret = vdev_rebuild_active(vd->vdev_child[i]);
966 } else if (vd->vdev_top_zap != 0) {
967 vdev_rebuild_t *vr = &vd->vdev_rebuild_config;
968 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
970 mutex_enter(&vd->vdev_rebuild_lock);
971 ret = (vrp->vrp_rebuild_state == VDEV_REBUILD_ACTIVE);
972 mutex_exit(&vd->vdev_rebuild_lock);
979 * Start a rebuild operation. The rebuild may be restarted when the
980 * top-level vdev is currently actively rebuilding.
983 vdev_rebuild(vdev_t *vd)
985 vdev_rebuild_t *vr = &vd->vdev_rebuild_config;
986 vdev_rebuild_phys_t *vrp __maybe_unused = &vr->vr_rebuild_phys;
988 ASSERT(vd->vdev_top == vd);
989 ASSERT(vdev_is_concrete(vd));
990 ASSERT(!vd->vdev_removing);
991 ASSERT(spa_feature_is_enabled(vd->vdev_spa,
992 SPA_FEATURE_DEVICE_REBUILD));
994 mutex_enter(&vd->vdev_rebuild_lock);
995 if (vd->vdev_rebuilding) {
996 ASSERT3U(vrp->vrp_rebuild_state, ==, VDEV_REBUILD_ACTIVE);
999 * Signal a running rebuild operation that it should restart
1000 * from the beginning because a new device was attached. The
1001 * vdev_rebuild_reset_wanted flag is set until the sync task
1002 * completes. This may be after the rebuild thread exits.
1004 if (!vd->vdev_rebuild_reset_wanted)
1005 vd->vdev_rebuild_reset_wanted = B_TRUE;
1007 vdev_rebuild_initiate(vd);
1009 mutex_exit(&vd->vdev_rebuild_lock);
1013 vdev_rebuild_restart_impl(vdev_t *vd)
1015 spa_t *spa = vd->vdev_spa;
1017 if (vd == spa->spa_root_vdev) {
1018 for (uint64_t i = 0; i < vd->vdev_children; i++)
1019 vdev_rebuild_restart_impl(vd->vdev_child[i]);
1021 } else if (vd->vdev_top_zap != 0) {
1022 vdev_rebuild_t *vr = &vd->vdev_rebuild_config;
1023 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
1025 mutex_enter(&vd->vdev_rebuild_lock);
1026 if (vrp->vrp_rebuild_state == VDEV_REBUILD_ACTIVE &&
1027 vdev_writeable(vd) && !vd->vdev_rebuilding) {
1028 ASSERT(spa_feature_is_active(spa,
1029 SPA_FEATURE_DEVICE_REBUILD));
1030 vd->vdev_rebuilding = B_TRUE;
1031 vd->vdev_rebuild_thread = thread_create(NULL, 0,
1032 vdev_rebuild_thread, vd, 0, &p0, TS_RUN,
1035 mutex_exit(&vd->vdev_rebuild_lock);
1040 * Conditionally restart all of the vdev_rebuild_thread's for a pool. The
1041 * feature flag must be active and the rebuild in the active state. This
1042 * cannot be used to start a new rebuild.
1045 vdev_rebuild_restart(spa_t *spa)
1047 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1049 vdev_rebuild_restart_impl(spa->spa_root_vdev);
1053 * Stop and wait for all of the vdev_rebuild_thread's associated with the
1054 * vdev tree provide to be terminated (canceled or stopped).
1057 vdev_rebuild_stop_wait(vdev_t *vd)
1059 spa_t *spa = vd->vdev_spa;
1061 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1063 if (vd == spa->spa_root_vdev) {
1064 for (uint64_t i = 0; i < vd->vdev_children; i++)
1065 vdev_rebuild_stop_wait(vd->vdev_child[i]);
1067 } else if (vd->vdev_top_zap != 0) {
1068 ASSERT(vd == vd->vdev_top);
1070 mutex_enter(&vd->vdev_rebuild_lock);
1071 if (vd->vdev_rebuild_thread != NULL) {
1072 vd->vdev_rebuild_exit_wanted = B_TRUE;
1073 while (vd->vdev_rebuilding) {
1074 cv_wait(&vd->vdev_rebuild_cv,
1075 &vd->vdev_rebuild_lock);
1077 vd->vdev_rebuild_exit_wanted = B_FALSE;
1079 mutex_exit(&vd->vdev_rebuild_lock);
1084 * Stop all rebuild operations but leave them in the active state so they
1085 * will be resumed when importing the pool.
1088 vdev_rebuild_stop_all(spa_t *spa)
1090 vdev_rebuild_stop_wait(spa->spa_root_vdev);
1094 * Rebuild statistics reported per top-level vdev.
1097 vdev_rebuild_get_stats(vdev_t *tvd, vdev_rebuild_stat_t *vrs)
1099 spa_t *spa = tvd->vdev_spa;
1101 if (!spa_feature_is_enabled(spa, SPA_FEATURE_DEVICE_REBUILD))
1102 return (SET_ERROR(ENOTSUP));
1104 if (tvd != tvd->vdev_top || tvd->vdev_top_zap == 0)
1105 return (SET_ERROR(EINVAL));
1107 int error = zap_contains(spa_meta_objset(spa),
1108 tvd->vdev_top_zap, VDEV_TOP_ZAP_VDEV_REBUILD_PHYS);
1110 if (error == ENOENT) {
1111 bzero(vrs, sizeof (vdev_rebuild_stat_t));
1112 vrs->vrs_state = VDEV_REBUILD_NONE;
1114 } else if (error == 0) {
1115 vdev_rebuild_t *vr = &tvd->vdev_rebuild_config;
1116 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
1118 mutex_enter(&tvd->vdev_rebuild_lock);
1119 vrs->vrs_state = vrp->vrp_rebuild_state;
1120 vrs->vrs_start_time = vrp->vrp_start_time;
1121 vrs->vrs_end_time = vrp->vrp_end_time;
1122 vrs->vrs_scan_time_ms = vrp->vrp_scan_time_ms;
1123 vrs->vrs_bytes_scanned = vrp->vrp_bytes_scanned;
1124 vrs->vrs_bytes_issued = vrp->vrp_bytes_issued;
1125 vrs->vrs_bytes_rebuilt = vrp->vrp_bytes_rebuilt;
1126 vrs->vrs_bytes_est = vrp->vrp_bytes_est;
1127 vrs->vrs_errors = vrp->vrp_errors;
1128 vrs->vrs_pass_time_ms = NSEC2MSEC(gethrtime() -
1129 vr->vr_pass_start_time);
1130 vrs->vrs_pass_bytes_scanned = vr->vr_pass_bytes_scanned;
1131 vrs->vrs_pass_bytes_issued = vr->vr_pass_bytes_issued;
1132 mutex_exit(&tvd->vdev_rebuild_lock);
1139 ZFS_MODULE_PARAM(zfs, zfs_, rebuild_max_segment, ULONG, ZMOD_RW,
1140 "Max segment size in bytes of rebuild reads");
1142 ZFS_MODULE_PARAM(zfs, zfs_, rebuild_vdev_limit, ULONG, ZMOD_RW,
1143 "Max bytes in flight per leaf vdev for sequential resilvers");
1145 ZFS_MODULE_PARAM(zfs, zfs_, rebuild_scrub_enabled, INT, ZMOD_RW,
1146 "Automatically scrub after sequential resilver completes");