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