xref: /illumos-gate/usr/src/uts/common/fs/zfs/dsl_destroy.c (revision 5d7b4d438c4a51eccc95e77a83a437b4d48380eb)
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.
23  * Copyright (c) 2013 by Delphix. All rights reserved.
24  * Copyright (c) 2013 Steven Hartland. All rights reserved.
25  * Copyright (c) 2013 by Joyent, Inc. All rights reserved.
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>
42 #include <sys/dmu_impl.h>
43 
44 typedef 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 
51 int
52 dsl_destroy_snapshot_check_impl(dsl_dataset_t *ds, boolean_t defer)
53 {
54 	if (!dsl_dataset_is_snapshot(ds))
55 		return (SET_ERROR(EINVAL));
56 
57 	if (dsl_dataset_long_held(ds))
58 		return (SET_ERROR(EBUSY));
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)
67 			return (SET_ERROR(ENOTSUP));
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)
76 		return (SET_ERROR(EBUSY));
77 
78 	/*
79 	 * Can't delete a branch point.
80 	 */
81 	if (ds->ds_phys->ds_num_children > 1)
82 		return (SET_ERROR(EEXIST));
83 
84 	return (0);
85 }
86 
87 static int
88 dsl_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));
130 
131 	return (0);
132 }
133 
134 struct 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 
142 static int
143 process_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 
148 	ASSERT(!BP_IS_HOLE(bp));
149 
150 	if (bp->blk_birth <= poa->ds->ds_phys->ds_prev_snap_txg) {
151 		dsl_deadlist_insert(&poa->ds->ds_deadlist, bp, tx);
152 		if (poa->ds_prev && !poa->after_branch_point &&
153 		    bp->blk_birth >
154 		    poa->ds_prev->ds_phys->ds_prev_snap_txg) {
155 			poa->ds_prev->ds_phys->ds_unique_bytes +=
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 
167 static void
168 process_old_deadlist(dsl_dataset_t *ds, dsl_dataset_t *ds_prev,
169     dsl_dataset_t *ds_next, boolean_t after_branch_point, dmu_tx_t *tx)
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));
186 	ASSERT3U(poa.used, ==, ds->ds_phys->ds_unique_bytes);
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);
195 	deadlist_obj = ds->ds_phys->ds_deadlist_obj;
196 	ds->ds_phys->ds_deadlist_obj = ds_next->ds_phys->ds_deadlist_obj;
197 	ds_next->ds_phys->ds_deadlist_obj = deadlist_obj;
198 	dsl_deadlist_open(&ds->ds_deadlist, mos, ds->ds_phys->ds_deadlist_obj);
199 	dsl_deadlist_open(&ds_next->ds_deadlist, mos,
200 	    ds_next->ds_phys->ds_deadlist_obj);
201 }
202 
203 static void
204 dsl_dataset_remove_clones_key(dsl_dataset_t *ds, uint64_t mintxg, dmu_tx_t *tx)
205 {
206 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
207 	zap_cursor_t zc;
208 	zap_attribute_t za;
209 
210 	/*
211 	 * If it is the old version, dd_clones doesn't exist so we can't
212 	 * find the clones, but dsl_deadlist_remove_key() is a no-op so it
213 	 * doesn't matter.
214 	 */
215 	if (ds->ds_dir->dd_phys->dd_clones == 0)
216 		return;
217 
218 	for (zap_cursor_init(&zc, mos, ds->ds_dir->dd_phys->dd_clones);
219 	    zap_cursor_retrieve(&zc, &za) == 0;
220 	    zap_cursor_advance(&zc)) {
221 		dsl_dataset_t *clone;
222 
223 		VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
224 		    za.za_first_integer, FTAG, &clone));
225 		if (clone->ds_dir->dd_origin_txg > mintxg) {
226 			dsl_deadlist_remove_key(&clone->ds_deadlist,
227 			    mintxg, tx);
228 			dsl_dataset_remove_clones_key(clone, mintxg, tx);
229 		}
230 		dsl_dataset_rele(clone, FTAG);
231 	}
232 	zap_cursor_fini(&zc);
233 }
234 
235 void
236 dsl_destroy_snapshot_sync_impl(dsl_dataset_t *ds, boolean_t defer, dmu_tx_t *tx)
237 {
238 	int err;
239 	int after_branch_point = FALSE;
240 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
241 	objset_t *mos = dp->dp_meta_objset;
242 	dsl_dataset_t *ds_prev = NULL;
243 	uint64_t obj;
244 
245 	ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
246 	ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg);
247 	ASSERT(refcount_is_zero(&ds->ds_longholds));
248 
249 	if (defer &&
250 	    (ds->ds_userrefs > 0 || ds->ds_phys->ds_num_children > 1)) {
251 		ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
252 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
253 		ds->ds_phys->ds_flags |= DS_FLAG_DEFER_DESTROY;
254 		spa_history_log_internal_ds(ds, "defer_destroy", tx, "");
255 		return;
256 	}
257 
258 	ASSERT3U(ds->ds_phys->ds_num_children, <=, 1);
259 
260 	/* We need to log before removing it from the namespace. */
261 	spa_history_log_internal_ds(ds, "destroy", tx, "");
262 
263 	dsl_scan_ds_destroyed(ds, tx);
264 
265 	obj = ds->ds_object;
266 
267 	if (ds->ds_phys->ds_prev_snap_obj != 0) {
268 		ASSERT3P(ds->ds_prev, ==, NULL);
269 		VERIFY0(dsl_dataset_hold_obj(dp,
270 		    ds->ds_phys->ds_prev_snap_obj, FTAG, &ds_prev));
271 		after_branch_point =
272 		    (ds_prev->ds_phys->ds_next_snap_obj != obj);
273 
274 		dmu_buf_will_dirty(ds_prev->ds_dbuf, tx);
275 		if (after_branch_point &&
276 		    ds_prev->ds_phys->ds_next_clones_obj != 0) {
277 			dsl_dataset_remove_from_next_clones(ds_prev, obj, tx);
278 			if (ds->ds_phys->ds_next_snap_obj != 0) {
279 				VERIFY0(zap_add_int(mos,
280 				    ds_prev->ds_phys->ds_next_clones_obj,
281 				    ds->ds_phys->ds_next_snap_obj, tx));
282 			}
283 		}
284 		if (!after_branch_point) {
285 			ds_prev->ds_phys->ds_next_snap_obj =
286 			    ds->ds_phys->ds_next_snap_obj;
287 		}
288 	}
289 
290 	dsl_dataset_t *ds_next;
291 	uint64_t old_unique;
292 	uint64_t used = 0, comp = 0, uncomp = 0;
293 
294 	VERIFY0(dsl_dataset_hold_obj(dp,
295 	    ds->ds_phys->ds_next_snap_obj, FTAG, &ds_next));
296 	ASSERT3U(ds_next->ds_phys->ds_prev_snap_obj, ==, obj);
297 
298 	old_unique = ds_next->ds_phys->ds_unique_bytes;
299 
300 	dmu_buf_will_dirty(ds_next->ds_dbuf, tx);
301 	ds_next->ds_phys->ds_prev_snap_obj =
302 	    ds->ds_phys->ds_prev_snap_obj;
303 	ds_next->ds_phys->ds_prev_snap_txg =
304 	    ds->ds_phys->ds_prev_snap_txg;
305 	ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
306 	    ds_prev ? ds_prev->ds_phys->ds_creation_txg : 0);
307 
308 	if (ds_next->ds_deadlist.dl_oldfmt) {
309 		process_old_deadlist(ds, ds_prev, ds_next,
310 		    after_branch_point, tx);
311 	} else {
312 		/* Adjust prev's unique space. */
313 		if (ds_prev && !after_branch_point) {
314 			dsl_deadlist_space_range(&ds_next->ds_deadlist,
315 			    ds_prev->ds_phys->ds_prev_snap_txg,
316 			    ds->ds_phys->ds_prev_snap_txg,
317 			    &used, &comp, &uncomp);
318 			ds_prev->ds_phys->ds_unique_bytes += used;
319 		}
320 
321 		/* Adjust snapused. */
322 		dsl_deadlist_space_range(&ds_next->ds_deadlist,
323 		    ds->ds_phys->ds_prev_snap_txg, UINT64_MAX,
324 		    &used, &comp, &uncomp);
325 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
326 		    -used, -comp, -uncomp, tx);
327 
328 		/* Move blocks to be freed to pool's free list. */
329 		dsl_deadlist_move_bpobj(&ds_next->ds_deadlist,
330 		    &dp->dp_free_bpobj, ds->ds_phys->ds_prev_snap_txg,
331 		    tx);
332 		dsl_dir_diduse_space(tx->tx_pool->dp_free_dir,
333 		    DD_USED_HEAD, used, comp, uncomp, tx);
334 
335 		/* Merge our deadlist into next's and free it. */
336 		dsl_deadlist_merge(&ds_next->ds_deadlist,
337 		    ds->ds_phys->ds_deadlist_obj, tx);
338 	}
339 	dsl_deadlist_close(&ds->ds_deadlist);
340 	dsl_deadlist_free(mos, ds->ds_phys->ds_deadlist_obj, tx);
341 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
342 	ds->ds_phys->ds_deadlist_obj = 0;
343 
344 	/* Collapse range in clone heads */
345 	dsl_dataset_remove_clones_key(ds,
346 	    ds->ds_phys->ds_creation_txg, tx);
347 
348 	if (dsl_dataset_is_snapshot(ds_next)) {
349 		dsl_dataset_t *ds_nextnext;
350 
351 		/*
352 		 * Update next's unique to include blocks which
353 		 * were previously shared by only this snapshot
354 		 * and it.  Those blocks will be born after the
355 		 * prev snap and before this snap, and will have
356 		 * died after the next snap and before the one
357 		 * after that (ie. be on the snap after next's
358 		 * deadlist).
359 		 */
360 		VERIFY0(dsl_dataset_hold_obj(dp,
361 		    ds_next->ds_phys->ds_next_snap_obj, FTAG, &ds_nextnext));
362 		dsl_deadlist_space_range(&ds_nextnext->ds_deadlist,
363 		    ds->ds_phys->ds_prev_snap_txg,
364 		    ds->ds_phys->ds_creation_txg,
365 		    &used, &comp, &uncomp);
366 		ds_next->ds_phys->ds_unique_bytes += used;
367 		dsl_dataset_rele(ds_nextnext, FTAG);
368 		ASSERT3P(ds_next->ds_prev, ==, NULL);
369 
370 		/* Collapse range in this head. */
371 		dsl_dataset_t *hds;
372 		VERIFY0(dsl_dataset_hold_obj(dp,
373 		    ds->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &hds));
374 		dsl_deadlist_remove_key(&hds->ds_deadlist,
375 		    ds->ds_phys->ds_creation_txg, tx);
376 		dsl_dataset_rele(hds, FTAG);
377 
378 	} else {
379 		ASSERT3P(ds_next->ds_prev, ==, ds);
380 		dsl_dataset_rele(ds_next->ds_prev, ds_next);
381 		ds_next->ds_prev = NULL;
382 		if (ds_prev) {
383 			VERIFY0(dsl_dataset_hold_obj(dp,
384 			    ds->ds_phys->ds_prev_snap_obj,
385 			    ds_next, &ds_next->ds_prev));
386 		}
387 
388 		dsl_dataset_recalc_head_uniq(ds_next);
389 
390 		/*
391 		 * Reduce the amount of our unconsumed refreservation
392 		 * being charged to our parent by the amount of
393 		 * new unique data we have gained.
394 		 */
395 		if (old_unique < ds_next->ds_reserved) {
396 			int64_t mrsdelta;
397 			uint64_t new_unique =
398 			    ds_next->ds_phys->ds_unique_bytes;
399 
400 			ASSERT(old_unique <= new_unique);
401 			mrsdelta = MIN(new_unique - old_unique,
402 			    ds_next->ds_reserved - old_unique);
403 			dsl_dir_diduse_space(ds->ds_dir,
404 			    DD_USED_REFRSRV, -mrsdelta, 0, 0, tx);
405 		}
406 	}
407 	dsl_dataset_rele(ds_next, FTAG);
408 
409 	/*
410 	 * This must be done after the dsl_traverse(), because it will
411 	 * re-open the objset.
412 	 */
413 	if (ds->ds_objset) {
414 		dmu_objset_evict(ds->ds_objset);
415 		ds->ds_objset = NULL;
416 	}
417 
418 	/* remove from snapshot namespace */
419 	dsl_dataset_t *ds_head;
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, B_TRUE));
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 		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 	dmu_object_free_zapified(mos, obj, tx);
456 }
457 
458 static void
459 dsl_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  */
490 int
491 dsl_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;
503 	dsda.dsda_successful_snaps = fnvlist_alloc();
504 	dsda.dsda_defer = defer;
505 	dsda.dsda_errlist = errlist;
506 
507 	error = dsl_sync_task(nvpair_name(pair),
508 	    dsl_destroy_snapshot_check, dsl_destroy_snapshot_sync,
509 	    &dsda, 0);
510 	fnvlist_free(dsda.dsda_successful_snaps);
511 
512 	return (error);
513 }
514 
515 int
516 dsl_destroy_snapshot(const char *name, boolean_t defer)
517 {
518 	int error;
519 	nvlist_t *nvl = fnvlist_alloc();
520 	nvlist_t *errlist = fnvlist_alloc();
521 
522 	fnvlist_add_boolean(nvl, name);
523 	error = dsl_destroy_snapshots_nvl(nvl, defer, errlist);
524 	fnvlist_free(errlist);
525 	fnvlist_free(nvl);
526 	return (error);
527 }
528 
529 struct killarg {
530 	dsl_dataset_t *ds;
531 	dmu_tx_t *tx;
532 };
533 
534 /* ARGSUSED */
535 static int
536 kill_blkptr(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
537     const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
538 {
539 	struct killarg *ka = arg;
540 	dmu_tx_t *tx = ka->tx;
541 
542 	if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
543 		return (0);
544 
545 	if (zb->zb_level == ZB_ZIL_LEVEL) {
546 		ASSERT(zilog != NULL);
547 		/*
548 		 * It's a block in the intent log.  It has no
549 		 * accounting, so just free it.
550 		 */
551 		dsl_free(ka->tx->tx_pool, ka->tx->tx_txg, bp);
552 	} else {
553 		ASSERT(zilog == NULL);
554 		ASSERT3U(bp->blk_birth, >, ka->ds->ds_phys->ds_prev_snap_txg);
555 		(void) dsl_dataset_block_kill(ka->ds, bp, tx, B_FALSE);
556 	}
557 
558 	return (0);
559 }
560 
561 static void
562 old_synchronous_dataset_destroy(dsl_dataset_t *ds, dmu_tx_t *tx)
563 {
564 	struct killarg ka;
565 
566 	/*
567 	 * Free everything that we point to (that's born after
568 	 * the previous snapshot, if we are a clone)
569 	 *
570 	 * NB: this should be very quick, because we already
571 	 * freed all the objects in open context.
572 	 */
573 	ka.ds = ds;
574 	ka.tx = tx;
575 	VERIFY0(traverse_dataset(ds,
576 	    ds->ds_phys->ds_prev_snap_txg, TRAVERSE_POST,
577 	    kill_blkptr, &ka));
578 	ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) || ds->ds_phys->ds_unique_bytes == 0);
579 }
580 
581 typedef struct dsl_destroy_head_arg {
582 	const char *ddha_name;
583 } dsl_destroy_head_arg_t;
584 
585 int
586 dsl_destroy_head_check_impl(dsl_dataset_t *ds, int expected_holds)
587 {
588 	int error;
589 	uint64_t count;
590 	objset_t *mos;
591 
592 	ASSERT(!dsl_dataset_is_snapshot(ds));
593 	if (dsl_dataset_is_snapshot(ds))
594 		return (SET_ERROR(EINVAL));
595 
596 	if (refcount_count(&ds->ds_longholds) != expected_holds)
597 		return (SET_ERROR(EBUSY));
598 
599 	mos = ds->ds_dir->dd_pool->dp_meta_objset;
600 
601 	/*
602 	 * Can't delete a head dataset if there are snapshots of it.
603 	 * (Except if the only snapshots are from the branch we cloned
604 	 * from.)
605 	 */
606 	if (ds->ds_prev != NULL &&
607 	    ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object)
608 		return (SET_ERROR(EBUSY));
609 
610 	/*
611 	 * Can't delete if there are children of this fs.
612 	 */
613 	error = zap_count(mos,
614 	    ds->ds_dir->dd_phys->dd_child_dir_zapobj, &count);
615 	if (error != 0)
616 		return (error);
617 	if (count != 0)
618 		return (SET_ERROR(EEXIST));
619 
620 	if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev) &&
621 	    ds->ds_prev->ds_phys->ds_num_children == 2 &&
622 	    ds->ds_prev->ds_userrefs == 0) {
623 		/* We need to remove the origin snapshot as well. */
624 		if (!refcount_is_zero(&ds->ds_prev->ds_longholds))
625 			return (SET_ERROR(EBUSY));
626 	}
627 	return (0);
628 }
629 
630 static int
631 dsl_destroy_head_check(void *arg, dmu_tx_t *tx)
632 {
633 	dsl_destroy_head_arg_t *ddha = arg;
634 	dsl_pool_t *dp = dmu_tx_pool(tx);
635 	dsl_dataset_t *ds;
636 	int error;
637 
638 	error = dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds);
639 	if (error != 0)
640 		return (error);
641 
642 	error = dsl_destroy_head_check_impl(ds, 0);
643 	dsl_dataset_rele(ds, FTAG);
644 	return (error);
645 }
646 
647 static void
648 dsl_dir_destroy_sync(uint64_t ddobj, dmu_tx_t *tx)
649 {
650 	dsl_dir_t *dd;
651 	dsl_pool_t *dp = dmu_tx_pool(tx);
652 	objset_t *mos = dp->dp_meta_objset;
653 	dd_used_t t;
654 
655 	ASSERT(RRW_WRITE_HELD(&dmu_tx_pool(tx)->dp_config_rwlock));
656 
657 	VERIFY0(dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd));
658 
659 	ASSERT0(dd->dd_phys->dd_head_dataset_obj);
660 
661 	/*
662 	 * Decrement the filesystem count for all parent filesystems.
663 	 *
664 	 * When we receive an incremental stream into a filesystem that already
665 	 * exists, a temporary clone is created.  We never count this temporary
666 	 * clone, whose name begins with a '%'.
667 	 */
668 	if (dd->dd_myname[0] != '%' && dd->dd_parent != NULL)
669 		dsl_fs_ss_count_adjust(dd->dd_parent, -1,
670 		    DD_FIELD_FILESYSTEM_COUNT, tx);
671 
672 	/*
673 	 * Remove our reservation. The impl() routine avoids setting the
674 	 * actual property, which would require the (already destroyed) ds.
675 	 */
676 	dsl_dir_set_reservation_sync_impl(dd, 0, tx);
677 
678 	ASSERT0(dd->dd_phys->dd_used_bytes);
679 	ASSERT0(dd->dd_phys->dd_reserved);
680 	for (t = 0; t < DD_USED_NUM; t++)
681 		ASSERT0(dd->dd_phys->dd_used_breakdown[t]);
682 
683 	VERIFY0(zap_destroy(mos, dd->dd_phys->dd_child_dir_zapobj, tx));
684 	VERIFY0(zap_destroy(mos, dd->dd_phys->dd_props_zapobj, tx));
685 	VERIFY0(dsl_deleg_destroy(mos, dd->dd_phys->dd_deleg_zapobj, tx));
686 	VERIFY0(zap_remove(mos,
687 	    dd->dd_parent->dd_phys->dd_child_dir_zapobj, dd->dd_myname, tx));
688 
689 	dsl_dir_rele(dd, FTAG);
690 	dmu_object_free_zapified(mos, ddobj, tx);
691 }
692 
693 void
694 dsl_destroy_head_sync_impl(dsl_dataset_t *ds, dmu_tx_t *tx)
695 {
696 	dsl_pool_t *dp = dmu_tx_pool(tx);
697 	objset_t *mos = dp->dp_meta_objset;
698 	uint64_t obj, ddobj, prevobj = 0;
699 	boolean_t rmorigin;
700 
701 	ASSERT3U(ds->ds_phys->ds_num_children, <=, 1);
702 	ASSERT(ds->ds_prev == NULL ||
703 	    ds->ds_prev->ds_phys->ds_next_snap_obj != ds->ds_object);
704 	ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg);
705 	ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
706 
707 	/* We need to log before removing it from the namespace. */
708 	spa_history_log_internal_ds(ds, "destroy", tx, "");
709 
710 	rmorigin = (dsl_dir_is_clone(ds->ds_dir) &&
711 	    DS_IS_DEFER_DESTROY(ds->ds_prev) &&
712 	    ds->ds_prev->ds_phys->ds_num_children == 2 &&
713 	    ds->ds_prev->ds_userrefs == 0);
714 
715 	/* Remove our reservation. */
716 	if (ds->ds_reserved != 0) {
717 		dsl_dataset_set_refreservation_sync_impl(ds,
718 		    (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED),
719 		    0, tx);
720 		ASSERT0(ds->ds_reserved);
721 	}
722 
723 	dsl_scan_ds_destroyed(ds, tx);
724 
725 	obj = ds->ds_object;
726 
727 	if (ds->ds_phys->ds_prev_snap_obj != 0) {
728 		/* This is a clone */
729 		ASSERT(ds->ds_prev != NULL);
730 		ASSERT3U(ds->ds_prev->ds_phys->ds_next_snap_obj, !=, obj);
731 		ASSERT0(ds->ds_phys->ds_next_snap_obj);
732 
733 		dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
734 		if (ds->ds_prev->ds_phys->ds_next_clones_obj != 0) {
735 			dsl_dataset_remove_from_next_clones(ds->ds_prev,
736 			    obj, tx);
737 		}
738 
739 		ASSERT3U(ds->ds_prev->ds_phys->ds_num_children, >, 1);
740 		ds->ds_prev->ds_phys->ds_num_children--;
741 	}
742 
743 	/*
744 	 * Destroy the deadlist.  Unless it's a clone, the
745 	 * deadlist should be empty.  (If it's a clone, it's
746 	 * safe to ignore the deadlist contents.)
747 	 */
748 	dsl_deadlist_close(&ds->ds_deadlist);
749 	dsl_deadlist_free(mos, ds->ds_phys->ds_deadlist_obj, tx);
750 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
751 	ds->ds_phys->ds_deadlist_obj = 0;
752 
753 	objset_t *os;
754 	VERIFY0(dmu_objset_from_ds(ds, &os));
755 
756 	if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_ASYNC_DESTROY)) {
757 		old_synchronous_dataset_destroy(ds, tx);
758 	} else {
759 		/*
760 		 * Move the bptree into the pool's list of trees to
761 		 * clean up and update space accounting information.
762 		 */
763 		uint64_t used, comp, uncomp;
764 
765 		zil_destroy_sync(dmu_objset_zil(os), tx);
766 
767 		if (!spa_feature_is_active(dp->dp_spa,
768 		    SPA_FEATURE_ASYNC_DESTROY)) {
769 			dsl_scan_t *scn = dp->dp_scan;
770 			spa_feature_incr(dp->dp_spa, SPA_FEATURE_ASYNC_DESTROY,
771 			    tx);
772 			dp->dp_bptree_obj = bptree_alloc(mos, tx);
773 			VERIFY0(zap_add(mos,
774 			    DMU_POOL_DIRECTORY_OBJECT,
775 			    DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1,
776 			    &dp->dp_bptree_obj, tx));
777 			ASSERT(!scn->scn_async_destroying);
778 			scn->scn_async_destroying = B_TRUE;
779 		}
780 
781 		used = ds->ds_dir->dd_phys->dd_used_bytes;
782 		comp = ds->ds_dir->dd_phys->dd_compressed_bytes;
783 		uncomp = ds->ds_dir->dd_phys->dd_uncompressed_bytes;
784 
785 		ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) ||
786 		    ds->ds_phys->ds_unique_bytes == used);
787 
788 		bptree_add(mos, dp->dp_bptree_obj,
789 		    &ds->ds_phys->ds_bp, ds->ds_phys->ds_prev_snap_txg,
790 		    used, comp, uncomp, tx);
791 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
792 		    -used, -comp, -uncomp, tx);
793 		dsl_dir_diduse_space(dp->dp_free_dir, DD_USED_HEAD,
794 		    used, comp, uncomp, tx);
795 	}
796 
797 	if (ds->ds_prev != NULL) {
798 		if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
799 			VERIFY0(zap_remove_int(mos,
800 			    ds->ds_prev->ds_dir->dd_phys->dd_clones,
801 			    ds->ds_object, tx));
802 		}
803 		prevobj = ds->ds_prev->ds_object;
804 		dsl_dataset_rele(ds->ds_prev, ds);
805 		ds->ds_prev = NULL;
806 	}
807 
808 	/*
809 	 * This must be done after the dsl_traverse(), because it will
810 	 * re-open the objset.
811 	 */
812 	if (ds->ds_objset) {
813 		dmu_objset_evict(ds->ds_objset);
814 		ds->ds_objset = NULL;
815 	}
816 
817 	/* Erase the link in the dir */
818 	dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
819 	ds->ds_dir->dd_phys->dd_head_dataset_obj = 0;
820 	ddobj = ds->ds_dir->dd_object;
821 	ASSERT(ds->ds_phys->ds_snapnames_zapobj != 0);
822 	VERIFY0(zap_destroy(mos, ds->ds_phys->ds_snapnames_zapobj, tx));
823 
824 	if (ds->ds_bookmarks != 0) {
825 		VERIFY0(zap_destroy(mos,
826 		    ds->ds_bookmarks, tx));
827 		spa_feature_decr(dp->dp_spa, SPA_FEATURE_BOOKMARKS, tx);
828 	}
829 
830 	spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
831 
832 	ASSERT0(ds->ds_phys->ds_next_clones_obj);
833 	ASSERT0(ds->ds_phys->ds_props_obj);
834 	ASSERT0(ds->ds_phys->ds_userrefs_obj);
835 	dsl_dir_rele(ds->ds_dir, ds);
836 	ds->ds_dir = NULL;
837 	dmu_object_free_zapified(mos, obj, tx);
838 
839 	dsl_dir_destroy_sync(ddobj, tx);
840 
841 	if (rmorigin) {
842 		dsl_dataset_t *prev;
843 		VERIFY0(dsl_dataset_hold_obj(dp, prevobj, FTAG, &prev));
844 		dsl_destroy_snapshot_sync_impl(prev, B_FALSE, tx);
845 		dsl_dataset_rele(prev, FTAG);
846 	}
847 }
848 
849 static void
850 dsl_destroy_head_sync(void *arg, dmu_tx_t *tx)
851 {
852 	dsl_destroy_head_arg_t *ddha = arg;
853 	dsl_pool_t *dp = dmu_tx_pool(tx);
854 	dsl_dataset_t *ds;
855 
856 	VERIFY0(dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds));
857 	dsl_destroy_head_sync_impl(ds, tx);
858 	dsl_dataset_rele(ds, FTAG);
859 }
860 
861 static void
862 dsl_destroy_head_begin_sync(void *arg, dmu_tx_t *tx)
863 {
864 	dsl_destroy_head_arg_t *ddha = arg;
865 	dsl_pool_t *dp = dmu_tx_pool(tx);
866 	dsl_dataset_t *ds;
867 
868 	VERIFY0(dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds));
869 
870 	/* Mark it as inconsistent on-disk, in case we crash */
871 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
872 	ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT;
873 
874 	spa_history_log_internal_ds(ds, "destroy begin", tx, "");
875 	dsl_dataset_rele(ds, FTAG);
876 }
877 
878 int
879 dsl_destroy_head(const char *name)
880 {
881 	dsl_destroy_head_arg_t ddha;
882 	int error;
883 	spa_t *spa;
884 	boolean_t isenabled;
885 
886 #ifdef _KERNEL
887 	zfs_destroy_unmount_origin(name);
888 #endif
889 
890 	error = spa_open(name, &spa, FTAG);
891 	if (error != 0)
892 		return (error);
893 	isenabled = spa_feature_is_enabled(spa, SPA_FEATURE_ASYNC_DESTROY);
894 	spa_close(spa, FTAG);
895 
896 	ddha.ddha_name = name;
897 
898 	if (!isenabled) {
899 		objset_t *os;
900 
901 		error = dsl_sync_task(name, dsl_destroy_head_check,
902 		    dsl_destroy_head_begin_sync, &ddha, 0);
903 		if (error != 0)
904 			return (error);
905 
906 		/*
907 		 * Head deletion is processed in one txg on old pools;
908 		 * remove the objects from open context so that the txg sync
909 		 * is not too long.
910 		 */
911 		error = dmu_objset_own(name, DMU_OST_ANY, B_FALSE, FTAG, &os);
912 		if (error == 0) {
913 			uint64_t prev_snap_txg =
914 			    dmu_objset_ds(os)->ds_phys->ds_prev_snap_txg;
915 			for (uint64_t obj = 0; error == 0;
916 			    error = dmu_object_next(os, &obj, FALSE,
917 			    prev_snap_txg))
918 				(void) dmu_free_long_object(os, obj);
919 			/* sync out all frees */
920 			txg_wait_synced(dmu_objset_pool(os), 0);
921 			dmu_objset_disown(os, FTAG);
922 		}
923 	}
924 
925 	return (dsl_sync_task(name, dsl_destroy_head_check,
926 	    dsl_destroy_head_sync, &ddha, 0));
927 }
928 
929 /*
930  * Note, this function is used as the callback for dmu_objset_find().  We
931  * always return 0 so that we will continue to find and process
932  * inconsistent datasets, even if we encounter an error trying to
933  * process one of them.
934  */
935 /* ARGSUSED */
936 int
937 dsl_destroy_inconsistent(const char *dsname, void *arg)
938 {
939 	objset_t *os;
940 
941 	if (dmu_objset_hold(dsname, FTAG, &os) == 0) {
942 		boolean_t inconsistent = DS_IS_INCONSISTENT(dmu_objset_ds(os));
943 		dmu_objset_rele(os, FTAG);
944 		if (inconsistent)
945 			(void) dsl_destroy_head(dsname);
946 	}
947 	return (0);
948 }
949