xref: /illumos-gate/usr/src/uts/common/fs/zfs/dsl_scan.c (revision 3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 
25 #include <sys/dsl_scan.h>
26 #include <sys/dsl_pool.h>
27 #include <sys/dsl_dataset.h>
28 #include <sys/dsl_prop.h>
29 #include <sys/dsl_dir.h>
30 #include <sys/dsl_synctask.h>
31 #include <sys/dnode.h>
32 #include <sys/dmu_tx.h>
33 #include <sys/dmu_objset.h>
34 #include <sys/arc.h>
35 #include <sys/zap.h>
36 #include <sys/zio.h>
37 #include <sys/zfs_context.h>
38 #include <sys/fs/zfs.h>
39 #include <sys/zfs_znode.h>
40 #include <sys/spa_impl.h>
41 #include <sys/vdev_impl.h>
42 #include <sys/zil_impl.h>
43 #include <sys/zio_checksum.h>
44 #include <sys/ddt.h>
45 #include <sys/sa.h>
46 #include <sys/sa_impl.h>
47 #ifdef _KERNEL
48 #include <sys/zfs_vfsops.h>
49 #endif
50 
51 typedef int (scan_cb_t)(dsl_pool_t *, const blkptr_t *, const zbookmark_t *);
52 
53 static scan_cb_t dsl_scan_defrag_cb;
54 static scan_cb_t dsl_scan_scrub_cb;
55 static scan_cb_t dsl_scan_remove_cb;
56 static dsl_syncfunc_t dsl_scan_cancel_sync;
57 static void dsl_scan_sync_state(dsl_scan_t *, dmu_tx_t *tx);
58 
59 int zfs_scan_min_time_ms = 1000; /* min millisecs to scrub per txg */
60 int zfs_resilver_min_time_ms = 3000; /* min millisecs to resilver per txg */
61 boolean_t zfs_no_scrub_io = B_FALSE; /* set to disable scrub i/o */
62 boolean_t zfs_no_scrub_prefetch = B_FALSE; /* set to disable srub prefetching */
63 enum ddt_class zfs_scrub_ddt_class_max = DDT_CLASS_DUPLICATE;
64 int dsl_scan_delay_completion = B_FALSE; /* set to delay scan completion */
65 
66 #define	DSL_SCAN_IS_SCRUB_RESILVER(scn) \
67 	((scn)->scn_phys.scn_func == POOL_SCAN_SCRUB || \
68 	(scn)->scn_phys.scn_func == POOL_SCAN_RESILVER)
69 
70 extern int zfs_txg_timeout;
71 
72 /* the order has to match pool_scan_type */
73 static scan_cb_t *scan_funcs[POOL_SCAN_FUNCS] = {
74 	NULL,
75 	dsl_scan_scrub_cb,	/* POOL_SCAN_SCRUB */
76 	dsl_scan_scrub_cb,	/* POOL_SCAN_RESILVER */
77 };
78 
79 int
80 dsl_scan_init(dsl_pool_t *dp, uint64_t txg)
81 {
82 	int err;
83 	dsl_scan_t *scn;
84 	spa_t *spa = dp->dp_spa;
85 	uint64_t f;
86 
87 	scn = dp->dp_scan = kmem_zalloc(sizeof (dsl_scan_t), KM_SLEEP);
88 	scn->scn_dp = dp;
89 
90 	err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
91 	    "scrub_func", sizeof (uint64_t), 1, &f);
92 	if (err == 0) {
93 		/*
94 		 * There was an old-style scrub in progress.  Restart a
95 		 * new-style scrub from the beginning.
96 		 */
97 		scn->scn_restart_txg = txg;
98 		zfs_dbgmsg("old-style scrub was in progress; "
99 		    "restarting new-style scrub in txg %llu",
100 		    scn->scn_restart_txg);
101 
102 		/*
103 		 * Load the queue obj from the old location so that it
104 		 * can be freed by dsl_scan_done().
105 		 */
106 		(void) zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
107 		    "scrub_queue", sizeof (uint64_t), 1,
108 		    &scn->scn_phys.scn_queue_obj);
109 	} else {
110 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
111 		    DMU_POOL_SCAN, sizeof (uint64_t), SCAN_PHYS_NUMINTS,
112 		    &scn->scn_phys);
113 		if (err == ENOENT)
114 			return (0);
115 		else if (err)
116 			return (err);
117 
118 		if (scn->scn_phys.scn_state == DSS_SCANNING &&
119 		    spa_prev_software_version(dp->dp_spa) < SPA_VERSION_SCAN) {
120 			/*
121 			 * A new-type scrub was in progress on an old
122 			 * pool, and the pool was accessed by old
123 			 * software.  Restart from the beginning, since
124 			 * the old software may have changed the pool in
125 			 * the meantime.
126 			 */
127 			scn->scn_restart_txg = txg;
128 			zfs_dbgmsg("new-style scrub was modified "
129 			    "by old software; restarting in txg %llu",
130 			    scn->scn_restart_txg);
131 		}
132 	}
133 
134 	spa_scan_stat_init(spa);
135 	return (0);
136 }
137 
138 void
139 dsl_scan_fini(dsl_pool_t *dp)
140 {
141 	if (dp->dp_scan) {
142 		kmem_free(dp->dp_scan, sizeof (dsl_scan_t));
143 		dp->dp_scan = NULL;
144 	}
145 }
146 
147 /* ARGSUSED */
148 static int
149 dsl_scan_setup_check(void *arg1, void *arg2, dmu_tx_t *tx)
150 {
151 	dsl_scan_t *scn = arg1;
152 
153 	if (scn->scn_phys.scn_state == DSS_SCANNING)
154 		return (EBUSY);
155 
156 	return (0);
157 }
158 
159 /* ARGSUSED */
160 static void
161 dsl_scan_setup_sync(void *arg1, void *arg2, dmu_tx_t *tx)
162 {
163 	dsl_scan_t *scn = arg1;
164 	pool_scan_func_t *funcp = arg2;
165 	dmu_object_type_t ot = 0;
166 	dsl_pool_t *dp = scn->scn_dp;
167 	spa_t *spa = dp->dp_spa;
168 
169 	ASSERT(scn->scn_phys.scn_state != DSS_SCANNING);
170 	ASSERT(*funcp > POOL_SCAN_NONE && *funcp < POOL_SCAN_FUNCS);
171 	bzero(&scn->scn_phys, sizeof (scn->scn_phys));
172 	scn->scn_phys.scn_func = *funcp;
173 	scn->scn_phys.scn_state = DSS_SCANNING;
174 	scn->scn_phys.scn_min_txg = 0;
175 	scn->scn_phys.scn_max_txg = tx->tx_txg;
176 	scn->scn_phys.scn_ddt_class_max = DDT_CLASSES - 1; /* the entire DDT */
177 	scn->scn_phys.scn_start_time = gethrestime_sec();
178 	scn->scn_phys.scn_errors = 0;
179 	scn->scn_phys.scn_to_examine = spa->spa_root_vdev->vdev_stat.vs_alloc;
180 	scn->scn_restart_txg = 0;
181 	spa_scan_stat_init(spa);
182 
183 	if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
184 		scn->scn_phys.scn_ddt_class_max = zfs_scrub_ddt_class_max;
185 
186 		/* rewrite all disk labels */
187 		vdev_config_dirty(spa->spa_root_vdev);
188 
189 		if (vdev_resilver_needed(spa->spa_root_vdev,
190 		    &scn->scn_phys.scn_min_txg, &scn->scn_phys.scn_max_txg)) {
191 			spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_START);
192 		} else {
193 			spa_event_notify(spa, NULL, ESC_ZFS_SCRUB_START);
194 		}
195 
196 		spa->spa_scrub_started = B_TRUE;
197 		/*
198 		 * If this is an incremental scrub, limit the DDT scrub phase
199 		 * to just the auto-ditto class (for correctness); the rest
200 		 * of the scrub should go faster using top-down pruning.
201 		 */
202 		if (scn->scn_phys.scn_min_txg > TXG_INITIAL)
203 			scn->scn_phys.scn_ddt_class_max = DDT_CLASS_DITTO;
204 
205 	}
206 
207 	/* back to the generic stuff */
208 
209 	if (dp->dp_blkstats == NULL) {
210 		dp->dp_blkstats =
211 		    kmem_alloc(sizeof (zfs_all_blkstats_t), KM_SLEEP);
212 	}
213 	bzero(dp->dp_blkstats, sizeof (zfs_all_blkstats_t));
214 
215 	if (spa_version(spa) < SPA_VERSION_DSL_SCRUB)
216 		ot = DMU_OT_ZAP_OTHER;
217 
218 	scn->scn_phys.scn_queue_obj = zap_create(dp->dp_meta_objset,
219 	    ot ? ot : DMU_OT_SCAN_QUEUE, DMU_OT_NONE, 0, tx);
220 
221 	dsl_scan_sync_state(scn, tx);
222 
223 	spa_history_log_internal(LOG_POOL_SCAN, spa, tx,
224 	    "func=%u mintxg=%llu maxtxg=%llu",
225 	    *funcp, scn->scn_phys.scn_min_txg, scn->scn_phys.scn_max_txg);
226 }
227 
228 /* ARGSUSED */
229 static void
230 dsl_scan_done(dsl_scan_t *scn, boolean_t complete, dmu_tx_t *tx)
231 {
232 	static const char *old_names[] = {
233 		"scrub_bookmark",
234 		"scrub_ddt_bookmark",
235 		"scrub_ddt_class_max",
236 		"scrub_queue",
237 		"scrub_min_txg",
238 		"scrub_max_txg",
239 		"scrub_func",
240 		"scrub_errors",
241 		NULL
242 	};
243 
244 	dsl_pool_t *dp = scn->scn_dp;
245 	spa_t *spa = dp->dp_spa;
246 	int i;
247 
248 	/* Remove any remnants of an old-style scrub. */
249 	for (i = 0; old_names[i]; i++) {
250 		(void) zap_remove(dp->dp_meta_objset,
251 		    DMU_POOL_DIRECTORY_OBJECT, old_names[i], tx);
252 	}
253 
254 	if (scn->scn_phys.scn_queue_obj != 0) {
255 		VERIFY(0 == dmu_object_free(dp->dp_meta_objset,
256 		    scn->scn_phys.scn_queue_obj, tx));
257 		scn->scn_phys.scn_queue_obj = 0;
258 	}
259 
260 	/*
261 	 * If we were "restarted" from a stopped state, don't bother
262 	 * with anything else.
263 	 */
264 	if (scn->scn_phys.scn_state != DSS_SCANNING)
265 		return;
266 
267 	if (complete)
268 		scn->scn_phys.scn_state = DSS_FINISHED;
269 	else
270 		scn->scn_phys.scn_state = DSS_CANCELED;
271 
272 	spa_history_log_internal(LOG_POOL_SCAN_DONE, spa, tx,
273 	    "complete=%u", complete);
274 
275 	if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
276 		mutex_enter(&spa->spa_scrub_lock);
277 		while (spa->spa_scrub_inflight > 0) {
278 			cv_wait(&spa->spa_scrub_io_cv,
279 			    &spa->spa_scrub_lock);
280 		}
281 		mutex_exit(&spa->spa_scrub_lock);
282 		spa->spa_scrub_started = B_FALSE;
283 		spa->spa_scrub_active = B_FALSE;
284 
285 		/*
286 		 * If the scrub/resilver completed, update all DTLs to
287 		 * reflect this.  Whether it succeeded or not, vacate
288 		 * all temporary scrub DTLs.
289 		 */
290 		vdev_dtl_reassess(spa->spa_root_vdev, tx->tx_txg,
291 		    complete ? scn->scn_phys.scn_max_txg : 0, B_TRUE);
292 		if (complete) {
293 			spa_event_notify(spa, NULL, scn->scn_phys.scn_min_txg ?
294 			    ESC_ZFS_RESILVER_FINISH : ESC_ZFS_SCRUB_FINISH);
295 		}
296 		spa_errlog_rotate(spa);
297 
298 		/*
299 		 * We may have finished replacing a device.
300 		 * Let the async thread assess this and handle the detach.
301 		 */
302 		spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
303 	}
304 
305 	scn->scn_phys.scn_end_time = gethrestime_sec();
306 }
307 
308 /* ARGSUSED */
309 static int
310 dsl_scan_cancel_check(void *arg1, void *arg2, dmu_tx_t *tx)
311 {
312 	dsl_scan_t *scn = arg1;
313 
314 	if (scn->scn_phys.scn_state != DSS_SCANNING)
315 		return (ENOENT);
316 	return (0);
317 }
318 
319 /* ARGSUSED */
320 static void
321 dsl_scan_cancel_sync(void *arg1, void *arg2, dmu_tx_t *tx)
322 {
323 	dsl_scan_t *scn = arg1;
324 
325 	dsl_scan_done(scn, B_FALSE, tx);
326 	dsl_scan_sync_state(scn, tx);
327 }
328 
329 int
330 dsl_scan_cancel(dsl_pool_t *dp)
331 {
332 	boolean_t complete = B_FALSE;
333 	int err;
334 
335 	err = dsl_sync_task_do(dp, dsl_scan_cancel_check,
336 	    dsl_scan_cancel_sync, dp->dp_scan, &complete, 3);
337 	return (err);
338 }
339 
340 static void dsl_scan_visitbp(blkptr_t *bp,
341     const zbookmark_t *zb, dnode_phys_t *dnp, arc_buf_t *pbuf,
342     dsl_dataset_t *ds, dsl_scan_t *scn, dmu_objset_type_t ostype,
343     dmu_tx_t *tx);
344 static void dsl_scan_visitdnode(dsl_scan_t *, dsl_dataset_t *ds,
345     dmu_objset_type_t ostype,
346     dnode_phys_t *dnp, arc_buf_t *buf, uint64_t object, dmu_tx_t *tx);
347 
348 void
349 dsl_free(dsl_pool_t *dp, uint64_t txg, const blkptr_t *bp)
350 {
351 	zio_free(dp->dp_spa, txg, bp);
352 }
353 
354 void
355 dsl_free_sync(zio_t *pio, dsl_pool_t *dp, uint64_t txg, const blkptr_t *bpp)
356 {
357 	ASSERT(dsl_pool_sync_context(dp));
358 	zio_nowait(zio_free_sync(pio, dp->dp_spa, txg, bpp, pio->io_flags));
359 }
360 
361 int
362 dsl_read(zio_t *pio, spa_t *spa, const blkptr_t *bpp, arc_buf_t *pbuf,
363     arc_done_func_t *done, void *private, int priority, int zio_flags,
364     uint32_t *arc_flags, const zbookmark_t *zb)
365 {
366 	return (arc_read(pio, spa, bpp, pbuf, done, private,
367 	    priority, zio_flags, arc_flags, zb));
368 }
369 
370 int
371 dsl_read_nolock(zio_t *pio, spa_t *spa, const blkptr_t *bpp,
372     arc_done_func_t *done, void *private, int priority, int zio_flags,
373     uint32_t *arc_flags, const zbookmark_t *zb)
374 {
375 	return (arc_read_nolock(pio, spa, bpp, done, private,
376 	    priority, zio_flags, arc_flags, zb));
377 }
378 
379 static boolean_t
380 bookmark_is_zero(const zbookmark_t *zb)
381 {
382 	return (zb->zb_objset == 0 && zb->zb_object == 0 &&
383 	    zb->zb_level == 0 && zb->zb_blkid == 0);
384 }
385 
386 /* dnp is the dnode for zb1->zb_object */
387 static boolean_t
388 bookmark_is_before(const dnode_phys_t *dnp, const zbookmark_t *zb1,
389     const zbookmark_t *zb2)
390 {
391 	uint64_t zb1nextL0, zb2thisobj;
392 
393 	ASSERT(zb1->zb_objset == zb2->zb_objset);
394 	ASSERT(zb2->zb_level == 0);
395 
396 	/*
397 	 * A bookmark in the deadlist is considered to be after
398 	 * everything else.
399 	 */
400 	if (zb2->zb_object == DMU_DEADLIST_OBJECT)
401 		return (B_TRUE);
402 
403 	/* The objset_phys_t isn't before anything. */
404 	if (dnp == NULL)
405 		return (B_FALSE);
406 
407 	zb1nextL0 = (zb1->zb_blkid + 1) <<
408 	    ((zb1->zb_level) * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT));
409 
410 	zb2thisobj = zb2->zb_object ? zb2->zb_object :
411 	    zb2->zb_blkid << (DNODE_BLOCK_SHIFT - DNODE_SHIFT);
412 
413 	if (zb1->zb_object == DMU_META_DNODE_OBJECT) {
414 		uint64_t nextobj = zb1nextL0 *
415 		    (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT) >> DNODE_SHIFT;
416 		return (nextobj <= zb2thisobj);
417 	}
418 
419 	if (zb1->zb_object < zb2thisobj)
420 		return (B_TRUE);
421 	if (zb1->zb_object > zb2thisobj)
422 		return (B_FALSE);
423 	if (zb2->zb_object == DMU_META_DNODE_OBJECT)
424 		return (B_FALSE);
425 	return (zb1nextL0 <= zb2->zb_blkid);
426 }
427 
428 static uint64_t
429 dsl_scan_ds_maxtxg(dsl_dataset_t *ds)
430 {
431 	uint64_t smt = ds->ds_dir->dd_pool->dp_scan->scn_phys.scn_max_txg;
432 	if (dsl_dataset_is_snapshot(ds))
433 		return (MIN(smt, ds->ds_phys->ds_creation_txg));
434 	return (smt);
435 }
436 
437 static void
438 dsl_scan_sync_state(dsl_scan_t *scn, dmu_tx_t *tx)
439 {
440 	VERIFY(0 == zap_update(scn->scn_dp->dp_meta_objset,
441 	    DMU_POOL_DIRECTORY_OBJECT,
442 	    DMU_POOL_SCAN, sizeof (uint64_t), SCAN_PHYS_NUMINTS,
443 	    &scn->scn_phys, tx));
444 }
445 
446 static boolean_t
447 dsl_scan_check_pause(dsl_scan_t *scn, const zbookmark_t *zb)
448 {
449 	uint64_t elapsed_nanosecs;
450 	int mintime;
451 
452 	/* we never skip user/group accounting objects */
453 	if (zb && (int64_t)zb->zb_object < 0)
454 		return (B_FALSE);
455 
456 	if (scn->scn_pausing)
457 		return (B_TRUE); /* we're already pausing */
458 
459 	if (!bookmark_is_zero(&scn->scn_phys.scn_bookmark))
460 		return (B_FALSE); /* we're resuming */
461 
462 	/* We only know how to resume from level-0 blocks. */
463 	if (zb && zb->zb_level != 0)
464 		return (B_FALSE);
465 
466 	mintime = (scn->scn_phys.scn_func == POOL_SCAN_RESILVER) ?
467 	    zfs_resilver_min_time_ms : zfs_scan_min_time_ms;
468 	elapsed_nanosecs = gethrtime() - scn->scn_sync_start_time;
469 	if (elapsed_nanosecs / NANOSEC > zfs_txg_timeout ||
470 	    (elapsed_nanosecs / MICROSEC > mintime &&
471 	    txg_sync_waiting(scn->scn_dp)) ||
472 	    spa_shutting_down(scn->scn_dp->dp_spa)) {
473 		if (zb) {
474 			dprintf("pausing at bookmark %llx/%llx/%llx/%llx\n",
475 			    (longlong_t)zb->zb_objset,
476 			    (longlong_t)zb->zb_object,
477 			    (longlong_t)zb->zb_level,
478 			    (longlong_t)zb->zb_blkid);
479 			scn->scn_phys.scn_bookmark = *zb;
480 		}
481 		dprintf("pausing at DDT bookmark %llx/%llx/%llx/%llx\n",
482 		    (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_class,
483 		    (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_type,
484 		    (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_checksum,
485 		    (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_cursor);
486 		scn->scn_pausing = B_TRUE;
487 		return (B_TRUE);
488 	}
489 	return (B_FALSE);
490 }
491 
492 typedef struct zil_scan_arg {
493 	dsl_pool_t	*zsa_dp;
494 	zil_header_t	*zsa_zh;
495 } zil_scan_arg_t;
496 
497 /* ARGSUSED */
498 static int
499 dsl_scan_zil_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
500 {
501 	zil_scan_arg_t *zsa = arg;
502 	dsl_pool_t *dp = zsa->zsa_dp;
503 	dsl_scan_t *scn = dp->dp_scan;
504 	zil_header_t *zh = zsa->zsa_zh;
505 	zbookmark_t zb;
506 
507 	if (bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
508 		return (0);
509 
510 	/*
511 	 * One block ("stubby") can be allocated a long time ago; we
512 	 * want to visit that one because it has been allocated
513 	 * (on-disk) even if it hasn't been claimed (even though for
514 	 * scrub there's nothing to do to it).
515 	 */
516 	if (claim_txg == 0 && bp->blk_birth >= spa_first_txg(dp->dp_spa))
517 		return (0);
518 
519 	SET_BOOKMARK(&zb, zh->zh_log.blk_cksum.zc_word[ZIL_ZC_OBJSET],
520 	    ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, bp->blk_cksum.zc_word[ZIL_ZC_SEQ]);
521 
522 	VERIFY(0 == scan_funcs[scn->scn_phys.scn_func](dp, bp, &zb));
523 	return (0);
524 }
525 
526 /* ARGSUSED */
527 static int
528 dsl_scan_zil_record(zilog_t *zilog, lr_t *lrc, void *arg, uint64_t claim_txg)
529 {
530 	if (lrc->lrc_txtype == TX_WRITE) {
531 		zil_scan_arg_t *zsa = arg;
532 		dsl_pool_t *dp = zsa->zsa_dp;
533 		dsl_scan_t *scn = dp->dp_scan;
534 		zil_header_t *zh = zsa->zsa_zh;
535 		lr_write_t *lr = (lr_write_t *)lrc;
536 		blkptr_t *bp = &lr->lr_blkptr;
537 		zbookmark_t zb;
538 
539 		if (bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
540 			return (0);
541 
542 		/*
543 		 * birth can be < claim_txg if this record's txg is
544 		 * already txg sync'ed (but this log block contains
545 		 * other records that are not synced)
546 		 */
547 		if (claim_txg == 0 || bp->blk_birth < claim_txg)
548 			return (0);
549 
550 		SET_BOOKMARK(&zb, zh->zh_log.blk_cksum.zc_word[ZIL_ZC_OBJSET],
551 		    lr->lr_foid, ZB_ZIL_LEVEL,
552 		    lr->lr_offset / BP_GET_LSIZE(bp));
553 
554 		VERIFY(0 == scan_funcs[scn->scn_phys.scn_func](dp, bp, &zb));
555 	}
556 	return (0);
557 }
558 
559 static void
560 dsl_scan_zil(dsl_pool_t *dp, zil_header_t *zh)
561 {
562 	uint64_t claim_txg = zh->zh_claim_txg;
563 	zil_scan_arg_t zsa = { dp, zh };
564 	zilog_t *zilog;
565 
566 	/*
567 	 * We only want to visit blocks that have been claimed but not yet
568 	 * replayed (or, in read-only mode, blocks that *would* be claimed).
569 	 */
570 	if (claim_txg == 0 && spa_writeable(dp->dp_spa))
571 		return;
572 
573 	zilog = zil_alloc(dp->dp_meta_objset, zh);
574 
575 	(void) zil_parse(zilog, dsl_scan_zil_block, dsl_scan_zil_record, &zsa,
576 	    claim_txg);
577 
578 	zil_free(zilog);
579 }
580 
581 /* ARGSUSED */
582 static void
583 dsl_scan_prefetch(dsl_scan_t *scn, arc_buf_t *buf, blkptr_t *bp,
584     uint64_t objset, uint64_t object, uint64_t blkid)
585 {
586 	zbookmark_t czb;
587 	uint32_t flags = ARC_NOWAIT | ARC_PREFETCH;
588 
589 	if (zfs_no_scrub_prefetch)
590 		return;
591 
592 	if (BP_IS_HOLE(bp) || bp->blk_birth <= scn->scn_phys.scn_min_txg ||
593 	    (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_DNODE))
594 		return;
595 
596 	SET_BOOKMARK(&czb, objset, object, BP_GET_LEVEL(bp), blkid);
597 
598 	/*
599 	 * XXX need to make sure all of these arc_read() prefetches are
600 	 * done before setting xlateall (similar to dsl_read())
601 	 */
602 	(void) arc_read(scn->scn_prefetch_zio_root, scn->scn_dp->dp_spa, bp,
603 	    buf, NULL, NULL, ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
604 	    &flags, &czb);
605 }
606 
607 static boolean_t
608 dsl_scan_check_resume(dsl_scan_t *scn, const dnode_phys_t *dnp,
609     const zbookmark_t *zb)
610 {
611 	/*
612 	 * We never skip over user/group accounting objects (obj<0)
613 	 */
614 	if (!bookmark_is_zero(&scn->scn_phys.scn_bookmark) &&
615 	    (int64_t)zb->zb_object >= 0) {
616 		/*
617 		 * If we already visited this bp & everything below (in
618 		 * a prior txg sync), don't bother doing it again.
619 		 */
620 		if (bookmark_is_before(dnp, zb, &scn->scn_phys.scn_bookmark))
621 			return (B_TRUE);
622 
623 		/*
624 		 * If we found the block we're trying to resume from, or
625 		 * we went past it to a different object, zero it out to
626 		 * indicate that it's OK to start checking for pausing
627 		 * again.
628 		 */
629 		if (bcmp(zb, &scn->scn_phys.scn_bookmark, sizeof (*zb)) == 0 ||
630 		    zb->zb_object > scn->scn_phys.scn_bookmark.zb_object) {
631 			dprintf("resuming at %llx/%llx/%llx/%llx\n",
632 			    (longlong_t)zb->zb_objset,
633 			    (longlong_t)zb->zb_object,
634 			    (longlong_t)zb->zb_level,
635 			    (longlong_t)zb->zb_blkid);
636 			bzero(&scn->scn_phys.scn_bookmark, sizeof (*zb));
637 		}
638 	}
639 	return (B_FALSE);
640 }
641 
642 /*
643  * Return nonzero on i/o error.
644  * Return new buf to write out in *bufp.
645  */
646 static int
647 dsl_scan_recurse(dsl_scan_t *scn, dsl_dataset_t *ds, dmu_objset_type_t ostype,
648     dnode_phys_t *dnp, const blkptr_t *bp,
649     const zbookmark_t *zb, dmu_tx_t *tx, arc_buf_t **bufp)
650 {
651 	dsl_pool_t *dp = scn->scn_dp;
652 	int err;
653 
654 	if (BP_GET_LEVEL(bp) > 0) {
655 		uint32_t flags = ARC_WAIT;
656 		int i;
657 		blkptr_t *cbp;
658 		int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
659 
660 		err = arc_read_nolock(NULL, dp->dp_spa, bp,
661 		    arc_getbuf_func, bufp,
662 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
663 		if (err) {
664 			scn->scn_phys.scn_errors++;
665 			return (err);
666 		}
667 		for (i = 0, cbp = (*bufp)->b_data; i < epb; i++, cbp++) {
668 			dsl_scan_prefetch(scn, *bufp, cbp, zb->zb_objset,
669 			    zb->zb_object, zb->zb_blkid * epb + i);
670 		}
671 		for (i = 0, cbp = (*bufp)->b_data; i < epb; i++, cbp++) {
672 			zbookmark_t czb;
673 
674 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
675 			    zb->zb_level - 1,
676 			    zb->zb_blkid * epb + i);
677 			dsl_scan_visitbp(cbp, &czb, dnp,
678 			    *bufp, ds, scn, ostype, tx);
679 		}
680 	} else if (BP_GET_TYPE(bp) == DMU_OT_USERGROUP_USED) {
681 		uint32_t flags = ARC_WAIT;
682 
683 		err = arc_read_nolock(NULL, dp->dp_spa, bp,
684 		    arc_getbuf_func, bufp,
685 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
686 		if (err) {
687 			scn->scn_phys.scn_errors++;
688 			return (err);
689 		}
690 	} else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) {
691 		uint32_t flags = ARC_WAIT;
692 		dnode_phys_t *cdnp;
693 		int i, j;
694 		int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
695 
696 		err = arc_read_nolock(NULL, dp->dp_spa, bp,
697 		    arc_getbuf_func, bufp,
698 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
699 		if (err) {
700 			scn->scn_phys.scn_errors++;
701 			return (err);
702 		}
703 		for (i = 0, cdnp = (*bufp)->b_data; i < epb; i++, cdnp++) {
704 			for (j = 0; j < cdnp->dn_nblkptr; j++) {
705 				blkptr_t *cbp = &cdnp->dn_blkptr[j];
706 				dsl_scan_prefetch(scn, *bufp, cbp,
707 				    zb->zb_objset, zb->zb_blkid * epb + i, j);
708 			}
709 		}
710 		for (i = 0, cdnp = (*bufp)->b_data; i < epb; i++, cdnp++) {
711 			dsl_scan_visitdnode(scn, ds, ostype,
712 			    cdnp, *bufp, zb->zb_blkid * epb + i, tx);
713 		}
714 
715 	} else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) {
716 		uint32_t flags = ARC_WAIT;
717 		objset_phys_t *osp;
718 
719 		err = arc_read_nolock(NULL, dp->dp_spa, bp,
720 		    arc_getbuf_func, bufp,
721 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
722 		if (err) {
723 			scn->scn_phys.scn_errors++;
724 			return (err);
725 		}
726 
727 		osp = (*bufp)->b_data;
728 
729 		if (DSL_SCAN_IS_SCRUB_RESILVER(scn))
730 			dsl_scan_zil(dp, &osp->os_zil_header);
731 
732 		dsl_scan_visitdnode(scn, ds, osp->os_type,
733 		    &osp->os_meta_dnode, *bufp, DMU_META_DNODE_OBJECT, tx);
734 
735 		if (OBJSET_BUF_HAS_USERUSED(*bufp)) {
736 			/*
737 			 * We also always visit user/group accounting
738 			 * objects, and never skip them, even if we are
739 			 * pausing.  This is necessary so that the space
740 			 * deltas from this txg get integrated.
741 			 */
742 			dsl_scan_visitdnode(scn, ds, osp->os_type,
743 			    &osp->os_groupused_dnode, *bufp,
744 			    DMU_GROUPUSED_OBJECT, tx);
745 			dsl_scan_visitdnode(scn, ds, osp->os_type,
746 			    &osp->os_userused_dnode, *bufp,
747 			    DMU_USERUSED_OBJECT, tx);
748 		}
749 	}
750 
751 	return (0);
752 }
753 
754 static void
755 dsl_scan_visitdnode(dsl_scan_t *scn, dsl_dataset_t *ds,
756     dmu_objset_type_t ostype, dnode_phys_t *dnp, arc_buf_t *buf,
757     uint64_t object, dmu_tx_t *tx)
758 {
759 	int j;
760 
761 	for (j = 0; j < dnp->dn_nblkptr; j++) {
762 		zbookmark_t czb;
763 
764 		SET_BOOKMARK(&czb, ds ? ds->ds_object : 0, object,
765 		    dnp->dn_nlevels - 1, j);
766 		dsl_scan_visitbp(&dnp->dn_blkptr[j],
767 		    &czb, dnp, buf, ds, scn, ostype, tx);
768 	}
769 
770 	if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
771 		zbookmark_t czb;
772 		SET_BOOKMARK(&czb, ds ? ds->ds_object : 0, object,
773 		    0, DMU_SPILL_BLKID);
774 		dsl_scan_visitbp(&dnp->dn_spill,
775 		    &czb, dnp, buf, ds, scn, ostype, tx);
776 	}
777 }
778 
779 /*
780  * The arguments are in this order because mdb can only print the
781  * first 5; we want them to be useful.
782  */
783 static void
784 dsl_scan_visitbp(blkptr_t *bp, const zbookmark_t *zb,
785     dnode_phys_t *dnp, arc_buf_t *pbuf,
786     dsl_dataset_t *ds, dsl_scan_t *scn, dmu_objset_type_t ostype,
787     dmu_tx_t *tx)
788 {
789 	dsl_pool_t *dp = scn->scn_dp;
790 	arc_buf_t *buf = NULL;
791 	blkptr_t bp_toread = *bp;
792 
793 	/* ASSERT(pbuf == NULL || arc_released(pbuf)); */
794 
795 	if (dsl_scan_check_pause(scn, zb))
796 		return;
797 
798 	if (dsl_scan_check_resume(scn, dnp, zb))
799 		return;
800 
801 	if (bp->blk_birth == 0)
802 		return;
803 
804 	scn->scn_visited_this_txg++;
805 
806 	dprintf_bp(bp,
807 	    "visiting ds=%p/%llu zb=%llx/%llx/%llx/%llx buf=%p bp=%p",
808 	    ds, ds ? ds->ds_object : 0,
809 	    zb->zb_objset, zb->zb_object, zb->zb_level, zb->zb_blkid,
810 	    pbuf, bp);
811 
812 	if (bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
813 		return;
814 
815 	if (BP_GET_TYPE(bp) != DMU_OT_USERGROUP_USED) {
816 		/*
817 		 * For non-user-accounting blocks, we need to read the
818 		 * new bp (from a deleted snapshot, found in
819 		 * check_existing_xlation).  If we used the old bp,
820 		 * pointers inside this block from before we resumed
821 		 * would be untranslated.
822 		 *
823 		 * For user-accounting blocks, we need to read the old
824 		 * bp, because we will apply the entire space delta to
825 		 * it (original untranslated -> translations from
826 		 * deleted snap -> now).
827 		 */
828 		bp_toread = *bp;
829 	}
830 
831 	if (dsl_scan_recurse(scn, ds, ostype, dnp, &bp_toread, zb, tx,
832 	    &buf) != 0)
833 		return;
834 
835 	/*
836 	 * If dsl_scan_ddt() has aready visited this block, it will have
837 	 * already done any translations or scrubbing, so don't call the
838 	 * callback again.
839 	 */
840 	if (ddt_class_contains(dp->dp_spa,
841 	    scn->scn_phys.scn_ddt_class_max, bp)) {
842 		ASSERT(buf == NULL);
843 		return;
844 	}
845 
846 	/*
847 	 * If this block is from the future (after cur_max_txg), then we
848 	 * are doing this on behalf of a deleted snapshot, and we will
849 	 * revisit the future block on the next pass of this dataset.
850 	 * Don't scan it now unless we need to because something
851 	 * under it was modified.
852 	 */
853 	if (bp->blk_birth <= scn->scn_phys.scn_cur_max_txg) {
854 		scan_funcs[scn->scn_phys.scn_func](dp, bp, zb);
855 	}
856 	if (buf)
857 		(void) arc_buf_remove_ref(buf, &buf);
858 }
859 
860 static void
861 dsl_scan_visit_rootbp(dsl_scan_t *scn, dsl_dataset_t *ds, blkptr_t *bp,
862     dmu_tx_t *tx)
863 {
864 	zbookmark_t zb;
865 
866 	SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
867 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
868 	dsl_scan_visitbp(bp, &zb, NULL, NULL,
869 	    ds, scn, DMU_OST_NONE, tx);
870 
871 	dprintf_ds(ds, "finished scan%s", "");
872 }
873 
874 void
875 dsl_scan_ds_destroyed(dsl_dataset_t *ds, dmu_tx_t *tx)
876 {
877 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
878 	dsl_scan_t *scn = dp->dp_scan;
879 	uint64_t mintxg;
880 
881 	if (scn->scn_phys.scn_state != DSS_SCANNING)
882 		return;
883 
884 	if (scn->scn_phys.scn_bookmark.zb_objset == ds->ds_object) {
885 		if (dsl_dataset_is_snapshot(ds)) {
886 			/* Note, scn_cur_{min,max}_txg stays the same. */
887 			scn->scn_phys.scn_bookmark.zb_objset =
888 			    ds->ds_phys->ds_next_snap_obj;
889 			zfs_dbgmsg("destroying ds %llu; currently traversing; "
890 			    "reset zb_objset to %llu",
891 			    (u_longlong_t)ds->ds_object,
892 			    (u_longlong_t)ds->ds_phys->ds_next_snap_obj);
893 			scn->scn_phys.scn_flags |= DSF_VISIT_DS_AGAIN;
894 		} else {
895 			SET_BOOKMARK(&scn->scn_phys.scn_bookmark,
896 			    ZB_DESTROYED_OBJSET, 0, 0, 0);
897 			zfs_dbgmsg("destroying ds %llu; currently traversing; "
898 			    "reset bookmark to -1,0,0,0",
899 			    (u_longlong_t)ds->ds_object);
900 		}
901 	} else if (zap_lookup_int_key(dp->dp_meta_objset,
902 	    scn->scn_phys.scn_queue_obj, ds->ds_object, &mintxg) == 0) {
903 		ASSERT3U(ds->ds_phys->ds_num_children, <=, 1);
904 		VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
905 		    scn->scn_phys.scn_queue_obj, ds->ds_object, tx));
906 		if (dsl_dataset_is_snapshot(ds)) {
907 			/*
908 			 * We keep the same mintxg; it could be >
909 			 * ds_creation_txg if the previous snapshot was
910 			 * deleted too.
911 			 */
912 			VERIFY(zap_add_int_key(dp->dp_meta_objset,
913 			    scn->scn_phys.scn_queue_obj,
914 			    ds->ds_phys->ds_next_snap_obj, mintxg, tx) == 0);
915 			zfs_dbgmsg("destroying ds %llu; in queue; "
916 			    "replacing with %llu",
917 			    (u_longlong_t)ds->ds_object,
918 			    (u_longlong_t)ds->ds_phys->ds_next_snap_obj);
919 		} else {
920 			zfs_dbgmsg("destroying ds %llu; in queue; removing",
921 			    (u_longlong_t)ds->ds_object);
922 		}
923 	} else {
924 		zfs_dbgmsg("destroying ds %llu; ignoring",
925 		    (u_longlong_t)ds->ds_object);
926 	}
927 
928 	/*
929 	 * dsl_scan_sync() should be called after this, and should sync
930 	 * out our changed state, but just to be safe, do it here.
931 	 */
932 	dsl_scan_sync_state(scn, tx);
933 }
934 
935 void
936 dsl_scan_ds_snapshotted(dsl_dataset_t *ds, dmu_tx_t *tx)
937 {
938 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
939 	dsl_scan_t *scn = dp->dp_scan;
940 	uint64_t mintxg;
941 
942 	if (scn->scn_phys.scn_state != DSS_SCANNING)
943 		return;
944 
945 	ASSERT(ds->ds_phys->ds_prev_snap_obj != 0);
946 
947 	if (scn->scn_phys.scn_bookmark.zb_objset == ds->ds_object) {
948 		scn->scn_phys.scn_bookmark.zb_objset =
949 		    ds->ds_phys->ds_prev_snap_obj;
950 		zfs_dbgmsg("snapshotting ds %llu; currently traversing; "
951 		    "reset zb_objset to %llu",
952 		    (u_longlong_t)ds->ds_object,
953 		    (u_longlong_t)ds->ds_phys->ds_prev_snap_obj);
954 	} else if (zap_lookup_int_key(dp->dp_meta_objset,
955 	    scn->scn_phys.scn_queue_obj, ds->ds_object, &mintxg) == 0) {
956 		VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
957 		    scn->scn_phys.scn_queue_obj, ds->ds_object, tx));
958 		VERIFY(zap_add_int_key(dp->dp_meta_objset,
959 		    scn->scn_phys.scn_queue_obj,
960 		    ds->ds_phys->ds_prev_snap_obj, mintxg, tx) == 0);
961 		zfs_dbgmsg("snapshotting ds %llu; in queue; "
962 		    "replacing with %llu",
963 		    (u_longlong_t)ds->ds_object,
964 		    (u_longlong_t)ds->ds_phys->ds_prev_snap_obj);
965 	}
966 	dsl_scan_sync_state(scn, tx);
967 }
968 
969 void
970 dsl_scan_ds_clone_swapped(dsl_dataset_t *ds1, dsl_dataset_t *ds2, dmu_tx_t *tx)
971 {
972 	dsl_pool_t *dp = ds1->ds_dir->dd_pool;
973 	dsl_scan_t *scn = dp->dp_scan;
974 	uint64_t mintxg;
975 
976 	if (scn->scn_phys.scn_state != DSS_SCANNING)
977 		return;
978 
979 	if (scn->scn_phys.scn_bookmark.zb_objset == ds1->ds_object) {
980 		scn->scn_phys.scn_bookmark.zb_objset = ds2->ds_object;
981 		zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
982 		    "reset zb_objset to %llu",
983 		    (u_longlong_t)ds1->ds_object,
984 		    (u_longlong_t)ds2->ds_object);
985 	} else if (scn->scn_phys.scn_bookmark.zb_objset == ds2->ds_object) {
986 		scn->scn_phys.scn_bookmark.zb_objset = ds1->ds_object;
987 		zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
988 		    "reset zb_objset to %llu",
989 		    (u_longlong_t)ds2->ds_object,
990 		    (u_longlong_t)ds1->ds_object);
991 	}
992 
993 	if (zap_lookup_int_key(dp->dp_meta_objset, scn->scn_phys.scn_queue_obj,
994 	    ds1->ds_object, &mintxg) == 0) {
995 		int err;
996 
997 		ASSERT3U(mintxg, ==, ds1->ds_phys->ds_prev_snap_txg);
998 		ASSERT3U(mintxg, ==, ds2->ds_phys->ds_prev_snap_txg);
999 		VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
1000 		    scn->scn_phys.scn_queue_obj, ds1->ds_object, tx));
1001 		err = zap_add_int_key(dp->dp_meta_objset,
1002 		    scn->scn_phys.scn_queue_obj, ds2->ds_object, mintxg, tx);
1003 		VERIFY(err == 0 || err == EEXIST);
1004 		if (err == EEXIST) {
1005 			/* Both were there to begin with */
1006 			VERIFY(0 == zap_add_int_key(dp->dp_meta_objset,
1007 			    scn->scn_phys.scn_queue_obj,
1008 			    ds1->ds_object, mintxg, tx));
1009 		}
1010 		zfs_dbgmsg("clone_swap ds %llu; in queue; "
1011 		    "replacing with %llu",
1012 		    (u_longlong_t)ds1->ds_object,
1013 		    (u_longlong_t)ds2->ds_object);
1014 	} else if (zap_lookup_int_key(dp->dp_meta_objset,
1015 	    scn->scn_phys.scn_queue_obj, ds2->ds_object, &mintxg) == 0) {
1016 		ASSERT3U(mintxg, ==, ds1->ds_phys->ds_prev_snap_txg);
1017 		ASSERT3U(mintxg, ==, ds2->ds_phys->ds_prev_snap_txg);
1018 		VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
1019 		    scn->scn_phys.scn_queue_obj, ds2->ds_object, tx));
1020 		VERIFY(0 == zap_add_int_key(dp->dp_meta_objset,
1021 		    scn->scn_phys.scn_queue_obj, ds1->ds_object, mintxg, tx));
1022 		zfs_dbgmsg("clone_swap ds %llu; in queue; "
1023 		    "replacing with %llu",
1024 		    (u_longlong_t)ds2->ds_object,
1025 		    (u_longlong_t)ds1->ds_object);
1026 	}
1027 
1028 	dsl_scan_sync_state(scn, tx);
1029 }
1030 
1031 struct enqueue_clones_arg {
1032 	dmu_tx_t *tx;
1033 	uint64_t originobj;
1034 };
1035 
1036 /* ARGSUSED */
1037 static int
1038 enqueue_clones_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
1039 {
1040 	struct enqueue_clones_arg *eca = arg;
1041 	dsl_dataset_t *ds;
1042 	int err;
1043 	dsl_pool_t *dp = spa->spa_dsl_pool;
1044 	dsl_scan_t *scn = dp->dp_scan;
1045 
1046 	err = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds);
1047 	if (err)
1048 		return (err);
1049 
1050 	if (ds->ds_dir->dd_phys->dd_origin_obj == eca->originobj) {
1051 		while (ds->ds_phys->ds_prev_snap_obj != eca->originobj) {
1052 			dsl_dataset_t *prev;
1053 			err = dsl_dataset_hold_obj(dp,
1054 			    ds->ds_phys->ds_prev_snap_obj, FTAG, &prev);
1055 
1056 			dsl_dataset_rele(ds, FTAG);
1057 			if (err)
1058 				return (err);
1059 			ds = prev;
1060 		}
1061 		VERIFY(zap_add_int_key(dp->dp_meta_objset,
1062 		    scn->scn_phys.scn_queue_obj, ds->ds_object,
1063 		    ds->ds_phys->ds_prev_snap_txg, eca->tx) == 0);
1064 	}
1065 	dsl_dataset_rele(ds, FTAG);
1066 	return (0);
1067 }
1068 
1069 static void
1070 dsl_scan_visitds(dsl_scan_t *scn, uint64_t dsobj, dmu_tx_t *tx)
1071 {
1072 	dsl_pool_t *dp = scn->scn_dp;
1073 	dsl_dataset_t *ds;
1074 
1075 	VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
1076 
1077 	/*
1078 	 * Iterate over the bps in this ds.
1079 	 */
1080 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1081 	dsl_scan_visit_rootbp(scn, ds, &ds->ds_phys->ds_bp, tx);
1082 
1083 	char *dsname = kmem_alloc(ZFS_MAXNAMELEN, KM_SLEEP);
1084 	dsl_dataset_name(ds, dsname);
1085 	zfs_dbgmsg("scanned dataset %llu (%s) with min=%llu max=%llu; "
1086 	    "pausing=%u",
1087 	    (longlong_t)dsobj, dsname,
1088 	    (longlong_t)scn->scn_phys.scn_cur_min_txg,
1089 	    (longlong_t)scn->scn_phys.scn_cur_max_txg,
1090 	    (int)scn->scn_pausing);
1091 	kmem_free(dsname, ZFS_MAXNAMELEN);
1092 
1093 	if (scn->scn_pausing)
1094 		goto out;
1095 
1096 	/*
1097 	 * We've finished this pass over this dataset.
1098 	 */
1099 
1100 	/*
1101 	 * If we did not completely visit this dataset, do another pass.
1102 	 */
1103 	if (scn->scn_phys.scn_flags & DSF_VISIT_DS_AGAIN) {
1104 		zfs_dbgmsg("incomplete pass; visiting again");
1105 		scn->scn_phys.scn_flags &= ~DSF_VISIT_DS_AGAIN;
1106 		VERIFY(zap_add_int_key(dp->dp_meta_objset,
1107 		    scn->scn_phys.scn_queue_obj, ds->ds_object,
1108 		    scn->scn_phys.scn_cur_max_txg, tx) == 0);
1109 		goto out;
1110 	}
1111 
1112 	/*
1113 	 * Add descendent datasets to work queue.
1114 	 */
1115 	if (ds->ds_phys->ds_next_snap_obj != 0) {
1116 		VERIFY(zap_add_int_key(dp->dp_meta_objset,
1117 		    scn->scn_phys.scn_queue_obj, ds->ds_phys->ds_next_snap_obj,
1118 		    ds->ds_phys->ds_creation_txg, tx) == 0);
1119 	}
1120 	if (ds->ds_phys->ds_num_children > 1) {
1121 		boolean_t usenext = B_FALSE;
1122 		if (ds->ds_phys->ds_next_clones_obj != 0) {
1123 			uint64_t count;
1124 			/*
1125 			 * A bug in a previous version of the code could
1126 			 * cause upgrade_clones_cb() to not set
1127 			 * ds_next_snap_obj when it should, leading to a
1128 			 * missing entry.  Therefore we can only use the
1129 			 * next_clones_obj when its count is correct.
1130 			 */
1131 			int err = zap_count(dp->dp_meta_objset,
1132 			    ds->ds_phys->ds_next_clones_obj, &count);
1133 			if (err == 0 &&
1134 			    count == ds->ds_phys->ds_num_children - 1)
1135 				usenext = B_TRUE;
1136 		}
1137 
1138 		if (usenext) {
1139 			VERIFY(zap_join_key(dp->dp_meta_objset,
1140 			    ds->ds_phys->ds_next_clones_obj,
1141 			    scn->scn_phys.scn_queue_obj,
1142 			    ds->ds_phys->ds_creation_txg, tx) == 0);
1143 		} else {
1144 			struct enqueue_clones_arg eca;
1145 			eca.tx = tx;
1146 			eca.originobj = ds->ds_object;
1147 
1148 			(void) dmu_objset_find_spa(ds->ds_dir->dd_pool->dp_spa,
1149 			    NULL, enqueue_clones_cb, &eca, DS_FIND_CHILDREN);
1150 		}
1151 	}
1152 
1153 out:
1154 	dsl_dataset_rele(ds, FTAG);
1155 }
1156 
1157 /* ARGSUSED */
1158 static int
1159 enqueue_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
1160 {
1161 	dmu_tx_t *tx = arg;
1162 	dsl_dataset_t *ds;
1163 	int err;
1164 	dsl_pool_t *dp = spa->spa_dsl_pool;
1165 	dsl_scan_t *scn = dp->dp_scan;
1166 
1167 	err = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds);
1168 	if (err)
1169 		return (err);
1170 
1171 	while (ds->ds_phys->ds_prev_snap_obj != 0) {
1172 		dsl_dataset_t *prev;
1173 		err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
1174 		    FTAG, &prev);
1175 		if (err) {
1176 			dsl_dataset_rele(ds, FTAG);
1177 			return (err);
1178 		}
1179 
1180 		/*
1181 		 * If this is a clone, we don't need to worry about it for now.
1182 		 */
1183 		if (prev->ds_phys->ds_next_snap_obj != ds->ds_object) {
1184 			dsl_dataset_rele(ds, FTAG);
1185 			dsl_dataset_rele(prev, FTAG);
1186 			return (0);
1187 		}
1188 		dsl_dataset_rele(ds, FTAG);
1189 		ds = prev;
1190 	}
1191 
1192 	VERIFY(zap_add_int_key(dp->dp_meta_objset, scn->scn_phys.scn_queue_obj,
1193 	    ds->ds_object, ds->ds_phys->ds_prev_snap_txg, tx) == 0);
1194 	dsl_dataset_rele(ds, FTAG);
1195 	return (0);
1196 }
1197 
1198 /*
1199  * Scrub/dedup interaction.
1200  *
1201  * If there are N references to a deduped block, we don't want to scrub it
1202  * N times -- ideally, we should scrub it exactly once.
1203  *
1204  * We leverage the fact that the dde's replication class (enum ddt_class)
1205  * is ordered from highest replication class (DDT_CLASS_DITTO) to lowest
1206  * (DDT_CLASS_UNIQUE) so that we may walk the DDT in that order.
1207  *
1208  * To prevent excess scrubbing, the scrub begins by walking the DDT
1209  * to find all blocks with refcnt > 1, and scrubs each of these once.
1210  * Since there are two replication classes which contain blocks with
1211  * refcnt > 1, we scrub the highest replication class (DDT_CLASS_DITTO) first.
1212  * Finally the top-down scrub begins, only visiting blocks with refcnt == 1.
1213  *
1214  * There would be nothing more to say if a block's refcnt couldn't change
1215  * during a scrub, but of course it can so we must account for changes
1216  * in a block's replication class.
1217  *
1218  * Here's an example of what can occur:
1219  *
1220  * If a block has refcnt > 1 during the DDT scrub phase, but has refcnt == 1
1221  * when visited during the top-down scrub phase, it will be scrubbed twice.
1222  * This negates our scrub optimization, but is otherwise harmless.
1223  *
1224  * If a block has refcnt == 1 during the DDT scrub phase, but has refcnt > 1
1225  * on each visit during the top-down scrub phase, it will never be scrubbed.
1226  * To catch this, ddt_sync_entry() notifies the scrub code whenever a block's
1227  * reference class transitions to a higher level (i.e DDT_CLASS_UNIQUE to
1228  * DDT_CLASS_DUPLICATE); if it transitions from refcnt == 1 to refcnt > 1
1229  * while a scrub is in progress, it scrubs the block right then.
1230  */
1231 static void
1232 dsl_scan_ddt(dsl_scan_t *scn, dmu_tx_t *tx)
1233 {
1234 	ddt_bookmark_t *ddb = &scn->scn_phys.scn_ddt_bookmark;
1235 	ddt_entry_t dde = { 0 };
1236 	int error;
1237 	uint64_t n = 0;
1238 
1239 	while ((error = ddt_walk(scn->scn_dp->dp_spa, ddb, &dde)) == 0) {
1240 		ddt_t *ddt;
1241 
1242 		if (ddb->ddb_class > scn->scn_phys.scn_ddt_class_max)
1243 			break;
1244 		dprintf("visiting ddb=%llu/%llu/%llu/%llx\n",
1245 		    (longlong_t)ddb->ddb_class,
1246 		    (longlong_t)ddb->ddb_type,
1247 		    (longlong_t)ddb->ddb_checksum,
1248 		    (longlong_t)ddb->ddb_cursor);
1249 
1250 		/* There should be no pending changes to the dedup table */
1251 		ddt = scn->scn_dp->dp_spa->spa_ddt[ddb->ddb_checksum];
1252 		ASSERT(avl_first(&ddt->ddt_tree) == NULL);
1253 
1254 		dsl_scan_ddt_entry(scn, ddb->ddb_checksum, &dde, tx);
1255 		n++;
1256 
1257 		if (dsl_scan_check_pause(scn, NULL))
1258 			break;
1259 	}
1260 
1261 	zfs_dbgmsg("scanned %llu ddt entries with class_max = %u; pausing=%u",
1262 	    (longlong_t)n, (int)scn->scn_phys.scn_ddt_class_max,
1263 	    (int)scn->scn_pausing);
1264 
1265 	ASSERT(error == 0 || error == ENOENT);
1266 	ASSERT(error != ENOENT ||
1267 	    ddb->ddb_class > scn->scn_phys.scn_ddt_class_max);
1268 }
1269 
1270 /* ARGSUSED */
1271 void
1272 dsl_scan_ddt_entry(dsl_scan_t *scn, enum zio_checksum checksum,
1273     ddt_entry_t *dde, dmu_tx_t *tx)
1274 {
1275 	const ddt_key_t *ddk = &dde->dde_key;
1276 	ddt_phys_t *ddp = dde->dde_phys;
1277 	blkptr_t bp;
1278 	zbookmark_t zb = { 0 };
1279 
1280 	if (scn->scn_phys.scn_state != DSS_SCANNING)
1281 		return;
1282 
1283 	for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
1284 		if (ddp->ddp_phys_birth == 0 ||
1285 		    ddp->ddp_phys_birth > scn->scn_phys.scn_cur_max_txg)
1286 			continue;
1287 		ddt_bp_create(checksum, ddk, ddp, &bp);
1288 
1289 		scn->scn_visited_this_txg++;
1290 		scan_funcs[scn->scn_phys.scn_func](scn->scn_dp, &bp, &zb);
1291 	}
1292 }
1293 
1294 static void
1295 dsl_scan_visit(dsl_scan_t *scn, dmu_tx_t *tx)
1296 {
1297 	dsl_pool_t *dp = scn->scn_dp;
1298 	zap_cursor_t zc;
1299 	zap_attribute_t za;
1300 
1301 	if (scn->scn_phys.scn_ddt_bookmark.ddb_class <=
1302 	    scn->scn_phys.scn_ddt_class_max) {
1303 		scn->scn_phys.scn_cur_min_txg = scn->scn_phys.scn_min_txg;
1304 		scn->scn_phys.scn_cur_max_txg = scn->scn_phys.scn_max_txg;
1305 		dsl_scan_ddt(scn, tx);
1306 		if (scn->scn_pausing)
1307 			return;
1308 	}
1309 
1310 	if (scn->scn_phys.scn_bookmark.zb_objset == DMU_META_OBJSET) {
1311 		/* First do the MOS & ORIGIN */
1312 
1313 		scn->scn_phys.scn_cur_min_txg = scn->scn_phys.scn_min_txg;
1314 		scn->scn_phys.scn_cur_max_txg = scn->scn_phys.scn_max_txg;
1315 		dsl_scan_visit_rootbp(scn, NULL,
1316 		    &dp->dp_meta_rootbp, tx);
1317 		spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
1318 		if (scn->scn_pausing)
1319 			return;
1320 
1321 		if (spa_version(dp->dp_spa) < SPA_VERSION_DSL_SCRUB) {
1322 			VERIFY(0 == dmu_objset_find_spa(dp->dp_spa,
1323 			    NULL, enqueue_cb, tx, DS_FIND_CHILDREN));
1324 		} else {
1325 			dsl_scan_visitds(scn,
1326 			    dp->dp_origin_snap->ds_object, tx);
1327 		}
1328 		ASSERT(!scn->scn_pausing);
1329 	} else if (scn->scn_phys.scn_bookmark.zb_objset !=
1330 	    ZB_DESTROYED_OBJSET) {
1331 		/*
1332 		 * If we were paused, continue from here.  Note if the
1333 		 * ds we were paused on was deleted, the zb_objset may
1334 		 * be -1, so we will skip this and find a new objset
1335 		 * below.
1336 		 */
1337 		dsl_scan_visitds(scn, scn->scn_phys.scn_bookmark.zb_objset, tx);
1338 		if (scn->scn_pausing)
1339 			return;
1340 	}
1341 
1342 	/*
1343 	 * In case we were paused right at the end of the ds, zero the
1344 	 * bookmark so we don't think that we're still trying to resume.
1345 	 */
1346 	bzero(&scn->scn_phys.scn_bookmark, sizeof (zbookmark_t));
1347 
1348 	/* keep pulling things out of the zap-object-as-queue */
1349 	while (zap_cursor_init(&zc, dp->dp_meta_objset,
1350 	    scn->scn_phys.scn_queue_obj),
1351 	    zap_cursor_retrieve(&zc, &za) == 0) {
1352 		dsl_dataset_t *ds;
1353 		uint64_t dsobj;
1354 
1355 		dsobj = strtonum(za.za_name, NULL);
1356 		VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
1357 		    scn->scn_phys.scn_queue_obj, dsobj, tx));
1358 
1359 		/* Set up min/max txg */
1360 		VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
1361 		if (za.za_first_integer != 0) {
1362 			scn->scn_phys.scn_cur_min_txg =
1363 			    MAX(scn->scn_phys.scn_min_txg,
1364 			    za.za_first_integer);
1365 		} else {
1366 			scn->scn_phys.scn_cur_min_txg =
1367 			    MAX(scn->scn_phys.scn_min_txg,
1368 			    ds->ds_phys->ds_prev_snap_txg);
1369 		}
1370 		scn->scn_phys.scn_cur_max_txg = dsl_scan_ds_maxtxg(ds);
1371 		dsl_dataset_rele(ds, FTAG);
1372 
1373 		dsl_scan_visitds(scn, dsobj, tx);
1374 		zap_cursor_fini(&zc);
1375 		if (scn->scn_pausing)
1376 			return;
1377 	}
1378 	zap_cursor_fini(&zc);
1379 }
1380 
1381 void
1382 dsl_scan_sync(dsl_pool_t *dp, dmu_tx_t *tx)
1383 {
1384 	dsl_scan_t *scn = dp->dp_scan;
1385 	spa_t *spa = dp->dp_spa;
1386 
1387 	/*
1388 	 * Check for scn_restart_txg before checking spa_load_state, so
1389 	 * that we can restart an old-style scan while the pool is being
1390 	 * imported (see dsl_scan_init).
1391 	 */
1392 	if (scn->scn_restart_txg != 0 &&
1393 	    scn->scn_restart_txg <= tx->tx_txg) {
1394 		pool_scan_func_t func = POOL_SCAN_SCRUB;
1395 		dsl_scan_done(scn, B_FALSE, tx);
1396 		if (vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL))
1397 			func = POOL_SCAN_RESILVER;
1398 		zfs_dbgmsg("restarting scan func=%u txg=%llu",
1399 		    func, tx->tx_txg);
1400 		dsl_scan_setup_sync(scn, &func, tx);
1401 	}
1402 
1403 	if (dp->dp_spa->spa_load_state != SPA_LOAD_NONE ||
1404 	    spa_shutting_down(spa) ||
1405 	    spa_sync_pass(dp->dp_spa) > 1 ||
1406 	    scn->scn_phys.scn_state != DSS_SCANNING)
1407 		return;
1408 
1409 	scn->scn_visited_this_txg = 0;
1410 	scn->scn_pausing = B_FALSE;
1411 	scn->scn_sync_start_time = gethrtime();
1412 	spa->spa_scrub_active = B_TRUE;
1413 
1414 	if (scn->scn_phys.scn_ddt_bookmark.ddb_class <=
1415 	    scn->scn_phys.scn_ddt_class_max) {
1416 		zfs_dbgmsg("doing scan sync txg %llu; "
1417 		    "ddt bm=%llu/%llu/%llu/%llx",
1418 		    (longlong_t)tx->tx_txg,
1419 		    (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_class,
1420 		    (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_type,
1421 		    (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_checksum,
1422 		    (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_cursor);
1423 		ASSERT(scn->scn_phys.scn_bookmark.zb_objset == 0);
1424 		ASSERT(scn->scn_phys.scn_bookmark.zb_object == 0);
1425 		ASSERT(scn->scn_phys.scn_bookmark.zb_level == 0);
1426 		ASSERT(scn->scn_phys.scn_bookmark.zb_blkid == 0);
1427 	} else {
1428 		zfs_dbgmsg("doing scan sync txg %llu; bm=%llu/%llu/%llu/%llu",
1429 		    (longlong_t)tx->tx_txg,
1430 		    (longlong_t)scn->scn_phys.scn_bookmark.zb_objset,
1431 		    (longlong_t)scn->scn_phys.scn_bookmark.zb_object,
1432 		    (longlong_t)scn->scn_phys.scn_bookmark.zb_level,
1433 		    (longlong_t)scn->scn_phys.scn_bookmark.zb_blkid);
1434 	}
1435 
1436 	scn->scn_prefetch_zio_root = zio_root(dp->dp_spa, NULL,
1437 	    NULL, ZIO_FLAG_CANFAIL);
1438 	dsl_scan_visit(scn, tx);
1439 	(void) zio_wait(scn->scn_prefetch_zio_root);
1440 	scn->scn_prefetch_zio_root = NULL;
1441 
1442 	zfs_dbgmsg("visited %llu blocks in %llums",
1443 	    (longlong_t)scn->scn_visited_this_txg,
1444 	    (longlong_t)(gethrtime() - scn->scn_sync_start_time) / MICROSEC);
1445 
1446 	if (!scn->scn_pausing) {
1447 		/* finished with scan. */
1448 		zfs_dbgmsg("finished scan txg %llu", (longlong_t)tx->tx_txg);
1449 		dsl_scan_done(scn, B_TRUE, tx);
1450 	}
1451 
1452 	if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
1453 		mutex_enter(&spa->spa_scrub_lock);
1454 		while (spa->spa_scrub_inflight > 0) {
1455 			cv_wait(&spa->spa_scrub_io_cv,
1456 			    &spa->spa_scrub_lock);
1457 		}
1458 		mutex_exit(&spa->spa_scrub_lock);
1459 	}
1460 
1461 	dsl_scan_sync_state(scn, tx);
1462 }
1463 
1464 /*
1465  * This will start a new scan, or restart an existing one.
1466  */
1467 void
1468 dsl_resilver_restart(dsl_pool_t *dp, uint64_t txg)
1469 {
1470 	if (txg == 0) {
1471 		dmu_tx_t *tx;
1472 		tx = dmu_tx_create_dd(dp->dp_mos_dir);
1473 		VERIFY(0 == dmu_tx_assign(tx, TXG_WAIT));
1474 
1475 		txg = dmu_tx_get_txg(tx);
1476 		dp->dp_scan->scn_restart_txg = txg;
1477 		dmu_tx_commit(tx);
1478 	} else {
1479 		dp->dp_scan->scn_restart_txg = txg;
1480 	}
1481 	zfs_dbgmsg("restarting resilver txg=%llu", txg);
1482 }
1483 
1484 boolean_t
1485 dsl_scan_resilvering(dsl_pool_t *dp)
1486 {
1487 	return (dp->dp_scan->scn_phys.scn_state == DSS_SCANNING &&
1488 	    dp->dp_scan->scn_phys.scn_func == POOL_SCAN_RESILVER);
1489 }
1490 
1491 /*
1492  * scrub consumers
1493  */
1494 
1495 static void
1496 count_block(zfs_all_blkstats_t *zab, const blkptr_t *bp)
1497 {
1498 	int i;
1499 
1500 	/*
1501 	 * If we resume after a reboot, zab will be NULL; don't record
1502 	 * incomplete stats in that case.
1503 	 */
1504 	if (zab == NULL)
1505 		return;
1506 
1507 	for (i = 0; i < 4; i++) {
1508 		int l = (i < 2) ? BP_GET_LEVEL(bp) : DN_MAX_LEVELS;
1509 		int t = (i & 1) ? BP_GET_TYPE(bp) : DMU_OT_TOTAL;
1510 		zfs_blkstat_t *zb = &zab->zab_type[l][t];
1511 		int equal;
1512 
1513 		zb->zb_count++;
1514 		zb->zb_asize += BP_GET_ASIZE(bp);
1515 		zb->zb_lsize += BP_GET_LSIZE(bp);
1516 		zb->zb_psize += BP_GET_PSIZE(bp);
1517 		zb->zb_gangs += BP_COUNT_GANG(bp);
1518 
1519 		switch (BP_GET_NDVAS(bp)) {
1520 		case 2:
1521 			if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
1522 			    DVA_GET_VDEV(&bp->blk_dva[1]))
1523 				zb->zb_ditto_2_of_2_samevdev++;
1524 			break;
1525 		case 3:
1526 			equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
1527 			    DVA_GET_VDEV(&bp->blk_dva[1])) +
1528 			    (DVA_GET_VDEV(&bp->blk_dva[0]) ==
1529 			    DVA_GET_VDEV(&bp->blk_dva[2])) +
1530 			    (DVA_GET_VDEV(&bp->blk_dva[1]) ==
1531 			    DVA_GET_VDEV(&bp->blk_dva[2]));
1532 			if (equal == 1)
1533 				zb->zb_ditto_2_of_3_samevdev++;
1534 			else if (equal == 3)
1535 				zb->zb_ditto_3_of_3_samevdev++;
1536 			break;
1537 		}
1538 	}
1539 }
1540 
1541 static void
1542 dsl_scan_scrub_done(zio_t *zio)
1543 {
1544 	spa_t *spa = zio->io_spa;
1545 
1546 	zio_data_buf_free(zio->io_data, zio->io_size);
1547 
1548 	mutex_enter(&spa->spa_scrub_lock);
1549 	spa->spa_scrub_inflight--;
1550 	cv_broadcast(&spa->spa_scrub_io_cv);
1551 
1552 	if (zio->io_error && (zio->io_error != ECKSUM ||
1553 	    !(zio->io_flags & ZIO_FLAG_SPECULATIVE))) {
1554 		spa->spa_dsl_pool->dp_scan->scn_phys.scn_errors++;
1555 	}
1556 	mutex_exit(&spa->spa_scrub_lock);
1557 }
1558 
1559 static int
1560 dsl_scan_scrub_cb(dsl_pool_t *dp,
1561     const blkptr_t *bp, const zbookmark_t *zb)
1562 {
1563 	dsl_scan_t *scn = dp->dp_scan;
1564 	size_t size = BP_GET_PSIZE(bp);
1565 	spa_t *spa = dp->dp_spa;
1566 	uint64_t phys_birth = BP_PHYSICAL_BIRTH(bp);
1567 	boolean_t needs_io;
1568 	int zio_flags = ZIO_FLAG_SCRUB_THREAD | ZIO_FLAG_RAW | ZIO_FLAG_CANFAIL;
1569 	int zio_priority;
1570 
1571 	if (phys_birth <= scn->scn_phys.scn_min_txg ||
1572 	    phys_birth >= scn->scn_phys.scn_max_txg)
1573 		return (0);
1574 
1575 	count_block(dp->dp_blkstats, bp);
1576 
1577 	ASSERT(DSL_SCAN_IS_SCRUB_RESILVER(scn));
1578 	if (scn->scn_phys.scn_func == POOL_SCAN_SCRUB) {
1579 		zio_flags |= ZIO_FLAG_SCRUB;
1580 		zio_priority = ZIO_PRIORITY_SCRUB;
1581 		needs_io = B_TRUE;
1582 	} else if (scn->scn_phys.scn_func == POOL_SCAN_RESILVER) {
1583 		zio_flags |= ZIO_FLAG_RESILVER;
1584 		zio_priority = ZIO_PRIORITY_RESILVER;
1585 		needs_io = B_FALSE;
1586 	}
1587 
1588 	/* If it's an intent log block, failure is expected. */
1589 	if (zb->zb_level == ZB_ZIL_LEVEL)
1590 		zio_flags |= ZIO_FLAG_SPECULATIVE;
1591 
1592 	for (int d = 0; d < BP_GET_NDVAS(bp); d++) {
1593 		vdev_t *vd = vdev_lookup_top(spa,
1594 		    DVA_GET_VDEV(&bp->blk_dva[d]));
1595 
1596 		/*
1597 		 * Keep track of how much data we've examined so that
1598 		 * zpool(1M) status can make useful progress reports.
1599 		 */
1600 		scn->scn_phys.scn_examined += DVA_GET_ASIZE(&bp->blk_dva[d]);
1601 		spa->spa_scan_pass_exam += DVA_GET_ASIZE(&bp->blk_dva[d]);
1602 
1603 		/* if it's a resilver, this may not be in the target range */
1604 		if (!needs_io) {
1605 			if (DVA_GET_GANG(&bp->blk_dva[d])) {
1606 				/*
1607 				 * Gang members may be spread across multiple
1608 				 * vdevs, so the best estimate we have is the
1609 				 * scrub range, which has already been checked.
1610 				 * XXX -- it would be better to change our
1611 				 * allocation policy to ensure that all
1612 				 * gang members reside on the same vdev.
1613 				 */
1614 				needs_io = B_TRUE;
1615 			} else {
1616 				needs_io = vdev_dtl_contains(vd, DTL_PARTIAL,
1617 				    phys_birth, 1);
1618 			}
1619 		}
1620 	}
1621 
1622 	if (needs_io && !zfs_no_scrub_io) {
1623 		void *data = zio_data_buf_alloc(size);
1624 
1625 		mutex_enter(&spa->spa_scrub_lock);
1626 		while (spa->spa_scrub_inflight >= spa->spa_scrub_maxinflight)
1627 			cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
1628 		spa->spa_scrub_inflight++;
1629 		mutex_exit(&spa->spa_scrub_lock);
1630 
1631 		zio_nowait(zio_read(NULL, spa, bp, data, size,
1632 		    dsl_scan_scrub_done, NULL, zio_priority,
1633 		    zio_flags, zb));
1634 	}
1635 
1636 	/* do not relocate this block */
1637 	return (0);
1638 }
1639 
1640 int
1641 dsl_scan(dsl_pool_t *dp, pool_scan_func_t func)
1642 {
1643 	spa_t *spa = dp->dp_spa;
1644 
1645 	/*
1646 	 * Purge all vdev caches and probe all devices.  We do this here
1647 	 * rather than in sync context because this requires a writer lock
1648 	 * on the spa_config lock, which we can't do from sync context.  The
1649 	 * spa_scrub_reopen flag indicates that vdev_open() should not
1650 	 * attempt to start another scrub.
1651 	 */
1652 	spa_vdev_state_enter(spa, SCL_NONE);
1653 	spa->spa_scrub_reopen = B_TRUE;
1654 	vdev_reopen(spa->spa_root_vdev);
1655 	spa->spa_scrub_reopen = B_FALSE;
1656 	(void) spa_vdev_state_exit(spa, NULL, 0);
1657 
1658 	return (dsl_sync_task_do(dp, dsl_scan_setup_check,
1659 	    dsl_scan_setup_sync, dp->dp_scan, &func, 0));
1660 }
1661