xref: /onnv-gate/usr/src/uts/common/fs/zfs/dsl_scan.c (revision 12470:54258108784b)
112296SLin.Ling@Sun.COM /*
212296SLin.Ling@Sun.COM  * CDDL HEADER START
312296SLin.Ling@Sun.COM  *
412296SLin.Ling@Sun.COM  * The contents of this file are subject to the terms of the
512296SLin.Ling@Sun.COM  * Common Development and Distribution License (the "License").
612296SLin.Ling@Sun.COM  * You may not use this file except in compliance with the License.
712296SLin.Ling@Sun.COM  *
812296SLin.Ling@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
912296SLin.Ling@Sun.COM  * or http://www.opensolaris.org/os/licensing.
1012296SLin.Ling@Sun.COM  * See the License for the specific language governing permissions
1112296SLin.Ling@Sun.COM  * and limitations under the License.
1212296SLin.Ling@Sun.COM  *
1312296SLin.Ling@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
1412296SLin.Ling@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1512296SLin.Ling@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
1612296SLin.Ling@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
1712296SLin.Ling@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
1812296SLin.Ling@Sun.COM  *
1912296SLin.Ling@Sun.COM  * CDDL HEADER END
2012296SLin.Ling@Sun.COM  */
2112296SLin.Ling@Sun.COM /*
2212296SLin.Ling@Sun.COM  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
2312296SLin.Ling@Sun.COM  */
2412296SLin.Ling@Sun.COM 
2512296SLin.Ling@Sun.COM #include <sys/dsl_scan.h>
2612296SLin.Ling@Sun.COM #include <sys/dsl_pool.h>
2712296SLin.Ling@Sun.COM #include <sys/dsl_dataset.h>
2812296SLin.Ling@Sun.COM #include <sys/dsl_prop.h>
2912296SLin.Ling@Sun.COM #include <sys/dsl_dir.h>
3012296SLin.Ling@Sun.COM #include <sys/dsl_synctask.h>
3112296SLin.Ling@Sun.COM #include <sys/dnode.h>
3212296SLin.Ling@Sun.COM #include <sys/dmu_tx.h>
3312296SLin.Ling@Sun.COM #include <sys/dmu_objset.h>
3412296SLin.Ling@Sun.COM #include <sys/arc.h>
3512296SLin.Ling@Sun.COM #include <sys/zap.h>
3612296SLin.Ling@Sun.COM #include <sys/zio.h>
3712296SLin.Ling@Sun.COM #include <sys/zfs_context.h>
3812296SLin.Ling@Sun.COM #include <sys/fs/zfs.h>
3912296SLin.Ling@Sun.COM #include <sys/zfs_znode.h>
4012296SLin.Ling@Sun.COM #include <sys/spa_impl.h>
4112296SLin.Ling@Sun.COM #include <sys/vdev_impl.h>
4212296SLin.Ling@Sun.COM #include <sys/zil_impl.h>
4312296SLin.Ling@Sun.COM #include <sys/zio_checksum.h>
4412296SLin.Ling@Sun.COM #include <sys/ddt.h>
4512296SLin.Ling@Sun.COM #include <sys/sa.h>
4612296SLin.Ling@Sun.COM #include <sys/sa_impl.h>
4712296SLin.Ling@Sun.COM #ifdef _KERNEL
4812296SLin.Ling@Sun.COM #include <sys/zfs_vfsops.h>
4912296SLin.Ling@Sun.COM #endif
5012296SLin.Ling@Sun.COM 
5112296SLin.Ling@Sun.COM typedef int (scan_cb_t)(dsl_pool_t *, const blkptr_t *, const zbookmark_t *);
5212296SLin.Ling@Sun.COM 
5312296SLin.Ling@Sun.COM static scan_cb_t dsl_scan_defrag_cb;
5412296SLin.Ling@Sun.COM static scan_cb_t dsl_scan_scrub_cb;
5512296SLin.Ling@Sun.COM static scan_cb_t dsl_scan_remove_cb;
5612296SLin.Ling@Sun.COM static dsl_syncfunc_t dsl_scan_cancel_sync;
5712296SLin.Ling@Sun.COM static void dsl_scan_sync_state(dsl_scan_t *, dmu_tx_t *tx);
5812296SLin.Ling@Sun.COM 
5912296SLin.Ling@Sun.COM int zfs_scan_min_time_ms = 1000; /* min millisecs to scrub per txg */
60*12470SMatthew.Ahrens@Sun.COM int zfs_free_min_time_ms = 1000; /* min millisecs to free per txg */
6112296SLin.Ling@Sun.COM int zfs_resilver_min_time_ms = 3000; /* min millisecs to resilver per txg */
6212296SLin.Ling@Sun.COM boolean_t zfs_no_scrub_io = B_FALSE; /* set to disable scrub i/o */
6312296SLin.Ling@Sun.COM boolean_t zfs_no_scrub_prefetch = B_FALSE; /* set to disable srub prefetching */
6412296SLin.Ling@Sun.COM enum ddt_class zfs_scrub_ddt_class_max = DDT_CLASS_DUPLICATE;
6512296SLin.Ling@Sun.COM int dsl_scan_delay_completion = B_FALSE; /* set to delay scan completion */
6612296SLin.Ling@Sun.COM 
6712296SLin.Ling@Sun.COM #define	DSL_SCAN_IS_SCRUB_RESILVER(scn) \
6812296SLin.Ling@Sun.COM 	((scn)->scn_phys.scn_func == POOL_SCAN_SCRUB || \
6912296SLin.Ling@Sun.COM 	(scn)->scn_phys.scn_func == POOL_SCAN_RESILVER)
7012296SLin.Ling@Sun.COM 
7112296SLin.Ling@Sun.COM extern int zfs_txg_timeout;
7212296SLin.Ling@Sun.COM 
7312296SLin.Ling@Sun.COM /* the order has to match pool_scan_type */
7412296SLin.Ling@Sun.COM static scan_cb_t *scan_funcs[POOL_SCAN_FUNCS] = {
7512296SLin.Ling@Sun.COM 	NULL,
7612296SLin.Ling@Sun.COM 	dsl_scan_scrub_cb,	/* POOL_SCAN_SCRUB */
7712296SLin.Ling@Sun.COM 	dsl_scan_scrub_cb,	/* POOL_SCAN_RESILVER */
7812296SLin.Ling@Sun.COM };
7912296SLin.Ling@Sun.COM 
8012296SLin.Ling@Sun.COM int
8112296SLin.Ling@Sun.COM dsl_scan_init(dsl_pool_t *dp, uint64_t txg)
8212296SLin.Ling@Sun.COM {
8312296SLin.Ling@Sun.COM 	int err;
8412296SLin.Ling@Sun.COM 	dsl_scan_t *scn;
8512296SLin.Ling@Sun.COM 	spa_t *spa = dp->dp_spa;
8612296SLin.Ling@Sun.COM 	uint64_t f;
8712296SLin.Ling@Sun.COM 
8812296SLin.Ling@Sun.COM 	scn = dp->dp_scan = kmem_zalloc(sizeof (dsl_scan_t), KM_SLEEP);
8912296SLin.Ling@Sun.COM 	scn->scn_dp = dp;
9012296SLin.Ling@Sun.COM 
9112296SLin.Ling@Sun.COM 	err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
9212296SLin.Ling@Sun.COM 	    "scrub_func", sizeof (uint64_t), 1, &f);
9312296SLin.Ling@Sun.COM 	if (err == 0) {
9412296SLin.Ling@Sun.COM 		/*
9512296SLin.Ling@Sun.COM 		 * There was an old-style scrub in progress.  Restart a
9612296SLin.Ling@Sun.COM 		 * new-style scrub from the beginning.
9712296SLin.Ling@Sun.COM 		 */
9812296SLin.Ling@Sun.COM 		scn->scn_restart_txg = txg;
9912296SLin.Ling@Sun.COM 		zfs_dbgmsg("old-style scrub was in progress; "
10012296SLin.Ling@Sun.COM 		    "restarting new-style scrub in txg %llu",
10112296SLin.Ling@Sun.COM 		    scn->scn_restart_txg);
10212296SLin.Ling@Sun.COM 
10312296SLin.Ling@Sun.COM 		/*
10412296SLin.Ling@Sun.COM 		 * Load the queue obj from the old location so that it
10512296SLin.Ling@Sun.COM 		 * can be freed by dsl_scan_done().
10612296SLin.Ling@Sun.COM 		 */
10712296SLin.Ling@Sun.COM 		(void) zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
10812296SLin.Ling@Sun.COM 		    "scrub_queue", sizeof (uint64_t), 1,
10912296SLin.Ling@Sun.COM 		    &scn->scn_phys.scn_queue_obj);
11012296SLin.Ling@Sun.COM 	} else {
11112296SLin.Ling@Sun.COM 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
11212296SLin.Ling@Sun.COM 		    DMU_POOL_SCAN, sizeof (uint64_t), SCAN_PHYS_NUMINTS,
11312296SLin.Ling@Sun.COM 		    &scn->scn_phys);
11412296SLin.Ling@Sun.COM 		if (err == ENOENT)
11512296SLin.Ling@Sun.COM 			return (0);
11612296SLin.Ling@Sun.COM 		else if (err)
11712296SLin.Ling@Sun.COM 			return (err);
11812296SLin.Ling@Sun.COM 
11912296SLin.Ling@Sun.COM 		if (scn->scn_phys.scn_state == DSS_SCANNING &&
12012296SLin.Ling@Sun.COM 		    spa_prev_software_version(dp->dp_spa) < SPA_VERSION_SCAN) {
12112296SLin.Ling@Sun.COM 			/*
12212296SLin.Ling@Sun.COM 			 * A new-type scrub was in progress on an old
12312296SLin.Ling@Sun.COM 			 * pool, and the pool was accessed by old
12412296SLin.Ling@Sun.COM 			 * software.  Restart from the beginning, since
12512296SLin.Ling@Sun.COM 			 * the old software may have changed the pool in
12612296SLin.Ling@Sun.COM 			 * the meantime.
12712296SLin.Ling@Sun.COM 			 */
12812296SLin.Ling@Sun.COM 			scn->scn_restart_txg = txg;
12912296SLin.Ling@Sun.COM 			zfs_dbgmsg("new-style scrub was modified "
13012296SLin.Ling@Sun.COM 			    "by old software; restarting in txg %llu",
13112296SLin.Ling@Sun.COM 			    scn->scn_restart_txg);
13212296SLin.Ling@Sun.COM 		}
13312296SLin.Ling@Sun.COM 	}
13412296SLin.Ling@Sun.COM 
13512296SLin.Ling@Sun.COM 	spa_scan_stat_init(spa);
13612296SLin.Ling@Sun.COM 	return (0);
13712296SLin.Ling@Sun.COM }
13812296SLin.Ling@Sun.COM 
13912296SLin.Ling@Sun.COM void
14012296SLin.Ling@Sun.COM dsl_scan_fini(dsl_pool_t *dp)
14112296SLin.Ling@Sun.COM {
14212296SLin.Ling@Sun.COM 	if (dp->dp_scan) {
14312296SLin.Ling@Sun.COM 		kmem_free(dp->dp_scan, sizeof (dsl_scan_t));
14412296SLin.Ling@Sun.COM 		dp->dp_scan = NULL;
14512296SLin.Ling@Sun.COM 	}
14612296SLin.Ling@Sun.COM }
14712296SLin.Ling@Sun.COM 
14812296SLin.Ling@Sun.COM /* ARGSUSED */
14912296SLin.Ling@Sun.COM static int
15012296SLin.Ling@Sun.COM dsl_scan_setup_check(void *arg1, void *arg2, dmu_tx_t *tx)
15112296SLin.Ling@Sun.COM {
15212296SLin.Ling@Sun.COM 	dsl_scan_t *scn = arg1;
15312296SLin.Ling@Sun.COM 
15412296SLin.Ling@Sun.COM 	if (scn->scn_phys.scn_state == DSS_SCANNING)
15512296SLin.Ling@Sun.COM 		return (EBUSY);
15612296SLin.Ling@Sun.COM 
15712296SLin.Ling@Sun.COM 	return (0);
15812296SLin.Ling@Sun.COM }
15912296SLin.Ling@Sun.COM 
16012296SLin.Ling@Sun.COM /* ARGSUSED */
16112296SLin.Ling@Sun.COM static void
16212296SLin.Ling@Sun.COM dsl_scan_setup_sync(void *arg1, void *arg2, dmu_tx_t *tx)
16312296SLin.Ling@Sun.COM {
16412296SLin.Ling@Sun.COM 	dsl_scan_t *scn = arg1;
16512296SLin.Ling@Sun.COM 	pool_scan_func_t *funcp = arg2;
16612296SLin.Ling@Sun.COM 	dmu_object_type_t ot = 0;
16712296SLin.Ling@Sun.COM 	dsl_pool_t *dp = scn->scn_dp;
16812296SLin.Ling@Sun.COM 	spa_t *spa = dp->dp_spa;
16912296SLin.Ling@Sun.COM 
17012296SLin.Ling@Sun.COM 	ASSERT(scn->scn_phys.scn_state != DSS_SCANNING);
17112296SLin.Ling@Sun.COM 	ASSERT(*funcp > POOL_SCAN_NONE && *funcp < POOL_SCAN_FUNCS);
17212296SLin.Ling@Sun.COM 	bzero(&scn->scn_phys, sizeof (scn->scn_phys));
17312296SLin.Ling@Sun.COM 	scn->scn_phys.scn_func = *funcp;
17412296SLin.Ling@Sun.COM 	scn->scn_phys.scn_state = DSS_SCANNING;
17512296SLin.Ling@Sun.COM 	scn->scn_phys.scn_min_txg = 0;
17612296SLin.Ling@Sun.COM 	scn->scn_phys.scn_max_txg = tx->tx_txg;
17712296SLin.Ling@Sun.COM 	scn->scn_phys.scn_ddt_class_max = DDT_CLASSES - 1; /* the entire DDT */
17812296SLin.Ling@Sun.COM 	scn->scn_phys.scn_start_time = gethrestime_sec();
17912296SLin.Ling@Sun.COM 	scn->scn_phys.scn_errors = 0;
18012296SLin.Ling@Sun.COM 	scn->scn_phys.scn_to_examine = spa->spa_root_vdev->vdev_stat.vs_alloc;
18112296SLin.Ling@Sun.COM 	scn->scn_restart_txg = 0;
18212296SLin.Ling@Sun.COM 	spa_scan_stat_init(spa);
18312296SLin.Ling@Sun.COM 
18412296SLin.Ling@Sun.COM 	if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
18512296SLin.Ling@Sun.COM 		scn->scn_phys.scn_ddt_class_max = zfs_scrub_ddt_class_max;
18612296SLin.Ling@Sun.COM 
18712296SLin.Ling@Sun.COM 		/* rewrite all disk labels */
18812296SLin.Ling@Sun.COM 		vdev_config_dirty(spa->spa_root_vdev);
18912296SLin.Ling@Sun.COM 
19012296SLin.Ling@Sun.COM 		if (vdev_resilver_needed(spa->spa_root_vdev,
19112296SLin.Ling@Sun.COM 		    &scn->scn_phys.scn_min_txg, &scn->scn_phys.scn_max_txg)) {
19212296SLin.Ling@Sun.COM 			spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_START);
19312296SLin.Ling@Sun.COM 		} else {
19412296SLin.Ling@Sun.COM 			spa_event_notify(spa, NULL, ESC_ZFS_SCRUB_START);
19512296SLin.Ling@Sun.COM 		}
19612296SLin.Ling@Sun.COM 
19712296SLin.Ling@Sun.COM 		spa->spa_scrub_started = B_TRUE;
19812296SLin.Ling@Sun.COM 		/*
19912296SLin.Ling@Sun.COM 		 * If this is an incremental scrub, limit the DDT scrub phase
20012296SLin.Ling@Sun.COM 		 * to just the auto-ditto class (for correctness); the rest
20112296SLin.Ling@Sun.COM 		 * of the scrub should go faster using top-down pruning.
20212296SLin.Ling@Sun.COM 		 */
20312296SLin.Ling@Sun.COM 		if (scn->scn_phys.scn_min_txg > TXG_INITIAL)
20412296SLin.Ling@Sun.COM 			scn->scn_phys.scn_ddt_class_max = DDT_CLASS_DITTO;
20512296SLin.Ling@Sun.COM 
20612296SLin.Ling@Sun.COM 	}
20712296SLin.Ling@Sun.COM 
20812296SLin.Ling@Sun.COM 	/* back to the generic stuff */
20912296SLin.Ling@Sun.COM 
21012296SLin.Ling@Sun.COM 	if (dp->dp_blkstats == NULL) {
21112296SLin.Ling@Sun.COM 		dp->dp_blkstats =
21212296SLin.Ling@Sun.COM 		    kmem_alloc(sizeof (zfs_all_blkstats_t), KM_SLEEP);
21312296SLin.Ling@Sun.COM 	}
21412296SLin.Ling@Sun.COM 	bzero(dp->dp_blkstats, sizeof (zfs_all_blkstats_t));
21512296SLin.Ling@Sun.COM 
21612296SLin.Ling@Sun.COM 	if (spa_version(spa) < SPA_VERSION_DSL_SCRUB)
21712296SLin.Ling@Sun.COM 		ot = DMU_OT_ZAP_OTHER;
21812296SLin.Ling@Sun.COM 
21912296SLin.Ling@Sun.COM 	scn->scn_phys.scn_queue_obj = zap_create(dp->dp_meta_objset,
22012296SLin.Ling@Sun.COM 	    ot ? ot : DMU_OT_SCAN_QUEUE, DMU_OT_NONE, 0, tx);
22112296SLin.Ling@Sun.COM 
22212296SLin.Ling@Sun.COM 	dsl_scan_sync_state(scn, tx);
22312296SLin.Ling@Sun.COM 
22412296SLin.Ling@Sun.COM 	spa_history_log_internal(LOG_POOL_SCAN, spa, tx,
22512296SLin.Ling@Sun.COM 	    "func=%u mintxg=%llu maxtxg=%llu",
22612296SLin.Ling@Sun.COM 	    *funcp, scn->scn_phys.scn_min_txg, scn->scn_phys.scn_max_txg);
22712296SLin.Ling@Sun.COM }
22812296SLin.Ling@Sun.COM 
22912296SLin.Ling@Sun.COM /* ARGSUSED */
23012296SLin.Ling@Sun.COM static void
23112296SLin.Ling@Sun.COM dsl_scan_done(dsl_scan_t *scn, boolean_t complete, dmu_tx_t *tx)
23212296SLin.Ling@Sun.COM {
23312296SLin.Ling@Sun.COM 	static const char *old_names[] = {
23412296SLin.Ling@Sun.COM 		"scrub_bookmark",
23512296SLin.Ling@Sun.COM 		"scrub_ddt_bookmark",
23612296SLin.Ling@Sun.COM 		"scrub_ddt_class_max",
23712296SLin.Ling@Sun.COM 		"scrub_queue",
23812296SLin.Ling@Sun.COM 		"scrub_min_txg",
23912296SLin.Ling@Sun.COM 		"scrub_max_txg",
24012296SLin.Ling@Sun.COM 		"scrub_func",
24112296SLin.Ling@Sun.COM 		"scrub_errors",
24212296SLin.Ling@Sun.COM 		NULL
24312296SLin.Ling@Sun.COM 	};
24412296SLin.Ling@Sun.COM 
24512296SLin.Ling@Sun.COM 	dsl_pool_t *dp = scn->scn_dp;
24612296SLin.Ling@Sun.COM 	spa_t *spa = dp->dp_spa;
24712296SLin.Ling@Sun.COM 	int i;
24812296SLin.Ling@Sun.COM 
24912296SLin.Ling@Sun.COM 	/* Remove any remnants of an old-style scrub. */
25012296SLin.Ling@Sun.COM 	for (i = 0; old_names[i]; i++) {
25112296SLin.Ling@Sun.COM 		(void) zap_remove(dp->dp_meta_objset,
25212296SLin.Ling@Sun.COM 		    DMU_POOL_DIRECTORY_OBJECT, old_names[i], tx);
25312296SLin.Ling@Sun.COM 	}
25412296SLin.Ling@Sun.COM 
25512296SLin.Ling@Sun.COM 	if (scn->scn_phys.scn_queue_obj != 0) {
25612296SLin.Ling@Sun.COM 		VERIFY(0 == dmu_object_free(dp->dp_meta_objset,
25712296SLin.Ling@Sun.COM 		    scn->scn_phys.scn_queue_obj, tx));
25812296SLin.Ling@Sun.COM 		scn->scn_phys.scn_queue_obj = 0;
25912296SLin.Ling@Sun.COM 	}
26012296SLin.Ling@Sun.COM 
26112296SLin.Ling@Sun.COM 	/*
26212296SLin.Ling@Sun.COM 	 * If we were "restarted" from a stopped state, don't bother
26312296SLin.Ling@Sun.COM 	 * with anything else.
26412296SLin.Ling@Sun.COM 	 */
26512296SLin.Ling@Sun.COM 	if (scn->scn_phys.scn_state != DSS_SCANNING)
26612296SLin.Ling@Sun.COM 		return;
26712296SLin.Ling@Sun.COM 
26812296SLin.Ling@Sun.COM 	if (complete)
26912296SLin.Ling@Sun.COM 		scn->scn_phys.scn_state = DSS_FINISHED;
27012296SLin.Ling@Sun.COM 	else
27112296SLin.Ling@Sun.COM 		scn->scn_phys.scn_state = DSS_CANCELED;
27212296SLin.Ling@Sun.COM 
27312296SLin.Ling@Sun.COM 	spa_history_log_internal(LOG_POOL_SCAN_DONE, spa, tx,
27412296SLin.Ling@Sun.COM 	    "complete=%u", complete);
27512296SLin.Ling@Sun.COM 
27612296SLin.Ling@Sun.COM 	if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
27712296SLin.Ling@Sun.COM 		mutex_enter(&spa->spa_scrub_lock);
27812296SLin.Ling@Sun.COM 		while (spa->spa_scrub_inflight > 0) {
27912296SLin.Ling@Sun.COM 			cv_wait(&spa->spa_scrub_io_cv,
28012296SLin.Ling@Sun.COM 			    &spa->spa_scrub_lock);
28112296SLin.Ling@Sun.COM 		}
28212296SLin.Ling@Sun.COM 		mutex_exit(&spa->spa_scrub_lock);
28312296SLin.Ling@Sun.COM 		spa->spa_scrub_started = B_FALSE;
28412296SLin.Ling@Sun.COM 		spa->spa_scrub_active = B_FALSE;
28512296SLin.Ling@Sun.COM 
28612296SLin.Ling@Sun.COM 		/*
28712296SLin.Ling@Sun.COM 		 * If the scrub/resilver completed, update all DTLs to
28812296SLin.Ling@Sun.COM 		 * reflect this.  Whether it succeeded or not, vacate
28912296SLin.Ling@Sun.COM 		 * all temporary scrub DTLs.
29012296SLin.Ling@Sun.COM 		 */
29112296SLin.Ling@Sun.COM 		vdev_dtl_reassess(spa->spa_root_vdev, tx->tx_txg,
29212296SLin.Ling@Sun.COM 		    complete ? scn->scn_phys.scn_max_txg : 0, B_TRUE);
29312296SLin.Ling@Sun.COM 		if (complete) {
29412296SLin.Ling@Sun.COM 			spa_event_notify(spa, NULL, scn->scn_phys.scn_min_txg ?
29512296SLin.Ling@Sun.COM 			    ESC_ZFS_RESILVER_FINISH : ESC_ZFS_SCRUB_FINISH);
29612296SLin.Ling@Sun.COM 		}
29712296SLin.Ling@Sun.COM 		spa_errlog_rotate(spa);
29812296SLin.Ling@Sun.COM 
29912296SLin.Ling@Sun.COM 		/*
30012296SLin.Ling@Sun.COM 		 * We may have finished replacing a device.
30112296SLin.Ling@Sun.COM 		 * Let the async thread assess this and handle the detach.
30212296SLin.Ling@Sun.COM 		 */
30312296SLin.Ling@Sun.COM 		spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
30412296SLin.Ling@Sun.COM 	}
30512296SLin.Ling@Sun.COM 
30612296SLin.Ling@Sun.COM 	scn->scn_phys.scn_end_time = gethrestime_sec();
30712296SLin.Ling@Sun.COM }
30812296SLin.Ling@Sun.COM 
30912296SLin.Ling@Sun.COM /* ARGSUSED */
31012296SLin.Ling@Sun.COM static int
31112296SLin.Ling@Sun.COM dsl_scan_cancel_check(void *arg1, void *arg2, dmu_tx_t *tx)
31212296SLin.Ling@Sun.COM {
31312296SLin.Ling@Sun.COM 	dsl_scan_t *scn = arg1;
31412296SLin.Ling@Sun.COM 
31512296SLin.Ling@Sun.COM 	if (scn->scn_phys.scn_state != DSS_SCANNING)
31612296SLin.Ling@Sun.COM 		return (ENOENT);
31712296SLin.Ling@Sun.COM 	return (0);
31812296SLin.Ling@Sun.COM }
31912296SLin.Ling@Sun.COM 
32012296SLin.Ling@Sun.COM /* ARGSUSED */
32112296SLin.Ling@Sun.COM static void
32212296SLin.Ling@Sun.COM dsl_scan_cancel_sync(void *arg1, void *arg2, dmu_tx_t *tx)
32312296SLin.Ling@Sun.COM {
32412296SLin.Ling@Sun.COM 	dsl_scan_t *scn = arg1;
32512296SLin.Ling@Sun.COM 
32612296SLin.Ling@Sun.COM 	dsl_scan_done(scn, B_FALSE, tx);
32712296SLin.Ling@Sun.COM 	dsl_scan_sync_state(scn, tx);
32812296SLin.Ling@Sun.COM }
32912296SLin.Ling@Sun.COM 
33012296SLin.Ling@Sun.COM int
33112296SLin.Ling@Sun.COM dsl_scan_cancel(dsl_pool_t *dp)
33212296SLin.Ling@Sun.COM {
33312296SLin.Ling@Sun.COM 	boolean_t complete = B_FALSE;
33412296SLin.Ling@Sun.COM 	int err;
33512296SLin.Ling@Sun.COM 
33612296SLin.Ling@Sun.COM 	err = dsl_sync_task_do(dp, dsl_scan_cancel_check,
33712296SLin.Ling@Sun.COM 	    dsl_scan_cancel_sync, dp->dp_scan, &complete, 3);
33812296SLin.Ling@Sun.COM 	return (err);
33912296SLin.Ling@Sun.COM }
34012296SLin.Ling@Sun.COM 
34112296SLin.Ling@Sun.COM static void dsl_scan_visitbp(blkptr_t *bp,
34212296SLin.Ling@Sun.COM     const zbookmark_t *zb, dnode_phys_t *dnp, arc_buf_t *pbuf,
34312296SLin.Ling@Sun.COM     dsl_dataset_t *ds, dsl_scan_t *scn, dmu_objset_type_t ostype,
34412296SLin.Ling@Sun.COM     dmu_tx_t *tx);
34512296SLin.Ling@Sun.COM static void dsl_scan_visitdnode(dsl_scan_t *, dsl_dataset_t *ds,
34612296SLin.Ling@Sun.COM     dmu_objset_type_t ostype,
34712296SLin.Ling@Sun.COM     dnode_phys_t *dnp, arc_buf_t *buf, uint64_t object, dmu_tx_t *tx);
34812296SLin.Ling@Sun.COM 
34912296SLin.Ling@Sun.COM void
35012296SLin.Ling@Sun.COM dsl_free(dsl_pool_t *dp, uint64_t txg, const blkptr_t *bp)
35112296SLin.Ling@Sun.COM {
35212296SLin.Ling@Sun.COM 	zio_free(dp->dp_spa, txg, bp);
35312296SLin.Ling@Sun.COM }
35412296SLin.Ling@Sun.COM 
35512296SLin.Ling@Sun.COM void
35612296SLin.Ling@Sun.COM dsl_free_sync(zio_t *pio, dsl_pool_t *dp, uint64_t txg, const blkptr_t *bpp)
35712296SLin.Ling@Sun.COM {
35812296SLin.Ling@Sun.COM 	ASSERT(dsl_pool_sync_context(dp));
35912296SLin.Ling@Sun.COM 	zio_nowait(zio_free_sync(pio, dp->dp_spa, txg, bpp, pio->io_flags));
36012296SLin.Ling@Sun.COM }
36112296SLin.Ling@Sun.COM 
36212296SLin.Ling@Sun.COM int
36312296SLin.Ling@Sun.COM dsl_read(zio_t *pio, spa_t *spa, const blkptr_t *bpp, arc_buf_t *pbuf,
36412296SLin.Ling@Sun.COM     arc_done_func_t *done, void *private, int priority, int zio_flags,
36512296SLin.Ling@Sun.COM     uint32_t *arc_flags, const zbookmark_t *zb)
36612296SLin.Ling@Sun.COM {
36712296SLin.Ling@Sun.COM 	return (arc_read(pio, spa, bpp, pbuf, done, private,
36812296SLin.Ling@Sun.COM 	    priority, zio_flags, arc_flags, zb));
36912296SLin.Ling@Sun.COM }
37012296SLin.Ling@Sun.COM 
37112296SLin.Ling@Sun.COM int
37212296SLin.Ling@Sun.COM dsl_read_nolock(zio_t *pio, spa_t *spa, const blkptr_t *bpp,
37312296SLin.Ling@Sun.COM     arc_done_func_t *done, void *private, int priority, int zio_flags,
37412296SLin.Ling@Sun.COM     uint32_t *arc_flags, const zbookmark_t *zb)
37512296SLin.Ling@Sun.COM {
37612296SLin.Ling@Sun.COM 	return (arc_read_nolock(pio, spa, bpp, done, private,
37712296SLin.Ling@Sun.COM 	    priority, zio_flags, arc_flags, zb));
37812296SLin.Ling@Sun.COM }
37912296SLin.Ling@Sun.COM 
38012296SLin.Ling@Sun.COM static boolean_t
38112296SLin.Ling@Sun.COM bookmark_is_zero(const zbookmark_t *zb)
38212296SLin.Ling@Sun.COM {
38312296SLin.Ling@Sun.COM 	return (zb->zb_objset == 0 && zb->zb_object == 0 &&
38412296SLin.Ling@Sun.COM 	    zb->zb_level == 0 && zb->zb_blkid == 0);
38512296SLin.Ling@Sun.COM }
38612296SLin.Ling@Sun.COM 
38712296SLin.Ling@Sun.COM /* dnp is the dnode for zb1->zb_object */
38812296SLin.Ling@Sun.COM static boolean_t
38912296SLin.Ling@Sun.COM bookmark_is_before(const dnode_phys_t *dnp, const zbookmark_t *zb1,
39012296SLin.Ling@Sun.COM     const zbookmark_t *zb2)
39112296SLin.Ling@Sun.COM {
39212296SLin.Ling@Sun.COM 	uint64_t zb1nextL0, zb2thisobj;
39312296SLin.Ling@Sun.COM 
39412296SLin.Ling@Sun.COM 	ASSERT(zb1->zb_objset == zb2->zb_objset);
39512296SLin.Ling@Sun.COM 	ASSERT(zb2->zb_level == 0);
39612296SLin.Ling@Sun.COM 
39712296SLin.Ling@Sun.COM 	/*
39812296SLin.Ling@Sun.COM 	 * A bookmark in the deadlist is considered to be after
39912296SLin.Ling@Sun.COM 	 * everything else.
40012296SLin.Ling@Sun.COM 	 */
40112296SLin.Ling@Sun.COM 	if (zb2->zb_object == DMU_DEADLIST_OBJECT)
40212296SLin.Ling@Sun.COM 		return (B_TRUE);
40312296SLin.Ling@Sun.COM 
40412296SLin.Ling@Sun.COM 	/* The objset_phys_t isn't before anything. */
40512296SLin.Ling@Sun.COM 	if (dnp == NULL)
40612296SLin.Ling@Sun.COM 		return (B_FALSE);
40712296SLin.Ling@Sun.COM 
40812296SLin.Ling@Sun.COM 	zb1nextL0 = (zb1->zb_blkid + 1) <<
40912296SLin.Ling@Sun.COM 	    ((zb1->zb_level) * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT));
41012296SLin.Ling@Sun.COM 
41112296SLin.Ling@Sun.COM 	zb2thisobj = zb2->zb_object ? zb2->zb_object :
41212296SLin.Ling@Sun.COM 	    zb2->zb_blkid << (DNODE_BLOCK_SHIFT - DNODE_SHIFT);
41312296SLin.Ling@Sun.COM 
41412296SLin.Ling@Sun.COM 	if (zb1->zb_object == DMU_META_DNODE_OBJECT) {
41512296SLin.Ling@Sun.COM 		uint64_t nextobj = zb1nextL0 *
41612296SLin.Ling@Sun.COM 		    (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT) >> DNODE_SHIFT;
41712296SLin.Ling@Sun.COM 		return (nextobj <= zb2thisobj);
41812296SLin.Ling@Sun.COM 	}
41912296SLin.Ling@Sun.COM 
42012296SLin.Ling@Sun.COM 	if (zb1->zb_object < zb2thisobj)
42112296SLin.Ling@Sun.COM 		return (B_TRUE);
42212296SLin.Ling@Sun.COM 	if (zb1->zb_object > zb2thisobj)
42312296SLin.Ling@Sun.COM 		return (B_FALSE);
42412296SLin.Ling@Sun.COM 	if (zb2->zb_object == DMU_META_DNODE_OBJECT)
42512296SLin.Ling@Sun.COM 		return (B_FALSE);
42612296SLin.Ling@Sun.COM 	return (zb1nextL0 <= zb2->zb_blkid);
42712296SLin.Ling@Sun.COM }
42812296SLin.Ling@Sun.COM 
42912296SLin.Ling@Sun.COM static uint64_t
43012296SLin.Ling@Sun.COM dsl_scan_ds_maxtxg(dsl_dataset_t *ds)
43112296SLin.Ling@Sun.COM {
43212296SLin.Ling@Sun.COM 	uint64_t smt = ds->ds_dir->dd_pool->dp_scan->scn_phys.scn_max_txg;
43312296SLin.Ling@Sun.COM 	if (dsl_dataset_is_snapshot(ds))
43412296SLin.Ling@Sun.COM 		return (MIN(smt, ds->ds_phys->ds_creation_txg));
43512296SLin.Ling@Sun.COM 	return (smt);
43612296SLin.Ling@Sun.COM }
43712296SLin.Ling@Sun.COM 
43812296SLin.Ling@Sun.COM static void
43912296SLin.Ling@Sun.COM dsl_scan_sync_state(dsl_scan_t *scn, dmu_tx_t *tx)
44012296SLin.Ling@Sun.COM {
44112296SLin.Ling@Sun.COM 	VERIFY(0 == zap_update(scn->scn_dp->dp_meta_objset,
44212296SLin.Ling@Sun.COM 	    DMU_POOL_DIRECTORY_OBJECT,
44312296SLin.Ling@Sun.COM 	    DMU_POOL_SCAN, sizeof (uint64_t), SCAN_PHYS_NUMINTS,
44412296SLin.Ling@Sun.COM 	    &scn->scn_phys, tx));
44512296SLin.Ling@Sun.COM }
44612296SLin.Ling@Sun.COM 
44712296SLin.Ling@Sun.COM static boolean_t
44812296SLin.Ling@Sun.COM dsl_scan_check_pause(dsl_scan_t *scn, const zbookmark_t *zb)
44912296SLin.Ling@Sun.COM {
45012296SLin.Ling@Sun.COM 	uint64_t elapsed_nanosecs;
45112296SLin.Ling@Sun.COM 	int mintime;
45212296SLin.Ling@Sun.COM 
45312296SLin.Ling@Sun.COM 	/* we never skip user/group accounting objects */
45412296SLin.Ling@Sun.COM 	if (zb && (int64_t)zb->zb_object < 0)
45512296SLin.Ling@Sun.COM 		return (B_FALSE);
45612296SLin.Ling@Sun.COM 
45712296SLin.Ling@Sun.COM 	if (scn->scn_pausing)
45812296SLin.Ling@Sun.COM 		return (B_TRUE); /* we're already pausing */
45912296SLin.Ling@Sun.COM 
46012296SLin.Ling@Sun.COM 	if (!bookmark_is_zero(&scn->scn_phys.scn_bookmark))
46112296SLin.Ling@Sun.COM 		return (B_FALSE); /* we're resuming */
46212296SLin.Ling@Sun.COM 
46312296SLin.Ling@Sun.COM 	/* We only know how to resume from level-0 blocks. */
46412296SLin.Ling@Sun.COM 	if (zb && zb->zb_level != 0)
46512296SLin.Ling@Sun.COM 		return (B_FALSE);
46612296SLin.Ling@Sun.COM 
46712296SLin.Ling@Sun.COM 	mintime = (scn->scn_phys.scn_func == POOL_SCAN_RESILVER) ?
46812296SLin.Ling@Sun.COM 	    zfs_resilver_min_time_ms : zfs_scan_min_time_ms;
46912296SLin.Ling@Sun.COM 	elapsed_nanosecs = gethrtime() - scn->scn_sync_start_time;
47012296SLin.Ling@Sun.COM 	if (elapsed_nanosecs / NANOSEC > zfs_txg_timeout ||
47112296SLin.Ling@Sun.COM 	    (elapsed_nanosecs / MICROSEC > mintime &&
47212296SLin.Ling@Sun.COM 	    txg_sync_waiting(scn->scn_dp)) ||
47312296SLin.Ling@Sun.COM 	    spa_shutting_down(scn->scn_dp->dp_spa)) {
47412296SLin.Ling@Sun.COM 		if (zb) {
47512296SLin.Ling@Sun.COM 			dprintf("pausing at bookmark %llx/%llx/%llx/%llx\n",
47612296SLin.Ling@Sun.COM 			    (longlong_t)zb->zb_objset,
47712296SLin.Ling@Sun.COM 			    (longlong_t)zb->zb_object,
47812296SLin.Ling@Sun.COM 			    (longlong_t)zb->zb_level,
47912296SLin.Ling@Sun.COM 			    (longlong_t)zb->zb_blkid);
48012296SLin.Ling@Sun.COM 			scn->scn_phys.scn_bookmark = *zb;
48112296SLin.Ling@Sun.COM 		}
48212296SLin.Ling@Sun.COM 		dprintf("pausing at DDT bookmark %llx/%llx/%llx/%llx\n",
48312296SLin.Ling@Sun.COM 		    (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_class,
48412296SLin.Ling@Sun.COM 		    (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_type,
48512296SLin.Ling@Sun.COM 		    (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_checksum,
48612296SLin.Ling@Sun.COM 		    (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_cursor);
48712296SLin.Ling@Sun.COM 		scn->scn_pausing = B_TRUE;
48812296SLin.Ling@Sun.COM 		return (B_TRUE);
48912296SLin.Ling@Sun.COM 	}
49012296SLin.Ling@Sun.COM 	return (B_FALSE);
49112296SLin.Ling@Sun.COM }
49212296SLin.Ling@Sun.COM 
49312296SLin.Ling@Sun.COM typedef struct zil_scan_arg {
49412296SLin.Ling@Sun.COM 	dsl_pool_t	*zsa_dp;
49512296SLin.Ling@Sun.COM 	zil_header_t	*zsa_zh;
49612296SLin.Ling@Sun.COM } zil_scan_arg_t;
49712296SLin.Ling@Sun.COM 
49812296SLin.Ling@Sun.COM /* ARGSUSED */
49912296SLin.Ling@Sun.COM static int
50012296SLin.Ling@Sun.COM dsl_scan_zil_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
50112296SLin.Ling@Sun.COM {
50212296SLin.Ling@Sun.COM 	zil_scan_arg_t *zsa = arg;
50312296SLin.Ling@Sun.COM 	dsl_pool_t *dp = zsa->zsa_dp;
50412296SLin.Ling@Sun.COM 	dsl_scan_t *scn = dp->dp_scan;
50512296SLin.Ling@Sun.COM 	zil_header_t *zh = zsa->zsa_zh;
50612296SLin.Ling@Sun.COM 	zbookmark_t zb;
50712296SLin.Ling@Sun.COM 
50812296SLin.Ling@Sun.COM 	if (bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
50912296SLin.Ling@Sun.COM 		return (0);
51012296SLin.Ling@Sun.COM 
51112296SLin.Ling@Sun.COM 	/*
51212296SLin.Ling@Sun.COM 	 * One block ("stubby") can be allocated a long time ago; we
51312296SLin.Ling@Sun.COM 	 * want to visit that one because it has been allocated
51412296SLin.Ling@Sun.COM 	 * (on-disk) even if it hasn't been claimed (even though for
51512296SLin.Ling@Sun.COM 	 * scrub there's nothing to do to it).
51612296SLin.Ling@Sun.COM 	 */
51712296SLin.Ling@Sun.COM 	if (claim_txg == 0 && bp->blk_birth >= spa_first_txg(dp->dp_spa))
51812296SLin.Ling@Sun.COM 		return (0);
51912296SLin.Ling@Sun.COM 
52012296SLin.Ling@Sun.COM 	SET_BOOKMARK(&zb, zh->zh_log.blk_cksum.zc_word[ZIL_ZC_OBJSET],
52112296SLin.Ling@Sun.COM 	    ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, bp->blk_cksum.zc_word[ZIL_ZC_SEQ]);
52212296SLin.Ling@Sun.COM 
52312296SLin.Ling@Sun.COM 	VERIFY(0 == scan_funcs[scn->scn_phys.scn_func](dp, bp, &zb));
52412296SLin.Ling@Sun.COM 	return (0);
52512296SLin.Ling@Sun.COM }
52612296SLin.Ling@Sun.COM 
52712296SLin.Ling@Sun.COM /* ARGSUSED */
52812296SLin.Ling@Sun.COM static int
52912296SLin.Ling@Sun.COM dsl_scan_zil_record(zilog_t *zilog, lr_t *lrc, void *arg, uint64_t claim_txg)
53012296SLin.Ling@Sun.COM {
53112296SLin.Ling@Sun.COM 	if (lrc->lrc_txtype == TX_WRITE) {
53212296SLin.Ling@Sun.COM 		zil_scan_arg_t *zsa = arg;
53312296SLin.Ling@Sun.COM 		dsl_pool_t *dp = zsa->zsa_dp;
53412296SLin.Ling@Sun.COM 		dsl_scan_t *scn = dp->dp_scan;
53512296SLin.Ling@Sun.COM 		zil_header_t *zh = zsa->zsa_zh;
53612296SLin.Ling@Sun.COM 		lr_write_t *lr = (lr_write_t *)lrc;
53712296SLin.Ling@Sun.COM 		blkptr_t *bp = &lr->lr_blkptr;
53812296SLin.Ling@Sun.COM 		zbookmark_t zb;
53912296SLin.Ling@Sun.COM 
54012296SLin.Ling@Sun.COM 		if (bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
54112296SLin.Ling@Sun.COM 			return (0);
54212296SLin.Ling@Sun.COM 
54312296SLin.Ling@Sun.COM 		/*
54412296SLin.Ling@Sun.COM 		 * birth can be < claim_txg if this record's txg is
54512296SLin.Ling@Sun.COM 		 * already txg sync'ed (but this log block contains
54612296SLin.Ling@Sun.COM 		 * other records that are not synced)
54712296SLin.Ling@Sun.COM 		 */
54812296SLin.Ling@Sun.COM 		if (claim_txg == 0 || bp->blk_birth < claim_txg)
54912296SLin.Ling@Sun.COM 			return (0);
55012296SLin.Ling@Sun.COM 
55112296SLin.Ling@Sun.COM 		SET_BOOKMARK(&zb, zh->zh_log.blk_cksum.zc_word[ZIL_ZC_OBJSET],
55212296SLin.Ling@Sun.COM 		    lr->lr_foid, ZB_ZIL_LEVEL,
55312296SLin.Ling@Sun.COM 		    lr->lr_offset / BP_GET_LSIZE(bp));
55412296SLin.Ling@Sun.COM 
55512296SLin.Ling@Sun.COM 		VERIFY(0 == scan_funcs[scn->scn_phys.scn_func](dp, bp, &zb));
55612296SLin.Ling@Sun.COM 	}
55712296SLin.Ling@Sun.COM 	return (0);
55812296SLin.Ling@Sun.COM }
55912296SLin.Ling@Sun.COM 
56012296SLin.Ling@Sun.COM static void
56112296SLin.Ling@Sun.COM dsl_scan_zil(dsl_pool_t *dp, zil_header_t *zh)
56212296SLin.Ling@Sun.COM {
56312296SLin.Ling@Sun.COM 	uint64_t claim_txg = zh->zh_claim_txg;
56412296SLin.Ling@Sun.COM 	zil_scan_arg_t zsa = { dp, zh };
56512296SLin.Ling@Sun.COM 	zilog_t *zilog;
56612296SLin.Ling@Sun.COM 
56712296SLin.Ling@Sun.COM 	/*
56812296SLin.Ling@Sun.COM 	 * We only want to visit blocks that have been claimed but not yet
56912296SLin.Ling@Sun.COM 	 * replayed (or, in read-only mode, blocks that *would* be claimed).
57012296SLin.Ling@Sun.COM 	 */
57112296SLin.Ling@Sun.COM 	if (claim_txg == 0 && spa_writeable(dp->dp_spa))
57212296SLin.Ling@Sun.COM 		return;
57312296SLin.Ling@Sun.COM 
57412296SLin.Ling@Sun.COM 	zilog = zil_alloc(dp->dp_meta_objset, zh);
57512296SLin.Ling@Sun.COM 
57612296SLin.Ling@Sun.COM 	(void) zil_parse(zilog, dsl_scan_zil_block, dsl_scan_zil_record, &zsa,
57712296SLin.Ling@Sun.COM 	    claim_txg);
57812296SLin.Ling@Sun.COM 
57912296SLin.Ling@Sun.COM 	zil_free(zilog);
58012296SLin.Ling@Sun.COM }
58112296SLin.Ling@Sun.COM 
58212296SLin.Ling@Sun.COM /* ARGSUSED */
58312296SLin.Ling@Sun.COM static void
58412296SLin.Ling@Sun.COM dsl_scan_prefetch(dsl_scan_t *scn, arc_buf_t *buf, blkptr_t *bp,
58512296SLin.Ling@Sun.COM     uint64_t objset, uint64_t object, uint64_t blkid)
58612296SLin.Ling@Sun.COM {
58712296SLin.Ling@Sun.COM 	zbookmark_t czb;
58812296SLin.Ling@Sun.COM 	uint32_t flags = ARC_NOWAIT | ARC_PREFETCH;
58912296SLin.Ling@Sun.COM 
59012296SLin.Ling@Sun.COM 	if (zfs_no_scrub_prefetch)
59112296SLin.Ling@Sun.COM 		return;
59212296SLin.Ling@Sun.COM 
59312296SLin.Ling@Sun.COM 	if (BP_IS_HOLE(bp) || bp->blk_birth <= scn->scn_phys.scn_min_txg ||
59412296SLin.Ling@Sun.COM 	    (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_DNODE))
59512296SLin.Ling@Sun.COM 		return;
59612296SLin.Ling@Sun.COM 
59712296SLin.Ling@Sun.COM 	SET_BOOKMARK(&czb, objset, object, BP_GET_LEVEL(bp), blkid);
59812296SLin.Ling@Sun.COM 
59912296SLin.Ling@Sun.COM 	/*
60012296SLin.Ling@Sun.COM 	 * XXX need to make sure all of these arc_read() prefetches are
60112296SLin.Ling@Sun.COM 	 * done before setting xlateall (similar to dsl_read())
60212296SLin.Ling@Sun.COM 	 */
603*12470SMatthew.Ahrens@Sun.COM 	(void) arc_read(scn->scn_zio_root, scn->scn_dp->dp_spa, bp,
60412296SLin.Ling@Sun.COM 	    buf, NULL, NULL, ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
60512296SLin.Ling@Sun.COM 	    &flags, &czb);
60612296SLin.Ling@Sun.COM }
60712296SLin.Ling@Sun.COM 
60812296SLin.Ling@Sun.COM static boolean_t
60912296SLin.Ling@Sun.COM dsl_scan_check_resume(dsl_scan_t *scn, const dnode_phys_t *dnp,
61012296SLin.Ling@Sun.COM     const zbookmark_t *zb)
61112296SLin.Ling@Sun.COM {
61212296SLin.Ling@Sun.COM 	/*
61312296SLin.Ling@Sun.COM 	 * We never skip over user/group accounting objects (obj<0)
61412296SLin.Ling@Sun.COM 	 */
61512296SLin.Ling@Sun.COM 	if (!bookmark_is_zero(&scn->scn_phys.scn_bookmark) &&
61612296SLin.Ling@Sun.COM 	    (int64_t)zb->zb_object >= 0) {
61712296SLin.Ling@Sun.COM 		/*
61812296SLin.Ling@Sun.COM 		 * If we already visited this bp & everything below (in
61912296SLin.Ling@Sun.COM 		 * a prior txg sync), don't bother doing it again.
62012296SLin.Ling@Sun.COM 		 */
62112296SLin.Ling@Sun.COM 		if (bookmark_is_before(dnp, zb, &scn->scn_phys.scn_bookmark))
62212296SLin.Ling@Sun.COM 			return (B_TRUE);
62312296SLin.Ling@Sun.COM 
62412296SLin.Ling@Sun.COM 		/*
62512296SLin.Ling@Sun.COM 		 * If we found the block we're trying to resume from, or
62612296SLin.Ling@Sun.COM 		 * we went past it to a different object, zero it out to
62712296SLin.Ling@Sun.COM 		 * indicate that it's OK to start checking for pausing
62812296SLin.Ling@Sun.COM 		 * again.
62912296SLin.Ling@Sun.COM 		 */
63012296SLin.Ling@Sun.COM 		if (bcmp(zb, &scn->scn_phys.scn_bookmark, sizeof (*zb)) == 0 ||
63112296SLin.Ling@Sun.COM 		    zb->zb_object > scn->scn_phys.scn_bookmark.zb_object) {
63212296SLin.Ling@Sun.COM 			dprintf("resuming at %llx/%llx/%llx/%llx\n",
63312296SLin.Ling@Sun.COM 			    (longlong_t)zb->zb_objset,
63412296SLin.Ling@Sun.COM 			    (longlong_t)zb->zb_object,
63512296SLin.Ling@Sun.COM 			    (longlong_t)zb->zb_level,
63612296SLin.Ling@Sun.COM 			    (longlong_t)zb->zb_blkid);
63712296SLin.Ling@Sun.COM 			bzero(&scn->scn_phys.scn_bookmark, sizeof (*zb));
63812296SLin.Ling@Sun.COM 		}
63912296SLin.Ling@Sun.COM 	}
64012296SLin.Ling@Sun.COM 	return (B_FALSE);
64112296SLin.Ling@Sun.COM }
64212296SLin.Ling@Sun.COM 
64312296SLin.Ling@Sun.COM /*
64412296SLin.Ling@Sun.COM  * Return nonzero on i/o error.
64512296SLin.Ling@Sun.COM  * Return new buf to write out in *bufp.
64612296SLin.Ling@Sun.COM  */
64712296SLin.Ling@Sun.COM static int
64812296SLin.Ling@Sun.COM dsl_scan_recurse(dsl_scan_t *scn, dsl_dataset_t *ds, dmu_objset_type_t ostype,
64912296SLin.Ling@Sun.COM     dnode_phys_t *dnp, const blkptr_t *bp,
65012296SLin.Ling@Sun.COM     const zbookmark_t *zb, dmu_tx_t *tx, arc_buf_t **bufp)
65112296SLin.Ling@Sun.COM {
65212296SLin.Ling@Sun.COM 	dsl_pool_t *dp = scn->scn_dp;
65312296SLin.Ling@Sun.COM 	int err;
65412296SLin.Ling@Sun.COM 
65512296SLin.Ling@Sun.COM 	if (BP_GET_LEVEL(bp) > 0) {
65612296SLin.Ling@Sun.COM 		uint32_t flags = ARC_WAIT;
65712296SLin.Ling@Sun.COM 		int i;
65812296SLin.Ling@Sun.COM 		blkptr_t *cbp;
65912296SLin.Ling@Sun.COM 		int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
66012296SLin.Ling@Sun.COM 
66112296SLin.Ling@Sun.COM 		err = arc_read_nolock(NULL, dp->dp_spa, bp,
66212296SLin.Ling@Sun.COM 		    arc_getbuf_func, bufp,
66312296SLin.Ling@Sun.COM 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
66412296SLin.Ling@Sun.COM 		if (err) {
66512296SLin.Ling@Sun.COM 			scn->scn_phys.scn_errors++;
66612296SLin.Ling@Sun.COM 			return (err);
66712296SLin.Ling@Sun.COM 		}
66812296SLin.Ling@Sun.COM 		for (i = 0, cbp = (*bufp)->b_data; i < epb; i++, cbp++) {
66912296SLin.Ling@Sun.COM 			dsl_scan_prefetch(scn, *bufp, cbp, zb->zb_objset,
67012296SLin.Ling@Sun.COM 			    zb->zb_object, zb->zb_blkid * epb + i);
67112296SLin.Ling@Sun.COM 		}
67212296SLin.Ling@Sun.COM 		for (i = 0, cbp = (*bufp)->b_data; i < epb; i++, cbp++) {
67312296SLin.Ling@Sun.COM 			zbookmark_t czb;
67412296SLin.Ling@Sun.COM 
67512296SLin.Ling@Sun.COM 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
67612296SLin.Ling@Sun.COM 			    zb->zb_level - 1,
67712296SLin.Ling@Sun.COM 			    zb->zb_blkid * epb + i);
67812296SLin.Ling@Sun.COM 			dsl_scan_visitbp(cbp, &czb, dnp,
67912296SLin.Ling@Sun.COM 			    *bufp, ds, scn, ostype, tx);
68012296SLin.Ling@Sun.COM 		}
68112296SLin.Ling@Sun.COM 	} else if (BP_GET_TYPE(bp) == DMU_OT_USERGROUP_USED) {
68212296SLin.Ling@Sun.COM 		uint32_t flags = ARC_WAIT;
68312296SLin.Ling@Sun.COM 
68412296SLin.Ling@Sun.COM 		err = arc_read_nolock(NULL, dp->dp_spa, bp,
68512296SLin.Ling@Sun.COM 		    arc_getbuf_func, bufp,
68612296SLin.Ling@Sun.COM 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
68712296SLin.Ling@Sun.COM 		if (err) {
68812296SLin.Ling@Sun.COM 			scn->scn_phys.scn_errors++;
68912296SLin.Ling@Sun.COM 			return (err);
69012296SLin.Ling@Sun.COM 		}
69112296SLin.Ling@Sun.COM 	} else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) {
69212296SLin.Ling@Sun.COM 		uint32_t flags = ARC_WAIT;
69312296SLin.Ling@Sun.COM 		dnode_phys_t *cdnp;
69412296SLin.Ling@Sun.COM 		int i, j;
69512296SLin.Ling@Sun.COM 		int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
69612296SLin.Ling@Sun.COM 
69712296SLin.Ling@Sun.COM 		err = arc_read_nolock(NULL, dp->dp_spa, bp,
69812296SLin.Ling@Sun.COM 		    arc_getbuf_func, bufp,
69912296SLin.Ling@Sun.COM 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
70012296SLin.Ling@Sun.COM 		if (err) {
70112296SLin.Ling@Sun.COM 			scn->scn_phys.scn_errors++;
70212296SLin.Ling@Sun.COM 			return (err);
70312296SLin.Ling@Sun.COM 		}
70412296SLin.Ling@Sun.COM 		for (i = 0, cdnp = (*bufp)->b_data; i < epb; i++, cdnp++) {
70512296SLin.Ling@Sun.COM 			for (j = 0; j < cdnp->dn_nblkptr; j++) {
70612296SLin.Ling@Sun.COM 				blkptr_t *cbp = &cdnp->dn_blkptr[j];
70712296SLin.Ling@Sun.COM 				dsl_scan_prefetch(scn, *bufp, cbp,
70812296SLin.Ling@Sun.COM 				    zb->zb_objset, zb->zb_blkid * epb + i, j);
70912296SLin.Ling@Sun.COM 			}
71012296SLin.Ling@Sun.COM 		}
71112296SLin.Ling@Sun.COM 		for (i = 0, cdnp = (*bufp)->b_data; i < epb; i++, cdnp++) {
71212296SLin.Ling@Sun.COM 			dsl_scan_visitdnode(scn, ds, ostype,
71312296SLin.Ling@Sun.COM 			    cdnp, *bufp, zb->zb_blkid * epb + i, tx);
71412296SLin.Ling@Sun.COM 		}
71512296SLin.Ling@Sun.COM 
71612296SLin.Ling@Sun.COM 	} else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) {
71712296SLin.Ling@Sun.COM 		uint32_t flags = ARC_WAIT;
71812296SLin.Ling@Sun.COM 		objset_phys_t *osp;
71912296SLin.Ling@Sun.COM 
72012296SLin.Ling@Sun.COM 		err = arc_read_nolock(NULL, dp->dp_spa, bp,
72112296SLin.Ling@Sun.COM 		    arc_getbuf_func, bufp,
72212296SLin.Ling@Sun.COM 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
72312296SLin.Ling@Sun.COM 		if (err) {
72412296SLin.Ling@Sun.COM 			scn->scn_phys.scn_errors++;
72512296SLin.Ling@Sun.COM 			return (err);
72612296SLin.Ling@Sun.COM 		}
72712296SLin.Ling@Sun.COM 
72812296SLin.Ling@Sun.COM 		osp = (*bufp)->b_data;
72912296SLin.Ling@Sun.COM 
73012296SLin.Ling@Sun.COM 		if (DSL_SCAN_IS_SCRUB_RESILVER(scn))
73112296SLin.Ling@Sun.COM 			dsl_scan_zil(dp, &osp->os_zil_header);
73212296SLin.Ling@Sun.COM 
73312296SLin.Ling@Sun.COM 		dsl_scan_visitdnode(scn, ds, osp->os_type,
73412296SLin.Ling@Sun.COM 		    &osp->os_meta_dnode, *bufp, DMU_META_DNODE_OBJECT, tx);
73512296SLin.Ling@Sun.COM 
73612296SLin.Ling@Sun.COM 		if (OBJSET_BUF_HAS_USERUSED(*bufp)) {
73712296SLin.Ling@Sun.COM 			/*
73812296SLin.Ling@Sun.COM 			 * We also always visit user/group accounting
73912296SLin.Ling@Sun.COM 			 * objects, and never skip them, even if we are
74012296SLin.Ling@Sun.COM 			 * pausing.  This is necessary so that the space
74112296SLin.Ling@Sun.COM 			 * deltas from this txg get integrated.
74212296SLin.Ling@Sun.COM 			 */
74312296SLin.Ling@Sun.COM 			dsl_scan_visitdnode(scn, ds, osp->os_type,
74412296SLin.Ling@Sun.COM 			    &osp->os_groupused_dnode, *bufp,
74512296SLin.Ling@Sun.COM 			    DMU_GROUPUSED_OBJECT, tx);
74612296SLin.Ling@Sun.COM 			dsl_scan_visitdnode(scn, ds, osp->os_type,
74712296SLin.Ling@Sun.COM 			    &osp->os_userused_dnode, *bufp,
74812296SLin.Ling@Sun.COM 			    DMU_USERUSED_OBJECT, tx);
74912296SLin.Ling@Sun.COM 		}
75012296SLin.Ling@Sun.COM 	}
75112296SLin.Ling@Sun.COM 
75212296SLin.Ling@Sun.COM 	return (0);
75312296SLin.Ling@Sun.COM }
75412296SLin.Ling@Sun.COM 
75512296SLin.Ling@Sun.COM static void
75612296SLin.Ling@Sun.COM dsl_scan_visitdnode(dsl_scan_t *scn, dsl_dataset_t *ds,
75712296SLin.Ling@Sun.COM     dmu_objset_type_t ostype, dnode_phys_t *dnp, arc_buf_t *buf,
75812296SLin.Ling@Sun.COM     uint64_t object, dmu_tx_t *tx)
75912296SLin.Ling@Sun.COM {
76012296SLin.Ling@Sun.COM 	int j;
76112296SLin.Ling@Sun.COM 
76212296SLin.Ling@Sun.COM 	for (j = 0; j < dnp->dn_nblkptr; j++) {
76312296SLin.Ling@Sun.COM 		zbookmark_t czb;
76412296SLin.Ling@Sun.COM 
76512296SLin.Ling@Sun.COM 		SET_BOOKMARK(&czb, ds ? ds->ds_object : 0, object,
76612296SLin.Ling@Sun.COM 		    dnp->dn_nlevels - 1, j);
76712296SLin.Ling@Sun.COM 		dsl_scan_visitbp(&dnp->dn_blkptr[j],
76812296SLin.Ling@Sun.COM 		    &czb, dnp, buf, ds, scn, ostype, tx);
76912296SLin.Ling@Sun.COM 	}
77012296SLin.Ling@Sun.COM 
77112296SLin.Ling@Sun.COM 	if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
77212296SLin.Ling@Sun.COM 		zbookmark_t czb;
77312296SLin.Ling@Sun.COM 		SET_BOOKMARK(&czb, ds ? ds->ds_object : 0, object,
77412296SLin.Ling@Sun.COM 		    0, DMU_SPILL_BLKID);
77512296SLin.Ling@Sun.COM 		dsl_scan_visitbp(&dnp->dn_spill,
77612296SLin.Ling@Sun.COM 		    &czb, dnp, buf, ds, scn, ostype, tx);
77712296SLin.Ling@Sun.COM 	}
77812296SLin.Ling@Sun.COM }
77912296SLin.Ling@Sun.COM 
78012296SLin.Ling@Sun.COM /*
78112296SLin.Ling@Sun.COM  * The arguments are in this order because mdb can only print the
78212296SLin.Ling@Sun.COM  * first 5; we want them to be useful.
78312296SLin.Ling@Sun.COM  */
78412296SLin.Ling@Sun.COM static void
78512296SLin.Ling@Sun.COM dsl_scan_visitbp(blkptr_t *bp, const zbookmark_t *zb,
78612296SLin.Ling@Sun.COM     dnode_phys_t *dnp, arc_buf_t *pbuf,
78712296SLin.Ling@Sun.COM     dsl_dataset_t *ds, dsl_scan_t *scn, dmu_objset_type_t ostype,
78812296SLin.Ling@Sun.COM     dmu_tx_t *tx)
78912296SLin.Ling@Sun.COM {
79012296SLin.Ling@Sun.COM 	dsl_pool_t *dp = scn->scn_dp;
79112296SLin.Ling@Sun.COM 	arc_buf_t *buf = NULL;
79212296SLin.Ling@Sun.COM 	blkptr_t bp_toread = *bp;
79312296SLin.Ling@Sun.COM 
79412296SLin.Ling@Sun.COM 	/* ASSERT(pbuf == NULL || arc_released(pbuf)); */
79512296SLin.Ling@Sun.COM 
79612296SLin.Ling@Sun.COM 	if (dsl_scan_check_pause(scn, zb))
79712296SLin.Ling@Sun.COM 		return;
79812296SLin.Ling@Sun.COM 
79912296SLin.Ling@Sun.COM 	if (dsl_scan_check_resume(scn, dnp, zb))
80012296SLin.Ling@Sun.COM 		return;
80112296SLin.Ling@Sun.COM 
80212296SLin.Ling@Sun.COM 	if (bp->blk_birth == 0)
80312296SLin.Ling@Sun.COM 		return;
80412296SLin.Ling@Sun.COM 
80512296SLin.Ling@Sun.COM 	scn->scn_visited_this_txg++;
80612296SLin.Ling@Sun.COM 
80712296SLin.Ling@Sun.COM 	dprintf_bp(bp,
80812296SLin.Ling@Sun.COM 	    "visiting ds=%p/%llu zb=%llx/%llx/%llx/%llx buf=%p bp=%p",
80912296SLin.Ling@Sun.COM 	    ds, ds ? ds->ds_object : 0,
81012296SLin.Ling@Sun.COM 	    zb->zb_objset, zb->zb_object, zb->zb_level, zb->zb_blkid,
81112296SLin.Ling@Sun.COM 	    pbuf, bp);
81212296SLin.Ling@Sun.COM 
81312296SLin.Ling@Sun.COM 	if (bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
81412296SLin.Ling@Sun.COM 		return;
81512296SLin.Ling@Sun.COM 
81612296SLin.Ling@Sun.COM 	if (BP_GET_TYPE(bp) != DMU_OT_USERGROUP_USED) {
81712296SLin.Ling@Sun.COM 		/*
81812296SLin.Ling@Sun.COM 		 * For non-user-accounting blocks, we need to read the
81912296SLin.Ling@Sun.COM 		 * new bp (from a deleted snapshot, found in
82012296SLin.Ling@Sun.COM 		 * check_existing_xlation).  If we used the old bp,
82112296SLin.Ling@Sun.COM 		 * pointers inside this block from before we resumed
82212296SLin.Ling@Sun.COM 		 * would be untranslated.
82312296SLin.Ling@Sun.COM 		 *
82412296SLin.Ling@Sun.COM 		 * For user-accounting blocks, we need to read the old
82512296SLin.Ling@Sun.COM 		 * bp, because we will apply the entire space delta to
82612296SLin.Ling@Sun.COM 		 * it (original untranslated -> translations from
82712296SLin.Ling@Sun.COM 		 * deleted snap -> now).
82812296SLin.Ling@Sun.COM 		 */
82912296SLin.Ling@Sun.COM 		bp_toread = *bp;
83012296SLin.Ling@Sun.COM 	}
83112296SLin.Ling@Sun.COM 
83212296SLin.Ling@Sun.COM 	if (dsl_scan_recurse(scn, ds, ostype, dnp, &bp_toread, zb, tx,
83312296SLin.Ling@Sun.COM 	    &buf) != 0)
83412296SLin.Ling@Sun.COM 		return;
83512296SLin.Ling@Sun.COM 
83612296SLin.Ling@Sun.COM 	/*
83712296SLin.Ling@Sun.COM 	 * If dsl_scan_ddt() has aready visited this block, it will have
83812296SLin.Ling@Sun.COM 	 * already done any translations or scrubbing, so don't call the
83912296SLin.Ling@Sun.COM 	 * callback again.
84012296SLin.Ling@Sun.COM 	 */
84112296SLin.Ling@Sun.COM 	if (ddt_class_contains(dp->dp_spa,
84212296SLin.Ling@Sun.COM 	    scn->scn_phys.scn_ddt_class_max, bp)) {
84312296SLin.Ling@Sun.COM 		ASSERT(buf == NULL);
84412296SLin.Ling@Sun.COM 		return;
84512296SLin.Ling@Sun.COM 	}
84612296SLin.Ling@Sun.COM 
84712296SLin.Ling@Sun.COM 	/*
84812296SLin.Ling@Sun.COM 	 * If this block is from the future (after cur_max_txg), then we
84912296SLin.Ling@Sun.COM 	 * are doing this on behalf of a deleted snapshot, and we will
85012296SLin.Ling@Sun.COM 	 * revisit the future block on the next pass of this dataset.
85112296SLin.Ling@Sun.COM 	 * Don't scan it now unless we need to because something
85212296SLin.Ling@Sun.COM 	 * under it was modified.
85312296SLin.Ling@Sun.COM 	 */
85412296SLin.Ling@Sun.COM 	if (bp->blk_birth <= scn->scn_phys.scn_cur_max_txg) {
85512296SLin.Ling@Sun.COM 		scan_funcs[scn->scn_phys.scn_func](dp, bp, zb);
85612296SLin.Ling@Sun.COM 	}
85712296SLin.Ling@Sun.COM 	if (buf)
85812296SLin.Ling@Sun.COM 		(void) arc_buf_remove_ref(buf, &buf);
85912296SLin.Ling@Sun.COM }
86012296SLin.Ling@Sun.COM 
86112296SLin.Ling@Sun.COM static void
86212296SLin.Ling@Sun.COM dsl_scan_visit_rootbp(dsl_scan_t *scn, dsl_dataset_t *ds, blkptr_t *bp,
86312296SLin.Ling@Sun.COM     dmu_tx_t *tx)
86412296SLin.Ling@Sun.COM {
86512296SLin.Ling@Sun.COM 	zbookmark_t zb;
86612296SLin.Ling@Sun.COM 
86712296SLin.Ling@Sun.COM 	SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
86812296SLin.Ling@Sun.COM 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
86912296SLin.Ling@Sun.COM 	dsl_scan_visitbp(bp, &zb, NULL, NULL,
87012296SLin.Ling@Sun.COM 	    ds, scn, DMU_OST_NONE, tx);
87112296SLin.Ling@Sun.COM 
87212296SLin.Ling@Sun.COM 	dprintf_ds(ds, "finished scan%s", "");
87312296SLin.Ling@Sun.COM }
87412296SLin.Ling@Sun.COM 
87512296SLin.Ling@Sun.COM void
87612296SLin.Ling@Sun.COM dsl_scan_ds_destroyed(dsl_dataset_t *ds, dmu_tx_t *tx)
87712296SLin.Ling@Sun.COM {
87812296SLin.Ling@Sun.COM 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
87912296SLin.Ling@Sun.COM 	dsl_scan_t *scn = dp->dp_scan;
88012296SLin.Ling@Sun.COM 	uint64_t mintxg;
88112296SLin.Ling@Sun.COM 
88212296SLin.Ling@Sun.COM 	if (scn->scn_phys.scn_state != DSS_SCANNING)
88312296SLin.Ling@Sun.COM 		return;
88412296SLin.Ling@Sun.COM 
88512296SLin.Ling@Sun.COM 	if (scn->scn_phys.scn_bookmark.zb_objset == ds->ds_object) {
88612296SLin.Ling@Sun.COM 		if (dsl_dataset_is_snapshot(ds)) {
88712296SLin.Ling@Sun.COM 			/* Note, scn_cur_{min,max}_txg stays the same. */
88812296SLin.Ling@Sun.COM 			scn->scn_phys.scn_bookmark.zb_objset =
88912296SLin.Ling@Sun.COM 			    ds->ds_phys->ds_next_snap_obj;
89012296SLin.Ling@Sun.COM 			zfs_dbgmsg("destroying ds %llu; currently traversing; "
89112296SLin.Ling@Sun.COM 			    "reset zb_objset to %llu",
89212296SLin.Ling@Sun.COM 			    (u_longlong_t)ds->ds_object,
89312296SLin.Ling@Sun.COM 			    (u_longlong_t)ds->ds_phys->ds_next_snap_obj);
89412296SLin.Ling@Sun.COM 			scn->scn_phys.scn_flags |= DSF_VISIT_DS_AGAIN;
89512296SLin.Ling@Sun.COM 		} else {
89612296SLin.Ling@Sun.COM 			SET_BOOKMARK(&scn->scn_phys.scn_bookmark,
89712296SLin.Ling@Sun.COM 			    ZB_DESTROYED_OBJSET, 0, 0, 0);
89812296SLin.Ling@Sun.COM 			zfs_dbgmsg("destroying ds %llu; currently traversing; "
89912296SLin.Ling@Sun.COM 			    "reset bookmark to -1,0,0,0",
90012296SLin.Ling@Sun.COM 			    (u_longlong_t)ds->ds_object);
90112296SLin.Ling@Sun.COM 		}
90212296SLin.Ling@Sun.COM 	} else if (zap_lookup_int_key(dp->dp_meta_objset,
90312296SLin.Ling@Sun.COM 	    scn->scn_phys.scn_queue_obj, ds->ds_object, &mintxg) == 0) {
90412296SLin.Ling@Sun.COM 		ASSERT3U(ds->ds_phys->ds_num_children, <=, 1);
90512296SLin.Ling@Sun.COM 		VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
90612296SLin.Ling@Sun.COM 		    scn->scn_phys.scn_queue_obj, ds->ds_object, tx));
90712296SLin.Ling@Sun.COM 		if (dsl_dataset_is_snapshot(ds)) {
90812296SLin.Ling@Sun.COM 			/*
90912296SLin.Ling@Sun.COM 			 * We keep the same mintxg; it could be >
91012296SLin.Ling@Sun.COM 			 * ds_creation_txg if the previous snapshot was
91112296SLin.Ling@Sun.COM 			 * deleted too.
91212296SLin.Ling@Sun.COM 			 */
91312296SLin.Ling@Sun.COM 			VERIFY(zap_add_int_key(dp->dp_meta_objset,
91412296SLin.Ling@Sun.COM 			    scn->scn_phys.scn_queue_obj,
91512296SLin.Ling@Sun.COM 			    ds->ds_phys->ds_next_snap_obj, mintxg, tx) == 0);
91612296SLin.Ling@Sun.COM 			zfs_dbgmsg("destroying ds %llu; in queue; "
91712296SLin.Ling@Sun.COM 			    "replacing with %llu",
91812296SLin.Ling@Sun.COM 			    (u_longlong_t)ds->ds_object,
91912296SLin.Ling@Sun.COM 			    (u_longlong_t)ds->ds_phys->ds_next_snap_obj);
92012296SLin.Ling@Sun.COM 		} else {
92112296SLin.Ling@Sun.COM 			zfs_dbgmsg("destroying ds %llu; in queue; removing",
92212296SLin.Ling@Sun.COM 			    (u_longlong_t)ds->ds_object);
92312296SLin.Ling@Sun.COM 		}
92412296SLin.Ling@Sun.COM 	} else {
92512296SLin.Ling@Sun.COM 		zfs_dbgmsg("destroying ds %llu; ignoring",
92612296SLin.Ling@Sun.COM 		    (u_longlong_t)ds->ds_object);
92712296SLin.Ling@Sun.COM 	}
92812296SLin.Ling@Sun.COM 
92912296SLin.Ling@Sun.COM 	/*
93012296SLin.Ling@Sun.COM 	 * dsl_scan_sync() should be called after this, and should sync
93112296SLin.Ling@Sun.COM 	 * out our changed state, but just to be safe, do it here.
93212296SLin.Ling@Sun.COM 	 */
93312296SLin.Ling@Sun.COM 	dsl_scan_sync_state(scn, tx);
93412296SLin.Ling@Sun.COM }
93512296SLin.Ling@Sun.COM 
93612296SLin.Ling@Sun.COM void
93712296SLin.Ling@Sun.COM dsl_scan_ds_snapshotted(dsl_dataset_t *ds, dmu_tx_t *tx)
93812296SLin.Ling@Sun.COM {
93912296SLin.Ling@Sun.COM 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
94012296SLin.Ling@Sun.COM 	dsl_scan_t *scn = dp->dp_scan;
94112296SLin.Ling@Sun.COM 	uint64_t mintxg;
94212296SLin.Ling@Sun.COM 
94312296SLin.Ling@Sun.COM 	if (scn->scn_phys.scn_state != DSS_SCANNING)
94412296SLin.Ling@Sun.COM 		return;
94512296SLin.Ling@Sun.COM 
94612296SLin.Ling@Sun.COM 	ASSERT(ds->ds_phys->ds_prev_snap_obj != 0);
94712296SLin.Ling@Sun.COM 
94812296SLin.Ling@Sun.COM 	if (scn->scn_phys.scn_bookmark.zb_objset == ds->ds_object) {
94912296SLin.Ling@Sun.COM 		scn->scn_phys.scn_bookmark.zb_objset =
95012296SLin.Ling@Sun.COM 		    ds->ds_phys->ds_prev_snap_obj;
95112296SLin.Ling@Sun.COM 		zfs_dbgmsg("snapshotting ds %llu; currently traversing; "
95212296SLin.Ling@Sun.COM 		    "reset zb_objset to %llu",
95312296SLin.Ling@Sun.COM 		    (u_longlong_t)ds->ds_object,
95412296SLin.Ling@Sun.COM 		    (u_longlong_t)ds->ds_phys->ds_prev_snap_obj);
95512296SLin.Ling@Sun.COM 	} else if (zap_lookup_int_key(dp->dp_meta_objset,
95612296SLin.Ling@Sun.COM 	    scn->scn_phys.scn_queue_obj, ds->ds_object, &mintxg) == 0) {
95712296SLin.Ling@Sun.COM 		VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
95812296SLin.Ling@Sun.COM 		    scn->scn_phys.scn_queue_obj, ds->ds_object, tx));
95912296SLin.Ling@Sun.COM 		VERIFY(zap_add_int_key(dp->dp_meta_objset,
96012296SLin.Ling@Sun.COM 		    scn->scn_phys.scn_queue_obj,
96112296SLin.Ling@Sun.COM 		    ds->ds_phys->ds_prev_snap_obj, mintxg, tx) == 0);
96212296SLin.Ling@Sun.COM 		zfs_dbgmsg("snapshotting ds %llu; in queue; "
96312296SLin.Ling@Sun.COM 		    "replacing with %llu",
96412296SLin.Ling@Sun.COM 		    (u_longlong_t)ds->ds_object,
96512296SLin.Ling@Sun.COM 		    (u_longlong_t)ds->ds_phys->ds_prev_snap_obj);
96612296SLin.Ling@Sun.COM 	}
96712296SLin.Ling@Sun.COM 	dsl_scan_sync_state(scn, tx);
96812296SLin.Ling@Sun.COM }
96912296SLin.Ling@Sun.COM 
97012296SLin.Ling@Sun.COM void
97112296SLin.Ling@Sun.COM dsl_scan_ds_clone_swapped(dsl_dataset_t *ds1, dsl_dataset_t *ds2, dmu_tx_t *tx)
97212296SLin.Ling@Sun.COM {
97312296SLin.Ling@Sun.COM 	dsl_pool_t *dp = ds1->ds_dir->dd_pool;
97412296SLin.Ling@Sun.COM 	dsl_scan_t *scn = dp->dp_scan;
97512296SLin.Ling@Sun.COM 	uint64_t mintxg;
97612296SLin.Ling@Sun.COM 
97712296SLin.Ling@Sun.COM 	if (scn->scn_phys.scn_state != DSS_SCANNING)
97812296SLin.Ling@Sun.COM 		return;
97912296SLin.Ling@Sun.COM 
98012296SLin.Ling@Sun.COM 	if (scn->scn_phys.scn_bookmark.zb_objset == ds1->ds_object) {
98112296SLin.Ling@Sun.COM 		scn->scn_phys.scn_bookmark.zb_objset = ds2->ds_object;
98212296SLin.Ling@Sun.COM 		zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
98312296SLin.Ling@Sun.COM 		    "reset zb_objset to %llu",
98412296SLin.Ling@Sun.COM 		    (u_longlong_t)ds1->ds_object,
98512296SLin.Ling@Sun.COM 		    (u_longlong_t)ds2->ds_object);
98612296SLin.Ling@Sun.COM 	} else if (scn->scn_phys.scn_bookmark.zb_objset == ds2->ds_object) {
98712296SLin.Ling@Sun.COM 		scn->scn_phys.scn_bookmark.zb_objset = ds1->ds_object;
98812296SLin.Ling@Sun.COM 		zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
98912296SLin.Ling@Sun.COM 		    "reset zb_objset to %llu",
99012296SLin.Ling@Sun.COM 		    (u_longlong_t)ds2->ds_object,
99112296SLin.Ling@Sun.COM 		    (u_longlong_t)ds1->ds_object);
99212296SLin.Ling@Sun.COM 	}
99312296SLin.Ling@Sun.COM 
99412296SLin.Ling@Sun.COM 	if (zap_lookup_int_key(dp->dp_meta_objset, scn->scn_phys.scn_queue_obj,
99512296SLin.Ling@Sun.COM 	    ds1->ds_object, &mintxg) == 0) {
99612296SLin.Ling@Sun.COM 		int err;
99712296SLin.Ling@Sun.COM 
99812296SLin.Ling@Sun.COM 		ASSERT3U(mintxg, ==, ds1->ds_phys->ds_prev_snap_txg);
99912296SLin.Ling@Sun.COM 		ASSERT3U(mintxg, ==, ds2->ds_phys->ds_prev_snap_txg);
100012296SLin.Ling@Sun.COM 		VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
100112296SLin.Ling@Sun.COM 		    scn->scn_phys.scn_queue_obj, ds1->ds_object, tx));
100212296SLin.Ling@Sun.COM 		err = zap_add_int_key(dp->dp_meta_objset,
100312296SLin.Ling@Sun.COM 		    scn->scn_phys.scn_queue_obj, ds2->ds_object, mintxg, tx);
100412296SLin.Ling@Sun.COM 		VERIFY(err == 0 || err == EEXIST);
100512296SLin.Ling@Sun.COM 		if (err == EEXIST) {
100612296SLin.Ling@Sun.COM 			/* Both were there to begin with */
100712296SLin.Ling@Sun.COM 			VERIFY(0 == zap_add_int_key(dp->dp_meta_objset,
100812296SLin.Ling@Sun.COM 			    scn->scn_phys.scn_queue_obj,
100912296SLin.Ling@Sun.COM 			    ds1->ds_object, mintxg, tx));
101012296SLin.Ling@Sun.COM 		}
101112296SLin.Ling@Sun.COM 		zfs_dbgmsg("clone_swap ds %llu; in queue; "
101212296SLin.Ling@Sun.COM 		    "replacing with %llu",
101312296SLin.Ling@Sun.COM 		    (u_longlong_t)ds1->ds_object,
101412296SLin.Ling@Sun.COM 		    (u_longlong_t)ds2->ds_object);
101512296SLin.Ling@Sun.COM 	} else if (zap_lookup_int_key(dp->dp_meta_objset,
101612296SLin.Ling@Sun.COM 	    scn->scn_phys.scn_queue_obj, ds2->ds_object, &mintxg) == 0) {
101712296SLin.Ling@Sun.COM 		ASSERT3U(mintxg, ==, ds1->ds_phys->ds_prev_snap_txg);
101812296SLin.Ling@Sun.COM 		ASSERT3U(mintxg, ==, ds2->ds_phys->ds_prev_snap_txg);
101912296SLin.Ling@Sun.COM 		VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
102012296SLin.Ling@Sun.COM 		    scn->scn_phys.scn_queue_obj, ds2->ds_object, tx));
102112296SLin.Ling@Sun.COM 		VERIFY(0 == zap_add_int_key(dp->dp_meta_objset,
102212296SLin.Ling@Sun.COM 		    scn->scn_phys.scn_queue_obj, ds1->ds_object, mintxg, tx));
102312296SLin.Ling@Sun.COM 		zfs_dbgmsg("clone_swap ds %llu; in queue; "
102412296SLin.Ling@Sun.COM 		    "replacing with %llu",
102512296SLin.Ling@Sun.COM 		    (u_longlong_t)ds2->ds_object,
102612296SLin.Ling@Sun.COM 		    (u_longlong_t)ds1->ds_object);
102712296SLin.Ling@Sun.COM 	}
102812296SLin.Ling@Sun.COM 
102912296SLin.Ling@Sun.COM 	dsl_scan_sync_state(scn, tx);
103012296SLin.Ling@Sun.COM }
103112296SLin.Ling@Sun.COM 
103212296SLin.Ling@Sun.COM struct enqueue_clones_arg {
103312296SLin.Ling@Sun.COM 	dmu_tx_t *tx;
103412296SLin.Ling@Sun.COM 	uint64_t originobj;
103512296SLin.Ling@Sun.COM };
103612296SLin.Ling@Sun.COM 
103712296SLin.Ling@Sun.COM /* ARGSUSED */
103812296SLin.Ling@Sun.COM static int
103912296SLin.Ling@Sun.COM enqueue_clones_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
104012296SLin.Ling@Sun.COM {
104112296SLin.Ling@Sun.COM 	struct enqueue_clones_arg *eca = arg;
104212296SLin.Ling@Sun.COM 	dsl_dataset_t *ds;
104312296SLin.Ling@Sun.COM 	int err;
104412296SLin.Ling@Sun.COM 	dsl_pool_t *dp = spa->spa_dsl_pool;
104512296SLin.Ling@Sun.COM 	dsl_scan_t *scn = dp->dp_scan;
104612296SLin.Ling@Sun.COM 
104712296SLin.Ling@Sun.COM 	err = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds);
104812296SLin.Ling@Sun.COM 	if (err)
104912296SLin.Ling@Sun.COM 		return (err);
105012296SLin.Ling@Sun.COM 
105112296SLin.Ling@Sun.COM 	if (ds->ds_dir->dd_phys->dd_origin_obj == eca->originobj) {
105212296SLin.Ling@Sun.COM 		while (ds->ds_phys->ds_prev_snap_obj != eca->originobj) {
105312296SLin.Ling@Sun.COM 			dsl_dataset_t *prev;
105412296SLin.Ling@Sun.COM 			err = dsl_dataset_hold_obj(dp,
105512296SLin.Ling@Sun.COM 			    ds->ds_phys->ds_prev_snap_obj, FTAG, &prev);
105612296SLin.Ling@Sun.COM 
105712296SLin.Ling@Sun.COM 			dsl_dataset_rele(ds, FTAG);
105812296SLin.Ling@Sun.COM 			if (err)
105912296SLin.Ling@Sun.COM 				return (err);
106012296SLin.Ling@Sun.COM 			ds = prev;
106112296SLin.Ling@Sun.COM 		}
106212296SLin.Ling@Sun.COM 		VERIFY(zap_add_int_key(dp->dp_meta_objset,
106312296SLin.Ling@Sun.COM 		    scn->scn_phys.scn_queue_obj, ds->ds_object,
106412296SLin.Ling@Sun.COM 		    ds->ds_phys->ds_prev_snap_txg, eca->tx) == 0);
106512296SLin.Ling@Sun.COM 	}
106612296SLin.Ling@Sun.COM 	dsl_dataset_rele(ds, FTAG);
106712296SLin.Ling@Sun.COM 	return (0);
106812296SLin.Ling@Sun.COM }
106912296SLin.Ling@Sun.COM 
107012296SLin.Ling@Sun.COM static void
107112296SLin.Ling@Sun.COM dsl_scan_visitds(dsl_scan_t *scn, uint64_t dsobj, dmu_tx_t *tx)
107212296SLin.Ling@Sun.COM {
107312296SLin.Ling@Sun.COM 	dsl_pool_t *dp = scn->scn_dp;
107412296SLin.Ling@Sun.COM 	dsl_dataset_t *ds;
107512296SLin.Ling@Sun.COM 
107612296SLin.Ling@Sun.COM 	VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
107712296SLin.Ling@Sun.COM 
107812296SLin.Ling@Sun.COM 	/*
107912296SLin.Ling@Sun.COM 	 * Iterate over the bps in this ds.
108012296SLin.Ling@Sun.COM 	 */
108112296SLin.Ling@Sun.COM 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
108212296SLin.Ling@Sun.COM 	dsl_scan_visit_rootbp(scn, ds, &ds->ds_phys->ds_bp, tx);
108312296SLin.Ling@Sun.COM 
108412296SLin.Ling@Sun.COM 	char *dsname = kmem_alloc(ZFS_MAXNAMELEN, KM_SLEEP);
108512296SLin.Ling@Sun.COM 	dsl_dataset_name(ds, dsname);
108612296SLin.Ling@Sun.COM 	zfs_dbgmsg("scanned dataset %llu (%s) with min=%llu max=%llu; "
108712296SLin.Ling@Sun.COM 	    "pausing=%u",
108812296SLin.Ling@Sun.COM 	    (longlong_t)dsobj, dsname,
108912296SLin.Ling@Sun.COM 	    (longlong_t)scn->scn_phys.scn_cur_min_txg,
109012296SLin.Ling@Sun.COM 	    (longlong_t)scn->scn_phys.scn_cur_max_txg,
109112296SLin.Ling@Sun.COM 	    (int)scn->scn_pausing);
109212296SLin.Ling@Sun.COM 	kmem_free(dsname, ZFS_MAXNAMELEN);
109312296SLin.Ling@Sun.COM 
109412296SLin.Ling@Sun.COM 	if (scn->scn_pausing)
109512296SLin.Ling@Sun.COM 		goto out;
109612296SLin.Ling@Sun.COM 
109712296SLin.Ling@Sun.COM 	/*
109812296SLin.Ling@Sun.COM 	 * We've finished this pass over this dataset.
109912296SLin.Ling@Sun.COM 	 */
110012296SLin.Ling@Sun.COM 
110112296SLin.Ling@Sun.COM 	/*
110212296SLin.Ling@Sun.COM 	 * If we did not completely visit this dataset, do another pass.
110312296SLin.Ling@Sun.COM 	 */
110412296SLin.Ling@Sun.COM 	if (scn->scn_phys.scn_flags & DSF_VISIT_DS_AGAIN) {
110512296SLin.Ling@Sun.COM 		zfs_dbgmsg("incomplete pass; visiting again");
110612296SLin.Ling@Sun.COM 		scn->scn_phys.scn_flags &= ~DSF_VISIT_DS_AGAIN;
110712296SLin.Ling@Sun.COM 		VERIFY(zap_add_int_key(dp->dp_meta_objset,
110812296SLin.Ling@Sun.COM 		    scn->scn_phys.scn_queue_obj, ds->ds_object,
110912296SLin.Ling@Sun.COM 		    scn->scn_phys.scn_cur_max_txg, tx) == 0);
111012296SLin.Ling@Sun.COM 		goto out;
111112296SLin.Ling@Sun.COM 	}
111212296SLin.Ling@Sun.COM 
111312296SLin.Ling@Sun.COM 	/*
111412296SLin.Ling@Sun.COM 	 * Add descendent datasets to work queue.
111512296SLin.Ling@Sun.COM 	 */
111612296SLin.Ling@Sun.COM 	if (ds->ds_phys->ds_next_snap_obj != 0) {
111712296SLin.Ling@Sun.COM 		VERIFY(zap_add_int_key(dp->dp_meta_objset,
111812296SLin.Ling@Sun.COM 		    scn->scn_phys.scn_queue_obj, ds->ds_phys->ds_next_snap_obj,
111912296SLin.Ling@Sun.COM 		    ds->ds_phys->ds_creation_txg, tx) == 0);
112012296SLin.Ling@Sun.COM 	}
112112296SLin.Ling@Sun.COM 	if (ds->ds_phys->ds_num_children > 1) {
112212296SLin.Ling@Sun.COM 		boolean_t usenext = B_FALSE;
112312296SLin.Ling@Sun.COM 		if (ds->ds_phys->ds_next_clones_obj != 0) {
112412296SLin.Ling@Sun.COM 			uint64_t count;
112512296SLin.Ling@Sun.COM 			/*
112612296SLin.Ling@Sun.COM 			 * A bug in a previous version of the code could
112712296SLin.Ling@Sun.COM 			 * cause upgrade_clones_cb() to not set
112812296SLin.Ling@Sun.COM 			 * ds_next_snap_obj when it should, leading to a
112912296SLin.Ling@Sun.COM 			 * missing entry.  Therefore we can only use the
113012296SLin.Ling@Sun.COM 			 * next_clones_obj when its count is correct.
113112296SLin.Ling@Sun.COM 			 */
113212296SLin.Ling@Sun.COM 			int err = zap_count(dp->dp_meta_objset,
113312296SLin.Ling@Sun.COM 			    ds->ds_phys->ds_next_clones_obj, &count);
113412296SLin.Ling@Sun.COM 			if (err == 0 &&
113512296SLin.Ling@Sun.COM 			    count == ds->ds_phys->ds_num_children - 1)
113612296SLin.Ling@Sun.COM 				usenext = B_TRUE;
113712296SLin.Ling@Sun.COM 		}
113812296SLin.Ling@Sun.COM 
113912296SLin.Ling@Sun.COM 		if (usenext) {
114012296SLin.Ling@Sun.COM 			VERIFY(zap_join_key(dp->dp_meta_objset,
114112296SLin.Ling@Sun.COM 			    ds->ds_phys->ds_next_clones_obj,
114212296SLin.Ling@Sun.COM 			    scn->scn_phys.scn_queue_obj,
114312296SLin.Ling@Sun.COM 			    ds->ds_phys->ds_creation_txg, tx) == 0);
114412296SLin.Ling@Sun.COM 		} else {
114512296SLin.Ling@Sun.COM 			struct enqueue_clones_arg eca;
114612296SLin.Ling@Sun.COM 			eca.tx = tx;
114712296SLin.Ling@Sun.COM 			eca.originobj = ds->ds_object;
114812296SLin.Ling@Sun.COM 
114912296SLin.Ling@Sun.COM 			(void) dmu_objset_find_spa(ds->ds_dir->dd_pool->dp_spa,
115012296SLin.Ling@Sun.COM 			    NULL, enqueue_clones_cb, &eca, DS_FIND_CHILDREN);
115112296SLin.Ling@Sun.COM 		}
115212296SLin.Ling@Sun.COM 	}
115312296SLin.Ling@Sun.COM 
115412296SLin.Ling@Sun.COM out:
115512296SLin.Ling@Sun.COM 	dsl_dataset_rele(ds, FTAG);
115612296SLin.Ling@Sun.COM }
115712296SLin.Ling@Sun.COM 
115812296SLin.Ling@Sun.COM /* ARGSUSED */
115912296SLin.Ling@Sun.COM static int
116012296SLin.Ling@Sun.COM enqueue_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
116112296SLin.Ling@Sun.COM {
116212296SLin.Ling@Sun.COM 	dmu_tx_t *tx = arg;
116312296SLin.Ling@Sun.COM 	dsl_dataset_t *ds;
116412296SLin.Ling@Sun.COM 	int err;
116512296SLin.Ling@Sun.COM 	dsl_pool_t *dp = spa->spa_dsl_pool;
116612296SLin.Ling@Sun.COM 	dsl_scan_t *scn = dp->dp_scan;
116712296SLin.Ling@Sun.COM 
116812296SLin.Ling@Sun.COM 	err = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds);
116912296SLin.Ling@Sun.COM 	if (err)
117012296SLin.Ling@Sun.COM 		return (err);
117112296SLin.Ling@Sun.COM 
117212296SLin.Ling@Sun.COM 	while (ds->ds_phys->ds_prev_snap_obj != 0) {
117312296SLin.Ling@Sun.COM 		dsl_dataset_t *prev;
117412296SLin.Ling@Sun.COM 		err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
117512296SLin.Ling@Sun.COM 		    FTAG, &prev);
117612296SLin.Ling@Sun.COM 		if (err) {
117712296SLin.Ling@Sun.COM 			dsl_dataset_rele(ds, FTAG);
117812296SLin.Ling@Sun.COM 			return (err);
117912296SLin.Ling@Sun.COM 		}
118012296SLin.Ling@Sun.COM 
118112296SLin.Ling@Sun.COM 		/*
118212296SLin.Ling@Sun.COM 		 * If this is a clone, we don't need to worry about it for now.
118312296SLin.Ling@Sun.COM 		 */
118412296SLin.Ling@Sun.COM 		if (prev->ds_phys->ds_next_snap_obj != ds->ds_object) {
118512296SLin.Ling@Sun.COM 			dsl_dataset_rele(ds, FTAG);
118612296SLin.Ling@Sun.COM 			dsl_dataset_rele(prev, FTAG);
118712296SLin.Ling@Sun.COM 			return (0);
118812296SLin.Ling@Sun.COM 		}
118912296SLin.Ling@Sun.COM 		dsl_dataset_rele(ds, FTAG);
119012296SLin.Ling@Sun.COM 		ds = prev;
119112296SLin.Ling@Sun.COM 	}
119212296SLin.Ling@Sun.COM 
119312296SLin.Ling@Sun.COM 	VERIFY(zap_add_int_key(dp->dp_meta_objset, scn->scn_phys.scn_queue_obj,
119412296SLin.Ling@Sun.COM 	    ds->ds_object, ds->ds_phys->ds_prev_snap_txg, tx) == 0);
119512296SLin.Ling@Sun.COM 	dsl_dataset_rele(ds, FTAG);
119612296SLin.Ling@Sun.COM 	return (0);
119712296SLin.Ling@Sun.COM }
119812296SLin.Ling@Sun.COM 
119912296SLin.Ling@Sun.COM /*
120012296SLin.Ling@Sun.COM  * Scrub/dedup interaction.
120112296SLin.Ling@Sun.COM  *
120212296SLin.Ling@Sun.COM  * If there are N references to a deduped block, we don't want to scrub it
120312296SLin.Ling@Sun.COM  * N times -- ideally, we should scrub it exactly once.
120412296SLin.Ling@Sun.COM  *
120512296SLin.Ling@Sun.COM  * We leverage the fact that the dde's replication class (enum ddt_class)
120612296SLin.Ling@Sun.COM  * is ordered from highest replication class (DDT_CLASS_DITTO) to lowest
120712296SLin.Ling@Sun.COM  * (DDT_CLASS_UNIQUE) so that we may walk the DDT in that order.
120812296SLin.Ling@Sun.COM  *
120912296SLin.Ling@Sun.COM  * To prevent excess scrubbing, the scrub begins by walking the DDT
121012296SLin.Ling@Sun.COM  * to find all blocks with refcnt > 1, and scrubs each of these once.
121112296SLin.Ling@Sun.COM  * Since there are two replication classes which contain blocks with
121212296SLin.Ling@Sun.COM  * refcnt > 1, we scrub the highest replication class (DDT_CLASS_DITTO) first.
121312296SLin.Ling@Sun.COM  * Finally the top-down scrub begins, only visiting blocks with refcnt == 1.
121412296SLin.Ling@Sun.COM  *
121512296SLin.Ling@Sun.COM  * There would be nothing more to say if a block's refcnt couldn't change
121612296SLin.Ling@Sun.COM  * during a scrub, but of course it can so we must account for changes
121712296SLin.Ling@Sun.COM  * in a block's replication class.
121812296SLin.Ling@Sun.COM  *
121912296SLin.Ling@Sun.COM  * Here's an example of what can occur:
122012296SLin.Ling@Sun.COM  *
122112296SLin.Ling@Sun.COM  * If a block has refcnt > 1 during the DDT scrub phase, but has refcnt == 1
122212296SLin.Ling@Sun.COM  * when visited during the top-down scrub phase, it will be scrubbed twice.
122312296SLin.Ling@Sun.COM  * This negates our scrub optimization, but is otherwise harmless.
122412296SLin.Ling@Sun.COM  *
122512296SLin.Ling@Sun.COM  * If a block has refcnt == 1 during the DDT scrub phase, but has refcnt > 1
122612296SLin.Ling@Sun.COM  * on each visit during the top-down scrub phase, it will never be scrubbed.
122712296SLin.Ling@Sun.COM  * To catch this, ddt_sync_entry() notifies the scrub code whenever a block's
122812296SLin.Ling@Sun.COM  * reference class transitions to a higher level (i.e DDT_CLASS_UNIQUE to
122912296SLin.Ling@Sun.COM  * DDT_CLASS_DUPLICATE); if it transitions from refcnt == 1 to refcnt > 1
123012296SLin.Ling@Sun.COM  * while a scrub is in progress, it scrubs the block right then.
123112296SLin.Ling@Sun.COM  */
123212296SLin.Ling@Sun.COM static void
123312296SLin.Ling@Sun.COM dsl_scan_ddt(dsl_scan_t *scn, dmu_tx_t *tx)
123412296SLin.Ling@Sun.COM {
123512296SLin.Ling@Sun.COM 	ddt_bookmark_t *ddb = &scn->scn_phys.scn_ddt_bookmark;
123612296SLin.Ling@Sun.COM 	ddt_entry_t dde = { 0 };
123712296SLin.Ling@Sun.COM 	int error;
123812296SLin.Ling@Sun.COM 	uint64_t n = 0;
123912296SLin.Ling@Sun.COM 
124012296SLin.Ling@Sun.COM 	while ((error = ddt_walk(scn->scn_dp->dp_spa, ddb, &dde)) == 0) {
124112296SLin.Ling@Sun.COM 		ddt_t *ddt;
124212296SLin.Ling@Sun.COM 
124312296SLin.Ling@Sun.COM 		if (ddb->ddb_class > scn->scn_phys.scn_ddt_class_max)
124412296SLin.Ling@Sun.COM 			break;
124512296SLin.Ling@Sun.COM 		dprintf("visiting ddb=%llu/%llu/%llu/%llx\n",
124612296SLin.Ling@Sun.COM 		    (longlong_t)ddb->ddb_class,
124712296SLin.Ling@Sun.COM 		    (longlong_t)ddb->ddb_type,
124812296SLin.Ling@Sun.COM 		    (longlong_t)ddb->ddb_checksum,
124912296SLin.Ling@Sun.COM 		    (longlong_t)ddb->ddb_cursor);
125012296SLin.Ling@Sun.COM 
125112296SLin.Ling@Sun.COM 		/* There should be no pending changes to the dedup table */
125212296SLin.Ling@Sun.COM 		ddt = scn->scn_dp->dp_spa->spa_ddt[ddb->ddb_checksum];
125312296SLin.Ling@Sun.COM 		ASSERT(avl_first(&ddt->ddt_tree) == NULL);
125412296SLin.Ling@Sun.COM 
125512296SLin.Ling@Sun.COM 		dsl_scan_ddt_entry(scn, ddb->ddb_checksum, &dde, tx);
125612296SLin.Ling@Sun.COM 		n++;
125712296SLin.Ling@Sun.COM 
125812296SLin.Ling@Sun.COM 		if (dsl_scan_check_pause(scn, NULL))
125912296SLin.Ling@Sun.COM 			break;
126012296SLin.Ling@Sun.COM 	}
126112296SLin.Ling@Sun.COM 
126212296SLin.Ling@Sun.COM 	zfs_dbgmsg("scanned %llu ddt entries with class_max = %u; pausing=%u",
126312296SLin.Ling@Sun.COM 	    (longlong_t)n, (int)scn->scn_phys.scn_ddt_class_max,
126412296SLin.Ling@Sun.COM 	    (int)scn->scn_pausing);
126512296SLin.Ling@Sun.COM 
126612296SLin.Ling@Sun.COM 	ASSERT(error == 0 || error == ENOENT);
126712296SLin.Ling@Sun.COM 	ASSERT(error != ENOENT ||
126812296SLin.Ling@Sun.COM 	    ddb->ddb_class > scn->scn_phys.scn_ddt_class_max);
126912296SLin.Ling@Sun.COM }
127012296SLin.Ling@Sun.COM 
127112296SLin.Ling@Sun.COM /* ARGSUSED */
127212296SLin.Ling@Sun.COM void
127312296SLin.Ling@Sun.COM dsl_scan_ddt_entry(dsl_scan_t *scn, enum zio_checksum checksum,
127412296SLin.Ling@Sun.COM     ddt_entry_t *dde, dmu_tx_t *tx)
127512296SLin.Ling@Sun.COM {
127612296SLin.Ling@Sun.COM 	const ddt_key_t *ddk = &dde->dde_key;
127712296SLin.Ling@Sun.COM 	ddt_phys_t *ddp = dde->dde_phys;
127812296SLin.Ling@Sun.COM 	blkptr_t bp;
127912296SLin.Ling@Sun.COM 	zbookmark_t zb = { 0 };
128012296SLin.Ling@Sun.COM 
128112296SLin.Ling@Sun.COM 	if (scn->scn_phys.scn_state != DSS_SCANNING)
128212296SLin.Ling@Sun.COM 		return;
128312296SLin.Ling@Sun.COM 
128412296SLin.Ling@Sun.COM 	for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
128512296SLin.Ling@Sun.COM 		if (ddp->ddp_phys_birth == 0 ||
128612296SLin.Ling@Sun.COM 		    ddp->ddp_phys_birth > scn->scn_phys.scn_cur_max_txg)
128712296SLin.Ling@Sun.COM 			continue;
128812296SLin.Ling@Sun.COM 		ddt_bp_create(checksum, ddk, ddp, &bp);
128912296SLin.Ling@Sun.COM 
129012296SLin.Ling@Sun.COM 		scn->scn_visited_this_txg++;
129112296SLin.Ling@Sun.COM 		scan_funcs[scn->scn_phys.scn_func](scn->scn_dp, &bp, &zb);
129212296SLin.Ling@Sun.COM 	}
129312296SLin.Ling@Sun.COM }
129412296SLin.Ling@Sun.COM 
129512296SLin.Ling@Sun.COM static void
129612296SLin.Ling@Sun.COM dsl_scan_visit(dsl_scan_t *scn, dmu_tx_t *tx)
129712296SLin.Ling@Sun.COM {
129812296SLin.Ling@Sun.COM 	dsl_pool_t *dp = scn->scn_dp;
129912296SLin.Ling@Sun.COM 	zap_cursor_t zc;
130012296SLin.Ling@Sun.COM 	zap_attribute_t za;
130112296SLin.Ling@Sun.COM 
130212296SLin.Ling@Sun.COM 	if (scn->scn_phys.scn_ddt_bookmark.ddb_class <=
130312296SLin.Ling@Sun.COM 	    scn->scn_phys.scn_ddt_class_max) {
130412296SLin.Ling@Sun.COM 		scn->scn_phys.scn_cur_min_txg = scn->scn_phys.scn_min_txg;
130512296SLin.Ling@Sun.COM 		scn->scn_phys.scn_cur_max_txg = scn->scn_phys.scn_max_txg;
130612296SLin.Ling@Sun.COM 		dsl_scan_ddt(scn, tx);
130712296SLin.Ling@Sun.COM 		if (scn->scn_pausing)
130812296SLin.Ling@Sun.COM 			return;
130912296SLin.Ling@Sun.COM 	}
131012296SLin.Ling@Sun.COM 
131112296SLin.Ling@Sun.COM 	if (scn->scn_phys.scn_bookmark.zb_objset == DMU_META_OBJSET) {
131212296SLin.Ling@Sun.COM 		/* First do the MOS & ORIGIN */
131312296SLin.Ling@Sun.COM 
131412296SLin.Ling@Sun.COM 		scn->scn_phys.scn_cur_min_txg = scn->scn_phys.scn_min_txg;
131512296SLin.Ling@Sun.COM 		scn->scn_phys.scn_cur_max_txg = scn->scn_phys.scn_max_txg;
131612296SLin.Ling@Sun.COM 		dsl_scan_visit_rootbp(scn, NULL,
131712296SLin.Ling@Sun.COM 		    &dp->dp_meta_rootbp, tx);
131812296SLin.Ling@Sun.COM 		spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
131912296SLin.Ling@Sun.COM 		if (scn->scn_pausing)
132012296SLin.Ling@Sun.COM 			return;
132112296SLin.Ling@Sun.COM 
132212296SLin.Ling@Sun.COM 		if (spa_version(dp->dp_spa) < SPA_VERSION_DSL_SCRUB) {
132312296SLin.Ling@Sun.COM 			VERIFY(0 == dmu_objset_find_spa(dp->dp_spa,
132412296SLin.Ling@Sun.COM 			    NULL, enqueue_cb, tx, DS_FIND_CHILDREN));
132512296SLin.Ling@Sun.COM 		} else {
132612296SLin.Ling@Sun.COM 			dsl_scan_visitds(scn,
132712296SLin.Ling@Sun.COM 			    dp->dp_origin_snap->ds_object, tx);
132812296SLin.Ling@Sun.COM 		}
132912296SLin.Ling@Sun.COM 		ASSERT(!scn->scn_pausing);
133012296SLin.Ling@Sun.COM 	} else if (scn->scn_phys.scn_bookmark.zb_objset !=
133112296SLin.Ling@Sun.COM 	    ZB_DESTROYED_OBJSET) {
133212296SLin.Ling@Sun.COM 		/*
133312296SLin.Ling@Sun.COM 		 * If we were paused, continue from here.  Note if the
133412296SLin.Ling@Sun.COM 		 * ds we were paused on was deleted, the zb_objset may
133512296SLin.Ling@Sun.COM 		 * be -1, so we will skip this and find a new objset
133612296SLin.Ling@Sun.COM 		 * below.
133712296SLin.Ling@Sun.COM 		 */
133812296SLin.Ling@Sun.COM 		dsl_scan_visitds(scn, scn->scn_phys.scn_bookmark.zb_objset, tx);
133912296SLin.Ling@Sun.COM 		if (scn->scn_pausing)
134012296SLin.Ling@Sun.COM 			return;
134112296SLin.Ling@Sun.COM 	}
134212296SLin.Ling@Sun.COM 
134312296SLin.Ling@Sun.COM 	/*
134412296SLin.Ling@Sun.COM 	 * In case we were paused right at the end of the ds, zero the
134512296SLin.Ling@Sun.COM 	 * bookmark so we don't think that we're still trying to resume.
134612296SLin.Ling@Sun.COM 	 */
134712296SLin.Ling@Sun.COM 	bzero(&scn->scn_phys.scn_bookmark, sizeof (zbookmark_t));
134812296SLin.Ling@Sun.COM 
134912296SLin.Ling@Sun.COM 	/* keep pulling things out of the zap-object-as-queue */
135012296SLin.Ling@Sun.COM 	while (zap_cursor_init(&zc, dp->dp_meta_objset,
135112296SLin.Ling@Sun.COM 	    scn->scn_phys.scn_queue_obj),
135212296SLin.Ling@Sun.COM 	    zap_cursor_retrieve(&zc, &za) == 0) {
135312296SLin.Ling@Sun.COM 		dsl_dataset_t *ds;
135412296SLin.Ling@Sun.COM 		uint64_t dsobj;
135512296SLin.Ling@Sun.COM 
135612296SLin.Ling@Sun.COM 		dsobj = strtonum(za.za_name, NULL);
135712296SLin.Ling@Sun.COM 		VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
135812296SLin.Ling@Sun.COM 		    scn->scn_phys.scn_queue_obj, dsobj, tx));
135912296SLin.Ling@Sun.COM 
136012296SLin.Ling@Sun.COM 		/* Set up min/max txg */
136112296SLin.Ling@Sun.COM 		VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
136212296SLin.Ling@Sun.COM 		if (za.za_first_integer != 0) {
136312296SLin.Ling@Sun.COM 			scn->scn_phys.scn_cur_min_txg =
136412296SLin.Ling@Sun.COM 			    MAX(scn->scn_phys.scn_min_txg,
136512296SLin.Ling@Sun.COM 			    za.za_first_integer);
136612296SLin.Ling@Sun.COM 		} else {
136712296SLin.Ling@Sun.COM 			scn->scn_phys.scn_cur_min_txg =
136812296SLin.Ling@Sun.COM 			    MAX(scn->scn_phys.scn_min_txg,
136912296SLin.Ling@Sun.COM 			    ds->ds_phys->ds_prev_snap_txg);
137012296SLin.Ling@Sun.COM 		}
137112296SLin.Ling@Sun.COM 		scn->scn_phys.scn_cur_max_txg = dsl_scan_ds_maxtxg(ds);
137212296SLin.Ling@Sun.COM 		dsl_dataset_rele(ds, FTAG);
137312296SLin.Ling@Sun.COM 
137412296SLin.Ling@Sun.COM 		dsl_scan_visitds(scn, dsobj, tx);
137512296SLin.Ling@Sun.COM 		zap_cursor_fini(&zc);
137612296SLin.Ling@Sun.COM 		if (scn->scn_pausing)
137712296SLin.Ling@Sun.COM 			return;
137812296SLin.Ling@Sun.COM 	}
137912296SLin.Ling@Sun.COM 	zap_cursor_fini(&zc);
138012296SLin.Ling@Sun.COM }
138112296SLin.Ling@Sun.COM 
1382*12470SMatthew.Ahrens@Sun.COM static int
1383*12470SMatthew.Ahrens@Sun.COM dsl_scan_free_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1384*12470SMatthew.Ahrens@Sun.COM {
1385*12470SMatthew.Ahrens@Sun.COM 	dsl_scan_t *scn = arg;
1386*12470SMatthew.Ahrens@Sun.COM 	uint64_t elapsed_nanosecs;
1387*12470SMatthew.Ahrens@Sun.COM 
1388*12470SMatthew.Ahrens@Sun.COM 	elapsed_nanosecs = gethrtime() - scn->scn_sync_start_time;
1389*12470SMatthew.Ahrens@Sun.COM 
1390*12470SMatthew.Ahrens@Sun.COM 	if (elapsed_nanosecs / NANOSEC > zfs_txg_timeout ||
1391*12470SMatthew.Ahrens@Sun.COM 	    (elapsed_nanosecs / MICROSEC > zfs_free_min_time_ms &&
1392*12470SMatthew.Ahrens@Sun.COM 	    txg_sync_waiting(scn->scn_dp)) ||
1393*12470SMatthew.Ahrens@Sun.COM 	    spa_shutting_down(scn->scn_dp->dp_spa))
1394*12470SMatthew.Ahrens@Sun.COM 		return (ERESTART);
1395*12470SMatthew.Ahrens@Sun.COM 
1396*12470SMatthew.Ahrens@Sun.COM 	zio_nowait(zio_free_sync(scn->scn_zio_root, scn->scn_dp->dp_spa,
1397*12470SMatthew.Ahrens@Sun.COM 	    dmu_tx_get_txg(tx), bp, 0));
1398*12470SMatthew.Ahrens@Sun.COM 	dsl_dir_diduse_space(tx->tx_pool->dp_free_dir, DD_USED_HEAD,
1399*12470SMatthew.Ahrens@Sun.COM 	    -bp_get_dsize_sync(scn->scn_dp->dp_spa, bp),
1400*12470SMatthew.Ahrens@Sun.COM 	    -BP_GET_PSIZE(bp), -BP_GET_UCSIZE(bp), tx);
1401*12470SMatthew.Ahrens@Sun.COM 	scn->scn_visited_this_txg++;
1402*12470SMatthew.Ahrens@Sun.COM 	return (0);
1403*12470SMatthew.Ahrens@Sun.COM }
1404*12470SMatthew.Ahrens@Sun.COM 
1405*12470SMatthew.Ahrens@Sun.COM boolean_t
1406*12470SMatthew.Ahrens@Sun.COM dsl_scan_active(dsl_scan_t *scn)
1407*12470SMatthew.Ahrens@Sun.COM {
1408*12470SMatthew.Ahrens@Sun.COM 	spa_t *spa = scn->scn_dp->dp_spa;
1409*12470SMatthew.Ahrens@Sun.COM 	uint64_t used = 0, comp, uncomp;
1410*12470SMatthew.Ahrens@Sun.COM 
1411*12470SMatthew.Ahrens@Sun.COM 	if (spa->spa_load_state != SPA_LOAD_NONE)
1412*12470SMatthew.Ahrens@Sun.COM 		return (B_FALSE);
1413*12470SMatthew.Ahrens@Sun.COM 	if (spa_shutting_down(spa))
1414*12470SMatthew.Ahrens@Sun.COM 		return (B_FALSE);
1415*12470SMatthew.Ahrens@Sun.COM 
1416*12470SMatthew.Ahrens@Sun.COM 	if (scn->scn_phys.scn_state == DSS_SCANNING)
1417*12470SMatthew.Ahrens@Sun.COM 		return (B_TRUE);
1418*12470SMatthew.Ahrens@Sun.COM 
1419*12470SMatthew.Ahrens@Sun.COM 	if (spa_version(scn->scn_dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
1420*12470SMatthew.Ahrens@Sun.COM 		(void) bpobj_space(&scn->scn_dp->dp_free_bpobj,
1421*12470SMatthew.Ahrens@Sun.COM 		    &used, &comp, &uncomp);
1422*12470SMatthew.Ahrens@Sun.COM 	}
1423*12470SMatthew.Ahrens@Sun.COM 	return (used != 0);
1424*12470SMatthew.Ahrens@Sun.COM }
1425*12470SMatthew.Ahrens@Sun.COM 
142612296SLin.Ling@Sun.COM void
142712296SLin.Ling@Sun.COM dsl_scan_sync(dsl_pool_t *dp, dmu_tx_t *tx)
142812296SLin.Ling@Sun.COM {
142912296SLin.Ling@Sun.COM 	dsl_scan_t *scn = dp->dp_scan;
143012296SLin.Ling@Sun.COM 	spa_t *spa = dp->dp_spa;
1431*12470SMatthew.Ahrens@Sun.COM 	int err;
143212296SLin.Ling@Sun.COM 
143312296SLin.Ling@Sun.COM 	/*
143412296SLin.Ling@Sun.COM 	 * Check for scn_restart_txg before checking spa_load_state, so
143512296SLin.Ling@Sun.COM 	 * that we can restart an old-style scan while the pool is being
143612296SLin.Ling@Sun.COM 	 * imported (see dsl_scan_init).
143712296SLin.Ling@Sun.COM 	 */
143812296SLin.Ling@Sun.COM 	if (scn->scn_restart_txg != 0 &&
143912296SLin.Ling@Sun.COM 	    scn->scn_restart_txg <= tx->tx_txg) {
144012296SLin.Ling@Sun.COM 		pool_scan_func_t func = POOL_SCAN_SCRUB;
144112296SLin.Ling@Sun.COM 		dsl_scan_done(scn, B_FALSE, tx);
144212296SLin.Ling@Sun.COM 		if (vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL))
144312296SLin.Ling@Sun.COM 			func = POOL_SCAN_RESILVER;
144412296SLin.Ling@Sun.COM 		zfs_dbgmsg("restarting scan func=%u txg=%llu",
144512296SLin.Ling@Sun.COM 		    func, tx->tx_txg);
144612296SLin.Ling@Sun.COM 		dsl_scan_setup_sync(scn, &func, tx);
144712296SLin.Ling@Sun.COM 	}
144812296SLin.Ling@Sun.COM 
1449*12470SMatthew.Ahrens@Sun.COM 
1450*12470SMatthew.Ahrens@Sun.COM 	if (!dsl_scan_active(scn) ||
1451*12470SMatthew.Ahrens@Sun.COM 	    spa_sync_pass(dp->dp_spa) > 1)
145212296SLin.Ling@Sun.COM 		return;
145312296SLin.Ling@Sun.COM 
145412296SLin.Ling@Sun.COM 	scn->scn_visited_this_txg = 0;
145512296SLin.Ling@Sun.COM 	scn->scn_pausing = B_FALSE;
145612296SLin.Ling@Sun.COM 	scn->scn_sync_start_time = gethrtime();
145712296SLin.Ling@Sun.COM 	spa->spa_scrub_active = B_TRUE;
145812296SLin.Ling@Sun.COM 
1459*12470SMatthew.Ahrens@Sun.COM 	/*
1460*12470SMatthew.Ahrens@Sun.COM 	 * First process the free list.  If we pause the free, don't do
1461*12470SMatthew.Ahrens@Sun.COM 	 * any scanning.  This ensures that there is no free list when
1462*12470SMatthew.Ahrens@Sun.COM 	 * we are scanning, so the scan code doesn't have to worry about
1463*12470SMatthew.Ahrens@Sun.COM 	 * traversing it.
1464*12470SMatthew.Ahrens@Sun.COM 	 */
1465*12470SMatthew.Ahrens@Sun.COM 	if (spa_version(dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
1466*12470SMatthew.Ahrens@Sun.COM 		scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
1467*12470SMatthew.Ahrens@Sun.COM 		    NULL, ZIO_FLAG_MUSTSUCCEED);
1468*12470SMatthew.Ahrens@Sun.COM 		err = bpobj_iterate(&dp->dp_free_bpobj,
1469*12470SMatthew.Ahrens@Sun.COM 		    dsl_scan_free_cb, scn, tx);
1470*12470SMatthew.Ahrens@Sun.COM 		VERIFY3U(0, ==, zio_wait(scn->scn_zio_root));
1471*12470SMatthew.Ahrens@Sun.COM 		if (scn->scn_visited_this_txg) {
1472*12470SMatthew.Ahrens@Sun.COM 			zfs_dbgmsg("freed %llu blocks in %llums from "
1473*12470SMatthew.Ahrens@Sun.COM 			    "free_bpobj txg %llu",
1474*12470SMatthew.Ahrens@Sun.COM 			    (longlong_t)scn->scn_visited_this_txg,
1475*12470SMatthew.Ahrens@Sun.COM 			    (longlong_t)
1476*12470SMatthew.Ahrens@Sun.COM 			    (gethrtime() - scn->scn_sync_start_time) / MICROSEC,
1477*12470SMatthew.Ahrens@Sun.COM 			    (longlong_t)tx->tx_txg);
1478*12470SMatthew.Ahrens@Sun.COM 			scn->scn_visited_this_txg = 0;
1479*12470SMatthew.Ahrens@Sun.COM 			/*
1480*12470SMatthew.Ahrens@Sun.COM 			 * Re-sync the ddt so that we can further modify
1481*12470SMatthew.Ahrens@Sun.COM 			 * it when doing bprewrite.
1482*12470SMatthew.Ahrens@Sun.COM 			 */
1483*12470SMatthew.Ahrens@Sun.COM 			ddt_sync(spa, tx->tx_txg);
1484*12470SMatthew.Ahrens@Sun.COM 		}
1485*12470SMatthew.Ahrens@Sun.COM 		if (err == ERESTART)
1486*12470SMatthew.Ahrens@Sun.COM 			return;
1487*12470SMatthew.Ahrens@Sun.COM 	}
1488*12470SMatthew.Ahrens@Sun.COM 
1489*12470SMatthew.Ahrens@Sun.COM 	if (scn->scn_phys.scn_state != DSS_SCANNING)
1490*12470SMatthew.Ahrens@Sun.COM 		return;
1491*12470SMatthew.Ahrens@Sun.COM 
1492*12470SMatthew.Ahrens@Sun.COM 
149312296SLin.Ling@Sun.COM 	if (scn->scn_phys.scn_ddt_bookmark.ddb_class <=
149412296SLin.Ling@Sun.COM 	    scn->scn_phys.scn_ddt_class_max) {
149512296SLin.Ling@Sun.COM 		zfs_dbgmsg("doing scan sync txg %llu; "
149612296SLin.Ling@Sun.COM 		    "ddt bm=%llu/%llu/%llu/%llx",
149712296SLin.Ling@Sun.COM 		    (longlong_t)tx->tx_txg,
149812296SLin.Ling@Sun.COM 		    (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_class,
149912296SLin.Ling@Sun.COM 		    (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_type,
150012296SLin.Ling@Sun.COM 		    (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_checksum,
150112296SLin.Ling@Sun.COM 		    (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_cursor);
150212296SLin.Ling@Sun.COM 		ASSERT(scn->scn_phys.scn_bookmark.zb_objset == 0);
150312296SLin.Ling@Sun.COM 		ASSERT(scn->scn_phys.scn_bookmark.zb_object == 0);
150412296SLin.Ling@Sun.COM 		ASSERT(scn->scn_phys.scn_bookmark.zb_level == 0);
150512296SLin.Ling@Sun.COM 		ASSERT(scn->scn_phys.scn_bookmark.zb_blkid == 0);
150612296SLin.Ling@Sun.COM 	} else {
150712296SLin.Ling@Sun.COM 		zfs_dbgmsg("doing scan sync txg %llu; bm=%llu/%llu/%llu/%llu",
150812296SLin.Ling@Sun.COM 		    (longlong_t)tx->tx_txg,
150912296SLin.Ling@Sun.COM 		    (longlong_t)scn->scn_phys.scn_bookmark.zb_objset,
151012296SLin.Ling@Sun.COM 		    (longlong_t)scn->scn_phys.scn_bookmark.zb_object,
151112296SLin.Ling@Sun.COM 		    (longlong_t)scn->scn_phys.scn_bookmark.zb_level,
151212296SLin.Ling@Sun.COM 		    (longlong_t)scn->scn_phys.scn_bookmark.zb_blkid);
151312296SLin.Ling@Sun.COM 	}
151412296SLin.Ling@Sun.COM 
1515*12470SMatthew.Ahrens@Sun.COM 	scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
151612296SLin.Ling@Sun.COM 	    NULL, ZIO_FLAG_CANFAIL);
151712296SLin.Ling@Sun.COM 	dsl_scan_visit(scn, tx);
1518*12470SMatthew.Ahrens@Sun.COM 	(void) zio_wait(scn->scn_zio_root);
1519*12470SMatthew.Ahrens@Sun.COM 	scn->scn_zio_root = NULL;
152012296SLin.Ling@Sun.COM 
152112296SLin.Ling@Sun.COM 	zfs_dbgmsg("visited %llu blocks in %llums",
152212296SLin.Ling@Sun.COM 	    (longlong_t)scn->scn_visited_this_txg,
152312296SLin.Ling@Sun.COM 	    (longlong_t)(gethrtime() - scn->scn_sync_start_time) / MICROSEC);
152412296SLin.Ling@Sun.COM 
152512296SLin.Ling@Sun.COM 	if (!scn->scn_pausing) {
152612296SLin.Ling@Sun.COM 		/* finished with scan. */
152712296SLin.Ling@Sun.COM 		zfs_dbgmsg("finished scan txg %llu", (longlong_t)tx->tx_txg);
152812296SLin.Ling@Sun.COM 		dsl_scan_done(scn, B_TRUE, tx);
152912296SLin.Ling@Sun.COM 	}
153012296SLin.Ling@Sun.COM 
153112296SLin.Ling@Sun.COM 	if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
153212296SLin.Ling@Sun.COM 		mutex_enter(&spa->spa_scrub_lock);
153312296SLin.Ling@Sun.COM 		while (spa->spa_scrub_inflight > 0) {
153412296SLin.Ling@Sun.COM 			cv_wait(&spa->spa_scrub_io_cv,
153512296SLin.Ling@Sun.COM 			    &spa->spa_scrub_lock);
153612296SLin.Ling@Sun.COM 		}
153712296SLin.Ling@Sun.COM 		mutex_exit(&spa->spa_scrub_lock);
153812296SLin.Ling@Sun.COM 	}
153912296SLin.Ling@Sun.COM 
154012296SLin.Ling@Sun.COM 	dsl_scan_sync_state(scn, tx);
154112296SLin.Ling@Sun.COM }
154212296SLin.Ling@Sun.COM 
154312296SLin.Ling@Sun.COM /*
154412296SLin.Ling@Sun.COM  * This will start a new scan, or restart an existing one.
154512296SLin.Ling@Sun.COM  */
154612296SLin.Ling@Sun.COM void
154712296SLin.Ling@Sun.COM dsl_resilver_restart(dsl_pool_t *dp, uint64_t txg)
154812296SLin.Ling@Sun.COM {
154912296SLin.Ling@Sun.COM 	if (txg == 0) {
155012296SLin.Ling@Sun.COM 		dmu_tx_t *tx;
155112296SLin.Ling@Sun.COM 		tx = dmu_tx_create_dd(dp->dp_mos_dir);
155212296SLin.Ling@Sun.COM 		VERIFY(0 == dmu_tx_assign(tx, TXG_WAIT));
155312296SLin.Ling@Sun.COM 
155412296SLin.Ling@Sun.COM 		txg = dmu_tx_get_txg(tx);
155512296SLin.Ling@Sun.COM 		dp->dp_scan->scn_restart_txg = txg;
155612296SLin.Ling@Sun.COM 		dmu_tx_commit(tx);
155712296SLin.Ling@Sun.COM 	} else {
155812296SLin.Ling@Sun.COM 		dp->dp_scan->scn_restart_txg = txg;
155912296SLin.Ling@Sun.COM 	}
156012296SLin.Ling@Sun.COM 	zfs_dbgmsg("restarting resilver txg=%llu", txg);
156112296SLin.Ling@Sun.COM }
156212296SLin.Ling@Sun.COM 
156312296SLin.Ling@Sun.COM boolean_t
156412296SLin.Ling@Sun.COM dsl_scan_resilvering(dsl_pool_t *dp)
156512296SLin.Ling@Sun.COM {
156612296SLin.Ling@Sun.COM 	return (dp->dp_scan->scn_phys.scn_state == DSS_SCANNING &&
156712296SLin.Ling@Sun.COM 	    dp->dp_scan->scn_phys.scn_func == POOL_SCAN_RESILVER);
156812296SLin.Ling@Sun.COM }
156912296SLin.Ling@Sun.COM 
157012296SLin.Ling@Sun.COM /*
157112296SLin.Ling@Sun.COM  * scrub consumers
157212296SLin.Ling@Sun.COM  */
157312296SLin.Ling@Sun.COM 
157412296SLin.Ling@Sun.COM static void
157512296SLin.Ling@Sun.COM count_block(zfs_all_blkstats_t *zab, const blkptr_t *bp)
157612296SLin.Ling@Sun.COM {
157712296SLin.Ling@Sun.COM 	int i;
157812296SLin.Ling@Sun.COM 
157912296SLin.Ling@Sun.COM 	/*
158012296SLin.Ling@Sun.COM 	 * If we resume after a reboot, zab will be NULL; don't record
158112296SLin.Ling@Sun.COM 	 * incomplete stats in that case.
158212296SLin.Ling@Sun.COM 	 */
158312296SLin.Ling@Sun.COM 	if (zab == NULL)
158412296SLin.Ling@Sun.COM 		return;
158512296SLin.Ling@Sun.COM 
158612296SLin.Ling@Sun.COM 	for (i = 0; i < 4; i++) {
158712296SLin.Ling@Sun.COM 		int l = (i < 2) ? BP_GET_LEVEL(bp) : DN_MAX_LEVELS;
158812296SLin.Ling@Sun.COM 		int t = (i & 1) ? BP_GET_TYPE(bp) : DMU_OT_TOTAL;
158912296SLin.Ling@Sun.COM 		zfs_blkstat_t *zb = &zab->zab_type[l][t];
159012296SLin.Ling@Sun.COM 		int equal;
159112296SLin.Ling@Sun.COM 
159212296SLin.Ling@Sun.COM 		zb->zb_count++;
159312296SLin.Ling@Sun.COM 		zb->zb_asize += BP_GET_ASIZE(bp);
159412296SLin.Ling@Sun.COM 		zb->zb_lsize += BP_GET_LSIZE(bp);
159512296SLin.Ling@Sun.COM 		zb->zb_psize += BP_GET_PSIZE(bp);
159612296SLin.Ling@Sun.COM 		zb->zb_gangs += BP_COUNT_GANG(bp);
159712296SLin.Ling@Sun.COM 
159812296SLin.Ling@Sun.COM 		switch (BP_GET_NDVAS(bp)) {
159912296SLin.Ling@Sun.COM 		case 2:
160012296SLin.Ling@Sun.COM 			if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
160112296SLin.Ling@Sun.COM 			    DVA_GET_VDEV(&bp->blk_dva[1]))
160212296SLin.Ling@Sun.COM 				zb->zb_ditto_2_of_2_samevdev++;
160312296SLin.Ling@Sun.COM 			break;
160412296SLin.Ling@Sun.COM 		case 3:
160512296SLin.Ling@Sun.COM 			equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
160612296SLin.Ling@Sun.COM 			    DVA_GET_VDEV(&bp->blk_dva[1])) +
160712296SLin.Ling@Sun.COM 			    (DVA_GET_VDEV(&bp->blk_dva[0]) ==
160812296SLin.Ling@Sun.COM 			    DVA_GET_VDEV(&bp->blk_dva[2])) +
160912296SLin.Ling@Sun.COM 			    (DVA_GET_VDEV(&bp->blk_dva[1]) ==
161012296SLin.Ling@Sun.COM 			    DVA_GET_VDEV(&bp->blk_dva[2]));
161112296SLin.Ling@Sun.COM 			if (equal == 1)
161212296SLin.Ling@Sun.COM 				zb->zb_ditto_2_of_3_samevdev++;
161312296SLin.Ling@Sun.COM 			else if (equal == 3)
161412296SLin.Ling@Sun.COM 				zb->zb_ditto_3_of_3_samevdev++;
161512296SLin.Ling@Sun.COM 			break;
161612296SLin.Ling@Sun.COM 		}
161712296SLin.Ling@Sun.COM 	}
161812296SLin.Ling@Sun.COM }
161912296SLin.Ling@Sun.COM 
162012296SLin.Ling@Sun.COM static void
162112296SLin.Ling@Sun.COM dsl_scan_scrub_done(zio_t *zio)
162212296SLin.Ling@Sun.COM {
162312296SLin.Ling@Sun.COM 	spa_t *spa = zio->io_spa;
162412296SLin.Ling@Sun.COM 
162512296SLin.Ling@Sun.COM 	zio_data_buf_free(zio->io_data, zio->io_size);
162612296SLin.Ling@Sun.COM 
162712296SLin.Ling@Sun.COM 	mutex_enter(&spa->spa_scrub_lock);
162812296SLin.Ling@Sun.COM 	spa->spa_scrub_inflight--;
162912296SLin.Ling@Sun.COM 	cv_broadcast(&spa->spa_scrub_io_cv);
163012296SLin.Ling@Sun.COM 
163112296SLin.Ling@Sun.COM 	if (zio->io_error && (zio->io_error != ECKSUM ||
163212296SLin.Ling@Sun.COM 	    !(zio->io_flags & ZIO_FLAG_SPECULATIVE))) {
163312296SLin.Ling@Sun.COM 		spa->spa_dsl_pool->dp_scan->scn_phys.scn_errors++;
163412296SLin.Ling@Sun.COM 	}
163512296SLin.Ling@Sun.COM 	mutex_exit(&spa->spa_scrub_lock);
163612296SLin.Ling@Sun.COM }
163712296SLin.Ling@Sun.COM 
163812296SLin.Ling@Sun.COM static int
163912296SLin.Ling@Sun.COM dsl_scan_scrub_cb(dsl_pool_t *dp,
164012296SLin.Ling@Sun.COM     const blkptr_t *bp, const zbookmark_t *zb)
164112296SLin.Ling@Sun.COM {
164212296SLin.Ling@Sun.COM 	dsl_scan_t *scn = dp->dp_scan;
164312296SLin.Ling@Sun.COM 	size_t size = BP_GET_PSIZE(bp);
164412296SLin.Ling@Sun.COM 	spa_t *spa = dp->dp_spa;
164512296SLin.Ling@Sun.COM 	uint64_t phys_birth = BP_PHYSICAL_BIRTH(bp);
164612296SLin.Ling@Sun.COM 	boolean_t needs_io;
164712296SLin.Ling@Sun.COM 	int zio_flags = ZIO_FLAG_SCRUB_THREAD | ZIO_FLAG_RAW | ZIO_FLAG_CANFAIL;
164812296SLin.Ling@Sun.COM 	int zio_priority;
164912296SLin.Ling@Sun.COM 
165012296SLin.Ling@Sun.COM 	if (phys_birth <= scn->scn_phys.scn_min_txg ||
165112296SLin.Ling@Sun.COM 	    phys_birth >= scn->scn_phys.scn_max_txg)
165212296SLin.Ling@Sun.COM 		return (0);
165312296SLin.Ling@Sun.COM 
165412296SLin.Ling@Sun.COM 	count_block(dp->dp_blkstats, bp);
165512296SLin.Ling@Sun.COM 
165612296SLin.Ling@Sun.COM 	ASSERT(DSL_SCAN_IS_SCRUB_RESILVER(scn));
165712296SLin.Ling@Sun.COM 	if (scn->scn_phys.scn_func == POOL_SCAN_SCRUB) {
165812296SLin.Ling@Sun.COM 		zio_flags |= ZIO_FLAG_SCRUB;
165912296SLin.Ling@Sun.COM 		zio_priority = ZIO_PRIORITY_SCRUB;
166012296SLin.Ling@Sun.COM 		needs_io = B_TRUE;
166112296SLin.Ling@Sun.COM 	} else if (scn->scn_phys.scn_func == POOL_SCAN_RESILVER) {
166212296SLin.Ling@Sun.COM 		zio_flags |= ZIO_FLAG_RESILVER;
166312296SLin.Ling@Sun.COM 		zio_priority = ZIO_PRIORITY_RESILVER;
166412296SLin.Ling@Sun.COM 		needs_io = B_FALSE;
166512296SLin.Ling@Sun.COM 	}
166612296SLin.Ling@Sun.COM 
166712296SLin.Ling@Sun.COM 	/* If it's an intent log block, failure is expected. */
166812296SLin.Ling@Sun.COM 	if (zb->zb_level == ZB_ZIL_LEVEL)
166912296SLin.Ling@Sun.COM 		zio_flags |= ZIO_FLAG_SPECULATIVE;
167012296SLin.Ling@Sun.COM 
167112296SLin.Ling@Sun.COM 	for (int d = 0; d < BP_GET_NDVAS(bp); d++) {
167212296SLin.Ling@Sun.COM 		vdev_t *vd = vdev_lookup_top(spa,
167312296SLin.Ling@Sun.COM 		    DVA_GET_VDEV(&bp->blk_dva[d]));
167412296SLin.Ling@Sun.COM 
167512296SLin.Ling@Sun.COM 		/*
167612296SLin.Ling@Sun.COM 		 * Keep track of how much data we've examined so that
167712296SLin.Ling@Sun.COM 		 * zpool(1M) status can make useful progress reports.
167812296SLin.Ling@Sun.COM 		 */
167912296SLin.Ling@Sun.COM 		scn->scn_phys.scn_examined += DVA_GET_ASIZE(&bp->blk_dva[d]);
168012296SLin.Ling@Sun.COM 		spa->spa_scan_pass_exam += DVA_GET_ASIZE(&bp->blk_dva[d]);
168112296SLin.Ling@Sun.COM 
168212296SLin.Ling@Sun.COM 		/* if it's a resilver, this may not be in the target range */
168312296SLin.Ling@Sun.COM 		if (!needs_io) {
168412296SLin.Ling@Sun.COM 			if (DVA_GET_GANG(&bp->blk_dva[d])) {
168512296SLin.Ling@Sun.COM 				/*
168612296SLin.Ling@Sun.COM 				 * Gang members may be spread across multiple
168712296SLin.Ling@Sun.COM 				 * vdevs, so the best estimate we have is the
168812296SLin.Ling@Sun.COM 				 * scrub range, which has already been checked.
168912296SLin.Ling@Sun.COM 				 * XXX -- it would be better to change our
169012296SLin.Ling@Sun.COM 				 * allocation policy to ensure that all
169112296SLin.Ling@Sun.COM 				 * gang members reside on the same vdev.
169212296SLin.Ling@Sun.COM 				 */
169312296SLin.Ling@Sun.COM 				needs_io = B_TRUE;
169412296SLin.Ling@Sun.COM 			} else {
169512296SLin.Ling@Sun.COM 				needs_io = vdev_dtl_contains(vd, DTL_PARTIAL,
169612296SLin.Ling@Sun.COM 				    phys_birth, 1);
169712296SLin.Ling@Sun.COM 			}
169812296SLin.Ling@Sun.COM 		}
169912296SLin.Ling@Sun.COM 	}
170012296SLin.Ling@Sun.COM 
170112296SLin.Ling@Sun.COM 	if (needs_io && !zfs_no_scrub_io) {
170212296SLin.Ling@Sun.COM 		void *data = zio_data_buf_alloc(size);
170312296SLin.Ling@Sun.COM 
170412296SLin.Ling@Sun.COM 		mutex_enter(&spa->spa_scrub_lock);
170512296SLin.Ling@Sun.COM 		while (spa->spa_scrub_inflight >= spa->spa_scrub_maxinflight)
170612296SLin.Ling@Sun.COM 			cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
170712296SLin.Ling@Sun.COM 		spa->spa_scrub_inflight++;
170812296SLin.Ling@Sun.COM 		mutex_exit(&spa->spa_scrub_lock);
170912296SLin.Ling@Sun.COM 
171012296SLin.Ling@Sun.COM 		zio_nowait(zio_read(NULL, spa, bp, data, size,
171112296SLin.Ling@Sun.COM 		    dsl_scan_scrub_done, NULL, zio_priority,
171212296SLin.Ling@Sun.COM 		    zio_flags, zb));
171312296SLin.Ling@Sun.COM 	}
171412296SLin.Ling@Sun.COM 
171512296SLin.Ling@Sun.COM 	/* do not relocate this block */
171612296SLin.Ling@Sun.COM 	return (0);
171712296SLin.Ling@Sun.COM }
171812296SLin.Ling@Sun.COM 
171912296SLin.Ling@Sun.COM int
172012296SLin.Ling@Sun.COM dsl_scan(dsl_pool_t *dp, pool_scan_func_t func)
172112296SLin.Ling@Sun.COM {
172212296SLin.Ling@Sun.COM 	spa_t *spa = dp->dp_spa;
172312296SLin.Ling@Sun.COM 
172412296SLin.Ling@Sun.COM 	/*
172512296SLin.Ling@Sun.COM 	 * Purge all vdev caches and probe all devices.  We do this here
172612296SLin.Ling@Sun.COM 	 * rather than in sync context because this requires a writer lock
172712296SLin.Ling@Sun.COM 	 * on the spa_config lock, which we can't do from sync context.  The
172812296SLin.Ling@Sun.COM 	 * spa_scrub_reopen flag indicates that vdev_open() should not
172912296SLin.Ling@Sun.COM 	 * attempt to start another scrub.
173012296SLin.Ling@Sun.COM 	 */
173112296SLin.Ling@Sun.COM 	spa_vdev_state_enter(spa, SCL_NONE);
173212296SLin.Ling@Sun.COM 	spa->spa_scrub_reopen = B_TRUE;
173312296SLin.Ling@Sun.COM 	vdev_reopen(spa->spa_root_vdev);
173412296SLin.Ling@Sun.COM 	spa->spa_scrub_reopen = B_FALSE;
173512296SLin.Ling@Sun.COM 	(void) spa_vdev_state_exit(spa, NULL, 0);
173612296SLin.Ling@Sun.COM 
173712296SLin.Ling@Sun.COM 	return (dsl_sync_task_do(dp, dsl_scan_setup_check,
173812296SLin.Ling@Sun.COM 	    dsl_scan_setup_sync, dp->dp_scan, &func, 0));
173912296SLin.Ling@Sun.COM }
1740