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