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.
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.
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]
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.
32 #include <sys/dmu_impl.h>
33 #include <sys/dmu_tx.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>
47 #include <sys/zio_checksum.h>
48 #include <sys/zfs_znode.h>
49 #include <zfs_fletcher.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>
60 #include <sys/policy.h>
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
;
69 static char *dmu_recv_tag
= "dmu_recv_tag";
70 const char *recv_clone_name
= "%recv";
72 #define BP_SPAN(datablkszsec, indblkshift, level) \
73 (((uint64_t)datablkszsec) << (SPA_MINBLOCKSHIFT + \
74 (level) * (indblkshift - SPA_BLKPTRSHIFT)))
76 static void byteswap_record(dmu_replay_record_t
*drr
);
78 struct send_thread_arg
{
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 */
85 zbookmark_phys_t resume
;
88 struct send_block_record
{
89 boolean_t eos_marker
; /* Marks the end of the stream */
93 uint16_t datablkszsec
;
97 typedef struct dump_bytes_io
{
98 dmu_sendarg_t
*dbi_dsp
;
104 dump_bytes_cb(void *arg
)
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 */
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.
121 ASSERT(dbi
->dbi_len
% 8 == 0 ||
122 (dsp
->dsa_featureflags
& DMU_BACKUP_FEATURE_RAW
) != 0);
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
);
128 mutex_enter(&ds
->ds_sendstream_lock
);
129 *dsp
->dsa_off
+= dbi
->dbi_len
;
130 mutex_exit(&ds
->ds_sendstream_lock
);
134 dump_bytes(dmu_sendarg_t
*dsp
, void *buf
, int len
)
142 #if defined(HAVE_LARGE_STACKS)
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.
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 */
155 return (dsp
->dsa_err
);
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.
164 dump_record(dmu_sendarg_t
*dsp
, void *payload
, int payload_len
)
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
),
171 if (dsp
->dsa_drr
->drr_type
== DRR_BEGIN
) {
172 dsp
->dsa_sent_begin
= B_TRUE
;
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
;
178 if (dsp
->dsa_drr
->drr_type
== DRR_END
) {
179 dsp
->dsa_sent_end
= B_TRUE
;
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
,
189 if (dump_bytes(dsp
, payload
, payload_len
) != 0)
190 return (SET_ERROR(EINTR
));
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.
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.
204 dump_free(dmu_sendarg_t
*dsp
, uint64_t object
, uint64_t offset
,
207 struct drr_free
*drrf
= &(dsp
->dsa_drr
->drr_u
.drr_free
);
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
218 * If the increasing-order constraint ever changes, we should find
219 * another way to assert that the one-record constraint is still
222 ASSERT(object
> dsp
->dsa_last_data_object
||
223 (object
== dsp
->dsa_last_data_object
&&
224 offset
> dsp
->dsa_last_data_offset
));
226 if (length
!= -1ULL && offset
+ length
< offset
)
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.
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
;
243 if (dsp
->dsa_pending_op
== PENDING_FREE
) {
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).
250 ASSERT(length
!= -1ULL);
252 * Check to see whether this free block can be aggregated
255 if (drrf
->drr_object
== object
&& drrf
->drr_offset
+
256 drrf
->drr_length
== offset
) {
257 drrf
->drr_length
+= length
;
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
;
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
));
277 dsp
->dsa_pending_op
= PENDING_FREE
;
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
)
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
);
292 * We send data in increasing object, offset order.
293 * See comment in dump_free() for details.
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;
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.
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
;
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
;
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);
327 ASSERT(BP_IS_PROTECTED(bp
));
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,
335 if (BP_SHOULD_BYTESWAP(bp
))
336 drrw
->drr_flags
|= DRR_RAW_BYTESWAP
;
337 zio_crypt_decode_params_bp(bp
, drrw
->drr_salt
,
339 zio_crypt_decode_mac_bp(bp
, drrw
->drr_mac
);
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
);
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
;
355 payload_size
= drrw
->drr_logical_size
;
358 if (bp
== NULL
|| BP_IS_EMBEDDED(bp
) || (BP_IS_PROTECTED(bp
) && !raw
)) {
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.
365 drrw
->drr_checksumtype
= ZIO_CHECKSUM_OFF
;
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
;
378 if (dump_record(dsp
, data
, payload_size
) != 0)
379 return (SET_ERROR(EINTR
));
384 dump_write_embedded(dmu_sendarg_t
*dsp
, uint64_t object
, uint64_t offset
,
385 int blksz
, const blkptr_t
*bp
)
387 char buf
[BPE_PAYLOAD_SIZE
];
388 struct drr_write_embedded
*drrw
=
389 &(dsp
->dsa_drr
->drr_u
.drr_write_embedded
);
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
;
397 ASSERT(BP_IS_EMBEDDED(bp
));
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
);
410 decode_embedded_bp_compressed(bp
, buf
);
412 if (dump_record(dsp
, buf
, P2ROUNDUP(drrw
->drr_psize
, 8)) != 0)
413 return (SET_ERROR(EINTR
));
418 dump_spill(dmu_sendarg_t
*dsp
, const blkptr_t
*bp
, uint64_t object
, void *data
)
420 struct drr_spill
*drrs
= &(dsp
->dsa_drr
->drr_u
.drr_spill
);
421 uint64_t blksz
= BP_GET_LSIZE(bp
);
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
;
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
;
436 /* handle raw send fields */
437 if (dsp
->dsa_featureflags
& DMU_BACKUP_FEATURE_RAW
) {
438 ASSERT(BP_IS_PROTECTED(bp
));
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
);
448 if (dump_record(dsp
, data
, blksz
) != 0)
449 return (SET_ERROR(EINTR
));
454 dump_freeobjects(dmu_sendarg_t
*dsp
, uint64_t firstobj
, uint64_t numobjs
)
456 struct drr_freeobjects
*drrfo
= &(dsp
->dsa_drr
->drr_u
.drr_freeobjects
);
457 uint64_t maxobj
= DNODES_PER_BLOCK
*
458 (DMU_META_DNODE(dsp
->dsa_os
)->dn_maxblkid
+ 1);
461 * ZoL < 0.7 does not handle large FREEOBJECTS records correctly,
462 * leading to zfs recv never completing. to avoid this issue, don't
463 * send FREEOBJECTS records for object IDs which cannot exist on the
467 if (maxobj
< firstobj
)
470 if (maxobj
< firstobj
+ numobjs
)
471 numobjs
= maxobj
- firstobj
;
475 * If there is a pending op, but it's not PENDING_FREEOBJECTS,
476 * push it out, since free block aggregation can only be done for
477 * blocks of the same type (i.e., DRR_FREE records can only be
478 * aggregated with other DRR_FREE records. DRR_FREEOBJECTS records
479 * can only be aggregated with other DRR_FREEOBJECTS records.
481 if (dsp
->dsa_pending_op
!= PENDING_NONE
&&
482 dsp
->dsa_pending_op
!= PENDING_FREEOBJECTS
) {
483 if (dump_record(dsp
, NULL
, 0) != 0)
484 return (SET_ERROR(EINTR
));
485 dsp
->dsa_pending_op
= PENDING_NONE
;
487 if (dsp
->dsa_pending_op
== PENDING_FREEOBJECTS
) {
489 * See whether this free object array can be aggregated
492 if (drrfo
->drr_firstobj
+ drrfo
->drr_numobjs
== firstobj
) {
493 drrfo
->drr_numobjs
+= numobjs
;
496 /* can't be aggregated. Push out pending record */
497 if (dump_record(dsp
, NULL
, 0) != 0)
498 return (SET_ERROR(EINTR
));
499 dsp
->dsa_pending_op
= PENDING_NONE
;
503 /* write a FREEOBJECTS record */
504 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
505 dsp
->dsa_drr
->drr_type
= DRR_FREEOBJECTS
;
506 drrfo
->drr_firstobj
= firstobj
;
507 drrfo
->drr_numobjs
= numobjs
;
508 drrfo
->drr_toguid
= dsp
->dsa_toguid
;
510 dsp
->dsa_pending_op
= PENDING_FREEOBJECTS
;
516 dump_dnode(dmu_sendarg_t
*dsp
, const blkptr_t
*bp
, uint64_t object
,
519 struct drr_object
*drro
= &(dsp
->dsa_drr
->drr_u
.drr_object
);
522 if (object
< dsp
->dsa_resume_object
) {
524 * Note: when resuming, we will visit all the dnodes in
525 * the block of dnodes that we are resuming from. In
526 * this case it's unnecessary to send the dnodes prior to
527 * the one we are resuming from. We should be at most one
528 * block's worth of dnodes behind the resume point.
530 ASSERT3U(dsp
->dsa_resume_object
- object
, <,
531 1 << (DNODE_BLOCK_SHIFT
- DNODE_SHIFT
));
535 if (dnp
== NULL
|| dnp
->dn_type
== DMU_OT_NONE
)
536 return (dump_freeobjects(dsp
, object
, 1));
538 if (dsp
->dsa_pending_op
!= PENDING_NONE
) {
539 if (dump_record(dsp
, NULL
, 0) != 0)
540 return (SET_ERROR(EINTR
));
541 dsp
->dsa_pending_op
= PENDING_NONE
;
544 /* write an OBJECT record */
545 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
546 dsp
->dsa_drr
->drr_type
= DRR_OBJECT
;
547 drro
->drr_object
= object
;
548 drro
->drr_type
= dnp
->dn_type
;
549 drro
->drr_bonustype
= dnp
->dn_bonustype
;
550 drro
->drr_blksz
= dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
;
551 drro
->drr_bonuslen
= dnp
->dn_bonuslen
;
552 drro
->drr_dn_slots
= dnp
->dn_extra_slots
+ 1;
553 drro
->drr_checksumtype
= dnp
->dn_checksum
;
554 drro
->drr_compress
= dnp
->dn_compress
;
555 drro
->drr_toguid
= dsp
->dsa_toguid
;
557 if (!(dsp
->dsa_featureflags
& DMU_BACKUP_FEATURE_LARGE_BLOCKS
) &&
558 drro
->drr_blksz
> SPA_OLD_MAXBLOCKSIZE
)
559 drro
->drr_blksz
= SPA_OLD_MAXBLOCKSIZE
;
561 bonuslen
= P2ROUNDUP(dnp
->dn_bonuslen
, 8);
563 if ((dsp
->dsa_featureflags
& DMU_BACKUP_FEATURE_RAW
)) {
564 ASSERT(BP_IS_ENCRYPTED(bp
));
566 if (BP_SHOULD_BYTESWAP(bp
))
567 drro
->drr_flags
|= DRR_RAW_BYTESWAP
;
569 /* needed for reconstructing dnp on recv side */
570 drro
->drr_indblkshift
= dnp
->dn_indblkshift
;
571 drro
->drr_nlevels
= dnp
->dn_nlevels
;
572 drro
->drr_nblkptr
= dnp
->dn_nblkptr
;
575 * Since we encrypt the entire bonus area, the (raw) part
576 * beyond the bonuslen is actually nonzero, so we need
580 drro
->drr_raw_bonuslen
= DN_MAX_BONUS_LEN(dnp
);
581 bonuslen
= drro
->drr_raw_bonuslen
;
585 if (dump_record(dsp
, DN_BONUS(dnp
), bonuslen
) != 0)
586 return (SET_ERROR(EINTR
));
588 /* Free anything past the end of the file. */
589 if (dump_free(dsp
, object
, (dnp
->dn_maxblkid
+ 1) *
590 (dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
), -1ULL) != 0)
591 return (SET_ERROR(EINTR
));
592 if (dsp
->dsa_err
!= 0)
593 return (SET_ERROR(EINTR
));
598 dump_object_range(dmu_sendarg_t
*dsp
, const blkptr_t
*bp
, uint64_t firstobj
,
601 struct drr_object_range
*drror
=
602 &(dsp
->dsa_drr
->drr_u
.drr_object_range
);
604 /* we only use this record type for raw sends */
605 ASSERT(BP_IS_PROTECTED(bp
));
606 ASSERT(dsp
->dsa_featureflags
& DMU_BACKUP_FEATURE_RAW
);
607 ASSERT3U(BP_GET_COMPRESS(bp
), ==, ZIO_COMPRESS_OFF
);
608 ASSERT3U(BP_GET_TYPE(bp
), ==, DMU_OT_DNODE
);
609 ASSERT0(BP_GET_LEVEL(bp
));
611 if (dsp
->dsa_pending_op
!= PENDING_NONE
) {
612 if (dump_record(dsp
, NULL
, 0) != 0)
613 return (SET_ERROR(EINTR
));
614 dsp
->dsa_pending_op
= PENDING_NONE
;
617 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
618 dsp
->dsa_drr
->drr_type
= DRR_OBJECT_RANGE
;
619 drror
->drr_firstobj
= firstobj
;
620 drror
->drr_numslots
= numslots
;
621 drror
->drr_toguid
= dsp
->dsa_toguid
;
622 if (BP_SHOULD_BYTESWAP(bp
))
623 drror
->drr_flags
|= DRR_RAW_BYTESWAP
;
624 zio_crypt_decode_params_bp(bp
, drror
->drr_salt
, drror
->drr_iv
);
625 zio_crypt_decode_mac_bp(bp
, drror
->drr_mac
);
627 if (dump_record(dsp
, NULL
, 0) != 0)
628 return (SET_ERROR(EINTR
));
633 backup_do_embed(dmu_sendarg_t
*dsp
, const blkptr_t
*bp
)
635 if (!BP_IS_EMBEDDED(bp
))
639 * Compression function must be legacy, or explicitly enabled.
641 if ((BP_GET_COMPRESS(bp
) >= ZIO_COMPRESS_LEGACY_FUNCTIONS
&&
642 !(dsp
->dsa_featureflags
& DMU_BACKUP_FEATURE_LZ4
)))
646 * Embed type must be explicitly enabled.
648 switch (BPE_GET_ETYPE(bp
)) {
649 case BP_EMBEDDED_TYPE_DATA
:
650 if (dsp
->dsa_featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA
)
660 * This is the callback function to traverse_dataset that acts as the worker
661 * thread for dmu_send_impl.
665 send_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
666 const zbookmark_phys_t
*zb
, const struct dnode_phys
*dnp
, void *arg
)
668 struct send_thread_arg
*sta
= arg
;
669 struct send_block_record
*record
;
670 uint64_t record_size
;
673 ASSERT(zb
->zb_object
== DMU_META_DNODE_OBJECT
||
674 zb
->zb_object
>= sta
->resume
.zb_object
);
675 ASSERT3P(sta
->ds
, !=, NULL
);
678 return (SET_ERROR(EINTR
));
681 ASSERT3U(zb
->zb_level
, ==, ZB_DNODE_LEVEL
);
683 } else if (zb
->zb_level
< 0) {
687 record
= kmem_zalloc(sizeof (struct send_block_record
), KM_SLEEP
);
688 record
->eos_marker
= B_FALSE
;
691 record
->indblkshift
= dnp
->dn_indblkshift
;
692 record
->datablkszsec
= dnp
->dn_datablkszsec
;
693 record_size
= dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
;
694 bqueue_enqueue(&sta
->q
, record
, record_size
);
700 * This function kicks off the traverse_dataset. It also handles setting the
701 * error code of the thread in case something goes wrong, and pushes the End of
702 * Stream record when the traverse_dataset call has finished. If there is no
703 * dataset to traverse, the thread immediately pushes End of Stream marker.
706 send_traverse_thread(void *arg
)
708 struct send_thread_arg
*st_arg
= arg
;
710 struct send_block_record
*data
;
711 fstrans_cookie_t cookie
= spl_fstrans_mark();
713 if (st_arg
->ds
!= NULL
) {
714 err
= traverse_dataset_resume(st_arg
->ds
,
715 st_arg
->fromtxg
, &st_arg
->resume
,
716 st_arg
->flags
, send_cb
, st_arg
);
719 st_arg
->error_code
= err
;
721 data
= kmem_zalloc(sizeof (*data
), KM_SLEEP
);
722 data
->eos_marker
= B_TRUE
;
723 bqueue_enqueue(&st_arg
->q
, data
, 1);
724 spl_fstrans_unmark(cookie
);
729 * This function actually handles figuring out what kind of record needs to be
730 * dumped, reading the data (which has hopefully been prefetched), and calling
731 * the appropriate helper function.
734 do_dump(dmu_sendarg_t
*dsa
, struct send_block_record
*data
)
736 dsl_dataset_t
*ds
= dmu_objset_ds(dsa
->dsa_os
);
737 const blkptr_t
*bp
= &data
->bp
;
738 const zbookmark_phys_t
*zb
= &data
->zb
;
739 uint8_t indblkshift
= data
->indblkshift
;
740 uint16_t dblkszsec
= data
->datablkszsec
;
741 spa_t
*spa
= ds
->ds_dir
->dd_pool
->dp_spa
;
742 dmu_object_type_t type
= bp
? BP_GET_TYPE(bp
) : DMU_OT_NONE
;
746 ASSERT3U(zb
->zb_level
, >=, 0);
748 ASSERT(zb
->zb_object
== DMU_META_DNODE_OBJECT
||
749 zb
->zb_object
>= dsa
->dsa_resume_object
);
752 * All bps of an encrypted os should have the encryption bit set.
753 * If this is not true it indicates tampering and we report an error.
755 if (dsa
->dsa_os
->os_encrypted
&&
756 !BP_IS_HOLE(bp
) && !BP_USES_CRYPT(bp
)) {
757 spa_log_error(spa
, zb
);
758 zfs_panic_recover("unencrypted block in encrypted "
759 "object set %llu", ds
->ds_object
);
760 return (SET_ERROR(EIO
));
763 if (zb
->zb_object
!= DMU_META_DNODE_OBJECT
&&
764 DMU_OBJECT_IS_SPECIAL(zb
->zb_object
)) {
766 } else if (BP_IS_HOLE(bp
) &&
767 zb
->zb_object
== DMU_META_DNODE_OBJECT
) {
768 uint64_t span
= BP_SPAN(dblkszsec
, indblkshift
, zb
->zb_level
);
769 uint64_t dnobj
= (zb
->zb_blkid
* span
) >> DNODE_SHIFT
;
770 err
= dump_freeobjects(dsa
, dnobj
, span
>> DNODE_SHIFT
);
771 } else if (BP_IS_HOLE(bp
)) {
772 uint64_t span
= BP_SPAN(dblkszsec
, indblkshift
, zb
->zb_level
);
773 uint64_t offset
= zb
->zb_blkid
* span
;
774 err
= dump_free(dsa
, zb
->zb_object
, offset
, span
);
775 } else if (zb
->zb_level
> 0 || type
== DMU_OT_OBJSET
) {
777 } else if (type
== DMU_OT_DNODE
) {
779 int epb
= BP_GET_LSIZE(bp
) >> DNODE_SHIFT
;
780 arc_flags_t aflags
= ARC_FLAG_WAIT
;
782 enum zio_flag zioflags
= ZIO_FLAG_CANFAIL
;
785 if (dsa
->dsa_featureflags
& DMU_BACKUP_FEATURE_RAW
) {
786 ASSERT(BP_IS_ENCRYPTED(bp
));
787 ASSERT3U(BP_GET_COMPRESS(bp
), ==, ZIO_COMPRESS_OFF
);
788 zioflags
|= ZIO_FLAG_RAW
;
791 ASSERT0(zb
->zb_level
);
793 if (arc_read(NULL
, spa
, bp
, arc_getbuf_func
, &abuf
,
794 ZIO_PRIORITY_ASYNC_READ
, zioflags
, &aflags
, zb
) != 0)
795 return (SET_ERROR(EIO
));
798 dnobj
= zb
->zb_blkid
* epb
;
801 * Raw sends require sending encryption parameters for the
802 * block of dnodes. Regular sends do not need to send this
805 if (dsa
->dsa_featureflags
& DMU_BACKUP_FEATURE_RAW
) {
806 ASSERT(arc_is_encrypted(abuf
));
807 err
= dump_object_range(dsa
, bp
, dnobj
, epb
);
811 for (i
= 0; i
< epb
; i
+= blk
[i
].dn_extra_slots
+ 1) {
812 err
= dump_dnode(dsa
, bp
, dnobj
+ i
, blk
+ i
);
817 arc_buf_destroy(abuf
, &abuf
);
818 } else if (type
== DMU_OT_SA
) {
819 arc_flags_t aflags
= ARC_FLAG_WAIT
;
821 enum zio_flag zioflags
= ZIO_FLAG_CANFAIL
;
823 if (dsa
->dsa_featureflags
& DMU_BACKUP_FEATURE_RAW
) {
824 ASSERT(BP_IS_PROTECTED(bp
));
825 zioflags
|= ZIO_FLAG_RAW
;
828 if (arc_read(NULL
, spa
, bp
, arc_getbuf_func
, &abuf
,
829 ZIO_PRIORITY_ASYNC_READ
, zioflags
, &aflags
, zb
) != 0)
830 return (SET_ERROR(EIO
));
832 err
= dump_spill(dsa
, bp
, zb
->zb_object
, abuf
->b_data
);
833 arc_buf_destroy(abuf
, &abuf
);
834 } else if (backup_do_embed(dsa
, bp
)) {
835 /* it's an embedded level-0 block of a regular object */
836 int blksz
= dblkszsec
<< SPA_MINBLOCKSHIFT
;
837 ASSERT0(zb
->zb_level
);
838 err
= dump_write_embedded(dsa
, zb
->zb_object
,
839 zb
->zb_blkid
* blksz
, blksz
, bp
);
841 /* it's a level-0 block of a regular object */
842 arc_flags_t aflags
= ARC_FLAG_WAIT
;
844 int blksz
= dblkszsec
<< SPA_MINBLOCKSHIFT
;
848 * If we have large blocks stored on disk but the send flags
849 * don't allow us to send large blocks, we split the data from
850 * the arc buf into chunks.
852 boolean_t split_large_blocks
= blksz
> SPA_OLD_MAXBLOCKSIZE
&&
853 !(dsa
->dsa_featureflags
& DMU_BACKUP_FEATURE_LARGE_BLOCKS
);
856 * Raw sends require that we always get raw data as it exists
857 * on disk, so we assert that we are not splitting blocks here.
859 boolean_t request_raw
=
860 (dsa
->dsa_featureflags
& DMU_BACKUP_FEATURE_RAW
) != 0;
863 * We should only request compressed data from the ARC if all
864 * the following are true:
865 * - stream compression was requested
866 * - we aren't splitting large blocks into smaller chunks
867 * - the data won't need to be byteswapped before sending
868 * - this isn't an embedded block
869 * - this isn't metadata (if receiving on a different endian
870 * system it can be byteswapped more easily)
872 boolean_t request_compressed
=
873 (dsa
->dsa_featureflags
& DMU_BACKUP_FEATURE_COMPRESSED
) &&
874 !split_large_blocks
&& !BP_SHOULD_BYTESWAP(bp
) &&
875 !BP_IS_EMBEDDED(bp
) && !DMU_OT_IS_METADATA(BP_GET_TYPE(bp
));
877 IMPLY(request_raw
, !split_large_blocks
);
878 IMPLY(request_raw
, BP_IS_PROTECTED(bp
));
879 ASSERT0(zb
->zb_level
);
880 ASSERT(zb
->zb_object
> dsa
->dsa_resume_object
||
881 (zb
->zb_object
== dsa
->dsa_resume_object
&&
882 zb
->zb_blkid
* blksz
>= dsa
->dsa_resume_offset
));
884 ASSERT3U(blksz
, ==, BP_GET_LSIZE(bp
));
886 enum zio_flag zioflags
= ZIO_FLAG_CANFAIL
;
888 zioflags
|= ZIO_FLAG_RAW
;
889 else if (request_compressed
)
890 zioflags
|= ZIO_FLAG_RAW_COMPRESS
;
892 if (arc_read(NULL
, spa
, bp
, arc_getbuf_func
, &abuf
,
893 ZIO_PRIORITY_ASYNC_READ
, zioflags
, &aflags
, zb
) != 0) {
894 if (zfs_send_corrupt_data
) {
895 /* Send a block filled with 0x"zfs badd bloc" */
896 abuf
= arc_alloc_buf(spa
, &abuf
, ARC_BUFC_DATA
,
899 for (ptr
= abuf
->b_data
;
900 (char *)ptr
< (char *)abuf
->b_data
+ blksz
;
902 *ptr
= 0x2f5baddb10cULL
;
904 return (SET_ERROR(EIO
));
908 offset
= zb
->zb_blkid
* blksz
;
910 if (split_large_blocks
) {
911 ASSERT0(arc_is_encrypted(abuf
));
912 ASSERT3U(arc_get_compression(abuf
), ==,
914 char *buf
= abuf
->b_data
;
915 while (blksz
> 0 && err
== 0) {
916 int n
= MIN(blksz
, SPA_OLD_MAXBLOCKSIZE
);
917 err
= dump_write(dsa
, type
, zb
->zb_object
,
918 offset
, n
, n
, NULL
, buf
);
924 err
= dump_write(dsa
, type
, zb
->zb_object
, offset
,
925 blksz
, arc_buf_size(abuf
), bp
, abuf
->b_data
);
927 arc_buf_destroy(abuf
, &abuf
);
930 ASSERT(err
== 0 || err
== EINTR
);
935 * Pop the new data off the queue, and free the old data.
937 static struct send_block_record
*
938 get_next_record(bqueue_t
*bq
, struct send_block_record
*data
)
940 struct send_block_record
*tmp
= bqueue_dequeue(bq
);
941 kmem_free(data
, sizeof (*data
));
946 * Actually do the bulk of the work in a zfs send.
948 * Note: Releases dp using the specified tag.
951 dmu_send_impl(void *tag
, dsl_pool_t
*dp
, dsl_dataset_t
*to_ds
,
952 zfs_bookmark_phys_t
*ancestor_zb
, boolean_t is_clone
,
953 boolean_t embedok
, boolean_t large_block_ok
, boolean_t compressok
,
954 boolean_t rawok
, int outfd
, uint64_t resumeobj
, uint64_t resumeoff
,
955 vnode_t
*vp
, offset_t
*off
)
958 dmu_replay_record_t
*drr
;
961 uint64_t fromtxg
= 0;
962 uint64_t featureflags
= 0;
963 struct send_thread_arg to_arg
;
964 void *payload
= NULL
;
965 size_t payload_len
= 0;
966 struct send_block_record
*to_data
;
968 err
= dmu_objset_from_ds(to_ds
, &os
);
970 dsl_pool_rele(dp
, tag
);
975 * If this is a non-raw send of an encrypted ds, we can ensure that
976 * the objset_phys_t is authenticated. This is safe because this is
977 * either a snapshot or we have owned the dataset, ensuring that
978 * it can't be modified.
980 if (!rawok
&& os
->os_encrypted
&&
981 arc_is_unauthenticated(os
->os_phys_buf
)) {
982 err
= arc_untransform(os
->os_phys_buf
, os
->os_spa
,
983 to_ds
->ds_object
, B_FALSE
);
985 dsl_pool_rele(dp
, tag
);
989 ASSERT0(arc_is_unauthenticated(os
->os_phys_buf
));
992 drr
= kmem_zalloc(sizeof (dmu_replay_record_t
), KM_SLEEP
);
993 drr
->drr_type
= DRR_BEGIN
;
994 drr
->drr_u
.drr_begin
.drr_magic
= DMU_BACKUP_MAGIC
;
995 DMU_SET_STREAM_HDRTYPE(drr
->drr_u
.drr_begin
.drr_versioninfo
,
998 bzero(&to_arg
, sizeof (to_arg
));
1001 if (dmu_objset_type(os
) == DMU_OST_ZFS
) {
1003 if (zfs_get_zplprop(os
, ZFS_PROP_VERSION
, &version
) != 0) {
1004 kmem_free(drr
, sizeof (dmu_replay_record_t
));
1005 dsl_pool_rele(dp
, tag
);
1006 return (SET_ERROR(EINVAL
));
1008 if (version
>= ZPL_VERSION_SA
) {
1009 featureflags
|= DMU_BACKUP_FEATURE_SA_SPILL
;
1014 /* raw sends imply large_block_ok */
1015 if ((large_block_ok
|| rawok
) &&
1016 to_ds
->ds_feature_inuse
[SPA_FEATURE_LARGE_BLOCKS
])
1017 featureflags
|= DMU_BACKUP_FEATURE_LARGE_BLOCKS
;
1018 if (to_ds
->ds_feature_inuse
[SPA_FEATURE_LARGE_DNODE
])
1019 featureflags
|= DMU_BACKUP_FEATURE_LARGE_DNODE
;
1021 /* encrypted datasets will not have embedded blocks */
1022 if ((embedok
|| rawok
) && !os
->os_encrypted
&&
1023 spa_feature_is_active(dp
->dp_spa
, SPA_FEATURE_EMBEDDED_DATA
)) {
1024 featureflags
|= DMU_BACKUP_FEATURE_EMBED_DATA
;
1027 /* raw send implies compressok */
1028 if (compressok
|| rawok
)
1029 featureflags
|= DMU_BACKUP_FEATURE_COMPRESSED
;
1030 if (rawok
&& os
->os_encrypted
)
1031 featureflags
|= DMU_BACKUP_FEATURE_RAW
;
1034 (DMU_BACKUP_FEATURE_EMBED_DATA
| DMU_BACKUP_FEATURE_COMPRESSED
|
1035 DMU_BACKUP_FEATURE_RAW
)) != 0 &&
1036 spa_feature_is_active(dp
->dp_spa
, SPA_FEATURE_LZ4_COMPRESS
)) {
1037 featureflags
|= DMU_BACKUP_FEATURE_LZ4
;
1040 if (resumeobj
!= 0 || resumeoff
!= 0) {
1041 featureflags
|= DMU_BACKUP_FEATURE_RESUMING
;
1044 DMU_SET_FEATUREFLAGS(drr
->drr_u
.drr_begin
.drr_versioninfo
,
1047 drr
->drr_u
.drr_begin
.drr_creation_time
=
1048 dsl_dataset_phys(to_ds
)->ds_creation_time
;
1049 drr
->drr_u
.drr_begin
.drr_type
= dmu_objset_type(os
);
1051 drr
->drr_u
.drr_begin
.drr_flags
|= DRR_FLAG_CLONE
;
1052 drr
->drr_u
.drr_begin
.drr_toguid
= dsl_dataset_phys(to_ds
)->ds_guid
;
1053 if (dsl_dataset_phys(to_ds
)->ds_flags
& DS_FLAG_CI_DATASET
)
1054 drr
->drr_u
.drr_begin
.drr_flags
|= DRR_FLAG_CI_DATA
;
1055 if (zfs_send_set_freerecords_bit
)
1056 drr
->drr_u
.drr_begin
.drr_flags
|= DRR_FLAG_FREERECORDS
;
1058 if (ancestor_zb
!= NULL
) {
1059 drr
->drr_u
.drr_begin
.drr_fromguid
=
1060 ancestor_zb
->zbm_guid
;
1061 fromtxg
= ancestor_zb
->zbm_creation_txg
;
1063 dsl_dataset_name(to_ds
, drr
->drr_u
.drr_begin
.drr_toname
);
1064 if (!to_ds
->ds_is_snapshot
) {
1065 (void) strlcat(drr
->drr_u
.drr_begin
.drr_toname
, "@--head--",
1066 sizeof (drr
->drr_u
.drr_begin
.drr_toname
));
1069 dsp
= kmem_zalloc(sizeof (dmu_sendarg_t
), KM_SLEEP
);
1073 dsp
->dsa_outfd
= outfd
;
1074 dsp
->dsa_proc
= curproc
;
1077 dsp
->dsa_toguid
= dsl_dataset_phys(to_ds
)->ds_guid
;
1078 dsp
->dsa_pending_op
= PENDING_NONE
;
1079 dsp
->dsa_featureflags
= featureflags
;
1080 dsp
->dsa_resume_object
= resumeobj
;
1081 dsp
->dsa_resume_offset
= resumeoff
;
1083 mutex_enter(&to_ds
->ds_sendstream_lock
);
1084 list_insert_head(&to_ds
->ds_sendstreams
, dsp
);
1085 mutex_exit(&to_ds
->ds_sendstream_lock
);
1087 dsl_dataset_long_hold(to_ds
, FTAG
);
1088 dsl_pool_rele(dp
, tag
);
1090 /* handle features that require a DRR_BEGIN payload */
1092 (DMU_BACKUP_FEATURE_RESUMING
| DMU_BACKUP_FEATURE_RAW
)) {
1093 nvlist_t
*keynvl
= NULL
;
1094 nvlist_t
*nvl
= fnvlist_alloc();
1096 if (featureflags
& DMU_BACKUP_FEATURE_RESUMING
) {
1097 dmu_object_info_t to_doi
;
1098 err
= dmu_object_info(os
, resumeobj
, &to_doi
);
1104 SET_BOOKMARK(&to_arg
.resume
, to_ds
->ds_object
,
1106 resumeoff
/ to_doi
.doi_data_block_size
);
1108 fnvlist_add_uint64(nvl
, "resume_object", resumeobj
);
1109 fnvlist_add_uint64(nvl
, "resume_offset", resumeoff
);
1112 if (featureflags
& DMU_BACKUP_FEATURE_RAW
) {
1113 ASSERT(os
->os_encrypted
);
1115 err
= dsl_crypto_populate_key_nvlist(to_ds
, &keynvl
);
1121 fnvlist_add_nvlist(nvl
, "crypt_keydata", keynvl
);
1124 payload
= fnvlist_pack(nvl
, &payload_len
);
1125 drr
->drr_payloadlen
= payload_len
;
1126 fnvlist_free(keynvl
);
1130 err
= dump_record(dsp
, payload
, payload_len
);
1131 fnvlist_pack_free(payload
, payload_len
);
1137 err
= bqueue_init(&to_arg
.q
, zfs_send_queue_length
,
1138 offsetof(struct send_block_record
, ln
));
1139 to_arg
.error_code
= 0;
1140 to_arg
.cancel
= B_FALSE
;
1142 to_arg
.fromtxg
= fromtxg
;
1143 to_arg
.flags
= TRAVERSE_PRE
| TRAVERSE_PREFETCH
;
1145 to_arg
.flags
|= TRAVERSE_NO_DECRYPT
;
1146 (void) thread_create(NULL
, 0, send_traverse_thread
, &to_arg
, 0, curproc
,
1147 TS_RUN
, minclsyspri
);
1149 to_data
= bqueue_dequeue(&to_arg
.q
);
1151 while (!to_data
->eos_marker
&& err
== 0) {
1152 err
= do_dump(dsp
, to_data
);
1153 to_data
= get_next_record(&to_arg
.q
, to_data
);
1154 if (issig(JUSTLOOKING
) && issig(FORREAL
))
1159 to_arg
.cancel
= B_TRUE
;
1160 while (!to_data
->eos_marker
) {
1161 to_data
= get_next_record(&to_arg
.q
, to_data
);
1164 kmem_free(to_data
, sizeof (*to_data
));
1166 bqueue_destroy(&to_arg
.q
);
1168 if (err
== 0 && to_arg
.error_code
!= 0)
1169 err
= to_arg
.error_code
;
1174 if (dsp
->dsa_pending_op
!= PENDING_NONE
)
1175 if (dump_record(dsp
, NULL
, 0) != 0)
1176 err
= SET_ERROR(EINTR
);
1179 if (err
== EINTR
&& dsp
->dsa_err
!= 0)
1184 bzero(drr
, sizeof (dmu_replay_record_t
));
1185 drr
->drr_type
= DRR_END
;
1186 drr
->drr_u
.drr_end
.drr_checksum
= dsp
->dsa_zc
;
1187 drr
->drr_u
.drr_end
.drr_toguid
= dsp
->dsa_toguid
;
1189 if (dump_record(dsp
, NULL
, 0) != 0)
1192 mutex_enter(&to_ds
->ds_sendstream_lock
);
1193 list_remove(&to_ds
->ds_sendstreams
, dsp
);
1194 mutex_exit(&to_ds
->ds_sendstream_lock
);
1196 VERIFY(err
!= 0 || (dsp
->dsa_sent_begin
&& dsp
->dsa_sent_end
));
1198 kmem_free(drr
, sizeof (dmu_replay_record_t
));
1199 kmem_free(dsp
, sizeof (dmu_sendarg_t
));
1201 dsl_dataset_long_rele(to_ds
, FTAG
);
1207 dmu_send_obj(const char *pool
, uint64_t tosnap
, uint64_t fromsnap
,
1208 boolean_t embedok
, boolean_t large_block_ok
, boolean_t compressok
,
1209 boolean_t rawok
, int outfd
, vnode_t
*vp
, offset_t
*off
)
1213 dsl_dataset_t
*fromds
= NULL
;
1214 ds_hold_flags_t dsflags
= (rawok
) ? 0 : DS_HOLD_FLAG_DECRYPT
;
1217 err
= dsl_pool_hold(pool
, FTAG
, &dp
);
1221 err
= dsl_dataset_hold_obj_flags(dp
, tosnap
, dsflags
, FTAG
, &ds
);
1223 dsl_pool_rele(dp
, FTAG
);
1227 if (fromsnap
!= 0) {
1228 zfs_bookmark_phys_t zb
;
1231 err
= dsl_dataset_hold_obj(dp
, fromsnap
, FTAG
, &fromds
);
1233 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
1234 dsl_pool_rele(dp
, FTAG
);
1237 if (!dsl_dataset_is_before(ds
, fromds
, 0))
1238 err
= SET_ERROR(EXDEV
);
1239 zb
.zbm_creation_time
=
1240 dsl_dataset_phys(fromds
)->ds_creation_time
;
1241 zb
.zbm_creation_txg
= dsl_dataset_phys(fromds
)->ds_creation_txg
;
1242 zb
.zbm_guid
= dsl_dataset_phys(fromds
)->ds_guid
;
1243 is_clone
= (fromds
->ds_dir
!= ds
->ds_dir
);
1244 dsl_dataset_rele(fromds
, FTAG
);
1245 err
= dmu_send_impl(FTAG
, dp
, ds
, &zb
, is_clone
,
1246 embedok
, large_block_ok
, compressok
, rawok
, outfd
,
1249 err
= dmu_send_impl(FTAG
, dp
, ds
, NULL
, B_FALSE
,
1250 embedok
, large_block_ok
, compressok
, rawok
, outfd
,
1253 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
1258 dmu_send(const char *tosnap
, const char *fromsnap
, boolean_t embedok
,
1259 boolean_t large_block_ok
, boolean_t compressok
, boolean_t rawok
,
1260 int outfd
, uint64_t resumeobj
, uint64_t resumeoff
, vnode_t
*vp
,
1266 ds_hold_flags_t dsflags
= (rawok
) ? 0 : DS_HOLD_FLAG_DECRYPT
;
1267 boolean_t owned
= B_FALSE
;
1269 if (fromsnap
!= NULL
&& strpbrk(fromsnap
, "@#") == NULL
)
1270 return (SET_ERROR(EINVAL
));
1272 err
= dsl_pool_hold(tosnap
, FTAG
, &dp
);
1276 if (strchr(tosnap
, '@') == NULL
&& spa_writeable(dp
->dp_spa
)) {
1278 * We are sending a filesystem or volume. Ensure
1279 * that it doesn't change by owning the dataset.
1281 err
= dsl_dataset_own(dp
, tosnap
, dsflags
, FTAG
, &ds
);
1284 err
= dsl_dataset_hold_flags(dp
, tosnap
, dsflags
, FTAG
, &ds
);
1287 dsl_pool_rele(dp
, FTAG
);
1291 if (fromsnap
!= NULL
) {
1292 zfs_bookmark_phys_t zb
;
1293 boolean_t is_clone
= B_FALSE
;
1294 int fsnamelen
= strchr(tosnap
, '@') - tosnap
;
1297 * If the fromsnap is in a different filesystem, then
1298 * mark the send stream as a clone.
1300 if (strncmp(tosnap
, fromsnap
, fsnamelen
) != 0 ||
1301 (fromsnap
[fsnamelen
] != '@' &&
1302 fromsnap
[fsnamelen
] != '#')) {
1306 if (strchr(fromsnap
, '@')) {
1307 dsl_dataset_t
*fromds
;
1308 err
= dsl_dataset_hold(dp
, fromsnap
, FTAG
, &fromds
);
1310 if (!dsl_dataset_is_before(ds
, fromds
, 0))
1311 err
= SET_ERROR(EXDEV
);
1312 zb
.zbm_creation_time
=
1313 dsl_dataset_phys(fromds
)->ds_creation_time
;
1314 zb
.zbm_creation_txg
=
1315 dsl_dataset_phys(fromds
)->ds_creation_txg
;
1316 zb
.zbm_guid
= dsl_dataset_phys(fromds
)->ds_guid
;
1317 is_clone
= (ds
->ds_dir
!= fromds
->ds_dir
);
1318 dsl_dataset_rele(fromds
, FTAG
);
1321 err
= dsl_bookmark_lookup(dp
, fromsnap
, ds
, &zb
);
1325 dsl_dataset_disown(ds
, dsflags
, FTAG
);
1327 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
1329 dsl_pool_rele(dp
, FTAG
);
1332 err
= dmu_send_impl(FTAG
, dp
, ds
, &zb
, is_clone
,
1333 embedok
, large_block_ok
, compressok
, rawok
,
1334 outfd
, resumeobj
, resumeoff
, vp
, off
);
1336 err
= dmu_send_impl(FTAG
, dp
, ds
, NULL
, B_FALSE
,
1337 embedok
, large_block_ok
, compressok
, rawok
,
1338 outfd
, resumeobj
, resumeoff
, vp
, off
);
1341 dsl_dataset_disown(ds
, dsflags
, FTAG
);
1343 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
1349 dmu_adjust_send_estimate_for_indirects(dsl_dataset_t
*ds
, uint64_t uncompressed
,
1350 uint64_t compressed
, boolean_t stream_compressed
, uint64_t *sizep
)
1355 * Assume that space (both on-disk and in-stream) is dominated by
1356 * data. We will adjust for indirect blocks and the copies property,
1357 * but ignore per-object space used (eg, dnodes and DRR_OBJECT records).
1360 uint64_t recordsize
;
1361 uint64_t record_count
;
1363 VERIFY0(dmu_objset_from_ds(ds
, &os
));
1365 /* Assume all (uncompressed) blocks are recordsize. */
1366 if (os
->os_phys
->os_type
== DMU_OST_ZVOL
) {
1367 err
= dsl_prop_get_int_ds(ds
,
1368 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
), &recordsize
);
1370 err
= dsl_prop_get_int_ds(ds
,
1371 zfs_prop_to_name(ZFS_PROP_RECORDSIZE
), &recordsize
);
1375 record_count
= uncompressed
/ recordsize
;
1378 * If we're estimating a send size for a compressed stream, use the
1379 * compressed data size to estimate the stream size. Otherwise, use the
1380 * uncompressed data size.
1382 size
= stream_compressed
? compressed
: uncompressed
;
1385 * Subtract out approximate space used by indirect blocks.
1386 * Assume most space is used by data blocks (non-indirect, non-dnode).
1387 * Assume no ditto blocks or internal fragmentation.
1389 * Therefore, space used by indirect blocks is sizeof(blkptr_t) per
1392 size
-= record_count
* sizeof (blkptr_t
);
1394 /* Add in the space for the record associated with each block. */
1395 size
+= record_count
* sizeof (dmu_replay_record_t
);
1403 dmu_send_estimate(dsl_dataset_t
*ds
, dsl_dataset_t
*fromds
,
1404 boolean_t stream_compressed
, uint64_t *sizep
)
1407 uint64_t uncomp
, comp
;
1409 ASSERT(dsl_pool_config_held(ds
->ds_dir
->dd_pool
));
1411 /* tosnap must be a snapshot */
1412 if (!ds
->ds_is_snapshot
)
1413 return (SET_ERROR(EINVAL
));
1415 /* fromsnap, if provided, must be a snapshot */
1416 if (fromds
!= NULL
&& !fromds
->ds_is_snapshot
)
1417 return (SET_ERROR(EINVAL
));
1420 * fromsnap must be an earlier snapshot from the same fs as tosnap,
1421 * or the origin's fs.
1423 if (fromds
!= NULL
&& !dsl_dataset_is_before(ds
, fromds
, 0))
1424 return (SET_ERROR(EXDEV
));
1426 /* Get compressed and uncompressed size estimates of changed data. */
1427 if (fromds
== NULL
) {
1428 uncomp
= dsl_dataset_phys(ds
)->ds_uncompressed_bytes
;
1429 comp
= dsl_dataset_phys(ds
)->ds_compressed_bytes
;
1432 err
= dsl_dataset_space_written(fromds
, ds
,
1433 &used
, &comp
, &uncomp
);
1438 err
= dmu_adjust_send_estimate_for_indirects(ds
, uncomp
, comp
,
1439 stream_compressed
, sizep
);
1441 * Add the size of the BEGIN and END records to the estimate.
1443 *sizep
+= 2 * sizeof (dmu_replay_record_t
);
1447 struct calculate_send_arg
{
1448 uint64_t uncompressed
;
1449 uint64_t compressed
;
1453 * Simple callback used to traverse the blocks of a snapshot and sum their
1454 * uncompressed and compressed sizes.
1458 dmu_calculate_send_traversal(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
1459 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
1461 struct calculate_send_arg
*space
= arg
;
1462 if (bp
!= NULL
&& !BP_IS_HOLE(bp
)) {
1463 space
->uncompressed
+= BP_GET_UCSIZE(bp
);
1464 space
->compressed
+= BP_GET_PSIZE(bp
);
1470 * Given a desination snapshot and a TXG, calculate the approximate size of a
1471 * send stream sent from that TXG. from_txg may be zero, indicating that the
1472 * whole snapshot will be sent.
1475 dmu_send_estimate_from_txg(dsl_dataset_t
*ds
, uint64_t from_txg
,
1476 boolean_t stream_compressed
, uint64_t *sizep
)
1479 struct calculate_send_arg size
= { 0 };
1481 ASSERT(dsl_pool_config_held(ds
->ds_dir
->dd_pool
));
1483 /* tosnap must be a snapshot */
1484 if (!dsl_dataset_is_snapshot(ds
))
1485 return (SET_ERROR(EINVAL
));
1487 /* verify that from_txg is before the provided snapshot was taken */
1488 if (from_txg
>= dsl_dataset_phys(ds
)->ds_creation_txg
) {
1489 return (SET_ERROR(EXDEV
));
1492 * traverse the blocks of the snapshot with birth times after
1493 * from_txg, summing their uncompressed size
1495 err
= traverse_dataset(ds
, from_txg
,
1496 TRAVERSE_POST
| TRAVERSE_NO_DECRYPT
,
1497 dmu_calculate_send_traversal
, &size
);
1502 err
= dmu_adjust_send_estimate_for_indirects(ds
, size
.uncompressed
,
1503 size
.compressed
, stream_compressed
, sizep
);
1507 typedef struct dmu_recv_begin_arg
{
1508 const char *drba_origin
;
1509 dmu_recv_cookie_t
*drba_cookie
;
1511 uint64_t drba_snapobj
;
1512 } dmu_recv_begin_arg_t
;
1515 recv_begin_check_existing_impl(dmu_recv_begin_arg_t
*drba
, dsl_dataset_t
*ds
,
1520 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
1522 /* temporary clone name must not exist */
1523 error
= zap_lookup(dp
->dp_meta_objset
,
1524 dsl_dir_phys(ds
->ds_dir
)->dd_child_dir_zapobj
, recv_clone_name
,
1526 if (error
!= ENOENT
)
1527 return (error
== 0 ? EBUSY
: error
);
1529 /* new snapshot name must not exist */
1530 error
= zap_lookup(dp
->dp_meta_objset
,
1531 dsl_dataset_phys(ds
)->ds_snapnames_zapobj
,
1532 drba
->drba_cookie
->drc_tosnap
, 8, 1, &val
);
1533 if (error
!= ENOENT
)
1534 return (error
== 0 ? EEXIST
: error
);
1537 * Check snapshot limit before receiving. We'll recheck again at the
1538 * end, but might as well abort before receiving if we're already over
1541 * Note that we do not check the file system limit with
1542 * dsl_dir_fscount_check because the temporary %clones don't count
1543 * against that limit.
1545 error
= dsl_fs_ss_limit_check(ds
->ds_dir
, 1, ZFS_PROP_SNAPSHOT_LIMIT
,
1546 NULL
, drba
->drba_cred
);
1550 if (fromguid
!= 0) {
1551 dsl_dataset_t
*snap
;
1552 uint64_t obj
= dsl_dataset_phys(ds
)->ds_prev_snap_obj
;
1554 /* Find snapshot in this dir that matches fromguid. */
1556 error
= dsl_dataset_hold_obj(dp
, obj
, FTAG
,
1559 return (SET_ERROR(ENODEV
));
1560 if (snap
->ds_dir
!= ds
->ds_dir
) {
1561 dsl_dataset_rele(snap
, FTAG
);
1562 return (SET_ERROR(ENODEV
));
1564 if (dsl_dataset_phys(snap
)->ds_guid
== fromguid
)
1566 obj
= dsl_dataset_phys(snap
)->ds_prev_snap_obj
;
1567 dsl_dataset_rele(snap
, FTAG
);
1570 return (SET_ERROR(ENODEV
));
1572 if (drba
->drba_cookie
->drc_force
) {
1573 drba
->drba_snapobj
= obj
;
1576 * If we are not forcing, there must be no
1577 * changes since fromsnap.
1579 if (dsl_dataset_modified_since_snap(ds
, snap
)) {
1580 dsl_dataset_rele(snap
, FTAG
);
1581 return (SET_ERROR(ETXTBSY
));
1583 drba
->drba_snapobj
= ds
->ds_prev
->ds_object
;
1586 dsl_dataset_rele(snap
, FTAG
);
1588 /* if full, then must be forced */
1589 if (!drba
->drba_cookie
->drc_force
)
1590 return (SET_ERROR(EEXIST
));
1593 * We don't support using zfs recv -F to blow away
1594 * encrypted filesystems. This would require the
1595 * dsl dir to point to the old encryption key and
1596 * the new one at the same time during the receive.
1598 if (ds
->ds_dir
->dd_crypto_obj
!= 0)
1599 return (SET_ERROR(EINVAL
));
1601 drba
->drba_snapobj
= 0;
1609 dmu_recv_begin_check(void *arg
, dmu_tx_t
*tx
)
1611 dmu_recv_begin_arg_t
*drba
= arg
;
1612 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1613 struct drr_begin
*drrb
= drba
->drba_cookie
->drc_drrb
;
1614 uint64_t fromguid
= drrb
->drr_fromguid
;
1615 int flags
= drrb
->drr_flags
;
1616 ds_hold_flags_t dsflags
= 0;
1618 uint64_t featureflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
1620 const char *tofs
= drba
->drba_cookie
->drc_tofs
;
1622 /* already checked */
1623 ASSERT3U(drrb
->drr_magic
, ==, DMU_BACKUP_MAGIC
);
1624 ASSERT(!(featureflags
& DMU_BACKUP_FEATURE_RESUMING
));
1626 if (DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) ==
1627 DMU_COMPOUNDSTREAM
||
1628 drrb
->drr_type
>= DMU_OST_NUMTYPES
||
1629 ((flags
& DRR_FLAG_CLONE
) && drba
->drba_origin
== NULL
))
1630 return (SET_ERROR(EINVAL
));
1632 /* Verify pool version supports SA if SA_SPILL feature set */
1633 if ((featureflags
& DMU_BACKUP_FEATURE_SA_SPILL
) &&
1634 spa_version(dp
->dp_spa
) < SPA_VERSION_SA
)
1635 return (SET_ERROR(ENOTSUP
));
1637 if (drba
->drba_cookie
->drc_resumable
&&
1638 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_EXTENSIBLE_DATASET
))
1639 return (SET_ERROR(ENOTSUP
));
1642 * The receiving code doesn't know how to translate a WRITE_EMBEDDED
1643 * record to a plain WRITE record, so the pool must have the
1644 * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
1645 * records. Same with WRITE_EMBEDDED records that use LZ4 compression.
1647 if ((featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA
) &&
1648 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_EMBEDDED_DATA
))
1649 return (SET_ERROR(ENOTSUP
));
1650 if ((featureflags
& DMU_BACKUP_FEATURE_LZ4
) &&
1651 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_LZ4_COMPRESS
))
1652 return (SET_ERROR(ENOTSUP
));
1655 * The receiving code doesn't know how to translate large blocks
1656 * to smaller ones, so the pool must have the LARGE_BLOCKS
1657 * feature enabled if the stream has LARGE_BLOCKS. Same with
1660 if ((featureflags
& DMU_BACKUP_FEATURE_LARGE_BLOCKS
) &&
1661 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_LARGE_BLOCKS
))
1662 return (SET_ERROR(ENOTSUP
));
1663 if ((featureflags
& DMU_BACKUP_FEATURE_LARGE_DNODE
) &&
1664 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_LARGE_DNODE
))
1665 return (SET_ERROR(ENOTSUP
));
1667 if ((featureflags
& DMU_BACKUP_FEATURE_RAW
)) {
1668 /* raw receives require the encryption feature */
1669 if (!spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_ENCRYPTION
))
1670 return (SET_ERROR(ENOTSUP
));
1672 dsflags
|= DS_HOLD_FLAG_DECRYPT
;
1675 error
= dsl_dataset_hold_flags(dp
, tofs
, dsflags
, FTAG
, &ds
);
1677 /* target fs already exists; recv into temp clone */
1679 /* Can't recv a clone into an existing fs */
1680 if (flags
& DRR_FLAG_CLONE
|| drba
->drba_origin
) {
1681 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
1682 return (SET_ERROR(EINVAL
));
1685 error
= recv_begin_check_existing_impl(drba
, ds
, fromguid
);
1686 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
1687 } else if (error
== ENOENT
) {
1688 /* target fs does not exist; must be a full backup or clone */
1689 char buf
[ZFS_MAX_DATASET_NAME_LEN
];
1692 * If it's a non-clone incremental, we are missing the
1693 * target fs, so fail the recv.
1695 if (fromguid
!= 0 && !(flags
& DRR_FLAG_CLONE
||
1697 return (SET_ERROR(ENOENT
));
1700 * If we're receiving a full send as a clone, and it doesn't
1701 * contain all the necessary free records and freeobject
1702 * records, reject it.
1704 if (fromguid
== 0 && drba
->drba_origin
&&
1705 !(flags
& DRR_FLAG_FREERECORDS
))
1706 return (SET_ERROR(EINVAL
));
1708 /* Open the parent of tofs */
1709 ASSERT3U(strlen(tofs
), <, sizeof (buf
));
1710 (void) strlcpy(buf
, tofs
, strrchr(tofs
, '/') - tofs
+ 1);
1711 error
= dsl_dataset_hold_flags(dp
, buf
, dsflags
, FTAG
, &ds
);
1716 * Check filesystem and snapshot limits before receiving. We'll
1717 * recheck snapshot limits again at the end (we create the
1718 * filesystems and increment those counts during begin_sync).
1720 error
= dsl_fs_ss_limit_check(ds
->ds_dir
, 1,
1721 ZFS_PROP_FILESYSTEM_LIMIT
, NULL
, drba
->drba_cred
);
1723 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
1727 error
= dsl_fs_ss_limit_check(ds
->ds_dir
, 1,
1728 ZFS_PROP_SNAPSHOT_LIMIT
, NULL
, drba
->drba_cred
);
1730 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
1734 if (drba
->drba_origin
!= NULL
) {
1735 dsl_dataset_t
*origin
;
1737 error
= dsl_dataset_hold_flags(dp
, drba
->drba_origin
,
1738 dsflags
, FTAG
, &origin
);
1740 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
1743 if (!origin
->ds_is_snapshot
) {
1744 dsl_dataset_rele_flags(origin
, dsflags
, FTAG
);
1745 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
1746 return (SET_ERROR(EINVAL
));
1748 if (dsl_dataset_phys(origin
)->ds_guid
!= fromguid
&&
1750 dsl_dataset_rele_flags(origin
, dsflags
, FTAG
);
1751 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
1752 return (SET_ERROR(ENODEV
));
1754 dsl_dataset_rele_flags(origin
,
1757 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
1764 dmu_recv_begin_sync(void *arg
, dmu_tx_t
*tx
)
1766 dmu_recv_begin_arg_t
*drba
= arg
;
1767 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1768 objset_t
*mos
= dp
->dp_meta_objset
;
1769 struct drr_begin
*drrb
= drba
->drba_cookie
->drc_drrb
;
1770 const char *tofs
= drba
->drba_cookie
->drc_tofs
;
1771 uint64_t featureflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
1772 dsl_dataset_t
*ds
, *newds
;
1775 ds_hold_flags_t dsflags
= 0;
1777 uint64_t crflags
= 0;
1778 dsl_crypto_params_t
*dcpp
= NULL
;
1779 dsl_crypto_params_t dcp
= { 0 };
1781 if (drrb
->drr_flags
& DRR_FLAG_CI_DATA
)
1782 crflags
|= DS_FLAG_CI_DATASET
;
1783 if ((featureflags
& DMU_BACKUP_FEATURE_RAW
) == 0) {
1784 dsflags
|= DS_HOLD_FLAG_DECRYPT
;
1786 dcp
.cp_cmd
= DCP_CMD_RAW_RECV
;
1789 error
= dsl_dataset_hold_flags(dp
, tofs
, dsflags
, FTAG
, &ds
);
1791 /* create temporary clone */
1792 dsl_dataset_t
*snap
= NULL
;
1794 if (drba
->drba_snapobj
!= 0) {
1795 VERIFY0(dsl_dataset_hold_obj(dp
,
1796 drba
->drba_snapobj
, FTAG
, &snap
));
1798 /* we use the dcp whenever we are not making a clone */
1802 dsobj
= dsl_dataset_create_sync(ds
->ds_dir
, recv_clone_name
,
1803 snap
, crflags
, drba
->drba_cred
, dcpp
, tx
);
1804 if (drba
->drba_snapobj
!= 0)
1805 dsl_dataset_rele(snap
, FTAG
);
1806 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
1810 dsl_dataset_t
*origin
= NULL
;
1812 VERIFY0(dsl_dir_hold(dp
, tofs
, FTAG
, &dd
, &tail
));
1814 if (drba
->drba_origin
!= NULL
) {
1815 VERIFY0(dsl_dataset_hold(dp
, drba
->drba_origin
,
1818 /* we use the dcp whenever we are not making a clone */
1822 /* Create new dataset. */
1823 dsobj
= dsl_dataset_create_sync(dd
, strrchr(tofs
, '/') + 1,
1824 origin
, crflags
, drba
->drba_cred
, dcpp
, tx
);
1826 dsl_dataset_rele(origin
, FTAG
);
1827 dsl_dir_rele(dd
, FTAG
);
1828 drba
->drba_cookie
->drc_newfs
= B_TRUE
;
1830 VERIFY0(dsl_dataset_own_obj(dp
, dsobj
, dsflags
, dmu_recv_tag
, &newds
));
1831 VERIFY0(dmu_objset_from_ds(newds
, &os
));
1833 if (drba
->drba_cookie
->drc_resumable
) {
1837 dsl_dataset_zapify(newds
, tx
);
1838 if (drrb
->drr_fromguid
!= 0) {
1839 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_FROMGUID
,
1840 8, 1, &drrb
->drr_fromguid
, tx
));
1842 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_TOGUID
,
1843 8, 1, &drrb
->drr_toguid
, tx
));
1844 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_TONAME
,
1845 1, strlen(drrb
->drr_toname
) + 1, drrb
->drr_toname
, tx
));
1846 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_OBJECT
,
1848 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_OFFSET
,
1850 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_BYTES
,
1852 if (featureflags
& DMU_BACKUP_FEATURE_LARGE_BLOCKS
) {
1853 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_LARGEBLOCK
,
1856 if (featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA
) {
1857 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_EMBEDOK
,
1860 if (featureflags
& DMU_BACKUP_FEATURE_COMPRESSED
) {
1861 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_COMPRESSOK
,
1864 if (featureflags
& DMU_BACKUP_FEATURE_RAW
) {
1865 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_RAWOK
,
1871 * Usually the os->os_encrypted value is tied to the presence of a
1872 * DSL Crypto Key object in the dd. However, that will not be received
1873 * until dmu_recv_stream(), so we set the value manually for now.
1875 if (featureflags
& DMU_BACKUP_FEATURE_RAW
) {
1876 os
->os_encrypted
= B_TRUE
;
1877 drba
->drba_cookie
->drc_raw
= B_TRUE
;
1880 dmu_buf_will_dirty(newds
->ds_dbuf
, tx
);
1881 dsl_dataset_phys(newds
)->ds_flags
|= DS_FLAG_INCONSISTENT
;
1884 * If we actually created a non-clone, we need to create the objset
1885 * in our new dataset. If this is a raw send we postpone this until
1886 * dmu_recv_stream() so that we can allocate the metadnode with the
1887 * properties from the DRR_BEGIN payload.
1889 rrw_enter(&newds
->ds_bp_rwlock
, RW_READER
, FTAG
);
1890 if (BP_IS_HOLE(dsl_dataset_get_blkptr(newds
)) &&
1891 (featureflags
& DMU_BACKUP_FEATURE_RAW
) == 0) {
1892 (void) dmu_objset_create_impl(dp
->dp_spa
,
1893 newds
, dsl_dataset_get_blkptr(newds
), drrb
->drr_type
, tx
);
1895 rrw_exit(&newds
->ds_bp_rwlock
, FTAG
);
1897 drba
->drba_cookie
->drc_ds
= newds
;
1899 spa_history_log_internal_ds(newds
, "receive", tx
, "");
1903 dmu_recv_resume_begin_check(void *arg
, dmu_tx_t
*tx
)
1905 dmu_recv_begin_arg_t
*drba
= arg
;
1906 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1907 struct drr_begin
*drrb
= drba
->drba_cookie
->drc_drrb
;
1909 ds_hold_flags_t dsflags
= 0;
1910 uint64_t featureflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
1912 const char *tofs
= drba
->drba_cookie
->drc_tofs
;
1915 /* 6 extra bytes for /%recv */
1916 char recvname
[ZFS_MAX_DATASET_NAME_LEN
+ 6];
1918 /* already checked */
1919 ASSERT3U(drrb
->drr_magic
, ==, DMU_BACKUP_MAGIC
);
1920 ASSERT(featureflags
& DMU_BACKUP_FEATURE_RESUMING
);
1922 if (DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) ==
1923 DMU_COMPOUNDSTREAM
||
1924 drrb
->drr_type
>= DMU_OST_NUMTYPES
)
1925 return (SET_ERROR(EINVAL
));
1927 /* Verify pool version supports SA if SA_SPILL feature set */
1928 if ((featureflags
& DMU_BACKUP_FEATURE_SA_SPILL
) &&
1929 spa_version(dp
->dp_spa
) < SPA_VERSION_SA
)
1930 return (SET_ERROR(ENOTSUP
));
1933 * The receiving code doesn't know how to translate a WRITE_EMBEDDED
1934 * record to a plain WRITE record, so the pool must have the
1935 * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
1936 * records. Same with WRITE_EMBEDDED records that use LZ4 compression.
1938 if ((featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA
) &&
1939 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_EMBEDDED_DATA
))
1940 return (SET_ERROR(ENOTSUP
));
1941 if ((featureflags
& DMU_BACKUP_FEATURE_LZ4
) &&
1942 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_LZ4_COMPRESS
))
1943 return (SET_ERROR(ENOTSUP
));
1946 * The receiving code doesn't know how to translate large blocks
1947 * to smaller ones, so the pool must have the LARGE_BLOCKS
1948 * feature enabled if the stream has LARGE_BLOCKS. Same with
1951 if ((featureflags
& DMU_BACKUP_FEATURE_LARGE_BLOCKS
) &&
1952 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_LARGE_BLOCKS
))
1953 return (SET_ERROR(ENOTSUP
));
1954 if ((featureflags
& DMU_BACKUP_FEATURE_LARGE_DNODE
) &&
1955 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_LARGE_DNODE
))
1956 return (SET_ERROR(ENOTSUP
));
1958 (void) snprintf(recvname
, sizeof (recvname
), "%s/%s",
1959 tofs
, recv_clone_name
);
1961 if ((featureflags
& DMU_BACKUP_FEATURE_RAW
) == 0)
1962 dsflags
|= DS_HOLD_FLAG_DECRYPT
;
1964 if (dsl_dataset_hold_flags(dp
, recvname
, dsflags
, FTAG
, &ds
) != 0) {
1965 /* %recv does not exist; continue in tofs */
1966 error
= dsl_dataset_hold_flags(dp
, tofs
, dsflags
, FTAG
, &ds
);
1971 /* check that ds is marked inconsistent */
1972 if (!DS_IS_INCONSISTENT(ds
)) {
1973 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
1974 return (SET_ERROR(EINVAL
));
1977 /* check that there is resuming data, and that the toguid matches */
1978 if (!dsl_dataset_is_zapified(ds
)) {
1979 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
1980 return (SET_ERROR(EINVAL
));
1982 error
= zap_lookup(dp
->dp_meta_objset
, ds
->ds_object
,
1983 DS_FIELD_RESUME_TOGUID
, sizeof (val
), 1, &val
);
1984 if (error
!= 0 || drrb
->drr_toguid
!= val
) {
1985 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
1986 return (SET_ERROR(EINVAL
));
1990 * Check if the receive is still running. If so, it will be owned.
1991 * Note that nothing else can own the dataset (e.g. after the receive
1992 * fails) because it will be marked inconsistent.
1994 if (dsl_dataset_has_owner(ds
)) {
1995 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
1996 return (SET_ERROR(EBUSY
));
1999 /* There should not be any snapshots of this fs yet. */
2000 if (ds
->ds_prev
!= NULL
&& ds
->ds_prev
->ds_dir
== ds
->ds_dir
) {
2001 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
2002 return (SET_ERROR(EINVAL
));
2006 * Note: resume point will be checked when we process the first WRITE
2010 /* check that the origin matches */
2012 (void) zap_lookup(dp
->dp_meta_objset
, ds
->ds_object
,
2013 DS_FIELD_RESUME_FROMGUID
, sizeof (val
), 1, &val
);
2014 if (drrb
->drr_fromguid
!= val
) {
2015 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
2016 return (SET_ERROR(EINVAL
));
2019 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
2024 dmu_recv_resume_begin_sync(void *arg
, dmu_tx_t
*tx
)
2026 dmu_recv_begin_arg_t
*drba
= arg
;
2027 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
2028 const char *tofs
= drba
->drba_cookie
->drc_tofs
;
2029 struct drr_begin
*drrb
= drba
->drba_cookie
->drc_drrb
;
2030 uint64_t featureflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
2033 ds_hold_flags_t dsflags
= 0;
2035 /* 6 extra bytes for /%recv */
2036 char recvname
[ZFS_MAX_DATASET_NAME_LEN
+ 6];
2038 (void) snprintf(recvname
, sizeof (recvname
), "%s/%s",
2039 tofs
, recv_clone_name
);
2041 if (featureflags
& DMU_BACKUP_FEATURE_RAW
) {
2042 drba
->drba_cookie
->drc_raw
= B_TRUE
;
2044 dsflags
|= DS_HOLD_FLAG_DECRYPT
;
2047 if (dsl_dataset_hold_flags(dp
, recvname
, dsflags
, FTAG
, &ds
) != 0) {
2048 /* %recv does not exist; continue in tofs */
2049 VERIFY0(dsl_dataset_hold_flags(dp
, tofs
, dsflags
, FTAG
, &ds
));
2050 drba
->drba_cookie
->drc_newfs
= B_TRUE
;
2053 /* clear the inconsistent flag so that we can own it */
2054 ASSERT(DS_IS_INCONSISTENT(ds
));
2055 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
2056 dsl_dataset_phys(ds
)->ds_flags
&= ~DS_FLAG_INCONSISTENT
;
2057 dsobj
= ds
->ds_object
;
2058 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
2060 VERIFY0(dsl_dataset_own_obj(dp
, dsobj
, dsflags
, dmu_recv_tag
, &ds
));
2061 VERIFY0(dmu_objset_from_ds(ds
, &os
));
2063 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
2064 dsl_dataset_phys(ds
)->ds_flags
|= DS_FLAG_INCONSISTENT
;
2066 rrw_enter(&ds
->ds_bp_rwlock
, RW_READER
, FTAG
);
2067 ASSERT(!BP_IS_HOLE(dsl_dataset_get_blkptr(ds
)));
2068 rrw_exit(&ds
->ds_bp_rwlock
, FTAG
);
2070 drba
->drba_cookie
->drc_ds
= ds
;
2072 spa_history_log_internal_ds(ds
, "resume receive", tx
, "");
2076 * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
2077 * succeeds; otherwise we will leak the holds on the datasets.
2080 dmu_recv_begin(char *tofs
, char *tosnap
, dmu_replay_record_t
*drr_begin
,
2081 boolean_t force
, boolean_t resumable
, char *origin
, dmu_recv_cookie_t
*drc
)
2083 dmu_recv_begin_arg_t drba
= { 0 };
2085 bzero(drc
, sizeof (dmu_recv_cookie_t
));
2086 drc
->drc_drr_begin
= drr_begin
;
2087 drc
->drc_drrb
= &drr_begin
->drr_u
.drr_begin
;
2088 drc
->drc_tosnap
= tosnap
;
2089 drc
->drc_tofs
= tofs
;
2090 drc
->drc_force
= force
;
2091 drc
->drc_resumable
= resumable
;
2092 drc
->drc_cred
= CRED();
2093 drc
->drc_clone
= (origin
!= NULL
);
2095 if (drc
->drc_drrb
->drr_magic
== BSWAP_64(DMU_BACKUP_MAGIC
)) {
2096 drc
->drc_byteswap
= B_TRUE
;
2097 (void) fletcher_4_incremental_byteswap(drr_begin
,
2098 sizeof (dmu_replay_record_t
), &drc
->drc_cksum
);
2099 byteswap_record(drr_begin
);
2100 } else if (drc
->drc_drrb
->drr_magic
== DMU_BACKUP_MAGIC
) {
2101 (void) fletcher_4_incremental_native(drr_begin
,
2102 sizeof (dmu_replay_record_t
), &drc
->drc_cksum
);
2104 return (SET_ERROR(EINVAL
));
2107 drba
.drba_origin
= origin
;
2108 drba
.drba_cookie
= drc
;
2109 drba
.drba_cred
= CRED();
2111 if (DMU_GET_FEATUREFLAGS(drc
->drc_drrb
->drr_versioninfo
) &
2112 DMU_BACKUP_FEATURE_RESUMING
) {
2113 return (dsl_sync_task(tofs
,
2114 dmu_recv_resume_begin_check
, dmu_recv_resume_begin_sync
,
2115 &drba
, 5, ZFS_SPACE_CHECK_NORMAL
));
2117 return (dsl_sync_task(tofs
,
2118 dmu_recv_begin_check
, dmu_recv_begin_sync
,
2119 &drba
, 5, ZFS_SPACE_CHECK_NORMAL
));
2123 struct receive_record_arg
{
2124 dmu_replay_record_t header
;
2125 void *payload
; /* Pointer to a buffer containing the payload */
2127 * If the record is a write, pointer to the arc_buf_t containing the
2132 uint64_t bytes_read
; /* bytes read from stream when record created */
2133 boolean_t eos_marker
; /* Marks the end of the stream */
2137 struct receive_writer_arg
{
2143 * These three args are used to signal to the main thread that we're
2151 /* A map from guid to dataset to help handle dedup'd streams. */
2152 avl_tree_t
*guid_to_ds_map
;
2153 boolean_t resumable
;
2155 uint64_t last_object
;
2156 uint64_t last_offset
;
2157 uint64_t max_object
; /* highest object ID referenced in stream */
2158 uint64_t bytes_read
; /* bytes read when current record created */
2162 list_t list
; /* List of struct receive_objnode. */
2164 * Last object looked up. Used to assert that objects are being looked
2165 * up in ascending order.
2167 uint64_t last_lookup
;
2170 struct receive_objnode
{
2175 struct receive_arg
{
2177 vnode_t
*vp
; /* The vnode to read the stream from */
2178 uint64_t voff
; /* The current offset in the stream */
2179 uint64_t bytes_read
;
2181 * A record that has had its payload read in, but hasn't yet been handed
2182 * off to the worker thread.
2184 struct receive_record_arg
*rrd
;
2185 /* A record that has had its header read in, but not its payload. */
2186 struct receive_record_arg
*next_rrd
;
2188 zio_cksum_t prev_cksum
;
2192 uint64_t featureflags
;
2193 /* Sorted list of objects not to issue prefetches for. */
2194 struct objlist ignore_objlist
;
2197 typedef struct guid_map_entry
{
2200 dsl_dataset_t
*gme_ds
;
2205 guid_compare(const void *arg1
, const void *arg2
)
2207 const guid_map_entry_t
*gmep1
= (const guid_map_entry_t
*)arg1
;
2208 const guid_map_entry_t
*gmep2
= (const guid_map_entry_t
*)arg2
;
2210 return (AVL_CMP(gmep1
->guid
, gmep2
->guid
));
2214 free_guid_map_onexit(void *arg
)
2216 avl_tree_t
*ca
= arg
;
2217 void *cookie
= NULL
;
2218 guid_map_entry_t
*gmep
;
2220 while ((gmep
= avl_destroy_nodes(ca
, &cookie
)) != NULL
) {
2221 dsl_dataset_long_rele(gmep
->gme_ds
, gmep
);
2222 dsl_dataset_rele_flags(gmep
->gme_ds
,
2223 (gmep
->raw
) ? 0 : DS_HOLD_FLAG_DECRYPT
, gmep
);
2224 kmem_free(gmep
, sizeof (guid_map_entry_t
));
2227 kmem_free(ca
, sizeof (avl_tree_t
));
2231 receive_read(struct receive_arg
*ra
, int len
, void *buf
)
2236 * The code doesn't rely on this (lengths being multiples of 8). See
2237 * comment in dump_bytes.
2239 ASSERT(len
% 8 == 0 ||
2240 (ra
->featureflags
& DMU_BACKUP_FEATURE_RAW
) != 0);
2242 while (done
< len
) {
2245 ra
->err
= vn_rdwr(UIO_READ
, ra
->vp
,
2246 (char *)buf
+ done
, len
- done
,
2247 ra
->voff
, UIO_SYSSPACE
, FAPPEND
,
2248 RLIM64_INFINITY
, CRED(), &resid
);
2250 if (resid
== len
- done
) {
2252 * Note: ECKSUM indicates that the receive
2253 * was interrupted and can potentially be resumed.
2255 ra
->err
= SET_ERROR(ECKSUM
);
2257 ra
->voff
+= len
- done
- resid
;
2263 ra
->bytes_read
+= len
;
2265 ASSERT3U(done
, ==, len
);
2269 noinline
static void
2270 byteswap_record(dmu_replay_record_t
*drr
)
2272 #define DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
2273 #define DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
2274 drr
->drr_type
= BSWAP_32(drr
->drr_type
);
2275 drr
->drr_payloadlen
= BSWAP_32(drr
->drr_payloadlen
);
2277 switch (drr
->drr_type
) {
2279 DO64(drr_begin
.drr_magic
);
2280 DO64(drr_begin
.drr_versioninfo
);
2281 DO64(drr_begin
.drr_creation_time
);
2282 DO32(drr_begin
.drr_type
);
2283 DO32(drr_begin
.drr_flags
);
2284 DO64(drr_begin
.drr_toguid
);
2285 DO64(drr_begin
.drr_fromguid
);
2288 DO64(drr_object
.drr_object
);
2289 DO32(drr_object
.drr_type
);
2290 DO32(drr_object
.drr_bonustype
);
2291 DO32(drr_object
.drr_blksz
);
2292 DO32(drr_object
.drr_bonuslen
);
2293 DO32(drr_object
.drr_raw_bonuslen
);
2294 DO64(drr_object
.drr_toguid
);
2296 case DRR_FREEOBJECTS
:
2297 DO64(drr_freeobjects
.drr_firstobj
);
2298 DO64(drr_freeobjects
.drr_numobjs
);
2299 DO64(drr_freeobjects
.drr_toguid
);
2302 DO64(drr_write
.drr_object
);
2303 DO32(drr_write
.drr_type
);
2304 DO64(drr_write
.drr_offset
);
2305 DO64(drr_write
.drr_logical_size
);
2306 DO64(drr_write
.drr_toguid
);
2307 ZIO_CHECKSUM_BSWAP(&drr
->drr_u
.drr_write
.drr_key
.ddk_cksum
);
2308 DO64(drr_write
.drr_key
.ddk_prop
);
2309 DO64(drr_write
.drr_compressed_size
);
2311 case DRR_WRITE_BYREF
:
2312 DO64(drr_write_byref
.drr_object
);
2313 DO64(drr_write_byref
.drr_offset
);
2314 DO64(drr_write_byref
.drr_length
);
2315 DO64(drr_write_byref
.drr_toguid
);
2316 DO64(drr_write_byref
.drr_refguid
);
2317 DO64(drr_write_byref
.drr_refobject
);
2318 DO64(drr_write_byref
.drr_refoffset
);
2319 ZIO_CHECKSUM_BSWAP(&drr
->drr_u
.drr_write_byref
.
2321 DO64(drr_write_byref
.drr_key
.ddk_prop
);
2323 case DRR_WRITE_EMBEDDED
:
2324 DO64(drr_write_embedded
.drr_object
);
2325 DO64(drr_write_embedded
.drr_offset
);
2326 DO64(drr_write_embedded
.drr_length
);
2327 DO64(drr_write_embedded
.drr_toguid
);
2328 DO32(drr_write_embedded
.drr_lsize
);
2329 DO32(drr_write_embedded
.drr_psize
);
2332 DO64(drr_free
.drr_object
);
2333 DO64(drr_free
.drr_offset
);
2334 DO64(drr_free
.drr_length
);
2335 DO64(drr_free
.drr_toguid
);
2338 DO64(drr_spill
.drr_object
);
2339 DO64(drr_spill
.drr_length
);
2340 DO64(drr_spill
.drr_toguid
);
2341 DO64(drr_spill
.drr_compressed_size
);
2342 DO32(drr_spill
.drr_type
);
2344 case DRR_OBJECT_RANGE
:
2345 DO64(drr_object_range
.drr_firstobj
);
2346 DO64(drr_object_range
.drr_numslots
);
2347 DO64(drr_object_range
.drr_toguid
);
2350 DO64(drr_end
.drr_toguid
);
2351 ZIO_CHECKSUM_BSWAP(&drr
->drr_u
.drr_end
.drr_checksum
);
2357 if (drr
->drr_type
!= DRR_BEGIN
) {
2358 ZIO_CHECKSUM_BSWAP(&drr
->drr_u
.drr_checksum
.drr_checksum
);
2365 static inline uint8_t
2366 deduce_nblkptr(dmu_object_type_t bonus_type
, uint64_t bonus_size
)
2368 if (bonus_type
== DMU_OT_SA
) {
2372 ((DN_OLD_MAX_BONUSLEN
-
2373 MIN(DN_OLD_MAX_BONUSLEN
, bonus_size
)) >> SPA_BLKPTRSHIFT
));
2378 save_resume_state(struct receive_writer_arg
*rwa
,
2379 uint64_t object
, uint64_t offset
, dmu_tx_t
*tx
)
2381 int txgoff
= dmu_tx_get_txg(tx
) & TXG_MASK
;
2383 if (!rwa
->resumable
)
2387 * We use ds_resume_bytes[] != 0 to indicate that we need to
2388 * update this on disk, so it must not be 0.
2390 ASSERT(rwa
->bytes_read
!= 0);
2393 * We only resume from write records, which have a valid
2394 * (non-meta-dnode) object number.
2396 ASSERT(object
!= 0);
2399 * For resuming to work correctly, we must receive records in order,
2400 * sorted by object,offset. This is checked by the callers, but
2401 * assert it here for good measure.
2403 ASSERT3U(object
, >=, rwa
->os
->os_dsl_dataset
->ds_resume_object
[txgoff
]);
2404 ASSERT(object
!= rwa
->os
->os_dsl_dataset
->ds_resume_object
[txgoff
] ||
2405 offset
>= rwa
->os
->os_dsl_dataset
->ds_resume_offset
[txgoff
]);
2406 ASSERT3U(rwa
->bytes_read
, >=,
2407 rwa
->os
->os_dsl_dataset
->ds_resume_bytes
[txgoff
]);
2409 rwa
->os
->os_dsl_dataset
->ds_resume_object
[txgoff
] = object
;
2410 rwa
->os
->os_dsl_dataset
->ds_resume_offset
[txgoff
] = offset
;
2411 rwa
->os
->os_dsl_dataset
->ds_resume_bytes
[txgoff
] = rwa
->bytes_read
;
2415 receive_object(struct receive_writer_arg
*rwa
, struct drr_object
*drro
,
2418 dmu_object_info_t doi
;
2423 if (drro
->drr_type
== DMU_OT_NONE
||
2424 !DMU_OT_IS_VALID(drro
->drr_type
) ||
2425 !DMU_OT_IS_VALID(drro
->drr_bonustype
) ||
2426 drro
->drr_checksumtype
>= ZIO_CHECKSUM_FUNCTIONS
||
2427 drro
->drr_compress
>= ZIO_COMPRESS_FUNCTIONS
||
2428 P2PHASE(drro
->drr_blksz
, SPA_MINBLOCKSIZE
) ||
2429 drro
->drr_blksz
< SPA_MINBLOCKSIZE
||
2430 drro
->drr_blksz
> spa_maxblocksize(dmu_objset_spa(rwa
->os
)) ||
2431 drro
->drr_bonuslen
>
2432 DN_BONUS_SIZE(spa_maxdnodesize(dmu_objset_spa(rwa
->os
))) ||
2433 drro
->drr_dn_slots
>
2434 (spa_maxdnodesize(dmu_objset_spa(rwa
->os
)) >> DNODE_SHIFT
)) {
2435 return (SET_ERROR(EINVAL
));
2439 if (drro
->drr_raw_bonuslen
< drro
->drr_bonuslen
||
2440 drro
->drr_indblkshift
> SPA_MAXBLOCKSHIFT
||
2441 drro
->drr_nlevels
> DN_MAX_LEVELS
||
2442 drro
->drr_nblkptr
> DN_MAX_NBLKPTR
||
2443 DN_SLOTS_TO_BONUSLEN(drro
->drr_dn_slots
) <
2444 drro
->drr_raw_bonuslen
)
2445 return (SET_ERROR(EINVAL
));
2447 if (drro
->drr_flags
!= 0 || drro
->drr_raw_bonuslen
!= 0 ||
2448 drro
->drr_indblkshift
!= 0 || drro
->drr_nlevels
!= 0 ||
2449 drro
->drr_nblkptr
!= 0)
2450 return (SET_ERROR(EINVAL
));
2453 err
= dmu_object_info(rwa
->os
, drro
->drr_object
, &doi
);
2455 if (err
!= 0 && err
!= ENOENT
)
2456 return (SET_ERROR(EINVAL
));
2457 object
= err
== 0 ? drro
->drr_object
: DMU_NEW_OBJECT
;
2459 if (drro
->drr_object
> rwa
->max_object
)
2460 rwa
->max_object
= drro
->drr_object
;
2463 * If we are losing blkptrs or changing the block size this must
2464 * be a new file instance. We must clear out the previous file
2465 * contents before we can change this type of metadata in the dnode.
2466 * Raw receives will also check that the indirect structure of the
2467 * dnode hasn't changed.
2470 uint32_t indblksz
= drro
->drr_indblkshift
?
2471 1ULL << drro
->drr_indblkshift
: 0;
2472 int nblkptr
= deduce_nblkptr(drro
->drr_bonustype
,
2473 drro
->drr_bonuslen
);
2475 /* nblkptr will be bounded by the bonus size and type */
2476 if (rwa
->raw
&& nblkptr
!= drro
->drr_nblkptr
)
2477 return (SET_ERROR(EINVAL
));
2479 if (drro
->drr_blksz
!= doi
.doi_data_block_size
||
2480 nblkptr
< doi
.doi_nblkptr
||
2482 (indblksz
!= doi
.doi_metadata_block_size
||
2483 drro
->drr_nlevels
< doi
.doi_indirection
))) {
2484 err
= dmu_free_long_range(rwa
->os
, drro
->drr_object
,
2487 return (SET_ERROR(EINVAL
));
2491 tx
= dmu_tx_create(rwa
->os
);
2492 dmu_tx_hold_bonus(tx
, object
);
2493 dmu_tx_hold_write(tx
, object
, 0, 0);
2494 err
= dmu_tx_assign(tx
, TXG_WAIT
);
2500 if (object
== DMU_NEW_OBJECT
) {
2501 /* currently free, want to be allocated */
2502 err
= dmu_object_claim_dnsize(rwa
->os
, drro
->drr_object
,
2503 drro
->drr_type
, drro
->drr_blksz
,
2504 drro
->drr_bonustype
, drro
->drr_bonuslen
,
2505 drro
->drr_dn_slots
<< DNODE_SHIFT
, tx
);
2506 } else if (drro
->drr_type
!= doi
.doi_type
||
2507 drro
->drr_blksz
!= doi
.doi_data_block_size
||
2508 drro
->drr_bonustype
!= doi
.doi_bonus_type
||
2509 drro
->drr_bonuslen
!= doi
.doi_bonus_size
) {
2510 /* currently allocated, but with different properties */
2511 err
= dmu_object_reclaim(rwa
->os
, drro
->drr_object
,
2512 drro
->drr_type
, drro
->drr_blksz
,
2513 drro
->drr_bonustype
, drro
->drr_bonuslen
, tx
);
2517 return (SET_ERROR(EINVAL
));
2521 VERIFY0(dmu_object_dirty_raw(rwa
->os
, drro
->drr_object
, tx
));
2523 dmu_object_set_checksum(rwa
->os
, drro
->drr_object
,
2524 drro
->drr_checksumtype
, tx
);
2525 dmu_object_set_compress(rwa
->os
, drro
->drr_object
,
2526 drro
->drr_compress
, tx
);
2528 /* handle more restrictive dnode structuring for raw recvs */
2531 * Set the indirect block shift and nlevels. This will not fail
2532 * because we ensured all of the blocks were free earlier if
2533 * this is a new object.
2535 VERIFY0(dmu_object_set_blocksize(rwa
->os
, drro
->drr_object
,
2536 drro
->drr_blksz
, drro
->drr_indblkshift
, tx
));
2537 VERIFY0(dmu_object_set_nlevels(rwa
->os
, drro
->drr_object
,
2538 drro
->drr_nlevels
, tx
));
2543 uint32_t flags
= DMU_READ_NO_PREFETCH
;
2546 flags
|= DMU_READ_NO_DECRYPT
;
2548 VERIFY0(dmu_bonus_hold_impl(rwa
->os
, drro
->drr_object
,
2550 dmu_buf_will_dirty(db
, tx
);
2552 ASSERT3U(db
->db_size
, >=, drro
->drr_bonuslen
);
2553 bcopy(data
, db
->db_data
, DRR_OBJECT_PAYLOAD_SIZE(drro
));
2556 * Raw bonus buffers have their byteorder determined by the
2557 * DRR_OBJECT_RANGE record.
2559 if (rwa
->byteswap
&& !rwa
->raw
) {
2560 dmu_object_byteswap_t byteswap
=
2561 DMU_OT_BYTESWAP(drro
->drr_bonustype
);
2562 dmu_ot_byteswap
[byteswap
].ob_func(db
->db_data
,
2563 DRR_OBJECT_PAYLOAD_SIZE(drro
));
2565 dmu_buf_rele(db
, FTAG
);
2574 receive_freeobjects(struct receive_writer_arg
*rwa
,
2575 struct drr_freeobjects
*drrfo
)
2580 if (drrfo
->drr_firstobj
+ drrfo
->drr_numobjs
< drrfo
->drr_firstobj
)
2581 return (SET_ERROR(EINVAL
));
2583 for (obj
= drrfo
->drr_firstobj
== 0 ? 1 : drrfo
->drr_firstobj
;
2584 obj
< drrfo
->drr_firstobj
+ drrfo
->drr_numobjs
&& next_err
== 0;
2585 next_err
= dmu_object_next(rwa
->os
, &obj
, FALSE
, 0)) {
2586 dmu_object_info_t doi
;
2589 err
= dmu_object_info(rwa
->os
, obj
, &doi
);
2596 err
= dmu_free_long_object_raw(rwa
->os
, obj
);
2598 err
= dmu_free_long_object(rwa
->os
, obj
);
2603 if (obj
> rwa
->max_object
)
2604 rwa
->max_object
= obj
;
2606 if (next_err
!= ESRCH
)
2612 receive_write(struct receive_writer_arg
*rwa
, struct drr_write
*drrw
,
2619 if (drrw
->drr_offset
+ drrw
->drr_logical_size
< drrw
->drr_offset
||
2620 !DMU_OT_IS_VALID(drrw
->drr_type
))
2621 return (SET_ERROR(EINVAL
));
2624 * For resuming to work, records must be in increasing order
2625 * by (object, offset).
2627 if (drrw
->drr_object
< rwa
->last_object
||
2628 (drrw
->drr_object
== rwa
->last_object
&&
2629 drrw
->drr_offset
< rwa
->last_offset
)) {
2630 return (SET_ERROR(EINVAL
));
2632 rwa
->last_object
= drrw
->drr_object
;
2633 rwa
->last_offset
= drrw
->drr_offset
;
2635 if (rwa
->last_object
> rwa
->max_object
)
2636 rwa
->max_object
= rwa
->last_object
;
2638 if (dmu_object_info(rwa
->os
, drrw
->drr_object
, NULL
) != 0)
2639 return (SET_ERROR(EINVAL
));
2641 tx
= dmu_tx_create(rwa
->os
);
2642 dmu_tx_hold_write(tx
, drrw
->drr_object
,
2643 drrw
->drr_offset
, drrw
->drr_logical_size
);
2644 err
= dmu_tx_assign(tx
, TXG_WAIT
);
2651 VERIFY0(dmu_object_dirty_raw(rwa
->os
, drrw
->drr_object
, tx
));
2653 if (rwa
->byteswap
&& !arc_is_encrypted(abuf
) &&
2654 arc_get_compression(abuf
) == ZIO_COMPRESS_OFF
) {
2655 dmu_object_byteswap_t byteswap
=
2656 DMU_OT_BYTESWAP(drrw
->drr_type
);
2657 dmu_ot_byteswap
[byteswap
].ob_func(abuf
->b_data
,
2658 DRR_WRITE_PAYLOAD_SIZE(drrw
));
2661 VERIFY0(dnode_hold(rwa
->os
, drrw
->drr_object
, FTAG
, &dn
));
2662 dmu_assign_arcbuf_by_dnode(dn
, drrw
->drr_offset
, abuf
, tx
);
2663 dnode_rele(dn
, FTAG
);
2666 * Note: If the receive fails, we want the resume stream to start
2667 * with the same record that we last successfully received (as opposed
2668 * to the next record), so that we can verify that we are
2669 * resuming from the correct location.
2671 save_resume_state(rwa
, drrw
->drr_object
, drrw
->drr_offset
, tx
);
2678 * Handle a DRR_WRITE_BYREF record. This record is used in dedup'ed
2679 * streams to refer to a copy of the data that is already on the
2680 * system because it came in earlier in the stream. This function
2681 * finds the earlier copy of the data, and uses that copy instead of
2682 * data from the stream to fulfill this write.
2685 receive_write_byref(struct receive_writer_arg
*rwa
,
2686 struct drr_write_byref
*drrwbr
)
2690 guid_map_entry_t gmesrch
;
2691 guid_map_entry_t
*gmep
;
2693 objset_t
*ref_os
= NULL
;
2694 int flags
= DMU_READ_PREFETCH
;
2697 if (drrwbr
->drr_offset
+ drrwbr
->drr_length
< drrwbr
->drr_offset
)
2698 return (SET_ERROR(EINVAL
));
2701 * If the GUID of the referenced dataset is different from the
2702 * GUID of the target dataset, find the referenced dataset.
2704 if (drrwbr
->drr_toguid
!= drrwbr
->drr_refguid
) {
2705 gmesrch
.guid
= drrwbr
->drr_refguid
;
2706 if ((gmep
= avl_find(rwa
->guid_to_ds_map
, &gmesrch
,
2708 return (SET_ERROR(EINVAL
));
2710 if (dmu_objset_from_ds(gmep
->gme_ds
, &ref_os
))
2711 return (SET_ERROR(EINVAL
));
2716 if (drrwbr
->drr_object
> rwa
->max_object
)
2717 rwa
->max_object
= drrwbr
->drr_object
;
2720 flags
|= DMU_READ_NO_DECRYPT
;
2722 /* may return either a regular db or an encrypted one */
2723 err
= dmu_buf_hold(ref_os
, drrwbr
->drr_refobject
,
2724 drrwbr
->drr_refoffset
, FTAG
, &dbp
, flags
);
2728 tx
= dmu_tx_create(rwa
->os
);
2730 dmu_tx_hold_write(tx
, drrwbr
->drr_object
,
2731 drrwbr
->drr_offset
, drrwbr
->drr_length
);
2732 err
= dmu_tx_assign(tx
, TXG_WAIT
);
2739 VERIFY0(dmu_object_dirty_raw(rwa
->os
, drrwbr
->drr_object
, tx
));
2740 dmu_copy_from_buf(rwa
->os
, drrwbr
->drr_object
,
2741 drrwbr
->drr_offset
, dbp
, tx
);
2743 dmu_write(rwa
->os
, drrwbr
->drr_object
,
2744 drrwbr
->drr_offset
, drrwbr
->drr_length
, dbp
->db_data
, tx
);
2746 dmu_buf_rele(dbp
, FTAG
);
2748 /* See comment in restore_write. */
2749 save_resume_state(rwa
, drrwbr
->drr_object
, drrwbr
->drr_offset
, tx
);
2755 receive_write_embedded(struct receive_writer_arg
*rwa
,
2756 struct drr_write_embedded
*drrwe
, void *data
)
2761 if (drrwe
->drr_offset
+ drrwe
->drr_length
< drrwe
->drr_offset
)
2762 return (SET_ERROR(EINVAL
));
2764 if (drrwe
->drr_psize
> BPE_PAYLOAD_SIZE
)
2765 return (SET_ERROR(EINVAL
));
2767 if (drrwe
->drr_etype
>= NUM_BP_EMBEDDED_TYPES
)
2768 return (SET_ERROR(EINVAL
));
2769 if (drrwe
->drr_compression
>= ZIO_COMPRESS_FUNCTIONS
)
2770 return (SET_ERROR(EINVAL
));
2772 return (SET_ERROR(EINVAL
));
2774 if (drrwe
->drr_object
> rwa
->max_object
)
2775 rwa
->max_object
= drrwe
->drr_object
;
2777 tx
= dmu_tx_create(rwa
->os
);
2779 dmu_tx_hold_write(tx
, drrwe
->drr_object
,
2780 drrwe
->drr_offset
, drrwe
->drr_length
);
2781 err
= dmu_tx_assign(tx
, TXG_WAIT
);
2787 dmu_write_embedded(rwa
->os
, drrwe
->drr_object
,
2788 drrwe
->drr_offset
, data
, drrwe
->drr_etype
,
2789 drrwe
->drr_compression
, drrwe
->drr_lsize
, drrwe
->drr_psize
,
2790 rwa
->byteswap
^ ZFS_HOST_BYTEORDER
, tx
);
2792 /* See comment in restore_write. */
2793 save_resume_state(rwa
, drrwe
->drr_object
, drrwe
->drr_offset
, tx
);
2799 receive_spill(struct receive_writer_arg
*rwa
, struct drr_spill
*drrs
,
2803 dmu_buf_t
*db
, *db_spill
;
2806 if (drrs
->drr_length
< SPA_MINBLOCKSIZE
||
2807 drrs
->drr_length
> spa_maxblocksize(dmu_objset_spa(rwa
->os
)))
2808 return (SET_ERROR(EINVAL
));
2811 if (!DMU_OT_IS_VALID(drrs
->drr_type
) ||
2812 drrs
->drr_compressiontype
>= ZIO_COMPRESS_FUNCTIONS
||
2813 drrs
->drr_compressed_size
== 0)
2814 return (SET_ERROR(EINVAL
));
2817 if (dmu_object_info(rwa
->os
, drrs
->drr_object
, NULL
) != 0)
2818 return (SET_ERROR(EINVAL
));
2820 if (drrs
->drr_object
> rwa
->max_object
)
2821 rwa
->max_object
= drrs
->drr_object
;
2823 VERIFY0(dmu_bonus_hold(rwa
->os
, drrs
->drr_object
, FTAG
, &db
));
2824 if ((err
= dmu_spill_hold_by_bonus(db
, FTAG
, &db_spill
)) != 0) {
2825 dmu_buf_rele(db
, FTAG
);
2829 tx
= dmu_tx_create(rwa
->os
);
2831 dmu_tx_hold_spill(tx
, db
->db_object
);
2833 err
= dmu_tx_assign(tx
, TXG_WAIT
);
2835 dmu_buf_rele(db
, FTAG
);
2836 dmu_buf_rele(db_spill
, FTAG
);
2840 dmu_buf_will_dirty(db_spill
, tx
);
2842 VERIFY0(dmu_object_dirty_raw(rwa
->os
, drrs
->drr_object
, tx
));
2844 if (db_spill
->db_size
< drrs
->drr_length
)
2845 VERIFY(0 == dbuf_spill_set_blksz(db_spill
,
2846 drrs
->drr_length
, tx
));
2847 dbuf_assign_arcbuf((dmu_buf_impl_t
*)db_spill
, abuf
, tx
);
2849 dmu_buf_rele(db
, FTAG
);
2850 dmu_buf_rele(db_spill
, FTAG
);
2858 receive_free(struct receive_writer_arg
*rwa
, struct drr_free
*drrf
)
2862 if (drrf
->drr_length
!= -1ULL &&
2863 drrf
->drr_offset
+ drrf
->drr_length
< drrf
->drr_offset
)
2864 return (SET_ERROR(EINVAL
));
2866 if (dmu_object_info(rwa
->os
, drrf
->drr_object
, NULL
) != 0)
2867 return (SET_ERROR(EINVAL
));
2869 if (drrf
->drr_object
> rwa
->max_object
)
2870 rwa
->max_object
= drrf
->drr_object
;
2873 err
= dmu_free_long_range_raw(rwa
->os
, drrf
->drr_object
,
2874 drrf
->drr_offset
, drrf
->drr_length
);
2876 err
= dmu_free_long_range(rwa
->os
, drrf
->drr_object
,
2877 drrf
->drr_offset
, drrf
->drr_length
);
2884 receive_object_range(struct receive_writer_arg
*rwa
,
2885 struct drr_object_range
*drror
)
2889 dnode_t
*mdn
= NULL
;
2890 dmu_buf_t
*db
= NULL
;
2894 * By default, we assume this block is in our native format
2895 * (ZFS_HOST_BYTEORDER). We then take into account whether
2896 * the send stream is byteswapped (rwa->byteswap). Finally,
2897 * we need to byteswap again if this particular block was
2898 * in non-native format on the send side.
2900 boolean_t byteorder
= ZFS_HOST_BYTEORDER
^ rwa
->byteswap
^
2901 !!DRR_IS_RAW_BYTESWAPPED(drror
->drr_flags
);
2904 * Since dnode block sizes are constant, we should not need to worry
2905 * about making sure that the dnode block size is the same on the
2906 * sending and receiving sides for the time being. For non-raw sends,
2907 * this does not matter (and in fact we do not send a DRR_OBJECT_RANGE
2908 * record at all). Raw sends require this record type because the
2909 * encryption parameters are used to protect an entire block of bonus
2910 * buffers. If the size of dnode blocks ever becomes variable,
2911 * handling will need to be added to ensure that dnode block sizes
2912 * match on the sending and receiving side.
2914 if (drror
->drr_numslots
!= DNODES_PER_BLOCK
||
2915 P2PHASE(drror
->drr_firstobj
, DNODES_PER_BLOCK
) != 0 ||
2917 return (SET_ERROR(EINVAL
));
2919 if (drror
->drr_firstobj
> rwa
->max_object
)
2920 rwa
->max_object
= drror
->drr_firstobj
;
2922 offset
= drror
->drr_firstobj
* sizeof (dnode_phys_t
);
2923 mdn
= DMU_META_DNODE(rwa
->os
);
2925 tx
= dmu_tx_create(rwa
->os
);
2926 ret
= dmu_tx_assign(tx
, TXG_WAIT
);
2932 ret
= dmu_buf_hold_by_dnode(mdn
, offset
, FTAG
, &db
,
2933 DMU_READ_PREFETCH
| DMU_READ_NO_DECRYPT
);
2940 * Convert the buffer associated with this range of dnodes to a
2941 * raw buffer. This ensures that it will be written out as a raw
2942 * buffer when we fill in the dnode objects in future records.
2943 * Since we are commiting this tx now, it is technically possible
2944 * for the dnode block to end up on-disk with the incorrect MAC.
2945 * Despite this, the dataset is marked as inconsistent so no other
2946 * code paths (apart from scrubs) will attempt to read this data.
2947 * Scrubs will not be effected by this either since scrubs only
2948 * read raw data and do not attempt to check the MAC.
2950 dmu_convert_to_raw(db
, byteorder
, drror
->drr_salt
, drror
->drr_iv
,
2951 drror
->drr_mac
, tx
);
2952 dmu_buf_rele(db
, FTAG
);
2957 /* used to destroy the drc_ds on error */
2959 dmu_recv_cleanup_ds(dmu_recv_cookie_t
*drc
)
2961 ds_hold_flags_t dsflags
= (drc
->drc_raw
) ? 0 : DS_HOLD_FLAG_DECRYPT
;
2964 * Wait for the txg sync before cleaning up the receive. For
2965 * resumable receives, this ensures that our resume state has
2966 * been written out to disk. For raw receives, this ensures
2967 * that the user accounting code will not attempt to do anything
2968 * after we stopped receiving the dataset.
2970 txg_wait_synced(drc
->drc_ds
->ds_dir
->dd_pool
, 0);
2972 if (drc
->drc_resumable
) {
2973 dsl_dataset_disown(drc
->drc_ds
, dsflags
, dmu_recv_tag
);
2975 char name
[ZFS_MAX_DATASET_NAME_LEN
];
2976 dsl_dataset_name(drc
->drc_ds
, name
);
2977 dsl_dataset_disown(drc
->drc_ds
, dsflags
, dmu_recv_tag
);
2978 (void) dsl_destroy_head(name
);
2983 receive_cksum(struct receive_arg
*ra
, int len
, void *buf
)
2986 (void) fletcher_4_incremental_byteswap(buf
, len
, &ra
->cksum
);
2988 (void) fletcher_4_incremental_native(buf
, len
, &ra
->cksum
);
2993 * Read the payload into a buffer of size len, and update the current record's
2995 * Allocate ra->next_rrd and read the next record's header into
2996 * ra->next_rrd->header.
2997 * Verify checksum of payload and next record.
3000 receive_read_payload_and_next_header(struct receive_arg
*ra
, int len
, void *buf
)
3003 zio_cksum_t cksum_orig
;
3004 zio_cksum_t
*cksump
;
3007 ASSERT3U(len
, <=, SPA_MAXBLOCKSIZE
);
3008 err
= receive_read(ra
, len
, buf
);
3011 receive_cksum(ra
, len
, buf
);
3013 /* note: rrd is NULL when reading the begin record's payload */
3014 if (ra
->rrd
!= NULL
) {
3015 ra
->rrd
->payload
= buf
;
3016 ra
->rrd
->payload_size
= len
;
3017 ra
->rrd
->bytes_read
= ra
->bytes_read
;
3021 ra
->prev_cksum
= ra
->cksum
;
3023 ra
->next_rrd
= kmem_zalloc(sizeof (*ra
->next_rrd
), KM_SLEEP
);
3024 err
= receive_read(ra
, sizeof (ra
->next_rrd
->header
),
3025 &ra
->next_rrd
->header
);
3026 ra
->next_rrd
->bytes_read
= ra
->bytes_read
;
3029 kmem_free(ra
->next_rrd
, sizeof (*ra
->next_rrd
));
3030 ra
->next_rrd
= NULL
;
3033 if (ra
->next_rrd
->header
.drr_type
== DRR_BEGIN
) {
3034 kmem_free(ra
->next_rrd
, sizeof (*ra
->next_rrd
));
3035 ra
->next_rrd
= NULL
;
3036 return (SET_ERROR(EINVAL
));
3040 * Note: checksum is of everything up to but not including the
3043 ASSERT3U(offsetof(dmu_replay_record_t
, drr_u
.drr_checksum
.drr_checksum
),
3044 ==, sizeof (dmu_replay_record_t
) - sizeof (zio_cksum_t
));
3046 offsetof(dmu_replay_record_t
, drr_u
.drr_checksum
.drr_checksum
),
3047 &ra
->next_rrd
->header
);
3049 cksum_orig
= ra
->next_rrd
->header
.drr_u
.drr_checksum
.drr_checksum
;
3050 cksump
= &ra
->next_rrd
->header
.drr_u
.drr_checksum
.drr_checksum
;
3053 byteswap_record(&ra
->next_rrd
->header
);
3055 if ((!ZIO_CHECKSUM_IS_ZERO(cksump
)) &&
3056 !ZIO_CHECKSUM_EQUAL(ra
->cksum
, *cksump
)) {
3057 kmem_free(ra
->next_rrd
, sizeof (*ra
->next_rrd
));
3058 ra
->next_rrd
= NULL
;
3059 return (SET_ERROR(ECKSUM
));
3062 receive_cksum(ra
, sizeof (cksum_orig
), &cksum_orig
);
3068 objlist_create(struct objlist
*list
)
3070 list_create(&list
->list
, sizeof (struct receive_objnode
),
3071 offsetof(struct receive_objnode
, node
));
3072 list
->last_lookup
= 0;
3076 objlist_destroy(struct objlist
*list
)
3078 struct receive_objnode
*n
;
3080 for (n
= list_remove_head(&list
->list
);
3081 n
!= NULL
; n
= list_remove_head(&list
->list
)) {
3082 kmem_free(n
, sizeof (*n
));
3084 list_destroy(&list
->list
);
3088 * This function looks through the objlist to see if the specified object number
3089 * is contained in the objlist. In the process, it will remove all object
3090 * numbers in the list that are smaller than the specified object number. Thus,
3091 * any lookup of an object number smaller than a previously looked up object
3092 * number will always return false; therefore, all lookups should be done in
3096 objlist_exists(struct objlist
*list
, uint64_t object
)
3098 struct receive_objnode
*node
= list_head(&list
->list
);
3099 ASSERT3U(object
, >=, list
->last_lookup
);
3100 list
->last_lookup
= object
;
3101 while (node
!= NULL
&& node
->object
< object
) {
3102 VERIFY3P(node
, ==, list_remove_head(&list
->list
));
3103 kmem_free(node
, sizeof (*node
));
3104 node
= list_head(&list
->list
);
3106 return (node
!= NULL
&& node
->object
== object
);
3110 * The objlist is a list of object numbers stored in ascending order. However,
3111 * the insertion of new object numbers does not seek out the correct location to
3112 * store a new object number; instead, it appends it to the list for simplicity.
3113 * Thus, any users must take care to only insert new object numbers in ascending
3117 objlist_insert(struct objlist
*list
, uint64_t object
)
3119 struct receive_objnode
*node
= kmem_zalloc(sizeof (*node
), KM_SLEEP
);
3120 node
->object
= object
;
3123 struct receive_objnode
*last_object
= list_tail(&list
->list
);
3124 uint64_t last_objnum
= (last_object
!= NULL
? last_object
->object
: 0);
3125 ASSERT3U(node
->object
, >, last_objnum
);
3128 list_insert_tail(&list
->list
, node
);
3132 * Issue the prefetch reads for any necessary indirect blocks.
3134 * We use the object ignore list to tell us whether or not to issue prefetches
3135 * for a given object. We do this for both correctness (in case the blocksize
3136 * of an object has changed) and performance (if the object doesn't exist, don't
3137 * needlessly try to issue prefetches). We also trim the list as we go through
3138 * the stream to prevent it from growing to an unbounded size.
3140 * The object numbers within will always be in sorted order, and any write
3141 * records we see will also be in sorted order, but they're not sorted with
3142 * respect to each other (i.e. we can get several object records before
3143 * receiving each object's write records). As a result, once we've reached a
3144 * given object number, we can safely remove any reference to lower object
3145 * numbers in the ignore list. In practice, we receive up to 32 object records
3146 * before receiving write records, so the list can have up to 32 nodes in it.
3150 receive_read_prefetch(struct receive_arg
*ra
,
3151 uint64_t object
, uint64_t offset
, uint64_t length
)
3153 if (!objlist_exists(&ra
->ignore_objlist
, object
)) {
3154 dmu_prefetch(ra
->os
, object
, 1, offset
, length
,
3155 ZIO_PRIORITY_SYNC_READ
);
3160 * Read records off the stream, issuing any necessary prefetches.
3163 receive_read_record(struct receive_arg
*ra
)
3167 switch (ra
->rrd
->header
.drr_type
) {
3170 struct drr_object
*drro
= &ra
->rrd
->header
.drr_u
.drr_object
;
3171 uint32_t size
= DRR_OBJECT_PAYLOAD_SIZE(drro
);
3172 void *buf
= kmem_zalloc(size
, KM_SLEEP
);
3173 dmu_object_info_t doi
;
3175 err
= receive_read_payload_and_next_header(ra
, size
, buf
);
3177 kmem_free(buf
, size
);
3180 err
= dmu_object_info(ra
->os
, drro
->drr_object
, &doi
);
3182 * See receive_read_prefetch for an explanation why we're
3183 * storing this object in the ignore_obj_list.
3185 if (err
== ENOENT
||
3186 (err
== 0 && doi
.doi_data_block_size
!= drro
->drr_blksz
)) {
3187 objlist_insert(&ra
->ignore_objlist
, drro
->drr_object
);
3192 case DRR_FREEOBJECTS
:
3194 err
= receive_read_payload_and_next_header(ra
, 0, NULL
);
3199 struct drr_write
*drrw
= &ra
->rrd
->header
.drr_u
.drr_write
;
3201 boolean_t is_meta
= DMU_OT_IS_METADATA(drrw
->drr_type
);
3204 boolean_t byteorder
= ZFS_HOST_BYTEORDER
^
3205 !!DRR_IS_RAW_BYTESWAPPED(drrw
->drr_flags
) ^
3208 abuf
= arc_loan_raw_buf(dmu_objset_spa(ra
->os
),
3209 drrw
->drr_object
, byteorder
, drrw
->drr_salt
,
3210 drrw
->drr_iv
, drrw
->drr_mac
, drrw
->drr_type
,
3211 drrw
->drr_compressed_size
, drrw
->drr_logical_size
,
3212 drrw
->drr_compressiontype
);
3213 } else if (DRR_WRITE_COMPRESSED(drrw
)) {
3214 ASSERT3U(drrw
->drr_compressed_size
, >, 0);
3215 ASSERT3U(drrw
->drr_logical_size
, >=,
3216 drrw
->drr_compressed_size
);
3218 abuf
= arc_loan_compressed_buf(
3219 dmu_objset_spa(ra
->os
),
3220 drrw
->drr_compressed_size
, drrw
->drr_logical_size
,
3221 drrw
->drr_compressiontype
);
3223 abuf
= arc_loan_buf(dmu_objset_spa(ra
->os
),
3224 is_meta
, drrw
->drr_logical_size
);
3227 err
= receive_read_payload_and_next_header(ra
,
3228 DRR_WRITE_PAYLOAD_SIZE(drrw
), abuf
->b_data
);
3230 dmu_return_arcbuf(abuf
);
3233 ra
->rrd
->arc_buf
= abuf
;
3234 receive_read_prefetch(ra
, drrw
->drr_object
, drrw
->drr_offset
,
3235 drrw
->drr_logical_size
);
3238 case DRR_WRITE_BYREF
:
3240 struct drr_write_byref
*drrwb
=
3241 &ra
->rrd
->header
.drr_u
.drr_write_byref
;
3242 err
= receive_read_payload_and_next_header(ra
, 0, NULL
);
3243 receive_read_prefetch(ra
, drrwb
->drr_object
, drrwb
->drr_offset
,
3247 case DRR_WRITE_EMBEDDED
:
3249 struct drr_write_embedded
*drrwe
=
3250 &ra
->rrd
->header
.drr_u
.drr_write_embedded
;
3251 uint32_t size
= P2ROUNDUP(drrwe
->drr_psize
, 8);
3252 void *buf
= kmem_zalloc(size
, KM_SLEEP
);
3254 err
= receive_read_payload_and_next_header(ra
, size
, buf
);
3256 kmem_free(buf
, size
);
3260 receive_read_prefetch(ra
, drrwe
->drr_object
, drrwe
->drr_offset
,
3267 * It might be beneficial to prefetch indirect blocks here, but
3268 * we don't really have the data to decide for sure.
3270 err
= receive_read_payload_and_next_header(ra
, 0, NULL
);
3275 struct drr_end
*drre
= &ra
->rrd
->header
.drr_u
.drr_end
;
3276 if (!ZIO_CHECKSUM_EQUAL(ra
->prev_cksum
, drre
->drr_checksum
))
3277 return (SET_ERROR(ECKSUM
));
3282 struct drr_spill
*drrs
= &ra
->rrd
->header
.drr_u
.drr_spill
;
3284 int len
= DRR_SPILL_PAYLOAD_SIZE(drrs
);
3286 /* DRR_SPILL records are either raw or uncompressed */
3288 boolean_t byteorder
= ZFS_HOST_BYTEORDER
^
3289 !!DRR_IS_RAW_BYTESWAPPED(drrs
->drr_flags
) ^
3292 abuf
= arc_loan_raw_buf(dmu_objset_spa(ra
->os
),
3293 drrs
->drr_object
, byteorder
, drrs
->drr_salt
,
3294 drrs
->drr_iv
, drrs
->drr_mac
, drrs
->drr_type
,
3295 drrs
->drr_compressed_size
, drrs
->drr_length
,
3296 drrs
->drr_compressiontype
);
3298 abuf
= arc_loan_buf(dmu_objset_spa(ra
->os
),
3299 DMU_OT_IS_METADATA(drrs
->drr_type
),
3303 err
= receive_read_payload_and_next_header(ra
, len
,
3306 dmu_return_arcbuf(abuf
);
3309 ra
->rrd
->arc_buf
= abuf
;
3312 case DRR_OBJECT_RANGE
:
3314 err
= receive_read_payload_and_next_header(ra
, 0, NULL
);
3318 return (SET_ERROR(EINVAL
));
3323 dprintf_drr(struct receive_record_arg
*rrd
, int err
)
3325 switch (rrd
->header
.drr_type
) {
3328 struct drr_object
*drro
= &rrd
->header
.drr_u
.drr_object
;
3329 dprintf("drr_type = OBJECT obj = %llu type = %u "
3330 "bonustype = %u blksz = %u bonuslen = %u cksumtype = %u "
3331 "compress = %u dn_slots = %u err = %d\n",
3332 drro
->drr_object
, drro
->drr_type
, drro
->drr_bonustype
,
3333 drro
->drr_blksz
, drro
->drr_bonuslen
,
3334 drro
->drr_checksumtype
, drro
->drr_compress
,
3335 drro
->drr_dn_slots
, err
);
3338 case DRR_FREEOBJECTS
:
3340 struct drr_freeobjects
*drrfo
=
3341 &rrd
->header
.drr_u
.drr_freeobjects
;
3342 dprintf("drr_type = FREEOBJECTS firstobj = %llu "
3343 "numobjs = %llu err = %d\n",
3344 drrfo
->drr_firstobj
, drrfo
->drr_numobjs
, err
);
3349 struct drr_write
*drrw
= &rrd
->header
.drr_u
.drr_write
;
3350 dprintf("drr_type = WRITE obj = %llu type = %u offset = %llu "
3351 "lsize = %llu cksumtype = %u cksumflags = %u "
3352 "compress = %u psize = %llu err = %d\n",
3353 drrw
->drr_object
, drrw
->drr_type
, drrw
->drr_offset
,
3354 drrw
->drr_logical_size
, drrw
->drr_checksumtype
,
3355 drrw
->drr_flags
, drrw
->drr_compressiontype
,
3356 drrw
->drr_compressed_size
, err
);
3359 case DRR_WRITE_BYREF
:
3361 struct drr_write_byref
*drrwbr
=
3362 &rrd
->header
.drr_u
.drr_write_byref
;
3363 dprintf("drr_type = WRITE_BYREF obj = %llu offset = %llu "
3364 "length = %llu toguid = %llx refguid = %llx "
3365 "refobject = %llu refoffset = %llu cksumtype = %u "
3366 "cksumflags = %u err = %d\n",
3367 drrwbr
->drr_object
, drrwbr
->drr_offset
,
3368 drrwbr
->drr_length
, drrwbr
->drr_toguid
,
3369 drrwbr
->drr_refguid
, drrwbr
->drr_refobject
,
3370 drrwbr
->drr_refoffset
, drrwbr
->drr_checksumtype
,
3371 drrwbr
->drr_flags
, err
);
3374 case DRR_WRITE_EMBEDDED
:
3376 struct drr_write_embedded
*drrwe
=
3377 &rrd
->header
.drr_u
.drr_write_embedded
;
3378 dprintf("drr_type = WRITE_EMBEDDED obj = %llu offset = %llu "
3379 "length = %llu compress = %u etype = %u lsize = %u "
3380 "psize = %u err = %d\n",
3381 drrwe
->drr_object
, drrwe
->drr_offset
, drrwe
->drr_length
,
3382 drrwe
->drr_compression
, drrwe
->drr_etype
,
3383 drrwe
->drr_lsize
, drrwe
->drr_psize
, err
);
3388 struct drr_free
*drrf
= &rrd
->header
.drr_u
.drr_free
;
3389 dprintf("drr_type = FREE obj = %llu offset = %llu "
3390 "length = %lld err = %d\n",
3391 drrf
->drr_object
, drrf
->drr_offset
, drrf
->drr_length
,
3397 struct drr_spill
*drrs
= &rrd
->header
.drr_u
.drr_spill
;
3398 dprintf("drr_type = SPILL obj = %llu length = %llu "
3399 "err = %d\n", drrs
->drr_object
, drrs
->drr_length
, err
);
3408 * Commit the records to the pool.
3411 receive_process_record(struct receive_writer_arg
*rwa
,
3412 struct receive_record_arg
*rrd
)
3416 /* Processing in order, therefore bytes_read should be increasing. */
3417 ASSERT3U(rrd
->bytes_read
, >=, rwa
->bytes_read
);
3418 rwa
->bytes_read
= rrd
->bytes_read
;
3420 switch (rrd
->header
.drr_type
) {
3423 struct drr_object
*drro
= &rrd
->header
.drr_u
.drr_object
;
3424 err
= receive_object(rwa
, drro
, rrd
->payload
);
3425 kmem_free(rrd
->payload
, rrd
->payload_size
);
3426 rrd
->payload
= NULL
;
3429 case DRR_FREEOBJECTS
:
3431 struct drr_freeobjects
*drrfo
=
3432 &rrd
->header
.drr_u
.drr_freeobjects
;
3433 err
= receive_freeobjects(rwa
, drrfo
);
3438 struct drr_write
*drrw
= &rrd
->header
.drr_u
.drr_write
;
3439 err
= receive_write(rwa
, drrw
, rrd
->arc_buf
);
3440 /* if receive_write() is successful, it consumes the arc_buf */
3442 dmu_return_arcbuf(rrd
->arc_buf
);
3443 rrd
->arc_buf
= NULL
;
3444 rrd
->payload
= NULL
;
3447 case DRR_WRITE_BYREF
:
3449 struct drr_write_byref
*drrwbr
=
3450 &rrd
->header
.drr_u
.drr_write_byref
;
3451 err
= receive_write_byref(rwa
, drrwbr
);
3454 case DRR_WRITE_EMBEDDED
:
3456 struct drr_write_embedded
*drrwe
=
3457 &rrd
->header
.drr_u
.drr_write_embedded
;
3458 err
= receive_write_embedded(rwa
, drrwe
, rrd
->payload
);
3459 kmem_free(rrd
->payload
, rrd
->payload_size
);
3460 rrd
->payload
= NULL
;
3465 struct drr_free
*drrf
= &rrd
->header
.drr_u
.drr_free
;
3466 err
= receive_free(rwa
, drrf
);
3471 struct drr_spill
*drrs
= &rrd
->header
.drr_u
.drr_spill
;
3472 err
= receive_spill(rwa
, drrs
, rrd
->arc_buf
);
3473 /* if receive_spill() is successful, it consumes the arc_buf */
3475 dmu_return_arcbuf(rrd
->arc_buf
);
3476 rrd
->arc_buf
= NULL
;
3477 rrd
->payload
= NULL
;
3480 case DRR_OBJECT_RANGE
:
3482 struct drr_object_range
*drror
=
3483 &rrd
->header
.drr_u
.drr_object_range
;
3484 return (receive_object_range(rwa
, drror
));
3487 return (SET_ERROR(EINVAL
));
3491 dprintf_drr(rrd
, err
);
3497 * dmu_recv_stream's worker thread; pull records off the queue, and then call
3498 * receive_process_record When we're done, signal the main thread and exit.
3501 receive_writer_thread(void *arg
)
3503 struct receive_writer_arg
*rwa
= arg
;
3504 struct receive_record_arg
*rrd
;
3505 fstrans_cookie_t cookie
= spl_fstrans_mark();
3507 for (rrd
= bqueue_dequeue(&rwa
->q
); !rrd
->eos_marker
;
3508 rrd
= bqueue_dequeue(&rwa
->q
)) {
3510 * If there's an error, the main thread will stop putting things
3511 * on the queue, but we need to clear everything in it before we
3514 if (rwa
->err
== 0) {
3515 rwa
->err
= receive_process_record(rwa
, rrd
);
3516 } else if (rrd
->arc_buf
!= NULL
) {
3517 dmu_return_arcbuf(rrd
->arc_buf
);
3518 rrd
->arc_buf
= NULL
;
3519 rrd
->payload
= NULL
;
3520 } else if (rrd
->payload
!= NULL
) {
3521 kmem_free(rrd
->payload
, rrd
->payload_size
);
3522 rrd
->payload
= NULL
;
3524 kmem_free(rrd
, sizeof (*rrd
));
3526 kmem_free(rrd
, sizeof (*rrd
));
3527 mutex_enter(&rwa
->mutex
);
3529 cv_signal(&rwa
->cv
);
3530 mutex_exit(&rwa
->mutex
);
3531 spl_fstrans_unmark(cookie
);
3536 resume_check(struct receive_arg
*ra
, nvlist_t
*begin_nvl
)
3539 objset_t
*mos
= dmu_objset_pool(ra
->os
)->dp_meta_objset
;
3540 uint64_t dsobj
= dmu_objset_id(ra
->os
);
3541 uint64_t resume_obj
, resume_off
;
3543 if (nvlist_lookup_uint64(begin_nvl
,
3544 "resume_object", &resume_obj
) != 0 ||
3545 nvlist_lookup_uint64(begin_nvl
,
3546 "resume_offset", &resume_off
) != 0) {
3547 return (SET_ERROR(EINVAL
));
3549 VERIFY0(zap_lookup(mos
, dsobj
,
3550 DS_FIELD_RESUME_OBJECT
, sizeof (val
), 1, &val
));
3551 if (resume_obj
!= val
)
3552 return (SET_ERROR(EINVAL
));
3553 VERIFY0(zap_lookup(mos
, dsobj
,
3554 DS_FIELD_RESUME_OFFSET
, sizeof (val
), 1, &val
));
3555 if (resume_off
!= val
)
3556 return (SET_ERROR(EINVAL
));
3562 * Read in the stream's records, one by one, and apply them to the pool. There
3563 * are two threads involved; the thread that calls this function will spin up a
3564 * worker thread, read the records off the stream one by one, and issue
3565 * prefetches for any necessary indirect blocks. It will then push the records
3566 * onto an internal blocking queue. The worker thread will pull the records off
3567 * the queue, and actually write the data into the DMU. This way, the worker
3568 * thread doesn't have to wait for reads to complete, since everything it needs
3569 * (the indirect blocks) will be prefetched.
3571 * NB: callers *must* call dmu_recv_end() if this succeeds.
3574 dmu_recv_stream(dmu_recv_cookie_t
*drc
, vnode_t
*vp
, offset_t
*voffp
,
3575 int cleanup_fd
, uint64_t *action_handlep
)
3578 struct receive_arg
*ra
;
3579 struct receive_writer_arg
*rwa
;
3581 uint32_t payloadlen
;
3583 nvlist_t
*begin_nvl
= NULL
;
3585 ra
= kmem_zalloc(sizeof (*ra
), KM_SLEEP
);
3586 rwa
= kmem_zalloc(sizeof (*rwa
), KM_SLEEP
);
3588 ra
->byteswap
= drc
->drc_byteswap
;
3589 ra
->raw
= drc
->drc_raw
;
3590 ra
->cksum
= drc
->drc_cksum
;
3594 if (dsl_dataset_is_zapified(drc
->drc_ds
)) {
3595 (void) zap_lookup(drc
->drc_ds
->ds_dir
->dd_pool
->dp_meta_objset
,
3596 drc
->drc_ds
->ds_object
, DS_FIELD_RESUME_BYTES
,
3597 sizeof (ra
->bytes_read
), 1, &ra
->bytes_read
);
3600 objlist_create(&ra
->ignore_objlist
);
3602 /* these were verified in dmu_recv_begin */
3603 ASSERT3U(DMU_GET_STREAM_HDRTYPE(drc
->drc_drrb
->drr_versioninfo
), ==,
3605 ASSERT3U(drc
->drc_drrb
->drr_type
, <, DMU_OST_NUMTYPES
);
3608 * Open the objset we are modifying.
3610 VERIFY0(dmu_objset_from_ds(drc
->drc_ds
, &ra
->os
));
3612 ASSERT(dsl_dataset_phys(drc
->drc_ds
)->ds_flags
& DS_FLAG_INCONSISTENT
);
3614 featureflags
= DMU_GET_FEATUREFLAGS(drc
->drc_drrb
->drr_versioninfo
);
3615 ra
->featureflags
= featureflags
;
3617 /* embedded data is incompatible with encrypted datasets */
3618 if (ra
->os
->os_encrypted
&&
3619 (featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA
)) {
3620 err
= SET_ERROR(EINVAL
);
3624 /* if this stream is dedup'ed, set up the avl tree for guid mapping */
3625 if (featureflags
& DMU_BACKUP_FEATURE_DEDUP
) {
3628 if (cleanup_fd
== -1) {
3629 err
= SET_ERROR(EBADF
);
3632 err
= zfs_onexit_fd_hold(cleanup_fd
, &minor
);
3638 if (*action_handlep
== 0) {
3639 rwa
->guid_to_ds_map
=
3640 kmem_alloc(sizeof (avl_tree_t
), KM_SLEEP
);
3641 avl_create(rwa
->guid_to_ds_map
, guid_compare
,
3642 sizeof (guid_map_entry_t
),
3643 offsetof(guid_map_entry_t
, avlnode
));
3644 err
= zfs_onexit_add_cb(minor
,
3645 free_guid_map_onexit
, rwa
->guid_to_ds_map
,
3650 err
= zfs_onexit_cb_data(minor
, *action_handlep
,
3651 (void **)&rwa
->guid_to_ds_map
);
3656 drc
->drc_guid_to_ds_map
= rwa
->guid_to_ds_map
;
3659 payloadlen
= drc
->drc_drr_begin
->drr_payloadlen
;
3661 if (payloadlen
!= 0)
3662 payload
= kmem_alloc(payloadlen
, KM_SLEEP
);
3664 err
= receive_read_payload_and_next_header(ra
, payloadlen
, payload
);
3666 if (payloadlen
!= 0)
3667 kmem_free(payload
, payloadlen
);
3670 if (payloadlen
!= 0) {
3671 err
= nvlist_unpack(payload
, payloadlen
, &begin_nvl
, KM_SLEEP
);
3672 kmem_free(payload
, payloadlen
);
3677 /* handle DSL encryption key payload */
3678 if (featureflags
& DMU_BACKUP_FEATURE_RAW
) {
3679 nvlist_t
*keynvl
= NULL
;
3681 ASSERT(ra
->os
->os_encrypted
);
3682 ASSERT(drc
->drc_raw
);
3684 err
= nvlist_lookup_nvlist(begin_nvl
, "crypt_keydata", &keynvl
);
3688 err
= dsl_crypto_recv_key(spa_name(ra
->os
->os_spa
),
3689 drc
->drc_ds
->ds_object
, drc
->drc_drrb
->drr_type
,
3695 if (featureflags
& DMU_BACKUP_FEATURE_RESUMING
) {
3696 err
= resume_check(ra
, begin_nvl
);
3701 (void) bqueue_init(&rwa
->q
, zfs_recv_queue_length
,
3702 offsetof(struct receive_record_arg
, node
));
3703 cv_init(&rwa
->cv
, NULL
, CV_DEFAULT
, NULL
);
3704 mutex_init(&rwa
->mutex
, NULL
, MUTEX_DEFAULT
, NULL
);
3706 rwa
->byteswap
= drc
->drc_byteswap
;
3707 rwa
->resumable
= drc
->drc_resumable
;
3708 rwa
->raw
= drc
->drc_raw
;
3710 (void) thread_create(NULL
, 0, receive_writer_thread
, rwa
, 0, curproc
,
3711 TS_RUN
, minclsyspri
);
3713 * We're reading rwa->err without locks, which is safe since we are the
3714 * only reader, and the worker thread is the only writer. It's ok if we
3715 * miss a write for an iteration or two of the loop, since the writer
3716 * thread will keep freeing records we send it until we send it an eos
3719 * We can leave this loop in 3 ways: First, if rwa->err is
3720 * non-zero. In that case, the writer thread will free the rrd we just
3721 * pushed. Second, if we're interrupted; in that case, either it's the
3722 * first loop and ra->rrd was never allocated, or it's later and ra->rrd
3723 * has been handed off to the writer thread who will free it. Finally,
3724 * if receive_read_record fails or we're at the end of the stream, then
3725 * we free ra->rrd and exit.
3727 while (rwa
->err
== 0) {
3728 if (issig(JUSTLOOKING
) && issig(FORREAL
)) {
3729 err
= SET_ERROR(EINTR
);
3733 ASSERT3P(ra
->rrd
, ==, NULL
);
3734 ra
->rrd
= ra
->next_rrd
;
3735 ra
->next_rrd
= NULL
;
3736 /* Allocates and loads header into ra->next_rrd */
3737 err
= receive_read_record(ra
);
3739 if (ra
->rrd
->header
.drr_type
== DRR_END
|| err
!= 0) {
3740 kmem_free(ra
->rrd
, sizeof (*ra
->rrd
));
3745 bqueue_enqueue(&rwa
->q
, ra
->rrd
,
3746 sizeof (struct receive_record_arg
) + ra
->rrd
->payload_size
);
3749 if (ra
->next_rrd
== NULL
)
3750 ra
->next_rrd
= kmem_zalloc(sizeof (*ra
->next_rrd
), KM_SLEEP
);
3751 ra
->next_rrd
->eos_marker
= B_TRUE
;
3752 bqueue_enqueue(&rwa
->q
, ra
->next_rrd
, 1);
3754 mutex_enter(&rwa
->mutex
);
3755 while (!rwa
->done
) {
3756 cv_wait(&rwa
->cv
, &rwa
->mutex
);
3758 mutex_exit(&rwa
->mutex
);
3761 * If we are receiving a full stream as a clone, all object IDs which
3762 * are greater than the maximum ID referenced in the stream are
3763 * by definition unused and must be freed.
3765 if (drc
->drc_clone
&& drc
->drc_drrb
->drr_fromguid
== 0) {
3766 uint64_t obj
= rwa
->max_object
+ 1;
3770 while (next_err
== 0) {
3771 free_err
= dmu_free_long_object(rwa
->os
, obj
);
3772 if (free_err
!= 0 && free_err
!= ENOENT
)
3775 next_err
= dmu_object_next(rwa
->os
, &obj
, FALSE
, 0);
3779 if (free_err
!= 0 && free_err
!= ENOENT
)
3781 else if (next_err
!= ESRCH
)
3786 cv_destroy(&rwa
->cv
);
3787 mutex_destroy(&rwa
->mutex
);
3788 bqueue_destroy(&rwa
->q
);
3793 nvlist_free(begin_nvl
);
3794 if ((featureflags
& DMU_BACKUP_FEATURE_DEDUP
) && (cleanup_fd
!= -1))
3795 zfs_onexit_fd_rele(cleanup_fd
);
3799 * Clean up references. If receive is not resumable,
3800 * destroy what we created, so we don't leave it in
3801 * the inconsistent state.
3803 dmu_recv_cleanup_ds(drc
);
3807 objlist_destroy(&ra
->ignore_objlist
);
3808 kmem_free(ra
, sizeof (*ra
));
3809 kmem_free(rwa
, sizeof (*rwa
));
3814 dmu_recv_end_check(void *arg
, dmu_tx_t
*tx
)
3816 dmu_recv_cookie_t
*drc
= arg
;
3817 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
3820 ASSERT3P(drc
->drc_ds
->ds_owner
, ==, dmu_recv_tag
);
3822 if (!drc
->drc_newfs
) {
3823 dsl_dataset_t
*origin_head
;
3825 error
= dsl_dataset_hold(dp
, drc
->drc_tofs
, FTAG
, &origin_head
);
3828 if (drc
->drc_force
) {
3830 * We will destroy any snapshots in tofs (i.e. before
3831 * origin_head) that are after the origin (which is
3832 * the snap before drc_ds, because drc_ds can not
3833 * have any snaps of its own).
3837 obj
= dsl_dataset_phys(origin_head
)->ds_prev_snap_obj
;
3839 dsl_dataset_phys(drc
->drc_ds
)->ds_prev_snap_obj
) {
3840 dsl_dataset_t
*snap
;
3841 error
= dsl_dataset_hold_obj(dp
, obj
, FTAG
,
3845 if (snap
->ds_dir
!= origin_head
->ds_dir
)
3846 error
= SET_ERROR(EINVAL
);
3848 error
= dsl_destroy_snapshot_check_impl(
3851 obj
= dsl_dataset_phys(snap
)->ds_prev_snap_obj
;
3852 dsl_dataset_rele(snap
, FTAG
);
3857 dsl_dataset_rele(origin_head
, FTAG
);
3861 error
= dsl_dataset_clone_swap_check_impl(drc
->drc_ds
,
3862 origin_head
, drc
->drc_force
, drc
->drc_owner
, tx
);
3864 dsl_dataset_rele(origin_head
, FTAG
);
3867 error
= dsl_dataset_snapshot_check_impl(origin_head
,
3868 drc
->drc_tosnap
, tx
, B_TRUE
, 1, drc
->drc_cred
);
3869 dsl_dataset_rele(origin_head
, FTAG
);
3873 error
= dsl_destroy_head_check_impl(drc
->drc_ds
, 1);
3875 error
= dsl_dataset_snapshot_check_impl(drc
->drc_ds
,
3876 drc
->drc_tosnap
, tx
, B_TRUE
, 1, drc
->drc_cred
);
3882 dmu_recv_end_sync(void *arg
, dmu_tx_t
*tx
)
3884 dmu_recv_cookie_t
*drc
= arg
;
3885 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
3886 boolean_t encrypted
= drc
->drc_ds
->ds_dir
->dd_crypto_obj
!= 0;
3888 spa_history_log_internal_ds(drc
->drc_ds
, "finish receiving",
3889 tx
, "snap=%s", drc
->drc_tosnap
);
3891 if (!drc
->drc_newfs
) {
3892 dsl_dataset_t
*origin_head
;
3894 VERIFY0(dsl_dataset_hold(dp
, drc
->drc_tofs
, FTAG
,
3897 if (drc
->drc_force
) {
3899 * Destroy any snapshots of drc_tofs (origin_head)
3900 * after the origin (the snap before drc_ds).
3904 obj
= dsl_dataset_phys(origin_head
)->ds_prev_snap_obj
;
3906 dsl_dataset_phys(drc
->drc_ds
)->ds_prev_snap_obj
) {
3907 dsl_dataset_t
*snap
;
3908 VERIFY0(dsl_dataset_hold_obj(dp
, obj
, FTAG
,
3910 ASSERT3P(snap
->ds_dir
, ==, origin_head
->ds_dir
);
3911 obj
= dsl_dataset_phys(snap
)->ds_prev_snap_obj
;
3912 dsl_destroy_snapshot_sync_impl(snap
,
3914 dsl_dataset_rele(snap
, FTAG
);
3917 VERIFY3P(drc
->drc_ds
->ds_prev
, ==,
3918 origin_head
->ds_prev
);
3920 dsl_dataset_clone_swap_sync_impl(drc
->drc_ds
,
3922 dsl_dataset_snapshot_sync_impl(origin_head
,
3923 drc
->drc_tosnap
, tx
);
3925 /* set snapshot's creation time and guid */
3926 dmu_buf_will_dirty(origin_head
->ds_prev
->ds_dbuf
, tx
);
3927 dsl_dataset_phys(origin_head
->ds_prev
)->ds_creation_time
=
3928 drc
->drc_drrb
->drr_creation_time
;
3929 dsl_dataset_phys(origin_head
->ds_prev
)->ds_guid
=
3930 drc
->drc_drrb
->drr_toguid
;
3931 dsl_dataset_phys(origin_head
->ds_prev
)->ds_flags
&=
3932 ~DS_FLAG_INCONSISTENT
;
3934 dmu_buf_will_dirty(origin_head
->ds_dbuf
, tx
);
3935 dsl_dataset_phys(origin_head
)->ds_flags
&=
3936 ~DS_FLAG_INCONSISTENT
;
3938 drc
->drc_newsnapobj
=
3939 dsl_dataset_phys(origin_head
)->ds_prev_snap_obj
;
3941 dsl_dataset_rele(origin_head
, FTAG
);
3942 dsl_destroy_head_sync_impl(drc
->drc_ds
, tx
);
3944 if (drc
->drc_owner
!= NULL
)
3945 VERIFY3P(origin_head
->ds_owner
, ==, drc
->drc_owner
);
3947 dsl_dataset_t
*ds
= drc
->drc_ds
;
3949 dsl_dataset_snapshot_sync_impl(ds
, drc
->drc_tosnap
, tx
);
3951 /* set snapshot's creation time and guid */
3952 dmu_buf_will_dirty(ds
->ds_prev
->ds_dbuf
, tx
);
3953 dsl_dataset_phys(ds
->ds_prev
)->ds_creation_time
=
3954 drc
->drc_drrb
->drr_creation_time
;
3955 dsl_dataset_phys(ds
->ds_prev
)->ds_guid
=
3956 drc
->drc_drrb
->drr_toguid
;
3957 dsl_dataset_phys(ds
->ds_prev
)->ds_flags
&=
3958 ~DS_FLAG_INCONSISTENT
;
3960 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
3961 dsl_dataset_phys(ds
)->ds_flags
&= ~DS_FLAG_INCONSISTENT
;
3962 if (dsl_dataset_has_resume_receive_state(ds
)) {
3963 (void) zap_remove(dp
->dp_meta_objset
, ds
->ds_object
,
3964 DS_FIELD_RESUME_FROMGUID
, tx
);
3965 (void) zap_remove(dp
->dp_meta_objset
, ds
->ds_object
,
3966 DS_FIELD_RESUME_OBJECT
, tx
);
3967 (void) zap_remove(dp
->dp_meta_objset
, ds
->ds_object
,
3968 DS_FIELD_RESUME_OFFSET
, tx
);
3969 (void) zap_remove(dp
->dp_meta_objset
, ds
->ds_object
,
3970 DS_FIELD_RESUME_BYTES
, tx
);
3971 (void) zap_remove(dp
->dp_meta_objset
, ds
->ds_object
,
3972 DS_FIELD_RESUME_TOGUID
, tx
);
3973 (void) zap_remove(dp
->dp_meta_objset
, ds
->ds_object
,
3974 DS_FIELD_RESUME_TONAME
, tx
);
3976 drc
->drc_newsnapobj
=
3977 dsl_dataset_phys(drc
->drc_ds
)->ds_prev_snap_obj
;
3979 zvol_create_minors(dp
->dp_spa
, drc
->drc_tofs
, B_TRUE
);
3982 * Release the hold from dmu_recv_begin. This must be done before
3983 * we return to open context, so that when we free the dataset's dnode
3984 * we can evict its bonus buffer. Since the dataset may be destroyed
3985 * at this point (and therefore won't have a valid pointer to the spa)
3986 * we release the key mapping manually here while we do have a valid
3987 * pointer, if it exists.
3989 if (!drc
->drc_raw
&& encrypted
) {
3990 (void) spa_keystore_remove_mapping(dmu_tx_pool(tx
)->dp_spa
,
3991 drc
->drc_ds
->ds_object
, drc
->drc_ds
);
3993 dsl_dataset_disown(drc
->drc_ds
, 0, dmu_recv_tag
);
3998 add_ds_to_guidmap(const char *name
, avl_tree_t
*guid_map
, uint64_t snapobj
,
4002 dsl_dataset_t
*snapds
;
4003 guid_map_entry_t
*gmep
;
4004 ds_hold_flags_t dsflags
= (raw
) ? 0 : DS_HOLD_FLAG_DECRYPT
;
4007 ASSERT(guid_map
!= NULL
);
4009 err
= dsl_pool_hold(name
, FTAG
, &dp
);
4012 gmep
= kmem_alloc(sizeof (*gmep
), KM_SLEEP
);
4013 err
= dsl_dataset_hold_obj_flags(dp
, snapobj
, dsflags
, gmep
, &snapds
);
4015 gmep
->guid
= dsl_dataset_phys(snapds
)->ds_guid
;
4017 gmep
->gme_ds
= snapds
;
4018 avl_add(guid_map
, gmep
);
4019 dsl_dataset_long_hold(snapds
, gmep
);
4021 kmem_free(gmep
, sizeof (*gmep
));
4024 dsl_pool_rele(dp
, FTAG
);
4028 static int dmu_recv_end_modified_blocks
= 3;
4031 dmu_recv_existing_end(dmu_recv_cookie_t
*drc
)
4035 * We will be destroying the ds; make sure its origin is unmounted if
4038 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4039 dsl_dataset_name(drc
->drc_ds
, name
);
4040 zfs_destroy_unmount_origin(name
);
4043 return (dsl_sync_task(drc
->drc_tofs
,
4044 dmu_recv_end_check
, dmu_recv_end_sync
, drc
,
4045 dmu_recv_end_modified_blocks
, ZFS_SPACE_CHECK_NORMAL
));
4049 dmu_recv_new_end(dmu_recv_cookie_t
*drc
)
4051 return (dsl_sync_task(drc
->drc_tofs
,
4052 dmu_recv_end_check
, dmu_recv_end_sync
, drc
,
4053 dmu_recv_end_modified_blocks
, ZFS_SPACE_CHECK_NORMAL
));
4057 dmu_recv_end(dmu_recv_cookie_t
*drc
, void *owner
)
4061 drc
->drc_owner
= owner
;
4064 error
= dmu_recv_new_end(drc
);
4066 error
= dmu_recv_existing_end(drc
);
4069 dmu_recv_cleanup_ds(drc
);
4070 } else if (drc
->drc_guid_to_ds_map
!= NULL
) {
4071 (void) add_ds_to_guidmap(drc
->drc_tofs
, drc
->drc_guid_to_ds_map
,
4072 drc
->drc_newsnapobj
, drc
->drc_raw
);
4078 * Return TRUE if this objset is currently being received into.
4081 dmu_objset_is_receiving(objset_t
*os
)
4083 return (os
->os_dsl_dataset
!= NULL
&&
4084 os
->os_dsl_dataset
->ds_owner
== dmu_recv_tag
);
4087 #if defined(_KERNEL)
4088 module_param(zfs_send_corrupt_data
, int, 0644);
4089 MODULE_PARM_DESC(zfs_send_corrupt_data
, "Allow sending corrupt data");