usr/src/uts/common/fs/zfs/dsl_destroy.c
author Matthew Ahrens <mahrens@delphix.com>
Tue, 20 Aug 2013 20:11:52 -0800
changeset 14234 189ec27885ae
parent 14177 8a2884391fc2
permissions -rw-r--r--
4047 panic from dbuf_free_range() from dmu_free_object() while doing zfs receive Reviewed by: Adam Leventhal <[email protected]> Reviewed by: George Wilson <[email protected]> Approved by: Dan McDonald <[email protected]>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14046
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
     1
/*
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
     2
 * CDDL HEADER START
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
     3
 *
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
     4
 * The contents of this file are subject to the terms of the
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
     5
 * Common Development and Distribution License (the "License").
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
     6
 * You may not use this file except in compliance with the License.
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
     7
 *
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
     8
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
     9
 * or http://www.opensolaris.org/os/licensing.
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    10
 * See the License for the specific language governing permissions
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    11
 * and limitations under the License.
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    12
 *
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    13
 * When distributing Covered Code, include this CDDL HEADER in each
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    14
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    15
 * If applicable, add the following below this CDDL HEADER, with the
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    16
 * fields enclosed by brackets "[]" replaced with your own identifying
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    17
 * information: Portions Copyright [yyyy] [name of copyright owner]
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    18
 *
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    19
 * CDDL HEADER END
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    20
 */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    21
/*
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    22
 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
14054
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
    23
 * Copyright (c) 2013 by Delphix. All rights reserved.
14128
1a272fe1337b 3740 Poor ZFS send / receive performance due to snapshot hold / release processing
Steven Hartland <smh@freebsd.org>
parents: 14094
diff changeset
    24
 * Copyright (c) 2013 Steven Hartland. All rights reserved.
14046
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    25
 */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    26
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    27
#include <sys/zfs_context.h>
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    28
#include <sys/dsl_userhold.h>
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    29
#include <sys/dsl_dataset.h>
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    30
#include <sys/dsl_synctask.h>
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    31
#include <sys/dmu_tx.h>
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    32
#include <sys/dsl_pool.h>
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    33
#include <sys/dsl_dir.h>
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    34
#include <sys/dmu_traverse.h>
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    35
#include <sys/dsl_scan.h>
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    36
#include <sys/dmu_objset.h>
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    37
#include <sys/zap.h>
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    38
#include <sys/zfeature.h>
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    39
#include <sys/zfs_ioctl.h>
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    40
#include <sys/dsl_deleg.h>
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    41
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    42
typedef struct dmu_snapshots_destroy_arg {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    43
	nvlist_t *dsda_snaps;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    44
	nvlist_t *dsda_successful_snaps;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    45
	boolean_t dsda_defer;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    46
	nvlist_t *dsda_errlist;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    47
} dmu_snapshots_destroy_arg_t;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    48
14177
8a2884391fc2 3888 zfs recv -F should destroy any snapshots created since the incremental source
Matthew Ahrens <mahrens@delphix.com>
parents: 14138
diff changeset
    49
