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