xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_send.c (revision 44bac77bf8165ebe38afb85dda247b928d88edf8)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/dmu.h>
29 #include <sys/dmu_impl.h>
30 #include <sys/dmu_tx.h>
31 #include <sys/dbuf.h>
32 #include <sys/dnode.h>
33 #include <sys/zfs_context.h>
34 #include <sys/dmu_objset.h>
35 #include <sys/dmu_traverse.h>
36 #include <sys/dsl_dataset.h>
37 #include <sys/dsl_dir.h>
38 #include <sys/dsl_pool.h>
39 #include <sys/dsl_synctask.h>
40 #include <sys/zfs_ioctl.h>
41 #include <sys/zap.h>
42 #include <sys/zio_checksum.h>
43 
44 static char *dmu_recv_tag = "dmu_recv_tag";
45 
46 struct backuparg {
47 	dmu_replay_record_t *drr;
48 	vnode_t *vp;
49 	offset_t *off;
50 	objset_t *os;
51 	zio_cksum_t zc;
52 	int err;
53 };
54 
55 static int
56 dump_bytes(struct backuparg *ba, void *buf, int len)
57 {
58 	ssize_t resid; /* have to get resid to get detailed errno */
59 	ASSERT3U(len % 8, ==, 0);
60 
61 	fletcher_4_incremental_native(buf, len, &ba->zc);
62 	ba->err = vn_rdwr(UIO_WRITE, ba->vp,
63 	    (caddr_t)buf, len,
64 	    0, UIO_SYSSPACE, FAPPEND, RLIM64_INFINITY, CRED(), &resid);
65 	*ba->off += len;
66 	return (ba->err);
67 }
68 
69 static int
70 dump_free(struct backuparg *ba, uint64_t object, uint64_t offset,
71     uint64_t length)
72 {
73 	/* write a FREE record */
74 	bzero(ba->drr, sizeof (dmu_replay_record_t));
75 	ba->drr->drr_type = DRR_FREE;
76 	ba->drr->drr_u.drr_free.drr_object = object;
77 	ba->drr->drr_u.drr_free.drr_offset = offset;
78 	ba->drr->drr_u.drr_free.drr_length = length;
79 
80 	if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t)))
81 		return (EINTR);
82 	return (0);
83 }
84 
85 static int
86 dump_data(struct backuparg *ba, dmu_object_type_t type,
87     uint64_t object, uint64_t offset, int blksz, void *data)
88 {
89 	/* write a DATA record */
90 	bzero(ba->drr, sizeof (dmu_replay_record_t));
91 	ba->drr->drr_type = DRR_WRITE;
92 	ba->drr->drr_u.drr_write.drr_object = object;
93 	ba->drr->drr_u.drr_write.drr_type = type;
94 	ba->drr->drr_u.drr_write.drr_offset = offset;
95 	ba->drr->drr_u.drr_write.drr_length = blksz;
96 
97 	if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t)))
98 		return (EINTR);
99 	if (dump_bytes(ba, data, blksz))
100 		return (EINTR);
101 	return (0);
102 }
103 
104 static int
105 dump_freeobjects(struct backuparg *ba, uint64_t firstobj, uint64_t numobjs)
106 {
107 	/* write a FREEOBJECTS record */
108 	bzero(ba->drr, sizeof (dmu_replay_record_t));
109 	ba->drr->drr_type = DRR_FREEOBJECTS;
110 	ba->drr->drr_u.drr_freeobjects.drr_firstobj = firstobj;
111 	ba->drr->drr_u.drr_freeobjects.drr_numobjs = numobjs;
112 
113 	if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t)))
114 		return (EINTR);
115 	return (0);
116 }
117 
118 static int
119 dump_dnode(struct backuparg *ba, uint64_t object, dnode_phys_t *dnp)
120 {
121 	if (dnp == NULL || dnp->dn_type == DMU_OT_NONE)
122 		return (dump_freeobjects(ba, object, 1));
123 
124 	/* write an OBJECT record */
125 	bzero(ba->drr, sizeof (dmu_replay_record_t));
126 	ba->drr->drr_type = DRR_OBJECT;
127 	ba->drr->drr_u.drr_object.drr_object = object;
128 	ba->drr->drr_u.drr_object.drr_type = dnp->dn_type;
129 	ba->drr->drr_u.drr_object.drr_bonustype = dnp->dn_bonustype;
130 	ba->drr->drr_u.drr_object.drr_blksz =
131 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
132 	ba->drr->drr_u.drr_object.drr_bonuslen = dnp->dn_bonuslen;
133 	ba->drr->drr_u.drr_object.drr_checksum = dnp->dn_checksum;
134 	ba->drr->drr_u.drr_object.drr_compress = dnp->dn_compress;
135 
136 	if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t)))
137 		return (EINTR);
138 
139 	if (dump_bytes(ba, DN_BONUS(dnp), P2ROUNDUP(dnp->dn_bonuslen, 8)))
140 		return (EINTR);
141 
142 	/* free anything past the end of the file */
143 	if (dump_free(ba, object, (dnp->dn_maxblkid + 1) *
144 	    (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT), -1ULL))
145 		return (EINTR);
146 	if (ba->err)
147 		return (EINTR);
148 	return (0);
149 }
150 
151 #define	BP_SPAN(dnp, level) \
152 	(((uint64_t)dnp->dn_datablkszsec) << (SPA_MINBLOCKSHIFT + \
153 	(level) * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT)))
154 
155 static int
156 backup_cb(traverse_blk_cache_t *bc, spa_t *spa, void *arg)
157 {
158 	struct backuparg *ba = arg;
159 	uint64_t object = bc->bc_bookmark.zb_object;
160 	int level = bc->bc_bookmark.zb_level;
161 	uint64_t blkid = bc->bc_bookmark.zb_blkid;
162 	blkptr_t *bp = bc->bc_blkptr.blk_birth ? &bc->bc_blkptr : NULL;
163 	dmu_object_type_t type = bp ? BP_GET_TYPE(bp) : DMU_OT_NONE;
164 	void *data = bc->bc_data;
165 	int err = 0;
166 
167 	if (issig(JUSTLOOKING) && issig(FORREAL))
168 		return (EINTR);
169 
170 	ASSERT(data || bp == NULL);
171 
172 	if (bp == NULL && object == 0) {
173 		uint64_t span = BP_SPAN(bc->bc_dnode, level);
174 		uint64_t dnobj = (blkid * span) >> DNODE_SHIFT;
175 		err = dump_freeobjects(ba, dnobj, span >> DNODE_SHIFT);
176 	} else if (bp == NULL) {
177 		uint64_t span = BP_SPAN(bc->bc_dnode, level);
178 		err = dump_free(ba, object, blkid * span, span);
179 	} else if (data && level == 0 && type == DMU_OT_DNODE) {
180 		dnode_phys_t *blk = data;
181 		int i;
182 		int blksz = BP_GET_LSIZE(bp);
183 
184 		for (i = 0; i < blksz >> DNODE_SHIFT; i++) {
185 			uint64_t dnobj =
186 			    (blkid << (DNODE_BLOCK_SHIFT - DNODE_SHIFT)) + i;
187 			err = dump_dnode(ba, dnobj, blk+i);
188 			if (err)
189 				break;
190 		}
191 	} else if (level == 0 &&
192 	    type != DMU_OT_DNODE && type != DMU_OT_OBJSET) {
193 		int blksz = BP_GET_LSIZE(bp);
194 		if (data == NULL) {
195 			uint32_t aflags = ARC_WAIT;
196 			arc_buf_t *abuf;
197 			zbookmark_t zb;
198 
199 			zb.zb_objset = ba->os->os->os_dsl_dataset->ds_object;
200 			zb.zb_object = object;
201 			zb.zb_level = level;
202 			zb.zb_blkid = blkid;
203 			(void) arc_read(NULL, spa, bp,
204 			    dmu_ot[type].ot_byteswap, arc_getbuf_func, &abuf,
205 			    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_MUSTSUCCEED,
206 			    &aflags, &zb);
207 
208 			if (abuf) {
209 				err = dump_data(ba, type, object, blkid * blksz,
210 				    blksz, abuf->b_data);
211 				(void) arc_buf_remove_ref(abuf, &abuf);
212 			}
213 		} else {
214 			err = dump_data(ba, type, object, blkid * blksz,
215 			    blksz, data);
216 		}
217 	}
218 
219 	ASSERT(err == 0 || err == EINTR);
220 	return (err);
221 }
222 
223 int
224 dmu_sendbackup(objset_t *tosnap, objset_t *fromsnap, boolean_t fromorigin,
225     vnode_t *vp, offset_t *off)
226 {
227 	dsl_dataset_t *ds = tosnap->os->os_dsl_dataset;
228 	dsl_dataset_t *fromds = fromsnap ? fromsnap->os->os_dsl_dataset : NULL;
229 	dmu_replay_record_t *drr;
230 	struct backuparg ba;
231 	int err;
232 	uint64_t fromtxg = 0;
233 
234 	/* tosnap must be a snapshot */
235 	if (ds->ds_phys->ds_next_snap_obj == 0)
236 		return (EINVAL);
237 
238 	/* fromsnap must be an earlier snapshot from the same fs as tosnap */
239 	if (fromds && (ds->ds_dir != fromds->ds_dir ||
240 	    fromds->ds_phys->ds_creation_txg >= ds->ds_phys->ds_creation_txg))
241 		return (EXDEV);
242 
243 	if (fromorigin) {
244 		if (fromsnap)
245 			return (EINVAL);
246 
247 		if (ds->ds_dir->dd_phys->dd_origin_obj != NULL) {
248 			dsl_pool_t *dp = ds->ds_dir->dd_pool;
249 			rw_enter(&dp->dp_config_rwlock, RW_READER);
250 			err = dsl_dataset_open_obj(dp,
251 			    ds->ds_dir->dd_phys->dd_origin_obj, NULL,
252 			    DS_MODE_NONE, FTAG, &fromds);
253 			rw_exit(&dp->dp_config_rwlock);
254 			if (err)
255 				return (err);
256 		} else {
257 			fromorigin = B_FALSE;
258 		}
259 	}
260 
261 
262 	drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP);
263 	drr->drr_type = DRR_BEGIN;
264 	drr->drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
265 	drr->drr_u.drr_begin.drr_version = DMU_BACKUP_STREAM_VERSION;
266 	drr->drr_u.drr_begin.drr_creation_time =
267 	    ds->ds_phys->ds_creation_time;
268 	drr->drr_u.drr_begin.drr_type = tosnap->os->os_phys->os_type;
269 	if (fromorigin)
270 		drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CLONE;
271 	drr->drr_u.drr_begin.drr_toguid = ds->ds_phys->ds_guid;
272 	if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
273 		drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CI_DATA;
274 
275 	if (fromds)
276 		drr->drr_u.drr_begin.drr_fromguid = fromds->ds_phys->ds_guid;
277 	dsl_dataset_name(ds, drr->drr_u.drr_begin.drr_toname);
278 
279 	if (fromds)
280 		fromtxg = fromds->ds_phys->ds_creation_txg;
281 	if (fromorigin)
282 		dsl_dataset_close(fromds, DS_MODE_NONE, FTAG);
283 
284 	ba.drr = drr;
285 	ba.vp = vp;
286 	ba.os = tosnap;
287 	ba.off = off;
288 	ZIO_SET_CHECKSUM(&ba.zc, 0, 0, 0, 0);
289 
290 	if (dump_bytes(&ba, drr, sizeof (dmu_replay_record_t))) {
291 		kmem_free(drr, sizeof (dmu_replay_record_t));
292 		return (ba.err);
293 	}
294 
295 	err = traverse_dsl_dataset(ds, fromtxg,
296 	    ADVANCE_PRE | ADVANCE_HOLES | ADVANCE_DATA | ADVANCE_NOLOCK,
297 	    backup_cb, &ba);
298 
299 	if (err) {
300 		if (err == EINTR && ba.err)
301 			err = ba.err;
302 		kmem_free(drr, sizeof (dmu_replay_record_t));
303 		return (err);
304 	}
305 
306 	bzero(drr, sizeof (dmu_replay_record_t));
307 	drr->drr_type = DRR_END;
308 	drr->drr_u.drr_end.drr_checksum = ba.zc;
309 
310 	if (dump_bytes(&ba, drr, sizeof (dmu_replay_record_t))) {
311 		kmem_free(drr, sizeof (dmu_replay_record_t));
312 		return (ba.err);
313 	}
314 
315 	kmem_free(drr, sizeof (dmu_replay_record_t));
316 
317 	return (0);
318 }
319 
320 struct recvbeginsyncarg {
321 	const char *tofs;
322 	const char *tosnap;
323 	dsl_dataset_t *origin;
324 	uint64_t fromguid;
325 	dmu_objset_type_t type;
326 	void *tag;
327 	boolean_t force;
328 	uint64_t dsflags;
329 	char clonelastname[MAXNAMELEN];
330 	dsl_dataset_t *ds; /* the ds to recv into; returned from the syncfunc */
331 };
332 
333 static dsl_dataset_t *
334 recv_full_sync_impl(dsl_pool_t *dp, uint64_t dsobj, dmu_objset_type_t type,
335     cred_t *cr, dmu_tx_t *tx)
336 {
337 	dsl_dataset_t *ds;
338 
339 	VERIFY(0 == dsl_dataset_open_obj(dp, dsobj, NULL,
340 	    DS_MODE_EXCLUSIVE, dmu_recv_tag, &ds));
341 
342 	if (type != DMU_OST_NONE) {
343 		(void) dmu_objset_create_impl(dp->dp_spa,
344 		    ds, &ds->ds_phys->ds_bp, type, tx);
345 	}
346 
347 	spa_history_internal_log(LOG_DS_REPLAY_FULL_SYNC,
348 	    ds->ds_dir->dd_pool->dp_spa, tx, cr, "dataset = %lld",
349 	    ds->ds_phys->ds_dir_obj);
350 
351 	return (ds);
352 }
353 
354 /* ARGSUSED */
355 static int
356 recv_full_check(void *arg1, void *arg2, dmu_tx_t *tx)
357 {
358 	dsl_dir_t *dd = arg1;
359 	struct recvbeginsyncarg *rbsa = arg2;
360 	objset_t *mos = dd->dd_pool->dp_meta_objset;
361 	uint64_t val;
362 	int err;
363 
364 	err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj,
365 	    strrchr(rbsa->tofs, '/') + 1, sizeof (uint64_t), 1, &val);
366 
367 	if (err != ENOENT)
368 		return (err ? err : EEXIST);
369 
370 	if (rbsa->origin) {
371 		/* make sure it's a snap in the same pool */
372 		if (rbsa->origin->ds_dir->dd_pool != dd->dd_pool)
373 			return (EXDEV);
374 		if (rbsa->origin->ds_phys->ds_num_children == 0)
375 			return (EINVAL);
376 		if (rbsa->origin->ds_phys->ds_guid != rbsa->fromguid)
377 			return (ENODEV);
378 	}
379 
380 	return (0);
381 }
382 
383 static void
384 recv_full_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
385 {
386 	dsl_dir_t *dd = arg1;
387 	struct recvbeginsyncarg *rbsa = arg2;
388 	uint64_t dsobj;
389 	uint64_t flags = DS_FLAG_INCONSISTENT;
390 
391 	flags |= rbsa->dsflags;
392 
393 	dsobj = dsl_dataset_create_sync(dd, strrchr(rbsa->tofs, '/') + 1,
394 	    rbsa->origin, flags, cr, tx);
395 
396 	rbsa->ds = recv_full_sync_impl(dd->dd_pool, dsobj,
397 	    rbsa->origin ? DMU_OST_NONE : rbsa->type, cr, tx);
398 }
399 
400 static int
401 recv_full_existing_check(void *arg1, void *arg2, dmu_tx_t *tx)
402 {
403 	dsl_dataset_t *ds = arg1;
404 	struct recvbeginsyncarg *rbsa = arg2;
405 	int err;
406 
407 	/* must be a head ds */
408 	if (ds->ds_phys->ds_next_snap_obj != 0)
409 		return (EINVAL);
410 
411 	/* must not be a clone ds */
412 	if (ds->ds_prev != NULL)
413 		return (EINVAL);
414 
415 	err = dsl_dataset_destroy_check(ds, rbsa->tag, tx);
416 	if (err)
417 		return (err);
418 
419 	if (rbsa->origin) {
420 		/* make sure it's a snap in the same pool */
421 		if (rbsa->origin->ds_dir->dd_pool != ds->ds_dir->dd_pool)
422 			return (EXDEV);
423 		if (rbsa->origin->ds_phys->ds_num_children == 0)
424 			return (EINVAL);
425 		if (rbsa->origin->ds_phys->ds_guid != rbsa->fromguid)
426 			return (ENODEV);
427 	}
428 
429 	return (0);
430 }
431 
432 static void
433 recv_full_existing_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
434 {
435 	dsl_dataset_t *ds = arg1;
436 	struct recvbeginsyncarg *rbsa = arg2;
437 	dsl_dir_t *dd = ds->ds_dir;
438 	uint64_t dsobj;
439 	uint64_t flags = DS_FLAG_INCONSISTENT;
440 
441 	flags |= rbsa->dsflags;
442 
443 	/*
444 	 * NB: caller must provide an extra hold on the dsl_dir_t, so it
445 	 * won't go away when dsl_dataset_destroy_sync() closes the
446 	 * dataset.
447 	 */
448 	dsl_dataset_destroy_sync(ds, rbsa->tag, cr, tx);
449 
450 	dsobj = dsl_dataset_create_sync_impl(dd, rbsa->origin, flags, tx);
451 
452 	rbsa->ds = recv_full_sync_impl(dd->dd_pool, dsobj,
453 	    rbsa->origin ? DMU_OST_NONE : rbsa->type, cr, tx);
454 }
455 
456 /* ARGSUSED */
457 static int
458 recv_incremental_check(void *arg1, void *arg2, dmu_tx_t *tx)
459 {
460 	dsl_dataset_t *ds = arg1;
461 	struct recvbeginsyncarg *rbsa = arg2;
462 	int err;
463 	uint64_t val;
464 
465 	/* must not have any changes since most recent snapshot */
466 	if (!rbsa->force && dsl_dataset_modified_since_lastsnap(ds))
467 		return (ETXTBSY);
468 
469 	/* must already be a snapshot of this fs */
470 	if (ds->ds_phys->ds_prev_snap_obj == 0)
471 		return (ENODEV);
472 
473 	/* most recent snapshot must match fromguid */
474 	if (ds->ds_prev->ds_phys->ds_guid != rbsa->fromguid)
475 		return (ENODEV);
476 
477 	/* temporary clone name must not exist */
478 	err = zap_lookup(ds->ds_dir->dd_pool->dp_meta_objset,
479 	    ds->ds_dir->dd_phys->dd_child_dir_zapobj,
480 	    rbsa->clonelastname, 8, 1, &val);
481 	if (err == 0)
482 		return (EEXIST);
483 	if (err != ENOENT)
484 		return (err);
485 
486 	/* new snapshot name must not exist */
487 	err = zap_lookup(ds->ds_dir->dd_pool->dp_meta_objset,
488 	    ds->ds_phys->ds_snapnames_zapobj, rbsa->tosnap, 8, 1, &val);
489 	if (err == 0)
490 		return (EEXIST);
491 	if (err != ENOENT)
492 		return (err);
493 	return (0);
494 }
495 
496 /* ARGSUSED */
497 static void
498 recv_online_incremental_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
499 {
500 	dsl_dataset_t *ohds = arg1;
501 	struct recvbeginsyncarg *rbsa = arg2;
502 	dsl_pool_t *dp = ohds->ds_dir->dd_pool;
503 	dsl_dataset_t *ods, *cds;
504 	uint64_t dsobj;
505 	uint64_t flags = DS_FLAG_INCONSISTENT;
506 
507 	flags |= rbsa->dsflags;
508 
509 	/* create the temporary clone */
510 	VERIFY(0 == dsl_dataset_open_obj(dp, ohds->ds_phys->ds_prev_snap_obj,
511 	    NULL, DS_MODE_STANDARD, FTAG, &ods));
512 	dsobj = dsl_dataset_create_sync(ohds->ds_dir,
513 	    rbsa->clonelastname, ods, flags, cr, tx);
514 	dsl_dataset_close(ods, DS_MODE_STANDARD, FTAG);
515 
516 	/* open the temporary clone */
517 	VERIFY(0 == dsl_dataset_open_obj(dp, dsobj, NULL,
518 	    DS_MODE_EXCLUSIVE, dmu_recv_tag, &cds));
519 
520 	/* copy the refquota from the target fs to the clone */
521 	if (ohds->ds_quota > 0)
522 		dsl_dataset_set_quota_sync(cds, &ohds->ds_quota, cr, tx);
523 
524 	rbsa->ds = cds;
525 
526 	spa_history_internal_log(LOG_DS_REPLAY_INC_SYNC,
527 	    dp->dp_spa, tx, cr, "dataset = %lld",
528 	    cds->ds_phys->ds_dir_obj);
529 }
530 
531 /* ARGSUSED */
532 static void
533 recv_offline_incremental_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
534 {
535 	dsl_dataset_t *ds = arg1;
536 
537 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
538 	ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT;
539 
540 	spa_history_internal_log(LOG_DS_REPLAY_INC_SYNC,
541 	    ds->ds_dir->dd_pool->dp_spa, tx, cr, "dataset = %lld",
542 	    ds->ds_phys->ds_dir_obj);
543 }
544 
545 /*
546  * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
547  * succeeds; otherwise we will leak the holds on the datasets.
548  */
549 int
550 dmu_recv_begin(char *tofs, char *tosnap, struct drr_begin *drrb,
551     boolean_t force, objset_t *origin, boolean_t online, dmu_recv_cookie_t *drc)
552 {
553 	int err = 0;
554 	boolean_t byteswap;
555 	struct recvbeginsyncarg rbsa;
556 	uint64_t version;
557 	int flags;
558 	dsl_dataset_t *ds;
559 
560 	if (drrb->drr_magic == DMU_BACKUP_MAGIC)
561 		byteswap = FALSE;
562 	else if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC))
563 		byteswap = TRUE;
564 	else
565 		return (EINVAL);
566 
567 	rbsa.tofs = tofs;
568 	rbsa.tosnap = tosnap;
569 	rbsa.origin = origin ? origin->os->os_dsl_dataset : NULL;
570 	rbsa.fromguid = drrb->drr_fromguid;
571 	rbsa.type = drrb->drr_type;
572 	rbsa.tag = FTAG;
573 	rbsa.dsflags = 0;
574 	version = drrb->drr_version;
575 	flags = drrb->drr_flags;
576 
577 	if (byteswap) {
578 		rbsa.type = BSWAP_32(rbsa.type);
579 		rbsa.fromguid = BSWAP_64(rbsa.fromguid);
580 		version = BSWAP_64(version);
581 		flags = BSWAP_32(flags);
582 	}
583 
584 	if (version != DMU_BACKUP_STREAM_VERSION ||
585 	    rbsa.type >= DMU_OST_NUMTYPES ||
586 	    ((flags & DRR_FLAG_CLONE) && origin == NULL))
587 		return (EINVAL);
588 
589 	if (flags & DRR_FLAG_CI_DATA)
590 		rbsa.dsflags = DS_FLAG_CI_DATASET;
591 
592 	bzero(drc, sizeof (dmu_recv_cookie_t));
593 	drc->drc_drrb = drrb;
594 	drc->drc_tosnap = tosnap;
595 	drc->drc_force = force;
596 
597 	/*
598 	 * Process the begin in syncing context.
599 	 */
600 	if (rbsa.fromguid && !(flags & DRR_FLAG_CLONE) && !online) {
601 		/* offline incremental receive */
602 		err = dsl_dataset_open(tofs,
603 		    DS_MODE_EXCLUSIVE, dmu_recv_tag, &ds);
604 		if (err)
605 			return (err);
606 
607 		/*
608 		 * Only do the rollback if the most recent snapshot
609 		 * matches the incremental source
610 		 */
611 		if (force) {
612 			if (ds->ds_prev == NULL ||
613 			    ds->ds_prev->ds_phys->ds_guid !=
614 			    rbsa.fromguid) {
615 				dsl_dataset_close(ds, DS_MODE_EXCLUSIVE,
616 				    dmu_recv_tag);
617 				return (ENODEV);
618 			}
619 			(void) dsl_dataset_rollback(ds, DMU_OST_NONE);
620 		}
621 		rbsa.force = B_FALSE;
622 		err = dsl_sync_task_do(ds->ds_dir->dd_pool,
623 		    recv_incremental_check,
624 		    recv_offline_incremental_sync,
625 		    ds, &rbsa, 1);
626 		if (err) {
627 			dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, dmu_recv_tag);
628 			return (err);
629 		}
630 		drc->drc_logical_ds = drc->drc_real_ds = ds;
631 	} else if (rbsa.fromguid && !(flags & DRR_FLAG_CLONE)) {
632 		/* online incremental receive */
633 
634 		/* tmp clone name is: tofs/%tosnap" */
635 		(void) snprintf(rbsa.clonelastname, sizeof (rbsa.clonelastname),
636 		    "%%%s", tosnap);
637 
638 		/* open the dataset we are logically receiving into */
639 		err = dsl_dataset_open(tofs,
640 		    DS_MODE_STANDARD, dmu_recv_tag, &ds);
641 		if (err)
642 			return (err);
643 
644 		rbsa.force = force;
645 		err = dsl_sync_task_do(ds->ds_dir->dd_pool,
646 		    recv_incremental_check,
647 		    recv_online_incremental_sync, ds, &rbsa, 5);
648 		if (err) {
649 			dsl_dataset_close(ds, DS_MODE_STANDARD, dmu_recv_tag);
650 			return (err);
651 		}
652 		drc->drc_logical_ds = ds;
653 		drc->drc_real_ds = rbsa.ds;
654 	} else {
655 		/* create new fs -- full backup or clone */
656 		dsl_dir_t *dd = NULL;
657 		const char *tail;
658 
659 		err = dsl_dir_open(tofs, FTAG, &dd, &tail);
660 		if (err)
661 			return (err);
662 		if (tail == NULL) {
663 			if (!force) {
664 				dsl_dir_close(dd, FTAG);
665 				return (EEXIST);
666 			}
667 
668 			rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
669 			err = dsl_dataset_open_obj(dd->dd_pool,
670 			    dd->dd_phys->dd_head_dataset_obj, NULL,
671 			    DS_MODE_EXCLUSIVE | DS_MODE_INCONSISTENT,
672 			    FTAG, &ds);
673 			rw_exit(&dd->dd_pool->dp_config_rwlock);
674 			if (err) {
675 				dsl_dir_close(dd, FTAG);
676 				return (err);
677 			}
678 
679 			err = dsl_sync_task_do(dd->dd_pool,
680 			    recv_full_existing_check,
681 			    recv_full_existing_sync, ds, &rbsa, 5);
682 			/* if successful, sync task closes the ds for us */
683 			if (err)
684 				dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG);
685 		} else {
686 			err = dsl_sync_task_do(dd->dd_pool, recv_full_check,
687 			    recv_full_sync, dd, &rbsa, 5);
688 			if (err)
689 				return (err);
690 		}
691 		dsl_dir_close(dd, FTAG);
692 		if (err)
693 			return (err);
694 		drc->drc_logical_ds = drc->drc_real_ds = rbsa.ds;
695 		drc->drc_newfs = B_TRUE;
696 	}
697 
698 	/* downgrade our hold on the ds from EXCLUSIVE to PRIMARY */
699 	dsl_dataset_downgrade(drc->drc_real_ds,
700 	    DS_MODE_EXCLUSIVE, DS_MODE_PRIMARY);
701 
702 	return (0);
703 }
704 
705 struct restorearg {
706 	int err;
707 	int byteswap;
708 	vnode_t *vp;
709 	char *buf;
710 	uint64_t voff;
711 	int bufsize; /* amount of memory allocated for buf */
712 	zio_cksum_t cksum;
713 };
714 
715 static void *
716 restore_read(struct restorearg *ra, int len)
717 {
718 	void *rv;
719 	int done = 0;
720 
721 	/* some things will require 8-byte alignment, so everything must */
722 	ASSERT3U(len % 8, ==, 0);
723 
724 	while (done < len) {
725 		ssize_t resid;
726 
727 		ra->err = vn_rdwr(UIO_READ, ra->vp,
728 		    (caddr_t)ra->buf + done, len - done,
729 		    ra->voff, UIO_SYSSPACE, FAPPEND,
730 		    RLIM64_INFINITY, CRED(), &resid);
731 
732 		if (resid == len - done)
733 			ra->err = EINVAL;
734 		ra->voff += len - done - resid;
735 		done = len - resid;
736 		if (ra->err)
737 			return (NULL);
738 	}
739 
740 	ASSERT3U(done, ==, len);
741 	rv = ra->buf;
742 	if (ra->byteswap)
743 		fletcher_4_incremental_byteswap(rv, len, &ra->cksum);
744 	else
745 		fletcher_4_incremental_native(rv, len, &ra->cksum);
746 	return (rv);
747 }
748 
749 static void
750 backup_byteswap(dmu_replay_record_t *drr)
751 {
752 #define	DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
753 #define	DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
754 	drr->drr_type = BSWAP_32(drr->drr_type);
755 	drr->drr_payloadlen = BSWAP_32(drr->drr_payloadlen);
756 	switch (drr->drr_type) {
757 	case DRR_BEGIN:
758 		DO64(drr_begin.drr_magic);
759 		DO64(drr_begin.drr_version);
760 		DO64(drr_begin.drr_creation_time);
761 		DO32(drr_begin.drr_type);
762 		DO32(drr_begin.drr_flags);
763 		DO64(drr_begin.drr_toguid);
764 		DO64(drr_begin.drr_fromguid);
765 		break;
766 	case DRR_OBJECT:
767 		DO64(drr_object.drr_object);
768 		/* DO64(drr_object.drr_allocation_txg); */
769 		DO32(drr_object.drr_type);
770 		DO32(drr_object.drr_bonustype);
771 		DO32(drr_object.drr_blksz);
772 		DO32(drr_object.drr_bonuslen);
773 		break;
774 	case DRR_FREEOBJECTS:
775 		DO64(drr_freeobjects.drr_firstobj);
776 		DO64(drr_freeobjects.drr_numobjs);
777 		break;
778 	case DRR_WRITE:
779 		DO64(drr_write.drr_object);
780 		DO32(drr_write.drr_type);
781 		DO64(drr_write.drr_offset);
782 		DO64(drr_write.drr_length);
783 		break;
784 	case DRR_FREE:
785 		DO64(drr_free.drr_object);
786 		DO64(drr_free.drr_offset);
787 		DO64(drr_free.drr_length);
788 		break;
789 	case DRR_END:
790 		DO64(drr_end.drr_checksum.zc_word[0]);
791 		DO64(drr_end.drr_checksum.zc_word[1]);
792 		DO64(drr_end.drr_checksum.zc_word[2]);
793 		DO64(drr_end.drr_checksum.zc_word[3]);
794 		break;
795 	}
796 #undef DO64
797 #undef DO32
798 }
799 
800 static int
801 restore_object(struct restorearg *ra, objset_t *os, struct drr_object *drro)
802 {
803 	int err;
804 	dmu_tx_t *tx;
805 
806 	err = dmu_object_info(os, drro->drr_object, NULL);
807 
808 	if (err != 0 && err != ENOENT)
809 		return (EINVAL);
810 
811 	if (drro->drr_type == DMU_OT_NONE ||
812 	    drro->drr_type >= DMU_OT_NUMTYPES ||
813 	    drro->drr_bonustype >= DMU_OT_NUMTYPES ||
814 	    drro->drr_checksum >= ZIO_CHECKSUM_FUNCTIONS ||
815 	    drro->drr_compress >= ZIO_COMPRESS_FUNCTIONS ||
816 	    P2PHASE(drro->drr_blksz, SPA_MINBLOCKSIZE) ||
817 	    drro->drr_blksz < SPA_MINBLOCKSIZE ||
818 	    drro->drr_blksz > SPA_MAXBLOCKSIZE ||
819 	    drro->drr_bonuslen > DN_MAX_BONUSLEN) {
820 		return (EINVAL);
821 	}
822 
823 	tx = dmu_tx_create(os);
824 
825 	if (err == ENOENT) {
826 		/* currently free, want to be allocated */
827 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
828 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 1);
829 		err = dmu_tx_assign(tx, TXG_WAIT);
830 		if (err) {
831 			dmu_tx_abort(tx);
832 			return (err);
833 		}
834 		err = dmu_object_claim(os, drro->drr_object,
835 		    drro->drr_type, drro->drr_blksz,
836 		    drro->drr_bonustype, drro->drr_bonuslen, tx);
837 	} else {
838 		/* currently allocated, want to be allocated */
839 		dmu_tx_hold_bonus(tx, drro->drr_object);
840 		/*
841 		 * We may change blocksize, so need to
842 		 * hold_write
843 		 */
844 		dmu_tx_hold_write(tx, drro->drr_object, 0, 1);
845 		err = dmu_tx_assign(tx, TXG_WAIT);
846 		if (err) {
847 			dmu_tx_abort(tx);
848 			return (err);
849 		}
850 
851 		err = dmu_object_reclaim(os, drro->drr_object,
852 		    drro->drr_type, drro->drr_blksz,
853 		    drro->drr_bonustype, drro->drr_bonuslen, tx);
854 	}
855 	if (err) {
856 		dmu_tx_commit(tx);
857 		return (EINVAL);
858 	}
859 
860 	dmu_object_set_checksum(os, drro->drr_object, drro->drr_checksum, tx);
861 	dmu_object_set_compress(os, drro->drr_object, drro->drr_compress, tx);
862 
863 	if (drro->drr_bonuslen) {
864 		dmu_buf_t *db;
865 		void *data;
866 		VERIFY(0 == dmu_bonus_hold(os, drro->drr_object, FTAG, &db));
867 		dmu_buf_will_dirty(db, tx);
868 
869 		ASSERT3U(db->db_size, >=, drro->drr_bonuslen);
870 		data = restore_read(ra, P2ROUNDUP(drro->drr_bonuslen, 8));
871 		if (data == NULL) {
872 			dmu_tx_commit(tx);
873 			return (ra->err);
874 		}
875 		bcopy(data, db->db_data, drro->drr_bonuslen);
876 		if (ra->byteswap) {
877 			dmu_ot[drro->drr_bonustype].ot_byteswap(db->db_data,
878 			    drro->drr_bonuslen);
879 		}
880 		dmu_buf_rele(db, FTAG);
881 	}
882 	dmu_tx_commit(tx);
883 	return (0);
884 }
885 
886 /* ARGSUSED */
887 static int
888 restore_freeobjects(struct restorearg *ra, objset_t *os,
889     struct drr_freeobjects *drrfo)
890 {
891 	uint64_t obj;
892 
893 	if (drrfo->drr_firstobj + drrfo->drr_numobjs < drrfo->drr_firstobj)
894 		return (EINVAL);
895 
896 	for (obj = drrfo->drr_firstobj;
897 	    obj < drrfo->drr_firstobj + drrfo->drr_numobjs;
898 	    (void) dmu_object_next(os, &obj, FALSE, 0)) {
899 		dmu_tx_t *tx;
900 		int err;
901 
902 		if (dmu_object_info(os, obj, NULL) != 0)
903 			continue;
904 
905 		tx = dmu_tx_create(os);
906 		dmu_tx_hold_bonus(tx, obj);
907 		err = dmu_tx_assign(tx, TXG_WAIT);
908 		if (err) {
909 			dmu_tx_abort(tx);
910 			return (err);
911 		}
912 		err = dmu_object_free(os, obj, tx);
913 		dmu_tx_commit(tx);
914 		if (err && err != ENOENT)
915 			return (EINVAL);
916 	}
917 	return (0);
918 }
919 
920 static int
921 restore_write(struct restorearg *ra, objset_t *os,
922     struct drr_write *drrw)
923 {
924 	dmu_tx_t *tx;
925 	void *data;
926 	int err;
927 
928 	if (drrw->drr_offset + drrw->drr_length < drrw->drr_offset ||
929 	    drrw->drr_type >= DMU_OT_NUMTYPES)
930 		return (EINVAL);
931 
932 	data = restore_read(ra, drrw->drr_length);
933 	if (data == NULL)
934 		return (ra->err);
935 
936 	if (dmu_object_info(os, drrw->drr_object, NULL) != 0)
937 		return (EINVAL);
938 
939 	tx = dmu_tx_create(os);
940 
941 	dmu_tx_hold_write(tx, drrw->drr_object,
942 	    drrw->drr_offset, drrw->drr_length);
943 	err = dmu_tx_assign(tx, TXG_WAIT);
944 	if (err) {
945 		dmu_tx_abort(tx);
946 		return (err);
947 	}
948 	if (ra->byteswap)
949 		dmu_ot[drrw->drr_type].ot_byteswap(data, drrw->drr_length);
950 	dmu_write(os, drrw->drr_object,
951 	    drrw->drr_offset, drrw->drr_length, data, tx);
952 	dmu_tx_commit(tx);
953 	return (0);
954 }
955 
956 /* ARGSUSED */
957 static int
958 restore_free(struct restorearg *ra, objset_t *os,
959     struct drr_free *drrf)
960 {
961 	dmu_tx_t *tx;
962 	int err;
963 
964 	if (drrf->drr_length != -1ULL &&
965 	    drrf->drr_offset + drrf->drr_length < drrf->drr_offset)
966 		return (EINVAL);
967 
968 	if (dmu_object_info(os, drrf->drr_object, NULL) != 0)
969 		return (EINVAL);
970 
971 	tx = dmu_tx_create(os);
972 
973 	dmu_tx_hold_free(tx, drrf->drr_object,
974 	    drrf->drr_offset, drrf->drr_length);
975 	err = dmu_tx_assign(tx, TXG_WAIT);
976 	if (err) {
977 		dmu_tx_abort(tx);
978 		return (err);
979 	}
980 	err = dmu_free_range(os, drrf->drr_object,
981 	    drrf->drr_offset, drrf->drr_length, tx);
982 	dmu_tx_commit(tx);
983 	return (err);
984 }
985 
986 void
987 dmu_recv_abort_cleanup(dmu_recv_cookie_t *drc)
988 {
989 	if (drc->drc_newfs || drc->drc_real_ds != drc->drc_logical_ds) {
990 		/*
991 		 * online incremental or new fs: destroy the fs (which
992 		 * may be a clone) that we created
993 		 */
994 		(void) dsl_dataset_destroy(drc->drc_real_ds, dmu_recv_tag);
995 		if (drc->drc_real_ds != drc->drc_logical_ds) {
996 			dsl_dataset_close(drc->drc_logical_ds,
997 			    DS_MODE_STANDARD, dmu_recv_tag);
998 		}
999 	} else {
1000 		/*
1001 		 * offline incremental: rollback to most recent snapshot.
1002 		 */
1003 		int lmode = DS_MODE_PRIMARY;
1004 		if (dsl_dataset_tryupgrade(drc->drc_real_ds,
1005 		    DS_MODE_PRIMARY, DS_MODE_EXCLUSIVE)) {
1006 			lmode = DS_MODE_EXCLUSIVE;
1007 			(void) dsl_dataset_rollback(drc->drc_real_ds,
1008 			    DMU_OST_NONE);
1009 		}
1010 		dsl_dataset_close(drc->drc_real_ds, lmode, FTAG);
1011 	}
1012 }
1013 
1014 /*
1015  * NB: callers *must* call dmu_recv_end() if this succeeds.
1016  */
1017 int
1018 dmu_recv_stream(dmu_recv_cookie_t *drc, vnode_t *vp, offset_t *voffp)
1019 {
1020 	struct restorearg ra = { 0 };
1021 	dmu_replay_record_t *drr;
1022 	objset_t *os;
1023 	zio_cksum_t pcksum;
1024 
1025 	if (drc->drc_drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC))
1026 		ra.byteswap = TRUE;
1027 
1028 	{
1029 		/* compute checksum of drr_begin record */
1030 		dmu_replay_record_t *drr;
1031 		drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP);
1032 
1033 		drr->drr_type = DRR_BEGIN;
1034 		drr->drr_u.drr_begin = *drc->drc_drrb;
1035 		if (ra.byteswap) {
1036 			fletcher_4_incremental_byteswap(drr,
1037 			    sizeof (dmu_replay_record_t), &ra.cksum);
1038 		} else {
1039 			fletcher_4_incremental_native(drr,
1040 			    sizeof (dmu_replay_record_t), &ra.cksum);
1041 		}
1042 		kmem_free(drr, sizeof (dmu_replay_record_t));
1043 	}
1044 
1045 	if (ra.byteswap) {
1046 		struct drr_begin *drrb = drc->drc_drrb;
1047 		drrb->drr_magic = BSWAP_64(drrb->drr_magic);
1048 		drrb->drr_version = BSWAP_64(drrb->drr_version);
1049 		drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
1050 		drrb->drr_type = BSWAP_32(drrb->drr_type);
1051 		drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
1052 		drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
1053 	}
1054 
1055 	ra.vp = vp;
1056 	ra.voff = *voffp;
1057 	ra.bufsize = 1<<20;
1058 	ra.buf = kmem_alloc(ra.bufsize, KM_SLEEP);
1059 
1060 	/* these were verified in dmu_recv_begin */
1061 	ASSERT(drc->drc_drrb->drr_version == DMU_BACKUP_STREAM_VERSION);
1062 	ASSERT(drc->drc_drrb->drr_type < DMU_OST_NUMTYPES);
1063 
1064 	/*
1065 	 * Open the objset we are modifying.
1066 	 */
1067 	VERIFY(dmu_objset_open_ds(drc->drc_real_ds, DMU_OST_ANY, &os) == 0);
1068 
1069 	ASSERT(drc->drc_real_ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT);
1070 
1071 	/*
1072 	 * Read records and process them.
1073 	 */
1074 	pcksum = ra.cksum;
1075 	while (ra.err == 0 &&
1076 	    NULL != (drr = restore_read(&ra, sizeof (*drr)))) {
1077 		if (issig(JUSTLOOKING) && issig(FORREAL)) {
1078 			ra.err = EINTR;
1079 			goto out;
1080 		}
1081 
1082 		if (ra.byteswap)
1083 			backup_byteswap(drr);
1084 
1085 		switch (drr->drr_type) {
1086 		case DRR_OBJECT:
1087 		{
1088 			/*
1089 			 * We need to make a copy of the record header,
1090 			 * because restore_{object,write} may need to
1091 			 * restore_read(), which will invalidate drr.
1092 			 */
1093 			struct drr_object drro = drr->drr_u.drr_object;
1094 			ra.err = restore_object(&ra, os, &drro);
1095 			break;
1096 		}
1097 		case DRR_FREEOBJECTS:
1098 		{
1099 			struct drr_freeobjects drrfo =
1100 			    drr->drr_u.drr_freeobjects;
1101 			ra.err = restore_freeobjects(&ra, os, &drrfo);
1102 			break;
1103 		}
1104 		case DRR_WRITE:
1105 		{
1106 			struct drr_write drrw = drr->drr_u.drr_write;
1107 			ra.err = restore_write(&ra, os, &drrw);
1108 			break;
1109 		}
1110 		case DRR_FREE:
1111 		{
1112 			struct drr_free drrf = drr->drr_u.drr_free;
1113 			ra.err = restore_free(&ra, os, &drrf);
1114 			break;
1115 		}
1116 		case DRR_END:
1117 		{
1118 			struct drr_end drre = drr->drr_u.drr_end;
1119 			/*
1120 			 * We compare against the *previous* checksum
1121 			 * value, because the stored checksum is of
1122 			 * everything before the DRR_END record.
1123 			 */
1124 			if (!ZIO_CHECKSUM_EQUAL(drre.drr_checksum, pcksum))
1125 				ra.err = ECKSUM;
1126 			goto out;
1127 		}
1128 		default:
1129 			ra.err = EINVAL;
1130 			goto out;
1131 		}
1132 		pcksum = ra.cksum;
1133 	}
1134 	ASSERT(ra.err != 0);
1135 
1136 out:
1137 	dmu_objset_close(os);
1138 
1139 	if (ra.err != 0) {
1140 		/*
1141 		 * rollback or destroy what we created, so we don't
1142 		 * leave it in the restoring state.
1143 		 */
1144 		txg_wait_synced(drc->drc_real_ds->ds_dir->dd_pool, 0);
1145 		dmu_recv_abort_cleanup(drc);
1146 	}
1147 
1148 	kmem_free(ra.buf, ra.bufsize);
1149 	*voffp = ra.voff;
1150 	return (ra.err);
1151 }
1152 
1153 struct recvendsyncarg {
1154 	char *tosnap;
1155 	uint64_t creation_time;
1156 	uint64_t toguid;
1157 };
1158 
1159 static int
1160 recv_end_check(void *arg1, void *arg2, dmu_tx_t *tx)
1161 {
1162 	dsl_dataset_t *ds = arg1;
1163 	struct recvendsyncarg *resa = arg2;
1164 
1165 	return (dsl_dataset_snapshot_check(ds, resa->tosnap, tx));
1166 }
1167 
1168 static void
1169 recv_end_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
1170 {
1171 	dsl_dataset_t *ds = arg1;
1172 	struct recvendsyncarg *resa = arg2;
1173 
1174 	dsl_dataset_snapshot_sync(ds, resa->tosnap, cr, tx);
1175 
1176 	/* set snapshot's creation time and guid */
1177 	dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1178 	ds->ds_prev->ds_phys->ds_creation_time = resa->creation_time;
1179 	ds->ds_prev->ds_phys->ds_guid = resa->toguid;
1180 	ds->ds_prev->ds_phys->ds_flags &= ~DS_FLAG_INCONSISTENT;
1181 
1182 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1183 	ds->ds_phys->ds_flags &= ~DS_FLAG_INCONSISTENT;
1184 }
1185 
1186 int
1187 dmu_recv_end(dmu_recv_cookie_t *drc)
1188 {
1189 	int err = 0;
1190 	int lmode;
1191 
1192 	/*
1193 	 * XXX hack; seems the ds is still dirty and
1194 	 * dsl_pool_zil_clean() expects it to have a ds_user_ptr (and
1195 	 * zil), but clone_swap() can close it.
1196 	 */
1197 	txg_wait_synced(drc->drc_real_ds->ds_dir->dd_pool, 0);
1198 
1199 	if (dsl_dataset_tryupgrade(drc->drc_real_ds,
1200 	    DS_MODE_PRIMARY, DS_MODE_EXCLUSIVE)) {
1201 		lmode = DS_MODE_EXCLUSIVE;
1202 	} else {
1203 		dmu_recv_abort_cleanup(drc);
1204 		return (EBUSY);
1205 	}
1206 
1207 	if (drc->drc_logical_ds != drc->drc_real_ds) {
1208 		if (err == 0 && dsl_dataset_tryupgrade(drc->drc_logical_ds,
1209 		    DS_MODE_STANDARD, DS_MODE_EXCLUSIVE)) {
1210 			lmode = DS_MODE_EXCLUSIVE;
1211 			err = dsl_dataset_clone_swap(drc->drc_real_ds,
1212 			    drc->drc_logical_ds, drc->drc_force);
1213 		} else {
1214 			lmode = DS_MODE_STANDARD;
1215 			err = EBUSY;
1216 		}
1217 	}
1218 
1219 	if (err == 0) {
1220 		struct recvendsyncarg resa;
1221 
1222 		resa.creation_time = drc->drc_drrb->drr_creation_time;
1223 		resa.toguid = drc->drc_drrb->drr_toguid;
1224 		resa.tosnap = drc->drc_tosnap;
1225 
1226 		err = dsl_sync_task_do(drc->drc_real_ds->ds_dir->dd_pool,
1227 		    recv_end_check, recv_end_sync,
1228 		    drc->drc_logical_ds, &resa, 3);
1229 		if (err) {
1230 			if (drc->drc_newfs) {
1231 				ASSERT(drc->drc_logical_ds == drc->drc_real_ds);
1232 				(void) dsl_dataset_destroy(drc->drc_real_ds,
1233 				    dmu_recv_tag);
1234 				return (err);
1235 			} else {
1236 				(void) dsl_dataset_rollback(drc->drc_logical_ds,
1237 				    DMU_OST_NONE);
1238 			}
1239 		}
1240 	}
1241 
1242 	if (drc->drc_logical_ds != drc->drc_real_ds) {
1243 		/* dsl_dataset_destroy() will close the ds */
1244 		(void) dsl_dataset_destroy(drc->drc_real_ds, dmu_recv_tag);
1245 	}
1246 	/* close the hold from dmu_recv_begin */
1247 	dsl_dataset_close(drc->drc_logical_ds, lmode, dmu_recv_tag);
1248 	return (err);
1249 }
1250