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.
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_t *zb, const dnode_phys_t *dnp, void *arg)
539 struct killarg *ka = arg;
540 dmu_tx_t *tx = ka->tx;
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 if (dsl_dataset_is_snapshot(ds))
593 return (SET_ERROR(EINVAL));
595 if (refcount_count(&ds->ds_longholds) != expected_holds)
596 return (SET_ERROR(EBUSY));
598 mos = ds->ds_dir->dd_pool->dp_meta_objset;
601 * Can't delete a head dataset if there are snapshots of it.
602 * (Except if the only snapshots are from the branch we cloned
605 if (ds->ds_prev != NULL &&
606 ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object)
607 return (SET_ERROR(EBUSY));
610 * Can't delete if there are children of this fs.
612 error = zap_count(mos,
613 ds->ds_dir->dd_phys->dd_child_dir_zapobj, &count);
617 return (SET_ERROR(EEXIST));
619 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev) &&
620 ds->ds_prev->ds_phys->ds_num_children == 2 &&
621 ds->ds_prev->ds_userrefs == 0) {
622 /* We need to remove the origin snapshot as well. */
623 if (!refcount_is_zero(&ds->ds_prev->ds_longholds))
624 return (SET_ERROR(EBUSY));
630 dsl_destroy_head_check(void *arg, dmu_tx_t *tx)
632 dsl_destroy_head_arg_t *ddha = arg;
633 dsl_pool_t *dp = dmu_tx_pool(tx);
637 error = dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds);
641 error = dsl_destroy_head_check_impl(ds, 0);
642 dsl_dataset_rele(ds, FTAG);
647 dsl_dir_destroy_sync(uint64_t ddobj, dmu_tx_t *tx)
650 dsl_pool_t *dp = dmu_tx_pool(tx);
651 objset_t *mos = dp->dp_meta_objset;
654 ASSERT(RRW_WRITE_HELD(&dmu_tx_pool(tx)->dp_config_rwlock));
656 VERIFY0(dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd));
658 ASSERT0(dd->dd_phys->dd_head_dataset_obj);
661 * Decrement the filesystem count for all parent filesystems.
663 * When we receive an incremental stream into a filesystem that already
664 * exists, a temporary clone is created. We never count this temporary
665 * clone, whose name begins with a '%'.
667 if (dd->dd_myname[0] != '%' && dd->dd_parent != NULL)
668 dsl_fs_ss_count_adjust(dd->dd_parent, -1,
669 DD_FIELD_FILESYSTEM_COUNT, tx);
672 * Remove our reservation. The impl() routine avoids setting the
673 * actual property, which would require the (already destroyed) ds.
675 dsl_dir_set_reservation_sync_impl(dd, 0, tx);
677 ASSERT0(dd->dd_phys->dd_used_bytes);
678 ASSERT0(dd->dd_phys->dd_reserved);
679 for (t = 0; t < DD_USED_NUM; t++)
680 ASSERT0(dd->dd_phys->dd_used_breakdown[t]);
682 VERIFY0(zap_destroy(mos, dd->dd_phys->dd_child_dir_zapobj, tx));
683 VERIFY0(zap_destroy(mos, dd->dd_phys->dd_props_zapobj, tx));
684 VERIFY0(dsl_deleg_destroy(mos, dd->dd_phys->dd_deleg_zapobj, tx));
685 VERIFY0(zap_remove(mos,
686 dd->dd_parent->dd_phys->dd_child_dir_zapobj, dd->dd_myname, tx));
688 dsl_dir_rele(dd, FTAG);
689 dmu_object_free_zapified(mos, ddobj, tx);
693 dsl_destroy_head_sync_impl(dsl_dataset_t *ds, dmu_tx_t *tx)
695 dsl_pool_t *dp = dmu_tx_pool(tx);
696 objset_t *mos = dp->dp_meta_objset;
697 uint64_t obj, ddobj, prevobj = 0;
700 ASSERT3U(ds->ds_phys->ds_num_children, <=, 1);
701 ASSERT(ds->ds_prev == NULL ||
702 ds->ds_prev->ds_phys->ds_next_snap_obj != ds->ds_object);
703 ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg);
704 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
706 /* We need to log before removing it from the namespace. */
707 spa_history_log_internal_ds(ds, "destroy", tx, "");
709 rmorigin = (dsl_dir_is_clone(ds->ds_dir) &&
710 DS_IS_DEFER_DESTROY(ds->ds_prev) &&
711 ds->ds_prev->ds_phys->ds_num_children == 2 &&
712 ds->ds_prev->ds_userrefs == 0);
714 /* Remove our reservation */
715 if (ds->ds_reserved != 0) {
716 dsl_dataset_set_refreservation_sync_impl(ds,
717 (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED),
719 ASSERT0(ds->ds_reserved);
722 dsl_scan_ds_destroyed(ds, tx);
726 if (ds->ds_phys->ds_prev_snap_obj != 0) {
727 /* This is a clone */
728 ASSERT(ds->ds_prev != NULL);
729 ASSERT3U(ds->ds_prev->ds_phys->ds_next_snap_obj, !=, obj);
730 ASSERT0(ds->ds_phys->ds_next_snap_obj);
732 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
733 if (ds->ds_prev->ds_phys->ds_next_clones_obj != 0) {
734 dsl_dataset_remove_from_next_clones(ds->ds_prev,
738 ASSERT3U(ds->ds_prev->ds_phys->ds_num_children, >, 1);
739 ds->ds_prev->ds_phys->ds_num_children--;
743 * Destroy the deadlist. Unless it's a clone, the
744 * deadlist should be empty. (If it's a clone, it's
745 * safe to ignore the deadlist contents.)
747 dsl_deadlist_close(&ds->ds_deadlist);
748 dsl_deadlist_free(mos, ds->ds_phys->ds_deadlist_obj, tx);
749 dmu_buf_will_dirty(ds->ds_dbuf, tx);
750 ds->ds_phys->ds_deadlist_obj = 0;
753 VERIFY0(dmu_objset_from_ds(ds, &os));
755 if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_ASYNC_DESTROY)) {
756 old_synchronous_dataset_destroy(ds, tx);
759 * Move the bptree into the pool's list of trees to
760 * clean up and update space accounting information.
762 uint64_t used, comp, uncomp;
764 zil_destroy_sync(dmu_objset_zil(os), tx);
766 if (!spa_feature_is_active(dp->dp_spa,
767 SPA_FEATURE_ASYNC_DESTROY)) {
768 dsl_scan_t *scn = dp->dp_scan;
769 spa_feature_incr(dp->dp_spa, SPA_FEATURE_ASYNC_DESTROY,
771 dp->dp_bptree_obj = bptree_alloc(mos, tx);
773 DMU_POOL_DIRECTORY_OBJECT,
774 DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1,
775 &dp->dp_bptree_obj, tx));
776 ASSERT(!scn->scn_async_destroying);
777 scn->scn_async_destroying = B_TRUE;
780 used = ds->ds_dir->dd_phys->dd_used_bytes;
781 comp = ds->ds_dir->dd_phys->dd_compressed_bytes;
782 uncomp = ds->ds_dir->dd_phys->dd_uncompressed_bytes;
784 ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) ||
785 ds->ds_phys->ds_unique_bytes == used);
787 bptree_add(mos, dp->dp_bptree_obj,
788 &ds->ds_phys->ds_bp, ds->ds_phys->ds_prev_snap_txg,
789 used, comp, uncomp, tx);
790 dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
791 -used, -comp, -uncomp, tx);
792 dsl_dir_diduse_space(dp->dp_free_dir, DD_USED_HEAD,
793 used, comp, uncomp, tx);
796 if (ds->ds_prev != NULL) {
797 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
798 VERIFY0(zap_remove_int(mos,
799 ds->ds_prev->ds_dir->dd_phys->dd_clones,
802 prevobj = ds->ds_prev->ds_object;
803 dsl_dataset_rele(ds->ds_prev, ds);
808 * This must be done after the dsl_traverse(), because it will
809 * re-open the objset.
812 dmu_objset_evict(ds->ds_objset);
813 ds->ds_objset = NULL;
816 /* Erase the link in the dir */
817 dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
818 ds->ds_dir->dd_phys->dd_head_dataset_obj = 0;
819 ddobj = ds->ds_dir->dd_object;
820 ASSERT(ds->ds_phys->ds_snapnames_zapobj != 0);
821 VERIFY0(zap_destroy(mos, ds->ds_phys->ds_snapnames_zapobj, tx));
823 if (ds->ds_bookmarks != 0) {
824 VERIFY0(zap_destroy(mos,
825 ds->ds_bookmarks, tx));
826 spa_feature_decr(dp->dp_spa, SPA_FEATURE_BOOKMARKS, tx);
829 spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
831 ASSERT0(ds->ds_phys->ds_next_clones_obj);
832 ASSERT0(ds->ds_phys->ds_props_obj);
833 ASSERT0(ds->ds_phys->ds_userrefs_obj);
834 dsl_dir_rele(ds->ds_dir, ds);
836 dmu_object_free_zapified(mos, obj, tx);
838 dsl_dir_destroy_sync(ddobj, tx);
842 VERIFY0(dsl_dataset_hold_obj(dp, prevobj, FTAG, &prev));
843 dsl_destroy_snapshot_sync_impl(prev, B_FALSE, tx);
844 dsl_dataset_rele(prev, FTAG);
849 dsl_destroy_head_sync(void *arg, dmu_tx_t *tx)
851 dsl_destroy_head_arg_t *ddha = arg;
852 dsl_pool_t *dp = dmu_tx_pool(tx);
855 VERIFY0(dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds));
856 dsl_destroy_head_sync_impl(ds, tx);
857 dsl_dataset_rele(ds, FTAG);
861 dsl_destroy_head_begin_sync(void *arg, dmu_tx_t *tx)
863 dsl_destroy_head_arg_t *ddha = arg;
864 dsl_pool_t *dp = dmu_tx_pool(tx);
867 VERIFY0(dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds));
869 /* Mark it as inconsistent on-disk, in case we crash */
870 dmu_buf_will_dirty(ds->ds_dbuf, tx);
871 ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT;
873 spa_history_log_internal_ds(ds, "destroy begin", tx, "");
874 dsl_dataset_rele(ds, FTAG);
878 dsl_destroy_head(const char *name)
880 dsl_destroy_head_arg_t ddha;
886 zfs_destroy_unmount_origin(name);
889 error = spa_open(name, &spa, FTAG);
892 isenabled = spa_feature_is_enabled(spa, SPA_FEATURE_ASYNC_DESTROY);
893 spa_close(spa, FTAG);
895 ddha.ddha_name = name;
900 error = dsl_sync_task(name, dsl_destroy_head_check,
901 dsl_destroy_head_begin_sync, &ddha, 0);
906 * Head deletion is processed in one txg on old pools;
907 * remove the objects from open context so that the txg sync
910 error = dmu_objset_own(name, DMU_OST_ANY, B_FALSE, FTAG, &os);
912 uint64_t prev_snap_txg =
913 dmu_objset_ds(os)->ds_phys->ds_prev_snap_txg;
914 for (uint64_t obj = 0; error == 0;
915 error = dmu_object_next(os, &obj, FALSE,
917 (void) dmu_free_long_object(os, obj);
918 /* sync out all frees */
919 txg_wait_synced(dmu_objset_pool(os), 0);
920 dmu_objset_disown(os, FTAG);
924 return (dsl_sync_task(name, dsl_destroy_head_check,
925 dsl_destroy_head_sync, &ddha, 0));
929 * Note, this function is used as the callback for dmu_objset_find(). We
930 * always return 0 so that we will continue to find and process
931 * inconsistent datasets, even if we encounter an error trying to
932 * process one of them.
936 dsl_destroy_inconsistent(const char *dsname, void *arg)
940 if (dmu_objset_hold(dsname, FTAG, &os) == 0) {
941 boolean_t inconsistent = DS_IS_INCONSISTENT(dmu_objset_ds(os));
942 dmu_objset_rele(os, FTAG);
944 (void) dsl_destroy_head(dsname);