3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens/*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * CDDL HEADER START
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * The contents of this file are subject to the terms of the
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Common Development and Distribution License (the "License").
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * You may not use this file except in compliance with the License.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * or http://www.opensolaris.org/os/licensing.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * See the License for the specific language governing permissions
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * and limitations under the License.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * When distributing Covered Code, include this CDDL HEADER in each
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * If applicable, add the following below this CDDL HEADER, with the
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * fields enclosed by brackets "[]" replaced with your own identifying
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * CDDL HEADER END
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens/*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * Copyright (c) 2013 Steven Hartland. All rights reserved.
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * Copyright (c) 2013 by Joyent, Inc. All rights reserved.
c3d26abc9ee97b4f60233556aadeb57e0bd30bb9Matthew Ahrens * Copyright (c) 2014 Integros [integros.com]
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens#include <sys/zfs_context.h>
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens#include <sys/dsl_userhold.h>
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens#include <sys/dsl_dataset.h>
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens#include <sys/dsl_synctask.h>
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens#include <sys/dmu_tx.h>
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens#include <sys/dsl_pool.h>
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens#include <sys/dsl_dir.h>
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens#include <sys/dmu_traverse.h>
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens#include <sys/dsl_scan.h>
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens#include <sys/dmu_objset.h>
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens#include <sys/zap.h>
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens#include <sys/zfeature.h>
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens#include <sys/zfs_ioctl.h>
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens#include <sys/dsl_deleg.h>
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens#include <sys/dmu_impl.h>
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrenstypedef struct dmu_snapshots_destroy_arg {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens nvlist_t *dsda_snaps;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens nvlist_t *dsda_successful_snaps;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens boolean_t dsda_defer;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens nvlist_t *dsda_errlist;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens} dmu_snapshots_destroy_arg_t;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrensint
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_destroy_snapshot_check_impl(dsl_dataset_t *ds, boolean_t defer)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs if (!ds->ds_is_snapshot)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINVAL));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (dsl_dataset_long_held(ds))
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EBUSY));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Only allow deferred destroy on pools that support it.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * NOTE: deferred destroy is only supported on snapshots.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (defer) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (spa_version(ds->ds_dir->dd_pool->dp_spa) <
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens SPA_VERSION_USERREFS)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(ENOTSUP));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * If this snapshot has an elevated user reference count,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * we can't destroy it yet.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (ds->ds_userrefs > 0)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EBUSY));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Can't delete a branch point.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_num_children > 1)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EEXIST));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstatic int
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_destroy_snapshot_check(void *arg, dmu_tx_t *tx)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_snapshots_destroy_arg_t *dsda = arg;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens nvpair_t *pair;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int error = 0;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (!dmu_tx_is_syncing(tx))
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens for (pair = nvlist_next_nvpair(dsda->dsda_snaps, NULL);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens pair != NULL; pair = nvlist_next_nvpair(dsda->dsda_snaps, pair)) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *ds;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_dataset_hold(dp, nvpair_name(pair),
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens FTAG, &ds);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * If the snapshot does not exist, silently ignore it
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * (it's "already destroyed").
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error == ENOENT)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens continue;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error == 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_destroy_snapshot_check_impl(ds,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsda->dsda_defer);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error == 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens fnvlist_add_boolean(dsda->dsda_successful_snaps,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens nvpair_name(pair));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens } else {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens fnvlist_add_int32(dsda->dsda_errlist,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens nvpair_name(pair), error);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens pair = nvlist_next_nvpair(dsda->dsda_errlist, NULL);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (pair != NULL)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (fnvpair_value_int32(pair));
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstruct process_old_arg {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *ds;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *ds_prev;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens boolean_t after_branch_point;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens zio_t *pio;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t used, comp, uncomp;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens};
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstatic int
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensprocess_old_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens struct process_old_arg *poa = arg;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = poa->ds->ds_dir->dd_pool;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman ASSERT(!BP_IS_HOLE(bp));
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (bp->blk_birth <= dsl_dataset_phys(poa->ds)->ds_prev_snap_txg) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_deadlist_insert(&poa->ds->ds_deadlist, bp, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (poa->ds_prev && !poa->after_branch_point &&
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens bp->blk_birth >
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(poa->ds_prev)->ds_prev_snap_txg) {
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(poa->ds_prev)->ds_unique_bytes +=
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens bp_get_dsize_sync(dp->dp_spa, bp);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens } else {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens poa->used += bp_get_dsize_sync(dp->dp_spa, bp);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens poa->comp += BP_GET_PSIZE(bp);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens poa->uncomp += BP_GET_UCSIZE(bp);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_free_sync(poa->pio, dp, tx->tx_txg, bp);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstatic void
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensprocess_old_deadlist(dsl_dataset_t *ds, dsl_dataset_t *ds_prev,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *ds_next, boolean_t after_branch_point, dmu_tx_t *tx)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens struct process_old_arg poa = { 0 };
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens objset_t *mos = dp->dp_meta_objset;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t deadlist_obj;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(ds->ds_deadlist.dl_oldfmt);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(ds_next->ds_deadlist.dl_oldfmt);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens poa.ds = ds;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens poa.ds_prev = ds_prev;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens poa.after_branch_point = after_branch_point;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens poa.pio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(bpobj_iterate(&ds_next->ds_deadlist.dl_bpobj,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens process_old_cb, &poa, tx));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(zio_wait(poa.pio));
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT3U(poa.used, ==, dsl_dataset_phys(ds)->ds_unique_bytes);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* change snapused */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens -poa.used, -poa.comp, -poa.uncomp, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* swap next's deadlist to our deadlist */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_deadlist_close(&ds->ds_deadlist);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_deadlist_close(&ds_next->ds_deadlist);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs deadlist_obj = dsl_dataset_phys(ds)->ds_deadlist_obj;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_deadlist_obj =
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds_next)->ds_deadlist_obj;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds_next)->ds_deadlist_obj = deadlist_obj;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_deadlist_open(&ds->ds_deadlist, mos,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_deadlist_obj);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_deadlist_open(&ds_next->ds_deadlist, mos,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds_next)->ds_deadlist_obj);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstatic void
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_remove_clones_key(dsl_dataset_t *ds, uint64_t mintxg, dmu_tx_t *tx)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens zap_cursor_t zc;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens zap_attribute_t za;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * If it is the old version, dd_clones doesn't exist so we can't
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * find the clones, but dsl_deadlist_remove_key() is a no-op so it
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * doesn't matter.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dir_phys(ds->ds_dir)->dd_clones == 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs for (zap_cursor_init(&zc, mos, dsl_dir_phys(ds->ds_dir)->dd_clones);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens zap_cursor_retrieve(&zc, &za) == 0;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens zap_cursor_advance(&zc)) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *clone;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens za.za_first_integer, FTAG, &clone));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (clone->ds_dir->dd_origin_txg > mintxg) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_deadlist_remove_key(&clone->ds_deadlist,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens mintxg, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_remove_clones_key(clone, mintxg, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(clone, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens zap_cursor_fini(&zc);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensvoid
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_destroy_snapshot_sync_impl(dsl_dataset_t *ds, boolean_t defer, dmu_tx_t *tx)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int err;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int after_branch_point = FALSE;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens objset_t *mos = dp->dp_meta_objset;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *ds_prev = NULL;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t obj;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
c166b69d29138aed7a415fe7cef698e54c6ae945Paul Dagnelie rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_bp.blk_birth, <=, tx->tx_txg);
c166b69d29138aed7a415fe7cef698e54c6ae945Paul Dagnelie rrw_exit(&ds->ds_bp_rwlock, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(refcount_is_zero(&ds->ds_longholds));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (defer &&
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs (ds->ds_userrefs > 0 ||
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_num_children > 1)) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_DEFER_DESTROY;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens spa_history_log_internal_ds(ds, "defer_destroy", tx, "");
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_num_children, <=, 1);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* We need to log before removing it from the namespace. */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens spa_history_log_internal_ds(ds, "destroy", tx, "");
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_scan_ds_destroyed(ds, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens obj = ds->ds_object;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens if (ds->ds_feature_inuse[f]) {
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens dsl_dataset_deactivate_feature(obj, f, tx);
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens ds->ds_feature_inuse[f] = B_FALSE;
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens }
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens }
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT3P(ds->ds_prev, ==, NULL);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &ds_prev));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens after_branch_point =
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs (dsl_dataset_phys(ds_prev)->ds_next_snap_obj != obj);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_buf_will_dirty(ds_prev->ds_dbuf, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (after_branch_point &&
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds_prev)->ds_next_clones_obj != 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_remove_from_next_clones(ds_prev, obj, tx);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_next_snap_obj != 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(zap_add_int(mos,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds_prev)->
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ds_next_clones_obj,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_next_snap_obj,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs tx));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (!after_branch_point) {
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds_prev)->ds_next_snap_obj =
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_next_snap_obj;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *ds_next;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t old_unique;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t used = 0, comp = 0, uncomp = 0;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_next_snap_obj, FTAG, &ds_next));
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds_next)->ds_prev_snap_obj, ==, obj);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs old_unique = dsl_dataset_phys(ds_next)->ds_unique_bytes;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_buf_will_dirty(ds_next->ds_dbuf, tx);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds_next)->ds_prev_snap_obj =
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds_next)->ds_prev_snap_txg =
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_txg;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, ==,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ds_prev ? dsl_dataset_phys(ds_prev)->ds_creation_txg : 0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (ds_next->ds_deadlist.dl_oldfmt) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens process_old_deadlist(ds, ds_prev, ds_next,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens after_branch_point, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens } else {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* Adjust prev's unique space. */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (ds_prev && !after_branch_point) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_deadlist_space_range(&ds_next->ds_deadlist,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds_prev)->ds_prev_snap_txg,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_txg,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens &used, &comp, &uncomp);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds_prev)->ds_unique_bytes += used;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* Adjust snapused. */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_deadlist_space_range(&ds_next->ds_deadlist,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_txg, UINT64_MAX,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens &used, &comp, &uncomp);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens -used, -comp, -uncomp, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* Move blocks to be freed to pool's free list. */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_deadlist_move_bpobj(&ds_next->ds_deadlist,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs &dp->dp_free_bpobj, dsl_dataset_phys(ds)->ds_prev_snap_txg,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_diduse_space(tx->tx_pool->dp_free_dir,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens DD_USED_HEAD, used, comp, uncomp, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* Merge our deadlist into next's and free it. */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_deadlist_merge(&ds_next->ds_deadlist,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_deadlist_obj, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_deadlist_close(&ds->ds_deadlist);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_deadlist_free(mos, dsl_dataset_phys(ds)->ds_deadlist_obj, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_deadlist_obj = 0;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* Collapse range in clone heads */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_remove_clones_key(ds,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_creation_txg, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs if (ds_next->ds_is_snapshot) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *ds_nextnext;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Update next's unique to include blocks which
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * were previously shared by only this snapshot
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * and it. Those blocks will be born after the
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * prev snap and before this snap, and will have
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * died after the next snap and before the one
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * after that (ie. be on the snap after next's
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * deadlist).
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds_next)->ds_next_snap_obj,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs FTAG, &ds_nextnext));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_deadlist_space_range(&ds_nextnext->ds_deadlist,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_txg,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_creation_txg,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens &used, &comp, &uncomp);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds_next)->ds_unique_bytes += used;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds_nextnext, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT3P(ds_next->ds_prev, ==, NULL);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* Collapse range in this head. */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *hds;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj, FTAG, &hds));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_deadlist_remove_key(&hds->ds_deadlist,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_creation_txg, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(hds, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens } else {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT3P(ds_next->ds_prev, ==, ds);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds_next->ds_prev, ds_next);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ds_next->ds_prev = NULL;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (ds_prev) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ds_next, &ds_next->ds_prev));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_recalc_head_uniq(ds_next);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Reduce the amount of our unconsumed refreservation
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * being charged to our parent by the amount of
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * new unique data we have gained.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (old_unique < ds_next->ds_reserved) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int64_t mrsdelta;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t new_unique =
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds_next)->ds_unique_bytes;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(old_unique <= new_unique);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens mrsdelta = MIN(new_unique - old_unique,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ds_next->ds_reserved - old_unique);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_diduse_space(ds->ds_dir,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens DD_USED_REFRSRV, -mrsdelta, 0, 0, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds_next, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * This must be done after the dsl_traverse(), because it will
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * re-open the objset.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (ds->ds_objset) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_objset_evict(ds->ds_objset);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ds->ds_objset = NULL;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* remove from snapshot namespace */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *ds_head;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT(dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj, FTAG, &ds_head));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_get_snapname(ds));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens#ifdef ZFS_DEBUG
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t val;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens err = dsl_dataset_snap_lookup(ds_head,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ds->ds_snapname, &val);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT0(err);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT3U(val, ==, obj);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens#endif
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek VERIFY0(dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx, B_TRUE));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds_head, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (ds_prev != NULL)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds_prev, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_next_clones_obj != 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t count;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT0(zap_count(mos,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_next_clones_obj, &count) &&
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs count == 0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dmu_object_free(mos,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_next_clones_obj, tx));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_props_obj != 0)
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs VERIFY0(zap_destroy(mos, dsl_dataset_phys(ds)->ds_props_obj,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs tx));
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_userrefs_obj != 0)
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs VERIFY0(zap_destroy(mos, dsl_dataset_phys(ds)->ds_userrefs_obj,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs tx));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_rele(ds->ds_dir, ds);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ds->ds_dir = NULL;
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens dmu_object_free_zapified(mos, obj, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstatic void
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_destroy_snapshot_sync(void *arg, dmu_tx_t *tx)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_snapshots_destroy_arg_t *dsda = arg;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens nvpair_t *pair;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens for (pair = nvlist_next_nvpair(dsda->dsda_successful_snaps, NULL);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens pair != NULL;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens pair = nvlist_next_nvpair(dsda->dsda_successful_snaps, pair)) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *ds;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_hold(dp, nvpair_name(pair), FTAG, &ds));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_destroy_snapshot_sync_impl(ds, dsda->dsda_defer, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens/*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * The semantics of this function are described in the comment above
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * lzc_destroy_snaps(). To summarize:
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * The snapshots must all be in the same pool.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Snapshots that don't exist will be silently ignored (considered to be
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * "already deleted").
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * On success, all snaps will be destroyed and this will return 0.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * On failure, no snaps will be destroyed, the errlist will be filled in,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * and this will return an errno.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensint
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_destroy_snapshots_nvl(nvlist_t *snaps, boolean_t defer,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens nvlist_t *errlist)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_snapshots_destroy_arg_t dsda;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int error;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens nvpair_t *pair;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens pair = nvlist_next_nvpair(snaps, NULL);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (pair == NULL)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsda.dsda_snaps = snaps;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsda.dsda_successful_snaps = fnvlist_alloc();
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsda.dsda_defer = defer;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsda.dsda_errlist = errlist;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_sync_task(nvpair_name(pair),
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_destroy_snapshot_check, dsl_destroy_snapshot_sync,
7d46dc6ca63a6f3f0d51aa655bfcf10cf2405a9eMatthew Ahrens &dsda, 0, ZFS_SPACE_CHECK_NONE);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens fnvlist_free(dsda.dsda_successful_snaps);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensint
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_destroy_snapshot(const char *name, boolean_t defer)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int error;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens nvlist_t *nvl = fnvlist_alloc();
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens nvlist_t *errlist = fnvlist_alloc();
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens fnvlist_add_boolean(nvl, name);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_destroy_snapshots_nvl(nvl, defer, errlist);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens fnvlist_free(errlist);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens fnvlist_free(nvl);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstruct killarg {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *ds;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_tx_t *tx;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens};
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens/* ARGSUSED */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstatic int
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrenskill_blkptr(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
7802d7bf98dec568dadf72286893b1fe5abd8602Matthew Ahrens const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens struct killarg *ka = arg;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_tx_t *tx = ka->tx;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (zb->zb_level == ZB_ZIL_LEVEL) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(zilog != NULL);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * It's a block in the intent log. It has no
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * accounting, so just free it.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_free(ka->tx->tx_pool, ka->tx->tx_txg, bp);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens } else {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(zilog == NULL);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT3U(bp->blk_birth, >,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ka->ds)->ds_prev_snap_txg);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens (void) dsl_dataset_block_kill(ka->ds, bp, tx, B_FALSE);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstatic void
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensold_synchronous_dataset_destroy(dsl_dataset_t *ds, dmu_tx_t *tx)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens struct killarg ka;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Free everything that we point to (that's born after
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * the previous snapshot, if we are a clone)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * NB: this should be very quick, because we already
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * freed all the objects in open context.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ka.ds = ds;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ka.tx = tx;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(traverse_dataset(ds,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_txg, TRAVERSE_POST,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens kill_blkptr, &ka));
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) ||
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes == 0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrenstypedef struct dsl_destroy_head_arg {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens const char *ddha_name;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens} dsl_destroy_head_arg_t;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensint
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_destroy_head_check_impl(dsl_dataset_t *ds, int expected_holds)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int error;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t count;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens objset_t *mos;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs ASSERT(!ds->ds_is_snapshot);
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs if (ds->ds_is_snapshot)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINVAL));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (refcount_count(&ds->ds_longholds) != expected_holds)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EBUSY));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens mos = ds->ds_dir->dd_pool->dp_meta_objset;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Can't delete a head dataset if there are snapshots of it.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * (Except if the only snapshots are from the branch we cloned
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * from.)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (ds->ds_prev != NULL &&
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj == ds->ds_object)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EBUSY));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Can't delete if there are children of this fs.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = zap_count(mos,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dir_phys(ds->ds_dir)->dd_child_dir_zapobj, &count);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (count != 0)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EEXIST));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev) &&
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_num_children == 2 &&
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ds->ds_prev->ds_userrefs == 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* We need to remove the origin snapshot as well. */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (!refcount_is_zero(&ds->ds_prev->ds_longholds))
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EBUSY));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstatic int
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_destroy_head_check(void *arg, dmu_tx_t *tx)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_destroy_head_arg_t *ddha = arg;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *ds;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int error;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_destroy_head_check_impl(ds, 0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstatic void
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dir_destroy_sync(uint64_t ddobj, dmu_tx_t *tx)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_t *dd;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens objset_t *mos = dp->dp_meta_objset;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dd_used_t t;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(RRW_WRITE_HELD(&dmu_tx_pool(tx)->dp_config_rwlock));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT0(dsl_dir_phys(dd)->dd_head_dataset_obj);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek /*
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * Decrement the filesystem count for all parent filesystems.
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek *
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * When we receive an incremental stream into a filesystem that already
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * exists, a temporary clone is created. We never count this temporary
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * clone, whose name begins with a '%'.
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek */
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek if (dd->dd_myname[0] != '%' && dd->dd_parent != NULL)
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek dsl_fs_ss_count_adjust(dd->dd_parent, -1,
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek DD_FIELD_FILESYSTEM_COUNT, tx);
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Remove our reservation. The impl() routine avoids setting the
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * actual property, which would require the (already destroyed) ds.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_set_reservation_sync_impl(dd, 0, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT0(dsl_dir_phys(dd)->dd_used_bytes);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT0(dsl_dir_phys(dd)->dd_reserved);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens for (t = 0; t < DD_USED_NUM; t++)
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT0(dsl_dir_phys(dd)->dd_used_breakdown[t]);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs VERIFY0(zap_destroy(mos, dsl_dir_phys(dd)->dd_child_dir_zapobj, tx));
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs VERIFY0(zap_destroy(mos, dsl_dir_phys(dd)->dd_props_zapobj, tx));
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs VERIFY0(dsl_deleg_destroy(mos, dsl_dir_phys(dd)->dd_deleg_zapobj, tx));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(zap_remove(mos,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dir_phys(dd->dd_parent)->dd_child_dir_zapobj,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dd->dd_myname, tx));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_rele(dd, FTAG);
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens dmu_object_free_zapified(mos, ddobj, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensvoid
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_destroy_head_sync_impl(dsl_dataset_t *ds, dmu_tx_t *tx)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens objset_t *mos = dp->dp_meta_objset;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t obj, ddobj, prevobj = 0;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens boolean_t rmorigin;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_num_children, <=, 1);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(ds->ds_prev == NULL ||
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj != ds->ds_object);
c166b69d29138aed7a415fe7cef698e54c6ae945Paul Dagnelie rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_bp.blk_birth, <=, tx->tx_txg);
c166b69d29138aed7a415fe7cef698e54c6ae945Paul Dagnelie rrw_exit(&ds->ds_bp_rwlock, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* We need to log before removing it from the namespace. */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens spa_history_log_internal_ds(ds, "destroy", tx, "");
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens rmorigin = (dsl_dir_is_clone(ds->ds_dir) &&
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens DS_IS_DEFER_DESTROY(ds->ds_prev) &&
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_num_children == 2 &&
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ds->ds_prev->ds_userrefs == 0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens /* Remove our reservation. */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (ds->ds_reserved != 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_set_refreservation_sync_impl(ds,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED),
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens 0, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT0(ds->ds_reserved);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens obj = ds->ds_object;
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens if (ds->ds_feature_inuse[f]) {
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens dsl_dataset_deactivate_feature(obj, f, tx);
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens ds->ds_feature_inuse[f] = B_FALSE;
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens }
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens dsl_scan_ds_destroyed(ds, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* This is a clone */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(ds->ds_prev != NULL);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj, !=,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs obj);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT0(dsl_dataset_phys(ds)->ds_next_snap_obj);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dataset_phys(ds->ds_prev)->ds_next_clones_obj != 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_remove_from_next_clones(ds->ds_prev,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens obj, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds->ds_prev)->ds_num_children, >, 1);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_num_children--;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Destroy the deadlist. Unless it's a clone, the
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * deadlist should be empty. (If it's a clone, it's
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * safe to ignore the deadlist contents.)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_deadlist_close(&ds->ds_deadlist);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_deadlist_free(mos, dsl_dataset_phys(ds)->ds_deadlist_obj, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_deadlist_obj = 0;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens objset_t *os;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dmu_objset_from_ds(ds, &os));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_ASYNC_DESTROY)) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens old_synchronous_dataset_destroy(ds, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens } else {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Move the bptree into the pool's list of trees to
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * clean up and update space accounting information.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t used, comp, uncomp;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens zil_destroy_sync(dmu_objset_zil(os), tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens if (!spa_feature_is_active(dp->dp_spa,
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens SPA_FEATURE_ASYNC_DESTROY)) {
4a92375985c37d61406d66cd2b10ee642eb1f5e7George Wilson dsl_scan_t *scn = dp->dp_scan;
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens spa_feature_incr(dp->dp_spa, SPA_FEATURE_ASYNC_DESTROY,
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dp->dp_bptree_obj = bptree_alloc(mos, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(zap_add(mos,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens DMU_POOL_DIRECTORY_OBJECT,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens &dp->dp_bptree_obj, tx));
4a92375985c37d61406d66cd2b10ee642eb1f5e7George Wilson ASSERT(!scn->scn_async_destroying);
4a92375985c37d61406d66cd2b10ee642eb1f5e7George Wilson scn->scn_async_destroying = B_TRUE;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs used = dsl_dir_phys(ds->ds_dir)->dd_used_bytes;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs comp = dsl_dir_phys(ds->ds_dir)->dd_compressed_bytes;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs uncomp = dsl_dir_phys(ds->ds_dir)->dd_uncompressed_bytes;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) ||
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes == used);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
c166b69d29138aed7a415fe7cef698e54c6ae945Paul Dagnelie rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens bptree_add(mos, dp->dp_bptree_obj,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs &dsl_dataset_phys(ds)->ds_bp,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_txg,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens used, comp, uncomp, tx);
c166b69d29138aed7a415fe7cef698e54c6ae945Paul Dagnelie rrw_exit(&ds->ds_bp_rwlock, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens -used, -comp, -uncomp, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_diduse_space(dp->dp_free_dir, DD_USED_HEAD,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens used, comp, uncomp, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (ds->ds_prev != NULL) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(zap_remove_int(mos,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dir_phys(ds->ds_prev->ds_dir)->dd_clones,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ds->ds_object, tx));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens prevobj = ds->ds_prev->ds_object;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds->ds_prev, ds);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ds->ds_prev = NULL;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * This must be done after the dsl_traverse(), because it will
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * re-open the objset.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (ds->ds_objset) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_objset_evict(ds->ds_objset);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ds->ds_objset = NULL;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* Erase the link in the dir */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj = 0;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ddobj = ds->ds_dir->dd_object;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT(dsl_dataset_phys(ds)->ds_snapnames_zapobj != 0);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs VERIFY0(zap_destroy(mos,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_snapnames_zapobj, tx));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens if (ds->ds_bookmarks != 0) {
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs VERIFY0(zap_destroy(mos, ds->ds_bookmarks, tx));
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens spa_feature_decr(dp->dp_spa, SPA_FEATURE_BOOKMARKS, tx);
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens }
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT0(dsl_dataset_phys(ds)->ds_next_clones_obj);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT0(dsl_dataset_phys(ds)->ds_props_obj);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT0(dsl_dataset_phys(ds)->ds_userrefs_obj);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_rele(ds->ds_dir, ds);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ds->ds_dir = NULL;
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens dmu_object_free_zapified(mos, obj, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_destroy_sync(ddobj, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (rmorigin) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *prev;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, prevobj, FTAG, &prev));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_destroy_snapshot_sync_impl(prev, B_FALSE, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(prev, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstatic void
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_destroy_head_sync(void *arg, dmu_tx_t *tx)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_destroy_head_arg_t *ddha = arg;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *ds;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_destroy_head_sync_impl(ds, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstatic void
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_destroy_head_begin_sync(void *arg, dmu_tx_t *tx)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_destroy_head_arg_t *ddha = arg;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *ds;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* Mark it as inconsistent on-disk, in case we crash */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_INCONSISTENT;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens spa_history_log_internal_ds(ds, "destroy begin", tx, "");
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensint
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_destroy_head(const char *name)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_destroy_head_arg_t ddha;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int error;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens spa_t *spa;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens boolean_t isenabled;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens#ifdef _KERNEL
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens zfs_destroy_unmount_origin(name);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens#endif
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = spa_open(name, &spa, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens isenabled = spa_feature_is_enabled(spa, SPA_FEATURE_ASYNC_DESTROY);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens spa_close(spa, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ddha.ddha_name = name;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (!isenabled) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens objset_t *os;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_sync_task(name, dsl_destroy_head_check,
7d46dc6ca63a6f3f0d51aa655bfcf10cf2405a9eMatthew Ahrens dsl_destroy_head_begin_sync, &ddha,
7d46dc6ca63a6f3f0d51aa655bfcf10cf2405a9eMatthew Ahrens 0, ZFS_SPACE_CHECK_NONE);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Head deletion is processed in one txg on old pools;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * remove the objects from open context so that the txg sync
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * is not too long.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dmu_objset_own(name, DMU_OST_ANY, B_FALSE, FTAG, &os);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error == 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t prev_snap_txg =
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(dmu_objset_ds(os))->
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ds_prev_snap_txg;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens for (uint64_t obj = 0; error == 0;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dmu_object_next(os, &obj, FALSE,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens prev_snap_txg))
713d6c208802cfbb806329ec0d154b641b80c355Matthew Ahrens (void) dmu_free_long_object(os, obj);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* sync out all frees */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens txg_wait_synced(dmu_objset_pool(os), 0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_objset_disown(os, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (dsl_sync_task(name, dsl_destroy_head_check,
7d46dc6ca63a6f3f0d51aa655bfcf10cf2405a9eMatthew Ahrens dsl_destroy_head_sync, &ddha, 0, ZFS_SPACE_CHECK_NONE));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens/*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Note, this function is used as the callback for dmu_objset_find(). We
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * always return 0 so that we will continue to find and process
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * inconsistent datasets, even if we encounter an error trying to
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * process one of them.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens/* ARGSUSED */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensint
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_destroy_inconsistent(const char *dsname, void *arg)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens objset_t *os;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (dmu_objset_hold(dsname, FTAG, &os) == 0) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens boolean_t need_destroy = DS_IS_INCONSISTENT(dmu_objset_ds(os));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens /*
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * If the dataset is inconsistent because a resumable receive
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * has failed, then do not destroy it.
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens */
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (dsl_dataset_has_resume_receive_state(dmu_objset_ds(os)))
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens need_destroy = B_FALSE;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_objset_rele(os, FTAG);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (need_destroy)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens (void) dsl_destroy_head(dsname);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}