xref: /titanic_51/usr/src/uts/common/fs/zfs/dmu_send.c (revision b59c4a48daf5a1863ecac763711b497b2f8321e4)
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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24  * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
25  * Copyright (c) 2014, Joyent, Inc. All rights reserved.
26  * Copyright 2014 HybridCluster. All rights reserved.
27  */
28 
29 #include <sys/dmu.h>
30 #include <sys/dmu_impl.h>
31 #include <sys/dmu_tx.h>
32 #include <sys/dbuf.h>
33 #include <sys/dnode.h>
34 #include <sys/zfs_context.h>
35 #include <sys/dmu_objset.h>
36 #include <sys/dmu_traverse.h>
37 #include <sys/dsl_dataset.h>
38 #include <sys/dsl_dir.h>
39 #include <sys/dsl_prop.h>
40 #include <sys/dsl_pool.h>
41 #include <sys/dsl_synctask.h>
42 #include <sys/zfs_ioctl.h>
43 #include <sys/zap.h>
44 #include <sys/zio_checksum.h>
45 #include <sys/zfs_znode.h>
46 #include <zfs_fletcher.h>
47 #include <sys/avl.h>
48 #include <sys/ddt.h>
49 #include <sys/zfs_onexit.h>
50 #include <sys/dmu_send.h>
51 #include <sys/dsl_destroy.h>
52 #include <sys/blkptr.h>
53 #include <sys/dsl_bookmark.h>
54 #include <sys/zfeature.h>
55 
56 /* Set this tunable to TRUE to replace corrupt data with 0x2f5baddb10c */
57 int zfs_send_corrupt_data = B_FALSE;
58 
59 static char *dmu_recv_tag = "dmu_recv_tag";
60 static const char *recv_clone_name = "%recv";
61 
62 static int
63 dump_bytes(dmu_sendarg_t *dsp, void *buf, int len)
64 {
65 	dsl_dataset_t *ds = dsp->dsa_os->os_dsl_dataset;
66 	ssize_t resid; /* have to get resid to get detailed errno */
67 	ASSERT0(len % 8);
68 
69 	fletcher_4_incremental_native(buf, len, &dsp->dsa_zc);
70 	dsp->dsa_err = vn_rdwr(UIO_WRITE, dsp->dsa_vp,
71 	    (caddr_t)buf, len,
72 	    0, UIO_SYSSPACE, FAPPEND, RLIM64_INFINITY, CRED(), &resid);
73 
74 	mutex_enter(&ds->ds_sendstream_lock);
75 	*dsp->dsa_off += len;
76 	mutex_exit(&ds->ds_sendstream_lock);
77 
78 	return (dsp->dsa_err);
79 }
80 
81 static int
82 dump_free(dmu_sendarg_t *dsp, uint64_t object, uint64_t offset,
83     uint64_t length)
84 {
85 	struct drr_free *drrf = &(dsp->dsa_drr->drr_u.drr_free);
86 
87 	/*
88 	 * When we receive a free record, dbuf_free_range() assumes
89 	 * that the receiving system doesn't have any dbufs in the range
90 	 * being freed.  This is always true because there is a one-record
91 	 * constraint: we only send one WRITE record for any given
92 	 * object+offset.  We know that the one-record constraint is
93 	 * true because we always send data in increasing order by
94 	 * object,offset.
95 	 *
96 	 * If the increasing-order constraint ever changes, we should find
97 	 * another way to assert that the one-record constraint is still
98 	 * satisfied.
99 	 */
100 	ASSERT(object > dsp->dsa_last_data_object ||
101 	    (object == dsp->dsa_last_data_object &&
102 	    offset > dsp->dsa_last_data_offset));
103 
104 	/*
105 	 * If we are doing a non-incremental send, then there can't
106 	 * be any data in the dataset we're receiving into.  Therefore
107 	 * a free record would simply be a no-op.  Save space by not
108 	 * sending it to begin with.
109 	 */
110 	if (!dsp->dsa_incremental)
111 		return (0);
112 
113 	if (length != -1ULL && offset + length < offset)
114 		length = -1ULL;
115 
116 	/*
117 	 * If there is a pending op, but it's not PENDING_FREE, push it out,
118 	 * since free block aggregation can only be done for blocks of the
119 	 * same type (i.e., DRR_FREE records can only be aggregated with
120 	 * other DRR_FREE records.  DRR_FREEOBJECTS records can only be
121 	 * aggregated with other DRR_FREEOBJECTS records.
122 	 */
123 	if (dsp->dsa_pending_op != PENDING_NONE &&
124 	    dsp->dsa_pending_op != PENDING_FREE) {
125 		if (dump_bytes(dsp, dsp->dsa_drr,
126 		    sizeof (dmu_replay_record_t)) != 0)
127 			return (SET_ERROR(EINTR));
128 		dsp->dsa_pending_op = PENDING_NONE;
129 	}
130 
131 	if (dsp->dsa_pending_op == PENDING_FREE) {
132 		/*
133 		 * There should never be a PENDING_FREE if length is -1
134 		 * (because dump_dnode is the only place where this
135 		 * function is called with a -1, and only after flushing
136 		 * any pending record).
137 		 */
138 		ASSERT(length != -1ULL);
139 		/*
140 		 * Check to see whether this free block can be aggregated
141 		 * with pending one.
142 		 */
143 		if (drrf->drr_object == object && drrf->drr_offset +
144 		    drrf->drr_length == offset) {
145 			drrf->drr_length += length;
146 			return (0);
147 		} else {
148 			/* not a continuation.  Push out pending record */
149 			if (dump_bytes(dsp, dsp->dsa_drr,
150 			    sizeof (dmu_replay_record_t)) != 0)
151 				return (SET_ERROR(EINTR));
152 			dsp->dsa_pending_op = PENDING_NONE;
153 		}
154 	}
155 	/* create a FREE record and make it pending */
156 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
157 	dsp->dsa_drr->drr_type = DRR_FREE;
158 	drrf->drr_object = object;
159 	drrf->drr_offset = offset;
160 	drrf->drr_length = length;
161 	drrf->drr_toguid = dsp->dsa_toguid;
162 	if (length == -1ULL) {
163 		if (dump_bytes(dsp, dsp->dsa_drr,
164 		    sizeof (dmu_replay_record_t)) != 0)
165 			return (SET_ERROR(EINTR));
166 	} else {
167 		dsp->dsa_pending_op = PENDING_FREE;
168 	}
169 
170 	return (0);
171 }
172 
173 static int
174 dump_write(dmu_sendarg_t *dsp, dmu_object_type_t type,
175     uint64_t object, uint64_t offset, int blksz, const blkptr_t *bp, void *data)
176 {
177 	struct drr_write *drrw = &(dsp->dsa_drr->drr_u.drr_write);
178 
179 	/*
180 	 * We send data in increasing object, offset order.
181 	 * See comment in dump_free() for details.
182 	 */
183 	ASSERT(object > dsp->dsa_last_data_object ||
184 	    (object == dsp->dsa_last_data_object &&
185 	    offset > dsp->dsa_last_data_offset));
186 	dsp->dsa_last_data_object = object;
187 	dsp->dsa_last_data_offset = offset + blksz - 1;
188 
189 	/*
190 	 * If there is any kind of pending aggregation (currently either
191 	 * a grouping of free objects or free blocks), push it out to
192 	 * the stream, since aggregation can't be done across operations
193 	 * of different types.
194 	 */
195 	if (dsp->dsa_pending_op != PENDING_NONE) {
196 		if (dump_bytes(dsp, dsp->dsa_drr,
197 		    sizeof (dmu_replay_record_t)) != 0)
198 			return (SET_ERROR(EINTR));
199 		dsp->dsa_pending_op = PENDING_NONE;
200 	}
201 	/* write a DATA record */
202 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
203 	dsp->dsa_drr->drr_type = DRR_WRITE;
204 	drrw->drr_object = object;
205 	drrw->drr_type = type;
206 	drrw->drr_offset = offset;
207 	drrw->drr_length = blksz;
208 	drrw->drr_toguid = dsp->dsa_toguid;
209 	if (BP_IS_EMBEDDED(bp)) {
210 		/*
211 		 * There's no pre-computed checksum of embedded BP's, so
212 		 * (like fletcher4-checkummed blocks) userland will have
213 		 * to compute a dedup-capable checksum itself.
214 		 */
215 		drrw->drr_checksumtype = ZIO_CHECKSUM_OFF;
216 	} else {
217 		drrw->drr_checksumtype = BP_GET_CHECKSUM(bp);
218 		if (zio_checksum_table[drrw->drr_checksumtype].ci_dedup)
219 			drrw->drr_checksumflags |= DRR_CHECKSUM_DEDUP;
220 		DDK_SET_LSIZE(&drrw->drr_key, BP_GET_LSIZE(bp));
221 		DDK_SET_PSIZE(&drrw->drr_key, BP_GET_PSIZE(bp));
222 		DDK_SET_COMPRESS(&drrw->drr_key, BP_GET_COMPRESS(bp));
223 		drrw->drr_key.ddk_cksum = bp->blk_cksum;
224 	}
225 
226 	if (dump_bytes(dsp, dsp->dsa_drr, sizeof (dmu_replay_record_t)) != 0)
227 		return (SET_ERROR(EINTR));
228 	if (dump_bytes(dsp, data, blksz) != 0)
229 		return (SET_ERROR(EINTR));
230 	return (0);
231 }
232 
233 static int
234 dump_write_embedded(dmu_sendarg_t *dsp, uint64_t object, uint64_t offset,
235     int blksz, const blkptr_t *bp)
236 {
237 	char buf[BPE_PAYLOAD_SIZE];
238 	struct drr_write_embedded *drrw =
239 	    &(dsp->dsa_drr->drr_u.drr_write_embedded);
240 
241 	if (dsp->dsa_pending_op != PENDING_NONE) {
242 		if (dump_bytes(dsp, dsp->dsa_drr,
243 		    sizeof (dmu_replay_record_t)) != 0)
244 			return (EINTR);
245 		dsp->dsa_pending_op = PENDING_NONE;
246 	}
247 
248 	ASSERT(BP_IS_EMBEDDED(bp));
249 
250 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
251 	dsp->dsa_drr->drr_type = DRR_WRITE_EMBEDDED;
252 	drrw->drr_object = object;
253 	drrw->drr_offset = offset;
254 	drrw->drr_length = blksz;
255 	drrw->drr_toguid = dsp->dsa_toguid;
256 	drrw->drr_compression = BP_GET_COMPRESS(bp);
257 	drrw->drr_etype = BPE_GET_ETYPE(bp);
258 	drrw->drr_lsize = BPE_GET_LSIZE(bp);
259 	drrw->drr_psize = BPE_GET_PSIZE(bp);
260 
261 	decode_embedded_bp_compressed(bp, buf);
262 
263 	if (dump_bytes(dsp, dsp->dsa_drr, sizeof (dmu_replay_record_t)) != 0)
264 		return (EINTR);
265 	if (dump_bytes(dsp, buf, P2ROUNDUP(drrw->drr_psize, 8)) != 0)
266 		return (EINTR);
267 	return (0);
268 }
269 
270 static int
271 dump_spill(dmu_sendarg_t *dsp, uint64_t object, int blksz, void *data)
272 {
273 	struct drr_spill *drrs = &(dsp->dsa_drr->drr_u.drr_spill);
274 
275 	if (dsp->dsa_pending_op != PENDING_NONE) {
276 		if (dump_bytes(dsp, dsp->dsa_drr,
277 		    sizeof (dmu_replay_record_t)) != 0)
278 			return (SET_ERROR(EINTR));
279 		dsp->dsa_pending_op = PENDING_NONE;
280 	}
281 
282 	/* write a SPILL record */
283 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
284 	dsp->dsa_drr->drr_type = DRR_SPILL;
285 	drrs->drr_object = object;
286 	drrs->drr_length = blksz;
287 	drrs->drr_toguid = dsp->dsa_toguid;
288 
289 	if (dump_bytes(dsp, dsp->dsa_drr, sizeof (dmu_replay_record_t)))
290 		return (SET_ERROR(EINTR));
291 	if (dump_bytes(dsp, data, blksz))
292 		return (SET_ERROR(EINTR));
293 	return (0);
294 }
295 
296 static int
297 dump_freeobjects(dmu_sendarg_t *dsp, uint64_t firstobj, uint64_t numobjs)
298 {
299 	struct drr_freeobjects *drrfo = &(dsp->dsa_drr->drr_u.drr_freeobjects);
300 
301 	/* See comment in dump_free(). */
302 	if (!dsp->dsa_incremental)
303 		return (0);
304 
305 	/*
306 	 * If there is a pending op, but it's not PENDING_FREEOBJECTS,
307 	 * push it out, since free block aggregation can only be done for
308 	 * blocks of the same type (i.e., DRR_FREE records can only be
309 	 * aggregated with other DRR_FREE records.  DRR_FREEOBJECTS records
310 	 * can only be aggregated with other DRR_FREEOBJECTS records.
311 	 */
312 	if (dsp->dsa_pending_op != PENDING_NONE &&
313 	    dsp->dsa_pending_op != PENDING_FREEOBJECTS) {
314 		if (dump_bytes(dsp, dsp->dsa_drr,
315 		    sizeof (dmu_replay_record_t)) != 0)
316 			return (SET_ERROR(EINTR));
317 		dsp->dsa_pending_op = PENDING_NONE;
318 	}
319 	if (dsp->dsa_pending_op == PENDING_FREEOBJECTS) {
320 		/*
321 		 * See whether this free object array can be aggregated
322 		 * with pending one
323 		 */
324 		if (drrfo->drr_firstobj + drrfo->drr_numobjs == firstobj) {
325 			drrfo->drr_numobjs += numobjs;
326 			return (0);
327 		} else {
328 			/* can't be aggregated.  Push out pending record */
329 			if (dump_bytes(dsp, dsp->dsa_drr,
330 			    sizeof (dmu_replay_record_t)) != 0)
331 				return (SET_ERROR(EINTR));
332 			dsp->dsa_pending_op = PENDING_NONE;
333 		}
334 	}
335 
336 	/* write a FREEOBJECTS record */
337 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
338 	dsp->dsa_drr->drr_type = DRR_FREEOBJECTS;
339 	drrfo->drr_firstobj = firstobj;
340 	drrfo->drr_numobjs = numobjs;
341 	drrfo->drr_toguid = dsp->dsa_toguid;
342 
343 	dsp->dsa_pending_op = PENDING_FREEOBJECTS;
344 
345 	return (0);
346 }
347 
348 static int
349 dump_dnode(dmu_sendarg_t *dsp, uint64_t object, dnode_phys_t *dnp)
350 {
351 	struct drr_object *drro = &(dsp->dsa_drr->drr_u.drr_object);
352 
353 	if (dnp == NULL || dnp->dn_type == DMU_OT_NONE)
354 		return (dump_freeobjects(dsp, object, 1));
355 
356 	if (dsp->dsa_pending_op != PENDING_NONE) {
357 		if (dump_bytes(dsp, dsp->dsa_drr,
358 		    sizeof (dmu_replay_record_t)) != 0)
359 			return (SET_ERROR(EINTR));
360 		dsp->dsa_pending_op = PENDING_NONE;
361 	}
362 
363 	/* write an OBJECT record */
364 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
365 	dsp->dsa_drr->drr_type = DRR_OBJECT;
366 	drro->drr_object = object;
367 	drro->drr_type = dnp->dn_type;
368 	drro->drr_bonustype = dnp->dn_bonustype;
369 	drro->drr_blksz = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
370 	drro->drr_bonuslen = dnp->dn_bonuslen;
371 	drro->drr_checksumtype = dnp->dn_checksum;
372 	drro->drr_compress = dnp->dn_compress;
373 	drro->drr_toguid = dsp->dsa_toguid;
374 
375 	if (dump_bytes(dsp, dsp->dsa_drr, sizeof (dmu_replay_record_t)) != 0)
376 		return (SET_ERROR(EINTR));
377 
378 	if (dump_bytes(dsp, DN_BONUS(dnp), P2ROUNDUP(dnp->dn_bonuslen, 8)) != 0)
379 		return (SET_ERROR(EINTR));
380 
381 	/* Free anything past the end of the file. */
382 	if (dump_free(dsp, object, (dnp->dn_maxblkid + 1) *
383 	    (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT), -1ULL) != 0)
384 		return (SET_ERROR(EINTR));
385 	if (dsp->dsa_err != 0)
386 		return (SET_ERROR(EINTR));
387 	return (0);
388 }
389 
390 static boolean_t
391 backup_do_embed(dmu_sendarg_t *dsp, const blkptr_t *bp)
392 {
393 	if (!BP_IS_EMBEDDED(bp))
394 		return (B_FALSE);
395 
396 	/*
397 	 * Compression function must be legacy, or explicitly enabled.
398 	 */
399 	if ((BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_LEGACY_FUNCTIONS &&
400 	    !(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_EMBED_DATA_LZ4)))
401 		return (B_FALSE);
402 
403 	/*
404 	 * Embed type must be explicitly enabled.
405 	 */
406 	switch (BPE_GET_ETYPE(bp)) {
407 	case BP_EMBEDDED_TYPE_DATA:
408 		if (dsp->dsa_featureflags & DMU_BACKUP_FEATURE_EMBED_DATA)
409 			return (B_TRUE);
410 		break;
411 	default:
412 		return (B_FALSE);
413 	}
414 	return (B_FALSE);
415 }
416 
417 #define	BP_SPAN(dnp, level) \
418 	(((uint64_t)dnp->dn_datablkszsec) << (SPA_MINBLOCKSHIFT + \
419 	(level) * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT)))
420 
421 /* ARGSUSED */
422 static int
423 backup_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
424     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
425 {
426 	dmu_sendarg_t *dsp = arg;
427 	dmu_object_type_t type = bp ? BP_GET_TYPE(bp) : DMU_OT_NONE;
428 	int err = 0;
429 
430 	if (issig(JUSTLOOKING) && issig(FORREAL))
431 		return (SET_ERROR(EINTR));
432 
433 	if (zb->zb_object != DMU_META_DNODE_OBJECT &&
434 	    DMU_OBJECT_IS_SPECIAL(zb->zb_object)) {
435 		return (0);
436 	} else if (zb->zb_level == ZB_ZIL_LEVEL) {
437 		/*
438 		 * If we are sending a non-snapshot (which is allowed on
439 		 * read-only pools), it may have a ZIL, which must be ignored.
440 		 */
441 		return (0);
442 	} else if (BP_IS_HOLE(bp) &&
443 	    zb->zb_object == DMU_META_DNODE_OBJECT) {
444 		uint64_t span = BP_SPAN(dnp, zb->zb_level);
445 		uint64_t dnobj = (zb->zb_blkid * span) >> DNODE_SHIFT;
446 		err = dump_freeobjects(dsp, dnobj, span >> DNODE_SHIFT);
447 	} else if (BP_IS_HOLE(bp)) {
448 		uint64_t span = BP_SPAN(dnp, zb->zb_level);
449 		err = dump_free(dsp, zb->zb_object, zb->zb_blkid * span, span);
450 	} else if (zb->zb_level > 0 || type == DMU_OT_OBJSET) {
451 		return (0);
452 	} else if (type == DMU_OT_DNODE) {
453 		dnode_phys_t *blk;
454 		int i;
455 		int blksz = BP_GET_LSIZE(bp);
456 		uint32_t aflags = ARC_WAIT;
457 		arc_buf_t *abuf;
458 
459 		if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
460 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
461 		    &aflags, zb) != 0)
462 			return (SET_ERROR(EIO));
463 
464 		blk = abuf->b_data;
465 		for (i = 0; i < blksz >> DNODE_SHIFT; i++) {
466 			uint64_t dnobj = (zb->zb_blkid <<
467 			    (DNODE_BLOCK_SHIFT - DNODE_SHIFT)) + i;
468 			err = dump_dnode(dsp, dnobj, blk+i);
469 			if (err != 0)
470 				break;
471 		}
472 		(void) arc_buf_remove_ref(abuf, &abuf);
473 	} else if (type == DMU_OT_SA) {
474 		uint32_t aflags = ARC_WAIT;
475 		arc_buf_t *abuf;
476 		int blksz = BP_GET_LSIZE(bp);
477 
478 		if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
479 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
480 		    &aflags, zb) != 0)
481 			return (SET_ERROR(EIO));
482 
483 		err = dump_spill(dsp, zb->zb_object, blksz, abuf->b_data);
484 		(void) arc_buf_remove_ref(abuf, &abuf);
485 	} else if (backup_do_embed(dsp, bp)) {
486 		/* it's an embedded level-0 block of a regular object */
487 		int blksz = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
488 		err = dump_write_embedded(dsp, zb->zb_object,
489 		    zb->zb_blkid * blksz, blksz, bp);
490 	} else { /* it's a level-0 block of a regular object */
491 		uint32_t aflags = ARC_WAIT;
492 		arc_buf_t *abuf;
493 		int blksz = BP_GET_LSIZE(bp);
494 
495 		ASSERT3U(blksz, ==, dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
496 		ASSERT0(zb->zb_level);
497 		if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
498 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
499 		    &aflags, zb) != 0) {
500 			if (zfs_send_corrupt_data) {
501 				/* Send a block filled with 0x"zfs badd bloc" */
502 				abuf = arc_buf_alloc(spa, blksz, &abuf,
503 				    ARC_BUFC_DATA);
504 				uint64_t *ptr;
505 				for (ptr = abuf->b_data;
506 				    (char *)ptr < (char *)abuf->b_data + blksz;
507 				    ptr++)
508 					*ptr = 0x2f5baddb10c;
509 			} else {
510 				return (SET_ERROR(EIO));
511 			}
512 		}
513 
514 		err = dump_write(dsp, type, zb->zb_object, zb->zb_blkid * blksz,
515 		    blksz, bp, abuf->b_data);
516 		(void) arc_buf_remove_ref(abuf, &abuf);
517 	}
518 
519 	ASSERT(err == 0 || err == EINTR);
520 	return (err);
521 }
522 
523 /*
524  * Releases dp using the specified tag.
525  */
526 static int
527 dmu_send_impl(void *tag, dsl_pool_t *dp, dsl_dataset_t *ds,
528     zfs_bookmark_phys_t *fromzb, boolean_t is_clone, boolean_t embedok,
529     int outfd, vnode_t *vp, offset_t *off)
530 {
531 	objset_t *os;
532 	dmu_replay_record_t *drr;
533 	dmu_sendarg_t *dsp;
534 	int err;
535 	uint64_t fromtxg = 0;
536 	uint64_t featureflags = 0;
537 
538 	err = dmu_objset_from_ds(ds, &os);
539 	if (err != 0) {
540 		dsl_pool_rele(dp, tag);
541 		return (err);
542 	}
543 
544 	drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP);
545 	drr->drr_type = DRR_BEGIN;
546 	drr->drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
547 	DMU_SET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo,
548 	    DMU_SUBSTREAM);
549 
550 #ifdef _KERNEL
551 	if (dmu_objset_type(os) == DMU_OST_ZFS) {
552 		uint64_t version;
553 		if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &version) != 0) {
554 			kmem_free(drr, sizeof (dmu_replay_record_t));
555 			dsl_pool_rele(dp, tag);
556 			return (SET_ERROR(EINVAL));
557 		}
558 		if (version >= ZPL_VERSION_SA) {
559 			featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
560 		}
561 	}
562 #endif
563 
564 	if (embedok &&
565 	    spa_feature_is_active(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA)) {
566 		featureflags |= DMU_BACKUP_FEATURE_EMBED_DATA;
567 		if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
568 			featureflags |= DMU_BACKUP_FEATURE_EMBED_DATA_LZ4;
569 	} else {
570 		embedok = B_FALSE;
571 	}
572 
573 	DMU_SET_FEATUREFLAGS(drr->drr_u.drr_begin.drr_versioninfo,
574 	    featureflags);
575 
576 	drr->drr_u.drr_begin.drr_creation_time =
577 	    ds->ds_phys->ds_creation_time;
578 	drr->drr_u.drr_begin.drr_type = dmu_objset_type(os);
579 	if (is_clone)
580 		drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CLONE;
581 	drr->drr_u.drr_begin.drr_toguid = ds->ds_phys->ds_guid;
582 	if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
583 		drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CI_DATA;
584 
585 	if (fromzb != NULL) {
586 		drr->drr_u.drr_begin.drr_fromguid = fromzb->zbm_guid;
587 		fromtxg = fromzb->zbm_creation_txg;
588 	}
589 	dsl_dataset_name(ds, drr->drr_u.drr_begin.drr_toname);
590 	if (!dsl_dataset_is_snapshot(ds)) {
591 		(void) strlcat(drr->drr_u.drr_begin.drr_toname, "@--head--",
592 		    sizeof (drr->drr_u.drr_begin.drr_toname));
593 	}
594 
595 	dsp = kmem_zalloc(sizeof (dmu_sendarg_t), KM_SLEEP);
596 
597 	dsp->dsa_drr = drr;
598 	dsp->dsa_vp = vp;
599 	dsp->dsa_outfd = outfd;
600 	dsp->dsa_proc = curproc;
601 	dsp->dsa_os = os;
602 	dsp->dsa_off = off;
603 	dsp->dsa_toguid = ds->ds_phys->ds_guid;
604 	ZIO_SET_CHECKSUM(&dsp->dsa_zc, 0, 0, 0, 0);
605 	dsp->dsa_pending_op = PENDING_NONE;
606 	dsp->dsa_incremental = (fromzb != NULL);
607 	dsp->dsa_featureflags = featureflags;
608 
609 	mutex_enter(&ds->ds_sendstream_lock);
610 	list_insert_head(&ds->ds_sendstreams, dsp);
611 	mutex_exit(&ds->ds_sendstream_lock);
612 
613 	dsl_dataset_long_hold(ds, FTAG);
614 	dsl_pool_rele(dp, tag);
615 
616 	if (dump_bytes(dsp, drr, sizeof (dmu_replay_record_t)) != 0) {
617 		err = dsp->dsa_err;
618 		goto out;
619 	}
620 
621 	err = traverse_dataset(ds, fromtxg, TRAVERSE_PRE | TRAVERSE_PREFETCH,
622 	    backup_cb, dsp);
623 
624 	if (dsp->dsa_pending_op != PENDING_NONE)
625 		if (dump_bytes(dsp, drr, sizeof (dmu_replay_record_t)) != 0)
626 			err = SET_ERROR(EINTR);
627 
628 	if (err != 0) {
629 		if (err == EINTR && dsp->dsa_err != 0)
630 			err = dsp->dsa_err;
631 		goto out;
632 	}
633 
634 	bzero(drr, sizeof (dmu_replay_record_t));
635 	drr->drr_type = DRR_END;
636 	drr->drr_u.drr_end.drr_checksum = dsp->dsa_zc;
637 	drr->drr_u.drr_end.drr_toguid = dsp->dsa_toguid;
638 
639 	if (dump_bytes(dsp, drr, sizeof (dmu_replay_record_t)) != 0) {
640 		err = dsp->dsa_err;
641 		goto out;
642 	}
643 
644 out:
645 	mutex_enter(&ds->ds_sendstream_lock);
646 	list_remove(&ds->ds_sendstreams, dsp);
647 	mutex_exit(&ds->ds_sendstream_lock);
648 
649 	kmem_free(drr, sizeof (dmu_replay_record_t));
650 	kmem_free(dsp, sizeof (dmu_sendarg_t));
651 
652 	dsl_dataset_long_rele(ds, FTAG);
653 
654 	return (err);
655 }
656 
657 int
658 dmu_send_obj(const char *pool, uint64_t tosnap, uint64_t fromsnap,
659     boolean_t embedok, int outfd, vnode_t *vp, offset_t *off)
660 {
661 	dsl_pool_t *dp;
662 	dsl_dataset_t *ds;
663 	dsl_dataset_t *fromds = NULL;
664 	int err;
665 
666 	err = dsl_pool_hold(pool, FTAG, &dp);
667 	if (err != 0)
668 		return (err);
669 
670 	err = dsl_dataset_hold_obj(dp, tosnap, FTAG, &ds);
671 	if (err != 0) {
672 		dsl_pool_rele(dp, FTAG);
673 		return (err);
674 	}
675 
676 	if (fromsnap != 0) {
677 		zfs_bookmark_phys_t zb;
678 		boolean_t is_clone;
679 
680 		err = dsl_dataset_hold_obj(dp, fromsnap, FTAG, &fromds);
681 		if (err != 0) {
682 			dsl_dataset_rele(ds, FTAG);
683 			dsl_pool_rele(dp, FTAG);
684 			return (err);
685 		}
686 		if (!dsl_dataset_is_before(ds, fromds, 0))
687 			err = SET_ERROR(EXDEV);
688 		zb.zbm_creation_time = fromds->ds_phys->ds_creation_time;
689 		zb.zbm_creation_txg = fromds->ds_phys->ds_creation_txg;
690 		zb.zbm_guid = fromds->ds_phys->ds_guid;
691 		is_clone = (fromds->ds_dir != ds->ds_dir);
692 		dsl_dataset_rele(fromds, FTAG);
693 		err = dmu_send_impl(FTAG, dp, ds, &zb, is_clone, embedok,
694 		    outfd, vp, off);
695 	} else {
696 		err = dmu_send_impl(FTAG, dp, ds, NULL, B_FALSE, embedok,
697 		    outfd, vp, off);
698 	}
699 	dsl_dataset_rele(ds, FTAG);
700 	return (err);
701 }
702 
703 int
704 dmu_send(const char *tosnap, const char *fromsnap, boolean_t embedok,
705     int outfd, vnode_t *vp, offset_t *off)
706 {
707 	dsl_pool_t *dp;
708 	dsl_dataset_t *ds;
709 	int err;
710 	boolean_t owned = B_FALSE;
711 
712 	if (fromsnap != NULL && strpbrk(fromsnap, "@#") == NULL)
713 		return (SET_ERROR(EINVAL));
714 
715 	err = dsl_pool_hold(tosnap, FTAG, &dp);
716 	if (err != 0)
717 		return (err);
718 
719 	if (strchr(tosnap, '@') == NULL && spa_writeable(dp->dp_spa)) {
720 		/*
721 		 * We are sending a filesystem or volume.  Ensure
722 		 * that it doesn't change by owning the dataset.
723 		 */
724 		err = dsl_dataset_own(dp, tosnap, FTAG, &ds);
725 		owned = B_TRUE;
726 	} else {
727 		err = dsl_dataset_hold(dp, tosnap, FTAG, &ds);
728 	}
729 	if (err != 0) {
730 		dsl_pool_rele(dp, FTAG);
731 		return (err);
732 	}
733 
734 	if (fromsnap != NULL) {
735 		zfs_bookmark_phys_t zb;
736 		boolean_t is_clone = B_FALSE;
737 		int fsnamelen = strchr(tosnap, '@') - tosnap;
738 
739 		/*
740 		 * If the fromsnap is in a different filesystem, then
741 		 * mark the send stream as a clone.
742 		 */
743 		if (strncmp(tosnap, fromsnap, fsnamelen) != 0 ||
744 		    (fromsnap[fsnamelen] != '@' &&
745 		    fromsnap[fsnamelen] != '#')) {
746 			is_clone = B_TRUE;
747 		}
748 
749 		if (strchr(fromsnap, '@')) {
750 			dsl_dataset_t *fromds;
751 			err = dsl_dataset_hold(dp, fromsnap, FTAG, &fromds);
752 			if (err == 0) {
753 				if (!dsl_dataset_is_before(ds, fromds, 0))
754 					err = SET_ERROR(EXDEV);
755 				zb.zbm_creation_time =
756 				    fromds->ds_phys->ds_creation_time;
757 				zb.zbm_creation_txg =
758 				    fromds->ds_phys->ds_creation_txg;
759 				zb.zbm_guid = fromds->ds_phys->ds_guid;
760 				is_clone = (ds->ds_dir != fromds->ds_dir);
761 				dsl_dataset_rele(fromds, FTAG);
762 			}
763 		} else {
764 			err = dsl_bookmark_lookup(dp, fromsnap, ds, &zb);
765 		}
766 		if (err != 0) {
767 			dsl_dataset_rele(ds, FTAG);
768 			dsl_pool_rele(dp, FTAG);
769 			return (err);
770 		}
771 		err = dmu_send_impl(FTAG, dp, ds, &zb, is_clone, embedok,
772 		    outfd, vp, off);
773 	} else {
774 		err = dmu_send_impl(FTAG, dp, ds, NULL, B_FALSE, embedok,
775 		    outfd, vp, off);
776 	}
777 	if (owned)
778 		dsl_dataset_disown(ds, FTAG);
779 	else
780 		dsl_dataset_rele(ds, FTAG);
781 	return (err);
782 }
783 
784 int
785 dmu_send_estimate(dsl_dataset_t *ds, dsl_dataset_t *fromds, uint64_t *sizep)
786 {
787 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
788 	int err;
789 	uint64_t size;
790 
791 	ASSERT(dsl_pool_config_held(dp));
792 
793 	/* tosnap must be a snapshot */
794 	if (!dsl_dataset_is_snapshot(ds))
795 		return (SET_ERROR(EINVAL));
796 
797 	/*
798 	 * fromsnap must be an earlier snapshot from the same fs as tosnap,
799 	 * or the origin's fs.
800 	 */
801 	if (fromds != NULL && !dsl_dataset_is_before(ds, fromds, 0))
802 		return (SET_ERROR(EXDEV));
803 
804 	/* Get uncompressed size estimate of changed data. */
805 	if (fromds == NULL) {
806 		size = ds->ds_phys->ds_uncompressed_bytes;
807 	} else {
808 		uint64_t used, comp;
809 		err = dsl_dataset_space_written(fromds, ds,
810 		    &used, &comp, &size);
811 		if (err != 0)
812 			return (err);
813 	}
814 
815 	/*
816 	 * Assume that space (both on-disk and in-stream) is dominated by
817 	 * data.  We will adjust for indirect blocks and the copies property,
818 	 * but ignore per-object space used (eg, dnodes and DRR_OBJECT records).
819 	 */
820 
821 	/*
822 	 * Subtract out approximate space used by indirect blocks.
823 	 * Assume most space is used by data blocks (non-indirect, non-dnode).
824 	 * Assume all blocks are recordsize.  Assume ditto blocks and
825 	 * internal fragmentation counter out compression.
826 	 *
827 	 * Therefore, space used by indirect blocks is sizeof(blkptr_t) per
828 	 * block, which we observe in practice.
829 	 */
830 	uint64_t recordsize;
831 	err = dsl_prop_get_int_ds(ds, "recordsize", &recordsize);
832 	if (err != 0)
833 		return (err);
834 	size -= size / recordsize * sizeof (blkptr_t);
835 
836 	/* Add in the space for the record associated with each block. */
837 	size += size / recordsize * sizeof (dmu_replay_record_t);
838 
839 	*sizep = size;
840 
841 	return (0);
842 }
843 
844 typedef struct dmu_recv_begin_arg {
845 	const char *drba_origin;
846 	dmu_recv_cookie_t *drba_cookie;
847 	cred_t *drba_cred;
848 	uint64_t drba_snapobj;
849 } dmu_recv_begin_arg_t;
850 
851 static int
852 recv_begin_check_existing_impl(dmu_recv_begin_arg_t *drba, dsl_dataset_t *ds,
853     uint64_t fromguid)
854 {
855 	uint64_t val;
856 	int error;
857 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
858 
859 	/* temporary clone name must not exist */
860 	error = zap_lookup(dp->dp_meta_objset,
861 	    ds->ds_dir->dd_phys->dd_child_dir_zapobj, recv_clone_name,
862 	    8, 1, &val);
863 	if (error != ENOENT)
864 		return (error == 0 ? EBUSY : error);
865 
866 	/* new snapshot name must not exist */
867 	error = zap_lookup(dp->dp_meta_objset,
868 	    ds->ds_phys->ds_snapnames_zapobj, drba->drba_cookie->drc_tosnap,
869 	    8, 1, &val);
870 	if (error != ENOENT)
871 		return (error == 0 ? EEXIST : error);
872 
873 	/*
874 	 * Check snapshot limit before receiving. We'll recheck again at the
875 	 * end, but might as well abort before receiving if we're already over
876 	 * the limit.
877 	 *
878 	 * Note that we do not check the file system limit with
879 	 * dsl_dir_fscount_check because the temporary %clones don't count
880 	 * against that limit.
881 	 */
882 	error = dsl_fs_ss_limit_check(ds->ds_dir, 1, ZFS_PROP_SNAPSHOT_LIMIT,
883 	    NULL, drba->drba_cred);
884 	if (error != 0)
885 		return (error);
886 
887 	if (fromguid != 0) {
888 		dsl_dataset_t *snap;
889 		uint64_t obj = ds->ds_phys->ds_prev_snap_obj;
890 
891 		/* Find snapshot in this dir that matches fromguid. */
892 		while (obj != 0) {
893 			error = dsl_dataset_hold_obj(dp, obj, FTAG,
894 			    &snap);
895 			if (error != 0)
896 				return (SET_ERROR(ENODEV));
897 			if (snap->ds_dir != ds->ds_dir) {
898 				dsl_dataset_rele(snap, FTAG);
899 				return (SET_ERROR(ENODEV));
900 			}
901 			if (snap->ds_phys->ds_guid == fromguid)
902 				break;
903 			obj = snap->ds_phys->ds_prev_snap_obj;
904 			dsl_dataset_rele(snap, FTAG);
905 		}
906 		if (obj == 0)
907 			return (SET_ERROR(ENODEV));
908 
909 		if (drba->drba_cookie->drc_force) {
910 			drba->drba_snapobj = obj;
911 		} else {
912 			/*
913 			 * If we are not forcing, there must be no
914 			 * changes since fromsnap.
915 			 */
916 			if (dsl_dataset_modified_since_snap(ds, snap)) {
917 				dsl_dataset_rele(snap, FTAG);
918 				return (SET_ERROR(ETXTBSY));
919 			}
920 			drba->drba_snapobj = ds->ds_prev->ds_object;
921 		}
922 
923 		dsl_dataset_rele(snap, FTAG);
924 	} else {
925 		/* if full, most recent snapshot must be $ORIGIN */
926 		if (ds->ds_phys->ds_prev_snap_txg >= TXG_INITIAL)
927 			return (SET_ERROR(ENODEV));
928 		drba->drba_snapobj = ds->ds_phys->ds_prev_snap_obj;
929 	}
930 
931 	return (0);
932 
933 }
934 
935 static int
936 dmu_recv_begin_check(void *arg, dmu_tx_t *tx)
937 {
938 	dmu_recv_begin_arg_t *drba = arg;
939 	dsl_pool_t *dp = dmu_tx_pool(tx);
940 	struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
941 	uint64_t fromguid = drrb->drr_fromguid;
942 	int flags = drrb->drr_flags;
943 	int error;
944 	uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
945 	dsl_dataset_t *ds;
946 	const char *tofs = drba->drba_cookie->drc_tofs;
947 
948 	/* already checked */
949 	ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
950 
951 	if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
952 	    DMU_COMPOUNDSTREAM ||
953 	    drrb->drr_type >= DMU_OST_NUMTYPES ||
954 	    ((flags & DRR_FLAG_CLONE) && drba->drba_origin == NULL))
955 		return (SET_ERROR(EINVAL));
956 
957 	/* Verify pool version supports SA if SA_SPILL feature set */
958 	if ((featureflags & DMU_BACKUP_FEATURE_SA_SPILL) &&
959 	    spa_version(dp->dp_spa) < SPA_VERSION_SA)
960 		return (SET_ERROR(ENOTSUP));
961 
962 	/*
963 	 * The receiving code doesn't know how to translate a WRITE_EMBEDDED
964 	 * record to a plan WRITE record, so the pool must have the
965 	 * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
966 	 * records.  Same with WRITE_EMBEDDED records that use LZ4 compression.
967 	 */
968 	if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) &&
969 	    !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA))
970 		return (SET_ERROR(ENOTSUP));
971 	if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA_LZ4) &&
972 	    !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
973 		return (SET_ERROR(ENOTSUP));
974 
975 	error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
976 	if (error == 0) {
977 		/* target fs already exists; recv into temp clone */
978 
979 		/* Can't recv a clone into an existing fs */
980 		if (flags & DRR_FLAG_CLONE) {
981 			dsl_dataset_rele(ds, FTAG);
982 			return (SET_ERROR(EINVAL));
983 		}
984 
985 		error = recv_begin_check_existing_impl(drba, ds, fromguid);
986 		dsl_dataset_rele(ds, FTAG);
987 	} else if (error == ENOENT) {
988 		/* target fs does not exist; must be a full backup or clone */
989 		char buf[MAXNAMELEN];
990 
991 		/*
992 		 * If it's a non-clone incremental, we are missing the
993 		 * target fs, so fail the recv.
994 		 */
995 		if (fromguid != 0 && !(flags & DRR_FLAG_CLONE))
996 			return (SET_ERROR(ENOENT));
997 
998 		/* Open the parent of tofs */
999 		ASSERT3U(strlen(tofs), <, MAXNAMELEN);
1000 		(void) strlcpy(buf, tofs, strrchr(tofs, '/') - tofs + 1);
1001 		error = dsl_dataset_hold(dp, buf, FTAG, &ds);
1002 		if (error != 0)
1003 			return (error);
1004 
1005 		/*
1006 		 * Check filesystem and snapshot limits before receiving. We'll
1007 		 * recheck snapshot limits again at the end (we create the
1008 		 * filesystems and increment those counts during begin_sync).
1009 		 */
1010 		error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
1011 		    ZFS_PROP_FILESYSTEM_LIMIT, NULL, drba->drba_cred);
1012 		if (error != 0) {
1013 			dsl_dataset_rele(ds, FTAG);
1014 			return (error);
1015 		}
1016 
1017 		error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
1018 		    ZFS_PROP_SNAPSHOT_LIMIT, NULL, drba->drba_cred);
1019 		if (error != 0) {
1020 			dsl_dataset_rele(ds, FTAG);
1021 			return (error);
1022 		}
1023 
1024 		if (drba->drba_origin != NULL) {
1025 			dsl_dataset_t *origin;
1026 			error = dsl_dataset_hold(dp, drba->drba_origin,
1027 			    FTAG, &origin);
1028 			if (error != 0) {
1029 				dsl_dataset_rele(ds, FTAG);
1030 				return (error);
1031 			}
1032 			if (!dsl_dataset_is_snapshot(origin)) {
1033 				dsl_dataset_rele(origin, FTAG);
1034 				dsl_dataset_rele(ds, FTAG);
1035 				return (SET_ERROR(EINVAL));
1036 			}
1037 			if (origin->ds_phys->ds_guid != fromguid) {
1038 				dsl_dataset_rele(origin, FTAG);
1039 				dsl_dataset_rele(ds, FTAG);
1040 				return (SET_ERROR(ENODEV));
1041 			}
1042 			dsl_dataset_rele(origin, FTAG);
1043 		}
1044 		dsl_dataset_rele(ds, FTAG);
1045 		error = 0;
1046 	}
1047 	return (error);
1048 }
1049 
1050 static void
1051 dmu_recv_begin_sync(void *arg, dmu_tx_t *tx)
1052 {
1053 	dmu_recv_begin_arg_t *drba = arg;
1054 	dsl_pool_t *dp = dmu_tx_pool(tx);
1055 	struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
1056 	const char *tofs = drba->drba_cookie->drc_tofs;
1057 	dsl_dataset_t *ds, *newds;
1058 	uint64_t dsobj;
1059 	int error;
1060 	uint64_t crflags;
1061 
1062 	crflags = (drrb->drr_flags & DRR_FLAG_CI_DATA) ?
1063 	    DS_FLAG_CI_DATASET : 0;
1064 
1065 	error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
1066 	if (error == 0) {
1067 		/* create temporary clone */
1068 		dsl_dataset_t *snap = NULL;
1069 		if (drba->drba_snapobj != 0) {
1070 			VERIFY0(dsl_dataset_hold_obj(dp,
1071 			    drba->drba_snapobj, FTAG, &snap));
1072 		}
1073 		dsobj = dsl_dataset_create_sync(ds->ds_dir, recv_clone_name,
1074 		    snap, crflags, drba->drba_cred, tx);
1075 		dsl_dataset_rele(snap, FTAG);
1076 		dsl_dataset_rele(ds, FTAG);
1077 	} else {
1078 		dsl_dir_t *dd;
1079 		const char *tail;
1080 		dsl_dataset_t *origin = NULL;
1081 
1082 		VERIFY0(dsl_dir_hold(dp, tofs, FTAG, &dd, &tail));
1083 
1084 		if (drba->drba_origin != NULL) {
1085 			VERIFY0(dsl_dataset_hold(dp, drba->drba_origin,
1086 			    FTAG, &origin));
1087 		}
1088 
1089 		/* Create new dataset. */
1090 		dsobj = dsl_dataset_create_sync(dd,
1091 		    strrchr(tofs, '/') + 1,
1092 		    origin, crflags, drba->drba_cred, tx);
1093 		if (origin != NULL)
1094 			dsl_dataset_rele(origin, FTAG);
1095 		dsl_dir_rele(dd, FTAG);
1096 		drba->drba_cookie->drc_newfs = B_TRUE;
1097 	}
1098 	VERIFY0(dsl_dataset_own_obj(dp, dsobj, dmu_recv_tag, &newds));
1099 
1100 	dmu_buf_will_dirty(newds->ds_dbuf, tx);
1101 	newds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT;
1102 
1103 	/*
1104 	 * If we actually created a non-clone, we need to create the
1105 	 * objset in our new dataset.
1106 	 */
1107 	if (BP_IS_HOLE(dsl_dataset_get_blkptr(newds))) {
1108 		(void) dmu_objset_create_impl(dp->dp_spa,
1109 		    newds, dsl_dataset_get_blkptr(newds), drrb->drr_type, tx);
1110 	}
1111 
1112 	drba->drba_cookie->drc_ds = newds;
1113 
1114 	spa_history_log_internal_ds(newds, "receive", tx, "");
1115 }
1116 
1117 /*
1118  * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
1119  * succeeds; otherwise we will leak the holds on the datasets.
1120  */
1121 int
1122 dmu_recv_begin(char *tofs, char *tosnap, struct drr_begin *drrb,
1123     boolean_t force, char *origin, dmu_recv_cookie_t *drc)
1124 {
1125 	dmu_recv_begin_arg_t drba = { 0 };
1126 	dmu_replay_record_t *drr;
1127 
1128 	bzero(drc, sizeof (dmu_recv_cookie_t));
1129 	drc->drc_drrb = drrb;
1130 	drc->drc_tosnap = tosnap;
1131 	drc->drc_tofs = tofs;
1132 	drc->drc_force = force;
1133 	drc->drc_cred = CRED();
1134 
1135 	if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC))
1136 		drc->drc_byteswap = B_TRUE;
1137 	else if (drrb->drr_magic != DMU_BACKUP_MAGIC)
1138 		return (SET_ERROR(EINVAL));
1139 
1140 	drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP);
1141 	drr->drr_type = DRR_BEGIN;
1142 	drr->drr_u.drr_begin = *drc->drc_drrb;
1143 	if (drc->drc_byteswap) {
1144 		fletcher_4_incremental_byteswap(drr,
1145 		    sizeof (dmu_replay_record_t), &drc->drc_cksum);
1146 	} else {
1147 		fletcher_4_incremental_native(drr,
1148 		    sizeof (dmu_replay_record_t), &drc->drc_cksum);
1149 	}
1150 	kmem_free(drr, sizeof (dmu_replay_record_t));
1151 
1152 	if (drc->drc_byteswap) {
1153 		drrb->drr_magic = BSWAP_64(drrb->drr_magic);
1154 		drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo);
1155 		drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
1156 		drrb->drr_type = BSWAP_32(drrb->drr_type);
1157 		drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
1158 		drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
1159 	}
1160 
1161 	drba.drba_origin = origin;
1162 	drba.drba_cookie = drc;
1163 	drba.drba_cred = CRED();
1164 
1165 	return (dsl_sync_task(tofs, dmu_recv_begin_check, dmu_recv_begin_sync,
1166 	    &drba, 5, ZFS_SPACE_CHECK_NORMAL));
1167 }
1168 
1169 struct restorearg {
1170 	int err;
1171 	boolean_t byteswap;
1172 	vnode_t *vp;
1173 	char *buf;
1174 	uint64_t voff;
1175 	int bufsize; /* amount of memory allocated for buf */
1176 	zio_cksum_t cksum;
1177 	avl_tree_t *guid_to_ds_map;
1178 };
1179 
1180 typedef struct guid_map_entry {
1181 	uint64_t	guid;
1182 	dsl_dataset_t	*gme_ds;
1183 	avl_node_t	avlnode;
1184 } guid_map_entry_t;
1185 
1186 static int
1187 guid_compare(const void *arg1, const void *arg2)
1188 {
1189 	const guid_map_entry_t *gmep1 = arg1;
1190 	const guid_map_entry_t *gmep2 = arg2;
1191 
1192 	if (gmep1->guid < gmep2->guid)
1193 		return (-1);
1194 	else if (gmep1->guid > gmep2->guid)
1195 		return (1);
1196 	return (0);
1197 }
1198 
1199 static void
1200 free_guid_map_onexit(void *arg)
1201 {
1202 	avl_tree_t *ca = arg;
1203 	void *cookie = NULL;
1204 	guid_map_entry_t *gmep;
1205 
1206 	while ((gmep = avl_destroy_nodes(ca, &cookie)) != NULL) {
1207 		dsl_dataset_long_rele(gmep->gme_ds, gmep);
1208 		dsl_dataset_rele(gmep->gme_ds, gmep);
1209 		kmem_free(gmep, sizeof (guid_map_entry_t));
1210 	}
1211 	avl_destroy(ca);
1212 	kmem_free(ca, sizeof (avl_tree_t));
1213 }
1214 
1215 static void *
1216 restore_read(struct restorearg *ra, int len, char *buf)
1217 {
1218 	int done = 0;
1219 
1220 	if (buf == NULL)
1221 		buf = ra->buf;
1222 
1223 	/* some things will require 8-byte alignment, so everything must */
1224 	ASSERT0(len % 8);
1225 
1226 	while (done < len) {
1227 		ssize_t resid;
1228 
1229 		ra->err = vn_rdwr(UIO_READ, ra->vp,
1230 		    buf + done, len - done,
1231 		    ra->voff, UIO_SYSSPACE, FAPPEND,
1232 		    RLIM64_INFINITY, CRED(), &resid);
1233 
1234 		if (resid == len - done)
1235 			ra->err = SET_ERROR(EINVAL);
1236 		ra->voff += len - done - resid;
1237 		done = len - resid;
1238 		if (ra->err != 0)
1239 			return (NULL);
1240 	}
1241 
1242 	ASSERT3U(done, ==, len);
1243 	if (ra->byteswap)
1244 		fletcher_4_incremental_byteswap(buf, len, &ra->cksum);
1245 	else
1246 		fletcher_4_incremental_native(buf, len, &ra->cksum);
1247 	return (buf);
1248 }
1249 
1250 static void
1251 backup_byteswap(dmu_replay_record_t *drr)
1252 {
1253 #define	DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
1254 #define	DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
1255 	drr->drr_type = BSWAP_32(drr->drr_type);
1256 	drr->drr_payloadlen = BSWAP_32(drr->drr_payloadlen);
1257 	switch (drr->drr_type) {
1258 	case DRR_BEGIN:
1259 		DO64(drr_begin.drr_magic);
1260 		DO64(drr_begin.drr_versioninfo);
1261 		DO64(drr_begin.drr_creation_time);
1262 		DO32(drr_begin.drr_type);
1263 		DO32(drr_begin.drr_flags);
1264 		DO64(drr_begin.drr_toguid);
1265 		DO64(drr_begin.drr_fromguid);
1266 		break;
1267 	case DRR_OBJECT:
1268 		DO64(drr_object.drr_object);
1269 		DO32(drr_object.drr_type);
1270 		DO32(drr_object.drr_bonustype);
1271 		DO32(drr_object.drr_blksz);
1272 		DO32(drr_object.drr_bonuslen);
1273 		DO64(drr_object.drr_toguid);
1274 		break;
1275 	case DRR_FREEOBJECTS:
1276 		DO64(drr_freeobjects.drr_firstobj);
1277 		DO64(drr_freeobjects.drr_numobjs);
1278 		DO64(drr_freeobjects.drr_toguid);
1279 		break;
1280 	case DRR_WRITE:
1281 		DO64(drr_write.drr_object);
1282 		DO32(drr_write.drr_type);
1283 		DO64(drr_write.drr_offset);
1284 		DO64(drr_write.drr_length);
1285 		DO64(drr_write.drr_toguid);
1286 		DO64(drr_write.drr_key.ddk_cksum.zc_word[0]);
1287 		DO64(drr_write.drr_key.ddk_cksum.zc_word[1]);
1288 		DO64(drr_write.drr_key.ddk_cksum.zc_word[2]);
1289 		DO64(drr_write.drr_key.ddk_cksum.zc_word[3]);
1290 		DO64(drr_write.drr_key.ddk_prop);
1291 		break;
1292 	case DRR_WRITE_BYREF:
1293 		DO64(drr_write_byref.drr_object);
1294 		DO64(drr_write_byref.drr_offset);
1295 		DO64(drr_write_byref.drr_length);
1296 		DO64(drr_write_byref.drr_toguid);
1297 		DO64(drr_write_byref.drr_refguid);
1298 		DO64(drr_write_byref.drr_refobject);
1299 		DO64(drr_write_byref.drr_refoffset);
1300 		DO64(drr_write_byref.drr_key.ddk_cksum.zc_word[0]);
1301 		DO64(drr_write_byref.drr_key.ddk_cksum.zc_word[1]);
1302 		DO64(drr_write_byref.drr_key.ddk_cksum.zc_word[2]);
1303 		DO64(drr_write_byref.drr_key.ddk_cksum.zc_word[3]);
1304 		DO64(drr_write_byref.drr_key.ddk_prop);
1305 		break;
1306 	case DRR_WRITE_EMBEDDED:
1307 		DO64(drr_write_embedded.drr_object);
1308 		DO64(drr_write_embedded.drr_offset);
1309 		DO64(drr_write_embedded.drr_length);
1310 		DO64(drr_write_embedded.drr_toguid);
1311 		DO32(drr_write_embedded.drr_lsize);
1312 		DO32(drr_write_embedded.drr_psize);
1313 		break;
1314 	case DRR_FREE:
1315 		DO64(drr_free.drr_object);
1316 		DO64(drr_free.drr_offset);
1317 		DO64(drr_free.drr_length);
1318 		DO64(drr_free.drr_toguid);
1319 		break;
1320 	case DRR_SPILL:
1321 		DO64(drr_spill.drr_object);
1322 		DO64(drr_spill.drr_length);
1323 		DO64(drr_spill.drr_toguid);
1324 		break;
1325 	case DRR_END:
1326 		DO64(drr_end.drr_checksum.zc_word[0]);
1327 		DO64(drr_end.drr_checksum.zc_word[1]);
1328 		DO64(drr_end.drr_checksum.zc_word[2]);
1329 		DO64(drr_end.drr_checksum.zc_word[3]);
1330 		DO64(drr_end.drr_toguid);
1331 		break;
1332 	}
1333 #undef DO64
1334 #undef DO32
1335 }
1336 
1337 static inline uint8_t
1338 deduce_nblkptr(dmu_object_type_t bonus_type, uint64_t bonus_size)
1339 {
1340 	if (bonus_type == DMU_OT_SA) {
1341 		return (1);
1342 	} else {
1343 		return (1 +
1344 		    ((DN_MAX_BONUSLEN - bonus_size) >> SPA_BLKPTRSHIFT));
1345 	}
1346 }
1347 
1348 static int
1349 restore_object(struct restorearg *ra, objset_t *os, struct drr_object *drro)
1350 {
1351 	dmu_object_info_t doi;
1352 	dmu_tx_t *tx;
1353 	void *data = NULL;
1354 	uint64_t object;
1355 	int err;
1356 
1357 	if (drro->drr_type == DMU_OT_NONE ||
1358 	    !DMU_OT_IS_VALID(drro->drr_type) ||
1359 	    !DMU_OT_IS_VALID(drro->drr_bonustype) ||
1360 	    drro->drr_checksumtype >= ZIO_CHECKSUM_FUNCTIONS ||
1361 	    drro->drr_compress >= ZIO_COMPRESS_FUNCTIONS ||
1362 	    P2PHASE(drro->drr_blksz, SPA_MINBLOCKSIZE) ||
1363 	    drro->drr_blksz < SPA_MINBLOCKSIZE ||
1364 	    drro->drr_blksz > SPA_MAXBLOCKSIZE ||
1365 	    drro->drr_bonuslen > DN_MAX_BONUSLEN) {
1366 		return (SET_ERROR(EINVAL));
1367 	}
1368 
1369 	err = dmu_object_info(os, drro->drr_object, &doi);
1370 
1371 	if (err != 0 && err != ENOENT)
1372 		return (SET_ERROR(EINVAL));
1373 	object = err == 0 ? drro->drr_object : DMU_NEW_OBJECT;
1374 
1375 	if (drro->drr_bonuslen) {
1376 		data = restore_read(ra, P2ROUNDUP(drro->drr_bonuslen, 8), NULL);
1377 		if (ra->err != 0)
1378 			return (ra->err);
1379 	}
1380 
1381 	/*
1382 	 * If we are losing blkptrs or changing the block size this must
1383 	 * be a new file instance.  We must clear out the previous file
1384 	 * contents before we can change this type of metadata in the dnode.
1385 	 */
1386 	if (err == 0) {
1387 		int nblkptr;
1388 
1389 		nblkptr = deduce_nblkptr(drro->drr_bonustype,
1390 		    drro->drr_bonuslen);
1391 
1392 		if (drro->drr_blksz != doi.doi_data_block_size ||
1393 		    nblkptr < doi.doi_nblkptr) {
1394 			err = dmu_free_long_range(os, drro->drr_object,
1395 			    0, DMU_OBJECT_END);
1396 			if (err != 0)
1397 				return (SET_ERROR(EINVAL));
1398 		}
1399 	}
1400 
1401 	tx = dmu_tx_create(os);
1402 	dmu_tx_hold_bonus(tx, object);
1403 	err = dmu_tx_assign(tx, TXG_WAIT);
1404 	if (err != 0) {
1405 		dmu_tx_abort(tx);
1406 		return (err);
1407 	}
1408 
1409 	if (object == DMU_NEW_OBJECT) {
1410 		/* currently free, want to be allocated */
1411 		err = dmu_object_claim(os, drro->drr_object,
1412 		    drro->drr_type, drro->drr_blksz,
1413 		    drro->drr_bonustype, drro->drr_bonuslen, tx);
1414 	} else if (drro->drr_type != doi.doi_type ||
1415 	    drro->drr_blksz != doi.doi_data_block_size ||
1416 	    drro->drr_bonustype != doi.doi_bonus_type ||
1417 	    drro->drr_bonuslen != doi.doi_bonus_size) {
1418 		/* currently allocated, but with different properties */
1419 		err = dmu_object_reclaim(os, drro->drr_object,
1420 		    drro->drr_type, drro->drr_blksz,
1421 		    drro->drr_bonustype, drro->drr_bonuslen, tx);
1422 	}
1423 	if (err != 0) {
1424 		dmu_tx_commit(tx);
1425 		return (SET_ERROR(EINVAL));
1426 	}
1427 
1428 	dmu_object_set_checksum(os, drro->drr_object, drro->drr_checksumtype,
1429 	    tx);
1430 	dmu_object_set_compress(os, drro->drr_object, drro->drr_compress, tx);
1431 
1432 	if (data != NULL) {
1433 		dmu_buf_t *db;
1434 
1435 		VERIFY(0 == dmu_bonus_hold(os, drro->drr_object, FTAG, &db));
1436 		dmu_buf_will_dirty(db, tx);
1437 
1438 		ASSERT3U(db->db_size, >=, drro->drr_bonuslen);
1439 		bcopy(data, db->db_data, drro->drr_bonuslen);
1440 		if (ra->byteswap) {
1441 			dmu_object_byteswap_t byteswap =
1442 			    DMU_OT_BYTESWAP(drro->drr_bonustype);
1443 			dmu_ot_byteswap[byteswap].ob_func(db->db_data,
1444 			    drro->drr_bonuslen);
1445 		}
1446 		dmu_buf_rele(db, FTAG);
1447 	}
1448 	dmu_tx_commit(tx);
1449 	return (0);
1450 }
1451 
1452 /* ARGSUSED */
1453 static int
1454 restore_freeobjects(struct restorearg *ra, objset_t *os,
1455     struct drr_freeobjects *drrfo)
1456 {
1457 	uint64_t obj;
1458 
1459 	if (drrfo->drr_firstobj + drrfo->drr_numobjs < drrfo->drr_firstobj)
1460 		return (SET_ERROR(EINVAL));
1461 
1462 	for (obj = drrfo->drr_firstobj;
1463 	    obj < drrfo->drr_firstobj + drrfo->drr_numobjs;
1464 	    (void) dmu_object_next(os, &obj, FALSE, 0)) {
1465 		int err;
1466 
1467 		if (dmu_object_info(os, obj, NULL) != 0)
1468 			continue;
1469 
1470 		err = dmu_free_long_object(os, obj);
1471 		if (err != 0)
1472 			return (err);
1473 	}
1474 	return (0);
1475 }
1476 
1477 static int
1478 restore_write(struct restorearg *ra, objset_t *os,
1479     struct drr_write *drrw)
1480 {
1481 	dmu_tx_t *tx;
1482 	void *data;
1483 	int err;
1484 
1485 	if (drrw->drr_offset + drrw->drr_length < drrw->drr_offset ||
1486 	    !DMU_OT_IS_VALID(drrw->drr_type))
1487 		return (SET_ERROR(EINVAL));
1488 
1489 	if (dmu_object_info(os, drrw->drr_object, NULL) != 0)
1490 		return (SET_ERROR(EINVAL));
1491 
1492 	dmu_buf_t *bonus;
1493 	if (dmu_bonus_hold(os, drrw->drr_object, FTAG, &bonus) != 0)
1494 		return (SET_ERROR(EINVAL));
1495 
1496 	arc_buf_t *abuf = dmu_request_arcbuf(bonus, drrw->drr_length);
1497 
1498 	data = restore_read(ra, drrw->drr_length, abuf->b_data);
1499 	if (data == NULL) {
1500 		dmu_return_arcbuf(abuf);
1501 		dmu_buf_rele(bonus, FTAG);
1502 		return (ra->err);
1503 	}
1504 
1505 	tx = dmu_tx_create(os);
1506 
1507 	dmu_tx_hold_write(tx, drrw->drr_object,
1508 	    drrw->drr_offset, drrw->drr_length);
1509 	err = dmu_tx_assign(tx, TXG_WAIT);
1510 	if (err != 0) {
1511 		dmu_return_arcbuf(abuf);
1512 		dmu_buf_rele(bonus, FTAG);
1513 		dmu_tx_abort(tx);
1514 		return (err);
1515 	}
1516 	if (ra->byteswap) {
1517 		dmu_object_byteswap_t byteswap =
1518 		    DMU_OT_BYTESWAP(drrw->drr_type);
1519 		dmu_ot_byteswap[byteswap].ob_func(data, drrw->drr_length);
1520 	}
1521 	dmu_assign_arcbuf(bonus, drrw->drr_offset, abuf, tx);
1522 	dmu_tx_commit(tx);
1523 	dmu_buf_rele(bonus, FTAG);
1524 	return (0);
1525 }
1526 
1527 /*
1528  * Handle a DRR_WRITE_BYREF record.  This record is used in dedup'ed
1529  * streams to refer to a copy of the data that is already on the
1530  * system because it came in earlier in the stream.  This function
1531  * finds the earlier copy of the data, and uses that copy instead of
1532  * data from the stream to fulfill this write.
1533  */
1534 static int
1535 restore_write_byref(struct restorearg *ra, objset_t *os,
1536     struct drr_write_byref *drrwbr)
1537 {
1538 	dmu_tx_t *tx;
1539 	int err;
1540 	guid_map_entry_t gmesrch;
1541 	guid_map_entry_t *gmep;
1542 	avl_index_t where;
1543 	objset_t *ref_os = NULL;
1544 	dmu_buf_t *dbp;
1545 
1546 	if (drrwbr->drr_offset + drrwbr->drr_length < drrwbr->drr_offset)
1547 		return (SET_ERROR(EINVAL));
1548 
1549 	/*
1550 	 * If the GUID of the referenced dataset is different from the
1551 	 * GUID of the target dataset, find the referenced dataset.
1552 	 */
1553 	if (drrwbr->drr_toguid != drrwbr->drr_refguid) {
1554 		gmesrch.guid = drrwbr->drr_refguid;
1555 		if ((gmep = avl_find(ra->guid_to_ds_map, &gmesrch,
1556 		    &where)) == NULL) {
1557 			return (SET_ERROR(EINVAL));
1558 		}
1559 		if (dmu_objset_from_ds(gmep->gme_ds, &ref_os))
1560 			return (SET_ERROR(EINVAL));
1561 	} else {
1562 		ref_os = os;
1563 	}
1564 
1565 	err = dmu_buf_hold(ref_os, drrwbr->drr_refobject,
1566 	    drrwbr->drr_refoffset, FTAG, &dbp, DMU_READ_PREFETCH);
1567 	if (err != 0)
1568 		return (err);
1569 
1570 	tx = dmu_tx_create(os);
1571 
1572 	dmu_tx_hold_write(tx, drrwbr->drr_object,
1573 	    drrwbr->drr_offset, drrwbr->drr_length);
1574 	err = dmu_tx_assign(tx, TXG_WAIT);
1575 	if (err != 0) {
1576 		dmu_tx_abort(tx);
1577 		return (err);
1578 	}
1579 	dmu_write(os, drrwbr->drr_object,
1580 	    drrwbr->drr_offset, drrwbr->drr_length, dbp->db_data, tx);
1581 	dmu_buf_rele(dbp, FTAG);
1582 	dmu_tx_commit(tx);
1583 	return (0);
1584 }
1585 
1586 static int
1587 restore_write_embedded(struct restorearg *ra, objset_t *os,
1588     struct drr_write_embedded *drrwnp)
1589 {
1590 	dmu_tx_t *tx;
1591 	int err;
1592 	void *data;
1593 
1594 	if (drrwnp->drr_offset + drrwnp->drr_length < drrwnp->drr_offset)
1595 		return (EINVAL);
1596 
1597 	if (drrwnp->drr_psize > BPE_PAYLOAD_SIZE)
1598 		return (EINVAL);
1599 
1600 	if (drrwnp->drr_etype >= NUM_BP_EMBEDDED_TYPES)
1601 		return (EINVAL);
1602 	if (drrwnp->drr_compression >= ZIO_COMPRESS_FUNCTIONS)
1603 		return (EINVAL);
1604 
1605 	data = restore_read(ra, P2ROUNDUP(drrwnp->drr_psize, 8), NULL);
1606 	if (data == NULL)
1607 		return (ra->err);
1608 
1609 	tx = dmu_tx_create(os);
1610 
1611 	dmu_tx_hold_write(tx, drrwnp->drr_object,
1612 	    drrwnp->drr_offset, drrwnp->drr_length);
1613 	err = dmu_tx_assign(tx, TXG_WAIT);
1614 	if (err != 0) {
1615 		dmu_tx_abort(tx);
1616 		return (err);
1617 	}
1618 
1619 	dmu_write_embedded(os, drrwnp->drr_object,
1620 	    drrwnp->drr_offset, data, drrwnp->drr_etype,
1621 	    drrwnp->drr_compression, drrwnp->drr_lsize, drrwnp->drr_psize,
1622 	    ra->byteswap ^ ZFS_HOST_BYTEORDER, tx);
1623 
1624 	dmu_tx_commit(tx);
1625 	return (0);
1626 }
1627 
1628 static int
1629 restore_spill(struct restorearg *ra, objset_t *os, struct drr_spill *drrs)
1630 {
1631 	dmu_tx_t *tx;
1632 	void *data;
1633 	dmu_buf_t *db, *db_spill;
1634 	int err;
1635 
1636 	if (drrs->drr_length < SPA_MINBLOCKSIZE ||
1637 	    drrs->drr_length > SPA_MAXBLOCKSIZE)
1638 		return (SET_ERROR(EINVAL));
1639 
1640 	data = restore_read(ra, drrs->drr_length, NULL);
1641 	if (data == NULL)
1642 		return (ra->err);
1643 
1644 	if (dmu_object_info(os, drrs->drr_object, NULL) != 0)
1645 		return (SET_ERROR(EINVAL));
1646 
1647 	VERIFY(0 == dmu_bonus_hold(os, drrs->drr_object, FTAG, &db));
1648 	if ((err = dmu_spill_hold_by_bonus(db, FTAG, &db_spill)) != 0) {
1649 		dmu_buf_rele(db, FTAG);
1650 		return (err);
1651 	}
1652 
1653 	tx = dmu_tx_create(os);
1654 
1655 	dmu_tx_hold_spill(tx, db->db_object);
1656 
1657 	err = dmu_tx_assign(tx, TXG_WAIT);
1658 	if (err != 0) {
1659 		dmu_buf_rele(db, FTAG);
1660 		dmu_buf_rele(db_spill, FTAG);
1661 		dmu_tx_abort(tx);
1662 		return (err);
1663 	}
1664 	dmu_buf_will_dirty(db_spill, tx);
1665 
1666 	if (db_spill->db_size < drrs->drr_length)
1667 		VERIFY(0 == dbuf_spill_set_blksz(db_spill,
1668 		    drrs->drr_length, tx));
1669 	bcopy(data, db_spill->db_data, drrs->drr_length);
1670 
1671 	dmu_buf_rele(db, FTAG);
1672 	dmu_buf_rele(db_spill, FTAG);
1673 
1674 	dmu_tx_commit(tx);
1675 	return (0);
1676 }
1677 
1678 /* ARGSUSED */
1679 static int
1680 restore_free(struct restorearg *ra, objset_t *os,
1681     struct drr_free *drrf)
1682 {
1683 	int err;
1684 
1685 	if (drrf->drr_length != -1ULL &&
1686 	    drrf->drr_offset + drrf->drr_length < drrf->drr_offset)
1687 		return (SET_ERROR(EINVAL));
1688 
1689 	if (dmu_object_info(os, drrf->drr_object, NULL) != 0)
1690 		return (SET_ERROR(EINVAL));
1691 
1692 	err = dmu_free_long_range(os, drrf->drr_object,
1693 	    drrf->drr_offset, drrf->drr_length);
1694 	return (err);
1695 }
1696 
1697 /* used to destroy the drc_ds on error */
1698 static void
1699 dmu_recv_cleanup_ds(dmu_recv_cookie_t *drc)
1700 {
1701 	char name[MAXNAMELEN];
1702 	dsl_dataset_name(drc->drc_ds, name);
1703 	dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
1704 	(void) dsl_destroy_head(name);
1705 }
1706 
1707 /*
1708  * NB: callers *must* call dmu_recv_end() if this succeeds.
1709  */
1710 int
1711 dmu_recv_stream(dmu_recv_cookie_t *drc, vnode_t *vp, offset_t *voffp,
1712     int cleanup_fd, uint64_t *action_handlep)
1713 {
1714 	struct restorearg ra = { 0 };
1715 	dmu_replay_record_t *drr;
1716 	objset_t *os;
1717 	zio_cksum_t pcksum;
1718 	int featureflags;
1719 
1720 	ra.byteswap = drc->drc_byteswap;
1721 	ra.cksum = drc->drc_cksum;
1722 	ra.vp = vp;
1723 	ra.voff = *voffp;
1724 	ra.bufsize = 1<<20;
1725 	ra.buf = kmem_alloc(ra.bufsize, KM_SLEEP);
1726 
1727 	/* these were verified in dmu_recv_begin */
1728 	ASSERT3U(DMU_GET_STREAM_HDRTYPE(drc->drc_drrb->drr_versioninfo), ==,
1729 	    DMU_SUBSTREAM);
1730 	ASSERT3U(drc->drc_drrb->drr_type, <, DMU_OST_NUMTYPES);
1731 
1732 	/*
1733 	 * Open the objset we are modifying.
1734 	 */
1735 	VERIFY0(dmu_objset_from_ds(drc->drc_ds, &os));
1736 
1737 	ASSERT(drc->drc_ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT);
1738 
1739 	featureflags = DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo);
1740 
1741 	/* if this stream is dedup'ed, set up the avl tree for guid mapping */
1742 	if (featureflags & DMU_BACKUP_FEATURE_DEDUP) {
1743 		minor_t minor;
1744 
1745 		if (cleanup_fd == -1) {
1746 			ra.err = SET_ERROR(EBADF);
1747 			goto out;
1748 		}
1749 		ra.err = zfs_onexit_fd_hold(cleanup_fd, &minor);
1750 		if (ra.err != 0) {
1751 			cleanup_fd = -1;
1752 			goto out;
1753 		}
1754 
1755 		if (*action_handlep == 0) {
1756 			ra.guid_to_ds_map =
1757 			    kmem_alloc(sizeof (avl_tree_t), KM_SLEEP);
1758 			avl_create(ra.guid_to_ds_map, guid_compare,
1759 			    sizeof (guid_map_entry_t),
1760 			    offsetof(guid_map_entry_t, avlnode));
1761 			ra.err = zfs_onexit_add_cb(minor,
1762 			    free_guid_map_onexit, ra.guid_to_ds_map,
1763 			    action_handlep);
1764 			if (ra.err != 0)
1765 				goto out;
1766 		} else {
1767 			ra.err = zfs_onexit_cb_data(minor, *action_handlep,
1768 			    (void **)&ra.guid_to_ds_map);
1769 			if (ra.err != 0)
1770 				goto out;
1771 		}
1772 
1773 		drc->drc_guid_to_ds_map = ra.guid_to_ds_map;
1774 	}
1775 
1776 	/*
1777 	 * Read records and process them.
1778 	 */
1779 	pcksum = ra.cksum;
1780 	while (ra.err == 0 &&
1781 	    NULL != (drr = restore_read(&ra, sizeof (*drr), NULL))) {
1782 		if (issig(JUSTLOOKING) && issig(FORREAL)) {
1783 			ra.err = SET_ERROR(EINTR);
1784 			goto out;
1785 		}
1786 
1787 		if (ra.byteswap)
1788 			backup_byteswap(drr);
1789 
1790 		switch (drr->drr_type) {
1791 		case DRR_OBJECT:
1792 		{
1793 			/*
1794 			 * We need to make a copy of the record header,
1795 			 * because restore_{object,write} may need to
1796 			 * restore_read(), which will invalidate drr.
1797 			 */
1798 			struct drr_object drro = drr->drr_u.drr_object;
1799 			ra.err = restore_object(&ra, os, &drro);
1800 			break;
1801 		}
1802 		case DRR_FREEOBJECTS:
1803 		{
1804 			struct drr_freeobjects drrfo =
1805 			    drr->drr_u.drr_freeobjects;
1806 			ra.err = restore_freeobjects(&ra, os, &drrfo);
1807 			break;
1808 		}
1809 		case DRR_WRITE:
1810 		{
1811 			struct drr_write drrw = drr->drr_u.drr_write;
1812 			ra.err = restore_write(&ra, os, &drrw);
1813 			break;
1814 		}
1815 		case DRR_WRITE_BYREF:
1816 		{
1817 			struct drr_write_byref drrwbr =
1818 			    drr->drr_u.drr_write_byref;
1819 			ra.err = restore_write_byref(&ra, os, &drrwbr);
1820 			break;
1821 		}
1822 		case DRR_WRITE_EMBEDDED:
1823 		{
1824 			struct drr_write_embedded drrwe =
1825 			    drr->drr_u.drr_write_embedded;
1826 			ra.err = restore_write_embedded(&ra, os, &drrwe);
1827 			break;
1828 		}
1829 		case DRR_FREE:
1830 		{
1831 			struct drr_free drrf = drr->drr_u.drr_free;
1832 			ra.err = restore_free(&ra, os, &drrf);
1833 			break;
1834 		}
1835 		case DRR_END:
1836 		{
1837 			struct drr_end drre = drr->drr_u.drr_end;
1838 			/*
1839 			 * We compare against the *previous* checksum
1840 			 * value, because the stored checksum is of
1841 			 * everything before the DRR_END record.
1842 			 */
1843 			if (!ZIO_CHECKSUM_EQUAL(drre.drr_checksum, pcksum))
1844 				ra.err = SET_ERROR(ECKSUM);
1845 			goto out;
1846 		}
1847 		case DRR_SPILL:
1848 		{
1849 			struct drr_spill drrs = drr->drr_u.drr_spill;
1850 			ra.err = restore_spill(&ra, os, &drrs);
1851 			break;
1852 		}
1853 		default:
1854 			ra.err = SET_ERROR(EINVAL);
1855 			goto out;
1856 		}
1857 		pcksum = ra.cksum;
1858 	}
1859 	ASSERT(ra.err != 0);
1860 
1861 out:
1862 	if ((featureflags & DMU_BACKUP_FEATURE_DEDUP) && (cleanup_fd != -1))
1863 		zfs_onexit_fd_rele(cleanup_fd);
1864 
1865 	if (ra.err != 0) {
1866 		/*
1867 		 * destroy what we created, so we don't leave it in the
1868 		 * inconsistent restoring state.
1869 		 */
1870 		dmu_recv_cleanup_ds(drc);
1871 	}
1872 
1873 	kmem_free(ra.buf, ra.bufsize);
1874 	*voffp = ra.voff;
1875 	return (ra.err);
1876 }
1877 
1878 static int
1879 dmu_recv_end_check(void *arg, dmu_tx_t *tx)
1880 {
1881 	dmu_recv_cookie_t *drc = arg;
1882 	dsl_pool_t *dp = dmu_tx_pool(tx);
1883 	int error;
1884 
1885 	ASSERT3P(drc->drc_ds->ds_owner, ==, dmu_recv_tag);
1886 
1887 	if (!drc->drc_newfs) {
1888 		dsl_dataset_t *origin_head;
1889 
1890 		error = dsl_dataset_hold(dp, drc->drc_tofs, FTAG, &origin_head);
1891 		if (error != 0)
1892 			return (error);
1893 		if (drc->drc_force) {
1894 			/*
1895 			 * We will destroy any snapshots in tofs (i.e. before
1896 			 * origin_head) that are after the origin (which is
1897 			 * the snap before drc_ds, because drc_ds can not
1898 			 * have any snaps of its own).
1899 			 */
1900 			uint64_t obj = origin_head->ds_phys->ds_prev_snap_obj;
1901 			while (obj != drc->drc_ds->ds_phys->ds_prev_snap_obj) {
1902 				dsl_dataset_t *snap;
1903 				error = dsl_dataset_hold_obj(dp, obj, FTAG,
1904 				    &snap);
1905 				if (error != 0)
1906 					return (error);
1907 				if (snap->ds_dir != origin_head->ds_dir)
1908 					error = SET_ERROR(EINVAL);
1909 				if (error == 0)  {
1910 					error = dsl_destroy_snapshot_check_impl(
1911 					    snap, B_FALSE);
1912 				}
1913 				obj = snap->ds_phys->ds_prev_snap_obj;
1914 				dsl_dataset_rele(snap, FTAG);
1915 				if (error != 0)
1916 					return (error);
1917 			}
1918 		}
1919 		error = dsl_dataset_clone_swap_check_impl(drc->drc_ds,
1920 		    origin_head, drc->drc_force, drc->drc_owner, tx);
1921 		if (error != 0) {
1922 			dsl_dataset_rele(origin_head, FTAG);
1923 			return (error);
1924 		}
1925 		error = dsl_dataset_snapshot_check_impl(origin_head,
1926 		    drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred);
1927 		dsl_dataset_rele(origin_head, FTAG);
1928 		if (error != 0)
1929 			return (error);
1930 
1931 		error = dsl_destroy_head_check_impl(drc->drc_ds, 1);
1932 	} else {
1933 		error = dsl_dataset_snapshot_check_impl(drc->drc_ds,
1934 		    drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred);
1935 	}
1936 	return (error);
1937 }
1938 
1939 static void
1940 dmu_recv_end_sync(void *arg, dmu_tx_t *tx)
1941 {
1942 	dmu_recv_cookie_t *drc = arg;
1943 	dsl_pool_t *dp = dmu_tx_pool(tx);
1944 
1945 	spa_history_log_internal_ds(drc->drc_ds, "finish receiving",
1946 	    tx, "snap=%s", drc->drc_tosnap);
1947 
1948 	if (!drc->drc_newfs) {
1949 		dsl_dataset_t *origin_head;
1950 
1951 		VERIFY0(dsl_dataset_hold(dp, drc->drc_tofs, FTAG,
1952 		    &origin_head));
1953 
1954 		if (drc->drc_force) {
1955 			/*
1956 			 * Destroy any snapshots of drc_tofs (origin_head)
1957 			 * after the origin (the snap before drc_ds).
1958 			 */
1959 			uint64_t obj = origin_head->ds_phys->ds_prev_snap_obj;
1960 			while (obj != drc->drc_ds->ds_phys->ds_prev_snap_obj) {
1961 				dsl_dataset_t *snap;
1962 				VERIFY0(dsl_dataset_hold_obj(dp, obj, FTAG,
1963 				    &snap));
1964 				ASSERT3P(snap->ds_dir, ==, origin_head->ds_dir);
1965 				obj = snap->ds_phys->ds_prev_snap_obj;
1966 				dsl_destroy_snapshot_sync_impl(snap,
1967 				    B_FALSE, tx);
1968 				dsl_dataset_rele(snap, FTAG);
1969 			}
1970 		}
1971 		VERIFY3P(drc->drc_ds->ds_prev, ==,
1972 		    origin_head->ds_prev);
1973 
1974 		dsl_dataset_clone_swap_sync_impl(drc->drc_ds,
1975 		    origin_head, tx);
1976 		dsl_dataset_snapshot_sync_impl(origin_head,
1977 		    drc->drc_tosnap, tx);
1978 
1979 		/* set snapshot's creation time and guid */
1980 		dmu_buf_will_dirty(origin_head->ds_prev->ds_dbuf, tx);
1981 		origin_head->ds_prev->ds_phys->ds_creation_time =
1982 		    drc->drc_drrb->drr_creation_time;
1983 		origin_head->ds_prev->ds_phys->ds_guid =
1984 		    drc->drc_drrb->drr_toguid;
1985 		origin_head->ds_prev->ds_phys->ds_flags &=
1986 		    ~DS_FLAG_INCONSISTENT;
1987 
1988 		dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
1989 		origin_head->ds_phys->ds_flags &= ~DS_FLAG_INCONSISTENT;
1990 
1991 		dsl_dataset_rele(origin_head, FTAG);
1992 		dsl_destroy_head_sync_impl(drc->drc_ds, tx);
1993 
1994 		if (drc->drc_owner != NULL)
1995 			VERIFY3P(origin_head->ds_owner, ==, drc->drc_owner);
1996 	} else {
1997 		dsl_dataset_t *ds = drc->drc_ds;
1998 
1999 		dsl_dataset_snapshot_sync_impl(ds, drc->drc_tosnap, tx);
2000 
2001 		/* set snapshot's creation time and guid */
2002 		dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
2003 		ds->ds_prev->ds_phys->ds_creation_time =
2004 		    drc->drc_drrb->drr_creation_time;
2005 		ds->ds_prev->ds_phys->ds_guid = drc->drc_drrb->drr_toguid;
2006 		ds->ds_prev->ds_phys->ds_flags &= ~DS_FLAG_INCONSISTENT;
2007 
2008 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
2009 		ds->ds_phys->ds_flags &= ~DS_FLAG_INCONSISTENT;
2010 	}
2011 	drc->drc_newsnapobj = drc->drc_ds->ds_phys->ds_prev_snap_obj;
2012 	/*
2013 	 * Release the hold from dmu_recv_begin.  This must be done before
2014 	 * we return to open context, so that when we free the dataset's dnode,
2015 	 * we can evict its bonus buffer.
2016 	 */
2017 	dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
2018 	drc->drc_ds = NULL;
2019 }
2020 
2021 static int
2022 add_ds_to_guidmap(const char *name, avl_tree_t *guid_map, uint64_t snapobj)
2023 {
2024 	dsl_pool_t *dp;
2025 	dsl_dataset_t *snapds;
2026 	guid_map_entry_t *gmep;
2027 	int err;
2028 
2029 	ASSERT(guid_map != NULL);
2030 
2031 	err = dsl_pool_hold(name, FTAG, &dp);
2032 	if (err != 0)
2033 		return (err);
2034 	gmep = kmem_alloc(sizeof (*gmep), KM_SLEEP);
2035 	err = dsl_dataset_hold_obj(dp, snapobj, gmep, &snapds);
2036 	if (err == 0) {
2037 		gmep->guid = snapds->ds_phys->ds_guid;
2038 		gmep->gme_ds = snapds;
2039 		avl_add(guid_map, gmep);
2040 		dsl_dataset_long_hold(snapds, gmep);
2041 	} else {
2042 		kmem_free(gmep, sizeof (*gmep));
2043 	}
2044 
2045 	dsl_pool_rele(dp, FTAG);
2046 	return (err);
2047 }
2048 
2049 static int dmu_recv_end_modified_blocks = 3;
2050 
2051 static int
2052 dmu_recv_existing_end(dmu_recv_cookie_t *drc)
2053 {
2054 	int error;
2055 	char name[MAXNAMELEN];
2056 
2057 #ifdef _KERNEL
2058 	/*
2059 	 * We will be destroying the ds; make sure its origin is unmounted if
2060 	 * necessary.
2061 	 */
2062 	dsl_dataset_name(drc->drc_ds, name);
2063 	zfs_destroy_unmount_origin(name);
2064 #endif
2065 
2066 	error = dsl_sync_task(drc->drc_tofs,
2067 	    dmu_recv_end_check, dmu_recv_end_sync, drc,
2068 	    dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL);
2069 
2070 	if (error != 0)
2071 		dmu_recv_cleanup_ds(drc);
2072 	return (error);
2073 }
2074 
2075 static int
2076 dmu_recv_new_end(dmu_recv_cookie_t *drc)
2077 {
2078 	int error;
2079 
2080 	error = dsl_sync_task(drc->drc_tofs,
2081 	    dmu_recv_end_check, dmu_recv_end_sync, drc,
2082 	    dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL);
2083 
2084 	if (error != 0) {
2085 		dmu_recv_cleanup_ds(drc);
2086 	} else if (drc->drc_guid_to_ds_map != NULL) {
2087 		(void) add_ds_to_guidmap(drc->drc_tofs,
2088 		    drc->drc_guid_to_ds_map,
2089 		    drc->drc_newsnapobj);
2090 	}
2091 	return (error);
2092 }
2093 
2094 int
2095 dmu_recv_end(dmu_recv_cookie_t *drc, void *owner)
2096 {
2097 	drc->drc_owner = owner;
2098 
2099 	if (drc->drc_newfs)
2100 		return (dmu_recv_new_end(drc));
2101 	else
2102 		return (dmu_recv_existing_end(drc));
2103 }
2104 
2105 /*
2106  * Return TRUE if this objset is currently being received into.
2107  */
2108 boolean_t
2109 dmu_objset_is_receiving(objset_t *os)
2110 {
2111 	return (os->os_dsl_dataset != NULL &&
2112 	    os->os_dsl_dataset->ds_owner == dmu_recv_tag);
2113 }
2114