]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/dsl_destroy.c
Illumos 5027 - zfs large block support
[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{
0c66c32d 54 if (!ds->ds_is_snapshot)
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
f1512ee6
MA
280 if (ds->ds_large_blocks) {
281 ASSERT0(zap_contains(mos, obj, DS_FIELD_LARGE_BLOCKS));
282 spa_feature_decr(dp->dp_spa, SPA_FEATURE_LARGE_BLOCKS, tx);
283 }
d683ddbb 284 if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
13fe0198
MA
285 ASSERT3P(ds->ds_prev, ==, NULL);
286 VERIFY0(dsl_dataset_hold_obj(dp,
d683ddbb 287 dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &ds_prev));
13fe0198 288 after_branch_point =
d683ddbb 289 (dsl_dataset_phys(ds_prev)->ds_next_snap_obj != obj);
13fe0198
MA
290
291 dmu_buf_will_dirty(ds_prev->ds_dbuf, tx);
292 if (after_branch_point &&
d683ddbb 293 dsl_dataset_phys(ds_prev)->ds_next_clones_obj != 0) {
13fe0198 294 dsl_dataset_remove_from_next_clones(ds_prev, obj, tx);
d683ddbb 295 if (dsl_dataset_phys(ds)->ds_next_snap_obj != 0) {
13fe0198 296 VERIFY0(zap_add_int(mos,
d683ddbb
JG
297 dsl_dataset_phys(ds_prev)->
298 ds_next_clones_obj,
299 dsl_dataset_phys(ds)->ds_next_snap_obj,
300 tx));
13fe0198
MA
301 }
302 }
303 if (!after_branch_point) {
d683ddbb
JG
304 dsl_dataset_phys(ds_prev)->ds_next_snap_obj =
305 dsl_dataset_phys(ds)->ds_next_snap_obj;
13fe0198
MA
306 }
307 }
308
309 VERIFY0(dsl_dataset_hold_obj(dp,
d683ddbb
JG
310 dsl_dataset_phys(ds)->ds_next_snap_obj, FTAG, &ds_next));
311 ASSERT3U(dsl_dataset_phys(ds_next)->ds_prev_snap_obj, ==, obj);
13fe0198 312
d683ddbb 313 old_unique = dsl_dataset_phys(ds_next)->ds_unique_bytes;
13fe0198
MA
314
315 dmu_buf_will_dirty(ds_next->ds_dbuf, tx);
d683ddbb
JG
316 dsl_dataset_phys(ds_next)->ds_prev_snap_obj =
317 dsl_dataset_phys(ds)->ds_prev_snap_obj;
318 dsl_dataset_phys(ds_next)->ds_prev_snap_txg =
319 dsl_dataset_phys(ds)->ds_prev_snap_txg;
320 ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, ==,
321 ds_prev ? dsl_dataset_phys(ds_prev)->ds_creation_txg : 0);
13fe0198
MA
322
323 if (ds_next->ds_deadlist.dl_oldfmt) {
324 process_old_deadlist(ds, ds_prev, ds_next,
325 after_branch_point, tx);
326 } else {
327 /* Adjust prev's unique space. */
328 if (ds_prev && !after_branch_point) {
329 dsl_deadlist_space_range(&ds_next->ds_deadlist,
d683ddbb
JG
330 dsl_dataset_phys(ds_prev)->ds_prev_snap_txg,
331 dsl_dataset_phys(ds)->ds_prev_snap_txg,
13fe0198 332 &used, &comp, &uncomp);
d683ddbb 333 dsl_dataset_phys(ds_prev)->ds_unique_bytes += used;
13fe0198
MA
334 }
335
336 /* Adjust snapused. */
337 dsl_deadlist_space_range(&ds_next->ds_deadlist,
d683ddbb 338 dsl_dataset_phys(ds)->ds_prev_snap_txg, UINT64_MAX,
13fe0198
MA
339 &used, &comp, &uncomp);
340 dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
341 -used, -comp, -uncomp, tx);
342
343 /* Move blocks to be freed to pool's free list. */
344 dsl_deadlist_move_bpobj(&ds_next->ds_deadlist,
d683ddbb 345 &dp->dp_free_bpobj, dsl_dataset_phys(ds)->ds_prev_snap_txg,
13fe0198
MA
346 tx);
347 dsl_dir_diduse_space(tx->tx_pool->dp_free_dir,
348 DD_USED_HEAD, used, comp, uncomp, tx);
349
350 /* Merge our deadlist into next's and free it. */
351 dsl_deadlist_merge(&ds_next->ds_deadlist,
d683ddbb 352 dsl_dataset_phys(ds)->ds_deadlist_obj, tx);
13fe0198
MA
353 }
354 dsl_deadlist_close(&ds->ds_deadlist);
d683ddbb 355 dsl_deadlist_free(mos, dsl_dataset_phys(ds)->ds_deadlist_obj, tx);
13fe0198 356 dmu_buf_will_dirty(ds->ds_dbuf, tx);
d683ddbb 357 dsl_dataset_phys(ds)->ds_deadlist_obj = 0;
13fe0198
MA
358
359 /* Collapse range in clone heads */
360 dsl_dataset_remove_clones_key(ds,
d683ddbb 361 dsl_dataset_phys(ds)->ds_creation_txg, tx);
13fe0198 362
0c66c32d 363 if (ds_next->ds_is_snapshot) {
13fe0198
MA
364 dsl_dataset_t *ds_nextnext;
365
366 /*
367 * Update next's unique to include blocks which
368 * were previously shared by only this snapshot
369 * and it. Those blocks will be born after the
370 * prev snap and before this snap, and will have
371 * died after the next snap and before the one
372 * after that (ie. be on the snap after next's
373 * deadlist).
374 */
375 VERIFY0(dsl_dataset_hold_obj(dp,
d683ddbb
JG
376 dsl_dataset_phys(ds_next)->ds_next_snap_obj,
377 FTAG, &ds_nextnext));
13fe0198 378 dsl_deadlist_space_range(&ds_nextnext->ds_deadlist,
d683ddbb
JG
379 dsl_dataset_phys(ds)->ds_prev_snap_txg,
380 dsl_dataset_phys(ds)->ds_creation_txg,
13fe0198 381 &used, &comp, &uncomp);
d683ddbb 382 dsl_dataset_phys(ds_next)->ds_unique_bytes += used;
13fe0198
MA
383 dsl_dataset_rele(ds_nextnext, FTAG);
384 ASSERT3P(ds_next->ds_prev, ==, NULL);
385
386 /* Collapse range in this head. */
387 VERIFY0(dsl_dataset_hold_obj(dp,
d683ddbb 388 dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj, FTAG, &hds));
13fe0198 389 dsl_deadlist_remove_key(&hds->ds_deadlist,
d683ddbb 390 dsl_dataset_phys(ds)->ds_creation_txg, tx);
13fe0198
MA
391 dsl_dataset_rele(hds, FTAG);
392
393 } else {
394 ASSERT3P(ds_next->ds_prev, ==, ds);
395 dsl_dataset_rele(ds_next->ds_prev, ds_next);
396 ds_next->ds_prev = NULL;
397 if (ds_prev) {
398 VERIFY0(dsl_dataset_hold_obj(dp,
d683ddbb 399 dsl_dataset_phys(ds)->ds_prev_snap_obj,
13fe0198
MA
400 ds_next, &ds_next->ds_prev));
401 }
402
403 dsl_dataset_recalc_head_uniq(ds_next);
404
405 /*
406 * Reduce the amount of our unconsumed refreservation
407 * being charged to our parent by the amount of
408 * new unique data we have gained.
409 */
410 if (old_unique < ds_next->ds_reserved) {
411 int64_t mrsdelta;
412 uint64_t new_unique =
d683ddbb 413 dsl_dataset_phys(ds_next)->ds_unique_bytes;
13fe0198
MA
414
415 ASSERT(old_unique <= new_unique);
416 mrsdelta = MIN(new_unique - old_unique,
417 ds_next->ds_reserved - old_unique);
418 dsl_dir_diduse_space(ds->ds_dir,
419 DD_USED_REFRSRV, -mrsdelta, 0, 0, tx);
420 }
421 }
422 dsl_dataset_rele(ds_next, FTAG);
423
424 /*
425 * This must be done after the dsl_traverse(), because it will
426 * re-open the objset.
427 */
428 if (ds->ds_objset) {
429 dmu_objset_evict(ds->ds_objset);
430 ds->ds_objset = NULL;
431 }
432
433 /* remove from snapshot namespace */
d683ddbb 434 ASSERT(dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0);
13fe0198 435 VERIFY0(dsl_dataset_hold_obj(dp,
d683ddbb 436 dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj, FTAG, &ds_head));
13fe0198
MA
437 VERIFY0(dsl_dataset_get_snapname(ds));
438#ifdef ZFS_DEBUG
439 {
440 uint64_t val;
441
442 err = dsl_dataset_snap_lookup(ds_head,
443 ds->ds_snapname, &val);
444 ASSERT0(err);
445 ASSERT3U(val, ==, obj);
446 }
447#endif
788eb90c 448 VERIFY0(dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx, B_TRUE));
13fe0198
MA
449 dsl_dataset_rele(ds_head, FTAG);
450
451 if (ds_prev != NULL)
452 dsl_dataset_rele(ds_prev, FTAG);
453
454 spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
455
d683ddbb 456 if (dsl_dataset_phys(ds)->ds_next_clones_obj != 0) {
13fe0198
MA
457 ASSERTV(uint64_t count);
458 ASSERT0(zap_count(mos,
d683ddbb
JG
459 dsl_dataset_phys(ds)->ds_next_clones_obj, &count) &&
460 count == 0);
13fe0198 461 VERIFY0(dmu_object_free(mos,
d683ddbb 462 dsl_dataset_phys(ds)->ds_next_clones_obj, tx));
13fe0198 463 }
d683ddbb
JG
464 if (dsl_dataset_phys(ds)->ds_props_obj != 0)
465 VERIFY0(zap_destroy(mos, dsl_dataset_phys(ds)->ds_props_obj,
466 tx));
467 if (dsl_dataset_phys(ds)->ds_userrefs_obj != 0)
468 VERIFY0(zap_destroy(mos, dsl_dataset_phys(ds)->ds_userrefs_obj,
469 tx));
13fe0198
MA
470 dsl_dir_rele(ds->ds_dir, ds);
471 ds->ds_dir = NULL;
fa86b5db 472 dmu_object_free_zapified(mos, obj, tx);
13fe0198
MA
473}
474
475static void
476dsl_destroy_snapshot_sync(void *arg, dmu_tx_t *tx)
477{
478 dmu_snapshots_destroy_arg_t *dsda = arg;
479 dsl_pool_t *dp = dmu_tx_pool(tx);
480 nvpair_t *pair;
481
482 for (pair = nvlist_next_nvpair(dsda->dsda_successful_snaps, NULL);
483 pair != NULL;
484 pair = nvlist_next_nvpair(dsda->dsda_successful_snaps, pair)) {
485 dsl_dataset_t *ds;
486
487 VERIFY0(dsl_dataset_hold(dp, nvpair_name(pair), FTAG, &ds));
488
489 dsl_destroy_snapshot_sync_impl(ds, dsda->dsda_defer, tx);
490 dsl_dataset_rele(ds, FTAG);
491 }
492}
493
494/*
495 * The semantics of this function are described in the comment above
496 * lzc_destroy_snaps(). To summarize:
497 *
498 * The snapshots must all be in the same pool.
499 *
500 * Snapshots that don't exist will be silently ignored (considered to be
501 * "already deleted").
502 *
503 * On success, all snaps will be destroyed and this will return 0.
504 * On failure, no snaps will be destroyed, the errlist will be filled in,
505 * and this will return an errno.
506 */
507int
508dsl_destroy_snapshots_nvl(nvlist_t *snaps, boolean_t defer,
509 nvlist_t *errlist)
510{
511 dmu_snapshots_destroy_arg_t dsda;
512 int error;
513 nvpair_t *pair;
514
515 pair = nvlist_next_nvpair(snaps, NULL);
516 if (pair == NULL)
517 return (0);
518
519 dsda.dsda_snaps = snaps;
d1d7e268 520 VERIFY0(nvlist_alloc(&dsda.dsda_successful_snaps,
79c76d5b 521 NV_UNIQUE_NAME, KM_SLEEP));
13fe0198
MA
522 dsda.dsda_defer = defer;
523 dsda.dsda_errlist = errlist;
524
525 error = dsl_sync_task(nvpair_name(pair),
526 dsl_destroy_snapshot_check, dsl_destroy_snapshot_sync,
3d45fdd6 527 &dsda, 0, ZFS_SPACE_CHECK_NONE);
13fe0198
MA
528 fnvlist_free(dsda.dsda_successful_snaps);
529
530 return (error);
531}
532
533int
534dsl_destroy_snapshot(const char *name, boolean_t defer)
535{
536 int error;
79c76d5b
BB
537 nvlist_t *nvl = fnvlist_alloc();
538 nvlist_t *errlist = fnvlist_alloc();
13fe0198
MA
539
540 fnvlist_add_boolean(nvl, name);
541 error = dsl_destroy_snapshots_nvl(nvl, defer, errlist);
542 fnvlist_free(errlist);
543 fnvlist_free(nvl);
544 return (error);
545}
546
547struct killarg {
548 dsl_dataset_t *ds;
549 dmu_tx_t *tx;
550};
551
552/* ARGSUSED */
553static int
554kill_blkptr(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
5dbd68a3 555 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
13fe0198
MA
556{
557 struct killarg *ka = arg;
558 dmu_tx_t *tx = ka->tx;
559
9b67f605 560 if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
13fe0198
MA
561 return (0);
562
563 if (zb->zb_level == ZB_ZIL_LEVEL) {
564 ASSERT(zilog != NULL);
565 /*
566 * It's a block in the intent log. It has no
567 * accounting, so just free it.
568 */
569 dsl_free(ka->tx->tx_pool, ka->tx->tx_txg, bp);
570 } else {
571 ASSERT(zilog == NULL);
d683ddbb
JG
572 ASSERT3U(bp->blk_birth, >,
573 dsl_dataset_phys(ka->ds)->ds_prev_snap_txg);
13fe0198
MA
574 (void) dsl_dataset_block_kill(ka->ds, bp, tx, B_FALSE);
575 }
576
577 return (0);
578}
579
580static void
581old_synchronous_dataset_destroy(dsl_dataset_t *ds, dmu_tx_t *tx)
582{
583 struct killarg ka;
584
585 /*
586 * Free everything that we point to (that's born after
587 * the previous snapshot, if we are a clone)
588 *
589 * NB: this should be very quick, because we already
590 * freed all the objects in open context.
591 */
592 ka.ds = ds;
593 ka.tx = tx;
594 VERIFY0(traverse_dataset(ds,
d683ddbb 595 dsl_dataset_phys(ds)->ds_prev_snap_txg, TRAVERSE_POST,
13fe0198 596 kill_blkptr, &ka));
d683ddbb
JG
597 ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) ||
598 dsl_dataset_phys(ds)->ds_unique_bytes == 0);
13fe0198
MA
599}
600
601typedef struct dsl_destroy_head_arg {
602 const char *ddha_name;
603} dsl_destroy_head_arg_t;
604
605int
606dsl_destroy_head_check_impl(dsl_dataset_t *ds, int expected_holds)
607{
608 int error;
609 uint64_t count;
610 objset_t *mos;
611
0c66c32d
JG
612 ASSERT(!ds->ds_is_snapshot);
613 if (ds->ds_is_snapshot)
2e528b49 614 return (SET_ERROR(EINVAL));
13fe0198
MA
615
616 if (refcount_count(&ds->ds_longholds) != expected_holds)
2e528b49 617 return (SET_ERROR(EBUSY));
13fe0198
MA
618
619 mos = ds->ds_dir->dd_pool->dp_meta_objset;
620
621 /*
622 * Can't delete a head dataset if there are snapshots of it.
623 * (Except if the only snapshots are from the branch we cloned
624 * from.)
625 */
626 if (ds->ds_prev != NULL &&
d683ddbb 627 dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj == ds->ds_object)
2e528b49 628 return (SET_ERROR(EBUSY));
13fe0198
MA
629
630 /*
631 * Can't delete if there are children of this fs.
632 */
633 error = zap_count(mos,
d683ddbb 634 dsl_dir_phys(ds->ds_dir)->dd_child_dir_zapobj, &count);
13fe0198
MA
635 if (error != 0)
636 return (error);
637 if (count != 0)
2e528b49 638 return (SET_ERROR(EEXIST));
13fe0198
MA
639
640 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev) &&
d683ddbb 641 dsl_dataset_phys(ds->ds_prev)->ds_num_children == 2 &&
13fe0198
MA
642 ds->ds_prev->ds_userrefs == 0) {
643 /* We need to remove the origin snapshot as well. */
644 if (!refcount_is_zero(&ds->ds_prev->ds_longholds))
2e528b49 645 return (SET_ERROR(EBUSY));
13fe0198
MA
646 }
647 return (0);
648}
649
650static int
651dsl_destroy_head_check(void *arg, dmu_tx_t *tx)
652{
653 dsl_destroy_head_arg_t *ddha = arg;
654 dsl_pool_t *dp = dmu_tx_pool(tx);
655 dsl_dataset_t *ds;
656 int error;
657
658 error = dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds);
659 if (error != 0)
660 return (error);
661
662 error = dsl_destroy_head_check_impl(ds, 0);
663 dsl_dataset_rele(ds, FTAG);
664 return (error);
665}
666
667static void
668dsl_dir_destroy_sync(uint64_t ddobj, dmu_tx_t *tx)
669{
670 dsl_dir_t *dd;
671 dsl_pool_t *dp = dmu_tx_pool(tx);
672 objset_t *mos = dp->dp_meta_objset;
673 dd_used_t t;
674
675 ASSERT(RRW_WRITE_HELD(&dmu_tx_pool(tx)->dp_config_rwlock));
676
677 VERIFY0(dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd));
678
d683ddbb 679 ASSERT0(dsl_dir_phys(dd)->dd_head_dataset_obj);
13fe0198 680
788eb90c
JJ
681 /*
682 * Decrement the filesystem count for all parent filesystems.
683 *
684 * When we receive an incremental stream into a filesystem that already
685 * exists, a temporary clone is created. We never count this temporary
686 * clone, whose name begins with a '%'.
687 */
688 if (dd->dd_myname[0] != '%' && dd->dd_parent != NULL)
689 dsl_fs_ss_count_adjust(dd->dd_parent, -1,
690 DD_FIELD_FILESYSTEM_COUNT, tx);
691
13fe0198
MA
692 /*
693 * Remove our reservation. The impl() routine avoids setting the
694 * actual property, which would require the (already destroyed) ds.
695 */
696 dsl_dir_set_reservation_sync_impl(dd, 0, tx);
697
d683ddbb
JG
698 ASSERT0(dsl_dir_phys(dd)->dd_used_bytes);
699 ASSERT0(dsl_dir_phys(dd)->dd_reserved);
13fe0198 700 for (t = 0; t < DD_USED_NUM; t++)
d683ddbb 701 ASSERT0(dsl_dir_phys(dd)->dd_used_breakdown[t]);
13fe0198 702
d683ddbb
JG
703 VERIFY0(zap_destroy(mos, dsl_dir_phys(dd)->dd_child_dir_zapobj, tx));
704 VERIFY0(zap_destroy(mos, dsl_dir_phys(dd)->dd_props_zapobj, tx));
705 VERIFY0(dsl_deleg_destroy(mos, dsl_dir_phys(dd)->dd_deleg_zapobj, tx));
13fe0198 706 VERIFY0(zap_remove(mos,
d683ddbb
JG
707 dsl_dir_phys(dd->dd_parent)->dd_child_dir_zapobj,
708 dd->dd_myname, tx));
13fe0198
MA
709
710 dsl_dir_rele(dd, FTAG);
fa86b5db 711 dmu_object_free_zapified(mos, ddobj, tx);
13fe0198
MA
712}
713
714void
715dsl_destroy_head_sync_impl(dsl_dataset_t *ds, dmu_tx_t *tx)
716{
717 dsl_pool_t *dp = dmu_tx_pool(tx);
718 objset_t *mos = dp->dp_meta_objset;
719 uint64_t obj, ddobj, prevobj = 0;
720 boolean_t rmorigin;
13fe0198
MA
721 objset_t *os;
722
d683ddbb 723 ASSERT3U(dsl_dataset_phys(ds)->ds_num_children, <=, 1);
13fe0198 724 ASSERT(ds->ds_prev == NULL ||
d683ddbb
JG
725 dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj != ds->ds_object);
726 ASSERT3U(dsl_dataset_phys(ds)->ds_bp.blk_birth, <=, tx->tx_txg);
13fe0198
MA
727 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
728
729 /* We need to log before removing it from the namespace. */
730 spa_history_log_internal_ds(ds, "destroy", tx, "");
731
732 rmorigin = (dsl_dir_is_clone(ds->ds_dir) &&
733 DS_IS_DEFER_DESTROY(ds->ds_prev) &&
d683ddbb 734 dsl_dataset_phys(ds->ds_prev)->ds_num_children == 2 &&
13fe0198
MA
735 ds->ds_prev->ds_userrefs == 0);
736
9b67f605 737 /* Remove our reservation. */
13fe0198
MA
738 if (ds->ds_reserved != 0) {
739 dsl_dataset_set_refreservation_sync_impl(ds,
740 (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED),
741 0, tx);
742 ASSERT0(ds->ds_reserved);
743 }
744
f1512ee6
MA
745 if (ds->ds_large_blocks)
746 spa_feature_decr(dp->dp_spa, SPA_FEATURE_LARGE_BLOCKS, tx);
747
13fe0198
MA
748 dsl_scan_ds_destroyed(ds, tx);
749
750 obj = ds->ds_object;
751
d683ddbb 752 if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
13fe0198
MA
753 /* This is a clone */
754 ASSERT(ds->ds_prev != NULL);
d683ddbb
JG
755 ASSERT3U(dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj, !=,
756 obj);
757 ASSERT0(dsl_dataset_phys(ds)->ds_next_snap_obj);
13fe0198
MA
758
759 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
d683ddbb 760 if (dsl_dataset_phys(ds->ds_prev)->ds_next_clones_obj != 0) {
13fe0198
MA
761 dsl_dataset_remove_from_next_clones(ds->ds_prev,
762 obj, tx);
763 }
764
d683ddbb
JG
765 ASSERT3U(dsl_dataset_phys(ds->ds_prev)->ds_num_children, >, 1);
766 dsl_dataset_phys(ds->ds_prev)->ds_num_children--;
13fe0198
MA
767 }
768
13fe0198
MA
769 /*
770 * Destroy the deadlist. Unless it's a clone, the
771 * deadlist should be empty. (If it's a clone, it's
772 * safe to ignore the deadlist contents.)
773 */
774 dsl_deadlist_close(&ds->ds_deadlist);
d683ddbb 775 dsl_deadlist_free(mos, dsl_dataset_phys(ds)->ds_deadlist_obj, tx);
13fe0198 776 dmu_buf_will_dirty(ds->ds_dbuf, tx);
d683ddbb 777 dsl_dataset_phys(ds)->ds_deadlist_obj = 0;
13fe0198
MA
778
779 VERIFY0(dmu_objset_from_ds(ds, &os));
780
fa86b5db 781 if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_ASYNC_DESTROY)) {
13fe0198
MA
782 old_synchronous_dataset_destroy(ds, tx);
783 } else {
784 /*
785 * Move the bptree into the pool's list of trees to
786 * clean up and update space accounting information.
787 */
788 uint64_t used, comp, uncomp;
789
790 zil_destroy_sync(dmu_objset_zil(os), tx);
791
fa86b5db
MA
792 if (!spa_feature_is_active(dp->dp_spa,
793 SPA_FEATURE_ASYNC_DESTROY)) {
2696dfaf 794 dsl_scan_t *scn = dp->dp_scan;
fa86b5db
MA
795 spa_feature_incr(dp->dp_spa, SPA_FEATURE_ASYNC_DESTROY,
796 tx);
13fe0198
MA
797 dp->dp_bptree_obj = bptree_alloc(mos, tx);
798 VERIFY0(zap_add(mos,
799 DMU_POOL_DIRECTORY_OBJECT,
800 DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1,
801 &dp->dp_bptree_obj, tx));
2696dfaf
GW
802 ASSERT(!scn->scn_async_destroying);
803 scn->scn_async_destroying = B_TRUE;
13fe0198
MA
804 }
805
d683ddbb
JG
806 used = dsl_dir_phys(ds->ds_dir)->dd_used_bytes;
807 comp = dsl_dir_phys(ds->ds_dir)->dd_compressed_bytes;
808 uncomp = dsl_dir_phys(ds->ds_dir)->dd_uncompressed_bytes;
13fe0198
MA
809
810 ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) ||
d683ddbb 811 dsl_dataset_phys(ds)->ds_unique_bytes == used);
13fe0198
MA
812
813 bptree_add(mos, dp->dp_bptree_obj,
d683ddbb
JG
814 &dsl_dataset_phys(ds)->ds_bp,
815 dsl_dataset_phys(ds)->ds_prev_snap_txg,
13fe0198
MA
816 used, comp, uncomp, tx);
817 dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
818 -used, -comp, -uncomp, tx);
819 dsl_dir_diduse_space(dp->dp_free_dir, DD_USED_HEAD,
820 used, comp, uncomp, tx);
821 }
822
823 if (ds->ds_prev != NULL) {
824 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
825 VERIFY0(zap_remove_int(mos,
d683ddbb 826 dsl_dir_phys(ds->ds_prev->ds_dir)->dd_clones,
13fe0198
MA
827 ds->ds_object, tx));
828 }
829 prevobj = ds->ds_prev->ds_object;
830 dsl_dataset_rele(ds->ds_prev, ds);
831 ds->ds_prev = NULL;
832 }
833
834 /*
835 * This must be done after the dsl_traverse(), because it will
836 * re-open the objset.
837 */
838 if (ds->ds_objset) {
839 dmu_objset_evict(ds->ds_objset);
840 ds->ds_objset = NULL;
841 }
842
843 /* Erase the link in the dir */
844 dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
d683ddbb 845 dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj = 0;
13fe0198 846 ddobj = ds->ds_dir->dd_object;
d683ddbb
JG
847 ASSERT(dsl_dataset_phys(ds)->ds_snapnames_zapobj != 0);
848 VERIFY0(zap_destroy(mos,
849 dsl_dataset_phys(ds)->ds_snapnames_zapobj, tx));
13fe0198 850
da536844 851 if (ds->ds_bookmarks != 0) {
d683ddbb 852 VERIFY0(zap_destroy(mos, ds->ds_bookmarks, tx));
da536844
MA
853 spa_feature_decr(dp->dp_spa, SPA_FEATURE_BOOKMARKS, tx);
854 }
855
13fe0198
MA
856 spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
857
d683ddbb
JG
858 ASSERT0(dsl_dataset_phys(ds)->ds_next_clones_obj);
859 ASSERT0(dsl_dataset_phys(ds)->ds_props_obj);
860 ASSERT0(dsl_dataset_phys(ds)->ds_userrefs_obj);
13fe0198
MA
861 dsl_dir_rele(ds->ds_dir, ds);
862 ds->ds_dir = NULL;
fa86b5db 863 dmu_object_free_zapified(mos, obj, tx);
13fe0198
MA
864
865 dsl_dir_destroy_sync(ddobj, tx);
866
867 if (rmorigin) {
868 dsl_dataset_t *prev;
869 VERIFY0(dsl_dataset_hold_obj(dp, prevobj, FTAG, &prev));
870 dsl_destroy_snapshot_sync_impl(prev, B_FALSE, tx);
871 dsl_dataset_rele(prev, FTAG);
872 }
873}
874
875static void
876dsl_destroy_head_sync(void *arg, dmu_tx_t *tx)
877{
878 dsl_destroy_head_arg_t *ddha = arg;
879 dsl_pool_t *dp = dmu_tx_pool(tx);
880 dsl_dataset_t *ds;
881
882 VERIFY0(dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds));
883 dsl_destroy_head_sync_impl(ds, tx);
884 dsl_dataset_rele(ds, FTAG);
885}
886
887static void
888dsl_destroy_head_begin_sync(void *arg, dmu_tx_t *tx)
889{
890 dsl_destroy_head_arg_t *ddha = arg;
891 dsl_pool_t *dp = dmu_tx_pool(tx);
892 dsl_dataset_t *ds;
893
894 VERIFY0(dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds));
895
896 /* Mark it as inconsistent on-disk, in case we crash */
897 dmu_buf_will_dirty(ds->ds_dbuf, tx);
d683ddbb 898 dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_INCONSISTENT;
13fe0198
MA
899
900 spa_history_log_internal_ds(ds, "destroy begin", tx, "");
901 dsl_dataset_rele(ds, FTAG);
902}
903
904int
905dsl_destroy_head(const char *name)
906{
907 dsl_destroy_head_arg_t ddha;
908 int error;
909 spa_t *spa;
910 boolean_t isenabled;
911
912#ifdef _KERNEL
913 zfs_destroy_unmount_origin(name);
914#endif
915
916 error = spa_open(name, &spa, FTAG);
917 if (error != 0)
918 return (error);
fa86b5db 919 isenabled = spa_feature_is_enabled(spa, SPA_FEATURE_ASYNC_DESTROY);
13fe0198
MA
920 spa_close(spa, FTAG);
921
922 ddha.ddha_name = name;
923
924 if (!isenabled) {
925 objset_t *os;
926
927 error = dsl_sync_task(name, dsl_destroy_head_check,
3d45fdd6
MA
928 dsl_destroy_head_begin_sync, &ddha,
929 0, ZFS_SPACE_CHECK_NONE);
13fe0198
MA
930 if (error != 0)
931 return (error);
932
933 /*
934 * Head deletion is processed in one txg on old pools;
935 * remove the objects from open context so that the txg sync
936 * is not too long.
937 */
938 error = dmu_objset_own(name, DMU_OST_ANY, B_FALSE, FTAG, &os);
939 if (error == 0) {
940 uint64_t obj;
941 uint64_t prev_snap_txg =
d683ddbb
JG
942 dsl_dataset_phys(dmu_objset_ds(os))->
943 ds_prev_snap_txg;
13fe0198
MA
944 for (obj = 0; error == 0;
945 error = dmu_object_next(os, &obj, FALSE,
946 prev_snap_txg))
b663a23d 947 (void) dmu_free_long_object(os, obj);
13fe0198
MA
948 /* sync out all frees */
949 txg_wait_synced(dmu_objset_pool(os), 0);
950 dmu_objset_disown(os, FTAG);
951 }
952 }
953
954 return (dsl_sync_task(name, dsl_destroy_head_check,
3d45fdd6 955 dsl_destroy_head_sync, &ddha, 0, ZFS_SPACE_CHECK_NONE));
13fe0198
MA
956}
957
958/*
959 * Note, this function is used as the callback for dmu_objset_find(). We
960 * always return 0 so that we will continue to find and process
961 * inconsistent datasets, even if we encounter an error trying to
962 * process one of them.
963 */
964/* ARGSUSED */
965int
966dsl_destroy_inconsistent(const char *dsname, void *arg)
967{
968 objset_t *os;
969
970 if (dmu_objset_hold(dsname, FTAG, &os) == 0) {
971 boolean_t inconsistent = DS_IS_INCONSISTENT(dmu_objset_ds(os));
972 dmu_objset_rele(os, FTAG);
973 if (inconsistent)
974 (void) dsl_destroy_head(dsname);
975 }
976 return (0);
977}
978
979
980#if defined(_KERNEL) && defined(HAVE_SPL)
981EXPORT_SYMBOL(dsl_destroy_head);
982EXPORT_SYMBOL(dsl_destroy_head_sync_impl);
983EXPORT_SYMBOL(dsl_dataset_user_hold_check_one);
984EXPORT_SYMBOL(dsl_destroy_snapshot_sync_impl);
985EXPORT_SYMBOL(dsl_destroy_inconsistent);
986EXPORT_SYMBOL(dsl_dataset_user_release_tmp);
987EXPORT_SYMBOL(dsl_destroy_head_check_impl);
988#endif