int
14046
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    50
dsl_destroy_snapshot_check_impl(dsl_dataset_t *ds, boolean_t defer)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    51
{
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    52
	if (!dsl_dataset_is_snapshot(ds))
14054
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
    53
		return (SET_ERROR(EINVAL));
14046
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    54
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    55
	if (dsl_dataset_long_held(ds))
14054
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
    56
		return (SET_ERROR(EBUSY));
14046
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    57
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    58
	/*
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    59
	 * Only allow deferred destroy on pools that support it.
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    60
	 * NOTE: deferred destroy is only supported on snapshots.
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    61
	 */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    62
	if (defer) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    63
		if (spa_version(ds->ds_dir->dd_pool->dp_spa) <
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    64
		    SPA_VERSION_USERREFS)
14054
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
    65
			return (SET_ERROR(ENOTSUP));
14046
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    66
		return (0);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    67
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    68
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    69
	/*
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    70
	 * If this snapshot has an elevated user reference count,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    71
	 * we can't destroy it yet.
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    72
	 */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    73
	if (ds->ds_userrefs > 0)
14054
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
    74
		return (SET_ERROR(EBUSY));
14046
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    75
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    76
	/*
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    77
	 * Can't delete a branch point.
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    78
	 */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    79
	if (ds->ds_phys->ds_num_children > 1)
14054
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
    80
		return (SET_ERROR(EEXIST));
14046
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    81
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    82
	return (0);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    83
}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    84
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    85
static int
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    86
dsl_destroy_snapshot_check(void *arg, dmu_tx_t *tx)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    87
{
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    88
	dmu_snapshots_destroy_arg_t *dsda = arg;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    89
	dsl_pool_t *dp = dmu_tx_pool(tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    90
	nvpair_t *pair;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    91
	int error = 0;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    92
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    93
	if (!dmu_tx_is_syncing(tx))
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    94
		return (0);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    95
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    96
	for (pair = nvlist_next_nvpair(dsda->dsda_snaps, NULL);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    97
	    pair != NULL; pair = nvlist_next_nvpair(dsda->dsda_snaps, pair)) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    98
		dsl_dataset_t *ds;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
    99
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   100
		error = dsl_dataset_hold(dp, nvpair_name(pair),
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   101
		    FTAG, &ds);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   102
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   103
		/*
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   104
		 * If the snapshot does not exist, silently ignore it
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   105
		 * (it's "already destroyed").
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   106
		 */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   107
		if (error == ENOENT)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   108
			continue;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   109
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   110
		if (error == 0) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   111
			error = dsl_destroy_snapshot_check_impl(ds,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   112
			    dsda->dsda_defer);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   113
			dsl_dataset_rele(ds, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   114
		}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   115
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   116
		if (error == 0) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   117
			fnvlist_add_boolean(dsda->dsda_successful_snaps,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   118
			    nvpair_name(pair));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   119
		} else {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   120
			fnvlist_add_int32(dsda->dsda_errlist,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   121
			    nvpair_name(pair), error);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   122
		}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   123
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   124
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   125
	pair = nvlist_next_nvpair(dsda->dsda_errlist, NULL);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   126
	if (pair != NULL)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   127
		return (fnvpair_value_int32(pair));
14128
1a272fe1337b 3740 Poor ZFS send / receive performance due to snapshot hold / release processing
Steven Hartland <smh@freebsd.org>
parents: 14094
diff changeset
   128
14046
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   129
	return (0);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   130
}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   131
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   132
struct process_old_arg {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   133
	dsl_dataset_t *ds;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   134
	dsl_dataset_t *ds_prev;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   135
	boolean_t after_branch_point;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   136
	zio_t *pio;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   137
	uint64_t used, comp, uncomp;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   138
};
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   139
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   140
static int
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   141
process_old_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   142
{
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   143
	struct process_old_arg *poa = arg;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   144
	dsl_pool_t *dp = poa->ds->ds_dir->dd_pool;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   145
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   146
	if (bp->blk_birth <= poa->ds->ds_phys->ds_prev_snap_txg) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   147
		dsl_deadlist_insert(&poa->ds->ds_deadlist, bp, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   148
		if (poa->ds_prev && !poa->after_branch_point &&
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   149
		    bp->blk_birth >
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   150
		    poa->ds_prev->ds_phys->ds_prev_snap_txg) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   151
			poa->ds_prev->ds_phys->ds_unique_bytes +=
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   152
			    bp_get_dsize_sync(dp->dp_spa, bp);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   153
		}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   154
	} else {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   155
		poa->used += bp_get_dsize_sync(dp->dp_spa, bp);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   156
		poa->comp += BP_GET_PSIZE(bp);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   157
		poa->uncomp += BP_GET_UCSIZE(bp);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   158
		dsl_free_sync(poa->pio, dp, tx->tx_txg, bp);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   159
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   160
	return (0);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   161
}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   162
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   163
static void
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   164
process_old_deadlist(dsl_dataset_t *ds, dsl_dataset_t *ds_prev,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   165
    dsl_dataset_t *ds_next, boolean_t after_branch_point, dmu_tx_t *tx)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   166
{
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   167
	struct process_old_arg poa = { 0 };
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   168
	dsl_pool_t *dp = ds->ds_dir->dd_pool;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   169
	objset_t *mos = dp->dp_meta_objset;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   170
	uint64_t deadlist_obj;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   171
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   172
	ASSERT(ds->ds_deadlist.dl_oldfmt);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   173
	ASSERT(ds_next->ds_deadlist.dl_oldfmt);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   174
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   175
	poa.ds = ds;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   176
	poa.ds_prev = ds_prev;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   177
	poa.after_branch_point = after_branch_point;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   178
	poa.pio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   179
	VERIFY0(bpobj_iterate(&ds_next->ds_deadlist.dl_bpobj,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   180
	    process_old_cb, &poa, tx));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   181
	VERIFY0(zio_wait(poa.pio));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   182
	ASSERT3U(poa.used, ==, ds->ds_phys->ds_unique_bytes);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   183
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   184
	/* change snapused */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   185
	dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   186
	    -poa.used, -poa.comp, -poa.uncomp, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   187
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   188
	/* swap next's deadlist to our deadlist */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   189
	dsl_deadlist_close(&ds->ds_deadlist);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   190
	dsl_deadlist_close(&ds_next->ds_deadlist);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   191
	deadlist_obj = ds->ds_phys->ds_deadlist_obj;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   192
	ds->ds_phys->ds_deadlist_obj = ds_next->ds_phys->ds_deadlist_obj;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   193
	ds_next->ds_phys->ds_deadlist_obj = deadlist_obj;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   194
	dsl_deadlist_open(&ds->ds_deadlist, mos, ds->ds_phys->ds_deadlist_obj);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   195
	dsl_deadlist_open(&ds_next->ds_deadlist, mos,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   196
	    ds_next->ds_phys->ds_deadlist_obj);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   197
}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   198
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   199
static void
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   200
dsl_dataset_remove_clones_key(dsl_dataset_t *ds, uint64_t mintxg, dmu_tx_t *tx)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   201
{
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   202
	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   203
	zap_cursor_t zc;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   204
	zap_attribute_t za;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   205
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   206
	/*
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   207
	 * If it is the old version, dd_clones doesn't exist so we can't
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   208
	 * find the clones, but dsl_deadlist_remove_key() is a no-op so it
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   209
	 * doesn't matter.
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   210
	 */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   211
	if (ds->ds_dir->dd_phys->dd_clones == 0)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   212
		return;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   213
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   214
	for (zap_cursor_init(&zc, mos, ds->ds_dir->dd_phys->dd_clones);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   215
	    zap_cursor_retrieve(&zc, &za) == 0;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   216
	    zap_cursor_advance(&zc)) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   217
		dsl_dataset_t *clone;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   218
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   219
		VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   220
		    za.za_first_integer, FTAG, &clone));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   221
		if (clone->ds_dir->dd_origin_txg > mintxg) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   222
			dsl_deadlist_remove_key(&clone->ds_deadlist,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   223
			    mintxg, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   224
			dsl_dataset_remove_clones_key(clone, mintxg, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   225
		}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   226
		dsl_dataset_rele(clone, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   227
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   228
	zap_cursor_fini(&zc);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   229
}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   230
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   231
void
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   232
dsl_destroy_snapshot_sync_impl(dsl_dataset_t *ds, boolean_t defer, dmu_tx_t *tx)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   233
{
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   234
	int err;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   235
	int after_branch_point = FALSE;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   236
	dsl_pool_t *dp = ds->ds_dir->dd_pool;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   237
	objset_t *mos = dp->dp_meta_objset;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   238
	dsl_dataset_t *ds_prev = NULL;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   239
	uint64_t obj;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   240
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   241
	ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   242
	ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   243
	ASSERT(refcount_is_zero(&ds->ds_longholds));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   244
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   245
	if (defer &&
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   246
	    (ds->ds_userrefs > 0 || ds->ds_phys->ds_num_children > 1)) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   247
		ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   248
		dmu_buf_will_dirty(ds->ds_dbuf, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   249
		ds->ds_phys->ds_flags |= DS_FLAG_DEFER_DESTROY;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   250
		spa_history_log_internal_ds(ds, "defer_destroy", tx, "");
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   251
		return;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   252
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   253
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   254
	ASSERT3U(ds->ds_phys->ds_num_children, <=, 1);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   255
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   256
	/* We need to log before removing it from the namespace. */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   257
	spa_history_log_internal_ds(ds, "destroy", tx, "");
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   258
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   259
	dsl_scan_ds_destroyed(ds, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   260
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   261
	obj = ds->ds_object;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   262
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   263
	if (ds->ds_phys->ds_prev_snap_obj != 0) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   264
		ASSERT3P(ds->ds_prev, ==, NULL);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   265
		VERIFY0(dsl_dataset_hold_obj(dp,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   266
		    ds->ds_phys->ds_prev_snap_obj, FTAG, &ds_prev));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   267
		after_branch_point =
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   268
		    (ds_prev->ds_phys->ds_next_snap_obj != obj);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   269
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   270
		dmu_buf_will_dirty(ds_prev->ds_dbuf, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   271
		if (after_branch_point &&
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   272
		    ds_prev->ds_phys->ds_next_clones_obj != 0) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   273
			dsl_dataset_remove_from_next_clones(ds_prev, obj, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   274
			if (ds->ds_phys->ds_next_snap_obj != 0) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   275
				VERIFY0(zap_add_int(mos,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   276
				    ds_prev->ds_phys->ds_next_clones_obj,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   277
				    ds->ds_phys->ds_next_snap_obj, tx));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   278
			}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   279
		}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   280
		if (!after_branch_point) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   281
			ds_prev->ds_phys->ds_next_snap_obj =
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   282
			    ds->ds_phys->ds_next_snap_obj;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   283
		}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   284
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   285
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   286
	dsl_dataset_t *ds_next;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   287
	uint64_t old_unique;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   288
	uint64_t used = 0, comp = 0, uncomp = 0;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   289
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   290
	VERIFY0(dsl_dataset_hold_obj(dp,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   291
	    ds->ds_phys->ds_next_snap_obj, FTAG, &ds_next));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   292
	ASSERT3U(ds_next->ds_phys->ds_prev_snap_obj, ==, obj);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   293
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   294
	old_unique = ds_next->ds_phys->ds_unique_bytes;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   295
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   296
	dmu_buf_will_dirty(ds_next->ds_dbuf, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   297
	ds_next->ds_phys->ds_prev_snap_obj =
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   298
	    ds->ds_phys->ds_prev_snap_obj;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   299
	ds_next->ds_phys->ds_prev_snap_txg =
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   300
	    ds->ds_phys->ds_prev_snap_txg;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   301
	ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   302
	    ds_prev ? ds_prev->ds_phys->ds_creation_txg : 0);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   303
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   304
	if (ds_next->ds_deadlist.dl_oldfmt) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   305
		process_old_deadlist(ds, ds_prev, ds_next,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   306
		    after_branch_point, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   307
	} else {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   308
		/* Adjust prev's unique space. */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   309
		if (ds_prev && !after_branch_point) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   310
			dsl_deadlist_space_range(&ds_next->ds_deadlist,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   311
			    ds_prev->ds_phys->ds_prev_snap_txg,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   312
			    ds->ds_phys->ds_prev_snap_txg,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   313
			    &used, &comp, &uncomp);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   314
			ds_prev->ds_phys->ds_unique_bytes += used;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   315
		}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   316
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   317
		/* Adjust snapused. */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   318
		dsl_deadlist_space_range(&ds_next->ds_deadlist,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   319
		    ds->ds_phys->ds_prev_snap_txg, UINT64_MAX,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   320
		    &used, &comp, &uncomp);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   321
		dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   322
		    -used, -comp, -uncomp, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   323
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   324
		/* Move blocks to be freed to pool's free list. */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   325
		dsl_deadlist_move_bpobj(&ds_next->ds_deadlist,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   326
		    &dp->dp_free_bpobj, ds->ds_phys->ds_prev_snap_txg,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   327
		    tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   328
		dsl_dir_diduse_space(tx->tx_pool->dp_free_dir,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   329
		    DD_USED_HEAD, used, comp, uncomp, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   330
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   331
		/* Merge our deadlist into next's and free it. */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   332
		dsl_deadlist_merge(&ds_next->ds_deadlist,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   333
		    ds->ds_phys->ds_deadlist_obj, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   334
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   335
	dsl_deadlist_close(&ds->ds_deadlist);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   336
	dsl_deadlist_free(mos, ds->ds_phys->ds_deadlist_obj, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   337
	dmu_buf_will_dirty(ds->ds_dbuf, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   338
	ds->ds_phys->ds_deadlist_obj = 0;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   339
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   340
	/* Collapse range in clone heads */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   341
	dsl_dataset_remove_clones_key(ds,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   342
	    ds->ds_phys->ds_creation_txg, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   343
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   344
	if (dsl_dataset_is_snapshot(ds_next)) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   345
		dsl_dataset_t *ds_nextnext;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   346
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   347
		/*
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   348
		 * Update next's unique to include blocks which
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   349
		 * were previously shared by only this snapshot
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   350
		 * and it.  Those blocks will be born after the
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   351
		 * prev snap and before this snap, and will have
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   352
		 * died after the next snap and before the one
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   353
		 * after that (ie. be on the snap after next's
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   354
		 * deadlist).
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   355
		 */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   356
		VERIFY0(dsl_dataset_hold_obj(dp,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   357
		    ds_next->ds_phys->ds_next_snap_obj, FTAG, &ds_nextnext));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   358
		dsl_deadlist_space_range(&ds_nextnext->ds_deadlist,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   359
		    ds->ds_phys->ds_prev_snap_txg,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   360
		    ds->ds_phys->ds_creation_txg,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   361
		    &used, &comp, &uncomp);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   362
		ds_next->ds_phys->ds_unique_bytes += used;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   363
		dsl_dataset_rele(ds_nextnext, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   364
		ASSERT3P(ds_next->ds_prev, ==, NULL);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   365
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   366
		/* Collapse range in this head. */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   367
		dsl_dataset_t *hds;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   368
		VERIFY0(dsl_dataset_hold_obj(dp,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   369
		    ds->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &hds));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   370
		dsl_deadlist_remove_key(&hds->ds_deadlist,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   371
		    ds->ds_phys->ds_creation_txg, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   372
		dsl_dataset_rele(hds, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   373
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   374
	} else {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   375
		ASSERT3P(ds_next->ds_prev, ==, ds);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   376
		dsl_dataset_rele(ds_next->ds_prev, ds_next);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   377
		ds_next->ds_prev = NULL;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   378
		if (ds_prev) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   379
			VERIFY0(dsl_dataset_hold_obj(dp,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   380
			    ds->ds_phys->ds_prev_snap_obj,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   381
			    ds_next, &ds_next->ds_prev));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   382
		}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   383
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   384
		dsl_dataset_recalc_head_uniq(ds_next);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   385
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   386
		/*
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   387
		 * Reduce the amount of our unconsumed refreservation
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   388
		 * being charged to our parent by the amount of
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   389
		 * new unique data we have gained.
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   390
		 */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   391
		if (old_unique < ds_next->ds_reserved) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   392
			int64_t mrsdelta;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   393
			uint64_t new_unique =
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   394
			    ds_next->ds_phys->ds_unique_bytes;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   395
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   396
			ASSERT(old_unique <= new_unique);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   397
			mrsdelta = MIN(new_unique - old_unique,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   398
			    ds_next->ds_reserved - old_unique);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   399
			dsl_dir_diduse_space(ds->ds_dir,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   400
			    DD_USED_REFRSRV, -mrsdelta, 0, 0, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   401
		}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   402
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   403
	dsl_dataset_rele(ds_next, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   404
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   405
	/*
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   406
	 * This must be done after the dsl_traverse(), because it will
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   407
	 * re-open the objset.
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   408
	 */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   409
	if (ds->ds_objset) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   410
		dmu_objset_evict(ds->ds_objset);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   411
		ds->ds_objset = NULL;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   412
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   413
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   414
	/* remove from snapshot namespace */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   415
	dsl_dataset_t *ds_head;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   416
	ASSERT(ds->ds_phys->ds_snapnames_zapobj == 0);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   417
	VERIFY0(dsl_dataset_hold_obj(dp,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   418
	    ds->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ds_head));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   419
	VERIFY0(dsl_dataset_get_snapname(ds));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   420
#ifdef ZFS_DEBUG
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   421
	{
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   422
		uint64_t val;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   423
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   424
		err = dsl_dataset_snap_lookup(ds_head,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   425
		    ds->ds_snapname, &val);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   426
		ASSERT0(err);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   427
		ASSERT3U(val, ==, obj);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   428
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   429
#endif
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   430
	VERIFY0(dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   431
	dsl_dataset_rele(ds_head, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   432
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   433
	if (ds_prev != NULL)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   434
		dsl_dataset_rele(ds_prev, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   435
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   436
	spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   437
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   438
	if (ds->ds_phys->ds_next_clones_obj != 0) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   439
		uint64_t count;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   440
		ASSERT0(zap_count(mos,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   441
		    ds->ds_phys->ds_next_clones_obj, &count) && count == 0);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   442
		VERIFY0(dmu_object_free(mos,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   443
		    ds->ds_phys->ds_next_clones_obj, tx));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   444
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   445
	if (ds->ds_phys->ds_props_obj != 0)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   446
		VERIFY0(zap_destroy(mos, ds->ds_phys->ds_props_obj, tx));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   447
	if (ds->ds_phys->ds_userrefs_obj != 0)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   448
		VERIFY0(zap_destroy(mos, ds->ds_phys->ds_userrefs_obj, tx));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   449
	dsl_dir_rele(ds->ds_dir, ds);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   450
	ds->ds_dir = NULL;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   451
	VERIFY0(dmu_object_free(mos, obj, tx));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   452
}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   453
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   454
static void
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   455
dsl_destroy_snapshot_sync(void *arg, dmu_tx_t *tx)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   456
{
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   457
	dmu_snapshots_destroy_arg_t *dsda = arg;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   458
	dsl_pool_t *dp = dmu_tx_pool(tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   459
	nvpair_t *pair;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   460
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   461
	for (pair = nvlist_next_nvpair(dsda->dsda_successful_snaps, NULL);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   462
	    pair != NULL;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   463
	    pair = nvlist_next_nvpair(dsda->dsda_successful_snaps, pair)) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   464
		dsl_dataset_t *ds;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   465
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   466
		VERIFY0(dsl_dataset_hold(dp, nvpair_name(pair), FTAG, &ds));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   467
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   468
		dsl_destroy_snapshot_sync_impl(ds, dsda->dsda_defer, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   469
		dsl_dataset_rele(ds, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   470
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   471
}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   472
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   473
/*
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   474
 * The semantics of this function are described in the comment above
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   475
 * lzc_destroy_snaps().  To summarize:
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   476
 *
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   477
 * The snapshots must all be in the same pool.
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   478
 *
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   479
 * Snapshots that don't exist will be silently ignored (considered to be
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   480
 * "already deleted").
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   481
 *
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   482
 * On success, all snaps will be destroyed and this will return 0.
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   483
 * On failure, no snaps will be destroyed, the errlist will be filled in,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   484
 * and this will return an errno.
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   485
 */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   486
int
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   487
dsl_destroy_snapshots_nvl(nvlist_t *snaps, boolean_t defer,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   488
    nvlist_t *errlist)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   489
{
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   490
	dmu_snapshots_destroy_arg_t dsda;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   491
	int error;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   492
	nvpair_t *pair;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   493
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   494
	pair = nvlist_next_nvpair(snaps, NULL);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   495
	if (pair == NULL)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   496
		return (0);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   497
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   498
	dsda.dsda_snaps = snaps;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   499
	dsda.dsda_successful_snaps = fnvlist_alloc();
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   500
	dsda.dsda_defer = defer;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   501
	dsda.dsda_errlist = errlist;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   502
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   503
	error = dsl_sync_task(nvpair_name(pair),
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   504
	    dsl_destroy_snapshot_check, dsl_destroy_snapshot_sync,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   505
	    &dsda, 0);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   506
	fnvlist_free(dsda.dsda_successful_snaps);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   507
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   508
	return (error);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   509
}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   510
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   511
int
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   512
dsl_destroy_snapshot(const char *name, boolean_t defer)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   513
{
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   514
	int error;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   515
	nvlist_t *nvl = fnvlist_alloc();
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   516
	nvlist_t *errlist = fnvlist_alloc();
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   517
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   518
	fnvlist_add_boolean(nvl, name);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   519
	error = dsl_destroy_snapshots_nvl(nvl, defer, errlist);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   520
	fnvlist_free(errlist);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   521
	fnvlist_free(nvl);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   522
	return (error);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   523
}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   524
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   525
struct killarg {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   526
	dsl_dataset_t *ds;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   527
	dmu_tx_t *tx;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   528
};
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   529
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   530
/* ARGSUSED */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   531
static int
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   532
kill_blkptr(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   533
    const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   534
{
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   535
	struct killarg *ka = arg;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   536
	dmu_tx_t *tx = ka->tx;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   537
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   538
	if (bp == NULL)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   539
		return (0);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   540
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   541
	if (zb->zb_level == ZB_ZIL_LEVEL) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   542
		ASSERT(zilog != NULL);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   543
		/*
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   544
		 * It's a block in the intent log.  It has no
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   545
		 * accounting, so just free it.
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   546
		 */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   547
		dsl_free(ka->tx->tx_pool, ka->tx->tx_txg, bp);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   548
	} else {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   549
		ASSERT(zilog == NULL);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   550
		ASSERT3U(bp->blk_birth, >, ka->ds->ds_phys->ds_prev_snap_txg);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   551
		(void) dsl_dataset_block_kill(ka->ds, bp, tx, B_FALSE);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   552
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   553
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   554
	return (0);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   555
}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   556
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   557
static void
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   558
old_synchronous_dataset_destroy(dsl_dataset_t *ds, dmu_tx_t *tx)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   559
{
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   560
	struct killarg ka;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   561
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   562
	/*
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   563
	 * Free everything that we point to (that's born after
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   564
	 * the previous snapshot, if we are a clone)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   565
	 *
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   566
	 * NB: this should be very quick, because we already
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   567
	 * freed all the objects in open context.
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   568
	 */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   569
	ka.ds = ds;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   570
	ka.tx = tx;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   571
	VERIFY0(traverse_dataset(ds,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   572
	    ds->ds_phys->ds_prev_snap_txg, TRAVERSE_POST,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   573
	    kill_blkptr, &ka));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   574
	ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) || ds->ds_phys->ds_unique_bytes == 0);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   575
}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   576
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   577
typedef struct dsl_destroy_head_arg {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   578
	const char *ddha_name;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   579
} dsl_destroy_head_arg_t;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   580
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   581
int
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   582
dsl_destroy_head_check_impl(dsl_dataset_t *ds, int expected_holds)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   583
{
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   584
	int error;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   585
	uint64_t count;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   586
	objset_t *mos;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   587
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   588
	if (dsl_dataset_is_snapshot(ds))
14054
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
   589
		return (SET_ERROR(EINVAL));
14046
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   590
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   591
	if (refcount_count(&ds->ds_longholds) != expected_holds)
14054
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
   592
		return (SET_ERROR(EBUSY));
14046
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   593
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   594
	mos = ds->ds_dir->dd_pool->dp_meta_objset;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   595
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   596
	/*
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   597
	 * Can't delete a head dataset if there are snapshots of it.
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   598
	 * (Except if the only snapshots are from the branch we cloned
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   599
	 * from.)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   600
	 */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   601
	if (ds->ds_prev != NULL &&
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   602
	    ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object)
14054
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
   603
		return (SET_ERROR(EBUSY));
14046
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   604
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   605
	/*
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   606
	 * Can't delete if there are children of this fs.
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   607
	 */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   608
	error = zap_count(mos,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   609
	    ds->ds_dir->dd_phys->dd_child_dir_zapobj, &count);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   610
	if (error != 0)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   611
		return (error);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   612
	if (count != 0)
14054
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
   613
		return (SET_ERROR(EEXIST));
14046
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   614
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   615
	if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev) &&
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   616
	    ds->ds_prev->ds_phys->ds_num_children == 2 &&
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   617
	    ds->ds_prev->ds_userrefs == 0) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   618
		/* We need to remove the origin snapshot as well. */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   619
		if (!refcount_is_zero(&ds->ds_prev->ds_longholds))
14054
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
   620
			return (SET_ERROR(EBUSY));
14046
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   621
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   622
	return (0);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   623
}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   624
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   625
static int
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   626
dsl_destroy_head_check(void *arg, dmu_tx_t *tx)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   627
{
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   628
	dsl_destroy_head_arg_t *ddha = arg;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   629
	dsl_pool_t *dp = dmu_tx_pool(tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   630
	dsl_dataset_t *ds;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   631
	int error;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   632
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   633
	error = dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   634
	if (error != 0)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   635
		return (error);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   636
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   637
	error = dsl_destroy_head_check_impl(ds, 0);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   638
	dsl_dataset_rele(ds, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   639
	return (error);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   640
}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   641
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   642
static void
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   643
dsl_dir_destroy_sync(uint64_t ddobj, dmu_tx_t *tx)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   644
{
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   645
	dsl_dir_t *dd;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   646
	dsl_pool_t *dp = dmu_tx_pool(tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   647
	objset_t *mos = dp->dp_meta_objset;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   648
	dd_used_t t;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   649
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   650
	ASSERT(RRW_WRITE_HELD(&dmu_tx_pool(tx)->dp_config_rwlock));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   651
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   652
	VERIFY0(dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   653
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   654
	ASSERT0(dd->dd_phys->dd_head_dataset_obj);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   655
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   656
	/*
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   657
	 * Remove our reservation. The impl() routine avoids setting the
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   658
	 * actual property, which would require the (already destroyed) ds.
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   659
	 */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   660
	dsl_dir_set_reservation_sync_impl(dd, 0, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   661
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   662
	ASSERT0(dd->dd_phys->dd_used_bytes);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   663
	ASSERT0(dd->dd_phys->dd_reserved);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   664
	for (t = 0; t < DD_USED_NUM; t++)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   665
		ASSERT0(dd->dd_phys->dd_used_breakdown[t]);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   666
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   667
	VERIFY0(zap_destroy(mos, dd->dd_phys->dd_child_dir_zapobj, tx));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   668
	VERIFY0(zap_destroy(mos, dd->dd_phys->dd_props_zapobj, tx));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   669
	VERIFY0(dsl_deleg_destroy(mos, dd->dd_phys->dd_deleg_zapobj, tx));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   670
	VERIFY0(zap_remove(mos,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   671
	    dd->dd_parent->dd_phys->dd_child_dir_zapobj, dd->dd_myname, tx));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   672
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   673
	dsl_dir_rele(dd, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   674
	VERIFY0(dmu_object_free(mos, ddobj, tx));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   675
}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   676
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   677
void
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   678
dsl_destroy_head_sync_impl(dsl_dataset_t *ds, dmu_tx_t *tx)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   679
{
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   680
	dsl_pool_t *dp = dmu_tx_pool(tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   681
	objset_t *mos = dp->dp_meta_objset;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   682
	uint64_t obj, ddobj, prevobj = 0;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   683
	boolean_t rmorigin;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   684
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   685
	ASSERT3U(ds->ds_phys->ds_num_children, <=, 1);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   686
	ASSERT(ds->ds_prev == NULL ||
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   687
	    ds->ds_prev->ds_phys->ds_next_snap_obj != ds->ds_object);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   688
	ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   689
	ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   690
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   691
	/* We need to log before removing it from the namespace. */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   692
	spa_history_log_internal_ds(ds, "destroy", tx, "");
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   693
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   694
	rmorigin = (dsl_dir_is_clone(ds->ds_dir) &&
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   695
	    DS_IS_DEFER_DESTROY(ds->ds_prev) &&
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   696
	    ds->ds_prev->ds_phys->ds_num_children == 2 &&
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   697
	    ds->ds_prev->ds_userrefs == 0);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   698
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   699
	/* Remove our reservation */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   700
	if (ds->ds_reserved != 0) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   701
		dsl_dataset_set_refreservation_sync_impl(ds,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   702
		    (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED),
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   703
		    0, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   704
		ASSERT0(ds->ds_reserved);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   705
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   706
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   707
	dsl_scan_ds_destroyed(ds, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   708
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   709
	obj = ds->ds_object;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   710
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   711
	if (ds->ds_phys->ds_prev_snap_obj != 0) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   712
		/* This is a clone */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   713
		ASSERT(ds->ds_prev != NULL);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   714
		ASSERT3U(ds->ds_prev->ds_phys->ds_next_snap_obj, !=, obj);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   715
		ASSERT0(ds->ds_phys->ds_next_snap_obj);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   716
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   717
		dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   718
		if (ds->ds_prev->ds_phys->ds_next_clones_obj != 0) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   719
			dsl_dataset_remove_from_next_clones(ds->ds_prev,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   720
			    obj, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   721
		}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   722
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   723
		ASSERT3U(ds->ds_prev->ds_phys->ds_num_children, >, 1);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   724
		ds->ds_prev->ds_phys->ds_num_children--;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   725
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   726
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   727
	zfeature_info_t *async_destroy =
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   728
	    &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY];
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   729
	objset_t *os;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   730
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   731
	/*
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   732
	 * Destroy the deadlist.  Unless it's a clone, the
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   733
	 * deadlist should be empty.  (If it's a clone, it's
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   734
	 * safe to ignore the deadlist contents.)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   735
	 */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   736
	dsl_deadlist_close(&ds->ds_deadlist);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   737
	dsl_deadlist_free(mos, ds->ds_phys->ds_deadlist_obj, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   738
	dmu_buf_will_dirty(ds->ds_dbuf, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   739
	ds->ds_phys->ds_deadlist_obj = 0;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   740
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   741
	VERIFY0(dmu_objset_from_ds(ds, &os));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   742
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   743
	if (!spa_feature_is_enabled(dp->dp_spa, async_destroy)) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   744
		old_synchronous_dataset_destroy(ds, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   745
	} else {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   746
		/*
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   747
		 * Move the bptree into the pool's list of trees to
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   748
		 * clean up and update space accounting information.
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   749
		 */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   750
		uint64_t used, comp, uncomp;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   751
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   752
		zil_destroy_sync(dmu_objset_zil(os), tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   753
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   754
		if (!spa_feature_is_active(dp->dp_spa, async_destroy)) {
14094
624fa8f61951 3642 dsl_scan_active() should not issue I/O to determine if async destroying is active
George Wilson <george.wilson@delphix.com>
parents: 14054
diff changeset
   755
			dsl_scan_t *scn = dp->dp_scan;
624fa8f61951 3642 dsl_scan_active() should not issue I/O to determine if async destroying is active
George Wilson <george.wilson@delphix.com>
parents: 14054
diff changeset
   756
14046
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   757
			spa_feature_incr(dp->dp_spa, async_destroy, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   758
			dp->dp_bptree_obj = bptree_alloc(mos, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   759
			VERIFY0(zap_add(mos,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   760
			    DMU_POOL_DIRECTORY_OBJECT,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   761
			    DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   762
			    &dp->dp_bptree_obj, tx));
14094
624fa8f61951 3642 dsl_scan_active() should not issue I/O to determine if async destroying is active
George Wilson <george.wilson@delphix.com>
parents: 14054
diff changeset
   763
			ASSERT(!scn->scn_async_destroying);
624fa8f61951 3642 dsl_scan_active() should not issue I/O to determine if async destroying is active
George Wilson <george.wilson@delphix.com>
parents: 14054
diff changeset
   764
			scn->scn_async_destroying = B_TRUE;
14046
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   765
		}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   766
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   767
		used = ds->ds_dir->dd_phys->dd_used_bytes;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   768
		comp = ds->ds_dir->dd_phys->dd_compressed_bytes;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   769
		uncomp = ds->ds_dir->dd_phys->dd_uncompressed_bytes;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   770
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   771
		ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) ||
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   772
		    ds->ds_phys->ds_unique_bytes == used);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   773
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   774
		bptree_add(mos, dp->dp_bptree_obj,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   775
		    &ds->ds_phys->ds_bp, ds->ds_phys->ds_prev_snap_txg,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   776
		    used, comp, uncomp, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   777
		dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   778
		    -used, -comp, -uncomp, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   779
		dsl_dir_diduse_space(dp->dp_free_dir, DD_USED_HEAD,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   780
		    used, comp, uncomp, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   781
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   782
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   783
	if (ds->ds_prev != NULL) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   784
		if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   785
			VERIFY0(zap_remove_int(mos,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   786
			    ds->ds_prev->ds_dir->dd_phys->dd_clones,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   787
			    ds->ds_object, tx));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   788
		}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   789
		prevobj = ds->ds_prev->ds_object;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   790
		dsl_dataset_rele(ds->ds_prev, ds);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   791
		ds->ds_prev = NULL;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   792
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   793
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   794
	/*
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   795
	 * This must be done after the dsl_traverse(), because it will
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   796
	 * re-open the objset.
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   797
	 */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   798
	if (ds->ds_objset) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   799
		dmu_objset_evict(ds->ds_objset);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   800
		ds->ds_objset = NULL;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   801
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   802
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   803
	/* Erase the link in the dir */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   804
	dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   805
	ds->ds_dir->dd_phys->dd_head_dataset_obj = 0;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   806
	ddobj = ds->ds_dir->dd_object;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   807
	ASSERT(ds->ds_phys->ds_snapnames_zapobj != 0);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   808
	VERIFY0(zap_destroy(mos, ds->ds_phys->ds_snapnames_zapobj, tx));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   809
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   810
	spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   811
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   812
	ASSERT0(ds->ds_phys->ds_next_clones_obj);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   813
	ASSERT0(ds->ds_phys->ds_props_obj);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   814
	ASSERT0(ds->ds_phys->ds_userrefs_obj);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   815
	dsl_dir_rele(ds->ds_dir, ds);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   816
	ds->ds_dir = NULL;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   817
	VERIFY0(dmu_object_free(mos, obj, tx));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   818
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   819
	dsl_dir_destroy_sync(ddobj, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   820
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   821
	if (rmorigin) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   822
		dsl_dataset_t *prev;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   823
		VERIFY0(dsl_dataset_hold_obj(dp, prevobj, FTAG, &prev));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   824
		dsl_destroy_snapshot_sync_impl(prev, B_FALSE, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   825
		dsl_dataset_rele(prev, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   826
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   827
}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   828
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   829
static void
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   830
dsl_destroy_head_sync(void *arg, dmu_tx_t *tx)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   831
{
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   832
	dsl_destroy_head_arg_t *ddha = arg;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   833
	dsl_pool_t *dp = dmu_tx_pool(tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   834
	dsl_dataset_t *ds;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   835
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   836
	VERIFY0(dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   837
	dsl_destroy_head_sync_impl(ds, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   838
	dsl_dataset_rele(ds, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   839
}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   840
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   841
static void
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   842
dsl_destroy_head_begin_sync(void *arg, dmu_tx_t *tx)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   843
{
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   844
	dsl_destroy_head_arg_t *ddha = arg;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   845
	dsl_pool_t *dp = dmu_tx_pool(tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   846
	dsl_dataset_t *ds;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   847
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   848
	VERIFY0(dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   849
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   850
	/* Mark it as inconsistent on-disk, in case we crash */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   851
	dmu_buf_will_dirty(ds->ds_dbuf, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   852
	ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   853
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   854
	spa_history_log_internal_ds(ds, "destroy begin", tx, "");
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   855
	dsl_dataset_rele(ds, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   856
}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   857
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   858
int
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   859
dsl_destroy_head(const char *name)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   860
{
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   861
	dsl_destroy_head_arg_t ddha;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   862
	int error;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   863
	spa_t *spa;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   864
	boolean_t isenabled;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   865
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   866
#ifdef _KERNEL
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   867
	zfs_destroy_unmount_origin(name);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   868
#endif
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   869
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   870
	error = spa_open(name, &spa, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   871
	if (error != 0)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   872
		return (error);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   873
	isenabled = spa_feature_is_enabled(spa,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   874
	    &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY]);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   875
	spa_close(spa, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   876
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   877
	ddha.ddha_name = name;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   878
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   879
	if (!isenabled) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   880
		objset_t *os;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   881
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   882
		error = dsl_sync_task(name, dsl_destroy_head_check,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   883
		    dsl_destroy_head_begin_sync, &ddha, 0);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   884
		if (error != 0)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   885
			return (error);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   886
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   887
		/*
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   888
		 * Head deletion is processed in one txg on old pools;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   889
		 * remove the objects from open context so that the txg sync
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   890
		 * is not too long.
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   891
		 */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   892
		error = dmu_objset_own(name, DMU_OST_ANY, B_FALSE, FTAG, &os);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   893
		if (error == 0) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   894
			uint64_t prev_snap_txg =
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   895
			    dmu_objset_ds(os)->ds_phys->ds_prev_snap_txg;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   896
			for (uint64_t obj = 0; error == 0;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   897
			    error = dmu_object_next(os, &obj, FALSE,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   898
			    prev_snap_txg))
14234
189ec27885ae 4047 panic from dbuf_free_range() from dmu_free_object() while doing zfs receive
Matthew Ahrens <mahrens@delphix.com>
parents: 14177
diff changeset
   899
				(void) dmu_free_long_object(os, obj);
14046
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   900
			/* sync out all frees */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   901
			txg_wait_synced(dmu_objset_pool(os), 0);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   902
			dmu_objset_disown(os, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   903
		}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   904
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   905
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   906
	return (dsl_sync_task(name, dsl_destroy_head_check,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   907
	    dsl_destroy_head_sync, &ddha, 0));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   908
}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   909
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   910
/*
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   911
 * Note, this function is used as the callback for dmu_objset_find().  We
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   912
 * always return 0 so that we will continue to find and process
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   913
 * inconsistent datasets, even if we encounter an error trying to
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   914
 * process one of them.
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   915
 */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   916
/* ARGSUSED */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   917
int
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   918
dsl_destroy_inconsistent(const char *dsname, void *arg)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   919
{
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   920
	objset_t *os;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   921
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   922
	if (dmu_objset_hold(dsname, FTAG, &os) == 0) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   923
		boolean_t inconsistent = DS_IS_INCONSISTENT(dmu_objset_ds(os));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   924
		dmu_objset_rele(os, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   925
		if (inconsistent)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   926
			(void) dsl_destroy_head(dsname);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   927
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   928
	return (0);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents:
diff changeset
   929
}