xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_send.c (revision df477c0afa111b5205c872dab36dbfde391656de)
1efb80947Sahrens /*
2efb80947Sahrens  * CDDL HEADER START
3efb80947Sahrens  *
4efb80947Sahrens  * The contents of this file are subject to the terms of the
5efb80947Sahrens  * Common Development and Distribution License (the "License").
6efb80947Sahrens  * You may not use this file except in compliance with the License.
7efb80947Sahrens  *
8efb80947Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9efb80947Sahrens  * or http://www.opensolaris.org/os/licensing.
10efb80947Sahrens  * See the License for the specific language governing permissions
11efb80947Sahrens  * and limitations under the License.
12efb80947Sahrens  *
13efb80947Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14efb80947Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15efb80947Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16efb80947Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17efb80947Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18efb80947Sahrens  *
19efb80947Sahrens  * CDDL HEADER END
20efb80947Sahrens  */
21efb80947Sahrens /*
22dc7cd546SMark Shellenbaum  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23ec5cf9d5SAlexander Stetsenko  * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24ca0cc391SMatthew Ahrens  * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
25a2afb611SJerry Jelinek  * Copyright (c) 2014, Joyent, Inc. All rights reserved.
26e77d42eaSMatthew Ahrens  * Copyright 2014 HybridCluster. All rights reserved.
27880094b6SAndrew Stormont  * Copyright 2016 RackTop Systems.
28c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
29ec5cf9d5SAlexander Stetsenko  */
30efb80947Sahrens 
31efb80947Sahrens #include <sys/dmu.h>
32efb80947Sahrens #include <sys/dmu_impl.h>
33efb80947Sahrens #include <sys/dmu_tx.h>
34efb80947Sahrens #include <sys/dbuf.h>
35efb80947Sahrens #include <sys/dnode.h>
36efb80947Sahrens #include <sys/zfs_context.h>
37efb80947Sahrens #include <sys/dmu_objset.h>
38efb80947Sahrens #include <sys/dmu_traverse.h>
39efb80947Sahrens #include <sys/dsl_dataset.h>
40efb80947Sahrens #include <sys/dsl_dir.h>
4192241e0bSTom Erickson #include <sys/dsl_prop.h>
42efb80947Sahrens #include <sys/dsl_pool.h>
43efb80947Sahrens #include <sys/dsl_synctask.h>
44efb80947Sahrens #include <sys/zfs_ioctl.h>
45efb80947Sahrens #include <sys/zap.h>
46efb80947Sahrens #include <sys/zio_checksum.h>
47dc7cd546SMark Shellenbaum #include <sys/zfs_znode.h>
48cde58dbcSMatthew Ahrens #include <zfs_fletcher.h>
499e69d7d0SLori Alt #include <sys/avl.h>
508e714474SLori Alt #include <sys/ddt.h>
51c99e4bdcSChris Kirby #include <sys/zfs_onexit.h>
523b2aab18SMatthew Ahrens #include <sys/dmu_send.h>
533b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
545d7b4d43SMatthew Ahrens #include <sys/blkptr.h>
5578f17100SMatthew Ahrens #include <sys/dsl_bookmark.h>
565d7b4d43SMatthew Ahrens #include <sys/zfeature.h>
57a2cdcdd2SPaul Dagnelie #include <sys/bqueue.h>
58efb80947Sahrens 
5919b94df9SMatthew Ahrens /* Set this tunable to TRUE to replace corrupt data with 0x2f5baddb10c */
6019b94df9SMatthew Ahrens int zfs_send_corrupt_data = B_FALSE;
61a2cdcdd2SPaul Dagnelie int zfs_send_queue_length = 16 * 1024 * 1024;
62a2cdcdd2SPaul Dagnelie int zfs_recv_queue_length = 16 * 1024 * 1024;
63880094b6SAndrew Stormont /* Set this tunable to FALSE to disable setting of DRR_FLAG_FREERECORDS */
64880094b6SAndrew Stormont int zfs_send_set_freerecords_bit = B_TRUE;
6519b94df9SMatthew Ahrens 
663cb34c60Sahrens static char *dmu_recv_tag = "dmu_recv_tag";
679c3fd121SMatthew Ahrens const char *recv_clone_name = "%recv";
683cb34c60Sahrens 
69*df477c0aSPaul Dagnelie /*
70*df477c0aSPaul Dagnelie  * Use this to override the recordsize calculation for fast zfs send estimates.
71*df477c0aSPaul Dagnelie  */
72*df477c0aSPaul Dagnelie uint64_t zfs_override_estimate_recordsize = 0;
73*df477c0aSPaul Dagnelie 
74a2cdcdd2SPaul Dagnelie #define	BP_SPAN(datablkszsec, indblkshift, level) \
75a2cdcdd2SPaul Dagnelie 	(((uint64_t)datablkszsec) << (SPA_MINBLOCKSHIFT + \
76a2cdcdd2SPaul Dagnelie 	(level) * (indblkshift - SPA_BLKPTRSHIFT)))
77a2cdcdd2SPaul Dagnelie 
789c3fd121SMatthew Ahrens static void byteswap_record(dmu_replay_record_t *drr);
799c3fd121SMatthew Ahrens 
80a2cdcdd2SPaul Dagnelie struct send_thread_arg {
81a2cdcdd2SPaul Dagnelie 	bqueue_t	q;
82a2cdcdd2SPaul Dagnelie 	dsl_dataset_t	*ds;		/* Dataset to traverse */
83a2cdcdd2SPaul Dagnelie 	uint64_t	fromtxg;	/* Traverse from this txg */
84a2cdcdd2SPaul Dagnelie 	int		flags;		/* flags to pass to traverse_dataset */
85a2cdcdd2SPaul Dagnelie 	int		error_code;
86a2cdcdd2SPaul Dagnelie 	boolean_t	cancel;
879c3fd121SMatthew Ahrens 	zbookmark_phys_t resume;
88a2cdcdd2SPaul Dagnelie };
89a2cdcdd2SPaul Dagnelie 
90a2cdcdd2SPaul Dagnelie struct send_block_record {
91a2cdcdd2SPaul Dagnelie 	boolean_t		eos_marker; /* Marks the end of the stream */
92a2cdcdd2SPaul Dagnelie 	blkptr_t		bp;
93a2cdcdd2SPaul Dagnelie 	zbookmark_phys_t	zb;
94a2cdcdd2SPaul Dagnelie 	uint8_t			indblkshift;
95a2cdcdd2SPaul Dagnelie 	uint16_t		datablkszsec;
96a2cdcdd2SPaul Dagnelie 	bqueue_node_t		ln;
97a2cdcdd2SPaul Dagnelie };
98a2cdcdd2SPaul Dagnelie 
99efb80947Sahrens static int
1004e3c9f44SBill Pijewski dump_bytes(dmu_sendarg_t *dsp, void *buf, int len)
101efb80947Sahrens {
1029c3fd121SMatthew Ahrens 	dsl_dataset_t *ds = dmu_objset_ds(dsp->dsa_os);
103efb80947Sahrens 	ssize_t resid; /* have to get resid to get detailed errno */
104c20404ffSEli Rosenthal 
105c20404ffSEli Rosenthal 	/*
106c20404ffSEli Rosenthal 	 * The code does not rely on this (len being a multiple of 8).  We keep
107c20404ffSEli Rosenthal 	 * this assertion because of the corresponding assertion in
108c20404ffSEli Rosenthal 	 * receive_read().  Keeping this assertion ensures that we do not
109c20404ffSEli Rosenthal 	 * inadvertently break backwards compatibility (causing the assertion
110c20404ffSEli Rosenthal 	 * in receive_read() to trigger on old software).
111c20404ffSEli Rosenthal 	 *
112c20404ffSEli Rosenthal 	 * Removing the assertions could be rolled into a new feature that uses
113c20404ffSEli Rosenthal 	 * data that isn't 8-byte aligned; if the assertions were removed, a
114c20404ffSEli Rosenthal 	 * feature flag would have to be added.
115c20404ffSEli Rosenthal 	 */
116c20404ffSEli Rosenthal 
117fb09f5aaSMadhav Suresh 	ASSERT0(len % 8);
118efb80947Sahrens 
1194e3c9f44SBill Pijewski 	dsp->dsa_err = vn_rdwr(UIO_WRITE, dsp->dsa_vp,
120efb80947Sahrens 	    (caddr_t)buf, len,
121efb80947Sahrens 	    0, UIO_SYSSPACE, FAPPEND, RLIM64_INFINITY, CRED(), &resid);
1224e3c9f44SBill Pijewski 
1234e3c9f44SBill Pijewski 	mutex_enter(&ds->ds_sendstream_lock);
1244e3c9f44SBill Pijewski 	*dsp->dsa_off += len;
1254e3c9f44SBill Pijewski 	mutex_exit(&ds->ds_sendstream_lock);
1264e3c9f44SBill Pijewski 
1274e3c9f44SBill Pijewski 	return (dsp->dsa_err);
128efb80947Sahrens }
129efb80947Sahrens 
13098110f08SMatthew Ahrens /*
13198110f08SMatthew Ahrens  * For all record types except BEGIN, fill in the checksum (overlaid in
13298110f08SMatthew Ahrens  * drr_u.drr_checksum.drr_checksum).  The checksum verifies everything
13398110f08SMatthew Ahrens  * up to the start of the checksum itself.
13498110f08SMatthew Ahrens  */
13598110f08SMatthew Ahrens static int
13698110f08SMatthew Ahrens dump_record(dmu_sendarg_t *dsp, void *payload, int payload_len)
13798110f08SMatthew Ahrens {
13898110f08SMatthew Ahrens 	ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
13998110f08SMatthew Ahrens 	    ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
140770499e1SDan Kimmel 	(void) fletcher_4_incremental_native(dsp->dsa_drr,
14198110f08SMatthew Ahrens 	    offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
14298110f08SMatthew Ahrens 	    &dsp->dsa_zc);
14312b90ee2SMatt Krantz 	if (dsp->dsa_drr->drr_type == DRR_BEGIN) {
14412b90ee2SMatt Krantz 		dsp->dsa_sent_begin = B_TRUE;
14512b90ee2SMatt Krantz 	} else {
14698110f08SMatthew Ahrens 		ASSERT(ZIO_CHECKSUM_IS_ZERO(&dsp->dsa_drr->drr_u.
14798110f08SMatthew Ahrens 		    drr_checksum.drr_checksum));
14898110f08SMatthew Ahrens 		dsp->dsa_drr->drr_u.drr_checksum.drr_checksum = dsp->dsa_zc;
14998110f08SMatthew Ahrens 	}
15012b90ee2SMatt Krantz 	if (dsp->dsa_drr->drr_type == DRR_END) {
15112b90ee2SMatt Krantz 		dsp->dsa_sent_end = B_TRUE;
15212b90ee2SMatt Krantz 	}
153770499e1SDan Kimmel 	(void) fletcher_4_incremental_native(&dsp->dsa_drr->
15498110f08SMatthew Ahrens 	    drr_u.drr_checksum.drr_checksum,
15598110f08SMatthew Ahrens 	    sizeof (zio_cksum_t), &dsp->dsa_zc);
15698110f08SMatthew Ahrens 	if (dump_bytes(dsp, dsp->dsa_drr, sizeof (dmu_replay_record_t)) != 0)
15798110f08SMatthew Ahrens 		return (SET_ERROR(EINTR));
15898110f08SMatthew Ahrens 	if (payload_len != 0) {
159770499e1SDan Kimmel 		(void) fletcher_4_incremental_native(payload, payload_len,
16098110f08SMatthew Ahrens 		    &dsp->dsa_zc);
16198110f08SMatthew Ahrens 		if (dump_bytes(dsp, payload, payload_len) != 0)
16298110f08SMatthew Ahrens 			return (SET_ERROR(EINTR));
16398110f08SMatthew Ahrens 	}
16498110f08SMatthew Ahrens 	return (0);
16598110f08SMatthew Ahrens }
16698110f08SMatthew Ahrens 
16768ecb2ecSPaul Dagnelie /*
16868ecb2ecSPaul Dagnelie  * Fill in the drr_free struct, or perform aggregation if the previous record is
16968ecb2ecSPaul Dagnelie  * also a free record, and the two are adjacent.
17068ecb2ecSPaul Dagnelie  *
17168ecb2ecSPaul Dagnelie  * Note that we send free records even for a full send, because we want to be
17268ecb2ecSPaul Dagnelie  * able to receive a full send as a clone, which requires a list of all the free
17368ecb2ecSPaul Dagnelie  * and freeobject records that were generated on the source.
17468ecb2ecSPaul Dagnelie  */
175efb80947Sahrens static int
1764e3c9f44SBill Pijewski dump_free(dmu_sendarg_t *dsp, uint64_t object, uint64_t offset,
177efb80947Sahrens     uint64_t length)
178efb80947Sahrens {
1794e3c9f44SBill Pijewski 	struct drr_free *drrf = &(dsp->dsa_drr->drr_u.drr_free);
1809e69d7d0SLori Alt 
1812f3d8780SMatthew Ahrens 	/*
1822f3d8780SMatthew Ahrens 	 * When we receive a free record, dbuf_free_range() assumes
1832f3d8780SMatthew Ahrens 	 * that the receiving system doesn't have any dbufs in the range
1842f3d8780SMatthew Ahrens 	 * being freed.  This is always true because there is a one-record
1852f3d8780SMatthew Ahrens 	 * constraint: we only send one WRITE record for any given
1869c3fd121SMatthew Ahrens 	 * object,offset.  We know that the one-record constraint is
1872f3d8780SMatthew Ahrens 	 * true because we always send data in increasing order by
1882f3d8780SMatthew Ahrens 	 * object,offset.
1892f3d8780SMatthew Ahrens 	 *
1902f3d8780SMatthew Ahrens 	 * If the increasing-order constraint ever changes, we should find
1912f3d8780SMatthew Ahrens 	 * another way to assert that the one-record constraint is still
1922f3d8780SMatthew Ahrens 	 * satisfied.
1932f3d8780SMatthew Ahrens 	 */
1942f3d8780SMatthew Ahrens 	ASSERT(object > dsp->dsa_last_data_object ||
1952f3d8780SMatthew Ahrens 	    (object == dsp->dsa_last_data_object &&
1962f3d8780SMatthew Ahrens 	    offset > dsp->dsa_last_data_offset));
1972f3d8780SMatthew Ahrens 
198534029e5SSimon Klinkert 	if (length != -1ULL && offset + length < offset)
199534029e5SSimon Klinkert 		length = -1ULL;
200534029e5SSimon Klinkert 
2019e69d7d0SLori Alt 	/*
2029e69d7d0SLori Alt 	 * If there is a pending op, but it's not PENDING_FREE, push it out,
2039e69d7d0SLori Alt 	 * since free block aggregation can only be done for blocks of the
2049e69d7d0SLori Alt 	 * same type (i.e., DRR_FREE records can only be aggregated with
2059e69d7d0SLori Alt 	 * other DRR_FREE records.  DRR_FREEOBJECTS records can only be
2069e69d7d0SLori Alt 	 * aggregated with other DRR_FREEOBJECTS records.
2079e69d7d0SLori Alt 	 */
2084e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE &&
2094e3c9f44SBill Pijewski 	    dsp->dsa_pending_op != PENDING_FREE) {
21098110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
211be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
2124e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
2139e69d7d0SLori Alt 	}
2149e69d7d0SLori Alt 
2154e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op == PENDING_FREE) {
2169e69d7d0SLori Alt 		/*
2179e69d7d0SLori Alt 		 * There should never be a PENDING_FREE if length is -1
2189e69d7d0SLori Alt 		 * (because dump_dnode is the only place where this
2199e69d7d0SLori Alt 		 * function is called with a -1, and only after flushing
2209e69d7d0SLori Alt 		 * any pending record).
2219e69d7d0SLori Alt 		 */
2229e69d7d0SLori Alt 		ASSERT(length != -1ULL);
2239e69d7d0SLori Alt 		/*
2249e69d7d0SLori Alt 		 * Check to see whether this free block can be aggregated
2259e69d7d0SLori Alt 		 * with pending one.
2269e69d7d0SLori Alt 		 */
2279e69d7d0SLori Alt 		if (drrf->drr_object == object && drrf->drr_offset +
2289e69d7d0SLori Alt 		    drrf->drr_length == offset) {
2299e69d7d0SLori Alt 			drrf->drr_length += length;
2309e69d7d0SLori Alt 			return (0);
2319e69d7d0SLori Alt 		} else {
2329e69d7d0SLori Alt 			/* not a continuation.  Push out pending record */
23398110f08SMatthew Ahrens 			if (dump_record(dsp, NULL, 0) != 0)
234be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINTR));
2354e3c9f44SBill Pijewski 			dsp->dsa_pending_op = PENDING_NONE;
2369e69d7d0SLori Alt 		}
2379e69d7d0SLori Alt 	}
2389e69d7d0SLori Alt 	/* create a FREE record and make it pending */
2394e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
2404e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_FREE;
2419e69d7d0SLori Alt 	drrf->drr_object = object;
2429e69d7d0SLori Alt 	drrf->drr_offset = offset;
2439e69d7d0SLori Alt 	drrf->drr_length = length;
2444e3c9f44SBill Pijewski 	drrf->drr_toguid = dsp->dsa_toguid;
2459e69d7d0SLori Alt 	if (length == -1ULL) {
24698110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
247be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
2489e69d7d0SLori Alt 	} else {
2494e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_FREE;
2509e69d7d0SLori Alt 	}
251efb80947Sahrens 
252efb80947Sahrens 	return (0);
253efb80947Sahrens }
254efb80947Sahrens 
255efb80947Sahrens static int
2565d7b4d43SMatthew Ahrens dump_write(dmu_sendarg_t *dsp, dmu_object_type_t type,
2575602294fSDan Kimmel     uint64_t object, uint64_t offset, int lsize, int psize, const blkptr_t *bp,
2585602294fSDan Kimmel     void *data)
259efb80947Sahrens {
2605602294fSDan Kimmel 	uint64_t payload_size;
2614e3c9f44SBill Pijewski 	struct drr_write *drrw = &(dsp->dsa_drr->drr_u.drr_write);
2629e69d7d0SLori Alt 
2632f3d8780SMatthew Ahrens 	/*
2642f3d8780SMatthew Ahrens 	 * We send data in increasing object, offset order.
2652f3d8780SMatthew Ahrens 	 * See comment in dump_free() for details.
2662f3d8780SMatthew Ahrens 	 */
2672f3d8780SMatthew Ahrens 	ASSERT(object > dsp->dsa_last_data_object ||
2682f3d8780SMatthew Ahrens 	    (object == dsp->dsa_last_data_object &&
2692f3d8780SMatthew Ahrens 	    offset > dsp->dsa_last_data_offset));
2702f3d8780SMatthew Ahrens 	dsp->dsa_last_data_object = object;
2715602294fSDan Kimmel 	dsp->dsa_last_data_offset = offset + lsize - 1;
2728e714474SLori Alt 
2739e69d7d0SLori Alt 	/*
2749e69d7d0SLori Alt 	 * If there is any kind of pending aggregation (currently either
2759e69d7d0SLori Alt 	 * a grouping of free objects or free blocks), push it out to
2769e69d7d0SLori Alt 	 * the stream, since aggregation can't be done across operations
2779e69d7d0SLori Alt 	 * of different types.
2789e69d7d0SLori Alt 	 */
2794e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE) {
28098110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
281be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
2824e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
2839e69d7d0SLori Alt 	}
28498110f08SMatthew Ahrens 	/* write a WRITE record */
2854e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
2864e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_WRITE;
2879e69d7d0SLori Alt 	drrw->drr_object = object;
2889e69d7d0SLori Alt 	drrw->drr_type = type;
2899e69d7d0SLori Alt 	drrw->drr_offset = offset;
2904e3c9f44SBill Pijewski 	drrw->drr_toguid = dsp->dsa_toguid;
2915602294fSDan Kimmel 	drrw->drr_logical_size = lsize;
2925602294fSDan Kimmel 
2935602294fSDan Kimmel 	/* only set the compression fields if the buf is compressed */
2945602294fSDan Kimmel 	if (lsize != psize) {
2955602294fSDan Kimmel 		ASSERT(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_COMPRESSED);
2965602294fSDan Kimmel 		ASSERT(!BP_IS_EMBEDDED(bp));
2975602294fSDan Kimmel 		ASSERT(!BP_SHOULD_BYTESWAP(bp));
2985602294fSDan Kimmel 		ASSERT(!DMU_OT_IS_METADATA(BP_GET_TYPE(bp)));
2995602294fSDan Kimmel 		ASSERT3U(BP_GET_COMPRESS(bp), !=, ZIO_COMPRESS_OFF);
3005602294fSDan Kimmel 		ASSERT3S(psize, >, 0);
3015602294fSDan Kimmel 		ASSERT3S(lsize, >=, psize);
3025602294fSDan Kimmel 
3035602294fSDan Kimmel 		drrw->drr_compressiontype = BP_GET_COMPRESS(bp);
3045602294fSDan Kimmel 		drrw->drr_compressed_size = psize;
3055602294fSDan Kimmel 		payload_size = drrw->drr_compressed_size;
3065602294fSDan Kimmel 	} else {
3075602294fSDan Kimmel 		payload_size = drrw->drr_logical_size;
3085602294fSDan Kimmel 	}
3095602294fSDan Kimmel 
310b5152584SMatthew Ahrens 	if (bp == NULL || BP_IS_EMBEDDED(bp)) {
3115d7b4d43SMatthew Ahrens 		/*
312b5152584SMatthew Ahrens 		 * There's no pre-computed checksum for partial-block
313b5152584SMatthew Ahrens 		 * writes or embedded BP's, so (like
314b5152584SMatthew Ahrens 		 * fletcher4-checkummed blocks) userland will have to
315b5152584SMatthew Ahrens 		 * compute a dedup-capable checksum itself.
3165d7b4d43SMatthew Ahrens 		 */
3175d7b4d43SMatthew Ahrens 		drrw->drr_checksumtype = ZIO_CHECKSUM_OFF;
3185d7b4d43SMatthew Ahrens 	} else {
3195d7b4d43SMatthew Ahrens 		drrw->drr_checksumtype = BP_GET_CHECKSUM(bp);
32045818ee1SMatthew Ahrens 		if (zio_checksum_table[drrw->drr_checksumtype].ci_flags &
32145818ee1SMatthew Ahrens 		    ZCHECKSUM_FLAG_DEDUP)
3225d7b4d43SMatthew Ahrens 			drrw->drr_checksumflags |= DRR_CHECKSUM_DEDUP;
3235d7b4d43SMatthew Ahrens 		DDK_SET_LSIZE(&drrw->drr_key, BP_GET_LSIZE(bp));
3245d7b4d43SMatthew Ahrens 		DDK_SET_PSIZE(&drrw->drr_key, BP_GET_PSIZE(bp));
3255d7b4d43SMatthew Ahrens 		DDK_SET_COMPRESS(&drrw->drr_key, BP_GET_COMPRESS(bp));
3265d7b4d43SMatthew Ahrens 		drrw->drr_key.ddk_cksum = bp->blk_cksum;
3275d7b4d43SMatthew Ahrens 	}
328efb80947Sahrens 
3295602294fSDan Kimmel 	if (dump_record(dsp, data, payload_size) != 0)
330be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
331efb80947Sahrens 	return (0);
332efb80947Sahrens }
333efb80947Sahrens 
3345d7b4d43SMatthew Ahrens static int
3355d7b4d43SMatthew Ahrens dump_write_embedded(dmu_sendarg_t *dsp, uint64_t object, uint64_t offset,
3365d7b4d43SMatthew Ahrens     int blksz, const blkptr_t *bp)
3375d7b4d43SMatthew Ahrens {
3385d7b4d43SMatthew Ahrens 	char buf[BPE_PAYLOAD_SIZE];
3395d7b4d43SMatthew Ahrens 	struct drr_write_embedded *drrw =
3405d7b4d43SMatthew Ahrens 	    &(dsp->dsa_drr->drr_u.drr_write_embedded);
3415d7b4d43SMatthew Ahrens 
3425d7b4d43SMatthew Ahrens 	if (dsp->dsa_pending_op != PENDING_NONE) {
34398110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
3445d7b4d43SMatthew Ahrens 			return (EINTR);
3455d7b4d43SMatthew Ahrens 		dsp->dsa_pending_op = PENDING_NONE;
3465d7b4d43SMatthew Ahrens 	}
3475d7b4d43SMatthew Ahrens 
3485d7b4d43SMatthew Ahrens 	ASSERT(BP_IS_EMBEDDED(bp));
3495d7b4d43SMatthew Ahrens 
3505d7b4d43SMatthew Ahrens 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
3515d7b4d43SMatthew Ahrens 	dsp->dsa_drr->drr_type = DRR_WRITE_EMBEDDED;
3525d7b4d43SMatthew Ahrens 	drrw->drr_object = object;
3535d7b4d43SMatthew Ahrens 	drrw->drr_offset = offset;
3545d7b4d43SMatthew Ahrens 	drrw->drr_length = blksz;
3555d7b4d43SMatthew Ahrens 	drrw->drr_toguid = dsp->dsa_toguid;
3565d7b4d43SMatthew Ahrens 	drrw->drr_compression = BP_GET_COMPRESS(bp);
3575d7b4d43SMatthew Ahrens 	drrw->drr_etype = BPE_GET_ETYPE(bp);
3585d7b4d43SMatthew Ahrens 	drrw->drr_lsize = BPE_GET_LSIZE(bp);
3595d7b4d43SMatthew Ahrens 	drrw->drr_psize = BPE_GET_PSIZE(bp);
3605d7b4d43SMatthew Ahrens 
3615d7b4d43SMatthew Ahrens 	decode_embedded_bp_compressed(bp, buf);
3625d7b4d43SMatthew Ahrens 
36398110f08SMatthew Ahrens 	if (dump_record(dsp, buf, P2ROUNDUP(drrw->drr_psize, 8)) != 0)
3645d7b4d43SMatthew Ahrens 		return (EINTR);
3655d7b4d43SMatthew Ahrens 	return (0);
3665d7b4d43SMatthew Ahrens }
3675d7b4d43SMatthew Ahrens 
3680a586ceaSMark Shellenbaum static int
3694e3c9f44SBill Pijewski dump_spill(dmu_sendarg_t *dsp, uint64_t object, int blksz, void *data)
3700a586ceaSMark Shellenbaum {
3714e3c9f44SBill Pijewski 	struct drr_spill *drrs = &(dsp->dsa_drr->drr_u.drr_spill);
3720a586ceaSMark Shellenbaum 
3734e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE) {
37498110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
375be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
3764e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
3770a586ceaSMark Shellenbaum 	}
3780a586ceaSMark Shellenbaum 
3790a586ceaSMark Shellenbaum 	/* write a SPILL record */
3804e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
3814e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_SPILL;
3820a586ceaSMark Shellenbaum 	drrs->drr_object = object;
3830a586ceaSMark Shellenbaum 	drrs->drr_length = blksz;
3844e3c9f44SBill Pijewski 	drrs->drr_toguid = dsp->dsa_toguid;
3850a586ceaSMark Shellenbaum 
38698110f08SMatthew Ahrens 	if (dump_record(dsp, data, blksz) != 0)
387be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
3880a586ceaSMark Shellenbaum 	return (0);
3890a586ceaSMark Shellenbaum }
3900a586ceaSMark Shellenbaum 
391efb80947Sahrens static int
3924e3c9f44SBill Pijewski dump_freeobjects(dmu_sendarg_t *dsp, uint64_t firstobj, uint64_t numobjs)
393efb80947Sahrens {
3944e3c9f44SBill Pijewski 	struct drr_freeobjects *drrfo = &(dsp->dsa_drr->drr_u.drr_freeobjects);
3959e69d7d0SLori Alt 
3969e69d7d0SLori Alt 	/*
3979e69d7d0SLori Alt 	 * If there is a pending op, but it's not PENDING_FREEOBJECTS,
3989e69d7d0SLori Alt 	 * push it out, since free block aggregation can only be done for
3999e69d7d0SLori Alt 	 * blocks of the same type (i.e., DRR_FREE records can only be
4009e69d7d0SLori Alt 	 * aggregated with other DRR_FREE records.  DRR_FREEOBJECTS records
4019e69d7d0SLori Alt 	 * can only be aggregated with other DRR_FREEOBJECTS records.
4029e69d7d0SLori Alt 	 */
4034e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE &&
4044e3c9f44SBill Pijewski 	    dsp->dsa_pending_op != PENDING_FREEOBJECTS) {
40598110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
406be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
4074e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
4089e69d7d0SLori Alt 	}
4094e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op == PENDING_FREEOBJECTS) {
4109e69d7d0SLori Alt 		/*
4119e69d7d0SLori Alt 		 * See whether this free object array can be aggregated
4129e69d7d0SLori Alt 		 * with pending one
4139e69d7d0SLori Alt 		 */
4149e69d7d0SLori Alt 		if (drrfo->drr_firstobj + drrfo->drr_numobjs == firstobj) {
4159e69d7d0SLori Alt 			drrfo->drr_numobjs += numobjs;
4169e69d7d0SLori Alt 			return (0);
4179e69d7d0SLori Alt 		} else {
4189e69d7d0SLori Alt 			/* can't be aggregated.  Push out pending record */
41998110f08SMatthew Ahrens 			if (dump_record(dsp, NULL, 0) != 0)
420be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINTR));
4214e3c9f44SBill Pijewski 			dsp->dsa_pending_op = PENDING_NONE;
4229e69d7d0SLori Alt 		}
4239e69d7d0SLori Alt 	}
4249e69d7d0SLori Alt 
425efb80947Sahrens 	/* write a FREEOBJECTS record */
4264e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
4274e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_FREEOBJECTS;
4289e69d7d0SLori Alt 	drrfo->drr_firstobj = firstobj;
4299e69d7d0SLori Alt 	drrfo->drr_numobjs = numobjs;
4304e3c9f44SBill Pijewski 	drrfo->drr_toguid = dsp->dsa_toguid;
4319e69d7d0SLori Alt 
4324e3c9f44SBill Pijewski 	dsp->dsa_pending_op = PENDING_FREEOBJECTS;
433efb80947Sahrens 
434efb80947Sahrens 	return (0);
435efb80947Sahrens }
436efb80947Sahrens 
437efb80947Sahrens static int
4384e3c9f44SBill Pijewski dump_dnode(dmu_sendarg_t *dsp, uint64_t object, dnode_phys_t *dnp)
439efb80947Sahrens {
4404e3c9f44SBill Pijewski 	struct drr_object *drro = &(dsp->dsa_drr->drr_u.drr_object);
4419e69d7d0SLori Alt 
4429c3fd121SMatthew Ahrens 	if (object < dsp->dsa_resume_object) {
4439c3fd121SMatthew Ahrens 		/*
4449c3fd121SMatthew Ahrens 		 * Note: when resuming, we will visit all the dnodes in
4459c3fd121SMatthew Ahrens 		 * the block of dnodes that we are resuming from.  In
4469c3fd121SMatthew Ahrens 		 * this case it's unnecessary to send the dnodes prior to
4479c3fd121SMatthew Ahrens 		 * the one we are resuming from.  We should be at most one
4489c3fd121SMatthew Ahrens 		 * block's worth of dnodes behind the resume point.
4499c3fd121SMatthew Ahrens 		 */
4509c3fd121SMatthew Ahrens 		ASSERT3U(dsp->dsa_resume_object - object, <,
4519c3fd121SMatthew Ahrens 		    1 << (DNODE_BLOCK_SHIFT - DNODE_SHIFT));
4529c3fd121SMatthew Ahrens 		return (0);
4539c3fd121SMatthew Ahrens 	}
4549c3fd121SMatthew Ahrens 
455efb80947Sahrens 	if (dnp == NULL || dnp->dn_type == DMU_OT_NONE)
4564e3c9f44SBill Pijewski 		return (dump_freeobjects(dsp, object, 1));
457efb80947Sahrens 
4584e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE) {
45998110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
460be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
4614e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
4629e69d7d0SLori Alt 	}
4639e69d7d0SLori Alt 
464efb80947Sahrens 	/* write an OBJECT record */
4654e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
4664e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_OBJECT;
4679e69d7d0SLori Alt 	drro->drr_object = object;
4689e69d7d0SLori Alt 	drro->drr_type = dnp->dn_type;
4699e69d7d0SLori Alt 	drro->drr_bonustype = dnp->dn_bonustype;
4709e69d7d0SLori Alt 	drro->drr_blksz = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
4719e69d7d0SLori Alt 	drro->drr_bonuslen = dnp->dn_bonuslen;
4729e69d7d0SLori Alt 	drro->drr_checksumtype = dnp->dn_checksum;
4739e69d7d0SLori Alt 	drro->drr_compress = dnp->dn_compress;
4744e3c9f44SBill Pijewski 	drro->drr_toguid = dsp->dsa_toguid;
4759e69d7d0SLori Alt 
476b5152584SMatthew Ahrens 	if (!(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
477b5152584SMatthew Ahrens 	    drro->drr_blksz > SPA_OLD_MAXBLOCKSIZE)
478b5152584SMatthew Ahrens 		drro->drr_blksz = SPA_OLD_MAXBLOCKSIZE;
479b5152584SMatthew Ahrens 
48098110f08SMatthew Ahrens 	if (dump_record(dsp, DN_BONUS(dnp),
48198110f08SMatthew Ahrens 	    P2ROUNDUP(dnp->dn_bonuslen, 8)) != 0) {
482be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
48398110f08SMatthew Ahrens 	}
484efb80947Sahrens 
4852f3d8780SMatthew Ahrens 	/* Free anything past the end of the file. */
4864e3c9f44SBill Pijewski 	if (dump_free(dsp, object, (dnp->dn_maxblkid + 1) *
4872f3d8780SMatthew Ahrens 	    (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT), -1ULL) != 0)
488be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
4893b2aab18SMatthew Ahrens 	if (dsp->dsa_err != 0)
490be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
491efb80947Sahrens 	return (0);
492efb80947Sahrens }
493efb80947Sahrens 
4945d7b4d43SMatthew Ahrens static boolean_t
4955d7b4d43SMatthew Ahrens backup_do_embed(dmu_sendarg_t *dsp, const blkptr_t *bp)
4965d7b4d43SMatthew Ahrens {
4975d7b4d43SMatthew Ahrens 	if (!BP_IS_EMBEDDED(bp))
4985d7b4d43SMatthew Ahrens 		return (B_FALSE);
4995d7b4d43SMatthew Ahrens 
5005d7b4d43SMatthew Ahrens 	/*
5015d7b4d43SMatthew Ahrens 	 * Compression function must be legacy, or explicitly enabled.
5025d7b4d43SMatthew Ahrens 	 */
5035d7b4d43SMatthew Ahrens 	if ((BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_LEGACY_FUNCTIONS &&
5045602294fSDan Kimmel 	    !(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_LZ4)))
5055d7b4d43SMatthew Ahrens 		return (B_FALSE);
5065d7b4d43SMatthew Ahrens 
5075d7b4d43SMatthew Ahrens 	/*
5085d7b4d43SMatthew Ahrens 	 * Embed type must be explicitly enabled.
5095d7b4d43SMatthew Ahrens 	 */
5105d7b4d43SMatthew Ahrens 	switch (BPE_GET_ETYPE(bp)) {
5115d7b4d43SMatthew Ahrens 	case BP_EMBEDDED_TYPE_DATA:
5125d7b4d43SMatthew Ahrens 		if (dsp->dsa_featureflags & DMU_BACKUP_FEATURE_EMBED_DATA)
5135d7b4d43SMatthew Ahrens 			return (B_TRUE);
5145d7b4d43SMatthew Ahrens 		break;
5155d7b4d43SMatthew Ahrens 	default:
5165d7b4d43SMatthew Ahrens 		return (B_FALSE);
5175d7b4d43SMatthew Ahrens 	}
5185d7b4d43SMatthew Ahrens 	return (B_FALSE);
5195d7b4d43SMatthew Ahrens }
5205d7b4d43SMatthew Ahrens 
521a2cdcdd2SPaul Dagnelie /*
522a2cdcdd2SPaul Dagnelie  * This is the callback function to traverse_dataset that acts as the worker
523a2cdcdd2SPaul Dagnelie  * thread for dmu_send_impl.
524a2cdcdd2SPaul Dagnelie  */
525a2cdcdd2SPaul Dagnelie /*ARGSUSED*/
526a2cdcdd2SPaul Dagnelie static int
527a2cdcdd2SPaul Dagnelie send_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
528a2cdcdd2SPaul Dagnelie     const zbookmark_phys_t *zb, const struct dnode_phys *dnp, void *arg)
529a2cdcdd2SPaul Dagnelie {
530a2cdcdd2SPaul Dagnelie 	struct send_thread_arg *sta = arg;
531a2cdcdd2SPaul Dagnelie 	struct send_block_record *record;
532a2cdcdd2SPaul Dagnelie 	uint64_t record_size;
533a2cdcdd2SPaul Dagnelie 	int err = 0;
534efb80947Sahrens 
5359c3fd121SMatthew Ahrens 	ASSERT(zb->zb_object == DMU_META_DNODE_OBJECT ||
5369c3fd121SMatthew Ahrens 	    zb->zb_object >= sta->resume.zb_object);
5379c3fd121SMatthew Ahrens 
538a2cdcdd2SPaul Dagnelie 	if (sta->cancel)
539a2cdcdd2SPaul Dagnelie 		return (SET_ERROR(EINTR));
540a2cdcdd2SPaul Dagnelie 
541a2cdcdd2SPaul Dagnelie 	if (bp == NULL) {
542a2cdcdd2SPaul Dagnelie 		ASSERT3U(zb->zb_level, ==, ZB_DNODE_LEVEL);
543a2cdcdd2SPaul Dagnelie 		return (0);
544a2cdcdd2SPaul Dagnelie 	} else if (zb->zb_level < 0) {
545a2cdcdd2SPaul Dagnelie 		return (0);
546a2cdcdd2SPaul Dagnelie 	}
547a2cdcdd2SPaul Dagnelie 
548a2cdcdd2SPaul Dagnelie 	record = kmem_zalloc(sizeof (struct send_block_record), KM_SLEEP);
549a2cdcdd2SPaul Dagnelie 	record->eos_marker = B_FALSE;
550a2cdcdd2SPaul Dagnelie 	record->bp = *bp;
551a2cdcdd2SPaul Dagnelie 	record->zb = *zb;
552a2cdcdd2SPaul Dagnelie 	record->indblkshift = dnp->dn_indblkshift;
553a2cdcdd2SPaul Dagnelie 	record->datablkszsec = dnp->dn_datablkszsec;
554a2cdcdd2SPaul Dagnelie 	record_size = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
555a2cdcdd2SPaul Dagnelie 	bqueue_enqueue(&sta->q, record, record_size);
556a2cdcdd2SPaul Dagnelie 
557a2cdcdd2SPaul Dagnelie 	return (err);
558a2cdcdd2SPaul Dagnelie }
559a2cdcdd2SPaul Dagnelie 
560a2cdcdd2SPaul Dagnelie /*
561a2cdcdd2SPaul Dagnelie  * This function kicks off the traverse_dataset.  It also handles setting the
562a2cdcdd2SPaul Dagnelie  * error code of the thread in case something goes wrong, and pushes the End of
563a2cdcdd2SPaul Dagnelie  * Stream record when the traverse_dataset call has finished.  If there is no
564a2cdcdd2SPaul Dagnelie  * dataset to traverse, the thread immediately pushes End of Stream marker.
565a2cdcdd2SPaul Dagnelie  */
566a2cdcdd2SPaul Dagnelie static void
567a2cdcdd2SPaul Dagnelie send_traverse_thread(void *arg)
568a2cdcdd2SPaul Dagnelie {
569a2cdcdd2SPaul Dagnelie 	struct send_thread_arg *st_arg = arg;
570a2cdcdd2SPaul Dagnelie 	int err;
571a2cdcdd2SPaul Dagnelie 	struct send_block_record *data;
572a2cdcdd2SPaul Dagnelie 
573a2cdcdd2SPaul Dagnelie 	if (st_arg->ds != NULL) {
5749c3fd121SMatthew Ahrens 		err = traverse_dataset_resume(st_arg->ds,
5759c3fd121SMatthew Ahrens 		    st_arg->fromtxg, &st_arg->resume,
5769c3fd121SMatthew Ahrens 		    st_arg->flags, send_cb, st_arg);
5779c3fd121SMatthew Ahrens 
578a2cdcdd2SPaul Dagnelie 		if (err != EINTR)
579a2cdcdd2SPaul Dagnelie 			st_arg->error_code = err;
580a2cdcdd2SPaul Dagnelie 	}
581a2cdcdd2SPaul Dagnelie 	data = kmem_zalloc(sizeof (*data), KM_SLEEP);
582a2cdcdd2SPaul Dagnelie 	data->eos_marker = B_TRUE;
583a2cdcdd2SPaul Dagnelie 	bqueue_enqueue(&st_arg->q, data, 1);
584f2c1e9bcSJorgen Lundman 	thread_exit();
585a2cdcdd2SPaul Dagnelie }
586a2cdcdd2SPaul Dagnelie 
587a2cdcdd2SPaul Dagnelie /*
588a2cdcdd2SPaul Dagnelie  * This function actually handles figuring out what kind of record needs to be
589a2cdcdd2SPaul Dagnelie  * dumped, reading the data (which has hopefully been prefetched), and calling
590a2cdcdd2SPaul Dagnelie  * the appropriate helper function.
591a2cdcdd2SPaul Dagnelie  */
592efb80947Sahrens static int
593a2cdcdd2SPaul Dagnelie do_dump(dmu_sendarg_t *dsa, struct send_block_record *data)
594efb80947Sahrens {
595a2cdcdd2SPaul Dagnelie 	dsl_dataset_t *ds = dmu_objset_ds(dsa->dsa_os);
596a2cdcdd2SPaul Dagnelie 	const blkptr_t *bp = &data->bp;
597a2cdcdd2SPaul Dagnelie 	const zbookmark_phys_t *zb = &data->zb;
598a2cdcdd2SPaul Dagnelie 	uint8_t indblkshift = data->indblkshift;
599a2cdcdd2SPaul Dagnelie 	uint16_t dblkszsec = data->datablkszsec;
600a2cdcdd2SPaul Dagnelie 	spa_t *spa = ds->ds_dir->dd_pool->dp_spa;
601efb80947Sahrens 	dmu_object_type_t type = bp ? BP_GET_TYPE(bp) : DMU_OT_NONE;
602efb80947Sahrens 	int err = 0;
603efb80947Sahrens 
604a2cdcdd2SPaul Dagnelie 	ASSERT3U(zb->zb_level, >=, 0);
605efb80947Sahrens 
6069c3fd121SMatthew Ahrens 	ASSERT(zb->zb_object == DMU_META_DNODE_OBJECT ||
6079c3fd121SMatthew Ahrens 	    zb->zb_object >= dsa->dsa_resume_object);
6089c3fd121SMatthew Ahrens 
609b24ab676SJeff Bonwick 	if (zb->zb_object != DMU_META_DNODE_OBJECT &&
610b24ab676SJeff Bonwick 	    DMU_OBJECT_IS_SPECIAL(zb->zb_object)) {
61114843421SMatthew Ahrens 		return (0);
61243466aaeSMax Grossman 	} else if (BP_IS_HOLE(bp) &&
61343466aaeSMax Grossman 	    zb->zb_object == DMU_META_DNODE_OBJECT) {
614a2cdcdd2SPaul Dagnelie 		uint64_t span = BP_SPAN(dblkszsec, indblkshift, zb->zb_level);
61588b7b0f2SMatthew Ahrens 		uint64_t dnobj = (zb->zb_blkid * span) >> DNODE_SHIFT;
616a2cdcdd2SPaul Dagnelie 		err = dump_freeobjects(dsa, dnobj, span >> DNODE_SHIFT);
61743466aaeSMax Grossman 	} else if (BP_IS_HOLE(bp)) {
618a2cdcdd2SPaul Dagnelie 		uint64_t span = BP_SPAN(dblkszsec, indblkshift, zb->zb_level);
619a2cdcdd2SPaul Dagnelie 		uint64_t offset = zb->zb_blkid * span;
620a2cdcdd2SPaul Dagnelie 		err = dump_free(dsa, zb->zb_object, offset, span);
62188b7b0f2SMatthew Ahrens 	} else if (zb->zb_level > 0 || type == DMU_OT_OBJSET) {
62288b7b0f2SMatthew Ahrens 		return (0);
62388b7b0f2SMatthew Ahrens 	} else if (type == DMU_OT_DNODE) {
624efb80947Sahrens 		int blksz = BP_GET_LSIZE(bp);
6257adb730bSGeorge Wilson 		arc_flags_t aflags = ARC_FLAG_WAIT;
62688b7b0f2SMatthew Ahrens 		arc_buf_t *abuf;
627efb80947Sahrens 
628a2cdcdd2SPaul Dagnelie 		ASSERT0(zb->zb_level);
629a2cdcdd2SPaul Dagnelie 
6301b912ec7SGeorge Wilson 		if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
6311b912ec7SGeorge Wilson 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
6321b912ec7SGeorge Wilson 		    &aflags, zb) != 0)
633be6fd75aSMatthew Ahrens 			return (SET_ERROR(EIO));
63488b7b0f2SMatthew Ahrens 
635a2cdcdd2SPaul Dagnelie 		dnode_phys_t *blk = abuf->b_data;
636a2cdcdd2SPaul Dagnelie 		uint64_t dnobj = zb->zb_blkid * (blksz >> DNODE_SHIFT);
637a2cdcdd2SPaul Dagnelie 		for (int i = 0; i < blksz >> DNODE_SHIFT; i++) {
638a2cdcdd2SPaul Dagnelie 			err = dump_dnode(dsa, dnobj + i, blk + i);
6393b2aab18SMatthew Ahrens 			if (err != 0)
640efb80947Sahrens 				break;
641efb80947Sahrens 		}
642dcbf3bd6SGeorge Wilson 		arc_buf_destroy(abuf, &abuf);
6430a586ceaSMark Shellenbaum 	} else if (type == DMU_OT_SA) {
6447adb730bSGeorge Wilson 		arc_flags_t aflags = ARC_FLAG_WAIT;
6450a586ceaSMark Shellenbaum 		arc_buf_t *abuf;
6460a586ceaSMark Shellenbaum 		int blksz = BP_GET_LSIZE(bp);
6470a586ceaSMark Shellenbaum 
6481b912ec7SGeorge Wilson 		if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
6491b912ec7SGeorge Wilson 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
6501b912ec7SGeorge Wilson 		    &aflags, zb) != 0)
651be6fd75aSMatthew Ahrens 			return (SET_ERROR(EIO));
6520a586ceaSMark Shellenbaum 
653a2cdcdd2SPaul Dagnelie 		err = dump_spill(dsa, zb->zb_object, blksz, abuf->b_data);
654dcbf3bd6SGeorge Wilson 		arc_buf_destroy(abuf, &abuf);
655a2cdcdd2SPaul Dagnelie 	} else if (backup_do_embed(dsa, bp)) {
6565d7b4d43SMatthew Ahrens 		/* it's an embedded level-0 block of a regular object */
657a2cdcdd2SPaul Dagnelie 		int blksz = dblkszsec << SPA_MINBLOCKSHIFT;
658a2cdcdd2SPaul Dagnelie 		ASSERT0(zb->zb_level);
659a2cdcdd2SPaul Dagnelie 		err = dump_write_embedded(dsa, zb->zb_object,
6605d7b4d43SMatthew Ahrens 		    zb->zb_blkid * blksz, blksz, bp);
661a2cdcdd2SPaul Dagnelie 	} else {
662a2cdcdd2SPaul Dagnelie 		/* it's a level-0 block of a regular object */
6637adb730bSGeorge Wilson 		arc_flags_t aflags = ARC_FLAG_WAIT;
66488b7b0f2SMatthew Ahrens 		arc_buf_t *abuf;
665a2cdcdd2SPaul Dagnelie 		int blksz = dblkszsec << SPA_MINBLOCKSHIFT;
666b5152584SMatthew Ahrens 		uint64_t offset;
66788b7b0f2SMatthew Ahrens 
6685602294fSDan Kimmel 		/*
6695602294fSDan Kimmel 		 * If we have large blocks stored on disk but the send flags
6705602294fSDan Kimmel 		 * don't allow us to send large blocks, we split the data from
6715602294fSDan Kimmel 		 * the arc buf into chunks.
6725602294fSDan Kimmel 		 */
6735602294fSDan Kimmel 		boolean_t split_large_blocks = blksz > SPA_OLD_MAXBLOCKSIZE &&
6745602294fSDan Kimmel 		    !(dsa->dsa_featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS);
6755602294fSDan Kimmel 		/*
6765602294fSDan Kimmel 		 * We should only request compressed data from the ARC if all
6775602294fSDan Kimmel 		 * the following are true:
6785602294fSDan Kimmel 		 *  - stream compression was requested
6795602294fSDan Kimmel 		 *  - we aren't splitting large blocks into smaller chunks
6805602294fSDan Kimmel 		 *  - the data won't need to be byteswapped before sending
6815602294fSDan Kimmel 		 *  - this isn't an embedded block
6825602294fSDan Kimmel 		 *  - this isn't metadata (if receiving on a different endian
6835602294fSDan Kimmel 		 *    system it can be byteswapped more easily)
6845602294fSDan Kimmel 		 */
6855602294fSDan Kimmel 		boolean_t request_compressed =
6865602294fSDan Kimmel 		    (dsa->dsa_featureflags & DMU_BACKUP_FEATURE_COMPRESSED) &&
6875602294fSDan Kimmel 		    !split_large_blocks && !BP_SHOULD_BYTESWAP(bp) &&
6885602294fSDan Kimmel 		    !BP_IS_EMBEDDED(bp) && !DMU_OT_IS_METADATA(BP_GET_TYPE(bp));
6895602294fSDan Kimmel 
6905602294fSDan Kimmel 		ASSERT0(zb->zb_level);
6915602294fSDan Kimmel 		ASSERT(zb->zb_object > dsa->dsa_resume_object ||
6925602294fSDan Kimmel 		    (zb->zb_object == dsa->dsa_resume_object &&
6935602294fSDan Kimmel 		    zb->zb_blkid * blksz >= dsa->dsa_resume_offset));
6945602294fSDan Kimmel 
69578f17100SMatthew Ahrens 		ASSERT0(zb->zb_level);
6969c3fd121SMatthew Ahrens 		ASSERT(zb->zb_object > dsa->dsa_resume_object ||
6979c3fd121SMatthew Ahrens 		    (zb->zb_object == dsa->dsa_resume_object &&
6989c3fd121SMatthew Ahrens 		    zb->zb_blkid * blksz >= dsa->dsa_resume_offset));
6999c3fd121SMatthew Ahrens 
7005602294fSDan Kimmel 		ASSERT3U(blksz, ==, BP_GET_LSIZE(bp));
7015602294fSDan Kimmel 
7025602294fSDan Kimmel 		enum zio_flag zioflags = ZIO_FLAG_CANFAIL;
7035602294fSDan Kimmel 		if (request_compressed)
7045602294fSDan Kimmel 			zioflags |= ZIO_FLAG_RAW;
7051b912ec7SGeorge Wilson 		if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
7065602294fSDan Kimmel 		    ZIO_PRIORITY_ASYNC_READ, zioflags, &aflags, zb) != 0) {
70719b94df9SMatthew Ahrens 			if (zfs_send_corrupt_data) {
70819b94df9SMatthew Ahrens 				/* Send a block filled with 0x"zfs badd bloc" */
7095602294fSDan Kimmel 				abuf = arc_alloc_buf(spa, &abuf, ARC_BUFC_DATA,
7105602294fSDan Kimmel 				    blksz);
71119b94df9SMatthew Ahrens 				uint64_t *ptr;
71219b94df9SMatthew Ahrens 				for (ptr = abuf->b_data;
71319b94df9SMatthew Ahrens 				    (char *)ptr < (char *)abuf->b_data + blksz;
71419b94df9SMatthew Ahrens 				    ptr++)
7158c76e076SBrian Behlendorf 					*ptr = 0x2f5baddb10cULL;
71619b94df9SMatthew Ahrens 			} else {
717be6fd75aSMatthew Ahrens 				return (SET_ERROR(EIO));
71819b94df9SMatthew Ahrens 			}
71919b94df9SMatthew Ahrens 		}
72088b7b0f2SMatthew Ahrens 
721b5152584SMatthew Ahrens 		offset = zb->zb_blkid * blksz;
722b5152584SMatthew Ahrens 
7235602294fSDan Kimmel 		if (split_large_blocks) {
7245602294fSDan Kimmel 			ASSERT3U(arc_get_compression(abuf), ==,
7255602294fSDan Kimmel 			    ZIO_COMPRESS_OFF);
726b5152584SMatthew Ahrens 			char *buf = abuf->b_data;
727b5152584SMatthew Ahrens 			while (blksz > 0 && err == 0) {
728b5152584SMatthew Ahrens 				int n = MIN(blksz, SPA_OLD_MAXBLOCKSIZE);
729a2cdcdd2SPaul Dagnelie 				err = dump_write(dsa, type, zb->zb_object,
7305602294fSDan Kimmel 				    offset, n, n, NULL, buf);
731b5152584SMatthew Ahrens 				offset += n;
732b5152584SMatthew Ahrens 				buf += n;
733b5152584SMatthew Ahrens 				blksz -= n;
734b5152584SMatthew Ahrens 			}
735b5152584SMatthew Ahrens 		} else {
7365602294fSDan Kimmel 			err = dump_write(dsa, type, zb->zb_object, offset,
7375602294fSDan Kimmel 			    blksz, arc_buf_size(abuf), bp, abuf->b_data);
738b5152584SMatthew Ahrens 		}
739dcbf3bd6SGeorge Wilson 		arc_buf_destroy(abuf, &abuf);
740efb80947Sahrens 	}
741efb80947Sahrens 
742efb80947Sahrens 	ASSERT(err == 0 || err == EINTR);
743efb80947Sahrens 	return (err);
744efb80947Sahrens }
745efb80947Sahrens 
7464445fffbSMatthew Ahrens /*
747a2cdcdd2SPaul Dagnelie  * Pop the new data off the queue, and free the old data.
748a2cdcdd2SPaul Dagnelie  */
749a2cdcdd2SPaul Dagnelie static struct send_block_record *
750a2cdcdd2SPaul Dagnelie get_next_record(bqueue_t *bq, struct send_block_record *data)
751a2cdcdd2SPaul Dagnelie {
752a2cdcdd2SPaul Dagnelie 	struct send_block_record *tmp = bqueue_dequeue(bq);
753a2cdcdd2SPaul Dagnelie 	kmem_free(data, sizeof (*data));
754a2cdcdd2SPaul Dagnelie 	return (tmp);
755a2cdcdd2SPaul Dagnelie }
756a2cdcdd2SPaul Dagnelie 
757a2cdcdd2SPaul Dagnelie /*
758a2cdcdd2SPaul Dagnelie  * Actually do the bulk of the work in a zfs send.
759a2cdcdd2SPaul Dagnelie  *
760a2cdcdd2SPaul Dagnelie  * Note: Releases dp using the specified tag.
7614445fffbSMatthew Ahrens  */
7623b2aab18SMatthew Ahrens static int
763a2cdcdd2SPaul Dagnelie dmu_send_impl(void *tag, dsl_pool_t *dp, dsl_dataset_t *to_ds,
7645602294fSDan Kimmel     zfs_bookmark_phys_t *ancestor_zb, boolean_t is_clone,
7655602294fSDan Kimmel     boolean_t embedok, boolean_t large_block_ok, boolean_t compressok,
7665602294fSDan Kimmel     int outfd, uint64_t resumeobj, uint64_t resumeoff,
7679c3fd121SMatthew Ahrens     vnode_t *vp, offset_t *off)
768efb80947Sahrens {
7693b2aab18SMatthew Ahrens 	objset_t *os;
770efb80947Sahrens 	dmu_replay_record_t *drr;
7714e3c9f44SBill Pijewski 	dmu_sendarg_t *dsp;
772efb80947Sahrens 	int err;
7733cb34c60Sahrens 	uint64_t fromtxg = 0;
7745d7b4d43SMatthew Ahrens 	uint64_t featureflags = 0;
7759c3fd121SMatthew Ahrens 	struct send_thread_arg to_arg = { 0 };
776efb80947Sahrens 
777a2cdcdd2SPaul Dagnelie 	err = dmu_objset_from_ds(to_ds, &os);
7783b2aab18SMatthew Ahrens 	if (err != 0) {
7793b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, tag);
7803b2aab18SMatthew Ahrens 		return (err);
7813b2aab18SMatthew Ahrens 	}
782efb80947Sahrens 
783efb80947Sahrens 	drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP);
784efb80947Sahrens 	drr->drr_type = DRR_BEGIN;
785efb80947Sahrens 	drr->drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
7869e69d7d0SLori Alt 	DMU_SET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo,
7879e69d7d0SLori Alt 	    DMU_SUBSTREAM);
788dc7cd546SMark Shellenbaum 
789dc7cd546SMark Shellenbaum #ifdef _KERNEL
7903b2aab18SMatthew Ahrens 	if (dmu_objset_type(os) == DMU_OST_ZFS) {
791dc7cd546SMark Shellenbaum 		uint64_t version;
7923b2aab18SMatthew Ahrens 		if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &version) != 0) {
7934e3c9f44SBill Pijewski 			kmem_free(drr, sizeof (dmu_replay_record_t));
7943b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, tag);
795be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
7964e3c9f44SBill Pijewski 		}
7973b2aab18SMatthew Ahrens 		if (version >= ZPL_VERSION_SA) {
7985d7b4d43SMatthew Ahrens 			featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
799dc7cd546SMark Shellenbaum 		}
800dc7cd546SMark Shellenbaum 	}
801dc7cd546SMark Shellenbaum #endif
802dc7cd546SMark Shellenbaum 
803ca0cc391SMatthew Ahrens 	if (large_block_ok && to_ds->ds_feature_inuse[SPA_FEATURE_LARGE_BLOCKS])
804b5152584SMatthew Ahrens 		featureflags |= DMU_BACKUP_FEATURE_LARGE_BLOCKS;
8055d7b4d43SMatthew Ahrens 	if (embedok &&
8065d7b4d43SMatthew Ahrens 	    spa_feature_is_active(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA)) {
8075d7b4d43SMatthew Ahrens 		featureflags |= DMU_BACKUP_FEATURE_EMBED_DATA;
8085d7b4d43SMatthew Ahrens 		if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
8095602294fSDan Kimmel 			featureflags |= DMU_BACKUP_FEATURE_LZ4;
8105602294fSDan Kimmel 	}
8115602294fSDan Kimmel 	if (compressok) {
8125602294fSDan Kimmel 		featureflags |= DMU_BACKUP_FEATURE_COMPRESSED;
8135602294fSDan Kimmel 	}
8145602294fSDan Kimmel 	if ((featureflags &
8155602294fSDan Kimmel 	    (DMU_BACKUP_FEATURE_EMBED_DATA | DMU_BACKUP_FEATURE_COMPRESSED)) !=
8165602294fSDan Kimmel 	    0 && spa_feature_is_active(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS)) {
8175602294fSDan Kimmel 		featureflags |= DMU_BACKUP_FEATURE_LZ4;
8185d7b4d43SMatthew Ahrens 	}
8195d7b4d43SMatthew Ahrens 
8209c3fd121SMatthew Ahrens 	if (resumeobj != 0 || resumeoff != 0) {
8219c3fd121SMatthew Ahrens 		featureflags |= DMU_BACKUP_FEATURE_RESUMING;
8229c3fd121SMatthew Ahrens 	}
8239c3fd121SMatthew Ahrens 
8245d7b4d43SMatthew Ahrens 	DMU_SET_FEATUREFLAGS(drr->drr_u.drr_begin.drr_versioninfo,
8255d7b4d43SMatthew Ahrens 	    featureflags);
8265d7b4d43SMatthew Ahrens 
827efb80947Sahrens 	drr->drr_u.drr_begin.drr_creation_time =
828a2cdcdd2SPaul Dagnelie 	    dsl_dataset_phys(to_ds)->ds_creation_time;
8293b2aab18SMatthew Ahrens 	drr->drr_u.drr_begin.drr_type = dmu_objset_type(os);
83078f17100SMatthew Ahrens 	if (is_clone)
8313cb34c60Sahrens 		drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CLONE;
832a2cdcdd2SPaul Dagnelie 	drr->drr_u.drr_begin.drr_toguid = dsl_dataset_phys(to_ds)->ds_guid;
833a2cdcdd2SPaul Dagnelie 	if (dsl_dataset_phys(to_ds)->ds_flags & DS_FLAG_CI_DATASET)
834ab04eb8eStimh 		drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CI_DATA;
835880094b6SAndrew Stormont 	if (zfs_send_set_freerecords_bit)
836880094b6SAndrew Stormont 		drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_FREERECORDS;
837ab04eb8eStimh 
838a2cdcdd2SPaul Dagnelie 	if (ancestor_zb != NULL) {
839a2cdcdd2SPaul Dagnelie 		drr->drr_u.drr_begin.drr_fromguid =
840a2cdcdd2SPaul Dagnelie 		    ancestor_zb->zbm_guid;
841a2cdcdd2SPaul Dagnelie 		fromtxg = ancestor_zb->zbm_creation_txg;
84278f17100SMatthew Ahrens 	}
843a2cdcdd2SPaul Dagnelie 	dsl_dataset_name(to_ds, drr->drr_u.drr_begin.drr_toname);
844a2cdcdd2SPaul Dagnelie 	if (!to_ds->ds_is_snapshot) {
84578f17100SMatthew Ahrens 		(void) strlcat(drr->drr_u.drr_begin.drr_toname, "@--head--",
84678f17100SMatthew Ahrens 		    sizeof (drr->drr_u.drr_begin.drr_toname));
8473b2aab18SMatthew Ahrens 	}
8483cb34c60Sahrens 
8494e3c9f44SBill Pijewski 	dsp = kmem_zalloc(sizeof (dmu_sendarg_t), KM_SLEEP);
8504e3c9f44SBill Pijewski 
8514e3c9f44SBill Pijewski 	dsp->dsa_drr = drr;
8524e3c9f44SBill Pijewski 	dsp->dsa_vp = vp;
8534e3c9f44SBill Pijewski 	dsp->dsa_outfd = outfd;
8544e3c9f44SBill Pijewski 	dsp->dsa_proc = curproc;
8553b2aab18SMatthew Ahrens 	dsp->dsa_os = os;
8564e3c9f44SBill Pijewski 	dsp->dsa_off = off;
857a2cdcdd2SPaul Dagnelie 	dsp->dsa_toguid = dsl_dataset_phys(to_ds)->ds_guid;
8584e3c9f44SBill Pijewski 	dsp->dsa_pending_op = PENDING_NONE;
8595d7b4d43SMatthew Ahrens 	dsp->dsa_featureflags = featureflags;
8609c3fd121SMatthew Ahrens 	dsp->dsa_resume_object = resumeobj;
8619c3fd121SMatthew Ahrens 	dsp->dsa_resume_offset = resumeoff;
8624e3c9f44SBill Pijewski 
863a2cdcdd2SPaul Dagnelie 	mutex_enter(&to_ds->ds_sendstream_lock);
864a2cdcdd2SPaul Dagnelie 	list_insert_head(&to_ds->ds_sendstreams, dsp);
865a2cdcdd2SPaul Dagnelie 	mutex_exit(&to_ds->ds_sendstream_lock);
8664e3c9f44SBill Pijewski 
867a2cdcdd2SPaul Dagnelie 	dsl_dataset_long_hold(to_ds, FTAG);
868de8d9cffSMatthew Ahrens 	dsl_pool_rele(dp, tag);
869de8d9cffSMatthew Ahrens 
8709c3fd121SMatthew Ahrens 	void *payload = NULL;
8719c3fd121SMatthew Ahrens 	size_t payload_len = 0;
8729c3fd121SMatthew Ahrens 	if (resumeobj != 0 || resumeoff != 0) {
8739c3fd121SMatthew Ahrens 		dmu_object_info_t to_doi;
8749c3fd121SMatthew Ahrens 		err = dmu_object_info(os, resumeobj, &to_doi);
8759c3fd121SMatthew Ahrens 		if (err != 0)
8769c3fd121SMatthew Ahrens 			goto out;
8779c3fd121SMatthew Ahrens 		SET_BOOKMARK(&to_arg.resume, to_ds->ds_object, resumeobj, 0,
8789c3fd121SMatthew Ahrens 		    resumeoff / to_doi.doi_data_block_size);
8799c3fd121SMatthew Ahrens 
8809c3fd121SMatthew Ahrens 		nvlist_t *nvl = fnvlist_alloc();
8819c3fd121SMatthew Ahrens 		fnvlist_add_uint64(nvl, "resume_object", resumeobj);
8829c3fd121SMatthew Ahrens 		fnvlist_add_uint64(nvl, "resume_offset", resumeoff);
8839c3fd121SMatthew Ahrens 		payload = fnvlist_pack(nvl, &payload_len);
8849c3fd121SMatthew Ahrens 		drr->drr_payloadlen = payload_len;
8859c3fd121SMatthew Ahrens 		fnvlist_free(nvl);
8869c3fd121SMatthew Ahrens 	}
8879c3fd121SMatthew Ahrens 
8889c3fd121SMatthew Ahrens 	err = dump_record(dsp, payload, payload_len);
8899c3fd121SMatthew Ahrens 	fnvlist_pack_free(payload, payload_len);
8909c3fd121SMatthew Ahrens 	if (err != 0) {
8914e3c9f44SBill Pijewski 		err = dsp->dsa_err;
8924e3c9f44SBill Pijewski 		goto out;
893efb80947Sahrens 	}
894efb80947Sahrens 
895a2cdcdd2SPaul Dagnelie 	err = bqueue_init(&to_arg.q, zfs_send_queue_length,
896a2cdcdd2SPaul Dagnelie 	    offsetof(struct send_block_record, ln));
897a2cdcdd2SPaul Dagnelie 	to_arg.error_code = 0;
898a2cdcdd2SPaul Dagnelie 	to_arg.cancel = B_FALSE;
899a2cdcdd2SPaul Dagnelie 	to_arg.ds = to_ds;
900a2cdcdd2SPaul Dagnelie 	to_arg.fromtxg = fromtxg;
901a2cdcdd2SPaul Dagnelie 	to_arg.flags = TRAVERSE_PRE | TRAVERSE_PREFETCH;
902a2cdcdd2SPaul Dagnelie 	(void) thread_create(NULL, 0, send_traverse_thread, &to_arg, 0, curproc,
903a2cdcdd2SPaul Dagnelie 	    TS_RUN, minclsyspri);
904a2cdcdd2SPaul Dagnelie 
905a2cdcdd2SPaul Dagnelie 	struct send_block_record *to_data;
906a2cdcdd2SPaul Dagnelie 	to_data = bqueue_dequeue(&to_arg.q);
907a2cdcdd2SPaul Dagnelie 
908a2cdcdd2SPaul Dagnelie 	while (!to_data->eos_marker && err == 0) {
909a2cdcdd2SPaul Dagnelie 		err = do_dump(dsp, to_data);
910a2cdcdd2SPaul Dagnelie 		to_data = get_next_record(&to_arg.q, to_data);
911a2cdcdd2SPaul Dagnelie 		if (issig(JUSTLOOKING) && issig(FORREAL))
912a2cdcdd2SPaul Dagnelie 			err = EINTR;
913a2cdcdd2SPaul Dagnelie 	}
914a2cdcdd2SPaul Dagnelie 
915a2cdcdd2SPaul Dagnelie 	if (err != 0) {
916a2cdcdd2SPaul Dagnelie 		to_arg.cancel = B_TRUE;
917a2cdcdd2SPaul Dagnelie 		while (!to_data->eos_marker) {
918a2cdcdd2SPaul Dagnelie 			to_data = get_next_record(&to_arg.q, to_data);
919a2cdcdd2SPaul Dagnelie 		}
920a2cdcdd2SPaul Dagnelie 	}
921a2cdcdd2SPaul Dagnelie 	kmem_free(to_data, sizeof (*to_data));
922a2cdcdd2SPaul Dagnelie 
923a2cdcdd2SPaul Dagnelie 	bqueue_destroy(&to_arg.q);
924a2cdcdd2SPaul Dagnelie 
925a2cdcdd2SPaul Dagnelie 	if (err == 0 && to_arg.error_code != 0)
926a2cdcdd2SPaul Dagnelie 		err = to_arg.error_code;
927a2cdcdd2SPaul Dagnelie 
928a2cdcdd2SPaul Dagnelie 	if (err != 0)
929a2cdcdd2SPaul Dagnelie 		goto out;
930efb80947Sahrens 
9314e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE)
93298110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
933be6fd75aSMatthew Ahrens 			err = SET_ERROR(EINTR);
9349e69d7d0SLori Alt 
9353b2aab18SMatthew Ahrens 	if (err != 0) {
9363b2aab18SMatthew Ahrens 		if (err == EINTR && dsp->dsa_err != 0)
9374e3c9f44SBill Pijewski 			err = dsp->dsa_err;
9384e3c9f44SBill Pijewski 		goto out;
939efb80947Sahrens 	}
940efb80947Sahrens 
941efb80947Sahrens 	bzero(drr, sizeof (dmu_replay_record_t));
942efb80947Sahrens 	drr->drr_type = DRR_END;
9434e3c9f44SBill Pijewski 	drr->drr_u.drr_end.drr_checksum = dsp->dsa_zc;
9444e3c9f44SBill Pijewski 	drr->drr_u.drr_end.drr_toguid = dsp->dsa_toguid;
945efb80947Sahrens 
946a2cdcdd2SPaul Dagnelie 	if (dump_record(dsp, NULL, 0) != 0)
9474e3c9f44SBill Pijewski 		err = dsp->dsa_err;
948efb80947Sahrens 
9494e3c9f44SBill Pijewski out:
950a2cdcdd2SPaul Dagnelie 	mutex_enter(&to_ds->ds_sendstream_lock);
951a2cdcdd2SPaul Dagnelie 	list_remove(&to_ds->ds_sendstreams, dsp);
952a2cdcdd2SPaul Dagnelie 	mutex_exit(&to_ds->ds_sendstream_lock);
9534e3c9f44SBill Pijewski 
95412b90ee2SMatt Krantz 	VERIFY(err != 0 || (dsp->dsa_sent_begin && dsp->dsa_sent_end));
95512b90ee2SMatt Krantz 
956efb80947Sahrens 	kmem_free(drr, sizeof (dmu_replay_record_t));
9574e3c9f44SBill Pijewski 	kmem_free(dsp, sizeof (dmu_sendarg_t));
958efb80947Sahrens 
959a2cdcdd2SPaul Dagnelie 	dsl_dataset_long_rele(to_ds, FTAG);
9603b2aab18SMatthew Ahrens 
9614e3c9f44SBill Pijewski 	return (err);
962efb80947Sahrens }
963efb80947Sahrens 
96419b94df9SMatthew Ahrens int
9653b2aab18SMatthew Ahrens dmu_send_obj(const char *pool, uint64_t tosnap, uint64_t fromsnap,
9665602294fSDan Kimmel     boolean_t embedok, boolean_t large_block_ok, boolean_t compressok,
967b5152584SMatthew Ahrens     int outfd, vnode_t *vp, offset_t *off)
9683b2aab18SMatthew Ahrens {
9693b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
9703b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
9713b2aab18SMatthew Ahrens 	dsl_dataset_t *fromds = NULL;
9723b2aab18SMatthew Ahrens 	int err;
9733b2aab18SMatthew Ahrens 
9743b2aab18SMatthew Ahrens 	err = dsl_pool_hold(pool, FTAG, &dp);
9753b2aab18SMatthew Ahrens 	if (err != 0)
9763b2aab18SMatthew Ahrens 		return (err);
9773b2aab18SMatthew Ahrens 
9783b2aab18SMatthew Ahrens 	err = dsl_dataset_hold_obj(dp, tosnap, FTAG, &ds);
9793b2aab18SMatthew Ahrens 	if (err != 0) {
9803b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
9813b2aab18SMatthew Ahrens 		return (err);
9823b2aab18SMatthew Ahrens 	}
9833b2aab18SMatthew Ahrens 
9843b2aab18SMatthew Ahrens 	if (fromsnap != 0) {
98578f17100SMatthew Ahrens 		zfs_bookmark_phys_t zb;
98678f17100SMatthew Ahrens 		boolean_t is_clone;
98778f17100SMatthew Ahrens 
9883b2aab18SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp, fromsnap, FTAG, &fromds);
9893b2aab18SMatthew Ahrens 		if (err != 0) {
9903b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
9913b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
9923b2aab18SMatthew Ahrens 			return (err);
9933b2aab18SMatthew Ahrens 		}
99478f17100SMatthew Ahrens 		if (!dsl_dataset_is_before(ds, fromds, 0))
99578f17100SMatthew Ahrens 			err = SET_ERROR(EXDEV);
996c1379625SJustin T. Gibbs 		zb.zbm_creation_time =
997c1379625SJustin T. Gibbs 		    dsl_dataset_phys(fromds)->ds_creation_time;
998c1379625SJustin T. Gibbs 		zb.zbm_creation_txg = dsl_dataset_phys(fromds)->ds_creation_txg;
999c1379625SJustin T. Gibbs 		zb.zbm_guid = dsl_dataset_phys(fromds)->ds_guid;
100078f17100SMatthew Ahrens 		is_clone = (fromds->ds_dir != ds->ds_dir);
100178f17100SMatthew Ahrens 		dsl_dataset_rele(fromds, FTAG);
1002b5152584SMatthew Ahrens 		err = dmu_send_impl(FTAG, dp, ds, &zb, is_clone,
10035602294fSDan Kimmel 		    embedok, large_block_ok, compressok, outfd, 0, 0, vp, off);
100478f17100SMatthew Ahrens 	} else {
1005b5152584SMatthew Ahrens 		err = dmu_send_impl(FTAG, dp, ds, NULL, B_FALSE,
10065602294fSDan Kimmel 		    embedok, large_block_ok, compressok, outfd, 0, 0, vp, off);
10073b2aab18SMatthew Ahrens 	}
100878f17100SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
100978f17100SMatthew Ahrens 	return (err);
10103b2aab18SMatthew Ahrens }
10113b2aab18SMatthew Ahrens 
10123b2aab18SMatthew Ahrens int
10139c3fd121SMatthew Ahrens dmu_send(const char *tosnap, const char *fromsnap, boolean_t embedok,
10145602294fSDan Kimmel     boolean_t large_block_ok, boolean_t compressok, int outfd,
10155602294fSDan Kimmel     uint64_t resumeobj, uint64_t resumeoff,
10169c3fd121SMatthew Ahrens     vnode_t *vp, offset_t *off)
10173b2aab18SMatthew Ahrens {
10183b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
10193b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
10203b2aab18SMatthew Ahrens 	int err;
102178f17100SMatthew Ahrens 	boolean_t owned = B_FALSE;
10223b2aab18SMatthew Ahrens 
102378f17100SMatthew Ahrens 	if (fromsnap != NULL && strpbrk(fromsnap, "@#") == NULL)
1024be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
10253b2aab18SMatthew Ahrens 
10263b2aab18SMatthew Ahrens 	err = dsl_pool_hold(tosnap, FTAG, &dp);
10273b2aab18SMatthew Ahrens 	if (err != 0)
10283b2aab18SMatthew Ahrens 		return (err);
10293b2aab18SMatthew Ahrens 
103078f17100SMatthew Ahrens 	if (strchr(tosnap, '@') == NULL && spa_writeable(dp->dp_spa)) {
103178f17100SMatthew Ahrens 		/*
103278f17100SMatthew Ahrens 		 * We are sending a filesystem or volume.  Ensure
103378f17100SMatthew Ahrens 		 * that it doesn't change by owning the dataset.
103478f17100SMatthew Ahrens 		 */
103578f17100SMatthew Ahrens 		err = dsl_dataset_own(dp, tosnap, FTAG, &ds);
103678f17100SMatthew Ahrens 		owned = B_TRUE;
103778f17100SMatthew Ahrens 	} else {
103878f17100SMatthew Ahrens 		err = dsl_dataset_hold(dp, tosnap, FTAG, &ds);
103978f17100SMatthew Ahrens 	}
10403b2aab18SMatthew Ahrens 	if (err != 0) {
10413b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
10423b2aab18SMatthew Ahrens 		return (err);
10433b2aab18SMatthew Ahrens 	}
10443b2aab18SMatthew Ahrens 
10453b2aab18SMatthew Ahrens 	if (fromsnap != NULL) {
104678f17100SMatthew Ahrens 		zfs_bookmark_phys_t zb;
104778f17100SMatthew Ahrens 		boolean_t is_clone = B_FALSE;
104878f17100SMatthew Ahrens 		int fsnamelen = strchr(tosnap, '@') - tosnap;
104978f17100SMatthew Ahrens 
105078f17100SMatthew Ahrens 		/*
105178f17100SMatthew Ahrens 		 * If the fromsnap is in a different filesystem, then
105278f17100SMatthew Ahrens 		 * mark the send stream as a clone.
105378f17100SMatthew Ahrens 		 */
105478f17100SMatthew Ahrens 		if (strncmp(tosnap, fromsnap, fsnamelen) != 0 ||
105578f17100SMatthew Ahrens 		    (fromsnap[fsnamelen] != '@' &&
105678f17100SMatthew Ahrens 		    fromsnap[fsnamelen] != '#')) {
105778f17100SMatthew Ahrens 			is_clone = B_TRUE;
105878f17100SMatthew Ahrens 		}
105978f17100SMatthew Ahrens 
106078f17100SMatthew Ahrens 		if (strchr(fromsnap, '@')) {
106178f17100SMatthew Ahrens 			dsl_dataset_t *fromds;
106278f17100SMatthew Ahrens 			err = dsl_dataset_hold(dp, fromsnap, FTAG, &fromds);
106378f17100SMatthew Ahrens 			if (err == 0) {
106478f17100SMatthew Ahrens 				if (!dsl_dataset_is_before(ds, fromds, 0))
106578f17100SMatthew Ahrens 					err = SET_ERROR(EXDEV);
106678f17100SMatthew Ahrens 				zb.zbm_creation_time =
1067c1379625SJustin T. Gibbs 				    dsl_dataset_phys(fromds)->ds_creation_time;
106878f17100SMatthew Ahrens 				zb.zbm_creation_txg =
1069c1379625SJustin T. Gibbs 				    dsl_dataset_phys(fromds)->ds_creation_txg;
1070c1379625SJustin T. Gibbs 				zb.zbm_guid = dsl_dataset_phys(fromds)->ds_guid;
107178f17100SMatthew Ahrens 				is_clone = (ds->ds_dir != fromds->ds_dir);
107278f17100SMatthew Ahrens 				dsl_dataset_rele(fromds, FTAG);
107378f17100SMatthew Ahrens 			}
107478f17100SMatthew Ahrens 		} else {
107578f17100SMatthew Ahrens 			err = dsl_bookmark_lookup(dp, fromsnap, ds, &zb);
107678f17100SMatthew Ahrens 		}
10773b2aab18SMatthew Ahrens 		if (err != 0) {
10783b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
10793b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
10803b2aab18SMatthew Ahrens 			return (err);
10813b2aab18SMatthew Ahrens 		}
1082b5152584SMatthew Ahrens 		err = dmu_send_impl(FTAG, dp, ds, &zb, is_clone,
10835602294fSDan Kimmel 		    embedok, large_block_ok, compressok,
10849c3fd121SMatthew Ahrens 		    outfd, resumeobj, resumeoff, vp, off);
108578f17100SMatthew Ahrens 	} else {
1086b5152584SMatthew Ahrens 		err = dmu_send_impl(FTAG, dp, ds, NULL, B_FALSE,
10875602294fSDan Kimmel 		    embedok, large_block_ok, compressok,
10889c3fd121SMatthew Ahrens 		    outfd, resumeobj, resumeoff, vp, off);
10893b2aab18SMatthew Ahrens 	}
109078f17100SMatthew Ahrens 	if (owned)
109178f17100SMatthew Ahrens 		dsl_dataset_disown(ds, FTAG);
109278f17100SMatthew Ahrens 	else
109378f17100SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
109478f17100SMatthew Ahrens 	return (err);
10953b2aab18SMatthew Ahrens }
10963b2aab18SMatthew Ahrens 
1097643da460SMax Grossman static int
10985602294fSDan Kimmel dmu_adjust_send_estimate_for_indirects(dsl_dataset_t *ds, uint64_t uncompressed,
10995602294fSDan Kimmel     uint64_t compressed, boolean_t stream_compressed, uint64_t *sizep)
1100643da460SMax Grossman {
1101*df477c0aSPaul Dagnelie 	int err = 0;
11025602294fSDan Kimmel 	uint64_t size;
1103643da460SMax Grossman 	/*
1104643da460SMax Grossman 	 * Assume that space (both on-disk and in-stream) is dominated by
1105643da460SMax Grossman 	 * data.  We will adjust for indirect blocks and the copies property,
1106643da460SMax Grossman 	 * but ignore per-object space used (eg, dnodes and DRR_OBJECT records).
1107643da460SMax Grossman 	 */
11085602294fSDan Kimmel 	uint64_t recordsize;
11095602294fSDan Kimmel 	uint64_t record_count;
11100255edccSPaul Dagnelie 	objset_t *os;
11110255edccSPaul Dagnelie 	VERIFY0(dmu_objset_from_ds(ds, &os));
11125602294fSDan Kimmel 
11135602294fSDan Kimmel 	/* Assume all (uncompressed) blocks are recordsize. */
1114*df477c0aSPaul Dagnelie 	if (zfs_override_estimate_recordsize != 0) {
1115*df477c0aSPaul Dagnelie 		recordsize = zfs_override_estimate_recordsize;
1116*df477c0aSPaul Dagnelie 	} else if (os->os_phys->os_type == DMU_OST_ZVOL) {
11170255edccSPaul Dagnelie 		err = dsl_prop_get_int_ds(ds,
11180255edccSPaul Dagnelie 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &recordsize);
11190255edccSPaul Dagnelie 	} else {
11200255edccSPaul Dagnelie 		err = dsl_prop_get_int_ds(ds,
11210255edccSPaul Dagnelie 		    zfs_prop_to_name(ZFS_PROP_RECORDSIZE), &recordsize);
11220255edccSPaul Dagnelie 	}
11235602294fSDan Kimmel 	if (err != 0)
11245602294fSDan Kimmel 		return (err);
11255602294fSDan Kimmel 	record_count = uncompressed / recordsize;
11265602294fSDan Kimmel 
11275602294fSDan Kimmel 	/*
11285602294fSDan Kimmel 	 * If we're estimating a send size for a compressed stream, use the
11295602294fSDan Kimmel 	 * compressed data size to estimate the stream size. Otherwise, use the
11305602294fSDan Kimmel 	 * uncompressed data size.
11315602294fSDan Kimmel 	 */
11325602294fSDan Kimmel 	size = stream_compressed ? compressed : uncompressed;
1133643da460SMax Grossman 
1134643da460SMax Grossman 	/*
1135643da460SMax Grossman 	 * Subtract out approximate space used by indirect blocks.
1136643da460SMax Grossman 	 * Assume most space is used by data blocks (non-indirect, non-dnode).
11375602294fSDan Kimmel 	 * Assume no ditto blocks or internal fragmentation.
1138643da460SMax Grossman 	 *
1139643da460SMax Grossman 	 * Therefore, space used by indirect blocks is sizeof(blkptr_t) per
11405602294fSDan Kimmel 	 * block.
1141643da460SMax Grossman 	 */
11425602294fSDan Kimmel 	size -= record_count * sizeof (blkptr_t);
1143643da460SMax Grossman 
1144643da460SMax Grossman 	/* Add in the space for the record associated with each block. */
11455602294fSDan Kimmel 	size += record_count * sizeof (dmu_replay_record_t);
1146643da460SMax Grossman 
1147643da460SMax Grossman 	*sizep = size;
1148643da460SMax Grossman 
1149643da460SMax Grossman 	return (0);
1150643da460SMax Grossman }
1151643da460SMax Grossman 
11523b2aab18SMatthew Ahrens int
11535602294fSDan Kimmel dmu_send_estimate(dsl_dataset_t *ds, dsl_dataset_t *fromds,
11545602294fSDan Kimmel     boolean_t stream_compressed, uint64_t *sizep)
115519b94df9SMatthew Ahrens {
115619b94df9SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
115719b94df9SMatthew Ahrens 	int err;
11585602294fSDan Kimmel 	uint64_t uncomp, comp;
115919b94df9SMatthew Ahrens 
11603b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
11613b2aab18SMatthew Ahrens 
116219b94df9SMatthew Ahrens 	/* tosnap must be a snapshot */
1163bc9014e6SJustin Gibbs 	if (!ds->ds_is_snapshot)
1164be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
116519b94df9SMatthew Ahrens 
116624218bebSAndriy Gapon 	/* fromsnap, if provided, must be a snapshot */
116724218bebSAndriy Gapon 	if (fromds != NULL && !fromds->ds_is_snapshot)
116824218bebSAndriy Gapon 		return (SET_ERROR(EINVAL));
116924218bebSAndriy Gapon 
11704445fffbSMatthew Ahrens 	/*
11714445fffbSMatthew Ahrens 	 * fromsnap must be an earlier snapshot from the same fs as tosnap,
11724445fffbSMatthew Ahrens 	 * or the origin's fs.
11734445fffbSMatthew Ahrens 	 */
117478f17100SMatthew Ahrens 	if (fromds != NULL && !dsl_dataset_is_before(ds, fromds, 0))
1175be6fd75aSMatthew Ahrens 		return (SET_ERROR(EXDEV));
117619b94df9SMatthew Ahrens 
11775602294fSDan Kimmel 	/* Get compressed and uncompressed size estimates of changed data. */
117819b94df9SMatthew Ahrens 	if (fromds == NULL) {
11795602294fSDan Kimmel 		uncomp = dsl_dataset_phys(ds)->ds_uncompressed_bytes;
11805602294fSDan Kimmel 		comp = dsl_dataset_phys(ds)->ds_compressed_bytes;
118119b94df9SMatthew Ahrens 	} else {
11825602294fSDan Kimmel 		uint64_t used;
118319b94df9SMatthew Ahrens 		err = dsl_dataset_space_written(fromds, ds,
11845602294fSDan Kimmel 		    &used, &comp, &uncomp);
11853b2aab18SMatthew Ahrens 		if (err != 0)
118619b94df9SMatthew Ahrens 			return (err);
118719b94df9SMatthew Ahrens 	}
118819b94df9SMatthew Ahrens 
11895602294fSDan Kimmel 	err = dmu_adjust_send_estimate_for_indirects(ds, uncomp, comp,
11905602294fSDan Kimmel 	    stream_compressed, sizep);
11910255edccSPaul Dagnelie 	/*
11920255edccSPaul Dagnelie 	 * Add the size of the BEGIN and END records to the estimate.
11930255edccSPaul Dagnelie 	 */
11940255edccSPaul Dagnelie 	*sizep += 2 * sizeof (dmu_replay_record_t);
1195643da460SMax Grossman 	return (err);
1196643da460SMax Grossman }
1197643da460SMax Grossman 
11985602294fSDan Kimmel struct calculate_send_arg {
11995602294fSDan Kimmel 	uint64_t uncompressed;
12005602294fSDan Kimmel 	uint64_t compressed;
12015602294fSDan Kimmel };
12025602294fSDan Kimmel 
1203643da460SMax Grossman /*
1204643da460SMax Grossman  * Simple callback used to traverse the blocks of a snapshot and sum their
12055602294fSDan Kimmel  * uncompressed and compressed sizes.
1206643da460SMax Grossman  */
1207643da460SMax Grossman /* ARGSUSED */
1208643da460SMax Grossman static int
1209643da460SMax Grossman dmu_calculate_send_traversal(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1210643da460SMax Grossman     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
1211643da460SMax Grossman {
12125602294fSDan Kimmel 	struct calculate_send_arg *space = arg;
1213643da460SMax Grossman 	if (bp != NULL && !BP_IS_HOLE(bp)) {
12145602294fSDan Kimmel 		space->uncompressed += BP_GET_UCSIZE(bp);
12155602294fSDan Kimmel 		space->compressed += BP_GET_PSIZE(bp);
1216643da460SMax Grossman 	}
1217643da460SMax Grossman 	return (0);
1218643da460SMax Grossman }
1219643da460SMax Grossman 
1220643da460SMax Grossman /*
1221643da460SMax Grossman  * Given a desination snapshot and a TXG, calculate the approximate size of a
1222643da460SMax Grossman  * send stream sent from that TXG. from_txg may be zero, indicating that the
1223643da460SMax Grossman  * whole snapshot will be sent.
1224643da460SMax Grossman  */
1225643da460SMax Grossman int
1226643da460SMax Grossman dmu_send_estimate_from_txg(dsl_dataset_t *ds, uint64_t from_txg,
12275602294fSDan Kimmel     boolean_t stream_compressed, uint64_t *sizep)
1228643da460SMax Grossman {
1229643da460SMax Grossman 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1230643da460SMax Grossman 	int err;
12315602294fSDan Kimmel 	struct calculate_send_arg size = { 0 };
1232643da460SMax Grossman 
1233643da460SMax Grossman 	ASSERT(dsl_pool_config_held(dp));
1234643da460SMax Grossman 
1235643da460SMax Grossman 	/* tosnap must be a snapshot */
12365602294fSDan Kimmel 	if (!ds->ds_is_snapshot)
1237643da460SMax Grossman 		return (SET_ERROR(EINVAL));
1238643da460SMax Grossman 
1239643da460SMax Grossman 	/* verify that from_txg is before the provided snapshot was taken */
1240643da460SMax Grossman 	if (from_txg >= dsl_dataset_phys(ds)->ds_creation_txg) {
1241643da460SMax Grossman 		return (SET_ERROR(EXDEV));
1242643da460SMax Grossman 	}
124319b94df9SMatthew Ahrens 
124419b94df9SMatthew Ahrens 	/*
1245643da460SMax Grossman 	 * traverse the blocks of the snapshot with birth times after
1246643da460SMax Grossman 	 * from_txg, summing their uncompressed size
124719b94df9SMatthew Ahrens 	 */
1248643da460SMax Grossman 	err = traverse_dataset(ds, from_txg, TRAVERSE_POST,
1249643da460SMax Grossman 	    dmu_calculate_send_traversal, &size);
1250643da460SMax Grossman 	if (err)
125119b94df9SMatthew Ahrens 		return (err);
125219b94df9SMatthew Ahrens 
12535602294fSDan Kimmel 	err = dmu_adjust_send_estimate_for_indirects(ds, size.uncompressed,
12545602294fSDan Kimmel 	    size.compressed, stream_compressed, sizep);
1255643da460SMax Grossman 	return (err);
125619b94df9SMatthew Ahrens }
125719b94df9SMatthew Ahrens 
12583b2aab18SMatthew Ahrens typedef struct dmu_recv_begin_arg {
12593b2aab18SMatthew Ahrens 	const char *drba_origin;
12603b2aab18SMatthew Ahrens 	dmu_recv_cookie_t *drba_cookie;
12613b2aab18SMatthew Ahrens 	cred_t *drba_cred;
126234f2f8cfSMatthew Ahrens 	uint64_t drba_snapobj;
12633b2aab18SMatthew Ahrens } dmu_recv_begin_arg_t;
1264f18faf3fSek 
1265f18faf3fSek static int
12663b2aab18SMatthew Ahrens recv_begin_check_existing_impl(dmu_recv_begin_arg_t *drba, dsl_dataset_t *ds,
12673b2aab18SMatthew Ahrens     uint64_t fromguid)
1268f18faf3fSek {
12693cb34c60Sahrens 	uint64_t val;
12703b2aab18SMatthew Ahrens 	int error;
12713b2aab18SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1272f18faf3fSek 
12733b2aab18SMatthew Ahrens 	/* temporary clone name must not exist */
12743b2aab18SMatthew Ahrens 	error = zap_lookup(dp->dp_meta_objset,
1275c1379625SJustin T. Gibbs 	    dsl_dir_phys(ds->ds_dir)->dd_child_dir_zapobj, recv_clone_name,
12763b2aab18SMatthew Ahrens 	    8, 1, &val);
12773b2aab18SMatthew Ahrens 	if (error != ENOENT)
12783b2aab18SMatthew Ahrens 		return (error == 0 ? EBUSY : error);
12793b2aab18SMatthew Ahrens 
1280feaa74e4SMark Maybee 	/* new snapshot name must not exist */
12813b2aab18SMatthew Ahrens 	error = zap_lookup(dp->dp_meta_objset,
1282c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_snapnames_zapobj,
1283c1379625SJustin T. Gibbs 	    drba->drba_cookie->drc_tosnap, 8, 1, &val);
12843b2aab18SMatthew Ahrens 	if (error != ENOENT)
12853b2aab18SMatthew Ahrens 		return (error == 0 ? EEXIST : error);
1286feaa74e4SMark Maybee 
1287a2afb611SJerry Jelinek 	/*
1288a2afb611SJerry Jelinek 	 * Check snapshot limit before receiving. We'll recheck again at the
1289a2afb611SJerry Jelinek 	 * end, but might as well abort before receiving if we're already over
1290a2afb611SJerry Jelinek 	 * the limit.
1291a2afb611SJerry Jelinek 	 *
1292a2afb611SJerry Jelinek 	 * Note that we do not check the file system limit with
1293a2afb611SJerry Jelinek 	 * dsl_dir_fscount_check because the temporary %clones don't count
1294a2afb611SJerry Jelinek 	 * against that limit.
1295a2afb611SJerry Jelinek 	 */
1296a2afb611SJerry Jelinek 	error = dsl_fs_ss_limit_check(ds->ds_dir, 1, ZFS_PROP_SNAPSHOT_LIMIT,
1297a2afb611SJerry Jelinek 	    NULL, drba->drba_cred);
1298a2afb611SJerry Jelinek 	if (error != 0)
1299a2afb611SJerry Jelinek 		return (error);
1300a2afb611SJerry Jelinek 
13013b2aab18SMatthew Ahrens 	if (fromguid != 0) {
130234f2f8cfSMatthew Ahrens 		dsl_dataset_t *snap;
1303c1379625SJustin T. Gibbs 		uint64_t obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
130434f2f8cfSMatthew Ahrens 
130534f2f8cfSMatthew Ahrens 		/* Find snapshot in this dir that matches fromguid. */
130634f2f8cfSMatthew Ahrens 		while (obj != 0) {
130734f2f8cfSMatthew Ahrens 			error = dsl_dataset_hold_obj(dp, obj, FTAG,
130834f2f8cfSMatthew Ahrens 			    &snap);
130934f2f8cfSMatthew Ahrens 			if (error != 0)
131034f2f8cfSMatthew Ahrens 				return (SET_ERROR(ENODEV));
131134f2f8cfSMatthew Ahrens 			if (snap->ds_dir != ds->ds_dir) {
131234f2f8cfSMatthew Ahrens 				dsl_dataset_rele(snap, FTAG);
131334f2f8cfSMatthew Ahrens 				return (SET_ERROR(ENODEV));
131434f2f8cfSMatthew Ahrens 			}
1315c1379625SJustin T. Gibbs 			if (dsl_dataset_phys(snap)->ds_guid == fromguid)
131634f2f8cfSMatthew Ahrens 				break;
1317c1379625SJustin T. Gibbs 			obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
131834f2f8cfSMatthew Ahrens 			dsl_dataset_rele(snap, FTAG);
131934f2f8cfSMatthew Ahrens 		}
132034f2f8cfSMatthew Ahrens 		if (obj == 0)
1321be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENODEV));
132292241e0bSTom Erickson 
132334f2f8cfSMatthew Ahrens 		if (drba->drba_cookie->drc_force) {
132434f2f8cfSMatthew Ahrens 			drba->drba_snapobj = obj;
132534f2f8cfSMatthew Ahrens 		} else {
132634f2f8cfSMatthew Ahrens 			/*
132734f2f8cfSMatthew Ahrens 			 * If we are not forcing, there must be no
132834f2f8cfSMatthew Ahrens 			 * changes since fromsnap.
132934f2f8cfSMatthew Ahrens 			 */
133034f2f8cfSMatthew Ahrens 			if (dsl_dataset_modified_since_snap(ds, snap)) {
133192241e0bSTom Erickson 				dsl_dataset_rele(snap, FTAG);
133234f2f8cfSMatthew Ahrens 				return (SET_ERROR(ETXTBSY));
133392241e0bSTom Erickson 			}
133434f2f8cfSMatthew Ahrens 			drba->drba_snapobj = ds->ds_prev->ds_object;
133592241e0bSTom Erickson 		}
133634f2f8cfSMatthew Ahrens 
133734f2f8cfSMatthew Ahrens 		dsl_dataset_rele(snap, FTAG);
1338ae46e4c7SMatthew Ahrens 	} else {
13395bae108fSAndriy Gapon 		/* if full, then must be forced */
13405bae108fSAndriy Gapon 		if (!drba->drba_cookie->drc_force)
13415bae108fSAndriy Gapon 			return (SET_ERROR(EEXIST));
13425bae108fSAndriy Gapon 		/* start from $ORIGIN@$ORIGIN, if supported */
13435bae108fSAndriy Gapon 		drba->drba_snapobj = dp->dp_origin_snap != NULL ?
13445bae108fSAndriy Gapon 		    dp->dp_origin_snap->ds_object : 0;
1345ae46e4c7SMatthew Ahrens 	}
13463cb34c60Sahrens 
13473cb34c60Sahrens 	return (0);
13483b2aab18SMatthew Ahrens 
13493b2aab18SMatthew Ahrens }
13503b2aab18SMatthew Ahrens 
13513b2aab18SMatthew Ahrens static int
13523b2aab18SMatthew Ahrens dmu_recv_begin_check(void *arg, dmu_tx_t *tx)
13533b2aab18SMatthew Ahrens {
13543b2aab18SMatthew Ahrens 	dmu_recv_begin_arg_t *drba = arg;
13553b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
13563b2aab18SMatthew Ahrens 	struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
13573b2aab18SMatthew Ahrens 	uint64_t fromguid = drrb->drr_fromguid;
13583b2aab18SMatthew Ahrens 	int flags = drrb->drr_flags;
13593b2aab18SMatthew Ahrens 	int error;
13605d7b4d43SMatthew Ahrens 	uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
13613b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
13623b2aab18SMatthew Ahrens 	const char *tofs = drba->drba_cookie->drc_tofs;
13633b2aab18SMatthew Ahrens 
13643b2aab18SMatthew Ahrens 	/* already checked */
13653b2aab18SMatthew Ahrens 	ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
13669c3fd121SMatthew Ahrens 	ASSERT(!(featureflags & DMU_BACKUP_FEATURE_RESUMING));
13673b2aab18SMatthew Ahrens 
13683b2aab18SMatthew Ahrens 	if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
13693b2aab18SMatthew Ahrens 	    DMU_COMPOUNDSTREAM ||
13703b2aab18SMatthew Ahrens 	    drrb->drr_type >= DMU_OST_NUMTYPES ||
13713b2aab18SMatthew Ahrens 	    ((flags & DRR_FLAG_CLONE) && drba->drba_origin == NULL))
1372be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
13733b2aab18SMatthew Ahrens 
13743b2aab18SMatthew Ahrens 	/* Verify pool version supports SA if SA_SPILL feature set */
13755d7b4d43SMatthew Ahrens 	if ((featureflags & DMU_BACKUP_FEATURE_SA_SPILL) &&
13765d7b4d43SMatthew Ahrens 	    spa_version(dp->dp_spa) < SPA_VERSION_SA)
13775d7b4d43SMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
13785d7b4d43SMatthew Ahrens 
13799c3fd121SMatthew Ahrens 	if (drba->drba_cookie->drc_resumable &&
13809c3fd121SMatthew Ahrens 	    !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EXTENSIBLE_DATASET))
13819c3fd121SMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
13829c3fd121SMatthew Ahrens 
13835d7b4d43SMatthew Ahrens 	/*
13845d7b4d43SMatthew Ahrens 	 * The receiving code doesn't know how to translate a WRITE_EMBEDDED
13855602294fSDan Kimmel 	 * record to a plain WRITE record, so the pool must have the
13865d7b4d43SMatthew Ahrens 	 * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
13875d7b4d43SMatthew Ahrens 	 * records.  Same with WRITE_EMBEDDED records that use LZ4 compression.
13885d7b4d43SMatthew Ahrens 	 */
13895d7b4d43SMatthew Ahrens 	if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) &&
13905d7b4d43SMatthew Ahrens 	    !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA))
13915d7b4d43SMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
13925602294fSDan Kimmel 	if ((featureflags & DMU_BACKUP_FEATURE_LZ4) &&
13935d7b4d43SMatthew Ahrens 	    !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
1394be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
13953b2aab18SMatthew Ahrens 
1396b5152584SMatthew Ahrens 	/*
1397b5152584SMatthew Ahrens 	 * The receiving code doesn't know how to translate large blocks
1398b5152584SMatthew Ahrens 	 * to smaller ones, so the pool must have the LARGE_BLOCKS
1399b5152584SMatthew Ahrens 	 * feature enabled if the stream has LARGE_BLOCKS.
1400b5152584SMatthew Ahrens 	 */
1401b5152584SMatthew Ahrens 	if ((featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
1402b5152584SMatthew Ahrens 	    !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LARGE_BLOCKS))
1403b5152584SMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
1404b5152584SMatthew Ahrens 
14053b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
14063b2aab18SMatthew Ahrens 	if (error == 0) {
14073b2aab18SMatthew Ahrens 		/* target fs already exists; recv into temp clone */
14083b2aab18SMatthew Ahrens 
14093b2aab18SMatthew Ahrens 		/* Can't recv a clone into an existing fs */
141068ecb2ecSPaul Dagnelie 		if (flags & DRR_FLAG_CLONE || drba->drba_origin) {
14113b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
1412be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
14133b2aab18SMatthew Ahrens 		}
14143b2aab18SMatthew Ahrens 
14153b2aab18SMatthew Ahrens 		error = recv_begin_check_existing_impl(drba, ds, fromguid);
14163b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
14173b2aab18SMatthew Ahrens 	} else if (error == ENOENT) {
14183b2aab18SMatthew Ahrens 		/* target fs does not exist; must be a full backup or clone */
14199adfa60dSMatthew Ahrens 		char buf[ZFS_MAX_DATASET_NAME_LEN];
14203b2aab18SMatthew Ahrens 
14213b2aab18SMatthew Ahrens 		/*
14223b2aab18SMatthew Ahrens 		 * If it's a non-clone incremental, we are missing the
14233b2aab18SMatthew Ahrens 		 * target fs, so fail the recv.
14243b2aab18SMatthew Ahrens 		 */
1425a2cdcdd2SPaul Dagnelie 		if (fromguid != 0 && !(flags & DRR_FLAG_CLONE ||
1426a2cdcdd2SPaul Dagnelie 		    drba->drba_origin))
1427be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOENT));
14283b2aab18SMatthew Ahrens 
142968ecb2ecSPaul Dagnelie 		/*
143068ecb2ecSPaul Dagnelie 		 * If we're receiving a full send as a clone, and it doesn't
143168ecb2ecSPaul Dagnelie 		 * contain all the necessary free records and freeobject
143268ecb2ecSPaul Dagnelie 		 * records, reject it.
143368ecb2ecSPaul Dagnelie 		 */
143468ecb2ecSPaul Dagnelie 		if (fromguid == 0 && drba->drba_origin &&
143568ecb2ecSPaul Dagnelie 		    !(flags & DRR_FLAG_FREERECORDS))
143668ecb2ecSPaul Dagnelie 			return (SET_ERROR(EINVAL));
143768ecb2ecSPaul Dagnelie 
14383b2aab18SMatthew Ahrens 		/* Open the parent of tofs */
14399adfa60dSMatthew Ahrens 		ASSERT3U(strlen(tofs), <, sizeof (buf));
14403b2aab18SMatthew Ahrens 		(void) strlcpy(buf, tofs, strrchr(tofs, '/') - tofs + 1);
14413b2aab18SMatthew Ahrens 		error = dsl_dataset_hold(dp, buf, FTAG, &ds);
14423b2aab18SMatthew Ahrens 		if (error != 0)
14433b2aab18SMatthew Ahrens 			return (error);
14443b2aab18SMatthew Ahrens 
1445a2afb611SJerry Jelinek 		/*
1446a2afb611SJerry Jelinek 		 * Check filesystem and snapshot limits before receiving. We'll
1447a2afb611SJerry Jelinek 		 * recheck snapshot limits again at the end (we create the
1448a2afb611SJerry Jelinek 		 * filesystems and increment those counts during begin_sync).
1449a2afb611SJerry Jelinek 		 */
1450a2afb611SJerry Jelinek 		error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
1451a2afb611SJerry Jelinek 		    ZFS_PROP_FILESYSTEM_LIMIT, NULL, drba->drba_cred);
1452a2afb611SJerry Jelinek 		if (error != 0) {
1453a2afb611SJerry Jelinek 			dsl_dataset_rele(ds, FTAG);
1454a2afb611SJerry Jelinek 			return (error);
1455a2afb611SJerry Jelinek 		}
1456a2afb611SJerry Jelinek 
1457a2afb611SJerry Jelinek 		error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
1458a2afb611SJerry Jelinek 		    ZFS_PROP_SNAPSHOT_LIMIT, NULL, drba->drba_cred);
1459a2afb611SJerry Jelinek 		if (error != 0) {
1460a2afb611SJerry Jelinek 			dsl_dataset_rele(ds, FTAG);
1461a2afb611SJerry Jelinek 			return (error);
1462a2afb611SJerry Jelinek 		}
1463a2afb611SJerry Jelinek 
14643b2aab18SMatthew Ahrens 		if (drba->drba_origin != NULL) {
14653b2aab18SMatthew Ahrens 			dsl_dataset_t *origin;
14663b2aab18SMatthew Ahrens 			error = dsl_dataset_hold(dp, drba->drba_origin,
14673b2aab18SMatthew Ahrens 			    FTAG, &origin);
14683b2aab18SMatthew Ahrens 			if (error != 0) {
14693b2aab18SMatthew Ahrens 				dsl_dataset_rele(ds, FTAG);
14703b2aab18SMatthew Ahrens 				return (error);
14713b2aab18SMatthew Ahrens 			}
1472bc9014e6SJustin Gibbs 			if (!origin->ds_is_snapshot) {
14733b2aab18SMatthew Ahrens 				dsl_dataset_rele(origin, FTAG);
14743b2aab18SMatthew Ahrens 				dsl_dataset_rele(ds, FTAG);
1475be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
14763b2aab18SMatthew Ahrens 			}
147768ecb2ecSPaul Dagnelie 			if (dsl_dataset_phys(origin)->ds_guid != fromguid &&
147868ecb2ecSPaul Dagnelie 			    fromguid != 0) {
14793b2aab18SMatthew Ahrens 				dsl_dataset_rele(origin, FTAG);
14803b2aab18SMatthew Ahrens 				dsl_dataset_rele(ds, FTAG);
1481be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENODEV));
14823b2aab18SMatthew Ahrens 			}
14833b2aab18SMatthew Ahrens 			dsl_dataset_rele(origin, FTAG);
14843b2aab18SMatthew Ahrens 		}
14853b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
14863b2aab18SMatthew Ahrens 		error = 0;
14873b2aab18SMatthew Ahrens 	}
14883b2aab18SMatthew Ahrens 	return (error);
1489f18faf3fSek }
1490f18faf3fSek 
1491f18faf3fSek static void
14923b2aab18SMatthew Ahrens dmu_recv_begin_sync(void *arg, dmu_tx_t *tx)
1493f18faf3fSek {
14943b2aab18SMatthew Ahrens 	dmu_recv_begin_arg_t *drba = arg;
14953b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
14969c3fd121SMatthew Ahrens 	objset_t *mos = dp->dp_meta_objset;
14973b2aab18SMatthew Ahrens 	struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
14983b2aab18SMatthew Ahrens 	const char *tofs = drba->drba_cookie->drc_tofs;
14993b2aab18SMatthew Ahrens 	dsl_dataset_t *ds, *newds;
1500f18faf3fSek 	uint64_t dsobj;
15013b2aab18SMatthew Ahrens 	int error;
15029c3fd121SMatthew Ahrens 	uint64_t crflags = 0;
15033b2aab18SMatthew Ahrens 
15049c3fd121SMatthew Ahrens 	if (drrb->drr_flags & DRR_FLAG_CI_DATA)
15059c3fd121SMatthew Ahrens 		crflags |= DS_FLAG_CI_DATASET;
1506f18faf3fSek 
15073b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
15083b2aab18SMatthew Ahrens 	if (error == 0) {
15093b2aab18SMatthew Ahrens 		/* create temporary clone */
151034f2f8cfSMatthew Ahrens 		dsl_dataset_t *snap = NULL;
151134f2f8cfSMatthew Ahrens 		if (drba->drba_snapobj != 0) {
151234f2f8cfSMatthew Ahrens 			VERIFY0(dsl_dataset_hold_obj(dp,
151334f2f8cfSMatthew Ahrens 			    drba->drba_snapobj, FTAG, &snap));
151434f2f8cfSMatthew Ahrens 		}
15153b2aab18SMatthew Ahrens 		dsobj = dsl_dataset_create_sync(ds->ds_dir, recv_clone_name,
151634f2f8cfSMatthew Ahrens 		    snap, crflags, drba->drba_cred, tx);
1517f40b29ceSPaul Dagnelie 		if (drba->drba_snapobj != 0)
1518f40b29ceSPaul Dagnelie 			dsl_dataset_rele(snap, FTAG);
15193b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
15203b2aab18SMatthew Ahrens 	} else {
15213b2aab18SMatthew Ahrens 		dsl_dir_t *dd;
15223b2aab18SMatthew Ahrens 		const char *tail;
15233b2aab18SMatthew Ahrens 		dsl_dataset_t *origin = NULL;
15243b2aab18SMatthew Ahrens 
15253b2aab18SMatthew Ahrens 		VERIFY0(dsl_dir_hold(dp, tofs, FTAG, &dd, &tail));
15263b2aab18SMatthew Ahrens 
15273b2aab18SMatthew Ahrens 		if (drba->drba_origin != NULL) {
15283b2aab18SMatthew Ahrens 			VERIFY0(dsl_dataset_hold(dp, drba->drba_origin,
15293b2aab18SMatthew Ahrens 			    FTAG, &origin));
15303b2aab18SMatthew Ahrens 		}
15313b2aab18SMatthew Ahrens 
15323b2aab18SMatthew Ahrens 		/* Create new dataset. */
15333b2aab18SMatthew Ahrens 		dsobj = dsl_dataset_create_sync(dd,
15343b2aab18SMatthew Ahrens 		    strrchr(tofs, '/') + 1,
15353b2aab18SMatthew Ahrens 		    origin, crflags, drba->drba_cred, tx);
15363b2aab18SMatthew Ahrens 		if (origin != NULL)
15373b2aab18SMatthew Ahrens 			dsl_dataset_rele(origin, FTAG);
15383b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
15393b2aab18SMatthew Ahrens 		drba->drba_cookie->drc_newfs = B_TRUE;
15403b2aab18SMatthew Ahrens 	}
15413b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_own_obj(dp, dsobj, dmu_recv_tag, &newds));
15423b2aab18SMatthew Ahrens 
15439c3fd121SMatthew Ahrens 	if (drba->drba_cookie->drc_resumable) {
15449c3fd121SMatthew Ahrens 		dsl_dataset_zapify(newds, tx);
15459c3fd121SMatthew Ahrens 		if (drrb->drr_fromguid != 0) {
15469c3fd121SMatthew Ahrens 			VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_FROMGUID,
15479c3fd121SMatthew Ahrens 			    8, 1, &drrb->drr_fromguid, tx));
15489c3fd121SMatthew Ahrens 		}
15499c3fd121SMatthew Ahrens 		VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_TOGUID,
15509c3fd121SMatthew Ahrens 		    8, 1, &drrb->drr_toguid, tx));
15519c3fd121SMatthew Ahrens 		VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_TONAME,
15529c3fd121SMatthew Ahrens 		    1, strlen(drrb->drr_toname) + 1, drrb->drr_toname, tx));
15539c3fd121SMatthew Ahrens 		uint64_t one = 1;
15549c3fd121SMatthew Ahrens 		uint64_t zero = 0;
15559c3fd121SMatthew Ahrens 		VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_OBJECT,
15569c3fd121SMatthew Ahrens 		    8, 1, &one, tx));
15579c3fd121SMatthew Ahrens 		VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_OFFSET,
15589c3fd121SMatthew Ahrens 		    8, 1, &zero, tx));
15599c3fd121SMatthew Ahrens 		VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_BYTES,
15609c3fd121SMatthew Ahrens 		    8, 1, &zero, tx));
15615602294fSDan Kimmel 		if (DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
15625602294fSDan Kimmel 		    DMU_BACKUP_FEATURE_LARGE_BLOCKS) {
15635602294fSDan Kimmel 			VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_LARGEBLOCK,
15645602294fSDan Kimmel 			    8, 1, &one, tx));
15655602294fSDan Kimmel 		}
15669c3fd121SMatthew Ahrens 		if (DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
15679c3fd121SMatthew Ahrens 		    DMU_BACKUP_FEATURE_EMBED_DATA) {
15689c3fd121SMatthew Ahrens 			VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_EMBEDOK,
15699c3fd121SMatthew Ahrens 			    8, 1, &one, tx));
15709c3fd121SMatthew Ahrens 		}
15715602294fSDan Kimmel 		if (DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
15725602294fSDan Kimmel 		    DMU_BACKUP_FEATURE_COMPRESSED) {
15735602294fSDan Kimmel 			VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_COMPRESSOK,
15745602294fSDan Kimmel 			    8, 1, &one, tx));
15755602294fSDan Kimmel 		}
15769c3fd121SMatthew Ahrens 	}
15779c3fd121SMatthew Ahrens 
15783b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(newds->ds_dbuf, tx);
1579c1379625SJustin T. Gibbs 	dsl_dataset_phys(newds)->ds_flags |= DS_FLAG_INCONSISTENT;
15803cb34c60Sahrens 
1581ae46e4c7SMatthew Ahrens 	/*
1582ae46e4c7SMatthew Ahrens 	 * If we actually created a non-clone, we need to create the
1583ae46e4c7SMatthew Ahrens 	 * objset in our new dataset.
1584ae46e4c7SMatthew Ahrens 	 */
1585c166b69dSPaul Dagnelie 	rrw_enter(&newds->ds_bp_rwlock, RW_READER, FTAG);
15863b2aab18SMatthew Ahrens 	if (BP_IS_HOLE(dsl_dataset_get_blkptr(newds))) {
1587ae46e4c7SMatthew Ahrens 		(void) dmu_objset_create_impl(dp->dp_spa,
15883b2aab18SMatthew Ahrens 		    newds, dsl_dataset_get_blkptr(newds), drrb->drr_type, tx);
1589ae46e4c7SMatthew Ahrens 	}
1590c166b69dSPaul Dagnelie 	rrw_exit(&newds->ds_bp_rwlock, FTAG);
1591ae46e4c7SMatthew Ahrens 
15923b2aab18SMatthew Ahrens 	drba->drba_cookie->drc_ds = newds;
1593dc7cd546SMark Shellenbaum 
15943b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(newds, "receive", tx, "");
1595dc7cd546SMark Shellenbaum }
1596dc7cd546SMark Shellenbaum 
15979c3fd121SMatthew Ahrens static int
15989c3fd121SMatthew Ahrens dmu_recv_resume_begin_check(void *arg, dmu_tx_t *tx)
15999c3fd121SMatthew Ahrens {
16009c3fd121SMatthew Ahrens 	dmu_recv_begin_arg_t *drba = arg;
16019c3fd121SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
16029c3fd121SMatthew Ahrens 	struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
16039c3fd121SMatthew Ahrens 	int error;
16049c3fd121SMatthew Ahrens 	uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
16059c3fd121SMatthew Ahrens 	dsl_dataset_t *ds;
16069c3fd121SMatthew Ahrens 	const char *tofs = drba->drba_cookie->drc_tofs;
16079c3fd121SMatthew Ahrens 
16089c3fd121SMatthew Ahrens 	/* already checked */
16099c3fd121SMatthew Ahrens 	ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
16109c3fd121SMatthew Ahrens 	ASSERT(featureflags & DMU_BACKUP_FEATURE_RESUMING);
16119c3fd121SMatthew Ahrens 
16129c3fd121SMatthew Ahrens 	if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
16139c3fd121SMatthew Ahrens 	    DMU_COMPOUNDSTREAM ||
16149c3fd121SMatthew Ahrens 	    drrb->drr_type >= DMU_OST_NUMTYPES)
16159c3fd121SMatthew Ahrens 		return (SET_ERROR(EINVAL));
16169c3fd121SMatthew Ahrens 
16179c3fd121SMatthew Ahrens 	/* Verify pool version supports SA if SA_SPILL feature set */
16189c3fd121SMatthew Ahrens 	if ((featureflags & DMU_BACKUP_FEATURE_SA_SPILL) &&
16199c3fd121SMatthew Ahrens 	    spa_version(dp->dp_spa) < SPA_VERSION_SA)
16209c3fd121SMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
16219c3fd121SMatthew Ahrens 
16229c3fd121SMatthew Ahrens 	/*
16239c3fd121SMatthew Ahrens 	 * The receiving code doesn't know how to translate a WRITE_EMBEDDED
16249c3fd121SMatthew Ahrens 	 * record to a plain WRITE record, so the pool must have the
16259c3fd121SMatthew Ahrens 	 * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
16269c3fd121SMatthew Ahrens 	 * records.  Same with WRITE_EMBEDDED records that use LZ4 compression.
16279c3fd121SMatthew Ahrens 	 */
16289c3fd121SMatthew Ahrens 	if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) &&
16299c3fd121SMatthew Ahrens 	    !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA))
16309c3fd121SMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
16315602294fSDan Kimmel 	if ((featureflags & DMU_BACKUP_FEATURE_LZ4) &&
16329c3fd121SMatthew Ahrens 	    !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
16339c3fd121SMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
16349c3fd121SMatthew Ahrens 
16359adfa60dSMatthew Ahrens 	/* 6 extra bytes for /%recv */
16369adfa60dSMatthew Ahrens 	char recvname[ZFS_MAX_DATASET_NAME_LEN + 6];
16379c3fd121SMatthew Ahrens 
16389c3fd121SMatthew Ahrens 	(void) snprintf(recvname, sizeof (recvname), "%s/%s",
16399c3fd121SMatthew Ahrens 	    tofs, recv_clone_name);
16409c3fd121SMatthew Ahrens 
16419c3fd121SMatthew Ahrens 	if (dsl_dataset_hold(dp, recvname, FTAG, &ds) != 0) {
16429c3fd121SMatthew Ahrens 		/* %recv does not exist; continue in tofs */
16439c3fd121SMatthew Ahrens 		error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
16449c3fd121SMatthew Ahrens 		if (error != 0)
16459c3fd121SMatthew Ahrens 			return (error);
16469c3fd121SMatthew Ahrens 	}
16479c3fd121SMatthew Ahrens 
16489c3fd121SMatthew Ahrens 	/* check that ds is marked inconsistent */
16499c3fd121SMatthew Ahrens 	if (!DS_IS_INCONSISTENT(ds)) {
16509c3fd121SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
16519c3fd121SMatthew Ahrens 		return (SET_ERROR(EINVAL));
16529c3fd121SMatthew Ahrens 	}
16539c3fd121SMatthew Ahrens 
16549c3fd121SMatthew Ahrens 	/* check that there is resuming data, and that the toguid matches */
16559c3fd121SMatthew Ahrens 	if (!dsl_dataset_is_zapified(ds)) {
16569c3fd121SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
16579c3fd121SMatthew Ahrens 		return (SET_ERROR(EINVAL));
16589c3fd121SMatthew Ahrens 	}
16599c3fd121SMatthew Ahrens 	uint64_t val;
16609c3fd121SMatthew Ahrens 	error = zap_lookup(dp->dp_meta_objset, ds->ds_object,
16619c3fd121SMatthew Ahrens 	    DS_FIELD_RESUME_TOGUID, sizeof (val), 1, &val);
16629c3fd121SMatthew Ahrens 	if (error != 0 || drrb->drr_toguid != val) {
16639c3fd121SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
16649c3fd121SMatthew Ahrens 		return (SET_ERROR(EINVAL));
16659c3fd121SMatthew Ahrens 	}
16669c3fd121SMatthew Ahrens 
16679c3fd121SMatthew Ahrens 	/*
16689c3fd121SMatthew Ahrens 	 * Check if the receive is still running.  If so, it will be owned.
16699c3fd121SMatthew Ahrens 	 * Note that nothing else can own the dataset (e.g. after the receive
16709c3fd121SMatthew Ahrens 	 * fails) because it will be marked inconsistent.
16719c3fd121SMatthew Ahrens 	 */
16729c3fd121SMatthew Ahrens 	if (dsl_dataset_has_owner(ds)) {
16739c3fd121SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
16749c3fd121SMatthew Ahrens 		return (SET_ERROR(EBUSY));
16759c3fd121SMatthew Ahrens 	}
16769c3fd121SMatthew Ahrens 
16779c3fd121SMatthew Ahrens 	/* There should not be any snapshots of this fs yet. */
16789c3fd121SMatthew Ahrens 	if (ds->ds_prev != NULL && ds->ds_prev->ds_dir == ds->ds_dir) {
16799c3fd121SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
16809c3fd121SMatthew Ahrens 		return (SET_ERROR(EINVAL));
16819c3fd121SMatthew Ahrens 	}
16829c3fd121SMatthew Ahrens 
16839c3fd121SMatthew Ahrens 	/*
16849c3fd121SMatthew Ahrens 	 * Note: resume point will be checked when we process the first WRITE
16859c3fd121SMatthew Ahrens 	 * record.
16869c3fd121SMatthew Ahrens 	 */
16879c3fd121SMatthew Ahrens 
16889c3fd121SMatthew Ahrens 	/* check that the origin matches */
16899c3fd121SMatthew Ahrens 	val = 0;
16909c3fd121SMatthew Ahrens 	(void) zap_lookup(dp->dp_meta_objset, ds->ds_object,
16919c3fd121SMatthew Ahrens 	    DS_FIELD_RESUME_FROMGUID, sizeof (val), 1, &val);
16929c3fd121SMatthew Ahrens 	if (drrb->drr_fromguid != val) {
16939c3fd121SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
16949c3fd121SMatthew Ahrens 		return (SET_ERROR(EINVAL));
16959c3fd121SMatthew Ahrens 	}
16969c3fd121SMatthew Ahrens 
16979c3fd121SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
16989c3fd121SMatthew Ahrens 	return (0);
16999c3fd121SMatthew Ahrens }
17009c3fd121SMatthew Ahrens 
17019c3fd121SMatthew Ahrens static void
17029c3fd121SMatthew Ahrens dmu_recv_resume_begin_sync(void *arg, dmu_tx_t *tx)
17039c3fd121SMatthew Ahrens {
17049c3fd121SMatthew Ahrens 	dmu_recv_begin_arg_t *drba = arg;
17059c3fd121SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
17069c3fd121SMatthew Ahrens 	const char *tofs = drba->drba_cookie->drc_tofs;
17079c3fd121SMatthew Ahrens 	dsl_dataset_t *ds;
17089c3fd121SMatthew Ahrens 	uint64_t dsobj;
17099adfa60dSMatthew Ahrens 	/* 6 extra bytes for /%recv */
17109adfa60dSMatthew Ahrens 	char recvname[ZFS_MAX_DATASET_NAME_LEN + 6];
17119c3fd121SMatthew Ahrens 
17129c3fd121SMatthew Ahrens 	(void) snprintf(recvname, sizeof (recvname), "%s/%s",
17139c3fd121SMatthew Ahrens 	    tofs, recv_clone_name);
17149c3fd121SMatthew Ahrens 
17159c3fd121SMatthew Ahrens 	if (dsl_dataset_hold(dp, recvname, FTAG, &ds) != 0) {
17169c3fd121SMatthew Ahrens 		/* %recv does not exist; continue in tofs */
17179c3fd121SMatthew Ahrens 		VERIFY0(dsl_dataset_hold(dp, tofs, FTAG, &ds));
17189c3fd121SMatthew Ahrens 		drba->drba_cookie->drc_newfs = B_TRUE;
17199c3fd121SMatthew Ahrens 	}
17209c3fd121SMatthew Ahrens 
17219c3fd121SMatthew Ahrens 	/* clear the inconsistent flag so that we can own it */
17229c3fd121SMatthew Ahrens 	ASSERT(DS_IS_INCONSISTENT(ds));
17239c3fd121SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
17249c3fd121SMatthew Ahrens 	dsl_dataset_phys(ds)->ds_flags &= ~DS_FLAG_INCONSISTENT;
17259c3fd121SMatthew Ahrens 	dsobj = ds->ds_object;
17269c3fd121SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
17279c3fd121SMatthew Ahrens 
17289c3fd121SMatthew Ahrens 	VERIFY0(dsl_dataset_own_obj(dp, dsobj, dmu_recv_tag, &ds));
17299c3fd121SMatthew Ahrens 
17309c3fd121SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
17319c3fd121SMatthew Ahrens 	dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_INCONSISTENT;
17329c3fd121SMatthew Ahrens 
1733c166b69dSPaul Dagnelie 	rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
17349c3fd121SMatthew Ahrens 	ASSERT(!BP_IS_HOLE(dsl_dataset_get_blkptr(ds)));
1735c166b69dSPaul Dagnelie 	rrw_exit(&ds->ds_bp_rwlock, FTAG);
17369c3fd121SMatthew Ahrens 
17379c3fd121SMatthew Ahrens 	drba->drba_cookie->drc_ds = ds;
17389c3fd121SMatthew Ahrens 
17399c3fd121SMatthew Ahrens 	spa_history_log_internal_ds(ds, "resume receive", tx, "");
17409c3fd121SMatthew Ahrens }
17419c3fd121SMatthew Ahrens 
17423cb34c60Sahrens /*
17433cb34c60Sahrens  * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
17443cb34c60Sahrens  * succeeds; otherwise we will leak the holds on the datasets.
17453cb34c60Sahrens  */
17463cb34c60Sahrens int
17479c3fd121SMatthew Ahrens dmu_recv_begin(char *tofs, char *tosnap, dmu_replay_record_t *drr_begin,
17489c3fd121SMatthew Ahrens     boolean_t force, boolean_t resumable, char *origin, dmu_recv_cookie_t *drc)
1749efb80947Sahrens {
17503b2aab18SMatthew Ahrens 	dmu_recv_begin_arg_t drba = { 0 };
1751ab04eb8eStimh 
17523cb34c60Sahrens 	bzero(drc, sizeof (dmu_recv_cookie_t));
17539c3fd121SMatthew Ahrens 	drc->drc_drr_begin = drr_begin;
17549c3fd121SMatthew Ahrens 	drc->drc_drrb = &drr_begin->drr_u.drr_begin;
17553cb34c60Sahrens 	drc->drc_tosnap = tosnap;
17563b2aab18SMatthew Ahrens 	drc->drc_tofs = tofs;
17573cb34c60Sahrens 	drc->drc_force = force;
17589c3fd121SMatthew Ahrens 	drc->drc_resumable = resumable;
1759a2afb611SJerry Jelinek 	drc->drc_cred = CRED();
1760efb80947Sahrens 
17619c3fd121SMatthew Ahrens 	if (drc->drc_drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
17623b2aab18SMatthew Ahrens 		drc->drc_byteswap = B_TRUE;
1763770499e1SDan Kimmel 		(void) fletcher_4_incremental_byteswap(drr_begin,
17643b2aab18SMatthew Ahrens 		    sizeof (dmu_replay_record_t), &drc->drc_cksum);
17659c3fd121SMatthew Ahrens 		byteswap_record(drr_begin);
17669c3fd121SMatthew Ahrens 	} else if (drc->drc_drrb->drr_magic == DMU_BACKUP_MAGIC) {
1767770499e1SDan Kimmel 		(void) fletcher_4_incremental_native(drr_begin,
17683b2aab18SMatthew Ahrens 		    sizeof (dmu_replay_record_t), &drc->drc_cksum);
17699c3fd121SMatthew Ahrens 	} else {
17709c3fd121SMatthew Ahrens 		return (SET_ERROR(EINVAL));
17713cb34c60Sahrens 	}
17723cb34c60Sahrens 
17733b2aab18SMatthew Ahrens 	drba.drba_origin = origin;
17743b2aab18SMatthew Ahrens 	drba.drba_cookie = drc;
17753b2aab18SMatthew Ahrens 	drba.drba_cred = CRED();
17763b2aab18SMatthew Ahrens 
17779c3fd121SMatthew Ahrens 	if (DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo) &
17789c3fd121SMatthew Ahrens 	    DMU_BACKUP_FEATURE_RESUMING) {
17799c3fd121SMatthew Ahrens 		return (dsl_sync_task(tofs,
17809c3fd121SMatthew Ahrens 		    dmu_recv_resume_begin_check, dmu_recv_resume_begin_sync,
17819c3fd121SMatthew Ahrens 		    &drba, 5, ZFS_SPACE_CHECK_NORMAL));
17829c3fd121SMatthew Ahrens 	} else  {
17839c3fd121SMatthew Ahrens 		return (dsl_sync_task(tofs,
17849c3fd121SMatthew Ahrens 		    dmu_recv_begin_check, dmu_recv_begin_sync,
17859c3fd121SMatthew Ahrens 		    &drba, 5, ZFS_SPACE_CHECK_NORMAL));
17869c3fd121SMatthew Ahrens 	}
1787efb80947Sahrens }
1788efb80947Sahrens 
1789a2cdcdd2SPaul Dagnelie struct receive_record_arg {
1790a2cdcdd2SPaul Dagnelie 	dmu_replay_record_t header;
1791a2cdcdd2SPaul Dagnelie 	void *payload; /* Pointer to a buffer containing the payload */
1792a2cdcdd2SPaul Dagnelie 	/*
1793a2cdcdd2SPaul Dagnelie 	 * If the record is a write, pointer to the arc_buf_t containing the
1794a2cdcdd2SPaul Dagnelie 	 * payload.
1795a2cdcdd2SPaul Dagnelie 	 */
1796a2cdcdd2SPaul Dagnelie 	arc_buf_t *write_buf;
1797a2cdcdd2SPaul Dagnelie 	int payload_size;
17989c3fd121SMatthew Ahrens 	uint64_t bytes_read; /* bytes read from stream when record created */
1799a2cdcdd2SPaul Dagnelie 	boolean_t eos_marker; /* Marks the end of the stream */
1800a2cdcdd2SPaul Dagnelie 	bqueue_node_t node;
1801a2cdcdd2SPaul Dagnelie };
1802a2cdcdd2SPaul Dagnelie 
1803a2cdcdd2SPaul Dagnelie struct receive_writer_arg {
180498110f08SMatthew Ahrens 	objset_t *os;
18053b2aab18SMatthew Ahrens 	boolean_t byteswap;
1806a2cdcdd2SPaul Dagnelie 	bqueue_t q;
18079c3fd121SMatthew Ahrens 
1808a2cdcdd2SPaul Dagnelie 	/*
1809a2cdcdd2SPaul Dagnelie 	 * These three args are used to signal to the main thread that we're
1810a2cdcdd2SPaul Dagnelie 	 * done.
1811a2cdcdd2SPaul Dagnelie 	 */
1812a2cdcdd2SPaul Dagnelie 	kmutex_t mutex;
1813a2cdcdd2SPaul Dagnelie 	kcondvar_t cv;
1814a2cdcdd2SPaul Dagnelie 	boolean_t done;
18159c3fd121SMatthew Ahrens 
1816a2cdcdd2SPaul Dagnelie 	int err;
1817a2cdcdd2SPaul Dagnelie 	/* A map from guid to dataset to help handle dedup'd streams. */
1818a2cdcdd2SPaul Dagnelie 	avl_tree_t *guid_to_ds_map;
18199c3fd121SMatthew Ahrens 	boolean_t resumable;
18209c3fd121SMatthew Ahrens 	uint64_t last_object, last_offset;
18219c3fd121SMatthew Ahrens 	uint64_t bytes_read; /* bytes read when current record created */
1822a2cdcdd2SPaul Dagnelie };
182398110f08SMatthew Ahrens 
182468ecb2ecSPaul Dagnelie struct objlist {
182568ecb2ecSPaul Dagnelie 	list_t list; /* List of struct receive_objnode. */
182668ecb2ecSPaul Dagnelie 	/*
182768ecb2ecSPaul Dagnelie 	 * Last object looked up. Used to assert that objects are being looked
182868ecb2ecSPaul Dagnelie 	 * up in ascending order.
182968ecb2ecSPaul Dagnelie 	 */
183068ecb2ecSPaul Dagnelie 	uint64_t last_lookup;
183168ecb2ecSPaul Dagnelie };
183268ecb2ecSPaul Dagnelie 
183368ecb2ecSPaul Dagnelie struct receive_objnode {
183468ecb2ecSPaul Dagnelie 	list_node_t node;
183568ecb2ecSPaul Dagnelie 	uint64_t object;
183668ecb2ecSPaul Dagnelie };
183768ecb2ecSPaul Dagnelie 
18385602294fSDan Kimmel struct receive_arg {
1839a2cdcdd2SPaul Dagnelie 	objset_t *os;
1840a2cdcdd2SPaul Dagnelie 	vnode_t *vp; /* The vnode to read the stream from */
1841a2cdcdd2SPaul Dagnelie 	uint64_t voff; /* The current offset in the stream */
18429c3fd121SMatthew Ahrens 	uint64_t bytes_read;
1843a2cdcdd2SPaul Dagnelie 	/*
1844a2cdcdd2SPaul Dagnelie 	 * A record that has had its payload read in, but hasn't yet been handed
1845a2cdcdd2SPaul Dagnelie 	 * off to the worker thread.
1846a2cdcdd2SPaul Dagnelie 	 */
1847a2cdcdd2SPaul Dagnelie 	struct receive_record_arg *rrd;
1848a2cdcdd2SPaul Dagnelie 	/* A record that has had its header read in, but not its payload. */
1849a2cdcdd2SPaul Dagnelie 	struct receive_record_arg *next_rrd;
18503cb34c60Sahrens 	zio_cksum_t cksum;
185198110f08SMatthew Ahrens 	zio_cksum_t prev_cksum;
1852a2cdcdd2SPaul Dagnelie 	int err;
1853a2cdcdd2SPaul Dagnelie 	boolean_t byteswap;
1854a2cdcdd2SPaul Dagnelie 	/* Sorted list of objects not to issue prefetches for. */
185568ecb2ecSPaul Dagnelie 	struct objlist ignore_objlist;
18563cb34c60Sahrens };
18573cb34c60Sahrens 
18589e69d7d0SLori Alt typedef struct guid_map_entry {
18599e69d7d0SLori Alt 	uint64_t	guid;
18609e69d7d0SLori Alt 	dsl_dataset_t	*gme_ds;
18619e69d7d0SLori Alt 	avl_node_t	avlnode;
18629e69d7d0SLori Alt } guid_map_entry_t;
18639e69d7d0SLori Alt 
18649e69d7d0SLori Alt static int
18659e69d7d0SLori Alt guid_compare(const void *arg1, const void *arg2)
18669e69d7d0SLori Alt {
18679e69d7d0SLori Alt 	const guid_map_entry_t *gmep1 = arg1;
18689e69d7d0SLori Alt 	const guid_map_entry_t *gmep2 = arg2;
18699e69d7d0SLori Alt 
18709e69d7d0SLori Alt 	if (gmep1->guid < gmep2->guid)
18719e69d7d0SLori Alt 		return (-1);
18729e69d7d0SLori Alt 	else if (gmep1->guid > gmep2->guid)
18739e69d7d0SLori Alt 		return (1);
18749e69d7d0SLori Alt 	return (0);
18759e69d7d0SLori Alt }
18769e69d7d0SLori Alt 
1877c99e4bdcSChris Kirby static void
1878c99e4bdcSChris Kirby free_guid_map_onexit(void *arg)
1879c99e4bdcSChris Kirby {
1880c99e4bdcSChris Kirby 	avl_tree_t *ca = arg;
1881c99e4bdcSChris Kirby 	void *cookie = NULL;
1882c99e4bdcSChris Kirby 	guid_map_entry_t *gmep;
1883c99e4bdcSChris Kirby 
1884c99e4bdcSChris Kirby 	while ((gmep = avl_destroy_nodes(ca, &cookie)) != NULL) {
18853b2aab18SMatthew Ahrens 		dsl_dataset_long_rele(gmep->gme_ds, gmep);
1886de8d9cffSMatthew Ahrens 		dsl_dataset_rele(gmep->gme_ds, gmep);
1887c99e4bdcSChris Kirby 		kmem_free(gmep, sizeof (guid_map_entry_t));
1888c99e4bdcSChris Kirby 	}
1889c99e4bdcSChris Kirby 	avl_destroy(ca);
1890c99e4bdcSChris Kirby 	kmem_free(ca, sizeof (avl_tree_t));
1891c99e4bdcSChris Kirby }
1892c99e4bdcSChris Kirby 
189398110f08SMatthew Ahrens static int
1894a2cdcdd2SPaul Dagnelie receive_read(struct receive_arg *ra, int len, void *buf)
1895efb80947Sahrens {
18963cb34c60Sahrens 	int done = 0;
1897efb80947Sahrens 
1898c20404ffSEli Rosenthal 	/*
1899c20404ffSEli Rosenthal 	 * The code doesn't rely on this (lengths being multiples of 8).  See
1900c20404ffSEli Rosenthal 	 * comment in dump_bytes.
1901c20404ffSEli Rosenthal 	 */
1902fb09f5aaSMadhav Suresh 	ASSERT0(len % 8);
1903efb80947Sahrens 
19043cb34c60Sahrens 	while (done < len) {
1905efb80947Sahrens 		ssize_t resid;
1906efb80947Sahrens 
1907efb80947Sahrens 		ra->err = vn_rdwr(UIO_READ, ra->vp,
190898110f08SMatthew Ahrens 		    (char *)buf + done, len - done,
1909efb80947Sahrens 		    ra->voff, UIO_SYSSPACE, FAPPEND,
1910efb80947Sahrens 		    RLIM64_INFINITY, CRED(), &resid);
1911efb80947Sahrens 
19129c3fd121SMatthew Ahrens 		if (resid == len - done) {
19139c3fd121SMatthew Ahrens 			/*
19149c3fd121SMatthew Ahrens 			 * Note: ECKSUM indicates that the receive
19159c3fd121SMatthew Ahrens 			 * was interrupted and can potentially be resumed.
19169c3fd121SMatthew Ahrens 			 */
19179c3fd121SMatthew Ahrens 			ra->err = SET_ERROR(ECKSUM);
19189c3fd121SMatthew Ahrens 		}
19193cb34c60Sahrens 		ra->voff += len - done - resid;
19203cb34c60Sahrens 		done = len - resid;
19213b2aab18SMatthew Ahrens 		if (ra->err != 0)
192298110f08SMatthew Ahrens 			return (ra->err);
1923efb80947Sahrens 	}
1924efb80947Sahrens 
19259c3fd121SMatthew Ahrens 	ra->bytes_read += len;
19269c3fd121SMatthew Ahrens 
19273cb34c60Sahrens 	ASSERT3U(done, ==, len);
192898110f08SMatthew Ahrens 	return (0);
1929efb80947Sahrens }
1930efb80947Sahrens 
1931efb80947Sahrens static void
193298110f08SMatthew Ahrens byteswap_record(dmu_replay_record_t *drr)
1933efb80947Sahrens {
1934efb80947Sahrens #define	DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
1935efb80947Sahrens #define	DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
1936efb80947Sahrens 	drr->drr_type = BSWAP_32(drr->drr_type);
19373cb34c60Sahrens 	drr->drr_payloadlen = BSWAP_32(drr->drr_payloadlen);
193898110f08SMatthew Ahrens 
1939efb80947Sahrens 	switch (drr->drr_type) {
1940efb80947Sahrens 	case DRR_BEGIN:
1941efb80947Sahrens 		DO64(drr_begin.drr_magic);
19429e69d7d0SLori Alt 		DO64(drr_begin.drr_versioninfo);
1943efb80947Sahrens 		DO64(drr_begin.drr_creation_time);
1944efb80947Sahrens 		DO32(drr_begin.drr_type);
19453cb34c60Sahrens 		DO32(drr_begin.drr_flags);
1946efb80947Sahrens 		DO64(drr_begin.drr_toguid);
1947efb80947Sahrens 		DO64(drr_begin.drr_fromguid);
1948efb80947Sahrens 		break;
1949efb80947Sahrens 	case DRR_OBJECT:
1950efb80947Sahrens 		DO64(drr_object.drr_object);
1951efb80947Sahrens 		DO32(drr_object.drr_type);
1952efb80947Sahrens 		DO32(drr_object.drr_bonustype);
1953efb80947Sahrens 		DO32(drr_object.drr_blksz);
1954efb80947Sahrens 		DO32(drr_object.drr_bonuslen);
19559e69d7d0SLori Alt 		DO64(drr_object.drr_toguid);
1956efb80947Sahrens 		break;
1957efb80947Sahrens 	case DRR_FREEOBJECTS:
1958efb80947Sahrens 		DO64(drr_freeobjects.drr_firstobj);
1959efb80947Sahrens 		DO64(drr_freeobjects.drr_numobjs);
19609e69d7d0SLori Alt 		DO64(drr_freeobjects.drr_toguid);
1961efb80947Sahrens 		break;
1962efb80947Sahrens 	case DRR_WRITE:
1963efb80947Sahrens 		DO64(drr_write.drr_object);
1964efb80947Sahrens 		DO32(drr_write.drr_type);
1965efb80947Sahrens 		DO64(drr_write.drr_offset);
19665602294fSDan Kimmel 		DO64(drr_write.drr_logical_size);
19679e69d7d0SLori Alt 		DO64(drr_write.drr_toguid);
196898110f08SMatthew Ahrens 		ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_write.drr_key.ddk_cksum);
19698e714474SLori Alt 		DO64(drr_write.drr_key.ddk_prop);
19705602294fSDan Kimmel 		DO64(drr_write.drr_compressed_size);
19719e69d7d0SLori Alt 		break;
19729e69d7d0SLori Alt 	case DRR_WRITE_BYREF:
19739e69d7d0SLori Alt 		DO64(drr_write_byref.drr_object);
19749e69d7d0SLori Alt 		DO64(drr_write_byref.drr_offset);
19759e69d7d0SLori Alt 		DO64(drr_write_byref.drr_length);
19769e69d7d0SLori Alt 		DO64(drr_write_byref.drr_toguid);
19779e69d7d0SLori Alt 		DO64(drr_write_byref.drr_refguid);
19789e69d7d0SLori Alt 		DO64(drr_write_byref.drr_refobject);
19799e69d7d0SLori Alt 		DO64(drr_write_byref.drr_refoffset);
198098110f08SMatthew Ahrens 		ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_write_byref.
198198110f08SMatthew Ahrens 		    drr_key.ddk_cksum);
19828e714474SLori Alt 		DO64(drr_write_byref.drr_key.ddk_prop);
1983efb80947Sahrens 		break;
19845d7b4d43SMatthew Ahrens 	case DRR_WRITE_EMBEDDED:
19855d7b4d43SMatthew Ahrens 		DO64(drr_write_embedded.drr_object);
19865d7b4d43SMatthew Ahrens 		DO64(drr_write_embedded.drr_offset);
19875d7b4d43SMatthew Ahrens 		DO64(drr_write_embedded.drr_length);
19885d7b4d43SMatthew Ahrens 		DO64(drr_write_embedded.drr_toguid);
19895d7b4d43SMatthew Ahrens 		DO32(drr_write_embedded.drr_lsize);
19905d7b4d43SMatthew Ahrens 		DO32(drr_write_embedded.drr_psize);
19915d7b4d43SMatthew Ahrens 		break;
1992efb80947Sahrens 	case DRR_FREE:
1993efb80947Sahrens 		DO64(drr_free.drr_object);
1994efb80947Sahrens 		DO64(drr_free.drr_offset);
1995efb80947Sahrens 		DO64(drr_free.drr_length);
19969e69d7d0SLori Alt 		DO64(drr_free.drr_toguid);
1997efb80947Sahrens 		break;
19980a586ceaSMark Shellenbaum 	case DRR_SPILL:
19990a586ceaSMark Shellenbaum 		DO64(drr_spill.drr_object);
20000a586ceaSMark Shellenbaum 		DO64(drr_spill.drr_length);
20010a586ceaSMark Shellenbaum 		DO64(drr_spill.drr_toguid);
20020a586ceaSMark Shellenbaum 		break;
2003efb80947Sahrens 	case DRR_END:
20049e69d7d0SLori Alt 		DO64(drr_end.drr_toguid);
200598110f08SMatthew Ahrens 		ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_end.drr_checksum);
2006efb80947Sahrens 		break;
2007efb80947Sahrens 	}
200898110f08SMatthew Ahrens 
200998110f08SMatthew Ahrens 	if (drr->drr_type != DRR_BEGIN) {
201098110f08SMatthew Ahrens 		ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_checksum.drr_checksum);
201198110f08SMatthew Ahrens 	}
201298110f08SMatthew Ahrens 
2013efb80947Sahrens #undef DO64
2014efb80947Sahrens #undef DO32
2015efb80947Sahrens }
2016efb80947Sahrens 
2017e77d42eaSMatthew Ahrens static inline uint8_t
2018e77d42eaSMatthew Ahrens deduce_nblkptr(dmu_object_type_t bonus_type, uint64_t bonus_size)
2019e77d42eaSMatthew Ahrens {
2020e77d42eaSMatthew Ahrens 	if (bonus_type == DMU_OT_SA) {
2021e77d42eaSMatthew Ahrens 		return (1);
2022e77d42eaSMatthew Ahrens 	} else {
2023e77d42eaSMatthew Ahrens 		return (1 +
2024e77d42eaSMatthew Ahrens 		    ((DN_MAX_BONUSLEN - bonus_size) >> SPA_BLKPTRSHIFT));
2025e77d42eaSMatthew Ahrens 	}
2026e77d42eaSMatthew Ahrens }
2027e77d42eaSMatthew Ahrens 
20289c3fd121SMatthew Ahrens static void
20299c3fd121SMatthew Ahrens save_resume_state(struct receive_writer_arg *rwa,
20309c3fd121SMatthew Ahrens     uint64_t object, uint64_t offset, dmu_tx_t *tx)
20319c3fd121SMatthew Ahrens {
20329c3fd121SMatthew Ahrens 	int txgoff = dmu_tx_get_txg(tx) & TXG_MASK;
20339c3fd121SMatthew Ahrens 
20349c3fd121SMatthew Ahrens 	if (!rwa->resumable)
20359c3fd121SMatthew Ahrens 		return;
20369c3fd121SMatthew Ahrens 
20379c3fd121SMatthew Ahrens 	/*
20389c3fd121SMatthew Ahrens 	 * We use ds_resume_bytes[] != 0 to indicate that we need to
20399c3fd121SMatthew Ahrens 	 * update this on disk, so it must not be 0.
20409c3fd121SMatthew Ahrens 	 */
20419c3fd121SMatthew Ahrens 	ASSERT(rwa->bytes_read != 0);
20429c3fd121SMatthew Ahrens 
20439c3fd121SMatthew Ahrens 	/*
20449c3fd121SMatthew Ahrens 	 * We only resume from write records, which have a valid
20459c3fd121SMatthew Ahrens 	 * (non-meta-dnode) object number.
20469c3fd121SMatthew Ahrens 	 */
20479c3fd121SMatthew Ahrens 	ASSERT(object != 0);
20489c3fd121SMatthew Ahrens 
20499c3fd121SMatthew Ahrens 	/*
20509c3fd121SMatthew Ahrens 	 * For resuming to work correctly, we must receive records in order,
20519c3fd121SMatthew Ahrens 	 * sorted by object,offset.  This is checked by the callers, but
20529c3fd121SMatthew Ahrens 	 * assert it here for good measure.
20539c3fd121SMatthew Ahrens 	 */
20549c3fd121SMatthew Ahrens 	ASSERT3U(object, >=, rwa->os->os_dsl_dataset->ds_resume_object[txgoff]);
20559c3fd121SMatthew Ahrens 	ASSERT(object != rwa->os->os_dsl_dataset->ds_resume_object[txgoff] ||
20569c3fd121SMatthew Ahrens 	    offset >= rwa->os->os_dsl_dataset->ds_resume_offset[txgoff]);
20579c3fd121SMatthew Ahrens 	ASSERT3U(rwa->bytes_read, >=,
20589c3fd121SMatthew Ahrens 	    rwa->os->os_dsl_dataset->ds_resume_bytes[txgoff]);
20599c3fd121SMatthew Ahrens 
20609c3fd121SMatthew Ahrens 	rwa->os->os_dsl_dataset->ds_resume_object[txgoff] = object;
20619c3fd121SMatthew Ahrens 	rwa->os->os_dsl_dataset->ds_resume_offset[txgoff] = offset;
20629c3fd121SMatthew Ahrens 	rwa->os->os_dsl_dataset->ds_resume_bytes[txgoff] = rwa->bytes_read;
20639c3fd121SMatthew Ahrens }
20649c3fd121SMatthew Ahrens 
2065efb80947Sahrens static int
2066a2cdcdd2SPaul Dagnelie receive_object(struct receive_writer_arg *rwa, struct drr_object *drro,
2067a2cdcdd2SPaul Dagnelie     void *data)
2068efb80947Sahrens {
2069e77d42eaSMatthew Ahrens 	dmu_object_info_t doi;
2070efb80947Sahrens 	dmu_tx_t *tx;
2071e77d42eaSMatthew Ahrens 	uint64_t object;
2072e77d42eaSMatthew Ahrens 	int err;
2073efb80947Sahrens 
2074efb80947Sahrens 	if (drro->drr_type == DMU_OT_NONE ||
2075ad135b5dSChristopher Siden 	    !DMU_OT_IS_VALID(drro->drr_type) ||
2076ad135b5dSChristopher Siden 	    !DMU_OT_IS_VALID(drro->drr_bonustype) ||
20779e69d7d0SLori Alt 	    drro->drr_checksumtype >= ZIO_CHECKSUM_FUNCTIONS ||
2078efb80947Sahrens 	    drro->drr_compress >= ZIO_COMPRESS_FUNCTIONS ||
2079efb80947Sahrens 	    P2PHASE(drro->drr_blksz, SPA_MINBLOCKSIZE) ||
2080efb80947Sahrens 	    drro->drr_blksz < SPA_MINBLOCKSIZE ||
2081a2cdcdd2SPaul Dagnelie 	    drro->drr_blksz > spa_maxblocksize(dmu_objset_spa(rwa->os)) ||
2082efb80947Sahrens 	    drro->drr_bonuslen > DN_MAX_BONUSLEN) {
2083be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2084efb80947Sahrens 	}
2085efb80947Sahrens 
2086a2cdcdd2SPaul Dagnelie 	err = dmu_object_info(rwa->os, drro->drr_object, &doi);
20872bf405a2SMark Maybee 
20882bf405a2SMark Maybee 	if (err != 0 && err != ENOENT)
2089be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2090e77d42eaSMatthew Ahrens 	object = err == 0 ? drro->drr_object : DMU_NEW_OBJECT;
20912bf405a2SMark Maybee 
2092e77d42eaSMatthew Ahrens 	/*
2093e77d42eaSMatthew Ahrens 	 * If we are losing blkptrs or changing the block size this must
2094e77d42eaSMatthew Ahrens 	 * be a new file instance.  We must clear out the previous file
2095e77d42eaSMatthew Ahrens 	 * contents before we can change this type of metadata in the dnode.
2096e77d42eaSMatthew Ahrens 	 */
2097e77d42eaSMatthew Ahrens 	if (err == 0) {
2098e77d42eaSMatthew Ahrens 		int nblkptr;
2099e77d42eaSMatthew Ahrens 
2100e77d42eaSMatthew Ahrens 		nblkptr = deduce_nblkptr(drro->drr_bonustype,
2101e77d42eaSMatthew Ahrens 		    drro->drr_bonuslen);
2102e77d42eaSMatthew Ahrens 
2103e77d42eaSMatthew Ahrens 		if (drro->drr_blksz != doi.doi_data_block_size ||
2104e77d42eaSMatthew Ahrens 		    nblkptr < doi.doi_nblkptr) {
2105a2cdcdd2SPaul Dagnelie 			err = dmu_free_long_range(rwa->os, drro->drr_object,
2106e77d42eaSMatthew Ahrens 			    0, DMU_OBJECT_END);
2107e77d42eaSMatthew Ahrens 			if (err != 0)
2108e77d42eaSMatthew Ahrens 				return (SET_ERROR(EINVAL));
2109efb80947Sahrens 		}
2110e77d42eaSMatthew Ahrens 	}
2111e77d42eaSMatthew Ahrens 
2112a2cdcdd2SPaul Dagnelie 	tx = dmu_tx_create(rwa->os);
2113e77d42eaSMatthew Ahrens 	dmu_tx_hold_bonus(tx, object);
2114e77d42eaSMatthew Ahrens 	err = dmu_tx_assign(tx, TXG_WAIT);
2115e77d42eaSMatthew Ahrens 	if (err != 0) {
2116e77d42eaSMatthew Ahrens 		dmu_tx_abort(tx);
2117e77d42eaSMatthew Ahrens 		return (err);
2118e77d42eaSMatthew Ahrens 	}
2119e77d42eaSMatthew Ahrens 
2120e77d42eaSMatthew Ahrens 	if (object == DMU_NEW_OBJECT) {
2121e77d42eaSMatthew Ahrens 		/* currently free, want to be allocated */
2122a2cdcdd2SPaul Dagnelie 		err = dmu_object_claim(rwa->os, drro->drr_object,
2123efb80947Sahrens 		    drro->drr_type, drro->drr_blksz,
2124efb80947Sahrens 		    drro->drr_bonustype, drro->drr_bonuslen, tx);
2125e77d42eaSMatthew Ahrens 	} else if (drro->drr_type != doi.doi_type ||
2126e77d42eaSMatthew Ahrens 	    drro->drr_blksz != doi.doi_data_block_size ||
2127e77d42eaSMatthew Ahrens 	    drro->drr_bonustype != doi.doi_bonus_type ||
2128e77d42eaSMatthew Ahrens 	    drro->drr_bonuslen != doi.doi_bonus_size) {
2129e77d42eaSMatthew Ahrens 		/* currently allocated, but with different properties */
2130a2cdcdd2SPaul Dagnelie 		err = dmu_object_reclaim(rwa->os, drro->drr_object,
2131efb80947Sahrens 		    drro->drr_type, drro->drr_blksz,
2132e77d42eaSMatthew Ahrens 		    drro->drr_bonustype, drro->drr_bonuslen, tx);
2133efb80947Sahrens 	}
21343b2aab18SMatthew Ahrens 	if (err != 0) {
2135e77d42eaSMatthew Ahrens 		dmu_tx_commit(tx);
2136be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
21370a586ceaSMark Shellenbaum 	}
21382bf405a2SMark Maybee 
2139a2cdcdd2SPaul Dagnelie 	dmu_object_set_checksum(rwa->os, drro->drr_object,
214098110f08SMatthew Ahrens 	    drro->drr_checksumtype, tx);
2141a2cdcdd2SPaul Dagnelie 	dmu_object_set_compress(rwa->os, drro->drr_object,
214298110f08SMatthew Ahrens 	    drro->drr_compress, tx);
2143efb80947Sahrens 
2144adee0b6fSTim Haley 	if (data != NULL) {
2145efb80947Sahrens 		dmu_buf_t *db;
2146adee0b6fSTim Haley 
2147a2cdcdd2SPaul Dagnelie 		VERIFY0(dmu_bonus_hold(rwa->os, drro->drr_object, FTAG, &db));
2148efb80947Sahrens 		dmu_buf_will_dirty(db, tx);
2149efb80947Sahrens 
21501934e92fSmaybee 		ASSERT3U(db->db_size, >=, drro->drr_bonuslen);
21511934e92fSmaybee 		bcopy(data, db->db_data, drro->drr_bonuslen);
2152a2cdcdd2SPaul Dagnelie 		if (rwa->byteswap) {
2153ad135b5dSChristopher Siden 			dmu_object_byteswap_t byteswap =
2154ad135b5dSChristopher Siden 			    DMU_OT_BYTESWAP(drro->drr_bonustype);
2155ad135b5dSChristopher Siden 			dmu_ot_byteswap[byteswap].ob_func(db->db_data,
2156efb80947Sahrens 			    drro->drr_bonuslen);
2157efb80947Sahrens 		}
2158efb80947Sahrens 		dmu_buf_rele(db, FTAG);
2159efb80947Sahrens 	}
2160efb80947Sahrens 	dmu_tx_commit(tx);
21619c3fd121SMatthew Ahrens 
2162efb80947Sahrens 	return (0);
2163efb80947Sahrens }
2164efb80947Sahrens 
2165efb80947Sahrens /* ARGSUSED */
2166efb80947Sahrens static int
2167a2cdcdd2SPaul Dagnelie receive_freeobjects(struct receive_writer_arg *rwa,
2168efb80947Sahrens     struct drr_freeobjects *drrfo)
2169efb80947Sahrens {
2170efb80947Sahrens 	uint64_t obj;
217168ecb2ecSPaul Dagnelie 	int next_err = 0;
2172efb80947Sahrens 
2173efb80947Sahrens 	if (drrfo->drr_firstobj + drrfo->drr_numobjs < drrfo->drr_firstobj)
2174be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2175efb80947Sahrens 
2176efb80947Sahrens 	for (obj = drrfo->drr_firstobj;
217768ecb2ecSPaul Dagnelie 	    obj < drrfo->drr_firstobj + drrfo->drr_numobjs && next_err == 0;
217868ecb2ecSPaul Dagnelie 	    next_err = dmu_object_next(rwa->os, &obj, FALSE, 0)) {
2179efb80947Sahrens 		int err;
2180efb80947Sahrens 
2181a2cdcdd2SPaul Dagnelie 		if (dmu_object_info(rwa->os, obj, NULL) != 0)
2182efb80947Sahrens 			continue;
2183efb80947Sahrens 
2184a2cdcdd2SPaul Dagnelie 		err = dmu_free_long_object(rwa->os, obj);
21853b2aab18SMatthew Ahrens 		if (err != 0)
2186efb80947Sahrens 			return (err);
2187efb80947Sahrens 	}
218868ecb2ecSPaul Dagnelie 	if (next_err != ESRCH)
218968ecb2ecSPaul Dagnelie 		return (next_err);
2190efb80947Sahrens 	return (0);
2191efb80947Sahrens }
2192efb80947Sahrens 
2193efb80947Sahrens static int
2194a2cdcdd2SPaul Dagnelie receive_write(struct receive_writer_arg *rwa, struct drr_write *drrw,
2195a2cdcdd2SPaul Dagnelie     arc_buf_t *abuf)
2196efb80947Sahrens {
2197efb80947Sahrens 	dmu_tx_t *tx;
2198efb80947Sahrens 	int err;
2199efb80947Sahrens 
22005602294fSDan Kimmel 	if (drrw->drr_offset + drrw->drr_logical_size < drrw->drr_offset ||
2201ad135b5dSChristopher Siden 	    !DMU_OT_IS_VALID(drrw->drr_type))
2202be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2203efb80947Sahrens 
22049c3fd121SMatthew Ahrens 	/*
22059c3fd121SMatthew Ahrens 	 * For resuming to work, records must be in increasing order
22069c3fd121SMatthew Ahrens 	 * by (object, offset).
22079c3fd121SMatthew Ahrens 	 */
22089c3fd121SMatthew Ahrens 	if (drrw->drr_object < rwa->last_object ||
22099c3fd121SMatthew Ahrens 	    (drrw->drr_object == rwa->last_object &&
22109c3fd121SMatthew Ahrens 	    drrw->drr_offset < rwa->last_offset)) {
22119c3fd121SMatthew Ahrens 		return (SET_ERROR(EINVAL));
22129c3fd121SMatthew Ahrens 	}
22139c3fd121SMatthew Ahrens 	rwa->last_object = drrw->drr_object;
22149c3fd121SMatthew Ahrens 	rwa->last_offset = drrw->drr_offset;
22159c3fd121SMatthew Ahrens 
2216a2cdcdd2SPaul Dagnelie 	if (dmu_object_info(rwa->os, drrw->drr_object, NULL) != 0)
22178a904709SMatthew Ahrens 		return (SET_ERROR(EINVAL));
22188a904709SMatthew Ahrens 
2219a2cdcdd2SPaul Dagnelie 	tx = dmu_tx_create(rwa->os);
2220efb80947Sahrens 
2221efb80947Sahrens 	dmu_tx_hold_write(tx, drrw->drr_object,
22225602294fSDan Kimmel 	    drrw->drr_offset, drrw->drr_logical_size);
2223efb80947Sahrens 	err = dmu_tx_assign(tx, TXG_WAIT);
22243b2aab18SMatthew Ahrens 	if (err != 0) {
2225efb80947Sahrens 		dmu_tx_abort(tx);
2226efb80947Sahrens 		return (err);
2227efb80947Sahrens 	}
2228a2cdcdd2SPaul Dagnelie 	if (rwa->byteswap) {
2229ad135b5dSChristopher Siden 		dmu_object_byteswap_t byteswap =
2230ad135b5dSChristopher Siden 		    DMU_OT_BYTESWAP(drrw->drr_type);
223198110f08SMatthew Ahrens 		dmu_ot_byteswap[byteswap].ob_func(abuf->b_data,
22325602294fSDan Kimmel 		    DRR_WRITE_PAYLOAD_SIZE(drrw));
2233ad135b5dSChristopher Siden 	}
223498110f08SMatthew Ahrens 
22355602294fSDan Kimmel 	/* use the bonus buf to look up the dnode in dmu_assign_arcbuf */
223698110f08SMatthew Ahrens 	dmu_buf_t *bonus;
2237a2cdcdd2SPaul Dagnelie 	if (dmu_bonus_hold(rwa->os, drrw->drr_object, FTAG, &bonus) != 0)
223898110f08SMatthew Ahrens 		return (SET_ERROR(EINVAL));
22398a904709SMatthew Ahrens 	dmu_assign_arcbuf(bonus, drrw->drr_offset, abuf, tx);
22409c3fd121SMatthew Ahrens 
22419c3fd121SMatthew Ahrens 	/*
22429c3fd121SMatthew Ahrens 	 * Note: If the receive fails, we want the resume stream to start
22439c3fd121SMatthew Ahrens 	 * with the same record that we last successfully received (as opposed
22449c3fd121SMatthew Ahrens 	 * to the next record), so that we can verify that we are
22459c3fd121SMatthew Ahrens 	 * resuming from the correct location.
22469c3fd121SMatthew Ahrens 	 */
22479c3fd121SMatthew Ahrens 	save_resume_state(rwa, drrw->drr_object, drrw->drr_offset, tx);
2248efb80947Sahrens 	dmu_tx_commit(tx);
22498a904709SMatthew Ahrens 	dmu_buf_rele(bonus, FTAG);
22509c3fd121SMatthew Ahrens 
2251efb80947Sahrens 	return (0);
2252efb80947Sahrens }
2253efb80947Sahrens 
22549e69d7d0SLori Alt /*
22559e69d7d0SLori Alt  * Handle a DRR_WRITE_BYREF record.  This record is used in dedup'ed
22569e69d7d0SLori Alt  * streams to refer to a copy of the data that is already on the
22579e69d7d0SLori Alt  * system because it came in earlier in the stream.  This function
22589e69d7d0SLori Alt  * finds the earlier copy of the data, and uses that copy instead of
22599e69d7d0SLori Alt  * data from the stream to fulfill this write.
22609e69d7d0SLori Alt  */
22619e69d7d0SLori Alt static int
2262a2cdcdd2SPaul Dagnelie receive_write_byref(struct receive_writer_arg *rwa,
2263a2cdcdd2SPaul Dagnelie     struct drr_write_byref *drrwbr)
22649e69d7d0SLori Alt {
22659e69d7d0SLori Alt 	dmu_tx_t *tx;
22669e69d7d0SLori Alt 	int err;
22679e69d7d0SLori Alt 	guid_map_entry_t gmesrch;
22689e69d7d0SLori Alt 	guid_map_entry_t *gmep;
22695d7b4d43SMatthew Ahrens 	avl_index_t where;
22709e69d7d0SLori Alt 	objset_t *ref_os = NULL;
22719e69d7d0SLori Alt 	dmu_buf_t *dbp;
22729e69d7d0SLori Alt 
22739e69d7d0SLori Alt 	if (drrwbr->drr_offset + drrwbr->drr_length < drrwbr->drr_offset)
2274be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
22759e69d7d0SLori Alt 
22769e69d7d0SLori Alt 	/*
22779e69d7d0SLori Alt 	 * If the GUID of the referenced dataset is different from the
22789e69d7d0SLori Alt 	 * GUID of the target dataset, find the referenced dataset.
22799e69d7d0SLori Alt 	 */
22809e69d7d0SLori Alt 	if (drrwbr->drr_toguid != drrwbr->drr_refguid) {
22819e69d7d0SLori Alt 		gmesrch.guid = drrwbr->drr_refguid;
2282a2cdcdd2SPaul Dagnelie 		if ((gmep = avl_find(rwa->guid_to_ds_map, &gmesrch,
22839e69d7d0SLori Alt 		    &where)) == NULL) {
2284be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
22859e69d7d0SLori Alt 		}
22869e69d7d0SLori Alt 		if (dmu_objset_from_ds(gmep->gme_ds, &ref_os))
2287be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
22889e69d7d0SLori Alt 	} else {
2289a2cdcdd2SPaul Dagnelie 		ref_os = rwa->os;
22909e69d7d0SLori Alt 	}
22919e69d7d0SLori Alt 
22925d7b4d43SMatthew Ahrens 	err = dmu_buf_hold(ref_os, drrwbr->drr_refobject,
22935d7b4d43SMatthew Ahrens 	    drrwbr->drr_refoffset, FTAG, &dbp, DMU_READ_PREFETCH);
22945d7b4d43SMatthew Ahrens 	if (err != 0)
22959e69d7d0SLori Alt 		return (err);
22969e69d7d0SLori Alt 
2297a2cdcdd2SPaul Dagnelie 	tx = dmu_tx_create(rwa->os);
22989e69d7d0SLori Alt 
22999e69d7d0SLori Alt 	dmu_tx_hold_write(tx, drrwbr->drr_object,
23009e69d7d0SLori Alt 	    drrwbr->drr_offset, drrwbr->drr_length);
23019e69d7d0SLori Alt 	err = dmu_tx_assign(tx, TXG_WAIT);
23023b2aab18SMatthew Ahrens 	if (err != 0) {
23039e69d7d0SLori Alt 		dmu_tx_abort(tx);
23049e69d7d0SLori Alt 		return (err);
23059e69d7d0SLori Alt 	}
2306a2cdcdd2SPaul Dagnelie 	dmu_write(rwa->os, drrwbr->drr_object,
23079e69d7d0SLori Alt 	    drrwbr->drr_offset, drrwbr->drr_length, dbp->db_data, tx);
23089e69d7d0SLori Alt 	dmu_buf_rele(dbp, FTAG);
23099c3fd121SMatthew Ahrens 
23109c3fd121SMatthew Ahrens 	/* See comment in restore_write. */
23119c3fd121SMatthew Ahrens 	save_resume_state(rwa, drrwbr->drr_object, drrwbr->drr_offset, tx);
23129e69d7d0SLori Alt 	dmu_tx_commit(tx);
23139e69d7d0SLori Alt 	return (0);
23149e69d7d0SLori Alt }
23159e69d7d0SLori Alt 
23165d7b4d43SMatthew Ahrens static int
2317a2cdcdd2SPaul Dagnelie receive_write_embedded(struct receive_writer_arg *rwa,
23189c3fd121SMatthew Ahrens     struct drr_write_embedded *drrwe, void *data)
23195d7b4d43SMatthew Ahrens {
23205d7b4d43SMatthew Ahrens 	dmu_tx_t *tx;
23215d7b4d43SMatthew Ahrens 	int err;
23225d7b4d43SMatthew Ahrens 
23239c3fd121SMatthew Ahrens 	if (drrwe->drr_offset + drrwe->drr_length < drrwe->drr_offset)
23245d7b4d43SMatthew Ahrens 		return (EINVAL);
23255d7b4d43SMatthew Ahrens 
23269c3fd121SMatthew Ahrens 	if (drrwe->drr_psize > BPE_PAYLOAD_SIZE)
23275d7b4d43SMatthew Ahrens 		return (EINVAL);
23285d7b4d43SMatthew Ahrens 
23299c3fd121SMatthew Ahrens 	if (drrwe->drr_etype >= NUM_BP_EMBEDDED_TYPES)
23305d7b4d43SMatthew Ahrens 		return (EINVAL);
23319c3fd121SMatthew Ahrens 	if (drrwe->drr_compression >= ZIO_COMPRESS_FUNCTIONS)
23325d7b4d43SMatthew Ahrens 		return (EINVAL);
23335d7b4d43SMatthew Ahrens 
2334a2cdcdd2SPaul Dagnelie 	tx = dmu_tx_create(rwa->os);
23355d7b4d43SMatthew Ahrens 
23369c3fd121SMatthew Ahrens 	dmu_tx_hold_write(tx, drrwe->drr_object,
23379c3fd121SMatthew Ahrens 	    drrwe->drr_offset, drrwe->drr_length);
23385d7b4d43SMatthew Ahrens 	err = dmu_tx_assign(tx, TXG_WAIT);
23395d7b4d43SMatthew Ahrens 	if (err != 0) {
23405d7b4d43SMatthew Ahrens 		dmu_tx_abort(tx);
23415d7b4d43SMatthew Ahrens 		return (err);
23425d7b4d43SMatthew Ahrens 	}
23435d7b4d43SMatthew Ahrens 
23449c3fd121SMatthew Ahrens 	dmu_write_embedded(rwa->os, drrwe->drr_object,
23459c3fd121SMatthew Ahrens 	    drrwe->drr_offset, data, drrwe->drr_etype,
23469c3fd121SMatthew Ahrens 	    drrwe->drr_compression, drrwe->drr_lsize, drrwe->drr_psize,
2347a2cdcdd2SPaul Dagnelie 	    rwa->byteswap ^ ZFS_HOST_BYTEORDER, tx);
23485d7b4d43SMatthew Ahrens 
23499c3fd121SMatthew Ahrens 	/* See comment in restore_write. */
23509c3fd121SMatthew Ahrens 	save_resume_state(rwa, drrwe->drr_object, drrwe->drr_offset, tx);
23515d7b4d43SMatthew Ahrens 	dmu_tx_commit(tx);
23525d7b4d43SMatthew Ahrens 	return (0);
23535d7b4d43SMatthew Ahrens }
23545d7b4d43SMatthew Ahrens 
23550a586ceaSMark Shellenbaum static int
2356a2cdcdd2SPaul Dagnelie receive_spill(struct receive_writer_arg *rwa, struct drr_spill *drrs,
2357a2cdcdd2SPaul Dagnelie     void *data)
23580a586ceaSMark Shellenbaum {
23590a586ceaSMark Shellenbaum 	dmu_tx_t *tx;
23600a586ceaSMark Shellenbaum 	dmu_buf_t *db, *db_spill;
23610a586ceaSMark Shellenbaum 	int err;
23620a586ceaSMark Shellenbaum 
23630a586ceaSMark Shellenbaum 	if (drrs->drr_length < SPA_MINBLOCKSIZE ||
2364a2cdcdd2SPaul Dagnelie 	    drrs->drr_length > spa_maxblocksize(dmu_objset_spa(rwa->os)))
2365be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
23660a586ceaSMark Shellenbaum 
2367a2cdcdd2SPaul Dagnelie 	if (dmu_object_info(rwa->os, drrs->drr_object, NULL) != 0)
2368be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
23690a586ceaSMark Shellenbaum 
2370a2cdcdd2SPaul Dagnelie 	VERIFY0(dmu_bonus_hold(rwa->os, drrs->drr_object, FTAG, &db));
23710a586ceaSMark Shellenbaum 	if ((err = dmu_spill_hold_by_bonus(db, FTAG, &db_spill)) != 0) {
23720a586ceaSMark Shellenbaum 		dmu_buf_rele(db, FTAG);
23730a586ceaSMark Shellenbaum 		return (err);
23740a586ceaSMark Shellenbaum 	}
23750a586ceaSMark Shellenbaum 
2376a2cdcdd2SPaul Dagnelie 	tx = dmu_tx_create(rwa->os);
23770a586ceaSMark Shellenbaum 
23780a586ceaSMark Shellenbaum 	dmu_tx_hold_spill(tx, db->db_object);
23790a586ceaSMark Shellenbaum 
23800a586ceaSMark Shellenbaum 	err = dmu_tx_assign(tx, TXG_WAIT);
23813b2aab18SMatthew Ahrens 	if (err != 0) {
23820a586ceaSMark Shellenbaum 		dmu_buf_rele(db, FTAG);
23830a586ceaSMark Shellenbaum 		dmu_buf_rele(db_spill, FTAG);
23840a586ceaSMark Shellenbaum 		dmu_tx_abort(tx);
23850a586ceaSMark Shellenbaum 		return (err);
23860a586ceaSMark Shellenbaum 	}
23870a586ceaSMark Shellenbaum 	dmu_buf_will_dirty(db_spill, tx);
23880a586ceaSMark Shellenbaum 
23890a586ceaSMark Shellenbaum 	if (db_spill->db_size < drrs->drr_length)
23900a586ceaSMark Shellenbaum 		VERIFY(0 == dbuf_spill_set_blksz(db_spill,
23910a586ceaSMark Shellenbaum 		    drrs->drr_length, tx));
23920a586ceaSMark Shellenbaum 	bcopy(data, db_spill->db_data, drrs->drr_length);
23930a586ceaSMark Shellenbaum 
23940a586ceaSMark Shellenbaum 	dmu_buf_rele(db, FTAG);
23950a586ceaSMark Shellenbaum 	dmu_buf_rele(db_spill, FTAG);
23960a586ceaSMark Shellenbaum 
23970a586ceaSMark Shellenbaum 	dmu_tx_commit(tx);
23980a586ceaSMark Shellenbaum 	return (0);
23990a586ceaSMark Shellenbaum }
24000a586ceaSMark Shellenbaum 
2401efb80947Sahrens /* ARGSUSED */
2402efb80947Sahrens static int
2403a2cdcdd2SPaul Dagnelie receive_free(struct receive_writer_arg *rwa, struct drr_free *drrf)
2404efb80947Sahrens {
2405efb80947Sahrens 	int err;
2406efb80947Sahrens 
2407efb80947Sahrens 	if (drrf->drr_length != -1ULL &&
2408efb80947Sahrens 	    drrf->drr_offset + drrf->drr_length < drrf->drr_offset)
2409be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2410efb80947Sahrens 
2411a2cdcdd2SPaul Dagnelie 	if (dmu_object_info(rwa->os, drrf->drr_object, NULL) != 0)
2412be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2413efb80947Sahrens 
2414a2cdcdd2SPaul Dagnelie 	err = dmu_free_long_range(rwa->os, drrf->drr_object,
2415efb80947Sahrens 	    drrf->drr_offset, drrf->drr_length);
2416a2cdcdd2SPaul Dagnelie 
2417efb80947Sahrens 	return (err);
2418efb80947Sahrens }
2419efb80947Sahrens 
24203b2aab18SMatthew Ahrens /* used to destroy the drc_ds on error */
24213b2aab18SMatthew Ahrens static void
24223b2aab18SMatthew Ahrens dmu_recv_cleanup_ds(dmu_recv_cookie_t *drc)
24233b2aab18SMatthew Ahrens {
24249c3fd121SMatthew Ahrens 	if (drc->drc_resumable) {
24259c3fd121SMatthew Ahrens 		/* wait for our resume state to be written to disk */
24269c3fd121SMatthew Ahrens 		txg_wait_synced(drc->drc_ds->ds_dir->dd_pool, 0);
24279c3fd121SMatthew Ahrens 		dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
24289c3fd121SMatthew Ahrens 	} else {
24299adfa60dSMatthew Ahrens 		char name[ZFS_MAX_DATASET_NAME_LEN];
24309c3fd121SMatthew Ahrens 		dsl_dataset_name(drc->drc_ds, name);
24319c3fd121SMatthew Ahrens 		dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
24329c3fd121SMatthew Ahrens 		(void) dsl_destroy_head(name);
24339c3fd121SMatthew Ahrens 	}
24343b2aab18SMatthew Ahrens }
24353b2aab18SMatthew Ahrens 
243698110f08SMatthew Ahrens static void
2437a2cdcdd2SPaul Dagnelie receive_cksum(struct receive_arg *ra, int len, void *buf)
243898110f08SMatthew Ahrens {
243998110f08SMatthew Ahrens 	if (ra->byteswap) {
2440770499e1SDan Kimmel 		(void) fletcher_4_incremental_byteswap(buf, len, &ra->cksum);
244198110f08SMatthew Ahrens 	} else {
2442770499e1SDan Kimmel 		(void) fletcher_4_incremental_native(buf, len, &ra->cksum);
244398110f08SMatthew Ahrens 	}
244498110f08SMatthew Ahrens }
244598110f08SMatthew Ahrens 
244698110f08SMatthew Ahrens /*
2447a2cdcdd2SPaul Dagnelie  * Read the payload into a buffer of size len, and update the current record's
2448a2cdcdd2SPaul Dagnelie  * payload field.
2449a2cdcdd2SPaul Dagnelie  * Allocate ra->next_rrd and read the next record's header into
2450a2cdcdd2SPaul Dagnelie  * ra->next_rrd->header.
245198110f08SMatthew Ahrens  * Verify checksum of payload and next record.
245298110f08SMatthew Ahrens  */
245398110f08SMatthew Ahrens static int
2454a2cdcdd2SPaul Dagnelie receive_read_payload_and_next_header(struct receive_arg *ra, int len, void *buf)
245598110f08SMatthew Ahrens {
245698110f08SMatthew Ahrens 	int err;
245798110f08SMatthew Ahrens 
245898110f08SMatthew Ahrens 	if (len != 0) {
2459a2cdcdd2SPaul Dagnelie 		ASSERT3U(len, <=, SPA_MAXBLOCKSIZE);
24609c3fd121SMatthew Ahrens 		err = receive_read(ra, len, buf);
246198110f08SMatthew Ahrens 		if (err != 0)
246298110f08SMatthew Ahrens 			return (err);
24639c3fd121SMatthew Ahrens 		receive_cksum(ra, len, buf);
24649c3fd121SMatthew Ahrens 
24659c3fd121SMatthew Ahrens 		/* note: rrd is NULL when reading the begin record's payload */
24669c3fd121SMatthew Ahrens 		if (ra->rrd != NULL) {
24679c3fd121SMatthew Ahrens 			ra->rrd->payload = buf;
24689c3fd121SMatthew Ahrens 			ra->rrd->payload_size = len;
24699c3fd121SMatthew Ahrens 			ra->rrd->bytes_read = ra->bytes_read;
24709c3fd121SMatthew Ahrens 		}
247198110f08SMatthew Ahrens 	}
247298110f08SMatthew Ahrens 
247398110f08SMatthew Ahrens 	ra->prev_cksum = ra->cksum;
247498110f08SMatthew Ahrens 
2475a2cdcdd2SPaul Dagnelie 	ra->next_rrd = kmem_zalloc(sizeof (*ra->next_rrd), KM_SLEEP);
2476a2cdcdd2SPaul Dagnelie 	err = receive_read(ra, sizeof (ra->next_rrd->header),
2477a2cdcdd2SPaul Dagnelie 	    &ra->next_rrd->header);
24789c3fd121SMatthew Ahrens 	ra->next_rrd->bytes_read = ra->bytes_read;
2479a2cdcdd2SPaul Dagnelie 	if (err != 0) {
2480a2cdcdd2SPaul Dagnelie 		kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
2481a2cdcdd2SPaul Dagnelie 		ra->next_rrd = NULL;
248298110f08SMatthew Ahrens 		return (err);
2483a2cdcdd2SPaul Dagnelie 	}
2484a2cdcdd2SPaul Dagnelie 	if (ra->next_rrd->header.drr_type == DRR_BEGIN) {
2485a2cdcdd2SPaul Dagnelie 		kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
2486a2cdcdd2SPaul Dagnelie 		ra->next_rrd = NULL;
248798110f08SMatthew Ahrens 		return (SET_ERROR(EINVAL));
2488a2cdcdd2SPaul Dagnelie 	}
248998110f08SMatthew Ahrens 
249098110f08SMatthew Ahrens 	/*
249198110f08SMatthew Ahrens 	 * Note: checksum is of everything up to but not including the
249298110f08SMatthew Ahrens 	 * checksum itself.
249398110f08SMatthew Ahrens 	 */
249498110f08SMatthew Ahrens 	ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
249598110f08SMatthew Ahrens 	    ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
2496a2cdcdd2SPaul Dagnelie 	receive_cksum(ra,
249798110f08SMatthew Ahrens 	    offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
2498a2cdcdd2SPaul Dagnelie 	    &ra->next_rrd->header);
249998110f08SMatthew Ahrens 
2500a2cdcdd2SPaul Dagnelie 	zio_cksum_t cksum_orig =
2501a2cdcdd2SPaul Dagnelie 	    ra->next_rrd->header.drr_u.drr_checksum.drr_checksum;
2502a2cdcdd2SPaul Dagnelie 	zio_cksum_t *cksump =
2503a2cdcdd2SPaul Dagnelie 	    &ra->next_rrd->header.drr_u.drr_checksum.drr_checksum;
250498110f08SMatthew Ahrens 
250598110f08SMatthew Ahrens 	if (ra->byteswap)
2506a2cdcdd2SPaul Dagnelie 		byteswap_record(&ra->next_rrd->header);
250798110f08SMatthew Ahrens 
250898110f08SMatthew Ahrens 	if ((!ZIO_CHECKSUM_IS_ZERO(cksump)) &&
2509a2cdcdd2SPaul Dagnelie 	    !ZIO_CHECKSUM_EQUAL(ra->cksum, *cksump)) {
2510a2cdcdd2SPaul Dagnelie 		kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
2511a2cdcdd2SPaul Dagnelie 		ra->next_rrd = NULL;
251298110f08SMatthew Ahrens 		return (SET_ERROR(ECKSUM));
2513a2cdcdd2SPaul Dagnelie 	}
251498110f08SMatthew Ahrens 
2515a2cdcdd2SPaul Dagnelie 	receive_cksum(ra, sizeof (cksum_orig), &cksum_orig);
251698110f08SMatthew Ahrens 
251798110f08SMatthew Ahrens 	return (0);
251898110f08SMatthew Ahrens }
251998110f08SMatthew Ahrens 
252068ecb2ecSPaul Dagnelie static void
252168ecb2ecSPaul Dagnelie objlist_create(struct objlist *list)
252268ecb2ecSPaul Dagnelie {
252368ecb2ecSPaul Dagnelie 	list_create(&list->list, sizeof (struct receive_objnode),
252468ecb2ecSPaul Dagnelie 	    offsetof(struct receive_objnode, node));
252568ecb2ecSPaul Dagnelie 	list->last_lookup = 0;
252668ecb2ecSPaul Dagnelie }
252768ecb2ecSPaul Dagnelie 
252868ecb2ecSPaul Dagnelie static void
252968ecb2ecSPaul Dagnelie objlist_destroy(struct objlist *list)
253068ecb2ecSPaul Dagnelie {
253168ecb2ecSPaul Dagnelie 	for (struct receive_objnode *n = list_remove_head(&list->list);
253268ecb2ecSPaul Dagnelie 	    n != NULL; n = list_remove_head(&list->list)) {
253368ecb2ecSPaul Dagnelie 		kmem_free(n, sizeof (*n));
253468ecb2ecSPaul Dagnelie 	}
253568ecb2ecSPaul Dagnelie 	list_destroy(&list->list);
253668ecb2ecSPaul Dagnelie }
253768ecb2ecSPaul Dagnelie 
253868ecb2ecSPaul Dagnelie /*
253968ecb2ecSPaul Dagnelie  * This function looks through the objlist to see if the specified object number
254068ecb2ecSPaul Dagnelie  * is contained in the objlist.  In the process, it will remove all object
254168ecb2ecSPaul Dagnelie  * numbers in the list that are smaller than the specified object number.  Thus,
254268ecb2ecSPaul Dagnelie  * any lookup of an object number smaller than a previously looked up object
254368ecb2ecSPaul Dagnelie  * number will always return false; therefore, all lookups should be done in
254468ecb2ecSPaul Dagnelie  * ascending order.
254568ecb2ecSPaul Dagnelie  */
254668ecb2ecSPaul Dagnelie static boolean_t
254768ecb2ecSPaul Dagnelie objlist_exists(struct objlist *list, uint64_t object)
254868ecb2ecSPaul Dagnelie {
254968ecb2ecSPaul Dagnelie 	struct receive_objnode *node = list_head(&list->list);
255068ecb2ecSPaul Dagnelie 	ASSERT3U(object, >=, list->last_lookup);
255168ecb2ecSPaul Dagnelie 	list->last_lookup = object;
255268ecb2ecSPaul Dagnelie 	while (node != NULL && node->object < object) {
255368ecb2ecSPaul Dagnelie 		VERIFY3P(node, ==, list_remove_head(&list->list));
255468ecb2ecSPaul Dagnelie 		kmem_free(node, sizeof (*node));
255568ecb2ecSPaul Dagnelie 		node = list_head(&list->list);
255668ecb2ecSPaul Dagnelie 	}
255768ecb2ecSPaul Dagnelie 	return (node != NULL && node->object == object);
255868ecb2ecSPaul Dagnelie }
255968ecb2ecSPaul Dagnelie 
256068ecb2ecSPaul Dagnelie /*
256168ecb2ecSPaul Dagnelie  * The objlist is a list of object numbers stored in ascending order.  However,
256268ecb2ecSPaul Dagnelie  * the insertion of new object numbers does not seek out the correct location to
256368ecb2ecSPaul Dagnelie  * store a new object number; instead, it appends it to the list for simplicity.
256468ecb2ecSPaul Dagnelie  * Thus, any users must take care to only insert new object numbers in ascending
256568ecb2ecSPaul Dagnelie  * order.
256668ecb2ecSPaul Dagnelie  */
256768ecb2ecSPaul Dagnelie static void
256868ecb2ecSPaul Dagnelie objlist_insert(struct objlist *list, uint64_t object)
256968ecb2ecSPaul Dagnelie {
257068ecb2ecSPaul Dagnelie 	struct receive_objnode *node = kmem_zalloc(sizeof (*node), KM_SLEEP);
257168ecb2ecSPaul Dagnelie 	node->object = object;
257268ecb2ecSPaul Dagnelie #ifdef ZFS_DEBUG
257368ecb2ecSPaul Dagnelie 	struct receive_objnode *last_object = list_tail(&list->list);
257468ecb2ecSPaul Dagnelie 	uint64_t last_objnum = (last_object != NULL ? last_object->object : 0);
257568ecb2ecSPaul Dagnelie 	ASSERT3U(node->object, >, last_objnum);
257668ecb2ecSPaul Dagnelie #endif
257768ecb2ecSPaul Dagnelie 	list_insert_tail(&list->list, node);
257868ecb2ecSPaul Dagnelie }
257968ecb2ecSPaul Dagnelie 
2580a2cdcdd2SPaul Dagnelie /*
2581a2cdcdd2SPaul Dagnelie  * Issue the prefetch reads for any necessary indirect blocks.
2582a2cdcdd2SPaul Dagnelie  *
2583a2cdcdd2SPaul Dagnelie  * We use the object ignore list to tell us whether or not to issue prefetches
2584a2cdcdd2SPaul Dagnelie  * for a given object.  We do this for both correctness (in case the blocksize
2585a2cdcdd2SPaul Dagnelie  * of an object has changed) and performance (if the object doesn't exist, don't
2586a2cdcdd2SPaul Dagnelie  * needlessly try to issue prefetches).  We also trim the list as we go through
2587a2cdcdd2SPaul Dagnelie  * the stream to prevent it from growing to an unbounded size.
2588a2cdcdd2SPaul Dagnelie  *
2589a2cdcdd2SPaul Dagnelie  * The object numbers within will always be in sorted order, and any write
2590a2cdcdd2SPaul Dagnelie  * records we see will also be in sorted order, but they're not sorted with
2591a2cdcdd2SPaul Dagnelie  * respect to each other (i.e. we can get several object records before
2592a2cdcdd2SPaul Dagnelie  * receiving each object's write records).  As a result, once we've reached a
2593a2cdcdd2SPaul Dagnelie  * given object number, we can safely remove any reference to lower object
2594a2cdcdd2SPaul Dagnelie  * numbers in the ignore list. In practice, we receive up to 32 object records
2595a2cdcdd2SPaul Dagnelie  * before receiving write records, so the list can have up to 32 nodes in it.
2596a2cdcdd2SPaul Dagnelie  */
2597a2cdcdd2SPaul Dagnelie /* ARGSUSED */
2598a2cdcdd2SPaul Dagnelie static void
2599a2cdcdd2SPaul Dagnelie receive_read_prefetch(struct receive_arg *ra,
2600a2cdcdd2SPaul Dagnelie     uint64_t object, uint64_t offset, uint64_t length)
2601a2cdcdd2SPaul Dagnelie {
260268ecb2ecSPaul Dagnelie 	if (!objlist_exists(&ra->ignore_objlist, object)) {
2603a2cdcdd2SPaul Dagnelie 		dmu_prefetch(ra->os, object, 1, offset, length,
2604a2cdcdd2SPaul Dagnelie 		    ZIO_PRIORITY_SYNC_READ);
2605a2cdcdd2SPaul Dagnelie 	}
2606a2cdcdd2SPaul Dagnelie }
2607a2cdcdd2SPaul Dagnelie 
2608a2cdcdd2SPaul Dagnelie /*
2609a2cdcdd2SPaul Dagnelie  * Read records off the stream, issuing any necessary prefetches.
2610a2cdcdd2SPaul Dagnelie  */
261198110f08SMatthew Ahrens static int
2612a2cdcdd2SPaul Dagnelie receive_read_record(struct receive_arg *ra)
261398110f08SMatthew Ahrens {
261498110f08SMatthew Ahrens 	int err;
261598110f08SMatthew Ahrens 
2616a2cdcdd2SPaul Dagnelie 	switch (ra->rrd->header.drr_type) {
261798110f08SMatthew Ahrens 	case DRR_OBJECT:
261898110f08SMatthew Ahrens 	{
2619a2cdcdd2SPaul Dagnelie 		struct drr_object *drro = &ra->rrd->header.drr_u.drr_object;
2620a2cdcdd2SPaul Dagnelie 		uint32_t size = P2ROUNDUP(drro->drr_bonuslen, 8);
2621a2cdcdd2SPaul Dagnelie 		void *buf = kmem_zalloc(size, KM_SLEEP);
2622a2cdcdd2SPaul Dagnelie 		dmu_object_info_t doi;
2623a2cdcdd2SPaul Dagnelie 		err = receive_read_payload_and_next_header(ra, size, buf);
2624a2cdcdd2SPaul Dagnelie 		if (err != 0) {
2625a2cdcdd2SPaul Dagnelie 			kmem_free(buf, size);
262698110f08SMatthew Ahrens 			return (err);
2627a2cdcdd2SPaul Dagnelie 		}
2628a2cdcdd2SPaul Dagnelie 		err = dmu_object_info(ra->os, drro->drr_object, &doi);
2629a2cdcdd2SPaul Dagnelie 		/*
2630a2cdcdd2SPaul Dagnelie 		 * See receive_read_prefetch for an explanation why we're
2631a2cdcdd2SPaul Dagnelie 		 * storing this object in the ignore_obj_list.
2632a2cdcdd2SPaul Dagnelie 		 */
2633a2cdcdd2SPaul Dagnelie 		if (err == ENOENT ||
2634a2cdcdd2SPaul Dagnelie 		    (err == 0 && doi.doi_data_block_size != drro->drr_blksz)) {
263568ecb2ecSPaul Dagnelie 			objlist_insert(&ra->ignore_objlist, drro->drr_object);
2636a2cdcdd2SPaul Dagnelie 			err = 0;
2637a2cdcdd2SPaul Dagnelie 		}
2638a2cdcdd2SPaul Dagnelie 		return (err);
263998110f08SMatthew Ahrens 	}
264098110f08SMatthew Ahrens 	case DRR_FREEOBJECTS:
264198110f08SMatthew Ahrens 	{
2642a2cdcdd2SPaul Dagnelie 		err = receive_read_payload_and_next_header(ra, 0, NULL);
2643a2cdcdd2SPaul Dagnelie 		return (err);
264498110f08SMatthew Ahrens 	}
264598110f08SMatthew Ahrens 	case DRR_WRITE:
264698110f08SMatthew Ahrens 	{
2647a2cdcdd2SPaul Dagnelie 		struct drr_write *drrw = &ra->rrd->header.drr_u.drr_write;
26485602294fSDan Kimmel 		arc_buf_t *abuf;
26495602294fSDan Kimmel 		boolean_t is_meta = DMU_OT_IS_METADATA(drrw->drr_type);
26505602294fSDan Kimmel 		if (DRR_WRITE_COMPRESSED(drrw)) {
26515602294fSDan Kimmel 			ASSERT3U(drrw->drr_compressed_size, >, 0);
26525602294fSDan Kimmel 			ASSERT3U(drrw->drr_logical_size, >=,
26535602294fSDan Kimmel 			    drrw->drr_compressed_size);
26545602294fSDan Kimmel 			ASSERT(!is_meta);
26555602294fSDan Kimmel 			abuf = arc_loan_compressed_buf(
26565602294fSDan Kimmel 			    dmu_objset_spa(ra->os),
26575602294fSDan Kimmel 			    drrw->drr_compressed_size, drrw->drr_logical_size,
26585602294fSDan Kimmel 			    drrw->drr_compressiontype);
26595602294fSDan Kimmel 		} else {
26605602294fSDan Kimmel 			abuf = arc_loan_buf(dmu_objset_spa(ra->os),
26615602294fSDan Kimmel 			    is_meta, drrw->drr_logical_size);
26625602294fSDan Kimmel 		}
266398110f08SMatthew Ahrens 
2664a2cdcdd2SPaul Dagnelie 		err = receive_read_payload_and_next_header(ra,
26655602294fSDan Kimmel 		    DRR_WRITE_PAYLOAD_SIZE(drrw), abuf->b_data);
2666a2cdcdd2SPaul Dagnelie 		if (err != 0) {
266798110f08SMatthew Ahrens 			dmu_return_arcbuf(abuf);
2668a2cdcdd2SPaul Dagnelie 			return (err);
2669a2cdcdd2SPaul Dagnelie 		}
2670a2cdcdd2SPaul Dagnelie 		ra->rrd->write_buf = abuf;
2671a2cdcdd2SPaul Dagnelie 		receive_read_prefetch(ra, drrw->drr_object, drrw->drr_offset,
26725602294fSDan Kimmel 		    drrw->drr_logical_size);
267398110f08SMatthew Ahrens 		return (err);
267498110f08SMatthew Ahrens 	}
267598110f08SMatthew Ahrens 	case DRR_WRITE_BYREF:
267698110f08SMatthew Ahrens 	{
2677a2cdcdd2SPaul Dagnelie 		struct drr_write_byref *drrwb =
2678a2cdcdd2SPaul Dagnelie 		    &ra->rrd->header.drr_u.drr_write_byref;
2679a2cdcdd2SPaul Dagnelie 		err = receive_read_payload_and_next_header(ra, 0, NULL);
2680a2cdcdd2SPaul Dagnelie 		receive_read_prefetch(ra, drrwb->drr_object, drrwb->drr_offset,
2681a2cdcdd2SPaul Dagnelie 		    drrwb->drr_length);
2682a2cdcdd2SPaul Dagnelie 		return (err);
268398110f08SMatthew Ahrens 	}
268498110f08SMatthew Ahrens 	case DRR_WRITE_EMBEDDED:
268598110f08SMatthew Ahrens 	{
268698110f08SMatthew Ahrens 		struct drr_write_embedded *drrwe =
2687a2cdcdd2SPaul Dagnelie 		    &ra->rrd->header.drr_u.drr_write_embedded;
2688a2cdcdd2SPaul Dagnelie 		uint32_t size = P2ROUNDUP(drrwe->drr_psize, 8);
2689a2cdcdd2SPaul Dagnelie 		void *buf = kmem_zalloc(size, KM_SLEEP);
2690a2cdcdd2SPaul Dagnelie 
2691a2cdcdd2SPaul Dagnelie 		err = receive_read_payload_and_next_header(ra, size, buf);
2692a2cdcdd2SPaul Dagnelie 		if (err != 0) {
2693a2cdcdd2SPaul Dagnelie 			kmem_free(buf, size);
269498110f08SMatthew Ahrens 			return (err);
2695a2cdcdd2SPaul Dagnelie 		}
2696a2cdcdd2SPaul Dagnelie 
2697a2cdcdd2SPaul Dagnelie 		receive_read_prefetch(ra, drrwe->drr_object, drrwe->drr_offset,
2698a2cdcdd2SPaul Dagnelie 		    drrwe->drr_length);
2699a2cdcdd2SPaul Dagnelie 		return (err);
270098110f08SMatthew Ahrens 	}
270198110f08SMatthew Ahrens 	case DRR_FREE:
270298110f08SMatthew Ahrens 	{
2703a2cdcdd2SPaul Dagnelie 		/*
2704a2cdcdd2SPaul Dagnelie 		 * It might be beneficial to prefetch indirect blocks here, but
2705a2cdcdd2SPaul Dagnelie 		 * we don't really have the data to decide for sure.
2706a2cdcdd2SPaul Dagnelie 		 */
2707a2cdcdd2SPaul Dagnelie 		err = receive_read_payload_and_next_header(ra, 0, NULL);
2708a2cdcdd2SPaul Dagnelie 		return (err);
270998110f08SMatthew Ahrens 	}
271098110f08SMatthew Ahrens 	case DRR_END:
271198110f08SMatthew Ahrens 	{
2712a2cdcdd2SPaul Dagnelie 		struct drr_end *drre = &ra->rrd->header.drr_u.drr_end;
271398110f08SMatthew Ahrens 		if (!ZIO_CHECKSUM_EQUAL(ra->prev_cksum, drre->drr_checksum))
27149c3fd121SMatthew Ahrens 			return (SET_ERROR(ECKSUM));
271598110f08SMatthew Ahrens 		return (0);
271698110f08SMatthew Ahrens 	}
271798110f08SMatthew Ahrens 	case DRR_SPILL:
271898110f08SMatthew Ahrens 	{
2719a2cdcdd2SPaul Dagnelie 		struct drr_spill *drrs = &ra->rrd->header.drr_u.drr_spill;
2720a2cdcdd2SPaul Dagnelie 		void *buf = kmem_zalloc(drrs->drr_length, KM_SLEEP);
2721a2cdcdd2SPaul Dagnelie 		err = receive_read_payload_and_next_header(ra, drrs->drr_length,
2722a2cdcdd2SPaul Dagnelie 		    buf);
272398110f08SMatthew Ahrens 		if (err != 0)
2724a2cdcdd2SPaul Dagnelie 			kmem_free(buf, drrs->drr_length);
2725a2cdcdd2SPaul Dagnelie 		return (err);
272698110f08SMatthew Ahrens 	}
272798110f08SMatthew Ahrens 	default:
272898110f08SMatthew Ahrens 		return (SET_ERROR(EINVAL));
272998110f08SMatthew Ahrens 	}
273098110f08SMatthew Ahrens }
273198110f08SMatthew Ahrens 
27323cb34c60Sahrens /*
2733a2cdcdd2SPaul Dagnelie  * Commit the records to the pool.
2734a2cdcdd2SPaul Dagnelie  */
2735a2cdcdd2SPaul Dagnelie static int
2736a2cdcdd2SPaul Dagnelie receive_process_record(struct receive_writer_arg *rwa,
2737a2cdcdd2SPaul Dagnelie     struct receive_record_arg *rrd)
2738a2cdcdd2SPaul Dagnelie {
2739a2cdcdd2SPaul Dagnelie 	int err;
2740a2cdcdd2SPaul Dagnelie 
27419c3fd121SMatthew Ahrens 	/* Processing in order, therefore bytes_read should be increasing. */
27429c3fd121SMatthew Ahrens 	ASSERT3U(rrd->bytes_read, >=, rwa->bytes_read);
27439c3fd121SMatthew Ahrens 	rwa->bytes_read = rrd->bytes_read;
27449c3fd121SMatthew Ahrens 
2745a2cdcdd2SPaul Dagnelie 	switch (rrd->header.drr_type) {
2746a2cdcdd2SPaul Dagnelie 	case DRR_OBJECT:
2747a2cdcdd2SPaul Dagnelie 	{
2748a2cdcdd2SPaul Dagnelie 		struct drr_object *drro = &rrd->header.drr_u.drr_object;
2749a2cdcdd2SPaul Dagnelie 		err = receive_object(rwa, drro, rrd->payload);
2750a2cdcdd2SPaul Dagnelie 		kmem_free(rrd->payload, rrd->payload_size);
2751a2cdcdd2SPaul Dagnelie 		rrd->payload = NULL;
2752a2cdcdd2SPaul Dagnelie 		return (err);
2753a2cdcdd2SPaul Dagnelie 	}
2754a2cdcdd2SPaul Dagnelie 	case DRR_FREEOBJECTS:
2755a2cdcdd2SPaul Dagnelie 	{
2756a2cdcdd2SPaul Dagnelie 		struct drr_freeobjects *drrfo =
2757a2cdcdd2SPaul Dagnelie 		    &rrd->header.drr_u.drr_freeobjects;
2758a2cdcdd2SPaul Dagnelie 		return (receive_freeobjects(rwa, drrfo));
2759a2cdcdd2SPaul Dagnelie 	}
2760a2cdcdd2SPaul Dagnelie 	case DRR_WRITE:
2761a2cdcdd2SPaul Dagnelie 	{
2762a2cdcdd2SPaul Dagnelie 		struct drr_write *drrw = &rrd->header.drr_u.drr_write;
2763a2cdcdd2SPaul Dagnelie 		err = receive_write(rwa, drrw, rrd->write_buf);
2764a2cdcdd2SPaul Dagnelie 		/* if receive_write() is successful, it consumes the arc_buf */
2765a2cdcdd2SPaul Dagnelie 		if (err != 0)
2766a2cdcdd2SPaul Dagnelie 			dmu_return_arcbuf(rrd->write_buf);
2767a2cdcdd2SPaul Dagnelie 		rrd->write_buf = NULL;
2768a2cdcdd2SPaul Dagnelie 		rrd->payload = NULL;
2769a2cdcdd2SPaul Dagnelie 		return (err);
2770a2cdcdd2SPaul Dagnelie 	}
2771a2cdcdd2SPaul Dagnelie 	case DRR_WRITE_BYREF:
2772a2cdcdd2SPaul Dagnelie 	{
2773a2cdcdd2SPaul Dagnelie 		struct drr_write_byref *drrwbr =
2774a2cdcdd2SPaul Dagnelie 		    &rrd->header.drr_u.drr_write_byref;
2775a2cdcdd2SPaul Dagnelie 		return (receive_write_byref(rwa, drrwbr));
2776a2cdcdd2SPaul Dagnelie 	}
2777a2cdcdd2SPaul Dagnelie 	case DRR_WRITE_EMBEDDED:
2778a2cdcdd2SPaul Dagnelie 	{
2779a2cdcdd2SPaul Dagnelie 		struct drr_write_embedded *drrwe =
2780a2cdcdd2SPaul Dagnelie 		    &rrd->header.drr_u.drr_write_embedded;
2781a2cdcdd2SPaul Dagnelie 		err = receive_write_embedded(rwa, drrwe, rrd->payload);
2782a2cdcdd2SPaul Dagnelie 		kmem_free(rrd->payload, rrd->payload_size);
2783a2cdcdd2SPaul Dagnelie 		rrd->payload = NULL;
2784a2cdcdd2SPaul Dagnelie 		return (err);
2785a2cdcdd2SPaul Dagnelie 	}
2786a2cdcdd2SPaul Dagnelie 	case DRR_FREE:
2787a2cdcdd2SPaul Dagnelie 	{
2788a2cdcdd2SPaul Dagnelie 		struct drr_free *drrf = &rrd->header.drr_u.drr_free;
2789a2cdcdd2SPaul Dagnelie 		return (receive_free(rwa, drrf));
2790a2cdcdd2SPaul Dagnelie 	}
2791a2cdcdd2SPaul Dagnelie 	case DRR_SPILL:
2792a2cdcdd2SPaul Dagnelie 	{
2793a2cdcdd2SPaul Dagnelie 		struct drr_spill *drrs = &rrd->header.drr_u.drr_spill;
2794a2cdcdd2SPaul Dagnelie 		err = receive_spill(rwa, drrs, rrd->payload);
2795a2cdcdd2SPaul Dagnelie 		kmem_free(rrd->payload, rrd->payload_size);
2796a2cdcdd2SPaul Dagnelie 		rrd->payload = NULL;
2797a2cdcdd2SPaul Dagnelie 		return (err);
2798a2cdcdd2SPaul Dagnelie 	}
2799a2cdcdd2SPaul Dagnelie 	default:
2800a2cdcdd2SPaul Dagnelie 		return (SET_ERROR(EINVAL));
2801a2cdcdd2SPaul Dagnelie 	}
2802a2cdcdd2SPaul Dagnelie }
2803a2cdcdd2SPaul Dagnelie 
2804a2cdcdd2SPaul Dagnelie /*
2805a2cdcdd2SPaul Dagnelie  * dmu_recv_stream's worker thread; pull records off the queue, and then call
2806a2cdcdd2SPaul Dagnelie  * receive_process_record  When we're done, signal the main thread and exit.
2807a2cdcdd2SPaul Dagnelie  */
2808a2cdcdd2SPaul Dagnelie static void
2809a2cdcdd2SPaul Dagnelie receive_writer_thread(void *arg)
2810a2cdcdd2SPaul Dagnelie {
2811a2cdcdd2SPaul Dagnelie 	struct receive_writer_arg *rwa = arg;
2812a2cdcdd2SPaul Dagnelie 	struct receive_record_arg *rrd;
2813a2cdcdd2SPaul Dagnelie 	for (rrd = bqueue_dequeue(&rwa->q); !rrd->eos_marker;
2814a2cdcdd2SPaul Dagnelie 	    rrd = bqueue_dequeue(&rwa->q)) {
2815a2cdcdd2SPaul Dagnelie 		/*
2816a2cdcdd2SPaul Dagnelie 		 * If there's an error, the main thread will stop putting things
2817a2cdcdd2SPaul Dagnelie 		 * on the queue, but we need to clear everything in it before we
2818a2cdcdd2SPaul Dagnelie 		 * can exit.
2819a2cdcdd2SPaul Dagnelie 		 */
2820a2cdcdd2SPaul Dagnelie 		if (rwa->err == 0) {
2821a2cdcdd2SPaul Dagnelie 			rwa->err = receive_process_record(rwa, rrd);
2822a2cdcdd2SPaul Dagnelie 		} else if (rrd->write_buf != NULL) {
2823a2cdcdd2SPaul Dagnelie 			dmu_return_arcbuf(rrd->write_buf);
2824a2cdcdd2SPaul Dagnelie 			rrd->write_buf = NULL;
2825a2cdcdd2SPaul Dagnelie 			rrd->payload = NULL;
2826a2cdcdd2SPaul Dagnelie 		} else if (rrd->payload != NULL) {
2827a2cdcdd2SPaul Dagnelie 			kmem_free(rrd->payload, rrd->payload_size);
2828a2cdcdd2SPaul Dagnelie 			rrd->payload = NULL;
2829a2cdcdd2SPaul Dagnelie 		}
2830a2cdcdd2SPaul Dagnelie 		kmem_free(rrd, sizeof (*rrd));
2831a2cdcdd2SPaul Dagnelie 	}
2832a2cdcdd2SPaul Dagnelie 	kmem_free(rrd, sizeof (*rrd));
2833a2cdcdd2SPaul Dagnelie 	mutex_enter(&rwa->mutex);
2834a2cdcdd2SPaul Dagnelie 	rwa->done = B_TRUE;
2835a2cdcdd2SPaul Dagnelie 	cv_signal(&rwa->cv);
2836a2cdcdd2SPaul Dagnelie 	mutex_exit(&rwa->mutex);
2837f2c1e9bcSJorgen Lundman 	thread_exit();
2838a2cdcdd2SPaul Dagnelie }
2839a2cdcdd2SPaul Dagnelie 
28409c3fd121SMatthew Ahrens static int
28419c3fd121SMatthew Ahrens resume_check(struct receive_arg *ra, nvlist_t *begin_nvl)
28429c3fd121SMatthew Ahrens {
28439c3fd121SMatthew Ahrens 	uint64_t val;
28449c3fd121SMatthew Ahrens 	objset_t *mos = dmu_objset_pool(ra->os)->dp_meta_objset;
28459c3fd121SMatthew Ahrens 	uint64_t dsobj = dmu_objset_id(ra->os);
28469c3fd121SMatthew Ahrens 	uint64_t resume_obj, resume_off;
28479c3fd121SMatthew Ahrens 
28489c3fd121SMatthew Ahrens 	if (nvlist_lookup_uint64(begin_nvl,
28499c3fd121SMatthew Ahrens 	    "resume_object", &resume_obj) != 0 ||
28509c3fd121SMatthew Ahrens 	    nvlist_lookup_uint64(begin_nvl,
28519c3fd121SMatthew Ahrens 	    "resume_offset", &resume_off) != 0) {
28529c3fd121SMatthew Ahrens 		return (SET_ERROR(EINVAL));
28539c3fd121SMatthew Ahrens 	}
28549c3fd121SMatthew Ahrens 	VERIFY0(zap_lookup(mos, dsobj,
28559c3fd121SMatthew Ahrens 	    DS_FIELD_RESUME_OBJECT, sizeof (val), 1, &val));
28569c3fd121SMatthew Ahrens 	if (resume_obj != val)
28579c3fd121SMatthew Ahrens 		return (SET_ERROR(EINVAL));
28589c3fd121SMatthew Ahrens 	VERIFY0(zap_lookup(mos, dsobj,
28599c3fd121SMatthew Ahrens 	    DS_FIELD_RESUME_OFFSET, sizeof (val), 1, &val));
28609c3fd121SMatthew Ahrens 	if (resume_off != val)
28619c3fd121SMatthew Ahrens 		return (SET_ERROR(EINVAL));
28629c3fd121SMatthew Ahrens 
28639c3fd121SMatthew Ahrens 	return (0);
28649c3fd121SMatthew Ahrens }
28659c3fd121SMatthew Ahrens 
2866a2cdcdd2SPaul Dagnelie /*
2867a2cdcdd2SPaul Dagnelie  * Read in the stream's records, one by one, and apply them to the pool.  There
2868a2cdcdd2SPaul Dagnelie  * are two threads involved; the thread that calls this function will spin up a
2869a2cdcdd2SPaul Dagnelie  * worker thread, read the records off the stream one by one, and issue
2870a2cdcdd2SPaul Dagnelie  * prefetches for any necessary indirect blocks.  It will then push the records
2871a2cdcdd2SPaul Dagnelie  * onto an internal blocking queue.  The worker thread will pull the records off
2872a2cdcdd2SPaul Dagnelie  * the queue, and actually write the data into the DMU.  This way, the worker
2873a2cdcdd2SPaul Dagnelie  * thread doesn't have to wait for reads to complete, since everything it needs
2874a2cdcdd2SPaul Dagnelie  * (the indirect blocks) will be prefetched.
2875a2cdcdd2SPaul Dagnelie  *
28763cb34c60Sahrens  * NB: callers *must* call dmu_recv_end() if this succeeds.
28773cb34c60Sahrens  */
2878efb80947Sahrens int
2879c99e4bdcSChris Kirby dmu_recv_stream(dmu_recv_cookie_t *drc, vnode_t *vp, offset_t *voffp,
2880c99e4bdcSChris Kirby     int cleanup_fd, uint64_t *action_handlep)
2881efb80947Sahrens {
288298110f08SMatthew Ahrens 	int err = 0;
2883a2cdcdd2SPaul Dagnelie 	struct receive_arg ra = { 0 };
2884a2cdcdd2SPaul Dagnelie 	struct receive_writer_arg rwa = { 0 };
28859e69d7d0SLori Alt 	int featureflags;
28869c3fd121SMatthew Ahrens 	nvlist_t *begin_nvl = NULL;
2887efb80947Sahrens 
28883b2aab18SMatthew Ahrens 	ra.byteswap = drc->drc_byteswap;
28893b2aab18SMatthew Ahrens 	ra.cksum = drc->drc_cksum;
28903cb34c60Sahrens 	ra.vp = vp;
28913cb34c60Sahrens 	ra.voff = *voffp;
28929c3fd121SMatthew Ahrens 
28939c3fd121SMatthew Ahrens 	if (dsl_dataset_is_zapified(drc->drc_ds)) {
28949c3fd121SMatthew Ahrens 		(void) zap_lookup(drc->drc_ds->ds_dir->dd_pool->dp_meta_objset,
28959c3fd121SMatthew Ahrens 		    drc->drc_ds->ds_object, DS_FIELD_RESUME_BYTES,
28969c3fd121SMatthew Ahrens 		    sizeof (ra.bytes_read), 1, &ra.bytes_read);
28979c3fd121SMatthew Ahrens 	}
28989c3fd121SMatthew Ahrens 
289968ecb2ecSPaul Dagnelie 	objlist_create(&ra.ignore_objlist);
2900efb80947Sahrens 
29013cb34c60Sahrens 	/* these were verified in dmu_recv_begin */
29023b2aab18SMatthew Ahrens 	ASSERT3U(DMU_GET_STREAM_HDRTYPE(drc->drc_drrb->drr_versioninfo), ==,
29039e69d7d0SLori Alt 	    DMU_SUBSTREAM);
29043b2aab18SMatthew Ahrens 	ASSERT3U(drc->drc_drrb->drr_type, <, DMU_OST_NUMTYPES);
2905efb80947Sahrens 
2906efb80947Sahrens 	/*
2907efb80947Sahrens 	 * Open the objset we are modifying.
2908efb80947Sahrens 	 */
290998110f08SMatthew Ahrens 	VERIFY0(dmu_objset_from_ds(drc->drc_ds, &ra.os));
2910efb80947Sahrens 
2911c1379625SJustin T. Gibbs 	ASSERT(dsl_dataset_phys(drc->drc_ds)->ds_flags & DS_FLAG_INCONSISTENT);
2912efb80947Sahrens 
29139e69d7d0SLori Alt 	featureflags = DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo);
29149e69d7d0SLori Alt 
29159e69d7d0SLori Alt 	/* if this stream is dedup'ed, set up the avl tree for guid mapping */
29169e69d7d0SLori Alt 	if (featureflags & DMU_BACKUP_FEATURE_DEDUP) {
2917a7f53a56SChris Kirby 		minor_t minor;
2918a7f53a56SChris Kirby 
2919c99e4bdcSChris Kirby 		if (cleanup_fd == -1) {
2920be6fd75aSMatthew Ahrens 			ra.err = SET_ERROR(EBADF);
2921c99e4bdcSChris Kirby 			goto out;
2922c99e4bdcSChris Kirby 		}
2923a7f53a56SChris Kirby 		ra.err = zfs_onexit_fd_hold(cleanup_fd, &minor);
29243b2aab18SMatthew Ahrens 		if (ra.err != 0) {
2925a7f53a56SChris Kirby 			cleanup_fd = -1;
2926a7f53a56SChris Kirby 			goto out;
2927a7f53a56SChris Kirby 		}
2928a7f53a56SChris Kirby 
2929c99e4bdcSChris Kirby 		if (*action_handlep == 0) {
2930a2cdcdd2SPaul Dagnelie 			rwa.guid_to_ds_map =
2931c99e4bdcSChris Kirby 			    kmem_alloc(sizeof (avl_tree_t), KM_SLEEP);
2932a2cdcdd2SPaul Dagnelie 			avl_create(rwa.guid_to_ds_map, guid_compare,
2933c99e4bdcSChris Kirby 			    sizeof (guid_map_entry_t),
2934c99e4bdcSChris Kirby 			    offsetof(guid_map_entry_t, avlnode));
293598110f08SMatthew Ahrens 			err = zfs_onexit_add_cb(minor,
2936a2cdcdd2SPaul Dagnelie 			    free_guid_map_onexit, rwa.guid_to_ds_map,
2937c99e4bdcSChris Kirby 			    action_handlep);
29383b2aab18SMatthew Ahrens 			if (ra.err != 0)
2939c99e4bdcSChris Kirby 				goto out;
2940c99e4bdcSChris Kirby 		} else {
294198110f08SMatthew Ahrens 			err = zfs_onexit_cb_data(minor, *action_handlep,
2942a2cdcdd2SPaul Dagnelie 			    (void **)&rwa.guid_to_ds_map);
29433b2aab18SMatthew Ahrens 			if (ra.err != 0)
2944c99e4bdcSChris Kirby 				goto out;
2945c99e4bdcSChris Kirby 		}
2946ec5cf9d5SAlexander Stetsenko 
2947a2cdcdd2SPaul Dagnelie 		drc->drc_guid_to_ds_map = rwa.guid_to_ds_map;
29489e69d7d0SLori Alt 	}
29499e69d7d0SLori Alt 
29509c3fd121SMatthew Ahrens 	uint32_t payloadlen = drc->drc_drr_begin->drr_payloadlen;
29519c3fd121SMatthew Ahrens 	void *payload = NULL;
29529c3fd121SMatthew Ahrens 	if (payloadlen != 0)
29539c3fd121SMatthew Ahrens 		payload = kmem_alloc(payloadlen, KM_SLEEP);
29549c3fd121SMatthew Ahrens 
29559c3fd121SMatthew Ahrens 	err = receive_read_payload_and_next_header(&ra, payloadlen, payload);
29569c3fd121SMatthew Ahrens 	if (err != 0) {
29579c3fd121SMatthew Ahrens 		if (payloadlen != 0)
29589c3fd121SMatthew Ahrens 			kmem_free(payload, payloadlen);
295998110f08SMatthew Ahrens 		goto out;
29609c3fd121SMatthew Ahrens 	}
29619c3fd121SMatthew Ahrens 	if (payloadlen != 0) {
29629c3fd121SMatthew Ahrens 		err = nvlist_unpack(payload, payloadlen, &begin_nvl, KM_SLEEP);
29639c3fd121SMatthew Ahrens 		kmem_free(payload, payloadlen);
29649c3fd121SMatthew Ahrens 		if (err != 0)
29659c3fd121SMatthew Ahrens 			goto out;
29669c3fd121SMatthew Ahrens 	}
29679c3fd121SMatthew Ahrens 
29689c3fd121SMatthew Ahrens 	if (featureflags & DMU_BACKUP_FEATURE_RESUMING) {
29699c3fd121SMatthew Ahrens 		err = resume_check(&ra, begin_nvl);
29709c3fd121SMatthew Ahrens 		if (err != 0)
29719c3fd121SMatthew Ahrens 			goto out;
29729c3fd121SMatthew Ahrens 	}
297398110f08SMatthew Ahrens 
2974a2cdcdd2SPaul Dagnelie 	(void) bqueue_init(&rwa.q, zfs_recv_queue_length,
2975a2cdcdd2SPaul Dagnelie 	    offsetof(struct receive_record_arg, node));
2976a2cdcdd2SPaul Dagnelie 	cv_init(&rwa.cv, NULL, CV_DEFAULT, NULL);
2977a2cdcdd2SPaul Dagnelie 	mutex_init(&rwa.mutex, NULL, MUTEX_DEFAULT, NULL);
2978a2cdcdd2SPaul Dagnelie 	rwa.os = ra.os;
2979a2cdcdd2SPaul Dagnelie 	rwa.byteswap = drc->drc_byteswap;
29809c3fd121SMatthew Ahrens 	rwa.resumable = drc->drc_resumable;
2981a2cdcdd2SPaul Dagnelie 
2982a2cdcdd2SPaul Dagnelie 	(void) thread_create(NULL, 0, receive_writer_thread, &rwa, 0, curproc,
2983a2cdcdd2SPaul Dagnelie 	    TS_RUN, minclsyspri);
2984a2cdcdd2SPaul Dagnelie 	/*
2985a2cdcdd2SPaul Dagnelie 	 * We're reading rwa.err without locks, which is safe since we are the
2986a2cdcdd2SPaul Dagnelie 	 * only reader, and the worker thread is the only writer.  It's ok if we
2987a2cdcdd2SPaul Dagnelie 	 * miss a write for an iteration or two of the loop, since the writer
2988a2cdcdd2SPaul Dagnelie 	 * thread will keep freeing records we send it until we send it an eos
2989a2cdcdd2SPaul Dagnelie 	 * marker.
2990a2cdcdd2SPaul Dagnelie 	 *
2991a2cdcdd2SPaul Dagnelie 	 * We can leave this loop in 3 ways:  First, if rwa.err is
2992a2cdcdd2SPaul Dagnelie 	 * non-zero.  In that case, the writer thread will free the rrd we just
2993a2cdcdd2SPaul Dagnelie 	 * pushed.  Second, if  we're interrupted; in that case, either it's the
2994a2cdcdd2SPaul Dagnelie 	 * first loop and ra.rrd was never allocated, or it's later, and ra.rrd
2995a2cdcdd2SPaul Dagnelie 	 * has been handed off to the writer thread who will free it.  Finally,
2996a2cdcdd2SPaul Dagnelie 	 * if receive_read_record fails or we're at the end of the stream, then
2997a2cdcdd2SPaul Dagnelie 	 * we free ra.rrd and exit.
2998a2cdcdd2SPaul Dagnelie 	 */
2999a2cdcdd2SPaul Dagnelie 	while (rwa.err == 0) {
3000efb80947Sahrens 		if (issig(JUSTLOOKING) && issig(FORREAL)) {
300198110f08SMatthew Ahrens 			err = SET_ERROR(EINTR);
300298110f08SMatthew Ahrens 			break;
3003efb80947Sahrens 		}
3004efb80947Sahrens 
3005a2cdcdd2SPaul Dagnelie 		ASSERT3P(ra.rrd, ==, NULL);
3006a2cdcdd2SPaul Dagnelie 		ra.rrd = ra.next_rrd;
3007a2cdcdd2SPaul Dagnelie 		ra.next_rrd = NULL;
3008a2cdcdd2SPaul Dagnelie 		/* Allocates and loads header into ra.next_rrd */
3009a2cdcdd2SPaul Dagnelie 		err = receive_read_record(&ra);
3010efb80947Sahrens 
3011a2cdcdd2SPaul Dagnelie 		if (ra.rrd->header.drr_type == DRR_END || err != 0) {
3012a2cdcdd2SPaul Dagnelie 			kmem_free(ra.rrd, sizeof (*ra.rrd));
3013a2cdcdd2SPaul Dagnelie 			ra.rrd = NULL;
30140a586ceaSMark Shellenbaum 			break;
3015a2cdcdd2SPaul Dagnelie 		}
3016a2cdcdd2SPaul Dagnelie 
3017a2cdcdd2SPaul Dagnelie 		bqueue_enqueue(&rwa.q, ra.rrd,
3018a2cdcdd2SPaul Dagnelie 		    sizeof (struct receive_record_arg) + ra.rrd->payload_size);
3019a2cdcdd2SPaul Dagnelie 		ra.rrd = NULL;
3020a2cdcdd2SPaul Dagnelie 	}
3021a2cdcdd2SPaul Dagnelie 	if (ra.next_rrd == NULL)
3022a2cdcdd2SPaul Dagnelie 		ra.next_rrd = kmem_zalloc(sizeof (*ra.next_rrd), KM_SLEEP);
3023a2cdcdd2SPaul Dagnelie 	ra.next_rrd->eos_marker = B_TRUE;
3024a2cdcdd2SPaul Dagnelie 	bqueue_enqueue(&rwa.q, ra.next_rrd, 1);
3025a2cdcdd2SPaul Dagnelie 
3026a2cdcdd2SPaul Dagnelie 	mutex_enter(&rwa.mutex);
3027a2cdcdd2SPaul Dagnelie 	while (!rwa.done) {
3028a2cdcdd2SPaul Dagnelie 		cv_wait(&rwa.cv, &rwa.mutex);
3029efb80947Sahrens 	}
3030a2cdcdd2SPaul Dagnelie 	mutex_exit(&rwa.mutex);
3031a2cdcdd2SPaul Dagnelie 
3032a2cdcdd2SPaul Dagnelie 	cv_destroy(&rwa.cv);
3033a2cdcdd2SPaul Dagnelie 	mutex_destroy(&rwa.mutex);
3034a2cdcdd2SPaul Dagnelie 	bqueue_destroy(&rwa.q);
3035a2cdcdd2SPaul Dagnelie 	if (err == 0)
3036a2cdcdd2SPaul Dagnelie 		err = rwa.err;
3037efb80947Sahrens 
3038efb80947Sahrens out:
30399c3fd121SMatthew Ahrens 	nvlist_free(begin_nvl);
3040a7f53a56SChris Kirby 	if ((featureflags & DMU_BACKUP_FEATURE_DEDUP) && (cleanup_fd != -1))
3041a7f53a56SChris Kirby 		zfs_onexit_fd_rele(cleanup_fd);
3042a7f53a56SChris Kirby 
304398110f08SMatthew Ahrens 	if (err != 0) {
3044efb80947Sahrens 		/*
30459c3fd121SMatthew Ahrens 		 * Clean up references. If receive is not resumable,
30469c3fd121SMatthew Ahrens 		 * destroy what we created, so we don't leave it in
30479c3fd121SMatthew Ahrens 		 * the inconsistent state.
3048efb80947Sahrens 		 */
30493b2aab18SMatthew Ahrens 		dmu_recv_cleanup_ds(drc);
3050efb80947Sahrens 	}
3051efb80947Sahrens 
30523cb34c60Sahrens 	*voffp = ra.voff;
305368ecb2ecSPaul Dagnelie 	objlist_destroy(&ra.ignore_objlist);
305498110f08SMatthew Ahrens 	return (err);
3055efb80947Sahrens }
3056f18faf3fSek 
30573cb34c60Sahrens static int
30583b2aab18SMatthew Ahrens dmu_recv_end_check(void *arg, dmu_tx_t *tx)
30593cb34c60Sahrens {
30603b2aab18SMatthew Ahrens 	dmu_recv_cookie_t *drc = arg;
30613b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
30623b2aab18SMatthew Ahrens 	int error;
30633b2aab18SMatthew Ahrens 
30643b2aab18SMatthew Ahrens 	ASSERT3P(drc->drc_ds->ds_owner, ==, dmu_recv_tag);
30653cb34c60Sahrens 
30663b2aab18SMatthew Ahrens 	if (!drc->drc_newfs) {
30673b2aab18SMatthew Ahrens 		dsl_dataset_t *origin_head;
30683b2aab18SMatthew Ahrens 
30693b2aab18SMatthew Ahrens 		error = dsl_dataset_hold(dp, drc->drc_tofs, FTAG, &origin_head);
30703b2aab18SMatthew Ahrens 		if (error != 0)
30713b2aab18SMatthew Ahrens 			return (error);
307234f2f8cfSMatthew Ahrens 		if (drc->drc_force) {
307334f2f8cfSMatthew Ahrens 			/*
307434f2f8cfSMatthew Ahrens 			 * We will destroy any snapshots in tofs (i.e. before
307534f2f8cfSMatthew Ahrens 			 * origin_head) that are after the origin (which is
307634f2f8cfSMatthew Ahrens 			 * the snap before drc_ds, because drc_ds can not
307734f2f8cfSMatthew Ahrens 			 * have any snaps of its own).
307834f2f8cfSMatthew Ahrens 			 */
3079c1379625SJustin T. Gibbs 			uint64_t obj;
3080c1379625SJustin T. Gibbs 
3081c1379625SJustin T. Gibbs 			obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
3082c1379625SJustin T. Gibbs 			while (obj !=
3083c1379625SJustin T. Gibbs 			    dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
308434f2f8cfSMatthew Ahrens 				dsl_dataset_t *snap;
308534f2f8cfSMatthew Ahrens 				error = dsl_dataset_hold_obj(dp, obj, FTAG,
308634f2f8cfSMatthew Ahrens 				    &snap);
308734f2f8cfSMatthew Ahrens 				if (error != 0)
3088beddaa9cSAndriy Gapon 					break;
308934f2f8cfSMatthew Ahrens 				if (snap->ds_dir != origin_head->ds_dir)
309034f2f8cfSMatthew Ahrens 					error = SET_ERROR(EINVAL);
309134f2f8cfSMatthew Ahrens 				if (error == 0)  {
309234f2f8cfSMatthew Ahrens 					error = dsl_destroy_snapshot_check_impl(
309334f2f8cfSMatthew Ahrens 					    snap, B_FALSE);
309434f2f8cfSMatthew Ahrens 				}
3095c1379625SJustin T. Gibbs 				obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
309634f2f8cfSMatthew Ahrens 				dsl_dataset_rele(snap, FTAG);
309734f2f8cfSMatthew Ahrens 				if (error != 0)
3098beddaa9cSAndriy Gapon 					break;
3099beddaa9cSAndriy Gapon 			}
3100beddaa9cSAndriy Gapon 			if (error != 0) {
3101beddaa9cSAndriy Gapon 				dsl_dataset_rele(origin_head, FTAG);
3102beddaa9cSAndriy Gapon 				return (error);
310334f2f8cfSMatthew Ahrens 			}
310434f2f8cfSMatthew Ahrens 		}
31053b2aab18SMatthew Ahrens 		error = dsl_dataset_clone_swap_check_impl(drc->drc_ds,
310691948b51SKeith M Wesolowski 		    origin_head, drc->drc_force, drc->drc_owner, tx);
31073b2aab18SMatthew Ahrens 		if (error != 0) {
31083b2aab18SMatthew Ahrens 			dsl_dataset_rele(origin_head, FTAG);
31093b2aab18SMatthew Ahrens 			return (error);
31103b2aab18SMatthew Ahrens 		}
31113b2aab18SMatthew Ahrens 		error = dsl_dataset_snapshot_check_impl(origin_head,
3112a2afb611SJerry Jelinek 		    drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred);
31133b2aab18SMatthew Ahrens 		dsl_dataset_rele(origin_head, FTAG);
31143b2aab18SMatthew Ahrens 		if (error != 0)
31153b2aab18SMatthew Ahrens 			return (error);
31163b2aab18SMatthew Ahrens 
31173b2aab18SMatthew Ahrens 		error = dsl_destroy_head_check_impl(drc->drc_ds, 1);
31183b2aab18SMatthew Ahrens 	} else {
31193b2aab18SMatthew Ahrens 		error = dsl_dataset_snapshot_check_impl(drc->drc_ds,
3120a2afb611SJerry Jelinek 		    drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred);
31213b2aab18SMatthew Ahrens 	}
31223b2aab18SMatthew Ahrens 	return (error);
31233cb34c60Sahrens }
31243cb34c60Sahrens 
31253cb34c60Sahrens static void
31263b2aab18SMatthew Ahrens dmu_recv_end_sync(void *arg, dmu_tx_t *tx)
31273cb34c60Sahrens {
31283b2aab18SMatthew Ahrens 	dmu_recv_cookie_t *drc = arg;
31293b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
31303b2aab18SMatthew Ahrens 
31313b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(drc->drc_ds, "finish receiving",
31323b2aab18SMatthew Ahrens 	    tx, "snap=%s", drc->drc_tosnap);
31333b2aab18SMatthew Ahrens 
31343b2aab18SMatthew Ahrens 	if (!drc->drc_newfs) {
31353b2aab18SMatthew Ahrens 		dsl_dataset_t *origin_head;
31363b2aab18SMatthew Ahrens 
31373b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold(dp, drc->drc_tofs, FTAG,
31383b2aab18SMatthew Ahrens 		    &origin_head));
313934f2f8cfSMatthew Ahrens 
314034f2f8cfSMatthew Ahrens 		if (drc->drc_force) {
314134f2f8cfSMatthew Ahrens 			/*
314234f2f8cfSMatthew Ahrens 			 * Destroy any snapshots of drc_tofs (origin_head)
314334f2f8cfSMatthew Ahrens 			 * after the origin (the snap before drc_ds).
314434f2f8cfSMatthew Ahrens 			 */
3145c1379625SJustin T. Gibbs 			uint64_t obj;
3146c1379625SJustin T. Gibbs 
3147c1379625SJustin T. Gibbs 			obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
3148c1379625SJustin T. Gibbs 			while (obj !=
3149c1379625SJustin T. Gibbs 			    dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
315034f2f8cfSMatthew Ahrens 				dsl_dataset_t *snap;
315134f2f8cfSMatthew Ahrens 				VERIFY0(dsl_dataset_hold_obj(dp, obj, FTAG,
315234f2f8cfSMatthew Ahrens 				    &snap));
315334f2f8cfSMatthew Ahrens 				ASSERT3P(snap->ds_dir, ==, origin_head->ds_dir);
3154c1379625SJustin T. Gibbs 				obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
315534f2f8cfSMatthew Ahrens 				dsl_destroy_snapshot_sync_impl(snap,
315634f2f8cfSMatthew Ahrens 				    B_FALSE, tx);
315734f2f8cfSMatthew Ahrens 				dsl_dataset_rele(snap, FTAG);
315834f2f8cfSMatthew Ahrens 			}
315934f2f8cfSMatthew Ahrens 		}
316034f2f8cfSMatthew Ahrens 		VERIFY3P(drc->drc_ds->ds_prev, ==,
316134f2f8cfSMatthew Ahrens 		    origin_head->ds_prev);
316234f2f8cfSMatthew Ahrens 
31633b2aab18SMatthew Ahrens 		dsl_dataset_clone_swap_sync_impl(drc->drc_ds,
31643b2aab18SMatthew Ahrens 		    origin_head, tx);
31653b2aab18SMatthew Ahrens 		dsl_dataset_snapshot_sync_impl(origin_head,
31663b2aab18SMatthew Ahrens 		    drc->drc_tosnap, tx);
31673b2aab18SMatthew Ahrens 
31683b2aab18SMatthew Ahrens 		/* set snapshot's creation time and guid */
31693b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(origin_head->ds_prev->ds_dbuf, tx);
3170c1379625SJustin T. Gibbs 		dsl_dataset_phys(origin_head->ds_prev)->ds_creation_time =
31713b2aab18SMatthew Ahrens 		    drc->drc_drrb->drr_creation_time;
3172c1379625SJustin T. Gibbs 		dsl_dataset_phys(origin_head->ds_prev)->ds_guid =
31733b2aab18SMatthew Ahrens 		    drc->drc_drrb->drr_toguid;
3174c1379625SJustin T. Gibbs 		dsl_dataset_phys(origin_head->ds_prev)->ds_flags &=
31753b2aab18SMatthew Ahrens 		    ~DS_FLAG_INCONSISTENT;
31763b2aab18SMatthew Ahrens 
31773b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
3178c1379625SJustin T. Gibbs 		dsl_dataset_phys(origin_head)->ds_flags &=
3179c1379625SJustin T. Gibbs 		    ~DS_FLAG_INCONSISTENT;
31803b2aab18SMatthew Ahrens 
31812ad25b40SChris Williamson 		drc->drc_newsnapobj =
31822ad25b40SChris Williamson 		    dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
31832ad25b40SChris Williamson 
31843b2aab18SMatthew Ahrens 		dsl_dataset_rele(origin_head, FTAG);
31853b2aab18SMatthew Ahrens 		dsl_destroy_head_sync_impl(drc->drc_ds, tx);
318691948b51SKeith M Wesolowski 
318791948b51SKeith M Wesolowski 		if (drc->drc_owner != NULL)
318891948b51SKeith M Wesolowski 			VERIFY3P(origin_head->ds_owner, ==, drc->drc_owner);
31893b2aab18SMatthew Ahrens 	} else {
31903b2aab18SMatthew Ahrens 		dsl_dataset_t *ds = drc->drc_ds;
31913cb34c60Sahrens 
31923b2aab18SMatthew Ahrens 		dsl_dataset_snapshot_sync_impl(ds, drc->drc_tosnap, tx);
31933cb34c60Sahrens 
31943b2aab18SMatthew Ahrens 		/* set snapshot's creation time and guid */
31953b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
3196c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds->ds_prev)->ds_creation_time =
31973b2aab18SMatthew Ahrens 		    drc->drc_drrb->drr_creation_time;
3198c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds->ds_prev)->ds_guid =
3199c1379625SJustin T. Gibbs 		    drc->drc_drrb->drr_toguid;
3200c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds->ds_prev)->ds_flags &=
3201c1379625SJustin T. Gibbs 		    ~DS_FLAG_INCONSISTENT;
32023cb34c60Sahrens 
32033b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
3204c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds)->ds_flags &= ~DS_FLAG_INCONSISTENT;
32059c3fd121SMatthew Ahrens 		if (dsl_dataset_has_resume_receive_state(ds)) {
32069c3fd121SMatthew Ahrens 			(void) zap_remove(dp->dp_meta_objset, ds->ds_object,
32079c3fd121SMatthew Ahrens 			    DS_FIELD_RESUME_FROMGUID, tx);
32089c3fd121SMatthew Ahrens 			(void) zap_remove(dp->dp_meta_objset, ds->ds_object,
32099c3fd121SMatthew Ahrens 			    DS_FIELD_RESUME_OBJECT, tx);
32109c3fd121SMatthew Ahrens 			(void) zap_remove(dp->dp_meta_objset, ds->ds_object,
32119c3fd121SMatthew Ahrens 			    DS_FIELD_RESUME_OFFSET, tx);
32129c3fd121SMatthew Ahrens 			(void) zap_remove(dp->dp_meta_objset, ds->ds_object,
32139c3fd121SMatthew Ahrens 			    DS_FIELD_RESUME_BYTES, tx);
32149c3fd121SMatthew Ahrens 			(void) zap_remove(dp->dp_meta_objset, ds->ds_object,
32159c3fd121SMatthew Ahrens 			    DS_FIELD_RESUME_TOGUID, tx);
32169c3fd121SMatthew Ahrens 			(void) zap_remove(dp->dp_meta_objset, ds->ds_object,
32179c3fd121SMatthew Ahrens 			    DS_FIELD_RESUME_TONAME, tx);
32189c3fd121SMatthew Ahrens 		}
32192ad25b40SChris Williamson 		drc->drc_newsnapobj =
32202ad25b40SChris Williamson 		    dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj;
32213b2aab18SMatthew Ahrens 	}
32223b2aab18SMatthew Ahrens 	/*
32233b2aab18SMatthew Ahrens 	 * Release the hold from dmu_recv_begin.  This must be done before
32243b2aab18SMatthew Ahrens 	 * we return to open context, so that when we free the dataset's dnode,
32253b2aab18SMatthew Ahrens 	 * we can evict its bonus buffer.
32263b2aab18SMatthew Ahrens 	 */
32273b2aab18SMatthew Ahrens 	dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
32283b2aab18SMatthew Ahrens 	drc->drc_ds = NULL;
32293cb34c60Sahrens }
32303cb34c60Sahrens 
3231ec5cf9d5SAlexander Stetsenko static int
32323b2aab18SMatthew Ahrens add_ds_to_guidmap(const char *name, avl_tree_t *guid_map, uint64_t snapobj)
3233ec5cf9d5SAlexander Stetsenko {
32343b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
3235ec5cf9d5SAlexander Stetsenko 	dsl_dataset_t *snapds;
3236ec5cf9d5SAlexander Stetsenko 	guid_map_entry_t *gmep;
3237ec5cf9d5SAlexander Stetsenko 	int err;
3238ec5cf9d5SAlexander Stetsenko 
3239ec5cf9d5SAlexander Stetsenko 	ASSERT(guid_map != NULL);
3240ec5cf9d5SAlexander Stetsenko 
32413b2aab18SMatthew Ahrens 	err = dsl_pool_hold(name, FTAG, &dp);
32423b2aab18SMatthew Ahrens 	if (err != 0)
32433b2aab18SMatthew Ahrens 		return (err);
3244de8d9cffSMatthew Ahrens 	gmep = kmem_alloc(sizeof (*gmep), KM_SLEEP);
3245de8d9cffSMatthew Ahrens 	err = dsl_dataset_hold_obj(dp, snapobj, gmep, &snapds);
3246ec5cf9d5SAlexander Stetsenko 	if (err == 0) {
3247c1379625SJustin T. Gibbs 		gmep->guid = dsl_dataset_phys(snapds)->ds_guid;
3248ec5cf9d5SAlexander Stetsenko 		gmep->gme_ds = snapds;
3249ec5cf9d5SAlexander Stetsenko 		avl_add(guid_map, gmep);
32503b2aab18SMatthew Ahrens 		dsl_dataset_long_hold(snapds, gmep);
3251de8d9cffSMatthew Ahrens 	} else {
3252de8d9cffSMatthew Ahrens 		kmem_free(gmep, sizeof (*gmep));
3253ec5cf9d5SAlexander Stetsenko 	}
3254ec5cf9d5SAlexander Stetsenko 
32553b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
3256ec5cf9d5SAlexander Stetsenko 	return (err);
3257ec5cf9d5SAlexander Stetsenko }
3258ec5cf9d5SAlexander Stetsenko 
32593b2aab18SMatthew Ahrens static int dmu_recv_end_modified_blocks = 3;
32603b2aab18SMatthew Ahrens 
3261ae46e4c7SMatthew Ahrens static int
3262ae46e4c7SMatthew Ahrens dmu_recv_existing_end(dmu_recv_cookie_t *drc)
3263f18faf3fSek {
32643b2aab18SMatthew Ahrens #ifdef _KERNEL
32653b2aab18SMatthew Ahrens 	/*
32663b2aab18SMatthew Ahrens 	 * We will be destroying the ds; make sure its origin is unmounted if
32673b2aab18SMatthew Ahrens 	 * necessary.
32683b2aab18SMatthew Ahrens 	 */
32699adfa60dSMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
32703b2aab18SMatthew Ahrens 	dsl_dataset_name(drc->drc_ds, name);
32713b2aab18SMatthew Ahrens 	zfs_destroy_unmount_origin(name);
32723b2aab18SMatthew Ahrens #endif
32733cb34c60Sahrens 
32742ad25b40SChris Williamson 	return (dsl_sync_task(drc->drc_tofs,
32753b2aab18SMatthew Ahrens 	    dmu_recv_end_check, dmu_recv_end_sync, drc,
32762ad25b40SChris Williamson 	    dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL));
3277f18faf3fSek }
3278ae46e4c7SMatthew Ahrens 
3279ae46e4c7SMatthew Ahrens static int
3280ae46e4c7SMatthew Ahrens dmu_recv_new_end(dmu_recv_cookie_t *drc)
32812ad25b40SChris Williamson {
32822ad25b40SChris Williamson 	return (dsl_sync_task(drc->drc_tofs,
32832ad25b40SChris Williamson 	    dmu_recv_end_check, dmu_recv_end_sync, drc,
32842ad25b40SChris Williamson 	    dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL));
32852ad25b40SChris Williamson }
32862ad25b40SChris Williamson 
32872ad25b40SChris Williamson int
32882ad25b40SChris Williamson dmu_recv_end(dmu_recv_cookie_t *drc, void *owner)
3289ae46e4c7SMatthew Ahrens {
32903b2aab18SMatthew Ahrens 	int error;
3291ae46e4c7SMatthew Ahrens 
32922ad25b40SChris Williamson 	drc->drc_owner = owner;
32932ad25b40SChris Williamson 
32942ad25b40SChris Williamson 	if (drc->drc_newfs)
32952ad25b40SChris Williamson 		error = dmu_recv_new_end(drc);
32962ad25b40SChris Williamson 	else
32972ad25b40SChris Williamson 		error = dmu_recv_existing_end(drc);
3298ae46e4c7SMatthew Ahrens 
32993b2aab18SMatthew Ahrens 	if (error != 0) {
33003b2aab18SMatthew Ahrens 		dmu_recv_cleanup_ds(drc);
33013b2aab18SMatthew Ahrens 	} else if (drc->drc_guid_to_ds_map != NULL) {
33023b2aab18SMatthew Ahrens 		(void) add_ds_to_guidmap(drc->drc_tofs,
33033b2aab18SMatthew Ahrens 		    drc->drc_guid_to_ds_map,
33043b2aab18SMatthew Ahrens 		    drc->drc_newsnapobj);
3305ae46e4c7SMatthew Ahrens 	}
33063b2aab18SMatthew Ahrens 	return (error);
3307ae46e4c7SMatthew Ahrens }
3308ae46e4c7SMatthew Ahrens 
33092f3d8780SMatthew Ahrens /*
33102f3d8780SMatthew Ahrens  * Return TRUE if this objset is currently being received into.
33112f3d8780SMatthew Ahrens  */
33122f3d8780SMatthew Ahrens boolean_t
33132f3d8780SMatthew Ahrens dmu_objset_is_receiving(objset_t *os)
33142f3d8780SMatthew Ahrens {
33152f3d8780SMatthew Ahrens 	return (os->os_dsl_dataset != NULL &&
33162f3d8780SMatthew Ahrens 	    os->os_dsl_dataset->ds_owner == dmu_recv_tag);
33172f3d8780SMatthew Ahrens }
3318