53089ab7c84db6fb76c16ca50076c147cda11757eschrock * CDDL HEADER START
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 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * See the License for the specific language governing permissions
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * and limitations under the License.
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 * CDDL HEADER END
10e67aa0db0823d5464aafdd681f3c966155c68eMatthew Ahrens * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
c3d26abc9ee97b4f60233556aadeb57e0bd30bb9Matthew Ahrens * Copyright (c) 2014 Integros [integros.com]
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.
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 bptree_phys_t *ba_phys; /* data in bonus buffer, dirtied if freeing */
53089ab7c84db6fb76c16ca50076c147cda11757eschrock boolean_t ba_free; /* true if freeing during traversal */
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 obj = dmu_object_alloc(os, DMU_OTN_UINT64_METADATA,
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens SPA_OLD_MAXBLOCKSIZE, DMU_OTN_UINT64_METADATA,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * Bonus buffer contents are already initialized to 0, but for
53089ab7c84db6fb76c16ca50076c147cda11757eschrock * readability we make it explicit.
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
53089ab7c84db6fb76c16ca50076c147cda11757eschrockbptree_free(objset_t *os, uint64_t obj, dmu_tx_t *tx)
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
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 * 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.
b420f3adeb349714478d1a7813d2c0e069d41555Richard Lowe VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
53089ab7c84db6fb76c16ca50076c147cda11757eschrock dmu_write(os, obj, bt->bt_end * sizeof (bte), sizeof (bte), &bte, tx);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock/* ARGSUSED */
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 return (0);
53089ab7c84db6fb76c16ca50076c147cda11757eschrock ba->ba_phys->bt_bytes -= bp_get_dsize_sync(spa, bp);
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 * If "free" is not set, traversal will stop and return an error if
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens * an i/o error is encountered.
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()).
53089ab7c84db6fb76c16ca50076c147cda11757eschrockbptree_iterate(objset_t *os, uint64_t obj, boolean_t free, bptree_itor_t func,
53089ab7c84db6fb76c16ca50076c147cda11757eschrock for (i = ba.ba_phys->bt_begin; i < ba.ba_phys->bt_end; i++) {
8b36997aa24d9817807faa4efa301ac9c07a2b78Matthew Ahrens int flags = TRAVERSE_PREFETCH_METADATA | TRAVERSE_POST;
53089ab7c84db6fb76c16ca50076c147cda11757eschrock err = dmu_read(os, obj, i * sizeof (bte), sizeof (bte),
10e67aa0db0823d5464aafdd681f3c966155c68eMatthew Ahrens zfs_dbgmsg("bptree index %lld: traversing from min_txg=%lld "
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens "bookmark %lld/%lld/%lld/%lld",
53089ab7c84db6fb76c16ca50076c147cda11757eschrock err = traverse_dataset_destroyed(os->os_spa, &bte.be_bp,
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.
53089ab7c84db6fb76c16ca50076c147cda11757eschrock /* save bookmark for future resume */
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens * Skip the rest of this tree and
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens * continue on to the next entry.
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens } else if (ioerr) {
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.
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens } else if (err != 0) {
53089ab7c84db6fb76c16ca50076c147cda11757eschrock /* if all blocks are free there should be no used space */