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