]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/dsl_destroy.c
Illumos 5314 - Remove "dbuf phys" db->db_data pointer aliases in ZFS
[mirror_zfs.git] / module / zfs / dsl_destroy.c
CommitLineData
13fe0198
MA
1/*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
5dbd68a3 23 * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
95fd54a1 24 * Copyright (c) 2013 Steven Hartland. All rights reserved.
788eb90c 25 * Copyright (c) 2013 by Joyent, Inc. All rights reserved.
13fe0198
MA
26 */
27
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>
38#include <sys/zap.h>
39#include <sys/zfeature.h>
40#include <sys/zfs_ioctl.h>
41#include <sys/dsl_deleg.h>
fa86b5db 42#include <sys/dmu_impl.h>
13fe0198
MA
43
44typedef struct dmu_snapshots_destroy_arg {
45 nvlist_t *dsda_snaps;
46 nvlist_t *dsda_successful_snaps;
47 boolean_t dsda_defer;
48 nvlist_t *dsda_errlist;
49} dmu_snapshots_destroy_arg_t;
50
19580676 51int
13fe0198
MA
52dsl_destroy_snapshot_check_impl(dsl_dataset_t *ds, boolean_t defer)
53{
54 if (!dsl_dataset_is_snapshot(ds))
2e528b49 55 return (SET_ERROR(EINVAL));
13fe0198
MA
56
57 if (dsl_dataset_long_held(ds))
2e528b49 58 return (SET_ERROR(EBUSY));
13fe0198
MA
59
60 /*
61 * Only allow deferred destroy on pools that support it.
62 * NOTE: deferred destroy is only supported on snapshots.
63 */
64 if (defer) {
65 if (spa_version(ds->ds_dir->dd_pool->dp_spa) <
66 SPA_VERSION_USERREFS)
2e528b49 67 return (SET_ERROR(ENOTSUP));
13fe0198
MA
68 return (0);
69 }
70
71 /*
72 * If this snapshot has an elevated user reference count,
73 * we can't destroy it yet.
74 */
75 if (ds->ds_userrefs > 0)
2e528b49 76 return (SET_ERROR(EBUSY));
13fe0198
MA
77
78 /*
79 * Can't delete a branch point.
80 */
d683ddbb 81 if (dsl_dataset_phys(ds)->ds_num_children > 1)
2e528b49 82 return (SET_ERROR(EEXIST));
13fe0198
MA
83
84 return (0);
85}
86
87static int
88dsl_destroy_snapshot_check(void *arg, dmu_tx_t *tx)
89{
90 dmu_snapshots_destroy_arg_t *dsda = arg;
91 dsl_pool_t *dp = dmu_tx_pool(tx);
92 nvpair_t *pair;
93 int error = 0;
94
95 if (!dmu_tx_is_syncing(tx))
96 return (0);
97
98 for (pair = nvlist_next_nvpair(dsda->dsda_snaps, NULL);
99 pair != NULL; pair = nvlist_next_nvpair(dsda->dsda_snaps, pair)) {
100 dsl_dataset_t *ds;
101
102 error = dsl_dataset_hold(dp, nvpair_name(pair),
103 FTAG, &ds);
104
105 /*
106 * If the snapshot does not exist, silently ignore it
107 * (it's "already destroyed").
108 */
109 if (error == ENOENT)
110 continue;
111
112 if (error == 0) {
113 error = dsl_destroy_snapshot_check_impl(ds,
114 dsda->dsda_defer);
115 dsl_dataset_rele(ds, FTAG);
116 }
117
118 if (error == 0) {
119 fnvlist_add_boolean(dsda->dsda_successful_snaps,
120 nvpair_name(pair));
121 } else {
122 fnvlist_add_int32(dsda->dsda_errlist,
123 nvpair_name(pair), error);
124 }
125 }
126
127 pair = nvlist_next_nvpair(dsda->dsda_errlist, NULL);
128 if (pair != NULL)
129 return (fnvpair_value_int32(pair));
95fd54a1 130
13fe0198
MA
131 return (0);
132}
133
134struct process_old_arg {
135 dsl_dataset_t *ds;
136 dsl_dataset_t *ds_prev;
137 boolean_t after_branch_point;
138 zio_t *pio;
139 uint64_t used, comp, uncomp;
140};
141
142static int
143process_old_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
144{
145 struct process_old_arg *poa = arg;
146 dsl_pool_t *dp = poa->ds->ds_dir->dd_pool;
147
b0bc7a84
MG
148 ASSERT(!BP_IS_HOLE(bp));
149
d683ddbb 150 if (bp->blk_birth <= dsl_dataset_phys(poa->ds)->ds_prev_snap_txg) {
13fe0198
MA
151 dsl_deadlist_insert(&poa->ds->ds_deadlist, bp, tx);
152 if (poa->ds_prev && !poa->after_branch_point &&
153 bp->blk_birth >
d683ddbb
JG
154 dsl_dataset_phys(poa->ds_prev)->ds_prev_snap_txg) {
155 dsl_dataset_phys(poa->ds_prev)->ds_unique_bytes +=
13fe0198
MA
156 bp_get_dsize_sync(dp->dp_spa, bp);
157 }
158 } else {
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);
163 }
164 return (0);
165}
166
167static void
168process_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)
170{
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;
175
176 ASSERT(ds->ds_deadlist.dl_oldfmt);
177 ASSERT(ds_next->ds_deadlist.dl_oldfmt);
178
179 poa.ds = ds;
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));
d683ddbb 186 ASSERT3U(poa.used, ==, dsl_dataset_phys(ds)->ds_unique_bytes);
13fe0198
MA
187
188 /* change snapused */
189 dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
190 -poa.used, -poa.comp, -poa.uncomp, tx);
191
192 /* swap next's deadlist to our deadlist */
193 dsl_deadlist_close(&ds->ds_deadlist);
194 dsl_deadlist_close(&ds_next->ds_deadlist);
d683ddbb
JG
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);
13fe0198 201 dsl_deadlist_open(&ds_next->ds_deadlist, mos,
d683ddbb 202 dsl_dataset_phys(ds_next)->ds_deadlist_obj);
13fe0198
MA
203}
204
205static void
206dsl_dataset_remove_clones_key(dsl_dataset_t *ds, uint64_t mintxg, dmu_tx_t *tx)
207{
208 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
77831e17
KK
209 zap_cursor_t *zc;
210 zap_attribute_t *za;
13fe0198
MA
211
212 /*
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
215 * doesn't matter.
216 */
d683ddbb 217 if (dsl_dir_phys(ds->ds_dir)->dd_clones == 0)
13fe0198
MA
218 return;
219
79c76d5b
BB
220 zc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP);
221 za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
77831e17 222
d683ddbb 223 for (zap_cursor_init(zc, mos, dsl_dir_phys(ds->ds_dir)->dd_clones);
77831e17
KK
224 zap_cursor_retrieve(zc, za) == 0;
225 zap_cursor_advance(zc)) {
13fe0198
MA
226 dsl_dataset_t *clone;
227
228 VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
77831e17 229 za->za_first_integer, FTAG, &clone));
13fe0198
MA
230 if (clone->ds_dir->dd_origin_txg > mintxg) {
231 dsl_deadlist_remove_key(&clone->ds_deadlist,
232 mintxg, tx);
233 dsl_dataset_remove_clones_key(clone, mintxg, tx);
234 }
235 dsl_dataset_rele(clone, FTAG);
236 }
77831e17
KK
237 zap_cursor_fini(zc);
238
239 kmem_free(za, sizeof (zap_attribute_t));
240 kmem_free(zc, sizeof (zap_cursor_t));
13fe0198
MA
241}
242
243void
244dsl_destroy_snapshot_sync_impl(dsl_dataset_t *ds, boolean_t defer, dmu_tx_t *tx)
245{
246#ifdef ZFS_DEBUG
247 int err;
248#endif
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;
255
256
257 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
d683ddbb 258 ASSERT3U(dsl_dataset_phys(ds)->ds_bp.blk_birth, <=, tx->tx_txg);
13fe0198
MA
259 ASSERT(refcount_is_zero(&ds->ds_longholds));
260
261 if (defer &&
d683ddbb
JG
262 (ds->ds_userrefs > 0 ||
263 dsl_dataset_phys(ds)->ds_num_children > 1)) {
13fe0198
MA
264 ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
265 dmu_buf_will_dirty(ds->ds_dbuf, tx);
d683ddbb 266 dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_DEFER_DESTROY;
13fe0198
MA
267 spa_history_log_internal_ds(ds, "defer_destroy", tx, "");
268 return;
269 }
270
d683ddbb 271 ASSERT3U(dsl_dataset_phys(ds)->ds_num_children, <=, 1);
13fe0198
MA
272
273 /* We need to log before removing it from the namespace. */
274 spa_history_log_internal_ds(ds, "destroy", tx, "");
275
276 dsl_scan_ds_destroyed(ds, tx);
277
278 obj = ds->ds_object;
279
d683ddbb 280 if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
13fe0198
MA
281 ASSERT3P(ds->ds_prev, ==, NULL);
282 VERIFY0(dsl_dataset_hold_obj(dp,
d683ddbb 283 dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &ds_prev));
13fe0198 284 after_branch_point =
d683ddbb 285 (dsl_dataset_phys(ds_prev)->ds_next_snap_obj != obj);
13fe0198
MA
286
287 dmu_buf_will_dirty(ds_prev->ds_dbuf, tx);
288 if (after_branch_point &&
d683ddbb 289 dsl_dataset_phys(ds_prev)->ds_next_clones_obj != 0) {
13fe0198 290 dsl_dataset_remove_from_next_clones(ds_prev, obj, tx);
d683ddbb 291 if (dsl_dataset_phys(ds)->ds_next_snap_obj != 0) {
13fe0198 292 VERIFY0(zap_add_int(mos,
d683ddbb
JG
293 dsl_dataset_phys(ds_prev)->
294 ds_next_clones_obj,
295 dsl_dataset_phys(ds)->ds_next_snap_obj,
296 tx));
13fe0198
MA
297 }
298 }
299 if (!after_branch_point) {
d683ddbb
JG
300 dsl_dataset_phys(ds_prev)->ds_next_snap_obj =
301 dsl_dataset_phys(ds)->ds_next_snap_obj;
13fe0198
MA
302 }
303 }
304
305 VERIFY0(dsl_dataset_hold_obj(dp,
d683ddbb
JG
306 dsl_dataset_phys(ds)->ds_next_snap_obj, FTAG, &ds_next));
307 ASSERT3U(dsl_dataset_phys(ds_next)->ds_prev_snap_obj, ==, obj);
13fe0198 308
d683ddbb 309 old_unique = dsl_dataset_phys(ds_next)->ds_unique_bytes;
13fe0198
MA
310
311 dmu_buf_will_dirty(ds_next->ds_dbuf, tx);
d683ddbb
JG
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);
13fe0198
MA
318
319 if (ds_next->ds_deadlist.dl_oldfmt) {
320 process_old_deadlist(ds, ds_prev, ds_next,
321 after_branch_point, tx);
322 } else {
323 /* Adjust prev's unique space. */
324 if (ds_prev && !after_branch_point) {
325 dsl_deadlist_space_range(&ds_next->ds_deadlist,
d683ddbb
JG
326 dsl_dataset_phys(ds_prev)->ds_prev_snap_txg,
327 dsl_dataset_phys(ds)->ds_prev_snap_txg,
13fe0198 328 &used, &comp, &uncomp);
d683ddbb 329 dsl_dataset_phys(ds_prev)->ds_unique_bytes += used;
13fe0198
MA
330 }
331
332 /* Adjust snapused. */
333 dsl_deadlist_space_range(&ds_next->ds_deadlist,
d683ddbb 334 dsl_dataset_phys(ds)->ds_prev_snap_txg, UINT64_MAX,
13fe0198
MA
335 &used, &comp, &uncomp);
336 dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
337 -used, -comp, -uncomp, tx);
338
339 /* Move blocks to be freed to pool's free list. */
340 dsl_deadlist_move_bpobj(&ds_next->ds_deadlist,
d683ddbb 341 &dp->dp_free_bpobj, dsl_dataset_phys(ds)->ds_prev_snap_txg,
13fe0198
MA
342 tx);
343 dsl_dir_diduse_space(tx->tx_pool->dp_free_dir,
344 DD_USED_HEAD, used, comp, uncomp, tx);
345
346 /* Merge our deadlist into next's and free it. */
347 dsl_deadlist_merge(&ds_next->ds_deadlist,
d683ddbb 348 dsl_dataset_phys(ds)->ds_deadlist_obj, tx);
13fe0198
MA
349 }
350 dsl_deadlist_close(&ds->ds_deadlist);
d683ddbb 351 dsl_deadlist_free(mos, dsl_dataset_phys(ds)->ds_deadlist_obj, tx);
13fe0198 352 dmu_buf_will_dirty(ds->ds_dbuf, tx);
d683ddbb 353 dsl_dataset_phys(ds)->ds_deadlist_obj = 0;
13fe0198
MA
354
355 /* Collapse range in clone heads */
356 dsl_dataset_remove_clones_key(ds,
d683ddbb 357 dsl_dataset_phys(ds)->ds_creation_txg, tx);
13fe0198
MA
358
359 if (dsl_dataset_is_snapshot(ds_next)) {
360 dsl_dataset_t *ds_nextnext;
361
362 /*
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
369 * deadlist).
370 */
371 VERIFY0(dsl_dataset_hold_obj(dp,
d683ddbb
JG
372 dsl_dataset_phys(ds_next)->ds_next_snap_obj,
373 FTAG, &ds_nextnext));
13fe0198 374 dsl_deadlist_space_range(&ds_nextnext->ds_deadlist,
d683ddbb
JG
375 dsl_dataset_phys(ds)->ds_prev_snap_txg,
376 dsl_dataset_phys(ds)->ds_creation_txg,
13fe0198 377 &used, &comp, &uncomp);
d683ddbb 378 dsl_dataset_phys(ds_next)->ds_unique_bytes += used;
13fe0198
MA
379 dsl_dataset_rele(ds_nextnext, FTAG);
380 ASSERT3P(ds_next->ds_prev, ==, NULL);
381
382 /* Collapse range in this head. */
383 VERIFY0(dsl_dataset_hold_obj(dp,
d683ddbb 384 dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj, FTAG, &hds));
13fe0198 385 dsl_deadlist_remove_key(&hds->ds_deadlist,
d683ddbb 386 dsl_dataset_phys(ds)->ds_creation_txg, tx);
13fe0198
MA
387 dsl_dataset_rele(hds, FTAG);
388
389 } else {
390 ASSERT3P(ds_next->ds_prev, ==, ds);
391 dsl_dataset_rele(ds_next->ds_prev, ds_next);
392 ds_next->ds_prev = NULL;
393 if (ds_prev) {
394 VERIFY0(dsl_dataset_hold_obj(dp,
d683ddbb 395 dsl_dataset_phys(ds)->ds_prev_snap_obj,
13fe0198
MA
396 ds_next, &ds_next->ds_prev));
397 }
398
399 dsl_dataset_recalc_head_uniq(ds_next);
400
401 /*
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.
405 */
406 if (old_unique < ds_next->ds_reserved) {
407 int64_t mrsdelta;
408 uint64_t new_unique =
d683ddbb 409 dsl_dataset_phys(ds_next)->ds_unique_bytes;
13fe0198
MA
410
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);
416 }
417 }
418 dsl_dataset_rele(ds_next, FTAG);
419
420 /*
421 * This must be done after the dsl_traverse(), because it will
422 * re-open the objset.
423 */
424 if (ds->ds_objset) {
425 dmu_objset_evict(ds->ds_objset);
426 ds->ds_objset = NULL;
427 }
428
429 /* remove from snapshot namespace */
d683ddbb 430 ASSERT(dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0);
13fe0198 431 VERIFY0(dsl_dataset_hold_obj(dp,
d683ddbb 432 dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj, FTAG, &ds_head));
13fe0198
MA
433 VERIFY0(dsl_dataset_get_snapname(ds));
434#ifdef ZFS_DEBUG
435 {
436 uint64_t val;
437
438 err = dsl_dataset_snap_lookup(ds_head,
439 ds->ds_snapname, &val);
440 ASSERT0(err);
441 ASSERT3U(val, ==, obj);
442 }
443#endif
788eb90c 444 VERIFY0(dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx, B_TRUE));
13fe0198
MA
445 dsl_dataset_rele(ds_head, FTAG);
446
447 if (ds_prev != NULL)
448 dsl_dataset_rele(ds_prev, FTAG);
449
450 spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
451
d683ddbb 452 if (dsl_dataset_phys(ds)->ds_next_clones_obj != 0) {
13fe0198
MA
453 ASSERTV(uint64_t count);
454 ASSERT0(zap_count(mos,
d683ddbb
JG
455 dsl_dataset_phys(ds)->ds_next_clones_obj, &count) &&
456 count == 0);
13fe0198 457 VERIFY0(dmu_object_free(mos,
d683ddbb 458 dsl_dataset_phys(ds)->ds_next_clones_obj, tx));
13fe0198 459 }
d683ddbb
JG
460 if (dsl_dataset_phys(ds)->ds_props_obj != 0)
461 VERIFY0(zap_destroy(mos, dsl_dataset_phys(ds)->ds_props_obj,
462 tx));
463 if (dsl_dataset_phys(ds)->ds_userrefs_obj != 0)
464 VERIFY0(zap_destroy(mos, dsl_dataset_phys(ds)->ds_userrefs_obj,
465 tx));
13fe0198
MA
466 dsl_dir_rele(ds->ds_dir, ds);
467 ds->ds_dir = NULL;
fa86b5db 468 dmu_object_free_zapified(mos, obj, tx);
13fe0198
MA
469}
470
471static void
472dsl_destroy_snapshot_sync(void *arg, dmu_tx_t *tx)
473{
474 dmu_snapshots_destroy_arg_t *dsda = arg;
475 dsl_pool_t *dp = dmu_tx_pool(tx);
476 nvpair_t *pair;
477
478 for (pair = nvlist_next_nvpair(dsda->dsda_successful_snaps, NULL);
479 pair != NULL;
480 pair = nvlist_next_nvpair(dsda->dsda_successful_snaps, pair)) {
481 dsl_dataset_t *ds;
482
483 VERIFY0(dsl_dataset_hold(dp, nvpair_name(pair), FTAG, &ds));
484
485 dsl_destroy_snapshot_sync_impl(ds, dsda->dsda_defer, tx);
486 dsl_dataset_rele(ds, FTAG);
487 }
488}
489
490/*
491 * The semantics of this function are described in the comment above
492 * lzc_destroy_snaps(). To summarize:
493 *
494 * The snapshots must all be in the same pool.
495 *
496 * Snapshots that don't exist will be silently ignored (considered to be
497 * "already deleted").
498 *
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.
502 */
503int
504dsl_destroy_snapshots_nvl(nvlist_t *snaps, boolean_t defer,
505 nvlist_t *errlist)
506{
507 dmu_snapshots_destroy_arg_t dsda;
508 int error;
509 nvpair_t *pair;
510
511 pair = nvlist_next_nvpair(snaps, NULL);
512 if (pair == NULL)
513 return (0);
514
515 dsda.dsda_snaps = snaps;
d1d7e268 516 VERIFY0(nvlist_alloc(&dsda.dsda_successful_snaps,
79c76d5b 517 NV_UNIQUE_NAME, KM_SLEEP));
13fe0198
MA
518 dsda.dsda_defer = defer;
519 dsda.dsda_errlist = errlist;
520
521 error = dsl_sync_task(nvpair_name(pair),
522 dsl_destroy_snapshot_check, dsl_destroy_snapshot_sync,
523 &dsda, 0);
524 fnvlist_free(dsda.dsda_successful_snaps);
525
526 return (error);
527}
528
529int
530dsl_destroy_snapshot(const char *name, boolean_t defer)
531{
532 int error;
79c76d5b
BB
533 nvlist_t *nvl = fnvlist_alloc();
534 nvlist_t *errlist = fnvlist_alloc();
13fe0198
MA
535
536 fnvlist_add_boolean(nvl, name);
537 error = dsl_destroy_snapshots_nvl(nvl, defer, errlist);
538 fnvlist_free(errlist);
539 fnvlist_free(nvl);
540 return (error);
541}
542
543struct killarg {
544 dsl_dataset_t *ds;
545 dmu_tx_t *tx;
546};
547
548/* ARGSUSED */
549static int
550kill_blkptr(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
5dbd68a3 551 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
13fe0198
MA
552{
553 struct killarg *ka = arg;
554 dmu_tx_t *tx = ka->tx;
555
9b67f605 556 if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
13fe0198
MA
557 return (0);
558
559 if (zb->zb_level == ZB_ZIL_LEVEL) {
560 ASSERT(zilog != NULL);
561 /*
562 * It's a block in the intent log. It has no
563 * accounting, so just free it.
564 */
565 dsl_free(ka->tx->tx_pool, ka->tx->tx_txg, bp);
566 } else {
567 ASSERT(zilog == NULL);
d683ddbb
JG
568 ASSERT3U(bp->blk_birth, >,
569 dsl_dataset_phys(ka->ds)->ds_prev_snap_txg);
13fe0198
MA
570 (void) dsl_dataset_block_kill(ka->ds, bp, tx, B_FALSE);
571 }
572
573 return (0);
574}
575
576static void
577old_synchronous_dataset_destroy(dsl_dataset_t *ds, dmu_tx_t *tx)
578{
579 struct killarg ka;
580
581 /*
582 * Free everything that we point to (that's born after
583 * the previous snapshot, if we are a clone)
584 *
585 * NB: this should be very quick, because we already
586 * freed all the objects in open context.
587 */
588 ka.ds = ds;
589 ka.tx = tx;
590 VERIFY0(traverse_dataset(ds,
d683ddbb 591 dsl_dataset_phys(ds)->ds_prev_snap_txg, TRAVERSE_POST,
13fe0198 592 kill_blkptr, &ka));
d683ddbb
JG
593 ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) ||
594 dsl_dataset_phys(ds)->ds_unique_bytes == 0);
13fe0198
MA
595}
596
597typedef struct dsl_destroy_head_arg {
598 const char *ddha_name;
599} dsl_destroy_head_arg_t;
600
601int
602dsl_destroy_head_check_impl(dsl_dataset_t *ds, int expected_holds)
603{
604 int error;
605 uint64_t count;
606 objset_t *mos;
607
9b67f605 608 ASSERT(!dsl_dataset_is_snapshot(ds));
13fe0198 609 if (dsl_dataset_is_snapshot(ds))
2e528b49 610 return (SET_ERROR(EINVAL));
13fe0198
MA
611
612 if (refcount_count(&ds->ds_longholds) != expected_holds)
2e528b49 613 return (SET_ERROR(EBUSY));
13fe0198
MA
614
615 mos = ds->ds_dir->dd_pool->dp_meta_objset;
616
617 /*
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
620 * from.)
621 */
622 if (ds->ds_prev != NULL &&
d683ddbb 623 dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj == ds->ds_object)
2e528b49 624 return (SET_ERROR(EBUSY));
13fe0198
MA
625
626 /*
627 * Can't delete if there are children of this fs.
628 */
629 error = zap_count(mos,
d683ddbb 630 dsl_dir_phys(ds->ds_dir)->dd_child_dir_zapobj, &count);
13fe0198
MA
631 if (error != 0)
632 return (error);
633 if (count != 0)
2e528b49 634 return (SET_ERROR(EEXIST));
13fe0198
MA
635
636 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev) &&
d683ddbb 637 dsl_dataset_phys(ds->ds_prev)->ds_num_children == 2 &&
13fe0198
MA
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))
2e528b49 641 return (SET_ERROR(EBUSY));
13fe0198
MA
642 }
643 return (0);
644}
645
646static int
647dsl_destroy_head_check(void *arg, dmu_tx_t *tx)
648{
649 dsl_destroy_head_arg_t *ddha = arg;
650 dsl_pool_t *dp = dmu_tx_pool(tx);
651 dsl_dataset_t *ds;
652 int error;
653
654 error = dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds);
655 if (error != 0)
656 return (error);
657
658 error = dsl_destroy_head_check_impl(ds, 0);
659 dsl_dataset_rele(ds, FTAG);
660 return (error);
661}
662
663static void
664dsl_dir_destroy_sync(uint64_t ddobj, dmu_tx_t *tx)
665{
666 dsl_dir_t *dd;
667 dsl_pool_t *dp = dmu_tx_pool(tx);
668 objset_t *mos = dp->dp_meta_objset;
669 dd_used_t t;
670
671 ASSERT(RRW_WRITE_HELD(&dmu_tx_pool(tx)->dp_config_rwlock));
672
673 VERIFY0(dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd));
674
d683ddbb 675 ASSERT0(dsl_dir_phys(dd)->dd_head_dataset_obj);
13fe0198 676
788eb90c
JJ
677 /*
678 * Decrement the filesystem count for all parent filesystems.
679 *
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 '%'.
683 */
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);
687
13fe0198
MA
688 /*
689 * Remove our reservation. The impl() routine avoids setting the
690 * actual property, which would require the (already destroyed) ds.
691 */
692 dsl_dir_set_reservation_sync_impl(dd, 0, tx);
693
d683ddbb
JG
694 ASSERT0(dsl_dir_phys(dd)->dd_used_bytes);
695 ASSERT0(dsl_dir_phys(dd)->dd_reserved);
13fe0198 696 for (t = 0; t < DD_USED_NUM; t++)
d683ddbb 697 ASSERT0(dsl_dir_phys(dd)->dd_used_breakdown[t]);
13fe0198 698
d683ddbb
JG
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));
13fe0198 702 VERIFY0(zap_remove(mos,
d683ddbb
JG
703 dsl_dir_phys(dd->dd_parent)->dd_child_dir_zapobj,
704 dd->dd_myname, tx));
13fe0198
MA
705
706 dsl_dir_rele(dd, FTAG);
fa86b5db 707 dmu_object_free_zapified(mos, ddobj, tx);
13fe0198
MA
708}
709
710void
711dsl_destroy_head_sync_impl(dsl_dataset_t *ds, dmu_tx_t *tx)
712{
713 dsl_pool_t *dp = dmu_tx_pool(tx);
714 objset_t *mos = dp->dp_meta_objset;
715 uint64_t obj, ddobj, prevobj = 0;
716 boolean_t rmorigin;
13fe0198
MA
717 objset_t *os;
718
d683ddbb 719 ASSERT3U(dsl_dataset_phys(ds)->ds_num_children, <=, 1);
13fe0198 720 ASSERT(ds->ds_prev == NULL ||
d683ddbb
JG
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);
13fe0198
MA
723 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
724
725 /* We need to log before removing it from the namespace. */
726 spa_history_log_internal_ds(ds, "destroy", tx, "");
727
728 rmorigin = (dsl_dir_is_clone(ds->ds_dir) &&
729 DS_IS_DEFER_DESTROY(ds->ds_prev) &&
d683ddbb 730 dsl_dataset_phys(ds->ds_prev)->ds_num_children == 2 &&
13fe0198
MA
731 ds->ds_prev->ds_userrefs == 0);
732
9b67f605 733 /* Remove our reservation. */
13fe0198
MA
734 if (ds->ds_reserved != 0) {
735 dsl_dataset_set_refreservation_sync_impl(ds,
736 (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED),
737 0, tx);
738 ASSERT0(ds->ds_reserved);
739 }
740
741 dsl_scan_ds_destroyed(ds, tx);
742
743 obj = ds->ds_object;
744
d683ddbb 745 if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
13fe0198
MA
746 /* This is a clone */
747 ASSERT(ds->ds_prev != NULL);
d683ddbb
JG
748 ASSERT3U(dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj, !=,
749 obj);
750 ASSERT0(dsl_dataset_phys(ds)->ds_next_snap_obj);
13fe0198
MA
751
752 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
d683ddbb 753 if (dsl_dataset_phys(ds->ds_prev)->ds_next_clones_obj != 0) {
13fe0198
MA
754 dsl_dataset_remove_from_next_clones(ds->ds_prev,
755 obj, tx);
756 }
757
d683ddbb
JG
758 ASSERT3U(dsl_dataset_phys(ds->ds_prev)->ds_num_children, >, 1);
759 dsl_dataset_phys(ds->ds_prev)->ds_num_children--;
13fe0198
MA
760 }
761
13fe0198
MA
762 /*
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.)
766 */
767 dsl_deadlist_close(&ds->ds_deadlist);
d683ddbb 768 dsl_deadlist_free(mos, dsl_dataset_phys(ds)->ds_deadlist_obj, tx);
13fe0198 769 dmu_buf_will_dirty(ds->ds_dbuf, tx);
d683ddbb 770 dsl_dataset_phys(ds)->ds_deadlist_obj = 0;
13fe0198
MA
771
772 VERIFY0(dmu_objset_from_ds(ds, &os));
773
fa86b5db 774 if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_ASYNC_DESTROY)) {
13fe0198
MA
775 old_synchronous_dataset_destroy(ds, tx);
776 } else {
777 /*
778 * Move the bptree into the pool's list of trees to
779 * clean up and update space accounting information.
780 */
781 uint64_t used, comp, uncomp;
782
783 zil_destroy_sync(dmu_objset_zil(os), tx);
784
fa86b5db
MA
785 if (!spa_feature_is_active(dp->dp_spa,
786 SPA_FEATURE_ASYNC_DESTROY)) {
2696dfaf 787 dsl_scan_t *scn = dp->dp_scan;
fa86b5db
MA
788 spa_feature_incr(dp->dp_spa, SPA_FEATURE_ASYNC_DESTROY,
789 tx);
13fe0198
MA
790 dp->dp_bptree_obj = bptree_alloc(mos, tx);
791 VERIFY0(zap_add(mos,
792 DMU_POOL_DIRECTORY_OBJECT,
793 DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1,
794 &dp->dp_bptree_obj, tx));
2696dfaf
GW
795 ASSERT(!scn->scn_async_destroying);
796 scn->scn_async_destroying = B_TRUE;
13fe0198
MA
797 }
798
d683ddbb
JG
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;
13fe0198
MA
802
803 ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) ||
d683ddbb 804 dsl_dataset_phys(ds)->ds_unique_bytes == used);
13fe0198
MA
805
806 bptree_add(mos, dp->dp_bptree_obj,
d683ddbb
JG
807 &dsl_dataset_phys(ds)->ds_bp,
808 dsl_dataset_phys(ds)->ds_prev_snap_txg,
13fe0198
MA
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);
814 }
815
816 if (ds->ds_prev != NULL) {
817 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
818 VERIFY0(zap_remove_int(mos,
d683ddbb 819 dsl_dir_phys(ds->ds_prev->ds_dir)->dd_clones,
13fe0198
MA
820 ds->ds_object, tx));
821 }
822 prevobj = ds->ds_prev->ds_object;
823 dsl_dataset_rele(ds->ds_prev, ds);
824 ds->ds_prev = NULL;
825 }
826
827 /*
828 * This must be done after the dsl_traverse(), because it will
829 * re-open the objset.
830 */
831 if (ds->ds_objset) {
832 dmu_objset_evict(ds->ds_objset);
833 ds->ds_objset = NULL;
834 }
835
836 /* Erase the link in the dir */
837 dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
d683ddbb 838 dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj = 0;
13fe0198 839 ddobj = ds->ds_dir->dd_object;
d683ddbb
JG
840 ASSERT(dsl_dataset_phys(ds)->ds_snapnames_zapobj != 0);
841 VERIFY0(zap_destroy(mos,
842 dsl_dataset_phys(ds)->ds_snapnames_zapobj, tx));
13fe0198 843
da536844 844 if (ds->ds_bookmarks != 0) {
d683ddbb 845 VERIFY0(zap_destroy(mos, ds->ds_bookmarks, tx));
da536844
MA
846 spa_feature_decr(dp->dp_spa, SPA_FEATURE_BOOKMARKS, tx);
847 }
848
13fe0198
MA
849 spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
850
d683ddbb
JG
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);
13fe0198
MA
854 dsl_dir_rele(ds->ds_dir, ds);
855 ds->ds_dir = NULL;
fa86b5db 856 dmu_object_free_zapified(mos, obj, tx);
13fe0198
MA
857
858 dsl_dir_destroy_sync(ddobj, tx);
859
860 if (rmorigin) {
861 dsl_dataset_t *prev;
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);
865 }
866}
867
868static void
869dsl_destroy_head_sync(void *arg, dmu_tx_t *tx)
870{
871 dsl_destroy_head_arg_t *ddha = arg;
872 dsl_pool_t *dp = dmu_tx_pool(tx);
873 dsl_dataset_t *ds;
874
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);
878}
879
880static void
881dsl_destroy_head_begin_sync(void *arg, dmu_tx_t *tx)
882{
883 dsl_destroy_head_arg_t *ddha = arg;
884 dsl_pool_t *dp = dmu_tx_pool(tx);
885 dsl_dataset_t *ds;
886
887 VERIFY0(dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds));
888
889 /* Mark it as inconsistent on-disk, in case we crash */
890 dmu_buf_will_dirty(ds->ds_dbuf, tx);
d683ddbb 891 dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_INCONSISTENT;
13fe0198
MA
892
893 spa_history_log_internal_ds(ds, "destroy begin", tx, "");
894 dsl_dataset_rele(ds, FTAG);
895}
896
897int
898dsl_destroy_head(const char *name)
899{
900 dsl_destroy_head_arg_t ddha;
901 int error;
902 spa_t *spa;
903 boolean_t isenabled;
904
905#ifdef _KERNEL
906 zfs_destroy_unmount_origin(name);
907#endif
908
909 error = spa_open(name, &spa, FTAG);
910 if (error != 0)
911 return (error);
fa86b5db 912 isenabled = spa_feature_is_enabled(spa, SPA_FEATURE_ASYNC_DESTROY);
13fe0198
MA
913 spa_close(spa, FTAG);
914
915 ddha.ddha_name = name;
916
917 if (!isenabled) {
918 objset_t *os;
919
920 error = dsl_sync_task(name, dsl_destroy_head_check,
921 dsl_destroy_head_begin_sync, &ddha, 0);
922 if (error != 0)
923 return (error);
924
925 /*
926 * Head deletion is processed in one txg on old pools;
927 * remove the objects from open context so that the txg sync
928 * is not too long.
929 */
930 error = dmu_objset_own(name, DMU_OST_ANY, B_FALSE, FTAG, &os);
931 if (error == 0) {
932 uint64_t obj;
933 uint64_t prev_snap_txg =
d683ddbb
JG
934 dsl_dataset_phys(dmu_objset_ds(os))->
935 ds_prev_snap_txg;
13fe0198
MA
936 for (obj = 0; error == 0;
937 error = dmu_object_next(os, &obj, FALSE,
938 prev_snap_txg))
b663a23d 939 (void) dmu_free_long_object(os, obj);
13fe0198
MA
940 /* sync out all frees */
941 txg_wait_synced(dmu_objset_pool(os), 0);
942 dmu_objset_disown(os, FTAG);
943 }
944 }
945
946 return (dsl_sync_task(name, dsl_destroy_head_check,
947 dsl_destroy_head_sync, &ddha, 0));
948}
949
950/*
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.
955 */
956/* ARGSUSED */
957int
958dsl_destroy_inconsistent(const char *dsname, void *arg)
959{
960 objset_t *os;
961
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);
965 if (inconsistent)
966 (void) dsl_destroy_head(dsname);
967 }
968 return (0);
969}
970
971
972#if defined(_KERNEL) && defined(HAVE_SPL)
973EXPORT_SYMBOL(dsl_destroy_head);
974EXPORT_SYMBOL(dsl_destroy_head_sync_impl);
975EXPORT_SYMBOL(dsl_dataset_user_hold_check_one);
976EXPORT_SYMBOL(dsl_destroy_snapshot_sync_impl);
977EXPORT_SYMBOL(dsl_destroy_inconsistent);
978EXPORT_SYMBOL(dsl_dataset_user_release_tmp);
979EXPORT_SYMBOL(dsl_destroy_head_check_impl);
980#endif