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) 2012, 2014 by Delphix. All rights reserved.
24 * Copyright (c) 2013 Steven Hartland. All rights reserved.
25 * Copyright (c) 2013 by Joyent, Inc. All rights reserved.
28 #include <sys/zfs_context.h>
29 #include <sys/dsl_userhold.h>
30 #include <sys/dsl_dataset.h>
31 #include <sys/dsl_synctask.h>
32 #include <sys/dmu_tx.h>
33 #include <sys/dsl_pool.h>
34 #include <sys/dsl_dir.h>
35 #include <sys/dmu_traverse.h>
36 #include <sys/dsl_scan.h>
37 #include <sys/dmu_objset.h>
39 #include <sys/zfeature.h>
40 #include <sys/zfs_ioctl.h>
41 #include <sys/dsl_deleg.h>
42 #include <sys/dmu_impl.h>
44 typedef struct dmu_snapshots_destroy_arg {
46 nvlist_t *dsda_successful_snaps;
48 nvlist_t *dsda_errlist;
49 } 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));
134 struct process_old_arg {
136 dsl_dataset_t *ds_prev;
137 boolean_t after_branch_point;
139 uint64_t used, comp, uncomp;
143 process_old_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
145 struct process_old_arg *poa = arg;
146 dsl_pool_t *dp = poa->ds->ds_dir->dd_pool;
148 ASSERT(!BP_IS_HOLE(bp));
150 if (bp->blk_birth <= poa->ds->ds_phys->ds_prev_snap_txg) {
151 dsl_deadlist_insert(&poa->ds->ds_deadlist, bp, tx);
152 if (poa->ds_prev && !poa->after_branch_point &&
154 poa->ds_prev->ds_phys->ds_prev_snap_txg) {
155 poa->ds_prev->ds_phys->ds_unique_bytes +=
156 bp_get_dsize_sync(dp->dp_spa, bp);
159 poa->used += bp_get_dsize_sync(dp->dp_spa, bp);
160 poa->comp += BP_GET_PSIZE(bp);
161 poa->uncomp += BP_GET_UCSIZE(bp);
162 dsl_free_sync(poa->pio, dp, tx->tx_txg, bp);
168 process_old_deadlist(dsl_dataset_t *ds, dsl_dataset_t *ds_prev,
169 dsl_dataset_t *ds_next, boolean_t after_branch_point, dmu_tx_t *tx)
171 struct process_old_arg poa = { 0 };
172 dsl_pool_t *dp = ds->ds_dir->dd_pool;
173 objset_t *mos = dp->dp_meta_objset;
174 uint64_t deadlist_obj;
176 ASSERT(ds->ds_deadlist.dl_oldfmt);
177 ASSERT(ds_next->ds_deadlist.dl_oldfmt);
180 poa.ds_prev = ds_prev;
181 poa.after_branch_point = after_branch_point;
182 poa.pio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
183 VERIFY0(bpobj_iterate(&ds_next->ds_deadlist.dl_bpobj,
184 process_old_cb, &poa, tx));
185 VERIFY0(zio_wait(poa.pio));
186 ASSERT3U(poa.used, ==, ds->ds_phys->ds_unique_bytes);
188 /* change snapused */
189 dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
190 -poa.used, -poa.comp, -poa.uncomp, tx);
192 /* swap next's deadlist to our deadlist */
193 dsl_deadlist_close(&ds->ds_deadlist);
194 dsl_deadlist_close(&ds_next->ds_deadlist);
195 deadlist_obj = ds->ds_phys->ds_deadlist_obj;
196 ds->ds_phys->ds_deadlist_obj = ds_next->ds_phys->ds_deadlist_obj;
197 ds_next->ds_phys->ds_deadlist_obj = deadlist_obj;
198 dsl_deadlist_open(&ds->ds_deadlist, mos, ds->ds_phys->ds_deadlist_obj);
199 dsl_deadlist_open(&ds_next->ds_deadlist, mos,
200 ds_next->ds_phys->ds_deadlist_obj);
204 dsl_dataset_remove_clones_key(dsl_dataset_t *ds, uint64_t mintxg, dmu_tx_t *tx)
206 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
211 * If it is the old version, dd_clones doesn't exist so we can't
212 * find the clones, but dsl_deadlist_remove_key() is a no-op so it
215 if (ds->ds_dir->dd_phys->dd_clones == 0)
218 for (zap_cursor_init(&zc, mos, ds->ds_dir->dd_phys->dd_clones);
219 zap_cursor_retrieve(&zc, &za) == 0;
220 zap_cursor_advance(&zc)) {
221 dsl_dataset_t *clone;
223 VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
224 za.za_first_integer, FTAG, &clone));
225 if (clone->ds_dir->dd_origin_txg > mintxg) {
226 dsl_deadlist_remove_key(&clone->ds_deadlist,
228 dsl_dataset_remove_clones_key(clone, mintxg, tx);
230 dsl_dataset_rele(clone, FTAG);
232 zap_cursor_fini(&zc);
236 dsl_destroy_snapshot_sync_impl(dsl_dataset_t *ds, boolean_t defer, dmu_tx_t *tx)
239 int after_branch_point = FALSE;
240 dsl_pool_t *dp = ds->ds_dir->dd_pool;
241 objset_t *mos = dp->dp_meta_objset;
242 dsl_dataset_t *ds_prev = NULL;
245 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
246 ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg);
247 ASSERT(refcount_is_zero(&ds->ds_longholds));
250 (ds->ds_userrefs > 0 || ds->ds_phys->ds_num_children > 1)) {
251 ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
252 dmu_buf_will_dirty(ds->ds_dbuf, tx);
253 ds->ds_phys->ds_flags |= DS_FLAG_DEFER_DESTROY;
254 spa_history_log_internal_ds(ds, "defer_destroy", tx, "");
258 ASSERT3U(ds->ds_phys->ds_num_children, <=, 1);
260 /* We need to log before removing it from the namespace. */
261 spa_history_log_internal_ds(ds, "destroy", tx, "");
263 dsl_scan_ds_destroyed(ds, tx);
267 if (ds->ds_phys->ds_prev_snap_obj != 0) {
268 ASSERT3P(ds->ds_prev, ==, NULL);
269 VERIFY0(dsl_dataset_hold_obj(dp,
270 ds->ds_phys->ds_prev_snap_obj, FTAG, &ds_prev));
272 (ds_prev->ds_phys->ds_next_snap_obj != obj);
274 dmu_buf_will_dirty(ds_prev->ds_dbuf, tx);
275 if (after_branch_point &&
276 ds_prev->ds_phys->ds_next_clones_obj != 0) {
277 dsl_dataset_remove_from_next_clones(ds_prev, obj, tx);
278 if (ds->ds_phys->ds_next_snap_obj != 0) {
279 VERIFY0(zap_add_int(mos,
280 ds_prev->ds_phys->ds_next_clones_obj,
281 ds->ds_phys->ds_next_snap_obj, tx));
284 if (!after_branch_point) {
285 ds_prev->ds_phys->ds_next_snap_obj =
286 ds->ds_phys->ds_next_snap_obj;
290 dsl_dataset_t *ds_next;
292 uint64_t used = 0, comp = 0, uncomp = 0;
294 VERIFY0(dsl_dataset_hold_obj(dp,
295 ds->ds_phys->ds_next_snap_obj, FTAG, &ds_next));
296 ASSERT3U(ds_next->ds_phys->ds_prev_snap_obj, ==, obj);
298 old_unique = ds_next->ds_phys->ds_unique_bytes;
300 dmu_buf_will_dirty(ds_next->ds_dbuf, tx);
301 ds_next->ds_phys->ds_prev_snap_obj =
302 ds->ds_phys->ds_prev_snap_obj;
303 ds_next->ds_phys->ds_prev_snap_txg =
304 ds->ds_phys->ds_prev_snap_txg;
305 ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
306 ds_prev ? ds_prev->ds_phys->ds_creation_txg : 0);
308 if (ds_next->ds_deadlist.dl_oldfmt) {
309 process_old_deadlist(ds, ds_prev, ds_next,
310 after_branch_point, tx);
312 /* Adjust prev's unique space. */
313 if (ds_prev && !after_branch_point) {
314 dsl_deadlist_space_range(&ds_next->ds_deadlist,
315 ds_prev->ds_phys->ds_prev_snap_txg,
316 ds->ds_phys->ds_prev_snap_txg,
317 &used, &comp, &uncomp);
318 ds_prev->ds_phys->ds_unique_bytes += used;
321 /* Adjust snapused. */
322 dsl_deadlist_space_range(&ds_next->ds_deadlist,
323 ds->ds_phys->ds_prev_snap_txg, UINT64_MAX,
324 &used, &comp, &uncomp);
325 dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
326 -used, -comp, -uncomp, tx);
328 /* Move blocks to be freed to pool's free list. */
329 dsl_deadlist_move_bpobj(&ds_next->ds_deadlist,
330 &dp->dp_free_bpobj, ds->ds_phys->ds_prev_snap_txg,
332 dsl_dir_diduse_space(tx->tx_pool->dp_free_dir,
333 DD_USED_HEAD, used, comp, uncomp, tx);
335 /* Merge our deadlist into next's and free it. */
336 dsl_deadlist_merge(&ds_next->ds_deadlist,
337 ds->ds_phys->ds_deadlist_obj, tx);
339 dsl_deadlist_close(&ds->ds_deadlist);
340 dsl_deadlist_free(mos, ds->ds_phys->ds_deadlist_obj, tx);
341 dmu_buf_will_dirty(ds->ds_dbuf, tx);
342 ds->ds_phys->ds_deadlist_obj = 0;
344 /* Collapse range in clone heads */
345 dsl_dataset_remove_clones_key(ds,
346 ds->ds_phys->ds_creation_txg, tx);
348 if (dsl_dataset_is_snapshot(ds_next)) {
349 dsl_dataset_t *ds_nextnext;
352 * Update next's unique to include blocks which
353 * were previously shared by only this snapshot
354 * and it. Those blocks will be born after the
355 * prev snap and before this snap, and will have
356 * died after the next snap and before the one
357 * after that (ie. be on the snap after next's
360 VERIFY0(dsl_dataset_hold_obj(dp,
361 ds_next->ds_phys->ds_next_snap_obj, FTAG, &ds_nextnext));
362 dsl_deadlist_space_range(&ds_nextnext->ds_deadlist,
363 ds->ds_phys->ds_prev_snap_txg,
364 ds->ds_phys->ds_creation_txg,
365 &used, &comp, &uncomp);
366 ds_next->ds_phys->ds_unique_bytes += used;
367 dsl_dataset_rele(ds_nextnext, FTAG);
368 ASSERT3P(ds_next->ds_prev, ==, NULL);
370 /* Collapse range in this head. */
372 VERIFY0(dsl_dataset_hold_obj(dp,
373 ds->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &hds));
374 dsl_deadlist_remove_key(&hds->ds_deadlist,
375 ds->ds_phys->ds_creation_txg, tx);
376 dsl_dataset_rele(hds, FTAG);
379 ASSERT3P(ds_next->ds_prev, ==, ds);
380 dsl_dataset_rele(ds_next->ds_prev, ds_next);
381 ds_next->ds_prev = NULL;
383 VERIFY0(dsl_dataset_hold_obj(dp,
384 ds->ds_phys->ds_prev_snap_obj,
385 ds_next, &ds_next->ds_prev));
388 dsl_dataset_recalc_head_uniq(ds_next);
391 * Reduce the amount of our unconsumed refreservation
392 * being charged to our parent by the amount of
393 * new unique data we have gained.
395 if (old_unique < ds_next->ds_reserved) {
397 uint64_t new_unique =
398 ds_next->ds_phys->ds_unique_bytes;
400 ASSERT(old_unique <= new_unique);
401 mrsdelta = MIN(new_unique - old_unique,
402 ds_next->ds_reserved - old_unique);
403 dsl_dir_diduse_space(ds->ds_dir,
404 DD_USED_REFRSRV, -mrsdelta, 0, 0, tx);
407 dsl_dataset_rele(ds_next, FTAG);
410 * This must be done after the dsl_traverse(), because it will
411 * re-open the objset.
414 dmu_objset_evict(ds->ds_objset);
415 ds->ds_objset = NULL;
418 /* remove from snapshot namespace */
419 dsl_dataset_t *ds_head;
420 ASSERT(ds->ds_phys->ds_snapnames_zapobj == 0);
421 VERIFY0(dsl_dataset_hold_obj(dp,
422 ds->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ds_head));
423 VERIFY0(dsl_dataset_get_snapname(ds));
428 err = dsl_dataset_snap_lookup(ds_head,
429 ds->ds_snapname, &val);
431 ASSERT3U(val, ==, obj);
434 VERIFY0(dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx, B_TRUE));
435 dsl_dataset_rele(ds_head, FTAG);
438 dsl_dataset_rele(ds_prev, FTAG);
440 spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
442 if (ds->ds_phys->ds_next_clones_obj != 0) {
444 ASSERT0(zap_count(mos,
445 ds->ds_phys->ds_next_clones_obj, &count) && count == 0);
446 VERIFY0(dmu_object_free(mos,
447 ds->ds_phys->ds_next_clones_obj, tx));
449 if (ds->ds_phys->ds_props_obj != 0)
450 VERIFY0(zap_destroy(mos, ds->ds_phys->ds_props_obj, tx));
451 if (ds->ds_phys->ds_userrefs_obj != 0)
452 VERIFY0(zap_destroy(mos, ds->ds_phys->ds_userrefs_obj, tx));
453 dsl_dir_rele(ds->ds_dir, ds);
455 dmu_object_free_zapified(mos, obj, tx);
459 dsl_destroy_snapshot_sync(void *arg, dmu_tx_t *tx)
461 dmu_snapshots_destroy_arg_t *dsda = arg;
462 dsl_pool_t *dp = dmu_tx_pool(tx);
465 for (pair = nvlist_next_nvpair(dsda->dsda_successful_snaps, NULL);
467 pair = nvlist_next_nvpair(dsda->dsda_successful_snaps, pair)) {
470 VERIFY0(dsl_dataset_hold(dp, nvpair_name(pair), FTAG, &ds));
472 dsl_destroy_snapshot_sync_impl(ds, dsda->dsda_defer, tx);
473 dsl_dataset_rele(ds, FTAG);
478 * The semantics of this function are described in the comment above
479 * lzc_destroy_snaps(). To summarize:
481 * The snapshots must all be in the same pool.
483 * Snapshots that don't exist will be silently ignored (considered to be
484 * "already deleted").
486 * On success, all snaps will be destroyed and this will return 0.
487 * On failure, no snaps will be destroyed, the errlist will be filled in,
488 * and this will return an errno.
491 dsl_destroy_snapshots_nvl(nvlist_t *snaps, boolean_t defer,
494 dmu_snapshots_destroy_arg_t dsda;
498 pair = nvlist_next_nvpair(snaps, NULL);
502 dsda.dsda_snaps = snaps;
503 dsda.dsda_successful_snaps = fnvlist_alloc();
504 dsda.dsda_defer = defer;
505 dsda.dsda_errlist = errlist;
507 error = dsl_sync_task(nvpair_name(pair),
508 dsl_destroy_snapshot_check, dsl_destroy_snapshot_sync,
510 fnvlist_free(dsda.dsda_successful_snaps);
516 dsl_destroy_snapshot(const char *name, boolean_t defer)
519 nvlist_t *nvl = fnvlist_alloc();
520 nvlist_t *errlist = fnvlist_alloc();
522 fnvlist_add_boolean(nvl, name);
523 error = dsl_destroy_snapshots_nvl(nvl, defer, errlist);
524 fnvlist_free(errlist);
536 kill_blkptr(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
537 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
539 struct killarg *ka = arg;
540 dmu_tx_t *tx = ka->tx;
542 if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
545 if (zb->zb_level == ZB_ZIL_LEVEL) {
546 ASSERT(zilog != NULL);
548 * It's a block in the intent log. It has no
549 * accounting, so just free it.
551 dsl_free(ka->tx->tx_pool, ka->tx->tx_txg, bp);
553 ASSERT(zilog == NULL);
554 ASSERT3U(bp->blk_birth, >, ka->ds->ds_phys->ds_prev_snap_txg);
555 (void) dsl_dataset_block_kill(ka->ds, bp, tx, B_FALSE);
562 old_synchronous_dataset_destroy(dsl_dataset_t *ds, dmu_tx_t *tx)
567 * Free everything that we point to (that's born after
568 * the previous snapshot, if we are a clone)
570 * NB: this should be very quick, because we already
571 * freed all the objects in open context.
575 VERIFY0(traverse_dataset(ds,
576 ds->ds_phys->ds_prev_snap_txg, TRAVERSE_POST,
578 ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) || ds->ds_phys->ds_unique_bytes == 0);
581 typedef struct dsl_destroy_head_arg {
582 const char *ddha_name;
583 } dsl_destroy_head_arg_t;
586 dsl_destroy_head_check_impl(dsl_dataset_t *ds, int expected_holds)
592 ASSERT(!dsl_dataset_is_snapshot(ds));
593 if (dsl_dataset_is_snapshot(ds))
594 return (SET_ERROR(EINVAL));
596 if (refcount_count(&ds->ds_longholds) != expected_holds)
597 return (SET_ERROR(EBUSY));
599 mos = ds->ds_dir->dd_pool->dp_meta_objset;
602 * Can't delete a head dataset if there are snapshots of it.
603 * (Except if the only snapshots are from the branch we cloned
606 if (ds->ds_prev != NULL &&
607 ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object)
608 return (SET_ERROR(EBUSY));
611 * Can't delete if there are children of this fs.
613 error = zap_count(mos,
614 ds->ds_dir->dd_phys->dd_child_dir_zapobj, &count);
618 return (SET_ERROR(EEXIST));
620 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev) &&
621 ds->ds_prev->ds_phys->ds_num_children == 2 &&
622 ds->ds_prev->ds_userrefs == 0) {
623 /* We need to remove the origin snapshot as well. */
624 if (!refcount_is_zero(&ds->ds_prev->ds_longholds))
625 return (SET_ERROR(EBUSY));
631 dsl_destroy_head_check(void *arg, dmu_tx_t *tx)
633 dsl_destroy_head_arg_t *ddha = arg;
634 dsl_pool_t *dp = dmu_tx_pool(tx);
638 error = dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds);
642 error = dsl_destroy_head_check_impl(ds, 0);
643 dsl_dataset_rele(ds, FTAG);
648 dsl_dir_destroy_sync(uint64_t ddobj, dmu_tx_t *tx)
651 dsl_pool_t *dp = dmu_tx_pool(tx);
652 objset_t *mos = dp->dp_meta_objset;
655 ASSERT(RRW_WRITE_HELD(&dmu_tx_pool(tx)->dp_config_rwlock));
657 VERIFY0(dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd));
659 ASSERT0(dd->dd_phys->dd_head_dataset_obj);
662 * Decrement the filesystem count for all parent filesystems.
664 * When we receive an incremental stream into a filesystem that already
665 * exists, a temporary clone is created. We never count this temporary
666 * clone, whose name begins with a '%'.
668 if (dd->dd_myname[0] != '%' && dd->dd_parent != NULL)
669 dsl_fs_ss_count_adjust(dd->dd_parent, -1,
670 DD_FIELD_FILESYSTEM_COUNT, tx);
673 * Remove our reservation. The impl() routine avoids setting the
674 * actual property, which would require the (already destroyed) ds.
676 dsl_dir_set_reservation_sync_impl(dd, 0, tx);
678 ASSERT0(dd->dd_phys->dd_used_bytes);
679 ASSERT0(dd->dd_phys->dd_reserved);
680 for (t = 0; t < DD_USED_NUM; t++)
681 ASSERT0(dd->dd_phys->dd_used_breakdown[t]);
683 VERIFY0(zap_destroy(mos, dd->dd_phys->dd_child_dir_zapobj, tx));
684 VERIFY0(zap_destroy(mos, dd->dd_phys->dd_props_zapobj, tx));
685 VERIFY0(dsl_deleg_destroy(mos, dd->dd_phys->dd_deleg_zapobj, tx));
686 VERIFY0(zap_remove(mos,
687 dd->dd_parent->dd_phys->dd_child_dir_zapobj, dd->dd_myname, tx));
689 dsl_dir_rele(dd, FTAG);
690 dmu_object_free_zapified(mos, ddobj, tx);
694 dsl_destroy_head_sync_impl(dsl_dataset_t *ds, dmu_tx_t *tx)
696 dsl_pool_t *dp = dmu_tx_pool(tx);
697 objset_t *mos = dp->dp_meta_objset;
698 uint64_t obj, ddobj, prevobj = 0;
701 ASSERT3U(ds->ds_phys->ds_num_children, <=, 1);
702 ASSERT(ds->ds_prev == NULL ||
703 ds->ds_prev->ds_phys->ds_next_snap_obj != ds->ds_object);
704 ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg);
705 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
707 /* We need to log before removing it from the namespace. */
708 spa_history_log_internal_ds(ds, "destroy", tx, "");
710 rmorigin = (dsl_dir_is_clone(ds->ds_dir) &&
711 DS_IS_DEFER_DESTROY(ds->ds_prev) &&
712 ds->ds_prev->ds_phys->ds_num_children == 2 &&
713 ds->ds_prev->ds_userrefs == 0);
715 /* Remove our reservation. */
716 if (ds->ds_reserved != 0) {
717 dsl_dataset_set_refreservation_sync_impl(ds,
718 (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED),
720 ASSERT0(ds->ds_reserved);
723 dsl_scan_ds_destroyed(ds, tx);
727 if (ds->ds_phys->ds_prev_snap_obj != 0) {
728 /* This is a clone */
729 ASSERT(ds->ds_prev != NULL);
730 ASSERT3U(ds->ds_prev->ds_phys->ds_next_snap_obj, !=, obj);
731 ASSERT0(ds->ds_phys->ds_next_snap_obj);
733 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
734 if (ds->ds_prev->ds_phys->ds_next_clones_obj != 0) {
735 dsl_dataset_remove_from_next_clones(ds->ds_prev,
739 ASSERT3U(ds->ds_prev->ds_phys->ds_num_children, >, 1);
740 ds->ds_prev->ds_phys->ds_num_children--;
744 * Destroy the deadlist. Unless it's a clone, the
745 * deadlist should be empty. (If it's a clone, it's
746 * safe to ignore the deadlist contents.)
748 dsl_deadlist_close(&ds->ds_deadlist);
749 dsl_deadlist_free(mos, ds->ds_phys->ds_deadlist_obj, tx);
750 dmu_buf_will_dirty(ds->ds_dbuf, tx);
751 ds->ds_phys->ds_deadlist_obj = 0;
754 VERIFY0(dmu_objset_from_ds(ds, &os));
756 if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_ASYNC_DESTROY)) {
757 old_synchronous_dataset_destroy(ds, tx);
760 * Move the bptree into the pool's list of trees to
761 * clean up and update space accounting information.
763 uint64_t used, comp, uncomp;
765 zil_destroy_sync(dmu_objset_zil(os), tx);
767 if (!spa_feature_is_active(dp->dp_spa,
768 SPA_FEATURE_ASYNC_DESTROY)) {
769 dsl_scan_t *scn = dp->dp_scan;
770 spa_feature_incr(dp->dp_spa, SPA_FEATURE_ASYNC_DESTROY,
772 dp->dp_bptree_obj = bptree_alloc(mos, tx);
774 DMU_POOL_DIRECTORY_OBJECT,
775 DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1,
776 &dp->dp_bptree_obj, tx));
777 ASSERT(!scn->scn_async_destroying);
778 scn->scn_async_destroying = B_TRUE;
781 used = ds->ds_dir->dd_phys->dd_used_bytes;
782 comp = ds->ds_dir->dd_phys->dd_compressed_bytes;
783 uncomp = ds->ds_dir->dd_phys->dd_uncompressed_bytes;
785 ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) ||
786 ds->ds_phys->ds_unique_bytes == used);
788 bptree_add(mos, dp->dp_bptree_obj,
789 &ds->ds_phys->ds_bp, ds->ds_phys->ds_prev_snap_txg,
790 used, comp, uncomp, tx);
791 dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
792 -used, -comp, -uncomp, tx);
793 dsl_dir_diduse_space(dp->dp_free_dir, DD_USED_HEAD,
794 used, comp, uncomp, tx);
797 if (ds->ds_prev != NULL) {
798 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
799 VERIFY0(zap_remove_int(mos,
800 ds->ds_prev->ds_dir->dd_phys->dd_clones,
803 prevobj = ds->ds_prev->ds_object;
804 dsl_dataset_rele(ds->ds_prev, ds);
809 * This must be done after the dsl_traverse(), because it will
810 * re-open the objset.
813 dmu_objset_evict(ds->ds_objset);
814 ds->ds_objset = NULL;
817 /* Erase the link in the dir */
818 dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
819 ds->ds_dir->dd_phys->dd_head_dataset_obj = 0;
820 ddobj = ds->ds_dir->dd_object;
821 ASSERT(ds->ds_phys->ds_snapnames_zapobj != 0);
822 VERIFY0(zap_destroy(mos, ds->ds_phys->ds_snapnames_zapobj, tx));
824 if (ds->ds_bookmarks != 0) {
825 VERIFY0(zap_destroy(mos,
826 ds->ds_bookmarks, tx));
827 spa_feature_decr(dp->dp_spa, SPA_FEATURE_BOOKMARKS, tx);
830 spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
832 ASSERT0(ds->ds_phys->ds_next_clones_obj);
833 ASSERT0(ds->ds_phys->ds_props_obj);
834 ASSERT0(ds->ds_phys->ds_userrefs_obj);
835 dsl_dir_rele(ds->ds_dir, ds);
837 dmu_object_free_zapified(mos, obj, tx);
839 dsl_dir_destroy_sync(ddobj, tx);
843 VERIFY0(dsl_dataset_hold_obj(dp, prevobj, FTAG, &prev));
844 dsl_destroy_snapshot_sync_impl(prev, B_FALSE, tx);
845 dsl_dataset_rele(prev, FTAG);
850 dsl_destroy_head_sync(void *arg, dmu_tx_t *tx)
852 dsl_destroy_head_arg_t *ddha = arg;
853 dsl_pool_t *dp = dmu_tx_pool(tx);
856 VERIFY0(dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds));
857 dsl_destroy_head_sync_impl(ds, tx);
858 dsl_dataset_rele(ds, FTAG);
862 dsl_destroy_head_begin_sync(void *arg, dmu_tx_t *tx)
864 dsl_destroy_head_arg_t *ddha = arg;
865 dsl_pool_t *dp = dmu_tx_pool(tx);
868 VERIFY0(dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds));
870 /* Mark it as inconsistent on-disk, in case we crash */
871 dmu_buf_will_dirty(ds->ds_dbuf, tx);
872 ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT;
874 spa_history_log_internal_ds(ds, "destroy begin", tx, "");
875 dsl_dataset_rele(ds, FTAG);
879 dsl_destroy_head(const char *name)
881 dsl_destroy_head_arg_t ddha;
887 zfs_destroy_unmount_origin(name);
890 error = spa_open(name, &spa, FTAG);
893 isenabled = spa_feature_is_enabled(spa, SPA_FEATURE_ASYNC_DESTROY);
894 spa_close(spa, FTAG);
896 ddha.ddha_name = name;
901 error = dsl_sync_task(name, dsl_destroy_head_check,
902 dsl_destroy_head_begin_sync, &ddha, 0);
907 * Head deletion is processed in one txg on old pools;
908 * remove the objects from open context so that the txg sync
911 error = dmu_objset_own(name, DMU_OST_ANY, B_FALSE, FTAG, &os);
913 uint64_t prev_snap_txg =
914 dmu_objset_ds(os)->ds_phys->ds_prev_snap_txg;
915 for (uint64_t obj = 0; error == 0;
916 error = dmu_object_next(os, &obj, FALSE,
918 (void) dmu_free_long_object(os, obj);
919 /* sync out all frees */
920 txg_wait_synced(dmu_objset_pool(os), 0);
921 dmu_objset_disown(os, FTAG);
925 return (dsl_sync_task(name, dsl_destroy_head_check,
926 dsl_destroy_head_sync, &ddha, 0));
930 * Note, this function is used as the callback for dmu_objset_find(). We
931 * always return 0 so that we will continue to find and process
932 * inconsistent datasets, even if we encounter an error trying to
933 * process one of them.
937 dsl_destroy_inconsistent(const char *dsname, void *arg)
941 if (dmu_objset_hold(dsname, FTAG, &os) == 0) {
942 boolean_t inconsistent = DS_IS_INCONSISTENT(dmu_objset_ds(os));
943 dmu_objset_rele(os, FTAG);
945 (void) dsl_destroy_head(dsname);