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