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, 2014 by Delphix. All rights reserved.
24 * Copyright (c) 2013 Steven Hartland. All rights reserved.
25 * Copyright (c) 2013 by Joyent, Inc. All rights reserved.
28 #include <sys/zfs_context.h>
29 #include <sys/dsl_userhold.h>
30 #include <sys/dsl_dataset.h>
31 #include <sys/dsl_synctask.h>
32 #include <sys/dmu_tx.h>
33 #include <sys/dsl_pool.h>
34 #include <sys/dsl_dir.h>
35 #include <sys/dmu_traverse.h>
36 #include <sys/dsl_scan.h>
37 #include <sys/dmu_objset.h>
39 #include <sys/zfeature.h>
40 #include <sys/zfs_ioctl.h>
41 #include <sys/dsl_deleg.h>
42 #include <sys/dmu_impl.h>
44 typedef struct dmu_snapshots_destroy_arg
{
46 nvlist_t
*dsda_successful_snaps
;
48 nvlist_t
*dsda_errlist
;
49 } dmu_snapshots_destroy_arg_t
;
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 dmu_snapshots_destroy_arg_t
*dsda
= arg
;
91 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
95 if (!dmu_tx_is_syncing(tx
))
98 for (pair
= nvlist_next_nvpair(dsda
->dsda_snaps
, NULL
);
99 pair
!= NULL
; pair
= nvlist_next_nvpair(dsda
->dsda_snaps
, pair
)) {
102 error
= dsl_dataset_hold(dp
, nvpair_name(pair
),
106 * If the snapshot does not exist, silently ignore it
107 * (it's "already destroyed").
113 error
= dsl_destroy_snapshot_check_impl(ds
,
115 dsl_dataset_rele(ds
, FTAG
);
119 fnvlist_add_boolean(dsda
->dsda_successful_snaps
,
122 fnvlist_add_int32(dsda
->dsda_errlist
,
123 nvpair_name(pair
), error
);
127 pair
= nvlist_next_nvpair(dsda
->dsda_errlist
, NULL
);
129 return (fnvpair_value_int32(pair
));
134 struct process_old_arg
{
136 dsl_dataset_t
*ds_prev
;
137 boolean_t after_branch_point
;
139 uint64_t used
, comp
, uncomp
;
143 process_old_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
145 struct process_old_arg
*poa
= arg
;
146 dsl_pool_t
*dp
= poa
->ds
->ds_dir
->dd_pool
;
148 ASSERT(!BP_IS_HOLE(bp
));
150 if (bp
->blk_birth
<= dsl_dataset_phys(poa
->ds
)->ds_prev_snap_txg
) {
151 dsl_deadlist_insert(&poa
->ds
->ds_deadlist
, bp
, tx
);
152 if (poa
->ds_prev
&& !poa
->after_branch_point
&&
154 dsl_dataset_phys(poa
->ds_prev
)->ds_prev_snap_txg
) {
155 dsl_dataset_phys(poa
->ds_prev
)->ds_unique_bytes
+=
156 bp_get_dsize_sync(dp
->dp_spa
, bp
);
159 poa
->used
+= bp_get_dsize_sync(dp
->dp_spa
, bp
);
160 poa
->comp
+= BP_GET_PSIZE(bp
);
161 poa
->uncomp
+= BP_GET_UCSIZE(bp
);
162 dsl_free_sync(poa
->pio
, dp
, tx
->tx_txg
, bp
);
168 process_old_deadlist(dsl_dataset_t
*ds
, dsl_dataset_t
*ds_prev
,
169 dsl_dataset_t
*ds_next
, boolean_t after_branch_point
, dmu_tx_t
*tx
)
171 struct process_old_arg poa
= { 0 };
172 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
173 objset_t
*mos
= dp
->dp_meta_objset
;
174 uint64_t deadlist_obj
;
176 ASSERT(ds
->ds_deadlist
.dl_oldfmt
);
177 ASSERT(ds_next
->ds_deadlist
.dl_oldfmt
);
180 poa
.ds_prev
= ds_prev
;
181 poa
.after_branch_point
= after_branch_point
;
182 poa
.pio
= zio_root(dp
->dp_spa
, NULL
, NULL
, ZIO_FLAG_MUSTSUCCEED
);
183 VERIFY0(bpobj_iterate(&ds_next
->ds_deadlist
.dl_bpobj
,
184 process_old_cb
, &poa
, tx
));
185 VERIFY0(zio_wait(poa
.pio
));
186 ASSERT3U(poa
.used
, ==, dsl_dataset_phys(ds
)->ds_unique_bytes
);
188 /* change snapused */
189 dsl_dir_diduse_space(ds
->ds_dir
, DD_USED_SNAP
,
190 -poa
.used
, -poa
.comp
, -poa
.uncomp
, tx
);
192 /* swap next's deadlist to our deadlist */
193 dsl_deadlist_close(&ds
->ds_deadlist
);
194 dsl_deadlist_close(&ds_next
->ds_deadlist
);
195 deadlist_obj
= dsl_dataset_phys(ds
)->ds_deadlist_obj
;
196 dsl_dataset_phys(ds
)->ds_deadlist_obj
=
197 dsl_dataset_phys(ds_next
)->ds_deadlist_obj
;
198 dsl_dataset_phys(ds_next
)->ds_deadlist_obj
= deadlist_obj
;
199 dsl_deadlist_open(&ds
->ds_deadlist
, mos
,
200 dsl_dataset_phys(ds
)->ds_deadlist_obj
);
201 dsl_deadlist_open(&ds_next
->ds_deadlist
, mos
,
202 dsl_dataset_phys(ds_next
)->ds_deadlist_obj
);
206 dsl_dataset_remove_clones_key(dsl_dataset_t
*ds
, uint64_t mintxg
, dmu_tx_t
*tx
)
208 objset_t
*mos
= ds
->ds_dir
->dd_pool
->dp_meta_objset
;
213 * If it is the old version, dd_clones doesn't exist so we can't
214 * find the clones, but dsl_deadlist_remove_key() is a no-op so it
217 if (dsl_dir_phys(ds
->ds_dir
)->dd_clones
== 0)
220 zc
= kmem_alloc(sizeof (zap_cursor_t
), KM_SLEEP
);
221 za
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
223 for (zap_cursor_init(zc
, mos
, dsl_dir_phys(ds
->ds_dir
)->dd_clones
);
224 zap_cursor_retrieve(zc
, za
) == 0;
225 zap_cursor_advance(zc
)) {
226 dsl_dataset_t
*clone
;
228 VERIFY0(dsl_dataset_hold_obj(ds
->ds_dir
->dd_pool
,
229 za
->za_first_integer
, FTAG
, &clone
));
230 if (clone
->ds_dir
->dd_origin_txg
> mintxg
) {
231 dsl_deadlist_remove_key(&clone
->ds_deadlist
,
233 dsl_dataset_remove_clones_key(clone
, mintxg
, tx
);
235 dsl_dataset_rele(clone
, FTAG
);
239 kmem_free(za
, sizeof (zap_attribute_t
));
240 kmem_free(zc
, sizeof (zap_cursor_t
));
244 dsl_destroy_snapshot_sync_impl(dsl_dataset_t
*ds
, boolean_t defer
, dmu_tx_t
*tx
)
249 int after_branch_point
= FALSE
;
250 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
251 objset_t
*mos
= dp
->dp_meta_objset
;
252 dsl_dataset_t
*ds_prev
= NULL
;
253 uint64_t obj
, old_unique
, used
= 0, comp
= 0, uncomp
= 0;
254 dsl_dataset_t
*ds_next
, *ds_head
, *hds
;
257 ASSERT(RRW_WRITE_HELD(&dp
->dp_config_rwlock
));
258 ASSERT3U(dsl_dataset_phys(ds
)->ds_bp
.blk_birth
, <=, tx
->tx_txg
);
259 ASSERT(refcount_is_zero(&ds
->ds_longholds
));
262 (ds
->ds_userrefs
> 0 ||
263 dsl_dataset_phys(ds
)->ds_num_children
> 1)) {
264 ASSERT(spa_version(dp
->dp_spa
) >= SPA_VERSION_USERREFS
);
265 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
266 dsl_dataset_phys(ds
)->ds_flags
|= DS_FLAG_DEFER_DESTROY
;
267 spa_history_log_internal_ds(ds
, "defer_destroy", tx
, "");
271 ASSERT3U(dsl_dataset_phys(ds
)->ds_num_children
, <=, 1);
273 /* We need to log before removing it from the namespace. */
274 spa_history_log_internal_ds(ds
, "destroy", tx
, "");
276 dsl_scan_ds_destroyed(ds
, tx
);
280 if (dsl_dataset_phys(ds
)->ds_prev_snap_obj
!= 0) {
281 ASSERT3P(ds
->ds_prev
, ==, NULL
);
282 VERIFY0(dsl_dataset_hold_obj(dp
,
283 dsl_dataset_phys(ds
)->ds_prev_snap_obj
, FTAG
, &ds_prev
));
285 (dsl_dataset_phys(ds_prev
)->ds_next_snap_obj
!= obj
);
287 dmu_buf_will_dirty(ds_prev
->ds_dbuf
, tx
);
288 if (after_branch_point
&&
289 dsl_dataset_phys(ds_prev
)->ds_next_clones_obj
!= 0) {
290 dsl_dataset_remove_from_next_clones(ds_prev
, obj
, tx
);
291 if (dsl_dataset_phys(ds
)->ds_next_snap_obj
!= 0) {
292 VERIFY0(zap_add_int(mos
,
293 dsl_dataset_phys(ds_prev
)->
295 dsl_dataset_phys(ds
)->ds_next_snap_obj
,
299 if (!after_branch_point
) {
300 dsl_dataset_phys(ds_prev
)->ds_next_snap_obj
=
301 dsl_dataset_phys(ds
)->ds_next_snap_obj
;
305 VERIFY0(dsl_dataset_hold_obj(dp
,
306 dsl_dataset_phys(ds
)->ds_next_snap_obj
, FTAG
, &ds_next
));
307 ASSERT3U(dsl_dataset_phys(ds_next
)->ds_prev_snap_obj
, ==, obj
);
309 old_unique
= dsl_dataset_phys(ds_next
)->ds_unique_bytes
;
311 dmu_buf_will_dirty(ds_next
->ds_dbuf
, tx
);
312 dsl_dataset_phys(ds_next
)->ds_prev_snap_obj
=
313 dsl_dataset_phys(ds
)->ds_prev_snap_obj
;
314 dsl_dataset_phys(ds_next
)->ds_prev_snap_txg
=
315 dsl_dataset_phys(ds
)->ds_prev_snap_txg
;
316 ASSERT3U(dsl_dataset_phys(ds
)->ds_prev_snap_txg
, ==,
317 ds_prev
? dsl_dataset_phys(ds_prev
)->ds_creation_txg
: 0);
319 if (ds_next
->ds_deadlist
.dl_oldfmt
) {
320 process_old_deadlist(ds
, ds_prev
, ds_next
,
321 after_branch_point
, tx
);
323 /* Adjust prev's unique space. */
324 if (ds_prev
&& !after_branch_point
) {
325 dsl_deadlist_space_range(&ds_next
->ds_deadlist
,
326 dsl_dataset_phys(ds_prev
)->ds_prev_snap_txg
,
327 dsl_dataset_phys(ds
)->ds_prev_snap_txg
,
328 &used
, &comp
, &uncomp
);
329 dsl_dataset_phys(ds_prev
)->ds_unique_bytes
+= used
;
332 /* Adjust snapused. */
333 dsl_deadlist_space_range(&ds_next
->ds_deadlist
,
334 dsl_dataset_phys(ds
)->ds_prev_snap_txg
, UINT64_MAX
,
335 &used
, &comp
, &uncomp
);
336 dsl_dir_diduse_space(ds
->ds_dir
, DD_USED_SNAP
,
337 -used
, -comp
, -uncomp
, tx
);
339 /* Move blocks to be freed to pool's free list. */
340 dsl_deadlist_move_bpobj(&ds_next
->ds_deadlist
,
341 &dp
->dp_free_bpobj
, dsl_dataset_phys(ds
)->ds_prev_snap_txg
,
343 dsl_dir_diduse_space(tx
->tx_pool
->dp_free_dir
,
344 DD_USED_HEAD
, used
, comp
, uncomp
, tx
);
346 /* Merge our deadlist into next's and free it. */
347 dsl_deadlist_merge(&ds_next
->ds_deadlist
,
348 dsl_dataset_phys(ds
)->ds_deadlist_obj
, tx
);
350 dsl_deadlist_close(&ds
->ds_deadlist
);
351 dsl_deadlist_free(mos
, dsl_dataset_phys(ds
)->ds_deadlist_obj
, tx
);
352 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
353 dsl_dataset_phys(ds
)->ds_deadlist_obj
= 0;
355 /* Collapse range in clone heads */
356 dsl_dataset_remove_clones_key(ds
,
357 dsl_dataset_phys(ds
)->ds_creation_txg
, tx
);
359 if (ds_next
->ds_is_snapshot
) {
360 dsl_dataset_t
*ds_nextnext
;
363 * Update next's unique to include blocks which
364 * were previously shared by only this snapshot
365 * and it. Those blocks will be born after the
366 * prev snap and before this snap, and will have
367 * died after the next snap and before the one
368 * after that (ie. be on the snap after next's
371 VERIFY0(dsl_dataset_hold_obj(dp
,
372 dsl_dataset_phys(ds_next
)->ds_next_snap_obj
,
373 FTAG
, &ds_nextnext
));
374 dsl_deadlist_space_range(&ds_nextnext
->ds_deadlist
,
375 dsl_dataset_phys(ds
)->ds_prev_snap_txg
,
376 dsl_dataset_phys(ds
)->ds_creation_txg
,
377 &used
, &comp
, &uncomp
);
378 dsl_dataset_phys(ds_next
)->ds_unique_bytes
+= used
;
379 dsl_dataset_rele(ds_nextnext
, FTAG
);
380 ASSERT3P(ds_next
->ds_prev
, ==, NULL
);
382 /* Collapse range in this head. */
383 VERIFY0(dsl_dataset_hold_obj(dp
,
384 dsl_dir_phys(ds
->ds_dir
)->dd_head_dataset_obj
, FTAG
, &hds
));
385 dsl_deadlist_remove_key(&hds
->ds_deadlist
,
386 dsl_dataset_phys(ds
)->ds_creation_txg
, tx
);
387 dsl_dataset_rele(hds
, FTAG
);
390 ASSERT3P(ds_next
->ds_prev
, ==, ds
);
391 dsl_dataset_rele(ds_next
->ds_prev
, ds_next
);
392 ds_next
->ds_prev
= NULL
;
394 VERIFY0(dsl_dataset_hold_obj(dp
,
395 dsl_dataset_phys(ds
)->ds_prev_snap_obj
,
396 ds_next
, &ds_next
->ds_prev
));
399 dsl_dataset_recalc_head_uniq(ds_next
);
402 * Reduce the amount of our unconsumed refreservation
403 * being charged to our parent by the amount of
404 * new unique data we have gained.
406 if (old_unique
< ds_next
->ds_reserved
) {
408 uint64_t new_unique
=
409 dsl_dataset_phys(ds_next
)->ds_unique_bytes
;
411 ASSERT(old_unique
<= new_unique
);
412 mrsdelta
= MIN(new_unique
- old_unique
,
413 ds_next
->ds_reserved
- old_unique
);
414 dsl_dir_diduse_space(ds
->ds_dir
,
415 DD_USED_REFRSRV
, -mrsdelta
, 0, 0, tx
);
418 dsl_dataset_rele(ds_next
, FTAG
);
421 * This must be done after the dsl_traverse(), because it will
422 * re-open the objset.
425 dmu_objset_evict(ds
->ds_objset
);
426 ds
->ds_objset
= NULL
;
429 /* remove from snapshot namespace */
430 ASSERT(dsl_dataset_phys(ds
)->ds_snapnames_zapobj
== 0);
431 VERIFY0(dsl_dataset_hold_obj(dp
,
432 dsl_dir_phys(ds
->ds_dir
)->dd_head_dataset_obj
, FTAG
, &ds_head
));
433 VERIFY0(dsl_dataset_get_snapname(ds
));
438 err
= dsl_dataset_snap_lookup(ds_head
,
439 ds
->ds_snapname
, &val
);
441 ASSERT3U(val
, ==, obj
);
444 VERIFY0(dsl_dataset_snap_remove(ds_head
, ds
->ds_snapname
, tx
, B_TRUE
));
445 dsl_dataset_rele(ds_head
, FTAG
);
448 dsl_dataset_rele(ds_prev
, FTAG
);
450 spa_prop_clear_bootfs(dp
->dp_spa
, ds
->ds_object
, tx
);
452 if (dsl_dataset_phys(ds
)->ds_next_clones_obj
!= 0) {
453 ASSERTV(uint64_t count
);
454 ASSERT0(zap_count(mos
,
455 dsl_dataset_phys(ds
)->ds_next_clones_obj
, &count
) &&
457 VERIFY0(dmu_object_free(mos
,
458 dsl_dataset_phys(ds
)->ds_next_clones_obj
, tx
));
460 if (dsl_dataset_phys(ds
)->ds_props_obj
!= 0)
461 VERIFY0(zap_destroy(mos
, dsl_dataset_phys(ds
)->ds_props_obj
,
463 if (dsl_dataset_phys(ds
)->ds_userrefs_obj
!= 0)
464 VERIFY0(zap_destroy(mos
, dsl_dataset_phys(ds
)->ds_userrefs_obj
,
466 dsl_dir_rele(ds
->ds_dir
, ds
);
468 dmu_object_free_zapified(mos
, obj
, tx
);
472 dsl_destroy_snapshot_sync(void *arg
, dmu_tx_t
*tx
)
474 dmu_snapshots_destroy_arg_t
*dsda
= arg
;
475 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
478 for (pair
= nvlist_next_nvpair(dsda
->dsda_successful_snaps
, NULL
);
480 pair
= nvlist_next_nvpair(dsda
->dsda_successful_snaps
, pair
)) {
483 VERIFY0(dsl_dataset_hold(dp
, nvpair_name(pair
), FTAG
, &ds
));
485 dsl_destroy_snapshot_sync_impl(ds
, dsda
->dsda_defer
, tx
);
486 dsl_dataset_rele(ds
, FTAG
);
491 * The semantics of this function are described in the comment above
492 * lzc_destroy_snaps(). To summarize:
494 * The snapshots must all be in the same pool.
496 * Snapshots that don't exist will be silently ignored (considered to be
497 * "already deleted").
499 * On success, all snaps will be destroyed and this will return 0.
500 * On failure, no snaps will be destroyed, the errlist will be filled in,
501 * and this will return an errno.
504 dsl_destroy_snapshots_nvl(nvlist_t
*snaps
, boolean_t defer
,
507 dmu_snapshots_destroy_arg_t dsda
;
511 pair
= nvlist_next_nvpair(snaps
, NULL
);
515 dsda
.dsda_snaps
= snaps
;
516 VERIFY0(nvlist_alloc(&dsda
.dsda_successful_snaps
,
517 NV_UNIQUE_NAME
, KM_SLEEP
));
518 dsda
.dsda_defer
= defer
;
519 dsda
.dsda_errlist
= errlist
;
521 error
= dsl_sync_task(nvpair_name(pair
),
522 dsl_destroy_snapshot_check
, dsl_destroy_snapshot_sync
,
524 fnvlist_free(dsda
.dsda_successful_snaps
);
530 dsl_destroy_snapshot(const char *name
, boolean_t defer
)
533 nvlist_t
*nvl
= fnvlist_alloc();
534 nvlist_t
*errlist
= fnvlist_alloc();
536 fnvlist_add_boolean(nvl
, name
);
537 error
= dsl_destroy_snapshots_nvl(nvl
, defer
, errlist
);
538 fnvlist_free(errlist
);
550 kill_blkptr(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
551 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
553 struct killarg
*ka
= arg
;
554 dmu_tx_t
*tx
= ka
->tx
;
556 if (BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
))
559 if (zb
->zb_level
== ZB_ZIL_LEVEL
) {
560 ASSERT(zilog
!= NULL
);
562 * It's a block in the intent log. It has no
563 * accounting, so just free it.
565 dsl_free(ka
->tx
->tx_pool
, ka
->tx
->tx_txg
, bp
);
567 ASSERT(zilog
== NULL
);
568 ASSERT3U(bp
->blk_birth
, >,
569 dsl_dataset_phys(ka
->ds
)->ds_prev_snap_txg
);
570 (void) dsl_dataset_block_kill(ka
->ds
, bp
, tx
, B_FALSE
);
577 old_synchronous_dataset_destroy(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
582 * Free everything that we point to (that's born after
583 * the previous snapshot, if we are a clone)
585 * NB: this should be very quick, because we already
586 * freed all the objects in open context.
590 VERIFY0(traverse_dataset(ds
,
591 dsl_dataset_phys(ds
)->ds_prev_snap_txg
, TRAVERSE_POST
,
593 ASSERT(!DS_UNIQUE_IS_ACCURATE(ds
) ||
594 dsl_dataset_phys(ds
)->ds_unique_bytes
== 0);
597 typedef struct dsl_destroy_head_arg
{
598 const char *ddha_name
;
599 } dsl_destroy_head_arg_t
;
602 dsl_destroy_head_check_impl(dsl_dataset_t
*ds
, int expected_holds
)
608 ASSERT(!ds
->ds_is_snapshot
);
609 if (ds
->ds_is_snapshot
)
610 return (SET_ERROR(EINVAL
));
612 if (refcount_count(&ds
->ds_longholds
) != expected_holds
)
613 return (SET_ERROR(EBUSY
));
615 mos
= ds
->ds_dir
->dd_pool
->dp_meta_objset
;
618 * Can't delete a head dataset if there are snapshots of it.
619 * (Except if the only snapshots are from the branch we cloned
622 if (ds
->ds_prev
!= NULL
&&
623 dsl_dataset_phys(ds
->ds_prev
)->ds_next_snap_obj
== ds
->ds_object
)
624 return (SET_ERROR(EBUSY
));
627 * Can't delete if there are children of this fs.
629 error
= zap_count(mos
,
630 dsl_dir_phys(ds
->ds_dir
)->dd_child_dir_zapobj
, &count
);
634 return (SET_ERROR(EEXIST
));
636 if (dsl_dir_is_clone(ds
->ds_dir
) && DS_IS_DEFER_DESTROY(ds
->ds_prev
) &&
637 dsl_dataset_phys(ds
->ds_prev
)->ds_num_children
== 2 &&
638 ds
->ds_prev
->ds_userrefs
== 0) {
639 /* We need to remove the origin snapshot as well. */
640 if (!refcount_is_zero(&ds
->ds_prev
->ds_longholds
))
641 return (SET_ERROR(EBUSY
));
647 dsl_destroy_head_check(void *arg
, dmu_tx_t
*tx
)
649 dsl_destroy_head_arg_t
*ddha
= arg
;
650 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
654 error
= dsl_dataset_hold(dp
, ddha
->ddha_name
, FTAG
, &ds
);
658 error
= dsl_destroy_head_check_impl(ds
, 0);
659 dsl_dataset_rele(ds
, FTAG
);
664 dsl_dir_destroy_sync(uint64_t ddobj
, dmu_tx_t
*tx
)
667 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
668 objset_t
*mos
= dp
->dp_meta_objset
;
671 ASSERT(RRW_WRITE_HELD(&dmu_tx_pool(tx
)->dp_config_rwlock
));
673 VERIFY0(dsl_dir_hold_obj(dp
, ddobj
, NULL
, FTAG
, &dd
));
675 ASSERT0(dsl_dir_phys(dd
)->dd_head_dataset_obj
);
678 * Decrement the filesystem count for all parent filesystems.
680 * When we receive an incremental stream into a filesystem that already
681 * exists, a temporary clone is created. We never count this temporary
682 * clone, whose name begins with a '%'.
684 if (dd
->dd_myname
[0] != '%' && dd
->dd_parent
!= NULL
)
685 dsl_fs_ss_count_adjust(dd
->dd_parent
, -1,
686 DD_FIELD_FILESYSTEM_COUNT
, tx
);
689 * Remove our reservation. The impl() routine avoids setting the
690 * actual property, which would require the (already destroyed) ds.
692 dsl_dir_set_reservation_sync_impl(dd
, 0, tx
);
694 ASSERT0(dsl_dir_phys(dd
)->dd_used_bytes
);
695 ASSERT0(dsl_dir_phys(dd
)->dd_reserved
);
696 for (t
= 0; t
< DD_USED_NUM
; t
++)
697 ASSERT0(dsl_dir_phys(dd
)->dd_used_breakdown
[t
]);
699 VERIFY0(zap_destroy(mos
, dsl_dir_phys(dd
)->dd_child_dir_zapobj
, tx
));
700 VERIFY0(zap_destroy(mos
, dsl_dir_phys(dd
)->dd_props_zapobj
, tx
));
701 VERIFY0(dsl_deleg_destroy(mos
, dsl_dir_phys(dd
)->dd_deleg_zapobj
, tx
));
702 VERIFY0(zap_remove(mos
,
703 dsl_dir_phys(dd
->dd_parent
)->dd_child_dir_zapobj
,
706 dsl_dir_rele(dd
, FTAG
);
707 dmu_object_free_zapified(mos
, ddobj
, tx
);
711 dsl_destroy_head_sync_impl(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
713 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
714 objset_t
*mos
= dp
->dp_meta_objset
;
715 uint64_t obj
, ddobj
, prevobj
= 0;
719 ASSERT3U(dsl_dataset_phys(ds
)->ds_num_children
, <=, 1);
720 ASSERT(ds
->ds_prev
== NULL
||
721 dsl_dataset_phys(ds
->ds_prev
)->ds_next_snap_obj
!= ds
->ds_object
);
722 ASSERT3U(dsl_dataset_phys(ds
)->ds_bp
.blk_birth
, <=, tx
->tx_txg
);
723 ASSERT(RRW_WRITE_HELD(&dp
->dp_config_rwlock
));
725 /* We need to log before removing it from the namespace. */
726 spa_history_log_internal_ds(ds
, "destroy", tx
, "");
728 rmorigin
= (dsl_dir_is_clone(ds
->ds_dir
) &&
729 DS_IS_DEFER_DESTROY(ds
->ds_prev
) &&
730 dsl_dataset_phys(ds
->ds_prev
)->ds_num_children
== 2 &&
731 ds
->ds_prev
->ds_userrefs
== 0);
733 /* Remove our reservation. */
734 if (ds
->ds_reserved
!= 0) {
735 dsl_dataset_set_refreservation_sync_impl(ds
,
736 (ZPROP_SRC_NONE
| ZPROP_SRC_LOCAL
| ZPROP_SRC_RECEIVED
),
738 ASSERT0(ds
->ds_reserved
);
741 dsl_scan_ds_destroyed(ds
, tx
);
745 if (dsl_dataset_phys(ds
)->ds_prev_snap_obj
!= 0) {
746 /* This is a clone */
747 ASSERT(ds
->ds_prev
!= NULL
);
748 ASSERT3U(dsl_dataset_phys(ds
->ds_prev
)->ds_next_snap_obj
, !=,
750 ASSERT0(dsl_dataset_phys(ds
)->ds_next_snap_obj
);
752 dmu_buf_will_dirty(ds
->ds_prev
->ds_dbuf
, tx
);
753 if (dsl_dataset_phys(ds
->ds_prev
)->ds_next_clones_obj
!= 0) {
754 dsl_dataset_remove_from_next_clones(ds
->ds_prev
,
758 ASSERT3U(dsl_dataset_phys(ds
->ds_prev
)->ds_num_children
, >, 1);
759 dsl_dataset_phys(ds
->ds_prev
)->ds_num_children
--;
763 * Destroy the deadlist. Unless it's a clone, the
764 * deadlist should be empty. (If it's a clone, it's
765 * safe to ignore the deadlist contents.)
767 dsl_deadlist_close(&ds
->ds_deadlist
);
768 dsl_deadlist_free(mos
, dsl_dataset_phys(ds
)->ds_deadlist_obj
, tx
);
769 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
770 dsl_dataset_phys(ds
)->ds_deadlist_obj
= 0;
772 VERIFY0(dmu_objset_from_ds(ds
, &os
));
774 if (!spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_ASYNC_DESTROY
)) {
775 old_synchronous_dataset_destroy(ds
, tx
);
778 * Move the bptree into the pool's list of trees to
779 * clean up and update space accounting information.
781 uint64_t used
, comp
, uncomp
;
783 zil_destroy_sync(dmu_objset_zil(os
), tx
);
785 if (!spa_feature_is_active(dp
->dp_spa
,
786 SPA_FEATURE_ASYNC_DESTROY
)) {
787 dsl_scan_t
*scn
= dp
->dp_scan
;
788 spa_feature_incr(dp
->dp_spa
, SPA_FEATURE_ASYNC_DESTROY
,
790 dp
->dp_bptree_obj
= bptree_alloc(mos
, tx
);
792 DMU_POOL_DIRECTORY_OBJECT
,
793 DMU_POOL_BPTREE_OBJ
, sizeof (uint64_t), 1,
794 &dp
->dp_bptree_obj
, tx
));
795 ASSERT(!scn
->scn_async_destroying
);
796 scn
->scn_async_destroying
= B_TRUE
;
799 used
= dsl_dir_phys(ds
->ds_dir
)->dd_used_bytes
;
800 comp
= dsl_dir_phys(ds
->ds_dir
)->dd_compressed_bytes
;
801 uncomp
= dsl_dir_phys(ds
->ds_dir
)->dd_uncompressed_bytes
;
803 ASSERT(!DS_UNIQUE_IS_ACCURATE(ds
) ||
804 dsl_dataset_phys(ds
)->ds_unique_bytes
== used
);
806 bptree_add(mos
, dp
->dp_bptree_obj
,
807 &dsl_dataset_phys(ds
)->ds_bp
,
808 dsl_dataset_phys(ds
)->ds_prev_snap_txg
,
809 used
, comp
, uncomp
, tx
);
810 dsl_dir_diduse_space(ds
->ds_dir
, DD_USED_HEAD
,
811 -used
, -comp
, -uncomp
, tx
);
812 dsl_dir_diduse_space(dp
->dp_free_dir
, DD_USED_HEAD
,
813 used
, comp
, uncomp
, tx
);
816 if (ds
->ds_prev
!= NULL
) {
817 if (spa_version(dp
->dp_spa
) >= SPA_VERSION_DIR_CLONES
) {
818 VERIFY0(zap_remove_int(mos
,
819 dsl_dir_phys(ds
->ds_prev
->ds_dir
)->dd_clones
,
822 prevobj
= ds
->ds_prev
->ds_object
;
823 dsl_dataset_rele(ds
->ds_prev
, ds
);
828 * This must be done after the dsl_traverse(), because it will
829 * re-open the objset.
832 dmu_objset_evict(ds
->ds_objset
);
833 ds
->ds_objset
= NULL
;
836 /* Erase the link in the dir */
837 dmu_buf_will_dirty(ds
->ds_dir
->dd_dbuf
, tx
);
838 dsl_dir_phys(ds
->ds_dir
)->dd_head_dataset_obj
= 0;
839 ddobj
= ds
->ds_dir
->dd_object
;
840 ASSERT(dsl_dataset_phys(ds
)->ds_snapnames_zapobj
!= 0);
841 VERIFY0(zap_destroy(mos
,
842 dsl_dataset_phys(ds
)->ds_snapnames_zapobj
, tx
));
844 if (ds
->ds_bookmarks
!= 0) {
845 VERIFY0(zap_destroy(mos
, ds
->ds_bookmarks
, tx
));
846 spa_feature_decr(dp
->dp_spa
, SPA_FEATURE_BOOKMARKS
, tx
);
849 spa_prop_clear_bootfs(dp
->dp_spa
, ds
->ds_object
, tx
);
851 ASSERT0(dsl_dataset_phys(ds
)->ds_next_clones_obj
);
852 ASSERT0(dsl_dataset_phys(ds
)->ds_props_obj
);
853 ASSERT0(dsl_dataset_phys(ds
)->ds_userrefs_obj
);
854 dsl_dir_rele(ds
->ds_dir
, ds
);
856 dmu_object_free_zapified(mos
, obj
, tx
);
858 dsl_dir_destroy_sync(ddobj
, tx
);
862 VERIFY0(dsl_dataset_hold_obj(dp
, prevobj
, FTAG
, &prev
));
863 dsl_destroy_snapshot_sync_impl(prev
, B_FALSE
, tx
);
864 dsl_dataset_rele(prev
, FTAG
);
869 dsl_destroy_head_sync(void *arg
, dmu_tx_t
*tx
)
871 dsl_destroy_head_arg_t
*ddha
= arg
;
872 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
875 VERIFY0(dsl_dataset_hold(dp
, ddha
->ddha_name
, FTAG
, &ds
));
876 dsl_destroy_head_sync_impl(ds
, tx
);
877 dsl_dataset_rele(ds
, FTAG
);
881 dsl_destroy_head_begin_sync(void *arg
, dmu_tx_t
*tx
)
883 dsl_destroy_head_arg_t
*ddha
= arg
;
884 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
887 VERIFY0(dsl_dataset_hold(dp
, ddha
->ddha_name
, FTAG
, &ds
));
889 /* Mark it as inconsistent on-disk, in case we crash */
890 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
891 dsl_dataset_phys(ds
)->ds_flags
|= DS_FLAG_INCONSISTENT
;
893 spa_history_log_internal_ds(ds
, "destroy begin", tx
, "");
894 dsl_dataset_rele(ds
, FTAG
);
898 dsl_destroy_head(const char *name
)
900 dsl_destroy_head_arg_t ddha
;
906 zfs_destroy_unmount_origin(name
);
909 error
= spa_open(name
, &spa
, FTAG
);
912 isenabled
= spa_feature_is_enabled(spa
, SPA_FEATURE_ASYNC_DESTROY
);
913 spa_close(spa
, FTAG
);
915 ddha
.ddha_name
= name
;
920 error
= dsl_sync_task(name
, dsl_destroy_head_check
,
921 dsl_destroy_head_begin_sync
, &ddha
, 0);
926 * Head deletion is processed in one txg on old pools;
927 * remove the objects from open context so that the txg sync
930 error
= dmu_objset_own(name
, DMU_OST_ANY
, B_FALSE
, FTAG
, &os
);
933 uint64_t prev_snap_txg
=
934 dsl_dataset_phys(dmu_objset_ds(os
))->
936 for (obj
= 0; error
== 0;
937 error
= dmu_object_next(os
, &obj
, FALSE
,
939 (void) dmu_free_long_object(os
, obj
);
940 /* sync out all frees */
941 txg_wait_synced(dmu_objset_pool(os
), 0);
942 dmu_objset_disown(os
, FTAG
);
946 return (dsl_sync_task(name
, dsl_destroy_head_check
,
947 dsl_destroy_head_sync
, &ddha
, 0));
951 * Note, this function is used as the callback for dmu_objset_find(). We
952 * always return 0 so that we will continue to find and process
953 * inconsistent datasets, even if we encounter an error trying to
954 * process one of them.
958 dsl_destroy_inconsistent(const char *dsname
, void *arg
)
962 if (dmu_objset_hold(dsname
, FTAG
, &os
) == 0) {
963 boolean_t inconsistent
= DS_IS_INCONSISTENT(dmu_objset_ds(os
));
964 dmu_objset_rele(os
, FTAG
);
966 (void) dsl_destroy_head(dsname
);
972 #if defined(_KERNEL) && defined(HAVE_SPL)
973 EXPORT_SYMBOL(dsl_destroy_head
);
974 EXPORT_SYMBOL(dsl_destroy_head_sync_impl
);
975 EXPORT_SYMBOL(dsl_dataset_user_hold_check_one
);
976 EXPORT_SYMBOL(dsl_destroy_snapshot_sync_impl
);
977 EXPORT_SYMBOL(dsl_destroy_inconsistent
);
978 EXPORT_SYMBOL(dsl_dataset_user_release_tmp
);
979 EXPORT_SYMBOL(dsl_destroy_head_check_impl
);