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]
22 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
24 * Copyright 2016 Gary Mills
27 #include <sys/dsl_scan.h>
28 #include <sys/dsl_pool.h>
29 #include <sys/dsl_dataset.h>
30 #include <sys/dsl_prop.h>
31 #include <sys/dsl_dir.h>
32 #include <sys/dsl_synctask.h>
33 #include <sys/dnode.h>
34 #include <sys/dmu_tx.h>
35 #include <sys/dmu_objset.h>
39 #include <sys/zfs_context.h>
40 #include <sys/fs/zfs.h>
41 #include <sys/zfs_znode.h>
42 #include <sys/spa_impl.h>
43 #include <sys/vdev_impl.h>
44 #include <sys/zil_impl.h>
45 #include <sys/zio_checksum.h>
48 #include <sys/sa_impl.h>
49 #include <sys/zfeature.h>
51 #include <sys/zfs_vfsops.h>
54 typedef int (scan_cb_t)(dsl_pool_t *, const blkptr_t *,
55 const zbookmark_phys_t *);
57 static scan_cb_t dsl_scan_scrub_cb;
58 static void dsl_scan_cancel_sync(void *, dmu_tx_t *);
59 static void dsl_scan_sync_state(dsl_scan_t *, dmu_tx_t *);
60 static boolean_t dsl_scan_restarting(dsl_scan_t *, dmu_tx_t *);
62 unsigned int zfs_top_maxinflight = 32; /* maximum I/Os per top-level */
63 unsigned int zfs_resilver_delay = 2; /* number of ticks to delay resilver */
64 unsigned int zfs_scrub_delay = 4; /* number of ticks to delay scrub */
65 unsigned int zfs_scan_idle = 50; /* idle window in clock ticks */
67 unsigned int zfs_scan_min_time_ms = 1000; /* min millisecs to scrub per txg */
68 unsigned int zfs_free_min_time_ms = 1000; /* min millisecs to free per txg */
69 unsigned int zfs_resilver_min_time_ms = 3000; /* min millisecs to resilver
71 boolean_t zfs_no_scrub_io = B_FALSE; /* set to disable scrub i/o */
72 boolean_t zfs_no_scrub_prefetch = B_FALSE; /* set to disable scrub prefetch */
74 SYSCTL_DECL(_vfs_zfs);
75 SYSCTL_UINT(_vfs_zfs, OID_AUTO, top_maxinflight, CTLFLAG_RWTUN,
76 &zfs_top_maxinflight, 0, "Maximum I/Os per top-level vdev");
77 SYSCTL_UINT(_vfs_zfs, OID_AUTO, resilver_delay, CTLFLAG_RWTUN,
78 &zfs_resilver_delay, 0, "Number of ticks to delay resilver");
79 SYSCTL_UINT(_vfs_zfs, OID_AUTO, scrub_delay, CTLFLAG_RWTUN,
80 &zfs_scrub_delay, 0, "Number of ticks to delay scrub");
81 SYSCTL_UINT(_vfs_zfs, OID_AUTO, scan_idle, CTLFLAG_RWTUN,
82 &zfs_scan_idle, 0, "Idle scan window in clock ticks");
83 SYSCTL_UINT(_vfs_zfs, OID_AUTO, scan_min_time_ms, CTLFLAG_RWTUN,
84 &zfs_scan_min_time_ms, 0, "Min millisecs to scrub per txg");
85 SYSCTL_UINT(_vfs_zfs, OID_AUTO, free_min_time_ms, CTLFLAG_RWTUN,
86 &zfs_free_min_time_ms, 0, "Min millisecs to free per txg");
87 SYSCTL_UINT(_vfs_zfs, OID_AUTO, resilver_min_time_ms, CTLFLAG_RWTUN,
88 &zfs_resilver_min_time_ms, 0, "Min millisecs to resilver per txg");
89 SYSCTL_INT(_vfs_zfs, OID_AUTO, no_scrub_io, CTLFLAG_RWTUN,
90 &zfs_no_scrub_io, 0, "Disable scrub I/O");
91 SYSCTL_INT(_vfs_zfs, OID_AUTO, no_scrub_prefetch, CTLFLAG_RWTUN,
92 &zfs_no_scrub_prefetch, 0, "Disable scrub prefetching");
94 enum ddt_class zfs_scrub_ddt_class_max = DDT_CLASS_DUPLICATE;
95 /* max number of blocks to free in a single TXG */
96 uint64_t zfs_free_max_blocks = UINT64_MAX;
97 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, free_max_blocks, CTLFLAG_RWTUN,
98 &zfs_free_max_blocks, 0, "Maximum number of blocks to free in one TXG");
101 #define DSL_SCAN_IS_SCRUB_RESILVER(scn) \
102 ((scn)->scn_phys.scn_func == POOL_SCAN_SCRUB || \
103 (scn)->scn_phys.scn_func == POOL_SCAN_RESILVER)
105 extern int zfs_txg_timeout;
108 * Enable/disable the processing of the free_bpobj object.
110 boolean_t zfs_free_bpobj_enabled = B_TRUE;
112 SYSCTL_INT(_vfs_zfs, OID_AUTO, free_bpobj_enabled, CTLFLAG_RWTUN,
113 &zfs_free_bpobj_enabled, 0, "Enable free_bpobj processing");
115 /* the order has to match pool_scan_type */
116 static scan_cb_t *scan_funcs[POOL_SCAN_FUNCS] = {
118 dsl_scan_scrub_cb, /* POOL_SCAN_SCRUB */
119 dsl_scan_scrub_cb, /* POOL_SCAN_RESILVER */
123 dsl_scan_init(dsl_pool_t *dp, uint64_t txg)
127 spa_t *spa = dp->dp_spa;
130 scn = dp->dp_scan = kmem_zalloc(sizeof (dsl_scan_t), KM_SLEEP);
134 * It's possible that we're resuming a scan after a reboot so
135 * make sure that the scan_async_destroying flag is initialized
138 ASSERT(!scn->scn_async_destroying);
139 scn->scn_async_destroying = spa_feature_is_active(dp->dp_spa,
140 SPA_FEATURE_ASYNC_DESTROY);
142 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
143 "scrub_func", sizeof (uint64_t), 1, &f);
146 * There was an old-style scrub in progress. Restart a
147 * new-style scrub from the beginning.
149 scn->scn_restart_txg = txg;
150 zfs_dbgmsg("old-style scrub was in progress; "
151 "restarting new-style scrub in txg %llu",
152 scn->scn_restart_txg);
155 * Load the queue obj from the old location so that it
156 * can be freed by dsl_scan_done().
158 (void) zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
159 "scrub_queue", sizeof (uint64_t), 1,
160 &scn->scn_phys.scn_queue_obj);
162 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
163 DMU_POOL_SCAN, sizeof (uint64_t), SCAN_PHYS_NUMINTS,
170 if (scn->scn_phys.scn_state == DSS_SCANNING &&
171 spa_prev_software_version(dp->dp_spa) < SPA_VERSION_SCAN) {
173 * A new-type scrub was in progress on an old
174 * pool, and the pool was accessed by old
175 * software. Restart from the beginning, since
176 * the old software may have changed the pool in
179 scn->scn_restart_txg = txg;
180 zfs_dbgmsg("new-style scrub was modified "
181 "by old software; restarting in txg %llu",
182 scn->scn_restart_txg);
186 spa_scan_stat_init(spa);
191 dsl_scan_fini(dsl_pool_t *dp)
194 kmem_free(dp->dp_scan, sizeof (dsl_scan_t));
201 dsl_scan_setup_check(void *arg, dmu_tx_t *tx)
203 dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
205 if (scn->scn_phys.scn_state == DSS_SCANNING)
206 return (SET_ERROR(EBUSY));
212 dsl_scan_setup_sync(void *arg, dmu_tx_t *tx)
214 dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
215 pool_scan_func_t *funcp = arg;
216 dmu_object_type_t ot = 0;
217 dsl_pool_t *dp = scn->scn_dp;
218 spa_t *spa = dp->dp_spa;
220 ASSERT(scn->scn_phys.scn_state != DSS_SCANNING);
221 ASSERT(*funcp > POOL_SCAN_NONE && *funcp < POOL_SCAN_FUNCS);
222 bzero(&scn->scn_phys, sizeof (scn->scn_phys));
223 scn->scn_phys.scn_func = *funcp;
224 scn->scn_phys.scn_state = DSS_SCANNING;
225 scn->scn_phys.scn_min_txg = 0;
226 scn->scn_phys.scn_max_txg = tx->tx_txg;
227 scn->scn_phys.scn_ddt_class_max = DDT_CLASSES - 1; /* the entire DDT */
228 scn->scn_phys.scn_start_time = gethrestime_sec();
229 scn->scn_phys.scn_errors = 0;
230 scn->scn_phys.scn_to_examine = spa->spa_root_vdev->vdev_stat.vs_alloc;
231 scn->scn_restart_txg = 0;
232 scn->scn_done_txg = 0;
233 spa_scan_stat_init(spa);
235 if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
236 scn->scn_phys.scn_ddt_class_max = zfs_scrub_ddt_class_max;
238 /* rewrite all disk labels */
239 vdev_config_dirty(spa->spa_root_vdev);
241 if (vdev_resilver_needed(spa->spa_root_vdev,
242 &scn->scn_phys.scn_min_txg, &scn->scn_phys.scn_max_txg)) {
243 spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_START);
245 spa_event_notify(spa, NULL, ESC_ZFS_SCRUB_START);
248 spa->spa_scrub_started = B_TRUE;
250 * If this is an incremental scrub, limit the DDT scrub phase
251 * to just the auto-ditto class (for correctness); the rest
252 * of the scrub should go faster using top-down pruning.
254 if (scn->scn_phys.scn_min_txg > TXG_INITIAL)
255 scn->scn_phys.scn_ddt_class_max = DDT_CLASS_DITTO;
259 /* back to the generic stuff */
261 if (dp->dp_blkstats == NULL) {
263 kmem_alloc(sizeof (zfs_all_blkstats_t), KM_SLEEP);
265 bzero(dp->dp_blkstats, sizeof (zfs_all_blkstats_t));
267 if (spa_version(spa) < SPA_VERSION_DSL_SCRUB)
268 ot = DMU_OT_ZAP_OTHER;
270 scn->scn_phys.scn_queue_obj = zap_create(dp->dp_meta_objset,
271 ot ? ot : DMU_OT_SCAN_QUEUE, DMU_OT_NONE, 0, tx);
273 dsl_scan_sync_state(scn, tx);
275 spa_history_log_internal(spa, "scan setup", tx,
276 "func=%u mintxg=%llu maxtxg=%llu",
277 *funcp, scn->scn_phys.scn_min_txg, scn->scn_phys.scn_max_txg);
282 dsl_scan_done(dsl_scan_t *scn, boolean_t complete, dmu_tx_t *tx)
284 static const char *old_names[] = {
286 "scrub_ddt_bookmark",
287 "scrub_ddt_class_max",
296 dsl_pool_t *dp = scn->scn_dp;
297 spa_t *spa = dp->dp_spa;
300 /* Remove any remnants of an old-style scrub. */
301 for (i = 0; old_names[i]; i++) {
302 (void) zap_remove(dp->dp_meta_objset,
303 DMU_POOL_DIRECTORY_OBJECT, old_names[i], tx);
306 if (scn->scn_phys.scn_queue_obj != 0) {
307 VERIFY(0 == dmu_object_free(dp->dp_meta_objset,
308 scn->scn_phys.scn_queue_obj, tx));
309 scn->scn_phys.scn_queue_obj = 0;
313 * If we were "restarted" from a stopped state, don't bother
314 * with anything else.
316 if (scn->scn_phys.scn_state != DSS_SCANNING)
320 scn->scn_phys.scn_state = DSS_FINISHED;
322 scn->scn_phys.scn_state = DSS_CANCELED;
324 if (dsl_scan_restarting(scn, tx))
325 spa_history_log_internal(spa, "scan aborted, restarting", tx,
326 "errors=%llu", spa_get_errlog_size(spa));
328 spa_history_log_internal(spa, "scan cancelled", tx,
329 "errors=%llu", spa_get_errlog_size(spa));
331 spa_history_log_internal(spa, "scan done", tx,
332 "errors=%llu", spa_get_errlog_size(spa));
334 if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
335 mutex_enter(&spa->spa_scrub_lock);
336 while (spa->spa_scrub_inflight > 0) {
337 cv_wait(&spa->spa_scrub_io_cv,
338 &spa->spa_scrub_lock);
340 mutex_exit(&spa->spa_scrub_lock);
341 spa->spa_scrub_started = B_FALSE;
342 spa->spa_scrub_active = B_FALSE;
345 * If the scrub/resilver completed, update all DTLs to
346 * reflect this. Whether it succeeded or not, vacate
347 * all temporary scrub DTLs.
349 vdev_dtl_reassess(spa->spa_root_vdev, tx->tx_txg,
350 complete ? scn->scn_phys.scn_max_txg : 0, B_TRUE);
352 spa_event_notify(spa, NULL, scn->scn_phys.scn_min_txg ?
353 ESC_ZFS_RESILVER_FINISH : ESC_ZFS_SCRUB_FINISH);
355 spa_errlog_rotate(spa);
358 * We may have finished replacing a device.
359 * Let the async thread assess this and handle the detach.
361 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
364 scn->scn_phys.scn_end_time = gethrestime_sec();
369 dsl_scan_cancel_check(void *arg, dmu_tx_t *tx)
371 dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
373 if (scn->scn_phys.scn_state != DSS_SCANNING)
374 return (SET_ERROR(ENOENT));
380 dsl_scan_cancel_sync(void *arg, dmu_tx_t *tx)
382 dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
384 dsl_scan_done(scn, B_FALSE, tx);
385 dsl_scan_sync_state(scn, tx);
389 dsl_scan_cancel(dsl_pool_t *dp)
391 return (dsl_sync_task(spa_name(dp->dp_spa), dsl_scan_cancel_check,
392 dsl_scan_cancel_sync, NULL, 3, ZFS_SPACE_CHECK_RESERVED));
395 static void dsl_scan_visitbp(blkptr_t *bp, const zbookmark_phys_t *zb,
396 dnode_phys_t *dnp, dsl_dataset_t *ds, dsl_scan_t *scn,
397 dmu_objset_type_t ostype, dmu_tx_t *tx);
398 static void dsl_scan_visitdnode(dsl_scan_t *, dsl_dataset_t *ds,
399 dmu_objset_type_t ostype,
400 dnode_phys_t *dnp, uint64_t object, dmu_tx_t *tx);
403 dsl_free(dsl_pool_t *dp, uint64_t txg, const blkptr_t *bp)
405 zio_free(dp->dp_spa, txg, bp);
409 dsl_free_sync(zio_t *pio, dsl_pool_t *dp, uint64_t txg, const blkptr_t *bpp)
411 ASSERT(dsl_pool_sync_context(dp));
412 zio_nowait(zio_free_sync(pio, dp->dp_spa, txg, bpp, BP_GET_PSIZE(bpp),
417 dsl_scan_ds_maxtxg(dsl_dataset_t *ds)
419 uint64_t smt = ds->ds_dir->dd_pool->dp_scan->scn_phys.scn_max_txg;
420 if (ds->ds_is_snapshot)
421 return (MIN(smt, dsl_dataset_phys(ds)->ds_creation_txg));
426 dsl_scan_sync_state(dsl_scan_t *scn, dmu_tx_t *tx)
428 VERIFY0(zap_update(scn->scn_dp->dp_meta_objset,
429 DMU_POOL_DIRECTORY_OBJECT,
430 DMU_POOL_SCAN, sizeof (uint64_t), SCAN_PHYS_NUMINTS,
431 &scn->scn_phys, tx));
434 extern int zfs_vdev_async_write_active_min_dirty_percent;
437 dsl_scan_check_pause(dsl_scan_t *scn, const zbookmark_phys_t *zb)
439 /* we never skip user/group accounting objects */
440 if (zb && (int64_t)zb->zb_object < 0)
443 if (scn->scn_pausing)
444 return (B_TRUE); /* we're already pausing */
446 if (!ZB_IS_ZERO(&scn->scn_phys.scn_bookmark))
447 return (B_FALSE); /* we're resuming */
449 /* We only know how to resume from level-0 blocks. */
450 if (zb && zb->zb_level != 0)
455 * - we have scanned for the maximum time: an entire txg
456 * timeout (default 5 sec)
458 * - we have scanned for at least the minimum time (default 1 sec
459 * for scrub, 3 sec for resilver), and either we have sufficient
460 * dirty data that we are starting to write more quickly
461 * (default 30%), or someone is explicitly waiting for this txg
464 * - the spa is shutting down because this pool is being exported
465 * or the machine is rebooting.
467 int mintime = (scn->scn_phys.scn_func == POOL_SCAN_RESILVER) ?
468 zfs_resilver_min_time_ms : zfs_scan_min_time_ms;
469 uint64_t elapsed_nanosecs = gethrtime() - scn->scn_sync_start_time;
470 int dirty_pct = scn->scn_dp->dp_dirty_total * 100 / zfs_dirty_data_max;
471 if (elapsed_nanosecs / NANOSEC >= zfs_txg_timeout ||
472 (NSEC2MSEC(elapsed_nanosecs) > mintime &&
473 (txg_sync_waiting(scn->scn_dp) ||
474 dirty_pct >= zfs_vdev_async_write_active_min_dirty_percent)) ||
475 spa_shutting_down(scn->scn_dp->dp_spa)) {
477 dprintf("pausing at bookmark %llx/%llx/%llx/%llx\n",
478 (longlong_t)zb->zb_objset,
479 (longlong_t)zb->zb_object,
480 (longlong_t)zb->zb_level,
481 (longlong_t)zb->zb_blkid);
482 scn->scn_phys.scn_bookmark = *zb;
484 dprintf("pausing at DDT bookmark %llx/%llx/%llx/%llx\n",
485 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_class,
486 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_type,
487 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_checksum,
488 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_cursor);
489 scn->scn_pausing = B_TRUE;
495 typedef struct zil_scan_arg {
497 zil_header_t *zsa_zh;
502 dsl_scan_zil_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
504 zil_scan_arg_t *zsa = arg;
505 dsl_pool_t *dp = zsa->zsa_dp;
506 dsl_scan_t *scn = dp->dp_scan;
507 zil_header_t *zh = zsa->zsa_zh;
510 if (BP_IS_HOLE(bp) || bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
514 * One block ("stubby") can be allocated a long time ago; we
515 * want to visit that one because it has been allocated
516 * (on-disk) even if it hasn't been claimed (even though for
517 * scrub there's nothing to do to it).
519 if (claim_txg == 0 && bp->blk_birth >= spa_first_txg(dp->dp_spa))
522 SET_BOOKMARK(&zb, zh->zh_log.blk_cksum.zc_word[ZIL_ZC_OBJSET],
523 ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, bp->blk_cksum.zc_word[ZIL_ZC_SEQ]);
525 VERIFY(0 == scan_funcs[scn->scn_phys.scn_func](dp, bp, &zb));
531 dsl_scan_zil_record(zilog_t *zilog, lr_t *lrc, void *arg, uint64_t claim_txg)
533 if (lrc->lrc_txtype == TX_WRITE) {
534 zil_scan_arg_t *zsa = arg;
535 dsl_pool_t *dp = zsa->zsa_dp;
536 dsl_scan_t *scn = dp->dp_scan;
537 zil_header_t *zh = zsa->zsa_zh;
538 lr_write_t *lr = (lr_write_t *)lrc;
539 blkptr_t *bp = &lr->lr_blkptr;
542 if (BP_IS_HOLE(bp) ||
543 bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
547 * birth can be < claim_txg if this record's txg is
548 * already txg sync'ed (but this log block contains
549 * other records that are not synced)
551 if (claim_txg == 0 || bp->blk_birth < claim_txg)
554 SET_BOOKMARK(&zb, zh->zh_log.blk_cksum.zc_word[ZIL_ZC_OBJSET],
555 lr->lr_foid, ZB_ZIL_LEVEL,
556 lr->lr_offset / BP_GET_LSIZE(bp));
558 VERIFY(0 == scan_funcs[scn->scn_phys.scn_func](dp, bp, &zb));
564 dsl_scan_zil(dsl_pool_t *dp, zil_header_t *zh)
566 uint64_t claim_txg = zh->zh_claim_txg;
567 zil_scan_arg_t zsa = { dp, zh };
571 * We only want to visit blocks that have been claimed but not yet
572 * replayed (or, in read-only mode, blocks that *would* be claimed).
574 if (claim_txg == 0 && spa_writeable(dp->dp_spa))
577 zilog = zil_alloc(dp->dp_meta_objset, zh);
579 (void) zil_parse(zilog, dsl_scan_zil_block, dsl_scan_zil_record, &zsa,
587 dsl_scan_prefetch(dsl_scan_t *scn, arc_buf_t *buf, blkptr_t *bp,
588 uint64_t objset, uint64_t object, uint64_t blkid)
590 zbookmark_phys_t czb;
591 arc_flags_t flags = ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH;
593 if (zfs_no_scrub_prefetch)
596 if (BP_IS_HOLE(bp) || bp->blk_birth <= scn->scn_phys.scn_min_txg ||
597 (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_DNODE))
600 SET_BOOKMARK(&czb, objset, object, BP_GET_LEVEL(bp), blkid);
602 (void) arc_read(scn->scn_zio_root, scn->scn_dp->dp_spa, bp,
603 NULL, NULL, ZIO_PRIORITY_ASYNC_READ,
604 ZIO_FLAG_CANFAIL | ZIO_FLAG_SCAN_THREAD, &flags, &czb);
608 dsl_scan_check_resume(dsl_scan_t *scn, const dnode_phys_t *dnp,
609 const zbookmark_phys_t *zb)
612 * We never skip over user/group accounting objects (obj<0)
614 if (!ZB_IS_ZERO(&scn->scn_phys.scn_bookmark) &&
615 (int64_t)zb->zb_object >= 0) {
617 * If we already visited this bp & everything below (in
618 * a prior txg sync), don't bother doing it again.
620 if (zbookmark_subtree_completed(dnp, zb,
621 &scn->scn_phys.scn_bookmark))
625 * If we found the block we're trying to resume from, or
626 * we went past it to a different object, zero it out to
627 * indicate that it's OK to start checking for pausing
630 if (bcmp(zb, &scn->scn_phys.scn_bookmark, sizeof (*zb)) == 0 ||
631 zb->zb_object > scn->scn_phys.scn_bookmark.zb_object) {
632 dprintf("resuming at %llx/%llx/%llx/%llx\n",
633 (longlong_t)zb->zb_objset,
634 (longlong_t)zb->zb_object,
635 (longlong_t)zb->zb_level,
636 (longlong_t)zb->zb_blkid);
637 bzero(&scn->scn_phys.scn_bookmark, sizeof (*zb));
644 * Return nonzero on i/o error.
645 * Return new buf to write out in *bufp.
648 dsl_scan_recurse(dsl_scan_t *scn, dsl_dataset_t *ds, dmu_objset_type_t ostype,
649 dnode_phys_t *dnp, const blkptr_t *bp,
650 const zbookmark_phys_t *zb, dmu_tx_t *tx)
652 dsl_pool_t *dp = scn->scn_dp;
653 int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCAN_THREAD;
656 if (BP_GET_LEVEL(bp) > 0) {
657 arc_flags_t flags = ARC_FLAG_WAIT;
660 int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
663 err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, &buf,
664 ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
666 scn->scn_phys.scn_errors++;
669 for (i = 0, cbp = buf->b_data; i < epb; i++, cbp++) {
670 dsl_scan_prefetch(scn, buf, cbp, zb->zb_objset,
671 zb->zb_object, zb->zb_blkid * epb + i);
673 for (i = 0, cbp = buf->b_data; i < epb; i++, cbp++) {
674 zbookmark_phys_t czb;
676 SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
678 zb->zb_blkid * epb + i);
679 dsl_scan_visitbp(cbp, &czb, dnp,
680 ds, scn, ostype, tx);
682 (void) arc_buf_remove_ref(buf, &buf);
683 } else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) {
684 arc_flags_t flags = ARC_FLAG_WAIT;
687 int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
690 err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, &buf,
691 ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
693 scn->scn_phys.scn_errors++;
696 for (i = 0, cdnp = buf->b_data; i < epb; i++, cdnp++) {
697 for (j = 0; j < cdnp->dn_nblkptr; j++) {
698 blkptr_t *cbp = &cdnp->dn_blkptr[j];
699 dsl_scan_prefetch(scn, buf, cbp,
700 zb->zb_objset, zb->zb_blkid * epb + i, j);
703 for (i = 0, cdnp = buf->b_data; i < epb; i++, cdnp++) {
704 dsl_scan_visitdnode(scn, ds, ostype,
705 cdnp, zb->zb_blkid * epb + i, tx);
708 (void) arc_buf_remove_ref(buf, &buf);
709 } else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) {
710 arc_flags_t flags = ARC_FLAG_WAIT;
714 err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, &buf,
715 ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
717 scn->scn_phys.scn_errors++;
723 dsl_scan_visitdnode(scn, ds, osp->os_type,
724 &osp->os_meta_dnode, DMU_META_DNODE_OBJECT, tx);
726 if (OBJSET_BUF_HAS_USERUSED(buf)) {
728 * We also always visit user/group accounting
729 * objects, and never skip them, even if we are
730 * pausing. This is necessary so that the space
731 * deltas from this txg get integrated.
733 dsl_scan_visitdnode(scn, ds, osp->os_type,
734 &osp->os_groupused_dnode,
735 DMU_GROUPUSED_OBJECT, tx);
736 dsl_scan_visitdnode(scn, ds, osp->os_type,
737 &osp->os_userused_dnode,
738 DMU_USERUSED_OBJECT, tx);
740 (void) arc_buf_remove_ref(buf, &buf);
747 dsl_scan_visitdnode(dsl_scan_t *scn, dsl_dataset_t *ds,
748 dmu_objset_type_t ostype, dnode_phys_t *dnp,
749 uint64_t object, dmu_tx_t *tx)
753 for (j = 0; j < dnp->dn_nblkptr; j++) {
754 zbookmark_phys_t czb;
756 SET_BOOKMARK(&czb, ds ? ds->ds_object : 0, object,
757 dnp->dn_nlevels - 1, j);
758 dsl_scan_visitbp(&dnp->dn_blkptr[j],
759 &czb, dnp, ds, scn, ostype, tx);
762 if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
763 zbookmark_phys_t czb;
764 SET_BOOKMARK(&czb, ds ? ds->ds_object : 0, object,
766 dsl_scan_visitbp(&dnp->dn_spill,
767 &czb, dnp, ds, scn, ostype, tx);
772 * The arguments are in this order because mdb can only print the
773 * first 5; we want them to be useful.
776 dsl_scan_visitbp(blkptr_t *bp, const zbookmark_phys_t *zb,
777 dnode_phys_t *dnp, dsl_dataset_t *ds, dsl_scan_t *scn,
778 dmu_objset_type_t ostype, dmu_tx_t *tx)
780 dsl_pool_t *dp = scn->scn_dp;
781 arc_buf_t *buf = NULL;
782 blkptr_t bp_toread = *bp;
784 /* ASSERT(pbuf == NULL || arc_released(pbuf)); */
786 if (dsl_scan_check_pause(scn, zb))
789 if (dsl_scan_check_resume(scn, dnp, zb))
795 scn->scn_visited_this_txg++;
798 "visiting ds=%p/%llu zb=%llx/%llx/%llx/%llx bp=%p",
799 ds, ds ? ds->ds_object : 0,
800 zb->zb_objset, zb->zb_object, zb->zb_level, zb->zb_blkid,
803 if (bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
806 if (dsl_scan_recurse(scn, ds, ostype, dnp, &bp_toread, zb, tx) != 0)
810 * If dsl_scan_ddt() has aready visited this block, it will have
811 * already done any translations or scrubbing, so don't call the
814 if (ddt_class_contains(dp->dp_spa,
815 scn->scn_phys.scn_ddt_class_max, bp)) {
821 * If this block is from the future (after cur_max_txg), then we
822 * are doing this on behalf of a deleted snapshot, and we will
823 * revisit the future block on the next pass of this dataset.
824 * Don't scan it now unless we need to because something
825 * under it was modified.
827 if (BP_PHYSICAL_BIRTH(bp) <= scn->scn_phys.scn_cur_max_txg) {
828 scan_funcs[scn->scn_phys.scn_func](dp, bp, zb);
833 dsl_scan_visit_rootbp(dsl_scan_t *scn, dsl_dataset_t *ds, blkptr_t *bp,
838 SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
839 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
840 dsl_scan_visitbp(bp, &zb, NULL,
841 ds, scn, DMU_OST_NONE, tx);
843 dprintf_ds(ds, "finished scan%s", "");
847 dsl_scan_ds_destroyed(dsl_dataset_t *ds, dmu_tx_t *tx)
849 dsl_pool_t *dp = ds->ds_dir->dd_pool;
850 dsl_scan_t *scn = dp->dp_scan;
853 if (scn->scn_phys.scn_state != DSS_SCANNING)
856 if (scn->scn_phys.scn_bookmark.zb_objset == ds->ds_object) {
857 if (ds->ds_is_snapshot) {
860 * - scn_cur_{min,max}_txg stays the same.
861 * - Setting the flag is not really necessary if
862 * scn_cur_max_txg == scn_max_txg, because there
863 * is nothing after this snapshot that we care
864 * about. However, we set it anyway and then
865 * ignore it when we retraverse it in
866 * dsl_scan_visitds().
868 scn->scn_phys.scn_bookmark.zb_objset =
869 dsl_dataset_phys(ds)->ds_next_snap_obj;
870 zfs_dbgmsg("destroying ds %llu; currently traversing; "
871 "reset zb_objset to %llu",
872 (u_longlong_t)ds->ds_object,
873 (u_longlong_t)dsl_dataset_phys(ds)->
875 scn->scn_phys.scn_flags |= DSF_VISIT_DS_AGAIN;
877 SET_BOOKMARK(&scn->scn_phys.scn_bookmark,
878 ZB_DESTROYED_OBJSET, 0, 0, 0);
879 zfs_dbgmsg("destroying ds %llu; currently traversing; "
880 "reset bookmark to -1,0,0,0",
881 (u_longlong_t)ds->ds_object);
883 } else if (zap_lookup_int_key(dp->dp_meta_objset,
884 scn->scn_phys.scn_queue_obj, ds->ds_object, &mintxg) == 0) {
885 ASSERT3U(dsl_dataset_phys(ds)->ds_num_children, <=, 1);
886 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
887 scn->scn_phys.scn_queue_obj, ds->ds_object, tx));
888 if (ds->ds_is_snapshot) {
890 * We keep the same mintxg; it could be >
891 * ds_creation_txg if the previous snapshot was
894 VERIFY(zap_add_int_key(dp->dp_meta_objset,
895 scn->scn_phys.scn_queue_obj,
896 dsl_dataset_phys(ds)->ds_next_snap_obj,
898 zfs_dbgmsg("destroying ds %llu; in queue; "
899 "replacing with %llu",
900 (u_longlong_t)ds->ds_object,
901 (u_longlong_t)dsl_dataset_phys(ds)->
904 zfs_dbgmsg("destroying ds %llu; in queue; removing",
905 (u_longlong_t)ds->ds_object);
910 * dsl_scan_sync() should be called after this, and should sync
911 * out our changed state, but just to be safe, do it here.
913 dsl_scan_sync_state(scn, tx);
917 dsl_scan_ds_snapshotted(dsl_dataset_t *ds, dmu_tx_t *tx)
919 dsl_pool_t *dp = ds->ds_dir->dd_pool;
920 dsl_scan_t *scn = dp->dp_scan;
923 if (scn->scn_phys.scn_state != DSS_SCANNING)
926 ASSERT(dsl_dataset_phys(ds)->ds_prev_snap_obj != 0);
928 if (scn->scn_phys.scn_bookmark.zb_objset == ds->ds_object) {
929 scn->scn_phys.scn_bookmark.zb_objset =
930 dsl_dataset_phys(ds)->ds_prev_snap_obj;
931 zfs_dbgmsg("snapshotting ds %llu; currently traversing; "
932 "reset zb_objset to %llu",
933 (u_longlong_t)ds->ds_object,
934 (u_longlong_t)dsl_dataset_phys(ds)->ds_prev_snap_obj);
935 } else if (zap_lookup_int_key(dp->dp_meta_objset,
936 scn->scn_phys.scn_queue_obj, ds->ds_object, &mintxg) == 0) {
937 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
938 scn->scn_phys.scn_queue_obj, ds->ds_object, tx));
939 VERIFY(zap_add_int_key(dp->dp_meta_objset,
940 scn->scn_phys.scn_queue_obj,
941 dsl_dataset_phys(ds)->ds_prev_snap_obj, mintxg, tx) == 0);
942 zfs_dbgmsg("snapshotting ds %llu; in queue; "
943 "replacing with %llu",
944 (u_longlong_t)ds->ds_object,
945 (u_longlong_t)dsl_dataset_phys(ds)->ds_prev_snap_obj);
947 dsl_scan_sync_state(scn, tx);
951 dsl_scan_ds_clone_swapped(dsl_dataset_t *ds1, dsl_dataset_t *ds2, dmu_tx_t *tx)
953 dsl_pool_t *dp = ds1->ds_dir->dd_pool;
954 dsl_scan_t *scn = dp->dp_scan;
957 if (scn->scn_phys.scn_state != DSS_SCANNING)
960 if (scn->scn_phys.scn_bookmark.zb_objset == ds1->ds_object) {
961 scn->scn_phys.scn_bookmark.zb_objset = ds2->ds_object;
962 zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
963 "reset zb_objset to %llu",
964 (u_longlong_t)ds1->ds_object,
965 (u_longlong_t)ds2->ds_object);
966 } else if (scn->scn_phys.scn_bookmark.zb_objset == ds2->ds_object) {
967 scn->scn_phys.scn_bookmark.zb_objset = ds1->ds_object;
968 zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
969 "reset zb_objset to %llu",
970 (u_longlong_t)ds2->ds_object,
971 (u_longlong_t)ds1->ds_object);
974 if (zap_lookup_int_key(dp->dp_meta_objset, scn->scn_phys.scn_queue_obj,
975 ds1->ds_object, &mintxg) == 0) {
978 ASSERT3U(mintxg, ==, dsl_dataset_phys(ds1)->ds_prev_snap_txg);
979 ASSERT3U(mintxg, ==, dsl_dataset_phys(ds2)->ds_prev_snap_txg);
980 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
981 scn->scn_phys.scn_queue_obj, ds1->ds_object, tx));
982 err = zap_add_int_key(dp->dp_meta_objset,
983 scn->scn_phys.scn_queue_obj, ds2->ds_object, mintxg, tx);
984 VERIFY(err == 0 || err == EEXIST);
986 /* Both were there to begin with */
987 VERIFY(0 == zap_add_int_key(dp->dp_meta_objset,
988 scn->scn_phys.scn_queue_obj,
989 ds1->ds_object, mintxg, tx));
991 zfs_dbgmsg("clone_swap ds %llu; in queue; "
992 "replacing with %llu",
993 (u_longlong_t)ds1->ds_object,
994 (u_longlong_t)ds2->ds_object);
995 } else if (zap_lookup_int_key(dp->dp_meta_objset,
996 scn->scn_phys.scn_queue_obj, ds2->ds_object, &mintxg) == 0) {
997 ASSERT3U(mintxg, ==, dsl_dataset_phys(ds1)->ds_prev_snap_txg);
998 ASSERT3U(mintxg, ==, dsl_dataset_phys(ds2)->ds_prev_snap_txg);
999 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
1000 scn->scn_phys.scn_queue_obj, ds2->ds_object, tx));
1001 VERIFY(0 == zap_add_int_key(dp->dp_meta_objset,
1002 scn->scn_phys.scn_queue_obj, ds1->ds_object, mintxg, tx));
1003 zfs_dbgmsg("clone_swap ds %llu; in queue; "
1004 "replacing with %llu",
1005 (u_longlong_t)ds2->ds_object,
1006 (u_longlong_t)ds1->ds_object);
1009 dsl_scan_sync_state(scn, tx);
1012 struct enqueue_clones_arg {
1019 enqueue_clones_cb(dsl_pool_t *dp, dsl_dataset_t *hds, void *arg)
1021 struct enqueue_clones_arg *eca = arg;
1024 dsl_scan_t *scn = dp->dp_scan;
1026 if (dsl_dir_phys(hds->ds_dir)->dd_origin_obj != eca->originobj)
1029 err = dsl_dataset_hold_obj(dp, hds->ds_object, FTAG, &ds);
1033 while (dsl_dataset_phys(ds)->ds_prev_snap_obj != eca->originobj) {
1034 dsl_dataset_t *prev;
1035 err = dsl_dataset_hold_obj(dp,
1036 dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev);
1038 dsl_dataset_rele(ds, FTAG);
1043 VERIFY(zap_add_int_key(dp->dp_meta_objset,
1044 scn->scn_phys.scn_queue_obj, ds->ds_object,
1045 dsl_dataset_phys(ds)->ds_prev_snap_txg, eca->tx) == 0);
1046 dsl_dataset_rele(ds, FTAG);
1051 dsl_scan_visitds(dsl_scan_t *scn, uint64_t dsobj, dmu_tx_t *tx)
1053 dsl_pool_t *dp = scn->scn_dp;
1057 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
1059 if (scn->scn_phys.scn_cur_min_txg >=
1060 scn->scn_phys.scn_max_txg) {
1062 * This can happen if this snapshot was created after the
1063 * scan started, and we already completed a previous snapshot
1064 * that was created after the scan started. This snapshot
1065 * only references blocks with:
1067 * birth < our ds_creation_txg
1068 * cur_min_txg is no less than ds_creation_txg.
1069 * We have already visited these blocks.
1071 * birth > scn_max_txg
1072 * The scan requested not to visit these blocks.
1074 * Subsequent snapshots (and clones) can reference our
1075 * blocks, or blocks with even higher birth times.
1076 * Therefore we do not need to visit them either,
1077 * so we do not add them to the work queue.
1079 * Note that checking for cur_min_txg >= cur_max_txg
1080 * is not sufficient, because in that case we may need to
1081 * visit subsequent snapshots. This happens when min_txg > 0,
1082 * which raises cur_min_txg. In this case we will visit
1083 * this dataset but skip all of its blocks, because the
1084 * rootbp's birth time is < cur_min_txg. Then we will
1085 * add the next snapshots/clones to the work queue.
1087 char *dsname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1088 dsl_dataset_name(ds, dsname);
1089 zfs_dbgmsg("scanning dataset %llu (%s) is unnecessary because "
1090 "cur_min_txg (%llu) >= max_txg (%llu)",
1092 scn->scn_phys.scn_cur_min_txg,
1093 scn->scn_phys.scn_max_txg);
1094 kmem_free(dsname, MAXNAMELEN);
1099 if (dmu_objset_from_ds(ds, &os))
1103 * Only the ZIL in the head (non-snapshot) is valid. Even though
1104 * snapshots can have ZIL block pointers (which may be the same
1105 * BP as in the head), they must be ignored. So we traverse the
1106 * ZIL here, rather than in scan_recurse(), because the regular
1107 * snapshot block-sharing rules don't apply to it.
1109 if (DSL_SCAN_IS_SCRUB_RESILVER(scn) && !ds->ds_is_snapshot)
1110 dsl_scan_zil(dp, &os->os_zil_header);
1113 * Iterate over the bps in this ds.
1115 dmu_buf_will_dirty(ds->ds_dbuf, tx);
1116 dsl_scan_visit_rootbp(scn, ds, &dsl_dataset_phys(ds)->ds_bp, tx);
1118 char *dsname = kmem_alloc(ZFS_MAXNAMELEN, KM_SLEEP);
1119 dsl_dataset_name(ds, dsname);
1120 zfs_dbgmsg("scanned dataset %llu (%s) with min=%llu max=%llu; "
1122 (longlong_t)dsobj, dsname,
1123 (longlong_t)scn->scn_phys.scn_cur_min_txg,
1124 (longlong_t)scn->scn_phys.scn_cur_max_txg,
1125 (int)scn->scn_pausing);
1126 kmem_free(dsname, ZFS_MAXNAMELEN);
1128 if (scn->scn_pausing)
1132 * We've finished this pass over this dataset.
1136 * If we did not completely visit this dataset, do another pass.
1138 if (scn->scn_phys.scn_flags & DSF_VISIT_DS_AGAIN) {
1139 zfs_dbgmsg("incomplete pass; visiting again");
1140 scn->scn_phys.scn_flags &= ~DSF_VISIT_DS_AGAIN;
1141 VERIFY(zap_add_int_key(dp->dp_meta_objset,
1142 scn->scn_phys.scn_queue_obj, ds->ds_object,
1143 scn->scn_phys.scn_cur_max_txg, tx) == 0);
1148 * Add descendent datasets to work queue.
1150 if (dsl_dataset_phys(ds)->ds_next_snap_obj != 0) {
1151 VERIFY(zap_add_int_key(dp->dp_meta_objset,
1152 scn->scn_phys.scn_queue_obj,
1153 dsl_dataset_phys(ds)->ds_next_snap_obj,
1154 dsl_dataset_phys(ds)->ds_creation_txg, tx) == 0);
1156 if (dsl_dataset_phys(ds)->ds_num_children > 1) {
1157 boolean_t usenext = B_FALSE;
1158 if (dsl_dataset_phys(ds)->ds_next_clones_obj != 0) {
1161 * A bug in a previous version of the code could
1162 * cause upgrade_clones_cb() to not set
1163 * ds_next_snap_obj when it should, leading to a
1164 * missing entry. Therefore we can only use the
1165 * next_clones_obj when its count is correct.
1167 int err = zap_count(dp->dp_meta_objset,
1168 dsl_dataset_phys(ds)->ds_next_clones_obj, &count);
1170 count == dsl_dataset_phys(ds)->ds_num_children - 1)
1175 VERIFY0(zap_join_key(dp->dp_meta_objset,
1176 dsl_dataset_phys(ds)->ds_next_clones_obj,
1177 scn->scn_phys.scn_queue_obj,
1178 dsl_dataset_phys(ds)->ds_creation_txg, tx));
1180 struct enqueue_clones_arg eca;
1182 eca.originobj = ds->ds_object;
1184 VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
1185 enqueue_clones_cb, &eca, DS_FIND_CHILDREN));
1190 dsl_dataset_rele(ds, FTAG);
1195 enqueue_cb(dsl_pool_t *dp, dsl_dataset_t *hds, void *arg)
1200 dsl_scan_t *scn = dp->dp_scan;
1202 err = dsl_dataset_hold_obj(dp, hds->ds_object, FTAG, &ds);
1206 while (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
1207 dsl_dataset_t *prev;
1208 err = dsl_dataset_hold_obj(dp,
1209 dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev);
1211 dsl_dataset_rele(ds, FTAG);
1216 * If this is a clone, we don't need to worry about it for now.
1218 if (dsl_dataset_phys(prev)->ds_next_snap_obj != ds->ds_object) {
1219 dsl_dataset_rele(ds, FTAG);
1220 dsl_dataset_rele(prev, FTAG);
1223 dsl_dataset_rele(ds, FTAG);
1227 VERIFY(zap_add_int_key(dp->dp_meta_objset, scn->scn_phys.scn_queue_obj,
1228 ds->ds_object, dsl_dataset_phys(ds)->ds_prev_snap_txg, tx) == 0);
1229 dsl_dataset_rele(ds, FTAG);
1234 * Scrub/dedup interaction.
1236 * If there are N references to a deduped block, we don't want to scrub it
1237 * N times -- ideally, we should scrub it exactly once.
1239 * We leverage the fact that the dde's replication class (enum ddt_class)
1240 * is ordered from highest replication class (DDT_CLASS_DITTO) to lowest
1241 * (DDT_CLASS_UNIQUE) so that we may walk the DDT in that order.
1243 * To prevent excess scrubbing, the scrub begins by walking the DDT
1244 * to find all blocks with refcnt > 1, and scrubs each of these once.
1245 * Since there are two replication classes which contain blocks with
1246 * refcnt > 1, we scrub the highest replication class (DDT_CLASS_DITTO) first.
1247 * Finally the top-down scrub begins, only visiting blocks with refcnt == 1.
1249 * There would be nothing more to say if a block's refcnt couldn't change
1250 * during a scrub, but of course it can so we must account for changes
1251 * in a block's replication class.
1253 * Here's an example of what can occur:
1255 * If a block has refcnt > 1 during the DDT scrub phase, but has refcnt == 1
1256 * when visited during the top-down scrub phase, it will be scrubbed twice.
1257 * This negates our scrub optimization, but is otherwise harmless.
1259 * If a block has refcnt == 1 during the DDT scrub phase, but has refcnt > 1
1260 * on each visit during the top-down scrub phase, it will never be scrubbed.
1261 * To catch this, ddt_sync_entry() notifies the scrub code whenever a block's
1262 * reference class transitions to a higher level (i.e DDT_CLASS_UNIQUE to
1263 * DDT_CLASS_DUPLICATE); if it transitions from refcnt == 1 to refcnt > 1
1264 * while a scrub is in progress, it scrubs the block right then.
1267 dsl_scan_ddt(dsl_scan_t *scn, dmu_tx_t *tx)
1269 ddt_bookmark_t *ddb = &scn->scn_phys.scn_ddt_bookmark;
1270 ddt_entry_t dde = { 0 };
1274 while ((error = ddt_walk(scn->scn_dp->dp_spa, ddb, &dde)) == 0) {
1277 if (ddb->ddb_class > scn->scn_phys.scn_ddt_class_max)
1279 dprintf("visiting ddb=%llu/%llu/%llu/%llx\n",
1280 (longlong_t)ddb->ddb_class,
1281 (longlong_t)ddb->ddb_type,
1282 (longlong_t)ddb->ddb_checksum,
1283 (longlong_t)ddb->ddb_cursor);
1285 /* There should be no pending changes to the dedup table */
1286 ddt = scn->scn_dp->dp_spa->spa_ddt[ddb->ddb_checksum];
1287 ASSERT(avl_first(&ddt->ddt_tree) == NULL);
1289 dsl_scan_ddt_entry(scn, ddb->ddb_checksum, &dde, tx);
1292 if (dsl_scan_check_pause(scn, NULL))
1296 zfs_dbgmsg("scanned %llu ddt entries with class_max = %u; pausing=%u",
1297 (longlong_t)n, (int)scn->scn_phys.scn_ddt_class_max,
1298 (int)scn->scn_pausing);
1300 ASSERT(error == 0 || error == ENOENT);
1301 ASSERT(error != ENOENT ||
1302 ddb->ddb_class > scn->scn_phys.scn_ddt_class_max);
1307 dsl_scan_ddt_entry(dsl_scan_t *scn, enum zio_checksum checksum,
1308 ddt_entry_t *dde, dmu_tx_t *tx)
1310 const ddt_key_t *ddk = &dde->dde_key;
1311 ddt_phys_t *ddp = dde->dde_phys;
1313 zbookmark_phys_t zb = { 0 };
1315 if (scn->scn_phys.scn_state != DSS_SCANNING)
1318 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
1319 if (ddp->ddp_phys_birth == 0 ||
1320 ddp->ddp_phys_birth > scn->scn_phys.scn_max_txg)
1322 ddt_bp_create(checksum, ddk, ddp, &bp);
1324 scn->scn_visited_this_txg++;
1325 scan_funcs[scn->scn_phys.scn_func](scn->scn_dp, &bp, &zb);
1330 dsl_scan_visit(dsl_scan_t *scn, dmu_tx_t *tx)
1332 dsl_pool_t *dp = scn->scn_dp;
1336 if (scn->scn_phys.scn_ddt_bookmark.ddb_class <=
1337 scn->scn_phys.scn_ddt_class_max) {
1338 scn->scn_phys.scn_cur_min_txg = scn->scn_phys.scn_min_txg;
1339 scn->scn_phys.scn_cur_max_txg = scn->scn_phys.scn_max_txg;
1340 dsl_scan_ddt(scn, tx);
1341 if (scn->scn_pausing)
1345 if (scn->scn_phys.scn_bookmark.zb_objset == DMU_META_OBJSET) {
1346 /* First do the MOS & ORIGIN */
1348 scn->scn_phys.scn_cur_min_txg = scn->scn_phys.scn_min_txg;
1349 scn->scn_phys.scn_cur_max_txg = scn->scn_phys.scn_max_txg;
1350 dsl_scan_visit_rootbp(scn, NULL,
1351 &dp->dp_meta_rootbp, tx);
1352 spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
1353 if (scn->scn_pausing)
1356 if (spa_version(dp->dp_spa) < SPA_VERSION_DSL_SCRUB) {
1357 VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
1358 enqueue_cb, tx, DS_FIND_CHILDREN));
1360 dsl_scan_visitds(scn,
1361 dp->dp_origin_snap->ds_object, tx);
1363 ASSERT(!scn->scn_pausing);
1364 } else if (scn->scn_phys.scn_bookmark.zb_objset !=
1365 ZB_DESTROYED_OBJSET) {
1367 * If we were paused, continue from here. Note if the
1368 * ds we were paused on was deleted, the zb_objset may
1369 * be -1, so we will skip this and find a new objset
1372 dsl_scan_visitds(scn, scn->scn_phys.scn_bookmark.zb_objset, tx);
1373 if (scn->scn_pausing)
1378 * In case we were paused right at the end of the ds, zero the
1379 * bookmark so we don't think that we're still trying to resume.
1381 bzero(&scn->scn_phys.scn_bookmark, sizeof (zbookmark_phys_t));
1383 /* keep pulling things out of the zap-object-as-queue */
1384 while (zap_cursor_init(&zc, dp->dp_meta_objset,
1385 scn->scn_phys.scn_queue_obj),
1386 zap_cursor_retrieve(&zc, &za) == 0) {
1390 dsobj = strtonum(za.za_name, NULL);
1391 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
1392 scn->scn_phys.scn_queue_obj, dsobj, tx));
1394 /* Set up min/max txg */
1395 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
1396 if (za.za_first_integer != 0) {
1397 scn->scn_phys.scn_cur_min_txg =
1398 MAX(scn->scn_phys.scn_min_txg,
1399 za.za_first_integer);
1401 scn->scn_phys.scn_cur_min_txg =
1402 MAX(scn->scn_phys.scn_min_txg,
1403 dsl_dataset_phys(ds)->ds_prev_snap_txg);
1405 scn->scn_phys.scn_cur_max_txg = dsl_scan_ds_maxtxg(ds);
1406 dsl_dataset_rele(ds, FTAG);
1408 dsl_scan_visitds(scn, dsobj, tx);
1409 zap_cursor_fini(&zc);
1410 if (scn->scn_pausing)
1413 zap_cursor_fini(&zc);
1417 dsl_scan_free_should_pause(dsl_scan_t *scn)
1419 uint64_t elapsed_nanosecs;
1424 if (scn->scn_visited_this_txg >= zfs_free_max_blocks)
1427 elapsed_nanosecs = gethrtime() - scn->scn_sync_start_time;
1428 return (elapsed_nanosecs / NANOSEC > zfs_txg_timeout ||
1429 (NSEC2MSEC(elapsed_nanosecs) > zfs_free_min_time_ms &&
1430 txg_sync_waiting(scn->scn_dp)) ||
1431 spa_shutting_down(scn->scn_dp->dp_spa));
1435 dsl_scan_free_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1437 dsl_scan_t *scn = arg;
1439 if (!scn->scn_is_bptree ||
1440 (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_OBJSET)) {
1441 if (dsl_scan_free_should_pause(scn))
1442 return (SET_ERROR(ERESTART));
1445 zio_nowait(zio_free_sync(scn->scn_zio_root, scn->scn_dp->dp_spa,
1446 dmu_tx_get_txg(tx), bp, BP_GET_PSIZE(bp), 0));
1447 dsl_dir_diduse_space(tx->tx_pool->dp_free_dir, DD_USED_HEAD,
1448 -bp_get_dsize_sync(scn->scn_dp->dp_spa, bp),
1449 -BP_GET_PSIZE(bp), -BP_GET_UCSIZE(bp), tx);
1450 scn->scn_visited_this_txg++;
1455 dsl_scan_active(dsl_scan_t *scn)
1457 spa_t *spa = scn->scn_dp->dp_spa;
1458 uint64_t used = 0, comp, uncomp;
1460 if (spa->spa_load_state != SPA_LOAD_NONE)
1462 if (spa_shutting_down(spa))
1464 if (scn->scn_phys.scn_state == DSS_SCANNING ||
1465 (scn->scn_async_destroying && !scn->scn_async_stalled))
1468 if (spa_version(scn->scn_dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
1469 (void) bpobj_space(&scn->scn_dp->dp_free_bpobj,
1470 &used, &comp, &uncomp);
1476 dsl_scan_sync(dsl_pool_t *dp, dmu_tx_t *tx)
1478 dsl_scan_t *scn = dp->dp_scan;
1479 spa_t *spa = dp->dp_spa;
1483 * Check for scn_restart_txg before checking spa_load_state, so
1484 * that we can restart an old-style scan while the pool is being
1485 * imported (see dsl_scan_init).
1487 if (dsl_scan_restarting(scn, tx)) {
1488 pool_scan_func_t func = POOL_SCAN_SCRUB;
1489 dsl_scan_done(scn, B_FALSE, tx);
1490 if (vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL))
1491 func = POOL_SCAN_RESILVER;
1492 zfs_dbgmsg("restarting scan func=%u txg=%llu",
1494 dsl_scan_setup_sync(&func, tx);
1498 * Only process scans in sync pass 1.
1500 if (spa_sync_pass(dp->dp_spa) > 1)
1504 * If the spa is shutting down, then stop scanning. This will
1505 * ensure that the scan does not dirty any new data during the
1508 if (spa_shutting_down(spa))
1512 * If the scan is inactive due to a stalled async destroy, try again.
1514 if (!scn->scn_async_stalled && !dsl_scan_active(scn))
1517 scn->scn_visited_this_txg = 0;
1518 scn->scn_pausing = B_FALSE;
1519 scn->scn_sync_start_time = gethrtime();
1520 spa->spa_scrub_active = B_TRUE;
1523 * First process the async destroys. If we pause, don't do
1524 * any scrubbing or resilvering. This ensures that there are no
1525 * async destroys while we are scanning, so the scan code doesn't
1526 * have to worry about traversing it. It is also faster to free the
1527 * blocks than to scrub them.
1529 if (zfs_free_bpobj_enabled &&
1530 spa_version(dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
1531 scn->scn_is_bptree = B_FALSE;
1532 scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
1533 NULL, ZIO_FLAG_MUSTSUCCEED);
1534 err = bpobj_iterate(&dp->dp_free_bpobj,
1535 dsl_scan_free_block_cb, scn, tx);
1536 VERIFY3U(0, ==, zio_wait(scn->scn_zio_root));
1538 if (err != 0 && err != ERESTART)
1539 zfs_panic_recover("error %u from bpobj_iterate()", err);
1542 if (err == 0 && spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) {
1543 ASSERT(scn->scn_async_destroying);
1544 scn->scn_is_bptree = B_TRUE;
1545 scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
1546 NULL, ZIO_FLAG_MUSTSUCCEED);
1547 err = bptree_iterate(dp->dp_meta_objset,
1548 dp->dp_bptree_obj, B_TRUE, dsl_scan_free_block_cb, scn, tx);
1549 VERIFY0(zio_wait(scn->scn_zio_root));
1551 if (err == EIO || err == ECKSUM) {
1553 } else if (err != 0 && err != ERESTART) {
1554 zfs_panic_recover("error %u from "
1555 "traverse_dataset_destroyed()", err);
1558 if (bptree_is_empty(dp->dp_meta_objset, dp->dp_bptree_obj)) {
1559 /* finished; deactivate async destroy feature */
1560 spa_feature_decr(spa, SPA_FEATURE_ASYNC_DESTROY, tx);
1561 ASSERT(!spa_feature_is_active(spa,
1562 SPA_FEATURE_ASYNC_DESTROY));
1563 VERIFY0(zap_remove(dp->dp_meta_objset,
1564 DMU_POOL_DIRECTORY_OBJECT,
1565 DMU_POOL_BPTREE_OBJ, tx));
1566 VERIFY0(bptree_free(dp->dp_meta_objset,
1567 dp->dp_bptree_obj, tx));
1568 dp->dp_bptree_obj = 0;
1569 scn->scn_async_destroying = B_FALSE;
1570 scn->scn_async_stalled = B_FALSE;
1573 * If we didn't make progress, mark the async
1574 * destroy as stalled, so that we will not initiate
1575 * a spa_sync() on its behalf. Note that we only
1576 * check this if we are not finished, because if the
1577 * bptree had no blocks for us to visit, we can
1578 * finish without "making progress".
1580 scn->scn_async_stalled =
1581 (scn->scn_visited_this_txg == 0);
1584 if (scn->scn_visited_this_txg) {
1585 zfs_dbgmsg("freed %llu blocks in %llums from "
1586 "free_bpobj/bptree txg %llu; err=%d",
1587 (longlong_t)scn->scn_visited_this_txg,
1589 NSEC2MSEC(gethrtime() - scn->scn_sync_start_time),
1590 (longlong_t)tx->tx_txg, err);
1591 scn->scn_visited_this_txg = 0;
1594 * Write out changes to the DDT that may be required as a
1595 * result of the blocks freed. This ensures that the DDT
1596 * is clean when a scrub/resilver runs.
1598 ddt_sync(spa, tx->tx_txg);
1602 if (dp->dp_free_dir != NULL && !scn->scn_async_destroying &&
1603 zfs_free_leak_on_eio &&
1604 (dsl_dir_phys(dp->dp_free_dir)->dd_used_bytes != 0 ||
1605 dsl_dir_phys(dp->dp_free_dir)->dd_compressed_bytes != 0 ||
1606 dsl_dir_phys(dp->dp_free_dir)->dd_uncompressed_bytes != 0)) {
1608 * We have finished background destroying, but there is still
1609 * some space left in the dp_free_dir. Transfer this leaked
1610 * space to the dp_leak_dir.
1612 if (dp->dp_leak_dir == NULL) {
1613 rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
1614 (void) dsl_dir_create_sync(dp, dp->dp_root_dir,
1616 VERIFY0(dsl_pool_open_special_dir(dp,
1617 LEAK_DIR_NAME, &dp->dp_leak_dir));
1618 rrw_exit(&dp->dp_config_rwlock, FTAG);
1620 dsl_dir_diduse_space(dp->dp_leak_dir, DD_USED_HEAD,
1621 dsl_dir_phys(dp->dp_free_dir)->dd_used_bytes,
1622 dsl_dir_phys(dp->dp_free_dir)->dd_compressed_bytes,
1623 dsl_dir_phys(dp->dp_free_dir)->dd_uncompressed_bytes, tx);
1624 dsl_dir_diduse_space(dp->dp_free_dir, DD_USED_HEAD,
1625 -dsl_dir_phys(dp->dp_free_dir)->dd_used_bytes,
1626 -dsl_dir_phys(dp->dp_free_dir)->dd_compressed_bytes,
1627 -dsl_dir_phys(dp->dp_free_dir)->dd_uncompressed_bytes, tx);
1629 if (dp->dp_free_dir != NULL && !scn->scn_async_destroying) {
1630 /* finished; verify that space accounting went to zero */
1631 ASSERT0(dsl_dir_phys(dp->dp_free_dir)->dd_used_bytes);
1632 ASSERT0(dsl_dir_phys(dp->dp_free_dir)->dd_compressed_bytes);
1633 ASSERT0(dsl_dir_phys(dp->dp_free_dir)->dd_uncompressed_bytes);
1636 if (scn->scn_phys.scn_state != DSS_SCANNING)
1639 if (scn->scn_done_txg == tx->tx_txg) {
1640 ASSERT(!scn->scn_pausing);
1641 /* finished with scan. */
1642 zfs_dbgmsg("txg %llu scan complete", tx->tx_txg);
1643 dsl_scan_done(scn, B_TRUE, tx);
1644 ASSERT3U(spa->spa_scrub_inflight, ==, 0);
1645 dsl_scan_sync_state(scn, tx);
1649 if (scn->scn_phys.scn_ddt_bookmark.ddb_class <=
1650 scn->scn_phys.scn_ddt_class_max) {
1651 zfs_dbgmsg("doing scan sync txg %llu; "
1652 "ddt bm=%llu/%llu/%llu/%llx",
1653 (longlong_t)tx->tx_txg,
1654 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_class,
1655 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_type,
1656 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_checksum,
1657 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_cursor);
1658 ASSERT(scn->scn_phys.scn_bookmark.zb_objset == 0);
1659 ASSERT(scn->scn_phys.scn_bookmark.zb_object == 0);
1660 ASSERT(scn->scn_phys.scn_bookmark.zb_level == 0);
1661 ASSERT(scn->scn_phys.scn_bookmark.zb_blkid == 0);
1663 zfs_dbgmsg("doing scan sync txg %llu; bm=%llu/%llu/%llu/%llu",
1664 (longlong_t)tx->tx_txg,
1665 (longlong_t)scn->scn_phys.scn_bookmark.zb_objset,
1666 (longlong_t)scn->scn_phys.scn_bookmark.zb_object,
1667 (longlong_t)scn->scn_phys.scn_bookmark.zb_level,
1668 (longlong_t)scn->scn_phys.scn_bookmark.zb_blkid);
1671 scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
1672 NULL, ZIO_FLAG_CANFAIL);
1673 dsl_pool_config_enter(dp, FTAG);
1674 dsl_scan_visit(scn, tx);
1675 dsl_pool_config_exit(dp, FTAG);
1676 (void) zio_wait(scn->scn_zio_root);
1677 scn->scn_zio_root = NULL;
1679 zfs_dbgmsg("visited %llu blocks in %llums",
1680 (longlong_t)scn->scn_visited_this_txg,
1681 (longlong_t)NSEC2MSEC(gethrtime() - scn->scn_sync_start_time));
1683 if (!scn->scn_pausing) {
1684 scn->scn_done_txg = tx->tx_txg + 1;
1685 zfs_dbgmsg("txg %llu traversal complete, waiting till txg %llu",
1686 tx->tx_txg, scn->scn_done_txg);
1689 if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
1690 mutex_enter(&spa->spa_scrub_lock);
1691 while (spa->spa_scrub_inflight > 0) {
1692 cv_wait(&spa->spa_scrub_io_cv,
1693 &spa->spa_scrub_lock);
1695 mutex_exit(&spa->spa_scrub_lock);
1698 dsl_scan_sync_state(scn, tx);
1702 * This will start a new scan, or restart an existing one.
1705 dsl_resilver_restart(dsl_pool_t *dp, uint64_t txg)
1709 tx = dmu_tx_create_dd(dp->dp_mos_dir);
1710 VERIFY(0 == dmu_tx_assign(tx, TXG_WAIT));
1712 txg = dmu_tx_get_txg(tx);
1713 dp->dp_scan->scn_restart_txg = txg;
1716 dp->dp_scan->scn_restart_txg = txg;
1718 zfs_dbgmsg("restarting resilver txg=%llu", txg);
1722 dsl_scan_resilvering(dsl_pool_t *dp)
1724 return (dp->dp_scan->scn_phys.scn_state == DSS_SCANNING &&
1725 dp->dp_scan->scn_phys.scn_func == POOL_SCAN_RESILVER);
1733 count_block(zfs_all_blkstats_t *zab, const blkptr_t *bp)
1738 * If we resume after a reboot, zab will be NULL; don't record
1739 * incomplete stats in that case.
1744 for (i = 0; i < 4; i++) {
1745 int l = (i < 2) ? BP_GET_LEVEL(bp) : DN_MAX_LEVELS;
1746 int t = (i & 1) ? BP_GET_TYPE(bp) : DMU_OT_TOTAL;
1747 if (t & DMU_OT_NEWTYPE)
1749 zfs_blkstat_t *zb = &zab->zab_type[l][t];
1753 zb->zb_asize += BP_GET_ASIZE(bp);
1754 zb->zb_lsize += BP_GET_LSIZE(bp);
1755 zb->zb_psize += BP_GET_PSIZE(bp);
1756 zb->zb_gangs += BP_COUNT_GANG(bp);
1758 switch (BP_GET_NDVAS(bp)) {
1760 if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
1761 DVA_GET_VDEV(&bp->blk_dva[1]))
1762 zb->zb_ditto_2_of_2_samevdev++;
1765 equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
1766 DVA_GET_VDEV(&bp->blk_dva[1])) +
1767 (DVA_GET_VDEV(&bp->blk_dva[0]) ==
1768 DVA_GET_VDEV(&bp->blk_dva[2])) +
1769 (DVA_GET_VDEV(&bp->blk_dva[1]) ==
1770 DVA_GET_VDEV(&bp->blk_dva[2]));
1772 zb->zb_ditto_2_of_3_samevdev++;
1773 else if (equal == 3)
1774 zb->zb_ditto_3_of_3_samevdev++;
1781 dsl_scan_scrub_done(zio_t *zio)
1783 spa_t *spa = zio->io_spa;
1785 zio_data_buf_free(zio->io_data, zio->io_size);
1787 mutex_enter(&spa->spa_scrub_lock);
1788 spa->spa_scrub_inflight--;
1789 cv_broadcast(&spa->spa_scrub_io_cv);
1791 if (zio->io_error && (zio->io_error != ECKSUM ||
1792 !(zio->io_flags & ZIO_FLAG_SPECULATIVE))) {
1793 spa->spa_dsl_pool->dp_scan->scn_phys.scn_errors++;
1795 mutex_exit(&spa->spa_scrub_lock);
1799 dsl_scan_scrub_cb(dsl_pool_t *dp,
1800 const blkptr_t *bp, const zbookmark_phys_t *zb)
1802 dsl_scan_t *scn = dp->dp_scan;
1803 size_t size = BP_GET_PSIZE(bp);
1804 spa_t *spa = dp->dp_spa;
1805 uint64_t phys_birth = BP_PHYSICAL_BIRTH(bp);
1807 int zio_flags = ZIO_FLAG_SCAN_THREAD | ZIO_FLAG_RAW | ZIO_FLAG_CANFAIL;
1808 unsigned int scan_delay = 0;
1810 if (phys_birth <= scn->scn_phys.scn_min_txg ||
1811 phys_birth >= scn->scn_phys.scn_max_txg)
1814 count_block(dp->dp_blkstats, bp);
1816 if (BP_IS_EMBEDDED(bp))
1819 ASSERT(DSL_SCAN_IS_SCRUB_RESILVER(scn));
1820 if (scn->scn_phys.scn_func == POOL_SCAN_SCRUB) {
1821 zio_flags |= ZIO_FLAG_SCRUB;
1823 scan_delay = zfs_scrub_delay;
1825 ASSERT3U(scn->scn_phys.scn_func, ==, POOL_SCAN_RESILVER);
1826 zio_flags |= ZIO_FLAG_RESILVER;
1828 scan_delay = zfs_resilver_delay;
1831 /* If it's an intent log block, failure is expected. */
1832 if (zb->zb_level == ZB_ZIL_LEVEL)
1833 zio_flags |= ZIO_FLAG_SPECULATIVE;
1835 for (int d = 0; d < BP_GET_NDVAS(bp); d++) {
1836 vdev_t *vd = vdev_lookup_top(spa,
1837 DVA_GET_VDEV(&bp->blk_dva[d]));
1840 * Keep track of how much data we've examined so that
1841 * zpool(1M) status can make useful progress reports.
1843 scn->scn_phys.scn_examined += DVA_GET_ASIZE(&bp->blk_dva[d]);
1844 spa->spa_scan_pass_exam += DVA_GET_ASIZE(&bp->blk_dva[d]);
1846 /* if it's a resilver, this may not be in the target range */
1848 if (DVA_GET_GANG(&bp->blk_dva[d])) {
1850 * Gang members may be spread across multiple
1851 * vdevs, so the best estimate we have is the
1852 * scrub range, which has already been checked.
1853 * XXX -- it would be better to change our
1854 * allocation policy to ensure that all
1855 * gang members reside on the same vdev.
1859 needs_io = vdev_dtl_contains(vd, DTL_PARTIAL,
1865 if (needs_io && !zfs_no_scrub_io) {
1866 vdev_t *rvd = spa->spa_root_vdev;
1867 uint64_t maxinflight = rvd->vdev_children *
1868 MAX(zfs_top_maxinflight, 1);
1869 void *data = zio_data_buf_alloc(size);
1871 mutex_enter(&spa->spa_scrub_lock);
1872 while (spa->spa_scrub_inflight >= maxinflight)
1873 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
1874 spa->spa_scrub_inflight++;
1875 mutex_exit(&spa->spa_scrub_lock);
1878 * If we're seeing recent (zfs_scan_idle) "important" I/Os
1879 * then throttle our workload to limit the impact of a scan.
1881 if (ddi_get_lbolt64() - spa->spa_last_io <= zfs_scan_idle)
1882 delay(MAX((int)scan_delay, 0));
1884 zio_nowait(zio_read(NULL, spa, bp, data, size,
1885 dsl_scan_scrub_done, NULL, ZIO_PRIORITY_SCRUB,
1889 /* do not relocate this block */
1894 dsl_scan(dsl_pool_t *dp, pool_scan_func_t func)
1896 spa_t *spa = dp->dp_spa;
1899 * Purge all vdev caches and probe all devices. We do this here
1900 * rather than in sync context because this requires a writer lock
1901 * on the spa_config lock, which we can't do from sync context. The
1902 * spa_scrub_reopen flag indicates that vdev_open() should not
1903 * attempt to start another scrub.
1905 spa_vdev_state_enter(spa, SCL_NONE);
1906 spa->spa_scrub_reopen = B_TRUE;
1907 vdev_reopen(spa->spa_root_vdev);
1908 spa->spa_scrub_reopen = B_FALSE;
1909 (void) spa_vdev_state_exit(spa, NULL, 0);
1911 return (dsl_sync_task(spa_name(spa), dsl_scan_setup_check,
1912 dsl_scan_setup_sync, &func, 0, ZFS_SPACE_CHECK_NONE));
1916 dsl_scan_restarting(dsl_scan_t *scn, dmu_tx_t *tx)
1918 return (scn->scn_restart_txg != 0 &&
1919 scn->scn_restart_txg <= tx->tx_txg);