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