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