9290 device removal reduces redundancy of mirrors
[unleashed.git] / usr / src / uts / common / fs / zfs / dsl_scan.c
blobfbc1c4d08bc323e7ddbd5b8b3ad7bc0c5570a735
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
22 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
24 * Copyright 2016 Gary Mills
25 * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
26 * Copyright 2017 Joyent, Inc.
27 * Copyright (c) 2017 Datto Inc.
30 #include <sys/dsl_scan.h>
31 #include <sys/dsl_pool.h>
32 #include <sys/dsl_dataset.h>
33 #include <sys/dsl_prop.h>
34 #include <sys/dsl_dir.h>
35 #include <sys/dsl_synctask.h>
36 #include <sys/dnode.h>
37 #include <sys/dmu_tx.h>
38 #include <sys/dmu_objset.h>
39 #include <sys/arc.h>
40 #include <sys/zap.h>
41 #include <sys/zio.h>
42 #include <sys/zfs_context.h>
43 #include <sys/fs/zfs.h>
44 #include <sys/zfs_znode.h>
45 #include <sys/spa_impl.h>
46 #include <sys/vdev_impl.h>
47 #include <sys/zil_impl.h>
48 #include <sys/zio_checksum.h>
49 #include <sys/ddt.h>
50 #include <sys/sa.h>
51 #include <sys/sa_impl.h>
52 #include <sys/zfeature.h>
53 #include <sys/abd.h>
54 #ifdef _KERNEL
55 #include <sys/zfs_vfsops.h>
56 #endif
58 typedef int (scan_cb_t)(dsl_pool_t *, const blkptr_t *,
59 const zbookmark_phys_t *);
61 static scan_cb_t dsl_scan_scrub_cb;
62 static void dsl_scan_cancel_sync(void *, dmu_tx_t *);
63 static void dsl_scan_sync_state(dsl_scan_t *, dmu_tx_t *);
64 static boolean_t dsl_scan_restarting(dsl_scan_t *, dmu_tx_t *);
66 int zfs_top_maxinflight = 32; /* maximum I/Os per top-level */
67 int zfs_resilver_delay = 2; /* number of ticks to delay resilver */
68 int zfs_scrub_delay = 4; /* number of ticks to delay scrub */
69 int zfs_scan_idle = 50; /* idle window in clock ticks */
71 int zfs_scan_min_time_ms = 1000; /* min millisecs to scrub per txg */
72 int zfs_free_min_time_ms = 1000; /* min millisecs to free per txg */
73 int zfs_obsolete_min_time_ms = 500; /* min millisecs to obsolete per txg */
74 int zfs_resilver_min_time_ms = 3000; /* min millisecs to resilver per txg */
75 boolean_t zfs_no_scrub_io = B_FALSE; /* set to disable scrub i/o */
76 boolean_t zfs_no_scrub_prefetch = B_FALSE; /* set to disable scrub prefetch */
77 enum ddt_class zfs_scrub_ddt_class_max = DDT_CLASS_DUPLICATE;
78 int dsl_scan_delay_completion = B_FALSE; /* set to delay scan completion */
79 /* max number of blocks to free in a single TXG */
80 uint64_t zfs_async_block_max_blocks = UINT64_MAX;
82 #define DSL_SCAN_IS_SCRUB_RESILVER(scn) \
83 ((scn)->scn_phys.scn_func == POOL_SCAN_SCRUB || \
84 (scn)->scn_phys.scn_func == POOL_SCAN_RESILVER)
86 extern int zfs_txg_timeout;
89 * Enable/disable the processing of the free_bpobj object.
91 boolean_t zfs_free_bpobj_enabled = B_TRUE;
93 /* the order has to match pool_scan_type */
94 static scan_cb_t *scan_funcs[POOL_SCAN_FUNCS] = {
95 NULL,
96 dsl_scan_scrub_cb, /* POOL_SCAN_SCRUB */
97 dsl_scan_scrub_cb, /* POOL_SCAN_RESILVER */
101 dsl_scan_init(dsl_pool_t *dp, uint64_t txg)
103 int err;
104 dsl_scan_t *scn;
105 spa_t *spa = dp->dp_spa;
106 uint64_t f;
108 scn = dp->dp_scan = kmem_zalloc(sizeof (dsl_scan_t), KM_SLEEP);
109 scn->scn_dp = dp;
112 * It's possible that we're resuming a scan after a reboot so
113 * make sure that the scan_async_destroying flag is initialized
114 * appropriately.
116 ASSERT(!scn->scn_async_destroying);
117 scn->scn_async_destroying = spa_feature_is_active(dp->dp_spa,
118 SPA_FEATURE_ASYNC_DESTROY);
120 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
121 "scrub_func", sizeof (uint64_t), 1, &f);
122 if (err == 0) {
124 * There was an old-style scrub in progress. Restart a
125 * new-style scrub from the beginning.
127 scn->scn_restart_txg = txg;
128 zfs_dbgmsg("old-style scrub was in progress; "
129 "restarting new-style scrub in txg %llu",
130 scn->scn_restart_txg);
133 * Load the queue obj from the old location so that it
134 * can be freed by dsl_scan_done().
136 (void) zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
137 "scrub_queue", sizeof (uint64_t), 1,
138 &scn->scn_phys.scn_queue_obj);
139 } else {
140 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
141 DMU_POOL_SCAN, sizeof (uint64_t), SCAN_PHYS_NUMINTS,
142 &scn->scn_phys);
143 if (err == ENOENT)
144 return (0);
145 else if (err)
146 return (err);
148 if (scn->scn_phys.scn_state == DSS_SCANNING &&
149 spa_prev_software_version(dp->dp_spa) < SPA_VERSION_SCAN) {
151 * A new-type scrub was in progress on an old
152 * pool, and the pool was accessed by old
153 * software. Restart from the beginning, since
154 * the old software may have changed the pool in
155 * the meantime.
157 scn->scn_restart_txg = txg;
158 zfs_dbgmsg("new-style scrub was modified "
159 "by old software; restarting in txg %llu",
160 scn->scn_restart_txg);
164 spa_scan_stat_init(spa);
165 return (0);
168 void
169 dsl_scan_fini(dsl_pool_t *dp)
171 if (dp->dp_scan) {
172 kmem_free(dp->dp_scan, sizeof (dsl_scan_t));
173 dp->dp_scan = NULL;
177 /* ARGSUSED */
178 static int
179 dsl_scan_setup_check(void *arg, dmu_tx_t *tx)
181 dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
183 if (scn->scn_phys.scn_state == DSS_SCANNING)
184 return (SET_ERROR(EBUSY));
186 return (0);
189 static void
190 dsl_scan_setup_sync(void *arg, dmu_tx_t *tx)
192 dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
193 pool_scan_func_t *funcp = arg;
194 dmu_object_type_t ot = 0;
195 dsl_pool_t *dp = scn->scn_dp;
196 spa_t *spa = dp->dp_spa;
198 ASSERT(scn->scn_phys.scn_state != DSS_SCANNING);
199 ASSERT(*funcp > POOL_SCAN_NONE && *funcp < POOL_SCAN_FUNCS);
200 bzero(&scn->scn_phys, sizeof (scn->scn_phys));
201 scn->scn_phys.scn_func = *funcp;
202 scn->scn_phys.scn_state = DSS_SCANNING;
203 scn->scn_phys.scn_min_txg = 0;
204 scn->scn_phys.scn_max_txg = tx->tx_txg;
205 scn->scn_phys.scn_ddt_class_max = DDT_CLASSES - 1; /* the entire DDT */
206 scn->scn_phys.scn_start_time = gethrestime_sec();
207 scn->scn_phys.scn_errors = 0;
208 scn->scn_phys.scn_to_examine = spa->spa_root_vdev->vdev_stat.vs_alloc;
209 scn->scn_restart_txg = 0;
210 scn->scn_done_txg = 0;
211 spa_scan_stat_init(spa);
213 if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
214 scn->scn_phys.scn_ddt_class_max = zfs_scrub_ddt_class_max;
216 /* rewrite all disk labels */
217 vdev_config_dirty(spa->spa_root_vdev);
219 if (vdev_resilver_needed(spa->spa_root_vdev,
220 &scn->scn_phys.scn_min_txg, &scn->scn_phys.scn_max_txg)) {
221 spa_event_notify(spa, NULL, NULL,
222 ESC_ZFS_RESILVER_START);
223 } else {
224 spa_event_notify(spa, NULL, NULL, ESC_ZFS_SCRUB_START);
227 spa->spa_scrub_started = B_TRUE;
229 * If this is an incremental scrub, limit the DDT scrub phase
230 * to just the auto-ditto class (for correctness); the rest
231 * of the scrub should go faster using top-down pruning.
233 if (scn->scn_phys.scn_min_txg > TXG_INITIAL)
234 scn->scn_phys.scn_ddt_class_max = DDT_CLASS_DITTO;
238 /* back to the generic stuff */
240 if (dp->dp_blkstats == NULL) {
241 dp->dp_blkstats =
242 kmem_alloc(sizeof (zfs_all_blkstats_t), KM_SLEEP);
244 bzero(dp->dp_blkstats, sizeof (zfs_all_blkstats_t));
246 if (spa_version(spa) < SPA_VERSION_DSL_SCRUB)
247 ot = DMU_OT_ZAP_OTHER;
249 scn->scn_phys.scn_queue_obj = zap_create(dp->dp_meta_objset,
250 ot ? ot : DMU_OT_SCAN_QUEUE, DMU_OT_NONE, 0, tx);
252 dsl_scan_sync_state(scn, tx);
254 spa_history_log_internal(spa, "scan setup", tx,
255 "func=%u mintxg=%llu maxtxg=%llu",
256 *funcp, scn->scn_phys.scn_min_txg, scn->scn_phys.scn_max_txg);
259 /* ARGSUSED */
260 static void
261 dsl_scan_done(dsl_scan_t *scn, boolean_t complete, dmu_tx_t *tx)
263 static const char *old_names[] = {
264 "scrub_bookmark",
265 "scrub_ddt_bookmark",
266 "scrub_ddt_class_max",
267 "scrub_queue",
268 "scrub_min_txg",
269 "scrub_max_txg",
270 "scrub_func",
271 "scrub_errors",
272 NULL
275 dsl_pool_t *dp = scn->scn_dp;
276 spa_t *spa = dp->dp_spa;
277 int i;
279 /* Remove any remnants of an old-style scrub. */
280 for (i = 0; old_names[i]; i++) {
281 (void) zap_remove(dp->dp_meta_objset,
282 DMU_POOL_DIRECTORY_OBJECT, old_names[i], tx);
285 if (scn->scn_phys.scn_queue_obj != 0) {
286 VERIFY(0 == dmu_object_free(dp->dp_meta_objset,
287 scn->scn_phys.scn_queue_obj, tx));
288 scn->scn_phys.scn_queue_obj = 0;
291 scn->scn_phys.scn_flags &= ~DSF_SCRUB_PAUSED;
294 * If we were "restarted" from a stopped state, don't bother
295 * with anything else.
297 if (scn->scn_phys.scn_state != DSS_SCANNING)
298 return;
300 if (complete)
301 scn->scn_phys.scn_state = DSS_FINISHED;
302 else
303 scn->scn_phys.scn_state = DSS_CANCELED;
305 if (dsl_scan_restarting(scn, tx))
306 spa_history_log_internal(spa, "scan aborted, restarting", tx,
307 "errors=%llu", spa_get_errlog_size(spa));
308 else if (!complete)
309 spa_history_log_internal(spa, "scan cancelled", tx,
310 "errors=%llu", spa_get_errlog_size(spa));
311 else
312 spa_history_log_internal(spa, "scan done", tx,
313 "errors=%llu", spa_get_errlog_size(spa));
315 if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
316 mutex_enter(&spa->spa_scrub_lock);
317 while (spa->spa_scrub_inflight > 0) {
318 cv_wait(&spa->spa_scrub_io_cv,
319 &spa->spa_scrub_lock);
321 mutex_exit(&spa->spa_scrub_lock);
322 spa->spa_scrub_started = B_FALSE;
323 spa->spa_scrub_active = B_FALSE;
326 * If the scrub/resilver completed, update all DTLs to
327 * reflect this. Whether it succeeded or not, vacate
328 * all temporary scrub DTLs.
330 * As the scrub does not currently support traversing
331 * data that have been freed but are part of a checkpoint,
332 * we don't mark the scrub as done in the DTLs as faults
333 * may still exist in those vdevs.
335 if (complete &&
336 !spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) {
337 vdev_dtl_reassess(spa->spa_root_vdev, tx->tx_txg,
338 scn->scn_phys.scn_max_txg, B_TRUE);
340 spa_event_notify(spa, NULL, NULL,
341 scn->scn_phys.scn_min_txg ?
342 ESC_ZFS_RESILVER_FINISH : ESC_ZFS_SCRUB_FINISH);
343 } else {
344 vdev_dtl_reassess(spa->spa_root_vdev, tx->tx_txg,
345 0, B_TRUE);
347 spa_errlog_rotate(spa);
350 * We may have finished replacing a device.
351 * Let the async thread assess this and handle the detach.
353 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
356 scn->scn_phys.scn_end_time = gethrestime_sec();
359 /* ARGSUSED */
360 static int
361 dsl_scan_cancel_check(void *arg, dmu_tx_t *tx)
363 dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
365 if (scn->scn_phys.scn_state != DSS_SCANNING)
366 return (SET_ERROR(ENOENT));
367 return (0);
370 /* ARGSUSED */
371 static void
372 dsl_scan_cancel_sync(void *arg, dmu_tx_t *tx)
374 dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
376 dsl_scan_done(scn, B_FALSE, tx);
377 dsl_scan_sync_state(scn, tx);
378 spa_event_notify(scn->scn_dp->dp_spa, NULL, NULL, ESC_ZFS_SCRUB_ABORT);
382 dsl_scan_cancel(dsl_pool_t *dp)
384 return (dsl_sync_task(spa_name(dp->dp_spa), dsl_scan_cancel_check,
385 dsl_scan_cancel_sync, NULL, 3, ZFS_SPACE_CHECK_RESERVED));
388 boolean_t
389 dsl_scan_is_paused_scrub(const dsl_scan_t *scn)
391 if (dsl_scan_scrubbing(scn->scn_dp) &&
392 scn->scn_phys.scn_flags & DSF_SCRUB_PAUSED)
393 return (B_TRUE);
395 return (B_FALSE);
398 static int
399 dsl_scrub_pause_resume_check(void *arg, dmu_tx_t *tx)
401 pool_scrub_cmd_t *cmd = arg;
402 dsl_pool_t *dp = dmu_tx_pool(tx);
403 dsl_scan_t *scn = dp->dp_scan;
405 if (*cmd == POOL_SCRUB_PAUSE) {
406 /* can't pause a scrub when there is no in-progress scrub */
407 if (!dsl_scan_scrubbing(dp))
408 return (SET_ERROR(ENOENT));
410 /* can't pause a paused scrub */
411 if (dsl_scan_is_paused_scrub(scn))
412 return (SET_ERROR(EBUSY));
413 } else if (*cmd != POOL_SCRUB_NORMAL) {
414 return (SET_ERROR(ENOTSUP));
417 return (0);
420 static void
421 dsl_scrub_pause_resume_sync(void *arg, dmu_tx_t *tx)
423 pool_scrub_cmd_t *cmd = arg;
424 dsl_pool_t *dp = dmu_tx_pool(tx);
425 spa_t *spa = dp->dp_spa;
426 dsl_scan_t *scn = dp->dp_scan;
428 if (*cmd == POOL_SCRUB_PAUSE) {
429 /* can't pause a scrub when there is no in-progress scrub */
430 spa->spa_scan_pass_scrub_pause = gethrestime_sec();
431 scn->scn_phys.scn_flags |= DSF_SCRUB_PAUSED;
432 dsl_scan_sync_state(scn, tx);
433 spa_event_notify(spa, NULL, NULL, ESC_ZFS_SCRUB_PAUSED);
434 } else {
435 ASSERT3U(*cmd, ==, POOL_SCRUB_NORMAL);
436 if (dsl_scan_is_paused_scrub(scn)) {
438 * We need to keep track of how much time we spend
439 * paused per pass so that we can adjust the scrub rate
440 * shown in the output of 'zpool status'
442 spa->spa_scan_pass_scrub_spent_paused +=
443 gethrestime_sec() - spa->spa_scan_pass_scrub_pause;
444 spa->spa_scan_pass_scrub_pause = 0;
445 scn->scn_phys.scn_flags &= ~DSF_SCRUB_PAUSED;
446 dsl_scan_sync_state(scn, tx);
452 * Set scrub pause/resume state if it makes sense to do so
455 dsl_scrub_set_pause_resume(const dsl_pool_t *dp, pool_scrub_cmd_t cmd)
457 return (dsl_sync_task(spa_name(dp->dp_spa),
458 dsl_scrub_pause_resume_check, dsl_scrub_pause_resume_sync, &cmd, 3,
459 ZFS_SPACE_CHECK_RESERVED));
462 boolean_t
463 dsl_scan_scrubbing(const dsl_pool_t *dp)
465 dsl_scan_t *scn = dp->dp_scan;
467 if (scn->scn_phys.scn_state == DSS_SCANNING &&
468 scn->scn_phys.scn_func == POOL_SCAN_SCRUB)
469 return (B_TRUE);
471 return (B_FALSE);
474 static void dsl_scan_visitbp(blkptr_t *bp, const zbookmark_phys_t *zb,
475 dnode_phys_t *dnp, dsl_dataset_t *ds, dsl_scan_t *scn,
476 dmu_objset_type_t ostype, dmu_tx_t *tx);
477 static void dsl_scan_visitdnode(dsl_scan_t *, dsl_dataset_t *ds,
478 dmu_objset_type_t ostype,
479 dnode_phys_t *dnp, uint64_t object, dmu_tx_t *tx);
481 void
482 dsl_free(dsl_pool_t *dp, uint64_t txg, const blkptr_t *bp)
484 zio_free(dp->dp_spa, txg, bp);
487 void
488 dsl_free_sync(zio_t *pio, dsl_pool_t *dp, uint64_t txg, const blkptr_t *bpp)
490 ASSERT(dsl_pool_sync_context(dp));
491 zio_nowait(zio_free_sync(pio, dp->dp_spa, txg, bpp, pio->io_flags));
494 static uint64_t
495 dsl_scan_ds_maxtxg(dsl_dataset_t *ds)
497 uint64_t smt = ds->ds_dir->dd_pool->dp_scan->scn_phys.scn_max_txg;
498 if (ds->ds_is_snapshot)
499 return (MIN(smt, dsl_dataset_phys(ds)->ds_creation_txg));
500 return (smt);
503 static void
504 dsl_scan_sync_state(dsl_scan_t *scn, dmu_tx_t *tx)
506 VERIFY0(zap_update(scn->scn_dp->dp_meta_objset,
507 DMU_POOL_DIRECTORY_OBJECT,
508 DMU_POOL_SCAN, sizeof (uint64_t), SCAN_PHYS_NUMINTS,
509 &scn->scn_phys, tx));
512 extern int zfs_vdev_async_write_active_min_dirty_percent;
514 static boolean_t
515 dsl_scan_check_suspend(dsl_scan_t *scn, const zbookmark_phys_t *zb)
517 /* we never skip user/group accounting objects */
518 if (zb && (int64_t)zb->zb_object < 0)
519 return (B_FALSE);
521 if (scn->scn_suspending)
522 return (B_TRUE); /* we're already suspending */
524 if (!ZB_IS_ZERO(&scn->scn_phys.scn_bookmark))
525 return (B_FALSE); /* we're resuming */
527 /* We only know how to resume from level-0 blocks. */
528 if (zb && zb->zb_level != 0)
529 return (B_FALSE);
532 * We suspend if:
533 * - we have scanned for the maximum time: an entire txg
534 * timeout (default 5 sec)
535 * or
536 * - we have scanned for at least the minimum time (default 1 sec
537 * for scrub, 3 sec for resilver), and either we have sufficient
538 * dirty data that we are starting to write more quickly
539 * (default 30%), or someone is explicitly waiting for this txg
540 * to complete.
541 * or
542 * - the spa is shutting down because this pool is being exported
543 * or the machine is rebooting.
545 int mintime = (scn->scn_phys.scn_func == POOL_SCAN_RESILVER) ?
546 zfs_resilver_min_time_ms : zfs_scan_min_time_ms;
547 uint64_t elapsed_nanosecs = gethrtime() - scn->scn_sync_start_time;
548 int dirty_pct = scn->scn_dp->dp_dirty_total * 100 / zfs_dirty_data_max;
549 if (elapsed_nanosecs / NANOSEC >= zfs_txg_timeout ||
550 (NSEC2MSEC(elapsed_nanosecs) > mintime &&
551 (txg_sync_waiting(scn->scn_dp) ||
552 dirty_pct >= zfs_vdev_async_write_active_min_dirty_percent)) ||
553 spa_shutting_down(scn->scn_dp->dp_spa)) {
554 if (zb) {
555 dprintf("suspending at bookmark %llx/%llx/%llx/%llx\n",
556 (longlong_t)zb->zb_objset,
557 (longlong_t)zb->zb_object,
558 (longlong_t)zb->zb_level,
559 (longlong_t)zb->zb_blkid);
560 scn->scn_phys.scn_bookmark = *zb;
562 dprintf("suspending at DDT bookmark %llx/%llx/%llx/%llx\n",
563 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_class,
564 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_type,
565 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_checksum,
566 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_cursor);
567 scn->scn_suspending = B_TRUE;
568 return (B_TRUE);
570 return (B_FALSE);
573 typedef struct zil_scan_arg {
574 dsl_pool_t *zsa_dp;
575 zil_header_t *zsa_zh;
576 } zil_scan_arg_t;
578 /* ARGSUSED */
579 static int
580 dsl_scan_zil_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
582 zil_scan_arg_t *zsa = arg;
583 dsl_pool_t *dp = zsa->zsa_dp;
584 dsl_scan_t *scn = dp->dp_scan;
585 zil_header_t *zh = zsa->zsa_zh;
586 zbookmark_phys_t zb;
588 if (BP_IS_HOLE(bp) || bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
589 return (0);
592 * One block ("stubby") can be allocated a long time ago; we
593 * want to visit that one because it has been allocated
594 * (on-disk) even if it hasn't been claimed (even though for
595 * scrub there's nothing to do to it).
597 if (claim_txg == 0 && bp->blk_birth >= spa_min_claim_txg(dp->dp_spa))
598 return (0);
600 SET_BOOKMARK(&zb, zh->zh_log.blk_cksum.zc_word[ZIL_ZC_OBJSET],
601 ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, bp->blk_cksum.zc_word[ZIL_ZC_SEQ]);
603 VERIFY(0 == scan_funcs[scn->scn_phys.scn_func](dp, bp, &zb));
604 return (0);
607 /* ARGSUSED */
608 static int
609 dsl_scan_zil_record(zilog_t *zilog, lr_t *lrc, void *arg, uint64_t claim_txg)
611 if (lrc->lrc_txtype == TX_WRITE) {
612 zil_scan_arg_t *zsa = arg;
613 dsl_pool_t *dp = zsa->zsa_dp;
614 dsl_scan_t *scn = dp->dp_scan;
615 zil_header_t *zh = zsa->zsa_zh;
616 lr_write_t *lr = (lr_write_t *)lrc;
617 blkptr_t *bp = &lr->lr_blkptr;
618 zbookmark_phys_t zb;
620 if (BP_IS_HOLE(bp) ||
621 bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
622 return (0);
625 * birth can be < claim_txg if this record's txg is
626 * already txg sync'ed (but this log block contains
627 * other records that are not synced)
629 if (claim_txg == 0 || bp->blk_birth < claim_txg)
630 return (0);
632 SET_BOOKMARK(&zb, zh->zh_log.blk_cksum.zc_word[ZIL_ZC_OBJSET],
633 lr->lr_foid, ZB_ZIL_LEVEL,
634 lr->lr_offset / BP_GET_LSIZE(bp));
636 VERIFY(0 == scan_funcs[scn->scn_phys.scn_func](dp, bp, &zb));
638 return (0);
641 static void
642 dsl_scan_zil(dsl_pool_t *dp, zil_header_t *zh)
644 uint64_t claim_txg = zh->zh_claim_txg;
645 zil_scan_arg_t zsa = { dp, zh };
646 zilog_t *zilog;
648 ASSERT(spa_writeable(dp->dp_spa));
651 * We only want to visit blocks that have been claimed
652 * but not yet replayed.
654 if (claim_txg == 0)
655 return;
657 zilog = zil_alloc(dp->dp_meta_objset, zh);
659 (void) zil_parse(zilog, dsl_scan_zil_block, dsl_scan_zil_record, &zsa,
660 claim_txg);
662 zil_free(zilog);
665 /* ARGSUSED */
666 static void
667 dsl_scan_prefetch(dsl_scan_t *scn, arc_buf_t *buf, blkptr_t *bp,
668 uint64_t objset, uint64_t object, uint64_t blkid)
670 zbookmark_phys_t czb;
671 arc_flags_t flags = ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH;
673 if (zfs_no_scrub_prefetch)
674 return;
676 if (BP_IS_HOLE(bp) || bp->blk_birth <= scn->scn_phys.scn_min_txg ||
677 (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_DNODE))
678 return;
680 SET_BOOKMARK(&czb, objset, object, BP_GET_LEVEL(bp), blkid);
682 (void) arc_read(scn->scn_zio_root, scn->scn_dp->dp_spa, bp,
683 NULL, NULL, ZIO_PRIORITY_ASYNC_READ,
684 ZIO_FLAG_CANFAIL | ZIO_FLAG_SCAN_THREAD, &flags, &czb);
687 static boolean_t
688 dsl_scan_check_resume(dsl_scan_t *scn, const dnode_phys_t *dnp,
689 const zbookmark_phys_t *zb)
692 * We never skip over user/group accounting objects (obj<0)
694 if (!ZB_IS_ZERO(&scn->scn_phys.scn_bookmark) &&
695 (int64_t)zb->zb_object >= 0) {
697 * If we already visited this bp & everything below (in
698 * a prior txg sync), don't bother doing it again.
700 if (zbookmark_subtree_completed(dnp, zb,
701 &scn->scn_phys.scn_bookmark))
702 return (B_TRUE);
705 * If we found the block we're trying to resume from, or
706 * we went past it to a different object, zero it out to
707 * indicate that it's OK to start checking for suspending
708 * again.
710 if (bcmp(zb, &scn->scn_phys.scn_bookmark, sizeof (*zb)) == 0 ||
711 zb->zb_object > scn->scn_phys.scn_bookmark.zb_object) {
712 dprintf("resuming at %llx/%llx/%llx/%llx\n",
713 (longlong_t)zb->zb_objset,
714 (longlong_t)zb->zb_object,
715 (longlong_t)zb->zb_level,
716 (longlong_t)zb->zb_blkid);
717 bzero(&scn->scn_phys.scn_bookmark, sizeof (*zb));
720 return (B_FALSE);
724 * Return nonzero on i/o error.
725 * Return new buf to write out in *bufp.
727 static int
728 dsl_scan_recurse(dsl_scan_t *scn, dsl_dataset_t *ds, dmu_objset_type_t ostype,
729 dnode_phys_t *dnp, const blkptr_t *bp,
730 const zbookmark_phys_t *zb, dmu_tx_t *tx)
732 dsl_pool_t *dp = scn->scn_dp;
733 int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCAN_THREAD;
734 int err;
736 if (BP_GET_LEVEL(bp) > 0) {
737 arc_flags_t flags = ARC_FLAG_WAIT;
738 int i;
739 blkptr_t *cbp;
740 int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
741 arc_buf_t *buf;
743 err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, &buf,
744 ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
745 if (err) {
746 scn->scn_phys.scn_errors++;
747 return (err);
749 for (i = 0, cbp = buf->b_data; i < epb; i++, cbp++) {
750 dsl_scan_prefetch(scn, buf, cbp, zb->zb_objset,
751 zb->zb_object, zb->zb_blkid * epb + i);
753 for (i = 0, cbp = buf->b_data; i < epb; i++, cbp++) {
754 zbookmark_phys_t czb;
756 SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
757 zb->zb_level - 1,
758 zb->zb_blkid * epb + i);
759 dsl_scan_visitbp(cbp, &czb, dnp,
760 ds, scn, ostype, tx);
762 arc_buf_destroy(buf, &buf);
763 } else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) {
764 arc_flags_t flags = ARC_FLAG_WAIT;
765 dnode_phys_t *cdnp;
766 int i, j;
767 int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
768 arc_buf_t *buf;
770 err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, &buf,
771 ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
772 if (err) {
773 scn->scn_phys.scn_errors++;
774 return (err);
776 for (i = 0, cdnp = buf->b_data; i < epb; i++, cdnp++) {
777 for (j = 0; j < cdnp->dn_nblkptr; j++) {
778 blkptr_t *cbp = &cdnp->dn_blkptr[j];
779 dsl_scan_prefetch(scn, buf, cbp,
780 zb->zb_objset, zb->zb_blkid * epb + i, j);
783 for (i = 0, cdnp = buf->b_data; i < epb; i++, cdnp++) {
784 dsl_scan_visitdnode(scn, ds, ostype,
785 cdnp, zb->zb_blkid * epb + i, tx);
788 arc_buf_destroy(buf, &buf);
789 } else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) {
790 arc_flags_t flags = ARC_FLAG_WAIT;
791 objset_phys_t *osp;
792 arc_buf_t *buf;
794 err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, &buf,
795 ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
796 if (err) {
797 scn->scn_phys.scn_errors++;
798 return (err);
801 osp = buf->b_data;
803 dsl_scan_visitdnode(scn, ds, osp->os_type,
804 &osp->os_meta_dnode, DMU_META_DNODE_OBJECT, tx);
806 if (OBJSET_BUF_HAS_USERUSED(buf)) {
808 * We also always visit user/group accounting
809 * objects, and never skip them, even if we are
810 * suspending. This is necessary so that the space
811 * deltas from this txg get integrated.
813 dsl_scan_visitdnode(scn, ds, osp->os_type,
814 &osp->os_groupused_dnode,
815 DMU_GROUPUSED_OBJECT, tx);
816 dsl_scan_visitdnode(scn, ds, osp->os_type,
817 &osp->os_userused_dnode,
818 DMU_USERUSED_OBJECT, tx);
820 arc_buf_destroy(buf, &buf);
823 return (0);
826 static void
827 dsl_scan_visitdnode(dsl_scan_t *scn, dsl_dataset_t *ds,
828 dmu_objset_type_t ostype, dnode_phys_t *dnp,
829 uint64_t object, dmu_tx_t *tx)
831 int j;
833 for (j = 0; j < dnp->dn_nblkptr; j++) {
834 zbookmark_phys_t czb;
836 SET_BOOKMARK(&czb, ds ? ds->ds_object : 0, object,
837 dnp->dn_nlevels - 1, j);
838 dsl_scan_visitbp(&dnp->dn_blkptr[j],
839 &czb, dnp, ds, scn, ostype, tx);
842 if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
843 zbookmark_phys_t czb;
844 SET_BOOKMARK(&czb, ds ? ds->ds_object : 0, object,
845 0, DMU_SPILL_BLKID);
846 dsl_scan_visitbp(&dnp->dn_spill,
847 &czb, dnp, ds, scn, ostype, tx);
852 * The arguments are in this order because mdb can only print the
853 * first 5; we want them to be useful.
855 static void
856 dsl_scan_visitbp(blkptr_t *bp, const zbookmark_phys_t *zb,
857 dnode_phys_t *dnp, dsl_dataset_t *ds, dsl_scan_t *scn,
858 dmu_objset_type_t ostype, dmu_tx_t *tx)
860 dsl_pool_t *dp = scn->scn_dp;
861 arc_buf_t *buf = NULL;
862 blkptr_t bp_toread = *bp;
864 /* ASSERT(pbuf == NULL || arc_released(pbuf)); */
866 if (dsl_scan_check_suspend(scn, zb))
867 return;
869 if (dsl_scan_check_resume(scn, dnp, zb))
870 return;
872 if (BP_IS_HOLE(bp))
873 return;
875 scn->scn_visited_this_txg++;
877 dprintf_bp(bp,
878 "visiting ds=%p/%llu zb=%llx/%llx/%llx/%llx bp=%p",
879 ds, ds ? ds->ds_object : 0,
880 zb->zb_objset, zb->zb_object, zb->zb_level, zb->zb_blkid,
881 bp);
883 if (bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
884 return;
886 if (dsl_scan_recurse(scn, ds, ostype, dnp, &bp_toread, zb, tx) != 0)
887 return;
890 * If dsl_scan_ddt() has already visited this block, it will have
891 * already done any translations or scrubbing, so don't call the
892 * callback again.
894 if (ddt_class_contains(dp->dp_spa,
895 scn->scn_phys.scn_ddt_class_max, bp)) {
896 ASSERT(buf == NULL);
897 return;
901 * If this block is from the future (after cur_max_txg), then we
902 * are doing this on behalf of a deleted snapshot, and we will
903 * revisit the future block on the next pass of this dataset.
904 * Don't scan it now unless we need to because something
905 * under it was modified.
907 if (BP_PHYSICAL_BIRTH(bp) <= scn->scn_phys.scn_cur_max_txg) {
908 scan_funcs[scn->scn_phys.scn_func](dp, bp, zb);
912 static void
913 dsl_scan_visit_rootbp(dsl_scan_t *scn, dsl_dataset_t *ds, blkptr_t *bp,
914 dmu_tx_t *tx)
916 zbookmark_phys_t zb;
918 SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
919 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
920 dsl_scan_visitbp(bp, &zb, NULL,
921 ds, scn, DMU_OST_NONE, tx);
923 dprintf_ds(ds, "finished scan%s", "");
926 void
927 dsl_scan_ds_destroyed(dsl_dataset_t *ds, dmu_tx_t *tx)
929 dsl_pool_t *dp = ds->ds_dir->dd_pool;
930 dsl_scan_t *scn = dp->dp_scan;
931 uint64_t mintxg;
933 if (scn->scn_phys.scn_state != DSS_SCANNING)
934 return;
936 if (scn->scn_phys.scn_bookmark.zb_objset == ds->ds_object) {
937 if (ds->ds_is_snapshot) {
939 * Note:
940 * - scn_cur_{min,max}_txg stays the same.
941 * - Setting the flag is not really necessary if
942 * scn_cur_max_txg == scn_max_txg, because there
943 * is nothing after this snapshot that we care
944 * about. However, we set it anyway and then
945 * ignore it when we retraverse it in
946 * dsl_scan_visitds().
948 scn->scn_phys.scn_bookmark.zb_objset =
949 dsl_dataset_phys(ds)->ds_next_snap_obj;
950 zfs_dbgmsg("destroying ds %llu; currently traversing; "
951 "reset zb_objset to %llu",
952 (u_longlong_t)ds->ds_object,
953 (u_longlong_t)dsl_dataset_phys(ds)->
954 ds_next_snap_obj);
955 scn->scn_phys.scn_flags |= DSF_VISIT_DS_AGAIN;
956 } else {
957 SET_BOOKMARK(&scn->scn_phys.scn_bookmark,
958 ZB_DESTROYED_OBJSET, 0, 0, 0);
959 zfs_dbgmsg("destroying ds %llu; currently traversing; "
960 "reset bookmark to -1,0,0,0",
961 (u_longlong_t)ds->ds_object);
963 } else if (zap_lookup_int_key(dp->dp_meta_objset,
964 scn->scn_phys.scn_queue_obj, ds->ds_object, &mintxg) == 0) {
965 ASSERT3U(dsl_dataset_phys(ds)->ds_num_children, <=, 1);
966 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
967 scn->scn_phys.scn_queue_obj, ds->ds_object, tx));
968 if (ds->ds_is_snapshot) {
970 * We keep the same mintxg; it could be >
971 * ds_creation_txg if the previous snapshot was
972 * deleted too.
974 VERIFY(zap_add_int_key(dp->dp_meta_objset,
975 scn->scn_phys.scn_queue_obj,
976 dsl_dataset_phys(ds)->ds_next_snap_obj,
977 mintxg, tx) == 0);
978 zfs_dbgmsg("destroying ds %llu; in queue; "
979 "replacing with %llu",
980 (u_longlong_t)ds->ds_object,
981 (u_longlong_t)dsl_dataset_phys(ds)->
982 ds_next_snap_obj);
983 } else {
984 zfs_dbgmsg("destroying ds %llu; in queue; removing",
985 (u_longlong_t)ds->ds_object);
990 * dsl_scan_sync() should be called after this, and should sync
991 * out our changed state, but just to be safe, do it here.
993 dsl_scan_sync_state(scn, tx);
996 void
997 dsl_scan_ds_snapshotted(dsl_dataset_t *ds, dmu_tx_t *tx)
999 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1000 dsl_scan_t *scn = dp->dp_scan;
1001 uint64_t mintxg;
1003 if (scn->scn_phys.scn_state != DSS_SCANNING)
1004 return;
1006 ASSERT(dsl_dataset_phys(ds)->ds_prev_snap_obj != 0);
1008 if (scn->scn_phys.scn_bookmark.zb_objset == ds->ds_object) {
1009 scn->scn_phys.scn_bookmark.zb_objset =
1010 dsl_dataset_phys(ds)->ds_prev_snap_obj;
1011 zfs_dbgmsg("snapshotting ds %llu; currently traversing; "
1012 "reset zb_objset to %llu",
1013 (u_longlong_t)ds->ds_object,
1014 (u_longlong_t)dsl_dataset_phys(ds)->ds_prev_snap_obj);
1015 } else if (zap_lookup_int_key(dp->dp_meta_objset,
1016 scn->scn_phys.scn_queue_obj, ds->ds_object, &mintxg) == 0) {
1017 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
1018 scn->scn_phys.scn_queue_obj, ds->ds_object, tx));
1019 VERIFY(zap_add_int_key(dp->dp_meta_objset,
1020 scn->scn_phys.scn_queue_obj,
1021 dsl_dataset_phys(ds)->ds_prev_snap_obj, mintxg, tx) == 0);
1022 zfs_dbgmsg("snapshotting ds %llu; in queue; "
1023 "replacing with %llu",
1024 (u_longlong_t)ds->ds_object,
1025 (u_longlong_t)dsl_dataset_phys(ds)->ds_prev_snap_obj);
1027 dsl_scan_sync_state(scn, tx);
1030 void
1031 dsl_scan_ds_clone_swapped(dsl_dataset_t *ds1, dsl_dataset_t *ds2, dmu_tx_t *tx)
1033 dsl_pool_t *dp = ds1->ds_dir->dd_pool;
1034 dsl_scan_t *scn = dp->dp_scan;
1035 uint64_t mintxg;
1037 if (scn->scn_phys.scn_state != DSS_SCANNING)
1038 return;
1040 if (scn->scn_phys.scn_bookmark.zb_objset == ds1->ds_object) {
1041 scn->scn_phys.scn_bookmark.zb_objset = ds2->ds_object;
1042 zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
1043 "reset zb_objset to %llu",
1044 (u_longlong_t)ds1->ds_object,
1045 (u_longlong_t)ds2->ds_object);
1046 } else if (scn->scn_phys.scn_bookmark.zb_objset == ds2->ds_object) {
1047 scn->scn_phys.scn_bookmark.zb_objset = ds1->ds_object;
1048 zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
1049 "reset zb_objset to %llu",
1050 (u_longlong_t)ds2->ds_object,
1051 (u_longlong_t)ds1->ds_object);
1054 if (zap_lookup_int_key(dp->dp_meta_objset, scn->scn_phys.scn_queue_obj,
1055 ds1->ds_object, &mintxg) == 0) {
1056 int err;
1058 ASSERT3U(mintxg, ==, dsl_dataset_phys(ds1)->ds_prev_snap_txg);
1059 ASSERT3U(mintxg, ==, dsl_dataset_phys(ds2)->ds_prev_snap_txg);
1060 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
1061 scn->scn_phys.scn_queue_obj, ds1->ds_object, tx));
1062 err = zap_add_int_key(dp->dp_meta_objset,
1063 scn->scn_phys.scn_queue_obj, ds2->ds_object, mintxg, tx);
1064 VERIFY(err == 0 || err == EEXIST);
1065 if (err == EEXIST) {
1066 /* Both were there to begin with */
1067 VERIFY(0 == zap_add_int_key(dp->dp_meta_objset,
1068 scn->scn_phys.scn_queue_obj,
1069 ds1->ds_object, mintxg, tx));
1071 zfs_dbgmsg("clone_swap ds %llu; in queue; "
1072 "replacing with %llu",
1073 (u_longlong_t)ds1->ds_object,
1074 (u_longlong_t)ds2->ds_object);
1075 } else if (zap_lookup_int_key(dp->dp_meta_objset,
1076 scn->scn_phys.scn_queue_obj, ds2->ds_object, &mintxg) == 0) {
1077 ASSERT3U(mintxg, ==, dsl_dataset_phys(ds1)->ds_prev_snap_txg);
1078 ASSERT3U(mintxg, ==, dsl_dataset_phys(ds2)->ds_prev_snap_txg);
1079 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
1080 scn->scn_phys.scn_queue_obj, ds2->ds_object, tx));
1081 VERIFY(0 == zap_add_int_key(dp->dp_meta_objset,
1082 scn->scn_phys.scn_queue_obj, ds1->ds_object, mintxg, tx));
1083 zfs_dbgmsg("clone_swap ds %llu; in queue; "
1084 "replacing with %llu",
1085 (u_longlong_t)ds2->ds_object,
1086 (u_longlong_t)ds1->ds_object);
1089 dsl_scan_sync_state(scn, tx);
1092 struct enqueue_clones_arg {
1093 dmu_tx_t *tx;
1094 uint64_t originobj;
1097 /* ARGSUSED */
1098 static int
1099 enqueue_clones_cb(dsl_pool_t *dp, dsl_dataset_t *hds, void *arg)
1101 struct enqueue_clones_arg *eca = arg;
1102 dsl_dataset_t *ds;
1103 int err;
1104 dsl_scan_t *scn = dp->dp_scan;
1106 if (dsl_dir_phys(hds->ds_dir)->dd_origin_obj != eca->originobj)
1107 return (0);
1109 err = dsl_dataset_hold_obj(dp, hds->ds_object, FTAG, &ds);
1110 if (err)
1111 return (err);
1113 while (dsl_dataset_phys(ds)->ds_prev_snap_obj != eca->originobj) {
1114 dsl_dataset_t *prev;
1115 err = dsl_dataset_hold_obj(dp,
1116 dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev);
1118 dsl_dataset_rele(ds, FTAG);
1119 if (err)
1120 return (err);
1121 ds = prev;
1123 VERIFY(zap_add_int_key(dp->dp_meta_objset,
1124 scn->scn_phys.scn_queue_obj, ds->ds_object,
1125 dsl_dataset_phys(ds)->ds_prev_snap_txg, eca->tx) == 0);
1126 dsl_dataset_rele(ds, FTAG);
1127 return (0);
1130 static void
1131 dsl_scan_visitds(dsl_scan_t *scn, uint64_t dsobj, dmu_tx_t *tx)
1133 dsl_pool_t *dp = scn->scn_dp;
1134 dsl_dataset_t *ds;
1136 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
1138 if (scn->scn_phys.scn_cur_min_txg >=
1139 scn->scn_phys.scn_max_txg) {
1141 * This can happen if this snapshot was created after the
1142 * scan started, and we already completed a previous snapshot
1143 * that was created after the scan started. This snapshot
1144 * only references blocks with:
1146 * birth < our ds_creation_txg
1147 * cur_min_txg is no less than ds_creation_txg.
1148 * We have already visited these blocks.
1149 * or
1150 * birth > scn_max_txg
1151 * The scan requested not to visit these blocks.
1153 * Subsequent snapshots (and clones) can reference our
1154 * blocks, or blocks with even higher birth times.
1155 * Therefore we do not need to visit them either,
1156 * so we do not add them to the work queue.
1158 * Note that checking for cur_min_txg >= cur_max_txg
1159 * is not sufficient, because in that case we may need to
1160 * visit subsequent snapshots. This happens when min_txg > 0,
1161 * which raises cur_min_txg. In this case we will visit
1162 * this dataset but skip all of its blocks, because the
1163 * rootbp's birth time is < cur_min_txg. Then we will
1164 * add the next snapshots/clones to the work queue.
1166 char *dsname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1167 dsl_dataset_name(ds, dsname);
1168 zfs_dbgmsg("scanning dataset %llu (%s) is unnecessary because "
1169 "cur_min_txg (%llu) >= max_txg (%llu)",
1170 dsobj, dsname,
1171 scn->scn_phys.scn_cur_min_txg,
1172 scn->scn_phys.scn_max_txg);
1173 kmem_free(dsname, MAXNAMELEN);
1175 goto out;
1179 * Only the ZIL in the head (non-snapshot) is valid. Even though
1180 * snapshots can have ZIL block pointers (which may be the same
1181 * BP as in the head), they must be ignored. In addition, $ORIGIN
1182 * doesn't have a objset (i.e. its ds_bp is a hole) so we don't
1183 * need to look for a ZIL in it either. So we traverse the ZIL here,
1184 * rather than in scan_recurse(), because the regular snapshot
1185 * block-sharing rules don't apply to it.
1187 if (DSL_SCAN_IS_SCRUB_RESILVER(scn) && !dsl_dataset_is_snapshot(ds) &&
1188 ds->ds_dir != dp->dp_origin_snap->ds_dir) {
1189 objset_t *os;
1190 if (dmu_objset_from_ds(ds, &os) != 0) {
1191 goto out;
1193 dsl_scan_zil(dp, &os->os_zil_header);
1197 * Iterate over the bps in this ds.
1199 dmu_buf_will_dirty(ds->ds_dbuf, tx);
1200 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
1201 dsl_scan_visit_rootbp(scn, ds, &dsl_dataset_phys(ds)->ds_bp, tx);
1202 rrw_exit(&ds->ds_bp_rwlock, FTAG);
1204 char *dsname = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP);
1205 dsl_dataset_name(ds, dsname);
1206 zfs_dbgmsg("scanned dataset %llu (%s) with min=%llu max=%llu; "
1207 "suspending=%u",
1208 (longlong_t)dsobj, dsname,
1209 (longlong_t)scn->scn_phys.scn_cur_min_txg,
1210 (longlong_t)scn->scn_phys.scn_cur_max_txg,
1211 (int)scn->scn_suspending);
1212 kmem_free(dsname, ZFS_MAX_DATASET_NAME_LEN);
1214 if (scn->scn_suspending)
1215 goto out;
1218 * We've finished this pass over this dataset.
1222 * If we did not completely visit this dataset, do another pass.
1224 if (scn->scn_phys.scn_flags & DSF_VISIT_DS_AGAIN) {
1225 zfs_dbgmsg("incomplete pass; visiting again");
1226 scn->scn_phys.scn_flags &= ~DSF_VISIT_DS_AGAIN;
1227 VERIFY(zap_add_int_key(dp->dp_meta_objset,
1228 scn->scn_phys.scn_queue_obj, ds->ds_object,
1229 scn->scn_phys.scn_cur_max_txg, tx) == 0);
1230 goto out;
1234 * Add descendent datasets to work queue.
1236 if (dsl_dataset_phys(ds)->ds_next_snap_obj != 0) {
1237 VERIFY(zap_add_int_key(dp->dp_meta_objset,
1238 scn->scn_phys.scn_queue_obj,
1239 dsl_dataset_phys(ds)->ds_next_snap_obj,
1240 dsl_dataset_phys(ds)->ds_creation_txg, tx) == 0);
1242 if (dsl_dataset_phys(ds)->ds_num_children > 1) {
1243 boolean_t usenext = B_FALSE;
1244 if (dsl_dataset_phys(ds)->ds_next_clones_obj != 0) {
1245 uint64_t count;
1247 * A bug in a previous version of the code could
1248 * cause upgrade_clones_cb() to not set
1249 * ds_next_snap_obj when it should, leading to a
1250 * missing entry. Therefore we can only use the
1251 * next_clones_obj when its count is correct.
1253 int err = zap_count(dp->dp_meta_objset,
1254 dsl_dataset_phys(ds)->ds_next_clones_obj, &count);
1255 if (err == 0 &&
1256 count == dsl_dataset_phys(ds)->ds_num_children - 1)
1257 usenext = B_TRUE;
1260 if (usenext) {
1261 VERIFY0(zap_join_key(dp->dp_meta_objset,
1262 dsl_dataset_phys(ds)->ds_next_clones_obj,
1263 scn->scn_phys.scn_queue_obj,
1264 dsl_dataset_phys(ds)->ds_creation_txg, tx));
1265 } else {
1266 struct enqueue_clones_arg eca;
1267 eca.tx = tx;
1268 eca.originobj = ds->ds_object;
1270 VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
1271 enqueue_clones_cb, &eca, DS_FIND_CHILDREN));
1275 out:
1276 dsl_dataset_rele(ds, FTAG);
1279 /* ARGSUSED */
1280 static int
1281 enqueue_cb(dsl_pool_t *dp, dsl_dataset_t *hds, void *arg)
1283 dmu_tx_t *tx = arg;
1284 dsl_dataset_t *ds;
1285 int err;
1286 dsl_scan_t *scn = dp->dp_scan;
1288 err = dsl_dataset_hold_obj(dp, hds->ds_object, FTAG, &ds);
1289 if (err)
1290 return (err);
1292 while (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
1293 dsl_dataset_t *prev;
1294 err = dsl_dataset_hold_obj(dp,
1295 dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev);
1296 if (err) {
1297 dsl_dataset_rele(ds, FTAG);
1298 return (err);
1302 * If this is a clone, we don't need to worry about it for now.
1304 if (dsl_dataset_phys(prev)->ds_next_snap_obj != ds->ds_object) {
1305 dsl_dataset_rele(ds, FTAG);
1306 dsl_dataset_rele(prev, FTAG);
1307 return (0);
1309 dsl_dataset_rele(ds, FTAG);
1310 ds = prev;
1313 VERIFY(zap_add_int_key(dp->dp_meta_objset, scn->scn_phys.scn_queue_obj,
1314 ds->ds_object, dsl_dataset_phys(ds)->ds_prev_snap_txg, tx) == 0);
1315 dsl_dataset_rele(ds, FTAG);
1316 return (0);
1320 * Scrub/dedup interaction.
1322 * If there are N references to a deduped block, we don't want to scrub it
1323 * N times -- ideally, we should scrub it exactly once.
1325 * We leverage the fact that the dde's replication class (enum ddt_class)
1326 * is ordered from highest replication class (DDT_CLASS_DITTO) to lowest
1327 * (DDT_CLASS_UNIQUE) so that we may walk the DDT in that order.
1329 * To prevent excess scrubbing, the scrub begins by walking the DDT
1330 * to find all blocks with refcnt > 1, and scrubs each of these once.
1331 * Since there are two replication classes which contain blocks with
1332 * refcnt > 1, we scrub the highest replication class (DDT_CLASS_DITTO) first.
1333 * Finally the top-down scrub begins, only visiting blocks with refcnt == 1.
1335 * There would be nothing more to say if a block's refcnt couldn't change
1336 * during a scrub, but of course it can so we must account for changes
1337 * in a block's replication class.
1339 * Here's an example of what can occur:
1341 * If a block has refcnt > 1 during the DDT scrub phase, but has refcnt == 1
1342 * when visited during the top-down scrub phase, it will be scrubbed twice.
1343 * This negates our scrub optimization, but is otherwise harmless.
1345 * If a block has refcnt == 1 during the DDT scrub phase, but has refcnt > 1
1346 * on each visit during the top-down scrub phase, it will never be scrubbed.
1347 * To catch this, ddt_sync_entry() notifies the scrub code whenever a block's
1348 * reference class transitions to a higher level (i.e DDT_CLASS_UNIQUE to
1349 * DDT_CLASS_DUPLICATE); if it transitions from refcnt == 1 to refcnt > 1
1350 * while a scrub is in progress, it scrubs the block right then.
1352 static void
1353 dsl_scan_ddt(dsl_scan_t *scn, dmu_tx_t *tx)
1355 ddt_bookmark_t *ddb = &scn->scn_phys.scn_ddt_bookmark;
1356 ddt_entry_t dde = { 0 };
1357 int error;
1358 uint64_t n = 0;
1360 while ((error = ddt_walk(scn->scn_dp->dp_spa, ddb, &dde)) == 0) {
1361 ddt_t *ddt;
1363 if (ddb->ddb_class > scn->scn_phys.scn_ddt_class_max)
1364 break;
1365 dprintf("visiting ddb=%llu/%llu/%llu/%llx\n",
1366 (longlong_t)ddb->ddb_class,
1367 (longlong_t)ddb->ddb_type,
1368 (longlong_t)ddb->ddb_checksum,
1369 (longlong_t)ddb->ddb_cursor);
1371 /* There should be no pending changes to the dedup table */
1372 ddt = scn->scn_dp->dp_spa->spa_ddt[ddb->ddb_checksum];
1373 ASSERT(avl_first(&ddt->ddt_tree) == NULL);
1375 dsl_scan_ddt_entry(scn, ddb->ddb_checksum, &dde, tx);
1376 n++;
1378 if (dsl_scan_check_suspend(scn, NULL))
1379 break;
1382 zfs_dbgmsg("scanned %llu ddt entries with class_max = %u; "
1383 "suspending=%u", (longlong_t)n,
1384 (int)scn->scn_phys.scn_ddt_class_max, (int)scn->scn_suspending);
1386 ASSERT(error == 0 || error == ENOENT);
1387 ASSERT(error != ENOENT ||
1388 ddb->ddb_class > scn->scn_phys.scn_ddt_class_max);
1391 /* ARGSUSED */
1392 void
1393 dsl_scan_ddt_entry(dsl_scan_t *scn, enum zio_checksum checksum,
1394 ddt_entry_t *dde, dmu_tx_t *tx)
1396 const ddt_key_t *ddk = &dde->dde_key;
1397 ddt_phys_t *ddp = dde->dde_phys;
1398 blkptr_t bp;
1399 zbookmark_phys_t zb = { 0 };
1401 if (scn->scn_phys.scn_state != DSS_SCANNING)
1402 return;
1404 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
1405 if (ddp->ddp_phys_birth == 0 ||
1406 ddp->ddp_phys_birth > scn->scn_phys.scn_max_txg)
1407 continue;
1408 ddt_bp_create(checksum, ddk, ddp, &bp);
1410 scn->scn_visited_this_txg++;
1411 scan_funcs[scn->scn_phys.scn_func](scn->scn_dp, &bp, &zb);
1415 static void
1416 dsl_scan_visit(dsl_scan_t *scn, dmu_tx_t *tx)
1418 dsl_pool_t *dp = scn->scn_dp;
1419 zap_cursor_t zc;
1420 zap_attribute_t za;
1422 if (scn->scn_phys.scn_ddt_bookmark.ddb_class <=
1423 scn->scn_phys.scn_ddt_class_max) {
1424 scn->scn_phys.scn_cur_min_txg = scn->scn_phys.scn_min_txg;
1425 scn->scn_phys.scn_cur_max_txg = scn->scn_phys.scn_max_txg;
1426 dsl_scan_ddt(scn, tx);
1427 if (scn->scn_suspending)
1428 return;
1431 if (scn->scn_phys.scn_bookmark.zb_objset == DMU_META_OBJSET) {
1432 /* First do the MOS & ORIGIN */
1434 scn->scn_phys.scn_cur_min_txg = scn->scn_phys.scn_min_txg;
1435 scn->scn_phys.scn_cur_max_txg = scn->scn_phys.scn_max_txg;
1436 dsl_scan_visit_rootbp(scn, NULL,
1437 &dp->dp_meta_rootbp, tx);
1438 spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
1439 if (scn->scn_suspending)
1440 return;
1442 if (spa_version(dp->dp_spa) < SPA_VERSION_DSL_SCRUB) {
1443 VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
1444 enqueue_cb, tx, DS_FIND_CHILDREN));
1445 } else {
1446 dsl_scan_visitds(scn,
1447 dp->dp_origin_snap->ds_object, tx);
1449 ASSERT(!scn->scn_suspending);
1450 } else if (scn->scn_phys.scn_bookmark.zb_objset !=
1451 ZB_DESTROYED_OBJSET) {
1453 * If we were suspended, continue from here. Note if the
1454 * ds we were suspended on was deleted, the zb_objset may
1455 * be -1, so we will skip this and find a new objset
1456 * below.
1458 dsl_scan_visitds(scn, scn->scn_phys.scn_bookmark.zb_objset, tx);
1459 if (scn->scn_suspending)
1460 return;
1464 * In case we were suspended right at the end of the ds, zero the
1465 * bookmark so we don't think that we're still trying to resume.
1467 bzero(&scn->scn_phys.scn_bookmark, sizeof (zbookmark_phys_t));
1469 /* keep pulling things out of the zap-object-as-queue */
1470 while (zap_cursor_init(&zc, dp->dp_meta_objset,
1471 scn->scn_phys.scn_queue_obj),
1472 zap_cursor_retrieve(&zc, &za) == 0) {
1473 dsl_dataset_t *ds;
1474 uint64_t dsobj;
1476 dsobj = zfs_strtonum(za.za_name, NULL);
1477 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
1478 scn->scn_phys.scn_queue_obj, dsobj, tx));
1480 /* Set up min/max txg */
1481 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
1482 if (za.za_first_integer != 0) {
1483 scn->scn_phys.scn_cur_min_txg =
1484 MAX(scn->scn_phys.scn_min_txg,
1485 za.za_first_integer);
1486 } else {
1487 scn->scn_phys.scn_cur_min_txg =
1488 MAX(scn->scn_phys.scn_min_txg,
1489 dsl_dataset_phys(ds)->ds_prev_snap_txg);
1491 scn->scn_phys.scn_cur_max_txg = dsl_scan_ds_maxtxg(ds);
1492 dsl_dataset_rele(ds, FTAG);
1494 dsl_scan_visitds(scn, dsobj, tx);
1495 zap_cursor_fini(&zc);
1496 if (scn->scn_suspending)
1497 return;
1499 zap_cursor_fini(&zc);
1502 static boolean_t
1503 dsl_scan_async_block_should_pause(dsl_scan_t *scn)
1505 uint64_t elapsed_nanosecs;
1507 if (zfs_recover)
1508 return (B_FALSE);
1510 if (scn->scn_visited_this_txg >= zfs_async_block_max_blocks)
1511 return (B_TRUE);
1513 elapsed_nanosecs = gethrtime() - scn->scn_sync_start_time;
1514 return (elapsed_nanosecs / NANOSEC > zfs_txg_timeout ||
1515 (NSEC2MSEC(elapsed_nanosecs) > scn->scn_async_block_min_time_ms &&
1516 txg_sync_waiting(scn->scn_dp)) ||
1517 spa_shutting_down(scn->scn_dp->dp_spa));
1520 static int
1521 dsl_scan_free_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1523 dsl_scan_t *scn = arg;
1525 if (!scn->scn_is_bptree ||
1526 (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_OBJSET)) {
1527 if (dsl_scan_async_block_should_pause(scn))
1528 return (SET_ERROR(ERESTART));
1531 zio_nowait(zio_free_sync(scn->scn_zio_root, scn->scn_dp->dp_spa,
1532 dmu_tx_get_txg(tx), bp, 0));
1533 dsl_dir_diduse_space(tx->tx_pool->dp_free_dir, DD_USED_HEAD,
1534 -bp_get_dsize_sync(scn->scn_dp->dp_spa, bp),
1535 -BP_GET_PSIZE(bp), -BP_GET_UCSIZE(bp), tx);
1536 scn->scn_visited_this_txg++;
1537 return (0);
1540 static int
1541 dsl_scan_obsolete_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1543 dsl_scan_t *scn = arg;
1544 const dva_t *dva = &bp->blk_dva[0];
1546 if (dsl_scan_async_block_should_pause(scn))
1547 return (SET_ERROR(ERESTART));
1549 spa_vdev_indirect_mark_obsolete(scn->scn_dp->dp_spa,
1550 DVA_GET_VDEV(dva), DVA_GET_OFFSET(dva),
1551 DVA_GET_ASIZE(dva), tx);
1552 scn->scn_visited_this_txg++;
1553 return (0);
1556 boolean_t
1557 dsl_scan_active(dsl_scan_t *scn)
1559 spa_t *spa = scn->scn_dp->dp_spa;
1560 uint64_t used = 0, comp, uncomp;
1562 if (spa->spa_load_state != SPA_LOAD_NONE)
1563 return (B_FALSE);
1564 if (spa_shutting_down(spa))
1565 return (B_FALSE);
1566 if ((scn->scn_phys.scn_state == DSS_SCANNING &&
1567 !dsl_scan_is_paused_scrub(scn)) ||
1568 (scn->scn_async_destroying && !scn->scn_async_stalled))
1569 return (B_TRUE);
1571 if (spa_version(scn->scn_dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
1572 (void) bpobj_space(&scn->scn_dp->dp_free_bpobj,
1573 &used, &comp, &uncomp);
1575 return (used != 0);
1578 static int
1579 dsl_process_async_destroys(dsl_pool_t *dp, dmu_tx_t *tx)
1581 dsl_scan_t *scn = dp->dp_scan;
1582 spa_t *spa = dp->dp_spa;
1583 int err = 0;
1585 if (spa_suspend_async_destroy(spa))
1586 return (0);
1588 if (zfs_free_bpobj_enabled &&
1589 spa_version(dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
1590 scn->scn_is_bptree = B_FALSE;
1591 scn->scn_async_block_min_time_ms = zfs_free_min_time_ms;
1592 scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
1593 NULL, ZIO_FLAG_MUSTSUCCEED);
1594 err = bpobj_iterate(&dp->dp_free_bpobj,
1595 dsl_scan_free_block_cb, scn, tx);
1596 VERIFY3U(0, ==, zio_wait(scn->scn_zio_root));
1598 if (err != 0 && err != ERESTART)
1599 zfs_panic_recover("error %u from bpobj_iterate()", err);
1602 if (err == 0 && spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) {
1603 ASSERT(scn->scn_async_destroying);
1604 scn->scn_is_bptree = B_TRUE;
1605 scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
1606 NULL, ZIO_FLAG_MUSTSUCCEED);
1607 err = bptree_iterate(dp->dp_meta_objset,
1608 dp->dp_bptree_obj, B_TRUE, dsl_scan_free_block_cb, scn, tx);
1609 VERIFY0(zio_wait(scn->scn_zio_root));
1611 if (err == EIO || err == ECKSUM) {
1612 err = 0;
1613 } else if (err != 0 && err != ERESTART) {
1614 zfs_panic_recover("error %u from "
1615 "traverse_dataset_destroyed()", err);
1618 if (bptree_is_empty(dp->dp_meta_objset, dp->dp_bptree_obj)) {
1619 /* finished; deactivate async destroy feature */
1620 spa_feature_decr(spa, SPA_FEATURE_ASYNC_DESTROY, tx);
1621 ASSERT(!spa_feature_is_active(spa,
1622 SPA_FEATURE_ASYNC_DESTROY));
1623 VERIFY0(zap_remove(dp->dp_meta_objset,
1624 DMU_POOL_DIRECTORY_OBJECT,
1625 DMU_POOL_BPTREE_OBJ, tx));
1626 VERIFY0(bptree_free(dp->dp_meta_objset,
1627 dp->dp_bptree_obj, tx));
1628 dp->dp_bptree_obj = 0;
1629 scn->scn_async_destroying = B_FALSE;
1630 scn->scn_async_stalled = B_FALSE;
1631 } else {
1633 * If we didn't make progress, mark the async
1634 * destroy as stalled, so that we will not initiate
1635 * a spa_sync() on its behalf. Note that we only
1636 * check this if we are not finished, because if the
1637 * bptree had no blocks for us to visit, we can
1638 * finish without "making progress".
1640 scn->scn_async_stalled =
1641 (scn->scn_visited_this_txg == 0);
1644 if (scn->scn_visited_this_txg) {
1645 zfs_dbgmsg("freed %llu blocks in %llums from "
1646 "free_bpobj/bptree txg %llu; err=%u",
1647 (longlong_t)scn->scn_visited_this_txg,
1648 (longlong_t)
1649 NSEC2MSEC(gethrtime() - scn->scn_sync_start_time),
1650 (longlong_t)tx->tx_txg, err);
1651 scn->scn_visited_this_txg = 0;
1654 * Write out changes to the DDT that may be required as a
1655 * result of the blocks freed. This ensures that the DDT
1656 * is clean when a scrub/resilver runs.
1658 ddt_sync(spa, tx->tx_txg);
1660 if (err != 0)
1661 return (err);
1662 if (dp->dp_free_dir != NULL && !scn->scn_async_destroying &&
1663 zfs_free_leak_on_eio &&
1664 (dsl_dir_phys(dp->dp_free_dir)->dd_used_bytes != 0 ||
1665 dsl_dir_phys(dp->dp_free_dir)->dd_compressed_bytes != 0 ||
1666 dsl_dir_phys(dp->dp_free_dir)->dd_uncompressed_bytes != 0)) {
1668 * We have finished background destroying, but there is still
1669 * some space left in the dp_free_dir. Transfer this leaked
1670 * space to the dp_leak_dir.
1672 if (dp->dp_leak_dir == NULL) {
1673 rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
1674 (void) dsl_dir_create_sync(dp, dp->dp_root_dir,
1675 LEAK_DIR_NAME, tx);
1676 VERIFY0(dsl_pool_open_special_dir(dp,
1677 LEAK_DIR_NAME, &dp->dp_leak_dir));
1678 rrw_exit(&dp->dp_config_rwlock, FTAG);
1680 dsl_dir_diduse_space(dp->dp_leak_dir, DD_USED_HEAD,
1681 dsl_dir_phys(dp->dp_free_dir)->dd_used_bytes,
1682 dsl_dir_phys(dp->dp_free_dir)->dd_compressed_bytes,
1683 dsl_dir_phys(dp->dp_free_dir)->dd_uncompressed_bytes, tx);
1684 dsl_dir_diduse_space(dp->dp_free_dir, DD_USED_HEAD,
1685 -dsl_dir_phys(dp->dp_free_dir)->dd_used_bytes,
1686 -dsl_dir_phys(dp->dp_free_dir)->dd_compressed_bytes,
1687 -dsl_dir_phys(dp->dp_free_dir)->dd_uncompressed_bytes, tx);
1690 if (dp->dp_free_dir != NULL && !scn->scn_async_destroying) {
1691 /* finished; verify that space accounting went to zero */
1692 ASSERT0(dsl_dir_phys(dp->dp_free_dir)->dd_used_bytes);
1693 ASSERT0(dsl_dir_phys(dp->dp_free_dir)->dd_compressed_bytes);
1694 ASSERT0(dsl_dir_phys(dp->dp_free_dir)->dd_uncompressed_bytes);
1697 EQUIV(bpobj_is_open(&dp->dp_obsolete_bpobj),
1698 0 == zap_contains(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1699 DMU_POOL_OBSOLETE_BPOBJ));
1700 if (err == 0 && bpobj_is_open(&dp->dp_obsolete_bpobj)) {
1701 ASSERT(spa_feature_is_active(dp->dp_spa,
1702 SPA_FEATURE_OBSOLETE_COUNTS));
1704 scn->scn_is_bptree = B_FALSE;
1705 scn->scn_async_block_min_time_ms = zfs_obsolete_min_time_ms;
1706 err = bpobj_iterate(&dp->dp_obsolete_bpobj,
1707 dsl_scan_obsolete_block_cb, scn, tx);
1708 if (err != 0 && err != ERESTART)
1709 zfs_panic_recover("error %u from bpobj_iterate()", err);
1711 if (bpobj_is_empty(&dp->dp_obsolete_bpobj))
1712 dsl_pool_destroy_obsolete_bpobj(dp, tx);
1715 return (0);
1718 void
1719 dsl_scan_sync(dsl_pool_t *dp, dmu_tx_t *tx)
1721 dsl_scan_t *scn = dp->dp_scan;
1722 spa_t *spa = dp->dp_spa;
1723 int err = 0;
1726 * Check for scn_restart_txg before checking spa_load_state, so
1727 * that we can restart an old-style scan while the pool is being
1728 * imported (see dsl_scan_init).
1730 if (dsl_scan_restarting(scn, tx)) {
1731 pool_scan_func_t func = POOL_SCAN_SCRUB;
1732 dsl_scan_done(scn, B_FALSE, tx);
1733 if (vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL))
1734 func = POOL_SCAN_RESILVER;
1735 zfs_dbgmsg("restarting scan func=%u txg=%llu",
1736 func, tx->tx_txg);
1737 dsl_scan_setup_sync(&func, tx);
1741 * Only process scans in sync pass 1.
1743 if (spa_sync_pass(dp->dp_spa) > 1)
1744 return;
1747 * If the spa is shutting down, then stop scanning. This will
1748 * ensure that the scan does not dirty any new data during the
1749 * shutdown phase.
1751 if (spa_shutting_down(spa))
1752 return;
1755 * If the scan is inactive due to a stalled async destroy, try again.
1757 if (!scn->scn_async_stalled && !dsl_scan_active(scn))
1758 return;
1760 scn->scn_visited_this_txg = 0;
1761 scn->scn_suspending = B_FALSE;
1762 scn->scn_sync_start_time = gethrtime();
1763 spa->spa_scrub_active = B_TRUE;
1766 * First process the async destroys. If we pause, don't do
1767 * any scrubbing or resilvering. This ensures that there are no
1768 * async destroys while we are scanning, so the scan code doesn't
1769 * have to worry about traversing it. It is also faster to free the
1770 * blocks than to scrub them.
1772 err = dsl_process_async_destroys(dp, tx);
1773 if (err != 0)
1774 return;
1776 if (scn->scn_phys.scn_state != DSS_SCANNING)
1777 return;
1779 if (scn->scn_done_txg == tx->tx_txg) {
1780 ASSERT(!scn->scn_suspending);
1781 /* finished with scan. */
1782 zfs_dbgmsg("txg %llu scan complete", tx->tx_txg);
1783 dsl_scan_done(scn, B_TRUE, tx);
1784 ASSERT3U(spa->spa_scrub_inflight, ==, 0);
1785 dsl_scan_sync_state(scn, tx);
1786 return;
1789 if (dsl_scan_is_paused_scrub(scn))
1790 return;
1792 if (scn->scn_phys.scn_ddt_bookmark.ddb_class <=
1793 scn->scn_phys.scn_ddt_class_max) {
1794 zfs_dbgmsg("doing scan sync txg %llu; "
1795 "ddt bm=%llu/%llu/%llu/%llx",
1796 (longlong_t)tx->tx_txg,
1797 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_class,
1798 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_type,
1799 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_checksum,
1800 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_cursor);
1801 ASSERT(scn->scn_phys.scn_bookmark.zb_objset == 0);
1802 ASSERT(scn->scn_phys.scn_bookmark.zb_object == 0);
1803 ASSERT(scn->scn_phys.scn_bookmark.zb_level == 0);
1804 ASSERT(scn->scn_phys.scn_bookmark.zb_blkid == 0);
1805 } else {
1806 zfs_dbgmsg("doing scan sync txg %llu; bm=%llu/%llu/%llu/%llu",
1807 (longlong_t)tx->tx_txg,
1808 (longlong_t)scn->scn_phys.scn_bookmark.zb_objset,
1809 (longlong_t)scn->scn_phys.scn_bookmark.zb_object,
1810 (longlong_t)scn->scn_phys.scn_bookmark.zb_level,
1811 (longlong_t)scn->scn_phys.scn_bookmark.zb_blkid);
1814 scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
1815 NULL, ZIO_FLAG_CANFAIL);
1816 dsl_pool_config_enter(dp, FTAG);
1817 dsl_scan_visit(scn, tx);
1818 dsl_pool_config_exit(dp, FTAG);
1819 (void) zio_wait(scn->scn_zio_root);
1820 scn->scn_zio_root = NULL;
1822 zfs_dbgmsg("visited %llu blocks in %llums",
1823 (longlong_t)scn->scn_visited_this_txg,
1824 (longlong_t)NSEC2MSEC(gethrtime() - scn->scn_sync_start_time));
1826 if (!scn->scn_suspending) {
1827 scn->scn_done_txg = tx->tx_txg + 1;
1828 zfs_dbgmsg("txg %llu traversal complete, waiting till txg %llu",
1829 tx->tx_txg, scn->scn_done_txg);
1832 if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
1833 mutex_enter(&spa->spa_scrub_lock);
1834 while (spa->spa_scrub_inflight > 0) {
1835 cv_wait(&spa->spa_scrub_io_cv,
1836 &spa->spa_scrub_lock);
1838 mutex_exit(&spa->spa_scrub_lock);
1841 dsl_scan_sync_state(scn, tx);
1845 * This will start a new scan, or restart an existing one.
1847 void
1848 dsl_resilver_restart(dsl_pool_t *dp, uint64_t txg)
1850 if (txg == 0) {
1851 dmu_tx_t *tx;
1852 tx = dmu_tx_create_dd(dp->dp_mos_dir);
1853 VERIFY(0 == dmu_tx_assign(tx, TXG_WAIT));
1855 txg = dmu_tx_get_txg(tx);
1856 dp->dp_scan->scn_restart_txg = txg;
1857 dmu_tx_commit(tx);
1858 } else {
1859 dp->dp_scan->scn_restart_txg = txg;
1861 zfs_dbgmsg("restarting resilver txg=%llu", txg);
1864 boolean_t
1865 dsl_scan_resilvering(dsl_pool_t *dp)
1867 return (dp->dp_scan->scn_phys.scn_state == DSS_SCANNING &&
1868 dp->dp_scan->scn_phys.scn_func == POOL_SCAN_RESILVER);
1872 * scrub consumers
1875 static void
1876 count_block(zfs_all_blkstats_t *zab, const blkptr_t *bp)
1878 int i;
1881 * If we resume after a reboot, zab will be NULL; don't record
1882 * incomplete stats in that case.
1884 if (zab == NULL)
1885 return;
1887 for (i = 0; i < 4; i++) {
1888 int l = (i < 2) ? BP_GET_LEVEL(bp) : DN_MAX_LEVELS;
1889 int t = (i & 1) ? BP_GET_TYPE(bp) : DMU_OT_TOTAL;
1890 if (t & DMU_OT_NEWTYPE)
1891 t = DMU_OT_OTHER;
1892 zfs_blkstat_t *zb = &zab->zab_type[l][t];
1893 int equal;
1895 zb->zb_count++;
1896 zb->zb_asize += BP_GET_ASIZE(bp);
1897 zb->zb_lsize += BP_GET_LSIZE(bp);
1898 zb->zb_psize += BP_GET_PSIZE(bp);
1899 zb->zb_gangs += BP_COUNT_GANG(bp);
1901 switch (BP_GET_NDVAS(bp)) {
1902 case 2:
1903 if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
1904 DVA_GET_VDEV(&bp->blk_dva[1]))
1905 zb->zb_ditto_2_of_2_samevdev++;
1906 break;
1907 case 3:
1908 equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
1909 DVA_GET_VDEV(&bp->blk_dva[1])) +
1910 (DVA_GET_VDEV(&bp->blk_dva[0]) ==
1911 DVA_GET_VDEV(&bp->blk_dva[2])) +
1912 (DVA_GET_VDEV(&bp->blk_dva[1]) ==
1913 DVA_GET_VDEV(&bp->blk_dva[2]));
1914 if (equal == 1)
1915 zb->zb_ditto_2_of_3_samevdev++;
1916 else if (equal == 3)
1917 zb->zb_ditto_3_of_3_samevdev++;
1918 break;
1923 static void
1924 dsl_scan_scrub_done(zio_t *zio)
1926 spa_t *spa = zio->io_spa;
1928 abd_free(zio->io_abd);
1930 mutex_enter(&spa->spa_scrub_lock);
1931 spa->spa_scrub_inflight--;
1932 cv_broadcast(&spa->spa_scrub_io_cv);
1934 if (zio->io_error && (zio->io_error != ECKSUM ||
1935 !(zio->io_flags & ZIO_FLAG_SPECULATIVE))) {
1936 spa->spa_dsl_pool->dp_scan->scn_phys.scn_errors++;
1938 mutex_exit(&spa->spa_scrub_lock);
1941 static int
1942 dsl_scan_scrub_cb(dsl_pool_t *dp,
1943 const blkptr_t *bp, const zbookmark_phys_t *zb)
1945 dsl_scan_t *scn = dp->dp_scan;
1946 size_t size = BP_GET_PSIZE(bp);
1947 spa_t *spa = dp->dp_spa;
1948 uint64_t phys_birth = BP_PHYSICAL_BIRTH(bp);
1949 boolean_t needs_io;
1950 int zio_flags = ZIO_FLAG_SCAN_THREAD | ZIO_FLAG_RAW | ZIO_FLAG_CANFAIL;
1951 int scan_delay = 0;
1953 if (phys_birth <= scn->scn_phys.scn_min_txg ||
1954 phys_birth >= scn->scn_phys.scn_max_txg)
1955 return (0);
1957 count_block(dp->dp_blkstats, bp);
1959 if (BP_IS_EMBEDDED(bp))
1960 return (0);
1962 ASSERT(DSL_SCAN_IS_SCRUB_RESILVER(scn));
1963 if (scn->scn_phys.scn_func == POOL_SCAN_SCRUB) {
1964 zio_flags |= ZIO_FLAG_SCRUB;
1965 needs_io = B_TRUE;
1966 scan_delay = zfs_scrub_delay;
1967 } else {
1968 ASSERT3U(scn->scn_phys.scn_func, ==, POOL_SCAN_RESILVER);
1969 zio_flags |= ZIO_FLAG_RESILVER;
1970 needs_io = B_FALSE;
1971 scan_delay = zfs_resilver_delay;
1974 /* If it's an intent log block, failure is expected. */
1975 if (zb->zb_level == ZB_ZIL_LEVEL)
1976 zio_flags |= ZIO_FLAG_SPECULATIVE;
1978 for (int d = 0; d < BP_GET_NDVAS(bp); d++) {
1979 vdev_t *vd = vdev_lookup_top(spa,
1980 DVA_GET_VDEV(&bp->blk_dva[d]));
1983 * Keep track of how much data we've examined so that
1984 * zpool(1M) status can make useful progress reports.
1986 scn->scn_phys.scn_examined += DVA_GET_ASIZE(&bp->blk_dva[d]);
1987 spa->spa_scan_pass_exam += DVA_GET_ASIZE(&bp->blk_dva[d]);
1989 /* if it's a resilver, this may not be in the target range */
1990 if (!needs_io) {
1991 if (vd->vdev_ops == &vdev_indirect_ops) {
1993 * The indirect vdev can point to multiple
1994 * vdevs. For simplicity, always create
1995 * the resilver zio_t. zio_vdev_io_start()
1996 * will bypass the child resilver i/o's if
1997 * they are on vdevs that don't have DTL's.
1999 needs_io = B_TRUE;
2000 } else if (DVA_GET_GANG(&bp->blk_dva[d])) {
2002 * Gang members may be spread across multiple
2003 * vdevs, so the best estimate we have is the
2004 * scrub range, which has already been checked.
2005 * XXX -- it would be better to change our
2006 * allocation policy to ensure that all
2007 * gang members reside on the same vdev.
2009 needs_io = B_TRUE;
2010 } else {
2011 needs_io = vdev_dtl_contains(vd, DTL_PARTIAL,
2012 phys_birth, 1);
2017 if (needs_io && !zfs_no_scrub_io) {
2018 vdev_t *rvd = spa->spa_root_vdev;
2019 uint64_t maxinflight = rvd->vdev_children * zfs_top_maxinflight;
2021 mutex_enter(&spa->spa_scrub_lock);
2022 while (spa->spa_scrub_inflight >= maxinflight)
2023 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
2024 spa->spa_scrub_inflight++;
2025 mutex_exit(&spa->spa_scrub_lock);
2028 * If we're seeing recent (zfs_scan_idle) "important" I/Os
2029 * then throttle our workload to limit the impact of a scan.
2031 if (ddi_get_lbolt64() - spa->spa_last_io <= zfs_scan_idle)
2032 delay(scan_delay);
2034 zio_nowait(zio_read(NULL, spa, bp,
2035 abd_alloc_for_io(size, B_FALSE), size, dsl_scan_scrub_done,
2036 NULL, ZIO_PRIORITY_SCRUB, zio_flags, zb));
2039 /* do not relocate this block */
2040 return (0);
2044 * Called by the ZFS_IOC_POOL_SCAN ioctl to start a scrub or resilver.
2045 * Can also be called to resume a paused scrub.
2048 dsl_scan(dsl_pool_t *dp, pool_scan_func_t func)
2050 spa_t *spa = dp->dp_spa;
2051 dsl_scan_t *scn = dp->dp_scan;
2054 * Purge all vdev caches and probe all devices. We do this here
2055 * rather than in sync context because this requires a writer lock
2056 * on the spa_config lock, which we can't do from sync context. The
2057 * spa_scrub_reopen flag indicates that vdev_open() should not
2058 * attempt to start another scrub.
2060 spa_vdev_state_enter(spa, SCL_NONE);
2061 spa->spa_scrub_reopen = B_TRUE;
2062 vdev_reopen(spa->spa_root_vdev);
2063 spa->spa_scrub_reopen = B_FALSE;
2064 (void) spa_vdev_state_exit(spa, NULL, 0);
2066 if (func == POOL_SCAN_SCRUB && dsl_scan_is_paused_scrub(scn)) {
2067 /* got scrub start cmd, resume paused scrub */
2068 int err = dsl_scrub_set_pause_resume(scn->scn_dp,
2069 POOL_SCRUB_NORMAL);
2070 if (err == 0) {
2071 spa_event_notify(spa, NULL, NULL, ESC_ZFS_SCRUB_RESUME);
2072 return (ECANCELED);
2075 return (SET_ERROR(err));
2078 return (dsl_sync_task(spa_name(spa), dsl_scan_setup_check,
2079 dsl_scan_setup_sync, &func, 0, ZFS_SPACE_CHECK_EXTRA_RESERVED));
2082 static boolean_t
2083 dsl_scan_restarting(dsl_scan_t *scn, dmu_tx_t *tx)
2085 return (scn->scn_restart_txg != 0 &&
2086 scn->scn_restart_txg <= tx->tx_txg);