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 zc = kmem_alloc(sizeof (zap_cursor_t), KM_PUSHPAGE);
215 za = kmem_alloc(sizeof (zap_attribute_t), KM_PUSHPAGE);
217 for (zap_cursor_init(zc, mos, ds->ds_dir->dd_phys->dd_clones);
218 zap_cursor_retrieve(zc, za) == 0;
219 zap_cursor_advance(zc)) {
220 dsl_dataset_t *clone;
222 VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
223 za->za_first_integer, FTAG, &clone));
224 if (clone->ds_dir->dd_origin_txg > mintxg) {
225 dsl_deadlist_remove_key(&clone->ds_deadlist,
227 dsl_dataset_remove_clones_key(clone, mintxg, tx);
229 dsl_dataset_rele(clone, FTAG);
233 kmem_free(za, sizeof (zap_attribute_t));
234 kmem_free(zc, sizeof (zap_cursor_t));
238 dsl_destroy_snapshot_sync_impl(dsl_dataset_t *ds, boolean_t defer, dmu_tx_t *tx)
243 int after_branch_point = FALSE;
244 dsl_pool_t *dp = ds->ds_dir->dd_pool;
245 objset_t *mos = dp->dp_meta_objset;
246 dsl_dataset_t *ds_prev = NULL;
247 uint64_t obj, old_unique, used = 0, comp = 0, uncomp = 0;
248 dsl_dataset_t *ds_next, *ds_head, *hds;
251 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
252 ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg);
253 ASSERT(refcount_is_zero(&ds->ds_longholds));
256 (ds->ds_userrefs > 0 || ds->ds_phys->ds_num_children > 1)) {
257 ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
258 dmu_buf_will_dirty(ds->ds_dbuf, tx);
259 ds->ds_phys->ds_flags |= DS_FLAG_DEFER_DESTROY;
260 spa_history_log_internal_ds(ds, "defer_destroy", tx, "");
264 ASSERT3U(ds->ds_phys->ds_num_children, <=, 1);
266 /* We need to log before removing it from the namespace. */
267 spa_history_log_internal_ds(ds, "destroy", tx, "");
269 dsl_scan_ds_destroyed(ds, tx);
273 if (ds->ds_phys->ds_prev_snap_obj != 0) {
274 ASSERT3P(ds->ds_prev, ==, NULL);
275 VERIFY0(dsl_dataset_hold_obj(dp,
276 ds->ds_phys->ds_prev_snap_obj, FTAG, &ds_prev));
278 (ds_prev->ds_phys->ds_next_snap_obj != obj);
280 dmu_buf_will_dirty(ds_prev->ds_dbuf, tx);
281 if (after_branch_point &&
282 ds_prev->ds_phys->ds_next_clones_obj != 0) {
283 dsl_dataset_remove_from_next_clones(ds_prev, obj, tx);
284 if (ds->ds_phys->ds_next_snap_obj != 0) {
285 VERIFY0(zap_add_int(mos,
286 ds_prev->ds_phys->ds_next_clones_obj,
287 ds->ds_phys->ds_next_snap_obj, tx));
290 if (!after_branch_point) {
291 ds_prev->ds_phys->ds_next_snap_obj =
292 ds->ds_phys->ds_next_snap_obj;
296 VERIFY0(dsl_dataset_hold_obj(dp,
297 ds->ds_phys->ds_next_snap_obj, FTAG, &ds_next));
298 ASSERT3U(ds_next->ds_phys->ds_prev_snap_obj, ==, obj);
300 old_unique = ds_next->ds_phys->ds_unique_bytes;
302 dmu_buf_will_dirty(ds_next->ds_dbuf, tx);
303 ds_next->ds_phys->ds_prev_snap_obj =
304 ds->ds_phys->ds_prev_snap_obj;
305 ds_next->ds_phys->ds_prev_snap_txg =
306 ds->ds_phys->ds_prev_snap_txg;
307 ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
308 ds_prev ? ds_prev->ds_phys->ds_creation_txg : 0);
310 if (ds_next->ds_deadlist.dl_oldfmt) {
311 process_old_deadlist(ds, ds_prev, ds_next,
312 after_branch_point, tx);
314 /* Adjust prev's unique space. */
315 if (ds_prev && !after_branch_point) {
316 dsl_deadlist_space_range(&ds_next->ds_deadlist,
317 ds_prev->ds_phys->ds_prev_snap_txg,
318 ds->ds_phys->ds_prev_snap_txg,
319 &used, &comp, &uncomp);
320 ds_prev->ds_phys->ds_unique_bytes += used;
323 /* Adjust snapused. */
324 dsl_deadlist_space_range(&ds_next->ds_deadlist,
325 ds->ds_phys->ds_prev_snap_txg, UINT64_MAX,
326 &used, &comp, &uncomp);
327 dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
328 -used, -comp, -uncomp, tx);
330 /* Move blocks to be freed to pool's free list. */
331 dsl_deadlist_move_bpobj(&ds_next->ds_deadlist,
332 &dp->dp_free_bpobj, ds->ds_phys->ds_prev_snap_txg,
334 dsl_dir_diduse_space(tx->tx_pool->dp_free_dir,
335 DD_USED_HEAD, used, comp, uncomp, tx);
337 /* Merge our deadlist into next's and free it. */
338 dsl_deadlist_merge(&ds_next->ds_deadlist,
339 ds->ds_phys->ds_deadlist_obj, tx);
341 dsl_deadlist_close(&ds->ds_deadlist);
342 dsl_deadlist_free(mos, ds->ds_phys->ds_deadlist_obj, tx);
343 dmu_buf_will_dirty(ds->ds_dbuf, tx);
344 ds->ds_phys->ds_deadlist_obj = 0;
346 /* Collapse range in clone heads */
347 dsl_dataset_remove_clones_key(ds,
348 ds->ds_phys->ds_creation_txg, tx);
350 if (dsl_dataset_is_snapshot(ds_next)) {
351 dsl_dataset_t *ds_nextnext;
354 * Update next's unique to include blocks which
355 * were previously shared by only this snapshot
356 * and it. Those blocks will be born after the
357 * prev snap and before this snap, and will have
358 * died after the next snap and before the one
359 * after that (ie. be on the snap after next's
362 VERIFY0(dsl_dataset_hold_obj(dp,
363 ds_next->ds_phys->ds_next_snap_obj, FTAG, &ds_nextnext));
364 dsl_deadlist_space_range(&ds_nextnext->ds_deadlist,
365 ds->ds_phys->ds_prev_snap_txg,
366 ds->ds_phys->ds_creation_txg,
367 &used, &comp, &uncomp);
368 ds_next->ds_phys->ds_unique_bytes += used;
369 dsl_dataset_rele(ds_nextnext, FTAG);
370 ASSERT3P(ds_next->ds_prev, ==, NULL);
372 /* Collapse range in this head. */
373 VERIFY0(dsl_dataset_hold_obj(dp,
374 ds->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &hds));
375 dsl_deadlist_remove_key(&hds->ds_deadlist,
376 ds->ds_phys->ds_creation_txg, tx);
377 dsl_dataset_rele(hds, FTAG);
380 ASSERT3P(ds_next->ds_prev, ==, ds);
381 dsl_dataset_rele(ds_next->ds_prev, ds_next);
382 ds_next->ds_prev = NULL;
384 VERIFY0(dsl_dataset_hold_obj(dp,
385 ds->ds_phys->ds_prev_snap_obj,
386 ds_next, &ds_next->ds_prev));
389 dsl_dataset_recalc_head_uniq(ds_next);
392 * Reduce the amount of our unconsumed refreservation
393 * being charged to our parent by the amount of
394 * new unique data we have gained.
396 if (old_unique < ds_next->ds_reserved) {
398 uint64_t new_unique =
399 ds_next->ds_phys->ds_unique_bytes;
401 ASSERT(old_unique <= new_unique);
402 mrsdelta = MIN(new_unique - old_unique,
403 ds_next->ds_reserved - old_unique);
404 dsl_dir_diduse_space(ds->ds_dir,
405 DD_USED_REFRSRV, -mrsdelta, 0, 0, tx);
408 dsl_dataset_rele(ds_next, FTAG);
411 * This must be done after the dsl_traverse(), because it will
412 * re-open the objset.
415 dmu_objset_evict(ds->ds_objset);
416 ds->ds_objset = NULL;
419 /* remove from snapshot namespace */
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));
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) {
443 ASSERTV(uint64_t count);
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 VERIFY0(dmu_object_free(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 VERIFY0(nvlist_alloc(&dsda.dsda_successful_snaps, NV_UNIQUE_NAME, KM_PUSHPAGE));
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)
522 VERIFY0(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_PUSHPAGE));
523 VERIFY0(nvlist_alloc(&errlist, NV_UNIQUE_NAME, KM_PUSHPAGE));
525 fnvlist_add_boolean(nvl, name);
526 error = dsl_destroy_snapshots_nvl(nvl, defer, errlist);
527 fnvlist_free(errlist);
539 kill_blkptr(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
540 const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
542 struct killarg *ka = arg;
543 dmu_tx_t *tx = ka->tx;
548 if (zb->zb_level == ZB_ZIL_LEVEL) {
549 ASSERT(zilog != NULL);
551 * It's a block in the intent log. It has no
552 * accounting, so just free it.
554 dsl_free(ka->tx->tx_pool, ka->tx->tx_txg, bp);
556 ASSERT(zilog == NULL);
557 ASSERT3U(bp->blk_birth, >, ka->ds->ds_phys->ds_prev_snap_txg);
558 (void) dsl_dataset_block_kill(ka->ds, bp, tx, B_FALSE);
565 old_synchronous_dataset_destroy(dsl_dataset_t *ds, dmu_tx_t *tx)
570 * Free everything that we point to (that's born after
571 * the previous snapshot, if we are a clone)
573 * NB: this should be very quick, because we already
574 * freed all the objects in open context.
578 VERIFY0(traverse_dataset(ds,
579 ds->ds_phys->ds_prev_snap_txg, TRAVERSE_POST,
581 ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) || ds->ds_phys->ds_unique_bytes == 0);
584 typedef struct dsl_destroy_head_arg {
585 const char *ddha_name;
586 } dsl_destroy_head_arg_t;
589 dsl_destroy_head_check_impl(dsl_dataset_t *ds, int expected_holds)
595 if (dsl_dataset_is_snapshot(ds))
596 return (SET_ERROR(EINVAL));
598 if (refcount_count(&ds->ds_longholds) != expected_holds)
599 return (SET_ERROR(EBUSY));
601 mos = ds->ds_dir->dd_pool->dp_meta_objset;
604 * Can't delete a head dataset if there are snapshots of it.
605 * (Except if the only snapshots are from the branch we cloned
608 if (ds->ds_prev != NULL &&
609 ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object)
610 return (SET_ERROR(EBUSY));
613 * Can't delete if there are children of this fs.
615 error = zap_count(mos,
616 ds->ds_dir->dd_phys->dd_child_dir_zapobj, &count);
620 return (SET_ERROR(EEXIST));
622 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev) &&
623 ds->ds_prev->ds_phys->ds_num_children == 2 &&
624 ds->ds_prev->ds_userrefs == 0) {
625 /* We need to remove the origin snapshot as well. */
626 if (!refcount_is_zero(&ds->ds_prev->ds_longholds))
627 return (SET_ERROR(EBUSY));
633 dsl_destroy_head_check(void *arg, dmu_tx_t *tx)
635 dsl_destroy_head_arg_t *ddha = arg;
636 dsl_pool_t *dp = dmu_tx_pool(tx);
640 error = dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds);
644 error = dsl_destroy_head_check_impl(ds, 0);
645 dsl_dataset_rele(ds, FTAG);
650 dsl_dir_destroy_sync(uint64_t ddobj, dmu_tx_t *tx)
653 dsl_pool_t *dp = dmu_tx_pool(tx);
654 objset_t *mos = dp->dp_meta_objset;
657 ASSERT(RRW_WRITE_HELD(&dmu_tx_pool(tx)->dp_config_rwlock));
659 VERIFY0(dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd));
661 ASSERT0(dd->dd_phys->dd_head_dataset_obj);
664 * Remove our reservation. The impl() routine avoids setting the
665 * actual property, which would require the (already destroyed) ds.
667 dsl_dir_set_reservation_sync_impl(dd, 0, tx);
669 ASSERT0(dd->dd_phys->dd_used_bytes);
670 ASSERT0(dd->dd_phys->dd_reserved);
671 for (t = 0; t < DD_USED_NUM; t++)
672 ASSERT0(dd->dd_phys->dd_used_breakdown[t]);
674 VERIFY0(zap_destroy(mos, dd->dd_phys->dd_child_dir_zapobj, tx));
675 VERIFY0(zap_destroy(mos, dd->dd_phys->dd_props_zapobj, tx));
676 VERIFY0(dsl_deleg_destroy(mos, dd->dd_phys->dd_deleg_zapobj, tx));
677 VERIFY0(zap_remove(mos,
678 dd->dd_parent->dd_phys->dd_child_dir_zapobj, dd->dd_myname, tx));
680 dsl_dir_rele(dd, FTAG);
681 VERIFY0(dmu_object_free(mos, ddobj, tx));
685 dsl_destroy_head_sync_impl(dsl_dataset_t *ds, dmu_tx_t *tx)
687 dsl_pool_t *dp = dmu_tx_pool(tx);
688 objset_t *mos = dp->dp_meta_objset;
689 uint64_t obj, ddobj, prevobj = 0;
691 zfeature_info_t *async_destroy;
694 ASSERT3U(ds->ds_phys->ds_num_children, <=, 1);
695 ASSERT(ds->ds_prev == NULL ||
696 ds->ds_prev->ds_phys->ds_next_snap_obj != ds->ds_object);
697 ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg);
698 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
700 /* We need to log before removing it from the namespace. */
701 spa_history_log_internal_ds(ds, "destroy", tx, "");
703 rmorigin = (dsl_dir_is_clone(ds->ds_dir) &&
704 DS_IS_DEFER_DESTROY(ds->ds_prev) &&
705 ds->ds_prev->ds_phys->ds_num_children == 2 &&
706 ds->ds_prev->ds_userrefs == 0);
708 /* Remove our reservation */
709 if (ds->ds_reserved != 0) {
710 dsl_dataset_set_refreservation_sync_impl(ds,
711 (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED),
713 ASSERT0(ds->ds_reserved);
716 dsl_scan_ds_destroyed(ds, tx);
720 if (ds->ds_phys->ds_prev_snap_obj != 0) {
721 /* This is a clone */
722 ASSERT(ds->ds_prev != NULL);
723 ASSERT3U(ds->ds_prev->ds_phys->ds_next_snap_obj, !=, obj);
724 ASSERT0(ds->ds_phys->ds_next_snap_obj);
726 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
727 if (ds->ds_prev->ds_phys->ds_next_clones_obj != 0) {
728 dsl_dataset_remove_from_next_clones(ds->ds_prev,
732 ASSERT3U(ds->ds_prev->ds_phys->ds_num_children, >, 1);
733 ds->ds_prev->ds_phys->ds_num_children--;
737 &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY];
740 * Destroy the deadlist. Unless it's a clone, the
741 * deadlist should be empty. (If it's a clone, it's
742 * safe to ignore the deadlist contents.)
744 dsl_deadlist_close(&ds->ds_deadlist);
745 dsl_deadlist_free(mos, ds->ds_phys->ds_deadlist_obj, tx);
746 dmu_buf_will_dirty(ds->ds_dbuf, tx);
747 ds->ds_phys->ds_deadlist_obj = 0;
749 VERIFY0(dmu_objset_from_ds(ds, &os));
751 if (!spa_feature_is_enabled(dp->dp_spa, async_destroy)) {
752 old_synchronous_dataset_destroy(ds, tx);
755 * Move the bptree into the pool's list of trees to
756 * clean up and update space accounting information.
758 uint64_t used, comp, uncomp;
760 zil_destroy_sync(dmu_objset_zil(os), tx);
762 if (!spa_feature_is_active(dp->dp_spa, async_destroy)) {
763 dsl_scan_t *scn = dp->dp_scan;
765 spa_feature_incr(dp->dp_spa, async_destroy, tx);
766 dp->dp_bptree_obj = bptree_alloc(mos, tx);
768 DMU_POOL_DIRECTORY_OBJECT,
769 DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1,
770 &dp->dp_bptree_obj, tx));
771 ASSERT(!scn->scn_async_destroying);
772 scn->scn_async_destroying = B_TRUE;
775 used = ds->ds_dir->dd_phys->dd_used_bytes;
776 comp = ds->ds_dir->dd_phys->dd_compressed_bytes;
777 uncomp = ds->ds_dir->dd_phys->dd_uncompressed_bytes;
779 ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) ||
780 ds->ds_phys->ds_unique_bytes == used);
782 bptree_add(mos, dp->dp_bptree_obj,
783 &ds->ds_phys->ds_bp, ds->ds_phys->ds_prev_snap_txg,
784 used, comp, uncomp, tx);
785 dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
786 -used, -comp, -uncomp, tx);
787 dsl_dir_diduse_space(dp->dp_free_dir, DD_USED_HEAD,
788 used, comp, uncomp, tx);
791 if (ds->ds_prev != NULL) {
792 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
793 VERIFY0(zap_remove_int(mos,
794 ds->ds_prev->ds_dir->dd_phys->dd_clones,
797 prevobj = ds->ds_prev->ds_object;
798 dsl_dataset_rele(ds->ds_prev, ds);
803 * This must be done after the dsl_traverse(), because it will
804 * re-open the objset.
807 dmu_objset_evict(ds->ds_objset);
808 ds->ds_objset = NULL;
811 /* Erase the link in the dir */
812 dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
813 ds->ds_dir->dd_phys->dd_head_dataset_obj = 0;
814 ddobj = ds->ds_dir->dd_object;
815 ASSERT(ds->ds_phys->ds_snapnames_zapobj != 0);
816 VERIFY0(zap_destroy(mos, ds->ds_phys->ds_snapnames_zapobj, tx));
818 spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
820 ASSERT0(ds->ds_phys->ds_next_clones_obj);
821 ASSERT0(ds->ds_phys->ds_props_obj);
822 ASSERT0(ds->ds_phys->ds_userrefs_obj);
823 dsl_dir_rele(ds->ds_dir, ds);
825 VERIFY0(dmu_object_free(mos, obj, tx));
827 dsl_dir_destroy_sync(ddobj, tx);
831 VERIFY0(dsl_dataset_hold_obj(dp, prevobj, FTAG, &prev));
832 dsl_destroy_snapshot_sync_impl(prev, B_FALSE, tx);
833 dsl_dataset_rele(prev, FTAG);
838 dsl_destroy_head_sync(void *arg, dmu_tx_t *tx)
840 dsl_destroy_head_arg_t *ddha = arg;
841 dsl_pool_t *dp = dmu_tx_pool(tx);
844 VERIFY0(dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds));
845 dsl_destroy_head_sync_impl(ds, tx);
846 dsl_dataset_rele(ds, FTAG);
850 dsl_destroy_head_begin_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));
858 /* Mark it as inconsistent on-disk, in case we crash */
859 dmu_buf_will_dirty(ds->ds_dbuf, tx);
860 ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT;
862 spa_history_log_internal_ds(ds, "destroy begin", tx, "");
863 dsl_dataset_rele(ds, FTAG);
867 dsl_destroy_head(const char *name)
869 dsl_destroy_head_arg_t ddha;
875 zfs_destroy_unmount_origin(name);
878 error = spa_open(name, &spa, FTAG);
881 isenabled = spa_feature_is_enabled(spa,
882 &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY]);
883 spa_close(spa, FTAG);
885 ddha.ddha_name = name;
890 error = dsl_sync_task(name, dsl_destroy_head_check,
891 dsl_destroy_head_begin_sync, &ddha, 0);
896 * Head deletion is processed in one txg on old pools;
897 * remove the objects from open context so that the txg sync
900 error = dmu_objset_own(name, DMU_OST_ANY, B_FALSE, FTAG, &os);
903 uint64_t prev_snap_txg =
904 dmu_objset_ds(os)->ds_phys->ds_prev_snap_txg;
905 for (obj = 0; error == 0;
906 error = dmu_object_next(os, &obj, FALSE,
908 (void) dmu_free_long_object(os, obj);
909 /* sync out all frees */
910 txg_wait_synced(dmu_objset_pool(os), 0);
911 dmu_objset_disown(os, FTAG);
915 return (dsl_sync_task(name, dsl_destroy_head_check,
916 dsl_destroy_head_sync, &ddha, 0));
920 * Note, this function is used as the callback for dmu_objset_find(). We
921 * always return 0 so that we will continue to find and process
922 * inconsistent datasets, even if we encounter an error trying to
923 * process one of them.
927 dsl_destroy_inconsistent(const char *dsname, void *arg)
931 if (dmu_objset_hold(dsname, FTAG, &os) == 0) {
932 boolean_t inconsistent = DS_IS_INCONSISTENT(dmu_objset_ds(os));
933 dmu_objset_rele(os, FTAG);
935 (void) dsl_destroy_head(dsname);
941 #if defined(_KERNEL) && defined(HAVE_SPL)
942 EXPORT_SYMBOL(dsl_destroy_head);
943 EXPORT_SYMBOL(dsl_destroy_head_sync_impl);
944 EXPORT_SYMBOL(dsl_dataset_user_hold_check_one);
945 EXPORT_SYMBOL(dsl_destroy_snapshot_sync_impl);
946 EXPORT_SYMBOL(dsl_destroy_inconsistent);
947 EXPORT_SYMBOL(dsl_dataset_user_release_tmp);
948 EXPORT_SYMBOL(dsl_destroy_head_check_impl);