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