dsl_scan.c revision 219089
1219089Spjd/*
2219089Spjd * CDDL HEADER START
3219089Spjd *
4219089Spjd * The contents of this file are subject to the terms of the
5219089Spjd * Common Development and Distribution License (the "License").
6219089Spjd * You may not use this file except in compliance with the License.
7219089Spjd *
8219089Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9219089Spjd * or http://www.opensolaris.org/os/licensing.
10219089Spjd * See the License for the specific language governing permissions
11219089Spjd * and limitations under the License.
12219089Spjd *
13219089Spjd * When distributing Covered Code, include this CDDL HEADER in each
14219089Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15219089Spjd * If applicable, add the following below this CDDL HEADER, with the
16219089Spjd * fields enclosed by brackets "[]" replaced with your own identifying
17219089Spjd * information: Portions Copyright [yyyy] [name of copyright owner]
18219089Spjd *
19219089Spjd * CDDL HEADER END
20219089Spjd */
21219089Spjd/*
22219089Spjd * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
23219089Spjd */
24219089Spjd
25219089Spjd#include <sys/dsl_scan.h>
26219089Spjd#include <sys/dsl_pool.h>
27219089Spjd#include <sys/dsl_dataset.h>
28219089Spjd#include <sys/dsl_prop.h>
29219089Spjd#include <sys/dsl_dir.h>
30219089Spjd#include <sys/dsl_synctask.h>
31219089Spjd#include <sys/dnode.h>
32219089Spjd#include <sys/dmu_tx.h>
33219089Spjd#include <sys/dmu_objset.h>
34219089Spjd#include <sys/arc.h>
35219089Spjd#include <sys/zap.h>
36219089Spjd#include <sys/zio.h>
37219089Spjd#include <sys/zfs_context.h>
38219089Spjd#include <sys/fs/zfs.h>
39219089Spjd#include <sys/zfs_znode.h>
40219089Spjd#include <sys/spa_impl.h>
41219089Spjd#include <sys/vdev_impl.h>
42219089Spjd#include <sys/zil_impl.h>
43219089Spjd#include <sys/zio_checksum.h>
44219089Spjd#include <sys/ddt.h>
45219089Spjd#include <sys/sa.h>
46219089Spjd#include <sys/sa_impl.h>
47219089Spjd#ifdef _KERNEL
48219089Spjd#include <sys/zfs_vfsops.h>
49219089Spjd#endif
50219089Spjd
51219089Spjdtypedef int (scan_cb_t)(dsl_pool_t *, const blkptr_t *, const zbookmark_t *);
52219089Spjd
53219089Spjdstatic scan_cb_t dsl_scan_defrag_cb;
54219089Spjdstatic scan_cb_t dsl_scan_scrub_cb;
55219089Spjdstatic scan_cb_t dsl_scan_remove_cb;
56219089Spjdstatic dsl_syncfunc_t dsl_scan_cancel_sync;
57219089Spjdstatic void dsl_scan_sync_state(dsl_scan_t *, dmu_tx_t *tx);
58219089Spjd
59219089Spjdint zfs_top_maxinflight = 32;		/* maximum I/Os per top-level */
60219089Spjdint zfs_resilver_delay = 2;		/* number of ticks to delay resilver */
61219089Spjdint zfs_scrub_delay = 4;		/* number of ticks to delay scrub */
62219089Spjdint zfs_scan_idle = 50;			/* idle window in clock ticks */
63219089Spjd
64219089Spjdint zfs_scan_min_time_ms = 1000; /* min millisecs to scrub per txg */
65219089Spjdint zfs_free_min_time_ms = 1000; /* min millisecs to free per txg */
66219089Spjdint zfs_resilver_min_time_ms = 3000; /* min millisecs to resilver per txg */
67219089Spjdboolean_t zfs_no_scrub_io = B_FALSE; /* set to disable scrub i/o */
68219089Spjdboolean_t zfs_no_scrub_prefetch = B_FALSE; /* set to disable srub prefetching */
69219089Spjdenum ddt_class zfs_scrub_ddt_class_max = DDT_CLASS_DUPLICATE;
70219089Spjdint dsl_scan_delay_completion = B_FALSE; /* set to delay scan completion */
71219089Spjd
72219089Spjd#define	DSL_SCAN_IS_SCRUB_RESILVER(scn) \
73219089Spjd	((scn)->scn_phys.scn_func == POOL_SCAN_SCRUB || \
74219089Spjd	(scn)->scn_phys.scn_func == POOL_SCAN_RESILVER)
75219089Spjd
76219089Spjdextern int zfs_txg_timeout;
77219089Spjd
78219089Spjd/* the order has to match pool_scan_type */
79219089Spjdstatic scan_cb_t *scan_funcs[POOL_SCAN_FUNCS] = {
80219089Spjd	NULL,
81219089Spjd	dsl_scan_scrub_cb,	/* POOL_SCAN_SCRUB */
82219089Spjd	dsl_scan_scrub_cb,	/* POOL_SCAN_RESILVER */
83219089Spjd};
84219089Spjd
85219089Spjdint
86219089Spjddsl_scan_init(dsl_pool_t *dp, uint64_t txg)
87219089Spjd{
88219089Spjd	int err;
89219089Spjd	dsl_scan_t *scn;
90219089Spjd	spa_t *spa = dp->dp_spa;
91219089Spjd	uint64_t f;
92219089Spjd
93219089Spjd	scn = dp->dp_scan = kmem_zalloc(sizeof (dsl_scan_t), KM_SLEEP);
94219089Spjd	scn->scn_dp = dp;
95219089Spjd
96219089Spjd	err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
97219089Spjd	    "scrub_func", sizeof (uint64_t), 1, &f);
98219089Spjd	if (err == 0) {
99219089Spjd		/*
100219089Spjd		 * There was an old-style scrub in progress.  Restart a
101219089Spjd		 * new-style scrub from the beginning.
102219089Spjd		 */
103219089Spjd		scn->scn_restart_txg = txg;
104219089Spjd		zfs_dbgmsg("old-style scrub was in progress; "
105219089Spjd		    "restarting new-style scrub in txg %llu",
106219089Spjd		    scn->scn_restart_txg);
107219089Spjd
108219089Spjd		/*
109219089Spjd		 * Load the queue obj from the old location so that it
110219089Spjd		 * can be freed by dsl_scan_done().
111219089Spjd		 */
112219089Spjd		(void) zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
113219089Spjd		    "scrub_queue", sizeof (uint64_t), 1,
114219089Spjd		    &scn->scn_phys.scn_queue_obj);
115219089Spjd	} else {
116219089Spjd		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
117219089Spjd		    DMU_POOL_SCAN, sizeof (uint64_t), SCAN_PHYS_NUMINTS,
118219089Spjd		    &scn->scn_phys);
119219089Spjd		if (err == ENOENT)
120219089Spjd			return (0);
121219089Spjd		else if (err)
122219089Spjd			return (err);
123219089Spjd
124219089Spjd		if (scn->scn_phys.scn_state == DSS_SCANNING &&
125219089Spjd		    spa_prev_software_version(dp->dp_spa) < SPA_VERSION_SCAN) {
126219089Spjd			/*
127219089Spjd			 * A new-type scrub was in progress on an old
128219089Spjd			 * pool, and the pool was accessed by old
129219089Spjd			 * software.  Restart from the beginning, since
130219089Spjd			 * the old software may have changed the pool in
131219089Spjd			 * the meantime.
132219089Spjd			 */
133219089Spjd			scn->scn_restart_txg = txg;
134219089Spjd			zfs_dbgmsg("new-style scrub was modified "
135219089Spjd			    "by old software; restarting in txg %llu",
136219089Spjd			    scn->scn_restart_txg);
137219089Spjd		}
138219089Spjd	}
139219089Spjd
140219089Spjd	spa_scan_stat_init(spa);
141219089Spjd	return (0);
142219089Spjd}
143219089Spjd
144219089Spjdvoid
145219089Spjddsl_scan_fini(dsl_pool_t *dp)
146219089Spjd{
147219089Spjd	if (dp->dp_scan) {
148219089Spjd		kmem_free(dp->dp_scan, sizeof (dsl_scan_t));
149219089Spjd		dp->dp_scan = NULL;
150219089Spjd	}
151219089Spjd}
152219089Spjd
153219089Spjd/* ARGSUSED */
154219089Spjdstatic int
155219089Spjddsl_scan_setup_check(void *arg1, void *arg2, dmu_tx_t *tx)
156219089Spjd{
157219089Spjd	dsl_scan_t *scn = arg1;
158219089Spjd
159219089Spjd	if (scn->scn_phys.scn_state == DSS_SCANNING)
160219089Spjd		return (EBUSY);
161219089Spjd
162219089Spjd	return (0);
163219089Spjd}
164219089Spjd
165219089Spjd/* ARGSUSED */
166219089Spjdstatic void
167219089Spjddsl_scan_setup_sync(void *arg1, void *arg2, dmu_tx_t *tx)
168219089Spjd{
169219089Spjd	dsl_scan_t *scn = arg1;
170219089Spjd	pool_scan_func_t *funcp = arg2;
171219089Spjd	dmu_object_type_t ot = 0;
172219089Spjd	dsl_pool_t *dp = scn->scn_dp;
173219089Spjd	spa_t *spa = dp->dp_spa;
174219089Spjd
175219089Spjd	ASSERT(scn->scn_phys.scn_state != DSS_SCANNING);
176219089Spjd	ASSERT(*funcp > POOL_SCAN_NONE && *funcp < POOL_SCAN_FUNCS);
177219089Spjd	bzero(&scn->scn_phys, sizeof (scn->scn_phys));
178219089Spjd	scn->scn_phys.scn_func = *funcp;
179219089Spjd	scn->scn_phys.scn_state = DSS_SCANNING;
180219089Spjd	scn->scn_phys.scn_min_txg = 0;
181219089Spjd	scn->scn_phys.scn_max_txg = tx->tx_txg;
182219089Spjd	scn->scn_phys.scn_ddt_class_max = DDT_CLASSES - 1; /* the entire DDT */
183219089Spjd	scn->scn_phys.scn_start_time = gethrestime_sec();
184219089Spjd	scn->scn_phys.scn_errors = 0;
185219089Spjd	scn->scn_phys.scn_to_examine = spa->spa_root_vdev->vdev_stat.vs_alloc;
186219089Spjd	scn->scn_restart_txg = 0;
187219089Spjd	spa_scan_stat_init(spa);
188219089Spjd
189219089Spjd	if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
190219089Spjd		scn->scn_phys.scn_ddt_class_max = zfs_scrub_ddt_class_max;
191219089Spjd
192219089Spjd		/* rewrite all disk labels */
193219089Spjd		vdev_config_dirty(spa->spa_root_vdev);
194219089Spjd
195219089Spjd		if (vdev_resilver_needed(spa->spa_root_vdev,
196219089Spjd		    &scn->scn_phys.scn_min_txg, &scn->scn_phys.scn_max_txg)) {
197219089Spjd			spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_START);
198219089Spjd		} else {
199219089Spjd			spa_event_notify(spa, NULL, ESC_ZFS_SCRUB_START);
200219089Spjd		}
201219089Spjd
202219089Spjd		spa->spa_scrub_started = B_TRUE;
203219089Spjd		/*
204219089Spjd		 * If this is an incremental scrub, limit the DDT scrub phase
205219089Spjd		 * to just the auto-ditto class (for correctness); the rest
206219089Spjd		 * of the scrub should go faster using top-down pruning.
207219089Spjd		 */
208219089Spjd		if (scn->scn_phys.scn_min_txg > TXG_INITIAL)
209219089Spjd			scn->scn_phys.scn_ddt_class_max = DDT_CLASS_DITTO;
210219089Spjd
211219089Spjd	}
212219089Spjd
213219089Spjd	/* back to the generic stuff */
214219089Spjd
215219089Spjd	if (dp->dp_blkstats == NULL) {
216219089Spjd		dp->dp_blkstats =
217219089Spjd		    kmem_alloc(sizeof (zfs_all_blkstats_t), KM_SLEEP);
218219089Spjd	}
219219089Spjd	bzero(dp->dp_blkstats, sizeof (zfs_all_blkstats_t));
220219089Spjd
221219089Spjd	if (spa_version(spa) < SPA_VERSION_DSL_SCRUB)
222219089Spjd		ot = DMU_OT_ZAP_OTHER;
223219089Spjd
224219089Spjd	scn->scn_phys.scn_queue_obj = zap_create(dp->dp_meta_objset,
225219089Spjd	    ot ? ot : DMU_OT_SCAN_QUEUE, DMU_OT_NONE, 0, tx);
226219089Spjd
227219089Spjd	dsl_scan_sync_state(scn, tx);
228219089Spjd
229219089Spjd	spa_history_log_internal(LOG_POOL_SCAN, spa, tx,
230219089Spjd	    "func=%u mintxg=%llu maxtxg=%llu",
231219089Spjd	    *funcp, scn->scn_phys.scn_min_txg, scn->scn_phys.scn_max_txg);
232219089Spjd}
233219089Spjd
234219089Spjd/* ARGSUSED */
235219089Spjdstatic void
236219089Spjddsl_scan_done(dsl_scan_t *scn, boolean_t complete, dmu_tx_t *tx)
237219089Spjd{
238219089Spjd	static const char *old_names[] = {
239219089Spjd		"scrub_bookmark",
240219089Spjd		"scrub_ddt_bookmark",
241219089Spjd		"scrub_ddt_class_max",
242219089Spjd		"scrub_queue",
243219089Spjd		"scrub_min_txg",
244219089Spjd		"scrub_max_txg",
245219089Spjd		"scrub_func",
246219089Spjd		"scrub_errors",
247219089Spjd		NULL
248219089Spjd	};
249219089Spjd
250219089Spjd	dsl_pool_t *dp = scn->scn_dp;
251219089Spjd	spa_t *spa = dp->dp_spa;
252219089Spjd	int i;
253219089Spjd
254219089Spjd	/* Remove any remnants of an old-style scrub. */
255219089Spjd	for (i = 0; old_names[i]; i++) {
256219089Spjd		(void) zap_remove(dp->dp_meta_objset,
257219089Spjd		    DMU_POOL_DIRECTORY_OBJECT, old_names[i], tx);
258219089Spjd	}
259219089Spjd
260219089Spjd	if (scn->scn_phys.scn_queue_obj != 0) {
261219089Spjd		VERIFY(0 == dmu_object_free(dp->dp_meta_objset,
262219089Spjd		    scn->scn_phys.scn_queue_obj, tx));
263219089Spjd		scn->scn_phys.scn_queue_obj = 0;
264219089Spjd	}
265219089Spjd
266219089Spjd	/*
267219089Spjd	 * If we were "restarted" from a stopped state, don't bother
268219089Spjd	 * with anything else.
269219089Spjd	 */
270219089Spjd	if (scn->scn_phys.scn_state != DSS_SCANNING)
271219089Spjd		return;
272219089Spjd
273219089Spjd	if (complete)
274219089Spjd		scn->scn_phys.scn_state = DSS_FINISHED;
275219089Spjd	else
276219089Spjd		scn->scn_phys.scn_state = DSS_CANCELED;
277219089Spjd
278219089Spjd	spa_history_log_internal(LOG_POOL_SCAN_DONE, spa, tx,
279219089Spjd	    "complete=%u", complete);
280219089Spjd
281219089Spjd	if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
282219089Spjd		mutex_enter(&spa->spa_scrub_lock);
283219089Spjd		while (spa->spa_scrub_inflight > 0) {
284219089Spjd			cv_wait(&spa->spa_scrub_io_cv,
285219089Spjd			    &spa->spa_scrub_lock);
286219089Spjd		}
287219089Spjd		mutex_exit(&spa->spa_scrub_lock);
288219089Spjd		spa->spa_scrub_started = B_FALSE;
289219089Spjd		spa->spa_scrub_active = B_FALSE;
290219089Spjd
291219089Spjd		/*
292219089Spjd		 * If the scrub/resilver completed, update all DTLs to
293219089Spjd		 * reflect this.  Whether it succeeded or not, vacate
294219089Spjd		 * all temporary scrub DTLs.
295219089Spjd		 */
296219089Spjd		vdev_dtl_reassess(spa->spa_root_vdev, tx->tx_txg,
297219089Spjd		    complete ? scn->scn_phys.scn_max_txg : 0, B_TRUE);
298219089Spjd		if (complete) {
299219089Spjd			spa_event_notify(spa, NULL, scn->scn_phys.scn_min_txg ?
300219089Spjd			    ESC_ZFS_RESILVER_FINISH : ESC_ZFS_SCRUB_FINISH);
301219089Spjd		}
302219089Spjd		spa_errlog_rotate(spa);
303219089Spjd
304219089Spjd		/*
305219089Spjd		 * We may have finished replacing a device.
306219089Spjd		 * Let the async thread assess this and handle the detach.
307219089Spjd		 */
308219089Spjd		spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
309219089Spjd	}
310219089Spjd
311219089Spjd	scn->scn_phys.scn_end_time = gethrestime_sec();
312219089Spjd}
313219089Spjd
314219089Spjd/* ARGSUSED */
315219089Spjdstatic int
316219089Spjddsl_scan_cancel_check(void *arg1, void *arg2, dmu_tx_t *tx)
317219089Spjd{
318219089Spjd	dsl_scan_t *scn = arg1;
319219089Spjd
320219089Spjd	if (scn->scn_phys.scn_state != DSS_SCANNING)
321219089Spjd		return (ENOENT);
322219089Spjd	return (0);
323219089Spjd}
324219089Spjd
325219089Spjd/* ARGSUSED */
326219089Spjdstatic void
327219089Spjddsl_scan_cancel_sync(void *arg1, void *arg2, dmu_tx_t *tx)
328219089Spjd{
329219089Spjd	dsl_scan_t *scn = arg1;
330219089Spjd
331219089Spjd	dsl_scan_done(scn, B_FALSE, tx);
332219089Spjd	dsl_scan_sync_state(scn, tx);
333219089Spjd}
334219089Spjd
335219089Spjdint
336219089Spjddsl_scan_cancel(dsl_pool_t *dp)
337219089Spjd{
338219089Spjd	boolean_t complete = B_FALSE;
339219089Spjd	int err;
340219089Spjd
341219089Spjd	err = dsl_sync_task_do(dp, dsl_scan_cancel_check,
342219089Spjd	    dsl_scan_cancel_sync, dp->dp_scan, &complete, 3);
343219089Spjd	return (err);
344219089Spjd}
345219089Spjd
346219089Spjdstatic void dsl_scan_visitbp(blkptr_t *bp,
347219089Spjd    const zbookmark_t *zb, dnode_phys_t *dnp, arc_buf_t *pbuf,
348219089Spjd    dsl_dataset_t *ds, dsl_scan_t *scn, dmu_objset_type_t ostype,
349219089Spjd    dmu_tx_t *tx);
350219089Spjdstatic void dsl_scan_visitdnode(dsl_scan_t *, dsl_dataset_t *ds,
351219089Spjd    dmu_objset_type_t ostype,
352219089Spjd    dnode_phys_t *dnp, arc_buf_t *buf, uint64_t object, dmu_tx_t *tx);
353219089Spjd
354219089Spjdvoid
355219089Spjddsl_free(dsl_pool_t *dp, uint64_t txg, const blkptr_t *bp)
356219089Spjd{
357219089Spjd	zio_free(dp->dp_spa, txg, bp);
358219089Spjd}
359219089Spjd
360219089Spjdvoid
361219089Spjddsl_free_sync(zio_t *pio, dsl_pool_t *dp, uint64_t txg, const blkptr_t *bpp)
362219089Spjd{
363219089Spjd	ASSERT(dsl_pool_sync_context(dp));
364219089Spjd	zio_nowait(zio_free_sync(pio, dp->dp_spa, txg, bpp, pio->io_flags));
365219089Spjd}
366219089Spjd
367219089Spjdint
368219089Spjddsl_read(zio_t *pio, spa_t *spa, const blkptr_t *bpp, arc_buf_t *pbuf,
369219089Spjd    arc_done_func_t *done, void *private, int priority, int zio_flags,
370219089Spjd    uint32_t *arc_flags, const zbookmark_t *zb)
371219089Spjd{
372219089Spjd	return (arc_read(pio, spa, bpp, pbuf, done, private,
373219089Spjd	    priority, zio_flags, arc_flags, zb));
374219089Spjd}
375219089Spjd
376219089Spjdint
377219089Spjddsl_read_nolock(zio_t *pio, spa_t *spa, const blkptr_t *bpp,
378219089Spjd    arc_done_func_t *done, void *private, int priority, int zio_flags,
379219089Spjd    uint32_t *arc_flags, const zbookmark_t *zb)
380219089Spjd{
381219089Spjd	return (arc_read_nolock(pio, spa, bpp, done, private,
382219089Spjd	    priority, zio_flags, arc_flags, zb));
383219089Spjd}
384219089Spjd
385219089Spjdstatic boolean_t
386219089Spjdbookmark_is_zero(const zbookmark_t *zb)
387219089Spjd{
388219089Spjd	return (zb->zb_objset == 0 && zb->zb_object == 0 &&
389219089Spjd	    zb->zb_level == 0 && zb->zb_blkid == 0);
390219089Spjd}
391219089Spjd
392219089Spjd/* dnp is the dnode for zb1->zb_object */
393219089Spjdstatic boolean_t
394219089Spjdbookmark_is_before(const dnode_phys_t *dnp, const zbookmark_t *zb1,
395219089Spjd    const zbookmark_t *zb2)
396219089Spjd{
397219089Spjd	uint64_t zb1nextL0, zb2thisobj;
398219089Spjd
399219089Spjd	ASSERT(zb1->zb_objset == zb2->zb_objset);
400219089Spjd	ASSERT(zb2->zb_level == 0);
401219089Spjd
402219089Spjd	/*
403219089Spjd	 * A bookmark in the deadlist is considered to be after
404219089Spjd	 * everything else.
405219089Spjd	 */
406219089Spjd	if (zb2->zb_object == DMU_DEADLIST_OBJECT)
407219089Spjd		return (B_TRUE);
408219089Spjd
409219089Spjd	/* The objset_phys_t isn't before anything. */
410219089Spjd	if (dnp == NULL)
411219089Spjd		return (B_FALSE);
412219089Spjd
413219089Spjd	zb1nextL0 = (zb1->zb_blkid + 1) <<
414219089Spjd	    ((zb1->zb_level) * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT));
415219089Spjd
416219089Spjd	zb2thisobj = zb2->zb_object ? zb2->zb_object :
417219089Spjd	    zb2->zb_blkid << (DNODE_BLOCK_SHIFT - DNODE_SHIFT);
418219089Spjd
419219089Spjd	if (zb1->zb_object == DMU_META_DNODE_OBJECT) {
420219089Spjd		uint64_t nextobj = zb1nextL0 *
421219089Spjd		    (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT) >> DNODE_SHIFT;
422219089Spjd		return (nextobj <= zb2thisobj);
423219089Spjd	}
424219089Spjd
425219089Spjd	if (zb1->zb_object < zb2thisobj)
426219089Spjd		return (B_TRUE);
427219089Spjd	if (zb1->zb_object > zb2thisobj)
428219089Spjd		return (B_FALSE);
429219089Spjd	if (zb2->zb_object == DMU_META_DNODE_OBJECT)
430219089Spjd		return (B_FALSE);
431219089Spjd	return (zb1nextL0 <= zb2->zb_blkid);
432219089Spjd}
433219089Spjd
434219089Spjdstatic uint64_t
435219089Spjddsl_scan_ds_maxtxg(dsl_dataset_t *ds)
436219089Spjd{
437219089Spjd	uint64_t smt = ds->ds_dir->dd_pool->dp_scan->scn_phys.scn_max_txg;
438219089Spjd	if (dsl_dataset_is_snapshot(ds))
439219089Spjd		return (MIN(smt, ds->ds_phys->ds_creation_txg));
440219089Spjd	return (smt);
441219089Spjd}
442219089Spjd
443219089Spjdstatic void
444219089Spjddsl_scan_sync_state(dsl_scan_t *scn, dmu_tx_t *tx)
445219089Spjd{
446219089Spjd	VERIFY(0 == zap_update(scn->scn_dp->dp_meta_objset,
447219089Spjd	    DMU_POOL_DIRECTORY_OBJECT,
448219089Spjd	    DMU_POOL_SCAN, sizeof (uint64_t), SCAN_PHYS_NUMINTS,
449219089Spjd	    &scn->scn_phys, tx));
450219089Spjd}
451219089Spjd
452219089Spjdstatic boolean_t
453219089Spjddsl_scan_check_pause(dsl_scan_t *scn, const zbookmark_t *zb)
454219089Spjd{
455219089Spjd	uint64_t elapsed_nanosecs;
456219089Spjd	int mintime;
457219089Spjd
458219089Spjd	/* we never skip user/group accounting objects */
459219089Spjd	if (zb && (int64_t)zb->zb_object < 0)
460219089Spjd		return (B_FALSE);
461219089Spjd
462219089Spjd	if (scn->scn_pausing)
463219089Spjd		return (B_TRUE); /* we're already pausing */
464219089Spjd
465219089Spjd	if (!bookmark_is_zero(&scn->scn_phys.scn_bookmark))
466219089Spjd		return (B_FALSE); /* we're resuming */
467219089Spjd
468219089Spjd	/* We only know how to resume from level-0 blocks. */
469219089Spjd	if (zb && zb->zb_level != 0)
470219089Spjd		return (B_FALSE);
471219089Spjd
472219089Spjd	mintime = (scn->scn_phys.scn_func == POOL_SCAN_RESILVER) ?
473219089Spjd	    zfs_resilver_min_time_ms : zfs_scan_min_time_ms;
474219089Spjd	elapsed_nanosecs = gethrtime() - scn->scn_sync_start_time;
475219089Spjd	if (elapsed_nanosecs / NANOSEC > zfs_txg_timeout ||
476219089Spjd	    (elapsed_nanosecs / MICROSEC > mintime &&
477219089Spjd	    txg_sync_waiting(scn->scn_dp)) ||
478219089Spjd	    spa_shutting_down(scn->scn_dp->dp_spa)) {
479219089Spjd		if (zb) {
480219089Spjd			dprintf("pausing at bookmark %llx/%llx/%llx/%llx\n",
481219089Spjd			    (longlong_t)zb->zb_objset,
482219089Spjd			    (longlong_t)zb->zb_object,
483219089Spjd			    (longlong_t)zb->zb_level,
484219089Spjd			    (longlong_t)zb->zb_blkid);
485219089Spjd			scn->scn_phys.scn_bookmark = *zb;
486219089Spjd		}
487219089Spjd		dprintf("pausing at DDT bookmark %llx/%llx/%llx/%llx\n",
488219089Spjd		    (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_class,
489219089Spjd		    (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_type,
490219089Spjd		    (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_checksum,
491219089Spjd		    (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_cursor);
492219089Spjd		scn->scn_pausing = B_TRUE;
493219089Spjd		return (B_TRUE);
494219089Spjd	}
495219089Spjd	return (B_FALSE);
496219089Spjd}
497219089Spjd
498219089Spjdtypedef struct zil_scan_arg {
499219089Spjd	dsl_pool_t	*zsa_dp;
500219089Spjd	zil_header_t	*zsa_zh;
501219089Spjd} zil_scan_arg_t;
502219089Spjd
503219089Spjd/* ARGSUSED */
504219089Spjdstatic int
505219089Spjddsl_scan_zil_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
506219089Spjd{
507219089Spjd	zil_scan_arg_t *zsa = arg;
508219089Spjd	dsl_pool_t *dp = zsa->zsa_dp;
509219089Spjd	dsl_scan_t *scn = dp->dp_scan;
510219089Spjd	zil_header_t *zh = zsa->zsa_zh;
511219089Spjd	zbookmark_t zb;
512219089Spjd
513219089Spjd	if (bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
514219089Spjd		return (0);
515219089Spjd
516219089Spjd	/*
517219089Spjd	 * One block ("stubby") can be allocated a long time ago; we
518219089Spjd	 * want to visit that one because it has been allocated
519219089Spjd	 * (on-disk) even if it hasn't been claimed (even though for
520219089Spjd	 * scrub there's nothing to do to it).
521219089Spjd	 */
522219089Spjd	if (claim_txg == 0 && bp->blk_birth >= spa_first_txg(dp->dp_spa))
523219089Spjd		return (0);
524219089Spjd
525219089Spjd	SET_BOOKMARK(&zb, zh->zh_log.blk_cksum.zc_word[ZIL_ZC_OBJSET],
526219089Spjd	    ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, bp->blk_cksum.zc_word[ZIL_ZC_SEQ]);
527219089Spjd
528219089Spjd	VERIFY(0 == scan_funcs[scn->scn_phys.scn_func](dp, bp, &zb));
529219089Spjd	return (0);
530219089Spjd}
531219089Spjd
532219089Spjd/* ARGSUSED */
533219089Spjdstatic int
534219089Spjddsl_scan_zil_record(zilog_t *zilog, lr_t *lrc, void *arg, uint64_t claim_txg)
535219089Spjd{
536219089Spjd	if (lrc->lrc_txtype == TX_WRITE) {
537219089Spjd		zil_scan_arg_t *zsa = arg;
538219089Spjd		dsl_pool_t *dp = zsa->zsa_dp;
539219089Spjd		dsl_scan_t *scn = dp->dp_scan;
540219089Spjd		zil_header_t *zh = zsa->zsa_zh;
541219089Spjd		lr_write_t *lr = (lr_write_t *)lrc;
542219089Spjd		blkptr_t *bp = &lr->lr_blkptr;
543219089Spjd		zbookmark_t zb;
544219089Spjd
545219089Spjd		if (bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
546219089Spjd			return (0);
547219089Spjd
548219089Spjd		/*
549219089Spjd		 * birth can be < claim_txg if this record's txg is
550219089Spjd		 * already txg sync'ed (but this log block contains
551219089Spjd		 * other records that are not synced)
552219089Spjd		 */
553219089Spjd		if (claim_txg == 0 || bp->blk_birth < claim_txg)
554219089Spjd			return (0);
555219089Spjd
556219089Spjd		SET_BOOKMARK(&zb, zh->zh_log.blk_cksum.zc_word[ZIL_ZC_OBJSET],
557219089Spjd		    lr->lr_foid, ZB_ZIL_LEVEL,
558219089Spjd		    lr->lr_offset / BP_GET_LSIZE(bp));
559219089Spjd
560219089Spjd		VERIFY(0 == scan_funcs[scn->scn_phys.scn_func](dp, bp, &zb));
561219089Spjd	}
562219089Spjd	return (0);
563219089Spjd}
564219089Spjd
565219089Spjdstatic void
566219089Spjddsl_scan_zil(dsl_pool_t *dp, zil_header_t *zh)
567219089Spjd{
568219089Spjd	uint64_t claim_txg = zh->zh_claim_txg;
569219089Spjd	zil_scan_arg_t zsa = { dp, zh };
570219089Spjd	zilog_t *zilog;
571219089Spjd
572219089Spjd	/*
573219089Spjd	 * We only want to visit blocks that have been claimed but not yet
574219089Spjd	 * replayed (or, in read-only mode, blocks that *would* be claimed).
575219089Spjd	 */
576219089Spjd	if (claim_txg == 0 && spa_writeable(dp->dp_spa))
577219089Spjd		return;
578219089Spjd
579219089Spjd	zilog = zil_alloc(dp->dp_meta_objset, zh);
580219089Spjd
581219089Spjd	(void) zil_parse(zilog, dsl_scan_zil_block, dsl_scan_zil_record, &zsa,
582219089Spjd	    claim_txg);
583219089Spjd
584219089Spjd	zil_free(zilog);
585219089Spjd}
586219089Spjd
587219089Spjd/* ARGSUSED */
588219089Spjdstatic void
589219089Spjddsl_scan_prefetch(dsl_scan_t *scn, arc_buf_t *buf, blkptr_t *bp,
590219089Spjd    uint64_t objset, uint64_t object, uint64_t blkid)
591219089Spjd{
592219089Spjd	zbookmark_t czb;
593219089Spjd	uint32_t flags = ARC_NOWAIT | ARC_PREFETCH;
594219089Spjd
595219089Spjd	if (zfs_no_scrub_prefetch)
596219089Spjd		return;
597219089Spjd
598219089Spjd	if (BP_IS_HOLE(bp) || bp->blk_birth <= scn->scn_phys.scn_min_txg ||
599219089Spjd	    (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_DNODE))
600219089Spjd		return;
601219089Spjd
602219089Spjd	SET_BOOKMARK(&czb, objset, object, BP_GET_LEVEL(bp), blkid);
603219089Spjd
604219089Spjd	/*
605219089Spjd	 * XXX need to make sure all of these arc_read() prefetches are
606219089Spjd	 * done before setting xlateall (similar to dsl_read())
607219089Spjd	 */
608219089Spjd	(void) arc_read(scn->scn_zio_root, scn->scn_dp->dp_spa, bp,
609219089Spjd	    buf, NULL, NULL, ZIO_PRIORITY_ASYNC_READ,
610219089Spjd	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SCAN_THREAD, &flags, &czb);
611219089Spjd}
612219089Spjd
613219089Spjdstatic boolean_t
614219089Spjddsl_scan_check_resume(dsl_scan_t *scn, const dnode_phys_t *dnp,
615219089Spjd    const zbookmark_t *zb)
616219089Spjd{
617219089Spjd	/*
618219089Spjd	 * We never skip over user/group accounting objects (obj<0)
619219089Spjd	 */
620219089Spjd	if (!bookmark_is_zero(&scn->scn_phys.scn_bookmark) &&
621219089Spjd	    (int64_t)zb->zb_object >= 0) {
622219089Spjd		/*
623219089Spjd		 * If we already visited this bp & everything below (in
624219089Spjd		 * a prior txg sync), don't bother doing it again.
625219089Spjd		 */
626219089Spjd		if (bookmark_is_before(dnp, zb, &scn->scn_phys.scn_bookmark))
627219089Spjd			return (B_TRUE);
628219089Spjd
629219089Spjd		/*
630219089Spjd		 * If we found the block we're trying to resume from, or
631219089Spjd		 * we went past it to a different object, zero it out to
632219089Spjd		 * indicate that it's OK to start checking for pausing
633219089Spjd		 * again.
634219089Spjd		 */
635219089Spjd		if (bcmp(zb, &scn->scn_phys.scn_bookmark, sizeof (*zb)) == 0 ||
636219089Spjd		    zb->zb_object > scn->scn_phys.scn_bookmark.zb_object) {
637219089Spjd			dprintf("resuming at %llx/%llx/%llx/%llx\n",
638219089Spjd			    (longlong_t)zb->zb_objset,
639219089Spjd			    (longlong_t)zb->zb_object,
640219089Spjd			    (longlong_t)zb->zb_level,
641219089Spjd			    (longlong_t)zb->zb_blkid);
642219089Spjd			bzero(&scn->scn_phys.scn_bookmark, sizeof (*zb));
643219089Spjd		}
644219089Spjd	}
645219089Spjd	return (B_FALSE);
646219089Spjd}
647219089Spjd
648219089Spjd/*
649219089Spjd * Return nonzero on i/o error.
650219089Spjd * Return new buf to write out in *bufp.
651219089Spjd */
652219089Spjdstatic int
653219089Spjddsl_scan_recurse(dsl_scan_t *scn, dsl_dataset_t *ds, dmu_objset_type_t ostype,
654219089Spjd    dnode_phys_t *dnp, const blkptr_t *bp,
655219089Spjd    const zbookmark_t *zb, dmu_tx_t *tx, arc_buf_t **bufp)
656219089Spjd{
657219089Spjd	dsl_pool_t *dp = scn->scn_dp;
658219089Spjd	int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCAN_THREAD;
659219089Spjd	int err;
660219089Spjd
661219089Spjd	if (BP_GET_LEVEL(bp) > 0) {
662219089Spjd		uint32_t flags = ARC_WAIT;
663219089Spjd		int i;
664219089Spjd		blkptr_t *cbp;
665219089Spjd		int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
666219089Spjd
667219089Spjd		err = arc_read_nolock(NULL, dp->dp_spa, bp,
668219089Spjd		    arc_getbuf_func, bufp,
669219089Spjd		    ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
670219089Spjd		if (err) {
671219089Spjd			scn->scn_phys.scn_errors++;
672219089Spjd			return (err);
673219089Spjd		}
674219089Spjd		for (i = 0, cbp = (*bufp)->b_data; i < epb; i++, cbp++) {
675219089Spjd			dsl_scan_prefetch(scn, *bufp, cbp, zb->zb_objset,
676219089Spjd			    zb->zb_object, zb->zb_blkid * epb + i);
677219089Spjd		}
678219089Spjd		for (i = 0, cbp = (*bufp)->b_data; i < epb; i++, cbp++) {
679219089Spjd			zbookmark_t czb;
680219089Spjd
681219089Spjd			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
682219089Spjd			    zb->zb_level - 1,
683219089Spjd			    zb->zb_blkid * epb + i);
684219089Spjd			dsl_scan_visitbp(cbp, &czb, dnp,
685219089Spjd			    *bufp, ds, scn, ostype, tx);
686219089Spjd		}
687219089Spjd	} else if (BP_GET_TYPE(bp) == DMU_OT_USERGROUP_USED) {
688219089Spjd		uint32_t flags = ARC_WAIT;
689219089Spjd
690219089Spjd		err = arc_read_nolock(NULL, dp->dp_spa, bp,
691219089Spjd		    arc_getbuf_func, bufp,
692219089Spjd		    ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
693219089Spjd		if (err) {
694219089Spjd			scn->scn_phys.scn_errors++;
695219089Spjd			return (err);
696219089Spjd		}
697219089Spjd	} else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) {
698219089Spjd		uint32_t flags = ARC_WAIT;
699219089Spjd		dnode_phys_t *cdnp;
700219089Spjd		int i, j;
701219089Spjd		int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
702219089Spjd
703219089Spjd		err = arc_read_nolock(NULL, dp->dp_spa, bp,
704219089Spjd		    arc_getbuf_func, bufp,
705219089Spjd		    ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
706219089Spjd		if (err) {
707219089Spjd			scn->scn_phys.scn_errors++;
708219089Spjd			return (err);
709219089Spjd		}
710219089Spjd		for (i = 0, cdnp = (*bufp)->b_data; i < epb; i++, cdnp++) {
711219089Spjd			for (j = 0; j < cdnp->dn_nblkptr; j++) {
712219089Spjd				blkptr_t *cbp = &cdnp->dn_blkptr[j];
713219089Spjd				dsl_scan_prefetch(scn, *bufp, cbp,
714219089Spjd				    zb->zb_objset, zb->zb_blkid * epb + i, j);
715219089Spjd			}
716219089Spjd		}
717219089Spjd		for (i = 0, cdnp = (*bufp)->b_data; i < epb; i++, cdnp++) {
718219089Spjd			dsl_scan_visitdnode(scn, ds, ostype,
719219089Spjd			    cdnp, *bufp, zb->zb_blkid * epb + i, tx);
720219089Spjd		}
721219089Spjd
722219089Spjd	} else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) {
723219089Spjd		uint32_t flags = ARC_WAIT;
724219089Spjd		objset_phys_t *osp;
725219089Spjd
726219089Spjd		err = arc_read_nolock(NULL, dp->dp_spa, bp,
727219089Spjd		    arc_getbuf_func, bufp,
728219089Spjd		    ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
729219089Spjd		if (err) {
730219089Spjd			scn->scn_phys.scn_errors++;
731219089Spjd			return (err);
732219089Spjd		}
733219089Spjd
734219089Spjd		osp = (*bufp)->b_data;
735219089Spjd
736219089Spjd		dsl_scan_visitdnode(scn, ds, osp->os_type,
737219089Spjd		    &osp->os_meta_dnode, *bufp, DMU_META_DNODE_OBJECT, tx);
738219089Spjd
739219089Spjd		if (OBJSET_BUF_HAS_USERUSED(*bufp)) {
740219089Spjd			/*
741219089Spjd			 * We also always visit user/group accounting
742219089Spjd			 * objects, and never skip them, even if we are
743219089Spjd			 * pausing.  This is necessary so that the space
744219089Spjd			 * deltas from this txg get integrated.
745219089Spjd			 */
746219089Spjd			dsl_scan_visitdnode(scn, ds, osp->os_type,
747219089Spjd			    &osp->os_groupused_dnode, *bufp,
748219089Spjd			    DMU_GROUPUSED_OBJECT, tx);
749219089Spjd			dsl_scan_visitdnode(scn, ds, osp->os_type,
750219089Spjd			    &osp->os_userused_dnode, *bufp,
751219089Spjd			    DMU_USERUSED_OBJECT, tx);
752219089Spjd		}
753219089Spjd	}
754219089Spjd
755219089Spjd	return (0);
756219089Spjd}
757219089Spjd
758219089Spjdstatic void
759219089Spjddsl_scan_visitdnode(dsl_scan_t *scn, dsl_dataset_t *ds,
760219089Spjd    dmu_objset_type_t ostype, dnode_phys_t *dnp, arc_buf_t *buf,
761219089Spjd    uint64_t object, dmu_tx_t *tx)
762219089Spjd{
763219089Spjd	int j;
764219089Spjd
765219089Spjd	for (j = 0; j < dnp->dn_nblkptr; j++) {
766219089Spjd		zbookmark_t czb;
767219089Spjd
768219089Spjd		SET_BOOKMARK(&czb, ds ? ds->ds_object : 0, object,
769219089Spjd		    dnp->dn_nlevels - 1, j);
770219089Spjd		dsl_scan_visitbp(&dnp->dn_blkptr[j],
771219089Spjd		    &czb, dnp, buf, ds, scn, ostype, tx);
772219089Spjd	}
773219089Spjd
774219089Spjd	if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
775219089Spjd		zbookmark_t czb;
776219089Spjd		SET_BOOKMARK(&czb, ds ? ds->ds_object : 0, object,
777219089Spjd		    0, DMU_SPILL_BLKID);
778219089Spjd		dsl_scan_visitbp(&dnp->dn_spill,
779219089Spjd		    &czb, dnp, buf, ds, scn, ostype, tx);
780219089Spjd	}
781219089Spjd}
782219089Spjd
783219089Spjd/*
784219089Spjd * The arguments are in this order because mdb can only print the
785219089Spjd * first 5; we want them to be useful.
786219089Spjd */
787219089Spjdstatic void
788219089Spjddsl_scan_visitbp(blkptr_t *bp, const zbookmark_t *zb,
789219089Spjd    dnode_phys_t *dnp, arc_buf_t *pbuf,
790219089Spjd    dsl_dataset_t *ds, dsl_scan_t *scn, dmu_objset_type_t ostype,
791219089Spjd    dmu_tx_t *tx)
792219089Spjd{
793219089Spjd	dsl_pool_t *dp = scn->scn_dp;
794219089Spjd	arc_buf_t *buf = NULL;
795219089Spjd	blkptr_t bp_toread = *bp;
796219089Spjd
797219089Spjd	/* ASSERT(pbuf == NULL || arc_released(pbuf)); */
798219089Spjd
799219089Spjd	if (dsl_scan_check_pause(scn, zb))
800219089Spjd		return;
801219089Spjd
802219089Spjd	if (dsl_scan_check_resume(scn, dnp, zb))
803219089Spjd		return;
804219089Spjd
805219089Spjd	if (bp->blk_birth == 0)
806219089Spjd		return;
807219089Spjd
808219089Spjd	scn->scn_visited_this_txg++;
809219089Spjd
810219089Spjd	dprintf_bp(bp,
811219089Spjd	    "visiting ds=%p/%llu zb=%llx/%llx/%llx/%llx buf=%p bp=%p",
812219089Spjd	    ds, ds ? ds->ds_object : 0,
813219089Spjd	    zb->zb_objset, zb->zb_object, zb->zb_level, zb->zb_blkid,
814219089Spjd	    pbuf, bp);
815219089Spjd
816219089Spjd	if (bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
817219089Spjd		return;
818219089Spjd
819219089Spjd	if (BP_GET_TYPE(bp) != DMU_OT_USERGROUP_USED) {
820219089Spjd		/*
821219089Spjd		 * For non-user-accounting blocks, we need to read the
822219089Spjd		 * new bp (from a deleted snapshot, found in
823219089Spjd		 * check_existing_xlation).  If we used the old bp,
824219089Spjd		 * pointers inside this block from before we resumed
825219089Spjd		 * would be untranslated.
826219089Spjd		 *
827219089Spjd		 * For user-accounting blocks, we need to read the old
828219089Spjd		 * bp, because we will apply the entire space delta to
829219089Spjd		 * it (original untranslated -> translations from
830219089Spjd		 * deleted snap -> now).
831219089Spjd		 */
832219089Spjd		bp_toread = *bp;
833219089Spjd	}
834219089Spjd
835219089Spjd	if (dsl_scan_recurse(scn, ds, ostype, dnp, &bp_toread, zb, tx,
836219089Spjd	    &buf) != 0)
837219089Spjd		return;
838219089Spjd
839219089Spjd	/*
840219089Spjd	 * If dsl_scan_ddt() has aready visited this block, it will have
841219089Spjd	 * already done any translations or scrubbing, so don't call the
842219089Spjd	 * callback again.
843219089Spjd	 */
844219089Spjd	if (ddt_class_contains(dp->dp_spa,
845219089Spjd	    scn->scn_phys.scn_ddt_class_max, bp)) {
846219089Spjd		ASSERT(buf == NULL);
847219089Spjd		return;
848219089Spjd	}
849219089Spjd
850219089Spjd	/*
851219089Spjd	 * If this block is from the future (after cur_max_txg), then we
852219089Spjd	 * are doing this on behalf of a deleted snapshot, and we will
853219089Spjd	 * revisit the future block on the next pass of this dataset.
854219089Spjd	 * Don't scan it now unless we need to because something
855219089Spjd	 * under it was modified.
856219089Spjd	 */
857219089Spjd	if (bp->blk_birth <= scn->scn_phys.scn_cur_max_txg) {
858219089Spjd		scan_funcs[scn->scn_phys.scn_func](dp, bp, zb);
859219089Spjd	}
860219089Spjd	if (buf)
861219089Spjd		(void) arc_buf_remove_ref(buf, &buf);
862219089Spjd}
863219089Spjd
864219089Spjdstatic void
865219089Spjddsl_scan_visit_rootbp(dsl_scan_t *scn, dsl_dataset_t *ds, blkptr_t *bp,
866219089Spjd    dmu_tx_t *tx)
867219089Spjd{
868219089Spjd	zbookmark_t zb;
869219089Spjd
870219089Spjd	SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
871219089Spjd	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
872219089Spjd	dsl_scan_visitbp(bp, &zb, NULL, NULL,
873219089Spjd	    ds, scn, DMU_OST_NONE, tx);
874219089Spjd
875219089Spjd	dprintf_ds(ds, "finished scan%s", "");
876219089Spjd}
877219089Spjd
878219089Spjdvoid
879219089Spjddsl_scan_ds_destroyed(dsl_dataset_t *ds, dmu_tx_t *tx)
880219089Spjd{
881219089Spjd	dsl_pool_t *dp = ds->ds_dir->dd_pool;
882219089Spjd	dsl_scan_t *scn = dp->dp_scan;
883219089Spjd	uint64_t mintxg;
884219089Spjd
885219089Spjd	if (scn->scn_phys.scn_state != DSS_SCANNING)
886219089Spjd		return;
887219089Spjd
888219089Spjd	if (scn->scn_phys.scn_bookmark.zb_objset == ds->ds_object) {
889219089Spjd		if (dsl_dataset_is_snapshot(ds)) {
890219089Spjd			/* Note, scn_cur_{min,max}_txg stays the same. */
891219089Spjd			scn->scn_phys.scn_bookmark.zb_objset =
892219089Spjd			    ds->ds_phys->ds_next_snap_obj;
893219089Spjd			zfs_dbgmsg("destroying ds %llu; currently traversing; "
894219089Spjd			    "reset zb_objset to %llu",
895219089Spjd			    (u_longlong_t)ds->ds_object,
896219089Spjd			    (u_longlong_t)ds->ds_phys->ds_next_snap_obj);
897219089Spjd			scn->scn_phys.scn_flags |= DSF_VISIT_DS_AGAIN;
898219089Spjd		} else {
899219089Spjd			SET_BOOKMARK(&scn->scn_phys.scn_bookmark,
900219089Spjd			    ZB_DESTROYED_OBJSET, 0, 0, 0);
901219089Spjd			zfs_dbgmsg("destroying ds %llu; currently traversing; "
902219089Spjd			    "reset bookmark to -1,0,0,0",
903219089Spjd			    (u_longlong_t)ds->ds_object);
904219089Spjd		}
905219089Spjd	} else if (zap_lookup_int_key(dp->dp_meta_objset,
906219089Spjd	    scn->scn_phys.scn_queue_obj, ds->ds_object, &mintxg) == 0) {
907219089Spjd		ASSERT3U(ds->ds_phys->ds_num_children, <=, 1);
908219089Spjd		VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
909219089Spjd		    scn->scn_phys.scn_queue_obj, ds->ds_object, tx));
910219089Spjd		if (dsl_dataset_is_snapshot(ds)) {
911219089Spjd			/*
912219089Spjd			 * We keep the same mintxg; it could be >
913219089Spjd			 * ds_creation_txg if the previous snapshot was
914219089Spjd			 * deleted too.
915219089Spjd			 */
916219089Spjd			VERIFY(zap_add_int_key(dp->dp_meta_objset,
917219089Spjd			    scn->scn_phys.scn_queue_obj,
918219089Spjd			    ds->ds_phys->ds_next_snap_obj, mintxg, tx) == 0);
919219089Spjd			zfs_dbgmsg("destroying ds %llu; in queue; "
920219089Spjd			    "replacing with %llu",
921219089Spjd			    (u_longlong_t)ds->ds_object,
922219089Spjd			    (u_longlong_t)ds->ds_phys->ds_next_snap_obj);
923219089Spjd		} else {
924219089Spjd			zfs_dbgmsg("destroying ds %llu; in queue; removing",
925219089Spjd			    (u_longlong_t)ds->ds_object);
926219089Spjd		}
927219089Spjd	} else {
928219089Spjd		zfs_dbgmsg("destroying ds %llu; ignoring",
929219089Spjd		    (u_longlong_t)ds->ds_object);
930219089Spjd	}
931219089Spjd
932219089Spjd	/*
933219089Spjd	 * dsl_scan_sync() should be called after this, and should sync
934219089Spjd	 * out our changed state, but just to be safe, do it here.
935219089Spjd	 */
936219089Spjd	dsl_scan_sync_state(scn, tx);
937219089Spjd}
938219089Spjd
939219089Spjdvoid
940219089Spjddsl_scan_ds_snapshotted(dsl_dataset_t *ds, dmu_tx_t *tx)
941219089Spjd{
942219089Spjd	dsl_pool_t *dp = ds->ds_dir->dd_pool;
943219089Spjd	dsl_scan_t *scn = dp->dp_scan;
944219089Spjd	uint64_t mintxg;
945219089Spjd
946219089Spjd	if (scn->scn_phys.scn_state != DSS_SCANNING)
947219089Spjd		return;
948219089Spjd
949219089Spjd	ASSERT(ds->ds_phys->ds_prev_snap_obj != 0);
950219089Spjd
951219089Spjd	if (scn->scn_phys.scn_bookmark.zb_objset == ds->ds_object) {
952219089Spjd		scn->scn_phys.scn_bookmark.zb_objset =
953219089Spjd		    ds->ds_phys->ds_prev_snap_obj;
954219089Spjd		zfs_dbgmsg("snapshotting ds %llu; currently traversing; "
955219089Spjd		    "reset zb_objset to %llu",
956219089Spjd		    (u_longlong_t)ds->ds_object,
957219089Spjd		    (u_longlong_t)ds->ds_phys->ds_prev_snap_obj);
958219089Spjd	} else if (zap_lookup_int_key(dp->dp_meta_objset,
959219089Spjd	    scn->scn_phys.scn_queue_obj, ds->ds_object, &mintxg) == 0) {
960219089Spjd		VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
961219089Spjd		    scn->scn_phys.scn_queue_obj, ds->ds_object, tx));
962219089Spjd		VERIFY(zap_add_int_key(dp->dp_meta_objset,
963219089Spjd		    scn->scn_phys.scn_queue_obj,
964219089Spjd		    ds->ds_phys->ds_prev_snap_obj, mintxg, tx) == 0);
965219089Spjd		zfs_dbgmsg("snapshotting ds %llu; in queue; "
966219089Spjd		    "replacing with %llu",
967219089Spjd		    (u_longlong_t)ds->ds_object,
968219089Spjd		    (u_longlong_t)ds->ds_phys->ds_prev_snap_obj);
969219089Spjd	}
970219089Spjd	dsl_scan_sync_state(scn, tx);
971219089Spjd}
972219089Spjd
973219089Spjdvoid
974219089Spjddsl_scan_ds_clone_swapped(dsl_dataset_t *ds1, dsl_dataset_t *ds2, dmu_tx_t *tx)
975219089Spjd{
976219089Spjd	dsl_pool_t *dp = ds1->ds_dir->dd_pool;
977219089Spjd	dsl_scan_t *scn = dp->dp_scan;
978219089Spjd	uint64_t mintxg;
979219089Spjd
980219089Spjd	if (scn->scn_phys.scn_state != DSS_SCANNING)
981219089Spjd		return;
982219089Spjd
983219089Spjd	if (scn->scn_phys.scn_bookmark.zb_objset == ds1->ds_object) {
984219089Spjd		scn->scn_phys.scn_bookmark.zb_objset = ds2->ds_object;
985219089Spjd		zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
986219089Spjd		    "reset zb_objset to %llu",
987219089Spjd		    (u_longlong_t)ds1->ds_object,
988219089Spjd		    (u_longlong_t)ds2->ds_object);
989219089Spjd	} else if (scn->scn_phys.scn_bookmark.zb_objset == ds2->ds_object) {
990219089Spjd		scn->scn_phys.scn_bookmark.zb_objset = ds1->ds_object;
991219089Spjd		zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
992219089Spjd		    "reset zb_objset to %llu",
993219089Spjd		    (u_longlong_t)ds2->ds_object,
994219089Spjd		    (u_longlong_t)ds1->ds_object);
995219089Spjd	}
996219089Spjd
997219089Spjd	if (zap_lookup_int_key(dp->dp_meta_objset, scn->scn_phys.scn_queue_obj,
998219089Spjd	    ds1->ds_object, &mintxg) == 0) {
999219089Spjd		int err;
1000219089Spjd
1001219089Spjd		ASSERT3U(mintxg, ==, ds1->ds_phys->ds_prev_snap_txg);
1002219089Spjd		ASSERT3U(mintxg, ==, ds2->ds_phys->ds_prev_snap_txg);
1003219089Spjd		VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
1004219089Spjd		    scn->scn_phys.scn_queue_obj, ds1->ds_object, tx));
1005219089Spjd		err = zap_add_int_key(dp->dp_meta_objset,
1006219089Spjd		    scn->scn_phys.scn_queue_obj, ds2->ds_object, mintxg, tx);
1007219089Spjd		VERIFY(err == 0 || err == EEXIST);
1008219089Spjd		if (err == EEXIST) {
1009219089Spjd			/* Both were there to begin with */
1010219089Spjd			VERIFY(0 == zap_add_int_key(dp->dp_meta_objset,
1011219089Spjd			    scn->scn_phys.scn_queue_obj,
1012219089Spjd			    ds1->ds_object, mintxg, tx));
1013219089Spjd		}
1014219089Spjd		zfs_dbgmsg("clone_swap ds %llu; in queue; "
1015219089Spjd		    "replacing with %llu",
1016219089Spjd		    (u_longlong_t)ds1->ds_object,
1017219089Spjd		    (u_longlong_t)ds2->ds_object);
1018219089Spjd	} else if (zap_lookup_int_key(dp->dp_meta_objset,
1019219089Spjd	    scn->scn_phys.scn_queue_obj, ds2->ds_object, &mintxg) == 0) {
1020219089Spjd		ASSERT3U(mintxg, ==, ds1->ds_phys->ds_prev_snap_txg);
1021219089Spjd		ASSERT3U(mintxg, ==, ds2->ds_phys->ds_prev_snap_txg);
1022219089Spjd		VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
1023219089Spjd		    scn->scn_phys.scn_queue_obj, ds2->ds_object, tx));
1024219089Spjd		VERIFY(0 == zap_add_int_key(dp->dp_meta_objset,
1025219089Spjd		    scn->scn_phys.scn_queue_obj, ds1->ds_object, mintxg, tx));
1026219089Spjd		zfs_dbgmsg("clone_swap ds %llu; in queue; "
1027219089Spjd		    "replacing with %llu",
1028219089Spjd		    (u_longlong_t)ds2->ds_object,
1029219089Spjd		    (u_longlong_t)ds1->ds_object);
1030219089Spjd	}
1031219089Spjd
1032219089Spjd	dsl_scan_sync_state(scn, tx);
1033219089Spjd}
1034219089Spjd
1035219089Spjdstruct enqueue_clones_arg {
1036219089Spjd	dmu_tx_t *tx;
1037219089Spjd	uint64_t originobj;
1038219089Spjd};
1039219089Spjd
1040219089Spjd/* ARGSUSED */
1041219089Spjdstatic int
1042219089Spjdenqueue_clones_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
1043219089Spjd{
1044219089Spjd	struct enqueue_clones_arg *eca = arg;
1045219089Spjd	dsl_dataset_t *ds;
1046219089Spjd	int err;
1047219089Spjd	dsl_pool_t *dp = spa->spa_dsl_pool;
1048219089Spjd	dsl_scan_t *scn = dp->dp_scan;
1049219089Spjd
1050219089Spjd	err = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds);
1051219089Spjd	if (err)
1052219089Spjd		return (err);
1053219089Spjd
1054219089Spjd	if (ds->ds_dir->dd_phys->dd_origin_obj == eca->originobj) {
1055219089Spjd		while (ds->ds_phys->ds_prev_snap_obj != eca->originobj) {
1056219089Spjd			dsl_dataset_t *prev;
1057219089Spjd			err = dsl_dataset_hold_obj(dp,
1058219089Spjd			    ds->ds_phys->ds_prev_snap_obj, FTAG, &prev);
1059219089Spjd
1060219089Spjd			dsl_dataset_rele(ds, FTAG);
1061219089Spjd			if (err)
1062219089Spjd				return (err);
1063219089Spjd			ds = prev;
1064219089Spjd		}
1065219089Spjd		VERIFY(zap_add_int_key(dp->dp_meta_objset,
1066219089Spjd		    scn->scn_phys.scn_queue_obj, ds->ds_object,
1067219089Spjd		    ds->ds_phys->ds_prev_snap_txg, eca->tx) == 0);
1068219089Spjd	}
1069219089Spjd	dsl_dataset_rele(ds, FTAG);
1070219089Spjd	return (0);
1071219089Spjd}
1072219089Spjd
1073219089Spjdstatic void
1074219089Spjddsl_scan_visitds(dsl_scan_t *scn, uint64_t dsobj, dmu_tx_t *tx)
1075219089Spjd{
1076219089Spjd	dsl_pool_t *dp = scn->scn_dp;
1077219089Spjd	dsl_dataset_t *ds;
1078219089Spjd	objset_t *os;
1079219089Spjd
1080219089Spjd	VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
1081219089Spjd
1082219089Spjd	if (dmu_objset_from_ds(ds, &os))
1083219089Spjd		goto out;
1084219089Spjd
1085219089Spjd	/*
1086219089Spjd	 * Only the ZIL in the head (non-snapshot) is valid.  Even though
1087219089Spjd	 * snapshots can have ZIL block pointers (which may be the same
1088219089Spjd	 * BP as in the head), they must be ignored.  So we traverse the
1089219089Spjd	 * ZIL here, rather than in scan_recurse(), because the regular
1090219089Spjd	 * snapshot block-sharing rules don't apply to it.
1091219089Spjd	 */
1092219089Spjd	if (DSL_SCAN_IS_SCRUB_RESILVER(scn) && !dsl_dataset_is_snapshot(ds))
1093219089Spjd		dsl_scan_zil(dp, &os->os_zil_header);
1094219089Spjd
1095219089Spjd	/*
1096219089Spjd	 * Iterate over the bps in this ds.
1097219089Spjd	 */
1098219089Spjd	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1099219089Spjd	dsl_scan_visit_rootbp(scn, ds, &ds->ds_phys->ds_bp, tx);
1100219089Spjd
1101219089Spjd	char *dsname = kmem_alloc(ZFS_MAXNAMELEN, KM_SLEEP);
1102219089Spjd	dsl_dataset_name(ds, dsname);
1103219089Spjd	zfs_dbgmsg("scanned dataset %llu (%s) with min=%llu max=%llu; "
1104219089Spjd	    "pausing=%u",
1105219089Spjd	    (longlong_t)dsobj, dsname,
1106219089Spjd	    (longlong_t)scn->scn_phys.scn_cur_min_txg,
1107219089Spjd	    (longlong_t)scn->scn_phys.scn_cur_max_txg,
1108219089Spjd	    (int)scn->scn_pausing);
1109219089Spjd	kmem_free(dsname, ZFS_MAXNAMELEN);
1110219089Spjd
1111219089Spjd	if (scn->scn_pausing)
1112219089Spjd		goto out;
1113219089Spjd
1114219089Spjd	/*
1115219089Spjd	 * We've finished this pass over this dataset.
1116219089Spjd	 */
1117219089Spjd
1118219089Spjd	/*
1119219089Spjd	 * If we did not completely visit this dataset, do another pass.
1120219089Spjd	 */
1121219089Spjd	if (scn->scn_phys.scn_flags & DSF_VISIT_DS_AGAIN) {
1122219089Spjd		zfs_dbgmsg("incomplete pass; visiting again");
1123219089Spjd		scn->scn_phys.scn_flags &= ~DSF_VISIT_DS_AGAIN;
1124219089Spjd		VERIFY(zap_add_int_key(dp->dp_meta_objset,
1125219089Spjd		    scn->scn_phys.scn_queue_obj, ds->ds_object,
1126219089Spjd		    scn->scn_phys.scn_cur_max_txg, tx) == 0);
1127219089Spjd		goto out;
1128219089Spjd	}
1129219089Spjd
1130219089Spjd	/*
1131219089Spjd	 * Add descendent datasets to work queue.
1132219089Spjd	 */
1133219089Spjd	if (ds->ds_phys->ds_next_snap_obj != 0) {
1134219089Spjd		VERIFY(zap_add_int_key(dp->dp_meta_objset,
1135219089Spjd		    scn->scn_phys.scn_queue_obj, ds->ds_phys->ds_next_snap_obj,
1136219089Spjd		    ds->ds_phys->ds_creation_txg, tx) == 0);
1137219089Spjd	}
1138219089Spjd	if (ds->ds_phys->ds_num_children > 1) {
1139219089Spjd		boolean_t usenext = B_FALSE;
1140219089Spjd		if (ds->ds_phys->ds_next_clones_obj != 0) {
1141219089Spjd			uint64_t count;
1142219089Spjd			/*
1143219089Spjd			 * A bug in a previous version of the code could
1144219089Spjd			 * cause upgrade_clones_cb() to not set
1145219089Spjd			 * ds_next_snap_obj when it should, leading to a
1146219089Spjd			 * missing entry.  Therefore we can only use the
1147219089Spjd			 * next_clones_obj when its count is correct.
1148219089Spjd			 */
1149219089Spjd			int err = zap_count(dp->dp_meta_objset,
1150219089Spjd			    ds->ds_phys->ds_next_clones_obj, &count);
1151219089Spjd			if (err == 0 &&
1152219089Spjd			    count == ds->ds_phys->ds_num_children - 1)
1153219089Spjd				usenext = B_TRUE;
1154219089Spjd		}
1155219089Spjd
1156219089Spjd		if (usenext) {
1157219089Spjd			VERIFY(zap_join_key(dp->dp_meta_objset,
1158219089Spjd			    ds->ds_phys->ds_next_clones_obj,
1159219089Spjd			    scn->scn_phys.scn_queue_obj,
1160219089Spjd			    ds->ds_phys->ds_creation_txg, tx) == 0);
1161219089Spjd		} else {
1162219089Spjd			struct enqueue_clones_arg eca;
1163219089Spjd			eca.tx = tx;
1164219089Spjd			eca.originobj = ds->ds_object;
1165219089Spjd
1166219089Spjd			(void) dmu_objset_find_spa(ds->ds_dir->dd_pool->dp_spa,
1167219089Spjd			    NULL, enqueue_clones_cb, &eca, DS_FIND_CHILDREN);
1168219089Spjd		}
1169219089Spjd	}
1170219089Spjd
1171219089Spjdout:
1172219089Spjd	dsl_dataset_rele(ds, FTAG);
1173219089Spjd}
1174219089Spjd
1175219089Spjd/* ARGSUSED */
1176219089Spjdstatic int
1177219089Spjdenqueue_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
1178219089Spjd{
1179219089Spjd	dmu_tx_t *tx = arg;
1180219089Spjd	dsl_dataset_t *ds;
1181219089Spjd	int err;
1182219089Spjd	dsl_pool_t *dp = spa->spa_dsl_pool;
1183219089Spjd	dsl_scan_t *scn = dp->dp_scan;
1184219089Spjd
1185219089Spjd	err = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds);
1186219089Spjd	if (err)
1187219089Spjd		return (err);
1188219089Spjd
1189219089Spjd	while (ds->ds_phys->ds_prev_snap_obj != 0) {
1190219089Spjd		dsl_dataset_t *prev;
1191219089Spjd		err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
1192219089Spjd		    FTAG, &prev);
1193219089Spjd		if (err) {
1194219089Spjd			dsl_dataset_rele(ds, FTAG);
1195219089Spjd			return (err);
1196219089Spjd		}
1197219089Spjd
1198219089Spjd		/*
1199219089Spjd		 * If this is a clone, we don't need to worry about it for now.
1200219089Spjd		 */
1201219089Spjd		if (prev->ds_phys->ds_next_snap_obj != ds->ds_object) {
1202219089Spjd			dsl_dataset_rele(ds, FTAG);
1203219089Spjd			dsl_dataset_rele(prev, FTAG);
1204219089Spjd			return (0);
1205219089Spjd		}
1206219089Spjd		dsl_dataset_rele(ds, FTAG);
1207219089Spjd		ds = prev;
1208219089Spjd	}
1209219089Spjd
1210219089Spjd	VERIFY(zap_add_int_key(dp->dp_meta_objset, scn->scn_phys.scn_queue_obj,
1211219089Spjd	    ds->ds_object, ds->ds_phys->ds_prev_snap_txg, tx) == 0);
1212219089Spjd	dsl_dataset_rele(ds, FTAG);
1213219089Spjd	return (0);
1214219089Spjd}
1215219089Spjd
1216219089Spjd/*
1217219089Spjd * Scrub/dedup interaction.
1218219089Spjd *
1219219089Spjd * If there are N references to a deduped block, we don't want to scrub it
1220219089Spjd * N times -- ideally, we should scrub it exactly once.
1221219089Spjd *
1222219089Spjd * We leverage the fact that the dde's replication class (enum ddt_class)
1223219089Spjd * is ordered from highest replication class (DDT_CLASS_DITTO) to lowest
1224219089Spjd * (DDT_CLASS_UNIQUE) so that we may walk the DDT in that order.
1225219089Spjd *
1226219089Spjd * To prevent excess scrubbing, the scrub begins by walking the DDT
1227219089Spjd * to find all blocks with refcnt > 1, and scrubs each of these once.
1228219089Spjd * Since there are two replication classes which contain blocks with
1229219089Spjd * refcnt > 1, we scrub the highest replication class (DDT_CLASS_DITTO) first.
1230219089Spjd * Finally the top-down scrub begins, only visiting blocks with refcnt == 1.
1231219089Spjd *
1232219089Spjd * There would be nothing more to say if a block's refcnt couldn't change
1233219089Spjd * during a scrub, but of course it can so we must account for changes
1234219089Spjd * in a block's replication class.
1235219089Spjd *
1236219089Spjd * Here's an example of what can occur:
1237219089Spjd *
1238219089Spjd * If a block has refcnt > 1 during the DDT scrub phase, but has refcnt == 1
1239219089Spjd * when visited during the top-down scrub phase, it will be scrubbed twice.
1240219089Spjd * This negates our scrub optimization, but is otherwise harmless.
1241219089Spjd *
1242219089Spjd * If a block has refcnt == 1 during the DDT scrub phase, but has refcnt > 1
1243219089Spjd * on each visit during the top-down scrub phase, it will never be scrubbed.
1244219089Spjd * To catch this, ddt_sync_entry() notifies the scrub code whenever a block's
1245219089Spjd * reference class transitions to a higher level (i.e DDT_CLASS_UNIQUE to
1246219089Spjd * DDT_CLASS_DUPLICATE); if it transitions from refcnt == 1 to refcnt > 1
1247219089Spjd * while a scrub is in progress, it scrubs the block right then.
1248219089Spjd */
1249219089Spjdstatic void
1250219089Spjddsl_scan_ddt(dsl_scan_t *scn, dmu_tx_t *tx)
1251219089Spjd{
1252219089Spjd	ddt_bookmark_t *ddb = &scn->scn_phys.scn_ddt_bookmark;
1253219089Spjd	ddt_entry_t dde = { 0 };
1254219089Spjd	int error;
1255219089Spjd	uint64_t n = 0;
1256219089Spjd
1257219089Spjd	while ((error = ddt_walk(scn->scn_dp->dp_spa, ddb, &dde)) == 0) {
1258219089Spjd		ddt_t *ddt;
1259219089Spjd
1260219089Spjd		if (ddb->ddb_class > scn->scn_phys.scn_ddt_class_max)
1261219089Spjd			break;
1262219089Spjd		dprintf("visiting ddb=%llu/%llu/%llu/%llx\n",
1263219089Spjd		    (longlong_t)ddb->ddb_class,
1264219089Spjd		    (longlong_t)ddb->ddb_type,
1265219089Spjd		    (longlong_t)ddb->ddb_checksum,
1266219089Spjd		    (longlong_t)ddb->ddb_cursor);
1267219089Spjd
1268219089Spjd		/* There should be no pending changes to the dedup table */
1269219089Spjd		ddt = scn->scn_dp->dp_spa->spa_ddt[ddb->ddb_checksum];
1270219089Spjd		ASSERT(avl_first(&ddt->ddt_tree) == NULL);
1271219089Spjd
1272219089Spjd		dsl_scan_ddt_entry(scn, ddb->ddb_checksum, &dde, tx);
1273219089Spjd		n++;
1274219089Spjd
1275219089Spjd		if (dsl_scan_check_pause(scn, NULL))
1276219089Spjd			break;
1277219089Spjd	}
1278219089Spjd
1279219089Spjd	zfs_dbgmsg("scanned %llu ddt entries with class_max = %u; pausing=%u",
1280219089Spjd	    (longlong_t)n, (int)scn->scn_phys.scn_ddt_class_max,
1281219089Spjd	    (int)scn->scn_pausing);
1282219089Spjd
1283219089Spjd	ASSERT(error == 0 || error == ENOENT);
1284219089Spjd	ASSERT(error != ENOENT ||
1285219089Spjd	    ddb->ddb_class > scn->scn_phys.scn_ddt_class_max);
1286219089Spjd}
1287219089Spjd
1288219089Spjd/* ARGSUSED */
1289219089Spjdvoid
1290219089Spjddsl_scan_ddt_entry(dsl_scan_t *scn, enum zio_checksum checksum,
1291219089Spjd    ddt_entry_t *dde, dmu_tx_t *tx)
1292219089Spjd{
1293219089Spjd	const ddt_key_t *ddk = &dde->dde_key;
1294219089Spjd	ddt_phys_t *ddp = dde->dde_phys;
1295219089Spjd	blkptr_t bp;
1296219089Spjd	zbookmark_t zb = { 0 };
1297219089Spjd
1298219089Spjd	if (scn->scn_phys.scn_state != DSS_SCANNING)
1299219089Spjd		return;
1300219089Spjd
1301219089Spjd	for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
1302219089Spjd		if (ddp->ddp_phys_birth == 0 ||
1303219089Spjd		    ddp->ddp_phys_birth > scn->scn_phys.scn_cur_max_txg)
1304219089Spjd			continue;
1305219089Spjd		ddt_bp_create(checksum, ddk, ddp, &bp);
1306219089Spjd
1307219089Spjd		scn->scn_visited_this_txg++;
1308219089Spjd		scan_funcs[scn->scn_phys.scn_func](scn->scn_dp, &bp, &zb);
1309219089Spjd	}
1310219089Spjd}
1311219089Spjd
1312219089Spjdstatic void
1313219089Spjddsl_scan_visit(dsl_scan_t *scn, dmu_tx_t *tx)
1314219089Spjd{
1315219089Spjd	dsl_pool_t *dp = scn->scn_dp;
1316219089Spjd	zap_cursor_t zc;
1317219089Spjd	zap_attribute_t za;
1318219089Spjd
1319219089Spjd	if (scn->scn_phys.scn_ddt_bookmark.ddb_class <=
1320219089Spjd	    scn->scn_phys.scn_ddt_class_max) {
1321219089Spjd		scn->scn_phys.scn_cur_min_txg = scn->scn_phys.scn_min_txg;
1322219089Spjd		scn->scn_phys.scn_cur_max_txg = scn->scn_phys.scn_max_txg;
1323219089Spjd		dsl_scan_ddt(scn, tx);
1324219089Spjd		if (scn->scn_pausing)
1325219089Spjd			return;
1326219089Spjd	}
1327219089Spjd
1328219089Spjd	if (scn->scn_phys.scn_bookmark.zb_objset == DMU_META_OBJSET) {
1329219089Spjd		/* First do the MOS & ORIGIN */
1330219089Spjd
1331219089Spjd		scn->scn_phys.scn_cur_min_txg = scn->scn_phys.scn_min_txg;
1332219089Spjd		scn->scn_phys.scn_cur_max_txg = scn->scn_phys.scn_max_txg;
1333219089Spjd		dsl_scan_visit_rootbp(scn, NULL,
1334219089Spjd		    &dp->dp_meta_rootbp, tx);
1335219089Spjd		spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
1336219089Spjd		if (scn->scn_pausing)
1337219089Spjd			return;
1338219089Spjd
1339219089Spjd		if (spa_version(dp->dp_spa) < SPA_VERSION_DSL_SCRUB) {
1340219089Spjd			VERIFY(0 == dmu_objset_find_spa(dp->dp_spa,
1341219089Spjd			    NULL, enqueue_cb, tx, DS_FIND_CHILDREN));
1342219089Spjd		} else {
1343219089Spjd			dsl_scan_visitds(scn,
1344219089Spjd			    dp->dp_origin_snap->ds_object, tx);
1345219089Spjd		}
1346219089Spjd		ASSERT(!scn->scn_pausing);
1347219089Spjd	} else if (scn->scn_phys.scn_bookmark.zb_objset !=
1348219089Spjd	    ZB_DESTROYED_OBJSET) {
1349219089Spjd		/*
1350219089Spjd		 * If we were paused, continue from here.  Note if the
1351219089Spjd		 * ds we were paused on was deleted, the zb_objset may
1352219089Spjd		 * be -1, so we will skip this and find a new objset
1353219089Spjd		 * below.
1354219089Spjd		 */
1355219089Spjd		dsl_scan_visitds(scn, scn->scn_phys.scn_bookmark.zb_objset, tx);
1356219089Spjd		if (scn->scn_pausing)
1357219089Spjd			return;
1358219089Spjd	}
1359219089Spjd
1360219089Spjd	/*
1361219089Spjd	 * In case we were paused right at the end of the ds, zero the
1362219089Spjd	 * bookmark so we don't think that we're still trying to resume.
1363219089Spjd	 */
1364219089Spjd	bzero(&scn->scn_phys.scn_bookmark, sizeof (zbookmark_t));
1365219089Spjd
1366219089Spjd	/* keep pulling things out of the zap-object-as-queue */
1367219089Spjd	while (zap_cursor_init(&zc, dp->dp_meta_objset,
1368219089Spjd	    scn->scn_phys.scn_queue_obj),
1369219089Spjd	    zap_cursor_retrieve(&zc, &za) == 0) {
1370219089Spjd		dsl_dataset_t *ds;
1371219089Spjd		uint64_t dsobj;
1372219089Spjd
1373219089Spjd		dsobj = strtonum(za.za_name, NULL);
1374219089Spjd		VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
1375219089Spjd		    scn->scn_phys.scn_queue_obj, dsobj, tx));
1376219089Spjd
1377219089Spjd		/* Set up min/max txg */
1378219089Spjd		VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
1379219089Spjd		if (za.za_first_integer != 0) {
1380219089Spjd			scn->scn_phys.scn_cur_min_txg =
1381219089Spjd			    MAX(scn->scn_phys.scn_min_txg,
1382219089Spjd			    za.za_first_integer);
1383219089Spjd		} else {
1384219089Spjd			scn->scn_phys.scn_cur_min_txg =
1385219089Spjd			    MAX(scn->scn_phys.scn_min_txg,
1386219089Spjd			    ds->ds_phys->ds_prev_snap_txg);
1387219089Spjd		}
1388219089Spjd		scn->scn_phys.scn_cur_max_txg = dsl_scan_ds_maxtxg(ds);
1389219089Spjd		dsl_dataset_rele(ds, FTAG);
1390219089Spjd
1391219089Spjd		dsl_scan_visitds(scn, dsobj, tx);
1392219089Spjd		zap_cursor_fini(&zc);
1393219089Spjd		if (scn->scn_pausing)
1394219089Spjd			return;
1395219089Spjd	}
1396219089Spjd	zap_cursor_fini(&zc);
1397219089Spjd}
1398219089Spjd
1399219089Spjdstatic int
1400219089Spjddsl_scan_free_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1401219089Spjd{
1402219089Spjd	dsl_scan_t *scn = arg;
1403219089Spjd	uint64_t elapsed_nanosecs;
1404219089Spjd
1405219089Spjd	elapsed_nanosecs = gethrtime() - scn->scn_sync_start_time;
1406219089Spjd
1407219089Spjd	if (elapsed_nanosecs / NANOSEC > zfs_txg_timeout ||
1408219089Spjd	    (elapsed_nanosecs / MICROSEC > zfs_free_min_time_ms &&
1409219089Spjd	    txg_sync_waiting(scn->scn_dp)) ||
1410219089Spjd	    spa_shutting_down(scn->scn_dp->dp_spa))
1411219089Spjd		return (ERESTART);
1412219089Spjd
1413219089Spjd	zio_nowait(zio_free_sync(scn->scn_zio_root, scn->scn_dp->dp_spa,
1414219089Spjd	    dmu_tx_get_txg(tx), bp, 0));
1415219089Spjd	dsl_dir_diduse_space(tx->tx_pool->dp_free_dir, DD_USED_HEAD,
1416219089Spjd	    -bp_get_dsize_sync(scn->scn_dp->dp_spa, bp),
1417219089Spjd	    -BP_GET_PSIZE(bp), -BP_GET_UCSIZE(bp), tx);
1418219089Spjd	scn->scn_visited_this_txg++;
1419219089Spjd	return (0);
1420219089Spjd}
1421219089Spjd
1422219089Spjdboolean_t
1423219089Spjddsl_scan_active(dsl_scan_t *scn)
1424219089Spjd{
1425219089Spjd	spa_t *spa = scn->scn_dp->dp_spa;
1426219089Spjd	uint64_t used = 0, comp, uncomp;
1427219089Spjd
1428219089Spjd	if (spa->spa_load_state != SPA_LOAD_NONE)
1429219089Spjd		return (B_FALSE);
1430219089Spjd	if (spa_shutting_down(spa))
1431219089Spjd		return (B_FALSE);
1432219089Spjd
1433219089Spjd	if (scn->scn_phys.scn_state == DSS_SCANNING)
1434219089Spjd		return (B_TRUE);
1435219089Spjd
1436219089Spjd	if (spa_version(scn->scn_dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
1437219089Spjd		(void) bpobj_space(&scn->scn_dp->dp_free_bpobj,
1438219089Spjd		    &used, &comp, &uncomp);
1439219089Spjd	}
1440219089Spjd	return (used != 0);
1441219089Spjd}
1442219089Spjd
1443219089Spjdvoid
1444219089Spjddsl_scan_sync(dsl_pool_t *dp, dmu_tx_t *tx)
1445219089Spjd{
1446219089Spjd	dsl_scan_t *scn = dp->dp_scan;
1447219089Spjd	spa_t *spa = dp->dp_spa;
1448219089Spjd	int err;
1449219089Spjd
1450219089Spjd	/*
1451219089Spjd	 * Check for scn_restart_txg before checking spa_load_state, so
1452219089Spjd	 * that we can restart an old-style scan while the pool is being
1453219089Spjd	 * imported (see dsl_scan_init).
1454219089Spjd	 */
1455219089Spjd	if (scn->scn_restart_txg != 0 &&
1456219089Spjd	    scn->scn_restart_txg <= tx->tx_txg) {
1457219089Spjd		pool_scan_func_t func = POOL_SCAN_SCRUB;
1458219089Spjd		dsl_scan_done(scn, B_FALSE, tx);
1459219089Spjd		if (vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL))
1460219089Spjd			func = POOL_SCAN_RESILVER;
1461219089Spjd		zfs_dbgmsg("restarting scan func=%u txg=%llu",
1462219089Spjd		    func, tx->tx_txg);
1463219089Spjd		dsl_scan_setup_sync(scn, &func, tx);
1464219089Spjd	}
1465219089Spjd
1466219089Spjd	if (!dsl_scan_active(scn) ||
1467219089Spjd	    spa_sync_pass(dp->dp_spa) > 1)
1468219089Spjd		return;
1469219089Spjd
1470219089Spjd	scn->scn_visited_this_txg = 0;
1471219089Spjd	scn->scn_pausing = B_FALSE;
1472219089Spjd	scn->scn_sync_start_time = gethrtime();
1473219089Spjd	spa->spa_scrub_active = B_TRUE;
1474219089Spjd
1475219089Spjd	/*
1476219089Spjd	 * First process the free list.  If we pause the free, don't do
1477219089Spjd	 * any scanning.  This ensures that there is no free list when
1478219089Spjd	 * we are scanning, so the scan code doesn't have to worry about
1479219089Spjd	 * traversing it.
1480219089Spjd	 */
1481219089Spjd	if (spa_version(dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
1482219089Spjd		scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
1483219089Spjd		    NULL, ZIO_FLAG_MUSTSUCCEED);
1484219089Spjd		err = bpobj_iterate(&dp->dp_free_bpobj,
1485219089Spjd		    dsl_scan_free_cb, scn, tx);
1486219089Spjd		VERIFY3U(0, ==, zio_wait(scn->scn_zio_root));
1487219089Spjd		if (scn->scn_visited_this_txg) {
1488219089Spjd			zfs_dbgmsg("freed %llu blocks in %llums from "
1489219089Spjd			    "free_bpobj txg %llu",
1490219089Spjd			    (longlong_t)scn->scn_visited_this_txg,
1491219089Spjd			    (longlong_t)
1492219089Spjd			    (gethrtime() - scn->scn_sync_start_time) / MICROSEC,
1493219089Spjd			    (longlong_t)tx->tx_txg);
1494219089Spjd			scn->scn_visited_this_txg = 0;
1495219089Spjd			/*
1496219089Spjd			 * Re-sync the ddt so that we can further modify
1497219089Spjd			 * it when doing bprewrite.
1498219089Spjd			 */
1499219089Spjd			ddt_sync(spa, tx->tx_txg);
1500219089Spjd		}
1501219089Spjd		if (err == ERESTART)
1502219089Spjd			return;
1503219089Spjd	}
1504219089Spjd
1505219089Spjd	if (scn->scn_phys.scn_state != DSS_SCANNING)
1506219089Spjd		return;
1507219089Spjd
1508219089Spjd	if (scn->scn_phys.scn_ddt_bookmark.ddb_class <=
1509219089Spjd	    scn->scn_phys.scn_ddt_class_max) {
1510219089Spjd		zfs_dbgmsg("doing scan sync txg %llu; "
1511219089Spjd		    "ddt bm=%llu/%llu/%llu/%llx",
1512219089Spjd		    (longlong_t)tx->tx_txg,
1513219089Spjd		    (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_class,
1514219089Spjd		    (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_type,
1515219089Spjd		    (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_checksum,
1516219089Spjd		    (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_cursor);
1517219089Spjd		ASSERT(scn->scn_phys.scn_bookmark.zb_objset == 0);
1518219089Spjd		ASSERT(scn->scn_phys.scn_bookmark.zb_object == 0);
1519219089Spjd		ASSERT(scn->scn_phys.scn_bookmark.zb_level == 0);
1520219089Spjd		ASSERT(scn->scn_phys.scn_bookmark.zb_blkid == 0);
1521219089Spjd	} else {
1522219089Spjd		zfs_dbgmsg("doing scan sync txg %llu; bm=%llu/%llu/%llu/%llu",
1523219089Spjd		    (longlong_t)tx->tx_txg,
1524219089Spjd		    (longlong_t)scn->scn_phys.scn_bookmark.zb_objset,
1525219089Spjd		    (longlong_t)scn->scn_phys.scn_bookmark.zb_object,
1526219089Spjd		    (longlong_t)scn->scn_phys.scn_bookmark.zb_level,
1527219089Spjd		    (longlong_t)scn->scn_phys.scn_bookmark.zb_blkid);
1528219089Spjd	}
1529219089Spjd
1530219089Spjd	scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
1531219089Spjd	    NULL, ZIO_FLAG_CANFAIL);
1532219089Spjd	dsl_scan_visit(scn, tx);
1533219089Spjd	(void) zio_wait(scn->scn_zio_root);
1534219089Spjd	scn->scn_zio_root = NULL;
1535219089Spjd
1536219089Spjd	zfs_dbgmsg("visited %llu blocks in %llums",
1537219089Spjd	    (longlong_t)scn->scn_visited_this_txg,
1538219089Spjd	    (longlong_t)(gethrtime() - scn->scn_sync_start_time) / MICROSEC);
1539219089Spjd
1540219089Spjd	if (!scn->scn_pausing) {
1541219089Spjd		/* finished with scan. */
1542219089Spjd		zfs_dbgmsg("finished scan txg %llu", (longlong_t)tx->tx_txg);
1543219089Spjd		dsl_scan_done(scn, B_TRUE, tx);
1544219089Spjd	}
1545219089Spjd
1546219089Spjd	if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
1547219089Spjd		mutex_enter(&spa->spa_scrub_lock);
1548219089Spjd		while (spa->spa_scrub_inflight > 0) {
1549219089Spjd			cv_wait(&spa->spa_scrub_io_cv,
1550219089Spjd			    &spa->spa_scrub_lock);
1551219089Spjd		}
1552219089Spjd		mutex_exit(&spa->spa_scrub_lock);
1553219089Spjd	}
1554219089Spjd
1555219089Spjd	dsl_scan_sync_state(scn, tx);
1556219089Spjd}
1557219089Spjd
1558219089Spjd/*
1559219089Spjd * This will start a new scan, or restart an existing one.
1560219089Spjd */
1561219089Spjdvoid
1562219089Spjddsl_resilver_restart(dsl_pool_t *dp, uint64_t txg)
1563219089Spjd{
1564219089Spjd	if (txg == 0) {
1565219089Spjd		dmu_tx_t *tx;
1566219089Spjd		tx = dmu_tx_create_dd(dp->dp_mos_dir);
1567219089Spjd		VERIFY(0 == dmu_tx_assign(tx, TXG_WAIT));
1568219089Spjd
1569219089Spjd		txg = dmu_tx_get_txg(tx);
1570219089Spjd		dp->dp_scan->scn_restart_txg = txg;
1571219089Spjd		dmu_tx_commit(tx);
1572219089Spjd	} else {
1573219089Spjd		dp->dp_scan->scn_restart_txg = txg;
1574219089Spjd	}
1575219089Spjd	zfs_dbgmsg("restarting resilver txg=%llu", txg);
1576219089Spjd}
1577219089Spjd
1578219089Spjdboolean_t
1579219089Spjddsl_scan_resilvering(dsl_pool_t *dp)
1580219089Spjd{
1581219089Spjd	return (dp->dp_scan->scn_phys.scn_state == DSS_SCANNING &&
1582219089Spjd	    dp->dp_scan->scn_phys.scn_func == POOL_SCAN_RESILVER);
1583219089Spjd}
1584219089Spjd
1585219089Spjd/*
1586219089Spjd * scrub consumers
1587219089Spjd */
1588219089Spjd
1589219089Spjdstatic void
1590219089Spjdcount_block(zfs_all_blkstats_t *zab, const blkptr_t *bp)
1591219089Spjd{
1592219089Spjd	int i;
1593219089Spjd
1594219089Spjd	/*
1595219089Spjd	 * If we resume after a reboot, zab will be NULL; don't record
1596219089Spjd	 * incomplete stats in that case.
1597219089Spjd	 */
1598219089Spjd	if (zab == NULL)
1599219089Spjd		return;
1600219089Spjd
1601219089Spjd	for (i = 0; i < 4; i++) {
1602219089Spjd		int l = (i < 2) ? BP_GET_LEVEL(bp) : DN_MAX_LEVELS;
1603219089Spjd		int t = (i & 1) ? BP_GET_TYPE(bp) : DMU_OT_TOTAL;
1604219089Spjd		zfs_blkstat_t *zb = &zab->zab_type[l][t];
1605219089Spjd		int equal;
1606219089Spjd
1607219089Spjd		zb->zb_count++;
1608219089Spjd		zb->zb_asize += BP_GET_ASIZE(bp);
1609219089Spjd		zb->zb_lsize += BP_GET_LSIZE(bp);
1610219089Spjd		zb->zb_psize += BP_GET_PSIZE(bp);
1611219089Spjd		zb->zb_gangs += BP_COUNT_GANG(bp);
1612219089Spjd
1613219089Spjd		switch (BP_GET_NDVAS(bp)) {
1614219089Spjd		case 2:
1615219089Spjd			if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
1616219089Spjd			    DVA_GET_VDEV(&bp->blk_dva[1]))
1617219089Spjd				zb->zb_ditto_2_of_2_samevdev++;
1618219089Spjd			break;
1619219089Spjd		case 3:
1620219089Spjd			equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
1621219089Spjd			    DVA_GET_VDEV(&bp->blk_dva[1])) +
1622219089Spjd			    (DVA_GET_VDEV(&bp->blk_dva[0]) ==
1623219089Spjd			    DVA_GET_VDEV(&bp->blk_dva[2])) +
1624219089Spjd			    (DVA_GET_VDEV(&bp->blk_dva[1]) ==
1625219089Spjd			    DVA_GET_VDEV(&bp->blk_dva[2]));
1626219089Spjd			if (equal == 1)
1627219089Spjd				zb->zb_ditto_2_of_3_samevdev++;
1628219089Spjd			else if (equal == 3)
1629219089Spjd				zb->zb_ditto_3_of_3_samevdev++;
1630219089Spjd			break;
1631219089Spjd		}
1632219089Spjd	}
1633219089Spjd}
1634219089Spjd
1635219089Spjdstatic void
1636219089Spjddsl_scan_scrub_done(zio_t *zio)
1637219089Spjd{
1638219089Spjd	spa_t *spa = zio->io_spa;
1639219089Spjd
1640219089Spjd	zio_data_buf_free(zio->io_data, zio->io_size);
1641219089Spjd
1642219089Spjd	mutex_enter(&spa->spa_scrub_lock);
1643219089Spjd	spa->spa_scrub_inflight--;
1644219089Spjd	cv_broadcast(&spa->spa_scrub_io_cv);
1645219089Spjd
1646219089Spjd	if (zio->io_error && (zio->io_error != ECKSUM ||
1647219089Spjd	    !(zio->io_flags & ZIO_FLAG_SPECULATIVE))) {
1648219089Spjd		spa->spa_dsl_pool->dp_scan->scn_phys.scn_errors++;
1649219089Spjd	}
1650219089Spjd	mutex_exit(&spa->spa_scrub_lock);
1651219089Spjd}
1652219089Spjd
1653219089Spjdstatic int
1654219089Spjddsl_scan_scrub_cb(dsl_pool_t *dp,
1655219089Spjd    const blkptr_t *bp, const zbookmark_t *zb)
1656219089Spjd{
1657219089Spjd	dsl_scan_t *scn = dp->dp_scan;
1658219089Spjd	size_t size = BP_GET_PSIZE(bp);
1659219089Spjd	spa_t *spa = dp->dp_spa;
1660219089Spjd	uint64_t phys_birth = BP_PHYSICAL_BIRTH(bp);
1661219089Spjd	boolean_t needs_io;
1662219089Spjd	int zio_flags = ZIO_FLAG_SCAN_THREAD | ZIO_FLAG_RAW | ZIO_FLAG_CANFAIL;
1663219089Spjd	int zio_priority;
1664219089Spjd	int scan_delay = 0;
1665219089Spjd
1666219089Spjd	if (phys_birth <= scn->scn_phys.scn_min_txg ||
1667219089Spjd	    phys_birth >= scn->scn_phys.scn_max_txg)
1668219089Spjd		return (0);
1669219089Spjd
1670219089Spjd	count_block(dp->dp_blkstats, bp);
1671219089Spjd
1672219089Spjd	ASSERT(DSL_SCAN_IS_SCRUB_RESILVER(scn));
1673219089Spjd	if (scn->scn_phys.scn_func == POOL_SCAN_SCRUB) {
1674219089Spjd		zio_flags |= ZIO_FLAG_SCRUB;
1675219089Spjd		zio_priority = ZIO_PRIORITY_SCRUB;
1676219089Spjd		needs_io = B_TRUE;
1677219089Spjd		scan_delay = zfs_scrub_delay;
1678219089Spjd	} else if (scn->scn_phys.scn_func == POOL_SCAN_RESILVER) {
1679219089Spjd		zio_flags |= ZIO_FLAG_RESILVER;
1680219089Spjd		zio_priority = ZIO_PRIORITY_RESILVER;
1681219089Spjd		needs_io = B_FALSE;
1682219089Spjd		scan_delay = zfs_resilver_delay;
1683219089Spjd	}
1684219089Spjd
1685219089Spjd	/* If it's an intent log block, failure is expected. */
1686219089Spjd	if (zb->zb_level == ZB_ZIL_LEVEL)
1687219089Spjd		zio_flags |= ZIO_FLAG_SPECULATIVE;
1688219089Spjd
1689219089Spjd	for (int d = 0; d < BP_GET_NDVAS(bp); d++) {
1690219089Spjd		vdev_t *vd = vdev_lookup_top(spa,
1691219089Spjd		    DVA_GET_VDEV(&bp->blk_dva[d]));
1692219089Spjd
1693219089Spjd		/*
1694219089Spjd		 * Keep track of how much data we've examined so that
1695219089Spjd		 * zpool(1M) status can make useful progress reports.
1696219089Spjd		 */
1697219089Spjd		scn->scn_phys.scn_examined += DVA_GET_ASIZE(&bp->blk_dva[d]);
1698219089Spjd		spa->spa_scan_pass_exam += DVA_GET_ASIZE(&bp->blk_dva[d]);
1699219089Spjd
1700219089Spjd		/* if it's a resilver, this may not be in the target range */
1701219089Spjd		if (!needs_io) {
1702219089Spjd			if (DVA_GET_GANG(&bp->blk_dva[d])) {
1703219089Spjd				/*
1704219089Spjd				 * Gang members may be spread across multiple
1705219089Spjd				 * vdevs, so the best estimate we have is the
1706219089Spjd				 * scrub range, which has already been checked.
1707219089Spjd				 * XXX -- it would be better to change our
1708219089Spjd				 * allocation policy to ensure that all
1709219089Spjd				 * gang members reside on the same vdev.
1710219089Spjd				 */
1711219089Spjd				needs_io = B_TRUE;
1712219089Spjd			} else {
1713219089Spjd				needs_io = vdev_dtl_contains(vd, DTL_PARTIAL,
1714219089Spjd				    phys_birth, 1);
1715219089Spjd			}
1716219089Spjd		}
1717219089Spjd	}
1718219089Spjd
1719219089Spjd	if (needs_io && !zfs_no_scrub_io) {
1720219089Spjd		vdev_t *rvd = spa->spa_root_vdev;
1721219089Spjd		uint64_t maxinflight = rvd->vdev_children * zfs_top_maxinflight;
1722219089Spjd		void *data = zio_data_buf_alloc(size);
1723219089Spjd
1724219089Spjd		mutex_enter(&spa->spa_scrub_lock);
1725219089Spjd		while (spa->spa_scrub_inflight >= maxinflight)
1726219089Spjd			cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
1727219089Spjd		spa->spa_scrub_inflight++;
1728219089Spjd		mutex_exit(&spa->spa_scrub_lock);
1729219089Spjd
1730219089Spjd		/*
1731219089Spjd		 * If we're seeing recent (zfs_scan_idle) "important" I/Os
1732219089Spjd		 * then throttle our workload to limit the impact of a scan.
1733219089Spjd		 */
1734219089Spjd		if (ddi_get_lbolt64() - spa->spa_last_io <= zfs_scan_idle)
1735219089Spjd			delay(scan_delay);
1736219089Spjd
1737219089Spjd		zio_nowait(zio_read(NULL, spa, bp, data, size,
1738219089Spjd		    dsl_scan_scrub_done, NULL, zio_priority,
1739219089Spjd		    zio_flags, zb));
1740219089Spjd	}
1741219089Spjd
1742219089Spjd	/* do not relocate this block */
1743219089Spjd	return (0);
1744219089Spjd}
1745219089Spjd
1746219089Spjdint
1747219089Spjddsl_scan(dsl_pool_t *dp, pool_scan_func_t func)
1748219089Spjd{
1749219089Spjd	spa_t *spa = dp->dp_spa;
1750219089Spjd
1751219089Spjd	/*
1752219089Spjd	 * Purge all vdev caches and probe all devices.  We do this here
1753219089Spjd	 * rather than in sync context because this requires a writer lock
1754219089Spjd	 * on the spa_config lock, which we can't do from sync context.  The
1755219089Spjd	 * spa_scrub_reopen flag indicates that vdev_open() should not
1756219089Spjd	 * attempt to start another scrub.
1757219089Spjd	 */
1758219089Spjd	spa_vdev_state_enter(spa, SCL_NONE);
1759219089Spjd	spa->spa_scrub_reopen = B_TRUE;
1760219089Spjd	vdev_reopen(spa->spa_root_vdev);
1761219089Spjd	spa->spa_scrub_reopen = B_FALSE;
1762219089Spjd	(void) spa_vdev_state_exit(spa, NULL, 0);
1763219089Spjd
1764219089Spjd	return (dsl_sync_task_do(dp, dsl_scan_setup_check,
1765219089Spjd	    dsl_scan_setup_sync, dp->dp_scan, &func, 0));
1766219089Spjd}
1767