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.
26 #include <sys/zfs_context.h>
27 #include <sys/dsl_userhold.h>
28 #include <sys/dsl_dataset.h>
29 #include <sys/dsl_synctask.h>
30 #include <sys/dmu_tx.h>
31 #include <sys/dsl_pool.h>
32 #include <sys/dsl_dir.h>
33 #include <sys/dmu_traverse.h>
34 #include <sys/dsl_scan.h>
35 #include <sys/dmu_objset.h>
37 #include <sys/zfeature.h>
38 #include <sys/zfs_ioctl.h>
39 #include <sys/dsl_deleg.h>
41 typedef struct dmu_snapshots_destroy_arg {
43 nvlist_t *dsda_successful_snaps;
45 nvlist_t *dsda_errlist;
46 } dmu_snapshots_destroy_arg_t;
52 dsl_destroy_snapshot_check_impl(dsl_dataset_t *ds, boolean_t defer)
54 if (!dsl_dataset_is_snapshot(ds))
55 return (SET_ERROR(EINVAL));
57 if (dsl_dataset_long_held(ds))
58 return (SET_ERROR(EBUSY));
61 * Only allow deferred destroy on pools that support it.
62 * NOTE: deferred destroy is only supported on snapshots.
65 if (spa_version(ds->ds_dir->dd_pool->dp_spa) <
67 return (SET_ERROR(ENOTSUP));
72 * If this snapshot has an elevated user reference count,
73 * we can't destroy it yet.
75 if (ds->ds_userrefs > 0)
76 return (SET_ERROR(EBUSY));
79 * Can't delete a branch point.
81 if (ds->ds_phys->ds_num_children > 1)
82 return (SET_ERROR(EEXIST));
88 dsl_destroy_snapshot_check(void *arg, dmu_tx_t *tx)
90 dmu_snapshots_destroy_arg_t *dsda = arg;
91 dsl_pool_t *dp = dmu_tx_pool(tx);
95 if (!dmu_tx_is_syncing(tx))
98 for (pair = nvlist_next_nvpair(dsda->dsda_snaps, NULL);
99 pair != NULL; pair = nvlist_next_nvpair(dsda->dsda_snaps, pair)) {
102 error = dsl_dataset_hold(dp, nvpair_name(pair),
106 * If the snapshot does not exist, silently ignore it
107 * (it's "already destroyed").
113 error = dsl_destroy_snapshot_check_impl(ds,
115 dsl_dataset_rele(ds, FTAG);
119 fnvlist_add_boolean(dsda->dsda_successful_snaps,
122 fnvlist_add_int32(dsda->dsda_errlist,
123 nvpair_name(pair), error);
127 pair = nvlist_next_nvpair(dsda->dsda_errlist, NULL);
129 return (fnvpair_value_int32(pair));
133 struct process_old_arg {
135 dsl_dataset_t *ds_prev;
136 boolean_t after_branch_point;
138 uint64_t used, comp, uncomp;
142 process_old_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
144 struct process_old_arg *poa = arg;
145 dsl_pool_t *dp = poa->ds->ds_dir->dd_pool;
147 if (bp->blk_birth <= poa->ds->ds_phys->ds_prev_snap_txg) {
148 dsl_deadlist_insert(&poa->ds->ds_deadlist, bp, tx);
149 if (poa->ds_prev && !poa->after_branch_point &&
151 poa->ds_prev->ds_phys->ds_prev_snap_txg) {
152 poa->ds_prev->ds_phys->ds_unique_bytes +=
153 bp_get_dsize_sync(dp->dp_spa, bp);
156 poa->used += bp_get_dsize_sync(dp->dp_spa, bp);
157 poa->comp += BP_GET_PSIZE(bp);
158 poa->uncomp += BP_GET_UCSIZE(bp);
159 dsl_free_sync(poa->pio, dp, tx->tx_txg, bp);
165 process_old_deadlist(dsl_dataset_t *ds, dsl_dataset_t *ds_prev,
166 dsl_dataset_t *ds_next, boolean_t after_branch_point, dmu_tx_t *tx)
168 struct process_old_arg poa = { 0 };
169 dsl_pool_t *dp = ds->ds_dir->dd_pool;
170 objset_t *mos = dp->dp_meta_objset;
171 uint64_t deadlist_obj;
173 ASSERT(ds->ds_deadlist.dl_oldfmt);
174 ASSERT(ds_next->ds_deadlist.dl_oldfmt);
177 poa.ds_prev = ds_prev;
178 poa.after_branch_point = after_branch_point;
179 poa.pio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
180 VERIFY0(bpobj_iterate(&ds_next->ds_deadlist.dl_bpobj,
181 process_old_cb, &poa, tx));
182 VERIFY0(zio_wait(poa.pio));
183 ASSERT3U(poa.used, ==, ds->ds_phys->ds_unique_bytes);
185 /* change snapused */
186 dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
187 -poa.used, -poa.comp, -poa.uncomp, tx);
189 /* swap next's deadlist to our deadlist */
190 dsl_deadlist_close(&ds->ds_deadlist);
191 dsl_deadlist_close(&ds_next->ds_deadlist);
192 deadlist_obj = ds->ds_phys->ds_deadlist_obj;
193 ds->ds_phys->ds_deadlist_obj = ds_next->ds_phys->ds_deadlist_obj;
194 ds_next->ds_phys->ds_deadlist_obj = deadlist_obj;
195 dsl_deadlist_open(&ds->ds_deadlist, mos, ds->ds_phys->ds_deadlist_obj);
196 dsl_deadlist_open(&ds_next->ds_deadlist, mos,
197 ds_next->ds_phys->ds_deadlist_obj);
201 dsl_dataset_remove_clones_key(dsl_dataset_t *ds, uint64_t mintxg, dmu_tx_t *tx)
203 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
208 * If it is the old version, dd_clones doesn't exist so we can't
209 * find the clones, but dsl_deadlist_remove_key() is a no-op so it
212 if (ds->ds_dir->dd_phys->dd_clones == 0)
215 for (zap_cursor_init(&zc, mos, ds->ds_dir->dd_phys->dd_clones);
216 zap_cursor_retrieve(&zc, &za) == 0;
217 zap_cursor_advance(&zc)) {
218 dsl_dataset_t *clone;
220 VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
221 za.za_first_integer, FTAG, &clone));
222 if (clone->ds_dir->dd_origin_txg > mintxg) {
223 dsl_deadlist_remove_key(&clone->ds_deadlist,
225 dsl_dataset_remove_clones_key(clone, mintxg, tx);
227 dsl_dataset_rele(clone, FTAG);
229 zap_cursor_fini(&zc);
233 dsl_destroy_snapshot_sync_impl(dsl_dataset_t *ds, boolean_t defer, dmu_tx_t *tx)
236 int after_branch_point = FALSE;
237 dsl_pool_t *dp = ds->ds_dir->dd_pool;
238 objset_t *mos = dp->dp_meta_objset;
239 dsl_dataset_t *ds_prev = NULL;
242 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
243 ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg);
244 ASSERT(refcount_is_zero(&ds->ds_longholds));
247 (ds->ds_userrefs > 0 || ds->ds_phys->ds_num_children > 1)) {
248 ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
249 dmu_buf_will_dirty(ds->ds_dbuf, tx);
250 ds->ds_phys->ds_flags |= DS_FLAG_DEFER_DESTROY;
251 spa_history_log_internal_ds(ds, "defer_destroy", tx, "");
255 ASSERT3U(ds->ds_phys->ds_num_children, <=, 1);
257 /* We need to log before removing it from the namespace. */
258 spa_history_log_internal_ds(ds, "destroy", tx, "");
260 dsl_scan_ds_destroyed(ds, tx);
264 if (ds->ds_phys->ds_prev_snap_obj != 0) {
265 ASSERT3P(ds->ds_prev, ==, NULL);
266 VERIFY0(dsl_dataset_hold_obj(dp,
267 ds->ds_phys->ds_prev_snap_obj, FTAG, &ds_prev));
269 (ds_prev->ds_phys->ds_next_snap_obj != obj);
271 dmu_buf_will_dirty(ds_prev->ds_dbuf, tx);
272 if (after_branch_point &&
273 ds_prev->ds_phys->ds_next_clones_obj != 0) {
274 dsl_dataset_remove_from_next_clones(ds_prev, obj, tx);
275 if (ds->ds_phys->ds_next_snap_obj != 0) {
276 VERIFY0(zap_add_int(mos,
277 ds_prev->ds_phys->ds_next_clones_obj,
278 ds->ds_phys->ds_next_snap_obj, tx));
281 if (!after_branch_point) {
282 ds_prev->ds_phys->ds_next_snap_obj =
283 ds->ds_phys->ds_next_snap_obj;
287 dsl_dataset_t *ds_next;
289 uint64_t used = 0, comp = 0, uncomp = 0;
291 VERIFY0(dsl_dataset_hold_obj(dp,
292 ds->ds_phys->ds_next_snap_obj, FTAG, &ds_next));
293 ASSERT3U(ds_next->ds_phys->ds_prev_snap_obj, ==, obj);
295 old_unique = ds_next->ds_phys->ds_unique_bytes;
297 dmu_buf_will_dirty(ds_next->ds_dbuf, tx);
298 ds_next->ds_phys->ds_prev_snap_obj =
299 ds->ds_phys->ds_prev_snap_obj;
300 ds_next->ds_phys->ds_prev_snap_txg =
301 ds->ds_phys->ds_prev_snap_txg;
302 ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
303 ds_prev ? ds_prev->ds_phys->ds_creation_txg : 0);
305 if (ds_next->ds_deadlist.dl_oldfmt) {
306 process_old_deadlist(ds, ds_prev, ds_next,
307 after_branch_point, tx);
309 /* Adjust prev's unique space. */
310 if (ds_prev && !after_branch_point) {
311 dsl_deadlist_space_range(&ds_next->ds_deadlist,
312 ds_prev->ds_phys->ds_prev_snap_txg,
313 ds->ds_phys->ds_prev_snap_txg,
314 &used, &comp, &uncomp);
315 ds_prev->ds_phys->ds_unique_bytes += used;
318 /* Adjust snapused. */
319 dsl_deadlist_space_range(&ds_next->ds_deadlist,
320 ds->ds_phys->ds_prev_snap_txg, UINT64_MAX,
321 &used, &comp, &uncomp);
322 dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
323 -used, -comp, -uncomp, tx);
325 /* Move blocks to be freed to pool's free list. */
326 dsl_deadlist_move_bpobj(&ds_next->ds_deadlist,
327 &dp->dp_free_bpobj, ds->ds_phys->ds_prev_snap_txg,
329 dsl_dir_diduse_space(tx->tx_pool->dp_free_dir,
330 DD_USED_HEAD, used, comp, uncomp, tx);
332 /* Merge our deadlist into next's and free it. */
333 dsl_deadlist_merge(&ds_next->ds_deadlist,
334 ds->ds_phys->ds_deadlist_obj, tx);
336 dsl_deadlist_close(&ds->ds_deadlist);
337 dsl_deadlist_free(mos, ds->ds_phys->ds_deadlist_obj, tx);
338 dmu_buf_will_dirty(ds->ds_dbuf, tx);
339 ds->ds_phys->ds_deadlist_obj = 0;
341 /* Collapse range in clone heads */
342 dsl_dataset_remove_clones_key(ds,
343 ds->ds_phys->ds_creation_txg, tx);
345 if (dsl_dataset_is_snapshot(ds_next)) {
346 dsl_dataset_t *ds_nextnext;
349 * Update next's unique to include blocks which
350 * were previously shared by only this snapshot
351 * and it. Those blocks will be born after the
352 * prev snap and before this snap, and will have
353 * died after the next snap and before the one
354 * after that (ie. be on the snap after next's
357 VERIFY0(dsl_dataset_hold_obj(dp,
358 ds_next->ds_phys->ds_next_snap_obj, FTAG, &ds_nextnext));
359 dsl_deadlist_space_range(&ds_nextnext->ds_deadlist,
360 ds->ds_phys->ds_prev_snap_txg,
361 ds->ds_phys->ds_creation_txg,
362 &used, &comp, &uncomp);
363 ds_next->ds_phys->ds_unique_bytes += used;
364 dsl_dataset_rele(ds_nextnext, FTAG);
365 ASSERT3P(ds_next->ds_prev, ==, NULL);
367 /* Collapse range in this head. */
369 VERIFY0(dsl_dataset_hold_obj(dp,
370 ds->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &hds));
371 dsl_deadlist_remove_key(&hds->ds_deadlist,
372 ds->ds_phys->ds_creation_txg, tx);
373 dsl_dataset_rele(hds, FTAG);
376 ASSERT3P(ds_next->ds_prev, ==, ds);
377 dsl_dataset_rele(ds_next->ds_prev, ds_next);
378 ds_next->ds_prev = NULL;
380 VERIFY0(dsl_dataset_hold_obj(dp,
381 ds->ds_phys->ds_prev_snap_obj,
382 ds_next, &ds_next->ds_prev));
385 dsl_dataset_recalc_head_uniq(ds_next);
388 * Reduce the amount of our unconsumed refreservation
389 * being charged to our parent by the amount of
390 * new unique data we have gained.
392 if (old_unique < ds_next->ds_reserved) {
394 uint64_t new_unique =
395 ds_next->ds_phys->ds_unique_bytes;
397 ASSERT(old_unique <= new_unique);
398 mrsdelta = MIN(new_unique - old_unique,
399 ds_next->ds_reserved - old_unique);
400 dsl_dir_diduse_space(ds->ds_dir,
401 DD_USED_REFRSRV, -mrsdelta, 0, 0, tx);
404 dsl_dataset_rele(ds_next, FTAG);
407 * This must be done after the dsl_traverse(), because it will
408 * re-open the objset.
411 dmu_objset_evict(ds->ds_objset);
412 ds->ds_objset = NULL;
415 /* remove from snapshot namespace */
416 dsl_dataset_t *ds_head;
417 ASSERT(ds->ds_phys->ds_snapnames_zapobj == 0);
418 VERIFY0(dsl_dataset_hold_obj(dp,
419 ds->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ds_head));
420 VERIFY0(dsl_dataset_get_snapname(ds));
425 err = dsl_dataset_snap_lookup(ds_head,
426 ds->ds_snapname, &val);
428 ASSERT3U(val, ==, obj);
431 VERIFY0(dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx));
432 dsl_dataset_rele(ds_head, FTAG);
435 dsl_dataset_rele(ds_prev, FTAG);
437 spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
439 if (ds->ds_phys->ds_next_clones_obj != 0) {
441 ASSERT0(zap_count(mos,
442 ds->ds_phys->ds_next_clones_obj, &count) && count == 0);
443 VERIFY0(dmu_object_free(mos,
444 ds->ds_phys->ds_next_clones_obj, tx));
446 if (ds->ds_phys->ds_props_obj != 0)
447 VERIFY0(zap_destroy(mos, ds->ds_phys->ds_props_obj, tx));
448 if (ds->ds_phys->ds_userrefs_obj != 0)
449 VERIFY0(zap_destroy(mos, ds->ds_phys->ds_userrefs_obj, tx));
450 dsl_dir_rele(ds->ds_dir, ds);
452 VERIFY0(dmu_object_free(mos, obj, tx));
456 dsl_destroy_snapshot_sync(void *arg, dmu_tx_t *tx)
458 dmu_snapshots_destroy_arg_t *dsda = arg;
459 dsl_pool_t *dp = dmu_tx_pool(tx);
462 for (pair = nvlist_next_nvpair(dsda->dsda_successful_snaps, NULL);
464 pair = nvlist_next_nvpair(dsda->dsda_successful_snaps, pair)) {
467 VERIFY0(dsl_dataset_hold(dp, nvpair_name(pair), FTAG, &ds));
469 dsl_destroy_snapshot_sync_impl(ds, dsda->dsda_defer, tx);
470 dsl_dataset_rele(ds, FTAG);
475 * The semantics of this function are described in the comment above
476 * lzc_destroy_snaps(). To summarize:
478 * The snapshots must all be in the same pool.
480 * Snapshots that don't exist will be silently ignored (considered to be
481 * "already deleted").
483 * On success, all snaps will be destroyed and this will return 0.
484 * On failure, no snaps will be destroyed, the errlist will be filled in,
485 * and this will return an errno.
488 dsl_destroy_snapshots_nvl(nvlist_t *snaps, boolean_t defer,
491 dmu_snapshots_destroy_arg_t dsda;
495 pair = nvlist_next_nvpair(snaps, NULL);
499 dsda.dsda_snaps = snaps;
500 dsda.dsda_successful_snaps = fnvlist_alloc();
501 dsda.dsda_defer = defer;
502 dsda.dsda_errlist = errlist;
504 error = dsl_sync_task(nvpair_name(pair),
505 dsl_destroy_snapshot_check, dsl_destroy_snapshot_sync,
507 fnvlist_free(dsda.dsda_successful_snaps);
513 dsl_destroy_snapshot(const char *name, boolean_t defer)
516 nvlist_t *nvl = fnvlist_alloc();
517 nvlist_t *errlist = fnvlist_alloc();
519 fnvlist_add_boolean(nvl, name);
520 error = dsl_destroy_snapshots_nvl(nvl, defer, errlist);
521 fnvlist_free(errlist);
533 kill_blkptr(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
534 const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
536 struct killarg *ka = arg;
537 dmu_tx_t *tx = ka->tx;
542 if (zb->zb_level == ZB_ZIL_LEVEL) {
543 ASSERT(zilog != NULL);
545 * It's a block in the intent log. It has no
546 * accounting, so just free it.
548 dsl_free(ka->tx->tx_pool, ka->tx->tx_txg, bp);
550 ASSERT(zilog == NULL);
551 ASSERT3U(bp->blk_birth, >, ka->ds->ds_phys->ds_prev_snap_txg);
552 (void) dsl_dataset_block_kill(ka->ds, bp, tx, B_FALSE);
559 old_synchronous_dataset_destroy(dsl_dataset_t *ds, dmu_tx_t *tx)
564 * Free everything that we point to (that's born after
565 * the previous snapshot, if we are a clone)
567 * NB: this should be very quick, because we already
568 * freed all the objects in open context.
572 VERIFY0(traverse_dataset(ds,
573 ds->ds_phys->ds_prev_snap_txg, TRAVERSE_POST,
575 ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) || ds->ds_phys->ds_unique_bytes == 0);
578 typedef struct dsl_destroy_head_arg {
579 const char *ddha_name;
580 } dsl_destroy_head_arg_t;
583 dsl_destroy_head_check_impl(dsl_dataset_t *ds, int expected_holds)
589 if (dsl_dataset_is_snapshot(ds))
590 return (SET_ERROR(EINVAL));
592 if (refcount_count(&ds->ds_longholds) != expected_holds)
593 return (SET_ERROR(EBUSY));
595 mos = ds->ds_dir->dd_pool->dp_meta_objset;
598 * Can't delete a head dataset if there are snapshots of it.
599 * (Except if the only snapshots are from the branch we cloned
602 if (ds->ds_prev != NULL &&
603 ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object)
604 return (SET_ERROR(EBUSY));
607 * Can't delete if there are children of this fs.
609 error = zap_count(mos,
610 ds->ds_dir->dd_phys->dd_child_dir_zapobj, &count);
614 return (SET_ERROR(EEXIST));
616 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev) &&
617 ds->ds_prev->ds_phys->ds_num_children == 2 &&
618 ds->ds_prev->ds_userrefs == 0) {
619 /* We need to remove the origin snapshot as well. */
620 if (!refcount_is_zero(&ds->ds_prev->ds_longholds))
621 return (SET_ERROR(EBUSY));
627 dsl_destroy_head_check(void *arg, dmu_tx_t *tx)
629 dsl_destroy_head_arg_t *ddha = arg;
630 dsl_pool_t *dp = dmu_tx_pool(tx);
634 error = dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds);
638 error = dsl_destroy_head_check_impl(ds, 0);
639 dsl_dataset_rele(ds, FTAG);
644 dsl_dir_destroy_sync(uint64_t ddobj, dmu_tx_t *tx)
647 dsl_pool_t *dp = dmu_tx_pool(tx);
648 objset_t *mos = dp->dp_meta_objset;
651 ASSERT(RRW_WRITE_HELD(&dmu_tx_pool(tx)->dp_config_rwlock));
653 VERIFY0(dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd));
655 ASSERT0(dd->dd_phys->dd_head_dataset_obj);
658 * Remove our reservation. The impl() routine avoids setting the
659 * actual property, which would require the (already destroyed) ds.
661 dsl_dir_set_reservation_sync_impl(dd, 0, tx);
663 ASSERT0(dd->dd_phys->dd_used_bytes);
664 ASSERT0(dd->dd_phys->dd_reserved);
665 for (t = 0; t < DD_USED_NUM; t++)
666 ASSERT0(dd->dd_phys->dd_used_breakdown[t]);
668 VERIFY0(zap_destroy(mos, dd->dd_phys->dd_child_dir_zapobj, tx));
669 VERIFY0(zap_destroy(mos, dd->dd_phys->dd_props_zapobj, tx));
670 VERIFY0(dsl_deleg_destroy(mos, dd->dd_phys->dd_deleg_zapobj, tx));
671 VERIFY0(zap_remove(mos,
672 dd->dd_parent->dd_phys->dd_child_dir_zapobj, dd->dd_myname, tx));
674 dsl_dir_rele(dd, FTAG);
675 VERIFY0(dmu_object_free(mos, ddobj, tx));
679 dsl_destroy_head_sync_impl(dsl_dataset_t *ds, dmu_tx_t *tx)
681 dsl_pool_t *dp = dmu_tx_pool(tx);
682 objset_t *mos = dp->dp_meta_objset;
683 uint64_t obj, ddobj, prevobj = 0;
686 ASSERT3U(ds->ds_phys->ds_num_children, <=, 1);
687 ASSERT(ds->ds_prev == NULL ||
688 ds->ds_prev->ds_phys->ds_next_snap_obj != ds->ds_object);
689 ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg);
690 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
692 /* We need to log before removing it from the namespace. */
693 spa_history_log_internal_ds(ds, "destroy", tx, "");
695 rmorigin = (dsl_dir_is_clone(ds->ds_dir) &&
696 DS_IS_DEFER_DESTROY(ds->ds_prev) &&
697 ds->ds_prev->ds_phys->ds_num_children == 2 &&
698 ds->ds_prev->ds_userrefs == 0);
700 /* Remove our reservation */
701 if (ds->ds_reserved != 0) {
702 dsl_dataset_set_refreservation_sync_impl(ds,
703 (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED),
705 ASSERT0(ds->ds_reserved);
708 dsl_scan_ds_destroyed(ds, tx);
712 if (ds->ds_phys->ds_prev_snap_obj != 0) {
713 /* This is a clone */
714 ASSERT(ds->ds_prev != NULL);
715 ASSERT3U(ds->ds_prev->ds_phys->ds_next_snap_obj, !=, obj);
716 ASSERT0(ds->ds_phys->ds_next_snap_obj);
718 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
719 if (ds->ds_prev->ds_phys->ds_next_clones_obj != 0) {
720 dsl_dataset_remove_from_next_clones(ds->ds_prev,
724 ASSERT3U(ds->ds_prev->ds_phys->ds_num_children, >, 1);
725 ds->ds_prev->ds_phys->ds_num_children--;
728 zfeature_info_t *async_destroy =
729 &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY];
733 * Destroy the deadlist. Unless it's a clone, the
734 * deadlist should be empty. (If it's a clone, it's
735 * safe to ignore the deadlist contents.)
737 dsl_deadlist_close(&ds->ds_deadlist);
738 dsl_deadlist_free(mos, ds->ds_phys->ds_deadlist_obj, tx);
739 dmu_buf_will_dirty(ds->ds_dbuf, tx);
740 ds->ds_phys->ds_deadlist_obj = 0;
742 VERIFY0(dmu_objset_from_ds(ds, &os));
744 if (!spa_feature_is_enabled(dp->dp_spa, async_destroy)) {
745 old_synchronous_dataset_destroy(ds, tx);
748 * Move the bptree into the pool's list of trees to
749 * clean up and update space accounting information.
751 uint64_t used, comp, uncomp;
753 zil_destroy_sync(dmu_objset_zil(os), tx);
755 if (!spa_feature_is_active(dp->dp_spa, async_destroy)) {
756 dsl_scan_t *scn = dp->dp_scan;
758 spa_feature_incr(dp->dp_spa, async_destroy, tx);
759 dp->dp_bptree_obj = bptree_alloc(mos, tx);
761 DMU_POOL_DIRECTORY_OBJECT,
762 DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1,
763 &dp->dp_bptree_obj, tx));
764 ASSERT(!scn->scn_async_destroying);
765 scn->scn_async_destroying = B_TRUE;
768 used = ds->ds_dir->dd_phys->dd_used_bytes;
769 comp = ds->ds_dir->dd_phys->dd_compressed_bytes;
770 uncomp = ds->ds_dir->dd_phys->dd_uncompressed_bytes;
772 ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) ||
773 ds->ds_phys->ds_unique_bytes == used);
775 bptree_add(mos, dp->dp_bptree_obj,
776 &ds->ds_phys->ds_bp, ds->ds_phys->ds_prev_snap_txg,
777 used, comp, uncomp, tx);
778 dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
779 -used, -comp, -uncomp, tx);
780 dsl_dir_diduse_space(dp->dp_free_dir, DD_USED_HEAD,
781 used, comp, uncomp, tx);
784 if (ds->ds_prev != NULL) {
785 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
786 VERIFY0(zap_remove_int(mos,
787 ds->ds_prev->ds_dir->dd_phys->dd_clones,
790 prevobj = ds->ds_prev->ds_object;
791 dsl_dataset_rele(ds->ds_prev, ds);
796 * This must be done after the dsl_traverse(), because it will
797 * re-open the objset.
800 dmu_objset_evict(ds->ds_objset);
801 ds->ds_objset = NULL;
804 /* Erase the link in the dir */
805 dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
806 ds->ds_dir->dd_phys->dd_head_dataset_obj = 0;
807 ddobj = ds->ds_dir->dd_object;
808 ASSERT(ds->ds_phys->ds_snapnames_zapobj != 0);
809 VERIFY0(zap_destroy(mos, ds->ds_phys->ds_snapnames_zapobj, tx));
811 spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
813 ASSERT0(ds->ds_phys->ds_next_clones_obj);
814 ASSERT0(ds->ds_phys->ds_props_obj);
815 ASSERT0(ds->ds_phys->ds_userrefs_obj);
816 dsl_dir_rele(ds->ds_dir, ds);
818 VERIFY0(dmu_object_free(mos, obj, tx));
820 dsl_dir_destroy_sync(ddobj, tx);
824 VERIFY0(dsl_dataset_hold_obj(dp, prevobj, FTAG, &prev));
825 dsl_destroy_snapshot_sync_impl(prev, B_FALSE, tx);
826 dsl_dataset_rele(prev, FTAG);
831 dsl_destroy_head_sync(void *arg, dmu_tx_t *tx)
833 dsl_destroy_head_arg_t *ddha = arg;
834 dsl_pool_t *dp = dmu_tx_pool(tx);
837 VERIFY0(dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds));
838 dsl_destroy_head_sync_impl(ds, tx);
839 dsl_dataset_rele(ds, FTAG);
843 dsl_destroy_head_begin_sync(void *arg, dmu_tx_t *tx)
845 dsl_destroy_head_arg_t *ddha = arg;
846 dsl_pool_t *dp = dmu_tx_pool(tx);
849 VERIFY0(dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds));
851 /* Mark it as inconsistent on-disk, in case we crash */
852 dmu_buf_will_dirty(ds->ds_dbuf, tx);
853 ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT;
855 spa_history_log_internal_ds(ds, "destroy begin", tx, "");
856 dsl_dataset_rele(ds, FTAG);
860 dsl_destroy_head(const char *name)
862 dsl_destroy_head_arg_t ddha;
868 zfs_destroy_unmount_origin(name);
871 error = spa_open(name, &spa, FTAG);
874 isenabled = spa_feature_is_enabled(spa,
875 &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY]);
876 spa_close(spa, FTAG);
878 ddha.ddha_name = name;
883 error = dsl_sync_task(name, dsl_destroy_head_check,
884 dsl_destroy_head_begin_sync, &ddha, 0);
889 * Head deletion is processed in one txg on old pools;
890 * remove the objects from open context so that the txg sync
893 error = dmu_objset_own(name, DMU_OST_ANY, B_FALSE, FTAG, &os);
895 uint64_t prev_snap_txg =
896 dmu_objset_ds(os)->ds_phys->ds_prev_snap_txg;
897 for (uint64_t obj = 0; error == 0;
898 error = dmu_object_next(os, &obj, FALSE,
900 (void) dmu_free_object(os, obj);
901 /* sync out all frees */
902 txg_wait_synced(dmu_objset_pool(os), 0);
903 dmu_objset_disown(os, FTAG);
907 return (dsl_sync_task(name, dsl_destroy_head_check,
908 dsl_destroy_head_sync, &ddha, 0));
912 * Note, this function is used as the callback for dmu_objset_find(). We
913 * always return 0 so that we will continue to find and process
914 * inconsistent datasets, even if we encounter an error trying to
915 * process one of them.
919 dsl_destroy_inconsistent(const char *dsname, void *arg)
923 if (dmu_objset_hold(dsname, FTAG, &os) == 0) {
924 boolean_t inconsistent = DS_IS_INCONSISTENT(dmu_objset_ds(os));
925 dmu_objset_rele(os, FTAG);
927 (void) dsl_destroy_head(dsname);