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) 2013 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 *, const zbookmark_t *);
55 static scan_cb_t dsl_scan_defrag_cb;
56 static scan_cb_t dsl_scan_scrub_cb;
57 static scan_cb_t dsl_scan_remove_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 *tx);
61 unsigned int zfs_top_maxinflight = 32; /* maximum I/Os per top-level */
62 unsigned int zfs_resilver_delay = 2; /* number of ticks to delay resilver */
63 unsigned int zfs_scrub_delay = 4; /* number of ticks to delay scrub */
64 unsigned int zfs_scan_idle = 50; /* idle window in clock ticks */
66 unsigned int zfs_scan_min_time_ms = 1000; /* min millisecs to scrub per txg */
67 unsigned int zfs_free_min_time_ms = 1000; /* min millisecs to free per txg */
68 unsigned int zfs_resilver_min_time_ms = 3000; /* min millisecs to resilver
70 boolean_t zfs_no_scrub_io = B_FALSE; /* set to disable scrub i/o */
71 boolean_t zfs_no_scrub_prefetch = B_FALSE; /* set to disable srub prefetching */
73 SYSCTL_DECL(_vfs_zfs);
74 TUNABLE_INT("vfs.zfs.top_maxinflight", &zfs_top_maxinflight);
75 SYSCTL_UINT(_vfs_zfs, OID_AUTO, top_maxinflight, CTLFLAG_RW,
76 &zfs_top_maxinflight, 0, "Maximum I/Os per top-level vdev");
77 TUNABLE_INT("vfs.zfs.resilver_delay", &zfs_resilver_delay);
78 SYSCTL_UINT(_vfs_zfs, OID_AUTO, resilver_delay, CTLFLAG_RW,
79 &zfs_resilver_delay, 0, "Number of ticks to delay resilver");
80 TUNABLE_INT("vfs.zfs.scrub_delay", &zfs_scrub_delay);
81 SYSCTL_UINT(_vfs_zfs, OID_AUTO, scrub_delay, CTLFLAG_RW,
82 &zfs_scrub_delay, 0, "Number of ticks to delay scrub");
83 TUNABLE_INT("vfs.zfs.scan_idle", &zfs_scan_idle);
84 SYSCTL_UINT(_vfs_zfs, OID_AUTO, scan_idle, CTLFLAG_RW,
85 &zfs_scan_idle, 0, "Idle scan window in clock ticks");
86 TUNABLE_INT("vfs.zfs.scan_min_time_ms", &zfs_scan_min_time_ms);
87 SYSCTL_UINT(_vfs_zfs, OID_AUTO, scan_min_time_ms, CTLFLAG_RW,
88 &zfs_scan_min_time_ms, 0, "Min millisecs to scrub per txg");
89 TUNABLE_INT("vfs.zfs.free_min_time_ms", &zfs_free_min_time_ms);
90 SYSCTL_UINT(_vfs_zfs, OID_AUTO, free_min_time_ms, CTLFLAG_RW,
91 &zfs_free_min_time_ms, 0, "Min millisecs to free per txg");
92 TUNABLE_INT("vfs.zfs.resilver_min_time_ms", &zfs_resilver_min_time_ms);
93 SYSCTL_UINT(_vfs_zfs, OID_AUTO, resilver_min_time_ms, CTLFLAG_RW,
94 &zfs_resilver_min_time_ms, 0, "Min millisecs to resilver per txg");
95 TUNABLE_INT("vfs.zfs.no_scrub_io", &zfs_no_scrub_io);
96 SYSCTL_INT(_vfs_zfs, OID_AUTO, no_scrub_io, CTLFLAG_RW,
97 &zfs_no_scrub_io, 0, "Disable scrub I/O");
98 TUNABLE_INT("vfs.zfs.no_scrub_prefetch", &zfs_no_scrub_prefetch);
99 SYSCTL_INT(_vfs_zfs, OID_AUTO, no_scrub_prefetch, CTLFLAG_RW,
100 &zfs_no_scrub_prefetch, 0, "Disable scrub prefetching");
102 enum ddt_class zfs_scrub_ddt_class_max = DDT_CLASS_DUPLICATE;
104 #define DSL_SCAN_IS_SCRUB_RESILVER(scn) \
105 ((scn)->scn_phys.scn_func == POOL_SCAN_SCRUB || \
106 (scn)->scn_phys.scn_func == POOL_SCAN_RESILVER)
108 extern int zfs_txg_timeout;
110 /* the order has to match pool_scan_type */
111 static scan_cb_t *scan_funcs[POOL_SCAN_FUNCS] = {
113 dsl_scan_scrub_cb, /* POOL_SCAN_SCRUB */
114 dsl_scan_scrub_cb, /* POOL_SCAN_RESILVER */
118 dsl_scan_init(dsl_pool_t *dp, uint64_t txg)
122 spa_t *spa = dp->dp_spa;
125 scn = dp->dp_scan = kmem_zalloc(sizeof (dsl_scan_t), KM_SLEEP);
129 * It's possible that we're resuming a scan after a reboot so
130 * make sure that the scan_async_destroying flag is initialized
133 ASSERT(!scn->scn_async_destroying);
134 scn->scn_async_destroying = spa_feature_is_active(dp->dp_spa,
135 &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY]);
137 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
138 "scrub_func", sizeof (uint64_t), 1, &f);
141 * There was an old-style scrub in progress. Restart a
142 * new-style scrub from the beginning.
144 scn->scn_restart_txg = txg;
145 zfs_dbgmsg("old-style scrub was in progress; "
146 "restarting new-style scrub in txg %llu",
147 scn->scn_restart_txg);
150 * Load the queue obj from the old location so that it
151 * can be freed by dsl_scan_done().
153 (void) zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
154 "scrub_queue", sizeof (uint64_t), 1,
155 &scn->scn_phys.scn_queue_obj);
157 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
158 DMU_POOL_SCAN, sizeof (uint64_t), SCAN_PHYS_NUMINTS,
165 if (scn->scn_phys.scn_state == DSS_SCANNING &&
166 spa_prev_software_version(dp->dp_spa) < SPA_VERSION_SCAN) {
168 * A new-type scrub was in progress on an old
169 * pool, and the pool was accessed by old
170 * software. Restart from the beginning, since
171 * the old software may have changed the pool in
174 scn->scn_restart_txg = txg;
175 zfs_dbgmsg("new-style scrub was modified "
176 "by old software; restarting in txg %llu",
177 scn->scn_restart_txg);
181 spa_scan_stat_init(spa);
186 dsl_scan_fini(dsl_pool_t *dp)
189 kmem_free(dp->dp_scan, sizeof (dsl_scan_t));
196 dsl_scan_setup_check(void *arg, dmu_tx_t *tx)
198 dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
200 if (scn->scn_phys.scn_state == DSS_SCANNING)
201 return (SET_ERROR(EBUSY));
207 dsl_scan_setup_sync(void *arg, dmu_tx_t *tx)
209 dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
210 pool_scan_func_t *funcp = arg;
211 dmu_object_type_t ot = 0;
212 dsl_pool_t *dp = scn->scn_dp;
213 spa_t *spa = dp->dp_spa;
215 ASSERT(scn->scn_phys.scn_state != DSS_SCANNING);
216 ASSERT(*funcp > POOL_SCAN_NONE && *funcp < POOL_SCAN_FUNCS);
217 bzero(&scn->scn_phys, sizeof (scn->scn_phys));
218 scn->scn_phys.scn_func = *funcp;
219 scn->scn_phys.scn_state = DSS_SCANNING;
220 scn->scn_phys.scn_min_txg = 0;
221 scn->scn_phys.scn_max_txg = tx->tx_txg;
222 scn->scn_phys.scn_ddt_class_max = DDT_CLASSES - 1; /* the entire DDT */
223 scn->scn_phys.scn_start_time = gethrestime_sec();
224 scn->scn_phys.scn_errors = 0;
225 scn->scn_phys.scn_to_examine = spa->spa_root_vdev->vdev_stat.vs_alloc;
226 scn->scn_restart_txg = 0;
227 scn->scn_done_txg = 0;
228 spa_scan_stat_init(spa);
230 if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
231 scn->scn_phys.scn_ddt_class_max = zfs_scrub_ddt_class_max;
233 /* rewrite all disk labels */
234 vdev_config_dirty(spa->spa_root_vdev);
236 if (vdev_resilver_needed(spa->spa_root_vdev,
237 &scn->scn_phys.scn_min_txg, &scn->scn_phys.scn_max_txg)) {
238 spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_START);
240 spa_event_notify(spa, NULL, ESC_ZFS_SCRUB_START);
243 spa->spa_scrub_started = B_TRUE;
245 * If this is an incremental scrub, limit the DDT scrub phase
246 * to just the auto-ditto class (for correctness); the rest
247 * of the scrub should go faster using top-down pruning.
249 if (scn->scn_phys.scn_min_txg > TXG_INITIAL)
250 scn->scn_phys.scn_ddt_class_max = DDT_CLASS_DITTO;
254 /* back to the generic stuff */
256 if (dp->dp_blkstats == NULL) {
258 kmem_alloc(sizeof (zfs_all_blkstats_t), KM_SLEEP);
260 bzero(dp->dp_blkstats, sizeof (zfs_all_blkstats_t));
262 if (spa_version(spa) < SPA_VERSION_DSL_SCRUB)
263 ot = DMU_OT_ZAP_OTHER;
265 scn->scn_phys.scn_queue_obj = zap_create(dp->dp_meta_objset,
266 ot ? ot : DMU_OT_SCAN_QUEUE, DMU_OT_NONE, 0, tx);
268 dsl_scan_sync_state(scn, tx);
270 spa_history_log_internal(spa, "scan setup", tx,
271 "func=%u mintxg=%llu maxtxg=%llu",
272 *funcp, scn->scn_phys.scn_min_txg, scn->scn_phys.scn_max_txg);
277 dsl_scan_done(dsl_scan_t *scn, boolean_t complete, dmu_tx_t *tx)
279 static const char *old_names[] = {
281 "scrub_ddt_bookmark",
282 "scrub_ddt_class_max",
291 dsl_pool_t *dp = scn->scn_dp;
292 spa_t *spa = dp->dp_spa;
295 /* Remove any remnants of an old-style scrub. */
296 for (i = 0; old_names[i]; i++) {
297 (void) zap_remove(dp->dp_meta_objset,
298 DMU_POOL_DIRECTORY_OBJECT, old_names[i], tx);
301 if (scn->scn_phys.scn_queue_obj != 0) {
302 VERIFY(0 == dmu_object_free(dp->dp_meta_objset,
303 scn->scn_phys.scn_queue_obj, tx));
304 scn->scn_phys.scn_queue_obj = 0;
308 * If we were "restarted" from a stopped state, don't bother
309 * with anything else.
311 if (scn->scn_phys.scn_state != DSS_SCANNING)
315 scn->scn_phys.scn_state = DSS_FINISHED;
317 scn->scn_phys.scn_state = DSS_CANCELED;
319 spa_history_log_internal(spa, "scan done", tx,
320 "complete=%u", complete);
322 if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
323 mutex_enter(&spa->spa_scrub_lock);
324 while (spa->spa_scrub_inflight > 0) {
325 cv_wait(&spa->spa_scrub_io_cv,
326 &spa->spa_scrub_lock);
328 mutex_exit(&spa->spa_scrub_lock);
329 spa->spa_scrub_started = B_FALSE;
330 spa->spa_scrub_active = B_FALSE;
333 * If the scrub/resilver completed, update all DTLs to
334 * reflect this. Whether it succeeded or not, vacate
335 * all temporary scrub DTLs.
337 vdev_dtl_reassess(spa->spa_root_vdev, tx->tx_txg,
338 complete ? scn->scn_phys.scn_max_txg : 0, B_TRUE);
340 spa_event_notify(spa, NULL, scn->scn_phys.scn_min_txg ?
341 ESC_ZFS_RESILVER_FINISH : ESC_ZFS_SCRUB_FINISH);
343 spa_errlog_rotate(spa);
346 * We may have finished replacing a device.
347 * Let the async thread assess this and handle the detach.
349 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
352 scn->scn_phys.scn_end_time = gethrestime_sec();
357 dsl_scan_cancel_check(void *arg, dmu_tx_t *tx)
359 dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
361 if (scn->scn_phys.scn_state != DSS_SCANNING)
362 return (SET_ERROR(ENOENT));
368 dsl_scan_cancel_sync(void *arg, dmu_tx_t *tx)
370 dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
372 dsl_scan_done(scn, B_FALSE, tx);
373 dsl_scan_sync_state(scn, tx);
377 dsl_scan_cancel(dsl_pool_t *dp)
379 return (dsl_sync_task(spa_name(dp->dp_spa), dsl_scan_cancel_check,
380 dsl_scan_cancel_sync, NULL, 3));
383 static void dsl_scan_visitbp(blkptr_t *bp,
384 const zbookmark_t *zb, dnode_phys_t *dnp, arc_buf_t *pbuf,
385 dsl_dataset_t *ds, dsl_scan_t *scn, dmu_objset_type_t ostype,
387 static void dsl_scan_visitdnode(dsl_scan_t *, dsl_dataset_t *ds,
388 dmu_objset_type_t ostype,
389 dnode_phys_t *dnp, arc_buf_t *buf, uint64_t object, dmu_tx_t *tx);
392 dsl_free(dsl_pool_t *dp, uint64_t txg, const blkptr_t *bp)
394 zio_free(dp->dp_spa, txg, bp);
398 dsl_free_sync(zio_t *pio, dsl_pool_t *dp, uint64_t txg, const blkptr_t *bpp)
400 ASSERT(dsl_pool_sync_context(dp));
401 zio_nowait(zio_free_sync(pio, dp->dp_spa, txg, bpp, BP_GET_PSIZE(bpp),
406 dsl_scan_ds_maxtxg(dsl_dataset_t *ds)
408 uint64_t smt = ds->ds_dir->dd_pool->dp_scan->scn_phys.scn_max_txg;
409 if (dsl_dataset_is_snapshot(ds))
410 return (MIN(smt, ds->ds_phys->ds_creation_txg));
415 dsl_scan_sync_state(dsl_scan_t *scn, dmu_tx_t *tx)
417 VERIFY0(zap_update(scn->scn_dp->dp_meta_objset,
418 DMU_POOL_DIRECTORY_OBJECT,
419 DMU_POOL_SCAN, sizeof (uint64_t), SCAN_PHYS_NUMINTS,
420 &scn->scn_phys, tx));
424 dsl_scan_check_pause(dsl_scan_t *scn, const zbookmark_t *zb)
426 uint64_t elapsed_nanosecs;
427 unsigned int mintime;
429 /* we never skip user/group accounting objects */
430 if (zb && (int64_t)zb->zb_object < 0)
433 if (scn->scn_pausing)
434 return (B_TRUE); /* we're already pausing */
436 if (!ZB_IS_ZERO(&scn->scn_phys.scn_bookmark))
437 return (B_FALSE); /* we're resuming */
439 /* We only know how to resume from level-0 blocks. */
440 if (zb && zb->zb_level != 0)
443 mintime = (scn->scn_phys.scn_func == POOL_SCAN_RESILVER) ?
444 zfs_resilver_min_time_ms : zfs_scan_min_time_ms;
445 elapsed_nanosecs = gethrtime() - scn->scn_sync_start_time;
446 if (elapsed_nanosecs / NANOSEC > zfs_txg_timeout ||
447 (NSEC2MSEC(elapsed_nanosecs) > mintime &&
448 txg_sync_waiting(scn->scn_dp)) ||
449 spa_shutting_down(scn->scn_dp->dp_spa)) {
451 dprintf("pausing at bookmark %llx/%llx/%llx/%llx\n",
452 (longlong_t)zb->zb_objset,
453 (longlong_t)zb->zb_object,
454 (longlong_t)zb->zb_level,
455 (longlong_t)zb->zb_blkid);
456 scn->scn_phys.scn_bookmark = *zb;
458 dprintf("pausing at DDT bookmark %llx/%llx/%llx/%llx\n",
459 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_class,
460 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_type,
461 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_checksum,
462 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_cursor);
463 scn->scn_pausing = B_TRUE;
469 typedef struct zil_scan_arg {
471 zil_header_t *zsa_zh;
476 dsl_scan_zil_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
478 zil_scan_arg_t *zsa = arg;
479 dsl_pool_t *dp = zsa->zsa_dp;
480 dsl_scan_t *scn = dp->dp_scan;
481 zil_header_t *zh = zsa->zsa_zh;
484 if (bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
488 * One block ("stubby") can be allocated a long time ago; we
489 * want to visit that one because it has been allocated
490 * (on-disk) even if it hasn't been claimed (even though for
491 * scrub there's nothing to do to it).
493 if (claim_txg == 0 && bp->blk_birth >= spa_first_txg(dp->dp_spa))
496 SET_BOOKMARK(&zb, zh->zh_log.blk_cksum.zc_word[ZIL_ZC_OBJSET],
497 ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, bp->blk_cksum.zc_word[ZIL_ZC_SEQ]);
499 VERIFY(0 == scan_funcs[scn->scn_phys.scn_func](dp, bp, &zb));
505 dsl_scan_zil_record(zilog_t *zilog, lr_t *lrc, void *arg, uint64_t claim_txg)
507 if (lrc->lrc_txtype == TX_WRITE) {
508 zil_scan_arg_t *zsa = arg;
509 dsl_pool_t *dp = zsa->zsa_dp;
510 dsl_scan_t *scn = dp->dp_scan;
511 zil_header_t *zh = zsa->zsa_zh;
512 lr_write_t *lr = (lr_write_t *)lrc;
513 blkptr_t *bp = &lr->lr_blkptr;
516 if (bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
520 * birth can be < claim_txg if this record's txg is
521 * already txg sync'ed (but this log block contains
522 * other records that are not synced)
524 if (claim_txg == 0 || bp->blk_birth < claim_txg)
527 SET_BOOKMARK(&zb, zh->zh_log.blk_cksum.zc_word[ZIL_ZC_OBJSET],
528 lr->lr_foid, ZB_ZIL_LEVEL,
529 lr->lr_offset / BP_GET_LSIZE(bp));
531 VERIFY(0 == scan_funcs[scn->scn_phys.scn_func](dp, bp, &zb));
537 dsl_scan_zil(dsl_pool_t *dp, zil_header_t *zh)
539 uint64_t claim_txg = zh->zh_claim_txg;
540 zil_scan_arg_t zsa = { dp, zh };
544 * We only want to visit blocks that have been claimed but not yet
545 * replayed (or, in read-only mode, blocks that *would* be claimed).
547 if (claim_txg == 0 && spa_writeable(dp->dp_spa))
550 zilog = zil_alloc(dp->dp_meta_objset, zh);
552 (void) zil_parse(zilog, dsl_scan_zil_block, dsl_scan_zil_record, &zsa,
560 dsl_scan_prefetch(dsl_scan_t *scn, arc_buf_t *buf, blkptr_t *bp,
561 uint64_t objset, uint64_t object, uint64_t blkid)
564 uint32_t flags = ARC_NOWAIT | ARC_PREFETCH;
566 if (zfs_no_scrub_prefetch)
569 if (BP_IS_HOLE(bp) || bp->blk_birth <= scn->scn_phys.scn_min_txg ||
570 (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_DNODE))
573 SET_BOOKMARK(&czb, objset, object, BP_GET_LEVEL(bp), blkid);
575 (void) arc_read(scn->scn_zio_root, scn->scn_dp->dp_spa, bp,
576 NULL, NULL, ZIO_PRIORITY_ASYNC_READ,
577 ZIO_FLAG_CANFAIL | ZIO_FLAG_SCAN_THREAD, &flags, &czb);
581 dsl_scan_check_resume(dsl_scan_t *scn, const dnode_phys_t *dnp,
582 const zbookmark_t *zb)
585 * We never skip over user/group accounting objects (obj<0)
587 if (!ZB_IS_ZERO(&scn->scn_phys.scn_bookmark) &&
588 (int64_t)zb->zb_object >= 0) {
590 * If we already visited this bp & everything below (in
591 * a prior txg sync), don't bother doing it again.
593 if (zbookmark_is_before(dnp, zb, &scn->scn_phys.scn_bookmark))
597 * If we found the block we're trying to resume from, or
598 * we went past it to a different object, zero it out to
599 * indicate that it's OK to start checking for pausing
602 if (bcmp(zb, &scn->scn_phys.scn_bookmark, sizeof (*zb)) == 0 ||
603 zb->zb_object > scn->scn_phys.scn_bookmark.zb_object) {
604 dprintf("resuming at %llx/%llx/%llx/%llx\n",
605 (longlong_t)zb->zb_objset,
606 (longlong_t)zb->zb_object,
607 (longlong_t)zb->zb_level,
608 (longlong_t)zb->zb_blkid);
609 bzero(&scn->scn_phys.scn_bookmark, sizeof (*zb));
616 * Return nonzero on i/o error.
617 * Return new buf to write out in *bufp.
620 dsl_scan_recurse(dsl_scan_t *scn, dsl_dataset_t *ds, dmu_objset_type_t ostype,
621 dnode_phys_t *dnp, const blkptr_t *bp,
622 const zbookmark_t *zb, dmu_tx_t *tx, arc_buf_t **bufp)
624 dsl_pool_t *dp = scn->scn_dp;
625 int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCAN_THREAD;
628 if (BP_GET_LEVEL(bp) > 0) {
629 uint32_t flags = ARC_WAIT;
632 int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
634 err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, bufp,
635 ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
637 scn->scn_phys.scn_errors++;
640 for (i = 0, cbp = (*bufp)->b_data; i < epb; i++, cbp++) {
641 dsl_scan_prefetch(scn, *bufp, cbp, zb->zb_objset,
642 zb->zb_object, zb->zb_blkid * epb + i);
644 for (i = 0, cbp = (*bufp)->b_data; i < epb; i++, cbp++) {
647 SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
649 zb->zb_blkid * epb + i);
650 dsl_scan_visitbp(cbp, &czb, dnp,
651 *bufp, ds, scn, ostype, tx);
653 } else if (BP_GET_TYPE(bp) == DMU_OT_USERGROUP_USED) {
654 uint32_t flags = ARC_WAIT;
656 err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, bufp,
657 ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
659 scn->scn_phys.scn_errors++;
662 } else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) {
663 uint32_t flags = ARC_WAIT;
666 int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
668 err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, bufp,
669 ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
671 scn->scn_phys.scn_errors++;
674 for (i = 0, cdnp = (*bufp)->b_data; i < epb; i++, cdnp++) {
675 for (j = 0; j < cdnp->dn_nblkptr; j++) {
676 blkptr_t *cbp = &cdnp->dn_blkptr[j];
677 dsl_scan_prefetch(scn, *bufp, cbp,
678 zb->zb_objset, zb->zb_blkid * epb + i, j);
681 for (i = 0, cdnp = (*bufp)->b_data; i < epb; i++, cdnp++) {
682 dsl_scan_visitdnode(scn, ds, ostype,
683 cdnp, *bufp, zb->zb_blkid * epb + i, tx);
686 } else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) {
687 uint32_t flags = ARC_WAIT;
690 err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, bufp,
691 ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
693 scn->scn_phys.scn_errors++;
697 osp = (*bufp)->b_data;
699 dsl_scan_visitdnode(scn, ds, osp->os_type,
700 &osp->os_meta_dnode, *bufp, DMU_META_DNODE_OBJECT, tx);
702 if (OBJSET_BUF_HAS_USERUSED(*bufp)) {
704 * We also always visit user/group accounting
705 * objects, and never skip them, even if we are
706 * pausing. This is necessary so that the space
707 * deltas from this txg get integrated.
709 dsl_scan_visitdnode(scn, ds, osp->os_type,
710 &osp->os_groupused_dnode, *bufp,
711 DMU_GROUPUSED_OBJECT, tx);
712 dsl_scan_visitdnode(scn, ds, osp->os_type,
713 &osp->os_userused_dnode, *bufp,
714 DMU_USERUSED_OBJECT, tx);
722 dsl_scan_visitdnode(dsl_scan_t *scn, dsl_dataset_t *ds,
723 dmu_objset_type_t ostype, dnode_phys_t *dnp, arc_buf_t *buf,
724 uint64_t object, dmu_tx_t *tx)
728 for (j = 0; j < dnp->dn_nblkptr; j++) {
731 SET_BOOKMARK(&czb, ds ? ds->ds_object : 0, object,
732 dnp->dn_nlevels - 1, j);
733 dsl_scan_visitbp(&dnp->dn_blkptr[j],
734 &czb, dnp, buf, ds, scn, ostype, tx);
737 if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
739 SET_BOOKMARK(&czb, ds ? ds->ds_object : 0, object,
741 dsl_scan_visitbp(&dnp->dn_spill,
742 &czb, dnp, buf, ds, scn, ostype, tx);
747 * The arguments are in this order because mdb can only print the
748 * first 5; we want them to be useful.
751 dsl_scan_visitbp(blkptr_t *bp, const zbookmark_t *zb,
752 dnode_phys_t *dnp, arc_buf_t *pbuf,
753 dsl_dataset_t *ds, dsl_scan_t *scn, dmu_objset_type_t ostype,
756 dsl_pool_t *dp = scn->scn_dp;
757 arc_buf_t *buf = NULL;
758 blkptr_t bp_toread = *bp;
760 /* ASSERT(pbuf == NULL || arc_released(pbuf)); */
762 if (dsl_scan_check_pause(scn, zb))
765 if (dsl_scan_check_resume(scn, dnp, zb))
768 if (bp->blk_birth == 0)
771 scn->scn_visited_this_txg++;
774 "visiting ds=%p/%llu zb=%llx/%llx/%llx/%llx buf=%p bp=%p",
775 ds, ds ? ds->ds_object : 0,
776 zb->zb_objset, zb->zb_object, zb->zb_level, zb->zb_blkid,
779 if (bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
782 if (dsl_scan_recurse(scn, ds, ostype, dnp, &bp_toread, zb, tx,
787 * If dsl_scan_ddt() has aready visited this block, it will have
788 * already done any translations or scrubbing, so don't call the
791 if (ddt_class_contains(dp->dp_spa,
792 scn->scn_phys.scn_ddt_class_max, bp)) {
798 * If this block is from the future (after cur_max_txg), then we
799 * are doing this on behalf of a deleted snapshot, and we will
800 * revisit the future block on the next pass of this dataset.
801 * Don't scan it now unless we need to because something
802 * under it was modified.
804 if (BP_PHYSICAL_BIRTH(bp) <= scn->scn_phys.scn_cur_max_txg) {
805 scan_funcs[scn->scn_phys.scn_func](dp, bp, zb);
808 (void) arc_buf_remove_ref(buf, &buf);
812 dsl_scan_visit_rootbp(dsl_scan_t *scn, dsl_dataset_t *ds, blkptr_t *bp,
817 SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
818 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
819 dsl_scan_visitbp(bp, &zb, NULL, NULL,
820 ds, scn, DMU_OST_NONE, tx);
822 dprintf_ds(ds, "finished scan%s", "");
826 dsl_scan_ds_destroyed(dsl_dataset_t *ds, dmu_tx_t *tx)
828 dsl_pool_t *dp = ds->ds_dir->dd_pool;
829 dsl_scan_t *scn = dp->dp_scan;
832 if (scn->scn_phys.scn_state != DSS_SCANNING)
835 if (scn->scn_phys.scn_bookmark.zb_objset == ds->ds_object) {
836 if (dsl_dataset_is_snapshot(ds)) {
837 /* Note, scn_cur_{min,max}_txg stays the same. */
838 scn->scn_phys.scn_bookmark.zb_objset =
839 ds->ds_phys->ds_next_snap_obj;
840 zfs_dbgmsg("destroying ds %llu; currently traversing; "
841 "reset zb_objset to %llu",
842 (u_longlong_t)ds->ds_object,
843 (u_longlong_t)ds->ds_phys->ds_next_snap_obj);
844 scn->scn_phys.scn_flags |= DSF_VISIT_DS_AGAIN;
846 SET_BOOKMARK(&scn->scn_phys.scn_bookmark,
847 ZB_DESTROYED_OBJSET, 0, 0, 0);
848 zfs_dbgmsg("destroying ds %llu; currently traversing; "
849 "reset bookmark to -1,0,0,0",
850 (u_longlong_t)ds->ds_object);
852 } else if (zap_lookup_int_key(dp->dp_meta_objset,
853 scn->scn_phys.scn_queue_obj, ds->ds_object, &mintxg) == 0) {
854 ASSERT3U(ds->ds_phys->ds_num_children, <=, 1);
855 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
856 scn->scn_phys.scn_queue_obj, ds->ds_object, tx));
857 if (dsl_dataset_is_snapshot(ds)) {
859 * We keep the same mintxg; it could be >
860 * ds_creation_txg if the previous snapshot was
863 VERIFY(zap_add_int_key(dp->dp_meta_objset,
864 scn->scn_phys.scn_queue_obj,
865 ds->ds_phys->ds_next_snap_obj, mintxg, tx) == 0);
866 zfs_dbgmsg("destroying ds %llu; in queue; "
867 "replacing with %llu",
868 (u_longlong_t)ds->ds_object,
869 (u_longlong_t)ds->ds_phys->ds_next_snap_obj);
871 zfs_dbgmsg("destroying ds %llu; in queue; removing",
872 (u_longlong_t)ds->ds_object);
875 zfs_dbgmsg("destroying ds %llu; ignoring",
876 (u_longlong_t)ds->ds_object);
880 * dsl_scan_sync() should be called after this, and should sync
881 * out our changed state, but just to be safe, do it here.
883 dsl_scan_sync_state(scn, tx);
887 dsl_scan_ds_snapshotted(dsl_dataset_t *ds, dmu_tx_t *tx)
889 dsl_pool_t *dp = ds->ds_dir->dd_pool;
890 dsl_scan_t *scn = dp->dp_scan;
893 if (scn->scn_phys.scn_state != DSS_SCANNING)
896 ASSERT(ds->ds_phys->ds_prev_snap_obj != 0);
898 if (scn->scn_phys.scn_bookmark.zb_objset == ds->ds_object) {
899 scn->scn_phys.scn_bookmark.zb_objset =
900 ds->ds_phys->ds_prev_snap_obj;
901 zfs_dbgmsg("snapshotting ds %llu; currently traversing; "
902 "reset zb_objset to %llu",
903 (u_longlong_t)ds->ds_object,
904 (u_longlong_t)ds->ds_phys->ds_prev_snap_obj);
905 } else if (zap_lookup_int_key(dp->dp_meta_objset,
906 scn->scn_phys.scn_queue_obj, ds->ds_object, &mintxg) == 0) {
907 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
908 scn->scn_phys.scn_queue_obj, ds->ds_object, tx));
909 VERIFY(zap_add_int_key(dp->dp_meta_objset,
910 scn->scn_phys.scn_queue_obj,
911 ds->ds_phys->ds_prev_snap_obj, mintxg, tx) == 0);
912 zfs_dbgmsg("snapshotting ds %llu; in queue; "
913 "replacing with %llu",
914 (u_longlong_t)ds->ds_object,
915 (u_longlong_t)ds->ds_phys->ds_prev_snap_obj);
917 dsl_scan_sync_state(scn, tx);
921 dsl_scan_ds_clone_swapped(dsl_dataset_t *ds1, dsl_dataset_t *ds2, dmu_tx_t *tx)
923 dsl_pool_t *dp = ds1->ds_dir->dd_pool;
924 dsl_scan_t *scn = dp->dp_scan;
927 if (scn->scn_phys.scn_state != DSS_SCANNING)
930 if (scn->scn_phys.scn_bookmark.zb_objset == ds1->ds_object) {
931 scn->scn_phys.scn_bookmark.zb_objset = ds2->ds_object;
932 zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
933 "reset zb_objset to %llu",
934 (u_longlong_t)ds1->ds_object,
935 (u_longlong_t)ds2->ds_object);
936 } else if (scn->scn_phys.scn_bookmark.zb_objset == ds2->ds_object) {
937 scn->scn_phys.scn_bookmark.zb_objset = ds1->ds_object;
938 zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
939 "reset zb_objset to %llu",
940 (u_longlong_t)ds2->ds_object,
941 (u_longlong_t)ds1->ds_object);
944 if (zap_lookup_int_key(dp->dp_meta_objset, scn->scn_phys.scn_queue_obj,
945 ds1->ds_object, &mintxg) == 0) {
948 ASSERT3U(mintxg, ==, ds1->ds_phys->ds_prev_snap_txg);
949 ASSERT3U(mintxg, ==, ds2->ds_phys->ds_prev_snap_txg);
950 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
951 scn->scn_phys.scn_queue_obj, ds1->ds_object, tx));
952 err = zap_add_int_key(dp->dp_meta_objset,
953 scn->scn_phys.scn_queue_obj, ds2->ds_object, mintxg, tx);
954 VERIFY(err == 0 || err == EEXIST);
956 /* Both were there to begin with */
957 VERIFY(0 == zap_add_int_key(dp->dp_meta_objset,
958 scn->scn_phys.scn_queue_obj,
959 ds1->ds_object, mintxg, tx));
961 zfs_dbgmsg("clone_swap ds %llu; in queue; "
962 "replacing with %llu",
963 (u_longlong_t)ds1->ds_object,
964 (u_longlong_t)ds2->ds_object);
965 } else if (zap_lookup_int_key(dp->dp_meta_objset,
966 scn->scn_phys.scn_queue_obj, ds2->ds_object, &mintxg) == 0) {
967 ASSERT3U(mintxg, ==, ds1->ds_phys->ds_prev_snap_txg);
968 ASSERT3U(mintxg, ==, ds2->ds_phys->ds_prev_snap_txg);
969 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
970 scn->scn_phys.scn_queue_obj, ds2->ds_object, tx));
971 VERIFY(0 == zap_add_int_key(dp->dp_meta_objset,
972 scn->scn_phys.scn_queue_obj, ds1->ds_object, mintxg, tx));
973 zfs_dbgmsg("clone_swap ds %llu; in queue; "
974 "replacing with %llu",
975 (u_longlong_t)ds2->ds_object,
976 (u_longlong_t)ds1->ds_object);
979 dsl_scan_sync_state(scn, tx);
982 struct enqueue_clones_arg {
989 enqueue_clones_cb(dsl_pool_t *dp, dsl_dataset_t *hds, void *arg)
991 struct enqueue_clones_arg *eca = arg;
994 dsl_scan_t *scn = dp->dp_scan;
996 if (hds->ds_dir->dd_phys->dd_origin_obj != eca->originobj)
999 err = dsl_dataset_hold_obj(dp, hds->ds_object, FTAG, &ds);
1003 while (ds->ds_phys->ds_prev_snap_obj != eca->originobj) {
1004 dsl_dataset_t *prev;
1005 err = dsl_dataset_hold_obj(dp,
1006 ds->ds_phys->ds_prev_snap_obj, FTAG, &prev);
1008 dsl_dataset_rele(ds, FTAG);
1013 VERIFY(zap_add_int_key(dp->dp_meta_objset,
1014 scn->scn_phys.scn_queue_obj, ds->ds_object,
1015 ds->ds_phys->ds_prev_snap_txg, eca->tx) == 0);
1016 dsl_dataset_rele(ds, FTAG);
1021 dsl_scan_visitds(dsl_scan_t *scn, uint64_t dsobj, dmu_tx_t *tx)
1023 dsl_pool_t *dp = scn->scn_dp;
1027 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
1029 if (dmu_objset_from_ds(ds, &os))
1033 * Only the ZIL in the head (non-snapshot) is valid. Even though
1034 * snapshots can have ZIL block pointers (which may be the same
1035 * BP as in the head), they must be ignored. So we traverse the
1036 * ZIL here, rather than in scan_recurse(), because the regular
1037 * snapshot block-sharing rules don't apply to it.
1039 if (DSL_SCAN_IS_SCRUB_RESILVER(scn) && !dsl_dataset_is_snapshot(ds))
1040 dsl_scan_zil(dp, &os->os_zil_header);
1043 * Iterate over the bps in this ds.
1045 dmu_buf_will_dirty(ds->ds_dbuf, tx);
1046 dsl_scan_visit_rootbp(scn, ds, &ds->ds_phys->ds_bp, tx);
1048 char *dsname = kmem_alloc(ZFS_MAXNAMELEN, KM_SLEEP);
1049 dsl_dataset_name(ds, dsname);
1050 zfs_dbgmsg("scanned dataset %llu (%s) with min=%llu max=%llu; "
1052 (longlong_t)dsobj, dsname,
1053 (longlong_t)scn->scn_phys.scn_cur_min_txg,
1054 (longlong_t)scn->scn_phys.scn_cur_max_txg,
1055 (int)scn->scn_pausing);
1056 kmem_free(dsname, ZFS_MAXNAMELEN);
1058 if (scn->scn_pausing)
1062 * We've finished this pass over this dataset.
1066 * If we did not completely visit this dataset, do another pass.
1068 if (scn->scn_phys.scn_flags & DSF_VISIT_DS_AGAIN) {
1069 zfs_dbgmsg("incomplete pass; visiting again");
1070 scn->scn_phys.scn_flags &= ~DSF_VISIT_DS_AGAIN;
1071 VERIFY(zap_add_int_key(dp->dp_meta_objset,
1072 scn->scn_phys.scn_queue_obj, ds->ds_object,
1073 scn->scn_phys.scn_cur_max_txg, tx) == 0);
1078 * Add descendent datasets to work queue.
1080 if (ds->ds_phys->ds_next_snap_obj != 0) {
1081 VERIFY(zap_add_int_key(dp->dp_meta_objset,
1082 scn->scn_phys.scn_queue_obj, ds->ds_phys->ds_next_snap_obj,
1083 ds->ds_phys->ds_creation_txg, tx) == 0);
1085 if (ds->ds_phys->ds_num_children > 1) {
1086 boolean_t usenext = B_FALSE;
1087 if (ds->ds_phys->ds_next_clones_obj != 0) {
1090 * A bug in a previous version of the code could
1091 * cause upgrade_clones_cb() to not set
1092 * ds_next_snap_obj when it should, leading to a
1093 * missing entry. Therefore we can only use the
1094 * next_clones_obj when its count is correct.
1096 int err = zap_count(dp->dp_meta_objset,
1097 ds->ds_phys->ds_next_clones_obj, &count);
1099 count == ds->ds_phys->ds_num_children - 1)
1104 VERIFY0(zap_join_key(dp->dp_meta_objset,
1105 ds->ds_phys->ds_next_clones_obj,
1106 scn->scn_phys.scn_queue_obj,
1107 ds->ds_phys->ds_creation_txg, tx));
1109 struct enqueue_clones_arg eca;
1111 eca.originobj = ds->ds_object;
1113 VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
1114 enqueue_clones_cb, &eca, DS_FIND_CHILDREN));
1119 dsl_dataset_rele(ds, FTAG);
1124 enqueue_cb(dsl_pool_t *dp, dsl_dataset_t *hds, void *arg)
1129 dsl_scan_t *scn = dp->dp_scan;
1131 err = dsl_dataset_hold_obj(dp, hds->ds_object, FTAG, &ds);
1135 while (ds->ds_phys->ds_prev_snap_obj != 0) {
1136 dsl_dataset_t *prev;
1137 err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
1140 dsl_dataset_rele(ds, FTAG);
1145 * If this is a clone, we don't need to worry about it for now.
1147 if (prev->ds_phys->ds_next_snap_obj != ds->ds_object) {
1148 dsl_dataset_rele(ds, FTAG);
1149 dsl_dataset_rele(prev, FTAG);
1152 dsl_dataset_rele(ds, FTAG);
1156 VERIFY(zap_add_int_key(dp->dp_meta_objset, scn->scn_phys.scn_queue_obj,
1157 ds->ds_object, ds->ds_phys->ds_prev_snap_txg, tx) == 0);
1158 dsl_dataset_rele(ds, FTAG);
1163 * Scrub/dedup interaction.
1165 * If there are N references to a deduped block, we don't want to scrub it
1166 * N times -- ideally, we should scrub it exactly once.
1168 * We leverage the fact that the dde's replication class (enum ddt_class)
1169 * is ordered from highest replication class (DDT_CLASS_DITTO) to lowest
1170 * (DDT_CLASS_UNIQUE) so that we may walk the DDT in that order.
1172 * To prevent excess scrubbing, the scrub begins by walking the DDT
1173 * to find all blocks with refcnt > 1, and scrubs each of these once.
1174 * Since there are two replication classes which contain blocks with
1175 * refcnt > 1, we scrub the highest replication class (DDT_CLASS_DITTO) first.
1176 * Finally the top-down scrub begins, only visiting blocks with refcnt == 1.
1178 * There would be nothing more to say if a block's refcnt couldn't change
1179 * during a scrub, but of course it can so we must account for changes
1180 * in a block's replication class.
1182 * Here's an example of what can occur:
1184 * If a block has refcnt > 1 during the DDT scrub phase, but has refcnt == 1
1185 * when visited during the top-down scrub phase, it will be scrubbed twice.
1186 * This negates our scrub optimization, but is otherwise harmless.
1188 * If a block has refcnt == 1 during the DDT scrub phase, but has refcnt > 1
1189 * on each visit during the top-down scrub phase, it will never be scrubbed.
1190 * To catch this, ddt_sync_entry() notifies the scrub code whenever a block's
1191 * reference class transitions to a higher level (i.e DDT_CLASS_UNIQUE to
1192 * DDT_CLASS_DUPLICATE); if it transitions from refcnt == 1 to refcnt > 1
1193 * while a scrub is in progress, it scrubs the block right then.
1196 dsl_scan_ddt(dsl_scan_t *scn, dmu_tx_t *tx)
1198 ddt_bookmark_t *ddb = &scn->scn_phys.scn_ddt_bookmark;
1199 ddt_entry_t dde = { 0 };
1203 while ((error = ddt_walk(scn->scn_dp->dp_spa, ddb, &dde)) == 0) {
1206 if (ddb->ddb_class > scn->scn_phys.scn_ddt_class_max)
1208 dprintf("visiting ddb=%llu/%llu/%llu/%llx\n",
1209 (longlong_t)ddb->ddb_class,
1210 (longlong_t)ddb->ddb_type,
1211 (longlong_t)ddb->ddb_checksum,
1212 (longlong_t)ddb->ddb_cursor);
1214 /* There should be no pending changes to the dedup table */
1215 ddt = scn->scn_dp->dp_spa->spa_ddt[ddb->ddb_checksum];
1216 ASSERT(avl_first(&ddt->ddt_tree) == NULL);
1218 dsl_scan_ddt_entry(scn, ddb->ddb_checksum, &dde, tx);
1221 if (dsl_scan_check_pause(scn, NULL))
1225 zfs_dbgmsg("scanned %llu ddt entries with class_max = %u; pausing=%u",
1226 (longlong_t)n, (int)scn->scn_phys.scn_ddt_class_max,
1227 (int)scn->scn_pausing);
1229 ASSERT(error == 0 || error == ENOENT);
1230 ASSERT(error != ENOENT ||
1231 ddb->ddb_class > scn->scn_phys.scn_ddt_class_max);
1236 dsl_scan_ddt_entry(dsl_scan_t *scn, enum zio_checksum checksum,
1237 ddt_entry_t *dde, dmu_tx_t *tx)
1239 const ddt_key_t *ddk = &dde->dde_key;
1240 ddt_phys_t *ddp = dde->dde_phys;
1242 zbookmark_t zb = { 0 };
1244 if (scn->scn_phys.scn_state != DSS_SCANNING)
1247 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
1248 if (ddp->ddp_phys_birth == 0 ||
1249 ddp->ddp_phys_birth > scn->scn_phys.scn_max_txg)
1251 ddt_bp_create(checksum, ddk, ddp, &bp);
1253 scn->scn_visited_this_txg++;
1254 scan_funcs[scn->scn_phys.scn_func](scn->scn_dp, &bp, &zb);
1259 dsl_scan_visit(dsl_scan_t *scn, dmu_tx_t *tx)
1261 dsl_pool_t *dp = scn->scn_dp;
1265 if (scn->scn_phys.scn_ddt_bookmark.ddb_class <=
1266 scn->scn_phys.scn_ddt_class_max) {
1267 scn->scn_phys.scn_cur_min_txg = scn->scn_phys.scn_min_txg;
1268 scn->scn_phys.scn_cur_max_txg = scn->scn_phys.scn_max_txg;
1269 dsl_scan_ddt(scn, tx);
1270 if (scn->scn_pausing)
1274 if (scn->scn_phys.scn_bookmark.zb_objset == DMU_META_OBJSET) {
1275 /* First do the MOS & ORIGIN */
1277 scn->scn_phys.scn_cur_min_txg = scn->scn_phys.scn_min_txg;
1278 scn->scn_phys.scn_cur_max_txg = scn->scn_phys.scn_max_txg;
1279 dsl_scan_visit_rootbp(scn, NULL,
1280 &dp->dp_meta_rootbp, tx);
1281 spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
1282 if (scn->scn_pausing)
1285 if (spa_version(dp->dp_spa) < SPA_VERSION_DSL_SCRUB) {
1286 VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
1287 enqueue_cb, tx, DS_FIND_CHILDREN));
1289 dsl_scan_visitds(scn,
1290 dp->dp_origin_snap->ds_object, tx);
1292 ASSERT(!scn->scn_pausing);
1293 } else if (scn->scn_phys.scn_bookmark.zb_objset !=
1294 ZB_DESTROYED_OBJSET) {
1296 * If we were paused, continue from here. Note if the
1297 * ds we were paused on was deleted, the zb_objset may
1298 * be -1, so we will skip this and find a new objset
1301 dsl_scan_visitds(scn, scn->scn_phys.scn_bookmark.zb_objset, tx);
1302 if (scn->scn_pausing)
1307 * In case we were paused right at the end of the ds, zero the
1308 * bookmark so we don't think that we're still trying to resume.
1310 bzero(&scn->scn_phys.scn_bookmark, sizeof (zbookmark_t));
1312 /* keep pulling things out of the zap-object-as-queue */
1313 while (zap_cursor_init(&zc, dp->dp_meta_objset,
1314 scn->scn_phys.scn_queue_obj),
1315 zap_cursor_retrieve(&zc, &za) == 0) {
1319 dsobj = strtonum(za.za_name, NULL);
1320 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
1321 scn->scn_phys.scn_queue_obj, dsobj, tx));
1323 /* Set up min/max txg */
1324 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
1325 if (za.za_first_integer != 0) {
1326 scn->scn_phys.scn_cur_min_txg =
1327 MAX(scn->scn_phys.scn_min_txg,
1328 za.za_first_integer);
1330 scn->scn_phys.scn_cur_min_txg =
1331 MAX(scn->scn_phys.scn_min_txg,
1332 ds->ds_phys->ds_prev_snap_txg);
1334 scn->scn_phys.scn_cur_max_txg = dsl_scan_ds_maxtxg(ds);
1335 dsl_dataset_rele(ds, FTAG);
1337 dsl_scan_visitds(scn, dsobj, tx);
1338 zap_cursor_fini(&zc);
1339 if (scn->scn_pausing)
1342 zap_cursor_fini(&zc);
1346 dsl_scan_free_should_pause(dsl_scan_t *scn)
1348 uint64_t elapsed_nanosecs;
1350 elapsed_nanosecs = gethrtime() - scn->scn_sync_start_time;
1351 return (elapsed_nanosecs / NANOSEC > zfs_txg_timeout ||
1352 (NSEC2MSEC(elapsed_nanosecs) > zfs_free_min_time_ms &&
1353 txg_sync_waiting(scn->scn_dp)) ||
1354 spa_shutting_down(scn->scn_dp->dp_spa));
1358 dsl_scan_free_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1360 dsl_scan_t *scn = arg;
1362 if (!scn->scn_is_bptree ||
1363 (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_OBJSET)) {
1364 if (dsl_scan_free_should_pause(scn))
1365 return (SET_ERROR(ERESTART));
1368 zio_nowait(zio_free_sync(scn->scn_zio_root, scn->scn_dp->dp_spa,
1369 dmu_tx_get_txg(tx), bp, BP_GET_PSIZE(bp), 0));
1370 dsl_dir_diduse_space(tx->tx_pool->dp_free_dir, DD_USED_HEAD,
1371 -bp_get_dsize_sync(scn->scn_dp->dp_spa, bp),
1372 -BP_GET_PSIZE(bp), -BP_GET_UCSIZE(bp), tx);
1373 scn->scn_visited_this_txg++;
1378 dsl_scan_active(dsl_scan_t *scn)
1380 spa_t *spa = scn->scn_dp->dp_spa;
1381 uint64_t used = 0, comp, uncomp;
1383 if (spa->spa_load_state != SPA_LOAD_NONE)
1385 if (spa_shutting_down(spa))
1388 if (scn->scn_phys.scn_state == DSS_SCANNING ||
1389 scn->scn_async_destroying)
1392 if (spa_version(scn->scn_dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
1393 (void) bpobj_space(&scn->scn_dp->dp_free_bpobj,
1394 &used, &comp, &uncomp);
1400 dsl_scan_sync(dsl_pool_t *dp, dmu_tx_t *tx)
1402 dsl_scan_t *scn = dp->dp_scan;
1403 spa_t *spa = dp->dp_spa;
1407 * Check for scn_restart_txg before checking spa_load_state, so
1408 * that we can restart an old-style scan while the pool is being
1409 * imported (see dsl_scan_init).
1411 if (scn->scn_restart_txg != 0 &&
1412 scn->scn_restart_txg <= tx->tx_txg) {
1413 pool_scan_func_t func = POOL_SCAN_SCRUB;
1414 dsl_scan_done(scn, B_FALSE, tx);
1415 if (vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL))
1416 func = POOL_SCAN_RESILVER;
1417 zfs_dbgmsg("restarting scan func=%u txg=%llu",
1419 dsl_scan_setup_sync(&func, tx);
1422 if (!dsl_scan_active(scn) ||
1423 spa_sync_pass(dp->dp_spa) > 1)
1426 scn->scn_visited_this_txg = 0;
1427 scn->scn_pausing = B_FALSE;
1428 scn->scn_sync_start_time = gethrtime();
1429 spa->spa_scrub_active = B_TRUE;
1432 * First process the free list. If we pause the free, don't do
1433 * any scanning. This ensures that there is no free list when
1434 * we are scanning, so the scan code doesn't have to worry about
1437 if (spa_version(dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
1438 scn->scn_is_bptree = B_FALSE;
1439 scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
1440 NULL, ZIO_FLAG_MUSTSUCCEED);
1441 err = bpobj_iterate(&dp->dp_free_bpobj,
1442 dsl_scan_free_block_cb, scn, tx);
1443 VERIFY3U(0, ==, zio_wait(scn->scn_zio_root));
1445 if (err == 0 && spa_feature_is_active(spa,
1446 &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY])) {
1447 ASSERT(scn->scn_async_destroying);
1448 scn->scn_is_bptree = B_TRUE;
1449 scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
1450 NULL, ZIO_FLAG_MUSTSUCCEED);
1451 err = bptree_iterate(dp->dp_meta_objset,
1452 dp->dp_bptree_obj, B_TRUE, dsl_scan_free_block_cb,
1454 VERIFY0(zio_wait(scn->scn_zio_root));
1457 zfeature_info_t *feat = &spa_feature_table
1458 [SPA_FEATURE_ASYNC_DESTROY];
1459 /* finished; deactivate async destroy feature */
1460 spa_feature_decr(spa, feat, tx);
1461 ASSERT(!spa_feature_is_active(spa, feat));
1462 VERIFY0(zap_remove(dp->dp_meta_objset,
1463 DMU_POOL_DIRECTORY_OBJECT,
1464 DMU_POOL_BPTREE_OBJ, tx));
1465 VERIFY0(bptree_free(dp->dp_meta_objset,
1466 dp->dp_bptree_obj, tx));
1467 dp->dp_bptree_obj = 0;
1468 scn->scn_async_destroying = B_FALSE;
1471 if (scn->scn_visited_this_txg) {
1472 zfs_dbgmsg("freed %llu blocks in %llums from "
1473 "free_bpobj/bptree txg %llu",
1474 (longlong_t)scn->scn_visited_this_txg,
1476 NSEC2MSEC(gethrtime() - scn->scn_sync_start_time),
1477 (longlong_t)tx->tx_txg);
1478 scn->scn_visited_this_txg = 0;
1480 * Re-sync the ddt so that we can further modify
1481 * it when doing bprewrite.
1483 ddt_sync(spa, tx->tx_txg);
1485 if (err == ERESTART)
1489 if (scn->scn_phys.scn_state != DSS_SCANNING)
1492 if (scn->scn_done_txg == tx->tx_txg) {
1493 ASSERT(!scn->scn_pausing);
1494 /* finished with scan. */
1495 zfs_dbgmsg("txg %llu scan complete", tx->tx_txg);
1496 dsl_scan_done(scn, B_TRUE, tx);
1497 ASSERT3U(spa->spa_scrub_inflight, ==, 0);
1498 dsl_scan_sync_state(scn, tx);
1502 if (scn->scn_phys.scn_ddt_bookmark.ddb_class <=
1503 scn->scn_phys.scn_ddt_class_max) {
1504 zfs_dbgmsg("doing scan sync txg %llu; "
1505 "ddt bm=%llu/%llu/%llu/%llx",
1506 (longlong_t)tx->tx_txg,
1507 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_class,
1508 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_type,
1509 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_checksum,
1510 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_cursor);
1511 ASSERT(scn->scn_phys.scn_bookmark.zb_objset == 0);
1512 ASSERT(scn->scn_phys.scn_bookmark.zb_object == 0);
1513 ASSERT(scn->scn_phys.scn_bookmark.zb_level == 0);
1514 ASSERT(scn->scn_phys.scn_bookmark.zb_blkid == 0);
1516 zfs_dbgmsg("doing scan sync txg %llu; bm=%llu/%llu/%llu/%llu",
1517 (longlong_t)tx->tx_txg,
1518 (longlong_t)scn->scn_phys.scn_bookmark.zb_objset,
1519 (longlong_t)scn->scn_phys.scn_bookmark.zb_object,
1520 (longlong_t)scn->scn_phys.scn_bookmark.zb_level,
1521 (longlong_t)scn->scn_phys.scn_bookmark.zb_blkid);
1524 scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
1525 NULL, ZIO_FLAG_CANFAIL);
1526 dsl_pool_config_enter(dp, FTAG);
1527 dsl_scan_visit(scn, tx);
1528 dsl_pool_config_exit(dp, FTAG);
1529 (void) zio_wait(scn->scn_zio_root);
1530 scn->scn_zio_root = NULL;
1532 zfs_dbgmsg("visited %llu blocks in %llums",
1533 (longlong_t)scn->scn_visited_this_txg,
1534 (longlong_t)NSEC2MSEC(gethrtime() - scn->scn_sync_start_time));
1536 if (!scn->scn_pausing) {
1537 scn->scn_done_txg = tx->tx_txg + 1;
1538 zfs_dbgmsg("txg %llu traversal complete, waiting till txg %llu",
1539 tx->tx_txg, scn->scn_done_txg);
1542 if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
1543 mutex_enter(&spa->spa_scrub_lock);
1544 while (spa->spa_scrub_inflight > 0) {
1545 cv_wait(&spa->spa_scrub_io_cv,
1546 &spa->spa_scrub_lock);
1548 mutex_exit(&spa->spa_scrub_lock);
1551 dsl_scan_sync_state(scn, tx);
1555 * This will start a new scan, or restart an existing one.
1558 dsl_resilver_restart(dsl_pool_t *dp, uint64_t txg)
1562 tx = dmu_tx_create_dd(dp->dp_mos_dir);
1563 VERIFY(0 == dmu_tx_assign(tx, TXG_WAIT));
1565 txg = dmu_tx_get_txg(tx);
1566 dp->dp_scan->scn_restart_txg = txg;
1569 dp->dp_scan->scn_restart_txg = txg;
1571 zfs_dbgmsg("restarting resilver txg=%llu", txg);
1575 dsl_scan_resilvering(dsl_pool_t *dp)
1577 return (dp->dp_scan->scn_phys.scn_state == DSS_SCANNING &&
1578 dp->dp_scan->scn_phys.scn_func == POOL_SCAN_RESILVER);
1586 count_block(zfs_all_blkstats_t *zab, const blkptr_t *bp)
1591 * If we resume after a reboot, zab will be NULL; don't record
1592 * incomplete stats in that case.
1597 for (i = 0; i < 4; i++) {
1598 int l = (i < 2) ? BP_GET_LEVEL(bp) : DN_MAX_LEVELS;
1599 int t = (i & 1) ? BP_GET_TYPE(bp) : DMU_OT_TOTAL;
1600 if (t & DMU_OT_NEWTYPE)
1602 zfs_blkstat_t *zb = &zab->zab_type[l][t];
1606 zb->zb_asize += BP_GET_ASIZE(bp);
1607 zb->zb_lsize += BP_GET_LSIZE(bp);
1608 zb->zb_psize += BP_GET_PSIZE(bp);
1609 zb->zb_gangs += BP_COUNT_GANG(bp);
1611 switch (BP_GET_NDVAS(bp)) {
1613 if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
1614 DVA_GET_VDEV(&bp->blk_dva[1]))
1615 zb->zb_ditto_2_of_2_samevdev++;
1618 equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
1619 DVA_GET_VDEV(&bp->blk_dva[1])) +
1620 (DVA_GET_VDEV(&bp->blk_dva[0]) ==
1621 DVA_GET_VDEV(&bp->blk_dva[2])) +
1622 (DVA_GET_VDEV(&bp->blk_dva[1]) ==
1623 DVA_GET_VDEV(&bp->blk_dva[2]));
1625 zb->zb_ditto_2_of_3_samevdev++;
1626 else if (equal == 3)
1627 zb->zb_ditto_3_of_3_samevdev++;
1634 dsl_scan_scrub_done(zio_t *zio)
1636 spa_t *spa = zio->io_spa;
1638 zio_data_buf_free(zio->io_data, zio->io_size);
1640 mutex_enter(&spa->spa_scrub_lock);
1641 spa->spa_scrub_inflight--;
1642 cv_broadcast(&spa->spa_scrub_io_cv);
1644 if (zio->io_error && (zio->io_error != ECKSUM ||
1645 !(zio->io_flags & ZIO_FLAG_SPECULATIVE))) {
1646 spa->spa_dsl_pool->dp_scan->scn_phys.scn_errors++;
1648 mutex_exit(&spa->spa_scrub_lock);
1652 dsl_scan_scrub_cb(dsl_pool_t *dp,
1653 const blkptr_t *bp, const zbookmark_t *zb)
1655 dsl_scan_t *scn = dp->dp_scan;
1656 size_t size = BP_GET_PSIZE(bp);
1657 spa_t *spa = dp->dp_spa;
1658 uint64_t phys_birth = BP_PHYSICAL_BIRTH(bp);
1660 int zio_flags = ZIO_FLAG_SCAN_THREAD | ZIO_FLAG_RAW | ZIO_FLAG_CANFAIL;
1662 unsigned int scan_delay = 0;
1664 if (phys_birth <= scn->scn_phys.scn_min_txg ||
1665 phys_birth >= scn->scn_phys.scn_max_txg)
1668 count_block(dp->dp_blkstats, bp);
1670 ASSERT(DSL_SCAN_IS_SCRUB_RESILVER(scn));
1671 if (scn->scn_phys.scn_func == POOL_SCAN_SCRUB) {
1672 zio_flags |= ZIO_FLAG_SCRUB;
1673 zio_priority = ZIO_PRIORITY_SCRUB;
1675 scan_delay = zfs_scrub_delay;
1677 ASSERT3U(scn->scn_phys.scn_func, ==, POOL_SCAN_RESILVER);
1678 zio_flags |= ZIO_FLAG_RESILVER;
1679 zio_priority = ZIO_PRIORITY_RESILVER;
1681 scan_delay = zfs_resilver_delay;
1684 /* If it's an intent log block, failure is expected. */
1685 if (zb->zb_level == ZB_ZIL_LEVEL)
1686 zio_flags |= ZIO_FLAG_SPECULATIVE;
1688 for (int d = 0; d < BP_GET_NDVAS(bp); d++) {
1689 vdev_t *vd = vdev_lookup_top(spa,
1690 DVA_GET_VDEV(&bp->blk_dva[d]));
1693 * Keep track of how much data we've examined so that
1694 * zpool(1M) status can make useful progress reports.
1696 scn->scn_phys.scn_examined += DVA_GET_ASIZE(&bp->blk_dva[d]);
1697 spa->spa_scan_pass_exam += DVA_GET_ASIZE(&bp->blk_dva[d]);
1699 /* if it's a resilver, this may not be in the target range */
1701 if (DVA_GET_GANG(&bp->blk_dva[d])) {
1703 * Gang members may be spread across multiple
1704 * vdevs, so the best estimate we have is the
1705 * scrub range, which has already been checked.
1706 * XXX -- it would be better to change our
1707 * allocation policy to ensure that all
1708 * gang members reside on the same vdev.
1712 needs_io = vdev_dtl_contains(vd, DTL_PARTIAL,
1718 if (needs_io && !zfs_no_scrub_io) {
1719 vdev_t *rvd = spa->spa_root_vdev;
1720 uint64_t maxinflight = rvd->vdev_children *
1721 MAX(zfs_top_maxinflight, 1);
1722 void *data = zio_data_buf_alloc(size);
1724 mutex_enter(&spa->spa_scrub_lock);
1725 while (spa->spa_scrub_inflight >= maxinflight)
1726 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
1727 spa->spa_scrub_inflight++;
1728 mutex_exit(&spa->spa_scrub_lock);
1731 * If we're seeing recent (zfs_scan_idle) "important" I/Os
1732 * then throttle our workload to limit the impact of a scan.
1734 if (ddi_get_lbolt64() - spa->spa_last_io <= zfs_scan_idle)
1735 delay(MAX((int)scan_delay, 0));
1737 zio_nowait(zio_read(NULL, spa, bp, data, size,
1738 dsl_scan_scrub_done, NULL, zio_priority,
1742 /* do not relocate this block */
1747 dsl_scan(dsl_pool_t *dp, pool_scan_func_t func)
1749 spa_t *spa = dp->dp_spa;
1752 * Purge all vdev caches and probe all devices. We do this here
1753 * rather than in sync context because this requires a writer lock
1754 * on the spa_config lock, which we can't do from sync context. The
1755 * spa_scrub_reopen flag indicates that vdev_open() should not
1756 * attempt to start another scrub.
1758 spa_vdev_state_enter(spa, SCL_NONE);
1759 spa->spa_scrub_reopen = B_TRUE;
1760 vdev_reopen(spa->spa_root_vdev);
1761 spa->spa_scrub_reopen = B_FALSE;
1762 (void) spa_vdev_state_exit(spa, NULL, 0);
1764 return (dsl_sync_task(spa_name(spa), dsl_scan_setup_check,
1765 dsl_scan_setup_sync, &func, 0));