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 this (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).
118 * Removing the assertions could be rolled into a new feature that uses
119 * data that isn't 8-byte aligned; if the assertions were removed, a
120 * feature flag would have to be added.
123 ASSERT0(dbi
->dbi_len
% 8);
125 dsp
->dsa_err
= vn_rdwr(UIO_WRITE
, dsp
->dsa_vp
,
126 (caddr_t
)dbi
->dbi_buf
, dbi
->dbi_len
,
127 0, UIO_SYSSPACE
, FAPPEND
, RLIM64_INFINITY
, CRED(), &resid
);
129 mutex_enter(&ds
->ds_sendstream_lock
);
130 *dsp
->dsa_off
+= dbi
->dbi_len
;
131 mutex_exit(&ds
->ds_sendstream_lock
);
135 dump_bytes(dmu_sendarg_t
*dsp
, void *buf
, int len
)
143 #if defined(HAVE_LARGE_STACKS)
147 * The vn_rdwr() call is performed in a taskq to ensure that there is
148 * always enough stack space to write safely to the target filesystem.
149 * The ZIO_TYPE_FREE threads are used because there can be a lot of
150 * them and they are used in vdev_file.c for a similar purpose.
152 spa_taskq_dispatch_sync(dmu_objset_spa(dsp
->dsa_os
), ZIO_TYPE_FREE
,
153 ZIO_TASKQ_ISSUE
, dump_bytes_cb
, &dbi
, TQ_SLEEP
);
154 #endif /* HAVE_LARGE_STACKS */
156 return (dsp
->dsa_err
);
160 * For all record types except BEGIN, fill in the checksum (overlaid in
161 * drr_u.drr_checksum.drr_checksum). The checksum verifies everything
162 * up to the start of the checksum itself.
165 dump_record(dmu_sendarg_t
*dsp
, void *payload
, int payload_len
)
167 ASSERT3U(offsetof(dmu_replay_record_t
, drr_u
.drr_checksum
.drr_checksum
),
168 ==, sizeof (dmu_replay_record_t
) - sizeof (zio_cksum_t
));
169 (void) fletcher_4_incremental_native(dsp
->dsa_drr
,
170 offsetof(dmu_replay_record_t
, drr_u
.drr_checksum
.drr_checksum
),
172 if (dsp
->dsa_drr
->drr_type
== DRR_BEGIN
) {
173 dsp
->dsa_sent_begin
= B_TRUE
;
175 ASSERT(ZIO_CHECKSUM_IS_ZERO(&dsp
->dsa_drr
->drr_u
.
176 drr_checksum
.drr_checksum
));
177 dsp
->dsa_drr
->drr_u
.drr_checksum
.drr_checksum
= dsp
->dsa_zc
;
179 if (dsp
->dsa_drr
->drr_type
== DRR_END
) {
180 dsp
->dsa_sent_end
= B_TRUE
;
182 (void) fletcher_4_incremental_native(&dsp
->dsa_drr
->
183 drr_u
.drr_checksum
.drr_checksum
,
184 sizeof (zio_cksum_t
), &dsp
->dsa_zc
);
185 if (dump_bytes(dsp
, dsp
->dsa_drr
, sizeof (dmu_replay_record_t
)) != 0)
186 return (SET_ERROR(EINTR
));
187 if (payload_len
!= 0) {
188 (void) fletcher_4_incremental_native(payload
, payload_len
,
190 if (dump_bytes(dsp
, payload
, payload_len
) != 0)
191 return (SET_ERROR(EINTR
));
197 * Fill in the drr_free struct, or perform aggregation if the previous record is
198 * also a free record, and the two are adjacent.
200 * Note that we send free records even for a full send, because we want to be
201 * able to receive a full send as a clone, which requires a list of all the free
202 * and freeobject records that were generated on the source.
205 dump_free(dmu_sendarg_t
*dsp
, uint64_t object
, uint64_t offset
,
208 struct drr_free
*drrf
= &(dsp
->dsa_drr
->drr_u
.drr_free
);
211 * When we receive a free record, dbuf_free_range() assumes
212 * that the receiving system doesn't have any dbufs in the range
213 * being freed. This is always true because there is a one-record
214 * constraint: we only send one WRITE record for any given
215 * object,offset. We know that the one-record constraint is
216 * true because we always send data in increasing order by
219 * If the increasing-order constraint ever changes, we should find
220 * another way to assert that the one-record constraint is still
223 ASSERT(object
> dsp
->dsa_last_data_object
||
224 (object
== dsp
->dsa_last_data_object
&&
225 offset
> dsp
->dsa_last_data_offset
));
227 if (length
!= -1ULL && offset
+ length
< offset
)
231 * If there is a pending op, but it's not PENDING_FREE, push it out,
232 * since free block aggregation can only be done for blocks of the
233 * same type (i.e., DRR_FREE records can only be aggregated with
234 * other DRR_FREE records. DRR_FREEOBJECTS records can only be
235 * aggregated with other DRR_FREEOBJECTS records.
237 if (dsp
->dsa_pending_op
!= PENDING_NONE
&&
238 dsp
->dsa_pending_op
!= PENDING_FREE
) {
239 if (dump_record(dsp
, NULL
, 0) != 0)
240 return (SET_ERROR(EINTR
));
241 dsp
->dsa_pending_op
= PENDING_NONE
;
244 if (dsp
->dsa_pending_op
== PENDING_FREE
) {
246 * There should never be a PENDING_FREE if length is -1
247 * (because dump_dnode is the only place where this
248 * function is called with a -1, and only after flushing
249 * any pending record).
251 ASSERT(length
!= -1ULL);
253 * Check to see whether this free block can be aggregated
256 if (drrf
->drr_object
== object
&& drrf
->drr_offset
+
257 drrf
->drr_length
== offset
) {
258 drrf
->drr_length
+= length
;
261 /* not a continuation. Push out pending record */
262 if (dump_record(dsp
, NULL
, 0) != 0)
263 return (SET_ERROR(EINTR
));
264 dsp
->dsa_pending_op
= PENDING_NONE
;
267 /* create a FREE record and make it pending */
268 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
269 dsp
->dsa_drr
->drr_type
= DRR_FREE
;
270 drrf
->drr_object
= object
;
271 drrf
->drr_offset
= offset
;
272 drrf
->drr_length
= length
;
273 drrf
->drr_toguid
= dsp
->dsa_toguid
;
274 if (length
== -1ULL) {
275 if (dump_record(dsp
, NULL
, 0) != 0)
276 return (SET_ERROR(EINTR
));
278 dsp
->dsa_pending_op
= PENDING_FREE
;
285 dump_write(dmu_sendarg_t
*dsp
, dmu_object_type_t type
,
286 uint64_t object
, uint64_t offset
, int lsize
, int psize
, const blkptr_t
*bp
,
289 uint64_t payload_size
;
290 struct drr_write
*drrw
= &(dsp
->dsa_drr
->drr_u
.drr_write
);
293 * We send data in increasing object, offset order.
294 * See comment in dump_free() for details.
296 ASSERT(object
> dsp
->dsa_last_data_object
||
297 (object
== dsp
->dsa_last_data_object
&&
298 offset
> dsp
->dsa_last_data_offset
));
299 dsp
->dsa_last_data_object
= object
;
300 dsp
->dsa_last_data_offset
= offset
+ lsize
- 1;
303 * If there is any kind of pending aggregation (currently either
304 * a grouping of free objects or free blocks), push it out to
305 * the stream, since aggregation can't be done across operations
306 * of different types.
308 if (dsp
->dsa_pending_op
!= PENDING_NONE
) {
309 if (dump_record(dsp
, NULL
, 0) != 0)
310 return (SET_ERROR(EINTR
));
311 dsp
->dsa_pending_op
= PENDING_NONE
;
313 /* write a WRITE record */
314 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
315 dsp
->dsa_drr
->drr_type
= DRR_WRITE
;
316 drrw
->drr_object
= object
;
317 drrw
->drr_type
= type
;
318 drrw
->drr_offset
= offset
;
319 drrw
->drr_toguid
= dsp
->dsa_toguid
;
320 drrw
->drr_logical_size
= lsize
;
322 /* only set the compression fields if the buf is compressed */
323 if (lsize
!= psize
) {
324 ASSERT(dsp
->dsa_featureflags
& DMU_BACKUP_FEATURE_COMPRESSED
);
325 ASSERT(!BP_IS_EMBEDDED(bp
));
326 ASSERT(!BP_SHOULD_BYTESWAP(bp
));
327 ASSERT(!DMU_OT_IS_METADATA(BP_GET_TYPE(bp
)));
328 ASSERT3U(BP_GET_COMPRESS(bp
), !=, ZIO_COMPRESS_OFF
);
329 ASSERT3S(psize
, >, 0);
330 ASSERT3S(lsize
, >=, psize
);
332 drrw
->drr_compressiontype
= BP_GET_COMPRESS(bp
);
333 drrw
->drr_compressed_size
= psize
;
334 payload_size
= drrw
->drr_compressed_size
;
336 payload_size
= drrw
->drr_logical_size
;
339 if (bp
== NULL
|| BP_IS_EMBEDDED(bp
)) {
341 * There's no pre-computed checksum for partial-block
342 * writes or embedded BP's, so (like
343 * fletcher4-checkummed blocks) userland will have to
344 * compute a dedup-capable checksum itself.
346 drrw
->drr_checksumtype
= ZIO_CHECKSUM_OFF
;
348 drrw
->drr_checksumtype
= BP_GET_CHECKSUM(bp
);
349 if (zio_checksum_table
[drrw
->drr_checksumtype
].ci_flags
&
350 ZCHECKSUM_FLAG_DEDUP
)
351 drrw
->drr_checksumflags
|= DRR_CHECKSUM_DEDUP
;
352 DDK_SET_LSIZE(&drrw
->drr_key
, BP_GET_LSIZE(bp
));
353 DDK_SET_PSIZE(&drrw
->drr_key
, BP_GET_PSIZE(bp
));
354 DDK_SET_COMPRESS(&drrw
->drr_key
, BP_GET_COMPRESS(bp
));
355 drrw
->drr_key
.ddk_cksum
= bp
->blk_cksum
;
358 if (dump_record(dsp
, data
, payload_size
) != 0)
359 return (SET_ERROR(EINTR
));
364 dump_write_embedded(dmu_sendarg_t
*dsp
, uint64_t object
, uint64_t offset
,
365 int blksz
, const blkptr_t
*bp
)
367 char buf
[BPE_PAYLOAD_SIZE
];
368 struct drr_write_embedded
*drrw
=
369 &(dsp
->dsa_drr
->drr_u
.drr_write_embedded
);
371 if (dsp
->dsa_pending_op
!= PENDING_NONE
) {
372 if (dump_record(dsp
, NULL
, 0) != 0)
374 dsp
->dsa_pending_op
= PENDING_NONE
;
377 ASSERT(BP_IS_EMBEDDED(bp
));
379 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
380 dsp
->dsa_drr
->drr_type
= DRR_WRITE_EMBEDDED
;
381 drrw
->drr_object
= object
;
382 drrw
->drr_offset
= offset
;
383 drrw
->drr_length
= blksz
;
384 drrw
->drr_toguid
= dsp
->dsa_toguid
;
385 drrw
->drr_compression
= BP_GET_COMPRESS(bp
);
386 drrw
->drr_etype
= BPE_GET_ETYPE(bp
);
387 drrw
->drr_lsize
= BPE_GET_LSIZE(bp
);
388 drrw
->drr_psize
= BPE_GET_PSIZE(bp
);
390 decode_embedded_bp_compressed(bp
, buf
);
392 if (dump_record(dsp
, buf
, P2ROUNDUP(drrw
->drr_psize
, 8)) != 0)
398 dump_spill(dmu_sendarg_t
*dsp
, uint64_t object
, int blksz
, void *data
)
400 struct drr_spill
*drrs
= &(dsp
->dsa_drr
->drr_u
.drr_spill
);
402 if (dsp
->dsa_pending_op
!= PENDING_NONE
) {
403 if (dump_record(dsp
, NULL
, 0) != 0)
404 return (SET_ERROR(EINTR
));
405 dsp
->dsa_pending_op
= PENDING_NONE
;
408 /* write a SPILL record */
409 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
410 dsp
->dsa_drr
->drr_type
= DRR_SPILL
;
411 drrs
->drr_object
= object
;
412 drrs
->drr_length
= blksz
;
413 drrs
->drr_toguid
= dsp
->dsa_toguid
;
415 if (dump_record(dsp
, data
, blksz
) != 0)
416 return (SET_ERROR(EINTR
));
421 dump_freeobjects(dmu_sendarg_t
*dsp
, uint64_t firstobj
, uint64_t numobjs
)
423 struct drr_freeobjects
*drrfo
= &(dsp
->dsa_drr
->drr_u
.drr_freeobjects
);
424 uint64_t maxobj
= DNODES_PER_BLOCK
*
425 (DMU_META_DNODE(dsp
->dsa_os
)->dn_maxblkid
+ 1);
428 * ZoL < 0.7 does not handle large FREEOBJECTS records correctly,
429 * leading to zfs recv never completing. to avoid this issue, don't
430 * send FREEOBJECTS records for object IDs which cannot exist on the
434 if (maxobj
< firstobj
)
437 if (maxobj
< firstobj
+ numobjs
)
438 numobjs
= maxobj
- firstobj
;
442 * If there is a pending op, but it's not PENDING_FREEOBJECTS,
443 * push it out, since free block aggregation can only be done for
444 * blocks of the same type (i.e., DRR_FREE records can only be
445 * aggregated with other DRR_FREE records. DRR_FREEOBJECTS records
446 * can only be aggregated with other DRR_FREEOBJECTS records.
448 if (dsp
->dsa_pending_op
!= PENDING_NONE
&&
449 dsp
->dsa_pending_op
!= PENDING_FREEOBJECTS
) {
450 if (dump_record(dsp
, NULL
, 0) != 0)
451 return (SET_ERROR(EINTR
));
452 dsp
->dsa_pending_op
= PENDING_NONE
;
454 if (dsp
->dsa_pending_op
== PENDING_FREEOBJECTS
) {
456 * See whether this free object array can be aggregated
459 if (drrfo
->drr_firstobj
+ drrfo
->drr_numobjs
== firstobj
) {
460 drrfo
->drr_numobjs
+= numobjs
;
463 /* can't be aggregated. Push out pending record */
464 if (dump_record(dsp
, NULL
, 0) != 0)
465 return (SET_ERROR(EINTR
));
466 dsp
->dsa_pending_op
= PENDING_NONE
;
470 /* write a FREEOBJECTS record */
471 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
472 dsp
->dsa_drr
->drr_type
= DRR_FREEOBJECTS
;
473 drrfo
->drr_firstobj
= firstobj
;
474 drrfo
->drr_numobjs
= numobjs
;
475 drrfo
->drr_toguid
= dsp
->dsa_toguid
;
477 dsp
->dsa_pending_op
= PENDING_FREEOBJECTS
;
483 dump_dnode(dmu_sendarg_t
*dsp
, uint64_t object
, dnode_phys_t
*dnp
)
485 struct drr_object
*drro
= &(dsp
->dsa_drr
->drr_u
.drr_object
);
487 if (object
< dsp
->dsa_resume_object
) {
489 * Note: when resuming, we will visit all the dnodes in
490 * the block of dnodes that we are resuming from. In
491 * this case it's unnecessary to send the dnodes prior to
492 * the one we are resuming from. We should be at most one
493 * block's worth of dnodes behind the resume point.
495 ASSERT3U(dsp
->dsa_resume_object
- object
, <,
496 1 << (DNODE_BLOCK_SHIFT
- DNODE_SHIFT
));
500 if (dnp
== NULL
|| dnp
->dn_type
== DMU_OT_NONE
)
501 return (dump_freeobjects(dsp
, object
, 1));
503 if (dsp
->dsa_pending_op
!= PENDING_NONE
) {
504 if (dump_record(dsp
, NULL
, 0) != 0)
505 return (SET_ERROR(EINTR
));
506 dsp
->dsa_pending_op
= PENDING_NONE
;
509 /* write an OBJECT record */
510 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
511 dsp
->dsa_drr
->drr_type
= DRR_OBJECT
;
512 drro
->drr_object
= object
;
513 drro
->drr_type
= dnp
->dn_type
;
514 drro
->drr_bonustype
= dnp
->dn_bonustype
;
515 drro
->drr_blksz
= dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
;
516 drro
->drr_bonuslen
= dnp
->dn_bonuslen
;
517 drro
->drr_dn_slots
= dnp
->dn_extra_slots
+ 1;
518 drro
->drr_checksumtype
= dnp
->dn_checksum
;
519 drro
->drr_compress
= dnp
->dn_compress
;
520 drro
->drr_toguid
= dsp
->dsa_toguid
;
522 if (!(dsp
->dsa_featureflags
& DMU_BACKUP_FEATURE_LARGE_BLOCKS
) &&
523 drro
->drr_blksz
> SPA_OLD_MAXBLOCKSIZE
)
524 drro
->drr_blksz
= SPA_OLD_MAXBLOCKSIZE
;
526 if (dump_record(dsp
, DN_BONUS(dnp
),
527 P2ROUNDUP(dnp
->dn_bonuslen
, 8)) != 0) {
528 return (SET_ERROR(EINTR
));
531 /* Free anything past the end of the file. */
532 if (dump_free(dsp
, object
, (dnp
->dn_maxblkid
+ 1) *
533 (dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
), -1ULL) != 0)
534 return (SET_ERROR(EINTR
));
535 if (dsp
->dsa_err
!= 0)
536 return (SET_ERROR(EINTR
));
541 backup_do_embed(dmu_sendarg_t
*dsp
, const blkptr_t
*bp
)
543 if (!BP_IS_EMBEDDED(bp
))
547 * Compression function must be legacy, or explicitly enabled.
549 if ((BP_GET_COMPRESS(bp
) >= ZIO_COMPRESS_LEGACY_FUNCTIONS
&&
550 !(dsp
->dsa_featureflags
& DMU_BACKUP_FEATURE_LZ4
)))
554 * Embed type must be explicitly enabled.
556 switch (BPE_GET_ETYPE(bp
)) {
557 case BP_EMBEDDED_TYPE_DATA
:
558 if (dsp
->dsa_featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA
)
568 * This is the callback function to traverse_dataset that acts as the worker
569 * thread for dmu_send_impl.
573 send_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
574 const zbookmark_phys_t
*zb
, const struct dnode_phys
*dnp
, void *arg
)
576 struct send_thread_arg
*sta
= arg
;
577 struct send_block_record
*record
;
578 uint64_t record_size
;
581 ASSERT(zb
->zb_object
== DMU_META_DNODE_OBJECT
||
582 zb
->zb_object
>= sta
->resume
.zb_object
);
585 return (SET_ERROR(EINTR
));
588 ASSERT3U(zb
->zb_level
, ==, ZB_DNODE_LEVEL
);
590 } else if (zb
->zb_level
< 0) {
594 record
= kmem_zalloc(sizeof (struct send_block_record
), KM_SLEEP
);
595 record
->eos_marker
= B_FALSE
;
598 record
->indblkshift
= dnp
->dn_indblkshift
;
599 record
->datablkszsec
= dnp
->dn_datablkszsec
;
600 record_size
= dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
;
601 bqueue_enqueue(&sta
->q
, record
, record_size
);
607 * This function kicks off the traverse_dataset. It also handles setting the
608 * error code of the thread in case something goes wrong, and pushes the End of
609 * Stream record when the traverse_dataset call has finished. If there is no
610 * dataset to traverse, the thread immediately pushes End of Stream marker.
613 send_traverse_thread(void *arg
)
615 struct send_thread_arg
*st_arg
= arg
;
617 struct send_block_record
*data
;
618 fstrans_cookie_t cookie
= spl_fstrans_mark();
620 if (st_arg
->ds
!= NULL
) {
621 err
= traverse_dataset_resume(st_arg
->ds
,
622 st_arg
->fromtxg
, &st_arg
->resume
,
623 st_arg
->flags
, send_cb
, st_arg
);
626 st_arg
->error_code
= err
;
628 data
= kmem_zalloc(sizeof (*data
), KM_SLEEP
);
629 data
->eos_marker
= B_TRUE
;
630 bqueue_enqueue(&st_arg
->q
, data
, 1);
631 spl_fstrans_unmark(cookie
);
636 * This function actually handles figuring out what kind of record needs to be
637 * dumped, reading the data (which has hopefully been prefetched), and calling
638 * the appropriate helper function.
641 do_dump(dmu_sendarg_t
*dsa
, struct send_block_record
*data
)
643 dsl_dataset_t
*ds
= dmu_objset_ds(dsa
->dsa_os
);
644 const blkptr_t
*bp
= &data
->bp
;
645 const zbookmark_phys_t
*zb
= &data
->zb
;
646 uint8_t indblkshift
= data
->indblkshift
;
647 uint16_t dblkszsec
= data
->datablkszsec
;
648 spa_t
*spa
= ds
->ds_dir
->dd_pool
->dp_spa
;
649 dmu_object_type_t type
= bp
? BP_GET_TYPE(bp
) : DMU_OT_NONE
;
653 ASSERT3U(zb
->zb_level
, >=, 0);
655 ASSERT(zb
->zb_object
== DMU_META_DNODE_OBJECT
||
656 zb
->zb_object
>= dsa
->dsa_resume_object
);
658 if (zb
->zb_object
!= DMU_META_DNODE_OBJECT
&&
659 DMU_OBJECT_IS_SPECIAL(zb
->zb_object
)) {
661 } else if (BP_IS_HOLE(bp
) &&
662 zb
->zb_object
== DMU_META_DNODE_OBJECT
) {
663 uint64_t span
= BP_SPAN(dblkszsec
, indblkshift
, zb
->zb_level
);
664 uint64_t dnobj
= (zb
->zb_blkid
* span
) >> DNODE_SHIFT
;
665 err
= dump_freeobjects(dsa
, dnobj
, span
>> DNODE_SHIFT
);
666 } else if (BP_IS_HOLE(bp
)) {
667 uint64_t span
= BP_SPAN(dblkszsec
, indblkshift
, zb
->zb_level
);
668 uint64_t offset
= zb
->zb_blkid
* span
;
669 err
= dump_free(dsa
, zb
->zb_object
, offset
, span
);
670 } else if (zb
->zb_level
> 0 || type
== DMU_OT_OBJSET
) {
672 } else if (type
== DMU_OT_DNODE
) {
674 int epb
= BP_GET_LSIZE(bp
) >> DNODE_SHIFT
;
675 arc_flags_t aflags
= ARC_FLAG_WAIT
;
679 ASSERT0(zb
->zb_level
);
681 if (arc_read(NULL
, spa
, bp
, arc_getbuf_func
, &abuf
,
682 ZIO_PRIORITY_ASYNC_READ
, ZIO_FLAG_CANFAIL
,
684 return (SET_ERROR(EIO
));
687 dnobj
= zb
->zb_blkid
* epb
;
688 for (i
= 0; i
< epb
; i
+= blk
[i
].dn_extra_slots
+ 1) {
689 err
= dump_dnode(dsa
, dnobj
+ i
, blk
+ i
);
693 arc_buf_destroy(abuf
, &abuf
);
694 } else if (type
== DMU_OT_SA
) {
695 arc_flags_t aflags
= ARC_FLAG_WAIT
;
697 int blksz
= BP_GET_LSIZE(bp
);
699 if (arc_read(NULL
, spa
, bp
, arc_getbuf_func
, &abuf
,
700 ZIO_PRIORITY_ASYNC_READ
, ZIO_FLAG_CANFAIL
,
702 return (SET_ERROR(EIO
));
704 err
= dump_spill(dsa
, zb
->zb_object
, blksz
, abuf
->b_data
);
705 arc_buf_destroy(abuf
, &abuf
);
706 } else if (backup_do_embed(dsa
, bp
)) {
707 /* it's an embedded level-0 block of a regular object */
708 int blksz
= dblkszsec
<< SPA_MINBLOCKSHIFT
;
709 ASSERT0(zb
->zb_level
);
710 err
= dump_write_embedded(dsa
, zb
->zb_object
,
711 zb
->zb_blkid
* blksz
, blksz
, bp
);
713 /* it's a level-0 block of a regular object */
714 arc_flags_t aflags
= ARC_FLAG_WAIT
;
716 int blksz
= dblkszsec
<< SPA_MINBLOCKSHIFT
;
720 * If we have large blocks stored on disk but the send flags
721 * don't allow us to send large blocks, we split the data from
722 * the arc buf into chunks.
724 boolean_t split_large_blocks
= blksz
> SPA_OLD_MAXBLOCKSIZE
&&
725 !(dsa
->dsa_featureflags
& DMU_BACKUP_FEATURE_LARGE_BLOCKS
);
727 * We should only request compressed data from the ARC if all
728 * the following are true:
729 * - stream compression was requested
730 * - we aren't splitting large blocks into smaller chunks
731 * - the data won't need to be byteswapped before sending
732 * - this isn't an embedded block
733 * - this isn't metadata (if receiving on a different endian
734 * system it can be byteswapped more easily)
736 boolean_t request_compressed
=
737 (dsa
->dsa_featureflags
& DMU_BACKUP_FEATURE_COMPRESSED
) &&
738 !split_large_blocks
&& !BP_SHOULD_BYTESWAP(bp
) &&
739 !BP_IS_EMBEDDED(bp
) && !DMU_OT_IS_METADATA(BP_GET_TYPE(bp
));
741 ASSERT0(zb
->zb_level
);
742 ASSERT(zb
->zb_object
> dsa
->dsa_resume_object
||
743 (zb
->zb_object
== dsa
->dsa_resume_object
&&
744 zb
->zb_blkid
* blksz
>= dsa
->dsa_resume_offset
));
746 ASSERT3U(blksz
, ==, BP_GET_LSIZE(bp
));
748 enum zio_flag zioflags
= ZIO_FLAG_CANFAIL
;
749 if (request_compressed
)
750 zioflags
|= ZIO_FLAG_RAW
;
752 if (arc_read(NULL
, spa
, bp
, arc_getbuf_func
, &abuf
,
753 ZIO_PRIORITY_ASYNC_READ
, zioflags
, &aflags
, zb
) != 0) {
754 if (zfs_send_corrupt_data
) {
755 /* Send a block filled with 0x"zfs badd bloc" */
756 abuf
= arc_alloc_buf(spa
, &abuf
, ARC_BUFC_DATA
,
759 for (ptr
= abuf
->b_data
;
760 (char *)ptr
< (char *)abuf
->b_data
+ blksz
;
762 *ptr
= 0x2f5baddb10cULL
;
764 return (SET_ERROR(EIO
));
768 offset
= zb
->zb_blkid
* blksz
;
770 if (split_large_blocks
) {
771 ASSERT3U(arc_get_compression(abuf
), ==,
773 char *buf
= abuf
->b_data
;
774 while (blksz
> 0 && err
== 0) {
775 int n
= MIN(blksz
, SPA_OLD_MAXBLOCKSIZE
);
776 err
= dump_write(dsa
, type
, zb
->zb_object
,
777 offset
, n
, n
, NULL
, buf
);
783 err
= dump_write(dsa
, type
, zb
->zb_object
, offset
,
784 blksz
, arc_buf_size(abuf
), bp
,
787 arc_buf_destroy(abuf
, &abuf
);
790 ASSERT(err
== 0 || err
== EINTR
);
795 * Pop the new data off the queue, and free the old data.
797 static struct send_block_record
*
798 get_next_record(bqueue_t
*bq
, struct send_block_record
*data
)
800 struct send_block_record
*tmp
= bqueue_dequeue(bq
);
801 kmem_free(data
, sizeof (*data
));
806 * Actually do the bulk of the work in a zfs send.
808 * Note: Releases dp using the specified tag.
811 dmu_send_impl(void *tag
, dsl_pool_t
*dp
, dsl_dataset_t
*to_ds
,
812 zfs_bookmark_phys_t
*ancestor_zb
, boolean_t is_clone
,
813 boolean_t embedok
, boolean_t large_block_ok
, boolean_t compressok
,
814 int outfd
, uint64_t resumeobj
, uint64_t resumeoff
,
815 vnode_t
*vp
, offset_t
*off
)
818 dmu_replay_record_t
*drr
;
821 uint64_t fromtxg
= 0;
822 uint64_t featureflags
= 0;
823 struct send_thread_arg to_arg
;
824 void *payload
= NULL
;
825 size_t payload_len
= 0;
826 struct send_block_record
*to_data
;
828 err
= dmu_objset_from_ds(to_ds
, &os
);
830 dsl_pool_rele(dp
, tag
);
834 drr
= kmem_zalloc(sizeof (dmu_replay_record_t
), KM_SLEEP
);
835 drr
->drr_type
= DRR_BEGIN
;
836 drr
->drr_u
.drr_begin
.drr_magic
= DMU_BACKUP_MAGIC
;
837 DMU_SET_STREAM_HDRTYPE(drr
->drr_u
.drr_begin
.drr_versioninfo
,
840 bzero(&to_arg
, sizeof (to_arg
));
843 if (dmu_objset_type(os
) == DMU_OST_ZFS
) {
845 if (zfs_get_zplprop(os
, ZFS_PROP_VERSION
, &version
) != 0) {
846 kmem_free(drr
, sizeof (dmu_replay_record_t
));
847 dsl_pool_rele(dp
, tag
);
848 return (SET_ERROR(EINVAL
));
850 if (version
>= ZPL_VERSION_SA
) {
851 featureflags
|= DMU_BACKUP_FEATURE_SA_SPILL
;
856 if (large_block_ok
&& to_ds
->ds_feature_inuse
[SPA_FEATURE_LARGE_BLOCKS
])
857 featureflags
|= DMU_BACKUP_FEATURE_LARGE_BLOCKS
;
858 if (to_ds
->ds_feature_inuse
[SPA_FEATURE_LARGE_DNODE
])
859 featureflags
|= DMU_BACKUP_FEATURE_LARGE_DNODE
;
861 spa_feature_is_active(dp
->dp_spa
, SPA_FEATURE_EMBEDDED_DATA
)) {
862 featureflags
|= DMU_BACKUP_FEATURE_EMBED_DATA
;
865 featureflags
|= DMU_BACKUP_FEATURE_COMPRESSED
;
868 (DMU_BACKUP_FEATURE_EMBED_DATA
| DMU_BACKUP_FEATURE_COMPRESSED
)) !=
869 0 && spa_feature_is_active(dp
->dp_spa
, SPA_FEATURE_LZ4_COMPRESS
)) {
870 featureflags
|= DMU_BACKUP_FEATURE_LZ4
;
873 if (resumeobj
!= 0 || resumeoff
!= 0) {
874 featureflags
|= DMU_BACKUP_FEATURE_RESUMING
;
877 DMU_SET_FEATUREFLAGS(drr
->drr_u
.drr_begin
.drr_versioninfo
,
880 drr
->drr_u
.drr_begin
.drr_creation_time
=
881 dsl_dataset_phys(to_ds
)->ds_creation_time
;
882 drr
->drr_u
.drr_begin
.drr_type
= dmu_objset_type(os
);
884 drr
->drr_u
.drr_begin
.drr_flags
|= DRR_FLAG_CLONE
;
885 drr
->drr_u
.drr_begin
.drr_toguid
= dsl_dataset_phys(to_ds
)->ds_guid
;
886 if (dsl_dataset_phys(to_ds
)->ds_flags
& DS_FLAG_CI_DATASET
)
887 drr
->drr_u
.drr_begin
.drr_flags
|= DRR_FLAG_CI_DATA
;
888 if (zfs_send_set_freerecords_bit
)
889 drr
->drr_u
.drr_begin
.drr_flags
|= DRR_FLAG_FREERECORDS
;
891 if (ancestor_zb
!= NULL
) {
892 drr
->drr_u
.drr_begin
.drr_fromguid
=
893 ancestor_zb
->zbm_guid
;
894 fromtxg
= ancestor_zb
->zbm_creation_txg
;
896 dsl_dataset_name(to_ds
, drr
->drr_u
.drr_begin
.drr_toname
);
897 if (!to_ds
->ds_is_snapshot
) {
898 (void) strlcat(drr
->drr_u
.drr_begin
.drr_toname
, "@--head--",
899 sizeof (drr
->drr_u
.drr_begin
.drr_toname
));
902 dsp
= kmem_zalloc(sizeof (dmu_sendarg_t
), KM_SLEEP
);
906 dsp
->dsa_outfd
= outfd
;
907 dsp
->dsa_proc
= curproc
;
910 dsp
->dsa_toguid
= dsl_dataset_phys(to_ds
)->ds_guid
;
911 dsp
->dsa_pending_op
= PENDING_NONE
;
912 dsp
->dsa_featureflags
= featureflags
;
913 dsp
->dsa_resume_object
= resumeobj
;
914 dsp
->dsa_resume_offset
= resumeoff
;
916 mutex_enter(&to_ds
->ds_sendstream_lock
);
917 list_insert_head(&to_ds
->ds_sendstreams
, dsp
);
918 mutex_exit(&to_ds
->ds_sendstream_lock
);
920 dsl_dataset_long_hold(to_ds
, FTAG
);
921 dsl_pool_rele(dp
, tag
);
923 if (resumeobj
!= 0 || resumeoff
!= 0) {
924 dmu_object_info_t to_doi
;
926 err
= dmu_object_info(os
, resumeobj
, &to_doi
);
929 SET_BOOKMARK(&to_arg
.resume
, to_ds
->ds_object
, resumeobj
, 0,
930 resumeoff
/ to_doi
.doi_data_block_size
);
932 nvl
= fnvlist_alloc();
933 fnvlist_add_uint64(nvl
, "resume_object", resumeobj
);
934 fnvlist_add_uint64(nvl
, "resume_offset", resumeoff
);
935 payload
= fnvlist_pack(nvl
, &payload_len
);
936 drr
->drr_payloadlen
= payload_len
;
940 err
= dump_record(dsp
, payload
, payload_len
);
941 fnvlist_pack_free(payload
, payload_len
);
947 err
= bqueue_init(&to_arg
.q
, zfs_send_queue_length
,
948 offsetof(struct send_block_record
, ln
));
949 to_arg
.error_code
= 0;
950 to_arg
.cancel
= B_FALSE
;
952 to_arg
.fromtxg
= fromtxg
;
953 to_arg
.flags
= TRAVERSE_PRE
| TRAVERSE_PREFETCH
;
954 (void) thread_create(NULL
, 0, send_traverse_thread
, &to_arg
, 0, curproc
,
955 TS_RUN
, minclsyspri
);
957 to_data
= bqueue_dequeue(&to_arg
.q
);
959 while (!to_data
->eos_marker
&& err
== 0) {
960 err
= do_dump(dsp
, to_data
);
961 to_data
= get_next_record(&to_arg
.q
, to_data
);
962 if (issig(JUSTLOOKING
) && issig(FORREAL
))
967 to_arg
.cancel
= B_TRUE
;
968 while (!to_data
->eos_marker
) {
969 to_data
= get_next_record(&to_arg
.q
, to_data
);
972 kmem_free(to_data
, sizeof (*to_data
));
974 bqueue_destroy(&to_arg
.q
);
976 if (err
== 0 && to_arg
.error_code
!= 0)
977 err
= to_arg
.error_code
;
982 if (dsp
->dsa_pending_op
!= PENDING_NONE
)
983 if (dump_record(dsp
, NULL
, 0) != 0)
984 err
= SET_ERROR(EINTR
);
987 if (err
== EINTR
&& dsp
->dsa_err
!= 0)
992 bzero(drr
, sizeof (dmu_replay_record_t
));
993 drr
->drr_type
= DRR_END
;
994 drr
->drr_u
.drr_end
.drr_checksum
= dsp
->dsa_zc
;
995 drr
->drr_u
.drr_end
.drr_toguid
= dsp
->dsa_toguid
;
997 if (dump_record(dsp
, NULL
, 0) != 0)
1001 mutex_enter(&to_ds
->ds_sendstream_lock
);
1002 list_remove(&to_ds
->ds_sendstreams
, dsp
);
1003 mutex_exit(&to_ds
->ds_sendstream_lock
);
1005 VERIFY(err
!= 0 || (dsp
->dsa_sent_begin
&& dsp
->dsa_sent_end
));
1007 kmem_free(drr
, sizeof (dmu_replay_record_t
));
1008 kmem_free(dsp
, sizeof (dmu_sendarg_t
));
1010 dsl_dataset_long_rele(to_ds
, FTAG
);
1016 dmu_send_obj(const char *pool
, uint64_t tosnap
, uint64_t fromsnap
,
1017 boolean_t embedok
, boolean_t large_block_ok
, boolean_t compressok
,
1018 int outfd
, vnode_t
*vp
, offset_t
*off
)
1022 dsl_dataset_t
*fromds
= NULL
;
1025 err
= dsl_pool_hold(pool
, FTAG
, &dp
);
1029 err
= dsl_dataset_hold_obj(dp
, tosnap
, FTAG
, &ds
);
1031 dsl_pool_rele(dp
, FTAG
);
1035 if (fromsnap
!= 0) {
1036 zfs_bookmark_phys_t zb
;
1039 err
= dsl_dataset_hold_obj(dp
, fromsnap
, FTAG
, &fromds
);
1041 dsl_dataset_rele(ds
, FTAG
);
1042 dsl_pool_rele(dp
, FTAG
);
1045 if (!dsl_dataset_is_before(ds
, fromds
, 0))
1046 err
= SET_ERROR(EXDEV
);
1047 zb
.zbm_creation_time
=
1048 dsl_dataset_phys(fromds
)->ds_creation_time
;
1049 zb
.zbm_creation_txg
= dsl_dataset_phys(fromds
)->ds_creation_txg
;
1050 zb
.zbm_guid
= dsl_dataset_phys(fromds
)->ds_guid
;
1051 is_clone
= (fromds
->ds_dir
!= ds
->ds_dir
);
1052 dsl_dataset_rele(fromds
, FTAG
);
1053 err
= dmu_send_impl(FTAG
, dp
, ds
, &zb
, is_clone
,
1054 embedok
, large_block_ok
, compressok
, outfd
, 0, 0, vp
, off
);
1056 err
= dmu_send_impl(FTAG
, dp
, ds
, NULL
, B_FALSE
,
1057 embedok
, large_block_ok
, compressok
, outfd
, 0, 0, vp
, off
);
1059 dsl_dataset_rele(ds
, FTAG
);
1064 dmu_send(const char *tosnap
, const char *fromsnap
, boolean_t embedok
,
1065 boolean_t large_block_ok
, boolean_t compressok
, int outfd
,
1066 uint64_t resumeobj
, uint64_t resumeoff
,
1067 vnode_t
*vp
, offset_t
*off
)
1072 boolean_t owned
= B_FALSE
;
1074 if (fromsnap
!= NULL
&& strpbrk(fromsnap
, "@#") == NULL
)
1075 return (SET_ERROR(EINVAL
));
1077 err
= dsl_pool_hold(tosnap
, FTAG
, &dp
);
1081 if (strchr(tosnap
, '@') == NULL
&& spa_writeable(dp
->dp_spa
)) {
1083 * We are sending a filesystem or volume. Ensure
1084 * that it doesn't change by owning the dataset.
1086 err
= dsl_dataset_own(dp
, tosnap
, FTAG
, &ds
);
1089 err
= dsl_dataset_hold(dp
, tosnap
, FTAG
, &ds
);
1092 dsl_pool_rele(dp
, FTAG
);
1096 if (fromsnap
!= NULL
) {
1097 zfs_bookmark_phys_t zb
;
1098 boolean_t is_clone
= B_FALSE
;
1099 int fsnamelen
= strchr(tosnap
, '@') - tosnap
;
1102 * If the fromsnap is in a different filesystem, then
1103 * mark the send stream as a clone.
1105 if (strncmp(tosnap
, fromsnap
, fsnamelen
) != 0 ||
1106 (fromsnap
[fsnamelen
] != '@' &&
1107 fromsnap
[fsnamelen
] != '#')) {
1111 if (strchr(fromsnap
, '@')) {
1112 dsl_dataset_t
*fromds
;
1113 err
= dsl_dataset_hold(dp
, fromsnap
, FTAG
, &fromds
);
1115 if (!dsl_dataset_is_before(ds
, fromds
, 0))
1116 err
= SET_ERROR(EXDEV
);
1117 zb
.zbm_creation_time
=
1118 dsl_dataset_phys(fromds
)->ds_creation_time
;
1119 zb
.zbm_creation_txg
=
1120 dsl_dataset_phys(fromds
)->ds_creation_txg
;
1121 zb
.zbm_guid
= dsl_dataset_phys(fromds
)->ds_guid
;
1122 is_clone
= (ds
->ds_dir
!= fromds
->ds_dir
);
1123 dsl_dataset_rele(fromds
, FTAG
);
1126 err
= dsl_bookmark_lookup(dp
, fromsnap
, ds
, &zb
);
1129 dsl_dataset_rele(ds
, FTAG
);
1130 dsl_pool_rele(dp
, FTAG
);
1133 err
= dmu_send_impl(FTAG
, dp
, ds
, &zb
, is_clone
,
1134 embedok
, large_block_ok
, compressok
,
1135 outfd
, resumeobj
, resumeoff
, vp
, off
);
1137 err
= dmu_send_impl(FTAG
, dp
, ds
, NULL
, B_FALSE
,
1138 embedok
, large_block_ok
, compressok
,
1139 outfd
, resumeobj
, resumeoff
, vp
, off
);
1142 dsl_dataset_disown(ds
, FTAG
);
1144 dsl_dataset_rele(ds
, FTAG
);
1149 dmu_adjust_send_estimate_for_indirects(dsl_dataset_t
*ds
, uint64_t uncompressed
,
1150 uint64_t compressed
, boolean_t stream_compressed
, uint64_t *sizep
)
1155 * Assume that space (both on-disk and in-stream) is dominated by
1156 * data. We will adjust for indirect blocks and the copies property,
1157 * but ignore per-object space used (eg, dnodes and DRR_OBJECT records).
1160 uint64_t recordsize
;
1161 uint64_t record_count
;
1163 VERIFY0(dmu_objset_from_ds(ds
, &os
));
1165 /* Assume all (uncompressed) blocks are recordsize. */
1166 if (os
->os_phys
->os_type
== DMU_OST_ZVOL
) {
1167 err
= dsl_prop_get_int_ds(ds
,
1168 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
), &recordsize
);
1170 err
= dsl_prop_get_int_ds(ds
,
1171 zfs_prop_to_name(ZFS_PROP_RECORDSIZE
), &recordsize
);
1175 record_count
= uncompressed
/ recordsize
;
1178 * If we're estimating a send size for a compressed stream, use the
1179 * compressed data size to estimate the stream size. Otherwise, use the
1180 * uncompressed data size.
1182 size
= stream_compressed
? compressed
: uncompressed
;
1185 * Subtract out approximate space used by indirect blocks.
1186 * Assume most space is used by data blocks (non-indirect, non-dnode).
1187 * Assume no ditto blocks or internal fragmentation.
1189 * Therefore, space used by indirect blocks is sizeof(blkptr_t) per
1192 size
-= record_count
* sizeof (blkptr_t
);
1194 /* Add in the space for the record associated with each block. */
1195 size
+= record_count
* sizeof (dmu_replay_record_t
);
1203 dmu_send_estimate(dsl_dataset_t
*ds
, dsl_dataset_t
*fromds
,
1204 boolean_t stream_compressed
, uint64_t *sizep
)
1207 uint64_t uncomp
, comp
;
1209 ASSERT(dsl_pool_config_held(ds
->ds_dir
->dd_pool
));
1211 /* tosnap must be a snapshot */
1212 if (!ds
->ds_is_snapshot
)
1213 return (SET_ERROR(EINVAL
));
1215 /* fromsnap, if provided, must be a snapshot */
1216 if (fromds
!= NULL
&& !fromds
->ds_is_snapshot
)
1217 return (SET_ERROR(EINVAL
));
1220 * fromsnap must be an earlier snapshot from the same fs as tosnap,
1221 * or the origin's fs.
1223 if (fromds
!= NULL
&& !dsl_dataset_is_before(ds
, fromds
, 0))
1224 return (SET_ERROR(EXDEV
));
1226 /* Get compressed and uncompressed size estimates of changed data. */
1227 if (fromds
== NULL
) {
1228 uncomp
= dsl_dataset_phys(ds
)->ds_uncompressed_bytes
;
1229 comp
= dsl_dataset_phys(ds
)->ds_compressed_bytes
;
1232 err
= dsl_dataset_space_written(fromds
, ds
,
1233 &used
, &comp
, &uncomp
);
1238 err
= dmu_adjust_send_estimate_for_indirects(ds
, uncomp
, comp
,
1239 stream_compressed
, sizep
);
1241 * Add the size of the BEGIN and END records to the estimate.
1243 *sizep
+= 2 * sizeof (dmu_replay_record_t
);
1247 struct calculate_send_arg
{
1248 uint64_t uncompressed
;
1249 uint64_t compressed
;
1253 * Simple callback used to traverse the blocks of a snapshot and sum their
1254 * uncompressed and compressed sizes.
1258 dmu_calculate_send_traversal(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
1259 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
1261 struct calculate_send_arg
*space
= arg
;
1262 if (bp
!= NULL
&& !BP_IS_HOLE(bp
)) {
1263 space
->uncompressed
+= BP_GET_UCSIZE(bp
);
1264 space
->compressed
+= BP_GET_PSIZE(bp
);
1270 * Given a desination snapshot and a TXG, calculate the approximate size of a
1271 * send stream sent from that TXG. from_txg may be zero, indicating that the
1272 * whole snapshot will be sent.
1275 dmu_send_estimate_from_txg(dsl_dataset_t
*ds
, uint64_t from_txg
,
1276 boolean_t stream_compressed
, uint64_t *sizep
)
1279 struct calculate_send_arg size
= { 0 };
1281 ASSERT(dsl_pool_config_held(ds
->ds_dir
->dd_pool
));
1283 /* tosnap must be a snapshot */
1284 if (!dsl_dataset_is_snapshot(ds
))
1285 return (SET_ERROR(EINVAL
));
1287 /* verify that from_txg is before the provided snapshot was taken */
1288 if (from_txg
>= dsl_dataset_phys(ds
)->ds_creation_txg
) {
1289 return (SET_ERROR(EXDEV
));
1292 * traverse the blocks of the snapshot with birth times after
1293 * from_txg, summing their uncompressed size
1295 err
= traverse_dataset(ds
, from_txg
, TRAVERSE_POST
,
1296 dmu_calculate_send_traversal
, &size
);
1301 err
= dmu_adjust_send_estimate_for_indirects(ds
, size
.uncompressed
,
1302 size
.compressed
, stream_compressed
, sizep
);
1306 typedef struct dmu_recv_begin_arg
{
1307 const char *drba_origin
;
1308 dmu_recv_cookie_t
*drba_cookie
;
1310 uint64_t drba_snapobj
;
1311 } dmu_recv_begin_arg_t
;
1314 recv_begin_check_existing_impl(dmu_recv_begin_arg_t
*drba
, dsl_dataset_t
*ds
,
1319 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
1321 /* temporary clone name must not exist */
1322 error
= zap_lookup(dp
->dp_meta_objset
,
1323 dsl_dir_phys(ds
->ds_dir
)->dd_child_dir_zapobj
, recv_clone_name
,
1325 if (error
!= ENOENT
)
1326 return (error
== 0 ? EBUSY
: error
);
1328 /* new snapshot name must not exist */
1329 error
= zap_lookup(dp
->dp_meta_objset
,
1330 dsl_dataset_phys(ds
)->ds_snapnames_zapobj
,
1331 drba
->drba_cookie
->drc_tosnap
, 8, 1, &val
);
1332 if (error
!= ENOENT
)
1333 return (error
== 0 ? EEXIST
: error
);
1336 * Check snapshot limit before receiving. We'll recheck again at the
1337 * end, but might as well abort before receiving if we're already over
1340 * Note that we do not check the file system limit with
1341 * dsl_dir_fscount_check because the temporary %clones don't count
1342 * against that limit.
1344 error
= dsl_fs_ss_limit_check(ds
->ds_dir
, 1, ZFS_PROP_SNAPSHOT_LIMIT
,
1345 NULL
, drba
->drba_cred
);
1349 if (fromguid
!= 0) {
1350 dsl_dataset_t
*snap
;
1351 uint64_t obj
= dsl_dataset_phys(ds
)->ds_prev_snap_obj
;
1353 /* Find snapshot in this dir that matches fromguid. */
1355 error
= dsl_dataset_hold_obj(dp
, obj
, FTAG
,
1358 return (SET_ERROR(ENODEV
));
1359 if (snap
->ds_dir
!= ds
->ds_dir
) {
1360 dsl_dataset_rele(snap
, FTAG
);
1361 return (SET_ERROR(ENODEV
));
1363 if (dsl_dataset_phys(snap
)->ds_guid
== fromguid
)
1365 obj
= dsl_dataset_phys(snap
)->ds_prev_snap_obj
;
1366 dsl_dataset_rele(snap
, FTAG
);
1369 return (SET_ERROR(ENODEV
));
1371 if (drba
->drba_cookie
->drc_force
) {
1372 drba
->drba_snapobj
= obj
;
1375 * If we are not forcing, there must be no
1376 * changes since fromsnap.
1378 if (dsl_dataset_modified_since_snap(ds
, snap
)) {
1379 dsl_dataset_rele(snap
, FTAG
);
1380 return (SET_ERROR(ETXTBSY
));
1382 drba
->drba_snapobj
= ds
->ds_prev
->ds_object
;
1385 dsl_dataset_rele(snap
, FTAG
);
1387 /* if full, then must be forced */
1388 if (!drba
->drba_cookie
->drc_force
)
1389 return (SET_ERROR(EEXIST
));
1390 /* start from $ORIGIN@$ORIGIN, if supported */
1391 drba
->drba_snapobj
= dp
->dp_origin_snap
!= NULL
?
1392 dp
->dp_origin_snap
->ds_object
: 0;
1400 dmu_recv_begin_check(void *arg
, dmu_tx_t
*tx
)
1402 dmu_recv_begin_arg_t
*drba
= arg
;
1403 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1404 struct drr_begin
*drrb
= drba
->drba_cookie
->drc_drrb
;
1405 uint64_t fromguid
= drrb
->drr_fromguid
;
1406 int flags
= drrb
->drr_flags
;
1408 uint64_t featureflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
1410 const char *tofs
= drba
->drba_cookie
->drc_tofs
;
1412 /* already checked */
1413 ASSERT3U(drrb
->drr_magic
, ==, DMU_BACKUP_MAGIC
);
1414 ASSERT(!(featureflags
& DMU_BACKUP_FEATURE_RESUMING
));
1416 if (DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) ==
1417 DMU_COMPOUNDSTREAM
||
1418 drrb
->drr_type
>= DMU_OST_NUMTYPES
||
1419 ((flags
& DRR_FLAG_CLONE
) && drba
->drba_origin
== NULL
))
1420 return (SET_ERROR(EINVAL
));
1422 /* Verify pool version supports SA if SA_SPILL feature set */
1423 if ((featureflags
& DMU_BACKUP_FEATURE_SA_SPILL
) &&
1424 spa_version(dp
->dp_spa
) < SPA_VERSION_SA
)
1425 return (SET_ERROR(ENOTSUP
));
1427 if (drba
->drba_cookie
->drc_resumable
&&
1428 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_EXTENSIBLE_DATASET
))
1429 return (SET_ERROR(ENOTSUP
));
1432 * The receiving code doesn't know how to translate a WRITE_EMBEDDED
1433 * record to a plain WRITE record, so the pool must have the
1434 * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
1435 * records. Same with WRITE_EMBEDDED records that use LZ4 compression.
1437 if ((featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA
) &&
1438 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_EMBEDDED_DATA
))
1439 return (SET_ERROR(ENOTSUP
));
1440 if ((featureflags
& DMU_BACKUP_FEATURE_LZ4
) &&
1441 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_LZ4_COMPRESS
))
1442 return (SET_ERROR(ENOTSUP
));
1445 * The receiving code doesn't know how to translate large blocks
1446 * to smaller ones, so the pool must have the LARGE_BLOCKS
1447 * feature enabled if the stream has LARGE_BLOCKS. Same with
1450 if ((featureflags
& DMU_BACKUP_FEATURE_LARGE_BLOCKS
) &&
1451 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_LARGE_BLOCKS
))
1452 return (SET_ERROR(ENOTSUP
));
1453 if ((featureflags
& DMU_BACKUP_FEATURE_LARGE_DNODE
) &&
1454 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_LARGE_DNODE
))
1455 return (SET_ERROR(ENOTSUP
));
1457 error
= dsl_dataset_hold(dp
, tofs
, FTAG
, &ds
);
1459 /* target fs already exists; recv into temp clone */
1461 /* Can't recv a clone into an existing fs */
1462 if (flags
& DRR_FLAG_CLONE
|| drba
->drba_origin
) {
1463 dsl_dataset_rele(ds
, FTAG
);
1464 return (SET_ERROR(EINVAL
));
1467 error
= recv_begin_check_existing_impl(drba
, ds
, fromguid
);
1468 dsl_dataset_rele(ds
, FTAG
);
1469 } else if (error
== ENOENT
) {
1470 /* target fs does not exist; must be a full backup or clone */
1471 char buf
[ZFS_MAX_DATASET_NAME_LEN
];
1474 * If it's a non-clone incremental, we are missing the
1475 * target fs, so fail the recv.
1477 if (fromguid
!= 0 && !(flags
& DRR_FLAG_CLONE
||
1479 return (SET_ERROR(ENOENT
));
1482 * If we're receiving a full send as a clone, and it doesn't
1483 * contain all the necessary free records and freeobject
1484 * records, reject it.
1486 if (fromguid
== 0 && drba
->drba_origin
&&
1487 !(flags
& DRR_FLAG_FREERECORDS
))
1488 return (SET_ERROR(EINVAL
));
1490 /* Open the parent of tofs */
1491 ASSERT3U(strlen(tofs
), <, sizeof (buf
));
1492 (void) strlcpy(buf
, tofs
, strrchr(tofs
, '/') - tofs
+ 1);
1493 error
= dsl_dataset_hold(dp
, buf
, FTAG
, &ds
);
1498 * Check filesystem and snapshot limits before receiving. We'll
1499 * recheck snapshot limits again at the end (we create the
1500 * filesystems and increment those counts during begin_sync).
1502 error
= dsl_fs_ss_limit_check(ds
->ds_dir
, 1,
1503 ZFS_PROP_FILESYSTEM_LIMIT
, NULL
, drba
->drba_cred
);
1505 dsl_dataset_rele(ds
, FTAG
);
1509 error
= dsl_fs_ss_limit_check(ds
->ds_dir
, 1,
1510 ZFS_PROP_SNAPSHOT_LIMIT
, NULL
, drba
->drba_cred
);
1512 dsl_dataset_rele(ds
, FTAG
);
1516 if (drba
->drba_origin
!= NULL
) {
1517 dsl_dataset_t
*origin
;
1518 error
= dsl_dataset_hold(dp
, drba
->drba_origin
,
1521 dsl_dataset_rele(ds
, FTAG
);
1524 if (!origin
->ds_is_snapshot
) {
1525 dsl_dataset_rele(origin
, FTAG
);
1526 dsl_dataset_rele(ds
, FTAG
);
1527 return (SET_ERROR(EINVAL
));
1529 if (dsl_dataset_phys(origin
)->ds_guid
!= fromguid
&&
1531 dsl_dataset_rele(origin
, FTAG
);
1532 dsl_dataset_rele(ds
, FTAG
);
1533 return (SET_ERROR(ENODEV
));
1535 dsl_dataset_rele(origin
, FTAG
);
1537 dsl_dataset_rele(ds
, FTAG
);
1544 dmu_recv_begin_sync(void *arg
, dmu_tx_t
*tx
)
1546 dmu_recv_begin_arg_t
*drba
= arg
;
1547 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1548 objset_t
*mos
= dp
->dp_meta_objset
;
1549 struct drr_begin
*drrb
= drba
->drba_cookie
->drc_drrb
;
1550 const char *tofs
= drba
->drba_cookie
->drc_tofs
;
1551 dsl_dataset_t
*ds
, *newds
;
1554 uint64_t crflags
= 0;
1556 if (drrb
->drr_flags
& DRR_FLAG_CI_DATA
)
1557 crflags
|= DS_FLAG_CI_DATASET
;
1559 error
= dsl_dataset_hold(dp
, tofs
, FTAG
, &ds
);
1561 /* create temporary clone */
1562 dsl_dataset_t
*snap
= NULL
;
1563 if (drba
->drba_snapobj
!= 0) {
1564 VERIFY0(dsl_dataset_hold_obj(dp
,
1565 drba
->drba_snapobj
, FTAG
, &snap
));
1567 dsobj
= dsl_dataset_create_sync(ds
->ds_dir
, recv_clone_name
,
1568 snap
, crflags
, drba
->drba_cred
, tx
);
1569 if (drba
->drba_snapobj
!= 0)
1570 dsl_dataset_rele(snap
, FTAG
);
1571 dsl_dataset_rele(ds
, FTAG
);
1575 dsl_dataset_t
*origin
= NULL
;
1577 VERIFY0(dsl_dir_hold(dp
, tofs
, FTAG
, &dd
, &tail
));
1579 if (drba
->drba_origin
!= NULL
) {
1580 VERIFY0(dsl_dataset_hold(dp
, drba
->drba_origin
,
1584 /* Create new dataset. */
1585 dsobj
= dsl_dataset_create_sync(dd
,
1586 strrchr(tofs
, '/') + 1,
1587 origin
, crflags
, drba
->drba_cred
, tx
);
1589 dsl_dataset_rele(origin
, FTAG
);
1590 dsl_dir_rele(dd
, FTAG
);
1591 drba
->drba_cookie
->drc_newfs
= B_TRUE
;
1593 VERIFY0(dsl_dataset_own_obj(dp
, dsobj
, dmu_recv_tag
, &newds
));
1595 if (drba
->drba_cookie
->drc_resumable
) {
1599 dsl_dataset_zapify(newds
, tx
);
1600 if (drrb
->drr_fromguid
!= 0) {
1601 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_FROMGUID
,
1602 8, 1, &drrb
->drr_fromguid
, tx
));
1604 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_TOGUID
,
1605 8, 1, &drrb
->drr_toguid
, tx
));
1606 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_TONAME
,
1607 1, strlen(drrb
->drr_toname
) + 1, drrb
->drr_toname
, tx
));
1608 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_OBJECT
,
1610 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_OFFSET
,
1612 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_BYTES
,
1614 if (DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
) &
1615 DMU_BACKUP_FEATURE_LARGE_BLOCKS
) {
1616 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_LARGEBLOCK
,
1619 if (DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
) &
1620 DMU_BACKUP_FEATURE_EMBED_DATA
) {
1621 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_EMBEDOK
,
1624 if (DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
) &
1625 DMU_BACKUP_FEATURE_COMPRESSED
) {
1626 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_COMPRESSOK
,
1631 dmu_buf_will_dirty(newds
->ds_dbuf
, tx
);
1632 dsl_dataset_phys(newds
)->ds_flags
|= DS_FLAG_INCONSISTENT
;
1635 * If we actually created a non-clone, we need to create the
1636 * objset in our new dataset.
1638 rrw_enter(&newds
->ds_bp_rwlock
, RW_READER
, FTAG
);
1639 if (BP_IS_HOLE(dsl_dataset_get_blkptr(newds
))) {
1640 (void) dmu_objset_create_impl(dp
->dp_spa
,
1641 newds
, dsl_dataset_get_blkptr(newds
), drrb
->drr_type
, tx
);
1643 rrw_exit(&newds
->ds_bp_rwlock
, FTAG
);
1645 drba
->drba_cookie
->drc_ds
= newds
;
1647 spa_history_log_internal_ds(newds
, "receive", tx
, "");
1651 dmu_recv_resume_begin_check(void *arg
, dmu_tx_t
*tx
)
1653 dmu_recv_begin_arg_t
*drba
= arg
;
1654 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1655 struct drr_begin
*drrb
= drba
->drba_cookie
->drc_drrb
;
1657 uint64_t featureflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
1659 const char *tofs
= drba
->drba_cookie
->drc_tofs
;
1662 /* 6 extra bytes for /%recv */
1663 char recvname
[ZFS_MAX_DATASET_NAME_LEN
+ 6];
1665 /* already checked */
1666 ASSERT3U(drrb
->drr_magic
, ==, DMU_BACKUP_MAGIC
);
1667 ASSERT(featureflags
& DMU_BACKUP_FEATURE_RESUMING
);
1669 if (DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) ==
1670 DMU_COMPOUNDSTREAM
||
1671 drrb
->drr_type
>= DMU_OST_NUMTYPES
)
1672 return (SET_ERROR(EINVAL
));
1674 /* Verify pool version supports SA if SA_SPILL feature set */
1675 if ((featureflags
& DMU_BACKUP_FEATURE_SA_SPILL
) &&
1676 spa_version(dp
->dp_spa
) < SPA_VERSION_SA
)
1677 return (SET_ERROR(ENOTSUP
));
1680 * The receiving code doesn't know how to translate a WRITE_EMBEDDED
1681 * record to a plain WRITE record, so the pool must have the
1682 * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
1683 * records. Same with WRITE_EMBEDDED records that use LZ4 compression.
1685 if ((featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA
) &&
1686 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_EMBEDDED_DATA
))
1687 return (SET_ERROR(ENOTSUP
));
1688 if ((featureflags
& DMU_BACKUP_FEATURE_LZ4
) &&
1689 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_LZ4_COMPRESS
))
1690 return (SET_ERROR(ENOTSUP
));
1693 * The receiving code doesn't know how to translate large blocks
1694 * to smaller ones, so the pool must have the LARGE_BLOCKS
1695 * feature enabled if the stream has LARGE_BLOCKS. Same with
1698 if ((featureflags
& DMU_BACKUP_FEATURE_LARGE_BLOCKS
) &&
1699 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_LARGE_BLOCKS
))
1700 return (SET_ERROR(ENOTSUP
));
1701 if ((featureflags
& DMU_BACKUP_FEATURE_LARGE_DNODE
) &&
1702 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_LARGE_DNODE
))
1703 return (SET_ERROR(ENOTSUP
));
1705 (void) snprintf(recvname
, sizeof (recvname
), "%s/%s",
1706 tofs
, recv_clone_name
);
1708 if (dsl_dataset_hold(dp
, recvname
, FTAG
, &ds
) != 0) {
1709 /* %recv does not exist; continue in tofs */
1710 error
= dsl_dataset_hold(dp
, tofs
, FTAG
, &ds
);
1715 /* check that ds is marked inconsistent */
1716 if (!DS_IS_INCONSISTENT(ds
)) {
1717 dsl_dataset_rele(ds
, FTAG
);
1718 return (SET_ERROR(EINVAL
));
1721 /* check that there is resuming data, and that the toguid matches */
1722 if (!dsl_dataset_is_zapified(ds
)) {
1723 dsl_dataset_rele(ds
, FTAG
);
1724 return (SET_ERROR(EINVAL
));
1726 error
= zap_lookup(dp
->dp_meta_objset
, ds
->ds_object
,
1727 DS_FIELD_RESUME_TOGUID
, sizeof (val
), 1, &val
);
1728 if (error
!= 0 || drrb
->drr_toguid
!= val
) {
1729 dsl_dataset_rele(ds
, FTAG
);
1730 return (SET_ERROR(EINVAL
));
1734 * Check if the receive is still running. If so, it will be owned.
1735 * Note that nothing else can own the dataset (e.g. after the receive
1736 * fails) because it will be marked inconsistent.
1738 if (dsl_dataset_has_owner(ds
)) {
1739 dsl_dataset_rele(ds
, FTAG
);
1740 return (SET_ERROR(EBUSY
));
1743 /* There should not be any snapshots of this fs yet. */
1744 if (ds
->ds_prev
!= NULL
&& ds
->ds_prev
->ds_dir
== ds
->ds_dir
) {
1745 dsl_dataset_rele(ds
, FTAG
);
1746 return (SET_ERROR(EINVAL
));
1750 * Note: resume point will be checked when we process the first WRITE
1754 /* check that the origin matches */
1756 (void) zap_lookup(dp
->dp_meta_objset
, ds
->ds_object
,
1757 DS_FIELD_RESUME_FROMGUID
, sizeof (val
), 1, &val
);
1758 if (drrb
->drr_fromguid
!= val
) {
1759 dsl_dataset_rele(ds
, FTAG
);
1760 return (SET_ERROR(EINVAL
));
1763 dsl_dataset_rele(ds
, FTAG
);
1768 dmu_recv_resume_begin_sync(void *arg
, dmu_tx_t
*tx
)
1770 dmu_recv_begin_arg_t
*drba
= arg
;
1771 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1772 const char *tofs
= drba
->drba_cookie
->drc_tofs
;
1775 /* 6 extra bytes for /%recv */
1776 char recvname
[ZFS_MAX_DATASET_NAME_LEN
+ 6];
1778 (void) snprintf(recvname
, sizeof (recvname
), "%s/%s",
1779 tofs
, recv_clone_name
);
1781 if (dsl_dataset_hold(dp
, recvname
, FTAG
, &ds
) != 0) {
1782 /* %recv does not exist; continue in tofs */
1783 VERIFY0(dsl_dataset_hold(dp
, tofs
, FTAG
, &ds
));
1784 drba
->drba_cookie
->drc_newfs
= B_TRUE
;
1787 /* clear the inconsistent flag so that we can own it */
1788 ASSERT(DS_IS_INCONSISTENT(ds
));
1789 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
1790 dsl_dataset_phys(ds
)->ds_flags
&= ~DS_FLAG_INCONSISTENT
;
1791 dsobj
= ds
->ds_object
;
1792 dsl_dataset_rele(ds
, FTAG
);
1794 VERIFY0(dsl_dataset_own_obj(dp
, dsobj
, dmu_recv_tag
, &ds
));
1796 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
1797 dsl_dataset_phys(ds
)->ds_flags
|= DS_FLAG_INCONSISTENT
;
1799 rrw_enter(&ds
->ds_bp_rwlock
, RW_READER
, FTAG
);
1800 ASSERT(!BP_IS_HOLE(dsl_dataset_get_blkptr(ds
)));
1801 rrw_exit(&ds
->ds_bp_rwlock
, FTAG
);
1803 drba
->drba_cookie
->drc_ds
= ds
;
1805 spa_history_log_internal_ds(ds
, "resume receive", tx
, "");
1809 * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
1810 * succeeds; otherwise we will leak the holds on the datasets.
1813 dmu_recv_begin(char *tofs
, char *tosnap
, dmu_replay_record_t
*drr_begin
,
1814 boolean_t force
, boolean_t resumable
, char *origin
, dmu_recv_cookie_t
*drc
)
1816 dmu_recv_begin_arg_t drba
= { 0 };
1818 bzero(drc
, sizeof (dmu_recv_cookie_t
));
1819 drc
->drc_drr_begin
= drr_begin
;
1820 drc
->drc_drrb
= &drr_begin
->drr_u
.drr_begin
;
1821 drc
->drc_tosnap
= tosnap
;
1822 drc
->drc_tofs
= tofs
;
1823 drc
->drc_force
= force
;
1824 drc
->drc_resumable
= resumable
;
1825 drc
->drc_cred
= CRED();
1826 drc
->drc_clone
= (origin
!= NULL
);
1828 if (drc
->drc_drrb
->drr_magic
== BSWAP_64(DMU_BACKUP_MAGIC
)) {
1829 drc
->drc_byteswap
= B_TRUE
;
1830 (void) fletcher_4_incremental_byteswap(drr_begin
,
1831 sizeof (dmu_replay_record_t
), &drc
->drc_cksum
);
1832 byteswap_record(drr_begin
);
1833 } else if (drc
->drc_drrb
->drr_magic
== DMU_BACKUP_MAGIC
) {
1834 (void) fletcher_4_incremental_native(drr_begin
,
1835 sizeof (dmu_replay_record_t
), &drc
->drc_cksum
);
1837 return (SET_ERROR(EINVAL
));
1840 drba
.drba_origin
= origin
;
1841 drba
.drba_cookie
= drc
;
1842 drba
.drba_cred
= CRED();
1844 if (DMU_GET_FEATUREFLAGS(drc
->drc_drrb
->drr_versioninfo
) &
1845 DMU_BACKUP_FEATURE_RESUMING
) {
1846 return (dsl_sync_task(tofs
,
1847 dmu_recv_resume_begin_check
, dmu_recv_resume_begin_sync
,
1848 &drba
, 5, ZFS_SPACE_CHECK_NORMAL
));
1850 return (dsl_sync_task(tofs
,
1851 dmu_recv_begin_check
, dmu_recv_begin_sync
,
1852 &drba
, 5, ZFS_SPACE_CHECK_NORMAL
));
1856 struct receive_record_arg
{
1857 dmu_replay_record_t header
;
1858 void *payload
; /* Pointer to a buffer containing the payload */
1860 * If the record is a write, pointer to the arc_buf_t containing the
1863 arc_buf_t
*write_buf
;
1865 uint64_t bytes_read
; /* bytes read from stream when record created */
1866 boolean_t eos_marker
; /* Marks the end of the stream */
1870 struct receive_writer_arg
{
1876 * These three args are used to signal to the main thread that we're
1884 /* A map from guid to dataset to help handle dedup'd streams. */
1885 avl_tree_t
*guid_to_ds_map
;
1886 boolean_t resumable
;
1887 uint64_t last_object
;
1888 uint64_t last_offset
;
1889 uint64_t max_object
; /* highest object ID referenced in stream */
1890 uint64_t bytes_read
; /* bytes read when current record created */
1894 list_t list
; /* List of struct receive_objnode. */
1896 * Last object looked up. Used to assert that objects are being looked
1897 * up in ascending order.
1899 uint64_t last_lookup
;
1902 struct receive_objnode
{
1907 struct receive_arg
{
1909 vnode_t
*vp
; /* The vnode to read the stream from */
1910 uint64_t voff
; /* The current offset in the stream */
1911 uint64_t bytes_read
;
1913 * A record that has had its payload read in, but hasn't yet been handed
1914 * off to the worker thread.
1916 struct receive_record_arg
*rrd
;
1917 /* A record that has had its header read in, but not its payload. */
1918 struct receive_record_arg
*next_rrd
;
1920 zio_cksum_t prev_cksum
;
1923 /* Sorted list of objects not to issue prefetches for. */
1924 struct objlist ignore_objlist
;
1927 typedef struct guid_map_entry
{
1929 dsl_dataset_t
*gme_ds
;
1934 guid_compare(const void *arg1
, const void *arg2
)
1936 const guid_map_entry_t
*gmep1
= (const guid_map_entry_t
*)arg1
;
1937 const guid_map_entry_t
*gmep2
= (const guid_map_entry_t
*)arg2
;
1939 return (AVL_CMP(gmep1
->guid
, gmep2
->guid
));
1943 free_guid_map_onexit(void *arg
)
1945 avl_tree_t
*ca
= arg
;
1946 void *cookie
= NULL
;
1947 guid_map_entry_t
*gmep
;
1949 while ((gmep
= avl_destroy_nodes(ca
, &cookie
)) != NULL
) {
1950 dsl_dataset_long_rele(gmep
->gme_ds
, gmep
);
1951 dsl_dataset_rele(gmep
->gme_ds
, gmep
);
1952 kmem_free(gmep
, sizeof (guid_map_entry_t
));
1955 kmem_free(ca
, sizeof (avl_tree_t
));
1959 receive_read(struct receive_arg
*ra
, int len
, void *buf
)
1964 * The code doesn't rely on this (lengths being multiples of 8). See
1965 * comment in dump_bytes.
1969 while (done
< len
) {
1972 ra
->err
= vn_rdwr(UIO_READ
, ra
->vp
,
1973 (char *)buf
+ done
, len
- done
,
1974 ra
->voff
, UIO_SYSSPACE
, FAPPEND
,
1975 RLIM64_INFINITY
, CRED(), &resid
);
1977 if (resid
== len
- done
) {
1979 * Note: ECKSUM indicates that the receive
1980 * was interrupted and can potentially be resumed.
1982 ra
->err
= SET_ERROR(ECKSUM
);
1984 ra
->voff
+= len
- done
- resid
;
1990 ra
->bytes_read
+= len
;
1992 ASSERT3U(done
, ==, len
);
1996 noinline
static void
1997 byteswap_record(dmu_replay_record_t
*drr
)
1999 #define DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
2000 #define DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
2001 drr
->drr_type
= BSWAP_32(drr
->drr_type
);
2002 drr
->drr_payloadlen
= BSWAP_32(drr
->drr_payloadlen
);
2004 switch (drr
->drr_type
) {
2006 DO64(drr_begin
.drr_magic
);
2007 DO64(drr_begin
.drr_versioninfo
);
2008 DO64(drr_begin
.drr_creation_time
);
2009 DO32(drr_begin
.drr_type
);
2010 DO32(drr_begin
.drr_flags
);
2011 DO64(drr_begin
.drr_toguid
);
2012 DO64(drr_begin
.drr_fromguid
);
2015 DO64(drr_object
.drr_object
);
2016 DO32(drr_object
.drr_type
);
2017 DO32(drr_object
.drr_bonustype
);
2018 DO32(drr_object
.drr_blksz
);
2019 DO32(drr_object
.drr_bonuslen
);
2020 DO64(drr_object
.drr_toguid
);
2022 case DRR_FREEOBJECTS
:
2023 DO64(drr_freeobjects
.drr_firstobj
);
2024 DO64(drr_freeobjects
.drr_numobjs
);
2025 DO64(drr_freeobjects
.drr_toguid
);
2028 DO64(drr_write
.drr_object
);
2029 DO32(drr_write
.drr_type
);
2030 DO64(drr_write
.drr_offset
);
2031 DO64(drr_write
.drr_logical_size
);
2032 DO64(drr_write
.drr_toguid
);
2033 ZIO_CHECKSUM_BSWAP(&drr
->drr_u
.drr_write
.drr_key
.ddk_cksum
);
2034 DO64(drr_write
.drr_key
.ddk_prop
);
2035 DO64(drr_write
.drr_compressed_size
);
2037 case DRR_WRITE_BYREF
:
2038 DO64(drr_write_byref
.drr_object
);
2039 DO64(drr_write_byref
.drr_offset
);
2040 DO64(drr_write_byref
.drr_length
);
2041 DO64(drr_write_byref
.drr_toguid
);
2042 DO64(drr_write_byref
.drr_refguid
);
2043 DO64(drr_write_byref
.drr_refobject
);
2044 DO64(drr_write_byref
.drr_refoffset
);
2045 ZIO_CHECKSUM_BSWAP(&drr
->drr_u
.drr_write_byref
.
2047 DO64(drr_write_byref
.drr_key
.ddk_prop
);
2049 case DRR_WRITE_EMBEDDED
:
2050 DO64(drr_write_embedded
.drr_object
);
2051 DO64(drr_write_embedded
.drr_offset
);
2052 DO64(drr_write_embedded
.drr_length
);
2053 DO64(drr_write_embedded
.drr_toguid
);
2054 DO32(drr_write_embedded
.drr_lsize
);
2055 DO32(drr_write_embedded
.drr_psize
);
2058 DO64(drr_free
.drr_object
);
2059 DO64(drr_free
.drr_offset
);
2060 DO64(drr_free
.drr_length
);
2061 DO64(drr_free
.drr_toguid
);
2064 DO64(drr_spill
.drr_object
);
2065 DO64(drr_spill
.drr_length
);
2066 DO64(drr_spill
.drr_toguid
);
2069 DO64(drr_end
.drr_toguid
);
2070 ZIO_CHECKSUM_BSWAP(&drr
->drr_u
.drr_end
.drr_checksum
);
2076 if (drr
->drr_type
!= DRR_BEGIN
) {
2077 ZIO_CHECKSUM_BSWAP(&drr
->drr_u
.drr_checksum
.drr_checksum
);
2084 static inline uint8_t
2085 deduce_nblkptr(dmu_object_type_t bonus_type
, uint64_t bonus_size
)
2087 if (bonus_type
== DMU_OT_SA
) {
2091 ((DN_OLD_MAX_BONUSLEN
-
2092 MIN(DN_OLD_MAX_BONUSLEN
, bonus_size
)) >> SPA_BLKPTRSHIFT
));
2097 save_resume_state(struct receive_writer_arg
*rwa
,
2098 uint64_t object
, uint64_t offset
, dmu_tx_t
*tx
)
2100 int txgoff
= dmu_tx_get_txg(tx
) & TXG_MASK
;
2102 if (!rwa
->resumable
)
2106 * We use ds_resume_bytes[] != 0 to indicate that we need to
2107 * update this on disk, so it must not be 0.
2109 ASSERT(rwa
->bytes_read
!= 0);
2112 * We only resume from write records, which have a valid
2113 * (non-meta-dnode) object number.
2115 ASSERT(object
!= 0);
2118 * For resuming to work correctly, we must receive records in order,
2119 * sorted by object,offset. This is checked by the callers, but
2120 * assert it here for good measure.
2122 ASSERT3U(object
, >=, rwa
->os
->os_dsl_dataset
->ds_resume_object
[txgoff
]);
2123 ASSERT(object
!= rwa
->os
->os_dsl_dataset
->ds_resume_object
[txgoff
] ||
2124 offset
>= rwa
->os
->os_dsl_dataset
->ds_resume_offset
[txgoff
]);
2125 ASSERT3U(rwa
->bytes_read
, >=,
2126 rwa
->os
->os_dsl_dataset
->ds_resume_bytes
[txgoff
]);
2128 rwa
->os
->os_dsl_dataset
->ds_resume_object
[txgoff
] = object
;
2129 rwa
->os
->os_dsl_dataset
->ds_resume_offset
[txgoff
] = offset
;
2130 rwa
->os
->os_dsl_dataset
->ds_resume_bytes
[txgoff
] = rwa
->bytes_read
;
2134 receive_object(struct receive_writer_arg
*rwa
, struct drr_object
*drro
,
2137 dmu_object_info_t doi
;
2142 if (drro
->drr_type
== DMU_OT_NONE
||
2143 !DMU_OT_IS_VALID(drro
->drr_type
) ||
2144 !DMU_OT_IS_VALID(drro
->drr_bonustype
) ||
2145 drro
->drr_checksumtype
>= ZIO_CHECKSUM_FUNCTIONS
||
2146 drro
->drr_compress
>= ZIO_COMPRESS_FUNCTIONS
||
2147 P2PHASE(drro
->drr_blksz
, SPA_MINBLOCKSIZE
) ||
2148 drro
->drr_blksz
< SPA_MINBLOCKSIZE
||
2149 drro
->drr_blksz
> spa_maxblocksize(dmu_objset_spa(rwa
->os
)) ||
2150 drro
->drr_bonuslen
>
2151 DN_BONUS_SIZE(spa_maxdnodesize(dmu_objset_spa(rwa
->os
))) ||
2152 drro
->drr_dn_slots
>
2153 (spa_maxdnodesize(dmu_objset_spa(rwa
->os
)) >> DNODE_SHIFT
)) {
2154 return (SET_ERROR(EINVAL
));
2157 err
= dmu_object_info(rwa
->os
, drro
->drr_object
, &doi
);
2159 if (err
!= 0 && err
!= ENOENT
)
2160 return (SET_ERROR(EINVAL
));
2161 object
= err
== 0 ? drro
->drr_object
: DMU_NEW_OBJECT
;
2163 if (drro
->drr_object
> rwa
->max_object
)
2164 rwa
->max_object
= drro
->drr_object
;
2167 * If we are losing blkptrs or changing the block size this must
2168 * be a new file instance. We must clear out the previous file
2169 * contents before we can change this type of metadata in the dnode.
2174 nblkptr
= deduce_nblkptr(drro
->drr_bonustype
,
2175 drro
->drr_bonuslen
);
2177 if (drro
->drr_blksz
!= doi
.doi_data_block_size
||
2178 nblkptr
< doi
.doi_nblkptr
) {
2179 err
= dmu_free_long_range(rwa
->os
, drro
->drr_object
,
2182 return (SET_ERROR(EINVAL
));
2186 tx
= dmu_tx_create(rwa
->os
);
2187 dmu_tx_hold_bonus(tx
, object
);
2188 err
= dmu_tx_assign(tx
, TXG_WAIT
);
2194 if (object
== DMU_NEW_OBJECT
) {
2195 /* currently free, want to be allocated */
2196 err
= dmu_object_claim_dnsize(rwa
->os
, drro
->drr_object
,
2197 drro
->drr_type
, drro
->drr_blksz
,
2198 drro
->drr_bonustype
, drro
->drr_bonuslen
,
2199 drro
->drr_dn_slots
<< DNODE_SHIFT
, tx
);
2200 } else if (drro
->drr_type
!= doi
.doi_type
||
2201 drro
->drr_blksz
!= doi
.doi_data_block_size
||
2202 drro
->drr_bonustype
!= doi
.doi_bonus_type
||
2203 drro
->drr_bonuslen
!= doi
.doi_bonus_size
) {
2204 /* currently allocated, but with different properties */
2205 err
= dmu_object_reclaim(rwa
->os
, drro
->drr_object
,
2206 drro
->drr_type
, drro
->drr_blksz
,
2207 drro
->drr_bonustype
, drro
->drr_bonuslen
, tx
);
2211 return (SET_ERROR(EINVAL
));
2214 dmu_object_set_checksum(rwa
->os
, drro
->drr_object
,
2215 drro
->drr_checksumtype
, tx
);
2216 dmu_object_set_compress(rwa
->os
, drro
->drr_object
,
2217 drro
->drr_compress
, tx
);
2222 VERIFY0(dmu_bonus_hold(rwa
->os
, drro
->drr_object
, FTAG
, &db
));
2223 dmu_buf_will_dirty(db
, tx
);
2225 ASSERT3U(db
->db_size
, >=, drro
->drr_bonuslen
);
2226 bcopy(data
, db
->db_data
, drro
->drr_bonuslen
);
2227 if (rwa
->byteswap
) {
2228 dmu_object_byteswap_t byteswap
=
2229 DMU_OT_BYTESWAP(drro
->drr_bonustype
);
2230 dmu_ot_byteswap
[byteswap
].ob_func(db
->db_data
,
2231 drro
->drr_bonuslen
);
2233 dmu_buf_rele(db
, FTAG
);
2242 receive_freeobjects(struct receive_writer_arg
*rwa
,
2243 struct drr_freeobjects
*drrfo
)
2248 if (drrfo
->drr_firstobj
+ drrfo
->drr_numobjs
< drrfo
->drr_firstobj
)
2249 return (SET_ERROR(EINVAL
));
2251 for (obj
= drrfo
->drr_firstobj
== 0 ? 1 : drrfo
->drr_firstobj
;
2252 obj
< drrfo
->drr_firstobj
+ drrfo
->drr_numobjs
&& next_err
== 0;
2253 next_err
= dmu_object_next(rwa
->os
, &obj
, FALSE
, 0)) {
2254 dmu_object_info_t doi
;
2257 err
= dmu_object_info(rwa
->os
, obj
, &doi
);
2263 err
= dmu_free_long_object(rwa
->os
, obj
);
2267 if (obj
> rwa
->max_object
)
2268 rwa
->max_object
= obj
;
2270 if (next_err
!= ESRCH
)
2276 receive_write(struct receive_writer_arg
*rwa
, struct drr_write
*drrw
,
2283 if (drrw
->drr_offset
+ drrw
->drr_logical_size
< drrw
->drr_offset
||
2284 !DMU_OT_IS_VALID(drrw
->drr_type
))
2285 return (SET_ERROR(EINVAL
));
2288 * For resuming to work, records must be in increasing order
2289 * by (object, offset).
2291 if (drrw
->drr_object
< rwa
->last_object
||
2292 (drrw
->drr_object
== rwa
->last_object
&&
2293 drrw
->drr_offset
< rwa
->last_offset
)) {
2294 return (SET_ERROR(EINVAL
));
2296 rwa
->last_object
= drrw
->drr_object
;
2297 rwa
->last_offset
= drrw
->drr_offset
;
2299 if (rwa
->last_object
> rwa
->max_object
)
2300 rwa
->max_object
= rwa
->last_object
;
2302 if (dmu_object_info(rwa
->os
, drrw
->drr_object
, NULL
) != 0)
2303 return (SET_ERROR(EINVAL
));
2305 tx
= dmu_tx_create(rwa
->os
);
2307 dmu_tx_hold_write(tx
, drrw
->drr_object
,
2308 drrw
->drr_offset
, drrw
->drr_logical_size
);
2309 err
= dmu_tx_assign(tx
, TXG_WAIT
);
2314 if (rwa
->byteswap
) {
2315 dmu_object_byteswap_t byteswap
=
2316 DMU_OT_BYTESWAP(drrw
->drr_type
);
2317 dmu_ot_byteswap
[byteswap
].ob_func(abuf
->b_data
,
2318 DRR_WRITE_PAYLOAD_SIZE(drrw
));
2321 /* use the bonus buf to look up the dnode in dmu_assign_arcbuf */
2322 if (dmu_bonus_hold(rwa
->os
, drrw
->drr_object
, FTAG
, &bonus
) != 0)
2323 return (SET_ERROR(EINVAL
));
2324 dmu_assign_arcbuf(bonus
, drrw
->drr_offset
, abuf
, tx
);
2327 * Note: If the receive fails, we want the resume stream to start
2328 * with the same record that we last successfully received (as opposed
2329 * to the next record), so that we can verify that we are
2330 * resuming from the correct location.
2332 save_resume_state(rwa
, drrw
->drr_object
, drrw
->drr_offset
, tx
);
2334 dmu_buf_rele(bonus
, FTAG
);
2340 * Handle a DRR_WRITE_BYREF record. This record is used in dedup'ed
2341 * streams to refer to a copy of the data that is already on the
2342 * system because it came in earlier in the stream. This function
2343 * finds the earlier copy of the data, and uses that copy instead of
2344 * data from the stream to fulfill this write.
2347 receive_write_byref(struct receive_writer_arg
*rwa
,
2348 struct drr_write_byref
*drrwbr
)
2352 guid_map_entry_t gmesrch
;
2353 guid_map_entry_t
*gmep
;
2355 objset_t
*ref_os
= NULL
;
2358 if (drrwbr
->drr_offset
+ drrwbr
->drr_length
< drrwbr
->drr_offset
)
2359 return (SET_ERROR(EINVAL
));
2362 * If the GUID of the referenced dataset is different from the
2363 * GUID of the target dataset, find the referenced dataset.
2365 if (drrwbr
->drr_toguid
!= drrwbr
->drr_refguid
) {
2366 gmesrch
.guid
= drrwbr
->drr_refguid
;
2367 if ((gmep
= avl_find(rwa
->guid_to_ds_map
, &gmesrch
,
2369 return (SET_ERROR(EINVAL
));
2371 if (dmu_objset_from_ds(gmep
->gme_ds
, &ref_os
))
2372 return (SET_ERROR(EINVAL
));
2377 if (drrwbr
->drr_object
> rwa
->max_object
)
2378 rwa
->max_object
= drrwbr
->drr_object
;
2380 err
= dmu_buf_hold(ref_os
, drrwbr
->drr_refobject
,
2381 drrwbr
->drr_refoffset
, FTAG
, &dbp
, DMU_READ_PREFETCH
);
2385 tx
= dmu_tx_create(rwa
->os
);
2387 dmu_tx_hold_write(tx
, drrwbr
->drr_object
,
2388 drrwbr
->drr_offset
, drrwbr
->drr_length
);
2389 err
= dmu_tx_assign(tx
, TXG_WAIT
);
2394 dmu_write(rwa
->os
, drrwbr
->drr_object
,
2395 drrwbr
->drr_offset
, drrwbr
->drr_length
, dbp
->db_data
, tx
);
2396 dmu_buf_rele(dbp
, FTAG
);
2398 /* See comment in restore_write. */
2399 save_resume_state(rwa
, drrwbr
->drr_object
, drrwbr
->drr_offset
, tx
);
2405 receive_write_embedded(struct receive_writer_arg
*rwa
,
2406 struct drr_write_embedded
*drrwe
, void *data
)
2411 if (drrwe
->drr_offset
+ drrwe
->drr_length
< drrwe
->drr_offset
)
2414 if (drrwe
->drr_psize
> BPE_PAYLOAD_SIZE
)
2417 if (drrwe
->drr_etype
>= NUM_BP_EMBEDDED_TYPES
)
2419 if (drrwe
->drr_compression
>= ZIO_COMPRESS_FUNCTIONS
)
2422 if (drrwe
->drr_object
> rwa
->max_object
)
2423 rwa
->max_object
= drrwe
->drr_object
;
2425 tx
= dmu_tx_create(rwa
->os
);
2427 dmu_tx_hold_write(tx
, drrwe
->drr_object
,
2428 drrwe
->drr_offset
, drrwe
->drr_length
);
2429 err
= dmu_tx_assign(tx
, TXG_WAIT
);
2435 dmu_write_embedded(rwa
->os
, drrwe
->drr_object
,
2436 drrwe
->drr_offset
, data
, drrwe
->drr_etype
,
2437 drrwe
->drr_compression
, drrwe
->drr_lsize
, drrwe
->drr_psize
,
2438 rwa
->byteswap
^ ZFS_HOST_BYTEORDER
, tx
);
2440 /* See comment in restore_write. */
2441 save_resume_state(rwa
, drrwe
->drr_object
, drrwe
->drr_offset
, tx
);
2447 receive_spill(struct receive_writer_arg
*rwa
, struct drr_spill
*drrs
,
2451 dmu_buf_t
*db
, *db_spill
;
2454 if (drrs
->drr_length
< SPA_MINBLOCKSIZE
||
2455 drrs
->drr_length
> spa_maxblocksize(dmu_objset_spa(rwa
->os
)))
2456 return (SET_ERROR(EINVAL
));
2458 if (dmu_object_info(rwa
->os
, drrs
->drr_object
, NULL
) != 0)
2459 return (SET_ERROR(EINVAL
));
2461 if (drrs
->drr_object
> rwa
->max_object
)
2462 rwa
->max_object
= drrs
->drr_object
;
2464 VERIFY0(dmu_bonus_hold(rwa
->os
, drrs
->drr_object
, FTAG
, &db
));
2465 if ((err
= dmu_spill_hold_by_bonus(db
, FTAG
, &db_spill
)) != 0) {
2466 dmu_buf_rele(db
, FTAG
);
2470 tx
= dmu_tx_create(rwa
->os
);
2472 dmu_tx_hold_spill(tx
, db
->db_object
);
2474 err
= dmu_tx_assign(tx
, TXG_WAIT
);
2476 dmu_buf_rele(db
, FTAG
);
2477 dmu_buf_rele(db_spill
, FTAG
);
2481 dmu_buf_will_dirty(db_spill
, tx
);
2483 if (db_spill
->db_size
< drrs
->drr_length
)
2484 VERIFY(0 == dbuf_spill_set_blksz(db_spill
,
2485 drrs
->drr_length
, tx
));
2486 bcopy(data
, db_spill
->db_data
, drrs
->drr_length
);
2488 dmu_buf_rele(db
, FTAG
);
2489 dmu_buf_rele(db_spill
, FTAG
);
2497 receive_free(struct receive_writer_arg
*rwa
, struct drr_free
*drrf
)
2501 if (drrf
->drr_length
!= -1ULL &&
2502 drrf
->drr_offset
+ drrf
->drr_length
< drrf
->drr_offset
)
2503 return (SET_ERROR(EINVAL
));
2505 if (dmu_object_info(rwa
->os
, drrf
->drr_object
, NULL
) != 0)
2506 return (SET_ERROR(EINVAL
));
2508 if (drrf
->drr_object
> rwa
->max_object
)
2509 rwa
->max_object
= drrf
->drr_object
;
2511 err
= dmu_free_long_range(rwa
->os
, drrf
->drr_object
,
2512 drrf
->drr_offset
, drrf
->drr_length
);
2517 /* used to destroy the drc_ds on error */
2519 dmu_recv_cleanup_ds(dmu_recv_cookie_t
*drc
)
2521 if (drc
->drc_resumable
) {
2522 /* wait for our resume state to be written to disk */
2523 txg_wait_synced(drc
->drc_ds
->ds_dir
->dd_pool
, 0);
2524 dsl_dataset_disown(drc
->drc_ds
, dmu_recv_tag
);
2526 char name
[ZFS_MAX_DATASET_NAME_LEN
];
2527 dsl_dataset_name(drc
->drc_ds
, name
);
2528 dsl_dataset_disown(drc
->drc_ds
, dmu_recv_tag
);
2529 (void) dsl_destroy_head(name
);
2534 receive_cksum(struct receive_arg
*ra
, int len
, void *buf
)
2537 (void) fletcher_4_incremental_byteswap(buf
, len
, &ra
->cksum
);
2539 (void) fletcher_4_incremental_native(buf
, len
, &ra
->cksum
);
2544 * Read the payload into a buffer of size len, and update the current record's
2546 * Allocate ra->next_rrd and read the next record's header into
2547 * ra->next_rrd->header.
2548 * Verify checksum of payload and next record.
2551 receive_read_payload_and_next_header(struct receive_arg
*ra
, int len
, void *buf
)
2554 zio_cksum_t cksum_orig
;
2555 zio_cksum_t
*cksump
;
2558 ASSERT3U(len
, <=, SPA_MAXBLOCKSIZE
);
2559 err
= receive_read(ra
, len
, buf
);
2562 receive_cksum(ra
, len
, buf
);
2564 /* note: rrd is NULL when reading the begin record's payload */
2565 if (ra
->rrd
!= NULL
) {
2566 ra
->rrd
->payload
= buf
;
2567 ra
->rrd
->payload_size
= len
;
2568 ra
->rrd
->bytes_read
= ra
->bytes_read
;
2572 ra
->prev_cksum
= ra
->cksum
;
2574 ra
->next_rrd
= kmem_zalloc(sizeof (*ra
->next_rrd
), KM_SLEEP
);
2575 err
= receive_read(ra
, sizeof (ra
->next_rrd
->header
),
2576 &ra
->next_rrd
->header
);
2577 ra
->next_rrd
->bytes_read
= ra
->bytes_read
;
2579 kmem_free(ra
->next_rrd
, sizeof (*ra
->next_rrd
));
2580 ra
->next_rrd
= NULL
;
2583 if (ra
->next_rrd
->header
.drr_type
== DRR_BEGIN
) {
2584 kmem_free(ra
->next_rrd
, sizeof (*ra
->next_rrd
));
2585 ra
->next_rrd
= NULL
;
2586 return (SET_ERROR(EINVAL
));
2590 * Note: checksum is of everything up to but not including the
2593 ASSERT3U(offsetof(dmu_replay_record_t
, drr_u
.drr_checksum
.drr_checksum
),
2594 ==, sizeof (dmu_replay_record_t
) - sizeof (zio_cksum_t
));
2596 offsetof(dmu_replay_record_t
, drr_u
.drr_checksum
.drr_checksum
),
2597 &ra
->next_rrd
->header
);
2599 cksum_orig
= ra
->next_rrd
->header
.drr_u
.drr_checksum
.drr_checksum
;
2600 cksump
= &ra
->next_rrd
->header
.drr_u
.drr_checksum
.drr_checksum
;
2603 byteswap_record(&ra
->next_rrd
->header
);
2605 if ((!ZIO_CHECKSUM_IS_ZERO(cksump
)) &&
2606 !ZIO_CHECKSUM_EQUAL(ra
->cksum
, *cksump
)) {
2607 kmem_free(ra
->next_rrd
, sizeof (*ra
->next_rrd
));
2608 ra
->next_rrd
= NULL
;
2609 return (SET_ERROR(ECKSUM
));
2612 receive_cksum(ra
, sizeof (cksum_orig
), &cksum_orig
);
2618 objlist_create(struct objlist
*list
)
2620 list_create(&list
->list
, sizeof (struct receive_objnode
),
2621 offsetof(struct receive_objnode
, node
));
2622 list
->last_lookup
= 0;
2626 objlist_destroy(struct objlist
*list
)
2628 struct receive_objnode
*n
;
2630 for (n
= list_remove_head(&list
->list
);
2631 n
!= NULL
; n
= list_remove_head(&list
->list
)) {
2632 kmem_free(n
, sizeof (*n
));
2634 list_destroy(&list
->list
);
2638 * This function looks through the objlist to see if the specified object number
2639 * is contained in the objlist. In the process, it will remove all object
2640 * numbers in the list that are smaller than the specified object number. Thus,
2641 * any lookup of an object number smaller than a previously looked up object
2642 * number will always return false; therefore, all lookups should be done in
2646 objlist_exists(struct objlist
*list
, uint64_t object
)
2648 struct receive_objnode
*node
= list_head(&list
->list
);
2649 ASSERT3U(object
, >=, list
->last_lookup
);
2650 list
->last_lookup
= object
;
2651 while (node
!= NULL
&& node
->object
< object
) {
2652 VERIFY3P(node
, ==, list_remove_head(&list
->list
));
2653 kmem_free(node
, sizeof (*node
));
2654 node
= list_head(&list
->list
);
2656 return (node
!= NULL
&& node
->object
== object
);
2660 * The objlist is a list of object numbers stored in ascending order. However,
2661 * the insertion of new object numbers does not seek out the correct location to
2662 * store a new object number; instead, it appends it to the list for simplicity.
2663 * Thus, any users must take care to only insert new object numbers in ascending
2667 objlist_insert(struct objlist
*list
, uint64_t object
)
2669 struct receive_objnode
*node
= kmem_zalloc(sizeof (*node
), KM_SLEEP
);
2670 node
->object
= object
;
2673 struct receive_objnode
*last_object
= list_tail(&list
->list
);
2674 uint64_t last_objnum
= (last_object
!= NULL
? last_object
->object
: 0);
2675 ASSERT3U(node
->object
, >, last_objnum
);
2678 list_insert_tail(&list
->list
, node
);
2682 * Issue the prefetch reads for any necessary indirect blocks.
2684 * We use the object ignore list to tell us whether or not to issue prefetches
2685 * for a given object. We do this for both correctness (in case the blocksize
2686 * of an object has changed) and performance (if the object doesn't exist, don't
2687 * needlessly try to issue prefetches). We also trim the list as we go through
2688 * the stream to prevent it from growing to an unbounded size.
2690 * The object numbers within will always be in sorted order, and any write
2691 * records we see will also be in sorted order, but they're not sorted with
2692 * respect to each other (i.e. we can get several object records before
2693 * receiving each object's write records). As a result, once we've reached a
2694 * given object number, we can safely remove any reference to lower object
2695 * numbers in the ignore list. In practice, we receive up to 32 object records
2696 * before receiving write records, so the list can have up to 32 nodes in it.
2700 receive_read_prefetch(struct receive_arg
*ra
,
2701 uint64_t object
, uint64_t offset
, uint64_t length
)
2703 if (!objlist_exists(&ra
->ignore_objlist
, object
)) {
2704 dmu_prefetch(ra
->os
, object
, 1, offset
, length
,
2705 ZIO_PRIORITY_SYNC_READ
);
2710 * Read records off the stream, issuing any necessary prefetches.
2713 receive_read_record(struct receive_arg
*ra
)
2717 switch (ra
->rrd
->header
.drr_type
) {
2720 struct drr_object
*drro
= &ra
->rrd
->header
.drr_u
.drr_object
;
2721 uint32_t size
= P2ROUNDUP(drro
->drr_bonuslen
, 8);
2722 void *buf
= kmem_zalloc(size
, KM_SLEEP
);
2723 dmu_object_info_t doi
;
2724 err
= receive_read_payload_and_next_header(ra
, size
, buf
);
2726 kmem_free(buf
, size
);
2729 err
= dmu_object_info(ra
->os
, drro
->drr_object
, &doi
);
2731 * See receive_read_prefetch for an explanation why we're
2732 * storing this object in the ignore_obj_list.
2734 if (err
== ENOENT
||
2735 (err
== 0 && doi
.doi_data_block_size
!= drro
->drr_blksz
)) {
2736 objlist_insert(&ra
->ignore_objlist
, drro
->drr_object
);
2741 case DRR_FREEOBJECTS
:
2743 err
= receive_read_payload_and_next_header(ra
, 0, NULL
);
2748 struct drr_write
*drrw
= &ra
->rrd
->header
.drr_u
.drr_write
;
2750 boolean_t is_meta
= DMU_OT_IS_METADATA(drrw
->drr_type
);
2751 if (DRR_WRITE_COMPRESSED(drrw
)) {
2752 ASSERT3U(drrw
->drr_compressed_size
, >, 0);
2753 ASSERT3U(drrw
->drr_logical_size
, >=,
2754 drrw
->drr_compressed_size
);
2756 abuf
= arc_loan_compressed_buf(
2757 dmu_objset_spa(ra
->os
),
2758 drrw
->drr_compressed_size
, drrw
->drr_logical_size
,
2759 drrw
->drr_compressiontype
);
2761 abuf
= arc_loan_buf(dmu_objset_spa(ra
->os
),
2762 is_meta
, drrw
->drr_logical_size
);
2765 err
= receive_read_payload_and_next_header(ra
,
2766 DRR_WRITE_PAYLOAD_SIZE(drrw
), abuf
->b_data
);
2768 dmu_return_arcbuf(abuf
);
2771 ra
->rrd
->write_buf
= abuf
;
2772 receive_read_prefetch(ra
, drrw
->drr_object
, drrw
->drr_offset
,
2773 drrw
->drr_logical_size
);
2776 case DRR_WRITE_BYREF
:
2778 struct drr_write_byref
*drrwb
=
2779 &ra
->rrd
->header
.drr_u
.drr_write_byref
;
2780 err
= receive_read_payload_and_next_header(ra
, 0, NULL
);
2781 receive_read_prefetch(ra
, drrwb
->drr_object
, drrwb
->drr_offset
,
2785 case DRR_WRITE_EMBEDDED
:
2787 struct drr_write_embedded
*drrwe
=
2788 &ra
->rrd
->header
.drr_u
.drr_write_embedded
;
2789 uint32_t size
= P2ROUNDUP(drrwe
->drr_psize
, 8);
2790 void *buf
= kmem_zalloc(size
, KM_SLEEP
);
2792 err
= receive_read_payload_and_next_header(ra
, size
, buf
);
2794 kmem_free(buf
, size
);
2798 receive_read_prefetch(ra
, drrwe
->drr_object
, drrwe
->drr_offset
,
2805 * It might be beneficial to prefetch indirect blocks here, but
2806 * we don't really have the data to decide for sure.
2808 err
= receive_read_payload_and_next_header(ra
, 0, NULL
);
2813 struct drr_end
*drre
= &ra
->rrd
->header
.drr_u
.drr_end
;
2814 if (!ZIO_CHECKSUM_EQUAL(ra
->prev_cksum
, drre
->drr_checksum
))
2815 return (SET_ERROR(ECKSUM
));
2820 struct drr_spill
*drrs
= &ra
->rrd
->header
.drr_u
.drr_spill
;
2821 void *buf
= kmem_zalloc(drrs
->drr_length
, KM_SLEEP
);
2822 err
= receive_read_payload_and_next_header(ra
, drrs
->drr_length
,
2825 kmem_free(buf
, drrs
->drr_length
);
2829 return (SET_ERROR(EINVAL
));
2834 dprintf_drr(struct receive_record_arg
*rrd
, int err
)
2836 switch (rrd
->header
.drr_type
) {
2839 struct drr_object
*drro
= &rrd
->header
.drr_u
.drr_object
;
2840 dprintf("drr_type = OBJECT obj = %llu type = %u "
2841 "bonustype = %u blksz = %u bonuslen = %u cksumtype = %u "
2842 "compress = %u dn_slots = %u err = %d\n",
2843 drro
->drr_object
, drro
->drr_type
, drro
->drr_bonustype
,
2844 drro
->drr_blksz
, drro
->drr_bonuslen
,
2845 drro
->drr_checksumtype
, drro
->drr_compress
,
2846 drro
->drr_dn_slots
, err
);
2849 case DRR_FREEOBJECTS
:
2851 struct drr_freeobjects
*drrfo
=
2852 &rrd
->header
.drr_u
.drr_freeobjects
;
2853 dprintf("drr_type = FREEOBJECTS firstobj = %llu "
2854 "numobjs = %llu err = %d\n",
2855 drrfo
->drr_firstobj
, drrfo
->drr_numobjs
, err
);
2860 struct drr_write
*drrw
= &rrd
->header
.drr_u
.drr_write
;
2861 dprintf("drr_type = WRITE obj = %llu type = %u offset = %llu "
2862 "lsize = %llu cksumtype = %u cksumflags = %u "
2863 "compress = %u psize = %llu err = %d\n",
2864 drrw
->drr_object
, drrw
->drr_type
, drrw
->drr_offset
,
2865 drrw
->drr_logical_size
, drrw
->drr_checksumtype
,
2866 drrw
->drr_checksumflags
, drrw
->drr_compressiontype
,
2867 drrw
->drr_compressed_size
, err
);
2870 case DRR_WRITE_BYREF
:
2872 struct drr_write_byref
*drrwbr
=
2873 &rrd
->header
.drr_u
.drr_write_byref
;
2874 dprintf("drr_type = WRITE_BYREF obj = %llu offset = %llu "
2875 "length = %llu toguid = %llx refguid = %llx "
2876 "refobject = %llu refoffset = %llu cksumtype = %u "
2877 "cksumflags = %u err = %d\n",
2878 drrwbr
->drr_object
, drrwbr
->drr_offset
,
2879 drrwbr
->drr_length
, drrwbr
->drr_toguid
,
2880 drrwbr
->drr_refguid
, drrwbr
->drr_refobject
,
2881 drrwbr
->drr_refoffset
, drrwbr
->drr_checksumtype
,
2882 drrwbr
->drr_checksumflags
, err
);
2885 case DRR_WRITE_EMBEDDED
:
2887 struct drr_write_embedded
*drrwe
=
2888 &rrd
->header
.drr_u
.drr_write_embedded
;
2889 dprintf("drr_type = WRITE_EMBEDDED obj = %llu offset = %llu "
2890 "length = %llu compress = %u etype = %u lsize = %u "
2891 "psize = %u err = %d\n",
2892 drrwe
->drr_object
, drrwe
->drr_offset
, drrwe
->drr_length
,
2893 drrwe
->drr_compression
, drrwe
->drr_etype
,
2894 drrwe
->drr_lsize
, drrwe
->drr_psize
, err
);
2899 struct drr_free
*drrf
= &rrd
->header
.drr_u
.drr_free
;
2900 dprintf("drr_type = FREE obj = %llu offset = %llu "
2901 "length = %lld err = %d\n",
2902 drrf
->drr_object
, drrf
->drr_offset
, drrf
->drr_length
,
2908 struct drr_spill
*drrs
= &rrd
->header
.drr_u
.drr_spill
;
2909 dprintf("drr_type = SPILL obj = %llu length = %llu "
2910 "err = %d\n", drrs
->drr_object
, drrs
->drr_length
, err
);
2919 * Commit the records to the pool.
2922 receive_process_record(struct receive_writer_arg
*rwa
,
2923 struct receive_record_arg
*rrd
)
2927 /* Processing in order, therefore bytes_read should be increasing. */
2928 ASSERT3U(rrd
->bytes_read
, >=, rwa
->bytes_read
);
2929 rwa
->bytes_read
= rrd
->bytes_read
;
2931 switch (rrd
->header
.drr_type
) {
2934 struct drr_object
*drro
= &rrd
->header
.drr_u
.drr_object
;
2935 err
= receive_object(rwa
, drro
, rrd
->payload
);
2936 kmem_free(rrd
->payload
, rrd
->payload_size
);
2937 rrd
->payload
= NULL
;
2940 case DRR_FREEOBJECTS
:
2942 struct drr_freeobjects
*drrfo
=
2943 &rrd
->header
.drr_u
.drr_freeobjects
;
2944 err
= receive_freeobjects(rwa
, drrfo
);
2949 struct drr_write
*drrw
= &rrd
->header
.drr_u
.drr_write
;
2950 err
= receive_write(rwa
, drrw
, rrd
->write_buf
);
2951 /* if receive_write() is successful, it consumes the arc_buf */
2953 dmu_return_arcbuf(rrd
->write_buf
);
2954 rrd
->write_buf
= NULL
;
2955 rrd
->payload
= NULL
;
2958 case DRR_WRITE_BYREF
:
2960 struct drr_write_byref
*drrwbr
=
2961 &rrd
->header
.drr_u
.drr_write_byref
;
2962 err
= receive_write_byref(rwa
, drrwbr
);
2965 case DRR_WRITE_EMBEDDED
:
2967 struct drr_write_embedded
*drrwe
=
2968 &rrd
->header
.drr_u
.drr_write_embedded
;
2969 err
= receive_write_embedded(rwa
, drrwe
, rrd
->payload
);
2970 kmem_free(rrd
->payload
, rrd
->payload_size
);
2971 rrd
->payload
= NULL
;
2976 struct drr_free
*drrf
= &rrd
->header
.drr_u
.drr_free
;
2977 err
= receive_free(rwa
, drrf
);
2982 struct drr_spill
*drrs
= &rrd
->header
.drr_u
.drr_spill
;
2983 err
= receive_spill(rwa
, drrs
, rrd
->payload
);
2984 kmem_free(rrd
->payload
, rrd
->payload_size
);
2985 rrd
->payload
= NULL
;
2989 return (SET_ERROR(EINVAL
));
2993 dprintf_drr(rrd
, err
);
2999 * dmu_recv_stream's worker thread; pull records off the queue, and then call
3000 * receive_process_record When we're done, signal the main thread and exit.
3003 receive_writer_thread(void *arg
)
3005 struct receive_writer_arg
*rwa
= arg
;
3006 struct receive_record_arg
*rrd
;
3007 fstrans_cookie_t cookie
= spl_fstrans_mark();
3009 for (rrd
= bqueue_dequeue(&rwa
->q
); !rrd
->eos_marker
;
3010 rrd
= bqueue_dequeue(&rwa
->q
)) {
3012 * If there's an error, the main thread will stop putting things
3013 * on the queue, but we need to clear everything in it before we
3016 if (rwa
->err
== 0) {
3017 rwa
->err
= receive_process_record(rwa
, rrd
);
3018 } else if (rrd
->write_buf
!= NULL
) {
3019 dmu_return_arcbuf(rrd
->write_buf
);
3020 rrd
->write_buf
= NULL
;
3021 rrd
->payload
= NULL
;
3022 } else if (rrd
->payload
!= NULL
) {
3023 kmem_free(rrd
->payload
, rrd
->payload_size
);
3024 rrd
->payload
= NULL
;
3026 kmem_free(rrd
, sizeof (*rrd
));
3028 kmem_free(rrd
, sizeof (*rrd
));
3029 mutex_enter(&rwa
->mutex
);
3031 cv_signal(&rwa
->cv
);
3032 mutex_exit(&rwa
->mutex
);
3033 spl_fstrans_unmark(cookie
);
3038 resume_check(struct receive_arg
*ra
, nvlist_t
*begin_nvl
)
3041 objset_t
*mos
= dmu_objset_pool(ra
->os
)->dp_meta_objset
;
3042 uint64_t dsobj
= dmu_objset_id(ra
->os
);
3043 uint64_t resume_obj
, resume_off
;
3045 if (nvlist_lookup_uint64(begin_nvl
,
3046 "resume_object", &resume_obj
) != 0 ||
3047 nvlist_lookup_uint64(begin_nvl
,
3048 "resume_offset", &resume_off
) != 0) {
3049 return (SET_ERROR(EINVAL
));
3051 VERIFY0(zap_lookup(mos
, dsobj
,
3052 DS_FIELD_RESUME_OBJECT
, sizeof (val
), 1, &val
));
3053 if (resume_obj
!= val
)
3054 return (SET_ERROR(EINVAL
));
3055 VERIFY0(zap_lookup(mos
, dsobj
,
3056 DS_FIELD_RESUME_OFFSET
, sizeof (val
), 1, &val
));
3057 if (resume_off
!= val
)
3058 return (SET_ERROR(EINVAL
));
3064 * Read in the stream's records, one by one, and apply them to the pool. There
3065 * are two threads involved; the thread that calls this function will spin up a
3066 * worker thread, read the records off the stream one by one, and issue
3067 * prefetches for any necessary indirect blocks. It will then push the records
3068 * onto an internal blocking queue. The worker thread will pull the records off
3069 * the queue, and actually write the data into the DMU. This way, the worker
3070 * thread doesn't have to wait for reads to complete, since everything it needs
3071 * (the indirect blocks) will be prefetched.
3073 * NB: callers *must* call dmu_recv_end() if this succeeds.
3076 dmu_recv_stream(dmu_recv_cookie_t
*drc
, vnode_t
*vp
, offset_t
*voffp
,
3077 int cleanup_fd
, uint64_t *action_handlep
)
3080 struct receive_arg
*ra
;
3081 struct receive_writer_arg
*rwa
;
3083 uint32_t payloadlen
;
3085 nvlist_t
*begin_nvl
= NULL
;
3087 ra
= kmem_zalloc(sizeof (*ra
), KM_SLEEP
);
3088 rwa
= kmem_zalloc(sizeof (*rwa
), KM_SLEEP
);
3090 ra
->byteswap
= drc
->drc_byteswap
;
3091 ra
->cksum
= drc
->drc_cksum
;
3095 if (dsl_dataset_is_zapified(drc
->drc_ds
)) {
3096 (void) zap_lookup(drc
->drc_ds
->ds_dir
->dd_pool
->dp_meta_objset
,
3097 drc
->drc_ds
->ds_object
, DS_FIELD_RESUME_BYTES
,
3098 sizeof (ra
->bytes_read
), 1, &ra
->bytes_read
);
3101 objlist_create(&ra
->ignore_objlist
);
3103 /* these were verified in dmu_recv_begin */
3104 ASSERT3U(DMU_GET_STREAM_HDRTYPE(drc
->drc_drrb
->drr_versioninfo
), ==,
3106 ASSERT3U(drc
->drc_drrb
->drr_type
, <, DMU_OST_NUMTYPES
);
3109 * Open the objset we are modifying.
3111 VERIFY0(dmu_objset_from_ds(drc
->drc_ds
, &ra
->os
));
3113 ASSERT(dsl_dataset_phys(drc
->drc_ds
)->ds_flags
& DS_FLAG_INCONSISTENT
);
3115 featureflags
= DMU_GET_FEATUREFLAGS(drc
->drc_drrb
->drr_versioninfo
);
3117 /* if this stream is dedup'ed, set up the avl tree for guid mapping */
3118 if (featureflags
& DMU_BACKUP_FEATURE_DEDUP
) {
3121 if (cleanup_fd
== -1) {
3122 ra
->err
= SET_ERROR(EBADF
);
3125 ra
->err
= zfs_onexit_fd_hold(cleanup_fd
, &minor
);
3131 if (*action_handlep
== 0) {
3132 rwa
->guid_to_ds_map
=
3133 kmem_alloc(sizeof (avl_tree_t
), KM_SLEEP
);
3134 avl_create(rwa
->guid_to_ds_map
, guid_compare
,
3135 sizeof (guid_map_entry_t
),
3136 offsetof(guid_map_entry_t
, avlnode
));
3137 err
= zfs_onexit_add_cb(minor
,
3138 free_guid_map_onexit
, rwa
->guid_to_ds_map
,
3143 err
= zfs_onexit_cb_data(minor
, *action_handlep
,
3144 (void **)&rwa
->guid_to_ds_map
);
3149 drc
->drc_guid_to_ds_map
= rwa
->guid_to_ds_map
;
3152 payloadlen
= drc
->drc_drr_begin
->drr_payloadlen
;
3154 if (payloadlen
!= 0)
3155 payload
= kmem_alloc(payloadlen
, KM_SLEEP
);
3157 err
= receive_read_payload_and_next_header(ra
, payloadlen
, payload
);
3159 if (payloadlen
!= 0)
3160 kmem_free(payload
, payloadlen
);
3163 if (payloadlen
!= 0) {
3164 err
= nvlist_unpack(payload
, payloadlen
, &begin_nvl
, KM_SLEEP
);
3165 kmem_free(payload
, payloadlen
);
3170 if (featureflags
& DMU_BACKUP_FEATURE_RESUMING
) {
3171 err
= resume_check(ra
, begin_nvl
);
3176 (void) bqueue_init(&rwa
->q
, zfs_recv_queue_length
,
3177 offsetof(struct receive_record_arg
, node
));
3178 cv_init(&rwa
->cv
, NULL
, CV_DEFAULT
, NULL
);
3179 mutex_init(&rwa
->mutex
, NULL
, MUTEX_DEFAULT
, NULL
);
3181 rwa
->byteswap
= drc
->drc_byteswap
;
3182 rwa
->resumable
= drc
->drc_resumable
;
3184 (void) thread_create(NULL
, 0, receive_writer_thread
, rwa
, 0, curproc
,
3185 TS_RUN
, minclsyspri
);
3187 * We're reading rwa->err without locks, which is safe since we are the
3188 * only reader, and the worker thread is the only writer. It's ok if we
3189 * miss a write for an iteration or two of the loop, since the writer
3190 * thread will keep freeing records we send it until we send it an eos
3193 * We can leave this loop in 3 ways: First, if rwa->err is
3194 * non-zero. In that case, the writer thread will free the rrd we just
3195 * pushed. Second, if we're interrupted; in that case, either it's the
3196 * first loop and ra->rrd was never allocated, or it's later and ra->rrd
3197 * has been handed off to the writer thread who will free it. Finally,
3198 * if receive_read_record fails or we're at the end of the stream, then
3199 * we free ra->rrd and exit.
3201 while (rwa
->err
== 0) {
3202 if (issig(JUSTLOOKING
) && issig(FORREAL
)) {
3203 err
= SET_ERROR(EINTR
);
3207 ASSERT3P(ra
->rrd
, ==, NULL
);
3208 ra
->rrd
= ra
->next_rrd
;
3209 ra
->next_rrd
= NULL
;
3210 /* Allocates and loads header into ra->next_rrd */
3211 err
= receive_read_record(ra
);
3213 if (ra
->rrd
->header
.drr_type
== DRR_END
|| err
!= 0) {
3214 kmem_free(ra
->rrd
, sizeof (*ra
->rrd
));
3219 bqueue_enqueue(&rwa
->q
, ra
->rrd
,
3220 sizeof (struct receive_record_arg
) + ra
->rrd
->payload_size
);
3223 if (ra
->next_rrd
== NULL
)
3224 ra
->next_rrd
= kmem_zalloc(sizeof (*ra
->next_rrd
), KM_SLEEP
);
3225 ra
->next_rrd
->eos_marker
= B_TRUE
;
3226 bqueue_enqueue(&rwa
->q
, ra
->next_rrd
, 1);
3228 mutex_enter(&rwa
->mutex
);
3229 while (!rwa
->done
) {
3230 cv_wait(&rwa
->cv
, &rwa
->mutex
);
3232 mutex_exit(&rwa
->mutex
);
3235 * If we are receiving a full stream as a clone, all object IDs which
3236 * are greater than the maximum ID referenced in the stream are
3237 * by definition unused and must be freed.
3239 if (drc
->drc_clone
&& drc
->drc_drrb
->drr_fromguid
== 0) {
3240 uint64_t obj
= rwa
->max_object
+ 1;
3244 while (next_err
== 0) {
3245 free_err
= dmu_free_long_object(rwa
->os
, obj
);
3246 if (free_err
!= 0 && free_err
!= ENOENT
)
3249 next_err
= dmu_object_next(rwa
->os
, &obj
, FALSE
, 0);
3253 if (free_err
!= 0 && free_err
!= ENOENT
)
3255 else if (next_err
!= ESRCH
)
3260 cv_destroy(&rwa
->cv
);
3261 mutex_destroy(&rwa
->mutex
);
3262 bqueue_destroy(&rwa
->q
);
3267 nvlist_free(begin_nvl
);
3268 if ((featureflags
& DMU_BACKUP_FEATURE_DEDUP
) && (cleanup_fd
!= -1))
3269 zfs_onexit_fd_rele(cleanup_fd
);
3273 * Clean up references. If receive is not resumable,
3274 * destroy what we created, so we don't leave it in
3275 * the inconsistent state.
3277 dmu_recv_cleanup_ds(drc
);
3281 objlist_destroy(&ra
->ignore_objlist
);
3282 kmem_free(ra
, sizeof (*ra
));
3283 kmem_free(rwa
, sizeof (*rwa
));
3288 dmu_recv_end_check(void *arg
, dmu_tx_t
*tx
)
3290 dmu_recv_cookie_t
*drc
= arg
;
3291 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
3294 ASSERT3P(drc
->drc_ds
->ds_owner
, ==, dmu_recv_tag
);
3296 if (!drc
->drc_newfs
) {
3297 dsl_dataset_t
*origin_head
;
3299 error
= dsl_dataset_hold(dp
, drc
->drc_tofs
, FTAG
, &origin_head
);
3302 if (drc
->drc_force
) {
3304 * We will destroy any snapshots in tofs (i.e. before
3305 * origin_head) that are after the origin (which is
3306 * the snap before drc_ds, because drc_ds can not
3307 * have any snaps of its own).
3311 obj
= dsl_dataset_phys(origin_head
)->ds_prev_snap_obj
;
3313 dsl_dataset_phys(drc
->drc_ds
)->ds_prev_snap_obj
) {
3314 dsl_dataset_t
*snap
;
3315 error
= dsl_dataset_hold_obj(dp
, obj
, FTAG
,
3319 if (snap
->ds_dir
!= origin_head
->ds_dir
)
3320 error
= SET_ERROR(EINVAL
);
3322 error
= dsl_destroy_snapshot_check_impl(
3325 obj
= dsl_dataset_phys(snap
)->ds_prev_snap_obj
;
3326 dsl_dataset_rele(snap
, FTAG
);
3331 dsl_dataset_rele(origin_head
, FTAG
);
3335 error
= dsl_dataset_clone_swap_check_impl(drc
->drc_ds
,
3336 origin_head
, drc
->drc_force
, drc
->drc_owner
, tx
);
3338 dsl_dataset_rele(origin_head
, FTAG
);
3341 error
= dsl_dataset_snapshot_check_impl(origin_head
,
3342 drc
->drc_tosnap
, tx
, B_TRUE
, 1, drc
->drc_cred
);
3343 dsl_dataset_rele(origin_head
, FTAG
);
3347 error
= dsl_destroy_head_check_impl(drc
->drc_ds
, 1);
3349 error
= dsl_dataset_snapshot_check_impl(drc
->drc_ds
,
3350 drc
->drc_tosnap
, tx
, B_TRUE
, 1, drc
->drc_cred
);
3356 dmu_recv_end_sync(void *arg
, dmu_tx_t
*tx
)
3358 dmu_recv_cookie_t
*drc
= arg
;
3359 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
3361 spa_history_log_internal_ds(drc
->drc_ds
, "finish receiving",
3362 tx
, "snap=%s", drc
->drc_tosnap
);
3364 if (!drc
->drc_newfs
) {
3365 dsl_dataset_t
*origin_head
;
3367 VERIFY0(dsl_dataset_hold(dp
, drc
->drc_tofs
, FTAG
,
3370 if (drc
->drc_force
) {
3372 * Destroy any snapshots of drc_tofs (origin_head)
3373 * after the origin (the snap before drc_ds).
3377 obj
= dsl_dataset_phys(origin_head
)->ds_prev_snap_obj
;
3379 dsl_dataset_phys(drc
->drc_ds
)->ds_prev_snap_obj
) {
3380 dsl_dataset_t
*snap
;
3381 VERIFY0(dsl_dataset_hold_obj(dp
, obj
, FTAG
,
3383 ASSERT3P(snap
->ds_dir
, ==, origin_head
->ds_dir
);
3384 obj
= dsl_dataset_phys(snap
)->ds_prev_snap_obj
;
3385 dsl_destroy_snapshot_sync_impl(snap
,
3387 dsl_dataset_rele(snap
, FTAG
);
3390 VERIFY3P(drc
->drc_ds
->ds_prev
, ==,
3391 origin_head
->ds_prev
);
3393 dsl_dataset_clone_swap_sync_impl(drc
->drc_ds
,
3395 dsl_dataset_snapshot_sync_impl(origin_head
,
3396 drc
->drc_tosnap
, tx
);
3398 /* set snapshot's creation time and guid */
3399 dmu_buf_will_dirty(origin_head
->ds_prev
->ds_dbuf
, tx
);
3400 dsl_dataset_phys(origin_head
->ds_prev
)->ds_creation_time
=
3401 drc
->drc_drrb
->drr_creation_time
;
3402 dsl_dataset_phys(origin_head
->ds_prev
)->ds_guid
=
3403 drc
->drc_drrb
->drr_toguid
;
3404 dsl_dataset_phys(origin_head
->ds_prev
)->ds_flags
&=
3405 ~DS_FLAG_INCONSISTENT
;
3407 dmu_buf_will_dirty(origin_head
->ds_dbuf
, tx
);
3408 dsl_dataset_phys(origin_head
)->ds_flags
&=
3409 ~DS_FLAG_INCONSISTENT
;
3411 drc
->drc_newsnapobj
=
3412 dsl_dataset_phys(origin_head
)->ds_prev_snap_obj
;
3414 dsl_dataset_rele(origin_head
, FTAG
);
3415 dsl_destroy_head_sync_impl(drc
->drc_ds
, tx
);
3417 if (drc
->drc_owner
!= NULL
)
3418 VERIFY3P(origin_head
->ds_owner
, ==, drc
->drc_owner
);
3420 dsl_dataset_t
*ds
= drc
->drc_ds
;
3422 dsl_dataset_snapshot_sync_impl(ds
, drc
->drc_tosnap
, tx
);
3424 /* set snapshot's creation time and guid */
3425 dmu_buf_will_dirty(ds
->ds_prev
->ds_dbuf
, tx
);
3426 dsl_dataset_phys(ds
->ds_prev
)->ds_creation_time
=
3427 drc
->drc_drrb
->drr_creation_time
;
3428 dsl_dataset_phys(ds
->ds_prev
)->ds_guid
=
3429 drc
->drc_drrb
->drr_toguid
;
3430 dsl_dataset_phys(ds
->ds_prev
)->ds_flags
&=
3431 ~DS_FLAG_INCONSISTENT
;
3433 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
3434 dsl_dataset_phys(ds
)->ds_flags
&= ~DS_FLAG_INCONSISTENT
;
3435 if (dsl_dataset_has_resume_receive_state(ds
)) {
3436 (void) zap_remove(dp
->dp_meta_objset
, ds
->ds_object
,
3437 DS_FIELD_RESUME_FROMGUID
, tx
);
3438 (void) zap_remove(dp
->dp_meta_objset
, ds
->ds_object
,
3439 DS_FIELD_RESUME_OBJECT
, tx
);
3440 (void) zap_remove(dp
->dp_meta_objset
, ds
->ds_object
,
3441 DS_FIELD_RESUME_OFFSET
, tx
);
3442 (void) zap_remove(dp
->dp_meta_objset
, ds
->ds_object
,
3443 DS_FIELD_RESUME_BYTES
, tx
);
3444 (void) zap_remove(dp
->dp_meta_objset
, ds
->ds_object
,
3445 DS_FIELD_RESUME_TOGUID
, tx
);
3446 (void) zap_remove(dp
->dp_meta_objset
, ds
->ds_object
,
3447 DS_FIELD_RESUME_TONAME
, tx
);
3449 drc
->drc_newsnapobj
=
3450 dsl_dataset_phys(drc
->drc_ds
)->ds_prev_snap_obj
;
3452 zvol_create_minors(dp
->dp_spa
, drc
->drc_tofs
, B_TRUE
);
3454 * Release the hold from dmu_recv_begin. This must be done before
3455 * we return to open context, so that when we free the dataset's dnode,
3456 * we can evict its bonus buffer.
3458 dsl_dataset_disown(drc
->drc_ds
, dmu_recv_tag
);
3463 add_ds_to_guidmap(const char *name
, avl_tree_t
*guid_map
, uint64_t snapobj
)
3466 dsl_dataset_t
*snapds
;
3467 guid_map_entry_t
*gmep
;
3470 ASSERT(guid_map
!= NULL
);
3472 err
= dsl_pool_hold(name
, FTAG
, &dp
);
3475 gmep
= kmem_alloc(sizeof (*gmep
), KM_SLEEP
);
3476 err
= dsl_dataset_hold_obj(dp
, snapobj
, gmep
, &snapds
);
3478 gmep
->guid
= dsl_dataset_phys(snapds
)->ds_guid
;
3479 gmep
->gme_ds
= snapds
;
3480 avl_add(guid_map
, gmep
);
3481 dsl_dataset_long_hold(snapds
, gmep
);
3483 kmem_free(gmep
, sizeof (*gmep
));
3486 dsl_pool_rele(dp
, FTAG
);
3490 static int dmu_recv_end_modified_blocks
= 3;
3493 dmu_recv_existing_end(dmu_recv_cookie_t
*drc
)
3497 * We will be destroying the ds; make sure its origin is unmounted if
3500 char name
[ZFS_MAX_DATASET_NAME_LEN
];
3501 dsl_dataset_name(drc
->drc_ds
, name
);
3502 zfs_destroy_unmount_origin(name
);
3505 return (dsl_sync_task(drc
->drc_tofs
,
3506 dmu_recv_end_check
, dmu_recv_end_sync
, drc
,
3507 dmu_recv_end_modified_blocks
, ZFS_SPACE_CHECK_NORMAL
));
3511 dmu_recv_new_end(dmu_recv_cookie_t
*drc
)
3513 return (dsl_sync_task(drc
->drc_tofs
,
3514 dmu_recv_end_check
, dmu_recv_end_sync
, drc
,
3515 dmu_recv_end_modified_blocks
, ZFS_SPACE_CHECK_NORMAL
));
3519 dmu_recv_end(dmu_recv_cookie_t
*drc
, void *owner
)
3523 drc
->drc_owner
= owner
;
3526 error
= dmu_recv_new_end(drc
);
3528 error
= dmu_recv_existing_end(drc
);
3531 dmu_recv_cleanup_ds(drc
);
3532 } else if (drc
->drc_guid_to_ds_map
!= NULL
) {
3533 (void) add_ds_to_guidmap(drc
->drc_tofs
,
3534 drc
->drc_guid_to_ds_map
,
3535 drc
->drc_newsnapobj
);
3541 * Return TRUE if this objset is currently being received into.
3544 dmu_objset_is_receiving(objset_t
*os
)
3546 return (os
->os_dsl_dataset
!= NULL
&&
3547 os
->os_dsl_dataset
->ds_owner
== dmu_recv_tag
);
3550 #if defined(_KERNEL)
3551 module_param(zfs_send_corrupt_data
, int, 0644);
3552 MODULE_PARM_DESC(zfs_send_corrupt_data
, "Allow sending corrupt data");