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) 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>
48 dsl_destroy_snapshot_check_impl(dsl_dataset_t
*ds
, boolean_t defer
)
50 if (!ds
->ds_is_snapshot
)
51 return (SET_ERROR(EINVAL
));
53 if (dsl_dataset_long_held(ds
))
54 return (SET_ERROR(EBUSY
));
57 * Only allow deferred destroy on pools that support it.
58 * NOTE: deferred destroy is only supported on snapshots.
61 if (spa_version(ds
->ds_dir
->dd_pool
->dp_spa
) <
63 return (SET_ERROR(ENOTSUP
));
68 * If this snapshot has an elevated user reference count,
69 * we can't destroy it yet.
71 if (ds
->ds_userrefs
> 0)
72 return (SET_ERROR(EBUSY
));
75 * Can't delete a branch point.
77 if (dsl_dataset_phys(ds
)->ds_num_children
> 1)
78 return (SET_ERROR(EEXIST
));
84 dsl_destroy_snapshot_check(void *arg
, dmu_tx_t
*tx
)
86 dsl_destroy_snapshot_arg_t
*ddsa
= arg
;
87 const char *dsname
= ddsa
->ddsa_name
;
88 boolean_t defer
= ddsa
->ddsa_defer
;
90 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
94 error
= dsl_dataset_hold(dp
, dsname
, FTAG
, &ds
);
97 * If the snapshot does not exist, silently ignore it, and
98 * dsl_destroy_snapshot_sync() will be a no-op
99 * (it's "already destroyed").
105 error
= dsl_destroy_snapshot_check_impl(ds
, defer
);
106 dsl_dataset_rele(ds
, FTAG
);
112 struct process_old_arg
{
114 dsl_dataset_t
*ds_prev
;
115 boolean_t after_branch_point
;
117 uint64_t used
, comp
, uncomp
;
121 process_old_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
123 struct process_old_arg
*poa
= arg
;
124 dsl_pool_t
*dp
= poa
->ds
->ds_dir
->dd_pool
;
126 ASSERT(!BP_IS_HOLE(bp
));
128 if (bp
->blk_birth
<= dsl_dataset_phys(poa
->ds
)->ds_prev_snap_txg
) {
129 dsl_deadlist_insert(&poa
->ds
->ds_deadlist
, bp
, tx
);
130 if (poa
->ds_prev
&& !poa
->after_branch_point
&&
132 dsl_dataset_phys(poa
->ds_prev
)->ds_prev_snap_txg
) {
133 dsl_dataset_phys(poa
->ds_prev
)->ds_unique_bytes
+=
134 bp_get_dsize_sync(dp
->dp_spa
, bp
);
137 poa
->used
+= bp_get_dsize_sync(dp
->dp_spa
, bp
);
138 poa
->comp
+= BP_GET_PSIZE(bp
);
139 poa
->uncomp
+= BP_GET_UCSIZE(bp
);
140 dsl_free_sync(poa
->pio
, dp
, tx
->tx_txg
, bp
);
146 process_old_deadlist(dsl_dataset_t
*ds
, dsl_dataset_t
*ds_prev
,
147 dsl_dataset_t
*ds_next
, boolean_t after_branch_point
, dmu_tx_t
*tx
)
149 struct process_old_arg poa
= { 0 };
150 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
151 objset_t
*mos
= dp
->dp_meta_objset
;
152 uint64_t deadlist_obj
;
154 ASSERT(ds
->ds_deadlist
.dl_oldfmt
);
155 ASSERT(ds_next
->ds_deadlist
.dl_oldfmt
);
158 poa
.ds_prev
= ds_prev
;
159 poa
.after_branch_point
= after_branch_point
;
160 poa
.pio
= zio_root(dp
->dp_spa
, NULL
, NULL
, ZIO_FLAG_MUSTSUCCEED
);
161 VERIFY0(bpobj_iterate(&ds_next
->ds_deadlist
.dl_bpobj
,
162 process_old_cb
, &poa
, tx
));
163 VERIFY0(zio_wait(poa
.pio
));
164 ASSERT3U(poa
.used
, ==, dsl_dataset_phys(ds
)->ds_unique_bytes
);
166 /* change snapused */
167 dsl_dir_diduse_space(ds
->ds_dir
, DD_USED_SNAP
,
168 -poa
.used
, -poa
.comp
, -poa
.uncomp
, tx
);
170 /* swap next's deadlist to our deadlist */
171 dsl_deadlist_close(&ds
->ds_deadlist
);
172 dsl_deadlist_close(&ds_next
->ds_deadlist
);
173 deadlist_obj
= dsl_dataset_phys(ds
)->ds_deadlist_obj
;
174 dsl_dataset_phys(ds
)->ds_deadlist_obj
=
175 dsl_dataset_phys(ds_next
)->ds_deadlist_obj
;
176 dsl_dataset_phys(ds_next
)->ds_deadlist_obj
= deadlist_obj
;
177 dsl_deadlist_open(&ds
->ds_deadlist
, mos
,
178 dsl_dataset_phys(ds
)->ds_deadlist_obj
);
179 dsl_deadlist_open(&ds_next
->ds_deadlist
, mos
,
180 dsl_dataset_phys(ds_next
)->ds_deadlist_obj
);
184 dsl_dataset_remove_clones_key(dsl_dataset_t
*ds
, uint64_t mintxg
, dmu_tx_t
*tx
)
186 objset_t
*mos
= ds
->ds_dir
->dd_pool
->dp_meta_objset
;
191 * If it is the old version, dd_clones doesn't exist so we can't
192 * find the clones, but dsl_deadlist_remove_key() is a no-op so it
195 if (dsl_dir_phys(ds
->ds_dir
)->dd_clones
== 0)
198 for (zap_cursor_init(&zc
, mos
, dsl_dir_phys(ds
->ds_dir
)->dd_clones
);
199 zap_cursor_retrieve(&zc
, &za
) == 0;
200 zap_cursor_advance(&zc
)) {
201 dsl_dataset_t
*clone
;
203 VERIFY0(dsl_dataset_hold_obj(ds
->ds_dir
->dd_pool
,
204 za
.za_first_integer
, FTAG
, &clone
));
205 if (clone
->ds_dir
->dd_origin_txg
> mintxg
) {
206 dsl_deadlist_remove_key(&clone
->ds_deadlist
,
208 dsl_dataset_remove_clones_key(clone
, mintxg
, tx
);
210 dsl_dataset_rele(clone
, FTAG
);
212 zap_cursor_fini(&zc
);
216 dsl_destroy_snapshot_sync_impl(dsl_dataset_t
*ds
, boolean_t defer
, dmu_tx_t
*tx
)
219 int after_branch_point
= FALSE
;
220 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
221 objset_t
*mos
= dp
->dp_meta_objset
;
222 dsl_dataset_t
*ds_prev
= NULL
;
225 ASSERT(RRW_WRITE_HELD(&dp
->dp_config_rwlock
));
226 rrw_enter(&ds
->ds_bp_rwlock
, RW_READER
, FTAG
);
227 ASSERT3U(dsl_dataset_phys(ds
)->ds_bp
.blk_birth
, <=, tx
->tx_txg
);
228 rrw_exit(&ds
->ds_bp_rwlock
, FTAG
);
229 ASSERT(refcount_is_zero(&ds
->ds_longholds
));
232 (ds
->ds_userrefs
> 0 ||
233 dsl_dataset_phys(ds
)->ds_num_children
> 1)) {
234 ASSERT(spa_version(dp
->dp_spa
) >= SPA_VERSION_USERREFS
);
235 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
236 dsl_dataset_phys(ds
)->ds_flags
|= DS_FLAG_DEFER_DESTROY
;
237 spa_history_log_internal_ds(ds
, "defer_destroy", tx
, "");
241 ASSERT3U(dsl_dataset_phys(ds
)->ds_num_children
, <=, 1);
243 /* We need to log before removing it from the namespace. */
244 spa_history_log_internal_ds(ds
, "destroy", tx
, "");
246 dsl_scan_ds_destroyed(ds
, tx
);
250 for (spa_feature_t f
= 0; f
< SPA_FEATURES
; f
++) {
251 if (ds
->ds_feature_inuse
[f
]) {
252 dsl_dataset_deactivate_feature(obj
, f
, tx
);
253 ds
->ds_feature_inuse
[f
] = B_FALSE
;
256 if (dsl_dataset_phys(ds
)->ds_prev_snap_obj
!= 0) {
257 ASSERT3P(ds
->ds_prev
, ==, NULL
);
258 VERIFY0(dsl_dataset_hold_obj(dp
,
259 dsl_dataset_phys(ds
)->ds_prev_snap_obj
, FTAG
, &ds_prev
));
261 (dsl_dataset_phys(ds_prev
)->ds_next_snap_obj
!= obj
);
263 dmu_buf_will_dirty(ds_prev
->ds_dbuf
, tx
);
264 if (after_branch_point
&&
265 dsl_dataset_phys(ds_prev
)->ds_next_clones_obj
!= 0) {
266 dsl_dataset_remove_from_next_clones(ds_prev
, obj
, tx
);
267 if (dsl_dataset_phys(ds
)->ds_next_snap_obj
!= 0) {
268 VERIFY0(zap_add_int(mos
,
269 dsl_dataset_phys(ds_prev
)->
271 dsl_dataset_phys(ds
)->ds_next_snap_obj
,
275 if (!after_branch_point
) {
276 dsl_dataset_phys(ds_prev
)->ds_next_snap_obj
=
277 dsl_dataset_phys(ds
)->ds_next_snap_obj
;
281 dsl_dataset_t
*ds_next
;
283 uint64_t used
= 0, comp
= 0, uncomp
= 0;
285 VERIFY0(dsl_dataset_hold_obj(dp
,
286 dsl_dataset_phys(ds
)->ds_next_snap_obj
, FTAG
, &ds_next
));
287 ASSERT3U(dsl_dataset_phys(ds_next
)->ds_prev_snap_obj
, ==, obj
);
289 old_unique
= dsl_dataset_phys(ds_next
)->ds_unique_bytes
;
291 dmu_buf_will_dirty(ds_next
->ds_dbuf
, tx
);
292 dsl_dataset_phys(ds_next
)->ds_prev_snap_obj
=
293 dsl_dataset_phys(ds
)->ds_prev_snap_obj
;
294 dsl_dataset_phys(ds_next
)->ds_prev_snap_txg
=
295 dsl_dataset_phys(ds
)->ds_prev_snap_txg
;
296 ASSERT3U(dsl_dataset_phys(ds
)->ds_prev_snap_txg
, ==,
297 ds_prev
? dsl_dataset_phys(ds_prev
)->ds_creation_txg
: 0);
299 if (ds_next
->ds_deadlist
.dl_oldfmt
) {
300 process_old_deadlist(ds
, ds_prev
, ds_next
,
301 after_branch_point
, tx
);
303 /* Adjust prev's unique space. */
304 if (ds_prev
&& !after_branch_point
) {
305 dsl_deadlist_space_range(&ds_next
->ds_deadlist
,
306 dsl_dataset_phys(ds_prev
)->ds_prev_snap_txg
,
307 dsl_dataset_phys(ds
)->ds_prev_snap_txg
,
308 &used
, &comp
, &uncomp
);
309 dsl_dataset_phys(ds_prev
)->ds_unique_bytes
+= used
;
312 /* Adjust snapused. */
313 dsl_deadlist_space_range(&ds_next
->ds_deadlist
,
314 dsl_dataset_phys(ds
)->ds_prev_snap_txg
, UINT64_MAX
,
315 &used
, &comp
, &uncomp
);
316 dsl_dir_diduse_space(ds
->ds_dir
, DD_USED_SNAP
,
317 -used
, -comp
, -uncomp
, tx
);
319 /* Move blocks to be freed to pool's free list. */
320 dsl_deadlist_move_bpobj(&ds_next
->ds_deadlist
,
321 &dp
->dp_free_bpobj
, dsl_dataset_phys(ds
)->ds_prev_snap_txg
,
323 dsl_dir_diduse_space(tx
->tx_pool
->dp_free_dir
,
324 DD_USED_HEAD
, used
, comp
, uncomp
, tx
);
326 /* Merge our deadlist into next's and free it. */
327 dsl_deadlist_merge(&ds_next
->ds_deadlist
,
328 dsl_dataset_phys(ds
)->ds_deadlist_obj
, tx
);
330 dsl_deadlist_close(&ds
->ds_deadlist
);
331 dsl_deadlist_free(mos
, dsl_dataset_phys(ds
)->ds_deadlist_obj
, tx
);
332 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
333 dsl_dataset_phys(ds
)->ds_deadlist_obj
= 0;
335 /* Collapse range in clone heads */
336 dsl_dataset_remove_clones_key(ds
,
337 dsl_dataset_phys(ds
)->ds_creation_txg
, tx
);
339 if (ds_next
->ds_is_snapshot
) {
340 dsl_dataset_t
*ds_nextnext
;
343 * Update next's unique to include blocks which
344 * were previously shared by only this snapshot
345 * and it. Those blocks will be born after the
346 * prev snap and before this snap, and will have
347 * died after the next snap and before the one
348 * after that (ie. be on the snap after next's
351 VERIFY0(dsl_dataset_hold_obj(dp
,
352 dsl_dataset_phys(ds_next
)->ds_next_snap_obj
,
353 FTAG
, &ds_nextnext
));
354 dsl_deadlist_space_range(&ds_nextnext
->ds_deadlist
,
355 dsl_dataset_phys(ds
)->ds_prev_snap_txg
,
356 dsl_dataset_phys(ds
)->ds_creation_txg
,
357 &used
, &comp
, &uncomp
);
358 dsl_dataset_phys(ds_next
)->ds_unique_bytes
+= used
;
359 dsl_dataset_rele(ds_nextnext
, FTAG
);
360 ASSERT3P(ds_next
->ds_prev
, ==, NULL
);
362 /* Collapse range in this head. */
364 VERIFY0(dsl_dataset_hold_obj(dp
,
365 dsl_dir_phys(ds
->ds_dir
)->dd_head_dataset_obj
, FTAG
, &hds
));
366 dsl_deadlist_remove_key(&hds
->ds_deadlist
,
367 dsl_dataset_phys(ds
)->ds_creation_txg
, tx
);
368 dsl_dataset_rele(hds
, FTAG
);
371 ASSERT3P(ds_next
->ds_prev
, ==, ds
);
372 dsl_dataset_rele(ds_next
->ds_prev
, ds_next
);
373 ds_next
->ds_prev
= NULL
;
375 VERIFY0(dsl_dataset_hold_obj(dp
,
376 dsl_dataset_phys(ds
)->ds_prev_snap_obj
,
377 ds_next
, &ds_next
->ds_prev
));
380 dsl_dataset_recalc_head_uniq(ds_next
);
383 * Reduce the amount of our unconsumed refreservation
384 * being charged to our parent by the amount of
385 * new unique data we have gained.
387 if (old_unique
< ds_next
->ds_reserved
) {
389 uint64_t new_unique
=
390 dsl_dataset_phys(ds_next
)->ds_unique_bytes
;
392 ASSERT(old_unique
<= new_unique
);
393 mrsdelta
= MIN(new_unique
- old_unique
,
394 ds_next
->ds_reserved
- old_unique
);
395 dsl_dir_diduse_space(ds
->ds_dir
,
396 DD_USED_REFRSRV
, -mrsdelta
, 0, 0, tx
);
399 dsl_dataset_rele(ds_next
, FTAG
);
402 * This must be done after the dsl_traverse(), because it will
403 * re-open the objset.
406 dmu_objset_evict(ds
->ds_objset
);
407 ds
->ds_objset
= NULL
;
410 /* remove from snapshot namespace */
411 dsl_dataset_t
*ds_head
;
412 ASSERT(dsl_dataset_phys(ds
)->ds_snapnames_zapobj
== 0);
413 VERIFY0(dsl_dataset_hold_obj(dp
,
414 dsl_dir_phys(ds
->ds_dir
)->dd_head_dataset_obj
, FTAG
, &ds_head
));
415 VERIFY0(dsl_dataset_get_snapname(ds
));
420 err
= dsl_dataset_snap_lookup(ds_head
,
421 ds
->ds_snapname
, &val
);
423 ASSERT3U(val
, ==, obj
);
426 VERIFY0(dsl_dataset_snap_remove(ds_head
, ds
->ds_snapname
, tx
, B_TRUE
));
427 dsl_dataset_rele(ds_head
, FTAG
);
430 dsl_dataset_rele(ds_prev
, FTAG
);
432 spa_prop_clear_bootfs(dp
->dp_spa
, ds
->ds_object
, tx
);
434 if (dsl_dataset_phys(ds
)->ds_next_clones_obj
!= 0) {
436 ASSERT0(zap_count(mos
,
437 dsl_dataset_phys(ds
)->ds_next_clones_obj
, &count
) &&
439 VERIFY0(dmu_object_free(mos
,
440 dsl_dataset_phys(ds
)->ds_next_clones_obj
, tx
));
442 if (dsl_dataset_phys(ds
)->ds_props_obj
!= 0)
443 VERIFY0(zap_destroy(mos
, dsl_dataset_phys(ds
)->ds_props_obj
,
445 if (dsl_dataset_phys(ds
)->ds_userrefs_obj
!= 0)
446 VERIFY0(zap_destroy(mos
, dsl_dataset_phys(ds
)->ds_userrefs_obj
,
448 dsl_dir_rele(ds
->ds_dir
, ds
);
450 dmu_object_free_zapified(mos
, obj
, tx
);
454 dsl_destroy_snapshot_sync(void *arg
, dmu_tx_t
*tx
)
456 dsl_destroy_snapshot_arg_t
*ddsa
= arg
;
457 const char *dsname
= ddsa
->ddsa_name
;
458 boolean_t defer
= ddsa
->ddsa_defer
;
460 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
463 int error
= dsl_dataset_hold(dp
, dsname
, FTAG
, &ds
);
467 dsl_destroy_snapshot_sync_impl(ds
, defer
, tx
);
468 dsl_dataset_rele(ds
, FTAG
);
472 * The semantics of this function are described in the comment above
473 * lzc_destroy_snaps(). To summarize:
475 * The snapshots must all be in the same pool.
477 * Snapshots that don't exist will be silently ignored (considered to be
478 * "already deleted").
480 * On success, all snaps will be destroyed and this will return 0.
481 * On failure, no snaps will be destroyed, the errlist will be filled in,
482 * and this will return an errno.
485 dsl_destroy_snapshots_nvl(nvlist_t
*snaps
, boolean_t defer
,
488 if (nvlist_next_nvpair(snaps
, NULL
) == NULL
)
492 * lzc_destroy_snaps() is documented to take an nvlist whose
493 * values "don't matter". We need to convert that nvlist to
494 * one that we know can be converted to LUA. We also don't
495 * care about any duplicate entries because the nvlist will
496 * be converted to a LUA table which should take care of this.
498 nvlist_t
*snaps_normalized
;
499 VERIFY0(nvlist_alloc(&snaps_normalized
, 0, KM_SLEEP
));
500 for (nvpair_t
*pair
= nvlist_next_nvpair(snaps
, NULL
);
501 pair
!= NULL
; pair
= nvlist_next_nvpair(snaps
, pair
)) {
502 fnvlist_add_boolean_value(snaps_normalized
,
503 nvpair_name(pair
), B_TRUE
);
507 VERIFY0(nvlist_alloc(&arg
, 0, KM_SLEEP
));
508 fnvlist_add_nvlist(arg
, "snaps", snaps_normalized
);
509 fnvlist_free(snaps_normalized
);
510 fnvlist_add_boolean_value(arg
, "defer", defer
);
513 VERIFY0(nvlist_alloc(&wrapper
, 0, KM_SLEEP
));
514 fnvlist_add_nvlist(wrapper
, ZCP_ARG_ARGLIST
, arg
);
517 const char *program
=
519 "snaps = arg['snaps']\n"
520 "defer = arg['defer']\n"
522 "has_errors = false\n"
523 "for snap, v in pairs(snaps) do\n"
524 " errno = zfs.check.destroy{snap, defer=defer}\n"
525 " zfs.debug('snap: ' .. snap .. ' errno: ' .. errno)\n"
526 " if errno == ENOENT then\n"
527 " snaps[snap] = nil\n"
528 " elseif errno ~= 0 then\n"
529 " errors[snap] = errno\n"
530 " has_errors = true\n"
533 "if has_errors then\n"
536 "for snap, v in pairs(snaps) do\n"
537 " errno = zfs.sync.destroy{snap, defer=defer}\n"
538 " assert(errno == 0)\n"
542 nvlist_t
*result
= fnvlist_alloc();
543 int error
= zcp_eval(nvpair_name(nvlist_next_nvpair(snaps
, NULL
)),
546 zfs_lua_max_memlimit
,
547 nvlist_next_nvpair(wrapper
, NULL
), result
);
549 char *errorstr
= NULL
;
550 (void) nvlist_lookup_string(result
, ZCP_RET_ERROR
, &errorstr
);
551 if (errorstr
!= NULL
) {
552 zfs_dbgmsg(errorstr
);
556 fnvlist_free(wrapper
);
559 * lzc_destroy_snaps() is documented to fill the errlist with
560 * int32 values, so we need to covert the int64 values that are
564 nvlist_t
*errlist_raw
= fnvlist_lookup_nvlist(result
, ZCP_RET_RETURN
);
565 for (nvpair_t
*pair
= nvlist_next_nvpair(errlist_raw
, NULL
);
566 pair
!= NULL
; pair
= nvlist_next_nvpair(errlist_raw
, pair
)) {
567 int32_t val
= (int32_t)fnvpair_value_int64(pair
);
570 fnvlist_add_int32(errlist
, nvpair_name(pair
), val
);
572 fnvlist_free(result
);
577 dsl_destroy_snapshot(const char *name
, boolean_t defer
)
580 nvlist_t
*nvl
= fnvlist_alloc();
581 nvlist_t
*errlist
= fnvlist_alloc();
583 fnvlist_add_boolean(nvl
, name
);
584 error
= dsl_destroy_snapshots_nvl(nvl
, defer
, errlist
);
585 fnvlist_free(errlist
);
597 kill_blkptr(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
598 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
600 struct killarg
*ka
= arg
;
601 dmu_tx_t
*tx
= ka
->tx
;
603 if (bp
== NULL
|| BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
))
606 if (zb
->zb_level
== ZB_ZIL_LEVEL
) {
607 ASSERT(zilog
!= NULL
);
609 * It's a block in the intent log. It has no
610 * accounting, so just free it.
612 dsl_free(ka
->tx
->tx_pool
, ka
->tx
->tx_txg
, bp
);
614 ASSERT(zilog
== NULL
);
615 ASSERT3U(bp
->blk_birth
, >,
616 dsl_dataset_phys(ka
->ds
)->ds_prev_snap_txg
);
617 (void) dsl_dataset_block_kill(ka
->ds
, bp
, tx
, B_FALSE
);
624 old_synchronous_dataset_destroy(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
629 * Free everything that we point to (that's born after
630 * the previous snapshot, if we are a clone)
632 * NB: this should be very quick, because we already
633 * freed all the objects in open context.
637 VERIFY0(traverse_dataset(ds
,
638 dsl_dataset_phys(ds
)->ds_prev_snap_txg
, TRAVERSE_POST
,
640 ASSERT(!DS_UNIQUE_IS_ACCURATE(ds
) ||
641 dsl_dataset_phys(ds
)->ds_unique_bytes
== 0);
645 dsl_destroy_head_check_impl(dsl_dataset_t
*ds
, int expected_holds
)
651 ASSERT(!ds
->ds_is_snapshot
);
652 if (ds
->ds_is_snapshot
)
653 return (SET_ERROR(EINVAL
));
655 if (refcount_count(&ds
->ds_longholds
) != expected_holds
)
656 return (SET_ERROR(EBUSY
));
658 mos
= ds
->ds_dir
->dd_pool
->dp_meta_objset
;
661 * Can't delete a head dataset if there are snapshots of it.
662 * (Except if the only snapshots are from the branch we cloned
665 if (ds
->ds_prev
!= NULL
&&
666 dsl_dataset_phys(ds
->ds_prev
)->ds_next_snap_obj
== ds
->ds_object
)
667 return (SET_ERROR(EBUSY
));
670 * Can't delete if there are children of this fs.
672 error
= zap_count(mos
,
673 dsl_dir_phys(ds
->ds_dir
)->dd_child_dir_zapobj
, &count
);
677 return (SET_ERROR(EEXIST
));
679 if (dsl_dir_is_clone(ds
->ds_dir
) && DS_IS_DEFER_DESTROY(ds
->ds_prev
) &&
680 dsl_dataset_phys(ds
->ds_prev
)->ds_num_children
== 2 &&
681 ds
->ds_prev
->ds_userrefs
== 0) {
682 /* We need to remove the origin snapshot as well. */
683 if (!refcount_is_zero(&ds
->ds_prev
->ds_longholds
))
684 return (SET_ERROR(EBUSY
));
690 dsl_destroy_head_check(void *arg
, dmu_tx_t
*tx
)
692 dsl_destroy_head_arg_t
*ddha
= arg
;
693 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
697 error
= dsl_dataset_hold(dp
, ddha
->ddha_name
, FTAG
, &ds
);
701 error
= dsl_destroy_head_check_impl(ds
, 0);
702 dsl_dataset_rele(ds
, FTAG
);
707 dsl_dir_destroy_sync(uint64_t ddobj
, dmu_tx_t
*tx
)
710 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
711 objset_t
*mos
= dp
->dp_meta_objset
;
714 ASSERT(RRW_WRITE_HELD(&dmu_tx_pool(tx
)->dp_config_rwlock
));
716 VERIFY0(dsl_dir_hold_obj(dp
, ddobj
, NULL
, FTAG
, &dd
));
718 ASSERT0(dsl_dir_phys(dd
)->dd_head_dataset_obj
);
721 * Decrement the filesystem count for all parent filesystems.
723 * When we receive an incremental stream into a filesystem that already
724 * exists, a temporary clone is created. We never count this temporary
725 * clone, whose name begins with a '%'.
727 if (dd
->dd_myname
[0] != '%' && dd
->dd_parent
!= NULL
)
728 dsl_fs_ss_count_adjust(dd
->dd_parent
, -1,
729 DD_FIELD_FILESYSTEM_COUNT
, tx
);
732 * Remove our reservation. The impl() routine avoids setting the
733 * actual property, which would require the (already destroyed) ds.
735 dsl_dir_set_reservation_sync_impl(dd
, 0, tx
);
737 ASSERT0(dsl_dir_phys(dd
)->dd_used_bytes
);
738 ASSERT0(dsl_dir_phys(dd
)->dd_reserved
);
739 for (t
= 0; t
< DD_USED_NUM
; t
++)
740 ASSERT0(dsl_dir_phys(dd
)->dd_used_breakdown
[t
]);
742 VERIFY0(zap_destroy(mos
, dsl_dir_phys(dd
)->dd_child_dir_zapobj
, tx
));
743 VERIFY0(zap_destroy(mos
, dsl_dir_phys(dd
)->dd_props_zapobj
, tx
));
744 VERIFY0(dsl_deleg_destroy(mos
, dsl_dir_phys(dd
)->dd_deleg_zapobj
, tx
));
745 VERIFY0(zap_remove(mos
,
746 dsl_dir_phys(dd
->dd_parent
)->dd_child_dir_zapobj
,
749 dsl_dir_rele(dd
, FTAG
);
750 dmu_object_free_zapified(mos
, ddobj
, tx
);
754 dsl_destroy_head_sync_impl(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
756 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
757 objset_t
*mos
= dp
->dp_meta_objset
;
758 uint64_t obj
, ddobj
, prevobj
= 0;
761 ASSERT3U(dsl_dataset_phys(ds
)->ds_num_children
, <=, 1);
762 ASSERT(ds
->ds_prev
== NULL
||
763 dsl_dataset_phys(ds
->ds_prev
)->ds_next_snap_obj
!= ds
->ds_object
);
764 rrw_enter(&ds
->ds_bp_rwlock
, RW_READER
, FTAG
);
765 ASSERT3U(dsl_dataset_phys(ds
)->ds_bp
.blk_birth
, <=, tx
->tx_txg
);
766 rrw_exit(&ds
->ds_bp_rwlock
, FTAG
);
767 ASSERT(RRW_WRITE_HELD(&dp
->dp_config_rwlock
));
769 /* We need to log before removing it from the namespace. */
770 spa_history_log_internal_ds(ds
, "destroy", tx
, "");
772 rmorigin
= (dsl_dir_is_clone(ds
->ds_dir
) &&
773 DS_IS_DEFER_DESTROY(ds
->ds_prev
) &&
774 dsl_dataset_phys(ds
->ds_prev
)->ds_num_children
== 2 &&
775 ds
->ds_prev
->ds_userrefs
== 0);
777 /* Remove our reservation. */
778 if (ds
->ds_reserved
!= 0) {
779 dsl_dataset_set_refreservation_sync_impl(ds
,
780 (ZPROP_SRC_NONE
| ZPROP_SRC_LOCAL
| ZPROP_SRC_RECEIVED
),
782 ASSERT0(ds
->ds_reserved
);
787 for (spa_feature_t f
= 0; f
< SPA_FEATURES
; f
++) {
788 if (ds
->ds_feature_inuse
[f
]) {
789 dsl_dataset_deactivate_feature(obj
, f
, tx
);
790 ds
->ds_feature_inuse
[f
] = B_FALSE
;
794 dsl_scan_ds_destroyed(ds
, tx
);
796 if (dsl_dataset_phys(ds
)->ds_prev_snap_obj
!= 0) {
797 /* This is a clone */
798 ASSERT(ds
->ds_prev
!= NULL
);
799 ASSERT3U(dsl_dataset_phys(ds
->ds_prev
)->ds_next_snap_obj
, !=,
801 ASSERT0(dsl_dataset_phys(ds
)->ds_next_snap_obj
);
803 dmu_buf_will_dirty(ds
->ds_prev
->ds_dbuf
, tx
);
804 if (dsl_dataset_phys(ds
->ds_prev
)->ds_next_clones_obj
!= 0) {
805 dsl_dataset_remove_from_next_clones(ds
->ds_prev
,
809 ASSERT3U(dsl_dataset_phys(ds
->ds_prev
)->ds_num_children
, >, 1);
810 dsl_dataset_phys(ds
->ds_prev
)->ds_num_children
--;
814 * Destroy the deadlist. Unless it's a clone, the
815 * deadlist should be empty. (If it's a clone, it's
816 * safe to ignore the deadlist contents.)
818 dsl_deadlist_close(&ds
->ds_deadlist
);
819 dsl_deadlist_free(mos
, dsl_dataset_phys(ds
)->ds_deadlist_obj
, tx
);
820 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
821 dsl_dataset_phys(ds
)->ds_deadlist_obj
= 0;
824 VERIFY0(dmu_objset_from_ds(ds
, &os
));
826 if (!spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_ASYNC_DESTROY
)) {
827 old_synchronous_dataset_destroy(ds
, tx
);
830 * Move the bptree into the pool's list of trees to
831 * clean up and update space accounting information.
833 uint64_t used
, comp
, uncomp
;
835 zil_destroy_sync(dmu_objset_zil(os
), tx
);
837 if (!spa_feature_is_active(dp
->dp_spa
,
838 SPA_FEATURE_ASYNC_DESTROY
)) {
839 dsl_scan_t
*scn
= dp
->dp_scan
;
840 spa_feature_incr(dp
->dp_spa
, SPA_FEATURE_ASYNC_DESTROY
,
842 dp
->dp_bptree_obj
= bptree_alloc(mos
, tx
);
844 DMU_POOL_DIRECTORY_OBJECT
,
845 DMU_POOL_BPTREE_OBJ
, sizeof (uint64_t), 1,
846 &dp
->dp_bptree_obj
, tx
));
847 ASSERT(!scn
->scn_async_destroying
);
848 scn
->scn_async_destroying
= B_TRUE
;
851 used
= dsl_dir_phys(ds
->ds_dir
)->dd_used_bytes
;
852 comp
= dsl_dir_phys(ds
->ds_dir
)->dd_compressed_bytes
;
853 uncomp
= dsl_dir_phys(ds
->ds_dir
)->dd_uncompressed_bytes
;
855 ASSERT(!DS_UNIQUE_IS_ACCURATE(ds
) ||
856 dsl_dataset_phys(ds
)->ds_unique_bytes
== used
);
858 rrw_enter(&ds
->ds_bp_rwlock
, RW_READER
, FTAG
);
859 bptree_add(mos
, dp
->dp_bptree_obj
,
860 &dsl_dataset_phys(ds
)->ds_bp
,
861 dsl_dataset_phys(ds
)->ds_prev_snap_txg
,
862 used
, comp
, uncomp
, tx
);
863 rrw_exit(&ds
->ds_bp_rwlock
, FTAG
);
864 dsl_dir_diduse_space(ds
->ds_dir
, DD_USED_HEAD
,
865 -used
, -comp
, -uncomp
, tx
);
866 dsl_dir_diduse_space(dp
->dp_free_dir
, DD_USED_HEAD
,
867 used
, comp
, uncomp
, tx
);
870 if (ds
->ds_prev
!= NULL
) {
871 if (spa_version(dp
->dp_spa
) >= SPA_VERSION_DIR_CLONES
) {
872 VERIFY0(zap_remove_int(mos
,
873 dsl_dir_phys(ds
->ds_prev
->ds_dir
)->dd_clones
,
876 prevobj
= ds
->ds_prev
->ds_object
;
877 dsl_dataset_rele(ds
->ds_prev
, ds
);
882 * This must be done after the dsl_traverse(), because it will
883 * re-open the objset.
886 dmu_objset_evict(ds
->ds_objset
);
887 ds
->ds_objset
= NULL
;
890 /* Erase the link in the dir */
891 dmu_buf_will_dirty(ds
->ds_dir
->dd_dbuf
, tx
);
892 dsl_dir_phys(ds
->ds_dir
)->dd_head_dataset_obj
= 0;
893 ddobj
= ds
->ds_dir
->dd_object
;
894 ASSERT(dsl_dataset_phys(ds
)->ds_snapnames_zapobj
!= 0);
895 VERIFY0(zap_destroy(mos
,
896 dsl_dataset_phys(ds
)->ds_snapnames_zapobj
, tx
));
898 if (ds
->ds_bookmarks
!= 0) {
899 VERIFY0(zap_destroy(mos
, ds
->ds_bookmarks
, tx
));
900 spa_feature_decr(dp
->dp_spa
, SPA_FEATURE_BOOKMARKS
, tx
);
903 spa_prop_clear_bootfs(dp
->dp_spa
, ds
->ds_object
, tx
);
905 ASSERT0(dsl_dataset_phys(ds
)->ds_next_clones_obj
);
906 ASSERT0(dsl_dataset_phys(ds
)->ds_props_obj
);
907 ASSERT0(dsl_dataset_phys(ds
)->ds_userrefs_obj
);
908 dsl_dir_rele(ds
->ds_dir
, ds
);
910 dmu_object_free_zapified(mos
, obj
, tx
);
912 dsl_dir_destroy_sync(ddobj
, tx
);
916 VERIFY0(dsl_dataset_hold_obj(dp
, prevobj
, FTAG
, &prev
));
917 dsl_destroy_snapshot_sync_impl(prev
, B_FALSE
, tx
);
918 dsl_dataset_rele(prev
, FTAG
);
923 dsl_destroy_head_sync(void *arg
, dmu_tx_t
*tx
)
925 dsl_destroy_head_arg_t
*ddha
= arg
;
926 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
929 VERIFY0(dsl_dataset_hold(dp
, ddha
->ddha_name
, FTAG
, &ds
));
930 dsl_destroy_head_sync_impl(ds
, tx
);
931 dsl_dataset_rele(ds
, FTAG
);
935 dsl_destroy_head_begin_sync(void *arg
, dmu_tx_t
*tx
)
937 dsl_destroy_head_arg_t
*ddha
= arg
;
938 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
941 VERIFY0(dsl_dataset_hold(dp
, ddha
->ddha_name
, FTAG
, &ds
));
943 /* Mark it as inconsistent on-disk, in case we crash */
944 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
945 dsl_dataset_phys(ds
)->ds_flags
|= DS_FLAG_INCONSISTENT
;
947 spa_history_log_internal_ds(ds
, "destroy begin", tx
, "");
948 dsl_dataset_rele(ds
, FTAG
);
952 dsl_destroy_head(const char *name
)
954 dsl_destroy_head_arg_t ddha
;
960 zfs_destroy_unmount_origin(name
);
963 error
= spa_open(name
, &spa
, FTAG
);
966 isenabled
= spa_feature_is_enabled(spa
, SPA_FEATURE_ASYNC_DESTROY
);
967 spa_close(spa
, FTAG
);
969 ddha
.ddha_name
= name
;
974 error
= dsl_sync_task(name
, dsl_destroy_head_check
,
975 dsl_destroy_head_begin_sync
, &ddha
,
976 0, ZFS_SPACE_CHECK_NONE
);
981 * Head deletion is processed in one txg on old pools;
982 * remove the objects from open context so that the txg sync
985 error
= dmu_objset_own(name
, DMU_OST_ANY
, B_FALSE
, FTAG
, &os
);
987 uint64_t prev_snap_txg
=
988 dsl_dataset_phys(dmu_objset_ds(os
))->
990 for (uint64_t obj
= 0; error
== 0;
991 error
= dmu_object_next(os
, &obj
, FALSE
,
993 (void) dmu_free_long_object(os
, obj
);
994 /* sync out all frees */
995 txg_wait_synced(dmu_objset_pool(os
), 0);
996 dmu_objset_disown(os
, FTAG
);
1000 return (dsl_sync_task(name
, dsl_destroy_head_check
,
1001 dsl_destroy_head_sync
, &ddha
, 0, ZFS_SPACE_CHECK_NONE
));
1005 * Note, this function is used as the callback for dmu_objset_find(). We
1006 * always return 0 so that we will continue to find and process
1007 * inconsistent datasets, even if we encounter an error trying to
1008 * process one of them.
1012 dsl_destroy_inconsistent(const char *dsname
, void *arg
)
1016 if (dmu_objset_hold(dsname
, FTAG
, &os
) == 0) {
1017 boolean_t need_destroy
= DS_IS_INCONSISTENT(dmu_objset_ds(os
));
1020 * If the dataset is inconsistent because a resumable receive
1021 * has failed, then do not destroy it.
1023 if (dsl_dataset_has_resume_receive_state(dmu_objset_ds(os
)))
1024 need_destroy
= B_FALSE
;
1026 dmu_objset_rele(os
, FTAG
);
1028 (void) dsl_destroy_head(dsname
);