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) 2012 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 dsl_syncfunc_t dsl_scan_cancel_sync;
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);
128 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
129 "scrub_func", sizeof (uint64_t), 1, &f);
132 * There was an old-style scrub in progress. Restart a
133 * new-style scrub from the beginning.
135 scn->scn_restart_txg = txg;
136 zfs_dbgmsg("old-style scrub was in progress; "
137 "restarting new-style scrub in txg %llu",
138 scn->scn_restart_txg);
141 * Load the queue obj from the old location so that it
142 * can be freed by dsl_scan_done().
144 (void) zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
145 "scrub_queue", sizeof (uint64_t), 1,
146 &scn->scn_phys.scn_queue_obj);
148 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
149 DMU_POOL_SCAN, sizeof (uint64_t), SCAN_PHYS_NUMINTS,
156 if (scn->scn_phys.scn_state == DSS_SCANNING &&
157 spa_prev_software_version(dp->dp_spa) < SPA_VERSION_SCAN) {
159 * A new-type scrub was in progress on an old
160 * pool, and the pool was accessed by old
161 * software. Restart from the beginning, since
162 * the old software may have changed the pool in
165 scn->scn_restart_txg = txg;
166 zfs_dbgmsg("new-style scrub was modified "
167 "by old software; restarting in txg %llu",
168 scn->scn_restart_txg);
172 spa_scan_stat_init(spa);
177 dsl_scan_fini(dsl_pool_t *dp)
180 kmem_free(dp->dp_scan, sizeof (dsl_scan_t));
187 dsl_scan_setup_check(void *arg1, void *arg2, dmu_tx_t *tx)
189 dsl_scan_t *scn = arg1;
191 if (scn->scn_phys.scn_state == DSS_SCANNING)
199 dsl_scan_setup_sync(void *arg1, void *arg2, dmu_tx_t *tx)
201 dsl_scan_t *scn = arg1;
202 pool_scan_func_t *funcp = arg2;
203 dmu_object_type_t ot = 0;
204 dsl_pool_t *dp = scn->scn_dp;
205 spa_t *spa = dp->dp_spa;
207 ASSERT(scn->scn_phys.scn_state != DSS_SCANNING);
208 ASSERT(*funcp > POOL_SCAN_NONE && *funcp < POOL_SCAN_FUNCS);
209 bzero(&scn->scn_phys, sizeof (scn->scn_phys));
210 scn->scn_phys.scn_func = *funcp;
211 scn->scn_phys.scn_state = DSS_SCANNING;
212 scn->scn_phys.scn_min_txg = 0;
213 scn->scn_phys.scn_max_txg = tx->tx_txg;
214 scn->scn_phys.scn_ddt_class_max = DDT_CLASSES - 1; /* the entire DDT */
215 scn->scn_phys.scn_start_time = gethrestime_sec();
216 scn->scn_phys.scn_errors = 0;
217 scn->scn_phys.scn_to_examine = spa->spa_root_vdev->vdev_stat.vs_alloc;
218 scn->scn_restart_txg = 0;
219 spa_scan_stat_init(spa);
221 if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
222 scn->scn_phys.scn_ddt_class_max = zfs_scrub_ddt_class_max;
224 /* rewrite all disk labels */
225 vdev_config_dirty(spa->spa_root_vdev);
227 if (vdev_resilver_needed(spa->spa_root_vdev,
228 &scn->scn_phys.scn_min_txg, &scn->scn_phys.scn_max_txg)) {
229 spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_START);
231 spa_event_notify(spa, NULL, ESC_ZFS_SCRUB_START);
234 spa->spa_scrub_started = B_TRUE;
236 * If this is an incremental scrub, limit the DDT scrub phase
237 * to just the auto-ditto class (for correctness); the rest
238 * of the scrub should go faster using top-down pruning.
240 if (scn->scn_phys.scn_min_txg > TXG_INITIAL)
241 scn->scn_phys.scn_ddt_class_max = DDT_CLASS_DITTO;
245 /* back to the generic stuff */
247 if (dp->dp_blkstats == NULL) {
249 kmem_alloc(sizeof (zfs_all_blkstats_t), KM_SLEEP);
251 bzero(dp->dp_blkstats, sizeof (zfs_all_blkstats_t));
253 if (spa_version(spa) < SPA_VERSION_DSL_SCRUB)
254 ot = DMU_OT_ZAP_OTHER;
256 scn->scn_phys.scn_queue_obj = zap_create(dp->dp_meta_objset,
257 ot ? ot : DMU_OT_SCAN_QUEUE, DMU_OT_NONE, 0, tx);
259 dsl_scan_sync_state(scn, tx);
261 spa_history_log_internal(LOG_POOL_SCAN, spa, tx,
262 "func=%u mintxg=%llu maxtxg=%llu",
263 *funcp, scn->scn_phys.scn_min_txg, scn->scn_phys.scn_max_txg);
268 dsl_scan_done(dsl_scan_t *scn, boolean_t complete, dmu_tx_t *tx)
270 static const char *old_names[] = {
272 "scrub_ddt_bookmark",
273 "scrub_ddt_class_max",
282 dsl_pool_t *dp = scn->scn_dp;
283 spa_t *spa = dp->dp_spa;
286 /* Remove any remnants of an old-style scrub. */
287 for (i = 0; old_names[i]; i++) {
288 (void) zap_remove(dp->dp_meta_objset,
289 DMU_POOL_DIRECTORY_OBJECT, old_names[i], tx);
292 if (scn->scn_phys.scn_queue_obj != 0) {
293 VERIFY(0 == dmu_object_free(dp->dp_meta_objset,
294 scn->scn_phys.scn_queue_obj, tx));
295 scn->scn_phys.scn_queue_obj = 0;
299 * If we were "restarted" from a stopped state, don't bother
300 * with anything else.
302 if (scn->scn_phys.scn_state != DSS_SCANNING)
306 scn->scn_phys.scn_state = DSS_FINISHED;
308 scn->scn_phys.scn_state = DSS_CANCELED;
310 spa_history_log_internal(LOG_POOL_SCAN_DONE, spa, tx,
311 "complete=%u", complete);
313 if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
314 mutex_enter(&spa->spa_scrub_lock);
315 while (spa->spa_scrub_inflight > 0) {
316 cv_wait(&spa->spa_scrub_io_cv,
317 &spa->spa_scrub_lock);
319 mutex_exit(&spa->spa_scrub_lock);
320 spa->spa_scrub_started = B_FALSE;
321 spa->spa_scrub_active = B_FALSE;
324 * If the scrub/resilver completed, update all DTLs to
325 * reflect this. Whether it succeeded or not, vacate
326 * all temporary scrub DTLs.
328 vdev_dtl_reassess(spa->spa_root_vdev, tx->tx_txg,
329 complete ? scn->scn_phys.scn_max_txg : 0, B_TRUE);
331 spa_event_notify(spa, NULL, scn->scn_phys.scn_min_txg ?
332 ESC_ZFS_RESILVER_FINISH : ESC_ZFS_SCRUB_FINISH);
334 spa_errlog_rotate(spa);
337 * We may have finished replacing a device.
338 * Let the async thread assess this and handle the detach.
340 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
343 scn->scn_phys.scn_end_time = gethrestime_sec();
348 dsl_scan_cancel_check(void *arg1, void *arg2, dmu_tx_t *tx)
350 dsl_scan_t *scn = arg1;
352 if (scn->scn_phys.scn_state != DSS_SCANNING)
359 dsl_scan_cancel_sync(void *arg1, void *arg2, dmu_tx_t *tx)
361 dsl_scan_t *scn = arg1;
363 dsl_scan_done(scn, B_FALSE, tx);
364 dsl_scan_sync_state(scn, tx);
368 dsl_scan_cancel(dsl_pool_t *dp)
370 boolean_t complete = B_FALSE;
373 err = dsl_sync_task_do(dp, dsl_scan_cancel_check,
374 dsl_scan_cancel_sync, dp->dp_scan, &complete, 3);
378 static void dsl_scan_visitbp(blkptr_t *bp,
379 const zbookmark_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 VERIFY(0 == 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_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 (elapsed_nanosecs / MICROSEC > 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->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->blk_birth <= scn->scn_phys.scn_cur_min_txg)
515 * birth can be < claim_txg if this record's txg is
516 * already txg sync'ed (but this log block contains
517 * other records that are not synced)
519 if (claim_txg == 0 || bp->blk_birth < claim_txg)
522 SET_BOOKMARK(&zb, zh->zh_log.blk_cksum.zc_word[ZIL_ZC_OBJSET],
523 lr->lr_foid, ZB_ZIL_LEVEL,
524 lr->lr_offset / BP_GET_LSIZE(bp));
526 VERIFY(0 == scan_funcs[scn->scn_phys.scn_func](dp, bp, &zb));
532 dsl_scan_zil(dsl_pool_t *dp, zil_header_t *zh)
534 uint64_t claim_txg = zh->zh_claim_txg;
535 zil_scan_arg_t zsa = { dp, zh };
539 * We only want to visit blocks that have been claimed but not yet
540 * replayed (or, in read-only mode, blocks that *would* be claimed).
542 if (claim_txg == 0 && spa_writeable(dp->dp_spa))
545 zilog = zil_alloc(dp->dp_meta_objset, zh);
547 (void) zil_parse(zilog, dsl_scan_zil_block, dsl_scan_zil_record, &zsa,
555 dsl_scan_prefetch(dsl_scan_t *scn, arc_buf_t *buf, blkptr_t *bp,
556 uint64_t objset, uint64_t object, uint64_t blkid)
559 uint32_t flags = ARC_NOWAIT | ARC_PREFETCH;
561 if (zfs_no_scrub_prefetch)
564 if (BP_IS_HOLE(bp) || bp->blk_birth <= scn->scn_phys.scn_min_txg ||
565 (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_DNODE))
568 SET_BOOKMARK(&czb, objset, object, BP_GET_LEVEL(bp), blkid);
570 (void) arc_read(scn->scn_zio_root, scn->scn_dp->dp_spa, bp,
571 NULL, NULL, ZIO_PRIORITY_ASYNC_READ,
572 ZIO_FLAG_CANFAIL | ZIO_FLAG_SCAN_THREAD, &flags, &czb);
576 dsl_scan_check_resume(dsl_scan_t *scn, const dnode_phys_t *dnp,
577 const zbookmark_t *zb)
580 * We never skip over user/group accounting objects (obj<0)
582 if (!ZB_IS_ZERO(&scn->scn_phys.scn_bookmark) &&
583 (int64_t)zb->zb_object >= 0) {
585 * If we already visited this bp & everything below (in
586 * a prior txg sync), don't bother doing it again.
588 if (zbookmark_is_before(dnp, zb, &scn->scn_phys.scn_bookmark))
592 * If we found the block we're trying to resume from, or
593 * we went past it to a different object, zero it out to
594 * indicate that it's OK to start checking for pausing
597 if (bcmp(zb, &scn->scn_phys.scn_bookmark, sizeof (*zb)) == 0 ||
598 zb->zb_object > scn->scn_phys.scn_bookmark.zb_object) {
599 dprintf("resuming at %llx/%llx/%llx/%llx\n",
600 (longlong_t)zb->zb_objset,
601 (longlong_t)zb->zb_object,
602 (longlong_t)zb->zb_level,
603 (longlong_t)zb->zb_blkid);
604 bzero(&scn->scn_phys.scn_bookmark, sizeof (*zb));
611 * Return nonzero on i/o error.
612 * Return new buf to write out in *bufp.
615 dsl_scan_recurse(dsl_scan_t *scn, dsl_dataset_t *ds, dmu_objset_type_t ostype,
616 dnode_phys_t *dnp, const blkptr_t *bp,
617 const zbookmark_t *zb, dmu_tx_t *tx, arc_buf_t **bufp)
619 dsl_pool_t *dp = scn->scn_dp;
620 int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCAN_THREAD;
623 if (BP_GET_LEVEL(bp) > 0) {
624 uint32_t flags = ARC_WAIT;
627 int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
629 err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, bufp,
630 ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
632 scn->scn_phys.scn_errors++;
635 for (i = 0, cbp = (*bufp)->b_data; i < epb; i++, cbp++) {
636 dsl_scan_prefetch(scn, *bufp, cbp, zb->zb_objset,
637 zb->zb_object, zb->zb_blkid * epb + i);
639 for (i = 0, cbp = (*bufp)->b_data; i < epb; i++, cbp++) {
642 SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
644 zb->zb_blkid * epb + i);
645 dsl_scan_visitbp(cbp, &czb, dnp,
646 *bufp, ds, scn, ostype, tx);
648 } else if (BP_GET_TYPE(bp) == DMU_OT_USERGROUP_USED) {
649 uint32_t flags = ARC_WAIT;
651 err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, bufp,
652 ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
654 scn->scn_phys.scn_errors++;
657 } else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) {
658 uint32_t flags = ARC_WAIT;
661 int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
663 err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, bufp,
664 ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
666 scn->scn_phys.scn_errors++;
669 for (i = 0, cdnp = (*bufp)->b_data; i < epb; i++, cdnp++) {
670 for (j = 0; j < cdnp->dn_nblkptr; j++) {
671 blkptr_t *cbp = &cdnp->dn_blkptr[j];
672 dsl_scan_prefetch(scn, *bufp, cbp,
673 zb->zb_objset, zb->zb_blkid * epb + i, j);
676 for (i = 0, cdnp = (*bufp)->b_data; i < epb; i++, cdnp++) {
677 dsl_scan_visitdnode(scn, ds, ostype,
678 cdnp, *bufp, zb->zb_blkid * epb + i, tx);
681 } else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) {
682 uint32_t flags = ARC_WAIT;
685 err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, bufp,
686 ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
688 scn->scn_phys.scn_errors++;
692 osp = (*bufp)->b_data;
694 dsl_scan_visitdnode(scn, ds, osp->os_type,
695 &osp->os_meta_dnode, *bufp, DMU_META_DNODE_OBJECT, tx);
697 if (OBJSET_BUF_HAS_USERUSED(*bufp)) {
699 * We also always visit user/group accounting
700 * objects, and never skip them, even if we are
701 * pausing. This is necessary so that the space
702 * deltas from this txg get integrated.
704 dsl_scan_visitdnode(scn, ds, osp->os_type,
705 &osp->os_groupused_dnode, *bufp,
706 DMU_GROUPUSED_OBJECT, tx);
707 dsl_scan_visitdnode(scn, ds, osp->os_type,
708 &osp->os_userused_dnode, *bufp,
709 DMU_USERUSED_OBJECT, tx);
717 dsl_scan_visitdnode(dsl_scan_t *scn, dsl_dataset_t *ds,
718 dmu_objset_type_t ostype, dnode_phys_t *dnp, arc_buf_t *buf,
719 uint64_t object, dmu_tx_t *tx)
723 for (j = 0; j < dnp->dn_nblkptr; j++) {
726 SET_BOOKMARK(&czb, ds ? ds->ds_object : 0, object,
727 dnp->dn_nlevels - 1, j);
728 dsl_scan_visitbp(&dnp->dn_blkptr[j],
729 &czb, dnp, buf, ds, scn, ostype, tx);
732 if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
734 SET_BOOKMARK(&czb, ds ? ds->ds_object : 0, object,
736 dsl_scan_visitbp(&dnp->dn_spill,
737 &czb, dnp, buf, ds, scn, ostype, tx);
742 * The arguments are in this order because mdb can only print the
743 * first 5; we want them to be useful.
746 dsl_scan_visitbp(blkptr_t *bp, const zbookmark_t *zb,
747 dnode_phys_t *dnp, arc_buf_t *pbuf,
748 dsl_dataset_t *ds, dsl_scan_t *scn, dmu_objset_type_t ostype,
751 dsl_pool_t *dp = scn->scn_dp;
752 arc_buf_t *buf = NULL;
753 blkptr_t bp_toread = *bp;
755 /* ASSERT(pbuf == NULL || arc_released(pbuf)); */
757 if (dsl_scan_check_pause(scn, zb))
760 if (dsl_scan_check_resume(scn, dnp, zb))
763 if (bp->blk_birth == 0)
766 scn->scn_visited_this_txg++;
769 "visiting ds=%p/%llu zb=%llx/%llx/%llx/%llx buf=%p bp=%p",
770 ds, ds ? ds->ds_object : 0,
771 zb->zb_objset, zb->zb_object, zb->zb_level, zb->zb_blkid,
774 if (bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
777 if (dsl_scan_recurse(scn, ds, ostype, dnp, &bp_toread, zb, tx,
782 * If dsl_scan_ddt() has aready visited this block, it will have
783 * already done any translations or scrubbing, so don't call the
786 if (ddt_class_contains(dp->dp_spa,
787 scn->scn_phys.scn_ddt_class_max, bp)) {
793 * If this block is from the future (after cur_max_txg), then we
794 * are doing this on behalf of a deleted snapshot, and we will
795 * revisit the future block on the next pass of this dataset.
796 * Don't scan it now unless we need to because something
797 * under it was modified.
799 if (bp->blk_birth <= scn->scn_phys.scn_cur_max_txg) {
800 scan_funcs[scn->scn_phys.scn_func](dp, bp, zb);
803 (void) arc_buf_remove_ref(buf, &buf);
807 dsl_scan_visit_rootbp(dsl_scan_t *scn, dsl_dataset_t *ds, blkptr_t *bp,
812 SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
813 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
814 dsl_scan_visitbp(bp, &zb, NULL, NULL,
815 ds, scn, DMU_OST_NONE, tx);
817 dprintf_ds(ds, "finished scan%s", "");
821 dsl_scan_ds_destroyed(dsl_dataset_t *ds, dmu_tx_t *tx)
823 dsl_pool_t *dp = ds->ds_dir->dd_pool;
824 dsl_scan_t *scn = dp->dp_scan;
827 if (scn->scn_phys.scn_state != DSS_SCANNING)
830 if (scn->scn_phys.scn_bookmark.zb_objset == ds->ds_object) {
831 if (dsl_dataset_is_snapshot(ds)) {
832 /* Note, scn_cur_{min,max}_txg stays the same. */
833 scn->scn_phys.scn_bookmark.zb_objset =
834 ds->ds_phys->ds_next_snap_obj;
835 zfs_dbgmsg("destroying ds %llu; currently traversing; "
836 "reset zb_objset to %llu",
837 (u_longlong_t)ds->ds_object,
838 (u_longlong_t)ds->ds_phys->ds_next_snap_obj);
839 scn->scn_phys.scn_flags |= DSF_VISIT_DS_AGAIN;
841 SET_BOOKMARK(&scn->scn_phys.scn_bookmark,
842 ZB_DESTROYED_OBJSET, 0, 0, 0);
843 zfs_dbgmsg("destroying ds %llu; currently traversing; "
844 "reset bookmark to -1,0,0,0",
845 (u_longlong_t)ds->ds_object);
847 } else if (zap_lookup_int_key(dp->dp_meta_objset,
848 scn->scn_phys.scn_queue_obj, ds->ds_object, &mintxg) == 0) {
849 ASSERT3U(ds->ds_phys->ds_num_children, <=, 1);
850 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
851 scn->scn_phys.scn_queue_obj, ds->ds_object, tx));
852 if (dsl_dataset_is_snapshot(ds)) {
854 * We keep the same mintxg; it could be >
855 * ds_creation_txg if the previous snapshot was
858 VERIFY(zap_add_int_key(dp->dp_meta_objset,
859 scn->scn_phys.scn_queue_obj,
860 ds->ds_phys->ds_next_snap_obj, mintxg, tx) == 0);
861 zfs_dbgmsg("destroying ds %llu; in queue; "
862 "replacing with %llu",
863 (u_longlong_t)ds->ds_object,
864 (u_longlong_t)ds->ds_phys->ds_next_snap_obj);
866 zfs_dbgmsg("destroying ds %llu; in queue; removing",
867 (u_longlong_t)ds->ds_object);
870 zfs_dbgmsg("destroying ds %llu; ignoring",
871 (u_longlong_t)ds->ds_object);
875 * dsl_scan_sync() should be called after this, and should sync
876 * out our changed state, but just to be safe, do it here.
878 dsl_scan_sync_state(scn, tx);
882 dsl_scan_ds_snapshotted(dsl_dataset_t *ds, dmu_tx_t *tx)
884 dsl_pool_t *dp = ds->ds_dir->dd_pool;
885 dsl_scan_t *scn = dp->dp_scan;
888 if (scn->scn_phys.scn_state != DSS_SCANNING)
891 ASSERT(ds->ds_phys->ds_prev_snap_obj != 0);
893 if (scn->scn_phys.scn_bookmark.zb_objset == ds->ds_object) {
894 scn->scn_phys.scn_bookmark.zb_objset =
895 ds->ds_phys->ds_prev_snap_obj;
896 zfs_dbgmsg("snapshotting ds %llu; currently traversing; "
897 "reset zb_objset to %llu",
898 (u_longlong_t)ds->ds_object,
899 (u_longlong_t)ds->ds_phys->ds_prev_snap_obj);
900 } else if (zap_lookup_int_key(dp->dp_meta_objset,
901 scn->scn_phys.scn_queue_obj, ds->ds_object, &mintxg) == 0) {
902 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
903 scn->scn_phys.scn_queue_obj, ds->ds_object, tx));
904 VERIFY(zap_add_int_key(dp->dp_meta_objset,
905 scn->scn_phys.scn_queue_obj,
906 ds->ds_phys->ds_prev_snap_obj, mintxg, tx) == 0);
907 zfs_dbgmsg("snapshotting ds %llu; in queue; "
908 "replacing with %llu",
909 (u_longlong_t)ds->ds_object,
910 (u_longlong_t)ds->ds_phys->ds_prev_snap_obj);
912 dsl_scan_sync_state(scn, tx);
916 dsl_scan_ds_clone_swapped(dsl_dataset_t *ds1, dsl_dataset_t *ds2, dmu_tx_t *tx)
918 dsl_pool_t *dp = ds1->ds_dir->dd_pool;
919 dsl_scan_t *scn = dp->dp_scan;
922 if (scn->scn_phys.scn_state != DSS_SCANNING)
925 if (scn->scn_phys.scn_bookmark.zb_objset == ds1->ds_object) {
926 scn->scn_phys.scn_bookmark.zb_objset = ds2->ds_object;
927 zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
928 "reset zb_objset to %llu",
929 (u_longlong_t)ds1->ds_object,
930 (u_longlong_t)ds2->ds_object);
931 } else if (scn->scn_phys.scn_bookmark.zb_objset == ds2->ds_object) {
932 scn->scn_phys.scn_bookmark.zb_objset = ds1->ds_object;
933 zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
934 "reset zb_objset to %llu",
935 (u_longlong_t)ds2->ds_object,
936 (u_longlong_t)ds1->ds_object);
939 if (zap_lookup_int_key(dp->dp_meta_objset, scn->scn_phys.scn_queue_obj,
940 ds1->ds_object, &mintxg) == 0) {
943 ASSERT3U(mintxg, ==, ds1->ds_phys->ds_prev_snap_txg);
944 ASSERT3U(mintxg, ==, ds2->ds_phys->ds_prev_snap_txg);
945 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
946 scn->scn_phys.scn_queue_obj, ds1->ds_object, tx));
947 err = zap_add_int_key(dp->dp_meta_objset,
948 scn->scn_phys.scn_queue_obj, ds2->ds_object, mintxg, tx);
949 VERIFY(err == 0 || err == EEXIST);
951 /* Both were there to begin with */
952 VERIFY(0 == zap_add_int_key(dp->dp_meta_objset,
953 scn->scn_phys.scn_queue_obj,
954 ds1->ds_object, mintxg, tx));
956 zfs_dbgmsg("clone_swap ds %llu; in queue; "
957 "replacing with %llu",
958 (u_longlong_t)ds1->ds_object,
959 (u_longlong_t)ds2->ds_object);
960 } else if (zap_lookup_int_key(dp->dp_meta_objset,
961 scn->scn_phys.scn_queue_obj, ds2->ds_object, &mintxg) == 0) {
962 ASSERT3U(mintxg, ==, ds1->ds_phys->ds_prev_snap_txg);
963 ASSERT3U(mintxg, ==, ds2->ds_phys->ds_prev_snap_txg);
964 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
965 scn->scn_phys.scn_queue_obj, ds2->ds_object, tx));
966 VERIFY(0 == zap_add_int_key(dp->dp_meta_objset,
967 scn->scn_phys.scn_queue_obj, ds1->ds_object, mintxg, tx));
968 zfs_dbgmsg("clone_swap ds %llu; in queue; "
969 "replacing with %llu",
970 (u_longlong_t)ds2->ds_object,
971 (u_longlong_t)ds1->ds_object);
974 dsl_scan_sync_state(scn, tx);
977 struct enqueue_clones_arg {
984 enqueue_clones_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
986 struct enqueue_clones_arg *eca = arg;
989 dsl_pool_t *dp = spa->spa_dsl_pool;
990 dsl_scan_t *scn = dp->dp_scan;
992 err = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds);
996 if (ds->ds_dir->dd_phys->dd_origin_obj == eca->originobj) {
997 while (ds->ds_phys->ds_prev_snap_obj != eca->originobj) {
999 err = dsl_dataset_hold_obj(dp,
1000 ds->ds_phys->ds_prev_snap_obj, FTAG, &prev);
1002 dsl_dataset_rele(ds, FTAG);
1007 VERIFY(zap_add_int_key(dp->dp_meta_objset,
1008 scn->scn_phys.scn_queue_obj, ds->ds_object,
1009 ds->ds_phys->ds_prev_snap_txg, eca->tx) == 0);
1011 dsl_dataset_rele(ds, FTAG);
1016 dsl_scan_visitds(dsl_scan_t *scn, uint64_t dsobj, dmu_tx_t *tx)
1018 dsl_pool_t *dp = scn->scn_dp;
1022 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
1024 if (dmu_objset_from_ds(ds, &os))
1028 * Only the ZIL in the head (non-snapshot) is valid. Even though
1029 * snapshots can have ZIL block pointers (which may be the same
1030 * BP as in the head), they must be ignored. So we traverse the
1031 * ZIL here, rather than in scan_recurse(), because the regular
1032 * snapshot block-sharing rules don't apply to it.
1034 if (DSL_SCAN_IS_SCRUB_RESILVER(scn) && !dsl_dataset_is_snapshot(ds))
1035 dsl_scan_zil(dp, &os->os_zil_header);
1038 * Iterate over the bps in this ds.
1040 dmu_buf_will_dirty(ds->ds_dbuf, tx);
1041 dsl_scan_visit_rootbp(scn, ds, &ds->ds_phys->ds_bp, tx);
1043 char *dsname = kmem_alloc(ZFS_MAXNAMELEN, KM_SLEEP);
1044 dsl_dataset_name(ds, dsname);
1045 zfs_dbgmsg("scanned dataset %llu (%s) with min=%llu max=%llu; "
1047 (longlong_t)dsobj, dsname,
1048 (longlong_t)scn->scn_phys.scn_cur_min_txg,
1049 (longlong_t)scn->scn_phys.scn_cur_max_txg,
1050 (int)scn->scn_pausing);
1051 kmem_free(dsname, ZFS_MAXNAMELEN);
1053 if (scn->scn_pausing)
1057 * We've finished this pass over this dataset.
1061 * If we did not completely visit this dataset, do another pass.
1063 if (scn->scn_phys.scn_flags & DSF_VISIT_DS_AGAIN) {
1064 zfs_dbgmsg("incomplete pass; visiting again");
1065 scn->scn_phys.scn_flags &= ~DSF_VISIT_DS_AGAIN;
1066 VERIFY(zap_add_int_key(dp->dp_meta_objset,
1067 scn->scn_phys.scn_queue_obj, ds->ds_object,
1068 scn->scn_phys.scn_cur_max_txg, tx) == 0);
1073 * Add descendent datasets to work queue.
1075 if (ds->ds_phys->ds_next_snap_obj != 0) {
1076 VERIFY(zap_add_int_key(dp->dp_meta_objset,
1077 scn->scn_phys.scn_queue_obj, ds->ds_phys->ds_next_snap_obj,
1078 ds->ds_phys->ds_creation_txg, tx) == 0);
1080 if (ds->ds_phys->ds_num_children > 1) {
1081 boolean_t usenext = B_FALSE;
1082 if (ds->ds_phys->ds_next_clones_obj != 0) {
1085 * A bug in a previous version of the code could
1086 * cause upgrade_clones_cb() to not set
1087 * ds_next_snap_obj when it should, leading to a
1088 * missing entry. Therefore we can only use the
1089 * next_clones_obj when its count is correct.
1091 int err = zap_count(dp->dp_meta_objset,
1092 ds->ds_phys->ds_next_clones_obj, &count);
1094 count == ds->ds_phys->ds_num_children - 1)
1099 VERIFY(zap_join_key(dp->dp_meta_objset,
1100 ds->ds_phys->ds_next_clones_obj,
1101 scn->scn_phys.scn_queue_obj,
1102 ds->ds_phys->ds_creation_txg, tx) == 0);
1104 struct enqueue_clones_arg eca;
1106 eca.originobj = ds->ds_object;
1108 (void) dmu_objset_find_spa(ds->ds_dir->dd_pool->dp_spa,
1109 NULL, enqueue_clones_cb, &eca, DS_FIND_CHILDREN);
1114 dsl_dataset_rele(ds, FTAG);
1119 enqueue_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
1124 dsl_pool_t *dp = spa->spa_dsl_pool;
1125 dsl_scan_t *scn = dp->dp_scan;
1127 err = dsl_dataset_hold_obj(dp, dsobj, 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_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_cur_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 VERIFY(0 == dmu_objset_find_spa(dp->dp_spa,
1283 NULL, 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_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;
1346 elapsed_nanosecs = gethrtime() - scn->scn_sync_start_time;
1347 return (elapsed_nanosecs / NANOSEC > zfs_txg_timeout ||
1348 (elapsed_nanosecs / MICROSEC > zfs_free_min_time_ms &&
1349 txg_sync_waiting(scn->scn_dp)) ||
1350 spa_shutting_down(scn->scn_dp->dp_spa));
1354 dsl_scan_free_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1356 dsl_scan_t *scn = arg;
1358 if (!scn->scn_is_bptree ||
1359 (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_OBJSET)) {
1360 if (dsl_scan_free_should_pause(scn))
1364 zio_nowait(zio_free_sync(scn->scn_zio_root, scn->scn_dp->dp_spa,
1365 dmu_tx_get_txg(tx), bp, BP_GET_PSIZE(bp), 0));
1366 dsl_dir_diduse_space(tx->tx_pool->dp_free_dir, DD_USED_HEAD,
1367 -bp_get_dsize_sync(scn->scn_dp->dp_spa, bp),
1368 -BP_GET_PSIZE(bp), -BP_GET_UCSIZE(bp), tx);
1369 scn->scn_visited_this_txg++;
1374 dsl_scan_active(dsl_scan_t *scn)
1376 spa_t *spa = scn->scn_dp->dp_spa;
1377 uint64_t used = 0, comp, uncomp;
1379 if (spa->spa_load_state != SPA_LOAD_NONE)
1381 if (spa_shutting_down(spa))
1384 if (scn->scn_phys.scn_state == DSS_SCANNING)
1387 if (spa_feature_is_active(spa,
1388 &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY])) {
1391 if (spa_version(scn->scn_dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
1392 (void) bpobj_space(&scn->scn_dp->dp_free_bpobj,
1393 &used, &comp, &uncomp);
1399 dsl_scan_sync(dsl_pool_t *dp, dmu_tx_t *tx)
1401 dsl_scan_t *scn = dp->dp_scan;
1402 spa_t *spa = dp->dp_spa;
1406 * Check for scn_restart_txg before checking spa_load_state, so
1407 * that we can restart an old-style scan while the pool is being
1408 * imported (see dsl_scan_init).
1410 if (scn->scn_restart_txg != 0 &&
1411 scn->scn_restart_txg <= tx->tx_txg) {
1412 pool_scan_func_t func = POOL_SCAN_SCRUB;
1413 dsl_scan_done(scn, B_FALSE, tx);
1414 if (vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL))
1415 func = POOL_SCAN_RESILVER;
1416 zfs_dbgmsg("restarting scan func=%u txg=%llu",
1418 dsl_scan_setup_sync(scn, &func, tx);
1421 if (!dsl_scan_active(scn) ||
1422 spa_sync_pass(dp->dp_spa) > 1)
1425 scn->scn_visited_this_txg = 0;
1426 scn->scn_pausing = B_FALSE;
1427 scn->scn_sync_start_time = gethrtime();
1428 spa->spa_scrub_active = B_TRUE;
1431 * First process the free list. If we pause the free, don't do
1432 * any scanning. This ensures that there is no free list when
1433 * we are scanning, so the scan code doesn't have to worry about
1436 if (spa_version(dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
1437 scn->scn_is_bptree = B_FALSE;
1438 scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
1439 NULL, ZIO_FLAG_MUSTSUCCEED);
1440 err = bpobj_iterate(&dp->dp_free_bpobj,
1441 dsl_scan_free_block_cb, scn, tx);
1442 VERIFY3U(0, ==, zio_wait(scn->scn_zio_root));
1444 if (err == 0 && spa_feature_is_active(spa,
1445 &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY])) {
1446 scn->scn_is_bptree = B_TRUE;
1447 scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
1448 NULL, ZIO_FLAG_MUSTSUCCEED);
1449 err = bptree_iterate(dp->dp_meta_objset,
1450 dp->dp_bptree_obj, B_TRUE, dsl_scan_free_block_cb,
1452 VERIFY3U(0, ==, zio_wait(scn->scn_zio_root));
1456 /* disable async destroy feature */
1457 spa_feature_decr(spa,
1458 &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY], tx);
1459 ASSERT(!spa_feature_is_active(spa,
1460 &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY]));
1461 VERIFY3U(0, ==, zap_remove(dp->dp_meta_objset,
1462 DMU_POOL_DIRECTORY_OBJECT,
1463 DMU_POOL_BPTREE_OBJ, tx));
1464 VERIFY3U(0, ==, bptree_free(dp->dp_meta_objset,
1465 dp->dp_bptree_obj, tx));
1466 dp->dp_bptree_obj = 0;
1468 if (scn->scn_visited_this_txg) {
1469 zfs_dbgmsg("freed %llu blocks in %llums from "
1470 "free_bpobj/bptree txg %llu",
1471 (longlong_t)scn->scn_visited_this_txg,
1473 (gethrtime() - scn->scn_sync_start_time) / MICROSEC,
1474 (longlong_t)tx->tx_txg);
1475 scn->scn_visited_this_txg = 0;
1477 * Re-sync the ddt so that we can further modify
1478 * it when doing bprewrite.
1480 ddt_sync(spa, tx->tx_txg);
1482 if (err == ERESTART)
1486 if (scn->scn_phys.scn_state != DSS_SCANNING)
1489 if (scn->scn_phys.scn_ddt_bookmark.ddb_class <=
1490 scn->scn_phys.scn_ddt_class_max) {
1491 zfs_dbgmsg("doing scan sync txg %llu; "
1492 "ddt bm=%llu/%llu/%llu/%llx",
1493 (longlong_t)tx->tx_txg,
1494 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_class,
1495 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_type,
1496 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_checksum,
1497 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_cursor);
1498 ASSERT(scn->scn_phys.scn_bookmark.zb_objset == 0);
1499 ASSERT(scn->scn_phys.scn_bookmark.zb_object == 0);
1500 ASSERT(scn->scn_phys.scn_bookmark.zb_level == 0);
1501 ASSERT(scn->scn_phys.scn_bookmark.zb_blkid == 0);
1503 zfs_dbgmsg("doing scan sync txg %llu; bm=%llu/%llu/%llu/%llu",
1504 (longlong_t)tx->tx_txg,
1505 (longlong_t)scn->scn_phys.scn_bookmark.zb_objset,
1506 (longlong_t)scn->scn_phys.scn_bookmark.zb_object,
1507 (longlong_t)scn->scn_phys.scn_bookmark.zb_level,
1508 (longlong_t)scn->scn_phys.scn_bookmark.zb_blkid);
1511 scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
1512 NULL, ZIO_FLAG_CANFAIL);
1513 dsl_scan_visit(scn, tx);
1514 (void) zio_wait(scn->scn_zio_root);
1515 scn->scn_zio_root = NULL;
1517 zfs_dbgmsg("visited %llu blocks in %llums",
1518 (longlong_t)scn->scn_visited_this_txg,
1519 (longlong_t)(gethrtime() - scn->scn_sync_start_time) / MICROSEC);
1521 if (!scn->scn_pausing) {
1522 /* finished with scan. */
1523 zfs_dbgmsg("finished scan txg %llu", (longlong_t)tx->tx_txg);
1524 dsl_scan_done(scn, B_TRUE, tx);
1527 if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
1528 mutex_enter(&spa->spa_scrub_lock);
1529 while (spa->spa_scrub_inflight > 0) {
1530 cv_wait(&spa->spa_scrub_io_cv,
1531 &spa->spa_scrub_lock);
1533 mutex_exit(&spa->spa_scrub_lock);
1536 dsl_scan_sync_state(scn, tx);
1540 * This will start a new scan, or restart an existing one.
1543 dsl_resilver_restart(dsl_pool_t *dp, uint64_t txg)
1547 tx = dmu_tx_create_dd(dp->dp_mos_dir);
1548 VERIFY(0 == dmu_tx_assign(tx, TXG_WAIT));
1550 txg = dmu_tx_get_txg(tx);
1551 dp->dp_scan->scn_restart_txg = txg;
1554 dp->dp_scan->scn_restart_txg = txg;
1556 zfs_dbgmsg("restarting resilver txg=%llu", txg);
1560 dsl_scan_resilvering(dsl_pool_t *dp)
1562 return (dp->dp_scan->scn_phys.scn_state == DSS_SCANNING &&
1563 dp->dp_scan->scn_phys.scn_func == POOL_SCAN_RESILVER);
1571 count_block(zfs_all_blkstats_t *zab, const blkptr_t *bp)
1576 * If we resume after a reboot, zab will be NULL; don't record
1577 * incomplete stats in that case.
1582 for (i = 0; i < 4; i++) {
1583 int l = (i < 2) ? BP_GET_LEVEL(bp) : DN_MAX_LEVELS;
1584 int t = (i & 1) ? BP_GET_TYPE(bp) : DMU_OT_TOTAL;
1585 if (t & DMU_OT_NEWTYPE)
1587 zfs_blkstat_t *zb = &zab->zab_type[l][t];
1591 zb->zb_asize += BP_GET_ASIZE(bp);
1592 zb->zb_lsize += BP_GET_LSIZE(bp);
1593 zb->zb_psize += BP_GET_PSIZE(bp);
1594 zb->zb_gangs += BP_COUNT_GANG(bp);
1596 switch (BP_GET_NDVAS(bp)) {
1598 if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
1599 DVA_GET_VDEV(&bp->blk_dva[1]))
1600 zb->zb_ditto_2_of_2_samevdev++;
1603 equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
1604 DVA_GET_VDEV(&bp->blk_dva[1])) +
1605 (DVA_GET_VDEV(&bp->blk_dva[0]) ==
1606 DVA_GET_VDEV(&bp->blk_dva[2])) +
1607 (DVA_GET_VDEV(&bp->blk_dva[1]) ==
1608 DVA_GET_VDEV(&bp->blk_dva[2]));
1610 zb->zb_ditto_2_of_3_samevdev++;
1611 else if (equal == 3)
1612 zb->zb_ditto_3_of_3_samevdev++;
1619 dsl_scan_scrub_done(zio_t *zio)
1621 spa_t *spa = zio->io_spa;
1623 zio_data_buf_free(zio->io_data, zio->io_size);
1625 mutex_enter(&spa->spa_scrub_lock);
1626 spa->spa_scrub_inflight--;
1627 cv_broadcast(&spa->spa_scrub_io_cv);
1629 if (zio->io_error && (zio->io_error != ECKSUM ||
1630 !(zio->io_flags & ZIO_FLAG_SPECULATIVE))) {
1631 spa->spa_dsl_pool->dp_scan->scn_phys.scn_errors++;
1633 mutex_exit(&spa->spa_scrub_lock);
1637 dsl_scan_scrub_cb(dsl_pool_t *dp,
1638 const blkptr_t *bp, const zbookmark_t *zb)
1640 dsl_scan_t *scn = dp->dp_scan;
1641 size_t size = BP_GET_PSIZE(bp);
1642 spa_t *spa = dp->dp_spa;
1643 uint64_t phys_birth = BP_PHYSICAL_BIRTH(bp);
1645 int zio_flags = ZIO_FLAG_SCAN_THREAD | ZIO_FLAG_RAW | ZIO_FLAG_CANFAIL;
1647 unsigned int scan_delay = 0;
1649 if (phys_birth <= scn->scn_phys.scn_min_txg ||
1650 phys_birth >= scn->scn_phys.scn_max_txg)
1653 count_block(dp->dp_blkstats, bp);
1655 ASSERT(DSL_SCAN_IS_SCRUB_RESILVER(scn));
1656 if (scn->scn_phys.scn_func == POOL_SCAN_SCRUB) {
1657 zio_flags |= ZIO_FLAG_SCRUB;
1658 zio_priority = ZIO_PRIORITY_SCRUB;
1660 scan_delay = zfs_scrub_delay;
1661 } else if (scn->scn_phys.scn_func == POOL_SCAN_RESILVER) {
1662 zio_flags |= ZIO_FLAG_RESILVER;
1663 zio_priority = ZIO_PRIORITY_RESILVER;
1665 scan_delay = zfs_resilver_delay;
1668 /* If it's an intent log block, failure is expected. */
1669 if (zb->zb_level == ZB_ZIL_LEVEL)
1670 zio_flags |= ZIO_FLAG_SPECULATIVE;
1672 for (int d = 0; d < BP_GET_NDVAS(bp); d++) {
1673 vdev_t *vd = vdev_lookup_top(spa,
1674 DVA_GET_VDEV(&bp->blk_dva[d]));
1677 * Keep track of how much data we've examined so that
1678 * zpool(1M) status can make useful progress reports.
1680 scn->scn_phys.scn_examined += DVA_GET_ASIZE(&bp->blk_dva[d]);
1681 spa->spa_scan_pass_exam += DVA_GET_ASIZE(&bp->blk_dva[d]);
1683 /* if it's a resilver, this may not be in the target range */
1685 if (DVA_GET_GANG(&bp->blk_dva[d])) {
1687 * Gang members may be spread across multiple
1688 * vdevs, so the best estimate we have is the
1689 * scrub range, which has already been checked.
1690 * XXX -- it would be better to change our
1691 * allocation policy to ensure that all
1692 * gang members reside on the same vdev.
1696 needs_io = vdev_dtl_contains(vd, DTL_PARTIAL,
1702 if (needs_io && !zfs_no_scrub_io) {
1703 vdev_t *rvd = spa->spa_root_vdev;
1704 uint64_t maxinflight = rvd->vdev_children *
1705 MAX(zfs_top_maxinflight, 1);
1706 void *data = zio_data_buf_alloc(size);
1708 mutex_enter(&spa->spa_scrub_lock);
1709 while (spa->spa_scrub_inflight >= maxinflight)
1710 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
1711 spa->spa_scrub_inflight++;
1712 mutex_exit(&spa->spa_scrub_lock);
1715 * If we're seeing recent (zfs_scan_idle) "important" I/Os
1716 * then throttle our workload to limit the impact of a scan.
1718 if (ddi_get_lbolt64() - spa->spa_last_io <= zfs_scan_idle)
1719 delay(MAX((int)scan_delay, 0));
1721 zio_nowait(zio_read(NULL, spa, bp, data, size,
1722 dsl_scan_scrub_done, NULL, zio_priority,
1726 /* do not relocate this block */
1731 dsl_scan(dsl_pool_t *dp, pool_scan_func_t func)
1733 spa_t *spa = dp->dp_spa;
1736 * Purge all vdev caches and probe all devices. We do this here
1737 * rather than in sync context because this requires a writer lock
1738 * on the spa_config lock, which we can't do from sync context. The
1739 * spa_scrub_reopen flag indicates that vdev_open() should not
1740 * attempt to start another scrub.
1742 spa_vdev_state_enter(spa, SCL_NONE);
1743 spa->spa_scrub_reopen = B_TRUE;
1744 vdev_reopen(spa->spa_root_vdev);
1745 spa->spa_scrub_reopen = B_FALSE;
1746 (void) spa_vdev_state_exit(spa, NULL, 0);
1748 return (dsl_sync_task_do(dp, dsl_scan_setup_check,
1749 dsl_scan_setup_sync, dp->dp_scan, &func, 0));