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.
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.
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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2013 by Delphix. All rights reserved.
24 * Copyright (c) 2013 Steven Hartland. All rights reserved.
27 #include <sys/zfs_context.h>
28 #include <sys/dsl_userhold.h>
29 #include <sys/dsl_dataset.h>
30 #include <sys/dsl_synctask.h>
31 #include <sys/dmu_tx.h>
32 #include <sys/dsl_pool.h>
33 #include <sys/dsl_dir.h>
34 #include <sys/dmu_traverse.h>
35 #include <sys/dsl_scan.h>
36 #include <sys/dmu_objset.h>
38 #include <sys/zfeature.h>
39 #include <sys/zfs_ioctl.h>
40 #include <sys/dsl_deleg.h>
42 typedef struct dmu_snapshots_destroy_arg {
44 nvlist_t *dsda_successful_snaps;
46 nvlist_t *dsda_errlist;
47 } dmu_snapshots_destroy_arg_t;
50 dsl_destroy_snapshot_check_impl(dsl_dataset_t *ds, boolean_t defer)
52 if (!dsl_dataset_is_snapshot(ds))
53 return (SET_ERROR(EINVAL));
55 if (dsl_dataset_long_held(ds))
56 return (SET_ERROR(EBUSY));
59 * Only allow deferred destroy on pools that support it.
60 * NOTE: deferred destroy is only supported on snapshots.
63 if (spa_version(ds->ds_dir->dd_pool->dp_spa) <
65 return (SET_ERROR(ENOTSUP));
70 * If this snapshot has an elevated user reference count,
71 * we can't destroy it yet.
73 if (ds->ds_userrefs > 0)
74 return (SET_ERROR(EBUSY));
77 * Can't delete a branch point.
79 if (ds->ds_phys->ds_num_children > 1)
80 return (SET_ERROR(EEXIST));
86 dsl_destroy_snapshot_check(void *arg, dmu_tx_t *tx)
88 dmu_snapshots_destroy_arg_t *dsda = arg;
89 dsl_pool_t *dp = dmu_tx_pool(tx);
93 if (!dmu_tx_is_syncing(tx))
96 for (pair = nvlist_next_nvpair(dsda->dsda_snaps, NULL);
97 pair != NULL; pair = nvlist_next_nvpair(dsda->dsda_snaps, pair)) {
100 error = dsl_dataset_hold(dp, nvpair_name(pair),
104 * If the snapshot does not exist, silently ignore it
105 * (it's "already destroyed").
111 error = dsl_destroy_snapshot_check_impl(ds,
113 dsl_dataset_rele(ds, FTAG);
117 fnvlist_add_boolean(dsda->dsda_successful_snaps,
120 fnvlist_add_int32(dsda->dsda_errlist,
121 nvpair_name(pair), error);
125 pair = nvlist_next_nvpair(dsda->dsda_errlist, NULL);
127 return (fnvpair_value_int32(pair));
132 struct process_old_arg {
134 dsl_dataset_t *ds_prev;
135 boolean_t after_branch_point;
137 uint64_t used, comp, uncomp;
141 process_old_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
143 struct process_old_arg *poa = arg;
144 dsl_pool_t *dp = poa->ds->ds_dir->dd_pool;
146 if (bp->blk_birth <= poa->ds->ds_phys->ds_prev_snap_txg) {
147 dsl_deadlist_insert(&poa->ds->ds_deadlist, bp, tx);
148 if (poa->ds_prev && !poa->after_branch_point &&
150 poa->ds_prev->ds_phys->ds_prev_snap_txg) {
151 poa->ds_prev->ds_phys->ds_unique_bytes +=
152 bp_get_dsize_sync(dp->dp_spa, bp);
155 poa->used += bp_get_dsize_sync(dp->dp_spa, bp);
156 poa->comp += BP_GET_PSIZE(bp);
157 poa->uncomp += BP_GET_UCSIZE(bp);
158 dsl_free_sync(poa->pio, dp, tx->tx_txg, bp);
164 process_old_deadlist(dsl_dataset_t *ds, dsl_dataset_t *ds_prev,
165 dsl_dataset_t *ds_next, boolean_t after_branch_point, dmu_tx_t *tx)
167 struct process_old_arg poa = { 0 };
168 dsl_pool_t *dp = ds->ds_dir->dd_pool;
169 objset_t *mos = dp->dp_meta_objset;
170 uint64_t deadlist_obj;
172 ASSERT(ds->ds_deadlist.dl_oldfmt);
173 ASSERT(ds_next->ds_deadlist.dl_oldfmt);
176 poa.ds_prev = ds_prev;
177 poa.after_branch_point = after_branch_point;
178 poa.pio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
179 VERIFY0(bpobj_iterate(&ds_next->ds_deadlist.dl_bpobj,
180 process_old_cb, &poa, tx));
181 VERIFY0(zio_wait(poa.pio));
182 ASSERT3U(poa.used, ==, ds->ds_phys->ds_unique_bytes);
184 /* change snapused */
185 dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
186 -poa.used, -poa.comp, -poa.uncomp, tx);
188 /* swap next's deadlist to our deadlist */
189 dsl_deadlist_close(&ds->ds_deadlist);
190 dsl_deadlist_close(&ds_next->ds_deadlist);
191 deadlist_obj = ds->ds_phys->ds_deadlist_obj;
192 ds->ds_phys->ds_deadlist_obj = ds_next->ds_phys->ds_deadlist_obj;
193 ds_next->ds_phys->ds_deadlist_obj = deadlist_obj;
194 dsl_deadlist_open(&ds->ds_deadlist, mos, ds->ds_phys->ds_deadlist_obj);
195 dsl_deadlist_open(&ds_next->ds_deadlist, mos,
196 ds_next->ds_phys->ds_deadlist_obj);
200 dsl_dataset_remove_clones_key(dsl_dataset_t *ds, uint64_t mintxg, dmu_tx_t *tx)
202 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
207 * If it is the old version, dd_clones doesn't exist so we can't
208 * find the clones, but dsl_deadlist_remove_key() is a no-op so it
211 if (ds->ds_dir->dd_phys->dd_clones == 0)
214 for (zap_cursor_init(&zc, mos, ds->ds_dir->dd_phys->dd_clones);
215 zap_cursor_retrieve(&zc, &za) == 0;
216 zap_cursor_advance(&zc)) {
217 dsl_dataset_t *clone;
219 VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
220 za.za_first_integer, FTAG, &clone));
221 if (clone->ds_dir->dd_origin_txg > mintxg) {
222 dsl_deadlist_remove_key(&clone->ds_deadlist,
224 dsl_dataset_remove_clones_key(clone, mintxg, tx);
226 dsl_dataset_rele(clone, FTAG);
228 zap_cursor_fini(&zc);
232 dsl_destroy_snapshot_sync_impl(dsl_dataset_t *ds, boolean_t defer, dmu_tx_t *tx)
235 int after_branch_point = FALSE;
236 dsl_pool_t *dp = ds->ds_dir->dd_pool;
237 objset_t *mos = dp->dp_meta_objset;
238 dsl_dataset_t *ds_prev = NULL;
241 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
242 ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg);
243 ASSERT(refcount_is_zero(&ds->ds_longholds));
246 (ds->ds_userrefs > 0 || ds->ds_phys->ds_num_children > 1)) {
247 ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
248 dmu_buf_will_dirty(ds->ds_dbuf, tx);
249 ds->ds_phys->ds_flags |= DS_FLAG_DEFER_DESTROY;
250 spa_history_log_internal_ds(ds, "defer_destroy", tx, "");
254 ASSERT3U(ds->ds_phys->ds_num_children, <=, 1);
256 /* We need to log before removing it from the namespace. */
257 spa_history_log_internal_ds(ds, "destroy", tx, "");
259 dsl_scan_ds_destroyed(ds, tx);
263 if (ds->ds_phys->ds_prev_snap_obj != 0) {
264 ASSERT3P(ds->ds_prev, ==, NULL);
265 VERIFY0(dsl_dataset_hold_obj(dp,
266 ds->ds_phys->ds_prev_snap_obj, FTAG, &ds_prev));
268 (ds_prev->ds_phys->ds_next_snap_obj != obj);
270 dmu_buf_will_dirty(ds_prev->ds_dbuf, tx);
271 if (after_branch_point &&
272 ds_prev->ds_phys->ds_next_clones_obj != 0) {
273 dsl_dataset_remove_from_next_clones(ds_prev, obj, tx);
274 if (ds->ds_phys->ds_next_snap_obj != 0) {
275 VERIFY0(zap_add_int(mos,
276 ds_prev->ds_phys->ds_next_clones_obj,
277 ds->ds_phys->ds_next_snap_obj, tx));
280 if (!after_branch_point) {
281 ds_prev->ds_phys->ds_next_snap_obj =
282 ds->ds_phys->ds_next_snap_obj;
286 dsl_dataset_t *ds_next;
288 uint64_t used = 0, comp = 0, uncomp = 0;
290 VERIFY0(dsl_dataset_hold_obj(dp,
291 ds->ds_phys->ds_next_snap_obj, FTAG, &ds_next));
292 ASSERT3U(ds_next->ds_phys->ds_prev_snap_obj, ==, obj);
294 old_unique = ds_next->ds_phys->ds_unique_bytes;
296 dmu_buf_will_dirty(ds_next->ds_dbuf, tx);
297 ds_next->ds_phys->ds_prev_snap_obj =
298 ds->ds_phys->ds_prev_snap_obj;
299 ds_next->ds_phys->ds_prev_snap_txg =
300 ds->ds_phys->ds_prev_snap_txg;
301 ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
302 ds_prev ? ds_prev->ds_phys->ds_creation_txg : 0);
304 if (ds_next->ds_deadlist.dl_oldfmt) {
305 process_old_deadlist(ds, ds_prev, ds_next,
306 after_branch_point, tx);
308 /* Adjust prev's unique space. */
309 if (ds_prev && !after_branch_point) {
310 dsl_deadlist_space_range(&ds_next->ds_deadlist,
311 ds_prev->ds_phys->ds_prev_snap_txg,
312 ds->ds_phys->ds_prev_snap_txg,
313 &used, &comp, &uncomp);
314 ds_prev->ds_phys->ds_unique_bytes += used;
317 /* Adjust snapused. */
318 dsl_deadlist_space_range(&ds_next->ds_deadlist,
319 ds->ds_phys->ds_prev_snap_txg, UINT64_MAX,
320 &used, &comp, &uncomp);
321 dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
322 -used, -comp, -uncomp, tx);
324 /* Move blocks to be freed to pool's free list. */
325 dsl_deadlist_move_bpobj(&ds_next->ds_deadlist,
326 &dp->dp_free_bpobj, ds->ds_phys->ds_prev_snap_txg,
328 dsl_dir_diduse_space(tx->tx_pool->dp_free_dir,
329 DD_USED_HEAD, used, comp, uncomp, tx);
331 /* Merge our deadlist into next's and free it. */
332 dsl_deadlist_merge(&ds_next->ds_deadlist,
333 ds->ds_phys->ds_deadlist_obj, tx);
335 dsl_deadlist_close(&ds->ds_deadlist);
336 dsl_deadlist_free(mos, ds->ds_phys->ds_deadlist_obj, tx);
337 dmu_buf_will_dirty(ds->ds_dbuf, tx);
338 ds->ds_phys->ds_deadlist_obj = 0;
340 /* Collapse range in clone heads */
341 dsl_dataset_remove_clones_key(ds,
342 ds->ds_phys->ds_creation_txg, tx);
344 if (dsl_dataset_is_snapshot(ds_next)) {
345 dsl_dataset_t *ds_nextnext;
348 * Update next's unique to include blocks which
349 * were previously shared by only this snapshot
350 * and it. Those blocks will be born after the
351 * prev snap and before this snap, and will have
352 * died after the next snap and before the one
353 * after that (ie. be on the snap after next's
356 VERIFY0(dsl_dataset_hold_obj(dp,
357 ds_next->ds_phys->ds_next_snap_obj, FTAG, &ds_nextnext));
358 dsl_deadlist_space_range(&ds_nextnext->ds_deadlist,
359 ds->ds_phys->ds_prev_snap_txg,
360 ds->ds_phys->ds_creation_txg,
361 &used, &comp, &uncomp);
362 ds_next->ds_phys->ds_unique_bytes += used;
363 dsl_dataset_rele(ds_nextnext, FTAG);
364 ASSERT3P(ds_next->ds_prev, ==, NULL);
366 /* Collapse range in this head. */
368 VERIFY0(dsl_dataset_hold_obj(dp,
369 ds->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &hds));
370 dsl_deadlist_remove_key(&hds->ds_deadlist,
371 ds->ds_phys->ds_creation_txg, tx);
372 dsl_dataset_rele(hds, FTAG);
375 ASSERT3P(ds_next->ds_prev, ==, ds);
376 dsl_dataset_rele(ds_next->ds_prev, ds_next);
377 ds_next->ds_prev = NULL;
379 VERIFY0(dsl_dataset_hold_obj(dp,
380 ds->ds_phys->ds_prev_snap_obj,
381 ds_next, &ds_next->ds_prev));
384 dsl_dataset_recalc_head_uniq(ds_next);
387 * Reduce the amount of our unconsumed refreservation
388 * being charged to our parent by the amount of
389 * new unique data we have gained.
391 if (old_unique < ds_next->ds_reserved) {
393 uint64_t new_unique =
394 ds_next->ds_phys->ds_unique_bytes;
396 ASSERT(old_unique <= new_unique);
397 mrsdelta = MIN(new_unique - old_unique,
398 ds_next->ds_reserved - old_unique);
399 dsl_dir_diduse_space(ds->ds_dir,
400 DD_USED_REFRSRV, -mrsdelta, 0, 0, tx);
403 dsl_dataset_rele(ds_next, FTAG);
406 * This must be done after the dsl_traverse(), because it will
407 * re-open the objset.
410 dmu_objset_evict(ds->ds_objset);
411 ds->ds_objset = NULL;
414 /* remove from snapshot namespace */
415 dsl_dataset_t *ds_head;
416 ASSERT(ds->ds_phys->ds_snapnames_zapobj == 0);
417 VERIFY0(dsl_dataset_hold_obj(dp,
418 ds->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ds_head));
419 VERIFY0(dsl_dataset_get_snapname(ds));
424 err = dsl_dataset_snap_lookup(ds_head,
425 ds->ds_snapname, &val);
427 ASSERT3U(val, ==, obj);
430 VERIFY0(dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx));
431 dsl_dataset_rele(ds_head, FTAG);
434 dsl_dataset_rele(ds_prev, FTAG);
436 spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
438 if (ds->ds_phys->ds_next_clones_obj != 0) {
440 ASSERT0(zap_count(mos,
441 ds->ds_phys->ds_next_clones_obj, &count) && count == 0);
442 VERIFY0(dmu_object_free(mos,
443 ds->ds_phys->ds_next_clones_obj, tx));
445 if (ds->ds_phys->ds_props_obj != 0)
446 VERIFY0(zap_destroy(mos, ds->ds_phys->ds_props_obj, tx));
447 if (ds->ds_phys->ds_userrefs_obj != 0)
448 VERIFY0(zap_destroy(mos, ds->ds_phys->ds_userrefs_obj, tx));
449 dsl_dir_rele(ds->ds_dir, ds);
451 VERIFY0(dmu_object_free(mos, obj, tx));
455 dsl_destroy_snapshot_sync(void *arg, dmu_tx_t *tx)
457 dmu_snapshots_destroy_arg_t *dsda = arg;
458 dsl_pool_t *dp = dmu_tx_pool(tx);
461 for (pair = nvlist_next_nvpair(dsda->dsda_successful_snaps, NULL);
463 pair = nvlist_next_nvpair(dsda->dsda_successful_snaps, pair)) {
466 VERIFY0(dsl_dataset_hold(dp, nvpair_name(pair), FTAG, &ds));
468 dsl_destroy_snapshot_sync_impl(ds, dsda->dsda_defer, tx);
469 dsl_dataset_rele(ds, FTAG);
474 * The semantics of this function are described in the comment above
475 * lzc_destroy_snaps(). To summarize:
477 * The snapshots must all be in the same pool.
479 * Snapshots that don't exist will be silently ignored (considered to be
480 * "already deleted").
482 * On success, all snaps will be destroyed and this will return 0.
483 * On failure, no snaps will be destroyed, the errlist will be filled in,
484 * and this will return an errno.
487 dsl_destroy_snapshots_nvl(nvlist_t *snaps, boolean_t defer,
490 dmu_snapshots_destroy_arg_t dsda;
494 pair = nvlist_next_nvpair(snaps, NULL);
498 dsda.dsda_snaps = snaps;
499 dsda.dsda_successful_snaps = fnvlist_alloc();
500 dsda.dsda_defer = defer;
501 dsda.dsda_errlist = errlist;
503 error = dsl_sync_task(nvpair_name(pair),
504 dsl_destroy_snapshot_check, dsl_destroy_snapshot_sync,
506 fnvlist_free(dsda.dsda_successful_snaps);
512 dsl_destroy_snapshot(const char *name, boolean_t defer)
515 nvlist_t *nvl = fnvlist_alloc();
516 nvlist_t *errlist = fnvlist_alloc();
518 fnvlist_add_boolean(nvl, name);
519 error = dsl_destroy_snapshots_nvl(nvl, defer, errlist);
520 fnvlist_free(errlist);
532 kill_blkptr(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
533 const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
535 struct killarg *ka = arg;
536 dmu_tx_t *tx = ka->tx;
541 if (zb->zb_level == ZB_ZIL_LEVEL) {
542 ASSERT(zilog != NULL);
544 * It's a block in the intent log. It has no
545 * accounting, so just free it.
547 dsl_free(ka->tx->tx_pool, ka->tx->tx_txg, bp);
549 ASSERT(zilog == NULL);
550 ASSERT3U(bp->blk_birth, >, ka->ds->ds_phys->ds_prev_snap_txg);
551 (void) dsl_dataset_block_kill(ka->ds, bp, tx, B_FALSE);
558 old_synchronous_dataset_destroy(dsl_dataset_t *ds, dmu_tx_t *tx)
563 * Free everything that we point to (that's born after
564 * the previous snapshot, if we are a clone)
566 * NB: this should be very quick, because we already
567 * freed all the objects in open context.
571 VERIFY0(traverse_dataset(ds,
572 ds->ds_phys->ds_prev_snap_txg, TRAVERSE_POST,
574 ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) || ds->ds_phys->ds_unique_bytes == 0);
577 typedef struct dsl_destroy_head_arg {
578 const char *ddha_name;
579 } dsl_destroy_head_arg_t;
582 dsl_destroy_head_check_impl(dsl_dataset_t *ds, int expected_holds)
588 if (dsl_dataset_is_snapshot(ds))
589 return (SET_ERROR(EINVAL));
591 if (refcount_count(&ds->ds_longholds) != expected_holds)
592 return (SET_ERROR(EBUSY));
594 mos = ds->ds_dir->dd_pool->dp_meta_objset;
597 * Can't delete a head dataset if there are snapshots of it.
598 * (Except if the only snapshots are from the branch we cloned
601 if (ds->ds_prev != NULL &&
602 ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object)
603 return (SET_ERROR(EBUSY));
606 * Can't delete if there are children of this fs.
608 error = zap_count(mos,
609 ds->ds_dir->dd_phys->dd_child_dir_zapobj, &count);
613 return (SET_ERROR(EEXIST));
615 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev) &&
616 ds->ds_prev->ds_phys->ds_num_children == 2 &&
617 ds->ds_prev->ds_userrefs == 0) {
618 /* We need to remove the origin snapshot as well. */
619 if (!refcount_is_zero(&ds->ds_prev->ds_longholds))
620 return (SET_ERROR(EBUSY));
626 dsl_destroy_head_check(void *arg, dmu_tx_t *tx)
628 dsl_destroy_head_arg_t *ddha = arg;
629 dsl_pool_t *dp = dmu_tx_pool(tx);
633 error = dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds);
637 error = dsl_destroy_head_check_impl(ds, 0);
638 dsl_dataset_rele(ds, FTAG);
643 dsl_dir_destroy_sync(uint64_t ddobj, dmu_tx_t *tx)
646 dsl_pool_t *dp = dmu_tx_pool(tx);
647 objset_t *mos = dp->dp_meta_objset;
650 ASSERT(RRW_WRITE_HELD(&dmu_tx_pool(tx)->dp_config_rwlock));
652 VERIFY0(dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd));
654 ASSERT0(dd->dd_phys->dd_head_dataset_obj);
657 * Remove our reservation. The impl() routine avoids setting the
658 * actual property, which would require the (already destroyed) ds.
660 dsl_dir_set_reservation_sync_impl(dd, 0, tx);
662 ASSERT0(dd->dd_phys->dd_used_bytes);
663 ASSERT0(dd->dd_phys->dd_reserved);
664 for (t = 0; t < DD_USED_NUM; t++)
665 ASSERT0(dd->dd_phys->dd_used_breakdown[t]);
667 VERIFY0(zap_destroy(mos, dd->dd_phys->dd_child_dir_zapobj, tx));
668 VERIFY0(zap_destroy(mos, dd->dd_phys->dd_props_zapobj, tx));
669 VERIFY0(dsl_deleg_destroy(mos, dd->dd_phys->dd_deleg_zapobj, tx));
670 VERIFY0(zap_remove(mos,
671 dd->dd_parent->dd_phys->dd_child_dir_zapobj, dd->dd_myname, tx));
673 dsl_dir_rele(dd, FTAG);
674 VERIFY0(dmu_object_free(mos, ddobj, tx));
678 dsl_destroy_head_sync_impl(dsl_dataset_t *ds, dmu_tx_t *tx)
680 dsl_pool_t *dp = dmu_tx_pool(tx);
681 objset_t *mos = dp->dp_meta_objset;
682 uint64_t obj, ddobj, prevobj = 0;
685 ASSERT3U(ds->ds_phys->ds_num_children, <=, 1);
686 ASSERT(ds->ds_prev == NULL ||
687 ds->ds_prev->ds_phys->ds_next_snap_obj != ds->ds_object);
688 ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg);
689 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
691 /* We need to log before removing it from the namespace. */
692 spa_history_log_internal_ds(ds, "destroy", tx, "");
694 rmorigin = (dsl_dir_is_clone(ds->ds_dir) &&
695 DS_IS_DEFER_DESTROY(ds->ds_prev) &&
696 ds->ds_prev->ds_phys->ds_num_children == 2 &&
697 ds->ds_prev->ds_userrefs == 0);
699 /* Remove our reservation */
700 if (ds->ds_reserved != 0) {
701 dsl_dataset_set_refreservation_sync_impl(ds,
702 (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED),
704 ASSERT0(ds->ds_reserved);
707 dsl_scan_ds_destroyed(ds, tx);
711 if (ds->ds_phys->ds_prev_snap_obj != 0) {
712 /* This is a clone */
713 ASSERT(ds->ds_prev != NULL);
714 ASSERT3U(ds->ds_prev->ds_phys->ds_next_snap_obj, !=, obj);
715 ASSERT0(ds->ds_phys->ds_next_snap_obj);
717 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
718 if (ds->ds_prev->ds_phys->ds_next_clones_obj != 0) {
719 dsl_dataset_remove_from_next_clones(ds->ds_prev,
723 ASSERT3U(ds->ds_prev->ds_phys->ds_num_children, >, 1);
724 ds->ds_prev->ds_phys->ds_num_children--;
727 zfeature_info_t *async_destroy =
728 &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY];
732 * Destroy the deadlist. Unless it's a clone, the
733 * deadlist should be empty. (If it's a clone, it's
734 * safe to ignore the deadlist contents.)
736 dsl_deadlist_close(&ds->ds_deadlist);
737 dsl_deadlist_free(mos, ds->ds_phys->ds_deadlist_obj, tx);
738 dmu_buf_will_dirty(ds->ds_dbuf, tx);
739 ds->ds_phys->ds_deadlist_obj = 0;
741 VERIFY0(dmu_objset_from_ds(ds, &os));
743 if (!spa_feature_is_enabled(dp->dp_spa, async_destroy)) {
744 old_synchronous_dataset_destroy(ds, tx);
747 * Move the bptree into the pool's list of trees to
748 * clean up and update space accounting information.
750 uint64_t used, comp, uncomp;
752 zil_destroy_sync(dmu_objset_zil(os), tx);
754 if (!spa_feature_is_active(dp->dp_spa, async_destroy)) {
755 dsl_scan_t *scn = dp->dp_scan;
757 spa_feature_incr(dp->dp_spa, async_destroy, tx);
758 dp->dp_bptree_obj = bptree_alloc(mos, tx);
760 DMU_POOL_DIRECTORY_OBJECT,
761 DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1,
762 &dp->dp_bptree_obj, tx));
763 ASSERT(!scn->scn_async_destroying);
764 scn->scn_async_destroying = B_TRUE;
767 used = ds->ds_dir->dd_phys->dd_used_bytes;
768 comp = ds->ds_dir->dd_phys->dd_compressed_bytes;
769 uncomp = ds->ds_dir->dd_phys->dd_uncompressed_bytes;
771 ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) ||
772 ds->ds_phys->ds_unique_bytes == used);
774 bptree_add(mos, dp->dp_bptree_obj,
775 &ds->ds_phys->ds_bp, ds->ds_phys->ds_prev_snap_txg,
776 used, comp, uncomp, tx);
777 dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
778 -used, -comp, -uncomp, tx);
779 dsl_dir_diduse_space(dp->dp_free_dir, DD_USED_HEAD,
780 used, comp, uncomp, tx);
783 if (ds->ds_prev != NULL) {
784 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
785 VERIFY0(zap_remove_int(mos,
786 ds->ds_prev->ds_dir->dd_phys->dd_clones,
789 prevobj = ds->ds_prev->ds_object;
790 dsl_dataset_rele(ds->ds_prev, ds);
795 * This must be done after the dsl_traverse(), because it will
796 * re-open the objset.
799 dmu_objset_evict(ds->ds_objset);
800 ds->ds_objset = NULL;
803 /* Erase the link in the dir */
804 dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
805 ds->ds_dir->dd_phys->dd_head_dataset_obj = 0;
806 ddobj = ds->ds_dir->dd_object;
807 ASSERT(ds->ds_phys->ds_snapnames_zapobj != 0);
808 VERIFY0(zap_destroy(mos, ds->ds_phys->ds_snapnames_zapobj, tx));
810 spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
812 ASSERT0(ds->ds_phys->ds_next_clones_obj);
813 ASSERT0(ds->ds_phys->ds_props_obj);
814 ASSERT0(ds->ds_phys->ds_userrefs_obj);
815 dsl_dir_rele(ds->ds_dir, ds);
817 VERIFY0(dmu_object_free(mos, obj, tx));
819 dsl_dir_destroy_sync(ddobj, tx);
823 VERIFY0(dsl_dataset_hold_obj(dp, prevobj, FTAG, &prev));
824 dsl_destroy_snapshot_sync_impl(prev, B_FALSE, tx);
825 dsl_dataset_rele(prev, FTAG);
830 dsl_destroy_head_sync(void *arg, dmu_tx_t *tx)
832 dsl_destroy_head_arg_t *ddha = arg;
833 dsl_pool_t *dp = dmu_tx_pool(tx);
836 VERIFY0(dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds));
837 dsl_destroy_head_sync_impl(ds, tx);
838 dsl_dataset_rele(ds, FTAG);
842 dsl_destroy_head_begin_sync(void *arg, dmu_tx_t *tx)
844 dsl_destroy_head_arg_t *ddha = arg;
845 dsl_pool_t *dp = dmu_tx_pool(tx);
848 VERIFY0(dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds));
850 /* Mark it as inconsistent on-disk, in case we crash */
851 dmu_buf_will_dirty(ds->ds_dbuf, tx);
852 ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT;
854 spa_history_log_internal_ds(ds, "destroy begin", tx, "");
855 dsl_dataset_rele(ds, FTAG);
859 dsl_destroy_head(const char *name)
861 dsl_destroy_head_arg_t ddha;
867 zfs_destroy_unmount_origin(name);
870 error = spa_open(name, &spa, FTAG);
873 isenabled = spa_feature_is_enabled(spa,
874 &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY]);
875 spa_close(spa, FTAG);
877 ddha.ddha_name = name;
882 error = dsl_sync_task(name, dsl_destroy_head_check,
883 dsl_destroy_head_begin_sync, &ddha, 0);
888 * Head deletion is processed in one txg on old pools;
889 * remove the objects from open context so that the txg sync
892 error = dmu_objset_own(name, DMU_OST_ANY, B_FALSE, FTAG, &os);
894 uint64_t prev_snap_txg =
895 dmu_objset_ds(os)->ds_phys->ds_prev_snap_txg;
896 for (uint64_t obj = 0; error == 0;
897 error = dmu_object_next(os, &obj, FALSE,
899 (void) dmu_free_long_object(os, obj);
900 /* sync out all frees */
901 txg_wait_synced(dmu_objset_pool(os), 0);
902 dmu_objset_disown(os, FTAG);
906 return (dsl_sync_task(name, dsl_destroy_head_check,
907 dsl_destroy_head_sync, &ddha, 0));
911 * Note, this function is used as the callback for dmu_objset_find(). We
912 * always return 0 so that we will continue to find and process
913 * inconsistent datasets, even if we encounter an error trying to
914 * process one of them.
918 dsl_destroy_inconsistent(const char *dsname, void *arg)
922 if (dmu_objset_hold(dsname, FTAG, &os) == 0) {
923 boolean_t inconsistent = DS_IS_INCONSISTENT(dmu_objset_ds(os));
924 dmu_objset_rele(os, FTAG);
926 (void) dsl_destroy_head(dsname);