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
5912586SGeorge.Wilson@Sun.COM int zfs_top_maxinflight = 32; /* maximum I/Os per top-level */
6012586SGeorge.Wilson@Sun.COM int zfs_resilver_delay = 2; /* number of ticks to delay resilver */
6112586SGeorge.Wilson@Sun.COM int zfs_scrub_delay = 4; /* number of ticks to delay scrub */
6212586SGeorge.Wilson@Sun.COM int zfs_scan_idle = 50; /* idle window in clock ticks */
6312586SGeorge.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
dsl_scan_init(dsl_pool_t * dp,uint64_t txg)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
dsl_scan_fini(dsl_pool_t * dp)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
dsl_scan_setup_check(void * arg1,void * arg2,dmu_tx_t * tx)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
dsl_scan_setup_sync(void * arg1,void * arg2,dmu_tx_t * tx)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
dsl_scan_done(dsl_scan_t * scn,boolean_t complete,dmu_tx_t * tx)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
dsl_scan_cancel_check(void * arg1,void * arg2,dmu_tx_t * tx)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
dsl_scan_cancel_sync(void * arg1,void * arg2,dmu_tx_t * tx)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
dsl_scan_cancel(dsl_pool_t * dp)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
dsl_free(dsl_pool_t * dp,uint64_t txg,const blkptr_t * bp)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
dsl_free_sync(zio_t * pio,dsl_pool_t * dp,uint64_t txg,const blkptr_t * bpp)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
dsl_read(zio_t * pio,spa_t * spa,const blkptr_t * bpp,arc_buf_t * pbuf,arc_done_func_t * done,void * private,int priority,int zio_flags,uint32_t * arc_flags,const zbookmark_t * zb)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
dsl_read_nolock(zio_t * pio,spa_t * spa,const blkptr_t * bpp,arc_done_func_t * done,void * private,int priority,int zio_flags,uint32_t * arc_flags,const zbookmark_t * zb)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
bookmark_is_zero(const zbookmark_t * zb)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
bookmark_is_before(const dnode_phys_t * dnp,const zbookmark_t * zb1,const zbookmark_t * zb2)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
dsl_scan_ds_maxtxg(dsl_dataset_t * ds)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
dsl_scan_sync_state(dsl_scan_t * scn,dmu_tx_t * tx)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
dsl_scan_check_pause(dsl_scan_t * scn,const zbookmark_t * zb)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
dsl_scan_zil_block(zilog_t * zilog,blkptr_t * bp,void * arg,uint64_t claim_txg)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
dsl_scan_zil_record(zilog_t * zilog,lr_t * lrc,void * arg,uint64_t claim_txg)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
dsl_scan_zil(dsl_pool_t * dp,zil_header_t * zh)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
dsl_scan_prefetch(dsl_scan_t * scn,arc_buf_t * buf,blkptr_t * bp,uint64_t objset,uint64_t object,uint64_t blkid)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,
60912586SGeorge.Wilson@Sun.COM buf, NULL, NULL, ZIO_PRIORITY_ASYNC_READ,
61012586SGeorge.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
dsl_scan_check_resume(dsl_scan_t * scn,const dnode_phys_t * dnp,const zbookmark_t * zb)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
dsl_scan_recurse(dsl_scan_t * scn,dsl_dataset_t * ds,dmu_objset_type_t ostype,dnode_phys_t * dnp,const blkptr_t * bp,const zbookmark_t * zb,dmu_tx_t * tx,arc_buf_t ** bufp)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;
65812586SGeorge.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,
66912586SGeorge.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,
69212586SGeorge.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,
70512586SGeorge.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,
72812586SGeorge.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 dsl_scan_visitdnode(scn, ds, osp->os_type,
73712296SLin.Ling@Sun.COM &osp->os_meta_dnode, *bufp, DMU_META_DNODE_OBJECT, tx);
73812296SLin.Ling@Sun.COM
73912296SLin.Ling@Sun.COM if (OBJSET_BUF_HAS_USERUSED(*bufp)) {
74012296SLin.Ling@Sun.COM /*
74112296SLin.Ling@Sun.COM * We also always visit user/group accounting
74212296SLin.Ling@Sun.COM * objects, and never skip them, even if we are
74312296SLin.Ling@Sun.COM * pausing. This is necessary so that the space
74412296SLin.Ling@Sun.COM * deltas from this txg get integrated.
74512296SLin.Ling@Sun.COM */
74612296SLin.Ling@Sun.COM dsl_scan_visitdnode(scn, ds, osp->os_type,
74712296SLin.Ling@Sun.COM &osp->os_groupused_dnode, *bufp,
74812296SLin.Ling@Sun.COM DMU_GROUPUSED_OBJECT, tx);
74912296SLin.Ling@Sun.COM dsl_scan_visitdnode(scn, ds, osp->os_type,
75012296SLin.Ling@Sun.COM &osp->os_userused_dnode, *bufp,
75112296SLin.Ling@Sun.COM DMU_USERUSED_OBJECT, tx);
75212296SLin.Ling@Sun.COM }
75312296SLin.Ling@Sun.COM }
75412296SLin.Ling@Sun.COM
75512296SLin.Ling@Sun.COM return (0);
75612296SLin.Ling@Sun.COM }
75712296SLin.Ling@Sun.COM
75812296SLin.Ling@Sun.COM static void
dsl_scan_visitdnode(dsl_scan_t * scn,dsl_dataset_t * ds,dmu_objset_type_t ostype,dnode_phys_t * dnp,arc_buf_t * buf,uint64_t object,dmu_tx_t * tx)75912296SLin.Ling@Sun.COM dsl_scan_visitdnode(dsl_scan_t *scn, dsl_dataset_t *ds,
76012296SLin.Ling@Sun.COM dmu_objset_type_t ostype, dnode_phys_t *dnp, arc_buf_t *buf,
76112296SLin.Ling@Sun.COM uint64_t object, dmu_tx_t *tx)
76212296SLin.Ling@Sun.COM {
76312296SLin.Ling@Sun.COM int j;
76412296SLin.Ling@Sun.COM
76512296SLin.Ling@Sun.COM for (j = 0; j < dnp->dn_nblkptr; j++) {
76612296SLin.Ling@Sun.COM zbookmark_t czb;
76712296SLin.Ling@Sun.COM
76812296SLin.Ling@Sun.COM SET_BOOKMARK(&czb, ds ? ds->ds_object : 0, object,
76912296SLin.Ling@Sun.COM dnp->dn_nlevels - 1, j);
77012296SLin.Ling@Sun.COM dsl_scan_visitbp(&dnp->dn_blkptr[j],
77112296SLin.Ling@Sun.COM &czb, dnp, buf, ds, scn, ostype, tx);
77212296SLin.Ling@Sun.COM }
77312296SLin.Ling@Sun.COM
77412296SLin.Ling@Sun.COM if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
77512296SLin.Ling@Sun.COM zbookmark_t czb;
77612296SLin.Ling@Sun.COM SET_BOOKMARK(&czb, ds ? ds->ds_object : 0, object,
77712296SLin.Ling@Sun.COM 0, DMU_SPILL_BLKID);
77812296SLin.Ling@Sun.COM dsl_scan_visitbp(&dnp->dn_spill,
77912296SLin.Ling@Sun.COM &czb, dnp, buf, ds, scn, ostype, tx);
78012296SLin.Ling@Sun.COM }
78112296SLin.Ling@Sun.COM }
78212296SLin.Ling@Sun.COM
78312296SLin.Ling@Sun.COM /*
78412296SLin.Ling@Sun.COM * The arguments are in this order because mdb can only print the
78512296SLin.Ling@Sun.COM * first 5; we want them to be useful.
78612296SLin.Ling@Sun.COM */
78712296SLin.Ling@Sun.COM static void
dsl_scan_visitbp(blkptr_t * bp,const zbookmark_t * zb,dnode_phys_t * dnp,arc_buf_t * pbuf,dsl_dataset_t * ds,dsl_scan_t * scn,dmu_objset_type_t ostype,dmu_tx_t * tx)78812296SLin.Ling@Sun.COM dsl_scan_visitbp(blkptr_t *bp, const zbookmark_t *zb,
78912296SLin.Ling@Sun.COM dnode_phys_t *dnp, arc_buf_t *pbuf,
79012296SLin.Ling@Sun.COM dsl_dataset_t *ds, dsl_scan_t *scn, dmu_objset_type_t ostype,
79112296SLin.Ling@Sun.COM dmu_tx_t *tx)
79212296SLin.Ling@Sun.COM {
79312296SLin.Ling@Sun.COM dsl_pool_t *dp = scn->scn_dp;
79412296SLin.Ling@Sun.COM arc_buf_t *buf = NULL;
79512296SLin.Ling@Sun.COM blkptr_t bp_toread = *bp;
79612296SLin.Ling@Sun.COM
79712296SLin.Ling@Sun.COM /* ASSERT(pbuf == NULL || arc_released(pbuf)); */
79812296SLin.Ling@Sun.COM
79912296SLin.Ling@Sun.COM if (dsl_scan_check_pause(scn, zb))
80012296SLin.Ling@Sun.COM return;
80112296SLin.Ling@Sun.COM
80212296SLin.Ling@Sun.COM if (dsl_scan_check_resume(scn, dnp, zb))
80312296SLin.Ling@Sun.COM return;
80412296SLin.Ling@Sun.COM
80512296SLin.Ling@Sun.COM if (bp->blk_birth == 0)
80612296SLin.Ling@Sun.COM return;
80712296SLin.Ling@Sun.COM
80812296SLin.Ling@Sun.COM scn->scn_visited_this_txg++;
80912296SLin.Ling@Sun.COM
81012296SLin.Ling@Sun.COM dprintf_bp(bp,
81112296SLin.Ling@Sun.COM "visiting ds=%p/%llu zb=%llx/%llx/%llx/%llx buf=%p bp=%p",
81212296SLin.Ling@Sun.COM ds, ds ? ds->ds_object : 0,
81312296SLin.Ling@Sun.COM zb->zb_objset, zb->zb_object, zb->zb_level, zb->zb_blkid,
81412296SLin.Ling@Sun.COM pbuf, bp);
81512296SLin.Ling@Sun.COM
81612296SLin.Ling@Sun.COM if (bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
81712296SLin.Ling@Sun.COM return;
81812296SLin.Ling@Sun.COM
81912296SLin.Ling@Sun.COM if (BP_GET_TYPE(bp) != DMU_OT_USERGROUP_USED) {
82012296SLin.Ling@Sun.COM /*
82112296SLin.Ling@Sun.COM * For non-user-accounting blocks, we need to read the
82212296SLin.Ling@Sun.COM * new bp (from a deleted snapshot, found in
82312296SLin.Ling@Sun.COM * check_existing_xlation). If we used the old bp,
82412296SLin.Ling@Sun.COM * pointers inside this block from before we resumed
82512296SLin.Ling@Sun.COM * would be untranslated.
82612296SLin.Ling@Sun.COM *
82712296SLin.Ling@Sun.COM * For user-accounting blocks, we need to read the old
82812296SLin.Ling@Sun.COM * bp, because we will apply the entire space delta to
82912296SLin.Ling@Sun.COM * it (original untranslated -> translations from
83012296SLin.Ling@Sun.COM * deleted snap -> now).
83112296SLin.Ling@Sun.COM */
83212296SLin.Ling@Sun.COM bp_toread = *bp;
83312296SLin.Ling@Sun.COM }
83412296SLin.Ling@Sun.COM
83512296SLin.Ling@Sun.COM if (dsl_scan_recurse(scn, ds, ostype, dnp, &bp_toread, zb, tx,
83612296SLin.Ling@Sun.COM &buf) != 0)
83712296SLin.Ling@Sun.COM return;
83812296SLin.Ling@Sun.COM
83912296SLin.Ling@Sun.COM /*
84012296SLin.Ling@Sun.COM * If dsl_scan_ddt() has aready visited this block, it will have
84112296SLin.Ling@Sun.COM * already done any translations or scrubbing, so don't call the
84212296SLin.Ling@Sun.COM * callback again.
84312296SLin.Ling@Sun.COM */
84412296SLin.Ling@Sun.COM if (ddt_class_contains(dp->dp_spa,
84512296SLin.Ling@Sun.COM scn->scn_phys.scn_ddt_class_max, bp)) {
84612296SLin.Ling@Sun.COM ASSERT(buf == NULL);
84712296SLin.Ling@Sun.COM return;
84812296SLin.Ling@Sun.COM }
84912296SLin.Ling@Sun.COM
85012296SLin.Ling@Sun.COM /*
85112296SLin.Ling@Sun.COM * If this block is from the future (after cur_max_txg), then we
85212296SLin.Ling@Sun.COM * are doing this on behalf of a deleted snapshot, and we will
85312296SLin.Ling@Sun.COM * revisit the future block on the next pass of this dataset.
85412296SLin.Ling@Sun.COM * Don't scan it now unless we need to because something
85512296SLin.Ling@Sun.COM * under it was modified.
85612296SLin.Ling@Sun.COM */
85712296SLin.Ling@Sun.COM if (bp->blk_birth <= scn->scn_phys.scn_cur_max_txg) {
85812296SLin.Ling@Sun.COM scan_funcs[scn->scn_phys.scn_func](dp, bp, zb);
85912296SLin.Ling@Sun.COM }
86012296SLin.Ling@Sun.COM if (buf)
86112296SLin.Ling@Sun.COM (void) arc_buf_remove_ref(buf, &buf);
86212296SLin.Ling@Sun.COM }
86312296SLin.Ling@Sun.COM
86412296SLin.Ling@Sun.COM static void
dsl_scan_visit_rootbp(dsl_scan_t * scn,dsl_dataset_t * ds,blkptr_t * bp,dmu_tx_t * tx)86512296SLin.Ling@Sun.COM dsl_scan_visit_rootbp(dsl_scan_t *scn, dsl_dataset_t *ds, blkptr_t *bp,
86612296SLin.Ling@Sun.COM dmu_tx_t *tx)
86712296SLin.Ling@Sun.COM {
86812296SLin.Ling@Sun.COM zbookmark_t zb;
86912296SLin.Ling@Sun.COM
87012296SLin.Ling@Sun.COM SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
87112296SLin.Ling@Sun.COM ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
87212296SLin.Ling@Sun.COM dsl_scan_visitbp(bp, &zb, NULL, NULL,
87312296SLin.Ling@Sun.COM ds, scn, DMU_OST_NONE, tx);
87412296SLin.Ling@Sun.COM
87512296SLin.Ling@Sun.COM dprintf_ds(ds, "finished scan%s", "");
87612296SLin.Ling@Sun.COM }
87712296SLin.Ling@Sun.COM
87812296SLin.Ling@Sun.COM void
dsl_scan_ds_destroyed(dsl_dataset_t * ds,dmu_tx_t * tx)87912296SLin.Ling@Sun.COM dsl_scan_ds_destroyed(dsl_dataset_t *ds, dmu_tx_t *tx)
88012296SLin.Ling@Sun.COM {
88112296SLin.Ling@Sun.COM dsl_pool_t *dp = ds->ds_dir->dd_pool;
88212296SLin.Ling@Sun.COM dsl_scan_t *scn = dp->dp_scan;
88312296SLin.Ling@Sun.COM uint64_t mintxg;
88412296SLin.Ling@Sun.COM
88512296SLin.Ling@Sun.COM if (scn->scn_phys.scn_state != DSS_SCANNING)
88612296SLin.Ling@Sun.COM return;
88712296SLin.Ling@Sun.COM
88812296SLin.Ling@Sun.COM if (scn->scn_phys.scn_bookmark.zb_objset == ds->ds_object) {
88912296SLin.Ling@Sun.COM if (dsl_dataset_is_snapshot(ds)) {
89012296SLin.Ling@Sun.COM /* Note, scn_cur_{min,max}_txg stays the same. */
89112296SLin.Ling@Sun.COM scn->scn_phys.scn_bookmark.zb_objset =
89212296SLin.Ling@Sun.COM ds->ds_phys->ds_next_snap_obj;
89312296SLin.Ling@Sun.COM zfs_dbgmsg("destroying ds %llu; currently traversing; "
89412296SLin.Ling@Sun.COM "reset zb_objset to %llu",
89512296SLin.Ling@Sun.COM (u_longlong_t)ds->ds_object,
89612296SLin.Ling@Sun.COM (u_longlong_t)ds->ds_phys->ds_next_snap_obj);
89712296SLin.Ling@Sun.COM scn->scn_phys.scn_flags |= DSF_VISIT_DS_AGAIN;
89812296SLin.Ling@Sun.COM } else {
89912296SLin.Ling@Sun.COM SET_BOOKMARK(&scn->scn_phys.scn_bookmark,
90012296SLin.Ling@Sun.COM ZB_DESTROYED_OBJSET, 0, 0, 0);
90112296SLin.Ling@Sun.COM zfs_dbgmsg("destroying ds %llu; currently traversing; "
90212296SLin.Ling@Sun.COM "reset bookmark to -1,0,0,0",
90312296SLin.Ling@Sun.COM (u_longlong_t)ds->ds_object);
90412296SLin.Ling@Sun.COM }
90512296SLin.Ling@Sun.COM } else if (zap_lookup_int_key(dp->dp_meta_objset,
90612296SLin.Ling@Sun.COM scn->scn_phys.scn_queue_obj, ds->ds_object, &mintxg) == 0) {
90712296SLin.Ling@Sun.COM ASSERT3U(ds->ds_phys->ds_num_children, <=, 1);
90812296SLin.Ling@Sun.COM VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
90912296SLin.Ling@Sun.COM scn->scn_phys.scn_queue_obj, ds->ds_object, tx));
91012296SLin.Ling@Sun.COM if (dsl_dataset_is_snapshot(ds)) {
91112296SLin.Ling@Sun.COM /*
91212296SLin.Ling@Sun.COM * We keep the same mintxg; it could be >
91312296SLin.Ling@Sun.COM * ds_creation_txg if the previous snapshot was
91412296SLin.Ling@Sun.COM * deleted too.
91512296SLin.Ling@Sun.COM */
91612296SLin.Ling@Sun.COM VERIFY(zap_add_int_key(dp->dp_meta_objset,
91712296SLin.Ling@Sun.COM scn->scn_phys.scn_queue_obj,
91812296SLin.Ling@Sun.COM ds->ds_phys->ds_next_snap_obj, mintxg, tx) == 0);
91912296SLin.Ling@Sun.COM zfs_dbgmsg("destroying ds %llu; in queue; "
92012296SLin.Ling@Sun.COM "replacing with %llu",
92112296SLin.Ling@Sun.COM (u_longlong_t)ds->ds_object,
92212296SLin.Ling@Sun.COM (u_longlong_t)ds->ds_phys->ds_next_snap_obj);
92312296SLin.Ling@Sun.COM } else {
92412296SLin.Ling@Sun.COM zfs_dbgmsg("destroying ds %llu; in queue; removing",
92512296SLin.Ling@Sun.COM (u_longlong_t)ds->ds_object);
92612296SLin.Ling@Sun.COM }
92712296SLin.Ling@Sun.COM } else {
92812296SLin.Ling@Sun.COM zfs_dbgmsg("destroying ds %llu; ignoring",
92912296SLin.Ling@Sun.COM (u_longlong_t)ds->ds_object);
93012296SLin.Ling@Sun.COM }
93112296SLin.Ling@Sun.COM
93212296SLin.Ling@Sun.COM /*
93312296SLin.Ling@Sun.COM * dsl_scan_sync() should be called after this, and should sync
93412296SLin.Ling@Sun.COM * out our changed state, but just to be safe, do it here.
93512296SLin.Ling@Sun.COM */
93612296SLin.Ling@Sun.COM dsl_scan_sync_state(scn, tx);
93712296SLin.Ling@Sun.COM }
93812296SLin.Ling@Sun.COM
93912296SLin.Ling@Sun.COM void
dsl_scan_ds_snapshotted(dsl_dataset_t * ds,dmu_tx_t * tx)94012296SLin.Ling@Sun.COM dsl_scan_ds_snapshotted(dsl_dataset_t *ds, dmu_tx_t *tx)
94112296SLin.Ling@Sun.COM {
94212296SLin.Ling@Sun.COM dsl_pool_t *dp = ds->ds_dir->dd_pool;
94312296SLin.Ling@Sun.COM dsl_scan_t *scn = dp->dp_scan;
94412296SLin.Ling@Sun.COM uint64_t mintxg;
94512296SLin.Ling@Sun.COM
94612296SLin.Ling@Sun.COM if (scn->scn_phys.scn_state != DSS_SCANNING)
94712296SLin.Ling@Sun.COM return;
94812296SLin.Ling@Sun.COM
94912296SLin.Ling@Sun.COM ASSERT(ds->ds_phys->ds_prev_snap_obj != 0);
95012296SLin.Ling@Sun.COM
95112296SLin.Ling@Sun.COM if (scn->scn_phys.scn_bookmark.zb_objset == ds->ds_object) {
95212296SLin.Ling@Sun.COM scn->scn_phys.scn_bookmark.zb_objset =
95312296SLin.Ling@Sun.COM ds->ds_phys->ds_prev_snap_obj;
95412296SLin.Ling@Sun.COM zfs_dbgmsg("snapshotting ds %llu; currently traversing; "
95512296SLin.Ling@Sun.COM "reset zb_objset to %llu",
95612296SLin.Ling@Sun.COM (u_longlong_t)ds->ds_object,
95712296SLin.Ling@Sun.COM (u_longlong_t)ds->ds_phys->ds_prev_snap_obj);
95812296SLin.Ling@Sun.COM } else if (zap_lookup_int_key(dp->dp_meta_objset,
95912296SLin.Ling@Sun.COM scn->scn_phys.scn_queue_obj, ds->ds_object, &mintxg) == 0) {
96012296SLin.Ling@Sun.COM VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
96112296SLin.Ling@Sun.COM scn->scn_phys.scn_queue_obj, ds->ds_object, tx));
96212296SLin.Ling@Sun.COM VERIFY(zap_add_int_key(dp->dp_meta_objset,
96312296SLin.Ling@Sun.COM scn->scn_phys.scn_queue_obj,
96412296SLin.Ling@Sun.COM ds->ds_phys->ds_prev_snap_obj, mintxg, tx) == 0);
96512296SLin.Ling@Sun.COM zfs_dbgmsg("snapshotting ds %llu; in queue; "
96612296SLin.Ling@Sun.COM "replacing with %llu",
96712296SLin.Ling@Sun.COM (u_longlong_t)ds->ds_object,
96812296SLin.Ling@Sun.COM (u_longlong_t)ds->ds_phys->ds_prev_snap_obj);
96912296SLin.Ling@Sun.COM }
97012296SLin.Ling@Sun.COM dsl_scan_sync_state(scn, tx);
97112296SLin.Ling@Sun.COM }
97212296SLin.Ling@Sun.COM
97312296SLin.Ling@Sun.COM void
dsl_scan_ds_clone_swapped(dsl_dataset_t * ds1,dsl_dataset_t * ds2,dmu_tx_t * tx)97412296SLin.Ling@Sun.COM dsl_scan_ds_clone_swapped(dsl_dataset_t *ds1, dsl_dataset_t *ds2, dmu_tx_t *tx)
97512296SLin.Ling@Sun.COM {
97612296SLin.Ling@Sun.COM dsl_pool_t *dp = ds1->ds_dir->dd_pool;
97712296SLin.Ling@Sun.COM dsl_scan_t *scn = dp->dp_scan;
97812296SLin.Ling@Sun.COM uint64_t mintxg;
97912296SLin.Ling@Sun.COM
98012296SLin.Ling@Sun.COM if (scn->scn_phys.scn_state != DSS_SCANNING)
98112296SLin.Ling@Sun.COM return;
98212296SLin.Ling@Sun.COM
98312296SLin.Ling@Sun.COM if (scn->scn_phys.scn_bookmark.zb_objset == ds1->ds_object) {
98412296SLin.Ling@Sun.COM scn->scn_phys.scn_bookmark.zb_objset = ds2->ds_object;
98512296SLin.Ling@Sun.COM zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
98612296SLin.Ling@Sun.COM "reset zb_objset to %llu",
98712296SLin.Ling@Sun.COM (u_longlong_t)ds1->ds_object,
98812296SLin.Ling@Sun.COM (u_longlong_t)ds2->ds_object);
98912296SLin.Ling@Sun.COM } else if (scn->scn_phys.scn_bookmark.zb_objset == ds2->ds_object) {
99012296SLin.Ling@Sun.COM scn->scn_phys.scn_bookmark.zb_objset = ds1->ds_object;
99112296SLin.Ling@Sun.COM zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
99212296SLin.Ling@Sun.COM "reset zb_objset to %llu",
99312296SLin.Ling@Sun.COM (u_longlong_t)ds2->ds_object,
99412296SLin.Ling@Sun.COM (u_longlong_t)ds1->ds_object);
99512296SLin.Ling@Sun.COM }
99612296SLin.Ling@Sun.COM
99712296SLin.Ling@Sun.COM if (zap_lookup_int_key(dp->dp_meta_objset, scn->scn_phys.scn_queue_obj,
99812296SLin.Ling@Sun.COM ds1->ds_object, &mintxg) == 0) {
99912296SLin.Ling@Sun.COM int err;
100012296SLin.Ling@Sun.COM
100112296SLin.Ling@Sun.COM ASSERT3U(mintxg, ==, ds1->ds_phys->ds_prev_snap_txg);
100212296SLin.Ling@Sun.COM ASSERT3U(mintxg, ==, ds2->ds_phys->ds_prev_snap_txg);
100312296SLin.Ling@Sun.COM VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
100412296SLin.Ling@Sun.COM scn->scn_phys.scn_queue_obj, ds1->ds_object, tx));
100512296SLin.Ling@Sun.COM err = zap_add_int_key(dp->dp_meta_objset,
100612296SLin.Ling@Sun.COM scn->scn_phys.scn_queue_obj, ds2->ds_object, mintxg, tx);
100712296SLin.Ling@Sun.COM VERIFY(err == 0 || err == EEXIST);
100812296SLin.Ling@Sun.COM if (err == EEXIST) {
100912296SLin.Ling@Sun.COM /* Both were there to begin with */
101012296SLin.Ling@Sun.COM VERIFY(0 == zap_add_int_key(dp->dp_meta_objset,
101112296SLin.Ling@Sun.COM scn->scn_phys.scn_queue_obj,
101212296SLin.Ling@Sun.COM ds1->ds_object, mintxg, tx));
101312296SLin.Ling@Sun.COM }
101412296SLin.Ling@Sun.COM zfs_dbgmsg("clone_swap ds %llu; in queue; "
101512296SLin.Ling@Sun.COM "replacing with %llu",
101612296SLin.Ling@Sun.COM (u_longlong_t)ds1->ds_object,
101712296SLin.Ling@Sun.COM (u_longlong_t)ds2->ds_object);
101812296SLin.Ling@Sun.COM } else if (zap_lookup_int_key(dp->dp_meta_objset,
101912296SLin.Ling@Sun.COM scn->scn_phys.scn_queue_obj, ds2->ds_object, &mintxg) == 0) {
102012296SLin.Ling@Sun.COM ASSERT3U(mintxg, ==, ds1->ds_phys->ds_prev_snap_txg);
102112296SLin.Ling@Sun.COM ASSERT3U(mintxg, ==, ds2->ds_phys->ds_prev_snap_txg);
102212296SLin.Ling@Sun.COM VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
102312296SLin.Ling@Sun.COM scn->scn_phys.scn_queue_obj, ds2->ds_object, tx));
102412296SLin.Ling@Sun.COM VERIFY(0 == zap_add_int_key(dp->dp_meta_objset,
102512296SLin.Ling@Sun.COM scn->scn_phys.scn_queue_obj, ds1->ds_object, mintxg, tx));
102612296SLin.Ling@Sun.COM zfs_dbgmsg("clone_swap ds %llu; in queue; "
102712296SLin.Ling@Sun.COM "replacing with %llu",
102812296SLin.Ling@Sun.COM (u_longlong_t)ds2->ds_object,
102912296SLin.Ling@Sun.COM (u_longlong_t)ds1->ds_object);
103012296SLin.Ling@Sun.COM }
103112296SLin.Ling@Sun.COM
103212296SLin.Ling@Sun.COM dsl_scan_sync_state(scn, tx);
103312296SLin.Ling@Sun.COM }
103412296SLin.Ling@Sun.COM
103512296SLin.Ling@Sun.COM struct enqueue_clones_arg {
103612296SLin.Ling@Sun.COM dmu_tx_t *tx;
103712296SLin.Ling@Sun.COM uint64_t originobj;
103812296SLin.Ling@Sun.COM };
103912296SLin.Ling@Sun.COM
104012296SLin.Ling@Sun.COM /* ARGSUSED */
104112296SLin.Ling@Sun.COM static int
enqueue_clones_cb(spa_t * spa,uint64_t dsobj,const char * dsname,void * arg)104212296SLin.Ling@Sun.COM enqueue_clones_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
104312296SLin.Ling@Sun.COM {
104412296SLin.Ling@Sun.COM struct enqueue_clones_arg *eca = arg;
104512296SLin.Ling@Sun.COM dsl_dataset_t *ds;
104612296SLin.Ling@Sun.COM int err;
104712296SLin.Ling@Sun.COM dsl_pool_t *dp = spa->spa_dsl_pool;
104812296SLin.Ling@Sun.COM dsl_scan_t *scn = dp->dp_scan;
104912296SLin.Ling@Sun.COM
105012296SLin.Ling@Sun.COM err = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds);
105112296SLin.Ling@Sun.COM if (err)
105212296SLin.Ling@Sun.COM return (err);
105312296SLin.Ling@Sun.COM
105412296SLin.Ling@Sun.COM if (ds->ds_dir->dd_phys->dd_origin_obj == eca->originobj) {
105512296SLin.Ling@Sun.COM while (ds->ds_phys->ds_prev_snap_obj != eca->originobj) {
105612296SLin.Ling@Sun.COM dsl_dataset_t *prev;
105712296SLin.Ling@Sun.COM err = dsl_dataset_hold_obj(dp,
105812296SLin.Ling@Sun.COM ds->ds_phys->ds_prev_snap_obj, FTAG, &prev);
105912296SLin.Ling@Sun.COM
106012296SLin.Ling@Sun.COM dsl_dataset_rele(ds, FTAG);
106112296SLin.Ling@Sun.COM if (err)
106212296SLin.Ling@Sun.COM return (err);
106312296SLin.Ling@Sun.COM ds = prev;
106412296SLin.Ling@Sun.COM }
106512296SLin.Ling@Sun.COM VERIFY(zap_add_int_key(dp->dp_meta_objset,
106612296SLin.Ling@Sun.COM scn->scn_phys.scn_queue_obj, ds->ds_object,
106712296SLin.Ling@Sun.COM ds->ds_phys->ds_prev_snap_txg, eca->tx) == 0);
106812296SLin.Ling@Sun.COM }
106912296SLin.Ling@Sun.COM dsl_dataset_rele(ds, FTAG);
107012296SLin.Ling@Sun.COM return (0);
107112296SLin.Ling@Sun.COM }
107212296SLin.Ling@Sun.COM
107312296SLin.Ling@Sun.COM static void
dsl_scan_visitds(dsl_scan_t * scn,uint64_t dsobj,dmu_tx_t * tx)107412296SLin.Ling@Sun.COM dsl_scan_visitds(dsl_scan_t *scn, uint64_t dsobj, dmu_tx_t *tx)
107512296SLin.Ling@Sun.COM {
107612296SLin.Ling@Sun.COM dsl_pool_t *dp = scn->scn_dp;
107712296SLin.Ling@Sun.COM dsl_dataset_t *ds;
1078*12827SMatthew.Ahrens@Sun.COM objset_t *os;
107912296SLin.Ling@Sun.COM
108012296SLin.Ling@Sun.COM VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
108112296SLin.Ling@Sun.COM
1082*12827SMatthew.Ahrens@Sun.COM if (dmu_objset_from_ds(ds, &os))
1083*12827SMatthew.Ahrens@Sun.COM goto out;
1084*12827SMatthew.Ahrens@Sun.COM
1085*12827SMatthew.Ahrens@Sun.COM /*
1086*12827SMatthew.Ahrens@Sun.COM * Only the ZIL in the head (non-snapshot) is valid. Even though
1087*12827SMatthew.Ahrens@Sun.COM * snapshots can have ZIL block pointers (which may be the same
1088*12827SMatthew.Ahrens@Sun.COM * BP as in the head), they must be ignored. So we traverse the
1089*12827SMatthew.Ahrens@Sun.COM * ZIL here, rather than in scan_recurse(), because the regular
1090*12827SMatthew.Ahrens@Sun.COM * snapshot block-sharing rules don't apply to it.
1091*12827SMatthew.Ahrens@Sun.COM */
1092*12827SMatthew.Ahrens@Sun.COM if (DSL_SCAN_IS_SCRUB_RESILVER(scn) && !dsl_dataset_is_snapshot(ds))
1093*12827SMatthew.Ahrens@Sun.COM dsl_scan_zil(dp, &os->os_zil_header);
1094*12827SMatthew.Ahrens@Sun.COM
109512296SLin.Ling@Sun.COM /*
109612296SLin.Ling@Sun.COM * Iterate over the bps in this ds.
109712296SLin.Ling@Sun.COM */
109812296SLin.Ling@Sun.COM dmu_buf_will_dirty(ds->ds_dbuf, tx);
109912296SLin.Ling@Sun.COM dsl_scan_visit_rootbp(scn, ds, &ds->ds_phys->ds_bp, tx);
110012296SLin.Ling@Sun.COM
110112296SLin.Ling@Sun.COM char *dsname = kmem_alloc(ZFS_MAXNAMELEN, KM_SLEEP);
110212296SLin.Ling@Sun.COM dsl_dataset_name(ds, dsname);
110312296SLin.Ling@Sun.COM zfs_dbgmsg("scanned dataset %llu (%s) with min=%llu max=%llu; "
110412296SLin.Ling@Sun.COM "pausing=%u",
110512296SLin.Ling@Sun.COM (longlong_t)dsobj, dsname,
110612296SLin.Ling@Sun.COM (longlong_t)scn->scn_phys.scn_cur_min_txg,
110712296SLin.Ling@Sun.COM (longlong_t)scn->scn_phys.scn_cur_max_txg,
110812296SLin.Ling@Sun.COM (int)scn->scn_pausing);
110912296SLin.Ling@Sun.COM kmem_free(dsname, ZFS_MAXNAMELEN);
111012296SLin.Ling@Sun.COM
111112296SLin.Ling@Sun.COM if (scn->scn_pausing)
111212296SLin.Ling@Sun.COM goto out;
111312296SLin.Ling@Sun.COM
111412296SLin.Ling@Sun.COM /*
111512296SLin.Ling@Sun.COM * We've finished this pass over this dataset.
111612296SLin.Ling@Sun.COM */
111712296SLin.Ling@Sun.COM
111812296SLin.Ling@Sun.COM /*
111912296SLin.Ling@Sun.COM * If we did not completely visit this dataset, do another pass.
112012296SLin.Ling@Sun.COM */
112112296SLin.Ling@Sun.COM if (scn->scn_phys.scn_flags & DSF_VISIT_DS_AGAIN) {
112212296SLin.Ling@Sun.COM zfs_dbgmsg("incomplete pass; visiting again");
112312296SLin.Ling@Sun.COM scn->scn_phys.scn_flags &= ~DSF_VISIT_DS_AGAIN;
112412296SLin.Ling@Sun.COM VERIFY(zap_add_int_key(dp->dp_meta_objset,
112512296SLin.Ling@Sun.COM scn->scn_phys.scn_queue_obj, ds->ds_object,
112612296SLin.Ling@Sun.COM scn->scn_phys.scn_cur_max_txg, tx) == 0);
112712296SLin.Ling@Sun.COM goto out;
112812296SLin.Ling@Sun.COM }
112912296SLin.Ling@Sun.COM
113012296SLin.Ling@Sun.COM /*
113112296SLin.Ling@Sun.COM * Add descendent datasets to work queue.
113212296SLin.Ling@Sun.COM */
113312296SLin.Ling@Sun.COM if (ds->ds_phys->ds_next_snap_obj != 0) {
113412296SLin.Ling@Sun.COM VERIFY(zap_add_int_key(dp->dp_meta_objset,
113512296SLin.Ling@Sun.COM scn->scn_phys.scn_queue_obj, ds->ds_phys->ds_next_snap_obj,
113612296SLin.Ling@Sun.COM ds->ds_phys->ds_creation_txg, tx) == 0);
113712296SLin.Ling@Sun.COM }
113812296SLin.Ling@Sun.COM if (ds->ds_phys->ds_num_children > 1) {
113912296SLin.Ling@Sun.COM boolean_t usenext = B_FALSE;
114012296SLin.Ling@Sun.COM if (ds->ds_phys->ds_next_clones_obj != 0) {
114112296SLin.Ling@Sun.COM uint64_t count;
114212296SLin.Ling@Sun.COM /*
114312296SLin.Ling@Sun.COM * A bug in a previous version of the code could
114412296SLin.Ling@Sun.COM * cause upgrade_clones_cb() to not set
114512296SLin.Ling@Sun.COM * ds_next_snap_obj when it should, leading to a
114612296SLin.Ling@Sun.COM * missing entry. Therefore we can only use the
114712296SLin.Ling@Sun.COM * next_clones_obj when its count is correct.
114812296SLin.Ling@Sun.COM */
114912296SLin.Ling@Sun.COM int err = zap_count(dp->dp_meta_objset,
115012296SLin.Ling@Sun.COM ds->ds_phys->ds_next_clones_obj, &count);
115112296SLin.Ling@Sun.COM if (err == 0 &&
115212296SLin.Ling@Sun.COM count == ds->ds_phys->ds_num_children - 1)
115312296SLin.Ling@Sun.COM usenext = B_TRUE;
115412296SLin.Ling@Sun.COM }
115512296SLin.Ling@Sun.COM
115612296SLin.Ling@Sun.COM if (usenext) {
115712296SLin.Ling@Sun.COM VERIFY(zap_join_key(dp->dp_meta_objset,
115812296SLin.Ling@Sun.COM ds->ds_phys->ds_next_clones_obj,
115912296SLin.Ling@Sun.COM scn->scn_phys.scn_queue_obj,
116012296SLin.Ling@Sun.COM ds->ds_phys->ds_creation_txg, tx) == 0);
116112296SLin.Ling@Sun.COM } else {
116212296SLin.Ling@Sun.COM struct enqueue_clones_arg eca;
116312296SLin.Ling@Sun.COM eca.tx = tx;
116412296SLin.Ling@Sun.COM eca.originobj = ds->ds_object;
116512296SLin.Ling@Sun.COM
116612296SLin.Ling@Sun.COM (void) dmu_objset_find_spa(ds->ds_dir->dd_pool->dp_spa,
116712296SLin.Ling@Sun.COM NULL, enqueue_clones_cb, &eca, DS_FIND_CHILDREN);
116812296SLin.Ling@Sun.COM }
116912296SLin.Ling@Sun.COM }
117012296SLin.Ling@Sun.COM
117112296SLin.Ling@Sun.COM out:
117212296SLin.Ling@Sun.COM dsl_dataset_rele(ds, FTAG);
117312296SLin.Ling@Sun.COM }
117412296SLin.Ling@Sun.COM
117512296SLin.Ling@Sun.COM /* ARGSUSED */
117612296SLin.Ling@Sun.COM static int
enqueue_cb(spa_t * spa,uint64_t dsobj,const char * dsname,void * arg)117712296SLin.Ling@Sun.COM enqueue_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
117812296SLin.Ling@Sun.COM {
117912296SLin.Ling@Sun.COM dmu_tx_t *tx = arg;
118012296SLin.Ling@Sun.COM dsl_dataset_t *ds;
118112296SLin.Ling@Sun.COM int err;
118212296SLin.Ling@Sun.COM dsl_pool_t *dp = spa->spa_dsl_pool;
118312296SLin.Ling@Sun.COM dsl_scan_t *scn = dp->dp_scan;
118412296SLin.Ling@Sun.COM
118512296SLin.Ling@Sun.COM err = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds);
118612296SLin.Ling@Sun.COM if (err)
118712296SLin.Ling@Sun.COM return (err);
118812296SLin.Ling@Sun.COM
118912296SLin.Ling@Sun.COM while (ds->ds_phys->ds_prev_snap_obj != 0) {
119012296SLin.Ling@Sun.COM dsl_dataset_t *prev;
119112296SLin.Ling@Sun.COM err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
119212296SLin.Ling@Sun.COM FTAG, &prev);
119312296SLin.Ling@Sun.COM if (err) {
119412296SLin.Ling@Sun.COM dsl_dataset_rele(ds, FTAG);
119512296SLin.Ling@Sun.COM return (err);
119612296SLin.Ling@Sun.COM }
119712296SLin.Ling@Sun.COM
119812296SLin.Ling@Sun.COM /*
119912296SLin.Ling@Sun.COM * If this is a clone, we don't need to worry about it for now.
120012296SLin.Ling@Sun.COM */
120112296SLin.Ling@Sun.COM if (prev->ds_phys->ds_next_snap_obj != ds->ds_object) {
120212296SLin.Ling@Sun.COM dsl_dataset_rele(ds, FTAG);
120312296SLin.Ling@Sun.COM dsl_dataset_rele(prev, FTAG);
120412296SLin.Ling@Sun.COM return (0);
120512296SLin.Ling@Sun.COM }
120612296SLin.Ling@Sun.COM dsl_dataset_rele(ds, FTAG);
120712296SLin.Ling@Sun.COM ds = prev;
120812296SLin.Ling@Sun.COM }
120912296SLin.Ling@Sun.COM
121012296SLin.Ling@Sun.COM VERIFY(zap_add_int_key(dp->dp_meta_objset, scn->scn_phys.scn_queue_obj,
121112296SLin.Ling@Sun.COM ds->ds_object, ds->ds_phys->ds_prev_snap_txg, tx) == 0);
121212296SLin.Ling@Sun.COM dsl_dataset_rele(ds, FTAG);
121312296SLin.Ling@Sun.COM return (0);
121412296SLin.Ling@Sun.COM }
121512296SLin.Ling@Sun.COM
121612296SLin.Ling@Sun.COM /*
121712296SLin.Ling@Sun.COM * Scrub/dedup interaction.
121812296SLin.Ling@Sun.COM *
121912296SLin.Ling@Sun.COM * If there are N references to a deduped block, we don't want to scrub it
122012296SLin.Ling@Sun.COM * N times -- ideally, we should scrub it exactly once.
122112296SLin.Ling@Sun.COM *
122212296SLin.Ling@Sun.COM * We leverage the fact that the dde's replication class (enum ddt_class)
122312296SLin.Ling@Sun.COM * is ordered from highest replication class (DDT_CLASS_DITTO) to lowest
122412296SLin.Ling@Sun.COM * (DDT_CLASS_UNIQUE) so that we may walk the DDT in that order.
122512296SLin.Ling@Sun.COM *
122612296SLin.Ling@Sun.COM * To prevent excess scrubbing, the scrub begins by walking the DDT
122712296SLin.Ling@Sun.COM * to find all blocks with refcnt > 1, and scrubs each of these once.
122812296SLin.Ling@Sun.COM * Since there are two replication classes which contain blocks with
122912296SLin.Ling@Sun.COM * refcnt > 1, we scrub the highest replication class (DDT_CLASS_DITTO) first.
123012296SLin.Ling@Sun.COM * Finally the top-down scrub begins, only visiting blocks with refcnt == 1.
123112296SLin.Ling@Sun.COM *
123212296SLin.Ling@Sun.COM * There would be nothing more to say if a block's refcnt couldn't change
123312296SLin.Ling@Sun.COM * during a scrub, but of course it can so we must account for changes
123412296SLin.Ling@Sun.COM * in a block's replication class.
123512296SLin.Ling@Sun.COM *
123612296SLin.Ling@Sun.COM * Here's an example of what can occur:
123712296SLin.Ling@Sun.COM *
123812296SLin.Ling@Sun.COM * If a block has refcnt > 1 during the DDT scrub phase, but has refcnt == 1
123912296SLin.Ling@Sun.COM * when visited during the top-down scrub phase, it will be scrubbed twice.
124012296SLin.Ling@Sun.COM * This negates our scrub optimization, but is otherwise harmless.
124112296SLin.Ling@Sun.COM *
124212296SLin.Ling@Sun.COM * If a block has refcnt == 1 during the DDT scrub phase, but has refcnt > 1
124312296SLin.Ling@Sun.COM * on each visit during the top-down scrub phase, it will never be scrubbed.
124412296SLin.Ling@Sun.COM * To catch this, ddt_sync_entry() notifies the scrub code whenever a block's
124512296SLin.Ling@Sun.COM * reference class transitions to a higher level (i.e DDT_CLASS_UNIQUE to
124612296SLin.Ling@Sun.COM * DDT_CLASS_DUPLICATE); if it transitions from refcnt == 1 to refcnt > 1
124712296SLin.Ling@Sun.COM * while a scrub is in progress, it scrubs the block right then.
124812296SLin.Ling@Sun.COM */
124912296SLin.Ling@Sun.COM static void
dsl_scan_ddt(dsl_scan_t * scn,dmu_tx_t * tx)125012296SLin.Ling@Sun.COM dsl_scan_ddt(dsl_scan_t *scn, dmu_tx_t *tx)
125112296SLin.Ling@Sun.COM {
125212296SLin.Ling@Sun.COM ddt_bookmark_t *ddb = &scn->scn_phys.scn_ddt_bookmark;
125312296SLin.Ling@Sun.COM ddt_entry_t dde = { 0 };
125412296SLin.Ling@Sun.COM int error;
125512296SLin.Ling@Sun.COM uint64_t n = 0;
125612296SLin.Ling@Sun.COM
125712296SLin.Ling@Sun.COM while ((error = ddt_walk(scn->scn_dp->dp_spa, ddb, &dde)) == 0) {
125812296SLin.Ling@Sun.COM ddt_t *ddt;
125912296SLin.Ling@Sun.COM
126012296SLin.Ling@Sun.COM if (ddb->ddb_class > scn->scn_phys.scn_ddt_class_max)
126112296SLin.Ling@Sun.COM break;
126212296SLin.Ling@Sun.COM dprintf("visiting ddb=%llu/%llu/%llu/%llx\n",
126312296SLin.Ling@Sun.COM (longlong_t)ddb->ddb_class,
126412296SLin.Ling@Sun.COM (longlong_t)ddb->ddb_type,
126512296SLin.Ling@Sun.COM (longlong_t)ddb->ddb_checksum,
126612296SLin.Ling@Sun.COM (longlong_t)ddb->ddb_cursor);
126712296SLin.Ling@Sun.COM
126812296SLin.Ling@Sun.COM /* There should be no pending changes to the dedup table */
126912296SLin.Ling@Sun.COM ddt = scn->scn_dp->dp_spa->spa_ddt[ddb->ddb_checksum];
127012296SLin.Ling@Sun.COM ASSERT(avl_first(&ddt->ddt_tree) == NULL);
127112296SLin.Ling@Sun.COM
127212296SLin.Ling@Sun.COM dsl_scan_ddt_entry(scn, ddb->ddb_checksum, &dde, tx);
127312296SLin.Ling@Sun.COM n++;
127412296SLin.Ling@Sun.COM
127512296SLin.Ling@Sun.COM if (dsl_scan_check_pause(scn, NULL))
127612296SLin.Ling@Sun.COM break;
127712296SLin.Ling@Sun.COM }
127812296SLin.Ling@Sun.COM
127912296SLin.Ling@Sun.COM zfs_dbgmsg("scanned %llu ddt entries with class_max = %u; pausing=%u",
128012296SLin.Ling@Sun.COM (longlong_t)n, (int)scn->scn_phys.scn_ddt_class_max,
128112296SLin.Ling@Sun.COM (int)scn->scn_pausing);
128212296SLin.Ling@Sun.COM
128312296SLin.Ling@Sun.COM ASSERT(error == 0 || error == ENOENT);
128412296SLin.Ling@Sun.COM ASSERT(error != ENOENT ||
128512296SLin.Ling@Sun.COM ddb->ddb_class > scn->scn_phys.scn_ddt_class_max);
128612296SLin.Ling@Sun.COM }
128712296SLin.Ling@Sun.COM
128812296SLin.Ling@Sun.COM /* ARGSUSED */
128912296SLin.Ling@Sun.COM void
dsl_scan_ddt_entry(dsl_scan_t * scn,enum zio_checksum checksum,ddt_entry_t * dde,dmu_tx_t * tx)129012296SLin.Ling@Sun.COM dsl_scan_ddt_entry(dsl_scan_t *scn, enum zio_checksum checksum,
129112296SLin.Ling@Sun.COM ddt_entry_t *dde, dmu_tx_t *tx)
129212296SLin.Ling@Sun.COM {
129312296SLin.Ling@Sun.COM const ddt_key_t *ddk = &dde->dde_key;
129412296SLin.Ling@Sun.COM ddt_phys_t *ddp = dde->dde_phys;
129512296SLin.Ling@Sun.COM blkptr_t bp;
129612296SLin.Ling@Sun.COM zbookmark_t zb = { 0 };
129712296SLin.Ling@Sun.COM
129812296SLin.Ling@Sun.COM if (scn->scn_phys.scn_state != DSS_SCANNING)
129912296SLin.Ling@Sun.COM return;
130012296SLin.Ling@Sun.COM
130112296SLin.Ling@Sun.COM for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
130212296SLin.Ling@Sun.COM if (ddp->ddp_phys_birth == 0 ||
130312296SLin.Ling@Sun.COM ddp->ddp_phys_birth > scn->scn_phys.scn_cur_max_txg)
130412296SLin.Ling@Sun.COM continue;
130512296SLin.Ling@Sun.COM ddt_bp_create(checksum, ddk, ddp, &bp);
130612296SLin.Ling@Sun.COM
130712296SLin.Ling@Sun.COM scn->scn_visited_this_txg++;
130812296SLin.Ling@Sun.COM scan_funcs[scn->scn_phys.scn_func](scn->scn_dp, &bp, &zb);
130912296SLin.Ling@Sun.COM }
131012296SLin.Ling@Sun.COM }
131112296SLin.Ling@Sun.COM
131212296SLin.Ling@Sun.COM static void
dsl_scan_visit(dsl_scan_t * scn,dmu_tx_t * tx)131312296SLin.Ling@Sun.COM dsl_scan_visit(dsl_scan_t *scn, dmu_tx_t *tx)
131412296SLin.Ling@Sun.COM {
131512296SLin.Ling@Sun.COM dsl_pool_t *dp = scn->scn_dp;
131612296SLin.Ling@Sun.COM zap_cursor_t zc;
131712296SLin.Ling@Sun.COM zap_attribute_t za;
131812296SLin.Ling@Sun.COM
131912296SLin.Ling@Sun.COM if (scn->scn_phys.scn_ddt_bookmark.ddb_class <=
132012296SLin.Ling@Sun.COM scn->scn_phys.scn_ddt_class_max) {
132112296SLin.Ling@Sun.COM scn->scn_phys.scn_cur_min_txg = scn->scn_phys.scn_min_txg;
132212296SLin.Ling@Sun.COM scn->scn_phys.scn_cur_max_txg = scn->scn_phys.scn_max_txg;
132312296SLin.Ling@Sun.COM dsl_scan_ddt(scn, tx);
132412296SLin.Ling@Sun.COM if (scn->scn_pausing)
132512296SLin.Ling@Sun.COM return;
132612296SLin.Ling@Sun.COM }
132712296SLin.Ling@Sun.COM
132812296SLin.Ling@Sun.COM if (scn->scn_phys.scn_bookmark.zb_objset == DMU_META_OBJSET) {
132912296SLin.Ling@Sun.COM /* First do the MOS & ORIGIN */
133012296SLin.Ling@Sun.COM
133112296SLin.Ling@Sun.COM scn->scn_phys.scn_cur_min_txg = scn->scn_phys.scn_min_txg;
133212296SLin.Ling@Sun.COM scn->scn_phys.scn_cur_max_txg = scn->scn_phys.scn_max_txg;
133312296SLin.Ling@Sun.COM dsl_scan_visit_rootbp(scn, NULL,
133412296SLin.Ling@Sun.COM &dp->dp_meta_rootbp, tx);
133512296SLin.Ling@Sun.COM spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
133612296SLin.Ling@Sun.COM if (scn->scn_pausing)
133712296SLin.Ling@Sun.COM return;
133812296SLin.Ling@Sun.COM
133912296SLin.Ling@Sun.COM if (spa_version(dp->dp_spa) < SPA_VERSION_DSL_SCRUB) {
134012296SLin.Ling@Sun.COM VERIFY(0 == dmu_objset_find_spa(dp->dp_spa,
134112296SLin.Ling@Sun.COM NULL, enqueue_cb, tx, DS_FIND_CHILDREN));
134212296SLin.Ling@Sun.COM } else {
134312296SLin.Ling@Sun.COM dsl_scan_visitds(scn,
134412296SLin.Ling@Sun.COM dp->dp_origin_snap->ds_object, tx);
134512296SLin.Ling@Sun.COM }
134612296SLin.Ling@Sun.COM ASSERT(!scn->scn_pausing);
134712296SLin.Ling@Sun.COM } else if (scn->scn_phys.scn_bookmark.zb_objset !=
134812296SLin.Ling@Sun.COM ZB_DESTROYED_OBJSET) {
134912296SLin.Ling@Sun.COM /*
135012296SLin.Ling@Sun.COM * If we were paused, continue from here. Note if the
135112296SLin.Ling@Sun.COM * ds we were paused on was deleted, the zb_objset may
135212296SLin.Ling@Sun.COM * be -1, so we will skip this and find a new objset
135312296SLin.Ling@Sun.COM * below.
135412296SLin.Ling@Sun.COM */
135512296SLin.Ling@Sun.COM dsl_scan_visitds(scn, scn->scn_phys.scn_bookmark.zb_objset, tx);
135612296SLin.Ling@Sun.COM if (scn->scn_pausing)
135712296SLin.Ling@Sun.COM return;
135812296SLin.Ling@Sun.COM }
135912296SLin.Ling@Sun.COM
136012296SLin.Ling@Sun.COM /*
136112296SLin.Ling@Sun.COM * In case we were paused right at the end of the ds, zero the
136212296SLin.Ling@Sun.COM * bookmark so we don't think that we're still trying to resume.
136312296SLin.Ling@Sun.COM */
136412296SLin.Ling@Sun.COM bzero(&scn->scn_phys.scn_bookmark, sizeof (zbookmark_t));
136512296SLin.Ling@Sun.COM
136612296SLin.Ling@Sun.COM /* keep pulling things out of the zap-object-as-queue */
136712296SLin.Ling@Sun.COM while (zap_cursor_init(&zc, dp->dp_meta_objset,
136812296SLin.Ling@Sun.COM scn->scn_phys.scn_queue_obj),
136912296SLin.Ling@Sun.COM zap_cursor_retrieve(&zc, &za) == 0) {
137012296SLin.Ling@Sun.COM dsl_dataset_t *ds;
137112296SLin.Ling@Sun.COM uint64_t dsobj;
137212296SLin.Ling@Sun.COM
137312296SLin.Ling@Sun.COM dsobj = strtonum(za.za_name, NULL);
137412296SLin.Ling@Sun.COM VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
137512296SLin.Ling@Sun.COM scn->scn_phys.scn_queue_obj, dsobj, tx));
137612296SLin.Ling@Sun.COM
137712296SLin.Ling@Sun.COM /* Set up min/max txg */
137812296SLin.Ling@Sun.COM VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
137912296SLin.Ling@Sun.COM if (za.za_first_integer != 0) {
138012296SLin.Ling@Sun.COM scn->scn_phys.scn_cur_min_txg =
138112296SLin.Ling@Sun.COM MAX(scn->scn_phys.scn_min_txg,
138212296SLin.Ling@Sun.COM za.za_first_integer);
138312296SLin.Ling@Sun.COM } else {
138412296SLin.Ling@Sun.COM scn->scn_phys.scn_cur_min_txg =
138512296SLin.Ling@Sun.COM MAX(scn->scn_phys.scn_min_txg,
138612296SLin.Ling@Sun.COM ds->ds_phys->ds_prev_snap_txg);
138712296SLin.Ling@Sun.COM }
138812296SLin.Ling@Sun.COM scn->scn_phys.scn_cur_max_txg = dsl_scan_ds_maxtxg(ds);
138912296SLin.Ling@Sun.COM dsl_dataset_rele(ds, FTAG);
139012296SLin.Ling@Sun.COM
139112296SLin.Ling@Sun.COM dsl_scan_visitds(scn, dsobj, tx);
139212296SLin.Ling@Sun.COM zap_cursor_fini(&zc);
139312296SLin.Ling@Sun.COM if (scn->scn_pausing)
139412296SLin.Ling@Sun.COM return;
139512296SLin.Ling@Sun.COM }
139612296SLin.Ling@Sun.COM zap_cursor_fini(&zc);
139712296SLin.Ling@Sun.COM }
139812296SLin.Ling@Sun.COM
139912470SMatthew.Ahrens@Sun.COM static int
dsl_scan_free_cb(void * arg,const blkptr_t * bp,dmu_tx_t * tx)140012470SMatthew.Ahrens@Sun.COM dsl_scan_free_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
140112470SMatthew.Ahrens@Sun.COM {
140212470SMatthew.Ahrens@Sun.COM dsl_scan_t *scn = arg;
140312470SMatthew.Ahrens@Sun.COM uint64_t elapsed_nanosecs;
140412470SMatthew.Ahrens@Sun.COM
140512470SMatthew.Ahrens@Sun.COM elapsed_nanosecs = gethrtime() - scn->scn_sync_start_time;
140612470SMatthew.Ahrens@Sun.COM
140712470SMatthew.Ahrens@Sun.COM if (elapsed_nanosecs / NANOSEC > zfs_txg_timeout ||
140812470SMatthew.Ahrens@Sun.COM (elapsed_nanosecs / MICROSEC > zfs_free_min_time_ms &&
140912470SMatthew.Ahrens@Sun.COM txg_sync_waiting(scn->scn_dp)) ||
141012470SMatthew.Ahrens@Sun.COM spa_shutting_down(scn->scn_dp->dp_spa))
141112470SMatthew.Ahrens@Sun.COM return (ERESTART);
141212470SMatthew.Ahrens@Sun.COM
141312470SMatthew.Ahrens@Sun.COM zio_nowait(zio_free_sync(scn->scn_zio_root, scn->scn_dp->dp_spa,
141412470SMatthew.Ahrens@Sun.COM dmu_tx_get_txg(tx), bp, 0));
141512470SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(tx->tx_pool->dp_free_dir, DD_USED_HEAD,
141612470SMatthew.Ahrens@Sun.COM -bp_get_dsize_sync(scn->scn_dp->dp_spa, bp),
141712470SMatthew.Ahrens@Sun.COM -BP_GET_PSIZE(bp), -BP_GET_UCSIZE(bp), tx);
141812470SMatthew.Ahrens@Sun.COM scn->scn_visited_this_txg++;
141912470SMatthew.Ahrens@Sun.COM return (0);
142012470SMatthew.Ahrens@Sun.COM }
142112470SMatthew.Ahrens@Sun.COM
142212470SMatthew.Ahrens@Sun.COM boolean_t
dsl_scan_active(dsl_scan_t * scn)142312470SMatthew.Ahrens@Sun.COM dsl_scan_active(dsl_scan_t *scn)
142412470SMatthew.Ahrens@Sun.COM {
142512470SMatthew.Ahrens@Sun.COM spa_t *spa = scn->scn_dp->dp_spa;
142612470SMatthew.Ahrens@Sun.COM uint64_t used = 0, comp, uncomp;
142712470SMatthew.Ahrens@Sun.COM
142812470SMatthew.Ahrens@Sun.COM if (spa->spa_load_state != SPA_LOAD_NONE)
142912470SMatthew.Ahrens@Sun.COM return (B_FALSE);
143012470SMatthew.Ahrens@Sun.COM if (spa_shutting_down(spa))
143112470SMatthew.Ahrens@Sun.COM return (B_FALSE);
143212470SMatthew.Ahrens@Sun.COM
143312470SMatthew.Ahrens@Sun.COM if (scn->scn_phys.scn_state == DSS_SCANNING)
143412470SMatthew.Ahrens@Sun.COM return (B_TRUE);
143512470SMatthew.Ahrens@Sun.COM
143612470SMatthew.Ahrens@Sun.COM if (spa_version(scn->scn_dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
143712470SMatthew.Ahrens@Sun.COM (void) bpobj_space(&scn->scn_dp->dp_free_bpobj,
143812470SMatthew.Ahrens@Sun.COM &used, &comp, &uncomp);
143912470SMatthew.Ahrens@Sun.COM }
144012470SMatthew.Ahrens@Sun.COM return (used != 0);
144112470SMatthew.Ahrens@Sun.COM }
144212470SMatthew.Ahrens@Sun.COM
144312296SLin.Ling@Sun.COM void
dsl_scan_sync(dsl_pool_t * dp,dmu_tx_t * tx)144412296SLin.Ling@Sun.COM dsl_scan_sync(dsl_pool_t *dp, dmu_tx_t *tx)
144512296SLin.Ling@Sun.COM {
144612296SLin.Ling@Sun.COM dsl_scan_t *scn = dp->dp_scan;
144712296SLin.Ling@Sun.COM spa_t *spa = dp->dp_spa;
144812470SMatthew.Ahrens@Sun.COM int err;
144912296SLin.Ling@Sun.COM
145012296SLin.Ling@Sun.COM /*
145112296SLin.Ling@Sun.COM * Check for scn_restart_txg before checking spa_load_state, so
145212296SLin.Ling@Sun.COM * that we can restart an old-style scan while the pool is being
145312296SLin.Ling@Sun.COM * imported (see dsl_scan_init).
145412296SLin.Ling@Sun.COM */
145512296SLin.Ling@Sun.COM if (scn->scn_restart_txg != 0 &&
145612296SLin.Ling@Sun.COM scn->scn_restart_txg <= tx->tx_txg) {
145712296SLin.Ling@Sun.COM pool_scan_func_t func = POOL_SCAN_SCRUB;
145812296SLin.Ling@Sun.COM dsl_scan_done(scn, B_FALSE, tx);
145912296SLin.Ling@Sun.COM if (vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL))
146012296SLin.Ling@Sun.COM func = POOL_SCAN_RESILVER;
146112296SLin.Ling@Sun.COM zfs_dbgmsg("restarting scan func=%u txg=%llu",
146212296SLin.Ling@Sun.COM func, tx->tx_txg);
146312296SLin.Ling@Sun.COM dsl_scan_setup_sync(scn, &func, tx);
146412296SLin.Ling@Sun.COM }
146512296SLin.Ling@Sun.COM
146612470SMatthew.Ahrens@Sun.COM if (!dsl_scan_active(scn) ||
146712470SMatthew.Ahrens@Sun.COM spa_sync_pass(dp->dp_spa) > 1)
146812296SLin.Ling@Sun.COM return;
146912296SLin.Ling@Sun.COM
147012296SLin.Ling@Sun.COM scn->scn_visited_this_txg = 0;
147112296SLin.Ling@Sun.COM scn->scn_pausing = B_FALSE;
147212296SLin.Ling@Sun.COM scn->scn_sync_start_time = gethrtime();
147312296SLin.Ling@Sun.COM spa->spa_scrub_active = B_TRUE;
147412296SLin.Ling@Sun.COM
147512470SMatthew.Ahrens@Sun.COM /*
147612470SMatthew.Ahrens@Sun.COM * First process the free list. If we pause the free, don't do
147712470SMatthew.Ahrens@Sun.COM * any scanning. This ensures that there is no free list when
147812470SMatthew.Ahrens@Sun.COM * we are scanning, so the scan code doesn't have to worry about
147912470SMatthew.Ahrens@Sun.COM * traversing it.
148012470SMatthew.Ahrens@Sun.COM */
148112470SMatthew.Ahrens@Sun.COM if (spa_version(dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
148212470SMatthew.Ahrens@Sun.COM scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
148312470SMatthew.Ahrens@Sun.COM NULL, ZIO_FLAG_MUSTSUCCEED);
148412470SMatthew.Ahrens@Sun.COM err = bpobj_iterate(&dp->dp_free_bpobj,
148512470SMatthew.Ahrens@Sun.COM dsl_scan_free_cb, scn, tx);
148612470SMatthew.Ahrens@Sun.COM VERIFY3U(0, ==, zio_wait(scn->scn_zio_root));
148712470SMatthew.Ahrens@Sun.COM if (scn->scn_visited_this_txg) {
148812470SMatthew.Ahrens@Sun.COM zfs_dbgmsg("freed %llu blocks in %llums from "
148912470SMatthew.Ahrens@Sun.COM "free_bpobj txg %llu",
149012470SMatthew.Ahrens@Sun.COM (longlong_t)scn->scn_visited_this_txg,
149112470SMatthew.Ahrens@Sun.COM (longlong_t)
149212470SMatthew.Ahrens@Sun.COM (gethrtime() - scn->scn_sync_start_time) / MICROSEC,
149312470SMatthew.Ahrens@Sun.COM (longlong_t)tx->tx_txg);
149412470SMatthew.Ahrens@Sun.COM scn->scn_visited_this_txg = 0;
149512470SMatthew.Ahrens@Sun.COM /*
149612470SMatthew.Ahrens@Sun.COM * Re-sync the ddt so that we can further modify
149712470SMatthew.Ahrens@Sun.COM * it when doing bprewrite.
149812470SMatthew.Ahrens@Sun.COM */
149912470SMatthew.Ahrens@Sun.COM ddt_sync(spa, tx->tx_txg);
150012470SMatthew.Ahrens@Sun.COM }
150112470SMatthew.Ahrens@Sun.COM if (err == ERESTART)
150212470SMatthew.Ahrens@Sun.COM return;
150312470SMatthew.Ahrens@Sun.COM }
150412470SMatthew.Ahrens@Sun.COM
150512470SMatthew.Ahrens@Sun.COM if (scn->scn_phys.scn_state != DSS_SCANNING)
150612470SMatthew.Ahrens@Sun.COM return;
150712470SMatthew.Ahrens@Sun.COM
150812296SLin.Ling@Sun.COM if (scn->scn_phys.scn_ddt_bookmark.ddb_class <=
150912296SLin.Ling@Sun.COM scn->scn_phys.scn_ddt_class_max) {
151012296SLin.Ling@Sun.COM zfs_dbgmsg("doing scan sync txg %llu; "
151112296SLin.Ling@Sun.COM "ddt bm=%llu/%llu/%llu/%llx",
151212296SLin.Ling@Sun.COM (longlong_t)tx->tx_txg,
151312296SLin.Ling@Sun.COM (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_class,
151412296SLin.Ling@Sun.COM (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_type,
151512296SLin.Ling@Sun.COM (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_checksum,
151612296SLin.Ling@Sun.COM (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_cursor);
151712296SLin.Ling@Sun.COM ASSERT(scn->scn_phys.scn_bookmark.zb_objset == 0);
151812296SLin.Ling@Sun.COM ASSERT(scn->scn_phys.scn_bookmark.zb_object == 0);
151912296SLin.Ling@Sun.COM ASSERT(scn->scn_phys.scn_bookmark.zb_level == 0);
152012296SLin.Ling@Sun.COM ASSERT(scn->scn_phys.scn_bookmark.zb_blkid == 0);
152112296SLin.Ling@Sun.COM } else {
152212296SLin.Ling@Sun.COM zfs_dbgmsg("doing scan sync txg %llu; bm=%llu/%llu/%llu/%llu",
152312296SLin.Ling@Sun.COM (longlong_t)tx->tx_txg,
152412296SLin.Ling@Sun.COM (longlong_t)scn->scn_phys.scn_bookmark.zb_objset,
152512296SLin.Ling@Sun.COM (longlong_t)scn->scn_phys.scn_bookmark.zb_object,
152612296SLin.Ling@Sun.COM (longlong_t)scn->scn_phys.scn_bookmark.zb_level,
152712296SLin.Ling@Sun.COM (longlong_t)scn->scn_phys.scn_bookmark.zb_blkid);
152812296SLin.Ling@Sun.COM }
152912296SLin.Ling@Sun.COM
153012470SMatthew.Ahrens@Sun.COM scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
153112296SLin.Ling@Sun.COM NULL, ZIO_FLAG_CANFAIL);
153212296SLin.Ling@Sun.COM dsl_scan_visit(scn, tx);
153312470SMatthew.Ahrens@Sun.COM (void) zio_wait(scn->scn_zio_root);
153412470SMatthew.Ahrens@Sun.COM scn->scn_zio_root = NULL;
153512296SLin.Ling@Sun.COM
153612296SLin.Ling@Sun.COM zfs_dbgmsg("visited %llu blocks in %llums",
153712296SLin.Ling@Sun.COM (longlong_t)scn->scn_visited_this_txg,
153812296SLin.Ling@Sun.COM (longlong_t)(gethrtime() - scn->scn_sync_start_time) / MICROSEC);
153912296SLin.Ling@Sun.COM
154012296SLin.Ling@Sun.COM if (!scn->scn_pausing) {
154112296SLin.Ling@Sun.COM /* finished with scan. */
154212296SLin.Ling@Sun.COM zfs_dbgmsg("finished scan txg %llu", (longlong_t)tx->tx_txg);
154312296SLin.Ling@Sun.COM dsl_scan_done(scn, B_TRUE, tx);
154412296SLin.Ling@Sun.COM }
154512296SLin.Ling@Sun.COM
154612296SLin.Ling@Sun.COM if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
154712296SLin.Ling@Sun.COM mutex_enter(&spa->spa_scrub_lock);
154812296SLin.Ling@Sun.COM while (spa->spa_scrub_inflight > 0) {
154912296SLin.Ling@Sun.COM cv_wait(&spa->spa_scrub_io_cv,
155012296SLin.Ling@Sun.COM &spa->spa_scrub_lock);
155112296SLin.Ling@Sun.COM }
155212296SLin.Ling@Sun.COM mutex_exit(&spa->spa_scrub_lock);
155312296SLin.Ling@Sun.COM }
155412296SLin.Ling@Sun.COM
155512296SLin.Ling@Sun.COM dsl_scan_sync_state(scn, tx);
155612296SLin.Ling@Sun.COM }
155712296SLin.Ling@Sun.COM
155812296SLin.Ling@Sun.COM /*
155912296SLin.Ling@Sun.COM * This will start a new scan, or restart an existing one.
156012296SLin.Ling@Sun.COM */
156112296SLin.Ling@Sun.COM void
dsl_resilver_restart(dsl_pool_t * dp,uint64_t txg)156212296SLin.Ling@Sun.COM dsl_resilver_restart(dsl_pool_t *dp, uint64_t txg)
156312296SLin.Ling@Sun.COM {
156412296SLin.Ling@Sun.COM if (txg == 0) {
156512296SLin.Ling@Sun.COM dmu_tx_t *tx;
156612296SLin.Ling@Sun.COM tx = dmu_tx_create_dd(dp->dp_mos_dir);
156712296SLin.Ling@Sun.COM VERIFY(0 == dmu_tx_assign(tx, TXG_WAIT));
156812296SLin.Ling@Sun.COM
156912296SLin.Ling@Sun.COM txg = dmu_tx_get_txg(tx);
157012296SLin.Ling@Sun.COM dp->dp_scan->scn_restart_txg = txg;
157112296SLin.Ling@Sun.COM dmu_tx_commit(tx);
157212296SLin.Ling@Sun.COM } else {
157312296SLin.Ling@Sun.COM dp->dp_scan->scn_restart_txg = txg;
157412296SLin.Ling@Sun.COM }
157512296SLin.Ling@Sun.COM zfs_dbgmsg("restarting resilver txg=%llu", txg);
157612296SLin.Ling@Sun.COM }
157712296SLin.Ling@Sun.COM
157812296SLin.Ling@Sun.COM boolean_t
dsl_scan_resilvering(dsl_pool_t * dp)157912296SLin.Ling@Sun.COM dsl_scan_resilvering(dsl_pool_t *dp)
158012296SLin.Ling@Sun.COM {
158112296SLin.Ling@Sun.COM return (dp->dp_scan->scn_phys.scn_state == DSS_SCANNING &&
158212296SLin.Ling@Sun.COM dp->dp_scan->scn_phys.scn_func == POOL_SCAN_RESILVER);
158312296SLin.Ling@Sun.COM }
158412296SLin.Ling@Sun.COM
158512296SLin.Ling@Sun.COM /*
158612296SLin.Ling@Sun.COM * scrub consumers
158712296SLin.Ling@Sun.COM */
158812296SLin.Ling@Sun.COM
158912296SLin.Ling@Sun.COM static void
count_block(zfs_all_blkstats_t * zab,const blkptr_t * bp)159012296SLin.Ling@Sun.COM count_block(zfs_all_blkstats_t *zab, const blkptr_t *bp)
159112296SLin.Ling@Sun.COM {
159212296SLin.Ling@Sun.COM int i;
159312296SLin.Ling@Sun.COM
159412296SLin.Ling@Sun.COM /*
159512296SLin.Ling@Sun.COM * If we resume after a reboot, zab will be NULL; don't record
159612296SLin.Ling@Sun.COM * incomplete stats in that case.
159712296SLin.Ling@Sun.COM */
159812296SLin.Ling@Sun.COM if (zab == NULL)
159912296SLin.Ling@Sun.COM return;
160012296SLin.Ling@Sun.COM
160112296SLin.Ling@Sun.COM for (i = 0; i < 4; i++) {
160212296SLin.Ling@Sun.COM int l = (i < 2) ? BP_GET_LEVEL(bp) : DN_MAX_LEVELS;
160312296SLin.Ling@Sun.COM int t = (i & 1) ? BP_GET_TYPE(bp) : DMU_OT_TOTAL;
160412296SLin.Ling@Sun.COM zfs_blkstat_t *zb = &zab->zab_type[l][t];
160512296SLin.Ling@Sun.COM int equal;
160612296SLin.Ling@Sun.COM
160712296SLin.Ling@Sun.COM zb->zb_count++;
160812296SLin.Ling@Sun.COM zb->zb_asize += BP_GET_ASIZE(bp);
160912296SLin.Ling@Sun.COM zb->zb_lsize += BP_GET_LSIZE(bp);
161012296SLin.Ling@Sun.COM zb->zb_psize += BP_GET_PSIZE(bp);
161112296SLin.Ling@Sun.COM zb->zb_gangs += BP_COUNT_GANG(bp);
161212296SLin.Ling@Sun.COM
161312296SLin.Ling@Sun.COM switch (BP_GET_NDVAS(bp)) {
161412296SLin.Ling@Sun.COM case 2:
161512296SLin.Ling@Sun.COM if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
161612296SLin.Ling@Sun.COM DVA_GET_VDEV(&bp->blk_dva[1]))
161712296SLin.Ling@Sun.COM zb->zb_ditto_2_of_2_samevdev++;
161812296SLin.Ling@Sun.COM break;
161912296SLin.Ling@Sun.COM case 3:
162012296SLin.Ling@Sun.COM equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
162112296SLin.Ling@Sun.COM DVA_GET_VDEV(&bp->blk_dva[1])) +
162212296SLin.Ling@Sun.COM (DVA_GET_VDEV(&bp->blk_dva[0]) ==
162312296SLin.Ling@Sun.COM DVA_GET_VDEV(&bp->blk_dva[2])) +
162412296SLin.Ling@Sun.COM (DVA_GET_VDEV(&bp->blk_dva[1]) ==
162512296SLin.Ling@Sun.COM DVA_GET_VDEV(&bp->blk_dva[2]));
162612296SLin.Ling@Sun.COM if (equal == 1)
162712296SLin.Ling@Sun.COM zb->zb_ditto_2_of_3_samevdev++;
162812296SLin.Ling@Sun.COM else if (equal == 3)
162912296SLin.Ling@Sun.COM zb->zb_ditto_3_of_3_samevdev++;
163012296SLin.Ling@Sun.COM break;
163112296SLin.Ling@Sun.COM }
163212296SLin.Ling@Sun.COM }
163312296SLin.Ling@Sun.COM }
163412296SLin.Ling@Sun.COM
163512296SLin.Ling@Sun.COM static void
dsl_scan_scrub_done(zio_t * zio)163612296SLin.Ling@Sun.COM dsl_scan_scrub_done(zio_t *zio)
163712296SLin.Ling@Sun.COM {
163812296SLin.Ling@Sun.COM spa_t *spa = zio->io_spa;
163912296SLin.Ling@Sun.COM
164012296SLin.Ling@Sun.COM zio_data_buf_free(zio->io_data, zio->io_size);
164112296SLin.Ling@Sun.COM
164212296SLin.Ling@Sun.COM mutex_enter(&spa->spa_scrub_lock);
164312296SLin.Ling@Sun.COM spa->spa_scrub_inflight--;
164412296SLin.Ling@Sun.COM cv_broadcast(&spa->spa_scrub_io_cv);
164512296SLin.Ling@Sun.COM
164612296SLin.Ling@Sun.COM if (zio->io_error && (zio->io_error != ECKSUM ||
164712296SLin.Ling@Sun.COM !(zio->io_flags & ZIO_FLAG_SPECULATIVE))) {
164812296SLin.Ling@Sun.COM spa->spa_dsl_pool->dp_scan->scn_phys.scn_errors++;
164912296SLin.Ling@Sun.COM }
165012296SLin.Ling@Sun.COM mutex_exit(&spa->spa_scrub_lock);
165112296SLin.Ling@Sun.COM }
165212296SLin.Ling@Sun.COM
165312296SLin.Ling@Sun.COM static int
dsl_scan_scrub_cb(dsl_pool_t * dp,const blkptr_t * bp,const zbookmark_t * zb)165412296SLin.Ling@Sun.COM dsl_scan_scrub_cb(dsl_pool_t *dp,
165512296SLin.Ling@Sun.COM const blkptr_t *bp, const zbookmark_t *zb)
165612296SLin.Ling@Sun.COM {
165712296SLin.Ling@Sun.COM dsl_scan_t *scn = dp->dp_scan;
165812296SLin.Ling@Sun.COM size_t size = BP_GET_PSIZE(bp);
165912296SLin.Ling@Sun.COM spa_t *spa = dp->dp_spa;
166012296SLin.Ling@Sun.COM uint64_t phys_birth = BP_PHYSICAL_BIRTH(bp);
166112296SLin.Ling@Sun.COM boolean_t needs_io;
166212586SGeorge.Wilson@Sun.COM int zio_flags = ZIO_FLAG_SCAN_THREAD | ZIO_FLAG_RAW | ZIO_FLAG_CANFAIL;
166312296SLin.Ling@Sun.COM int zio_priority;
166412586SGeorge.Wilson@Sun.COM int scan_delay = 0;
166512296SLin.Ling@Sun.COM
166612296SLin.Ling@Sun.COM if (phys_birth <= scn->scn_phys.scn_min_txg ||
166712296SLin.Ling@Sun.COM phys_birth >= scn->scn_phys.scn_max_txg)
166812296SLin.Ling@Sun.COM return (0);
166912296SLin.Ling@Sun.COM
167012296SLin.Ling@Sun.COM count_block(dp->dp_blkstats, bp);
167112296SLin.Ling@Sun.COM
167212296SLin.Ling@Sun.COM ASSERT(DSL_SCAN_IS_SCRUB_RESILVER(scn));
167312296SLin.Ling@Sun.COM if (scn->scn_phys.scn_func == POOL_SCAN_SCRUB) {
167412296SLin.Ling@Sun.COM zio_flags |= ZIO_FLAG_SCRUB;
167512296SLin.Ling@Sun.COM zio_priority = ZIO_PRIORITY_SCRUB;
167612296SLin.Ling@Sun.COM needs_io = B_TRUE;
167712586SGeorge.Wilson@Sun.COM scan_delay = zfs_scrub_delay;
167812296SLin.Ling@Sun.COM } else if (scn->scn_phys.scn_func == POOL_SCAN_RESILVER) {
167912296SLin.Ling@Sun.COM zio_flags |= ZIO_FLAG_RESILVER;
168012296SLin.Ling@Sun.COM zio_priority = ZIO_PRIORITY_RESILVER;
168112296SLin.Ling@Sun.COM needs_io = B_FALSE;
168212586SGeorge.Wilson@Sun.COM scan_delay = zfs_resilver_delay;
168312296SLin.Ling@Sun.COM }
168412296SLin.Ling@Sun.COM
168512296SLin.Ling@Sun.COM /* If it's an intent log block, failure is expected. */
168612296SLin.Ling@Sun.COM if (zb->zb_level == ZB_ZIL_LEVEL)
168712296SLin.Ling@Sun.COM zio_flags |= ZIO_FLAG_SPECULATIVE;
168812296SLin.Ling@Sun.COM
168912296SLin.Ling@Sun.COM for (int d = 0; d < BP_GET_NDVAS(bp); d++) {
169012296SLin.Ling@Sun.COM vdev_t *vd = vdev_lookup_top(spa,
169112296SLin.Ling@Sun.COM DVA_GET_VDEV(&bp->blk_dva[d]));
169212296SLin.Ling@Sun.COM
169312296SLin.Ling@Sun.COM /*
169412296SLin.Ling@Sun.COM * Keep track of how much data we've examined so that
169512296SLin.Ling@Sun.COM * zpool(1M) status can make useful progress reports.
169612296SLin.Ling@Sun.COM */
169712296SLin.Ling@Sun.COM scn->scn_phys.scn_examined += DVA_GET_ASIZE(&bp->blk_dva[d]);
169812296SLin.Ling@Sun.COM spa->spa_scan_pass_exam += DVA_GET_ASIZE(&bp->blk_dva[d]);
169912296SLin.Ling@Sun.COM
170012296SLin.Ling@Sun.COM /* if it's a resilver, this may not be in the target range */
170112296SLin.Ling@Sun.COM if (!needs_io) {
170212296SLin.Ling@Sun.COM if (DVA_GET_GANG(&bp->blk_dva[d])) {
170312296SLin.Ling@Sun.COM /*
170412296SLin.Ling@Sun.COM * Gang members may be spread across multiple
170512296SLin.Ling@Sun.COM * vdevs, so the best estimate we have is the
170612296SLin.Ling@Sun.COM * scrub range, which has already been checked.
170712296SLin.Ling@Sun.COM * XXX -- it would be better to change our
170812296SLin.Ling@Sun.COM * allocation policy to ensure that all
170912296SLin.Ling@Sun.COM * gang members reside on the same vdev.
171012296SLin.Ling@Sun.COM */
171112296SLin.Ling@Sun.COM needs_io = B_TRUE;
171212296SLin.Ling@Sun.COM } else {
171312296SLin.Ling@Sun.COM needs_io = vdev_dtl_contains(vd, DTL_PARTIAL,
171412296SLin.Ling@Sun.COM phys_birth, 1);
171512296SLin.Ling@Sun.COM }
171612296SLin.Ling@Sun.COM }
171712296SLin.Ling@Sun.COM }
171812296SLin.Ling@Sun.COM
171912296SLin.Ling@Sun.COM if (needs_io && !zfs_no_scrub_io) {
172012586SGeorge.Wilson@Sun.COM vdev_t *rvd = spa->spa_root_vdev;
172112586SGeorge.Wilson@Sun.COM uint64_t maxinflight = rvd->vdev_children * zfs_top_maxinflight;
172212296SLin.Ling@Sun.COM void *data = zio_data_buf_alloc(size);
172312296SLin.Ling@Sun.COM
172412296SLin.Ling@Sun.COM mutex_enter(&spa->spa_scrub_lock);
172512586SGeorge.Wilson@Sun.COM while (spa->spa_scrub_inflight >= maxinflight)
172612296SLin.Ling@Sun.COM cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
172712296SLin.Ling@Sun.COM spa->spa_scrub_inflight++;
172812296SLin.Ling@Sun.COM mutex_exit(&spa->spa_scrub_lock);
172912296SLin.Ling@Sun.COM
173012586SGeorge.Wilson@Sun.COM /*
173112586SGeorge.Wilson@Sun.COM * If we're seeing recent (zfs_scan_idle) "important" I/Os
173212586SGeorge.Wilson@Sun.COM * then throttle our workload to limit the impact of a scan.
173312586SGeorge.Wilson@Sun.COM */
173412586SGeorge.Wilson@Sun.COM if (ddi_get_lbolt64() - spa->spa_last_io <= zfs_scan_idle)
173512586SGeorge.Wilson@Sun.COM delay(scan_delay);
173612586SGeorge.Wilson@Sun.COM
173712296SLin.Ling@Sun.COM zio_nowait(zio_read(NULL, spa, bp, data, size,
173812296SLin.Ling@Sun.COM dsl_scan_scrub_done, NULL, zio_priority,
173912296SLin.Ling@Sun.COM zio_flags, zb));
174012296SLin.Ling@Sun.COM }
174112296SLin.Ling@Sun.COM
174212296SLin.Ling@Sun.COM /* do not relocate this block */
174312296SLin.Ling@Sun.COM return (0);
174412296SLin.Ling@Sun.COM }
174512296SLin.Ling@Sun.COM
174612296SLin.Ling@Sun.COM int
dsl_scan(dsl_pool_t * dp,pool_scan_func_t func)174712296SLin.Ling@Sun.COM dsl_scan(dsl_pool_t *dp, pool_scan_func_t func)
174812296SLin.Ling@Sun.COM {
174912296SLin.Ling@Sun.COM spa_t *spa = dp->dp_spa;
175012296SLin.Ling@Sun.COM
175112296SLin.Ling@Sun.COM /*
175212296SLin.Ling@Sun.COM * Purge all vdev caches and probe all devices. We do this here
175312296SLin.Ling@Sun.COM * rather than in sync context because this requires a writer lock
175412296SLin.Ling@Sun.COM * on the spa_config lock, which we can't do from sync context. The
175512296SLin.Ling@Sun.COM * spa_scrub_reopen flag indicates that vdev_open() should not
175612296SLin.Ling@Sun.COM * attempt to start another scrub.
175712296SLin.Ling@Sun.COM */
175812296SLin.Ling@Sun.COM spa_vdev_state_enter(spa, SCL_NONE);
175912296SLin.Ling@Sun.COM spa->spa_scrub_reopen = B_TRUE;
176012296SLin.Ling@Sun.COM vdev_reopen(spa->spa_root_vdev);
176112296SLin.Ling@Sun.COM spa->spa_scrub_reopen = B_FALSE;
176212296SLin.Ling@Sun.COM (void) spa_vdev_state_exit(spa, NULL, 0);
176312296SLin.Ling@Sun.COM
176412296SLin.Ling@Sun.COM return (dsl_sync_task_do(dp, dsl_scan_setup_check,
176512296SLin.Ling@Sun.COM dsl_scan_setup_sync, dp->dp_scan, &func, 0));
176612296SLin.Ling@Sun.COM }
1767