xref: /titanic_41/usr/src/uts/common/fs/zfs/dsl_dataset.c (revision 8275a87e46b79352e8c1a918b91373159c477438)
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 	/* Zero out the deadlist. */
966 	bplist_close(&ds->ds_deadlist);
967 	bplist_destroy(mos, ds->ds_phys->ds_deadlist_obj, tx);
968 	ds->ds_phys->ds_deadlist_obj =
969 	    bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx);
970 	VERIFY(0 == bplist_open(&ds->ds_deadlist, mos,
971 	    ds->ds_phys->ds_deadlist_obj));
972 
973 	{
974 		/* Free blkptrs that we gave birth to */
975 		zio_t *zio;
976 		uint64_t used = 0, compressed = 0, uncompressed = 0;
977 		struct killarg ka;
978 
979 		zio = zio_root(tx->tx_pool->dp_spa, NULL, NULL,
980 		    ZIO_FLAG_MUSTSUCCEED);
981 		ka.usedp = &used;
982 		ka.compressedp = &compressed;
983 		ka.uncompressedp = &uncompressed;
984 		ka.zio = zio;
985 		ka.tx = tx;
986 		(void) traverse_dsl_dataset(ds, ds->ds_phys->ds_prev_snap_txg,
987 		    ADVANCE_POST, kill_blkptr, &ka);
988 		(void) zio_wait(zio);
989 
990 		dsl_dir_diduse_space(ds->ds_dir,
991 		    -used, -compressed, -uncompressed, tx);
992 	}
993 
994 	/* Change our contents to that of the prev snapshot */
995 	ASSERT3U(ds->ds_prev->ds_object, ==, ds->ds_phys->ds_prev_snap_obj);
996 	ds->ds_phys->ds_bp = ds->ds_prev->ds_phys->ds_bp;
997 	ds->ds_phys->ds_used_bytes = ds->ds_prev->ds_phys->ds_used_bytes;
998 	ds->ds_phys->ds_compressed_bytes =
999 	    ds->ds_prev->ds_phys->ds_compressed_bytes;
1000 	ds->ds_phys->ds_uncompressed_bytes =
1001 	    ds->ds_prev->ds_phys->ds_uncompressed_bytes;
1002 	ds->ds_phys->ds_flags = ds->ds_prev->ds_phys->ds_flags;
1003 	ds->ds_phys->ds_unique_bytes = 0;
1004 
1005 	if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) {
1006 		dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1007 		ds->ds_prev->ds_phys->ds_unique_bytes = 0;
1008 	}
1009 
1010 	spa_history_internal_log(LOG_DS_ROLLBACK, ds->ds_dir->dd_pool->dp_spa,
1011 	    tx, cr, "dataset = %llu", ds->ds_object);
1012 }
1013 
1014 /* ARGSUSED */
1015 static int
1016 dsl_dataset_destroy_begin_check(void *arg1, void *arg2, dmu_tx_t *tx)
1017 {
1018 	dsl_dataset_t *ds = arg1;
1019 
1020 	/*
1021 	 * Can't delete a head dataset if there are snapshots of it.
1022 	 * (Except if the only snapshots are from the branch we cloned
1023 	 * from.)
1024 	 */
1025 	if (ds->ds_prev != NULL &&
1026 	    ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object)
1027 		return (EINVAL);
1028 
1029 	return (0);
1030 }
1031 
1032 /* ARGSUSED */
1033 static void
1034 dsl_dataset_destroy_begin_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
1035 {
1036 	dsl_dataset_t *ds = arg1;
1037 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1038 
1039 	/* Mark it as inconsistent on-disk, in case we crash */
1040 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1041 	ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT;
1042 
1043 	spa_history_internal_log(LOG_DS_DESTROY_BEGIN, dp->dp_spa, tx,
1044 	    cr, "dataset = %llu", ds->ds_object);
1045 }
1046 
1047 /* ARGSUSED */
1048 static int
1049 dsl_dataset_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx)
1050 {
1051 	dsl_dataset_t *ds = arg1;
1052 
1053 	/* Can't delete a branch point. */
1054 	if (ds->ds_phys->ds_num_children > 1)
1055 		return (EEXIST);
1056 
1057 	/*
1058 	 * Can't delete a head dataset if there are snapshots of it.
1059 	 * (Except if the only snapshots are from the branch we cloned
1060 	 * from.)
1061 	 */
1062 	if (ds->ds_prev != NULL &&
1063 	    ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object)
1064 		return (EINVAL);
1065 
1066 	/*
1067 	 * If we made changes this txg, traverse_dsl_dataset won't find
1068 	 * them.  Try again.
1069 	 */
1070 	if (ds->ds_phys->ds_bp.blk_birth >= tx->tx_txg)
1071 		return (EAGAIN);
1072 
1073 	/* XXX we should do some i/o error checking... */
1074 	return (0);
1075 }
1076 
1077 static void
1078 dsl_dataset_destroy_sync(void *arg1, void *tag, cred_t *cr, dmu_tx_t *tx)
1079 {
1080 	dsl_dataset_t *ds = arg1;
1081 	uint64_t used = 0, compressed = 0, uncompressed = 0;
1082 	zio_t *zio;
1083 	int err;
1084 	int after_branch_point = FALSE;
1085 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1086 	objset_t *mos = dp->dp_meta_objset;
1087 	dsl_dataset_t *ds_prev = NULL;
1088 	uint64_t obj;
1089 
1090 	ASSERT3U(ds->ds_open_refcount, ==, DS_REF_MAX);
1091 	ASSERT3U(ds->ds_phys->ds_num_children, <=, 1);
1092 	ASSERT(ds->ds_prev == NULL ||
1093 	    ds->ds_prev->ds_phys->ds_next_snap_obj != ds->ds_object);
1094 	ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg);
1095 
1096 	ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock));
1097 
1098 	obj = ds->ds_object;
1099 
1100 	if (ds->ds_phys->ds_prev_snap_obj != 0) {
1101 		if (ds->ds_prev) {
1102 			ds_prev = ds->ds_prev;
1103 		} else {
1104 			VERIFY(0 == dsl_dataset_open_obj(dp,
1105 			    ds->ds_phys->ds_prev_snap_obj, NULL,
1106 			    DS_MODE_NONE, FTAG, &ds_prev));
1107 		}
1108 		after_branch_point =
1109 		    (ds_prev->ds_phys->ds_next_snap_obj != obj);
1110 
1111 		dmu_buf_will_dirty(ds_prev->ds_dbuf, tx);
1112 		if (after_branch_point &&
1113 		    ds->ds_phys->ds_next_snap_obj == 0) {
1114 			/* This clone is toast. */
1115 			ASSERT(ds_prev->ds_phys->ds_num_children > 1);
1116 			ds_prev->ds_phys->ds_num_children--;
1117 		} else if (!after_branch_point) {
1118 			ds_prev->ds_phys->ds_next_snap_obj =
1119 			    ds->ds_phys->ds_next_snap_obj;
1120 		}
1121 	}
1122 
1123 	zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
1124 
1125 	if (ds->ds_phys->ds_next_snap_obj != 0) {
1126 		blkptr_t bp;
1127 		dsl_dataset_t *ds_next;
1128 		uint64_t itor = 0;
1129 
1130 		spa_scrub_restart(dp->dp_spa, tx->tx_txg);
1131 
1132 		VERIFY(0 == dsl_dataset_open_obj(dp,
1133 		    ds->ds_phys->ds_next_snap_obj, NULL,
1134 		    DS_MODE_NONE, FTAG, &ds_next));
1135 		ASSERT3U(ds_next->ds_phys->ds_prev_snap_obj, ==, obj);
1136 
1137 		dmu_buf_will_dirty(ds_next->ds_dbuf, tx);
1138 		ds_next->ds_phys->ds_prev_snap_obj =
1139 		    ds->ds_phys->ds_prev_snap_obj;
1140 		ds_next->ds_phys->ds_prev_snap_txg =
1141 		    ds->ds_phys->ds_prev_snap_txg;
1142 		ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
1143 		    ds_prev ? ds_prev->ds_phys->ds_creation_txg : 0);
1144 
1145 		/*
1146 		 * Transfer to our deadlist (which will become next's
1147 		 * new deadlist) any entries from next's current
1148 		 * deadlist which were born before prev, and free the
1149 		 * other entries.
1150 		 *
1151 		 * XXX we're doing this long task with the config lock held
1152 		 */
1153 		while (bplist_iterate(&ds_next->ds_deadlist, &itor,
1154 		    &bp) == 0) {
1155 			if (bp.blk_birth <= ds->ds_phys->ds_prev_snap_txg) {
1156 				VERIFY(0 == bplist_enqueue(&ds->ds_deadlist,
1157 				    &bp, tx));
1158 				if (ds_prev && !after_branch_point &&
1159 				    bp.blk_birth >
1160 				    ds_prev->ds_phys->ds_prev_snap_txg) {
1161 					ds_prev->ds_phys->ds_unique_bytes +=
1162 					    bp_get_dasize(dp->dp_spa, &bp);
1163 				}
1164 			} else {
1165 				used += bp_get_dasize(dp->dp_spa, &bp);
1166 				compressed += BP_GET_PSIZE(&bp);
1167 				uncompressed += BP_GET_UCSIZE(&bp);
1168 				/* XXX check return value? */
1169 				(void) arc_free(zio, dp->dp_spa, tx->tx_txg,
1170 				    &bp, NULL, NULL, ARC_NOWAIT);
1171 			}
1172 		}
1173 
1174 		/* free next's deadlist */
1175 		bplist_close(&ds_next->ds_deadlist);
1176 		bplist_destroy(mos, ds_next->ds_phys->ds_deadlist_obj, tx);
1177 
1178 		/* set next's deadlist to our deadlist */
1179 		ds_next->ds_phys->ds_deadlist_obj =
1180 		    ds->ds_phys->ds_deadlist_obj;
1181 		VERIFY(0 == bplist_open(&ds_next->ds_deadlist, mos,
1182 		    ds_next->ds_phys->ds_deadlist_obj));
1183 		ds->ds_phys->ds_deadlist_obj = 0;
1184 
1185 		if (ds_next->ds_phys->ds_next_snap_obj != 0) {
1186 			/*
1187 			 * Update next's unique to include blocks which
1188 			 * were previously shared by only this snapshot
1189 			 * and it.  Those blocks will be born after the
1190 			 * prev snap and before this snap, and will have
1191 			 * died after the next snap and before the one
1192 			 * after that (ie. be on the snap after next's
1193 			 * deadlist).
1194 			 *
1195 			 * XXX we're doing this long task with the
1196 			 * config lock held
1197 			 */
1198 			dsl_dataset_t *ds_after_next;
1199 
1200 			VERIFY(0 == dsl_dataset_open_obj(dp,
1201 			    ds_next->ds_phys->ds_next_snap_obj, NULL,
1202 			    DS_MODE_NONE, FTAG, &ds_after_next));
1203 			itor = 0;
1204 			while (bplist_iterate(&ds_after_next->ds_deadlist,
1205 			    &itor, &bp) == 0) {
1206 				if (bp.blk_birth >
1207 				    ds->ds_phys->ds_prev_snap_txg &&
1208 				    bp.blk_birth <=
1209 				    ds->ds_phys->ds_creation_txg) {
1210 					ds_next->ds_phys->ds_unique_bytes +=
1211 					    bp_get_dasize(dp->dp_spa, &bp);
1212 				}
1213 			}
1214 
1215 			dsl_dataset_close(ds_after_next, DS_MODE_NONE, FTAG);
1216 			ASSERT3P(ds_next->ds_prev, ==, NULL);
1217 		} else {
1218 			/*
1219 			 * It would be nice to update the head dataset's
1220 			 * unique.  To do so we would have to traverse
1221 			 * it for blocks born after ds_prev, which is
1222 			 * pretty expensive just to maintain something
1223 			 * for debugging purposes.
1224 			 */
1225 			ASSERT3P(ds_next->ds_prev, ==, ds);
1226 			dsl_dataset_close(ds_next->ds_prev, DS_MODE_NONE,
1227 			    ds_next);
1228 			if (ds_prev) {
1229 				VERIFY(0 == dsl_dataset_open_obj(dp,
1230 				    ds->ds_phys->ds_prev_snap_obj, NULL,
1231 				    DS_MODE_NONE, ds_next, &ds_next->ds_prev));
1232 			} else {
1233 				ds_next->ds_prev = NULL;
1234 			}
1235 		}
1236 		dsl_dataset_close(ds_next, DS_MODE_NONE, FTAG);
1237 
1238 		/*
1239 		 * NB: unique_bytes is not accurate for head objsets
1240 		 * because we don't update it when we delete the most
1241 		 * recent snapshot -- see above comment.
1242 		 */
1243 		ASSERT3U(used, ==, ds->ds_phys->ds_unique_bytes);
1244 	} else {
1245 		/*
1246 		 * There's no next snapshot, so this is a head dataset.
1247 		 * Destroy the deadlist.  Unless it's a clone, the
1248 		 * deadlist should be empty.  (If it's a clone, it's
1249 		 * safe to ignore the deadlist contents.)
1250 		 */
1251 		struct killarg ka;
1252 
1253 		ASSERT(after_branch_point || bplist_empty(&ds->ds_deadlist));
1254 		bplist_close(&ds->ds_deadlist);
1255 		bplist_destroy(mos, ds->ds_phys->ds_deadlist_obj, tx);
1256 		ds->ds_phys->ds_deadlist_obj = 0;
1257 
1258 		/*
1259 		 * Free everything that we point to (that's born after
1260 		 * the previous snapshot, if we are a clone)
1261 		 *
1262 		 * XXX we're doing this long task with the config lock held
1263 		 */
1264 		ka.usedp = &used;
1265 		ka.compressedp = &compressed;
1266 		ka.uncompressedp = &uncompressed;
1267 		ka.zio = zio;
1268 		ka.tx = tx;
1269 		err = traverse_dsl_dataset(ds, ds->ds_phys->ds_prev_snap_txg,
1270 		    ADVANCE_POST, kill_blkptr, &ka);
1271 		ASSERT3U(err, ==, 0);
1272 	}
1273 
1274 	err = zio_wait(zio);
1275 	ASSERT3U(err, ==, 0);
1276 
1277 	dsl_dir_diduse_space(ds->ds_dir, -used, -compressed, -uncompressed, tx);
1278 
1279 	if (ds->ds_phys->ds_snapnames_zapobj) {
1280 		err = zap_destroy(mos, ds->ds_phys->ds_snapnames_zapobj, tx);
1281 		ASSERT(err == 0);
1282 	}
1283 
1284 	if (ds->ds_dir->dd_phys->dd_head_dataset_obj == ds->ds_object) {
1285 		/* Erase the link in the dataset */
1286 		dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
1287 		ds->ds_dir->dd_phys->dd_head_dataset_obj = 0;
1288 		/*
1289 		 * dsl_dir_sync_destroy() called us, they'll destroy
1290 		 * the dataset.
1291 		 */
1292 	} else {
1293 		/* remove from snapshot namespace */
1294 		dsl_dataset_t *ds_head;
1295 		VERIFY(0 == dsl_dataset_open_obj(dp,
1296 		    ds->ds_dir->dd_phys->dd_head_dataset_obj, NULL,
1297 		    DS_MODE_NONE, FTAG, &ds_head));
1298 		VERIFY(0 == dsl_dataset_get_snapname(ds));
1299 #ifdef ZFS_DEBUG
1300 		{
1301 			uint64_t val;
1302 			err = zap_lookup(mos,
1303 			    ds_head->ds_phys->ds_snapnames_zapobj,
1304 			    ds->ds_snapname, 8, 1, &val);
1305 			ASSERT3U(err, ==, 0);
1306 			ASSERT3U(val, ==, obj);
1307 		}
1308 #endif
1309 		err = zap_remove(mos, ds_head->ds_phys->ds_snapnames_zapobj,
1310 		    ds->ds_snapname, tx);
1311 		ASSERT(err == 0);
1312 		dsl_dataset_close(ds_head, DS_MODE_NONE, FTAG);
1313 	}
1314 
1315 	if (ds_prev && ds->ds_prev != ds_prev)
1316 		dsl_dataset_close(ds_prev, DS_MODE_NONE, FTAG);
1317 
1318 	spa_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
1319 	spa_history_internal_log(LOG_DS_DESTROY, dp->dp_spa, tx,
1320 	    cr, "dataset = %llu", ds->ds_object);
1321 
1322 	dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, tag);
1323 	VERIFY(0 == dmu_object_free(mos, obj, tx));
1324 
1325 }
1326 
1327 /* ARGSUSED */
1328 int
1329 dsl_dataset_snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx)
1330 {
1331 	objset_t *os = arg1;
1332 	dsl_dataset_t *ds = os->os->os_dsl_dataset;
1333 	const char *snapname = arg2;
1334 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
1335 	int err;
1336 	uint64_t value;
1337 
1338 	/*
1339 	 * We don't allow multiple snapshots of the same txg.  If there
1340 	 * is already one, try again.
1341 	 */
1342 	if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg)
1343 		return (EAGAIN);
1344 
1345 	/*
1346 	 * Check for conflicting name snapshot name.
1347 	 */
1348 	err = zap_lookup(mos, ds->ds_phys->ds_snapnames_zapobj,
1349 	    snapname, 8, 1, &value);
1350 	if (err == 0)
1351 		return (EEXIST);
1352 	if (err != ENOENT)
1353 		return (err);
1354 
1355 	/*
1356 	 * Check that the dataset's name is not too long.  Name consists
1357 	 * of the dataset's length + 1 for the @-sign + snapshot name's length
1358 	 */
1359 	if (dsl_dataset_namelen(ds) + 1 + strlen(snapname) >= MAXNAMELEN)
1360 		return (ENAMETOOLONG);
1361 
1362 	ds->ds_trysnap_txg = tx->tx_txg;
1363 	return (0);
1364 }
1365 
1366 void
1367 dsl_dataset_snapshot_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
1368 {
1369 	objset_t *os = arg1;
1370 	dsl_dataset_t *ds = os->os->os_dsl_dataset;
1371 	const char *snapname = arg2;
1372 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1373 	dmu_buf_t *dbuf;
1374 	dsl_dataset_phys_t *dsphys;
1375 	uint64_t dsobj;
1376 	objset_t *mos = dp->dp_meta_objset;
1377 	int err;
1378 
1379 	spa_scrub_restart(dp->dp_spa, tx->tx_txg);
1380 	ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock));
1381 
1382 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
1383 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
1384 	VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
1385 	dmu_buf_will_dirty(dbuf, tx);
1386 	dsphys = dbuf->db_data;
1387 	dsphys->ds_dir_obj = ds->ds_dir->dd_object;
1388 	dsphys->ds_fsid_guid = unique_create();
1389 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
1390 	    sizeof (dsphys->ds_guid));
1391 	dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj;
1392 	dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg;
1393 	dsphys->ds_next_snap_obj = ds->ds_object;
1394 	dsphys->ds_num_children = 1;
1395 	dsphys->ds_creation_time = gethrestime_sec();
1396 	dsphys->ds_creation_txg = tx->tx_txg;
1397 	dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj;
1398 	dsphys->ds_used_bytes = ds->ds_phys->ds_used_bytes;
1399 	dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes;
1400 	dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes;
1401 	dsphys->ds_flags = ds->ds_phys->ds_flags;
1402 	dsphys->ds_bp = ds->ds_phys->ds_bp;
1403 	dmu_buf_rele(dbuf, FTAG);
1404 
1405 	ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0);
1406 	if (ds->ds_prev) {
1407 		ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj ==
1408 		    ds->ds_object ||
1409 		    ds->ds_prev->ds_phys->ds_num_children > 1);
1410 		if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) {
1411 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1412 			ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
1413 			    ds->ds_prev->ds_phys->ds_creation_txg);
1414 			ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj;
1415 		}
1416 	}
1417 
1418 	bplist_close(&ds->ds_deadlist);
1419 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1420 	ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, dsphys->ds_creation_txg);
1421 	ds->ds_phys->ds_prev_snap_obj = dsobj;
1422 	ds->ds_phys->ds_prev_snap_txg = dsphys->ds_creation_txg;
1423 	ds->ds_phys->ds_unique_bytes = 0;
1424 	ds->ds_phys->ds_deadlist_obj =
1425 	    bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx);
1426 	VERIFY(0 == bplist_open(&ds->ds_deadlist, mos,
1427 	    ds->ds_phys->ds_deadlist_obj));
1428 
1429 	dprintf("snap '%s' -> obj %llu\n", snapname, dsobj);
1430 	err = zap_add(mos, ds->ds_phys->ds_snapnames_zapobj,
1431 	    snapname, 8, 1, &dsobj, tx);
1432 	ASSERT(err == 0);
1433 
1434 	if (ds->ds_prev)
1435 		dsl_dataset_close(ds->ds_prev, DS_MODE_NONE, ds);
1436 	VERIFY(0 == dsl_dataset_open_obj(dp,
1437 	    ds->ds_phys->ds_prev_snap_obj, snapname,
1438 	    DS_MODE_NONE, ds, &ds->ds_prev));
1439 
1440 	spa_history_internal_log(LOG_DS_SNAPSHOT, dp->dp_spa, tx, cr,
1441 	    "dataset = %llu", dsobj);
1442 }
1443 
1444 void
1445 dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
1446 {
1447 	ASSERT(dmu_tx_is_syncing(tx));
1448 	ASSERT(ds->ds_user_ptr != NULL);
1449 	ASSERT(ds->ds_phys->ds_next_snap_obj == 0);
1450 
1451 	/*
1452 	 * in case we had to change ds_fsid_guid when we opened it,
1453 	 * sync it out now.
1454 	 */
1455 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1456 	ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid;
1457 
1458 	dsl_dir_dirty(ds->ds_dir, tx);
1459 	dmu_objset_sync(ds->ds_user_ptr, zio, tx);
1460 }
1461 
1462 void
1463 dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
1464 {
1465 	dsl_dir_stats(ds->ds_dir, nv);
1466 
1467 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
1468 	    ds->ds_phys->ds_creation_time);
1469 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
1470 	    ds->ds_phys->ds_creation_txg);
1471 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED,
1472 	    ds->ds_phys->ds_used_bytes);
1473 
1474 	if (ds->ds_phys->ds_next_snap_obj) {
1475 		/*
1476 		 * This is a snapshot; override the dd's space used with
1477 		 * our unique space and compression ratio.
1478 		 */
1479 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
1480 		    ds->ds_phys->ds_unique_bytes);
1481 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
1482 		    ds->ds_phys->ds_compressed_bytes == 0 ? 100 :
1483 		    (ds->ds_phys->ds_uncompressed_bytes * 100 /
1484 		    ds->ds_phys->ds_compressed_bytes));
1485 	}
1486 }
1487 
1488 void
1489 dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
1490 {
1491 	stat->dds_creation_txg = ds->ds_phys->ds_creation_txg;
1492 	stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT;
1493 	if (ds->ds_phys->ds_next_snap_obj) {
1494 		stat->dds_is_snapshot = B_TRUE;
1495 		stat->dds_num_clones = ds->ds_phys->ds_num_children - 1;
1496 	}
1497 
1498 	/* clone origin is really a dsl_dir thing... */
1499 	if (ds->ds_dir->dd_phys->dd_clone_parent_obj) {
1500 		dsl_dataset_t *ods;
1501 
1502 		rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER);
1503 		VERIFY(0 == dsl_dataset_open_obj(ds->ds_dir->dd_pool,
1504 		    ds->ds_dir->dd_phys->dd_clone_parent_obj,
1505 		    NULL, DS_MODE_NONE, FTAG, &ods));
1506 		dsl_dataset_name(ods, stat->dds_clone_of);
1507 		dsl_dataset_close(ods, DS_MODE_NONE, FTAG);
1508 		rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
1509 	}
1510 }
1511 
1512 uint64_t
1513 dsl_dataset_fsid_guid(dsl_dataset_t *ds)
1514 {
1515 	return (ds->ds_fsid_guid);
1516 }
1517 
1518 void
1519 dsl_dataset_space(dsl_dataset_t *ds,
1520     uint64_t *refdbytesp, uint64_t *availbytesp,
1521     uint64_t *usedobjsp, uint64_t *availobjsp)
1522 {
1523 	*refdbytesp = ds->ds_phys->ds_used_bytes;
1524 	*availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
1525 	*usedobjsp = ds->ds_phys->ds_bp.blk_fill;
1526 	*availobjsp = DN_MAX_OBJECT - *usedobjsp;
1527 }
1528 
1529 /* ARGSUSED */
1530 static int
1531 dsl_dataset_snapshot_rename_check(void *arg1, void *arg2, dmu_tx_t *tx)
1532 {
1533 	dsl_dataset_t *ds = arg1;
1534 	char *newsnapname = arg2;
1535 	dsl_dir_t *dd = ds->ds_dir;
1536 	objset_t *mos = dd->dd_pool->dp_meta_objset;
1537 	dsl_dataset_t *hds;
1538 	uint64_t val;
1539 	int err;
1540 
1541 	err = dsl_dataset_open_obj(dd->dd_pool,
1542 	    dd->dd_phys->dd_head_dataset_obj, NULL, DS_MODE_NONE, FTAG, &hds);
1543 	if (err)
1544 		return (err);
1545 
1546 	/* new name better not be in use */
1547 	err = zap_lookup(mos, hds->ds_phys->ds_snapnames_zapobj,
1548 	    newsnapname, 8, 1, &val);
1549 	dsl_dataset_close(hds, DS_MODE_NONE, FTAG);
1550 
1551 	if (err == 0)
1552 		err = EEXIST;
1553 	else if (err == ENOENT)
1554 		err = 0;
1555 
1556 	/* dataset name + 1 for the "@" + the new snapshot name must fit */
1557 	if (dsl_dir_namelen(ds->ds_dir) + 1 + strlen(newsnapname) >= MAXNAMELEN)
1558 		err = ENAMETOOLONG;
1559 
1560 	return (err);
1561 }
1562 
1563 static void
1564 dsl_dataset_snapshot_rename_sync(void *arg1, void *arg2,
1565     cred_t *cr, dmu_tx_t *tx)
1566 {
1567 	dsl_dataset_t *ds = arg1;
1568 	const char *newsnapname = arg2;
1569 	dsl_dir_t *dd = ds->ds_dir;
1570 	objset_t *mos = dd->dd_pool->dp_meta_objset;
1571 	dsl_dataset_t *hds;
1572 	int err;
1573 
1574 	ASSERT(ds->ds_phys->ds_next_snap_obj != 0);
1575 
1576 	VERIFY(0 == dsl_dataset_open_obj(dd->dd_pool,
1577 	    dd->dd_phys->dd_head_dataset_obj, NULL, DS_MODE_NONE, FTAG, &hds));
1578 
1579 	VERIFY(0 == dsl_dataset_get_snapname(ds));
1580 	err = zap_remove(mos, hds->ds_phys->ds_snapnames_zapobj,
1581 	    ds->ds_snapname, tx);
1582 	ASSERT3U(err, ==, 0);
1583 	mutex_enter(&ds->ds_lock);
1584 	(void) strcpy(ds->ds_snapname, newsnapname);
1585 	mutex_exit(&ds->ds_lock);
1586 	err = zap_add(mos, hds->ds_phys->ds_snapnames_zapobj,
1587 	    ds->ds_snapname, 8, 1, &ds->ds_object, tx);
1588 	ASSERT3U(err, ==, 0);
1589 
1590 	spa_history_internal_log(LOG_DS_RENAME, dd->dd_pool->dp_spa, tx,
1591 	    cr, "dataset = %llu", ds->ds_object);
1592 	dsl_dataset_close(hds, DS_MODE_NONE, FTAG);
1593 }
1594 
1595 struct renamearg {
1596 	dsl_sync_task_group_t *dstg;
1597 	char failed[MAXPATHLEN];
1598 	char *oldsnap;
1599 	char *newsnap;
1600 };
1601 
1602 static int
1603 dsl_snapshot_rename_one(char *name, void *arg)
1604 {
1605 	struct renamearg *ra = arg;
1606 	dsl_dataset_t *ds = NULL;
1607 	char *cp;
1608 	int err;
1609 
1610 	cp = name + strlen(name);
1611 	*cp = '@';
1612 	(void) strcpy(cp + 1, ra->oldsnap);
1613 
1614 	/*
1615 	 * For recursive snapshot renames the parent won't be changing
1616 	 * so we just pass name for both the to/from argument.
1617 	 */
1618 	if (err = zfs_secpolicy_rename_perms(name, name, CRED())) {
1619 		(void) strcpy(ra->failed, name);
1620 		return (err);
1621 	}
1622 
1623 	err = dsl_dataset_open(name, DS_MODE_READONLY | DS_MODE_STANDARD,
1624 	    ra->dstg, &ds);
1625 	if (err == ENOENT) {
1626 		*cp = '\0';
1627 		return (0);
1628 	}
1629 	if (err) {
1630 		(void) strcpy(ra->failed, name);
1631 		*cp = '\0';
1632 		dsl_dataset_close(ds, DS_MODE_STANDARD, ra->dstg);
1633 		return (err);
1634 	}
1635 
1636 #ifdef _KERNEL
1637 	/* for all filesystems undergoing rename, we'll need to unmount it */
1638 	(void) zfs_unmount_snap(name, NULL);
1639 #endif
1640 
1641 	*cp = '\0';
1642 
1643 	dsl_sync_task_create(ra->dstg, dsl_dataset_snapshot_rename_check,
1644 	    dsl_dataset_snapshot_rename_sync, ds, ra->newsnap, 0);
1645 
1646 	return (0);
1647 }
1648 
1649 static int
1650 dsl_recursive_rename(char *oldname, const char *newname)
1651 {
1652 	int err;
1653 	struct renamearg *ra;
1654 	dsl_sync_task_t *dst;
1655 	spa_t *spa;
1656 	char *cp, *fsname = spa_strdup(oldname);
1657 	int len = strlen(oldname);
1658 
1659 	/* truncate the snapshot name to get the fsname */
1660 	cp = strchr(fsname, '@');
1661 	*cp = '\0';
1662 
1663 	err = spa_open(fsname, &spa, FTAG);
1664 	if (err) {
1665 		kmem_free(fsname, len + 1);
1666 		return (err);
1667 	}
1668 	ra = kmem_alloc(sizeof (struct renamearg), KM_SLEEP);
1669 	ra->dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
1670 
1671 	ra->oldsnap = strchr(oldname, '@') + 1;
1672 	ra->newsnap = strchr(newname, '@') + 1;
1673 	*ra->failed = '\0';
1674 
1675 	err = dmu_objset_find(fsname, dsl_snapshot_rename_one, ra,
1676 	    DS_FIND_CHILDREN);
1677 	kmem_free(fsname, len + 1);
1678 
1679 	if (err == 0) {
1680 		err = dsl_sync_task_group_wait(ra->dstg);
1681 	}
1682 
1683 	for (dst = list_head(&ra->dstg->dstg_tasks); dst;
1684 	    dst = list_next(&ra->dstg->dstg_tasks, dst)) {
1685 		dsl_dataset_t *ds = dst->dst_arg1;
1686 		if (dst->dst_err) {
1687 			dsl_dir_name(ds->ds_dir, ra->failed);
1688 			(void) strcat(ra->failed, "@");
1689 			(void) strcat(ra->failed, ra->newsnap);
1690 		}
1691 		dsl_dataset_close(ds, DS_MODE_STANDARD, ra->dstg);
1692 	}
1693 
1694 	if (err)
1695 		(void) strcpy(oldname, ra->failed);
1696 
1697 	dsl_sync_task_group_destroy(ra->dstg);
1698 	kmem_free(ra, sizeof (struct renamearg));
1699 	spa_close(spa, FTAG);
1700 	return (err);
1701 }
1702 
1703 static int
1704 dsl_valid_rename(char *oldname, void *arg)
1705 {
1706 	int delta = *(int *)arg;
1707 
1708 	if (strlen(oldname) + delta >= MAXNAMELEN)
1709 		return (ENAMETOOLONG);
1710 
1711 	return (0);
1712 }
1713 
1714 #pragma weak dmu_objset_rename = dsl_dataset_rename
1715 int
1716 dsl_dataset_rename(char *oldname, const char *newname,
1717     boolean_t recursive)
1718 {
1719 	dsl_dir_t *dd;
1720 	dsl_dataset_t *ds;
1721 	const char *tail;
1722 	int err;
1723 
1724 	err = dsl_dir_open(oldname, FTAG, &dd, &tail);
1725 	if (err)
1726 		return (err);
1727 	if (tail == NULL) {
1728 		int delta = strlen(newname) - strlen(oldname);
1729 
1730 		/* if we're growing, validate child size lengths */
1731 		if (delta > 0)
1732 			err = dmu_objset_find(oldname, dsl_valid_rename,
1733 			    &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
1734 
1735 		if (!err)
1736 			err = dsl_dir_rename(dd, newname);
1737 		dsl_dir_close(dd, FTAG);
1738 		return (err);
1739 	}
1740 	if (tail[0] != '@') {
1741 		/* the name ended in a nonexistant component */
1742 		dsl_dir_close(dd, FTAG);
1743 		return (ENOENT);
1744 	}
1745 
1746 	dsl_dir_close(dd, FTAG);
1747 
1748 	/* new name must be snapshot in same filesystem */
1749 	tail = strchr(newname, '@');
1750 	if (tail == NULL)
1751 		return (EINVAL);
1752 	tail++;
1753 	if (strncmp(oldname, newname, tail - newname) != 0)
1754 		return (EXDEV);
1755 
1756 	if (recursive) {
1757 		err = dsl_recursive_rename(oldname, newname);
1758 	} else {
1759 		err = dsl_dataset_open(oldname,
1760 		    DS_MODE_READONLY | DS_MODE_STANDARD, FTAG, &ds);
1761 		if (err)
1762 			return (err);
1763 
1764 		err = dsl_sync_task_do(ds->ds_dir->dd_pool,
1765 		    dsl_dataset_snapshot_rename_check,
1766 		    dsl_dataset_snapshot_rename_sync, ds, (char *)tail, 1);
1767 
1768 		dsl_dataset_close(ds, DS_MODE_STANDARD, FTAG);
1769 	}
1770 
1771 	return (err);
1772 }
1773 
1774 struct promotearg {
1775 	uint64_t used, comp, uncomp, unique;
1776 	uint64_t newnext_obj, snapnames_obj;
1777 };
1778 
1779 /* ARGSUSED */
1780 static int
1781 dsl_dataset_promote_check(void *arg1, void *arg2, dmu_tx_t *tx)
1782 {
1783 	dsl_dataset_t *hds = arg1;
1784 	struct promotearg *pa = arg2;
1785 	dsl_dir_t *dd = hds->ds_dir;
1786 	dsl_pool_t *dp = hds->ds_dir->dd_pool;
1787 	dsl_dir_t *pdd = NULL;
1788 	dsl_dataset_t *ds = NULL;
1789 	dsl_dataset_t *pivot_ds = NULL;
1790 	dsl_dataset_t *newnext_ds = NULL;
1791 	int err;
1792 	char *name = NULL;
1793 	uint64_t itor = 0;
1794 	blkptr_t bp;
1795 
1796 	bzero(pa, sizeof (*pa));
1797 
1798 	/* Check that it is a clone */
1799 	if (dd->dd_phys->dd_clone_parent_obj == 0)
1800 		return (EINVAL);
1801 
1802 	/* Since this is so expensive, don't do the preliminary check */
1803 	if (!dmu_tx_is_syncing(tx))
1804 		return (0);
1805 
1806 	if (err = dsl_dataset_open_obj(dp,
1807 	    dd->dd_phys->dd_clone_parent_obj,
1808 	    NULL, DS_MODE_EXCLUSIVE, FTAG, &pivot_ds))
1809 		goto out;
1810 	pdd = pivot_ds->ds_dir;
1811 
1812 	{
1813 		dsl_dataset_t *phds;
1814 		if (err = dsl_dataset_open_obj(dd->dd_pool,
1815 		    pdd->dd_phys->dd_head_dataset_obj,
1816 		    NULL, DS_MODE_NONE, FTAG, &phds))
1817 			goto out;
1818 		pa->snapnames_obj = phds->ds_phys->ds_snapnames_zapobj;
1819 		dsl_dataset_close(phds, DS_MODE_NONE, FTAG);
1820 	}
1821 
1822 	if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE) {
1823 		err = EXDEV;
1824 		goto out;
1825 	}
1826 
1827 	/* find pivot point's new next ds */
1828 	VERIFY(0 == dsl_dataset_open_obj(dd->dd_pool, hds->ds_object,
1829 	    NULL, DS_MODE_NONE, FTAG, &newnext_ds));
1830 	while (newnext_ds->ds_phys->ds_prev_snap_obj != pivot_ds->ds_object) {
1831 		dsl_dataset_t *prev;
1832 
1833 		if (err = dsl_dataset_open_obj(dd->dd_pool,
1834 		    newnext_ds->ds_phys->ds_prev_snap_obj,
1835 		    NULL, DS_MODE_NONE, FTAG, &prev))
1836 			goto out;
1837 		dsl_dataset_close(newnext_ds, DS_MODE_NONE, FTAG);
1838 		newnext_ds = prev;
1839 	}
1840 	pa->newnext_obj = newnext_ds->ds_object;
1841 
1842 	/* compute pivot point's new unique space */
1843 	while ((err = bplist_iterate(&newnext_ds->ds_deadlist,
1844 	    &itor, &bp)) == 0) {
1845 		if (bp.blk_birth > pivot_ds->ds_phys->ds_prev_snap_txg)
1846 			pa->unique += bp_get_dasize(dd->dd_pool->dp_spa, &bp);
1847 	}
1848 	if (err != ENOENT)
1849 		goto out;
1850 
1851 	/* Walk the snapshots that we are moving */
1852 	name = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1853 	ds = pivot_ds;
1854 	/* CONSTCOND */
1855 	while (TRUE) {
1856 		uint64_t val, dlused, dlcomp, dluncomp;
1857 		dsl_dataset_t *prev;
1858 
1859 		/* Check that the snapshot name does not conflict */
1860 		dsl_dataset_name(ds, name);
1861 		err = zap_lookup(dd->dd_pool->dp_meta_objset,
1862 		    hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname,
1863 		    8, 1, &val);
1864 		if (err != ENOENT) {
1865 			if (err == 0)
1866 				err = EEXIST;
1867 			goto out;
1868 		}
1869 
1870 		/*
1871 		 * compute space to transfer.  Each snapshot gave birth to:
1872 		 * (my used) - (prev's used) + (deadlist's used)
1873 		 */
1874 		pa->used += ds->ds_phys->ds_used_bytes;
1875 		pa->comp += ds->ds_phys->ds_compressed_bytes;
1876 		pa->uncomp += ds->ds_phys->ds_uncompressed_bytes;
1877 
1878 		/* If we reach the first snapshot, we're done. */
1879 		if (ds->ds_phys->ds_prev_snap_obj == 0)
1880 			break;
1881 
1882 		if (err = bplist_space(&ds->ds_deadlist,
1883 		    &dlused, &dlcomp, &dluncomp))
1884 			goto out;
1885 		if (err = dsl_dataset_open_obj(dd->dd_pool,
1886 		    ds->ds_phys->ds_prev_snap_obj, NULL, DS_MODE_EXCLUSIVE,
1887 		    FTAG, &prev))
1888 			goto out;
1889 		pa->used += dlused - prev->ds_phys->ds_used_bytes;
1890 		pa->comp += dlcomp - prev->ds_phys->ds_compressed_bytes;
1891 		pa->uncomp += dluncomp - prev->ds_phys->ds_uncompressed_bytes;
1892 
1893 		/*
1894 		 * We could be a clone of a clone.  If we reach our
1895 		 * parent's branch point, we're done.
1896 		 */
1897 		if (prev->ds_phys->ds_next_snap_obj != ds->ds_object) {
1898 			dsl_dataset_close(prev, DS_MODE_EXCLUSIVE, FTAG);
1899 			break;
1900 		}
1901 		if (ds != pivot_ds)
1902 			dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG);
1903 		ds = prev;
1904 	}
1905 
1906 	/* Check that there is enough space here */
1907 	err = dsl_dir_transfer_possible(pdd, dd, pa->used);
1908 
1909 out:
1910 	if (ds && ds != pivot_ds)
1911 		dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG);
1912 	if (pivot_ds)
1913 		dsl_dataset_close(pivot_ds, DS_MODE_EXCLUSIVE, FTAG);
1914 	if (newnext_ds)
1915 		dsl_dataset_close(newnext_ds, DS_MODE_NONE, FTAG);
1916 	if (name)
1917 		kmem_free(name, MAXPATHLEN);
1918 	return (err);
1919 }
1920 
1921 static void
1922 dsl_dataset_promote_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
1923 {
1924 	dsl_dataset_t *hds = arg1;
1925 	struct promotearg *pa = arg2;
1926 	dsl_dir_t *dd = hds->ds_dir;
1927 	dsl_pool_t *dp = hds->ds_dir->dd_pool;
1928 	dsl_dir_t *pdd = NULL;
1929 	dsl_dataset_t *ds, *pivot_ds;
1930 	char *name;
1931 
1932 	ASSERT(dd->dd_phys->dd_clone_parent_obj != 0);
1933 	ASSERT(0 == (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE));
1934 
1935 	VERIFY(0 == dsl_dataset_open_obj(dp,
1936 	    dd->dd_phys->dd_clone_parent_obj,
1937 	    NULL, DS_MODE_EXCLUSIVE, FTAG, &pivot_ds));
1938 	/*
1939 	 * We need to explicitly open pdd, since pivot_ds's pdd will be
1940 	 * changing.
1941 	 */
1942 	VERIFY(0 == dsl_dir_open_obj(dp, pivot_ds->ds_dir->dd_object,
1943 	    NULL, FTAG, &pdd));
1944 
1945 	/* move snapshots to this dir */
1946 	name = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1947 	ds = pivot_ds;
1948 	/* CONSTCOND */
1949 	while (TRUE) {
1950 		dsl_dataset_t *prev;
1951 
1952 		/* move snap name entry */
1953 		dsl_dataset_name(ds, name);
1954 		VERIFY(0 == zap_remove(dp->dp_meta_objset,
1955 		    pa->snapnames_obj, ds->ds_snapname, tx));
1956 		VERIFY(0 == zap_add(dp->dp_meta_objset,
1957 		    hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname,
1958 		    8, 1, &ds->ds_object, tx));
1959 
1960 		/* change containing dsl_dir */
1961 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
1962 		ASSERT3U(ds->ds_phys->ds_dir_obj, ==, pdd->dd_object);
1963 		ds->ds_phys->ds_dir_obj = dd->dd_object;
1964 		ASSERT3P(ds->ds_dir, ==, pdd);
1965 		dsl_dir_close(ds->ds_dir, ds);
1966 		VERIFY(0 == dsl_dir_open_obj(dp, dd->dd_object,
1967 		    NULL, ds, &ds->ds_dir));
1968 
1969 		ASSERT3U(dsl_prop_numcb(ds), ==, 0);
1970 
1971 		if (ds->ds_phys->ds_prev_snap_obj == 0)
1972 			break;
1973 
1974 		VERIFY(0 == dsl_dataset_open_obj(dp,
1975 		    ds->ds_phys->ds_prev_snap_obj, NULL, DS_MODE_EXCLUSIVE,
1976 		    FTAG, &prev));
1977 
1978 		if (prev->ds_phys->ds_next_snap_obj != ds->ds_object) {
1979 			dsl_dataset_close(prev, DS_MODE_EXCLUSIVE, FTAG);
1980 			break;
1981 		}
1982 		if (ds != pivot_ds)
1983 			dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG);
1984 		ds = prev;
1985 	}
1986 	if (ds != pivot_ds)
1987 		dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG);
1988 
1989 	/* change pivot point's next snap */
1990 	dmu_buf_will_dirty(pivot_ds->ds_dbuf, tx);
1991 	pivot_ds->ds_phys->ds_next_snap_obj = pa->newnext_obj;
1992 
1993 	/* change clone_parent-age */
1994 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1995 	ASSERT3U(dd->dd_phys->dd_clone_parent_obj, ==, pivot_ds->ds_object);
1996 	dd->dd_phys->dd_clone_parent_obj = pdd->dd_phys->dd_clone_parent_obj;
1997 	dmu_buf_will_dirty(pdd->dd_dbuf, tx);
1998 	pdd->dd_phys->dd_clone_parent_obj = pivot_ds->ds_object;
1999 
2000 	/* change space accounting */
2001 	dsl_dir_diduse_space(pdd, -pa->used, -pa->comp, -pa->uncomp, tx);
2002 	dsl_dir_diduse_space(dd, pa->used, pa->comp, pa->uncomp, tx);
2003 	pivot_ds->ds_phys->ds_unique_bytes = pa->unique;
2004 
2005 	/* log history record */
2006 	spa_history_internal_log(LOG_DS_PROMOTE, dd->dd_pool->dp_spa, tx,
2007 	    cr, "dataset = %llu", ds->ds_object);
2008 
2009 	dsl_dir_close(pdd, FTAG);
2010 	dsl_dataset_close(pivot_ds, DS_MODE_EXCLUSIVE, FTAG);
2011 	kmem_free(name, MAXPATHLEN);
2012 }
2013 
2014 int
2015 dsl_dataset_promote(const char *name)
2016 {
2017 	dsl_dataset_t *ds;
2018 	int err;
2019 	dmu_object_info_t doi;
2020 	struct promotearg pa;
2021 
2022 	err = dsl_dataset_open(name, DS_MODE_NONE, FTAG, &ds);
2023 	if (err)
2024 		return (err);
2025 
2026 	err = dmu_object_info(ds->ds_dir->dd_pool->dp_meta_objset,
2027 	    ds->ds_phys->ds_snapnames_zapobj, &doi);
2028 	if (err) {
2029 		dsl_dataset_close(ds, DS_MODE_NONE, FTAG);
2030 		return (err);
2031 	}
2032 
2033 	/*
2034 	 * Add in 128x the snapnames zapobj size, since we will be moving
2035 	 * a bunch of snapnames to the promoted ds, and dirtying their
2036 	 * bonus buffers.
2037 	 */
2038 	err = dsl_sync_task_do(ds->ds_dir->dd_pool,
2039 	    dsl_dataset_promote_check,
2040 	    dsl_dataset_promote_sync, ds, &pa, 2 + 2 * doi.doi_physical_blks);
2041 	dsl_dataset_close(ds, DS_MODE_NONE, FTAG);
2042 	return (err);
2043 }
2044 
2045 /*
2046  * Given a pool name and a dataset object number in that pool,
2047  * return the name of that dataset.
2048  */
2049 int
2050 dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
2051 {
2052 	spa_t *spa;
2053 	dsl_pool_t *dp;
2054 	dsl_dataset_t *ds = NULL;
2055 	int error;
2056 
2057 	if ((error = spa_open(pname, &spa, FTAG)) != 0)
2058 		return (error);
2059 	dp = spa_get_dsl(spa);
2060 	rw_enter(&dp->dp_config_rwlock, RW_READER);
2061 	if ((error = dsl_dataset_open_obj(dp, obj,
2062 	    NULL, DS_MODE_NONE, FTAG, &ds)) != 0) {
2063 		rw_exit(&dp->dp_config_rwlock);
2064 		spa_close(spa, FTAG);
2065 		return (error);
2066 	}
2067 	dsl_dataset_name(ds, buf);
2068 	dsl_dataset_close(ds, DS_MODE_NONE, FTAG);
2069 	rw_exit(&dp->dp_config_rwlock);
2070 	spa_close(spa, FTAG);
2071 
2072 	return (0);
2073 }
2074