xref: /freebsd-src/sys/contrib/openzfs/module/zfs/dmu_send.c (revision 15f0b8c309dea1dcb14d3e374686576ff68ac43f)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy /*
22eda14cbcSMatt Macy  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23eda14cbcSMatt Macy  * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24eda14cbcSMatt Macy  * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
25eda14cbcSMatt Macy  * Copyright (c) 2014, Joyent, Inc. All rights reserved.
26eda14cbcSMatt Macy  * Copyright 2014 HybridCluster. All rights reserved.
27eda14cbcSMatt Macy  * Copyright 2016 RackTop Systems.
28eda14cbcSMatt Macy  * Copyright (c) 2016 Actifio, Inc. All rights reserved.
29eda14cbcSMatt Macy  * Copyright (c) 2019, Klara Inc.
30eda14cbcSMatt Macy  * Copyright (c) 2019, Allan Jude
31eda14cbcSMatt Macy  */
32eda14cbcSMatt Macy 
33eda14cbcSMatt Macy #include <sys/dmu.h>
34eda14cbcSMatt Macy #include <sys/dmu_impl.h>
35eda14cbcSMatt Macy #include <sys/dmu_tx.h>
36eda14cbcSMatt Macy #include <sys/dbuf.h>
37eda14cbcSMatt Macy #include <sys/dnode.h>
38eda14cbcSMatt Macy #include <sys/zfs_context.h>
39eda14cbcSMatt Macy #include <sys/dmu_objset.h>
40eda14cbcSMatt Macy #include <sys/dmu_traverse.h>
41eda14cbcSMatt Macy #include <sys/dsl_dataset.h>
42eda14cbcSMatt Macy #include <sys/dsl_dir.h>
43eda14cbcSMatt Macy #include <sys/dsl_prop.h>
44eda14cbcSMatt Macy #include <sys/dsl_pool.h>
45eda14cbcSMatt Macy #include <sys/dsl_synctask.h>
46eda14cbcSMatt Macy #include <sys/spa_impl.h>
47eda14cbcSMatt Macy #include <sys/zfs_ioctl.h>
48eda14cbcSMatt Macy #include <sys/zap.h>
49eda14cbcSMatt Macy #include <sys/zio_checksum.h>
50eda14cbcSMatt Macy #include <sys/zfs_znode.h>
51eda14cbcSMatt Macy #include <zfs_fletcher.h>
52eda14cbcSMatt Macy #include <sys/avl.h>
53eda14cbcSMatt Macy #include <sys/ddt.h>
54eda14cbcSMatt Macy #include <sys/zfs_onexit.h>
55eda14cbcSMatt Macy #include <sys/dmu_send.h>
56eda14cbcSMatt Macy #include <sys/dmu_recv.h>
57eda14cbcSMatt Macy #include <sys/dsl_destroy.h>
58eda14cbcSMatt Macy #include <sys/blkptr.h>
59eda14cbcSMatt Macy #include <sys/dsl_bookmark.h>
60eda14cbcSMatt Macy #include <sys/zfeature.h>
61eda14cbcSMatt Macy #include <sys/bqueue.h>
62eda14cbcSMatt Macy #include <sys/zvol.h>
63eda14cbcSMatt Macy #include <sys/policy.h>
64eda14cbcSMatt Macy #include <sys/objlist.h>
65eda14cbcSMatt Macy #ifdef _KERNEL
66eda14cbcSMatt Macy #include <sys/zfs_vfsops.h>
67eda14cbcSMatt Macy #endif
68eda14cbcSMatt Macy 
69eda14cbcSMatt Macy /* Set this tunable to TRUE to replace corrupt data with 0x2f5baddb10c */
70e92ffd9bSMartin Matuska static int zfs_send_corrupt_data = B_FALSE;
71eda14cbcSMatt Macy /*
72eda14cbcSMatt Macy  * This tunable controls the amount of data (measured in bytes) that will be
73eda14cbcSMatt Macy  * prefetched by zfs send.  If the main thread is blocking on reads that haven't
74eda14cbcSMatt Macy  * completed, this variable might need to be increased.  If instead the main
75eda14cbcSMatt Macy  * thread is issuing new reads because the prefetches have fallen out of the
76eda14cbcSMatt Macy  * cache, this may need to be decreased.
77eda14cbcSMatt Macy  */
78be181ee2SMartin Matuska static uint_t zfs_send_queue_length = SPA_MAXBLOCKSIZE;
79eda14cbcSMatt Macy /*
80eda14cbcSMatt Macy  * This tunable controls the length of the queues that zfs send worker threads
81eda14cbcSMatt Macy  * use to communicate.  If the send_main_thread is blocking on these queues,
82eda14cbcSMatt Macy  * this variable may need to be increased.  If there is a significant slowdown
83eda14cbcSMatt Macy  * at the start of a send as these threads consume all the available IO
84eda14cbcSMatt Macy  * resources, this variable may need to be decreased.
85eda14cbcSMatt Macy  */
86be181ee2SMartin Matuska static uint_t zfs_send_no_prefetch_queue_length = 1024 * 1024;
87eda14cbcSMatt Macy /*
88eda14cbcSMatt Macy  * These tunables control the fill fraction of the queues by zfs send.  The fill
89eda14cbcSMatt Macy  * fraction controls the frequency with which threads have to be cv_signaled.
90eda14cbcSMatt Macy  * If a lot of cpu time is being spent on cv_signal, then these should be tuned
91eda14cbcSMatt Macy  * down.  If the queues empty before the signalled thread can catch up, then
92eda14cbcSMatt Macy  * these should be tuned up.
93eda14cbcSMatt Macy  */
94be181ee2SMartin Matuska static uint_t zfs_send_queue_ff = 20;
95be181ee2SMartin Matuska static uint_t zfs_send_no_prefetch_queue_ff = 20;
96eda14cbcSMatt Macy 
97eda14cbcSMatt Macy /*
98eda14cbcSMatt Macy  * Use this to override the recordsize calculation for fast zfs send estimates.
99eda14cbcSMatt Macy  */
100be181ee2SMartin Matuska static uint_t zfs_override_estimate_recordsize = 0;
101eda14cbcSMatt Macy 
102eda14cbcSMatt Macy /* Set this tunable to FALSE to disable setting of DRR_FLAG_FREERECORDS */
103e92ffd9bSMartin Matuska static const boolean_t zfs_send_set_freerecords_bit = B_TRUE;
104eda14cbcSMatt Macy 
105eda14cbcSMatt Macy /* Set this tunable to FALSE is disable sending unmodified spill blocks. */
106e92ffd9bSMartin Matuska static int zfs_send_unmodified_spill_blocks = B_TRUE;
107eda14cbcSMatt Macy 
108eda14cbcSMatt Macy static inline boolean_t
109eda14cbcSMatt Macy overflow_multiply(uint64_t a, uint64_t b, uint64_t *c)
110eda14cbcSMatt Macy {
111eda14cbcSMatt Macy 	uint64_t temp = a * b;
112eda14cbcSMatt Macy 	if (b != 0 && temp / b != a)
113eda14cbcSMatt Macy 		return (B_FALSE);
114eda14cbcSMatt Macy 	*c = temp;
115eda14cbcSMatt Macy 	return (B_TRUE);
116eda14cbcSMatt Macy }
117eda14cbcSMatt Macy 
118eda14cbcSMatt Macy struct send_thread_arg {
119eda14cbcSMatt Macy 	bqueue_t	q;
120eda14cbcSMatt Macy 	objset_t	*os;		/* Objset to traverse */
121eda14cbcSMatt Macy 	uint64_t	fromtxg;	/* Traverse from this txg */
122eda14cbcSMatt Macy 	int		flags;		/* flags to pass to traverse_dataset */
123eda14cbcSMatt Macy 	int		error_code;
124eda14cbcSMatt Macy 	boolean_t	cancel;
125eda14cbcSMatt Macy 	zbookmark_phys_t resume;
126eda14cbcSMatt Macy 	uint64_t	*num_blocks_visited;
127eda14cbcSMatt Macy };
128eda14cbcSMatt Macy 
129eda14cbcSMatt Macy struct redact_list_thread_arg {
130eda14cbcSMatt Macy 	boolean_t		cancel;
131eda14cbcSMatt Macy 	bqueue_t		q;
132eda14cbcSMatt Macy 	zbookmark_phys_t	resume;
133eda14cbcSMatt Macy 	redaction_list_t	*rl;
134eda14cbcSMatt Macy 	boolean_t		mark_redact;
135eda14cbcSMatt Macy 	int			error_code;
136eda14cbcSMatt Macy 	uint64_t		*num_blocks_visited;
137eda14cbcSMatt Macy };
138eda14cbcSMatt Macy 
139eda14cbcSMatt Macy struct send_merge_thread_arg {
140eda14cbcSMatt Macy 	bqueue_t			q;
141eda14cbcSMatt Macy 	objset_t			*os;
142eda14cbcSMatt Macy 	struct redact_list_thread_arg	*from_arg;
143eda14cbcSMatt Macy 	struct send_thread_arg		*to_arg;
144eda14cbcSMatt Macy 	struct redact_list_thread_arg	*redact_arg;
145eda14cbcSMatt Macy 	int				error;
146eda14cbcSMatt Macy 	boolean_t			cancel;
147eda14cbcSMatt Macy };
148eda14cbcSMatt Macy 
149eda14cbcSMatt Macy struct send_range {
150eda14cbcSMatt Macy 	boolean_t		eos_marker; /* Marks the end of the stream */
151eda14cbcSMatt Macy 	uint64_t		object;
152eda14cbcSMatt Macy 	uint64_t		start_blkid;
153eda14cbcSMatt Macy 	uint64_t		end_blkid;
154eda14cbcSMatt Macy 	bqueue_node_t		ln;
155eda14cbcSMatt Macy 	enum type {DATA, HOLE, OBJECT, OBJECT_RANGE, REDACT,
156eda14cbcSMatt Macy 	    PREVIOUSLY_REDACTED} type;
157eda14cbcSMatt Macy 	union {
158eda14cbcSMatt Macy 		struct srd {
159eda14cbcSMatt Macy 			dmu_object_type_t	obj_type;
160eda14cbcSMatt Macy 			uint32_t		datablksz; // logical size
161eda14cbcSMatt Macy 			uint32_t		datasz; // payload size
162eda14cbcSMatt Macy 			blkptr_t		bp;
163eda14cbcSMatt Macy 			arc_buf_t		*abuf;
164eda14cbcSMatt Macy 			abd_t			*abd;
165eda14cbcSMatt Macy 			kmutex_t		lock;
166eda14cbcSMatt Macy 			kcondvar_t		cv;
167eda14cbcSMatt Macy 			boolean_t		io_outstanding;
168681ce946SMartin Matuska 			boolean_t		io_compressed;
169eda14cbcSMatt Macy 			int			io_err;
170eda14cbcSMatt Macy 		} data;
171eda14cbcSMatt Macy 		struct srh {
172eda14cbcSMatt Macy 			uint32_t		datablksz;
173eda14cbcSMatt Macy 		} hole;
174eda14cbcSMatt Macy 		struct sro {
175eda14cbcSMatt Macy 			/*
176eda14cbcSMatt Macy 			 * This is a pointer because embedding it in the
177eda14cbcSMatt Macy 			 * struct causes these structures to be massively larger
178eda14cbcSMatt Macy 			 * for all range types; this makes the code much less
179eda14cbcSMatt Macy 			 * memory efficient.
180eda14cbcSMatt Macy 			 */
181eda14cbcSMatt Macy 			dnode_phys_t		*dnp;
182eda14cbcSMatt Macy 			blkptr_t		bp;
183eda14cbcSMatt Macy 		} object;
184eda14cbcSMatt Macy 		struct srr {
185eda14cbcSMatt Macy 			uint32_t		datablksz;
186eda14cbcSMatt Macy 		} redact;
187eda14cbcSMatt Macy 		struct sror {
188eda14cbcSMatt Macy 			blkptr_t		bp;
189eda14cbcSMatt Macy 		} object_range;
190eda14cbcSMatt Macy 	} sru;
191eda14cbcSMatt Macy };
192eda14cbcSMatt Macy 
193eda14cbcSMatt Macy /*
194eda14cbcSMatt Macy  * The list of data whose inclusion in a send stream can be pending from
195eda14cbcSMatt Macy  * one call to backup_cb to another.  Multiple calls to dump_free(),
196eda14cbcSMatt Macy  * dump_freeobjects(), and dump_redact() can be aggregated into a single
197eda14cbcSMatt Macy  * DRR_FREE, DRR_FREEOBJECTS, or DRR_REDACT replay record.
198eda14cbcSMatt Macy  */
199eda14cbcSMatt Macy typedef enum {
200eda14cbcSMatt Macy 	PENDING_NONE,
201eda14cbcSMatt Macy 	PENDING_FREE,
202eda14cbcSMatt Macy 	PENDING_FREEOBJECTS,
203eda14cbcSMatt Macy 	PENDING_REDACT
204eda14cbcSMatt Macy } dmu_pendop_t;
205eda14cbcSMatt Macy 
206eda14cbcSMatt Macy typedef struct dmu_send_cookie {
207eda14cbcSMatt Macy 	dmu_replay_record_t *dsc_drr;
208eda14cbcSMatt Macy 	dmu_send_outparams_t *dsc_dso;
209eda14cbcSMatt Macy 	offset_t *dsc_off;
210eda14cbcSMatt Macy 	objset_t *dsc_os;
211eda14cbcSMatt Macy 	zio_cksum_t dsc_zc;
212eda14cbcSMatt Macy 	uint64_t dsc_toguid;
213eda14cbcSMatt Macy 	uint64_t dsc_fromtxg;
214eda14cbcSMatt Macy 	int dsc_err;
215eda14cbcSMatt Macy 	dmu_pendop_t dsc_pending_op;
216eda14cbcSMatt Macy 	uint64_t dsc_featureflags;
217eda14cbcSMatt Macy 	uint64_t dsc_last_data_object;
218eda14cbcSMatt Macy 	uint64_t dsc_last_data_offset;
219eda14cbcSMatt Macy 	uint64_t dsc_resume_object;
220eda14cbcSMatt Macy 	uint64_t dsc_resume_offset;
221eda14cbcSMatt Macy 	boolean_t dsc_sent_begin;
222eda14cbcSMatt Macy 	boolean_t dsc_sent_end;
223eda14cbcSMatt Macy } dmu_send_cookie_t;
224eda14cbcSMatt Macy 
225eda14cbcSMatt Macy static int do_dump(dmu_send_cookie_t *dscp, struct send_range *range);
226eda14cbcSMatt Macy 
227eda14cbcSMatt Macy static void
228eda14cbcSMatt Macy range_free(struct send_range *range)
229eda14cbcSMatt Macy {
230eda14cbcSMatt Macy 	if (range->type == OBJECT) {
231eda14cbcSMatt Macy 		size_t size = sizeof (dnode_phys_t) *
232eda14cbcSMatt Macy 		    (range->sru.object.dnp->dn_extra_slots + 1);
233eda14cbcSMatt Macy 		kmem_free(range->sru.object.dnp, size);
234eda14cbcSMatt Macy 	} else if (range->type == DATA) {
235eda14cbcSMatt Macy 		mutex_enter(&range->sru.data.lock);
236eda14cbcSMatt Macy 		while (range->sru.data.io_outstanding)
237eda14cbcSMatt Macy 			cv_wait(&range->sru.data.cv, &range->sru.data.lock);
238eda14cbcSMatt Macy 		if (range->sru.data.abd != NULL)
239eda14cbcSMatt Macy 			abd_free(range->sru.data.abd);
240eda14cbcSMatt Macy 		if (range->sru.data.abuf != NULL) {
241eda14cbcSMatt Macy 			arc_buf_destroy(range->sru.data.abuf,
242eda14cbcSMatt Macy 			    &range->sru.data.abuf);
243eda14cbcSMatt Macy 		}
244eda14cbcSMatt Macy 		mutex_exit(&range->sru.data.lock);
245eda14cbcSMatt Macy 
246eda14cbcSMatt Macy 		cv_destroy(&range->sru.data.cv);
247eda14cbcSMatt Macy 		mutex_destroy(&range->sru.data.lock);
248eda14cbcSMatt Macy 	}
249eda14cbcSMatt Macy 	kmem_free(range, sizeof (*range));
250eda14cbcSMatt Macy }
251eda14cbcSMatt Macy 
252eda14cbcSMatt Macy /*
253eda14cbcSMatt Macy  * For all record types except BEGIN, fill in the checksum (overlaid in
254eda14cbcSMatt Macy  * drr_u.drr_checksum.drr_checksum).  The checksum verifies everything
255eda14cbcSMatt Macy  * up to the start of the checksum itself.
256eda14cbcSMatt Macy  */
257eda14cbcSMatt Macy static int
258eda14cbcSMatt Macy dump_record(dmu_send_cookie_t *dscp, void *payload, int payload_len)
259eda14cbcSMatt Macy {
260eda14cbcSMatt Macy 	dmu_send_outparams_t *dso = dscp->dsc_dso;
261eda14cbcSMatt Macy 	ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
262eda14cbcSMatt Macy 	    ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
263eda14cbcSMatt Macy 	(void) fletcher_4_incremental_native(dscp->dsc_drr,
264eda14cbcSMatt Macy 	    offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
265eda14cbcSMatt Macy 	    &dscp->dsc_zc);
266eda14cbcSMatt Macy 	if (dscp->dsc_drr->drr_type == DRR_BEGIN) {
267eda14cbcSMatt Macy 		dscp->dsc_sent_begin = B_TRUE;
268eda14cbcSMatt Macy 	} else {
269eda14cbcSMatt Macy 		ASSERT(ZIO_CHECKSUM_IS_ZERO(&dscp->dsc_drr->drr_u.
270eda14cbcSMatt Macy 		    drr_checksum.drr_checksum));
271eda14cbcSMatt Macy 		dscp->dsc_drr->drr_u.drr_checksum.drr_checksum = dscp->dsc_zc;
272eda14cbcSMatt Macy 	}
273eda14cbcSMatt Macy 	if (dscp->dsc_drr->drr_type == DRR_END) {
274eda14cbcSMatt Macy 		dscp->dsc_sent_end = B_TRUE;
275eda14cbcSMatt Macy 	}
276eda14cbcSMatt Macy 	(void) fletcher_4_incremental_native(&dscp->dsc_drr->
277eda14cbcSMatt Macy 	    drr_u.drr_checksum.drr_checksum,
278eda14cbcSMatt Macy 	    sizeof (zio_cksum_t), &dscp->dsc_zc);
279eda14cbcSMatt Macy 	*dscp->dsc_off += sizeof (dmu_replay_record_t);
280eda14cbcSMatt Macy 	dscp->dsc_err = dso->dso_outfunc(dscp->dsc_os, dscp->dsc_drr,
281eda14cbcSMatt Macy 	    sizeof (dmu_replay_record_t), dso->dso_arg);
282eda14cbcSMatt Macy 	if (dscp->dsc_err != 0)
283eda14cbcSMatt Macy 		return (SET_ERROR(EINTR));
284eda14cbcSMatt Macy 	if (payload_len != 0) {
285eda14cbcSMatt Macy 		*dscp->dsc_off += payload_len;
286eda14cbcSMatt Macy 		/*
287eda14cbcSMatt Macy 		 * payload is null when dso_dryrun == B_TRUE (i.e. when we're
288eda14cbcSMatt Macy 		 * doing a send size calculation)
289eda14cbcSMatt Macy 		 */
290eda14cbcSMatt Macy 		if (payload != NULL) {
291eda14cbcSMatt Macy 			(void) fletcher_4_incremental_native(
292eda14cbcSMatt Macy 			    payload, payload_len, &dscp->dsc_zc);
293eda14cbcSMatt Macy 		}
294eda14cbcSMatt Macy 
295eda14cbcSMatt Macy 		/*
296eda14cbcSMatt Macy 		 * The code does not rely on this (len being a multiple of 8).
297eda14cbcSMatt Macy 		 * We keep this assertion because of the corresponding assertion
298eda14cbcSMatt Macy 		 * in receive_read().  Keeping this assertion ensures that we do
299eda14cbcSMatt Macy 		 * not inadvertently break backwards compatibility (causing the
300eda14cbcSMatt Macy 		 * assertion in receive_read() to trigger on old software).
301eda14cbcSMatt Macy 		 *
302eda14cbcSMatt Macy 		 * Raw sends cannot be received on old software, and so can
303eda14cbcSMatt Macy 		 * bypass this assertion.
304eda14cbcSMatt Macy 		 */
305eda14cbcSMatt Macy 
306eda14cbcSMatt Macy 		ASSERT((payload_len % 8 == 0) ||
307eda14cbcSMatt Macy 		    (dscp->dsc_featureflags & DMU_BACKUP_FEATURE_RAW));
308eda14cbcSMatt Macy 
309eda14cbcSMatt Macy 		dscp->dsc_err = dso->dso_outfunc(dscp->dsc_os, payload,
310eda14cbcSMatt Macy 		    payload_len, dso->dso_arg);
311eda14cbcSMatt Macy 		if (dscp->dsc_err != 0)
312eda14cbcSMatt Macy 			return (SET_ERROR(EINTR));
313eda14cbcSMatt Macy 	}
314eda14cbcSMatt Macy 	return (0);
315eda14cbcSMatt Macy }
316eda14cbcSMatt Macy 
317eda14cbcSMatt Macy /*
318eda14cbcSMatt Macy  * Fill in the drr_free struct, or perform aggregation if the previous record is
319eda14cbcSMatt Macy  * also a free record, and the two are adjacent.
320eda14cbcSMatt Macy  *
321eda14cbcSMatt Macy  * Note that we send free records even for a full send, because we want to be
322eda14cbcSMatt Macy  * able to receive a full send as a clone, which requires a list of all the free
323eda14cbcSMatt Macy  * and freeobject records that were generated on the source.
324eda14cbcSMatt Macy  */
325eda14cbcSMatt Macy static int
326eda14cbcSMatt Macy dump_free(dmu_send_cookie_t *dscp, uint64_t object, uint64_t offset,
327eda14cbcSMatt Macy     uint64_t length)
328eda14cbcSMatt Macy {
329eda14cbcSMatt Macy 	struct drr_free *drrf = &(dscp->dsc_drr->drr_u.drr_free);
330eda14cbcSMatt Macy 
331eda14cbcSMatt Macy 	/*
332eda14cbcSMatt Macy 	 * When we receive a free record, dbuf_free_range() assumes
333eda14cbcSMatt Macy 	 * that the receiving system doesn't have any dbufs in the range
334eda14cbcSMatt Macy 	 * being freed.  This is always true because there is a one-record
335eda14cbcSMatt Macy 	 * constraint: we only send one WRITE record for any given
336eda14cbcSMatt Macy 	 * object,offset.  We know that the one-record constraint is
337eda14cbcSMatt Macy 	 * true because we always send data in increasing order by
338eda14cbcSMatt Macy 	 * object,offset.
339eda14cbcSMatt Macy 	 *
340eda14cbcSMatt Macy 	 * If the increasing-order constraint ever changes, we should find
341eda14cbcSMatt Macy 	 * another way to assert that the one-record constraint is still
342eda14cbcSMatt Macy 	 * satisfied.
343eda14cbcSMatt Macy 	 */
344eda14cbcSMatt Macy 	ASSERT(object > dscp->dsc_last_data_object ||
345eda14cbcSMatt Macy 	    (object == dscp->dsc_last_data_object &&
346eda14cbcSMatt Macy 	    offset > dscp->dsc_last_data_offset));
347eda14cbcSMatt Macy 
348eda14cbcSMatt Macy 	/*
349eda14cbcSMatt Macy 	 * If there is a pending op, but it's not PENDING_FREE, push it out,
350eda14cbcSMatt Macy 	 * since free block aggregation can only be done for blocks of the
351eda14cbcSMatt Macy 	 * same type (i.e., DRR_FREE records can only be aggregated with
352eda14cbcSMatt Macy 	 * other DRR_FREE records.  DRR_FREEOBJECTS records can only be
353eda14cbcSMatt Macy 	 * aggregated with other DRR_FREEOBJECTS records).
354eda14cbcSMatt Macy 	 */
355eda14cbcSMatt Macy 	if (dscp->dsc_pending_op != PENDING_NONE &&
356eda14cbcSMatt Macy 	    dscp->dsc_pending_op != PENDING_FREE) {
357eda14cbcSMatt Macy 		if (dump_record(dscp, NULL, 0) != 0)
358eda14cbcSMatt Macy 			return (SET_ERROR(EINTR));
359eda14cbcSMatt Macy 		dscp->dsc_pending_op = PENDING_NONE;
360eda14cbcSMatt Macy 	}
361eda14cbcSMatt Macy 
362eda14cbcSMatt Macy 	if (dscp->dsc_pending_op == PENDING_FREE) {
363eda14cbcSMatt Macy 		/*
364eda14cbcSMatt Macy 		 * Check to see whether this free block can be aggregated
365eda14cbcSMatt Macy 		 * with pending one.
366eda14cbcSMatt Macy 		 */
367eda14cbcSMatt Macy 		if (drrf->drr_object == object && drrf->drr_offset +
368eda14cbcSMatt Macy 		    drrf->drr_length == offset) {
369eda14cbcSMatt Macy 			if (offset + length < offset || length == UINT64_MAX)
370eda14cbcSMatt Macy 				drrf->drr_length = UINT64_MAX;
371eda14cbcSMatt Macy 			else
372eda14cbcSMatt Macy 				drrf->drr_length += length;
373eda14cbcSMatt Macy 			return (0);
374eda14cbcSMatt Macy 		} else {
375eda14cbcSMatt Macy 			/* not a continuation.  Push out pending record */
376eda14cbcSMatt Macy 			if (dump_record(dscp, NULL, 0) != 0)
377eda14cbcSMatt Macy 				return (SET_ERROR(EINTR));
378eda14cbcSMatt Macy 			dscp->dsc_pending_op = PENDING_NONE;
379eda14cbcSMatt Macy 		}
380eda14cbcSMatt Macy 	}
381eda14cbcSMatt Macy 	/* create a FREE record and make it pending */
382da5137abSMartin Matuska 	memset(dscp->dsc_drr, 0, sizeof (dmu_replay_record_t));
383eda14cbcSMatt Macy 	dscp->dsc_drr->drr_type = DRR_FREE;
384eda14cbcSMatt Macy 	drrf->drr_object = object;
385eda14cbcSMatt Macy 	drrf->drr_offset = offset;
386eda14cbcSMatt Macy 	if (offset + length < offset)
387eda14cbcSMatt Macy 		drrf->drr_length = DMU_OBJECT_END;
388eda14cbcSMatt Macy 	else
389eda14cbcSMatt Macy 		drrf->drr_length = length;
390eda14cbcSMatt Macy 	drrf->drr_toguid = dscp->dsc_toguid;
391eda14cbcSMatt Macy 	if (length == DMU_OBJECT_END) {
392eda14cbcSMatt Macy 		if (dump_record(dscp, NULL, 0) != 0)
393eda14cbcSMatt Macy 			return (SET_ERROR(EINTR));
394eda14cbcSMatt Macy 	} else {
395eda14cbcSMatt Macy 		dscp->dsc_pending_op = PENDING_FREE;
396eda14cbcSMatt Macy 	}
397eda14cbcSMatt Macy 
398eda14cbcSMatt Macy 	return (0);
399eda14cbcSMatt Macy }
400eda14cbcSMatt Macy 
401eda14cbcSMatt Macy /*
402eda14cbcSMatt Macy  * Fill in the drr_redact struct, or perform aggregation if the previous record
403eda14cbcSMatt Macy  * is also a redaction record, and the two are adjacent.
404eda14cbcSMatt Macy  */
405eda14cbcSMatt Macy static int
406eda14cbcSMatt Macy dump_redact(dmu_send_cookie_t *dscp, uint64_t object, uint64_t offset,
407eda14cbcSMatt Macy     uint64_t length)
408eda14cbcSMatt Macy {
409eda14cbcSMatt Macy 	struct drr_redact *drrr = &dscp->dsc_drr->drr_u.drr_redact;
410eda14cbcSMatt Macy 
411eda14cbcSMatt Macy 	/*
412eda14cbcSMatt Macy 	 * If there is a pending op, but it's not PENDING_REDACT, push it out,
413eda14cbcSMatt Macy 	 * since free block aggregation can only be done for blocks of the
414eda14cbcSMatt Macy 	 * same type (i.e., DRR_REDACT records can only be aggregated with
415eda14cbcSMatt Macy 	 * other DRR_REDACT records).
416eda14cbcSMatt Macy 	 */
417eda14cbcSMatt Macy 	if (dscp->dsc_pending_op != PENDING_NONE &&
418eda14cbcSMatt Macy 	    dscp->dsc_pending_op != PENDING_REDACT) {
419eda14cbcSMatt Macy 		if (dump_record(dscp, NULL, 0) != 0)
420eda14cbcSMatt Macy 			return (SET_ERROR(EINTR));
421eda14cbcSMatt Macy 		dscp->dsc_pending_op = PENDING_NONE;
422eda14cbcSMatt Macy 	}
423eda14cbcSMatt Macy 
424eda14cbcSMatt Macy 	if (dscp->dsc_pending_op == PENDING_REDACT) {
425eda14cbcSMatt Macy 		/*
426eda14cbcSMatt Macy 		 * Check to see whether this redacted block can be aggregated
427eda14cbcSMatt Macy 		 * with pending one.
428eda14cbcSMatt Macy 		 */
429eda14cbcSMatt Macy 		if (drrr->drr_object == object && drrr->drr_offset +
430eda14cbcSMatt Macy 		    drrr->drr_length == offset) {
431eda14cbcSMatt Macy 			drrr->drr_length += length;
432eda14cbcSMatt Macy 			return (0);
433eda14cbcSMatt Macy 		} else {
434eda14cbcSMatt Macy 			/* not a continuation.  Push out pending record */
435eda14cbcSMatt Macy 			if (dump_record(dscp, NULL, 0) != 0)
436eda14cbcSMatt Macy 				return (SET_ERROR(EINTR));
437eda14cbcSMatt Macy 			dscp->dsc_pending_op = PENDING_NONE;
438eda14cbcSMatt Macy 		}
439eda14cbcSMatt Macy 	}
440eda14cbcSMatt Macy 	/* create a REDACT record and make it pending */
441da5137abSMartin Matuska 	memset(dscp->dsc_drr, 0, sizeof (dmu_replay_record_t));
442eda14cbcSMatt Macy 	dscp->dsc_drr->drr_type = DRR_REDACT;
443eda14cbcSMatt Macy 	drrr->drr_object = object;
444eda14cbcSMatt Macy 	drrr->drr_offset = offset;
445eda14cbcSMatt Macy 	drrr->drr_length = length;
446eda14cbcSMatt Macy 	drrr->drr_toguid = dscp->dsc_toguid;
447eda14cbcSMatt Macy 	dscp->dsc_pending_op = PENDING_REDACT;
448eda14cbcSMatt Macy 
449eda14cbcSMatt Macy 	return (0);
450eda14cbcSMatt Macy }
451eda14cbcSMatt Macy 
452eda14cbcSMatt Macy static int
453eda14cbcSMatt Macy dmu_dump_write(dmu_send_cookie_t *dscp, dmu_object_type_t type, uint64_t object,
454681ce946SMartin Matuska     uint64_t offset, int lsize, int psize, const blkptr_t *bp,
455681ce946SMartin Matuska     boolean_t io_compressed, void *data)
456eda14cbcSMatt Macy {
457eda14cbcSMatt Macy 	uint64_t payload_size;
458eda14cbcSMatt Macy 	boolean_t raw = (dscp->dsc_featureflags & DMU_BACKUP_FEATURE_RAW);
459eda14cbcSMatt Macy 	struct drr_write *drrw = &(dscp->dsc_drr->drr_u.drr_write);
460eda14cbcSMatt Macy 
461eda14cbcSMatt Macy 	/*
462eda14cbcSMatt Macy 	 * We send data in increasing object, offset order.
463eda14cbcSMatt Macy 	 * See comment in dump_free() for details.
464eda14cbcSMatt Macy 	 */
465eda14cbcSMatt Macy 	ASSERT(object > dscp->dsc_last_data_object ||
466eda14cbcSMatt Macy 	    (object == dscp->dsc_last_data_object &&
467eda14cbcSMatt Macy 	    offset > dscp->dsc_last_data_offset));
468eda14cbcSMatt Macy 	dscp->dsc_last_data_object = object;
469eda14cbcSMatt Macy 	dscp->dsc_last_data_offset = offset + lsize - 1;
470eda14cbcSMatt Macy 
471eda14cbcSMatt Macy 	/*
472eda14cbcSMatt Macy 	 * If there is any kind of pending aggregation (currently either
473eda14cbcSMatt Macy 	 * a grouping of free objects or free blocks), push it out to
474eda14cbcSMatt Macy 	 * the stream, since aggregation can't be done across operations
475eda14cbcSMatt Macy 	 * of different types.
476eda14cbcSMatt Macy 	 */
477eda14cbcSMatt Macy 	if (dscp->dsc_pending_op != PENDING_NONE) {
478eda14cbcSMatt Macy 		if (dump_record(dscp, NULL, 0) != 0)
479eda14cbcSMatt Macy 			return (SET_ERROR(EINTR));
480eda14cbcSMatt Macy 		dscp->dsc_pending_op = PENDING_NONE;
481eda14cbcSMatt Macy 	}
482eda14cbcSMatt Macy 	/* write a WRITE record */
483da5137abSMartin Matuska 	memset(dscp->dsc_drr, 0, sizeof (dmu_replay_record_t));
484eda14cbcSMatt Macy 	dscp->dsc_drr->drr_type = DRR_WRITE;
485eda14cbcSMatt Macy 	drrw->drr_object = object;
486eda14cbcSMatt Macy 	drrw->drr_type = type;
487eda14cbcSMatt Macy 	drrw->drr_offset = offset;
488eda14cbcSMatt Macy 	drrw->drr_toguid = dscp->dsc_toguid;
489eda14cbcSMatt Macy 	drrw->drr_logical_size = lsize;
490eda14cbcSMatt Macy 
491eda14cbcSMatt Macy 	/* only set the compression fields if the buf is compressed or raw */
492681ce946SMartin Matuska 	boolean_t compressed =
493681ce946SMartin Matuska 	    (bp != NULL ? BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF &&
494681ce946SMartin Matuska 	    io_compressed : lsize != psize);
495681ce946SMartin Matuska 	if (raw || compressed) {
496eda14cbcSMatt Macy 		ASSERT(raw || dscp->dsc_featureflags &
497eda14cbcSMatt Macy 		    DMU_BACKUP_FEATURE_COMPRESSED);
498eda14cbcSMatt Macy 		ASSERT(!BP_IS_EMBEDDED(bp));
499eda14cbcSMatt Macy 		ASSERT3S(psize, >, 0);
500eda14cbcSMatt Macy 
501eda14cbcSMatt Macy 		if (raw) {
502eda14cbcSMatt Macy 			ASSERT(BP_IS_PROTECTED(bp));
503eda14cbcSMatt Macy 
504eda14cbcSMatt Macy 			/*
505eda14cbcSMatt Macy 			 * This is a raw protected block so we need to pass
506eda14cbcSMatt Macy 			 * along everything the receiving side will need to
507eda14cbcSMatt Macy 			 * interpret this block, including the byteswap, salt,
508eda14cbcSMatt Macy 			 * IV, and MAC.
509eda14cbcSMatt Macy 			 */
510eda14cbcSMatt Macy 			if (BP_SHOULD_BYTESWAP(bp))
511eda14cbcSMatt Macy 				drrw->drr_flags |= DRR_RAW_BYTESWAP;
512eda14cbcSMatt Macy 			zio_crypt_decode_params_bp(bp, drrw->drr_salt,
513eda14cbcSMatt Macy 			    drrw->drr_iv);
514eda14cbcSMatt Macy 			zio_crypt_decode_mac_bp(bp, drrw->drr_mac);
515eda14cbcSMatt Macy 		} else {
516eda14cbcSMatt Macy 			/* this is a compressed block */
517eda14cbcSMatt Macy 			ASSERT(dscp->dsc_featureflags &
518eda14cbcSMatt Macy 			    DMU_BACKUP_FEATURE_COMPRESSED);
519eda14cbcSMatt Macy 			ASSERT(!BP_SHOULD_BYTESWAP(bp));
520eda14cbcSMatt Macy 			ASSERT(!DMU_OT_IS_METADATA(BP_GET_TYPE(bp)));
521eda14cbcSMatt Macy 			ASSERT3U(BP_GET_COMPRESS(bp), !=, ZIO_COMPRESS_OFF);
522eda14cbcSMatt Macy 			ASSERT3S(lsize, >=, psize);
523eda14cbcSMatt Macy 		}
524eda14cbcSMatt Macy 
525eda14cbcSMatt Macy 		/* set fields common to compressed and raw sends */
526eda14cbcSMatt Macy 		drrw->drr_compressiontype = BP_GET_COMPRESS(bp);
527eda14cbcSMatt Macy 		drrw->drr_compressed_size = psize;
528eda14cbcSMatt Macy 		payload_size = drrw->drr_compressed_size;
529eda14cbcSMatt Macy 	} else {
530eda14cbcSMatt Macy 		payload_size = drrw->drr_logical_size;
531eda14cbcSMatt Macy 	}
532eda14cbcSMatt Macy 
533eda14cbcSMatt Macy 	if (bp == NULL || BP_IS_EMBEDDED(bp) || (BP_IS_PROTECTED(bp) && !raw)) {
534eda14cbcSMatt Macy 		/*
535eda14cbcSMatt Macy 		 * There's no pre-computed checksum for partial-block writes,
536eda14cbcSMatt Macy 		 * embedded BP's, or encrypted BP's that are being sent as
537eda14cbcSMatt Macy 		 * plaintext, so (like fletcher4-checksummed blocks) userland
538eda14cbcSMatt Macy 		 * will have to compute a dedup-capable checksum itself.
539eda14cbcSMatt Macy 		 */
540eda14cbcSMatt Macy 		drrw->drr_checksumtype = ZIO_CHECKSUM_OFF;
541eda14cbcSMatt Macy 	} else {
542eda14cbcSMatt Macy 		drrw->drr_checksumtype = BP_GET_CHECKSUM(bp);
543eda14cbcSMatt Macy 		if (zio_checksum_table[drrw->drr_checksumtype].ci_flags &
544eda14cbcSMatt Macy 		    ZCHECKSUM_FLAG_DEDUP)
545eda14cbcSMatt Macy 			drrw->drr_flags |= DRR_CHECKSUM_DEDUP;
546eda14cbcSMatt Macy 		DDK_SET_LSIZE(&drrw->drr_key, BP_GET_LSIZE(bp));
547eda14cbcSMatt Macy 		DDK_SET_PSIZE(&drrw->drr_key, BP_GET_PSIZE(bp));
548eda14cbcSMatt Macy 		DDK_SET_COMPRESS(&drrw->drr_key, BP_GET_COMPRESS(bp));
549eda14cbcSMatt Macy 		DDK_SET_CRYPT(&drrw->drr_key, BP_IS_PROTECTED(bp));
550eda14cbcSMatt Macy 		drrw->drr_key.ddk_cksum = bp->blk_cksum;
551eda14cbcSMatt Macy 	}
552eda14cbcSMatt Macy 
553eda14cbcSMatt Macy 	if (dump_record(dscp, data, payload_size) != 0)
554eda14cbcSMatt Macy 		return (SET_ERROR(EINTR));
555eda14cbcSMatt Macy 	return (0);
556eda14cbcSMatt Macy }
557eda14cbcSMatt Macy 
558eda14cbcSMatt Macy static int
559eda14cbcSMatt Macy dump_write_embedded(dmu_send_cookie_t *dscp, uint64_t object, uint64_t offset,
560eda14cbcSMatt Macy     int blksz, const blkptr_t *bp)
561eda14cbcSMatt Macy {
562eda14cbcSMatt Macy 	char buf[BPE_PAYLOAD_SIZE];
563eda14cbcSMatt Macy 	struct drr_write_embedded *drrw =
564eda14cbcSMatt Macy 	    &(dscp->dsc_drr->drr_u.drr_write_embedded);
565eda14cbcSMatt Macy 
566eda14cbcSMatt Macy 	if (dscp->dsc_pending_op != PENDING_NONE) {
567eda14cbcSMatt Macy 		if (dump_record(dscp, NULL, 0) != 0)
568eda14cbcSMatt Macy 			return (SET_ERROR(EINTR));
569eda14cbcSMatt Macy 		dscp->dsc_pending_op = PENDING_NONE;
570eda14cbcSMatt Macy 	}
571eda14cbcSMatt Macy 
572eda14cbcSMatt Macy 	ASSERT(BP_IS_EMBEDDED(bp));
573eda14cbcSMatt Macy 
574da5137abSMartin Matuska 	memset(dscp->dsc_drr, 0, sizeof (dmu_replay_record_t));
575eda14cbcSMatt Macy 	dscp->dsc_drr->drr_type = DRR_WRITE_EMBEDDED;
576eda14cbcSMatt Macy 	drrw->drr_object = object;
577eda14cbcSMatt Macy 	drrw->drr_offset = offset;
578eda14cbcSMatt Macy 	drrw->drr_length = blksz;
579eda14cbcSMatt Macy 	drrw->drr_toguid = dscp->dsc_toguid;
580eda14cbcSMatt Macy 	drrw->drr_compression = BP_GET_COMPRESS(bp);
581eda14cbcSMatt Macy 	drrw->drr_etype = BPE_GET_ETYPE(bp);
582eda14cbcSMatt Macy 	drrw->drr_lsize = BPE_GET_LSIZE(bp);
583eda14cbcSMatt Macy 	drrw->drr_psize = BPE_GET_PSIZE(bp);
584eda14cbcSMatt Macy 
585eda14cbcSMatt Macy 	decode_embedded_bp_compressed(bp, buf);
586eda14cbcSMatt Macy 
587*15f0b8c3SMartin Matuska 	uint32_t psize = drrw->drr_psize;
588*15f0b8c3SMartin Matuska 	uint32_t rsize = P2ROUNDUP(psize, 8);
589*15f0b8c3SMartin Matuska 
590*15f0b8c3SMartin Matuska 	if (psize != rsize)
591*15f0b8c3SMartin Matuska 		memset(buf + psize, 0, rsize - psize);
592*15f0b8c3SMartin Matuska 
593*15f0b8c3SMartin Matuska 	if (dump_record(dscp, buf, rsize) != 0)
594eda14cbcSMatt Macy 		return (SET_ERROR(EINTR));
595eda14cbcSMatt Macy 	return (0);
596eda14cbcSMatt Macy }
597eda14cbcSMatt Macy 
598eda14cbcSMatt Macy static int
599eda14cbcSMatt Macy dump_spill(dmu_send_cookie_t *dscp, const blkptr_t *bp, uint64_t object,
600eda14cbcSMatt Macy     void *data)
601eda14cbcSMatt Macy {
602eda14cbcSMatt Macy 	struct drr_spill *drrs = &(dscp->dsc_drr->drr_u.drr_spill);
603eda14cbcSMatt Macy 	uint64_t blksz = BP_GET_LSIZE(bp);
604eda14cbcSMatt Macy 	uint64_t payload_size = blksz;
605eda14cbcSMatt Macy 
606eda14cbcSMatt Macy 	if (dscp->dsc_pending_op != PENDING_NONE) {
607eda14cbcSMatt Macy 		if (dump_record(dscp, NULL, 0) != 0)
608eda14cbcSMatt Macy 			return (SET_ERROR(EINTR));
609eda14cbcSMatt Macy 		dscp->dsc_pending_op = PENDING_NONE;
610eda14cbcSMatt Macy 	}
611eda14cbcSMatt Macy 
612eda14cbcSMatt Macy 	/* write a SPILL record */
613da5137abSMartin Matuska 	memset(dscp->dsc_drr, 0, sizeof (dmu_replay_record_t));
614eda14cbcSMatt Macy 	dscp->dsc_drr->drr_type = DRR_SPILL;
615eda14cbcSMatt Macy 	drrs->drr_object = object;
616eda14cbcSMatt Macy 	drrs->drr_length = blksz;
617eda14cbcSMatt Macy 	drrs->drr_toguid = dscp->dsc_toguid;
618eda14cbcSMatt Macy 
619eda14cbcSMatt Macy 	/* See comment in dump_dnode() for full details */
620eda14cbcSMatt Macy 	if (zfs_send_unmodified_spill_blocks &&
621eda14cbcSMatt Macy 	    (bp->blk_birth <= dscp->dsc_fromtxg)) {
622eda14cbcSMatt Macy 		drrs->drr_flags |= DRR_SPILL_UNMODIFIED;
623eda14cbcSMatt Macy 	}
624eda14cbcSMatt Macy 
625eda14cbcSMatt Macy 	/* handle raw send fields */
626eda14cbcSMatt Macy 	if (dscp->dsc_featureflags & DMU_BACKUP_FEATURE_RAW) {
627eda14cbcSMatt Macy 		ASSERT(BP_IS_PROTECTED(bp));
628eda14cbcSMatt Macy 
629eda14cbcSMatt Macy 		if (BP_SHOULD_BYTESWAP(bp))
630eda14cbcSMatt Macy 			drrs->drr_flags |= DRR_RAW_BYTESWAP;
631eda14cbcSMatt Macy 		drrs->drr_compressiontype = BP_GET_COMPRESS(bp);
632eda14cbcSMatt Macy 		drrs->drr_compressed_size = BP_GET_PSIZE(bp);
633eda14cbcSMatt Macy 		zio_crypt_decode_params_bp(bp, drrs->drr_salt, drrs->drr_iv);
634eda14cbcSMatt Macy 		zio_crypt_decode_mac_bp(bp, drrs->drr_mac);
635eda14cbcSMatt Macy 		payload_size = drrs->drr_compressed_size;
636eda14cbcSMatt Macy 	}
637eda14cbcSMatt Macy 
638eda14cbcSMatt Macy 	if (dump_record(dscp, data, payload_size) != 0)
639eda14cbcSMatt Macy 		return (SET_ERROR(EINTR));
640eda14cbcSMatt Macy 	return (0);
641eda14cbcSMatt Macy }
642eda14cbcSMatt Macy 
643eda14cbcSMatt Macy static int
644eda14cbcSMatt Macy dump_freeobjects(dmu_send_cookie_t *dscp, uint64_t firstobj, uint64_t numobjs)
645eda14cbcSMatt Macy {
646eda14cbcSMatt Macy 	struct drr_freeobjects *drrfo = &(dscp->dsc_drr->drr_u.drr_freeobjects);
647eda14cbcSMatt Macy 	uint64_t maxobj = DNODES_PER_BLOCK *
648eda14cbcSMatt Macy 	    (DMU_META_DNODE(dscp->dsc_os)->dn_maxblkid + 1);
649eda14cbcSMatt Macy 
650eda14cbcSMatt Macy 	/*
651eda14cbcSMatt Macy 	 * ZoL < 0.7 does not handle large FREEOBJECTS records correctly,
652eda14cbcSMatt Macy 	 * leading to zfs recv never completing. to avoid this issue, don't
653eda14cbcSMatt Macy 	 * send FREEOBJECTS records for object IDs which cannot exist on the
654eda14cbcSMatt Macy 	 * receiving side.
655eda14cbcSMatt Macy 	 */
656eda14cbcSMatt Macy 	if (maxobj > 0) {
657c40487d4SMatt Macy 		if (maxobj <= firstobj)
658eda14cbcSMatt Macy 			return (0);
659eda14cbcSMatt Macy 
660eda14cbcSMatt Macy 		if (maxobj < firstobj + numobjs)
661eda14cbcSMatt Macy 			numobjs = maxobj - firstobj;
662eda14cbcSMatt Macy 	}
663eda14cbcSMatt Macy 
664eda14cbcSMatt Macy 	/*
665eda14cbcSMatt Macy 	 * If there is a pending op, but it's not PENDING_FREEOBJECTS,
666eda14cbcSMatt Macy 	 * push it out, since free block aggregation can only be done for
667eda14cbcSMatt Macy 	 * blocks of the same type (i.e., DRR_FREE records can only be
668eda14cbcSMatt Macy 	 * aggregated with other DRR_FREE records.  DRR_FREEOBJECTS records
669eda14cbcSMatt Macy 	 * can only be aggregated with other DRR_FREEOBJECTS records).
670eda14cbcSMatt Macy 	 */
671eda14cbcSMatt Macy 	if (dscp->dsc_pending_op != PENDING_NONE &&
672eda14cbcSMatt Macy 	    dscp->dsc_pending_op != PENDING_FREEOBJECTS) {
673eda14cbcSMatt Macy 		if (dump_record(dscp, NULL, 0) != 0)
674eda14cbcSMatt Macy 			return (SET_ERROR(EINTR));
675eda14cbcSMatt Macy 		dscp->dsc_pending_op = PENDING_NONE;
676eda14cbcSMatt Macy 	}
677eda14cbcSMatt Macy 
678eda14cbcSMatt Macy 	if (dscp->dsc_pending_op == PENDING_FREEOBJECTS) {
679eda14cbcSMatt Macy 		/*
680eda14cbcSMatt Macy 		 * See whether this free object array can be aggregated
681eda14cbcSMatt Macy 		 * with pending one
682eda14cbcSMatt Macy 		 */
683eda14cbcSMatt Macy 		if (drrfo->drr_firstobj + drrfo->drr_numobjs == firstobj) {
684eda14cbcSMatt Macy 			drrfo->drr_numobjs += numobjs;
685eda14cbcSMatt Macy 			return (0);
686eda14cbcSMatt Macy 		} else {
687eda14cbcSMatt Macy 			/* can't be aggregated.  Push out pending record */
688eda14cbcSMatt Macy 			if (dump_record(dscp, NULL, 0) != 0)
689eda14cbcSMatt Macy 				return (SET_ERROR(EINTR));
690eda14cbcSMatt Macy 			dscp->dsc_pending_op = PENDING_NONE;
691eda14cbcSMatt Macy 		}
692eda14cbcSMatt Macy 	}
693eda14cbcSMatt Macy 
694eda14cbcSMatt Macy 	/* write a FREEOBJECTS record */
695da5137abSMartin Matuska 	memset(dscp->dsc_drr, 0, sizeof (dmu_replay_record_t));
696eda14cbcSMatt Macy 	dscp->dsc_drr->drr_type = DRR_FREEOBJECTS;
697eda14cbcSMatt Macy 	drrfo->drr_firstobj = firstobj;
698eda14cbcSMatt Macy 	drrfo->drr_numobjs = numobjs;
699eda14cbcSMatt Macy 	drrfo->drr_toguid = dscp->dsc_toguid;
700eda14cbcSMatt Macy 
701eda14cbcSMatt Macy 	dscp->dsc_pending_op = PENDING_FREEOBJECTS;
702eda14cbcSMatt Macy 
703eda14cbcSMatt Macy 	return (0);
704eda14cbcSMatt Macy }
705eda14cbcSMatt Macy 
706eda14cbcSMatt Macy static int
707eda14cbcSMatt Macy dump_dnode(dmu_send_cookie_t *dscp, const blkptr_t *bp, uint64_t object,
708eda14cbcSMatt Macy     dnode_phys_t *dnp)
709eda14cbcSMatt Macy {
710eda14cbcSMatt Macy 	struct drr_object *drro = &(dscp->dsc_drr->drr_u.drr_object);
711eda14cbcSMatt Macy 	int bonuslen;
712eda14cbcSMatt Macy 
713eda14cbcSMatt Macy 	if (object < dscp->dsc_resume_object) {
714eda14cbcSMatt Macy 		/*
715eda14cbcSMatt Macy 		 * Note: when resuming, we will visit all the dnodes in
716eda14cbcSMatt Macy 		 * the block of dnodes that we are resuming from.  In
717eda14cbcSMatt Macy 		 * this case it's unnecessary to send the dnodes prior to
718eda14cbcSMatt Macy 		 * the one we are resuming from.  We should be at most one
719eda14cbcSMatt Macy 		 * block's worth of dnodes behind the resume point.
720eda14cbcSMatt Macy 		 */
721eda14cbcSMatt Macy 		ASSERT3U(dscp->dsc_resume_object - object, <,
722eda14cbcSMatt Macy 		    1 << (DNODE_BLOCK_SHIFT - DNODE_SHIFT));
723eda14cbcSMatt Macy 		return (0);
724eda14cbcSMatt Macy 	}
725eda14cbcSMatt Macy 
726eda14cbcSMatt Macy 	if (dnp == NULL || dnp->dn_type == DMU_OT_NONE)
727eda14cbcSMatt Macy 		return (dump_freeobjects(dscp, object, 1));
728eda14cbcSMatt Macy 
729eda14cbcSMatt Macy 	if (dscp->dsc_pending_op != PENDING_NONE) {
730eda14cbcSMatt Macy 		if (dump_record(dscp, NULL, 0) != 0)
731eda14cbcSMatt Macy 			return (SET_ERROR(EINTR));
732eda14cbcSMatt Macy 		dscp->dsc_pending_op = PENDING_NONE;
733eda14cbcSMatt Macy 	}
734eda14cbcSMatt Macy 
735eda14cbcSMatt Macy 	/* write an OBJECT record */
736da5137abSMartin Matuska 	memset(dscp->dsc_drr, 0, sizeof (dmu_replay_record_t));
737eda14cbcSMatt Macy 	dscp->dsc_drr->drr_type = DRR_OBJECT;
738eda14cbcSMatt Macy 	drro->drr_object = object;
739eda14cbcSMatt Macy 	drro->drr_type = dnp->dn_type;
740eda14cbcSMatt Macy 	drro->drr_bonustype = dnp->dn_bonustype;
741eda14cbcSMatt Macy 	drro->drr_blksz = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
742eda14cbcSMatt Macy 	drro->drr_bonuslen = dnp->dn_bonuslen;
743eda14cbcSMatt Macy 	drro->drr_dn_slots = dnp->dn_extra_slots + 1;
744eda14cbcSMatt Macy 	drro->drr_checksumtype = dnp->dn_checksum;
745eda14cbcSMatt Macy 	drro->drr_compress = dnp->dn_compress;
746eda14cbcSMatt Macy 	drro->drr_toguid = dscp->dsc_toguid;
747eda14cbcSMatt Macy 
748eda14cbcSMatt Macy 	if (!(dscp->dsc_featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
749eda14cbcSMatt Macy 	    drro->drr_blksz > SPA_OLD_MAXBLOCKSIZE)
750eda14cbcSMatt Macy 		drro->drr_blksz = SPA_OLD_MAXBLOCKSIZE;
751eda14cbcSMatt Macy 
752eda14cbcSMatt Macy 	bonuslen = P2ROUNDUP(dnp->dn_bonuslen, 8);
753eda14cbcSMatt Macy 
754eda14cbcSMatt Macy 	if ((dscp->dsc_featureflags & DMU_BACKUP_FEATURE_RAW)) {
755eda14cbcSMatt Macy 		ASSERT(BP_IS_ENCRYPTED(bp));
756eda14cbcSMatt Macy 
757eda14cbcSMatt Macy 		if (BP_SHOULD_BYTESWAP(bp))
758eda14cbcSMatt Macy 			drro->drr_flags |= DRR_RAW_BYTESWAP;
759eda14cbcSMatt Macy 
760eda14cbcSMatt Macy 		/* needed for reconstructing dnp on recv side */
761eda14cbcSMatt Macy 		drro->drr_maxblkid = dnp->dn_maxblkid;
762eda14cbcSMatt Macy 		drro->drr_indblkshift = dnp->dn_indblkshift;
763eda14cbcSMatt Macy 		drro->drr_nlevels = dnp->dn_nlevels;
764eda14cbcSMatt Macy 		drro->drr_nblkptr = dnp->dn_nblkptr;
765eda14cbcSMatt Macy 
766eda14cbcSMatt Macy 		/*
767eda14cbcSMatt Macy 		 * Since we encrypt the entire bonus area, the (raw) part
768eda14cbcSMatt Macy 		 * beyond the bonuslen is actually nonzero, so we need
769eda14cbcSMatt Macy 		 * to send it.
770eda14cbcSMatt Macy 		 */
771eda14cbcSMatt Macy 		if (bonuslen != 0) {
772c03c5b1cSMartin Matuska 			if (drro->drr_bonuslen > DN_MAX_BONUS_LEN(dnp))
773c03c5b1cSMartin Matuska 				return (SET_ERROR(EINVAL));
774eda14cbcSMatt Macy 			drro->drr_raw_bonuslen = DN_MAX_BONUS_LEN(dnp);
775eda14cbcSMatt Macy 			bonuslen = drro->drr_raw_bonuslen;
776eda14cbcSMatt Macy 		}
777eda14cbcSMatt Macy 	}
778eda14cbcSMatt Macy 
779eda14cbcSMatt Macy 	/*
780eda14cbcSMatt Macy 	 * DRR_OBJECT_SPILL is set for every dnode which references a
781eda14cbcSMatt Macy 	 * spill block.	 This allows the receiving pool to definitively
782eda14cbcSMatt Macy 	 * determine when a spill block should be kept or freed.
783eda14cbcSMatt Macy 	 */
784eda14cbcSMatt Macy 	if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)
785eda14cbcSMatt Macy 		drro->drr_flags |= DRR_OBJECT_SPILL;
786eda14cbcSMatt Macy 
787eda14cbcSMatt Macy 	if (dump_record(dscp, DN_BONUS(dnp), bonuslen) != 0)
788eda14cbcSMatt Macy 		return (SET_ERROR(EINTR));
789eda14cbcSMatt Macy 
790eda14cbcSMatt Macy 	/* Free anything past the end of the file. */
791eda14cbcSMatt Macy 	if (dump_free(dscp, object, (dnp->dn_maxblkid + 1) *
792eda14cbcSMatt Macy 	    (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT), DMU_OBJECT_END) != 0)
793eda14cbcSMatt Macy 		return (SET_ERROR(EINTR));
794eda14cbcSMatt Macy 
795eda14cbcSMatt Macy 	/*
796eda14cbcSMatt Macy 	 * Send DRR_SPILL records for unmodified spill blocks.	This is useful
797eda14cbcSMatt Macy 	 * because changing certain attributes of the object (e.g. blocksize)
798eda14cbcSMatt Macy 	 * can cause old versions of ZFS to incorrectly remove a spill block.
799eda14cbcSMatt Macy 	 * Including these records in the stream forces an up to date version
800eda14cbcSMatt Macy 	 * to always be written ensuring they're never lost.  Current versions
801eda14cbcSMatt Macy 	 * of the code which understand the DRR_FLAG_SPILL_BLOCK feature can
802eda14cbcSMatt Macy 	 * ignore these unmodified spill blocks.
803eda14cbcSMatt Macy 	 */
804eda14cbcSMatt Macy 	if (zfs_send_unmodified_spill_blocks &&
805eda14cbcSMatt Macy 	    (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) &&
806eda14cbcSMatt Macy 	    (DN_SPILL_BLKPTR(dnp)->blk_birth <= dscp->dsc_fromtxg)) {
807eda14cbcSMatt Macy 		struct send_range record;
808eda14cbcSMatt Macy 		blkptr_t *bp = DN_SPILL_BLKPTR(dnp);
809eda14cbcSMatt Macy 
810da5137abSMartin Matuska 		memset(&record, 0, sizeof (struct send_range));
811eda14cbcSMatt Macy 		record.type = DATA;
812eda14cbcSMatt Macy 		record.object = object;
813eda14cbcSMatt Macy 		record.eos_marker = B_FALSE;
814eda14cbcSMatt Macy 		record.start_blkid = DMU_SPILL_BLKID;
815eda14cbcSMatt Macy 		record.end_blkid = record.start_blkid + 1;
816eda14cbcSMatt Macy 		record.sru.data.bp = *bp;
817eda14cbcSMatt Macy 		record.sru.data.obj_type = dnp->dn_type;
818eda14cbcSMatt Macy 		record.sru.data.datablksz = BP_GET_LSIZE(bp);
819eda14cbcSMatt Macy 
820eda14cbcSMatt Macy 		if (do_dump(dscp, &record) != 0)
821eda14cbcSMatt Macy 			return (SET_ERROR(EINTR));
822eda14cbcSMatt Macy 	}
823eda14cbcSMatt Macy 
824eda14cbcSMatt Macy 	if (dscp->dsc_err != 0)
825eda14cbcSMatt Macy 		return (SET_ERROR(EINTR));
826eda14cbcSMatt Macy 
827eda14cbcSMatt Macy 	return (0);
828eda14cbcSMatt Macy }
829eda14cbcSMatt Macy 
830eda14cbcSMatt Macy static int
831eda14cbcSMatt Macy dump_object_range(dmu_send_cookie_t *dscp, const blkptr_t *bp,
832eda14cbcSMatt Macy     uint64_t firstobj, uint64_t numslots)
833eda14cbcSMatt Macy {
834eda14cbcSMatt Macy 	struct drr_object_range *drror =
835eda14cbcSMatt Macy 	    &(dscp->dsc_drr->drr_u.drr_object_range);
836eda14cbcSMatt Macy 
837eda14cbcSMatt Macy 	/* we only use this record type for raw sends */
838eda14cbcSMatt Macy 	ASSERT(BP_IS_PROTECTED(bp));
839eda14cbcSMatt Macy 	ASSERT(dscp->dsc_featureflags & DMU_BACKUP_FEATURE_RAW);
840eda14cbcSMatt Macy 	ASSERT3U(BP_GET_COMPRESS(bp), ==, ZIO_COMPRESS_OFF);
841eda14cbcSMatt Macy 	ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_DNODE);
842eda14cbcSMatt Macy 	ASSERT0(BP_GET_LEVEL(bp));
843eda14cbcSMatt Macy 
844eda14cbcSMatt Macy 	if (dscp->dsc_pending_op != PENDING_NONE) {
845eda14cbcSMatt Macy 		if (dump_record(dscp, NULL, 0) != 0)
846eda14cbcSMatt Macy 			return (SET_ERROR(EINTR));
847eda14cbcSMatt Macy 		dscp->dsc_pending_op = PENDING_NONE;
848eda14cbcSMatt Macy 	}
849eda14cbcSMatt Macy 
850da5137abSMartin Matuska 	memset(dscp->dsc_drr, 0, sizeof (dmu_replay_record_t));
851eda14cbcSMatt Macy 	dscp->dsc_drr->drr_type = DRR_OBJECT_RANGE;
852eda14cbcSMatt Macy 	drror->drr_firstobj = firstobj;
853eda14cbcSMatt Macy 	drror->drr_numslots = numslots;
854eda14cbcSMatt Macy 	drror->drr_toguid = dscp->dsc_toguid;
855eda14cbcSMatt Macy 	if (BP_SHOULD_BYTESWAP(bp))
856eda14cbcSMatt Macy 		drror->drr_flags |= DRR_RAW_BYTESWAP;
857eda14cbcSMatt Macy 	zio_crypt_decode_params_bp(bp, drror->drr_salt, drror->drr_iv);
858eda14cbcSMatt Macy 	zio_crypt_decode_mac_bp(bp, drror->drr_mac);
859eda14cbcSMatt Macy 
860eda14cbcSMatt Macy 	if (dump_record(dscp, NULL, 0) != 0)
861eda14cbcSMatt Macy 		return (SET_ERROR(EINTR));
862eda14cbcSMatt Macy 	return (0);
863eda14cbcSMatt Macy }
864eda14cbcSMatt Macy 
865eda14cbcSMatt Macy static boolean_t
866eda14cbcSMatt Macy send_do_embed(const blkptr_t *bp, uint64_t featureflags)
867eda14cbcSMatt Macy {
868eda14cbcSMatt Macy 	if (!BP_IS_EMBEDDED(bp))
869eda14cbcSMatt Macy 		return (B_FALSE);
870eda14cbcSMatt Macy 
871eda14cbcSMatt Macy 	/*
872eda14cbcSMatt Macy 	 * Compression function must be legacy, or explicitly enabled.
873eda14cbcSMatt Macy 	 */
874eda14cbcSMatt Macy 	if ((BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_LEGACY_FUNCTIONS &&
875eda14cbcSMatt Macy 	    !(featureflags & DMU_BACKUP_FEATURE_LZ4)))
876eda14cbcSMatt Macy 		return (B_FALSE);
877eda14cbcSMatt Macy 
878eda14cbcSMatt Macy 	/*
879eda14cbcSMatt Macy 	 * If we have not set the ZSTD feature flag, we can't send ZSTD
880eda14cbcSMatt Macy 	 * compressed embedded blocks, as the receiver may not support them.
881eda14cbcSMatt Macy 	 */
882eda14cbcSMatt Macy 	if ((BP_GET_COMPRESS(bp) == ZIO_COMPRESS_ZSTD &&
883eda14cbcSMatt Macy 	    !(featureflags & DMU_BACKUP_FEATURE_ZSTD)))
884eda14cbcSMatt Macy 		return (B_FALSE);
885eda14cbcSMatt Macy 
886eda14cbcSMatt Macy 	/*
887eda14cbcSMatt Macy 	 * Embed type must be explicitly enabled.
888eda14cbcSMatt Macy 	 */
889eda14cbcSMatt Macy 	switch (BPE_GET_ETYPE(bp)) {
890eda14cbcSMatt Macy 	case BP_EMBEDDED_TYPE_DATA:
891eda14cbcSMatt Macy 		if (featureflags & DMU_BACKUP_FEATURE_EMBED_DATA)
892eda14cbcSMatt Macy 			return (B_TRUE);
893eda14cbcSMatt Macy 		break;
894eda14cbcSMatt Macy 	default:
895eda14cbcSMatt Macy 		return (B_FALSE);
896eda14cbcSMatt Macy 	}
897eda14cbcSMatt Macy 	return (B_FALSE);
898eda14cbcSMatt Macy }
899eda14cbcSMatt Macy 
900eda14cbcSMatt Macy /*
901eda14cbcSMatt Macy  * This function actually handles figuring out what kind of record needs to be
902eda14cbcSMatt Macy  * dumped, and calling the appropriate helper function.  In most cases,
903eda14cbcSMatt Macy  * the data has already been read by send_reader_thread().
904eda14cbcSMatt Macy  */
905eda14cbcSMatt Macy static int
906eda14cbcSMatt Macy do_dump(dmu_send_cookie_t *dscp, struct send_range *range)
907eda14cbcSMatt Macy {
908eda14cbcSMatt Macy 	int err = 0;
909eda14cbcSMatt Macy 	switch (range->type) {
910eda14cbcSMatt Macy 	case OBJECT:
911eda14cbcSMatt Macy 		err = dump_dnode(dscp, &range->sru.object.bp, range->object,
912eda14cbcSMatt Macy 		    range->sru.object.dnp);
913eda14cbcSMatt Macy 		return (err);
914eda14cbcSMatt Macy 	case OBJECT_RANGE: {
915eda14cbcSMatt Macy 		ASSERT3U(range->start_blkid + 1, ==, range->end_blkid);
916eda14cbcSMatt Macy 		if (!(dscp->dsc_featureflags & DMU_BACKUP_FEATURE_RAW)) {
917eda14cbcSMatt Macy 			return (0);
918eda14cbcSMatt Macy 		}
919eda14cbcSMatt Macy 		uint64_t epb = BP_GET_LSIZE(&range->sru.object_range.bp) >>
920eda14cbcSMatt Macy 		    DNODE_SHIFT;
921eda14cbcSMatt Macy 		uint64_t firstobj = range->start_blkid * epb;
922eda14cbcSMatt Macy 		err = dump_object_range(dscp, &range->sru.object_range.bp,
923eda14cbcSMatt Macy 		    firstobj, epb);
924eda14cbcSMatt Macy 		break;
925eda14cbcSMatt Macy 	}
926eda14cbcSMatt Macy 	case REDACT: {
927eda14cbcSMatt Macy 		struct srr *srrp = &range->sru.redact;
928eda14cbcSMatt Macy 		err = dump_redact(dscp, range->object, range->start_blkid *
929eda14cbcSMatt Macy 		    srrp->datablksz, (range->end_blkid - range->start_blkid) *
930eda14cbcSMatt Macy 		    srrp->datablksz);
931eda14cbcSMatt Macy 		return (err);
932eda14cbcSMatt Macy 	}
933eda14cbcSMatt Macy 	case DATA: {
934eda14cbcSMatt Macy 		struct srd *srdp = &range->sru.data;
935eda14cbcSMatt Macy 		blkptr_t *bp = &srdp->bp;
936eda14cbcSMatt Macy 		spa_t *spa =
937eda14cbcSMatt Macy 		    dmu_objset_spa(dscp->dsc_os);
938eda14cbcSMatt Macy 
939eda14cbcSMatt Macy 		ASSERT3U(srdp->datablksz, ==, BP_GET_LSIZE(bp));
940eda14cbcSMatt Macy 		ASSERT3U(range->start_blkid + 1, ==, range->end_blkid);
941eda14cbcSMatt Macy 		if (BP_GET_TYPE(bp) == DMU_OT_SA) {
942eda14cbcSMatt Macy 			arc_flags_t aflags = ARC_FLAG_WAIT;
943dbd5678dSMartin Matuska 			zio_flag_t zioflags = ZIO_FLAG_CANFAIL;
944eda14cbcSMatt Macy 
945eda14cbcSMatt Macy 			if (dscp->dsc_featureflags & DMU_BACKUP_FEATURE_RAW) {
946eda14cbcSMatt Macy 				ASSERT(BP_IS_PROTECTED(bp));
947eda14cbcSMatt Macy 				zioflags |= ZIO_FLAG_RAW;
948eda14cbcSMatt Macy 			}
949eda14cbcSMatt Macy 
950eda14cbcSMatt Macy 			zbookmark_phys_t zb;
951eda14cbcSMatt Macy 			ASSERT3U(range->start_blkid, ==, DMU_SPILL_BLKID);
952eda14cbcSMatt Macy 			zb.zb_objset = dmu_objset_id(dscp->dsc_os);
953eda14cbcSMatt Macy 			zb.zb_object = range->object;
954eda14cbcSMatt Macy 			zb.zb_level = 0;
955eda14cbcSMatt Macy 			zb.zb_blkid = range->start_blkid;
956eda14cbcSMatt Macy 
957eda14cbcSMatt Macy 			arc_buf_t *abuf = NULL;
958eda14cbcSMatt Macy 			if (!dscp->dsc_dso->dso_dryrun && arc_read(NULL, spa,
959eda14cbcSMatt Macy 			    bp, arc_getbuf_func, &abuf, ZIO_PRIORITY_ASYNC_READ,
960eda14cbcSMatt Macy 			    zioflags, &aflags, &zb) != 0)
961eda14cbcSMatt Macy 				return (SET_ERROR(EIO));
962eda14cbcSMatt Macy 
963eda14cbcSMatt Macy 			err = dump_spill(dscp, bp, zb.zb_object,
964eda14cbcSMatt Macy 			    (abuf == NULL ? NULL : abuf->b_data));
965eda14cbcSMatt Macy 			if (abuf != NULL)
966eda14cbcSMatt Macy 				arc_buf_destroy(abuf, &abuf);
967eda14cbcSMatt Macy 			return (err);
968eda14cbcSMatt Macy 		}
969eda14cbcSMatt Macy 		if (send_do_embed(bp, dscp->dsc_featureflags)) {
970eda14cbcSMatt Macy 			err = dump_write_embedded(dscp, range->object,
971eda14cbcSMatt Macy 			    range->start_blkid * srdp->datablksz,
972eda14cbcSMatt Macy 			    srdp->datablksz, bp);
973eda14cbcSMatt Macy 			return (err);
974eda14cbcSMatt Macy 		}
975eda14cbcSMatt Macy 		ASSERT(range->object > dscp->dsc_resume_object ||
976eda14cbcSMatt Macy 		    (range->object == dscp->dsc_resume_object &&
977eda14cbcSMatt Macy 		    range->start_blkid * srdp->datablksz >=
978eda14cbcSMatt Macy 		    dscp->dsc_resume_offset));
979eda14cbcSMatt Macy 		/* it's a level-0 block of a regular object */
980eda14cbcSMatt Macy 
981eda14cbcSMatt Macy 		mutex_enter(&srdp->lock);
982eda14cbcSMatt Macy 		while (srdp->io_outstanding)
983eda14cbcSMatt Macy 			cv_wait(&srdp->cv, &srdp->lock);
984eda14cbcSMatt Macy 		err = srdp->io_err;
985eda14cbcSMatt Macy 		mutex_exit(&srdp->lock);
986eda14cbcSMatt Macy 
987eda14cbcSMatt Macy 		if (err != 0) {
988eda14cbcSMatt Macy 			if (zfs_send_corrupt_data &&
989eda14cbcSMatt Macy 			    !dscp->dsc_dso->dso_dryrun) {
990eda14cbcSMatt Macy 				/*
991eda14cbcSMatt Macy 				 * Send a block filled with 0x"zfs badd bloc"
992eda14cbcSMatt Macy 				 */
993eda14cbcSMatt Macy 				srdp->abuf = arc_alloc_buf(spa, &srdp->abuf,
994eda14cbcSMatt Macy 				    ARC_BUFC_DATA, srdp->datablksz);
995eda14cbcSMatt Macy 				uint64_t *ptr;
996eda14cbcSMatt Macy 				for (ptr = srdp->abuf->b_data;
997eda14cbcSMatt Macy 				    (char *)ptr < (char *)srdp->abuf->b_data +
998eda14cbcSMatt Macy 				    srdp->datablksz; ptr++)
999eda14cbcSMatt Macy 					*ptr = 0x2f5baddb10cULL;
1000eda14cbcSMatt Macy 			} else {
1001eda14cbcSMatt Macy 				return (SET_ERROR(EIO));
1002eda14cbcSMatt Macy 			}
1003eda14cbcSMatt Macy 		}
1004eda14cbcSMatt Macy 
1005eda14cbcSMatt Macy 		ASSERT(dscp->dsc_dso->dso_dryrun ||
1006eda14cbcSMatt Macy 		    srdp->abuf != NULL || srdp->abd != NULL);
1007eda14cbcSMatt Macy 
1008eda14cbcSMatt Macy 		uint64_t offset = range->start_blkid * srdp->datablksz;
1009eda14cbcSMatt Macy 
1010eda14cbcSMatt Macy 		char *data = NULL;
1011eda14cbcSMatt Macy 		if (srdp->abd != NULL) {
1012eda14cbcSMatt Macy 			data = abd_to_buf(srdp->abd);
1013eda14cbcSMatt Macy 			ASSERT3P(srdp->abuf, ==, NULL);
1014eda14cbcSMatt Macy 		} else if (srdp->abuf != NULL) {
1015eda14cbcSMatt Macy 			data = srdp->abuf->b_data;
1016eda14cbcSMatt Macy 		}
1017eda14cbcSMatt Macy 
1018eda14cbcSMatt Macy 		/*
1019eda14cbcSMatt Macy 		 * If we have large blocks stored on disk but the send flags
1020eda14cbcSMatt Macy 		 * don't allow us to send large blocks, we split the data from
1021eda14cbcSMatt Macy 		 * the arc buf into chunks.
1022eda14cbcSMatt Macy 		 */
1023eda14cbcSMatt Macy 		if (srdp->datablksz > SPA_OLD_MAXBLOCKSIZE &&
1024eda14cbcSMatt Macy 		    !(dscp->dsc_featureflags &
1025eda14cbcSMatt Macy 		    DMU_BACKUP_FEATURE_LARGE_BLOCKS)) {
1026eda14cbcSMatt Macy 			while (srdp->datablksz > 0 && err == 0) {
1027eda14cbcSMatt Macy 				int n = MIN(srdp->datablksz,
1028eda14cbcSMatt Macy 				    SPA_OLD_MAXBLOCKSIZE);
1029eda14cbcSMatt Macy 				err = dmu_dump_write(dscp, srdp->obj_type,
1030681ce946SMartin Matuska 				    range->object, offset, n, n, NULL, B_FALSE,
1031681ce946SMartin Matuska 				    data);
1032eda14cbcSMatt Macy 				offset += n;
1033eda14cbcSMatt Macy 				/*
1034eda14cbcSMatt Macy 				 * When doing dry run, data==NULL is used as a
1035eda14cbcSMatt Macy 				 * sentinel value by
1036eda14cbcSMatt Macy 				 * dmu_dump_write()->dump_record().
1037eda14cbcSMatt Macy 				 */
1038eda14cbcSMatt Macy 				if (data != NULL)
1039eda14cbcSMatt Macy 					data += n;
1040eda14cbcSMatt Macy 				srdp->datablksz -= n;
1041eda14cbcSMatt Macy 			}
1042eda14cbcSMatt Macy 		} else {
1043eda14cbcSMatt Macy 			err = dmu_dump_write(dscp, srdp->obj_type,
1044eda14cbcSMatt Macy 			    range->object, offset,
1045681ce946SMartin Matuska 			    srdp->datablksz, srdp->datasz, bp,
1046681ce946SMartin Matuska 			    srdp->io_compressed, data);
1047eda14cbcSMatt Macy 		}
1048eda14cbcSMatt Macy 		return (err);
1049eda14cbcSMatt Macy 	}
1050eda14cbcSMatt Macy 	case HOLE: {
1051eda14cbcSMatt Macy 		struct srh *srhp = &range->sru.hole;
1052eda14cbcSMatt Macy 		if (range->object == DMU_META_DNODE_OBJECT) {
1053eda14cbcSMatt Macy 			uint32_t span = srhp->datablksz >> DNODE_SHIFT;
1054eda14cbcSMatt Macy 			uint64_t first_obj = range->start_blkid * span;
1055eda14cbcSMatt Macy 			uint64_t numobj = range->end_blkid * span - first_obj;
1056eda14cbcSMatt Macy 			return (dump_freeobjects(dscp, first_obj, numobj));
1057eda14cbcSMatt Macy 		}
1058eda14cbcSMatt Macy 		uint64_t offset = 0;
1059eda14cbcSMatt Macy 
1060eda14cbcSMatt Macy 		/*
1061eda14cbcSMatt Macy 		 * If this multiply overflows, we don't need to send this block.
1062eda14cbcSMatt Macy 		 * Even if it has a birth time, it can never not be a hole, so
1063eda14cbcSMatt Macy 		 * we don't need to send records for it.
1064eda14cbcSMatt Macy 		 */
1065eda14cbcSMatt Macy 		if (!overflow_multiply(range->start_blkid, srhp->datablksz,
1066eda14cbcSMatt Macy 		    &offset)) {
1067eda14cbcSMatt Macy 			return (0);
1068eda14cbcSMatt Macy 		}
1069eda14cbcSMatt Macy 		uint64_t len = 0;
1070eda14cbcSMatt Macy 
1071eda14cbcSMatt Macy 		if (!overflow_multiply(range->end_blkid, srhp->datablksz, &len))
1072eda14cbcSMatt Macy 			len = UINT64_MAX;
1073eda14cbcSMatt Macy 		len = len - offset;
1074eda14cbcSMatt Macy 		return (dump_free(dscp, range->object, offset, len));
1075eda14cbcSMatt Macy 	}
1076eda14cbcSMatt Macy 	default:
1077eda14cbcSMatt Macy 		panic("Invalid range type in do_dump: %d", range->type);
1078eda14cbcSMatt Macy 	}
1079eda14cbcSMatt Macy 	return (err);
1080eda14cbcSMatt Macy }
1081eda14cbcSMatt Macy 
1082eda14cbcSMatt Macy static struct send_range *
1083eda14cbcSMatt Macy range_alloc(enum type type, uint64_t object, uint64_t start_blkid,
1084eda14cbcSMatt Macy     uint64_t end_blkid, boolean_t eos)
1085eda14cbcSMatt Macy {
1086eda14cbcSMatt Macy 	struct send_range *range = kmem_alloc(sizeof (*range), KM_SLEEP);
1087eda14cbcSMatt Macy 	range->type = type;
1088eda14cbcSMatt Macy 	range->object = object;
1089eda14cbcSMatt Macy 	range->start_blkid = start_blkid;
1090eda14cbcSMatt Macy 	range->end_blkid = end_blkid;
1091eda14cbcSMatt Macy 	range->eos_marker = eos;
1092eda14cbcSMatt Macy 	if (type == DATA) {
1093eda14cbcSMatt Macy 		range->sru.data.abd = NULL;
1094eda14cbcSMatt Macy 		range->sru.data.abuf = NULL;
1095eda14cbcSMatt Macy 		mutex_init(&range->sru.data.lock, NULL, MUTEX_DEFAULT, NULL);
1096eda14cbcSMatt Macy 		cv_init(&range->sru.data.cv, NULL, CV_DEFAULT, NULL);
1097eda14cbcSMatt Macy 		range->sru.data.io_outstanding = 0;
1098eda14cbcSMatt Macy 		range->sru.data.io_err = 0;
1099681ce946SMartin Matuska 		range->sru.data.io_compressed = B_FALSE;
1100eda14cbcSMatt Macy 	}
1101eda14cbcSMatt Macy 	return (range);
1102eda14cbcSMatt Macy }
1103eda14cbcSMatt Macy 
1104eda14cbcSMatt Macy /*
1105eda14cbcSMatt Macy  * This is the callback function to traverse_dataset that acts as a worker
1106eda14cbcSMatt Macy  * thread for dmu_send_impl.
1107eda14cbcSMatt Macy  */
1108eda14cbcSMatt Macy static int
1109eda14cbcSMatt Macy send_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1110eda14cbcSMatt Macy     const zbookmark_phys_t *zb, const struct dnode_phys *dnp, void *arg)
1111eda14cbcSMatt Macy {
1112e92ffd9bSMartin Matuska 	(void) zilog;
1113eda14cbcSMatt Macy 	struct send_thread_arg *sta = arg;
1114eda14cbcSMatt Macy 	struct send_range *record;
1115eda14cbcSMatt Macy 
1116eda14cbcSMatt Macy 	ASSERT(zb->zb_object == DMU_META_DNODE_OBJECT ||
1117eda14cbcSMatt Macy 	    zb->zb_object >= sta->resume.zb_object);
1118eda14cbcSMatt Macy 
1119eda14cbcSMatt Macy 	/*
1120eda14cbcSMatt Macy 	 * All bps of an encrypted os should have the encryption bit set.
1121eda14cbcSMatt Macy 	 * If this is not true it indicates tampering and we report an error.
1122eda14cbcSMatt Macy 	 */
1123eda14cbcSMatt Macy 	if (sta->os->os_encrypted &&
1124eda14cbcSMatt Macy 	    !BP_IS_HOLE(bp) && !BP_USES_CRYPT(bp)) {
1125eda14cbcSMatt Macy 		spa_log_error(spa, zb);
1126eda14cbcSMatt Macy 		zfs_panic_recover("unencrypted block in encrypted "
1127eda14cbcSMatt Macy 		    "object set %llu", dmu_objset_id(sta->os));
1128eda14cbcSMatt Macy 		return (SET_ERROR(EIO));
1129eda14cbcSMatt Macy 	}
1130eda14cbcSMatt Macy 
1131eda14cbcSMatt Macy 	if (sta->cancel)
1132eda14cbcSMatt Macy 		return (SET_ERROR(EINTR));
1133eda14cbcSMatt Macy 	if (zb->zb_object != DMU_META_DNODE_OBJECT &&
1134eda14cbcSMatt Macy 	    DMU_OBJECT_IS_SPECIAL(zb->zb_object))
1135eda14cbcSMatt Macy 		return (0);
1136eda14cbcSMatt Macy 	atomic_inc_64(sta->num_blocks_visited);
1137eda14cbcSMatt Macy 
1138eda14cbcSMatt Macy 	if (zb->zb_level == ZB_DNODE_LEVEL) {
1139eda14cbcSMatt Macy 		if (zb->zb_object == DMU_META_DNODE_OBJECT)
1140eda14cbcSMatt Macy 			return (0);
1141eda14cbcSMatt Macy 		record = range_alloc(OBJECT, zb->zb_object, 0, 0, B_FALSE);
1142eda14cbcSMatt Macy 		record->sru.object.bp = *bp;
1143eda14cbcSMatt Macy 		size_t size  = sizeof (*dnp) * (dnp->dn_extra_slots + 1);
1144eda14cbcSMatt Macy 		record->sru.object.dnp = kmem_alloc(size, KM_SLEEP);
1145da5137abSMartin Matuska 		memcpy(record->sru.object.dnp, dnp, size);
1146eda14cbcSMatt Macy 		bqueue_enqueue(&sta->q, record, sizeof (*record));
1147eda14cbcSMatt Macy 		return (0);
1148eda14cbcSMatt Macy 	}
1149eda14cbcSMatt Macy 	if (zb->zb_level == 0 && zb->zb_object == DMU_META_DNODE_OBJECT &&
1150eda14cbcSMatt Macy 	    !BP_IS_HOLE(bp)) {
1151eda14cbcSMatt Macy 		record = range_alloc(OBJECT_RANGE, 0, zb->zb_blkid,
1152eda14cbcSMatt Macy 		    zb->zb_blkid + 1, B_FALSE);
1153eda14cbcSMatt Macy 		record->sru.object_range.bp = *bp;
1154eda14cbcSMatt Macy 		bqueue_enqueue(&sta->q, record, sizeof (*record));
1155eda14cbcSMatt Macy 		return (0);
1156eda14cbcSMatt Macy 	}
1157eda14cbcSMatt Macy 	if (zb->zb_level < 0 || (zb->zb_level > 0 && !BP_IS_HOLE(bp)))
1158eda14cbcSMatt Macy 		return (0);
1159eda14cbcSMatt Macy 	if (zb->zb_object == DMU_META_DNODE_OBJECT && !BP_IS_HOLE(bp))
1160eda14cbcSMatt Macy 		return (0);
1161eda14cbcSMatt Macy 
1162eda14cbcSMatt Macy 	uint64_t span = bp_span_in_blocks(dnp->dn_indblkshift, zb->zb_level);
1163eda14cbcSMatt Macy 	uint64_t start;
1164eda14cbcSMatt Macy 
1165eda14cbcSMatt Macy 	/*
1166eda14cbcSMatt Macy 	 * If this multiply overflows, we don't need to send this block.
1167eda14cbcSMatt Macy 	 * Even if it has a birth time, it can never not be a hole, so
1168eda14cbcSMatt Macy 	 * we don't need to send records for it.
1169eda14cbcSMatt Macy 	 */
1170eda14cbcSMatt Macy 	if (!overflow_multiply(span, zb->zb_blkid, &start) || (!(zb->zb_blkid ==
1171eda14cbcSMatt Macy 	    DMU_SPILL_BLKID || DMU_OT_IS_METADATA(dnp->dn_type)) &&
1172eda14cbcSMatt Macy 	    span * zb->zb_blkid > dnp->dn_maxblkid)) {
1173eda14cbcSMatt Macy 		ASSERT(BP_IS_HOLE(bp));
1174eda14cbcSMatt Macy 		return (0);
1175eda14cbcSMatt Macy 	}
1176eda14cbcSMatt Macy 
1177eda14cbcSMatt Macy 	if (zb->zb_blkid == DMU_SPILL_BLKID)
1178eda14cbcSMatt Macy 		ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_SA);
1179eda14cbcSMatt Macy 
1180eda14cbcSMatt Macy 	enum type record_type = DATA;
1181eda14cbcSMatt Macy 	if (BP_IS_HOLE(bp))
1182eda14cbcSMatt Macy 		record_type = HOLE;
1183eda14cbcSMatt Macy 	else if (BP_IS_REDACTED(bp))
1184eda14cbcSMatt Macy 		record_type = REDACT;
1185eda14cbcSMatt Macy 	else
1186eda14cbcSMatt Macy 		record_type = DATA;
1187eda14cbcSMatt Macy 
1188eda14cbcSMatt Macy 	record = range_alloc(record_type, zb->zb_object, start,
1189eda14cbcSMatt Macy 	    (start + span < start ? 0 : start + span), B_FALSE);
1190eda14cbcSMatt Macy 
1191eda14cbcSMatt Macy 	uint64_t datablksz = (zb->zb_blkid == DMU_SPILL_BLKID ?
1192eda14cbcSMatt Macy 	    BP_GET_LSIZE(bp) : dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
1193eda14cbcSMatt Macy 
1194eda14cbcSMatt Macy 	if (BP_IS_HOLE(bp)) {
1195eda14cbcSMatt Macy 		record->sru.hole.datablksz = datablksz;
1196eda14cbcSMatt Macy 	} else if (BP_IS_REDACTED(bp)) {
1197eda14cbcSMatt Macy 		record->sru.redact.datablksz = datablksz;
1198eda14cbcSMatt Macy 	} else {
1199eda14cbcSMatt Macy 		record->sru.data.datablksz = datablksz;
1200eda14cbcSMatt Macy 		record->sru.data.obj_type = dnp->dn_type;
1201eda14cbcSMatt Macy 		record->sru.data.bp = *bp;
1202eda14cbcSMatt Macy 	}
1203eda14cbcSMatt Macy 
1204eda14cbcSMatt Macy 	bqueue_enqueue(&sta->q, record, sizeof (*record));
1205eda14cbcSMatt Macy 	return (0);
1206eda14cbcSMatt Macy }
1207eda14cbcSMatt Macy 
1208eda14cbcSMatt Macy struct redact_list_cb_arg {
1209eda14cbcSMatt Macy 	uint64_t *num_blocks_visited;
1210eda14cbcSMatt Macy 	bqueue_t *q;
1211eda14cbcSMatt Macy 	boolean_t *cancel;
1212eda14cbcSMatt Macy 	boolean_t mark_redact;
1213eda14cbcSMatt Macy };
1214eda14cbcSMatt Macy 
1215eda14cbcSMatt Macy static int
1216eda14cbcSMatt Macy redact_list_cb(redact_block_phys_t *rb, void *arg)
1217eda14cbcSMatt Macy {
1218eda14cbcSMatt Macy 	struct redact_list_cb_arg *rlcap = arg;
1219eda14cbcSMatt Macy 
1220eda14cbcSMatt Macy 	atomic_inc_64(rlcap->num_blocks_visited);
1221eda14cbcSMatt Macy 	if (*rlcap->cancel)
1222eda14cbcSMatt Macy 		return (-1);
1223eda14cbcSMatt Macy 
1224eda14cbcSMatt Macy 	struct send_range *data = range_alloc(REDACT, rb->rbp_object,
1225eda14cbcSMatt Macy 	    rb->rbp_blkid, rb->rbp_blkid + redact_block_get_count(rb), B_FALSE);
1226eda14cbcSMatt Macy 	ASSERT3U(data->end_blkid, >, rb->rbp_blkid);
1227eda14cbcSMatt Macy 	if (rlcap->mark_redact) {
1228eda14cbcSMatt Macy 		data->type = REDACT;
1229eda14cbcSMatt Macy 		data->sru.redact.datablksz = redact_block_get_size(rb);
1230eda14cbcSMatt Macy 	} else {
1231eda14cbcSMatt Macy 		data->type = PREVIOUSLY_REDACTED;
1232eda14cbcSMatt Macy 	}
1233eda14cbcSMatt Macy 	bqueue_enqueue(rlcap->q, data, sizeof (*data));
1234eda14cbcSMatt Macy 
1235eda14cbcSMatt Macy 	return (0);
1236eda14cbcSMatt Macy }
1237eda14cbcSMatt Macy 
1238eda14cbcSMatt Macy /*
1239eda14cbcSMatt Macy  * This function kicks off the traverse_dataset.  It also handles setting the
1240eda14cbcSMatt Macy  * error code of the thread in case something goes wrong, and pushes the End of
1241eda14cbcSMatt Macy  * Stream record when the traverse_dataset call has finished.
1242eda14cbcSMatt Macy  */
1243da5137abSMartin Matuska static __attribute__((noreturn)) void
1244eda14cbcSMatt Macy send_traverse_thread(void *arg)
1245eda14cbcSMatt Macy {
1246eda14cbcSMatt Macy 	struct send_thread_arg *st_arg = arg;
1247eda14cbcSMatt Macy 	int err = 0;
1248eda14cbcSMatt Macy 	struct send_range *data;
1249eda14cbcSMatt Macy 	fstrans_cookie_t cookie = spl_fstrans_mark();
1250eda14cbcSMatt Macy 
1251eda14cbcSMatt Macy 	err = traverse_dataset_resume(st_arg->os->os_dsl_dataset,
1252eda14cbcSMatt Macy 	    st_arg->fromtxg, &st_arg->resume,
1253eda14cbcSMatt Macy 	    st_arg->flags, send_cb, st_arg);
1254eda14cbcSMatt Macy 
1255eda14cbcSMatt Macy 	if (err != EINTR)
1256eda14cbcSMatt Macy 		st_arg->error_code = err;
1257eda14cbcSMatt Macy 	data = range_alloc(DATA, 0, 0, 0, B_TRUE);
1258eda14cbcSMatt Macy 	bqueue_enqueue_flush(&st_arg->q, data, sizeof (*data));
1259eda14cbcSMatt Macy 	spl_fstrans_unmark(cookie);
1260eda14cbcSMatt Macy 	thread_exit();
1261eda14cbcSMatt Macy }
1262eda14cbcSMatt Macy 
1263eda14cbcSMatt Macy /*
1264eda14cbcSMatt Macy  * Utility function that causes End of Stream records to compare after of all
1265eda14cbcSMatt Macy  * others, so that other threads' comparison logic can stay simple.
1266eda14cbcSMatt Macy  */
1267eda14cbcSMatt Macy static int __attribute__((unused))
1268eda14cbcSMatt Macy send_range_after(const struct send_range *from, const struct send_range *to)
1269eda14cbcSMatt Macy {
1270eda14cbcSMatt Macy 	if (from->eos_marker == B_TRUE)
1271eda14cbcSMatt Macy 		return (1);
1272eda14cbcSMatt Macy 	if (to->eos_marker == B_TRUE)
1273eda14cbcSMatt Macy 		return (-1);
1274eda14cbcSMatt Macy 
1275eda14cbcSMatt Macy 	uint64_t from_obj = from->object;
1276eda14cbcSMatt Macy 	uint64_t from_end_obj = from->object + 1;
1277eda14cbcSMatt Macy 	uint64_t to_obj = to->object;
1278eda14cbcSMatt Macy 	uint64_t to_end_obj = to->object + 1;
1279eda14cbcSMatt Macy 	if (from_obj == 0) {
1280eda14cbcSMatt Macy 		ASSERT(from->type == HOLE || from->type == OBJECT_RANGE);
1281eda14cbcSMatt Macy 		from_obj = from->start_blkid << DNODES_PER_BLOCK_SHIFT;
1282eda14cbcSMatt Macy 		from_end_obj = from->end_blkid << DNODES_PER_BLOCK_SHIFT;
1283eda14cbcSMatt Macy 	}
1284eda14cbcSMatt Macy 	if (to_obj == 0) {
1285eda14cbcSMatt Macy 		ASSERT(to->type == HOLE || to->type == OBJECT_RANGE);
1286eda14cbcSMatt Macy 		to_obj = to->start_blkid << DNODES_PER_BLOCK_SHIFT;
1287eda14cbcSMatt Macy 		to_end_obj = to->end_blkid << DNODES_PER_BLOCK_SHIFT;
1288eda14cbcSMatt Macy 	}
1289eda14cbcSMatt Macy 
1290eda14cbcSMatt Macy 	if (from_end_obj <= to_obj)
1291eda14cbcSMatt Macy 		return (-1);
1292eda14cbcSMatt Macy 	if (from_obj >= to_end_obj)
1293eda14cbcSMatt Macy 		return (1);
1294eda14cbcSMatt Macy 	int64_t cmp = TREE_CMP(to->type == OBJECT_RANGE, from->type ==
1295eda14cbcSMatt Macy 	    OBJECT_RANGE);
1296eda14cbcSMatt Macy 	if (unlikely(cmp))
1297eda14cbcSMatt Macy 		return (cmp);
1298eda14cbcSMatt Macy 	cmp = TREE_CMP(to->type == OBJECT, from->type == OBJECT);
1299eda14cbcSMatt Macy 	if (unlikely(cmp))
1300eda14cbcSMatt Macy 		return (cmp);
1301eda14cbcSMatt Macy 	if (from->end_blkid <= to->start_blkid)
1302eda14cbcSMatt Macy 		return (-1);
1303eda14cbcSMatt Macy 	if (from->start_blkid >= to->end_blkid)
1304eda14cbcSMatt Macy 		return (1);
1305eda14cbcSMatt Macy 	return (0);
1306eda14cbcSMatt Macy }
1307eda14cbcSMatt Macy 
1308eda14cbcSMatt Macy /*
1309eda14cbcSMatt Macy  * Pop the new data off the queue, check that the records we receive are in
1310eda14cbcSMatt Macy  * the right order, but do not free the old data.  This is used so that the
1311eda14cbcSMatt Macy  * records can be sent on to the main thread without copying the data.
1312eda14cbcSMatt Macy  */
1313eda14cbcSMatt Macy static struct send_range *
1314eda14cbcSMatt Macy get_next_range_nofree(bqueue_t *bq, struct send_range *prev)
1315eda14cbcSMatt Macy {
1316eda14cbcSMatt Macy 	struct send_range *next = bqueue_dequeue(bq);
1317eda14cbcSMatt Macy 	ASSERT3S(send_range_after(prev, next), ==, -1);
1318eda14cbcSMatt Macy 	return (next);
1319eda14cbcSMatt Macy }
1320eda14cbcSMatt Macy 
1321eda14cbcSMatt Macy /*
1322eda14cbcSMatt Macy  * Pop the new data off the queue, check that the records we receive are in
1323eda14cbcSMatt Macy  * the right order, and free the old data.
1324eda14cbcSMatt Macy  */
1325eda14cbcSMatt Macy static struct send_range *
1326eda14cbcSMatt Macy get_next_range(bqueue_t *bq, struct send_range *prev)
1327eda14cbcSMatt Macy {
1328eda14cbcSMatt Macy 	struct send_range *next = get_next_range_nofree(bq, prev);
1329eda14cbcSMatt Macy 	range_free(prev);
1330eda14cbcSMatt Macy 	return (next);
1331eda14cbcSMatt Macy }
1332eda14cbcSMatt Macy 
1333da5137abSMartin Matuska static __attribute__((noreturn)) void
1334eda14cbcSMatt Macy redact_list_thread(void *arg)
1335eda14cbcSMatt Macy {
1336eda14cbcSMatt Macy 	struct redact_list_thread_arg *rlt_arg = arg;
1337eda14cbcSMatt Macy 	struct send_range *record;
1338eda14cbcSMatt Macy 	fstrans_cookie_t cookie = spl_fstrans_mark();
1339eda14cbcSMatt Macy 	if (rlt_arg->rl != NULL) {
1340eda14cbcSMatt Macy 		struct redact_list_cb_arg rlcba = {0};
1341eda14cbcSMatt Macy 		rlcba.cancel = &rlt_arg->cancel;
1342eda14cbcSMatt Macy 		rlcba.q = &rlt_arg->q;
1343eda14cbcSMatt Macy 		rlcba.num_blocks_visited = rlt_arg->num_blocks_visited;
1344eda14cbcSMatt Macy 		rlcba.mark_redact = rlt_arg->mark_redact;
1345eda14cbcSMatt Macy 		int err = dsl_redaction_list_traverse(rlt_arg->rl,
1346eda14cbcSMatt Macy 		    &rlt_arg->resume, redact_list_cb, &rlcba);
1347eda14cbcSMatt Macy 		if (err != EINTR)
1348eda14cbcSMatt Macy 			rlt_arg->error_code = err;
1349eda14cbcSMatt Macy 	}
1350eda14cbcSMatt Macy 	record = range_alloc(DATA, 0, 0, 0, B_TRUE);
1351eda14cbcSMatt Macy 	bqueue_enqueue_flush(&rlt_arg->q, record, sizeof (*record));
1352eda14cbcSMatt Macy 	spl_fstrans_unmark(cookie);
1353eda14cbcSMatt Macy 
1354eda14cbcSMatt Macy 	thread_exit();
1355eda14cbcSMatt Macy }
1356eda14cbcSMatt Macy 
1357eda14cbcSMatt Macy /*
1358eda14cbcSMatt Macy  * Compare the start point of the two provided ranges. End of stream ranges
1359eda14cbcSMatt Macy  * compare last, objects compare before any data or hole inside that object and
1360eda14cbcSMatt Macy  * multi-object holes that start at the same object.
1361eda14cbcSMatt Macy  */
1362eda14cbcSMatt Macy static int
1363eda14cbcSMatt Macy send_range_start_compare(struct send_range *r1, struct send_range *r2)
1364eda14cbcSMatt Macy {
1365eda14cbcSMatt Macy 	uint64_t r1_objequiv = r1->object;
1366eda14cbcSMatt Macy 	uint64_t r1_l0equiv = r1->start_blkid;
1367eda14cbcSMatt Macy 	uint64_t r2_objequiv = r2->object;
1368eda14cbcSMatt Macy 	uint64_t r2_l0equiv = r2->start_blkid;
1369eda14cbcSMatt Macy 	int64_t cmp = TREE_CMP(r1->eos_marker, r2->eos_marker);
1370eda14cbcSMatt Macy 	if (unlikely(cmp))
1371eda14cbcSMatt Macy 		return (cmp);
1372eda14cbcSMatt Macy 	if (r1->object == 0) {
1373eda14cbcSMatt Macy 		r1_objequiv = r1->start_blkid * DNODES_PER_BLOCK;
1374eda14cbcSMatt Macy 		r1_l0equiv = 0;
1375eda14cbcSMatt Macy 	}
1376eda14cbcSMatt Macy 	if (r2->object == 0) {
1377eda14cbcSMatt Macy 		r2_objequiv = r2->start_blkid * DNODES_PER_BLOCK;
1378eda14cbcSMatt Macy 		r2_l0equiv = 0;
1379eda14cbcSMatt Macy 	}
1380eda14cbcSMatt Macy 
1381eda14cbcSMatt Macy 	cmp = TREE_CMP(r1_objequiv, r2_objequiv);
1382eda14cbcSMatt Macy 	if (likely(cmp))
1383eda14cbcSMatt Macy 		return (cmp);
1384eda14cbcSMatt Macy 	cmp = TREE_CMP(r2->type == OBJECT_RANGE, r1->type == OBJECT_RANGE);
1385eda14cbcSMatt Macy 	if (unlikely(cmp))
1386eda14cbcSMatt Macy 		return (cmp);
1387eda14cbcSMatt Macy 	cmp = TREE_CMP(r2->type == OBJECT, r1->type == OBJECT);
1388eda14cbcSMatt Macy 	if (unlikely(cmp))
1389eda14cbcSMatt Macy 		return (cmp);
1390eda14cbcSMatt Macy 
1391eda14cbcSMatt Macy 	return (TREE_CMP(r1_l0equiv, r2_l0equiv));
1392eda14cbcSMatt Macy }
1393eda14cbcSMatt Macy 
1394eda14cbcSMatt Macy enum q_idx {
1395eda14cbcSMatt Macy 	REDACT_IDX = 0,
1396eda14cbcSMatt Macy 	TO_IDX,
1397eda14cbcSMatt Macy 	FROM_IDX,
1398eda14cbcSMatt Macy 	NUM_THREADS
1399eda14cbcSMatt Macy };
1400eda14cbcSMatt Macy 
1401eda14cbcSMatt Macy /*
1402eda14cbcSMatt Macy  * This function returns the next range the send_merge_thread should operate on.
1403eda14cbcSMatt Macy  * The inputs are two arrays; the first one stores the range at the front of the
1404eda14cbcSMatt Macy  * queues stored in the second one.  The ranges are sorted in descending
1405eda14cbcSMatt Macy  * priority order; the metadata from earlier ranges overrules metadata from
1406eda14cbcSMatt Macy  * later ranges.  out_mask is used to return which threads the ranges came from;
1407eda14cbcSMatt Macy  * bit i is set if ranges[i] started at the same place as the returned range.
1408eda14cbcSMatt Macy  *
1409eda14cbcSMatt Macy  * This code is not hardcoded to compare a specific number of threads; it could
1410eda14cbcSMatt Macy  * be used with any number, just by changing the q_idx enum.
1411eda14cbcSMatt Macy  *
1412eda14cbcSMatt Macy  * The "next range" is the one with the earliest start; if two starts are equal,
1413eda14cbcSMatt Macy  * the highest-priority range is the next to operate on.  If a higher-priority
1414eda14cbcSMatt Macy  * range starts in the middle of the first range, then the first range will be
1415eda14cbcSMatt Macy  * truncated to end where the higher-priority range starts, and we will operate
1416eda14cbcSMatt Macy  * on that one next time.   In this way, we make sure that each block covered by
1417eda14cbcSMatt Macy  * some range gets covered by a returned range, and each block covered is
1418eda14cbcSMatt Macy  * returned using the metadata of the highest-priority range it appears in.
1419eda14cbcSMatt Macy  *
1420eda14cbcSMatt Macy  * For example, if the three ranges at the front of the queues were [2,4),
1421eda14cbcSMatt Macy  * [3,5), and [1,3), then the ranges returned would be [1,2) with the metadata
1422eda14cbcSMatt Macy  * from the third range, [2,4) with the metadata from the first range, and then
1423eda14cbcSMatt Macy  * [4,5) with the metadata from the second.
1424eda14cbcSMatt Macy  */
1425eda14cbcSMatt Macy static struct send_range *
1426eda14cbcSMatt Macy find_next_range(struct send_range **ranges, bqueue_t **qs, uint64_t *out_mask)
1427eda14cbcSMatt Macy {
1428eda14cbcSMatt Macy 	int idx = 0; // index of the range with the earliest start
1429eda14cbcSMatt Macy 	int i;
1430eda14cbcSMatt Macy 	uint64_t bmask = 0;
1431eda14cbcSMatt Macy 	for (i = 1; i < NUM_THREADS; i++) {
1432eda14cbcSMatt Macy 		if (send_range_start_compare(ranges[i], ranges[idx]) < 0)
1433eda14cbcSMatt Macy 			idx = i;
1434eda14cbcSMatt Macy 	}
1435eda14cbcSMatt Macy 	if (ranges[idx]->eos_marker) {
1436eda14cbcSMatt Macy 		struct send_range *ret = range_alloc(DATA, 0, 0, 0, B_TRUE);
1437eda14cbcSMatt Macy 		*out_mask = 0;
1438eda14cbcSMatt Macy 		return (ret);
1439eda14cbcSMatt Macy 	}
1440eda14cbcSMatt Macy 	/*
1441eda14cbcSMatt Macy 	 * Find all the ranges that start at that same point.
1442eda14cbcSMatt Macy 	 */
1443eda14cbcSMatt Macy 	for (i = 0; i < NUM_THREADS; i++) {
1444eda14cbcSMatt Macy 		if (send_range_start_compare(ranges[i], ranges[idx]) == 0)
1445eda14cbcSMatt Macy 			bmask |= 1 << i;
1446eda14cbcSMatt Macy 	}
1447eda14cbcSMatt Macy 	*out_mask = bmask;
1448eda14cbcSMatt Macy 	/*
1449eda14cbcSMatt Macy 	 * OBJECT_RANGE records only come from the TO thread, and should always
1450eda14cbcSMatt Macy 	 * be treated as overlapping with nothing and sent on immediately.  They
1451eda14cbcSMatt Macy 	 * are only used in raw sends, and are never redacted.
1452eda14cbcSMatt Macy 	 */
1453eda14cbcSMatt Macy 	if (ranges[idx]->type == OBJECT_RANGE) {
1454eda14cbcSMatt Macy 		ASSERT3U(idx, ==, TO_IDX);
1455eda14cbcSMatt Macy 		ASSERT3U(*out_mask, ==, 1 << TO_IDX);
1456eda14cbcSMatt Macy 		struct send_range *ret = ranges[idx];
1457eda14cbcSMatt Macy 		ranges[idx] = get_next_range_nofree(qs[idx], ranges[idx]);
1458eda14cbcSMatt Macy 		return (ret);
1459eda14cbcSMatt Macy 	}
1460eda14cbcSMatt Macy 	/*
1461eda14cbcSMatt Macy 	 * Find the first start or end point after the start of the first range.
1462eda14cbcSMatt Macy 	 */
1463eda14cbcSMatt Macy 	uint64_t first_change = ranges[idx]->end_blkid;
1464eda14cbcSMatt Macy 	for (i = 0; i < NUM_THREADS; i++) {
1465eda14cbcSMatt Macy 		if (i == idx || ranges[i]->eos_marker ||
1466eda14cbcSMatt Macy 		    ranges[i]->object > ranges[idx]->object ||
1467eda14cbcSMatt Macy 		    ranges[i]->object == DMU_META_DNODE_OBJECT)
1468eda14cbcSMatt Macy 			continue;
1469eda14cbcSMatt Macy 		ASSERT3U(ranges[i]->object, ==, ranges[idx]->object);
1470eda14cbcSMatt Macy 		if (first_change > ranges[i]->start_blkid &&
1471eda14cbcSMatt Macy 		    (bmask & (1 << i)) == 0)
1472eda14cbcSMatt Macy 			first_change = ranges[i]->start_blkid;
1473eda14cbcSMatt Macy 		else if (first_change > ranges[i]->end_blkid)
1474eda14cbcSMatt Macy 			first_change = ranges[i]->end_blkid;
1475eda14cbcSMatt Macy 	}
1476eda14cbcSMatt Macy 	/*
1477eda14cbcSMatt Macy 	 * Update all ranges to no longer overlap with the range we're
1478eda14cbcSMatt Macy 	 * returning. All such ranges must start at the same place as the range
1479eda14cbcSMatt Macy 	 * being returned, and end at or after first_change. Thus we update
1480eda14cbcSMatt Macy 	 * their start to first_change. If that makes them size 0, then free
1481eda14cbcSMatt Macy 	 * them and pull a new range from that thread.
1482eda14cbcSMatt Macy 	 */
1483eda14cbcSMatt Macy 	for (i = 0; i < NUM_THREADS; i++) {
1484eda14cbcSMatt Macy 		if (i == idx || (bmask & (1 << i)) == 0)
1485eda14cbcSMatt Macy 			continue;
1486eda14cbcSMatt Macy 		ASSERT3U(first_change, >, ranges[i]->start_blkid);
1487eda14cbcSMatt Macy 		ranges[i]->start_blkid = first_change;
1488eda14cbcSMatt Macy 		ASSERT3U(ranges[i]->start_blkid, <=, ranges[i]->end_blkid);
1489eda14cbcSMatt Macy 		if (ranges[i]->start_blkid == ranges[i]->end_blkid)
1490eda14cbcSMatt Macy 			ranges[i] = get_next_range(qs[i], ranges[i]);
1491eda14cbcSMatt Macy 	}
1492eda14cbcSMatt Macy 	/*
1493eda14cbcSMatt Macy 	 * Short-circuit the simple case; if the range doesn't overlap with
1494eda14cbcSMatt Macy 	 * anything else, or it only overlaps with things that start at the same
1495eda14cbcSMatt Macy 	 * place and are longer, send it on.
1496eda14cbcSMatt Macy 	 */
1497eda14cbcSMatt Macy 	if (first_change == ranges[idx]->end_blkid) {
1498eda14cbcSMatt Macy 		struct send_range *ret = ranges[idx];
1499eda14cbcSMatt Macy 		ranges[idx] = get_next_range_nofree(qs[idx], ranges[idx]);
1500eda14cbcSMatt Macy 		return (ret);
1501eda14cbcSMatt Macy 	}
1502eda14cbcSMatt Macy 
1503eda14cbcSMatt Macy 	/*
1504eda14cbcSMatt Macy 	 * Otherwise, return a truncated copy of ranges[idx] and move the start
1505eda14cbcSMatt Macy 	 * of ranges[idx] back to first_change.
1506eda14cbcSMatt Macy 	 */
1507eda14cbcSMatt Macy 	struct send_range *ret = kmem_alloc(sizeof (*ret), KM_SLEEP);
1508eda14cbcSMatt Macy 	*ret = *ranges[idx];
1509eda14cbcSMatt Macy 	ret->end_blkid = first_change;
1510eda14cbcSMatt Macy 	ranges[idx]->start_blkid = first_change;
1511eda14cbcSMatt Macy 	return (ret);
1512eda14cbcSMatt Macy }
1513eda14cbcSMatt Macy 
1514eda14cbcSMatt Macy #define	FROM_AND_REDACT_BITS ((1 << REDACT_IDX) | (1 << FROM_IDX))
1515eda14cbcSMatt Macy 
1516eda14cbcSMatt Macy /*
1517eda14cbcSMatt Macy  * Merge the results from the from thread and the to thread, and then hand the
1518eda14cbcSMatt Macy  * records off to send_prefetch_thread to prefetch them.  If this is not a
1519eda14cbcSMatt Macy  * send from a redaction bookmark, the from thread will push an end of stream
1520eda14cbcSMatt Macy  * record and stop, and we'll just send everything that was changed in the
1521eda14cbcSMatt Macy  * to_ds since the ancestor's creation txg. If it is, then since
1522eda14cbcSMatt Macy  * traverse_dataset has a canonical order, we can compare each change as
1523eda14cbcSMatt Macy  * they're pulled off the queues.  That will give us a stream that is
1524eda14cbcSMatt Macy  * appropriately sorted, and covers all records.  In addition, we pull the
1525eda14cbcSMatt Macy  * data from the redact_list_thread and use that to determine which blocks
1526eda14cbcSMatt Macy  * should be redacted.
1527eda14cbcSMatt Macy  */
1528da5137abSMartin Matuska static __attribute__((noreturn)) void
1529eda14cbcSMatt Macy send_merge_thread(void *arg)
1530eda14cbcSMatt Macy {
1531eda14cbcSMatt Macy 	struct send_merge_thread_arg *smt_arg = arg;
1532eda14cbcSMatt Macy 	struct send_range *front_ranges[NUM_THREADS];
1533eda14cbcSMatt Macy 	bqueue_t *queues[NUM_THREADS];
1534eda14cbcSMatt Macy 	int err = 0;
1535eda14cbcSMatt Macy 	fstrans_cookie_t cookie = spl_fstrans_mark();
1536eda14cbcSMatt Macy 
1537eda14cbcSMatt Macy 	if (smt_arg->redact_arg == NULL) {
1538eda14cbcSMatt Macy 		front_ranges[REDACT_IDX] =
1539eda14cbcSMatt Macy 		    kmem_zalloc(sizeof (struct send_range), KM_SLEEP);
1540eda14cbcSMatt Macy 		front_ranges[REDACT_IDX]->eos_marker = B_TRUE;
1541eda14cbcSMatt Macy 		front_ranges[REDACT_IDX]->type = REDACT;
1542eda14cbcSMatt Macy 		queues[REDACT_IDX] = NULL;
1543eda14cbcSMatt Macy 	} else {
1544eda14cbcSMatt Macy 		front_ranges[REDACT_IDX] =
1545eda14cbcSMatt Macy 		    bqueue_dequeue(&smt_arg->redact_arg->q);
1546eda14cbcSMatt Macy 		queues[REDACT_IDX] = &smt_arg->redact_arg->q;
1547eda14cbcSMatt Macy 	}
1548eda14cbcSMatt Macy 	front_ranges[TO_IDX] = bqueue_dequeue(&smt_arg->to_arg->q);
1549eda14cbcSMatt Macy 	queues[TO_IDX] = &smt_arg->to_arg->q;
1550eda14cbcSMatt Macy 	front_ranges[FROM_IDX] = bqueue_dequeue(&smt_arg->from_arg->q);
1551eda14cbcSMatt Macy 	queues[FROM_IDX] = &smt_arg->from_arg->q;
1552eda14cbcSMatt Macy 	uint64_t mask = 0;
1553eda14cbcSMatt Macy 	struct send_range *range;
1554eda14cbcSMatt Macy 	for (range = find_next_range(front_ranges, queues, &mask);
1555eda14cbcSMatt Macy 	    !range->eos_marker && err == 0 && !smt_arg->cancel;
1556eda14cbcSMatt Macy 	    range = find_next_range(front_ranges, queues, &mask)) {
1557eda14cbcSMatt Macy 		/*
1558eda14cbcSMatt Macy 		 * If the range in question was in both the from redact bookmark
1559eda14cbcSMatt Macy 		 * and the bookmark we're using to redact, then don't send it.
1560eda14cbcSMatt Macy 		 * It's already redacted on the receiving system, so a redaction
1561eda14cbcSMatt Macy 		 * record would be redundant.
1562eda14cbcSMatt Macy 		 */
1563eda14cbcSMatt Macy 		if ((mask & FROM_AND_REDACT_BITS) == FROM_AND_REDACT_BITS) {
1564eda14cbcSMatt Macy 			ASSERT3U(range->type, ==, REDACT);
1565eda14cbcSMatt Macy 			range_free(range);
1566eda14cbcSMatt Macy 			continue;
1567eda14cbcSMatt Macy 		}
1568eda14cbcSMatt Macy 		bqueue_enqueue(&smt_arg->q, range, sizeof (*range));
1569eda14cbcSMatt Macy 
1570eda14cbcSMatt Macy 		if (smt_arg->to_arg->error_code != 0) {
1571eda14cbcSMatt Macy 			err = smt_arg->to_arg->error_code;
1572eda14cbcSMatt Macy 		} else if (smt_arg->from_arg->error_code != 0) {
1573eda14cbcSMatt Macy 			err = smt_arg->from_arg->error_code;
1574eda14cbcSMatt Macy 		} else if (smt_arg->redact_arg != NULL &&
1575eda14cbcSMatt Macy 		    smt_arg->redact_arg->error_code != 0) {
1576eda14cbcSMatt Macy 			err = smt_arg->redact_arg->error_code;
1577eda14cbcSMatt Macy 		}
1578eda14cbcSMatt Macy 	}
1579eda14cbcSMatt Macy 	if (smt_arg->cancel && err == 0)
1580eda14cbcSMatt Macy 		err = SET_ERROR(EINTR);
1581eda14cbcSMatt Macy 	smt_arg->error = err;
1582eda14cbcSMatt Macy 	if (smt_arg->error != 0) {
1583eda14cbcSMatt Macy 		smt_arg->to_arg->cancel = B_TRUE;
1584eda14cbcSMatt Macy 		smt_arg->from_arg->cancel = B_TRUE;
1585eda14cbcSMatt Macy 		if (smt_arg->redact_arg != NULL)
1586eda14cbcSMatt Macy 			smt_arg->redact_arg->cancel = B_TRUE;
1587eda14cbcSMatt Macy 	}
1588eda14cbcSMatt Macy 	for (int i = 0; i < NUM_THREADS; i++) {
1589eda14cbcSMatt Macy 		while (!front_ranges[i]->eos_marker) {
1590eda14cbcSMatt Macy 			front_ranges[i] = get_next_range(queues[i],
1591eda14cbcSMatt Macy 			    front_ranges[i]);
1592eda14cbcSMatt Macy 		}
1593eda14cbcSMatt Macy 		range_free(front_ranges[i]);
1594eda14cbcSMatt Macy 	}
1595eda14cbcSMatt Macy 	range->eos_marker = B_TRUE;
1596eda14cbcSMatt Macy 	bqueue_enqueue_flush(&smt_arg->q, range, 1);
1597eda14cbcSMatt Macy 	spl_fstrans_unmark(cookie);
1598eda14cbcSMatt Macy 	thread_exit();
1599eda14cbcSMatt Macy }
1600eda14cbcSMatt Macy 
1601eda14cbcSMatt Macy struct send_reader_thread_arg {
1602eda14cbcSMatt Macy 	struct send_merge_thread_arg *smta;
1603eda14cbcSMatt Macy 	bqueue_t q;
1604eda14cbcSMatt Macy 	boolean_t cancel;
1605eda14cbcSMatt Macy 	boolean_t issue_reads;
1606eda14cbcSMatt Macy 	uint64_t featureflags;
1607eda14cbcSMatt Macy 	int error;
1608eda14cbcSMatt Macy };
1609eda14cbcSMatt Macy 
1610eda14cbcSMatt Macy static void
1611eda14cbcSMatt Macy dmu_send_read_done(zio_t *zio)
1612eda14cbcSMatt Macy {
1613eda14cbcSMatt Macy 	struct send_range *range = zio->io_private;
1614eda14cbcSMatt Macy 
1615eda14cbcSMatt Macy 	mutex_enter(&range->sru.data.lock);
1616eda14cbcSMatt Macy 	if (zio->io_error != 0) {
1617eda14cbcSMatt Macy 		abd_free(range->sru.data.abd);
1618eda14cbcSMatt Macy 		range->sru.data.abd = NULL;
1619eda14cbcSMatt Macy 		range->sru.data.io_err = zio->io_error;
1620eda14cbcSMatt Macy 	}
1621eda14cbcSMatt Macy 
1622eda14cbcSMatt Macy 	ASSERT(range->sru.data.io_outstanding);
1623eda14cbcSMatt Macy 	range->sru.data.io_outstanding = B_FALSE;
1624eda14cbcSMatt Macy 	cv_broadcast(&range->sru.data.cv);
1625eda14cbcSMatt Macy 	mutex_exit(&range->sru.data.lock);
1626eda14cbcSMatt Macy }
1627eda14cbcSMatt Macy 
1628eda14cbcSMatt Macy static void
1629eda14cbcSMatt Macy issue_data_read(struct send_reader_thread_arg *srta, struct send_range *range)
1630eda14cbcSMatt Macy {
1631eda14cbcSMatt Macy 	struct srd *srdp = &range->sru.data;
1632eda14cbcSMatt Macy 	blkptr_t *bp = &srdp->bp;
1633eda14cbcSMatt Macy 	objset_t *os = srta->smta->os;
1634eda14cbcSMatt Macy 
1635eda14cbcSMatt Macy 	ASSERT3U(range->type, ==, DATA);
1636eda14cbcSMatt Macy 	ASSERT3U(range->start_blkid + 1, ==, range->end_blkid);
1637eda14cbcSMatt Macy 	/*
1638eda14cbcSMatt Macy 	 * If we have large blocks stored on disk but
1639eda14cbcSMatt Macy 	 * the send flags don't allow us to send large
1640eda14cbcSMatt Macy 	 * blocks, we split the data from the arc buf
1641eda14cbcSMatt Macy 	 * into chunks.
1642eda14cbcSMatt Macy 	 */
1643eda14cbcSMatt Macy 	boolean_t split_large_blocks =
1644eda14cbcSMatt Macy 	    srdp->datablksz > SPA_OLD_MAXBLOCKSIZE &&
1645eda14cbcSMatt Macy 	    !(srta->featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS);
1646eda14cbcSMatt Macy 	/*
1647eda14cbcSMatt Macy 	 * We should only request compressed data from the ARC if all
1648eda14cbcSMatt Macy 	 * the following are true:
1649eda14cbcSMatt Macy 	 *  - stream compression was requested
1650eda14cbcSMatt Macy 	 *  - we aren't splitting large blocks into smaller chunks
1651eda14cbcSMatt Macy 	 *  - the data won't need to be byteswapped before sending
1652eda14cbcSMatt Macy 	 *  - this isn't an embedded block
1653eda14cbcSMatt Macy 	 *  - this isn't metadata (if receiving on a different endian
1654eda14cbcSMatt Macy 	 *    system it can be byteswapped more easily)
1655eda14cbcSMatt Macy 	 */
1656eda14cbcSMatt Macy 	boolean_t request_compressed =
1657eda14cbcSMatt Macy 	    (srta->featureflags & DMU_BACKUP_FEATURE_COMPRESSED) &&
1658eda14cbcSMatt Macy 	    !split_large_blocks && !BP_SHOULD_BYTESWAP(bp) &&
1659eda14cbcSMatt Macy 	    !BP_IS_EMBEDDED(bp) && !DMU_OT_IS_METADATA(BP_GET_TYPE(bp));
1660eda14cbcSMatt Macy 
1661dbd5678dSMartin Matuska 	zio_flag_t zioflags = ZIO_FLAG_CANFAIL;
1662eda14cbcSMatt Macy 
1663681ce946SMartin Matuska 	if (srta->featureflags & DMU_BACKUP_FEATURE_RAW) {
1664eda14cbcSMatt Macy 		zioflags |= ZIO_FLAG_RAW;
1665681ce946SMartin Matuska 		srdp->io_compressed = B_TRUE;
1666681ce946SMartin Matuska 	} else if (request_compressed) {
1667eda14cbcSMatt Macy 		zioflags |= ZIO_FLAG_RAW_COMPRESS;
1668681ce946SMartin Matuska 		srdp->io_compressed = B_TRUE;
1669681ce946SMartin Matuska 	}
1670eda14cbcSMatt Macy 
1671eda14cbcSMatt Macy 	srdp->datasz = (zioflags & ZIO_FLAG_RAW_COMPRESS) ?
1672eda14cbcSMatt Macy 	    BP_GET_PSIZE(bp) : BP_GET_LSIZE(bp);
1673eda14cbcSMatt Macy 
1674eda14cbcSMatt Macy 	if (!srta->issue_reads)
1675eda14cbcSMatt Macy 		return;
1676eda14cbcSMatt Macy 	if (BP_IS_REDACTED(bp))
1677eda14cbcSMatt Macy 		return;
1678eda14cbcSMatt Macy 	if (send_do_embed(bp, srta->featureflags))
1679eda14cbcSMatt Macy 		return;
1680eda14cbcSMatt Macy 
1681eda14cbcSMatt Macy 	zbookmark_phys_t zb = {
1682eda14cbcSMatt Macy 	    .zb_objset = dmu_objset_id(os),
1683eda14cbcSMatt Macy 	    .zb_object = range->object,
1684eda14cbcSMatt Macy 	    .zb_level = 0,
1685eda14cbcSMatt Macy 	    .zb_blkid = range->start_blkid,
1686eda14cbcSMatt Macy 	};
1687eda14cbcSMatt Macy 
1688eda14cbcSMatt Macy 	arc_flags_t aflags = ARC_FLAG_CACHED_ONLY;
1689eda14cbcSMatt Macy 
1690eda14cbcSMatt Macy 	int arc_err = arc_read(NULL, os->os_spa, bp,
1691eda14cbcSMatt Macy 	    arc_getbuf_func, &srdp->abuf, ZIO_PRIORITY_ASYNC_READ,
1692eda14cbcSMatt Macy 	    zioflags, &aflags, &zb);
1693eda14cbcSMatt Macy 	/*
1694eda14cbcSMatt Macy 	 * If the data is not already cached in the ARC, we read directly
1695eda14cbcSMatt Macy 	 * from zio.  This avoids the performance overhead of adding a new
1696eda14cbcSMatt Macy 	 * entry to the ARC, and we also avoid polluting the ARC cache with
1697eda14cbcSMatt Macy 	 * data that is not likely to be used in the future.
1698eda14cbcSMatt Macy 	 */
1699eda14cbcSMatt Macy 	if (arc_err != 0) {
1700eda14cbcSMatt Macy 		srdp->abd = abd_alloc_linear(srdp->datasz, B_FALSE);
1701eda14cbcSMatt Macy 		srdp->io_outstanding = B_TRUE;
1702eda14cbcSMatt Macy 		zio_nowait(zio_read(NULL, os->os_spa, bp, srdp->abd,
1703eda14cbcSMatt Macy 		    srdp->datasz, dmu_send_read_done, range,
1704eda14cbcSMatt Macy 		    ZIO_PRIORITY_ASYNC_READ, zioflags, &zb));
1705eda14cbcSMatt Macy 	}
1706eda14cbcSMatt Macy }
1707eda14cbcSMatt Macy 
1708eda14cbcSMatt Macy /*
1709eda14cbcSMatt Macy  * Create a new record with the given values.
1710eda14cbcSMatt Macy  */
1711eda14cbcSMatt Macy static void
1712eda14cbcSMatt Macy enqueue_range(struct send_reader_thread_arg *srta, bqueue_t *q, dnode_t *dn,
1713eda14cbcSMatt Macy     uint64_t blkid, uint64_t count, const blkptr_t *bp, uint32_t datablksz)
1714eda14cbcSMatt Macy {
1715eda14cbcSMatt Macy 	enum type range_type = (bp == NULL || BP_IS_HOLE(bp) ? HOLE :
1716eda14cbcSMatt Macy 	    (BP_IS_REDACTED(bp) ? REDACT : DATA));
1717eda14cbcSMatt Macy 
1718eda14cbcSMatt Macy 	struct send_range *range = range_alloc(range_type, dn->dn_object,
1719eda14cbcSMatt Macy 	    blkid, blkid + count, B_FALSE);
1720eda14cbcSMatt Macy 
1721*15f0b8c3SMartin Matuska 	if (blkid == DMU_SPILL_BLKID) {
1722*15f0b8c3SMartin Matuska 		ASSERT3P(bp, !=, NULL);
1723eda14cbcSMatt Macy 		ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_SA);
1724*15f0b8c3SMartin Matuska 	}
1725eda14cbcSMatt Macy 
1726eda14cbcSMatt Macy 	switch (range_type) {
1727eda14cbcSMatt Macy 	case HOLE:
1728eda14cbcSMatt Macy 		range->sru.hole.datablksz = datablksz;
1729eda14cbcSMatt Macy 		break;
1730eda14cbcSMatt Macy 	case DATA:
1731eda14cbcSMatt Macy 		ASSERT3U(count, ==, 1);
1732eda14cbcSMatt Macy 		range->sru.data.datablksz = datablksz;
1733eda14cbcSMatt Macy 		range->sru.data.obj_type = dn->dn_type;
1734eda14cbcSMatt Macy 		range->sru.data.bp = *bp;
1735eda14cbcSMatt Macy 		issue_data_read(srta, range);
1736eda14cbcSMatt Macy 		break;
1737eda14cbcSMatt Macy 	case REDACT:
1738eda14cbcSMatt Macy 		range->sru.redact.datablksz = datablksz;
1739eda14cbcSMatt Macy 		break;
1740eda14cbcSMatt Macy 	default:
1741eda14cbcSMatt Macy 		break;
1742eda14cbcSMatt Macy 	}
1743eda14cbcSMatt Macy 	bqueue_enqueue(q, range, datablksz);
1744eda14cbcSMatt Macy }
1745eda14cbcSMatt Macy 
1746eda14cbcSMatt Macy /*
1747eda14cbcSMatt Macy  * This thread is responsible for two things: First, it retrieves the correct
1748eda14cbcSMatt Macy  * blkptr in the to ds if we need to send the data because of something from
1749eda14cbcSMatt Macy  * the from thread.  As a result of this, we're the first ones to discover that
1750eda14cbcSMatt Macy  * some indirect blocks can be discarded because they're not holes. Second,
1751eda14cbcSMatt Macy  * it issues prefetches for the data we need to send.
1752eda14cbcSMatt Macy  */
1753da5137abSMartin Matuska static __attribute__((noreturn)) void
1754eda14cbcSMatt Macy send_reader_thread(void *arg)
1755eda14cbcSMatt Macy {
1756eda14cbcSMatt Macy 	struct send_reader_thread_arg *srta = arg;
1757eda14cbcSMatt Macy 	struct send_merge_thread_arg *smta = srta->smta;
1758eda14cbcSMatt Macy 	bqueue_t *inq = &smta->q;
1759eda14cbcSMatt Macy 	bqueue_t *outq = &srta->q;
1760eda14cbcSMatt Macy 	objset_t *os = smta->os;
1761eda14cbcSMatt Macy 	fstrans_cookie_t cookie = spl_fstrans_mark();
1762eda14cbcSMatt Macy 	struct send_range *range = bqueue_dequeue(inq);
1763eda14cbcSMatt Macy 	int err = 0;
1764eda14cbcSMatt Macy 
1765eda14cbcSMatt Macy 	/*
1766eda14cbcSMatt Macy 	 * If the record we're analyzing is from a redaction bookmark from the
1767eda14cbcSMatt Macy 	 * fromds, then we need to know whether or not it exists in the tods so
1768eda14cbcSMatt Macy 	 * we know whether to create records for it or not. If it does, we need
1769eda14cbcSMatt Macy 	 * the datablksz so we can generate an appropriate record for it.
1770eda14cbcSMatt Macy 	 * Finally, if it isn't redacted, we need the blkptr so that we can send
1771eda14cbcSMatt Macy 	 * a WRITE record containing the actual data.
1772eda14cbcSMatt Macy 	 */
1773eda14cbcSMatt Macy 	uint64_t last_obj = UINT64_MAX;
1774eda14cbcSMatt Macy 	uint64_t last_obj_exists = B_TRUE;
1775eda14cbcSMatt Macy 	while (!range->eos_marker && !srta->cancel && smta->error == 0 &&
1776eda14cbcSMatt Macy 	    err == 0) {
1777eda14cbcSMatt Macy 		switch (range->type) {
1778eda14cbcSMatt Macy 		case DATA:
1779eda14cbcSMatt Macy 			issue_data_read(srta, range);
1780eda14cbcSMatt Macy 			bqueue_enqueue(outq, range, range->sru.data.datablksz);
1781eda14cbcSMatt Macy 			range = get_next_range_nofree(inq, range);
1782eda14cbcSMatt Macy 			break;
1783eda14cbcSMatt Macy 		case HOLE:
1784eda14cbcSMatt Macy 		case OBJECT:
1785eda14cbcSMatt Macy 		case OBJECT_RANGE:
1786eda14cbcSMatt Macy 		case REDACT: // Redacted blocks must exist
1787eda14cbcSMatt Macy 			bqueue_enqueue(outq, range, sizeof (*range));
1788eda14cbcSMatt Macy 			range = get_next_range_nofree(inq, range);
1789eda14cbcSMatt Macy 			break;
1790eda14cbcSMatt Macy 		case PREVIOUSLY_REDACTED: {
1791eda14cbcSMatt Macy 			/*
1792eda14cbcSMatt Macy 			 * This entry came from the "from bookmark" when
1793eda14cbcSMatt Macy 			 * sending from a bookmark that has a redaction
1794eda14cbcSMatt Macy 			 * list.  We need to check if this object/blkid
1795eda14cbcSMatt Macy 			 * exists in the target ("to") dataset, and if
1796eda14cbcSMatt Macy 			 * not then we drop this entry.  We also need
1797eda14cbcSMatt Macy 			 * to fill in the block pointer so that we know
1798eda14cbcSMatt Macy 			 * what to prefetch.
1799eda14cbcSMatt Macy 			 *
1800eda14cbcSMatt Macy 			 * To accomplish the above, we first cache whether or
1801eda14cbcSMatt Macy 			 * not the last object we examined exists.  If it
1802eda14cbcSMatt Macy 			 * doesn't, we can drop this record. If it does, we hold
1803eda14cbcSMatt Macy 			 * the dnode and use it to call dbuf_dnode_findbp. We do
1804eda14cbcSMatt Macy 			 * this instead of dbuf_bookmark_findbp because we will
1805eda14cbcSMatt Macy 			 * often operate on large ranges, and holding the dnode
1806eda14cbcSMatt Macy 			 * once is more efficient.
1807eda14cbcSMatt Macy 			 */
1808eda14cbcSMatt Macy 			boolean_t object_exists = B_TRUE;
1809eda14cbcSMatt Macy 			/*
1810eda14cbcSMatt Macy 			 * If the data is redacted, we only care if it exists,
1811eda14cbcSMatt Macy 			 * so that we don't send records for objects that have
1812eda14cbcSMatt Macy 			 * been deleted.
1813eda14cbcSMatt Macy 			 */
1814eda14cbcSMatt Macy 			dnode_t *dn;
1815eda14cbcSMatt Macy 			if (range->object == last_obj && !last_obj_exists) {
1816eda14cbcSMatt Macy 				/*
1817eda14cbcSMatt Macy 				 * If we're still examining the same object as
1818eda14cbcSMatt Macy 				 * previously, and it doesn't exist, we don't
1819eda14cbcSMatt Macy 				 * need to call dbuf_bookmark_findbp.
1820eda14cbcSMatt Macy 				 */
1821eda14cbcSMatt Macy 				object_exists = B_FALSE;
1822eda14cbcSMatt Macy 			} else {
1823eda14cbcSMatt Macy 				err = dnode_hold(os, range->object, FTAG, &dn);
1824eda14cbcSMatt Macy 				if (err == ENOENT) {
1825eda14cbcSMatt Macy 					object_exists = B_FALSE;
1826eda14cbcSMatt Macy 					err = 0;
1827eda14cbcSMatt Macy 				}
1828eda14cbcSMatt Macy 				last_obj = range->object;
1829eda14cbcSMatt Macy 				last_obj_exists = object_exists;
1830eda14cbcSMatt Macy 			}
1831eda14cbcSMatt Macy 
1832eda14cbcSMatt Macy 			if (err != 0) {
1833eda14cbcSMatt Macy 				break;
1834eda14cbcSMatt Macy 			} else if (!object_exists) {
1835eda14cbcSMatt Macy 				/*
1836eda14cbcSMatt Macy 				 * The block was modified, but doesn't
1837eda14cbcSMatt Macy 				 * exist in the to dataset; if it was
1838eda14cbcSMatt Macy 				 * deleted in the to dataset, then we'll
1839eda14cbcSMatt Macy 				 * visit the hole bp for it at some point.
1840eda14cbcSMatt Macy 				 */
1841eda14cbcSMatt Macy 				range = get_next_range(inq, range);
1842eda14cbcSMatt Macy 				continue;
1843eda14cbcSMatt Macy 			}
1844eda14cbcSMatt Macy 			uint64_t file_max =
1845*15f0b8c3SMartin Matuska 			    MIN(dn->dn_maxblkid, range->end_blkid);
1846eda14cbcSMatt Macy 			/*
1847eda14cbcSMatt Macy 			 * The object exists, so we need to try to find the
1848eda14cbcSMatt Macy 			 * blkptr for each block in the range we're processing.
1849eda14cbcSMatt Macy 			 */
1850eda14cbcSMatt Macy 			rw_enter(&dn->dn_struct_rwlock, RW_READER);
1851eda14cbcSMatt Macy 			for (uint64_t blkid = range->start_blkid;
1852eda14cbcSMatt Macy 			    blkid < file_max; blkid++) {
1853eda14cbcSMatt Macy 				blkptr_t bp;
1854eda14cbcSMatt Macy 				uint32_t datablksz =
1855eda14cbcSMatt Macy 				    dn->dn_phys->dn_datablkszsec <<
1856eda14cbcSMatt Macy 				    SPA_MINBLOCKSHIFT;
1857eda14cbcSMatt Macy 				uint64_t offset = blkid * datablksz;
1858eda14cbcSMatt Macy 				/*
1859eda14cbcSMatt Macy 				 * This call finds the next non-hole block in
1860eda14cbcSMatt Macy 				 * the object. This is to prevent a
1861eda14cbcSMatt Macy 				 * performance problem where we're unredacting
1862eda14cbcSMatt Macy 				 * a large hole. Using dnode_next_offset to
1863eda14cbcSMatt Macy 				 * skip over the large hole avoids iterating
1864eda14cbcSMatt Macy 				 * over every block in it.
1865eda14cbcSMatt Macy 				 */
1866eda14cbcSMatt Macy 				err = dnode_next_offset(dn, DNODE_FIND_HAVELOCK,
1867eda14cbcSMatt Macy 				    &offset, 1, 1, 0);
1868eda14cbcSMatt Macy 				if (err == ESRCH) {
1869eda14cbcSMatt Macy 					offset = UINT64_MAX;
1870eda14cbcSMatt Macy 					err = 0;
1871eda14cbcSMatt Macy 				} else if (err != 0) {
1872eda14cbcSMatt Macy 					break;
1873eda14cbcSMatt Macy 				}
1874eda14cbcSMatt Macy 				if (offset != blkid * datablksz) {
1875eda14cbcSMatt Macy 					/*
1876eda14cbcSMatt Macy 					 * if there is a hole from here
1877eda14cbcSMatt Macy 					 * (blkid) to offset
1878eda14cbcSMatt Macy 					 */
1879eda14cbcSMatt Macy 					offset = MIN(offset, file_max *
1880eda14cbcSMatt Macy 					    datablksz);
1881eda14cbcSMatt Macy 					uint64_t nblks = (offset / datablksz) -
1882eda14cbcSMatt Macy 					    blkid;
1883eda14cbcSMatt Macy 					enqueue_range(srta, outq, dn, blkid,
1884eda14cbcSMatt Macy 					    nblks, NULL, datablksz);
1885eda14cbcSMatt Macy 					blkid += nblks;
1886eda14cbcSMatt Macy 				}
1887eda14cbcSMatt Macy 				if (blkid >= file_max)
1888eda14cbcSMatt Macy 					break;
1889eda14cbcSMatt Macy 				err = dbuf_dnode_findbp(dn, 0, blkid, &bp,
1890eda14cbcSMatt Macy 				    NULL, NULL);
1891eda14cbcSMatt Macy 				if (err != 0)
1892eda14cbcSMatt Macy 					break;
1893eda14cbcSMatt Macy 				ASSERT(!BP_IS_HOLE(&bp));
1894eda14cbcSMatt Macy 				enqueue_range(srta, outq, dn, blkid, 1, &bp,
1895eda14cbcSMatt Macy 				    datablksz);
1896eda14cbcSMatt Macy 			}
1897eda14cbcSMatt Macy 			rw_exit(&dn->dn_struct_rwlock);
1898eda14cbcSMatt Macy 			dnode_rele(dn, FTAG);
1899eda14cbcSMatt Macy 			range = get_next_range(inq, range);
1900eda14cbcSMatt Macy 		}
1901eda14cbcSMatt Macy 		}
1902eda14cbcSMatt Macy 	}
1903eda14cbcSMatt Macy 	if (srta->cancel || err != 0) {
1904eda14cbcSMatt Macy 		smta->cancel = B_TRUE;
1905eda14cbcSMatt Macy 		srta->error = err;
1906eda14cbcSMatt Macy 	} else if (smta->error != 0) {
1907eda14cbcSMatt Macy 		srta->error = smta->error;
1908eda14cbcSMatt Macy 	}
1909eda14cbcSMatt Macy 	while (!range->eos_marker)
1910eda14cbcSMatt Macy 		range = get_next_range(inq, range);
1911eda14cbcSMatt Macy 
1912eda14cbcSMatt Macy 	bqueue_enqueue_flush(outq, range, 1);
1913eda14cbcSMatt Macy 	spl_fstrans_unmark(cookie);
1914eda14cbcSMatt Macy 	thread_exit();
1915eda14cbcSMatt Macy }
1916eda14cbcSMatt Macy 
1917eda14cbcSMatt Macy #define	NUM_SNAPS_NOT_REDACTED UINT64_MAX
1918eda14cbcSMatt Macy 
1919eda14cbcSMatt Macy struct dmu_send_params {
1920eda14cbcSMatt Macy 	/* Pool args */
1921a0b956f5SMartin Matuska 	const void *tag; // Tag dp was held with, will be used to release dp.
1922eda14cbcSMatt Macy 	dsl_pool_t *dp;
1923eda14cbcSMatt Macy 	/* To snapshot args */
1924eda14cbcSMatt Macy 	const char *tosnap;
1925eda14cbcSMatt Macy 	dsl_dataset_t *to_ds;
1926eda14cbcSMatt Macy 	/* From snapshot args */
1927eda14cbcSMatt Macy 	zfs_bookmark_phys_t ancestor_zb;
1928eda14cbcSMatt Macy 	uint64_t *fromredactsnaps;
1929eda14cbcSMatt Macy 	/* NUM_SNAPS_NOT_REDACTED if not sending from redaction bookmark */
1930eda14cbcSMatt Macy 	uint64_t numfromredactsnaps;
1931eda14cbcSMatt Macy 	/* Stream params */
1932eda14cbcSMatt Macy 	boolean_t is_clone;
1933eda14cbcSMatt Macy 	boolean_t embedok;
1934eda14cbcSMatt Macy 	boolean_t large_block_ok;
1935eda14cbcSMatt Macy 	boolean_t compressok;
1936eda14cbcSMatt Macy 	boolean_t rawok;
1937eda14cbcSMatt Macy 	boolean_t savedok;
1938eda14cbcSMatt Macy 	uint64_t resumeobj;
1939eda14cbcSMatt Macy 	uint64_t resumeoff;
1940eda14cbcSMatt Macy 	uint64_t saved_guid;
1941eda14cbcSMatt Macy 	zfs_bookmark_phys_t *redactbook;
1942eda14cbcSMatt Macy 	/* Stream output params */
1943eda14cbcSMatt Macy 	dmu_send_outparams_t *dso;
1944eda14cbcSMatt Macy 
1945eda14cbcSMatt Macy 	/* Stream progress params */
1946eda14cbcSMatt Macy 	offset_t *off;
1947eda14cbcSMatt Macy 	int outfd;
1948eda14cbcSMatt Macy 	char saved_toname[MAXNAMELEN];
1949eda14cbcSMatt Macy };
1950eda14cbcSMatt Macy 
1951eda14cbcSMatt Macy static int
1952eda14cbcSMatt Macy setup_featureflags(struct dmu_send_params *dspp, objset_t *os,
1953eda14cbcSMatt Macy     uint64_t *featureflags)
1954eda14cbcSMatt Macy {
1955eda14cbcSMatt Macy 	dsl_dataset_t *to_ds = dspp->to_ds;
1956eda14cbcSMatt Macy 	dsl_pool_t *dp = dspp->dp;
1957eda14cbcSMatt Macy #ifdef _KERNEL
1958eda14cbcSMatt Macy 	if (dmu_objset_type(os) == DMU_OST_ZFS) {
1959eda14cbcSMatt Macy 		uint64_t version;
1960eda14cbcSMatt Macy 		if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &version) != 0)
1961eda14cbcSMatt Macy 			return (SET_ERROR(EINVAL));
1962eda14cbcSMatt Macy 
1963eda14cbcSMatt Macy 		if (version >= ZPL_VERSION_SA)
1964eda14cbcSMatt Macy 			*featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
1965eda14cbcSMatt Macy 	}
1966eda14cbcSMatt Macy #endif
1967eda14cbcSMatt Macy 
1968eda14cbcSMatt Macy 	/* raw sends imply large_block_ok */
1969eda14cbcSMatt Macy 	if ((dspp->rawok || dspp->large_block_ok) &&
1970eda14cbcSMatt Macy 	    dsl_dataset_feature_is_active(to_ds, SPA_FEATURE_LARGE_BLOCKS)) {
1971eda14cbcSMatt Macy 		*featureflags |= DMU_BACKUP_FEATURE_LARGE_BLOCKS;
1972eda14cbcSMatt Macy 	}
1973eda14cbcSMatt Macy 
1974eda14cbcSMatt Macy 	/* encrypted datasets will not have embedded blocks */
1975eda14cbcSMatt Macy 	if ((dspp->embedok || dspp->rawok) && !os->os_encrypted &&
1976eda14cbcSMatt Macy 	    spa_feature_is_active(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA)) {
1977eda14cbcSMatt Macy 		*featureflags |= DMU_BACKUP_FEATURE_EMBED_DATA;
1978eda14cbcSMatt Macy 	}
1979eda14cbcSMatt Macy 
1980eda14cbcSMatt Macy 	/* raw send implies compressok */
1981eda14cbcSMatt Macy 	if (dspp->compressok || dspp->rawok)
1982eda14cbcSMatt Macy 		*featureflags |= DMU_BACKUP_FEATURE_COMPRESSED;
1983eda14cbcSMatt Macy 
1984eda14cbcSMatt Macy 	if (dspp->rawok && os->os_encrypted)
1985eda14cbcSMatt Macy 		*featureflags |= DMU_BACKUP_FEATURE_RAW;
1986eda14cbcSMatt Macy 
1987eda14cbcSMatt Macy 	if ((*featureflags &
1988eda14cbcSMatt Macy 	    (DMU_BACKUP_FEATURE_EMBED_DATA | DMU_BACKUP_FEATURE_COMPRESSED |
1989eda14cbcSMatt Macy 	    DMU_BACKUP_FEATURE_RAW)) != 0 &&
1990eda14cbcSMatt Macy 	    spa_feature_is_active(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS)) {
1991eda14cbcSMatt Macy 		*featureflags |= DMU_BACKUP_FEATURE_LZ4;
1992eda14cbcSMatt Macy 	}
1993eda14cbcSMatt Macy 
1994eda14cbcSMatt Macy 	/*
1995eda14cbcSMatt Macy 	 * We specifically do not include DMU_BACKUP_FEATURE_EMBED_DATA here to
1996eda14cbcSMatt Macy 	 * allow sending ZSTD compressed datasets to a receiver that does not
1997eda14cbcSMatt Macy 	 * support ZSTD
1998eda14cbcSMatt Macy 	 */
1999eda14cbcSMatt Macy 	if ((*featureflags &
2000eda14cbcSMatt Macy 	    (DMU_BACKUP_FEATURE_COMPRESSED | DMU_BACKUP_FEATURE_RAW)) != 0 &&
2001eda14cbcSMatt Macy 	    dsl_dataset_feature_is_active(to_ds, SPA_FEATURE_ZSTD_COMPRESS)) {
2002eda14cbcSMatt Macy 		*featureflags |= DMU_BACKUP_FEATURE_ZSTD;
2003eda14cbcSMatt Macy 	}
2004eda14cbcSMatt Macy 
2005eda14cbcSMatt Macy 	if (dspp->resumeobj != 0 || dspp->resumeoff != 0) {
2006eda14cbcSMatt Macy 		*featureflags |= DMU_BACKUP_FEATURE_RESUMING;
2007eda14cbcSMatt Macy 	}
2008eda14cbcSMatt Macy 
2009eda14cbcSMatt Macy 	if (dspp->redactbook != NULL) {
2010eda14cbcSMatt Macy 		*featureflags |= DMU_BACKUP_FEATURE_REDACTED;
2011eda14cbcSMatt Macy 	}
2012eda14cbcSMatt Macy 
2013eda14cbcSMatt Macy 	if (dsl_dataset_feature_is_active(to_ds, SPA_FEATURE_LARGE_DNODE)) {
2014eda14cbcSMatt Macy 		*featureflags |= DMU_BACKUP_FEATURE_LARGE_DNODE;
2015eda14cbcSMatt Macy 	}
2016eda14cbcSMatt Macy 	return (0);
2017eda14cbcSMatt Macy }
2018eda14cbcSMatt Macy 
2019eda14cbcSMatt Macy static dmu_replay_record_t *
2020eda14cbcSMatt Macy create_begin_record(struct dmu_send_params *dspp, objset_t *os,
2021eda14cbcSMatt Macy     uint64_t featureflags)
2022eda14cbcSMatt Macy {
2023eda14cbcSMatt Macy 	dmu_replay_record_t *drr = kmem_zalloc(sizeof (dmu_replay_record_t),
2024eda14cbcSMatt Macy 	    KM_SLEEP);
2025eda14cbcSMatt Macy 	drr->drr_type = DRR_BEGIN;
2026eda14cbcSMatt Macy 
2027eda14cbcSMatt Macy 	struct drr_begin *drrb = &drr->drr_u.drr_begin;
2028eda14cbcSMatt Macy 	dsl_dataset_t *to_ds = dspp->to_ds;
2029eda14cbcSMatt Macy 
2030eda14cbcSMatt Macy 	drrb->drr_magic = DMU_BACKUP_MAGIC;
2031eda14cbcSMatt Macy 	drrb->drr_creation_time = dsl_dataset_phys(to_ds)->ds_creation_time;
2032eda14cbcSMatt Macy 	drrb->drr_type = dmu_objset_type(os);
2033eda14cbcSMatt Macy 	drrb->drr_toguid = dsl_dataset_phys(to_ds)->ds_guid;
2034eda14cbcSMatt Macy 	drrb->drr_fromguid = dspp->ancestor_zb.zbm_guid;
2035eda14cbcSMatt Macy 
2036eda14cbcSMatt Macy 	DMU_SET_STREAM_HDRTYPE(drrb->drr_versioninfo, DMU_SUBSTREAM);
2037eda14cbcSMatt Macy 	DMU_SET_FEATUREFLAGS(drrb->drr_versioninfo, featureflags);
2038eda14cbcSMatt Macy 
2039eda14cbcSMatt Macy 	if (dspp->is_clone)
2040eda14cbcSMatt Macy 		drrb->drr_flags |= DRR_FLAG_CLONE;
2041eda14cbcSMatt Macy 	if (dsl_dataset_phys(dspp->to_ds)->ds_flags & DS_FLAG_CI_DATASET)
2042eda14cbcSMatt Macy 		drrb->drr_flags |= DRR_FLAG_CI_DATA;
2043eda14cbcSMatt Macy 	if (zfs_send_set_freerecords_bit)
2044eda14cbcSMatt Macy 		drrb->drr_flags |= DRR_FLAG_FREERECORDS;
2045eda14cbcSMatt Macy 	drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_SPILL_BLOCK;
2046eda14cbcSMatt Macy 
2047eda14cbcSMatt Macy 	if (dspp->savedok) {
2048eda14cbcSMatt Macy 		drrb->drr_toguid = dspp->saved_guid;
2049eda14cbcSMatt Macy 		strlcpy(drrb->drr_toname, dspp->saved_toname,
2050eda14cbcSMatt Macy 		    sizeof (drrb->drr_toname));
2051eda14cbcSMatt Macy 	} else {
2052eda14cbcSMatt Macy 		dsl_dataset_name(to_ds, drrb->drr_toname);
2053eda14cbcSMatt Macy 		if (!to_ds->ds_is_snapshot) {
2054eda14cbcSMatt Macy 			(void) strlcat(drrb->drr_toname, "@--head--",
2055eda14cbcSMatt Macy 			    sizeof (drrb->drr_toname));
2056eda14cbcSMatt Macy 		}
2057eda14cbcSMatt Macy 	}
2058eda14cbcSMatt Macy 	return (drr);
2059eda14cbcSMatt Macy }
2060eda14cbcSMatt Macy 
2061eda14cbcSMatt Macy static void
2062eda14cbcSMatt Macy setup_to_thread(struct send_thread_arg *to_arg, objset_t *to_os,
2063eda14cbcSMatt Macy     dmu_sendstatus_t *dssp, uint64_t fromtxg, boolean_t rawok)
2064eda14cbcSMatt Macy {
2065eda14cbcSMatt Macy 	VERIFY0(bqueue_init(&to_arg->q, zfs_send_no_prefetch_queue_ff,
2066eda14cbcSMatt Macy 	    MAX(zfs_send_no_prefetch_queue_length, 2 * zfs_max_recordsize),
2067eda14cbcSMatt Macy 	    offsetof(struct send_range, ln)));
2068eda14cbcSMatt Macy 	to_arg->error_code = 0;
2069eda14cbcSMatt Macy 	to_arg->cancel = B_FALSE;
2070eda14cbcSMatt Macy 	to_arg->os = to_os;
2071eda14cbcSMatt Macy 	to_arg->fromtxg = fromtxg;
2072eda14cbcSMatt Macy 	to_arg->flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA;
2073eda14cbcSMatt Macy 	if (rawok)
2074eda14cbcSMatt Macy 		to_arg->flags |= TRAVERSE_NO_DECRYPT;
207553b70c86SMartin Matuska 	if (zfs_send_corrupt_data)
207653b70c86SMartin Matuska 		to_arg->flags |= TRAVERSE_HARD;
2077eda14cbcSMatt Macy 	to_arg->num_blocks_visited = &dssp->dss_blocks;
2078eda14cbcSMatt Macy 	(void) thread_create(NULL, 0, send_traverse_thread, to_arg, 0,
2079eda14cbcSMatt Macy 	    curproc, TS_RUN, minclsyspri);
2080eda14cbcSMatt Macy }
2081eda14cbcSMatt Macy 
2082eda14cbcSMatt Macy static void
2083eda14cbcSMatt Macy setup_from_thread(struct redact_list_thread_arg *from_arg,
2084eda14cbcSMatt Macy     redaction_list_t *from_rl, dmu_sendstatus_t *dssp)
2085eda14cbcSMatt Macy {
2086eda14cbcSMatt Macy 	VERIFY0(bqueue_init(&from_arg->q, zfs_send_no_prefetch_queue_ff,
2087eda14cbcSMatt Macy 	    MAX(zfs_send_no_prefetch_queue_length, 2 * zfs_max_recordsize),
2088eda14cbcSMatt Macy 	    offsetof(struct send_range, ln)));
2089eda14cbcSMatt Macy 	from_arg->error_code = 0;
2090eda14cbcSMatt Macy 	from_arg->cancel = B_FALSE;
2091eda14cbcSMatt Macy 	from_arg->rl = from_rl;
2092eda14cbcSMatt Macy 	from_arg->mark_redact = B_FALSE;
2093eda14cbcSMatt Macy 	from_arg->num_blocks_visited = &dssp->dss_blocks;
2094eda14cbcSMatt Macy 	/*
2095eda14cbcSMatt Macy 	 * If from_ds is null, send_traverse_thread just returns success and
2096eda14cbcSMatt Macy 	 * enqueues an eos marker.
2097eda14cbcSMatt Macy 	 */
2098eda14cbcSMatt Macy 	(void) thread_create(NULL, 0, redact_list_thread, from_arg, 0,
2099eda14cbcSMatt Macy 	    curproc, TS_RUN, minclsyspri);
2100eda14cbcSMatt Macy }
2101eda14cbcSMatt Macy 
2102eda14cbcSMatt Macy static void
2103eda14cbcSMatt Macy setup_redact_list_thread(struct redact_list_thread_arg *rlt_arg,
2104eda14cbcSMatt Macy     struct dmu_send_params *dspp, redaction_list_t *rl, dmu_sendstatus_t *dssp)
2105eda14cbcSMatt Macy {
2106eda14cbcSMatt Macy 	if (dspp->redactbook == NULL)
2107eda14cbcSMatt Macy 		return;
2108eda14cbcSMatt Macy 
2109eda14cbcSMatt Macy 	rlt_arg->cancel = B_FALSE;
2110eda14cbcSMatt Macy 	VERIFY0(bqueue_init(&rlt_arg->q, zfs_send_no_prefetch_queue_ff,
2111eda14cbcSMatt Macy 	    MAX(zfs_send_no_prefetch_queue_length, 2 * zfs_max_recordsize),
2112eda14cbcSMatt Macy 	    offsetof(struct send_range, ln)));
2113eda14cbcSMatt Macy 	rlt_arg->error_code = 0;
2114eda14cbcSMatt Macy 	rlt_arg->mark_redact = B_TRUE;
2115eda14cbcSMatt Macy 	rlt_arg->rl = rl;
2116eda14cbcSMatt Macy 	rlt_arg->num_blocks_visited = &dssp->dss_blocks;
2117eda14cbcSMatt Macy 
2118eda14cbcSMatt Macy 	(void) thread_create(NULL, 0, redact_list_thread, rlt_arg, 0,
2119eda14cbcSMatt Macy 	    curproc, TS_RUN, minclsyspri);
2120eda14cbcSMatt Macy }
2121eda14cbcSMatt Macy 
2122eda14cbcSMatt Macy static void
2123eda14cbcSMatt Macy setup_merge_thread(struct send_merge_thread_arg *smt_arg,
2124eda14cbcSMatt Macy     struct dmu_send_params *dspp, struct redact_list_thread_arg *from_arg,
2125eda14cbcSMatt Macy     struct send_thread_arg *to_arg, struct redact_list_thread_arg *rlt_arg,
2126eda14cbcSMatt Macy     objset_t *os)
2127eda14cbcSMatt Macy {
2128eda14cbcSMatt Macy 	VERIFY0(bqueue_init(&smt_arg->q, zfs_send_no_prefetch_queue_ff,
2129eda14cbcSMatt Macy 	    MAX(zfs_send_no_prefetch_queue_length, 2 * zfs_max_recordsize),
2130eda14cbcSMatt Macy 	    offsetof(struct send_range, ln)));
2131eda14cbcSMatt Macy 	smt_arg->cancel = B_FALSE;
2132eda14cbcSMatt Macy 	smt_arg->error = 0;
2133eda14cbcSMatt Macy 	smt_arg->from_arg = from_arg;
2134eda14cbcSMatt Macy 	smt_arg->to_arg = to_arg;
2135eda14cbcSMatt Macy 	if (dspp->redactbook != NULL)
2136eda14cbcSMatt Macy 		smt_arg->redact_arg = rlt_arg;
2137eda14cbcSMatt Macy 
2138eda14cbcSMatt Macy 	smt_arg->os = os;
2139eda14cbcSMatt Macy 	(void) thread_create(NULL, 0, send_merge_thread, smt_arg, 0, curproc,
2140eda14cbcSMatt Macy 	    TS_RUN, minclsyspri);
2141eda14cbcSMatt Macy }
2142eda14cbcSMatt Macy 
2143eda14cbcSMatt Macy static void
2144eda14cbcSMatt Macy setup_reader_thread(struct send_reader_thread_arg *srt_arg,
2145eda14cbcSMatt Macy     struct dmu_send_params *dspp, struct send_merge_thread_arg *smt_arg,
2146eda14cbcSMatt Macy     uint64_t featureflags)
2147eda14cbcSMatt Macy {
2148eda14cbcSMatt Macy 	VERIFY0(bqueue_init(&srt_arg->q, zfs_send_queue_ff,
2149eda14cbcSMatt Macy 	    MAX(zfs_send_queue_length, 2 * zfs_max_recordsize),
2150eda14cbcSMatt Macy 	    offsetof(struct send_range, ln)));
2151eda14cbcSMatt Macy 	srt_arg->smta = smt_arg;
2152eda14cbcSMatt Macy 	srt_arg->issue_reads = !dspp->dso->dso_dryrun;
2153eda14cbcSMatt Macy 	srt_arg->featureflags = featureflags;
2154eda14cbcSMatt Macy 	(void) thread_create(NULL, 0, send_reader_thread, srt_arg, 0,
2155eda14cbcSMatt Macy 	    curproc, TS_RUN, minclsyspri);
2156eda14cbcSMatt Macy }
2157eda14cbcSMatt Macy 
2158eda14cbcSMatt Macy static int
2159eda14cbcSMatt Macy setup_resume_points(struct dmu_send_params *dspp,
2160eda14cbcSMatt Macy     struct send_thread_arg *to_arg, struct redact_list_thread_arg *from_arg,
2161eda14cbcSMatt Macy     struct redact_list_thread_arg *rlt_arg,
2162eda14cbcSMatt Macy     struct send_merge_thread_arg *smt_arg, boolean_t resuming, objset_t *os,
2163eda14cbcSMatt Macy     redaction_list_t *redact_rl, nvlist_t *nvl)
2164eda14cbcSMatt Macy {
2165e92ffd9bSMartin Matuska 	(void) smt_arg;
2166eda14cbcSMatt Macy 	dsl_dataset_t *to_ds = dspp->to_ds;
2167eda14cbcSMatt Macy 	int err = 0;
2168eda14cbcSMatt Macy 
2169eda14cbcSMatt Macy 	uint64_t obj = 0;
2170eda14cbcSMatt Macy 	uint64_t blkid = 0;
2171eda14cbcSMatt Macy 	if (resuming) {
2172eda14cbcSMatt Macy 		obj = dspp->resumeobj;
2173eda14cbcSMatt Macy 		dmu_object_info_t to_doi;
2174eda14cbcSMatt Macy 		err = dmu_object_info(os, obj, &to_doi);
2175eda14cbcSMatt Macy 		if (err != 0)
2176eda14cbcSMatt Macy 			return (err);
2177eda14cbcSMatt Macy 
2178eda14cbcSMatt Macy 		blkid = dspp->resumeoff / to_doi.doi_data_block_size;
2179eda14cbcSMatt Macy 	}
2180eda14cbcSMatt Macy 	/*
2181eda14cbcSMatt Macy 	 * If we're resuming a redacted send, we can skip to the appropriate
2182eda14cbcSMatt Macy 	 * point in the redaction bookmark by binary searching through it.
2183eda14cbcSMatt Macy 	 */
2184eda14cbcSMatt Macy 	if (redact_rl != NULL) {
2185eda14cbcSMatt Macy 		SET_BOOKMARK(&rlt_arg->resume, to_ds->ds_object, obj, 0, blkid);
2186eda14cbcSMatt Macy 	}
2187eda14cbcSMatt Macy 
2188eda14cbcSMatt Macy 	SET_BOOKMARK(&to_arg->resume, to_ds->ds_object, obj, 0, blkid);
2189eda14cbcSMatt Macy 	if (nvlist_exists(nvl, BEGINNV_REDACT_FROM_SNAPS)) {
2190eda14cbcSMatt Macy 		uint64_t objset = dspp->ancestor_zb.zbm_redaction_obj;
2191eda14cbcSMatt Macy 		/*
2192eda14cbcSMatt Macy 		 * Note: If the resume point is in an object whose
2193eda14cbcSMatt Macy 		 * blocksize is different in the from vs to snapshots,
2194eda14cbcSMatt Macy 		 * we will have divided by the "wrong" blocksize.
2195eda14cbcSMatt Macy 		 * However, in this case fromsnap's send_cb() will
2196eda14cbcSMatt Macy 		 * detect that the blocksize has changed and therefore
2197eda14cbcSMatt Macy 		 * ignore this object.
2198eda14cbcSMatt Macy 		 *
2199eda14cbcSMatt Macy 		 * If we're resuming a send from a redaction bookmark,
2200eda14cbcSMatt Macy 		 * we still cannot accidentally suggest blocks behind
2201eda14cbcSMatt Macy 		 * the to_ds.  In addition, we know that any blocks in
2202eda14cbcSMatt Macy 		 * the object in the to_ds will have to be sent, since
2203eda14cbcSMatt Macy 		 * the size changed.  Therefore, we can't cause any harm
2204eda14cbcSMatt Macy 		 * this way either.
2205eda14cbcSMatt Macy 		 */
2206eda14cbcSMatt Macy 		SET_BOOKMARK(&from_arg->resume, objset, obj, 0, blkid);
2207eda14cbcSMatt Macy 	}
2208eda14cbcSMatt Macy 	if (resuming) {
2209eda14cbcSMatt Macy 		fnvlist_add_uint64(nvl, BEGINNV_RESUME_OBJECT, dspp->resumeobj);
2210eda14cbcSMatt Macy 		fnvlist_add_uint64(nvl, BEGINNV_RESUME_OFFSET, dspp->resumeoff);
2211eda14cbcSMatt Macy 	}
2212eda14cbcSMatt Macy 	return (0);
2213eda14cbcSMatt Macy }
2214eda14cbcSMatt Macy 
2215eda14cbcSMatt Macy static dmu_sendstatus_t *
2216eda14cbcSMatt Macy setup_send_progress(struct dmu_send_params *dspp)
2217eda14cbcSMatt Macy {
2218eda14cbcSMatt Macy 	dmu_sendstatus_t *dssp = kmem_zalloc(sizeof (*dssp), KM_SLEEP);
2219eda14cbcSMatt Macy 	dssp->dss_outfd = dspp->outfd;
2220eda14cbcSMatt Macy 	dssp->dss_off = dspp->off;
2221eda14cbcSMatt Macy 	dssp->dss_proc = curproc;
2222eda14cbcSMatt Macy 	mutex_enter(&dspp->to_ds->ds_sendstream_lock);
2223eda14cbcSMatt Macy 	list_insert_head(&dspp->to_ds->ds_sendstreams, dssp);
2224eda14cbcSMatt Macy 	mutex_exit(&dspp->to_ds->ds_sendstream_lock);
2225eda14cbcSMatt Macy 	return (dssp);
2226eda14cbcSMatt Macy }
2227eda14cbcSMatt Macy 
2228eda14cbcSMatt Macy /*
2229eda14cbcSMatt Macy  * Actually do the bulk of the work in a zfs send.
2230eda14cbcSMatt Macy  *
2231eda14cbcSMatt Macy  * The idea is that we want to do a send from ancestor_zb to to_ds.  We also
2232eda14cbcSMatt Macy  * want to not send any data that has been modified by all the datasets in
2233eda14cbcSMatt Macy  * redactsnaparr, and store the list of blocks that are redacted in this way in
2234eda14cbcSMatt Macy  * a bookmark named redactbook, created on the to_ds.  We do this by creating
2235eda14cbcSMatt Macy  * several worker threads, whose function is described below.
2236eda14cbcSMatt Macy  *
2237eda14cbcSMatt Macy  * There are three cases.
2238eda14cbcSMatt Macy  * The first case is a redacted zfs send.  In this case there are 5 threads.
2239eda14cbcSMatt Macy  * The first thread is the to_ds traversal thread: it calls dataset_traverse on
2240eda14cbcSMatt Macy  * the to_ds and finds all the blocks that have changed since ancestor_zb (if
2241eda14cbcSMatt Macy  * it's a full send, that's all blocks in the dataset).  It then sends those
2242eda14cbcSMatt Macy  * blocks on to the send merge thread. The redact list thread takes the data
2243eda14cbcSMatt Macy  * from the redaction bookmark and sends those blocks on to the send merge
2244eda14cbcSMatt Macy  * thread.  The send merge thread takes the data from the to_ds traversal
2245eda14cbcSMatt Macy  * thread, and combines it with the redaction records from the redact list
2246eda14cbcSMatt Macy  * thread.  If a block appears in both the to_ds's data and the redaction data,
2247eda14cbcSMatt Macy  * the send merge thread will mark it as redacted and send it on to the prefetch
2248eda14cbcSMatt Macy  * thread.  Otherwise, the send merge thread will send the block on to the
2249eda14cbcSMatt Macy  * prefetch thread unchanged. The prefetch thread will issue prefetch reads for
2250eda14cbcSMatt Macy  * any data that isn't redacted, and then send the data on to the main thread.
2251eda14cbcSMatt Macy  * The main thread behaves the same as in a normal send case, issuing demand
2252eda14cbcSMatt Macy  * reads for data blocks and sending out records over the network
2253eda14cbcSMatt Macy  *
2254eda14cbcSMatt Macy  * The graphic below diagrams the flow of data in the case of a redacted zfs
2255eda14cbcSMatt Macy  * send.  Each box represents a thread, and each line represents the flow of
2256eda14cbcSMatt Macy  * data.
2257eda14cbcSMatt Macy  *
2258eda14cbcSMatt Macy  *             Records from the |
2259eda14cbcSMatt Macy  *           redaction bookmark |
2260eda14cbcSMatt Macy  * +--------------------+       |  +---------------------------+
2261eda14cbcSMatt Macy  * |                    |       v  | Send Merge Thread         |
2262eda14cbcSMatt Macy  * | Redact List Thread +----------> Apply redaction marks to  |
2263eda14cbcSMatt Macy  * |                    |          | records as specified by   |
2264eda14cbcSMatt Macy  * +--------------------+          | redaction ranges          |
2265eda14cbcSMatt Macy  *                                 +----^---------------+------+
2266eda14cbcSMatt Macy  *                                      |               | Merged data
2267eda14cbcSMatt Macy  *                                      |               |
2268eda14cbcSMatt Macy  *                                      |  +------------v--------+
2269eda14cbcSMatt Macy  *                                      |  | Prefetch Thread     |
2270eda14cbcSMatt Macy  * +--------------------+               |  | Issues prefetch     |
2271eda14cbcSMatt Macy  * | to_ds Traversal    |               |  | reads of data blocks|
2272eda14cbcSMatt Macy  * | Thread (finds      +---------------+  +------------+--------+
2273eda14cbcSMatt Macy  * | candidate blocks)  |  Blocks modified              | Prefetched data
2274eda14cbcSMatt Macy  * +--------------------+  by to_ds since               |
2275eda14cbcSMatt Macy  *                         ancestor_zb     +------------v----+
2276eda14cbcSMatt Macy  *                                         | Main Thread     |  File Descriptor
2277eda14cbcSMatt Macy  *                                         | Sends data over +->(to zfs receive)
2278eda14cbcSMatt Macy  *                                         | wire            |
2279eda14cbcSMatt Macy  *                                         +-----------------+
2280eda14cbcSMatt Macy  *
2281eda14cbcSMatt Macy  * The second case is an incremental send from a redaction bookmark.  The to_ds
2282eda14cbcSMatt Macy  * traversal thread and the main thread behave the same as in the redacted
2283eda14cbcSMatt Macy  * send case.  The new thread is the from bookmark traversal thread.  It
2284eda14cbcSMatt Macy  * iterates over the redaction list in the redaction bookmark, and enqueues
2285eda14cbcSMatt Macy  * records for each block that was redacted in the original send.  The send
2286eda14cbcSMatt Macy  * merge thread now has to merge the data from the two threads.  For details
2287eda14cbcSMatt Macy  * about that process, see the header comment of send_merge_thread().  Any data
2288eda14cbcSMatt Macy  * it decides to send on will be prefetched by the prefetch thread.  Note that
2289eda14cbcSMatt Macy  * you can perform a redacted send from a redaction bookmark; in that case,
2290eda14cbcSMatt Macy  * the data flow behaves very similarly to the flow in the redacted send case,
2291eda14cbcSMatt Macy  * except with the addition of the bookmark traversal thread iterating over the
2292eda14cbcSMatt Macy  * redaction bookmark.  The send_merge_thread also has to take on the
2293eda14cbcSMatt Macy  * responsibility of merging the redact list thread's records, the bookmark
2294eda14cbcSMatt Macy  * traversal thread's records, and the to_ds records.
2295eda14cbcSMatt Macy  *
2296eda14cbcSMatt Macy  * +---------------------+
2297eda14cbcSMatt Macy  * |                     |
2298eda14cbcSMatt Macy  * | Redact List Thread  +--------------+
2299eda14cbcSMatt Macy  * |                     |              |
2300eda14cbcSMatt Macy  * +---------------------+              |
2301eda14cbcSMatt Macy  *        Blocks in redaction list      | Ranges modified by every secure snap
2302eda14cbcSMatt Macy  *        of from bookmark              | (or EOS if not readcted)
2303eda14cbcSMatt Macy  *                                      |
2304eda14cbcSMatt Macy  * +---------------------+   |     +----v----------------------+
2305eda14cbcSMatt Macy  * | bookmark Traversal  |   v     | Send Merge Thread         |
2306eda14cbcSMatt Macy  * | Thread (finds       +---------> Merges bookmark, rlt, and |
2307eda14cbcSMatt Macy  * | candidate blocks)   |         | to_ds send records        |
2308eda14cbcSMatt Macy  * +---------------------+         +----^---------------+------+
2309eda14cbcSMatt Macy  *                                      |               | Merged data
2310eda14cbcSMatt Macy  *                                      |  +------------v--------+
2311eda14cbcSMatt Macy  *                                      |  | Prefetch Thread     |
2312eda14cbcSMatt Macy  * +--------------------+               |  | Issues prefetch     |
2313eda14cbcSMatt Macy  * | to_ds Traversal    |               |  | reads of data blocks|
2314eda14cbcSMatt Macy  * | Thread (finds      +---------------+  +------------+--------+
2315eda14cbcSMatt Macy  * | candidate blocks)  |  Blocks modified              | Prefetched data
2316eda14cbcSMatt Macy  * +--------------------+  by to_ds since  +------------v----+
2317eda14cbcSMatt Macy  *                         ancestor_zb     | Main Thread     |  File Descriptor
2318eda14cbcSMatt Macy  *                                         | Sends data over +->(to zfs receive)
2319eda14cbcSMatt Macy  *                                         | wire            |
2320eda14cbcSMatt Macy  *                                         +-----------------+
2321eda14cbcSMatt Macy  *
2322eda14cbcSMatt Macy  * The final case is a simple zfs full or incremental send.  The to_ds traversal
2323eda14cbcSMatt Macy  * thread behaves the same as always. The redact list thread is never started.
2324eda14cbcSMatt Macy  * The send merge thread takes all the blocks that the to_ds traversal thread
2325eda14cbcSMatt Macy  * sends it, prefetches the data, and sends the blocks on to the main thread.
2326eda14cbcSMatt Macy  * The main thread sends the data over the wire.
2327eda14cbcSMatt Macy  *
2328eda14cbcSMatt Macy  * To keep performance acceptable, we want to prefetch the data in the worker
2329eda14cbcSMatt Macy  * threads.  While the to_ds thread could simply use the TRAVERSE_PREFETCH
2330eda14cbcSMatt Macy  * feature built into traverse_dataset, the combining and deletion of records
2331eda14cbcSMatt Macy  * due to redaction and sends from redaction bookmarks mean that we could
2332eda14cbcSMatt Macy  * issue many unnecessary prefetches.  As a result, we only prefetch data
2333eda14cbcSMatt Macy  * after we've determined that the record is not going to be redacted.  To
2334eda14cbcSMatt Macy  * prevent the prefetching from getting too far ahead of the main thread, the
2335eda14cbcSMatt Macy  * blocking queues that are used for communication are capped not by the
2336eda14cbcSMatt Macy  * number of entries in the queue, but by the sum of the size of the
2337eda14cbcSMatt Macy  * prefetches associated with them.  The limit on the amount of data that the
2338eda14cbcSMatt Macy  * thread can prefetch beyond what the main thread has reached is controlled
2339eda14cbcSMatt Macy  * by the global variable zfs_send_queue_length.  In addition, to prevent poor
2340eda14cbcSMatt Macy  * performance in the beginning of a send, we also limit the distance ahead
2341eda14cbcSMatt Macy  * that the traversal threads can be.  That distance is controlled by the
2342eda14cbcSMatt Macy  * zfs_send_no_prefetch_queue_length tunable.
2343eda14cbcSMatt Macy  *
2344eda14cbcSMatt Macy  * Note: Releases dp using the specified tag.
2345eda14cbcSMatt Macy  */
2346eda14cbcSMatt Macy static int
2347eda14cbcSMatt Macy dmu_send_impl(struct dmu_send_params *dspp)
2348eda14cbcSMatt Macy {
2349eda14cbcSMatt Macy 	objset_t *os;
2350eda14cbcSMatt Macy 	dmu_replay_record_t *drr;
2351eda14cbcSMatt Macy 	dmu_sendstatus_t *dssp;
2352eda14cbcSMatt Macy 	dmu_send_cookie_t dsc = {0};
2353eda14cbcSMatt Macy 	int err;
2354eda14cbcSMatt Macy 	uint64_t fromtxg = dspp->ancestor_zb.zbm_creation_txg;
2355eda14cbcSMatt Macy 	uint64_t featureflags = 0;
2356eda14cbcSMatt Macy 	struct redact_list_thread_arg *from_arg;
2357eda14cbcSMatt Macy 	struct send_thread_arg *to_arg;
2358eda14cbcSMatt Macy 	struct redact_list_thread_arg *rlt_arg;
2359eda14cbcSMatt Macy 	struct send_merge_thread_arg *smt_arg;
2360eda14cbcSMatt Macy 	struct send_reader_thread_arg *srt_arg;
2361eda14cbcSMatt Macy 	struct send_range *range;
2362eda14cbcSMatt Macy 	redaction_list_t *from_rl = NULL;
2363eda14cbcSMatt Macy 	redaction_list_t *redact_rl = NULL;
2364eda14cbcSMatt Macy 	boolean_t resuming = (dspp->resumeobj != 0 || dspp->resumeoff != 0);
2365eda14cbcSMatt Macy 	boolean_t book_resuming = resuming;
2366eda14cbcSMatt Macy 
2367eda14cbcSMatt Macy 	dsl_dataset_t *to_ds = dspp->to_ds;
2368eda14cbcSMatt Macy 	zfs_bookmark_phys_t *ancestor_zb = &dspp->ancestor_zb;
2369eda14cbcSMatt Macy 	dsl_pool_t *dp = dspp->dp;
2370a0b956f5SMartin Matuska 	const void *tag = dspp->tag;
2371eda14cbcSMatt Macy 
2372eda14cbcSMatt Macy 	err = dmu_objset_from_ds(to_ds, &os);
2373eda14cbcSMatt Macy 	if (err != 0) {
2374eda14cbcSMatt Macy 		dsl_pool_rele(dp, tag);
2375eda14cbcSMatt Macy 		return (err);
2376eda14cbcSMatt Macy 	}
2377eda14cbcSMatt Macy 
2378eda14cbcSMatt Macy 	/*
2379eda14cbcSMatt Macy 	 * If this is a non-raw send of an encrypted ds, we can ensure that
2380eda14cbcSMatt Macy 	 * the objset_phys_t is authenticated. This is safe because this is
2381eda14cbcSMatt Macy 	 * either a snapshot or we have owned the dataset, ensuring that
2382eda14cbcSMatt Macy 	 * it can't be modified.
2383eda14cbcSMatt Macy 	 */
2384eda14cbcSMatt Macy 	if (!dspp->rawok && os->os_encrypted &&
2385eda14cbcSMatt Macy 	    arc_is_unauthenticated(os->os_phys_buf)) {
2386eda14cbcSMatt Macy 		zbookmark_phys_t zb;
2387eda14cbcSMatt Macy 
2388eda14cbcSMatt Macy 		SET_BOOKMARK(&zb, to_ds->ds_object, ZB_ROOT_OBJECT,
2389eda14cbcSMatt Macy 		    ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
2390eda14cbcSMatt Macy 		err = arc_untransform(os->os_phys_buf, os->os_spa,
2391eda14cbcSMatt Macy 		    &zb, B_FALSE);
2392eda14cbcSMatt Macy 		if (err != 0) {
2393eda14cbcSMatt Macy 			dsl_pool_rele(dp, tag);
2394eda14cbcSMatt Macy 			return (err);
2395eda14cbcSMatt Macy 		}
2396eda14cbcSMatt Macy 
2397eda14cbcSMatt Macy 		ASSERT0(arc_is_unauthenticated(os->os_phys_buf));
2398eda14cbcSMatt Macy 	}
2399eda14cbcSMatt Macy 
2400eda14cbcSMatt Macy 	if ((err = setup_featureflags(dspp, os, &featureflags)) != 0) {
2401eda14cbcSMatt Macy 		dsl_pool_rele(dp, tag);
2402eda14cbcSMatt Macy 		return (err);
2403eda14cbcSMatt Macy 	}
2404eda14cbcSMatt Macy 
2405eda14cbcSMatt Macy 	/*
2406eda14cbcSMatt Macy 	 * If we're doing a redacted send, hold the bookmark's redaction list.
2407eda14cbcSMatt Macy 	 */
2408eda14cbcSMatt Macy 	if (dspp->redactbook != NULL) {
2409eda14cbcSMatt Macy 		err = dsl_redaction_list_hold_obj(dp,
2410eda14cbcSMatt Macy 		    dspp->redactbook->zbm_redaction_obj, FTAG,
2411eda14cbcSMatt Macy 		    &redact_rl);
2412eda14cbcSMatt Macy 		if (err != 0) {
2413eda14cbcSMatt Macy 			dsl_pool_rele(dp, tag);
2414eda14cbcSMatt Macy 			return (SET_ERROR(EINVAL));
2415eda14cbcSMatt Macy 		}
2416eda14cbcSMatt Macy 		dsl_redaction_list_long_hold(dp, redact_rl, FTAG);
2417eda14cbcSMatt Macy 	}
2418eda14cbcSMatt Macy 
2419eda14cbcSMatt Macy 	/*
2420eda14cbcSMatt Macy 	 * If we're sending from a redaction bookmark, hold the redaction list
2421eda14cbcSMatt Macy 	 * so that we can consider sending the redacted blocks.
2422eda14cbcSMatt Macy 	 */
2423eda14cbcSMatt Macy 	if (ancestor_zb->zbm_redaction_obj != 0) {
2424eda14cbcSMatt Macy 		err = dsl_redaction_list_hold_obj(dp,
2425eda14cbcSMatt Macy 		    ancestor_zb->zbm_redaction_obj, FTAG, &from_rl);
2426eda14cbcSMatt Macy 		if (err != 0) {
2427eda14cbcSMatt Macy 			if (redact_rl != NULL) {
2428eda14cbcSMatt Macy 				dsl_redaction_list_long_rele(redact_rl, FTAG);
2429eda14cbcSMatt Macy 				dsl_redaction_list_rele(redact_rl, FTAG);
2430eda14cbcSMatt Macy 			}
2431eda14cbcSMatt Macy 			dsl_pool_rele(dp, tag);
2432eda14cbcSMatt Macy 			return (SET_ERROR(EINVAL));
2433eda14cbcSMatt Macy 		}
2434eda14cbcSMatt Macy 		dsl_redaction_list_long_hold(dp, from_rl, FTAG);
2435eda14cbcSMatt Macy 	}
2436eda14cbcSMatt Macy 
2437eda14cbcSMatt Macy 	dsl_dataset_long_hold(to_ds, FTAG);
2438eda14cbcSMatt Macy 
2439eda14cbcSMatt Macy 	from_arg = kmem_zalloc(sizeof (*from_arg), KM_SLEEP);
2440eda14cbcSMatt Macy 	to_arg = kmem_zalloc(sizeof (*to_arg), KM_SLEEP);
2441eda14cbcSMatt Macy 	rlt_arg = kmem_zalloc(sizeof (*rlt_arg), KM_SLEEP);
2442eda14cbcSMatt Macy 	smt_arg = kmem_zalloc(sizeof (*smt_arg), KM_SLEEP);
2443eda14cbcSMatt Macy 	srt_arg = kmem_zalloc(sizeof (*srt_arg), KM_SLEEP);
2444eda14cbcSMatt Macy 
2445eda14cbcSMatt Macy 	drr = create_begin_record(dspp, os, featureflags);
2446eda14cbcSMatt Macy 	dssp = setup_send_progress(dspp);
2447eda14cbcSMatt Macy 
2448eda14cbcSMatt Macy 	dsc.dsc_drr = drr;
2449eda14cbcSMatt Macy 	dsc.dsc_dso = dspp->dso;
2450eda14cbcSMatt Macy 	dsc.dsc_os = os;
2451eda14cbcSMatt Macy 	dsc.dsc_off = dspp->off;
2452eda14cbcSMatt Macy 	dsc.dsc_toguid = dsl_dataset_phys(to_ds)->ds_guid;
2453eda14cbcSMatt Macy 	dsc.dsc_fromtxg = fromtxg;
2454eda14cbcSMatt Macy 	dsc.dsc_pending_op = PENDING_NONE;
2455eda14cbcSMatt Macy 	dsc.dsc_featureflags = featureflags;
2456eda14cbcSMatt Macy 	dsc.dsc_resume_object = dspp->resumeobj;
2457eda14cbcSMatt Macy 	dsc.dsc_resume_offset = dspp->resumeoff;
2458eda14cbcSMatt Macy 
2459eda14cbcSMatt Macy 	dsl_pool_rele(dp, tag);
2460eda14cbcSMatt Macy 
2461eda14cbcSMatt Macy 	void *payload = NULL;
2462eda14cbcSMatt Macy 	size_t payload_len = 0;
2463eda14cbcSMatt Macy 	nvlist_t *nvl = fnvlist_alloc();
2464eda14cbcSMatt Macy 
2465eda14cbcSMatt Macy 	/*
2466eda14cbcSMatt Macy 	 * If we're doing a redacted send, we include the snapshots we're
2467eda14cbcSMatt Macy 	 * redacted with respect to so that the target system knows what send
2468eda14cbcSMatt Macy 	 * streams can be correctly received on top of this dataset. If we're
2469eda14cbcSMatt Macy 	 * instead sending a redacted dataset, we include the snapshots that the
2470eda14cbcSMatt Macy 	 * dataset was created with respect to.
2471eda14cbcSMatt Macy 	 */
2472eda14cbcSMatt Macy 	if (dspp->redactbook != NULL) {
2473eda14cbcSMatt Macy 		fnvlist_add_uint64_array(nvl, BEGINNV_REDACT_SNAPS,
2474eda14cbcSMatt Macy 		    redact_rl->rl_phys->rlp_snaps,
2475eda14cbcSMatt Macy 		    redact_rl->rl_phys->rlp_num_snaps);
2476eda14cbcSMatt Macy 	} else if (dsl_dataset_feature_is_active(to_ds,
2477eda14cbcSMatt Macy 	    SPA_FEATURE_REDACTED_DATASETS)) {
2478eda14cbcSMatt Macy 		uint64_t *tods_guids;
2479eda14cbcSMatt Macy 		uint64_t length;
2480eda14cbcSMatt Macy 		VERIFY(dsl_dataset_get_uint64_array_feature(to_ds,
2481eda14cbcSMatt Macy 		    SPA_FEATURE_REDACTED_DATASETS, &length, &tods_guids));
2482eda14cbcSMatt Macy 		fnvlist_add_uint64_array(nvl, BEGINNV_REDACT_SNAPS, tods_guids,
2483eda14cbcSMatt Macy 		    length);
2484eda14cbcSMatt Macy 	}
2485eda14cbcSMatt Macy 
2486eda14cbcSMatt Macy 	/*
2487eda14cbcSMatt Macy 	 * If we're sending from a redaction bookmark, then we should retrieve
2488eda14cbcSMatt Macy 	 * the guids of that bookmark so we can send them over the wire.
2489eda14cbcSMatt Macy 	 */
2490eda14cbcSMatt Macy 	if (from_rl != NULL) {
2491eda14cbcSMatt Macy 		fnvlist_add_uint64_array(nvl, BEGINNV_REDACT_FROM_SNAPS,
2492eda14cbcSMatt Macy 		    from_rl->rl_phys->rlp_snaps,
2493eda14cbcSMatt Macy 		    from_rl->rl_phys->rlp_num_snaps);
2494eda14cbcSMatt Macy 	}
2495eda14cbcSMatt Macy 
2496eda14cbcSMatt Macy 	/*
2497eda14cbcSMatt Macy 	 * If the snapshot we're sending from is redacted, include the redaction
2498eda14cbcSMatt Macy 	 * list in the stream.
2499eda14cbcSMatt Macy 	 */
2500eda14cbcSMatt Macy 	if (dspp->numfromredactsnaps != NUM_SNAPS_NOT_REDACTED) {
2501eda14cbcSMatt Macy 		ASSERT3P(from_rl, ==, NULL);
2502eda14cbcSMatt Macy 		fnvlist_add_uint64_array(nvl, BEGINNV_REDACT_FROM_SNAPS,
2503eda14cbcSMatt Macy 		    dspp->fromredactsnaps, (uint_t)dspp->numfromredactsnaps);
2504eda14cbcSMatt Macy 		if (dspp->numfromredactsnaps > 0) {
2505eda14cbcSMatt Macy 			kmem_free(dspp->fromredactsnaps,
2506eda14cbcSMatt Macy 			    dspp->numfromredactsnaps * sizeof (uint64_t));
2507eda14cbcSMatt Macy 			dspp->fromredactsnaps = NULL;
2508eda14cbcSMatt Macy 		}
2509eda14cbcSMatt Macy 	}
2510eda14cbcSMatt Macy 
2511eda14cbcSMatt Macy 	if (resuming || book_resuming) {
2512eda14cbcSMatt Macy 		err = setup_resume_points(dspp, to_arg, from_arg,
2513eda14cbcSMatt Macy 		    rlt_arg, smt_arg, resuming, os, redact_rl, nvl);
2514eda14cbcSMatt Macy 		if (err != 0)
2515eda14cbcSMatt Macy 			goto out;
2516eda14cbcSMatt Macy 	}
2517eda14cbcSMatt Macy 
2518eda14cbcSMatt Macy 	if (featureflags & DMU_BACKUP_FEATURE_RAW) {
2519dbd5678dSMartin Matuska 		uint64_t ivset_guid = ancestor_zb->zbm_ivset_guid;
2520eda14cbcSMatt Macy 		nvlist_t *keynvl = NULL;
2521eda14cbcSMatt Macy 		ASSERT(os->os_encrypted);
2522eda14cbcSMatt Macy 
2523eda14cbcSMatt Macy 		err = dsl_crypto_populate_key_nvlist(os, ivset_guid,
2524eda14cbcSMatt Macy 		    &keynvl);
2525eda14cbcSMatt Macy 		if (err != 0) {
2526eda14cbcSMatt Macy 			fnvlist_free(nvl);
2527eda14cbcSMatt Macy 			goto out;
2528eda14cbcSMatt Macy 		}
2529eda14cbcSMatt Macy 
2530eda14cbcSMatt Macy 		fnvlist_add_nvlist(nvl, "crypt_keydata", keynvl);
2531eda14cbcSMatt Macy 		fnvlist_free(keynvl);
2532eda14cbcSMatt Macy 	}
2533eda14cbcSMatt Macy 
2534eda14cbcSMatt Macy 	if (!nvlist_empty(nvl)) {
2535eda14cbcSMatt Macy 		payload = fnvlist_pack(nvl, &payload_len);
2536eda14cbcSMatt Macy 		drr->drr_payloadlen = payload_len;
2537eda14cbcSMatt Macy 	}
2538eda14cbcSMatt Macy 
2539eda14cbcSMatt Macy 	fnvlist_free(nvl);
2540eda14cbcSMatt Macy 	err = dump_record(&dsc, payload, payload_len);
2541eda14cbcSMatt Macy 	fnvlist_pack_free(payload, payload_len);
2542eda14cbcSMatt Macy 	if (err != 0) {
2543eda14cbcSMatt Macy 		err = dsc.dsc_err;
2544eda14cbcSMatt Macy 		goto out;
2545eda14cbcSMatt Macy 	}
2546eda14cbcSMatt Macy 
2547eda14cbcSMatt Macy 	setup_to_thread(to_arg, os, dssp, fromtxg, dspp->rawok);
2548eda14cbcSMatt Macy 	setup_from_thread(from_arg, from_rl, dssp);
2549eda14cbcSMatt Macy 	setup_redact_list_thread(rlt_arg, dspp, redact_rl, dssp);
2550eda14cbcSMatt Macy 	setup_merge_thread(smt_arg, dspp, from_arg, to_arg, rlt_arg, os);
2551eda14cbcSMatt Macy 	setup_reader_thread(srt_arg, dspp, smt_arg, featureflags);
2552eda14cbcSMatt Macy 
2553eda14cbcSMatt Macy 	range = bqueue_dequeue(&srt_arg->q);
2554eda14cbcSMatt Macy 	while (err == 0 && !range->eos_marker) {
2555eda14cbcSMatt Macy 		err = do_dump(&dsc, range);
2556eda14cbcSMatt Macy 		range = get_next_range(&srt_arg->q, range);
2557eda14cbcSMatt Macy 		if (issig(JUSTLOOKING) && issig(FORREAL))
2558eda14cbcSMatt Macy 			err = SET_ERROR(EINTR);
2559eda14cbcSMatt Macy 	}
2560eda14cbcSMatt Macy 
2561eda14cbcSMatt Macy 	/*
2562eda14cbcSMatt Macy 	 * If we hit an error or are interrupted, cancel our worker threads and
2563eda14cbcSMatt Macy 	 * clear the queue of any pending records.  The threads will pass the
2564eda14cbcSMatt Macy 	 * cancel up the tree of worker threads, and each one will clean up any
2565eda14cbcSMatt Macy 	 * pending records before exiting.
2566eda14cbcSMatt Macy 	 */
2567eda14cbcSMatt Macy 	if (err != 0) {
2568eda14cbcSMatt Macy 		srt_arg->cancel = B_TRUE;
2569eda14cbcSMatt Macy 		while (!range->eos_marker) {
2570eda14cbcSMatt Macy 			range = get_next_range(&srt_arg->q, range);
2571eda14cbcSMatt Macy 		}
2572eda14cbcSMatt Macy 	}
2573eda14cbcSMatt Macy 	range_free(range);
2574eda14cbcSMatt Macy 
2575eda14cbcSMatt Macy 	bqueue_destroy(&srt_arg->q);
2576eda14cbcSMatt Macy 	bqueue_destroy(&smt_arg->q);
2577eda14cbcSMatt Macy 	if (dspp->redactbook != NULL)
2578eda14cbcSMatt Macy 		bqueue_destroy(&rlt_arg->q);
2579eda14cbcSMatt Macy 	bqueue_destroy(&to_arg->q);
2580eda14cbcSMatt Macy 	bqueue_destroy(&from_arg->q);
2581eda14cbcSMatt Macy 
2582eda14cbcSMatt Macy 	if (err == 0 && srt_arg->error != 0)
2583eda14cbcSMatt Macy 		err = srt_arg->error;
2584eda14cbcSMatt Macy 
2585eda14cbcSMatt Macy 	if (err != 0)
2586eda14cbcSMatt Macy 		goto out;
2587eda14cbcSMatt Macy 
2588eda14cbcSMatt Macy 	if (dsc.dsc_pending_op != PENDING_NONE)
2589eda14cbcSMatt Macy 		if (dump_record(&dsc, NULL, 0) != 0)
2590eda14cbcSMatt Macy 			err = SET_ERROR(EINTR);
2591eda14cbcSMatt Macy 
2592eda14cbcSMatt Macy 	if (err != 0) {
2593eda14cbcSMatt Macy 		if (err == EINTR && dsc.dsc_err != 0)
2594eda14cbcSMatt Macy 			err = dsc.dsc_err;
2595eda14cbcSMatt Macy 		goto out;
2596eda14cbcSMatt Macy 	}
2597eda14cbcSMatt Macy 
2598eda14cbcSMatt Macy 	/*
2599eda14cbcSMatt Macy 	 * Send the DRR_END record if this is not a saved stream.
2600eda14cbcSMatt Macy 	 * Otherwise, the omitted DRR_END record will signal to
2601eda14cbcSMatt Macy 	 * the receive side that the stream is incomplete.
2602eda14cbcSMatt Macy 	 */
2603eda14cbcSMatt Macy 	if (!dspp->savedok) {
2604da5137abSMartin Matuska 		memset(drr, 0, sizeof (dmu_replay_record_t));
2605eda14cbcSMatt Macy 		drr->drr_type = DRR_END;
2606eda14cbcSMatt Macy 		drr->drr_u.drr_end.drr_checksum = dsc.dsc_zc;
2607eda14cbcSMatt Macy 		drr->drr_u.drr_end.drr_toguid = dsc.dsc_toguid;
2608eda14cbcSMatt Macy 
2609eda14cbcSMatt Macy 		if (dump_record(&dsc, NULL, 0) != 0)
2610eda14cbcSMatt Macy 			err = dsc.dsc_err;
2611eda14cbcSMatt Macy 	}
2612eda14cbcSMatt Macy out:
2613eda14cbcSMatt Macy 	mutex_enter(&to_ds->ds_sendstream_lock);
2614eda14cbcSMatt Macy 	list_remove(&to_ds->ds_sendstreams, dssp);
2615eda14cbcSMatt Macy 	mutex_exit(&to_ds->ds_sendstream_lock);
2616eda14cbcSMatt Macy 
2617eda14cbcSMatt Macy 	VERIFY(err != 0 || (dsc.dsc_sent_begin &&
2618eda14cbcSMatt Macy 	    (dsc.dsc_sent_end || dspp->savedok)));
2619eda14cbcSMatt Macy 
2620eda14cbcSMatt Macy 	kmem_free(drr, sizeof (dmu_replay_record_t));
2621eda14cbcSMatt Macy 	kmem_free(dssp, sizeof (dmu_sendstatus_t));
2622eda14cbcSMatt Macy 	kmem_free(from_arg, sizeof (*from_arg));
2623eda14cbcSMatt Macy 	kmem_free(to_arg, sizeof (*to_arg));
2624eda14cbcSMatt Macy 	kmem_free(rlt_arg, sizeof (*rlt_arg));
2625eda14cbcSMatt Macy 	kmem_free(smt_arg, sizeof (*smt_arg));
2626eda14cbcSMatt Macy 	kmem_free(srt_arg, sizeof (*srt_arg));
2627eda14cbcSMatt Macy 
2628eda14cbcSMatt Macy 	dsl_dataset_long_rele(to_ds, FTAG);
2629eda14cbcSMatt Macy 	if (from_rl != NULL) {
2630eda14cbcSMatt Macy 		dsl_redaction_list_long_rele(from_rl, FTAG);
2631eda14cbcSMatt Macy 		dsl_redaction_list_rele(from_rl, FTAG);
2632eda14cbcSMatt Macy 	}
2633eda14cbcSMatt Macy 	if (redact_rl != NULL) {
2634eda14cbcSMatt Macy 		dsl_redaction_list_long_rele(redact_rl, FTAG);
2635eda14cbcSMatt Macy 		dsl_redaction_list_rele(redact_rl, FTAG);
2636eda14cbcSMatt Macy 	}
2637eda14cbcSMatt Macy 
2638eda14cbcSMatt Macy 	return (err);
2639eda14cbcSMatt Macy }
2640eda14cbcSMatt Macy 
2641eda14cbcSMatt Macy int
2642eda14cbcSMatt Macy dmu_send_obj(const char *pool, uint64_t tosnap, uint64_t fromsnap,
2643eda14cbcSMatt Macy     boolean_t embedok, boolean_t large_block_ok, boolean_t compressok,
2644eda14cbcSMatt Macy     boolean_t rawok, boolean_t savedok, int outfd, offset_t *off,
2645eda14cbcSMatt Macy     dmu_send_outparams_t *dsop)
2646eda14cbcSMatt Macy {
2647eda14cbcSMatt Macy 	int err;
2648eda14cbcSMatt Macy 	dsl_dataset_t *fromds;
26497877fdebSMatt Macy 	ds_hold_flags_t dsflags;
2650eda14cbcSMatt Macy 	struct dmu_send_params dspp = {0};
2651eda14cbcSMatt Macy 	dspp.embedok = embedok;
2652eda14cbcSMatt Macy 	dspp.large_block_ok = large_block_ok;
2653eda14cbcSMatt Macy 	dspp.compressok = compressok;
2654eda14cbcSMatt Macy 	dspp.outfd = outfd;
2655eda14cbcSMatt Macy 	dspp.off = off;
2656eda14cbcSMatt Macy 	dspp.dso = dsop;
2657eda14cbcSMatt Macy 	dspp.tag = FTAG;
2658eda14cbcSMatt Macy 	dspp.rawok = rawok;
2659eda14cbcSMatt Macy 	dspp.savedok = savedok;
2660eda14cbcSMatt Macy 
26617877fdebSMatt Macy 	dsflags = (rawok) ? DS_HOLD_FLAG_NONE : DS_HOLD_FLAG_DECRYPT;
2662eda14cbcSMatt Macy 	err = dsl_pool_hold(pool, FTAG, &dspp.dp);
2663eda14cbcSMatt Macy 	if (err != 0)
2664eda14cbcSMatt Macy 		return (err);
2665eda14cbcSMatt Macy 
2666eda14cbcSMatt Macy 	err = dsl_dataset_hold_obj_flags(dspp.dp, tosnap, dsflags, FTAG,
2667eda14cbcSMatt Macy 	    &dspp.to_ds);
2668eda14cbcSMatt Macy 	if (err != 0) {
2669eda14cbcSMatt Macy 		dsl_pool_rele(dspp.dp, FTAG);
2670eda14cbcSMatt Macy 		return (err);
2671eda14cbcSMatt Macy 	}
2672eda14cbcSMatt Macy 
2673eda14cbcSMatt Macy 	if (fromsnap != 0) {
2674eda14cbcSMatt Macy 		err = dsl_dataset_hold_obj_flags(dspp.dp, fromsnap, dsflags,
2675eda14cbcSMatt Macy 		    FTAG, &fromds);
2676eda14cbcSMatt Macy 		if (err != 0) {
2677eda14cbcSMatt Macy 			dsl_dataset_rele_flags(dspp.to_ds, dsflags, FTAG);
2678eda14cbcSMatt Macy 			dsl_pool_rele(dspp.dp, FTAG);
2679eda14cbcSMatt Macy 			return (err);
2680eda14cbcSMatt Macy 		}
2681eda14cbcSMatt Macy 		dspp.ancestor_zb.zbm_guid = dsl_dataset_phys(fromds)->ds_guid;
2682eda14cbcSMatt Macy 		dspp.ancestor_zb.zbm_creation_txg =
2683eda14cbcSMatt Macy 		    dsl_dataset_phys(fromds)->ds_creation_txg;
2684eda14cbcSMatt Macy 		dspp.ancestor_zb.zbm_creation_time =
2685eda14cbcSMatt Macy 		    dsl_dataset_phys(fromds)->ds_creation_time;
2686eda14cbcSMatt Macy 
2687eda14cbcSMatt Macy 		if (dsl_dataset_is_zapified(fromds)) {
2688eda14cbcSMatt Macy 			(void) zap_lookup(dspp.dp->dp_meta_objset,
2689eda14cbcSMatt Macy 			    fromds->ds_object, DS_FIELD_IVSET_GUID, 8, 1,
2690eda14cbcSMatt Macy 			    &dspp.ancestor_zb.zbm_ivset_guid);
2691eda14cbcSMatt Macy 		}
2692eda14cbcSMatt Macy 
2693eda14cbcSMatt Macy 		/* See dmu_send for the reasons behind this. */
2694eda14cbcSMatt Macy 		uint64_t *fromredact;
2695eda14cbcSMatt Macy 
2696eda14cbcSMatt Macy 		if (!dsl_dataset_get_uint64_array_feature(fromds,
2697eda14cbcSMatt Macy 		    SPA_FEATURE_REDACTED_DATASETS,
2698eda14cbcSMatt Macy 		    &dspp.numfromredactsnaps,
2699eda14cbcSMatt Macy 		    &fromredact)) {
2700eda14cbcSMatt Macy 			dspp.numfromredactsnaps = NUM_SNAPS_NOT_REDACTED;
2701eda14cbcSMatt Macy 		} else if (dspp.numfromredactsnaps > 0) {
2702eda14cbcSMatt Macy 			uint64_t size = dspp.numfromredactsnaps *
2703eda14cbcSMatt Macy 			    sizeof (uint64_t);
2704eda14cbcSMatt Macy 			dspp.fromredactsnaps = kmem_zalloc(size, KM_SLEEP);
2705da5137abSMartin Matuska 			memcpy(dspp.fromredactsnaps, fromredact, size);
2706eda14cbcSMatt Macy 		}
2707eda14cbcSMatt Macy 
2708c40487d4SMatt Macy 		boolean_t is_before =
2709c40487d4SMatt Macy 		    dsl_dataset_is_before(dspp.to_ds, fromds, 0);
2710eda14cbcSMatt Macy 		dspp.is_clone = (dspp.to_ds->ds_dir !=
2711eda14cbcSMatt Macy 		    fromds->ds_dir);
2712eda14cbcSMatt Macy 		dsl_dataset_rele(fromds, FTAG);
2713c40487d4SMatt Macy 		if (!is_before) {
2714c40487d4SMatt Macy 			dsl_pool_rele(dspp.dp, FTAG);
2715c40487d4SMatt Macy 			err = SET_ERROR(EXDEV);
2716c40487d4SMatt Macy 		} else {
2717eda14cbcSMatt Macy 			err = dmu_send_impl(&dspp);
2718eda14cbcSMatt Macy 		}
2719eda14cbcSMatt Macy 	} else {
2720eda14cbcSMatt Macy 		dspp.numfromredactsnaps = NUM_SNAPS_NOT_REDACTED;
2721eda14cbcSMatt Macy 		err = dmu_send_impl(&dspp);
2722eda14cbcSMatt Macy 	}
2723dbd5678dSMartin Matuska 	if (dspp.fromredactsnaps)
2724dbd5678dSMartin Matuska 		kmem_free(dspp.fromredactsnaps,
2725dbd5678dSMartin Matuska 		    dspp.numfromredactsnaps * sizeof (uint64_t));
2726dbd5678dSMartin Matuska 
2727eda14cbcSMatt Macy 	dsl_dataset_rele(dspp.to_ds, FTAG);
2728eda14cbcSMatt Macy 	return (err);
2729eda14cbcSMatt Macy }
2730eda14cbcSMatt Macy 
2731eda14cbcSMatt Macy int
2732eda14cbcSMatt Macy dmu_send(const char *tosnap, const char *fromsnap, boolean_t embedok,
2733eda14cbcSMatt Macy     boolean_t large_block_ok, boolean_t compressok, boolean_t rawok,
2734eda14cbcSMatt Macy     boolean_t savedok, uint64_t resumeobj, uint64_t resumeoff,
2735eda14cbcSMatt Macy     const char *redactbook, int outfd, offset_t *off,
2736eda14cbcSMatt Macy     dmu_send_outparams_t *dsop)
2737eda14cbcSMatt Macy {
2738eda14cbcSMatt Macy 	int err = 0;
27397877fdebSMatt Macy 	ds_hold_flags_t dsflags;
2740eda14cbcSMatt Macy 	boolean_t owned = B_FALSE;
2741eda14cbcSMatt Macy 	dsl_dataset_t *fromds = NULL;
2742eda14cbcSMatt Macy 	zfs_bookmark_phys_t book = {0};
2743eda14cbcSMatt Macy 	struct dmu_send_params dspp = {0};
2744eda14cbcSMatt Macy 
27457877fdebSMatt Macy 	dsflags = (rawok) ? DS_HOLD_FLAG_NONE : DS_HOLD_FLAG_DECRYPT;
2746eda14cbcSMatt Macy 	dspp.tosnap = tosnap;
2747eda14cbcSMatt Macy 	dspp.embedok = embedok;
2748eda14cbcSMatt Macy 	dspp.large_block_ok = large_block_ok;
2749eda14cbcSMatt Macy 	dspp.compressok = compressok;
2750eda14cbcSMatt Macy 	dspp.outfd = outfd;
2751eda14cbcSMatt Macy 	dspp.off = off;
2752eda14cbcSMatt Macy 	dspp.dso = dsop;
2753eda14cbcSMatt Macy 	dspp.tag = FTAG;
2754eda14cbcSMatt Macy 	dspp.resumeobj = resumeobj;
2755eda14cbcSMatt Macy 	dspp.resumeoff = resumeoff;
2756eda14cbcSMatt Macy 	dspp.rawok = rawok;
2757eda14cbcSMatt Macy 	dspp.savedok = savedok;
2758eda14cbcSMatt Macy 
2759eda14cbcSMatt Macy 	if (fromsnap != NULL && strpbrk(fromsnap, "@#") == NULL)
2760eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
2761eda14cbcSMatt Macy 
2762eda14cbcSMatt Macy 	err = dsl_pool_hold(tosnap, FTAG, &dspp.dp);
2763eda14cbcSMatt Macy 	if (err != 0)
2764eda14cbcSMatt Macy 		return (err);
2765eda14cbcSMatt Macy 
2766eda14cbcSMatt Macy 	if (strchr(tosnap, '@') == NULL && spa_writeable(dspp.dp->dp_spa)) {
2767eda14cbcSMatt Macy 		/*
2768eda14cbcSMatt Macy 		 * We are sending a filesystem or volume.  Ensure
2769eda14cbcSMatt Macy 		 * that it doesn't change by owning the dataset.
2770eda14cbcSMatt Macy 		 */
2771eda14cbcSMatt Macy 
2772eda14cbcSMatt Macy 		if (savedok) {
2773eda14cbcSMatt Macy 			/*
2774eda14cbcSMatt Macy 			 * We are looking for the dataset that represents the
2775eda14cbcSMatt Macy 			 * partially received send stream. If this stream was
2776eda14cbcSMatt Macy 			 * received as a new snapshot of an existing dataset,
2777eda14cbcSMatt Macy 			 * this will be saved in a hidden clone named
2778eda14cbcSMatt Macy 			 * "<pool>/<dataset>/%recv". Otherwise, the stream
2779eda14cbcSMatt Macy 			 * will be saved in the live dataset itself. In
2780eda14cbcSMatt Macy 			 * either case we need to use dsl_dataset_own_force()
2781eda14cbcSMatt Macy 			 * because the stream is marked as inconsistent,
2782eda14cbcSMatt Macy 			 * which would normally make it unavailable to be
2783eda14cbcSMatt Macy 			 * owned.
2784eda14cbcSMatt Macy 			 */
2785eda14cbcSMatt Macy 			char *name = kmem_asprintf("%s/%s", tosnap,
2786eda14cbcSMatt Macy 			    recv_clone_name);
2787eda14cbcSMatt Macy 			err = dsl_dataset_own_force(dspp.dp, name, dsflags,
2788eda14cbcSMatt Macy 			    FTAG, &dspp.to_ds);
2789eda14cbcSMatt Macy 			if (err == ENOENT) {
2790eda14cbcSMatt Macy 				err = dsl_dataset_own_force(dspp.dp, tosnap,
2791eda14cbcSMatt Macy 				    dsflags, FTAG, &dspp.to_ds);
2792eda14cbcSMatt Macy 			}
2793eda14cbcSMatt Macy 
2794eda14cbcSMatt Macy 			if (err == 0) {
2795eda14cbcSMatt Macy 				err = zap_lookup(dspp.dp->dp_meta_objset,
2796eda14cbcSMatt Macy 				    dspp.to_ds->ds_object,
2797eda14cbcSMatt Macy 				    DS_FIELD_RESUME_TOGUID, 8, 1,
2798eda14cbcSMatt Macy 				    &dspp.saved_guid);
2799eda14cbcSMatt Macy 			}
2800eda14cbcSMatt Macy 
2801eda14cbcSMatt Macy 			if (err == 0) {
2802eda14cbcSMatt Macy 				err = zap_lookup(dspp.dp->dp_meta_objset,
2803eda14cbcSMatt Macy 				    dspp.to_ds->ds_object,
2804eda14cbcSMatt Macy 				    DS_FIELD_RESUME_TONAME, 1,
2805eda14cbcSMatt Macy 				    sizeof (dspp.saved_toname),
2806eda14cbcSMatt Macy 				    dspp.saved_toname);
2807eda14cbcSMatt Macy 			}
2808eda14cbcSMatt Macy 			if (err != 0)
2809eda14cbcSMatt Macy 				dsl_dataset_disown(dspp.to_ds, dsflags, FTAG);
2810eda14cbcSMatt Macy 
2811eda14cbcSMatt Macy 			kmem_strfree(name);
2812eda14cbcSMatt Macy 		} else {
2813eda14cbcSMatt Macy 			err = dsl_dataset_own(dspp.dp, tosnap, dsflags,
2814eda14cbcSMatt Macy 			    FTAG, &dspp.to_ds);
2815eda14cbcSMatt Macy 		}
2816eda14cbcSMatt Macy 		owned = B_TRUE;
2817eda14cbcSMatt Macy 	} else {
2818eda14cbcSMatt Macy 		err = dsl_dataset_hold_flags(dspp.dp, tosnap, dsflags, FTAG,
2819eda14cbcSMatt Macy 		    &dspp.to_ds);
2820eda14cbcSMatt Macy 	}
2821eda14cbcSMatt Macy 
2822eda14cbcSMatt Macy 	if (err != 0) {
2823eda14cbcSMatt Macy 		dsl_pool_rele(dspp.dp, FTAG);
2824eda14cbcSMatt Macy 		return (err);
2825eda14cbcSMatt Macy 	}
2826eda14cbcSMatt Macy 
2827eda14cbcSMatt Macy 	if (redactbook != NULL) {
2828eda14cbcSMatt Macy 		char path[ZFS_MAX_DATASET_NAME_LEN];
2829eda14cbcSMatt Macy 		(void) strlcpy(path, tosnap, sizeof (path));
2830eda14cbcSMatt Macy 		char *at = strchr(path, '@');
2831eda14cbcSMatt Macy 		if (at == NULL) {
2832eda14cbcSMatt Macy 			err = EINVAL;
2833eda14cbcSMatt Macy 		} else {
2834eda14cbcSMatt Macy 			(void) snprintf(at, sizeof (path) - (at - path), "#%s",
2835eda14cbcSMatt Macy 			    redactbook);
2836eda14cbcSMatt Macy 			err = dsl_bookmark_lookup(dspp.dp, path,
2837eda14cbcSMatt Macy 			    NULL, &book);
2838eda14cbcSMatt Macy 			dspp.redactbook = &book;
2839eda14cbcSMatt Macy 		}
2840eda14cbcSMatt Macy 	}
2841eda14cbcSMatt Macy 
2842eda14cbcSMatt Macy 	if (err != 0) {
2843eda14cbcSMatt Macy 		dsl_pool_rele(dspp.dp, FTAG);
2844eda14cbcSMatt Macy 		if (owned)
2845eda14cbcSMatt Macy 			dsl_dataset_disown(dspp.to_ds, dsflags, FTAG);
2846eda14cbcSMatt Macy 		else
2847eda14cbcSMatt Macy 			dsl_dataset_rele_flags(dspp.to_ds, dsflags, FTAG);
2848eda14cbcSMatt Macy 		return (err);
2849eda14cbcSMatt Macy 	}
2850eda14cbcSMatt Macy 
2851eda14cbcSMatt Macy 	if (fromsnap != NULL) {
2852eda14cbcSMatt Macy 		zfs_bookmark_phys_t *zb = &dspp.ancestor_zb;
2853eda14cbcSMatt Macy 		int fsnamelen;
2854eda14cbcSMatt Macy 		if (strpbrk(tosnap, "@#") != NULL)
2855eda14cbcSMatt Macy 			fsnamelen = strpbrk(tosnap, "@#") - tosnap;
2856eda14cbcSMatt Macy 		else
2857eda14cbcSMatt Macy 			fsnamelen = strlen(tosnap);
2858eda14cbcSMatt Macy 
2859eda14cbcSMatt Macy 		/*
2860eda14cbcSMatt Macy 		 * If the fromsnap is in a different filesystem, then
2861eda14cbcSMatt Macy 		 * mark the send stream as a clone.
2862eda14cbcSMatt Macy 		 */
2863eda14cbcSMatt Macy 		if (strncmp(tosnap, fromsnap, fsnamelen) != 0 ||
2864eda14cbcSMatt Macy 		    (fromsnap[fsnamelen] != '@' &&
2865eda14cbcSMatt Macy 		    fromsnap[fsnamelen] != '#')) {
2866eda14cbcSMatt Macy 			dspp.is_clone = B_TRUE;
2867eda14cbcSMatt Macy 		}
2868eda14cbcSMatt Macy 
2869eda14cbcSMatt Macy 		if (strchr(fromsnap, '@') != NULL) {
2870eda14cbcSMatt Macy 			err = dsl_dataset_hold(dspp.dp, fromsnap, FTAG,
2871eda14cbcSMatt Macy 			    &fromds);
2872eda14cbcSMatt Macy 
2873eda14cbcSMatt Macy 			if (err != 0) {
2874eda14cbcSMatt Macy 				ASSERT3P(fromds, ==, NULL);
2875eda14cbcSMatt Macy 			} else {
2876eda14cbcSMatt Macy 				/*
2877eda14cbcSMatt Macy 				 * We need to make a deep copy of the redact
2878eda14cbcSMatt Macy 				 * snapshots of the from snapshot, because the
2879eda14cbcSMatt Macy 				 * array will be freed when we evict from_ds.
2880eda14cbcSMatt Macy 				 */
2881eda14cbcSMatt Macy 				uint64_t *fromredact;
2882eda14cbcSMatt Macy 				if (!dsl_dataset_get_uint64_array_feature(
2883eda14cbcSMatt Macy 				    fromds, SPA_FEATURE_REDACTED_DATASETS,
2884eda14cbcSMatt Macy 				    &dspp.numfromredactsnaps,
2885eda14cbcSMatt Macy 				    &fromredact)) {
2886eda14cbcSMatt Macy 					dspp.numfromredactsnaps =
2887eda14cbcSMatt Macy 					    NUM_SNAPS_NOT_REDACTED;
2888eda14cbcSMatt Macy 				} else if (dspp.numfromredactsnaps > 0) {
2889eda14cbcSMatt Macy 					uint64_t size =
2890eda14cbcSMatt Macy 					    dspp.numfromredactsnaps *
2891eda14cbcSMatt Macy 					    sizeof (uint64_t);
2892eda14cbcSMatt Macy 					dspp.fromredactsnaps = kmem_zalloc(size,
2893eda14cbcSMatt Macy 					    KM_SLEEP);
2894da5137abSMartin Matuska 					memcpy(dspp.fromredactsnaps, fromredact,
2895eda14cbcSMatt Macy 					    size);
2896eda14cbcSMatt Macy 				}
2897eda14cbcSMatt Macy 				if (!dsl_dataset_is_before(dspp.to_ds, fromds,
2898eda14cbcSMatt Macy 				    0)) {
2899eda14cbcSMatt Macy 					err = SET_ERROR(EXDEV);
2900eda14cbcSMatt Macy 				} else {
2901eda14cbcSMatt Macy 					zb->zbm_creation_txg =
2902eda14cbcSMatt Macy 					    dsl_dataset_phys(fromds)->
2903eda14cbcSMatt Macy 					    ds_creation_txg;
2904eda14cbcSMatt Macy 					zb->zbm_creation_time =
2905eda14cbcSMatt Macy 					    dsl_dataset_phys(fromds)->
2906eda14cbcSMatt Macy 					    ds_creation_time;
2907eda14cbcSMatt Macy 					zb->zbm_guid =
2908eda14cbcSMatt Macy 					    dsl_dataset_phys(fromds)->ds_guid;
2909eda14cbcSMatt Macy 					zb->zbm_redaction_obj = 0;
2910eda14cbcSMatt Macy 
2911eda14cbcSMatt Macy 					if (dsl_dataset_is_zapified(fromds)) {
2912eda14cbcSMatt Macy 						(void) zap_lookup(
2913eda14cbcSMatt Macy 						    dspp.dp->dp_meta_objset,
2914eda14cbcSMatt Macy 						    fromds->ds_object,
2915eda14cbcSMatt Macy 						    DS_FIELD_IVSET_GUID, 8, 1,
2916eda14cbcSMatt Macy 						    &zb->zbm_ivset_guid);
2917eda14cbcSMatt Macy 					}
2918eda14cbcSMatt Macy 				}
2919eda14cbcSMatt Macy 				dsl_dataset_rele(fromds, FTAG);
2920eda14cbcSMatt Macy 			}
2921eda14cbcSMatt Macy 		} else {
2922eda14cbcSMatt Macy 			dspp.numfromredactsnaps = NUM_SNAPS_NOT_REDACTED;
2923eda14cbcSMatt Macy 			err = dsl_bookmark_lookup(dspp.dp, fromsnap, dspp.to_ds,
2924eda14cbcSMatt Macy 			    zb);
2925eda14cbcSMatt Macy 			if (err == EXDEV && zb->zbm_redaction_obj != 0 &&
2926eda14cbcSMatt Macy 			    zb->zbm_guid ==
2927eda14cbcSMatt Macy 			    dsl_dataset_phys(dspp.to_ds)->ds_guid)
2928eda14cbcSMatt Macy 				err = 0;
2929eda14cbcSMatt Macy 		}
2930eda14cbcSMatt Macy 
2931eda14cbcSMatt Macy 		if (err == 0) {
2932eda14cbcSMatt Macy 			/* dmu_send_impl will call dsl_pool_rele for us. */
2933eda14cbcSMatt Macy 			err = dmu_send_impl(&dspp);
2934eda14cbcSMatt Macy 		} else {
2935dbd5678dSMartin Matuska 			if (dspp.fromredactsnaps)
2936dbd5678dSMartin Matuska 				kmem_free(dspp.fromredactsnaps,
2937dbd5678dSMartin Matuska 				    dspp.numfromredactsnaps *
2938dbd5678dSMartin Matuska 				    sizeof (uint64_t));
2939eda14cbcSMatt Macy 			dsl_pool_rele(dspp.dp, FTAG);
2940eda14cbcSMatt Macy 		}
2941eda14cbcSMatt Macy 	} else {
2942eda14cbcSMatt Macy 		dspp.numfromredactsnaps = NUM_SNAPS_NOT_REDACTED;
2943eda14cbcSMatt Macy 		err = dmu_send_impl(&dspp);
2944eda14cbcSMatt Macy 	}
2945eda14cbcSMatt Macy 	if (owned)
2946eda14cbcSMatt Macy 		dsl_dataset_disown(dspp.to_ds, dsflags, FTAG);
2947eda14cbcSMatt Macy 	else
2948eda14cbcSMatt Macy 		dsl_dataset_rele_flags(dspp.to_ds, dsflags, FTAG);
2949eda14cbcSMatt Macy 	return (err);
2950eda14cbcSMatt Macy }
2951eda14cbcSMatt Macy 
2952eda14cbcSMatt Macy static int
2953eda14cbcSMatt Macy dmu_adjust_send_estimate_for_indirects(dsl_dataset_t *ds, uint64_t uncompressed,
2954eda14cbcSMatt Macy     uint64_t compressed, boolean_t stream_compressed, uint64_t *sizep)
2955eda14cbcSMatt Macy {
2956eda14cbcSMatt Macy 	int err = 0;
2957eda14cbcSMatt Macy 	uint64_t size;
2958eda14cbcSMatt Macy 	/*
2959eda14cbcSMatt Macy 	 * Assume that space (both on-disk and in-stream) is dominated by
2960eda14cbcSMatt Macy 	 * data.  We will adjust for indirect blocks and the copies property,
2961eda14cbcSMatt Macy 	 * but ignore per-object space used (eg, dnodes and DRR_OBJECT records).
2962eda14cbcSMatt Macy 	 */
2963eda14cbcSMatt Macy 
2964eda14cbcSMatt Macy 	uint64_t recordsize;
2965eda14cbcSMatt Macy 	uint64_t record_count;
2966eda14cbcSMatt Macy 	objset_t *os;
2967eda14cbcSMatt Macy 	VERIFY0(dmu_objset_from_ds(ds, &os));
2968eda14cbcSMatt Macy 
2969eda14cbcSMatt Macy 	/* Assume all (uncompressed) blocks are recordsize. */
2970eda14cbcSMatt Macy 	if (zfs_override_estimate_recordsize != 0) {
2971eda14cbcSMatt Macy 		recordsize = zfs_override_estimate_recordsize;
2972eda14cbcSMatt Macy 	} else if (os->os_phys->os_type == DMU_OST_ZVOL) {
2973eda14cbcSMatt Macy 		err = dsl_prop_get_int_ds(ds,
2974eda14cbcSMatt Macy 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &recordsize);
2975eda14cbcSMatt Macy 	} else {
2976eda14cbcSMatt Macy 		err = dsl_prop_get_int_ds(ds,
2977eda14cbcSMatt Macy 		    zfs_prop_to_name(ZFS_PROP_RECORDSIZE), &recordsize);
2978eda14cbcSMatt Macy 	}
2979eda14cbcSMatt Macy 	if (err != 0)
2980eda14cbcSMatt Macy 		return (err);
2981eda14cbcSMatt Macy 	record_count = uncompressed / recordsize;
2982eda14cbcSMatt Macy 
2983eda14cbcSMatt Macy 	/*
2984eda14cbcSMatt Macy 	 * If we're estimating a send size for a compressed stream, use the
2985eda14cbcSMatt Macy 	 * compressed data size to estimate the stream size. Otherwise, use the
2986eda14cbcSMatt Macy 	 * uncompressed data size.
2987eda14cbcSMatt Macy 	 */
2988eda14cbcSMatt Macy 	size = stream_compressed ? compressed : uncompressed;
2989eda14cbcSMatt Macy 
2990eda14cbcSMatt Macy 	/*
2991eda14cbcSMatt Macy 	 * Subtract out approximate space used by indirect blocks.
2992eda14cbcSMatt Macy 	 * Assume most space is used by data blocks (non-indirect, non-dnode).
2993eda14cbcSMatt Macy 	 * Assume no ditto blocks or internal fragmentation.
2994eda14cbcSMatt Macy 	 *
2995eda14cbcSMatt Macy 	 * Therefore, space used by indirect blocks is sizeof(blkptr_t) per
2996eda14cbcSMatt Macy 	 * block.
2997eda14cbcSMatt Macy 	 */
2998eda14cbcSMatt Macy 	size -= record_count * sizeof (blkptr_t);
2999eda14cbcSMatt Macy 
3000eda14cbcSMatt Macy 	/* Add in the space for the record associated with each block. */
3001eda14cbcSMatt Macy 	size += record_count * sizeof (dmu_replay_record_t);
3002eda14cbcSMatt Macy 
3003eda14cbcSMatt Macy 	*sizep = size;
3004eda14cbcSMatt Macy 
3005eda14cbcSMatt Macy 	return (0);
3006eda14cbcSMatt Macy }
3007eda14cbcSMatt Macy 
3008eda14cbcSMatt Macy int
3009eda14cbcSMatt Macy dmu_send_estimate_fast(dsl_dataset_t *origds, dsl_dataset_t *fromds,
3010eda14cbcSMatt Macy     zfs_bookmark_phys_t *frombook, boolean_t stream_compressed,
3011eda14cbcSMatt Macy     boolean_t saved, uint64_t *sizep)
3012eda14cbcSMatt Macy {
3013eda14cbcSMatt Macy 	int err;
3014eda14cbcSMatt Macy 	dsl_dataset_t *ds = origds;
3015eda14cbcSMatt Macy 	uint64_t uncomp, comp;
3016eda14cbcSMatt Macy 
3017eda14cbcSMatt Macy 	ASSERT(dsl_pool_config_held(origds->ds_dir->dd_pool));
3018eda14cbcSMatt Macy 	ASSERT(fromds == NULL || frombook == NULL);
3019eda14cbcSMatt Macy 
3020eda14cbcSMatt Macy 	/*
3021eda14cbcSMatt Macy 	 * If this is a saved send we may actually be sending
3022eda14cbcSMatt Macy 	 * from the %recv clone used for resuming.
3023eda14cbcSMatt Macy 	 */
3024eda14cbcSMatt Macy 	if (saved) {
3025eda14cbcSMatt Macy 		objset_t *mos = origds->ds_dir->dd_pool->dp_meta_objset;
3026eda14cbcSMatt Macy 		uint64_t guid;
3027eda14cbcSMatt Macy 		char dsname[ZFS_MAX_DATASET_NAME_LEN + 6];
3028eda14cbcSMatt Macy 
3029eda14cbcSMatt Macy 		dsl_dataset_name(origds, dsname);
3030eda14cbcSMatt Macy 		(void) strcat(dsname, "/");
3031be181ee2SMartin Matuska 		(void) strlcat(dsname, recv_clone_name,
3032be181ee2SMartin Matuska 		    sizeof (dsname) - strlen(dsname));
3033eda14cbcSMatt Macy 
3034eda14cbcSMatt Macy 		err = dsl_dataset_hold(origds->ds_dir->dd_pool,
3035eda14cbcSMatt Macy 		    dsname, FTAG, &ds);
3036eda14cbcSMatt Macy 		if (err != ENOENT && err != 0) {
3037eda14cbcSMatt Macy 			return (err);
3038eda14cbcSMatt Macy 		} else if (err == ENOENT) {
3039eda14cbcSMatt Macy 			ds = origds;
3040eda14cbcSMatt Macy 		}
3041eda14cbcSMatt Macy 
3042eda14cbcSMatt Macy 		/* check that this dataset has partially received data */
3043eda14cbcSMatt Macy 		err = zap_lookup(mos, ds->ds_object,
3044eda14cbcSMatt Macy 		    DS_FIELD_RESUME_TOGUID, 8, 1, &guid);
3045eda14cbcSMatt Macy 		if (err != 0) {
3046eda14cbcSMatt Macy 			err = SET_ERROR(err == ENOENT ? EINVAL : err);
3047eda14cbcSMatt Macy 			goto out;
3048eda14cbcSMatt Macy 		}
3049eda14cbcSMatt Macy 
3050eda14cbcSMatt Macy 		err = zap_lookup(mos, ds->ds_object,
3051eda14cbcSMatt Macy 		    DS_FIELD_RESUME_TONAME, 1, sizeof (dsname), dsname);
3052eda14cbcSMatt Macy 		if (err != 0) {
3053eda14cbcSMatt Macy 			err = SET_ERROR(err == ENOENT ? EINVAL : err);
3054eda14cbcSMatt Macy 			goto out;
3055eda14cbcSMatt Macy 		}
3056eda14cbcSMatt Macy 	}
3057eda14cbcSMatt Macy 
3058eda14cbcSMatt Macy 	/* tosnap must be a snapshot or the target of a saved send */
3059eda14cbcSMatt Macy 	if (!ds->ds_is_snapshot && ds == origds)
3060eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
3061eda14cbcSMatt Macy 
3062eda14cbcSMatt Macy 	if (fromds != NULL) {
3063eda14cbcSMatt Macy 		uint64_t used;
3064eda14cbcSMatt Macy 		if (!fromds->ds_is_snapshot) {
3065eda14cbcSMatt Macy 			err = SET_ERROR(EINVAL);
3066eda14cbcSMatt Macy 			goto out;
3067eda14cbcSMatt Macy 		}
3068eda14cbcSMatt Macy 
3069eda14cbcSMatt Macy 		if (!dsl_dataset_is_before(ds, fromds, 0)) {
3070eda14cbcSMatt Macy 			err = SET_ERROR(EXDEV);
3071eda14cbcSMatt Macy 			goto out;
3072eda14cbcSMatt Macy 		}
3073eda14cbcSMatt Macy 
3074eda14cbcSMatt Macy 		err = dsl_dataset_space_written(fromds, ds, &used, &comp,
3075eda14cbcSMatt Macy 		    &uncomp);
3076eda14cbcSMatt Macy 		if (err != 0)
3077eda14cbcSMatt Macy 			goto out;
3078eda14cbcSMatt Macy 	} else if (frombook != NULL) {
3079eda14cbcSMatt Macy 		uint64_t used;
3080eda14cbcSMatt Macy 		err = dsl_dataset_space_written_bookmark(frombook, ds, &used,
3081eda14cbcSMatt Macy 		    &comp, &uncomp);
3082eda14cbcSMatt Macy 		if (err != 0)
3083eda14cbcSMatt Macy 			goto out;
3084eda14cbcSMatt Macy 	} else {
3085eda14cbcSMatt Macy 		uncomp = dsl_dataset_phys(ds)->ds_uncompressed_bytes;
3086eda14cbcSMatt Macy 		comp = dsl_dataset_phys(ds)->ds_compressed_bytes;
3087eda14cbcSMatt Macy 	}
3088eda14cbcSMatt Macy 
3089eda14cbcSMatt Macy 	err = dmu_adjust_send_estimate_for_indirects(ds, uncomp, comp,
3090eda14cbcSMatt Macy 	    stream_compressed, sizep);
3091eda14cbcSMatt Macy 	/*
3092eda14cbcSMatt Macy 	 * Add the size of the BEGIN and END records to the estimate.
3093eda14cbcSMatt Macy 	 */
3094eda14cbcSMatt Macy 	*sizep += 2 * sizeof (dmu_replay_record_t);
3095eda14cbcSMatt Macy 
3096eda14cbcSMatt Macy out:
3097eda14cbcSMatt Macy 	if (ds != origds)
3098eda14cbcSMatt Macy 		dsl_dataset_rele(ds, FTAG);
3099eda14cbcSMatt Macy 	return (err);
3100eda14cbcSMatt Macy }
3101eda14cbcSMatt Macy 
3102eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs_send, zfs_send_, corrupt_data, INT, ZMOD_RW,
3103eda14cbcSMatt Macy 	"Allow sending corrupt data");
3104eda14cbcSMatt Macy 
3105be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_send, zfs_send_, queue_length, UINT, ZMOD_RW,
3106eda14cbcSMatt Macy 	"Maximum send queue length");
3107eda14cbcSMatt Macy 
3108eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs_send, zfs_send_, unmodified_spill_blocks, INT, ZMOD_RW,
3109eda14cbcSMatt Macy 	"Send unmodified spill blocks");
3110eda14cbcSMatt Macy 
3111be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_send, zfs_send_, no_prefetch_queue_length, UINT, ZMOD_RW,
3112eda14cbcSMatt Macy 	"Maximum send queue length for non-prefetch queues");
3113eda14cbcSMatt Macy 
3114be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_send, zfs_send_, queue_ff, UINT, ZMOD_RW,
3115eda14cbcSMatt Macy 	"Send queue fill fraction");
3116eda14cbcSMatt Macy 
3117be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_send, zfs_send_, no_prefetch_queue_ff, UINT, ZMOD_RW,
3118eda14cbcSMatt Macy 	"Send queue fill fraction for non-prefetch queues");
3119eda14cbcSMatt Macy 
3120be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_send, zfs_, override_estimate_recordsize, UINT, ZMOD_RW,
3121eda14cbcSMatt Macy 	"Override block size estimate with fixed size");
3122