xref: /titanic_50/usr/src/uts/common/fs/zfs/dsl_dataset.c (revision 6aa4fc89ec1cf2cdf7d7c3b9ec059802ac9abe65)
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) 2011, 2014 by Delphix. All rights reserved.
24  * Copyright (c) 2014, Joyent, Inc. All rights reserved.
25  * Copyright (c) 2014 RackTop Systems.
26  */
27 
28 #include <sys/dmu_objset.h>
29 #include <sys/dsl_dataset.h>
30 #include <sys/dsl_dir.h>
31 #include <sys/dsl_prop.h>
32 #include <sys/dsl_synctask.h>
33 #include <sys/dmu_traverse.h>
34 #include <sys/dmu_impl.h>
35 #include <sys/dmu_tx.h>
36 #include <sys/arc.h>
37 #include <sys/zio.h>
38 #include <sys/zap.h>
39 #include <sys/zfeature.h>
40 #include <sys/unique.h>
41 #include <sys/zfs_context.h>
42 #include <sys/zfs_ioctl.h>
43 #include <sys/spa.h>
44 #include <sys/zfs_znode.h>
45 #include <sys/zfs_onexit.h>
46 #include <sys/zvol.h>
47 #include <sys/dsl_scan.h>
48 #include <sys/dsl_deadlist.h>
49 #include <sys/dsl_destroy.h>
50 #include <sys/dsl_userhold.h>
51 #include <sys/dsl_bookmark.h>
52 
53 /*
54  * The SPA supports block sizes up to 16MB.  However, very large blocks
55  * can have an impact on i/o latency (e.g. tying up a spinning disk for
56  * ~300ms), and also potentially on the memory allocator.  Therefore,
57  * we do not allow the recordsize to be set larger than zfs_max_recordsize
58  * (default 1MB).  Larger blocks can be created by changing this tunable,
59  * and pools with larger blocks can always be imported and used, regardless
60  * of this setting.
61  */
62 int zfs_max_recordsize = 1 * 1024 * 1024;
63 
64 #define	SWITCH64(x, y) \
65 	{ \
66 		uint64_t __tmp = (x); \
67 		(x) = (y); \
68 		(y) = __tmp; \
69 	}
70 
71 #define	DS_REF_MAX	(1ULL << 62)
72 
73 /*
74  * Figure out how much of this delta should be propogated to the dsl_dir
75  * layer.  If there's a refreservation, that space has already been
76  * partially accounted for in our ancestors.
77  */
78 static int64_t
79 parent_delta(dsl_dataset_t *ds, int64_t delta)
80 {
81 	uint64_t old_bytes, new_bytes;
82 
83 	if (ds->ds_reserved == 0)
84 		return (delta);
85 
86 	old_bytes = MAX(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
87 	new_bytes = MAX(ds->ds_phys->ds_unique_bytes + delta, ds->ds_reserved);
88 
89 	ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta));
90 	return (new_bytes - old_bytes);
91 }
92 
93 void
94 dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx)
95 {
96 	int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
97 	int compressed = BP_GET_PSIZE(bp);
98 	int uncompressed = BP_GET_UCSIZE(bp);
99 	int64_t delta;
100 
101 	dprintf_bp(bp, "ds=%p", ds);
102 
103 	ASSERT(dmu_tx_is_syncing(tx));
104 	/* It could have been compressed away to nothing */
105 	if (BP_IS_HOLE(bp))
106 		return;
107 	ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE);
108 	ASSERT(DMU_OT_IS_VALID(BP_GET_TYPE(bp)));
109 	if (ds == NULL) {
110 		dsl_pool_mos_diduse_space(tx->tx_pool,
111 		    used, compressed, uncompressed);
112 		return;
113 	}
114 
115 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
116 	mutex_enter(&ds->ds_lock);
117 	delta = parent_delta(ds, used);
118 	ds->ds_phys->ds_referenced_bytes += used;
119 	ds->ds_phys->ds_compressed_bytes += compressed;
120 	ds->ds_phys->ds_uncompressed_bytes += uncompressed;
121 	ds->ds_phys->ds_unique_bytes += used;
122 	if (BP_GET_LSIZE(bp) > SPA_OLD_MAXBLOCKSIZE)
123 		ds->ds_need_large_blocks = B_TRUE;
124 	mutex_exit(&ds->ds_lock);
125 	dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta,
126 	    compressed, uncompressed, tx);
127 	dsl_dir_transfer_space(ds->ds_dir, used - delta,
128 	    DD_USED_REFRSRV, DD_USED_HEAD, tx);
129 }
130 
131 int
132 dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx,
133     boolean_t async)
134 {
135 	int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
136 	int compressed = BP_GET_PSIZE(bp);
137 	int uncompressed = BP_GET_UCSIZE(bp);
138 
139 	if (BP_IS_HOLE(bp))
140 		return (0);
141 
142 	ASSERT(dmu_tx_is_syncing(tx));
143 	ASSERT(bp->blk_birth <= tx->tx_txg);
144 
145 	if (ds == NULL) {
146 		dsl_free(tx->tx_pool, tx->tx_txg, bp);
147 		dsl_pool_mos_diduse_space(tx->tx_pool,
148 		    -used, -compressed, -uncompressed);
149 		return (used);
150 	}
151 	ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool);
152 
153 	ASSERT(!dsl_dataset_is_snapshot(ds));
154 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
155 
156 	if (bp->blk_birth > ds->ds_phys->ds_prev_snap_txg) {
157 		int64_t delta;
158 
159 		dprintf_bp(bp, "freeing ds=%llu", ds->ds_object);
160 		dsl_free(tx->tx_pool, tx->tx_txg, bp);
161 
162 		mutex_enter(&ds->ds_lock);
163 		ASSERT(ds->ds_phys->ds_unique_bytes >= used ||
164 		    !DS_UNIQUE_IS_ACCURATE(ds));
165 		delta = parent_delta(ds, -used);
166 		ds->ds_phys->ds_unique_bytes -= used;
167 		mutex_exit(&ds->ds_lock);
168 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
169 		    delta, -compressed, -uncompressed, tx);
170 		dsl_dir_transfer_space(ds->ds_dir, -used - delta,
171 		    DD_USED_REFRSRV, DD_USED_HEAD, tx);
172 	} else {
173 		dprintf_bp(bp, "putting on dead list: %s", "");
174 		if (async) {
175 			/*
176 			 * We are here as part of zio's write done callback,
177 			 * which means we're a zio interrupt thread.  We can't
178 			 * call dsl_deadlist_insert() now because it may block
179 			 * waiting for I/O.  Instead, put bp on the deferred
180 			 * queue and let dsl_pool_sync() finish the job.
181 			 */
182 			bplist_append(&ds->ds_pending_deadlist, bp);
183 		} else {
184 			dsl_deadlist_insert(&ds->ds_deadlist, bp, tx);
185 		}
186 		ASSERT3U(ds->ds_prev->ds_object, ==,
187 		    ds->ds_phys->ds_prev_snap_obj);
188 		ASSERT(ds->ds_prev->ds_phys->ds_num_children > 0);
189 		/* if (bp->blk_birth > prev prev snap txg) prev unique += bs */
190 		if (ds->ds_prev->ds_phys->ds_next_snap_obj ==
191 		    ds->ds_object && bp->blk_birth >
192 		    ds->ds_prev->ds_phys->ds_prev_snap_txg) {
193 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
194 			mutex_enter(&ds->ds_prev->ds_lock);
195 			ds->ds_prev->ds_phys->ds_unique_bytes += used;
196 			mutex_exit(&ds->ds_prev->ds_lock);
197 		}
198 		if (bp->blk_birth > ds->ds_dir->dd_origin_txg) {
199 			dsl_dir_transfer_space(ds->ds_dir, used,
200 			    DD_USED_HEAD, DD_USED_SNAP, tx);
201 		}
202 	}
203 	mutex_enter(&ds->ds_lock);
204 	ASSERT3U(ds->ds_phys->ds_referenced_bytes, >=, used);
205 	ds->ds_phys->ds_referenced_bytes -= used;
206 	ASSERT3U(ds->ds_phys->ds_compressed_bytes, >=, compressed);
207 	ds->ds_phys->ds_compressed_bytes -= compressed;
208 	ASSERT3U(ds->ds_phys->ds_uncompressed_bytes, >=, uncompressed);
209 	ds->ds_phys->ds_uncompressed_bytes -= uncompressed;
210 	mutex_exit(&ds->ds_lock);
211 
212 	return (used);
213 }
214 
215 uint64_t
216 dsl_dataset_prev_snap_txg(dsl_dataset_t *ds)
217 {
218 	uint64_t trysnap = 0;
219 
220 	if (ds == NULL)
221 		return (0);
222 	/*
223 	 * The snapshot creation could fail, but that would cause an
224 	 * incorrect FALSE return, which would only result in an
225 	 * overestimation of the amount of space that an operation would
226 	 * consume, which is OK.
227 	 *
228 	 * There's also a small window where we could miss a pending
229 	 * snapshot, because we could set the sync task in the quiescing
230 	 * phase.  So this should only be used as a guess.
231 	 */
232 	if (ds->ds_trysnap_txg >
233 	    spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa))
234 		trysnap = ds->ds_trysnap_txg;
235 	return (MAX(ds->ds_phys->ds_prev_snap_txg, trysnap));
236 }
237 
238 boolean_t
239 dsl_dataset_block_freeable(dsl_dataset_t *ds, const blkptr_t *bp,
240     uint64_t blk_birth)
241 {
242 	if (blk_birth <= dsl_dataset_prev_snap_txg(ds) ||
243 	    (bp != NULL && BP_IS_HOLE(bp)))
244 		return (B_FALSE);
245 
246 	ddt_prefetch(dsl_dataset_get_spa(ds), bp);
247 
248 	return (B_TRUE);
249 }
250 
251 /* ARGSUSED */
252 static void
253 dsl_dataset_evict(dmu_buf_t *db, void *dsv)
254 {
255 	dsl_dataset_t *ds = dsv;
256 
257 	ASSERT(ds->ds_owner == NULL);
258 
259 	unique_remove(ds->ds_fsid_guid);
260 
261 	if (ds->ds_objset != NULL)
262 		dmu_objset_evict(ds->ds_objset);
263 
264 	if (ds->ds_prev) {
265 		dsl_dataset_rele(ds->ds_prev, ds);
266 		ds->ds_prev = NULL;
267 	}
268 
269 	bplist_destroy(&ds->ds_pending_deadlist);
270 	if (ds->ds_phys->ds_deadlist_obj != 0)
271 		dsl_deadlist_close(&ds->ds_deadlist);
272 	if (ds->ds_dir)
273 		dsl_dir_rele(ds->ds_dir, ds);
274 
275 	ASSERT(!list_link_active(&ds->ds_synced_link));
276 
277 	mutex_destroy(&ds->ds_lock);
278 	mutex_destroy(&ds->ds_opening_lock);
279 	mutex_destroy(&ds->ds_sendstream_lock);
280 	refcount_destroy(&ds->ds_longholds);
281 
282 	kmem_free(ds, sizeof (dsl_dataset_t));
283 }
284 
285 int
286 dsl_dataset_get_snapname(dsl_dataset_t *ds)
287 {
288 	dsl_dataset_phys_t *headphys;
289 	int err;
290 	dmu_buf_t *headdbuf;
291 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
292 	objset_t *mos = dp->dp_meta_objset;
293 
294 	if (ds->ds_snapname[0])
295 		return (0);
296 	if (ds->ds_phys->ds_next_snap_obj == 0)
297 		return (0);
298 
299 	err = dmu_bonus_hold(mos, ds->ds_dir->dd_phys->dd_head_dataset_obj,
300 	    FTAG, &headdbuf);
301 	if (err != 0)
302 		return (err);
303 	headphys = headdbuf->db_data;
304 	err = zap_value_search(dp->dp_meta_objset,
305 	    headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname);
306 	dmu_buf_rele(headdbuf, FTAG);
307 	return (err);
308 }
309 
310 int
311 dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value)
312 {
313 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
314 	uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
315 	matchtype_t mt;
316 	int err;
317 
318 	if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
319 		mt = MT_FIRST;
320 	else
321 		mt = MT_EXACT;
322 
323 	err = zap_lookup_norm(mos, snapobj, name, 8, 1,
324 	    value, mt, NULL, 0, NULL);
325 	if (err == ENOTSUP && mt == MT_FIRST)
326 		err = zap_lookup(mos, snapobj, name, 8, 1, value);
327 	return (err);
328 }
329 
330 int
331 dsl_dataset_snap_remove(dsl_dataset_t *ds, const char *name, dmu_tx_t *tx,
332     boolean_t adj_cnt)
333 {
334 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
335 	uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
336 	matchtype_t mt;
337 	int err;
338 
339 	dsl_dir_snap_cmtime_update(ds->ds_dir);
340 
341 	if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
342 		mt = MT_FIRST;
343 	else
344 		mt = MT_EXACT;
345 
346 	err = zap_remove_norm(mos, snapobj, name, mt, tx);
347 	if (err == ENOTSUP && mt == MT_FIRST)
348 		err = zap_remove(mos, snapobj, name, tx);
349 
350 	if (err == 0 && adj_cnt)
351 		dsl_fs_ss_count_adjust(ds->ds_dir, -1,
352 		    DD_FIELD_SNAPSHOT_COUNT, tx);
353 
354 	return (err);
355 }
356 
357 int
358 dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag,
359     dsl_dataset_t **dsp)
360 {
361 	objset_t *mos = dp->dp_meta_objset;
362 	dmu_buf_t *dbuf;
363 	dsl_dataset_t *ds;
364 	int err;
365 	dmu_object_info_t doi;
366 
367 	ASSERT(dsl_pool_config_held(dp));
368 
369 	err = dmu_bonus_hold(mos, dsobj, tag, &dbuf);
370 	if (err != 0)
371 		return (err);
372 
373 	/* Make sure dsobj has the correct object type. */
374 	dmu_object_info_from_db(dbuf, &doi);
375 	if (doi.doi_bonus_type != DMU_OT_DSL_DATASET) {
376 		dmu_buf_rele(dbuf, tag);
377 		return (SET_ERROR(EINVAL));
378 	}
379 
380 	ds = dmu_buf_get_user(dbuf);
381 	if (ds == NULL) {
382 		dsl_dataset_t *winner = NULL;
383 
384 		ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP);
385 		ds->ds_dbuf = dbuf;
386 		ds->ds_object = dsobj;
387 		ds->ds_phys = dbuf->db_data;
388 
389 		mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL);
390 		mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL);
391 		mutex_init(&ds->ds_sendstream_lock, NULL, MUTEX_DEFAULT, NULL);
392 		refcount_create(&ds->ds_longholds);
393 
394 		bplist_create(&ds->ds_pending_deadlist);
395 		dsl_deadlist_open(&ds->ds_deadlist,
396 		    mos, ds->ds_phys->ds_deadlist_obj);
397 
398 		list_create(&ds->ds_sendstreams, sizeof (dmu_sendarg_t),
399 		    offsetof(dmu_sendarg_t, dsa_link));
400 
401 		if (doi.doi_type == DMU_OTN_ZAP_METADATA) {
402 			err = zap_contains(mos, dsobj, DS_FIELD_LARGE_BLOCKS);
403 			if (err == 0)
404 				ds->ds_large_blocks = B_TRUE;
405 			else
406 				ASSERT3U(err, ==, ENOENT);
407 		}
408 
409 		if (err == 0) {
410 			err = dsl_dir_hold_obj(dp,
411 			    ds->ds_phys->ds_dir_obj, NULL, ds, &ds->ds_dir);
412 		}
413 		if (err != 0) {
414 			mutex_destroy(&ds->ds_lock);
415 			mutex_destroy(&ds->ds_opening_lock);
416 			mutex_destroy(&ds->ds_sendstream_lock);
417 			refcount_destroy(&ds->ds_longholds);
418 			bplist_destroy(&ds->ds_pending_deadlist);
419 			dsl_deadlist_close(&ds->ds_deadlist);
420 			kmem_free(ds, sizeof (dsl_dataset_t));
421 			dmu_buf_rele(dbuf, tag);
422 			return (err);
423 		}
424 
425 		if (!dsl_dataset_is_snapshot(ds)) {
426 			ds->ds_snapname[0] = '\0';
427 			if (ds->ds_phys->ds_prev_snap_obj != 0) {
428 				err = dsl_dataset_hold_obj(dp,
429 				    ds->ds_phys->ds_prev_snap_obj,
430 				    ds, &ds->ds_prev);
431 			}
432 			if (doi.doi_type == DMU_OTN_ZAP_METADATA) {
433 				int zaperr = zap_lookup(mos, ds->ds_object,
434 				    DS_FIELD_BOOKMARK_NAMES,
435 				    sizeof (ds->ds_bookmarks), 1,
436 				    &ds->ds_bookmarks);
437 				if (zaperr != ENOENT)
438 					VERIFY0(zaperr);
439 			}
440 		} else {
441 			if (zfs_flags & ZFS_DEBUG_SNAPNAMES)
442 				err = dsl_dataset_get_snapname(ds);
443 			if (err == 0 && ds->ds_phys->ds_userrefs_obj != 0) {
444 				err = zap_count(
445 				    ds->ds_dir->dd_pool->dp_meta_objset,
446 				    ds->ds_phys->ds_userrefs_obj,
447 				    &ds->ds_userrefs);
448 			}
449 		}
450 
451 		if (err == 0 && !dsl_dataset_is_snapshot(ds)) {
452 			err = dsl_prop_get_int_ds(ds,
453 			    zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
454 			    &ds->ds_reserved);
455 			if (err == 0) {
456 				err = dsl_prop_get_int_ds(ds,
457 				    zfs_prop_to_name(ZFS_PROP_REFQUOTA),
458 				    &ds->ds_quota);
459 			}
460 		} else {
461 			ds->ds_reserved = ds->ds_quota = 0;
462 		}
463 
464 		if (err != 0 || (winner = dmu_buf_set_user_ie(dbuf, ds,
465 		    &ds->ds_phys, dsl_dataset_evict)) != NULL) {
466 			bplist_destroy(&ds->ds_pending_deadlist);
467 			dsl_deadlist_close(&ds->ds_deadlist);
468 			if (ds->ds_prev)
469 				dsl_dataset_rele(ds->ds_prev, ds);
470 			dsl_dir_rele(ds->ds_dir, ds);
471 			mutex_destroy(&ds->ds_lock);
472 			mutex_destroy(&ds->ds_opening_lock);
473 			mutex_destroy(&ds->ds_sendstream_lock);
474 			refcount_destroy(&ds->ds_longholds);
475 			kmem_free(ds, sizeof (dsl_dataset_t));
476 			if (err != 0) {
477 				dmu_buf_rele(dbuf, tag);
478 				return (err);
479 			}
480 			ds = winner;
481 		} else {
482 			ds->ds_fsid_guid =
483 			    unique_insert(ds->ds_phys->ds_fsid_guid);
484 		}
485 	}
486 	ASSERT3P(ds->ds_dbuf, ==, dbuf);
487 	ASSERT3P(ds->ds_phys, ==, dbuf->db_data);
488 	ASSERT(ds->ds_phys->ds_prev_snap_obj != 0 ||
489 	    spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN ||
490 	    dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap);
491 	*dsp = ds;
492 	return (0);
493 }
494 
495 int
496 dsl_dataset_hold(dsl_pool_t *dp, const char *name,
497     void *tag, dsl_dataset_t **dsp)
498 {
499 	dsl_dir_t *dd;
500 	const char *snapname;
501 	uint64_t obj;
502 	int err = 0;
503 
504 	err = dsl_dir_hold(dp, name, FTAG, &dd, &snapname);
505 	if (err != 0)
506 		return (err);
507 
508 	ASSERT(dsl_pool_config_held(dp));
509 	obj = dd->dd_phys->dd_head_dataset_obj;
510 	if (obj != 0)
511 		err = dsl_dataset_hold_obj(dp, obj, tag, dsp);
512 	else
513 		err = SET_ERROR(ENOENT);
514 
515 	/* we may be looking for a snapshot */
516 	if (err == 0 && snapname != NULL) {
517 		dsl_dataset_t *ds;
518 
519 		if (*snapname++ != '@') {
520 			dsl_dataset_rele(*dsp, tag);
521 			dsl_dir_rele(dd, FTAG);
522 			return (SET_ERROR(ENOENT));
523 		}
524 
525 		dprintf("looking for snapshot '%s'\n", snapname);
526 		err = dsl_dataset_snap_lookup(*dsp, snapname, &obj);
527 		if (err == 0)
528 			err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
529 		dsl_dataset_rele(*dsp, tag);
530 
531 		if (err == 0) {
532 			mutex_enter(&ds->ds_lock);
533 			if (ds->ds_snapname[0] == 0)
534 				(void) strlcpy(ds->ds_snapname, snapname,
535 				    sizeof (ds->ds_snapname));
536 			mutex_exit(&ds->ds_lock);
537 			*dsp = ds;
538 		}
539 	}
540 
541 	dsl_dir_rele(dd, FTAG);
542 	return (err);
543 }
544 
545 int
546 dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj,
547     void *tag, dsl_dataset_t **dsp)
548 {
549 	int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp);
550 	if (err != 0)
551 		return (err);
552 	if (!dsl_dataset_tryown(*dsp, tag)) {
553 		dsl_dataset_rele(*dsp, tag);
554 		*dsp = NULL;
555 		return (SET_ERROR(EBUSY));
556 	}
557 	return (0);
558 }
559 
560 int
561 dsl_dataset_own(dsl_pool_t *dp, const char *name,
562     void *tag, dsl_dataset_t **dsp)
563 {
564 	int err = dsl_dataset_hold(dp, name, tag, dsp);
565 	if (err != 0)
566 		return (err);
567 	if (!dsl_dataset_tryown(*dsp, tag)) {
568 		dsl_dataset_rele(*dsp, tag);
569 		return (SET_ERROR(EBUSY));
570 	}
571 	return (0);
572 }
573 
574 /*
575  * See the comment above dsl_pool_hold() for details.  In summary, a long
576  * hold is used to prevent destruction of a dataset while the pool hold
577  * is dropped, allowing other concurrent operations (e.g. spa_sync()).
578  *
579  * The dataset and pool must be held when this function is called.  After it
580  * is called, the pool hold may be released while the dataset is still held
581  * and accessed.
582  */
583 void
584 dsl_dataset_long_hold(dsl_dataset_t *ds, void *tag)
585 {
586 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
587 	(void) refcount_add(&ds->ds_longholds, tag);
588 }
589 
590 void
591 dsl_dataset_long_rele(dsl_dataset_t *ds, void *tag)
592 {
593 	(void) refcount_remove(&ds->ds_longholds, tag);
594 }
595 
596 /* Return B_TRUE if there are any long holds on this dataset. */
597 boolean_t
598 dsl_dataset_long_held(dsl_dataset_t *ds)
599 {
600 	return (!refcount_is_zero(&ds->ds_longholds));
601 }
602 
603 void
604 dsl_dataset_name(dsl_dataset_t *ds, char *name)
605 {
606 	if (ds == NULL) {
607 		(void) strcpy(name, "mos");
608 	} else {
609 		dsl_dir_name(ds->ds_dir, name);
610 		VERIFY0(dsl_dataset_get_snapname(ds));
611 		if (ds->ds_snapname[0]) {
612 			(void) strcat(name, "@");
613 			/*
614 			 * We use a "recursive" mutex so that we
615 			 * can call dprintf_ds() with ds_lock held.
616 			 */
617 			if (!MUTEX_HELD(&ds->ds_lock)) {
618 				mutex_enter(&ds->ds_lock);
619 				(void) strcat(name, ds->ds_snapname);
620 				mutex_exit(&ds->ds_lock);
621 			} else {
622 				(void) strcat(name, ds->ds_snapname);
623 			}
624 		}
625 	}
626 }
627 
628 void
629 dsl_dataset_rele(dsl_dataset_t *ds, void *tag)
630 {
631 	dmu_buf_rele(ds->ds_dbuf, tag);
632 }
633 
634 void
635 dsl_dataset_disown(dsl_dataset_t *ds, void *tag)
636 {
637 	ASSERT3P(ds->ds_owner, ==, tag);
638 	ASSERT(ds->ds_dbuf != NULL);
639 
640 	mutex_enter(&ds->ds_lock);
641 	ds->ds_owner = NULL;
642 	mutex_exit(&ds->ds_lock);
643 	dsl_dataset_long_rele(ds, tag);
644 	dsl_dataset_rele(ds, tag);
645 }
646 
647 boolean_t
648 dsl_dataset_tryown(dsl_dataset_t *ds, void *tag)
649 {
650 	boolean_t gotit = FALSE;
651 
652 	mutex_enter(&ds->ds_lock);
653 	if (ds->ds_owner == NULL && !DS_IS_INCONSISTENT(ds)) {
654 		ds->ds_owner = tag;
655 		dsl_dataset_long_hold(ds, tag);
656 		gotit = TRUE;
657 	}
658 	mutex_exit(&ds->ds_lock);
659 	return (gotit);
660 }
661 
662 uint64_t
663 dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin,
664     uint64_t flags, dmu_tx_t *tx)
665 {
666 	dsl_pool_t *dp = dd->dd_pool;
667 	dmu_buf_t *dbuf;
668 	dsl_dataset_phys_t *dsphys;
669 	uint64_t dsobj;
670 	objset_t *mos = dp->dp_meta_objset;
671 
672 	if (origin == NULL)
673 		origin = dp->dp_origin_snap;
674 
675 	ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp);
676 	ASSERT(origin == NULL || origin->ds_phys->ds_num_children > 0);
677 	ASSERT(dmu_tx_is_syncing(tx));
678 	ASSERT(dd->dd_phys->dd_head_dataset_obj == 0);
679 
680 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
681 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
682 	VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
683 	dmu_buf_will_dirty(dbuf, tx);
684 	dsphys = dbuf->db_data;
685 	bzero(dsphys, sizeof (dsl_dataset_phys_t));
686 	dsphys->ds_dir_obj = dd->dd_object;
687 	dsphys->ds_flags = flags;
688 	dsphys->ds_fsid_guid = unique_create();
689 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
690 	    sizeof (dsphys->ds_guid));
691 	dsphys->ds_snapnames_zapobj =
692 	    zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP,
693 	    DMU_OT_NONE, 0, tx);
694 	dsphys->ds_creation_time = gethrestime_sec();
695 	dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg;
696 
697 	if (origin == NULL) {
698 		dsphys->ds_deadlist_obj = dsl_deadlist_alloc(mos, tx);
699 	} else {
700 		dsl_dataset_t *ohds; /* head of the origin snapshot */
701 
702 		dsphys->ds_prev_snap_obj = origin->ds_object;
703 		dsphys->ds_prev_snap_txg =
704 		    origin->ds_phys->ds_creation_txg;
705 		dsphys->ds_referenced_bytes =
706 		    origin->ds_phys->ds_referenced_bytes;
707 		dsphys->ds_compressed_bytes =
708 		    origin->ds_phys->ds_compressed_bytes;
709 		dsphys->ds_uncompressed_bytes =
710 		    origin->ds_phys->ds_uncompressed_bytes;
711 		dsphys->ds_bp = origin->ds_phys->ds_bp;
712 
713 		/*
714 		 * Inherit flags that describe the dataset's contents
715 		 * (INCONSISTENT) or properties (Case Insensitive).
716 		 */
717 		dsphys->ds_flags |= origin->ds_phys->ds_flags &
718 		    (DS_FLAG_INCONSISTENT | DS_FLAG_CI_DATASET);
719 
720 		if (origin->ds_large_blocks)
721 			dsl_dataset_activate_large_blocks_sync_impl(dsobj, tx);
722 
723 		dmu_buf_will_dirty(origin->ds_dbuf, tx);
724 		origin->ds_phys->ds_num_children++;
725 
726 		VERIFY0(dsl_dataset_hold_obj(dp,
727 		    origin->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ohds));
728 		dsphys->ds_deadlist_obj = dsl_deadlist_clone(&ohds->ds_deadlist,
729 		    dsphys->ds_prev_snap_txg, dsphys->ds_prev_snap_obj, tx);
730 		dsl_dataset_rele(ohds, FTAG);
731 
732 		if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) {
733 			if (origin->ds_phys->ds_next_clones_obj == 0) {
734 				origin->ds_phys->ds_next_clones_obj =
735 				    zap_create(mos,
736 				    DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
737 			}
738 			VERIFY0(zap_add_int(mos,
739 			    origin->ds_phys->ds_next_clones_obj, dsobj, tx));
740 		}
741 
742 		dmu_buf_will_dirty(dd->dd_dbuf, tx);
743 		dd->dd_phys->dd_origin_obj = origin->ds_object;
744 		if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
745 			if (origin->ds_dir->dd_phys->dd_clones == 0) {
746 				dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
747 				origin->ds_dir->dd_phys->dd_clones =
748 				    zap_create(mos,
749 				    DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
750 			}
751 			VERIFY0(zap_add_int(mos,
752 			    origin->ds_dir->dd_phys->dd_clones, dsobj, tx));
753 		}
754 	}
755 
756 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
757 		dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
758 
759 	dmu_buf_rele(dbuf, FTAG);
760 
761 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
762 	dd->dd_phys->dd_head_dataset_obj = dsobj;
763 
764 	return (dsobj);
765 }
766 
767 static void
768 dsl_dataset_zero_zil(dsl_dataset_t *ds, dmu_tx_t *tx)
769 {
770 	objset_t *os;
771 
772 	VERIFY0(dmu_objset_from_ds(ds, &os));
773 	bzero(&os->os_zil_header, sizeof (os->os_zil_header));
774 	dsl_dataset_dirty(ds, tx);
775 }
776 
777 uint64_t
778 dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname,
779     dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx)
780 {
781 	dsl_pool_t *dp = pdd->dd_pool;
782 	uint64_t dsobj, ddobj;
783 	dsl_dir_t *dd;
784 
785 	ASSERT(dmu_tx_is_syncing(tx));
786 	ASSERT(lastname[0] != '@');
787 
788 	ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx);
789 	VERIFY0(dsl_dir_hold_obj(dp, ddobj, lastname, FTAG, &dd));
790 
791 	dsobj = dsl_dataset_create_sync_dd(dd, origin,
792 	    flags & ~DS_CREATE_FLAG_NODIRTY, tx);
793 
794 	dsl_deleg_set_create_perms(dd, tx, cr);
795 
796 	/*
797 	 * Since we're creating a new node we know it's a leaf, so we can
798 	 * initialize the counts if the limit feature is active.
799 	 */
800 	if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) {
801 		uint64_t cnt = 0;
802 		objset_t *os = dd->dd_pool->dp_meta_objset;
803 
804 		dsl_dir_zapify(dd, tx);
805 		VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
806 		    sizeof (cnt), 1, &cnt, tx));
807 		VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
808 		    sizeof (cnt), 1, &cnt, tx));
809 	}
810 
811 	dsl_dir_rele(dd, FTAG);
812 
813 	/*
814 	 * If we are creating a clone, make sure we zero out any stale
815 	 * data from the origin snapshots zil header.
816 	 */
817 	if (origin != NULL && !(flags & DS_CREATE_FLAG_NODIRTY)) {
818 		dsl_dataset_t *ds;
819 
820 		VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
821 		dsl_dataset_zero_zil(ds, tx);
822 		dsl_dataset_rele(ds, FTAG);
823 	}
824 
825 	return (dsobj);
826 }
827 
828 /*
829  * The unique space in the head dataset can be calculated by subtracting
830  * the space used in the most recent snapshot, that is still being used
831  * in this file system, from the space currently in use.  To figure out
832  * the space in the most recent snapshot still in use, we need to take
833  * the total space used in the snapshot and subtract out the space that
834  * has been freed up since the snapshot was taken.
835  */
836 void
837 dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds)
838 {
839 	uint64_t mrs_used;
840 	uint64_t dlused, dlcomp, dluncomp;
841 
842 	ASSERT(!dsl_dataset_is_snapshot(ds));
843 
844 	if (ds->ds_phys->ds_prev_snap_obj != 0)
845 		mrs_used = ds->ds_prev->ds_phys->ds_referenced_bytes;
846 	else
847 		mrs_used = 0;
848 
849 	dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp);
850 
851 	ASSERT3U(dlused, <=, mrs_used);
852 	ds->ds_phys->ds_unique_bytes =
853 	    ds->ds_phys->ds_referenced_bytes - (mrs_used - dlused);
854 
855 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) >=
856 	    SPA_VERSION_UNIQUE_ACCURATE)
857 		ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
858 }
859 
860 void
861 dsl_dataset_remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj,
862     dmu_tx_t *tx)
863 {
864 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
865 	uint64_t count;
866 	int err;
867 
868 	ASSERT(ds->ds_phys->ds_num_children >= 2);
869 	err = zap_remove_int(mos, ds->ds_phys->ds_next_clones_obj, obj, tx);
870 	/*
871 	 * The err should not be ENOENT, but a bug in a previous version
872 	 * of the code could cause upgrade_clones_cb() to not set
873 	 * ds_next_snap_obj when it should, leading to a missing entry.
874 	 * If we knew that the pool was created after
875 	 * SPA_VERSION_NEXT_CLONES, we could assert that it isn't
876 	 * ENOENT.  However, at least we can check that we don't have
877 	 * too many entries in the next_clones_obj even after failing to
878 	 * remove this one.
879 	 */
880 	if (err != ENOENT)
881 		VERIFY0(err);
882 	ASSERT0(zap_count(mos, ds->ds_phys->ds_next_clones_obj,
883 	    &count));
884 	ASSERT3U(count, <=, ds->ds_phys->ds_num_children - 2);
885 }
886 
887 
888 blkptr_t *
889 dsl_dataset_get_blkptr(dsl_dataset_t *ds)
890 {
891 	return (&ds->ds_phys->ds_bp);
892 }
893 
894 void
895 dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx)
896 {
897 	ASSERT(dmu_tx_is_syncing(tx));
898 	/* If it's the meta-objset, set dp_meta_rootbp */
899 	if (ds == NULL) {
900 		tx->tx_pool->dp_meta_rootbp = *bp;
901 	} else {
902 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
903 		ds->ds_phys->ds_bp = *bp;
904 	}
905 }
906 
907 spa_t *
908 dsl_dataset_get_spa(dsl_dataset_t *ds)
909 {
910 	return (ds->ds_dir->dd_pool->dp_spa);
911 }
912 
913 void
914 dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx)
915 {
916 	dsl_pool_t *dp;
917 
918 	if (ds == NULL) /* this is the meta-objset */
919 		return;
920 
921 	ASSERT(ds->ds_objset != NULL);
922 
923 	if (ds->ds_phys->ds_next_snap_obj != 0)
924 		panic("dirtying snapshot!");
925 
926 	dp = ds->ds_dir->dd_pool;
927 
928 	if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg)) {
929 		/* up the hold count until we can be written out */
930 		dmu_buf_add_ref(ds->ds_dbuf, ds);
931 	}
932 }
933 
934 boolean_t
935 dsl_dataset_is_dirty(dsl_dataset_t *ds)
936 {
937 	for (int t = 0; t < TXG_SIZE; t++) {
938 		if (txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets,
939 		    ds, t))
940 			return (B_TRUE);
941 	}
942 	return (B_FALSE);
943 }
944 
945 static int
946 dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx)
947 {
948 	uint64_t asize;
949 
950 	if (!dmu_tx_is_syncing(tx))
951 		return (0);
952 
953 	/*
954 	 * If there's an fs-only reservation, any blocks that might become
955 	 * owned by the snapshot dataset must be accommodated by space
956 	 * outside of the reservation.
957 	 */
958 	ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds));
959 	asize = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
960 	if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
961 		return (SET_ERROR(ENOSPC));
962 
963 	/*
964 	 * Propagate any reserved space for this snapshot to other
965 	 * snapshot checks in this sync group.
966 	 */
967 	if (asize > 0)
968 		dsl_dir_willuse_space(ds->ds_dir, asize, tx);
969 
970 	return (0);
971 }
972 
973 typedef struct dsl_dataset_snapshot_arg {
974 	nvlist_t *ddsa_snaps;
975 	nvlist_t *ddsa_props;
976 	nvlist_t *ddsa_errors;
977 	cred_t *ddsa_cr;
978 } dsl_dataset_snapshot_arg_t;
979 
980 int
981 dsl_dataset_snapshot_check_impl(dsl_dataset_t *ds, const char *snapname,
982     dmu_tx_t *tx, boolean_t recv, uint64_t cnt, cred_t *cr)
983 {
984 	int error;
985 	uint64_t value;
986 
987 	ds->ds_trysnap_txg = tx->tx_txg;
988 
989 	if (!dmu_tx_is_syncing(tx))
990 		return (0);
991 
992 	/*
993 	 * We don't allow multiple snapshots of the same txg.  If there
994 	 * is already one, try again.
995 	 */
996 	if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg)
997 		return (SET_ERROR(EAGAIN));
998 
999 	/*
1000 	 * Check for conflicting snapshot name.
1001 	 */
1002 	error = dsl_dataset_snap_lookup(ds, snapname, &value);
1003 	if (error == 0)
1004 		return (SET_ERROR(EEXIST));
1005 	if (error != ENOENT)
1006 		return (error);
1007 
1008 	/*
1009 	 * We don't allow taking snapshots of inconsistent datasets, such as
1010 	 * those into which we are currently receiving.  However, if we are
1011 	 * creating this snapshot as part of a receive, this check will be
1012 	 * executed atomically with respect to the completion of the receive
1013 	 * itself but prior to the clearing of DS_FLAG_INCONSISTENT; in this
1014 	 * case we ignore this, knowing it will be fixed up for us shortly in
1015 	 * dmu_recv_end_sync().
1016 	 */
1017 	if (!recv && DS_IS_INCONSISTENT(ds))
1018 		return (SET_ERROR(EBUSY));
1019 
1020 	/*
1021 	 * Skip the check for temporary snapshots or if we have already checked
1022 	 * the counts in dsl_dataset_snapshot_check. This means we really only
1023 	 * check the count here when we're receiving a stream.
1024 	 */
1025 	if (cnt != 0 && cr != NULL) {
1026 		error = dsl_fs_ss_limit_check(ds->ds_dir, cnt,
1027 		    ZFS_PROP_SNAPSHOT_LIMIT, NULL, cr);
1028 		if (error != 0)
1029 			return (error);
1030 	}
1031 
1032 	error = dsl_dataset_snapshot_reserve_space(ds, tx);
1033 	if (error != 0)
1034 		return (error);
1035 
1036 	return (0);
1037 }
1038 
1039 static int
1040 dsl_dataset_snapshot_check(void *arg, dmu_tx_t *tx)
1041 {
1042 	dsl_dataset_snapshot_arg_t *ddsa = arg;
1043 	dsl_pool_t *dp = dmu_tx_pool(tx);
1044 	nvpair_t *pair;
1045 	int rv = 0;
1046 
1047 	/*
1048 	 * Pre-compute how many total new snapshots will be created for each
1049 	 * level in the tree and below. This is needed for validating the
1050 	 * snapshot limit when either taking a recursive snapshot or when
1051 	 * taking multiple snapshots.
1052 	 *
1053 	 * The problem is that the counts are not actually adjusted when
1054 	 * we are checking, only when we finally sync. For a single snapshot,
1055 	 * this is easy, the count will increase by 1 at each node up the tree,
1056 	 * but its more complicated for the recursive/multiple snapshot case.
1057 	 *
1058 	 * The dsl_fs_ss_limit_check function does recursively check the count
1059 	 * at each level up the tree but since it is validating each snapshot
1060 	 * independently we need to be sure that we are validating the complete
1061 	 * count for the entire set of snapshots. We do this by rolling up the
1062 	 * counts for each component of the name into an nvlist and then
1063 	 * checking each of those cases with the aggregated count.
1064 	 *
1065 	 * This approach properly handles not only the recursive snapshot
1066 	 * case (where we get all of those on the ddsa_snaps list) but also
1067 	 * the sibling case (e.g. snapshot a/b and a/c so that we will also
1068 	 * validate the limit on 'a' using a count of 2).
1069 	 *
1070 	 * We validate the snapshot names in the third loop and only report
1071 	 * name errors once.
1072 	 */
1073 	if (dmu_tx_is_syncing(tx)) {
1074 		nvlist_t *cnt_track = NULL;
1075 		cnt_track = fnvlist_alloc();
1076 
1077 		/* Rollup aggregated counts into the cnt_track list */
1078 		for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1079 		    pair != NULL;
1080 		    pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1081 			char *pdelim;
1082 			uint64_t val;
1083 			char nm[MAXPATHLEN];
1084 
1085 			(void) strlcpy(nm, nvpair_name(pair), sizeof (nm));
1086 			pdelim = strchr(nm, '@');
1087 			if (pdelim == NULL)
1088 				continue;
1089 			*pdelim = '\0';
1090 
1091 			do {
1092 				if (nvlist_lookup_uint64(cnt_track, nm,
1093 				    &val) == 0) {
1094 					/* update existing entry */
1095 					fnvlist_add_uint64(cnt_track, nm,
1096 					    val + 1);
1097 				} else {
1098 					/* add to list */
1099 					fnvlist_add_uint64(cnt_track, nm, 1);
1100 				}
1101 
1102 				pdelim = strrchr(nm, '/');
1103 				if (pdelim != NULL)
1104 					*pdelim = '\0';
1105 			} while (pdelim != NULL);
1106 		}
1107 
1108 		/* Check aggregated counts at each level */
1109 		for (pair = nvlist_next_nvpair(cnt_track, NULL);
1110 		    pair != NULL; pair = nvlist_next_nvpair(cnt_track, pair)) {
1111 			int error = 0;
1112 			char *name;
1113 			uint64_t cnt = 0;
1114 			dsl_dataset_t *ds;
1115 
1116 			name = nvpair_name(pair);
1117 			cnt = fnvpair_value_uint64(pair);
1118 			ASSERT(cnt > 0);
1119 
1120 			error = dsl_dataset_hold(dp, name, FTAG, &ds);
1121 			if (error == 0) {
1122 				error = dsl_fs_ss_limit_check(ds->ds_dir, cnt,
1123 				    ZFS_PROP_SNAPSHOT_LIMIT, NULL,
1124 				    ddsa->ddsa_cr);
1125 				dsl_dataset_rele(ds, FTAG);
1126 			}
1127 
1128 			if (error != 0) {
1129 				if (ddsa->ddsa_errors != NULL)
1130 					fnvlist_add_int32(ddsa->ddsa_errors,
1131 					    name, error);
1132 				rv = error;
1133 				/* only report one error for this check */
1134 				break;
1135 			}
1136 		}
1137 		nvlist_free(cnt_track);
1138 	}
1139 
1140 	for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1141 	    pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1142 		int error = 0;
1143 		dsl_dataset_t *ds;
1144 		char *name, *atp;
1145 		char dsname[MAXNAMELEN];
1146 
1147 		name = nvpair_name(pair);
1148 		if (strlen(name) >= MAXNAMELEN)
1149 			error = SET_ERROR(ENAMETOOLONG);
1150 		if (error == 0) {
1151 			atp = strchr(name, '@');
1152 			if (atp == NULL)
1153 				error = SET_ERROR(EINVAL);
1154 			if (error == 0)
1155 				(void) strlcpy(dsname, name, atp - name + 1);
1156 		}
1157 		if (error == 0)
1158 			error = dsl_dataset_hold(dp, dsname, FTAG, &ds);
1159 		if (error == 0) {
1160 			/* passing 0/NULL skips dsl_fs_ss_limit_check */
1161 			error = dsl_dataset_snapshot_check_impl(ds,
1162 			    atp + 1, tx, B_FALSE, 0, NULL);
1163 			dsl_dataset_rele(ds, FTAG);
1164 		}
1165 
1166 		if (error != 0) {
1167 			if (ddsa->ddsa_errors != NULL) {
1168 				fnvlist_add_int32(ddsa->ddsa_errors,
1169 				    name, error);
1170 			}
1171 			rv = error;
1172 		}
1173 	}
1174 
1175 	return (rv);
1176 }
1177 
1178 void
1179 dsl_dataset_snapshot_sync_impl(dsl_dataset_t *ds, const char *snapname,
1180     dmu_tx_t *tx)
1181 {
1182 	static zil_header_t zero_zil;
1183 
1184 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1185 	dmu_buf_t *dbuf;
1186 	dsl_dataset_phys_t *dsphys;
1187 	uint64_t dsobj, crtxg;
1188 	objset_t *mos = dp->dp_meta_objset;
1189 	objset_t *os;
1190 
1191 	ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
1192 
1193 	/*
1194 	 * If we are on an old pool, the zil must not be active, in which
1195 	 * case it will be zeroed.  Usually zil_suspend() accomplishes this.
1196 	 */
1197 	ASSERT(spa_version(dmu_tx_pool(tx)->dp_spa) >= SPA_VERSION_FAST_SNAP ||
1198 	    dmu_objset_from_ds(ds, &os) != 0 ||
1199 	    bcmp(&os->os_phys->os_zil_header, &zero_zil,
1200 	    sizeof (zero_zil)) == 0);
1201 
1202 	dsl_fs_ss_count_adjust(ds->ds_dir, 1, DD_FIELD_SNAPSHOT_COUNT, tx);
1203 
1204 	/*
1205 	 * The origin's ds_creation_txg has to be < TXG_INITIAL
1206 	 */
1207 	if (strcmp(snapname, ORIGIN_DIR_NAME) == 0)
1208 		crtxg = 1;
1209 	else
1210 		crtxg = tx->tx_txg;
1211 
1212 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
1213 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
1214 	VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
1215 	dmu_buf_will_dirty(dbuf, tx);
1216 	dsphys = dbuf->db_data;
1217 	bzero(dsphys, sizeof (dsl_dataset_phys_t));
1218 	dsphys->ds_dir_obj = ds->ds_dir->dd_object;
1219 	dsphys->ds_fsid_guid = unique_create();
1220 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
1221 	    sizeof (dsphys->ds_guid));
1222 	dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj;
1223 	dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg;
1224 	dsphys->ds_next_snap_obj = ds->ds_object;
1225 	dsphys->ds_num_children = 1;
1226 	dsphys->ds_creation_time = gethrestime_sec();
1227 	dsphys->ds_creation_txg = crtxg;
1228 	dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj;
1229 	dsphys->ds_referenced_bytes = ds->ds_phys->ds_referenced_bytes;
1230 	dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes;
1231 	dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes;
1232 	dsphys->ds_flags = ds->ds_phys->ds_flags;
1233 	dsphys->ds_bp = ds->ds_phys->ds_bp;
1234 	dmu_buf_rele(dbuf, FTAG);
1235 
1236 	if (ds->ds_large_blocks)
1237 		dsl_dataset_activate_large_blocks_sync_impl(dsobj, tx);
1238 
1239 	ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0);
1240 	if (ds->ds_prev) {
1241 		uint64_t next_clones_obj =
1242 		    ds->ds_prev->ds_phys->ds_next_clones_obj;
1243 		ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj ==
1244 		    ds->ds_object ||
1245 		    ds->ds_prev->ds_phys->ds_num_children > 1);
1246 		if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) {
1247 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1248 			ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
1249 			    ds->ds_prev->ds_phys->ds_creation_txg);
1250 			ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj;
1251 		} else if (next_clones_obj != 0) {
1252 			dsl_dataset_remove_from_next_clones(ds->ds_prev,
1253 			    dsphys->ds_next_snap_obj, tx);
1254 			VERIFY0(zap_add_int(mos,
1255 			    next_clones_obj, dsobj, tx));
1256 		}
1257 	}
1258 
1259 	/*
1260 	 * If we have a reference-reservation on this dataset, we will
1261 	 * need to increase the amount of refreservation being charged
1262 	 * since our unique space is going to zero.
1263 	 */
1264 	if (ds->ds_reserved) {
1265 		int64_t delta;
1266 		ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
1267 		delta = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
1268 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV,
1269 		    delta, 0, 0, tx);
1270 	}
1271 
1272 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1273 	ds->ds_phys->ds_deadlist_obj = dsl_deadlist_clone(&ds->ds_deadlist,
1274 	    UINT64_MAX, ds->ds_phys->ds_prev_snap_obj, tx);
1275 	dsl_deadlist_close(&ds->ds_deadlist);
1276 	dsl_deadlist_open(&ds->ds_deadlist, mos, ds->ds_phys->ds_deadlist_obj);
1277 	dsl_deadlist_add_key(&ds->ds_deadlist,
1278 	    ds->ds_phys->ds_prev_snap_txg, tx);
1279 
1280 	ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, tx->tx_txg);
1281 	ds->ds_phys->ds_prev_snap_obj = dsobj;
1282 	ds->ds_phys->ds_prev_snap_txg = crtxg;
1283 	ds->ds_phys->ds_unique_bytes = 0;
1284 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
1285 		ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1286 
1287 	VERIFY0(zap_add(mos, ds->ds_phys->ds_snapnames_zapobj,
1288 	    snapname, 8, 1, &dsobj, tx));
1289 
1290 	if (ds->ds_prev)
1291 		dsl_dataset_rele(ds->ds_prev, ds);
1292 	VERIFY0(dsl_dataset_hold_obj(dp,
1293 	    ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
1294 
1295 	dsl_scan_ds_snapshotted(ds, tx);
1296 
1297 	dsl_dir_snap_cmtime_update(ds->ds_dir);
1298 
1299 	spa_history_log_internal_ds(ds->ds_prev, "snapshot", tx, "");
1300 }
1301 
1302 static void
1303 dsl_dataset_snapshot_sync(void *arg, dmu_tx_t *tx)
1304 {
1305 	dsl_dataset_snapshot_arg_t *ddsa = arg;
1306 	dsl_pool_t *dp = dmu_tx_pool(tx);
1307 	nvpair_t *pair;
1308 
1309 	for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1310 	    pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1311 		dsl_dataset_t *ds;
1312 		char *name, *atp;
1313 		char dsname[MAXNAMELEN];
1314 
1315 		name = nvpair_name(pair);
1316 		atp = strchr(name, '@');
1317 		(void) strlcpy(dsname, name, atp - name + 1);
1318 		VERIFY0(dsl_dataset_hold(dp, dsname, FTAG, &ds));
1319 
1320 		dsl_dataset_snapshot_sync_impl(ds, atp + 1, tx);
1321 		if (ddsa->ddsa_props != NULL) {
1322 			dsl_props_set_sync_impl(ds->ds_prev,
1323 			    ZPROP_SRC_LOCAL, ddsa->ddsa_props, tx);
1324 		}
1325 		dsl_dataset_rele(ds, FTAG);
1326 	}
1327 }
1328 
1329 /*
1330  * The snapshots must all be in the same pool.
1331  * All-or-nothing: if there are any failures, nothing will be modified.
1332  */
1333 int
1334 dsl_dataset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors)
1335 {
1336 	dsl_dataset_snapshot_arg_t ddsa;
1337 	nvpair_t *pair;
1338 	boolean_t needsuspend;
1339 	int error;
1340 	spa_t *spa;
1341 	char *firstname;
1342 	nvlist_t *suspended = NULL;
1343 
1344 	pair = nvlist_next_nvpair(snaps, NULL);
1345 	if (pair == NULL)
1346 		return (0);
1347 	firstname = nvpair_name(pair);
1348 
1349 	error = spa_open(firstname, &spa, FTAG);
1350 	if (error != 0)
1351 		return (error);
1352 	needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
1353 	spa_close(spa, FTAG);
1354 
1355 	if (needsuspend) {
1356 		suspended = fnvlist_alloc();
1357 		for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1358 		    pair = nvlist_next_nvpair(snaps, pair)) {
1359 			char fsname[MAXNAMELEN];
1360 			char *snapname = nvpair_name(pair);
1361 			char *atp;
1362 			void *cookie;
1363 
1364 			atp = strchr(snapname, '@');
1365 			if (atp == NULL) {
1366 				error = SET_ERROR(EINVAL);
1367 				break;
1368 			}
1369 			(void) strlcpy(fsname, snapname, atp - snapname + 1);
1370 
1371 			error = zil_suspend(fsname, &cookie);
1372 			if (error != 0)
1373 				break;
1374 			fnvlist_add_uint64(suspended, fsname,
1375 			    (uintptr_t)cookie);
1376 		}
1377 	}
1378 
1379 	ddsa.ddsa_snaps = snaps;
1380 	ddsa.ddsa_props = props;
1381 	ddsa.ddsa_errors = errors;
1382 	ddsa.ddsa_cr = CRED();
1383 
1384 	if (error == 0) {
1385 		error = dsl_sync_task(firstname, dsl_dataset_snapshot_check,
1386 		    dsl_dataset_snapshot_sync, &ddsa,
1387 		    fnvlist_num_pairs(snaps) * 3, ZFS_SPACE_CHECK_NORMAL);
1388 	}
1389 
1390 	if (suspended != NULL) {
1391 		for (pair = nvlist_next_nvpair(suspended, NULL); pair != NULL;
1392 		    pair = nvlist_next_nvpair(suspended, pair)) {
1393 			zil_resume((void *)(uintptr_t)
1394 			    fnvpair_value_uint64(pair));
1395 		}
1396 		fnvlist_free(suspended);
1397 	}
1398 
1399 	return (error);
1400 }
1401 
1402 typedef struct dsl_dataset_snapshot_tmp_arg {
1403 	const char *ddsta_fsname;
1404 	const char *ddsta_snapname;
1405 	minor_t ddsta_cleanup_minor;
1406 	const char *ddsta_htag;
1407 } dsl_dataset_snapshot_tmp_arg_t;
1408 
1409 static int
1410 dsl_dataset_snapshot_tmp_check(void *arg, dmu_tx_t *tx)
1411 {
1412 	dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
1413 	dsl_pool_t *dp = dmu_tx_pool(tx);
1414 	dsl_dataset_t *ds;
1415 	int error;
1416 
1417 	error = dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds);
1418 	if (error != 0)
1419 		return (error);
1420 
1421 	/* NULL cred means no limit check for tmp snapshot */
1422 	error = dsl_dataset_snapshot_check_impl(ds, ddsta->ddsta_snapname,
1423 	    tx, B_FALSE, 0, NULL);
1424 	if (error != 0) {
1425 		dsl_dataset_rele(ds, FTAG);
1426 		return (error);
1427 	}
1428 
1429 	if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS) {
1430 		dsl_dataset_rele(ds, FTAG);
1431 		return (SET_ERROR(ENOTSUP));
1432 	}
1433 	error = dsl_dataset_user_hold_check_one(NULL, ddsta->ddsta_htag,
1434 	    B_TRUE, tx);
1435 	if (error != 0) {
1436 		dsl_dataset_rele(ds, FTAG);
1437 		return (error);
1438 	}
1439 
1440 	dsl_dataset_rele(ds, FTAG);
1441 	return (0);
1442 }
1443 
1444 static void
1445 dsl_dataset_snapshot_tmp_sync(void *arg, dmu_tx_t *tx)
1446 {
1447 	dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
1448 	dsl_pool_t *dp = dmu_tx_pool(tx);
1449 	dsl_dataset_t *ds;
1450 
1451 	VERIFY0(dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds));
1452 
1453 	dsl_dataset_snapshot_sync_impl(ds, ddsta->ddsta_snapname, tx);
1454 	dsl_dataset_user_hold_sync_one(ds->ds_prev, ddsta->ddsta_htag,
1455 	    ddsta->ddsta_cleanup_minor, gethrestime_sec(), tx);
1456 	dsl_destroy_snapshot_sync_impl(ds->ds_prev, B_TRUE, tx);
1457 
1458 	dsl_dataset_rele(ds, FTAG);
1459 }
1460 
1461 int
1462 dsl_dataset_snapshot_tmp(const char *fsname, const char *snapname,
1463     minor_t cleanup_minor, const char *htag)
1464 {
1465 	dsl_dataset_snapshot_tmp_arg_t ddsta;
1466 	int error;
1467 	spa_t *spa;
1468 	boolean_t needsuspend;
1469 	void *cookie;
1470 
1471 	ddsta.ddsta_fsname = fsname;
1472 	ddsta.ddsta_snapname = snapname;
1473 	ddsta.ddsta_cleanup_minor = cleanup_minor;
1474 	ddsta.ddsta_htag = htag;
1475 
1476 	error = spa_open(fsname, &spa, FTAG);
1477 	if (error != 0)
1478 		return (error);
1479 	needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
1480 	spa_close(spa, FTAG);
1481 
1482 	if (needsuspend) {
1483 		error = zil_suspend(fsname, &cookie);
1484 		if (error != 0)
1485 			return (error);
1486 	}
1487 
1488 	error = dsl_sync_task(fsname, dsl_dataset_snapshot_tmp_check,
1489 	    dsl_dataset_snapshot_tmp_sync, &ddsta, 3, ZFS_SPACE_CHECK_RESERVED);
1490 
1491 	if (needsuspend)
1492 		zil_resume(cookie);
1493 	return (error);
1494 }
1495 
1496 
1497 void
1498 dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
1499 {
1500 	ASSERT(dmu_tx_is_syncing(tx));
1501 	ASSERT(ds->ds_objset != NULL);
1502 	ASSERT(ds->ds_phys->ds_next_snap_obj == 0);
1503 
1504 	/*
1505 	 * in case we had to change ds_fsid_guid when we opened it,
1506 	 * sync it out now.
1507 	 */
1508 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1509 	ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid;
1510 
1511 	dmu_objset_sync(ds->ds_objset, zio, tx);
1512 
1513 	if (ds->ds_need_large_blocks && !ds->ds_large_blocks) {
1514 		dsl_dataset_activate_large_blocks_sync_impl(ds->ds_object, tx);
1515 		ds->ds_large_blocks = B_TRUE;
1516 	}
1517 }
1518 
1519 static void
1520 get_clones_stat(dsl_dataset_t *ds, nvlist_t *nv)
1521 {
1522 	uint64_t count = 0;
1523 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
1524 	zap_cursor_t zc;
1525 	zap_attribute_t za;
1526 	nvlist_t *propval = fnvlist_alloc();
1527 	nvlist_t *val = fnvlist_alloc();
1528 
1529 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
1530 
1531 	/*
1532 	 * There may be missing entries in ds_next_clones_obj
1533 	 * due to a bug in a previous version of the code.
1534 	 * Only trust it if it has the right number of entries.
1535 	 */
1536 	if (ds->ds_phys->ds_next_clones_obj != 0) {
1537 		VERIFY0(zap_count(mos, ds->ds_phys->ds_next_clones_obj,
1538 		    &count));
1539 	}
1540 	if (count != ds->ds_phys->ds_num_children - 1)
1541 		goto fail;
1542 	for (zap_cursor_init(&zc, mos, ds->ds_phys->ds_next_clones_obj);
1543 	    zap_cursor_retrieve(&zc, &za) == 0;
1544 	    zap_cursor_advance(&zc)) {
1545 		dsl_dataset_t *clone;
1546 		char buf[ZFS_MAXNAMELEN];
1547 		VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
1548 		    za.za_first_integer, FTAG, &clone));
1549 		dsl_dir_name(clone->ds_dir, buf);
1550 		fnvlist_add_boolean(val, buf);
1551 		dsl_dataset_rele(clone, FTAG);
1552 	}
1553 	zap_cursor_fini(&zc);
1554 	fnvlist_add_nvlist(propval, ZPROP_VALUE, val);
1555 	fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES), propval);
1556 fail:
1557 	nvlist_free(val);
1558 	nvlist_free(propval);
1559 }
1560 
1561 void
1562 dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
1563 {
1564 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1565 	uint64_t refd, avail, uobjs, aobjs, ratio;
1566 
1567 	ASSERT(dsl_pool_config_held(dp));
1568 
1569 	ratio = ds->ds_phys->ds_compressed_bytes == 0 ? 100 :
1570 	    (ds->ds_phys->ds_uncompressed_bytes * 100 /
1571 	    ds->ds_phys->ds_compressed_bytes);
1572 
1573 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO, ratio);
1574 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALREFERENCED,
1575 	    ds->ds_phys->ds_uncompressed_bytes);
1576 
1577 	if (dsl_dataset_is_snapshot(ds)) {
1578 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, ratio);
1579 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
1580 		    ds->ds_phys->ds_unique_bytes);
1581 		get_clones_stat(ds, nv);
1582 	} else {
1583 		if (ds->ds_prev != NULL && ds->ds_prev != dp->dp_origin_snap) {
1584 			char buf[MAXNAMELEN];
1585 			dsl_dataset_name(ds->ds_prev, buf);
1586 			dsl_prop_nvlist_add_string(nv, ZFS_PROP_PREV_SNAP, buf);
1587 		}
1588 
1589 		dsl_dir_stats(ds->ds_dir, nv);
1590 	}
1591 
1592 	dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs);
1593 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail);
1594 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd);
1595 
1596 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
1597 	    ds->ds_phys->ds_creation_time);
1598 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
1599 	    ds->ds_phys->ds_creation_txg);
1600 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA,
1601 	    ds->ds_quota);
1602 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION,
1603 	    ds->ds_reserved);
1604 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID,
1605 	    ds->ds_phys->ds_guid);
1606 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE,
1607 	    ds->ds_phys->ds_unique_bytes);
1608 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID,
1609 	    ds->ds_object);
1610 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS,
1611 	    ds->ds_userrefs);
1612 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY,
1613 	    DS_IS_DEFER_DESTROY(ds) ? 1 : 0);
1614 
1615 	if (ds->ds_phys->ds_prev_snap_obj != 0) {
1616 		uint64_t written, comp, uncomp;
1617 		dsl_pool_t *dp = ds->ds_dir->dd_pool;
1618 		dsl_dataset_t *prev;
1619 
1620 		int err = dsl_dataset_hold_obj(dp,
1621 		    ds->ds_phys->ds_prev_snap_obj, FTAG, &prev);
1622 		if (err == 0) {
1623 			err = dsl_dataset_space_written(prev, ds, &written,
1624 			    &comp, &uncomp);
1625 			dsl_dataset_rele(prev, FTAG);
1626 			if (err == 0) {
1627 				dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN,
1628 				    written);
1629 			}
1630 		}
1631 	}
1632 }
1633 
1634 void
1635 dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
1636 {
1637 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1638 	ASSERT(dsl_pool_config_held(dp));
1639 
1640 	stat->dds_creation_txg = ds->ds_phys->ds_creation_txg;
1641 	stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT;
1642 	stat->dds_guid = ds->ds_phys->ds_guid;
1643 	stat->dds_origin[0] = '\0';
1644 	if (dsl_dataset_is_snapshot(ds)) {
1645 		stat->dds_is_snapshot = B_TRUE;
1646 		stat->dds_num_clones = ds->ds_phys->ds_num_children - 1;
1647 	} else {
1648 		stat->dds_is_snapshot = B_FALSE;
1649 		stat->dds_num_clones = 0;
1650 
1651 		if (dsl_dir_is_clone(ds->ds_dir)) {
1652 			dsl_dataset_t *ods;
1653 
1654 			VERIFY0(dsl_dataset_hold_obj(dp,
1655 			    ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &ods));
1656 			dsl_dataset_name(ods, stat->dds_origin);
1657 			dsl_dataset_rele(ods, FTAG);
1658 		}
1659 	}
1660 }
1661 
1662 uint64_t
1663 dsl_dataset_fsid_guid(dsl_dataset_t *ds)
1664 {
1665 	return (ds->ds_fsid_guid);
1666 }
1667 
1668 void
1669 dsl_dataset_space(dsl_dataset_t *ds,
1670     uint64_t *refdbytesp, uint64_t *availbytesp,
1671     uint64_t *usedobjsp, uint64_t *availobjsp)
1672 {
1673 	*refdbytesp = ds->ds_phys->ds_referenced_bytes;
1674 	*availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
1675 	if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes)
1676 		*availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes;
1677 	if (ds->ds_quota != 0) {
1678 		/*
1679 		 * Adjust available bytes according to refquota
1680 		 */
1681 		if (*refdbytesp < ds->ds_quota)
1682 			*availbytesp = MIN(*availbytesp,
1683 			    ds->ds_quota - *refdbytesp);
1684 		else
1685 			*availbytesp = 0;
1686 	}
1687 	*usedobjsp = BP_GET_FILL(&ds->ds_phys->ds_bp);
1688 	*availobjsp = DN_MAX_OBJECT - *usedobjsp;
1689 }
1690 
1691 boolean_t
1692 dsl_dataset_modified_since_snap(dsl_dataset_t *ds, dsl_dataset_t *snap)
1693 {
1694 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1695 
1696 	ASSERT(dsl_pool_config_held(dp));
1697 	if (snap == NULL)
1698 		return (B_FALSE);
1699 	if (ds->ds_phys->ds_bp.blk_birth >
1700 	    snap->ds_phys->ds_creation_txg) {
1701 		objset_t *os, *os_snap;
1702 		/*
1703 		 * It may be that only the ZIL differs, because it was
1704 		 * reset in the head.  Don't count that as being
1705 		 * modified.
1706 		 */
1707 		if (dmu_objset_from_ds(ds, &os) != 0)
1708 			return (B_TRUE);
1709 		if (dmu_objset_from_ds(snap, &os_snap) != 0)
1710 			return (B_TRUE);
1711 		return (bcmp(&os->os_phys->os_meta_dnode,
1712 		    &os_snap->os_phys->os_meta_dnode,
1713 		    sizeof (os->os_phys->os_meta_dnode)) != 0);
1714 	}
1715 	return (B_FALSE);
1716 }
1717 
1718 typedef struct dsl_dataset_rename_snapshot_arg {
1719 	const char *ddrsa_fsname;
1720 	const char *ddrsa_oldsnapname;
1721 	const char *ddrsa_newsnapname;
1722 	boolean_t ddrsa_recursive;
1723 	dmu_tx_t *ddrsa_tx;
1724 } dsl_dataset_rename_snapshot_arg_t;
1725 
1726 /* ARGSUSED */
1727 static int
1728 dsl_dataset_rename_snapshot_check_impl(dsl_pool_t *dp,
1729     dsl_dataset_t *hds, void *arg)
1730 {
1731 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
1732 	int error;
1733 	uint64_t val;
1734 
1735 	error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
1736 	if (error != 0) {
1737 		/* ignore nonexistent snapshots */
1738 		return (error == ENOENT ? 0 : error);
1739 	}
1740 
1741 	/* new name should not exist */
1742 	error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_newsnapname, &val);
1743 	if (error == 0)
1744 		error = SET_ERROR(EEXIST);
1745 	else if (error == ENOENT)
1746 		error = 0;
1747 
1748 	/* dataset name + 1 for the "@" + the new snapshot name must fit */
1749 	if (dsl_dir_namelen(hds->ds_dir) + 1 +
1750 	    strlen(ddrsa->ddrsa_newsnapname) >= MAXNAMELEN)
1751 		error = SET_ERROR(ENAMETOOLONG);
1752 
1753 	return (error);
1754 }
1755 
1756 static int
1757 dsl_dataset_rename_snapshot_check(void *arg, dmu_tx_t *tx)
1758 {
1759 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
1760 	dsl_pool_t *dp = dmu_tx_pool(tx);
1761 	dsl_dataset_t *hds;
1762 	int error;
1763 
1764 	error = dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds);
1765 	if (error != 0)
1766 		return (error);
1767 
1768 	if (ddrsa->ddrsa_recursive) {
1769 		error = dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
1770 		    dsl_dataset_rename_snapshot_check_impl, ddrsa,
1771 		    DS_FIND_CHILDREN);
1772 	} else {
1773 		error = dsl_dataset_rename_snapshot_check_impl(dp, hds, ddrsa);
1774 	}
1775 	dsl_dataset_rele(hds, FTAG);
1776 	return (error);
1777 }
1778 
1779 static int
1780 dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t *dp,
1781     dsl_dataset_t *hds, void *arg)
1782 {
1783 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
1784 	dsl_dataset_t *ds;
1785 	uint64_t val;
1786 	dmu_tx_t *tx = ddrsa->ddrsa_tx;
1787 	int error;
1788 
1789 	error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
1790 	ASSERT(error == 0 || error == ENOENT);
1791 	if (error == ENOENT) {
1792 		/* ignore nonexistent snapshots */
1793 		return (0);
1794 	}
1795 
1796 	VERIFY0(dsl_dataset_hold_obj(dp, val, FTAG, &ds));
1797 
1798 	/* log before we change the name */
1799 	spa_history_log_internal_ds(ds, "rename", tx,
1800 	    "-> @%s", ddrsa->ddrsa_newsnapname);
1801 
1802 	VERIFY0(dsl_dataset_snap_remove(hds, ddrsa->ddrsa_oldsnapname, tx,
1803 	    B_FALSE));
1804 	mutex_enter(&ds->ds_lock);
1805 	(void) strcpy(ds->ds_snapname, ddrsa->ddrsa_newsnapname);
1806 	mutex_exit(&ds->ds_lock);
1807 	VERIFY0(zap_add(dp->dp_meta_objset, hds->ds_phys->ds_snapnames_zapobj,
1808 	    ds->ds_snapname, 8, 1, &ds->ds_object, tx));
1809 
1810 	dsl_dataset_rele(ds, FTAG);
1811 	return (0);
1812 }
1813 
1814 static void
1815 dsl_dataset_rename_snapshot_sync(void *arg, dmu_tx_t *tx)
1816 {
1817 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
1818 	dsl_pool_t *dp = dmu_tx_pool(tx);
1819 	dsl_dataset_t *hds;
1820 
1821 	VERIFY0(dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds));
1822 	ddrsa->ddrsa_tx = tx;
1823 	if (ddrsa->ddrsa_recursive) {
1824 		VERIFY0(dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
1825 		    dsl_dataset_rename_snapshot_sync_impl, ddrsa,
1826 		    DS_FIND_CHILDREN));
1827 	} else {
1828 		VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp, hds, ddrsa));
1829 	}
1830 	dsl_dataset_rele(hds, FTAG);
1831 }
1832 
1833 int
1834 dsl_dataset_rename_snapshot(const char *fsname,
1835     const char *oldsnapname, const char *newsnapname, boolean_t recursive)
1836 {
1837 	dsl_dataset_rename_snapshot_arg_t ddrsa;
1838 
1839 	ddrsa.ddrsa_fsname = fsname;
1840 	ddrsa.ddrsa_oldsnapname = oldsnapname;
1841 	ddrsa.ddrsa_newsnapname = newsnapname;
1842 	ddrsa.ddrsa_recursive = recursive;
1843 
1844 	return (dsl_sync_task(fsname, dsl_dataset_rename_snapshot_check,
1845 	    dsl_dataset_rename_snapshot_sync, &ddrsa,
1846 	    1, ZFS_SPACE_CHECK_RESERVED));
1847 }
1848 
1849 /*
1850  * If we're doing an ownership handoff, we need to make sure that there is
1851  * only one long hold on the dataset.  We're not allowed to change anything here
1852  * so we don't permanently release the long hold or regular hold here.  We want
1853  * to do this only when syncing to avoid the dataset unexpectedly going away
1854  * when we release the long hold.
1855  */
1856 static int
1857 dsl_dataset_handoff_check(dsl_dataset_t *ds, void *owner, dmu_tx_t *tx)
1858 {
1859 	boolean_t held;
1860 
1861 	if (!dmu_tx_is_syncing(tx))
1862 		return (0);
1863 
1864 	if (owner != NULL) {
1865 		VERIFY3P(ds->ds_owner, ==, owner);
1866 		dsl_dataset_long_rele(ds, owner);
1867 	}
1868 
1869 	held = dsl_dataset_long_held(ds);
1870 
1871 	if (owner != NULL)
1872 		dsl_dataset_long_hold(ds, owner);
1873 
1874 	if (held)
1875 		return (SET_ERROR(EBUSY));
1876 
1877 	return (0);
1878 }
1879 
1880 typedef struct dsl_dataset_rollback_arg {
1881 	const char *ddra_fsname;
1882 	void *ddra_owner;
1883 	nvlist_t *ddra_result;
1884 } dsl_dataset_rollback_arg_t;
1885 
1886 static int
1887 dsl_dataset_rollback_check(void *arg, dmu_tx_t *tx)
1888 {
1889 	dsl_dataset_rollback_arg_t *ddra = arg;
1890 	dsl_pool_t *dp = dmu_tx_pool(tx);
1891 	dsl_dataset_t *ds;
1892 	int64_t unused_refres_delta;
1893 	int error;
1894 
1895 	error = dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds);
1896 	if (error != 0)
1897 		return (error);
1898 
1899 	/* must not be a snapshot */
1900 	if (dsl_dataset_is_snapshot(ds)) {
1901 		dsl_dataset_rele(ds, FTAG);
1902 		return (SET_ERROR(EINVAL));
1903 	}
1904 
1905 	/* must have a most recent snapshot */
1906 	if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) {
1907 		dsl_dataset_rele(ds, FTAG);
1908 		return (SET_ERROR(EINVAL));
1909 	}
1910 
1911 	/* must not have any bookmarks after the most recent snapshot */
1912 	nvlist_t *proprequest = fnvlist_alloc();
1913 	fnvlist_add_boolean(proprequest, zfs_prop_to_name(ZFS_PROP_CREATETXG));
1914 	nvlist_t *bookmarks = fnvlist_alloc();
1915 	error = dsl_get_bookmarks_impl(ds, proprequest, bookmarks);
1916 	fnvlist_free(proprequest);
1917 	if (error != 0)
1918 		return (error);
1919 	for (nvpair_t *pair = nvlist_next_nvpair(bookmarks, NULL);
1920 	    pair != NULL; pair = nvlist_next_nvpair(bookmarks, pair)) {
1921 		nvlist_t *valuenv =
1922 		    fnvlist_lookup_nvlist(fnvpair_value_nvlist(pair),
1923 		    zfs_prop_to_name(ZFS_PROP_CREATETXG));
1924 		uint64_t createtxg = fnvlist_lookup_uint64(valuenv, "value");
1925 		if (createtxg > ds->ds_phys->ds_prev_snap_txg) {
1926 			fnvlist_free(bookmarks);
1927 			dsl_dataset_rele(ds, FTAG);
1928 			return (SET_ERROR(EEXIST));
1929 		}
1930 	}
1931 	fnvlist_free(bookmarks);
1932 
1933 	error = dsl_dataset_handoff_check(ds, ddra->ddra_owner, tx);
1934 	if (error != 0) {
1935 		dsl_dataset_rele(ds, FTAG);
1936 		return (error);
1937 	}
1938 
1939 	/*
1940 	 * Check if the snap we are rolling back to uses more than
1941 	 * the refquota.
1942 	 */
1943 	if (ds->ds_quota != 0 &&
1944 	    ds->ds_prev->ds_phys->ds_referenced_bytes > ds->ds_quota) {
1945 		dsl_dataset_rele(ds, FTAG);
1946 		return (SET_ERROR(EDQUOT));
1947 	}
1948 
1949 	/*
1950 	 * When we do the clone swap, we will temporarily use more space
1951 	 * due to the refreservation (the head will no longer have any
1952 	 * unique space, so the entire amount of the refreservation will need
1953 	 * to be free).  We will immediately destroy the clone, freeing
1954 	 * this space, but the freeing happens over many txg's.
1955 	 */
1956 	unused_refres_delta = (int64_t)MIN(ds->ds_reserved,
1957 	    ds->ds_phys->ds_unique_bytes);
1958 
1959 	if (unused_refres_delta > 0 &&
1960 	    unused_refres_delta >
1961 	    dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) {
1962 		dsl_dataset_rele(ds, FTAG);
1963 		return (SET_ERROR(ENOSPC));
1964 	}
1965 
1966 	dsl_dataset_rele(ds, FTAG);
1967 	return (0);
1968 }
1969 
1970 static void
1971 dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx)
1972 {
1973 	dsl_dataset_rollback_arg_t *ddra = arg;
1974 	dsl_pool_t *dp = dmu_tx_pool(tx);
1975 	dsl_dataset_t *ds, *clone;
1976 	uint64_t cloneobj;
1977 	char namebuf[ZFS_MAXNAMELEN];
1978 
1979 	VERIFY0(dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds));
1980 
1981 	dsl_dataset_name(ds->ds_prev, namebuf);
1982 	fnvlist_add_string(ddra->ddra_result, "target", namebuf);
1983 
1984 	cloneobj = dsl_dataset_create_sync(ds->ds_dir, "%rollback",
1985 	    ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, tx);
1986 
1987 	VERIFY0(dsl_dataset_hold_obj(dp, cloneobj, FTAG, &clone));
1988 
1989 	dsl_dataset_clone_swap_sync_impl(clone, ds, tx);
1990 	dsl_dataset_zero_zil(ds, tx);
1991 
1992 	dsl_destroy_head_sync_impl(clone, tx);
1993 
1994 	dsl_dataset_rele(clone, FTAG);
1995 	dsl_dataset_rele(ds, FTAG);
1996 }
1997 
1998 /*
1999  * Rolls back the given filesystem or volume to the most recent snapshot.
2000  * The name of the most recent snapshot will be returned under key "target"
2001  * in the result nvlist.
2002  *
2003  * If owner != NULL:
2004  * - The existing dataset MUST be owned by the specified owner at entry
2005  * - Upon return, dataset will still be held by the same owner, whether we
2006  *   succeed or not.
2007  *
2008  * This mode is required any time the existing filesystem is mounted.  See
2009  * notes above zfs_suspend_fs() for further details.
2010  */
2011 int
2012 dsl_dataset_rollback(const char *fsname, void *owner, nvlist_t *result)
2013 {
2014 	dsl_dataset_rollback_arg_t ddra;
2015 
2016 	ddra.ddra_fsname = fsname;
2017 	ddra.ddra_owner = owner;
2018 	ddra.ddra_result = result;
2019 
2020 	return (dsl_sync_task(fsname, dsl_dataset_rollback_check,
2021 	    dsl_dataset_rollback_sync, &ddra,
2022 	    1, ZFS_SPACE_CHECK_RESERVED));
2023 }
2024 
2025 struct promotenode {
2026 	list_node_t link;
2027 	dsl_dataset_t *ds;
2028 };
2029 
2030 typedef struct dsl_dataset_promote_arg {
2031 	const char *ddpa_clonename;
2032 	dsl_dataset_t *ddpa_clone;
2033 	list_t shared_snaps, origin_snaps, clone_snaps;
2034 	dsl_dataset_t *origin_origin; /* origin of the origin */
2035 	uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap;
2036 	char *err_ds;
2037 	cred_t *cr;
2038 } dsl_dataset_promote_arg_t;
2039 
2040 static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep);
2041 static int promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp,
2042     void *tag);
2043 static void promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag);
2044 
2045 static int
2046 dsl_dataset_promote_check(void *arg, dmu_tx_t *tx)
2047 {
2048 	dsl_dataset_promote_arg_t *ddpa = arg;
2049 	dsl_pool_t *dp = dmu_tx_pool(tx);
2050 	dsl_dataset_t *hds;
2051 	struct promotenode *snap;
2052 	dsl_dataset_t *origin_ds;
2053 	int err;
2054 	uint64_t unused;
2055 	uint64_t ss_mv_cnt;
2056 
2057 	err = promote_hold(ddpa, dp, FTAG);
2058 	if (err != 0)
2059 		return (err);
2060 
2061 	hds = ddpa->ddpa_clone;
2062 
2063 	if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE) {
2064 		promote_rele(ddpa, FTAG);
2065 		return (SET_ERROR(EXDEV));
2066 	}
2067 
2068 	/*
2069 	 * Compute and check the amount of space to transfer.  Since this is
2070 	 * so expensive, don't do the preliminary check.
2071 	 */
2072 	if (!dmu_tx_is_syncing(tx)) {
2073 		promote_rele(ddpa, FTAG);
2074 		return (0);
2075 	}
2076 
2077 	snap = list_head(&ddpa->shared_snaps);
2078 	origin_ds = snap->ds;
2079 
2080 	/* compute origin's new unique space */
2081 	snap = list_tail(&ddpa->clone_snaps);
2082 	ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
2083 	dsl_deadlist_space_range(&snap->ds->ds_deadlist,
2084 	    origin_ds->ds_phys->ds_prev_snap_txg, UINT64_MAX,
2085 	    &ddpa->unique, &unused, &unused);
2086 
2087 	/*
2088 	 * Walk the snapshots that we are moving
2089 	 *
2090 	 * Compute space to transfer.  Consider the incremental changes
2091 	 * to used by each snapshot:
2092 	 * (my used) = (prev's used) + (blocks born) - (blocks killed)
2093 	 * So each snapshot gave birth to:
2094 	 * (blocks born) = (my used) - (prev's used) + (blocks killed)
2095 	 * So a sequence would look like:
2096 	 * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0)
2097 	 * Which simplifies to:
2098 	 * uN + kN + kN-1 + ... + k1 + k0
2099 	 * Note however, if we stop before we reach the ORIGIN we get:
2100 	 * uN + kN + kN-1 + ... + kM - uM-1
2101 	 */
2102 	ss_mv_cnt = 0;
2103 	ddpa->used = origin_ds->ds_phys->ds_referenced_bytes;
2104 	ddpa->comp = origin_ds->ds_phys->ds_compressed_bytes;
2105 	ddpa->uncomp = origin_ds->ds_phys->ds_uncompressed_bytes;
2106 	for (snap = list_head(&ddpa->shared_snaps); snap;
2107 	    snap = list_next(&ddpa->shared_snaps, snap)) {
2108 		uint64_t val, dlused, dlcomp, dluncomp;
2109 		dsl_dataset_t *ds = snap->ds;
2110 
2111 		ss_mv_cnt++;
2112 
2113 		/*
2114 		 * If there are long holds, we won't be able to evict
2115 		 * the objset.
2116 		 */
2117 		if (dsl_dataset_long_held(ds)) {
2118 			err = SET_ERROR(EBUSY);
2119 			goto out;
2120 		}
2121 
2122 		/* Check that the snapshot name does not conflict */
2123 		VERIFY0(dsl_dataset_get_snapname(ds));
2124 		err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val);
2125 		if (err == 0) {
2126 			(void) strcpy(ddpa->err_ds, snap->ds->ds_snapname);
2127 			err = SET_ERROR(EEXIST);
2128 			goto out;
2129 		}
2130 		if (err != ENOENT)
2131 			goto out;
2132 
2133 		/* The very first snapshot does not have a deadlist */
2134 		if (ds->ds_phys->ds_prev_snap_obj == 0)
2135 			continue;
2136 
2137 		dsl_deadlist_space(&ds->ds_deadlist,
2138 		    &dlused, &dlcomp, &dluncomp);
2139 		ddpa->used += dlused;
2140 		ddpa->comp += dlcomp;
2141 		ddpa->uncomp += dluncomp;
2142 	}
2143 
2144 	/*
2145 	 * If we are a clone of a clone then we never reached ORIGIN,
2146 	 * so we need to subtract out the clone origin's used space.
2147 	 */
2148 	if (ddpa->origin_origin) {
2149 		ddpa->used -= ddpa->origin_origin->ds_phys->ds_referenced_bytes;
2150 		ddpa->comp -= ddpa->origin_origin->ds_phys->ds_compressed_bytes;
2151 		ddpa->uncomp -=
2152 		    ddpa->origin_origin->ds_phys->ds_uncompressed_bytes;
2153 	}
2154 
2155 	/* Check that there is enough space and limit headroom here */
2156 	err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir,
2157 	    0, ss_mv_cnt, ddpa->used, ddpa->cr);
2158 	if (err != 0)
2159 		goto out;
2160 
2161 	/*
2162 	 * Compute the amounts of space that will be used by snapshots
2163 	 * after the promotion (for both origin and clone).  For each,
2164 	 * it is the amount of space that will be on all of their
2165 	 * deadlists (that was not born before their new origin).
2166 	 */
2167 	if (hds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
2168 		uint64_t space;
2169 
2170 		/*
2171 		 * Note, typically this will not be a clone of a clone,
2172 		 * so dd_origin_txg will be < TXG_INITIAL, so
2173 		 * these snaplist_space() -> dsl_deadlist_space_range()
2174 		 * calls will be fast because they do not have to
2175 		 * iterate over all bps.
2176 		 */
2177 		snap = list_head(&ddpa->origin_snaps);
2178 		err = snaplist_space(&ddpa->shared_snaps,
2179 		    snap->ds->ds_dir->dd_origin_txg, &ddpa->cloneusedsnap);
2180 		if (err != 0)
2181 			goto out;
2182 
2183 		err = snaplist_space(&ddpa->clone_snaps,
2184 		    snap->ds->ds_dir->dd_origin_txg, &space);
2185 		if (err != 0)
2186 			goto out;
2187 		ddpa->cloneusedsnap += space;
2188 	}
2189 	if (origin_ds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
2190 		err = snaplist_space(&ddpa->origin_snaps,
2191 		    origin_ds->ds_phys->ds_creation_txg, &ddpa->originusedsnap);
2192 		if (err != 0)
2193 			goto out;
2194 	}
2195 
2196 out:
2197 	promote_rele(ddpa, FTAG);
2198 	return (err);
2199 }
2200 
2201 static void
2202 dsl_dataset_promote_sync(void *arg, dmu_tx_t *tx)
2203 {
2204 	dsl_dataset_promote_arg_t *ddpa = arg;
2205 	dsl_pool_t *dp = dmu_tx_pool(tx);
2206 	dsl_dataset_t *hds;
2207 	struct promotenode *snap;
2208 	dsl_dataset_t *origin_ds;
2209 	dsl_dataset_t *origin_head;
2210 	dsl_dir_t *dd;
2211 	dsl_dir_t *odd = NULL;
2212 	uint64_t oldnext_obj;
2213 	int64_t delta;
2214 
2215 	VERIFY0(promote_hold(ddpa, dp, FTAG));
2216 	hds = ddpa->ddpa_clone;
2217 
2218 	ASSERT0(hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE);
2219 
2220 	snap = list_head(&ddpa->shared_snaps);
2221 	origin_ds = snap->ds;
2222 	dd = hds->ds_dir;
2223 
2224 	snap = list_head(&ddpa->origin_snaps);
2225 	origin_head = snap->ds;
2226 
2227 	/*
2228 	 * We need to explicitly open odd, since origin_ds's dd will be
2229 	 * changing.
2230 	 */
2231 	VERIFY0(dsl_dir_hold_obj(dp, origin_ds->ds_dir->dd_object,
2232 	    NULL, FTAG, &odd));
2233 
2234 	/* change origin's next snap */
2235 	dmu_buf_will_dirty(origin_ds->ds_dbuf, tx);
2236 	oldnext_obj = origin_ds->ds_phys->ds_next_snap_obj;
2237 	snap = list_tail(&ddpa->clone_snaps);
2238 	ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
2239 	origin_ds->ds_phys->ds_next_snap_obj = snap->ds->ds_object;
2240 
2241 	/* change the origin's next clone */
2242 	if (origin_ds->ds_phys->ds_next_clones_obj) {
2243 		dsl_dataset_remove_from_next_clones(origin_ds,
2244 		    snap->ds->ds_object, tx);
2245 		VERIFY0(zap_add_int(dp->dp_meta_objset,
2246 		    origin_ds->ds_phys->ds_next_clones_obj,
2247 		    oldnext_obj, tx));
2248 	}
2249 
2250 	/* change origin */
2251 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
2252 	ASSERT3U(dd->dd_phys->dd_origin_obj, ==, origin_ds->ds_object);
2253 	dd->dd_phys->dd_origin_obj = odd->dd_phys->dd_origin_obj;
2254 	dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg;
2255 	dmu_buf_will_dirty(odd->dd_dbuf, tx);
2256 	odd->dd_phys->dd_origin_obj = origin_ds->ds_object;
2257 	origin_head->ds_dir->dd_origin_txg =
2258 	    origin_ds->ds_phys->ds_creation_txg;
2259 
2260 	/* change dd_clone entries */
2261 	if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
2262 		VERIFY0(zap_remove_int(dp->dp_meta_objset,
2263 		    odd->dd_phys->dd_clones, hds->ds_object, tx));
2264 		VERIFY0(zap_add_int(dp->dp_meta_objset,
2265 		    ddpa->origin_origin->ds_dir->dd_phys->dd_clones,
2266 		    hds->ds_object, tx));
2267 
2268 		VERIFY0(zap_remove_int(dp->dp_meta_objset,
2269 		    ddpa->origin_origin->ds_dir->dd_phys->dd_clones,
2270 		    origin_head->ds_object, tx));
2271 		if (dd->dd_phys->dd_clones == 0) {
2272 			dd->dd_phys->dd_clones = zap_create(dp->dp_meta_objset,
2273 			    DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
2274 		}
2275 		VERIFY0(zap_add_int(dp->dp_meta_objset,
2276 		    dd->dd_phys->dd_clones, origin_head->ds_object, tx));
2277 	}
2278 
2279 	/* move snapshots to this dir */
2280 	for (snap = list_head(&ddpa->shared_snaps); snap;
2281 	    snap = list_next(&ddpa->shared_snaps, snap)) {
2282 		dsl_dataset_t *ds = snap->ds;
2283 
2284 		/*
2285 		 * Property callbacks are registered to a particular
2286 		 * dsl_dir.  Since ours is changing, evict the objset
2287 		 * so that they will be unregistered from the old dsl_dir.
2288 		 */
2289 		if (ds->ds_objset) {
2290 			dmu_objset_evict(ds->ds_objset);
2291 			ds->ds_objset = NULL;
2292 		}
2293 
2294 		/* move snap name entry */
2295 		VERIFY0(dsl_dataset_get_snapname(ds));
2296 		VERIFY0(dsl_dataset_snap_remove(origin_head,
2297 		    ds->ds_snapname, tx, B_TRUE));
2298 		VERIFY0(zap_add(dp->dp_meta_objset,
2299 		    hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname,
2300 		    8, 1, &ds->ds_object, tx));
2301 		dsl_fs_ss_count_adjust(hds->ds_dir, 1,
2302 		    DD_FIELD_SNAPSHOT_COUNT, tx);
2303 
2304 		/* change containing dsl_dir */
2305 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
2306 		ASSERT3U(ds->ds_phys->ds_dir_obj, ==, odd->dd_object);
2307 		ds->ds_phys->ds_dir_obj = dd->dd_object;
2308 		ASSERT3P(ds->ds_dir, ==, odd);
2309 		dsl_dir_rele(ds->ds_dir, ds);
2310 		VERIFY0(dsl_dir_hold_obj(dp, dd->dd_object,
2311 		    NULL, ds, &ds->ds_dir));
2312 
2313 		/* move any clone references */
2314 		if (ds->ds_phys->ds_next_clones_obj &&
2315 		    spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
2316 			zap_cursor_t zc;
2317 			zap_attribute_t za;
2318 
2319 			for (zap_cursor_init(&zc, dp->dp_meta_objset,
2320 			    ds->ds_phys->ds_next_clones_obj);
2321 			    zap_cursor_retrieve(&zc, &za) == 0;
2322 			    zap_cursor_advance(&zc)) {
2323 				dsl_dataset_t *cnds;
2324 				uint64_t o;
2325 
2326 				if (za.za_first_integer == oldnext_obj) {
2327 					/*
2328 					 * We've already moved the
2329 					 * origin's reference.
2330 					 */
2331 					continue;
2332 				}
2333 
2334 				VERIFY0(dsl_dataset_hold_obj(dp,
2335 				    za.za_first_integer, FTAG, &cnds));
2336 				o = cnds->ds_dir->dd_phys->dd_head_dataset_obj;
2337 
2338 				VERIFY0(zap_remove_int(dp->dp_meta_objset,
2339 				    odd->dd_phys->dd_clones, o, tx));
2340 				VERIFY0(zap_add_int(dp->dp_meta_objset,
2341 				    dd->dd_phys->dd_clones, o, tx));
2342 				dsl_dataset_rele(cnds, FTAG);
2343 			}
2344 			zap_cursor_fini(&zc);
2345 		}
2346 
2347 		ASSERT(!dsl_prop_hascb(ds));
2348 	}
2349 
2350 	/*
2351 	 * Change space accounting.
2352 	 * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either
2353 	 * both be valid, or both be 0 (resulting in delta == 0).  This
2354 	 * is true for each of {clone,origin} independently.
2355 	 */
2356 
2357 	delta = ddpa->cloneusedsnap -
2358 	    dd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
2359 	ASSERT3S(delta, >=, 0);
2360 	ASSERT3U(ddpa->used, >=, delta);
2361 	dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx);
2362 	dsl_dir_diduse_space(dd, DD_USED_HEAD,
2363 	    ddpa->used - delta, ddpa->comp, ddpa->uncomp, tx);
2364 
2365 	delta = ddpa->originusedsnap -
2366 	    odd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
2367 	ASSERT3S(delta, <=, 0);
2368 	ASSERT3U(ddpa->used, >=, -delta);
2369 	dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx);
2370 	dsl_dir_diduse_space(odd, DD_USED_HEAD,
2371 	    -ddpa->used - delta, -ddpa->comp, -ddpa->uncomp, tx);
2372 
2373 	origin_ds->ds_phys->ds_unique_bytes = ddpa->unique;
2374 
2375 	/* log history record */
2376 	spa_history_log_internal_ds(hds, "promote", tx, "");
2377 
2378 	dsl_dir_rele(odd, FTAG);
2379 	promote_rele(ddpa, FTAG);
2380 }
2381 
2382 /*
2383  * Make a list of dsl_dataset_t's for the snapshots between first_obj
2384  * (exclusive) and last_obj (inclusive).  The list will be in reverse
2385  * order (last_obj will be the list_head()).  If first_obj == 0, do all
2386  * snapshots back to this dataset's origin.
2387  */
2388 static int
2389 snaplist_make(dsl_pool_t *dp,
2390     uint64_t first_obj, uint64_t last_obj, list_t *l, void *tag)
2391 {
2392 	uint64_t obj = last_obj;
2393 
2394 	list_create(l, sizeof (struct promotenode),
2395 	    offsetof(struct promotenode, link));
2396 
2397 	while (obj != first_obj) {
2398 		dsl_dataset_t *ds;
2399 		struct promotenode *snap;
2400 		int err;
2401 
2402 		err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
2403 		ASSERT(err != ENOENT);
2404 		if (err != 0)
2405 			return (err);
2406 
2407 		if (first_obj == 0)
2408 			first_obj = ds->ds_dir->dd_phys->dd_origin_obj;
2409 
2410 		snap = kmem_alloc(sizeof (*snap), KM_SLEEP);
2411 		snap->ds = ds;
2412 		list_insert_tail(l, snap);
2413 		obj = ds->ds_phys->ds_prev_snap_obj;
2414 	}
2415 
2416 	return (0);
2417 }
2418 
2419 static int
2420 snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep)
2421 {
2422 	struct promotenode *snap;
2423 
2424 	*spacep = 0;
2425 	for (snap = list_head(l); snap; snap = list_next(l, snap)) {
2426 		uint64_t used, comp, uncomp;
2427 		dsl_deadlist_space_range(&snap->ds->ds_deadlist,
2428 		    mintxg, UINT64_MAX, &used, &comp, &uncomp);
2429 		*spacep += used;
2430 	}
2431 	return (0);
2432 }
2433 
2434 static void
2435 snaplist_destroy(list_t *l, void *tag)
2436 {
2437 	struct promotenode *snap;
2438 
2439 	if (l == NULL || !list_link_active(&l->list_head))
2440 		return;
2441 
2442 	while ((snap = list_tail(l)) != NULL) {
2443 		list_remove(l, snap);
2444 		dsl_dataset_rele(snap->ds, tag);
2445 		kmem_free(snap, sizeof (*snap));
2446 	}
2447 	list_destroy(l);
2448 }
2449 
2450 static int
2451 promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, void *tag)
2452 {
2453 	int error;
2454 	dsl_dir_t *dd;
2455 	struct promotenode *snap;
2456 
2457 	error = dsl_dataset_hold(dp, ddpa->ddpa_clonename, tag,
2458 	    &ddpa->ddpa_clone);
2459 	if (error != 0)
2460 		return (error);
2461 	dd = ddpa->ddpa_clone->ds_dir;
2462 
2463 	if (dsl_dataset_is_snapshot(ddpa->ddpa_clone) ||
2464 	    !dsl_dir_is_clone(dd)) {
2465 		dsl_dataset_rele(ddpa->ddpa_clone, tag);
2466 		return (SET_ERROR(EINVAL));
2467 	}
2468 
2469 	error = snaplist_make(dp, 0, dd->dd_phys->dd_origin_obj,
2470 	    &ddpa->shared_snaps, tag);
2471 	if (error != 0)
2472 		goto out;
2473 
2474 	error = snaplist_make(dp, 0, ddpa->ddpa_clone->ds_object,
2475 	    &ddpa->clone_snaps, tag);
2476 	if (error != 0)
2477 		goto out;
2478 
2479 	snap = list_head(&ddpa->shared_snaps);
2480 	ASSERT3U(snap->ds->ds_object, ==, dd->dd_phys->dd_origin_obj);
2481 	error = snaplist_make(dp, dd->dd_phys->dd_origin_obj,
2482 	    snap->ds->ds_dir->dd_phys->dd_head_dataset_obj,
2483 	    &ddpa->origin_snaps, tag);
2484 	if (error != 0)
2485 		goto out;
2486 
2487 	if (snap->ds->ds_dir->dd_phys->dd_origin_obj != 0) {
2488 		error = dsl_dataset_hold_obj(dp,
2489 		    snap->ds->ds_dir->dd_phys->dd_origin_obj,
2490 		    tag, &ddpa->origin_origin);
2491 		if (error != 0)
2492 			goto out;
2493 	}
2494 out:
2495 	if (error != 0)
2496 		promote_rele(ddpa, tag);
2497 	return (error);
2498 }
2499 
2500 static void
2501 promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag)
2502 {
2503 	snaplist_destroy(&ddpa->shared_snaps, tag);
2504 	snaplist_destroy(&ddpa->clone_snaps, tag);
2505 	snaplist_destroy(&ddpa->origin_snaps, tag);
2506 	if (ddpa->origin_origin != NULL)
2507 		dsl_dataset_rele(ddpa->origin_origin, tag);
2508 	dsl_dataset_rele(ddpa->ddpa_clone, tag);
2509 }
2510 
2511 /*
2512  * Promote a clone.
2513  *
2514  * If it fails due to a conflicting snapshot name, "conflsnap" will be filled
2515  * in with the name.  (It must be at least MAXNAMELEN bytes long.)
2516  */
2517 int
2518 dsl_dataset_promote(const char *name, char *conflsnap)
2519 {
2520 	dsl_dataset_promote_arg_t ddpa = { 0 };
2521 	uint64_t numsnaps;
2522 	int error;
2523 	objset_t *os;
2524 
2525 	/*
2526 	 * We will modify space proportional to the number of
2527 	 * snapshots.  Compute numsnaps.
2528 	 */
2529 	error = dmu_objset_hold(name, FTAG, &os);
2530 	if (error != 0)
2531 		return (error);
2532 	error = zap_count(dmu_objset_pool(os)->dp_meta_objset,
2533 	    dmu_objset_ds(os)->ds_phys->ds_snapnames_zapobj, &numsnaps);
2534 	dmu_objset_rele(os, FTAG);
2535 	if (error != 0)
2536 		return (error);
2537 
2538 	ddpa.ddpa_clonename = name;
2539 	ddpa.err_ds = conflsnap;
2540 	ddpa.cr = CRED();
2541 
2542 	return (dsl_sync_task(name, dsl_dataset_promote_check,
2543 	    dsl_dataset_promote_sync, &ddpa,
2544 	    2 + numsnaps, ZFS_SPACE_CHECK_RESERVED));
2545 }
2546 
2547 int
2548 dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone,
2549     dsl_dataset_t *origin_head, boolean_t force, void *owner, dmu_tx_t *tx)
2550 {
2551 	int64_t unused_refres_delta;
2552 
2553 	/* they should both be heads */
2554 	if (dsl_dataset_is_snapshot(clone) ||
2555 	    dsl_dataset_is_snapshot(origin_head))
2556 		return (SET_ERROR(EINVAL));
2557 
2558 	/* if we are not forcing, the branch point should be just before them */
2559 	if (!force && clone->ds_prev != origin_head->ds_prev)
2560 		return (SET_ERROR(EINVAL));
2561 
2562 	/* clone should be the clone (unless they are unrelated) */
2563 	if (clone->ds_prev != NULL &&
2564 	    clone->ds_prev != clone->ds_dir->dd_pool->dp_origin_snap &&
2565 	    origin_head->ds_dir != clone->ds_prev->ds_dir)
2566 		return (SET_ERROR(EINVAL));
2567 
2568 	/* the clone should be a child of the origin */
2569 	if (clone->ds_dir->dd_parent != origin_head->ds_dir)
2570 		return (SET_ERROR(EINVAL));
2571 
2572 	/* origin_head shouldn't be modified unless 'force' */
2573 	if (!force &&
2574 	    dsl_dataset_modified_since_snap(origin_head, origin_head->ds_prev))
2575 		return (SET_ERROR(ETXTBSY));
2576 
2577 	/* origin_head should have no long holds (e.g. is not mounted) */
2578 	if (dsl_dataset_handoff_check(origin_head, owner, tx))
2579 		return (SET_ERROR(EBUSY));
2580 
2581 	/* check amount of any unconsumed refreservation */
2582 	unused_refres_delta =
2583 	    (int64_t)MIN(origin_head->ds_reserved,
2584 	    origin_head->ds_phys->ds_unique_bytes) -
2585 	    (int64_t)MIN(origin_head->ds_reserved,
2586 	    clone->ds_phys->ds_unique_bytes);
2587 
2588 	if (unused_refres_delta > 0 &&
2589 	    unused_refres_delta >
2590 	    dsl_dir_space_available(origin_head->ds_dir, NULL, 0, TRUE))
2591 		return (SET_ERROR(ENOSPC));
2592 
2593 	/* clone can't be over the head's refquota */
2594 	if (origin_head->ds_quota != 0 &&
2595 	    clone->ds_phys->ds_referenced_bytes > origin_head->ds_quota)
2596 		return (SET_ERROR(EDQUOT));
2597 
2598 	return (0);
2599 }
2600 
2601 void
2602 dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone,
2603     dsl_dataset_t *origin_head, dmu_tx_t *tx)
2604 {
2605 	dsl_pool_t *dp = dmu_tx_pool(tx);
2606 	int64_t unused_refres_delta;
2607 
2608 	ASSERT(clone->ds_reserved == 0);
2609 	ASSERT(origin_head->ds_quota == 0 ||
2610 	    clone->ds_phys->ds_unique_bytes <= origin_head->ds_quota);
2611 	ASSERT3P(clone->ds_prev, ==, origin_head->ds_prev);
2612 
2613 	dmu_buf_will_dirty(clone->ds_dbuf, tx);
2614 	dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
2615 
2616 	if (clone->ds_objset != NULL) {
2617 		dmu_objset_evict(clone->ds_objset);
2618 		clone->ds_objset = NULL;
2619 	}
2620 
2621 	if (origin_head->ds_objset != NULL) {
2622 		dmu_objset_evict(origin_head->ds_objset);
2623 		origin_head->ds_objset = NULL;
2624 	}
2625 
2626 	unused_refres_delta =
2627 	    (int64_t)MIN(origin_head->ds_reserved,
2628 	    origin_head->ds_phys->ds_unique_bytes) -
2629 	    (int64_t)MIN(origin_head->ds_reserved,
2630 	    clone->ds_phys->ds_unique_bytes);
2631 
2632 	/*
2633 	 * Reset origin's unique bytes, if it exists.
2634 	 */
2635 	if (clone->ds_prev) {
2636 		dsl_dataset_t *origin = clone->ds_prev;
2637 		uint64_t comp, uncomp;
2638 
2639 		dmu_buf_will_dirty(origin->ds_dbuf, tx);
2640 		dsl_deadlist_space_range(&clone->ds_deadlist,
2641 		    origin->ds_phys->ds_prev_snap_txg, UINT64_MAX,
2642 		    &origin->ds_phys->ds_unique_bytes, &comp, &uncomp);
2643 	}
2644 
2645 	/* swap blkptrs */
2646 	{
2647 		blkptr_t tmp;
2648 		tmp = origin_head->ds_phys->ds_bp;
2649 		origin_head->ds_phys->ds_bp = clone->ds_phys->ds_bp;
2650 		clone->ds_phys->ds_bp = tmp;
2651 	}
2652 
2653 	/* set dd_*_bytes */
2654 	{
2655 		int64_t dused, dcomp, duncomp;
2656 		uint64_t cdl_used, cdl_comp, cdl_uncomp;
2657 		uint64_t odl_used, odl_comp, odl_uncomp;
2658 
2659 		ASSERT3U(clone->ds_dir->dd_phys->
2660 		    dd_used_breakdown[DD_USED_SNAP], ==, 0);
2661 
2662 		dsl_deadlist_space(&clone->ds_deadlist,
2663 		    &cdl_used, &cdl_comp, &cdl_uncomp);
2664 		dsl_deadlist_space(&origin_head->ds_deadlist,
2665 		    &odl_used, &odl_comp, &odl_uncomp);
2666 
2667 		dused = clone->ds_phys->ds_referenced_bytes + cdl_used -
2668 		    (origin_head->ds_phys->ds_referenced_bytes + odl_used);
2669 		dcomp = clone->ds_phys->ds_compressed_bytes + cdl_comp -
2670 		    (origin_head->ds_phys->ds_compressed_bytes + odl_comp);
2671 		duncomp = clone->ds_phys->ds_uncompressed_bytes +
2672 		    cdl_uncomp -
2673 		    (origin_head->ds_phys->ds_uncompressed_bytes + odl_uncomp);
2674 
2675 		dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_HEAD,
2676 		    dused, dcomp, duncomp, tx);
2677 		dsl_dir_diduse_space(clone->ds_dir, DD_USED_HEAD,
2678 		    -dused, -dcomp, -duncomp, tx);
2679 
2680 		/*
2681 		 * The difference in the space used by snapshots is the
2682 		 * difference in snapshot space due to the head's
2683 		 * deadlist (since that's the only thing that's
2684 		 * changing that affects the snapused).
2685 		 */
2686 		dsl_deadlist_space_range(&clone->ds_deadlist,
2687 		    origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
2688 		    &cdl_used, &cdl_comp, &cdl_uncomp);
2689 		dsl_deadlist_space_range(&origin_head->ds_deadlist,
2690 		    origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
2691 		    &odl_used, &odl_comp, &odl_uncomp);
2692 		dsl_dir_transfer_space(origin_head->ds_dir, cdl_used - odl_used,
2693 		    DD_USED_HEAD, DD_USED_SNAP, tx);
2694 	}
2695 
2696 	/* swap ds_*_bytes */
2697 	SWITCH64(origin_head->ds_phys->ds_referenced_bytes,
2698 	    clone->ds_phys->ds_referenced_bytes);
2699 	SWITCH64(origin_head->ds_phys->ds_compressed_bytes,
2700 	    clone->ds_phys->ds_compressed_bytes);
2701 	SWITCH64(origin_head->ds_phys->ds_uncompressed_bytes,
2702 	    clone->ds_phys->ds_uncompressed_bytes);
2703 	SWITCH64(origin_head->ds_phys->ds_unique_bytes,
2704 	    clone->ds_phys->ds_unique_bytes);
2705 
2706 	/* apply any parent delta for change in unconsumed refreservation */
2707 	dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_REFRSRV,
2708 	    unused_refres_delta, 0, 0, tx);
2709 
2710 	/*
2711 	 * Swap deadlists.
2712 	 */
2713 	dsl_deadlist_close(&clone->ds_deadlist);
2714 	dsl_deadlist_close(&origin_head->ds_deadlist);
2715 	SWITCH64(origin_head->ds_phys->ds_deadlist_obj,
2716 	    clone->ds_phys->ds_deadlist_obj);
2717 	dsl_deadlist_open(&clone->ds_deadlist, dp->dp_meta_objset,
2718 	    clone->ds_phys->ds_deadlist_obj);
2719 	dsl_deadlist_open(&origin_head->ds_deadlist, dp->dp_meta_objset,
2720 	    origin_head->ds_phys->ds_deadlist_obj);
2721 
2722 	dsl_scan_ds_clone_swapped(origin_head, clone, tx);
2723 
2724 	spa_history_log_internal_ds(clone, "clone swap", tx,
2725 	    "parent=%s", origin_head->ds_dir->dd_myname);
2726 }
2727 
2728 /*
2729  * Given a pool name and a dataset object number in that pool,
2730  * return the name of that dataset.
2731  */
2732 int
2733 dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
2734 {
2735 	dsl_pool_t *dp;
2736 	dsl_dataset_t *ds;
2737 	int error;
2738 
2739 	error = dsl_pool_hold(pname, FTAG, &dp);
2740 	if (error != 0)
2741 		return (error);
2742 
2743 	error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
2744 	if (error == 0) {
2745 		dsl_dataset_name(ds, buf);
2746 		dsl_dataset_rele(ds, FTAG);
2747 	}
2748 	dsl_pool_rele(dp, FTAG);
2749 
2750 	return (error);
2751 }
2752 
2753 int
2754 dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota,
2755     uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv)
2756 {
2757 	int error = 0;
2758 
2759 	ASSERT3S(asize, >, 0);
2760 
2761 	/*
2762 	 * *ref_rsrv is the portion of asize that will come from any
2763 	 * unconsumed refreservation space.
2764 	 */
2765 	*ref_rsrv = 0;
2766 
2767 	mutex_enter(&ds->ds_lock);
2768 	/*
2769 	 * Make a space adjustment for reserved bytes.
2770 	 */
2771 	if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) {
2772 		ASSERT3U(*used, >=,
2773 		    ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
2774 		*used -= (ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
2775 		*ref_rsrv =
2776 		    asize - MIN(asize, parent_delta(ds, asize + inflight));
2777 	}
2778 
2779 	if (!check_quota || ds->ds_quota == 0) {
2780 		mutex_exit(&ds->ds_lock);
2781 		return (0);
2782 	}
2783 	/*
2784 	 * If they are requesting more space, and our current estimate
2785 	 * is over quota, they get to try again unless the actual
2786 	 * on-disk is over quota and there are no pending changes (which
2787 	 * may free up space for us).
2788 	 */
2789 	if (ds->ds_phys->ds_referenced_bytes + inflight >= ds->ds_quota) {
2790 		if (inflight > 0 ||
2791 		    ds->ds_phys->ds_referenced_bytes < ds->ds_quota)
2792 			error = SET_ERROR(ERESTART);
2793 		else
2794 			error = SET_ERROR(EDQUOT);
2795 	}
2796 	mutex_exit(&ds->ds_lock);
2797 
2798 	return (error);
2799 }
2800 
2801 typedef struct dsl_dataset_set_qr_arg {
2802 	const char *ddsqra_name;
2803 	zprop_source_t ddsqra_source;
2804 	uint64_t ddsqra_value;
2805 } dsl_dataset_set_qr_arg_t;
2806 
2807 
2808 /* ARGSUSED */
2809 static int
2810 dsl_dataset_set_refquota_check(void *arg, dmu_tx_t *tx)
2811 {
2812 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
2813 	dsl_pool_t *dp = dmu_tx_pool(tx);
2814 	dsl_dataset_t *ds;
2815 	int error;
2816 	uint64_t newval;
2817 
2818 	if (spa_version(dp->dp_spa) < SPA_VERSION_REFQUOTA)
2819 		return (SET_ERROR(ENOTSUP));
2820 
2821 	error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
2822 	if (error != 0)
2823 		return (error);
2824 
2825 	if (dsl_dataset_is_snapshot(ds)) {
2826 		dsl_dataset_rele(ds, FTAG);
2827 		return (SET_ERROR(EINVAL));
2828 	}
2829 
2830 	error = dsl_prop_predict(ds->ds_dir,
2831 	    zfs_prop_to_name(ZFS_PROP_REFQUOTA),
2832 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
2833 	if (error != 0) {
2834 		dsl_dataset_rele(ds, FTAG);
2835 		return (error);
2836 	}
2837 
2838 	if (newval == 0) {
2839 		dsl_dataset_rele(ds, FTAG);
2840 		return (0);
2841 	}
2842 
2843 	if (newval < ds->ds_phys->ds_referenced_bytes ||
2844 	    newval < ds->ds_reserved) {
2845 		dsl_dataset_rele(ds, FTAG);
2846 		return (SET_ERROR(ENOSPC));
2847 	}
2848 
2849 	dsl_dataset_rele(ds, FTAG);
2850 	return (0);
2851 }
2852 
2853 static void
2854 dsl_dataset_set_refquota_sync(void *arg, dmu_tx_t *tx)
2855 {
2856 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
2857 	dsl_pool_t *dp = dmu_tx_pool(tx);
2858 	dsl_dataset_t *ds;
2859 	uint64_t newval;
2860 
2861 	VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
2862 
2863 	dsl_prop_set_sync_impl(ds,
2864 	    zfs_prop_to_name(ZFS_PROP_REFQUOTA),
2865 	    ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
2866 	    &ddsqra->ddsqra_value, tx);
2867 
2868 	VERIFY0(dsl_prop_get_int_ds(ds,
2869 	    zfs_prop_to_name(ZFS_PROP_REFQUOTA), &newval));
2870 
2871 	if (ds->ds_quota != newval) {
2872 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
2873 		ds->ds_quota = newval;
2874 	}
2875 	dsl_dataset_rele(ds, FTAG);
2876 }
2877 
2878 int
2879 dsl_dataset_set_refquota(const char *dsname, zprop_source_t source,
2880     uint64_t refquota)
2881 {
2882 	dsl_dataset_set_qr_arg_t ddsqra;
2883 
2884 	ddsqra.ddsqra_name = dsname;
2885 	ddsqra.ddsqra_source = source;
2886 	ddsqra.ddsqra_value = refquota;
2887 
2888 	return (dsl_sync_task(dsname, dsl_dataset_set_refquota_check,
2889 	    dsl_dataset_set_refquota_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE));
2890 }
2891 
2892 static int
2893 dsl_dataset_set_refreservation_check(void *arg, dmu_tx_t *tx)
2894 {
2895 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
2896 	dsl_pool_t *dp = dmu_tx_pool(tx);
2897 	dsl_dataset_t *ds;
2898 	int error;
2899 	uint64_t newval, unique;
2900 
2901 	if (spa_version(dp->dp_spa) < SPA_VERSION_REFRESERVATION)
2902 		return (SET_ERROR(ENOTSUP));
2903 
2904 	error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
2905 	if (error != 0)
2906 		return (error);
2907 
2908 	if (dsl_dataset_is_snapshot(ds)) {
2909 		dsl_dataset_rele(ds, FTAG);
2910 		return (SET_ERROR(EINVAL));
2911 	}
2912 
2913 	error = dsl_prop_predict(ds->ds_dir,
2914 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
2915 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
2916 	if (error != 0) {
2917 		dsl_dataset_rele(ds, FTAG);
2918 		return (error);
2919 	}
2920 
2921 	/*
2922 	 * If we are doing the preliminary check in open context, the
2923 	 * space estimates may be inaccurate.
2924 	 */
2925 	if (!dmu_tx_is_syncing(tx)) {
2926 		dsl_dataset_rele(ds, FTAG);
2927 		return (0);
2928 	}
2929 
2930 	mutex_enter(&ds->ds_lock);
2931 	if (!DS_UNIQUE_IS_ACCURATE(ds))
2932 		dsl_dataset_recalc_head_uniq(ds);
2933 	unique = ds->ds_phys->ds_unique_bytes;
2934 	mutex_exit(&ds->ds_lock);
2935 
2936 	if (MAX(unique, newval) > MAX(unique, ds->ds_reserved)) {
2937 		uint64_t delta = MAX(unique, newval) -
2938 		    MAX(unique, ds->ds_reserved);
2939 
2940 		if (delta >
2941 		    dsl_dir_space_available(ds->ds_dir, NULL, 0, B_TRUE) ||
2942 		    (ds->ds_quota > 0 && newval > ds->ds_quota)) {
2943 			dsl_dataset_rele(ds, FTAG);
2944 			return (SET_ERROR(ENOSPC));
2945 		}
2946 	}
2947 
2948 	dsl_dataset_rele(ds, FTAG);
2949 	return (0);
2950 }
2951 
2952 void
2953 dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds,
2954     zprop_source_t source, uint64_t value, dmu_tx_t *tx)
2955 {
2956 	uint64_t newval;
2957 	uint64_t unique;
2958 	int64_t delta;
2959 
2960 	dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
2961 	    source, sizeof (value), 1, &value, tx);
2962 
2963 	VERIFY0(dsl_prop_get_int_ds(ds,
2964 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &newval));
2965 
2966 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
2967 	mutex_enter(&ds->ds_dir->dd_lock);
2968 	mutex_enter(&ds->ds_lock);
2969 	ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
2970 	unique = ds->ds_phys->ds_unique_bytes;
2971 	delta = MAX(0, (int64_t)(newval - unique)) -
2972 	    MAX(0, (int64_t)(ds->ds_reserved - unique));
2973 	ds->ds_reserved = newval;
2974 	mutex_exit(&ds->ds_lock);
2975 
2976 	dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx);
2977 	mutex_exit(&ds->ds_dir->dd_lock);
2978 }
2979 
2980 static void
2981 dsl_dataset_set_refreservation_sync(void *arg, dmu_tx_t *tx)
2982 {
2983 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
2984 	dsl_pool_t *dp = dmu_tx_pool(tx);
2985 	dsl_dataset_t *ds;
2986 
2987 	VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
2988 	dsl_dataset_set_refreservation_sync_impl(ds,
2989 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, tx);
2990 	dsl_dataset_rele(ds, FTAG);
2991 }
2992 
2993 int
2994 dsl_dataset_set_refreservation(const char *dsname, zprop_source_t source,
2995     uint64_t refreservation)
2996 {
2997 	dsl_dataset_set_qr_arg_t ddsqra;
2998 
2999 	ddsqra.ddsqra_name = dsname;
3000 	ddsqra.ddsqra_source = source;
3001 	ddsqra.ddsqra_value = refreservation;
3002 
3003 	return (dsl_sync_task(dsname, dsl_dataset_set_refreservation_check,
3004 	    dsl_dataset_set_refreservation_sync, &ddsqra,
3005 	    0, ZFS_SPACE_CHECK_NONE));
3006 }
3007 
3008 /*
3009  * Return (in *usedp) the amount of space written in new that is not
3010  * present in oldsnap.  New may be a snapshot or the head.  Old must be
3011  * a snapshot before new, in new's filesystem (or its origin).  If not then
3012  * fail and return EINVAL.
3013  *
3014  * The written space is calculated by considering two components:  First, we
3015  * ignore any freed space, and calculate the written as new's used space
3016  * minus old's used space.  Next, we add in the amount of space that was freed
3017  * between the two snapshots, thus reducing new's used space relative to old's.
3018  * Specifically, this is the space that was born before old->ds_creation_txg,
3019  * and freed before new (ie. on new's deadlist or a previous deadlist).
3020  *
3021  * space freed                         [---------------------]
3022  * snapshots                       ---O-------O--------O-------O------
3023  *                                         oldsnap            new
3024  */
3025 int
3026 dsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new,
3027     uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
3028 {
3029 	int err = 0;
3030 	uint64_t snapobj;
3031 	dsl_pool_t *dp = new->ds_dir->dd_pool;
3032 
3033 	ASSERT(dsl_pool_config_held(dp));
3034 
3035 	*usedp = 0;
3036 	*usedp += new->ds_phys->ds_referenced_bytes;
3037 	*usedp -= oldsnap->ds_phys->ds_referenced_bytes;
3038 
3039 	*compp = 0;
3040 	*compp += new->ds_phys->ds_compressed_bytes;
3041 	*compp -= oldsnap->ds_phys->ds_compressed_bytes;
3042 
3043 	*uncompp = 0;
3044 	*uncompp += new->ds_phys->ds_uncompressed_bytes;
3045 	*uncompp -= oldsnap->ds_phys->ds_uncompressed_bytes;
3046 
3047 	snapobj = new->ds_object;
3048 	while (snapobj != oldsnap->ds_object) {
3049 		dsl_dataset_t *snap;
3050 		uint64_t used, comp, uncomp;
3051 
3052 		if (snapobj == new->ds_object) {
3053 			snap = new;
3054 		} else {
3055 			err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap);
3056 			if (err != 0)
3057 				break;
3058 		}
3059 
3060 		if (snap->ds_phys->ds_prev_snap_txg ==
3061 		    oldsnap->ds_phys->ds_creation_txg) {
3062 			/*
3063 			 * The blocks in the deadlist can not be born after
3064 			 * ds_prev_snap_txg, so get the whole deadlist space,
3065 			 * which is more efficient (especially for old-format
3066 			 * deadlists).  Unfortunately the deadlist code
3067 			 * doesn't have enough information to make this
3068 			 * optimization itself.
3069 			 */
3070 			dsl_deadlist_space(&snap->ds_deadlist,
3071 			    &used, &comp, &uncomp);
3072 		} else {
3073 			dsl_deadlist_space_range(&snap->ds_deadlist,
3074 			    0, oldsnap->ds_phys->ds_creation_txg,
3075 			    &used, &comp, &uncomp);
3076 		}
3077 		*usedp += used;
3078 		*compp += comp;
3079 		*uncompp += uncomp;
3080 
3081 		/*
3082 		 * If we get to the beginning of the chain of snapshots
3083 		 * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap
3084 		 * was not a snapshot of/before new.
3085 		 */
3086 		snapobj = snap->ds_phys->ds_prev_snap_obj;
3087 		if (snap != new)
3088 			dsl_dataset_rele(snap, FTAG);
3089 		if (snapobj == 0) {
3090 			err = SET_ERROR(EINVAL);
3091 			break;
3092 		}
3093 
3094 	}
3095 	return (err);
3096 }
3097 
3098 /*
3099  * Return (in *usedp) the amount of space that will be reclaimed if firstsnap,
3100  * lastsnap, and all snapshots in between are deleted.
3101  *
3102  * blocks that would be freed            [---------------------------]
3103  * snapshots                       ---O-------O--------O-------O--------O
3104  *                                        firstsnap        lastsnap
3105  *
3106  * This is the set of blocks that were born after the snap before firstsnap,
3107  * (birth > firstsnap->prev_snap_txg) and died before the snap after the
3108  * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist).
3109  * We calculate this by iterating over the relevant deadlists (from the snap
3110  * after lastsnap, backward to the snap after firstsnap), summing up the
3111  * space on the deadlist that was born after the snap before firstsnap.
3112  */
3113 int
3114 dsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap,
3115     dsl_dataset_t *lastsnap,
3116     uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
3117 {
3118 	int err = 0;
3119 	uint64_t snapobj;
3120 	dsl_pool_t *dp = firstsnap->ds_dir->dd_pool;
3121 
3122 	ASSERT(dsl_dataset_is_snapshot(firstsnap));
3123 	ASSERT(dsl_dataset_is_snapshot(lastsnap));
3124 
3125 	/*
3126 	 * Check that the snapshots are in the same dsl_dir, and firstsnap
3127 	 * is before lastsnap.
3128 	 */
3129 	if (firstsnap->ds_dir != lastsnap->ds_dir ||
3130 	    firstsnap->ds_phys->ds_creation_txg >
3131 	    lastsnap->ds_phys->ds_creation_txg)
3132 		return (SET_ERROR(EINVAL));
3133 
3134 	*usedp = *compp = *uncompp = 0;
3135 
3136 	snapobj = lastsnap->ds_phys->ds_next_snap_obj;
3137 	while (snapobj != firstsnap->ds_object) {
3138 		dsl_dataset_t *ds;
3139 		uint64_t used, comp, uncomp;
3140 
3141 		err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &ds);
3142 		if (err != 0)
3143 			break;
3144 
3145 		dsl_deadlist_space_range(&ds->ds_deadlist,
3146 		    firstsnap->ds_phys->ds_prev_snap_txg, UINT64_MAX,
3147 		    &used, &comp, &uncomp);
3148 		*usedp += used;
3149 		*compp += comp;
3150 		*uncompp += uncomp;
3151 
3152 		snapobj = ds->ds_phys->ds_prev_snap_obj;
3153 		ASSERT3U(snapobj, !=, 0);
3154 		dsl_dataset_rele(ds, FTAG);
3155 	}
3156 	return (err);
3157 }
3158 
3159 static int
3160 dsl_dataset_activate_large_blocks_check(void *arg, dmu_tx_t *tx)
3161 {
3162 	const char *dsname = arg;
3163 	dsl_dataset_t *ds;
3164 	dsl_pool_t *dp = dmu_tx_pool(tx);
3165 	int error = 0;
3166 
3167 	if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LARGE_BLOCKS))
3168 		return (SET_ERROR(ENOTSUP));
3169 
3170 	ASSERT(spa_feature_is_enabled(dp->dp_spa,
3171 	    SPA_FEATURE_EXTENSIBLE_DATASET));
3172 
3173 	error = dsl_dataset_hold(dp, dsname, FTAG, &ds);
3174 	if (error != 0)
3175 		return (error);
3176 
3177 	if (ds->ds_large_blocks)
3178 		error = EALREADY;
3179 	dsl_dataset_rele(ds, FTAG);
3180 
3181 	return (error);
3182 }
3183 
3184 void
3185 dsl_dataset_activate_large_blocks_sync_impl(uint64_t dsobj, dmu_tx_t *tx)
3186 {
3187 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
3188 	objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset;
3189 	uint64_t zero = 0;
3190 
3191 	spa_feature_incr(spa, SPA_FEATURE_LARGE_BLOCKS, tx);
3192 	dmu_object_zapify(mos, dsobj, DMU_OT_DSL_DATASET, tx);
3193 
3194 	VERIFY0(zap_add(mos, dsobj, DS_FIELD_LARGE_BLOCKS,
3195 	    sizeof (zero), 1, &zero, tx));
3196 }
3197 
3198 static void
3199 dsl_dataset_activate_large_blocks_sync(void *arg, dmu_tx_t *tx)
3200 {
3201 	const char *dsname = arg;
3202 	dsl_dataset_t *ds;
3203 
3204 	VERIFY0(dsl_dataset_hold(dmu_tx_pool(tx), dsname, FTAG, &ds));
3205 
3206 	dsl_dataset_activate_large_blocks_sync_impl(ds->ds_object, tx);
3207 	ASSERT(!ds->ds_large_blocks);
3208 	ds->ds_large_blocks = B_TRUE;
3209 	dsl_dataset_rele(ds, FTAG);
3210 }
3211 
3212 int
3213 dsl_dataset_activate_large_blocks(const char *dsname)
3214 {
3215 	int error;
3216 
3217 	error = dsl_sync_task(dsname,
3218 	    dsl_dataset_activate_large_blocks_check,
3219 	    dsl_dataset_activate_large_blocks_sync, (void *)dsname,
3220 	    1, ZFS_SPACE_CHECK_RESERVED);
3221 
3222 	/*
3223 	 * EALREADY indicates that this dataset already supports large blocks.
3224 	 */
3225 	if (error == EALREADY)
3226 		error = 0;
3227 	return (error);
3228 }
3229 
3230 /*
3231  * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline.
3232  * For example, they could both be snapshots of the same filesystem, and
3233  * 'earlier' is before 'later'.  Or 'earlier' could be the origin of
3234  * 'later's filesystem.  Or 'earlier' could be an older snapshot in the origin's
3235  * filesystem.  Or 'earlier' could be the origin's origin.
3236  *
3237  * If non-zero, earlier_txg is used instead of earlier's ds_creation_txg.
3238  */
3239 boolean_t
3240 dsl_dataset_is_before(dsl_dataset_t *later, dsl_dataset_t *earlier,
3241 	uint64_t earlier_txg)
3242 {
3243 	dsl_pool_t *dp = later->ds_dir->dd_pool;
3244 	int error;
3245 	boolean_t ret;
3246 
3247 	ASSERT(dsl_pool_config_held(dp));
3248 	ASSERT(dsl_dataset_is_snapshot(earlier) || earlier_txg != 0);
3249 
3250 	if (earlier_txg == 0)
3251 		earlier_txg = earlier->ds_phys->ds_creation_txg;
3252 
3253 	if (dsl_dataset_is_snapshot(later) &&
3254 	    earlier_txg >= later->ds_phys->ds_creation_txg)
3255 		return (B_FALSE);
3256 
3257 	if (later->ds_dir == earlier->ds_dir)
3258 		return (B_TRUE);
3259 	if (!dsl_dir_is_clone(later->ds_dir))
3260 		return (B_FALSE);
3261 
3262 	if (later->ds_dir->dd_phys->dd_origin_obj == earlier->ds_object)
3263 		return (B_TRUE);
3264 	dsl_dataset_t *origin;
3265 	error = dsl_dataset_hold_obj(dp,
3266 	    later->ds_dir->dd_phys->dd_origin_obj, FTAG, &origin);
3267 	if (error != 0)
3268 		return (B_FALSE);
3269 	ret = dsl_dataset_is_before(origin, earlier, earlier_txg);
3270 	dsl_dataset_rele(origin, FTAG);
3271 	return (ret);
3272 }
3273 
3274 
3275 void
3276 dsl_dataset_zapify(dsl_dataset_t *ds, dmu_tx_t *tx)
3277 {
3278 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
3279 	dmu_object_zapify(mos, ds->ds_object, DMU_OT_DSL_DATASET, tx);
3280 }
3281