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