]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/dmu_send.c
Unify behavior of deadman parameters
[mirror_zfs.git] / module / zfs / dmu_send.c
CommitLineData
34dc7c2f
BB
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
428870ff 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
8d35c149 23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
e6d3a843 24 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
788eb90c 25 * Copyright (c) 2014, Joyent, Inc. All rights reserved.
47dfff3b 26 * Copyright 2014 HybridCluster. All rights reserved.
b607405f 27 * Copyright 2016 RackTop Systems.
a0bd735a 28 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
8d35c149 29 */
34dc7c2f 30
34dc7c2f
BB
31#include <sys/dmu.h>
32#include <sys/dmu_impl.h>
33#include <sys/dmu_tx.h>
34#include <sys/dbuf.h>
35#include <sys/dnode.h>
36#include <sys/zfs_context.h>
37#include <sys/dmu_objset.h>
38#include <sys/dmu_traverse.h>
39#include <sys/dsl_dataset.h>
40#include <sys/dsl_dir.h>
428870ff 41#include <sys/dsl_prop.h>
34dc7c2f
BB
42#include <sys/dsl_pool.h>
43#include <sys/dsl_synctask.h>
044baf00 44#include <sys/spa_impl.h>
34dc7c2f
BB
45#include <sys/zfs_ioctl.h>
46#include <sys/zap.h>
47#include <sys/zio_checksum.h>
428870ff
BB
48#include <sys/zfs_znode.h>
49#include <zfs_fletcher.h>
50#include <sys/avl.h>
51#include <sys/ddt.h>
572e2857 52#include <sys/zfs_onexit.h>
13fe0198
MA
53#include <sys/dmu_send.h>
54#include <sys/dsl_destroy.h>
9b67f605 55#include <sys/blkptr.h>
da536844 56#include <sys/dsl_bookmark.h>
9b67f605 57#include <sys/zfeature.h>
fcff0f35 58#include <sys/bqueue.h>
a0bd735a 59#include <sys/zvol.h>
f74b821a 60#include <sys/policy.h>
34dc7c2f 61
330d06f9
MA
62/* Set this tunable to TRUE to replace corrupt data with 0x2f5baddb10c */
63int zfs_send_corrupt_data = B_FALSE;
3b0d9928
BB
64int zfs_send_queue_length = SPA_MAXBLOCKSIZE;
65int zfs_recv_queue_length = SPA_MAXBLOCKSIZE;
b607405f
AS
66/* Set this tunable to FALSE to disable setting of DRR_FLAG_FREERECORDS */
67int zfs_send_set_freerecords_bit = B_TRUE;
330d06f9 68
34dc7c2f 69static char *dmu_recv_tag = "dmu_recv_tag";
47dfff3b 70const char *recv_clone_name = "%recv";
34dc7c2f 71
ca0845d5
PD
72/*
73 * Use this to override the recordsize calculation for fast zfs send estimates.
74 */
75unsigned long zfs_override_estimate_recordsize = 0;
76
fcff0f35
PD
77#define BP_SPAN(datablkszsec, indblkshift, level) \
78 (((uint64_t)datablkszsec) << (SPA_MINBLOCKSHIFT + \
79 (level) * (indblkshift - SPA_BLKPTRSHIFT)))
80
47dfff3b
MA
81static void byteswap_record(dmu_replay_record_t *drr);
82
fcff0f35
PD
83struct send_thread_arg {
84 bqueue_t q;
85 dsl_dataset_t *ds; /* Dataset to traverse */
86 uint64_t fromtxg; /* Traverse from this txg */
87 int flags; /* flags to pass to traverse_dataset */
88 int error_code;
89 boolean_t cancel;
47dfff3b 90 zbookmark_phys_t resume;
fcff0f35
PD
91};
92
93struct send_block_record {
94 boolean_t eos_marker; /* Marks the end of the stream */
95 blkptr_t bp;
96 zbookmark_phys_t zb;
97 uint8_t indblkshift;
98 uint16_t datablkszsec;
99 bqueue_node_t ln;
100};
101
044baf00
BB
102typedef struct dump_bytes_io {
103 dmu_sendarg_t *dbi_dsp;
104 void *dbi_buf;
105 int dbi_len;
106} dump_bytes_io_t;
107
108static void
b58986ee 109dump_bytes_cb(void *arg)
34dc7c2f 110{
044baf00
BB
111 dump_bytes_io_t *dbi = (dump_bytes_io_t *)arg;
112 dmu_sendarg_t *dsp = dbi->dbi_dsp;
47dfff3b 113 dsl_dataset_t *ds = dmu_objset_ds(dsp->dsa_os);
34dc7c2f 114 ssize_t resid; /* have to get resid to get detailed errno */
f8866f8a
ER
115
116 /*
b5256303 117 * The code does not rely on len being a multiple of 8. We keep
f8866f8a
ER
118 * this assertion because of the corresponding assertion in
119 * receive_read(). Keeping this assertion ensures that we do not
120 * inadvertently break backwards compatibility (causing the assertion
b5256303
TC
121 * in receive_read() to trigger on old software). Newer feature flags
122 * (such as raw send) may break this assertion since they were
123 * introduced after the requirement was made obsolete.
f8866f8a
ER
124 */
125
b5256303
TC
126 ASSERT(dbi->dbi_len % 8 == 0 ||
127 (dsp->dsa_featureflags & DMU_BACKUP_FEATURE_RAW) != 0);
34dc7c2f 128
37abac6d 129 dsp->dsa_err = vn_rdwr(UIO_WRITE, dsp->dsa_vp,
044baf00 130 (caddr_t)dbi->dbi_buf, dbi->dbi_len,
34dc7c2f 131 0, UIO_SYSSPACE, FAPPEND, RLIM64_INFINITY, CRED(), &resid);
37abac6d
BP
132
133 mutex_enter(&ds->ds_sendstream_lock);
044baf00 134 *dsp->dsa_off += dbi->dbi_len;
37abac6d 135 mutex_exit(&ds->ds_sendstream_lock);
044baf00
BB
136}
137
138static int
139dump_bytes(dmu_sendarg_t *dsp, void *buf, int len)
140{
141 dump_bytes_io_t dbi;
142
143 dbi.dbi_dsp = dsp;
144 dbi.dbi_buf = buf;
145 dbi.dbi_len = len;
146
b58986ee
BB
147#if defined(HAVE_LARGE_STACKS)
148 dump_bytes_cb(&dbi);
149#else
044baf00
BB
150 /*
151 * The vn_rdwr() call is performed in a taskq to ensure that there is
152 * always enough stack space to write safely to the target filesystem.
153 * The ZIO_TYPE_FREE threads are used because there can be a lot of
154 * them and they are used in vdev_file.c for a similar purpose.
155 */
156 spa_taskq_dispatch_sync(dmu_objset_spa(dsp->dsa_os), ZIO_TYPE_FREE,
b58986ee
BB
157 ZIO_TASKQ_ISSUE, dump_bytes_cb, &dbi, TQ_SLEEP);
158#endif /* HAVE_LARGE_STACKS */
37abac6d
BP
159
160 return (dsp->dsa_err);
34dc7c2f
BB
161}
162
37f8a883
MA
163/*
164 * For all record types except BEGIN, fill in the checksum (overlaid in
165 * drr_u.drr_checksum.drr_checksum). The checksum verifies everything
166 * up to the start of the checksum itself.
167 */
168static int
169dump_record(dmu_sendarg_t *dsp, void *payload, int payload_len)
170{
171 ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
172 ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
a6255b7f 173 (void) fletcher_4_incremental_native(dsp->dsa_drr,
37f8a883
MA
174 offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
175 &dsp->dsa_zc);
51907a31
K
176 if (dsp->dsa_drr->drr_type == DRR_BEGIN) {
177 dsp->dsa_sent_begin = B_TRUE;
178 } else {
37f8a883
MA
179 ASSERT(ZIO_CHECKSUM_IS_ZERO(&dsp->dsa_drr->drr_u.
180 drr_checksum.drr_checksum));
181 dsp->dsa_drr->drr_u.drr_checksum.drr_checksum = dsp->dsa_zc;
182 }
51907a31
K
183 if (dsp->dsa_drr->drr_type == DRR_END) {
184 dsp->dsa_sent_end = B_TRUE;
185 }
a6255b7f 186 (void) fletcher_4_incremental_native(&dsp->dsa_drr->
37f8a883
MA
187 drr_u.drr_checksum.drr_checksum,
188 sizeof (zio_cksum_t), &dsp->dsa_zc);
189 if (dump_bytes(dsp, dsp->dsa_drr, sizeof (dmu_replay_record_t)) != 0)
190 return (SET_ERROR(EINTR));
191 if (payload_len != 0) {
a6255b7f 192 (void) fletcher_4_incremental_native(payload, payload_len,
37f8a883
MA
193 &dsp->dsa_zc);
194 if (dump_bytes(dsp, payload, payload_len) != 0)
195 return (SET_ERROR(EINTR));
196 }
197 return (0);
198}
199
e6d3a843
PD
200/*
201 * Fill in the drr_free struct, or perform aggregation if the previous record is
202 * also a free record, and the two are adjacent.
203 *
204 * Note that we send free records even for a full send, because we want to be
205 * able to receive a full send as a clone, which requires a list of all the free
206 * and freeobject records that were generated on the source.
207 */
34dc7c2f 208static int
37abac6d 209dump_free(dmu_sendarg_t *dsp, uint64_t object, uint64_t offset,
34dc7c2f
BB
210 uint64_t length)
211{
37abac6d 212 struct drr_free *drrf = &(dsp->dsa_drr->drr_u.drr_free);
428870ff 213
ea97f8ce
MA
214 /*
215 * When we receive a free record, dbuf_free_range() assumes
216 * that the receiving system doesn't have any dbufs in the range
217 * being freed. This is always true because there is a one-record
218 * constraint: we only send one WRITE record for any given
47dfff3b 219 * object,offset. We know that the one-record constraint is
ea97f8ce
MA
220 * true because we always send data in increasing order by
221 * object,offset.
222 *
223 * If the increasing-order constraint ever changes, we should find
224 * another way to assert that the one-record constraint is still
225 * satisfied.
226 */
227 ASSERT(object > dsp->dsa_last_data_object ||
228 (object == dsp->dsa_last_data_object &&
229 offset > dsp->dsa_last_data_offset));
230
428870ff
BB
231 /*
232 * If there is a pending op, but it's not PENDING_FREE, push it out,
233 * since free block aggregation can only be done for blocks of the
234 * same type (i.e., DRR_FREE records can only be aggregated with
235 * other DRR_FREE records. DRR_FREEOBJECTS records can only be
236 * aggregated with other DRR_FREEOBJECTS records.
237 */
37abac6d
BP
238 if (dsp->dsa_pending_op != PENDING_NONE &&
239 dsp->dsa_pending_op != PENDING_FREE) {
37f8a883 240 if (dump_record(dsp, NULL, 0) != 0)
2e528b49 241 return (SET_ERROR(EINTR));
37abac6d 242 dsp->dsa_pending_op = PENDING_NONE;
428870ff
BB
243 }
244
37abac6d 245 if (dsp->dsa_pending_op == PENDING_FREE) {
428870ff 246 /*
ee45fbd8 247 * There should never be a PENDING_FREE if length is
248 * DMU_OBJECT_END (because dump_dnode is the only place where
249 * this function is called with a DMU_OBJECT_END, and only after
250 * flushing any pending record).
428870ff 251 */
ee45fbd8 252 ASSERT(length != DMU_OBJECT_END);
428870ff
BB
253 /*
254 * Check to see whether this free block can be aggregated
255 * with pending one.
256 */
257 if (drrf->drr_object == object && drrf->drr_offset +
258 drrf->drr_length == offset) {
ee45fbd8 259 if (offset + length < offset)
260 drrf->drr_length = DMU_OBJECT_END;
261 else
262 drrf->drr_length += length;
428870ff
BB
263 return (0);
264 } else {
265 /* not a continuation. Push out pending record */
37f8a883 266 if (dump_record(dsp, NULL, 0) != 0)
2e528b49 267 return (SET_ERROR(EINTR));
37abac6d 268 dsp->dsa_pending_op = PENDING_NONE;
428870ff
BB
269 }
270 }
271 /* create a FREE record and make it pending */
37abac6d
BP
272 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
273 dsp->dsa_drr->drr_type = DRR_FREE;
428870ff
BB
274 drrf->drr_object = object;
275 drrf->drr_offset = offset;
ee45fbd8 276 if (offset + length < offset)
277 drrf->drr_length = DMU_OBJECT_END;
278 else
279 drrf->drr_length = length;
37abac6d 280 drrf->drr_toguid = dsp->dsa_toguid;
ee45fbd8 281 if (length == DMU_OBJECT_END) {
37f8a883 282 if (dump_record(dsp, NULL, 0) != 0)
2e528b49 283 return (SET_ERROR(EINTR));
428870ff 284 } else {
37abac6d 285 dsp->dsa_pending_op = PENDING_FREE;
428870ff 286 }
34dc7c2f 287
34dc7c2f
BB
288 return (0);
289}
290
291static int
b5256303
TC
292dump_write(dmu_sendarg_t *dsp, dmu_object_type_t type, uint64_t object,
293 uint64_t offset, int lsize, int psize, const blkptr_t *bp, void *data)
34dc7c2f 294{
2aa34383 295 uint64_t payload_size;
b5256303 296 boolean_t raw = (dsp->dsa_featureflags & DMU_BACKUP_FEATURE_RAW);
37abac6d 297 struct drr_write *drrw = &(dsp->dsa_drr->drr_u.drr_write);
428870ff 298
ea97f8ce
MA
299 /*
300 * We send data in increasing object, offset order.
301 * See comment in dump_free() for details.
302 */
303 ASSERT(object > dsp->dsa_last_data_object ||
304 (object == dsp->dsa_last_data_object &&
305 offset > dsp->dsa_last_data_offset));
306 dsp->dsa_last_data_object = object;
2aa34383 307 dsp->dsa_last_data_offset = offset + lsize - 1;
428870ff
BB
308
309 /*
310 * If there is any kind of pending aggregation (currently either
311 * a grouping of free objects or free blocks), push it out to
312 * the stream, since aggregation can't be done across operations
313 * of different types.
314 */
37abac6d 315 if (dsp->dsa_pending_op != PENDING_NONE) {
37f8a883 316 if (dump_record(dsp, NULL, 0) != 0)
2e528b49 317 return (SET_ERROR(EINTR));
37abac6d 318 dsp->dsa_pending_op = PENDING_NONE;
428870ff 319 }
37f8a883 320 /* write a WRITE record */
37abac6d
BP
321 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
322 dsp->dsa_drr->drr_type = DRR_WRITE;
428870ff
BB
323 drrw->drr_object = object;
324 drrw->drr_type = type;
325 drrw->drr_offset = offset;
37abac6d 326 drrw->drr_toguid = dsp->dsa_toguid;
2aa34383
DK
327 drrw->drr_logical_size = lsize;
328
b5256303
TC
329 /* only set the compression fields if the buf is compressed or raw */
330 if (raw || lsize != psize) {
2aa34383 331 ASSERT(!BP_IS_EMBEDDED(bp));
2aa34383 332 ASSERT3S(psize, >, 0);
2aa34383 333
b5256303
TC
334 if (raw) {
335 ASSERT(BP_IS_PROTECTED(bp));
336
337 /*
9b840763
TC
338 * This is a raw protected block so we need to pass
339 * along everything the receiving side will need to
340 * interpret this block, including the byteswap, salt,
341 * IV, and MAC.
b5256303 342 */
b5256303
TC
343 if (BP_SHOULD_BYTESWAP(bp))
344 drrw->drr_flags |= DRR_RAW_BYTESWAP;
345 zio_crypt_decode_params_bp(bp, drrw->drr_salt,
346 drrw->drr_iv);
347 zio_crypt_decode_mac_bp(bp, drrw->drr_mac);
348 } else {
349 /* this is a compressed block */
350 ASSERT(dsp->dsa_featureflags &
351 DMU_BACKUP_FEATURE_COMPRESSED);
352 ASSERT(!BP_SHOULD_BYTESWAP(bp));
353 ASSERT(!DMU_OT_IS_METADATA(BP_GET_TYPE(bp)));
354 ASSERT3U(BP_GET_COMPRESS(bp), !=, ZIO_COMPRESS_OFF);
355 ASSERT3S(lsize, >=, psize);
356 }
357
358 /* set fields common to compressed and raw sends */
2aa34383
DK
359 drrw->drr_compressiontype = BP_GET_COMPRESS(bp);
360 drrw->drr_compressed_size = psize;
361 payload_size = drrw->drr_compressed_size;
362 } else {
363 payload_size = drrw->drr_logical_size;
364 }
365
b5256303 366 if (bp == NULL || BP_IS_EMBEDDED(bp) || (BP_IS_PROTECTED(bp) && !raw)) {
9b67f605 367 /*
b5256303
TC
368 * There's no pre-computed checksum for partial-block writes,
369 * embedded BP's, or encrypted BP's that are being sent as
370 * plaintext, so (like fletcher4-checkummed blocks) userland
371 * will have to compute a dedup-capable checksum itself.
9b67f605
MA
372 */
373 drrw->drr_checksumtype = ZIO_CHECKSUM_OFF;
374 } else {
375 drrw->drr_checksumtype = BP_GET_CHECKSUM(bp);
3c67d83a
TH
376 if (zio_checksum_table[drrw->drr_checksumtype].ci_flags &
377 ZCHECKSUM_FLAG_DEDUP)
b5256303 378 drrw->drr_flags |= DRR_CHECKSUM_DEDUP;
9b67f605
MA
379 DDK_SET_LSIZE(&drrw->drr_key, BP_GET_LSIZE(bp));
380 DDK_SET_PSIZE(&drrw->drr_key, BP_GET_PSIZE(bp));
381 DDK_SET_COMPRESS(&drrw->drr_key, BP_GET_COMPRESS(bp));
b5256303 382 DDK_SET_CRYPT(&drrw->drr_key, BP_IS_PROTECTED(bp));
9b67f605
MA
383 drrw->drr_key.ddk_cksum = bp->blk_cksum;
384 }
428870ff 385
2aa34383 386 if (dump_record(dsp, data, payload_size) != 0)
2e528b49 387 return (SET_ERROR(EINTR));
428870ff
BB
388 return (0);
389}
390
9b67f605
MA
391static int
392dump_write_embedded(dmu_sendarg_t *dsp, uint64_t object, uint64_t offset,
393 int blksz, const blkptr_t *bp)
394{
395 char buf[BPE_PAYLOAD_SIZE];
396 struct drr_write_embedded *drrw =
397 &(dsp->dsa_drr->drr_u.drr_write_embedded);
398
399 if (dsp->dsa_pending_op != PENDING_NONE) {
37f8a883 400 if (dump_record(dsp, NULL, 0) != 0)
ecb2b7dc 401 return (SET_ERROR(EINTR));
9b67f605
MA
402 dsp->dsa_pending_op = PENDING_NONE;
403 }
404
405 ASSERT(BP_IS_EMBEDDED(bp));
406
407 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
408 dsp->dsa_drr->drr_type = DRR_WRITE_EMBEDDED;
409 drrw->drr_object = object;
410 drrw->drr_offset = offset;
411 drrw->drr_length = blksz;
412 drrw->drr_toguid = dsp->dsa_toguid;
413 drrw->drr_compression = BP_GET_COMPRESS(bp);
414 drrw->drr_etype = BPE_GET_ETYPE(bp);
415 drrw->drr_lsize = BPE_GET_LSIZE(bp);
416 drrw->drr_psize = BPE_GET_PSIZE(bp);
417
418 decode_embedded_bp_compressed(bp, buf);
419
37f8a883 420 if (dump_record(dsp, buf, P2ROUNDUP(drrw->drr_psize, 8)) != 0)
ecb2b7dc 421 return (SET_ERROR(EINTR));
9b67f605
MA
422 return (0);
423}
424
428870ff 425static int
b5256303 426dump_spill(dmu_sendarg_t *dsp, const blkptr_t *bp, uint64_t object, void *data)
428870ff 427{
37abac6d 428 struct drr_spill *drrs = &(dsp->dsa_drr->drr_u.drr_spill);
b5256303 429 uint64_t blksz = BP_GET_LSIZE(bp);
b0ee5946 430 uint64_t payload_size = blksz;
428870ff 431
37abac6d 432 if (dsp->dsa_pending_op != PENDING_NONE) {
37f8a883 433 if (dump_record(dsp, NULL, 0) != 0)
2e528b49 434 return (SET_ERROR(EINTR));
37abac6d 435 dsp->dsa_pending_op = PENDING_NONE;
428870ff
BB
436 }
437
438 /* write a SPILL record */
37abac6d
BP
439 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
440 dsp->dsa_drr->drr_type = DRR_SPILL;
428870ff
BB
441 drrs->drr_object = object;
442 drrs->drr_length = blksz;
37abac6d 443 drrs->drr_toguid = dsp->dsa_toguid;
34dc7c2f 444
b5256303 445 /* handle raw send fields */
9b840763
TC
446 if (dsp->dsa_featureflags & DMU_BACKUP_FEATURE_RAW) {
447 ASSERT(BP_IS_PROTECTED(bp));
448
b5256303
TC
449 if (BP_SHOULD_BYTESWAP(bp))
450 drrs->drr_flags |= DRR_RAW_BYTESWAP;
451 drrs->drr_compressiontype = BP_GET_COMPRESS(bp);
452 drrs->drr_compressed_size = BP_GET_PSIZE(bp);
453 zio_crypt_decode_params_bp(bp, drrs->drr_salt, drrs->drr_iv);
454 zio_crypt_decode_mac_bp(bp, drrs->drr_mac);
b0ee5946 455 payload_size = drrs->drr_compressed_size;
b5256303
TC
456 }
457
b0ee5946 458 if (dump_record(dsp, data, payload_size) != 0)
2e528b49 459 return (SET_ERROR(EINTR));
34dc7c2f
BB
460 return (0);
461}
462
463static int
37abac6d 464dump_freeobjects(dmu_sendarg_t *dsp, uint64_t firstobj, uint64_t numobjs)
34dc7c2f 465{
37abac6d 466 struct drr_freeobjects *drrfo = &(dsp->dsa_drr->drr_u.drr_freeobjects);
829e95c4
FG
467 uint64_t maxobj = DNODES_PER_BLOCK *
468 (DMU_META_DNODE(dsp->dsa_os)->dn_maxblkid + 1);
469
470 /*
471 * ZoL < 0.7 does not handle large FREEOBJECTS records correctly,
472 * leading to zfs recv never completing. to avoid this issue, don't
473 * send FREEOBJECTS records for object IDs which cannot exist on the
474 * receiving side.
475 */
476 if (maxobj > 0) {
477 if (maxobj < firstobj)
478 return (0);
479
480 if (maxobj < firstobj + numobjs)
481 numobjs = maxobj - firstobj;
482 }
428870ff
BB
483
484 /*
485 * If there is a pending op, but it's not PENDING_FREEOBJECTS,
486 * push it out, since free block aggregation can only be done for
487 * blocks of the same type (i.e., DRR_FREE records can only be
488 * aggregated with other DRR_FREE records. DRR_FREEOBJECTS records
489 * can only be aggregated with other DRR_FREEOBJECTS records.
490 */
37abac6d
BP
491 if (dsp->dsa_pending_op != PENDING_NONE &&
492 dsp->dsa_pending_op != PENDING_FREEOBJECTS) {
37f8a883 493 if (dump_record(dsp, NULL, 0) != 0)
2e528b49 494 return (SET_ERROR(EINTR));
37abac6d 495 dsp->dsa_pending_op = PENDING_NONE;
428870ff 496 }
37abac6d 497 if (dsp->dsa_pending_op == PENDING_FREEOBJECTS) {
428870ff
BB
498 /*
499 * See whether this free object array can be aggregated
500 * with pending one
501 */
502 if (drrfo->drr_firstobj + drrfo->drr_numobjs == firstobj) {
503 drrfo->drr_numobjs += numobjs;
504 return (0);
505 } else {
506 /* can't be aggregated. Push out pending record */
37f8a883 507 if (dump_record(dsp, NULL, 0) != 0)
2e528b49 508 return (SET_ERROR(EINTR));
37abac6d 509 dsp->dsa_pending_op = PENDING_NONE;
428870ff
BB
510 }
511 }
512
34dc7c2f 513 /* write a FREEOBJECTS record */
37abac6d
BP
514 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
515 dsp->dsa_drr->drr_type = DRR_FREEOBJECTS;
428870ff
BB
516 drrfo->drr_firstobj = firstobj;
517 drrfo->drr_numobjs = numobjs;
37abac6d 518 drrfo->drr_toguid = dsp->dsa_toguid;
428870ff 519
37abac6d 520 dsp->dsa_pending_op = PENDING_FREEOBJECTS;
34dc7c2f 521
34dc7c2f
BB
522 return (0);
523}
524
525static int
b5256303
TC
526dump_dnode(dmu_sendarg_t *dsp, const blkptr_t *bp, uint64_t object,
527 dnode_phys_t *dnp)
34dc7c2f 528{
37abac6d 529 struct drr_object *drro = &(dsp->dsa_drr->drr_u.drr_object);
4807c0ba 530 int bonuslen;
428870ff 531
47dfff3b
MA
532 if (object < dsp->dsa_resume_object) {
533 /*
534 * Note: when resuming, we will visit all the dnodes in
535 * the block of dnodes that we are resuming from. In
536 * this case it's unnecessary to send the dnodes prior to
537 * the one we are resuming from. We should be at most one
538 * block's worth of dnodes behind the resume point.
539 */
540 ASSERT3U(dsp->dsa_resume_object - object, <,
541 1 << (DNODE_BLOCK_SHIFT - DNODE_SHIFT));
542 return (0);
543 }
544
34dc7c2f 545 if (dnp == NULL || dnp->dn_type == DMU_OT_NONE)
37abac6d 546 return (dump_freeobjects(dsp, object, 1));
34dc7c2f 547
37abac6d 548 if (dsp->dsa_pending_op != PENDING_NONE) {
37f8a883 549 if (dump_record(dsp, NULL, 0) != 0)
2e528b49 550 return (SET_ERROR(EINTR));
37abac6d 551 dsp->dsa_pending_op = PENDING_NONE;
428870ff
BB
552 }
553
34dc7c2f 554 /* write an OBJECT record */
37abac6d
BP
555 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
556 dsp->dsa_drr->drr_type = DRR_OBJECT;
428870ff
BB
557 drro->drr_object = object;
558 drro->drr_type = dnp->dn_type;
559 drro->drr_bonustype = dnp->dn_bonustype;
560 drro->drr_blksz = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
561 drro->drr_bonuslen = dnp->dn_bonuslen;
50c957f7 562 drro->drr_dn_slots = dnp->dn_extra_slots + 1;
428870ff
BB
563 drro->drr_checksumtype = dnp->dn_checksum;
564 drro->drr_compress = dnp->dn_compress;
37abac6d 565 drro->drr_toguid = dsp->dsa_toguid;
428870ff 566
f1512ee6
MA
567 if (!(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
568 drro->drr_blksz > SPA_OLD_MAXBLOCKSIZE)
569 drro->drr_blksz = SPA_OLD_MAXBLOCKSIZE;
570
4807c0ba
TC
571 bonuslen = P2ROUNDUP(dnp->dn_bonuslen, 8);
572
9b840763
TC
573 if ((dsp->dsa_featureflags & DMU_BACKUP_FEATURE_RAW)) {
574 ASSERT(BP_IS_ENCRYPTED(bp));
575
b5256303
TC
576 if (BP_SHOULD_BYTESWAP(bp))
577 drro->drr_flags |= DRR_RAW_BYTESWAP;
578
579 /* needed for reconstructing dnp on recv side */
ae76f45c 580 drro->drr_maxblkid = dnp->dn_maxblkid;
b5256303
TC
581 drro->drr_indblkshift = dnp->dn_indblkshift;
582 drro->drr_nlevels = dnp->dn_nlevels;
583 drro->drr_nblkptr = dnp->dn_nblkptr;
584
585 /*
586 * Since we encrypt the entire bonus area, the (raw) part
4807c0ba 587 * beyond the bonuslen is actually nonzero, so we need
b5256303
TC
588 * to send it.
589 */
590 if (bonuslen != 0) {
591 drro->drr_raw_bonuslen = DN_MAX_BONUS_LEN(dnp);
592 bonuslen = drro->drr_raw_bonuslen;
593 }
37f8a883 594 }
34dc7c2f 595
b5256303
TC
596 if (dump_record(dsp, DN_BONUS(dnp), bonuslen) != 0)
597 return (SET_ERROR(EINTR));
598
ea97f8ce 599 /* Free anything past the end of the file. */
37abac6d 600 if (dump_free(dsp, object, (dnp->dn_maxblkid + 1) *
ee45fbd8 601 (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT), DMU_OBJECT_END) != 0)
2e528b49 602 return (SET_ERROR(EINTR));
13fe0198 603 if (dsp->dsa_err != 0)
2e528b49 604 return (SET_ERROR(EINTR));
34dc7c2f
BB
605 return (0);
606}
607
b5256303
TC
608static int
609dump_object_range(dmu_sendarg_t *dsp, const blkptr_t *bp, uint64_t firstobj,
610 uint64_t numslots)
611{
612 struct drr_object_range *drror =
613 &(dsp->dsa_drr->drr_u.drr_object_range);
614
615 /* we only use this record type for raw sends */
616 ASSERT(BP_IS_PROTECTED(bp));
617 ASSERT(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_RAW);
618 ASSERT3U(BP_GET_COMPRESS(bp), ==, ZIO_COMPRESS_OFF);
619 ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_DNODE);
620 ASSERT0(BP_GET_LEVEL(bp));
621
622 if (dsp->dsa_pending_op != PENDING_NONE) {
623 if (dump_record(dsp, NULL, 0) != 0)
624 return (SET_ERROR(EINTR));
625 dsp->dsa_pending_op = PENDING_NONE;
626 }
627
628 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
629 dsp->dsa_drr->drr_type = DRR_OBJECT_RANGE;
630 drror->drr_firstobj = firstobj;
631 drror->drr_numslots = numslots;
632 drror->drr_toguid = dsp->dsa_toguid;
b5256303
TC
633 if (BP_SHOULD_BYTESWAP(bp))
634 drror->drr_flags |= DRR_RAW_BYTESWAP;
635 zio_crypt_decode_params_bp(bp, drror->drr_salt, drror->drr_iv);
636 zio_crypt_decode_mac_bp(bp, drror->drr_mac);
637
638 if (dump_record(dsp, NULL, 0) != 0)
639 return (SET_ERROR(EINTR));
640 return (0);
641}
642
9b67f605
MA
643static boolean_t
644backup_do_embed(dmu_sendarg_t *dsp, const blkptr_t *bp)
645{
646 if (!BP_IS_EMBEDDED(bp))
647 return (B_FALSE);
648
649 /*
650 * Compression function must be legacy, or explicitly enabled.
651 */
652 if ((BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_LEGACY_FUNCTIONS &&
2aa34383 653 !(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_LZ4)))
9b67f605
MA
654 return (B_FALSE);
655
656 /*
657 * Embed type must be explicitly enabled.
658 */
659 switch (BPE_GET_ETYPE(bp)) {
660 case BP_EMBEDDED_TYPE_DATA:
661 if (dsp->dsa_featureflags & DMU_BACKUP_FEATURE_EMBED_DATA)
662 return (B_TRUE);
663 break;
664 default:
665 return (B_FALSE);
666 }
667 return (B_FALSE);
668}
669
fcff0f35
PD
670/*
671 * This is the callback function to traverse_dataset that acts as the worker
672 * thread for dmu_send_impl.
673 */
674/*ARGSUSED*/
675static int
676send_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
677 const zbookmark_phys_t *zb, const struct dnode_phys *dnp, void *arg)
678{
679 struct send_thread_arg *sta = arg;
680 struct send_block_record *record;
681 uint64_t record_size;
682 int err = 0;
683
47dfff3b
MA
684 ASSERT(zb->zb_object == DMU_META_DNODE_OBJECT ||
685 zb->zb_object >= sta->resume.zb_object);
b5256303 686 ASSERT3P(sta->ds, !=, NULL);
47dfff3b 687
fcff0f35
PD
688 if (sta->cancel)
689 return (SET_ERROR(EINTR));
34dc7c2f 690
fcff0f35
PD
691 if (bp == NULL) {
692 ASSERT3U(zb->zb_level, ==, ZB_DNODE_LEVEL);
693 return (0);
694 } else if (zb->zb_level < 0) {
695 return (0);
696 }
697
698 record = kmem_zalloc(sizeof (struct send_block_record), KM_SLEEP);
699 record->eos_marker = B_FALSE;
700 record->bp = *bp;
701 record->zb = *zb;
702 record->indblkshift = dnp->dn_indblkshift;
703 record->datablkszsec = dnp->dn_datablkszsec;
704 record_size = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
705 bqueue_enqueue(&sta->q, record, record_size);
706
707 return (err);
708}
709
710/*
711 * This function kicks off the traverse_dataset. It also handles setting the
712 * error code of the thread in case something goes wrong, and pushes the End of
713 * Stream record when the traverse_dataset call has finished. If there is no
714 * dataset to traverse, the thread immediately pushes End of Stream marker.
715 */
716static void
717send_traverse_thread(void *arg)
718{
719 struct send_thread_arg *st_arg = arg;
720 int err;
721 struct send_block_record *data;
3e635ac1 722 fstrans_cookie_t cookie = spl_fstrans_mark();
fcff0f35
PD
723
724 if (st_arg->ds != NULL) {
47dfff3b
MA
725 err = traverse_dataset_resume(st_arg->ds,
726 st_arg->fromtxg, &st_arg->resume,
727 st_arg->flags, send_cb, st_arg);
728
fcff0f35
PD
729 if (err != EINTR)
730 st_arg->error_code = err;
731 }
732 data = kmem_zalloc(sizeof (*data), KM_SLEEP);
733 data->eos_marker = B_TRUE;
734 bqueue_enqueue(&st_arg->q, data, 1);
3e635ac1 735 spl_fstrans_unmark(cookie);
34a6b428 736 thread_exit();
fcff0f35
PD
737}
738
739/*
740 * This function actually handles figuring out what kind of record needs to be
741 * dumped, reading the data (which has hopefully been prefetched), and calling
742 * the appropriate helper function.
743 */
34dc7c2f 744static int
fcff0f35 745do_dump(dmu_sendarg_t *dsa, struct send_block_record *data)
34dc7c2f 746{
fcff0f35
PD
747 dsl_dataset_t *ds = dmu_objset_ds(dsa->dsa_os);
748 const blkptr_t *bp = &data->bp;
749 const zbookmark_phys_t *zb = &data->zb;
750 uint8_t indblkshift = data->indblkshift;
751 uint16_t dblkszsec = data->datablkszsec;
752 spa_t *spa = ds->ds_dir->dd_pool->dp_spa;
34dc7c2f 753 dmu_object_type_t type = bp ? BP_GET_TYPE(bp) : DMU_OT_NONE;
34dc7c2f
BB
754 int err = 0;
755
fcff0f35 756 ASSERT3U(zb->zb_level, >=, 0);
34dc7c2f 757
47dfff3b
MA
758 ASSERT(zb->zb_object == DMU_META_DNODE_OBJECT ||
759 zb->zb_object >= dsa->dsa_resume_object);
760
b5256303
TC
761 /*
762 * All bps of an encrypted os should have the encryption bit set.
763 * If this is not true it indicates tampering and we report an error.
764 */
765 if (dsa->dsa_os->os_encrypted &&
766 !BP_IS_HOLE(bp) && !BP_USES_CRYPT(bp)) {
767 spa_log_error(spa, zb);
768 zfs_panic_recover("unencrypted block in encrypted "
769 "object set %llu", ds->ds_object);
770 return (SET_ERROR(EIO));
771 }
772
428870ff
BB
773 if (zb->zb_object != DMU_META_DNODE_OBJECT &&
774 DMU_OBJECT_IS_SPECIAL(zb->zb_object)) {
9babb374 775 return (0);
b0bc7a84
MG
776 } else if (BP_IS_HOLE(bp) &&
777 zb->zb_object == DMU_META_DNODE_OBJECT) {
fcff0f35 778 uint64_t span = BP_SPAN(dblkszsec, indblkshift, zb->zb_level);
b128c09f 779 uint64_t dnobj = (zb->zb_blkid * span) >> DNODE_SHIFT;
fcff0f35 780 err = dump_freeobjects(dsa, dnobj, span >> DNODE_SHIFT);
b0bc7a84 781 } else if (BP_IS_HOLE(bp)) {
fcff0f35
PD
782 uint64_t span = BP_SPAN(dblkszsec, indblkshift, zb->zb_level);
783 uint64_t offset = zb->zb_blkid * span;
ee45fbd8 784 /* Don't dump free records for offsets > DMU_OBJECT_END */
785 if (zb->zb_blkid == 0 || span <= DMU_OBJECT_END / zb->zb_blkid)
786 err = dump_free(dsa, zb->zb_object, offset, span);
b128c09f
BB
787 } else if (zb->zb_level > 0 || type == DMU_OT_OBJSET) {
788 return (0);
789 } else if (type == DMU_OT_DNODE) {
50c957f7 790 int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
2a432414 791 arc_flags_t aflags = ARC_FLAG_WAIT;
b128c09f 792 arc_buf_t *abuf;
b5256303 793 enum zio_flag zioflags = ZIO_FLAG_CANFAIL;
fcff0f35 794
b5256303
TC
795 if (dsa->dsa_featureflags & DMU_BACKUP_FEATURE_RAW) {
796 ASSERT(BP_IS_ENCRYPTED(bp));
797 ASSERT3U(BP_GET_COMPRESS(bp), ==, ZIO_COMPRESS_OFF);
798 zioflags |= ZIO_FLAG_RAW;
799 }
800
fcff0f35 801 ASSERT0(zb->zb_level);
b128c09f 802
294f6806 803 if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
b5256303 804 ZIO_PRIORITY_ASYNC_READ, zioflags, &aflags, zb) != 0)
2e528b49 805 return (SET_ERROR(EIO));
34dc7c2f 806
1c27024e
DB
807 dnode_phys_t *blk = abuf->b_data;
808 uint64_t dnobj = zb->zb_blkid * epb;
b5256303
TC
809
810 /*
811 * Raw sends require sending encryption parameters for the
812 * block of dnodes. Regular sends do not need to send this
813 * info.
814 */
815 if (dsa->dsa_featureflags & DMU_BACKUP_FEATURE_RAW) {
816 ASSERT(arc_is_encrypted(abuf));
817 err = dump_object_range(dsa, bp, dnobj, epb);
818 }
819
820 if (err == 0) {
1c27024e
DB
821 for (int i = 0; i < epb;
822 i += blk[i].dn_extra_slots + 1) {
b5256303
TC
823 err = dump_dnode(dsa, bp, dnobj + i, blk + i);
824 if (err != 0)
825 break;
826 }
34dc7c2f 827 }
d3c2ae1c 828 arc_buf_destroy(abuf, &abuf);
428870ff 829 } else if (type == DMU_OT_SA) {
2a432414 830 arc_flags_t aflags = ARC_FLAG_WAIT;
b128c09f 831 arc_buf_t *abuf;
b5256303
TC
832 enum zio_flag zioflags = ZIO_FLAG_CANFAIL;
833
834 if (dsa->dsa_featureflags & DMU_BACKUP_FEATURE_RAW) {
835 ASSERT(BP_IS_PROTECTED(bp));
836 zioflags |= ZIO_FLAG_RAW;
837 }
b128c09f 838
294f6806 839 if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
b5256303 840 ZIO_PRIORITY_ASYNC_READ, zioflags, &aflags, zb) != 0)
2e528b49 841 return (SET_ERROR(EIO));
b128c09f 842
b5256303 843 err = dump_spill(dsa, bp, zb->zb_object, abuf->b_data);
d3c2ae1c 844 arc_buf_destroy(abuf, &abuf);
fcff0f35 845 } else if (backup_do_embed(dsa, bp)) {
9b67f605 846 /* it's an embedded level-0 block of a regular object */
fcff0f35
PD
847 int blksz = dblkszsec << SPA_MINBLOCKSHIFT;
848 ASSERT0(zb->zb_level);
849 err = dump_write_embedded(dsa, zb->zb_object,
9b67f605 850 zb->zb_blkid * blksz, blksz, bp);
fcff0f35
PD
851 } else {
852 /* it's a level-0 block of a regular object */
2a432414 853 arc_flags_t aflags = ARC_FLAG_WAIT;
428870ff 854 arc_buf_t *abuf;
fcff0f35
PD
855 int blksz = dblkszsec << SPA_MINBLOCKSHIFT;
856 uint64_t offset;
2aa34383
DK
857
858 /*
859 * If we have large blocks stored on disk but the send flags
860 * don't allow us to send large blocks, we split the data from
861 * the arc buf into chunks.
862 */
a7004725 863 boolean_t split_large_blocks = blksz > SPA_OLD_MAXBLOCKSIZE &&
2aa34383 864 !(dsa->dsa_featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS);
b5256303
TC
865
866 /*
867 * Raw sends require that we always get raw data as it exists
868 * on disk, so we assert that we are not splitting blocks here.
869 */
870 boolean_t request_raw =
871 (dsa->dsa_featureflags & DMU_BACKUP_FEATURE_RAW) != 0;
872
2aa34383
DK
873 /*
874 * We should only request compressed data from the ARC if all
875 * the following are true:
876 * - stream compression was requested
877 * - we aren't splitting large blocks into smaller chunks
878 * - the data won't need to be byteswapped before sending
879 * - this isn't an embedded block
880 * - this isn't metadata (if receiving on a different endian
881 * system it can be byteswapped more easily)
882 */
883 boolean_t request_compressed =
884 (dsa->dsa_featureflags & DMU_BACKUP_FEATURE_COMPRESSED) &&
885 !split_large_blocks && !BP_SHOULD_BYTESWAP(bp) &&
886 !BP_IS_EMBEDDED(bp) && !DMU_OT_IS_METADATA(BP_GET_TYPE(bp));
428870ff 887
b5256303
TC
888 IMPLY(request_raw, !split_large_blocks);
889 IMPLY(request_raw, BP_IS_PROTECTED(bp));
da536844 890 ASSERT0(zb->zb_level);
47dfff3b
MA
891 ASSERT(zb->zb_object > dsa->dsa_resume_object ||
892 (zb->zb_object == dsa->dsa_resume_object &&
893 zb->zb_blkid * blksz >= dsa->dsa_resume_offset));
894
a7004725
DK
895 ASSERT3U(blksz, ==, BP_GET_LSIZE(bp));
896
897 enum zio_flag zioflags = ZIO_FLAG_CANFAIL;
b5256303 898 if (request_raw)
2aa34383 899 zioflags |= ZIO_FLAG_RAW;
b5256303
TC
900 else if (request_compressed)
901 zioflags |= ZIO_FLAG_RAW_COMPRESS;
2aa34383 902
294f6806 903 if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
a7004725 904 ZIO_PRIORITY_ASYNC_READ, zioflags, &aflags, zb) != 0) {
330d06f9 905 if (zfs_send_corrupt_data) {
330d06f9 906 /* Send a block filled with 0x"zfs badd bloc" */
2aa34383
DK
907 abuf = arc_alloc_buf(spa, &abuf, ARC_BUFC_DATA,
908 blksz);
a7004725 909 uint64_t *ptr;
330d06f9
MA
910 for (ptr = abuf->b_data;
911 (char *)ptr < (char *)abuf->b_data + blksz;
912 ptr++)
dd26aa53 913 *ptr = 0x2f5baddb10cULL;
330d06f9 914 } else {
2e528b49 915 return (SET_ERROR(EIO));
330d06f9
MA
916 }
917 }
428870ff 918
f1512ee6
MA
919 offset = zb->zb_blkid * blksz;
920
2aa34383 921 if (split_large_blocks) {
b5256303 922 ASSERT0(arc_is_encrypted(abuf));
2aa34383
DK
923 ASSERT3U(arc_get_compression(abuf), ==,
924 ZIO_COMPRESS_OFF);
a7004725 925 char *buf = abuf->b_data;
f1512ee6
MA
926 while (blksz > 0 && err == 0) {
927 int n = MIN(blksz, SPA_OLD_MAXBLOCKSIZE);
fcff0f35 928 err = dump_write(dsa, type, zb->zb_object,
2aa34383 929 offset, n, n, NULL, buf);
f1512ee6
MA
930 offset += n;
931 buf += n;
932 blksz -= n;
933 }
934 } else {
2aa34383 935 err = dump_write(dsa, type, zb->zb_object, offset,
b5256303 936 blksz, arc_buf_size(abuf), bp, abuf->b_data);
f1512ee6 937 }
d3c2ae1c 938 arc_buf_destroy(abuf, &abuf);
34dc7c2f
BB
939 }
940
941 ASSERT(err == 0 || err == EINTR);
942 return (err);
943}
944
6f1ffb06 945/*
fcff0f35
PD
946 * Pop the new data off the queue, and free the old data.
947 */
948static struct send_block_record *
949get_next_record(bqueue_t *bq, struct send_block_record *data)
950{
951 struct send_block_record *tmp = bqueue_dequeue(bq);
952 kmem_free(data, sizeof (*data));
953 return (tmp);
954}
955
956/*
957 * Actually do the bulk of the work in a zfs send.
958 *
959 * Note: Releases dp using the specified tag.
6f1ffb06 960 */
13fe0198 961static int
fcff0f35 962dmu_send_impl(void *tag, dsl_pool_t *dp, dsl_dataset_t *to_ds,
2aa34383
DK
963 zfs_bookmark_phys_t *ancestor_zb, boolean_t is_clone,
964 boolean_t embedok, boolean_t large_block_ok, boolean_t compressok,
b5256303 965 boolean_t rawok, int outfd, uint64_t resumeobj, uint64_t resumeoff,
47dfff3b 966 vnode_t *vp, offset_t *off)
34dc7c2f 967{
13fe0198 968 objset_t *os;
34dc7c2f 969 dmu_replay_record_t *drr;
37abac6d 970 dmu_sendarg_t *dsp;
34dc7c2f
BB
971 int err;
972 uint64_t fromtxg = 0;
9b67f605 973 uint64_t featureflags = 0;
fcff0f35 974 struct send_thread_arg to_arg;
47dfff3b
MA
975 void *payload = NULL;
976 size_t payload_len = 0;
fcff0f35 977 struct send_block_record *to_data;
34dc7c2f 978
fcff0f35 979 err = dmu_objset_from_ds(to_ds, &os);
13fe0198 980 if (err != 0) {
13fe0198
MA
981 dsl_pool_rele(dp, tag);
982 return (err);
983 }
34dc7c2f 984
b5256303
TC
985 /*
986 * If this is a non-raw send of an encrypted ds, we can ensure that
987 * the objset_phys_t is authenticated. This is safe because this is
988 * either a snapshot or we have owned the dataset, ensuring that
989 * it can't be modified.
990 */
991 if (!rawok && os->os_encrypted &&
992 arc_is_unauthenticated(os->os_phys_buf)) {
a2c2ed1b
TC
993 zbookmark_phys_t zb;
994
995 SET_BOOKMARK(&zb, to_ds->ds_object, ZB_ROOT_OBJECT,
996 ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
b5256303 997 err = arc_untransform(os->os_phys_buf, os->os_spa,
a2c2ed1b 998 &zb, B_FALSE);
b5256303
TC
999 if (err != 0) {
1000 dsl_pool_rele(dp, tag);
1001 return (err);
1002 }
1003
1004 ASSERT0(arc_is_unauthenticated(os->os_phys_buf));
1005 }
1006
34dc7c2f
BB
1007 drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP);
1008 drr->drr_type = DRR_BEGIN;
1009 drr->drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
428870ff
BB
1010 DMU_SET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo,
1011 DMU_SUBSTREAM);
1012
47dfff3b
MA
1013 bzero(&to_arg, sizeof (to_arg));
1014
428870ff 1015#ifdef _KERNEL
13fe0198 1016 if (dmu_objset_type(os) == DMU_OST_ZFS) {
428870ff 1017 uint64_t version;
13fe0198 1018 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &version) != 0) {
37abac6d 1019 kmem_free(drr, sizeof (dmu_replay_record_t));
13fe0198 1020 dsl_pool_rele(dp, tag);
2e528b49 1021 return (SET_ERROR(EINVAL));
37abac6d 1022 }
13fe0198 1023 if (version >= ZPL_VERSION_SA) {
9b67f605 1024 featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
428870ff
BB
1025 }
1026 }
1027#endif
1028
b5256303
TC
1029 /* raw sends imply large_block_ok */
1030 if ((large_block_ok || rawok) &&
1031 to_ds->ds_feature_inuse[SPA_FEATURE_LARGE_BLOCKS])
f1512ee6 1032 featureflags |= DMU_BACKUP_FEATURE_LARGE_BLOCKS;
50c957f7
NB
1033 if (to_ds->ds_feature_inuse[SPA_FEATURE_LARGE_DNODE])
1034 featureflags |= DMU_BACKUP_FEATURE_LARGE_DNODE;
b5256303
TC
1035
1036 /* encrypted datasets will not have embedded blocks */
1037 if ((embedok || rawok) && !os->os_encrypted &&
9b67f605
MA
1038 spa_feature_is_active(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA)) {
1039 featureflags |= DMU_BACKUP_FEATURE_EMBED_DATA;
2aa34383 1040 }
b5256303
TC
1041
1042 /* raw send implies compressok */
1043 if (compressok || rawok)
2aa34383 1044 featureflags |= DMU_BACKUP_FEATURE_COMPRESSED;
b5256303
TC
1045 if (rawok && os->os_encrypted)
1046 featureflags |= DMU_BACKUP_FEATURE_RAW;
1047
2aa34383 1048 if ((featureflags &
b5256303
TC
1049 (DMU_BACKUP_FEATURE_EMBED_DATA | DMU_BACKUP_FEATURE_COMPRESSED |
1050 DMU_BACKUP_FEATURE_RAW)) != 0 &&
1051 spa_feature_is_active(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS)) {
2aa34383 1052 featureflags |= DMU_BACKUP_FEATURE_LZ4;
9b67f605
MA
1053 }
1054
47dfff3b
MA
1055 if (resumeobj != 0 || resumeoff != 0) {
1056 featureflags |= DMU_BACKUP_FEATURE_RESUMING;
1057 }
1058
9b67f605
MA
1059 DMU_SET_FEATUREFLAGS(drr->drr_u.drr_begin.drr_versioninfo,
1060 featureflags);
1061
34dc7c2f 1062 drr->drr_u.drr_begin.drr_creation_time =
fcff0f35 1063 dsl_dataset_phys(to_ds)->ds_creation_time;
13fe0198 1064 drr->drr_u.drr_begin.drr_type = dmu_objset_type(os);
da536844 1065 if (is_clone)
34dc7c2f 1066 drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CLONE;
fcff0f35
PD
1067 drr->drr_u.drr_begin.drr_toguid = dsl_dataset_phys(to_ds)->ds_guid;
1068 if (dsl_dataset_phys(to_ds)->ds_flags & DS_FLAG_CI_DATASET)
34dc7c2f 1069 drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CI_DATA;
b607405f
AS
1070 if (zfs_send_set_freerecords_bit)
1071 drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_FREERECORDS;
34dc7c2f 1072
fcff0f35
PD
1073 if (ancestor_zb != NULL) {
1074 drr->drr_u.drr_begin.drr_fromguid =
1075 ancestor_zb->zbm_guid;
1076 fromtxg = ancestor_zb->zbm_creation_txg;
da536844 1077 }
fcff0f35
PD
1078 dsl_dataset_name(to_ds, drr->drr_u.drr_begin.drr_toname);
1079 if (!to_ds->ds_is_snapshot) {
da536844
MA
1080 (void) strlcat(drr->drr_u.drr_begin.drr_toname, "@--head--",
1081 sizeof (drr->drr_u.drr_begin.drr_toname));
13fe0198 1082 }
34dc7c2f 1083
37abac6d
BP
1084 dsp = kmem_zalloc(sizeof (dmu_sendarg_t), KM_SLEEP);
1085
1086 dsp->dsa_drr = drr;
1087 dsp->dsa_vp = vp;
1088 dsp->dsa_outfd = outfd;
1089 dsp->dsa_proc = curproc;
13fe0198 1090 dsp->dsa_os = os;
37abac6d 1091 dsp->dsa_off = off;
fcff0f35 1092 dsp->dsa_toguid = dsl_dataset_phys(to_ds)->ds_guid;
37abac6d 1093 dsp->dsa_pending_op = PENDING_NONE;
9b67f605 1094 dsp->dsa_featureflags = featureflags;
47dfff3b
MA
1095 dsp->dsa_resume_object = resumeobj;
1096 dsp->dsa_resume_offset = resumeoff;
37abac6d 1097
fcff0f35
PD
1098 mutex_enter(&to_ds->ds_sendstream_lock);
1099 list_insert_head(&to_ds->ds_sendstreams, dsp);
1100 mutex_exit(&to_ds->ds_sendstream_lock);
37abac6d 1101
fcff0f35 1102 dsl_dataset_long_hold(to_ds, FTAG);
7ec09286
MA
1103 dsl_pool_rele(dp, tag);
1104
b5256303
TC
1105 /* handle features that require a DRR_BEGIN payload */
1106 if (featureflags &
1107 (DMU_BACKUP_FEATURE_RESUMING | DMU_BACKUP_FEATURE_RAW)) {
1108 nvlist_t *keynvl = NULL;
1109 nvlist_t *nvl = fnvlist_alloc();
1110
1111 if (featureflags & DMU_BACKUP_FEATURE_RESUMING) {
1112 dmu_object_info_t to_doi;
1113 err = dmu_object_info(os, resumeobj, &to_doi);
1114 if (err != 0) {
1115 fnvlist_free(nvl);
1116 goto out;
1117 }
1118
1119 SET_BOOKMARK(&to_arg.resume, to_ds->ds_object,
1120 resumeobj, 0,
1121 resumeoff / to_doi.doi_data_block_size);
1122
1123 fnvlist_add_uint64(nvl, "resume_object", resumeobj);
1124 fnvlist_add_uint64(nvl, "resume_offset", resumeoff);
1125 }
1126
1127 if (featureflags & DMU_BACKUP_FEATURE_RAW) {
1128 ASSERT(os->os_encrypted);
1129
1130 err = dsl_crypto_populate_key_nvlist(to_ds, &keynvl);
1131 if (err != 0) {
1132 fnvlist_free(nvl);
1133 goto out;
1134 }
1135
1136 fnvlist_add_nvlist(nvl, "crypt_keydata", keynvl);
1137 }
47dfff3b 1138
47dfff3b
MA
1139 payload = fnvlist_pack(nvl, &payload_len);
1140 drr->drr_payloadlen = payload_len;
b5256303 1141 fnvlist_free(keynvl);
47dfff3b
MA
1142 fnvlist_free(nvl);
1143 }
1144
1145 err = dump_record(dsp, payload, payload_len);
1146 fnvlist_pack_free(payload, payload_len);
1147 if (err != 0) {
37abac6d
BP
1148 err = dsp->dsa_err;
1149 goto out;
34dc7c2f
BB
1150 }
1151
3b0d9928
BB
1152 err = bqueue_init(&to_arg.q,
1153 MAX(zfs_send_queue_length, 2 * zfs_max_recordsize),
fcff0f35
PD
1154 offsetof(struct send_block_record, ln));
1155 to_arg.error_code = 0;
1156 to_arg.cancel = B_FALSE;
1157 to_arg.ds = to_ds;
1158 to_arg.fromtxg = fromtxg;
1159 to_arg.flags = TRAVERSE_PRE | TRAVERSE_PREFETCH;
b5256303
TC
1160 if (rawok)
1161 to_arg.flags |= TRAVERSE_NO_DECRYPT;
fcff0f35
PD
1162 (void) thread_create(NULL, 0, send_traverse_thread, &to_arg, 0, curproc,
1163 TS_RUN, minclsyspri);
1164
1165 to_data = bqueue_dequeue(&to_arg.q);
1166
1167 while (!to_data->eos_marker && err == 0) {
1168 err = do_dump(dsp, to_data);
1169 to_data = get_next_record(&to_arg.q, to_data);
1170 if (issig(JUSTLOOKING) && issig(FORREAL))
1171 err = EINTR;
1172 }
1173
1174 if (err != 0) {
1175 to_arg.cancel = B_TRUE;
1176 while (!to_data->eos_marker) {
1177 to_data = get_next_record(&to_arg.q, to_data);
1178 }
1179 }
1180 kmem_free(to_data, sizeof (*to_data));
1181
1182 bqueue_destroy(&to_arg.q);
1183
1184 if (err == 0 && to_arg.error_code != 0)
1185 err = to_arg.error_code;
1186
1187 if (err != 0)
1188 goto out;
34dc7c2f 1189
37abac6d 1190 if (dsp->dsa_pending_op != PENDING_NONE)
37f8a883 1191 if (dump_record(dsp, NULL, 0) != 0)
2e528b49 1192 err = SET_ERROR(EINTR);
428870ff 1193
13fe0198
MA
1194 if (err != 0) {
1195 if (err == EINTR && dsp->dsa_err != 0)
37abac6d
BP
1196 err = dsp->dsa_err;
1197 goto out;
34dc7c2f
BB
1198 }
1199
1200 bzero(drr, sizeof (dmu_replay_record_t));
1201 drr->drr_type = DRR_END;
37abac6d
BP
1202 drr->drr_u.drr_end.drr_checksum = dsp->dsa_zc;
1203 drr->drr_u.drr_end.drr_toguid = dsp->dsa_toguid;
34dc7c2f 1204
fcff0f35 1205 if (dump_record(dsp, NULL, 0) != 0)
37abac6d 1206 err = dsp->dsa_err;
37abac6d 1207out:
fcff0f35
PD
1208 mutex_enter(&to_ds->ds_sendstream_lock);
1209 list_remove(&to_ds->ds_sendstreams, dsp);
1210 mutex_exit(&to_ds->ds_sendstream_lock);
37abac6d 1211
51907a31
K
1212 VERIFY(err != 0 || (dsp->dsa_sent_begin && dsp->dsa_sent_end));
1213
34dc7c2f 1214 kmem_free(drr, sizeof (dmu_replay_record_t));
37abac6d 1215 kmem_free(dsp, sizeof (dmu_sendarg_t));
34dc7c2f 1216
fcff0f35 1217 dsl_dataset_long_rele(to_ds, FTAG);
13fe0198 1218
37abac6d 1219 return (err);
34dc7c2f
BB
1220}
1221
330d06f9 1222int
13fe0198 1223dmu_send_obj(const char *pool, uint64_t tosnap, uint64_t fromsnap,
2aa34383 1224 boolean_t embedok, boolean_t large_block_ok, boolean_t compressok,
b5256303 1225 boolean_t rawok, int outfd, vnode_t *vp, offset_t *off)
13fe0198
MA
1226{
1227 dsl_pool_t *dp;
1228 dsl_dataset_t *ds;
1229 dsl_dataset_t *fromds = NULL;
b5256303 1230 ds_hold_flags_t dsflags = (rawok) ? 0 : DS_HOLD_FLAG_DECRYPT;
13fe0198
MA
1231 int err;
1232
1233 err = dsl_pool_hold(pool, FTAG, &dp);
1234 if (err != 0)
1235 return (err);
1236
b5256303 1237 err = dsl_dataset_hold_obj_flags(dp, tosnap, dsflags, FTAG, &ds);
13fe0198
MA
1238 if (err != 0) {
1239 dsl_pool_rele(dp, FTAG);
1240 return (err);
1241 }
1242
1243 if (fromsnap != 0) {
da536844
MA
1244 zfs_bookmark_phys_t zb;
1245 boolean_t is_clone;
1246
13fe0198
MA
1247 err = dsl_dataset_hold_obj(dp, fromsnap, FTAG, &fromds);
1248 if (err != 0) {
b5256303 1249 dsl_dataset_rele_flags(ds, dsflags, FTAG);
13fe0198
MA
1250 dsl_pool_rele(dp, FTAG);
1251 return (err);
1252 }
da536844
MA
1253 if (!dsl_dataset_is_before(ds, fromds, 0))
1254 err = SET_ERROR(EXDEV);
d683ddbb
JG
1255 zb.zbm_creation_time =
1256 dsl_dataset_phys(fromds)->ds_creation_time;
1257 zb.zbm_creation_txg = dsl_dataset_phys(fromds)->ds_creation_txg;
1258 zb.zbm_guid = dsl_dataset_phys(fromds)->ds_guid;
da536844
MA
1259 is_clone = (fromds->ds_dir != ds->ds_dir);
1260 dsl_dataset_rele(fromds, FTAG);
f1512ee6 1261 err = dmu_send_impl(FTAG, dp, ds, &zb, is_clone,
b5256303
TC
1262 embedok, large_block_ok, compressok, rawok, outfd,
1263 0, 0, vp, off);
da536844 1264 } else {
f1512ee6 1265 err = dmu_send_impl(FTAG, dp, ds, NULL, B_FALSE,
b5256303
TC
1266 embedok, large_block_ok, compressok, rawok, outfd,
1267 0, 0, vp, off);
13fe0198 1268 }
b5256303 1269 dsl_dataset_rele_flags(ds, dsflags, FTAG);
da536844 1270 return (err);
13fe0198
MA
1271}
1272
1273int
47dfff3b 1274dmu_send(const char *tosnap, const char *fromsnap, boolean_t embedok,
b5256303
TC
1275 boolean_t large_block_ok, boolean_t compressok, boolean_t rawok,
1276 int outfd, uint64_t resumeobj, uint64_t resumeoff, vnode_t *vp,
1277 offset_t *off)
13fe0198
MA
1278{
1279 dsl_pool_t *dp;
1280 dsl_dataset_t *ds;
13fe0198 1281 int err;
b5256303 1282 ds_hold_flags_t dsflags = (rawok) ? 0 : DS_HOLD_FLAG_DECRYPT;
da536844 1283 boolean_t owned = B_FALSE;
13fe0198 1284
da536844 1285 if (fromsnap != NULL && strpbrk(fromsnap, "@#") == NULL)
2e528b49 1286 return (SET_ERROR(EINVAL));
13fe0198
MA
1287
1288 err = dsl_pool_hold(tosnap, FTAG, &dp);
1289 if (err != 0)
1290 return (err);
1291
da536844
MA
1292 if (strchr(tosnap, '@') == NULL && spa_writeable(dp->dp_spa)) {
1293 /*
1294 * We are sending a filesystem or volume. Ensure
1295 * that it doesn't change by owning the dataset.
1296 */
b5256303 1297 err = dsl_dataset_own(dp, tosnap, dsflags, FTAG, &ds);
da536844
MA
1298 owned = B_TRUE;
1299 } else {
b5256303 1300 err = dsl_dataset_hold_flags(dp, tosnap, dsflags, FTAG, &ds);
da536844 1301 }
13fe0198
MA
1302 if (err != 0) {
1303 dsl_pool_rele(dp, FTAG);
1304 return (err);
1305 }
1306
1307 if (fromsnap != NULL) {
da536844
MA
1308 zfs_bookmark_phys_t zb;
1309 boolean_t is_clone = B_FALSE;
1310 int fsnamelen = strchr(tosnap, '@') - tosnap;
1311
1312 /*
1313 * If the fromsnap is in a different filesystem, then
1314 * mark the send stream as a clone.
1315 */
1316 if (strncmp(tosnap, fromsnap, fsnamelen) != 0 ||
1317 (fromsnap[fsnamelen] != '@' &&
1318 fromsnap[fsnamelen] != '#')) {
1319 is_clone = B_TRUE;
1320 }
1321
1322 if (strchr(fromsnap, '@')) {
1323 dsl_dataset_t *fromds;
1324 err = dsl_dataset_hold(dp, fromsnap, FTAG, &fromds);
1325 if (err == 0) {
1326 if (!dsl_dataset_is_before(ds, fromds, 0))
1327 err = SET_ERROR(EXDEV);
1328 zb.zbm_creation_time =
d683ddbb 1329 dsl_dataset_phys(fromds)->ds_creation_time;
da536844 1330 zb.zbm_creation_txg =
d683ddbb
JG
1331 dsl_dataset_phys(fromds)->ds_creation_txg;
1332 zb.zbm_guid = dsl_dataset_phys(fromds)->ds_guid;
da536844
MA
1333 is_clone = (ds->ds_dir != fromds->ds_dir);
1334 dsl_dataset_rele(fromds, FTAG);
1335 }
1336 } else {
1337 err = dsl_bookmark_lookup(dp, fromsnap, ds, &zb);
1338 }
13fe0198 1339 if (err != 0) {
b5256303
TC
1340 if (owned)
1341 dsl_dataset_disown(ds, dsflags, FTAG);
1342 else
1343 dsl_dataset_rele_flags(ds, dsflags, FTAG);
1344
13fe0198
MA
1345 dsl_pool_rele(dp, FTAG);
1346 return (err);
1347 }
f1512ee6 1348 err = dmu_send_impl(FTAG, dp, ds, &zb, is_clone,
b5256303 1349 embedok, large_block_ok, compressok, rawok,
47dfff3b 1350 outfd, resumeobj, resumeoff, vp, off);
da536844 1351 } else {
f1512ee6 1352 err = dmu_send_impl(FTAG, dp, ds, NULL, B_FALSE,
b5256303 1353 embedok, large_block_ok, compressok, rawok,
47dfff3b 1354 outfd, resumeobj, resumeoff, vp, off);
13fe0198 1355 }
da536844 1356 if (owned)
b5256303 1357 dsl_dataset_disown(ds, dsflags, FTAG);
da536844 1358 else
b5256303
TC
1359 dsl_dataset_rele_flags(ds, dsflags, FTAG);
1360
da536844 1361 return (err);
13fe0198
MA
1362}
1363
5dc8b736 1364static int
2aa34383
DK
1365dmu_adjust_send_estimate_for_indirects(dsl_dataset_t *ds, uint64_t uncompressed,
1366 uint64_t compressed, boolean_t stream_compressed, uint64_t *sizep)
5dc8b736 1367{
ca0845d5 1368 int err = 0;
2aa34383 1369 uint64_t size;
5dc8b736
MG
1370 /*
1371 * Assume that space (both on-disk and in-stream) is dominated by
1372 * data. We will adjust for indirect blocks and the copies property,
1373 * but ignore per-object space used (eg, dnodes and DRR_OBJECT records).
1374 */
1375
2aa34383
DK
1376 uint64_t recordsize;
1377 uint64_t record_count;
dd429b46
PD
1378 objset_t *os;
1379 VERIFY0(dmu_objset_from_ds(ds, &os));
2aa34383
DK
1380
1381 /* Assume all (uncompressed) blocks are recordsize. */
ca0845d5
PD
1382 if (zfs_override_estimate_recordsize != 0) {
1383 recordsize = zfs_override_estimate_recordsize;
1384 } else if (os->os_phys->os_type == DMU_OST_ZVOL) {
dd429b46
PD
1385 err = dsl_prop_get_int_ds(ds,
1386 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &recordsize);
1387 } else {
1388 err = dsl_prop_get_int_ds(ds,
1389 zfs_prop_to_name(ZFS_PROP_RECORDSIZE), &recordsize);
1390 }
2aa34383
DK
1391 if (err != 0)
1392 return (err);
1393 record_count = uncompressed / recordsize;
1394
1395 /*
1396 * If we're estimating a send size for a compressed stream, use the
1397 * compressed data size to estimate the stream size. Otherwise, use the
1398 * uncompressed data size.
1399 */
1400 size = stream_compressed ? compressed : uncompressed;
1401
5dc8b736
MG
1402 /*
1403 * Subtract out approximate space used by indirect blocks.
1404 * Assume most space is used by data blocks (non-indirect, non-dnode).
2aa34383 1405 * Assume no ditto blocks or internal fragmentation.
5dc8b736
MG
1406 *
1407 * Therefore, space used by indirect blocks is sizeof(blkptr_t) per
2aa34383 1408 * block.
5dc8b736 1409 */
2aa34383 1410 size -= record_count * sizeof (blkptr_t);
5dc8b736
MG
1411
1412 /* Add in the space for the record associated with each block. */
2aa34383 1413 size += record_count * sizeof (dmu_replay_record_t);
5dc8b736
MG
1414
1415 *sizep = size;
1416
1417 return (0);
1418}
1419
13fe0198 1420int
2aa34383
DK
1421dmu_send_estimate(dsl_dataset_t *ds, dsl_dataset_t *fromds,
1422 boolean_t stream_compressed, uint64_t *sizep)
330d06f9 1423{
330d06f9 1424 int err;
2aa34383 1425 uint64_t uncomp, comp;
13fe0198 1426
fd0fd646 1427 ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
330d06f9
MA
1428
1429 /* tosnap must be a snapshot */
0c66c32d 1430 if (!ds->ds_is_snapshot)
2e528b49 1431 return (SET_ERROR(EINVAL));
330d06f9 1432
71e2fe41
AG
1433 /* fromsnap, if provided, must be a snapshot */
1434 if (fromds != NULL && !fromds->ds_is_snapshot)
1435 return (SET_ERROR(EINVAL));
1436
6f1ffb06
MA
1437 /*
1438 * fromsnap must be an earlier snapshot from the same fs as tosnap,
1439 * or the origin's fs.
1440 */
da536844 1441 if (fromds != NULL && !dsl_dataset_is_before(ds, fromds, 0))
2e528b49 1442 return (SET_ERROR(EXDEV));
330d06f9 1443
2aa34383 1444 /* Get compressed and uncompressed size estimates of changed data. */
330d06f9 1445 if (fromds == NULL) {
2aa34383
DK
1446 uncomp = dsl_dataset_phys(ds)->ds_uncompressed_bytes;
1447 comp = dsl_dataset_phys(ds)->ds_compressed_bytes;
330d06f9 1448 } else {
2aa34383 1449 uint64_t used;
330d06f9 1450 err = dsl_dataset_space_written(fromds, ds,
2aa34383 1451 &used, &comp, &uncomp);
13fe0198 1452 if (err != 0)
330d06f9
MA
1453 return (err);
1454 }
1455
2aa34383
DK
1456 err = dmu_adjust_send_estimate_for_indirects(ds, uncomp, comp,
1457 stream_compressed, sizep);
dd429b46
PD
1458 /*
1459 * Add the size of the BEGIN and END records to the estimate.
1460 */
1461 *sizep += 2 * sizeof (dmu_replay_record_t);
5dc8b736
MG
1462 return (err);
1463}
330d06f9 1464
2aa34383
DK
1465struct calculate_send_arg {
1466 uint64_t uncompressed;
1467 uint64_t compressed;
1468};
1469
5dc8b736
MG
1470/*
1471 * Simple callback used to traverse the blocks of a snapshot and sum their
2aa34383 1472 * uncompressed and compressed sizes.
5dc8b736
MG
1473 */
1474/* ARGSUSED */
1475static int
1476dmu_calculate_send_traversal(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1477 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
1478{
2aa34383 1479 struct calculate_send_arg *space = arg;
5dc8b736 1480 if (bp != NULL && !BP_IS_HOLE(bp)) {
2aa34383
DK
1481 space->uncompressed += BP_GET_UCSIZE(bp);
1482 space->compressed += BP_GET_PSIZE(bp);
5dc8b736
MG
1483 }
1484 return (0);
1485}
1486
1487/*
1488 * Given a desination snapshot and a TXG, calculate the approximate size of a
1489 * send stream sent from that TXG. from_txg may be zero, indicating that the
1490 * whole snapshot will be sent.
1491 */
1492int
1493dmu_send_estimate_from_txg(dsl_dataset_t *ds, uint64_t from_txg,
2aa34383 1494 boolean_t stream_compressed, uint64_t *sizep)
5dc8b736 1495{
5dc8b736 1496 int err;
2aa34383 1497 struct calculate_send_arg size = { 0 };
5dc8b736 1498
fd0fd646 1499 ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
5dc8b736
MG
1500
1501 /* tosnap must be a snapshot */
1502 if (!dsl_dataset_is_snapshot(ds))
1503 return (SET_ERROR(EINVAL));
1504
1505 /* verify that from_txg is before the provided snapshot was taken */
1506 if (from_txg >= dsl_dataset_phys(ds)->ds_creation_txg) {
1507 return (SET_ERROR(EXDEV));
1508 }
330d06f9 1509 /*
5dc8b736
MG
1510 * traverse the blocks of the snapshot with birth times after
1511 * from_txg, summing their uncompressed size
330d06f9 1512 */
b5256303
TC
1513 err = traverse_dataset(ds, from_txg,
1514 TRAVERSE_POST | TRAVERSE_NO_DECRYPT,
5dc8b736 1515 dmu_calculate_send_traversal, &size);
2aa34383 1516
5dc8b736 1517 if (err)
330d06f9 1518 return (err);
330d06f9 1519
2aa34383
DK
1520 err = dmu_adjust_send_estimate_for_indirects(ds, size.uncompressed,
1521 size.compressed, stream_compressed, sizep);
5dc8b736 1522 return (err);
330d06f9
MA
1523}
1524
13fe0198
MA
1525typedef struct dmu_recv_begin_arg {
1526 const char *drba_origin;
1527 dmu_recv_cookie_t *drba_cookie;
1528 cred_t *drba_cred;
19580676 1529 uint64_t drba_snapobj;
13fe0198 1530} dmu_recv_begin_arg_t;
34dc7c2f 1531
34dc7c2f 1532static int
13fe0198
MA
1533recv_begin_check_existing_impl(dmu_recv_begin_arg_t *drba, dsl_dataset_t *ds,
1534 uint64_t fromguid)
34dc7c2f 1535{
34dc7c2f 1536 uint64_t val;
13fe0198
MA
1537 int error;
1538 dsl_pool_t *dp = ds->ds_dir->dd_pool;
4a385862
TC
1539 struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
1540 uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
1541 boolean_t encrypted = ds->ds_dir->dd_crypto_obj != 0;
1542 boolean_t raw = (featureflags & DMU_BACKUP_FEATURE_RAW) != 0;
34dc7c2f 1543
13fe0198
MA
1544 /* temporary clone name must not exist */
1545 error = zap_lookup(dp->dp_meta_objset,
d683ddbb 1546 dsl_dir_phys(ds->ds_dir)->dd_child_dir_zapobj, recv_clone_name,
13fe0198
MA
1547 8, 1, &val);
1548 if (error != ENOENT)
1549 return (error == 0 ? EBUSY : error);
1550
572e2857 1551 /* new snapshot name must not exist */
13fe0198 1552 error = zap_lookup(dp->dp_meta_objset,
d683ddbb
JG
1553 dsl_dataset_phys(ds)->ds_snapnames_zapobj,
1554 drba->drba_cookie->drc_tosnap, 8, 1, &val);
13fe0198
MA
1555 if (error != ENOENT)
1556 return (error == 0 ? EEXIST : error);
572e2857 1557
788eb90c
JJ
1558 /*
1559 * Check snapshot limit before receiving. We'll recheck again at the
1560 * end, but might as well abort before receiving if we're already over
1561 * the limit.
1562 *
1563 * Note that we do not check the file system limit with
1564 * dsl_dir_fscount_check because the temporary %clones don't count
1565 * against that limit.
1566 */
1567 error = dsl_fs_ss_limit_check(ds->ds_dir, 1, ZFS_PROP_SNAPSHOT_LIMIT,
1568 NULL, drba->drba_cred);
1569 if (error != 0)
1570 return (error);
1571
13fe0198 1572 if (fromguid != 0) {
19580676 1573 dsl_dataset_t *snap;
d683ddbb 1574 uint64_t obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
19580676 1575
4a385862
TC
1576 /* Can't perform a raw receive on top of a non-raw receive */
1577 if (!encrypted && raw)
1578 return (SET_ERROR(EINVAL));
1579
19580676
MA
1580 /* Find snapshot in this dir that matches fromguid. */
1581 while (obj != 0) {
1582 error = dsl_dataset_hold_obj(dp, obj, FTAG,
1583 &snap);
1584 if (error != 0)
1585 return (SET_ERROR(ENODEV));
1586 if (snap->ds_dir != ds->ds_dir) {
1587 dsl_dataset_rele(snap, FTAG);
1588 return (SET_ERROR(ENODEV));
1589 }
d683ddbb 1590 if (dsl_dataset_phys(snap)->ds_guid == fromguid)
19580676 1591 break;
d683ddbb 1592 obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
19580676
MA
1593 dsl_dataset_rele(snap, FTAG);
1594 }
1595 if (obj == 0)
2e528b49 1596 return (SET_ERROR(ENODEV));
34dc7c2f 1597
19580676
MA
1598 if (drba->drba_cookie->drc_force) {
1599 drba->drba_snapobj = obj;
1600 } else {
1601 /*
1602 * If we are not forcing, there must be no
1603 * changes since fromsnap.
1604 */
1605 if (dsl_dataset_modified_since_snap(ds, snap)) {
428870ff 1606 dsl_dataset_rele(snap, FTAG);
19580676 1607 return (SET_ERROR(ETXTBSY));
428870ff 1608 }
19580676 1609 drba->drba_snapobj = ds->ds_prev->ds_object;
428870ff 1610 }
19580676
MA
1611
1612 dsl_dataset_rele(snap, FTAG);
428870ff 1613 } else {
cf50a2b0
AG
1614 /* if full, then must be forced */
1615 if (!drba->drba_cookie->drc_force)
1616 return (SET_ERROR(EEXIST));
b5256303
TC
1617
1618 /*
1619 * We don't support using zfs recv -F to blow away
1620 * encrypted filesystems. This would require the
1621 * dsl dir to point to the old encryption key and
1622 * the new one at the same time during the receive.
1623 */
4a385862 1624 if ((!encrypted && raw) || encrypted)
b5256303
TC
1625 return (SET_ERROR(EINVAL));
1626
1627 drba->drba_snapobj = 0;
428870ff 1628 }
34dc7c2f 1629
34dc7c2f 1630 return (0);
13fe0198
MA
1631
1632}
1633
1634static int
1635dmu_recv_begin_check(void *arg, dmu_tx_t *tx)
1636{
1637 dmu_recv_begin_arg_t *drba = arg;
1638 dsl_pool_t *dp = dmu_tx_pool(tx);
1639 struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
1640 uint64_t fromguid = drrb->drr_fromguid;
1641 int flags = drrb->drr_flags;
b5256303 1642 ds_hold_flags_t dsflags = 0;
13fe0198 1643 int error;
9b67f605 1644 uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
13fe0198
MA
1645 dsl_dataset_t *ds;
1646 const char *tofs = drba->drba_cookie->drc_tofs;
1647
1648 /* already checked */
1649 ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
47dfff3b 1650 ASSERT(!(featureflags & DMU_BACKUP_FEATURE_RESUMING));
13fe0198
MA
1651
1652 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
1653 DMU_COMPOUNDSTREAM ||
1654 drrb->drr_type >= DMU_OST_NUMTYPES ||
1655 ((flags & DRR_FLAG_CLONE) && drba->drba_origin == NULL))
2e528b49 1656 return (SET_ERROR(EINVAL));
13fe0198
MA
1657
1658 /* Verify pool version supports SA if SA_SPILL feature set */
9b67f605
MA
1659 if ((featureflags & DMU_BACKUP_FEATURE_SA_SPILL) &&
1660 spa_version(dp->dp_spa) < SPA_VERSION_SA)
1661 return (SET_ERROR(ENOTSUP));
1662
47dfff3b
MA
1663 if (drba->drba_cookie->drc_resumable &&
1664 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EXTENSIBLE_DATASET))
1665 return (SET_ERROR(ENOTSUP));
1666
9b67f605
MA
1667 /*
1668 * The receiving code doesn't know how to translate a WRITE_EMBEDDED
2aa34383 1669 * record to a plain WRITE record, so the pool must have the
9b67f605
MA
1670 * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
1671 * records. Same with WRITE_EMBEDDED records that use LZ4 compression.
1672 */
1673 if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) &&
1674 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA))
1675 return (SET_ERROR(ENOTSUP));
2aa34383 1676 if ((featureflags & DMU_BACKUP_FEATURE_LZ4) &&
9b67f605 1677 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
2e528b49 1678 return (SET_ERROR(ENOTSUP));
13fe0198 1679
f1512ee6
MA
1680 /*
1681 * The receiving code doesn't know how to translate large blocks
1682 * to smaller ones, so the pool must have the LARGE_BLOCKS
73aac4aa
NB
1683 * feature enabled if the stream has LARGE_BLOCKS. Same with
1684 * large dnodes.
f1512ee6
MA
1685 */
1686 if ((featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
1687 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LARGE_BLOCKS))
1688 return (SET_ERROR(ENOTSUP));
50c957f7
NB
1689 if ((featureflags & DMU_BACKUP_FEATURE_LARGE_DNODE) &&
1690 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LARGE_DNODE))
1691 return (SET_ERROR(ENOTSUP));
1692
b5256303
TC
1693 if ((featureflags & DMU_BACKUP_FEATURE_RAW)) {
1694 /* raw receives require the encryption feature */
1695 if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_ENCRYPTION))
1696 return (SET_ERROR(ENOTSUP));
1697 } else {
1698 dsflags |= DS_HOLD_FLAG_DECRYPT;
1699 }
1700
1701 error = dsl_dataset_hold_flags(dp, tofs, dsflags, FTAG, &ds);
13fe0198
MA
1702 if (error == 0) {
1703 /* target fs already exists; recv into temp clone */
1704
1705 /* Can't recv a clone into an existing fs */
e6d3a843 1706 if (flags & DRR_FLAG_CLONE || drba->drba_origin) {
b5256303 1707 dsl_dataset_rele_flags(ds, dsflags, FTAG);
2e528b49 1708 return (SET_ERROR(EINVAL));
13fe0198
MA
1709 }
1710
1711 error = recv_begin_check_existing_impl(drba, ds, fromguid);
b5256303 1712 dsl_dataset_rele_flags(ds, dsflags, FTAG);
13fe0198
MA
1713 } else if (error == ENOENT) {
1714 /* target fs does not exist; must be a full backup or clone */
eca7b760 1715 char buf[ZFS_MAX_DATASET_NAME_LEN];
13fe0198
MA
1716
1717 /*
1718 * If it's a non-clone incremental, we are missing the
1719 * target fs, so fail the recv.
1720 */
fcff0f35
PD
1721 if (fromguid != 0 && !(flags & DRR_FLAG_CLONE ||
1722 drba->drba_origin))
2e528b49 1723 return (SET_ERROR(ENOENT));
13fe0198 1724
e6d3a843
PD
1725 /*
1726 * If we're receiving a full send as a clone, and it doesn't
1727 * contain all the necessary free records and freeobject
1728 * records, reject it.
1729 */
1730 if (fromguid == 0 && drba->drba_origin &&
1731 !(flags & DRR_FLAG_FREERECORDS))
1732 return (SET_ERROR(EINVAL));
1733
13fe0198 1734 /* Open the parent of tofs */
eca7b760 1735 ASSERT3U(strlen(tofs), <, sizeof (buf));
13fe0198 1736 (void) strlcpy(buf, tofs, strrchr(tofs, '/') - tofs + 1);
b5256303 1737 error = dsl_dataset_hold_flags(dp, buf, dsflags, FTAG, &ds);
13fe0198
MA
1738 if (error != 0)
1739 return (error);
1740
788eb90c
JJ
1741 /*
1742 * Check filesystem and snapshot limits before receiving. We'll
1743 * recheck snapshot limits again at the end (we create the
1744 * filesystems and increment those counts during begin_sync).
1745 */
1746 error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
1747 ZFS_PROP_FILESYSTEM_LIMIT, NULL, drba->drba_cred);
1748 if (error != 0) {
b5256303 1749 dsl_dataset_rele_flags(ds, dsflags, FTAG);
788eb90c
JJ
1750 return (error);
1751 }
1752
1753 error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
1754 ZFS_PROP_SNAPSHOT_LIMIT, NULL, drba->drba_cred);
1755 if (error != 0) {
b5256303 1756 dsl_dataset_rele_flags(ds, dsflags, FTAG);
788eb90c
JJ
1757 return (error);
1758 }
1759
13fe0198
MA
1760 if (drba->drba_origin != NULL) {
1761 dsl_dataset_t *origin;
b5256303
TC
1762
1763 error = dsl_dataset_hold_flags(dp, drba->drba_origin,
1764 dsflags, FTAG, &origin);
13fe0198 1765 if (error != 0) {
b5256303 1766 dsl_dataset_rele_flags(ds, dsflags, FTAG);
13fe0198
MA
1767 return (error);
1768 }
0c66c32d 1769 if (!origin->ds_is_snapshot) {
9b840763 1770 dsl_dataset_rele_flags(origin, dsflags, FTAG);
b5256303 1771 dsl_dataset_rele_flags(ds, dsflags, FTAG);
2e528b49 1772 return (SET_ERROR(EINVAL));
13fe0198 1773 }
e6d3a843
PD
1774 if (dsl_dataset_phys(origin)->ds_guid != fromguid &&
1775 fromguid != 0) {
9b840763 1776 dsl_dataset_rele_flags(origin, dsflags, FTAG);
b5256303 1777 dsl_dataset_rele_flags(ds, dsflags, FTAG);
2e528b49 1778 return (SET_ERROR(ENODEV));
13fe0198 1779 }
b5256303
TC
1780 dsl_dataset_rele_flags(origin,
1781 dsflags, FTAG);
13fe0198 1782 }
b5256303 1783 dsl_dataset_rele_flags(ds, dsflags, FTAG);
13fe0198
MA
1784 error = 0;
1785 }
1786 return (error);
34dc7c2f
BB
1787}
1788
34dc7c2f 1789static void
13fe0198 1790dmu_recv_begin_sync(void *arg, dmu_tx_t *tx)
34dc7c2f 1791{
13fe0198
MA
1792 dmu_recv_begin_arg_t *drba = arg;
1793 dsl_pool_t *dp = dmu_tx_pool(tx);
47dfff3b 1794 objset_t *mos = dp->dp_meta_objset;
13fe0198
MA
1795 struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
1796 const char *tofs = drba->drba_cookie->drc_tofs;
b5256303 1797 uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
13fe0198 1798 dsl_dataset_t *ds, *newds;
b5256303 1799 objset_t *os;
34dc7c2f 1800 uint64_t dsobj;
b5256303 1801 ds_hold_flags_t dsflags = 0;
13fe0198 1802 int error;
47dfff3b 1803 uint64_t crflags = 0;
b5256303
TC
1804 dsl_crypto_params_t *dcpp = NULL;
1805 dsl_crypto_params_t dcp = { 0 };
13fe0198 1806
47dfff3b
MA
1807 if (drrb->drr_flags & DRR_FLAG_CI_DATA)
1808 crflags |= DS_FLAG_CI_DATASET;
b5256303
TC
1809 if ((featureflags & DMU_BACKUP_FEATURE_RAW) == 0) {
1810 dsflags |= DS_HOLD_FLAG_DECRYPT;
1811 } else {
1812 dcp.cp_cmd = DCP_CMD_RAW_RECV;
1813 }
34dc7c2f 1814
b5256303 1815 error = dsl_dataset_hold_flags(dp, tofs, dsflags, FTAG, &ds);
13fe0198
MA
1816 if (error == 0) {
1817 /* create temporary clone */
19580676 1818 dsl_dataset_t *snap = NULL;
b5256303 1819
19580676
MA
1820 if (drba->drba_snapobj != 0) {
1821 VERIFY0(dsl_dataset_hold_obj(dp,
1822 drba->drba_snapobj, FTAG, &snap));
b5256303
TC
1823 } else {
1824 /* we use the dcp whenever we are not making a clone */
1825 dcpp = &dcp;
19580676 1826 }
b5256303 1827
13fe0198 1828 dsobj = dsl_dataset_create_sync(ds->ds_dir, recv_clone_name,
b5256303 1829 snap, crflags, drba->drba_cred, dcpp, tx);
6b42ea85
PD
1830 if (drba->drba_snapobj != 0)
1831 dsl_dataset_rele(snap, FTAG);
b5256303 1832 dsl_dataset_rele_flags(ds, dsflags, FTAG);
13fe0198
MA
1833 } else {
1834 dsl_dir_t *dd;
1835 const char *tail;
1836 dsl_dataset_t *origin = NULL;
1837
1838 VERIFY0(dsl_dir_hold(dp, tofs, FTAG, &dd, &tail));
1839
1840 if (drba->drba_origin != NULL) {
1841 VERIFY0(dsl_dataset_hold(dp, drba->drba_origin,
1842 FTAG, &origin));
b5256303
TC
1843 } else {
1844 /* we use the dcp whenever we are not making a clone */
1845 dcpp = &dcp;
13fe0198
MA
1846 }
1847
1848 /* Create new dataset. */
b5256303
TC
1849 dsobj = dsl_dataset_create_sync(dd, strrchr(tofs, '/') + 1,
1850 origin, crflags, drba->drba_cred, dcpp, tx);
13fe0198
MA
1851 if (origin != NULL)
1852 dsl_dataset_rele(origin, FTAG);
1853 dsl_dir_rele(dd, FTAG);
1854 drba->drba_cookie->drc_newfs = B_TRUE;
1855 }
b5256303
TC
1856 VERIFY0(dsl_dataset_own_obj(dp, dsobj, dsflags, dmu_recv_tag, &newds));
1857 VERIFY0(dmu_objset_from_ds(newds, &os));
13fe0198 1858
47dfff3b 1859 if (drba->drba_cookie->drc_resumable) {
47dfff3b
MA
1860 dsl_dataset_zapify(newds, tx);
1861 if (drrb->drr_fromguid != 0) {
1862 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_FROMGUID,
1863 8, 1, &drrb->drr_fromguid, tx));
1864 }
1865 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_TOGUID,
1866 8, 1, &drrb->drr_toguid, tx));
1867 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_TONAME,
1868 1, strlen(drrb->drr_toname) + 1, drrb->drr_toname, tx));
1c27024e
DB
1869 uint64_t one = 1;
1870 uint64_t zero = 0;
47dfff3b
MA
1871 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_OBJECT,
1872 8, 1, &one, tx));
1873 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_OFFSET,
1874 8, 1, &zero, tx));
1875 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_BYTES,
1876 8, 1, &zero, tx));
b5256303 1877 if (featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) {
2aa34383 1878 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_LARGEBLOCK,
02730c33 1879 8, 1, &one, tx));
2aa34383 1880 }
b5256303 1881 if (featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) {
47dfff3b
MA
1882 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_EMBEDOK,
1883 8, 1, &one, tx));
1884 }
b5256303 1885 if (featureflags & DMU_BACKUP_FEATURE_COMPRESSED) {
2aa34383
DK
1886 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_COMPRESSOK,
1887 8, 1, &one, tx));
1888 }
b5256303
TC
1889 if (featureflags & DMU_BACKUP_FEATURE_RAW) {
1890 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_RAWOK,
1891 8, 1, &one, tx));
1892 }
1893 }
1894
1895 /*
1896 * Usually the os->os_encrypted value is tied to the presence of a
1897 * DSL Crypto Key object in the dd. However, that will not be received
1898 * until dmu_recv_stream(), so we set the value manually for now.
1899 */
1900 if (featureflags & DMU_BACKUP_FEATURE_RAW) {
1901 os->os_encrypted = B_TRUE;
1902 drba->drba_cookie->drc_raw = B_TRUE;
47dfff3b
MA
1903 }
1904
13fe0198 1905 dmu_buf_will_dirty(newds->ds_dbuf, tx);
d683ddbb 1906 dsl_dataset_phys(newds)->ds_flags |= DS_FLAG_INCONSISTENT;
34dc7c2f 1907
428870ff 1908 /*
b5256303
TC
1909 * If we actually created a non-clone, we need to create the objset
1910 * in our new dataset. If this is a raw send we postpone this until
1911 * dmu_recv_stream() so that we can allocate the metadnode with the
1912 * properties from the DRR_BEGIN payload.
428870ff 1913 */
cc9bb3e5 1914 rrw_enter(&newds->ds_bp_rwlock, RW_READER, FTAG);
b5256303
TC
1915 if (BP_IS_HOLE(dsl_dataset_get_blkptr(newds)) &&
1916 (featureflags & DMU_BACKUP_FEATURE_RAW) == 0) {
428870ff 1917 (void) dmu_objset_create_impl(dp->dp_spa,
13fe0198 1918 newds, dsl_dataset_get_blkptr(newds), drrb->drr_type, tx);
428870ff 1919 }
cc9bb3e5 1920 rrw_exit(&newds->ds_bp_rwlock, FTAG);
34dc7c2f 1921
13fe0198 1922 drba->drba_cookie->drc_ds = newds;
428870ff 1923
13fe0198 1924 spa_history_log_internal_ds(newds, "receive", tx, "");
34dc7c2f
BB
1925}
1926
47dfff3b
MA
1927static int
1928dmu_recv_resume_begin_check(void *arg, dmu_tx_t *tx)
1929{
1930 dmu_recv_begin_arg_t *drba = arg;
1931 dsl_pool_t *dp = dmu_tx_pool(tx);
1932 struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
1933 int error;
b5256303 1934 ds_hold_flags_t dsflags = 0;
47dfff3b
MA
1935 uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
1936 dsl_dataset_t *ds;
1937 const char *tofs = drba->drba_cookie->drc_tofs;
eca7b760 1938
47dfff3b
MA
1939 /* already checked */
1940 ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
1941 ASSERT(featureflags & DMU_BACKUP_FEATURE_RESUMING);
1942
1943 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
1944 DMU_COMPOUNDSTREAM ||
1945 drrb->drr_type >= DMU_OST_NUMTYPES)
1946 return (SET_ERROR(EINVAL));
1947
1948 /* Verify pool version supports SA if SA_SPILL feature set */
1949 if ((featureflags & DMU_BACKUP_FEATURE_SA_SPILL) &&
1950 spa_version(dp->dp_spa) < SPA_VERSION_SA)
1951 return (SET_ERROR(ENOTSUP));
1952
1953 /*
1954 * The receiving code doesn't know how to translate a WRITE_EMBEDDED
1955 * record to a plain WRITE record, so the pool must have the
1956 * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
1957 * records. Same with WRITE_EMBEDDED records that use LZ4 compression.
1958 */
1959 if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) &&
1960 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA))
1961 return (SET_ERROR(ENOTSUP));
2aa34383 1962 if ((featureflags & DMU_BACKUP_FEATURE_LZ4) &&
47dfff3b
MA
1963 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
1964 return (SET_ERROR(ENOTSUP));
1965
73aac4aa
NB
1966 /*
1967 * The receiving code doesn't know how to translate large blocks
1968 * to smaller ones, so the pool must have the LARGE_BLOCKS
1969 * feature enabled if the stream has LARGE_BLOCKS. Same with
1970 * large dnodes.
1971 */
1972 if ((featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
1973 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LARGE_BLOCKS))
1974 return (SET_ERROR(ENOTSUP));
1975 if ((featureflags & DMU_BACKUP_FEATURE_LARGE_DNODE) &&
1976 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LARGE_DNODE))
1977 return (SET_ERROR(ENOTSUP));
1978
1c27024e
DB
1979 /* 6 extra bytes for /%recv */
1980 char recvname[ZFS_MAX_DATASET_NAME_LEN + 6];
47dfff3b
MA
1981 (void) snprintf(recvname, sizeof (recvname), "%s/%s",
1982 tofs, recv_clone_name);
1983
b5256303
TC
1984 if ((featureflags & DMU_BACKUP_FEATURE_RAW) == 0)
1985 dsflags |= DS_HOLD_FLAG_DECRYPT;
1986
1987 if (dsl_dataset_hold_flags(dp, recvname, dsflags, FTAG, &ds) != 0) {
47dfff3b 1988 /* %recv does not exist; continue in tofs */
b5256303 1989 error = dsl_dataset_hold_flags(dp, tofs, dsflags, FTAG, &ds);
47dfff3b
MA
1990 if (error != 0)
1991 return (error);
1992 }
1993
1994 /* check that ds is marked inconsistent */
1995 if (!DS_IS_INCONSISTENT(ds)) {
b5256303 1996 dsl_dataset_rele_flags(ds, dsflags, FTAG);
47dfff3b
MA
1997 return (SET_ERROR(EINVAL));
1998 }
1999
2000 /* check that there is resuming data, and that the toguid matches */
2001 if (!dsl_dataset_is_zapified(ds)) {
b5256303 2002 dsl_dataset_rele_flags(ds, dsflags, FTAG);
47dfff3b
MA
2003 return (SET_ERROR(EINVAL));
2004 }
1c27024e 2005 uint64_t val;
47dfff3b
MA
2006 error = zap_lookup(dp->dp_meta_objset, ds->ds_object,
2007 DS_FIELD_RESUME_TOGUID, sizeof (val), 1, &val);
2008 if (error != 0 || drrb->drr_toguid != val) {
b5256303 2009 dsl_dataset_rele_flags(ds, dsflags, FTAG);
47dfff3b
MA
2010 return (SET_ERROR(EINVAL));
2011 }
2012
2013 /*
2014 * Check if the receive is still running. If so, it will be owned.
2015 * Note that nothing else can own the dataset (e.g. after the receive
2016 * fails) because it will be marked inconsistent.
2017 */
2018 if (dsl_dataset_has_owner(ds)) {
b5256303 2019 dsl_dataset_rele_flags(ds, dsflags, FTAG);
47dfff3b
MA
2020 return (SET_ERROR(EBUSY));
2021 }
2022
2023 /* There should not be any snapshots of this fs yet. */
2024 if (ds->ds_prev != NULL && ds->ds_prev->ds_dir == ds->ds_dir) {
b5256303 2025 dsl_dataset_rele_flags(ds, dsflags, FTAG);
47dfff3b
MA
2026 return (SET_ERROR(EINVAL));
2027 }
2028
2029 /*
2030 * Note: resume point will be checked when we process the first WRITE
2031 * record.
2032 */
2033
2034 /* check that the origin matches */
2035 val = 0;
2036 (void) zap_lookup(dp->dp_meta_objset, ds->ds_object,
2037 DS_FIELD_RESUME_FROMGUID, sizeof (val), 1, &val);
2038 if (drrb->drr_fromguid != val) {
b5256303 2039 dsl_dataset_rele_flags(ds, dsflags, FTAG);
47dfff3b
MA
2040 return (SET_ERROR(EINVAL));
2041 }
2042
b5256303 2043 dsl_dataset_rele_flags(ds, dsflags, FTAG);
47dfff3b
MA
2044 return (0);
2045}
2046
2047static void
2048dmu_recv_resume_begin_sync(void *arg, dmu_tx_t *tx)
2049{
2050 dmu_recv_begin_arg_t *drba = arg;
2051 dsl_pool_t *dp = dmu_tx_pool(tx);
2052 const char *tofs = drba->drba_cookie->drc_tofs;
b5256303
TC
2053 struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
2054 uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
47dfff3b 2055 dsl_dataset_t *ds;
b5256303
TC
2056 objset_t *os;
2057 ds_hold_flags_t dsflags = 0;
47dfff3b 2058 uint64_t dsobj;
eca7b760
IK
2059 /* 6 extra bytes for /%recv */
2060 char recvname[ZFS_MAX_DATASET_NAME_LEN + 6];
47dfff3b
MA
2061
2062 (void) snprintf(recvname, sizeof (recvname), "%s/%s",
2063 tofs, recv_clone_name);
2064
b5256303
TC
2065 if (featureflags & DMU_BACKUP_FEATURE_RAW) {
2066 drba->drba_cookie->drc_raw = B_TRUE;
2067 } else {
2068 dsflags |= DS_HOLD_FLAG_DECRYPT;
2069 }
2070
2071 if (dsl_dataset_hold_flags(dp, recvname, dsflags, FTAG, &ds) != 0) {
47dfff3b 2072 /* %recv does not exist; continue in tofs */
b5256303 2073 VERIFY0(dsl_dataset_hold_flags(dp, tofs, dsflags, FTAG, &ds));
47dfff3b
MA
2074 drba->drba_cookie->drc_newfs = B_TRUE;
2075 }
2076
2077 /* clear the inconsistent flag so that we can own it */
2078 ASSERT(DS_IS_INCONSISTENT(ds));
2079 dmu_buf_will_dirty(ds->ds_dbuf, tx);
2080 dsl_dataset_phys(ds)->ds_flags &= ~DS_FLAG_INCONSISTENT;
2081 dsobj = ds->ds_object;
b5256303 2082 dsl_dataset_rele_flags(ds, dsflags, FTAG);
47dfff3b 2083
b5256303
TC
2084 VERIFY0(dsl_dataset_own_obj(dp, dsobj, dsflags, dmu_recv_tag, &ds));
2085 VERIFY0(dmu_objset_from_ds(ds, &os));
47dfff3b
MA
2086
2087 dmu_buf_will_dirty(ds->ds_dbuf, tx);
2088 dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_INCONSISTENT;
2089
cc9bb3e5 2090 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
2637dda8
TC
2091 ASSERT(!BP_IS_HOLE(dsl_dataset_get_blkptr(ds)) ||
2092 drba->drba_cookie->drc_raw);
cc9bb3e5 2093 rrw_exit(&ds->ds_bp_rwlock, FTAG);
47dfff3b
MA
2094
2095 drba->drba_cookie->drc_ds = ds;
2096
2097 spa_history_log_internal_ds(ds, "resume receive", tx, "");
2098}
2099
34dc7c2f
BB
2100/*
2101 * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
2102 * succeeds; otherwise we will leak the holds on the datasets.
2103 */
2104int
47dfff3b
MA
2105dmu_recv_begin(char *tofs, char *tosnap, dmu_replay_record_t *drr_begin,
2106 boolean_t force, boolean_t resumable, char *origin, dmu_recv_cookie_t *drc)
34dc7c2f 2107{
13fe0198 2108 dmu_recv_begin_arg_t drba = { 0 };
34dc7c2f
BB
2109
2110 bzero(drc, sizeof (dmu_recv_cookie_t));
47dfff3b
MA
2111 drc->drc_drr_begin = drr_begin;
2112 drc->drc_drrb = &drr_begin->drr_u.drr_begin;
34dc7c2f 2113 drc->drc_tosnap = tosnap;
13fe0198 2114 drc->drc_tofs = tofs;
34dc7c2f 2115 drc->drc_force = force;
47dfff3b 2116 drc->drc_resumable = resumable;
788eb90c 2117 drc->drc_cred = CRED();
48fbb9dd 2118 drc->drc_clone = (origin != NULL);
34dc7c2f 2119
47dfff3b 2120 if (drc->drc_drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
13fe0198 2121 drc->drc_byteswap = B_TRUE;
a6255b7f 2122 (void) fletcher_4_incremental_byteswap(drr_begin,
13fe0198 2123 sizeof (dmu_replay_record_t), &drc->drc_cksum);
47dfff3b
MA
2124 byteswap_record(drr_begin);
2125 } else if (drc->drc_drrb->drr_magic == DMU_BACKUP_MAGIC) {
a6255b7f 2126 (void) fletcher_4_incremental_native(drr_begin,
13fe0198 2127 sizeof (dmu_replay_record_t), &drc->drc_cksum);
47dfff3b
MA
2128 } else {
2129 return (SET_ERROR(EINVAL));
34dc7c2f
BB
2130 }
2131
13fe0198
MA
2132 drba.drba_origin = origin;
2133 drba.drba_cookie = drc;
2134 drba.drba_cred = CRED();
2135
47dfff3b
MA
2136 if (DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo) &
2137 DMU_BACKUP_FEATURE_RESUMING) {
2138 return (dsl_sync_task(tofs,
2139 dmu_recv_resume_begin_check, dmu_recv_resume_begin_sync,
2140 &drba, 5, ZFS_SPACE_CHECK_NORMAL));
2141 } else {
2142 return (dsl_sync_task(tofs,
2143 dmu_recv_begin_check, dmu_recv_begin_sync,
2144 &drba, 5, ZFS_SPACE_CHECK_NORMAL));
2145 }
34dc7c2f
BB
2146}
2147
fcff0f35
PD
2148struct receive_record_arg {
2149 dmu_replay_record_t header;
2150 void *payload; /* Pointer to a buffer containing the payload */
2151 /*
2152 * If the record is a write, pointer to the arc_buf_t containing the
2153 * payload.
2154 */
b5256303 2155 arc_buf_t *arc_buf;
fcff0f35 2156 int payload_size;
47dfff3b 2157 uint64_t bytes_read; /* bytes read from stream when record created */
fcff0f35
PD
2158 boolean_t eos_marker; /* Marks the end of the stream */
2159 bqueue_node_t node;
2160};
2161
2162struct receive_writer_arg {
37f8a883 2163 objset_t *os;
13fe0198 2164 boolean_t byteswap;
fcff0f35 2165 bqueue_t q;
47dfff3b 2166
fcff0f35
PD
2167 /*
2168 * These three args are used to signal to the main thread that we're
2169 * done.
2170 */
2171 kmutex_t mutex;
2172 kcondvar_t cv;
2173 boolean_t done;
47dfff3b 2174
fcff0f35
PD
2175 int err;
2176 /* A map from guid to dataset to help handle dedup'd streams. */
2177 avl_tree_t *guid_to_ds_map;
47dfff3b 2178 boolean_t resumable;
9b840763 2179 boolean_t raw;
48fbb9dd
FG
2180 uint64_t last_object;
2181 uint64_t last_offset;
2182 uint64_t max_object; /* highest object ID referenced in stream */
47dfff3b 2183 uint64_t bytes_read; /* bytes read when current record created */
095495e0
TC
2184
2185 /* Encryption parameters for the last received DRR_OBJECT_RANGE */
0c03d21a 2186 boolean_t or_crypt_params_present;
095495e0
TC
2187 uint64_t or_firstobj;
2188 uint64_t or_numslots;
2189 uint8_t or_salt[ZIO_DATA_SALT_LEN];
2190 uint8_t or_iv[ZIO_DATA_IV_LEN];
2191 uint8_t or_mac[ZIO_DATA_MAC_LEN];
2192 boolean_t or_byteorder;
fcff0f35 2193};
37f8a883 2194
e6d3a843
PD
2195struct objlist {
2196 list_t list; /* List of struct receive_objnode. */
2197 /*
2198 * Last object looked up. Used to assert that objects are being looked
2199 * up in ascending order.
2200 */
2201 uint64_t last_lookup;
2202};
2203
2204struct receive_objnode {
2205 list_node_t node;
2206 uint64_t object;
2207};
2208
fcff0f35
PD
2209struct receive_arg {
2210 objset_t *os;
2211 vnode_t *vp; /* The vnode to read the stream from */
2212 uint64_t voff; /* The current offset in the stream */
47dfff3b 2213 uint64_t bytes_read;
fcff0f35
PD
2214 /*
2215 * A record that has had its payload read in, but hasn't yet been handed
2216 * off to the worker thread.
2217 */
2218 struct receive_record_arg *rrd;
2219 /* A record that has had its header read in, but not its payload. */
2220 struct receive_record_arg *next_rrd;
34dc7c2f 2221 zio_cksum_t cksum;
37f8a883 2222 zio_cksum_t prev_cksum;
fcff0f35
PD
2223 int err;
2224 boolean_t byteswap;
9b840763 2225 boolean_t raw;
b5256303 2226 uint64_t featureflags;
fcff0f35 2227 /* Sorted list of objects not to issue prefetches for. */
e6d3a843 2228 struct objlist ignore_objlist;
34dc7c2f
BB
2229};
2230
428870ff
BB
2231typedef struct guid_map_entry {
2232 uint64_t guid;
b5256303 2233 boolean_t raw;
428870ff
BB
2234 dsl_dataset_t *gme_ds;
2235 avl_node_t avlnode;
2236} guid_map_entry_t;
2237
2238static int
2239guid_compare(const void *arg1, const void *arg2)
2240{
ee36c709
GN
2241 const guid_map_entry_t *gmep1 = (const guid_map_entry_t *)arg1;
2242 const guid_map_entry_t *gmep2 = (const guid_map_entry_t *)arg2;
428870ff 2243
ee36c709 2244 return (AVL_CMP(gmep1->guid, gmep2->guid));
428870ff
BB
2245}
2246
572e2857
BB
2247static void
2248free_guid_map_onexit(void *arg)
2249{
2250 avl_tree_t *ca = arg;
2251 void *cookie = NULL;
2252 guid_map_entry_t *gmep;
2253
2254 while ((gmep = avl_destroy_nodes(ca, &cookie)) != NULL) {
13fe0198 2255 dsl_dataset_long_rele(gmep->gme_ds, gmep);
b5256303
TC
2256 dsl_dataset_rele_flags(gmep->gme_ds,
2257 (gmep->raw) ? 0 : DS_HOLD_FLAG_DECRYPT, gmep);
572e2857
BB
2258 kmem_free(gmep, sizeof (guid_map_entry_t));
2259 }
2260 avl_destroy(ca);
2261 kmem_free(ca, sizeof (avl_tree_t));
2262}
2263
37f8a883 2264static int
fcff0f35 2265receive_read(struct receive_arg *ra, int len, void *buf)
34dc7c2f 2266{
34dc7c2f
BB
2267 int done = 0;
2268
f8866f8a
ER
2269 /*
2270 * The code doesn't rely on this (lengths being multiples of 8). See
2271 * comment in dump_bytes.
2272 */
b5256303
TC
2273 ASSERT(len % 8 == 0 ||
2274 (ra->featureflags & DMU_BACKUP_FEATURE_RAW) != 0);
34dc7c2f
BB
2275
2276 while (done < len) {
2277 ssize_t resid;
2278
2279 ra->err = vn_rdwr(UIO_READ, ra->vp,
37f8a883 2280 (char *)buf + done, len - done,
34dc7c2f
BB
2281 ra->voff, UIO_SYSSPACE, FAPPEND,
2282 RLIM64_INFINITY, CRED(), &resid);
2283
47dfff3b
MA
2284 if (resid == len - done) {
2285 /*
2286 * Note: ECKSUM indicates that the receive
2287 * was interrupted and can potentially be resumed.
2288 */
2289 ra->err = SET_ERROR(ECKSUM);
2290 }
34dc7c2f
BB
2291 ra->voff += len - done - resid;
2292 done = len - resid;
13fe0198 2293 if (ra->err != 0)
37f8a883 2294 return (ra->err);
34dc7c2f
BB
2295 }
2296
47dfff3b
MA
2297 ra->bytes_read += len;
2298
34dc7c2f 2299 ASSERT3U(done, ==, len);
37f8a883 2300 return (0);
34dc7c2f
BB
2301}
2302
60948de1 2303noinline static void
37f8a883 2304byteswap_record(dmu_replay_record_t *drr)
34dc7c2f
BB
2305{
2306#define DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
2307#define DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
2308 drr->drr_type = BSWAP_32(drr->drr_type);
2309 drr->drr_payloadlen = BSWAP_32(drr->drr_payloadlen);
37f8a883 2310
34dc7c2f
BB
2311 switch (drr->drr_type) {
2312 case DRR_BEGIN:
2313 DO64(drr_begin.drr_magic);
428870ff 2314 DO64(drr_begin.drr_versioninfo);
34dc7c2f
BB
2315 DO64(drr_begin.drr_creation_time);
2316 DO32(drr_begin.drr_type);
2317 DO32(drr_begin.drr_flags);
2318 DO64(drr_begin.drr_toguid);
2319 DO64(drr_begin.drr_fromguid);
2320 break;
2321 case DRR_OBJECT:
2322 DO64(drr_object.drr_object);
34dc7c2f
BB
2323 DO32(drr_object.drr_type);
2324 DO32(drr_object.drr_bonustype);
2325 DO32(drr_object.drr_blksz);
2326 DO32(drr_object.drr_bonuslen);
b5256303 2327 DO32(drr_object.drr_raw_bonuslen);
428870ff 2328 DO64(drr_object.drr_toguid);
ae76f45c 2329 DO64(drr_object.drr_maxblkid);
34dc7c2f
BB
2330 break;
2331 case DRR_FREEOBJECTS:
2332 DO64(drr_freeobjects.drr_firstobj);
2333 DO64(drr_freeobjects.drr_numobjs);
428870ff 2334 DO64(drr_freeobjects.drr_toguid);
34dc7c2f
BB
2335 break;
2336 case DRR_WRITE:
2337 DO64(drr_write.drr_object);
2338 DO32(drr_write.drr_type);
2339 DO64(drr_write.drr_offset);
2aa34383 2340 DO64(drr_write.drr_logical_size);
428870ff 2341 DO64(drr_write.drr_toguid);
37f8a883 2342 ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_write.drr_key.ddk_cksum);
428870ff 2343 DO64(drr_write.drr_key.ddk_prop);
2aa34383 2344 DO64(drr_write.drr_compressed_size);
428870ff
BB
2345 break;
2346 case DRR_WRITE_BYREF:
2347 DO64(drr_write_byref.drr_object);
2348 DO64(drr_write_byref.drr_offset);
2349 DO64(drr_write_byref.drr_length);
2350 DO64(drr_write_byref.drr_toguid);
2351 DO64(drr_write_byref.drr_refguid);
2352 DO64(drr_write_byref.drr_refobject);
2353 DO64(drr_write_byref.drr_refoffset);
37f8a883
MA
2354 ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_write_byref.
2355 drr_key.ddk_cksum);
428870ff 2356 DO64(drr_write_byref.drr_key.ddk_prop);
34dc7c2f 2357 break;
9b67f605
MA
2358 case DRR_WRITE_EMBEDDED:
2359 DO64(drr_write_embedded.drr_object);
2360 DO64(drr_write_embedded.drr_offset);
2361 DO64(drr_write_embedded.drr_length);
2362 DO64(drr_write_embedded.drr_toguid);
2363 DO32(drr_write_embedded.drr_lsize);
2364 DO32(drr_write_embedded.drr_psize);
2365 break;
34dc7c2f
BB
2366 case DRR_FREE:
2367 DO64(drr_free.drr_object);
2368 DO64(drr_free.drr_offset);
2369 DO64(drr_free.drr_length);
428870ff
BB
2370 DO64(drr_free.drr_toguid);
2371 break;
2372 case DRR_SPILL:
2373 DO64(drr_spill.drr_object);
2374 DO64(drr_spill.drr_length);
2375 DO64(drr_spill.drr_toguid);
b5256303
TC
2376 DO64(drr_spill.drr_compressed_size);
2377 DO32(drr_spill.drr_type);
2378 break;
2379 case DRR_OBJECT_RANGE:
2380 DO64(drr_object_range.drr_firstobj);
2381 DO64(drr_object_range.drr_numslots);
2382 DO64(drr_object_range.drr_toguid);
34dc7c2f
BB
2383 break;
2384 case DRR_END:
428870ff 2385 DO64(drr_end.drr_toguid);
37f8a883 2386 ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_end.drr_checksum);
34dc7c2f 2387 break;
e75c13c3
BB
2388 default:
2389 break;
34dc7c2f 2390 }
37f8a883
MA
2391
2392 if (drr->drr_type != DRR_BEGIN) {
2393 ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_checksum.drr_checksum);
2394 }
2395
34dc7c2f
BB
2396#undef DO64
2397#undef DO32
2398}
2399
6c59307a
MA
2400static inline uint8_t
2401deduce_nblkptr(dmu_object_type_t bonus_type, uint64_t bonus_size)
2402{
2403 if (bonus_type == DMU_OT_SA) {
2404 return (1);
2405 } else {
2406 return (1 +
50c957f7
NB
2407 ((DN_OLD_MAX_BONUSLEN -
2408 MIN(DN_OLD_MAX_BONUSLEN, bonus_size)) >> SPA_BLKPTRSHIFT));
6c59307a
MA
2409 }
2410}
2411
47dfff3b
MA
2412static void
2413save_resume_state(struct receive_writer_arg *rwa,
2414 uint64_t object, uint64_t offset, dmu_tx_t *tx)
2415{
2416 int txgoff = dmu_tx_get_txg(tx) & TXG_MASK;
2417
2418 if (!rwa->resumable)
2419 return;
2420
2421 /*
2422 * We use ds_resume_bytes[] != 0 to indicate that we need to
2423 * update this on disk, so it must not be 0.
2424 */
2425 ASSERT(rwa->bytes_read != 0);
2426
2427 /*
2428 * We only resume from write records, which have a valid
2429 * (non-meta-dnode) object number.
2430 */
2431 ASSERT(object != 0);
2432
2433 /*
2434 * For resuming to work correctly, we must receive records in order,
2435 * sorted by object,offset. This is checked by the callers, but
2436 * assert it here for good measure.
2437 */
2438 ASSERT3U(object, >=, rwa->os->os_dsl_dataset->ds_resume_object[txgoff]);
2439 ASSERT(object != rwa->os->os_dsl_dataset->ds_resume_object[txgoff] ||
2440 offset >= rwa->os->os_dsl_dataset->ds_resume_offset[txgoff]);
2441 ASSERT3U(rwa->bytes_read, >=,
2442 rwa->os->os_dsl_dataset->ds_resume_bytes[txgoff]);
2443
2444 rwa->os->os_dsl_dataset->ds_resume_object[txgoff] = object;
2445 rwa->os->os_dsl_dataset->ds_resume_offset[txgoff] = offset;
2446 rwa->os->os_dsl_dataset->ds_resume_bytes[txgoff] = rwa->bytes_read;
2447}
2448
60948de1 2449noinline static int
fcff0f35 2450receive_object(struct receive_writer_arg *rwa, struct drr_object *drro,
4ea3f864 2451 void *data)
34dc7c2f 2452{
6c59307a 2453 dmu_object_info_t doi;
34dc7c2f 2454 dmu_tx_t *tx;
6c59307a
MA
2455 uint64_t object;
2456 int err;
34dc7c2f 2457
34dc7c2f 2458 if (drro->drr_type == DMU_OT_NONE ||
9ae529ec
CS
2459 !DMU_OT_IS_VALID(drro->drr_type) ||
2460 !DMU_OT_IS_VALID(drro->drr_bonustype) ||
428870ff 2461 drro->drr_checksumtype >= ZIO_CHECKSUM_FUNCTIONS ||
34dc7c2f
BB
2462 drro->drr_compress >= ZIO_COMPRESS_FUNCTIONS ||
2463 P2PHASE(drro->drr_blksz, SPA_MINBLOCKSIZE) ||
2464 drro->drr_blksz < SPA_MINBLOCKSIZE ||
fcff0f35 2465 drro->drr_blksz > spa_maxblocksize(dmu_objset_spa(rwa->os)) ||
50c957f7 2466 drro->drr_bonuslen >
73aac4aa
NB
2467 DN_BONUS_SIZE(spa_maxdnodesize(dmu_objset_spa(rwa->os))) ||
2468 drro->drr_dn_slots >
2469 (spa_maxdnodesize(dmu_objset_spa(rwa->os)) >> DNODE_SHIFT)) {
2e528b49 2470 return (SET_ERROR(EINVAL));
34dc7c2f
BB
2471 }
2472
9b840763 2473 if (rwa->raw) {
095495e0
TC
2474 /*
2475 * We should have received a DRR_OBJECT_RANGE record
2476 * containing this block and stored it in rwa.
2477 */
2478 if (drro->drr_object < rwa->or_firstobj ||
2479 drro->drr_object >= rwa->or_firstobj + rwa->or_numslots ||
2480 drro->drr_raw_bonuslen < drro->drr_bonuslen ||
b5256303
TC
2481 drro->drr_indblkshift > SPA_MAXBLOCKSHIFT ||
2482 drro->drr_nlevels > DN_MAX_LEVELS ||
2483 drro->drr_nblkptr > DN_MAX_NBLKPTR ||
2484 DN_SLOTS_TO_BONUSLEN(drro->drr_dn_slots) <
2485 drro->drr_raw_bonuslen)
2486 return (SET_ERROR(EINVAL));
2487 } else {
2488 if (drro->drr_flags != 0 || drro->drr_raw_bonuslen != 0 ||
2489 drro->drr_indblkshift != 0 || drro->drr_nlevels != 0 ||
2490 drro->drr_nblkptr != 0)
2491 return (SET_ERROR(EINVAL));
2492 }
2493
fcff0f35 2494 err = dmu_object_info(rwa->os, drro->drr_object, &doi);
047116ac 2495 if (err != 0 && err != ENOENT && err != EEXIST)
2e528b49 2496 return (SET_ERROR(EINVAL));
9babb374 2497
48fbb9dd
FG
2498 if (drro->drr_object > rwa->max_object)
2499 rwa->max_object = drro->drr_object;
2500
6c59307a
MA
2501 /*
2502 * If we are losing blkptrs or changing the block size this must
2503 * be a new file instance. We must clear out the previous file
2504 * contents before we can change this type of metadata in the dnode.
b5256303
TC
2505 * Raw receives will also check that the indirect structure of the
2506 * dnode hasn't changed.
6c59307a
MA
2507 */
2508 if (err == 0) {
b5256303
TC
2509 uint32_t indblksz = drro->drr_indblkshift ?
2510 1ULL << drro->drr_indblkshift : 0;
2511 int nblkptr = deduce_nblkptr(drro->drr_bonustype,
6c59307a
MA
2512 drro->drr_bonuslen);
2513
047116ac
TC
2514 object = drro->drr_object;
2515
b5256303 2516 /* nblkptr will be bounded by the bonus size and type */
9b840763 2517 if (rwa->raw && nblkptr != drro->drr_nblkptr)
b5256303
TC
2518 return (SET_ERROR(EINVAL));
2519
0c03d21a 2520 if (drro->drr_blksz != doi.doi_data_block_size ||
b5256303 2521 nblkptr < doi.doi_nblkptr ||
0c03d21a
MA
2522 drro->drr_dn_slots != doi.doi_dnodesize >> DNODE_SHIFT ||
2523 (rwa->raw &&
2524 (indblksz != doi.doi_metadata_block_size ||
2525 drro->drr_nlevels < doi.doi_indirection))) {
2526 err = dmu_free_long_range(rwa->os,
ae76f45c
TC
2527 drro->drr_object, 0, DMU_OBJECT_END);
2528 if (err != 0)
2529 return (SET_ERROR(EINVAL));
34dc7c2f 2530 }
047116ac
TC
2531
2532 /*
2533 * The dmu does not currently support decreasing nlevels
2534 * on an object. For non-raw sends, this does not matter
2535 * and the new object can just use the previous one's nlevels.
2536 * For raw sends, however, the structure of the received dnode
2537 * (including nlevels) must match that of the send side.
2538 * Therefore, instead of using dmu_object_reclaim(), we must
2539 * free the object completely and call dmu_object_claim_dnsize()
2540 * instead.
2541 */
2542 if ((rwa->raw && drro->drr_nlevels < doi.doi_indirection) ||
2543 drro->drr_dn_slots != doi.doi_dnodesize >> DNODE_SHIFT) {
0c03d21a 2544 err = dmu_free_long_object(rwa->os, drro->drr_object);
047116ac
TC
2545 if (err != 0)
2546 return (SET_ERROR(EINVAL));
2547
2548 txg_wait_synced(dmu_objset_pool(rwa->os), 0);
2549 object = DMU_NEW_OBJECT;
2550 }
2551 } else if (err == EEXIST) {
2552 /*
2553 * The object requested is currently an interior slot of a
2554 * multi-slot dnode. This will be resolved when the next txg
2555 * is synced out, since the send stream will have told us
2556 * to free this slot when we freed the associated dnode
2557 * earlier in the stream.
2558 */
2559 txg_wait_synced(dmu_objset_pool(rwa->os), 0);
2560 object = drro->drr_object;
2561 } else {
2562 /* object is free and we are about to allocate a new one */
2563 object = DMU_NEW_OBJECT;
2564 }
2565
2566 /*
2567 * If this is a multi-slot dnode there is a chance that this
2568 * object will expand into a slot that is already used by
2569 * another object from the previous snapshot. We must free
2570 * these objects before we attempt to allocate the new dnode.
2571 */
2572 if (drro->drr_dn_slots > 1) {
5121c4fb
TC
2573 boolean_t need_sync = B_FALSE;
2574
047116ac
TC
2575 for (uint64_t slot = drro->drr_object + 1;
2576 slot < drro->drr_object + drro->drr_dn_slots;
2577 slot++) {
2578 dmu_object_info_t slot_doi;
2579
2580 err = dmu_object_info(rwa->os, slot, &slot_doi);
2581 if (err == ENOENT || err == EEXIST)
2582 continue;
2583 else if (err != 0)
2584 return (err);
2585
0c03d21a 2586 err = dmu_free_long_object(rwa->os, slot);
047116ac
TC
2587
2588 if (err != 0)
2589 return (err);
5121c4fb
TC
2590
2591 need_sync = B_TRUE;
047116ac
TC
2592 }
2593
5121c4fb
TC
2594 if (need_sync)
2595 txg_wait_synced(dmu_objset_pool(rwa->os), 0);
6c59307a
MA
2596 }
2597
fcff0f35 2598 tx = dmu_tx_create(rwa->os);
6c59307a 2599 dmu_tx_hold_bonus(tx, object);
b5256303 2600 dmu_tx_hold_write(tx, object, 0, 0);
6c59307a
MA
2601 err = dmu_tx_assign(tx, TXG_WAIT);
2602 if (err != 0) {
2603 dmu_tx_abort(tx);
2604 return (err);
2605 }
2606
2607 if (object == DMU_NEW_OBJECT) {
2608 /* currently free, want to be allocated */
50c957f7 2609 err = dmu_object_claim_dnsize(rwa->os, drro->drr_object,
34dc7c2f 2610 drro->drr_type, drro->drr_blksz,
50c957f7
NB
2611 drro->drr_bonustype, drro->drr_bonuslen,
2612 drro->drr_dn_slots << DNODE_SHIFT, tx);
6c59307a
MA
2613 } else if (drro->drr_type != doi.doi_type ||
2614 drro->drr_blksz != doi.doi_data_block_size ||
2615 drro->drr_bonustype != doi.doi_bonus_type ||
e0dd0a32 2616 drro->drr_bonuslen != doi.doi_bonus_size) {
6c59307a 2617 /* currently allocated, but with different properties */
e14a32b1 2618 err = dmu_object_reclaim_dnsize(rwa->os, drro->drr_object,
34dc7c2f 2619 drro->drr_type, drro->drr_blksz,
e14a32b1
TC
2620 drro->drr_bonustype, drro->drr_bonuslen,
2621 drro->drr_dn_slots << DNODE_SHIFT, tx);
34dc7c2f 2622 }
13fe0198 2623 if (err != 0) {
9b840763 2624 dmu_tx_commit(tx);
2e528b49 2625 return (SET_ERROR(EINVAL));
428870ff 2626 }
9babb374 2627
0c03d21a 2628 if (rwa->or_crypt_params_present) {
095495e0 2629 /*
0c03d21a
MA
2630 * Set the crypt params for the buffer associated with this
2631 * range of dnodes. This causes the blkptr_t to have the
2632 * same crypt params (byteorder, salt, iv, mac) as on the
2633 * sending side.
2634 *
2635 * Since we are committing this tx now, it is possible for
2636 * the dnode block to end up on-disk with the incorrect MAC,
2637 * if subsequent objects in this block are received in a
2638 * different txg. However, since the dataset is marked as
2639 * inconsistent, no code paths will do a non-raw read (or
2640 * decrypt the block / verify the MAC). The receive code and
2641 * scrub code can safely do raw reads and verify the
2642 * checksum. They don't need to verify the MAC.
095495e0 2643 */
0c03d21a
MA
2644 dmu_buf_t *db = NULL;
2645 uint64_t offset = rwa->or_firstobj * DNODE_MIN_SIZE;
2646
2647 err = dmu_buf_hold_by_dnode(DMU_META_DNODE(rwa->os),
2648 offset, FTAG, &db, DMU_READ_PREFETCH | DMU_READ_NO_DECRYPT);
095495e0
TC
2649 if (err != 0) {
2650 dmu_tx_commit(tx);
2651 return (SET_ERROR(EINVAL));
2652 }
0c03d21a
MA
2653
2654 dmu_buf_set_crypt_params(db, rwa->or_byteorder,
2655 rwa->or_salt, rwa->or_iv, rwa->or_mac, tx);
2656
2657 dmu_buf_rele(db, FTAG);
2658
2659 rwa->or_crypt_params_present = B_FALSE;
095495e0 2660 }
9b840763 2661
fcff0f35 2662 dmu_object_set_checksum(rwa->os, drro->drr_object,
37f8a883 2663 drro->drr_checksumtype, tx);
fcff0f35 2664 dmu_object_set_compress(rwa->os, drro->drr_object,
37f8a883 2665 drro->drr_compress, tx);
34dc7c2f 2666
b5256303 2667 /* handle more restrictive dnode structuring for raw recvs */
9b840763 2668 if (rwa->raw) {
b5256303
TC
2669 /*
2670 * Set the indirect block shift and nlevels. This will not fail
2671 * because we ensured all of the blocks were free earlier if
2672 * this is a new object.
2673 */
2674 VERIFY0(dmu_object_set_blocksize(rwa->os, drro->drr_object,
2675 drro->drr_blksz, drro->drr_indblkshift, tx));
2676 VERIFY0(dmu_object_set_nlevels(rwa->os, drro->drr_object,
2677 drro->drr_nlevels, tx));
ae76f45c
TC
2678 VERIFY0(dmu_object_set_maxblkid(rwa->os, drro->drr_object,
2679 drro->drr_maxblkid, tx));
b5256303
TC
2680 }
2681
b128c09f 2682 if (data != NULL) {
34dc7c2f 2683 dmu_buf_t *db;
b5256303 2684 uint32_t flags = DMU_READ_NO_PREFETCH;
b128c09f 2685
9b840763 2686 if (rwa->raw)
b5256303
TC
2687 flags |= DMU_READ_NO_DECRYPT;
2688
2689 VERIFY0(dmu_bonus_hold_impl(rwa->os, drro->drr_object,
2690 FTAG, flags, &db));
34dc7c2f
BB
2691 dmu_buf_will_dirty(db, tx);
2692
2693 ASSERT3U(db->db_size, >=, drro->drr_bonuslen);
b5256303
TC
2694 bcopy(data, db->db_data, DRR_OBJECT_PAYLOAD_SIZE(drro));
2695
2696 /*
2697 * Raw bonus buffers have their byteorder determined by the
2698 * DRR_OBJECT_RANGE record.
2699 */
9b840763 2700 if (rwa->byteswap && !rwa->raw) {
9ae529ec
CS
2701 dmu_object_byteswap_t byteswap =
2702 DMU_OT_BYTESWAP(drro->drr_bonustype);
2703 dmu_ot_byteswap[byteswap].ob_func(db->db_data,
b5256303 2704 DRR_OBJECT_PAYLOAD_SIZE(drro));
34dc7c2f
BB
2705 }
2706 dmu_buf_rele(db, FTAG);
2707 }
2708 dmu_tx_commit(tx);
47dfff3b 2709
34dc7c2f
BB
2710 return (0);
2711}
2712
2713/* ARGSUSED */
60948de1 2714noinline static int
fcff0f35 2715receive_freeobjects(struct receive_writer_arg *rwa,
34dc7c2f
BB
2716 struct drr_freeobjects *drrfo)
2717{
2718 uint64_t obj;
e6d3a843 2719 int next_err = 0;
34dc7c2f
BB
2720
2721 if (drrfo->drr_firstobj + drrfo->drr_numobjs < drrfo->drr_firstobj)
2e528b49 2722 return (SET_ERROR(EINVAL));
34dc7c2f 2723
50c957f7 2724 for (obj = drrfo->drr_firstobj == 0 ? 1 : drrfo->drr_firstobj;
e6d3a843
PD
2725 obj < drrfo->drr_firstobj + drrfo->drr_numobjs && next_err == 0;
2726 next_err = dmu_object_next(rwa->os, &obj, FALSE, 0)) {
50c957f7 2727 dmu_object_info_t doi;
34dc7c2f
BB
2728 int err;
2729
50c957f7 2730 err = dmu_object_info(rwa->os, obj, &doi);
39f56627 2731 if (err == ENOENT)
34dc7c2f 2732 continue;
39f56627 2733 else if (err != 0)
50c957f7 2734 return (err);
34dc7c2f 2735
0c03d21a 2736 err = dmu_free_long_object(rwa->os, obj);
440a3eb9 2737
13fe0198 2738 if (err != 0)
34dc7c2f 2739 return (err);
48fbb9dd
FG
2740
2741 if (obj > rwa->max_object)
2742 rwa->max_object = obj;
34dc7c2f 2743 }
e6d3a843
PD
2744 if (next_err != ESRCH)
2745 return (next_err);
34dc7c2f
BB
2746 return (0);
2747}
2748
60948de1 2749noinline static int
fcff0f35 2750receive_write(struct receive_writer_arg *rwa, struct drr_write *drrw,
4ea3f864 2751 arc_buf_t *abuf)
34dc7c2f 2752{
34dc7c2f 2753 int err;
440a3eb9
TC
2754 dmu_tx_t *tx;
2755 dnode_t *dn;
34dc7c2f 2756
2aa34383 2757 if (drrw->drr_offset + drrw->drr_logical_size < drrw->drr_offset ||
9ae529ec 2758 !DMU_OT_IS_VALID(drrw->drr_type))
2e528b49 2759 return (SET_ERROR(EINVAL));
34dc7c2f 2760
47dfff3b
MA
2761 /*
2762 * For resuming to work, records must be in increasing order
2763 * by (object, offset).
2764 */
2765 if (drrw->drr_object < rwa->last_object ||
2766 (drrw->drr_object == rwa->last_object &&
2767 drrw->drr_offset < rwa->last_offset)) {
2768 return (SET_ERROR(EINVAL));
2769 }
2770 rwa->last_object = drrw->drr_object;
2771 rwa->last_offset = drrw->drr_offset;
2772
48fbb9dd
FG
2773 if (rwa->last_object > rwa->max_object)
2774 rwa->max_object = rwa->last_object;
2775
fcff0f35 2776 if (dmu_object_info(rwa->os, drrw->drr_object, NULL) != 0)
88904bb3
MA
2777 return (SET_ERROR(EINVAL));
2778
fcff0f35 2779 tx = dmu_tx_create(rwa->os);
34dc7c2f 2780 dmu_tx_hold_write(tx, drrw->drr_object,
2aa34383 2781 drrw->drr_offset, drrw->drr_logical_size);
34dc7c2f 2782 err = dmu_tx_assign(tx, TXG_WAIT);
13fe0198 2783 if (err != 0) {
34dc7c2f
BB
2784 dmu_tx_abort(tx);
2785 return (err);
2786 }
9b840763 2787
b5256303
TC
2788 if (rwa->byteswap && !arc_is_encrypted(abuf) &&
2789 arc_get_compression(abuf) == ZIO_COMPRESS_OFF) {
9ae529ec
CS
2790 dmu_object_byteswap_t byteswap =
2791 DMU_OT_BYTESWAP(drrw->drr_type);
37f8a883 2792 dmu_ot_byteswap[byteswap].ob_func(abuf->b_data,
2aa34383 2793 DRR_WRITE_PAYLOAD_SIZE(drrw));
9ae529ec 2794 }
37f8a883 2795
440a3eb9
TC
2796 VERIFY0(dnode_hold(rwa->os, drrw->drr_object, FTAG, &dn));
2797 dmu_assign_arcbuf_by_dnode(dn, drrw->drr_offset, abuf, tx);
2798 dnode_rele(dn, FTAG);
47dfff3b
MA
2799
2800 /*
2801 * Note: If the receive fails, we want the resume stream to start
2802 * with the same record that we last successfully received (as opposed
2803 * to the next record), so that we can verify that we are
2804 * resuming from the correct location.
2805 */
2806 save_resume_state(rwa, drrw->drr_object, drrw->drr_offset, tx);
34dc7c2f 2807 dmu_tx_commit(tx);
47dfff3b 2808
34dc7c2f
BB
2809 return (0);
2810}
2811
428870ff
BB
2812/*
2813 * Handle a DRR_WRITE_BYREF record. This record is used in dedup'ed
2814 * streams to refer to a copy of the data that is already on the
2815 * system because it came in earlier in the stream. This function
2816 * finds the earlier copy of the data, and uses that copy instead of
2817 * data from the stream to fulfill this write.
2818 */
2819static int
fcff0f35
PD
2820receive_write_byref(struct receive_writer_arg *rwa,
2821 struct drr_write_byref *drrwbr)
428870ff
BB
2822{
2823 dmu_tx_t *tx;
2824 int err;
2825 guid_map_entry_t gmesrch;
2826 guid_map_entry_t *gmep;
9b67f605 2827 avl_index_t where;
428870ff 2828 objset_t *ref_os = NULL;
b5256303 2829 int flags = DMU_READ_PREFETCH;
428870ff
BB
2830 dmu_buf_t *dbp;
2831
2832 if (drrwbr->drr_offset + drrwbr->drr_length < drrwbr->drr_offset)
2e528b49 2833 return (SET_ERROR(EINVAL));
428870ff
BB
2834
2835 /*
2836 * If the GUID of the referenced dataset is different from the
2837 * GUID of the target dataset, find the referenced dataset.
2838 */
2839 if (drrwbr->drr_toguid != drrwbr->drr_refguid) {
2840 gmesrch.guid = drrwbr->drr_refguid;
fcff0f35 2841 if ((gmep = avl_find(rwa->guid_to_ds_map, &gmesrch,
428870ff 2842 &where)) == NULL) {
2e528b49 2843 return (SET_ERROR(EINVAL));
428870ff
BB
2844 }
2845 if (dmu_objset_from_ds(gmep->gme_ds, &ref_os))
2e528b49 2846 return (SET_ERROR(EINVAL));
428870ff 2847 } else {
fcff0f35 2848 ref_os = rwa->os;
428870ff
BB
2849 }
2850
48fbb9dd
FG
2851 if (drrwbr->drr_object > rwa->max_object)
2852 rwa->max_object = drrwbr->drr_object;
2853
9b840763 2854 if (rwa->raw)
b5256303 2855 flags |= DMU_READ_NO_DECRYPT;
b5256303
TC
2856
2857 /* may return either a regular db or an encrypted one */
c65aa5b2 2858 err = dmu_buf_hold(ref_os, drrwbr->drr_refobject,
b5256303 2859 drrwbr->drr_refoffset, FTAG, &dbp, flags);
9b67f605 2860 if (err != 0)
428870ff
BB
2861 return (err);
2862
fcff0f35 2863 tx = dmu_tx_create(rwa->os);
428870ff
BB
2864
2865 dmu_tx_hold_write(tx, drrwbr->drr_object,
2866 drrwbr->drr_offset, drrwbr->drr_length);
2867 err = dmu_tx_assign(tx, TXG_WAIT);
13fe0198 2868 if (err != 0) {
428870ff
BB
2869 dmu_tx_abort(tx);
2870 return (err);
2871 }
b5256303 2872
9b840763 2873 if (rwa->raw) {
b5256303
TC
2874 dmu_copy_from_buf(rwa->os, drrwbr->drr_object,
2875 drrwbr->drr_offset, dbp, tx);
2876 } else {
2877 dmu_write(rwa->os, drrwbr->drr_object,
2878 drrwbr->drr_offset, drrwbr->drr_length, dbp->db_data, tx);
2879 }
428870ff 2880 dmu_buf_rele(dbp, FTAG);
47dfff3b
MA
2881
2882 /* See comment in restore_write. */
2883 save_resume_state(rwa, drrwbr->drr_object, drrwbr->drr_offset, tx);
428870ff
BB
2884 dmu_tx_commit(tx);
2885 return (0);
2886}
2887
9b67f605 2888static int
fcff0f35 2889receive_write_embedded(struct receive_writer_arg *rwa,
47dfff3b 2890 struct drr_write_embedded *drrwe, void *data)
9b67f605
MA
2891{
2892 dmu_tx_t *tx;
2893 int err;
9b67f605 2894
47dfff3b 2895 if (drrwe->drr_offset + drrwe->drr_length < drrwe->drr_offset)
ecb2b7dc 2896 return (SET_ERROR(EINVAL));
9b67f605 2897
47dfff3b 2898 if (drrwe->drr_psize > BPE_PAYLOAD_SIZE)
ecb2b7dc 2899 return (SET_ERROR(EINVAL));
9b67f605 2900
47dfff3b 2901 if (drrwe->drr_etype >= NUM_BP_EMBEDDED_TYPES)
ecb2b7dc 2902 return (SET_ERROR(EINVAL));
47dfff3b 2903 if (drrwe->drr_compression >= ZIO_COMPRESS_FUNCTIONS)
ecb2b7dc 2904 return (SET_ERROR(EINVAL));
440a3eb9
TC
2905 if (rwa->raw)
2906 return (SET_ERROR(EINVAL));
9b67f605 2907
48fbb9dd
FG
2908 if (drrwe->drr_object > rwa->max_object)
2909 rwa->max_object = drrwe->drr_object;
2910
fcff0f35 2911 tx = dmu_tx_create(rwa->os);
9b67f605 2912
47dfff3b
MA
2913 dmu_tx_hold_write(tx, drrwe->drr_object,
2914 drrwe->drr_offset, drrwe->drr_length);
9b67f605
MA
2915 err = dmu_tx_assign(tx, TXG_WAIT);
2916 if (err != 0) {
2917 dmu_tx_abort(tx);
2918 return (err);
2919 }
2920
47dfff3b
MA
2921 dmu_write_embedded(rwa->os, drrwe->drr_object,
2922 drrwe->drr_offset, data, drrwe->drr_etype,
2923 drrwe->drr_compression, drrwe->drr_lsize, drrwe->drr_psize,
fcff0f35 2924 rwa->byteswap ^ ZFS_HOST_BYTEORDER, tx);
9b67f605 2925
47dfff3b
MA
2926 /* See comment in restore_write. */
2927 save_resume_state(rwa, drrwe->drr_object, drrwe->drr_offset, tx);
9b67f605
MA
2928 dmu_tx_commit(tx);
2929 return (0);
2930}
2931
428870ff 2932static int
fcff0f35 2933receive_spill(struct receive_writer_arg *rwa, struct drr_spill *drrs,
b5256303 2934 arc_buf_t *abuf)
428870ff
BB
2935{
2936 dmu_tx_t *tx;
428870ff
BB
2937 dmu_buf_t *db, *db_spill;
2938 int err;
2939
2940 if (drrs->drr_length < SPA_MINBLOCKSIZE ||
fcff0f35 2941 drrs->drr_length > spa_maxblocksize(dmu_objset_spa(rwa->os)))
2e528b49 2942 return (SET_ERROR(EINVAL));
428870ff 2943
9b840763 2944 if (rwa->raw) {
b5256303
TC
2945 if (!DMU_OT_IS_VALID(drrs->drr_type) ||
2946 drrs->drr_compressiontype >= ZIO_COMPRESS_FUNCTIONS ||
2947 drrs->drr_compressed_size == 0)
2948 return (SET_ERROR(EINVAL));
2949 }
2950
fcff0f35 2951 if (dmu_object_info(rwa->os, drrs->drr_object, NULL) != 0)
2e528b49 2952 return (SET_ERROR(EINVAL));
428870ff 2953
48fbb9dd
FG
2954 if (drrs->drr_object > rwa->max_object)
2955 rwa->max_object = drrs->drr_object;
2956
fcff0f35 2957 VERIFY0(dmu_bonus_hold(rwa->os, drrs->drr_object, FTAG, &db));
428870ff
BB
2958 if ((err = dmu_spill_hold_by_bonus(db, FTAG, &db_spill)) != 0) {
2959 dmu_buf_rele(db, FTAG);
2960 return (err);
2961 }
2962
fcff0f35 2963 tx = dmu_tx_create(rwa->os);
428870ff
BB
2964
2965 dmu_tx_hold_spill(tx, db->db_object);
2966
2967 err = dmu_tx_assign(tx, TXG_WAIT);
13fe0198 2968 if (err != 0) {
428870ff
BB
2969 dmu_buf_rele(db, FTAG);
2970 dmu_buf_rele(db_spill, FTAG);
2971 dmu_tx_abort(tx);
2972 return (err);
2973 }
0c03d21a 2974 dmu_buf_will_dirty(db_spill, tx);
428870ff
BB
2975
2976 if (db_spill->db_size < drrs->drr_length)
2977 VERIFY(0 == dbuf_spill_set_blksz(db_spill,
2978 drrs->drr_length, tx));
1a234278
PZ
2979
2980 if (rwa->byteswap && !arc_is_encrypted(abuf) &&
2981 arc_get_compression(abuf) == ZIO_COMPRESS_OFF) {
2982 dmu_object_byteswap_t byteswap =
2983 DMU_OT_BYTESWAP(drrs->drr_type);
2984 dmu_ot_byteswap[byteswap].ob_func(abuf->b_data,
2985 DRR_SPILL_PAYLOAD_SIZE(drrs));
2986 }
2987
440a3eb9 2988 dbuf_assign_arcbuf((dmu_buf_impl_t *)db_spill, abuf, tx);
428870ff
BB
2989
2990 dmu_buf_rele(db, FTAG);
2991 dmu_buf_rele(db_spill, FTAG);
2992
2993 dmu_tx_commit(tx);
2994 return (0);
2995}
2996
34dc7c2f 2997/* ARGSUSED */
60948de1 2998noinline static int
fcff0f35 2999receive_free(struct receive_writer_arg *rwa, struct drr_free *drrf)
34dc7c2f 3000{
34dc7c2f
BB
3001 int err;
3002
ee45fbd8 3003 if (drrf->drr_length != DMU_OBJECT_END &&
34dc7c2f 3004 drrf->drr_offset + drrf->drr_length < drrf->drr_offset)
2e528b49 3005 return (SET_ERROR(EINVAL));
34dc7c2f 3006
fcff0f35 3007 if (dmu_object_info(rwa->os, drrf->drr_object, NULL) != 0)
2e528b49 3008 return (SET_ERROR(EINVAL));
34dc7c2f 3009
48fbb9dd
FG
3010 if (drrf->drr_object > rwa->max_object)
3011 rwa->max_object = drrf->drr_object;
3012
0c03d21a
MA
3013 err = dmu_free_long_range(rwa->os, drrf->drr_object,
3014 drrf->drr_offset, drrf->drr_length);
fcff0f35 3015
34dc7c2f
BB
3016 return (err);
3017}
3018
b5256303
TC
3019static int
3020receive_object_range(struct receive_writer_arg *rwa,
3021 struct drr_object_range *drror)
3022{
b5256303
TC
3023 /*
3024 * By default, we assume this block is in our native format
3025 * (ZFS_HOST_BYTEORDER). We then take into account whether
3026 * the send stream is byteswapped (rwa->byteswap). Finally,
3027 * we need to byteswap again if this particular block was
3028 * in non-native format on the send side.
3029 */
3030 boolean_t byteorder = ZFS_HOST_BYTEORDER ^ rwa->byteswap ^
3031 !!DRR_IS_RAW_BYTESWAPPED(drror->drr_flags);
3032
3033 /*
3034 * Since dnode block sizes are constant, we should not need to worry
3035 * about making sure that the dnode block size is the same on the
3036 * sending and receiving sides for the time being. For non-raw sends,
3037 * this does not matter (and in fact we do not send a DRR_OBJECT_RANGE
3038 * record at all). Raw sends require this record type because the
3039 * encryption parameters are used to protect an entire block of bonus
3040 * buffers. If the size of dnode blocks ever becomes variable,
3041 * handling will need to be added to ensure that dnode block sizes
3042 * match on the sending and receiving side.
3043 */
3044 if (drror->drr_numslots != DNODES_PER_BLOCK ||
3045 P2PHASE(drror->drr_firstobj, DNODES_PER_BLOCK) != 0 ||
9b840763 3046 !rwa->raw)
b5256303
TC
3047 return (SET_ERROR(EINVAL));
3048
48fbb9dd
FG
3049 if (drror->drr_firstobj > rwa->max_object)
3050 rwa->max_object = drror->drr_firstobj;
3051
b5256303 3052 /*
095495e0 3053 * The DRR_OBJECT_RANGE handling must be deferred to receive_object()
0c03d21a
MA
3054 * so that the block of dnodes is not written out when it's empty,
3055 * and converted to a HOLE BP.
b5256303 3056 */
0c03d21a 3057 rwa->or_crypt_params_present = B_TRUE;
095495e0
TC
3058 rwa->or_firstobj = drror->drr_firstobj;
3059 rwa->or_numslots = drror->drr_numslots;
3060 bcopy(drror->drr_salt, rwa->or_salt, ZIO_DATA_SALT_LEN);
3061 bcopy(drror->drr_iv, rwa->or_iv, ZIO_DATA_IV_LEN);
3062 bcopy(drror->drr_mac, rwa->or_mac, ZIO_DATA_MAC_LEN);
3063 rwa->or_byteorder = byteorder;
3064
b5256303
TC
3065 return (0);
3066}
3067
13fe0198
MA
3068/* used to destroy the drc_ds on error */
3069static void
3070dmu_recv_cleanup_ds(dmu_recv_cookie_t *drc)
3071{
2637dda8 3072 dsl_dataset_t *ds = drc->drc_ds;
b5256303
TC
3073 ds_hold_flags_t dsflags = (drc->drc_raw) ? 0 : DS_HOLD_FLAG_DECRYPT;
3074
3075 /*
3076 * Wait for the txg sync before cleaning up the receive. For
3077 * resumable receives, this ensures that our resume state has
3078 * been written out to disk. For raw receives, this ensures
3079 * that the user accounting code will not attempt to do anything
3080 * after we stopped receiving the dataset.
3081 */
2637dda8 3082 txg_wait_synced(ds->ds_dir->dd_pool, 0);
0c03d21a 3083 ds->ds_objset->os_raw_receive = B_FALSE;
b5256303 3084
2637dda8
TC
3085 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
3086 if (drc->drc_resumable && !BP_IS_HOLE(dsl_dataset_get_blkptr(ds))) {
3087 rrw_exit(&ds->ds_bp_rwlock, FTAG);
3088 dsl_dataset_disown(ds, dsflags, dmu_recv_tag);
47dfff3b 3089 } else {
eca7b760 3090 char name[ZFS_MAX_DATASET_NAME_LEN];
2637dda8
TC
3091 rrw_exit(&ds->ds_bp_rwlock, FTAG);
3092 dsl_dataset_name(ds, name);
3093 dsl_dataset_disown(ds, dsflags, dmu_recv_tag);
47dfff3b
MA
3094 (void) dsl_destroy_head(name);
3095 }
13fe0198
MA
3096}
3097
37f8a883 3098static void
fcff0f35 3099receive_cksum(struct receive_arg *ra, int len, void *buf)
37f8a883
MA
3100{
3101 if (ra->byteswap) {
a6255b7f 3102 (void) fletcher_4_incremental_byteswap(buf, len, &ra->cksum);
37f8a883 3103 } else {
a6255b7f 3104 (void) fletcher_4_incremental_native(buf, len, &ra->cksum);
37f8a883
MA
3105 }
3106}
3107
3108/*
fcff0f35
PD
3109 * Read the payload into a buffer of size len, and update the current record's
3110 * payload field.
3111 * Allocate ra->next_rrd and read the next record's header into
3112 * ra->next_rrd->header.
37f8a883
MA
3113 * Verify checksum of payload and next record.
3114 */
3115static int
fcff0f35 3116receive_read_payload_and_next_header(struct receive_arg *ra, int len, void *buf)
37f8a883
MA
3117{
3118 int err;
3119 zio_cksum_t cksum_orig;
3120 zio_cksum_t *cksump;
3121
3122 if (len != 0) {
fcff0f35 3123 ASSERT3U(len, <=, SPA_MAXBLOCKSIZE);
47dfff3b 3124 err = receive_read(ra, len, buf);
37f8a883
MA
3125 if (err != 0)
3126 return (err);
47dfff3b
MA
3127 receive_cksum(ra, len, buf);
3128
3129 /* note: rrd is NULL when reading the begin record's payload */
3130 if (ra->rrd != NULL) {
3131 ra->rrd->payload = buf;
3132 ra->rrd->payload_size = len;
3133 ra->rrd->bytes_read = ra->bytes_read;
3134 }
37f8a883
MA
3135 }
3136
3137 ra->prev_cksum = ra->cksum;
3138
fcff0f35
PD
3139 ra->next_rrd = kmem_zalloc(sizeof (*ra->next_rrd), KM_SLEEP);
3140 err = receive_read(ra, sizeof (ra->next_rrd->header),
3141 &ra->next_rrd->header);
47dfff3b 3142 ra->next_rrd->bytes_read = ra->bytes_read;
b5256303 3143
fcff0f35
PD
3144 if (err != 0) {
3145 kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
3146 ra->next_rrd = NULL;
37f8a883 3147 return (err);
fcff0f35
PD
3148 }
3149 if (ra->next_rrd->header.drr_type == DRR_BEGIN) {
3150 kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
3151 ra->next_rrd = NULL;
37f8a883 3152 return (SET_ERROR(EINVAL));
fcff0f35 3153 }
37f8a883
MA
3154
3155 /*
3156 * Note: checksum is of everything up to but not including the
3157 * checksum itself.
3158 */
3159 ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
3160 ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
fcff0f35 3161 receive_cksum(ra,
37f8a883 3162 offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
fcff0f35 3163 &ra->next_rrd->header);
37f8a883 3164
fcff0f35
PD
3165 cksum_orig = ra->next_rrd->header.drr_u.drr_checksum.drr_checksum;
3166 cksump = &ra->next_rrd->header.drr_u.drr_checksum.drr_checksum;
37f8a883
MA
3167
3168 if (ra->byteswap)
fcff0f35 3169 byteswap_record(&ra->next_rrd->header);
37f8a883
MA
3170
3171 if ((!ZIO_CHECKSUM_IS_ZERO(cksump)) &&
fcff0f35
PD
3172 !ZIO_CHECKSUM_EQUAL(ra->cksum, *cksump)) {
3173 kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
3174 ra->next_rrd = NULL;
37f8a883 3175 return (SET_ERROR(ECKSUM));
fcff0f35 3176 }
37f8a883 3177
fcff0f35 3178 receive_cksum(ra, sizeof (cksum_orig), &cksum_orig);
37f8a883
MA
3179
3180 return (0);
3181}
3182
e6d3a843
PD
3183static void
3184objlist_create(struct objlist *list)
3185{
3186 list_create(&list->list, sizeof (struct receive_objnode),
3187 offsetof(struct receive_objnode, node));
3188 list->last_lookup = 0;
3189}
3190
3191static void
3192objlist_destroy(struct objlist *list)
3193{
1c27024e 3194 for (struct receive_objnode *n = list_remove_head(&list->list);
e6d3a843
PD
3195 n != NULL; n = list_remove_head(&list->list)) {
3196 kmem_free(n, sizeof (*n));
3197 }
3198 list_destroy(&list->list);
3199}
3200
3201/*
3202 * This function looks through the objlist to see if the specified object number
3203 * is contained in the objlist. In the process, it will remove all object
3204 * numbers in the list that are smaller than the specified object number. Thus,
3205 * any lookup of an object number smaller than a previously looked up object
3206 * number will always return false; therefore, all lookups should be done in
3207 * ascending order.
3208 */
3209static boolean_t
3210objlist_exists(struct objlist *list, uint64_t object)
3211{
3212 struct receive_objnode *node = list_head(&list->list);
3213 ASSERT3U(object, >=, list->last_lookup);
3214 list->last_lookup = object;
3215 while (node != NULL && node->object < object) {
3216 VERIFY3P(node, ==, list_remove_head(&list->list));
3217 kmem_free(node, sizeof (*node));
3218 node = list_head(&list->list);
3219 }
3220 return (node != NULL && node->object == object);
3221}
3222
3223/*
3224 * The objlist is a list of object numbers stored in ascending order. However,
3225 * the insertion of new object numbers does not seek out the correct location to
3226 * store a new object number; instead, it appends it to the list for simplicity.
3227 * Thus, any users must take care to only insert new object numbers in ascending
3228 * order.
3229 */
3230static void
3231objlist_insert(struct objlist *list, uint64_t object)
3232{
3233 struct receive_objnode *node = kmem_zalloc(sizeof (*node), KM_SLEEP);
3234 node->object = object;
3235#ifdef ZFS_DEBUG
3236 {
3237 struct receive_objnode *last_object = list_tail(&list->list);
3238 uint64_t last_objnum = (last_object != NULL ? last_object->object : 0);
3239 ASSERT3U(node->object, >, last_objnum);
3240 }
3241#endif
3242 list_insert_tail(&list->list, node);
3243}
3244
fcff0f35
PD
3245/*
3246 * Issue the prefetch reads for any necessary indirect blocks.
3247 *
3248 * We use the object ignore list to tell us whether or not to issue prefetches
3249 * for a given object. We do this for both correctness (in case the blocksize
3250 * of an object has changed) and performance (if the object doesn't exist, don't
3251 * needlessly try to issue prefetches). We also trim the list as we go through
3252 * the stream to prevent it from growing to an unbounded size.
3253 *
3254 * The object numbers within will always be in sorted order, and any write
3255 * records we see will also be in sorted order, but they're not sorted with
3256 * respect to each other (i.e. we can get several object records before
3257 * receiving each object's write records). As a result, once we've reached a
3258 * given object number, we can safely remove any reference to lower object
3259 * numbers in the ignore list. In practice, we receive up to 32 object records
3260 * before receiving write records, so the list can have up to 32 nodes in it.
3261 */
3262/* ARGSUSED */
3263static void
3264receive_read_prefetch(struct receive_arg *ra,
3265 uint64_t object, uint64_t offset, uint64_t length)
3266{
e6d3a843 3267 if (!objlist_exists(&ra->ignore_objlist, object)) {
fcff0f35
PD
3268 dmu_prefetch(ra->os, object, 1, offset, length,
3269 ZIO_PRIORITY_SYNC_READ);
3270 }
3271}
3272
3273/*
3274 * Read records off the stream, issuing any necessary prefetches.
3275 */
37f8a883 3276static int
fcff0f35 3277receive_read_record(struct receive_arg *ra)
37f8a883
MA
3278{
3279 int err;
3280
fcff0f35 3281 switch (ra->rrd->header.drr_type) {
37f8a883
MA
3282 case DRR_OBJECT:
3283 {
fcff0f35 3284 struct drr_object *drro = &ra->rrd->header.drr_u.drr_object;
b5256303 3285 uint32_t size = DRR_OBJECT_PAYLOAD_SIZE(drro);
fcff0f35
PD
3286 void *buf = kmem_zalloc(size, KM_SLEEP);
3287 dmu_object_info_t doi;
b5256303 3288
fcff0f35
PD
3289 err = receive_read_payload_and_next_header(ra, size, buf);
3290 if (err != 0) {
3291 kmem_free(buf, size);
37f8a883 3292 return (err);
fcff0f35
PD
3293 }
3294 err = dmu_object_info(ra->os, drro->drr_object, &doi);
3295 /*
3296 * See receive_read_prefetch for an explanation why we're
3297 * storing this object in the ignore_obj_list.
3298 */
047116ac 3299 if (err == ENOENT || err == EEXIST ||
fcff0f35 3300 (err == 0 && doi.doi_data_block_size != drro->drr_blksz)) {
e6d3a843 3301 objlist_insert(&ra->ignore_objlist, drro->drr_object);
fcff0f35
PD
3302 err = 0;
3303 }
3304 return (err);
37f8a883
MA
3305 }
3306 case DRR_FREEOBJECTS:
3307 {
fcff0f35
PD
3308 err = receive_read_payload_and_next_header(ra, 0, NULL);
3309 return (err);
37f8a883
MA
3310 }
3311 case DRR_WRITE:
3312 {
fcff0f35 3313 struct drr_write *drrw = &ra->rrd->header.drr_u.drr_write;
2aa34383
DK
3314 arc_buf_t *abuf;
3315 boolean_t is_meta = DMU_OT_IS_METADATA(drrw->drr_type);
b5256303 3316
9b840763 3317 if (ra->raw) {
b5256303
TC
3318 boolean_t byteorder = ZFS_HOST_BYTEORDER ^
3319 !!DRR_IS_RAW_BYTESWAPPED(drrw->drr_flags) ^
3320 ra->byteswap;
3321
3322 abuf = arc_loan_raw_buf(dmu_objset_spa(ra->os),
3323 drrw->drr_object, byteorder, drrw->drr_salt,
3324 drrw->drr_iv, drrw->drr_mac, drrw->drr_type,
3325 drrw->drr_compressed_size, drrw->drr_logical_size,
3326 drrw->drr_compressiontype);
3327 } else if (DRR_WRITE_COMPRESSED(drrw)) {
2aa34383
DK
3328 ASSERT3U(drrw->drr_compressed_size, >, 0);
3329 ASSERT3U(drrw->drr_logical_size, >=,
3330 drrw->drr_compressed_size);
3331 ASSERT(!is_meta);
3332 abuf = arc_loan_compressed_buf(
3333 dmu_objset_spa(ra->os),
3334 drrw->drr_compressed_size, drrw->drr_logical_size,
3335 drrw->drr_compressiontype);
3336 } else {
3337 abuf = arc_loan_buf(dmu_objset_spa(ra->os),
3338 is_meta, drrw->drr_logical_size);
3339 }
37f8a883 3340
fcff0f35 3341 err = receive_read_payload_and_next_header(ra,
2aa34383 3342 DRR_WRITE_PAYLOAD_SIZE(drrw), abuf->b_data);
fcff0f35 3343 if (err != 0) {
37f8a883 3344 dmu_return_arcbuf(abuf);
fcff0f35
PD
3345 return (err);
3346 }
b5256303 3347 ra->rrd->arc_buf = abuf;
fcff0f35 3348 receive_read_prefetch(ra, drrw->drr_object, drrw->drr_offset,
2aa34383 3349 drrw->drr_logical_size);
37f8a883
MA
3350 return (err);
3351 }
3352 case DRR_WRITE_BYREF:
3353 {
fcff0f35
PD
3354 struct drr_write_byref *drrwb =
3355 &ra->rrd->header.drr_u.drr_write_byref;
3356 err = receive_read_payload_and_next_header(ra, 0, NULL);
3357 receive_read_prefetch(ra, drrwb->drr_object, drrwb->drr_offset,
3358 drrwb->drr_length);
3359 return (err);
37f8a883
MA
3360 }
3361 case DRR_WRITE_EMBEDDED:
3362 {
3363 struct drr_write_embedded *drrwe =
fcff0f35
PD
3364 &ra->rrd->header.drr_u.drr_write_embedded;
3365 uint32_t size = P2ROUNDUP(drrwe->drr_psize, 8);
3366 void *buf = kmem_zalloc(size, KM_SLEEP);
3367
3368 err = receive_read_payload_and_next_header(ra, size, buf);
3369 if (err != 0) {
3370 kmem_free(buf, size);
37f8a883 3371 return (err);
fcff0f35
PD
3372 }
3373
3374 receive_read_prefetch(ra, drrwe->drr_object, drrwe->drr_offset,
3375 drrwe->drr_length);
3376 return (err);
37f8a883
MA
3377 }
3378 case DRR_FREE:
3379 {
fcff0f35
PD
3380 /*
3381 * It might be beneficial to prefetch indirect blocks here, but
3382 * we don't really have the data to decide for sure.
3383 */
3384 err = receive_read_payload_and_next_header(ra, 0, NULL);
3385 return (err);
37f8a883
MA
3386 }
3387 case DRR_END:
3388 {
fcff0f35 3389 struct drr_end *drre = &ra->rrd->header.drr_u.drr_end;
37f8a883 3390 if (!ZIO_CHECKSUM_EQUAL(ra->prev_cksum, drre->drr_checksum))
47dfff3b 3391 return (SET_ERROR(ECKSUM));
37f8a883
MA
3392 return (0);
3393 }
3394 case DRR_SPILL:
3395 {
fcff0f35 3396 struct drr_spill *drrs = &ra->rrd->header.drr_u.drr_spill;
b5256303
TC
3397 arc_buf_t *abuf;
3398 int len = DRR_SPILL_PAYLOAD_SIZE(drrs);
3399
3400 /* DRR_SPILL records are either raw or uncompressed */
9b840763 3401 if (ra->raw) {
b5256303
TC
3402 boolean_t byteorder = ZFS_HOST_BYTEORDER ^
3403 !!DRR_IS_RAW_BYTESWAPPED(drrs->drr_flags) ^
3404 ra->byteswap;
3405
3406 abuf = arc_loan_raw_buf(dmu_objset_spa(ra->os),
b0ee5946 3407 dmu_objset_id(ra->os), byteorder, drrs->drr_salt,
b5256303
TC
3408 drrs->drr_iv, drrs->drr_mac, drrs->drr_type,
3409 drrs->drr_compressed_size, drrs->drr_length,
3410 drrs->drr_compressiontype);
3411 } else {
3412 abuf = arc_loan_buf(dmu_objset_spa(ra->os),
3413 DMU_OT_IS_METADATA(drrs->drr_type),
3414 drrs->drr_length);
3415 }
3416
3417 err = receive_read_payload_and_next_header(ra, len,
3418 abuf->b_data);
3419 if (err != 0) {
3420 dmu_return_arcbuf(abuf);
3421 return (err);
3422 }
3423 ra->rrd->arc_buf = abuf;
3424 return (err);
3425 }
3426 case DRR_OBJECT_RANGE:
3427 {
3428 err = receive_read_payload_and_next_header(ra, 0, NULL);
fcff0f35
PD
3429 return (err);
3430 }
3431 default:
3432 return (SET_ERROR(EINVAL));
3433 }
3434}
3435
6a8ee4f7
NB
3436static void
3437dprintf_drr(struct receive_record_arg *rrd, int err)
3438{
2fd92c3d 3439#ifdef ZFS_DEBUG
6a8ee4f7
NB
3440 switch (rrd->header.drr_type) {
3441 case DRR_OBJECT:
3442 {
3443 struct drr_object *drro = &rrd->header.drr_u.drr_object;
3444 dprintf("drr_type = OBJECT obj = %llu type = %u "
3445 "bonustype = %u blksz = %u bonuslen = %u cksumtype = %u "
3446 "compress = %u dn_slots = %u err = %d\n",
3447 drro->drr_object, drro->drr_type, drro->drr_bonustype,
3448 drro->drr_blksz, drro->drr_bonuslen,
3449 drro->drr_checksumtype, drro->drr_compress,
3450 drro->drr_dn_slots, err);
3451 break;
3452 }
3453 case DRR_FREEOBJECTS:
3454 {
3455 struct drr_freeobjects *drrfo =
3456 &rrd->header.drr_u.drr_freeobjects;
3457 dprintf("drr_type = FREEOBJECTS firstobj = %llu "
3458 "numobjs = %llu err = %d\n",
3459 drrfo->drr_firstobj, drrfo->drr_numobjs, err);
3460 break;
3461 }
3462 case DRR_WRITE:
3463 {
3464 struct drr_write *drrw = &rrd->header.drr_u.drr_write;
3465 dprintf("drr_type = WRITE obj = %llu type = %u offset = %llu "
3466 "lsize = %llu cksumtype = %u cksumflags = %u "
3467 "compress = %u psize = %llu err = %d\n",
3468 drrw->drr_object, drrw->drr_type, drrw->drr_offset,
3469 drrw->drr_logical_size, drrw->drr_checksumtype,
b5256303 3470 drrw->drr_flags, drrw->drr_compressiontype,
6a8ee4f7
NB
3471 drrw->drr_compressed_size, err);
3472 break;
3473 }
3474 case DRR_WRITE_BYREF:
3475 {
3476 struct drr_write_byref *drrwbr =
3477 &rrd->header.drr_u.drr_write_byref;
3478 dprintf("drr_type = WRITE_BYREF obj = %llu offset = %llu "
3479 "length = %llu toguid = %llx refguid = %llx "
3480 "refobject = %llu refoffset = %llu cksumtype = %u "
3481 "cksumflags = %u err = %d\n",
3482 drrwbr->drr_object, drrwbr->drr_offset,
3483 drrwbr->drr_length, drrwbr->drr_toguid,
3484 drrwbr->drr_refguid, drrwbr->drr_refobject,
3485 drrwbr->drr_refoffset, drrwbr->drr_checksumtype,
b5256303 3486 drrwbr->drr_flags, err);
6a8ee4f7
NB
3487 break;
3488 }
3489 case DRR_WRITE_EMBEDDED:
3490 {
3491 struct drr_write_embedded *drrwe =
3492 &rrd->header.drr_u.drr_write_embedded;
3493 dprintf("drr_type = WRITE_EMBEDDED obj = %llu offset = %llu "
3494 "length = %llu compress = %u etype = %u lsize = %u "
3495 "psize = %u err = %d\n",
3496 drrwe->drr_object, drrwe->drr_offset, drrwe->drr_length,
3497 drrwe->drr_compression, drrwe->drr_etype,
3498 drrwe->drr_lsize, drrwe->drr_psize, err);
3499 break;
3500 }
3501 case DRR_FREE:
3502 {
3503 struct drr_free *drrf = &rrd->header.drr_u.drr_free;
3504 dprintf("drr_type = FREE obj = %llu offset = %llu "
3505 "length = %lld err = %d\n",
3506 drrf->drr_object, drrf->drr_offset, drrf->drr_length,
3507 err);
3508 break;
3509 }
3510 case DRR_SPILL:
3511 {
3512 struct drr_spill *drrs = &rrd->header.drr_u.drr_spill;
3513 dprintf("drr_type = SPILL obj = %llu length = %llu "
3514 "err = %d\n", drrs->drr_object, drrs->drr_length, err);
3515 break;
3516 }
3517 default:
3518 return;
3519 }
2fd92c3d 3520#endif
6a8ee4f7
NB
3521}
3522
fcff0f35
PD
3523/*
3524 * Commit the records to the pool.
3525 */
3526static int
3527receive_process_record(struct receive_writer_arg *rwa,
3528 struct receive_record_arg *rrd)
3529{
3530 int err;
3531
47dfff3b
MA
3532 /* Processing in order, therefore bytes_read should be increasing. */
3533 ASSERT3U(rrd->bytes_read, >=, rwa->bytes_read);
3534 rwa->bytes_read = rrd->bytes_read;
3535
fcff0f35
PD
3536 switch (rrd->header.drr_type) {
3537 case DRR_OBJECT:
3538 {
3539 struct drr_object *drro = &rrd->header.drr_u.drr_object;
3540 err = receive_object(rwa, drro, rrd->payload);
3541 kmem_free(rrd->payload, rrd->payload_size);
3542 rrd->payload = NULL;
6a8ee4f7 3543 break;
fcff0f35
PD
3544 }
3545 case DRR_FREEOBJECTS:
3546 {
3547 struct drr_freeobjects *drrfo =
3548 &rrd->header.drr_u.drr_freeobjects;
6a8ee4f7
NB
3549 err = receive_freeobjects(rwa, drrfo);
3550 break;
fcff0f35
PD
3551 }
3552 case DRR_WRITE:
3553 {
3554 struct drr_write *drrw = &rrd->header.drr_u.drr_write;
b5256303 3555 err = receive_write(rwa, drrw, rrd->arc_buf);
fcff0f35
PD
3556 /* if receive_write() is successful, it consumes the arc_buf */
3557 if (err != 0)
b5256303
TC
3558 dmu_return_arcbuf(rrd->arc_buf);
3559 rrd->arc_buf = NULL;
fcff0f35 3560 rrd->payload = NULL;
6a8ee4f7 3561 break;
fcff0f35
PD
3562 }
3563 case DRR_WRITE_BYREF:
3564 {
3565 struct drr_write_byref *drrwbr =
3566 &rrd->header.drr_u.drr_write_byref;
6a8ee4f7
NB
3567 err = receive_write_byref(rwa, drrwbr);
3568 break;
fcff0f35
PD
3569 }
3570 case DRR_WRITE_EMBEDDED:
3571 {
3572 struct drr_write_embedded *drrwe =
3573 &rrd->header.drr_u.drr_write_embedded;
3574 err = receive_write_embedded(rwa, drrwe, rrd->payload);
3575 kmem_free(rrd->payload, rrd->payload_size);
3576 rrd->payload = NULL;
6a8ee4f7 3577 break;
fcff0f35
PD
3578 }
3579 case DRR_FREE:
3580 {
3581 struct drr_free *drrf = &rrd->header.drr_u.drr_free;
6a8ee4f7
NB
3582 err = receive_free(rwa, drrf);
3583 break;
fcff0f35
PD
3584 }
3585 case DRR_SPILL:
3586 {
3587 struct drr_spill *drrs = &rrd->header.drr_u.drr_spill;
b5256303
TC
3588 err = receive_spill(rwa, drrs, rrd->arc_buf);
3589 /* if receive_spill() is successful, it consumes the arc_buf */
3590 if (err != 0)
3591 dmu_return_arcbuf(rrd->arc_buf);
3592 rrd->arc_buf = NULL;
fcff0f35 3593 rrd->payload = NULL;
6a8ee4f7 3594 break;
37f8a883 3595 }
b5256303
TC
3596 case DRR_OBJECT_RANGE:
3597 {
3598 struct drr_object_range *drror =
3599 &rrd->header.drr_u.drr_object_range;
3600 return (receive_object_range(rwa, drror));
3601 }
37f8a883
MA
3602 default:
3603 return (SET_ERROR(EINVAL));
3604 }
6a8ee4f7
NB
3605
3606 if (err != 0)
3607 dprintf_drr(rrd, err);
3608
3609 return (err);
37f8a883
MA
3610}
3611
34dc7c2f 3612/*
fcff0f35
PD
3613 * dmu_recv_stream's worker thread; pull records off the queue, and then call
3614 * receive_process_record When we're done, signal the main thread and exit.
3615 */
3616static void
3617receive_writer_thread(void *arg)
3618{
3619 struct receive_writer_arg *rwa = arg;
3620 struct receive_record_arg *rrd;
3e635ac1
TC
3621 fstrans_cookie_t cookie = spl_fstrans_mark();
3622
fcff0f35
PD
3623 for (rrd = bqueue_dequeue(&rwa->q); !rrd->eos_marker;
3624 rrd = bqueue_dequeue(&rwa->q)) {
3625 /*
3626 * If there's an error, the main thread will stop putting things
3627 * on the queue, but we need to clear everything in it before we
3628 * can exit.
3629 */
3630 if (rwa->err == 0) {
3631 rwa->err = receive_process_record(rwa, rrd);
b5256303
TC
3632 } else if (rrd->arc_buf != NULL) {
3633 dmu_return_arcbuf(rrd->arc_buf);
3634 rrd->arc_buf = NULL;
fcff0f35
PD
3635 rrd->payload = NULL;
3636 } else if (rrd->payload != NULL) {
3637 kmem_free(rrd->payload, rrd->payload_size);
3638 rrd->payload = NULL;
3639 }
3640 kmem_free(rrd, sizeof (*rrd));
3641 }
3642 kmem_free(rrd, sizeof (*rrd));
3643 mutex_enter(&rwa->mutex);
3644 rwa->done = B_TRUE;
3645 cv_signal(&rwa->cv);
3646 mutex_exit(&rwa->mutex);
3e635ac1 3647 spl_fstrans_unmark(cookie);
34a6b428 3648 thread_exit();
fcff0f35
PD
3649}
3650
47dfff3b
MA
3651static int
3652resume_check(struct receive_arg *ra, nvlist_t *begin_nvl)
3653{
3654 uint64_t val;
3655 objset_t *mos = dmu_objset_pool(ra->os)->dp_meta_objset;
3656 uint64_t dsobj = dmu_objset_id(ra->os);
3657 uint64_t resume_obj, resume_off;
3658
3659 if (nvlist_lookup_uint64(begin_nvl,
3660 "resume_object", &resume_obj) != 0 ||
3661 nvlist_lookup_uint64(begin_nvl,
3662 "resume_offset", &resume_off) != 0) {
3663 return (SET_ERROR(EINVAL));
3664 }
3665 VERIFY0(zap_lookup(mos, dsobj,
3666 DS_FIELD_RESUME_OBJECT, sizeof (val), 1, &val));
3667 if (resume_obj != val)
3668 return (SET_ERROR(EINVAL));
3669 VERIFY0(zap_lookup(mos, dsobj,
3670 DS_FIELD_RESUME_OFFSET, sizeof (val), 1, &val));
3671 if (resume_off != val)
3672 return (SET_ERROR(EINVAL));
3673
3674 return (0);
3675}
3676
fcff0f35
PD
3677/*
3678 * Read in the stream's records, one by one, and apply them to the pool. There
3679 * are two threads involved; the thread that calls this function will spin up a
3680 * worker thread, read the records off the stream one by one, and issue
3681 * prefetches for any necessary indirect blocks. It will then push the records
3682 * onto an internal blocking queue. The worker thread will pull the records off
3683 * the queue, and actually write the data into the DMU. This way, the worker
3684 * thread doesn't have to wait for reads to complete, since everything it needs
3685 * (the indirect blocks) will be prefetched.
3686 *
34dc7c2f
BB
3687 * NB: callers *must* call dmu_recv_end() if this succeeds.
3688 */
3689int
572e2857
BB
3690dmu_recv_stream(dmu_recv_cookie_t *drc, vnode_t *vp, offset_t *voffp,
3691 int cleanup_fd, uint64_t *action_handlep)
34dc7c2f 3692{
37f8a883 3693 int err = 0;
04bc4610
NB
3694 struct receive_arg *ra;
3695 struct receive_writer_arg *rwa;
428870ff 3696 int featureflags;
47dfff3b
MA
3697 uint32_t payloadlen;
3698 void *payload;
3699 nvlist_t *begin_nvl = NULL;
34dc7c2f 3700
04bc4610
NB
3701 ra = kmem_zalloc(sizeof (*ra), KM_SLEEP);
3702 rwa = kmem_zalloc(sizeof (*rwa), KM_SLEEP);
3703
3704 ra->byteswap = drc->drc_byteswap;
9b840763 3705 ra->raw = drc->drc_raw;
04bc4610
NB
3706 ra->cksum = drc->drc_cksum;
3707 ra->vp = vp;
3708 ra->voff = *voffp;
47dfff3b
MA
3709
3710 if (dsl_dataset_is_zapified(drc->drc_ds)) {
3711 (void) zap_lookup(drc->drc_ds->ds_dir->dd_pool->dp_meta_objset,
3712 drc->drc_ds->ds_object, DS_FIELD_RESUME_BYTES,
3713 sizeof (ra->bytes_read), 1, &ra->bytes_read);
3714 }
3715
e6d3a843 3716 objlist_create(&ra->ignore_objlist);
34dc7c2f
BB
3717
3718 /* these were verified in dmu_recv_begin */
13fe0198 3719 ASSERT3U(DMU_GET_STREAM_HDRTYPE(drc->drc_drrb->drr_versioninfo), ==,
428870ff 3720 DMU_SUBSTREAM);
13fe0198 3721 ASSERT3U(drc->drc_drrb->drr_type, <, DMU_OST_NUMTYPES);
34dc7c2f
BB
3722
3723 /*
3724 * Open the objset we are modifying.
3725 */
04bc4610 3726 VERIFY0(dmu_objset_from_ds(drc->drc_ds, &ra->os));
34dc7c2f 3727
d683ddbb 3728 ASSERT(dsl_dataset_phys(drc->drc_ds)->ds_flags & DS_FLAG_INCONSISTENT);
34dc7c2f 3729
428870ff 3730 featureflags = DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo);
b5256303 3731 ra->featureflags = featureflags;
428870ff 3732
9b840763
TC
3733 /* embedded data is incompatible with encrypted datasets */
3734 if (ra->os->os_encrypted &&
3735 (featureflags & DMU_BACKUP_FEATURE_EMBED_DATA)) {
3736 err = SET_ERROR(EINVAL);
3737 goto out;
3738 }
3739
428870ff
BB
3740 /* if this stream is dedup'ed, set up the avl tree for guid mapping */
3741 if (featureflags & DMU_BACKUP_FEATURE_DEDUP) {
572e2857
BB
3742 minor_t minor;
3743
3744 if (cleanup_fd == -1) {
9b840763 3745 err = SET_ERROR(EBADF);
572e2857
BB
3746 goto out;
3747 }
9b840763
TC
3748 err = zfs_onexit_fd_hold(cleanup_fd, &minor);
3749 if (err != 0) {
572e2857
BB
3750 cleanup_fd = -1;
3751 goto out;
3752 }
3753
3754 if (*action_handlep == 0) {
04bc4610 3755 rwa->guid_to_ds_map =
572e2857 3756 kmem_alloc(sizeof (avl_tree_t), KM_SLEEP);
04bc4610 3757 avl_create(rwa->guid_to_ds_map, guid_compare,
572e2857
BB
3758 sizeof (guid_map_entry_t),
3759 offsetof(guid_map_entry_t, avlnode));
37f8a883 3760 err = zfs_onexit_add_cb(minor,
04bc4610 3761 free_guid_map_onexit, rwa->guid_to_ds_map,
572e2857 3762 action_handlep);
9b840763 3763 if (err != 0)
572e2857
BB
3764 goto out;
3765 } else {
37f8a883 3766 err = zfs_onexit_cb_data(minor, *action_handlep,
04bc4610 3767 (void **)&rwa->guid_to_ds_map);
9b840763 3768 if (err != 0)
572e2857
BB
3769 goto out;
3770 }
8d35c149 3771
04bc4610 3772 drc->drc_guid_to_ds_map = rwa->guid_to_ds_map;
428870ff
BB
3773 }
3774
47dfff3b
MA
3775 payloadlen = drc->drc_drr_begin->drr_payloadlen;
3776 payload = NULL;
3777 if (payloadlen != 0)
3778 payload = kmem_alloc(payloadlen, KM_SLEEP);
3779
3780 err = receive_read_payload_and_next_header(ra, payloadlen, payload);
3781 if (err != 0) {
3782 if (payloadlen != 0)
3783 kmem_free(payload, payloadlen);
37f8a883 3784 goto out;
47dfff3b
MA
3785 }
3786 if (payloadlen != 0) {
3787 err = nvlist_unpack(payload, payloadlen, &begin_nvl, KM_SLEEP);
3788 kmem_free(payload, payloadlen);
3789 if (err != 0)
3790 goto out;
3791 }
3792
b5256303
TC
3793 /* handle DSL encryption key payload */
3794 if (featureflags & DMU_BACKUP_FEATURE_RAW) {
3795 nvlist_t *keynvl = NULL;
3796
3797 ASSERT(ra->os->os_encrypted);
3798 ASSERT(drc->drc_raw);
3799
3800 err = nvlist_lookup_nvlist(begin_nvl, "crypt_keydata", &keynvl);
3801 if (err != 0)
3802 goto out;
3803
b0918402
TC
3804 /*
3805 * If this is a new dataset we set the key immediately.
3806 * Otherwise we don't want to change the key until we
3807 * are sure the rest of the receive succeeded so we stash
3808 * the keynvl away until then.
3809 */
3810 err = dsl_crypto_recv_raw(spa_name(ra->os->os_spa),
b5256303 3811 drc->drc_ds->ds_object, drc->drc_drrb->drr_type,
b0918402 3812 keynvl, drc->drc_newfs);
b5256303
TC
3813 if (err != 0)
3814 goto out;
b0918402
TC
3815
3816 if (!drc->drc_newfs)
3817 drc->drc_keynvl = fnvlist_dup(keynvl);
b5256303
TC
3818 }
3819
47dfff3b
MA
3820 if (featureflags & DMU_BACKUP_FEATURE_RESUMING) {
3821 err = resume_check(ra, begin_nvl);
3822 if (err != 0)
3823 goto out;
3824 }
37f8a883 3825
3b0d9928
BB
3826 (void) bqueue_init(&rwa->q,
3827 MAX(zfs_recv_queue_length, 2 * zfs_max_recordsize),
fcff0f35 3828 offsetof(struct receive_record_arg, node));
04bc4610
NB
3829 cv_init(&rwa->cv, NULL, CV_DEFAULT, NULL);
3830 mutex_init(&rwa->mutex, NULL, MUTEX_DEFAULT, NULL);
3831 rwa->os = ra->os;
3832 rwa->byteswap = drc->drc_byteswap;
47dfff3b 3833 rwa->resumable = drc->drc_resumable;
9b840763 3834 rwa->raw = drc->drc_raw;
0c03d21a 3835 rwa->os->os_raw_receive = drc->drc_raw;
fcff0f35 3836
04bc4610 3837 (void) thread_create(NULL, 0, receive_writer_thread, rwa, 0, curproc,
fcff0f35
PD
3838 TS_RUN, minclsyspri);
3839 /*
04bc4610 3840 * We're reading rwa->err without locks, which is safe since we are the
fcff0f35
PD
3841 * only reader, and the worker thread is the only writer. It's ok if we
3842 * miss a write for an iteration or two of the loop, since the writer
3843 * thread will keep freeing records we send it until we send it an eos
3844 * marker.
3845 *
04bc4610 3846 * We can leave this loop in 3 ways: First, if rwa->err is
fcff0f35
PD
3847 * non-zero. In that case, the writer thread will free the rrd we just
3848 * pushed. Second, if we're interrupted; in that case, either it's the
47dfff3b 3849 * first loop and ra->rrd was never allocated, or it's later and ra->rrd
fcff0f35
PD
3850 * has been handed off to the writer thread who will free it. Finally,
3851 * if receive_read_record fails or we're at the end of the stream, then
04bc4610 3852 * we free ra->rrd and exit.
fcff0f35 3853 */
04bc4610 3854 while (rwa->err == 0) {
34dc7c2f 3855 if (issig(JUSTLOOKING) && issig(FORREAL)) {
37f8a883
MA
3856 err = SET_ERROR(EINTR);
3857 break;
34dc7c2f
BB
3858 }
3859
04bc4610
NB
3860 ASSERT3P(ra->rrd, ==, NULL);
3861 ra->rrd = ra->next_rrd;
3862 ra->next_rrd = NULL;
3863 /* Allocates and loads header into ra->next_rrd */
3864 err = receive_read_record(ra);
34dc7c2f 3865
04bc4610
NB
3866 if (ra->rrd->header.drr_type == DRR_END || err != 0) {
3867 kmem_free(ra->rrd, sizeof (*ra->rrd));
3868 ra->rrd = NULL;
428870ff 3869 break;
fcff0f35
PD
3870 }
3871
04bc4610
NB
3872 bqueue_enqueue(&rwa->q, ra->rrd,
3873 sizeof (struct receive_record_arg) + ra->rrd->payload_size);
3874 ra->rrd = NULL;
fcff0f35 3875 }
04bc4610
NB
3876 if (ra->next_rrd == NULL)
3877 ra->next_rrd = kmem_zalloc(sizeof (*ra->next_rrd), KM_SLEEP);
3878 ra->next_rrd->eos_marker = B_TRUE;
3879 bqueue_enqueue(&rwa->q, ra->next_rrd, 1);
fcff0f35 3880
04bc4610
NB
3881 mutex_enter(&rwa->mutex);
3882 while (!rwa->done) {
3883 cv_wait(&rwa->cv, &rwa->mutex);
34dc7c2f 3884 }
04bc4610 3885 mutex_exit(&rwa->mutex);
fcff0f35 3886
48fbb9dd
FG
3887 /*
3888 * If we are receiving a full stream as a clone, all object IDs which
3889 * are greater than the maximum ID referenced in the stream are
3890 * by definition unused and must be freed.
3891 */
3892 if (drc->drc_clone && drc->drc_drrb->drr_fromguid == 0) {
3893 uint64_t obj = rwa->max_object + 1;
3894 int free_err = 0;
3895 int next_err = 0;
3896
3897 while (next_err == 0) {
0c03d21a 3898 free_err = dmu_free_long_object(rwa->os, obj);
48fbb9dd
FG
3899 if (free_err != 0 && free_err != ENOENT)
3900 break;
3901
3902 next_err = dmu_object_next(rwa->os, &obj, FALSE, 0);
3903 }
3904
3905 if (err == 0) {
3906 if (free_err != 0 && free_err != ENOENT)
3907 err = free_err;
3908 else if (next_err != ESRCH)
3909 err = next_err;
3910 }
3911 }
3912
04bc4610
NB
3913 cv_destroy(&rwa->cv);
3914 mutex_destroy(&rwa->mutex);
3915 bqueue_destroy(&rwa->q);
fcff0f35 3916 if (err == 0)
04bc4610 3917 err = rwa->err;
34dc7c2f
BB
3918
3919out:
47dfff3b 3920 nvlist_free(begin_nvl);
572e2857
BB
3921 if ((featureflags & DMU_BACKUP_FEATURE_DEDUP) && (cleanup_fd != -1))
3922 zfs_onexit_fd_rele(cleanup_fd);
3923
37f8a883 3924 if (err != 0) {
34dc7c2f 3925 /*
47dfff3b
MA
3926 * Clean up references. If receive is not resumable,
3927 * destroy what we created, so we don't leave it in
3928 * the inconsistent state.
34dc7c2f 3929 */
13fe0198 3930 dmu_recv_cleanup_ds(drc);
b0918402 3931 nvlist_free(drc->drc_keynvl);
34dc7c2f
BB
3932 }
3933
04bc4610 3934 *voffp = ra->voff;
e6d3a843 3935 objlist_destroy(&ra->ignore_objlist);
04bc4610
NB
3936 kmem_free(ra, sizeof (*ra));
3937 kmem_free(rwa, sizeof (*rwa));
37f8a883 3938 return (err);
34dc7c2f
BB
3939}
3940
34dc7c2f 3941static int
13fe0198 3942dmu_recv_end_check(void *arg, dmu_tx_t *tx)
34dc7c2f 3943{
13fe0198
MA
3944 dmu_recv_cookie_t *drc = arg;
3945 dsl_pool_t *dp = dmu_tx_pool(tx);
3946 int error;
3947
3948 ASSERT3P(drc->drc_ds->ds_owner, ==, dmu_recv_tag);
3949
3950 if (!drc->drc_newfs) {
3951 dsl_dataset_t *origin_head;
3952
3953 error = dsl_dataset_hold(dp, drc->drc_tofs, FTAG, &origin_head);
3954 if (error != 0)
3955 return (error);
19580676
MA
3956 if (drc->drc_force) {
3957 /*
3958 * We will destroy any snapshots in tofs (i.e. before
3959 * origin_head) that are after the origin (which is
3960 * the snap before drc_ds, because drc_ds can not
3961 * have any snaps of its own).
3962 */
d683ddbb
JG
3963 uint64_t obj;
3964
3965 obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
3966 while (obj !=
3967 dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
19580676
MA
3968 dsl_dataset_t *snap;
3969 error = dsl_dataset_hold_obj(dp, obj, FTAG,
3970 &snap);
3971 if (error != 0)
b6640117 3972 break;
19580676
MA
3973 if (snap->ds_dir != origin_head->ds_dir)
3974 error = SET_ERROR(EINVAL);
3975 if (error == 0) {
3976 error = dsl_destroy_snapshot_check_impl(
3977 snap, B_FALSE);
3978 }
d683ddbb 3979 obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
19580676
MA
3980 dsl_dataset_rele(snap, FTAG);
3981 if (error != 0)
b6640117
AG
3982 break;
3983 }
3984 if (error != 0) {
3985 dsl_dataset_rele(origin_head, FTAG);
3986 return (error);
19580676
MA
3987 }
3988 }
b0918402
TC
3989 if (drc->drc_keynvl != NULL) {
3990 error = dsl_crypto_recv_raw_key_check(drc->drc_ds,
3991 drc->drc_keynvl, tx);
3992 if (error != 0) {
3993 dsl_dataset_rele(origin_head, FTAG);
3994 return (error);
3995 }
3996 }
3997
13fe0198 3998 error = dsl_dataset_clone_swap_check_impl(drc->drc_ds,
831baf06 3999 origin_head, drc->drc_force, drc->drc_owner, tx);
13fe0198
MA
4000 if (error != 0) {
4001 dsl_dataset_rele(origin_head, FTAG);
4002 return (error);
4003 }
4004 error = dsl_dataset_snapshot_check_impl(origin_head,
788eb90c 4005 drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred);
13fe0198
MA
4006 dsl_dataset_rele(origin_head, FTAG);
4007 if (error != 0)
4008 return (error);
34dc7c2f 4009
13fe0198
MA
4010 error = dsl_destroy_head_check_impl(drc->drc_ds, 1);
4011 } else {
4012 error = dsl_dataset_snapshot_check_impl(drc->drc_ds,
788eb90c 4013 drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred);
13fe0198
MA
4014 }
4015 return (error);
34dc7c2f
BB
4016}
4017
4018static void
13fe0198 4019dmu_recv_end_sync(void *arg, dmu_tx_t *tx)
34dc7c2f 4020{
13fe0198
MA
4021 dmu_recv_cookie_t *drc = arg;
4022 dsl_pool_t *dp = dmu_tx_pool(tx);
b5256303 4023 boolean_t encrypted = drc->drc_ds->ds_dir->dd_crypto_obj != 0;
13fe0198
MA
4024
4025 spa_history_log_internal_ds(drc->drc_ds, "finish receiving",
4026 tx, "snap=%s", drc->drc_tosnap);
0c03d21a 4027 drc->drc_ds->ds_objset->os_raw_receive = B_FALSE;
13fe0198
MA
4028
4029 if (!drc->drc_newfs) {
4030 dsl_dataset_t *origin_head;
4031
4032 VERIFY0(dsl_dataset_hold(dp, drc->drc_tofs, FTAG,
4033 &origin_head));
19580676
MA
4034
4035 if (drc->drc_force) {
4036 /*
4037 * Destroy any snapshots of drc_tofs (origin_head)
4038 * after the origin (the snap before drc_ds).
4039 */
d683ddbb
JG
4040 uint64_t obj;
4041
4042 obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
4043 while (obj !=
4044 dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
19580676
MA
4045 dsl_dataset_t *snap;
4046 VERIFY0(dsl_dataset_hold_obj(dp, obj, FTAG,
4047 &snap));
4048 ASSERT3P(snap->ds_dir, ==, origin_head->ds_dir);
d683ddbb 4049 obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
19580676
MA
4050 dsl_destroy_snapshot_sync_impl(snap,
4051 B_FALSE, tx);
4052 dsl_dataset_rele(snap, FTAG);
4053 }
4054 }
b0918402
TC
4055 if (drc->drc_keynvl != NULL) {
4056 dsl_crypto_recv_raw_key_sync(drc->drc_ds,
4057 drc->drc_keynvl, tx);
4058 nvlist_free(drc->drc_keynvl);
4059 drc->drc_keynvl = NULL;
4060 }
4061
4062 VERIFY3P(drc->drc_ds->ds_prev, ==, origin_head->ds_prev);
19580676 4063
13fe0198
MA
4064 dsl_dataset_clone_swap_sync_impl(drc->drc_ds,
4065 origin_head, tx);
4066 dsl_dataset_snapshot_sync_impl(origin_head,
4067 drc->drc_tosnap, tx);
4068
4069 /* set snapshot's creation time and guid */
4070 dmu_buf_will_dirty(origin_head->ds_prev->ds_dbuf, tx);
d683ddbb 4071 dsl_dataset_phys(origin_head->ds_prev)->ds_creation_time =
13fe0198 4072 drc->drc_drrb->drr_creation_time;
d683ddbb 4073 dsl_dataset_phys(origin_head->ds_prev)->ds_guid =
13fe0198 4074 drc->drc_drrb->drr_toguid;
d683ddbb 4075 dsl_dataset_phys(origin_head->ds_prev)->ds_flags &=
13fe0198
MA
4076 ~DS_FLAG_INCONSISTENT;
4077
4078 dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
d683ddbb
JG
4079 dsl_dataset_phys(origin_head)->ds_flags &=
4080 ~DS_FLAG_INCONSISTENT;
13fe0198 4081
2e0e443a
GM
4082 drc->drc_newsnapobj =
4083 dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
4084
13fe0198
MA
4085 dsl_dataset_rele(origin_head, FTAG);
4086 dsl_destroy_head_sync_impl(drc->drc_ds, tx);
831baf06
KW
4087
4088 if (drc->drc_owner != NULL)
4089 VERIFY3P(origin_head->ds_owner, ==, drc->drc_owner);
13fe0198
MA
4090 } else {
4091 dsl_dataset_t *ds = drc->drc_ds;
34dc7c2f 4092
13fe0198 4093 dsl_dataset_snapshot_sync_impl(ds, drc->drc_tosnap, tx);
34dc7c2f 4094
13fe0198
MA
4095 /* set snapshot's creation time and guid */
4096 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
d683ddbb 4097 dsl_dataset_phys(ds->ds_prev)->ds_creation_time =
13fe0198 4098 drc->drc_drrb->drr_creation_time;
d683ddbb
JG
4099 dsl_dataset_phys(ds->ds_prev)->ds_guid =
4100 drc->drc_drrb->drr_toguid;
4101 dsl_dataset_phys(ds->ds_prev)->ds_flags &=
4102 ~DS_FLAG_INCONSISTENT;
34dc7c2f 4103
13fe0198 4104 dmu_buf_will_dirty(ds->ds_dbuf, tx);
d683ddbb 4105 dsl_dataset_phys(ds)->ds_flags &= ~DS_FLAG_INCONSISTENT;
47dfff3b
MA
4106 if (dsl_dataset_has_resume_receive_state(ds)) {
4107 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
4108 DS_FIELD_RESUME_FROMGUID, tx);
4109 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
4110 DS_FIELD_RESUME_OBJECT, tx);
4111 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
4112 DS_FIELD_RESUME_OFFSET, tx);
4113 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
4114 DS_FIELD_RESUME_BYTES, tx);
4115 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
4116 DS_FIELD_RESUME_TOGUID, tx);
4117 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
4118 DS_FIELD_RESUME_TONAME, tx);
4119 }
2e0e443a
GM
4120 drc->drc_newsnapobj =
4121 dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj;
13fe0198 4122 }
a0bd735a 4123 zvol_create_minors(dp->dp_spa, drc->drc_tofs, B_TRUE);
b5256303 4124
13fe0198
MA
4125 /*
4126 * Release the hold from dmu_recv_begin. This must be done before
b5256303
TC
4127 * we return to open context, so that when we free the dataset's dnode
4128 * we can evict its bonus buffer. Since the dataset may be destroyed
4129 * at this point (and therefore won't have a valid pointer to the spa)
4130 * we release the key mapping manually here while we do have a valid
4131 * pointer, if it exists.
13fe0198 4132 */
b5256303
TC
4133 if (!drc->drc_raw && encrypted) {
4134 (void) spa_keystore_remove_mapping(dmu_tx_pool(tx)->dp_spa,
4135 drc->drc_ds->ds_object, drc->drc_ds);
4136 }
4137 dsl_dataset_disown(drc->drc_ds, 0, dmu_recv_tag);
13fe0198 4138 drc->drc_ds = NULL;
34dc7c2f
BB
4139}
4140
8d35c149 4141static int
b5256303
TC
4142add_ds_to_guidmap(const char *name, avl_tree_t *guid_map, uint64_t snapobj,
4143 boolean_t raw)
8d35c149 4144{
13fe0198 4145 dsl_pool_t *dp;
8d35c149
AS
4146 dsl_dataset_t *snapds;
4147 guid_map_entry_t *gmep;
b5256303 4148 ds_hold_flags_t dsflags = (raw) ? 0 : DS_HOLD_FLAG_DECRYPT;
8d35c149
AS
4149 int err;
4150
4151 ASSERT(guid_map != NULL);
4152
13fe0198
MA
4153 err = dsl_pool_hold(name, FTAG, &dp);
4154 if (err != 0)
4155 return (err);
7ec09286 4156 gmep = kmem_alloc(sizeof (*gmep), KM_SLEEP);
b5256303 4157 err = dsl_dataset_hold_obj_flags(dp, snapobj, dsflags, gmep, &snapds);
8d35c149 4158 if (err == 0) {
d683ddbb 4159 gmep->guid = dsl_dataset_phys(snapds)->ds_guid;
b5256303 4160 gmep->raw = raw;
8d35c149
AS
4161 gmep->gme_ds = snapds;
4162 avl_add(guid_map, gmep);
13fe0198 4163 dsl_dataset_long_hold(snapds, gmep);
7ec09286
MA
4164 } else {
4165 kmem_free(gmep, sizeof (*gmep));
8d35c149
AS
4166 }
4167
13fe0198 4168 dsl_pool_rele(dp, FTAG);
8d35c149
AS
4169 return (err);
4170}
4171
13fe0198
MA
4172static int dmu_recv_end_modified_blocks = 3;
4173
428870ff
BB
4174static int
4175dmu_recv_existing_end(dmu_recv_cookie_t *drc)
34dc7c2f 4176{
13fe0198 4177#ifdef _KERNEL
13fe0198
MA
4178 /*
4179 * We will be destroying the ds; make sure its origin is unmounted if
4180 * necessary.
4181 */
eca7b760 4182 char name[ZFS_MAX_DATASET_NAME_LEN];
13fe0198
MA
4183 dsl_dataset_name(drc->drc_ds, name);
4184 zfs_destroy_unmount_origin(name);
13fe0198 4185#endif
34dc7c2f 4186
2e0e443a 4187 return (dsl_sync_task(drc->drc_tofs,
13fe0198 4188 dmu_recv_end_check, dmu_recv_end_sync, drc,
2e0e443a 4189 dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL));
34dc7c2f 4190}
428870ff
BB
4191
4192static int
4193dmu_recv_new_end(dmu_recv_cookie_t *drc)
2e0e443a
GM
4194{
4195 return (dsl_sync_task(drc->drc_tofs,
4196 dmu_recv_end_check, dmu_recv_end_sync, drc,
4197 dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL));
4198}
4199
4200int
4201dmu_recv_end(dmu_recv_cookie_t *drc, void *owner)
428870ff 4202{
13fe0198 4203 int error;
428870ff 4204
2e0e443a
GM
4205 drc->drc_owner = owner;
4206
4207 if (drc->drc_newfs)
4208 error = dmu_recv_new_end(drc);
4209 else
4210 error = dmu_recv_existing_end(drc);
428870ff 4211
13fe0198
MA
4212 if (error != 0) {
4213 dmu_recv_cleanup_ds(drc);
b0918402 4214 nvlist_free(drc->drc_keynvl);
13fe0198 4215 } else if (drc->drc_guid_to_ds_map != NULL) {
b5256303
TC
4216 (void) add_ds_to_guidmap(drc->drc_tofs, drc->drc_guid_to_ds_map,
4217 drc->drc_newsnapobj, drc->drc_raw);
428870ff 4218 }
13fe0198 4219 return (error);
428870ff
BB
4220}
4221
ea97f8ce
MA
4222/*
4223 * Return TRUE if this objset is currently being received into.
4224 */
4225boolean_t
4226dmu_objset_is_receiving(objset_t *os)
4227{
4228 return (os->os_dsl_dataset != NULL &&
4229 os->os_dsl_dataset->ds_owner == dmu_recv_tag);
4230}
fd8febbd
TF
4231
4232#if defined(_KERNEL)
ca0845d5
PD
4233/* BEGIN CSTYLED */
4234module_param(zfs_override_estimate_recordsize, ulong, 0644);
4235MODULE_PARM_DESC(zfs_override_estimate_recordsize,
4236 "Record size calculation override for zfs send estimates");
4237/* END CSTYLED */
4238
fd8febbd
TF
4239module_param(zfs_send_corrupt_data, int, 0644);
4240MODULE_PARM_DESC(zfs_send_corrupt_data, "Allow sending corrupt data");
3b0d9928
BB
4241
4242module_param(zfs_send_queue_length, int, 0644);
4243MODULE_PARM_DESC(zfs_send_queue_length, "Maximum send queue length");
4244
4245module_param(zfs_recv_queue_length, int, 0644);
4246MODULE_PARM_DESC(zfs_recv_queue_length, "Maximum receive queue length");
fd8febbd 4247#endif