53089ab7c84db6fb76c16ca50076c147cda11757eschrock/*
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * CDDL HEADER START
53089ab7c84db6fb76c16ca50076c147cda11757eschrock *
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * The contents of this file are subject to the terms of the
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * Common Development and Distribution License (the "License").
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * You may not use this file except in compliance with the License.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock *
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * or http://www.opensolaris.org/os/licensing.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * See the License for the specific language governing permissions
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * and limitations under the License.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock *
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * When distributing Covered Code, include this CDDL HEADER in each
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * If applicable, add the following below this CDDL HEADER, with the
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * fields enclosed by brackets "[]" replaced with your own identifying
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * information: Portions Copyright [yyyy] [name of copyright owner]
53089ab7c84db6fb76c16ca50076c147cda11757eschrock *
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * CDDL HEADER END
53089ab7c84db6fb76c16ca50076c147cda11757eschrock */
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock/*
10e67aa0db0823d5464aafdd681f3c966155c68eMatthew Ahrens * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
c3d26abc9ee97b4f60233556aadeb57e0bd30bb9Matthew Ahrens * Copyright (c) 2014 Integros [integros.com]
53089ab7c84db6fb76c16ca50076c147cda11757eschrock */
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock#include <sys/arc.h>
53089ab7c84db6fb76c16ca50076c147cda11757eschrock#include <sys/bptree.h>
53089ab7c84db6fb76c16ca50076c147cda11757eschrock#include <sys/dmu.h>
53089ab7c84db6fb76c16ca50076c147cda11757eschrock#include <sys/dmu_objset.h>
53089ab7c84db6fb76c16ca50076c147cda11757eschrock#include <sys/dmu_tx.h>
53089ab7c84db6fb76c16ca50076c147cda11757eschrock#include <sys/dmu_traverse.h>
53089ab7c84db6fb76c16ca50076c147cda11757eschrock#include <sys/dsl_dataset.h>
53089ab7c84db6fb76c16ca50076c147cda11757eschrock#include <sys/dsl_dir.h>
53089ab7c84db6fb76c16ca50076c147cda11757eschrock#include <sys/dsl_pool.h>
53089ab7c84db6fb76c16ca50076c147cda11757eschrock#include <sys/dnode.h>
53089ab7c84db6fb76c16ca50076c147cda11757eschrock#include <sys/refcount.h>
53089ab7c84db6fb76c16ca50076c147cda11757eschrock#include <sys/spa.h>
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock/*
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * A bptree is a queue of root block pointers from destroyed datasets. When a
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * dataset is destroyed its root block pointer is put on the end of the pool's
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * bptree queue so the dataset's blocks can be freed asynchronously by
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * dsl_scan_sync. This allows the delete operation to finish without traversing
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * all the dataset's blocks.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock *
f7170741490edba9d1d9c697c177c887172bc741Will Andrews * Note that while bt_begin and bt_end are only ever incremented in this code,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * they are effectively reset to 0 every time the entire bptree is freed because
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * the bptree's object is destroyed and re-created.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock */
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrockstruct bptree_args {
53089ab7c84db6fb76c16ca50076c147cda11757eschrock bptree_phys_t *ba_phys; /* data in bonus buffer, dirtied if freeing */
53089ab7c84db6fb76c16ca50076c147cda11757eschrock boolean_t ba_free; /* true if freeing during traversal */
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock bptree_itor_t *ba_func; /* function to call for each blockpointer */
53089ab7c84db6fb76c16ca50076c147cda11757eschrock void *ba_arg; /* caller supplied argument to ba_func */
53089ab7c84db6fb76c16ca50076c147cda11757eschrock dmu_tx_t *ba_tx; /* caller supplied tx, NULL if not freeing */
53089ab7c84db6fb76c16ca50076c147cda11757eschrock} bptree_args_t;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrockuint64_t
53089ab7c84db6fb76c16ca50076c147cda11757eschrockbptree_alloc(objset_t *os, dmu_tx_t *tx)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock{
53089ab7c84db6fb76c16ca50076c147cda11757eschrock uint64_t obj;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock dmu_buf_t *db;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock bptree_phys_t *bt;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock obj = dmu_object_alloc(os, DMU_OTN_UINT64_METADATA,
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens SPA_OLD_MAXBLOCKSIZE, DMU_OTN_UINT64_METADATA,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock sizeof (bptree_phys_t), tx);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock /*
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * Bonus buffer contents are already initialized to 0, but for
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * readability we make it explicit.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock */
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
53089ab7c84db6fb76c16ca50076c147cda11757eschrock dmu_buf_will_dirty(db, tx);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock bt = db->db_data;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock bt->bt_begin = 0;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock bt->bt_end = 0;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock bt->bt_bytes = 0;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock bt->bt_comp = 0;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock bt->bt_uncomp = 0;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock dmu_buf_rele(db, FTAG);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock return (obj);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock}
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrockint
53089ab7c84db6fb76c16ca50076c147cda11757eschrockbptree_free(objset_t *os, uint64_t obj, dmu_tx_t *tx)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock{
53089ab7c84db6fb76c16ca50076c147cda11757eschrock dmu_buf_t *db;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock bptree_phys_t *bt;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
53089ab7c84db6fb76c16ca50076c147cda11757eschrock bt = db->db_data;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock ASSERT3U(bt->bt_begin, ==, bt->bt_end);
fb09f5aad449c97fe309678f3f604982b563a96fMadhav Suresh ASSERT0(bt->bt_bytes);
fb09f5aad449c97fe309678f3f604982b563a96fMadhav Suresh ASSERT0(bt->bt_comp);
fb09f5aad449c97fe309678f3f604982b563a96fMadhav Suresh ASSERT0(bt->bt_uncomp);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock dmu_buf_rele(db, FTAG);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock return (dmu_object_free(os, obj, tx));
53089ab7c84db6fb76c16ca50076c147cda11757eschrock}
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrensboolean_t
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrensbptree_is_empty(objset_t *os, uint64_t obj)
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens{
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens dmu_buf_t *db;
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens bptree_phys_t *bt;
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens boolean_t rv;
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens VERIFY0(dmu_bonus_hold(os, obj, FTAG, &db));
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens bt = db->db_data;
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens rv = (bt->bt_begin == bt->bt_end);
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens dmu_buf_rele(db, FTAG);
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens return (rv);
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens}
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens
53089ab7c84db6fb76c16ca50076c147cda11757eschrockvoid
53089ab7c84db6fb76c16ca50076c147cda11757eschrockbptree_add(objset_t *os, uint64_t obj, blkptr_t *bp, uint64_t birth_txg,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock uint64_t bytes, uint64_t comp, uint64_t uncomp, dmu_tx_t *tx)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock{
53089ab7c84db6fb76c16ca50076c147cda11757eschrock dmu_buf_t *db;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock bptree_phys_t *bt;
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens bptree_entry_phys_t bte = { 0 };
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock /*
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * bptree objects are in the pool mos, therefore they can only be
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * modified in syncing context. Furthermore, this is only modified
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * by the sync thread, so no locking is necessary.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock */
53089ab7c84db6fb76c16ca50076c147cda11757eschrock ASSERT(dmu_tx_is_syncing(tx));
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
53089ab7c84db6fb76c16ca50076c147cda11757eschrock bt = db->db_data;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock bte.be_birth_txg = birth_txg;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock bte.be_bp = *bp;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock dmu_write(os, obj, bt->bt_end * sizeof (bte), sizeof (bte), &bte, tx);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock dmu_buf_will_dirty(db, tx);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock bt->bt_end++;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock bt->bt_bytes += bytes;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock bt->bt_comp += comp;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock bt->bt_uncomp += uncomp;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock dmu_buf_rele(db, FTAG);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock}
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock/* ARGSUSED */
53089ab7c84db6fb76c16ca50076c147cda11757eschrockstatic int
1b912ec7100c10e7243bf0879af0fe580e08c73dGeorge Wilsonbptree_visit_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
7802d7bf98dec568dadf72286893b1fe5abd8602Matthew Ahrens const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock{
53089ab7c84db6fb76c16ca50076c147cda11757eschrock int err;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock struct bptree_args *ba = arg;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (bp == NULL || BP_IS_HOLE(bp))
53089ab7c84db6fb76c16ca50076c147cda11757eschrock return (0);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock err = ba->ba_func(ba->ba_arg, bp, ba->ba_tx);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock if (err == 0 && ba->ba_free) {
53089ab7c84db6fb76c16ca50076c147cda11757eschrock ba->ba_phys->bt_bytes -= bp_get_dsize_sync(spa, bp);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock ba->ba_phys->bt_comp -= BP_GET_PSIZE(bp);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock ba->ba_phys->bt_uncomp -= BP_GET_UCSIZE(bp);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock }
53089ab7c84db6fb76c16ca50076c147cda11757eschrock return (err);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock}
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens/*
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens * If "free" is set:
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens * - It is assumed that "func" will be freeing the block pointers.
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens * - If "func" returns nonzero, the bookmark will be remembered and
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens * iteration will be restarted from this point on next invocation.
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens * - If an i/o error is encountered (e.g. "func" returns EIO or ECKSUM),
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens * bptree_iterate will remember the bookmark, continue traversing
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens * any additional entries, and return 0.
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens *
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens * If "free" is not set, traversal will stop and return an error if
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens * an i/o error is encountered.
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens *
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens * In either case, if zfs_free_leak_on_eio is set, i/o errors will be
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens * ignored and traversal will continue (i.e. TRAVERSE_HARD will be passed to
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens * traverse_dataset_destroyed()).
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens */
53089ab7c84db6fb76c16ca50076c147cda11757eschrockint
53089ab7c84db6fb76c16ca50076c147cda11757eschrockbptree_iterate(objset_t *os, uint64_t obj, boolean_t free, bptree_itor_t func,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock void *arg, dmu_tx_t *tx)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock{
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens boolean_t ioerr = B_FALSE;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock int err;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock uint64_t i;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock dmu_buf_t *db;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock struct bptree_args ba;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock ASSERT(!free || dmu_tx_is_syncing(tx));
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock err = dmu_bonus_hold(os, obj, FTAG, &db);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock if (err != 0)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock return (err);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock if (free)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock dmu_buf_will_dirty(db, tx);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock ba.ba_phys = db->db_data;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock ba.ba_free = free;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock ba.ba_func = func;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock ba.ba_arg = arg;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock ba.ba_tx = tx;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock err = 0;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock for (i = ba.ba_phys->bt_begin; i < ba.ba_phys->bt_end; i++) {
53089ab7c84db6fb76c16ca50076c147cda11757eschrock bptree_entry_phys_t bte;
8b36997aa24d9817807faa4efa301ac9c07a2b78Matthew Ahrens int flags = TRAVERSE_PREFETCH_METADATA | TRAVERSE_POST;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock err = dmu_read(os, obj, i * sizeof (bte), sizeof (bte),
53089ab7c84db6fb76c16ca50076c147cda11757eschrock &bte, DMU_READ_NO_PREFETCH);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock if (err != 0)
53089ab7c84db6fb76c16ca50076c147cda11757eschrock break;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens if (zfs_free_leak_on_eio)
8b36997aa24d9817807faa4efa301ac9c07a2b78Matthew Ahrens flags |= TRAVERSE_HARD;
10e67aa0db0823d5464aafdd681f3c966155c68eMatthew Ahrens zfs_dbgmsg("bptree index %lld: traversing from min_txg=%lld "
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens "bookmark %lld/%lld/%lld/%lld",
10e67aa0db0823d5464aafdd681f3c966155c68eMatthew Ahrens (longlong_t)i,
10e67aa0db0823d5464aafdd681f3c966155c68eMatthew Ahrens (longlong_t)bte.be_birth_txg,
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens (longlong_t)bte.be_zb.zb_objset,
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens (longlong_t)bte.be_zb.zb_object,
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens (longlong_t)bte.be_zb.zb_level,
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens (longlong_t)bte.be_zb.zb_blkid);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock err = traverse_dataset_destroyed(os->os_spa, &bte.be_bp,
8b36997aa24d9817807faa4efa301ac9c07a2b78Matthew Ahrens bte.be_birth_txg, &bte.be_zb, flags,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock bptree_visit_cb, &ba);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock if (free) {
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens /*
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens * The callback has freed the visited block pointers.
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens * Record our traversal progress on disk, either by
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens * updating this record's bookmark, or by logically
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens * removing this record by advancing bt_begin.
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens */
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens if (err != 0) {
53089ab7c84db6fb76c16ca50076c147cda11757eschrock /* save bookmark for future resume */
53089ab7c84db6fb76c16ca50076c147cda11757eschrock ASSERT3U(bte.be_zb.zb_objset, ==,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock ZB_DESTROYED_OBJSET);
fb09f5aad449c97fe309678f3f604982b563a96fMadhav Suresh ASSERT0(bte.be_zb.zb_level);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock dmu_write(os, obj, i * sizeof (bte),
53089ab7c84db6fb76c16ca50076c147cda11757eschrock sizeof (bte), &bte, tx);
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens if (err == EIO || err == ECKSUM ||
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens err == ENXIO) {
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens /*
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens * Skip the rest of this tree and
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens * continue on to the next entry.
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens */
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens err = 0;
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens ioerr = B_TRUE;
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens } else {
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens break;
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens }
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens } else if (ioerr) {
8b36997aa24d9817807faa4efa301ac9c07a2b78Matthew Ahrens /*
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens * This entry is finished, but there were
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens * i/o errors on previous entries, so we
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens * can't adjust bt_begin. Set this entry's
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens * be_birth_txg such that it will be
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens * treated as a no-op in future traversals.
8b36997aa24d9817807faa4efa301ac9c07a2b78Matthew Ahrens */
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens bte.be_birth_txg = UINT64_MAX;
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens dmu_write(os, obj, i * sizeof (bte),
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens sizeof (bte), &bte, tx);
8b36997aa24d9817807faa4efa301ac9c07a2b78Matthew Ahrens }
8b36997aa24d9817807faa4efa301ac9c07a2b78Matthew Ahrens
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens if (!ioerr) {
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens ba.ba_phys->bt_begin++;
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens (void) dmu_free_range(os, obj,
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens i * sizeof (bte), sizeof (bte), tx);
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens }
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens } else if (err != 0) {
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens break;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock }
53089ab7c84db6fb76c16ca50076c147cda11757eschrock }
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens ASSERT(!free || err != 0 || ioerr ||
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens ba.ba_phys->bt_begin == ba.ba_phys->bt_end);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock /* if all blocks are free there should be no used space */
53089ab7c84db6fb76c16ca50076c147cda11757eschrock if (ba.ba_phys->bt_begin == ba.ba_phys->bt_end) {
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens if (zfs_free_leak_on_eio) {
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens ba.ba_phys->bt_bytes = 0;
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens ba.ba_phys->bt_comp = 0;
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens ba.ba_phys->bt_uncomp = 0;
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens }
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens
fb09f5aad449c97fe309678f3f604982b563a96fMadhav Suresh ASSERT0(ba.ba_phys->bt_bytes);
fb09f5aad449c97fe309678f3f604982b563a96fMadhav Suresh ASSERT0(ba.ba_phys->bt_comp);
fb09f5aad449c97fe309678f3f604982b563a96fMadhav Suresh ASSERT0(ba.ba_phys->bt_uncomp);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock }
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock dmu_buf_rele(db, FTAG);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock
53089ab7c84db6fb76c16ca50076c147cda11757eschrock return (err);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock}