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) 2016 Actifio, Inc. All rights reserved.
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>
49 dsl_destroy_snapshot_check_impl(dsl_dataset_t *ds, boolean_t defer)
51 if (!ds->ds_is_snapshot)
52 return (SET_ERROR(EINVAL));
54 if (dsl_dataset_long_held(ds))
55 return (SET_ERROR(EBUSY));
58 * Only allow deferred destroy on pools that support it.
59 * NOTE: deferred destroy is only supported on snapshots.
62 if (spa_version(ds->ds_dir->dd_pool->dp_spa) <
64 return (SET_ERROR(ENOTSUP));
69 * If this snapshot has an elevated user reference count,
70 * we can't destroy it yet.
72 if (ds->ds_userrefs > 0)
73 return (SET_ERROR(EBUSY));
76 * Can't delete a branch point.
78 if (dsl_dataset_phys(ds)->ds_num_children > 1)
79 return (SET_ERROR(EEXIST));
85 dsl_destroy_snapshot_check(void *arg, dmu_tx_t *tx)
87 dsl_destroy_snapshot_arg_t *ddsa = arg;
88 const char *dsname = ddsa->ddsa_name;
89 boolean_t defer = ddsa->ddsa_defer;
91 dsl_pool_t *dp = dmu_tx_pool(tx);
95 error = dsl_dataset_hold(dp, dsname, FTAG, &ds);
98 * If the snapshot does not exist, silently ignore it, and
99 * dsl_destroy_snapshot_sync() will be a no-op
100 * (it's "already destroyed").
106 error = dsl_destroy_snapshot_check_impl(ds, defer);
107 dsl_dataset_rele(ds, FTAG);
113 struct process_old_arg {
115 dsl_dataset_t *ds_prev;
116 boolean_t after_branch_point;
118 uint64_t used, comp, uncomp;
122 process_old_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
124 struct process_old_arg *poa = arg;
125 dsl_pool_t *dp = poa->ds->ds_dir->dd_pool;
127 ASSERT(!BP_IS_HOLE(bp));
129 if (bp->blk_birth <= dsl_dataset_phys(poa->ds)->ds_prev_snap_txg) {
130 dsl_deadlist_insert(&poa->ds->ds_deadlist, bp, tx);
131 if (poa->ds_prev && !poa->after_branch_point &&
133 dsl_dataset_phys(poa->ds_prev)->ds_prev_snap_txg) {
134 dsl_dataset_phys(poa->ds_prev)->ds_unique_bytes +=
135 bp_get_dsize_sync(dp->dp_spa, bp);
138 poa->used += bp_get_dsize_sync(dp->dp_spa, bp);
139 poa->comp += BP_GET_PSIZE(bp);
140 poa->uncomp += BP_GET_UCSIZE(bp);
141 dsl_free_sync(poa->pio, dp, tx->tx_txg, bp);
147 process_old_deadlist(dsl_dataset_t *ds, dsl_dataset_t *ds_prev,
148 dsl_dataset_t *ds_next, boolean_t after_branch_point, dmu_tx_t *tx)
150 struct process_old_arg poa = { 0 };
151 dsl_pool_t *dp = ds->ds_dir->dd_pool;
152 objset_t *mos = dp->dp_meta_objset;
153 uint64_t deadlist_obj;
155 ASSERT(ds->ds_deadlist.dl_oldfmt);
156 ASSERT(ds_next->ds_deadlist.dl_oldfmt);
159 poa.ds_prev = ds_prev;
160 poa.after_branch_point = after_branch_point;
161 poa.pio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
162 VERIFY0(bpobj_iterate(&ds_next->ds_deadlist.dl_bpobj,
163 process_old_cb, &poa, tx));
164 VERIFY0(zio_wait(poa.pio));
165 ASSERT3U(poa.used, ==, dsl_dataset_phys(ds)->ds_unique_bytes);
167 /* change snapused */
168 dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
169 -poa.used, -poa.comp, -poa.uncomp, tx);
171 /* swap next's deadlist to our deadlist */
172 dsl_deadlist_close(&ds->ds_deadlist);
173 dsl_deadlist_close(&ds_next->ds_deadlist);
174 deadlist_obj = dsl_dataset_phys(ds)->ds_deadlist_obj;
175 dsl_dataset_phys(ds)->ds_deadlist_obj =
176 dsl_dataset_phys(ds_next)->ds_deadlist_obj;
177 dsl_dataset_phys(ds_next)->ds_deadlist_obj = deadlist_obj;
178 dsl_deadlist_open(&ds->ds_deadlist, mos,
179 dsl_dataset_phys(ds)->ds_deadlist_obj);
180 dsl_deadlist_open(&ds_next->ds_deadlist, mos,
181 dsl_dataset_phys(ds_next)->ds_deadlist_obj);
184 struct removeclonesnode {
190 dsl_dataset_remove_clones_key(dsl_dataset_t *ds, uint64_t mintxg, dmu_tx_t *tx)
192 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
194 struct removeclonesnode *rcn;
196 list_create(&clones, sizeof (struct removeclonesnode),
197 offsetof(struct removeclonesnode, link));
199 rcn = kmem_zalloc(sizeof (struct removeclonesnode), KM_SLEEP);
201 list_insert_head(&clones, rcn);
203 for (; rcn != NULL; rcn = list_next(&clones, rcn)) {
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
211 if (dsl_dir_phys(rcn->ds->ds_dir)->dd_clones == 0)
214 for (zap_cursor_init(&zc, mos,
215 dsl_dir_phys(rcn->ds->ds_dir)->dd_clones);
216 zap_cursor_retrieve(&zc, &za) == 0;
217 zap_cursor_advance(&zc)) {
218 dsl_dataset_t *clone;
220 VERIFY0(dsl_dataset_hold_obj(rcn->ds->ds_dir->dd_pool,
221 za.za_first_integer, FTAG, &clone));
222 if (clone->ds_dir->dd_origin_txg > mintxg) {
223 dsl_deadlist_remove_key(&clone->ds_deadlist,
225 if (dsl_dataset_remap_deadlist_exists(clone)) {
226 dsl_deadlist_remove_key(
227 &clone->ds_remap_deadlist, mintxg,
231 sizeof (struct removeclonesnode), KM_SLEEP);
233 list_insert_tail(&clones, rcn);
235 dsl_dataset_rele(clone, FTAG);
238 zap_cursor_fini(&zc);
241 rcn = list_remove_head(&clones);
242 kmem_free(rcn, sizeof (struct removeclonesnode));
243 while ((rcn = list_remove_head(&clones)) != NULL) {
244 dsl_dataset_rele(rcn->ds, FTAG);
245 kmem_free(rcn, sizeof (struct removeclonesnode));
247 list_destroy(&clones);
251 dsl_destroy_snapshot_handle_remaps(dsl_dataset_t *ds, dsl_dataset_t *ds_next,
254 dsl_pool_t *dp = ds->ds_dir->dd_pool;
256 /* Move blocks to be obsoleted to pool's obsolete list. */
257 if (dsl_dataset_remap_deadlist_exists(ds_next)) {
258 if (!bpobj_is_open(&dp->dp_obsolete_bpobj))
259 dsl_pool_create_obsolete_bpobj(dp, tx);
261 dsl_deadlist_move_bpobj(&ds_next->ds_remap_deadlist,
262 &dp->dp_obsolete_bpobj,
263 dsl_dataset_phys(ds)->ds_prev_snap_txg, tx);
266 /* Merge our deadlist into next's and free it. */
267 if (dsl_dataset_remap_deadlist_exists(ds)) {
268 uint64_t remap_deadlist_object =
269 dsl_dataset_get_remap_deadlist_object(ds);
270 ASSERT(remap_deadlist_object != 0);
272 mutex_enter(&ds_next->ds_remap_deadlist_lock);
273 if (!dsl_dataset_remap_deadlist_exists(ds_next))
274 dsl_dataset_create_remap_deadlist(ds_next, tx);
275 mutex_exit(&ds_next->ds_remap_deadlist_lock);
277 dsl_deadlist_merge(&ds_next->ds_remap_deadlist,
278 remap_deadlist_object, tx);
279 dsl_dataset_destroy_remap_deadlist(ds, tx);
284 dsl_destroy_snapshot_sync_impl(dsl_dataset_t *ds, boolean_t defer, dmu_tx_t *tx)
286 int after_branch_point = FALSE;
287 dsl_pool_t *dp = ds->ds_dir->dd_pool;
288 objset_t *mos = dp->dp_meta_objset;
289 dsl_dataset_t *ds_prev = NULL;
292 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
293 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
294 ASSERT3U(dsl_dataset_phys(ds)->ds_bp.blk_birth, <=, tx->tx_txg);
295 rrw_exit(&ds->ds_bp_rwlock, FTAG);
296 ASSERT(refcount_is_zero(&ds->ds_longholds));
299 (ds->ds_userrefs > 0 ||
300 dsl_dataset_phys(ds)->ds_num_children > 1)) {
301 ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
302 dmu_buf_will_dirty(ds->ds_dbuf, tx);
303 dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_DEFER_DESTROY;
304 spa_history_log_internal_ds(ds, "defer_destroy", tx, "");
308 ASSERT3U(dsl_dataset_phys(ds)->ds_num_children, <=, 1);
310 /* We need to log before removing it from the namespace. */
311 spa_history_log_internal_ds(ds, "destroy", tx, "");
313 dsl_scan_ds_destroyed(ds, tx);
317 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
318 if (ds->ds_feature_inuse[f]) {
319 dsl_dataset_deactivate_feature(obj, f, tx);
320 ds->ds_feature_inuse[f] = B_FALSE;
323 if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
324 ASSERT3P(ds->ds_prev, ==, NULL);
325 VERIFY0(dsl_dataset_hold_obj(dp,
326 dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &ds_prev));
328 (dsl_dataset_phys(ds_prev)->ds_next_snap_obj != obj);
330 dmu_buf_will_dirty(ds_prev->ds_dbuf, tx);
331 if (after_branch_point &&
332 dsl_dataset_phys(ds_prev)->ds_next_clones_obj != 0) {
333 dsl_dataset_remove_from_next_clones(ds_prev, obj, tx);
334 if (dsl_dataset_phys(ds)->ds_next_snap_obj != 0) {
335 VERIFY0(zap_add_int(mos,
336 dsl_dataset_phys(ds_prev)->
338 dsl_dataset_phys(ds)->ds_next_snap_obj,
342 if (!after_branch_point) {
343 dsl_dataset_phys(ds_prev)->ds_next_snap_obj =
344 dsl_dataset_phys(ds)->ds_next_snap_obj;
348 dsl_dataset_t *ds_next;
350 uint64_t used = 0, comp = 0, uncomp = 0;
352 VERIFY0(dsl_dataset_hold_obj(dp,
353 dsl_dataset_phys(ds)->ds_next_snap_obj, FTAG, &ds_next));
354 ASSERT3U(dsl_dataset_phys(ds_next)->ds_prev_snap_obj, ==, obj);
356 old_unique = dsl_dataset_phys(ds_next)->ds_unique_bytes;
358 dmu_buf_will_dirty(ds_next->ds_dbuf, tx);
359 dsl_dataset_phys(ds_next)->ds_prev_snap_obj =
360 dsl_dataset_phys(ds)->ds_prev_snap_obj;
361 dsl_dataset_phys(ds_next)->ds_prev_snap_txg =
362 dsl_dataset_phys(ds)->ds_prev_snap_txg;
363 ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, ==,
364 ds_prev ? dsl_dataset_phys(ds_prev)->ds_creation_txg : 0);
366 if (ds_next->ds_deadlist.dl_oldfmt) {
367 process_old_deadlist(ds, ds_prev, ds_next,
368 after_branch_point, tx);
370 /* Adjust prev's unique space. */
371 if (ds_prev && !after_branch_point) {
372 dsl_deadlist_space_range(&ds_next->ds_deadlist,
373 dsl_dataset_phys(ds_prev)->ds_prev_snap_txg,
374 dsl_dataset_phys(ds)->ds_prev_snap_txg,
375 &used, &comp, &uncomp);
376 dsl_dataset_phys(ds_prev)->ds_unique_bytes += used;
379 /* Adjust snapused. */
380 dsl_deadlist_space_range(&ds_next->ds_deadlist,
381 dsl_dataset_phys(ds)->ds_prev_snap_txg, UINT64_MAX,
382 &used, &comp, &uncomp);
383 dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
384 -used, -comp, -uncomp, tx);
386 /* Move blocks to be freed to pool's free list. */
387 dsl_deadlist_move_bpobj(&ds_next->ds_deadlist,
388 &dp->dp_free_bpobj, dsl_dataset_phys(ds)->ds_prev_snap_txg,
390 dsl_dir_diduse_space(tx->tx_pool->dp_free_dir,
391 DD_USED_HEAD, used, comp, uncomp, tx);
393 /* Merge our deadlist into next's and free it. */
394 dsl_deadlist_merge(&ds_next->ds_deadlist,
395 dsl_dataset_phys(ds)->ds_deadlist_obj, tx);
398 dsl_deadlist_close(&ds->ds_deadlist);
399 dsl_deadlist_free(mos, dsl_dataset_phys(ds)->ds_deadlist_obj, tx);
400 dmu_buf_will_dirty(ds->ds_dbuf, tx);
401 dsl_dataset_phys(ds)->ds_deadlist_obj = 0;
403 dsl_destroy_snapshot_handle_remaps(ds, ds_next, tx);
405 /* Collapse range in clone heads */
406 dsl_dataset_remove_clones_key(ds,
407 dsl_dataset_phys(ds)->ds_creation_txg, tx);
409 if (ds_next->ds_is_snapshot) {
410 dsl_dataset_t *ds_nextnext;
413 * Update next's unique to include blocks which
414 * were previously shared by only this snapshot
415 * and it. Those blocks will be born after the
416 * prev snap and before this snap, and will have
417 * died after the next snap and before the one
418 * after that (ie. be on the snap after next's
421 VERIFY0(dsl_dataset_hold_obj(dp,
422 dsl_dataset_phys(ds_next)->ds_next_snap_obj,
423 FTAG, &ds_nextnext));
424 dsl_deadlist_space_range(&ds_nextnext->ds_deadlist,
425 dsl_dataset_phys(ds)->ds_prev_snap_txg,
426 dsl_dataset_phys(ds)->ds_creation_txg,
427 &used, &comp, &uncomp);
428 dsl_dataset_phys(ds_next)->ds_unique_bytes += used;
429 dsl_dataset_rele(ds_nextnext, FTAG);
430 ASSERT3P(ds_next->ds_prev, ==, NULL);
432 /* Collapse range in this head. */
434 VERIFY0(dsl_dataset_hold_obj(dp,
435 dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj, FTAG, &hds));
436 dsl_deadlist_remove_key(&hds->ds_deadlist,
437 dsl_dataset_phys(ds)->ds_creation_txg, tx);
438 if (dsl_dataset_remap_deadlist_exists(hds)) {
439 dsl_deadlist_remove_key(&hds->ds_remap_deadlist,
440 dsl_dataset_phys(ds)->ds_creation_txg, tx);
442 dsl_dataset_rele(hds, FTAG);
445 ASSERT3P(ds_next->ds_prev, ==, ds);
446 dsl_dataset_rele(ds_next->ds_prev, ds_next);
447 ds_next->ds_prev = NULL;
449 VERIFY0(dsl_dataset_hold_obj(dp,
450 dsl_dataset_phys(ds)->ds_prev_snap_obj,
451 ds_next, &ds_next->ds_prev));
454 dsl_dataset_recalc_head_uniq(ds_next);
457 * Reduce the amount of our unconsumed refreservation
458 * being charged to our parent by the amount of
459 * new unique data we have gained.
461 if (old_unique < ds_next->ds_reserved) {
463 uint64_t new_unique =
464 dsl_dataset_phys(ds_next)->ds_unique_bytes;
466 ASSERT(old_unique <= new_unique);
467 mrsdelta = MIN(new_unique - old_unique,
468 ds_next->ds_reserved - old_unique);
469 dsl_dir_diduse_space(ds->ds_dir,
470 DD_USED_REFRSRV, -mrsdelta, 0, 0, tx);
473 dsl_dataset_rele(ds_next, FTAG);
476 * This must be done after the dsl_traverse(), because it will
477 * re-open the objset.
480 dmu_objset_evict(ds->ds_objset);
481 ds->ds_objset = NULL;
484 /* remove from snapshot namespace */
485 dsl_dataset_t *ds_head;
486 ASSERT(dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0);
487 VERIFY0(dsl_dataset_hold_obj(dp,
488 dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj, FTAG, &ds_head));
489 VERIFY0(dsl_dataset_get_snapname(ds));
495 err = dsl_dataset_snap_lookup(ds_head,
496 ds->ds_snapname, &val);
498 ASSERT3U(val, ==, obj);
501 VERIFY0(dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx, B_TRUE));
502 dsl_dataset_rele(ds_head, FTAG);
505 dsl_dataset_rele(ds_prev, FTAG);
507 spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
509 if (dsl_dataset_phys(ds)->ds_next_clones_obj != 0) {
510 ASSERTV(uint64_t count);
511 ASSERT0(zap_count(mos,
512 dsl_dataset_phys(ds)->ds_next_clones_obj, &count) &&
514 VERIFY0(dmu_object_free(mos,
515 dsl_dataset_phys(ds)->ds_next_clones_obj, tx));
517 if (dsl_dataset_phys(ds)->ds_props_obj != 0)
518 VERIFY0(zap_destroy(mos, dsl_dataset_phys(ds)->ds_props_obj,
520 if (dsl_dataset_phys(ds)->ds_userrefs_obj != 0)
521 VERIFY0(zap_destroy(mos, dsl_dataset_phys(ds)->ds_userrefs_obj,
523 dsl_dir_rele(ds->ds_dir, ds);
525 dmu_object_free_zapified(mos, obj, tx);
529 dsl_destroy_snapshot_sync(void *arg, dmu_tx_t *tx)
531 dsl_destroy_snapshot_arg_t *ddsa = arg;
532 const char *dsname = ddsa->ddsa_name;
533 boolean_t defer = ddsa->ddsa_defer;
535 dsl_pool_t *dp = dmu_tx_pool(tx);
538 int error = dsl_dataset_hold(dp, dsname, FTAG, &ds);
542 dsl_destroy_snapshot_sync_impl(ds, defer, tx);
543 zvol_remove_minors(dp->dp_spa, dsname, B_TRUE);
544 dsl_dataset_rele(ds, FTAG);
548 * The semantics of this function are described in the comment above
549 * lzc_destroy_snaps(). To summarize:
551 * The snapshots must all be in the same pool.
553 * Snapshots that don't exist will be silently ignored (considered to be
554 * "already deleted").
556 * On success, all snaps will be destroyed and this will return 0.
557 * On failure, no snaps will be destroyed, the errlist will be filled in,
558 * and this will return an errno.
561 dsl_destroy_snapshots_nvl(nvlist_t *snaps, boolean_t defer,
564 if (nvlist_next_nvpair(snaps, NULL) == NULL)
568 * lzc_destroy_snaps() is documented to take an nvlist whose
569 * values "don't matter". We need to convert that nvlist to
570 * one that we know can be converted to LUA. We also don't
571 * care about any duplicate entries because the nvlist will
572 * be converted to a LUA table which should take care of this.
574 nvlist_t *snaps_normalized;
575 VERIFY0(nvlist_alloc(&snaps_normalized, 0, KM_SLEEP));
576 for (nvpair_t *pair = nvlist_next_nvpair(snaps, NULL);
577 pair != NULL; pair = nvlist_next_nvpair(snaps, pair)) {
578 fnvlist_add_boolean_value(snaps_normalized,
579 nvpair_name(pair), B_TRUE);
583 VERIFY0(nvlist_alloc(&arg, 0, KM_SLEEP));
584 fnvlist_add_nvlist(arg, "snaps", snaps_normalized);
585 fnvlist_free(snaps_normalized);
586 fnvlist_add_boolean_value(arg, "defer", defer);
589 VERIFY0(nvlist_alloc(&wrapper, 0, KM_SLEEP));
590 fnvlist_add_nvlist(wrapper, ZCP_ARG_ARGLIST, arg);
593 const char *program =
595 "snaps = arg['snaps']\n"
596 "defer = arg['defer']\n"
598 "has_errors = false\n"
599 "for snap, v in pairs(snaps) do\n"
600 " errno = zfs.check.destroy{snap, defer=defer}\n"
601 " zfs.debug('snap: ' .. snap .. ' errno: ' .. errno)\n"
602 " if errno == ENOENT then\n"
603 " snaps[snap] = nil\n"
604 " elseif errno ~= 0 then\n"
605 " errors[snap] = errno\n"
606 " has_errors = true\n"
609 "if has_errors then\n"
612 "for snap, v in pairs(snaps) do\n"
613 " errno = zfs.sync.destroy{snap, defer=defer}\n"
614 " assert(errno == 0)\n"
618 nvlist_t *result = fnvlist_alloc();
619 int error = zcp_eval(nvpair_name(nvlist_next_nvpair(snaps, NULL)),
623 zfs_lua_max_memlimit,
624 nvlist_next_nvpair(wrapper, NULL), result);
626 char *errorstr = NULL;
627 (void) nvlist_lookup_string(result, ZCP_RET_ERROR, &errorstr);
628 if (errorstr != NULL) {
629 zfs_dbgmsg(errorstr);
633 fnvlist_free(wrapper);
636 * lzc_destroy_snaps() is documented to fill the errlist with
637 * int32 values, so we need to covert the int64 values that are
641 nvlist_t *errlist_raw = fnvlist_lookup_nvlist(result, ZCP_RET_RETURN);
642 for (nvpair_t *pair = nvlist_next_nvpair(errlist_raw, NULL);
643 pair != NULL; pair = nvlist_next_nvpair(errlist_raw, pair)) {
644 int32_t val = (int32_t)fnvpair_value_int64(pair);
647 fnvlist_add_int32(errlist, nvpair_name(pair), val);
649 fnvlist_free(result);
654 dsl_destroy_snapshot(const char *name, boolean_t defer)
657 nvlist_t *nvl = fnvlist_alloc();
658 nvlist_t *errlist = fnvlist_alloc();
660 fnvlist_add_boolean(nvl, name);
661 error = dsl_destroy_snapshots_nvl(nvl, defer, errlist);
662 fnvlist_free(errlist);
674 kill_blkptr(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
675 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
677 struct killarg *ka = arg;
678 dmu_tx_t *tx = ka->tx;
680 if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
683 if (zb->zb_level == ZB_ZIL_LEVEL) {
684 ASSERT(zilog != NULL);
686 * It's a block in the intent log. It has no
687 * accounting, so just free it.
689 dsl_free(ka->tx->tx_pool, ka->tx->tx_txg, bp);
691 ASSERT(zilog == NULL);
692 ASSERT3U(bp->blk_birth, >,
693 dsl_dataset_phys(ka->ds)->ds_prev_snap_txg);
694 (void) dsl_dataset_block_kill(ka->ds, bp, tx, B_FALSE);
701 old_synchronous_dataset_destroy(dsl_dataset_t *ds, dmu_tx_t *tx)
706 * Free everything that we point to (that's born after
707 * the previous snapshot, if we are a clone)
709 * NB: this should be very quick, because we already
710 * freed all the objects in open context.
714 VERIFY0(traverse_dataset(ds,
715 dsl_dataset_phys(ds)->ds_prev_snap_txg, TRAVERSE_POST |
716 TRAVERSE_NO_DECRYPT, kill_blkptr, &ka));
717 ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) ||
718 dsl_dataset_phys(ds)->ds_unique_bytes == 0);
722 dsl_destroy_head_check_impl(dsl_dataset_t *ds, int expected_holds)
728 ASSERT(!ds->ds_is_snapshot);
729 if (ds->ds_is_snapshot)
730 return (SET_ERROR(EINVAL));
732 if (refcount_count(&ds->ds_longholds) != expected_holds)
733 return (SET_ERROR(EBUSY));
735 mos = ds->ds_dir->dd_pool->dp_meta_objset;
738 * Can't delete a head dataset if there are snapshots of it.
739 * (Except if the only snapshots are from the branch we cloned
742 if (ds->ds_prev != NULL &&
743 dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj == ds->ds_object)
744 return (SET_ERROR(EBUSY));
747 * Can't delete if there are children of this fs.
749 error = zap_count(mos,
750 dsl_dir_phys(ds->ds_dir)->dd_child_dir_zapobj, &count);
754 return (SET_ERROR(EEXIST));
756 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev) &&
757 dsl_dataset_phys(ds->ds_prev)->ds_num_children == 2 &&
758 ds->ds_prev->ds_userrefs == 0) {
759 /* We need to remove the origin snapshot as well. */
760 if (!refcount_is_zero(&ds->ds_prev->ds_longholds))
761 return (SET_ERROR(EBUSY));
767 dsl_destroy_head_check(void *arg, dmu_tx_t *tx)
769 dsl_destroy_head_arg_t *ddha = arg;
770 dsl_pool_t *dp = dmu_tx_pool(tx);
774 error = dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds);
778 error = dsl_destroy_head_check_impl(ds, 0);
779 dsl_dataset_rele(ds, FTAG);
784 dsl_dir_destroy_sync(uint64_t ddobj, dmu_tx_t *tx)
787 dsl_pool_t *dp = dmu_tx_pool(tx);
788 objset_t *mos = dp->dp_meta_objset;
791 ASSERT(RRW_WRITE_HELD(&dmu_tx_pool(tx)->dp_config_rwlock));
793 VERIFY0(dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd));
795 ASSERT0(dsl_dir_phys(dd)->dd_head_dataset_obj);
798 * Decrement the filesystem count for all parent filesystems.
800 * When we receive an incremental stream into a filesystem that already
801 * exists, a temporary clone is created. We never count this temporary
802 * clone, whose name begins with a '%'.
804 if (dd->dd_myname[0] != '%' && dd->dd_parent != NULL)
805 dsl_fs_ss_count_adjust(dd->dd_parent, -1,
806 DD_FIELD_FILESYSTEM_COUNT, tx);
809 * Remove our reservation. The impl() routine avoids setting the
810 * actual property, which would require the (already destroyed) ds.
812 dsl_dir_set_reservation_sync_impl(dd, 0, tx);
814 ASSERT0(dsl_dir_phys(dd)->dd_used_bytes);
815 ASSERT0(dsl_dir_phys(dd)->dd_reserved);
816 for (t = 0; t < DD_USED_NUM; t++)
817 ASSERT0(dsl_dir_phys(dd)->dd_used_breakdown[t]);
819 if (dd->dd_crypto_obj != 0) {
820 dsl_crypto_key_destroy_sync(dd->dd_crypto_obj, tx);
821 (void) spa_keystore_unload_wkey_impl(dp->dp_spa, dd->dd_object);
824 VERIFY0(zap_destroy(mos, dsl_dir_phys(dd)->dd_child_dir_zapobj, tx));
825 VERIFY0(zap_destroy(mos, dsl_dir_phys(dd)->dd_props_zapobj, tx));
826 VERIFY0(dsl_deleg_destroy(mos, dsl_dir_phys(dd)->dd_deleg_zapobj, tx));
827 VERIFY0(zap_remove(mos,
828 dsl_dir_phys(dd->dd_parent)->dd_child_dir_zapobj,
831 dsl_dir_rele(dd, FTAG);
832 dmu_object_free_zapified(mos, ddobj, tx);
836 dsl_destroy_head_sync_impl(dsl_dataset_t *ds, dmu_tx_t *tx)
838 dsl_pool_t *dp = dmu_tx_pool(tx);
839 objset_t *mos = dp->dp_meta_objset;
840 uint64_t obj, ddobj, prevobj = 0;
843 ASSERT3U(dsl_dataset_phys(ds)->ds_num_children, <=, 1);
844 ASSERT(ds->ds_prev == NULL ||
845 dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj != ds->ds_object);
846 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
847 ASSERT3U(dsl_dataset_phys(ds)->ds_bp.blk_birth, <=, tx->tx_txg);
848 rrw_exit(&ds->ds_bp_rwlock, FTAG);
849 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
851 /* We need to log before removing it from the namespace. */
852 spa_history_log_internal_ds(ds, "destroy", tx, "");
854 rmorigin = (dsl_dir_is_clone(ds->ds_dir) &&
855 DS_IS_DEFER_DESTROY(ds->ds_prev) &&
856 dsl_dataset_phys(ds->ds_prev)->ds_num_children == 2 &&
857 ds->ds_prev->ds_userrefs == 0);
859 /* Remove our reservation. */
860 if (ds->ds_reserved != 0) {
861 dsl_dataset_set_refreservation_sync_impl(ds,
862 (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED),
864 ASSERT0(ds->ds_reserved);
869 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
870 if (ds->ds_feature_inuse[f]) {
871 dsl_dataset_deactivate_feature(obj, f, tx);
872 ds->ds_feature_inuse[f] = B_FALSE;
876 dsl_scan_ds_destroyed(ds, tx);
878 if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
879 /* This is a clone */
880 ASSERT(ds->ds_prev != NULL);
881 ASSERT3U(dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj, !=,
883 ASSERT0(dsl_dataset_phys(ds)->ds_next_snap_obj);
885 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
886 if (dsl_dataset_phys(ds->ds_prev)->ds_next_clones_obj != 0) {
887 dsl_dataset_remove_from_next_clones(ds->ds_prev,
891 ASSERT3U(dsl_dataset_phys(ds->ds_prev)->ds_num_children, >, 1);
892 dsl_dataset_phys(ds->ds_prev)->ds_num_children--;
896 * Destroy the deadlist. Unless it's a clone, the
897 * deadlist should be empty since the dataset has no snapshots.
898 * (If it's a clone, it's safe to ignore the deadlist contents
899 * since they are still referenced by the origin snapshot.)
901 dsl_deadlist_close(&ds->ds_deadlist);
902 dsl_deadlist_free(mos, dsl_dataset_phys(ds)->ds_deadlist_obj, tx);
903 dmu_buf_will_dirty(ds->ds_dbuf, tx);
904 dsl_dataset_phys(ds)->ds_deadlist_obj = 0;
906 if (dsl_dataset_remap_deadlist_exists(ds))
907 dsl_dataset_destroy_remap_deadlist(ds, tx);
910 VERIFY0(dmu_objset_from_ds(ds, &os));
912 if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_ASYNC_DESTROY)) {
913 old_synchronous_dataset_destroy(ds, tx);
916 * Move the bptree into the pool's list of trees to
917 * clean up and update space accounting information.
919 uint64_t used, comp, uncomp;
921 zil_destroy_sync(dmu_objset_zil(os), tx);
923 if (!spa_feature_is_active(dp->dp_spa,
924 SPA_FEATURE_ASYNC_DESTROY)) {
925 dsl_scan_t *scn = dp->dp_scan;
926 spa_feature_incr(dp->dp_spa, SPA_FEATURE_ASYNC_DESTROY,
928 dp->dp_bptree_obj = bptree_alloc(mos, tx);
930 DMU_POOL_DIRECTORY_OBJECT,
931 DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1,
932 &dp->dp_bptree_obj, tx));
933 ASSERT(!scn->scn_async_destroying);
934 scn->scn_async_destroying = B_TRUE;
937 used = dsl_dir_phys(ds->ds_dir)->dd_used_bytes;
938 comp = dsl_dir_phys(ds->ds_dir)->dd_compressed_bytes;
939 uncomp = dsl_dir_phys(ds->ds_dir)->dd_uncompressed_bytes;
941 ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) ||
942 dsl_dataset_phys(ds)->ds_unique_bytes == used);
944 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
945 bptree_add(mos, dp->dp_bptree_obj,
946 &dsl_dataset_phys(ds)->ds_bp,
947 dsl_dataset_phys(ds)->ds_prev_snap_txg,
948 used, comp, uncomp, tx);
949 rrw_exit(&ds->ds_bp_rwlock, FTAG);
950 dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
951 -used, -comp, -uncomp, tx);
952 dsl_dir_diduse_space(dp->dp_free_dir, DD_USED_HEAD,
953 used, comp, uncomp, tx);
956 if (ds->ds_prev != NULL) {
957 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
958 VERIFY0(zap_remove_int(mos,
959 dsl_dir_phys(ds->ds_prev->ds_dir)->dd_clones,
962 prevobj = ds->ds_prev->ds_object;
963 dsl_dataset_rele(ds->ds_prev, ds);
968 * This must be done after the dsl_traverse(), because it will
969 * re-open the objset.
972 dmu_objset_evict(ds->ds_objset);
973 ds->ds_objset = NULL;
976 /* Erase the link in the dir */
977 dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
978 dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj = 0;
979 ddobj = ds->ds_dir->dd_object;
980 ASSERT(dsl_dataset_phys(ds)->ds_snapnames_zapobj != 0);
981 VERIFY0(zap_destroy(mos,
982 dsl_dataset_phys(ds)->ds_snapnames_zapobj, tx));
984 if (ds->ds_bookmarks != 0) {
985 VERIFY0(zap_destroy(mos, ds->ds_bookmarks, tx));
986 spa_feature_decr(dp->dp_spa, SPA_FEATURE_BOOKMARKS, tx);
989 spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
991 ASSERT0(dsl_dataset_phys(ds)->ds_next_clones_obj);
992 ASSERT0(dsl_dataset_phys(ds)->ds_props_obj);
993 ASSERT0(dsl_dataset_phys(ds)->ds_userrefs_obj);
994 dsl_dir_rele(ds->ds_dir, ds);
996 dmu_object_free_zapified(mos, obj, tx);
998 dsl_dir_destroy_sync(ddobj, tx);
1001 dsl_dataset_t *prev;
1002 VERIFY0(dsl_dataset_hold_obj(dp, prevobj, FTAG, &prev));
1003 dsl_destroy_snapshot_sync_impl(prev, B_FALSE, tx);
1004 dsl_dataset_rele(prev, FTAG);
1009 dsl_destroy_head_sync(void *arg, dmu_tx_t *tx)
1011 dsl_destroy_head_arg_t *ddha = arg;
1012 dsl_pool_t *dp = dmu_tx_pool(tx);
1015 VERIFY0(dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds));
1016 dsl_destroy_head_sync_impl(ds, tx);
1017 zvol_remove_minors(dp->dp_spa, ddha->ddha_name, B_TRUE);
1018 dsl_dataset_rele(ds, FTAG);
1022 dsl_destroy_head_begin_sync(void *arg, dmu_tx_t *tx)
1024 dsl_destroy_head_arg_t *ddha = arg;
1025 dsl_pool_t *dp = dmu_tx_pool(tx);
1028 VERIFY0(dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds));
1030 /* Mark it as inconsistent on-disk, in case we crash */
1031 dmu_buf_will_dirty(ds->ds_dbuf, tx);
1032 dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_INCONSISTENT;
1034 spa_history_log_internal_ds(ds, "destroy begin", tx, "");
1035 dsl_dataset_rele(ds, FTAG);
1039 dsl_destroy_head(const char *name)
1041 dsl_destroy_head_arg_t ddha;
1044 boolean_t isenabled;
1047 zfs_destroy_unmount_origin(name);
1050 error = spa_open(name, &spa, FTAG);
1053 isenabled = spa_feature_is_enabled(spa, SPA_FEATURE_ASYNC_DESTROY);
1054 spa_close(spa, FTAG);
1056 ddha.ddha_name = name;
1061 error = dsl_sync_task(name, dsl_destroy_head_check,
1062 dsl_destroy_head_begin_sync, &ddha,
1063 0, ZFS_SPACE_CHECK_DESTROY);
1068 * Head deletion is processed in one txg on old pools;
1069 * remove the objects from open context so that the txg sync
1072 error = dmu_objset_own(name, DMU_OST_ANY, B_FALSE, B_FALSE,
1075 uint64_t prev_snap_txg =
1076 dsl_dataset_phys(dmu_objset_ds(os))->
1078 for (uint64_t obj = 0; error == 0;
1079 error = dmu_object_next(os, &obj, FALSE,
1081 (void) dmu_free_long_object(os, obj);
1082 /* sync out all frees */
1083 txg_wait_synced(dmu_objset_pool(os), 0);
1084 dmu_objset_disown(os, B_FALSE, FTAG);
1088 return (dsl_sync_task(name, dsl_destroy_head_check,
1089 dsl_destroy_head_sync, &ddha, 0, ZFS_SPACE_CHECK_DESTROY));
1093 * Note, this function is used as the callback for dmu_objset_find(). We
1094 * always return 0 so that we will continue to find and process
1095 * inconsistent datasets, even if we encounter an error trying to
1096 * process one of them.
1100 dsl_destroy_inconsistent(const char *dsname, void *arg)
1104 if (dmu_objset_hold(dsname, FTAG, &os) == 0) {
1105 boolean_t need_destroy = DS_IS_INCONSISTENT(dmu_objset_ds(os));
1108 * If the dataset is inconsistent because a resumable receive
1109 * has failed, then do not destroy it.
1111 if (dsl_dataset_has_resume_receive_state(dmu_objset_ds(os)))
1112 need_destroy = B_FALSE;
1114 dmu_objset_rele(os, FTAG);
1116 (void) dsl_destroy_head(dsname);
1122 #if defined(_KERNEL)
1123 EXPORT_SYMBOL(dsl_destroy_head);
1124 EXPORT_SYMBOL(dsl_destroy_head_sync_impl);
1125 EXPORT_SYMBOL(dsl_dataset_user_hold_check_one);
1126 EXPORT_SYMBOL(dsl_destroy_snapshot_sync_impl);
1127 EXPORT_SYMBOL(dsl_destroy_inconsistent);
1128 EXPORT_SYMBOL(dsl_dataset_user_release_tmp);
1129 EXPORT_SYMBOL(dsl_destroy_head_check_impl);