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