xref: /titanic_51/usr/src/uts/common/fs/zfs/dsl_dataset.c (revision 63e9dad6fd7aa76bcbab14646b8f095642f8a2ae)
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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/dmu_objset.h>
29 #include <sys/dsl_dataset.h>
30 #include <sys/dsl_dir.h>
31 #include <sys/dsl_prop.h>
32 #include <sys/dsl_synctask.h>
33 #include <sys/dmu_traverse.h>
34 #include <sys/dmu_tx.h>
35 #include <sys/arc.h>
36 #include <sys/zio.h>
37 #include <sys/zap.h>
38 #include <sys/unique.h>
39 #include <sys/zfs_context.h>
40 #include <sys/zfs_ioctl.h>
41 #include <sys/spa.h>
42 #include <sys/sunddi.h>
43 
44 static dsl_checkfunc_t dsl_dataset_destroy_begin_check;
45 static dsl_syncfunc_t dsl_dataset_destroy_begin_sync;
46 static dsl_checkfunc_t dsl_dataset_rollback_check;
47 static dsl_syncfunc_t dsl_dataset_rollback_sync;
48 static dsl_syncfunc_t dsl_dataset_set_reservation_sync;
49 
50 #define	DS_REF_MAX	(1ULL << 62)
51 
52 #define	DSL_DEADLIST_BLOCKSIZE	SPA_MAXBLOCKSIZE
53 
54 /*
55  * We use weighted reference counts to express the various forms of exclusion
56  * between different open modes.  A STANDARD open is 1 point, an EXCLUSIVE open
57  * is DS_REF_MAX, and a PRIMARY open is little more than half of an EXCLUSIVE.
58  * This makes the exclusion logic simple: the total refcnt for all opens cannot
59  * exceed DS_REF_MAX.  For example, EXCLUSIVE opens are exclusive because their
60  * weight (DS_REF_MAX) consumes the entire refcnt space.  PRIMARY opens consume
61  * just over half of the refcnt space, so there can't be more than one, but it
62  * can peacefully coexist with any number of STANDARD opens.
63  */
64 static uint64_t ds_refcnt_weight[DS_MODE_LEVELS] = {
65 	0,			/* DS_MODE_NONE - invalid		*/
66 	1,			/* DS_MODE_STANDARD - unlimited number	*/
67 	(DS_REF_MAX >> 1) + 1,	/* DS_MODE_PRIMARY - only one of these	*/
68 	DS_REF_MAX		/* DS_MODE_EXCLUSIVE - no other opens	*/
69 };
70 
71 /*
72  * Figure out how much of this delta should be propogated to the dsl_dir
73  * layer.  If there's a refreservation, that space has already been
74  * partially accounted for in our ancestors.
75  */
76 static int64_t
77 parent_delta(dsl_dataset_t *ds, int64_t delta)
78 {
79 	uint64_t old_bytes, new_bytes;
80 
81 	if (ds->ds_reserved == 0)
82 		return (delta);
83 
84 	old_bytes = MAX(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
85 	new_bytes = MAX(ds->ds_phys->ds_unique_bytes + delta, ds->ds_reserved);
86 
87 	ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta));
88 	return (new_bytes - old_bytes);
89 }
90 
91 void
92 dsl_dataset_block_born(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx)
93 {
94 	int used = bp_get_dasize(tx->tx_pool->dp_spa, bp);
95 	int compressed = BP_GET_PSIZE(bp);
96 	int uncompressed = BP_GET_UCSIZE(bp);
97 	int64_t delta;
98 
99 	dprintf_bp(bp, "born, ds=%p\n", ds);
100 
101 	ASSERT(dmu_tx_is_syncing(tx));
102 	/* It could have been compressed away to nothing */
103 	if (BP_IS_HOLE(bp))
104 		return;
105 	ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE);
106 	ASSERT3U(BP_GET_TYPE(bp), <, DMU_OT_NUMTYPES);
107 	if (ds == NULL) {
108 		/*
109 		 * Account for the meta-objset space in its placeholder
110 		 * dsl_dir.
111 		 */
112 		ASSERT3U(compressed, ==, uncompressed); /* it's all metadata */
113 		dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir,
114 		    used, compressed, uncompressed, tx);
115 		dsl_dir_dirty(tx->tx_pool->dp_mos_dir, tx);
116 		return;
117 	}
118 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
119 	mutex_enter(&ds->ds_lock);
120 	delta = parent_delta(ds, used);
121 	ds->ds_phys->ds_used_bytes += used;
122 	ds->ds_phys->ds_compressed_bytes += compressed;
123 	ds->ds_phys->ds_uncompressed_bytes += uncompressed;
124 	ds->ds_phys->ds_unique_bytes += used;
125 	mutex_exit(&ds->ds_lock);
126 	dsl_dir_diduse_space(ds->ds_dir, delta, compressed, uncompressed, tx);
127 }
128 
129 void
130 dsl_dataset_block_kill(dsl_dataset_t *ds, blkptr_t *bp, zio_t *pio,
131     dmu_tx_t *tx)
132 {
133 	int used = bp_get_dasize(tx->tx_pool->dp_spa, bp);
134 	int compressed = BP_GET_PSIZE(bp);
135 	int uncompressed = BP_GET_UCSIZE(bp);
136 
137 	ASSERT(dmu_tx_is_syncing(tx));
138 	/* No block pointer => nothing to free */
139 	if (BP_IS_HOLE(bp))
140 		return;
141 
142 	ASSERT(used > 0);
143 	if (ds == NULL) {
144 		int err;
145 		/*
146 		 * Account for the meta-objset space in its placeholder
147 		 * dataset.
148 		 */
149 		err = arc_free(pio, tx->tx_pool->dp_spa,
150 		    tx->tx_txg, bp, NULL, NULL, pio ? ARC_NOWAIT: ARC_WAIT);
151 		ASSERT(err == 0);
152 
153 		dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir,
154 		    -used, -compressed, -uncompressed, tx);
155 		dsl_dir_dirty(tx->tx_pool->dp_mos_dir, tx);
156 		return;
157 	}
158 	ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool);
159 
160 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
161 
162 	if (bp->blk_birth > ds->ds_phys->ds_prev_snap_txg) {
163 		int err;
164 		int64_t delta;
165 
166 		dprintf_bp(bp, "freeing: %s", "");
167 		err = arc_free(pio, tx->tx_pool->dp_spa,
168 		    tx->tx_txg, bp, NULL, NULL, pio ? ARC_NOWAIT: ARC_WAIT);
169 		ASSERT(err == 0);
170 
171 		mutex_enter(&ds->ds_lock);
172 		ASSERT(ds->ds_phys->ds_unique_bytes >= used ||
173 		    !DS_UNIQUE_IS_ACCURATE(ds));
174 		delta = parent_delta(ds, -used);
175 		ds->ds_phys->ds_unique_bytes -= used;
176 		mutex_exit(&ds->ds_lock);
177 		dsl_dir_diduse_space(ds->ds_dir,
178 		    delta, -compressed, -uncompressed, tx);
179 	} else {
180 		dprintf_bp(bp, "putting on dead list: %s", "");
181 		VERIFY(0 == bplist_enqueue(&ds->ds_deadlist, bp, tx));
182 		ASSERT3U(ds->ds_prev->ds_object, ==,
183 		    ds->ds_phys->ds_prev_snap_obj);
184 		ASSERT(ds->ds_prev->ds_phys->ds_num_children > 0);
185 		/* if (bp->blk_birth > prev prev snap txg) prev unique += bs */
186 		if (ds->ds_prev->ds_phys->ds_next_snap_obj ==
187 		    ds->ds_object && bp->blk_birth >
188 		    ds->ds_prev->ds_phys->ds_prev_snap_txg) {
189 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
190 			mutex_enter(&ds->ds_prev->ds_lock);
191 			ds->ds_prev->ds_phys->ds_unique_bytes += used;
192 			mutex_exit(&ds->ds_prev->ds_lock);
193 		}
194 	}
195 	mutex_enter(&ds->ds_lock);
196 	ASSERT3U(ds->ds_phys->ds_used_bytes, >=, used);
197 	ds->ds_phys->ds_used_bytes -= used;
198 	ASSERT3U(ds->ds_phys->ds_compressed_bytes, >=, compressed);
199 	ds->ds_phys->ds_compressed_bytes -= compressed;
200 	ASSERT3U(ds->ds_phys->ds_uncompressed_bytes, >=, uncompressed);
201 	ds->ds_phys->ds_uncompressed_bytes -= uncompressed;
202 	mutex_exit(&ds->ds_lock);
203 }
204 
205 uint64_t
206 dsl_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 
228 int
229 dsl_dataset_block_freeable(dsl_dataset_t *ds, uint64_t blk_birth)
230 {
231 	return (blk_birth > dsl_dataset_prev_snap_txg(ds));
232 }
233 
234 /* ARGSUSED */
235 static void
236 dsl_dataset_evict(dmu_buf_t *db, void *dsv)
237 {
238 	dsl_dataset_t *ds = dsv;
239 
240 	/* open_refcount == DS_REF_MAX when deleting */
241 	ASSERT(ds->ds_open_refcount == 0 ||
242 	    ds->ds_open_refcount == DS_REF_MAX);
243 
244 	dprintf_ds(ds, "evicting %s\n", "");
245 
246 	unique_remove(ds->ds_fsid_guid);
247 
248 	if (ds->ds_user_ptr != NULL)
249 		ds->ds_user_evict_func(ds, ds->ds_user_ptr);
250 
251 	if (ds->ds_prev) {
252 		dsl_dataset_close(ds->ds_prev, DS_MODE_NONE, ds);
253 		ds->ds_prev = NULL;
254 	}
255 
256 	bplist_close(&ds->ds_deadlist);
257 	dsl_dir_close(ds->ds_dir, ds);
258 
259 	ASSERT(!list_link_active(&ds->ds_synced_link));
260 
261 	mutex_destroy(&ds->ds_lock);
262 	mutex_destroy(&ds->ds_opening_lock);
263 	mutex_destroy(&ds->ds_deadlist.bpl_lock);
264 
265 	kmem_free(ds, sizeof (dsl_dataset_t));
266 }
267 
268 static int
269 dsl_dataset_get_snapname(dsl_dataset_t *ds)
270 {
271 	dsl_dataset_phys_t *headphys;
272 	int err;
273 	dmu_buf_t *headdbuf;
274 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
275 	objset_t *mos = dp->dp_meta_objset;
276 
277 	if (ds->ds_snapname[0])
278 		return (0);
279 	if (ds->ds_phys->ds_next_snap_obj == 0)
280 		return (0);
281 
282 	err = dmu_bonus_hold(mos, ds->ds_dir->dd_phys->dd_head_dataset_obj,
283 	    FTAG, &headdbuf);
284 	if (err)
285 		return (err);
286 	headphys = headdbuf->db_data;
287 	err = zap_value_search(dp->dp_meta_objset,
288 	    headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname);
289 	dmu_buf_rele(headdbuf, FTAG);
290 	return (err);
291 }
292 
293 static int
294 dsl_dataset_snap_lookup(objset_t *os, uint64_t flags,
295     uint64_t snapnames_zapobj, const char *name, uint64_t *value)
296 {
297 	matchtype_t mt;
298 	int err;
299 
300 	if (flags & DS_FLAG_CI_DATASET)
301 		mt = MT_FIRST;
302 	else
303 		mt = MT_EXACT;
304 
305 	err = zap_lookup_norm(os, snapnames_zapobj, name, 8, 1,
306 	    value, mt, NULL, 0, NULL);
307 	if (err == ENOTSUP && mt == MT_FIRST)
308 		err = zap_lookup(os, snapnames_zapobj, name, 8, 1, value);
309 	return (err);
310 }
311 
312 static int
313 dsl_dataset_snap_remove(objset_t *os, uint64_t flags,
314     uint64_t snapnames_zapobj, char *name, dmu_tx_t *tx)
315 {
316 	matchtype_t mt;
317 	int err;
318 
319 	if (flags & DS_FLAG_CI_DATASET)
320 		mt = MT_FIRST;
321 	else
322 		mt = MT_EXACT;
323 
324 	err = zap_remove_norm(os, snapnames_zapobj, name, mt, tx);
325 	if (err == ENOTSUP && mt == MT_FIRST)
326 		err = zap_remove(os, snapnames_zapobj, name, tx);
327 	return (err);
328 }
329 
330 int
331 dsl_dataset_open_obj(dsl_pool_t *dp, uint64_t dsobj, const char *snapname,
332     int mode, void *tag, dsl_dataset_t **dsp)
333 {
334 	uint64_t weight = ds_refcnt_weight[DS_MODE_LEVEL(mode)];
335 	objset_t *mos = dp->dp_meta_objset;
336 	dmu_buf_t *dbuf;
337 	dsl_dataset_t *ds;
338 	int err;
339 
340 	ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) ||
341 	    dsl_pool_sync_context(dp));
342 
343 	err = dmu_bonus_hold(mos, dsobj, tag, &dbuf);
344 	if (err)
345 		return (err);
346 	ds = dmu_buf_get_user(dbuf);
347 	if (ds == NULL) {
348 		dsl_dataset_t *winner;
349 
350 		ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP);
351 		ds->ds_dbuf = dbuf;
352 		ds->ds_object = dsobj;
353 		ds->ds_phys = dbuf->db_data;
354 
355 		mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL);
356 		mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL);
357 		mutex_init(&ds->ds_deadlist.bpl_lock, NULL, MUTEX_DEFAULT,
358 		    NULL);
359 
360 		err = bplist_open(&ds->ds_deadlist,
361 		    mos, ds->ds_phys->ds_deadlist_obj);
362 		if (err == 0) {
363 			err = dsl_dir_open_obj(dp,
364 			    ds->ds_phys->ds_dir_obj, NULL, ds, &ds->ds_dir);
365 		}
366 		if (err) {
367 			/*
368 			 * we don't really need to close the blist if we
369 			 * just opened it.
370 			 */
371 			mutex_destroy(&ds->ds_lock);
372 			mutex_destroy(&ds->ds_opening_lock);
373 			mutex_destroy(&ds->ds_deadlist.bpl_lock);
374 			kmem_free(ds, sizeof (dsl_dataset_t));
375 			dmu_buf_rele(dbuf, tag);
376 			return (err);
377 		}
378 
379 		if (ds->ds_dir->dd_phys->dd_head_dataset_obj == dsobj) {
380 			ds->ds_snapname[0] = '\0';
381 			if (ds->ds_phys->ds_prev_snap_obj) {
382 				err = dsl_dataset_open_obj(dp,
383 				    ds->ds_phys->ds_prev_snap_obj, NULL,
384 				    DS_MODE_NONE, ds, &ds->ds_prev);
385 			}
386 		} else {
387 			if (snapname) {
388 #ifdef ZFS_DEBUG
389 				dsl_dataset_phys_t *headphys;
390 				dmu_buf_t *headdbuf;
391 				err = dmu_bonus_hold(mos,
392 				    ds->ds_dir->dd_phys->dd_head_dataset_obj,
393 				    FTAG, &headdbuf);
394 				if (err == 0) {
395 					uint64_t foundobj;
396 
397 					headphys = headdbuf->db_data;
398 					err = dsl_dataset_snap_lookup(
399 					    dp->dp_meta_objset,
400 					    headphys->ds_flags,
401 					    headphys->ds_snapnames_zapobj,
402 					    snapname, &foundobj);
403 					ASSERT3U(foundobj, ==, dsobj);
404 					dmu_buf_rele(headdbuf, FTAG);
405 				}
406 #endif
407 				(void) strcat(ds->ds_snapname, snapname);
408 			} else if (zfs_flags & ZFS_DEBUG_SNAPNAMES) {
409 				err = dsl_dataset_get_snapname(ds);
410 			}
411 		}
412 
413 		if (!dsl_dataset_is_snapshot(ds)) {
414 			/*
415 			 * In sync context, we're called with either no lock
416 			 * or with the write lock.  If we're not syncing,
417 			 * we're always called with the read lock held.
418 			 */
419 			boolean_t need_lock =
420 			    !RW_WRITE_HELD(&dp->dp_config_rwlock) &&
421 			    dsl_pool_sync_context(dp);
422 
423 			if (need_lock)
424 				rw_enter(&dp->dp_config_rwlock, RW_READER);
425 
426 			err = dsl_prop_get_ds_locked(ds->ds_dir,
427 			    "refreservation", sizeof (uint64_t), 1,
428 			    &ds->ds_reserved, NULL);
429 			if (err == 0) {
430 				err = dsl_prop_get_ds_locked(ds->ds_dir,
431 				    "refquota", sizeof (uint64_t), 1,
432 				    &ds->ds_quota, NULL);
433 			}
434 
435 			if (need_lock)
436 				rw_exit(&dp->dp_config_rwlock);
437 		} else {
438 			ds->ds_reserved = ds->ds_quota = 0;
439 		}
440 
441 		if (err == 0) {
442 			winner = dmu_buf_set_user_ie(dbuf, ds, &ds->ds_phys,
443 			    dsl_dataset_evict);
444 		}
445 		if (err || winner) {
446 			bplist_close(&ds->ds_deadlist);
447 			if (ds->ds_prev) {
448 				dsl_dataset_close(ds->ds_prev,
449 				    DS_MODE_NONE, ds);
450 			}
451 			dsl_dir_close(ds->ds_dir, ds);
452 			mutex_destroy(&ds->ds_lock);
453 			mutex_destroy(&ds->ds_opening_lock);
454 			mutex_destroy(&ds->ds_deadlist.bpl_lock);
455 			kmem_free(ds, sizeof (dsl_dataset_t));
456 			if (err) {
457 				dmu_buf_rele(dbuf, tag);
458 				return (err);
459 			}
460 			ds = winner;
461 		} else {
462 			ds->ds_fsid_guid =
463 			    unique_insert(ds->ds_phys->ds_fsid_guid);
464 		}
465 	}
466 	ASSERT3P(ds->ds_dbuf, ==, dbuf);
467 	ASSERT3P(ds->ds_phys, ==, dbuf->db_data);
468 
469 	mutex_enter(&ds->ds_lock);
470 	if ((DS_MODE_LEVEL(mode) == DS_MODE_PRIMARY &&
471 	    (ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT) &&
472 	    !DS_MODE_IS_INCONSISTENT(mode)) ||
473 	    (ds->ds_open_refcount + weight > DS_REF_MAX)) {
474 		mutex_exit(&ds->ds_lock);
475 		dsl_dataset_close(ds, DS_MODE_NONE, tag);
476 		return (EBUSY);
477 	}
478 	ds->ds_open_refcount += weight;
479 	mutex_exit(&ds->ds_lock);
480 
481 	*dsp = ds;
482 	return (0);
483 }
484 
485 int
486 dsl_dataset_open_spa(spa_t *spa, const char *name, int mode,
487     void *tag, dsl_dataset_t **dsp)
488 {
489 	dsl_dir_t *dd;
490 	dsl_pool_t *dp;
491 	const char *tail;
492 	uint64_t obj;
493 	dsl_dataset_t *ds = NULL;
494 	int err = 0;
495 
496 	err = dsl_dir_open_spa(spa, name, FTAG, &dd, &tail);
497 	if (err)
498 		return (err);
499 
500 	dp = dd->dd_pool;
501 	obj = dd->dd_phys->dd_head_dataset_obj;
502 	rw_enter(&dp->dp_config_rwlock, RW_READER);
503 	if (obj == 0) {
504 		/* A dataset with no associated objset */
505 		err = ENOENT;
506 		goto out;
507 	}
508 
509 	if (tail != NULL) {
510 		objset_t *mos = dp->dp_meta_objset;
511 		uint64_t flags;
512 
513 		err = dsl_dataset_open_obj(dp, obj, NULL,
514 		    DS_MODE_NONE, tag, &ds);
515 		if (err)
516 			goto out;
517 		flags = ds->ds_phys->ds_flags;
518 		obj = ds->ds_phys->ds_snapnames_zapobj;
519 		dsl_dataset_close(ds, DS_MODE_NONE, tag);
520 		ds = NULL;
521 
522 		if (tail[0] != '@') {
523 			err = ENOENT;
524 			goto out;
525 		}
526 		tail++;
527 
528 		/* Look for a snapshot */
529 		if (!DS_MODE_IS_READONLY(mode)) {
530 			err = EROFS;
531 			goto out;
532 		}
533 		dprintf("looking for snapshot '%s'\n", tail);
534 		err = dsl_dataset_snap_lookup(mos, flags, obj, tail, &obj);
535 		if (err)
536 			goto out;
537 	}
538 	err = dsl_dataset_open_obj(dp, obj, tail, mode, tag, &ds);
539 
540 out:
541 	rw_exit(&dp->dp_config_rwlock);
542 	dsl_dir_close(dd, FTAG);
543 
544 	ASSERT3U((err == 0), ==, (ds != NULL));
545 	/* ASSERT(ds == NULL || strcmp(name, ds->ds_name) == 0); */
546 
547 	*dsp = ds;
548 	return (err);
549 }
550 
551 int
552 dsl_dataset_open(const char *name, int mode, void *tag, dsl_dataset_t **dsp)
553 {
554 	return (dsl_dataset_open_spa(NULL, name, mode, tag, dsp));
555 }
556 
557 void
558 dsl_dataset_name(dsl_dataset_t *ds, char *name)
559 {
560 	if (ds == NULL) {
561 		(void) strcpy(name, "mos");
562 	} else {
563 		dsl_dir_name(ds->ds_dir, name);
564 		VERIFY(0 == dsl_dataset_get_snapname(ds));
565 		if (ds->ds_snapname[0]) {
566 			(void) strcat(name, "@");
567 			if (!MUTEX_HELD(&ds->ds_lock)) {
568 				/*
569 				 * We use a "recursive" mutex so that we
570 				 * can call dprintf_ds() with ds_lock held.
571 				 */
572 				mutex_enter(&ds->ds_lock);
573 				(void) strcat(name, ds->ds_snapname);
574 				mutex_exit(&ds->ds_lock);
575 			} else {
576 				(void) strcat(name, ds->ds_snapname);
577 			}
578 		}
579 	}
580 }
581 
582 static int
583 dsl_dataset_namelen(dsl_dataset_t *ds)
584 {
585 	int result;
586 
587 	if (ds == NULL) {
588 		result = 3;	/* "mos" */
589 	} else {
590 		result = dsl_dir_namelen(ds->ds_dir);
591 		VERIFY(0 == dsl_dataset_get_snapname(ds));
592 		if (ds->ds_snapname[0]) {
593 			++result;	/* adding one for the @-sign */
594 			if (!MUTEX_HELD(&ds->ds_lock)) {
595 				/* see dsl_datset_name */
596 				mutex_enter(&ds->ds_lock);
597 				result += strlen(ds->ds_snapname);
598 				mutex_exit(&ds->ds_lock);
599 			} else {
600 				result += strlen(ds->ds_snapname);
601 			}
602 		}
603 	}
604 
605 	return (result);
606 }
607 
608 void
609 dsl_dataset_close(dsl_dataset_t *ds, int mode, void *tag)
610 {
611 	uint64_t weight = ds_refcnt_weight[DS_MODE_LEVEL(mode)];
612 	mutex_enter(&ds->ds_lock);
613 	ASSERT3U(ds->ds_open_refcount, >=, weight);
614 	ds->ds_open_refcount -= weight;
615 	mutex_exit(&ds->ds_lock);
616 
617 	dmu_buf_rele(ds->ds_dbuf, tag);
618 }
619 
620 void
621 dsl_dataset_downgrade(dsl_dataset_t *ds, int oldmode, int newmode)
622 {
623 	uint64_t oldweight = ds_refcnt_weight[DS_MODE_LEVEL(oldmode)];
624 	uint64_t newweight = ds_refcnt_weight[DS_MODE_LEVEL(newmode)];
625 	mutex_enter(&ds->ds_lock);
626 	ASSERT3U(ds->ds_open_refcount, >=, oldweight);
627 	ASSERT3U(oldweight, >=, newweight);
628 	ds->ds_open_refcount -= oldweight;
629 	ds->ds_open_refcount += newweight;
630 	mutex_exit(&ds->ds_lock);
631 }
632 
633 boolean_t
634 dsl_dataset_tryupgrade(dsl_dataset_t *ds, int oldmode, int newmode)
635 {
636 	boolean_t rv;
637 	uint64_t oldweight = ds_refcnt_weight[DS_MODE_LEVEL(oldmode)];
638 	uint64_t newweight = ds_refcnt_weight[DS_MODE_LEVEL(newmode)];
639 	mutex_enter(&ds->ds_lock);
640 	ASSERT3U(ds->ds_open_refcount, >=, oldweight);
641 	ASSERT3U(newweight, >=, oldweight);
642 	if (ds->ds_open_refcount - oldweight + newweight > DS_REF_MAX) {
643 		rv = B_FALSE;
644 	} else {
645 		ds->ds_open_refcount -= oldweight;
646 		ds->ds_open_refcount += newweight;
647 		rv = B_TRUE;
648 	}
649 	mutex_exit(&ds->ds_lock);
650 	return (rv);
651 }
652 
653 void
654 dsl_dataset_create_root(dsl_pool_t *dp, uint64_t *ddobjp, dmu_tx_t *tx)
655 {
656 	objset_t *mos = dp->dp_meta_objset;
657 	dmu_buf_t *dbuf;
658 	dsl_dataset_phys_t *dsphys;
659 	dsl_dataset_t *ds;
660 	uint64_t dsobj;
661 	dsl_dir_t *dd;
662 
663 	dsl_dir_create_root(mos, ddobjp, tx);
664 	VERIFY(0 == dsl_dir_open_obj(dp, *ddobjp, NULL, FTAG, &dd));
665 
666 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
667 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
668 	VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
669 	dmu_buf_will_dirty(dbuf, tx);
670 	dsphys = dbuf->db_data;
671 	dsphys->ds_dir_obj = dd->dd_object;
672 	dsphys->ds_fsid_guid = unique_create();
673 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
674 	    sizeof (dsphys->ds_guid));
675 	dsphys->ds_snapnames_zapobj =
676 	    zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP,
677 	    DMU_OT_NONE, 0, tx);
678 	dsphys->ds_creation_time = gethrestime_sec();
679 	dsphys->ds_creation_txg = tx->tx_txg;
680 	dsphys->ds_deadlist_obj =
681 	    bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx);
682 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
683 		dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
684 	dmu_buf_rele(dbuf, FTAG);
685 
686 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
687 	dd->dd_phys->dd_head_dataset_obj = dsobj;
688 	dsl_dir_close(dd, FTAG);
689 
690 	VERIFY(0 ==
691 	    dsl_dataset_open_obj(dp, dsobj, NULL, DS_MODE_NONE, FTAG, &ds));
692 	(void) dmu_objset_create_impl(dp->dp_spa, ds,
693 	    &ds->ds_phys->ds_bp, DMU_OST_ZFS, tx);
694 	dsl_dataset_close(ds, DS_MODE_NONE, FTAG);
695 }
696 
697 uint64_t
698 dsl_dataset_create_sync_impl(dsl_dir_t *dd, dsl_dataset_t *origin,
699     uint64_t flags, dmu_tx_t *tx)
700 {
701 	dsl_pool_t *dp = dd->dd_pool;
702 	dmu_buf_t *dbuf;
703 	dsl_dataset_phys_t *dsphys;
704 	uint64_t dsobj;
705 	objset_t *mos = dp->dp_meta_objset;
706 
707 	ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp);
708 	ASSERT(origin == NULL || origin->ds_phys->ds_num_children > 0);
709 	ASSERT(dmu_tx_is_syncing(tx));
710 	ASSERT(dd->dd_phys->dd_head_dataset_obj == 0);
711 
712 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
713 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
714 	VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
715 	dmu_buf_will_dirty(dbuf, tx);
716 	dsphys = dbuf->db_data;
717 	dsphys->ds_dir_obj = dd->dd_object;
718 	dsphys->ds_flags = flags;
719 	dsphys->ds_fsid_guid = unique_create();
720 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
721 	    sizeof (dsphys->ds_guid));
722 	dsphys->ds_snapnames_zapobj =
723 	    zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP,
724 	    DMU_OT_NONE, 0, tx);
725 	dsphys->ds_creation_time = gethrestime_sec();
726 	dsphys->ds_creation_txg = tx->tx_txg;
727 	dsphys->ds_deadlist_obj =
728 	    bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx);
729 
730 	if (origin) {
731 		dsphys->ds_prev_snap_obj = origin->ds_object;
732 		dsphys->ds_prev_snap_txg =
733 		    origin->ds_phys->ds_creation_txg;
734 		dsphys->ds_used_bytes =
735 		    origin->ds_phys->ds_used_bytes;
736 		dsphys->ds_compressed_bytes =
737 		    origin->ds_phys->ds_compressed_bytes;
738 		dsphys->ds_uncompressed_bytes =
739 		    origin->ds_phys->ds_uncompressed_bytes;
740 		dsphys->ds_bp = origin->ds_phys->ds_bp;
741 		dsphys->ds_flags |= origin->ds_phys->ds_flags;
742 
743 		dmu_buf_will_dirty(origin->ds_dbuf, tx);
744 		origin->ds_phys->ds_num_children++;
745 
746 		dmu_buf_will_dirty(dd->dd_dbuf, tx);
747 		dd->dd_phys->dd_origin_obj = origin->ds_object;
748 	}
749 
750 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
751 		dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
752 
753 	dmu_buf_rele(dbuf, FTAG);
754 
755 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
756 	dd->dd_phys->dd_head_dataset_obj = dsobj;
757 
758 	return (dsobj);
759 }
760 
761 uint64_t
762 dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname,
763     dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx)
764 {
765 	dsl_pool_t *dp = pdd->dd_pool;
766 	uint64_t dsobj, ddobj;
767 	dsl_dir_t *dd;
768 
769 	ASSERT(lastname[0] != '@');
770 
771 	ddobj = dsl_dir_create_sync(pdd, lastname, tx);
772 	VERIFY(0 == dsl_dir_open_obj(dp, ddobj, lastname, FTAG, &dd));
773 
774 	dsobj = dsl_dataset_create_sync_impl(dd, origin, flags, tx);
775 
776 	dsl_deleg_set_create_perms(dd, tx, cr);
777 
778 	dsl_dir_close(dd, FTAG);
779 
780 	return (dsobj);
781 }
782 
783 struct destroyarg {
784 	dsl_sync_task_group_t *dstg;
785 	char *snapname;
786 	char *failed;
787 };
788 
789 static int
790 dsl_snapshot_destroy_one(char *name, void *arg)
791 {
792 	struct destroyarg *da = arg;
793 	dsl_dataset_t *ds;
794 	char *cp;
795 	int err;
796 
797 	(void) strcat(name, "@");
798 	(void) strcat(name, da->snapname);
799 	err = dsl_dataset_open(name,
800 	    DS_MODE_EXCLUSIVE | DS_MODE_READONLY | DS_MODE_INCONSISTENT,
801 	    da->dstg, &ds);
802 	cp = strchr(name, '@');
803 	*cp = '\0';
804 	if (err == ENOENT)
805 		return (0);
806 	if (err) {
807 		(void) strcpy(da->failed, name);
808 		return (err);
809 	}
810 
811 	dsl_sync_task_create(da->dstg, dsl_dataset_destroy_check,
812 	    dsl_dataset_destroy_sync, ds, da->dstg, 0);
813 	return (0);
814 }
815 
816 /*
817  * Destroy 'snapname' in all descendants of 'fsname'.
818  */
819 #pragma weak dmu_snapshots_destroy = dsl_snapshots_destroy
820 int
821 dsl_snapshots_destroy(char *fsname, char *snapname)
822 {
823 	int err;
824 	struct destroyarg da;
825 	dsl_sync_task_t *dst;
826 	spa_t *spa;
827 
828 	err = spa_open(fsname, &spa, FTAG);
829 	if (err)
830 		return (err);
831 	da.dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
832 	da.snapname = snapname;
833 	da.failed = fsname;
834 
835 	err = dmu_objset_find(fsname,
836 	    dsl_snapshot_destroy_one, &da, DS_FIND_CHILDREN);
837 
838 	if (err == 0)
839 		err = dsl_sync_task_group_wait(da.dstg);
840 
841 	for (dst = list_head(&da.dstg->dstg_tasks); dst;
842 	    dst = list_next(&da.dstg->dstg_tasks, dst)) {
843 		dsl_dataset_t *ds = dst->dst_arg1;
844 		if (dst->dst_err) {
845 			dsl_dataset_name(ds, fsname);
846 			*strchr(fsname, '@') = '\0';
847 		}
848 		/*
849 		 * If it was successful, destroy_sync would have
850 		 * closed the ds
851 		 */
852 		if (err)
853 			dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, da.dstg);
854 	}
855 
856 	dsl_sync_task_group_destroy(da.dstg);
857 	spa_close(spa, FTAG);
858 	return (err);
859 }
860 
861 /*
862  * ds must be opened EXCLUSIVE or PRIMARY.  on return (whether
863  * successful or not), ds will be closed and caller can no longer
864  * dereference it.
865  */
866 int
867 dsl_dataset_destroy(dsl_dataset_t *ds, void *tag)
868 {
869 	int err;
870 	dsl_sync_task_group_t *dstg;
871 	objset_t *os;
872 	dsl_dir_t *dd;
873 	uint64_t obj;
874 
875 	if (ds->ds_open_refcount != DS_REF_MAX) {
876 		if (dsl_dataset_tryupgrade(ds, DS_MODE_PRIMARY,
877 		    DS_MODE_EXCLUSIVE) == 0) {
878 			dsl_dataset_close(ds, DS_MODE_PRIMARY, tag);
879 			return (EBUSY);
880 		}
881 	}
882 
883 	if (dsl_dataset_is_snapshot(ds)) {
884 		/* Destroying a snapshot is simpler */
885 		err = dsl_sync_task_do(ds->ds_dir->dd_pool,
886 		    dsl_dataset_destroy_check, dsl_dataset_destroy_sync,
887 		    ds, tag, 0);
888 		goto out;
889 	}
890 
891 	dd = ds->ds_dir;
892 
893 	/*
894 	 * Check for errors and mark this ds as inconsistent, in
895 	 * case we crash while freeing the objects.
896 	 */
897 	err = dsl_sync_task_do(dd->dd_pool, dsl_dataset_destroy_begin_check,
898 	    dsl_dataset_destroy_begin_sync, ds, NULL, 0);
899 	if (err)
900 		goto out;
901 
902 	err = dmu_objset_open_ds(ds, DMU_OST_ANY, &os);
903 	if (err)
904 		goto out;
905 
906 	/*
907 	 * remove the objects in open context, so that we won't
908 	 * have too much to do in syncing context.
909 	 */
910 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE,
911 	    ds->ds_phys->ds_prev_snap_txg)) {
912 		dmu_tx_t *tx = dmu_tx_create(os);
913 		dmu_tx_hold_free(tx, obj, 0, DMU_OBJECT_END);
914 		dmu_tx_hold_bonus(tx, obj);
915 		err = dmu_tx_assign(tx, TXG_WAIT);
916 		if (err) {
917 			/*
918 			 * Perhaps there is not enough disk
919 			 * space.  Just deal with it from
920 			 * dsl_dataset_destroy_sync().
921 			 */
922 			dmu_tx_abort(tx);
923 			continue;
924 		}
925 		VERIFY(0 == dmu_object_free(os, obj, tx));
926 		dmu_tx_commit(tx);
927 	}
928 	/* Make sure it's not dirty before we finish destroying it. */
929 	txg_wait_synced(dd->dd_pool, 0);
930 
931 	dmu_objset_close(os);
932 	if (err != ESRCH)
933 		goto out;
934 
935 	if (ds->ds_user_ptr) {
936 		ds->ds_user_evict_func(ds, ds->ds_user_ptr);
937 		ds->ds_user_ptr = NULL;
938 	}
939 
940 	rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
941 	err = dsl_dir_open_obj(dd->dd_pool, dd->dd_object, NULL, FTAG, &dd);
942 	rw_exit(&dd->dd_pool->dp_config_rwlock);
943 
944 	if (err)
945 		goto out;
946 
947 	/*
948 	 * Blow away the dsl_dir + head dataset.
949 	 */
950 	dstg = dsl_sync_task_group_create(ds->ds_dir->dd_pool);
951 	dsl_sync_task_create(dstg, dsl_dataset_destroy_check,
952 	    dsl_dataset_destroy_sync, ds, tag, 0);
953 	dsl_sync_task_create(dstg, dsl_dir_destroy_check,
954 	    dsl_dir_destroy_sync, dd, FTAG, 0);
955 	err = dsl_sync_task_group_wait(dstg);
956 	dsl_sync_task_group_destroy(dstg);
957 	/* if it is successful, *destroy_sync will close the ds+dd */
958 	if (err)
959 		dsl_dir_close(dd, FTAG);
960 out:
961 	if (err)
962 		dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, tag);
963 	return (err);
964 }
965 
966 int
967 dsl_dataset_rollback(dsl_dataset_t *ds, dmu_objset_type_t ost)
968 {
969 	ASSERT3U(ds->ds_open_refcount, ==, DS_REF_MAX);
970 
971 	return (dsl_sync_task_do(ds->ds_dir->dd_pool,
972 	    dsl_dataset_rollback_check, dsl_dataset_rollback_sync,
973 	    ds, &ost, 0));
974 }
975 
976 void *
977 dsl_dataset_set_user_ptr(dsl_dataset_t *ds,
978     void *p, dsl_dataset_evict_func_t func)
979 {
980 	void *old;
981 
982 	mutex_enter(&ds->ds_lock);
983 	old = ds->ds_user_ptr;
984 	if (old == NULL) {
985 		ds->ds_user_ptr = p;
986 		ds->ds_user_evict_func = func;
987 	}
988 	mutex_exit(&ds->ds_lock);
989 	return (old);
990 }
991 
992 void *
993 dsl_dataset_get_user_ptr(dsl_dataset_t *ds)
994 {
995 	return (ds->ds_user_ptr);
996 }
997 
998 
999 blkptr_t *
1000 dsl_dataset_get_blkptr(dsl_dataset_t *ds)
1001 {
1002 	return (&ds->ds_phys->ds_bp);
1003 }
1004 
1005 void
1006 dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx)
1007 {
1008 	ASSERT(dmu_tx_is_syncing(tx));
1009 	/* If it's the meta-objset, set dp_meta_rootbp */
1010 	if (ds == NULL) {
1011 		tx->tx_pool->dp_meta_rootbp = *bp;
1012 	} else {
1013 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
1014 		ds->ds_phys->ds_bp = *bp;
1015 	}
1016 }
1017 
1018 spa_t *
1019 dsl_dataset_get_spa(dsl_dataset_t *ds)
1020 {
1021 	return (ds->ds_dir->dd_pool->dp_spa);
1022 }
1023 
1024 void
1025 dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx)
1026 {
1027 	dsl_pool_t *dp;
1028 
1029 	if (ds == NULL) /* this is the meta-objset */
1030 		return;
1031 
1032 	ASSERT(ds->ds_user_ptr != NULL);
1033 
1034 	if (ds->ds_phys->ds_next_snap_obj != 0)
1035 		panic("dirtying snapshot!");
1036 
1037 	dp = ds->ds_dir->dd_pool;
1038 
1039 	if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg) == 0) {
1040 		/* up the hold count until we can be written out */
1041 		dmu_buf_add_ref(ds->ds_dbuf, ds);
1042 	}
1043 }
1044 
1045 /*
1046  * The unique space in the head dataset can be calculated by subtracting
1047  * the space used in the most recent snapshot, that is still being used
1048  * in this file system, from the space currently in use.  To figure out
1049  * the space in the most recent snapshot still in use, we need to take
1050  * the total space used in the snapshot and subtract out the space that
1051  * has been freed up since the snapshot was taken.
1052  */
1053 static void
1054 dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds)
1055 {
1056 	uint64_t mrs_used;
1057 	uint64_t dlused, dlcomp, dluncomp;
1058 
1059 	ASSERT(ds->ds_object == ds->ds_dir->dd_phys->dd_head_dataset_obj);
1060 
1061 	if (ds->ds_phys->ds_prev_snap_obj != 0)
1062 		mrs_used = ds->ds_prev->ds_phys->ds_used_bytes;
1063 	else
1064 		mrs_used = 0;
1065 
1066 	VERIFY(0 == bplist_space(&ds->ds_deadlist, &dlused, &dlcomp,
1067 	    &dluncomp));
1068 
1069 	ASSERT3U(dlused, <=, mrs_used);
1070 	ds->ds_phys->ds_unique_bytes =
1071 	    ds->ds_phys->ds_used_bytes - (mrs_used - dlused);
1072 
1073 	if (!DS_UNIQUE_IS_ACCURATE(ds) &&
1074 	    spa_version(ds->ds_dir->dd_pool->dp_spa) >=
1075 	    SPA_VERSION_UNIQUE_ACCURATE)
1076 		ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1077 }
1078 
1079 static uint64_t
1080 dsl_dataset_unique(dsl_dataset_t *ds)
1081 {
1082 	if (!DS_UNIQUE_IS_ACCURATE(ds) && !dsl_dataset_is_snapshot(ds))
1083 		dsl_dataset_recalc_head_uniq(ds);
1084 
1085 	return (ds->ds_phys->ds_unique_bytes);
1086 }
1087 
1088 struct killarg {
1089 	int64_t *usedp;
1090 	int64_t *compressedp;
1091 	int64_t *uncompressedp;
1092 	zio_t *zio;
1093 	dmu_tx_t *tx;
1094 };
1095 
1096 static int
1097 kill_blkptr(traverse_blk_cache_t *bc, spa_t *spa, void *arg)
1098 {
1099 	struct killarg *ka = arg;
1100 	blkptr_t *bp = &bc->bc_blkptr;
1101 
1102 	ASSERT3U(bc->bc_errno, ==, 0);
1103 
1104 	/*
1105 	 * Since this callback is not called concurrently, no lock is
1106 	 * needed on the accounting values.
1107 	 */
1108 	*ka->usedp += bp_get_dasize(spa, bp);
1109 	*ka->compressedp += BP_GET_PSIZE(bp);
1110 	*ka->uncompressedp += BP_GET_UCSIZE(bp);
1111 	/* XXX check for EIO? */
1112 	(void) arc_free(ka->zio, spa, ka->tx->tx_txg, bp, NULL, NULL,
1113 	    ARC_NOWAIT);
1114 	return (0);
1115 }
1116 
1117 /* ARGSUSED */
1118 static int
1119 dsl_dataset_rollback_check(void *arg1, void *arg2, dmu_tx_t *tx)
1120 {
1121 	dsl_dataset_t *ds = arg1;
1122 	dmu_objset_type_t *ost = arg2;
1123 
1124 	/*
1125 	 * We can only roll back to emptyness if it is a ZPL objset.
1126 	 */
1127 	if (*ost != DMU_OST_ZFS && ds->ds_phys->ds_prev_snap_txg == 0)
1128 		return (EINVAL);
1129 
1130 	/*
1131 	 * This must not be a snapshot.
1132 	 */
1133 	if (ds->ds_phys->ds_next_snap_obj != 0)
1134 		return (EINVAL);
1135 
1136 	/*
1137 	 * If we made changes this txg, traverse_dsl_dataset won't find
1138 	 * them.  Try again.
1139 	 */
1140 	if (ds->ds_phys->ds_bp.blk_birth >= tx->tx_txg)
1141 		return (EAGAIN);
1142 
1143 	return (0);
1144 }
1145 
1146 /* ARGSUSED */
1147 static void
1148 dsl_dataset_rollback_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
1149 {
1150 	dsl_dataset_t *ds = arg1;
1151 	dmu_objset_type_t *ost = arg2;
1152 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
1153 
1154 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1155 
1156 	/*
1157 	 * Before the roll back destroy the zil.
1158 	 */
1159 	if (ds->ds_user_ptr != NULL) {
1160 		zil_rollback_destroy(
1161 		    ((objset_impl_t *)ds->ds_user_ptr)->os_zil, tx);
1162 
1163 		/*
1164 		 * We need to make sure that the objset_impl_t is reopened after
1165 		 * we do the rollback, otherwise it will have the wrong
1166 		 * objset_phys_t.  Normally this would happen when this
1167 		 * DS_MODE_EXCLUSIVE dataset-open is closed, thus causing the
1168 		 * dataset to be immediately evicted.  But when doing "zfs recv
1169 		 * -F", we reopen the objset before that, so that there is no
1170 		 * window where the dataset is closed and inconsistent.
1171 		 */
1172 		ds->ds_user_evict_func(ds, ds->ds_user_ptr);
1173 		ds->ds_user_ptr = NULL;
1174 	}
1175 
1176 	/* Zero out the deadlist. */
1177 	bplist_close(&ds->ds_deadlist);
1178 	bplist_destroy(mos, ds->ds_phys->ds_deadlist_obj, tx);
1179 	ds->ds_phys->ds_deadlist_obj =
1180 	    bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx);
1181 	VERIFY(0 == bplist_open(&ds->ds_deadlist, mos,
1182 	    ds->ds_phys->ds_deadlist_obj));
1183 
1184 	{
1185 		/* Free blkptrs that we gave birth to */
1186 		zio_t *zio;
1187 		int64_t used = 0, compressed = 0, uncompressed = 0;
1188 		struct killarg ka;
1189 		int64_t delta;
1190 
1191 		zio = zio_root(tx->tx_pool->dp_spa, NULL, NULL,
1192 		    ZIO_FLAG_MUSTSUCCEED);
1193 		ka.usedp = &used;
1194 		ka.compressedp = &compressed;
1195 		ka.uncompressedp = &uncompressed;
1196 		ka.zio = zio;
1197 		ka.tx = tx;
1198 		(void) traverse_dsl_dataset(ds, ds->ds_phys->ds_prev_snap_txg,
1199 		    ADVANCE_POST, kill_blkptr, &ka);
1200 		(void) zio_wait(zio);
1201 
1202 		/* only deduct space beyond any refreservation */
1203 		delta = parent_delta(ds, -used);
1204 		dsl_dir_diduse_space(ds->ds_dir,
1205 		    delta, -compressed, -uncompressed, tx);
1206 	}
1207 
1208 	if (ds->ds_prev) {
1209 		/* Change our contents to that of the prev snapshot */
1210 		ASSERT3U(ds->ds_prev->ds_object, ==,
1211 		    ds->ds_phys->ds_prev_snap_obj);
1212 		ds->ds_phys->ds_bp = ds->ds_prev->ds_phys->ds_bp;
1213 		ds->ds_phys->ds_used_bytes =
1214 		    ds->ds_prev->ds_phys->ds_used_bytes;
1215 		ds->ds_phys->ds_compressed_bytes =
1216 		    ds->ds_prev->ds_phys->ds_compressed_bytes;
1217 		ds->ds_phys->ds_uncompressed_bytes =
1218 		    ds->ds_prev->ds_phys->ds_uncompressed_bytes;
1219 		ds->ds_phys->ds_flags = ds->ds_prev->ds_phys->ds_flags;
1220 		ds->ds_phys->ds_unique_bytes = 0;
1221 
1222 		if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) {
1223 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1224 			ds->ds_prev->ds_phys->ds_unique_bytes = 0;
1225 		}
1226 	} else {
1227 		/* Zero out our contents, recreate objset */
1228 		bzero(&ds->ds_phys->ds_bp, sizeof (blkptr_t));
1229 		ds->ds_phys->ds_used_bytes = 0;
1230 		ds->ds_phys->ds_compressed_bytes = 0;
1231 		ds->ds_phys->ds_uncompressed_bytes = 0;
1232 		ds->ds_phys->ds_flags = 0;
1233 		ds->ds_phys->ds_unique_bytes = 0;
1234 		(void) dmu_objset_create_impl(ds->ds_dir->dd_pool->dp_spa, ds,
1235 		    &ds->ds_phys->ds_bp, *ost, tx);
1236 	}
1237 
1238 	spa_history_internal_log(LOG_DS_ROLLBACK, ds->ds_dir->dd_pool->dp_spa,
1239 	    tx, cr, "dataset = %llu", ds->ds_object);
1240 }
1241 
1242 /* ARGSUSED */
1243 static int
1244 dsl_dataset_destroy_begin_check(void *arg1, void *arg2, dmu_tx_t *tx)
1245 {
1246 	dsl_dataset_t *ds = arg1;
1247 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
1248 	uint64_t count;
1249 	int err;
1250 
1251 	/*
1252 	 * Can't delete a head dataset if there are snapshots of it.
1253 	 * (Except if the only snapshots are from the branch we cloned
1254 	 * from.)
1255 	 */
1256 	if (ds->ds_prev != NULL &&
1257 	    ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object)
1258 		return (EINVAL);
1259 
1260 	/*
1261 	 * This is really a dsl_dir thing, but check it here so that
1262 	 * we'll be less likely to leave this dataset inconsistent &
1263 	 * nearly destroyed.
1264 	 */
1265 	err = zap_count(mos, ds->ds_dir->dd_phys->dd_child_dir_zapobj, &count);
1266 	if (err)
1267 		return (err);
1268 	if (count != 0)
1269 		return (EEXIST);
1270 
1271 	return (0);
1272 }
1273 
1274 /* ARGSUSED */
1275 static void
1276 dsl_dataset_destroy_begin_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
1277 {
1278 	dsl_dataset_t *ds = arg1;
1279 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1280 
1281 	/* Mark it as inconsistent on-disk, in case we crash */
1282 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1283 	ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT;
1284 
1285 	spa_history_internal_log(LOG_DS_DESTROY_BEGIN, dp->dp_spa, tx,
1286 	    cr, "dataset = %llu", ds->ds_object);
1287 }
1288 
1289 /* ARGSUSED */
1290 int
1291 dsl_dataset_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx)
1292 {
1293 	dsl_dataset_t *ds = arg1;
1294 
1295 	/* Can't delete a branch point. */
1296 	if (ds->ds_phys->ds_num_children > 1)
1297 		return (EEXIST);
1298 
1299 	/*
1300 	 * Can't delete a head dataset if there are snapshots of it.
1301 	 * (Except if the only snapshots are from the branch we cloned
1302 	 * from.)
1303 	 */
1304 	if (ds->ds_prev != NULL &&
1305 	    ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object)
1306 		return (EINVAL);
1307 
1308 	/*
1309 	 * If we made changes this txg, traverse_dsl_dataset won't find
1310 	 * them.  Try again.
1311 	 */
1312 	if (ds->ds_phys->ds_bp.blk_birth >= tx->tx_txg)
1313 		return (EAGAIN);
1314 
1315 	/* XXX we should do some i/o error checking... */
1316 	return (0);
1317 }
1318 
1319 void
1320 dsl_dataset_destroy_sync(void *arg1, void *tag, cred_t *cr, dmu_tx_t *tx)
1321 {
1322 	dsl_dataset_t *ds = arg1;
1323 	int64_t used = 0, compressed = 0, uncompressed = 0;
1324 	zio_t *zio;
1325 	int err;
1326 	int after_branch_point = FALSE;
1327 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1328 	objset_t *mos = dp->dp_meta_objset;
1329 	dsl_dataset_t *ds_prev = NULL;
1330 	uint64_t obj;
1331 
1332 	ASSERT3U(ds->ds_open_refcount, ==, DS_REF_MAX);
1333 	ASSERT3U(ds->ds_phys->ds_num_children, <=, 1);
1334 	ASSERT(ds->ds_prev == NULL ||
1335 	    ds->ds_prev->ds_phys->ds_next_snap_obj != ds->ds_object);
1336 	ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg);
1337 
1338 	/* Remove our reservation */
1339 	if (ds->ds_reserved != 0) {
1340 		uint64_t val = 0;
1341 		dsl_dataset_set_reservation_sync(ds, &val, cr, tx);
1342 		ASSERT3U(ds->ds_reserved, ==, 0);
1343 	}
1344 
1345 	ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock));
1346 
1347 	obj = ds->ds_object;
1348 
1349 	if (ds->ds_phys->ds_prev_snap_obj != 0) {
1350 		if (ds->ds_prev) {
1351 			ds_prev = ds->ds_prev;
1352 		} else {
1353 			VERIFY(0 == dsl_dataset_open_obj(dp,
1354 			    ds->ds_phys->ds_prev_snap_obj, NULL,
1355 			    DS_MODE_NONE, FTAG, &ds_prev));
1356 		}
1357 		after_branch_point =
1358 		    (ds_prev->ds_phys->ds_next_snap_obj != obj);
1359 
1360 		dmu_buf_will_dirty(ds_prev->ds_dbuf, tx);
1361 		if (after_branch_point &&
1362 		    ds->ds_phys->ds_next_snap_obj == 0) {
1363 			/* This clone is toast. */
1364 			ASSERT(ds_prev->ds_phys->ds_num_children > 1);
1365 			ds_prev->ds_phys->ds_num_children--;
1366 		} else if (!after_branch_point) {
1367 			ds_prev->ds_phys->ds_next_snap_obj =
1368 			    ds->ds_phys->ds_next_snap_obj;
1369 		}
1370 	}
1371 
1372 	zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
1373 
1374 	if (ds->ds_phys->ds_next_snap_obj != 0) {
1375 		blkptr_t bp;
1376 		dsl_dataset_t *ds_next;
1377 		uint64_t itor = 0;
1378 		uint64_t old_unique;
1379 
1380 		spa_scrub_restart(dp->dp_spa, tx->tx_txg);
1381 
1382 		VERIFY(0 == dsl_dataset_open_obj(dp,
1383 		    ds->ds_phys->ds_next_snap_obj, NULL,
1384 		    DS_MODE_NONE, FTAG, &ds_next));
1385 		ASSERT3U(ds_next->ds_phys->ds_prev_snap_obj, ==, obj);
1386 
1387 		old_unique = dsl_dataset_unique(ds_next);
1388 
1389 		dmu_buf_will_dirty(ds_next->ds_dbuf, tx);
1390 		ds_next->ds_phys->ds_prev_snap_obj =
1391 		    ds->ds_phys->ds_prev_snap_obj;
1392 		ds_next->ds_phys->ds_prev_snap_txg =
1393 		    ds->ds_phys->ds_prev_snap_txg;
1394 		ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
1395 		    ds_prev ? ds_prev->ds_phys->ds_creation_txg : 0);
1396 
1397 		/*
1398 		 * Transfer to our deadlist (which will become next's
1399 		 * new deadlist) any entries from next's current
1400 		 * deadlist which were born before prev, and free the
1401 		 * other entries.
1402 		 *
1403 		 * XXX we're doing this long task with the config lock held
1404 		 */
1405 		while (bplist_iterate(&ds_next->ds_deadlist, &itor,
1406 		    &bp) == 0) {
1407 			if (bp.blk_birth <= ds->ds_phys->ds_prev_snap_txg) {
1408 				VERIFY(0 == bplist_enqueue(&ds->ds_deadlist,
1409 				    &bp, tx));
1410 				if (ds_prev && !after_branch_point &&
1411 				    bp.blk_birth >
1412 				    ds_prev->ds_phys->ds_prev_snap_txg) {
1413 					ds_prev->ds_phys->ds_unique_bytes +=
1414 					    bp_get_dasize(dp->dp_spa, &bp);
1415 				}
1416 			} else {
1417 				used += bp_get_dasize(dp->dp_spa, &bp);
1418 				compressed += BP_GET_PSIZE(&bp);
1419 				uncompressed += BP_GET_UCSIZE(&bp);
1420 				/* XXX check return value? */
1421 				(void) arc_free(zio, dp->dp_spa, tx->tx_txg,
1422 				    &bp, NULL, NULL, ARC_NOWAIT);
1423 			}
1424 		}
1425 
1426 		/* free next's deadlist */
1427 		bplist_close(&ds_next->ds_deadlist);
1428 		bplist_destroy(mos, ds_next->ds_phys->ds_deadlist_obj, tx);
1429 
1430 		/* set next's deadlist to our deadlist */
1431 		ds_next->ds_phys->ds_deadlist_obj =
1432 		    ds->ds_phys->ds_deadlist_obj;
1433 		VERIFY(0 == bplist_open(&ds_next->ds_deadlist, mos,
1434 		    ds_next->ds_phys->ds_deadlist_obj));
1435 		ds->ds_phys->ds_deadlist_obj = 0;
1436 
1437 		if (ds_next->ds_phys->ds_next_snap_obj != 0) {
1438 			/*
1439 			 * Update next's unique to include blocks which
1440 			 * were previously shared by only this snapshot
1441 			 * and it.  Those blocks will be born after the
1442 			 * prev snap and before this snap, and will have
1443 			 * died after the next snap and before the one
1444 			 * after that (ie. be on the snap after next's
1445 			 * deadlist).
1446 			 *
1447 			 * XXX we're doing this long task with the
1448 			 * config lock held
1449 			 */
1450 			dsl_dataset_t *ds_after_next;
1451 
1452 			VERIFY(0 == dsl_dataset_open_obj(dp,
1453 			    ds_next->ds_phys->ds_next_snap_obj, NULL,
1454 			    DS_MODE_NONE, FTAG, &ds_after_next));
1455 			itor = 0;
1456 			while (bplist_iterate(&ds_after_next->ds_deadlist,
1457 			    &itor, &bp) == 0) {
1458 				if (bp.blk_birth >
1459 				    ds->ds_phys->ds_prev_snap_txg &&
1460 				    bp.blk_birth <=
1461 				    ds->ds_phys->ds_creation_txg) {
1462 					ds_next->ds_phys->ds_unique_bytes +=
1463 					    bp_get_dasize(dp->dp_spa, &bp);
1464 				}
1465 			}
1466 
1467 			dsl_dataset_close(ds_after_next, DS_MODE_NONE, FTAG);
1468 			ASSERT3P(ds_next->ds_prev, ==, NULL);
1469 		} else {
1470 			ASSERT3P(ds_next->ds_prev, ==, ds);
1471 			dsl_dataset_close(ds_next->ds_prev, DS_MODE_NONE,
1472 			    ds_next);
1473 			if (ds_prev) {
1474 				VERIFY(0 == dsl_dataset_open_obj(dp,
1475 				    ds->ds_phys->ds_prev_snap_obj, NULL,
1476 				    DS_MODE_NONE, ds_next, &ds_next->ds_prev));
1477 			} else {
1478 				ds_next->ds_prev = NULL;
1479 			}
1480 
1481 			dsl_dataset_recalc_head_uniq(ds_next);
1482 
1483 			/*
1484 			 * Reduce the amount of our unconsmed refreservation
1485 			 * being charged to our parent by the amount of
1486 			 * new unique data we have gained.
1487 			 */
1488 			if (old_unique < ds_next->ds_reserved) {
1489 				int64_t mrsdelta;
1490 				uint64_t new_unique =
1491 				    ds_next->ds_phys->ds_unique_bytes;
1492 
1493 				ASSERT(old_unique <= new_unique);
1494 				mrsdelta = MIN(new_unique - old_unique,
1495 				    ds_next->ds_reserved - old_unique);
1496 				dsl_dir_diduse_space(ds->ds_dir, -mrsdelta,
1497 				    0, 0, tx);
1498 			}
1499 		}
1500 		dsl_dataset_close(ds_next, DS_MODE_NONE, FTAG);
1501 
1502 		/*
1503 		 * NB: unique_bytes might not be accurate for the head objset.
1504 		 * Before SPA_VERSION 9, we didn't update its value when we
1505 		 * deleted the most recent snapshot.
1506 		 */
1507 		ASSERT3U(used, ==, ds->ds_phys->ds_unique_bytes);
1508 	} else {
1509 		/*
1510 		 * There's no next snapshot, so this is a head dataset.
1511 		 * Destroy the deadlist.  Unless it's a clone, the
1512 		 * deadlist should be empty.  (If it's a clone, it's
1513 		 * safe to ignore the deadlist contents.)
1514 		 */
1515 		struct killarg ka;
1516 
1517 		ASSERT(after_branch_point || bplist_empty(&ds->ds_deadlist));
1518 		bplist_close(&ds->ds_deadlist);
1519 		bplist_destroy(mos, ds->ds_phys->ds_deadlist_obj, tx);
1520 		ds->ds_phys->ds_deadlist_obj = 0;
1521 
1522 		/*
1523 		 * Free everything that we point to (that's born after
1524 		 * the previous snapshot, if we are a clone)
1525 		 *
1526 		 * XXX we're doing this long task with the config lock held
1527 		 */
1528 		ka.usedp = &used;
1529 		ka.compressedp = &compressed;
1530 		ka.uncompressedp = &uncompressed;
1531 		ka.zio = zio;
1532 		ka.tx = tx;
1533 		err = traverse_dsl_dataset(ds, ds->ds_phys->ds_prev_snap_txg,
1534 		    ADVANCE_POST, kill_blkptr, &ka);
1535 		ASSERT3U(err, ==, 0);
1536 		ASSERT(spa_version(dp->dp_spa) <
1537 		    SPA_VERSION_UNIQUE_ACCURATE ||
1538 		    used == ds->ds_phys->ds_unique_bytes);
1539 	}
1540 
1541 	err = zio_wait(zio);
1542 	ASSERT3U(err, ==, 0);
1543 
1544 	dsl_dir_diduse_space(ds->ds_dir, -used, -compressed, -uncompressed, tx);
1545 
1546 	if (ds->ds_phys->ds_snapnames_zapobj) {
1547 		err = zap_destroy(mos, ds->ds_phys->ds_snapnames_zapobj, tx);
1548 		ASSERT(err == 0);
1549 	}
1550 
1551 	if (ds->ds_dir->dd_phys->dd_head_dataset_obj == ds->ds_object) {
1552 		/* Erase the link in the dataset */
1553 		dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
1554 		ds->ds_dir->dd_phys->dd_head_dataset_obj = 0;
1555 		/*
1556 		 * dsl_dir_sync_destroy() called us, they'll destroy
1557 		 * the dataset.
1558 		 */
1559 	} else {
1560 		/* remove from snapshot namespace */
1561 		dsl_dataset_t *ds_head;
1562 		VERIFY(0 == dsl_dataset_open_obj(dp,
1563 		    ds->ds_dir->dd_phys->dd_head_dataset_obj, NULL,
1564 		    DS_MODE_NONE, FTAG, &ds_head));
1565 		VERIFY(0 == dsl_dataset_get_snapname(ds));
1566 #ifdef ZFS_DEBUG
1567 		{
1568 			uint64_t val;
1569 
1570 			err = dsl_dataset_snap_lookup(mos,
1571 			    ds_head->ds_phys->ds_flags,
1572 			    ds_head->ds_phys->ds_snapnames_zapobj,
1573 			    ds->ds_snapname, &val);
1574 			ASSERT3U(err, ==, 0);
1575 			ASSERT3U(val, ==, obj);
1576 		}
1577 #endif
1578 		err = dsl_dataset_snap_remove(mos,
1579 		    ds_head->ds_phys->ds_flags,
1580 		    ds_head->ds_phys->ds_snapnames_zapobj,
1581 		    ds->ds_snapname, tx);
1582 		ASSERT(err == 0);
1583 		dsl_dataset_close(ds_head, DS_MODE_NONE, FTAG);
1584 	}
1585 
1586 	if (ds_prev && ds->ds_prev != ds_prev)
1587 		dsl_dataset_close(ds_prev, DS_MODE_NONE, FTAG);
1588 
1589 	spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
1590 	spa_history_internal_log(LOG_DS_DESTROY, dp->dp_spa, tx,
1591 	    cr, "dataset = %llu", ds->ds_object);
1592 
1593 	dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, tag);
1594 	VERIFY(0 == dmu_object_free(mos, obj, tx));
1595 
1596 }
1597 
1598 static int
1599 dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx)
1600 {
1601 	uint64_t asize;
1602 
1603 	if (!dmu_tx_is_syncing(tx))
1604 		return (0);
1605 
1606 	/*
1607 	 * If there's an fs-only reservation, any blocks that might become
1608 	 * owned by the snapshot dataset must be accommodated by space
1609 	 * outside of the reservation.
1610 	 */
1611 	asize = MIN(dsl_dataset_unique(ds), ds->ds_reserved);
1612 	if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, FALSE))
1613 		return (ENOSPC);
1614 
1615 	/*
1616 	 * Propogate any reserved space for this snapshot to other
1617 	 * snapshot checks in this sync group.
1618 	 */
1619 	if (asize > 0)
1620 		dsl_dir_willuse_space(ds->ds_dir, asize, tx);
1621 
1622 	return (0);
1623 }
1624 
1625 /* ARGSUSED */
1626 int
1627 dsl_dataset_snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx)
1628 {
1629 	dsl_dataset_t *ds = arg1;
1630 	const char *snapname = arg2;
1631 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
1632 	int err;
1633 	uint64_t value;
1634 
1635 	/*
1636 	 * We don't allow multiple snapshots of the same txg.  If there
1637 	 * is already one, try again.
1638 	 */
1639 	if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg)
1640 		return (EAGAIN);
1641 
1642 	/*
1643 	 * Check for conflicting name snapshot name.
1644 	 */
1645 	err = dsl_dataset_snap_lookup(mos, ds->ds_phys->ds_flags,
1646 	    ds->ds_phys->ds_snapnames_zapobj, snapname, &value);
1647 	if (err == 0)
1648 		return (EEXIST);
1649 	if (err != ENOENT)
1650 		return (err);
1651 
1652 	/*
1653 	 * Check that the dataset's name is not too long.  Name consists
1654 	 * of the dataset's length + 1 for the @-sign + snapshot name's length
1655 	 */
1656 	if (dsl_dataset_namelen(ds) + 1 + strlen(snapname) >= MAXNAMELEN)
1657 		return (ENAMETOOLONG);
1658 
1659 	err = dsl_dataset_snapshot_reserve_space(ds, tx);
1660 	if (err)
1661 		return (err);
1662 
1663 	ds->ds_trysnap_txg = tx->tx_txg;
1664 	return (0);
1665 }
1666 
1667 void
1668 dsl_dataset_snapshot_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
1669 {
1670 	dsl_dataset_t *ds = arg1;
1671 	const char *snapname = arg2;
1672 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1673 	dmu_buf_t *dbuf;
1674 	dsl_dataset_phys_t *dsphys;
1675 	uint64_t dsobj;
1676 	objset_t *mos = dp->dp_meta_objset;
1677 	int err;
1678 
1679 	spa_scrub_restart(dp->dp_spa, tx->tx_txg);
1680 	ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock));
1681 
1682 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
1683 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
1684 	VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
1685 	dmu_buf_will_dirty(dbuf, tx);
1686 	dsphys = dbuf->db_data;
1687 	dsphys->ds_dir_obj = ds->ds_dir->dd_object;
1688 	dsphys->ds_fsid_guid = unique_create();
1689 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
1690 	    sizeof (dsphys->ds_guid));
1691 	dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj;
1692 	dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg;
1693 	dsphys->ds_next_snap_obj = ds->ds_object;
1694 	dsphys->ds_num_children = 1;
1695 	dsphys->ds_creation_time = gethrestime_sec();
1696 	dsphys->ds_creation_txg = tx->tx_txg;
1697 	dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj;
1698 	dsphys->ds_used_bytes = ds->ds_phys->ds_used_bytes;
1699 	dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes;
1700 	dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes;
1701 	dsphys->ds_flags = ds->ds_phys->ds_flags;
1702 	dsphys->ds_bp = ds->ds_phys->ds_bp;
1703 	dmu_buf_rele(dbuf, FTAG);
1704 
1705 	ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0);
1706 	if (ds->ds_prev) {
1707 		ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj ==
1708 		    ds->ds_object ||
1709 		    ds->ds_prev->ds_phys->ds_num_children > 1);
1710 		if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) {
1711 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1712 			ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
1713 			    ds->ds_prev->ds_phys->ds_creation_txg);
1714 			ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj;
1715 		}
1716 	}
1717 
1718 	/*
1719 	 * If we have a reference-reservation on this dataset, we will
1720 	 * need to increase the amount of refreservation being charged
1721 	 * since our unique space is going to zero.
1722 	 */
1723 	if (ds->ds_reserved) {
1724 		int64_t add = MIN(dsl_dataset_unique(ds), ds->ds_reserved);
1725 		dsl_dir_diduse_space(ds->ds_dir, add, 0, 0, tx);
1726 	}
1727 
1728 	bplist_close(&ds->ds_deadlist);
1729 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1730 	ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, tx->tx_txg);
1731 	ds->ds_phys->ds_prev_snap_obj = dsobj;
1732 	ds->ds_phys->ds_prev_snap_txg = tx->tx_txg;
1733 	ds->ds_phys->ds_unique_bytes = 0;
1734 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
1735 		ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1736 	ds->ds_phys->ds_deadlist_obj =
1737 	    bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx);
1738 	VERIFY(0 == bplist_open(&ds->ds_deadlist, mos,
1739 	    ds->ds_phys->ds_deadlist_obj));
1740 
1741 	dprintf("snap '%s' -> obj %llu\n", snapname, dsobj);
1742 	err = zap_add(mos, ds->ds_phys->ds_snapnames_zapobj,
1743 	    snapname, 8, 1, &dsobj, tx);
1744 	ASSERT(err == 0);
1745 
1746 	if (ds->ds_prev)
1747 		dsl_dataset_close(ds->ds_prev, DS_MODE_NONE, ds);
1748 	VERIFY(0 == dsl_dataset_open_obj(dp,
1749 	    ds->ds_phys->ds_prev_snap_obj, snapname,
1750 	    DS_MODE_NONE, ds, &ds->ds_prev));
1751 
1752 	spa_history_internal_log(LOG_DS_SNAPSHOT, dp->dp_spa, tx, cr,
1753 	    "dataset = %llu", dsobj);
1754 }
1755 
1756 void
1757 dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
1758 {
1759 	ASSERT(dmu_tx_is_syncing(tx));
1760 	ASSERT(ds->ds_user_ptr != NULL);
1761 	ASSERT(ds->ds_phys->ds_next_snap_obj == 0);
1762 
1763 	/*
1764 	 * in case we had to change ds_fsid_guid when we opened it,
1765 	 * sync it out now.
1766 	 */
1767 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1768 	ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid;
1769 
1770 	dsl_dir_dirty(ds->ds_dir, tx);
1771 	dmu_objset_sync(ds->ds_user_ptr, zio, tx);
1772 }
1773 
1774 void
1775 dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
1776 {
1777 	uint64_t refd, avail, uobjs, aobjs;
1778 
1779 	dsl_dir_stats(ds->ds_dir, nv);
1780 
1781 	dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs);
1782 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail);
1783 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd);
1784 
1785 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
1786 	    ds->ds_phys->ds_creation_time);
1787 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
1788 	    ds->ds_phys->ds_creation_txg);
1789 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA,
1790 	    ds->ds_quota);
1791 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION,
1792 	    ds->ds_reserved);
1793 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID,
1794 	    ds->ds_phys->ds_guid);
1795 
1796 	if (ds->ds_phys->ds_next_snap_obj) {
1797 		/*
1798 		 * This is a snapshot; override the dd's space used with
1799 		 * our unique space and compression ratio.
1800 		 */
1801 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
1802 		    ds->ds_phys->ds_unique_bytes);
1803 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
1804 		    ds->ds_phys->ds_compressed_bytes == 0 ? 100 :
1805 		    (ds->ds_phys->ds_uncompressed_bytes * 100 /
1806 		    ds->ds_phys->ds_compressed_bytes));
1807 	}
1808 }
1809 
1810 void
1811 dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
1812 {
1813 	stat->dds_creation_txg = ds->ds_phys->ds_creation_txg;
1814 	stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT;
1815 	stat->dds_guid = ds->ds_phys->ds_guid;
1816 	if (ds->ds_phys->ds_next_snap_obj) {
1817 		stat->dds_is_snapshot = B_TRUE;
1818 		stat->dds_num_clones = ds->ds_phys->ds_num_children - 1;
1819 	}
1820 
1821 	/* clone origin is really a dsl_dir thing... */
1822 	rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER);
1823 	if (ds->ds_dir->dd_phys->dd_origin_obj) {
1824 		dsl_dataset_t *ods;
1825 
1826 		VERIFY(0 == dsl_dataset_open_obj(ds->ds_dir->dd_pool,
1827 		    ds->ds_dir->dd_phys->dd_origin_obj,
1828 		    NULL, DS_MODE_NONE, FTAG, &ods));
1829 		dsl_dataset_name(ods, stat->dds_origin);
1830 		dsl_dataset_close(ods, DS_MODE_NONE, FTAG);
1831 	}
1832 	rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
1833 }
1834 
1835 uint64_t
1836 dsl_dataset_fsid_guid(dsl_dataset_t *ds)
1837 {
1838 	return (ds->ds_fsid_guid);
1839 }
1840 
1841 void
1842 dsl_dataset_space(dsl_dataset_t *ds,
1843     uint64_t *refdbytesp, uint64_t *availbytesp,
1844     uint64_t *usedobjsp, uint64_t *availobjsp)
1845 {
1846 	*refdbytesp = ds->ds_phys->ds_used_bytes;
1847 	*availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
1848 	if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes)
1849 		*availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes;
1850 	if (ds->ds_quota != 0) {
1851 		/*
1852 		 * Adjust available bytes according to refquota
1853 		 */
1854 		if (*refdbytesp < ds->ds_quota)
1855 			*availbytesp = MIN(*availbytesp,
1856 			    ds->ds_quota - *refdbytesp);
1857 		else
1858 			*availbytesp = 0;
1859 	}
1860 	*usedobjsp = ds->ds_phys->ds_bp.blk_fill;
1861 	*availobjsp = DN_MAX_OBJECT - *usedobjsp;
1862 }
1863 
1864 boolean_t
1865 dsl_dataset_modified_since_lastsnap(dsl_dataset_t *ds)
1866 {
1867 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1868 
1869 	ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) ||
1870 	    dsl_pool_sync_context(dp));
1871 	if (ds->ds_prev == NULL)
1872 		return (B_FALSE);
1873 	if (ds->ds_phys->ds_bp.blk_birth >
1874 	    ds->ds_prev->ds_phys->ds_creation_txg)
1875 		return (B_TRUE);
1876 	return (B_FALSE);
1877 }
1878 
1879 /* ARGSUSED */
1880 static int
1881 dsl_dataset_snapshot_rename_check(void *arg1, void *arg2, dmu_tx_t *tx)
1882 {
1883 	dsl_dataset_t *ds = arg1;
1884 	char *newsnapname = arg2;
1885 	dsl_dir_t *dd = ds->ds_dir;
1886 	objset_t *mos = dd->dd_pool->dp_meta_objset;
1887 	dsl_dataset_t *hds;
1888 	uint64_t val;
1889 	int err;
1890 
1891 	err = dsl_dataset_open_obj(dd->dd_pool,
1892 	    dd->dd_phys->dd_head_dataset_obj, NULL, DS_MODE_NONE, FTAG, &hds);
1893 	if (err)
1894 		return (err);
1895 
1896 	/* new name better not be in use */
1897 	err = dsl_dataset_snap_lookup(mos, hds->ds_phys->ds_flags,
1898 	    hds->ds_phys->ds_snapnames_zapobj, newsnapname, &val);
1899 	dsl_dataset_close(hds, DS_MODE_NONE, FTAG);
1900 
1901 	if (err == 0)
1902 		err = EEXIST;
1903 	else if (err == ENOENT)
1904 		err = 0;
1905 
1906 	/* dataset name + 1 for the "@" + the new snapshot name must fit */
1907 	if (dsl_dir_namelen(ds->ds_dir) + 1 + strlen(newsnapname) >= MAXNAMELEN)
1908 		err = ENAMETOOLONG;
1909 
1910 	return (err);
1911 }
1912 
1913 static void
1914 dsl_dataset_snapshot_rename_sync(void *arg1, void *arg2,
1915     cred_t *cr, dmu_tx_t *tx)
1916 {
1917 	dsl_dataset_t *ds = arg1;
1918 	const char *newsnapname = arg2;
1919 	dsl_dir_t *dd = ds->ds_dir;
1920 	objset_t *mos = dd->dd_pool->dp_meta_objset;
1921 	dsl_dataset_t *hds;
1922 	int err;
1923 
1924 	ASSERT(ds->ds_phys->ds_next_snap_obj != 0);
1925 
1926 	VERIFY(0 == dsl_dataset_open_obj(dd->dd_pool,
1927 	    dd->dd_phys->dd_head_dataset_obj, NULL, DS_MODE_NONE, FTAG, &hds));
1928 
1929 	VERIFY(0 == dsl_dataset_get_snapname(ds));
1930 	err = dsl_dataset_snap_remove(mos, hds->ds_phys->ds_flags,
1931 	    hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname, tx);
1932 	ASSERT3U(err, ==, 0);
1933 	mutex_enter(&ds->ds_lock);
1934 	(void) strcpy(ds->ds_snapname, newsnapname);
1935 	mutex_exit(&ds->ds_lock);
1936 	err = zap_add(mos, hds->ds_phys->ds_snapnames_zapobj,
1937 	    ds->ds_snapname, 8, 1, &ds->ds_object, tx);
1938 	ASSERT3U(err, ==, 0);
1939 
1940 	spa_history_internal_log(LOG_DS_RENAME, dd->dd_pool->dp_spa, tx,
1941 	    cr, "dataset = %llu", ds->ds_object);
1942 	dsl_dataset_close(hds, DS_MODE_NONE, FTAG);
1943 }
1944 
1945 struct renamesnaparg {
1946 	dsl_sync_task_group_t *dstg;
1947 	char failed[MAXPATHLEN];
1948 	char *oldsnap;
1949 	char *newsnap;
1950 };
1951 
1952 static int
1953 dsl_snapshot_rename_one(char *name, void *arg)
1954 {
1955 	struct renamesnaparg *ra = arg;
1956 	dsl_dataset_t *ds = NULL;
1957 	char *cp;
1958 	int err;
1959 
1960 	cp = name + strlen(name);
1961 	*cp = '@';
1962 	(void) strcpy(cp + 1, ra->oldsnap);
1963 
1964 	/*
1965 	 * For recursive snapshot renames the parent won't be changing
1966 	 * so we just pass name for both the to/from argument.
1967 	 */
1968 	if (err = zfs_secpolicy_rename_perms(name, name, CRED())) {
1969 		(void) strcpy(ra->failed, name);
1970 		return (err);
1971 	}
1972 
1973 	err = dsl_dataset_open(name, DS_MODE_READONLY | DS_MODE_STANDARD,
1974 	    ra->dstg, &ds);
1975 	if (err == ENOENT) {
1976 		*cp = '\0';
1977 		return (0);
1978 	}
1979 	if (err) {
1980 		(void) strcpy(ra->failed, name);
1981 		*cp = '\0';
1982 		dsl_dataset_close(ds, DS_MODE_STANDARD, ra->dstg);
1983 		return (err);
1984 	}
1985 
1986 #ifdef _KERNEL
1987 	/* for all filesystems undergoing rename, we'll need to unmount it */
1988 	(void) zfs_unmount_snap(name, NULL);
1989 #endif
1990 
1991 	*cp = '\0';
1992 
1993 	dsl_sync_task_create(ra->dstg, dsl_dataset_snapshot_rename_check,
1994 	    dsl_dataset_snapshot_rename_sync, ds, ra->newsnap, 0);
1995 
1996 	return (0);
1997 }
1998 
1999 static int
2000 dsl_recursive_rename(char *oldname, const char *newname)
2001 {
2002 	int err;
2003 	struct renamesnaparg *ra;
2004 	dsl_sync_task_t *dst;
2005 	spa_t *spa;
2006 	char *cp, *fsname = spa_strdup(oldname);
2007 	int len = strlen(oldname);
2008 
2009 	/* truncate the snapshot name to get the fsname */
2010 	cp = strchr(fsname, '@');
2011 	*cp = '\0';
2012 
2013 	err = spa_open(fsname, &spa, FTAG);
2014 	if (err) {
2015 		kmem_free(fsname, len + 1);
2016 		return (err);
2017 	}
2018 	ra = kmem_alloc(sizeof (struct renamesnaparg), KM_SLEEP);
2019 	ra->dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
2020 
2021 	ra->oldsnap = strchr(oldname, '@') + 1;
2022 	ra->newsnap = strchr(newname, '@') + 1;
2023 	*ra->failed = '\0';
2024 
2025 	err = dmu_objset_find(fsname, dsl_snapshot_rename_one, ra,
2026 	    DS_FIND_CHILDREN);
2027 	kmem_free(fsname, len + 1);
2028 
2029 	if (err == 0) {
2030 		err = dsl_sync_task_group_wait(ra->dstg);
2031 	}
2032 
2033 	for (dst = list_head(&ra->dstg->dstg_tasks); dst;
2034 	    dst = list_next(&ra->dstg->dstg_tasks, dst)) {
2035 		dsl_dataset_t *ds = dst->dst_arg1;
2036 		if (dst->dst_err) {
2037 			dsl_dir_name(ds->ds_dir, ra->failed);
2038 			(void) strcat(ra->failed, "@");
2039 			(void) strcat(ra->failed, ra->newsnap);
2040 		}
2041 		dsl_dataset_close(ds, DS_MODE_STANDARD, ra->dstg);
2042 	}
2043 
2044 	if (err)
2045 		(void) strcpy(oldname, ra->failed);
2046 
2047 	dsl_sync_task_group_destroy(ra->dstg);
2048 	kmem_free(ra, sizeof (struct renamesnaparg));
2049 	spa_close(spa, FTAG);
2050 	return (err);
2051 }
2052 
2053 static int
2054 dsl_valid_rename(char *oldname, void *arg)
2055 {
2056 	int delta = *(int *)arg;
2057 
2058 	if (strlen(oldname) + delta >= MAXNAMELEN)
2059 		return (ENAMETOOLONG);
2060 
2061 	return (0);
2062 }
2063 
2064 #pragma weak dmu_objset_rename = dsl_dataset_rename
2065 int
2066 dsl_dataset_rename(char *oldname, const char *newname,
2067     boolean_t recursive)
2068 {
2069 	dsl_dir_t *dd;
2070 	dsl_dataset_t *ds;
2071 	const char *tail;
2072 	int err;
2073 
2074 	err = dsl_dir_open(oldname, FTAG, &dd, &tail);
2075 	if (err)
2076 		return (err);
2077 	if (tail == NULL) {
2078 		int delta = strlen(newname) - strlen(oldname);
2079 
2080 		/* if we're growing, validate child size lengths */
2081 		if (delta > 0)
2082 			err = dmu_objset_find(oldname, dsl_valid_rename,
2083 			    &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
2084 
2085 		if (!err)
2086 			err = dsl_dir_rename(dd, newname);
2087 		dsl_dir_close(dd, FTAG);
2088 		return (err);
2089 	}
2090 	if (tail[0] != '@') {
2091 		/* the name ended in a nonexistant component */
2092 		dsl_dir_close(dd, FTAG);
2093 		return (ENOENT);
2094 	}
2095 
2096 	dsl_dir_close(dd, FTAG);
2097 
2098 	/* new name must be snapshot in same filesystem */
2099 	tail = strchr(newname, '@');
2100 	if (tail == NULL)
2101 		return (EINVAL);
2102 	tail++;
2103 	if (strncmp(oldname, newname, tail - newname) != 0)
2104 		return (EXDEV);
2105 
2106 	if (recursive) {
2107 		err = dsl_recursive_rename(oldname, newname);
2108 	} else {
2109 		err = dsl_dataset_open(oldname,
2110 		    DS_MODE_READONLY | DS_MODE_STANDARD, FTAG, &ds);
2111 		if (err)
2112 			return (err);
2113 
2114 		err = dsl_sync_task_do(ds->ds_dir->dd_pool,
2115 		    dsl_dataset_snapshot_rename_check,
2116 		    dsl_dataset_snapshot_rename_sync, ds, (char *)tail, 1);
2117 
2118 		dsl_dataset_close(ds, DS_MODE_STANDARD, FTAG);
2119 	}
2120 
2121 	return (err);
2122 }
2123 
2124 struct promotearg {
2125 	uint64_t used, comp, uncomp, unique;
2126 	uint64_t ds_flags, newnext_obj, snapnames_obj;
2127 };
2128 
2129 /* ARGSUSED */
2130 static int
2131 dsl_dataset_promote_check(void *arg1, void *arg2, dmu_tx_t *tx)
2132 {
2133 	dsl_dataset_t *hds = arg1;
2134 	struct promotearg *pa = arg2;
2135 	dsl_dir_t *dd = hds->ds_dir;
2136 	dsl_pool_t *dp = hds->ds_dir->dd_pool;
2137 	dsl_dir_t *odd = NULL;
2138 	dsl_dataset_t *ds = NULL;
2139 	dsl_dataset_t *origin_ds = NULL;
2140 	dsl_dataset_t *newnext_ds = NULL;
2141 	int err;
2142 	char *name = NULL;
2143 	uint64_t itor = 0;
2144 	blkptr_t bp;
2145 
2146 	bzero(pa, sizeof (*pa));
2147 
2148 	/* Check that it is a clone */
2149 	if (dd->dd_phys->dd_origin_obj == 0)
2150 		return (EINVAL);
2151 
2152 	/* Since this is so expensive, don't do the preliminary check */
2153 	if (!dmu_tx_is_syncing(tx))
2154 		return (0);
2155 
2156 	if (err = dsl_dataset_open_obj(dp, dd->dd_phys->dd_origin_obj,
2157 	    NULL, DS_MODE_EXCLUSIVE, FTAG, &origin_ds))
2158 		goto out;
2159 	odd = origin_ds->ds_dir;
2160 
2161 	{
2162 		dsl_dataset_t *phds;
2163 		if (err = dsl_dataset_open_obj(dd->dd_pool,
2164 		    odd->dd_phys->dd_head_dataset_obj,
2165 		    NULL, DS_MODE_NONE, FTAG, &phds))
2166 			goto out;
2167 		pa->ds_flags = phds->ds_phys->ds_flags;
2168 		pa->snapnames_obj = phds->ds_phys->ds_snapnames_zapobj;
2169 		dsl_dataset_close(phds, DS_MODE_NONE, FTAG);
2170 	}
2171 
2172 	if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE) {
2173 		err = EXDEV;
2174 		goto out;
2175 	}
2176 
2177 	/* find origin's new next ds */
2178 	VERIFY(0 == dsl_dataset_open_obj(dd->dd_pool, hds->ds_object,
2179 	    NULL, DS_MODE_NONE, FTAG, &newnext_ds));
2180 	while (newnext_ds->ds_phys->ds_prev_snap_obj != origin_ds->ds_object) {
2181 		dsl_dataset_t *prev;
2182 
2183 		if (err = dsl_dataset_open_obj(dd->dd_pool,
2184 		    newnext_ds->ds_phys->ds_prev_snap_obj,
2185 		    NULL, DS_MODE_NONE, FTAG, &prev))
2186 			goto out;
2187 		dsl_dataset_close(newnext_ds, DS_MODE_NONE, FTAG);
2188 		newnext_ds = prev;
2189 	}
2190 	pa->newnext_obj = newnext_ds->ds_object;
2191 
2192 	/* compute origin's new unique space */
2193 	while ((err = bplist_iterate(&newnext_ds->ds_deadlist,
2194 	    &itor, &bp)) == 0) {
2195 		if (bp.blk_birth > origin_ds->ds_phys->ds_prev_snap_txg)
2196 			pa->unique += bp_get_dasize(dd->dd_pool->dp_spa, &bp);
2197 	}
2198 	if (err != ENOENT)
2199 		goto out;
2200 
2201 	/* Walk the snapshots that we are moving */
2202 	name = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2203 	ds = origin_ds;
2204 	/* CONSTCOND */
2205 	while (TRUE) {
2206 		uint64_t val, dlused, dlcomp, dluncomp;
2207 		dsl_dataset_t *prev;
2208 
2209 		/* Check that the snapshot name does not conflict */
2210 		dsl_dataset_name(ds, name);
2211 		err = dsl_dataset_snap_lookup(dd->dd_pool->dp_meta_objset,
2212 		    hds->ds_phys->ds_flags, hds->ds_phys->ds_snapnames_zapobj,
2213 		    ds->ds_snapname, &val);
2214 		if (err != ENOENT) {
2215 			if (err == 0)
2216 				err = EEXIST;
2217 			goto out;
2218 		}
2219 
2220 		/*
2221 		 * compute space to transfer.  Each snapshot gave birth to:
2222 		 * (my used) - (prev's used) + (deadlist's used)
2223 		 */
2224 		pa->used += ds->ds_phys->ds_used_bytes;
2225 		pa->comp += ds->ds_phys->ds_compressed_bytes;
2226 		pa->uncomp += ds->ds_phys->ds_uncompressed_bytes;
2227 
2228 		/* If we reach the first snapshot, we're done. */
2229 		if (ds->ds_phys->ds_prev_snap_obj == 0)
2230 			break;
2231 
2232 		if (err = bplist_space(&ds->ds_deadlist,
2233 		    &dlused, &dlcomp, &dluncomp))
2234 			goto out;
2235 		if (err = dsl_dataset_open_obj(dd->dd_pool,
2236 		    ds->ds_phys->ds_prev_snap_obj, NULL, DS_MODE_EXCLUSIVE,
2237 		    FTAG, &prev))
2238 			goto out;
2239 		pa->used += dlused - prev->ds_phys->ds_used_bytes;
2240 		pa->comp += dlcomp - prev->ds_phys->ds_compressed_bytes;
2241 		pa->uncomp += dluncomp - prev->ds_phys->ds_uncompressed_bytes;
2242 
2243 		/*
2244 		 * We could be a clone of a clone.  If we reach our
2245 		 * parent's branch point, we're done.
2246 		 */
2247 		if (prev->ds_phys->ds_next_snap_obj != ds->ds_object) {
2248 			dsl_dataset_close(prev, DS_MODE_EXCLUSIVE, FTAG);
2249 			break;
2250 		}
2251 		if (ds != origin_ds)
2252 			dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG);
2253 		ds = prev;
2254 	}
2255 
2256 	/* Check that there is enough space here */
2257 	err = dsl_dir_transfer_possible(odd, dd, pa->used);
2258 
2259 out:
2260 	if (ds && ds != origin_ds)
2261 		dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG);
2262 	if (origin_ds)
2263 		dsl_dataset_close(origin_ds, DS_MODE_EXCLUSIVE, FTAG);
2264 	if (newnext_ds)
2265 		dsl_dataset_close(newnext_ds, DS_MODE_NONE, FTAG);
2266 	if (name)
2267 		kmem_free(name, MAXPATHLEN);
2268 	return (err);
2269 }
2270 
2271 static void
2272 dsl_dataset_promote_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
2273 {
2274 	dsl_dataset_t *hds = arg1;
2275 	struct promotearg *pa = arg2;
2276 	dsl_dir_t *dd = hds->ds_dir;
2277 	dsl_pool_t *dp = hds->ds_dir->dd_pool;
2278 	dsl_dir_t *odd = NULL;
2279 	dsl_dataset_t *ds, *origin_ds;
2280 	char *name;
2281 
2282 	ASSERT(dd->dd_phys->dd_origin_obj != 0);
2283 	ASSERT(0 == (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE));
2284 
2285 	VERIFY(0 == dsl_dataset_open_obj(dp, dd->dd_phys->dd_origin_obj,
2286 	    NULL, DS_MODE_EXCLUSIVE, FTAG, &origin_ds));
2287 	/*
2288 	 * We need to explicitly open odd, since origin_ds's dd will be
2289 	 * changing.
2290 	 */
2291 	VERIFY(0 == dsl_dir_open_obj(dp, origin_ds->ds_dir->dd_object,
2292 	    NULL, FTAG, &odd));
2293 
2294 	/* move snapshots to this dir */
2295 	name = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2296 	ds = origin_ds;
2297 	/* CONSTCOND */
2298 	while (TRUE) {
2299 		dsl_dataset_t *prev;
2300 
2301 		/* move snap name entry */
2302 		dsl_dataset_name(ds, name);
2303 		VERIFY(0 == dsl_dataset_snap_remove(dp->dp_meta_objset,
2304 		    pa->ds_flags, pa->snapnames_obj, ds->ds_snapname, tx));
2305 		VERIFY(0 == zap_add(dp->dp_meta_objset,
2306 		    hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname,
2307 		    8, 1, &ds->ds_object, tx));
2308 
2309 		/* change containing dsl_dir */
2310 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
2311 		ASSERT3U(ds->ds_phys->ds_dir_obj, ==, odd->dd_object);
2312 		ds->ds_phys->ds_dir_obj = dd->dd_object;
2313 		ASSERT3P(ds->ds_dir, ==, odd);
2314 		dsl_dir_close(ds->ds_dir, ds);
2315 		VERIFY(0 == dsl_dir_open_obj(dp, dd->dd_object,
2316 		    NULL, ds, &ds->ds_dir));
2317 
2318 		ASSERT3U(dsl_prop_numcb(ds), ==, 0);
2319 
2320 		if (ds->ds_phys->ds_prev_snap_obj == 0)
2321 			break;
2322 
2323 		VERIFY(0 == dsl_dataset_open_obj(dp,
2324 		    ds->ds_phys->ds_prev_snap_obj, NULL, DS_MODE_EXCLUSIVE,
2325 		    FTAG, &prev));
2326 
2327 		if (prev->ds_phys->ds_next_snap_obj != ds->ds_object) {
2328 			dsl_dataset_close(prev, DS_MODE_EXCLUSIVE, FTAG);
2329 			break;
2330 		}
2331 		if (ds != origin_ds)
2332 			dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG);
2333 		ds = prev;
2334 	}
2335 	if (ds != origin_ds)
2336 		dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG);
2337 
2338 	/* change origin's next snap */
2339 	dmu_buf_will_dirty(origin_ds->ds_dbuf, tx);
2340 	origin_ds->ds_phys->ds_next_snap_obj = pa->newnext_obj;
2341 
2342 	/* change origin */
2343 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
2344 	ASSERT3U(dd->dd_phys->dd_origin_obj, ==, origin_ds->ds_object);
2345 	dd->dd_phys->dd_origin_obj = odd->dd_phys->dd_origin_obj;
2346 	dmu_buf_will_dirty(odd->dd_dbuf, tx);
2347 	odd->dd_phys->dd_origin_obj = origin_ds->ds_object;
2348 
2349 	/* change space accounting */
2350 	dsl_dir_diduse_space(odd, -pa->used, -pa->comp, -pa->uncomp, tx);
2351 	dsl_dir_diduse_space(dd, pa->used, pa->comp, pa->uncomp, tx);
2352 	origin_ds->ds_phys->ds_unique_bytes = pa->unique;
2353 
2354 	/* log history record */
2355 	spa_history_internal_log(LOG_DS_PROMOTE, dd->dd_pool->dp_spa, tx,
2356 	    cr, "dataset = %llu", ds->ds_object);
2357 
2358 	dsl_dir_close(odd, FTAG);
2359 	dsl_dataset_close(origin_ds, DS_MODE_EXCLUSIVE, FTAG);
2360 	kmem_free(name, MAXPATHLEN);
2361 }
2362 
2363 int
2364 dsl_dataset_promote(const char *name)
2365 {
2366 	dsl_dataset_t *ds;
2367 	int err;
2368 	dmu_object_info_t doi;
2369 	struct promotearg pa;
2370 
2371 	err = dsl_dataset_open(name, DS_MODE_NONE, FTAG, &ds);
2372 	if (err)
2373 		return (err);
2374 
2375 	err = dmu_object_info(ds->ds_dir->dd_pool->dp_meta_objset,
2376 	    ds->ds_phys->ds_snapnames_zapobj, &doi);
2377 	if (err) {
2378 		dsl_dataset_close(ds, DS_MODE_NONE, FTAG);
2379 		return (err);
2380 	}
2381 
2382 	/*
2383 	 * Add in 128x the snapnames zapobj size, since we will be moving
2384 	 * a bunch of snapnames to the promoted ds, and dirtying their
2385 	 * bonus buffers.
2386 	 */
2387 	err = dsl_sync_task_do(ds->ds_dir->dd_pool,
2388 	    dsl_dataset_promote_check,
2389 	    dsl_dataset_promote_sync, ds, &pa, 2 + 2 * doi.doi_physical_blks);
2390 	dsl_dataset_close(ds, DS_MODE_NONE, FTAG);
2391 	return (err);
2392 }
2393 
2394 struct cloneswaparg {
2395 	dsl_dataset_t *cds; /* clone dataset */
2396 	dsl_dataset_t *ohds; /* origin's head dataset */
2397 	boolean_t force;
2398 	int64_t unused_refres_delta; /* change in unconsumed refreservation */
2399 };
2400 
2401 /* ARGSUSED */
2402 static int
2403 dsl_dataset_clone_swap_check(void *arg1, void *arg2, dmu_tx_t *tx)
2404 {
2405 	struct cloneswaparg *csa = arg1;
2406 
2407 	/* they should both be heads */
2408 	if (dsl_dataset_is_snapshot(csa->cds) ||
2409 	    dsl_dataset_is_snapshot(csa->ohds))
2410 		return (EINVAL);
2411 
2412 	/* the branch point should be just before them */
2413 	if (csa->cds->ds_prev != csa->ohds->ds_prev)
2414 		return (EINVAL);
2415 
2416 	/* cds should be the clone */
2417 	if (csa->cds->ds_prev->ds_phys->ds_next_snap_obj !=
2418 	    csa->ohds->ds_object)
2419 		return (EINVAL);
2420 
2421 	/* the clone should be a child of the origin */
2422 	if (csa->cds->ds_dir->dd_parent != csa->ohds->ds_dir)
2423 		return (EINVAL);
2424 
2425 	/* ohds shouldn't be modified unless 'force' */
2426 	if (!csa->force && dsl_dataset_modified_since_lastsnap(csa->ohds))
2427 		return (ETXTBSY);
2428 
2429 	/* adjust amount of any unconsumed refreservation */
2430 	csa->unused_refres_delta =
2431 	    (int64_t)MIN(csa->ohds->ds_reserved,
2432 	    csa->ohds->ds_phys->ds_unique_bytes) -
2433 	    (int64_t)MIN(csa->ohds->ds_reserved,
2434 	    csa->cds->ds_phys->ds_unique_bytes);
2435 
2436 	if (csa->unused_refres_delta > 0 &&
2437 	    csa->unused_refres_delta >
2438 	    dsl_dir_space_available(csa->ohds->ds_dir, NULL, 0, TRUE))
2439 		return (ENOSPC);
2440 
2441 	return (0);
2442 }
2443 
2444 /* ARGSUSED */
2445 static void
2446 dsl_dataset_clone_swap_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
2447 {
2448 	struct cloneswaparg *csa = arg1;
2449 	dsl_pool_t *dp = csa->cds->ds_dir->dd_pool;
2450 	uint64_t itor = 0;
2451 	blkptr_t bp;
2452 	uint64_t unique = 0;
2453 	int err;
2454 
2455 	ASSERT(csa->cds->ds_reserved == 0);
2456 	ASSERT(csa->cds->ds_quota == csa->ohds->ds_quota);
2457 
2458 	dmu_buf_will_dirty(csa->cds->ds_dbuf, tx);
2459 	dmu_buf_will_dirty(csa->ohds->ds_dbuf, tx);
2460 	dmu_buf_will_dirty(csa->cds->ds_prev->ds_dbuf, tx);
2461 
2462 	if (csa->cds->ds_user_ptr != NULL) {
2463 		csa->cds->ds_user_evict_func(csa->cds, csa->cds->ds_user_ptr);
2464 		csa->cds->ds_user_ptr = NULL;
2465 	}
2466 
2467 	if (csa->ohds->ds_user_ptr != NULL) {
2468 		csa->ohds->ds_user_evict_func(csa->ohds,
2469 		    csa->ohds->ds_user_ptr);
2470 		csa->ohds->ds_user_ptr = NULL;
2471 	}
2472 
2473 	/* compute unique space */
2474 	while ((err = bplist_iterate(&csa->cds->ds_deadlist,
2475 	    &itor, &bp)) == 0) {
2476 		if (bp.blk_birth > csa->cds->ds_prev->ds_phys->ds_prev_snap_txg)
2477 			unique += bp_get_dasize(dp->dp_spa, &bp);
2478 	}
2479 	VERIFY(err == ENOENT);
2480 
2481 	/* reset origin's unique bytes */
2482 	csa->cds->ds_prev->ds_phys->ds_unique_bytes = unique;
2483 
2484 	/* swap blkptrs */
2485 	{
2486 		blkptr_t tmp;
2487 		tmp = csa->ohds->ds_phys->ds_bp;
2488 		csa->ohds->ds_phys->ds_bp = csa->cds->ds_phys->ds_bp;
2489 		csa->cds->ds_phys->ds_bp = tmp;
2490 	}
2491 
2492 	/* set dd_*_bytes */
2493 	{
2494 		int64_t dused, dcomp, duncomp;
2495 		uint64_t cdl_used, cdl_comp, cdl_uncomp;
2496 		uint64_t odl_used, odl_comp, odl_uncomp;
2497 
2498 		VERIFY(0 == bplist_space(&csa->cds->ds_deadlist, &cdl_used,
2499 		    &cdl_comp, &cdl_uncomp));
2500 		VERIFY(0 == bplist_space(&csa->ohds->ds_deadlist, &odl_used,
2501 		    &odl_comp, &odl_uncomp));
2502 		dused = csa->cds->ds_phys->ds_used_bytes + cdl_used -
2503 		    (csa->ohds->ds_phys->ds_used_bytes + odl_used);
2504 		dcomp = csa->cds->ds_phys->ds_compressed_bytes + cdl_comp -
2505 		    (csa->ohds->ds_phys->ds_compressed_bytes + odl_comp);
2506 		duncomp = csa->cds->ds_phys->ds_uncompressed_bytes +
2507 		    cdl_uncomp -
2508 		    (csa->ohds->ds_phys->ds_uncompressed_bytes + odl_uncomp);
2509 
2510 		dsl_dir_diduse_space(csa->ohds->ds_dir,
2511 		    dused, dcomp, duncomp, tx);
2512 		dsl_dir_diduse_space(csa->cds->ds_dir,
2513 		    -dused, -dcomp, -duncomp, tx);
2514 	}
2515 
2516 #define	SWITCH64(x, y) \
2517 	{ \
2518 		uint64_t __tmp = (x); \
2519 		(x) = (y); \
2520 		(y) = __tmp; \
2521 	}
2522 
2523 	/* swap ds_*_bytes */
2524 	SWITCH64(csa->ohds->ds_phys->ds_used_bytes,
2525 	    csa->cds->ds_phys->ds_used_bytes);
2526 	SWITCH64(csa->ohds->ds_phys->ds_compressed_bytes,
2527 	    csa->cds->ds_phys->ds_compressed_bytes);
2528 	SWITCH64(csa->ohds->ds_phys->ds_uncompressed_bytes,
2529 	    csa->cds->ds_phys->ds_uncompressed_bytes);
2530 	SWITCH64(csa->ohds->ds_phys->ds_unique_bytes,
2531 	    csa->cds->ds_phys->ds_unique_bytes);
2532 
2533 	/* apply any parent delta for change in unconsumed refreservation */
2534 	dsl_dir_diduse_space(csa->ohds->ds_dir, csa->unused_refres_delta,
2535 	    0, 0, tx);
2536 
2537 	/* swap deadlists */
2538 	bplist_close(&csa->cds->ds_deadlist);
2539 	bplist_close(&csa->ohds->ds_deadlist);
2540 	SWITCH64(csa->ohds->ds_phys->ds_deadlist_obj,
2541 	    csa->cds->ds_phys->ds_deadlist_obj);
2542 	VERIFY(0 == bplist_open(&csa->cds->ds_deadlist, dp->dp_meta_objset,
2543 	    csa->cds->ds_phys->ds_deadlist_obj));
2544 	VERIFY(0 == bplist_open(&csa->ohds->ds_deadlist, dp->dp_meta_objset,
2545 	    csa->ohds->ds_phys->ds_deadlist_obj));
2546 }
2547 
2548 /*
2549  * Swap 'clone' with its origin head file system.
2550  */
2551 int
2552 dsl_dataset_clone_swap(dsl_dataset_t *clone, dsl_dataset_t *origin_head,
2553     boolean_t force)
2554 {
2555 	struct cloneswaparg csa;
2556 
2557 	ASSERT(clone->ds_open_refcount == DS_REF_MAX);
2558 	ASSERT(origin_head->ds_open_refcount == DS_REF_MAX);
2559 
2560 	csa.cds = clone;
2561 	csa.ohds = origin_head;
2562 	csa.force = force;
2563 	return (dsl_sync_task_do(clone->ds_dir->dd_pool,
2564 	    dsl_dataset_clone_swap_check,
2565 	    dsl_dataset_clone_swap_sync, &csa, NULL, 9));
2566 }
2567 
2568 /*
2569  * Given a pool name and a dataset object number in that pool,
2570  * return the name of that dataset.
2571  */
2572 int
2573 dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
2574 {
2575 	spa_t *spa;
2576 	dsl_pool_t *dp;
2577 	dsl_dataset_t *ds = NULL;
2578 	int error;
2579 
2580 	if ((error = spa_open(pname, &spa, FTAG)) != 0)
2581 		return (error);
2582 	dp = spa_get_dsl(spa);
2583 	rw_enter(&dp->dp_config_rwlock, RW_READER);
2584 	if ((error = dsl_dataset_open_obj(dp, obj,
2585 	    NULL, DS_MODE_NONE, FTAG, &ds)) != 0) {
2586 		rw_exit(&dp->dp_config_rwlock);
2587 		spa_close(spa, FTAG);
2588 		return (error);
2589 	}
2590 	dsl_dataset_name(ds, buf);
2591 	dsl_dataset_close(ds, DS_MODE_NONE, FTAG);
2592 	rw_exit(&dp->dp_config_rwlock);
2593 	spa_close(spa, FTAG);
2594 
2595 	return (0);
2596 }
2597 
2598 int
2599 dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota,
2600     uint64_t asize, uint64_t inflight, uint64_t *used,
2601     uint64_t *ref_rsrv)
2602 {
2603 	int error = 0;
2604 
2605 	ASSERT3S(asize, >, 0);
2606 
2607 	/*
2608 	 * *ref_rsrv is the portion of asize that will come from any
2609 	 * unconsumed refreservation space.
2610 	 */
2611 	*ref_rsrv = 0;
2612 
2613 	mutex_enter(&ds->ds_lock);
2614 	/*
2615 	 * Make a space adjustment for reserved bytes.
2616 	 */
2617 	if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) {
2618 		ASSERT3U(*used, >=,
2619 		    ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
2620 		*used -= (ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
2621 		*ref_rsrv =
2622 		    asize - MIN(asize, parent_delta(ds, asize + inflight));
2623 	}
2624 
2625 	if (!check_quota || ds->ds_quota == 0) {
2626 		mutex_exit(&ds->ds_lock);
2627 		return (0);
2628 	}
2629 	/*
2630 	 * If they are requesting more space, and our current estimate
2631 	 * is over quota, they get to try again unless the actual
2632 	 * on-disk is over quota and there are no pending changes (which
2633 	 * may free up space for us).
2634 	 */
2635 	if (ds->ds_phys->ds_used_bytes + inflight >= ds->ds_quota) {
2636 		if (inflight > 0 || ds->ds_phys->ds_used_bytes < ds->ds_quota)
2637 			error = ERESTART;
2638 		else
2639 			error = EDQUOT;
2640 	}
2641 	mutex_exit(&ds->ds_lock);
2642 
2643 	return (error);
2644 }
2645 
2646 /* ARGSUSED */
2647 static int
2648 dsl_dataset_set_quota_check(void *arg1, void *arg2, dmu_tx_t *tx)
2649 {
2650 	dsl_dataset_t *ds = arg1;
2651 	uint64_t *quotap = arg2;
2652 	uint64_t new_quota = *quotap;
2653 
2654 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_REFQUOTA)
2655 		return (ENOTSUP);
2656 
2657 	if (new_quota == 0)
2658 		return (0);
2659 
2660 	if (new_quota < ds->ds_phys->ds_used_bytes ||
2661 	    new_quota < ds->ds_reserved)
2662 		return (ENOSPC);
2663 
2664 	return (0);
2665 }
2666 
2667 /* ARGSUSED */
2668 void
2669 dsl_dataset_set_quota_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
2670 {
2671 	dsl_dataset_t *ds = arg1;
2672 	uint64_t *quotap = arg2;
2673 	uint64_t new_quota = *quotap;
2674 
2675 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
2676 
2677 	mutex_enter(&ds->ds_lock);
2678 	ds->ds_quota = new_quota;
2679 	mutex_exit(&ds->ds_lock);
2680 
2681 	dsl_prop_set_uint64_sync(ds->ds_dir, "refquota", new_quota, cr, tx);
2682 
2683 	spa_history_internal_log(LOG_DS_REFQUOTA, ds->ds_dir->dd_pool->dp_spa,
2684 	    tx, cr, "%lld dataset = %llu ",
2685 	    (longlong_t)new_quota, ds->ds_dir->dd_phys->dd_head_dataset_obj);
2686 }
2687 
2688 int
2689 dsl_dataset_set_quota(const char *dsname, uint64_t quota)
2690 {
2691 	dsl_dataset_t *ds;
2692 	int err;
2693 
2694 	err = dsl_dataset_open(dsname, DS_MODE_STANDARD, FTAG, &ds);
2695 	if (err)
2696 		return (err);
2697 
2698 	if (quota != ds->ds_quota) {
2699 		/*
2700 		 * If someone removes a file, then tries to set the quota, we
2701 		 * want to make sure the file freeing takes effect.
2702 		 */
2703 		txg_wait_open(ds->ds_dir->dd_pool, 0);
2704 
2705 		err = dsl_sync_task_do(ds->ds_dir->dd_pool,
2706 		    dsl_dataset_set_quota_check, dsl_dataset_set_quota_sync,
2707 		    ds, &quota, 0);
2708 	}
2709 	dsl_dataset_close(ds, DS_MODE_STANDARD, FTAG);
2710 	return (err);
2711 }
2712 
2713 static int
2714 dsl_dataset_set_reservation_check(void *arg1, void *arg2, dmu_tx_t *tx)
2715 {
2716 	dsl_dataset_t *ds = arg1;
2717 	uint64_t *reservationp = arg2;
2718 	uint64_t new_reservation = *reservationp;
2719 	int64_t delta;
2720 	uint64_t unique;
2721 
2722 	if (new_reservation > INT64_MAX)
2723 		return (EOVERFLOW);
2724 
2725 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) <
2726 	    SPA_VERSION_REFRESERVATION)
2727 		return (ENOTSUP);
2728 
2729 	if (dsl_dataset_is_snapshot(ds))
2730 		return (EINVAL);
2731 
2732 	/*
2733 	 * If we are doing the preliminary check in open context, the
2734 	 * space estimates may be inaccurate.
2735 	 */
2736 	if (!dmu_tx_is_syncing(tx))
2737 		return (0);
2738 
2739 	mutex_enter(&ds->ds_lock);
2740 	unique = dsl_dataset_unique(ds);
2741 	delta = MAX(unique, new_reservation) - MAX(unique, ds->ds_reserved);
2742 	mutex_exit(&ds->ds_lock);
2743 
2744 	if (delta > 0 &&
2745 	    delta > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
2746 		return (ENOSPC);
2747 	if (delta > 0 && ds->ds_quota > 0 &&
2748 	    new_reservation > ds->ds_quota)
2749 		return (ENOSPC);
2750 
2751 	return (0);
2752 }
2753 
2754 /* ARGSUSED */
2755 static void
2756 dsl_dataset_set_reservation_sync(void *arg1, void *arg2, cred_t *cr,
2757     dmu_tx_t *tx)
2758 {
2759 	dsl_dataset_t *ds = arg1;
2760 	uint64_t *reservationp = arg2;
2761 	uint64_t new_reservation = *reservationp;
2762 	uint64_t unique;
2763 	int64_t delta;
2764 
2765 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
2766 
2767 	mutex_enter(&ds->ds_lock);
2768 	unique = dsl_dataset_unique(ds);
2769 	delta = MAX(0, (int64_t)(new_reservation - unique)) -
2770 	    MAX(0, (int64_t)(ds->ds_reserved - unique));
2771 	ds->ds_reserved = new_reservation;
2772 	mutex_exit(&ds->ds_lock);
2773 
2774 	dsl_prop_set_uint64_sync(ds->ds_dir, "refreservation",
2775 	    new_reservation, cr, tx);
2776 
2777 	dsl_dir_diduse_space(ds->ds_dir, delta, 0, 0, tx);
2778 
2779 	spa_history_internal_log(LOG_DS_REFRESERV,
2780 	    ds->ds_dir->dd_pool->dp_spa, tx, cr, "%lld dataset = %llu",
2781 	    (longlong_t)new_reservation,
2782 	    ds->ds_dir->dd_phys->dd_head_dataset_obj);
2783 }
2784 
2785 int
2786 dsl_dataset_set_reservation(const char *dsname, uint64_t reservation)
2787 {
2788 	dsl_dataset_t *ds;
2789 	int err;
2790 
2791 	err = dsl_dataset_open(dsname, DS_MODE_STANDARD, FTAG, &ds);
2792 	if (err)
2793 		return (err);
2794 
2795 	err = dsl_sync_task_do(ds->ds_dir->dd_pool,
2796 	    dsl_dataset_set_reservation_check,
2797 	    dsl_dataset_set_reservation_sync, ds, &reservation, 0);
2798 	dsl_dataset_close(ds, DS_MODE_STANDARD, FTAG);
2799 	return (err);
2800 }
2801