xref: /titanic_50/usr/src/uts/common/fs/zfs/dsl_destroy.c (revision e1d3217b9afde782c4d3e946fda0e6ef36a61306)
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))
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 	if (dsl_dataset_is_snapshot(ds))
593 		return (SET_ERROR(EINVAL));
594 
595 	if (refcount_count(&ds->ds_longholds) != expected_holds)
596 		return (SET_ERROR(EBUSY));
597 
598 	mos = ds->ds_dir->dd_pool->dp_meta_objset;
599 
600 	/*
601 	 * Can't delete a head dataset if there are snapshots of it.
602 	 * (Except if the only snapshots are from the branch we cloned
603 	 * from.)
604 	 */
605 	if (ds->ds_prev != NULL &&
606 	    ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object)
607 		return (SET_ERROR(EBUSY));
608 
609 	/*
610 	 * Can't delete if there are children of this fs.
611 	 */
612 	error = zap_count(mos,
613 	    ds->ds_dir->dd_phys->dd_child_dir_zapobj, &count);
614 	if (error != 0)
615 		return (error);
616 	if (count != 0)
617 		return (SET_ERROR(EEXIST));
618 
619 	if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev) &&
620 	    ds->ds_prev->ds_phys->ds_num_children == 2 &&
621 	    ds->ds_prev->ds_userrefs == 0) {
622 		/* We need to remove the origin snapshot as well. */
623 		if (!refcount_is_zero(&ds->ds_prev->ds_longholds))
624 			return (SET_ERROR(EBUSY));
625 	}
626 	return (0);
627 }
628 
629 static int
630 dsl_destroy_head_check(void *arg, dmu_tx_t *tx)
631 {
632 	dsl_destroy_head_arg_t *ddha = arg;
633 	dsl_pool_t *dp = dmu_tx_pool(tx);
634 	dsl_dataset_t *ds;
635 	int error;
636 
637 	error = dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds);
638 	if (error != 0)
639 		return (error);
640 
641 	error = dsl_destroy_head_check_impl(ds, 0);
642 	dsl_dataset_rele(ds, FTAG);
643 	return (error);
644 }
645 
646 static void
647 dsl_dir_destroy_sync(uint64_t ddobj, dmu_tx_t *tx)
648 {
649 	dsl_dir_t *dd;
650 	dsl_pool_t *dp = dmu_tx_pool(tx);
651 	objset_t *mos = dp->dp_meta_objset;
652 	dd_used_t t;
653 
654 	ASSERT(RRW_WRITE_HELD(&dmu_tx_pool(tx)->dp_config_rwlock));
655 
656 	VERIFY0(dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd));
657 
658 	ASSERT0(dd->dd_phys->dd_head_dataset_obj);
659 
660 	/*
661 	 * Decrement the filesystem count for all parent filesystems.
662 	 *
663 	 * When we receive an incremental stream into a filesystem that already
664 	 * exists, a temporary clone is created.  We never count this temporary
665 	 * clone, whose name begins with a '%'.
666 	 */
667 	if (dd->dd_myname[0] != '%' && dd->dd_parent != NULL)
668 		dsl_fs_ss_count_adjust(dd->dd_parent, -1,
669 		    DD_FIELD_FILESYSTEM_COUNT, tx);
670 
671 	/*
672 	 * Remove our reservation. The impl() routine avoids setting the
673 	 * actual property, which would require the (already destroyed) ds.
674 	 */
675 	dsl_dir_set_reservation_sync_impl(dd, 0, tx);
676 
677 	ASSERT0(dd->dd_phys->dd_used_bytes);
678 	ASSERT0(dd->dd_phys->dd_reserved);
679 	for (t = 0; t < DD_USED_NUM; t++)
680 		ASSERT0(dd->dd_phys->dd_used_breakdown[t]);
681 
682 	VERIFY0(zap_destroy(mos, dd->dd_phys->dd_child_dir_zapobj, tx));
683 	VERIFY0(zap_destroy(mos, dd->dd_phys->dd_props_zapobj, tx));
684 	VERIFY0(dsl_deleg_destroy(mos, dd->dd_phys->dd_deleg_zapobj, tx));
685 	VERIFY0(zap_remove(mos,
686 	    dd->dd_parent->dd_phys->dd_child_dir_zapobj, dd->dd_myname, tx));
687 
688 	dsl_dir_rele(dd, FTAG);
689 	dmu_object_free_zapified(mos, ddobj, tx);
690 }
691 
692 void
693 dsl_destroy_head_sync_impl(dsl_dataset_t *ds, dmu_tx_t *tx)
694 {
695 	dsl_pool_t *dp = dmu_tx_pool(tx);
696 	objset_t *mos = dp->dp_meta_objset;
697 	uint64_t obj, ddobj, prevobj = 0;
698 	boolean_t rmorigin;
699 
700 	ASSERT3U(ds->ds_phys->ds_num_children, <=, 1);
701 	ASSERT(ds->ds_prev == NULL ||
702 	    ds->ds_prev->ds_phys->ds_next_snap_obj != ds->ds_object);
703 	ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg);
704 	ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
705 
706 	/* We need to log before removing it from the namespace. */
707 	spa_history_log_internal_ds(ds, "destroy", tx, "");
708 
709 	rmorigin = (dsl_dir_is_clone(ds->ds_dir) &&
710 	    DS_IS_DEFER_DESTROY(ds->ds_prev) &&
711 	    ds->ds_prev->ds_phys->ds_num_children == 2 &&
712 	    ds->ds_prev->ds_userrefs == 0);
713 
714 	/* Remove our reservation */
715 	if (ds->ds_reserved != 0) {
716 		dsl_dataset_set_refreservation_sync_impl(ds,
717 		    (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED),
718 		    0, tx);
719 		ASSERT0(ds->ds_reserved);
720 	}
721 
722 	dsl_scan_ds_destroyed(ds, tx);
723 
724 	obj = ds->ds_object;
725 
726 	if (ds->ds_phys->ds_prev_snap_obj != 0) {
727 		/* This is a clone */
728 		ASSERT(ds->ds_prev != NULL);
729 		ASSERT3U(ds->ds_prev->ds_phys->ds_next_snap_obj, !=, obj);
730 		ASSERT0(ds->ds_phys->ds_next_snap_obj);
731 
732 		dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
733 		if (ds->ds_prev->ds_phys->ds_next_clones_obj != 0) {
734 			dsl_dataset_remove_from_next_clones(ds->ds_prev,
735 			    obj, tx);
736 		}
737 
738 		ASSERT3U(ds->ds_prev->ds_phys->ds_num_children, >, 1);
739 		ds->ds_prev->ds_phys->ds_num_children--;
740 	}
741 
742 	/*
743 	 * Destroy the deadlist.  Unless it's a clone, the
744 	 * deadlist should be empty.  (If it's a clone, it's
745 	 * safe to ignore the deadlist contents.)
746 	 */
747 	dsl_deadlist_close(&ds->ds_deadlist);
748 	dsl_deadlist_free(mos, ds->ds_phys->ds_deadlist_obj, tx);
749 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
750 	ds->ds_phys->ds_deadlist_obj = 0;
751 
752 	objset_t *os;
753 	VERIFY0(dmu_objset_from_ds(ds, &os));
754 
755 	if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_ASYNC_DESTROY)) {
756 		old_synchronous_dataset_destroy(ds, tx);
757 	} else {
758 		/*
759 		 * Move the bptree into the pool's list of trees to
760 		 * clean up and update space accounting information.
761 		 */
762 		uint64_t used, comp, uncomp;
763 
764 		zil_destroy_sync(dmu_objset_zil(os), tx);
765 
766 		if (!spa_feature_is_active(dp->dp_spa,
767 		    SPA_FEATURE_ASYNC_DESTROY)) {
768 			dsl_scan_t *scn = dp->dp_scan;
769 			spa_feature_incr(dp->dp_spa, SPA_FEATURE_ASYNC_DESTROY,
770 			    tx);
771 			dp->dp_bptree_obj = bptree_alloc(mos, tx);
772 			VERIFY0(zap_add(mos,
773 			    DMU_POOL_DIRECTORY_OBJECT,
774 			    DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1,
775 			    &dp->dp_bptree_obj, tx));
776 			ASSERT(!scn->scn_async_destroying);
777 			scn->scn_async_destroying = B_TRUE;
778 		}
779 
780 		used = ds->ds_dir->dd_phys->dd_used_bytes;
781 		comp = ds->ds_dir->dd_phys->dd_compressed_bytes;
782 		uncomp = ds->ds_dir->dd_phys->dd_uncompressed_bytes;
783 
784 		ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) ||
785 		    ds->ds_phys->ds_unique_bytes == used);
786 
787 		bptree_add(mos, dp->dp_bptree_obj,
788 		    &ds->ds_phys->ds_bp, ds->ds_phys->ds_prev_snap_txg,
789 		    used, comp, uncomp, tx);
790 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
791 		    -used, -comp, -uncomp, tx);
792 		dsl_dir_diduse_space(dp->dp_free_dir, DD_USED_HEAD,
793 		    used, comp, uncomp, tx);
794 	}
795 
796 	if (ds->ds_prev != NULL) {
797 		if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
798 			VERIFY0(zap_remove_int(mos,
799 			    ds->ds_prev->ds_dir->dd_phys->dd_clones,
800 			    ds->ds_object, tx));
801 		}
802 		prevobj = ds->ds_prev->ds_object;
803 		dsl_dataset_rele(ds->ds_prev, ds);
804 		ds->ds_prev = NULL;
805 	}
806 
807 	/*
808 	 * This must be done after the dsl_traverse(), because it will
809 	 * re-open the objset.
810 	 */
811 	if (ds->ds_objset) {
812 		dmu_objset_evict(ds->ds_objset);
813 		ds->ds_objset = NULL;
814 	}
815 
816 	/* Erase the link in the dir */
817 	dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
818 	ds->ds_dir->dd_phys->dd_head_dataset_obj = 0;
819 	ddobj = ds->ds_dir->dd_object;
820 	ASSERT(ds->ds_phys->ds_snapnames_zapobj != 0);
821 	VERIFY0(zap_destroy(mos, ds->ds_phys->ds_snapnames_zapobj, tx));
822 
823 	if (ds->ds_bookmarks != 0) {
824 		VERIFY0(zap_destroy(mos,
825 		    ds->ds_bookmarks, tx));
826 		spa_feature_decr(dp->dp_spa, SPA_FEATURE_BOOKMARKS, tx);
827 	}
828 
829 	spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
830 
831 	ASSERT0(ds->ds_phys->ds_next_clones_obj);
832 	ASSERT0(ds->ds_phys->ds_props_obj);
833 	ASSERT0(ds->ds_phys->ds_userrefs_obj);
834 	dsl_dir_rele(ds->ds_dir, ds);
835 	ds->ds_dir = NULL;
836 	dmu_object_free_zapified(mos, obj, tx);
837 
838 	dsl_dir_destroy_sync(ddobj, tx);
839 
840 	if (rmorigin) {
841 		dsl_dataset_t *prev;
842 		VERIFY0(dsl_dataset_hold_obj(dp, prevobj, FTAG, &prev));
843 		dsl_destroy_snapshot_sync_impl(prev, B_FALSE, tx);
844 		dsl_dataset_rele(prev, FTAG);
845 	}
846 }
847 
848 static void
849 dsl_destroy_head_sync(void *arg, dmu_tx_t *tx)
850 {
851 	dsl_destroy_head_arg_t *ddha = arg;
852 	dsl_pool_t *dp = dmu_tx_pool(tx);
853 	dsl_dataset_t *ds;
854 
855 	VERIFY0(dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds));
856 	dsl_destroy_head_sync_impl(ds, tx);
857 	dsl_dataset_rele(ds, FTAG);
858 }
859 
860 static void
861 dsl_destroy_head_begin_sync(void *arg, dmu_tx_t *tx)
862 {
863 	dsl_destroy_head_arg_t *ddha = arg;
864 	dsl_pool_t *dp = dmu_tx_pool(tx);
865 	dsl_dataset_t *ds;
866 
867 	VERIFY0(dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds));
868 
869 	/* Mark it as inconsistent on-disk, in case we crash */
870 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
871 	ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT;
872 
873 	spa_history_log_internal_ds(ds, "destroy begin", tx, "");
874 	dsl_dataset_rele(ds, FTAG);
875 }
876 
877 int
878 dsl_destroy_head(const char *name)
879 {
880 	dsl_destroy_head_arg_t ddha;
881 	int error;
882 	spa_t *spa;
883 	boolean_t isenabled;
884 
885 #ifdef _KERNEL
886 	zfs_destroy_unmount_origin(name);
887 #endif
888 
889 	error = spa_open(name, &spa, FTAG);
890 	if (error != 0)
891 		return (error);
892 	isenabled = spa_feature_is_enabled(spa, SPA_FEATURE_ASYNC_DESTROY);
893 	spa_close(spa, FTAG);
894 
895 	ddha.ddha_name = name;
896 
897 	if (!isenabled) {
898 		objset_t *os;
899 
900 		error = dsl_sync_task(name, dsl_destroy_head_check,
901 		    dsl_destroy_head_begin_sync, &ddha, 0);
902 		if (error != 0)
903 			return (error);
904 
905 		/*
906 		 * Head deletion is processed in one txg on old pools;
907 		 * remove the objects from open context so that the txg sync
908 		 * is not too long.
909 		 */
910 		error = dmu_objset_own(name, DMU_OST_ANY, B_FALSE, FTAG, &os);
911 		if (error == 0) {
912 			uint64_t prev_snap_txg =
913 			    dmu_objset_ds(os)->ds_phys->ds_prev_snap_txg;
914 			for (uint64_t obj = 0; error == 0;
915 			    error = dmu_object_next(os, &obj, FALSE,
916 			    prev_snap_txg))
917 				(void) dmu_free_long_object(os, obj);
918 			/* sync out all frees */
919 			txg_wait_synced(dmu_objset_pool(os), 0);
920 			dmu_objset_disown(os, FTAG);
921 		}
922 	}
923 
924 	return (dsl_sync_task(name, dsl_destroy_head_check,
925 	    dsl_destroy_head_sync, &ddha, 0));
926 }
927 
928 /*
929  * Note, this function is used as the callback for dmu_objset_find().  We
930  * always return 0 so that we will continue to find and process
931  * inconsistent datasets, even if we encounter an error trying to
932  * process one of them.
933  */
934 /* ARGSUSED */
935 int
936 dsl_destroy_inconsistent(const char *dsname, void *arg)
937 {
938 	objset_t *os;
939 
940 	if (dmu_objset_hold(dsname, FTAG, &os) == 0) {
941 		boolean_t inconsistent = DS_IS_INCONSISTENT(dmu_objset_ds(os));
942 		dmu_objset_rele(os, FTAG);
943 		if (inconsistent)
944 			(void) dsl_destroy_head(dsname);
945 	}
946 	return (0);
947 }
948