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