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, 2017 by Delphix. All rights reserved.
24 * Copyright (c) 2013 Steven Hartland. All rights reserved.
25 * Copyright (c) 2013 by Joyent, Inc. All rights reserved.
26 * Copyright (c) 2014 Integros [integros.com]
29 #include <sys/zfs_context.h>
30 #include <sys/dsl_userhold.h>
31 #include <sys/dsl_dataset.h>
32 #include <sys/dsl_synctask.h>
33 #include <sys/dsl_destroy.h>
34 #include <sys/dmu_tx.h>
35 #include <sys/dsl_pool.h>
36 #include <sys/dsl_dir.h>
37 #include <sys/dmu_traverse.h>
38 #include <sys/dsl_scan.h>
39 #include <sys/dmu_objset.h>
41 #include <sys/zfeature.h>
42 #include <sys/zfs_ioctl.h>
43 #include <sys/dsl_deleg.h>
44 #include <sys/dmu_impl.h>
46 #if defined(__FreeBSD__) && defined(_KERNEL)
52 dsl_destroy_snapshot_check_impl(dsl_dataset_t *ds, boolean_t defer)
54 if (!ds->ds_is_snapshot)
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 (dsl_dataset_phys(ds)->ds_num_children > 1)
82 return (SET_ERROR(EEXIST));
88 dsl_destroy_snapshot_check(void *arg, dmu_tx_t *tx)
90 dsl_destroy_snapshot_arg_t *ddsa = arg;
91 const char *dsname = ddsa->ddsa_name;
92 boolean_t defer = ddsa->ddsa_defer;
94 dsl_pool_t *dp = dmu_tx_pool(tx);
98 error = dsl_dataset_hold(dp, dsname, FTAG, &ds);
101 * If the snapshot does not exist, silently ignore it, and
102 * dsl_destroy_snapshot_sync() will be a no-op
103 * (it's "already destroyed").
109 error = dsl_destroy_snapshot_check_impl(ds, defer);
110 dsl_dataset_rele(ds, FTAG);
116 struct process_old_arg {
118 dsl_dataset_t *ds_prev;
119 boolean_t after_branch_point;
121 uint64_t used, comp, uncomp;
125 process_old_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
127 struct process_old_arg *poa = arg;
128 dsl_pool_t *dp = poa->ds->ds_dir->dd_pool;
130 ASSERT(!BP_IS_HOLE(bp));
132 if (bp->blk_birth <= dsl_dataset_phys(poa->ds)->ds_prev_snap_txg) {
133 dsl_deadlist_insert(&poa->ds->ds_deadlist, bp, tx);
134 if (poa->ds_prev && !poa->after_branch_point &&
136 dsl_dataset_phys(poa->ds_prev)->ds_prev_snap_txg) {
137 dsl_dataset_phys(poa->ds_prev)->ds_unique_bytes +=
138 bp_get_dsize_sync(dp->dp_spa, bp);
141 poa->used += bp_get_dsize_sync(dp->dp_spa, bp);
142 poa->comp += BP_GET_PSIZE(bp);
143 poa->uncomp += BP_GET_UCSIZE(bp);
144 dsl_free_sync(poa->pio, dp, tx->tx_txg, bp);
150 process_old_deadlist(dsl_dataset_t *ds, dsl_dataset_t *ds_prev,
151 dsl_dataset_t *ds_next, boolean_t after_branch_point, dmu_tx_t *tx)
153 struct process_old_arg poa = { 0 };
154 dsl_pool_t *dp = ds->ds_dir->dd_pool;
155 objset_t *mos = dp->dp_meta_objset;
156 uint64_t deadlist_obj;
158 ASSERT(ds->ds_deadlist.dl_oldfmt);
159 ASSERT(ds_next->ds_deadlist.dl_oldfmt);
162 poa.ds_prev = ds_prev;
163 poa.after_branch_point = after_branch_point;
164 poa.pio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
165 VERIFY0(bpobj_iterate(&ds_next->ds_deadlist.dl_bpobj,
166 process_old_cb, &poa, tx));
167 VERIFY0(zio_wait(poa.pio));
168 ASSERT3U(poa.used, ==, dsl_dataset_phys(ds)->ds_unique_bytes);
170 /* change snapused */
171 dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
172 -poa.used, -poa.comp, -poa.uncomp, tx);
174 /* swap next's deadlist to our deadlist */
175 dsl_deadlist_close(&ds->ds_deadlist);
176 dsl_deadlist_close(&ds_next->ds_deadlist);
177 deadlist_obj = dsl_dataset_phys(ds)->ds_deadlist_obj;
178 dsl_dataset_phys(ds)->ds_deadlist_obj =
179 dsl_dataset_phys(ds_next)->ds_deadlist_obj;
180 dsl_dataset_phys(ds_next)->ds_deadlist_obj = deadlist_obj;
181 dsl_deadlist_open(&ds->ds_deadlist, mos,
182 dsl_dataset_phys(ds)->ds_deadlist_obj);
183 dsl_deadlist_open(&ds_next->ds_deadlist, mos,
184 dsl_dataset_phys(ds_next)->ds_deadlist_obj);
188 dsl_dataset_remove_clones_key(dsl_dataset_t *ds, uint64_t mintxg, dmu_tx_t *tx)
190 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
195 * If it is the old version, dd_clones doesn't exist so we can't
196 * find the clones, but dsl_deadlist_remove_key() is a no-op so it
199 if (dsl_dir_phys(ds->ds_dir)->dd_clones == 0)
202 for (zap_cursor_init(&zc, mos, dsl_dir_phys(ds->ds_dir)->dd_clones);
203 zap_cursor_retrieve(&zc, &za) == 0;
204 zap_cursor_advance(&zc)) {
205 dsl_dataset_t *clone;
207 VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
208 za.za_first_integer, FTAG, &clone));
209 if (clone->ds_dir->dd_origin_txg > mintxg) {
210 dsl_deadlist_remove_key(&clone->ds_deadlist,
212 if (dsl_dataset_remap_deadlist_exists(clone)) {
213 dsl_deadlist_remove_key(
214 &clone->ds_remap_deadlist, mintxg, tx);
216 dsl_dataset_remove_clones_key(clone, mintxg, tx);
218 dsl_dataset_rele(clone, FTAG);
220 zap_cursor_fini(&zc);
224 dsl_destroy_snapshot_handle_remaps(dsl_dataset_t *ds, dsl_dataset_t *ds_next,
227 dsl_pool_t *dp = ds->ds_dir->dd_pool;
229 /* Move blocks to be obsoleted to pool's obsolete list. */
230 if (dsl_dataset_remap_deadlist_exists(ds_next)) {
231 if (!bpobj_is_open(&dp->dp_obsolete_bpobj))
232 dsl_pool_create_obsolete_bpobj(dp, tx);
234 dsl_deadlist_move_bpobj(&ds_next->ds_remap_deadlist,
235 &dp->dp_obsolete_bpobj,
236 dsl_dataset_phys(ds)->ds_prev_snap_txg, tx);
239 /* Merge our deadlist into next's and free it. */
240 if (dsl_dataset_remap_deadlist_exists(ds)) {
241 uint64_t remap_deadlist_object =
242 dsl_dataset_get_remap_deadlist_object(ds);
243 ASSERT(remap_deadlist_object != 0);
245 mutex_enter(&ds_next->ds_remap_deadlist_lock);
246 if (!dsl_dataset_remap_deadlist_exists(ds_next))
247 dsl_dataset_create_remap_deadlist(ds_next, tx);
248 mutex_exit(&ds_next->ds_remap_deadlist_lock);
250 dsl_deadlist_merge(&ds_next->ds_remap_deadlist,
251 remap_deadlist_object, tx);
252 dsl_dataset_destroy_remap_deadlist(ds, tx);
257 dsl_destroy_snapshot_sync_impl(dsl_dataset_t *ds, boolean_t defer, dmu_tx_t *tx)
260 int after_branch_point = FALSE;
261 dsl_pool_t *dp = ds->ds_dir->dd_pool;
262 objset_t *mos = dp->dp_meta_objset;
263 dsl_dataset_t *ds_prev = NULL;
266 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
267 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
268 ASSERT3U(dsl_dataset_phys(ds)->ds_bp.blk_birth, <=, tx->tx_txg);
269 rrw_exit(&ds->ds_bp_rwlock, FTAG);
270 ASSERT(zfs_refcount_is_zero(&ds->ds_longholds));
273 (ds->ds_userrefs > 0 ||
274 dsl_dataset_phys(ds)->ds_num_children > 1)) {
275 ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
276 dmu_buf_will_dirty(ds->ds_dbuf, tx);
277 dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_DEFER_DESTROY;
278 spa_history_log_internal_ds(ds, "defer_destroy", tx, "");
282 ASSERT3U(dsl_dataset_phys(ds)->ds_num_children, <=, 1);
284 /* We need to log before removing it from the namespace. */
285 spa_history_log_internal_ds(ds, "destroy", tx, "");
287 dsl_scan_ds_destroyed(ds, tx);
291 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
292 if (ds->ds_feature_inuse[f]) {
293 dsl_dataset_deactivate_feature(obj, f, tx);
294 ds->ds_feature_inuse[f] = B_FALSE;
297 if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
298 ASSERT3P(ds->ds_prev, ==, NULL);
299 VERIFY0(dsl_dataset_hold_obj(dp,
300 dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &ds_prev));
302 (dsl_dataset_phys(ds_prev)->ds_next_snap_obj != obj);
304 dmu_buf_will_dirty(ds_prev->ds_dbuf, tx);
305 if (after_branch_point &&
306 dsl_dataset_phys(ds_prev)->ds_next_clones_obj != 0) {
307 dsl_dataset_remove_from_next_clones(ds_prev, obj, tx);
308 if (dsl_dataset_phys(ds)->ds_next_snap_obj != 0) {
309 VERIFY0(zap_add_int(mos,
310 dsl_dataset_phys(ds_prev)->
312 dsl_dataset_phys(ds)->ds_next_snap_obj,
316 if (!after_branch_point) {
317 dsl_dataset_phys(ds_prev)->ds_next_snap_obj =
318 dsl_dataset_phys(ds)->ds_next_snap_obj;
322 dsl_dataset_t *ds_next;
324 uint64_t used = 0, comp = 0, uncomp = 0;
326 VERIFY0(dsl_dataset_hold_obj(dp,
327 dsl_dataset_phys(ds)->ds_next_snap_obj, FTAG, &ds_next));
328 ASSERT3U(dsl_dataset_phys(ds_next)->ds_prev_snap_obj, ==, obj);
330 old_unique = dsl_dataset_phys(ds_next)->ds_unique_bytes;
332 dmu_buf_will_dirty(ds_next->ds_dbuf, tx);
333 dsl_dataset_phys(ds_next)->ds_prev_snap_obj =
334 dsl_dataset_phys(ds)->ds_prev_snap_obj;
335 dsl_dataset_phys(ds_next)->ds_prev_snap_txg =
336 dsl_dataset_phys(ds)->ds_prev_snap_txg;
337 ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, ==,
338 ds_prev ? dsl_dataset_phys(ds_prev)->ds_creation_txg : 0);
340 if (ds_next->ds_deadlist.dl_oldfmt) {
341 process_old_deadlist(ds, ds_prev, ds_next,
342 after_branch_point, tx);
344 /* Adjust prev's unique space. */
345 if (ds_prev && !after_branch_point) {
346 dsl_deadlist_space_range(&ds_next->ds_deadlist,
347 dsl_dataset_phys(ds_prev)->ds_prev_snap_txg,
348 dsl_dataset_phys(ds)->ds_prev_snap_txg,
349 &used, &comp, &uncomp);
350 dsl_dataset_phys(ds_prev)->ds_unique_bytes += used;
353 /* Adjust snapused. */
354 dsl_deadlist_space_range(&ds_next->ds_deadlist,
355 dsl_dataset_phys(ds)->ds_prev_snap_txg, UINT64_MAX,
356 &used, &comp, &uncomp);
357 dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
358 -used, -comp, -uncomp, tx);
360 /* Move blocks to be freed to pool's free list. */
361 dsl_deadlist_move_bpobj(&ds_next->ds_deadlist,
362 &dp->dp_free_bpobj, dsl_dataset_phys(ds)->ds_prev_snap_txg,
364 dsl_dir_diduse_space(tx->tx_pool->dp_free_dir,
365 DD_USED_HEAD, used, comp, uncomp, tx);
367 /* Merge our deadlist into next's and free it. */
368 dsl_deadlist_merge(&ds_next->ds_deadlist,
369 dsl_dataset_phys(ds)->ds_deadlist_obj, tx);
372 dsl_deadlist_close(&ds->ds_deadlist);
373 dsl_deadlist_free(mos, dsl_dataset_phys(ds)->ds_deadlist_obj, tx);
374 dmu_buf_will_dirty(ds->ds_dbuf, tx);
375 dsl_dataset_phys(ds)->ds_deadlist_obj = 0;
377 dsl_destroy_snapshot_handle_remaps(ds, ds_next, tx);
379 /* Collapse range in clone heads */
380 dsl_dataset_remove_clones_key(ds,
381 dsl_dataset_phys(ds)->ds_creation_txg, tx);
383 if (ds_next->ds_is_snapshot) {
384 dsl_dataset_t *ds_nextnext;
387 * Update next's unique to include blocks which
388 * were previously shared by only this snapshot
389 * and it. Those blocks will be born after the
390 * prev snap and before this snap, and will have
391 * died after the next snap and before the one
392 * after that (ie. be on the snap after next's
395 VERIFY0(dsl_dataset_hold_obj(dp,
396 dsl_dataset_phys(ds_next)->ds_next_snap_obj,
397 FTAG, &ds_nextnext));
398 dsl_deadlist_space_range(&ds_nextnext->ds_deadlist,
399 dsl_dataset_phys(ds)->ds_prev_snap_txg,
400 dsl_dataset_phys(ds)->ds_creation_txg,
401 &used, &comp, &uncomp);
402 dsl_dataset_phys(ds_next)->ds_unique_bytes += used;
403 dsl_dataset_rele(ds_nextnext, FTAG);
404 ASSERT3P(ds_next->ds_prev, ==, NULL);
406 /* Collapse range in this head. */
408 VERIFY0(dsl_dataset_hold_obj(dp,
409 dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj, FTAG, &hds));
410 dsl_deadlist_remove_key(&hds->ds_deadlist,
411 dsl_dataset_phys(ds)->ds_creation_txg, tx);
412 if (dsl_dataset_remap_deadlist_exists(hds)) {
413 dsl_deadlist_remove_key(&hds->ds_remap_deadlist,
414 dsl_dataset_phys(ds)->ds_creation_txg, tx);
416 dsl_dataset_rele(hds, FTAG);
419 ASSERT3P(ds_next->ds_prev, ==, ds);
420 dsl_dataset_rele(ds_next->ds_prev, ds_next);
421 ds_next->ds_prev = NULL;
423 VERIFY0(dsl_dataset_hold_obj(dp,
424 dsl_dataset_phys(ds)->ds_prev_snap_obj,
425 ds_next, &ds_next->ds_prev));
428 dsl_dataset_recalc_head_uniq(ds_next);
431 * Reduce the amount of our unconsumed refreservation
432 * being charged to our parent by the amount of
433 * new unique data we have gained.
435 if (old_unique < ds_next->ds_reserved) {
437 uint64_t new_unique =
438 dsl_dataset_phys(ds_next)->ds_unique_bytes;
440 ASSERT(old_unique <= new_unique);
441 mrsdelta = MIN(new_unique - old_unique,
442 ds_next->ds_reserved - old_unique);
443 dsl_dir_diduse_space(ds->ds_dir,
444 DD_USED_REFRSRV, -mrsdelta, 0, 0, tx);
447 dsl_dataset_rele(ds_next, FTAG);
450 * This must be done after the dsl_traverse(), because it will
451 * re-open the objset.
454 dmu_objset_evict(ds->ds_objset);
455 ds->ds_objset = NULL;
458 /* remove from snapshot namespace */
459 dsl_dataset_t *ds_head;
460 ASSERT(dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0);
461 VERIFY0(dsl_dataset_hold_obj(dp,
462 dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj, FTAG, &ds_head));
463 VERIFY0(dsl_dataset_get_snapname(ds));
468 err = dsl_dataset_snap_lookup(ds_head,
469 ds->ds_snapname, &val);
471 ASSERT3U(val, ==, obj);
474 VERIFY0(dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx, B_TRUE));
475 dsl_dataset_rele(ds_head, FTAG);
478 dsl_dataset_rele(ds_prev, FTAG);
480 spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
482 if (dsl_dataset_phys(ds)->ds_next_clones_obj != 0) {
484 ASSERT0(zap_count(mos,
485 dsl_dataset_phys(ds)->ds_next_clones_obj, &count) &&
487 VERIFY0(dmu_object_free(mos,
488 dsl_dataset_phys(ds)->ds_next_clones_obj, tx));
490 if (dsl_dataset_phys(ds)->ds_props_obj != 0)
491 VERIFY0(zap_destroy(mos, dsl_dataset_phys(ds)->ds_props_obj,
493 if (dsl_dataset_phys(ds)->ds_userrefs_obj != 0)
494 VERIFY0(zap_destroy(mos, dsl_dataset_phys(ds)->ds_userrefs_obj,
497 #if defined(__FreeBSD__) && defined(_KERNEL)
498 char dsname[ZFS_MAX_DATASET_NAME_LEN];
500 dsl_dataset_name(ds, dsname);
501 zvol_remove_minors(dp->dp_spa, dsname);
504 dsl_dir_rele(ds->ds_dir, ds);
506 dmu_object_free_zapified(mos, obj, tx);
510 dsl_destroy_snapshot_sync(void *arg, dmu_tx_t *tx)
512 dsl_destroy_snapshot_arg_t *ddsa = arg;
513 const char *dsname = ddsa->ddsa_name;
514 boolean_t defer = ddsa->ddsa_defer;
516 dsl_pool_t *dp = dmu_tx_pool(tx);
519 int error = dsl_dataset_hold(dp, dsname, FTAG, &ds);
523 dsl_destroy_snapshot_sync_impl(ds, defer, tx);
524 dsl_dataset_rele(ds, FTAG);
528 * The semantics of this function are described in the comment above
529 * lzc_destroy_snaps(). To summarize:
531 * The snapshots must all be in the same pool.
533 * Snapshots that don't exist will be silently ignored (considered to be
534 * "already deleted").
536 * On success, all snaps will be destroyed and this will return 0.
537 * On failure, no snaps will be destroyed, the errlist will be filled in,
538 * and this will return an errno.
541 dsl_destroy_snapshots_nvl(nvlist_t *snaps, boolean_t defer,
544 if (nvlist_next_nvpair(snaps, NULL) == NULL)
548 * lzc_destroy_snaps() is documented to take an nvlist whose
549 * values "don't matter". We need to convert that nvlist to
550 * one that we know can be converted to LUA. We also don't
551 * care about any duplicate entries because the nvlist will
552 * be converted to a LUA table which should take care of this.
554 nvlist_t *snaps_normalized;
555 VERIFY0(nvlist_alloc(&snaps_normalized, 0, KM_SLEEP));
556 for (nvpair_t *pair = nvlist_next_nvpair(snaps, NULL);
557 pair != NULL; pair = nvlist_next_nvpair(snaps, pair)) {
558 fnvlist_add_boolean_value(snaps_normalized,
559 nvpair_name(pair), B_TRUE);
563 VERIFY0(nvlist_alloc(&arg, 0, KM_SLEEP));
564 fnvlist_add_nvlist(arg, "snaps", snaps_normalized);
565 fnvlist_free(snaps_normalized);
566 fnvlist_add_boolean_value(arg, "defer", defer);
569 VERIFY0(nvlist_alloc(&wrapper, 0, KM_SLEEP));
570 fnvlist_add_nvlist(wrapper, ZCP_ARG_ARGLIST, arg);
573 const char *program =
575 "snaps = arg['snaps']\n"
576 "defer = arg['defer']\n"
578 "has_errors = false\n"
579 "for snap, v in pairs(snaps) do\n"
580 " errno = zfs.check.destroy{snap, defer=defer}\n"
581 " zfs.debug('snap: ' .. snap .. ' errno: ' .. errno)\n"
582 " if errno == ENOENT then\n"
583 " snaps[snap] = nil\n"
584 " elseif errno ~= 0 then\n"
585 " errors[snap] = errno\n"
586 " has_errors = true\n"
589 "if has_errors then\n"
592 "for snap, v in pairs(snaps) do\n"
593 " errno = zfs.sync.destroy{snap, defer=defer}\n"
594 " assert(errno == 0)\n"
598 nvlist_t *result = fnvlist_alloc();
599 int error = zcp_eval(nvpair_name(nvlist_next_nvpair(snaps, NULL)),
603 zfs_lua_max_memlimit,
604 nvlist_next_nvpair(wrapper, NULL), result);
606 char *errorstr = NULL;
607 (void) nvlist_lookup_string(result, ZCP_RET_ERROR, &errorstr);
608 if (errorstr != NULL) {
609 zfs_dbgmsg(errorstr);
613 fnvlist_free(wrapper);
616 * lzc_destroy_snaps() is documented to fill the errlist with
617 * int32 values, so we need to covert the int64 values that are
621 nvlist_t *errlist_raw = fnvlist_lookup_nvlist(result, ZCP_RET_RETURN);
622 for (nvpair_t *pair = nvlist_next_nvpair(errlist_raw, NULL);
623 pair != NULL; pair = nvlist_next_nvpair(errlist_raw, pair)) {
624 int32_t val = (int32_t)fnvpair_value_int64(pair);
627 fnvlist_add_int32(errlist, nvpair_name(pair), val);
629 fnvlist_free(result);
634 dsl_destroy_snapshot(const char *name, boolean_t defer)
637 nvlist_t *nvl = fnvlist_alloc();
638 nvlist_t *errlist = fnvlist_alloc();
640 fnvlist_add_boolean(nvl, name);
641 error = dsl_destroy_snapshots_nvl(nvl, defer, errlist);
642 fnvlist_free(errlist);
654 kill_blkptr(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
655 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
657 struct killarg *ka = arg;
658 dmu_tx_t *tx = ka->tx;
660 if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
663 if (zb->zb_level == ZB_ZIL_LEVEL) {
664 ASSERT(zilog != NULL);
666 * It's a block in the intent log. It has no
667 * accounting, so just free it.
669 dsl_free(ka->tx->tx_pool, ka->tx->tx_txg, bp);
671 ASSERT(zilog == NULL);
672 ASSERT3U(bp->blk_birth, >,
673 dsl_dataset_phys(ka->ds)->ds_prev_snap_txg);
674 (void) dsl_dataset_block_kill(ka->ds, bp, tx, B_FALSE);
681 old_synchronous_dataset_destroy(dsl_dataset_t *ds, dmu_tx_t *tx)
686 * Free everything that we point to (that's born after
687 * the previous snapshot, if we are a clone)
689 * NB: this should be very quick, because we already
690 * freed all the objects in open context.
694 VERIFY0(traverse_dataset(ds,
695 dsl_dataset_phys(ds)->ds_prev_snap_txg, TRAVERSE_POST,
697 ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) ||
698 dsl_dataset_phys(ds)->ds_unique_bytes == 0);
702 dsl_destroy_head_check_impl(dsl_dataset_t *ds, int expected_holds)
708 ASSERT(!ds->ds_is_snapshot);
709 if (ds->ds_is_snapshot)
710 return (SET_ERROR(EINVAL));
712 if (zfs_refcount_count(&ds->ds_longholds) != expected_holds)
713 return (SET_ERROR(EBUSY));
715 mos = ds->ds_dir->dd_pool->dp_meta_objset;
718 * Can't delete a head dataset if there are snapshots of it.
719 * (Except if the only snapshots are from the branch we cloned
722 if (ds->ds_prev != NULL &&
723 dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj == ds->ds_object)
724 return (SET_ERROR(EBUSY));
727 * Can't delete if there are children of this fs.
729 error = zap_count(mos,
730 dsl_dir_phys(ds->ds_dir)->dd_child_dir_zapobj, &count);
734 return (SET_ERROR(EEXIST));
736 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev) &&
737 dsl_dataset_phys(ds->ds_prev)->ds_num_children == 2 &&
738 ds->ds_prev->ds_userrefs == 0) {
739 /* We need to remove the origin snapshot as well. */
740 if (!zfs_refcount_is_zero(&ds->ds_prev->ds_longholds))
741 return (SET_ERROR(EBUSY));
747 dsl_destroy_head_check(void *arg, dmu_tx_t *tx)
749 dsl_destroy_head_arg_t *ddha = arg;
750 dsl_pool_t *dp = dmu_tx_pool(tx);
754 error = dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds);
758 error = dsl_destroy_head_check_impl(ds, 0);
759 dsl_dataset_rele(ds, FTAG);
764 dsl_dir_destroy_sync(uint64_t ddobj, dmu_tx_t *tx)
767 dsl_pool_t *dp = dmu_tx_pool(tx);
768 objset_t *mos = dp->dp_meta_objset;
771 ASSERT(RRW_WRITE_HELD(&dmu_tx_pool(tx)->dp_config_rwlock));
773 VERIFY0(dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd));
775 ASSERT0(dsl_dir_phys(dd)->dd_head_dataset_obj);
778 * Decrement the filesystem count for all parent filesystems.
780 * When we receive an incremental stream into a filesystem that already
781 * exists, a temporary clone is created. We never count this temporary
782 * clone, whose name begins with a '%'.
784 if (dd->dd_myname[0] != '%' && dd->dd_parent != NULL)
785 dsl_fs_ss_count_adjust(dd->dd_parent, -1,
786 DD_FIELD_FILESYSTEM_COUNT, tx);
789 * Remove our reservation. The impl() routine avoids setting the
790 * actual property, which would require the (already destroyed) ds.
792 dsl_dir_set_reservation_sync_impl(dd, 0, tx);
794 ASSERT0(dsl_dir_phys(dd)->dd_used_bytes);
795 ASSERT0(dsl_dir_phys(dd)->dd_reserved);
796 for (t = 0; t < DD_USED_NUM; t++)
797 ASSERT0(dsl_dir_phys(dd)->dd_used_breakdown[t]);
799 VERIFY0(zap_destroy(mos, dsl_dir_phys(dd)->dd_child_dir_zapobj, tx));
800 VERIFY0(zap_destroy(mos, dsl_dir_phys(dd)->dd_props_zapobj, tx));
801 if (dsl_dir_phys(dd)->dd_clones != 0)
802 VERIFY0(zap_destroy(mos, dsl_dir_phys(dd)->dd_clones, tx));
803 VERIFY0(dsl_deleg_destroy(mos, dsl_dir_phys(dd)->dd_deleg_zapobj, tx));
804 VERIFY0(zap_remove(mos,
805 dsl_dir_phys(dd->dd_parent)->dd_child_dir_zapobj,
808 dsl_dir_rele(dd, FTAG);
809 dmu_object_free_zapified(mos, ddobj, tx);
813 dsl_destroy_head_sync_impl(dsl_dataset_t *ds, dmu_tx_t *tx)
815 dsl_pool_t *dp = dmu_tx_pool(tx);
816 objset_t *mos = dp->dp_meta_objset;
817 uint64_t obj, ddobj, prevobj = 0;
820 ASSERT3U(dsl_dataset_phys(ds)->ds_num_children, <=, 1);
821 ASSERT(ds->ds_prev == NULL ||
822 dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj != ds->ds_object);
823 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
824 ASSERT3U(dsl_dataset_phys(ds)->ds_bp.blk_birth, <=, tx->tx_txg);
825 rrw_exit(&ds->ds_bp_rwlock, FTAG);
826 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
828 /* We need to log before removing it from the namespace. */
829 spa_history_log_internal_ds(ds, "destroy", tx, "");
831 rmorigin = (dsl_dir_is_clone(ds->ds_dir) &&
832 DS_IS_DEFER_DESTROY(ds->ds_prev) &&
833 dsl_dataset_phys(ds->ds_prev)->ds_num_children == 2 &&
834 ds->ds_prev->ds_userrefs == 0);
836 /* Remove our reservation. */
837 if (ds->ds_reserved != 0) {
838 dsl_dataset_set_refreservation_sync_impl(ds,
839 (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED),
841 ASSERT0(ds->ds_reserved);
846 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
847 if (ds->ds_feature_inuse[f]) {
848 dsl_dataset_deactivate_feature(obj, f, tx);
849 ds->ds_feature_inuse[f] = B_FALSE;
853 dsl_scan_ds_destroyed(ds, tx);
855 if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
856 /* This is a clone */
857 ASSERT(ds->ds_prev != NULL);
858 ASSERT3U(dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj, !=,
860 ASSERT0(dsl_dataset_phys(ds)->ds_next_snap_obj);
862 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
863 if (dsl_dataset_phys(ds->ds_prev)->ds_next_clones_obj != 0) {
864 dsl_dataset_remove_from_next_clones(ds->ds_prev,
868 ASSERT3U(dsl_dataset_phys(ds->ds_prev)->ds_num_children, >, 1);
869 dsl_dataset_phys(ds->ds_prev)->ds_num_children--;
873 * Destroy the deadlist. Unless it's a clone, the
874 * deadlist should be empty since the dataset has no snapshots.
875 * (If it's a clone, it's safe to ignore the deadlist contents
876 * since they are still referenced by the origin snapshot.)
878 dsl_deadlist_close(&ds->ds_deadlist);
879 dsl_deadlist_free(mos, dsl_dataset_phys(ds)->ds_deadlist_obj, tx);
880 dmu_buf_will_dirty(ds->ds_dbuf, tx);
881 dsl_dataset_phys(ds)->ds_deadlist_obj = 0;
883 if (dsl_dataset_remap_deadlist_exists(ds))
884 dsl_dataset_destroy_remap_deadlist(ds, tx);
887 VERIFY0(dmu_objset_from_ds(ds, &os));
889 if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_ASYNC_DESTROY)) {
890 old_synchronous_dataset_destroy(ds, tx);
893 * Move the bptree into the pool's list of trees to
894 * clean up and update space accounting information.
896 uint64_t used, comp, uncomp;
898 zil_destroy_sync(dmu_objset_zil(os), tx);
900 if (!spa_feature_is_active(dp->dp_spa,
901 SPA_FEATURE_ASYNC_DESTROY)) {
902 dsl_scan_t *scn = dp->dp_scan;
903 spa_feature_incr(dp->dp_spa, SPA_FEATURE_ASYNC_DESTROY,
905 dp->dp_bptree_obj = bptree_alloc(mos, tx);
907 DMU_POOL_DIRECTORY_OBJECT,
908 DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1,
909 &dp->dp_bptree_obj, tx));
910 ASSERT(!scn->scn_async_destroying);
911 scn->scn_async_destroying = B_TRUE;
914 used = dsl_dir_phys(ds->ds_dir)->dd_used_bytes;
915 comp = dsl_dir_phys(ds->ds_dir)->dd_compressed_bytes;
916 uncomp = dsl_dir_phys(ds->ds_dir)->dd_uncompressed_bytes;
918 ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) ||
919 dsl_dataset_phys(ds)->ds_unique_bytes == used);
921 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
922 bptree_add(mos, dp->dp_bptree_obj,
923 &dsl_dataset_phys(ds)->ds_bp,
924 dsl_dataset_phys(ds)->ds_prev_snap_txg,
925 used, comp, uncomp, tx);
926 rrw_exit(&ds->ds_bp_rwlock, FTAG);
927 dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
928 -used, -comp, -uncomp, tx);
929 dsl_dir_diduse_space(dp->dp_free_dir, DD_USED_HEAD,
930 used, comp, uncomp, tx);
933 if (ds->ds_prev != NULL) {
934 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
935 VERIFY0(zap_remove_int(mos,
936 dsl_dir_phys(ds->ds_prev->ds_dir)->dd_clones,
939 prevobj = ds->ds_prev->ds_object;
940 dsl_dataset_rele(ds->ds_prev, ds);
945 * This must be done after the dsl_traverse(), because it will
946 * re-open the objset.
949 dmu_objset_evict(ds->ds_objset);
950 ds->ds_objset = NULL;
953 /* Erase the link in the dir */
954 dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
955 dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj = 0;
956 ddobj = ds->ds_dir->dd_object;
957 ASSERT(dsl_dataset_phys(ds)->ds_snapnames_zapobj != 0);
958 VERIFY0(zap_destroy(mos,
959 dsl_dataset_phys(ds)->ds_snapnames_zapobj, tx));
961 if (ds->ds_bookmarks != 0) {
962 VERIFY0(zap_destroy(mos, ds->ds_bookmarks, tx));
963 spa_feature_decr(dp->dp_spa, SPA_FEATURE_BOOKMARKS, tx);
966 spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
968 ASSERT0(dsl_dataset_phys(ds)->ds_next_clones_obj);
969 ASSERT0(dsl_dataset_phys(ds)->ds_props_obj);
970 ASSERT0(dsl_dataset_phys(ds)->ds_userrefs_obj);
971 dsl_dir_rele(ds->ds_dir, ds);
973 dmu_object_free_zapified(mos, obj, tx);
975 dsl_dir_destroy_sync(ddobj, tx);
979 VERIFY0(dsl_dataset_hold_obj(dp, prevobj, FTAG, &prev));
980 dsl_destroy_snapshot_sync_impl(prev, B_FALSE, tx);
981 dsl_dataset_rele(prev, FTAG);
986 dsl_destroy_head_sync(void *arg, dmu_tx_t *tx)
988 dsl_destroy_head_arg_t *ddha = arg;
989 dsl_pool_t *dp = dmu_tx_pool(tx);
992 VERIFY0(dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds));
993 dsl_destroy_head_sync_impl(ds, tx);
994 #if defined(__FreeBSD__) && defined(_KERNEL)
995 zvol_remove_minors(dp->dp_spa, ddha->ddha_name);
997 dsl_dataset_rele(ds, FTAG);
1001 dsl_destroy_head_begin_sync(void *arg, dmu_tx_t *tx)
1003 dsl_destroy_head_arg_t *ddha = arg;
1004 dsl_pool_t *dp = dmu_tx_pool(tx);
1007 VERIFY0(dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds));
1009 /* Mark it as inconsistent on-disk, in case we crash */
1010 dmu_buf_will_dirty(ds->ds_dbuf, tx);
1011 dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_INCONSISTENT;
1013 spa_history_log_internal_ds(ds, "destroy begin", tx, "");
1014 dsl_dataset_rele(ds, FTAG);
1018 dsl_destroy_head(const char *name)
1020 dsl_destroy_head_arg_t ddha;
1023 boolean_t isenabled;
1026 zfs_destroy_unmount_origin(name);
1029 error = spa_open(name, &spa, FTAG);
1032 isenabled = spa_feature_is_enabled(spa, SPA_FEATURE_ASYNC_DESTROY);
1033 spa_close(spa, FTAG);
1035 ddha.ddha_name = name;
1040 error = dsl_sync_task(name, dsl_destroy_head_check,
1041 dsl_destroy_head_begin_sync, &ddha,
1042 0, ZFS_SPACE_CHECK_DESTROY);
1047 * Head deletion is processed in one txg on old pools;
1048 * remove the objects from open context so that the txg sync
1051 error = dmu_objset_own(name, DMU_OST_ANY, B_FALSE, FTAG, &os);
1053 uint64_t prev_snap_txg =
1054 dsl_dataset_phys(dmu_objset_ds(os))->
1056 for (uint64_t obj = 0; error == 0;
1057 error = dmu_object_next(os, &obj, FALSE,
1059 (void) dmu_free_long_object(os, obj);
1060 /* sync out all frees */
1061 txg_wait_synced(dmu_objset_pool(os), 0);
1062 dmu_objset_disown(os, FTAG);
1066 return (dsl_sync_task(name, dsl_destroy_head_check,
1067 dsl_destroy_head_sync, &ddha, 0, ZFS_SPACE_CHECK_DESTROY));
1071 * Note, this function is used as the callback for dmu_objset_find(). We
1072 * always return 0 so that we will continue to find and process
1073 * inconsistent datasets, even if we encounter an error trying to
1074 * process one of them.
1078 dsl_destroy_inconsistent(const char *dsname, void *arg)
1082 if (dmu_objset_hold(dsname, FTAG, &os) == 0) {
1083 boolean_t need_destroy = DS_IS_INCONSISTENT(dmu_objset_ds(os));
1086 * If the dataset is inconsistent because a resumable receive
1087 * has failed, then do not destroy it.
1089 if (dsl_dataset_has_resume_receive_state(dmu_objset_ds(os)))
1090 need_destroy = B_FALSE;
1092 dmu_objset_rele(os, FTAG);
1094 (void) dsl_destroy_head(dsname);