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, 2016 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
);
185 dsl_dataset_remove_clones_key(dsl_dataset_t
*ds
, uint64_t mintxg
, dmu_tx_t
*tx
)
187 objset_t
*mos
= ds
->ds_dir
->dd_pool
->dp_meta_objset
;
192 * If it is the old version, dd_clones doesn't exist so we can't
193 * find the clones, but dsl_deadlist_remove_key() is a no-op so it
196 if (dsl_dir_phys(ds
->ds_dir
)->dd_clones
== 0)
199 zc
= kmem_alloc(sizeof (zap_cursor_t
), KM_SLEEP
);
200 za
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
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 dsl_dataset_remove_clones_key(clone
, mintxg
, tx
);
214 dsl_dataset_rele(clone
, FTAG
);
218 kmem_free(za
, sizeof (zap_attribute_t
));
219 kmem_free(zc
, sizeof (zap_cursor_t
));
223 dsl_destroy_snapshot_sync_impl(dsl_dataset_t
*ds
, boolean_t defer
, dmu_tx_t
*tx
)
225 int after_branch_point
= FALSE
;
226 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
227 objset_t
*mos
= dp
->dp_meta_objset
;
228 dsl_dataset_t
*ds_prev
= NULL
;
231 ASSERT(RRW_WRITE_HELD(&dp
->dp_config_rwlock
));
232 rrw_enter(&ds
->ds_bp_rwlock
, RW_READER
, FTAG
);
233 ASSERT3U(dsl_dataset_phys(ds
)->ds_bp
.blk_birth
, <=, tx
->tx_txg
);
234 rrw_exit(&ds
->ds_bp_rwlock
, FTAG
);
235 ASSERT(refcount_is_zero(&ds
->ds_longholds
));
238 (ds
->ds_userrefs
> 0 ||
239 dsl_dataset_phys(ds
)->ds_num_children
> 1)) {
240 ASSERT(spa_version(dp
->dp_spa
) >= SPA_VERSION_USERREFS
);
241 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
242 dsl_dataset_phys(ds
)->ds_flags
|= DS_FLAG_DEFER_DESTROY
;
243 spa_history_log_internal_ds(ds
, "defer_destroy", tx
, "");
247 ASSERT3U(dsl_dataset_phys(ds
)->ds_num_children
, <=, 1);
249 /* We need to log before removing it from the namespace. */
250 spa_history_log_internal_ds(ds
, "destroy", tx
, "");
252 dsl_scan_ds_destroyed(ds
, tx
);
256 for (spa_feature_t f
= 0; f
< SPA_FEATURES
; f
++) {
257 if (ds
->ds_feature_inuse
[f
]) {
258 dsl_dataset_deactivate_feature(obj
, f
, tx
);
259 ds
->ds_feature_inuse
[f
] = B_FALSE
;
262 if (dsl_dataset_phys(ds
)->ds_prev_snap_obj
!= 0) {
263 ASSERT3P(ds
->ds_prev
, ==, NULL
);
264 VERIFY0(dsl_dataset_hold_obj(dp
,
265 dsl_dataset_phys(ds
)->ds_prev_snap_obj
, FTAG
, &ds_prev
));
267 (dsl_dataset_phys(ds_prev
)->ds_next_snap_obj
!= obj
);
269 dmu_buf_will_dirty(ds_prev
->ds_dbuf
, tx
);
270 if (after_branch_point
&&
271 dsl_dataset_phys(ds_prev
)->ds_next_clones_obj
!= 0) {
272 dsl_dataset_remove_from_next_clones(ds_prev
, obj
, tx
);
273 if (dsl_dataset_phys(ds
)->ds_next_snap_obj
!= 0) {
274 VERIFY0(zap_add_int(mos
,
275 dsl_dataset_phys(ds_prev
)->
277 dsl_dataset_phys(ds
)->ds_next_snap_obj
,
281 if (!after_branch_point
) {
282 dsl_dataset_phys(ds_prev
)->ds_next_snap_obj
=
283 dsl_dataset_phys(ds
)->ds_next_snap_obj
;
287 dsl_dataset_t
*ds_next
;
289 uint64_t used
= 0, comp
= 0, uncomp
= 0;
291 VERIFY0(dsl_dataset_hold_obj(dp
,
292 dsl_dataset_phys(ds
)->ds_next_snap_obj
, FTAG
, &ds_next
));
293 ASSERT3U(dsl_dataset_phys(ds_next
)->ds_prev_snap_obj
, ==, obj
);
295 old_unique
= dsl_dataset_phys(ds_next
)->ds_unique_bytes
;
297 dmu_buf_will_dirty(ds_next
->ds_dbuf
, tx
);
298 dsl_dataset_phys(ds_next
)->ds_prev_snap_obj
=
299 dsl_dataset_phys(ds
)->ds_prev_snap_obj
;
300 dsl_dataset_phys(ds_next
)->ds_prev_snap_txg
=
301 dsl_dataset_phys(ds
)->ds_prev_snap_txg
;
302 ASSERT3U(dsl_dataset_phys(ds
)->ds_prev_snap_txg
, ==,
303 ds_prev
? dsl_dataset_phys(ds_prev
)->ds_creation_txg
: 0);
305 if (ds_next
->ds_deadlist
.dl_oldfmt
) {
306 process_old_deadlist(ds
, ds_prev
, ds_next
,
307 after_branch_point
, tx
);
309 /* Adjust prev's unique space. */
310 if (ds_prev
&& !after_branch_point
) {
311 dsl_deadlist_space_range(&ds_next
->ds_deadlist
,
312 dsl_dataset_phys(ds_prev
)->ds_prev_snap_txg
,
313 dsl_dataset_phys(ds
)->ds_prev_snap_txg
,
314 &used
, &comp
, &uncomp
);
315 dsl_dataset_phys(ds_prev
)->ds_unique_bytes
+= used
;
318 /* Adjust snapused. */
319 dsl_deadlist_space_range(&ds_next
->ds_deadlist
,
320 dsl_dataset_phys(ds
)->ds_prev_snap_txg
, UINT64_MAX
,
321 &used
, &comp
, &uncomp
);
322 dsl_dir_diduse_space(ds
->ds_dir
, DD_USED_SNAP
,
323 -used
, -comp
, -uncomp
, tx
);
325 /* Move blocks to be freed to pool's free list. */
326 dsl_deadlist_move_bpobj(&ds_next
->ds_deadlist
,
327 &dp
->dp_free_bpobj
, dsl_dataset_phys(ds
)->ds_prev_snap_txg
,
329 dsl_dir_diduse_space(tx
->tx_pool
->dp_free_dir
,
330 DD_USED_HEAD
, used
, comp
, uncomp
, tx
);
332 /* Merge our deadlist into next's and free it. */
333 dsl_deadlist_merge(&ds_next
->ds_deadlist
,
334 dsl_dataset_phys(ds
)->ds_deadlist_obj
, tx
);
336 dsl_deadlist_close(&ds
->ds_deadlist
);
337 dsl_deadlist_free(mos
, dsl_dataset_phys(ds
)->ds_deadlist_obj
, tx
);
338 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
339 dsl_dataset_phys(ds
)->ds_deadlist_obj
= 0;
341 /* Collapse range in clone heads */
342 dsl_dataset_remove_clones_key(ds
,
343 dsl_dataset_phys(ds
)->ds_creation_txg
, tx
);
345 if (ds_next
->ds_is_snapshot
) {
346 dsl_dataset_t
*ds_nextnext
;
349 * Update next's unique to include blocks which
350 * were previously shared by only this snapshot
351 * and it. Those blocks will be born after the
352 * prev snap and before this snap, and will have
353 * died after the next snap and before the one
354 * after that (ie. be on the snap after next's
357 VERIFY0(dsl_dataset_hold_obj(dp
,
358 dsl_dataset_phys(ds_next
)->ds_next_snap_obj
,
359 FTAG
, &ds_nextnext
));
360 dsl_deadlist_space_range(&ds_nextnext
->ds_deadlist
,
361 dsl_dataset_phys(ds
)->ds_prev_snap_txg
,
362 dsl_dataset_phys(ds
)->ds_creation_txg
,
363 &used
, &comp
, &uncomp
);
364 dsl_dataset_phys(ds_next
)->ds_unique_bytes
+= used
;
365 dsl_dataset_rele(ds_nextnext
, FTAG
);
366 ASSERT3P(ds_next
->ds_prev
, ==, NULL
);
368 /* Collapse range in this head. */
370 VERIFY0(dsl_dataset_hold_obj(dp
,
371 dsl_dir_phys(ds
->ds_dir
)->dd_head_dataset_obj
, FTAG
, &hds
));
372 dsl_deadlist_remove_key(&hds
->ds_deadlist
,
373 dsl_dataset_phys(ds
)->ds_creation_txg
, tx
);
374 dsl_dataset_rele(hds
, FTAG
);
377 ASSERT3P(ds_next
->ds_prev
, ==, ds
);
378 dsl_dataset_rele(ds_next
->ds_prev
, ds_next
);
379 ds_next
->ds_prev
= NULL
;
381 VERIFY0(dsl_dataset_hold_obj(dp
,
382 dsl_dataset_phys(ds
)->ds_prev_snap_obj
,
383 ds_next
, &ds_next
->ds_prev
));
386 dsl_dataset_recalc_head_uniq(ds_next
);
389 * Reduce the amount of our unconsumed refreservation
390 * being charged to our parent by the amount of
391 * new unique data we have gained.
393 if (old_unique
< ds_next
->ds_reserved
) {
395 uint64_t new_unique
=
396 dsl_dataset_phys(ds_next
)->ds_unique_bytes
;
398 ASSERT(old_unique
<= new_unique
);
399 mrsdelta
= MIN(new_unique
- old_unique
,
400 ds_next
->ds_reserved
- old_unique
);
401 dsl_dir_diduse_space(ds
->ds_dir
,
402 DD_USED_REFRSRV
, -mrsdelta
, 0, 0, tx
);
405 dsl_dataset_rele(ds_next
, FTAG
);
408 * This must be done after the dsl_traverse(), because it will
409 * re-open the objset.
412 dmu_objset_evict(ds
->ds_objset
);
413 ds
->ds_objset
= NULL
;
416 /* remove from snapshot namespace */
417 dsl_dataset_t
*ds_head
;
418 ASSERT(dsl_dataset_phys(ds
)->ds_snapnames_zapobj
== 0);
419 VERIFY0(dsl_dataset_hold_obj(dp
,
420 dsl_dir_phys(ds
->ds_dir
)->dd_head_dataset_obj
, FTAG
, &ds_head
));
421 VERIFY0(dsl_dataset_get_snapname(ds
));
427 err
= dsl_dataset_snap_lookup(ds_head
,
428 ds
->ds_snapname
, &val
);
430 ASSERT3U(val
, ==, obj
);
433 VERIFY0(dsl_dataset_snap_remove(ds_head
, ds
->ds_snapname
, tx
, B_TRUE
));
434 dsl_dataset_rele(ds_head
, FTAG
);
437 dsl_dataset_rele(ds_prev
, FTAG
);
439 spa_prop_clear_bootfs(dp
->dp_spa
, ds
->ds_object
, tx
);
441 if (dsl_dataset_phys(ds
)->ds_next_clones_obj
!= 0) {
442 ASSERTV(uint64_t count
);
443 ASSERT0(zap_count(mos
,
444 dsl_dataset_phys(ds
)->ds_next_clones_obj
, &count
) &&
446 VERIFY0(dmu_object_free(mos
,
447 dsl_dataset_phys(ds
)->ds_next_clones_obj
, tx
));
449 if (dsl_dataset_phys(ds
)->ds_props_obj
!= 0)
450 VERIFY0(zap_destroy(mos
, dsl_dataset_phys(ds
)->ds_props_obj
,
452 if (dsl_dataset_phys(ds
)->ds_userrefs_obj
!= 0)
453 VERIFY0(zap_destroy(mos
, dsl_dataset_phys(ds
)->ds_userrefs_obj
,
455 dsl_dir_rele(ds
->ds_dir
, ds
);
457 dmu_object_free_zapified(mos
, obj
, tx
);
461 dsl_destroy_snapshot_sync(void *arg
, dmu_tx_t
*tx
)
463 dsl_destroy_snapshot_arg_t
*ddsa
= arg
;
464 const char *dsname
= ddsa
->ddsa_name
;
465 boolean_t defer
= ddsa
->ddsa_defer
;
467 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
470 int error
= dsl_dataset_hold(dp
, dsname
, FTAG
, &ds
);
474 dsl_destroy_snapshot_sync_impl(ds
, defer
, tx
);
475 zvol_remove_minors(dp
->dp_spa
, dsname
, B_TRUE
);
476 dsl_dataset_rele(ds
, FTAG
);
480 * The semantics of this function are described in the comment above
481 * lzc_destroy_snaps(). To summarize:
483 * The snapshots must all be in the same pool.
485 * Snapshots that don't exist will be silently ignored (considered to be
486 * "already deleted").
488 * On success, all snaps will be destroyed and this will return 0.
489 * On failure, no snaps will be destroyed, the errlist will be filled in,
490 * and this will return an errno.
493 dsl_destroy_snapshots_nvl(nvlist_t
*snaps
, boolean_t defer
,
496 if (nvlist_next_nvpair(snaps
, NULL
) == NULL
)
499 nvlist_t
*arg
= fnvlist_alloc();
500 nvlist_t
*snaps_normalized
= fnvlist_alloc();
502 * lzc_destroy_snaps() is documented to take an nvlist whose
503 * values "don't matter". We need to convert that nvlist to one
504 * that we know can be converted to LUA.
506 for (nvpair_t
*pair
= nvlist_next_nvpair(snaps
, NULL
);
507 pair
!= NULL
; pair
= nvlist_next_nvpair(snaps
, pair
)) {
508 fnvlist_add_boolean_value(snaps_normalized
,
509 nvpair_name(pair
), B_TRUE
);
511 fnvlist_add_nvlist(arg
, "snaps", snaps_normalized
);
512 fnvlist_free(snaps_normalized
);
513 fnvlist_add_boolean_value(arg
, "defer", defer
);
515 nvlist_t
*wrapper
= fnvlist_alloc();
516 fnvlist_add_nvlist(wrapper
, ZCP_ARG_ARGLIST
, arg
);
519 const char *program
=
521 "snaps = arg['snaps']\n"
522 "defer = arg['defer']\n"
524 "has_errors = false\n"
525 "for snap, v in pairs(snaps) do\n"
526 " errno = zfs.check.destroy{snap, defer=defer}\n"
527 " zfs.debug('snap: ' .. snap .. ' errno: ' .. errno)\n"
528 " if errno == ENOENT then\n"
529 " snaps[snap] = nil\n"
530 " elseif errno ~= 0 then\n"
531 " errors[snap] = errno\n"
532 " has_errors = true\n"
535 "if has_errors then\n"
538 "for snap, v in pairs(snaps) do\n"
539 " errno = zfs.sync.destroy{snap, defer=defer}\n"
540 " assert(errno == 0)\n"
544 nvlist_t
*result
= fnvlist_alloc();
545 int error
= zcp_eval(nvpair_name(nvlist_next_nvpair(snaps
, NULL
)),
548 zfs_lua_max_memlimit
,
549 fnvlist_lookup_nvpair(wrapper
, ZCP_ARG_ARGLIST
), result
);
551 char *errorstr
= NULL
;
552 (void) nvlist_lookup_string(result
, ZCP_RET_ERROR
, &errorstr
);
553 if (errorstr
!= NULL
) {
554 zfs_dbgmsg(errorstr
);
558 fnvlist_free(wrapper
);
561 * lzc_destroy_snaps() is documented to fill the errlist with
562 * int32 values, so we need to covert the int64 values that are
566 nvlist_t
*errlist_raw
= fnvlist_lookup_nvlist(result
, ZCP_RET_RETURN
);
567 for (nvpair_t
*pair
= nvlist_next_nvpair(errlist_raw
, NULL
);
568 pair
!= NULL
; pair
= nvlist_next_nvpair(errlist_raw
, pair
)) {
569 int32_t val
= (int32_t)fnvpair_value_int64(pair
);
572 fnvlist_add_int32(errlist
, nvpair_name(pair
), val
);
574 fnvlist_free(result
);
579 dsl_destroy_snapshot(const char *name
, boolean_t defer
)
582 nvlist_t
*nvl
= fnvlist_alloc();
583 nvlist_t
*errlist
= fnvlist_alloc();
585 fnvlist_add_boolean(nvl
, name
);
586 error
= dsl_destroy_snapshots_nvl(nvl
, defer
, errlist
);
587 fnvlist_free(errlist
);
599 kill_blkptr(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
600 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
602 struct killarg
*ka
= arg
;
603 dmu_tx_t
*tx
= ka
->tx
;
605 if (bp
== NULL
|| BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
))
608 if (zb
->zb_level
== ZB_ZIL_LEVEL
) {
609 ASSERT(zilog
!= NULL
);
611 * It's a block in the intent log. It has no
612 * accounting, so just free it.
614 dsl_free(ka
->tx
->tx_pool
, ka
->tx
->tx_txg
, bp
);
616 ASSERT(zilog
== NULL
);
617 ASSERT3U(bp
->blk_birth
, >,
618 dsl_dataset_phys(ka
->ds
)->ds_prev_snap_txg
);
619 (void) dsl_dataset_block_kill(ka
->ds
, bp
, tx
, B_FALSE
);
626 old_synchronous_dataset_destroy(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
631 * Free everything that we point to (that's born after
632 * the previous snapshot, if we are a clone)
634 * NB: this should be very quick, because we already
635 * freed all the objects in open context.
639 VERIFY0(traverse_dataset(ds
,
640 dsl_dataset_phys(ds
)->ds_prev_snap_txg
, TRAVERSE_POST
|
641 TRAVERSE_NO_DECRYPT
, kill_blkptr
, &ka
));
642 ASSERT(!DS_UNIQUE_IS_ACCURATE(ds
) ||
643 dsl_dataset_phys(ds
)->ds_unique_bytes
== 0);
647 dsl_destroy_head_check_impl(dsl_dataset_t
*ds
, int expected_holds
)
653 ASSERT(!ds
->ds_is_snapshot
);
654 if (ds
->ds_is_snapshot
)
655 return (SET_ERROR(EINVAL
));
657 if (refcount_count(&ds
->ds_longholds
) != expected_holds
)
658 return (SET_ERROR(EBUSY
));
660 mos
= ds
->ds_dir
->dd_pool
->dp_meta_objset
;
663 * Can't delete a head dataset if there are snapshots of it.
664 * (Except if the only snapshots are from the branch we cloned
667 if (ds
->ds_prev
!= NULL
&&
668 dsl_dataset_phys(ds
->ds_prev
)->ds_next_snap_obj
== ds
->ds_object
)
669 return (SET_ERROR(EBUSY
));
672 * Can't delete if there are children of this fs.
674 error
= zap_count(mos
,
675 dsl_dir_phys(ds
->ds_dir
)->dd_child_dir_zapobj
, &count
);
679 return (SET_ERROR(EEXIST
));
681 if (dsl_dir_is_clone(ds
->ds_dir
) && DS_IS_DEFER_DESTROY(ds
->ds_prev
) &&
682 dsl_dataset_phys(ds
->ds_prev
)->ds_num_children
== 2 &&
683 ds
->ds_prev
->ds_userrefs
== 0) {
684 /* We need to remove the origin snapshot as well. */
685 if (!refcount_is_zero(&ds
->ds_prev
->ds_longholds
))
686 return (SET_ERROR(EBUSY
));
692 dsl_destroy_head_check(void *arg
, dmu_tx_t
*tx
)
694 dsl_destroy_head_arg_t
*ddha
= arg
;
695 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
699 error
= dsl_dataset_hold(dp
, ddha
->ddha_name
, FTAG
, &ds
);
703 error
= dsl_destroy_head_check_impl(ds
, 0);
704 dsl_dataset_rele(ds
, FTAG
);
709 dsl_dir_destroy_sync(uint64_t ddobj
, dmu_tx_t
*tx
)
712 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
713 objset_t
*mos
= dp
->dp_meta_objset
;
716 ASSERT(RRW_WRITE_HELD(&dmu_tx_pool(tx
)->dp_config_rwlock
));
718 VERIFY0(dsl_dir_hold_obj(dp
, ddobj
, NULL
, FTAG
, &dd
));
720 ASSERT0(dsl_dir_phys(dd
)->dd_head_dataset_obj
);
723 * Decrement the filesystem count for all parent filesystems.
725 * When we receive an incremental stream into a filesystem that already
726 * exists, a temporary clone is created. We never count this temporary
727 * clone, whose name begins with a '%'.
729 if (dd
->dd_myname
[0] != '%' && dd
->dd_parent
!= NULL
)
730 dsl_fs_ss_count_adjust(dd
->dd_parent
, -1,
731 DD_FIELD_FILESYSTEM_COUNT
, tx
);
734 * Remove our reservation. The impl() routine avoids setting the
735 * actual property, which would require the (already destroyed) ds.
737 dsl_dir_set_reservation_sync_impl(dd
, 0, tx
);
739 ASSERT0(dsl_dir_phys(dd
)->dd_used_bytes
);
740 ASSERT0(dsl_dir_phys(dd
)->dd_reserved
);
741 for (t
= 0; t
< DD_USED_NUM
; t
++)
742 ASSERT0(dsl_dir_phys(dd
)->dd_used_breakdown
[t
]);
744 if (dd
->dd_crypto_obj
!= 0) {
745 dsl_crypto_key_destroy_sync(dd
->dd_crypto_obj
, tx
);
746 (void) spa_keystore_unload_wkey_impl(dp
->dp_spa
, dd
->dd_object
);
749 VERIFY0(zap_destroy(mos
, dsl_dir_phys(dd
)->dd_child_dir_zapobj
, tx
));
750 VERIFY0(zap_destroy(mos
, dsl_dir_phys(dd
)->dd_props_zapobj
, tx
));
751 VERIFY0(dsl_deleg_destroy(mos
, dsl_dir_phys(dd
)->dd_deleg_zapobj
, tx
));
752 VERIFY0(zap_remove(mos
,
753 dsl_dir_phys(dd
->dd_parent
)->dd_child_dir_zapobj
,
756 dsl_dir_rele(dd
, FTAG
);
757 dmu_object_free_zapified(mos
, ddobj
, tx
);
761 dsl_destroy_head_sync_impl(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
763 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
764 objset_t
*mos
= dp
->dp_meta_objset
;
765 uint64_t obj
, ddobj
, prevobj
= 0;
768 ASSERT3U(dsl_dataset_phys(ds
)->ds_num_children
, <=, 1);
769 ASSERT(ds
->ds_prev
== NULL
||
770 dsl_dataset_phys(ds
->ds_prev
)->ds_next_snap_obj
!= ds
->ds_object
);
771 rrw_enter(&ds
->ds_bp_rwlock
, RW_READER
, FTAG
);
772 ASSERT3U(dsl_dataset_phys(ds
)->ds_bp
.blk_birth
, <=, tx
->tx_txg
);
773 rrw_exit(&ds
->ds_bp_rwlock
, FTAG
);
774 ASSERT(RRW_WRITE_HELD(&dp
->dp_config_rwlock
));
776 /* We need to log before removing it from the namespace. */
777 spa_history_log_internal_ds(ds
, "destroy", tx
, "");
779 rmorigin
= (dsl_dir_is_clone(ds
->ds_dir
) &&
780 DS_IS_DEFER_DESTROY(ds
->ds_prev
) &&
781 dsl_dataset_phys(ds
->ds_prev
)->ds_num_children
== 2 &&
782 ds
->ds_prev
->ds_userrefs
== 0);
784 /* Remove our reservation. */
785 if (ds
->ds_reserved
!= 0) {
786 dsl_dataset_set_refreservation_sync_impl(ds
,
787 (ZPROP_SRC_NONE
| ZPROP_SRC_LOCAL
| ZPROP_SRC_RECEIVED
),
789 ASSERT0(ds
->ds_reserved
);
794 for (spa_feature_t f
= 0; f
< SPA_FEATURES
; f
++) {
795 if (ds
->ds_feature_inuse
[f
]) {
796 dsl_dataset_deactivate_feature(obj
, f
, tx
);
797 ds
->ds_feature_inuse
[f
] = B_FALSE
;
801 dsl_scan_ds_destroyed(ds
, tx
);
803 if (dsl_dataset_phys(ds
)->ds_prev_snap_obj
!= 0) {
804 /* This is a clone */
805 ASSERT(ds
->ds_prev
!= NULL
);
806 ASSERT3U(dsl_dataset_phys(ds
->ds_prev
)->ds_next_snap_obj
, !=,
808 ASSERT0(dsl_dataset_phys(ds
)->ds_next_snap_obj
);
810 dmu_buf_will_dirty(ds
->ds_prev
->ds_dbuf
, tx
);
811 if (dsl_dataset_phys(ds
->ds_prev
)->ds_next_clones_obj
!= 0) {
812 dsl_dataset_remove_from_next_clones(ds
->ds_prev
,
816 ASSERT3U(dsl_dataset_phys(ds
->ds_prev
)->ds_num_children
, >, 1);
817 dsl_dataset_phys(ds
->ds_prev
)->ds_num_children
--;
821 * Destroy the deadlist. Unless it's a clone, the
822 * deadlist should be empty. (If it's a clone, it's
823 * safe to ignore the deadlist contents.)
825 dsl_deadlist_close(&ds
->ds_deadlist
);
826 dsl_deadlist_free(mos
, dsl_dataset_phys(ds
)->ds_deadlist_obj
, tx
);
827 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
828 dsl_dataset_phys(ds
)->ds_deadlist_obj
= 0;
831 VERIFY0(dmu_objset_from_ds(ds
, &os
));
833 if (!spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_ASYNC_DESTROY
)) {
834 old_synchronous_dataset_destroy(ds
, tx
);
837 * Move the bptree into the pool's list of trees to
838 * clean up and update space accounting information.
840 uint64_t used
, comp
, uncomp
;
842 zil_destroy_sync(dmu_objset_zil(os
), tx
);
844 if (!spa_feature_is_active(dp
->dp_spa
,
845 SPA_FEATURE_ASYNC_DESTROY
)) {
846 dsl_scan_t
*scn
= dp
->dp_scan
;
847 spa_feature_incr(dp
->dp_spa
, SPA_FEATURE_ASYNC_DESTROY
,
849 dp
->dp_bptree_obj
= bptree_alloc(mos
, tx
);
851 DMU_POOL_DIRECTORY_OBJECT
,
852 DMU_POOL_BPTREE_OBJ
, sizeof (uint64_t), 1,
853 &dp
->dp_bptree_obj
, tx
));
854 ASSERT(!scn
->scn_async_destroying
);
855 scn
->scn_async_destroying
= B_TRUE
;
858 used
= dsl_dir_phys(ds
->ds_dir
)->dd_used_bytes
;
859 comp
= dsl_dir_phys(ds
->ds_dir
)->dd_compressed_bytes
;
860 uncomp
= dsl_dir_phys(ds
->ds_dir
)->dd_uncompressed_bytes
;
862 ASSERT(!DS_UNIQUE_IS_ACCURATE(ds
) ||
863 dsl_dataset_phys(ds
)->ds_unique_bytes
== used
);
865 rrw_enter(&ds
->ds_bp_rwlock
, RW_READER
, FTAG
);
866 bptree_add(mos
, dp
->dp_bptree_obj
,
867 &dsl_dataset_phys(ds
)->ds_bp
,
868 dsl_dataset_phys(ds
)->ds_prev_snap_txg
,
869 used
, comp
, uncomp
, tx
);
870 rrw_exit(&ds
->ds_bp_rwlock
, FTAG
);
871 dsl_dir_diduse_space(ds
->ds_dir
, DD_USED_HEAD
,
872 -used
, -comp
, -uncomp
, tx
);
873 dsl_dir_diduse_space(dp
->dp_free_dir
, DD_USED_HEAD
,
874 used
, comp
, uncomp
, tx
);
877 if (ds
->ds_prev
!= NULL
) {
878 if (spa_version(dp
->dp_spa
) >= SPA_VERSION_DIR_CLONES
) {
879 VERIFY0(zap_remove_int(mos
,
880 dsl_dir_phys(ds
->ds_prev
->ds_dir
)->dd_clones
,
883 prevobj
= ds
->ds_prev
->ds_object
;
884 dsl_dataset_rele(ds
->ds_prev
, ds
);
889 * This must be done after the dsl_traverse(), because it will
890 * re-open the objset.
893 dmu_objset_evict(ds
->ds_objset
);
894 ds
->ds_objset
= NULL
;
897 /* Erase the link in the dir */
898 dmu_buf_will_dirty(ds
->ds_dir
->dd_dbuf
, tx
);
899 dsl_dir_phys(ds
->ds_dir
)->dd_head_dataset_obj
= 0;
900 ddobj
= ds
->ds_dir
->dd_object
;
901 ASSERT(dsl_dataset_phys(ds
)->ds_snapnames_zapobj
!= 0);
902 VERIFY0(zap_destroy(mos
,
903 dsl_dataset_phys(ds
)->ds_snapnames_zapobj
, tx
));
905 if (ds
->ds_bookmarks
!= 0) {
906 VERIFY0(zap_destroy(mos
, ds
->ds_bookmarks
, tx
));
907 spa_feature_decr(dp
->dp_spa
, SPA_FEATURE_BOOKMARKS
, tx
);
910 spa_prop_clear_bootfs(dp
->dp_spa
, ds
->ds_object
, tx
);
912 ASSERT0(dsl_dataset_phys(ds
)->ds_next_clones_obj
);
913 ASSERT0(dsl_dataset_phys(ds
)->ds_props_obj
);
914 ASSERT0(dsl_dataset_phys(ds
)->ds_userrefs_obj
);
915 dsl_dir_rele(ds
->ds_dir
, ds
);
917 dmu_object_free_zapified(mos
, obj
, tx
);
919 dsl_dir_destroy_sync(ddobj
, tx
);
923 VERIFY0(dsl_dataset_hold_obj(dp
, prevobj
, FTAG
, &prev
));
924 dsl_destroy_snapshot_sync_impl(prev
, B_FALSE
, tx
);
925 dsl_dataset_rele(prev
, FTAG
);
930 dsl_destroy_head_sync(void *arg
, dmu_tx_t
*tx
)
932 dsl_destroy_head_arg_t
*ddha
= arg
;
933 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
936 VERIFY0(dsl_dataset_hold(dp
, ddha
->ddha_name
, FTAG
, &ds
));
937 dsl_destroy_head_sync_impl(ds
, tx
);
938 zvol_remove_minors(dp
->dp_spa
, ddha
->ddha_name
, B_TRUE
);
939 dsl_dataset_rele(ds
, FTAG
);
943 dsl_destroy_head_begin_sync(void *arg
, dmu_tx_t
*tx
)
945 dsl_destroy_head_arg_t
*ddha
= arg
;
946 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
949 VERIFY0(dsl_dataset_hold(dp
, ddha
->ddha_name
, FTAG
, &ds
));
951 /* Mark it as inconsistent on-disk, in case we crash */
952 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
953 dsl_dataset_phys(ds
)->ds_flags
|= DS_FLAG_INCONSISTENT
;
955 spa_history_log_internal_ds(ds
, "destroy begin", tx
, "");
956 dsl_dataset_rele(ds
, FTAG
);
960 dsl_destroy_head(const char *name
)
962 dsl_destroy_head_arg_t ddha
;
968 zfs_destroy_unmount_origin(name
);
971 error
= spa_open(name
, &spa
, FTAG
);
974 isenabled
= spa_feature_is_enabled(spa
, SPA_FEATURE_ASYNC_DESTROY
);
975 spa_close(spa
, FTAG
);
977 ddha
.ddha_name
= name
;
982 error
= dsl_sync_task(name
, dsl_destroy_head_check
,
983 dsl_destroy_head_begin_sync
, &ddha
,
984 0, ZFS_SPACE_CHECK_NONE
);
989 * Head deletion is processed in one txg on old pools;
990 * remove the objects from open context so that the txg sync
993 error
= dmu_objset_own(name
, DMU_OST_ANY
, B_FALSE
, B_FALSE
,
996 uint64_t prev_snap_txg
=
997 dsl_dataset_phys(dmu_objset_ds(os
))->
999 for (uint64_t obj
= 0; error
== 0;
1000 error
= dmu_object_next(os
, &obj
, FALSE
,
1002 (void) dmu_free_long_object(os
, obj
);
1003 /* sync out all frees */
1004 txg_wait_synced(dmu_objset_pool(os
), 0);
1005 dmu_objset_disown(os
, B_FALSE
, FTAG
);
1009 return (dsl_sync_task(name
, dsl_destroy_head_check
,
1010 dsl_destroy_head_sync
, &ddha
, 0, ZFS_SPACE_CHECK_NONE
));
1014 * Note, this function is used as the callback for dmu_objset_find(). We
1015 * always return 0 so that we will continue to find and process
1016 * inconsistent datasets, even if we encounter an error trying to
1017 * process one of them.
1021 dsl_destroy_inconsistent(const char *dsname
, void *arg
)
1025 if (dmu_objset_hold(dsname
, FTAG
, &os
) == 0) {
1026 boolean_t need_destroy
= DS_IS_INCONSISTENT(dmu_objset_ds(os
));
1029 * If the dataset is inconsistent because a resumable receive
1030 * has failed, then do not destroy it.
1032 if (dsl_dataset_has_resume_receive_state(dmu_objset_ds(os
)))
1033 need_destroy
= B_FALSE
;
1035 dmu_objset_rele(os
, FTAG
);
1037 (void) dsl_destroy_head(dsname
);
1043 #if defined(_KERNEL) && defined(HAVE_SPL)
1044 EXPORT_SYMBOL(dsl_destroy_head
);
1045 EXPORT_SYMBOL(dsl_destroy_head_sync_impl
);
1046 EXPORT_SYMBOL(dsl_dataset_user_hold_check_one
);
1047 EXPORT_SYMBOL(dsl_destroy_snapshot_sync_impl
);
1048 EXPORT_SYMBOL(dsl_destroy_inconsistent
);
1049 EXPORT_SYMBOL(dsl_dataset_user_release_tmp
);
1050 EXPORT_SYMBOL(dsl_destroy_head_check_impl
);