1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * This file and its contents are supplied under the terms of the
4 * Common Development and Distribution License ("CDDL"), version 1.0.
5 * You may only use this file in accordance with the terms of version
6 * 1.0 of the CDDL.
7 *
8 * A full copy of the text of the CDDL should have accompanied this
9 * source. A copy of the CDDL is also available via the Internet at
10 * https://opensource.org/license/CDDL-1.0.
11 */
12 /*
13 *
14 * Copyright (c) 2018, Intel Corporation.
15 * Copyright (c) 2020 by Lawrence Livermore National Security, LLC.
16 * Copyright (c) 2022, 2026 Hewlett Packard Enterprise Development LP.
17 * Copyright (c) 2024 by Delphix. All rights reserved.
18 */
19
20 #include <sys/vdev_impl.h>
21 #include <sys/vdev_draid.h>
22 #include <sys/dsl_scan.h>
23 #include <sys/spa_impl.h>
24 #include <sys/metaslab_impl.h>
25 #include <sys/vdev_rebuild.h>
26 #include <sys/zio.h>
27 #include <sys/dmu_tx.h>
28 #include <sys/arc.h>
29 #include <sys/arc_impl.h>
30 #include <sys/zap.h>
31
32 /*
33 * This file contains the sequential reconstruction implementation for
34 * resilvering. This form of resilvering is internally referred to as device
35 * rebuild to avoid conflating it with the traditional healing reconstruction
36 * performed by the dsl scan code.
37 *
38 * When replacing a device, or scrubbing the pool, ZFS has historically used
39 * a process called resilvering which is a form of healing reconstruction.
40 * This approach has the advantage that as blocks are read from disk their
41 * checksums can be immediately verified and the data repaired. Unfortunately,
42 * it also results in a random IO pattern to the disk even when extra care
43 * is taken to sequentialize the IO as much as possible. This substantially
44 * increases the time required to resilver the pool and restore redundancy.
45 *
46 * For mirrored devices it's possible to implement an alternate sequential
47 * reconstruction strategy when resilvering. Sequential reconstruction
48 * behaves like a traditional RAID rebuild and reconstructs a device in LBA
49 * order without verifying the checksum. After this phase completes a second
50 * scrub phase is started to verify all of the checksums. This two phase
51 * process will take longer than the healing reconstruction described above.
52 * However, it has that advantage that after the reconstruction first phase
53 * completes redundancy has been restored. At this point the pool can incur
54 * another device failure without risking data loss.
55 *
56 * There are a few noteworthy limitations and other advantages of resilvering
57 * using sequential reconstruction vs healing reconstruction.
58 *
59 * Limitations:
60 *
61 * - Sequential reconstruction is not possible on RAIDZ due to its
62 * variable stripe width. Note dRAID uses a fixed stripe width which
63 * avoids this issue, but comes at the expense of some usable capacity.
64 *
65 * - Block checksums are not verified during sequential reconstruction.
66 * Similar to traditional RAID the parity/mirror data is reconstructed
67 * but cannot be immediately double checked. For this reason when the
68 * last active resilver completes the pool is automatically scrubbed
69 * by default.
70 *
71 * - Deferred resilvers using sequential reconstruction are not currently
72 * supported. When adding another vdev to an active top-level resilver
73 * it must be restarted.
74 *
75 * Advantages:
76 *
77 * - Sequential reconstruction is performed in LBA order which may be faster
78 * than healing reconstruction particularly when using HDDs (or
79 * especially with SMR devices). Only allocated capacity is resilvered.
80 *
81 * - Sequential reconstruction is not constrained by ZFS block boundaries.
82 * This allows it to issue larger IOs to disk which span multiple blocks
83 * allowing all of these logical blocks to be repaired with a single IO.
84 *
85 * - Unlike a healing resilver or scrub which are pool wide operations,
86 * sequential reconstruction is handled by the top-level vdevs. This
87 * allows for it to be started or canceled on a top-level vdev without
88 * impacting any other top-level vdevs in the pool.
89 *
90 * - Data only referenced by a pool checkpoint will be repaired because
91 * that space is reflected in the space maps. This differs for a
92 * healing resilver or scrub which will not repair that data.
93 */
94
95
96 /*
97 * Size of rebuild reads; defaults to 1MiB per data disk and is capped at
98 * SPA_MAXBLOCKSIZE.
99 */
100 static uint64_t zfs_rebuild_max_segment = 1024 * 1024;
101
102 /*
103 * Maximum number of parallelly executed bytes per leaf vdev caused by a
104 * sequential resilver. We attempt to strike a balance here between keeping
105 * the vdev queues full of I/Os at all times and not overflowing the queues
106 * to cause long latency, which would cause long txg sync times.
107 *
108 * A large default value can be safely used here because the default target
109 * segment size is also large (zfs_rebuild_max_segment=1M). This helps keep
110 * the queue depth short.
111 *
112 * 64MB was observed to deliver the best performance and set as the default.
113 * Testing was performed with a 106-drive dRAID HDD pool (draid2:11d:106c)
114 * and a rebuild rate of 1.2GB/s was measured to the distribute spare.
115 * Smaller values were unable to fully saturate the available pool I/O.
116 */
117 static uint64_t zfs_rebuild_vdev_limit = 64 << 20;
118
119 /*
120 * Automatically start a pool scrub when the last active sequential resilver
121 * completes in order to verify the checksums of all blocks which have been
122 * resilvered. This option is enabled by default and is strongly recommended.
123 */
124 static int zfs_rebuild_scrub_enabled = 1;
125
126 /*
127 * For vdev_rebuild_initiate_sync() and vdev_rebuild_reset_sync().
128 */
129 static __attribute__((noreturn)) void vdev_rebuild_thread(void *arg);
130 static void vdev_rebuild_reset_sync(void *arg, dmu_tx_t *tx);
131
132 /*
133 * Clear the per-vdev rebuild bytes value for a vdev tree.
134 */
135 static void
clear_rebuild_bytes(vdev_t * vd)136 clear_rebuild_bytes(vdev_t *vd)
137 {
138 vdev_stat_t *vs = &vd->vdev_stat;
139
140 for (uint64_t i = 0; i < vd->vdev_children; i++)
141 clear_rebuild_bytes(vd->vdev_child[i]);
142
143 mutex_enter(&vd->vdev_stat_lock);
144 vs->vs_rebuild_processed = 0;
145 mutex_exit(&vd->vdev_stat_lock);
146 }
147
148 /*
149 * Determines whether a vdev_rebuild_thread() should be stopped.
150 */
151 static boolean_t
vdev_rebuild_should_stop(vdev_t * vd)152 vdev_rebuild_should_stop(vdev_t *vd)
153 {
154 return (!vdev_writeable(vd) || vd->vdev_removing ||
155 vd->vdev_rebuild_exit_wanted ||
156 vd->vdev_rebuild_cancel_wanted ||
157 vd->vdev_rebuild_reset_wanted);
158 }
159
160 /*
161 * Determine if the rebuild should be canceled. This may happen when all
162 * vdevs with MISSING DTLs are detached.
163 */
164 static boolean_t
vdev_rebuild_should_cancel(vdev_t * vd)165 vdev_rebuild_should_cancel(vdev_t *vd)
166 {
167 vdev_rebuild_t *vr = &vd->vdev_rebuild_config;
168 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
169
170 if (!vdev_resilver_needed(vd, &vrp->vrp_min_txg, &vrp->vrp_max_txg))
171 return (B_TRUE);
172
173 return (B_FALSE);
174 }
175
176 /*
177 * The sync task for updating the on-disk state of a rebuild. This is
178 * scheduled by vdev_rebuild_range().
179 */
180 static void
vdev_rebuild_update_sync(void * arg,dmu_tx_t * tx)181 vdev_rebuild_update_sync(void *arg, dmu_tx_t *tx)
182 {
183 int vdev_id = (uintptr_t)arg;
184 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
185 vdev_t *vd = vdev_lookup_top(spa, vdev_id);
186 vdev_rebuild_t *vr = &vd->vdev_rebuild_config;
187 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
188 uint64_t txg = dmu_tx_get_txg(tx);
189
190 mutex_enter(&vd->vdev_rebuild_lock);
191
192 if (vr->vr_scan_offset[txg & TXG_MASK] > 0) {
193 vrp->vrp_last_offset = vr->vr_scan_offset[txg & TXG_MASK];
194 vr->vr_scan_offset[txg & TXG_MASK] = 0;
195 }
196
197 vrp->vrp_scan_time_ms = vr->vr_prev_scan_time_ms +
198 NSEC2MSEC(gethrtime() - vr->vr_pass_start_time);
199
200 VERIFY0(zap_update(vd->vdev_spa->spa_meta_objset, vd->vdev_top_zap,
201 VDEV_TOP_ZAP_VDEV_REBUILD_PHYS, sizeof (uint64_t),
202 REBUILD_PHYS_ENTRIES, vrp, tx));
203
204 mutex_exit(&vd->vdev_rebuild_lock);
205 }
206
207 /*
208 * Initialize the on-disk state for a new rebuild, start the rebuild thread.
209 */
210 static void
vdev_rebuild_initiate_sync(void * arg,dmu_tx_t * tx)211 vdev_rebuild_initiate_sync(void *arg, dmu_tx_t *tx)
212 {
213 int vdev_id = (uintptr_t)arg;
214 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
215 vdev_t *vd = vdev_lookup_top(spa, vdev_id);
216 vdev_rebuild_t *vr = &vd->vdev_rebuild_config;
217 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
218
219 ASSERT(vd->vdev_rebuilding);
220
221 spa_feature_incr(vd->vdev_spa, SPA_FEATURE_DEVICE_REBUILD, tx);
222
223 mutex_enter(&vd->vdev_rebuild_lock);
224 memset(vrp, 0, sizeof (uint64_t) * REBUILD_PHYS_ENTRIES);
225 vrp->vrp_rebuild_state = VDEV_REBUILD_ACTIVE;
226 vrp->vrp_min_txg = TXG_INITIAL;
227 vrp->vrp_max_txg = dmu_tx_get_txg(tx);
228 vrp->vrp_start_time = gethrestime_sec();
229 vrp->vrp_scan_time_ms = 0;
230 vr->vr_prev_scan_time_ms = 0;
231
232 /*
233 * Rebuilds are currently only used when replacing a device, in which
234 * case there must be DTL_MISSING entries. In the future, we could
235 * allow rebuilds to be used in a way similar to a scrub. This would
236 * be useful because it would allow us to rebuild the space used by
237 * pool checkpoints.
238 */
239 VERIFY(vdev_resilver_needed(vd, &vrp->vrp_min_txg, &vrp->vrp_max_txg));
240
241 VERIFY0(zap_update(vd->vdev_spa->spa_meta_objset, vd->vdev_top_zap,
242 VDEV_TOP_ZAP_VDEV_REBUILD_PHYS, sizeof (uint64_t),
243 REBUILD_PHYS_ENTRIES, vrp, tx));
244
245 spa_history_log_internal(spa, "rebuild", tx,
246 "vdev_id=%llu vdev_guid=%llu started",
247 (u_longlong_t)vd->vdev_id, (u_longlong_t)vd->vdev_guid);
248
249 ASSERT0P(vd->vdev_rebuild_thread);
250 vd->vdev_rebuild_thread = thread_create(NULL, 0,
251 vdev_rebuild_thread, vd, 0, &p0, TS_RUN, maxclsyspri);
252
253 mutex_exit(&vd->vdev_rebuild_lock);
254 }
255
256 static void
vdev_rebuild_log_notify(spa_t * spa,vdev_t * vd,const char * name)257 vdev_rebuild_log_notify(spa_t *spa, vdev_t *vd, const char *name)
258 {
259 nvlist_t *aux = fnvlist_alloc();
260
261 fnvlist_add_string(aux, ZFS_EV_RESILVER_TYPE, "sequential");
262 spa_event_notify(spa, vd, aux, name);
263 nvlist_free(aux);
264 }
265
266 /*
267 * Called to request that a new rebuild be started. The feature will remain
268 * active for the duration of the rebuild, then revert to the enabled state.
269 */
270 static void
vdev_rebuild_initiate(vdev_t * vd,uint64_t txg)271 vdev_rebuild_initiate(vdev_t *vd, uint64_t txg)
272 {
273 spa_t *spa = vd->vdev_spa;
274
275 ASSERT(vd->vdev_top == vd);
276 ASSERT(MUTEX_HELD(&vd->vdev_rebuild_lock));
277 ASSERT(!vd->vdev_rebuilding);
278
279 dmu_tx_t *tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
280
281 vd->vdev_rebuilding = B_TRUE;
282
283 dsl_sync_task_nowait(spa_get_dsl(spa), vdev_rebuild_initiate_sync,
284 (void *)(uintptr_t)vd->vdev_id, tx);
285 dmu_tx_commit(tx);
286
287 vdev_rebuild_log_notify(spa, vd, ESC_ZFS_RESILVER_START);
288 }
289
290 /*
291 * Update the on-disk state to completed when a rebuild finishes.
292 */
293 static void
vdev_rebuild_complete_sync(void * arg,dmu_tx_t * tx)294 vdev_rebuild_complete_sync(void *arg, dmu_tx_t *tx)
295 {
296 int vdev_id = (uintptr_t)arg;
297 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
298 vdev_t *vd = vdev_lookup_top(spa, vdev_id);
299 vdev_rebuild_t *vr = &vd->vdev_rebuild_config;
300 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
301
302 mutex_enter(&vd->vdev_rebuild_lock);
303
304 /*
305 * Handle a second device failure if it occurs after all rebuild I/O
306 * has completed but before this sync task has been executed.
307 */
308 if (vd->vdev_rebuild_reset_wanted) {
309 mutex_exit(&vd->vdev_rebuild_lock);
310 vdev_rebuild_reset_sync(arg, tx);
311 return;
312 }
313
314 vrp->vrp_rebuild_state = VDEV_REBUILD_COMPLETE;
315 vrp->vrp_end_time = gethrestime_sec();
316
317 VERIFY0(zap_update(vd->vdev_spa->spa_meta_objset, vd->vdev_top_zap,
318 VDEV_TOP_ZAP_VDEV_REBUILD_PHYS, sizeof (uint64_t),
319 REBUILD_PHYS_ENTRIES, vrp, tx));
320
321 vdev_dtl_reassess(vd, tx->tx_txg, vrp->vrp_max_txg, B_TRUE, B_TRUE);
322 spa_feature_decr(vd->vdev_spa, SPA_FEATURE_DEVICE_REBUILD, tx);
323
324 spa_history_log_internal(spa, "rebuild", tx,
325 "vdev_id=%llu vdev_guid=%llu complete",
326 (u_longlong_t)vd->vdev_id, (u_longlong_t)vd->vdev_guid);
327 vdev_rebuild_log_notify(spa, vd, ESC_ZFS_RESILVER_FINISH);
328
329 /* Handles detaching of spares */
330 spa_async_request(spa, SPA_ASYNC_REBUILD_DONE);
331 vd->vdev_rebuilding = B_FALSE;
332 mutex_exit(&vd->vdev_rebuild_lock);
333
334 /*
335 * While we're in syncing context take the opportunity to
336 * setup the scrub when there are no more active rebuilds.
337 */
338 setup_sync_arg_t setup_sync_arg = {
339 .func = POOL_SCAN_SCRUB,
340 .txgstart = 0,
341 .txgend = 0,
342 };
343 if (dsl_scan_setup_check(&setup_sync_arg.func, tx) == 0 &&
344 zfs_rebuild_scrub_enabled) {
345 dsl_scan_setup_sync(&setup_sync_arg, tx);
346 }
347
348 cv_broadcast(&vd->vdev_rebuild_cv);
349
350 /* Clear recent error events (i.e. duplicate events tracking) */
351 zfs_ereport_clear(spa, NULL);
352 }
353
354 /*
355 * Update the on-disk state to canceled when a rebuild finishes.
356 */
357 static void
vdev_rebuild_cancel_sync(void * arg,dmu_tx_t * tx)358 vdev_rebuild_cancel_sync(void *arg, dmu_tx_t *tx)
359 {
360 int vdev_id = (uintptr_t)arg;
361 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
362 vdev_t *vd = vdev_lookup_top(spa, vdev_id);
363 vdev_rebuild_t *vr = &vd->vdev_rebuild_config;
364 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
365
366 mutex_enter(&vd->vdev_rebuild_lock);
367 vrp->vrp_rebuild_state = VDEV_REBUILD_CANCELED;
368 vrp->vrp_end_time = gethrestime_sec();
369
370 VERIFY0(zap_update(vd->vdev_spa->spa_meta_objset, vd->vdev_top_zap,
371 VDEV_TOP_ZAP_VDEV_REBUILD_PHYS, sizeof (uint64_t),
372 REBUILD_PHYS_ENTRIES, vrp, tx));
373
374 spa_feature_decr(vd->vdev_spa, SPA_FEATURE_DEVICE_REBUILD, tx);
375
376 spa_history_log_internal(spa, "rebuild", tx,
377 "vdev_id=%llu vdev_guid=%llu canceled",
378 (u_longlong_t)vd->vdev_id, (u_longlong_t)vd->vdev_guid);
379 vdev_rebuild_log_notify(spa, vd, ESC_ZFS_RESILVER_FINISH);
380
381 vd->vdev_rebuild_cancel_wanted = B_FALSE;
382 vd->vdev_rebuilding = B_FALSE;
383 mutex_exit(&vd->vdev_rebuild_lock);
384
385 spa_notify_waiters(spa);
386 cv_broadcast(&vd->vdev_rebuild_cv);
387 }
388
389 /*
390 * Resets the progress of a running rebuild. This will occur when a new
391 * vdev is added to rebuild.
392 */
393 static void
vdev_rebuild_reset_sync(void * arg,dmu_tx_t * tx)394 vdev_rebuild_reset_sync(void *arg, dmu_tx_t *tx)
395 {
396 int vdev_id = (uintptr_t)arg;
397 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
398 vdev_t *vd = vdev_lookup_top(spa, vdev_id);
399 vdev_rebuild_t *vr = &vd->vdev_rebuild_config;
400 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
401
402 mutex_enter(&vd->vdev_rebuild_lock);
403
404 ASSERT(vrp->vrp_rebuild_state == VDEV_REBUILD_ACTIVE);
405 ASSERT0P(vd->vdev_rebuild_thread);
406
407 vrp->vrp_last_offset = 0;
408 vrp->vrp_min_txg = TXG_INITIAL;
409 vrp->vrp_max_txg = dmu_tx_get_txg(tx);
410 vrp->vrp_bytes_scanned = 0;
411 vrp->vrp_bytes_issued = 0;
412 vrp->vrp_bytes_rebuilt = 0;
413 vrp->vrp_bytes_est = 0;
414 vrp->vrp_scan_time_ms = 0;
415 vr->vr_prev_scan_time_ms = 0;
416
417 /* See vdev_rebuild_initiate_sync comment */
418 VERIFY(vdev_resilver_needed(vd, &vrp->vrp_min_txg, &vrp->vrp_max_txg));
419
420 VERIFY0(zap_update(vd->vdev_spa->spa_meta_objset, vd->vdev_top_zap,
421 VDEV_TOP_ZAP_VDEV_REBUILD_PHYS, sizeof (uint64_t),
422 REBUILD_PHYS_ENTRIES, vrp, tx));
423
424 spa_history_log_internal(spa, "rebuild", tx,
425 "vdev_id=%llu vdev_guid=%llu reset",
426 (u_longlong_t)vd->vdev_id, (u_longlong_t)vd->vdev_guid);
427
428 vd->vdev_rebuild_reset_wanted = B_FALSE;
429 ASSERT(vd->vdev_rebuilding);
430
431 vd->vdev_rebuild_thread = thread_create(NULL, 0,
432 vdev_rebuild_thread, vd, 0, &p0, TS_RUN, maxclsyspri);
433
434 mutex_exit(&vd->vdev_rebuild_lock);
435 }
436
437 /*
438 * Clear the last rebuild status.
439 */
440 void
vdev_rebuild_clear_sync(void * arg,dmu_tx_t * tx)441 vdev_rebuild_clear_sync(void *arg, dmu_tx_t *tx)
442 {
443 int vdev_id = (uintptr_t)arg;
444 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
445 vdev_t *vd = vdev_lookup_top(spa, vdev_id);
446 vdev_rebuild_t *vr = &vd->vdev_rebuild_config;
447 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
448 objset_t *mos = spa_meta_objset(spa);
449
450 mutex_enter(&vd->vdev_rebuild_lock);
451
452 if (!spa_feature_is_enabled(spa, SPA_FEATURE_DEVICE_REBUILD) ||
453 vrp->vrp_rebuild_state == VDEV_REBUILD_ACTIVE) {
454 mutex_exit(&vd->vdev_rebuild_lock);
455 return;
456 }
457
458 clear_rebuild_bytes(vd);
459 memset(vrp, 0, sizeof (uint64_t) * REBUILD_PHYS_ENTRIES);
460
461 if (vd->vdev_top_zap != 0 && zap_contains(mos, vd->vdev_top_zap,
462 VDEV_TOP_ZAP_VDEV_REBUILD_PHYS) == 0) {
463 VERIFY0(zap_update(mos, vd->vdev_top_zap,
464 VDEV_TOP_ZAP_VDEV_REBUILD_PHYS, sizeof (uint64_t),
465 REBUILD_PHYS_ENTRIES, vrp, tx));
466 }
467
468 mutex_exit(&vd->vdev_rebuild_lock);
469 }
470
471 /*
472 * The zio_done_func_t callback for each rebuild I/O issued. It's responsible
473 * for updating the rebuild stats and limiting the number of in flight I/Os.
474 */
475 static void
vdev_rebuild_cb(zio_t * zio)476 vdev_rebuild_cb(zio_t *zio)
477 {
478 vdev_rebuild_t *vr = zio->io_private;
479 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
480 vdev_t *vd = vr->vr_top_vdev;
481
482 mutex_enter(&vr->vr_io_lock);
483 if (zio->io_error == ENXIO && !vdev_writeable(vd)) {
484 /*
485 * The I/O failed because the top-level vdev was unavailable.
486 * Attempt to roll back to the last completed offset, in order
487 * resume from the correct location if the pool is resumed.
488 * (This works because spa_sync waits on spa_txg_zio before
489 * it runs sync tasks.)
490 */
491 uint64_t *off = &vr->vr_scan_offset[zio->io_txg & TXG_MASK];
492 *off = MIN(*off, zio->io_offset);
493 } else if (zio->io_error) {
494 vrp->vrp_errors++;
495 }
496
497 abd_free(zio->io_abd);
498
499 ASSERT3U(vr->vr_bytes_inflight, >, 0);
500 vr->vr_bytes_inflight -= zio->io_size;
501 cv_broadcast(&vr->vr_io_cv);
502 mutex_exit(&vr->vr_io_lock);
503
504 spa_config_exit(vd->vdev_spa, SCL_STATE_ALL, vd);
505 }
506
507 /*
508 * Initialize a block pointer that can be used to read the given segment
509 * for sequential rebuild.
510 */
511 static void
vdev_rebuild_blkptr_init(blkptr_t * bp,vdev_t * vd,uint64_t start,uint64_t asize)512 vdev_rebuild_blkptr_init(blkptr_t *bp, vdev_t *vd, uint64_t start,
513 uint64_t asize)
514 {
515 ASSERT(vd->vdev_ops == &vdev_draid_ops ||
516 vd->vdev_ops == &vdev_mirror_ops ||
517 vd->vdev_ops == &vdev_replacing_ops ||
518 vd->vdev_ops == &vdev_spare_ops);
519
520 uint64_t psize = vd->vdev_ops == &vdev_draid_ops ?
521 vdev_draid_asize_to_psize(vd, asize, 0) : asize;
522
523 BP_ZERO(bp);
524
525 DVA_SET_VDEV(&bp->blk_dva[0], vd->vdev_id);
526 DVA_SET_OFFSET(&bp->blk_dva[0], start);
527 DVA_SET_GANG(&bp->blk_dva[0], 0);
528 DVA_SET_ASIZE(&bp->blk_dva[0], asize);
529
530 BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL);
531 BP_SET_LSIZE(bp, psize);
532 BP_SET_PSIZE(bp, psize);
533 BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
534 BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF);
535 BP_SET_TYPE(bp, DMU_OT_NONE);
536 BP_SET_LEVEL(bp, 0);
537 BP_SET_DEDUP(bp, 0);
538 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
539 }
540
541 /*
542 * Issues a rebuild I/O and takes care of rate limiting the number of queued
543 * rebuild I/Os. The provided start and size must be properly aligned for the
544 * top-level vdev type being rebuilt.
545 */
546 static int
vdev_rebuild_range(vdev_rebuild_t * vr,uint64_t start,uint64_t size)547 vdev_rebuild_range(vdev_rebuild_t *vr, uint64_t start, uint64_t size)
548 {
549 uint64_t ms_id __maybe_unused = vr->vr_scan_msp->ms_id;
550 vdev_t *vd = vr->vr_top_vdev;
551 spa_t *spa = vd->vdev_spa;
552 blkptr_t blk;
553
554 ASSERT3U(ms_id, ==, start >> vd->vdev_ms_shift);
555 ASSERT3U(ms_id, ==, (start + size - 1) >> vd->vdev_ms_shift);
556
557 vr->vr_pass_bytes_scanned += size;
558 vr->vr_rebuild_phys.vrp_bytes_scanned += size;
559
560 /*
561 * Rebuild the data in this range by constructing a special block
562 * pointer. It has no relation to any existing blocks in the pool.
563 * However, by disabling checksum verification and issuing a scrub IO
564 * we can reconstruct and repair any children with missing data.
565 */
566 vdev_rebuild_blkptr_init(&blk, vd, start, size);
567 uint64_t psize = BP_GET_PSIZE(&blk);
568
569 if (!vdev_dtl_need_resilver(vd, &blk.blk_dva[0], psize, TXG_UNKNOWN)) {
570 vr->vr_pass_bytes_skipped += size;
571 return (0);
572 }
573
574 mutex_enter(&vr->vr_io_lock);
575
576 /* Limit in flight rebuild I/Os */
577 while (vr->vr_bytes_inflight >= vr->vr_bytes_inflight_max)
578 cv_wait(&vr->vr_io_cv, &vr->vr_io_lock);
579
580 vr->vr_bytes_inflight += psize;
581 mutex_exit(&vr->vr_io_lock);
582
583 dmu_tx_t *tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
584 VERIFY0(dmu_tx_assign(tx, DMU_TX_WAIT | DMU_TX_SUSPEND));
585 uint64_t txg = dmu_tx_get_txg(tx);
586 vr->vr_last_txg = txg;
587
588 spa_config_enter(spa, SCL_STATE_ALL, vd, RW_READER);
589 mutex_enter(&vd->vdev_rebuild_lock);
590
591 /* This is the first I/O for this txg. */
592 if (vr->vr_scan_offset[txg & TXG_MASK] == 0) {
593 vr->vr_scan_offset[txg & TXG_MASK] = start;
594 dsl_sync_task_nowait(spa_get_dsl(spa),
595 vdev_rebuild_update_sync,
596 (void *)(uintptr_t)vd->vdev_id, tx);
597 }
598
599 /* When exiting write out our progress. */
600 if (vdev_rebuild_should_stop(vd)) {
601 mutex_enter(&vr->vr_io_lock);
602 vr->vr_bytes_inflight -= psize;
603 mutex_exit(&vr->vr_io_lock);
604 spa_config_exit(vd->vdev_spa, SCL_STATE_ALL, vd);
605 mutex_exit(&vd->vdev_rebuild_lock);
606 dmu_tx_commit(tx);
607 return (SET_ERROR(EINTR));
608 }
609 mutex_exit(&vd->vdev_rebuild_lock);
610
611 vr->vr_scan_offset[txg & TXG_MASK] = start + size;
612 vr->vr_pass_bytes_issued += size;
613 vr->vr_rebuild_phys.vrp_bytes_issued += size;
614
615 zio_nowait(zio_read(spa->spa_txg_zio[txg & TXG_MASK], spa, &blk,
616 abd_alloc(psize, B_FALSE), psize, vdev_rebuild_cb, vr,
617 ZIO_PRIORITY_REBUILD, ZIO_FLAG_RAW | ZIO_FLAG_CANFAIL |
618 ZIO_FLAG_RESILVER, NULL));
619 /* vdev_rebuild_cb releases SCL_STATE_ALL */
620
621 dmu_tx_commit(tx);
622
623 return (0);
624 }
625
626 /*
627 * Issues rebuild I/Os for all ranges in the provided vr->vr_tree range tree.
628 */
629 static int
vdev_rebuild_ranges(vdev_rebuild_t * vr)630 vdev_rebuild_ranges(vdev_rebuild_t *vr)
631 {
632 vdev_t *vd = vr->vr_top_vdev;
633 zfs_btree_t *t = &vr->vr_scan_tree->rt_root;
634 zfs_btree_index_t idx;
635 int error;
636
637 for (zfs_range_seg_t *rs = zfs_btree_first(t, &idx); rs != NULL;
638 rs = zfs_btree_next(t, &idx, &idx)) {
639 uint64_t start = zfs_rs_get_start(rs, vr->vr_scan_tree);
640 uint64_t size = zfs_rs_get_end(rs, vr->vr_scan_tree) - start;
641
642 /*
643 * zfs_scan_suspend_progress can be set to disable rebuild
644 * progress for testing. See comment in dsl_scan_sync().
645 */
646 while (zfs_scan_suspend_progress &&
647 !vdev_rebuild_should_stop(vd)) {
648 delay(hz);
649 }
650
651 while (size > 0) {
652 uint64_t chunk_size;
653
654 /*
655 * Split range into legally-sized logical chunks
656 * given the constraints of the top-level vdev
657 * being rebuilt (dRAID or mirror).
658 */
659 ASSERT3P(vd->vdev_ops, !=, NULL);
660 chunk_size = vd->vdev_ops->vdev_op_rebuild_asize(vd,
661 start, size, zfs_rebuild_max_segment);
662
663 error = vdev_rebuild_range(vr, start, chunk_size);
664 if (error != 0)
665 return (error);
666
667 size -= chunk_size;
668 start += chunk_size;
669 }
670 }
671
672 return (0);
673 }
674
675 /*
676 * Calculates the estimated capacity which remains to be scanned. Since
677 * we traverse the pool in metaslab order only allocated capacity beyond
678 * the vrp_last_offset need be considered. All lower offsets must have
679 * already been rebuilt and are thus already included in vrp_bytes_scanned.
680 */
681 static void
vdev_rebuild_update_bytes_est(vdev_t * vd,uint64_t ms_id)682 vdev_rebuild_update_bytes_est(vdev_t *vd, uint64_t ms_id)
683 {
684 vdev_rebuild_t *vr = &vd->vdev_rebuild_config;
685 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
686 uint64_t bytes_est = vrp->vrp_bytes_scanned;
687
688 if (vrp->vrp_last_offset < vd->vdev_ms[ms_id]->ms_start)
689 return;
690
691 for (uint64_t i = ms_id; i < vd->vdev_ms_count; i++) {
692 metaslab_t *msp = vd->vdev_ms[i];
693
694 mutex_enter(&msp->ms_lock);
695 bytes_est += metaslab_allocated_space(msp);
696 mutex_exit(&msp->ms_lock);
697 }
698
699 vrp->vrp_bytes_est = bytes_est;
700 }
701
702 /*
703 * Load from disk the top-level vdev's rebuild information.
704 */
705 int
vdev_rebuild_load(vdev_t * vd)706 vdev_rebuild_load(vdev_t *vd)
707 {
708 vdev_rebuild_t *vr = &vd->vdev_rebuild_config;
709 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
710 spa_t *spa = vd->vdev_spa;
711 int err = 0;
712
713 mutex_enter(&vd->vdev_rebuild_lock);
714 vd->vdev_rebuilding = B_FALSE;
715
716 if (!spa_feature_is_enabled(spa, SPA_FEATURE_DEVICE_REBUILD)) {
717 memset(vrp, 0, sizeof (uint64_t) * REBUILD_PHYS_ENTRIES);
718 mutex_exit(&vd->vdev_rebuild_lock);
719 return (SET_ERROR(ENOTSUP));
720 }
721
722 ASSERT(vd->vdev_top == vd);
723
724 err = zap_lookup(spa->spa_meta_objset, vd->vdev_top_zap,
725 VDEV_TOP_ZAP_VDEV_REBUILD_PHYS, sizeof (uint64_t),
726 REBUILD_PHYS_ENTRIES, vrp);
727
728 /*
729 * A missing or damaged VDEV_TOP_ZAP_VDEV_REBUILD_PHYS should
730 * not prevent a pool from being imported. Clear the rebuild
731 * status allowing a new resilver/rebuild to be started.
732 */
733 if (err == ENOENT || err == EOVERFLOW || err == ECKSUM) {
734 memset(vrp, 0, sizeof (uint64_t) * REBUILD_PHYS_ENTRIES);
735 } else if (err) {
736 mutex_exit(&vd->vdev_rebuild_lock);
737 return (err);
738 }
739
740 vr->vr_prev_scan_time_ms = vrp->vrp_scan_time_ms;
741 vr->vr_top_vdev = vd;
742
743 mutex_exit(&vd->vdev_rebuild_lock);
744
745 return (0);
746 }
747
748 /*
749 * Each scan thread is responsible for rebuilding a top-level vdev. The
750 * rebuild progress in tracked on-disk in VDEV_TOP_ZAP_VDEV_REBUILD_PHYS.
751 */
752 static __attribute__((noreturn)) void
vdev_rebuild_thread(void * arg)753 vdev_rebuild_thread(void *arg)
754 {
755 vdev_t *vd = arg;
756 spa_t *spa = vd->vdev_spa;
757 vdev_t *rvd = spa->spa_root_vdev;
758 dsl_pool_t *dp = spa_get_dsl(spa);
759 int error = 0;
760
761 /*
762 * If there's a scrub in process request that it be stopped. This
763 * is not required for a correct rebuild, but we do want rebuilds to
764 * emulate the resilver behavior as much as possible.
765 */
766 if (dsl_scan_scrubbing(dp))
767 dsl_scan_cancel(dp);
768
769 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
770 mutex_enter(&vd->vdev_rebuild_lock);
771
772 ASSERT3P(vd->vdev_top, ==, vd);
773 ASSERT3P(vd->vdev_rebuild_thread, !=, NULL);
774 ASSERT(vd->vdev_rebuilding);
775 ASSERT(spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REBUILD));
776 ASSERT3B(vd->vdev_rebuild_cancel_wanted, ==, B_FALSE);
777
778 vdev_rebuild_t *vr = &vd->vdev_rebuild_config;
779 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
780 vr->vr_top_vdev = vd;
781 vr->vr_scan_msp = NULL;
782 vr->vr_scan_tree = zfs_range_tree_create_flags(
783 NULL, ZFS_RANGE_SEG64, NULL, 0, 0,
784 ZFS_RT_F_DYN_NAME, vdev_rt_name(vd, "vr_scan_tree"));
785 mutex_init(&vr->vr_io_lock, NULL, MUTEX_DEFAULT, NULL);
786 cv_init(&vr->vr_io_cv, NULL, CV_DEFAULT, NULL);
787
788 vr->vr_pass_start_time = gethrtime();
789 vr->vr_pass_bytes_scanned = 0;
790 vr->vr_pass_bytes_issued = 0;
791 vr->vr_pass_bytes_skipped = 0;
792
793 uint64_t update_est_time = gethrtime();
794 vdev_rebuild_update_bytes_est(vd, 0);
795
796 clear_rebuild_bytes(vr->vr_top_vdev);
797
798 mutex_exit(&vd->vdev_rebuild_lock);
799
800 /*
801 * Systematically walk the metaslabs and issue rebuild I/Os for
802 * all ranges in the allocated space map.
803 */
804 for (uint64_t i = 0; i < vd->vdev_ms_count; i++) {
805 metaslab_t *msp = vd->vdev_ms[i];
806 vr->vr_scan_msp = msp;
807
808 /*
809 * Calculate the max number of in-flight bytes for top-level
810 * vdev scanning operations (minimum 1MB, maximum 1/2 of
811 * arc_c_max shared by all top-level vdevs). Limits for the
812 * issuing phase are done per top-level vdev and are handled
813 * separately.
814 */
815 uint64_t limit = (arc_c_max / 2) / MAX(rvd->vdev_children, 1);
816 vr->vr_bytes_inflight_max = MIN(limit, MAX(1ULL << 20,
817 zfs_rebuild_vdev_limit * vd->vdev_children));
818 vr->vr_last_txg = 0;
819
820 /*
821 * Removal of vdevs from the vdev tree may eliminate the need
822 * for the rebuild, in which case it should be canceled. The
823 * vdev_rebuild_cancel_wanted flag is set until the sync task
824 * completes. This may be after the rebuild thread exits.
825 */
826 if (vdev_rebuild_should_cancel(vd)) {
827 vd->vdev_rebuild_cancel_wanted = B_TRUE;
828 error = EINTR;
829 break;
830 }
831
832 ASSERT0(zfs_range_tree_space(vr->vr_scan_tree));
833
834 /* Disable any new allocations to this metaslab */
835 spa_config_exit(spa, SCL_CONFIG, FTAG);
836 metaslab_disable(msp);
837
838 mutex_enter(&msp->ms_sync_lock);
839 mutex_enter(&msp->ms_lock);
840
841 /*
842 * If there are outstanding allocations wait for them to be
843 * synced. This is needed to ensure all allocated ranges are
844 * on disk and therefore will be rebuilt.
845 */
846 for (int j = 0; j < TXG_SIZE; j++) {
847 if (zfs_range_tree_space(msp->ms_allocating[j])) {
848 mutex_exit(&msp->ms_lock);
849 mutex_exit(&msp->ms_sync_lock);
850 txg_wait_synced(dp, 0);
851 mutex_enter(&msp->ms_sync_lock);
852 mutex_enter(&msp->ms_lock);
853 break;
854 }
855 }
856
857 /*
858 * When a metaslab has been allocated from read its allocated
859 * ranges from the space map object into the vr_scan_tree.
860 * Then add inflight / unflushed ranges and remove inflight /
861 * unflushed frees. This is the minimum range to be rebuilt.
862 */
863 if (msp->ms_sm != NULL) {
864 VERIFY0(space_map_load(msp->ms_sm,
865 vr->vr_scan_tree, SM_ALLOC));
866
867 for (int i = 0; i < TXG_SIZE; i++) {
868 ASSERT0(zfs_range_tree_space(
869 msp->ms_allocating[i]));
870 }
871
872 zfs_range_tree_walk(msp->ms_unflushed_allocs,
873 zfs_range_tree_add, vr->vr_scan_tree);
874 zfs_range_tree_walk(msp->ms_unflushed_frees,
875 zfs_range_tree_remove, vr->vr_scan_tree);
876
877 /*
878 * Remove ranges which have already been rebuilt based
879 * on the last offset. This can happen when restarting
880 * a scan after exporting and re-importing the pool.
881 */
882 zfs_range_tree_clear(vr->vr_scan_tree, 0,
883 vrp->vrp_last_offset);
884 }
885
886 mutex_exit(&msp->ms_lock);
887 mutex_exit(&msp->ms_sync_lock);
888
889 /*
890 * To provide an accurate estimate re-calculate the estimated
891 * size every 5 minutes to account for recent allocations and
892 * frees made to space maps which have not yet been rebuilt.
893 */
894 if (gethrtime() > update_est_time + SEC2NSEC(300)) {
895 update_est_time = gethrtime();
896 vdev_rebuild_update_bytes_est(vd, i);
897 }
898
899 /*
900 * Walk the allocated space map and issue the rebuild I/O.
901 */
902 error = vdev_rebuild_ranges(vr);
903 zfs_range_tree_vacate(vr->vr_scan_tree, NULL, NULL);
904
905 /*
906 * Allow rebuilt ranges to be sync-ed before enabling metaslab
907 * to avoid any interfering allocations. Otherwise, we might
908 * see checksum errors after scrub.
909 */
910 if (vr->vr_last_txg != 0)
911 txg_wait_synced(dp, vr->vr_last_txg);
912
913 metaslab_enable(msp, B_FALSE, B_FALSE);
914 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
915
916 if (error != 0)
917 break;
918 }
919
920 zfs_range_tree_destroy(vr->vr_scan_tree);
921 spa_config_exit(spa, SCL_CONFIG, FTAG);
922
923 /* Wait for any remaining rebuild I/O to complete */
924 mutex_enter(&vr->vr_io_lock);
925 while (vr->vr_bytes_inflight > 0)
926 cv_wait(&vr->vr_io_cv, &vr->vr_io_lock);
927
928 mutex_exit(&vr->vr_io_lock);
929
930 mutex_destroy(&vr->vr_io_lock);
931 cv_destroy(&vr->vr_io_cv);
932
933 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
934
935 dmu_tx_t *tx = dmu_tx_create_dd(dp->dp_mos_dir);
936 VERIFY0(dmu_tx_assign(tx, DMU_TX_WAIT | DMU_TX_SUSPEND));
937
938 mutex_enter(&vd->vdev_rebuild_lock);
939 if (error == 0) {
940 /*
941 * After a successful rebuild clear the DTLs of all ranges
942 * which were missing when the rebuild was started. These
943 * ranges must have been rebuilt as a consequence of rebuilding
944 * all allocated space. Note that unlike a scrub or resilver
945 * the rebuild operation will reconstruct data only referenced
946 * by a pool checkpoint. See the dsl_scan_done() comments.
947 */
948 dsl_sync_task_nowait(dp, vdev_rebuild_complete_sync,
949 (void *)(uintptr_t)vd->vdev_id, tx);
950 } else if (vd->vdev_rebuild_cancel_wanted) {
951 /*
952 * The rebuild operation was canceled. This will occur when
953 * a device participating in the rebuild is detached.
954 */
955 dsl_sync_task_nowait(dp, vdev_rebuild_cancel_sync,
956 (void *)(uintptr_t)vd->vdev_id, tx);
957 } else if (vd->vdev_rebuild_reset_wanted) {
958 /*
959 * Reset the running rebuild without canceling and restarting
960 * it. This will occur when a new device is attached and must
961 * participate in the rebuild.
962 */
963 dsl_sync_task_nowait(dp, vdev_rebuild_reset_sync,
964 (void *)(uintptr_t)vd->vdev_id, tx);
965 } else {
966 /*
967 * The rebuild operation should be suspended. This may occur
968 * when detaching a child vdev or when exporting the pool. The
969 * rebuild is left in the active state so it will be resumed.
970 */
971 ASSERT(vrp->vrp_rebuild_state == VDEV_REBUILD_ACTIVE);
972 vd->vdev_rebuilding = B_FALSE;
973 }
974
975 dmu_tx_commit(tx);
976
977 vd->vdev_rebuild_thread = NULL;
978 mutex_exit(&vd->vdev_rebuild_lock);
979 spa_config_exit(spa, SCL_CONFIG, FTAG);
980
981 cv_broadcast(&vd->vdev_rebuild_cv);
982
983 thread_exit();
984 }
985
986 /*
987 * Returns B_TRUE if any top-level vdev are rebuilding.
988 */
989 boolean_t
vdev_rebuild_active(vdev_t * vd)990 vdev_rebuild_active(vdev_t *vd)
991 {
992 spa_t *spa = vd->vdev_spa;
993 boolean_t ret = B_FALSE;
994
995 if (vd == spa->spa_root_vdev) {
996 for (uint64_t i = 0; i < vd->vdev_children; i++) {
997 ret = vdev_rebuild_active(vd->vdev_child[i]);
998 if (ret)
999 return (ret);
1000 }
1001 } else if (vd->vdev_top_zap != 0) {
1002 vdev_rebuild_t *vr = &vd->vdev_rebuild_config;
1003 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
1004
1005 mutex_enter(&vd->vdev_rebuild_lock);
1006 ret = (vrp->vrp_rebuild_state == VDEV_REBUILD_ACTIVE);
1007 mutex_exit(&vd->vdev_rebuild_lock);
1008 }
1009
1010 return (ret);
1011 }
1012
1013 /*
1014 * Start a rebuild operation. The rebuild may be restarted when the
1015 * top-level vdev is currently actively rebuilding.
1016 */
1017 void
vdev_rebuild(vdev_t * vd,uint64_t txg)1018 vdev_rebuild(vdev_t *vd, uint64_t txg)
1019 {
1020 vdev_rebuild_t *vr = &vd->vdev_rebuild_config;
1021 vdev_rebuild_phys_t *vrp __maybe_unused = &vr->vr_rebuild_phys;
1022
1023 ASSERT(vd->vdev_top == vd);
1024 ASSERT(vdev_is_concrete(vd));
1025 ASSERT(!vd->vdev_removing);
1026 ASSERT(spa_feature_is_enabled(vd->vdev_spa,
1027 SPA_FEATURE_DEVICE_REBUILD));
1028
1029 mutex_enter(&vd->vdev_rebuild_lock);
1030 if (vd->vdev_rebuilding) {
1031 ASSERT3U(vrp->vrp_rebuild_state, ==, VDEV_REBUILD_ACTIVE);
1032
1033 /*
1034 * Signal a running rebuild operation that it should restart
1035 * from the beginning because a new device was attached. The
1036 * vdev_rebuild_reset_wanted flag is set until the sync task
1037 * completes. This may be after the rebuild thread exits.
1038 */
1039 if (!vd->vdev_rebuild_reset_wanted)
1040 vd->vdev_rebuild_reset_wanted = B_TRUE;
1041 } else {
1042 vdev_rebuild_initiate(vd, txg);
1043 }
1044 mutex_exit(&vd->vdev_rebuild_lock);
1045 }
1046
1047 static void
vdev_rebuild_restart_impl(vdev_t * vd)1048 vdev_rebuild_restart_impl(vdev_t *vd)
1049 {
1050 spa_t *spa = vd->vdev_spa;
1051
1052 if (vd == spa->spa_root_vdev) {
1053 for (uint64_t i = 0; i < vd->vdev_children; i++)
1054 vdev_rebuild_restart_impl(vd->vdev_child[i]);
1055
1056 } else if (vd->vdev_top_zap != 0) {
1057 vdev_rebuild_t *vr = &vd->vdev_rebuild_config;
1058 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
1059
1060 mutex_enter(&vd->vdev_rebuild_lock);
1061 if (vrp->vrp_rebuild_state == VDEV_REBUILD_ACTIVE &&
1062 vdev_writeable(vd) && !vd->vdev_rebuilding) {
1063 ASSERT(spa_feature_is_active(spa,
1064 SPA_FEATURE_DEVICE_REBUILD));
1065 vd->vdev_rebuilding = B_TRUE;
1066 vd->vdev_rebuild_thread = thread_create(NULL, 0,
1067 vdev_rebuild_thread, vd, 0, &p0, TS_RUN,
1068 maxclsyspri);
1069 }
1070 mutex_exit(&vd->vdev_rebuild_lock);
1071 }
1072 }
1073
1074 /*
1075 * Conditionally restart all of the vdev_rebuild_thread's for a pool. The
1076 * feature flag must be active and the rebuild in the active state. This
1077 * cannot be used to start a new rebuild.
1078 */
1079 void
vdev_rebuild_restart(spa_t * spa)1080 vdev_rebuild_restart(spa_t *spa)
1081 {
1082 ASSERT(spa_namespace_held() ||
1083 spa->spa_load_thread == curthread);
1084
1085 vdev_rebuild_restart_impl(spa->spa_root_vdev);
1086 }
1087
1088 /*
1089 * Stop and wait for all of the vdev_rebuild_thread's associated with the
1090 * vdev tree provide to be terminated (canceled or stopped).
1091 */
1092 void
vdev_rebuild_stop_wait(vdev_t * vd)1093 vdev_rebuild_stop_wait(vdev_t *vd)
1094 {
1095 spa_t *spa = vd->vdev_spa;
1096
1097 ASSERT(spa_namespace_held() ||
1098 spa->spa_export_thread == curthread);
1099
1100 if (vd == spa->spa_root_vdev) {
1101 for (uint64_t i = 0; i < vd->vdev_children; i++)
1102 vdev_rebuild_stop_wait(vd->vdev_child[i]);
1103
1104 } else if (vd->vdev_top_zap != 0) {
1105 ASSERT(vd == vd->vdev_top);
1106
1107 mutex_enter(&vd->vdev_rebuild_lock);
1108 if (vd->vdev_rebuild_thread != NULL) {
1109 vd->vdev_rebuild_exit_wanted = B_TRUE;
1110 while (vd->vdev_rebuilding) {
1111 cv_wait(&vd->vdev_rebuild_cv,
1112 &vd->vdev_rebuild_lock);
1113 }
1114 vd->vdev_rebuild_exit_wanted = B_FALSE;
1115 }
1116 mutex_exit(&vd->vdev_rebuild_lock);
1117 }
1118 }
1119
1120 /*
1121 * Stop all rebuild operations but leave them in the active state so they
1122 * will be resumed when importing the pool.
1123 */
1124 void
vdev_rebuild_stop_all(spa_t * spa)1125 vdev_rebuild_stop_all(spa_t *spa)
1126 {
1127 vdev_rebuild_stop_wait(spa->spa_root_vdev);
1128 }
1129
1130 /*
1131 * Return rebuild transaction groups range. It's used to populate DTLs
1132 * of the non-writable devices during the rebuild so that they could be
1133 * healed correctly, in case they are cleared, and not miss the data
1134 * that was written to their spares during the rebuild.
1135 */
1136 void
vdev_rebuild_txgs(vdev_t * vd,uint64_t * min_txg,uint64_t * size)1137 vdev_rebuild_txgs(vdev_t *vd, uint64_t *min_txg, uint64_t *size)
1138 {
1139 vdev_rebuild_t *vr = &vd->vdev_rebuild_config;
1140 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
1141
1142 *min_txg = vrp->vrp_min_txg;
1143 *size = vrp->vrp_max_txg - vrp->vrp_min_txg;
1144 }
1145
1146 /*
1147 * Rebuild statistics reported per top-level vdev.
1148 */
1149 int
vdev_rebuild_get_stats(vdev_t * tvd,vdev_rebuild_stat_t * vrs)1150 vdev_rebuild_get_stats(vdev_t *tvd, vdev_rebuild_stat_t *vrs)
1151 {
1152 spa_t *spa = tvd->vdev_spa;
1153
1154 if (!spa_feature_is_enabled(spa, SPA_FEATURE_DEVICE_REBUILD))
1155 return (SET_ERROR(ENOTSUP));
1156
1157 if (tvd != tvd->vdev_top || tvd->vdev_top_zap == 0)
1158 return (SET_ERROR(EINVAL));
1159
1160 int error = zap_contains(spa_meta_objset(spa),
1161 tvd->vdev_top_zap, VDEV_TOP_ZAP_VDEV_REBUILD_PHYS);
1162
1163 if (error == ENOENT) {
1164 memset(vrs, 0, sizeof (vdev_rebuild_stat_t));
1165 vrs->vrs_state = VDEV_REBUILD_NONE;
1166 error = 0;
1167 } else if (error == 0) {
1168 vdev_rebuild_t *vr = &tvd->vdev_rebuild_config;
1169 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
1170
1171 mutex_enter(&tvd->vdev_rebuild_lock);
1172 vrs->vrs_state = vrp->vrp_rebuild_state;
1173 vrs->vrs_start_time = vrp->vrp_start_time;
1174 vrs->vrs_end_time = vrp->vrp_end_time;
1175 vrs->vrs_scan_time_ms = vrp->vrp_scan_time_ms;
1176 vrs->vrs_bytes_scanned = vrp->vrp_bytes_scanned;
1177 vrs->vrs_bytes_issued = vrp->vrp_bytes_issued;
1178 vrs->vrs_bytes_rebuilt = vrp->vrp_bytes_rebuilt;
1179 vrs->vrs_bytes_est = vrp->vrp_bytes_est;
1180 vrs->vrs_errors = vrp->vrp_errors;
1181 vrs->vrs_pass_time_ms = NSEC2MSEC(gethrtime() -
1182 vr->vr_pass_start_time);
1183 vrs->vrs_pass_bytes_scanned = vr->vr_pass_bytes_scanned;
1184 vrs->vrs_pass_bytes_issued = vr->vr_pass_bytes_issued;
1185 vrs->vrs_pass_bytes_skipped = vr->vr_pass_bytes_skipped;
1186 mutex_exit(&tvd->vdev_rebuild_lock);
1187 }
1188
1189 return (error);
1190 }
1191
1192 ZFS_MODULE_PARAM(zfs, zfs_, rebuild_max_segment, U64, ZMOD_RW,
1193 "Max segment size in bytes of rebuild reads");
1194
1195 ZFS_MODULE_PARAM(zfs, zfs_, rebuild_vdev_limit, U64, ZMOD_RW,
1196 "Max bytes in flight per leaf vdev for sequential resilvers");
1197
1198 ZFS_MODULE_PARAM(zfs, zfs_, rebuild_scrub_enabled, INT, ZMOD_RW,
1199 "Automatically scrub after sequential resilver completes");
1200