xref: /freebsd/sys/contrib/openzfs/module/zfs/dmu_recv.c (revision 2a58b312b62f908ec92311d1bd8536dbaeb8e55b)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy /*
22eda14cbcSMatt Macy  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23eda14cbcSMatt Macy  * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24eda14cbcSMatt Macy  * Copyright (c) 2011, 2020 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 (c) 2018, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
28eda14cbcSMatt Macy  * Copyright (c) 2019, Klara Inc.
29eda14cbcSMatt Macy  * Copyright (c) 2019, Allan Jude
30271171e0SMartin Matuska  * Copyright (c) 2019 Datto Inc.
31271171e0SMartin Matuska  * Copyright (c) 2022 Axcient.
32eda14cbcSMatt Macy  */
33eda14cbcSMatt Macy 
3415f0b8c3SMartin Matuska #include <sys/arc.h>
35271171e0SMartin Matuska #include <sys/spa_impl.h>
36eda14cbcSMatt Macy #include <sys/dmu.h>
37eda14cbcSMatt Macy #include <sys/dmu_impl.h>
38eda14cbcSMatt Macy #include <sys/dmu_send.h>
39eda14cbcSMatt Macy #include <sys/dmu_recv.h>
40eda14cbcSMatt Macy #include <sys/dmu_tx.h>
41eda14cbcSMatt Macy #include <sys/dbuf.h>
42eda14cbcSMatt Macy #include <sys/dnode.h>
43eda14cbcSMatt Macy #include <sys/zfs_context.h>
44eda14cbcSMatt Macy #include <sys/dmu_objset.h>
45eda14cbcSMatt Macy #include <sys/dmu_traverse.h>
46eda14cbcSMatt Macy #include <sys/dsl_dataset.h>
47eda14cbcSMatt Macy #include <sys/dsl_dir.h>
48eda14cbcSMatt Macy #include <sys/dsl_prop.h>
49eda14cbcSMatt Macy #include <sys/dsl_pool.h>
50eda14cbcSMatt Macy #include <sys/dsl_synctask.h>
51eda14cbcSMatt Macy #include <sys/zfs_ioctl.h>
52eda14cbcSMatt Macy #include <sys/zap.h>
53eda14cbcSMatt Macy #include <sys/zvol.h>
54eda14cbcSMatt Macy #include <sys/zio_checksum.h>
55eda14cbcSMatt Macy #include <sys/zfs_znode.h>
56eda14cbcSMatt Macy #include <zfs_fletcher.h>
57eda14cbcSMatt Macy #include <sys/avl.h>
58eda14cbcSMatt Macy #include <sys/ddt.h>
59eda14cbcSMatt Macy #include <sys/zfs_onexit.h>
60eda14cbcSMatt Macy #include <sys/dsl_destroy.h>
61eda14cbcSMatt Macy #include <sys/blkptr.h>
62eda14cbcSMatt Macy #include <sys/dsl_bookmark.h>
63eda14cbcSMatt Macy #include <sys/zfeature.h>
64eda14cbcSMatt Macy #include <sys/bqueue.h>
65eda14cbcSMatt Macy #include <sys/objlist.h>
66eda14cbcSMatt Macy #ifdef _KERNEL
67eda14cbcSMatt Macy #include <sys/zfs_vfsops.h>
68eda14cbcSMatt Macy #endif
69eda14cbcSMatt Macy #include <sys/zfs_file.h>
70eda14cbcSMatt Macy 
71be181ee2SMartin Matuska static uint_t zfs_recv_queue_length = SPA_MAXBLOCKSIZE;
72be181ee2SMartin Matuska static uint_t zfs_recv_queue_ff = 20;
73be181ee2SMartin Matuska static uint_t zfs_recv_write_batch_size = 1024 * 1024;
74271171e0SMartin Matuska static int zfs_recv_best_effort_corrective = 0;
75eda14cbcSMatt Macy 
76a0b956f5SMartin Matuska static const void *const dmu_recv_tag = "dmu_recv_tag";
77e92ffd9bSMartin Matuska const char *const recv_clone_name = "%recv";
78eda14cbcSMatt Macy 
7915f0b8c3SMartin Matuska typedef enum {
8015f0b8c3SMartin Matuska 	ORNS_NO,
8115f0b8c3SMartin Matuska 	ORNS_YES,
8215f0b8c3SMartin Matuska 	ORNS_MAYBE
8315f0b8c3SMartin Matuska } or_need_sync_t;
8415f0b8c3SMartin Matuska 
85eda14cbcSMatt Macy static int receive_read_payload_and_next_header(dmu_recv_cookie_t *ra, int len,
86eda14cbcSMatt Macy     void *buf);
87eda14cbcSMatt Macy 
88eda14cbcSMatt Macy struct receive_record_arg {
89eda14cbcSMatt Macy 	dmu_replay_record_t header;
90eda14cbcSMatt Macy 	void *payload; /* Pointer to a buffer containing the payload */
91eda14cbcSMatt Macy 	/*
927877fdebSMatt Macy 	 * If the record is a WRITE or SPILL, pointer to the abd containing the
93eda14cbcSMatt Macy 	 * payload.
94eda14cbcSMatt Macy 	 */
957877fdebSMatt Macy 	abd_t *abd;
96eda14cbcSMatt Macy 	int payload_size;
97eda14cbcSMatt Macy 	uint64_t bytes_read; /* bytes read from stream when record created */
98eda14cbcSMatt Macy 	boolean_t eos_marker; /* Marks the end of the stream */
99eda14cbcSMatt Macy 	bqueue_node_t node;
100eda14cbcSMatt Macy };
101eda14cbcSMatt Macy 
102eda14cbcSMatt Macy struct receive_writer_arg {
103eda14cbcSMatt Macy 	objset_t *os;
104eda14cbcSMatt Macy 	boolean_t byteswap;
105eda14cbcSMatt Macy 	bqueue_t q;
106eda14cbcSMatt Macy 
107eda14cbcSMatt Macy 	/*
1087877fdebSMatt Macy 	 * These three members are used to signal to the main thread when
1097877fdebSMatt Macy 	 * we're done.
110eda14cbcSMatt Macy 	 */
111eda14cbcSMatt Macy 	kmutex_t mutex;
112eda14cbcSMatt Macy 	kcondvar_t cv;
113eda14cbcSMatt Macy 	boolean_t done;
114eda14cbcSMatt Macy 
115eda14cbcSMatt Macy 	int err;
116271171e0SMartin Matuska 	const char *tofs;
117271171e0SMartin Matuska 	boolean_t heal;
118eda14cbcSMatt Macy 	boolean_t resumable;
119eda14cbcSMatt Macy 	boolean_t raw;   /* DMU_BACKUP_FEATURE_RAW set */
120eda14cbcSMatt Macy 	boolean_t spill; /* DRR_FLAG_SPILL_BLOCK set */
121eda14cbcSMatt Macy 	boolean_t full;  /* this is a full send stream */
122eda14cbcSMatt Macy 	uint64_t last_object;
123eda14cbcSMatt Macy 	uint64_t last_offset;
124eda14cbcSMatt Macy 	uint64_t max_object; /* highest object ID referenced in stream */
125eda14cbcSMatt Macy 	uint64_t bytes_read; /* bytes read when current record created */
126eda14cbcSMatt Macy 
127eda14cbcSMatt Macy 	list_t write_batch;
128eda14cbcSMatt Macy 
129eda14cbcSMatt Macy 	/* Encryption parameters for the last received DRR_OBJECT_RANGE */
130eda14cbcSMatt Macy 	boolean_t or_crypt_params_present;
131eda14cbcSMatt Macy 	uint64_t or_firstobj;
132eda14cbcSMatt Macy 	uint64_t or_numslots;
133eda14cbcSMatt Macy 	uint8_t or_salt[ZIO_DATA_SALT_LEN];
134eda14cbcSMatt Macy 	uint8_t or_iv[ZIO_DATA_IV_LEN];
135eda14cbcSMatt Macy 	uint8_t or_mac[ZIO_DATA_MAC_LEN];
136eda14cbcSMatt Macy 	boolean_t or_byteorder;
137271171e0SMartin Matuska 	zio_t *heal_pio;
13815f0b8c3SMartin Matuska 
13915f0b8c3SMartin Matuska 	/* Keep track of DRR_FREEOBJECTS right after DRR_OBJECT_RANGE */
14015f0b8c3SMartin Matuska 	or_need_sync_t or_need_sync;
141eda14cbcSMatt Macy };
142eda14cbcSMatt Macy 
143eda14cbcSMatt Macy typedef struct dmu_recv_begin_arg {
144eda14cbcSMatt Macy 	const char *drba_origin;
145eda14cbcSMatt Macy 	dmu_recv_cookie_t *drba_cookie;
146eda14cbcSMatt Macy 	cred_t *drba_cred;
147eda14cbcSMatt Macy 	proc_t *drba_proc;
148eda14cbcSMatt Macy 	dsl_crypto_params_t *drba_dcp;
149eda14cbcSMatt Macy } dmu_recv_begin_arg_t;
150eda14cbcSMatt Macy 
151eda14cbcSMatt Macy static void
152eda14cbcSMatt Macy byteswap_record(dmu_replay_record_t *drr)
153eda14cbcSMatt Macy {
154eda14cbcSMatt Macy #define	DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
155eda14cbcSMatt Macy #define	DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
156eda14cbcSMatt Macy 	drr->drr_type = BSWAP_32(drr->drr_type);
157eda14cbcSMatt Macy 	drr->drr_payloadlen = BSWAP_32(drr->drr_payloadlen);
158eda14cbcSMatt Macy 
159eda14cbcSMatt Macy 	switch (drr->drr_type) {
160eda14cbcSMatt Macy 	case DRR_BEGIN:
161eda14cbcSMatt Macy 		DO64(drr_begin.drr_magic);
162eda14cbcSMatt Macy 		DO64(drr_begin.drr_versioninfo);
163eda14cbcSMatt Macy 		DO64(drr_begin.drr_creation_time);
164eda14cbcSMatt Macy 		DO32(drr_begin.drr_type);
165eda14cbcSMatt Macy 		DO32(drr_begin.drr_flags);
166eda14cbcSMatt Macy 		DO64(drr_begin.drr_toguid);
167eda14cbcSMatt Macy 		DO64(drr_begin.drr_fromguid);
168eda14cbcSMatt Macy 		break;
169eda14cbcSMatt Macy 	case DRR_OBJECT:
170eda14cbcSMatt Macy 		DO64(drr_object.drr_object);
171eda14cbcSMatt Macy 		DO32(drr_object.drr_type);
172eda14cbcSMatt Macy 		DO32(drr_object.drr_bonustype);
173eda14cbcSMatt Macy 		DO32(drr_object.drr_blksz);
174eda14cbcSMatt Macy 		DO32(drr_object.drr_bonuslen);
175eda14cbcSMatt Macy 		DO32(drr_object.drr_raw_bonuslen);
176eda14cbcSMatt Macy 		DO64(drr_object.drr_toguid);
177eda14cbcSMatt Macy 		DO64(drr_object.drr_maxblkid);
178eda14cbcSMatt Macy 		break;
179eda14cbcSMatt Macy 	case DRR_FREEOBJECTS:
180eda14cbcSMatt Macy 		DO64(drr_freeobjects.drr_firstobj);
181eda14cbcSMatt Macy 		DO64(drr_freeobjects.drr_numobjs);
182eda14cbcSMatt Macy 		DO64(drr_freeobjects.drr_toguid);
183eda14cbcSMatt Macy 		break;
184eda14cbcSMatt Macy 	case DRR_WRITE:
185eda14cbcSMatt Macy 		DO64(drr_write.drr_object);
186eda14cbcSMatt Macy 		DO32(drr_write.drr_type);
187eda14cbcSMatt Macy 		DO64(drr_write.drr_offset);
188eda14cbcSMatt Macy 		DO64(drr_write.drr_logical_size);
189eda14cbcSMatt Macy 		DO64(drr_write.drr_toguid);
190eda14cbcSMatt Macy 		ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_write.drr_key.ddk_cksum);
191eda14cbcSMatt Macy 		DO64(drr_write.drr_key.ddk_prop);
192eda14cbcSMatt Macy 		DO64(drr_write.drr_compressed_size);
193eda14cbcSMatt Macy 		break;
194eda14cbcSMatt Macy 	case DRR_WRITE_EMBEDDED:
195eda14cbcSMatt Macy 		DO64(drr_write_embedded.drr_object);
196eda14cbcSMatt Macy 		DO64(drr_write_embedded.drr_offset);
197eda14cbcSMatt Macy 		DO64(drr_write_embedded.drr_length);
198eda14cbcSMatt Macy 		DO64(drr_write_embedded.drr_toguid);
199eda14cbcSMatt Macy 		DO32(drr_write_embedded.drr_lsize);
200eda14cbcSMatt Macy 		DO32(drr_write_embedded.drr_psize);
201eda14cbcSMatt Macy 		break;
202eda14cbcSMatt Macy 	case DRR_FREE:
203eda14cbcSMatt Macy 		DO64(drr_free.drr_object);
204eda14cbcSMatt Macy 		DO64(drr_free.drr_offset);
205eda14cbcSMatt Macy 		DO64(drr_free.drr_length);
206eda14cbcSMatt Macy 		DO64(drr_free.drr_toguid);
207eda14cbcSMatt Macy 		break;
208eda14cbcSMatt Macy 	case DRR_SPILL:
209eda14cbcSMatt Macy 		DO64(drr_spill.drr_object);
210eda14cbcSMatt Macy 		DO64(drr_spill.drr_length);
211eda14cbcSMatt Macy 		DO64(drr_spill.drr_toguid);
212eda14cbcSMatt Macy 		DO64(drr_spill.drr_compressed_size);
213eda14cbcSMatt Macy 		DO32(drr_spill.drr_type);
214eda14cbcSMatt Macy 		break;
215eda14cbcSMatt Macy 	case DRR_OBJECT_RANGE:
216eda14cbcSMatt Macy 		DO64(drr_object_range.drr_firstobj);
217eda14cbcSMatt Macy 		DO64(drr_object_range.drr_numslots);
218eda14cbcSMatt Macy 		DO64(drr_object_range.drr_toguid);
219eda14cbcSMatt Macy 		break;
220eda14cbcSMatt Macy 	case DRR_REDACT:
221eda14cbcSMatt Macy 		DO64(drr_redact.drr_object);
222eda14cbcSMatt Macy 		DO64(drr_redact.drr_offset);
223eda14cbcSMatt Macy 		DO64(drr_redact.drr_length);
224eda14cbcSMatt Macy 		DO64(drr_redact.drr_toguid);
225eda14cbcSMatt Macy 		break;
226eda14cbcSMatt Macy 	case DRR_END:
227eda14cbcSMatt Macy 		DO64(drr_end.drr_toguid);
228eda14cbcSMatt Macy 		ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_end.drr_checksum);
229eda14cbcSMatt Macy 		break;
230eda14cbcSMatt Macy 	default:
231eda14cbcSMatt Macy 		break;
232eda14cbcSMatt Macy 	}
233eda14cbcSMatt Macy 
234eda14cbcSMatt Macy 	if (drr->drr_type != DRR_BEGIN) {
235eda14cbcSMatt Macy 		ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_checksum.drr_checksum);
236eda14cbcSMatt Macy 	}
237eda14cbcSMatt Macy 
238eda14cbcSMatt Macy #undef DO64
239eda14cbcSMatt Macy #undef DO32
240eda14cbcSMatt Macy }
241eda14cbcSMatt Macy 
242eda14cbcSMatt Macy static boolean_t
243eda14cbcSMatt Macy redact_snaps_contains(uint64_t *snaps, uint64_t num_snaps, uint64_t guid)
244eda14cbcSMatt Macy {
245eda14cbcSMatt Macy 	for (int i = 0; i < num_snaps; i++) {
246eda14cbcSMatt Macy 		if (snaps[i] == guid)
247eda14cbcSMatt Macy 			return (B_TRUE);
248eda14cbcSMatt Macy 	}
249eda14cbcSMatt Macy 	return (B_FALSE);
250eda14cbcSMatt Macy }
251eda14cbcSMatt Macy 
252eda14cbcSMatt Macy /*
253eda14cbcSMatt Macy  * Check that the new stream we're trying to receive is redacted with respect to
254eda14cbcSMatt Macy  * a subset of the snapshots that the origin was redacted with respect to.  For
255eda14cbcSMatt Macy  * the reasons behind this, see the man page on redacted zfs sends and receives.
256eda14cbcSMatt Macy  */
257eda14cbcSMatt Macy static boolean_t
258eda14cbcSMatt Macy compatible_redact_snaps(uint64_t *origin_snaps, uint64_t origin_num_snaps,
259eda14cbcSMatt Macy     uint64_t *redact_snaps, uint64_t num_redact_snaps)
260eda14cbcSMatt Macy {
261eda14cbcSMatt Macy 	/*
262eda14cbcSMatt Macy 	 * Short circuit the comparison; if we are redacted with respect to
263eda14cbcSMatt Macy 	 * more snapshots than the origin, we can't be redacted with respect
264eda14cbcSMatt Macy 	 * to a subset.
265eda14cbcSMatt Macy 	 */
266eda14cbcSMatt Macy 	if (num_redact_snaps > origin_num_snaps) {
267eda14cbcSMatt Macy 		return (B_FALSE);
268eda14cbcSMatt Macy 	}
269eda14cbcSMatt Macy 
270eda14cbcSMatt Macy 	for (int i = 0; i < num_redact_snaps; i++) {
271eda14cbcSMatt Macy 		if (!redact_snaps_contains(origin_snaps, origin_num_snaps,
272eda14cbcSMatt Macy 		    redact_snaps[i])) {
273eda14cbcSMatt Macy 			return (B_FALSE);
274eda14cbcSMatt Macy 		}
275eda14cbcSMatt Macy 	}
276eda14cbcSMatt Macy 	return (B_TRUE);
277eda14cbcSMatt Macy }
278eda14cbcSMatt Macy 
279eda14cbcSMatt Macy static boolean_t
280eda14cbcSMatt Macy redact_check(dmu_recv_begin_arg_t *drba, dsl_dataset_t *origin)
281eda14cbcSMatt Macy {
282eda14cbcSMatt Macy 	uint64_t *origin_snaps;
283eda14cbcSMatt Macy 	uint64_t origin_num_snaps;
284eda14cbcSMatt Macy 	dmu_recv_cookie_t *drc = drba->drba_cookie;
285eda14cbcSMatt Macy 	struct drr_begin *drrb = drc->drc_drrb;
286eda14cbcSMatt Macy 	int featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
287eda14cbcSMatt Macy 	int err = 0;
288eda14cbcSMatt Macy 	boolean_t ret = B_TRUE;
289eda14cbcSMatt Macy 	uint64_t *redact_snaps;
290eda14cbcSMatt Macy 	uint_t numredactsnaps;
291eda14cbcSMatt Macy 
292eda14cbcSMatt Macy 	/*
293eda14cbcSMatt Macy 	 * If this is a full send stream, we're safe no matter what.
294eda14cbcSMatt Macy 	 */
295eda14cbcSMatt Macy 	if (drrb->drr_fromguid == 0)
296eda14cbcSMatt Macy 		return (ret);
297eda14cbcSMatt Macy 
298eda14cbcSMatt Macy 	VERIFY(dsl_dataset_get_uint64_array_feature(origin,
299eda14cbcSMatt Macy 	    SPA_FEATURE_REDACTED_DATASETS, &origin_num_snaps, &origin_snaps));
300eda14cbcSMatt Macy 
301eda14cbcSMatt Macy 	if (nvlist_lookup_uint64_array(drc->drc_begin_nvl,
302eda14cbcSMatt Macy 	    BEGINNV_REDACT_FROM_SNAPS, &redact_snaps, &numredactsnaps) ==
303eda14cbcSMatt Macy 	    0) {
304eda14cbcSMatt Macy 		/*
305eda14cbcSMatt Macy 		 * If the send stream was sent from the redaction bookmark or
306eda14cbcSMatt Macy 		 * the redacted version of the dataset, then we're safe.  Verify
307eda14cbcSMatt Macy 		 * that this is from the a compatible redaction bookmark or
308eda14cbcSMatt Macy 		 * redacted dataset.
309eda14cbcSMatt Macy 		 */
310eda14cbcSMatt Macy 		if (!compatible_redact_snaps(origin_snaps, origin_num_snaps,
311eda14cbcSMatt Macy 		    redact_snaps, numredactsnaps)) {
312eda14cbcSMatt Macy 			err = EINVAL;
313eda14cbcSMatt Macy 		}
314eda14cbcSMatt Macy 	} else if (featureflags & DMU_BACKUP_FEATURE_REDACTED) {
315eda14cbcSMatt Macy 		/*
316eda14cbcSMatt Macy 		 * If the stream is redacted, it must be redacted with respect
317eda14cbcSMatt Macy 		 * to a subset of what the origin is redacted with respect to.
318eda14cbcSMatt Macy 		 * See case number 2 in the zfs man page section on redacted zfs
319eda14cbcSMatt Macy 		 * send.
320eda14cbcSMatt Macy 		 */
321eda14cbcSMatt Macy 		err = nvlist_lookup_uint64_array(drc->drc_begin_nvl,
322eda14cbcSMatt Macy 		    BEGINNV_REDACT_SNAPS, &redact_snaps, &numredactsnaps);
323eda14cbcSMatt Macy 
324eda14cbcSMatt Macy 		if (err != 0 || !compatible_redact_snaps(origin_snaps,
325eda14cbcSMatt Macy 		    origin_num_snaps, redact_snaps, numredactsnaps)) {
326eda14cbcSMatt Macy 			err = EINVAL;
327eda14cbcSMatt Macy 		}
328eda14cbcSMatt Macy 	} else if (!redact_snaps_contains(origin_snaps, origin_num_snaps,
329eda14cbcSMatt Macy 	    drrb->drr_toguid)) {
330eda14cbcSMatt Macy 		/*
331eda14cbcSMatt Macy 		 * If the stream isn't redacted but the origin is, this must be
332eda14cbcSMatt Macy 		 * one of the snapshots the origin is redacted with respect to.
333eda14cbcSMatt Macy 		 * See case number 1 in the zfs man page section on redacted zfs
334eda14cbcSMatt Macy 		 * send.
335eda14cbcSMatt Macy 		 */
336eda14cbcSMatt Macy 		err = EINVAL;
337eda14cbcSMatt Macy 	}
338eda14cbcSMatt Macy 
339eda14cbcSMatt Macy 	if (err != 0)
340eda14cbcSMatt Macy 		ret = B_FALSE;
341eda14cbcSMatt Macy 	return (ret);
342eda14cbcSMatt Macy }
343eda14cbcSMatt Macy 
344eda14cbcSMatt Macy /*
345eda14cbcSMatt Macy  * If we previously received a stream with --large-block, we don't support
346eda14cbcSMatt Macy  * receiving an incremental on top of it without --large-block.  This avoids
347eda14cbcSMatt Macy  * forcing a read-modify-write or trying to re-aggregate a string of WRITE
348eda14cbcSMatt Macy  * records.
349eda14cbcSMatt Macy  */
350eda14cbcSMatt Macy static int
351eda14cbcSMatt Macy recv_check_large_blocks(dsl_dataset_t *ds, uint64_t featureflags)
352eda14cbcSMatt Macy {
353eda14cbcSMatt Macy 	if (dsl_dataset_feature_is_active(ds, SPA_FEATURE_LARGE_BLOCKS) &&
354eda14cbcSMatt Macy 	    !(featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS))
355eda14cbcSMatt Macy 		return (SET_ERROR(ZFS_ERR_STREAM_LARGE_BLOCK_MISMATCH));
356eda14cbcSMatt Macy 	return (0);
357eda14cbcSMatt Macy }
358eda14cbcSMatt Macy 
359eda14cbcSMatt Macy static int
360eda14cbcSMatt Macy recv_begin_check_existing_impl(dmu_recv_begin_arg_t *drba, dsl_dataset_t *ds,
361eda14cbcSMatt Macy     uint64_t fromguid, uint64_t featureflags)
362eda14cbcSMatt Macy {
363271171e0SMartin Matuska 	uint64_t obj;
364eda14cbcSMatt Macy 	uint64_t children;
365eda14cbcSMatt Macy 	int error;
366271171e0SMartin Matuska 	dsl_dataset_t *snap;
367eda14cbcSMatt Macy 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
368eda14cbcSMatt Macy 	boolean_t encrypted = ds->ds_dir->dd_crypto_obj != 0;
369eda14cbcSMatt Macy 	boolean_t raw = (featureflags & DMU_BACKUP_FEATURE_RAW) != 0;
370eda14cbcSMatt Macy 	boolean_t embed = (featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) != 0;
371eda14cbcSMatt Macy 
372eda14cbcSMatt Macy 	/* Temporary clone name must not exist. */
373eda14cbcSMatt Macy 	error = zap_lookup(dp->dp_meta_objset,
374eda14cbcSMatt Macy 	    dsl_dir_phys(ds->ds_dir)->dd_child_dir_zapobj, recv_clone_name,
375271171e0SMartin Matuska 	    8, 1, &obj);
376eda14cbcSMatt Macy 	if (error != ENOENT)
377eda14cbcSMatt Macy 		return (error == 0 ? SET_ERROR(EBUSY) : error);
378eda14cbcSMatt Macy 
379eda14cbcSMatt Macy 	/* Resume state must not be set. */
380eda14cbcSMatt Macy 	if (dsl_dataset_has_resume_receive_state(ds))
381eda14cbcSMatt Macy 		return (SET_ERROR(EBUSY));
382eda14cbcSMatt Macy 
383271171e0SMartin Matuska 	/* New snapshot name must not exist if we're not healing it. */
384eda14cbcSMatt Macy 	error = zap_lookup(dp->dp_meta_objset,
385eda14cbcSMatt Macy 	    dsl_dataset_phys(ds)->ds_snapnames_zapobj,
386271171e0SMartin Matuska 	    drba->drba_cookie->drc_tosnap, 8, 1, &obj);
387271171e0SMartin Matuska 	if (drba->drba_cookie->drc_heal) {
388271171e0SMartin Matuska 		if (error != 0)
389271171e0SMartin Matuska 			return (error);
390271171e0SMartin Matuska 	} else if (error != ENOENT) {
391eda14cbcSMatt Macy 		return (error == 0 ? SET_ERROR(EEXIST) : error);
392271171e0SMartin Matuska 	}
393eda14cbcSMatt Macy 
394eda14cbcSMatt Macy 	/* Must not have children if receiving a ZVOL. */
395eda14cbcSMatt Macy 	error = zap_count(dp->dp_meta_objset,
396eda14cbcSMatt Macy 	    dsl_dir_phys(ds->ds_dir)->dd_child_dir_zapobj, &children);
397eda14cbcSMatt Macy 	if (error != 0)
398eda14cbcSMatt Macy 		return (error);
399eda14cbcSMatt Macy 	if (drba->drba_cookie->drc_drrb->drr_type != DMU_OST_ZFS &&
400eda14cbcSMatt Macy 	    children > 0)
401eda14cbcSMatt Macy 		return (SET_ERROR(ZFS_ERR_WRONG_PARENT));
402eda14cbcSMatt Macy 
403eda14cbcSMatt Macy 	/*
404eda14cbcSMatt Macy 	 * Check snapshot limit before receiving. We'll recheck again at the
405eda14cbcSMatt Macy 	 * end, but might as well abort before receiving if we're already over
406eda14cbcSMatt Macy 	 * the limit.
407eda14cbcSMatt Macy 	 *
408eda14cbcSMatt Macy 	 * Note that we do not check the file system limit with
409eda14cbcSMatt Macy 	 * dsl_dir_fscount_check because the temporary %clones don't count
410eda14cbcSMatt Macy 	 * against that limit.
411eda14cbcSMatt Macy 	 */
412eda14cbcSMatt Macy 	error = dsl_fs_ss_limit_check(ds->ds_dir, 1, ZFS_PROP_SNAPSHOT_LIMIT,
413eda14cbcSMatt Macy 	    NULL, drba->drba_cred, drba->drba_proc);
414eda14cbcSMatt Macy 	if (error != 0)
415eda14cbcSMatt Macy 		return (error);
416eda14cbcSMatt Macy 
417271171e0SMartin Matuska 	if (drba->drba_cookie->drc_heal) {
418271171e0SMartin Matuska 		/* Encryption is incompatible with embedded data. */
419271171e0SMartin Matuska 		if (encrypted && embed)
420271171e0SMartin Matuska 			return (SET_ERROR(EINVAL));
421271171e0SMartin Matuska 
422271171e0SMartin Matuska 		/* Healing is not supported when in 'force' mode. */
423271171e0SMartin Matuska 		if (drba->drba_cookie->drc_force)
424271171e0SMartin Matuska 			return (SET_ERROR(EINVAL));
425271171e0SMartin Matuska 
426271171e0SMartin Matuska 		/* Must have keys loaded if doing encrypted non-raw recv. */
427271171e0SMartin Matuska 		if (encrypted && !raw) {
428271171e0SMartin Matuska 			if (spa_keystore_lookup_key(dp->dp_spa, ds->ds_object,
429271171e0SMartin Matuska 			    NULL, NULL) != 0)
430271171e0SMartin Matuska 				return (SET_ERROR(EACCES));
431271171e0SMartin Matuska 		}
432271171e0SMartin Matuska 
433271171e0SMartin Matuska 		error = dsl_dataset_hold_obj(dp, obj, FTAG, &snap);
434271171e0SMartin Matuska 		if (error != 0)
435271171e0SMartin Matuska 			return (error);
436271171e0SMartin Matuska 
437271171e0SMartin Matuska 		/*
438271171e0SMartin Matuska 		 * When not doing best effort corrective recv healing can only
439271171e0SMartin Matuska 		 * be done if the send stream is for the same snapshot as the
440271171e0SMartin Matuska 		 * one we are trying to heal.
441271171e0SMartin Matuska 		 */
442271171e0SMartin Matuska 		if (zfs_recv_best_effort_corrective == 0 &&
443271171e0SMartin Matuska 		    drba->drba_cookie->drc_drrb->drr_toguid !=
444271171e0SMartin Matuska 		    dsl_dataset_phys(snap)->ds_guid) {
445271171e0SMartin Matuska 			dsl_dataset_rele(snap, FTAG);
446271171e0SMartin Matuska 			return (SET_ERROR(ENOTSUP));
447271171e0SMartin Matuska 		}
448271171e0SMartin Matuska 		dsl_dataset_rele(snap, FTAG);
449271171e0SMartin Matuska 	} else if (fromguid != 0) {
450271171e0SMartin Matuska 		/* Sanity check the incremental recv */
451eda14cbcSMatt Macy 		uint64_t obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
452eda14cbcSMatt Macy 
453eda14cbcSMatt Macy 		/* Can't perform a raw receive on top of a non-raw receive */
454eda14cbcSMatt Macy 		if (!encrypted && raw)
455eda14cbcSMatt Macy 			return (SET_ERROR(EINVAL));
456eda14cbcSMatt Macy 
457eda14cbcSMatt Macy 		/* Encryption is incompatible with embedded data */
458eda14cbcSMatt Macy 		if (encrypted && embed)
459eda14cbcSMatt Macy 			return (SET_ERROR(EINVAL));
460eda14cbcSMatt Macy 
461eda14cbcSMatt Macy 		/* Find snapshot in this dir that matches fromguid. */
462eda14cbcSMatt Macy 		while (obj != 0) {
463eda14cbcSMatt Macy 			error = dsl_dataset_hold_obj(dp, obj, FTAG,
464eda14cbcSMatt Macy 			    &snap);
465eda14cbcSMatt Macy 			if (error != 0)
466eda14cbcSMatt Macy 				return (SET_ERROR(ENODEV));
467eda14cbcSMatt Macy 			if (snap->ds_dir != ds->ds_dir) {
468eda14cbcSMatt Macy 				dsl_dataset_rele(snap, FTAG);
469eda14cbcSMatt Macy 				return (SET_ERROR(ENODEV));
470eda14cbcSMatt Macy 			}
471eda14cbcSMatt Macy 			if (dsl_dataset_phys(snap)->ds_guid == fromguid)
472eda14cbcSMatt Macy 				break;
473eda14cbcSMatt Macy 			obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
474eda14cbcSMatt Macy 			dsl_dataset_rele(snap, FTAG);
475eda14cbcSMatt Macy 		}
476eda14cbcSMatt Macy 		if (obj == 0)
477eda14cbcSMatt Macy 			return (SET_ERROR(ENODEV));
478eda14cbcSMatt Macy 
479eda14cbcSMatt Macy 		if (drba->drba_cookie->drc_force) {
480eda14cbcSMatt Macy 			drba->drba_cookie->drc_fromsnapobj = obj;
481eda14cbcSMatt Macy 		} else {
482eda14cbcSMatt Macy 			/*
483eda14cbcSMatt Macy 			 * If we are not forcing, there must be no
484eda14cbcSMatt Macy 			 * changes since fromsnap. Raw sends have an
485eda14cbcSMatt Macy 			 * additional constraint that requires that
486eda14cbcSMatt Macy 			 * no "noop" snapshots exist between fromsnap
487eda14cbcSMatt Macy 			 * and tosnap for the IVset checking code to
488eda14cbcSMatt Macy 			 * work properly.
489eda14cbcSMatt Macy 			 */
490eda14cbcSMatt Macy 			if (dsl_dataset_modified_since_snap(ds, snap) ||
491eda14cbcSMatt Macy 			    (raw &&
492eda14cbcSMatt Macy 			    dsl_dataset_phys(ds)->ds_prev_snap_obj !=
493eda14cbcSMatt Macy 			    snap->ds_object)) {
494eda14cbcSMatt Macy 				dsl_dataset_rele(snap, FTAG);
495eda14cbcSMatt Macy 				return (SET_ERROR(ETXTBSY));
496eda14cbcSMatt Macy 			}
497eda14cbcSMatt Macy 			drba->drba_cookie->drc_fromsnapobj =
498eda14cbcSMatt Macy 			    ds->ds_prev->ds_object;
499eda14cbcSMatt Macy 		}
500eda14cbcSMatt Macy 
501eda14cbcSMatt Macy 		if (dsl_dataset_feature_is_active(snap,
502eda14cbcSMatt Macy 		    SPA_FEATURE_REDACTED_DATASETS) && !redact_check(drba,
503eda14cbcSMatt Macy 		    snap)) {
504eda14cbcSMatt Macy 			dsl_dataset_rele(snap, FTAG);
505eda14cbcSMatt Macy 			return (SET_ERROR(EINVAL));
506eda14cbcSMatt Macy 		}
507eda14cbcSMatt Macy 
508eda14cbcSMatt Macy 		error = recv_check_large_blocks(snap, featureflags);
509eda14cbcSMatt Macy 		if (error != 0) {
510eda14cbcSMatt Macy 			dsl_dataset_rele(snap, FTAG);
511eda14cbcSMatt Macy 			return (error);
512eda14cbcSMatt Macy 		}
513eda14cbcSMatt Macy 
514eda14cbcSMatt Macy 		dsl_dataset_rele(snap, FTAG);
515eda14cbcSMatt Macy 	} else {
516271171e0SMartin Matuska 		/* If full and not healing then must be forced. */
517eda14cbcSMatt Macy 		if (!drba->drba_cookie->drc_force)
518eda14cbcSMatt Macy 			return (SET_ERROR(EEXIST));
519eda14cbcSMatt Macy 
520eda14cbcSMatt Macy 		/*
521eda14cbcSMatt Macy 		 * We don't support using zfs recv -F to blow away
522eda14cbcSMatt Macy 		 * encrypted filesystems. This would require the
523eda14cbcSMatt Macy 		 * dsl dir to point to the old encryption key and
524eda14cbcSMatt Macy 		 * the new one at the same time during the receive.
525eda14cbcSMatt Macy 		 */
526eda14cbcSMatt Macy 		if ((!encrypted && raw) || encrypted)
527eda14cbcSMatt Macy 			return (SET_ERROR(EINVAL));
528eda14cbcSMatt Macy 
529eda14cbcSMatt Macy 		/*
530eda14cbcSMatt Macy 		 * Perform the same encryption checks we would if
531eda14cbcSMatt Macy 		 * we were creating a new dataset from scratch.
532eda14cbcSMatt Macy 		 */
533eda14cbcSMatt Macy 		if (!raw) {
534eda14cbcSMatt Macy 			boolean_t will_encrypt;
535eda14cbcSMatt Macy 
536eda14cbcSMatt Macy 			error = dmu_objset_create_crypt_check(
537eda14cbcSMatt Macy 			    ds->ds_dir->dd_parent, drba->drba_dcp,
538eda14cbcSMatt Macy 			    &will_encrypt);
539eda14cbcSMatt Macy 			if (error != 0)
540eda14cbcSMatt Macy 				return (error);
541eda14cbcSMatt Macy 
542eda14cbcSMatt Macy 			if (will_encrypt && embed)
543eda14cbcSMatt Macy 				return (SET_ERROR(EINVAL));
544eda14cbcSMatt Macy 		}
545eda14cbcSMatt Macy 	}
546eda14cbcSMatt Macy 
547eda14cbcSMatt Macy 	return (0);
548eda14cbcSMatt Macy }
549eda14cbcSMatt Macy 
550eda14cbcSMatt Macy /*
551eda14cbcSMatt Macy  * Check that any feature flags used in the data stream we're receiving are
552eda14cbcSMatt Macy  * supported by the pool we are receiving into.
553eda14cbcSMatt Macy  *
554eda14cbcSMatt Macy  * Note that some of the features we explicitly check here have additional
555eda14cbcSMatt Macy  * (implicit) features they depend on, but those dependencies are enforced
556eda14cbcSMatt Macy  * through the zfeature_register() calls declaring the features that we
557eda14cbcSMatt Macy  * explicitly check.
558eda14cbcSMatt Macy  */
559eda14cbcSMatt Macy static int
560eda14cbcSMatt Macy recv_begin_check_feature_flags_impl(uint64_t featureflags, spa_t *spa)
561eda14cbcSMatt Macy {
562eda14cbcSMatt Macy 	/*
563eda14cbcSMatt Macy 	 * Check if there are any unsupported feature flags.
564eda14cbcSMatt Macy 	 */
565eda14cbcSMatt Macy 	if (!DMU_STREAM_SUPPORTED(featureflags)) {
566eda14cbcSMatt Macy 		return (SET_ERROR(ZFS_ERR_UNKNOWN_SEND_STREAM_FEATURE));
567eda14cbcSMatt Macy 	}
568eda14cbcSMatt Macy 
569eda14cbcSMatt Macy 	/* Verify pool version supports SA if SA_SPILL feature set */
570eda14cbcSMatt Macy 	if ((featureflags & DMU_BACKUP_FEATURE_SA_SPILL) &&
571eda14cbcSMatt Macy 	    spa_version(spa) < SPA_VERSION_SA)
572eda14cbcSMatt Macy 		return (SET_ERROR(ENOTSUP));
573eda14cbcSMatt Macy 
574eda14cbcSMatt Macy 	/*
575eda14cbcSMatt Macy 	 * LZ4 compressed, ZSTD compressed, embedded, mooched, large blocks,
576eda14cbcSMatt Macy 	 * and large_dnodes in the stream can only be used if those pool
577eda14cbcSMatt Macy 	 * features are enabled because we don't attempt to decompress /
578eda14cbcSMatt Macy 	 * un-embed / un-mooch / split up the blocks / dnodes during the
579eda14cbcSMatt Macy 	 * receive process.
580eda14cbcSMatt Macy 	 */
581eda14cbcSMatt Macy 	if ((featureflags & DMU_BACKUP_FEATURE_LZ4) &&
582eda14cbcSMatt Macy 	    !spa_feature_is_enabled(spa, SPA_FEATURE_LZ4_COMPRESS))
583eda14cbcSMatt Macy 		return (SET_ERROR(ENOTSUP));
584eda14cbcSMatt Macy 	if ((featureflags & DMU_BACKUP_FEATURE_ZSTD) &&
585eda14cbcSMatt Macy 	    !spa_feature_is_enabled(spa, SPA_FEATURE_ZSTD_COMPRESS))
586eda14cbcSMatt Macy 		return (SET_ERROR(ENOTSUP));
587eda14cbcSMatt Macy 	if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) &&
588eda14cbcSMatt Macy 	    !spa_feature_is_enabled(spa, SPA_FEATURE_EMBEDDED_DATA))
589eda14cbcSMatt Macy 		return (SET_ERROR(ENOTSUP));
590eda14cbcSMatt Macy 	if ((featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
591eda14cbcSMatt Macy 	    !spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS))
592eda14cbcSMatt Macy 		return (SET_ERROR(ENOTSUP));
593eda14cbcSMatt Macy 	if ((featureflags & DMU_BACKUP_FEATURE_LARGE_DNODE) &&
594eda14cbcSMatt Macy 	    !spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_DNODE))
595eda14cbcSMatt Macy 		return (SET_ERROR(ENOTSUP));
596eda14cbcSMatt Macy 
597eda14cbcSMatt Macy 	/*
598eda14cbcSMatt Macy 	 * Receiving redacted streams requires that redacted datasets are
599eda14cbcSMatt Macy 	 * enabled.
600eda14cbcSMatt Macy 	 */
601eda14cbcSMatt Macy 	if ((featureflags & DMU_BACKUP_FEATURE_REDACTED) &&
602eda14cbcSMatt Macy 	    !spa_feature_is_enabled(spa, SPA_FEATURE_REDACTED_DATASETS))
603eda14cbcSMatt Macy 		return (SET_ERROR(ENOTSUP));
604eda14cbcSMatt Macy 
605eda14cbcSMatt Macy 	return (0);
606eda14cbcSMatt Macy }
607eda14cbcSMatt Macy 
608eda14cbcSMatt Macy static int
609eda14cbcSMatt Macy dmu_recv_begin_check(void *arg, dmu_tx_t *tx)
610eda14cbcSMatt Macy {
611eda14cbcSMatt Macy 	dmu_recv_begin_arg_t *drba = arg;
612eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
613eda14cbcSMatt Macy 	struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
614eda14cbcSMatt Macy 	uint64_t fromguid = drrb->drr_fromguid;
615eda14cbcSMatt Macy 	int flags = drrb->drr_flags;
6167877fdebSMatt Macy 	ds_hold_flags_t dsflags = DS_HOLD_FLAG_NONE;
617eda14cbcSMatt Macy 	int error;
618eda14cbcSMatt Macy 	uint64_t featureflags = drba->drba_cookie->drc_featureflags;
619eda14cbcSMatt Macy 	dsl_dataset_t *ds;
620eda14cbcSMatt Macy 	const char *tofs = drba->drba_cookie->drc_tofs;
621eda14cbcSMatt Macy 
622eda14cbcSMatt Macy 	/* already checked */
623eda14cbcSMatt Macy 	ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
624eda14cbcSMatt Macy 	ASSERT(!(featureflags & DMU_BACKUP_FEATURE_RESUMING));
625eda14cbcSMatt Macy 
626eda14cbcSMatt Macy 	if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
627eda14cbcSMatt Macy 	    DMU_COMPOUNDSTREAM ||
628eda14cbcSMatt Macy 	    drrb->drr_type >= DMU_OST_NUMTYPES ||
629eda14cbcSMatt Macy 	    ((flags & DRR_FLAG_CLONE) && drba->drba_origin == NULL))
630eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
631eda14cbcSMatt Macy 
632eda14cbcSMatt Macy 	error = recv_begin_check_feature_flags_impl(featureflags, dp->dp_spa);
633eda14cbcSMatt Macy 	if (error != 0)
634eda14cbcSMatt Macy 		return (error);
635eda14cbcSMatt Macy 
636eda14cbcSMatt Macy 	/* Resumable receives require extensible datasets */
637eda14cbcSMatt Macy 	if (drba->drba_cookie->drc_resumable &&
638eda14cbcSMatt Macy 	    !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EXTENSIBLE_DATASET))
639eda14cbcSMatt Macy 		return (SET_ERROR(ENOTSUP));
640eda14cbcSMatt Macy 
641eda14cbcSMatt Macy 	if (featureflags & DMU_BACKUP_FEATURE_RAW) {
642eda14cbcSMatt Macy 		/* raw receives require the encryption feature */
643eda14cbcSMatt Macy 		if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_ENCRYPTION))
644eda14cbcSMatt Macy 			return (SET_ERROR(ENOTSUP));
645eda14cbcSMatt Macy 
646eda14cbcSMatt Macy 		/* embedded data is incompatible with encryption and raw recv */
647eda14cbcSMatt Macy 		if (featureflags & DMU_BACKUP_FEATURE_EMBED_DATA)
648eda14cbcSMatt Macy 			return (SET_ERROR(EINVAL));
649eda14cbcSMatt Macy 
650eda14cbcSMatt Macy 		/* raw receives require spill block allocation flag */
651eda14cbcSMatt Macy 		if (!(flags & DRR_FLAG_SPILL_BLOCK))
652eda14cbcSMatt Macy 			return (SET_ERROR(ZFS_ERR_SPILL_BLOCK_FLAG_MISSING));
653eda14cbcSMatt Macy 	} else {
654c03c5b1cSMartin Matuska 		/*
655c03c5b1cSMartin Matuska 		 * We support unencrypted datasets below encrypted ones now,
656c03c5b1cSMartin Matuska 		 * so add the DS_HOLD_FLAG_DECRYPT flag only if we are dealing
657c03c5b1cSMartin Matuska 		 * with a dataset we may encrypt.
658c03c5b1cSMartin Matuska 		 */
659dbd5678dSMartin Matuska 		if (drba->drba_dcp == NULL ||
660c03c5b1cSMartin Matuska 		    drba->drba_dcp->cp_crypt != ZIO_CRYPT_OFF) {
661eda14cbcSMatt Macy 			dsflags |= DS_HOLD_FLAG_DECRYPT;
662eda14cbcSMatt Macy 		}
663c03c5b1cSMartin Matuska 	}
664eda14cbcSMatt Macy 
665eda14cbcSMatt Macy 	error = dsl_dataset_hold_flags(dp, tofs, dsflags, FTAG, &ds);
666eda14cbcSMatt Macy 	if (error == 0) {
667eda14cbcSMatt Macy 		/* target fs already exists; recv into temp clone */
668eda14cbcSMatt Macy 
669eda14cbcSMatt Macy 		/* Can't recv a clone into an existing fs */
670eda14cbcSMatt Macy 		if (flags & DRR_FLAG_CLONE || drba->drba_origin) {
671eda14cbcSMatt Macy 			dsl_dataset_rele_flags(ds, dsflags, FTAG);
672eda14cbcSMatt Macy 			return (SET_ERROR(EINVAL));
673eda14cbcSMatt Macy 		}
674eda14cbcSMatt Macy 
675eda14cbcSMatt Macy 		error = recv_begin_check_existing_impl(drba, ds, fromguid,
676eda14cbcSMatt Macy 		    featureflags);
677eda14cbcSMatt Macy 		dsl_dataset_rele_flags(ds, dsflags, FTAG);
678eda14cbcSMatt Macy 	} else if (error == ENOENT) {
679eda14cbcSMatt Macy 		/* target fs does not exist; must be a full backup or clone */
680eda14cbcSMatt Macy 		char buf[ZFS_MAX_DATASET_NAME_LEN];
681eda14cbcSMatt Macy 		objset_t *os;
682eda14cbcSMatt Macy 
683271171e0SMartin Matuska 		/* healing recv must be done "into" an existing snapshot */
684271171e0SMartin Matuska 		if (drba->drba_cookie->drc_heal == B_TRUE)
685271171e0SMartin Matuska 			return (SET_ERROR(ENOTSUP));
686271171e0SMartin Matuska 
687eda14cbcSMatt Macy 		/*
688eda14cbcSMatt Macy 		 * If it's a non-clone incremental, we are missing the
689eda14cbcSMatt Macy 		 * target fs, so fail the recv.
690eda14cbcSMatt Macy 		 */
691eda14cbcSMatt Macy 		if (fromguid != 0 && !((flags & DRR_FLAG_CLONE) ||
692eda14cbcSMatt Macy 		    drba->drba_origin))
693eda14cbcSMatt Macy 			return (SET_ERROR(ENOENT));
694eda14cbcSMatt Macy 
695eda14cbcSMatt Macy 		/*
696eda14cbcSMatt Macy 		 * If we're receiving a full send as a clone, and it doesn't
697eda14cbcSMatt Macy 		 * contain all the necessary free records and freeobject
698eda14cbcSMatt Macy 		 * records, reject it.
699eda14cbcSMatt Macy 		 */
700eda14cbcSMatt Macy 		if (fromguid == 0 && drba->drba_origin != NULL &&
701eda14cbcSMatt Macy 		    !(flags & DRR_FLAG_FREERECORDS))
702eda14cbcSMatt Macy 			return (SET_ERROR(EINVAL));
703eda14cbcSMatt Macy 
704eda14cbcSMatt Macy 		/* Open the parent of tofs */
705eda14cbcSMatt Macy 		ASSERT3U(strlen(tofs), <, sizeof (buf));
706eda14cbcSMatt Macy 		(void) strlcpy(buf, tofs, strrchr(tofs, '/') - tofs + 1);
707eda14cbcSMatt Macy 		error = dsl_dataset_hold(dp, buf, FTAG, &ds);
708eda14cbcSMatt Macy 		if (error != 0)
709eda14cbcSMatt Macy 			return (error);
710eda14cbcSMatt Macy 
711eda14cbcSMatt Macy 		if ((featureflags & DMU_BACKUP_FEATURE_RAW) == 0 &&
712eda14cbcSMatt Macy 		    drba->drba_origin == NULL) {
713eda14cbcSMatt Macy 			boolean_t will_encrypt;
714eda14cbcSMatt Macy 
715eda14cbcSMatt Macy 			/*
716eda14cbcSMatt Macy 			 * Check that we aren't breaking any encryption rules
717eda14cbcSMatt Macy 			 * and that we have all the parameters we need to
718eda14cbcSMatt Macy 			 * create an encrypted dataset if necessary. If we are
719eda14cbcSMatt Macy 			 * making an encrypted dataset the stream can't have
720eda14cbcSMatt Macy 			 * embedded data.
721eda14cbcSMatt Macy 			 */
722eda14cbcSMatt Macy 			error = dmu_objset_create_crypt_check(ds->ds_dir,
723eda14cbcSMatt Macy 			    drba->drba_dcp, &will_encrypt);
724eda14cbcSMatt Macy 			if (error != 0) {
725eda14cbcSMatt Macy 				dsl_dataset_rele(ds, FTAG);
726eda14cbcSMatt Macy 				return (error);
727eda14cbcSMatt Macy 			}
728eda14cbcSMatt Macy 
729eda14cbcSMatt Macy 			if (will_encrypt &&
730eda14cbcSMatt Macy 			    (featureflags & DMU_BACKUP_FEATURE_EMBED_DATA)) {
731eda14cbcSMatt Macy 				dsl_dataset_rele(ds, FTAG);
732eda14cbcSMatt Macy 				return (SET_ERROR(EINVAL));
733eda14cbcSMatt Macy 			}
734eda14cbcSMatt Macy 		}
735eda14cbcSMatt Macy 
736eda14cbcSMatt Macy 		/*
737eda14cbcSMatt Macy 		 * Check filesystem and snapshot limits before receiving. We'll
738eda14cbcSMatt Macy 		 * recheck snapshot limits again at the end (we create the
739eda14cbcSMatt Macy 		 * filesystems and increment those counts during begin_sync).
740eda14cbcSMatt Macy 		 */
741eda14cbcSMatt Macy 		error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
742eda14cbcSMatt Macy 		    ZFS_PROP_FILESYSTEM_LIMIT, NULL,
743eda14cbcSMatt Macy 		    drba->drba_cred, drba->drba_proc);
744eda14cbcSMatt Macy 		if (error != 0) {
745eda14cbcSMatt Macy 			dsl_dataset_rele(ds, FTAG);
746eda14cbcSMatt Macy 			return (error);
747eda14cbcSMatt Macy 		}
748eda14cbcSMatt Macy 
749eda14cbcSMatt Macy 		error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
750eda14cbcSMatt Macy 		    ZFS_PROP_SNAPSHOT_LIMIT, NULL,
751eda14cbcSMatt Macy 		    drba->drba_cred, drba->drba_proc);
752eda14cbcSMatt Macy 		if (error != 0) {
753eda14cbcSMatt Macy 			dsl_dataset_rele(ds, FTAG);
754eda14cbcSMatt Macy 			return (error);
755eda14cbcSMatt Macy 		}
756eda14cbcSMatt Macy 
757eda14cbcSMatt Macy 		/* can't recv below anything but filesystems (eg. no ZVOLs) */
758eda14cbcSMatt Macy 		error = dmu_objset_from_ds(ds, &os);
759eda14cbcSMatt Macy 		if (error != 0) {
760eda14cbcSMatt Macy 			dsl_dataset_rele(ds, FTAG);
761eda14cbcSMatt Macy 			return (error);
762eda14cbcSMatt Macy 		}
763eda14cbcSMatt Macy 		if (dmu_objset_type(os) != DMU_OST_ZFS) {
764eda14cbcSMatt Macy 			dsl_dataset_rele(ds, FTAG);
765eda14cbcSMatt Macy 			return (SET_ERROR(ZFS_ERR_WRONG_PARENT));
766eda14cbcSMatt Macy 		}
767eda14cbcSMatt Macy 
768eda14cbcSMatt Macy 		if (drba->drba_origin != NULL) {
769eda14cbcSMatt Macy 			dsl_dataset_t *origin;
770eda14cbcSMatt Macy 			error = dsl_dataset_hold_flags(dp, drba->drba_origin,
771eda14cbcSMatt Macy 			    dsflags, FTAG, &origin);
772eda14cbcSMatt Macy 			if (error != 0) {
773eda14cbcSMatt Macy 				dsl_dataset_rele(ds, FTAG);
774eda14cbcSMatt Macy 				return (error);
775eda14cbcSMatt Macy 			}
776eda14cbcSMatt Macy 			if (!origin->ds_is_snapshot) {
777eda14cbcSMatt Macy 				dsl_dataset_rele_flags(origin, dsflags, FTAG);
778eda14cbcSMatt Macy 				dsl_dataset_rele(ds, FTAG);
779eda14cbcSMatt Macy 				return (SET_ERROR(EINVAL));
780eda14cbcSMatt Macy 			}
781eda14cbcSMatt Macy 			if (dsl_dataset_phys(origin)->ds_guid != fromguid &&
782eda14cbcSMatt Macy 			    fromguid != 0) {
783eda14cbcSMatt Macy 				dsl_dataset_rele_flags(origin, dsflags, FTAG);
784eda14cbcSMatt Macy 				dsl_dataset_rele(ds, FTAG);
785eda14cbcSMatt Macy 				return (SET_ERROR(ENODEV));
786eda14cbcSMatt Macy 			}
787eda14cbcSMatt Macy 
788eda14cbcSMatt Macy 			if (origin->ds_dir->dd_crypto_obj != 0 &&
789eda14cbcSMatt Macy 			    (featureflags & DMU_BACKUP_FEATURE_EMBED_DATA)) {
790eda14cbcSMatt Macy 				dsl_dataset_rele_flags(origin, dsflags, FTAG);
791eda14cbcSMatt Macy 				dsl_dataset_rele(ds, FTAG);
792eda14cbcSMatt Macy 				return (SET_ERROR(EINVAL));
793eda14cbcSMatt Macy 			}
794eda14cbcSMatt Macy 
795eda14cbcSMatt Macy 			/*
796eda14cbcSMatt Macy 			 * If the origin is redacted we need to verify that this
797eda14cbcSMatt Macy 			 * send stream can safely be received on top of the
798eda14cbcSMatt Macy 			 * origin.
799eda14cbcSMatt Macy 			 */
800eda14cbcSMatt Macy 			if (dsl_dataset_feature_is_active(origin,
801eda14cbcSMatt Macy 			    SPA_FEATURE_REDACTED_DATASETS)) {
802eda14cbcSMatt Macy 				if (!redact_check(drba, origin)) {
803eda14cbcSMatt Macy 					dsl_dataset_rele_flags(origin, dsflags,
804eda14cbcSMatt Macy 					    FTAG);
805eda14cbcSMatt Macy 					dsl_dataset_rele_flags(ds, dsflags,
806eda14cbcSMatt Macy 					    FTAG);
807eda14cbcSMatt Macy 					return (SET_ERROR(EINVAL));
808eda14cbcSMatt Macy 				}
809eda14cbcSMatt Macy 			}
810eda14cbcSMatt Macy 
811eda14cbcSMatt Macy 			error = recv_check_large_blocks(ds, featureflags);
812eda14cbcSMatt Macy 			if (error != 0) {
813eda14cbcSMatt Macy 				dsl_dataset_rele_flags(origin, dsflags, FTAG);
814eda14cbcSMatt Macy 				dsl_dataset_rele_flags(ds, dsflags, FTAG);
815eda14cbcSMatt Macy 				return (error);
816eda14cbcSMatt Macy 			}
817eda14cbcSMatt Macy 
818eda14cbcSMatt Macy 			dsl_dataset_rele_flags(origin, dsflags, FTAG);
819eda14cbcSMatt Macy 		}
820eda14cbcSMatt Macy 
821eda14cbcSMatt Macy 		dsl_dataset_rele(ds, FTAG);
822eda14cbcSMatt Macy 		error = 0;
823eda14cbcSMatt Macy 	}
824eda14cbcSMatt Macy 	return (error);
825eda14cbcSMatt Macy }
826eda14cbcSMatt Macy 
827eda14cbcSMatt Macy static void
828eda14cbcSMatt Macy dmu_recv_begin_sync(void *arg, dmu_tx_t *tx)
829eda14cbcSMatt Macy {
830eda14cbcSMatt Macy 	dmu_recv_begin_arg_t *drba = arg;
831eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
832eda14cbcSMatt Macy 	objset_t *mos = dp->dp_meta_objset;
833eda14cbcSMatt Macy 	dmu_recv_cookie_t *drc = drba->drba_cookie;
834eda14cbcSMatt Macy 	struct drr_begin *drrb = drc->drc_drrb;
835eda14cbcSMatt Macy 	const char *tofs = drc->drc_tofs;
836eda14cbcSMatt Macy 	uint64_t featureflags = drc->drc_featureflags;
837eda14cbcSMatt Macy 	dsl_dataset_t *ds, *newds;
838eda14cbcSMatt Macy 	objset_t *os;
839eda14cbcSMatt Macy 	uint64_t dsobj;
8407877fdebSMatt Macy 	ds_hold_flags_t dsflags = DS_HOLD_FLAG_NONE;
841eda14cbcSMatt Macy 	int error;
842eda14cbcSMatt Macy 	uint64_t crflags = 0;
843eda14cbcSMatt Macy 	dsl_crypto_params_t dummy_dcp = { 0 };
844eda14cbcSMatt Macy 	dsl_crypto_params_t *dcp = drba->drba_dcp;
845eda14cbcSMatt Macy 
846eda14cbcSMatt Macy 	if (drrb->drr_flags & DRR_FLAG_CI_DATA)
847eda14cbcSMatt Macy 		crflags |= DS_FLAG_CI_DATASET;
848eda14cbcSMatt Macy 
849eda14cbcSMatt Macy 	if ((featureflags & DMU_BACKUP_FEATURE_RAW) == 0)
850eda14cbcSMatt Macy 		dsflags |= DS_HOLD_FLAG_DECRYPT;
851eda14cbcSMatt Macy 
852eda14cbcSMatt Macy 	/*
853eda14cbcSMatt Macy 	 * Raw, non-incremental recvs always use a dummy dcp with
854eda14cbcSMatt Macy 	 * the raw cmd set. Raw incremental recvs do not use a dcp
855eda14cbcSMatt Macy 	 * since the encryption parameters are already set in stone.
856eda14cbcSMatt Macy 	 */
857eda14cbcSMatt Macy 	if (dcp == NULL && drrb->drr_fromguid == 0 &&
858eda14cbcSMatt Macy 	    drba->drba_origin == NULL) {
859eda14cbcSMatt Macy 		ASSERT3P(dcp, ==, NULL);
860eda14cbcSMatt Macy 		dcp = &dummy_dcp;
861eda14cbcSMatt Macy 
862eda14cbcSMatt Macy 		if (featureflags & DMU_BACKUP_FEATURE_RAW)
863eda14cbcSMatt Macy 			dcp->cp_cmd = DCP_CMD_RAW_RECV;
864eda14cbcSMatt Macy 	}
865eda14cbcSMatt Macy 
866eda14cbcSMatt Macy 	error = dsl_dataset_hold_flags(dp, tofs, dsflags, FTAG, &ds);
867eda14cbcSMatt Macy 	if (error == 0) {
868271171e0SMartin Matuska 		/* Create temporary clone unless we're doing corrective recv */
869eda14cbcSMatt Macy 		dsl_dataset_t *snap = NULL;
870eda14cbcSMatt Macy 
871eda14cbcSMatt Macy 		if (drba->drba_cookie->drc_fromsnapobj != 0) {
872eda14cbcSMatt Macy 			VERIFY0(dsl_dataset_hold_obj(dp,
873eda14cbcSMatt Macy 			    drba->drba_cookie->drc_fromsnapobj, FTAG, &snap));
874eda14cbcSMatt Macy 			ASSERT3P(dcp, ==, NULL);
875eda14cbcSMatt Macy 		}
876271171e0SMartin Matuska 		if (drc->drc_heal) {
877271171e0SMartin Matuska 			/* When healing we want to use the provided snapshot */
878271171e0SMartin Matuska 			VERIFY0(dsl_dataset_snap_lookup(ds, drc->drc_tosnap,
879271171e0SMartin Matuska 			    &dsobj));
880271171e0SMartin Matuska 		} else {
881271171e0SMartin Matuska 			dsobj = dsl_dataset_create_sync(ds->ds_dir,
882271171e0SMartin Matuska 			    recv_clone_name, snap, crflags, drba->drba_cred,
883271171e0SMartin Matuska 			    dcp, tx);
884271171e0SMartin Matuska 		}
885eda14cbcSMatt Macy 		if (drba->drba_cookie->drc_fromsnapobj != 0)
886eda14cbcSMatt Macy 			dsl_dataset_rele(snap, FTAG);
887eda14cbcSMatt Macy 		dsl_dataset_rele_flags(ds, dsflags, FTAG);
888eda14cbcSMatt Macy 	} else {
889eda14cbcSMatt Macy 		dsl_dir_t *dd;
890eda14cbcSMatt Macy 		const char *tail;
891eda14cbcSMatt Macy 		dsl_dataset_t *origin = NULL;
892eda14cbcSMatt Macy 
893eda14cbcSMatt Macy 		VERIFY0(dsl_dir_hold(dp, tofs, FTAG, &dd, &tail));
894eda14cbcSMatt Macy 
895eda14cbcSMatt Macy 		if (drba->drba_origin != NULL) {
896eda14cbcSMatt Macy 			VERIFY0(dsl_dataset_hold(dp, drba->drba_origin,
897eda14cbcSMatt Macy 			    FTAG, &origin));
898eda14cbcSMatt Macy 			ASSERT3P(dcp, ==, NULL);
899eda14cbcSMatt Macy 		}
900eda14cbcSMatt Macy 
901eda14cbcSMatt Macy 		/* Create new dataset. */
902eda14cbcSMatt Macy 		dsobj = dsl_dataset_create_sync(dd, strrchr(tofs, '/') + 1,
903eda14cbcSMatt Macy 		    origin, crflags, drba->drba_cred, dcp, tx);
904eda14cbcSMatt Macy 		if (origin != NULL)
905eda14cbcSMatt Macy 			dsl_dataset_rele(origin, FTAG);
906eda14cbcSMatt Macy 		dsl_dir_rele(dd, FTAG);
907eda14cbcSMatt Macy 		drc->drc_newfs = B_TRUE;
908eda14cbcSMatt Macy 	}
909eda14cbcSMatt Macy 	VERIFY0(dsl_dataset_own_obj_force(dp, dsobj, dsflags, dmu_recv_tag,
910eda14cbcSMatt Macy 	    &newds));
911eda14cbcSMatt Macy 	if (dsl_dataset_feature_is_active(newds,
912eda14cbcSMatt Macy 	    SPA_FEATURE_REDACTED_DATASETS)) {
913eda14cbcSMatt Macy 		/*
914eda14cbcSMatt Macy 		 * If the origin dataset is redacted, the child will be redacted
915eda14cbcSMatt Macy 		 * when we create it.  We clear the new dataset's
916eda14cbcSMatt Macy 		 * redaction info; if it should be redacted, we'll fill
917eda14cbcSMatt Macy 		 * in its information later.
918eda14cbcSMatt Macy 		 */
919eda14cbcSMatt Macy 		dsl_dataset_deactivate_feature(newds,
920eda14cbcSMatt Macy 		    SPA_FEATURE_REDACTED_DATASETS, tx);
921eda14cbcSMatt Macy 	}
922eda14cbcSMatt Macy 	VERIFY0(dmu_objset_from_ds(newds, &os));
923eda14cbcSMatt Macy 
924eda14cbcSMatt Macy 	if (drc->drc_resumable) {
925eda14cbcSMatt Macy 		dsl_dataset_zapify(newds, tx);
926eda14cbcSMatt Macy 		if (drrb->drr_fromguid != 0) {
927eda14cbcSMatt Macy 			VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_FROMGUID,
928eda14cbcSMatt Macy 			    8, 1, &drrb->drr_fromguid, tx));
929eda14cbcSMatt Macy 		}
930eda14cbcSMatt Macy 		VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_TOGUID,
931eda14cbcSMatt Macy 		    8, 1, &drrb->drr_toguid, tx));
932eda14cbcSMatt Macy 		VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_TONAME,
933eda14cbcSMatt Macy 		    1, strlen(drrb->drr_toname) + 1, drrb->drr_toname, tx));
934eda14cbcSMatt Macy 		uint64_t one = 1;
935eda14cbcSMatt Macy 		uint64_t zero = 0;
936eda14cbcSMatt Macy 		VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_OBJECT,
937eda14cbcSMatt Macy 		    8, 1, &one, tx));
938eda14cbcSMatt Macy 		VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_OFFSET,
939eda14cbcSMatt Macy 		    8, 1, &zero, tx));
940eda14cbcSMatt Macy 		VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_BYTES,
941eda14cbcSMatt Macy 		    8, 1, &zero, tx));
942eda14cbcSMatt Macy 		if (featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) {
943eda14cbcSMatt Macy 			VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_LARGEBLOCK,
944eda14cbcSMatt Macy 			    8, 1, &one, tx));
945eda14cbcSMatt Macy 		}
946eda14cbcSMatt Macy 		if (featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) {
947eda14cbcSMatt Macy 			VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_EMBEDOK,
948eda14cbcSMatt Macy 			    8, 1, &one, tx));
949eda14cbcSMatt Macy 		}
950eda14cbcSMatt Macy 		if (featureflags & DMU_BACKUP_FEATURE_COMPRESSED) {
951eda14cbcSMatt Macy 			VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_COMPRESSOK,
952eda14cbcSMatt Macy 			    8, 1, &one, tx));
953eda14cbcSMatt Macy 		}
954eda14cbcSMatt Macy 		if (featureflags & DMU_BACKUP_FEATURE_RAW) {
955eda14cbcSMatt Macy 			VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_RAWOK,
956eda14cbcSMatt Macy 			    8, 1, &one, tx));
957eda14cbcSMatt Macy 		}
958eda14cbcSMatt Macy 
959eda14cbcSMatt Macy 		uint64_t *redact_snaps;
960eda14cbcSMatt Macy 		uint_t numredactsnaps;
961eda14cbcSMatt Macy 		if (nvlist_lookup_uint64_array(drc->drc_begin_nvl,
962eda14cbcSMatt Macy 		    BEGINNV_REDACT_FROM_SNAPS, &redact_snaps,
963eda14cbcSMatt Macy 		    &numredactsnaps) == 0) {
964eda14cbcSMatt Macy 			VERIFY0(zap_add(mos, dsobj,
965eda14cbcSMatt Macy 			    DS_FIELD_RESUME_REDACT_BOOKMARK_SNAPS,
966eda14cbcSMatt Macy 			    sizeof (*redact_snaps), numredactsnaps,
967eda14cbcSMatt Macy 			    redact_snaps, tx));
968eda14cbcSMatt Macy 		}
969eda14cbcSMatt Macy 	}
970eda14cbcSMatt Macy 
971eda14cbcSMatt Macy 	/*
972eda14cbcSMatt Macy 	 * Usually the os->os_encrypted value is tied to the presence of a
973eda14cbcSMatt Macy 	 * DSL Crypto Key object in the dd. However, that will not be received
974eda14cbcSMatt Macy 	 * until dmu_recv_stream(), so we set the value manually for now.
975eda14cbcSMatt Macy 	 */
976eda14cbcSMatt Macy 	if (featureflags & DMU_BACKUP_FEATURE_RAW) {
977eda14cbcSMatt Macy 		os->os_encrypted = B_TRUE;
978eda14cbcSMatt Macy 		drba->drba_cookie->drc_raw = B_TRUE;
979eda14cbcSMatt Macy 	}
980eda14cbcSMatt Macy 
981eda14cbcSMatt Macy 	if (featureflags & DMU_BACKUP_FEATURE_REDACTED) {
982eda14cbcSMatt Macy 		uint64_t *redact_snaps;
983eda14cbcSMatt Macy 		uint_t numredactsnaps;
984eda14cbcSMatt Macy 		VERIFY0(nvlist_lookup_uint64_array(drc->drc_begin_nvl,
985eda14cbcSMatt Macy 		    BEGINNV_REDACT_SNAPS, &redact_snaps, &numredactsnaps));
986eda14cbcSMatt Macy 		dsl_dataset_activate_redaction(newds, redact_snaps,
987eda14cbcSMatt Macy 		    numredactsnaps, tx);
988eda14cbcSMatt Macy 	}
989eda14cbcSMatt Macy 
990eda14cbcSMatt Macy 	dmu_buf_will_dirty(newds->ds_dbuf, tx);
991eda14cbcSMatt Macy 	dsl_dataset_phys(newds)->ds_flags |= DS_FLAG_INCONSISTENT;
992eda14cbcSMatt Macy 
993eda14cbcSMatt Macy 	/*
994eda14cbcSMatt Macy 	 * If we actually created a non-clone, we need to create the objset
995eda14cbcSMatt Macy 	 * in our new dataset. If this is a raw send we postpone this until
996eda14cbcSMatt Macy 	 * dmu_recv_stream() so that we can allocate the metadnode with the
997eda14cbcSMatt Macy 	 * properties from the DRR_BEGIN payload.
998eda14cbcSMatt Macy 	 */
999eda14cbcSMatt Macy 	rrw_enter(&newds->ds_bp_rwlock, RW_READER, FTAG);
1000eda14cbcSMatt Macy 	if (BP_IS_HOLE(dsl_dataset_get_blkptr(newds)) &&
1001271171e0SMartin Matuska 	    (featureflags & DMU_BACKUP_FEATURE_RAW) == 0 &&
1002271171e0SMartin Matuska 	    !drc->drc_heal) {
1003eda14cbcSMatt Macy 		(void) dmu_objset_create_impl(dp->dp_spa,
1004eda14cbcSMatt Macy 		    newds, dsl_dataset_get_blkptr(newds), drrb->drr_type, tx);
1005eda14cbcSMatt Macy 	}
1006eda14cbcSMatt Macy 	rrw_exit(&newds->ds_bp_rwlock, FTAG);
1007eda14cbcSMatt Macy 
1008eda14cbcSMatt Macy 	drba->drba_cookie->drc_ds = newds;
1009eda14cbcSMatt Macy 	drba->drba_cookie->drc_os = os;
1010eda14cbcSMatt Macy 
1011eda14cbcSMatt Macy 	spa_history_log_internal_ds(newds, "receive", tx, " ");
1012eda14cbcSMatt Macy }
1013eda14cbcSMatt Macy 
1014eda14cbcSMatt Macy static int
1015eda14cbcSMatt Macy dmu_recv_resume_begin_check(void *arg, dmu_tx_t *tx)
1016eda14cbcSMatt Macy {
1017eda14cbcSMatt Macy 	dmu_recv_begin_arg_t *drba = arg;
1018eda14cbcSMatt Macy 	dmu_recv_cookie_t *drc = drba->drba_cookie;
1019eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
1020eda14cbcSMatt Macy 	struct drr_begin *drrb = drc->drc_drrb;
1021eda14cbcSMatt Macy 	int error;
10227877fdebSMatt Macy 	ds_hold_flags_t dsflags = DS_HOLD_FLAG_NONE;
1023eda14cbcSMatt Macy 	dsl_dataset_t *ds;
1024eda14cbcSMatt Macy 	const char *tofs = drc->drc_tofs;
1025eda14cbcSMatt Macy 
1026eda14cbcSMatt Macy 	/* already checked */
1027eda14cbcSMatt Macy 	ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
1028eda14cbcSMatt Macy 	ASSERT(drc->drc_featureflags & DMU_BACKUP_FEATURE_RESUMING);
1029eda14cbcSMatt Macy 
1030eda14cbcSMatt Macy 	if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
1031eda14cbcSMatt Macy 	    DMU_COMPOUNDSTREAM ||
1032eda14cbcSMatt Macy 	    drrb->drr_type >= DMU_OST_NUMTYPES)
1033eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
1034eda14cbcSMatt Macy 
1035eda14cbcSMatt Macy 	/*
1036eda14cbcSMatt Macy 	 * This is mostly a sanity check since we should have already done these
1037eda14cbcSMatt Macy 	 * checks during a previous attempt to receive the data.
1038eda14cbcSMatt Macy 	 */
1039eda14cbcSMatt Macy 	error = recv_begin_check_feature_flags_impl(drc->drc_featureflags,
1040eda14cbcSMatt Macy 	    dp->dp_spa);
1041eda14cbcSMatt Macy 	if (error != 0)
1042eda14cbcSMatt Macy 		return (error);
1043eda14cbcSMatt Macy 
1044eda14cbcSMatt Macy 	/* 6 extra bytes for /%recv */
1045eda14cbcSMatt Macy 	char recvname[ZFS_MAX_DATASET_NAME_LEN + 6];
1046eda14cbcSMatt Macy 
1047eda14cbcSMatt Macy 	(void) snprintf(recvname, sizeof (recvname), "%s/%s",
1048eda14cbcSMatt Macy 	    tofs, recv_clone_name);
1049eda14cbcSMatt Macy 
1050eda14cbcSMatt Macy 	if (drc->drc_featureflags & DMU_BACKUP_FEATURE_RAW) {
1051eda14cbcSMatt Macy 		/* raw receives require spill block allocation flag */
1052eda14cbcSMatt Macy 		if (!(drrb->drr_flags & DRR_FLAG_SPILL_BLOCK))
1053eda14cbcSMatt Macy 			return (SET_ERROR(ZFS_ERR_SPILL_BLOCK_FLAG_MISSING));
1054eda14cbcSMatt Macy 	} else {
1055eda14cbcSMatt Macy 		dsflags |= DS_HOLD_FLAG_DECRYPT;
1056eda14cbcSMatt Macy 	}
1057eda14cbcSMatt Macy 
1058be181ee2SMartin Matuska 	boolean_t recvexist = B_TRUE;
1059eda14cbcSMatt Macy 	if (dsl_dataset_hold_flags(dp, recvname, dsflags, FTAG, &ds) != 0) {
1060eda14cbcSMatt Macy 		/* %recv does not exist; continue in tofs */
1061be181ee2SMartin Matuska 		recvexist = B_FALSE;
1062eda14cbcSMatt Macy 		error = dsl_dataset_hold_flags(dp, tofs, dsflags, FTAG, &ds);
1063eda14cbcSMatt Macy 		if (error != 0)
1064eda14cbcSMatt Macy 			return (error);
1065eda14cbcSMatt Macy 	}
1066eda14cbcSMatt Macy 
1067be181ee2SMartin Matuska 	/*
1068be181ee2SMartin Matuska 	 * Resume of full/newfs recv on existing dataset should be done with
1069be181ee2SMartin Matuska 	 * force flag
1070be181ee2SMartin Matuska 	 */
1071be181ee2SMartin Matuska 	if (recvexist && drrb->drr_fromguid == 0 && !drc->drc_force) {
1072be181ee2SMartin Matuska 		dsl_dataset_rele_flags(ds, dsflags, FTAG);
1073be181ee2SMartin Matuska 		return (SET_ERROR(ZFS_ERR_RESUME_EXISTS));
1074be181ee2SMartin Matuska 	}
1075be181ee2SMartin Matuska 
1076eda14cbcSMatt Macy 	/* check that ds is marked inconsistent */
1077eda14cbcSMatt Macy 	if (!DS_IS_INCONSISTENT(ds)) {
1078eda14cbcSMatt Macy 		dsl_dataset_rele_flags(ds, dsflags, FTAG);
1079eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
1080eda14cbcSMatt Macy 	}
1081eda14cbcSMatt Macy 
1082eda14cbcSMatt Macy 	/* check that there is resuming data, and that the toguid matches */
1083eda14cbcSMatt Macy 	if (!dsl_dataset_is_zapified(ds)) {
1084eda14cbcSMatt Macy 		dsl_dataset_rele_flags(ds, dsflags, FTAG);
1085eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
1086eda14cbcSMatt Macy 	}
1087eda14cbcSMatt Macy 	uint64_t val;
1088eda14cbcSMatt Macy 	error = zap_lookup(dp->dp_meta_objset, ds->ds_object,
1089eda14cbcSMatt Macy 	    DS_FIELD_RESUME_TOGUID, sizeof (val), 1, &val);
1090eda14cbcSMatt Macy 	if (error != 0 || drrb->drr_toguid != val) {
1091eda14cbcSMatt Macy 		dsl_dataset_rele_flags(ds, dsflags, FTAG);
1092eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
1093eda14cbcSMatt Macy 	}
1094eda14cbcSMatt Macy 
1095eda14cbcSMatt Macy 	/*
1096eda14cbcSMatt Macy 	 * Check if the receive is still running.  If so, it will be owned.
1097eda14cbcSMatt Macy 	 * Note that nothing else can own the dataset (e.g. after the receive
1098eda14cbcSMatt Macy 	 * fails) because it will be marked inconsistent.
1099eda14cbcSMatt Macy 	 */
1100eda14cbcSMatt Macy 	if (dsl_dataset_has_owner(ds)) {
1101eda14cbcSMatt Macy 		dsl_dataset_rele_flags(ds, dsflags, FTAG);
1102eda14cbcSMatt Macy 		return (SET_ERROR(EBUSY));
1103eda14cbcSMatt Macy 	}
1104eda14cbcSMatt Macy 
1105eda14cbcSMatt Macy 	/* There should not be any snapshots of this fs yet. */
1106eda14cbcSMatt Macy 	if (ds->ds_prev != NULL && ds->ds_prev->ds_dir == ds->ds_dir) {
1107eda14cbcSMatt Macy 		dsl_dataset_rele_flags(ds, dsflags, FTAG);
1108eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
1109eda14cbcSMatt Macy 	}
1110eda14cbcSMatt Macy 
1111eda14cbcSMatt Macy 	/*
1112eda14cbcSMatt Macy 	 * Note: resume point will be checked when we process the first WRITE
1113eda14cbcSMatt Macy 	 * record.
1114eda14cbcSMatt Macy 	 */
1115eda14cbcSMatt Macy 
1116eda14cbcSMatt Macy 	/* check that the origin matches */
1117eda14cbcSMatt Macy 	val = 0;
1118eda14cbcSMatt Macy 	(void) zap_lookup(dp->dp_meta_objset, ds->ds_object,
1119eda14cbcSMatt Macy 	    DS_FIELD_RESUME_FROMGUID, sizeof (val), 1, &val);
1120eda14cbcSMatt Macy 	if (drrb->drr_fromguid != val) {
1121eda14cbcSMatt Macy 		dsl_dataset_rele_flags(ds, dsflags, FTAG);
1122eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
1123eda14cbcSMatt Macy 	}
1124eda14cbcSMatt Macy 
1125eda14cbcSMatt Macy 	if (ds->ds_prev != NULL && drrb->drr_fromguid != 0)
1126eda14cbcSMatt Macy 		drc->drc_fromsnapobj = ds->ds_prev->ds_object;
1127eda14cbcSMatt Macy 
1128eda14cbcSMatt Macy 	/*
1129eda14cbcSMatt Macy 	 * If we're resuming, and the send is redacted, then the original send
1130eda14cbcSMatt Macy 	 * must have been redacted, and must have been redacted with respect to
1131eda14cbcSMatt Macy 	 * the same snapshots.
1132eda14cbcSMatt Macy 	 */
1133eda14cbcSMatt Macy 	if (drc->drc_featureflags & DMU_BACKUP_FEATURE_REDACTED) {
1134eda14cbcSMatt Macy 		uint64_t num_ds_redact_snaps;
1135eda14cbcSMatt Macy 		uint64_t *ds_redact_snaps;
1136eda14cbcSMatt Macy 
1137eda14cbcSMatt Macy 		uint_t num_stream_redact_snaps;
1138eda14cbcSMatt Macy 		uint64_t *stream_redact_snaps;
1139eda14cbcSMatt Macy 
1140eda14cbcSMatt Macy 		if (nvlist_lookup_uint64_array(drc->drc_begin_nvl,
1141eda14cbcSMatt Macy 		    BEGINNV_REDACT_SNAPS, &stream_redact_snaps,
1142eda14cbcSMatt Macy 		    &num_stream_redact_snaps) != 0) {
1143eda14cbcSMatt Macy 			dsl_dataset_rele_flags(ds, dsflags, FTAG);
1144eda14cbcSMatt Macy 			return (SET_ERROR(EINVAL));
1145eda14cbcSMatt Macy 		}
1146eda14cbcSMatt Macy 
1147eda14cbcSMatt Macy 		if (!dsl_dataset_get_uint64_array_feature(ds,
1148eda14cbcSMatt Macy 		    SPA_FEATURE_REDACTED_DATASETS, &num_ds_redact_snaps,
1149eda14cbcSMatt Macy 		    &ds_redact_snaps)) {
1150eda14cbcSMatt Macy 			dsl_dataset_rele_flags(ds, dsflags, FTAG);
1151eda14cbcSMatt Macy 			return (SET_ERROR(EINVAL));
1152eda14cbcSMatt Macy 		}
1153eda14cbcSMatt Macy 
1154eda14cbcSMatt Macy 		for (int i = 0; i < num_ds_redact_snaps; i++) {
1155eda14cbcSMatt Macy 			if (!redact_snaps_contains(ds_redact_snaps,
1156eda14cbcSMatt Macy 			    num_ds_redact_snaps, stream_redact_snaps[i])) {
1157eda14cbcSMatt Macy 				dsl_dataset_rele_flags(ds, dsflags, FTAG);
1158eda14cbcSMatt Macy 				return (SET_ERROR(EINVAL));
1159eda14cbcSMatt Macy 			}
1160eda14cbcSMatt Macy 		}
1161eda14cbcSMatt Macy 	}
1162eda14cbcSMatt Macy 
1163eda14cbcSMatt Macy 	error = recv_check_large_blocks(ds, drc->drc_featureflags);
1164eda14cbcSMatt Macy 	if (error != 0) {
1165eda14cbcSMatt Macy 		dsl_dataset_rele_flags(ds, dsflags, FTAG);
1166eda14cbcSMatt Macy 		return (error);
1167eda14cbcSMatt Macy 	}
1168eda14cbcSMatt Macy 
1169eda14cbcSMatt Macy 	dsl_dataset_rele_flags(ds, dsflags, FTAG);
1170eda14cbcSMatt Macy 	return (0);
1171eda14cbcSMatt Macy }
1172eda14cbcSMatt Macy 
1173eda14cbcSMatt Macy static void
1174eda14cbcSMatt Macy dmu_recv_resume_begin_sync(void *arg, dmu_tx_t *tx)
1175eda14cbcSMatt Macy {
1176eda14cbcSMatt Macy 	dmu_recv_begin_arg_t *drba = arg;
1177eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
1178eda14cbcSMatt Macy 	const char *tofs = drba->drba_cookie->drc_tofs;
1179eda14cbcSMatt Macy 	uint64_t featureflags = drba->drba_cookie->drc_featureflags;
1180eda14cbcSMatt Macy 	dsl_dataset_t *ds;
11817877fdebSMatt Macy 	ds_hold_flags_t dsflags = DS_HOLD_FLAG_NONE;
1182eda14cbcSMatt Macy 	/* 6 extra bytes for /%recv */
1183eda14cbcSMatt Macy 	char recvname[ZFS_MAX_DATASET_NAME_LEN + 6];
1184eda14cbcSMatt Macy 
1185eda14cbcSMatt Macy 	(void) snprintf(recvname, sizeof (recvname), "%s/%s", tofs,
1186eda14cbcSMatt Macy 	    recv_clone_name);
1187eda14cbcSMatt Macy 
1188eda14cbcSMatt Macy 	if (featureflags & DMU_BACKUP_FEATURE_RAW) {
1189eda14cbcSMatt Macy 		drba->drba_cookie->drc_raw = B_TRUE;
1190eda14cbcSMatt Macy 	} else {
1191eda14cbcSMatt Macy 		dsflags |= DS_HOLD_FLAG_DECRYPT;
1192eda14cbcSMatt Macy 	}
1193eda14cbcSMatt Macy 
1194eda14cbcSMatt Macy 	if (dsl_dataset_own_force(dp, recvname, dsflags, dmu_recv_tag, &ds)
1195eda14cbcSMatt Macy 	    != 0) {
1196eda14cbcSMatt Macy 		/* %recv does not exist; continue in tofs */
1197eda14cbcSMatt Macy 		VERIFY0(dsl_dataset_own_force(dp, tofs, dsflags, dmu_recv_tag,
1198eda14cbcSMatt Macy 		    &ds));
1199eda14cbcSMatt Macy 		drba->drba_cookie->drc_newfs = B_TRUE;
1200eda14cbcSMatt Macy 	}
1201eda14cbcSMatt Macy 
1202eda14cbcSMatt Macy 	ASSERT(DS_IS_INCONSISTENT(ds));
1203eda14cbcSMatt Macy 	rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
1204eda14cbcSMatt Macy 	ASSERT(!BP_IS_HOLE(dsl_dataset_get_blkptr(ds)) ||
1205eda14cbcSMatt Macy 	    drba->drba_cookie->drc_raw);
1206eda14cbcSMatt Macy 	rrw_exit(&ds->ds_bp_rwlock, FTAG);
1207eda14cbcSMatt Macy 
1208eda14cbcSMatt Macy 	drba->drba_cookie->drc_ds = ds;
1209eda14cbcSMatt Macy 	VERIFY0(dmu_objset_from_ds(ds, &drba->drba_cookie->drc_os));
1210eda14cbcSMatt Macy 	drba->drba_cookie->drc_should_save = B_TRUE;
1211eda14cbcSMatt Macy 
1212eda14cbcSMatt Macy 	spa_history_log_internal_ds(ds, "resume receive", tx, " ");
1213eda14cbcSMatt Macy }
1214eda14cbcSMatt Macy 
1215eda14cbcSMatt Macy /*
1216eda14cbcSMatt Macy  * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
1217eda14cbcSMatt Macy  * succeeds; otherwise we will leak the holds on the datasets.
1218eda14cbcSMatt Macy  */
1219eda14cbcSMatt Macy int
1220*2a58b312SMartin Matuska dmu_recv_begin(const char *tofs, const char *tosnap,
1221*2a58b312SMartin Matuska     dmu_replay_record_t *drr_begin, boolean_t force, boolean_t heal,
1222*2a58b312SMartin Matuska     boolean_t resumable, nvlist_t *localprops, nvlist_t *hidden_args,
1223*2a58b312SMartin Matuska     const char *origin, dmu_recv_cookie_t *drc, zfs_file_t *fp,
1224*2a58b312SMartin Matuska     offset_t *voffp)
1225eda14cbcSMatt Macy {
1226eda14cbcSMatt Macy 	dmu_recv_begin_arg_t drba = { 0 };
1227*2a58b312SMartin Matuska 	int err = 0;
1228eda14cbcSMatt Macy 
1229da5137abSMartin Matuska 	memset(drc, 0, sizeof (dmu_recv_cookie_t));
1230eda14cbcSMatt Macy 	drc->drc_drr_begin = drr_begin;
1231eda14cbcSMatt Macy 	drc->drc_drrb = &drr_begin->drr_u.drr_begin;
1232eda14cbcSMatt Macy 	drc->drc_tosnap = tosnap;
1233eda14cbcSMatt Macy 	drc->drc_tofs = tofs;
1234eda14cbcSMatt Macy 	drc->drc_force = force;
1235271171e0SMartin Matuska 	drc->drc_heal = heal;
1236eda14cbcSMatt Macy 	drc->drc_resumable = resumable;
1237eda14cbcSMatt Macy 	drc->drc_cred = CRED();
1238eda14cbcSMatt Macy 	drc->drc_proc = curproc;
1239eda14cbcSMatt Macy 	drc->drc_clone = (origin != NULL);
1240eda14cbcSMatt Macy 
1241eda14cbcSMatt Macy 	if (drc->drc_drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
1242eda14cbcSMatt Macy 		drc->drc_byteswap = B_TRUE;
1243eda14cbcSMatt Macy 		(void) fletcher_4_incremental_byteswap(drr_begin,
1244eda14cbcSMatt Macy 		    sizeof (dmu_replay_record_t), &drc->drc_cksum);
1245eda14cbcSMatt Macy 		byteswap_record(drr_begin);
1246eda14cbcSMatt Macy 	} else if (drc->drc_drrb->drr_magic == DMU_BACKUP_MAGIC) {
1247eda14cbcSMatt Macy 		(void) fletcher_4_incremental_native(drr_begin,
1248eda14cbcSMatt Macy 		    sizeof (dmu_replay_record_t), &drc->drc_cksum);
1249eda14cbcSMatt Macy 	} else {
1250eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
1251eda14cbcSMatt Macy 	}
1252eda14cbcSMatt Macy 
1253eda14cbcSMatt Macy 	drc->drc_fp = fp;
1254eda14cbcSMatt Macy 	drc->drc_voff = *voffp;
1255eda14cbcSMatt Macy 	drc->drc_featureflags =
1256eda14cbcSMatt Macy 	    DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo);
1257eda14cbcSMatt Macy 
1258eda14cbcSMatt Macy 	uint32_t payloadlen = drc->drc_drr_begin->drr_payloadlen;
125915f0b8c3SMartin Matuska 
126015f0b8c3SMartin Matuska 	/*
126115f0b8c3SMartin Matuska 	 * Since OpenZFS 2.0.0, we have enforced a 64MB limit in userspace
126215f0b8c3SMartin Matuska 	 * configurable via ZFS_SENDRECV_MAX_NVLIST. We enforce 256MB as a hard
126315f0b8c3SMartin Matuska 	 * upper limit. Systems with less than 1GB of RAM will see a lower
126415f0b8c3SMartin Matuska 	 * limit from `arc_all_memory() / 4`.
126515f0b8c3SMartin Matuska 	 */
126615f0b8c3SMartin Matuska 	if (payloadlen > (MIN((1U << 28), arc_all_memory() / 4)))
126715f0b8c3SMartin Matuska 		return (E2BIG);
126815f0b8c3SMartin Matuska 
1269*2a58b312SMartin Matuska 
1270*2a58b312SMartin Matuska 	if (payloadlen != 0) {
1271*2a58b312SMartin Matuska 		void *payload = vmem_alloc(payloadlen, KM_SLEEP);
1272*2a58b312SMartin Matuska 		/*
1273*2a58b312SMartin Matuska 		 * For compatibility with recursive send streams, we don't do
1274*2a58b312SMartin Matuska 		 * this here if the stream could be part of a package. Instead,
1275*2a58b312SMartin Matuska 		 * we'll do it in dmu_recv_stream. If we pull the next header
1276*2a58b312SMartin Matuska 		 * too early, and it's the END record, we break the `recv_skip`
1277*2a58b312SMartin Matuska 		 * logic.
1278*2a58b312SMartin Matuska 		 */
1279eda14cbcSMatt Macy 
1280eda14cbcSMatt Macy 		err = receive_read_payload_and_next_header(drc, payloadlen,
1281eda14cbcSMatt Macy 		    payload);
1282eda14cbcSMatt Macy 		if (err != 0) {
128315f0b8c3SMartin Matuska 			vmem_free(payload, payloadlen);
1284eda14cbcSMatt Macy 			return (err);
1285eda14cbcSMatt Macy 		}
1286eda14cbcSMatt Macy 		err = nvlist_unpack(payload, payloadlen, &drc->drc_begin_nvl,
1287eda14cbcSMatt Macy 		    KM_SLEEP);
128815f0b8c3SMartin Matuska 		vmem_free(payload, payloadlen);
1289eda14cbcSMatt Macy 		if (err != 0) {
1290eda14cbcSMatt Macy 			kmem_free(drc->drc_next_rrd,
1291eda14cbcSMatt Macy 			    sizeof (*drc->drc_next_rrd));
1292eda14cbcSMatt Macy 			return (err);
1293eda14cbcSMatt Macy 		}
1294eda14cbcSMatt Macy 	}
1295eda14cbcSMatt Macy 
1296eda14cbcSMatt Macy 	if (drc->drc_drrb->drr_flags & DRR_FLAG_SPILL_BLOCK)
1297eda14cbcSMatt Macy 		drc->drc_spill = B_TRUE;
1298eda14cbcSMatt Macy 
1299eda14cbcSMatt Macy 	drba.drba_origin = origin;
1300eda14cbcSMatt Macy 	drba.drba_cookie = drc;
1301eda14cbcSMatt Macy 	drba.drba_cred = CRED();
1302eda14cbcSMatt Macy 	drba.drba_proc = curproc;
1303eda14cbcSMatt Macy 
1304eda14cbcSMatt Macy 	if (drc->drc_featureflags & DMU_BACKUP_FEATURE_RESUMING) {
1305eda14cbcSMatt Macy 		err = dsl_sync_task(tofs,
1306eda14cbcSMatt Macy 		    dmu_recv_resume_begin_check, dmu_recv_resume_begin_sync,
1307eda14cbcSMatt Macy 		    &drba, 5, ZFS_SPACE_CHECK_NORMAL);
1308eda14cbcSMatt Macy 	} else {
1309eda14cbcSMatt Macy 		/*
1310eda14cbcSMatt Macy 		 * For non-raw, non-incremental, non-resuming receives the
1311eda14cbcSMatt Macy 		 * user can specify encryption parameters on the command line
1312eda14cbcSMatt Macy 		 * with "zfs recv -o". For these receives we create a dcp and
1313eda14cbcSMatt Macy 		 * pass it to the sync task. Creating the dcp will implicitly
1314eda14cbcSMatt Macy 		 * remove the encryption params from the localprops nvlist,
1315eda14cbcSMatt Macy 		 * which avoids errors when trying to set these normally
1316eda14cbcSMatt Macy 		 * read-only properties. Any other kind of receive that
1317eda14cbcSMatt Macy 		 * attempts to set these properties will fail as a result.
1318eda14cbcSMatt Macy 		 */
1319eda14cbcSMatt Macy 		if ((DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo) &
1320eda14cbcSMatt Macy 		    DMU_BACKUP_FEATURE_RAW) == 0 &&
1321eda14cbcSMatt Macy 		    origin == NULL && drc->drc_drrb->drr_fromguid == 0) {
1322eda14cbcSMatt Macy 			err = dsl_crypto_params_create_nvlist(DCP_CMD_NONE,
1323eda14cbcSMatt Macy 			    localprops, hidden_args, &drba.drba_dcp);
1324eda14cbcSMatt Macy 		}
1325eda14cbcSMatt Macy 
1326eda14cbcSMatt Macy 		if (err == 0) {
1327eda14cbcSMatt Macy 			err = dsl_sync_task(tofs,
1328eda14cbcSMatt Macy 			    dmu_recv_begin_check, dmu_recv_begin_sync,
1329eda14cbcSMatt Macy 			    &drba, 5, ZFS_SPACE_CHECK_NORMAL);
1330eda14cbcSMatt Macy 			dsl_crypto_params_free(drba.drba_dcp, !!err);
1331eda14cbcSMatt Macy 		}
1332eda14cbcSMatt Macy 	}
1333eda14cbcSMatt Macy 
1334eda14cbcSMatt Macy 	if (err != 0) {
1335eda14cbcSMatt Macy 		kmem_free(drc->drc_next_rrd, sizeof (*drc->drc_next_rrd));
1336eda14cbcSMatt Macy 		nvlist_free(drc->drc_begin_nvl);
1337eda14cbcSMatt Macy 	}
1338eda14cbcSMatt Macy 	return (err);
1339eda14cbcSMatt Macy }
1340eda14cbcSMatt Macy 
1341271171e0SMartin Matuska /*
1342271171e0SMartin Matuska  * Holds data need for corrective recv callback
1343271171e0SMartin Matuska  */
1344271171e0SMartin Matuska typedef struct cr_cb_data {
1345271171e0SMartin Matuska 	uint64_t size;
1346271171e0SMartin Matuska 	zbookmark_phys_t zb;
1347271171e0SMartin Matuska 	spa_t *spa;
1348271171e0SMartin Matuska } cr_cb_data_t;
1349271171e0SMartin Matuska 
1350271171e0SMartin Matuska static void
1351271171e0SMartin Matuska corrective_read_done(zio_t *zio)
1352271171e0SMartin Matuska {
1353271171e0SMartin Matuska 	cr_cb_data_t *data = zio->io_private;
1354271171e0SMartin Matuska 	/* Corruption corrected; update error log if needed */
1355271171e0SMartin Matuska 	if (zio->io_error == 0)
1356271171e0SMartin Matuska 		spa_remove_error(data->spa, &data->zb);
1357271171e0SMartin Matuska 	kmem_free(data, sizeof (cr_cb_data_t));
1358271171e0SMartin Matuska 	abd_free(zio->io_abd);
1359271171e0SMartin Matuska }
1360271171e0SMartin Matuska 
1361271171e0SMartin Matuska /*
1362271171e0SMartin Matuska  * zio_rewrite the data pointed to by bp with the data from the rrd's abd.
1363271171e0SMartin Matuska  */
1364271171e0SMartin Matuska static int
1365271171e0SMartin Matuska do_corrective_recv(struct receive_writer_arg *rwa, struct drr_write *drrw,
1366271171e0SMartin Matuska     struct receive_record_arg *rrd, blkptr_t *bp)
1367271171e0SMartin Matuska {
1368271171e0SMartin Matuska 	int err;
1369271171e0SMartin Matuska 	zio_t *io;
1370271171e0SMartin Matuska 	zbookmark_phys_t zb;
1371271171e0SMartin Matuska 	dnode_t *dn;
1372271171e0SMartin Matuska 	abd_t *abd = rrd->abd;
1373271171e0SMartin Matuska 	zio_cksum_t bp_cksum = bp->blk_cksum;
1374dbd5678dSMartin Matuska 	zio_flag_t flags = ZIO_FLAG_SPECULATIVE |
1375271171e0SMartin Matuska 	    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_RETRY | ZIO_FLAG_CANFAIL;
1376271171e0SMartin Matuska 
1377271171e0SMartin Matuska 	if (rwa->raw)
1378271171e0SMartin Matuska 		flags |= ZIO_FLAG_RAW;
1379271171e0SMartin Matuska 
1380271171e0SMartin Matuska 	err = dnode_hold(rwa->os, drrw->drr_object, FTAG, &dn);
1381271171e0SMartin Matuska 	if (err != 0)
1382271171e0SMartin Matuska 		return (err);
1383271171e0SMartin Matuska 	SET_BOOKMARK(&zb, dmu_objset_id(rwa->os), drrw->drr_object, 0,
1384271171e0SMartin Matuska 	    dbuf_whichblock(dn, 0, drrw->drr_offset));
1385271171e0SMartin Matuska 	dnode_rele(dn, FTAG);
1386271171e0SMartin Matuska 
1387271171e0SMartin Matuska 	if (!rwa->raw && DRR_WRITE_COMPRESSED(drrw)) {
1388271171e0SMartin Matuska 		/* Decompress the stream data */
1389271171e0SMartin Matuska 		abd_t *dabd = abd_alloc_linear(
1390271171e0SMartin Matuska 		    drrw->drr_logical_size, B_FALSE);
1391271171e0SMartin Matuska 		err = zio_decompress_data(drrw->drr_compressiontype,
1392271171e0SMartin Matuska 		    abd, abd_to_buf(dabd), abd_get_size(abd),
1393271171e0SMartin Matuska 		    abd_get_size(dabd), NULL);
1394271171e0SMartin Matuska 
1395271171e0SMartin Matuska 		if (err != 0) {
1396271171e0SMartin Matuska 			abd_free(dabd);
1397271171e0SMartin Matuska 			return (err);
1398271171e0SMartin Matuska 		}
1399271171e0SMartin Matuska 		/* Swap in the newly decompressed data into the abd */
1400271171e0SMartin Matuska 		abd_free(abd);
1401271171e0SMartin Matuska 		abd = dabd;
1402271171e0SMartin Matuska 	}
1403271171e0SMartin Matuska 
1404271171e0SMartin Matuska 	if (!rwa->raw && BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF) {
1405271171e0SMartin Matuska 		/* Recompress the data */
1406271171e0SMartin Matuska 		abd_t *cabd = abd_alloc_linear(BP_GET_PSIZE(bp),
1407271171e0SMartin Matuska 		    B_FALSE);
1408*2a58b312SMartin Matuska 		void *buf = abd_to_buf(cabd);
1409271171e0SMartin Matuska 		uint64_t csize = zio_compress_data(BP_GET_COMPRESS(bp),
1410*2a58b312SMartin Matuska 		    abd, &buf, abd_get_size(abd),
1411271171e0SMartin Matuska 		    rwa->os->os_complevel);
1412271171e0SMartin Matuska 		abd_zero_off(cabd, csize, BP_GET_PSIZE(bp) - csize);
1413271171e0SMartin Matuska 		/* Swap in newly compressed data into the abd */
1414271171e0SMartin Matuska 		abd_free(abd);
1415271171e0SMartin Matuska 		abd = cabd;
1416271171e0SMartin Matuska 		flags |= ZIO_FLAG_RAW_COMPRESS;
1417271171e0SMartin Matuska 	}
1418271171e0SMartin Matuska 
1419271171e0SMartin Matuska 	/*
1420271171e0SMartin Matuska 	 * The stream is not encrypted but the data on-disk is.
1421271171e0SMartin Matuska 	 * We need to re-encrypt the buf using the same
1422271171e0SMartin Matuska 	 * encryption type, salt, iv, and mac that was used to encrypt
1423271171e0SMartin Matuska 	 * the block previosly.
1424271171e0SMartin Matuska 	 */
1425271171e0SMartin Matuska 	if (!rwa->raw && BP_USES_CRYPT(bp)) {
1426271171e0SMartin Matuska 		dsl_dataset_t *ds;
1427271171e0SMartin Matuska 		dsl_crypto_key_t *dck = NULL;
1428271171e0SMartin Matuska 		uint8_t salt[ZIO_DATA_SALT_LEN];
1429271171e0SMartin Matuska 		uint8_t iv[ZIO_DATA_IV_LEN];
1430271171e0SMartin Matuska 		uint8_t mac[ZIO_DATA_MAC_LEN];
1431271171e0SMartin Matuska 		boolean_t no_crypt = B_FALSE;
1432271171e0SMartin Matuska 		dsl_pool_t *dp = dmu_objset_pool(rwa->os);
1433271171e0SMartin Matuska 		abd_t *eabd = abd_alloc_linear(BP_GET_PSIZE(bp), B_FALSE);
1434271171e0SMartin Matuska 
1435271171e0SMartin Matuska 		zio_crypt_decode_params_bp(bp, salt, iv);
1436271171e0SMartin Matuska 		zio_crypt_decode_mac_bp(bp, mac);
1437271171e0SMartin Matuska 
1438271171e0SMartin Matuska 		dsl_pool_config_enter(dp, FTAG);
1439271171e0SMartin Matuska 		err = dsl_dataset_hold_flags(dp, rwa->tofs,
1440271171e0SMartin Matuska 		    DS_HOLD_FLAG_DECRYPT, FTAG, &ds);
1441271171e0SMartin Matuska 		if (err != 0) {
1442271171e0SMartin Matuska 			dsl_pool_config_exit(dp, FTAG);
1443271171e0SMartin Matuska 			abd_free(eabd);
1444271171e0SMartin Matuska 			return (SET_ERROR(EACCES));
1445271171e0SMartin Matuska 		}
1446271171e0SMartin Matuska 
1447271171e0SMartin Matuska 		/* Look up the key from the spa's keystore */
1448271171e0SMartin Matuska 		err = spa_keystore_lookup_key(rwa->os->os_spa,
1449271171e0SMartin Matuska 		    zb.zb_objset, FTAG, &dck);
1450271171e0SMartin Matuska 		if (err != 0) {
1451271171e0SMartin Matuska 			dsl_dataset_rele_flags(ds, DS_HOLD_FLAG_DECRYPT,
1452271171e0SMartin Matuska 			    FTAG);
1453271171e0SMartin Matuska 			dsl_pool_config_exit(dp, FTAG);
1454271171e0SMartin Matuska 			abd_free(eabd);
1455271171e0SMartin Matuska 			return (SET_ERROR(EACCES));
1456271171e0SMartin Matuska 		}
1457271171e0SMartin Matuska 
1458271171e0SMartin Matuska 		err = zio_do_crypt_abd(B_TRUE, &dck->dck_key,
1459271171e0SMartin Matuska 		    BP_GET_TYPE(bp), BP_SHOULD_BYTESWAP(bp), salt, iv,
1460271171e0SMartin Matuska 		    mac, abd_get_size(abd), abd, eabd, &no_crypt);
1461271171e0SMartin Matuska 
1462271171e0SMartin Matuska 		spa_keystore_dsl_key_rele(rwa->os->os_spa, dck, FTAG);
1463271171e0SMartin Matuska 		dsl_dataset_rele_flags(ds, DS_HOLD_FLAG_DECRYPT, FTAG);
1464271171e0SMartin Matuska 		dsl_pool_config_exit(dp, FTAG);
1465271171e0SMartin Matuska 
1466271171e0SMartin Matuska 		ASSERT0(no_crypt);
1467271171e0SMartin Matuska 		if (err != 0) {
1468271171e0SMartin Matuska 			abd_free(eabd);
1469271171e0SMartin Matuska 			return (err);
1470271171e0SMartin Matuska 		}
1471271171e0SMartin Matuska 		/* Swap in the newly encrypted data into the abd */
1472271171e0SMartin Matuska 		abd_free(abd);
1473271171e0SMartin Matuska 		abd = eabd;
1474271171e0SMartin Matuska 
1475271171e0SMartin Matuska 		/*
1476271171e0SMartin Matuska 		 * We want to prevent zio_rewrite() from trying to
1477271171e0SMartin Matuska 		 * encrypt the data again
1478271171e0SMartin Matuska 		 */
1479271171e0SMartin Matuska 		flags |= ZIO_FLAG_RAW_ENCRYPT;
1480271171e0SMartin Matuska 	}
1481271171e0SMartin Matuska 	rrd->abd = abd;
1482271171e0SMartin Matuska 
1483271171e0SMartin Matuska 	io = zio_rewrite(NULL, rwa->os->os_spa, bp->blk_birth, bp, abd,
1484271171e0SMartin Matuska 	    BP_GET_PSIZE(bp), NULL, NULL, ZIO_PRIORITY_SYNC_WRITE, flags, &zb);
1485271171e0SMartin Matuska 
1486271171e0SMartin Matuska 	ASSERT(abd_get_size(abd) == BP_GET_LSIZE(bp) ||
1487271171e0SMartin Matuska 	    abd_get_size(abd) == BP_GET_PSIZE(bp));
1488271171e0SMartin Matuska 
1489271171e0SMartin Matuska 	/* compute new bp checksum value and make sure it matches the old one */
1490271171e0SMartin Matuska 	zio_checksum_compute(io, BP_GET_CHECKSUM(bp), abd, abd_get_size(abd));
1491271171e0SMartin Matuska 	if (!ZIO_CHECKSUM_EQUAL(bp_cksum, io->io_bp->blk_cksum)) {
1492271171e0SMartin Matuska 		zio_destroy(io);
1493271171e0SMartin Matuska 		if (zfs_recv_best_effort_corrective != 0)
1494271171e0SMartin Matuska 			return (0);
1495271171e0SMartin Matuska 		return (SET_ERROR(ECKSUM));
1496271171e0SMartin Matuska 	}
1497271171e0SMartin Matuska 
1498271171e0SMartin Matuska 	/* Correct the corruption in place */
1499271171e0SMartin Matuska 	err = zio_wait(io);
1500271171e0SMartin Matuska 	if (err == 0) {
1501271171e0SMartin Matuska 		cr_cb_data_t *cb_data =
1502271171e0SMartin Matuska 		    kmem_alloc(sizeof (cr_cb_data_t), KM_SLEEP);
1503271171e0SMartin Matuska 		cb_data->spa = rwa->os->os_spa;
1504271171e0SMartin Matuska 		cb_data->size = drrw->drr_logical_size;
1505271171e0SMartin Matuska 		cb_data->zb = zb;
1506271171e0SMartin Matuska 		/* Test if healing worked by re-reading the bp */
1507271171e0SMartin Matuska 		err = zio_wait(zio_read(rwa->heal_pio, rwa->os->os_spa, bp,
1508271171e0SMartin Matuska 		    abd_alloc_for_io(drrw->drr_logical_size, B_FALSE),
1509271171e0SMartin Matuska 		    drrw->drr_logical_size, corrective_read_done,
1510271171e0SMartin Matuska 		    cb_data, ZIO_PRIORITY_ASYNC_READ, flags, NULL));
1511271171e0SMartin Matuska 	}
1512271171e0SMartin Matuska 	if (err != 0 && zfs_recv_best_effort_corrective != 0)
1513271171e0SMartin Matuska 		err = 0;
1514271171e0SMartin Matuska 
1515271171e0SMartin Matuska 	return (err);
1516271171e0SMartin Matuska }
1517271171e0SMartin Matuska 
1518eda14cbcSMatt Macy static int
1519eda14cbcSMatt Macy receive_read(dmu_recv_cookie_t *drc, int len, void *buf)
1520eda14cbcSMatt Macy {
1521eda14cbcSMatt Macy 	int done = 0;
1522eda14cbcSMatt Macy 
1523eda14cbcSMatt Macy 	/*
1524eda14cbcSMatt Macy 	 * The code doesn't rely on this (lengths being multiples of 8).  See
1525eda14cbcSMatt Macy 	 * comment in dump_bytes.
1526eda14cbcSMatt Macy 	 */
1527eda14cbcSMatt Macy 	ASSERT(len % 8 == 0 ||
1528eda14cbcSMatt Macy 	    (drc->drc_featureflags & DMU_BACKUP_FEATURE_RAW) != 0);
1529eda14cbcSMatt Macy 
1530eda14cbcSMatt Macy 	while (done < len) {
153115f0b8c3SMartin Matuska 		ssize_t resid = len - done;
1532eda14cbcSMatt Macy 		zfs_file_t *fp = drc->drc_fp;
1533eda14cbcSMatt Macy 		int err = zfs_file_read(fp, (char *)buf + done,
1534eda14cbcSMatt Macy 		    len - done, &resid);
153515f0b8c3SMartin Matuska 		if (err == 0 && resid == len - done) {
1536eda14cbcSMatt Macy 			/*
1537eda14cbcSMatt Macy 			 * Note: ECKSUM or ZFS_ERR_STREAM_TRUNCATED indicates
1538eda14cbcSMatt Macy 			 * that the receive was interrupted and can
1539eda14cbcSMatt Macy 			 * potentially be resumed.
1540eda14cbcSMatt Macy 			 */
1541eda14cbcSMatt Macy 			err = SET_ERROR(ZFS_ERR_STREAM_TRUNCATED);
1542eda14cbcSMatt Macy 		}
1543eda14cbcSMatt Macy 		drc->drc_voff += len - done - resid;
1544eda14cbcSMatt Macy 		done = len - resid;
1545eda14cbcSMatt Macy 		if (err != 0)
1546eda14cbcSMatt Macy 			return (err);
1547eda14cbcSMatt Macy 	}
1548eda14cbcSMatt Macy 
1549eda14cbcSMatt Macy 	drc->drc_bytes_read += len;
1550eda14cbcSMatt Macy 
1551eda14cbcSMatt Macy 	ASSERT3U(done, ==, len);
1552eda14cbcSMatt Macy 	return (0);
1553eda14cbcSMatt Macy }
1554eda14cbcSMatt Macy 
1555eda14cbcSMatt Macy static inline uint8_t
1556eda14cbcSMatt Macy deduce_nblkptr(dmu_object_type_t bonus_type, uint64_t bonus_size)
1557eda14cbcSMatt Macy {
1558eda14cbcSMatt Macy 	if (bonus_type == DMU_OT_SA) {
1559eda14cbcSMatt Macy 		return (1);
1560eda14cbcSMatt Macy 	} else {
1561eda14cbcSMatt Macy 		return (1 +
1562eda14cbcSMatt Macy 		    ((DN_OLD_MAX_BONUSLEN -
1563eda14cbcSMatt Macy 		    MIN(DN_OLD_MAX_BONUSLEN, bonus_size)) >> SPA_BLKPTRSHIFT));
1564eda14cbcSMatt Macy 	}
1565eda14cbcSMatt Macy }
1566eda14cbcSMatt Macy 
1567eda14cbcSMatt Macy static void
1568eda14cbcSMatt Macy save_resume_state(struct receive_writer_arg *rwa,
1569eda14cbcSMatt Macy     uint64_t object, uint64_t offset, dmu_tx_t *tx)
1570eda14cbcSMatt Macy {
1571eda14cbcSMatt Macy 	int txgoff = dmu_tx_get_txg(tx) & TXG_MASK;
1572eda14cbcSMatt Macy 
1573eda14cbcSMatt Macy 	if (!rwa->resumable)
1574eda14cbcSMatt Macy 		return;
1575eda14cbcSMatt Macy 
1576eda14cbcSMatt Macy 	/*
1577eda14cbcSMatt Macy 	 * We use ds_resume_bytes[] != 0 to indicate that we need to
1578eda14cbcSMatt Macy 	 * update this on disk, so it must not be 0.
1579eda14cbcSMatt Macy 	 */
1580eda14cbcSMatt Macy 	ASSERT(rwa->bytes_read != 0);
1581eda14cbcSMatt Macy 
1582eda14cbcSMatt Macy 	/*
1583eda14cbcSMatt Macy 	 * We only resume from write records, which have a valid
1584eda14cbcSMatt Macy 	 * (non-meta-dnode) object number.
1585eda14cbcSMatt Macy 	 */
1586eda14cbcSMatt Macy 	ASSERT(object != 0);
1587eda14cbcSMatt Macy 
1588eda14cbcSMatt Macy 	/*
1589eda14cbcSMatt Macy 	 * For resuming to work correctly, we must receive records in order,
1590eda14cbcSMatt Macy 	 * sorted by object,offset.  This is checked by the callers, but
1591eda14cbcSMatt Macy 	 * assert it here for good measure.
1592eda14cbcSMatt Macy 	 */
1593eda14cbcSMatt Macy 	ASSERT3U(object, >=, rwa->os->os_dsl_dataset->ds_resume_object[txgoff]);
1594eda14cbcSMatt Macy 	ASSERT(object != rwa->os->os_dsl_dataset->ds_resume_object[txgoff] ||
1595eda14cbcSMatt Macy 	    offset >= rwa->os->os_dsl_dataset->ds_resume_offset[txgoff]);
1596eda14cbcSMatt Macy 	ASSERT3U(rwa->bytes_read, >=,
1597eda14cbcSMatt Macy 	    rwa->os->os_dsl_dataset->ds_resume_bytes[txgoff]);
1598eda14cbcSMatt Macy 
1599eda14cbcSMatt Macy 	rwa->os->os_dsl_dataset->ds_resume_object[txgoff] = object;
1600eda14cbcSMatt Macy 	rwa->os->os_dsl_dataset->ds_resume_offset[txgoff] = offset;
1601eda14cbcSMatt Macy 	rwa->os->os_dsl_dataset->ds_resume_bytes[txgoff] = rwa->bytes_read;
1602eda14cbcSMatt Macy }
1603eda14cbcSMatt Macy 
1604eda14cbcSMatt Macy static int
1605eda14cbcSMatt Macy receive_object_is_same_generation(objset_t *os, uint64_t object,
1606eda14cbcSMatt Macy     dmu_object_type_t old_bonus_type, dmu_object_type_t new_bonus_type,
1607eda14cbcSMatt Macy     const void *new_bonus, boolean_t *samegenp)
1608eda14cbcSMatt Macy {
1609eda14cbcSMatt Macy 	zfs_file_info_t zoi;
1610eda14cbcSMatt Macy 	int err;
1611eda14cbcSMatt Macy 
1612eda14cbcSMatt Macy 	dmu_buf_t *old_bonus_dbuf;
1613eda14cbcSMatt Macy 	err = dmu_bonus_hold(os, object, FTAG, &old_bonus_dbuf);
1614eda14cbcSMatt Macy 	if (err != 0)
1615eda14cbcSMatt Macy 		return (err);
1616eda14cbcSMatt Macy 	err = dmu_get_file_info(os, old_bonus_type, old_bonus_dbuf->db_data,
1617eda14cbcSMatt Macy 	    &zoi);
1618eda14cbcSMatt Macy 	dmu_buf_rele(old_bonus_dbuf, FTAG);
1619eda14cbcSMatt Macy 	if (err != 0)
1620eda14cbcSMatt Macy 		return (err);
1621eda14cbcSMatt Macy 	uint64_t old_gen = zoi.zfi_generation;
1622eda14cbcSMatt Macy 
1623eda14cbcSMatt Macy 	err = dmu_get_file_info(os, new_bonus_type, new_bonus, &zoi);
1624eda14cbcSMatt Macy 	if (err != 0)
1625eda14cbcSMatt Macy 		return (err);
1626eda14cbcSMatt Macy 	uint64_t new_gen = zoi.zfi_generation;
1627eda14cbcSMatt Macy 
1628eda14cbcSMatt Macy 	*samegenp = (old_gen == new_gen);
1629eda14cbcSMatt Macy 	return (0);
1630eda14cbcSMatt Macy }
1631eda14cbcSMatt Macy 
1632eda14cbcSMatt Macy static int
1633eda14cbcSMatt Macy receive_handle_existing_object(const struct receive_writer_arg *rwa,
1634eda14cbcSMatt Macy     const struct drr_object *drro, const dmu_object_info_t *doi,
1635eda14cbcSMatt Macy     const void *bonus_data,
1636eda14cbcSMatt Macy     uint64_t *object_to_hold, uint32_t *new_blksz)
1637eda14cbcSMatt Macy {
1638eda14cbcSMatt Macy 	uint32_t indblksz = drro->drr_indblkshift ?
1639eda14cbcSMatt Macy 	    1ULL << drro->drr_indblkshift : 0;
1640eda14cbcSMatt Macy 	int nblkptr = deduce_nblkptr(drro->drr_bonustype,
1641eda14cbcSMatt Macy 	    drro->drr_bonuslen);
1642eda14cbcSMatt Macy 	uint8_t dn_slots = drro->drr_dn_slots != 0 ?
1643eda14cbcSMatt Macy 	    drro->drr_dn_slots : DNODE_MIN_SLOTS;
1644eda14cbcSMatt Macy 	boolean_t do_free_range = B_FALSE;
1645eda14cbcSMatt Macy 	int err;
1646eda14cbcSMatt Macy 
1647eda14cbcSMatt Macy 	*object_to_hold = drro->drr_object;
1648eda14cbcSMatt Macy 
1649eda14cbcSMatt Macy 	/* nblkptr should be bounded by the bonus size and type */
1650eda14cbcSMatt Macy 	if (rwa->raw && nblkptr != drro->drr_nblkptr)
1651eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
1652eda14cbcSMatt Macy 
1653eda14cbcSMatt Macy 	/*
1654eda14cbcSMatt Macy 	 * After the previous send stream, the sending system may
1655eda14cbcSMatt Macy 	 * have freed this object, and then happened to re-allocate
1656eda14cbcSMatt Macy 	 * this object number in a later txg. In this case, we are
1657eda14cbcSMatt Macy 	 * receiving a different logical file, and the block size may
1658eda14cbcSMatt Macy 	 * appear to be different.  i.e. we may have a different
1659eda14cbcSMatt Macy 	 * block size for this object than what the send stream says.
1660eda14cbcSMatt Macy 	 * In this case we need to remove the object's contents,
1661eda14cbcSMatt Macy 	 * so that its structure can be changed and then its contents
1662eda14cbcSMatt Macy 	 * entirely replaced by subsequent WRITE records.
1663eda14cbcSMatt Macy 	 *
1664eda14cbcSMatt Macy 	 * If this is a -L (--large-block) incremental stream, and
1665eda14cbcSMatt Macy 	 * the previous stream was not -L, the block size may appear
1666eda14cbcSMatt Macy 	 * to increase.  i.e. we may have a smaller block size for
1667eda14cbcSMatt Macy 	 * this object than what the send stream says.  In this case
1668eda14cbcSMatt Macy 	 * we need to keep the object's contents and block size
1669eda14cbcSMatt Macy 	 * intact, so that we don't lose parts of the object's
1670eda14cbcSMatt Macy 	 * contents that are not changed by this incremental send
1671eda14cbcSMatt Macy 	 * stream.
1672eda14cbcSMatt Macy 	 *
1673eda14cbcSMatt Macy 	 * We can distinguish between the two above cases by using
1674eda14cbcSMatt Macy 	 * the ZPL's generation number (see
1675eda14cbcSMatt Macy 	 * receive_object_is_same_generation()).  However, we only
1676eda14cbcSMatt Macy 	 * want to rely on the generation number when absolutely
1677eda14cbcSMatt Macy 	 * necessary, because with raw receives, the generation is
1678eda14cbcSMatt Macy 	 * encrypted.  We also want to minimize dependence on the
1679eda14cbcSMatt Macy 	 * ZPL, so that other types of datasets can also be received
1680eda14cbcSMatt Macy 	 * (e.g. ZVOLs, although note that ZVOLS currently do not
1681eda14cbcSMatt Macy 	 * reallocate their objects or change their structure).
1682eda14cbcSMatt Macy 	 * Therefore, we check a number of different cases where we
1683eda14cbcSMatt Macy 	 * know it is safe to discard the object's contents, before
1684eda14cbcSMatt Macy 	 * using the ZPL's generation number to make the above
1685eda14cbcSMatt Macy 	 * distinction.
1686eda14cbcSMatt Macy 	 */
1687eda14cbcSMatt Macy 	if (drro->drr_blksz != doi->doi_data_block_size) {
1688eda14cbcSMatt Macy 		if (rwa->raw) {
1689eda14cbcSMatt Macy 			/*
1690eda14cbcSMatt Macy 			 * RAW streams always have large blocks, so
1691eda14cbcSMatt Macy 			 * we are sure that the data is not needed
1692eda14cbcSMatt Macy 			 * due to changing --large-block to be on.
1693eda14cbcSMatt Macy 			 * Which is fortunate since the bonus buffer
1694eda14cbcSMatt Macy 			 * (which contains the ZPL generation) is
1695eda14cbcSMatt Macy 			 * encrypted, and the key might not be
1696eda14cbcSMatt Macy 			 * loaded.
1697eda14cbcSMatt Macy 			 */
1698eda14cbcSMatt Macy 			do_free_range = B_TRUE;
1699eda14cbcSMatt Macy 		} else if (rwa->full) {
1700eda14cbcSMatt Macy 			/*
1701eda14cbcSMatt Macy 			 * This is a full send stream, so it always
1702eda14cbcSMatt Macy 			 * replaces what we have.  Even if the
1703eda14cbcSMatt Macy 			 * generation numbers happen to match, this
1704eda14cbcSMatt Macy 			 * can not actually be the same logical file.
1705eda14cbcSMatt Macy 			 * This is relevant when receiving a full
1706eda14cbcSMatt Macy 			 * send as a clone.
1707eda14cbcSMatt Macy 			 */
1708eda14cbcSMatt Macy 			do_free_range = B_TRUE;
1709eda14cbcSMatt Macy 		} else if (drro->drr_type !=
1710eda14cbcSMatt Macy 		    DMU_OT_PLAIN_FILE_CONTENTS ||
1711eda14cbcSMatt Macy 		    doi->doi_type != DMU_OT_PLAIN_FILE_CONTENTS) {
1712eda14cbcSMatt Macy 			/*
1713eda14cbcSMatt Macy 			 * PLAIN_FILE_CONTENTS are the only type of
1714eda14cbcSMatt Macy 			 * objects that have ever been stored with
1715eda14cbcSMatt Macy 			 * large blocks, so we don't need the special
1716eda14cbcSMatt Macy 			 * logic below.  ZAP blocks can shrink (when
1717eda14cbcSMatt Macy 			 * there's only one block), so we don't want
1718eda14cbcSMatt Macy 			 * to hit the error below about block size
1719eda14cbcSMatt Macy 			 * only increasing.
1720eda14cbcSMatt Macy 			 */
1721eda14cbcSMatt Macy 			do_free_range = B_TRUE;
1722eda14cbcSMatt Macy 		} else if (doi->doi_max_offset <=
1723eda14cbcSMatt Macy 		    doi->doi_data_block_size) {
1724eda14cbcSMatt Macy 			/*
1725eda14cbcSMatt Macy 			 * There is only one block.  We can free it,
1726eda14cbcSMatt Macy 			 * because its contents will be replaced by a
1727eda14cbcSMatt Macy 			 * WRITE record.  This can not be the no-L ->
1728eda14cbcSMatt Macy 			 * -L case, because the no-L case would have
1729eda14cbcSMatt Macy 			 * resulted in multiple blocks.  If we
1730eda14cbcSMatt Macy 			 * supported -L -> no-L, it would not be safe
1731eda14cbcSMatt Macy 			 * to free the file's contents.  Fortunately,
1732eda14cbcSMatt Macy 			 * that is not allowed (see
1733eda14cbcSMatt Macy 			 * recv_check_large_blocks()).
1734eda14cbcSMatt Macy 			 */
1735eda14cbcSMatt Macy 			do_free_range = B_TRUE;
1736eda14cbcSMatt Macy 		} else {
1737eda14cbcSMatt Macy 			boolean_t is_same_gen;
1738eda14cbcSMatt Macy 			err = receive_object_is_same_generation(rwa->os,
1739eda14cbcSMatt Macy 			    drro->drr_object, doi->doi_bonus_type,
1740eda14cbcSMatt Macy 			    drro->drr_bonustype, bonus_data, &is_same_gen);
1741eda14cbcSMatt Macy 			if (err != 0)
1742eda14cbcSMatt Macy 				return (SET_ERROR(EINVAL));
1743eda14cbcSMatt Macy 
1744eda14cbcSMatt Macy 			if (is_same_gen) {
1745eda14cbcSMatt Macy 				/*
1746eda14cbcSMatt Macy 				 * This is the same logical file, and
1747eda14cbcSMatt Macy 				 * the block size must be increasing.
1748eda14cbcSMatt Macy 				 * It could only decrease if
1749eda14cbcSMatt Macy 				 * --large-block was changed to be
1750eda14cbcSMatt Macy 				 * off, which is checked in
1751eda14cbcSMatt Macy 				 * recv_check_large_blocks().
1752eda14cbcSMatt Macy 				 */
1753eda14cbcSMatt Macy 				if (drro->drr_blksz <=
1754eda14cbcSMatt Macy 				    doi->doi_data_block_size)
1755eda14cbcSMatt Macy 					return (SET_ERROR(EINVAL));
1756eda14cbcSMatt Macy 				/*
1757eda14cbcSMatt Macy 				 * We keep the existing blocksize and
1758eda14cbcSMatt Macy 				 * contents.
1759eda14cbcSMatt Macy 				 */
1760eda14cbcSMatt Macy 				*new_blksz =
1761eda14cbcSMatt Macy 				    doi->doi_data_block_size;
1762eda14cbcSMatt Macy 			} else {
1763eda14cbcSMatt Macy 				do_free_range = B_TRUE;
1764eda14cbcSMatt Macy 			}
1765eda14cbcSMatt Macy 		}
1766eda14cbcSMatt Macy 	}
1767eda14cbcSMatt Macy 
1768eda14cbcSMatt Macy 	/* nblkptr can only decrease if the object was reallocated */
1769eda14cbcSMatt Macy 	if (nblkptr < doi->doi_nblkptr)
1770eda14cbcSMatt Macy 		do_free_range = B_TRUE;
1771eda14cbcSMatt Macy 
1772eda14cbcSMatt Macy 	/* number of slots can only change on reallocation */
1773eda14cbcSMatt Macy 	if (dn_slots != doi->doi_dnodesize >> DNODE_SHIFT)
1774eda14cbcSMatt Macy 		do_free_range = B_TRUE;
1775eda14cbcSMatt Macy 
1776eda14cbcSMatt Macy 	/*
1777eda14cbcSMatt Macy 	 * For raw sends we also check a few other fields to
1778eda14cbcSMatt Macy 	 * ensure we are preserving the objset structure exactly
1779eda14cbcSMatt Macy 	 * as it was on the receive side:
1780eda14cbcSMatt Macy 	 *     - A changed indirect block size
1781eda14cbcSMatt Macy 	 *     - A smaller nlevels
1782eda14cbcSMatt Macy 	 */
1783eda14cbcSMatt Macy 	if (rwa->raw) {
1784eda14cbcSMatt Macy 		if (indblksz != doi->doi_metadata_block_size)
1785eda14cbcSMatt Macy 			do_free_range = B_TRUE;
1786eda14cbcSMatt Macy 		if (drro->drr_nlevels < doi->doi_indirection)
1787eda14cbcSMatt Macy 			do_free_range = B_TRUE;
1788eda14cbcSMatt Macy 	}
1789eda14cbcSMatt Macy 
1790eda14cbcSMatt Macy 	if (do_free_range) {
1791eda14cbcSMatt Macy 		err = dmu_free_long_range(rwa->os, drro->drr_object,
1792eda14cbcSMatt Macy 		    0, DMU_OBJECT_END);
1793eda14cbcSMatt Macy 		if (err != 0)
1794eda14cbcSMatt Macy 			return (SET_ERROR(EINVAL));
1795eda14cbcSMatt Macy 	}
1796eda14cbcSMatt Macy 
1797eda14cbcSMatt Macy 	/*
1798eda14cbcSMatt Macy 	 * The dmu does not currently support decreasing nlevels
1799eda14cbcSMatt Macy 	 * or changing the number of dnode slots on an object. For
1800eda14cbcSMatt Macy 	 * non-raw sends, this does not matter and the new object
1801eda14cbcSMatt Macy 	 * can just use the previous one's nlevels. For raw sends,
1802eda14cbcSMatt Macy 	 * however, the structure of the received dnode (including
1803eda14cbcSMatt Macy 	 * nlevels and dnode slots) must match that of the send
1804eda14cbcSMatt Macy 	 * side. Therefore, instead of using dmu_object_reclaim(),
1805eda14cbcSMatt Macy 	 * we must free the object completely and call
1806eda14cbcSMatt Macy 	 * dmu_object_claim_dnsize() instead.
1807eda14cbcSMatt Macy 	 */
1808eda14cbcSMatt Macy 	if ((rwa->raw && drro->drr_nlevels < doi->doi_indirection) ||
1809eda14cbcSMatt Macy 	    dn_slots != doi->doi_dnodesize >> DNODE_SHIFT) {
1810eda14cbcSMatt Macy 		err = dmu_free_long_object(rwa->os, drro->drr_object);
1811eda14cbcSMatt Macy 		if (err != 0)
1812eda14cbcSMatt Macy 			return (SET_ERROR(EINVAL));
1813eda14cbcSMatt Macy 
1814eda14cbcSMatt Macy 		txg_wait_synced(dmu_objset_pool(rwa->os), 0);
1815eda14cbcSMatt Macy 		*object_to_hold = DMU_NEW_OBJECT;
1816eda14cbcSMatt Macy 	}
1817eda14cbcSMatt Macy 
1818eda14cbcSMatt Macy 	/*
1819eda14cbcSMatt Macy 	 * For raw receives, free everything beyond the new incoming
1820eda14cbcSMatt Macy 	 * maxblkid. Normally this would be done with a DRR_FREE
1821eda14cbcSMatt Macy 	 * record that would come after this DRR_OBJECT record is
1822eda14cbcSMatt Macy 	 * processed. However, for raw receives we manually set the
1823eda14cbcSMatt Macy 	 * maxblkid from the drr_maxblkid and so we must first free
1824eda14cbcSMatt Macy 	 * everything above that blkid to ensure the DMU is always
1825eda14cbcSMatt Macy 	 * consistent with itself. We will never free the first block
1826eda14cbcSMatt Macy 	 * of the object here because a maxblkid of 0 could indicate
1827eda14cbcSMatt Macy 	 * an object with a single block or one with no blocks. This
1828eda14cbcSMatt Macy 	 * free may be skipped when dmu_free_long_range() was called
1829eda14cbcSMatt Macy 	 * above since it covers the entire object's contents.
1830eda14cbcSMatt Macy 	 */
1831eda14cbcSMatt Macy 	if (rwa->raw && *object_to_hold != DMU_NEW_OBJECT && !do_free_range) {
1832eda14cbcSMatt Macy 		err = dmu_free_long_range(rwa->os, drro->drr_object,
1833eda14cbcSMatt Macy 		    (drro->drr_maxblkid + 1) * doi->doi_data_block_size,
1834eda14cbcSMatt Macy 		    DMU_OBJECT_END);
1835eda14cbcSMatt Macy 		if (err != 0)
1836eda14cbcSMatt Macy 			return (SET_ERROR(EINVAL));
1837eda14cbcSMatt Macy 	}
1838eda14cbcSMatt Macy 	return (0);
1839eda14cbcSMatt Macy }
1840eda14cbcSMatt Macy 
1841eda14cbcSMatt Macy noinline static int
1842eda14cbcSMatt Macy receive_object(struct receive_writer_arg *rwa, struct drr_object *drro,
1843eda14cbcSMatt Macy     void *data)
1844eda14cbcSMatt Macy {
1845eda14cbcSMatt Macy 	dmu_object_info_t doi;
1846eda14cbcSMatt Macy 	dmu_tx_t *tx;
1847eda14cbcSMatt Macy 	int err;
1848eda14cbcSMatt Macy 	uint32_t new_blksz = drro->drr_blksz;
1849eda14cbcSMatt Macy 	uint8_t dn_slots = drro->drr_dn_slots != 0 ?
1850eda14cbcSMatt Macy 	    drro->drr_dn_slots : DNODE_MIN_SLOTS;
1851eda14cbcSMatt Macy 
1852eda14cbcSMatt Macy 	if (drro->drr_type == DMU_OT_NONE ||
1853eda14cbcSMatt Macy 	    !DMU_OT_IS_VALID(drro->drr_type) ||
1854eda14cbcSMatt Macy 	    !DMU_OT_IS_VALID(drro->drr_bonustype) ||
1855eda14cbcSMatt Macy 	    drro->drr_checksumtype >= ZIO_CHECKSUM_FUNCTIONS ||
1856eda14cbcSMatt Macy 	    drro->drr_compress >= ZIO_COMPRESS_FUNCTIONS ||
1857eda14cbcSMatt Macy 	    P2PHASE(drro->drr_blksz, SPA_MINBLOCKSIZE) ||
1858eda14cbcSMatt Macy 	    drro->drr_blksz < SPA_MINBLOCKSIZE ||
1859eda14cbcSMatt Macy 	    drro->drr_blksz > spa_maxblocksize(dmu_objset_spa(rwa->os)) ||
1860eda14cbcSMatt Macy 	    drro->drr_bonuslen >
1861eda14cbcSMatt Macy 	    DN_BONUS_SIZE(spa_maxdnodesize(dmu_objset_spa(rwa->os))) ||
1862eda14cbcSMatt Macy 	    dn_slots >
1863eda14cbcSMatt Macy 	    (spa_maxdnodesize(dmu_objset_spa(rwa->os)) >> DNODE_SHIFT)) {
1864eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
1865eda14cbcSMatt Macy 	}
1866eda14cbcSMatt Macy 
1867eda14cbcSMatt Macy 	if (rwa->raw) {
1868eda14cbcSMatt Macy 		/*
1869eda14cbcSMatt Macy 		 * We should have received a DRR_OBJECT_RANGE record
1870eda14cbcSMatt Macy 		 * containing this block and stored it in rwa.
1871eda14cbcSMatt Macy 		 */
1872eda14cbcSMatt Macy 		if (drro->drr_object < rwa->or_firstobj ||
1873eda14cbcSMatt Macy 		    drro->drr_object >= rwa->or_firstobj + rwa->or_numslots ||
1874eda14cbcSMatt Macy 		    drro->drr_raw_bonuslen < drro->drr_bonuslen ||
1875eda14cbcSMatt Macy 		    drro->drr_indblkshift > SPA_MAXBLOCKSHIFT ||
1876eda14cbcSMatt Macy 		    drro->drr_nlevels > DN_MAX_LEVELS ||
1877eda14cbcSMatt Macy 		    drro->drr_nblkptr > DN_MAX_NBLKPTR ||
1878eda14cbcSMatt Macy 		    DN_SLOTS_TO_BONUSLEN(dn_slots) <
1879eda14cbcSMatt Macy 		    drro->drr_raw_bonuslen)
1880eda14cbcSMatt Macy 			return (SET_ERROR(EINVAL));
1881eda14cbcSMatt Macy 	} else {
1882eda14cbcSMatt Macy 		/*
1883eda14cbcSMatt Macy 		 * The DRR_OBJECT_SPILL flag is valid when the DRR_BEGIN
1884eda14cbcSMatt Macy 		 * record indicates this by setting DRR_FLAG_SPILL_BLOCK.
1885eda14cbcSMatt Macy 		 */
1886eda14cbcSMatt Macy 		if (((drro->drr_flags & ~(DRR_OBJECT_SPILL))) ||
1887eda14cbcSMatt Macy 		    (!rwa->spill && DRR_OBJECT_HAS_SPILL(drro->drr_flags))) {
1888eda14cbcSMatt Macy 			return (SET_ERROR(EINVAL));
1889eda14cbcSMatt Macy 		}
1890eda14cbcSMatt Macy 
1891eda14cbcSMatt Macy 		if (drro->drr_raw_bonuslen != 0 || drro->drr_nblkptr != 0 ||
1892eda14cbcSMatt Macy 		    drro->drr_indblkshift != 0 || drro->drr_nlevels != 0) {
1893eda14cbcSMatt Macy 			return (SET_ERROR(EINVAL));
1894eda14cbcSMatt Macy 		}
1895eda14cbcSMatt Macy 	}
1896eda14cbcSMatt Macy 
1897eda14cbcSMatt Macy 	err = dmu_object_info(rwa->os, drro->drr_object, &doi);
1898eda14cbcSMatt Macy 
1899eda14cbcSMatt Macy 	if (err != 0 && err != ENOENT && err != EEXIST)
1900eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
1901eda14cbcSMatt Macy 
1902eda14cbcSMatt Macy 	if (drro->drr_object > rwa->max_object)
1903eda14cbcSMatt Macy 		rwa->max_object = drro->drr_object;
1904eda14cbcSMatt Macy 
1905eda14cbcSMatt Macy 	/*
1906eda14cbcSMatt Macy 	 * If we are losing blkptrs or changing the block size this must
1907eda14cbcSMatt Macy 	 * be a new file instance.  We must clear out the previous file
1908eda14cbcSMatt Macy 	 * contents before we can change this type of metadata in the dnode.
1909eda14cbcSMatt Macy 	 * Raw receives will also check that the indirect structure of the
1910eda14cbcSMatt Macy 	 * dnode hasn't changed.
1911eda14cbcSMatt Macy 	 */
1912eda14cbcSMatt Macy 	uint64_t object_to_hold;
1913eda14cbcSMatt Macy 	if (err == 0) {
1914eda14cbcSMatt Macy 		err = receive_handle_existing_object(rwa, drro, &doi, data,
1915eda14cbcSMatt Macy 		    &object_to_hold, &new_blksz);
1916be181ee2SMartin Matuska 		if (err != 0)
1917be181ee2SMartin Matuska 			return (err);
1918eda14cbcSMatt Macy 	} else if (err == EEXIST) {
1919eda14cbcSMatt Macy 		/*
1920eda14cbcSMatt Macy 		 * The object requested is currently an interior slot of a
1921eda14cbcSMatt Macy 		 * multi-slot dnode. This will be resolved when the next txg
1922eda14cbcSMatt Macy 		 * is synced out, since the send stream will have told us
1923eda14cbcSMatt Macy 		 * to free this slot when we freed the associated dnode
1924eda14cbcSMatt Macy 		 * earlier in the stream.
1925eda14cbcSMatt Macy 		 */
1926eda14cbcSMatt Macy 		txg_wait_synced(dmu_objset_pool(rwa->os), 0);
1927eda14cbcSMatt Macy 
1928eda14cbcSMatt Macy 		if (dmu_object_info(rwa->os, drro->drr_object, NULL) != ENOENT)
1929eda14cbcSMatt Macy 			return (SET_ERROR(EINVAL));
1930eda14cbcSMatt Macy 
1931eda14cbcSMatt Macy 		/* object was freed and we are about to allocate a new one */
1932eda14cbcSMatt Macy 		object_to_hold = DMU_NEW_OBJECT;
1933eda14cbcSMatt Macy 	} else {
193415f0b8c3SMartin Matuska 		/*
193515f0b8c3SMartin Matuska 		 * If the only record in this range so far was DRR_FREEOBJECTS
193615f0b8c3SMartin Matuska 		 * with at least one actually freed object, it's possible that
193715f0b8c3SMartin Matuska 		 * the block will now be converted to a hole. We need to wait
193815f0b8c3SMartin Matuska 		 * for the txg to sync to prevent races.
193915f0b8c3SMartin Matuska 		 */
194015f0b8c3SMartin Matuska 		if (rwa->or_need_sync == ORNS_YES)
194115f0b8c3SMartin Matuska 			txg_wait_synced(dmu_objset_pool(rwa->os), 0);
194215f0b8c3SMartin Matuska 
1943eda14cbcSMatt Macy 		/* object is free and we are about to allocate a new one */
1944eda14cbcSMatt Macy 		object_to_hold = DMU_NEW_OBJECT;
1945eda14cbcSMatt Macy 	}
1946eda14cbcSMatt Macy 
194715f0b8c3SMartin Matuska 	/* Only relevant for the first object in the range */
194815f0b8c3SMartin Matuska 	rwa->or_need_sync = ORNS_NO;
194915f0b8c3SMartin Matuska 
1950eda14cbcSMatt Macy 	/*
1951eda14cbcSMatt Macy 	 * If this is a multi-slot dnode there is a chance that this
1952eda14cbcSMatt Macy 	 * object will expand into a slot that is already used by
1953eda14cbcSMatt Macy 	 * another object from the previous snapshot. We must free
1954eda14cbcSMatt Macy 	 * these objects before we attempt to allocate the new dnode.
1955eda14cbcSMatt Macy 	 */
1956eda14cbcSMatt Macy 	if (dn_slots > 1) {
1957eda14cbcSMatt Macy 		boolean_t need_sync = B_FALSE;
1958eda14cbcSMatt Macy 
1959eda14cbcSMatt Macy 		for (uint64_t slot = drro->drr_object + 1;
1960eda14cbcSMatt Macy 		    slot < drro->drr_object + dn_slots;
1961eda14cbcSMatt Macy 		    slot++) {
1962eda14cbcSMatt Macy 			dmu_object_info_t slot_doi;
1963eda14cbcSMatt Macy 
1964eda14cbcSMatt Macy 			err = dmu_object_info(rwa->os, slot, &slot_doi);
1965eda14cbcSMatt Macy 			if (err == ENOENT || err == EEXIST)
1966eda14cbcSMatt Macy 				continue;
1967eda14cbcSMatt Macy 			else if (err != 0)
1968eda14cbcSMatt Macy 				return (err);
1969eda14cbcSMatt Macy 
1970eda14cbcSMatt Macy 			err = dmu_free_long_object(rwa->os, slot);
1971eda14cbcSMatt Macy 			if (err != 0)
1972eda14cbcSMatt Macy 				return (err);
1973eda14cbcSMatt Macy 
1974eda14cbcSMatt Macy 			need_sync = B_TRUE;
1975eda14cbcSMatt Macy 		}
1976eda14cbcSMatt Macy 
1977eda14cbcSMatt Macy 		if (need_sync)
1978eda14cbcSMatt Macy 			txg_wait_synced(dmu_objset_pool(rwa->os), 0);
1979eda14cbcSMatt Macy 	}
1980eda14cbcSMatt Macy 
1981eda14cbcSMatt Macy 	tx = dmu_tx_create(rwa->os);
1982eda14cbcSMatt Macy 	dmu_tx_hold_bonus(tx, object_to_hold);
1983eda14cbcSMatt Macy 	dmu_tx_hold_write(tx, object_to_hold, 0, 0);
1984eda14cbcSMatt Macy 	err = dmu_tx_assign(tx, TXG_WAIT);
1985eda14cbcSMatt Macy 	if (err != 0) {
1986eda14cbcSMatt Macy 		dmu_tx_abort(tx);
1987eda14cbcSMatt Macy 		return (err);
1988eda14cbcSMatt Macy 	}
1989eda14cbcSMatt Macy 
1990eda14cbcSMatt Macy 	if (object_to_hold == DMU_NEW_OBJECT) {
1991eda14cbcSMatt Macy 		/* Currently free, wants to be allocated */
1992eda14cbcSMatt Macy 		err = dmu_object_claim_dnsize(rwa->os, drro->drr_object,
1993eda14cbcSMatt Macy 		    drro->drr_type, new_blksz,
1994eda14cbcSMatt Macy 		    drro->drr_bonustype, drro->drr_bonuslen,
1995eda14cbcSMatt Macy 		    dn_slots << DNODE_SHIFT, tx);
1996eda14cbcSMatt Macy 	} else if (drro->drr_type != doi.doi_type ||
1997eda14cbcSMatt Macy 	    new_blksz != doi.doi_data_block_size ||
1998eda14cbcSMatt Macy 	    drro->drr_bonustype != doi.doi_bonus_type ||
1999eda14cbcSMatt Macy 	    drro->drr_bonuslen != doi.doi_bonus_size) {
2000eda14cbcSMatt Macy 		/* Currently allocated, but with different properties */
2001eda14cbcSMatt Macy 		err = dmu_object_reclaim_dnsize(rwa->os, drro->drr_object,
2002eda14cbcSMatt Macy 		    drro->drr_type, new_blksz,
2003eda14cbcSMatt Macy 		    drro->drr_bonustype, drro->drr_bonuslen,
2004eda14cbcSMatt Macy 		    dn_slots << DNODE_SHIFT, rwa->spill ?
2005eda14cbcSMatt Macy 		    DRR_OBJECT_HAS_SPILL(drro->drr_flags) : B_FALSE, tx);
2006eda14cbcSMatt Macy 	} else if (rwa->spill && !DRR_OBJECT_HAS_SPILL(drro->drr_flags)) {
2007eda14cbcSMatt Macy 		/*
2008eda14cbcSMatt Macy 		 * Currently allocated, the existing version of this object
2009eda14cbcSMatt Macy 		 * may reference a spill block that is no longer allocated
2010eda14cbcSMatt Macy 		 * at the source and needs to be freed.
2011eda14cbcSMatt Macy 		 */
2012eda14cbcSMatt Macy 		err = dmu_object_rm_spill(rwa->os, drro->drr_object, tx);
2013eda14cbcSMatt Macy 	}
2014eda14cbcSMatt Macy 
2015eda14cbcSMatt Macy 	if (err != 0) {
2016eda14cbcSMatt Macy 		dmu_tx_commit(tx);
2017eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
2018eda14cbcSMatt Macy 	}
2019eda14cbcSMatt Macy 
2020eda14cbcSMatt Macy 	if (rwa->or_crypt_params_present) {
2021eda14cbcSMatt Macy 		/*
2022eda14cbcSMatt Macy 		 * Set the crypt params for the buffer associated with this
2023eda14cbcSMatt Macy 		 * range of dnodes.  This causes the blkptr_t to have the
2024eda14cbcSMatt Macy 		 * same crypt params (byteorder, salt, iv, mac) as on the
2025eda14cbcSMatt Macy 		 * sending side.
2026eda14cbcSMatt Macy 		 *
2027eda14cbcSMatt Macy 		 * Since we are committing this tx now, it is possible for
2028eda14cbcSMatt Macy 		 * the dnode block to end up on-disk with the incorrect MAC,
2029eda14cbcSMatt Macy 		 * if subsequent objects in this block are received in a
2030eda14cbcSMatt Macy 		 * different txg.  However, since the dataset is marked as
2031eda14cbcSMatt Macy 		 * inconsistent, no code paths will do a non-raw read (or
2032eda14cbcSMatt Macy 		 * decrypt the block / verify the MAC). The receive code and
2033eda14cbcSMatt Macy 		 * scrub code can safely do raw reads and verify the
2034eda14cbcSMatt Macy 		 * checksum.  They don't need to verify the MAC.
2035eda14cbcSMatt Macy 		 */
2036eda14cbcSMatt Macy 		dmu_buf_t *db = NULL;
2037eda14cbcSMatt Macy 		uint64_t offset = rwa->or_firstobj * DNODE_MIN_SIZE;
2038eda14cbcSMatt Macy 
2039eda14cbcSMatt Macy 		err = dmu_buf_hold_by_dnode(DMU_META_DNODE(rwa->os),
2040eda14cbcSMatt Macy 		    offset, FTAG, &db, DMU_READ_PREFETCH | DMU_READ_NO_DECRYPT);
2041eda14cbcSMatt Macy 		if (err != 0) {
2042eda14cbcSMatt Macy 			dmu_tx_commit(tx);
2043eda14cbcSMatt Macy 			return (SET_ERROR(EINVAL));
2044eda14cbcSMatt Macy 		}
2045eda14cbcSMatt Macy 
2046eda14cbcSMatt Macy 		dmu_buf_set_crypt_params(db, rwa->or_byteorder,
2047eda14cbcSMatt Macy 		    rwa->or_salt, rwa->or_iv, rwa->or_mac, tx);
2048eda14cbcSMatt Macy 
2049eda14cbcSMatt Macy 		dmu_buf_rele(db, FTAG);
2050eda14cbcSMatt Macy 
2051eda14cbcSMatt Macy 		rwa->or_crypt_params_present = B_FALSE;
2052eda14cbcSMatt Macy 	}
2053eda14cbcSMatt Macy 
2054eda14cbcSMatt Macy 	dmu_object_set_checksum(rwa->os, drro->drr_object,
2055eda14cbcSMatt Macy 	    drro->drr_checksumtype, tx);
2056eda14cbcSMatt Macy 	dmu_object_set_compress(rwa->os, drro->drr_object,
2057eda14cbcSMatt Macy 	    drro->drr_compress, tx);
2058eda14cbcSMatt Macy 
2059eda14cbcSMatt Macy 	/* handle more restrictive dnode structuring for raw recvs */
2060eda14cbcSMatt Macy 	if (rwa->raw) {
2061eda14cbcSMatt Macy 		/*
2062eda14cbcSMatt Macy 		 * Set the indirect block size, block shift, nlevels.
2063eda14cbcSMatt Macy 		 * This will not fail because we ensured all of the
2064eda14cbcSMatt Macy 		 * blocks were freed earlier if this is a new object.
2065eda14cbcSMatt Macy 		 * For non-new objects block size and indirect block
2066eda14cbcSMatt Macy 		 * shift cannot change and nlevels can only increase.
2067eda14cbcSMatt Macy 		 */
2068eda14cbcSMatt Macy 		ASSERT3U(new_blksz, ==, drro->drr_blksz);
2069eda14cbcSMatt Macy 		VERIFY0(dmu_object_set_blocksize(rwa->os, drro->drr_object,
2070eda14cbcSMatt Macy 		    drro->drr_blksz, drro->drr_indblkshift, tx));
2071eda14cbcSMatt Macy 		VERIFY0(dmu_object_set_nlevels(rwa->os, drro->drr_object,
2072eda14cbcSMatt Macy 		    drro->drr_nlevels, tx));
2073eda14cbcSMatt Macy 
2074eda14cbcSMatt Macy 		/*
2075eda14cbcSMatt Macy 		 * Set the maxblkid. This will always succeed because
2076eda14cbcSMatt Macy 		 * we freed all blocks beyond the new maxblkid above.
2077eda14cbcSMatt Macy 		 */
2078eda14cbcSMatt Macy 		VERIFY0(dmu_object_set_maxblkid(rwa->os, drro->drr_object,
2079eda14cbcSMatt Macy 		    drro->drr_maxblkid, tx));
2080eda14cbcSMatt Macy 	}
2081eda14cbcSMatt Macy 
2082eda14cbcSMatt Macy 	if (data != NULL) {
2083eda14cbcSMatt Macy 		dmu_buf_t *db;
2084eda14cbcSMatt Macy 		dnode_t *dn;
2085eda14cbcSMatt Macy 		uint32_t flags = DMU_READ_NO_PREFETCH;
2086eda14cbcSMatt Macy 
2087eda14cbcSMatt Macy 		if (rwa->raw)
2088eda14cbcSMatt Macy 			flags |= DMU_READ_NO_DECRYPT;
2089eda14cbcSMatt Macy 
2090eda14cbcSMatt Macy 		VERIFY0(dnode_hold(rwa->os, drro->drr_object, FTAG, &dn));
2091eda14cbcSMatt Macy 		VERIFY0(dmu_bonus_hold_by_dnode(dn, FTAG, &db, flags));
2092eda14cbcSMatt Macy 
2093eda14cbcSMatt Macy 		dmu_buf_will_dirty(db, tx);
2094eda14cbcSMatt Macy 
2095eda14cbcSMatt Macy 		ASSERT3U(db->db_size, >=, drro->drr_bonuslen);
2096da5137abSMartin Matuska 		memcpy(db->db_data, data, DRR_OBJECT_PAYLOAD_SIZE(drro));
2097eda14cbcSMatt Macy 
2098eda14cbcSMatt Macy 		/*
2099eda14cbcSMatt Macy 		 * Raw bonus buffers have their byteorder determined by the
2100eda14cbcSMatt Macy 		 * DRR_OBJECT_RANGE record.
2101eda14cbcSMatt Macy 		 */
2102eda14cbcSMatt Macy 		if (rwa->byteswap && !rwa->raw) {
2103eda14cbcSMatt Macy 			dmu_object_byteswap_t byteswap =
2104eda14cbcSMatt Macy 			    DMU_OT_BYTESWAP(drro->drr_bonustype);
2105eda14cbcSMatt Macy 			dmu_ot_byteswap[byteswap].ob_func(db->db_data,
2106eda14cbcSMatt Macy 			    DRR_OBJECT_PAYLOAD_SIZE(drro));
2107eda14cbcSMatt Macy 		}
2108eda14cbcSMatt Macy 		dmu_buf_rele(db, FTAG);
2109eda14cbcSMatt Macy 		dnode_rele(dn, FTAG);
2110eda14cbcSMatt Macy 	}
2111eda14cbcSMatt Macy 	dmu_tx_commit(tx);
2112eda14cbcSMatt Macy 
2113eda14cbcSMatt Macy 	return (0);
2114eda14cbcSMatt Macy }
2115eda14cbcSMatt Macy 
2116eda14cbcSMatt Macy noinline static int
2117eda14cbcSMatt Macy receive_freeobjects(struct receive_writer_arg *rwa,
2118eda14cbcSMatt Macy     struct drr_freeobjects *drrfo)
2119eda14cbcSMatt Macy {
2120eda14cbcSMatt Macy 	uint64_t obj;
2121eda14cbcSMatt Macy 	int next_err = 0;
2122eda14cbcSMatt Macy 
2123eda14cbcSMatt Macy 	if (drrfo->drr_firstobj + drrfo->drr_numobjs < drrfo->drr_firstobj)
2124eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
2125eda14cbcSMatt Macy 
2126eda14cbcSMatt Macy 	for (obj = drrfo->drr_firstobj == 0 ? 1 : drrfo->drr_firstobj;
2127eda14cbcSMatt Macy 	    obj < drrfo->drr_firstobj + drrfo->drr_numobjs &&
2128eda14cbcSMatt Macy 	    obj < DN_MAX_OBJECT && next_err == 0;
2129eda14cbcSMatt Macy 	    next_err = dmu_object_next(rwa->os, &obj, FALSE, 0)) {
2130eda14cbcSMatt Macy 		dmu_object_info_t doi;
2131eda14cbcSMatt Macy 		int err;
2132eda14cbcSMatt Macy 
2133eda14cbcSMatt Macy 		err = dmu_object_info(rwa->os, obj, &doi);
2134eda14cbcSMatt Macy 		if (err == ENOENT)
2135eda14cbcSMatt Macy 			continue;
2136eda14cbcSMatt Macy 		else if (err != 0)
2137eda14cbcSMatt Macy 			return (err);
2138eda14cbcSMatt Macy 
2139eda14cbcSMatt Macy 		err = dmu_free_long_object(rwa->os, obj);
2140eda14cbcSMatt Macy 
2141eda14cbcSMatt Macy 		if (err != 0)
2142eda14cbcSMatt Macy 			return (err);
214315f0b8c3SMartin Matuska 
214415f0b8c3SMartin Matuska 		if (rwa->or_need_sync == ORNS_MAYBE)
214515f0b8c3SMartin Matuska 			rwa->or_need_sync = ORNS_YES;
2146eda14cbcSMatt Macy 	}
2147eda14cbcSMatt Macy 	if (next_err != ESRCH)
2148eda14cbcSMatt Macy 		return (next_err);
2149eda14cbcSMatt Macy 	return (0);
2150eda14cbcSMatt Macy }
2151eda14cbcSMatt Macy 
2152eda14cbcSMatt Macy /*
2153eda14cbcSMatt Macy  * Note: if this fails, the caller will clean up any records left on the
2154eda14cbcSMatt Macy  * rwa->write_batch list.
2155eda14cbcSMatt Macy  */
2156eda14cbcSMatt Macy static int
2157eda14cbcSMatt Macy flush_write_batch_impl(struct receive_writer_arg *rwa)
2158eda14cbcSMatt Macy {
2159eda14cbcSMatt Macy 	dnode_t *dn;
2160eda14cbcSMatt Macy 	int err;
2161eda14cbcSMatt Macy 
2162eda14cbcSMatt Macy 	if (dnode_hold(rwa->os, rwa->last_object, FTAG, &dn) != 0)
2163eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
2164eda14cbcSMatt Macy 
2165eda14cbcSMatt Macy 	struct receive_record_arg *last_rrd = list_tail(&rwa->write_batch);
2166eda14cbcSMatt Macy 	struct drr_write *last_drrw = &last_rrd->header.drr_u.drr_write;
2167eda14cbcSMatt Macy 
2168eda14cbcSMatt Macy 	struct receive_record_arg *first_rrd = list_head(&rwa->write_batch);
2169eda14cbcSMatt Macy 	struct drr_write *first_drrw = &first_rrd->header.drr_u.drr_write;
2170eda14cbcSMatt Macy 
2171eda14cbcSMatt Macy 	ASSERT3U(rwa->last_object, ==, last_drrw->drr_object);
2172eda14cbcSMatt Macy 	ASSERT3U(rwa->last_offset, ==, last_drrw->drr_offset);
2173eda14cbcSMatt Macy 
2174eda14cbcSMatt Macy 	dmu_tx_t *tx = dmu_tx_create(rwa->os);
2175eda14cbcSMatt Macy 	dmu_tx_hold_write_by_dnode(tx, dn, first_drrw->drr_offset,
2176eda14cbcSMatt Macy 	    last_drrw->drr_offset - first_drrw->drr_offset +
2177eda14cbcSMatt Macy 	    last_drrw->drr_logical_size);
2178eda14cbcSMatt Macy 	err = dmu_tx_assign(tx, TXG_WAIT);
2179eda14cbcSMatt Macy 	if (err != 0) {
2180eda14cbcSMatt Macy 		dmu_tx_abort(tx);
2181eda14cbcSMatt Macy 		dnode_rele(dn, FTAG);
2182eda14cbcSMatt Macy 		return (err);
2183eda14cbcSMatt Macy 	}
2184eda14cbcSMatt Macy 
2185eda14cbcSMatt Macy 	struct receive_record_arg *rrd;
2186eda14cbcSMatt Macy 	while ((rrd = list_head(&rwa->write_batch)) != NULL) {
2187eda14cbcSMatt Macy 		struct drr_write *drrw = &rrd->header.drr_u.drr_write;
21887877fdebSMatt Macy 		abd_t *abd = rrd->abd;
2189eda14cbcSMatt Macy 
2190eda14cbcSMatt Macy 		ASSERT3U(drrw->drr_object, ==, rwa->last_object);
2191eda14cbcSMatt Macy 
21927877fdebSMatt Macy 		if (drrw->drr_logical_size != dn->dn_datablksz) {
21937877fdebSMatt Macy 			/*
21947877fdebSMatt Macy 			 * The WRITE record is larger than the object's block
21957877fdebSMatt Macy 			 * size.  We must be receiving an incremental
21967877fdebSMatt Macy 			 * large-block stream into a dataset that previously did
21977877fdebSMatt Macy 			 * a non-large-block receive.  Lightweight writes must
21987877fdebSMatt Macy 			 * be exactly one block, so we need to decompress the
21997877fdebSMatt Macy 			 * data (if compressed) and do a normal dmu_write().
22007877fdebSMatt Macy 			 */
22017877fdebSMatt Macy 			ASSERT3U(drrw->drr_logical_size, >, dn->dn_datablksz);
22027877fdebSMatt Macy 			if (DRR_WRITE_COMPRESSED(drrw)) {
22037877fdebSMatt Macy 				abd_t *decomp_abd =
22047877fdebSMatt Macy 				    abd_alloc_linear(drrw->drr_logical_size,
22057877fdebSMatt Macy 				    B_FALSE);
22067877fdebSMatt Macy 
22077877fdebSMatt Macy 				err = zio_decompress_data(
22087877fdebSMatt Macy 				    drrw->drr_compressiontype,
22097877fdebSMatt Macy 				    abd, abd_to_buf(decomp_abd),
22107877fdebSMatt Macy 				    abd_get_size(abd),
22117877fdebSMatt Macy 				    abd_get_size(decomp_abd), NULL);
22127877fdebSMatt Macy 
22137877fdebSMatt Macy 				if (err == 0) {
22147877fdebSMatt Macy 					dmu_write_by_dnode(dn,
22157877fdebSMatt Macy 					    drrw->drr_offset,
22167877fdebSMatt Macy 					    drrw->drr_logical_size,
22177877fdebSMatt Macy 					    abd_to_buf(decomp_abd), tx);
22187877fdebSMatt Macy 				}
22197877fdebSMatt Macy 				abd_free(decomp_abd);
22207877fdebSMatt Macy 			} else {
22217877fdebSMatt Macy 				dmu_write_by_dnode(dn,
22227877fdebSMatt Macy 				    drrw->drr_offset,
22237877fdebSMatt Macy 				    drrw->drr_logical_size,
22247877fdebSMatt Macy 				    abd_to_buf(abd), tx);
22257877fdebSMatt Macy 			}
22267877fdebSMatt Macy 			if (err == 0)
22277877fdebSMatt Macy 				abd_free(abd);
22287877fdebSMatt Macy 		} else {
2229*2a58b312SMartin Matuska 			zio_prop_t zp = {0};
22307877fdebSMatt Macy 			dmu_write_policy(rwa->os, dn, 0, 0, &zp);
22317877fdebSMatt Macy 
2232dbd5678dSMartin Matuska 			zio_flag_t zio_flags = 0;
22337877fdebSMatt Macy 
22347877fdebSMatt Macy 			if (rwa->raw) {
22357877fdebSMatt Macy 				zp.zp_encrypt = B_TRUE;
22367877fdebSMatt Macy 				zp.zp_compress = drrw->drr_compressiontype;
22377877fdebSMatt Macy 				zp.zp_byteorder = ZFS_HOST_BYTEORDER ^
22387877fdebSMatt Macy 				    !!DRR_IS_RAW_BYTESWAPPED(drrw->drr_flags) ^
22397877fdebSMatt Macy 				    rwa->byteswap;
2240da5137abSMartin Matuska 				memcpy(zp.zp_salt, drrw->drr_salt,
22417877fdebSMatt Macy 				    ZIO_DATA_SALT_LEN);
2242da5137abSMartin Matuska 				memcpy(zp.zp_iv, drrw->drr_iv,
22437877fdebSMatt Macy 				    ZIO_DATA_IV_LEN);
2244da5137abSMartin Matuska 				memcpy(zp.zp_mac, drrw->drr_mac,
22457877fdebSMatt Macy 				    ZIO_DATA_MAC_LEN);
22467877fdebSMatt Macy 				if (DMU_OT_IS_ENCRYPTED(zp.zp_type)) {
22477877fdebSMatt Macy 					zp.zp_nopwrite = B_FALSE;
22487877fdebSMatt Macy 					zp.zp_copies = MIN(zp.zp_copies,
22497877fdebSMatt Macy 					    SPA_DVAS_PER_BP - 1);
22507877fdebSMatt Macy 				}
22517877fdebSMatt Macy 				zio_flags |= ZIO_FLAG_RAW;
22527877fdebSMatt Macy 			} else if (DRR_WRITE_COMPRESSED(drrw)) {
22537877fdebSMatt Macy 				ASSERT3U(drrw->drr_compressed_size, >, 0);
22547877fdebSMatt Macy 				ASSERT3U(drrw->drr_logical_size, >=,
22557877fdebSMatt Macy 				    drrw->drr_compressed_size);
22567877fdebSMatt Macy 				zp.zp_compress = drrw->drr_compressiontype;
22577877fdebSMatt Macy 				zio_flags |= ZIO_FLAG_RAW_COMPRESS;
22587877fdebSMatt Macy 			} else if (rwa->byteswap) {
22597877fdebSMatt Macy 				/*
22607877fdebSMatt Macy 				 * Note: compressed blocks never need to be
22617877fdebSMatt Macy 				 * byteswapped, because WRITE records for
22627877fdebSMatt Macy 				 * metadata blocks are never compressed. The
22637877fdebSMatt Macy 				 * exception is raw streams, which are written
22647877fdebSMatt Macy 				 * in the original byteorder, and the byteorder
22657877fdebSMatt Macy 				 * bit is preserved in the BP by setting
22667877fdebSMatt Macy 				 * zp_byteorder above.
22677877fdebSMatt Macy 				 */
2268eda14cbcSMatt Macy 				dmu_object_byteswap_t byteswap =
2269eda14cbcSMatt Macy 				    DMU_OT_BYTESWAP(drrw->drr_type);
22707877fdebSMatt Macy 				dmu_ot_byteswap[byteswap].ob_func(
22717877fdebSMatt Macy 				    abd_to_buf(abd),
2272eda14cbcSMatt Macy 				    DRR_WRITE_PAYLOAD_SIZE(drrw));
2273eda14cbcSMatt Macy 			}
2274eda14cbcSMatt Macy 
2275eda14cbcSMatt Macy 			/*
22767877fdebSMatt Macy 			 * Since this data can't be read until the receive
22777877fdebSMatt Macy 			 * completes, we can do a "lightweight" write for
22787877fdebSMatt Macy 			 * improved performance.
2279eda14cbcSMatt Macy 			 */
22807877fdebSMatt Macy 			err = dmu_lightweight_write_by_dnode(dn,
22817877fdebSMatt Macy 			    drrw->drr_offset, abd, &zp, zio_flags, tx);
2282eda14cbcSMatt Macy 		}
2283eda14cbcSMatt Macy 
2284eda14cbcSMatt Macy 		if (err != 0) {
2285eda14cbcSMatt Macy 			/*
2286eda14cbcSMatt Macy 			 * This rrd is left on the list, so the caller will
22877877fdebSMatt Macy 			 * free it (and the abd).
2288eda14cbcSMatt Macy 			 */
2289eda14cbcSMatt Macy 			break;
2290eda14cbcSMatt Macy 		}
2291eda14cbcSMatt Macy 
2292eda14cbcSMatt Macy 		/*
2293eda14cbcSMatt Macy 		 * Note: If the receive fails, we want the resume stream to
2294eda14cbcSMatt Macy 		 * start with the same record that we last successfully
2295eda14cbcSMatt Macy 		 * received (as opposed to the next record), so that we can
2296eda14cbcSMatt Macy 		 * verify that we are resuming from the correct location.
2297eda14cbcSMatt Macy 		 */
2298eda14cbcSMatt Macy 		save_resume_state(rwa, drrw->drr_object, drrw->drr_offset, tx);
2299eda14cbcSMatt Macy 
2300eda14cbcSMatt Macy 		list_remove(&rwa->write_batch, rrd);
2301eda14cbcSMatt Macy 		kmem_free(rrd, sizeof (*rrd));
2302eda14cbcSMatt Macy 	}
2303eda14cbcSMatt Macy 
2304eda14cbcSMatt Macy 	dmu_tx_commit(tx);
2305eda14cbcSMatt Macy 	dnode_rele(dn, FTAG);
2306eda14cbcSMatt Macy 	return (err);
2307eda14cbcSMatt Macy }
2308eda14cbcSMatt Macy 
2309eda14cbcSMatt Macy noinline static int
2310eda14cbcSMatt Macy flush_write_batch(struct receive_writer_arg *rwa)
2311eda14cbcSMatt Macy {
2312eda14cbcSMatt Macy 	if (list_is_empty(&rwa->write_batch))
2313eda14cbcSMatt Macy 		return (0);
2314eda14cbcSMatt Macy 	int err = rwa->err;
2315eda14cbcSMatt Macy 	if (err == 0)
2316eda14cbcSMatt Macy 		err = flush_write_batch_impl(rwa);
2317eda14cbcSMatt Macy 	if (err != 0) {
2318eda14cbcSMatt Macy 		struct receive_record_arg *rrd;
2319eda14cbcSMatt Macy 		while ((rrd = list_remove_head(&rwa->write_batch)) != NULL) {
23207877fdebSMatt Macy 			abd_free(rrd->abd);
2321eda14cbcSMatt Macy 			kmem_free(rrd, sizeof (*rrd));
2322eda14cbcSMatt Macy 		}
2323eda14cbcSMatt Macy 	}
2324eda14cbcSMatt Macy 	ASSERT(list_is_empty(&rwa->write_batch));
2325eda14cbcSMatt Macy 	return (err);
2326eda14cbcSMatt Macy }
2327eda14cbcSMatt Macy 
2328eda14cbcSMatt Macy noinline static int
2329eda14cbcSMatt Macy receive_process_write_record(struct receive_writer_arg *rwa,
2330eda14cbcSMatt Macy     struct receive_record_arg *rrd)
2331eda14cbcSMatt Macy {
2332eda14cbcSMatt Macy 	int err = 0;
2333eda14cbcSMatt Macy 
2334eda14cbcSMatt Macy 	ASSERT3U(rrd->header.drr_type, ==, DRR_WRITE);
2335eda14cbcSMatt Macy 	struct drr_write *drrw = &rrd->header.drr_u.drr_write;
2336eda14cbcSMatt Macy 
2337eda14cbcSMatt Macy 	if (drrw->drr_offset + drrw->drr_logical_size < drrw->drr_offset ||
2338eda14cbcSMatt Macy 	    !DMU_OT_IS_VALID(drrw->drr_type))
2339eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
2340eda14cbcSMatt Macy 
2341271171e0SMartin Matuska 	if (rwa->heal) {
2342271171e0SMartin Matuska 		blkptr_t *bp;
2343271171e0SMartin Matuska 		dmu_buf_t *dbp;
2344271171e0SMartin Matuska 		dnode_t *dn;
2345271171e0SMartin Matuska 		int flags = DB_RF_CANFAIL;
2346271171e0SMartin Matuska 
2347271171e0SMartin Matuska 		if (rwa->raw)
2348271171e0SMartin Matuska 			flags |= DB_RF_NO_DECRYPT;
2349271171e0SMartin Matuska 
2350271171e0SMartin Matuska 		if (rwa->byteswap) {
2351271171e0SMartin Matuska 			dmu_object_byteswap_t byteswap =
2352271171e0SMartin Matuska 			    DMU_OT_BYTESWAP(drrw->drr_type);
2353271171e0SMartin Matuska 			dmu_ot_byteswap[byteswap].ob_func(abd_to_buf(rrd->abd),
2354271171e0SMartin Matuska 			    DRR_WRITE_PAYLOAD_SIZE(drrw));
2355271171e0SMartin Matuska 		}
2356271171e0SMartin Matuska 
2357271171e0SMartin Matuska 		err = dmu_buf_hold_noread(rwa->os, drrw->drr_object,
2358271171e0SMartin Matuska 		    drrw->drr_offset, FTAG, &dbp);
2359271171e0SMartin Matuska 		if (err != 0)
2360271171e0SMartin Matuska 			return (err);
2361271171e0SMartin Matuska 
2362271171e0SMartin Matuska 		/* Try to read the object to see if it needs healing */
2363271171e0SMartin Matuska 		err = dbuf_read((dmu_buf_impl_t *)dbp, NULL, flags);
2364271171e0SMartin Matuska 		/*
2365271171e0SMartin Matuska 		 * We only try to heal when dbuf_read() returns a ECKSUMs.
2366271171e0SMartin Matuska 		 * Other errors (even EIO) get returned to caller.
2367271171e0SMartin Matuska 		 * EIO indicates that the device is not present/accessible,
2368271171e0SMartin Matuska 		 * so writing to it will likely fail.
2369271171e0SMartin Matuska 		 * If the block is healthy, we don't want to overwrite it
2370271171e0SMartin Matuska 		 * unnecessarily.
2371271171e0SMartin Matuska 		 */
2372271171e0SMartin Matuska 		if (err != ECKSUM) {
2373271171e0SMartin Matuska 			dmu_buf_rele(dbp, FTAG);
2374271171e0SMartin Matuska 			return (err);
2375271171e0SMartin Matuska 		}
2376271171e0SMartin Matuska 		dn = dmu_buf_dnode_enter(dbp);
2377271171e0SMartin Matuska 		/* Make sure the on-disk block and recv record sizes match */
2378271171e0SMartin Matuska 		if (drrw->drr_logical_size !=
2379271171e0SMartin Matuska 		    dn->dn_datablkszsec << SPA_MINBLOCKSHIFT) {
2380271171e0SMartin Matuska 			err = ENOTSUP;
2381271171e0SMartin Matuska 			dmu_buf_dnode_exit(dbp);
2382271171e0SMartin Matuska 			dmu_buf_rele(dbp, FTAG);
2383271171e0SMartin Matuska 			return (err);
2384271171e0SMartin Matuska 		}
2385271171e0SMartin Matuska 		/* Get the block pointer for the corrupted block */
2386271171e0SMartin Matuska 		bp = dmu_buf_get_blkptr(dbp);
2387271171e0SMartin Matuska 		err = do_corrective_recv(rwa, drrw, rrd, bp);
2388271171e0SMartin Matuska 		dmu_buf_dnode_exit(dbp);
2389271171e0SMartin Matuska 		dmu_buf_rele(dbp, FTAG);
2390271171e0SMartin Matuska 		return (err);
2391271171e0SMartin Matuska 	}
2392271171e0SMartin Matuska 
2393eda14cbcSMatt Macy 	/*
2394eda14cbcSMatt Macy 	 * For resuming to work, records must be in increasing order
2395eda14cbcSMatt Macy 	 * by (object, offset).
2396eda14cbcSMatt Macy 	 */
2397eda14cbcSMatt Macy 	if (drrw->drr_object < rwa->last_object ||
2398eda14cbcSMatt Macy 	    (drrw->drr_object == rwa->last_object &&
2399eda14cbcSMatt Macy 	    drrw->drr_offset < rwa->last_offset)) {
2400eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
2401eda14cbcSMatt Macy 	}
2402eda14cbcSMatt Macy 
2403eda14cbcSMatt Macy 	struct receive_record_arg *first_rrd = list_head(&rwa->write_batch);
2404eda14cbcSMatt Macy 	struct drr_write *first_drrw = &first_rrd->header.drr_u.drr_write;
2405eda14cbcSMatt Macy 	uint64_t batch_size =
2406eda14cbcSMatt Macy 	    MIN(zfs_recv_write_batch_size, DMU_MAX_ACCESS / 2);
2407eda14cbcSMatt Macy 	if (first_rrd != NULL &&
2408eda14cbcSMatt Macy 	    (drrw->drr_object != first_drrw->drr_object ||
2409eda14cbcSMatt Macy 	    drrw->drr_offset >= first_drrw->drr_offset + batch_size)) {
2410eda14cbcSMatt Macy 		err = flush_write_batch(rwa);
2411eda14cbcSMatt Macy 		if (err != 0)
2412eda14cbcSMatt Macy 			return (err);
2413eda14cbcSMatt Macy 	}
2414eda14cbcSMatt Macy 
2415eda14cbcSMatt Macy 	rwa->last_object = drrw->drr_object;
2416eda14cbcSMatt Macy 	rwa->last_offset = drrw->drr_offset;
2417eda14cbcSMatt Macy 
2418eda14cbcSMatt Macy 	if (rwa->last_object > rwa->max_object)
2419eda14cbcSMatt Macy 		rwa->max_object = rwa->last_object;
2420eda14cbcSMatt Macy 
2421eda14cbcSMatt Macy 	list_insert_tail(&rwa->write_batch, rrd);
2422eda14cbcSMatt Macy 	/*
2423eda14cbcSMatt Macy 	 * Return EAGAIN to indicate that we will use this rrd again,
2424eda14cbcSMatt Macy 	 * so the caller should not free it
2425eda14cbcSMatt Macy 	 */
2426eda14cbcSMatt Macy 	return (EAGAIN);
2427eda14cbcSMatt Macy }
2428eda14cbcSMatt Macy 
2429eda14cbcSMatt Macy static int
2430eda14cbcSMatt Macy receive_write_embedded(struct receive_writer_arg *rwa,
2431eda14cbcSMatt Macy     struct drr_write_embedded *drrwe, void *data)
2432eda14cbcSMatt Macy {
2433eda14cbcSMatt Macy 	dmu_tx_t *tx;
2434eda14cbcSMatt Macy 	int err;
2435eda14cbcSMatt Macy 
2436eda14cbcSMatt Macy 	if (drrwe->drr_offset + drrwe->drr_length < drrwe->drr_offset)
2437eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
2438eda14cbcSMatt Macy 
2439eda14cbcSMatt Macy 	if (drrwe->drr_psize > BPE_PAYLOAD_SIZE)
2440eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
2441eda14cbcSMatt Macy 
2442eda14cbcSMatt Macy 	if (drrwe->drr_etype >= NUM_BP_EMBEDDED_TYPES)
2443eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
2444eda14cbcSMatt Macy 	if (drrwe->drr_compression >= ZIO_COMPRESS_FUNCTIONS)
2445eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
2446eda14cbcSMatt Macy 	if (rwa->raw)
2447eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
2448eda14cbcSMatt Macy 
2449eda14cbcSMatt Macy 	if (drrwe->drr_object > rwa->max_object)
2450eda14cbcSMatt Macy 		rwa->max_object = drrwe->drr_object;
2451eda14cbcSMatt Macy 
2452eda14cbcSMatt Macy 	tx = dmu_tx_create(rwa->os);
2453eda14cbcSMatt Macy 
2454eda14cbcSMatt Macy 	dmu_tx_hold_write(tx, drrwe->drr_object,
2455eda14cbcSMatt Macy 	    drrwe->drr_offset, drrwe->drr_length);
2456eda14cbcSMatt Macy 	err = dmu_tx_assign(tx, TXG_WAIT);
2457eda14cbcSMatt Macy 	if (err != 0) {
2458eda14cbcSMatt Macy 		dmu_tx_abort(tx);
2459eda14cbcSMatt Macy 		return (err);
2460eda14cbcSMatt Macy 	}
2461eda14cbcSMatt Macy 
2462eda14cbcSMatt Macy 	dmu_write_embedded(rwa->os, drrwe->drr_object,
2463eda14cbcSMatt Macy 	    drrwe->drr_offset, data, drrwe->drr_etype,
2464eda14cbcSMatt Macy 	    drrwe->drr_compression, drrwe->drr_lsize, drrwe->drr_psize,
2465eda14cbcSMatt Macy 	    rwa->byteswap ^ ZFS_HOST_BYTEORDER, tx);
2466eda14cbcSMatt Macy 
2467eda14cbcSMatt Macy 	/* See comment in restore_write. */
2468eda14cbcSMatt Macy 	save_resume_state(rwa, drrwe->drr_object, drrwe->drr_offset, tx);
2469eda14cbcSMatt Macy 	dmu_tx_commit(tx);
2470eda14cbcSMatt Macy 	return (0);
2471eda14cbcSMatt Macy }
2472eda14cbcSMatt Macy 
2473eda14cbcSMatt Macy static int
2474eda14cbcSMatt Macy receive_spill(struct receive_writer_arg *rwa, struct drr_spill *drrs,
24757877fdebSMatt Macy     abd_t *abd)
2476eda14cbcSMatt Macy {
2477eda14cbcSMatt Macy 	dmu_buf_t *db, *db_spill;
2478eda14cbcSMatt Macy 	int err;
2479eda14cbcSMatt Macy 
2480eda14cbcSMatt Macy 	if (drrs->drr_length < SPA_MINBLOCKSIZE ||
2481eda14cbcSMatt Macy 	    drrs->drr_length > spa_maxblocksize(dmu_objset_spa(rwa->os)))
2482eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
2483eda14cbcSMatt Macy 
2484eda14cbcSMatt Macy 	/*
2485eda14cbcSMatt Macy 	 * This is an unmodified spill block which was added to the stream
2486eda14cbcSMatt Macy 	 * to resolve an issue with incorrectly removing spill blocks.  It
2487eda14cbcSMatt Macy 	 * should be ignored by current versions of the code which support
2488eda14cbcSMatt Macy 	 * the DRR_FLAG_SPILL_BLOCK flag.
2489eda14cbcSMatt Macy 	 */
2490eda14cbcSMatt Macy 	if (rwa->spill && DRR_SPILL_IS_UNMODIFIED(drrs->drr_flags)) {
24917877fdebSMatt Macy 		abd_free(abd);
2492eda14cbcSMatt Macy 		return (0);
2493eda14cbcSMatt Macy 	}
2494eda14cbcSMatt Macy 
2495eda14cbcSMatt Macy 	if (rwa->raw) {
2496eda14cbcSMatt Macy 		if (!DMU_OT_IS_VALID(drrs->drr_type) ||
2497eda14cbcSMatt Macy 		    drrs->drr_compressiontype >= ZIO_COMPRESS_FUNCTIONS ||
2498eda14cbcSMatt Macy 		    drrs->drr_compressed_size == 0)
2499eda14cbcSMatt Macy 			return (SET_ERROR(EINVAL));
2500eda14cbcSMatt Macy 	}
2501eda14cbcSMatt Macy 
2502eda14cbcSMatt Macy 	if (dmu_object_info(rwa->os, drrs->drr_object, NULL) != 0)
2503eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
2504eda14cbcSMatt Macy 
2505eda14cbcSMatt Macy 	if (drrs->drr_object > rwa->max_object)
2506eda14cbcSMatt Macy 		rwa->max_object = drrs->drr_object;
2507eda14cbcSMatt Macy 
2508eda14cbcSMatt Macy 	VERIFY0(dmu_bonus_hold(rwa->os, drrs->drr_object, FTAG, &db));
2509eda14cbcSMatt Macy 	if ((err = dmu_spill_hold_by_bonus(db, DMU_READ_NO_DECRYPT, FTAG,
2510eda14cbcSMatt Macy 	    &db_spill)) != 0) {
2511eda14cbcSMatt Macy 		dmu_buf_rele(db, FTAG);
2512eda14cbcSMatt Macy 		return (err);
2513eda14cbcSMatt Macy 	}
2514eda14cbcSMatt Macy 
25157877fdebSMatt Macy 	dmu_tx_t *tx = dmu_tx_create(rwa->os);
2516eda14cbcSMatt Macy 
2517eda14cbcSMatt Macy 	dmu_tx_hold_spill(tx, db->db_object);
2518eda14cbcSMatt Macy 
2519eda14cbcSMatt Macy 	err = dmu_tx_assign(tx, TXG_WAIT);
2520eda14cbcSMatt Macy 	if (err != 0) {
2521eda14cbcSMatt Macy 		dmu_buf_rele(db, FTAG);
2522eda14cbcSMatt Macy 		dmu_buf_rele(db_spill, FTAG);
2523eda14cbcSMatt Macy 		dmu_tx_abort(tx);
2524eda14cbcSMatt Macy 		return (err);
2525eda14cbcSMatt Macy 	}
2526eda14cbcSMatt Macy 
2527eda14cbcSMatt Macy 	/*
2528eda14cbcSMatt Macy 	 * Spill blocks may both grow and shrink.  When a change in size
2529eda14cbcSMatt Macy 	 * occurs any existing dbuf must be updated to match the logical
2530eda14cbcSMatt Macy 	 * size of the provided arc_buf_t.
2531eda14cbcSMatt Macy 	 */
2532eda14cbcSMatt Macy 	if (db_spill->db_size != drrs->drr_length) {
2533eda14cbcSMatt Macy 		dmu_buf_will_fill(db_spill, tx);
25347877fdebSMatt Macy 		VERIFY0(dbuf_spill_set_blksz(db_spill,
2535eda14cbcSMatt Macy 		    drrs->drr_length, tx));
2536eda14cbcSMatt Macy 	}
2537eda14cbcSMatt Macy 
25387877fdebSMatt Macy 	arc_buf_t *abuf;
25397877fdebSMatt Macy 	if (rwa->raw) {
25407877fdebSMatt Macy 		boolean_t byteorder = ZFS_HOST_BYTEORDER ^
25417877fdebSMatt Macy 		    !!DRR_IS_RAW_BYTESWAPPED(drrs->drr_flags) ^
25427877fdebSMatt Macy 		    rwa->byteswap;
25437877fdebSMatt Macy 
25447877fdebSMatt Macy 		abuf = arc_loan_raw_buf(dmu_objset_spa(rwa->os),
25457877fdebSMatt Macy 		    drrs->drr_object, byteorder, drrs->drr_salt,
25467877fdebSMatt Macy 		    drrs->drr_iv, drrs->drr_mac, drrs->drr_type,
25477877fdebSMatt Macy 		    drrs->drr_compressed_size, drrs->drr_length,
25487877fdebSMatt Macy 		    drrs->drr_compressiontype, 0);
25497877fdebSMatt Macy 	} else {
25507877fdebSMatt Macy 		abuf = arc_loan_buf(dmu_objset_spa(rwa->os),
25517877fdebSMatt Macy 		    DMU_OT_IS_METADATA(drrs->drr_type),
25527877fdebSMatt Macy 		    drrs->drr_length);
25537877fdebSMatt Macy 		if (rwa->byteswap) {
2554eda14cbcSMatt Macy 			dmu_object_byteswap_t byteswap =
2555eda14cbcSMatt Macy 			    DMU_OT_BYTESWAP(drrs->drr_type);
25567877fdebSMatt Macy 			dmu_ot_byteswap[byteswap].ob_func(abd_to_buf(abd),
2557eda14cbcSMatt Macy 			    DRR_SPILL_PAYLOAD_SIZE(drrs));
2558eda14cbcSMatt Macy 		}
25597877fdebSMatt Macy 	}
2560eda14cbcSMatt Macy 
2561da5137abSMartin Matuska 	memcpy(abuf->b_data, abd_to_buf(abd), DRR_SPILL_PAYLOAD_SIZE(drrs));
25627877fdebSMatt Macy 	abd_free(abd);
2563eda14cbcSMatt Macy 	dbuf_assign_arcbuf((dmu_buf_impl_t *)db_spill, abuf, tx);
2564eda14cbcSMatt Macy 
2565eda14cbcSMatt Macy 	dmu_buf_rele(db, FTAG);
2566eda14cbcSMatt Macy 	dmu_buf_rele(db_spill, FTAG);
2567eda14cbcSMatt Macy 
2568eda14cbcSMatt Macy 	dmu_tx_commit(tx);
2569eda14cbcSMatt Macy 	return (0);
2570eda14cbcSMatt Macy }
2571eda14cbcSMatt Macy 
2572eda14cbcSMatt Macy noinline static int
2573eda14cbcSMatt Macy receive_free(struct receive_writer_arg *rwa, struct drr_free *drrf)
2574eda14cbcSMatt Macy {
2575eda14cbcSMatt Macy 	int err;
2576eda14cbcSMatt Macy 
2577eda14cbcSMatt Macy 	if (drrf->drr_length != -1ULL &&
2578eda14cbcSMatt Macy 	    drrf->drr_offset + drrf->drr_length < drrf->drr_offset)
2579eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
2580eda14cbcSMatt Macy 
2581eda14cbcSMatt Macy 	if (dmu_object_info(rwa->os, drrf->drr_object, NULL) != 0)
2582eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
2583eda14cbcSMatt Macy 
2584eda14cbcSMatt Macy 	if (drrf->drr_object > rwa->max_object)
2585eda14cbcSMatt Macy 		rwa->max_object = drrf->drr_object;
2586eda14cbcSMatt Macy 
2587eda14cbcSMatt Macy 	err = dmu_free_long_range(rwa->os, drrf->drr_object,
2588eda14cbcSMatt Macy 	    drrf->drr_offset, drrf->drr_length);
2589eda14cbcSMatt Macy 
2590eda14cbcSMatt Macy 	return (err);
2591eda14cbcSMatt Macy }
2592eda14cbcSMatt Macy 
2593eda14cbcSMatt Macy static int
2594eda14cbcSMatt Macy receive_object_range(struct receive_writer_arg *rwa,
2595eda14cbcSMatt Macy     struct drr_object_range *drror)
2596eda14cbcSMatt Macy {
2597eda14cbcSMatt Macy 	/*
2598eda14cbcSMatt Macy 	 * By default, we assume this block is in our native format
2599eda14cbcSMatt Macy 	 * (ZFS_HOST_BYTEORDER). We then take into account whether
2600eda14cbcSMatt Macy 	 * the send stream is byteswapped (rwa->byteswap). Finally,
2601eda14cbcSMatt Macy 	 * we need to byteswap again if this particular block was
2602eda14cbcSMatt Macy 	 * in non-native format on the send side.
2603eda14cbcSMatt Macy 	 */
2604eda14cbcSMatt Macy 	boolean_t byteorder = ZFS_HOST_BYTEORDER ^ rwa->byteswap ^
2605eda14cbcSMatt Macy 	    !!DRR_IS_RAW_BYTESWAPPED(drror->drr_flags);
2606eda14cbcSMatt Macy 
2607eda14cbcSMatt Macy 	/*
2608eda14cbcSMatt Macy 	 * Since dnode block sizes are constant, we should not need to worry
2609eda14cbcSMatt Macy 	 * about making sure that the dnode block size is the same on the
2610eda14cbcSMatt Macy 	 * sending and receiving sides for the time being. For non-raw sends,
2611eda14cbcSMatt Macy 	 * this does not matter (and in fact we do not send a DRR_OBJECT_RANGE
2612eda14cbcSMatt Macy 	 * record at all). Raw sends require this record type because the
2613eda14cbcSMatt Macy 	 * encryption parameters are used to protect an entire block of bonus
2614eda14cbcSMatt Macy 	 * buffers. If the size of dnode blocks ever becomes variable,
2615eda14cbcSMatt Macy 	 * handling will need to be added to ensure that dnode block sizes
2616eda14cbcSMatt Macy 	 * match on the sending and receiving side.
2617eda14cbcSMatt Macy 	 */
2618eda14cbcSMatt Macy 	if (drror->drr_numslots != DNODES_PER_BLOCK ||
2619eda14cbcSMatt Macy 	    P2PHASE(drror->drr_firstobj, DNODES_PER_BLOCK) != 0 ||
2620eda14cbcSMatt Macy 	    !rwa->raw)
2621eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
2622eda14cbcSMatt Macy 
2623eda14cbcSMatt Macy 	if (drror->drr_firstobj > rwa->max_object)
2624eda14cbcSMatt Macy 		rwa->max_object = drror->drr_firstobj;
2625eda14cbcSMatt Macy 
2626eda14cbcSMatt Macy 	/*
2627eda14cbcSMatt Macy 	 * The DRR_OBJECT_RANGE handling must be deferred to receive_object()
2628eda14cbcSMatt Macy 	 * so that the block of dnodes is not written out when it's empty,
2629eda14cbcSMatt Macy 	 * and converted to a HOLE BP.
2630eda14cbcSMatt Macy 	 */
2631eda14cbcSMatt Macy 	rwa->or_crypt_params_present = B_TRUE;
2632eda14cbcSMatt Macy 	rwa->or_firstobj = drror->drr_firstobj;
2633eda14cbcSMatt Macy 	rwa->or_numslots = drror->drr_numslots;
2634da5137abSMartin Matuska 	memcpy(rwa->or_salt, drror->drr_salt, ZIO_DATA_SALT_LEN);
2635da5137abSMartin Matuska 	memcpy(rwa->or_iv, drror->drr_iv, ZIO_DATA_IV_LEN);
2636da5137abSMartin Matuska 	memcpy(rwa->or_mac, drror->drr_mac, ZIO_DATA_MAC_LEN);
2637eda14cbcSMatt Macy 	rwa->or_byteorder = byteorder;
2638eda14cbcSMatt Macy 
263915f0b8c3SMartin Matuska 	rwa->or_need_sync = ORNS_MAYBE;
264015f0b8c3SMartin Matuska 
2641eda14cbcSMatt Macy 	return (0);
2642eda14cbcSMatt Macy }
2643eda14cbcSMatt Macy 
2644eda14cbcSMatt Macy /*
2645eda14cbcSMatt Macy  * Until we have the ability to redact large ranges of data efficiently, we
2646eda14cbcSMatt Macy  * process these records as frees.
2647eda14cbcSMatt Macy  */
2648eda14cbcSMatt Macy noinline static int
2649eda14cbcSMatt Macy receive_redact(struct receive_writer_arg *rwa, struct drr_redact *drrr)
2650eda14cbcSMatt Macy {
2651eda14cbcSMatt Macy 	struct drr_free drrf = {0};
2652eda14cbcSMatt Macy 	drrf.drr_length = drrr->drr_length;
2653eda14cbcSMatt Macy 	drrf.drr_object = drrr->drr_object;
2654eda14cbcSMatt Macy 	drrf.drr_offset = drrr->drr_offset;
2655eda14cbcSMatt Macy 	drrf.drr_toguid = drrr->drr_toguid;
2656eda14cbcSMatt Macy 	return (receive_free(rwa, &drrf));
2657eda14cbcSMatt Macy }
2658eda14cbcSMatt Macy 
2659eda14cbcSMatt Macy /* used to destroy the drc_ds on error */
2660eda14cbcSMatt Macy static void
2661eda14cbcSMatt Macy dmu_recv_cleanup_ds(dmu_recv_cookie_t *drc)
2662eda14cbcSMatt Macy {
2663eda14cbcSMatt Macy 	dsl_dataset_t *ds = drc->drc_ds;
26647877fdebSMatt Macy 	ds_hold_flags_t dsflags;
2665eda14cbcSMatt Macy 
26667877fdebSMatt Macy 	dsflags = (drc->drc_raw) ? DS_HOLD_FLAG_NONE : DS_HOLD_FLAG_DECRYPT;
2667eda14cbcSMatt Macy 	/*
2668eda14cbcSMatt Macy 	 * Wait for the txg sync before cleaning up the receive. For
2669eda14cbcSMatt Macy 	 * resumable receives, this ensures that our resume state has
2670eda14cbcSMatt Macy 	 * been written out to disk. For raw receives, this ensures
2671eda14cbcSMatt Macy 	 * that the user accounting code will not attempt to do anything
2672eda14cbcSMatt Macy 	 * after we stopped receiving the dataset.
2673eda14cbcSMatt Macy 	 */
2674eda14cbcSMatt Macy 	txg_wait_synced(ds->ds_dir->dd_pool, 0);
2675eda14cbcSMatt Macy 	ds->ds_objset->os_raw_receive = B_FALSE;
2676eda14cbcSMatt Macy 
2677eda14cbcSMatt Macy 	rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
2678eda14cbcSMatt Macy 	if (drc->drc_resumable && drc->drc_should_save &&
2679eda14cbcSMatt Macy 	    !BP_IS_HOLE(dsl_dataset_get_blkptr(ds))) {
2680eda14cbcSMatt Macy 		rrw_exit(&ds->ds_bp_rwlock, FTAG);
2681eda14cbcSMatt Macy 		dsl_dataset_disown(ds, dsflags, dmu_recv_tag);
2682eda14cbcSMatt Macy 	} else {
2683eda14cbcSMatt Macy 		char name[ZFS_MAX_DATASET_NAME_LEN];
2684eda14cbcSMatt Macy 		rrw_exit(&ds->ds_bp_rwlock, FTAG);
2685eda14cbcSMatt Macy 		dsl_dataset_name(ds, name);
2686eda14cbcSMatt Macy 		dsl_dataset_disown(ds, dsflags, dmu_recv_tag);
2687271171e0SMartin Matuska 		if (!drc->drc_heal)
2688eda14cbcSMatt Macy 			(void) dsl_destroy_head(name);
2689eda14cbcSMatt Macy 	}
2690eda14cbcSMatt Macy }
2691eda14cbcSMatt Macy 
2692eda14cbcSMatt Macy static void
2693eda14cbcSMatt Macy receive_cksum(dmu_recv_cookie_t *drc, int len, void *buf)
2694eda14cbcSMatt Macy {
2695eda14cbcSMatt Macy 	if (drc->drc_byteswap) {
2696eda14cbcSMatt Macy 		(void) fletcher_4_incremental_byteswap(buf, len,
2697eda14cbcSMatt Macy 		    &drc->drc_cksum);
2698eda14cbcSMatt Macy 	} else {
2699eda14cbcSMatt Macy 		(void) fletcher_4_incremental_native(buf, len, &drc->drc_cksum);
2700eda14cbcSMatt Macy 	}
2701eda14cbcSMatt Macy }
2702eda14cbcSMatt Macy 
2703eda14cbcSMatt Macy /*
2704eda14cbcSMatt Macy  * Read the payload into a buffer of size len, and update the current record's
2705eda14cbcSMatt Macy  * payload field.
2706eda14cbcSMatt Macy  * Allocate drc->drc_next_rrd and read the next record's header into
2707eda14cbcSMatt Macy  * drc->drc_next_rrd->header.
2708eda14cbcSMatt Macy  * Verify checksum of payload and next record.
2709eda14cbcSMatt Macy  */
2710eda14cbcSMatt Macy static int
2711eda14cbcSMatt Macy receive_read_payload_and_next_header(dmu_recv_cookie_t *drc, int len, void *buf)
2712eda14cbcSMatt Macy {
2713eda14cbcSMatt Macy 	int err;
2714eda14cbcSMatt Macy 
2715eda14cbcSMatt Macy 	if (len != 0) {
2716eda14cbcSMatt Macy 		ASSERT3U(len, <=, SPA_MAXBLOCKSIZE);
2717eda14cbcSMatt Macy 		err = receive_read(drc, len, buf);
2718eda14cbcSMatt Macy 		if (err != 0)
2719eda14cbcSMatt Macy 			return (err);
2720eda14cbcSMatt Macy 		receive_cksum(drc, len, buf);
2721eda14cbcSMatt Macy 
2722eda14cbcSMatt Macy 		/* note: rrd is NULL when reading the begin record's payload */
2723eda14cbcSMatt Macy 		if (drc->drc_rrd != NULL) {
2724eda14cbcSMatt Macy 			drc->drc_rrd->payload = buf;
2725eda14cbcSMatt Macy 			drc->drc_rrd->payload_size = len;
2726eda14cbcSMatt Macy 			drc->drc_rrd->bytes_read = drc->drc_bytes_read;
2727eda14cbcSMatt Macy 		}
2728eda14cbcSMatt Macy 	} else {
2729eda14cbcSMatt Macy 		ASSERT3P(buf, ==, NULL);
2730eda14cbcSMatt Macy 	}
2731eda14cbcSMatt Macy 
2732eda14cbcSMatt Macy 	drc->drc_prev_cksum = drc->drc_cksum;
2733eda14cbcSMatt Macy 
2734eda14cbcSMatt Macy 	drc->drc_next_rrd = kmem_zalloc(sizeof (*drc->drc_next_rrd), KM_SLEEP);
2735eda14cbcSMatt Macy 	err = receive_read(drc, sizeof (drc->drc_next_rrd->header),
2736eda14cbcSMatt Macy 	    &drc->drc_next_rrd->header);
2737eda14cbcSMatt Macy 	drc->drc_next_rrd->bytes_read = drc->drc_bytes_read;
2738eda14cbcSMatt Macy 
2739eda14cbcSMatt Macy 	if (err != 0) {
2740eda14cbcSMatt Macy 		kmem_free(drc->drc_next_rrd, sizeof (*drc->drc_next_rrd));
2741eda14cbcSMatt Macy 		drc->drc_next_rrd = NULL;
2742eda14cbcSMatt Macy 		return (err);
2743eda14cbcSMatt Macy 	}
2744eda14cbcSMatt Macy 	if (drc->drc_next_rrd->header.drr_type == DRR_BEGIN) {
2745eda14cbcSMatt Macy 		kmem_free(drc->drc_next_rrd, sizeof (*drc->drc_next_rrd));
2746eda14cbcSMatt Macy 		drc->drc_next_rrd = NULL;
2747eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
2748eda14cbcSMatt Macy 	}
2749eda14cbcSMatt Macy 
2750eda14cbcSMatt Macy 	/*
2751eda14cbcSMatt Macy 	 * Note: checksum is of everything up to but not including the
2752eda14cbcSMatt Macy 	 * checksum itself.
2753eda14cbcSMatt Macy 	 */
2754eda14cbcSMatt Macy 	ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
2755eda14cbcSMatt Macy 	    ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
2756eda14cbcSMatt Macy 	receive_cksum(drc,
2757eda14cbcSMatt Macy 	    offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
2758eda14cbcSMatt Macy 	    &drc->drc_next_rrd->header);
2759eda14cbcSMatt Macy 
2760eda14cbcSMatt Macy 	zio_cksum_t cksum_orig =
2761eda14cbcSMatt Macy 	    drc->drc_next_rrd->header.drr_u.drr_checksum.drr_checksum;
2762eda14cbcSMatt Macy 	zio_cksum_t *cksump =
2763eda14cbcSMatt Macy 	    &drc->drc_next_rrd->header.drr_u.drr_checksum.drr_checksum;
2764eda14cbcSMatt Macy 
2765eda14cbcSMatt Macy 	if (drc->drc_byteswap)
2766eda14cbcSMatt Macy 		byteswap_record(&drc->drc_next_rrd->header);
2767eda14cbcSMatt Macy 
2768eda14cbcSMatt Macy 	if ((!ZIO_CHECKSUM_IS_ZERO(cksump)) &&
2769eda14cbcSMatt Macy 	    !ZIO_CHECKSUM_EQUAL(drc->drc_cksum, *cksump)) {
2770eda14cbcSMatt Macy 		kmem_free(drc->drc_next_rrd, sizeof (*drc->drc_next_rrd));
2771eda14cbcSMatt Macy 		drc->drc_next_rrd = NULL;
2772eda14cbcSMatt Macy 		return (SET_ERROR(ECKSUM));
2773eda14cbcSMatt Macy 	}
2774eda14cbcSMatt Macy 
2775eda14cbcSMatt Macy 	receive_cksum(drc, sizeof (cksum_orig), &cksum_orig);
2776eda14cbcSMatt Macy 
2777eda14cbcSMatt Macy 	return (0);
2778eda14cbcSMatt Macy }
2779eda14cbcSMatt Macy 
2780eda14cbcSMatt Macy /*
2781eda14cbcSMatt Macy  * Issue the prefetch reads for any necessary indirect blocks.
2782eda14cbcSMatt Macy  *
2783eda14cbcSMatt Macy  * We use the object ignore list to tell us whether or not to issue prefetches
2784eda14cbcSMatt Macy  * for a given object.  We do this for both correctness (in case the blocksize
2785eda14cbcSMatt Macy  * of an object has changed) and performance (if the object doesn't exist, don't
2786eda14cbcSMatt Macy  * needlessly try to issue prefetches).  We also trim the list as we go through
2787eda14cbcSMatt Macy  * the stream to prevent it from growing to an unbounded size.
2788eda14cbcSMatt Macy  *
2789eda14cbcSMatt Macy  * The object numbers within will always be in sorted order, and any write
2790eda14cbcSMatt Macy  * records we see will also be in sorted order, but they're not sorted with
2791eda14cbcSMatt Macy  * respect to each other (i.e. we can get several object records before
2792eda14cbcSMatt Macy  * receiving each object's write records).  As a result, once we've reached a
2793eda14cbcSMatt Macy  * given object number, we can safely remove any reference to lower object
2794eda14cbcSMatt Macy  * numbers in the ignore list. In practice, we receive up to 32 object records
2795eda14cbcSMatt Macy  * before receiving write records, so the list can have up to 32 nodes in it.
2796eda14cbcSMatt Macy  */
2797eda14cbcSMatt Macy static void
2798eda14cbcSMatt Macy receive_read_prefetch(dmu_recv_cookie_t *drc, uint64_t object, uint64_t offset,
2799eda14cbcSMatt Macy     uint64_t length)
2800eda14cbcSMatt Macy {
2801eda14cbcSMatt Macy 	if (!objlist_exists(drc->drc_ignore_objlist, object)) {
2802eda14cbcSMatt Macy 		dmu_prefetch(drc->drc_os, object, 1, offset, length,
2803eda14cbcSMatt Macy 		    ZIO_PRIORITY_SYNC_READ);
2804eda14cbcSMatt Macy 	}
2805eda14cbcSMatt Macy }
2806eda14cbcSMatt Macy 
2807eda14cbcSMatt Macy /*
2808eda14cbcSMatt Macy  * Read records off the stream, issuing any necessary prefetches.
2809eda14cbcSMatt Macy  */
2810eda14cbcSMatt Macy static int
2811eda14cbcSMatt Macy receive_read_record(dmu_recv_cookie_t *drc)
2812eda14cbcSMatt Macy {
2813eda14cbcSMatt Macy 	int err;
2814eda14cbcSMatt Macy 
2815eda14cbcSMatt Macy 	switch (drc->drc_rrd->header.drr_type) {
2816eda14cbcSMatt Macy 	case DRR_OBJECT:
2817eda14cbcSMatt Macy 	{
2818eda14cbcSMatt Macy 		struct drr_object *drro =
2819eda14cbcSMatt Macy 		    &drc->drc_rrd->header.drr_u.drr_object;
2820eda14cbcSMatt Macy 		uint32_t size = DRR_OBJECT_PAYLOAD_SIZE(drro);
2821eda14cbcSMatt Macy 		void *buf = NULL;
2822eda14cbcSMatt Macy 		dmu_object_info_t doi;
2823eda14cbcSMatt Macy 
2824eda14cbcSMatt Macy 		if (size != 0)
2825eda14cbcSMatt Macy 			buf = kmem_zalloc(size, KM_SLEEP);
2826eda14cbcSMatt Macy 
2827eda14cbcSMatt Macy 		err = receive_read_payload_and_next_header(drc, size, buf);
2828eda14cbcSMatt Macy 		if (err != 0) {
2829eda14cbcSMatt Macy 			kmem_free(buf, size);
2830eda14cbcSMatt Macy 			return (err);
2831eda14cbcSMatt Macy 		}
2832eda14cbcSMatt Macy 		err = dmu_object_info(drc->drc_os, drro->drr_object, &doi);
2833eda14cbcSMatt Macy 		/*
2834eda14cbcSMatt Macy 		 * See receive_read_prefetch for an explanation why we're
2835eda14cbcSMatt Macy 		 * storing this object in the ignore_obj_list.
2836eda14cbcSMatt Macy 		 */
2837eda14cbcSMatt Macy 		if (err == ENOENT || err == EEXIST ||
2838eda14cbcSMatt Macy 		    (err == 0 && doi.doi_data_block_size != drro->drr_blksz)) {
2839eda14cbcSMatt Macy 			objlist_insert(drc->drc_ignore_objlist,
2840eda14cbcSMatt Macy 			    drro->drr_object);
2841eda14cbcSMatt Macy 			err = 0;
2842eda14cbcSMatt Macy 		}
2843eda14cbcSMatt Macy 		return (err);
2844eda14cbcSMatt Macy 	}
2845eda14cbcSMatt Macy 	case DRR_FREEOBJECTS:
2846eda14cbcSMatt Macy 	{
2847eda14cbcSMatt Macy 		err = receive_read_payload_and_next_header(drc, 0, NULL);
2848eda14cbcSMatt Macy 		return (err);
2849eda14cbcSMatt Macy 	}
2850eda14cbcSMatt Macy 	case DRR_WRITE:
2851eda14cbcSMatt Macy 	{
2852eda14cbcSMatt Macy 		struct drr_write *drrw = &drc->drc_rrd->header.drr_u.drr_write;
28537877fdebSMatt Macy 		int size = DRR_WRITE_PAYLOAD_SIZE(drrw);
28547877fdebSMatt Macy 		abd_t *abd = abd_alloc_linear(size, B_FALSE);
28557877fdebSMatt Macy 		err = receive_read_payload_and_next_header(drc, size,
28567877fdebSMatt Macy 		    abd_to_buf(abd));
2857eda14cbcSMatt Macy 		if (err != 0) {
28587877fdebSMatt Macy 			abd_free(abd);
2859eda14cbcSMatt Macy 			return (err);
2860eda14cbcSMatt Macy 		}
28617877fdebSMatt Macy 		drc->drc_rrd->abd = abd;
2862eda14cbcSMatt Macy 		receive_read_prefetch(drc, drrw->drr_object, drrw->drr_offset,
2863eda14cbcSMatt Macy 		    drrw->drr_logical_size);
2864eda14cbcSMatt Macy 		return (err);
2865eda14cbcSMatt Macy 	}
2866eda14cbcSMatt Macy 	case DRR_WRITE_EMBEDDED:
2867eda14cbcSMatt Macy 	{
2868eda14cbcSMatt Macy 		struct drr_write_embedded *drrwe =
2869eda14cbcSMatt Macy 		    &drc->drc_rrd->header.drr_u.drr_write_embedded;
2870eda14cbcSMatt Macy 		uint32_t size = P2ROUNDUP(drrwe->drr_psize, 8);
2871eda14cbcSMatt Macy 		void *buf = kmem_zalloc(size, KM_SLEEP);
2872eda14cbcSMatt Macy 
2873eda14cbcSMatt Macy 		err = receive_read_payload_and_next_header(drc, size, buf);
2874eda14cbcSMatt Macy 		if (err != 0) {
2875eda14cbcSMatt Macy 			kmem_free(buf, size);
2876eda14cbcSMatt Macy 			return (err);
2877eda14cbcSMatt Macy 		}
2878eda14cbcSMatt Macy 
2879eda14cbcSMatt Macy 		receive_read_prefetch(drc, drrwe->drr_object, drrwe->drr_offset,
2880eda14cbcSMatt Macy 		    drrwe->drr_length);
2881eda14cbcSMatt Macy 		return (err);
2882eda14cbcSMatt Macy 	}
2883eda14cbcSMatt Macy 	case DRR_FREE:
2884eda14cbcSMatt Macy 	case DRR_REDACT:
2885eda14cbcSMatt Macy 	{
2886eda14cbcSMatt Macy 		/*
2887eda14cbcSMatt Macy 		 * It might be beneficial to prefetch indirect blocks here, but
2888eda14cbcSMatt Macy 		 * we don't really have the data to decide for sure.
2889eda14cbcSMatt Macy 		 */
2890eda14cbcSMatt Macy 		err = receive_read_payload_and_next_header(drc, 0, NULL);
2891eda14cbcSMatt Macy 		return (err);
2892eda14cbcSMatt Macy 	}
2893eda14cbcSMatt Macy 	case DRR_END:
2894eda14cbcSMatt Macy 	{
2895eda14cbcSMatt Macy 		struct drr_end *drre = &drc->drc_rrd->header.drr_u.drr_end;
2896eda14cbcSMatt Macy 		if (!ZIO_CHECKSUM_EQUAL(drc->drc_prev_cksum,
2897eda14cbcSMatt Macy 		    drre->drr_checksum))
2898eda14cbcSMatt Macy 			return (SET_ERROR(ECKSUM));
2899eda14cbcSMatt Macy 		return (0);
2900eda14cbcSMatt Macy 	}
2901eda14cbcSMatt Macy 	case DRR_SPILL:
2902eda14cbcSMatt Macy 	{
2903eda14cbcSMatt Macy 		struct drr_spill *drrs = &drc->drc_rrd->header.drr_u.drr_spill;
29047877fdebSMatt Macy 		int size = DRR_SPILL_PAYLOAD_SIZE(drrs);
29057877fdebSMatt Macy 		abd_t *abd = abd_alloc_linear(size, B_FALSE);
29067877fdebSMatt Macy 		err = receive_read_payload_and_next_header(drc, size,
29077877fdebSMatt Macy 		    abd_to_buf(abd));
2908eda14cbcSMatt Macy 		if (err != 0)
29097877fdebSMatt Macy 			abd_free(abd);
2910eda14cbcSMatt Macy 		else
29117877fdebSMatt Macy 			drc->drc_rrd->abd = abd;
2912eda14cbcSMatt Macy 		return (err);
2913eda14cbcSMatt Macy 	}
2914eda14cbcSMatt Macy 	case DRR_OBJECT_RANGE:
2915eda14cbcSMatt Macy 	{
2916eda14cbcSMatt Macy 		err = receive_read_payload_and_next_header(drc, 0, NULL);
2917eda14cbcSMatt Macy 		return (err);
2918eda14cbcSMatt Macy 
2919eda14cbcSMatt Macy 	}
2920eda14cbcSMatt Macy 	default:
2921eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
2922eda14cbcSMatt Macy 	}
2923eda14cbcSMatt Macy }
2924eda14cbcSMatt Macy 
2925eda14cbcSMatt Macy 
2926eda14cbcSMatt Macy 
2927eda14cbcSMatt Macy static void
2928eda14cbcSMatt Macy dprintf_drr(struct receive_record_arg *rrd, int err)
2929eda14cbcSMatt Macy {
2930eda14cbcSMatt Macy #ifdef ZFS_DEBUG
2931eda14cbcSMatt Macy 	switch (rrd->header.drr_type) {
2932eda14cbcSMatt Macy 	case DRR_OBJECT:
2933eda14cbcSMatt Macy 	{
2934eda14cbcSMatt Macy 		struct drr_object *drro = &rrd->header.drr_u.drr_object;
2935eda14cbcSMatt Macy 		dprintf("drr_type = OBJECT obj = %llu type = %u "
2936eda14cbcSMatt Macy 		    "bonustype = %u blksz = %u bonuslen = %u cksumtype = %u "
2937eda14cbcSMatt Macy 		    "compress = %u dn_slots = %u err = %d\n",
293833b8c039SMartin Matuska 		    (u_longlong_t)drro->drr_object, drro->drr_type,
293933b8c039SMartin Matuska 		    drro->drr_bonustype, drro->drr_blksz, drro->drr_bonuslen,
2940eda14cbcSMatt Macy 		    drro->drr_checksumtype, drro->drr_compress,
2941eda14cbcSMatt Macy 		    drro->drr_dn_slots, err);
2942eda14cbcSMatt Macy 		break;
2943eda14cbcSMatt Macy 	}
2944eda14cbcSMatt Macy 	case DRR_FREEOBJECTS:
2945eda14cbcSMatt Macy 	{
2946eda14cbcSMatt Macy 		struct drr_freeobjects *drrfo =
2947eda14cbcSMatt Macy 		    &rrd->header.drr_u.drr_freeobjects;
2948eda14cbcSMatt Macy 		dprintf("drr_type = FREEOBJECTS firstobj = %llu "
2949eda14cbcSMatt Macy 		    "numobjs = %llu err = %d\n",
295033b8c039SMartin Matuska 		    (u_longlong_t)drrfo->drr_firstobj,
295133b8c039SMartin Matuska 		    (u_longlong_t)drrfo->drr_numobjs, err);
2952eda14cbcSMatt Macy 		break;
2953eda14cbcSMatt Macy 	}
2954eda14cbcSMatt Macy 	case DRR_WRITE:
2955eda14cbcSMatt Macy 	{
2956eda14cbcSMatt Macy 		struct drr_write *drrw = &rrd->header.drr_u.drr_write;
2957eda14cbcSMatt Macy 		dprintf("drr_type = WRITE obj = %llu type = %u offset = %llu "
2958eda14cbcSMatt Macy 		    "lsize = %llu cksumtype = %u flags = %u "
2959eda14cbcSMatt Macy 		    "compress = %u psize = %llu err = %d\n",
296033b8c039SMartin Matuska 		    (u_longlong_t)drrw->drr_object, drrw->drr_type,
296133b8c039SMartin Matuska 		    (u_longlong_t)drrw->drr_offset,
296233b8c039SMartin Matuska 		    (u_longlong_t)drrw->drr_logical_size,
296333b8c039SMartin Matuska 		    drrw->drr_checksumtype, drrw->drr_flags,
296433b8c039SMartin Matuska 		    drrw->drr_compressiontype,
296533b8c039SMartin Matuska 		    (u_longlong_t)drrw->drr_compressed_size, err);
2966eda14cbcSMatt Macy 		break;
2967eda14cbcSMatt Macy 	}
2968eda14cbcSMatt Macy 	case DRR_WRITE_BYREF:
2969eda14cbcSMatt Macy 	{
2970eda14cbcSMatt Macy 		struct drr_write_byref *drrwbr =
2971eda14cbcSMatt Macy 		    &rrd->header.drr_u.drr_write_byref;
2972eda14cbcSMatt Macy 		dprintf("drr_type = WRITE_BYREF obj = %llu offset = %llu "
2973eda14cbcSMatt Macy 		    "length = %llu toguid = %llx refguid = %llx "
2974eda14cbcSMatt Macy 		    "refobject = %llu refoffset = %llu cksumtype = %u "
2975eda14cbcSMatt Macy 		    "flags = %u err = %d\n",
297633b8c039SMartin Matuska 		    (u_longlong_t)drrwbr->drr_object,
297733b8c039SMartin Matuska 		    (u_longlong_t)drrwbr->drr_offset,
297833b8c039SMartin Matuska 		    (u_longlong_t)drrwbr->drr_length,
297933b8c039SMartin Matuska 		    (u_longlong_t)drrwbr->drr_toguid,
298033b8c039SMartin Matuska 		    (u_longlong_t)drrwbr->drr_refguid,
298133b8c039SMartin Matuska 		    (u_longlong_t)drrwbr->drr_refobject,
298233b8c039SMartin Matuska 		    (u_longlong_t)drrwbr->drr_refoffset,
298333b8c039SMartin Matuska 		    drrwbr->drr_checksumtype, drrwbr->drr_flags, err);
2984eda14cbcSMatt Macy 		break;
2985eda14cbcSMatt Macy 	}
2986eda14cbcSMatt Macy 	case DRR_WRITE_EMBEDDED:
2987eda14cbcSMatt Macy 	{
2988eda14cbcSMatt Macy 		struct drr_write_embedded *drrwe =
2989eda14cbcSMatt Macy 		    &rrd->header.drr_u.drr_write_embedded;
2990eda14cbcSMatt Macy 		dprintf("drr_type = WRITE_EMBEDDED obj = %llu offset = %llu "
2991eda14cbcSMatt Macy 		    "length = %llu compress = %u etype = %u lsize = %u "
2992eda14cbcSMatt Macy 		    "psize = %u err = %d\n",
299333b8c039SMartin Matuska 		    (u_longlong_t)drrwe->drr_object,
299433b8c039SMartin Matuska 		    (u_longlong_t)drrwe->drr_offset,
299533b8c039SMartin Matuska 		    (u_longlong_t)drrwe->drr_length,
2996eda14cbcSMatt Macy 		    drrwe->drr_compression, drrwe->drr_etype,
2997eda14cbcSMatt Macy 		    drrwe->drr_lsize, drrwe->drr_psize, err);
2998eda14cbcSMatt Macy 		break;
2999eda14cbcSMatt Macy 	}
3000eda14cbcSMatt Macy 	case DRR_FREE:
3001eda14cbcSMatt Macy 	{
3002eda14cbcSMatt Macy 		struct drr_free *drrf = &rrd->header.drr_u.drr_free;
3003eda14cbcSMatt Macy 		dprintf("drr_type = FREE obj = %llu offset = %llu "
3004eda14cbcSMatt Macy 		    "length = %lld err = %d\n",
300533b8c039SMartin Matuska 		    (u_longlong_t)drrf->drr_object,
300633b8c039SMartin Matuska 		    (u_longlong_t)drrf->drr_offset,
300733b8c039SMartin Matuska 		    (longlong_t)drrf->drr_length,
3008eda14cbcSMatt Macy 		    err);
3009eda14cbcSMatt Macy 		break;
3010eda14cbcSMatt Macy 	}
3011eda14cbcSMatt Macy 	case DRR_SPILL:
3012eda14cbcSMatt Macy 	{
3013eda14cbcSMatt Macy 		struct drr_spill *drrs = &rrd->header.drr_u.drr_spill;
3014eda14cbcSMatt Macy 		dprintf("drr_type = SPILL obj = %llu length = %llu "
301533b8c039SMartin Matuska 		    "err = %d\n", (u_longlong_t)drrs->drr_object,
301633b8c039SMartin Matuska 		    (u_longlong_t)drrs->drr_length, err);
3017eda14cbcSMatt Macy 		break;
3018eda14cbcSMatt Macy 	}
3019eda14cbcSMatt Macy 	case DRR_OBJECT_RANGE:
3020eda14cbcSMatt Macy 	{
3021eda14cbcSMatt Macy 		struct drr_object_range *drror =
3022eda14cbcSMatt Macy 		    &rrd->header.drr_u.drr_object_range;
3023eda14cbcSMatt Macy 		dprintf("drr_type = OBJECT_RANGE firstobj = %llu "
3024eda14cbcSMatt Macy 		    "numslots = %llu flags = %u err = %d\n",
302533b8c039SMartin Matuska 		    (u_longlong_t)drror->drr_firstobj,
302633b8c039SMartin Matuska 		    (u_longlong_t)drror->drr_numslots,
3027eda14cbcSMatt Macy 		    drror->drr_flags, err);
3028eda14cbcSMatt Macy 		break;
3029eda14cbcSMatt Macy 	}
3030eda14cbcSMatt Macy 	default:
3031eda14cbcSMatt Macy 		return;
3032eda14cbcSMatt Macy 	}
3033eda14cbcSMatt Macy #endif
3034eda14cbcSMatt Macy }
3035eda14cbcSMatt Macy 
3036eda14cbcSMatt Macy /*
3037eda14cbcSMatt Macy  * Commit the records to the pool.
3038eda14cbcSMatt Macy  */
3039eda14cbcSMatt Macy static int
3040eda14cbcSMatt Macy receive_process_record(struct receive_writer_arg *rwa,
3041eda14cbcSMatt Macy     struct receive_record_arg *rrd)
3042eda14cbcSMatt Macy {
3043eda14cbcSMatt Macy 	int err;
3044eda14cbcSMatt Macy 
3045eda14cbcSMatt Macy 	/* Processing in order, therefore bytes_read should be increasing. */
3046eda14cbcSMatt Macy 	ASSERT3U(rrd->bytes_read, >=, rwa->bytes_read);
3047eda14cbcSMatt Macy 	rwa->bytes_read = rrd->bytes_read;
3048eda14cbcSMatt Macy 
3049271171e0SMartin Matuska 	/* We can only heal write records; other ones get ignored */
3050271171e0SMartin Matuska 	if (rwa->heal && rrd->header.drr_type != DRR_WRITE) {
3051271171e0SMartin Matuska 		if (rrd->abd != NULL) {
3052271171e0SMartin Matuska 			abd_free(rrd->abd);
3053271171e0SMartin Matuska 			rrd->abd = NULL;
3054271171e0SMartin Matuska 		} else if (rrd->payload != NULL) {
3055271171e0SMartin Matuska 			kmem_free(rrd->payload, rrd->payload_size);
3056271171e0SMartin Matuska 			rrd->payload = NULL;
3057271171e0SMartin Matuska 		}
3058271171e0SMartin Matuska 		return (0);
3059271171e0SMartin Matuska 	}
3060271171e0SMartin Matuska 
3061271171e0SMartin Matuska 	if (!rwa->heal && rrd->header.drr_type != DRR_WRITE) {
3062eda14cbcSMatt Macy 		err = flush_write_batch(rwa);
3063eda14cbcSMatt Macy 		if (err != 0) {
30647877fdebSMatt Macy 			if (rrd->abd != NULL) {
30657877fdebSMatt Macy 				abd_free(rrd->abd);
30667877fdebSMatt Macy 				rrd->abd = NULL;
3067eda14cbcSMatt Macy 				rrd->payload = NULL;
3068eda14cbcSMatt Macy 			} else if (rrd->payload != NULL) {
3069eda14cbcSMatt Macy 				kmem_free(rrd->payload, rrd->payload_size);
3070eda14cbcSMatt Macy 				rrd->payload = NULL;
3071eda14cbcSMatt Macy 			}
3072eda14cbcSMatt Macy 
3073eda14cbcSMatt Macy 			return (err);
3074eda14cbcSMatt Macy 		}
3075eda14cbcSMatt Macy 	}
3076eda14cbcSMatt Macy 
3077eda14cbcSMatt Macy 	switch (rrd->header.drr_type) {
3078eda14cbcSMatt Macy 	case DRR_OBJECT:
3079eda14cbcSMatt Macy 	{
3080eda14cbcSMatt Macy 		struct drr_object *drro = &rrd->header.drr_u.drr_object;
3081eda14cbcSMatt Macy 		err = receive_object(rwa, drro, rrd->payload);
3082eda14cbcSMatt Macy 		kmem_free(rrd->payload, rrd->payload_size);
3083eda14cbcSMatt Macy 		rrd->payload = NULL;
3084eda14cbcSMatt Macy 		break;
3085eda14cbcSMatt Macy 	}
3086eda14cbcSMatt Macy 	case DRR_FREEOBJECTS:
3087eda14cbcSMatt Macy 	{
3088eda14cbcSMatt Macy 		struct drr_freeobjects *drrfo =
3089eda14cbcSMatt Macy 		    &rrd->header.drr_u.drr_freeobjects;
3090eda14cbcSMatt Macy 		err = receive_freeobjects(rwa, drrfo);
3091eda14cbcSMatt Macy 		break;
3092eda14cbcSMatt Macy 	}
3093eda14cbcSMatt Macy 	case DRR_WRITE:
3094eda14cbcSMatt Macy 	{
3095eda14cbcSMatt Macy 		err = receive_process_write_record(rwa, rrd);
3096271171e0SMartin Matuska 		if (rwa->heal) {
3097eda14cbcSMatt Macy 			/*
3098271171e0SMartin Matuska 			 * If healing - always free the abd after processing
3099271171e0SMartin Matuska 			 */
3100271171e0SMartin Matuska 			abd_free(rrd->abd);
3101271171e0SMartin Matuska 			rrd->abd = NULL;
3102271171e0SMartin Matuska 		} else if (err != EAGAIN) {
3103271171e0SMartin Matuska 			/*
3104271171e0SMartin Matuska 			 * On success, a non-healing
3105271171e0SMartin Matuska 			 * receive_process_write_record() returns
3106eda14cbcSMatt Macy 			 * EAGAIN to indicate that we do not want to free
3107eda14cbcSMatt Macy 			 * the rrd or arc_buf.
3108eda14cbcSMatt Macy 			 */
3109eda14cbcSMatt Macy 			ASSERT(err != 0);
31107877fdebSMatt Macy 			abd_free(rrd->abd);
31117877fdebSMatt Macy 			rrd->abd = NULL;
3112eda14cbcSMatt Macy 		}
3113eda14cbcSMatt Macy 		break;
3114eda14cbcSMatt Macy 	}
3115eda14cbcSMatt Macy 	case DRR_WRITE_EMBEDDED:
3116eda14cbcSMatt Macy 	{
3117eda14cbcSMatt Macy 		struct drr_write_embedded *drrwe =
3118eda14cbcSMatt Macy 		    &rrd->header.drr_u.drr_write_embedded;
3119eda14cbcSMatt Macy 		err = receive_write_embedded(rwa, drrwe, rrd->payload);
3120eda14cbcSMatt Macy 		kmem_free(rrd->payload, rrd->payload_size);
3121eda14cbcSMatt Macy 		rrd->payload = NULL;
3122eda14cbcSMatt Macy 		break;
3123eda14cbcSMatt Macy 	}
3124eda14cbcSMatt Macy 	case DRR_FREE:
3125eda14cbcSMatt Macy 	{
3126eda14cbcSMatt Macy 		struct drr_free *drrf = &rrd->header.drr_u.drr_free;
3127eda14cbcSMatt Macy 		err = receive_free(rwa, drrf);
3128eda14cbcSMatt Macy 		break;
3129eda14cbcSMatt Macy 	}
3130eda14cbcSMatt Macy 	case DRR_SPILL:
3131eda14cbcSMatt Macy 	{
3132eda14cbcSMatt Macy 		struct drr_spill *drrs = &rrd->header.drr_u.drr_spill;
31337877fdebSMatt Macy 		err = receive_spill(rwa, drrs, rrd->abd);
3134eda14cbcSMatt Macy 		if (err != 0)
31357877fdebSMatt Macy 			abd_free(rrd->abd);
31367877fdebSMatt Macy 		rrd->abd = NULL;
3137eda14cbcSMatt Macy 		rrd->payload = NULL;
3138eda14cbcSMatt Macy 		break;
3139eda14cbcSMatt Macy 	}
3140eda14cbcSMatt Macy 	case DRR_OBJECT_RANGE:
3141eda14cbcSMatt Macy 	{
3142eda14cbcSMatt Macy 		struct drr_object_range *drror =
3143eda14cbcSMatt Macy 		    &rrd->header.drr_u.drr_object_range;
3144eda14cbcSMatt Macy 		err = receive_object_range(rwa, drror);
3145eda14cbcSMatt Macy 		break;
3146eda14cbcSMatt Macy 	}
3147eda14cbcSMatt Macy 	case DRR_REDACT:
3148eda14cbcSMatt Macy 	{
3149eda14cbcSMatt Macy 		struct drr_redact *drrr = &rrd->header.drr_u.drr_redact;
3150eda14cbcSMatt Macy 		err = receive_redact(rwa, drrr);
3151eda14cbcSMatt Macy 		break;
3152eda14cbcSMatt Macy 	}
3153eda14cbcSMatt Macy 	default:
3154eda14cbcSMatt Macy 		err = (SET_ERROR(EINVAL));
3155eda14cbcSMatt Macy 	}
3156eda14cbcSMatt Macy 
3157eda14cbcSMatt Macy 	if (err != 0)
3158eda14cbcSMatt Macy 		dprintf_drr(rrd, err);
3159eda14cbcSMatt Macy 
3160eda14cbcSMatt Macy 	return (err);
3161eda14cbcSMatt Macy }
3162eda14cbcSMatt Macy 
3163eda14cbcSMatt Macy /*
3164eda14cbcSMatt Macy  * dmu_recv_stream's worker thread; pull records off the queue, and then call
3165eda14cbcSMatt Macy  * receive_process_record  When we're done, signal the main thread and exit.
3166eda14cbcSMatt Macy  */
3167da5137abSMartin Matuska static __attribute__((noreturn)) void
3168eda14cbcSMatt Macy receive_writer_thread(void *arg)
3169eda14cbcSMatt Macy {
3170eda14cbcSMatt Macy 	struct receive_writer_arg *rwa = arg;
3171eda14cbcSMatt Macy 	struct receive_record_arg *rrd;
3172eda14cbcSMatt Macy 	fstrans_cookie_t cookie = spl_fstrans_mark();
3173eda14cbcSMatt Macy 
3174eda14cbcSMatt Macy 	for (rrd = bqueue_dequeue(&rwa->q); !rrd->eos_marker;
3175eda14cbcSMatt Macy 	    rrd = bqueue_dequeue(&rwa->q)) {
3176eda14cbcSMatt Macy 		/*
3177eda14cbcSMatt Macy 		 * If there's an error, the main thread will stop putting things
3178eda14cbcSMatt Macy 		 * on the queue, but we need to clear everything in it before we
3179eda14cbcSMatt Macy 		 * can exit.
3180eda14cbcSMatt Macy 		 */
3181eda14cbcSMatt Macy 		int err = 0;
3182eda14cbcSMatt Macy 		if (rwa->err == 0) {
3183eda14cbcSMatt Macy 			err = receive_process_record(rwa, rrd);
31847877fdebSMatt Macy 		} else if (rrd->abd != NULL) {
31857877fdebSMatt Macy 			abd_free(rrd->abd);
31867877fdebSMatt Macy 			rrd->abd = NULL;
3187eda14cbcSMatt Macy 			rrd->payload = NULL;
3188eda14cbcSMatt Macy 		} else if (rrd->payload != NULL) {
3189eda14cbcSMatt Macy 			kmem_free(rrd->payload, rrd->payload_size);
3190eda14cbcSMatt Macy 			rrd->payload = NULL;
3191eda14cbcSMatt Macy 		}
3192eda14cbcSMatt Macy 		/*
3193eda14cbcSMatt Macy 		 * EAGAIN indicates that this record has been saved (on
3194eda14cbcSMatt Macy 		 * raw->write_batch), and will be used again, so we don't
3195eda14cbcSMatt Macy 		 * free it.
3196271171e0SMartin Matuska 		 * When healing data we always need to free the record.
3197eda14cbcSMatt Macy 		 */
3198271171e0SMartin Matuska 		if (err != EAGAIN || rwa->heal) {
3199eda14cbcSMatt Macy 			if (rwa->err == 0)
3200eda14cbcSMatt Macy 				rwa->err = err;
3201eda14cbcSMatt Macy 			kmem_free(rrd, sizeof (*rrd));
3202eda14cbcSMatt Macy 		}
3203eda14cbcSMatt Macy 	}
3204eda14cbcSMatt Macy 	kmem_free(rrd, sizeof (*rrd));
3205eda14cbcSMatt Macy 
3206271171e0SMartin Matuska 	if (rwa->heal) {
3207271171e0SMartin Matuska 		zio_wait(rwa->heal_pio);
3208271171e0SMartin Matuska 	} else {
3209eda14cbcSMatt Macy 		int err = flush_write_batch(rwa);
3210eda14cbcSMatt Macy 		if (rwa->err == 0)
3211eda14cbcSMatt Macy 			rwa->err = err;
3212271171e0SMartin Matuska 	}
3213eda14cbcSMatt Macy 	mutex_enter(&rwa->mutex);
3214eda14cbcSMatt Macy 	rwa->done = B_TRUE;
3215eda14cbcSMatt Macy 	cv_signal(&rwa->cv);
3216eda14cbcSMatt Macy 	mutex_exit(&rwa->mutex);
3217eda14cbcSMatt Macy 	spl_fstrans_unmark(cookie);
3218eda14cbcSMatt Macy 	thread_exit();
3219eda14cbcSMatt Macy }
3220eda14cbcSMatt Macy 
3221eda14cbcSMatt Macy static int
3222eda14cbcSMatt Macy resume_check(dmu_recv_cookie_t *drc, nvlist_t *begin_nvl)
3223eda14cbcSMatt Macy {
3224eda14cbcSMatt Macy 	uint64_t val;
3225eda14cbcSMatt Macy 	objset_t *mos = dmu_objset_pool(drc->drc_os)->dp_meta_objset;
3226eda14cbcSMatt Macy 	uint64_t dsobj = dmu_objset_id(drc->drc_os);
3227eda14cbcSMatt Macy 	uint64_t resume_obj, resume_off;
3228eda14cbcSMatt Macy 
3229eda14cbcSMatt Macy 	if (nvlist_lookup_uint64(begin_nvl,
3230eda14cbcSMatt Macy 	    "resume_object", &resume_obj) != 0 ||
3231eda14cbcSMatt Macy 	    nvlist_lookup_uint64(begin_nvl,
3232eda14cbcSMatt Macy 	    "resume_offset", &resume_off) != 0) {
3233eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
3234eda14cbcSMatt Macy 	}
3235eda14cbcSMatt Macy 	VERIFY0(zap_lookup(mos, dsobj,
3236eda14cbcSMatt Macy 	    DS_FIELD_RESUME_OBJECT, sizeof (val), 1, &val));
3237eda14cbcSMatt Macy 	if (resume_obj != val)
3238eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
3239eda14cbcSMatt Macy 	VERIFY0(zap_lookup(mos, dsobj,
3240eda14cbcSMatt Macy 	    DS_FIELD_RESUME_OFFSET, sizeof (val), 1, &val));
3241eda14cbcSMatt Macy 	if (resume_off != val)
3242eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
3243eda14cbcSMatt Macy 
3244eda14cbcSMatt Macy 	return (0);
3245eda14cbcSMatt Macy }
3246eda14cbcSMatt Macy 
3247eda14cbcSMatt Macy /*
3248eda14cbcSMatt Macy  * Read in the stream's records, one by one, and apply them to the pool.  There
3249eda14cbcSMatt Macy  * are two threads involved; the thread that calls this function will spin up a
3250eda14cbcSMatt Macy  * worker thread, read the records off the stream one by one, and issue
3251eda14cbcSMatt Macy  * prefetches for any necessary indirect blocks.  It will then push the records
3252eda14cbcSMatt Macy  * onto an internal blocking queue.  The worker thread will pull the records off
3253eda14cbcSMatt Macy  * the queue, and actually write the data into the DMU.  This way, the worker
3254eda14cbcSMatt Macy  * thread doesn't have to wait for reads to complete, since everything it needs
3255eda14cbcSMatt Macy  * (the indirect blocks) will be prefetched.
3256eda14cbcSMatt Macy  *
3257eda14cbcSMatt Macy  * NB: callers *must* call dmu_recv_end() if this succeeds.
3258eda14cbcSMatt Macy  */
3259eda14cbcSMatt Macy int
3260eda14cbcSMatt Macy dmu_recv_stream(dmu_recv_cookie_t *drc, offset_t *voffp)
3261eda14cbcSMatt Macy {
3262eda14cbcSMatt Macy 	int err = 0;
3263eda14cbcSMatt Macy 	struct receive_writer_arg *rwa = kmem_zalloc(sizeof (*rwa), KM_SLEEP);
3264eda14cbcSMatt Macy 
326516038816SMartin Matuska 	if (dsl_dataset_has_resume_receive_state(drc->drc_ds)) {
326616038816SMartin Matuska 		uint64_t bytes = 0;
3267eda14cbcSMatt Macy 		(void) zap_lookup(drc->drc_ds->ds_dir->dd_pool->dp_meta_objset,
3268eda14cbcSMatt Macy 		    drc->drc_ds->ds_object, DS_FIELD_RESUME_BYTES,
3269eda14cbcSMatt Macy 		    sizeof (bytes), 1, &bytes);
3270eda14cbcSMatt Macy 		drc->drc_bytes_read += bytes;
3271eda14cbcSMatt Macy 	}
3272eda14cbcSMatt Macy 
3273eda14cbcSMatt Macy 	drc->drc_ignore_objlist = objlist_create();
3274eda14cbcSMatt Macy 
3275eda14cbcSMatt Macy 	/* these were verified in dmu_recv_begin */
3276eda14cbcSMatt Macy 	ASSERT3U(DMU_GET_STREAM_HDRTYPE(drc->drc_drrb->drr_versioninfo), ==,
3277eda14cbcSMatt Macy 	    DMU_SUBSTREAM);
3278eda14cbcSMatt Macy 	ASSERT3U(drc->drc_drrb->drr_type, <, DMU_OST_NUMTYPES);
3279eda14cbcSMatt Macy 
3280eda14cbcSMatt Macy 	ASSERT(dsl_dataset_phys(drc->drc_ds)->ds_flags & DS_FLAG_INCONSISTENT);
3281eda14cbcSMatt Macy 	ASSERT0(drc->drc_os->os_encrypted &&
3282eda14cbcSMatt Macy 	    (drc->drc_featureflags & DMU_BACKUP_FEATURE_EMBED_DATA));
3283eda14cbcSMatt Macy 
3284eda14cbcSMatt Macy 	/* handle DSL encryption key payload */
3285eda14cbcSMatt Macy 	if (drc->drc_featureflags & DMU_BACKUP_FEATURE_RAW) {
3286eda14cbcSMatt Macy 		nvlist_t *keynvl = NULL;
3287eda14cbcSMatt Macy 
3288eda14cbcSMatt Macy 		ASSERT(drc->drc_os->os_encrypted);
3289eda14cbcSMatt Macy 		ASSERT(drc->drc_raw);
3290eda14cbcSMatt Macy 
3291eda14cbcSMatt Macy 		err = nvlist_lookup_nvlist(drc->drc_begin_nvl, "crypt_keydata",
3292eda14cbcSMatt Macy 		    &keynvl);
3293eda14cbcSMatt Macy 		if (err != 0)
3294eda14cbcSMatt Macy 			goto out;
3295eda14cbcSMatt Macy 
3296271171e0SMartin Matuska 		if (!drc->drc_heal) {
3297eda14cbcSMatt Macy 			/*
3298eda14cbcSMatt Macy 			 * If this is a new dataset we set the key immediately.
3299eda14cbcSMatt Macy 			 * Otherwise we don't want to change the key until we
3300271171e0SMartin Matuska 			 * are sure the rest of the receive succeeded so we
3301271171e0SMartin Matuska 			 * stash the keynvl away until then.
3302eda14cbcSMatt Macy 			 */
3303eda14cbcSMatt Macy 			err = dsl_crypto_recv_raw(spa_name(drc->drc_os->os_spa),
3304eda14cbcSMatt Macy 			    drc->drc_ds->ds_object, drc->drc_fromsnapobj,
3305eda14cbcSMatt Macy 			    drc->drc_drrb->drr_type, keynvl, drc->drc_newfs);
3306eda14cbcSMatt Macy 			if (err != 0)
3307eda14cbcSMatt Macy 				goto out;
3308271171e0SMartin Matuska 		}
3309eda14cbcSMatt Macy 
3310eda14cbcSMatt Macy 		/* see comment in dmu_recv_end_sync() */
3311eda14cbcSMatt Macy 		drc->drc_ivset_guid = 0;
3312eda14cbcSMatt Macy 		(void) nvlist_lookup_uint64(keynvl, "to_ivset_guid",
3313eda14cbcSMatt Macy 		    &drc->drc_ivset_guid);
3314eda14cbcSMatt Macy 
3315eda14cbcSMatt Macy 		if (!drc->drc_newfs)
3316eda14cbcSMatt Macy 			drc->drc_keynvl = fnvlist_dup(keynvl);
3317eda14cbcSMatt Macy 	}
3318eda14cbcSMatt Macy 
3319eda14cbcSMatt Macy 	if (drc->drc_featureflags & DMU_BACKUP_FEATURE_RESUMING) {
3320eda14cbcSMatt Macy 		err = resume_check(drc, drc->drc_begin_nvl);
3321eda14cbcSMatt Macy 		if (err != 0)
3322eda14cbcSMatt Macy 			goto out;
3323eda14cbcSMatt Macy 	}
3324eda14cbcSMatt Macy 
3325eda14cbcSMatt Macy 	/*
3326*2a58b312SMartin Matuska 	 * For compatibility with recursive send streams, we do this here,
3327*2a58b312SMartin Matuska 	 * rather than in dmu_recv_begin. If we pull the next header too
3328*2a58b312SMartin Matuska 	 * early, and it's the END record, we break the `recv_skip` logic.
3329*2a58b312SMartin Matuska 	 */
3330*2a58b312SMartin Matuska 	if (drc->drc_drr_begin->drr_payloadlen == 0) {
3331*2a58b312SMartin Matuska 		err = receive_read_payload_and_next_header(drc, 0, NULL);
3332*2a58b312SMartin Matuska 		if (err != 0)
3333*2a58b312SMartin Matuska 			goto out;
3334*2a58b312SMartin Matuska 	}
3335*2a58b312SMartin Matuska 
3336*2a58b312SMartin Matuska 	/*
3337eda14cbcSMatt Macy 	 * If we failed before this point we will clean up any new resume
3338eda14cbcSMatt Macy 	 * state that was created. Now that we've gotten past the initial
3339eda14cbcSMatt Macy 	 * checks we are ok to retain that resume state.
3340eda14cbcSMatt Macy 	 */
3341eda14cbcSMatt Macy 	drc->drc_should_save = B_TRUE;
3342eda14cbcSMatt Macy 
3343eda14cbcSMatt Macy 	(void) bqueue_init(&rwa->q, zfs_recv_queue_ff,
3344eda14cbcSMatt Macy 	    MAX(zfs_recv_queue_length, 2 * zfs_max_recordsize),
3345eda14cbcSMatt Macy 	    offsetof(struct receive_record_arg, node));
3346eda14cbcSMatt Macy 	cv_init(&rwa->cv, NULL, CV_DEFAULT, NULL);
3347eda14cbcSMatt Macy 	mutex_init(&rwa->mutex, NULL, MUTEX_DEFAULT, NULL);
3348eda14cbcSMatt Macy 	rwa->os = drc->drc_os;
3349eda14cbcSMatt Macy 	rwa->byteswap = drc->drc_byteswap;
3350271171e0SMartin Matuska 	rwa->heal = drc->drc_heal;
3351271171e0SMartin Matuska 	rwa->tofs = drc->drc_tofs;
3352eda14cbcSMatt Macy 	rwa->resumable = drc->drc_resumable;
3353eda14cbcSMatt Macy 	rwa->raw = drc->drc_raw;
3354eda14cbcSMatt Macy 	rwa->spill = drc->drc_spill;
3355eda14cbcSMatt Macy 	rwa->full = (drc->drc_drr_begin->drr_u.drr_begin.drr_fromguid == 0);
3356eda14cbcSMatt Macy 	rwa->os->os_raw_receive = drc->drc_raw;
3357271171e0SMartin Matuska 	if (drc->drc_heal) {
3358271171e0SMartin Matuska 		rwa->heal_pio = zio_root(drc->drc_os->os_spa, NULL, NULL,
3359271171e0SMartin Matuska 		    ZIO_FLAG_GODFATHER);
3360271171e0SMartin Matuska 	}
3361eda14cbcSMatt Macy 	list_create(&rwa->write_batch, sizeof (struct receive_record_arg),
3362eda14cbcSMatt Macy 	    offsetof(struct receive_record_arg, node.bqn_node));
3363eda14cbcSMatt Macy 
3364eda14cbcSMatt Macy 	(void) thread_create(NULL, 0, receive_writer_thread, rwa, 0, curproc,
3365eda14cbcSMatt Macy 	    TS_RUN, minclsyspri);
3366eda14cbcSMatt Macy 	/*
3367eda14cbcSMatt Macy 	 * We're reading rwa->err without locks, which is safe since we are the
3368eda14cbcSMatt Macy 	 * only reader, and the worker thread is the only writer.  It's ok if we
3369eda14cbcSMatt Macy 	 * miss a write for an iteration or two of the loop, since the writer
3370eda14cbcSMatt Macy 	 * thread will keep freeing records we send it until we send it an eos
3371eda14cbcSMatt Macy 	 * marker.
3372eda14cbcSMatt Macy 	 *
3373eda14cbcSMatt Macy 	 * We can leave this loop in 3 ways:  First, if rwa->err is
3374eda14cbcSMatt Macy 	 * non-zero.  In that case, the writer thread will free the rrd we just
3375eda14cbcSMatt Macy 	 * pushed.  Second, if  we're interrupted; in that case, either it's the
3376eda14cbcSMatt Macy 	 * first loop and drc->drc_rrd was never allocated, or it's later, and
3377eda14cbcSMatt Macy 	 * drc->drc_rrd has been handed off to the writer thread who will free
3378eda14cbcSMatt Macy 	 * it.  Finally, if receive_read_record fails or we're at the end of the
3379eda14cbcSMatt Macy 	 * stream, then we free drc->drc_rrd and exit.
3380eda14cbcSMatt Macy 	 */
3381eda14cbcSMatt Macy 	while (rwa->err == 0) {
3382eda14cbcSMatt Macy 		if (issig(JUSTLOOKING) && issig(FORREAL)) {
3383eda14cbcSMatt Macy 			err = SET_ERROR(EINTR);
3384eda14cbcSMatt Macy 			break;
3385eda14cbcSMatt Macy 		}
3386eda14cbcSMatt Macy 
3387eda14cbcSMatt Macy 		ASSERT3P(drc->drc_rrd, ==, NULL);
3388eda14cbcSMatt Macy 		drc->drc_rrd = drc->drc_next_rrd;
3389eda14cbcSMatt Macy 		drc->drc_next_rrd = NULL;
3390eda14cbcSMatt Macy 		/* Allocates and loads header into drc->drc_next_rrd */
3391eda14cbcSMatt Macy 		err = receive_read_record(drc);
3392eda14cbcSMatt Macy 
3393eda14cbcSMatt Macy 		if (drc->drc_rrd->header.drr_type == DRR_END || err != 0) {
3394eda14cbcSMatt Macy 			kmem_free(drc->drc_rrd, sizeof (*drc->drc_rrd));
3395eda14cbcSMatt Macy 			drc->drc_rrd = NULL;
3396eda14cbcSMatt Macy 			break;
3397eda14cbcSMatt Macy 		}
3398eda14cbcSMatt Macy 
3399eda14cbcSMatt Macy 		bqueue_enqueue(&rwa->q, drc->drc_rrd,
3400eda14cbcSMatt Macy 		    sizeof (struct receive_record_arg) +
3401eda14cbcSMatt Macy 		    drc->drc_rrd->payload_size);
3402eda14cbcSMatt Macy 		drc->drc_rrd = NULL;
3403eda14cbcSMatt Macy 	}
3404eda14cbcSMatt Macy 
3405eda14cbcSMatt Macy 	ASSERT3P(drc->drc_rrd, ==, NULL);
3406eda14cbcSMatt Macy 	drc->drc_rrd = kmem_zalloc(sizeof (*drc->drc_rrd), KM_SLEEP);
3407eda14cbcSMatt Macy 	drc->drc_rrd->eos_marker = B_TRUE;
3408eda14cbcSMatt Macy 	bqueue_enqueue_flush(&rwa->q, drc->drc_rrd, 1);
3409eda14cbcSMatt Macy 
3410eda14cbcSMatt Macy 	mutex_enter(&rwa->mutex);
3411eda14cbcSMatt Macy 	while (!rwa->done) {
3412eda14cbcSMatt Macy 		/*
3413eda14cbcSMatt Macy 		 * We need to use cv_wait_sig() so that any process that may
3414eda14cbcSMatt Macy 		 * be sleeping here can still fork.
3415eda14cbcSMatt Macy 		 */
3416eda14cbcSMatt Macy 		(void) cv_wait_sig(&rwa->cv, &rwa->mutex);
3417eda14cbcSMatt Macy 	}
3418eda14cbcSMatt Macy 	mutex_exit(&rwa->mutex);
3419eda14cbcSMatt Macy 
3420eda14cbcSMatt Macy 	/*
3421eda14cbcSMatt Macy 	 * If we are receiving a full stream as a clone, all object IDs which
3422eda14cbcSMatt Macy 	 * are greater than the maximum ID referenced in the stream are
3423eda14cbcSMatt Macy 	 * by definition unused and must be freed.
3424eda14cbcSMatt Macy 	 */
3425eda14cbcSMatt Macy 	if (drc->drc_clone && drc->drc_drrb->drr_fromguid == 0) {
3426eda14cbcSMatt Macy 		uint64_t obj = rwa->max_object + 1;
3427eda14cbcSMatt Macy 		int free_err = 0;
3428eda14cbcSMatt Macy 		int next_err = 0;
3429eda14cbcSMatt Macy 
3430eda14cbcSMatt Macy 		while (next_err == 0) {
3431eda14cbcSMatt Macy 			free_err = dmu_free_long_object(rwa->os, obj);
3432eda14cbcSMatt Macy 			if (free_err != 0 && free_err != ENOENT)
3433eda14cbcSMatt Macy 				break;
3434eda14cbcSMatt Macy 
3435eda14cbcSMatt Macy 			next_err = dmu_object_next(rwa->os, &obj, FALSE, 0);
3436eda14cbcSMatt Macy 		}
3437eda14cbcSMatt Macy 
3438eda14cbcSMatt Macy 		if (err == 0) {
3439eda14cbcSMatt Macy 			if (free_err != 0 && free_err != ENOENT)
3440eda14cbcSMatt Macy 				err = free_err;
3441eda14cbcSMatt Macy 			else if (next_err != ESRCH)
3442eda14cbcSMatt Macy 				err = next_err;
3443eda14cbcSMatt Macy 		}
3444eda14cbcSMatt Macy 	}
3445eda14cbcSMatt Macy 
3446eda14cbcSMatt Macy 	cv_destroy(&rwa->cv);
3447eda14cbcSMatt Macy 	mutex_destroy(&rwa->mutex);
3448eda14cbcSMatt Macy 	bqueue_destroy(&rwa->q);
3449eda14cbcSMatt Macy 	list_destroy(&rwa->write_batch);
3450eda14cbcSMatt Macy 	if (err == 0)
3451eda14cbcSMatt Macy 		err = rwa->err;
3452eda14cbcSMatt Macy 
3453eda14cbcSMatt Macy out:
3454eda14cbcSMatt Macy 	/*
3455eda14cbcSMatt Macy 	 * If we hit an error before we started the receive_writer_thread
3456eda14cbcSMatt Macy 	 * we need to clean up the next_rrd we create by processing the
3457eda14cbcSMatt Macy 	 * DRR_BEGIN record.
3458eda14cbcSMatt Macy 	 */
3459eda14cbcSMatt Macy 	if (drc->drc_next_rrd != NULL)
3460eda14cbcSMatt Macy 		kmem_free(drc->drc_next_rrd, sizeof (*drc->drc_next_rrd));
3461eda14cbcSMatt Macy 
3462eda14cbcSMatt Macy 	/*
3463eda14cbcSMatt Macy 	 * The objset will be invalidated by dmu_recv_end() when we do
3464eda14cbcSMatt Macy 	 * dsl_dataset_clone_swap_sync_impl().
3465eda14cbcSMatt Macy 	 */
3466eda14cbcSMatt Macy 	drc->drc_os = NULL;
3467eda14cbcSMatt Macy 
3468eda14cbcSMatt Macy 	kmem_free(rwa, sizeof (*rwa));
3469eda14cbcSMatt Macy 	nvlist_free(drc->drc_begin_nvl);
3470eda14cbcSMatt Macy 
3471eda14cbcSMatt Macy 	if (err != 0) {
3472eda14cbcSMatt Macy 		/*
3473eda14cbcSMatt Macy 		 * Clean up references. If receive is not resumable,
3474eda14cbcSMatt Macy 		 * destroy what we created, so we don't leave it in
3475eda14cbcSMatt Macy 		 * the inconsistent state.
3476eda14cbcSMatt Macy 		 */
3477eda14cbcSMatt Macy 		dmu_recv_cleanup_ds(drc);
3478eda14cbcSMatt Macy 		nvlist_free(drc->drc_keynvl);
3479eda14cbcSMatt Macy 	}
3480eda14cbcSMatt Macy 
3481eda14cbcSMatt Macy 	objlist_destroy(drc->drc_ignore_objlist);
3482eda14cbcSMatt Macy 	drc->drc_ignore_objlist = NULL;
3483eda14cbcSMatt Macy 	*voffp = drc->drc_voff;
3484eda14cbcSMatt Macy 	return (err);
3485eda14cbcSMatt Macy }
3486eda14cbcSMatt Macy 
3487eda14cbcSMatt Macy static int
3488eda14cbcSMatt Macy dmu_recv_end_check(void *arg, dmu_tx_t *tx)
3489eda14cbcSMatt Macy {
3490eda14cbcSMatt Macy 	dmu_recv_cookie_t *drc = arg;
3491eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
3492eda14cbcSMatt Macy 	int error;
3493eda14cbcSMatt Macy 
3494eda14cbcSMatt Macy 	ASSERT3P(drc->drc_ds->ds_owner, ==, dmu_recv_tag);
3495eda14cbcSMatt Macy 
3496271171e0SMartin Matuska 	if (drc->drc_heal) {
3497271171e0SMartin Matuska 		error = 0;
3498271171e0SMartin Matuska 	} else if (!drc->drc_newfs) {
3499eda14cbcSMatt Macy 		dsl_dataset_t *origin_head;
3500eda14cbcSMatt Macy 
3501eda14cbcSMatt Macy 		error = dsl_dataset_hold(dp, drc->drc_tofs, FTAG, &origin_head);
3502eda14cbcSMatt Macy 		if (error != 0)
3503eda14cbcSMatt Macy 			return (error);
3504eda14cbcSMatt Macy 		if (drc->drc_force) {
3505eda14cbcSMatt Macy 			/*
3506eda14cbcSMatt Macy 			 * We will destroy any snapshots in tofs (i.e. before
3507eda14cbcSMatt Macy 			 * origin_head) that are after the origin (which is
3508eda14cbcSMatt Macy 			 * the snap before drc_ds, because drc_ds can not
3509eda14cbcSMatt Macy 			 * have any snaps of its own).
3510eda14cbcSMatt Macy 			 */
3511eda14cbcSMatt Macy 			uint64_t obj;
3512eda14cbcSMatt Macy 
3513eda14cbcSMatt Macy 			obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
3514eda14cbcSMatt Macy 			while (obj !=
3515eda14cbcSMatt Macy 			    dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
3516eda14cbcSMatt Macy 				dsl_dataset_t *snap;
3517eda14cbcSMatt Macy 				error = dsl_dataset_hold_obj(dp, obj, FTAG,
3518eda14cbcSMatt Macy 				    &snap);
3519eda14cbcSMatt Macy 				if (error != 0)
3520eda14cbcSMatt Macy 					break;
3521eda14cbcSMatt Macy 				if (snap->ds_dir != origin_head->ds_dir)
3522eda14cbcSMatt Macy 					error = SET_ERROR(EINVAL);
3523eda14cbcSMatt Macy 				if (error == 0)  {
3524eda14cbcSMatt Macy 					error = dsl_destroy_snapshot_check_impl(
3525eda14cbcSMatt Macy 					    snap, B_FALSE);
3526eda14cbcSMatt Macy 				}
3527eda14cbcSMatt Macy 				obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
3528eda14cbcSMatt Macy 				dsl_dataset_rele(snap, FTAG);
3529eda14cbcSMatt Macy 				if (error != 0)
3530eda14cbcSMatt Macy 					break;
3531eda14cbcSMatt Macy 			}
3532eda14cbcSMatt Macy 			if (error != 0) {
3533eda14cbcSMatt Macy 				dsl_dataset_rele(origin_head, FTAG);
3534eda14cbcSMatt Macy 				return (error);
3535eda14cbcSMatt Macy 			}
3536eda14cbcSMatt Macy 		}
3537eda14cbcSMatt Macy 		if (drc->drc_keynvl != NULL) {
3538eda14cbcSMatt Macy 			error = dsl_crypto_recv_raw_key_check(drc->drc_ds,
3539eda14cbcSMatt Macy 			    drc->drc_keynvl, tx);
3540eda14cbcSMatt Macy 			if (error != 0) {
3541eda14cbcSMatt Macy 				dsl_dataset_rele(origin_head, FTAG);
3542eda14cbcSMatt Macy 				return (error);
3543eda14cbcSMatt Macy 			}
3544eda14cbcSMatt Macy 		}
3545eda14cbcSMatt Macy 
3546eda14cbcSMatt Macy 		error = dsl_dataset_clone_swap_check_impl(drc->drc_ds,
3547eda14cbcSMatt Macy 		    origin_head, drc->drc_force, drc->drc_owner, tx);
3548eda14cbcSMatt Macy 		if (error != 0) {
3549eda14cbcSMatt Macy 			dsl_dataset_rele(origin_head, FTAG);
3550eda14cbcSMatt Macy 			return (error);
3551eda14cbcSMatt Macy 		}
3552eda14cbcSMatt Macy 		error = dsl_dataset_snapshot_check_impl(origin_head,
3553eda14cbcSMatt Macy 		    drc->drc_tosnap, tx, B_TRUE, 1,
3554eda14cbcSMatt Macy 		    drc->drc_cred, drc->drc_proc);
3555eda14cbcSMatt Macy 		dsl_dataset_rele(origin_head, FTAG);
3556eda14cbcSMatt Macy 		if (error != 0)
3557eda14cbcSMatt Macy 			return (error);
3558eda14cbcSMatt Macy 
3559eda14cbcSMatt Macy 		error = dsl_destroy_head_check_impl(drc->drc_ds, 1);
3560eda14cbcSMatt Macy 	} else {
3561eda14cbcSMatt Macy 		error = dsl_dataset_snapshot_check_impl(drc->drc_ds,
3562eda14cbcSMatt Macy 		    drc->drc_tosnap, tx, B_TRUE, 1,
3563eda14cbcSMatt Macy 		    drc->drc_cred, drc->drc_proc);
3564eda14cbcSMatt Macy 	}
3565eda14cbcSMatt Macy 	return (error);
3566eda14cbcSMatt Macy }
3567eda14cbcSMatt Macy 
3568eda14cbcSMatt Macy static void
3569eda14cbcSMatt Macy dmu_recv_end_sync(void *arg, dmu_tx_t *tx)
3570eda14cbcSMatt Macy {
3571eda14cbcSMatt Macy 	dmu_recv_cookie_t *drc = arg;
3572eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
3573eda14cbcSMatt Macy 	boolean_t encrypted = drc->drc_ds->ds_dir->dd_crypto_obj != 0;
3574271171e0SMartin Matuska 	uint64_t newsnapobj = 0;
3575eda14cbcSMatt Macy 
3576eda14cbcSMatt Macy 	spa_history_log_internal_ds(drc->drc_ds, "finish receiving",
3577eda14cbcSMatt Macy 	    tx, "snap=%s", drc->drc_tosnap);
3578eda14cbcSMatt Macy 	drc->drc_ds->ds_objset->os_raw_receive = B_FALSE;
3579eda14cbcSMatt Macy 
3580271171e0SMartin Matuska 	if (drc->drc_heal) {
3581271171e0SMartin Matuska 		if (drc->drc_keynvl != NULL) {
3582271171e0SMartin Matuska 			nvlist_free(drc->drc_keynvl);
3583271171e0SMartin Matuska 			drc->drc_keynvl = NULL;
3584271171e0SMartin Matuska 		}
3585271171e0SMartin Matuska 	} else if (!drc->drc_newfs) {
3586eda14cbcSMatt Macy 		dsl_dataset_t *origin_head;
3587eda14cbcSMatt Macy 
3588eda14cbcSMatt Macy 		VERIFY0(dsl_dataset_hold(dp, drc->drc_tofs, FTAG,
3589eda14cbcSMatt Macy 		    &origin_head));
3590eda14cbcSMatt Macy 
3591eda14cbcSMatt Macy 		if (drc->drc_force) {
3592eda14cbcSMatt Macy 			/*
3593eda14cbcSMatt Macy 			 * Destroy any snapshots of drc_tofs (origin_head)
3594eda14cbcSMatt Macy 			 * after the origin (the snap before drc_ds).
3595eda14cbcSMatt Macy 			 */
3596eda14cbcSMatt Macy 			uint64_t obj;
3597eda14cbcSMatt Macy 
3598eda14cbcSMatt Macy 			obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
3599eda14cbcSMatt Macy 			while (obj !=
3600eda14cbcSMatt Macy 			    dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
3601eda14cbcSMatt Macy 				dsl_dataset_t *snap;
3602eda14cbcSMatt Macy 				VERIFY0(dsl_dataset_hold_obj(dp, obj, FTAG,
3603eda14cbcSMatt Macy 				    &snap));
3604eda14cbcSMatt Macy 				ASSERT3P(snap->ds_dir, ==, origin_head->ds_dir);
3605eda14cbcSMatt Macy 				obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
3606eda14cbcSMatt Macy 				dsl_destroy_snapshot_sync_impl(snap,
3607eda14cbcSMatt Macy 				    B_FALSE, tx);
3608eda14cbcSMatt Macy 				dsl_dataset_rele(snap, FTAG);
3609eda14cbcSMatt Macy 			}
3610eda14cbcSMatt Macy 		}
3611eda14cbcSMatt Macy 		if (drc->drc_keynvl != NULL) {
3612eda14cbcSMatt Macy 			dsl_crypto_recv_raw_key_sync(drc->drc_ds,
3613eda14cbcSMatt Macy 			    drc->drc_keynvl, tx);
3614eda14cbcSMatt Macy 			nvlist_free(drc->drc_keynvl);
3615eda14cbcSMatt Macy 			drc->drc_keynvl = NULL;
3616eda14cbcSMatt Macy 		}
3617eda14cbcSMatt Macy 
3618eda14cbcSMatt Macy 		VERIFY3P(drc->drc_ds->ds_prev, ==,
3619eda14cbcSMatt Macy 		    origin_head->ds_prev);
3620eda14cbcSMatt Macy 
3621eda14cbcSMatt Macy 		dsl_dataset_clone_swap_sync_impl(drc->drc_ds,
3622eda14cbcSMatt Macy 		    origin_head, tx);
3623eda14cbcSMatt Macy 		/*
3624eda14cbcSMatt Macy 		 * The objset was evicted by dsl_dataset_clone_swap_sync_impl,
3625eda14cbcSMatt Macy 		 * so drc_os is no longer valid.
3626eda14cbcSMatt Macy 		 */
3627eda14cbcSMatt Macy 		drc->drc_os = NULL;
3628eda14cbcSMatt Macy 
3629eda14cbcSMatt Macy 		dsl_dataset_snapshot_sync_impl(origin_head,
3630eda14cbcSMatt Macy 		    drc->drc_tosnap, tx);
3631eda14cbcSMatt Macy 
3632eda14cbcSMatt Macy 		/* set snapshot's creation time and guid */
3633eda14cbcSMatt Macy 		dmu_buf_will_dirty(origin_head->ds_prev->ds_dbuf, tx);
3634eda14cbcSMatt Macy 		dsl_dataset_phys(origin_head->ds_prev)->ds_creation_time =
3635eda14cbcSMatt Macy 		    drc->drc_drrb->drr_creation_time;
3636eda14cbcSMatt Macy 		dsl_dataset_phys(origin_head->ds_prev)->ds_guid =
3637eda14cbcSMatt Macy 		    drc->drc_drrb->drr_toguid;
3638eda14cbcSMatt Macy 		dsl_dataset_phys(origin_head->ds_prev)->ds_flags &=
3639eda14cbcSMatt Macy 		    ~DS_FLAG_INCONSISTENT;
3640eda14cbcSMatt Macy 
3641eda14cbcSMatt Macy 		dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
3642eda14cbcSMatt Macy 		dsl_dataset_phys(origin_head)->ds_flags &=
3643eda14cbcSMatt Macy 		    ~DS_FLAG_INCONSISTENT;
3644eda14cbcSMatt Macy 
3645eda14cbcSMatt Macy 		newsnapobj =
3646eda14cbcSMatt Macy 		    dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
3647eda14cbcSMatt Macy 
3648eda14cbcSMatt Macy 		dsl_dataset_rele(origin_head, FTAG);
3649eda14cbcSMatt Macy 		dsl_destroy_head_sync_impl(drc->drc_ds, tx);
3650eda14cbcSMatt Macy 
3651eda14cbcSMatt Macy 		if (drc->drc_owner != NULL)
3652eda14cbcSMatt Macy 			VERIFY3P(origin_head->ds_owner, ==, drc->drc_owner);
3653eda14cbcSMatt Macy 	} else {
3654eda14cbcSMatt Macy 		dsl_dataset_t *ds = drc->drc_ds;
3655eda14cbcSMatt Macy 
3656eda14cbcSMatt Macy 		dsl_dataset_snapshot_sync_impl(ds, drc->drc_tosnap, tx);
3657eda14cbcSMatt Macy 
3658eda14cbcSMatt Macy 		/* set snapshot's creation time and guid */
3659eda14cbcSMatt Macy 		dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
3660eda14cbcSMatt Macy 		dsl_dataset_phys(ds->ds_prev)->ds_creation_time =
3661eda14cbcSMatt Macy 		    drc->drc_drrb->drr_creation_time;
3662eda14cbcSMatt Macy 		dsl_dataset_phys(ds->ds_prev)->ds_guid =
3663eda14cbcSMatt Macy 		    drc->drc_drrb->drr_toguid;
3664eda14cbcSMatt Macy 		dsl_dataset_phys(ds->ds_prev)->ds_flags &=
3665eda14cbcSMatt Macy 		    ~DS_FLAG_INCONSISTENT;
3666eda14cbcSMatt Macy 
3667eda14cbcSMatt Macy 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
3668eda14cbcSMatt Macy 		dsl_dataset_phys(ds)->ds_flags &= ~DS_FLAG_INCONSISTENT;
3669eda14cbcSMatt Macy 		if (dsl_dataset_has_resume_receive_state(ds)) {
3670eda14cbcSMatt Macy 			(void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3671eda14cbcSMatt Macy 			    DS_FIELD_RESUME_FROMGUID, tx);
3672eda14cbcSMatt Macy 			(void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3673eda14cbcSMatt Macy 			    DS_FIELD_RESUME_OBJECT, tx);
3674eda14cbcSMatt Macy 			(void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3675eda14cbcSMatt Macy 			    DS_FIELD_RESUME_OFFSET, tx);
3676eda14cbcSMatt Macy 			(void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3677eda14cbcSMatt Macy 			    DS_FIELD_RESUME_BYTES, tx);
3678eda14cbcSMatt Macy 			(void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3679eda14cbcSMatt Macy 			    DS_FIELD_RESUME_TOGUID, tx);
3680eda14cbcSMatt Macy 			(void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3681eda14cbcSMatt Macy 			    DS_FIELD_RESUME_TONAME, tx);
3682eda14cbcSMatt Macy 			(void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3683eda14cbcSMatt Macy 			    DS_FIELD_RESUME_REDACT_BOOKMARK_SNAPS, tx);
3684eda14cbcSMatt Macy 		}
3685eda14cbcSMatt Macy 		newsnapobj =
3686eda14cbcSMatt Macy 		    dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj;
3687eda14cbcSMatt Macy 	}
3688eda14cbcSMatt Macy 
3689eda14cbcSMatt Macy 	/*
3690eda14cbcSMatt Macy 	 * If this is a raw receive, the crypt_keydata nvlist will include
3691eda14cbcSMatt Macy 	 * a to_ivset_guid for us to set on the new snapshot. This value
3692eda14cbcSMatt Macy 	 * will override the value generated by the snapshot code. However,
3693eda14cbcSMatt Macy 	 * this value may not be present, because older implementations of
3694eda14cbcSMatt Macy 	 * the raw send code did not include this value, and we are still
3695eda14cbcSMatt Macy 	 * allowed to receive them if the zfs_disable_ivset_guid_check
3696eda14cbcSMatt Macy 	 * tunable is set, in which case we will leave the newly-generated
3697eda14cbcSMatt Macy 	 * value.
3698eda14cbcSMatt Macy 	 */
3699271171e0SMartin Matuska 	if (!drc->drc_heal && drc->drc_raw && drc->drc_ivset_guid != 0) {
3700eda14cbcSMatt Macy 		dmu_object_zapify(dp->dp_meta_objset, newsnapobj,
3701eda14cbcSMatt Macy 		    DMU_OT_DSL_DATASET, tx);
3702eda14cbcSMatt Macy 		VERIFY0(zap_update(dp->dp_meta_objset, newsnapobj,
3703eda14cbcSMatt Macy 		    DS_FIELD_IVSET_GUID, sizeof (uint64_t), 1,
3704eda14cbcSMatt Macy 		    &drc->drc_ivset_guid, tx));
3705eda14cbcSMatt Macy 	}
3706eda14cbcSMatt Macy 
3707eda14cbcSMatt Macy 	/*
3708eda14cbcSMatt Macy 	 * Release the hold from dmu_recv_begin.  This must be done before
3709eda14cbcSMatt Macy 	 * we return to open context, so that when we free the dataset's dnode
3710eda14cbcSMatt Macy 	 * we can evict its bonus buffer. Since the dataset may be destroyed
3711eda14cbcSMatt Macy 	 * at this point (and therefore won't have a valid pointer to the spa)
3712eda14cbcSMatt Macy 	 * we release the key mapping manually here while we do have a valid
3713eda14cbcSMatt Macy 	 * pointer, if it exists.
3714eda14cbcSMatt Macy 	 */
3715eda14cbcSMatt Macy 	if (!drc->drc_raw && encrypted) {
3716eda14cbcSMatt Macy 		(void) spa_keystore_remove_mapping(dmu_tx_pool(tx)->dp_spa,
3717eda14cbcSMatt Macy 		    drc->drc_ds->ds_object, drc->drc_ds);
3718eda14cbcSMatt Macy 	}
3719eda14cbcSMatt Macy 	dsl_dataset_disown(drc->drc_ds, 0, dmu_recv_tag);
3720eda14cbcSMatt Macy 	drc->drc_ds = NULL;
3721eda14cbcSMatt Macy }
3722eda14cbcSMatt Macy 
3723eda14cbcSMatt Macy static int dmu_recv_end_modified_blocks = 3;
3724eda14cbcSMatt Macy 
3725eda14cbcSMatt Macy static int
3726eda14cbcSMatt Macy dmu_recv_existing_end(dmu_recv_cookie_t *drc)
3727eda14cbcSMatt Macy {
3728eda14cbcSMatt Macy #ifdef _KERNEL
3729eda14cbcSMatt Macy 	/*
3730eda14cbcSMatt Macy 	 * We will be destroying the ds; make sure its origin is unmounted if
3731eda14cbcSMatt Macy 	 * necessary.
3732eda14cbcSMatt Macy 	 */
3733eda14cbcSMatt Macy 	char name[ZFS_MAX_DATASET_NAME_LEN];
3734eda14cbcSMatt Macy 	dsl_dataset_name(drc->drc_ds, name);
3735eda14cbcSMatt Macy 	zfs_destroy_unmount_origin(name);
3736eda14cbcSMatt Macy #endif
3737eda14cbcSMatt Macy 
3738eda14cbcSMatt Macy 	return (dsl_sync_task(drc->drc_tofs,
3739eda14cbcSMatt Macy 	    dmu_recv_end_check, dmu_recv_end_sync, drc,
3740eda14cbcSMatt Macy 	    dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL));
3741eda14cbcSMatt Macy }
3742eda14cbcSMatt Macy 
3743eda14cbcSMatt Macy static int
3744eda14cbcSMatt Macy dmu_recv_new_end(dmu_recv_cookie_t *drc)
3745eda14cbcSMatt Macy {
3746eda14cbcSMatt Macy 	return (dsl_sync_task(drc->drc_tofs,
3747eda14cbcSMatt Macy 	    dmu_recv_end_check, dmu_recv_end_sync, drc,
3748eda14cbcSMatt Macy 	    dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL));
3749eda14cbcSMatt Macy }
3750eda14cbcSMatt Macy 
3751eda14cbcSMatt Macy int
3752eda14cbcSMatt Macy dmu_recv_end(dmu_recv_cookie_t *drc, void *owner)
3753eda14cbcSMatt Macy {
3754eda14cbcSMatt Macy 	int error;
3755eda14cbcSMatt Macy 
3756eda14cbcSMatt Macy 	drc->drc_owner = owner;
3757eda14cbcSMatt Macy 
3758eda14cbcSMatt Macy 	if (drc->drc_newfs)
3759eda14cbcSMatt Macy 		error = dmu_recv_new_end(drc);
3760eda14cbcSMatt Macy 	else
3761eda14cbcSMatt Macy 		error = dmu_recv_existing_end(drc);
3762eda14cbcSMatt Macy 
3763eda14cbcSMatt Macy 	if (error != 0) {
3764eda14cbcSMatt Macy 		dmu_recv_cleanup_ds(drc);
3765eda14cbcSMatt Macy 		nvlist_free(drc->drc_keynvl);
3766271171e0SMartin Matuska 	} else if (!drc->drc_heal) {
3767eda14cbcSMatt Macy 		if (drc->drc_newfs) {
3768eda14cbcSMatt Macy 			zvol_create_minor(drc->drc_tofs);
3769eda14cbcSMatt Macy 		}
3770eda14cbcSMatt Macy 		char *snapname = kmem_asprintf("%s@%s",
3771eda14cbcSMatt Macy 		    drc->drc_tofs, drc->drc_tosnap);
3772eda14cbcSMatt Macy 		zvol_create_minor(snapname);
3773eda14cbcSMatt Macy 		kmem_strfree(snapname);
3774eda14cbcSMatt Macy 	}
3775eda14cbcSMatt Macy 	return (error);
3776eda14cbcSMatt Macy }
3777eda14cbcSMatt Macy 
3778eda14cbcSMatt Macy /*
3779eda14cbcSMatt Macy  * Return TRUE if this objset is currently being received into.
3780eda14cbcSMatt Macy  */
3781eda14cbcSMatt Macy boolean_t
3782eda14cbcSMatt Macy dmu_objset_is_receiving(objset_t *os)
3783eda14cbcSMatt Macy {
3784eda14cbcSMatt Macy 	return (os->os_dsl_dataset != NULL &&
3785eda14cbcSMatt Macy 	    os->os_dsl_dataset->ds_owner == dmu_recv_tag);
3786eda14cbcSMatt Macy }
3787eda14cbcSMatt Macy 
3788be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_recv, zfs_recv_, queue_length, UINT, ZMOD_RW,
3789eda14cbcSMatt Macy 	"Maximum receive queue length");
3790eda14cbcSMatt Macy 
3791be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_recv, zfs_recv_, queue_ff, UINT, ZMOD_RW,
3792eda14cbcSMatt Macy 	"Receive queue fill fraction");
3793eda14cbcSMatt Macy 
3794be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_recv, zfs_recv_, write_batch_size, UINT, ZMOD_RW,
3795eda14cbcSMatt Macy 	"Maximum amount of writes to batch into one transaction");
3796271171e0SMartin Matuska 
3797271171e0SMartin Matuska ZFS_MODULE_PARAM(zfs_recv, zfs_recv_, best_effort_corrective, INT, ZMOD_RW,
3798271171e0SMartin Matuska 	"Ignore errors during corrective receive");
3799271171e0SMartin Matuska /* END CSTYLED */
3800