xref: /titanic_51/usr/src/uts/common/fs/zfs/dsl_dataset.c (revision fdea908e419c50bb9207dfd6dee8556aa6e39cf3)
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 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/dmu_objset.h>
29 #include <sys/dsl_dataset.h>
30 #include <sys/dsl_dir.h>
31 #include <sys/dsl_prop.h>
32 #include <sys/dsl_synctask.h>
33 #include <sys/dmu_traverse.h>
34 #include <sys/dmu_tx.h>
35 #include <sys/arc.h>
36 #include <sys/zio.h>
37 #include <sys/zap.h>
38 #include <sys/unique.h>
39 #include <sys/zfs_context.h>
40 #include <sys/zfs_ioctl.h>
41 #include <sys/spa.h>
42 #include <sys/sunddi.h>
43 
44 static dsl_checkfunc_t dsl_dataset_destroy_begin_check;
45 static dsl_syncfunc_t dsl_dataset_destroy_begin_sync;
46 static dsl_checkfunc_t dsl_dataset_rollback_check;
47 static dsl_syncfunc_t dsl_dataset_rollback_sync;
48 static dsl_checkfunc_t dsl_dataset_destroy_check;
49 static dsl_syncfunc_t dsl_dataset_destroy_sync;
50 
51 #define	DS_REF_MAX	(1ULL << 62)
52 
53 #define	DSL_DEADLIST_BLOCKSIZE	SPA_MAXBLOCKSIZE
54 
55 /*
56  * We use weighted reference counts to express the various forms of exclusion
57  * between different open modes.  A STANDARD open is 1 point, an EXCLUSIVE open
58  * is DS_REF_MAX, and a PRIMARY open is little more than half of an EXCLUSIVE.
59  * This makes the exclusion logic simple: the total refcnt for all opens cannot
60  * exceed DS_REF_MAX.  For example, EXCLUSIVE opens are exclusive because their
61  * weight (DS_REF_MAX) consumes the entire refcnt space.  PRIMARY opens consume
62  * just over half of the refcnt space, so there can't be more than one, but it
63  * can peacefully coexist with any number of STANDARD opens.
64  */
65 static uint64_t ds_refcnt_weight[DS_MODE_LEVELS] = {
66 	0,			/* DS_MODE_NONE - invalid		*/
67 	1,			/* DS_MODE_STANDARD - unlimited number	*/
68 	(DS_REF_MAX >> 1) + 1,	/* DS_MODE_PRIMARY - only one of these	*/
69 	DS_REF_MAX		/* DS_MODE_EXCLUSIVE - no other opens	*/
70 };
71 
72 
73 void
74 dsl_dataset_block_born(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx)
75 {
76 	int used = bp_get_dasize(tx->tx_pool->dp_spa, bp);
77 	int compressed = BP_GET_PSIZE(bp);
78 	int uncompressed = BP_GET_UCSIZE(bp);
79 
80 	dprintf_bp(bp, "born, ds=%p\n", ds);
81 
82 	ASSERT(dmu_tx_is_syncing(tx));
83 	/* It could have been compressed away to nothing */
84 	if (BP_IS_HOLE(bp))
85 		return;
86 	ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE);
87 	ASSERT3U(BP_GET_TYPE(bp), <, DMU_OT_NUMTYPES);
88 	if (ds == NULL) {
89 		/*
90 		 * Account for the meta-objset space in its placeholder
91 		 * dsl_dir.
92 		 */
93 		ASSERT3U(compressed, ==, uncompressed); /* it's all metadata */
94 		dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir,
95 		    used, compressed, uncompressed, tx);
96 		dsl_dir_dirty(tx->tx_pool->dp_mos_dir, tx);
97 		return;
98 	}
99 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
100 	mutex_enter(&ds->ds_lock);
101 	ds->ds_phys->ds_used_bytes += used;
102 	ds->ds_phys->ds_compressed_bytes += compressed;
103 	ds->ds_phys->ds_uncompressed_bytes += uncompressed;
104 	ds->ds_phys->ds_unique_bytes += used;
105 	mutex_exit(&ds->ds_lock);
106 	dsl_dir_diduse_space(ds->ds_dir,
107 	    used, compressed, uncompressed, tx);
108 }
109 
110 void
111 dsl_dataset_block_kill(dsl_dataset_t *ds, blkptr_t *bp, zio_t *pio,
112     dmu_tx_t *tx)
113 {
114 	int used = bp_get_dasize(tx->tx_pool->dp_spa, bp);
115 	int compressed = BP_GET_PSIZE(bp);
116 	int uncompressed = BP_GET_UCSIZE(bp);
117 
118 	ASSERT(dmu_tx_is_syncing(tx));
119 	/* No block pointer => nothing to free */
120 	if (BP_IS_HOLE(bp))
121 		return;
122 
123 	ASSERT(used > 0);
124 	if (ds == NULL) {
125 		int err;
126 		/*
127 		 * Account for the meta-objset space in its placeholder
128 		 * dataset.
129 		 */
130 		err = arc_free(pio, tx->tx_pool->dp_spa,
131 		    tx->tx_txg, bp, NULL, NULL, pio ? ARC_NOWAIT: ARC_WAIT);
132 		ASSERT(err == 0);
133 
134 		dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir,
135 		    -used, -compressed, -uncompressed, tx);
136 		dsl_dir_dirty(tx->tx_pool->dp_mos_dir, tx);
137 		return;
138 	}
139 	ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool);
140 
141 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
142 
143 	if (bp->blk_birth > ds->ds_phys->ds_prev_snap_txg) {
144 		int err;
145 
146 		dprintf_bp(bp, "freeing: %s", "");
147 		err = arc_free(pio, tx->tx_pool->dp_spa,
148 		    tx->tx_txg, bp, NULL, NULL, pio ? ARC_NOWAIT: ARC_WAIT);
149 		ASSERT(err == 0);
150 
151 		mutex_enter(&ds->ds_lock);
152 		/* XXX unique_bytes is not accurate for head datasets */
153 		/* ASSERT3U(ds->ds_phys->ds_unique_bytes, >=, used); */
154 		ds->ds_phys->ds_unique_bytes -= used;
155 		mutex_exit(&ds->ds_lock);
156 		dsl_dir_diduse_space(ds->ds_dir,
157 		    -used, -compressed, -uncompressed, tx);
158 	} else {
159 		dprintf_bp(bp, "putting on dead list: %s", "");
160 		VERIFY(0 == bplist_enqueue(&ds->ds_deadlist, bp, tx));
161 		/* if (bp->blk_birth > prev prev snap txg) prev unique += bs */
162 		if (ds->ds_phys->ds_prev_snap_obj != 0) {
163 			ASSERT3U(ds->ds_prev->ds_object, ==,
164 			    ds->ds_phys->ds_prev_snap_obj);
165 			ASSERT(ds->ds_prev->ds_phys->ds_num_children > 0);
166 			if (ds->ds_prev->ds_phys->ds_next_snap_obj ==
167 			    ds->ds_object && bp->blk_birth >
168 			    ds->ds_prev->ds_phys->ds_prev_snap_txg) {
169 				dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
170 				mutex_enter(&ds->ds_prev->ds_lock);
171 				ds->ds_prev->ds_phys->ds_unique_bytes +=
172 				    used;
173 				mutex_exit(&ds->ds_prev->ds_lock);
174 			}
175 		}
176 	}
177 	mutex_enter(&ds->ds_lock);
178 	ASSERT3U(ds->ds_phys->ds_used_bytes, >=, used);
179 	ds->ds_phys->ds_used_bytes -= used;
180 	ASSERT3U(ds->ds_phys->ds_compressed_bytes, >=, compressed);
181 	ds->ds_phys->ds_compressed_bytes -= compressed;
182 	ASSERT3U(ds->ds_phys->ds_uncompressed_bytes, >=, uncompressed);
183 	ds->ds_phys->ds_uncompressed_bytes -= uncompressed;
184 	mutex_exit(&ds->ds_lock);
185 }
186 
187 uint64_t
188 dsl_dataset_prev_snap_txg(dsl_dataset_t *ds)
189 {
190 	uint64_t trysnap = 0;
191 
192 	if (ds == NULL)
193 		return (0);
194 	/*
195 	 * The snapshot creation could fail, but that would cause an
196 	 * incorrect FALSE return, which would only result in an
197 	 * overestimation of the amount of space that an operation would
198 	 * consume, which is OK.
199 	 *
200 	 * There's also a small window where we could miss a pending
201 	 * snapshot, because we could set the sync task in the quiescing
202 	 * phase.  So this should only be used as a guess.
203 	 */
204 	if (ds->ds_trysnap_txg >
205 	    spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa))
206 		trysnap = ds->ds_trysnap_txg;
207 	return (MAX(ds->ds_phys->ds_prev_snap_txg, trysnap));
208 }
209 
210 int
211 dsl_dataset_block_freeable(dsl_dataset_t *ds, uint64_t blk_birth)
212 {
213 	return (blk_birth > dsl_dataset_prev_snap_txg(ds));
214 }
215 
216 /* ARGSUSED */
217 static void
218 dsl_dataset_evict(dmu_buf_t *db, void *dsv)
219 {
220 	dsl_dataset_t *ds = dsv;
221 
222 	/* open_refcount == DS_REF_MAX when deleting */
223 	ASSERT(ds->ds_open_refcount == 0 ||
224 	    ds->ds_open_refcount == DS_REF_MAX);
225 
226 	dprintf_ds(ds, "evicting %s\n", "");
227 
228 	unique_remove(ds->ds_fsid_guid);
229 
230 	if (ds->ds_user_ptr != NULL)
231 		ds->ds_user_evict_func(ds, ds->ds_user_ptr);
232 
233 	if (ds->ds_prev) {
234 		dsl_dataset_close(ds->ds_prev, DS_MODE_NONE, ds);
235 		ds->ds_prev = NULL;
236 	}
237 
238 	bplist_close(&ds->ds_deadlist);
239 	dsl_dir_close(ds->ds_dir, ds);
240 
241 	ASSERT(!list_link_active(&ds->ds_synced_link));
242 
243 	mutex_destroy(&ds->ds_lock);
244 	mutex_destroy(&ds->ds_opening_lock);
245 	mutex_destroy(&ds->ds_deadlist.bpl_lock);
246 
247 	kmem_free(ds, sizeof (dsl_dataset_t));
248 }
249 
250 static int
251 dsl_dataset_get_snapname(dsl_dataset_t *ds)
252 {
253 	dsl_dataset_phys_t *headphys;
254 	int err;
255 	dmu_buf_t *headdbuf;
256 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
257 	objset_t *mos = dp->dp_meta_objset;
258 
259 	if (ds->ds_snapname[0])
260 		return (0);
261 	if (ds->ds_phys->ds_next_snap_obj == 0)
262 		return (0);
263 
264 	err = dmu_bonus_hold(mos, ds->ds_dir->dd_phys->dd_head_dataset_obj,
265 	    FTAG, &headdbuf);
266 	if (err)
267 		return (err);
268 	headphys = headdbuf->db_data;
269 	err = zap_value_search(dp->dp_meta_objset,
270 	    headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname);
271 	dmu_buf_rele(headdbuf, FTAG);
272 	return (err);
273 }
274 
275 int
276 dsl_dataset_open_obj(dsl_pool_t *dp, uint64_t dsobj, const char *snapname,
277     int mode, void *tag, dsl_dataset_t **dsp)
278 {
279 	uint64_t weight = ds_refcnt_weight[DS_MODE_LEVEL(mode)];
280 	objset_t *mos = dp->dp_meta_objset;
281 	dmu_buf_t *dbuf;
282 	dsl_dataset_t *ds;
283 	int err;
284 
285 	ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) ||
286 	    dsl_pool_sync_context(dp));
287 
288 	err = dmu_bonus_hold(mos, dsobj, tag, &dbuf);
289 	if (err)
290 		return (err);
291 	ds = dmu_buf_get_user(dbuf);
292 	if (ds == NULL) {
293 		dsl_dataset_t *winner;
294 
295 		ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP);
296 		ds->ds_dbuf = dbuf;
297 		ds->ds_object = dsobj;
298 		ds->ds_phys = dbuf->db_data;
299 
300 		mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL);
301 		mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL);
302 		mutex_init(&ds->ds_deadlist.bpl_lock, NULL, MUTEX_DEFAULT,
303 		    NULL);
304 
305 		err = bplist_open(&ds->ds_deadlist,
306 		    mos, ds->ds_phys->ds_deadlist_obj);
307 		if (err == 0) {
308 			err = dsl_dir_open_obj(dp,
309 			    ds->ds_phys->ds_dir_obj, NULL, ds, &ds->ds_dir);
310 		}
311 		if (err) {
312 			/*
313 			 * we don't really need to close the blist if we
314 			 * just opened it.
315 			 */
316 			mutex_destroy(&ds->ds_lock);
317 			mutex_destroy(&ds->ds_opening_lock);
318 			mutex_destroy(&ds->ds_deadlist.bpl_lock);
319 			kmem_free(ds, sizeof (dsl_dataset_t));
320 			dmu_buf_rele(dbuf, tag);
321 			return (err);
322 		}
323 
324 		if (ds->ds_dir->dd_phys->dd_head_dataset_obj == dsobj) {
325 			ds->ds_snapname[0] = '\0';
326 			if (ds->ds_phys->ds_prev_snap_obj) {
327 				err = dsl_dataset_open_obj(dp,
328 				    ds->ds_phys->ds_prev_snap_obj, NULL,
329 				    DS_MODE_NONE, ds, &ds->ds_prev);
330 			}
331 		} else {
332 			if (snapname) {
333 #ifdef ZFS_DEBUG
334 				dsl_dataset_phys_t *headphys;
335 				dmu_buf_t *headdbuf;
336 				err = dmu_bonus_hold(mos,
337 				    ds->ds_dir->dd_phys->dd_head_dataset_obj,
338 				    FTAG, &headdbuf);
339 				if (err == 0) {
340 					headphys = headdbuf->db_data;
341 					uint64_t foundobj;
342 					err = zap_lookup(dp->dp_meta_objset,
343 					    headphys->ds_snapnames_zapobj,
344 					    snapname, sizeof (foundobj), 1,
345 					    &foundobj);
346 					ASSERT3U(foundobj, ==, dsobj);
347 					dmu_buf_rele(headdbuf, FTAG);
348 				}
349 #endif
350 				(void) strcat(ds->ds_snapname, snapname);
351 			} else if (zfs_flags & ZFS_DEBUG_SNAPNAMES) {
352 				err = dsl_dataset_get_snapname(ds);
353 			}
354 		}
355 
356 		if (err == 0) {
357 			winner = dmu_buf_set_user_ie(dbuf, ds, &ds->ds_phys,
358 			    dsl_dataset_evict);
359 		}
360 		if (err || winner) {
361 			bplist_close(&ds->ds_deadlist);
362 			if (ds->ds_prev) {
363 				dsl_dataset_close(ds->ds_prev,
364 				    DS_MODE_NONE, ds);
365 			}
366 			dsl_dir_close(ds->ds_dir, ds);
367 			mutex_destroy(&ds->ds_lock);
368 			mutex_destroy(&ds->ds_opening_lock);
369 			mutex_destroy(&ds->ds_deadlist.bpl_lock);
370 			kmem_free(ds, sizeof (dsl_dataset_t));
371 			if (err) {
372 				dmu_buf_rele(dbuf, tag);
373 				return (err);
374 			}
375 			ds = winner;
376 		} else {
377 			ds->ds_fsid_guid =
378 			    unique_insert(ds->ds_phys->ds_fsid_guid);
379 		}
380 	}
381 	ASSERT3P(ds->ds_dbuf, ==, dbuf);
382 	ASSERT3P(ds->ds_phys, ==, dbuf->db_data);
383 
384 	mutex_enter(&ds->ds_lock);
385 	if ((DS_MODE_LEVEL(mode) == DS_MODE_PRIMARY &&
386 	    (ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT) &&
387 	    !DS_MODE_IS_INCONSISTENT(mode)) ||
388 	    (ds->ds_open_refcount + weight > DS_REF_MAX)) {
389 		mutex_exit(&ds->ds_lock);
390 		dsl_dataset_close(ds, DS_MODE_NONE, tag);
391 		return (EBUSY);
392 	}
393 	ds->ds_open_refcount += weight;
394 	mutex_exit(&ds->ds_lock);
395 
396 	*dsp = ds;
397 	return (0);
398 }
399 
400 int
401 dsl_dataset_open_spa(spa_t *spa, const char *name, int mode,
402     void *tag, dsl_dataset_t **dsp)
403 {
404 	dsl_dir_t *dd;
405 	dsl_pool_t *dp;
406 	const char *tail;
407 	uint64_t obj;
408 	dsl_dataset_t *ds = NULL;
409 	int err = 0;
410 
411 	err = dsl_dir_open_spa(spa, name, FTAG, &dd, &tail);
412 	if (err)
413 		return (err);
414 
415 	dp = dd->dd_pool;
416 	obj = dd->dd_phys->dd_head_dataset_obj;
417 	rw_enter(&dp->dp_config_rwlock, RW_READER);
418 	if (obj == 0) {
419 		/* A dataset with no associated objset */
420 		err = ENOENT;
421 		goto out;
422 	}
423 
424 	if (tail != NULL) {
425 		objset_t *mos = dp->dp_meta_objset;
426 
427 		err = dsl_dataset_open_obj(dp, obj, NULL,
428 		    DS_MODE_NONE, tag, &ds);
429 		if (err)
430 			goto out;
431 		obj = ds->ds_phys->ds_snapnames_zapobj;
432 		dsl_dataset_close(ds, DS_MODE_NONE, tag);
433 		ds = NULL;
434 
435 		if (tail[0] != '@') {
436 			err = ENOENT;
437 			goto out;
438 		}
439 		tail++;
440 
441 		/* Look for a snapshot */
442 		if (!DS_MODE_IS_READONLY(mode)) {
443 			err = EROFS;
444 			goto out;
445 		}
446 		dprintf("looking for snapshot '%s'\n", tail);
447 		err = zap_lookup(mos, obj, tail, 8, 1, &obj);
448 		if (err)
449 			goto out;
450 	}
451 	err = dsl_dataset_open_obj(dp, obj, tail, mode, tag, &ds);
452 
453 out:
454 	rw_exit(&dp->dp_config_rwlock);
455 	dsl_dir_close(dd, FTAG);
456 
457 	ASSERT3U((err == 0), ==, (ds != NULL));
458 	/* ASSERT(ds == NULL || strcmp(name, ds->ds_name) == 0); */
459 
460 	*dsp = ds;
461 	return (err);
462 }
463 
464 int
465 dsl_dataset_open(const char *name, int mode, void *tag, dsl_dataset_t **dsp)
466 {
467 	return (dsl_dataset_open_spa(NULL, name, mode, tag, dsp));
468 }
469 
470 void
471 dsl_dataset_name(dsl_dataset_t *ds, char *name)
472 {
473 	if (ds == NULL) {
474 		(void) strcpy(name, "mos");
475 	} else {
476 		dsl_dir_name(ds->ds_dir, name);
477 		VERIFY(0 == dsl_dataset_get_snapname(ds));
478 		if (ds->ds_snapname[0]) {
479 			(void) strcat(name, "@");
480 			if (!MUTEX_HELD(&ds->ds_lock)) {
481 				/*
482 				 * We use a "recursive" mutex so that we
483 				 * can call dprintf_ds() with ds_lock held.
484 				 */
485 				mutex_enter(&ds->ds_lock);
486 				(void) strcat(name, ds->ds_snapname);
487 				mutex_exit(&ds->ds_lock);
488 			} else {
489 				(void) strcat(name, ds->ds_snapname);
490 			}
491 		}
492 	}
493 }
494 
495 static int
496 dsl_dataset_namelen(dsl_dataset_t *ds)
497 {
498 	int result;
499 
500 	if (ds == NULL) {
501 		result = 3;	/* "mos" */
502 	} else {
503 		result = dsl_dir_namelen(ds->ds_dir);
504 		VERIFY(0 == dsl_dataset_get_snapname(ds));
505 		if (ds->ds_snapname[0]) {
506 			++result;	/* adding one for the @-sign */
507 			if (!MUTEX_HELD(&ds->ds_lock)) {
508 				/* see dsl_datset_name */
509 				mutex_enter(&ds->ds_lock);
510 				result += strlen(ds->ds_snapname);
511 				mutex_exit(&ds->ds_lock);
512 			} else {
513 				result += strlen(ds->ds_snapname);
514 			}
515 		}
516 	}
517 
518 	return (result);
519 }
520 
521 void
522 dsl_dataset_close(dsl_dataset_t *ds, int mode, void *tag)
523 {
524 	uint64_t weight = ds_refcnt_weight[DS_MODE_LEVEL(mode)];
525 	mutex_enter(&ds->ds_lock);
526 	ASSERT3U(ds->ds_open_refcount, >=, weight);
527 	ds->ds_open_refcount -= weight;
528 	dprintf_ds(ds, "closing mode %u refcount now 0x%llx\n",
529 	    mode, ds->ds_open_refcount);
530 	mutex_exit(&ds->ds_lock);
531 
532 	dmu_buf_rele(ds->ds_dbuf, tag);
533 }
534 
535 void
536 dsl_dataset_create_root(dsl_pool_t *dp, uint64_t *ddobjp, dmu_tx_t *tx)
537 {
538 	objset_t *mos = dp->dp_meta_objset;
539 	dmu_buf_t *dbuf;
540 	dsl_dataset_phys_t *dsphys;
541 	dsl_dataset_t *ds;
542 	uint64_t dsobj;
543 	dsl_dir_t *dd;
544 
545 	dsl_dir_create_root(mos, ddobjp, tx);
546 	VERIFY(0 == dsl_dir_open_obj(dp, *ddobjp, NULL, FTAG, &dd));
547 
548 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
549 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
550 	VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
551 	dmu_buf_will_dirty(dbuf, tx);
552 	dsphys = dbuf->db_data;
553 	dsphys->ds_dir_obj = dd->dd_object;
554 	dsphys->ds_fsid_guid = unique_create();
555 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
556 	    sizeof (dsphys->ds_guid));
557 	dsphys->ds_snapnames_zapobj =
558 	    zap_create(mos, DMU_OT_DSL_DS_SNAP_MAP, DMU_OT_NONE, 0, tx);
559 	dsphys->ds_creation_time = gethrestime_sec();
560 	dsphys->ds_creation_txg = tx->tx_txg;
561 	dsphys->ds_deadlist_obj =
562 	    bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx);
563 	dmu_buf_rele(dbuf, FTAG);
564 
565 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
566 	dd->dd_phys->dd_head_dataset_obj = dsobj;
567 	dsl_dir_close(dd, FTAG);
568 
569 	VERIFY(0 ==
570 	    dsl_dataset_open_obj(dp, dsobj, NULL, DS_MODE_NONE, FTAG, &ds));
571 	(void) dmu_objset_create_impl(dp->dp_spa, ds,
572 	    &ds->ds_phys->ds_bp, DMU_OST_ZFS, tx);
573 	dsl_dataset_close(ds, DS_MODE_NONE, FTAG);
574 }
575 
576 uint64_t
577 dsl_dataset_create_sync(dsl_dir_t *pdd,
578     const char *lastname, dsl_dataset_t *clone_parent, dmu_tx_t *tx)
579 {
580 	dsl_pool_t *dp = pdd->dd_pool;
581 	dmu_buf_t *dbuf;
582 	dsl_dataset_phys_t *dsphys;
583 	uint64_t dsobj, ddobj;
584 	objset_t *mos = dp->dp_meta_objset;
585 	dsl_dir_t *dd;
586 
587 	ASSERT(clone_parent == NULL || clone_parent->ds_dir->dd_pool == dp);
588 	ASSERT(clone_parent == NULL ||
589 	    clone_parent->ds_phys->ds_num_children > 0);
590 	ASSERT(lastname[0] != '@');
591 	ASSERT(dmu_tx_is_syncing(tx));
592 
593 	ddobj = dsl_dir_create_sync(pdd, lastname, tx);
594 	VERIFY(0 == dsl_dir_open_obj(dp, ddobj, lastname, FTAG, &dd));
595 
596 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
597 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
598 	VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
599 	dmu_buf_will_dirty(dbuf, tx);
600 	dsphys = dbuf->db_data;
601 	dsphys->ds_dir_obj = dd->dd_object;
602 	dsphys->ds_fsid_guid = unique_create();
603 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
604 	    sizeof (dsphys->ds_guid));
605 	dsphys->ds_snapnames_zapobj =
606 	    zap_create(mos, DMU_OT_DSL_DS_SNAP_MAP, DMU_OT_NONE, 0, tx);
607 	dsphys->ds_creation_time = gethrestime_sec();
608 	dsphys->ds_creation_txg = tx->tx_txg;
609 	dsphys->ds_deadlist_obj =
610 	    bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx);
611 	if (clone_parent) {
612 		dsphys->ds_prev_snap_obj = clone_parent->ds_object;
613 		dsphys->ds_prev_snap_txg =
614 		    clone_parent->ds_phys->ds_creation_txg;
615 		dsphys->ds_used_bytes =
616 		    clone_parent->ds_phys->ds_used_bytes;
617 		dsphys->ds_compressed_bytes =
618 		    clone_parent->ds_phys->ds_compressed_bytes;
619 		dsphys->ds_uncompressed_bytes =
620 		    clone_parent->ds_phys->ds_uncompressed_bytes;
621 		dsphys->ds_bp = clone_parent->ds_phys->ds_bp;
622 
623 		dmu_buf_will_dirty(clone_parent->ds_dbuf, tx);
624 		clone_parent->ds_phys->ds_num_children++;
625 
626 		dmu_buf_will_dirty(dd->dd_dbuf, tx);
627 		dd->dd_phys->dd_clone_parent_obj = clone_parent->ds_object;
628 	}
629 	dmu_buf_rele(dbuf, FTAG);
630 
631 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
632 	dd->dd_phys->dd_head_dataset_obj = dsobj;
633 	dsl_dir_close(dd, FTAG);
634 
635 	return (dsobj);
636 }
637 
638 struct destroyarg {
639 	dsl_sync_task_group_t *dstg;
640 	char *snapname;
641 	char *failed;
642 };
643 
644 static int
645 dsl_snapshot_destroy_one(char *name, void *arg)
646 {
647 	struct destroyarg *da = arg;
648 	dsl_dataset_t *ds;
649 	char *cp;
650 	int err;
651 
652 	(void) strcat(name, "@");
653 	(void) strcat(name, da->snapname);
654 	err = dsl_dataset_open(name,
655 	    DS_MODE_EXCLUSIVE | DS_MODE_READONLY | DS_MODE_INCONSISTENT,
656 	    da->dstg, &ds);
657 	cp = strchr(name, '@');
658 	*cp = '\0';
659 	if (err == ENOENT)
660 		return (0);
661 	if (err) {
662 		(void) strcpy(da->failed, name);
663 		return (err);
664 	}
665 
666 	dsl_sync_task_create(da->dstg, dsl_dataset_destroy_check,
667 	    dsl_dataset_destroy_sync, ds, da->dstg, 0);
668 	return (0);
669 }
670 
671 /*
672  * Destroy 'snapname' in all descendants of 'fsname'.
673  */
674 #pragma weak dmu_snapshots_destroy = dsl_snapshots_destroy
675 int
676 dsl_snapshots_destroy(char *fsname, char *snapname)
677 {
678 	int err;
679 	struct destroyarg da;
680 	dsl_sync_task_t *dst;
681 	spa_t *spa;
682 
683 	err = spa_open(fsname, &spa, FTAG);
684 	if (err)
685 		return (err);
686 	da.dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
687 	da.snapname = snapname;
688 	da.failed = fsname;
689 
690 	err = dmu_objset_find(fsname,
691 	    dsl_snapshot_destroy_one, &da, DS_FIND_CHILDREN);
692 
693 	if (err == 0)
694 		err = dsl_sync_task_group_wait(da.dstg);
695 
696 	for (dst = list_head(&da.dstg->dstg_tasks); dst;
697 	    dst = list_next(&da.dstg->dstg_tasks, dst)) {
698 		dsl_dataset_t *ds = dst->dst_arg1;
699 		if (dst->dst_err) {
700 			dsl_dataset_name(ds, fsname);
701 			*strchr(fsname, '@') = '\0';
702 		}
703 		/*
704 		 * If it was successful, destroy_sync would have
705 		 * closed the ds
706 		 */
707 		if (err)
708 			dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, da.dstg);
709 	}
710 
711 	dsl_sync_task_group_destroy(da.dstg);
712 	spa_close(spa, FTAG);
713 	return (err);
714 }
715 
716 int
717 dsl_dataset_destroy(const char *name)
718 {
719 	int err;
720 	dsl_sync_task_group_t *dstg;
721 	objset_t *os;
722 	dsl_dataset_t *ds;
723 	dsl_dir_t *dd;
724 	uint64_t obj;
725 
726 	if (strchr(name, '@')) {
727 		/* Destroying a snapshot is simpler */
728 		err = dsl_dataset_open(name,
729 		    DS_MODE_EXCLUSIVE | DS_MODE_READONLY | DS_MODE_INCONSISTENT,
730 		    FTAG, &ds);
731 		if (err)
732 			return (err);
733 		err = dsl_sync_task_do(ds->ds_dir->dd_pool,
734 		    dsl_dataset_destroy_check, dsl_dataset_destroy_sync,
735 		    ds, FTAG, 0);
736 		if (err)
737 			dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG);
738 		return (err);
739 	}
740 
741 	err = dmu_objset_open(name, DMU_OST_ANY,
742 	    DS_MODE_EXCLUSIVE | DS_MODE_INCONSISTENT, &os);
743 	if (err)
744 		return (err);
745 	ds = os->os->os_dsl_dataset;
746 	dd = ds->ds_dir;
747 
748 	/*
749 	 * Check for errors and mark this ds as inconsistent, in
750 	 * case we crash while freeing the objects.
751 	 */
752 	err = dsl_sync_task_do(dd->dd_pool, dsl_dataset_destroy_begin_check,
753 	    dsl_dataset_destroy_begin_sync, ds, NULL, 0);
754 	if (err) {
755 		dmu_objset_close(os);
756 		return (err);
757 	}
758 
759 	/*
760 	 * remove the objects in open context, so that we won't
761 	 * have too much to do in syncing context.
762 	 */
763 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE,
764 	    ds->ds_phys->ds_prev_snap_txg)) {
765 		dmu_tx_t *tx = dmu_tx_create(os);
766 		dmu_tx_hold_free(tx, obj, 0, DMU_OBJECT_END);
767 		dmu_tx_hold_bonus(tx, obj);
768 		err = dmu_tx_assign(tx, TXG_WAIT);
769 		if (err) {
770 			/*
771 			 * Perhaps there is not enough disk
772 			 * space.  Just deal with it from
773 			 * dsl_dataset_destroy_sync().
774 			 */
775 			dmu_tx_abort(tx);
776 			continue;
777 		}
778 		VERIFY(0 == dmu_object_free(os, obj, tx));
779 		dmu_tx_commit(tx);
780 	}
781 	/* Make sure it's not dirty before we finish destroying it. */
782 	txg_wait_synced(dd->dd_pool, 0);
783 
784 	dmu_objset_close(os);
785 	if (err != ESRCH)
786 		return (err);
787 
788 	err = dsl_dataset_open(name,
789 	    DS_MODE_EXCLUSIVE | DS_MODE_READONLY | DS_MODE_INCONSISTENT,
790 	    FTAG, &ds);
791 	if (err)
792 		return (err);
793 
794 	err = dsl_dir_open(name, FTAG, &dd, NULL);
795 	if (err) {
796 		dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG);
797 		return (err);
798 	}
799 
800 	/*
801 	 * Blow away the dsl_dir + head dataset.
802 	 */
803 	dstg = dsl_sync_task_group_create(ds->ds_dir->dd_pool);
804 	dsl_sync_task_create(dstg, dsl_dataset_destroy_check,
805 	    dsl_dataset_destroy_sync, ds, FTAG, 0);
806 	dsl_sync_task_create(dstg, dsl_dir_destroy_check,
807 	    dsl_dir_destroy_sync, dd, FTAG, 0);
808 	err = dsl_sync_task_group_wait(dstg);
809 	dsl_sync_task_group_destroy(dstg);
810 	/* if it is successful, *destroy_sync will close the ds+dd */
811 	if (err) {
812 		dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG);
813 		dsl_dir_close(dd, FTAG);
814 	}
815 	return (err);
816 }
817 
818 int
819 dsl_dataset_rollback(dsl_dataset_t *ds)
820 {
821 	ASSERT3U(ds->ds_open_refcount, ==, DS_REF_MAX);
822 	return (dsl_sync_task_do(ds->ds_dir->dd_pool,
823 	    dsl_dataset_rollback_check, dsl_dataset_rollback_sync,
824 	    ds, NULL, 0));
825 }
826 
827 void *
828 dsl_dataset_set_user_ptr(dsl_dataset_t *ds,
829     void *p, dsl_dataset_evict_func_t func)
830 {
831 	void *old;
832 
833 	mutex_enter(&ds->ds_lock);
834 	old = ds->ds_user_ptr;
835 	if (old == NULL) {
836 		ds->ds_user_ptr = p;
837 		ds->ds_user_evict_func = func;
838 	}
839 	mutex_exit(&ds->ds_lock);
840 	return (old);
841 }
842 
843 void *
844 dsl_dataset_get_user_ptr(dsl_dataset_t *ds)
845 {
846 	return (ds->ds_user_ptr);
847 }
848 
849 
850 blkptr_t *
851 dsl_dataset_get_blkptr(dsl_dataset_t *ds)
852 {
853 	return (&ds->ds_phys->ds_bp);
854 }
855 
856 void
857 dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx)
858 {
859 	ASSERT(dmu_tx_is_syncing(tx));
860 	/* If it's the meta-objset, set dp_meta_rootbp */
861 	if (ds == NULL) {
862 		tx->tx_pool->dp_meta_rootbp = *bp;
863 	} else {
864 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
865 		ds->ds_phys->ds_bp = *bp;
866 	}
867 }
868 
869 spa_t *
870 dsl_dataset_get_spa(dsl_dataset_t *ds)
871 {
872 	return (ds->ds_dir->dd_pool->dp_spa);
873 }
874 
875 void
876 dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx)
877 {
878 	dsl_pool_t *dp;
879 
880 	if (ds == NULL) /* this is the meta-objset */
881 		return;
882 
883 	ASSERT(ds->ds_user_ptr != NULL);
884 
885 	if (ds->ds_phys->ds_next_snap_obj != 0)
886 		panic("dirtying snapshot!");
887 
888 	dp = ds->ds_dir->dd_pool;
889 
890 	if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg) == 0) {
891 		/* up the hold count until we can be written out */
892 		dmu_buf_add_ref(ds->ds_dbuf, ds);
893 	}
894 }
895 
896 struct killarg {
897 	uint64_t *usedp;
898 	uint64_t *compressedp;
899 	uint64_t *uncompressedp;
900 	zio_t *zio;
901 	dmu_tx_t *tx;
902 };
903 
904 static int
905 kill_blkptr(traverse_blk_cache_t *bc, spa_t *spa, void *arg)
906 {
907 	struct killarg *ka = arg;
908 	blkptr_t *bp = &bc->bc_blkptr;
909 
910 	ASSERT3U(bc->bc_errno, ==, 0);
911 
912 	/*
913 	 * Since this callback is not called concurrently, no lock is
914 	 * needed on the accounting values.
915 	 */
916 	*ka->usedp += bp_get_dasize(spa, bp);
917 	*ka->compressedp += BP_GET_PSIZE(bp);
918 	*ka->uncompressedp += BP_GET_UCSIZE(bp);
919 	/* XXX check for EIO? */
920 	(void) arc_free(ka->zio, spa, ka->tx->tx_txg, bp, NULL, NULL,
921 	    ARC_NOWAIT);
922 	return (0);
923 }
924 
925 /* ARGSUSED */
926 static int
927 dsl_dataset_rollback_check(void *arg1, void *arg2, dmu_tx_t *tx)
928 {
929 	dsl_dataset_t *ds = arg1;
930 
931 	/*
932 	 * There must be a previous snapshot.  I suppose we could roll
933 	 * it back to being empty (and re-initialize the upper (ZPL)
934 	 * layer).  But for now there's no way to do this via the user
935 	 * interface.
936 	 */
937 	if (ds->ds_phys->ds_prev_snap_txg == 0)
938 		return (EINVAL);
939 
940 	/*
941 	 * This must not be a snapshot.
942 	 */
943 	if (ds->ds_phys->ds_next_snap_obj != 0)
944 		return (EINVAL);
945 
946 	/*
947 	 * If we made changes this txg, traverse_dsl_dataset won't find
948 	 * them.  Try again.
949 	 */
950 	if (ds->ds_phys->ds_bp.blk_birth >= tx->tx_txg)
951 		return (EAGAIN);
952 
953 	return (0);
954 }
955 
956 /* ARGSUSED */
957 static void
958 dsl_dataset_rollback_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
959 {
960 	dsl_dataset_t *ds = arg1;
961 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
962 
963 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
964 
965 	/* Before the roll back destroy the zil */
966 	zil_rollback_destroy(((objset_impl_t *)ds->ds_user_ptr)->os_zil, tx);
967 
968 	/* Zero out the deadlist. */
969 	bplist_close(&ds->ds_deadlist);
970 	bplist_destroy(mos, ds->ds_phys->ds_deadlist_obj, tx);
971 	ds->ds_phys->ds_deadlist_obj =
972 	    bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx);
973 	VERIFY(0 == bplist_open(&ds->ds_deadlist, mos,
974 	    ds->ds_phys->ds_deadlist_obj));
975 
976 	{
977 		/* Free blkptrs that we gave birth to */
978 		zio_t *zio;
979 		uint64_t used = 0, compressed = 0, uncompressed = 0;
980 		struct killarg ka;
981 
982 		zio = zio_root(tx->tx_pool->dp_spa, NULL, NULL,
983 		    ZIO_FLAG_MUSTSUCCEED);
984 		ka.usedp = &used;
985 		ka.compressedp = &compressed;
986 		ka.uncompressedp = &uncompressed;
987 		ka.zio = zio;
988 		ka.tx = tx;
989 		(void) traverse_dsl_dataset(ds, ds->ds_phys->ds_prev_snap_txg,
990 		    ADVANCE_POST, kill_blkptr, &ka);
991 		(void) zio_wait(zio);
992 
993 		dsl_dir_diduse_space(ds->ds_dir,
994 		    -used, -compressed, -uncompressed, tx);
995 	}
996 
997 	/* Change our contents to that of the prev snapshot */
998 	ASSERT3U(ds->ds_prev->ds_object, ==, ds->ds_phys->ds_prev_snap_obj);
999 	ds->ds_phys->ds_bp = ds->ds_prev->ds_phys->ds_bp;
1000 	ds->ds_phys->ds_used_bytes = ds->ds_prev->ds_phys->ds_used_bytes;
1001 	ds->ds_phys->ds_compressed_bytes =
1002 	    ds->ds_prev->ds_phys->ds_compressed_bytes;
1003 	ds->ds_phys->ds_uncompressed_bytes =
1004 	    ds->ds_prev->ds_phys->ds_uncompressed_bytes;
1005 	ds->ds_phys->ds_flags = ds->ds_prev->ds_phys->ds_flags;
1006 	ds->ds_phys->ds_unique_bytes = 0;
1007 
1008 	if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) {
1009 		dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1010 		ds->ds_prev->ds_phys->ds_unique_bytes = 0;
1011 	}
1012 
1013 	spa_history_internal_log(LOG_DS_ROLLBACK, ds->ds_dir->dd_pool->dp_spa,
1014 	    tx, cr, "dataset = %llu", ds->ds_object);
1015 }
1016 
1017 /* ARGSUSED */
1018 static int
1019 dsl_dataset_destroy_begin_check(void *arg1, void *arg2, dmu_tx_t *tx)
1020 {
1021 	dsl_dataset_t *ds = arg1;
1022 
1023 	/*
1024 	 * Can't delete a head dataset if there are snapshots of it.
1025 	 * (Except if the only snapshots are from the branch we cloned
1026 	 * from.)
1027 	 */
1028 	if (ds->ds_prev != NULL &&
1029 	    ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object)
1030 		return (EINVAL);
1031 
1032 	return (0);
1033 }
1034 
1035 /* ARGSUSED */
1036 static void
1037 dsl_dataset_destroy_begin_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
1038 {
1039 	dsl_dataset_t *ds = arg1;
1040 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1041 
1042 	/* Mark it as inconsistent on-disk, in case we crash */
1043 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1044 	ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT;
1045 
1046 	spa_history_internal_log(LOG_DS_DESTROY_BEGIN, dp->dp_spa, tx,
1047 	    cr, "dataset = %llu", ds->ds_object);
1048 }
1049 
1050 /* ARGSUSED */
1051 static int
1052 dsl_dataset_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx)
1053 {
1054 	dsl_dataset_t *ds = arg1;
1055 
1056 	/* Can't delete a branch point. */
1057 	if (ds->ds_phys->ds_num_children > 1)
1058 		return (EEXIST);
1059 
1060 	/*
1061 	 * Can't delete a head dataset if there are snapshots of it.
1062 	 * (Except if the only snapshots are from the branch we cloned
1063 	 * from.)
1064 	 */
1065 	if (ds->ds_prev != NULL &&
1066 	    ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object)
1067 		return (EINVAL);
1068 
1069 	/*
1070 	 * If we made changes this txg, traverse_dsl_dataset won't find
1071 	 * them.  Try again.
1072 	 */
1073 	if (ds->ds_phys->ds_bp.blk_birth >= tx->tx_txg)
1074 		return (EAGAIN);
1075 
1076 	/* XXX we should do some i/o error checking... */
1077 	return (0);
1078 }
1079 
1080 static void
1081 dsl_dataset_destroy_sync(void *arg1, void *tag, cred_t *cr, dmu_tx_t *tx)
1082 {
1083 	dsl_dataset_t *ds = arg1;
1084 	uint64_t used = 0, compressed = 0, uncompressed = 0;
1085 	zio_t *zio;
1086 	int err;
1087 	int after_branch_point = FALSE;
1088 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1089 	objset_t *mos = dp->dp_meta_objset;
1090 	dsl_dataset_t *ds_prev = NULL;
1091 	uint64_t obj;
1092 
1093 	ASSERT3U(ds->ds_open_refcount, ==, DS_REF_MAX);
1094 	ASSERT3U(ds->ds_phys->ds_num_children, <=, 1);
1095 	ASSERT(ds->ds_prev == NULL ||
1096 	    ds->ds_prev->ds_phys->ds_next_snap_obj != ds->ds_object);
1097 	ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg);
1098 
1099 	ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock));
1100 
1101 	obj = ds->ds_object;
1102 
1103 	if (ds->ds_phys->ds_prev_snap_obj != 0) {
1104 		if (ds->ds_prev) {
1105 			ds_prev = ds->ds_prev;
1106 		} else {
1107 			VERIFY(0 == dsl_dataset_open_obj(dp,
1108 			    ds->ds_phys->ds_prev_snap_obj, NULL,
1109 			    DS_MODE_NONE, FTAG, &ds_prev));
1110 		}
1111 		after_branch_point =
1112 		    (ds_prev->ds_phys->ds_next_snap_obj != obj);
1113 
1114 		dmu_buf_will_dirty(ds_prev->ds_dbuf, tx);
1115 		if (after_branch_point &&
1116 		    ds->ds_phys->ds_next_snap_obj == 0) {
1117 			/* This clone is toast. */
1118 			ASSERT(ds_prev->ds_phys->ds_num_children > 1);
1119 			ds_prev->ds_phys->ds_num_children--;
1120 		} else if (!after_branch_point) {
1121 			ds_prev->ds_phys->ds_next_snap_obj =
1122 			    ds->ds_phys->ds_next_snap_obj;
1123 		}
1124 	}
1125 
1126 	zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
1127 
1128 	if (ds->ds_phys->ds_next_snap_obj != 0) {
1129 		blkptr_t bp;
1130 		dsl_dataset_t *ds_next;
1131 		uint64_t itor = 0;
1132 
1133 		spa_scrub_restart(dp->dp_spa, tx->tx_txg);
1134 
1135 		VERIFY(0 == dsl_dataset_open_obj(dp,
1136 		    ds->ds_phys->ds_next_snap_obj, NULL,
1137 		    DS_MODE_NONE, FTAG, &ds_next));
1138 		ASSERT3U(ds_next->ds_phys->ds_prev_snap_obj, ==, obj);
1139 
1140 		dmu_buf_will_dirty(ds_next->ds_dbuf, tx);
1141 		ds_next->ds_phys->ds_prev_snap_obj =
1142 		    ds->ds_phys->ds_prev_snap_obj;
1143 		ds_next->ds_phys->ds_prev_snap_txg =
1144 		    ds->ds_phys->ds_prev_snap_txg;
1145 		ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
1146 		    ds_prev ? ds_prev->ds_phys->ds_creation_txg : 0);
1147 
1148 		/*
1149 		 * Transfer to our deadlist (which will become next's
1150 		 * new deadlist) any entries from next's current
1151 		 * deadlist which were born before prev, and free the
1152 		 * other entries.
1153 		 *
1154 		 * XXX we're doing this long task with the config lock held
1155 		 */
1156 		while (bplist_iterate(&ds_next->ds_deadlist, &itor,
1157 		    &bp) == 0) {
1158 			if (bp.blk_birth <= ds->ds_phys->ds_prev_snap_txg) {
1159 				VERIFY(0 == bplist_enqueue(&ds->ds_deadlist,
1160 				    &bp, tx));
1161 				if (ds_prev && !after_branch_point &&
1162 				    bp.blk_birth >
1163 				    ds_prev->ds_phys->ds_prev_snap_txg) {
1164 					ds_prev->ds_phys->ds_unique_bytes +=
1165 					    bp_get_dasize(dp->dp_spa, &bp);
1166 				}
1167 			} else {
1168 				used += bp_get_dasize(dp->dp_spa, &bp);
1169 				compressed += BP_GET_PSIZE(&bp);
1170 				uncompressed += BP_GET_UCSIZE(&bp);
1171 				/* XXX check return value? */
1172 				(void) arc_free(zio, dp->dp_spa, tx->tx_txg,
1173 				    &bp, NULL, NULL, ARC_NOWAIT);
1174 			}
1175 		}
1176 
1177 		/* free next's deadlist */
1178 		bplist_close(&ds_next->ds_deadlist);
1179 		bplist_destroy(mos, ds_next->ds_phys->ds_deadlist_obj, tx);
1180 
1181 		/* set next's deadlist to our deadlist */
1182 		ds_next->ds_phys->ds_deadlist_obj =
1183 		    ds->ds_phys->ds_deadlist_obj;
1184 		VERIFY(0 == bplist_open(&ds_next->ds_deadlist, mos,
1185 		    ds_next->ds_phys->ds_deadlist_obj));
1186 		ds->ds_phys->ds_deadlist_obj = 0;
1187 
1188 		if (ds_next->ds_phys->ds_next_snap_obj != 0) {
1189 			/*
1190 			 * Update next's unique to include blocks which
1191 			 * were previously shared by only this snapshot
1192 			 * and it.  Those blocks will be born after the
1193 			 * prev snap and before this snap, and will have
1194 			 * died after the next snap and before the one
1195 			 * after that (ie. be on the snap after next's
1196 			 * deadlist).
1197 			 *
1198 			 * XXX we're doing this long task with the
1199 			 * config lock held
1200 			 */
1201 			dsl_dataset_t *ds_after_next;
1202 
1203 			VERIFY(0 == dsl_dataset_open_obj(dp,
1204 			    ds_next->ds_phys->ds_next_snap_obj, NULL,
1205 			    DS_MODE_NONE, FTAG, &ds_after_next));
1206 			itor = 0;
1207 			while (bplist_iterate(&ds_after_next->ds_deadlist,
1208 			    &itor, &bp) == 0) {
1209 				if (bp.blk_birth >
1210 				    ds->ds_phys->ds_prev_snap_txg &&
1211 				    bp.blk_birth <=
1212 				    ds->ds_phys->ds_creation_txg) {
1213 					ds_next->ds_phys->ds_unique_bytes +=
1214 					    bp_get_dasize(dp->dp_spa, &bp);
1215 				}
1216 			}
1217 
1218 			dsl_dataset_close(ds_after_next, DS_MODE_NONE, FTAG);
1219 			ASSERT3P(ds_next->ds_prev, ==, NULL);
1220 		} else {
1221 			/*
1222 			 * It would be nice to update the head dataset's
1223 			 * unique.  To do so we would have to traverse
1224 			 * it for blocks born after ds_prev, which is
1225 			 * pretty expensive just to maintain something
1226 			 * for debugging purposes.
1227 			 */
1228 			ASSERT3P(ds_next->ds_prev, ==, ds);
1229 			dsl_dataset_close(ds_next->ds_prev, DS_MODE_NONE,
1230 			    ds_next);
1231 			if (ds_prev) {
1232 				VERIFY(0 == dsl_dataset_open_obj(dp,
1233 				    ds->ds_phys->ds_prev_snap_obj, NULL,
1234 				    DS_MODE_NONE, ds_next, &ds_next->ds_prev));
1235 			} else {
1236 				ds_next->ds_prev = NULL;
1237 			}
1238 		}
1239 		dsl_dataset_close(ds_next, DS_MODE_NONE, FTAG);
1240 
1241 		/*
1242 		 * NB: unique_bytes is not accurate for head objsets
1243 		 * because we don't update it when we delete the most
1244 		 * recent snapshot -- see above comment.
1245 		 */
1246 		ASSERT3U(used, ==, ds->ds_phys->ds_unique_bytes);
1247 	} else {
1248 		/*
1249 		 * There's no next snapshot, so this is a head dataset.
1250 		 * Destroy the deadlist.  Unless it's a clone, the
1251 		 * deadlist should be empty.  (If it's a clone, it's
1252 		 * safe to ignore the deadlist contents.)
1253 		 */
1254 		struct killarg ka;
1255 
1256 		ASSERT(after_branch_point || bplist_empty(&ds->ds_deadlist));
1257 		bplist_close(&ds->ds_deadlist);
1258 		bplist_destroy(mos, ds->ds_phys->ds_deadlist_obj, tx);
1259 		ds->ds_phys->ds_deadlist_obj = 0;
1260 
1261 		/*
1262 		 * Free everything that we point to (that's born after
1263 		 * the previous snapshot, if we are a clone)
1264 		 *
1265 		 * XXX we're doing this long task with the config lock held
1266 		 */
1267 		ka.usedp = &used;
1268 		ka.compressedp = &compressed;
1269 		ka.uncompressedp = &uncompressed;
1270 		ka.zio = zio;
1271 		ka.tx = tx;
1272 		err = traverse_dsl_dataset(ds, ds->ds_phys->ds_prev_snap_txg,
1273 		    ADVANCE_POST, kill_blkptr, &ka);
1274 		ASSERT3U(err, ==, 0);
1275 	}
1276 
1277 	err = zio_wait(zio);
1278 	ASSERT3U(err, ==, 0);
1279 
1280 	dsl_dir_diduse_space(ds->ds_dir, -used, -compressed, -uncompressed, tx);
1281 
1282 	if (ds->ds_phys->ds_snapnames_zapobj) {
1283 		err = zap_destroy(mos, ds->ds_phys->ds_snapnames_zapobj, tx);
1284 		ASSERT(err == 0);
1285 	}
1286 
1287 	if (ds->ds_dir->dd_phys->dd_head_dataset_obj == ds->ds_object) {
1288 		/* Erase the link in the dataset */
1289 		dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
1290 		ds->ds_dir->dd_phys->dd_head_dataset_obj = 0;
1291 		/*
1292 		 * dsl_dir_sync_destroy() called us, they'll destroy
1293 		 * the dataset.
1294 		 */
1295 	} else {
1296 		/* remove from snapshot namespace */
1297 		dsl_dataset_t *ds_head;
1298 		VERIFY(0 == dsl_dataset_open_obj(dp,
1299 		    ds->ds_dir->dd_phys->dd_head_dataset_obj, NULL,
1300 		    DS_MODE_NONE, FTAG, &ds_head));
1301 		VERIFY(0 == dsl_dataset_get_snapname(ds));
1302 #ifdef ZFS_DEBUG
1303 		{
1304 			uint64_t val;
1305 			err = zap_lookup(mos,
1306 			    ds_head->ds_phys->ds_snapnames_zapobj,
1307 			    ds->ds_snapname, 8, 1, &val);
1308 			ASSERT3U(err, ==, 0);
1309 			ASSERT3U(val, ==, obj);
1310 		}
1311 #endif
1312 		err = zap_remove(mos, ds_head->ds_phys->ds_snapnames_zapobj,
1313 		    ds->ds_snapname, tx);
1314 		ASSERT(err == 0);
1315 		dsl_dataset_close(ds_head, DS_MODE_NONE, FTAG);
1316 	}
1317 
1318 	if (ds_prev && ds->ds_prev != ds_prev)
1319 		dsl_dataset_close(ds_prev, DS_MODE_NONE, FTAG);
1320 
1321 	spa_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
1322 	spa_history_internal_log(LOG_DS_DESTROY, dp->dp_spa, tx,
1323 	    cr, "dataset = %llu", ds->ds_object);
1324 
1325 	dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, tag);
1326 	VERIFY(0 == dmu_object_free(mos, obj, tx));
1327 
1328 }
1329 
1330 /* ARGSUSED */
1331 int
1332 dsl_dataset_snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx)
1333 {
1334 	objset_t *os = arg1;
1335 	dsl_dataset_t *ds = os->os->os_dsl_dataset;
1336 	const char *snapname = arg2;
1337 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
1338 	int err;
1339 	uint64_t value;
1340 
1341 	/*
1342 	 * We don't allow multiple snapshots of the same txg.  If there
1343 	 * is already one, try again.
1344 	 */
1345 	if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg)
1346 		return (EAGAIN);
1347 
1348 	/*
1349 	 * Check for conflicting name snapshot name.
1350 	 */
1351 	err = zap_lookup(mos, ds->ds_phys->ds_snapnames_zapobj,
1352 	    snapname, 8, 1, &value);
1353 	if (err == 0)
1354 		return (EEXIST);
1355 	if (err != ENOENT)
1356 		return (err);
1357 
1358 	/*
1359 	 * Check that the dataset's name is not too long.  Name consists
1360 	 * of the dataset's length + 1 for the @-sign + snapshot name's length
1361 	 */
1362 	if (dsl_dataset_namelen(ds) + 1 + strlen(snapname) >= MAXNAMELEN)
1363 		return (ENAMETOOLONG);
1364 
1365 	ds->ds_trysnap_txg = tx->tx_txg;
1366 	return (0);
1367 }
1368 
1369 void
1370 dsl_dataset_snapshot_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
1371 {
1372 	objset_t *os = arg1;
1373 	dsl_dataset_t *ds = os->os->os_dsl_dataset;
1374 	const char *snapname = arg2;
1375 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1376 	dmu_buf_t *dbuf;
1377 	dsl_dataset_phys_t *dsphys;
1378 	uint64_t dsobj;
1379 	objset_t *mos = dp->dp_meta_objset;
1380 	int err;
1381 
1382 	spa_scrub_restart(dp->dp_spa, tx->tx_txg);
1383 	ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock));
1384 
1385 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
1386 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
1387 	VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
1388 	dmu_buf_will_dirty(dbuf, tx);
1389 	dsphys = dbuf->db_data;
1390 	dsphys->ds_dir_obj = ds->ds_dir->dd_object;
1391 	dsphys->ds_fsid_guid = unique_create();
1392 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
1393 	    sizeof (dsphys->ds_guid));
1394 	dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj;
1395 	dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg;
1396 	dsphys->ds_next_snap_obj = ds->ds_object;
1397 	dsphys->ds_num_children = 1;
1398 	dsphys->ds_creation_time = gethrestime_sec();
1399 	dsphys->ds_creation_txg = tx->tx_txg;
1400 	dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj;
1401 	dsphys->ds_used_bytes = ds->ds_phys->ds_used_bytes;
1402 	dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes;
1403 	dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes;
1404 	dsphys->ds_flags = ds->ds_phys->ds_flags;
1405 	dsphys->ds_bp = ds->ds_phys->ds_bp;
1406 	dmu_buf_rele(dbuf, FTAG);
1407 
1408 	ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0);
1409 	if (ds->ds_prev) {
1410 		ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj ==
1411 		    ds->ds_object ||
1412 		    ds->ds_prev->ds_phys->ds_num_children > 1);
1413 		if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) {
1414 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1415 			ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
1416 			    ds->ds_prev->ds_phys->ds_creation_txg);
1417 			ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj;
1418 		}
1419 	}
1420 
1421 	bplist_close(&ds->ds_deadlist);
1422 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1423 	ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, dsphys->ds_creation_txg);
1424 	ds->ds_phys->ds_prev_snap_obj = dsobj;
1425 	ds->ds_phys->ds_prev_snap_txg = dsphys->ds_creation_txg;
1426 	ds->ds_phys->ds_unique_bytes = 0;
1427 	ds->ds_phys->ds_deadlist_obj =
1428 	    bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx);
1429 	VERIFY(0 == bplist_open(&ds->ds_deadlist, mos,
1430 	    ds->ds_phys->ds_deadlist_obj));
1431 
1432 	dprintf("snap '%s' -> obj %llu\n", snapname, dsobj);
1433 	err = zap_add(mos, ds->ds_phys->ds_snapnames_zapobj,
1434 	    snapname, 8, 1, &dsobj, tx);
1435 	ASSERT(err == 0);
1436 
1437 	if (ds->ds_prev)
1438 		dsl_dataset_close(ds->ds_prev, DS_MODE_NONE, ds);
1439 	VERIFY(0 == dsl_dataset_open_obj(dp,
1440 	    ds->ds_phys->ds_prev_snap_obj, snapname,
1441 	    DS_MODE_NONE, ds, &ds->ds_prev));
1442 
1443 	spa_history_internal_log(LOG_DS_SNAPSHOT, dp->dp_spa, tx, cr,
1444 	    "dataset = %llu", dsobj);
1445 }
1446 
1447 void
1448 dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
1449 {
1450 	ASSERT(dmu_tx_is_syncing(tx));
1451 	ASSERT(ds->ds_user_ptr != NULL);
1452 	ASSERT(ds->ds_phys->ds_next_snap_obj == 0);
1453 
1454 	/*
1455 	 * in case we had to change ds_fsid_guid when we opened it,
1456 	 * sync it out now.
1457 	 */
1458 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1459 	ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid;
1460 
1461 	dsl_dir_dirty(ds->ds_dir, tx);
1462 	dmu_objset_sync(ds->ds_user_ptr, zio, tx);
1463 }
1464 
1465 void
1466 dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
1467 {
1468 	dsl_dir_stats(ds->ds_dir, nv);
1469 
1470 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
1471 	    ds->ds_phys->ds_creation_time);
1472 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
1473 	    ds->ds_phys->ds_creation_txg);
1474 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED,
1475 	    ds->ds_phys->ds_used_bytes);
1476 
1477 	if (ds->ds_phys->ds_next_snap_obj) {
1478 		/*
1479 		 * This is a snapshot; override the dd's space used with
1480 		 * our unique space and compression ratio.
1481 		 */
1482 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
1483 		    ds->ds_phys->ds_unique_bytes);
1484 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
1485 		    ds->ds_phys->ds_compressed_bytes == 0 ? 100 :
1486 		    (ds->ds_phys->ds_uncompressed_bytes * 100 /
1487 		    ds->ds_phys->ds_compressed_bytes));
1488 	}
1489 }
1490 
1491 void
1492 dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
1493 {
1494 	stat->dds_creation_txg = ds->ds_phys->ds_creation_txg;
1495 	stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT;
1496 	if (ds->ds_phys->ds_next_snap_obj) {
1497 		stat->dds_is_snapshot = B_TRUE;
1498 		stat->dds_num_clones = ds->ds_phys->ds_num_children - 1;
1499 	}
1500 
1501 	/* clone origin is really a dsl_dir thing... */
1502 	if (ds->ds_dir->dd_phys->dd_clone_parent_obj) {
1503 		dsl_dataset_t *ods;
1504 
1505 		rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER);
1506 		VERIFY(0 == dsl_dataset_open_obj(ds->ds_dir->dd_pool,
1507 		    ds->ds_dir->dd_phys->dd_clone_parent_obj,
1508 		    NULL, DS_MODE_NONE, FTAG, &ods));
1509 		dsl_dataset_name(ods, stat->dds_clone_of);
1510 		dsl_dataset_close(ods, DS_MODE_NONE, FTAG);
1511 		rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
1512 	}
1513 }
1514 
1515 uint64_t
1516 dsl_dataset_fsid_guid(dsl_dataset_t *ds)
1517 {
1518 	return (ds->ds_fsid_guid);
1519 }
1520 
1521 void
1522 dsl_dataset_space(dsl_dataset_t *ds,
1523     uint64_t *refdbytesp, uint64_t *availbytesp,
1524     uint64_t *usedobjsp, uint64_t *availobjsp)
1525 {
1526 	*refdbytesp = ds->ds_phys->ds_used_bytes;
1527 	*availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
1528 	*usedobjsp = ds->ds_phys->ds_bp.blk_fill;
1529 	*availobjsp = DN_MAX_OBJECT - *usedobjsp;
1530 }
1531 
1532 /* ARGSUSED */
1533 static int
1534 dsl_dataset_snapshot_rename_check(void *arg1, void *arg2, dmu_tx_t *tx)
1535 {
1536 	dsl_dataset_t *ds = arg1;
1537 	char *newsnapname = arg2;
1538 	dsl_dir_t *dd = ds->ds_dir;
1539 	objset_t *mos = dd->dd_pool->dp_meta_objset;
1540 	dsl_dataset_t *hds;
1541 	uint64_t val;
1542 	int err;
1543 
1544 	err = dsl_dataset_open_obj(dd->dd_pool,
1545 	    dd->dd_phys->dd_head_dataset_obj, NULL, DS_MODE_NONE, FTAG, &hds);
1546 	if (err)
1547 		return (err);
1548 
1549 	/* new name better not be in use */
1550 	err = zap_lookup(mos, hds->ds_phys->ds_snapnames_zapobj,
1551 	    newsnapname, 8, 1, &val);
1552 	dsl_dataset_close(hds, DS_MODE_NONE, FTAG);
1553 
1554 	if (err == 0)
1555 		err = EEXIST;
1556 	else if (err == ENOENT)
1557 		err = 0;
1558 
1559 	/* dataset name + 1 for the "@" + the new snapshot name must fit */
1560 	if (dsl_dir_namelen(ds->ds_dir) + 1 + strlen(newsnapname) >= MAXNAMELEN)
1561 		err = ENAMETOOLONG;
1562 
1563 	return (err);
1564 }
1565 
1566 static void
1567 dsl_dataset_snapshot_rename_sync(void *arg1, void *arg2,
1568     cred_t *cr, dmu_tx_t *tx)
1569 {
1570 	dsl_dataset_t *ds = arg1;
1571 	const char *newsnapname = arg2;
1572 	dsl_dir_t *dd = ds->ds_dir;
1573 	objset_t *mos = dd->dd_pool->dp_meta_objset;
1574 	dsl_dataset_t *hds;
1575 	int err;
1576 
1577 	ASSERT(ds->ds_phys->ds_next_snap_obj != 0);
1578 
1579 	VERIFY(0 == dsl_dataset_open_obj(dd->dd_pool,
1580 	    dd->dd_phys->dd_head_dataset_obj, NULL, DS_MODE_NONE, FTAG, &hds));
1581 
1582 	VERIFY(0 == dsl_dataset_get_snapname(ds));
1583 	err = zap_remove(mos, hds->ds_phys->ds_snapnames_zapobj,
1584 	    ds->ds_snapname, tx);
1585 	ASSERT3U(err, ==, 0);
1586 	mutex_enter(&ds->ds_lock);
1587 	(void) strcpy(ds->ds_snapname, newsnapname);
1588 	mutex_exit(&ds->ds_lock);
1589 	err = zap_add(mos, hds->ds_phys->ds_snapnames_zapobj,
1590 	    ds->ds_snapname, 8, 1, &ds->ds_object, tx);
1591 	ASSERT3U(err, ==, 0);
1592 
1593 	spa_history_internal_log(LOG_DS_RENAME, dd->dd_pool->dp_spa, tx,
1594 	    cr, "dataset = %llu", ds->ds_object);
1595 	dsl_dataset_close(hds, DS_MODE_NONE, FTAG);
1596 }
1597 
1598 struct renamearg {
1599 	dsl_sync_task_group_t *dstg;
1600 	char failed[MAXPATHLEN];
1601 	char *oldsnap;
1602 	char *newsnap;
1603 };
1604 
1605 static int
1606 dsl_snapshot_rename_one(char *name, void *arg)
1607 {
1608 	struct renamearg *ra = arg;
1609 	dsl_dataset_t *ds = NULL;
1610 	char *cp;
1611 	int err;
1612 
1613 	cp = name + strlen(name);
1614 	*cp = '@';
1615 	(void) strcpy(cp + 1, ra->oldsnap);
1616 
1617 	/*
1618 	 * For recursive snapshot renames the parent won't be changing
1619 	 * so we just pass name for both the to/from argument.
1620 	 */
1621 	if (err = zfs_secpolicy_rename_perms(name, name, CRED())) {
1622 		(void) strcpy(ra->failed, name);
1623 		return (err);
1624 	}
1625 
1626 	err = dsl_dataset_open(name, DS_MODE_READONLY | DS_MODE_STANDARD,
1627 	    ra->dstg, &ds);
1628 	if (err == ENOENT) {
1629 		*cp = '\0';
1630 		return (0);
1631 	}
1632 	if (err) {
1633 		(void) strcpy(ra->failed, name);
1634 		*cp = '\0';
1635 		dsl_dataset_close(ds, DS_MODE_STANDARD, ra->dstg);
1636 		return (err);
1637 	}
1638 
1639 #ifdef _KERNEL
1640 	/* for all filesystems undergoing rename, we'll need to unmount it */
1641 	(void) zfs_unmount_snap(name, NULL);
1642 #endif
1643 
1644 	*cp = '\0';
1645 
1646 	dsl_sync_task_create(ra->dstg, dsl_dataset_snapshot_rename_check,
1647 	    dsl_dataset_snapshot_rename_sync, ds, ra->newsnap, 0);
1648 
1649 	return (0);
1650 }
1651 
1652 static int
1653 dsl_recursive_rename(char *oldname, const char *newname)
1654 {
1655 	int err;
1656 	struct renamearg *ra;
1657 	dsl_sync_task_t *dst;
1658 	spa_t *spa;
1659 	char *cp, *fsname = spa_strdup(oldname);
1660 	int len = strlen(oldname);
1661 
1662 	/* truncate the snapshot name to get the fsname */
1663 	cp = strchr(fsname, '@');
1664 	*cp = '\0';
1665 
1666 	err = spa_open(fsname, &spa, FTAG);
1667 	if (err) {
1668 		kmem_free(fsname, len + 1);
1669 		return (err);
1670 	}
1671 	ra = kmem_alloc(sizeof (struct renamearg), KM_SLEEP);
1672 	ra->dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
1673 
1674 	ra->oldsnap = strchr(oldname, '@') + 1;
1675 	ra->newsnap = strchr(newname, '@') + 1;
1676 	*ra->failed = '\0';
1677 
1678 	err = dmu_objset_find(fsname, dsl_snapshot_rename_one, ra,
1679 	    DS_FIND_CHILDREN);
1680 	kmem_free(fsname, len + 1);
1681 
1682 	if (err == 0) {
1683 		err = dsl_sync_task_group_wait(ra->dstg);
1684 	}
1685 
1686 	for (dst = list_head(&ra->dstg->dstg_tasks); dst;
1687 	    dst = list_next(&ra->dstg->dstg_tasks, dst)) {
1688 		dsl_dataset_t *ds = dst->dst_arg1;
1689 		if (dst->dst_err) {
1690 			dsl_dir_name(ds->ds_dir, ra->failed);
1691 			(void) strcat(ra->failed, "@");
1692 			(void) strcat(ra->failed, ra->newsnap);
1693 		}
1694 		dsl_dataset_close(ds, DS_MODE_STANDARD, ra->dstg);
1695 	}
1696 
1697 	if (err)
1698 		(void) strcpy(oldname, ra->failed);
1699 
1700 	dsl_sync_task_group_destroy(ra->dstg);
1701 	kmem_free(ra, sizeof (struct renamearg));
1702 	spa_close(spa, FTAG);
1703 	return (err);
1704 }
1705 
1706 static int
1707 dsl_valid_rename(char *oldname, void *arg)
1708 {
1709 	int delta = *(int *)arg;
1710 
1711 	if (strlen(oldname) + delta >= MAXNAMELEN)
1712 		return (ENAMETOOLONG);
1713 
1714 	return (0);
1715 }
1716 
1717 #pragma weak dmu_objset_rename = dsl_dataset_rename
1718 int
1719 dsl_dataset_rename(char *oldname, const char *newname,
1720     boolean_t recursive)
1721 {
1722 	dsl_dir_t *dd;
1723 	dsl_dataset_t *ds;
1724 	const char *tail;
1725 	int err;
1726 
1727 	err = dsl_dir_open(oldname, FTAG, &dd, &tail);
1728 	if (err)
1729 		return (err);
1730 	if (tail == NULL) {
1731 		int delta = strlen(newname) - strlen(oldname);
1732 
1733 		/* if we're growing, validate child size lengths */
1734 		if (delta > 0)
1735 			err = dmu_objset_find(oldname, dsl_valid_rename,
1736 			    &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
1737 
1738 		if (!err)
1739 			err = dsl_dir_rename(dd, newname);
1740 		dsl_dir_close(dd, FTAG);
1741 		return (err);
1742 	}
1743 	if (tail[0] != '@') {
1744 		/* the name ended in a nonexistant component */
1745 		dsl_dir_close(dd, FTAG);
1746 		return (ENOENT);
1747 	}
1748 
1749 	dsl_dir_close(dd, FTAG);
1750 
1751 	/* new name must be snapshot in same filesystem */
1752 	tail = strchr(newname, '@');
1753 	if (tail == NULL)
1754 		return (EINVAL);
1755 	tail++;
1756 	if (strncmp(oldname, newname, tail - newname) != 0)
1757 		return (EXDEV);
1758 
1759 	if (recursive) {
1760 		err = dsl_recursive_rename(oldname, newname);
1761 	} else {
1762 		err = dsl_dataset_open(oldname,
1763 		    DS_MODE_READONLY | DS_MODE_STANDARD, FTAG, &ds);
1764 		if (err)
1765 			return (err);
1766 
1767 		err = dsl_sync_task_do(ds->ds_dir->dd_pool,
1768 		    dsl_dataset_snapshot_rename_check,
1769 		    dsl_dataset_snapshot_rename_sync, ds, (char *)tail, 1);
1770 
1771 		dsl_dataset_close(ds, DS_MODE_STANDARD, FTAG);
1772 	}
1773 
1774 	return (err);
1775 }
1776 
1777 struct promotearg {
1778 	uint64_t used, comp, uncomp, unique;
1779 	uint64_t newnext_obj, snapnames_obj;
1780 };
1781 
1782 /* ARGSUSED */
1783 static int
1784 dsl_dataset_promote_check(void *arg1, void *arg2, dmu_tx_t *tx)
1785 {
1786 	dsl_dataset_t *hds = arg1;
1787 	struct promotearg *pa = arg2;
1788 	dsl_dir_t *dd = hds->ds_dir;
1789 	dsl_pool_t *dp = hds->ds_dir->dd_pool;
1790 	dsl_dir_t *pdd = NULL;
1791 	dsl_dataset_t *ds = NULL;
1792 	dsl_dataset_t *pivot_ds = NULL;
1793 	dsl_dataset_t *newnext_ds = NULL;
1794 	int err;
1795 	char *name = NULL;
1796 	uint64_t itor = 0;
1797 	blkptr_t bp;
1798 
1799 	bzero(pa, sizeof (*pa));
1800 
1801 	/* Check that it is a clone */
1802 	if (dd->dd_phys->dd_clone_parent_obj == 0)
1803 		return (EINVAL);
1804 
1805 	/* Since this is so expensive, don't do the preliminary check */
1806 	if (!dmu_tx_is_syncing(tx))
1807 		return (0);
1808 
1809 	if (err = dsl_dataset_open_obj(dp,
1810 	    dd->dd_phys->dd_clone_parent_obj,
1811 	    NULL, DS_MODE_EXCLUSIVE, FTAG, &pivot_ds))
1812 		goto out;
1813 	pdd = pivot_ds->ds_dir;
1814 
1815 	{
1816 		dsl_dataset_t *phds;
1817 		if (err = dsl_dataset_open_obj(dd->dd_pool,
1818 		    pdd->dd_phys->dd_head_dataset_obj,
1819 		    NULL, DS_MODE_NONE, FTAG, &phds))
1820 			goto out;
1821 		pa->snapnames_obj = phds->ds_phys->ds_snapnames_zapobj;
1822 		dsl_dataset_close(phds, DS_MODE_NONE, FTAG);
1823 	}
1824 
1825 	if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE) {
1826 		err = EXDEV;
1827 		goto out;
1828 	}
1829 
1830 	/* find pivot point's new next ds */
1831 	VERIFY(0 == dsl_dataset_open_obj(dd->dd_pool, hds->ds_object,
1832 	    NULL, DS_MODE_NONE, FTAG, &newnext_ds));
1833 	while (newnext_ds->ds_phys->ds_prev_snap_obj != pivot_ds->ds_object) {
1834 		dsl_dataset_t *prev;
1835 
1836 		if (err = dsl_dataset_open_obj(dd->dd_pool,
1837 		    newnext_ds->ds_phys->ds_prev_snap_obj,
1838 		    NULL, DS_MODE_NONE, FTAG, &prev))
1839 			goto out;
1840 		dsl_dataset_close(newnext_ds, DS_MODE_NONE, FTAG);
1841 		newnext_ds = prev;
1842 	}
1843 	pa->newnext_obj = newnext_ds->ds_object;
1844 
1845 	/* compute pivot point's new unique space */
1846 	while ((err = bplist_iterate(&newnext_ds->ds_deadlist,
1847 	    &itor, &bp)) == 0) {
1848 		if (bp.blk_birth > pivot_ds->ds_phys->ds_prev_snap_txg)
1849 			pa->unique += bp_get_dasize(dd->dd_pool->dp_spa, &bp);
1850 	}
1851 	if (err != ENOENT)
1852 		goto out;
1853 
1854 	/* Walk the snapshots that we are moving */
1855 	name = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1856 	ds = pivot_ds;
1857 	/* CONSTCOND */
1858 	while (TRUE) {
1859 		uint64_t val, dlused, dlcomp, dluncomp;
1860 		dsl_dataset_t *prev;
1861 
1862 		/* Check that the snapshot name does not conflict */
1863 		dsl_dataset_name(ds, name);
1864 		err = zap_lookup(dd->dd_pool->dp_meta_objset,
1865 		    hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname,
1866 		    8, 1, &val);
1867 		if (err != ENOENT) {
1868 			if (err == 0)
1869 				err = EEXIST;
1870 			goto out;
1871 		}
1872 
1873 		/*
1874 		 * compute space to transfer.  Each snapshot gave birth to:
1875 		 * (my used) - (prev's used) + (deadlist's used)
1876 		 */
1877 		pa->used += ds->ds_phys->ds_used_bytes;
1878 		pa->comp += ds->ds_phys->ds_compressed_bytes;
1879 		pa->uncomp += ds->ds_phys->ds_uncompressed_bytes;
1880 
1881 		/* If we reach the first snapshot, we're done. */
1882 		if (ds->ds_phys->ds_prev_snap_obj == 0)
1883 			break;
1884 
1885 		if (err = bplist_space(&ds->ds_deadlist,
1886 		    &dlused, &dlcomp, &dluncomp))
1887 			goto out;
1888 		if (err = dsl_dataset_open_obj(dd->dd_pool,
1889 		    ds->ds_phys->ds_prev_snap_obj, NULL, DS_MODE_EXCLUSIVE,
1890 		    FTAG, &prev))
1891 			goto out;
1892 		pa->used += dlused - prev->ds_phys->ds_used_bytes;
1893 		pa->comp += dlcomp - prev->ds_phys->ds_compressed_bytes;
1894 		pa->uncomp += dluncomp - prev->ds_phys->ds_uncompressed_bytes;
1895 
1896 		/*
1897 		 * We could be a clone of a clone.  If we reach our
1898 		 * parent's branch point, we're done.
1899 		 */
1900 		if (prev->ds_phys->ds_next_snap_obj != ds->ds_object) {
1901 			dsl_dataset_close(prev, DS_MODE_EXCLUSIVE, FTAG);
1902 			break;
1903 		}
1904 		if (ds != pivot_ds)
1905 			dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG);
1906 		ds = prev;
1907 	}
1908 
1909 	/* Check that there is enough space here */
1910 	err = dsl_dir_transfer_possible(pdd, dd, pa->used);
1911 
1912 out:
1913 	if (ds && ds != pivot_ds)
1914 		dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG);
1915 	if (pivot_ds)
1916 		dsl_dataset_close(pivot_ds, DS_MODE_EXCLUSIVE, FTAG);
1917 	if (newnext_ds)
1918 		dsl_dataset_close(newnext_ds, DS_MODE_NONE, FTAG);
1919 	if (name)
1920 		kmem_free(name, MAXPATHLEN);
1921 	return (err);
1922 }
1923 
1924 static void
1925 dsl_dataset_promote_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
1926 {
1927 	dsl_dataset_t *hds = arg1;
1928 	struct promotearg *pa = arg2;
1929 	dsl_dir_t *dd = hds->ds_dir;
1930 	dsl_pool_t *dp = hds->ds_dir->dd_pool;
1931 	dsl_dir_t *pdd = NULL;
1932 	dsl_dataset_t *ds, *pivot_ds;
1933 	char *name;
1934 
1935 	ASSERT(dd->dd_phys->dd_clone_parent_obj != 0);
1936 	ASSERT(0 == (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE));
1937 
1938 	VERIFY(0 == dsl_dataset_open_obj(dp,
1939 	    dd->dd_phys->dd_clone_parent_obj,
1940 	    NULL, DS_MODE_EXCLUSIVE, FTAG, &pivot_ds));
1941 	/*
1942 	 * We need to explicitly open pdd, since pivot_ds's pdd will be
1943 	 * changing.
1944 	 */
1945 	VERIFY(0 == dsl_dir_open_obj(dp, pivot_ds->ds_dir->dd_object,
1946 	    NULL, FTAG, &pdd));
1947 
1948 	/* move snapshots to this dir */
1949 	name = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1950 	ds = pivot_ds;
1951 	/* CONSTCOND */
1952 	while (TRUE) {
1953 		dsl_dataset_t *prev;
1954 
1955 		/* move snap name entry */
1956 		dsl_dataset_name(ds, name);
1957 		VERIFY(0 == zap_remove(dp->dp_meta_objset,
1958 		    pa->snapnames_obj, ds->ds_snapname, tx));
1959 		VERIFY(0 == zap_add(dp->dp_meta_objset,
1960 		    hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname,
1961 		    8, 1, &ds->ds_object, tx));
1962 
1963 		/* change containing dsl_dir */
1964 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
1965 		ASSERT3U(ds->ds_phys->ds_dir_obj, ==, pdd->dd_object);
1966 		ds->ds_phys->ds_dir_obj = dd->dd_object;
1967 		ASSERT3P(ds->ds_dir, ==, pdd);
1968 		dsl_dir_close(ds->ds_dir, ds);
1969 		VERIFY(0 == dsl_dir_open_obj(dp, dd->dd_object,
1970 		    NULL, ds, &ds->ds_dir));
1971 
1972 		ASSERT3U(dsl_prop_numcb(ds), ==, 0);
1973 
1974 		if (ds->ds_phys->ds_prev_snap_obj == 0)
1975 			break;
1976 
1977 		VERIFY(0 == dsl_dataset_open_obj(dp,
1978 		    ds->ds_phys->ds_prev_snap_obj, NULL, DS_MODE_EXCLUSIVE,
1979 		    FTAG, &prev));
1980 
1981 		if (prev->ds_phys->ds_next_snap_obj != ds->ds_object) {
1982 			dsl_dataset_close(prev, DS_MODE_EXCLUSIVE, FTAG);
1983 			break;
1984 		}
1985 		if (ds != pivot_ds)
1986 			dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG);
1987 		ds = prev;
1988 	}
1989 	if (ds != pivot_ds)
1990 		dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG);
1991 
1992 	/* change pivot point's next snap */
1993 	dmu_buf_will_dirty(pivot_ds->ds_dbuf, tx);
1994 	pivot_ds->ds_phys->ds_next_snap_obj = pa->newnext_obj;
1995 
1996 	/* change clone_parent-age */
1997 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1998 	ASSERT3U(dd->dd_phys->dd_clone_parent_obj, ==, pivot_ds->ds_object);
1999 	dd->dd_phys->dd_clone_parent_obj = pdd->dd_phys->dd_clone_parent_obj;
2000 	dmu_buf_will_dirty(pdd->dd_dbuf, tx);
2001 	pdd->dd_phys->dd_clone_parent_obj = pivot_ds->ds_object;
2002 
2003 	/* change space accounting */
2004 	dsl_dir_diduse_space(pdd, -pa->used, -pa->comp, -pa->uncomp, tx);
2005 	dsl_dir_diduse_space(dd, pa->used, pa->comp, pa->uncomp, tx);
2006 	pivot_ds->ds_phys->ds_unique_bytes = pa->unique;
2007 
2008 	/* log history record */
2009 	spa_history_internal_log(LOG_DS_PROMOTE, dd->dd_pool->dp_spa, tx,
2010 	    cr, "dataset = %llu", ds->ds_object);
2011 
2012 	dsl_dir_close(pdd, FTAG);
2013 	dsl_dataset_close(pivot_ds, DS_MODE_EXCLUSIVE, FTAG);
2014 	kmem_free(name, MAXPATHLEN);
2015 }
2016 
2017 int
2018 dsl_dataset_promote(const char *name)
2019 {
2020 	dsl_dataset_t *ds;
2021 	int err;
2022 	dmu_object_info_t doi;
2023 	struct promotearg pa;
2024 
2025 	err = dsl_dataset_open(name, DS_MODE_NONE, FTAG, &ds);
2026 	if (err)
2027 		return (err);
2028 
2029 	err = dmu_object_info(ds->ds_dir->dd_pool->dp_meta_objset,
2030 	    ds->ds_phys->ds_snapnames_zapobj, &doi);
2031 	if (err) {
2032 		dsl_dataset_close(ds, DS_MODE_NONE, FTAG);
2033 		return (err);
2034 	}
2035 
2036 	/*
2037 	 * Add in 128x the snapnames zapobj size, since we will be moving
2038 	 * a bunch of snapnames to the promoted ds, and dirtying their
2039 	 * bonus buffers.
2040 	 */
2041 	err = dsl_sync_task_do(ds->ds_dir->dd_pool,
2042 	    dsl_dataset_promote_check,
2043 	    dsl_dataset_promote_sync, ds, &pa, 2 + 2 * doi.doi_physical_blks);
2044 	dsl_dataset_close(ds, DS_MODE_NONE, FTAG);
2045 	return (err);
2046 }
2047 
2048 /*
2049  * Given a pool name and a dataset object number in that pool,
2050  * return the name of that dataset.
2051  */
2052 int
2053 dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
2054 {
2055 	spa_t *spa;
2056 	dsl_pool_t *dp;
2057 	dsl_dataset_t *ds = NULL;
2058 	int error;
2059 
2060 	if ((error = spa_open(pname, &spa, FTAG)) != 0)
2061 		return (error);
2062 	dp = spa_get_dsl(spa);
2063 	rw_enter(&dp->dp_config_rwlock, RW_READER);
2064 	if ((error = dsl_dataset_open_obj(dp, obj,
2065 	    NULL, DS_MODE_NONE, FTAG, &ds)) != 0) {
2066 		rw_exit(&dp->dp_config_rwlock);
2067 		spa_close(spa, FTAG);
2068 		return (error);
2069 	}
2070 	dsl_dataset_name(ds, buf);
2071 	dsl_dataset_close(ds, DS_MODE_NONE, FTAG);
2072 	rw_exit(&dp->dp_config_rwlock);
2073 	spa_close(spa, FTAG);
2074 
2075 	return (0);
2076 }
2077