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, 2015 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
)
250 int after_branch_point
= FALSE
;
251 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
252 objset_t
*mos
= dp
->dp_meta_objset
;
253 dsl_dataset_t
*ds_prev
= NULL
;
254 uint64_t obj
, old_unique
, used
= 0, comp
= 0, uncomp
= 0;
255 dsl_dataset_t
*ds_next
, *ds_head
, *hds
;
258 ASSERT(RRW_WRITE_HELD(&dp
->dp_config_rwlock
));
259 ASSERT3U(dsl_dataset_phys(ds
)->ds_bp
.blk_birth
, <=, tx
->tx_txg
);
260 ASSERT(refcount_is_zero(&ds
->ds_longholds
));
263 (ds
->ds_userrefs
> 0 ||
264 dsl_dataset_phys(ds
)->ds_num_children
> 1)) {
265 ASSERT(spa_version(dp
->dp_spa
) >= SPA_VERSION_USERREFS
);
266 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
267 dsl_dataset_phys(ds
)->ds_flags
|= DS_FLAG_DEFER_DESTROY
;
268 spa_history_log_internal_ds(ds
, "defer_destroy", tx
, "");
272 ASSERT3U(dsl_dataset_phys(ds
)->ds_num_children
, <=, 1);
274 /* We need to log before removing it from the namespace. */
275 spa_history_log_internal_ds(ds
, "destroy", tx
, "");
277 dsl_scan_ds_destroyed(ds
, tx
);
281 for (f
= 0; f
< SPA_FEATURES
; f
++) {
282 if (ds
->ds_feature_inuse
[f
]) {
283 dsl_dataset_deactivate_feature(obj
, f
, tx
);
284 ds
->ds_feature_inuse
[f
] = B_FALSE
;
287 if (dsl_dataset_phys(ds
)->ds_prev_snap_obj
!= 0) {
288 ASSERT3P(ds
->ds_prev
, ==, NULL
);
289 VERIFY0(dsl_dataset_hold_obj(dp
,
290 dsl_dataset_phys(ds
)->ds_prev_snap_obj
, FTAG
, &ds_prev
));
292 (dsl_dataset_phys(ds_prev
)->ds_next_snap_obj
!= obj
);
294 dmu_buf_will_dirty(ds_prev
->ds_dbuf
, tx
);
295 if (after_branch_point
&&
296 dsl_dataset_phys(ds_prev
)->ds_next_clones_obj
!= 0) {
297 dsl_dataset_remove_from_next_clones(ds_prev
, obj
, tx
);
298 if (dsl_dataset_phys(ds
)->ds_next_snap_obj
!= 0) {
299 VERIFY0(zap_add_int(mos
,
300 dsl_dataset_phys(ds_prev
)->
302 dsl_dataset_phys(ds
)->ds_next_snap_obj
,
306 if (!after_branch_point
) {
307 dsl_dataset_phys(ds_prev
)->ds_next_snap_obj
=
308 dsl_dataset_phys(ds
)->ds_next_snap_obj
;
312 VERIFY0(dsl_dataset_hold_obj(dp
,
313 dsl_dataset_phys(ds
)->ds_next_snap_obj
, FTAG
, &ds_next
));
314 ASSERT3U(dsl_dataset_phys(ds_next
)->ds_prev_snap_obj
, ==, obj
);
316 old_unique
= dsl_dataset_phys(ds_next
)->ds_unique_bytes
;
318 dmu_buf_will_dirty(ds_next
->ds_dbuf
, tx
);
319 dsl_dataset_phys(ds_next
)->ds_prev_snap_obj
=
320 dsl_dataset_phys(ds
)->ds_prev_snap_obj
;
321 dsl_dataset_phys(ds_next
)->ds_prev_snap_txg
=
322 dsl_dataset_phys(ds
)->ds_prev_snap_txg
;
323 ASSERT3U(dsl_dataset_phys(ds
)->ds_prev_snap_txg
, ==,
324 ds_prev
? dsl_dataset_phys(ds_prev
)->ds_creation_txg
: 0);
326 if (ds_next
->ds_deadlist
.dl_oldfmt
) {
327 process_old_deadlist(ds
, ds_prev
, ds_next
,
328 after_branch_point
, tx
);
330 /* Adjust prev's unique space. */
331 if (ds_prev
&& !after_branch_point
) {
332 dsl_deadlist_space_range(&ds_next
->ds_deadlist
,
333 dsl_dataset_phys(ds_prev
)->ds_prev_snap_txg
,
334 dsl_dataset_phys(ds
)->ds_prev_snap_txg
,
335 &used
, &comp
, &uncomp
);
336 dsl_dataset_phys(ds_prev
)->ds_unique_bytes
+= used
;
339 /* Adjust snapused. */
340 dsl_deadlist_space_range(&ds_next
->ds_deadlist
,
341 dsl_dataset_phys(ds
)->ds_prev_snap_txg
, UINT64_MAX
,
342 &used
, &comp
, &uncomp
);
343 dsl_dir_diduse_space(ds
->ds_dir
, DD_USED_SNAP
,
344 -used
, -comp
, -uncomp
, tx
);
346 /* Move blocks to be freed to pool's free list. */
347 dsl_deadlist_move_bpobj(&ds_next
->ds_deadlist
,
348 &dp
->dp_free_bpobj
, dsl_dataset_phys(ds
)->ds_prev_snap_txg
,
350 dsl_dir_diduse_space(tx
->tx_pool
->dp_free_dir
,
351 DD_USED_HEAD
, used
, comp
, uncomp
, tx
);
353 /* Merge our deadlist into next's and free it. */
354 dsl_deadlist_merge(&ds_next
->ds_deadlist
,
355 dsl_dataset_phys(ds
)->ds_deadlist_obj
, tx
);
357 dsl_deadlist_close(&ds
->ds_deadlist
);
358 dsl_deadlist_free(mos
, dsl_dataset_phys(ds
)->ds_deadlist_obj
, tx
);
359 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
360 dsl_dataset_phys(ds
)->ds_deadlist_obj
= 0;
362 /* Collapse range in clone heads */
363 dsl_dataset_remove_clones_key(ds
,
364 dsl_dataset_phys(ds
)->ds_creation_txg
, tx
);
366 if (ds_next
->ds_is_snapshot
) {
367 dsl_dataset_t
*ds_nextnext
;
370 * Update next's unique to include blocks which
371 * were previously shared by only this snapshot
372 * and it. Those blocks will be born after the
373 * prev snap and before this snap, and will have
374 * died after the next snap and before the one
375 * after that (ie. be on the snap after next's
378 VERIFY0(dsl_dataset_hold_obj(dp
,
379 dsl_dataset_phys(ds_next
)->ds_next_snap_obj
,
380 FTAG
, &ds_nextnext
));
381 dsl_deadlist_space_range(&ds_nextnext
->ds_deadlist
,
382 dsl_dataset_phys(ds
)->ds_prev_snap_txg
,
383 dsl_dataset_phys(ds
)->ds_creation_txg
,
384 &used
, &comp
, &uncomp
);
385 dsl_dataset_phys(ds_next
)->ds_unique_bytes
+= used
;
386 dsl_dataset_rele(ds_nextnext
, FTAG
);
387 ASSERT3P(ds_next
->ds_prev
, ==, NULL
);
389 /* Collapse range in this head. */
390 VERIFY0(dsl_dataset_hold_obj(dp
,
391 dsl_dir_phys(ds
->ds_dir
)->dd_head_dataset_obj
, FTAG
, &hds
));
392 dsl_deadlist_remove_key(&hds
->ds_deadlist
,
393 dsl_dataset_phys(ds
)->ds_creation_txg
, tx
);
394 dsl_dataset_rele(hds
, FTAG
);
397 ASSERT3P(ds_next
->ds_prev
, ==, ds
);
398 dsl_dataset_rele(ds_next
->ds_prev
, ds_next
);
399 ds_next
->ds_prev
= NULL
;
401 VERIFY0(dsl_dataset_hold_obj(dp
,
402 dsl_dataset_phys(ds
)->ds_prev_snap_obj
,
403 ds_next
, &ds_next
->ds_prev
));
406 dsl_dataset_recalc_head_uniq(ds_next
);
409 * Reduce the amount of our unconsumed refreservation
410 * being charged to our parent by the amount of
411 * new unique data we have gained.
413 if (old_unique
< ds_next
->ds_reserved
) {
415 uint64_t new_unique
=
416 dsl_dataset_phys(ds_next
)->ds_unique_bytes
;
418 ASSERT(old_unique
<= new_unique
);
419 mrsdelta
= MIN(new_unique
- old_unique
,
420 ds_next
->ds_reserved
- old_unique
);
421 dsl_dir_diduse_space(ds
->ds_dir
,
422 DD_USED_REFRSRV
, -mrsdelta
, 0, 0, tx
);
425 dsl_dataset_rele(ds_next
, FTAG
);
428 * This must be done after the dsl_traverse(), because it will
429 * re-open the objset.
432 dmu_objset_evict(ds
->ds_objset
);
433 ds
->ds_objset
= NULL
;
436 /* remove from snapshot namespace */
437 ASSERT(dsl_dataset_phys(ds
)->ds_snapnames_zapobj
== 0);
438 VERIFY0(dsl_dataset_hold_obj(dp
,
439 dsl_dir_phys(ds
->ds_dir
)->dd_head_dataset_obj
, FTAG
, &ds_head
));
440 VERIFY0(dsl_dataset_get_snapname(ds
));
445 err
= dsl_dataset_snap_lookup(ds_head
,
446 ds
->ds_snapname
, &val
);
448 ASSERT3U(val
, ==, obj
);
451 VERIFY0(dsl_dataset_snap_remove(ds_head
, ds
->ds_snapname
, tx
, B_TRUE
));
452 dsl_dataset_rele(ds_head
, FTAG
);
455 dsl_dataset_rele(ds_prev
, FTAG
);
457 spa_prop_clear_bootfs(dp
->dp_spa
, ds
->ds_object
, tx
);
459 if (dsl_dataset_phys(ds
)->ds_next_clones_obj
!= 0) {
460 ASSERTV(uint64_t count
);
461 ASSERT0(zap_count(mos
,
462 dsl_dataset_phys(ds
)->ds_next_clones_obj
, &count
) &&
464 VERIFY0(dmu_object_free(mos
,
465 dsl_dataset_phys(ds
)->ds_next_clones_obj
, tx
));
467 if (dsl_dataset_phys(ds
)->ds_props_obj
!= 0)
468 VERIFY0(zap_destroy(mos
, dsl_dataset_phys(ds
)->ds_props_obj
,
470 if (dsl_dataset_phys(ds
)->ds_userrefs_obj
!= 0)
471 VERIFY0(zap_destroy(mos
, dsl_dataset_phys(ds
)->ds_userrefs_obj
,
473 dsl_dir_rele(ds
->ds_dir
, ds
);
475 dmu_object_free_zapified(mos
, obj
, tx
);
479 dsl_destroy_snapshot_sync(void *arg
, dmu_tx_t
*tx
)
481 dmu_snapshots_destroy_arg_t
*dsda
= arg
;
482 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
485 for (pair
= nvlist_next_nvpair(dsda
->dsda_successful_snaps
, NULL
);
487 pair
= nvlist_next_nvpair(dsda
->dsda_successful_snaps
, pair
)) {
490 VERIFY0(dsl_dataset_hold(dp
, nvpair_name(pair
), FTAG
, &ds
));
492 dsl_destroy_snapshot_sync_impl(ds
, dsda
->dsda_defer
, tx
);
493 dsl_dataset_rele(ds
, FTAG
);
498 * The semantics of this function are described in the comment above
499 * lzc_destroy_snaps(). To summarize:
501 * The snapshots must all be in the same pool.
503 * Snapshots that don't exist will be silently ignored (considered to be
504 * "already deleted").
506 * On success, all snaps will be destroyed and this will return 0.
507 * On failure, no snaps will be destroyed, the errlist will be filled in,
508 * and this will return an errno.
511 dsl_destroy_snapshots_nvl(nvlist_t
*snaps
, boolean_t defer
,
514 dmu_snapshots_destroy_arg_t dsda
;
518 pair
= nvlist_next_nvpair(snaps
, NULL
);
522 dsda
.dsda_snaps
= snaps
;
523 VERIFY0(nvlist_alloc(&dsda
.dsda_successful_snaps
,
524 NV_UNIQUE_NAME
, KM_SLEEP
));
525 dsda
.dsda_defer
= defer
;
526 dsda
.dsda_errlist
= errlist
;
528 error
= dsl_sync_task(nvpair_name(pair
),
529 dsl_destroy_snapshot_check
, dsl_destroy_snapshot_sync
,
530 &dsda
, 0, ZFS_SPACE_CHECK_NONE
);
531 fnvlist_free(dsda
.dsda_successful_snaps
);
537 dsl_destroy_snapshot(const char *name
, boolean_t defer
)
540 nvlist_t
*nvl
= fnvlist_alloc();
541 nvlist_t
*errlist
= fnvlist_alloc();
543 fnvlist_add_boolean(nvl
, name
);
544 error
= dsl_destroy_snapshots_nvl(nvl
, defer
, errlist
);
545 fnvlist_free(errlist
);
557 kill_blkptr(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
558 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
560 struct killarg
*ka
= arg
;
561 dmu_tx_t
*tx
= ka
->tx
;
563 if (bp
== NULL
|| BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
))
566 if (zb
->zb_level
== ZB_ZIL_LEVEL
) {
567 ASSERT(zilog
!= NULL
);
569 * It's a block in the intent log. It has no
570 * accounting, so just free it.
572 dsl_free(ka
->tx
->tx_pool
, ka
->tx
->tx_txg
, bp
);
574 ASSERT(zilog
== NULL
);
575 ASSERT3U(bp
->blk_birth
, >,
576 dsl_dataset_phys(ka
->ds
)->ds_prev_snap_txg
);
577 (void) dsl_dataset_block_kill(ka
->ds
, bp
, tx
, B_FALSE
);
584 old_synchronous_dataset_destroy(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
589 * Free everything that we point to (that's born after
590 * the previous snapshot, if we are a clone)
592 * NB: this should be very quick, because we already
593 * freed all the objects in open context.
597 VERIFY0(traverse_dataset(ds
,
598 dsl_dataset_phys(ds
)->ds_prev_snap_txg
, TRAVERSE_POST
,
600 ASSERT(!DS_UNIQUE_IS_ACCURATE(ds
) ||
601 dsl_dataset_phys(ds
)->ds_unique_bytes
== 0);
604 typedef struct dsl_destroy_head_arg
{
605 const char *ddha_name
;
606 } dsl_destroy_head_arg_t
;
609 dsl_destroy_head_check_impl(dsl_dataset_t
*ds
, int expected_holds
)
615 ASSERT(!ds
->ds_is_snapshot
);
616 if (ds
->ds_is_snapshot
)
617 return (SET_ERROR(EINVAL
));
619 if (refcount_count(&ds
->ds_longholds
) != expected_holds
)
620 return (SET_ERROR(EBUSY
));
622 mos
= ds
->ds_dir
->dd_pool
->dp_meta_objset
;
625 * Can't delete a head dataset if there are snapshots of it.
626 * (Except if the only snapshots are from the branch we cloned
629 if (ds
->ds_prev
!= NULL
&&
630 dsl_dataset_phys(ds
->ds_prev
)->ds_next_snap_obj
== ds
->ds_object
)
631 return (SET_ERROR(EBUSY
));
634 * Can't delete if there are children of this fs.
636 error
= zap_count(mos
,
637 dsl_dir_phys(ds
->ds_dir
)->dd_child_dir_zapobj
, &count
);
641 return (SET_ERROR(EEXIST
));
643 if (dsl_dir_is_clone(ds
->ds_dir
) && DS_IS_DEFER_DESTROY(ds
->ds_prev
) &&
644 dsl_dataset_phys(ds
->ds_prev
)->ds_num_children
== 2 &&
645 ds
->ds_prev
->ds_userrefs
== 0) {
646 /* We need to remove the origin snapshot as well. */
647 if (!refcount_is_zero(&ds
->ds_prev
->ds_longholds
))
648 return (SET_ERROR(EBUSY
));
654 dsl_destroy_head_check(void *arg
, dmu_tx_t
*tx
)
656 dsl_destroy_head_arg_t
*ddha
= arg
;
657 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
661 error
= dsl_dataset_hold(dp
, ddha
->ddha_name
, FTAG
, &ds
);
665 error
= dsl_destroy_head_check_impl(ds
, 0);
666 dsl_dataset_rele(ds
, FTAG
);
671 dsl_dir_destroy_sync(uint64_t ddobj
, dmu_tx_t
*tx
)
674 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
675 objset_t
*mos
= dp
->dp_meta_objset
;
678 ASSERT(RRW_WRITE_HELD(&dmu_tx_pool(tx
)->dp_config_rwlock
));
680 VERIFY0(dsl_dir_hold_obj(dp
, ddobj
, NULL
, FTAG
, &dd
));
682 ASSERT0(dsl_dir_phys(dd
)->dd_head_dataset_obj
);
685 * Decrement the filesystem count for all parent filesystems.
687 * When we receive an incremental stream into a filesystem that already
688 * exists, a temporary clone is created. We never count this temporary
689 * clone, whose name begins with a '%'.
691 if (dd
->dd_myname
[0] != '%' && dd
->dd_parent
!= NULL
)
692 dsl_fs_ss_count_adjust(dd
->dd_parent
, -1,
693 DD_FIELD_FILESYSTEM_COUNT
, tx
);
696 * Remove our reservation. The impl() routine avoids setting the
697 * actual property, which would require the (already destroyed) ds.
699 dsl_dir_set_reservation_sync_impl(dd
, 0, tx
);
701 ASSERT0(dsl_dir_phys(dd
)->dd_used_bytes
);
702 ASSERT0(dsl_dir_phys(dd
)->dd_reserved
);
703 for (t
= 0; t
< DD_USED_NUM
; t
++)
704 ASSERT0(dsl_dir_phys(dd
)->dd_used_breakdown
[t
]);
706 VERIFY0(zap_destroy(mos
, dsl_dir_phys(dd
)->dd_child_dir_zapobj
, tx
));
707 VERIFY0(zap_destroy(mos
, dsl_dir_phys(dd
)->dd_props_zapobj
, tx
));
708 VERIFY0(dsl_deleg_destroy(mos
, dsl_dir_phys(dd
)->dd_deleg_zapobj
, tx
));
709 VERIFY0(zap_remove(mos
,
710 dsl_dir_phys(dd
->dd_parent
)->dd_child_dir_zapobj
,
713 dsl_dir_rele(dd
, FTAG
);
714 dmu_object_free_zapified(mos
, ddobj
, tx
);
718 dsl_destroy_head_sync_impl(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
720 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
722 objset_t
*mos
= dp
->dp_meta_objset
;
723 uint64_t obj
, ddobj
, prevobj
= 0;
727 ASSERT3U(dsl_dataset_phys(ds
)->ds_num_children
, <=, 1);
728 ASSERT(ds
->ds_prev
== NULL
||
729 dsl_dataset_phys(ds
->ds_prev
)->ds_next_snap_obj
!= ds
->ds_object
);
730 ASSERT3U(dsl_dataset_phys(ds
)->ds_bp
.blk_birth
, <=, tx
->tx_txg
);
731 ASSERT(RRW_WRITE_HELD(&dp
->dp_config_rwlock
));
733 /* We need to log before removing it from the namespace. */
734 spa_history_log_internal_ds(ds
, "destroy", tx
, "");
736 rmorigin
= (dsl_dir_is_clone(ds
->ds_dir
) &&
737 DS_IS_DEFER_DESTROY(ds
->ds_prev
) &&
738 dsl_dataset_phys(ds
->ds_prev
)->ds_num_children
== 2 &&
739 ds
->ds_prev
->ds_userrefs
== 0);
741 /* Remove our reservation. */
742 if (ds
->ds_reserved
!= 0) {
743 dsl_dataset_set_refreservation_sync_impl(ds
,
744 (ZPROP_SRC_NONE
| ZPROP_SRC_LOCAL
| ZPROP_SRC_RECEIVED
),
746 ASSERT0(ds
->ds_reserved
);
751 for (f
= 0; f
< SPA_FEATURES
; f
++) {
752 if (ds
->ds_feature_inuse
[f
]) {
753 dsl_dataset_deactivate_feature(obj
, f
, tx
);
754 ds
->ds_feature_inuse
[f
] = B_FALSE
;
758 dsl_scan_ds_destroyed(ds
, tx
);
760 if (dsl_dataset_phys(ds
)->ds_prev_snap_obj
!= 0) {
761 /* This is a clone */
762 ASSERT(ds
->ds_prev
!= NULL
);
763 ASSERT3U(dsl_dataset_phys(ds
->ds_prev
)->ds_next_snap_obj
, !=,
765 ASSERT0(dsl_dataset_phys(ds
)->ds_next_snap_obj
);
767 dmu_buf_will_dirty(ds
->ds_prev
->ds_dbuf
, tx
);
768 if (dsl_dataset_phys(ds
->ds_prev
)->ds_next_clones_obj
!= 0) {
769 dsl_dataset_remove_from_next_clones(ds
->ds_prev
,
773 ASSERT3U(dsl_dataset_phys(ds
->ds_prev
)->ds_num_children
, >, 1);
774 dsl_dataset_phys(ds
->ds_prev
)->ds_num_children
--;
778 * Destroy the deadlist. Unless it's a clone, the
779 * deadlist should be empty. (If it's a clone, it's
780 * safe to ignore the deadlist contents.)
782 dsl_deadlist_close(&ds
->ds_deadlist
);
783 dsl_deadlist_free(mos
, dsl_dataset_phys(ds
)->ds_deadlist_obj
, tx
);
784 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
785 dsl_dataset_phys(ds
)->ds_deadlist_obj
= 0;
787 VERIFY0(dmu_objset_from_ds(ds
, &os
));
789 if (!spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_ASYNC_DESTROY
)) {
790 old_synchronous_dataset_destroy(ds
, tx
);
793 * Move the bptree into the pool's list of trees to
794 * clean up and update space accounting information.
796 uint64_t used
, comp
, uncomp
;
798 zil_destroy_sync(dmu_objset_zil(os
), tx
);
800 if (!spa_feature_is_active(dp
->dp_spa
,
801 SPA_FEATURE_ASYNC_DESTROY
)) {
802 dsl_scan_t
*scn
= dp
->dp_scan
;
803 spa_feature_incr(dp
->dp_spa
, SPA_FEATURE_ASYNC_DESTROY
,
805 dp
->dp_bptree_obj
= bptree_alloc(mos
, tx
);
807 DMU_POOL_DIRECTORY_OBJECT
,
808 DMU_POOL_BPTREE_OBJ
, sizeof (uint64_t), 1,
809 &dp
->dp_bptree_obj
, tx
));
810 ASSERT(!scn
->scn_async_destroying
);
811 scn
->scn_async_destroying
= B_TRUE
;
814 used
= dsl_dir_phys(ds
->ds_dir
)->dd_used_bytes
;
815 comp
= dsl_dir_phys(ds
->ds_dir
)->dd_compressed_bytes
;
816 uncomp
= dsl_dir_phys(ds
->ds_dir
)->dd_uncompressed_bytes
;
818 ASSERT(!DS_UNIQUE_IS_ACCURATE(ds
) ||
819 dsl_dataset_phys(ds
)->ds_unique_bytes
== used
);
821 bptree_add(mos
, dp
->dp_bptree_obj
,
822 &dsl_dataset_phys(ds
)->ds_bp
,
823 dsl_dataset_phys(ds
)->ds_prev_snap_txg
,
824 used
, comp
, uncomp
, tx
);
825 dsl_dir_diduse_space(ds
->ds_dir
, DD_USED_HEAD
,
826 -used
, -comp
, -uncomp
, tx
);
827 dsl_dir_diduse_space(dp
->dp_free_dir
, DD_USED_HEAD
,
828 used
, comp
, uncomp
, tx
);
831 if (ds
->ds_prev
!= NULL
) {
832 if (spa_version(dp
->dp_spa
) >= SPA_VERSION_DIR_CLONES
) {
833 VERIFY0(zap_remove_int(mos
,
834 dsl_dir_phys(ds
->ds_prev
->ds_dir
)->dd_clones
,
837 prevobj
= ds
->ds_prev
->ds_object
;
838 dsl_dataset_rele(ds
->ds_prev
, ds
);
843 * This must be done after the dsl_traverse(), because it will
844 * re-open the objset.
847 dmu_objset_evict(ds
->ds_objset
);
848 ds
->ds_objset
= NULL
;
851 /* Erase the link in the dir */
852 dmu_buf_will_dirty(ds
->ds_dir
->dd_dbuf
, tx
);
853 dsl_dir_phys(ds
->ds_dir
)->dd_head_dataset_obj
= 0;
854 ddobj
= ds
->ds_dir
->dd_object
;
855 ASSERT(dsl_dataset_phys(ds
)->ds_snapnames_zapobj
!= 0);
856 VERIFY0(zap_destroy(mos
,
857 dsl_dataset_phys(ds
)->ds_snapnames_zapobj
, tx
));
859 if (ds
->ds_bookmarks
!= 0) {
860 VERIFY0(zap_destroy(mos
, ds
->ds_bookmarks
, tx
));
861 spa_feature_decr(dp
->dp_spa
, SPA_FEATURE_BOOKMARKS
, tx
);
864 spa_prop_clear_bootfs(dp
->dp_spa
, ds
->ds_object
, tx
);
866 ASSERT0(dsl_dataset_phys(ds
)->ds_next_clones_obj
);
867 ASSERT0(dsl_dataset_phys(ds
)->ds_props_obj
);
868 ASSERT0(dsl_dataset_phys(ds
)->ds_userrefs_obj
);
869 dsl_dir_rele(ds
->ds_dir
, ds
);
871 dmu_object_free_zapified(mos
, obj
, tx
);
873 dsl_dir_destroy_sync(ddobj
, tx
);
877 VERIFY0(dsl_dataset_hold_obj(dp
, prevobj
, FTAG
, &prev
));
878 dsl_destroy_snapshot_sync_impl(prev
, B_FALSE
, tx
);
879 dsl_dataset_rele(prev
, FTAG
);
884 dsl_destroy_head_sync(void *arg
, dmu_tx_t
*tx
)
886 dsl_destroy_head_arg_t
*ddha
= arg
;
887 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
890 VERIFY0(dsl_dataset_hold(dp
, ddha
->ddha_name
, FTAG
, &ds
));
891 dsl_destroy_head_sync_impl(ds
, tx
);
892 dsl_dataset_rele(ds
, FTAG
);
896 dsl_destroy_head_begin_sync(void *arg
, dmu_tx_t
*tx
)
898 dsl_destroy_head_arg_t
*ddha
= arg
;
899 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
902 VERIFY0(dsl_dataset_hold(dp
, ddha
->ddha_name
, FTAG
, &ds
));
904 /* Mark it as inconsistent on-disk, in case we crash */
905 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
906 dsl_dataset_phys(ds
)->ds_flags
|= DS_FLAG_INCONSISTENT
;
908 spa_history_log_internal_ds(ds
, "destroy begin", tx
, "");
909 dsl_dataset_rele(ds
, FTAG
);
913 dsl_destroy_head(const char *name
)
915 dsl_destroy_head_arg_t ddha
;
921 zfs_destroy_unmount_origin(name
);
924 error
= spa_open(name
, &spa
, FTAG
);
927 isenabled
= spa_feature_is_enabled(spa
, SPA_FEATURE_ASYNC_DESTROY
);
928 spa_close(spa
, FTAG
);
930 ddha
.ddha_name
= name
;
935 error
= dsl_sync_task(name
, dsl_destroy_head_check
,
936 dsl_destroy_head_begin_sync
, &ddha
,
937 0, ZFS_SPACE_CHECK_NONE
);
942 * Head deletion is processed in one txg on old pools;
943 * remove the objects from open context so that the txg sync
946 error
= dmu_objset_own(name
, DMU_OST_ANY
, B_FALSE
, FTAG
, &os
);
949 uint64_t prev_snap_txg
=
950 dsl_dataset_phys(dmu_objset_ds(os
))->
952 for (obj
= 0; error
== 0;
953 error
= dmu_object_next(os
, &obj
, FALSE
,
955 (void) dmu_free_long_object(os
, obj
);
956 /* sync out all frees */
957 txg_wait_synced(dmu_objset_pool(os
), 0);
958 dmu_objset_disown(os
, FTAG
);
962 return (dsl_sync_task(name
, dsl_destroy_head_check
,
963 dsl_destroy_head_sync
, &ddha
, 0, ZFS_SPACE_CHECK_NONE
));
967 * Note, this function is used as the callback for dmu_objset_find(). We
968 * always return 0 so that we will continue to find and process
969 * inconsistent datasets, even if we encounter an error trying to
970 * process one of them.
974 dsl_destroy_inconsistent(const char *dsname
, void *arg
)
978 if (dmu_objset_hold(dsname
, FTAG
, &os
) == 0) {
979 boolean_t inconsistent
= DS_IS_INCONSISTENT(dmu_objset_ds(os
));
980 dmu_objset_rele(os
, FTAG
);
982 (void) dsl_destroy_head(dsname
);
988 #if defined(_KERNEL) && defined(HAVE_SPL)
989 EXPORT_SYMBOL(dsl_destroy_head
);
990 EXPORT_SYMBOL(dsl_destroy_head_sync_impl
);
991 EXPORT_SYMBOL(dsl_dataset_user_hold_check_one
);
992 EXPORT_SYMBOL(dsl_destroy_snapshot_sync_impl
);
993 EXPORT_SYMBOL(dsl_destroy_inconsistent
);
994 EXPORT_SYMBOL(dsl_dataset_user_release_tmp
);
995 EXPORT_SYMBOL(dsl_destroy_head_check_impl
);