xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_send.c (revision ef96fc31)
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;
616ccda740Sloli int zfs_send_queue_length = SPA_MAXBLOCKSIZE;
62880094b6SAndrew Stormont /* Set this tunable to FALSE to disable setting of DRR_FLAG_FREERECORDS */
63880094b6SAndrew Stormont int zfs_send_set_freerecords_bit = B_TRUE;
64eb633035STom Caputi /* Set this tunable to FALSE is disable sending unmodified spill blocks. */
65eb633035STom Caputi int zfs_send_unmodified_spill_blocks = B_TRUE;
6619b94df9SMatthew Ahrens 
67df477c0aSPaul Dagnelie /*
68df477c0aSPaul Dagnelie  * Use this to override the recordsize calculation for fast zfs send estimates.
69df477c0aSPaul Dagnelie  */
70df477c0aSPaul Dagnelie uint64_t zfs_override_estimate_recordsize = 0;
71df477c0aSPaul Dagnelie 
72a2cdcdd2SPaul Dagnelie #define	BP_SPAN(datablkszsec, indblkshift, level) \
73a2cdcdd2SPaul Dagnelie 	(((uint64_t)datablkszsec) << (SPA_MINBLOCKSHIFT + \
74a2cdcdd2SPaul Dagnelie 	(level) * (indblkshift - SPA_BLKPTRSHIFT)))
75a2cdcdd2SPaul Dagnelie 
76a2cdcdd2SPaul Dagnelie struct send_thread_arg {
77a2cdcdd2SPaul Dagnelie 	bqueue_t	q;
78a2cdcdd2SPaul Dagnelie 	dsl_dataset_t	*ds;		/* Dataset to traverse */
79a2cdcdd2SPaul Dagnelie 	uint64_t	fromtxg;	/* Traverse from this txg */
80a2cdcdd2SPaul Dagnelie 	int		flags;		/* flags to pass to traverse_dataset */
81a2cdcdd2SPaul Dagnelie 	int		error_code;
82a2cdcdd2SPaul Dagnelie 	boolean_t	cancel;
839c3fd121SMatthew Ahrens 	zbookmark_phys_t resume;
84a2cdcdd2SPaul Dagnelie };
85a2cdcdd2SPaul Dagnelie 
86a2cdcdd2SPaul Dagnelie struct send_block_record {
87a2cdcdd2SPaul Dagnelie 	boolean_t		eos_marker; /* Marks the end of the stream */
88a2cdcdd2SPaul Dagnelie 	blkptr_t		bp;
89a2cdcdd2SPaul Dagnelie 	zbookmark_phys_t	zb;
90a2cdcdd2SPaul Dagnelie 	uint8_t			indblkshift;
91a2cdcdd2SPaul Dagnelie 	uint16_t		datablkszsec;
92a2cdcdd2SPaul Dagnelie 	bqueue_node_t		ln;
93a2cdcdd2SPaul Dagnelie };
94a2cdcdd2SPaul Dagnelie 
95eb633035STom Caputi static int do_dump(dmu_sendarg_t *dsa, struct send_block_record *data);
96eb633035STom Caputi 
97efb80947Sahrens static int
dump_bytes(dmu_sendarg_t * dsp,void * buf,int len)984e3c9f44SBill Pijewski dump_bytes(dmu_sendarg_t *dsp, void *buf, int len)
99efb80947Sahrens {
1009c3fd121SMatthew Ahrens 	dsl_dataset_t *ds = dmu_objset_ds(dsp->dsa_os);
101efb80947Sahrens 	ssize_t resid; /* have to get resid to get detailed errno */
102c20404ffSEli Rosenthal 
103c20404ffSEli Rosenthal 	/*
104eb633035STom Caputi 	 * The code does not rely on len being a multiple of 8.  We keep
105c20404ffSEli Rosenthal 	 * this assertion because of the corresponding assertion in
106c20404ffSEli Rosenthal 	 * receive_read().  Keeping this assertion ensures that we do not
107c20404ffSEli Rosenthal 	 * inadvertently break backwards compatibility (causing the assertion
108eb633035STom Caputi 	 * in receive_read() to trigger on old software). Newer feature flags
109eb633035STom Caputi 	 * (such as raw send) may break this assertion since they were
110eb633035STom Caputi 	 * introduced after the requirement was made obsolete.
111c20404ffSEli Rosenthal 	 */
112c20404ffSEli Rosenthal 
113eb633035STom Caputi 	ASSERT(len % 8 == 0 ||
114eb633035STom Caputi 	    (dsp->dsa_featureflags & DMU_BACKUP_FEATURE_RAW) != 0);
115efb80947Sahrens 
1164e3c9f44SBill Pijewski 	dsp->dsa_err = vn_rdwr(UIO_WRITE, dsp->dsa_vp,
117efb80947Sahrens 	    (caddr_t)buf, len,
118efb80947Sahrens 	    0, UIO_SYSSPACE, FAPPEND, RLIM64_INFINITY, CRED(), &resid);
1194e3c9f44SBill Pijewski 
1204e3c9f44SBill Pijewski 	mutex_enter(&ds->ds_sendstream_lock);
1214e3c9f44SBill Pijewski 	*dsp->dsa_off += len;
1224e3c9f44SBill Pijewski 	mutex_exit(&ds->ds_sendstream_lock);
1234e3c9f44SBill Pijewski 
1244e3c9f44SBill Pijewski 	return (dsp->dsa_err);
125efb80947Sahrens }
126efb80947Sahrens 
12798110f08SMatthew Ahrens /*
12898110f08SMatthew Ahrens  * For all record types except BEGIN, fill in the checksum (overlaid in
12998110f08SMatthew Ahrens  * drr_u.drr_checksum.drr_checksum).  The checksum verifies everything
13098110f08SMatthew Ahrens  * up to the start of the checksum itself.
13198110f08SMatthew Ahrens  */
13298110f08SMatthew Ahrens static int
dump_record(dmu_sendarg_t * dsp,void * payload,int payload_len)13398110f08SMatthew Ahrens dump_record(dmu_sendarg_t *dsp, void *payload, int payload_len)
13498110f08SMatthew Ahrens {
13598110f08SMatthew Ahrens 	ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
13698110f08SMatthew Ahrens 	    ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
137770499e1SDan Kimmel 	(void) fletcher_4_incremental_native(dsp->dsa_drr,
13898110f08SMatthew Ahrens 	    offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
13998110f08SMatthew Ahrens 	    &dsp->dsa_zc);
14012b90ee2SMatt Krantz 	if (dsp->dsa_drr->drr_type == DRR_BEGIN) {
14112b90ee2SMatt Krantz 		dsp->dsa_sent_begin = B_TRUE;
14212b90ee2SMatt Krantz 	} else {
14398110f08SMatthew Ahrens 		ASSERT(ZIO_CHECKSUM_IS_ZERO(&dsp->dsa_drr->drr_u.
14498110f08SMatthew Ahrens 		    drr_checksum.drr_checksum));
14598110f08SMatthew Ahrens 		dsp->dsa_drr->drr_u.drr_checksum.drr_checksum = dsp->dsa_zc;
14698110f08SMatthew Ahrens 	}
14712b90ee2SMatt Krantz 	if (dsp->dsa_drr->drr_type == DRR_END) {
14812b90ee2SMatt Krantz 		dsp->dsa_sent_end = B_TRUE;
14912b90ee2SMatt Krantz 	}
150770499e1SDan Kimmel 	(void) fletcher_4_incremental_native(&dsp->dsa_drr->
15198110f08SMatthew Ahrens 	    drr_u.drr_checksum.drr_checksum,
15298110f08SMatthew Ahrens 	    sizeof (zio_cksum_t), &dsp->dsa_zc);
15398110f08SMatthew Ahrens 	if (dump_bytes(dsp, dsp->dsa_drr, sizeof (dmu_replay_record_t)) != 0)
15498110f08SMatthew Ahrens 		return (SET_ERROR(EINTR));
15598110f08SMatthew Ahrens 	if (payload_len != 0) {
156770499e1SDan Kimmel 		(void) fletcher_4_incremental_native(payload, payload_len,
15798110f08SMatthew Ahrens 		    &dsp->dsa_zc);
15898110f08SMatthew Ahrens 		if (dump_bytes(dsp, payload, payload_len) != 0)
15998110f08SMatthew Ahrens 			return (SET_ERROR(EINTR));
16098110f08SMatthew Ahrens 	}
16198110f08SMatthew Ahrens 	return (0);
16298110f08SMatthew Ahrens }
16398110f08SMatthew Ahrens 
16468ecb2ecSPaul Dagnelie /*
16568ecb2ecSPaul Dagnelie  * Fill in the drr_free struct, or perform aggregation if the previous record is
16668ecb2ecSPaul Dagnelie  * also a free record, and the two are adjacent.
16768ecb2ecSPaul Dagnelie  *
16868ecb2ecSPaul Dagnelie  * Note that we send free records even for a full send, because we want to be
16968ecb2ecSPaul Dagnelie  * able to receive a full send as a clone, which requires a list of all the free
17068ecb2ecSPaul Dagnelie  * and freeobject records that were generated on the source.
17168ecb2ecSPaul Dagnelie  */
172efb80947Sahrens static int
dump_free(dmu_sendarg_t * dsp,uint64_t object,uint64_t offset,uint64_t length)1734e3c9f44SBill Pijewski dump_free(dmu_sendarg_t *dsp, uint64_t object, uint64_t offset,
174efb80947Sahrens     uint64_t length)
175efb80947Sahrens {
1764e3c9f44SBill Pijewski 	struct drr_free *drrf = &(dsp->dsa_drr->drr_u.drr_free);
1779e69d7d0SLori Alt 
1782f3d8780SMatthew Ahrens 	/*
1792f3d8780SMatthew Ahrens 	 * When we receive a free record, dbuf_free_range() assumes
1802f3d8780SMatthew Ahrens 	 * that the receiving system doesn't have any dbufs in the range
1812f3d8780SMatthew Ahrens 	 * being freed.  This is always true because there is a one-record
1822f3d8780SMatthew Ahrens 	 * constraint: we only send one WRITE record for any given
1839c3fd121SMatthew Ahrens 	 * object,offset.  We know that the one-record constraint is
1842f3d8780SMatthew Ahrens 	 * true because we always send data in increasing order by
1852f3d8780SMatthew Ahrens 	 * object,offset.
1862f3d8780SMatthew Ahrens 	 *
1872f3d8780SMatthew Ahrens 	 * If the increasing-order constraint ever changes, we should find
1882f3d8780SMatthew Ahrens 	 * another way to assert that the one-record constraint is still
1892f3d8780SMatthew Ahrens 	 * satisfied.
1902f3d8780SMatthew Ahrens 	 */
1912f3d8780SMatthew Ahrens 	ASSERT(object > dsp->dsa_last_data_object ||
1922f3d8780SMatthew Ahrens 	    (object == dsp->dsa_last_data_object &&
1932f3d8780SMatthew Ahrens 	    offset > dsp->dsa_last_data_offset));
1942f3d8780SMatthew Ahrens 
1959e69d7d0SLori Alt 	/*
1969e69d7d0SLori Alt 	 * If there is a pending op, but it's not PENDING_FREE, push it out,
1979e69d7d0SLori Alt 	 * since free block aggregation can only be done for blocks of the
1989e69d7d0SLori Alt 	 * same type (i.e., DRR_FREE records can only be aggregated with
1999e69d7d0SLori Alt 	 * other DRR_FREE records.  DRR_FREEOBJECTS records can only be
2009e69d7d0SLori Alt 	 * aggregated with other DRR_FREEOBJECTS records.
2019e69d7d0SLori Alt 	 */
2024e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE &&
2034e3c9f44SBill Pijewski 	    dsp->dsa_pending_op != PENDING_FREE) {
20498110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
205be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
2064e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
2079e69d7d0SLori Alt 	}
2089e69d7d0SLori Alt 
2094e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op == PENDING_FREE) {
2109e69d7d0SLori Alt 		/*
211eb633035STom Caputi 		 * There should never be a PENDING_FREE if length is
212eb633035STom Caputi 		 * DMU_OBJECT_END (because dump_dnode is the only place where
213eb633035STom Caputi 		 * this function is called with a DMU_OBJECT_END, and only after
214eb633035STom Caputi 		 * flushing any pending record).
2159e69d7d0SLori Alt 		 */
216eb633035STom Caputi 		ASSERT(length != DMU_OBJECT_END);
2179e69d7d0SLori Alt 		/*
2189e69d7d0SLori Alt 		 * Check to see whether this free block can be aggregated
2199e69d7d0SLori Alt 		 * with pending one.
2209e69d7d0SLori Alt 		 */
2219e69d7d0SLori Alt 		if (drrf->drr_object == object && drrf->drr_offset +
2229e69d7d0SLori Alt 		    drrf->drr_length == offset) {
223eb633035STom Caputi 			if (offset + length < offset)
224eb633035STom Caputi 				drrf->drr_length = DMU_OBJECT_END;
225eb633035STom Caputi 			else
226eb633035STom Caputi 				drrf->drr_length += length;
2279e69d7d0SLori Alt 			return (0);
2289e69d7d0SLori Alt 		} else {
2299e69d7d0SLori Alt 			/* not a continuation.  Push out pending record */
23098110f08SMatthew Ahrens 			if (dump_record(dsp, NULL, 0) != 0)
231be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINTR));
2324e3c9f44SBill Pijewski 			dsp->dsa_pending_op = PENDING_NONE;
2339e69d7d0SLori Alt 		}
2349e69d7d0SLori Alt 	}
2359e69d7d0SLori Alt 	/* create a FREE record and make it pending */
2364e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
2374e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_FREE;
2389e69d7d0SLori Alt 	drrf->drr_object = object;
2399e69d7d0SLori Alt 	drrf->drr_offset = offset;
240eb633035STom Caputi 	if (offset + length < offset)
241eb633035STom Caputi 		drrf->drr_length = DMU_OBJECT_END;
242eb633035STom Caputi 	else
243eb633035STom Caputi 		drrf->drr_length = length;
2444e3c9f44SBill Pijewski 	drrf->drr_toguid = dsp->dsa_toguid;
245eb633035STom Caputi 	if (length == DMU_OBJECT_END) {
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
dump_write(dmu_sendarg_t * dsp,dmu_object_type_t type,uint64_t object,uint64_t offset,int lsize,int psize,const blkptr_t * bp,void * data)256eb633035STom Caputi dump_write(dmu_sendarg_t *dsp, dmu_object_type_t type, uint64_t object,
257eb633035STom Caputi     uint64_t offset, int lsize, int psize, const blkptr_t *bp, void *data)
258efb80947Sahrens {
2595602294fSDan Kimmel 	uint64_t payload_size;
260eb633035STom Caputi 	boolean_t raw = (dsp->dsa_featureflags & DMU_BACKUP_FEATURE_RAW);
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 
293eb633035STom Caputi 	/* only set the compression fields if the buf is compressed or raw */
294eb633035STom Caputi 	if (raw || lsize != psize) {
2955602294fSDan Kimmel 		ASSERT(!BP_IS_EMBEDDED(bp));
2965602294fSDan Kimmel 		ASSERT3S(psize, >, 0);
2975602294fSDan Kimmel 
298eb633035STom Caputi 		if (raw) {
299eb633035STom Caputi 			ASSERT(BP_IS_PROTECTED(bp));
300eb633035STom Caputi 
301eb633035STom Caputi 			/*
302eb633035STom Caputi 			 * This is a raw protected block so we need to pass
303eb633035STom Caputi 			 * along everything the receiving side will need to
304eb633035STom Caputi 			 * interpret this block, including the byteswap, salt,
305eb633035STom Caputi 			 * IV, and MAC.
306eb633035STom Caputi 			 */
307eb633035STom Caputi 			if (BP_SHOULD_BYTESWAP(bp))
308eb633035STom Caputi 				drrw->drr_flags |= DRR_RAW_BYTESWAP;
309eb633035STom Caputi 			zio_crypt_decode_params_bp(bp, drrw->drr_salt,
310eb633035STom Caputi 			    drrw->drr_iv);
311eb633035STom Caputi 			zio_crypt_decode_mac_bp(bp, drrw->drr_mac);
312eb633035STom Caputi 		} else {
313eb633035STom Caputi 			/* this is a compressed block */
314eb633035STom Caputi 			ASSERT(dsp->dsa_featureflags &
315eb633035STom Caputi 			    DMU_BACKUP_FEATURE_COMPRESSED);
316eb633035STom Caputi 			ASSERT(!BP_SHOULD_BYTESWAP(bp));
317eb633035STom Caputi 			ASSERT(!DMU_OT_IS_METADATA(BP_GET_TYPE(bp)));
318eb633035STom Caputi 			ASSERT3U(BP_GET_COMPRESS(bp), !=, ZIO_COMPRESS_OFF);
319eb633035STom Caputi 			ASSERT3S(lsize, >=, psize);
320eb633035STom Caputi 		}
321eb633035STom Caputi 
322eb633035STom Caputi 		/* set fields common to compressed and raw sends */
3235602294fSDan Kimmel 		drrw->drr_compressiontype = BP_GET_COMPRESS(bp);
3245602294fSDan Kimmel 		drrw->drr_compressed_size = psize;
3255602294fSDan Kimmel 		payload_size = drrw->drr_compressed_size;
3265602294fSDan Kimmel 	} else {
3275602294fSDan Kimmel 		payload_size = drrw->drr_logical_size;
3285602294fSDan Kimmel 	}
3295602294fSDan Kimmel 
330eb633035STom Caputi 	if (bp == NULL || BP_IS_EMBEDDED(bp) || (BP_IS_PROTECTED(bp) && !raw)) {
3315d7b4d43SMatthew Ahrens 		/*
332eb633035STom Caputi 		 * There's no pre-computed checksum for partial-block writes,
333eb633035STom Caputi 		 * embedded BP's, or encrypted BP's that are being sent as
334eb633035STom Caputi 		 * plaintext, so (like fletcher4-checkummed blocks) userland
335eb633035STom Caputi 		 * will have to compute a dedup-capable checksum itself.
3365d7b4d43SMatthew Ahrens 		 */
3375d7b4d43SMatthew Ahrens 		drrw->drr_checksumtype = ZIO_CHECKSUM_OFF;
3385d7b4d43SMatthew Ahrens 	} else {
3395d7b4d43SMatthew Ahrens 		drrw->drr_checksumtype = BP_GET_CHECKSUM(bp);
34045818ee1SMatthew Ahrens 		if (zio_checksum_table[drrw->drr_checksumtype].ci_flags &
34145818ee1SMatthew Ahrens 		    ZCHECKSUM_FLAG_DEDUP)
342eb633035STom Caputi 			drrw->drr_flags |= DRR_CHECKSUM_DEDUP;
3435d7b4d43SMatthew Ahrens 		DDK_SET_LSIZE(&drrw->drr_key, BP_GET_LSIZE(bp));
3445d7b4d43SMatthew Ahrens 		DDK_SET_PSIZE(&drrw->drr_key, BP_GET_PSIZE(bp));
3455d7b4d43SMatthew Ahrens 		DDK_SET_COMPRESS(&drrw->drr_key, BP_GET_COMPRESS(bp));
346eb633035STom Caputi 		DDK_SET_CRYPT(&drrw->drr_key, BP_IS_PROTECTED(bp));
3475d7b4d43SMatthew Ahrens 		drrw->drr_key.ddk_cksum = bp->blk_cksum;
3485d7b4d43SMatthew Ahrens 	}
349efb80947Sahrens 
3505602294fSDan Kimmel 	if (dump_record(dsp, data, payload_size) != 0)
351be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
352efb80947Sahrens 	return (0);
353efb80947Sahrens }
354efb80947Sahrens 
3555d7b4d43SMatthew Ahrens static int
dump_write_embedded(dmu_sendarg_t * dsp,uint64_t object,uint64_t offset,int blksz,const blkptr_t * bp)3565d7b4d43SMatthew Ahrens dump_write_embedded(dmu_sendarg_t *dsp, uint64_t object, uint64_t offset,
3575d7b4d43SMatthew Ahrens     int blksz, const blkptr_t *bp)
3585d7b4d43SMatthew Ahrens {
3595d7b4d43SMatthew Ahrens 	char buf[BPE_PAYLOAD_SIZE];
3605d7b4d43SMatthew Ahrens 	struct drr_write_embedded *drrw =
3615d7b4d43SMatthew Ahrens 	    &(dsp->dsa_drr->drr_u.drr_write_embedded);
3625d7b4d43SMatthew Ahrens 
3635d7b4d43SMatthew Ahrens 	if (dsp->dsa_pending_op != PENDING_NONE) {
36498110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
3655d7b4d43SMatthew Ahrens 			return (EINTR);
3665d7b4d43SMatthew Ahrens 		dsp->dsa_pending_op = PENDING_NONE;
3675d7b4d43SMatthew Ahrens 	}
3685d7b4d43SMatthew Ahrens 
3695d7b4d43SMatthew Ahrens 	ASSERT(BP_IS_EMBEDDED(bp));
3705d7b4d43SMatthew Ahrens 
3715d7b4d43SMatthew Ahrens 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
3725d7b4d43SMatthew Ahrens 	dsp->dsa_drr->drr_type = DRR_WRITE_EMBEDDED;
3735d7b4d43SMatthew Ahrens 	drrw->drr_object = object;
3745d7b4d43SMatthew Ahrens 	drrw->drr_offset = offset;
3755d7b4d43SMatthew Ahrens 	drrw->drr_length = blksz;
3765d7b4d43SMatthew Ahrens 	drrw->drr_toguid = dsp->dsa_toguid;
3775d7b4d43SMatthew Ahrens 	drrw->drr_compression = BP_GET_COMPRESS(bp);
3785d7b4d43SMatthew Ahrens 	drrw->drr_etype = BPE_GET_ETYPE(bp);
3795d7b4d43SMatthew Ahrens 	drrw->drr_lsize = BPE_GET_LSIZE(bp);
3805d7b4d43SMatthew Ahrens 	drrw->drr_psize = BPE_GET_PSIZE(bp);
3815d7b4d43SMatthew Ahrens 
3825d7b4d43SMatthew Ahrens 	decode_embedded_bp_compressed(bp, buf);
3835d7b4d43SMatthew Ahrens 
38498110f08SMatthew Ahrens 	if (dump_record(dsp, buf, P2ROUNDUP(drrw->drr_psize, 8)) != 0)
3855d7b4d43SMatthew Ahrens 		return (EINTR);
3865d7b4d43SMatthew Ahrens 	return (0);
3875d7b4d43SMatthew Ahrens }
3885d7b4d43SMatthew Ahrens 
3890a586ceaSMark Shellenbaum static int
dump_spill(dmu_sendarg_t * dsp,const blkptr_t * bp,uint64_t object,void * data)390eb633035STom Caputi dump_spill(dmu_sendarg_t *dsp, const blkptr_t *bp, uint64_t object, void *data)
3910a586ceaSMark Shellenbaum {
3924e3c9f44SBill Pijewski 	struct drr_spill *drrs = &(dsp->dsa_drr->drr_u.drr_spill);
393eb633035STom Caputi 	uint64_t blksz = BP_GET_LSIZE(bp);
394eb633035STom Caputi 	uint64_t payload_size = blksz;
3950a586ceaSMark Shellenbaum 
3964e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE) {
39798110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
398be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
3994e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
4000a586ceaSMark Shellenbaum 	}
4010a586ceaSMark Shellenbaum 
4020a586ceaSMark Shellenbaum 	/* write a SPILL record */
4034e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
4044e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_SPILL;
4050a586ceaSMark Shellenbaum 	drrs->drr_object = object;
4060a586ceaSMark Shellenbaum 	drrs->drr_length = blksz;
4074e3c9f44SBill Pijewski 	drrs->drr_toguid = dsp->dsa_toguid;
4080a586ceaSMark Shellenbaum 
409eb633035STom Caputi 	/* See comment in dump_dnode() for full details */
410eb633035STom Caputi 	if (zfs_send_unmodified_spill_blocks &&
411eb633035STom Caputi 	    (bp->blk_birth <= dsp->dsa_fromtxg)) {
412eb633035STom Caputi 		drrs->drr_flags |= DRR_SPILL_UNMODIFIED;
413eb633035STom Caputi 	}
414eb633035STom Caputi 
415eb633035STom Caputi 	/* handle raw send fields */
416eb633035STom Caputi 	if (dsp->dsa_featureflags & DMU_BACKUP_FEATURE_RAW) {
417eb633035STom Caputi 		ASSERT(BP_IS_PROTECTED(bp));
418eb633035STom Caputi 
419eb633035STom Caputi 		if (BP_SHOULD_BYTESWAP(bp))
420eb633035STom Caputi 			drrs->drr_flags |= DRR_RAW_BYTESWAP;
421eb633035STom Caputi 		drrs->drr_compressiontype = BP_GET_COMPRESS(bp);
422eb633035STom Caputi 		drrs->drr_compressed_size = BP_GET_PSIZE(bp);
423eb633035STom Caputi 		zio_crypt_decode_params_bp(bp, drrs->drr_salt, drrs->drr_iv);
424eb633035STom Caputi 		zio_crypt_decode_mac_bp(bp, drrs->drr_mac);
425eb633035STom Caputi 		payload_size = drrs->drr_compressed_size;
426eb633035STom Caputi 	}
427eb633035STom Caputi 
428eb633035STom Caputi 	if (dump_record(dsp, data, payload_size) != 0)
429be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
4300a586ceaSMark Shellenbaum 	return (0);
4310a586ceaSMark Shellenbaum }
4320a586ceaSMark Shellenbaum 
433efb80947Sahrens static int
dump_freeobjects(dmu_sendarg_t * dsp,uint64_t firstobj,uint64_t numobjs)4344e3c9f44SBill Pijewski dump_freeobjects(dmu_sendarg_t *dsp, uint64_t firstobj, uint64_t numobjs)
435efb80947Sahrens {
4364e3c9f44SBill Pijewski 	struct drr_freeobjects *drrfo = &(dsp->dsa_drr->drr_u.drr_freeobjects);
4379e69d7d0SLori Alt 
4389e69d7d0SLori Alt 	/*
4399e69d7d0SLori Alt 	 * If there is a pending op, but it's not PENDING_FREEOBJECTS,
4409e69d7d0SLori Alt 	 * push it out, since free block aggregation can only be done for
4419e69d7d0SLori Alt 	 * blocks of the same type (i.e., DRR_FREE records can only be
4429e69d7d0SLori Alt 	 * aggregated with other DRR_FREE records.  DRR_FREEOBJECTS records
4439e69d7d0SLori Alt 	 * can only be aggregated with other DRR_FREEOBJECTS records.
4449e69d7d0SLori Alt 	 */
4454e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE &&
4464e3c9f44SBill Pijewski 	    dsp->dsa_pending_op != PENDING_FREEOBJECTS) {
44798110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
448be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
4494e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
4509e69d7d0SLori Alt 	}
4514e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op == PENDING_FREEOBJECTS) {
4529e69d7d0SLori Alt 		/*
4539e69d7d0SLori Alt 		 * See whether this free object array can be aggregated
4549e69d7d0SLori Alt 		 * with pending one
4559e69d7d0SLori Alt 		 */
4569e69d7d0SLori Alt 		if (drrfo->drr_firstobj + drrfo->drr_numobjs == firstobj) {
4579e69d7d0SLori Alt 			drrfo->drr_numobjs += numobjs;
4589e69d7d0SLori Alt 			return (0);
4599e69d7d0SLori Alt 		} else {
4609e69d7d0SLori Alt 			/* can't be aggregated.  Push out pending record */
46198110f08SMatthew Ahrens 			if (dump_record(dsp, NULL, 0) != 0)
462be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINTR));
4634e3c9f44SBill Pijewski 			dsp->dsa_pending_op = PENDING_NONE;
4649e69d7d0SLori Alt 		}
4659e69d7d0SLori Alt 	}
4669e69d7d0SLori Alt 
467efb80947Sahrens 	/* write a FREEOBJECTS record */
4684e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
4694e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_FREEOBJECTS;
4709e69d7d0SLori Alt 	drrfo->drr_firstobj = firstobj;
4719e69d7d0SLori Alt 	drrfo->drr_numobjs = numobjs;
4724e3c9f44SBill Pijewski 	drrfo->drr_toguid = dsp->dsa_toguid;
4739e69d7d0SLori Alt 
4744e3c9f44SBill Pijewski 	dsp->dsa_pending_op = PENDING_FREEOBJECTS;
475efb80947Sahrens 
476efb80947Sahrens 	return (0);
477efb80947Sahrens }
478efb80947Sahrens 
479efb80947Sahrens static int
dump_dnode(dmu_sendarg_t * dsp,const blkptr_t * bp,uint64_t object,dnode_phys_t * dnp)480eb633035STom Caputi dump_dnode(dmu_sendarg_t *dsp, const blkptr_t *bp, uint64_t object,
481eb633035STom Caputi     dnode_phys_t *dnp)
482efb80947Sahrens {
4834e3c9f44SBill Pijewski 	struct drr_object *drro = &(dsp->dsa_drr->drr_u.drr_object);
484eb633035STom Caputi 	int bonuslen;
4859e69d7d0SLori Alt 
4869c3fd121SMatthew Ahrens 	if (object < dsp->dsa_resume_object) {
4879c3fd121SMatthew Ahrens 		/*
4889c3fd121SMatthew Ahrens 		 * Note: when resuming, we will visit all the dnodes in
4899c3fd121SMatthew Ahrens 		 * the block of dnodes that we are resuming from.  In
4909c3fd121SMatthew Ahrens 		 * this case it's unnecessary to send the dnodes prior to
4919c3fd121SMatthew Ahrens 		 * the one we are resuming from.  We should be at most one
4929c3fd121SMatthew Ahrens 		 * block's worth of dnodes behind the resume point.
4939c3fd121SMatthew Ahrens 		 */
4949c3fd121SMatthew Ahrens 		ASSERT3U(dsp->dsa_resume_object - object, <,
4959c3fd121SMatthew Ahrens 		    1 << (DNODE_BLOCK_SHIFT - DNODE_SHIFT));
4969c3fd121SMatthew Ahrens 		return (0);
4979c3fd121SMatthew Ahrens 	}
4989c3fd121SMatthew Ahrens 
499efb80947Sahrens 	if (dnp == NULL || dnp->dn_type == DMU_OT_NONE)
5004e3c9f44SBill Pijewski 		return (dump_freeobjects(dsp, object, 1));
501efb80947Sahrens 
5024e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE) {
50398110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
504be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
5054e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
5069e69d7d0SLori Alt 	}
5079e69d7d0SLori Alt 
508efb80947Sahrens 	/* write an OBJECT record */
5094e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
5104e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_OBJECT;
5119e69d7d0SLori Alt 	drro->drr_object = object;
5129e69d7d0SLori Alt 	drro->drr_type = dnp->dn_type;
5139e69d7d0SLori Alt 	drro->drr_bonustype = dnp->dn_bonustype;
5149e69d7d0SLori Alt 	drro->drr_blksz = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
5159e69d7d0SLori Alt 	drro->drr_bonuslen = dnp->dn_bonuslen;
51654811da5SToomas Soome 	drro->drr_dn_slots = dnp->dn_extra_slots + 1;
5179e69d7d0SLori Alt 	drro->drr_checksumtype = dnp->dn_checksum;
5189e69d7d0SLori Alt 	drro->drr_compress = dnp->dn_compress;
5194e3c9f44SBill Pijewski 	drro->drr_toguid = dsp->dsa_toguid;
5209e69d7d0SLori Alt 
521b5152584SMatthew Ahrens 	if (!(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
522b5152584SMatthew Ahrens 	    drro->drr_blksz > SPA_OLD_MAXBLOCKSIZE)
523b5152584SMatthew Ahrens 		drro->drr_blksz = SPA_OLD_MAXBLOCKSIZE;
524b5152584SMatthew Ahrens 
525eb633035STom Caputi 	bonuslen = P2ROUNDUP(dnp->dn_bonuslen, 8);
526eb633035STom Caputi 
527eb633035STom Caputi 	if ((dsp->dsa_featureflags & DMU_BACKUP_FEATURE_RAW)) {
528eb633035STom Caputi 		ASSERT(BP_IS_ENCRYPTED(bp));
529eb633035STom Caputi 
530eb633035STom Caputi 		if (BP_SHOULD_BYTESWAP(bp))
531eb633035STom Caputi 			drro->drr_flags |= DRR_RAW_BYTESWAP;
532eb633035STom Caputi 
533eb633035STom Caputi 		/* needed for reconstructing dnp on recv side */
534eb633035STom Caputi 		drro->drr_maxblkid = dnp->dn_maxblkid;
535eb633035STom Caputi 		drro->drr_indblkshift = dnp->dn_indblkshift;
536eb633035STom Caputi 		drro->drr_nlevels = dnp->dn_nlevels;
537eb633035STom Caputi 		drro->drr_nblkptr = dnp->dn_nblkptr;
538eb633035STom Caputi 
539eb633035STom Caputi 		/*
540eb633035STom Caputi 		 * Since we encrypt the entire bonus area, the (raw) part
541eb633035STom Caputi 		 * beyond the bonuslen is actually nonzero, so we need
542eb633035STom Caputi 		 * to send it.
543eb633035STom Caputi 		 */
544eb633035STom Caputi 		if (bonuslen != 0) {
545eb633035STom Caputi 			drro->drr_raw_bonuslen = DN_MAX_BONUS_LEN(dnp);
546eb633035STom Caputi 			bonuslen = drro->drr_raw_bonuslen;
547eb633035STom Caputi 		}
54898110f08SMatthew Ahrens 	}
549efb80947Sahrens 
550eb633035STom Caputi 	/*
551eb633035STom Caputi 	 * DRR_OBJECT_SPILL is set for every dnode which references a
552eb633035STom Caputi 	 * spill block.  This allows the receiving pool to definitively
553eb633035STom Caputi 	 * determine when a spill block should be kept or freed.
554eb633035STom Caputi 	 */
555eb633035STom Caputi 	if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)
556eb633035STom Caputi 		drro->drr_flags |= DRR_OBJECT_SPILL;
557eb633035STom Caputi 
558eb633035STom Caputi 	if (dump_record(dsp, DN_BONUS(dnp), bonuslen) != 0)
559eb633035STom Caputi 		return (SET_ERROR(EINTR));
560eb633035STom Caputi 
5612f3d8780SMatthew Ahrens 	/* Free anything past the end of the file. */
5624e3c9f44SBill Pijewski 	if (dump_free(dsp, object, (dnp->dn_maxblkid + 1) *
563eb633035STom Caputi 	    (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT), DMU_OBJECT_END) != 0)
564be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
565eb633035STom Caputi 
566eb633035STom Caputi 	/*
567eb633035STom Caputi 	 * Send DRR_SPILL records for unmodified spill blocks.  This is useful
568eb633035STom Caputi 	 * because changing certain attributes of the object (e.g. blocksize)
569eb633035STom Caputi 	 * can cause old versions of ZFS to incorrectly remove a spill block.
570eb633035STom Caputi 	 * Including these records in the stream forces an up to date version
571eb633035STom Caputi 	 * to always be written ensuring they're never lost.  Current versions
572eb633035STom Caputi 	 * of the code which understand the DRR_FLAG_SPILL_BLOCK feature can
573eb633035STom Caputi 	 * ignore these unmodified spill blocks.
574eb633035STom Caputi 	 */
575eb633035STom Caputi 	if (zfs_send_unmodified_spill_blocks &&
576eb633035STom Caputi 	    (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) &&
577eb633035STom Caputi 	    (DN_SPILL_BLKPTR(dnp)->blk_birth <= dsp->dsa_fromtxg)) {
578eb633035STom Caputi 		struct send_block_record record;
579eb633035STom Caputi 
580eb633035STom Caputi 		bzero(&record, sizeof (struct send_block_record));
581eb633035STom Caputi 		record.eos_marker = B_FALSE;
582eb633035STom Caputi 		record.bp = *DN_SPILL_BLKPTR(dnp);
583eb633035STom Caputi 		SET_BOOKMARK(&(record.zb), dmu_objset_id(dsp->dsa_os),
584eb633035STom Caputi 		    object, 0, DMU_SPILL_BLKID);
585eb633035STom Caputi 
586eb633035STom Caputi 		if (do_dump(dsp, &record) != 0)
587eb633035STom Caputi 			return (SET_ERROR(EINTR));
588eb633035STom Caputi 	}
589eb633035STom Caputi 
5903b2aab18SMatthew Ahrens 	if (dsp->dsa_err != 0)
591be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
592efb80947Sahrens 	return (0);
593efb80947Sahrens }
594efb80947Sahrens 
595eb633035STom Caputi static int
dump_object_range(dmu_sendarg_t * dsp,const blkptr_t * bp,uint64_t firstobj,uint64_t numslots)596eb633035STom Caputi dump_object_range(dmu_sendarg_t *dsp, const blkptr_t *bp, uint64_t firstobj,
597eb633035STom Caputi     uint64_t numslots)
598eb633035STom Caputi {
599eb633035STom Caputi 	struct drr_object_range *drror =
600eb633035STom Caputi 	    &(dsp->dsa_drr->drr_u.drr_object_range);
601eb633035STom Caputi 
602eb633035STom Caputi 	/* we only use this record type for raw sends */
603eb633035STom Caputi 	ASSERT(BP_IS_PROTECTED(bp));
604eb633035STom Caputi 	ASSERT(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_RAW);
605eb633035STom Caputi 	ASSERT3U(BP_GET_COMPRESS(bp), ==, ZIO_COMPRESS_OFF);
606eb633035STom Caputi 	ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_DNODE);
607eb633035STom Caputi 	ASSERT0(BP_GET_LEVEL(bp));
608eb633035STom Caputi 
609eb633035STom Caputi 	if (dsp->dsa_pending_op != PENDING_NONE) {
610eb633035STom Caputi 		if (dump_record(dsp, NULL, 0) != 0)
611eb633035STom Caputi 			return (SET_ERROR(EINTR));
612eb633035STom Caputi 		dsp->dsa_pending_op = PENDING_NONE;
613eb633035STom Caputi 	}
614eb633035STom Caputi 
615eb633035STom Caputi 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
616eb633035STom Caputi 	dsp->dsa_drr->drr_type = DRR_OBJECT_RANGE;
617eb633035STom Caputi 	drror->drr_firstobj = firstobj;
618eb633035STom Caputi 	drror->drr_numslots = numslots;
619eb633035STom Caputi 	drror->drr_toguid = dsp->dsa_toguid;
620eb633035STom Caputi 	if (BP_SHOULD_BYTESWAP(bp))
621eb633035STom Caputi 		drror->drr_flags |= DRR_RAW_BYTESWAP;
622eb633035STom Caputi 	zio_crypt_decode_params_bp(bp, drror->drr_salt, drror->drr_iv);
623eb633035STom Caputi 	zio_crypt_decode_mac_bp(bp, drror->drr_mac);
624eb633035STom Caputi 
625eb633035STom Caputi 	if (dump_record(dsp, NULL, 0) != 0)
626eb633035STom Caputi 		return (SET_ERROR(EINTR));
627eb633035STom Caputi 	return (0);
628eb633035STom Caputi }
629eb633035STom Caputi 
6305d7b4d43SMatthew Ahrens static boolean_t
backup_do_embed(dmu_sendarg_t * dsp,const blkptr_t * bp)6315d7b4d43SMatthew Ahrens backup_do_embed(dmu_sendarg_t *dsp, const blkptr_t *bp)
6325d7b4d43SMatthew Ahrens {
6335d7b4d43SMatthew Ahrens 	if (!BP_IS_EMBEDDED(bp))
6345d7b4d43SMatthew Ahrens 		return (B_FALSE);
6355d7b4d43SMatthew Ahrens 
6365d7b4d43SMatthew Ahrens 	/*
6375d7b4d43SMatthew Ahrens 	 * Compression function must be legacy, or explicitly enabled.
6385d7b4d43SMatthew Ahrens 	 */
6395d7b4d43SMatthew Ahrens 	if ((BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_LEGACY_FUNCTIONS &&
6405602294fSDan Kimmel 	    !(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_LZ4)))
6415d7b4d43SMatthew Ahrens 		return (B_FALSE);
6425d7b4d43SMatthew Ahrens 
6435d7b4d43SMatthew Ahrens 	/*
6445d7b4d43SMatthew Ahrens 	 * Embed type must be explicitly enabled.
6455d7b4d43SMatthew Ahrens 	 */
6465d7b4d43SMatthew Ahrens 	switch (BPE_GET_ETYPE(bp)) {
6475d7b4d43SMatthew Ahrens 	case BP_EMBEDDED_TYPE_DATA:
6485d7b4d43SMatthew Ahrens 		if (dsp->dsa_featureflags & DMU_BACKUP_FEATURE_EMBED_DATA)
6495d7b4d43SMatthew Ahrens 			return (B_TRUE);
6505d7b4d43SMatthew Ahrens 		break;
6515d7b4d43SMatthew Ahrens 	default:
6525d7b4d43SMatthew Ahrens 		return (B_FALSE);
6535d7b4d43SMatthew Ahrens 	}
6545d7b4d43SMatthew Ahrens 	return (B_FALSE);
6555d7b4d43SMatthew Ahrens }
6565d7b4d43SMatthew Ahrens 
657a2cdcdd2SPaul Dagnelie /*
658a2cdcdd2SPaul Dagnelie  * This is the callback function to traverse_dataset that acts as the worker
659a2cdcdd2SPaul Dagnelie  * thread for dmu_send_impl.
660a2cdcdd2SPaul Dagnelie  */
661a2cdcdd2SPaul Dagnelie /*ARGSUSED*/
662a2cdcdd2SPaul Dagnelie static int
send_cb(spa_t * spa,zilog_t * zilog,const blkptr_t * bp,const zbookmark_phys_t * zb,const struct dnode_phys * dnp,void * arg)663a2cdcdd2SPaul Dagnelie send_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
664a2cdcdd2SPaul Dagnelie     const zbookmark_phys_t *zb, const struct dnode_phys *dnp, void *arg)
665a2cdcdd2SPaul Dagnelie {
666a2cdcdd2SPaul Dagnelie 	struct send_thread_arg *sta = arg;
667a2cdcdd2SPaul Dagnelie 	struct send_block_record *record;
668a2cdcdd2SPaul Dagnelie 	uint64_t record_size;
669a2cdcdd2SPaul Dagnelie 	int err = 0;
670efb80947Sahrens 
6719c3fd121SMatthew Ahrens 	ASSERT(zb->zb_object == DMU_META_DNODE_OBJECT ||
6729c3fd121SMatthew Ahrens 	    zb->zb_object >= sta->resume.zb_object);
673eb633035STom Caputi 	ASSERT3P(sta->ds, !=, NULL);
6749c3fd121SMatthew Ahrens 
675a2cdcdd2SPaul Dagnelie 	if (sta->cancel)
676a2cdcdd2SPaul Dagnelie 		return (SET_ERROR(EINTR));
677a2cdcdd2SPaul Dagnelie 
678a2cdcdd2SPaul Dagnelie 	if (bp == NULL) {
679a2cdcdd2SPaul Dagnelie 		ASSERT3U(zb->zb_level, ==, ZB_DNODE_LEVEL);
680a2cdcdd2SPaul Dagnelie 		return (0);
681a2cdcdd2SPaul Dagnelie 	} else if (zb->zb_level < 0) {
682a2cdcdd2SPaul Dagnelie 		return (0);
683a2cdcdd2SPaul Dagnelie 	}
684a2cdcdd2SPaul Dagnelie 
685a2cdcdd2SPaul Dagnelie 	record = kmem_zalloc(sizeof (struct send_block_record), KM_SLEEP);
686a2cdcdd2SPaul Dagnelie 	record->eos_marker = B_FALSE;
687a2cdcdd2SPaul Dagnelie 	record->bp = *bp;
688a2cdcdd2SPaul Dagnelie 	record->zb = *zb;
689a2cdcdd2SPaul Dagnelie 	record->indblkshift = dnp->dn_indblkshift;
690a2cdcdd2SPaul Dagnelie 	record->datablkszsec = dnp->dn_datablkszsec;
691a2cdcdd2SPaul Dagnelie 	record_size = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
692a2cdcdd2SPaul Dagnelie 	bqueue_enqueue(&sta->q, record, record_size);
693a2cdcdd2SPaul Dagnelie 
694a2cdcdd2SPaul Dagnelie 	return (err);
695a2cdcdd2SPaul Dagnelie }
696a2cdcdd2SPaul Dagnelie 
697a2cdcdd2SPaul Dagnelie /*
698a2cdcdd2SPaul Dagnelie  * This function kicks off the traverse_dataset.  It also handles setting the
699a2cdcdd2SPaul Dagnelie  * error code of the thread in case something goes wrong, and pushes the End of
700a2cdcdd2SPaul Dagnelie  * Stream record when the traverse_dataset call has finished.  If there is no
701a2cdcdd2SPaul Dagnelie  * dataset to traverse, the thread immediately pushes End of Stream marker.
702a2cdcdd2SPaul Dagnelie  */
703a2cdcdd2SPaul Dagnelie static void
send_traverse_thread(void * arg)704a2cdcdd2SPaul Dagnelie send_traverse_thread(void *arg)
705a2cdcdd2SPaul Dagnelie {
706a2cdcdd2SPaul Dagnelie 	struct send_thread_arg *st_arg = arg;
707a2cdcdd2SPaul Dagnelie 	int err;
708a2cdcdd2SPaul Dagnelie 	struct send_block_record *data;
709a2cdcdd2SPaul Dagnelie 
710a2cdcdd2SPaul Dagnelie 	if (st_arg->ds != NULL) {
7119c3fd121SMatthew Ahrens 		err = traverse_dataset_resume(st_arg->ds,
7129c3fd121SMatthew Ahrens 		    st_arg->fromtxg, &st_arg->resume,
7139c3fd121SMatthew Ahrens 		    st_arg->flags, send_cb, st_arg);
7149c3fd121SMatthew Ahrens 
715a2cdcdd2SPaul Dagnelie 		if (err != EINTR)
716a2cdcdd2SPaul Dagnelie 			st_arg->error_code = err;
717a2cdcdd2SPaul Dagnelie 	}
718a2cdcdd2SPaul Dagnelie 	data = kmem_zalloc(sizeof (*data), KM_SLEEP);
719a2cdcdd2SPaul Dagnelie 	data->eos_marker = B_TRUE;
720a2cdcdd2SPaul Dagnelie 	bqueue_enqueue(&st_arg->q, data, 1);
721f2c1e9bcSJorgen Lundman 	thread_exit();
722a2cdcdd2SPaul Dagnelie }
723a2cdcdd2SPaul Dagnelie 
724a2cdcdd2SPaul Dagnelie /*
725a2cdcdd2SPaul Dagnelie  * This function actually handles figuring out what kind of record needs to be
726a2cdcdd2SPaul Dagnelie  * dumped, reading the data (which has hopefully been prefetched), and calling
727a2cdcdd2SPaul Dagnelie  * the appropriate helper function.
728a2cdcdd2SPaul Dagnelie  */
729efb80947Sahrens static int
do_dump(dmu_sendarg_t * dsa,struct send_block_record * data)730a2cdcdd2SPaul Dagnelie do_dump(dmu_sendarg_t *dsa, struct send_block_record *data)
731efb80947Sahrens {
732a2cdcdd2SPaul Dagnelie 	dsl_dataset_t *ds = dmu_objset_ds(dsa->dsa_os);
733a2cdcdd2SPaul Dagnelie 	const blkptr_t *bp = &data->bp;
734a2cdcdd2SPaul Dagnelie 	const zbookmark_phys_t *zb = &data->zb;
735a2cdcdd2SPaul Dagnelie 	uint8_t indblkshift = data->indblkshift;
736a2cdcdd2SPaul Dagnelie 	uint16_t dblkszsec = data->datablkszsec;
737a2cdcdd2SPaul Dagnelie 	spa_t *spa = ds->ds_dir->dd_pool->dp_spa;
738efb80947Sahrens 	dmu_object_type_t type = bp ? BP_GET_TYPE(bp) : DMU_OT_NONE;
739efb80947Sahrens 	int err = 0;
740efb80947Sahrens 
741a2cdcdd2SPaul Dagnelie 	ASSERT3U(zb->zb_level, >=, 0);
742efb80947Sahrens 
7439c3fd121SMatthew Ahrens 	ASSERT(zb->zb_object == DMU_META_DNODE_OBJECT ||
7449c3fd121SMatthew Ahrens 	    zb->zb_object >= dsa->dsa_resume_object);
7459c3fd121SMatthew Ahrens 
746eb633035STom Caputi 	/*
747eb633035STom Caputi 	 * All bps of an encrypted os should have the encryption bit set.
748eb633035STom Caputi 	 * If this is not true it indicates tampering and we report an error.
749eb633035STom Caputi 	 */
750eb633035STom Caputi 	if (dsa->dsa_os->os_encrypted &&
751eb633035STom Caputi 	    !BP_IS_HOLE(bp) && !BP_USES_CRYPT(bp)) {
752eb633035STom Caputi 		spa_log_error(spa, zb);
753eb633035STom Caputi 		zfs_panic_recover("unencrypted block in encrypted "
754eb633035STom Caputi 		    "object set %llu", ds->ds_object);
755eb633035STom Caputi 		return (SET_ERROR(EIO));
756eb633035STom Caputi 	}
757eb633035STom Caputi 
758b24ab676SJeff Bonwick 	if (zb->zb_object != DMU_META_DNODE_OBJECT &&
759b24ab676SJeff Bonwick 	    DMU_OBJECT_IS_SPECIAL(zb->zb_object)) {
76014843421SMatthew Ahrens 		return (0);
76143466aaeSMax Grossman 	} else if (BP_IS_HOLE(bp) &&
76243466aaeSMax Grossman 	    zb->zb_object == DMU_META_DNODE_OBJECT) {
763a2cdcdd2SPaul Dagnelie 		uint64_t span = BP_SPAN(dblkszsec, indblkshift, zb->zb_level);
76488b7b0f2SMatthew Ahrens 		uint64_t dnobj = (zb->zb_blkid * span) >> DNODE_SHIFT;
765a2cdcdd2SPaul Dagnelie 		err = dump_freeobjects(dsa, dnobj, span >> DNODE_SHIFT);
76643466aaeSMax Grossman 	} else if (BP_IS_HOLE(bp)) {
767a2cdcdd2SPaul Dagnelie 		uint64_t span = BP_SPAN(dblkszsec, indblkshift, zb->zb_level);
768a2cdcdd2SPaul Dagnelie 		uint64_t offset = zb->zb_blkid * span;
769eb633035STom Caputi 		/* Don't dump free records for offsets > DMU_OBJECT_END */
770eb633035STom Caputi 		if (zb->zb_blkid == 0 || span <= DMU_OBJECT_END / zb->zb_blkid)
771eb633035STom Caputi 			err = dump_free(dsa, zb->zb_object, offset, span);
77288b7b0f2SMatthew Ahrens 	} else if (zb->zb_level > 0 || type == DMU_OT_OBJSET) {
77388b7b0f2SMatthew Ahrens 		return (0);
77488b7b0f2SMatthew Ahrens 	} else if (type == DMU_OT_DNODE) {
77554811da5SToomas Soome 		int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
7767adb730bSGeorge Wilson 		arc_flags_t aflags = ARC_FLAG_WAIT;
77788b7b0f2SMatthew Ahrens 		arc_buf_t *abuf;
778eb633035STom Caputi 		enum zio_flag zioflags = ZIO_FLAG_CANFAIL;
779eb633035STom Caputi 
780eb633035STom Caputi 		if (dsa->dsa_featureflags & DMU_BACKUP_FEATURE_RAW) {
781eb633035STom Caputi 			ASSERT(BP_IS_ENCRYPTED(bp));
782eb633035STom Caputi 			ASSERT3U(BP_GET_COMPRESS(bp), ==, ZIO_COMPRESS_OFF);
783eb633035STom Caputi 			zioflags |= ZIO_FLAG_RAW;
784eb633035STom Caputi 		}
785efb80947Sahrens 
786a2cdcdd2SPaul Dagnelie 		ASSERT0(zb->zb_level);
787a2cdcdd2SPaul Dagnelie 
7881b912ec7SGeorge Wilson 		if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
789eb633035STom Caputi 		    ZIO_PRIORITY_ASYNC_READ, zioflags, &aflags, zb) != 0)
790be6fd75aSMatthew Ahrens 			return (SET_ERROR(EIO));
79188b7b0f2SMatthew Ahrens 
792a2cdcdd2SPaul Dagnelie 		dnode_phys_t *blk = abuf->b_data;
79354811da5SToomas Soome 		uint64_t dnobj = zb->zb_blkid * epb;
794eb633035STom Caputi 
795eb633035STom Caputi 		/*
796eb633035STom Caputi 		 * Raw sends require sending encryption parameters for the
797eb633035STom Caputi 		 * block of dnodes. Regular sends do not need to send this
798eb633035STom Caputi 		 * info.
799eb633035STom Caputi 		 */
800eb633035STom Caputi 		if (dsa->dsa_featureflags & DMU_BACKUP_FEATURE_RAW) {
801eb633035STom Caputi 			ASSERT(arc_is_encrypted(abuf));
802eb633035STom Caputi 			err = dump_object_range(dsa, bp, dnobj, epb);
803eb633035STom Caputi 		}
804eb633035STom Caputi 
805eb633035STom Caputi 		if (err == 0) {
806eb633035STom Caputi 			for (int i = 0; i < epb;
807eb633035STom Caputi 			    i += blk[i].dn_extra_slots + 1) {
808eb633035STom Caputi 				err = dump_dnode(dsa, bp, dnobj + i, blk + i);
809eb633035STom Caputi 				if (err != 0)
810eb633035STom Caputi 					break;
811eb633035STom Caputi 			}
812efb80947Sahrens 		}
813dcbf3bd6SGeorge Wilson 		arc_buf_destroy(abuf, &abuf);
8140a586ceaSMark Shellenbaum 	} else if (type == DMU_OT_SA) {
8157adb730bSGeorge Wilson 		arc_flags_t aflags = ARC_FLAG_WAIT;
8160a586ceaSMark Shellenbaum 		arc_buf_t *abuf;
817eb633035STom Caputi 		enum zio_flag zioflags = ZIO_FLAG_CANFAIL;
818eb633035STom Caputi 
819eb633035STom Caputi 		if (dsa->dsa_featureflags & DMU_BACKUP_FEATURE_RAW) {
820eb633035STom Caputi 			ASSERT(BP_IS_PROTECTED(bp));
821eb633035STom Caputi 			zioflags |= ZIO_FLAG_RAW;
822eb633035STom Caputi 		}
8230a586ceaSMark Shellenbaum 
8241b912ec7SGeorge Wilson 		if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
825eb633035STom Caputi 		    ZIO_PRIORITY_ASYNC_READ, zioflags, &aflags, zb) != 0)
826be6fd75aSMatthew Ahrens 			return (SET_ERROR(EIO));
8270a586ceaSMark Shellenbaum 
828eb633035STom Caputi 		err = dump_spill(dsa, bp, zb->zb_object, abuf->b_data);
829dcbf3bd6SGeorge Wilson 		arc_buf_destroy(abuf, &abuf);
830a2cdcdd2SPaul Dagnelie 	} else if (backup_do_embed(dsa, bp)) {
8315d7b4d43SMatthew Ahrens 		/* it's an embedded level-0 block of a regular object */
832a2cdcdd2SPaul Dagnelie 		int blksz = dblkszsec << SPA_MINBLOCKSHIFT;
833a2cdcdd2SPaul Dagnelie 		ASSERT0(zb->zb_level);
834a2cdcdd2SPaul Dagnelie 		err = dump_write_embedded(dsa, zb->zb_object,
8355d7b4d43SMatthew Ahrens 		    zb->zb_blkid * blksz, blksz, bp);
836a2cdcdd2SPaul Dagnelie 	} else {
837a2cdcdd2SPaul Dagnelie 		/* it's a level-0 block of a regular object */
8387adb730bSGeorge Wilson 		arc_flags_t aflags = ARC_FLAG_WAIT;
83988b7b0f2SMatthew Ahrens 		arc_buf_t *abuf;
840a2cdcdd2SPaul Dagnelie 		int blksz = dblkszsec << SPA_MINBLOCKSHIFT;
841b5152584SMatthew Ahrens 		uint64_t offset;
84288b7b0f2SMatthew Ahrens 
8435602294fSDan Kimmel 		/*
8445602294fSDan Kimmel 		 * If we have large blocks stored on disk but the send flags
8455602294fSDan Kimmel 		 * don't allow us to send large blocks, we split the data from
8465602294fSDan Kimmel 		 * the arc buf into chunks.
8475602294fSDan Kimmel 		 */
8485602294fSDan Kimmel 		boolean_t split_large_blocks = blksz > SPA_OLD_MAXBLOCKSIZE &&
8495602294fSDan Kimmel 		    !(dsa->dsa_featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS);
850eb633035STom Caputi 
851eb633035STom Caputi 		/*
852eb633035STom Caputi 		 * Raw sends require that we always get raw data as it exists
853eb633035STom Caputi 		 * on disk, so we assert that we are not splitting blocks here.
854eb633035STom Caputi 		 */
855eb633035STom Caputi 		boolean_t request_raw =
856eb633035STom Caputi 		    (dsa->dsa_featureflags & DMU_BACKUP_FEATURE_RAW) != 0;
857eb633035STom Caputi 
8585602294fSDan Kimmel 		/*
8595602294fSDan Kimmel 		 * We should only request compressed data from the ARC if all
8605602294fSDan Kimmel 		 * the following are true:
8615602294fSDan Kimmel 		 *  - stream compression was requested
8625602294fSDan Kimmel 		 *  - we aren't splitting large blocks into smaller chunks
8635602294fSDan Kimmel 		 *  - the data won't need to be byteswapped before sending
8645602294fSDan Kimmel 		 *  - this isn't an embedded block
8655602294fSDan Kimmel 		 *  - this isn't metadata (if receiving on a different endian
8665602294fSDan Kimmel 		 *    system it can be byteswapped more easily)
8675602294fSDan Kimmel 		 */
8685602294fSDan Kimmel 		boolean_t request_compressed =
8695602294fSDan Kimmel 		    (dsa->dsa_featureflags & DMU_BACKUP_FEATURE_COMPRESSED) &&
8705602294fSDan Kimmel 		    !split_large_blocks && !BP_SHOULD_BYTESWAP(bp) &&
8715602294fSDan Kimmel 		    !BP_IS_EMBEDDED(bp) && !DMU_OT_IS_METADATA(BP_GET_TYPE(bp));
8725602294fSDan Kimmel 
873eb633035STom Caputi 		IMPLY(request_raw, !split_large_blocks);
874eb633035STom Caputi 		IMPLY(request_raw, BP_IS_PROTECTED(bp));
8755602294fSDan Kimmel 		ASSERT0(zb->zb_level);
8765602294fSDan Kimmel 		ASSERT(zb->zb_object > dsa->dsa_resume_object ||
8775602294fSDan Kimmel 		    (zb->zb_object == dsa->dsa_resume_object &&
8785602294fSDan Kimmel 		    zb->zb_blkid * blksz >= dsa->dsa_resume_offset));
8795602294fSDan Kimmel 
88078f17100SMatthew Ahrens 		ASSERT0(zb->zb_level);
8819c3fd121SMatthew Ahrens 		ASSERT(zb->zb_object > dsa->dsa_resume_object ||
8829c3fd121SMatthew Ahrens 		    (zb->zb_object == dsa->dsa_resume_object &&
8839c3fd121SMatthew Ahrens 		    zb->zb_blkid * blksz >= dsa->dsa_resume_offset));
8849c3fd121SMatthew Ahrens 
8855602294fSDan Kimmel 		ASSERT3U(blksz, ==, BP_GET_LSIZE(bp));
8865602294fSDan Kimmel 
8875602294fSDan Kimmel 		enum zio_flag zioflags = ZIO_FLAG_CANFAIL;
888eb633035STom Caputi 		if (request_raw)
8895602294fSDan Kimmel 			zioflags |= ZIO_FLAG_RAW;
890eb633035STom Caputi 		else if (request_compressed)
891eb633035STom Caputi 			zioflags |= ZIO_FLAG_RAW_COMPRESS;
892eb633035STom Caputi 
8931b912ec7SGeorge Wilson 		if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
8945602294fSDan Kimmel 		    ZIO_PRIORITY_ASYNC_READ, zioflags, &aflags, zb) != 0) {
89519b94df9SMatthew Ahrens 			if (zfs_send_corrupt_data) {
89619b94df9SMatthew Ahrens 				/* Send a block filled with 0x"zfs badd bloc" */
8975602294fSDan Kimmel 				abuf = arc_alloc_buf(spa, &abuf, ARC_BUFC_DATA,
8985602294fSDan Kimmel 				    blksz);
89919b94df9SMatthew Ahrens 				uint64_t *ptr;
90019b94df9SMatthew Ahrens 				for (ptr = abuf->b_data;
90119b94df9SMatthew Ahrens 				    (char *)ptr < (char *)abuf->b_data + blksz;
90219b94df9SMatthew Ahrens 				    ptr++)
9038c76e076SBrian Behlendorf 					*ptr = 0x2f5baddb10cULL;
90419b94df9SMatthew Ahrens 			} else {
905be6fd75aSMatthew Ahrens 				return (SET_ERROR(EIO));
90619b94df9SMatthew Ahrens 			}
90719b94df9SMatthew Ahrens 		}
90888b7b0f2SMatthew Ahrens 
909b5152584SMatthew Ahrens 		offset = zb->zb_blkid * blksz;
910b5152584SMatthew Ahrens 
9115602294fSDan Kimmel 		if (split_large_blocks) {
912eb633035STom Caputi 			ASSERT0(arc_is_encrypted(abuf));
9135602294fSDan Kimmel 			ASSERT3U(arc_get_compression(abuf), ==,
9145602294fSDan Kimmel 			    ZIO_COMPRESS_OFF);
915b5152584SMatthew Ahrens 			char *buf = abuf->b_data;
916b5152584SMatthew Ahrens 			while (blksz > 0 && err == 0) {
917b5152584SMatthew Ahrens 				int n = MIN(blksz, SPA_OLD_MAXBLOCKSIZE);
918a2cdcdd2SPaul Dagnelie 				err = dump_write(dsa, type, zb->zb_object,
9195602294fSDan Kimmel 				    offset, n, n, NULL, buf);
920b5152584SMatthew Ahrens 				offset += n;
921b5152584SMatthew Ahrens 				buf += n;
922b5152584SMatthew Ahrens 				blksz -= n;
923b5152584SMatthew Ahrens 			}
924b5152584SMatthew Ahrens 		} else {
9255602294fSDan Kimmel 			err = dump_write(dsa, type, zb->zb_object, offset,
9265602294fSDan Kimmel 			    blksz, arc_buf_size(abuf), bp, abuf->b_data);
927b5152584SMatthew Ahrens 		}
928dcbf3bd6SGeorge Wilson 		arc_buf_destroy(abuf, &abuf);
929efb80947Sahrens 	}
930efb80947Sahrens 
931efb80947Sahrens 	ASSERT(err == 0 || err == EINTR);
932efb80947Sahrens 	return (err);
933efb80947Sahrens }
934efb80947Sahrens 
9354445fffbSMatthew Ahrens /*
936a2cdcdd2SPaul Dagnelie  * Pop the new data off the queue, and free the old data.
937a2cdcdd2SPaul Dagnelie  */
938a2cdcdd2SPaul Dagnelie static struct send_block_record *
get_next_record(bqueue_t * bq,struct send_block_record * data)939a2cdcdd2SPaul Dagnelie get_next_record(bqueue_t *bq, struct send_block_record *data)
940a2cdcdd2SPaul Dagnelie {
941a2cdcdd2SPaul Dagnelie 	struct send_block_record *tmp = bqueue_dequeue(bq);
942a2cdcdd2SPaul Dagnelie 	kmem_free(data, sizeof (*data));
943a2cdcdd2SPaul Dagnelie 	return (tmp);
944a2cdcdd2SPaul Dagnelie }
945a2cdcdd2SPaul Dagnelie 
946a2cdcdd2SPaul Dagnelie /*
947a2cdcdd2SPaul Dagnelie  * Actually do the bulk of the work in a zfs send.
948a2cdcdd2SPaul Dagnelie  *
949a2cdcdd2SPaul Dagnelie  * Note: Releases dp using the specified tag.
9504445fffbSMatthew Ahrens  */
9513b2aab18SMatthew Ahrens static int
dmu_send_impl(void * tag,dsl_pool_t * dp,dsl_dataset_t * to_ds,zfs_bookmark_phys_t * ancestor_zb,boolean_t is_clone,boolean_t embedok,boolean_t large_block_ok,boolean_t compressok,boolean_t rawok,int outfd,uint64_t resumeobj,uint64_t resumeoff,vnode_t * vp,offset_t * off)952a2cdcdd2SPaul Dagnelie dmu_send_impl(void *tag, dsl_pool_t *dp, dsl_dataset_t *to_ds,
9535602294fSDan Kimmel     zfs_bookmark_phys_t *ancestor_zb, boolean_t is_clone,
9545602294fSDan Kimmel     boolean_t embedok, boolean_t large_block_ok, boolean_t compressok,
955eb633035STom Caputi     boolean_t rawok, int outfd, uint64_t resumeobj, uint64_t resumeoff,
9569c3fd121SMatthew Ahrens     vnode_t *vp, offset_t *off)
957efb80947Sahrens {
9583b2aab18SMatthew Ahrens 	objset_t *os;
959efb80947Sahrens 	dmu_replay_record_t *drr;
9604e3c9f44SBill Pijewski 	dmu_sendarg_t *dsp;
961efb80947Sahrens 	int err;
9623cb34c60Sahrens 	uint64_t fromtxg = 0;
9635d7b4d43SMatthew Ahrens 	uint64_t featureflags = 0;
9649c3fd121SMatthew Ahrens 	struct send_thread_arg to_arg = { 0 };
965efb80947Sahrens 
966a2cdcdd2SPaul Dagnelie 	err = dmu_objset_from_ds(to_ds, &os);
9673b2aab18SMatthew Ahrens 	if (err != 0) {
9683b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, tag);
9693b2aab18SMatthew Ahrens 		return (err);
9703b2aab18SMatthew Ahrens 	}
971efb80947Sahrens 
972eb633035STom Caputi 	/*
973eb633035STom Caputi 	 * If this is a non-raw send of an encrypted ds, we can ensure that
974eb633035STom Caputi 	 * the objset_phys_t is authenticated. This is safe because this is
975eb633035STom Caputi 	 * either a snapshot or we have owned the dataset, ensuring that
976eb633035STom Caputi 	 * it can't be modified.
977eb633035STom Caputi 	 */
978eb633035STom Caputi 	if (!rawok && os->os_encrypted &&
979eb633035STom Caputi 	    arc_is_unauthenticated(os->os_phys_buf)) {
980eb633035STom Caputi 		zbookmark_phys_t zb;
981eb633035STom Caputi 
982eb633035STom Caputi 		SET_BOOKMARK(&zb, to_ds->ds_object, ZB_ROOT_OBJECT,
983eb633035STom Caputi 		    ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
984eb633035STom Caputi 		err = arc_untransform(os->os_phys_buf, os->os_spa,
985eb633035STom Caputi 		    &zb, B_FALSE);
986eb633035STom Caputi 		if (err != 0) {
987eb633035STom Caputi 			dsl_pool_rele(dp, tag);
988eb633035STom Caputi 			return (err);
989eb633035STom Caputi 		}
990eb633035STom Caputi 
991eb633035STom Caputi 		ASSERT0(arc_is_unauthenticated(os->os_phys_buf));
992eb633035STom Caputi 	}
993eb633035STom Caputi 
994efb80947Sahrens 	drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP);
995efb80947Sahrens 	drr->drr_type = DRR_BEGIN;
996efb80947Sahrens 	drr->drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
9979e69d7d0SLori Alt 	DMU_SET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo,
9989e69d7d0SLori Alt 	    DMU_SUBSTREAM);
999dc7cd546SMark Shellenbaum 
1000dc7cd546SMark Shellenbaum #ifdef _KERNEL
10013b2aab18SMatthew Ahrens 	if (dmu_objset_type(os) == DMU_OST_ZFS) {
1002dc7cd546SMark Shellenbaum 		uint64_t version;
10033b2aab18SMatthew Ahrens 		if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &version) != 0) {
10044e3c9f44SBill Pijewski 			kmem_free(drr, sizeof (dmu_replay_record_t));
10053b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, tag);
1006be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
10074e3c9f44SBill Pijewski 		}
10083b2aab18SMatthew Ahrens 		if (version >= ZPL_VERSION_SA) {
10095d7b4d43SMatthew Ahrens 			featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
1010dc7cd546SMark Shellenbaum 		}
1011dc7cd546SMark Shellenbaum 	}
1012dc7cd546SMark Shellenbaum #endif
1013dc7cd546SMark Shellenbaum 
1014eb633035STom Caputi 	/* raw sends imply large_block_ok */
1015eb633035STom Caputi 	if ((large_block_ok || rawok) &&
1016eb633035STom Caputi 	    to_ds->ds_feature_inuse[SPA_FEATURE_LARGE_BLOCKS])
1017b5152584SMatthew Ahrens 		featureflags |= DMU_BACKUP_FEATURE_LARGE_BLOCKS;
101854811da5SToomas Soome 	if (to_ds->ds_feature_inuse[SPA_FEATURE_LARGE_DNODE])
101954811da5SToomas Soome 		featureflags |= DMU_BACKUP_FEATURE_LARGE_DNODE;
1020eb633035STom Caputi 
1021eb633035STom Caputi 	/* encrypted datasets will not have embedded blocks */
1022eb633035STom Caputi 	if ((embedok || rawok) && !os->os_encrypted &&
10235d7b4d43SMatthew Ahrens 	    spa_feature_is_active(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA)) {
10245d7b4d43SMatthew Ahrens 		featureflags |= DMU_BACKUP_FEATURE_EMBED_DATA;
10255602294fSDan Kimmel 	}
1026eb633035STom Caputi 
1027eb633035STom Caputi 	/* raw send implies compressok */
1028eb633035STom Caputi 	if (compressok || rawok)
10295602294fSDan Kimmel 		featureflags |= DMU_BACKUP_FEATURE_COMPRESSED;
1030eb633035STom Caputi 	if (rawok && os->os_encrypted)
1031eb633035STom Caputi 		featureflags |= DMU_BACKUP_FEATURE_RAW;
1032eb633035STom Caputi 
10335602294fSDan Kimmel 	if ((featureflags &
1034eb633035STom Caputi 	    (DMU_BACKUP_FEATURE_EMBED_DATA | DMU_BACKUP_FEATURE_COMPRESSED |
1035eb633035STom Caputi 	    DMU_BACKUP_FEATURE_RAW)) != 0 &&
1036eb633035STom Caputi 	    spa_feature_is_active(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS)) {
10375602294fSDan Kimmel 		featureflags |= DMU_BACKUP_FEATURE_LZ4;
10385d7b4d43SMatthew Ahrens 	}
10395d7b4d43SMatthew Ahrens 
10409c3fd121SMatthew Ahrens 	if (resumeobj != 0 || resumeoff != 0) {
10419c3fd121SMatthew Ahrens 		featureflags |= DMU_BACKUP_FEATURE_RESUMING;
10429c3fd121SMatthew Ahrens 	}
10439c3fd121SMatthew Ahrens 
10445d7b4d43SMatthew Ahrens 	DMU_SET_FEATUREFLAGS(drr->drr_u.drr_begin.drr_versioninfo,
10455d7b4d43SMatthew Ahrens 	    featureflags);
10465d7b4d43SMatthew Ahrens 
1047efb80947Sahrens 	drr->drr_u.drr_begin.drr_creation_time =
1048a2cdcdd2SPaul Dagnelie 	    dsl_dataset_phys(to_ds)->ds_creation_time;
10493b2aab18SMatthew Ahrens 	drr->drr_u.drr_begin.drr_type = dmu_objset_type(os);
105078f17100SMatthew Ahrens 	if (is_clone)
10513cb34c60Sahrens 		drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CLONE;
1052a2cdcdd2SPaul Dagnelie 	drr->drr_u.drr_begin.drr_toguid = dsl_dataset_phys(to_ds)->ds_guid;
1053a2cdcdd2SPaul Dagnelie 	if (dsl_dataset_phys(to_ds)->ds_flags & DS_FLAG_CI_DATASET)
1054ab04eb8eStimh 		drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CI_DATA;
1055880094b6SAndrew Stormont 	if (zfs_send_set_freerecords_bit)
1056880094b6SAndrew Stormont 		drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_FREERECORDS;
1057ab04eb8eStimh 
1058eb633035STom Caputi 	drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_SPILL_BLOCK;
1059eb633035STom Caputi 
1060a2cdcdd2SPaul Dagnelie 	if (ancestor_zb != NULL) {
1061a2cdcdd2SPaul Dagnelie 		drr->drr_u.drr_begin.drr_fromguid =
1062a2cdcdd2SPaul Dagnelie 		    ancestor_zb->zbm_guid;
1063a2cdcdd2SPaul Dagnelie 		fromtxg = ancestor_zb->zbm_creation_txg;
106478f17100SMatthew Ahrens 	}
1065a2cdcdd2SPaul Dagnelie 	dsl_dataset_name(to_ds, drr->drr_u.drr_begin.drr_toname);
1066a2cdcdd2SPaul Dagnelie 	if (!to_ds->ds_is_snapshot) {
106778f17100SMatthew Ahrens 		(void) strlcat(drr->drr_u.drr_begin.drr_toname, "@--head--",
106878f17100SMatthew Ahrens 		    sizeof (drr->drr_u.drr_begin.drr_toname));
10693b2aab18SMatthew Ahrens 	}
10703cb34c60Sahrens 
10714e3c9f44SBill Pijewski 	dsp = kmem_zalloc(sizeof (dmu_sendarg_t), KM_SLEEP);
10724e3c9f44SBill Pijewski 
10734e3c9f44SBill Pijewski 	dsp->dsa_drr = drr;
10744e3c9f44SBill Pijewski 	dsp->dsa_vp = vp;
10754e3c9f44SBill Pijewski 	dsp->dsa_outfd = outfd;
10764e3c9f44SBill Pijewski 	dsp->dsa_proc = curproc;
10773b2aab18SMatthew Ahrens 	dsp->dsa_os = os;
10784e3c9f44SBill Pijewski 	dsp->dsa_off = off;
1079a2cdcdd2SPaul Dagnelie 	dsp->dsa_toguid = dsl_dataset_phys(to_ds)->ds_guid;
1080eb633035STom Caputi 	dsp->dsa_fromtxg = fromtxg;
10814e3c9f44SBill Pijewski 	dsp->dsa_pending_op = PENDING_NONE;
10825d7b4d43SMatthew Ahrens 	dsp->dsa_featureflags = featureflags;
10839c3fd121SMatthew Ahrens 	dsp->dsa_resume_object = resumeobj;
10849c3fd121SMatthew Ahrens 	dsp->dsa_resume_offset = resumeoff;
10854e3c9f44SBill Pijewski 
1086a2cdcdd2SPaul Dagnelie 	mutex_enter(&to_ds->ds_sendstream_lock);
1087a2cdcdd2SPaul Dagnelie 	list_insert_head(&to_ds->ds_sendstreams, dsp);
1088a2cdcdd2SPaul Dagnelie 	mutex_exit(&to_ds->ds_sendstream_lock);
10894e3c9f44SBill Pijewski 
1090a2cdcdd2SPaul Dagnelie 	dsl_dataset_long_hold(to_ds, FTAG);
1091de8d9cffSMatthew Ahrens 	dsl_pool_rele(dp, tag);
1092de8d9cffSMatthew Ahrens 
10939c3fd121SMatthew Ahrens 	void *payload = NULL;
10949c3fd121SMatthew Ahrens 	size_t payload_len = 0;
1095eb633035STom Caputi 	/* handle features that require a DRR_BEGIN payload */
1096eb633035STom Caputi 	if (featureflags &
1097eb633035STom Caputi 	    (DMU_BACKUP_FEATURE_RESUMING | DMU_BACKUP_FEATURE_RAW)) {
1098eb633035STom Caputi 		nvlist_t *keynvl = NULL;
10999c3fd121SMatthew Ahrens 		nvlist_t *nvl = fnvlist_alloc();
1100eb633035STom Caputi 
1101eb633035STom Caputi 		if (featureflags & DMU_BACKUP_FEATURE_RESUMING) {
1102eb633035STom Caputi 			dmu_object_info_t to_doi;
1103eb633035STom Caputi 			err = dmu_object_info(os, resumeobj, &to_doi);
1104eb633035STom Caputi 			if (err != 0) {
1105eb633035STom Caputi 				fnvlist_free(nvl);
1106eb633035STom Caputi 				goto out;
1107eb633035STom Caputi 			}
1108eb633035STom Caputi 
1109eb633035STom Caputi 			SET_BOOKMARK(&to_arg.resume, to_ds->ds_object,
1110eb633035STom Caputi 			    resumeobj, 0,
1111eb633035STom Caputi 			    resumeoff / to_doi.doi_data_block_size);
1112eb633035STom Caputi 
1113eb633035STom Caputi 			fnvlist_add_uint64(nvl, "resume_object", resumeobj);
1114eb633035STom Caputi 			fnvlist_add_uint64(nvl, "resume_offset", resumeoff);
1115eb633035STom Caputi 		}
1116eb633035STom Caputi 
1117eb633035STom Caputi 		if (featureflags & DMU_BACKUP_FEATURE_RAW) {
1118eb633035STom Caputi 			uint64_t ivset_guid = (ancestor_zb != NULL) ?
1119eb633035STom Caputi 			    ancestor_zb->zbm_ivset_guid : 0;
1120eb633035STom Caputi 
1121eb633035STom Caputi 			ASSERT(os->os_encrypted);
1122eb633035STom Caputi 
1123eb633035STom Caputi 			err = dsl_crypto_populate_key_nvlist(to_ds,
1124eb633035STom Caputi 			    ivset_guid, &keynvl);
1125eb633035STom Caputi 			if (err != 0) {
1126eb633035STom Caputi 				fnvlist_free(nvl);
1127eb633035STom Caputi 				goto out;
1128eb633035STom Caputi 			}
1129eb633035STom Caputi 
1130eb633035STom Caputi 			fnvlist_add_nvlist(nvl, "crypt_keydata", keynvl);
1131eb633035STom Caputi 		}
1132eb633035STom Caputi 
11339c3fd121SMatthew Ahrens 		payload = fnvlist_pack(nvl, &payload_len);
11349c3fd121SMatthew Ahrens 		drr->drr_payloadlen = payload_len;
1135eb633035STom Caputi 		fnvlist_free(keynvl);
11369c3fd121SMatthew Ahrens 		fnvlist_free(nvl);
11379c3fd121SMatthew Ahrens 	}
11389c3fd121SMatthew Ahrens 
11399c3fd121SMatthew Ahrens 	err = dump_record(dsp, payload, payload_len);
11409c3fd121SMatthew Ahrens 	fnvlist_pack_free(payload, payload_len);
11419c3fd121SMatthew Ahrens 	if (err != 0) {
11424e3c9f44SBill Pijewski 		err = dsp->dsa_err;
11434e3c9f44SBill Pijewski 		goto out;
1144efb80947Sahrens 	}
1145efb80947Sahrens 
11466ccda740Sloli 	err = bqueue_init(&to_arg.q,
11476ccda740Sloli 	    MAX(zfs_send_queue_length, 2 * zfs_max_recordsize),
1148a2cdcdd2SPaul Dagnelie 	    offsetof(struct send_block_record, ln));
1149a2cdcdd2SPaul Dagnelie 	to_arg.error_code = 0;
1150a2cdcdd2SPaul Dagnelie 	to_arg.cancel = B_FALSE;
1151a2cdcdd2SPaul Dagnelie 	to_arg.ds = to_ds;
1152a2cdcdd2SPaul Dagnelie 	to_arg.fromtxg = fromtxg;
1153a2cdcdd2SPaul Dagnelie 	to_arg.flags = TRAVERSE_PRE | TRAVERSE_PREFETCH;
1154eb633035STom Caputi 	if (rawok)
1155eb633035STom Caputi 		to_arg.flags |= TRAVERSE_NO_DECRYPT;
1156a2cdcdd2SPaul Dagnelie 	(void) thread_create(NULL, 0, send_traverse_thread, &to_arg, 0, curproc,
1157a2cdcdd2SPaul Dagnelie 	    TS_RUN, minclsyspri);
1158a2cdcdd2SPaul Dagnelie 
1159a2cdcdd2SPaul Dagnelie 	struct send_block_record *to_data;
1160a2cdcdd2SPaul Dagnelie 	to_data = bqueue_dequeue(&to_arg.q);
1161a2cdcdd2SPaul Dagnelie 
1162a2cdcdd2SPaul Dagnelie 	while (!to_data->eos_marker && err == 0) {
1163a2cdcdd2SPaul Dagnelie 		err = do_dump(dsp, to_data);
1164a2cdcdd2SPaul Dagnelie 		to_data = get_next_record(&to_arg.q, to_data);
1165a2cdcdd2SPaul Dagnelie 		if (issig(JUSTLOOKING) && issig(FORREAL))
1166a2cdcdd2SPaul Dagnelie 			err = EINTR;
1167a2cdcdd2SPaul Dagnelie 	}
1168a2cdcdd2SPaul Dagnelie 
1169a2cdcdd2SPaul Dagnelie 	if (err != 0) {
1170a2cdcdd2SPaul Dagnelie 		to_arg.cancel = B_TRUE;
1171a2cdcdd2SPaul Dagnelie 		while (!to_data->eos_marker) {
1172a2cdcdd2SPaul Dagnelie 			to_data = get_next_record(&to_arg.q, to_data);
1173a2cdcdd2SPaul Dagnelie 		}
1174a2cdcdd2SPaul Dagnelie 	}
1175a2cdcdd2SPaul Dagnelie 	kmem_free(to_data, sizeof (*to_data));
1176a2cdcdd2SPaul Dagnelie 
1177a2cdcdd2SPaul Dagnelie 	bqueue_destroy(&to_arg.q);
1178a2cdcdd2SPaul Dagnelie 
1179a2cdcdd2SPaul Dagnelie 	if (err == 0 && to_arg.error_code != 0)
1180a2cdcdd2SPaul Dagnelie 		err = to_arg.error_code;
1181a2cdcdd2SPaul Dagnelie 
1182a2cdcdd2SPaul Dagnelie 	if (err != 0)
1183a2cdcdd2SPaul Dagnelie 		goto out;
1184efb80947Sahrens 
11854e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE)
118698110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
1187be6fd75aSMatthew Ahrens 			err = SET_ERROR(EINTR);
11889e69d7d0SLori Alt 
11893b2aab18SMatthew Ahrens 	if (err != 0) {
11903b2aab18SMatthew Ahrens 		if (err == EINTR && dsp->dsa_err != 0)
11914e3c9f44SBill Pijewski 			err = dsp->dsa_err;
11924e3c9f44SBill Pijewski 		goto out;
1193efb80947Sahrens 	}
1194efb80947Sahrens 
1195efb80947Sahrens 	bzero(drr, sizeof (dmu_replay_record_t));
1196efb80947Sahrens 	drr->drr_type = DRR_END;
11974e3c9f44SBill Pijewski 	drr->drr_u.drr_end.drr_checksum = dsp->dsa_zc;
11984e3c9f44SBill Pijewski 	drr->drr_u.drr_end.drr_toguid = dsp->dsa_toguid;
1199efb80947Sahrens 
1200a2cdcdd2SPaul Dagnelie 	if (dump_record(dsp, NULL, 0) != 0)
12014e3c9f44SBill Pijewski 		err = dsp->dsa_err;
12024e3c9f44SBill Pijewski out:
1203a2cdcdd2SPaul Dagnelie 	mutex_enter(&to_ds->ds_sendstream_lock);
1204a2cdcdd2SPaul Dagnelie 	list_remove(&to_ds->ds_sendstreams, dsp);
1205a2cdcdd2SPaul Dagnelie 	mutex_exit(&to_ds->ds_sendstream_lock);
12064e3c9f44SBill Pijewski 
120712b90ee2SMatt Krantz 	VERIFY(err != 0 || (dsp->dsa_sent_begin && dsp->dsa_sent_end));
120812b90ee2SMatt Krantz 
1209efb80947Sahrens 	kmem_free(drr, sizeof (dmu_replay_record_t));
12104e3c9f44SBill Pijewski 	kmem_free(dsp, sizeof (dmu_sendarg_t));
1211efb80947Sahrens 
1212a2cdcdd2SPaul Dagnelie 	dsl_dataset_long_rele(to_ds, FTAG);
12133b2aab18SMatthew Ahrens 
12144e3c9f44SBill Pijewski 	return (err);
1215efb80947Sahrens }
1216efb80947Sahrens 
121719b94df9SMatthew Ahrens int
dmu_send_obj(const char * pool,uint64_t tosnap,uint64_t fromsnap,boolean_t embedok,boolean_t large_block_ok,boolean_t compressok,boolean_t rawok,int outfd,vnode_t * vp,offset_t * off)12183b2aab18SMatthew Ahrens dmu_send_obj(const char *pool, uint64_t tosnap, uint64_t fromsnap,
12195602294fSDan Kimmel     boolean_t embedok, boolean_t large_block_ok, boolean_t compressok,
1220eb633035STom Caputi     boolean_t rawok, int outfd, vnode_t *vp, offset_t *off)
12213b2aab18SMatthew Ahrens {
12223b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
12233b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
12243b2aab18SMatthew Ahrens 	dsl_dataset_t *fromds = NULL;
1225*ef96fc31SToomas Soome 	ds_hold_flags_t dsflags;
12263b2aab18SMatthew Ahrens 	int err;
12273b2aab18SMatthew Ahrens 
1228*ef96fc31SToomas Soome 	dsflags = (rawok) ? DS_HOLD_FLAG_NONE : DS_HOLD_FLAG_DECRYPT;
12293b2aab18SMatthew Ahrens 	err = dsl_pool_hold(pool, FTAG, &dp);
12303b2aab18SMatthew Ahrens 	if (err != 0)
12313b2aab18SMatthew Ahrens 		return (err);
12323b2aab18SMatthew Ahrens 
1233eb633035STom Caputi 	err = dsl_dataset_hold_obj_flags(dp, tosnap, dsflags, FTAG, &ds);
12343b2aab18SMatthew Ahrens 	if (err != 0) {
12353b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
12363b2aab18SMatthew Ahrens 		return (err);
12373b2aab18SMatthew Ahrens 	}
12383b2aab18SMatthew Ahrens 
12393b2aab18SMatthew Ahrens 	if (fromsnap != 0) {
1240eb633035STom Caputi 		zfs_bookmark_phys_t zb = { 0 };
124178f17100SMatthew Ahrens 		boolean_t is_clone;
124278f17100SMatthew Ahrens 
12433b2aab18SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp, fromsnap, FTAG, &fromds);
12443b2aab18SMatthew Ahrens 		if (err != 0) {
1245eb633035STom Caputi 			dsl_dataset_rele_flags(ds, dsflags, FTAG);
12463b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
12473b2aab18SMatthew Ahrens 			return (err);
12483b2aab18SMatthew Ahrens 		}
1249eb633035STom Caputi 		if (!dsl_dataset_is_before(ds, fromds, 0)) {
125078f17100SMatthew Ahrens 			err = SET_ERROR(EXDEV);
1251eb633035STom Caputi 			dsl_dataset_rele(fromds, FTAG);
1252eb633035STom Caputi 			dsl_dataset_rele_flags(ds, dsflags, FTAG);
1253eb633035STom Caputi 			dsl_pool_rele(dp, FTAG);
1254eb633035STom Caputi 			return (err);
1255eb633035STom Caputi 		}
1256eb633035STom Caputi 
1257c1379625SJustin T. Gibbs 		zb.zbm_creation_time =
1258c1379625SJustin T. Gibbs 		    dsl_dataset_phys(fromds)->ds_creation_time;
1259c1379625SJustin T. Gibbs 		zb.zbm_creation_txg = dsl_dataset_phys(fromds)->ds_creation_txg;
1260c1379625SJustin T. Gibbs 		zb.zbm_guid = dsl_dataset_phys(fromds)->ds_guid;
1261eb633035STom Caputi 
1262eb633035STom Caputi 		if (dsl_dataset_is_zapified(fromds)) {
1263eb633035STom Caputi 			(void) zap_lookup(dp->dp_meta_objset,
1264eb633035STom Caputi 			    fromds->ds_object, DS_FIELD_IVSET_GUID, 8, 1,
1265eb633035STom Caputi 			    &zb.zbm_ivset_guid);
1266eb633035STom Caputi 		}
1267eb633035STom Caputi 
126878f17100SMatthew Ahrens 		is_clone = (fromds->ds_dir != ds->ds_dir);
126978f17100SMatthew Ahrens 		dsl_dataset_rele(fromds, FTAG);
1270b5152584SMatthew Ahrens 		err = dmu_send_impl(FTAG, dp, ds, &zb, is_clone,
1271eb633035STom Caputi 		    embedok, large_block_ok, compressok, rawok, outfd,
1272eb633035STom Caputi 		    0, 0, vp, off);
127378f17100SMatthew Ahrens 	} else {
1274b5152584SMatthew Ahrens 		err = dmu_send_impl(FTAG, dp, ds, NULL, B_FALSE,
1275eb633035STom Caputi 		    embedok, large_block_ok, compressok, rawok, outfd,
1276eb633035STom Caputi 		    0, 0, vp, off);
12773b2aab18SMatthew Ahrens 	}
1278eb633035STom Caputi 	dsl_dataset_rele_flags(ds, dsflags, FTAG);
127978f17100SMatthew Ahrens 	return (err);
12803b2aab18SMatthew Ahrens }
12813b2aab18SMatthew Ahrens 
12823b2aab18SMatthew Ahrens int
dmu_send(const char * tosnap,const char * fromsnap,boolean_t embedok,boolean_t large_block_ok,boolean_t compressok,boolean_t rawok,int outfd,uint64_t resumeobj,uint64_t resumeoff,vnode_t * vp,offset_t * off)12839c3fd121SMatthew Ahrens dmu_send(const char *tosnap, const char *fromsnap, boolean_t embedok,
1284eb633035STom Caputi     boolean_t large_block_ok, boolean_t compressok, boolean_t rawok,
1285eb633035STom Caputi     int outfd, uint64_t resumeobj, uint64_t resumeoff, vnode_t *vp,
1286eb633035STom Caputi     offset_t *off)
12873b2aab18SMatthew Ahrens {
12883b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
12893b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
12903b2aab18SMatthew Ahrens 	int err;
1291*ef96fc31SToomas Soome 	ds_hold_flags_t dsflags;
129278f17100SMatthew Ahrens 	boolean_t owned = B_FALSE;
12933b2aab18SMatthew Ahrens 
1294*ef96fc31SToomas Soome 	dsflags = (rawok) ? DS_HOLD_FLAG_NONE : DS_HOLD_FLAG_DECRYPT;
129578f17100SMatthew Ahrens 	if (fromsnap != NULL && strpbrk(fromsnap, "@#") == NULL)
1296be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
12973b2aab18SMatthew Ahrens 
12983b2aab18SMatthew Ahrens 	err = dsl_pool_hold(tosnap, FTAG, &dp);
12993b2aab18SMatthew Ahrens 	if (err != 0)
13003b2aab18SMatthew Ahrens 		return (err);
130178f17100SMatthew Ahrens 	if (strchr(tosnap, '@') == NULL && spa_writeable(dp->dp_spa)) {
130278f17100SMatthew Ahrens 		/*
130378f17100SMatthew Ahrens 		 * We are sending a filesystem or volume.  Ensure
130478f17100SMatthew Ahrens 		 * that it doesn't change by owning the dataset.
130578f17100SMatthew Ahrens 		 */
1306eb633035STom Caputi 		err = dsl_dataset_own(dp, tosnap, dsflags, FTAG, &ds);
130778f17100SMatthew Ahrens 		owned = B_TRUE;
130878f17100SMatthew Ahrens 	} else {
1309eb633035STom Caputi 		err = dsl_dataset_hold_flags(dp, tosnap, dsflags, FTAG, &ds);
131078f17100SMatthew Ahrens 	}
13113b2aab18SMatthew Ahrens 	if (err != 0) {
13123b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
13133b2aab18SMatthew Ahrens 		return (err);
13143b2aab18SMatthew Ahrens 	}
13153b2aab18SMatthew Ahrens 
13163b2aab18SMatthew Ahrens 	if (fromsnap != NULL) {
1317eb633035STom Caputi 		zfs_bookmark_phys_t zb = { 0 };
131878f17100SMatthew Ahrens 		boolean_t is_clone = B_FALSE;
131978f17100SMatthew Ahrens 		int fsnamelen = strchr(tosnap, '@') - tosnap;
132078f17100SMatthew Ahrens 
132178f17100SMatthew Ahrens 		/*
132278f17100SMatthew Ahrens 		 * If the fromsnap is in a different filesystem, then
132378f17100SMatthew Ahrens 		 * mark the send stream as a clone.
132478f17100SMatthew Ahrens 		 */
132578f17100SMatthew Ahrens 		if (strncmp(tosnap, fromsnap, fsnamelen) != 0 ||
132678f17100SMatthew Ahrens 		    (fromsnap[fsnamelen] != '@' &&
132778f17100SMatthew Ahrens 		    fromsnap[fsnamelen] != '#')) {
132878f17100SMatthew Ahrens 			is_clone = B_TRUE;
132978f17100SMatthew Ahrens 		}
133078f17100SMatthew Ahrens 
133178f17100SMatthew Ahrens 		if (strchr(fromsnap, '@')) {
133278f17100SMatthew Ahrens 			dsl_dataset_t *fromds;
133378f17100SMatthew Ahrens 			err = dsl_dataset_hold(dp, fromsnap, FTAG, &fromds);
133478f17100SMatthew Ahrens 			if (err == 0) {
133578f17100SMatthew Ahrens 				if (!dsl_dataset_is_before(ds, fromds, 0))
133678f17100SMatthew Ahrens 					err = SET_ERROR(EXDEV);
133778f17100SMatthew Ahrens 				zb.zbm_creation_time =
1338c1379625SJustin T. Gibbs 				    dsl_dataset_phys(fromds)->ds_creation_time;
133978f17100SMatthew Ahrens 				zb.zbm_creation_txg =
1340c1379625SJustin T. Gibbs 				    dsl_dataset_phys(fromds)->ds_creation_txg;
1341c1379625SJustin T. Gibbs 				zb.zbm_guid = dsl_dataset_phys(fromds)->ds_guid;
134278f17100SMatthew Ahrens 				is_clone = (ds->ds_dir != fromds->ds_dir);
1343eb633035STom Caputi 
1344eb633035STom Caputi 				if (dsl_dataset_is_zapified(fromds)) {
1345eb633035STom Caputi 					(void) zap_lookup(dp->dp_meta_objset,
1346eb633035STom Caputi 					    fromds->ds_object,
1347eb633035STom Caputi 					    DS_FIELD_IVSET_GUID, 8, 1,
1348eb633035STom Caputi 					    &zb.zbm_ivset_guid);
1349eb633035STom Caputi 				}
135078f17100SMatthew Ahrens 				dsl_dataset_rele(fromds, FTAG);
135178f17100SMatthew Ahrens 			}
135278f17100SMatthew Ahrens 		} else {
135378f17100SMatthew Ahrens 			err = dsl_bookmark_lookup(dp, fromsnap, ds, &zb);
135478f17100SMatthew Ahrens 		}
13553b2aab18SMatthew Ahrens 		if (err != 0) {
1356eb633035STom Caputi 			if (owned)
1357eb633035STom Caputi 				dsl_dataset_disown(ds, dsflags, FTAG);
1358eb633035STom Caputi 			else
1359eb633035STom Caputi 				dsl_dataset_rele_flags(ds, dsflags, FTAG);
1360eb633035STom Caputi 
13613b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
13623b2aab18SMatthew Ahrens 			return (err);
13633b2aab18SMatthew Ahrens 		}
1364b5152584SMatthew Ahrens 		err = dmu_send_impl(FTAG, dp, ds, &zb, is_clone,
1365eb633035STom Caputi 		    embedok, large_block_ok, compressok, rawok,
13669c3fd121SMatthew Ahrens 		    outfd, resumeobj, resumeoff, vp, off);
136778f17100SMatthew Ahrens 	} else {
1368b5152584SMatthew Ahrens 		err = dmu_send_impl(FTAG, dp, ds, NULL, B_FALSE,
1369eb633035STom Caputi 		    embedok, large_block_ok, compressok, rawok,
13709c3fd121SMatthew Ahrens 		    outfd, resumeobj, resumeoff, vp, off);
13713b2aab18SMatthew Ahrens 	}
137278f17100SMatthew Ahrens 	if (owned)
1373eb633035STom Caputi 		dsl_dataset_disown(ds, dsflags, FTAG);
137478f17100SMatthew Ahrens 	else
1375eb633035STom Caputi 		dsl_dataset_rele_flags(ds, dsflags, FTAG);
1376eb633035STom Caputi 
137778f17100SMatthew Ahrens 	return (err);
13783b2aab18SMatthew Ahrens }
13793b2aab18SMatthew Ahrens 
1380643da460SMax Grossman static int
dmu_adjust_send_estimate_for_indirects(dsl_dataset_t * ds,uint64_t uncompressed,uint64_t compressed,boolean_t stream_compressed,uint64_t * sizep)13815602294fSDan Kimmel dmu_adjust_send_estimate_for_indirects(dsl_dataset_t *ds, uint64_t uncompressed,
13825602294fSDan Kimmel     uint64_t compressed, boolean_t stream_compressed, uint64_t *sizep)
1383643da460SMax Grossman {
1384df477c0aSPaul Dagnelie 	int err = 0;
13855602294fSDan Kimmel 	uint64_t size;
1386643da460SMax Grossman 	/*
1387643da460SMax Grossman 	 * Assume that space (both on-disk and in-stream) is dominated by
1388643da460SMax Grossman 	 * data.  We will adjust for indirect blocks and the copies property,
1389643da460SMax Grossman 	 * but ignore per-object space used (eg, dnodes and DRR_OBJECT records).
1390643da460SMax Grossman 	 */
13915602294fSDan Kimmel 	uint64_t recordsize;
13925602294fSDan Kimmel 	uint64_t record_count;
13930255edccSPaul Dagnelie 	objset_t *os;
13940255edccSPaul Dagnelie 	VERIFY0(dmu_objset_from_ds(ds, &os));
13955602294fSDan Kimmel 
13965602294fSDan Kimmel 	/* Assume all (uncompressed) blocks are recordsize. */
1397df477c0aSPaul Dagnelie 	if (zfs_override_estimate_recordsize != 0) {
1398df477c0aSPaul Dagnelie 		recordsize = zfs_override_estimate_recordsize;
1399df477c0aSPaul Dagnelie 	} else if (os->os_phys->os_type == DMU_OST_ZVOL) {
14000255edccSPaul Dagnelie 		err = dsl_prop_get_int_ds(ds,
14010255edccSPaul Dagnelie 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &recordsize);
14020255edccSPaul Dagnelie 	} else {
14030255edccSPaul Dagnelie 		err = dsl_prop_get_int_ds(ds,
14040255edccSPaul Dagnelie 		    zfs_prop_to_name(ZFS_PROP_RECORDSIZE), &recordsize);
14050255edccSPaul Dagnelie 	}
14065602294fSDan Kimmel 	if (err != 0)
14075602294fSDan Kimmel 		return (err);
14085602294fSDan Kimmel 	record_count = uncompressed / recordsize;
14095602294fSDan Kimmel 
14105602294fSDan Kimmel 	/*
14115602294fSDan Kimmel 	 * If we're estimating a send size for a compressed stream, use the
14125602294fSDan Kimmel 	 * compressed data size to estimate the stream size. Otherwise, use the
14135602294fSDan Kimmel 	 * uncompressed data size.
14145602294fSDan Kimmel 	 */
14155602294fSDan Kimmel 	size = stream_compressed ? compressed : uncompressed;
1416643da460SMax Grossman 
1417643da460SMax Grossman 	/*
1418643da460SMax Grossman 	 * Subtract out approximate space used by indirect blocks.
1419643da460SMax Grossman 	 * Assume most space is used by data blocks (non-indirect, non-dnode).
14205602294fSDan Kimmel 	 * Assume no ditto blocks or internal fragmentation.
1421643da460SMax Grossman 	 *
1422643da460SMax Grossman 	 * Therefore, space used by indirect blocks is sizeof(blkptr_t) per
14235602294fSDan Kimmel 	 * block.
1424643da460SMax Grossman 	 */
14255602294fSDan Kimmel 	size -= record_count * sizeof (blkptr_t);
1426643da460SMax Grossman 
1427643da460SMax Grossman 	/* Add in the space for the record associated with each block. */
14285602294fSDan Kimmel 	size += record_count * sizeof (dmu_replay_record_t);
1429643da460SMax Grossman 
1430643da460SMax Grossman 	*sizep = size;
1431643da460SMax Grossman 
1432643da460SMax Grossman 	return (0);
1433643da460SMax Grossman }
1434643da460SMax Grossman 
14353b2aab18SMatthew Ahrens int
dmu_send_estimate(dsl_dataset_t * ds,dsl_dataset_t * fromds,boolean_t stream_compressed,uint64_t * sizep)14365602294fSDan Kimmel dmu_send_estimate(dsl_dataset_t *ds, dsl_dataset_t *fromds,
14375602294fSDan Kimmel     boolean_t stream_compressed, uint64_t *sizep)
143819b94df9SMatthew Ahrens {
143919b94df9SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
144019b94df9SMatthew Ahrens 	int err;
14415602294fSDan Kimmel 	uint64_t uncomp, comp;
144219b94df9SMatthew Ahrens 
14433b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
14443b2aab18SMatthew Ahrens 
144519b94df9SMatthew Ahrens 	/* tosnap must be a snapshot */
1446bc9014e6SJustin Gibbs 	if (!ds->ds_is_snapshot)
1447be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
144819b94df9SMatthew Ahrens 
144924218bebSAndriy Gapon 	/* fromsnap, if provided, must be a snapshot */
145024218bebSAndriy Gapon 	if (fromds != NULL && !fromds->ds_is_snapshot)
145124218bebSAndriy Gapon 		return (SET_ERROR(EINVAL));
145224218bebSAndriy Gapon 
14534445fffbSMatthew Ahrens 	/*
14544445fffbSMatthew Ahrens 	 * fromsnap must be an earlier snapshot from the same fs as tosnap,
14554445fffbSMatthew Ahrens 	 * or the origin's fs.
14564445fffbSMatthew Ahrens 	 */
145778f17100SMatthew Ahrens 	if (fromds != NULL && !dsl_dataset_is_before(ds, fromds, 0))
1458be6fd75aSMatthew Ahrens 		return (SET_ERROR(EXDEV));
145919b94df9SMatthew Ahrens 
14605602294fSDan Kimmel 	/* Get compressed and uncompressed size estimates of changed data. */
146119b94df9SMatthew Ahrens 	if (fromds == NULL) {
14625602294fSDan Kimmel 		uncomp = dsl_dataset_phys(ds)->ds_uncompressed_bytes;
14635602294fSDan Kimmel 		comp = dsl_dataset_phys(ds)->ds_compressed_bytes;
146419b94df9SMatthew Ahrens 	} else {
14655602294fSDan Kimmel 		uint64_t used;
146619b94df9SMatthew Ahrens 		err = dsl_dataset_space_written(fromds, ds,
14675602294fSDan Kimmel 		    &used, &comp, &uncomp);
14683b2aab18SMatthew Ahrens 		if (err != 0)
146919b94df9SMatthew Ahrens 			return (err);
147019b94df9SMatthew Ahrens 	}
147119b94df9SMatthew Ahrens 
14725602294fSDan Kimmel 	err = dmu_adjust_send_estimate_for_indirects(ds, uncomp, comp,
14735602294fSDan Kimmel 	    stream_compressed, sizep);
14740255edccSPaul Dagnelie 	/*
14750255edccSPaul Dagnelie 	 * Add the size of the BEGIN and END records to the estimate.
14760255edccSPaul Dagnelie 	 */
14770255edccSPaul Dagnelie 	*sizep += 2 * sizeof (dmu_replay_record_t);
1478643da460SMax Grossman 	return (err);
1479643da460SMax Grossman }
1480643da460SMax Grossman 
14815602294fSDan Kimmel struct calculate_send_arg {
14825602294fSDan Kimmel 	uint64_t uncompressed;
14835602294fSDan Kimmel 	uint64_t compressed;
14845602294fSDan Kimmel };
14855602294fSDan Kimmel 
1486643da460SMax Grossman /*
1487643da460SMax Grossman  * Simple callback used to traverse the blocks of a snapshot and sum their
14885602294fSDan Kimmel  * uncompressed and compressed sizes.
1489643da460SMax Grossman  */
1490643da460SMax Grossman /* ARGSUSED */
1491643da460SMax Grossman static int
dmu_calculate_send_traversal(spa_t * spa,zilog_t * zilog,const blkptr_t * bp,const zbookmark_phys_t * zb,const dnode_phys_t * dnp,void * arg)1492643da460SMax Grossman dmu_calculate_send_traversal(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1493643da460SMax Grossman     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
1494643da460SMax Grossman {
14955602294fSDan Kimmel 	struct calculate_send_arg *space = arg;
1496643da460SMax Grossman 	if (bp != NULL && !BP_IS_HOLE(bp)) {
14975602294fSDan Kimmel 		space->uncompressed += BP_GET_UCSIZE(bp);
14985602294fSDan Kimmel 		space->compressed += BP_GET_PSIZE(bp);
1499643da460SMax Grossman 	}
1500643da460SMax Grossman 	return (0);
1501643da460SMax Grossman }
1502643da460SMax Grossman 
1503643da460SMax Grossman /*
1504643da460SMax Grossman  * Given a desination snapshot and a TXG, calculate the approximate size of a
1505643da460SMax Grossman  * send stream sent from that TXG. from_txg may be zero, indicating that the
1506643da460SMax Grossman  * whole snapshot will be sent.
1507643da460SMax Grossman  */
1508643da460SMax Grossman int
dmu_send_estimate_from_txg(dsl_dataset_t * ds,uint64_t from_txg,boolean_t stream_compressed,uint64_t * sizep)1509643da460SMax Grossman dmu_send_estimate_from_txg(dsl_dataset_t *ds, uint64_t from_txg,
15105602294fSDan Kimmel     boolean_t stream_compressed, uint64_t *sizep)
1511643da460SMax Grossman {
1512643da460SMax Grossman 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1513643da460SMax Grossman 	int err;
15145602294fSDan Kimmel 	struct calculate_send_arg size = { 0 };
1515643da460SMax Grossman 
1516643da460SMax Grossman 	ASSERT(dsl_pool_config_held(dp));
1517643da460SMax Grossman 
1518643da460SMax Grossman 	/* tosnap must be a snapshot */
15195602294fSDan Kimmel 	if (!ds->ds_is_snapshot)
1520643da460SMax Grossman 		return (SET_ERROR(EINVAL));
1521643da460SMax Grossman 
1522643da460SMax Grossman 	/* verify that from_txg is before the provided snapshot was taken */
1523643da460SMax Grossman 	if (from_txg >= dsl_dataset_phys(ds)->ds_creation_txg) {
1524643da460SMax Grossman 		return (SET_ERROR(EXDEV));
1525643da460SMax Grossman 	}
152619b94df9SMatthew Ahrens 
152719b94df9SMatthew Ahrens 	/*
1528643da460SMax Grossman 	 * traverse the blocks of the snapshot with birth times after
1529643da460SMax Grossman 	 * from_txg, summing their uncompressed size
153019b94df9SMatthew Ahrens 	 */
1531eb633035STom Caputi 	err = traverse_dataset(ds, from_txg,
1532eb633035STom Caputi 	    TRAVERSE_POST | TRAVERSE_NO_DECRYPT,
1533643da460SMax Grossman 	    dmu_calculate_send_traversal, &size);
1534643da460SMax Grossman 	if (err)
153519b94df9SMatthew Ahrens 		return (err);
153619b94df9SMatthew Ahrens 
15375602294fSDan Kimmel 	err = dmu_adjust_send_estimate_for_indirects(ds, size.uncompressed,
15385602294fSDan Kimmel 	    size.compressed, stream_compressed, sizep);
1539643da460SMax Grossman 	return (err);
154019b94df9SMatthew Ahrens }
1541