]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/dsl_destroy.c
linux spl: fix typo in top comment of spl-condvar.c
[mirror_zfs.git] / module / zfs / dsl_destroy.c
CommitLineData
13fe0198
MA
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
1d3ba0bf 9 * or https://opensource.org/licenses/CDDL-1.0.
13fe0198
MA
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
d52d80b7 23 * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
95fd54a1 24 * Copyright (c) 2013 Steven Hartland. All rights reserved.
788eb90c 25 * Copyright (c) 2013 by Joyent, Inc. All rights reserved.
a0bd735a 26 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
13fe0198
MA
27 */
28
29#include <sys/zfs_context.h>
30#include <sys/dsl_userhold.h>
31#include <sys/dsl_dataset.h>
32#include <sys/dsl_synctask.h>
d99a0153 33#include <sys/dsl_destroy.h>
30af21b0 34#include <sys/dsl_bookmark.h>
13fe0198
MA
35#include <sys/dmu_tx.h>
36#include <sys/dsl_pool.h>
37#include <sys/dsl_dir.h>
38#include <sys/dmu_traverse.h>
39#include <sys/dsl_scan.h>
40#include <sys/dmu_objset.h>
41#include <sys/zap.h>
42#include <sys/zfeature.h>
43#include <sys/zfs_ioctl.h>
44#include <sys/dsl_deleg.h>
fa86b5db 45#include <sys/dmu_impl.h>
a0bd735a 46#include <sys/zvol.h>
d99a0153 47#include <sys/zcp.h>
37f03da8
SH
48#include <sys/dsl_deadlist.h>
49#include <sys/zthr.h>
50#include <sys/spa_impl.h>
13fe0198 51
17e21265
PD
52extern int zfs_snapshot_history_enabled;
53
19580676 54int
13fe0198
MA
55dsl_destroy_snapshot_check_impl(dsl_dataset_t *ds, boolean_t defer)
56{
0c66c32d 57 if (!ds->ds_is_snapshot)
2e528b49 58 return (SET_ERROR(EINVAL));
13fe0198
MA
59
60 if (dsl_dataset_long_held(ds))
2e528b49 61 return (SET_ERROR(EBUSY));
13fe0198
MA
62
63 /*
64 * Only allow deferred destroy on pools that support it.
65 * NOTE: deferred destroy is only supported on snapshots.
66 */
67 if (defer) {
68 if (spa_version(ds->ds_dir->dd_pool->dp_spa) <
69 SPA_VERSION_USERREFS)
2e528b49 70 return (SET_ERROR(ENOTSUP));
13fe0198
MA
71 return (0);
72 }
73
74 /*
75 * If this snapshot has an elevated user reference count,
76 * we can't destroy it yet.
77 */
78 if (ds->ds_userrefs > 0)
2e528b49 79 return (SET_ERROR(EBUSY));
13fe0198
MA
80
81 /*
82 * Can't delete a branch point.
83 */
d683ddbb 84 if (dsl_dataset_phys(ds)->ds_num_children > 1)
2e528b49 85 return (SET_ERROR(EEXIST));
13fe0198
MA
86
87 return (0);
88}
89
d99a0153 90int
13fe0198
MA
91dsl_destroy_snapshot_check(void *arg, dmu_tx_t *tx)
92{
d99a0153
CW
93 dsl_destroy_snapshot_arg_t *ddsa = arg;
94 const char *dsname = ddsa->ddsa_name;
95 boolean_t defer = ddsa->ddsa_defer;
96
13fe0198 97 dsl_pool_t *dp = dmu_tx_pool(tx);
13fe0198 98 int error = 0;
d99a0153 99 dsl_dataset_t *ds;
13fe0198 100
d99a0153 101 error = dsl_dataset_hold(dp, dsname, FTAG, &ds);
13fe0198 102
d99a0153
CW
103 /*
104 * If the snapshot does not exist, silently ignore it, and
105 * dsl_destroy_snapshot_sync() will be a no-op
106 * (it's "already destroyed").
107 */
108 if (error == ENOENT)
109 return (0);
13fe0198 110
d99a0153
CW
111 if (error == 0) {
112 error = dsl_destroy_snapshot_check_impl(ds, defer);
113 dsl_dataset_rele(ds, FTAG);
13fe0198
MA
114 }
115
d99a0153 116 return (error);
13fe0198
MA
117}
118
119struct process_old_arg {
120 dsl_dataset_t *ds;
121 dsl_dataset_t *ds_prev;
122 boolean_t after_branch_point;
123 zio_t *pio;
124 uint64_t used, comp, uncomp;
125};
126
127static int
37f03da8 128process_old_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed, dmu_tx_t *tx)
13fe0198
MA
129{
130 struct process_old_arg *poa = arg;
131 dsl_pool_t *dp = poa->ds->ds_dir->dd_pool;
132
b0bc7a84
MG
133 ASSERT(!BP_IS_HOLE(bp));
134
d683ddbb 135 if (bp->blk_birth <= dsl_dataset_phys(poa->ds)->ds_prev_snap_txg) {
37f03da8 136 dsl_deadlist_insert(&poa->ds->ds_deadlist, bp, bp_freed, tx);
13fe0198
MA
137 if (poa->ds_prev && !poa->after_branch_point &&
138 bp->blk_birth >
d683ddbb
JG
139 dsl_dataset_phys(poa->ds_prev)->ds_prev_snap_txg) {
140 dsl_dataset_phys(poa->ds_prev)->ds_unique_bytes +=
13fe0198
MA
141 bp_get_dsize_sync(dp->dp_spa, bp);
142 }
143 } else {
144 poa->used += bp_get_dsize_sync(dp->dp_spa, bp);
145 poa->comp += BP_GET_PSIZE(bp);
146 poa->uncomp += BP_GET_UCSIZE(bp);
147 dsl_free_sync(poa->pio, dp, tx->tx_txg, bp);
148 }
149 return (0);
150}
151
152static void
153process_old_deadlist(dsl_dataset_t *ds, dsl_dataset_t *ds_prev,
154 dsl_dataset_t *ds_next, boolean_t after_branch_point, dmu_tx_t *tx)
155{
156 struct process_old_arg poa = { 0 };
157 dsl_pool_t *dp = ds->ds_dir->dd_pool;
158 objset_t *mos = dp->dp_meta_objset;
159 uint64_t deadlist_obj;
160
161 ASSERT(ds->ds_deadlist.dl_oldfmt);
162 ASSERT(ds_next->ds_deadlist.dl_oldfmt);
163
164 poa.ds = ds;
165 poa.ds_prev = ds_prev;
166 poa.after_branch_point = after_branch_point;
167 poa.pio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
168 VERIFY0(bpobj_iterate(&ds_next->ds_deadlist.dl_bpobj,
169 process_old_cb, &poa, tx));
170 VERIFY0(zio_wait(poa.pio));
d683ddbb 171 ASSERT3U(poa.used, ==, dsl_dataset_phys(ds)->ds_unique_bytes);
13fe0198
MA
172
173 /* change snapused */
174 dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
175 -poa.used, -poa.comp, -poa.uncomp, tx);
176
177 /* swap next's deadlist to our deadlist */
178 dsl_deadlist_close(&ds->ds_deadlist);
179 dsl_deadlist_close(&ds_next->ds_deadlist);
d683ddbb
JG
180 deadlist_obj = dsl_dataset_phys(ds)->ds_deadlist_obj;
181 dsl_dataset_phys(ds)->ds_deadlist_obj =
182 dsl_dataset_phys(ds_next)->ds_deadlist_obj;
183 dsl_dataset_phys(ds_next)->ds_deadlist_obj = deadlist_obj;
184 dsl_deadlist_open(&ds->ds_deadlist, mos,
185 dsl_dataset_phys(ds)->ds_deadlist_obj);
13fe0198 186 dsl_deadlist_open(&ds_next->ds_deadlist, mos,
d683ddbb 187 dsl_dataset_phys(ds_next)->ds_deadlist_obj);
13fe0198
MA
188}
189
30af21b0
PD
190typedef struct remaining_clones_key {
191 dsl_dataset_t *rck_clone;
192 list_node_t rck_node;
193} remaining_clones_key_t;
194
195static remaining_clones_key_t *
196rck_alloc(dsl_dataset_t *clone)
197{
198 remaining_clones_key_t *rck = kmem_alloc(sizeof (*rck), KM_SLEEP);
199 rck->rck_clone = clone;
200 return (rck);
201}
c434d880 202
13fe0198 203static void
30af21b0 204dsl_dir_remove_clones_key_impl(dsl_dir_t *dd, uint64_t mintxg, dmu_tx_t *tx,
dd66857d 205 list_t *stack, const void *tag)
13fe0198 206{
30af21b0
PD
207 objset_t *mos = dd->dd_pool->dp_meta_objset;
208
209 /*
210 * If it is the old version, dd_clones doesn't exist so we can't
211 * find the clones, but dsl_deadlist_remove_key() is a no-op so it
212 * doesn't matter.
213 */
214 if (dsl_dir_phys(dd)->dd_clones == 0)
215 return;
13fe0198 216
30af21b0
PD
217 zap_cursor_t *zc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP);
218 zap_attribute_t *za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
c434d880 219
30af21b0
PD
220 for (zap_cursor_init(zc, mos, dsl_dir_phys(dd)->dd_clones);
221 zap_cursor_retrieve(zc, za) == 0;
222 zap_cursor_advance(zc)) {
223 dsl_dataset_t *clone;
c434d880 224
30af21b0
PD
225 VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,
226 za->za_first_integer, tag, &clone));
227
228 if (clone->ds_dir->dd_origin_txg > mintxg) {
229 dsl_deadlist_remove_key(&clone->ds_deadlist,
230 mintxg, tx);
231
232 if (dsl_dataset_remap_deadlist_exists(clone)) {
233 dsl_deadlist_remove_key(
234 &clone->ds_remap_deadlist, mintxg, tx);
a1d477c2 235 }
30af21b0
PD
236
237 list_insert_head(stack, rck_alloc(clone));
238 } else {
239 dsl_dataset_rele(clone, tag);
13fe0198 240 }
13fe0198 241 }
30af21b0
PD
242 zap_cursor_fini(zc);
243
244 kmem_free(za, sizeof (zap_attribute_t));
245 kmem_free(zc, sizeof (zap_cursor_t));
246}
77831e17 247
30af21b0
PD
248void
249dsl_dir_remove_clones_key(dsl_dir_t *top_dd, uint64_t mintxg, dmu_tx_t *tx)
250{
251 list_t stack;
252
253 list_create(&stack, sizeof (remaining_clones_key_t),
254 offsetof(remaining_clones_key_t, rck_node));
255
256 dsl_dir_remove_clones_key_impl(top_dd, mintxg, tx, &stack, FTAG);
257 for (remaining_clones_key_t *rck = list_remove_head(&stack);
258 rck != NULL; rck = list_remove_head(&stack)) {
259 dsl_dataset_t *clone = rck->rck_clone;
260 dsl_dir_t *clone_dir = clone->ds_dir;
261
262 kmem_free(rck, sizeof (*rck));
263
264 dsl_dir_remove_clones_key_impl(clone_dir, mintxg, tx,
265 &stack, FTAG);
266 dsl_dataset_rele(clone, FTAG);
c434d880 267 }
30af21b0
PD
268
269 list_destroy(&stack);
13fe0198
MA
270}
271
a1d477c2
MA
272static void
273dsl_destroy_snapshot_handle_remaps(dsl_dataset_t *ds, dsl_dataset_t *ds_next,
274 dmu_tx_t *tx)
275{
276 dsl_pool_t *dp = ds->ds_dir->dd_pool;
277
278 /* Move blocks to be obsoleted to pool's obsolete list. */
279 if (dsl_dataset_remap_deadlist_exists(ds_next)) {
280 if (!bpobj_is_open(&dp->dp_obsolete_bpobj))
281 dsl_pool_create_obsolete_bpobj(dp, tx);
282
283 dsl_deadlist_move_bpobj(&ds_next->ds_remap_deadlist,
284 &dp->dp_obsolete_bpobj,
285 dsl_dataset_phys(ds)->ds_prev_snap_txg, tx);
286 }
287
288 /* Merge our deadlist into next's and free it. */
289 if (dsl_dataset_remap_deadlist_exists(ds)) {
290 uint64_t remap_deadlist_object =
291 dsl_dataset_get_remap_deadlist_object(ds);
292 ASSERT(remap_deadlist_object != 0);
293
294 mutex_enter(&ds_next->ds_remap_deadlist_lock);
295 if (!dsl_dataset_remap_deadlist_exists(ds_next))
296 dsl_dataset_create_remap_deadlist(ds_next, tx);
297 mutex_exit(&ds_next->ds_remap_deadlist_lock);
298
299 dsl_deadlist_merge(&ds_next->ds_remap_deadlist,
300 remap_deadlist_object, tx);
301 dsl_dataset_destroy_remap_deadlist(ds, tx);
302 }
303}
304
13fe0198
MA
305void
306dsl_destroy_snapshot_sync_impl(dsl_dataset_t *ds, boolean_t defer, dmu_tx_t *tx)
307{
13fe0198
MA
308 int after_branch_point = FALSE;
309 dsl_pool_t *dp = ds->ds_dir->dd_pool;
310 objset_t *mos = dp->dp_meta_objset;
311 dsl_dataset_t *ds_prev = NULL;
1c27024e 312 uint64_t obj;
13fe0198
MA
313
314 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
cc9bb3e5 315 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
d683ddbb 316 ASSERT3U(dsl_dataset_phys(ds)->ds_bp.blk_birth, <=, tx->tx_txg);
cc9bb3e5 317 rrw_exit(&ds->ds_bp_rwlock, FTAG);
424fd7c3 318 ASSERT(zfs_refcount_is_zero(&ds->ds_longholds));
13fe0198
MA
319
320 if (defer &&
d683ddbb
JG
321 (ds->ds_userrefs > 0 ||
322 dsl_dataset_phys(ds)->ds_num_children > 1)) {
13fe0198
MA
323 ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
324 dmu_buf_will_dirty(ds->ds_dbuf, tx);
d683ddbb 325 dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_DEFER_DESTROY;
17e21265
PD
326 if (zfs_snapshot_history_enabled) {
327 spa_history_log_internal_ds(ds, "defer_destroy", tx,
328 " ");
329 }
13fe0198
MA
330 return;
331 }
332
d683ddbb 333 ASSERT3U(dsl_dataset_phys(ds)->ds_num_children, <=, 1);
13fe0198 334
17e21265
PD
335 if (zfs_snapshot_history_enabled) {
336 /* We need to log before removing it from the namespace. */
337 spa_history_log_internal_ds(ds, "destroy", tx, " ");
338 }
13fe0198
MA
339
340 dsl_scan_ds_destroyed(ds, tx);
341
342 obj = ds->ds_object;
343
30af21b0
PD
344 boolean_t book_exists = dsl_bookmark_ds_destroyed(ds, tx);
345
1c27024e 346 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
d52d80b7
PD
347 if (dsl_dataset_feature_is_active(ds, f))
348 dsl_dataset_deactivate_feature(ds, f, tx);
f1512ee6 349 }
d683ddbb 350 if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
13fe0198
MA
351 ASSERT3P(ds->ds_prev, ==, NULL);
352 VERIFY0(dsl_dataset_hold_obj(dp,
d683ddbb 353 dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &ds_prev));
13fe0198 354 after_branch_point =
d683ddbb 355 (dsl_dataset_phys(ds_prev)->ds_next_snap_obj != obj);
13fe0198
MA
356
357 dmu_buf_will_dirty(ds_prev->ds_dbuf, tx);
358 if (after_branch_point &&
d683ddbb 359 dsl_dataset_phys(ds_prev)->ds_next_clones_obj != 0) {
13fe0198 360 dsl_dataset_remove_from_next_clones(ds_prev, obj, tx);
d683ddbb 361 if (dsl_dataset_phys(ds)->ds_next_snap_obj != 0) {
13fe0198 362 VERIFY0(zap_add_int(mos,
d683ddbb
JG
363 dsl_dataset_phys(ds_prev)->
364 ds_next_clones_obj,
365 dsl_dataset_phys(ds)->ds_next_snap_obj,
366 tx));
13fe0198
MA
367 }
368 }
369 if (!after_branch_point) {
d683ddbb
JG
370 dsl_dataset_phys(ds_prev)->ds_next_snap_obj =
371 dsl_dataset_phys(ds)->ds_next_snap_obj;
13fe0198
MA
372 }
373 }
374
1c27024e
DB
375 dsl_dataset_t *ds_next;
376 uint64_t old_unique;
377 uint64_t used = 0, comp = 0, uncomp = 0;
378
13fe0198 379 VERIFY0(dsl_dataset_hold_obj(dp,
d683ddbb
JG
380 dsl_dataset_phys(ds)->ds_next_snap_obj, FTAG, &ds_next));
381 ASSERT3U(dsl_dataset_phys(ds_next)->ds_prev_snap_obj, ==, obj);
13fe0198 382
d683ddbb 383 old_unique = dsl_dataset_phys(ds_next)->ds_unique_bytes;
13fe0198
MA
384
385 dmu_buf_will_dirty(ds_next->ds_dbuf, tx);
d683ddbb
JG
386 dsl_dataset_phys(ds_next)->ds_prev_snap_obj =
387 dsl_dataset_phys(ds)->ds_prev_snap_obj;
388 dsl_dataset_phys(ds_next)->ds_prev_snap_txg =
389 dsl_dataset_phys(ds)->ds_prev_snap_txg;
390 ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, ==,
391 ds_prev ? dsl_dataset_phys(ds_prev)->ds_creation_txg : 0);
13fe0198
MA
392
393 if (ds_next->ds_deadlist.dl_oldfmt) {
394 process_old_deadlist(ds, ds_prev, ds_next,
395 after_branch_point, tx);
396 } else {
397 /* Adjust prev's unique space. */
398 if (ds_prev && !after_branch_point) {
399 dsl_deadlist_space_range(&ds_next->ds_deadlist,
d683ddbb
JG
400 dsl_dataset_phys(ds_prev)->ds_prev_snap_txg,
401 dsl_dataset_phys(ds)->ds_prev_snap_txg,
13fe0198 402 &used, &comp, &uncomp);
d683ddbb 403 dsl_dataset_phys(ds_prev)->ds_unique_bytes += used;
13fe0198
MA
404 }
405
406 /* Adjust snapused. */
407 dsl_deadlist_space_range(&ds_next->ds_deadlist,
d683ddbb 408 dsl_dataset_phys(ds)->ds_prev_snap_txg, UINT64_MAX,
13fe0198
MA
409 &used, &comp, &uncomp);
410 dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
411 -used, -comp, -uncomp, tx);
412
413 /* Move blocks to be freed to pool's free list. */
414 dsl_deadlist_move_bpobj(&ds_next->ds_deadlist,
d683ddbb 415 &dp->dp_free_bpobj, dsl_dataset_phys(ds)->ds_prev_snap_txg,
13fe0198
MA
416 tx);
417 dsl_dir_diduse_space(tx->tx_pool->dp_free_dir,
418 DD_USED_HEAD, used, comp, uncomp, tx);
419
420 /* Merge our deadlist into next's and free it. */
421 dsl_deadlist_merge(&ds_next->ds_deadlist,
d683ddbb 422 dsl_dataset_phys(ds)->ds_deadlist_obj, tx);
325d288c
MA
423
424 /*
425 * We are done with the deadlist tree (generated/used
426 * by dsl_deadlist_move_bpobj() and dsl_deadlist_merge()).
427 * Discard it to save memory.
428 */
429 dsl_deadlist_discard_tree(&ds_next->ds_deadlist);
13fe0198 430 }
a1d477c2 431
13fe0198 432 dsl_deadlist_close(&ds->ds_deadlist);
d683ddbb 433 dsl_deadlist_free(mos, dsl_dataset_phys(ds)->ds_deadlist_obj, tx);
13fe0198 434 dmu_buf_will_dirty(ds->ds_dbuf, tx);
d683ddbb 435 dsl_dataset_phys(ds)->ds_deadlist_obj = 0;
13fe0198 436
a1d477c2
MA
437 dsl_destroy_snapshot_handle_remaps(ds, ds_next, tx);
438
30af21b0
PD
439 if (!book_exists) {
440 /* Collapse range in clone heads */
441 dsl_dir_remove_clones_key(ds->ds_dir,
442 dsl_dataset_phys(ds)->ds_creation_txg, tx);
443 }
13fe0198 444
0c66c32d 445 if (ds_next->ds_is_snapshot) {
13fe0198
MA
446 dsl_dataset_t *ds_nextnext;
447
448 /*
449 * Update next's unique to include blocks which
450 * were previously shared by only this snapshot
451 * and it. Those blocks will be born after the
452 * prev snap and before this snap, and will have
453 * died after the next snap and before the one
454 * after that (ie. be on the snap after next's
455 * deadlist).
456 */
457 VERIFY0(dsl_dataset_hold_obj(dp,
d683ddbb
JG
458 dsl_dataset_phys(ds_next)->ds_next_snap_obj,
459 FTAG, &ds_nextnext));
13fe0198 460 dsl_deadlist_space_range(&ds_nextnext->ds_deadlist,
d683ddbb
JG
461 dsl_dataset_phys(ds)->ds_prev_snap_txg,
462 dsl_dataset_phys(ds)->ds_creation_txg,
13fe0198 463 &used, &comp, &uncomp);
d683ddbb 464 dsl_dataset_phys(ds_next)->ds_unique_bytes += used;
13fe0198
MA
465 dsl_dataset_rele(ds_nextnext, FTAG);
466 ASSERT3P(ds_next->ds_prev, ==, NULL);
467
468 /* Collapse range in this head. */
1c27024e 469 dsl_dataset_t *hds;
13fe0198 470 VERIFY0(dsl_dataset_hold_obj(dp,
30af21b0
PD
471 dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj,
472 FTAG, &hds));
473 if (!book_exists) {
474 /* Collapse range in this head. */
475 dsl_deadlist_remove_key(&hds->ds_deadlist,
476 dsl_dataset_phys(ds)->ds_creation_txg, tx);
477 }
a1d477c2
MA
478 if (dsl_dataset_remap_deadlist_exists(hds)) {
479 dsl_deadlist_remove_key(&hds->ds_remap_deadlist,
480 dsl_dataset_phys(ds)->ds_creation_txg, tx);
481 }
13fe0198
MA
482 dsl_dataset_rele(hds, FTAG);
483
484 } else {
485 ASSERT3P(ds_next->ds_prev, ==, ds);
486 dsl_dataset_rele(ds_next->ds_prev, ds_next);
487 ds_next->ds_prev = NULL;
488 if (ds_prev) {
489 VERIFY0(dsl_dataset_hold_obj(dp,
d683ddbb 490 dsl_dataset_phys(ds)->ds_prev_snap_obj,
13fe0198
MA
491 ds_next, &ds_next->ds_prev));
492 }
493
494 dsl_dataset_recalc_head_uniq(ds_next);
495
496 /*
497 * Reduce the amount of our unconsumed refreservation
498 * being charged to our parent by the amount of
499 * new unique data we have gained.
500 */
501 if (old_unique < ds_next->ds_reserved) {
502 int64_t mrsdelta;
503 uint64_t new_unique =
d683ddbb 504 dsl_dataset_phys(ds_next)->ds_unique_bytes;
13fe0198
MA
505
506 ASSERT(old_unique <= new_unique);
507 mrsdelta = MIN(new_unique - old_unique,
508 ds_next->ds_reserved - old_unique);
509 dsl_dir_diduse_space(ds->ds_dir,
510 DD_USED_REFRSRV, -mrsdelta, 0, 0, tx);
511 }
512 }
513 dsl_dataset_rele(ds_next, FTAG);
514
515 /*
516 * This must be done after the dsl_traverse(), because it will
517 * re-open the objset.
518 */
519 if (ds->ds_objset) {
520 dmu_objset_evict(ds->ds_objset);
521 ds->ds_objset = NULL;
522 }
523
524 /* remove from snapshot namespace */
1c27024e 525 dsl_dataset_t *ds_head;
d683ddbb 526 ASSERT(dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0);
13fe0198 527 VERIFY0(dsl_dataset_hold_obj(dp,
d683ddbb 528 dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj, FTAG, &ds_head));
13fe0198
MA
529 VERIFY0(dsl_dataset_get_snapname(ds));
530#ifdef ZFS_DEBUG
531 {
532 uint64_t val;
a0bd735a 533 int err;
13fe0198
MA
534
535 err = dsl_dataset_snap_lookup(ds_head,
536 ds->ds_snapname, &val);
537 ASSERT0(err);
538 ASSERT3U(val, ==, obj);
539 }
540#endif
788eb90c 541 VERIFY0(dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx, B_TRUE));
13fe0198
MA
542 dsl_dataset_rele(ds_head, FTAG);
543
544 if (ds_prev != NULL)
545 dsl_dataset_rele(ds_prev, FTAG);
546
547 spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
548
d683ddbb 549 if (dsl_dataset_phys(ds)->ds_next_clones_obj != 0) {
2a8ba608 550 uint64_t count __maybe_unused;
13fe0198 551 ASSERT0(zap_count(mos,
d683ddbb
JG
552 dsl_dataset_phys(ds)->ds_next_clones_obj, &count) &&
553 count == 0);
13fe0198 554 VERIFY0(dmu_object_free(mos,
d683ddbb 555 dsl_dataset_phys(ds)->ds_next_clones_obj, tx));
13fe0198 556 }
d683ddbb
JG
557 if (dsl_dataset_phys(ds)->ds_props_obj != 0)
558 VERIFY0(zap_destroy(mos, dsl_dataset_phys(ds)->ds_props_obj,
559 tx));
560 if (dsl_dataset_phys(ds)->ds_userrefs_obj != 0)
561 VERIFY0(zap_destroy(mos, dsl_dataset_phys(ds)->ds_userrefs_obj,
562 tx));
13fe0198
MA
563 dsl_dir_rele(ds->ds_dir, ds);
564 ds->ds_dir = NULL;
fa86b5db 565 dmu_object_free_zapified(mos, obj, tx);
13fe0198
MA
566}
567
d99a0153 568void
13fe0198
MA
569dsl_destroy_snapshot_sync(void *arg, dmu_tx_t *tx)
570{
d99a0153
CW
571 dsl_destroy_snapshot_arg_t *ddsa = arg;
572 const char *dsname = ddsa->ddsa_name;
573 boolean_t defer = ddsa->ddsa_defer;
13fe0198 574
d99a0153
CW
575 dsl_pool_t *dp = dmu_tx_pool(tx);
576 dsl_dataset_t *ds;
13fe0198 577
d99a0153
CW
578 int error = dsl_dataset_hold(dp, dsname, FTAG, &ds);
579 if (error == ENOENT)
580 return;
581 ASSERT0(error);
582 dsl_destroy_snapshot_sync_impl(ds, defer, tx);
583 zvol_remove_minors(dp->dp_spa, dsname, B_TRUE);
584 dsl_dataset_rele(ds, FTAG);
13fe0198
MA
585}
586
587/*
588 * The semantics of this function are described in the comment above
589 * lzc_destroy_snaps(). To summarize:
590 *
591 * The snapshots must all be in the same pool.
592 *
593 * Snapshots that don't exist will be silently ignored (considered to be
594 * "already deleted").
595 *
596 * On success, all snaps will be destroyed and this will return 0.
597 * On failure, no snaps will be destroyed, the errlist will be filled in,
598 * and this will return an errno.
599 */
600int
601dsl_destroy_snapshots_nvl(nvlist_t *snaps, boolean_t defer,
602 nvlist_t *errlist)
603{
d99a0153 604 if (nvlist_next_nvpair(snaps, NULL) == NULL)
13fe0198
MA
605 return (0);
606
d99a0153
CW
607 /*
608 * lzc_destroy_snaps() is documented to take an nvlist whose
8d103d88 609 * values "don't matter". We need to convert that nvlist to
b2c5904a 610 * one that we know can be converted to LUA.
d99a0153 611 */
b2c5904a 612 nvlist_t *snaps_normalized = fnvlist_alloc();
d99a0153
CW
613 for (nvpair_t *pair = nvlist_next_nvpair(snaps, NULL);
614 pair != NULL; pair = nvlist_next_nvpair(snaps, pair)) {
615 fnvlist_add_boolean_value(snaps_normalized,
616 nvpair_name(pair), B_TRUE);
617 }
8d103d88 618
b2c5904a 619 nvlist_t *arg = fnvlist_alloc();
d99a0153
CW
620 fnvlist_add_nvlist(arg, "snaps", snaps_normalized);
621 fnvlist_free(snaps_normalized);
622 fnvlist_add_boolean_value(arg, "defer", defer);
623
b2c5904a 624 nvlist_t *wrapper = fnvlist_alloc();
d99a0153
CW
625 fnvlist_add_nvlist(wrapper, ZCP_ARG_ARGLIST, arg);
626 fnvlist_free(arg);
627
628 const char *program =
629 "arg = ...\n"
630 "snaps = arg['snaps']\n"
631 "defer = arg['defer']\n"
632 "errors = { }\n"
633 "has_errors = false\n"
634 "for snap, v in pairs(snaps) do\n"
635 " errno = zfs.check.destroy{snap, defer=defer}\n"
636 " zfs.debug('snap: ' .. snap .. ' errno: ' .. errno)\n"
637 " if errno == ENOENT then\n"
638 " snaps[snap] = nil\n"
639 " elseif errno ~= 0 then\n"
640 " errors[snap] = errno\n"
641 " has_errors = true\n"
642 " end\n"
643 "end\n"
644 "if has_errors then\n"
645 " return errors\n"
646 "end\n"
647 "for snap, v in pairs(snaps) do\n"
648 " errno = zfs.sync.destroy{snap, defer=defer}\n"
649 " assert(errno == 0)\n"
650 "end\n"
651 "return { }\n";
652
653 nvlist_t *result = fnvlist_alloc();
654 int error = zcp_eval(nvpair_name(nvlist_next_nvpair(snaps, NULL)),
655 program,
5b72a38d 656 B_TRUE,
d99a0153
CW
657 0,
658 zfs_lua_max_memlimit,
b2c5904a 659 fnvlist_lookup_nvpair(wrapper, ZCP_ARG_ARGLIST), result);
d99a0153 660 if (error != 0) {
d1807f16 661 const char *errorstr = NULL;
d99a0153
CW
662 (void) nvlist_lookup_string(result, ZCP_RET_ERROR, &errorstr);
663 if (errorstr != NULL) {
8e739b2c 664 zfs_dbgmsg("%s", errorstr);
d99a0153 665 }
70a5fc05
JL
666 fnvlist_free(wrapper);
667 fnvlist_free(result);
d99a0153
CW
668 return (error);
669 }
670 fnvlist_free(wrapper);
13fe0198 671
d99a0153
CW
672 /*
673 * lzc_destroy_snaps() is documented to fill the errlist with
e1cfd73f 674 * int32 values, so we need to convert the int64 values that are
d99a0153
CW
675 * returned from LUA.
676 */
677 int rv = 0;
678 nvlist_t *errlist_raw = fnvlist_lookup_nvlist(result, ZCP_RET_RETURN);
679 for (nvpair_t *pair = nvlist_next_nvpair(errlist_raw, NULL);
680 pair != NULL; pair = nvlist_next_nvpair(errlist_raw, pair)) {
681 int32_t val = (int32_t)fnvpair_value_int64(pair);
682 if (rv == 0)
683 rv = val;
684 fnvlist_add_int32(errlist, nvpair_name(pair), val);
685 }
686 fnvlist_free(result);
687 return (rv);
13fe0198
MA
688}
689
690int
691dsl_destroy_snapshot(const char *name, boolean_t defer)
692{
693 int error;
79c76d5b
BB
694 nvlist_t *nvl = fnvlist_alloc();
695 nvlist_t *errlist = fnvlist_alloc();
13fe0198
MA
696
697 fnvlist_add_boolean(nvl, name);
698 error = dsl_destroy_snapshots_nvl(nvl, defer, errlist);
699 fnvlist_free(errlist);
700 fnvlist_free(nvl);
701 return (error);
702}
703
704struct killarg {
705 dsl_dataset_t *ds;
706 dmu_tx_t *tx;
707};
708
13fe0198
MA
709static int
710kill_blkptr(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
5dbd68a3 711 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
13fe0198 712{
14e4e3cb 713 (void) spa, (void) dnp;
13fe0198
MA
714 struct killarg *ka = arg;
715 dmu_tx_t *tx = ka->tx;
716
30af21b0
PD
717 if (zb->zb_level == ZB_DNODE_LEVEL || BP_IS_HOLE(bp) ||
718 BP_IS_EMBEDDED(bp))
13fe0198
MA
719 return (0);
720
721 if (zb->zb_level == ZB_ZIL_LEVEL) {
722 ASSERT(zilog != NULL);
723 /*
724 * It's a block in the intent log. It has no
725 * accounting, so just free it.
726 */
727 dsl_free(ka->tx->tx_pool, ka->tx->tx_txg, bp);
728 } else {
729 ASSERT(zilog == NULL);
d683ddbb
JG
730 ASSERT3U(bp->blk_birth, >,
731 dsl_dataset_phys(ka->ds)->ds_prev_snap_txg);
13fe0198
MA
732 (void) dsl_dataset_block_kill(ka->ds, bp, tx, B_FALSE);
733 }
734
735 return (0);
736}
737
738static void
739old_synchronous_dataset_destroy(dsl_dataset_t *ds, dmu_tx_t *tx)
740{
741 struct killarg ka;
742
d87676a9
MA
743 spa_history_log_internal_ds(ds, "destroy", tx,
744 "(synchronous, mintxg=%llu)",
745 (long long)dsl_dataset_phys(ds)->ds_prev_snap_txg);
746
13fe0198
MA
747 /*
748 * Free everything that we point to (that's born after
749 * the previous snapshot, if we are a clone)
750 *
751 * NB: this should be very quick, because we already
752 * freed all the objects in open context.
753 */
754 ka.ds = ds;
755 ka.tx = tx;
756 VERIFY0(traverse_dataset(ds,
b5256303
TC
757 dsl_dataset_phys(ds)->ds_prev_snap_txg, TRAVERSE_POST |
758 TRAVERSE_NO_DECRYPT, kill_blkptr, &ka));
d683ddbb
JG
759 ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) ||
760 dsl_dataset_phys(ds)->ds_unique_bytes == 0);
13fe0198
MA
761}
762
13fe0198
MA
763int
764dsl_destroy_head_check_impl(dsl_dataset_t *ds, int expected_holds)
765{
766 int error;
767 uint64_t count;
768 objset_t *mos;
769
0c66c32d
JG
770 ASSERT(!ds->ds_is_snapshot);
771 if (ds->ds_is_snapshot)
2e528b49 772 return (SET_ERROR(EINVAL));
13fe0198 773
424fd7c3 774 if (zfs_refcount_count(&ds->ds_longholds) != expected_holds)
2e528b49 775 return (SET_ERROR(EBUSY));
13fe0198 776
5a42ef04
PD
777 ASSERT0(ds->ds_dir->dd_activity_waiters);
778
13fe0198
MA
779 mos = ds->ds_dir->dd_pool->dp_meta_objset;
780
781 /*
782 * Can't delete a head dataset if there are snapshots of it.
783 * (Except if the only snapshots are from the branch we cloned
784 * from.)
785 */
786 if (ds->ds_prev != NULL &&
d683ddbb 787 dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj == ds->ds_object)
2e528b49 788 return (SET_ERROR(EBUSY));
13fe0198
MA
789
790 /*
791 * Can't delete if there are children of this fs.
792 */
793 error = zap_count(mos,
d683ddbb 794 dsl_dir_phys(ds->ds_dir)->dd_child_dir_zapobj, &count);
13fe0198
MA
795 if (error != 0)
796 return (error);
797 if (count != 0)
2e528b49 798 return (SET_ERROR(EEXIST));
13fe0198
MA
799
800 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev) &&
d683ddbb 801 dsl_dataset_phys(ds->ds_prev)->ds_num_children == 2 &&
13fe0198
MA
802 ds->ds_prev->ds_userrefs == 0) {
803 /* We need to remove the origin snapshot as well. */
424fd7c3 804 if (!zfs_refcount_is_zero(&ds->ds_prev->ds_longholds))
2e528b49 805 return (SET_ERROR(EBUSY));
13fe0198
MA
806 }
807 return (0);
808}
809
d99a0153 810int
13fe0198
MA
811dsl_destroy_head_check(void *arg, dmu_tx_t *tx)
812{
813 dsl_destroy_head_arg_t *ddha = arg;
814 dsl_pool_t *dp = dmu_tx_pool(tx);
815 dsl_dataset_t *ds;
816 int error;
817
818 error = dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds);
819 if (error != 0)
820 return (error);
821
822 error = dsl_destroy_head_check_impl(ds, 0);
823 dsl_dataset_rele(ds, FTAG);
824 return (error);
825}
826
827static void
828dsl_dir_destroy_sync(uint64_t ddobj, dmu_tx_t *tx)
829{
830 dsl_dir_t *dd;
831 dsl_pool_t *dp = dmu_tx_pool(tx);
832 objset_t *mos = dp->dp_meta_objset;
833 dd_used_t t;
834
835 ASSERT(RRW_WRITE_HELD(&dmu_tx_pool(tx)->dp_config_rwlock));
836
837 VERIFY0(dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd));
838
d683ddbb 839 ASSERT0(dsl_dir_phys(dd)->dd_head_dataset_obj);
13fe0198 840
0f5f2386 841 /* Decrement the filesystem count for all parent filesystems. */
842 if (dd->dd_parent != NULL)
788eb90c
JJ
843 dsl_fs_ss_count_adjust(dd->dd_parent, -1,
844 DD_FIELD_FILESYSTEM_COUNT, tx);
845
13fe0198
MA
846 /*
847 * Remove our reservation. The impl() routine avoids setting the
848 * actual property, which would require the (already destroyed) ds.
849 */
850 dsl_dir_set_reservation_sync_impl(dd, 0, tx);
851
d683ddbb
JG
852 ASSERT0(dsl_dir_phys(dd)->dd_used_bytes);
853 ASSERT0(dsl_dir_phys(dd)->dd_reserved);
13fe0198 854 for (t = 0; t < DD_USED_NUM; t++)
d683ddbb 855 ASSERT0(dsl_dir_phys(dd)->dd_used_breakdown[t]);
13fe0198 856
b5256303
TC
857 if (dd->dd_crypto_obj != 0) {
858 dsl_crypto_key_destroy_sync(dd->dd_crypto_obj, tx);
859 (void) spa_keystore_unload_wkey_impl(dp->dp_spa, dd->dd_object);
860 }
861
d683ddbb
JG
862 VERIFY0(zap_destroy(mos, dsl_dir_phys(dd)->dd_child_dir_zapobj, tx));
863 VERIFY0(zap_destroy(mos, dsl_dir_phys(dd)->dd_props_zapobj, tx));
0aa5916a
MA
864 if (dsl_dir_phys(dd)->dd_clones != 0)
865 VERIFY0(zap_destroy(mos, dsl_dir_phys(dd)->dd_clones, tx));
d683ddbb 866 VERIFY0(dsl_deleg_destroy(mos, dsl_dir_phys(dd)->dd_deleg_zapobj, tx));
13fe0198 867 VERIFY0(zap_remove(mos,
d683ddbb
JG
868 dsl_dir_phys(dd->dd_parent)->dd_child_dir_zapobj,
869 dd->dd_myname, tx));
13fe0198
MA
870
871 dsl_dir_rele(dd, FTAG);
fa86b5db 872 dmu_object_free_zapified(mos, ddobj, tx);
13fe0198
MA
873}
874
37f03da8
SH
875static void
876dsl_clone_destroy_assert(dsl_dir_t *dd)
877{
878 uint64_t used, comp, uncomp;
879
880 ASSERT(dsl_dir_is_clone(dd));
881 dsl_deadlist_space(&dd->dd_livelist, &used, &comp, &uncomp);
882
883 ASSERT3U(dsl_dir_phys(dd)->dd_used_bytes, ==, used);
884 ASSERT3U(dsl_dir_phys(dd)->dd_compressed_bytes, ==, comp);
885 /*
886 * Greater than because we do not track embedded block pointers in
887 * the livelist
888 */
889 ASSERT3U(dsl_dir_phys(dd)->dd_uncompressed_bytes, >=, uncomp);
890
891 ASSERT(list_is_empty(&dd->dd_pending_allocs.bpl_list));
892 ASSERT(list_is_empty(&dd->dd_pending_frees.bpl_list));
893}
894
895/*
896 * Start the delete process for a clone. Free its zil, verify the space usage
897 * and queue the blkptrs for deletion by adding the livelist to the pool-wide
898 * delete queue.
899 */
900static void
901dsl_async_clone_destroy(dsl_dataset_t *ds, dmu_tx_t *tx)
902{
903 uint64_t zap_obj, to_delete, used, comp, uncomp;
904 objset_t *os;
905 dsl_dir_t *dd = ds->ds_dir;
906 dsl_pool_t *dp = dmu_tx_pool(tx);
907 objset_t *mos = dp->dp_meta_objset;
908 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
909 VERIFY0(dmu_objset_from_ds(ds, &os));
910
d87676a9
MA
911 uint64_t mintxg = 0;
912 dsl_deadlist_entry_t *dle = dsl_deadlist_first(&dd->dd_livelist);
913 if (dle != NULL)
914 mintxg = dle->dle_mintxg;
915
916 spa_history_log_internal_ds(ds, "destroy", tx,
917 "(livelist, mintxg=%llu)", (long long)mintxg);
918
37f03da8
SH
919 /* Check that the clone is in a correct state to be deleted */
920 dsl_clone_destroy_assert(dd);
921
922 /* Destroy the zil */
923 zil_destroy_sync(dmu_objset_zil(os), tx);
924
925 VERIFY0(zap_lookup(mos, dd->dd_object,
926 DD_FIELD_LIVELIST, sizeof (uint64_t), 1, &to_delete));
927 /* Initialize deleted_clones entry to track livelists to cleanup */
928 int error = zap_lookup(mos, DMU_POOL_DIRECTORY_OBJECT,
929 DMU_POOL_DELETED_CLONES, sizeof (uint64_t), 1, &zap_obj);
930 if (error == ENOENT) {
931 zap_obj = zap_create(mos, DMU_OTN_ZAP_METADATA,
932 DMU_OT_NONE, 0, tx);
933 VERIFY0(zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
934 DMU_POOL_DELETED_CLONES, sizeof (uint64_t), 1,
935 &(zap_obj), tx));
936 spa->spa_livelists_to_delete = zap_obj;
937 } else if (error != 0) {
938 zfs_panic_recover("zfs: error %d was returned while looking "
d87676a9 939 "up DMU_POOL_DELETED_CLONES in the zap", error);
37f03da8
SH
940 return;
941 }
942 VERIFY0(zap_add_int(mos, zap_obj, to_delete, tx));
943
944 /* Clone is no longer using space, now tracked by dp_free_dir */
945 dsl_deadlist_space(&dd->dd_livelist, &used, &comp, &uncomp);
946 dsl_dir_diduse_space(dd, DD_USED_HEAD,
947 -used, -comp, -dsl_dir_phys(dd)->dd_uncompressed_bytes,
948 tx);
949 dsl_dir_diduse_space(dp->dp_free_dir, DD_USED_HEAD,
950 used, comp, uncomp, tx);
951 dsl_dir_remove_livelist(dd, tx, B_FALSE);
952 zthr_wakeup(spa->spa_livelist_delete_zthr);
953}
954
955/*
956 * Move the bptree into the pool's list of trees to clean up, update space
957 * accounting information and destroy the zil.
958 */
47d57dbc 959static void
37f03da8
SH
960dsl_async_dataset_destroy(dsl_dataset_t *ds, dmu_tx_t *tx)
961{
962 uint64_t used, comp, uncomp;
963 objset_t *os;
964
965 VERIFY0(dmu_objset_from_ds(ds, &os));
966 dsl_pool_t *dp = dmu_tx_pool(tx);
967 objset_t *mos = dp->dp_meta_objset;
968
d87676a9
MA
969 spa_history_log_internal_ds(ds, "destroy", tx,
970 "(bptree, mintxg=%llu)",
971 (long long)dsl_dataset_phys(ds)->ds_prev_snap_txg);
972
37f03da8
SH
973 zil_destroy_sync(dmu_objset_zil(os), tx);
974
975 if (!spa_feature_is_active(dp->dp_spa,
976 SPA_FEATURE_ASYNC_DESTROY)) {
977 dsl_scan_t *scn = dp->dp_scan;
978 spa_feature_incr(dp->dp_spa, SPA_FEATURE_ASYNC_DESTROY,
979 tx);
980 dp->dp_bptree_obj = bptree_alloc(mos, tx);
981 VERIFY0(zap_add(mos,
982 DMU_POOL_DIRECTORY_OBJECT,
983 DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1,
984 &dp->dp_bptree_obj, tx));
985 ASSERT(!scn->scn_async_destroying);
986 scn->scn_async_destroying = B_TRUE;
987 }
988
989 used = dsl_dir_phys(ds->ds_dir)->dd_used_bytes;
990 comp = dsl_dir_phys(ds->ds_dir)->dd_compressed_bytes;
991 uncomp = dsl_dir_phys(ds->ds_dir)->dd_uncompressed_bytes;
992
993 ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) ||
994 dsl_dataset_phys(ds)->ds_unique_bytes == used);
995
996 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
997 bptree_add(mos, dp->dp_bptree_obj,
998 &dsl_dataset_phys(ds)->ds_bp,
999 dsl_dataset_phys(ds)->ds_prev_snap_txg,
1000 used, comp, uncomp, tx);
1001 rrw_exit(&ds->ds_bp_rwlock, FTAG);
1002 dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
1003 -used, -comp, -uncomp, tx);
1004 dsl_dir_diduse_space(dp->dp_free_dir, DD_USED_HEAD,
1005 used, comp, uncomp, tx);
1006}
1007
13fe0198
MA
1008void
1009dsl_destroy_head_sync_impl(dsl_dataset_t *ds, dmu_tx_t *tx)
1010{
1011 dsl_pool_t *dp = dmu_tx_pool(tx);
1012 objset_t *mos = dp->dp_meta_objset;
1013 uint64_t obj, ddobj, prevobj = 0;
1014 boolean_t rmorigin;
13fe0198 1015
d683ddbb 1016 ASSERT3U(dsl_dataset_phys(ds)->ds_num_children, <=, 1);
13fe0198 1017 ASSERT(ds->ds_prev == NULL ||
d683ddbb 1018 dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj != ds->ds_object);
cc9bb3e5 1019 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
d683ddbb 1020 ASSERT3U(dsl_dataset_phys(ds)->ds_bp.blk_birth, <=, tx->tx_txg);
cc9bb3e5 1021 rrw_exit(&ds->ds_bp_rwlock, FTAG);
13fe0198
MA
1022 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
1023
5a42ef04
PD
1024 dsl_dir_cancel_waiters(ds->ds_dir);
1025
13fe0198
MA
1026 rmorigin = (dsl_dir_is_clone(ds->ds_dir) &&
1027 DS_IS_DEFER_DESTROY(ds->ds_prev) &&
d683ddbb 1028 dsl_dataset_phys(ds->ds_prev)->ds_num_children == 2 &&
13fe0198
MA
1029 ds->ds_prev->ds_userrefs == 0);
1030
9b67f605 1031 /* Remove our reservation. */
13fe0198
MA
1032 if (ds->ds_reserved != 0) {
1033 dsl_dataset_set_refreservation_sync_impl(ds,
1034 (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED),
1035 0, tx);
1036 ASSERT0(ds->ds_reserved);
1037 }
1038
241b5415 1039 obj = ds->ds_object;
f1512ee6 1040
1c27024e 1041 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
d52d80b7
PD
1042 if (dsl_dataset_feature_is_active(ds, f))
1043 dsl_dataset_deactivate_feature(ds, f, tx);
241b5415 1044 }
13fe0198 1045
241b5415 1046 dsl_scan_ds_destroyed(ds, tx);
13fe0198 1047
d683ddbb 1048 if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
13fe0198
MA
1049 /* This is a clone */
1050 ASSERT(ds->ds_prev != NULL);
d683ddbb
JG
1051 ASSERT3U(dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj, !=,
1052 obj);
1053 ASSERT0(dsl_dataset_phys(ds)->ds_next_snap_obj);
13fe0198
MA
1054
1055 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
d683ddbb 1056 if (dsl_dataset_phys(ds->ds_prev)->ds_next_clones_obj != 0) {
13fe0198
MA
1057 dsl_dataset_remove_from_next_clones(ds->ds_prev,
1058 obj, tx);
1059 }
1060
d683ddbb
JG
1061 ASSERT3U(dsl_dataset_phys(ds->ds_prev)->ds_num_children, >, 1);
1062 dsl_dataset_phys(ds->ds_prev)->ds_num_children--;
13fe0198
MA
1063 }
1064
13fe0198 1065 /*
37f03da8 1066 * Destroy the deadlist. Unless it's a clone, the
a1d477c2
MA
1067 * deadlist should be empty since the dataset has no snapshots.
1068 * (If it's a clone, it's safe to ignore the deadlist contents
1069 * since they are still referenced by the origin snapshot.)
13fe0198
MA
1070 */
1071 dsl_deadlist_close(&ds->ds_deadlist);
d683ddbb 1072 dsl_deadlist_free(mos, dsl_dataset_phys(ds)->ds_deadlist_obj, tx);
13fe0198 1073 dmu_buf_will_dirty(ds->ds_dbuf, tx);
d683ddbb 1074 dsl_dataset_phys(ds)->ds_deadlist_obj = 0;
13fe0198 1075
a1d477c2
MA
1076 if (dsl_dataset_remap_deadlist_exists(ds))
1077 dsl_dataset_destroy_remap_deadlist(ds, tx);
1078
37f03da8
SH
1079 /*
1080 * Each destroy is responsible for both destroying (enqueuing
1081 * to be destroyed) the blkptrs comprising the dataset as well as
1082 * those belonging to the zil.
1083 */
1084 if (dsl_deadlist_is_open(&ds->ds_dir->dd_livelist)) {
1085 dsl_async_clone_destroy(ds, tx);
1086 } else if (spa_feature_is_enabled(dp->dp_spa,
1087 SPA_FEATURE_ASYNC_DESTROY)) {
1088 dsl_async_dataset_destroy(ds, tx);
13fe0198 1089 } else {
37f03da8 1090 old_synchronous_dataset_destroy(ds, tx);
13fe0198
MA
1091 }
1092
1093 if (ds->ds_prev != NULL) {
1094 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
1095 VERIFY0(zap_remove_int(mos,
d683ddbb 1096 dsl_dir_phys(ds->ds_prev->ds_dir)->dd_clones,
13fe0198
MA
1097 ds->ds_object, tx));
1098 }
1099 prevobj = ds->ds_prev->ds_object;
1100 dsl_dataset_rele(ds->ds_prev, ds);
1101 ds->ds_prev = NULL;
1102 }
1103
1104 /*
1105 * This must be done after the dsl_traverse(), because it will
1106 * re-open the objset.
1107 */
1108 if (ds->ds_objset) {
1109 dmu_objset_evict(ds->ds_objset);
1110 ds->ds_objset = NULL;
1111 }
1112
1113 /* Erase the link in the dir */
1114 dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
d683ddbb 1115 dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj = 0;
13fe0198 1116 ddobj = ds->ds_dir->dd_object;
d683ddbb
JG
1117 ASSERT(dsl_dataset_phys(ds)->ds_snapnames_zapobj != 0);
1118 VERIFY0(zap_destroy(mos,
1119 dsl_dataset_phys(ds)->ds_snapnames_zapobj, tx));
13fe0198 1120
30af21b0
PD
1121 if (ds->ds_bookmarks_obj != 0) {
1122 void *cookie = NULL;
1123 dsl_bookmark_node_t *dbn;
1124
1125 while ((dbn = avl_destroy_nodes(&ds->ds_bookmarks, &cookie)) !=
1126 NULL) {
1127 if (dbn->dbn_phys.zbm_redaction_obj != 0) {
bee9cfb8
PD
1128 dnode_t *rl;
1129 VERIFY0(dnode_hold(mos,
1130 dbn->dbn_phys.zbm_redaction_obj, FTAG,
1131 &rl));
1132 if (rl->dn_have_spill) {
1133 spa_feature_decr(dmu_objset_spa(mos),
1134 SPA_FEATURE_REDACTION_LIST_SPILL,
1135 tx);
1136 }
1137 dnode_rele(rl, FTAG);
30af21b0
PD
1138 VERIFY0(dmu_object_free(mos,
1139 dbn->dbn_phys.zbm_redaction_obj, tx));
1140 spa_feature_decr(dmu_objset_spa(mos),
1141 SPA_FEATURE_REDACTION_BOOKMARKS, tx);
1142 }
1143 if (dbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN) {
1144 spa_feature_decr(dmu_objset_spa(mos),
1145 SPA_FEATURE_BOOKMARK_WRITTEN, tx);
1146 }
1147 spa_strfree(dbn->dbn_name);
1148 mutex_destroy(&dbn->dbn_lock);
1149 kmem_free(dbn, sizeof (*dbn));
1150 }
1151 avl_destroy(&ds->ds_bookmarks);
1152 VERIFY0(zap_destroy(mos, ds->ds_bookmarks_obj, tx));
da536844
MA
1153 spa_feature_decr(dp->dp_spa, SPA_FEATURE_BOOKMARKS, tx);
1154 }
1155
13fe0198
MA
1156 spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
1157
d683ddbb
JG
1158 ASSERT0(dsl_dataset_phys(ds)->ds_next_clones_obj);
1159 ASSERT0(dsl_dataset_phys(ds)->ds_props_obj);
1160 ASSERT0(dsl_dataset_phys(ds)->ds_userrefs_obj);
13fe0198
MA
1161 dsl_dir_rele(ds->ds_dir, ds);
1162 ds->ds_dir = NULL;
fa86b5db 1163 dmu_object_free_zapified(mos, obj, tx);
13fe0198
MA
1164
1165 dsl_dir_destroy_sync(ddobj, tx);
1166
1167 if (rmorigin) {
1168 dsl_dataset_t *prev;
1169 VERIFY0(dsl_dataset_hold_obj(dp, prevobj, FTAG, &prev));
1170 dsl_destroy_snapshot_sync_impl(prev, B_FALSE, tx);
1171 dsl_dataset_rele(prev, FTAG);
1172 }
0409d332
GA
1173 /* Delete errlog. */
1174 if (spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_HEAD_ERRLOG))
1175 spa_delete_dataset_errlog(dp->dp_spa, ds->ds_object, tx);
13fe0198
MA
1176}
1177
d99a0153 1178void
13fe0198
MA
1179dsl_destroy_head_sync(void *arg, dmu_tx_t *tx)
1180{
1181 dsl_destroy_head_arg_t *ddha = arg;
1182 dsl_pool_t *dp = dmu_tx_pool(tx);
1183 dsl_dataset_t *ds;
1184
1185 VERIFY0(dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds));
1186 dsl_destroy_head_sync_impl(ds, tx);
a0bd735a 1187 zvol_remove_minors(dp->dp_spa, ddha->ddha_name, B_TRUE);
13fe0198
MA
1188 dsl_dataset_rele(ds, FTAG);
1189}
1190
1191static void
1192dsl_destroy_head_begin_sync(void *arg, dmu_tx_t *tx)
1193{
1194 dsl_destroy_head_arg_t *ddha = arg;
1195 dsl_pool_t *dp = dmu_tx_pool(tx);
1196 dsl_dataset_t *ds;
1197
1198 VERIFY0(dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds));
1199
1200 /* Mark it as inconsistent on-disk, in case we crash */
1201 dmu_buf_will_dirty(ds->ds_dbuf, tx);
d683ddbb 1202 dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_INCONSISTENT;
13fe0198 1203
74756182 1204 spa_history_log_internal_ds(ds, "destroy begin", tx, " ");
13fe0198
MA
1205 dsl_dataset_rele(ds, FTAG);
1206}
1207
1208int
1209dsl_destroy_head(const char *name)
1210{
1211 dsl_destroy_head_arg_t ddha;
1212 int error;
1213 spa_t *spa;
1214 boolean_t isenabled;
1215
1216#ifdef _KERNEL
1217 zfs_destroy_unmount_origin(name);
1218#endif
1219
1220 error = spa_open(name, &spa, FTAG);
1221 if (error != 0)
1222 return (error);
fa86b5db 1223 isenabled = spa_feature_is_enabled(spa, SPA_FEATURE_ASYNC_DESTROY);
13fe0198
MA
1224 spa_close(spa, FTAG);
1225
1226 ddha.ddha_name = name;
1227
1228 if (!isenabled) {
1229 objset_t *os;
1230
1231 error = dsl_sync_task(name, dsl_destroy_head_check,
3d45fdd6 1232 dsl_destroy_head_begin_sync, &ddha,
d2734cce 1233 0, ZFS_SPACE_CHECK_DESTROY);
13fe0198
MA
1234 if (error != 0)
1235 return (error);
1236
1237 /*
1238 * Head deletion is processed in one txg on old pools;
1239 * remove the objects from open context so that the txg sync
9949b856
TC
1240 * is not too long. This optimization can only work for
1241 * encrypted datasets if the wrapping key is loaded.
13fe0198 1242 */
9949b856 1243 error = dmu_objset_own(name, DMU_OST_ANY, B_FALSE, B_TRUE,
b5256303 1244 FTAG, &os);
13fe0198 1245 if (error == 0) {
13fe0198 1246 uint64_t prev_snap_txg =
d683ddbb
JG
1247 dsl_dataset_phys(dmu_objset_ds(os))->
1248 ds_prev_snap_txg;
1c27024e 1249 for (uint64_t obj = 0; error == 0;
13fe0198
MA
1250 error = dmu_object_next(os, &obj, FALSE,
1251 prev_snap_txg))
b663a23d 1252 (void) dmu_free_long_object(os, obj);
13fe0198
MA
1253 /* sync out all frees */
1254 txg_wait_synced(dmu_objset_pool(os), 0);
9949b856 1255 dmu_objset_disown(os, B_TRUE, FTAG);
13fe0198
MA
1256 }
1257 }
1258
1259 return (dsl_sync_task(name, dsl_destroy_head_check,
d2734cce 1260 dsl_destroy_head_sync, &ddha, 0, ZFS_SPACE_CHECK_DESTROY));
13fe0198
MA
1261}
1262
1263/*
1264 * Note, this function is used as the callback for dmu_objset_find(). We
1265 * always return 0 so that we will continue to find and process
1266 * inconsistent datasets, even if we encounter an error trying to
1267 * process one of them.
1268 */
13fe0198
MA
1269int
1270dsl_destroy_inconsistent(const char *dsname, void *arg)
1271{
14e4e3cb 1272 (void) arg;
13fe0198
MA
1273 objset_t *os;
1274
1275 if (dmu_objset_hold(dsname, FTAG, &os) == 0) {
47dfff3b
MA
1276 boolean_t need_destroy = DS_IS_INCONSISTENT(dmu_objset_ds(os));
1277
1278 /*
1279 * If the dataset is inconsistent because a resumable receive
1280 * has failed, then do not destroy it.
1281 */
1282 if (dsl_dataset_has_resume_receive_state(dmu_objset_ds(os)))
1283 need_destroy = B_FALSE;
1284
13fe0198 1285 dmu_objset_rele(os, FTAG);
47dfff3b 1286 if (need_destroy)
13fe0198
MA
1287 (void) dsl_destroy_head(dsname);
1288 }
1289 return (0);
1290}
1291
1292
93ce2b4c 1293#if defined(_KERNEL)
13fe0198
MA
1294EXPORT_SYMBOL(dsl_destroy_head);
1295EXPORT_SYMBOL(dsl_destroy_head_sync_impl);
1296EXPORT_SYMBOL(dsl_dataset_user_hold_check_one);
1297EXPORT_SYMBOL(dsl_destroy_snapshot_sync_impl);
1298EXPORT_SYMBOL(dsl_destroy_inconsistent);
1299EXPORT_SYMBOL(dsl_dataset_user_release_tmp);
1300EXPORT_SYMBOL(dsl_destroy_head_check_impl);
1301#endif