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