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