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, 2014 by Delphix. All rights reserved.
26 #include <sys/dsl_scan.h>
27 #include <sys/dsl_pool.h>
28 #include <sys/dsl_dataset.h>
29 #include <sys/dsl_prop.h>
30 #include <sys/dsl_dir.h>
31 #include <sys/dsl_synctask.h>
32 #include <sys/dnode.h>
33 #include <sys/dmu_tx.h>
34 #include <sys/dmu_objset.h>
38 #include <sys/zfs_context.h>
39 #include <sys/fs/zfs.h>
40 #include <sys/zfs_znode.h>
41 #include <sys/spa_impl.h>
42 #include <sys/vdev_impl.h>
43 #include <sys/zil_impl.h>
44 #include <sys/zio_checksum.h>
47 #include <sys/sa_impl.h>
48 #include <sys/zfeature.h>
50 #include <sys/zfs_vfsops.h>
53 typedef int (scan_cb_t)(dsl_pool_t *, const blkptr_t *,
54 const zbookmark_phys_t *);
56 static scan_cb_t dsl_scan_scrub_cb;
57 static void dsl_scan_cancel_sync(void *, dmu_tx_t *);
58 static void dsl_scan_sync_state(dsl_scan_t *, dmu_tx_t *tx);
60 unsigned int zfs_top_maxinflight = 32; /* maximum I/Os per top-level */
61 unsigned int zfs_resilver_delay = 2; /* number of ticks to delay resilver */
62 unsigned int zfs_scrub_delay = 4; /* number of ticks to delay scrub */
63 unsigned int zfs_scan_idle = 50; /* idle window in clock ticks */
65 unsigned int zfs_scan_min_time_ms = 1000; /* min millisecs to scrub per txg */
66 unsigned int zfs_free_min_time_ms = 1000; /* min millisecs to free per txg */
67 unsigned int zfs_resilver_min_time_ms = 3000; /* min millisecs to resilver
69 boolean_t zfs_no_scrub_io = B_FALSE; /* set to disable scrub i/o */
70 boolean_t zfs_no_scrub_prefetch = B_FALSE; /* set to disable scrub prefetch */
72 SYSCTL_DECL(_vfs_zfs);
73 SYSCTL_UINT(_vfs_zfs, OID_AUTO, top_maxinflight, CTLFLAG_RWTUN,
74 &zfs_top_maxinflight, 0, "Maximum I/Os per top-level vdev");
75 SYSCTL_UINT(_vfs_zfs, OID_AUTO, resilver_delay, CTLFLAG_RWTUN,
76 &zfs_resilver_delay, 0, "Number of ticks to delay resilver");
77 SYSCTL_UINT(_vfs_zfs, OID_AUTO, scrub_delay, CTLFLAG_RWTUN,
78 &zfs_scrub_delay, 0, "Number of ticks to delay scrub");
79 SYSCTL_UINT(_vfs_zfs, OID_AUTO, scan_idle, CTLFLAG_RWTUN,
80 &zfs_scan_idle, 0, "Idle scan window in clock ticks");
81 SYSCTL_UINT(_vfs_zfs, OID_AUTO, scan_min_time_ms, CTLFLAG_RWTUN,
82 &zfs_scan_min_time_ms, 0, "Min millisecs to scrub per txg");
83 SYSCTL_UINT(_vfs_zfs, OID_AUTO, free_min_time_ms, CTLFLAG_RWTUN,
84 &zfs_free_min_time_ms, 0, "Min millisecs to free per txg");
85 SYSCTL_UINT(_vfs_zfs, OID_AUTO, resilver_min_time_ms, CTLFLAG_RWTUN,
86 &zfs_resilver_min_time_ms, 0, "Min millisecs to resilver per txg");
87 SYSCTL_INT(_vfs_zfs, OID_AUTO, no_scrub_io, CTLFLAG_RWTUN,
88 &zfs_no_scrub_io, 0, "Disable scrub I/O");
89 SYSCTL_INT(_vfs_zfs, OID_AUTO, no_scrub_prefetch, CTLFLAG_RWTUN,
90 &zfs_no_scrub_prefetch, 0, "Disable scrub prefetching");
92 enum ddt_class zfs_scrub_ddt_class_max = DDT_CLASS_DUPLICATE;
93 /* max number of blocks to free in a single TXG */
94 uint64_t zfs_free_max_blocks = UINT64_MAX;
95 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, free_max_blocks, CTLFLAG_RWTUN,
96 &zfs_free_max_blocks, 0, "Maximum number of blocks to free in one TXG");
99 #define DSL_SCAN_IS_SCRUB_RESILVER(scn) \
100 ((scn)->scn_phys.scn_func == POOL_SCAN_SCRUB || \
101 (scn)->scn_phys.scn_func == POOL_SCAN_RESILVER)
103 extern int zfs_txg_timeout;
105 /* the order has to match pool_scan_type */
106 static scan_cb_t *scan_funcs[POOL_SCAN_FUNCS] = {
108 dsl_scan_scrub_cb, /* POOL_SCAN_SCRUB */
109 dsl_scan_scrub_cb, /* POOL_SCAN_RESILVER */
113 dsl_scan_init(dsl_pool_t *dp, uint64_t txg)
117 spa_t *spa = dp->dp_spa;
120 scn = dp->dp_scan = kmem_zalloc(sizeof (dsl_scan_t), KM_SLEEP);
124 * It's possible that we're resuming a scan after a reboot so
125 * make sure that the scan_async_destroying flag is initialized
128 ASSERT(!scn->scn_async_destroying);
129 scn->scn_async_destroying = spa_feature_is_active(dp->dp_spa,
130 SPA_FEATURE_ASYNC_DESTROY);
132 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
133 "scrub_func", sizeof (uint64_t), 1, &f);
136 * There was an old-style scrub in progress. Restart a
137 * new-style scrub from the beginning.
139 scn->scn_restart_txg = txg;
140 zfs_dbgmsg("old-style scrub was in progress; "
141 "restarting new-style scrub in txg %llu",
142 scn->scn_restart_txg);
145 * Load the queue obj from the old location so that it
146 * can be freed by dsl_scan_done().
148 (void) zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
149 "scrub_queue", sizeof (uint64_t), 1,
150 &scn->scn_phys.scn_queue_obj);
152 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
153 DMU_POOL_SCAN, sizeof (uint64_t), SCAN_PHYS_NUMINTS,
160 if (scn->scn_phys.scn_state == DSS_SCANNING &&
161 spa_prev_software_version(dp->dp_spa) < SPA_VERSION_SCAN) {
163 * A new-type scrub was in progress on an old
164 * pool, and the pool was accessed by old
165 * software. Restart from the beginning, since
166 * the old software may have changed the pool in
169 scn->scn_restart_txg = txg;
170 zfs_dbgmsg("new-style scrub was modified "
171 "by old software; restarting in txg %llu",
172 scn->scn_restart_txg);
176 spa_scan_stat_init(spa);
181 dsl_scan_fini(dsl_pool_t *dp)
184 kmem_free(dp->dp_scan, sizeof (dsl_scan_t));
191 dsl_scan_setup_check(void *arg, dmu_tx_t *tx)
193 dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
195 if (scn->scn_phys.scn_state == DSS_SCANNING)
196 return (SET_ERROR(EBUSY));
202 dsl_scan_setup_sync(void *arg, dmu_tx_t *tx)
204 dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
205 pool_scan_func_t *funcp = arg;
206 dmu_object_type_t ot = 0;
207 dsl_pool_t *dp = scn->scn_dp;
208 spa_t *spa = dp->dp_spa;
210 ASSERT(scn->scn_phys.scn_state != DSS_SCANNING);
211 ASSERT(*funcp > POOL_SCAN_NONE && *funcp < POOL_SCAN_FUNCS);
212 bzero(&scn->scn_phys, sizeof (scn->scn_phys));
213 scn->scn_phys.scn_func = *funcp;
214 scn->scn_phys.scn_state = DSS_SCANNING;
215 scn->scn_phys.scn_min_txg = 0;
216 scn->scn_phys.scn_max_txg = tx->tx_txg;
217 scn->scn_phys.scn_ddt_class_max = DDT_CLASSES - 1; /* the entire DDT */
218 scn->scn_phys.scn_start_time = gethrestime_sec();
219 scn->scn_phys.scn_errors = 0;
220 scn->scn_phys.scn_to_examine = spa->spa_root_vdev->vdev_stat.vs_alloc;
221 scn->scn_restart_txg = 0;
222 scn->scn_done_txg = 0;
223 spa_scan_stat_init(spa);
225 if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
226 scn->scn_phys.scn_ddt_class_max = zfs_scrub_ddt_class_max;
228 /* rewrite all disk labels */
229 vdev_config_dirty(spa->spa_root_vdev);
231 if (vdev_resilver_needed(spa->spa_root_vdev,
232 &scn->scn_phys.scn_min_txg, &scn->scn_phys.scn_max_txg)) {
233 spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_START);
235 spa_event_notify(spa, NULL, ESC_ZFS_SCRUB_START);
238 spa->spa_scrub_started = B_TRUE;
240 * If this is an incremental scrub, limit the DDT scrub phase
241 * to just the auto-ditto class (for correctness); the rest
242 * of the scrub should go faster using top-down pruning.
244 if (scn->scn_phys.scn_min_txg > TXG_INITIAL)
245 scn->scn_phys.scn_ddt_class_max = DDT_CLASS_DITTO;
249 /* back to the generic stuff */
251 if (dp->dp_blkstats == NULL) {
253 kmem_alloc(sizeof (zfs_all_blkstats_t), KM_SLEEP);
255 bzero(dp->dp_blkstats, sizeof (zfs_all_blkstats_t));
257 if (spa_version(spa) < SPA_VERSION_DSL_SCRUB)
258 ot = DMU_OT_ZAP_OTHER;
260 scn->scn_phys.scn_queue_obj = zap_create(dp->dp_meta_objset,
261 ot ? ot : DMU_OT_SCAN_QUEUE, DMU_OT_NONE, 0, tx);
263 dsl_scan_sync_state(scn, tx);
265 spa_history_log_internal(spa, "scan setup", tx,
266 "func=%u mintxg=%llu maxtxg=%llu",
267 *funcp, scn->scn_phys.scn_min_txg, scn->scn_phys.scn_max_txg);
272 dsl_scan_done(dsl_scan_t *scn, boolean_t complete, dmu_tx_t *tx)
274 static const char *old_names[] = {
276 "scrub_ddt_bookmark",
277 "scrub_ddt_class_max",
286 dsl_pool_t *dp = scn->scn_dp;
287 spa_t *spa = dp->dp_spa;
290 /* Remove any remnants of an old-style scrub. */
291 for (i = 0; old_names[i]; i++) {
292 (void) zap_remove(dp->dp_meta_objset,
293 DMU_POOL_DIRECTORY_OBJECT, old_names[i], tx);
296 if (scn->scn_phys.scn_queue_obj != 0) {
297 VERIFY(0 == dmu_object_free(dp->dp_meta_objset,
298 scn->scn_phys.scn_queue_obj, tx));
299 scn->scn_phys.scn_queue_obj = 0;
303 * If we were "restarted" from a stopped state, don't bother
304 * with anything else.
306 if (scn->scn_phys.scn_state != DSS_SCANNING)
310 scn->scn_phys.scn_state = DSS_FINISHED;
312 scn->scn_phys.scn_state = DSS_CANCELED;
314 spa_history_log_internal(spa, "scan done", tx,
315 "complete=%u", complete);
317 if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
318 mutex_enter(&spa->spa_scrub_lock);
319 while (spa->spa_scrub_inflight > 0) {
320 cv_wait(&spa->spa_scrub_io_cv,
321 &spa->spa_scrub_lock);
323 mutex_exit(&spa->spa_scrub_lock);
324 spa->spa_scrub_started = B_FALSE;
325 spa->spa_scrub_active = B_FALSE;
328 * If the scrub/resilver completed, update all DTLs to
329 * reflect this. Whether it succeeded or not, vacate
330 * all temporary scrub DTLs.
332 vdev_dtl_reassess(spa->spa_root_vdev, tx->tx_txg,
333 complete ? scn->scn_phys.scn_max_txg : 0, B_TRUE);
335 spa_event_notify(spa, NULL, scn->scn_phys.scn_min_txg ?
336 ESC_ZFS_RESILVER_FINISH : ESC_ZFS_SCRUB_FINISH);
338 spa_errlog_rotate(spa);
341 * We may have finished replacing a device.
342 * Let the async thread assess this and handle the detach.
344 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
347 scn->scn_phys.scn_end_time = gethrestime_sec();
352 dsl_scan_cancel_check(void *arg, dmu_tx_t *tx)
354 dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
356 if (scn->scn_phys.scn_state != DSS_SCANNING)
357 return (SET_ERROR(ENOENT));
363 dsl_scan_cancel_sync(void *arg, dmu_tx_t *tx)
365 dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
367 dsl_scan_done(scn, B_FALSE, tx);
368 dsl_scan_sync_state(scn, tx);
372 dsl_scan_cancel(dsl_pool_t *dp)
374 return (dsl_sync_task(spa_name(dp->dp_spa), dsl_scan_cancel_check,
375 dsl_scan_cancel_sync, NULL, 3, ZFS_SPACE_CHECK_RESERVED));
378 static void dsl_scan_visitbp(blkptr_t *bp,
379 const zbookmark_phys_t *zb, dnode_phys_t *dnp, arc_buf_t *pbuf,
380 dsl_dataset_t *ds, dsl_scan_t *scn, dmu_objset_type_t ostype,
382 static void dsl_scan_visitdnode(dsl_scan_t *, dsl_dataset_t *ds,
383 dmu_objset_type_t ostype,
384 dnode_phys_t *dnp, arc_buf_t *buf, uint64_t object, dmu_tx_t *tx);
387 dsl_free(dsl_pool_t *dp, uint64_t txg, const blkptr_t *bp)
389 zio_free(dp->dp_spa, txg, bp);
393 dsl_free_sync(zio_t *pio, dsl_pool_t *dp, uint64_t txg, const blkptr_t *bpp)
395 ASSERT(dsl_pool_sync_context(dp));
396 zio_nowait(zio_free_sync(pio, dp->dp_spa, txg, bpp, BP_GET_PSIZE(bpp),
401 dsl_scan_ds_maxtxg(dsl_dataset_t *ds)
403 uint64_t smt = ds->ds_dir->dd_pool->dp_scan->scn_phys.scn_max_txg;
404 if (dsl_dataset_is_snapshot(ds))
405 return (MIN(smt, ds->ds_phys->ds_creation_txg));
410 dsl_scan_sync_state(dsl_scan_t *scn, dmu_tx_t *tx)
412 VERIFY0(zap_update(scn->scn_dp->dp_meta_objset,
413 DMU_POOL_DIRECTORY_OBJECT,
414 DMU_POOL_SCAN, sizeof (uint64_t), SCAN_PHYS_NUMINTS,
415 &scn->scn_phys, tx));
419 dsl_scan_check_pause(dsl_scan_t *scn, const zbookmark_phys_t *zb)
421 uint64_t elapsed_nanosecs;
422 unsigned int mintime;
424 /* we never skip user/group accounting objects */
425 if (zb && (int64_t)zb->zb_object < 0)
428 if (scn->scn_pausing)
429 return (B_TRUE); /* we're already pausing */
431 if (!ZB_IS_ZERO(&scn->scn_phys.scn_bookmark))
432 return (B_FALSE); /* we're resuming */
434 /* We only know how to resume from level-0 blocks. */
435 if (zb && zb->zb_level != 0)
438 mintime = (scn->scn_phys.scn_func == POOL_SCAN_RESILVER) ?
439 zfs_resilver_min_time_ms : zfs_scan_min_time_ms;
440 elapsed_nanosecs = gethrtime() - scn->scn_sync_start_time;
441 if (elapsed_nanosecs / NANOSEC > zfs_txg_timeout ||
442 (NSEC2MSEC(elapsed_nanosecs) > mintime &&
443 txg_sync_waiting(scn->scn_dp)) ||
444 spa_shutting_down(scn->scn_dp->dp_spa)) {
446 dprintf("pausing at bookmark %llx/%llx/%llx/%llx\n",
447 (longlong_t)zb->zb_objset,
448 (longlong_t)zb->zb_object,
449 (longlong_t)zb->zb_level,
450 (longlong_t)zb->zb_blkid);
451 scn->scn_phys.scn_bookmark = *zb;
453 dprintf("pausing at DDT bookmark %llx/%llx/%llx/%llx\n",
454 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_class,
455 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_type,
456 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_checksum,
457 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_cursor);
458 scn->scn_pausing = B_TRUE;
464 typedef struct zil_scan_arg {
466 zil_header_t *zsa_zh;
471 dsl_scan_zil_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
473 zil_scan_arg_t *zsa = arg;
474 dsl_pool_t *dp = zsa->zsa_dp;
475 dsl_scan_t *scn = dp->dp_scan;
476 zil_header_t *zh = zsa->zsa_zh;
479 if (BP_IS_HOLE(bp) || bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
483 * One block ("stubby") can be allocated a long time ago; we
484 * want to visit that one because it has been allocated
485 * (on-disk) even if it hasn't been claimed (even though for
486 * scrub there's nothing to do to it).
488 if (claim_txg == 0 && bp->blk_birth >= spa_first_txg(dp->dp_spa))
491 SET_BOOKMARK(&zb, zh->zh_log.blk_cksum.zc_word[ZIL_ZC_OBJSET],
492 ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, bp->blk_cksum.zc_word[ZIL_ZC_SEQ]);
494 VERIFY(0 == scan_funcs[scn->scn_phys.scn_func](dp, bp, &zb));
500 dsl_scan_zil_record(zilog_t *zilog, lr_t *lrc, void *arg, uint64_t claim_txg)
502 if (lrc->lrc_txtype == TX_WRITE) {
503 zil_scan_arg_t *zsa = arg;
504 dsl_pool_t *dp = zsa->zsa_dp;
505 dsl_scan_t *scn = dp->dp_scan;
506 zil_header_t *zh = zsa->zsa_zh;
507 lr_write_t *lr = (lr_write_t *)lrc;
508 blkptr_t *bp = &lr->lr_blkptr;
511 if (BP_IS_HOLE(bp) ||
512 bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
516 * birth can be < claim_txg if this record's txg is
517 * already txg sync'ed (but this log block contains
518 * other records that are not synced)
520 if (claim_txg == 0 || bp->blk_birth < claim_txg)
523 SET_BOOKMARK(&zb, zh->zh_log.blk_cksum.zc_word[ZIL_ZC_OBJSET],
524 lr->lr_foid, ZB_ZIL_LEVEL,
525 lr->lr_offset / BP_GET_LSIZE(bp));
527 VERIFY(0 == scan_funcs[scn->scn_phys.scn_func](dp, bp, &zb));
533 dsl_scan_zil(dsl_pool_t *dp, zil_header_t *zh)
535 uint64_t claim_txg = zh->zh_claim_txg;
536 zil_scan_arg_t zsa = { dp, zh };
540 * We only want to visit blocks that have been claimed but not yet
541 * replayed (or, in read-only mode, blocks that *would* be claimed).
543 if (claim_txg == 0 && spa_writeable(dp->dp_spa))
546 zilog = zil_alloc(dp->dp_meta_objset, zh);
548 (void) zil_parse(zilog, dsl_scan_zil_block, dsl_scan_zil_record, &zsa,
556 dsl_scan_prefetch(dsl_scan_t *scn, arc_buf_t *buf, blkptr_t *bp,
557 uint64_t objset, uint64_t object, uint64_t blkid)
559 zbookmark_phys_t czb;
560 uint32_t flags = ARC_NOWAIT | ARC_PREFETCH;
562 if (zfs_no_scrub_prefetch)
565 if (BP_IS_HOLE(bp) || bp->blk_birth <= scn->scn_phys.scn_min_txg ||
566 (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_DNODE))
569 SET_BOOKMARK(&czb, objset, object, BP_GET_LEVEL(bp), blkid);
571 (void) arc_read(scn->scn_zio_root, scn->scn_dp->dp_spa, bp,
572 NULL, NULL, ZIO_PRIORITY_ASYNC_READ,
573 ZIO_FLAG_CANFAIL | ZIO_FLAG_SCAN_THREAD, &flags, &czb);
577 dsl_scan_check_resume(dsl_scan_t *scn, const dnode_phys_t *dnp,
578 const zbookmark_phys_t *zb)
581 * We never skip over user/group accounting objects (obj<0)
583 if (!ZB_IS_ZERO(&scn->scn_phys.scn_bookmark) &&
584 (int64_t)zb->zb_object >= 0) {
586 * If we already visited this bp & everything below (in
587 * a prior txg sync), don't bother doing it again.
589 if (zbookmark_is_before(dnp, zb, &scn->scn_phys.scn_bookmark))
593 * If we found the block we're trying to resume from, or
594 * we went past it to a different object, zero it out to
595 * indicate that it's OK to start checking for pausing
598 if (bcmp(zb, &scn->scn_phys.scn_bookmark, sizeof (*zb)) == 0 ||
599 zb->zb_object > scn->scn_phys.scn_bookmark.zb_object) {
600 dprintf("resuming at %llx/%llx/%llx/%llx\n",
601 (longlong_t)zb->zb_objset,
602 (longlong_t)zb->zb_object,
603 (longlong_t)zb->zb_level,
604 (longlong_t)zb->zb_blkid);
605 bzero(&scn->scn_phys.scn_bookmark, sizeof (*zb));
612 * Return nonzero on i/o error.
613 * Return new buf to write out in *bufp.
616 dsl_scan_recurse(dsl_scan_t *scn, dsl_dataset_t *ds, dmu_objset_type_t ostype,
617 dnode_phys_t *dnp, const blkptr_t *bp,
618 const zbookmark_phys_t *zb, dmu_tx_t *tx, arc_buf_t **bufp)
620 dsl_pool_t *dp = scn->scn_dp;
621 int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCAN_THREAD;
624 if (BP_GET_LEVEL(bp) > 0) {
625 uint32_t flags = ARC_WAIT;
628 int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
630 err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, bufp,
631 ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
633 scn->scn_phys.scn_errors++;
636 for (i = 0, cbp = (*bufp)->b_data; i < epb; i++, cbp++) {
637 dsl_scan_prefetch(scn, *bufp, cbp, zb->zb_objset,
638 zb->zb_object, zb->zb_blkid * epb + i);
640 for (i = 0, cbp = (*bufp)->b_data; i < epb; i++, cbp++) {
641 zbookmark_phys_t czb;
643 SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
645 zb->zb_blkid * epb + i);
646 dsl_scan_visitbp(cbp, &czb, dnp,
647 *bufp, ds, scn, ostype, tx);
649 } else if (BP_GET_TYPE(bp) == DMU_OT_USERGROUP_USED) {
650 uint32_t flags = ARC_WAIT;
652 err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, bufp,
653 ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
655 scn->scn_phys.scn_errors++;
658 } else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) {
659 uint32_t flags = ARC_WAIT;
662 int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
664 err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, bufp,
665 ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
667 scn->scn_phys.scn_errors++;
670 for (i = 0, cdnp = (*bufp)->b_data; i < epb; i++, cdnp++) {
671 for (j = 0; j < cdnp->dn_nblkptr; j++) {
672 blkptr_t *cbp = &cdnp->dn_blkptr[j];
673 dsl_scan_prefetch(scn, *bufp, cbp,
674 zb->zb_objset, zb->zb_blkid * epb + i, j);
677 for (i = 0, cdnp = (*bufp)->b_data; i < epb; i++, cdnp++) {
678 dsl_scan_visitdnode(scn, ds, ostype,
679 cdnp, *bufp, zb->zb_blkid * epb + i, tx);
682 } else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) {
683 uint32_t flags = ARC_WAIT;
686 err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, bufp,
687 ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
689 scn->scn_phys.scn_errors++;
693 osp = (*bufp)->b_data;
695 dsl_scan_visitdnode(scn, ds, osp->os_type,
696 &osp->os_meta_dnode, *bufp, DMU_META_DNODE_OBJECT, tx);
698 if (OBJSET_BUF_HAS_USERUSED(*bufp)) {
700 * We also always visit user/group accounting
701 * objects, and never skip them, even if we are
702 * pausing. This is necessary so that the space
703 * deltas from this txg get integrated.
705 dsl_scan_visitdnode(scn, ds, osp->os_type,
706 &osp->os_groupused_dnode, *bufp,
707 DMU_GROUPUSED_OBJECT, tx);
708 dsl_scan_visitdnode(scn, ds, osp->os_type,
709 &osp->os_userused_dnode, *bufp,
710 DMU_USERUSED_OBJECT, tx);
718 dsl_scan_visitdnode(dsl_scan_t *scn, dsl_dataset_t *ds,
719 dmu_objset_type_t ostype, dnode_phys_t *dnp, arc_buf_t *buf,
720 uint64_t object, dmu_tx_t *tx)
724 for (j = 0; j < dnp->dn_nblkptr; j++) {
725 zbookmark_phys_t czb;
727 SET_BOOKMARK(&czb, ds ? ds->ds_object : 0, object,
728 dnp->dn_nlevels - 1, j);
729 dsl_scan_visitbp(&dnp->dn_blkptr[j],
730 &czb, dnp, buf, ds, scn, ostype, tx);
733 if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
734 zbookmark_phys_t czb;
735 SET_BOOKMARK(&czb, ds ? ds->ds_object : 0, object,
737 dsl_scan_visitbp(&dnp->dn_spill,
738 &czb, dnp, buf, ds, scn, ostype, tx);
743 * The arguments are in this order because mdb can only print the
744 * first 5; we want them to be useful.
747 dsl_scan_visitbp(blkptr_t *bp, const zbookmark_phys_t *zb,
748 dnode_phys_t *dnp, arc_buf_t *pbuf,
749 dsl_dataset_t *ds, dsl_scan_t *scn, dmu_objset_type_t ostype,
752 dsl_pool_t *dp = scn->scn_dp;
753 arc_buf_t *buf = NULL;
754 blkptr_t bp_toread = *bp;
756 /* ASSERT(pbuf == NULL || arc_released(pbuf)); */
758 if (dsl_scan_check_pause(scn, zb))
761 if (dsl_scan_check_resume(scn, dnp, zb))
767 scn->scn_visited_this_txg++;
770 "visiting ds=%p/%llu zb=%llx/%llx/%llx/%llx buf=%p bp=%p",
771 ds, ds ? ds->ds_object : 0,
772 zb->zb_objset, zb->zb_object, zb->zb_level, zb->zb_blkid,
775 if (bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
778 if (dsl_scan_recurse(scn, ds, ostype, dnp, &bp_toread, zb, tx,
783 * If dsl_scan_ddt() has aready visited this block, it will have
784 * already done any translations or scrubbing, so don't call the
787 if (ddt_class_contains(dp->dp_spa,
788 scn->scn_phys.scn_ddt_class_max, bp)) {
794 * If this block is from the future (after cur_max_txg), then we
795 * are doing this on behalf of a deleted snapshot, and we will
796 * revisit the future block on the next pass of this dataset.
797 * Don't scan it now unless we need to because something
798 * under it was modified.
800 if (BP_PHYSICAL_BIRTH(bp) <= scn->scn_phys.scn_cur_max_txg) {
801 scan_funcs[scn->scn_phys.scn_func](dp, bp, zb);
804 (void) arc_buf_remove_ref(buf, &buf);
808 dsl_scan_visit_rootbp(dsl_scan_t *scn, dsl_dataset_t *ds, blkptr_t *bp,
813 SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
814 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
815 dsl_scan_visitbp(bp, &zb, NULL, NULL,
816 ds, scn, DMU_OST_NONE, tx);
818 dprintf_ds(ds, "finished scan%s", "");
822 dsl_scan_ds_destroyed(dsl_dataset_t *ds, dmu_tx_t *tx)
824 dsl_pool_t *dp = ds->ds_dir->dd_pool;
825 dsl_scan_t *scn = dp->dp_scan;
828 if (scn->scn_phys.scn_state != DSS_SCANNING)
831 if (scn->scn_phys.scn_bookmark.zb_objset == ds->ds_object) {
832 if (dsl_dataset_is_snapshot(ds)) {
833 /* Note, scn_cur_{min,max}_txg stays the same. */
834 scn->scn_phys.scn_bookmark.zb_objset =
835 ds->ds_phys->ds_next_snap_obj;
836 zfs_dbgmsg("destroying ds %llu; currently traversing; "
837 "reset zb_objset to %llu",
838 (u_longlong_t)ds->ds_object,
839 (u_longlong_t)ds->ds_phys->ds_next_snap_obj);
840 scn->scn_phys.scn_flags |= DSF_VISIT_DS_AGAIN;
842 SET_BOOKMARK(&scn->scn_phys.scn_bookmark,
843 ZB_DESTROYED_OBJSET, 0, 0, 0);
844 zfs_dbgmsg("destroying ds %llu; currently traversing; "
845 "reset bookmark to -1,0,0,0",
846 (u_longlong_t)ds->ds_object);
848 } else if (zap_lookup_int_key(dp->dp_meta_objset,
849 scn->scn_phys.scn_queue_obj, ds->ds_object, &mintxg) == 0) {
850 ASSERT3U(ds->ds_phys->ds_num_children, <=, 1);
851 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
852 scn->scn_phys.scn_queue_obj, ds->ds_object, tx));
853 if (dsl_dataset_is_snapshot(ds)) {
855 * We keep the same mintxg; it could be >
856 * ds_creation_txg if the previous snapshot was
859 VERIFY(zap_add_int_key(dp->dp_meta_objset,
860 scn->scn_phys.scn_queue_obj,
861 ds->ds_phys->ds_next_snap_obj, mintxg, tx) == 0);
862 zfs_dbgmsg("destroying ds %llu; in queue; "
863 "replacing with %llu",
864 (u_longlong_t)ds->ds_object,
865 (u_longlong_t)ds->ds_phys->ds_next_snap_obj);
867 zfs_dbgmsg("destroying ds %llu; in queue; removing",
868 (u_longlong_t)ds->ds_object);
871 zfs_dbgmsg("destroying ds %llu; ignoring",
872 (u_longlong_t)ds->ds_object);
876 * dsl_scan_sync() should be called after this, and should sync
877 * out our changed state, but just to be safe, do it here.
879 dsl_scan_sync_state(scn, tx);
883 dsl_scan_ds_snapshotted(dsl_dataset_t *ds, dmu_tx_t *tx)
885 dsl_pool_t *dp = ds->ds_dir->dd_pool;
886 dsl_scan_t *scn = dp->dp_scan;
889 if (scn->scn_phys.scn_state != DSS_SCANNING)
892 ASSERT(ds->ds_phys->ds_prev_snap_obj != 0);
894 if (scn->scn_phys.scn_bookmark.zb_objset == ds->ds_object) {
895 scn->scn_phys.scn_bookmark.zb_objset =
896 ds->ds_phys->ds_prev_snap_obj;
897 zfs_dbgmsg("snapshotting ds %llu; currently traversing; "
898 "reset zb_objset to %llu",
899 (u_longlong_t)ds->ds_object,
900 (u_longlong_t)ds->ds_phys->ds_prev_snap_obj);
901 } else if (zap_lookup_int_key(dp->dp_meta_objset,
902 scn->scn_phys.scn_queue_obj, ds->ds_object, &mintxg) == 0) {
903 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
904 scn->scn_phys.scn_queue_obj, ds->ds_object, tx));
905 VERIFY(zap_add_int_key(dp->dp_meta_objset,
906 scn->scn_phys.scn_queue_obj,
907 ds->ds_phys->ds_prev_snap_obj, mintxg, tx) == 0);
908 zfs_dbgmsg("snapshotting ds %llu; in queue; "
909 "replacing with %llu",
910 (u_longlong_t)ds->ds_object,
911 (u_longlong_t)ds->ds_phys->ds_prev_snap_obj);
913 dsl_scan_sync_state(scn, tx);
917 dsl_scan_ds_clone_swapped(dsl_dataset_t *ds1, dsl_dataset_t *ds2, dmu_tx_t *tx)
919 dsl_pool_t *dp = ds1->ds_dir->dd_pool;
920 dsl_scan_t *scn = dp->dp_scan;
923 if (scn->scn_phys.scn_state != DSS_SCANNING)
926 if (scn->scn_phys.scn_bookmark.zb_objset == ds1->ds_object) {
927 scn->scn_phys.scn_bookmark.zb_objset = ds2->ds_object;
928 zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
929 "reset zb_objset to %llu",
930 (u_longlong_t)ds1->ds_object,
931 (u_longlong_t)ds2->ds_object);
932 } else if (scn->scn_phys.scn_bookmark.zb_objset == ds2->ds_object) {
933 scn->scn_phys.scn_bookmark.zb_objset = ds1->ds_object;
934 zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
935 "reset zb_objset to %llu",
936 (u_longlong_t)ds2->ds_object,
937 (u_longlong_t)ds1->ds_object);
940 if (zap_lookup_int_key(dp->dp_meta_objset, scn->scn_phys.scn_queue_obj,
941 ds1->ds_object, &mintxg) == 0) {
944 ASSERT3U(mintxg, ==, ds1->ds_phys->ds_prev_snap_txg);
945 ASSERT3U(mintxg, ==, ds2->ds_phys->ds_prev_snap_txg);
946 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
947 scn->scn_phys.scn_queue_obj, ds1->ds_object, tx));
948 err = zap_add_int_key(dp->dp_meta_objset,
949 scn->scn_phys.scn_queue_obj, ds2->ds_object, mintxg, tx);
950 VERIFY(err == 0 || err == EEXIST);
952 /* Both were there to begin with */
953 VERIFY(0 == zap_add_int_key(dp->dp_meta_objset,
954 scn->scn_phys.scn_queue_obj,
955 ds1->ds_object, mintxg, tx));
957 zfs_dbgmsg("clone_swap ds %llu; in queue; "
958 "replacing with %llu",
959 (u_longlong_t)ds1->ds_object,
960 (u_longlong_t)ds2->ds_object);
961 } else if (zap_lookup_int_key(dp->dp_meta_objset,
962 scn->scn_phys.scn_queue_obj, ds2->ds_object, &mintxg) == 0) {
963 ASSERT3U(mintxg, ==, ds1->ds_phys->ds_prev_snap_txg);
964 ASSERT3U(mintxg, ==, ds2->ds_phys->ds_prev_snap_txg);
965 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
966 scn->scn_phys.scn_queue_obj, ds2->ds_object, tx));
967 VERIFY(0 == zap_add_int_key(dp->dp_meta_objset,
968 scn->scn_phys.scn_queue_obj, ds1->ds_object, mintxg, tx));
969 zfs_dbgmsg("clone_swap ds %llu; in queue; "
970 "replacing with %llu",
971 (u_longlong_t)ds2->ds_object,
972 (u_longlong_t)ds1->ds_object);
975 dsl_scan_sync_state(scn, tx);
978 struct enqueue_clones_arg {
985 enqueue_clones_cb(dsl_pool_t *dp, dsl_dataset_t *hds, void *arg)
987 struct enqueue_clones_arg *eca = arg;
990 dsl_scan_t *scn = dp->dp_scan;
992 if (hds->ds_dir->dd_phys->dd_origin_obj != eca->originobj)
995 err = dsl_dataset_hold_obj(dp, hds->ds_object, FTAG, &ds);
999 while (ds->ds_phys->ds_prev_snap_obj != eca->originobj) {
1000 dsl_dataset_t *prev;
1001 err = dsl_dataset_hold_obj(dp,
1002 ds->ds_phys->ds_prev_snap_obj, FTAG, &prev);
1004 dsl_dataset_rele(ds, FTAG);
1009 VERIFY(zap_add_int_key(dp->dp_meta_objset,
1010 scn->scn_phys.scn_queue_obj, ds->ds_object,
1011 ds->ds_phys->ds_prev_snap_txg, eca->tx) == 0);
1012 dsl_dataset_rele(ds, FTAG);
1017 dsl_scan_visitds(dsl_scan_t *scn, uint64_t dsobj, dmu_tx_t *tx)
1019 dsl_pool_t *dp = scn->scn_dp;
1023 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
1025 if (dmu_objset_from_ds(ds, &os))
1029 * Only the ZIL in the head (non-snapshot) is valid. Even though
1030 * snapshots can have ZIL block pointers (which may be the same
1031 * BP as in the head), they must be ignored. So we traverse the
1032 * ZIL here, rather than in scan_recurse(), because the regular
1033 * snapshot block-sharing rules don't apply to it.
1035 if (DSL_SCAN_IS_SCRUB_RESILVER(scn) && !dsl_dataset_is_snapshot(ds))
1036 dsl_scan_zil(dp, &os->os_zil_header);
1039 * Iterate over the bps in this ds.
1041 dmu_buf_will_dirty(ds->ds_dbuf, tx);
1042 dsl_scan_visit_rootbp(scn, ds, &ds->ds_phys->ds_bp, tx);
1044 char *dsname = kmem_alloc(ZFS_MAXNAMELEN, KM_SLEEP);
1045 dsl_dataset_name(ds, dsname);
1046 zfs_dbgmsg("scanned dataset %llu (%s) with min=%llu max=%llu; "
1048 (longlong_t)dsobj, dsname,
1049 (longlong_t)scn->scn_phys.scn_cur_min_txg,
1050 (longlong_t)scn->scn_phys.scn_cur_max_txg,
1051 (int)scn->scn_pausing);
1052 kmem_free(dsname, ZFS_MAXNAMELEN);
1054 if (scn->scn_pausing)
1058 * We've finished this pass over this dataset.
1062 * If we did not completely visit this dataset, do another pass.
1064 if (scn->scn_phys.scn_flags & DSF_VISIT_DS_AGAIN) {
1065 zfs_dbgmsg("incomplete pass; visiting again");
1066 scn->scn_phys.scn_flags &= ~DSF_VISIT_DS_AGAIN;
1067 VERIFY(zap_add_int_key(dp->dp_meta_objset,
1068 scn->scn_phys.scn_queue_obj, ds->ds_object,
1069 scn->scn_phys.scn_cur_max_txg, tx) == 0);
1074 * Add descendent datasets to work queue.
1076 if (ds->ds_phys->ds_next_snap_obj != 0) {
1077 VERIFY(zap_add_int_key(dp->dp_meta_objset,
1078 scn->scn_phys.scn_queue_obj, ds->ds_phys->ds_next_snap_obj,
1079 ds->ds_phys->ds_creation_txg, tx) == 0);
1081 if (ds->ds_phys->ds_num_children > 1) {
1082 boolean_t usenext = B_FALSE;
1083 if (ds->ds_phys->ds_next_clones_obj != 0) {
1086 * A bug in a previous version of the code could
1087 * cause upgrade_clones_cb() to not set
1088 * ds_next_snap_obj when it should, leading to a
1089 * missing entry. Therefore we can only use the
1090 * next_clones_obj when its count is correct.
1092 int err = zap_count(dp->dp_meta_objset,
1093 ds->ds_phys->ds_next_clones_obj, &count);
1095 count == ds->ds_phys->ds_num_children - 1)
1100 VERIFY0(zap_join_key(dp->dp_meta_objset,
1101 ds->ds_phys->ds_next_clones_obj,
1102 scn->scn_phys.scn_queue_obj,
1103 ds->ds_phys->ds_creation_txg, tx));
1105 struct enqueue_clones_arg eca;
1107 eca.originobj = ds->ds_object;
1109 VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
1110 enqueue_clones_cb, &eca, DS_FIND_CHILDREN));
1115 dsl_dataset_rele(ds, FTAG);
1120 enqueue_cb(dsl_pool_t *dp, dsl_dataset_t *hds, void *arg)
1125 dsl_scan_t *scn = dp->dp_scan;
1127 err = dsl_dataset_hold_obj(dp, hds->ds_object, FTAG, &ds);
1131 while (ds->ds_phys->ds_prev_snap_obj != 0) {
1132 dsl_dataset_t *prev;
1133 err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
1136 dsl_dataset_rele(ds, FTAG);
1141 * If this is a clone, we don't need to worry about it for now.
1143 if (prev->ds_phys->ds_next_snap_obj != ds->ds_object) {
1144 dsl_dataset_rele(ds, FTAG);
1145 dsl_dataset_rele(prev, FTAG);
1148 dsl_dataset_rele(ds, FTAG);
1152 VERIFY(zap_add_int_key(dp->dp_meta_objset, scn->scn_phys.scn_queue_obj,
1153 ds->ds_object, ds->ds_phys->ds_prev_snap_txg, tx) == 0);
1154 dsl_dataset_rele(ds, FTAG);
1159 * Scrub/dedup interaction.
1161 * If there are N references to a deduped block, we don't want to scrub it
1162 * N times -- ideally, we should scrub it exactly once.
1164 * We leverage the fact that the dde's replication class (enum ddt_class)
1165 * is ordered from highest replication class (DDT_CLASS_DITTO) to lowest
1166 * (DDT_CLASS_UNIQUE) so that we may walk the DDT in that order.
1168 * To prevent excess scrubbing, the scrub begins by walking the DDT
1169 * to find all blocks with refcnt > 1, and scrubs each of these once.
1170 * Since there are two replication classes which contain blocks with
1171 * refcnt > 1, we scrub the highest replication class (DDT_CLASS_DITTO) first.
1172 * Finally the top-down scrub begins, only visiting blocks with refcnt == 1.
1174 * There would be nothing more to say if a block's refcnt couldn't change
1175 * during a scrub, but of course it can so we must account for changes
1176 * in a block's replication class.
1178 * Here's an example of what can occur:
1180 * If a block has refcnt > 1 during the DDT scrub phase, but has refcnt == 1
1181 * when visited during the top-down scrub phase, it will be scrubbed twice.
1182 * This negates our scrub optimization, but is otherwise harmless.
1184 * If a block has refcnt == 1 during the DDT scrub phase, but has refcnt > 1
1185 * on each visit during the top-down scrub phase, it will never be scrubbed.
1186 * To catch this, ddt_sync_entry() notifies the scrub code whenever a block's
1187 * reference class transitions to a higher level (i.e DDT_CLASS_UNIQUE to
1188 * DDT_CLASS_DUPLICATE); if it transitions from refcnt == 1 to refcnt > 1
1189 * while a scrub is in progress, it scrubs the block right then.
1192 dsl_scan_ddt(dsl_scan_t *scn, dmu_tx_t *tx)
1194 ddt_bookmark_t *ddb = &scn->scn_phys.scn_ddt_bookmark;
1195 ddt_entry_t dde = { 0 };
1199 while ((error = ddt_walk(scn->scn_dp->dp_spa, ddb, &dde)) == 0) {
1202 if (ddb->ddb_class > scn->scn_phys.scn_ddt_class_max)
1204 dprintf("visiting ddb=%llu/%llu/%llu/%llx\n",
1205 (longlong_t)ddb->ddb_class,
1206 (longlong_t)ddb->ddb_type,
1207 (longlong_t)ddb->ddb_checksum,
1208 (longlong_t)ddb->ddb_cursor);
1210 /* There should be no pending changes to the dedup table */
1211 ddt = scn->scn_dp->dp_spa->spa_ddt[ddb->ddb_checksum];
1212 ASSERT(avl_first(&ddt->ddt_tree) == NULL);
1214 dsl_scan_ddt_entry(scn, ddb->ddb_checksum, &dde, tx);
1217 if (dsl_scan_check_pause(scn, NULL))
1221 zfs_dbgmsg("scanned %llu ddt entries with class_max = %u; pausing=%u",
1222 (longlong_t)n, (int)scn->scn_phys.scn_ddt_class_max,
1223 (int)scn->scn_pausing);
1225 ASSERT(error == 0 || error == ENOENT);
1226 ASSERT(error != ENOENT ||
1227 ddb->ddb_class > scn->scn_phys.scn_ddt_class_max);
1232 dsl_scan_ddt_entry(dsl_scan_t *scn, enum zio_checksum checksum,
1233 ddt_entry_t *dde, dmu_tx_t *tx)
1235 const ddt_key_t *ddk = &dde->dde_key;
1236 ddt_phys_t *ddp = dde->dde_phys;
1238 zbookmark_phys_t zb = { 0 };
1240 if (scn->scn_phys.scn_state != DSS_SCANNING)
1243 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
1244 if (ddp->ddp_phys_birth == 0 ||
1245 ddp->ddp_phys_birth > scn->scn_phys.scn_max_txg)
1247 ddt_bp_create(checksum, ddk, ddp, &bp);
1249 scn->scn_visited_this_txg++;
1250 scan_funcs[scn->scn_phys.scn_func](scn->scn_dp, &bp, &zb);
1255 dsl_scan_visit(dsl_scan_t *scn, dmu_tx_t *tx)
1257 dsl_pool_t *dp = scn->scn_dp;
1261 if (scn->scn_phys.scn_ddt_bookmark.ddb_class <=
1262 scn->scn_phys.scn_ddt_class_max) {
1263 scn->scn_phys.scn_cur_min_txg = scn->scn_phys.scn_min_txg;
1264 scn->scn_phys.scn_cur_max_txg = scn->scn_phys.scn_max_txg;
1265 dsl_scan_ddt(scn, tx);
1266 if (scn->scn_pausing)
1270 if (scn->scn_phys.scn_bookmark.zb_objset == DMU_META_OBJSET) {
1271 /* First do the MOS & ORIGIN */
1273 scn->scn_phys.scn_cur_min_txg = scn->scn_phys.scn_min_txg;
1274 scn->scn_phys.scn_cur_max_txg = scn->scn_phys.scn_max_txg;
1275 dsl_scan_visit_rootbp(scn, NULL,
1276 &dp->dp_meta_rootbp, tx);
1277 spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
1278 if (scn->scn_pausing)
1281 if (spa_version(dp->dp_spa) < SPA_VERSION_DSL_SCRUB) {
1282 VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
1283 enqueue_cb, tx, DS_FIND_CHILDREN));
1285 dsl_scan_visitds(scn,
1286 dp->dp_origin_snap->ds_object, tx);
1288 ASSERT(!scn->scn_pausing);
1289 } else if (scn->scn_phys.scn_bookmark.zb_objset !=
1290 ZB_DESTROYED_OBJSET) {
1292 * If we were paused, continue from here. Note if the
1293 * ds we were paused on was deleted, the zb_objset may
1294 * be -1, so we will skip this and find a new objset
1297 dsl_scan_visitds(scn, scn->scn_phys.scn_bookmark.zb_objset, tx);
1298 if (scn->scn_pausing)
1303 * In case we were paused right at the end of the ds, zero the
1304 * bookmark so we don't think that we're still trying to resume.
1306 bzero(&scn->scn_phys.scn_bookmark, sizeof (zbookmark_phys_t));
1308 /* keep pulling things out of the zap-object-as-queue */
1309 while (zap_cursor_init(&zc, dp->dp_meta_objset,
1310 scn->scn_phys.scn_queue_obj),
1311 zap_cursor_retrieve(&zc, &za) == 0) {
1315 dsobj = strtonum(za.za_name, NULL);
1316 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
1317 scn->scn_phys.scn_queue_obj, dsobj, tx));
1319 /* Set up min/max txg */
1320 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
1321 if (za.za_first_integer != 0) {
1322 scn->scn_phys.scn_cur_min_txg =
1323 MAX(scn->scn_phys.scn_min_txg,
1324 za.za_first_integer);
1326 scn->scn_phys.scn_cur_min_txg =
1327 MAX(scn->scn_phys.scn_min_txg,
1328 ds->ds_phys->ds_prev_snap_txg);
1330 scn->scn_phys.scn_cur_max_txg = dsl_scan_ds_maxtxg(ds);
1331 dsl_dataset_rele(ds, FTAG);
1333 dsl_scan_visitds(scn, dsobj, tx);
1334 zap_cursor_fini(&zc);
1335 if (scn->scn_pausing)
1338 zap_cursor_fini(&zc);
1342 dsl_scan_free_should_pause(dsl_scan_t *scn)
1344 uint64_t elapsed_nanosecs;
1349 if (scn->scn_visited_this_txg >= zfs_free_max_blocks)
1352 elapsed_nanosecs = gethrtime() - scn->scn_sync_start_time;
1353 return (elapsed_nanosecs / NANOSEC > zfs_txg_timeout ||
1354 (NSEC2MSEC(elapsed_nanosecs) > zfs_free_min_time_ms &&
1355 txg_sync_waiting(scn->scn_dp)) ||
1356 spa_shutting_down(scn->scn_dp->dp_spa));
1360 dsl_scan_free_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1362 dsl_scan_t *scn = arg;
1364 if (!scn->scn_is_bptree ||
1365 (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_OBJSET)) {
1366 if (dsl_scan_free_should_pause(scn))
1367 return (SET_ERROR(ERESTART));
1370 zio_nowait(zio_free_sync(scn->scn_zio_root, scn->scn_dp->dp_spa,
1371 dmu_tx_get_txg(tx), bp, BP_GET_PSIZE(bp), 0));
1372 dsl_dir_diduse_space(tx->tx_pool->dp_free_dir, DD_USED_HEAD,
1373 -bp_get_dsize_sync(scn->scn_dp->dp_spa, bp),
1374 -BP_GET_PSIZE(bp), -BP_GET_UCSIZE(bp), tx);
1375 scn->scn_visited_this_txg++;
1380 dsl_scan_active(dsl_scan_t *scn)
1382 spa_t *spa = scn->scn_dp->dp_spa;
1383 uint64_t used = 0, comp, uncomp;
1385 if (spa->spa_load_state != SPA_LOAD_NONE)
1387 if (spa_shutting_down(spa))
1389 if (scn->scn_phys.scn_state == DSS_SCANNING ||
1390 (scn->scn_async_destroying && !scn->scn_async_stalled))
1393 if (spa_version(scn->scn_dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
1394 (void) bpobj_space(&scn->scn_dp->dp_free_bpobj,
1395 &used, &comp, &uncomp);
1401 dsl_scan_sync(dsl_pool_t *dp, dmu_tx_t *tx)
1403 dsl_scan_t *scn = dp->dp_scan;
1404 spa_t *spa = dp->dp_spa;
1408 * Check for scn_restart_txg before checking spa_load_state, so
1409 * that we can restart an old-style scan while the pool is being
1410 * imported (see dsl_scan_init).
1412 if (scn->scn_restart_txg != 0 &&
1413 scn->scn_restart_txg <= tx->tx_txg) {
1414 pool_scan_func_t func = POOL_SCAN_SCRUB;
1415 dsl_scan_done(scn, B_FALSE, tx);
1416 if (vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL))
1417 func = POOL_SCAN_RESILVER;
1418 zfs_dbgmsg("restarting scan func=%u txg=%llu",
1420 dsl_scan_setup_sync(&func, tx);
1424 * If the scan is inactive due to a stalled async destroy, try again.
1426 if ((!scn->scn_async_stalled && !dsl_scan_active(scn)) ||
1427 spa_sync_pass(dp->dp_spa) > 1)
1430 scn->scn_visited_this_txg = 0;
1431 scn->scn_pausing = B_FALSE;
1432 scn->scn_sync_start_time = gethrtime();
1433 spa->spa_scrub_active = B_TRUE;
1436 * First process the async destroys. If we pause, don't do
1437 * any scrubbing or resilvering. This ensures that there are no
1438 * async destroys while we are scanning, so the scan code doesn't
1439 * have to worry about traversing it. It is also faster to free the
1440 * blocks than to scrub them.
1442 if (spa_version(dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
1443 scn->scn_is_bptree = B_FALSE;
1444 scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
1445 NULL, ZIO_FLAG_MUSTSUCCEED);
1446 err = bpobj_iterate(&dp->dp_free_bpobj,
1447 dsl_scan_free_block_cb, scn, tx);
1448 VERIFY3U(0, ==, zio_wait(scn->scn_zio_root));
1450 if (err != 0 && err != ERESTART)
1451 zfs_panic_recover("error %u from bpobj_iterate()", err);
1454 if (err == 0 && spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) {
1455 ASSERT(scn->scn_async_destroying);
1456 scn->scn_is_bptree = B_TRUE;
1457 scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
1458 NULL, ZIO_FLAG_MUSTSUCCEED);
1459 err = bptree_iterate(dp->dp_meta_objset,
1460 dp->dp_bptree_obj, B_TRUE, dsl_scan_free_block_cb, scn, tx);
1461 VERIFY0(zio_wait(scn->scn_zio_root));
1463 if (err == EIO || err == ECKSUM) {
1465 } else if (err != 0 && err != ERESTART) {
1466 zfs_panic_recover("error %u from "
1467 "traverse_dataset_destroyed()", err);
1471 * If we didn't make progress, mark the async destroy as
1472 * stalled, so that we will not initiate a spa_sync() on
1475 scn->scn_async_stalled = (scn->scn_visited_this_txg == 0);
1477 if (bptree_is_empty(dp->dp_meta_objset, dp->dp_bptree_obj)) {
1478 /* finished; deactivate async destroy feature */
1479 spa_feature_decr(spa, SPA_FEATURE_ASYNC_DESTROY, tx);
1480 ASSERT(!spa_feature_is_active(spa,
1481 SPA_FEATURE_ASYNC_DESTROY));
1482 VERIFY0(zap_remove(dp->dp_meta_objset,
1483 DMU_POOL_DIRECTORY_OBJECT,
1484 DMU_POOL_BPTREE_OBJ, tx));
1485 VERIFY0(bptree_free(dp->dp_meta_objset,
1486 dp->dp_bptree_obj, tx));
1487 dp->dp_bptree_obj = 0;
1488 scn->scn_async_destroying = B_FALSE;
1491 if (scn->scn_visited_this_txg) {
1492 zfs_dbgmsg("freed %llu blocks in %llums from "
1493 "free_bpobj/bptree txg %llu; err=%u",
1494 (longlong_t)scn->scn_visited_this_txg,
1496 NSEC2MSEC(gethrtime() - scn->scn_sync_start_time),
1497 (longlong_t)tx->tx_txg, err);
1498 scn->scn_visited_this_txg = 0;
1501 * Write out changes to the DDT that may be required as a
1502 * result of the blocks freed. This ensures that the DDT
1503 * is clean when a scrub/resilver runs.
1505 ddt_sync(spa, tx->tx_txg);
1509 if (!scn->scn_async_destroying && zfs_free_leak_on_eio &&
1510 (dp->dp_free_dir->dd_phys->dd_used_bytes != 0 ||
1511 dp->dp_free_dir->dd_phys->dd_compressed_bytes != 0 ||
1512 dp->dp_free_dir->dd_phys->dd_uncompressed_bytes != 0)) {
1514 * We have finished background destroying, but there is still
1515 * some space left in the dp_free_dir. Transfer this leaked
1516 * space to the dp_leak_dir.
1518 if (dp->dp_leak_dir == NULL) {
1519 rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
1520 (void) dsl_dir_create_sync(dp, dp->dp_root_dir,
1522 VERIFY0(dsl_pool_open_special_dir(dp,
1523 LEAK_DIR_NAME, &dp->dp_leak_dir));
1524 rrw_exit(&dp->dp_config_rwlock, FTAG);
1526 dsl_dir_diduse_space(dp->dp_leak_dir, DD_USED_HEAD,
1527 dp->dp_free_dir->dd_phys->dd_used_bytes,
1528 dp->dp_free_dir->dd_phys->dd_compressed_bytes,
1529 dp->dp_free_dir->dd_phys->dd_uncompressed_bytes, tx);
1530 dsl_dir_diduse_space(dp->dp_free_dir, DD_USED_HEAD,
1531 -dp->dp_free_dir->dd_phys->dd_used_bytes,
1532 -dp->dp_free_dir->dd_phys->dd_compressed_bytes,
1533 -dp->dp_free_dir->dd_phys->dd_uncompressed_bytes, tx);
1535 if (!scn->scn_async_destroying) {
1536 /* finished; verify that space accounting went to zero */
1537 ASSERT0(dp->dp_free_dir->dd_phys->dd_used_bytes);
1538 ASSERT0(dp->dp_free_dir->dd_phys->dd_compressed_bytes);
1539 ASSERT0(dp->dp_free_dir->dd_phys->dd_uncompressed_bytes);
1542 if (scn->scn_phys.scn_state != DSS_SCANNING)
1545 if (scn->scn_done_txg == tx->tx_txg) {
1546 ASSERT(!scn->scn_pausing);
1547 /* finished with scan. */
1548 zfs_dbgmsg("txg %llu scan complete", tx->tx_txg);
1549 dsl_scan_done(scn, B_TRUE, tx);
1550 ASSERT3U(spa->spa_scrub_inflight, ==, 0);
1551 dsl_scan_sync_state(scn, tx);
1555 if (scn->scn_phys.scn_ddt_bookmark.ddb_class <=
1556 scn->scn_phys.scn_ddt_class_max) {
1557 zfs_dbgmsg("doing scan sync txg %llu; "
1558 "ddt bm=%llu/%llu/%llu/%llx",
1559 (longlong_t)tx->tx_txg,
1560 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_class,
1561 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_type,
1562 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_checksum,
1563 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_cursor);
1564 ASSERT(scn->scn_phys.scn_bookmark.zb_objset == 0);
1565 ASSERT(scn->scn_phys.scn_bookmark.zb_object == 0);
1566 ASSERT(scn->scn_phys.scn_bookmark.zb_level == 0);
1567 ASSERT(scn->scn_phys.scn_bookmark.zb_blkid == 0);
1569 zfs_dbgmsg("doing scan sync txg %llu; bm=%llu/%llu/%llu/%llu",
1570 (longlong_t)tx->tx_txg,
1571 (longlong_t)scn->scn_phys.scn_bookmark.zb_objset,
1572 (longlong_t)scn->scn_phys.scn_bookmark.zb_object,
1573 (longlong_t)scn->scn_phys.scn_bookmark.zb_level,
1574 (longlong_t)scn->scn_phys.scn_bookmark.zb_blkid);
1577 scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
1578 NULL, ZIO_FLAG_CANFAIL);
1579 dsl_pool_config_enter(dp, FTAG);
1580 dsl_scan_visit(scn, tx);
1581 dsl_pool_config_exit(dp, FTAG);
1582 (void) zio_wait(scn->scn_zio_root);
1583 scn->scn_zio_root = NULL;
1585 zfs_dbgmsg("visited %llu blocks in %llums",
1586 (longlong_t)scn->scn_visited_this_txg,
1587 (longlong_t)NSEC2MSEC(gethrtime() - scn->scn_sync_start_time));
1589 if (!scn->scn_pausing) {
1590 scn->scn_done_txg = tx->tx_txg + 1;
1591 zfs_dbgmsg("txg %llu traversal complete, waiting till txg %llu",
1592 tx->tx_txg, scn->scn_done_txg);
1595 if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
1596 mutex_enter(&spa->spa_scrub_lock);
1597 while (spa->spa_scrub_inflight > 0) {
1598 cv_wait(&spa->spa_scrub_io_cv,
1599 &spa->spa_scrub_lock);
1601 mutex_exit(&spa->spa_scrub_lock);
1604 dsl_scan_sync_state(scn, tx);
1608 * This will start a new scan, or restart an existing one.
1611 dsl_resilver_restart(dsl_pool_t *dp, uint64_t txg)
1615 tx = dmu_tx_create_dd(dp->dp_mos_dir);
1616 VERIFY(0 == dmu_tx_assign(tx, TXG_WAIT));
1618 txg = dmu_tx_get_txg(tx);
1619 dp->dp_scan->scn_restart_txg = txg;
1622 dp->dp_scan->scn_restart_txg = txg;
1624 zfs_dbgmsg("restarting resilver txg=%llu", txg);
1628 dsl_scan_resilvering(dsl_pool_t *dp)
1630 return (dp->dp_scan->scn_phys.scn_state == DSS_SCANNING &&
1631 dp->dp_scan->scn_phys.scn_func == POOL_SCAN_RESILVER);
1639 count_block(zfs_all_blkstats_t *zab, const blkptr_t *bp)
1644 * If we resume after a reboot, zab will be NULL; don't record
1645 * incomplete stats in that case.
1650 for (i = 0; i < 4; i++) {
1651 int l = (i < 2) ? BP_GET_LEVEL(bp) : DN_MAX_LEVELS;
1652 int t = (i & 1) ? BP_GET_TYPE(bp) : DMU_OT_TOTAL;
1653 if (t & DMU_OT_NEWTYPE)
1655 zfs_blkstat_t *zb = &zab->zab_type[l][t];
1659 zb->zb_asize += BP_GET_ASIZE(bp);
1660 zb->zb_lsize += BP_GET_LSIZE(bp);
1661 zb->zb_psize += BP_GET_PSIZE(bp);
1662 zb->zb_gangs += BP_COUNT_GANG(bp);
1664 switch (BP_GET_NDVAS(bp)) {
1666 if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
1667 DVA_GET_VDEV(&bp->blk_dva[1]))
1668 zb->zb_ditto_2_of_2_samevdev++;
1671 equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
1672 DVA_GET_VDEV(&bp->blk_dva[1])) +
1673 (DVA_GET_VDEV(&bp->blk_dva[0]) ==
1674 DVA_GET_VDEV(&bp->blk_dva[2])) +
1675 (DVA_GET_VDEV(&bp->blk_dva[1]) ==
1676 DVA_GET_VDEV(&bp->blk_dva[2]));
1678 zb->zb_ditto_2_of_3_samevdev++;
1679 else if (equal == 3)
1680 zb->zb_ditto_3_of_3_samevdev++;
1687 dsl_scan_scrub_done(zio_t *zio)
1689 spa_t *spa = zio->io_spa;
1691 zio_data_buf_free(zio->io_data, zio->io_size);
1693 mutex_enter(&spa->spa_scrub_lock);
1694 spa->spa_scrub_inflight--;
1695 cv_broadcast(&spa->spa_scrub_io_cv);
1697 if (zio->io_error && (zio->io_error != ECKSUM ||
1698 !(zio->io_flags & ZIO_FLAG_SPECULATIVE))) {
1699 spa->spa_dsl_pool->dp_scan->scn_phys.scn_errors++;
1701 mutex_exit(&spa->spa_scrub_lock);
1705 dsl_scan_scrub_cb(dsl_pool_t *dp,
1706 const blkptr_t *bp, const zbookmark_phys_t *zb)
1708 dsl_scan_t *scn = dp->dp_scan;
1709 size_t size = BP_GET_PSIZE(bp);
1710 spa_t *spa = dp->dp_spa;
1711 uint64_t phys_birth = BP_PHYSICAL_BIRTH(bp);
1713 int zio_flags = ZIO_FLAG_SCAN_THREAD | ZIO_FLAG_RAW | ZIO_FLAG_CANFAIL;
1714 unsigned int scan_delay = 0;
1716 if (phys_birth <= scn->scn_phys.scn_min_txg ||
1717 phys_birth >= scn->scn_phys.scn_max_txg)
1720 count_block(dp->dp_blkstats, bp);
1722 if (BP_IS_EMBEDDED(bp))
1725 ASSERT(DSL_SCAN_IS_SCRUB_RESILVER(scn));
1726 if (scn->scn_phys.scn_func == POOL_SCAN_SCRUB) {
1727 zio_flags |= ZIO_FLAG_SCRUB;
1729 scan_delay = zfs_scrub_delay;
1731 ASSERT3U(scn->scn_phys.scn_func, ==, POOL_SCAN_RESILVER);
1732 zio_flags |= ZIO_FLAG_RESILVER;
1734 scan_delay = zfs_resilver_delay;
1737 /* If it's an intent log block, failure is expected. */
1738 if (zb->zb_level == ZB_ZIL_LEVEL)
1739 zio_flags |= ZIO_FLAG_SPECULATIVE;
1741 for (int d = 0; d < BP_GET_NDVAS(bp); d++) {
1742 vdev_t *vd = vdev_lookup_top(spa,
1743 DVA_GET_VDEV(&bp->blk_dva[d]));
1746 * Keep track of how much data we've examined so that
1747 * zpool(1M) status can make useful progress reports.
1749 scn->scn_phys.scn_examined += DVA_GET_ASIZE(&bp->blk_dva[d]);
1750 spa->spa_scan_pass_exam += DVA_GET_ASIZE(&bp->blk_dva[d]);
1752 /* if it's a resilver, this may not be in the target range */
1754 if (DVA_GET_GANG(&bp->blk_dva[d])) {
1756 * Gang members may be spread across multiple
1757 * vdevs, so the best estimate we have is the
1758 * scrub range, which has already been checked.
1759 * XXX -- it would be better to change our
1760 * allocation policy to ensure that all
1761 * gang members reside on the same vdev.
1765 needs_io = vdev_dtl_contains(vd, DTL_PARTIAL,
1771 if (needs_io && !zfs_no_scrub_io) {
1772 vdev_t *rvd = spa->spa_root_vdev;
1773 uint64_t maxinflight = rvd->vdev_children *
1774 MAX(zfs_top_maxinflight, 1);
1775 void *data = zio_data_buf_alloc(size);
1777 mutex_enter(&spa->spa_scrub_lock);
1778 while (spa->spa_scrub_inflight >= maxinflight)
1779 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
1780 spa->spa_scrub_inflight++;
1781 mutex_exit(&spa->spa_scrub_lock);
1784 * If we're seeing recent (zfs_scan_idle) "important" I/Os
1785 * then throttle our workload to limit the impact of a scan.
1787 if (ddi_get_lbolt64() - spa->spa_last_io <= zfs_scan_idle)
1788 delay(MAX((int)scan_delay, 0));
1790 zio_nowait(zio_read(NULL, spa, bp, data, size,
1791 dsl_scan_scrub_done, NULL, ZIO_PRIORITY_SCRUB,
1795 /* do not relocate this block */
1800 dsl_scan(dsl_pool_t *dp, pool_scan_func_t func)
1802 spa_t *spa = dp->dp_spa;
1805 * Purge all vdev caches and probe all devices. We do this here
1806 * rather than in sync context because this requires a writer lock
1807 * on the spa_config lock, which we can't do from sync context. The
1808 * spa_scrub_reopen flag indicates that vdev_open() should not
1809 * attempt to start another scrub.
1811 spa_vdev_state_enter(spa, SCL_NONE);
1812 spa->spa_scrub_reopen = B_TRUE;
1813 vdev_reopen(spa->spa_root_vdev);
1814 spa->spa_scrub_reopen = B_FALSE;
1815 (void) spa_vdev_state_exit(spa, NULL, 0);
1817 return (dsl_sync_task(spa_name(spa), dsl_scan_setup_check,
1818 dsl_scan_setup_sync, &func, 0, ZFS_SPACE_CHECK_NONE));