dsl_scan.c revision af3465da8fa420c4ec701e3e57704d537a6f755b
0N/A/*
1472N/A * CDDL HEADER START
0N/A *
0N/A * The contents of this file are subject to the terms of the
0N/A * Common Development and Distribution License (the "License").
0N/A * You may not use this file except in compliance with the License.
0N/A *
0N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
0N/A * or http://www.opensolaris.org/os/licensing.
0N/A * See the License for the specific language governing permissions
0N/A * and limitations under the License.
0N/A *
0N/A * When distributing Covered Code, include this CDDL HEADER in each
0N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
0N/A * If applicable, add the following below this CDDL HEADER, with the
0N/A * fields enclosed by brackets "[]" replaced with your own identifying
0N/A * information: Portions Copyright [yyyy] [name of copyright owner]
0N/A *
1472N/A * CDDL HEADER END
1472N/A */
1472N/A/*
0N/A * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
0N/A */
0N/A
0N/A#include <sys/dsl_scan.h>
0N/A#include <sys/dsl_pool.h>
0N/A#include <sys/dsl_dataset.h>
0N/A#include <sys/dsl_prop.h>
0N/A#include <sys/dsl_dir.h>
0N/A#include <sys/dsl_synctask.h>
0N/A#include <sys/dnode.h>
0N/A#include <sys/dmu_tx.h>
0N/A#include <sys/dmu_objset.h>
0N/A#include <sys/arc.h>
0N/A#include <sys/zap.h>
0N/A#include <sys/zio.h>
0N/A#include <sys/zfs_context.h>
0N/A#include <sys/fs/zfs.h>
0N/A#include <sys/zfs_znode.h>
0N/A#include <sys/spa_impl.h>
0N/A#include <sys/vdev_impl.h>
0N/A#include <sys/zil_impl.h>
0N/A#include <sys/zio_checksum.h>
0N/A#include <sys/ddt.h>
0N/A#include <sys/sa.h>
0N/A#include <sys/sa_impl.h>
0N/A#include <sys/zfeature.h>
0N/A#ifdef _KERNEL
1064N/A#include <sys/zfs_vfsops.h>
0N/A#endif
0N/A
0N/Atypedef int (scan_cb_t)(dsl_pool_t *, const blkptr_t *,
0N/A const zbookmark_phys_t *);
0N/A
0N/Astatic scan_cb_t dsl_scan_scrub_cb;
0N/Astatic void dsl_scan_cancel_sync(void *, dmu_tx_t *);
0N/Astatic void dsl_scan_sync_state(dsl_scan_t *, dmu_tx_t *tx);
0N/A
0N/Aint zfs_top_maxinflight = 32; /* maximum I/Os per top-level */
1064N/Aint zfs_resilver_delay = 2; /* number of ticks to delay resilver */
0N/Aint zfs_scrub_delay = 4; /* number of ticks to delay scrub */
0N/Aint zfs_scan_idle = 50; /* idle window in clock ticks */
0N/A
0N/Aint zfs_scan_min_time_ms = 1000; /* min millisecs to scrub per txg */
0N/Aint zfs_free_min_time_ms = 1000; /* min millisecs to free per txg */
0N/Aint zfs_resilver_min_time_ms = 3000; /* min millisecs to resilver per txg */
0N/Aboolean_t zfs_no_scrub_io = B_FALSE; /* set to disable scrub i/o */
0N/Aboolean_t zfs_no_scrub_prefetch = B_FALSE; /* set to disable scrub prefetch */
0N/Aenum ddt_class zfs_scrub_ddt_class_max = DDT_CLASS_DUPLICATE;
0N/Aint dsl_scan_delay_completion = B_FALSE; /* set to delay scan completion */
0N/A/* max number of blocks to free in a single TXG */
0N/Auint64_t zfs_free_max_blocks = UINT64_MAX;
0N/A
0N/A#define DSL_SCAN_IS_SCRUB_RESILVER(scn) \
0N/A ((scn)->scn_phys.scn_func == POOL_SCAN_SCRUB || \
0N/A (scn)->scn_phys.scn_func == POOL_SCAN_RESILVER)
0N/A
0N/Aextern int zfs_txg_timeout;
0N/A
0N/A/* the order has to match pool_scan_type */
0N/Astatic scan_cb_t *scan_funcs[POOL_SCAN_FUNCS] = {
0N/A NULL,
0N/A dsl_scan_scrub_cb, /* POOL_SCAN_SCRUB */
0N/A dsl_scan_scrub_cb, /* POOL_SCAN_RESILVER */
0N/A};
0N/A
0N/Aint
0N/Adsl_scan_init(dsl_pool_t *dp, uint64_t txg)
0N/A{
0N/A int err;
0N/A dsl_scan_t *scn;
0N/A spa_t *spa = dp->dp_spa;
0N/A uint64_t f;
0N/A
0N/A scn = dp->dp_scan = kmem_zalloc(sizeof (dsl_scan_t), KM_SLEEP);
0N/A scn->scn_dp = dp;
0N/A
0N/A /*
0N/A * It's possible that we're resuming a scan after a reboot so
0N/A * make sure that the scan_async_destroying flag is initialized
0N/A * appropriately.
0N/A */
0N/A ASSERT(!scn->scn_async_destroying);
0N/A scn->scn_async_destroying = spa_feature_is_active(dp->dp_spa,
0N/A SPA_FEATURE_ASYNC_DESTROY);
0N/A
0N/A err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
0N/A "scrub_func", sizeof (uint64_t), 1, &f);
0N/A if (err == 0) {
0N/A /*
0N/A * There was an old-style scrub in progress. Restart a
0N/A * new-style scrub from the beginning.
0N/A */
0N/A scn->scn_restart_txg = txg;
1064N/A zfs_dbgmsg("old-style scrub was in progress; "
1064N/A "restarting new-style scrub in txg %llu",
1064N/A scn->scn_restart_txg);
1064N/A
0N/A /*
0N/A * Load the queue obj from the old location so that it
0N/A * can be freed by dsl_scan_done().
0N/A */
0N/A (void) zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
0N/A "scrub_queue", sizeof (uint64_t), 1,
0N/A &scn->scn_phys.scn_queue_obj);
0N/A } else {
0N/A err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
0N/A DMU_POOL_SCAN, sizeof (uint64_t), SCAN_PHYS_NUMINTS,
0N/A &scn->scn_phys);
0N/A if (err == ENOENT)
0N/A return (0);
0N/A else if (err)
0N/A return (err);
0N/A
0N/A if (scn->scn_phys.scn_state == DSS_SCANNING &&
0N/A spa_prev_software_version(dp->dp_spa) < SPA_VERSION_SCAN) {
0N/A /*
0N/A * A new-type scrub was in progress on an old
0N/A * pool, and the pool was accessed by old
0N/A * software. Restart from the beginning, since
0N/A * the old software may have changed the pool in
0N/A * the meantime.
0N/A */
0N/A scn->scn_restart_txg = txg;
0N/A zfs_dbgmsg("new-style scrub was modified "
0N/A "by old software; restarting in txg %llu",
0N/A scn->scn_restart_txg);
0N/A }
0N/A }
0N/A
0N/A spa_scan_stat_init(spa);
0N/A return (0);
0N/A}
0N/A
0N/Avoid
0N/Adsl_scan_fini(dsl_pool_t *dp)
0N/A{
0N/A if (dp->dp_scan) {
0N/A kmem_free(dp->dp_scan, sizeof (dsl_scan_t));
0N/A dp->dp_scan = NULL;
0N/A }
0N/A}
0N/A
0N/A/* ARGSUSED */
0N/Astatic int
0N/Adsl_scan_setup_check(void *arg, dmu_tx_t *tx)
0N/A{
0N/A dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
0N/A
0N/A if (scn->scn_phys.scn_state == DSS_SCANNING)
0N/A return (SET_ERROR(EBUSY));
0N/A
0N/A return (0);
0N/A}
0N/A
0N/Astatic void
0N/Adsl_scan_setup_sync(void *arg, dmu_tx_t *tx)
0N/A{
0N/A dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
0N/A pool_scan_func_t *funcp = arg;
0N/A dmu_object_type_t ot = 0;
0N/A dsl_pool_t *dp = scn->scn_dp;
0N/A spa_t *spa = dp->dp_spa;
0N/A
0N/A ASSERT(scn->scn_phys.scn_state != DSS_SCANNING);
0N/A ASSERT(*funcp > POOL_SCAN_NONE && *funcp < POOL_SCAN_FUNCS);
0N/A bzero(&scn->scn_phys, sizeof (scn->scn_phys));
0N/A scn->scn_phys.scn_func = *funcp;
0N/A scn->scn_phys.scn_state = DSS_SCANNING;
0N/A scn->scn_phys.scn_min_txg = 0;
0N/A scn->scn_phys.scn_max_txg = tx->tx_txg;
0N/A scn->scn_phys.scn_ddt_class_max = DDT_CLASSES - 1; /* the entire DDT */
0N/A scn->scn_phys.scn_start_time = gethrestime_sec();
1064N/A scn->scn_phys.scn_errors = 0;
1064N/A scn->scn_phys.scn_to_examine = spa->spa_root_vdev->vdev_stat.vs_alloc;
1064N/A scn->scn_restart_txg = 0;
1064N/A scn->scn_done_txg = 0;
1064N/A spa_scan_stat_init(spa);
0N/A
0N/A if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
0N/A scn->scn_phys.scn_ddt_class_max = zfs_scrub_ddt_class_max;
0N/A
0N/A /* rewrite all disk labels */
0N/A vdev_config_dirty(spa->spa_root_vdev);
0N/A
0N/A if (vdev_resilver_needed(spa->spa_root_vdev,
0N/A &scn->scn_phys.scn_min_txg, &scn->scn_phys.scn_max_txg)) {
0N/A spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_START);
0N/A } else {
0N/A spa_event_notify(spa, NULL, ESC_ZFS_SCRUB_START);
0N/A }
0N/A
1150N/A spa->spa_scrub_started = B_TRUE;
0N/A /*
0N/A * If this is an incremental scrub, limit the DDT scrub phase
0N/A * to just the auto-ditto class (for correctness); the rest
0N/A * of the scrub should go faster using top-down pruning.
0N/A */
0N/A if (scn->scn_phys.scn_min_txg > TXG_INITIAL)
0N/A scn->scn_phys.scn_ddt_class_max = DDT_CLASS_DITTO;
0N/A
0N/A }
0N/A
0N/A /* back to the generic stuff */
0N/A
0N/A if (dp->dp_blkstats == NULL) {
0N/A dp->dp_blkstats =
0N/A kmem_alloc(sizeof (zfs_all_blkstats_t), KM_SLEEP);
0N/A }
0N/A bzero(dp->dp_blkstats, sizeof (zfs_all_blkstats_t));
0N/A
0N/A if (spa_version(spa) < SPA_VERSION_DSL_SCRUB)
0N/A ot = DMU_OT_ZAP_OTHER;
0N/A
0N/A scn->scn_phys.scn_queue_obj = zap_create(dp->dp_meta_objset,
0N/A ot ? ot : DMU_OT_SCAN_QUEUE, DMU_OT_NONE, 0, tx);
0N/A
0N/A dsl_scan_sync_state(scn, tx);
0N/A
0N/A spa_history_log_internal(spa, "scan setup", tx,
0N/A "func=%u mintxg=%llu maxtxg=%llu",
0N/A *funcp, scn->scn_phys.scn_min_txg, scn->scn_phys.scn_max_txg);
0N/A}
0N/A
0N/A/* ARGSUSED */
0N/Astatic void
0N/Adsl_scan_done(dsl_scan_t *scn, boolean_t complete, dmu_tx_t *tx)
0N/A{
0N/A static const char *old_names[] = {
0N/A "scrub_bookmark",
0N/A "scrub_ddt_bookmark",
0N/A "scrub_ddt_class_max",
0N/A "scrub_queue",
0N/A "scrub_min_txg",
0N/A "scrub_max_txg",
0N/A "scrub_func",
0N/A "scrub_errors",
0N/A NULL
0N/A };
0N/A
0N/A dsl_pool_t *dp = scn->scn_dp;
0N/A spa_t *spa = dp->dp_spa;
0N/A int i;
0N/A
0N/A /* Remove any remnants of an old-style scrub. */
350N/A for (i = 0; old_names[i]; i++) {
350N/A (void) zap_remove(dp->dp_meta_objset,
350N/A DMU_POOL_DIRECTORY_OBJECT, old_names[i], tx);
350N/A }
350N/A
350N/A if (scn->scn_phys.scn_queue_obj != 0) {
350N/A VERIFY(0 == dmu_object_free(dp->dp_meta_objset,
350N/A scn->scn_phys.scn_queue_obj, tx));
350N/A scn->scn_phys.scn_queue_obj = 0;
350N/A }
350N/A
350N/A /*
350N/A * If we were "restarted" from a stopped state, don't bother
0N/A * with anything else.
0N/A */
0N/A if (scn->scn_phys.scn_state != DSS_SCANNING)
0N/A return;
0N/A
0N/A if (complete)
0N/A scn->scn_phys.scn_state = DSS_FINISHED;
0N/A else
0N/A scn->scn_phys.scn_state = DSS_CANCELED;
0N/A
0N/A spa_history_log_internal(spa, "scan done", tx,
0N/A "complete=%u", complete);
0N/A
0N/A if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
0N/A mutex_enter(&spa->spa_scrub_lock);
0N/A while (spa->spa_scrub_inflight > 0) {
0N/A cv_wait(&spa->spa_scrub_io_cv,
0N/A &spa->spa_scrub_lock);
0N/A }
0N/A mutex_exit(&spa->spa_scrub_lock);
0N/A spa->spa_scrub_started = B_FALSE;
0N/A spa->spa_scrub_active = B_FALSE;
0N/A
0N/A /*
0N/A * If the scrub/resilver completed, update all DTLs to
0N/A * reflect this. Whether it succeeded or not, vacate
0N/A * all temporary scrub DTLs.
0N/A */
0N/A vdev_dtl_reassess(spa->spa_root_vdev, tx->tx_txg,
350N/A complete ? scn->scn_phys.scn_max_txg : 0, B_TRUE);
113N/A if (complete) {
113N/A spa_event_notify(spa, NULL, scn->scn_phys.scn_min_txg ?
113N/A ESC_ZFS_RESILVER_FINISH : ESC_ZFS_SCRUB_FINISH);
113N/A }
0N/A spa_errlog_rotate(spa);
642N/A
642N/A /*
642N/A * We may have finished replacing a device.
642N/A * Let the async thread assess this and handle the detach.
642N/A */
642N/A spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
642N/A }
642N/A
325N/A scn->scn_phys.scn_end_time = gethrestime_sec();
1491N/A}
1491N/A
1491N/A/* ARGSUSED */
0N/Astatic int
0N/Adsl_scan_cancel_check(void *arg, dmu_tx_t *tx)
0N/A{
0N/A dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
0N/A
0N/A if (scn->scn_phys.scn_state != DSS_SCANNING)
1438N/A return (SET_ERROR(ENOENT));
1438N/A return (0);
1438N/A}
1438N/A
0N/A/* ARGSUSED */
350N/Astatic void
350N/Adsl_scan_cancel_sync(void *arg, dmu_tx_t *tx)
350N/A{
350N/A dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
0N/A
0N/A dsl_scan_done(scn, B_FALSE, tx);
0N/A dsl_scan_sync_state(scn, tx);
0N/A}
0N/A
0N/Aint
0N/Adsl_scan_cancel(dsl_pool_t *dp)
389N/A{
389N/A return (dsl_sync_task(spa_name(dp->dp_spa), dsl_scan_cancel_check,
389N/A dsl_scan_cancel_sync, NULL, 3, ZFS_SPACE_CHECK_RESERVED));
389N/A}
389N/A
389N/Astatic void dsl_scan_visitbp(blkptr_t *bp,
0N/A const zbookmark_phys_t *zb, dnode_phys_t *dnp, arc_buf_t *pbuf,
0N/A dsl_dataset_t *ds, dsl_scan_t *scn, dmu_objset_type_t ostype,
0N/A dmu_tx_t *tx);
0N/Astatic void dsl_scan_visitdnode(dsl_scan_t *, dsl_dataset_t *ds,
0N/A dmu_objset_type_t ostype,
0N/A dnode_phys_t *dnp, arc_buf_t *buf, uint64_t object, dmu_tx_t *tx);
462N/A
462N/Avoid
462N/Adsl_free(dsl_pool_t *dp, uint64_t txg, const blkptr_t *bp)
0N/A{
0N/A zio_free(dp->dp_spa, txg, bp);
0N/A}
0N/A
0N/Avoid
0N/Adsl_free_sync(zio_t *pio, dsl_pool_t *dp, uint64_t txg, const blkptr_t *bpp)
0N/A{
0N/A ASSERT(dsl_pool_sync_context(dp));
0N/A zio_nowait(zio_free_sync(pio, dp->dp_spa, txg, bpp, pio->io_flags));
0N/A}
0N/A
0N/Astatic uint64_t
0N/Adsl_scan_ds_maxtxg(dsl_dataset_t *ds)
0N/A{
0N/A uint64_t smt = ds->ds_dir->dd_pool->dp_scan->scn_phys.scn_max_txg;
0N/A if (dsl_dataset_is_snapshot(ds))
0N/A return (MIN(smt, ds->ds_phys->ds_creation_txg));
0N/A return (smt);
0N/A}
0N/A
0N/Astatic void
0N/Adsl_scan_sync_state(dsl_scan_t *scn, dmu_tx_t *tx)
0N/A{
0N/A VERIFY0(zap_update(scn->scn_dp->dp_meta_objset,
0N/A DMU_POOL_DIRECTORY_OBJECT,
0N/A DMU_POOL_SCAN, sizeof (uint64_t), SCAN_PHYS_NUMINTS,
0N/A &scn->scn_phys, tx));
0N/A}
0N/A
0N/Astatic boolean_t
0N/Adsl_scan_check_pause(dsl_scan_t *scn, const zbookmark_phys_t *zb)
0N/A{
0N/A uint64_t elapsed_nanosecs;
0N/A int mintime;
0N/A
0N/A /* we never skip user/group accounting objects */
0N/A if (zb && (int64_t)zb->zb_object < 0)
0N/A return (B_FALSE);
0N/A
0N/A if (scn->scn_pausing)
0N/A return (B_TRUE); /* we're already pausing */
0N/A
0N/A if (!ZB_IS_ZERO(&scn->scn_phys.scn_bookmark))
0N/A return (B_FALSE); /* we're resuming */
0N/A
0N/A /* We only know how to resume from level-0 blocks. */
0N/A if (zb && zb->zb_level != 0)
0N/A return (B_FALSE);
0N/A
0N/A mintime = (scn->scn_phys.scn_func == POOL_SCAN_RESILVER) ?
0N/A zfs_resilver_min_time_ms : zfs_scan_min_time_ms;
0N/A elapsed_nanosecs = gethrtime() - scn->scn_sync_start_time;
0N/A if (elapsed_nanosecs / NANOSEC > zfs_txg_timeout ||
0N/A (NSEC2MSEC(elapsed_nanosecs) > mintime &&
0N/A txg_sync_waiting(scn->scn_dp)) ||
0N/A spa_shutting_down(scn->scn_dp->dp_spa)) {
0N/A if (zb) {
0N/A dprintf("pausing at bookmark %llx/%llx/%llx/%llx\n",
0N/A (longlong_t)zb->zb_objset,
0N/A (longlong_t)zb->zb_object,
0N/A (longlong_t)zb->zb_level,
0N/A (longlong_t)zb->zb_blkid);
0N/A scn->scn_phys.scn_bookmark = *zb;
0N/A }
0N/A dprintf("pausing at DDT bookmark %llx/%llx/%llx/%llx\n",
0N/A (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_class,
0N/A (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_type,
0N/A (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_checksum,
0N/A (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_cursor);
0N/A scn->scn_pausing = B_TRUE;
0N/A return (B_TRUE);
0N/A }
0N/A return (B_FALSE);
0N/A}
0N/A
0N/Atypedef struct zil_scan_arg {
0N/A dsl_pool_t *zsa_dp;
0N/A zil_header_t *zsa_zh;
0N/A} zil_scan_arg_t;
0N/A
0N/A/* ARGSUSED */
0N/Astatic int
0N/Adsl_scan_zil_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
0N/A{
0N/A zil_scan_arg_t *zsa = arg;
0N/A dsl_pool_t *dp = zsa->zsa_dp;
0N/A dsl_scan_t *scn = dp->dp_scan;
0N/A zil_header_t *zh = zsa->zsa_zh;
0N/A zbookmark_phys_t zb;
0N/A
0N/A if (BP_IS_HOLE(bp) || bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
0N/A return (0);
0N/A
0N/A /*
0N/A * One block ("stubby") can be allocated a long time ago; we
0N/A * want to visit that one because it has been allocated
0N/A * (on-disk) even if it hasn't been claimed (even though for
0N/A * scrub there's nothing to do to it).
0N/A */
0N/A if (claim_txg == 0 && bp->blk_birth >= spa_first_txg(dp->dp_spa))
0N/A return (0);
0N/A
0N/A SET_BOOKMARK(&zb, zh->zh_log.blk_cksum.zc_word[ZIL_ZC_OBJSET],
0N/A ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, bp->blk_cksum.zc_word[ZIL_ZC_SEQ]);
0N/A
0N/A VERIFY(0 == scan_funcs[scn->scn_phys.scn_func](dp, bp, &zb));
0N/A return (0);
0N/A}
0N/A
0N/A/* ARGSUSED */
0N/Astatic int
0N/Adsl_scan_zil_record(zilog_t *zilog, lr_t *lrc, void *arg, uint64_t claim_txg)
0N/A{
0N/A if (lrc->lrc_txtype == TX_WRITE) {
0N/A zil_scan_arg_t *zsa = arg;
0N/A dsl_pool_t *dp = zsa->zsa_dp;
0N/A dsl_scan_t *scn = dp->dp_scan;
0N/A zil_header_t *zh = zsa->zsa_zh;
0N/A lr_write_t *lr = (lr_write_t *)lrc;
0N/A blkptr_t *bp = &lr->lr_blkptr;
0N/A zbookmark_phys_t zb;
0N/A
0N/A if (BP_IS_HOLE(bp) ||
0N/A bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
0N/A return (0);
0N/A
0N/A /*
0N/A * birth can be < claim_txg if this record's txg is
0N/A * already txg sync'ed (but this log block contains
0N/A * other records that are not synced)
0N/A */
0N/A if (claim_txg == 0 || bp->blk_birth < claim_txg)
0N/A return (0);
0N/A
0N/A SET_BOOKMARK(&zb, zh->zh_log.blk_cksum.zc_word[ZIL_ZC_OBJSET],
0N/A lr->lr_foid, ZB_ZIL_LEVEL,
0N/A lr->lr_offset / BP_GET_LSIZE(bp));
0N/A
0N/A VERIFY(0 == scan_funcs[scn->scn_phys.scn_func](dp, bp, &zb));
0N/A }
0N/A return (0);
0N/A}
681N/A
681N/Astatic void
681N/Adsl_scan_zil(dsl_pool_t *dp, zil_header_t *zh)
681N/A{
169N/A uint64_t claim_txg = zh->zh_claim_txg;
169N/A zil_scan_arg_t zsa = { dp, zh };
681N/A zilog_t *zilog;
681N/A
681N/A /*
0N/A * We only want to visit blocks that have been claimed but not yet
0N/A * replayed (or, in read-only mode, blocks that *would* be claimed).
0N/A */
0N/A if (claim_txg == 0 && spa_writeable(dp->dp_spa))
0N/A return;
0N/A
434N/A zilog = zil_alloc(dp->dp_meta_objset, zh);
0N/A
0N/A (void) zil_parse(zilog, dsl_scan_zil_block, dsl_scan_zil_record, &zsa,
0N/A claim_txg);
0N/A
0N/A zil_free(zilog);
0N/A}
0N/A
0N/A/* ARGSUSED */
0N/Astatic void
0N/Adsl_scan_prefetch(dsl_scan_t *scn, arc_buf_t *buf, blkptr_t *bp,
0N/A uint64_t objset, uint64_t object, uint64_t blkid)
0N/A{
0N/A zbookmark_phys_t czb;
0N/A uint32_t flags = ARC_NOWAIT | ARC_PREFETCH;
0N/A
0N/A if (zfs_no_scrub_prefetch)
0N/A return;
0N/A
0N/A if (BP_IS_HOLE(bp) || bp->blk_birth <= scn->scn_phys.scn_min_txg ||
0N/A (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_DNODE))
0N/A return;
0N/A
0N/A SET_BOOKMARK(&czb, objset, object, BP_GET_LEVEL(bp), blkid);
0N/A
0N/A (void) arc_read(scn->scn_zio_root, scn->scn_dp->dp_spa, bp,
0N/A NULL, NULL, ZIO_PRIORITY_ASYNC_READ,
0N/A ZIO_FLAG_CANFAIL | ZIO_FLAG_SCAN_THREAD, &flags, &czb);
0N/A}
0N/A
0N/Astatic boolean_t
0N/Adsl_scan_check_resume(dsl_scan_t *scn, const dnode_phys_t *dnp,
0N/A const zbookmark_phys_t *zb)
0N/A{
0N/A /*
0N/A * We never skip over user/group accounting objects (obj<0)
0N/A */
0N/A if (!ZB_IS_ZERO(&scn->scn_phys.scn_bookmark) &&
0N/A (int64_t)zb->zb_object >= 0) {
0N/A /*
0N/A * If we already visited this bp & everything below (in
0N/A * a prior txg sync), don't bother doing it again.
0N/A */
0N/A if (zbookmark_is_before(dnp, zb, &scn->scn_phys.scn_bookmark))
0N/A return (B_TRUE);
0N/A
0N/A /*
0N/A * If we found the block we're trying to resume from, or
0N/A * we went past it to a different object, zero it out to
0N/A * indicate that it's OK to start checking for pausing
0N/A * again.
0N/A */
0N/A if (bcmp(zb, &scn->scn_phys.scn_bookmark, sizeof (*zb)) == 0 ||
0N/A zb->zb_object > scn->scn_phys.scn_bookmark.zb_object) {
0N/A dprintf("resuming at %llx/%llx/%llx/%llx\n",
0N/A (longlong_t)zb->zb_objset,
0N/A (longlong_t)zb->zb_object,
0N/A (longlong_t)zb->zb_level,
0N/A (longlong_t)zb->zb_blkid);
0N/A bzero(&scn->scn_phys.scn_bookmark, sizeof (*zb));
0N/A }
0N/A }
0N/A return (B_FALSE);
0N/A}
0N/A
0N/A/*
0N/A * Return nonzero on i/o error.
0N/A * Return new buf to write out in *bufp.
0N/A */
0N/Astatic int
0N/Adsl_scan_recurse(dsl_scan_t *scn, dsl_dataset_t *ds, dmu_objset_type_t ostype,
0N/A dnode_phys_t *dnp, const blkptr_t *bp,
0N/A const zbookmark_phys_t *zb, dmu_tx_t *tx, arc_buf_t **bufp)
0N/A{
0N/A dsl_pool_t *dp = scn->scn_dp;
0N/A int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCAN_THREAD;
0N/A int err;
0N/A
0N/A if (BP_GET_LEVEL(bp) > 0) {
0N/A uint32_t flags = ARC_WAIT;
0N/A int i;
0N/A blkptr_t *cbp;
0N/A int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
0N/A
0N/A err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, bufp,
0N/A ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
0N/A if (err) {
0N/A scn->scn_phys.scn_errors++;
0N/A return (err);
0N/A }
0N/A for (i = 0, cbp = (*bufp)->b_data; i < epb; i++, cbp++) {
0N/A dsl_scan_prefetch(scn, *bufp, cbp, zb->zb_objset,
0N/A zb->zb_object, zb->zb_blkid * epb + i);
0N/A }
0N/A for (i = 0, cbp = (*bufp)->b_data; i < epb; i++, cbp++) {
0N/A zbookmark_phys_t czb;
0N/A
0N/A SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
0N/A zb->zb_level - 1,
0N/A zb->zb_blkid * epb + i);
0N/A dsl_scan_visitbp(cbp, &czb, dnp,
0N/A *bufp, ds, scn, ostype, tx);
0N/A }
0N/A } else if (BP_GET_TYPE(bp) == DMU_OT_USERGROUP_USED) {
0N/A uint32_t flags = ARC_WAIT;
0N/A
0N/A err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, bufp,
0N/A ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
0N/A if (err) {
0N/A scn->scn_phys.scn_errors++;
0N/A return (err);
0N/A }
0N/A } else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) {
0N/A uint32_t flags = ARC_WAIT;
0N/A dnode_phys_t *cdnp;
0N/A int i, j;
0N/A int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
0N/A
0N/A err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, bufp,
0N/A ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
263N/A if (err) {
0N/A scn->scn_phys.scn_errors++;
0N/A return (err);
263N/A }
263N/A for (i = 0, cdnp = (*bufp)->b_data; i < epb; i++, cdnp++) {
263N/A for (j = 0; j < cdnp->dn_nblkptr; j++) {
263N/A blkptr_t *cbp = &cdnp->dn_blkptr[j];
263N/A dsl_scan_prefetch(scn, *bufp, cbp,
263N/A zb->zb_objset, zb->zb_blkid * epb + i, j);
481N/A }
481N/A }
481N/A for (i = 0, cdnp = (*bufp)->b_data; i < epb; i++, cdnp++) {
0N/A dsl_scan_visitdnode(scn, ds, ostype,
0N/A cdnp, *bufp, zb->zb_blkid * epb + i, tx);
0N/A }
0N/A
0N/A } else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) {
0N/A uint32_t flags = ARC_WAIT;
0N/A objset_phys_t *osp;
0N/A
0N/A err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, bufp,
1410N/A ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
1410N/A if (err) {
1410N/A scn->scn_phys.scn_errors++;
1410N/A return (err);
1410N/A }
0N/A
0N/A osp = (*bufp)->b_data;
0N/A
0N/A dsl_scan_visitdnode(scn, ds, osp->os_type,
0N/A &osp->os_meta_dnode, *bufp, DMU_META_DNODE_OBJECT, tx);
0N/A
0N/A if (OBJSET_BUF_HAS_USERUSED(*bufp)) {
0N/A /*
0N/A * We also always visit user/group accounting
0N/A * objects, and never skip them, even if we are
0N/A * pausing. This is necessary so that the space
0N/A * deltas from this txg get integrated.
0N/A */
0N/A dsl_scan_visitdnode(scn, ds, osp->os_type,
0N/A &osp->os_groupused_dnode, *bufp,
0N/A DMU_GROUPUSED_OBJECT, tx);
0N/A dsl_scan_visitdnode(scn, ds, osp->os_type,
0N/A &osp->os_userused_dnode, *bufp,
0N/A DMU_USERUSED_OBJECT, tx);
0N/A }
0N/A }
0N/A
0N/A return (0);
0N/A}
0N/A
615N/Astatic void
615N/Adsl_scan_visitdnode(dsl_scan_t *scn, dsl_dataset_t *ds,
615N/A dmu_objset_type_t ostype, dnode_phys_t *dnp, arc_buf_t *buf,
615N/A uint64_t object, dmu_tx_t *tx)
615N/A{
615N/A int j;
0N/A
0N/A for (j = 0; j < dnp->dn_nblkptr; j++) {
0N/A zbookmark_phys_t czb;
0N/A
0N/A SET_BOOKMARK(&czb, ds ? ds->ds_object : 0, object,
0N/A dnp->dn_nlevels - 1, j);
0N/A dsl_scan_visitbp(&dnp->dn_blkptr[j],
0N/A &czb, dnp, buf, ds, scn, ostype, tx);
0N/A }
0N/A
0N/A if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
0N/A zbookmark_phys_t czb;
0N/A SET_BOOKMARK(&czb, ds ? ds->ds_object : 0, object,
0N/A 0, DMU_SPILL_BLKID);
0N/A dsl_scan_visitbp(&dnp->dn_spill,
0N/A &czb, dnp, buf, ds, scn, ostype, tx);
0N/A }
0N/A}
0N/A
0N/A/*
0N/A * The arguments are in this order because mdb can only print the
0N/A * first 5; we want them to be useful.
0N/A */
0N/Astatic void
0N/Adsl_scan_visitbp(blkptr_t *bp, const zbookmark_phys_t *zb,
0N/A dnode_phys_t *dnp, arc_buf_t *pbuf,
0N/A dsl_dataset_t *ds, dsl_scan_t *scn, dmu_objset_type_t ostype,
0N/A dmu_tx_t *tx)
0N/A{
0N/A dsl_pool_t *dp = scn->scn_dp;
0N/A arc_buf_t *buf = NULL;
0N/A blkptr_t bp_toread = *bp;
0N/A
0N/A /* ASSERT(pbuf == NULL || arc_released(pbuf)); */
0N/A
0N/A if (dsl_scan_check_pause(scn, zb))
989N/A return;
989N/A
989N/A if (dsl_scan_check_resume(scn, dnp, zb))
989N/A return;
989N/A
0N/A if (BP_IS_HOLE(bp))
0N/A return;
0N/A
0N/A scn->scn_visited_this_txg++;
0N/A
0N/A dprintf_bp(bp,
0N/A "visiting ds=%p/%llu zb=%llx/%llx/%llx/%llx buf=%p bp=%p",
0N/A ds, ds ? ds->ds_object : 0,
0N/A zb->zb_objset, zb->zb_object, zb->zb_level, zb->zb_blkid,
0N/A pbuf, bp);
0N/A
0N/A if (bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
0N/A return;
0N/A
0N/A if (dsl_scan_recurse(scn, ds, ostype, dnp, &bp_toread, zb, tx,
100N/A &buf) != 0)
0N/A return;
0N/A
1207N/A /*
1207N/A * If dsl_scan_ddt() has aready visited this block, it will have
1207N/A * already done any translations or scrubbing, so don't call the
100N/A * callback again.
100N/A */
100N/A if (ddt_class_contains(dp->dp_spa,
370N/A scn->scn_phys.scn_ddt_class_max, bp)) {
100N/A ASSERT(buf == NULL);
100N/A return;
100N/A }
0N/A
0N/A /*
100N/A * If this block is from the future (after cur_max_txg), then we
0N/A * are doing this on behalf of a deleted snapshot, and we will
0N/A * revisit the future block on the next pass of this dataset.
0N/A * Don't scan it now unless we need to because something
0N/A * under it was modified.
0N/A */
0N/A if (BP_PHYSICAL_BIRTH(bp) <= scn->scn_phys.scn_cur_max_txg) {
0N/A scan_funcs[scn->scn_phys.scn_func](dp, bp, zb);
0N/A }
0N/A if (buf)
0N/A (void) arc_buf_remove_ref(buf, &buf);
0N/A}
0N/A
0N/Astatic void
0N/Adsl_scan_visit_rootbp(dsl_scan_t *scn, dsl_dataset_t *ds, blkptr_t *bp,
0N/A dmu_tx_t *tx)
0N/A{
0N/A zbookmark_phys_t zb;
0N/A
0N/A SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
0N/A ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
0N/A dsl_scan_visitbp(bp, &zb, NULL, NULL,
0N/A ds, scn, DMU_OST_NONE, tx);
0N/A
0N/A dprintf_ds(ds, "finished scan%s", "");
100N/A}
0N/A
0N/Avoid
0N/Adsl_scan_ds_destroyed(dsl_dataset_t *ds, dmu_tx_t *tx)
0N/A{
0N/A dsl_pool_t *dp = ds->ds_dir->dd_pool;
0N/A dsl_scan_t *scn = dp->dp_scan;
0N/A uint64_t mintxg;
0N/A
0N/A if (scn->scn_phys.scn_state != DSS_SCANNING)
0N/A return;
0N/A
0N/A if (scn->scn_phys.scn_bookmark.zb_objset == ds->ds_object) {
0N/A if (dsl_dataset_is_snapshot(ds)) {
0N/A /* Note, scn_cur_{min,max}_txg stays the same. */
1064N/A scn->scn_phys.scn_bookmark.zb_objset =
0N/A ds->ds_phys->ds_next_snap_obj;
0N/A zfs_dbgmsg("destroying ds %llu; currently traversing; "
0N/A "reset zb_objset to %llu",
0N/A (u_longlong_t)ds->ds_object,
0N/A (u_longlong_t)ds->ds_phys->ds_next_snap_obj);
0N/A scn->scn_phys.scn_flags |= DSF_VISIT_DS_AGAIN;
0N/A } else {
0N/A SET_BOOKMARK(&scn->scn_phys.scn_bookmark,
0N/A ZB_DESTROYED_OBJSET, 0, 0, 0);
0N/A zfs_dbgmsg("destroying ds %llu; currently traversing; "
0N/A "reset bookmark to -1,0,0,0",
0N/A (u_longlong_t)ds->ds_object);
0N/A }
0N/A } else if (zap_lookup_int_key(dp->dp_meta_objset,
0N/A scn->scn_phys.scn_queue_obj, ds->ds_object, &mintxg) == 0) {
0N/A ASSERT3U(ds->ds_phys->ds_num_children, <=, 1);
0N/A VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
0N/A scn->scn_phys.scn_queue_obj, ds->ds_object, tx));
0N/A if (dsl_dataset_is_snapshot(ds)) {
0N/A /*
0N/A * We keep the same mintxg; it could be >
0N/A * ds_creation_txg if the previous snapshot was
0N/A * deleted too.
0N/A */
0N/A VERIFY(zap_add_int_key(dp->dp_meta_objset,
0N/A scn->scn_phys.scn_queue_obj,
0N/A ds->ds_phys->ds_next_snap_obj, mintxg, tx) == 0);
0N/A zfs_dbgmsg("destroying ds %llu; in queue; "
0N/A "replacing with %llu",
1064N/A (u_longlong_t)ds->ds_object,
0N/A (u_longlong_t)ds->ds_phys->ds_next_snap_obj);
0N/A } else {
0N/A zfs_dbgmsg("destroying ds %llu; in queue; removing",
0N/A (u_longlong_t)ds->ds_object);
0N/A }
0N/A } else {
0N/A zfs_dbgmsg("destroying ds %llu; ignoring",
0N/A (u_longlong_t)ds->ds_object);
0N/A }
0N/A
0N/A /*
0N/A * dsl_scan_sync() should be called after this, and should sync
0N/A * out our changed state, but just to be safe, do it here.
0N/A */
0N/A dsl_scan_sync_state(scn, tx);
0N/A}
0N/A
0N/Avoid
0N/Adsl_scan_ds_snapshotted(dsl_dataset_t *ds, dmu_tx_t *tx)
0N/A{
0N/A dsl_pool_t *dp = ds->ds_dir->dd_pool;
0N/A dsl_scan_t *scn = dp->dp_scan;
0N/A uint64_t mintxg;
0N/A
0N/A if (scn->scn_phys.scn_state != DSS_SCANNING)
474N/A return;
132N/A
132N/A ASSERT(ds->ds_phys->ds_prev_snap_obj != 0);
0N/A
0N/A if (scn->scn_phys.scn_bookmark.zb_objset == ds->ds_object) {
0N/A scn->scn_phys.scn_bookmark.zb_objset =
0N/A ds->ds_phys->ds_prev_snap_obj;
0N/A zfs_dbgmsg("snapshotting ds %llu; currently traversing; "
0N/A "reset zb_objset to %llu",
0N/A (u_longlong_t)ds->ds_object,
0N/A (u_longlong_t)ds->ds_phys->ds_prev_snap_obj);
0N/A } else if (zap_lookup_int_key(dp->dp_meta_objset,
0N/A scn->scn_phys.scn_queue_obj, ds->ds_object, &mintxg) == 0) {
0N/A VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
514N/A scn->scn_phys.scn_queue_obj, ds->ds_object, tx));
514N/A VERIFY(zap_add_int_key(dp->dp_meta_objset,
514N/A scn->scn_phys.scn_queue_obj,
514N/A ds->ds_phys->ds_prev_snap_obj, mintxg, tx) == 0);
514N/A zfs_dbgmsg("snapshotting ds %llu; in queue; "
514N/A "replacing with %llu",
514N/A (u_longlong_t)ds->ds_object,
1064N/A (u_longlong_t)ds->ds_phys->ds_prev_snap_obj);
514N/A }
514N/A dsl_scan_sync_state(scn, tx);
514N/A}
1064N/A
514N/Avoid
514N/Adsl_scan_ds_clone_swapped(dsl_dataset_t *ds1, dsl_dataset_t *ds2, dmu_tx_t *tx)
1064N/A{
0N/A dsl_pool_t *dp = ds1->ds_dir->dd_pool;
0N/A dsl_scan_t *scn = dp->dp_scan;
0N/A uint64_t mintxg;
0N/A
0N/A if (scn->scn_phys.scn_state != DSS_SCANNING)
0N/A return;
0N/A
0N/A if (scn->scn_phys.scn_bookmark.zb_objset == ds1->ds_object) {
0N/A scn->scn_phys.scn_bookmark.zb_objset = ds2->ds_object;
0N/A zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
0N/A "reset zb_objset to %llu",
0N/A (u_longlong_t)ds1->ds_object,
0N/A (u_longlong_t)ds2->ds_object);
0N/A } else if (scn->scn_phys.scn_bookmark.zb_objset == ds2->ds_object) {
0N/A scn->scn_phys.scn_bookmark.zb_objset = ds1->ds_object;
0N/A zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
0N/A "reset zb_objset to %llu",
0N/A (u_longlong_t)ds2->ds_object,
0N/A (u_longlong_t)ds1->ds_object);
0N/A }
0N/A
0N/A if (zap_lookup_int_key(dp->dp_meta_objset, scn->scn_phys.scn_queue_obj,
0N/A ds1->ds_object, &mintxg) == 0) {
370N/A int err;
0N/A
0N/A ASSERT3U(mintxg, ==, ds1->ds_phys->ds_prev_snap_txg);
0N/A ASSERT3U(mintxg, ==, ds2->ds_phys->ds_prev_snap_txg);
0N/A VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
0N/A scn->scn_phys.scn_queue_obj, ds1->ds_object, tx));
0N/A err = zap_add_int_key(dp->dp_meta_objset,
0N/A scn->scn_phys.scn_queue_obj, ds2->ds_object, mintxg, tx);
0N/A VERIFY(err == 0 || err == EEXIST);
1507N/A if (err == EEXIST) {
1507N/A /* Both were there to begin with */
1507N/A VERIFY(0 == zap_add_int_key(dp->dp_meta_objset,
1507N/A scn->scn_phys.scn_queue_obj,
0N/A ds1->ds_object, mintxg, tx));
0N/A }
0N/A zfs_dbgmsg("clone_swap ds %llu; in queue; "
0N/A "replacing with %llu",
0N/A (u_longlong_t)ds1->ds_object,
0N/A (u_longlong_t)ds2->ds_object);
0N/A } else if (zap_lookup_int_key(dp->dp_meta_objset,
0N/A scn->scn_phys.scn_queue_obj, ds2->ds_object, &mintxg) == 0) {
0N/A ASSERT3U(mintxg, ==, ds1->ds_phys->ds_prev_snap_txg);
0N/A ASSERT3U(mintxg, ==, ds2->ds_phys->ds_prev_snap_txg);
0N/A VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
0N/A scn->scn_phys.scn_queue_obj, ds2->ds_object, tx));
0N/A VERIFY(0 == zap_add_int_key(dp->dp_meta_objset,
0N/A scn->scn_phys.scn_queue_obj, ds1->ds_object, mintxg, tx));
0N/A zfs_dbgmsg("clone_swap ds %llu; in queue; "
0N/A "replacing with %llu",
0N/A (u_longlong_t)ds2->ds_object,
0N/A (u_longlong_t)ds1->ds_object);
0N/A }
1064N/A
1064N/A dsl_scan_sync_state(scn, tx);
1064N/A}
0N/A
0N/Astruct enqueue_clones_arg {
0N/A dmu_tx_t *tx;
0N/A uint64_t originobj;
0N/A};
0N/A
0N/A/* ARGSUSED */
0N/Astatic int
0N/Aenqueue_clones_cb(dsl_pool_t *dp, dsl_dataset_t *hds, void *arg)
0N/A{
0N/A struct enqueue_clones_arg *eca = arg;
0N/A dsl_dataset_t *ds;
0N/A int err;
0N/A dsl_scan_t *scn = dp->dp_scan;
0N/A
0N/A if (hds->ds_dir->dd_phys->dd_origin_obj != eca->originobj)
0N/A return (0);
0N/A
0N/A err = dsl_dataset_hold_obj(dp, hds->ds_object, FTAG, &ds);
0N/A if (err)
0N/A return (err);
0N/A
0N/A while (ds->ds_phys->ds_prev_snap_obj != eca->originobj) {
0N/A dsl_dataset_t *prev;
0N/A err = dsl_dataset_hold_obj(dp,
0N/A ds->ds_phys->ds_prev_snap_obj, FTAG, &prev);
0N/A
0N/A dsl_dataset_rele(ds, FTAG);
0N/A if (err)
0N/A return (err);
0N/A ds = prev;
0N/A }
0N/A VERIFY(zap_add_int_key(dp->dp_meta_objset,
0N/A scn->scn_phys.scn_queue_obj, ds->ds_object,
0N/A ds->ds_phys->ds_prev_snap_txg, eca->tx) == 0);
0N/A dsl_dataset_rele(ds, FTAG);
0N/A return (0);
0N/A}
0N/A
0N/Astatic void
0N/Adsl_scan_visitds(dsl_scan_t *scn, uint64_t dsobj, dmu_tx_t *tx)
0N/A{
0N/A dsl_pool_t *dp = scn->scn_dp;
0N/A dsl_dataset_t *ds;
0N/A objset_t *os;
0N/A
0N/A VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
0N/A
0N/A if (dmu_objset_from_ds(ds, &os))
1064N/A goto out;
0N/A
0N/A /*
0N/A * Only the ZIL in the head (non-snapshot) is valid. Even though
0N/A * snapshots can have ZIL block pointers (which may be the same
0N/A * BP as in the head), they must be ignored. So we traverse the
0N/A * ZIL here, rather than in scan_recurse(), because the regular
0N/A * snapshot block-sharing rules don't apply to it.
0N/A */
0N/A if (DSL_SCAN_IS_SCRUB_RESILVER(scn) && !dsl_dataset_is_snapshot(ds))
0N/A dsl_scan_zil(dp, &os->os_zil_header);
0N/A
0N/A /*
0N/A * Iterate over the bps in this ds.
0N/A */
0N/A dmu_buf_will_dirty(ds->ds_dbuf, tx);
0N/A dsl_scan_visit_rootbp(scn, ds, &ds->ds_phys->ds_bp, tx);
0N/A
0N/A char *dsname = kmem_alloc(ZFS_MAXNAMELEN, KM_SLEEP);
0N/A dsl_dataset_name(ds, dsname);
0N/A zfs_dbgmsg("scanned dataset %llu (%s) with min=%llu max=%llu; "
0N/A "pausing=%u",
0N/A (longlong_t)dsobj, dsname,
0N/A (longlong_t)scn->scn_phys.scn_cur_min_txg,
0N/A (longlong_t)scn->scn_phys.scn_cur_max_txg,
0N/A (int)scn->scn_pausing);
0N/A kmem_free(dsname, ZFS_MAXNAMELEN);
0N/A
0N/A if (scn->scn_pausing)
0N/A goto out;
0N/A
0N/A /*
0N/A * We've finished this pass over this dataset.
0N/A */
0N/A
0N/A /*
0N/A * If we did not completely visit this dataset, do another pass.
0N/A */
0N/A if (scn->scn_phys.scn_flags & DSF_VISIT_DS_AGAIN) {
0N/A zfs_dbgmsg("incomplete pass; visiting again");
0N/A scn->scn_phys.scn_flags &= ~DSF_VISIT_DS_AGAIN;
0N/A VERIFY(zap_add_int_key(dp->dp_meta_objset,
219N/A scn->scn_phys.scn_queue_obj, ds->ds_object,
219N/A scn->scn_phys.scn_cur_max_txg, tx) == 0);
219N/A goto out;
0N/A }
0N/A
0N/A /*
0N/A * Add descendent datasets to work queue.
0N/A */
0N/A if (ds->ds_phys->ds_next_snap_obj != 0) {
0N/A VERIFY(zap_add_int_key(dp->dp_meta_objset,
0N/A scn->scn_phys.scn_queue_obj, ds->ds_phys->ds_next_snap_obj,
0N/A ds->ds_phys->ds_creation_txg, tx) == 0);
0N/A }
0N/A if (ds->ds_phys->ds_num_children > 1) {
0N/A boolean_t usenext = B_FALSE;
71N/A if (ds->ds_phys->ds_next_clones_obj != 0) {
71N/A uint64_t count;
71N/A /*
71N/A * A bug in a previous version of the code could
71N/A * cause upgrade_clones_cb() to not set
71N/A * ds_next_snap_obj when it should, leading to a
405N/A * missing entry. Therefore we can only use the
405N/A * next_clones_obj when its count is correct.
405N/A */
405N/A int err = zap_count(dp->dp_meta_objset,
405N/A ds->ds_phys->ds_next_clones_obj, &count);
405N/A if (err == 0 &&
0N/A count == ds->ds_phys->ds_num_children - 1)
1366N/A usenext = B_TRUE;
1366N/A }
0N/A
0N/A if (usenext) {
0N/A VERIFY0(zap_join_key(dp->dp_meta_objset,
0N/A ds->ds_phys->ds_next_clones_obj,
0N/A scn->scn_phys.scn_queue_obj,
0N/A ds->ds_phys->ds_creation_txg, tx));
0N/A } else {
0N/A struct enqueue_clones_arg eca;
0N/A eca.tx = tx;
0N/A eca.originobj = ds->ds_object;
0N/A
0N/A VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
0N/A enqueue_clones_cb, &eca, DS_FIND_CHILDREN));
0N/A }
0N/A }
0N/A
0N/Aout:
0N/A dsl_dataset_rele(ds, FTAG);
0N/A}
0N/A
0N/A/* ARGSUSED */
0N/Astatic int
0N/Aenqueue_cb(dsl_pool_t *dp, dsl_dataset_t *hds, void *arg)
0N/A{
0N/A dmu_tx_t *tx = arg;
0N/A dsl_dataset_t *ds;
0N/A int err;
0N/A dsl_scan_t *scn = dp->dp_scan;
0N/A
0N/A err = dsl_dataset_hold_obj(dp, hds->ds_object, FTAG, &ds);
0N/A if (err)
0N/A return (err);
0N/A
0N/A while (ds->ds_phys->ds_prev_snap_obj != 0) {
0N/A dsl_dataset_t *prev;
0N/A err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
0N/A FTAG, &prev);
0N/A if (err) {
0N/A dsl_dataset_rele(ds, FTAG);
370N/A return (err);
0N/A }
0N/A
880N/A /*
880N/A * If this is a clone, we don't need to worry about it for now.
880N/A */
0N/A if (prev->ds_phys->ds_next_snap_obj != ds->ds_object) {
0N/A dsl_dataset_rele(ds, FTAG);
0N/A dsl_dataset_rele(prev, FTAG);
0N/A return (0);
0N/A }
0N/A dsl_dataset_rele(ds, FTAG);
0N/A ds = prev;
0N/A }
0N/A
1494N/A VERIFY(zap_add_int_key(dp->dp_meta_objset, scn->scn_phys.scn_queue_obj,
1494N/A ds->ds_object, ds->ds_phys->ds_prev_snap_txg, tx) == 0);
1494N/A dsl_dataset_rele(ds, FTAG);
0N/A return (0);
0N/A}
0N/A
0N/A/*
0N/A * Scrub/dedup interaction.
0N/A *
0N/A * If there are N references to a deduped block, we don't want to scrub it
0N/A * N times -- ideally, we should scrub it exactly once.
0N/A *
0N/A * We leverage the fact that the dde's replication class (enum ddt_class)
0N/A * is ordered from highest replication class (DDT_CLASS_DITTO) to lowest
0N/A * (DDT_CLASS_UNIQUE) so that we may walk the DDT in that order.
0N/A *
0N/A * To prevent excess scrubbing, the scrub begins by walking the DDT
0N/A * to find all blocks with refcnt > 1, and scrubs each of these once.
0N/A * Since there are two replication classes which contain blocks with
0N/A * refcnt > 1, we scrub the highest replication class (DDT_CLASS_DITTO) first.
0N/A * Finally the top-down scrub begins, only visiting blocks with refcnt == 1.
0N/A *
0N/A * There would be nothing more to say if a block's refcnt couldn't change
0N/A * during a scrub, but of course it can so we must account for changes
0N/A * in a block's replication class.
0N/A *
0N/A * Here's an example of what can occur:
0N/A *
0N/A * If a block has refcnt > 1 during the DDT scrub phase, but has refcnt == 1
0N/A * when visited during the top-down scrub phase, it will be scrubbed twice.
0N/A * This negates our scrub optimization, but is otherwise harmless.
0N/A *
0N/A * If a block has refcnt == 1 during the DDT scrub phase, but has refcnt > 1
0N/A * on each visit during the top-down scrub phase, it will never be scrubbed.
0N/A * To catch this, ddt_sync_entry() notifies the scrub code whenever a block's
0N/A * reference class transitions to a higher level (i.e DDT_CLASS_UNIQUE to
0N/A * DDT_CLASS_DUPLICATE); if it transitions from refcnt == 1 to refcnt > 1
0N/A * while a scrub is in progress, it scrubs the block right then.
0N/A */
0N/Astatic void
0N/Adsl_scan_ddt(dsl_scan_t *scn, dmu_tx_t *tx)
0N/A{
0N/A ddt_bookmark_t *ddb = &scn->scn_phys.scn_ddt_bookmark;
0N/A ddt_entry_t dde = { 0 };
0N/A int error;
0N/A uint64_t n = 0;
0N/A
0N/A while ((error = ddt_walk(scn->scn_dp->dp_spa, ddb, &dde)) == 0) {
0N/A ddt_t *ddt;
0N/A
0N/A if (ddb->ddb_class > scn->scn_phys.scn_ddt_class_max)
0N/A break;
0N/A dprintf("visiting ddb=%llu/%llu/%llu/%llx\n",
0N/A (longlong_t)ddb->ddb_class,
0N/A (longlong_t)ddb->ddb_type,
0N/A (longlong_t)ddb->ddb_checksum,
0N/A (longlong_t)ddb->ddb_cursor);
0N/A
0N/A /* There should be no pending changes to the dedup table */
0N/A ddt = scn->scn_dp->dp_spa->spa_ddt[ddb->ddb_checksum];
0N/A ASSERT(avl_first(&ddt->ddt_tree) == NULL);
0N/A
0N/A dsl_scan_ddt_entry(scn, ddb->ddb_checksum, &dde, tx);
0N/A n++;
0N/A
0N/A if (dsl_scan_check_pause(scn, NULL))
0N/A break;
0N/A }
0N/A
0N/A zfs_dbgmsg("scanned %llu ddt entries with class_max = %u; pausing=%u",
0N/A (longlong_t)n, (int)scn->scn_phys.scn_ddt_class_max,
0N/A (int)scn->scn_pausing);
0N/A
0N/A ASSERT(error == 0 || error == ENOENT);
0N/A ASSERT(error != ENOENT ||
0N/A ddb->ddb_class > scn->scn_phys.scn_ddt_class_max);
0N/A}
0N/A
0N/A/* ARGSUSED */
0N/Avoid
0N/Adsl_scan_ddt_entry(dsl_scan_t *scn, enum zio_checksum checksum,
0N/A ddt_entry_t *dde, dmu_tx_t *tx)
0N/A{
0N/A const ddt_key_t *ddk = &dde->dde_key;
0N/A ddt_phys_t *ddp = dde->dde_phys;
0N/A blkptr_t bp;
0N/A zbookmark_phys_t zb = { 0 };
0N/A
0N/A if (scn->scn_phys.scn_state != DSS_SCANNING)
0N/A return;
0N/A
0N/A for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
0N/A if (ddp->ddp_phys_birth == 0 ||
0N/A ddp->ddp_phys_birth > scn->scn_phys.scn_max_txg)
0N/A continue;
0N/A ddt_bp_create(checksum, ddk, ddp, &bp);
0N/A
0N/A scn->scn_visited_this_txg++;
342N/A scan_funcs[scn->scn_phys.scn_func](scn->scn_dp, &bp, &zb);
342N/A }
1193N/A}
342N/A
0N/Astatic void
0N/Adsl_scan_visit(dsl_scan_t *scn, dmu_tx_t *tx)
0N/A{
0N/A dsl_pool_t *dp = scn->scn_dp;
0N/A zap_cursor_t zc;
0N/A zap_attribute_t za;
0N/A
0N/A if (scn->scn_phys.scn_ddt_bookmark.ddb_class <=
0N/A scn->scn_phys.scn_ddt_class_max) {
0N/A scn->scn_phys.scn_cur_min_txg = scn->scn_phys.scn_min_txg;
0N/A scn->scn_phys.scn_cur_max_txg = scn->scn_phys.scn_max_txg;
0N/A dsl_scan_ddt(scn, tx);
0N/A if (scn->scn_pausing)
0N/A return;
0N/A }
0N/A
0N/A if (scn->scn_phys.scn_bookmark.zb_objset == DMU_META_OBJSET) {
0N/A /* First do the MOS & ORIGIN */
0N/A
0N/A scn->scn_phys.scn_cur_min_txg = scn->scn_phys.scn_min_txg;
0N/A scn->scn_phys.scn_cur_max_txg = scn->scn_phys.scn_max_txg;
0N/A dsl_scan_visit_rootbp(scn, NULL,
0N/A &dp->dp_meta_rootbp, tx);
0N/A spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
0N/A if (scn->scn_pausing)
0N/A return;
0N/A
0N/A if (spa_version(dp->dp_spa) < SPA_VERSION_DSL_SCRUB) {
0N/A VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
0N/A enqueue_cb, tx, DS_FIND_CHILDREN));
0N/A } else {
0N/A dsl_scan_visitds(scn,
0N/A dp->dp_origin_snap->ds_object, tx);
0N/A }
0N/A ASSERT(!scn->scn_pausing);
0N/A } else if (scn->scn_phys.scn_bookmark.zb_objset !=
0N/A ZB_DESTROYED_OBJSET) {
0N/A /*
0N/A * If we were paused, continue from here. Note if the
483N/A * ds we were paused on was deleted, the zb_objset may
483N/A * be -1, so we will skip this and find a new objset
483N/A * below.
483N/A */
483N/A dsl_scan_visitds(scn, scn->scn_phys.scn_bookmark.zb_objset, tx);
483N/A if (scn->scn_pausing)
0N/A return;
375N/A }
375N/A
0N/A /*
1284N/A * In case we were paused right at the end of the ds, zero the
1284N/A * bookmark so we don't think that we're still trying to resume.
342N/A */
0N/A bzero(&scn->scn_phys.scn_bookmark, sizeof (zbookmark_phys_t));
0N/A
0N/A /* keep pulling things out of the zap-object-as-queue */
0N/A while (zap_cursor_init(&zc, dp->dp_meta_objset,
0N/A scn->scn_phys.scn_queue_obj),
0N/A zap_cursor_retrieve(&zc, &za) == 0) {
0N/A dsl_dataset_t *ds;
0N/A uint64_t dsobj;
0N/A
0N/A dsobj = strtonum(za.za_name, NULL);
0N/A VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
0N/A scn->scn_phys.scn_queue_obj, dsobj, tx));
0N/A
1064N/A /* Set up min/max txg */
1064N/A VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
0N/A if (za.za_first_integer != 0) {
0N/A scn->scn_phys.scn_cur_min_txg =
1064N/A MAX(scn->scn_phys.scn_min_txg,
1064N/A za.za_first_integer);
0N/A } else {
0N/A scn->scn_phys.scn_cur_min_txg =
0N/A MAX(scn->scn_phys.scn_min_txg,
0N/A ds->ds_phys->ds_prev_snap_txg);
0N/A }
0N/A scn->scn_phys.scn_cur_max_txg = dsl_scan_ds_maxtxg(ds);
0N/A dsl_dataset_rele(ds, FTAG);
0N/A
0N/A dsl_scan_visitds(scn, dsobj, tx);
0N/A zap_cursor_fini(&zc);
0N/A if (scn->scn_pausing)
0N/A return;
1064N/A }
1064N/A zap_cursor_fini(&zc);
1064N/A}
0N/A
1440N/Astatic boolean_t
1440N/Adsl_scan_free_should_pause(dsl_scan_t *scn)
1440N/A{
1440N/A uint64_t elapsed_nanosecs;
0N/A
0N/A if (zfs_recover)
0N/A return (B_FALSE);
0N/A
0N/A if (scn->scn_visited_this_txg >= zfs_free_max_blocks)
0N/A return (B_TRUE);
0N/A
0N/A elapsed_nanosecs = gethrtime() - scn->scn_sync_start_time;
0N/A return (elapsed_nanosecs / NANOSEC > zfs_txg_timeout ||
0N/A (NSEC2MSEC(elapsed_nanosecs) > zfs_free_min_time_ms &&
0N/A txg_sync_waiting(scn->scn_dp)) ||
0N/A spa_shutting_down(scn->scn_dp->dp_spa));
0N/A}
0N/A
0N/Astatic int
0N/Adsl_scan_free_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
0N/A{
0N/A dsl_scan_t *scn = arg;
0N/A
0N/A if (!scn->scn_is_bptree ||
0N/A (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_OBJSET)) {
0N/A if (dsl_scan_free_should_pause(scn))
0N/A return (SET_ERROR(ERESTART));
0N/A }
0N/A
0N/A zio_nowait(zio_free_sync(scn->scn_zio_root, scn->scn_dp->dp_spa,
0N/A dmu_tx_get_txg(tx), bp, 0));
0N/A dsl_dir_diduse_space(tx->tx_pool->dp_free_dir, DD_USED_HEAD,
0N/A -bp_get_dsize_sync(scn->scn_dp->dp_spa, bp),
0N/A -BP_GET_PSIZE(bp), -BP_GET_UCSIZE(bp), tx);
0N/A scn->scn_visited_this_txg++;
0N/A return (0);
0N/A}
0N/A
0N/Aboolean_t
0N/Adsl_scan_active(dsl_scan_t *scn)
0N/A{
0N/A spa_t *spa = scn->scn_dp->dp_spa;
0N/A uint64_t used = 0, comp, uncomp;
0N/A
0N/A if (spa->spa_load_state != SPA_LOAD_NONE)
0N/A return (B_FALSE);
0N/A if (spa_shutting_down(spa))
695N/A return (B_FALSE);
695N/A if (scn->scn_phys.scn_state == DSS_SCANNING ||
695N/A (scn->scn_async_destroying && !scn->scn_async_stalled))
679N/A return (B_TRUE);
695N/A
679N/A if (spa_version(scn->scn_dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
534N/A (void) bpobj_space(&scn->scn_dp->dp_free_bpobj,
534N/A &used, &comp, &uncomp);
534N/A }
534N/A return (used != 0);
1064N/A}
1064N/A
534N/Avoid
534N/Adsl_scan_sync(dsl_pool_t *dp, dmu_tx_t *tx)
0N/A{
0N/A dsl_scan_t *scn = dp->dp_scan;
1145N/A spa_t *spa = dp->dp_spa;
0N/A int err = 0;
0N/A
0N/A /*
1145N/A * Check for scn_restart_txg before checking spa_load_state, so
1145N/A * that we can restart an old-style scan while the pool is being
1145N/A * imported (see dsl_scan_init).
1145N/A */
1145N/A if (scn->scn_restart_txg != 0 &&
1145N/A scn->scn_restart_txg <= tx->tx_txg) {
1145N/A pool_scan_func_t func = POOL_SCAN_SCRUB;
1145N/A dsl_scan_done(scn, B_FALSE, tx);
1145N/A if (vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL))
1145N/A func = POOL_SCAN_RESILVER;
1145N/A zfs_dbgmsg("restarting scan func=%u txg=%llu",
1145N/A func, tx->tx_txg);
1145N/A dsl_scan_setup_sync(&func, tx);
1145N/A }
1145N/A
1145N/A /*
1145N/A * If the scan is inactive due to a stalled async destroy, try again.
1145N/A */
1145N/A if ((!scn->scn_async_stalled && !dsl_scan_active(scn)) ||
1145N/A spa_sync_pass(dp->dp_spa) > 1)
1145N/A return;
1145N/A
1145N/A scn->scn_visited_this_txg = 0;
1145N/A scn->scn_pausing = B_FALSE;
1145N/A scn->scn_sync_start_time = gethrtime();
1145N/A spa->spa_scrub_active = B_TRUE;
1145N/A
1145N/A /*
1145N/A * First process the async destroys. If we pause, don't do
1145N/A * any scrubbing or resilvering. This ensures that there are no
1145N/A * async destroys while we are scanning, so the scan code doesn't
1145N/A * have to worry about traversing it. It is also faster to free the
1145N/A * blocks than to scrub them.
1145N/A */
1145N/A if (spa_version(dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
1145N/A scn->scn_is_bptree = B_FALSE;
0N/A scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
0N/A NULL, ZIO_FLAG_MUSTSUCCEED);
0N/A err = bpobj_iterate(&dp->dp_free_bpobj,
0N/A dsl_scan_free_block_cb, scn, tx);
1064N/A VERIFY3U(0, ==, zio_wait(scn->scn_zio_root));
1064N/A
0N/A if (err != 0 && err != ERESTART)
0N/A zfs_panic_recover("error %u from bpobj_iterate()", err);
0N/A }
1064N/A
0N/A if (err == 0 && spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) {
342N/A ASSERT(scn->scn_async_destroying);
342N/A scn->scn_is_bptree = B_TRUE;
342N/A scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
342N/A NULL, ZIO_FLAG_MUSTSUCCEED);
342N/A err = bptree_iterate(dp->dp_meta_objset,
342N/A dp->dp_bptree_obj, B_TRUE, dsl_scan_free_block_cb, scn, tx);
0N/A VERIFY0(zio_wait(scn->scn_zio_root));
0N/A
0N/A if (err == EIO || err == ECKSUM) {
0N/A err = 0;
0N/A } else if (err != 0 && err != ERESTART) {
0N/A zfs_panic_recover("error %u from "
0N/A "traverse_dataset_destroyed()", err);
0N/A }
0N/A
0N/A /*
0N/A * If we didn't make progress, mark the async destroy as
0N/A * stalled, so that we will not initiate a spa_sync() on
0N/A * its behalf.
1064N/A */
1064N/A scn->scn_async_stalled = (scn->scn_visited_this_txg == 0);
0N/A
0N/A if (bptree_is_empty(dp->dp_meta_objset, dp->dp_bptree_obj)) {
1064N/A /* finished; deactivate async destroy feature */
1064N/A spa_feature_decr(spa, SPA_FEATURE_ASYNC_DESTROY, tx);
0N/A ASSERT(!spa_feature_is_active(spa,
0N/A SPA_FEATURE_ASYNC_DESTROY));
0N/A VERIFY0(zap_remove(dp->dp_meta_objset,
1064N/A DMU_POOL_DIRECTORY_OBJECT,
0N/A DMU_POOL_BPTREE_OBJ, tx));
1145N/A VERIFY0(bptree_free(dp->dp_meta_objset,
1145N/A dp->dp_bptree_obj, tx));
1145N/A dp->dp_bptree_obj = 0;
1145N/A scn->scn_async_destroying = B_FALSE;
1145N/A }
1145N/A }
1145N/A if (scn->scn_visited_this_txg) {
1145N/A zfs_dbgmsg("freed %llu blocks in %llums from "
1145N/A "free_bpobj/bptree txg %llu; err=%u",
1145N/A (longlong_t)scn->scn_visited_this_txg,
0N/A (longlong_t)
0N/A NSEC2MSEC(gethrtime() - scn->scn_sync_start_time),
0N/A (longlong_t)tx->tx_txg, err);
0N/A scn->scn_visited_this_txg = 0;
0N/A
1145N/A /*
1145N/A * Write out changes to the DDT that may be required as a
1145N/A * result of the blocks freed. This ensures that the DDT
1145N/A * is clean when a scrub/resilver runs.
1145N/A */
1145N/A ddt_sync(spa, tx->tx_txg);
1145N/A }
1145N/A if (err != 0)
1145N/A return;
1145N/A if (!scn->scn_async_destroying && zfs_free_leak_on_eio &&
1145N/A (dp->dp_free_dir->dd_phys->dd_used_bytes != 0 ||
1145N/A dp->dp_free_dir->dd_phys->dd_compressed_bytes != 0 ||
1145N/A dp->dp_free_dir->dd_phys->dd_uncompressed_bytes != 0)) {
1145N/A /*
1145N/A * We have finished background destroying, but there is still
1145N/A * some space left in the dp_free_dir. Transfer this leaked
1145N/A * space to the dp_leak_dir.
1145N/A */
1145N/A if (dp->dp_leak_dir == NULL) {
1145N/A rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
1145N/A (void) dsl_dir_create_sync(dp, dp->dp_root_dir,
1145N/A LEAK_DIR_NAME, tx);
1145N/A VERIFY0(dsl_pool_open_special_dir(dp,
1145N/A LEAK_DIR_NAME, &dp->dp_leak_dir));
1145N/A rrw_exit(&dp->dp_config_rwlock, FTAG);
1145N/A }
1145N/A dsl_dir_diduse_space(dp->dp_leak_dir, DD_USED_HEAD,
1145N/A dp->dp_free_dir->dd_phys->dd_used_bytes,
1064N/A dp->dp_free_dir->dd_phys->dd_compressed_bytes,
1064N/A dp->dp_free_dir->dd_phys->dd_uncompressed_bytes, tx);
1064N/A dsl_dir_diduse_space(dp->dp_free_dir, DD_USED_HEAD,
0N/A -dp->dp_free_dir->dd_phys->dd_used_bytes,
1145N/A -dp->dp_free_dir->dd_phys->dd_compressed_bytes,
1064N/A -dp->dp_free_dir->dd_phys->dd_uncompressed_bytes, tx);
0N/A }
0N/A if (!scn->scn_async_destroying) {
0N/A /* finished; verify that space accounting went to zero */
0N/A ASSERT0(dp->dp_free_dir->dd_phys->dd_used_bytes);
1064N/A ASSERT0(dp->dp_free_dir->dd_phys->dd_compressed_bytes);
0N/A ASSERT0(dp->dp_free_dir->dd_phys->dd_uncompressed_bytes);
0N/A }
0N/A
0N/A if (scn->scn_phys.scn_state != DSS_SCANNING)
0N/A return;
0N/A
0N/A if (scn->scn_done_txg == tx->tx_txg) {
0N/A ASSERT(!scn->scn_pausing);
0N/A /* finished with scan. */
0N/A zfs_dbgmsg("txg %llu scan complete", tx->tx_txg);
0N/A dsl_scan_done(scn, B_TRUE, tx);
0N/A ASSERT3U(spa->spa_scrub_inflight, ==, 0);
0N/A dsl_scan_sync_state(scn, tx);
94N/A return;
94N/A }
94N/A
94N/A if (scn->scn_phys.scn_ddt_bookmark.ddb_class <=
0N/A scn->scn_phys.scn_ddt_class_max) {
0N/A zfs_dbgmsg("doing scan sync txg %llu; "
0N/A "ddt bm=%llu/%llu/%llu/%llx",
0N/A (longlong_t)tx->tx_txg,
0N/A (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_class,
0N/A (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_type,
0N/A (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_checksum,
0N/A (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_cursor);
0N/A ASSERT(scn->scn_phys.scn_bookmark.zb_objset == 0);
0N/A ASSERT(scn->scn_phys.scn_bookmark.zb_object == 0);
0N/A ASSERT(scn->scn_phys.scn_bookmark.zb_level == 0);
0N/A ASSERT(scn->scn_phys.scn_bookmark.zb_blkid == 0);
0N/A } else {
0N/A zfs_dbgmsg("doing scan sync txg %llu; bm=%llu/%llu/%llu/%llu",
0N/A (longlong_t)tx->tx_txg,
1145N/A (longlong_t)scn->scn_phys.scn_bookmark.zb_objset,
1145N/A (longlong_t)scn->scn_phys.scn_bookmark.zb_object,
1145N/A (longlong_t)scn->scn_phys.scn_bookmark.zb_level,
1145N/A (longlong_t)scn->scn_phys.scn_bookmark.zb_blkid);
1145N/A }
1145N/A
1145N/A scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
0N/A NULL, ZIO_FLAG_CANFAIL);
0N/A dsl_pool_config_enter(dp, FTAG);
0N/A dsl_scan_visit(scn, tx);
0N/A dsl_pool_config_exit(dp, FTAG);
0N/A (void) zio_wait(scn->scn_zio_root);
0N/A scn->scn_zio_root = NULL;
1284N/A
1284N/A zfs_dbgmsg("visited %llu blocks in %llums",
1284N/A (longlong_t)scn->scn_visited_this_txg,
1284N/A (longlong_t)NSEC2MSEC(gethrtime() - scn->scn_sync_start_time));
1284N/A
0N/A if (!scn->scn_pausing) {
0N/A scn->scn_done_txg = tx->tx_txg + 1;
0N/A zfs_dbgmsg("txg %llu traversal complete, waiting till txg %llu",
0N/A tx->tx_txg, scn->scn_done_txg);
0N/A }
534N/A
534N/A if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
0N/A mutex_enter(&spa->spa_scrub_lock);
0N/A while (spa->spa_scrub_inflight > 0) {
0N/A cv_wait(&spa->spa_scrub_io_cv,
0N/A &spa->spa_scrub_lock);
0N/A }
0N/A mutex_exit(&spa->spa_scrub_lock);
0N/A }
0N/A
0N/A dsl_scan_sync_state(scn, tx);
0N/A}
0N/A
0N/A/*
0N/A * This will start a new scan, or restart an existing one.
0N/A */
0N/Avoid
0N/Adsl_resilver_restart(dsl_pool_t *dp, uint64_t txg)
0N/A{
0N/A if (txg == 0) {
0N/A dmu_tx_t *tx;
0N/A tx = dmu_tx_create_dd(dp->dp_mos_dir);
0N/A VERIFY(0 == dmu_tx_assign(tx, TXG_WAIT));
0N/A
0N/A txg = dmu_tx_get_txg(tx);
0N/A dp->dp_scan->scn_restart_txg = txg;
0N/A dmu_tx_commit(tx);
0N/A } else {
0N/A dp->dp_scan->scn_restart_txg = txg;
0N/A }
0N/A zfs_dbgmsg("restarting resilver txg=%llu", txg);
0N/A}
0N/A
0N/Aboolean_t
0N/Adsl_scan_resilvering(dsl_pool_t *dp)
0N/A{
0N/A return (dp->dp_scan->scn_phys.scn_state == DSS_SCANNING &&
0N/A dp->dp_scan->scn_phys.scn_func == POOL_SCAN_RESILVER);
0N/A}
0N/A
0N/A/*
0N/A * scrub consumers
0N/A */
0N/A
0N/Astatic void
0N/Acount_block(zfs_all_blkstats_t *zab, const blkptr_t *bp)
0N/A{
0N/A int i;
0N/A
0N/A /*
0N/A * If we resume after a reboot, zab will be NULL; don't record
0N/A * incomplete stats in that case.
0N/A */
0N/A if (zab == NULL)
0N/A return;
0N/A
0N/A for (i = 0; i < 4; i++) {
0N/A int l = (i < 2) ? BP_GET_LEVEL(bp) : DN_MAX_LEVELS;
0N/A int t = (i & 1) ? BP_GET_TYPE(bp) : DMU_OT_TOTAL;
0N/A if (t & DMU_OT_NEWTYPE)
0N/A t = DMU_OT_OTHER;
0N/A zfs_blkstat_t *zb = &zab->zab_type[l][t];
0N/A int equal;
0N/A
0N/A zb->zb_count++;
452N/A zb->zb_asize += BP_GET_ASIZE(bp);
0N/A zb->zb_lsize += BP_GET_LSIZE(bp);
0N/A zb->zb_psize += BP_GET_PSIZE(bp);
0N/A zb->zb_gangs += BP_COUNT_GANG(bp);
0N/A
0N/A switch (BP_GET_NDVAS(bp)) {
0N/A case 2:
0N/A if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
0N/A DVA_GET_VDEV(&bp->blk_dva[1]))
0N/A zb->zb_ditto_2_of_2_samevdev++;
0N/A break;
0N/A case 3:
0N/A equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
0N/A DVA_GET_VDEV(&bp->blk_dva[1])) +
0N/A (DVA_GET_VDEV(&bp->blk_dva[0]) ==
0N/A DVA_GET_VDEV(&bp->blk_dva[2])) +
0N/A (DVA_GET_VDEV(&bp->blk_dva[1]) ==
0N/A DVA_GET_VDEV(&bp->blk_dva[2]));
0N/A if (equal == 1)
0N/A zb->zb_ditto_2_of_3_samevdev++;
0N/A else if (equal == 3)
0N/A zb->zb_ditto_3_of_3_samevdev++;
0N/A break;
0N/A }
0N/A }
0N/A}
0N/A
0N/Astatic void
0N/Adsl_scan_scrub_done(zio_t *zio)
0N/A{
0N/A spa_t *spa = zio->io_spa;
0N/A
0N/A zio_data_buf_free(zio->io_data, zio->io_size);
0N/A
0N/A mutex_enter(&spa->spa_scrub_lock);
0N/A spa->spa_scrub_inflight--;
0N/A cv_broadcast(&spa->spa_scrub_io_cv);
0N/A
0N/A if (zio->io_error && (zio->io_error != ECKSUM ||
0N/A !(zio->io_flags & ZIO_FLAG_SPECULATIVE))) {
0N/A spa->spa_dsl_pool->dp_scan->scn_phys.scn_errors++;
0N/A }
0N/A mutex_exit(&spa->spa_scrub_lock);
0N/A}
0N/A
0N/Astatic int
0N/Adsl_scan_scrub_cb(dsl_pool_t *dp,
0N/A const blkptr_t *bp, const zbookmark_phys_t *zb)
0N/A{
0N/A dsl_scan_t *scn = dp->dp_scan;
0N/A size_t size = BP_GET_PSIZE(bp);
0N/A spa_t *spa = dp->dp_spa;
0N/A uint64_t phys_birth = BP_PHYSICAL_BIRTH(bp);
1145N/A boolean_t needs_io;
1145N/A int zio_flags = ZIO_FLAG_SCAN_THREAD | ZIO_FLAG_RAW | ZIO_FLAG_CANFAIL;
1145N/A int scan_delay = 0;
1145N/A
1145N/A if (phys_birth <= scn->scn_phys.scn_min_txg ||
1145N/A phys_birth >= scn->scn_phys.scn_max_txg)
1145N/A return (0);
1145N/A
1145N/A count_block(dp->dp_blkstats, bp);
1145N/A
1145N/A if (BP_IS_EMBEDDED(bp))
1145N/A return (0);
0N/A
0N/A ASSERT(DSL_SCAN_IS_SCRUB_RESILVER(scn));
0N/A if (scn->scn_phys.scn_func == POOL_SCAN_SCRUB) {
0N/A zio_flags |= ZIO_FLAG_SCRUB;
0N/A needs_io = B_TRUE;
0N/A scan_delay = zfs_scrub_delay;
0N/A } else {
0N/A ASSERT3U(scn->scn_phys.scn_func, ==, POOL_SCAN_RESILVER);
0N/A zio_flags |= ZIO_FLAG_RESILVER;
0N/A needs_io = B_FALSE;
0N/A scan_delay = zfs_resilver_delay;
0N/A }
0N/A
0N/A /* If it's an intent log block, failure is expected. */
0N/A if (zb->zb_level == ZB_ZIL_LEVEL)
0N/A zio_flags |= ZIO_FLAG_SPECULATIVE;
0N/A
0N/A for (int d = 0; d < BP_GET_NDVAS(bp); d++) {
0N/A vdev_t *vd = vdev_lookup_top(spa,
0N/A DVA_GET_VDEV(&bp->blk_dva[d]));
0N/A
0N/A /*
0N/A * Keep track of how much data we've examined so that
0N/A * zpool(1M) status can make useful progress reports.
0N/A */
0N/A scn->scn_phys.scn_examined += DVA_GET_ASIZE(&bp->blk_dva[d]);
0N/A spa->spa_scan_pass_exam += DVA_GET_ASIZE(&bp->blk_dva[d]);
0N/A
0N/A /* if it's a resilver, this may not be in the target range */
0N/A if (!needs_io) {
0N/A if (DVA_GET_GANG(&bp->blk_dva[d])) {
0N/A /*
0N/A * Gang members may be spread across multiple
0N/A * vdevs, so the best estimate we have is the
1064N/A * scrub range, which has already been checked.
0N/A * XXX -- it would be better to change our
94N/A * allocation policy to ensure that all
1064N/A * gang members reside on the same vdev.
1064N/A */
1064N/A needs_io = B_TRUE;
94N/A } else {
94N/A needs_io = vdev_dtl_contains(vd, DTL_PARTIAL,
0N/A phys_birth, 1);
1064N/A }
0N/A }
0N/A }
0N/A
1064N/A if (needs_io && !zfs_no_scrub_io) {
94N/A vdev_t *rvd = spa->spa_root_vdev;
1283N/A uint64_t maxinflight = rvd->vdev_children * zfs_top_maxinflight;
1283N/A void *data = zio_data_buf_alloc(size);
1283N/A
1283N/A mutex_enter(&spa->spa_scrub_lock);
1283N/A while (spa->spa_scrub_inflight >= maxinflight)
1283N/A cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
1283N/A spa->spa_scrub_inflight++;
94N/A mutex_exit(&spa->spa_scrub_lock);
1064N/A
1064N/A /*
1064N/A * If we're seeing recent (zfs_scan_idle) "important" I/Os
0N/A * then throttle our workload to limit the impact of a scan.
0N/A */
0N/A if (ddi_get_lbolt64() - spa->spa_last_io <= zfs_scan_idle)
0N/A delay(scan_delay);
94N/A
1064N/A zio_nowait(zio_read(NULL, spa, bp, data, size,
1064N/A dsl_scan_scrub_done, NULL, ZIO_PRIORITY_SCRUB,
94N/A zio_flags, zb));
0N/A }
0N/A
1064N/A /* do not relocate this block */
0N/A return (0);
0N/A}
0N/A
0N/Aint
0N/Adsl_scan(dsl_pool_t *dp, pool_scan_func_t func)
0N/A{
0N/A spa_t *spa = dp->dp_spa;
0N/A
0N/A /*
0N/A * Purge all vdev caches and probe all devices. We do this here
0N/A * rather than in sync context because this requires a writer lock
0N/A * on the spa_config lock, which we can't do from sync context. The
0N/A * spa_scrub_reopen flag indicates that vdev_open() should not
0N/A * attempt to start another scrub.
0N/A */
0N/A spa_vdev_state_enter(spa, SCL_NONE);
0N/A spa->spa_scrub_reopen = B_TRUE;
1064N/A vdev_reopen(spa->spa_root_vdev);
1064N/A spa->spa_scrub_reopen = B_FALSE;
0N/A (void) spa_vdev_state_exit(spa, NULL, 0);
1145N/A
1145N/A return (dsl_sync_task(spa_name(spa), dsl_scan_setup_check,
1145N/A dsl_scan_setup_sync, &func, 0, ZFS_SPACE_CHECK_NONE));
1145N/A}
0N/A