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) 2013 by Delphix. All rights reserved.
24 * Copyright (c) 2013 Steven Hartland. All rights reserved.
27 #include <sys/zfs_context.h>
28 #include <sys/dsl_userhold.h>
29 #include <sys/dsl_dataset.h>
30 #include <sys/dsl_synctask.h>
31 #include <sys/dmu_tx.h>
32 #include <sys/dsl_pool.h>
33 #include <sys/dsl_dir.h>
34 #include <sys/dmu_traverse.h>
35 #include <sys/dsl_scan.h>
36 #include <sys/dmu_objset.h>
38 #include <sys/zfeature.h>
39 #include <sys/zfs_ioctl.h>
40 #include <sys/dsl_deleg.h>
42 typedef struct dmu_snapshots_destroy_arg
{
44 nvlist_t
*dsda_successful_snaps
;
46 nvlist_t
*dsda_errlist
;
47 } dmu_snapshots_destroy_arg_t
;
50 dsl_destroy_snapshot_check_impl(dsl_dataset_t
*ds
, boolean_t defer
)
52 if (!dsl_dataset_is_snapshot(ds
))
53 return (SET_ERROR(EINVAL
));
55 if (dsl_dataset_long_held(ds
))
56 return (SET_ERROR(EBUSY
));
59 * Only allow deferred destroy on pools that support it.
60 * NOTE: deferred destroy is only supported on snapshots.
63 if (spa_version(ds
->ds_dir
->dd_pool
->dp_spa
) <
65 return (SET_ERROR(ENOTSUP
));
70 * If this snapshot has an elevated user reference count,
71 * we can't destroy it yet.
73 if (ds
->ds_userrefs
> 0)
74 return (SET_ERROR(EBUSY
));
77 * Can't delete a branch point.
79 if (ds
->ds_phys
->ds_num_children
> 1)
80 return (SET_ERROR(EEXIST
));
86 dsl_destroy_snapshot_check(void *arg
, dmu_tx_t
*tx
)
88 dmu_snapshots_destroy_arg_t
*dsda
= arg
;
89 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
93 if (!dmu_tx_is_syncing(tx
))
96 for (pair
= nvlist_next_nvpair(dsda
->dsda_snaps
, NULL
);
97 pair
!= NULL
; pair
= nvlist_next_nvpair(dsda
->dsda_snaps
, pair
)) {
100 error
= dsl_dataset_hold(dp
, nvpair_name(pair
),
104 * If the snapshot does not exist, silently ignore it
105 * (it's "already destroyed").
111 error
= dsl_destroy_snapshot_check_impl(ds
,
113 dsl_dataset_rele(ds
, FTAG
);
117 fnvlist_add_boolean(dsda
->dsda_successful_snaps
,
120 fnvlist_add_int32(dsda
->dsda_errlist
,
121 nvpair_name(pair
), error
);
125 pair
= nvlist_next_nvpair(dsda
->dsda_errlist
, NULL
);
127 return (fnvpair_value_int32(pair
));
132 struct process_old_arg
{
134 dsl_dataset_t
*ds_prev
;
135 boolean_t after_branch_point
;
137 uint64_t used
, comp
, uncomp
;
141 process_old_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
143 struct process_old_arg
*poa
= arg
;
144 dsl_pool_t
*dp
= poa
->ds
->ds_dir
->dd_pool
;
146 if (bp
->blk_birth
<= poa
->ds
->ds_phys
->ds_prev_snap_txg
) {
147 dsl_deadlist_insert(&poa
->ds
->ds_deadlist
, bp
, tx
);
148 if (poa
->ds_prev
&& !poa
->after_branch_point
&&
150 poa
->ds_prev
->ds_phys
->ds_prev_snap_txg
) {
151 poa
->ds_prev
->ds_phys
->ds_unique_bytes
+=
152 bp_get_dsize_sync(dp
->dp_spa
, bp
);
155 poa
->used
+= bp_get_dsize_sync(dp
->dp_spa
, bp
);
156 poa
->comp
+= BP_GET_PSIZE(bp
);
157 poa
->uncomp
+= BP_GET_UCSIZE(bp
);
158 dsl_free_sync(poa
->pio
, dp
, tx
->tx_txg
, bp
);
164 process_old_deadlist(dsl_dataset_t
*ds
, dsl_dataset_t
*ds_prev
,
165 dsl_dataset_t
*ds_next
, boolean_t after_branch_point
, dmu_tx_t
*tx
)
167 struct process_old_arg poa
= { 0 };
168 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
169 objset_t
*mos
= dp
->dp_meta_objset
;
170 uint64_t deadlist_obj
;
172 ASSERT(ds
->ds_deadlist
.dl_oldfmt
);
173 ASSERT(ds_next
->ds_deadlist
.dl_oldfmt
);
176 poa
.ds_prev
= ds_prev
;
177 poa
.after_branch_point
= after_branch_point
;
178 poa
.pio
= zio_root(dp
->dp_spa
, NULL
, NULL
, ZIO_FLAG_MUSTSUCCEED
);
179 VERIFY0(bpobj_iterate(&ds_next
->ds_deadlist
.dl_bpobj
,
180 process_old_cb
, &poa
, tx
));
181 VERIFY0(zio_wait(poa
.pio
));
182 ASSERT3U(poa
.used
, ==, ds
->ds_phys
->ds_unique_bytes
);
184 /* change snapused */
185 dsl_dir_diduse_space(ds
->ds_dir
, DD_USED_SNAP
,
186 -poa
.used
, -poa
.comp
, -poa
.uncomp
, tx
);
188 /* swap next's deadlist to our deadlist */
189 dsl_deadlist_close(&ds
->ds_deadlist
);
190 dsl_deadlist_close(&ds_next
->ds_deadlist
);
191 deadlist_obj
= ds
->ds_phys
->ds_deadlist_obj
;
192 ds
->ds_phys
->ds_deadlist_obj
= ds_next
->ds_phys
->ds_deadlist_obj
;
193 ds_next
->ds_phys
->ds_deadlist_obj
= deadlist_obj
;
194 dsl_deadlist_open(&ds
->ds_deadlist
, mos
, ds
->ds_phys
->ds_deadlist_obj
);
195 dsl_deadlist_open(&ds_next
->ds_deadlist
, mos
,
196 ds_next
->ds_phys
->ds_deadlist_obj
);
200 dsl_dataset_remove_clones_key(dsl_dataset_t
*ds
, uint64_t mintxg
, dmu_tx_t
*tx
)
202 objset_t
*mos
= ds
->ds_dir
->dd_pool
->dp_meta_objset
;
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 it
211 if (ds
->ds_dir
->dd_phys
->dd_clones
== 0)
214 zc
= kmem_alloc(sizeof (zap_cursor_t
), KM_PUSHPAGE
);
215 za
= kmem_alloc(sizeof (zap_attribute_t
), KM_PUSHPAGE
);
217 for (zap_cursor_init(zc
, mos
, ds
->ds_dir
->dd_phys
->dd_clones
);
218 zap_cursor_retrieve(zc
, za
) == 0;
219 zap_cursor_advance(zc
)) {
220 dsl_dataset_t
*clone
;
222 VERIFY0(dsl_dataset_hold_obj(ds
->ds_dir
->dd_pool
,
223 za
->za_first_integer
, FTAG
, &clone
));
224 if (clone
->ds_dir
->dd_origin_txg
> mintxg
) {
225 dsl_deadlist_remove_key(&clone
->ds_deadlist
,
227 dsl_dataset_remove_clones_key(clone
, mintxg
, tx
);
229 dsl_dataset_rele(clone
, FTAG
);
233 kmem_free(za
, sizeof (zap_attribute_t
));
234 kmem_free(zc
, sizeof (zap_cursor_t
));
238 dsl_destroy_snapshot_sync_impl(dsl_dataset_t
*ds
, boolean_t defer
, dmu_tx_t
*tx
)
243 int after_branch_point
= FALSE
;
244 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
245 objset_t
*mos
= dp
->dp_meta_objset
;
246 dsl_dataset_t
*ds_prev
= NULL
;
247 uint64_t obj
, old_unique
, used
= 0, comp
= 0, uncomp
= 0;
248 dsl_dataset_t
*ds_next
, *ds_head
, *hds
;
251 ASSERT(RRW_WRITE_HELD(&dp
->dp_config_rwlock
));
252 ASSERT3U(ds
->ds_phys
->ds_bp
.blk_birth
, <=, tx
->tx_txg
);
253 ASSERT(refcount_is_zero(&ds
->ds_longholds
));
256 (ds
->ds_userrefs
> 0 || ds
->ds_phys
->ds_num_children
> 1)) {
257 ASSERT(spa_version(dp
->dp_spa
) >= SPA_VERSION_USERREFS
);
258 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
259 ds
->ds_phys
->ds_flags
|= DS_FLAG_DEFER_DESTROY
;
260 spa_history_log_internal_ds(ds
, "defer_destroy", tx
, "");
264 ASSERT3U(ds
->ds_phys
->ds_num_children
, <=, 1);
266 /* We need to log before removing it from the namespace. */
267 spa_history_log_internal_ds(ds
, "destroy", tx
, "");
269 dsl_scan_ds_destroyed(ds
, tx
);
273 if (ds
->ds_phys
->ds_prev_snap_obj
!= 0) {
274 ASSERT3P(ds
->ds_prev
, ==, NULL
);
275 VERIFY0(dsl_dataset_hold_obj(dp
,
276 ds
->ds_phys
->ds_prev_snap_obj
, FTAG
, &ds_prev
));
278 (ds_prev
->ds_phys
->ds_next_snap_obj
!= obj
);
280 dmu_buf_will_dirty(ds_prev
->ds_dbuf
, tx
);
281 if (after_branch_point
&&
282 ds_prev
->ds_phys
->ds_next_clones_obj
!= 0) {
283 dsl_dataset_remove_from_next_clones(ds_prev
, obj
, tx
);
284 if (ds
->ds_phys
->ds_next_snap_obj
!= 0) {
285 VERIFY0(zap_add_int(mos
,
286 ds_prev
->ds_phys
->ds_next_clones_obj
,
287 ds
->ds_phys
->ds_next_snap_obj
, tx
));
290 if (!after_branch_point
) {
291 ds_prev
->ds_phys
->ds_next_snap_obj
=
292 ds
->ds_phys
->ds_next_snap_obj
;
296 VERIFY0(dsl_dataset_hold_obj(dp
,
297 ds
->ds_phys
->ds_next_snap_obj
, FTAG
, &ds_next
));
298 ASSERT3U(ds_next
->ds_phys
->ds_prev_snap_obj
, ==, obj
);
300 old_unique
= ds_next
->ds_phys
->ds_unique_bytes
;
302 dmu_buf_will_dirty(ds_next
->ds_dbuf
, tx
);
303 ds_next
->ds_phys
->ds_prev_snap_obj
=
304 ds
->ds_phys
->ds_prev_snap_obj
;
305 ds_next
->ds_phys
->ds_prev_snap_txg
=
306 ds
->ds_phys
->ds_prev_snap_txg
;
307 ASSERT3U(ds
->ds_phys
->ds_prev_snap_txg
, ==,
308 ds_prev
? ds_prev
->ds_phys
->ds_creation_txg
: 0);
310 if (ds_next
->ds_deadlist
.dl_oldfmt
) {
311 process_old_deadlist(ds
, ds_prev
, ds_next
,
312 after_branch_point
, tx
);
314 /* Adjust prev's unique space. */
315 if (ds_prev
&& !after_branch_point
) {
316 dsl_deadlist_space_range(&ds_next
->ds_deadlist
,
317 ds_prev
->ds_phys
->ds_prev_snap_txg
,
318 ds
->ds_phys
->ds_prev_snap_txg
,
319 &used
, &comp
, &uncomp
);
320 ds_prev
->ds_phys
->ds_unique_bytes
+= used
;
323 /* Adjust snapused. */
324 dsl_deadlist_space_range(&ds_next
->ds_deadlist
,
325 ds
->ds_phys
->ds_prev_snap_txg
, UINT64_MAX
,
326 &used
, &comp
, &uncomp
);
327 dsl_dir_diduse_space(ds
->ds_dir
, DD_USED_SNAP
,
328 -used
, -comp
, -uncomp
, tx
);
330 /* Move blocks to be freed to pool's free list. */
331 dsl_deadlist_move_bpobj(&ds_next
->ds_deadlist
,
332 &dp
->dp_free_bpobj
, ds
->ds_phys
->ds_prev_snap_txg
,
334 dsl_dir_diduse_space(tx
->tx_pool
->dp_free_dir
,
335 DD_USED_HEAD
, used
, comp
, uncomp
, tx
);
337 /* Merge our deadlist into next's and free it. */
338 dsl_deadlist_merge(&ds_next
->ds_deadlist
,
339 ds
->ds_phys
->ds_deadlist_obj
, tx
);
341 dsl_deadlist_close(&ds
->ds_deadlist
);
342 dsl_deadlist_free(mos
, ds
->ds_phys
->ds_deadlist_obj
, tx
);
343 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
344 ds
->ds_phys
->ds_deadlist_obj
= 0;
346 /* Collapse range in clone heads */
347 dsl_dataset_remove_clones_key(ds
,
348 ds
->ds_phys
->ds_creation_txg
, tx
);
350 if (dsl_dataset_is_snapshot(ds_next
)) {
351 dsl_dataset_t
*ds_nextnext
;
354 * Update next's unique to include blocks which
355 * were previously shared by only this snapshot
356 * and it. Those blocks will be born after the
357 * prev snap and before this snap, and will have
358 * died after the next snap and before the one
359 * after that (ie. be on the snap after next's
362 VERIFY0(dsl_dataset_hold_obj(dp
,
363 ds_next
->ds_phys
->ds_next_snap_obj
, FTAG
, &ds_nextnext
));
364 dsl_deadlist_space_range(&ds_nextnext
->ds_deadlist
,
365 ds
->ds_phys
->ds_prev_snap_txg
,
366 ds
->ds_phys
->ds_creation_txg
,
367 &used
, &comp
, &uncomp
);
368 ds_next
->ds_phys
->ds_unique_bytes
+= used
;
369 dsl_dataset_rele(ds_nextnext
, FTAG
);
370 ASSERT3P(ds_next
->ds_prev
, ==, NULL
);
372 /* Collapse range in this head. */
373 VERIFY0(dsl_dataset_hold_obj(dp
,
374 ds
->ds_dir
->dd_phys
->dd_head_dataset_obj
, FTAG
, &hds
));
375 dsl_deadlist_remove_key(&hds
->ds_deadlist
,
376 ds
->ds_phys
->ds_creation_txg
, tx
);
377 dsl_dataset_rele(hds
, FTAG
);
380 ASSERT3P(ds_next
->ds_prev
, ==, ds
);
381 dsl_dataset_rele(ds_next
->ds_prev
, ds_next
);
382 ds_next
->ds_prev
= NULL
;
384 VERIFY0(dsl_dataset_hold_obj(dp
,
385 ds
->ds_phys
->ds_prev_snap_obj
,
386 ds_next
, &ds_next
->ds_prev
));
389 dsl_dataset_recalc_head_uniq(ds_next
);
392 * Reduce the amount of our unconsumed refreservation
393 * being charged to our parent by the amount of
394 * new unique data we have gained.
396 if (old_unique
< ds_next
->ds_reserved
) {
398 uint64_t new_unique
=
399 ds_next
->ds_phys
->ds_unique_bytes
;
401 ASSERT(old_unique
<= new_unique
);
402 mrsdelta
= MIN(new_unique
- old_unique
,
403 ds_next
->ds_reserved
- old_unique
);
404 dsl_dir_diduse_space(ds
->ds_dir
,
405 DD_USED_REFRSRV
, -mrsdelta
, 0, 0, tx
);
408 dsl_dataset_rele(ds_next
, FTAG
);
411 * This must be done after the dsl_traverse(), because it will
412 * re-open the objset.
415 dmu_objset_evict(ds
->ds_objset
);
416 ds
->ds_objset
= NULL
;
419 /* remove from snapshot namespace */
420 ASSERT(ds
->ds_phys
->ds_snapnames_zapobj
== 0);
421 VERIFY0(dsl_dataset_hold_obj(dp
,
422 ds
->ds_dir
->dd_phys
->dd_head_dataset_obj
, FTAG
, &ds_head
));
423 VERIFY0(dsl_dataset_get_snapname(ds
));
428 err
= dsl_dataset_snap_lookup(ds_head
,
429 ds
->ds_snapname
, &val
);
431 ASSERT3U(val
, ==, obj
);
434 VERIFY0(dsl_dataset_snap_remove(ds_head
, ds
->ds_snapname
, tx
));
435 dsl_dataset_rele(ds_head
, FTAG
);
438 dsl_dataset_rele(ds_prev
, FTAG
);
440 spa_prop_clear_bootfs(dp
->dp_spa
, ds
->ds_object
, tx
);
442 if (ds
->ds_phys
->ds_next_clones_obj
!= 0) {
443 ASSERTV(uint64_t count
);
444 ASSERT0(zap_count(mos
,
445 ds
->ds_phys
->ds_next_clones_obj
, &count
) && count
== 0);
446 VERIFY0(dmu_object_free(mos
,
447 ds
->ds_phys
->ds_next_clones_obj
, tx
));
449 if (ds
->ds_phys
->ds_props_obj
!= 0)
450 VERIFY0(zap_destroy(mos
, ds
->ds_phys
->ds_props_obj
, tx
));
451 if (ds
->ds_phys
->ds_userrefs_obj
!= 0)
452 VERIFY0(zap_destroy(mos
, ds
->ds_phys
->ds_userrefs_obj
, tx
));
453 dsl_dir_rele(ds
->ds_dir
, ds
);
455 VERIFY0(dmu_object_free(mos
, obj
, tx
));
459 dsl_destroy_snapshot_sync(void *arg
, dmu_tx_t
*tx
)
461 dmu_snapshots_destroy_arg_t
*dsda
= arg
;
462 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
465 for (pair
= nvlist_next_nvpair(dsda
->dsda_successful_snaps
, NULL
);
467 pair
= nvlist_next_nvpair(dsda
->dsda_successful_snaps
, pair
)) {
470 VERIFY0(dsl_dataset_hold(dp
, nvpair_name(pair
), FTAG
, &ds
));
472 dsl_destroy_snapshot_sync_impl(ds
, dsda
->dsda_defer
, tx
);
473 dsl_dataset_rele(ds
, FTAG
);
478 * The semantics of this function are described in the comment above
479 * lzc_destroy_snaps(). To summarize:
481 * The snapshots must all be in the same pool.
483 * Snapshots that don't exist will be silently ignored (considered to be
484 * "already deleted").
486 * On success, all snaps will be destroyed and this will return 0.
487 * On failure, no snaps will be destroyed, the errlist will be filled in,
488 * and this will return an errno.
491 dsl_destroy_snapshots_nvl(nvlist_t
*snaps
, boolean_t defer
,
494 dmu_snapshots_destroy_arg_t dsda
;
498 pair
= nvlist_next_nvpair(snaps
, NULL
);
502 dsda
.dsda_snaps
= snaps
;
503 VERIFY0(nvlist_alloc(&dsda
.dsda_successful_snaps
,
504 NV_UNIQUE_NAME
, KM_PUSHPAGE
));
505 dsda
.dsda_defer
= defer
;
506 dsda
.dsda_errlist
= errlist
;
508 error
= dsl_sync_task(nvpair_name(pair
),
509 dsl_destroy_snapshot_check
, dsl_destroy_snapshot_sync
,
511 fnvlist_free(dsda
.dsda_successful_snaps
);
517 dsl_destroy_snapshot(const char *name
, boolean_t defer
)
523 VERIFY0(nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, KM_PUSHPAGE
));
524 VERIFY0(nvlist_alloc(&errlist
, NV_UNIQUE_NAME
, KM_PUSHPAGE
));
526 fnvlist_add_boolean(nvl
, name
);
527 error
= dsl_destroy_snapshots_nvl(nvl
, defer
, errlist
);
528 fnvlist_free(errlist
);
540 kill_blkptr(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
541 const zbookmark_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
543 struct killarg
*ka
= arg
;
544 dmu_tx_t
*tx
= ka
->tx
;
549 if (zb
->zb_level
== ZB_ZIL_LEVEL
) {
550 ASSERT(zilog
!= NULL
);
552 * It's a block in the intent log. It has no
553 * accounting, so just free it.
555 dsl_free(ka
->tx
->tx_pool
, ka
->tx
->tx_txg
, bp
);
557 ASSERT(zilog
== NULL
);
558 ASSERT3U(bp
->blk_birth
, >, ka
->ds
->ds_phys
->ds_prev_snap_txg
);
559 (void) dsl_dataset_block_kill(ka
->ds
, bp
, tx
, B_FALSE
);
566 old_synchronous_dataset_destroy(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
571 * Free everything that we point to (that's born after
572 * the previous snapshot, if we are a clone)
574 * NB: this should be very quick, because we already
575 * freed all the objects in open context.
579 VERIFY0(traverse_dataset(ds
,
580 ds
->ds_phys
->ds_prev_snap_txg
, TRAVERSE_POST
,
582 ASSERT(!DS_UNIQUE_IS_ACCURATE(ds
) || ds
->ds_phys
->ds_unique_bytes
== 0);
585 typedef struct dsl_destroy_head_arg
{
586 const char *ddha_name
;
587 } dsl_destroy_head_arg_t
;
590 dsl_destroy_head_check_impl(dsl_dataset_t
*ds
, int expected_holds
)
596 if (dsl_dataset_is_snapshot(ds
))
597 return (SET_ERROR(EINVAL
));
599 if (refcount_count(&ds
->ds_longholds
) != expected_holds
)
600 return (SET_ERROR(EBUSY
));
602 mos
= ds
->ds_dir
->dd_pool
->dp_meta_objset
;
605 * Can't delete a head dataset if there are snapshots of it.
606 * (Except if the only snapshots are from the branch we cloned
609 if (ds
->ds_prev
!= NULL
&&
610 ds
->ds_prev
->ds_phys
->ds_next_snap_obj
== ds
->ds_object
)
611 return (SET_ERROR(EBUSY
));
614 * Can't delete if there are children of this fs.
616 error
= zap_count(mos
,
617 ds
->ds_dir
->dd_phys
->dd_child_dir_zapobj
, &count
);
621 return (SET_ERROR(EEXIST
));
623 if (dsl_dir_is_clone(ds
->ds_dir
) && DS_IS_DEFER_DESTROY(ds
->ds_prev
) &&
624 ds
->ds_prev
->ds_phys
->ds_num_children
== 2 &&
625 ds
->ds_prev
->ds_userrefs
== 0) {
626 /* We need to remove the origin snapshot as well. */
627 if (!refcount_is_zero(&ds
->ds_prev
->ds_longholds
))
628 return (SET_ERROR(EBUSY
));
634 dsl_destroy_head_check(void *arg
, dmu_tx_t
*tx
)
636 dsl_destroy_head_arg_t
*ddha
= arg
;
637 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
641 error
= dsl_dataset_hold(dp
, ddha
->ddha_name
, FTAG
, &ds
);
645 error
= dsl_destroy_head_check_impl(ds
, 0);
646 dsl_dataset_rele(ds
, FTAG
);
651 dsl_dir_destroy_sync(uint64_t ddobj
, dmu_tx_t
*tx
)
654 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
655 objset_t
*mos
= dp
->dp_meta_objset
;
658 ASSERT(RRW_WRITE_HELD(&dmu_tx_pool(tx
)->dp_config_rwlock
));
660 VERIFY0(dsl_dir_hold_obj(dp
, ddobj
, NULL
, FTAG
, &dd
));
662 ASSERT0(dd
->dd_phys
->dd_head_dataset_obj
);
665 * Remove our reservation. The impl() routine avoids setting the
666 * actual property, which would require the (already destroyed) ds.
668 dsl_dir_set_reservation_sync_impl(dd
, 0, tx
);
670 ASSERT0(dd
->dd_phys
->dd_used_bytes
);
671 ASSERT0(dd
->dd_phys
->dd_reserved
);
672 for (t
= 0; t
< DD_USED_NUM
; t
++)
673 ASSERT0(dd
->dd_phys
->dd_used_breakdown
[t
]);
675 VERIFY0(zap_destroy(mos
, dd
->dd_phys
->dd_child_dir_zapobj
, tx
));
676 VERIFY0(zap_destroy(mos
, dd
->dd_phys
->dd_props_zapobj
, tx
));
677 VERIFY0(dsl_deleg_destroy(mos
, dd
->dd_phys
->dd_deleg_zapobj
, tx
));
678 VERIFY0(zap_remove(mos
,
679 dd
->dd_parent
->dd_phys
->dd_child_dir_zapobj
, dd
->dd_myname
, tx
));
681 dsl_dir_rele(dd
, FTAG
);
682 VERIFY0(dmu_object_free(mos
, ddobj
, tx
));
686 dsl_destroy_head_sync_impl(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
688 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
689 objset_t
*mos
= dp
->dp_meta_objset
;
690 uint64_t obj
, ddobj
, prevobj
= 0;
692 zfeature_info_t
*async_destroy
;
695 ASSERT3U(ds
->ds_phys
->ds_num_children
, <=, 1);
696 ASSERT(ds
->ds_prev
== NULL
||
697 ds
->ds_prev
->ds_phys
->ds_next_snap_obj
!= ds
->ds_object
);
698 ASSERT3U(ds
->ds_phys
->ds_bp
.blk_birth
, <=, tx
->tx_txg
);
699 ASSERT(RRW_WRITE_HELD(&dp
->dp_config_rwlock
));
701 /* We need to log before removing it from the namespace. */
702 spa_history_log_internal_ds(ds
, "destroy", tx
, "");
704 rmorigin
= (dsl_dir_is_clone(ds
->ds_dir
) &&
705 DS_IS_DEFER_DESTROY(ds
->ds_prev
) &&
706 ds
->ds_prev
->ds_phys
->ds_num_children
== 2 &&
707 ds
->ds_prev
->ds_userrefs
== 0);
709 /* Remove our reservation */
710 if (ds
->ds_reserved
!= 0) {
711 dsl_dataset_set_refreservation_sync_impl(ds
,
712 (ZPROP_SRC_NONE
| ZPROP_SRC_LOCAL
| ZPROP_SRC_RECEIVED
),
714 ASSERT0(ds
->ds_reserved
);
717 dsl_scan_ds_destroyed(ds
, tx
);
721 if (ds
->ds_phys
->ds_prev_snap_obj
!= 0) {
722 /* This is a clone */
723 ASSERT(ds
->ds_prev
!= NULL
);
724 ASSERT3U(ds
->ds_prev
->ds_phys
->ds_next_snap_obj
, !=, obj
);
725 ASSERT0(ds
->ds_phys
->ds_next_snap_obj
);
727 dmu_buf_will_dirty(ds
->ds_prev
->ds_dbuf
, tx
);
728 if (ds
->ds_prev
->ds_phys
->ds_next_clones_obj
!= 0) {
729 dsl_dataset_remove_from_next_clones(ds
->ds_prev
,
733 ASSERT3U(ds
->ds_prev
->ds_phys
->ds_num_children
, >, 1);
734 ds
->ds_prev
->ds_phys
->ds_num_children
--;
738 &spa_feature_table
[SPA_FEATURE_ASYNC_DESTROY
];
741 * Destroy the deadlist. Unless it's a clone, the
742 * deadlist should be empty. (If it's a clone, it's
743 * safe to ignore the deadlist contents.)
745 dsl_deadlist_close(&ds
->ds_deadlist
);
746 dsl_deadlist_free(mos
, ds
->ds_phys
->ds_deadlist_obj
, tx
);
747 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
748 ds
->ds_phys
->ds_deadlist_obj
= 0;
750 VERIFY0(dmu_objset_from_ds(ds
, &os
));
752 if (!spa_feature_is_enabled(dp
->dp_spa
, async_destroy
)) {
753 old_synchronous_dataset_destroy(ds
, tx
);
756 * Move the bptree into the pool's list of trees to
757 * clean up and update space accounting information.
759 uint64_t used
, comp
, uncomp
;
761 zil_destroy_sync(dmu_objset_zil(os
), tx
);
763 if (!spa_feature_is_active(dp
->dp_spa
, async_destroy
)) {
764 dsl_scan_t
*scn
= dp
->dp_scan
;
766 spa_feature_incr(dp
->dp_spa
, async_destroy
, tx
);
767 dp
->dp_bptree_obj
= bptree_alloc(mos
, tx
);
769 DMU_POOL_DIRECTORY_OBJECT
,
770 DMU_POOL_BPTREE_OBJ
, sizeof (uint64_t), 1,
771 &dp
->dp_bptree_obj
, tx
));
772 ASSERT(!scn
->scn_async_destroying
);
773 scn
->scn_async_destroying
= B_TRUE
;
776 used
= ds
->ds_dir
->dd_phys
->dd_used_bytes
;
777 comp
= ds
->ds_dir
->dd_phys
->dd_compressed_bytes
;
778 uncomp
= ds
->ds_dir
->dd_phys
->dd_uncompressed_bytes
;
780 ASSERT(!DS_UNIQUE_IS_ACCURATE(ds
) ||
781 ds
->ds_phys
->ds_unique_bytes
== used
);
783 bptree_add(mos
, dp
->dp_bptree_obj
,
784 &ds
->ds_phys
->ds_bp
, ds
->ds_phys
->ds_prev_snap_txg
,
785 used
, comp
, uncomp
, tx
);
786 dsl_dir_diduse_space(ds
->ds_dir
, DD_USED_HEAD
,
787 -used
, -comp
, -uncomp
, tx
);
788 dsl_dir_diduse_space(dp
->dp_free_dir
, DD_USED_HEAD
,
789 used
, comp
, uncomp
, tx
);
792 if (ds
->ds_prev
!= NULL
) {
793 if (spa_version(dp
->dp_spa
) >= SPA_VERSION_DIR_CLONES
) {
794 VERIFY0(zap_remove_int(mos
,
795 ds
->ds_prev
->ds_dir
->dd_phys
->dd_clones
,
798 prevobj
= ds
->ds_prev
->ds_object
;
799 dsl_dataset_rele(ds
->ds_prev
, ds
);
804 * This must be done after the dsl_traverse(), because it will
805 * re-open the objset.
808 dmu_objset_evict(ds
->ds_objset
);
809 ds
->ds_objset
= NULL
;
812 /* Erase the link in the dir */
813 dmu_buf_will_dirty(ds
->ds_dir
->dd_dbuf
, tx
);
814 ds
->ds_dir
->dd_phys
->dd_head_dataset_obj
= 0;
815 ddobj
= ds
->ds_dir
->dd_object
;
816 ASSERT(ds
->ds_phys
->ds_snapnames_zapobj
!= 0);
817 VERIFY0(zap_destroy(mos
, ds
->ds_phys
->ds_snapnames_zapobj
, tx
));
819 spa_prop_clear_bootfs(dp
->dp_spa
, ds
->ds_object
, tx
);
821 ASSERT0(ds
->ds_phys
->ds_next_clones_obj
);
822 ASSERT0(ds
->ds_phys
->ds_props_obj
);
823 ASSERT0(ds
->ds_phys
->ds_userrefs_obj
);
824 dsl_dir_rele(ds
->ds_dir
, ds
);
826 VERIFY0(dmu_object_free(mos
, obj
, tx
));
828 dsl_dir_destroy_sync(ddobj
, tx
);
832 VERIFY0(dsl_dataset_hold_obj(dp
, prevobj
, FTAG
, &prev
));
833 dsl_destroy_snapshot_sync_impl(prev
, B_FALSE
, tx
);
834 dsl_dataset_rele(prev
, FTAG
);
839 dsl_destroy_head_sync(void *arg
, dmu_tx_t
*tx
)
841 dsl_destroy_head_arg_t
*ddha
= arg
;
842 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
845 VERIFY0(dsl_dataset_hold(dp
, ddha
->ddha_name
, FTAG
, &ds
));
846 dsl_destroy_head_sync_impl(ds
, tx
);
847 dsl_dataset_rele(ds
, FTAG
);
851 dsl_destroy_head_begin_sync(void *arg
, dmu_tx_t
*tx
)
853 dsl_destroy_head_arg_t
*ddha
= arg
;
854 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
857 VERIFY0(dsl_dataset_hold(dp
, ddha
->ddha_name
, FTAG
, &ds
));
859 /* Mark it as inconsistent on-disk, in case we crash */
860 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
861 ds
->ds_phys
->ds_flags
|= DS_FLAG_INCONSISTENT
;
863 spa_history_log_internal_ds(ds
, "destroy begin", tx
, "");
864 dsl_dataset_rele(ds
, FTAG
);
868 dsl_destroy_head(const char *name
)
870 dsl_destroy_head_arg_t ddha
;
876 zfs_destroy_unmount_origin(name
);
879 error
= spa_open(name
, &spa
, FTAG
);
882 isenabled
= spa_feature_is_enabled(spa
,
883 &spa_feature_table
[SPA_FEATURE_ASYNC_DESTROY
]);
884 spa_close(spa
, FTAG
);
886 ddha
.ddha_name
= name
;
891 error
= dsl_sync_task(name
, dsl_destroy_head_check
,
892 dsl_destroy_head_begin_sync
, &ddha
, 0);
897 * Head deletion is processed in one txg on old pools;
898 * remove the objects from open context so that the txg sync
901 error
= dmu_objset_own(name
, DMU_OST_ANY
, B_FALSE
, FTAG
, &os
);
904 uint64_t prev_snap_txg
=
905 dmu_objset_ds(os
)->ds_phys
->ds_prev_snap_txg
;
906 for (obj
= 0; error
== 0;
907 error
= dmu_object_next(os
, &obj
, FALSE
,
909 (void) dmu_free_long_object(os
, obj
);
910 /* sync out all frees */
911 txg_wait_synced(dmu_objset_pool(os
), 0);
912 dmu_objset_disown(os
, FTAG
);
916 return (dsl_sync_task(name
, dsl_destroy_head_check
,
917 dsl_destroy_head_sync
, &ddha
, 0));
921 * Note, this function is used as the callback for dmu_objset_find(). We
922 * always return 0 so that we will continue to find and process
923 * inconsistent datasets, even if we encounter an error trying to
924 * process one of them.
928 dsl_destroy_inconsistent(const char *dsname
, void *arg
)
932 if (dmu_objset_hold(dsname
, FTAG
, &os
) == 0) {
933 boolean_t inconsistent
= DS_IS_INCONSISTENT(dmu_objset_ds(os
));
934 dmu_objset_rele(os
, FTAG
);
936 (void) dsl_destroy_head(dsname
);
942 #if defined(_KERNEL) && defined(HAVE_SPL)
943 EXPORT_SYMBOL(dsl_destroy_head
);
944 EXPORT_SYMBOL(dsl_destroy_head_sync_impl
);
945 EXPORT_SYMBOL(dsl_dataset_user_hold_check_one
);
946 EXPORT_SYMBOL(dsl_destroy_snapshot_sync_impl
);
947 EXPORT_SYMBOL(dsl_destroy_inconsistent
);
948 EXPORT_SYMBOL(dsl_dataset_user_release_tmp
);
949 EXPORT_SYMBOL(dsl_destroy_head_check_impl
);