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