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