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