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
);
426 * If there is a pending op, but it's not PENDING_FREEOBJECTS,
427 * push it out, since free block aggregation can only be done for
428 * blocks of the same type (i.e., DRR_FREE records can only be
429 * aggregated with other DRR_FREE records. DRR_FREEOBJECTS records
430 * can only be aggregated with other DRR_FREEOBJECTS records.
432 if (dsp
->dsa_pending_op
!= PENDING_NONE
&&
433 dsp
->dsa_pending_op
!= PENDING_FREEOBJECTS
) {
434 if (dump_record(dsp
, NULL
, 0) != 0)
435 return (SET_ERROR(EINTR
));
436 dsp
->dsa_pending_op
= PENDING_NONE
;
438 if (dsp
->dsa_pending_op
== PENDING_FREEOBJECTS
) {
440 * See whether this free object array can be aggregated
443 if (drrfo
->drr_firstobj
+ drrfo
->drr_numobjs
== firstobj
) {
444 drrfo
->drr_numobjs
+= numobjs
;
447 /* can't be aggregated. Push out pending record */
448 if (dump_record(dsp
, NULL
, 0) != 0)
449 return (SET_ERROR(EINTR
));
450 dsp
->dsa_pending_op
= PENDING_NONE
;
454 /* write a FREEOBJECTS record */
455 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
456 dsp
->dsa_drr
->drr_type
= DRR_FREEOBJECTS
;
457 drrfo
->drr_firstobj
= firstobj
;
458 drrfo
->drr_numobjs
= numobjs
;
459 drrfo
->drr_toguid
= dsp
->dsa_toguid
;
461 dsp
->dsa_pending_op
= PENDING_FREEOBJECTS
;
467 dump_dnode(dmu_sendarg_t
*dsp
, uint64_t object
, dnode_phys_t
*dnp
)
469 struct drr_object
*drro
= &(dsp
->dsa_drr
->drr_u
.drr_object
);
471 if (object
< dsp
->dsa_resume_object
) {
473 * Note: when resuming, we will visit all the dnodes in
474 * the block of dnodes that we are resuming from. In
475 * this case it's unnecessary to send the dnodes prior to
476 * the one we are resuming from. We should be at most one
477 * block's worth of dnodes behind the resume point.
479 ASSERT3U(dsp
->dsa_resume_object
- object
, <,
480 1 << (DNODE_BLOCK_SHIFT
- DNODE_SHIFT
));
484 if (dnp
== NULL
|| dnp
->dn_type
== DMU_OT_NONE
)
485 return (dump_freeobjects(dsp
, object
, 1));
487 if (dsp
->dsa_pending_op
!= PENDING_NONE
) {
488 if (dump_record(dsp
, NULL
, 0) != 0)
489 return (SET_ERROR(EINTR
));
490 dsp
->dsa_pending_op
= PENDING_NONE
;
493 /* write an OBJECT record */
494 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
495 dsp
->dsa_drr
->drr_type
= DRR_OBJECT
;
496 drro
->drr_object
= object
;
497 drro
->drr_type
= dnp
->dn_type
;
498 drro
->drr_bonustype
= dnp
->dn_bonustype
;
499 drro
->drr_blksz
= dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
;
500 drro
->drr_bonuslen
= dnp
->dn_bonuslen
;
501 drro
->drr_dn_slots
= dnp
->dn_extra_slots
+ 1;
502 drro
->drr_checksumtype
= dnp
->dn_checksum
;
503 drro
->drr_compress
= dnp
->dn_compress
;
504 drro
->drr_toguid
= dsp
->dsa_toguid
;
506 if (!(dsp
->dsa_featureflags
& DMU_BACKUP_FEATURE_LARGE_BLOCKS
) &&
507 drro
->drr_blksz
> SPA_OLD_MAXBLOCKSIZE
)
508 drro
->drr_blksz
= SPA_OLD_MAXBLOCKSIZE
;
510 if (dump_record(dsp
, DN_BONUS(dnp
),
511 P2ROUNDUP(dnp
->dn_bonuslen
, 8)) != 0) {
512 return (SET_ERROR(EINTR
));
515 /* Free anything past the end of the file. */
516 if (dump_free(dsp
, object
, (dnp
->dn_maxblkid
+ 1) *
517 (dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
), -1ULL) != 0)
518 return (SET_ERROR(EINTR
));
519 if (dsp
->dsa_err
!= 0)
520 return (SET_ERROR(EINTR
));
525 backup_do_embed(dmu_sendarg_t
*dsp
, const blkptr_t
*bp
)
527 if (!BP_IS_EMBEDDED(bp
))
531 * Compression function must be legacy, or explicitly enabled.
533 if ((BP_GET_COMPRESS(bp
) >= ZIO_COMPRESS_LEGACY_FUNCTIONS
&&
534 !(dsp
->dsa_featureflags
& DMU_BACKUP_FEATURE_LZ4
)))
538 * Embed type must be explicitly enabled.
540 switch (BPE_GET_ETYPE(bp
)) {
541 case BP_EMBEDDED_TYPE_DATA
:
542 if (dsp
->dsa_featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA
)
552 * This is the callback function to traverse_dataset that acts as the worker
553 * thread for dmu_send_impl.
557 send_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
558 const zbookmark_phys_t
*zb
, const struct dnode_phys
*dnp
, void *arg
)
560 struct send_thread_arg
*sta
= arg
;
561 struct send_block_record
*record
;
562 uint64_t record_size
;
565 ASSERT(zb
->zb_object
== DMU_META_DNODE_OBJECT
||
566 zb
->zb_object
>= sta
->resume
.zb_object
);
569 return (SET_ERROR(EINTR
));
572 ASSERT3U(zb
->zb_level
, ==, ZB_DNODE_LEVEL
);
574 } else if (zb
->zb_level
< 0) {
578 record
= kmem_zalloc(sizeof (struct send_block_record
), KM_SLEEP
);
579 record
->eos_marker
= B_FALSE
;
582 record
->indblkshift
= dnp
->dn_indblkshift
;
583 record
->datablkszsec
= dnp
->dn_datablkszsec
;
584 record_size
= dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
;
585 bqueue_enqueue(&sta
->q
, record
, record_size
);
591 * This function kicks off the traverse_dataset. It also handles setting the
592 * error code of the thread in case something goes wrong, and pushes the End of
593 * Stream record when the traverse_dataset call has finished. If there is no
594 * dataset to traverse, the thread immediately pushes End of Stream marker.
597 send_traverse_thread(void *arg
)
599 struct send_thread_arg
*st_arg
= arg
;
601 struct send_block_record
*data
;
602 fstrans_cookie_t cookie
= spl_fstrans_mark();
604 if (st_arg
->ds
!= NULL
) {
605 err
= traverse_dataset_resume(st_arg
->ds
,
606 st_arg
->fromtxg
, &st_arg
->resume
,
607 st_arg
->flags
, send_cb
, st_arg
);
610 st_arg
->error_code
= err
;
612 data
= kmem_zalloc(sizeof (*data
), KM_SLEEP
);
613 data
->eos_marker
= B_TRUE
;
614 bqueue_enqueue(&st_arg
->q
, data
, 1);
615 spl_fstrans_unmark(cookie
);
619 * This function actually handles figuring out what kind of record needs to be
620 * dumped, reading the data (which has hopefully been prefetched), and calling
621 * the appropriate helper function.
624 do_dump(dmu_sendarg_t
*dsa
, struct send_block_record
*data
)
626 dsl_dataset_t
*ds
= dmu_objset_ds(dsa
->dsa_os
);
627 const blkptr_t
*bp
= &data
->bp
;
628 const zbookmark_phys_t
*zb
= &data
->zb
;
629 uint8_t indblkshift
= data
->indblkshift
;
630 uint16_t dblkszsec
= data
->datablkszsec
;
631 spa_t
*spa
= ds
->ds_dir
->dd_pool
->dp_spa
;
632 dmu_object_type_t type
= bp
? BP_GET_TYPE(bp
) : DMU_OT_NONE
;
636 ASSERT3U(zb
->zb_level
, >=, 0);
638 ASSERT(zb
->zb_object
== DMU_META_DNODE_OBJECT
||
639 zb
->zb_object
>= dsa
->dsa_resume_object
);
641 if (zb
->zb_object
!= DMU_META_DNODE_OBJECT
&&
642 DMU_OBJECT_IS_SPECIAL(zb
->zb_object
)) {
644 } else if (BP_IS_HOLE(bp
) &&
645 zb
->zb_object
== DMU_META_DNODE_OBJECT
) {
646 uint64_t span
= BP_SPAN(dblkszsec
, indblkshift
, zb
->zb_level
);
647 uint64_t dnobj
= (zb
->zb_blkid
* span
) >> DNODE_SHIFT
;
648 err
= dump_freeobjects(dsa
, dnobj
, span
>> DNODE_SHIFT
);
649 } else if (BP_IS_HOLE(bp
)) {
650 uint64_t span
= BP_SPAN(dblkszsec
, indblkshift
, zb
->zb_level
);
651 uint64_t offset
= zb
->zb_blkid
* span
;
652 err
= dump_free(dsa
, zb
->zb_object
, offset
, span
);
653 } else if (zb
->zb_level
> 0 || type
== DMU_OT_OBJSET
) {
655 } else if (type
== DMU_OT_DNODE
) {
657 int epb
= BP_GET_LSIZE(bp
) >> DNODE_SHIFT
;
658 arc_flags_t aflags
= ARC_FLAG_WAIT
;
662 ASSERT0(zb
->zb_level
);
664 if (arc_read(NULL
, spa
, bp
, arc_getbuf_func
, &abuf
,
665 ZIO_PRIORITY_ASYNC_READ
, ZIO_FLAG_CANFAIL
,
667 return (SET_ERROR(EIO
));
670 dnobj
= zb
->zb_blkid
* epb
;
671 for (i
= 0; i
< epb
; i
+= blk
[i
].dn_extra_slots
+ 1) {
672 err
= dump_dnode(dsa
, dnobj
+ i
, blk
+ i
);
676 arc_buf_destroy(abuf
, &abuf
);
677 } else if (type
== DMU_OT_SA
) {
678 arc_flags_t aflags
= ARC_FLAG_WAIT
;
680 int blksz
= BP_GET_LSIZE(bp
);
682 if (arc_read(NULL
, spa
, bp
, arc_getbuf_func
, &abuf
,
683 ZIO_PRIORITY_ASYNC_READ
, ZIO_FLAG_CANFAIL
,
685 return (SET_ERROR(EIO
));
687 err
= dump_spill(dsa
, zb
->zb_object
, blksz
, abuf
->b_data
);
688 arc_buf_destroy(abuf
, &abuf
);
689 } else if (backup_do_embed(dsa
, bp
)) {
690 /* it's an embedded level-0 block of a regular object */
691 int blksz
= dblkszsec
<< SPA_MINBLOCKSHIFT
;
692 ASSERT0(zb
->zb_level
);
693 err
= dump_write_embedded(dsa
, zb
->zb_object
,
694 zb
->zb_blkid
* blksz
, blksz
, bp
);
696 /* it's a level-0 block of a regular object */
697 arc_flags_t aflags
= ARC_FLAG_WAIT
;
699 int blksz
= dblkszsec
<< SPA_MINBLOCKSHIFT
;
701 enum zio_flag zioflags
= ZIO_FLAG_CANFAIL
;
704 * If we have large blocks stored on disk but the send flags
705 * don't allow us to send large blocks, we split the data from
706 * the arc buf into chunks.
708 boolean_t split_large_blocks
=
709 data
->datablkszsec
> SPA_OLD_MAXBLOCKSIZE
&&
710 !(dsa
->dsa_featureflags
& DMU_BACKUP_FEATURE_LARGE_BLOCKS
);
712 * We should only request compressed data from the ARC if all
713 * the following are true:
714 * - stream compression was requested
715 * - we aren't splitting large blocks into smaller chunks
716 * - the data won't need to be byteswapped before sending
717 * - this isn't an embedded block
718 * - this isn't metadata (if receiving on a different endian
719 * system it can be byteswapped more easily)
721 boolean_t request_compressed
=
722 (dsa
->dsa_featureflags
& DMU_BACKUP_FEATURE_COMPRESSED
) &&
723 !split_large_blocks
&& !BP_SHOULD_BYTESWAP(bp
) &&
724 !BP_IS_EMBEDDED(bp
) && !DMU_OT_IS_METADATA(BP_GET_TYPE(bp
));
726 ASSERT0(zb
->zb_level
);
727 ASSERT(zb
->zb_object
> dsa
->dsa_resume_object
||
728 (zb
->zb_object
== dsa
->dsa_resume_object
&&
729 zb
->zb_blkid
* blksz
>= dsa
->dsa_resume_offset
));
731 if (request_compressed
)
732 zioflags
|= ZIO_FLAG_RAW
;
734 if (arc_read(NULL
, spa
, bp
, arc_getbuf_func
, &abuf
,
735 ZIO_PRIORITY_ASYNC_READ
, zioflags
,
737 if (zfs_send_corrupt_data
) {
739 /* Send a block filled with 0x"zfs badd bloc" */
740 abuf
= arc_alloc_buf(spa
, &abuf
, ARC_BUFC_DATA
,
742 for (ptr
= abuf
->b_data
;
743 (char *)ptr
< (char *)abuf
->b_data
+ blksz
;
745 *ptr
= 0x2f5baddb10cULL
;
747 return (SET_ERROR(EIO
));
751 offset
= zb
->zb_blkid
* blksz
;
753 if (split_large_blocks
) {
754 char *buf
= abuf
->b_data
;
755 ASSERT3U(arc_get_compression(abuf
), ==,
757 while (blksz
> 0 && err
== 0) {
758 int n
= MIN(blksz
, SPA_OLD_MAXBLOCKSIZE
);
759 err
= dump_write(dsa
, type
, zb
->zb_object
,
760 offset
, n
, n
, NULL
, buf
);
766 err
= dump_write(dsa
, type
, zb
->zb_object
, offset
,
767 blksz
, arc_buf_size(abuf
), bp
,
770 arc_buf_destroy(abuf
, &abuf
);
773 ASSERT(err
== 0 || err
== EINTR
);
778 * Pop the new data off the queue, and free the old data.
780 static struct send_block_record
*
781 get_next_record(bqueue_t
*bq
, struct send_block_record
*data
)
783 struct send_block_record
*tmp
= bqueue_dequeue(bq
);
784 kmem_free(data
, sizeof (*data
));
789 * Actually do the bulk of the work in a zfs send.
791 * Note: Releases dp using the specified tag.
794 dmu_send_impl(void *tag
, dsl_pool_t
*dp
, dsl_dataset_t
*to_ds
,
795 zfs_bookmark_phys_t
*ancestor_zb
, boolean_t is_clone
,
796 boolean_t embedok
, boolean_t large_block_ok
, boolean_t compressok
,
797 int outfd
, uint64_t resumeobj
, uint64_t resumeoff
,
798 vnode_t
*vp
, offset_t
*off
)
801 dmu_replay_record_t
*drr
;
804 uint64_t fromtxg
= 0;
805 uint64_t featureflags
= 0;
806 struct send_thread_arg to_arg
;
807 void *payload
= NULL
;
808 size_t payload_len
= 0;
809 struct send_block_record
*to_data
;
811 err
= dmu_objset_from_ds(to_ds
, &os
);
813 dsl_pool_rele(dp
, tag
);
817 drr
= kmem_zalloc(sizeof (dmu_replay_record_t
), KM_SLEEP
);
818 drr
->drr_type
= DRR_BEGIN
;
819 drr
->drr_u
.drr_begin
.drr_magic
= DMU_BACKUP_MAGIC
;
820 DMU_SET_STREAM_HDRTYPE(drr
->drr_u
.drr_begin
.drr_versioninfo
,
823 bzero(&to_arg
, sizeof (to_arg
));
826 if (dmu_objset_type(os
) == DMU_OST_ZFS
) {
828 if (zfs_get_zplprop(os
, ZFS_PROP_VERSION
, &version
) != 0) {
829 kmem_free(drr
, sizeof (dmu_replay_record_t
));
830 dsl_pool_rele(dp
, tag
);
831 return (SET_ERROR(EINVAL
));
833 if (version
>= ZPL_VERSION_SA
) {
834 featureflags
|= DMU_BACKUP_FEATURE_SA_SPILL
;
839 if (large_block_ok
&& to_ds
->ds_feature_inuse
[SPA_FEATURE_LARGE_BLOCKS
])
840 featureflags
|= DMU_BACKUP_FEATURE_LARGE_BLOCKS
;
841 if (to_ds
->ds_feature_inuse
[SPA_FEATURE_LARGE_DNODE
])
842 featureflags
|= DMU_BACKUP_FEATURE_LARGE_DNODE
;
844 spa_feature_is_active(dp
->dp_spa
, SPA_FEATURE_EMBEDDED_DATA
)) {
845 featureflags
|= DMU_BACKUP_FEATURE_EMBED_DATA
;
848 featureflags
|= DMU_BACKUP_FEATURE_COMPRESSED
;
851 (DMU_BACKUP_FEATURE_EMBED_DATA
| DMU_BACKUP_FEATURE_COMPRESSED
)) !=
852 0 && spa_feature_is_active(dp
->dp_spa
, SPA_FEATURE_LZ4_COMPRESS
)) {
853 featureflags
|= DMU_BACKUP_FEATURE_LZ4
;
856 if (resumeobj
!= 0 || resumeoff
!= 0) {
857 featureflags
|= DMU_BACKUP_FEATURE_RESUMING
;
860 DMU_SET_FEATUREFLAGS(drr
->drr_u
.drr_begin
.drr_versioninfo
,
863 drr
->drr_u
.drr_begin
.drr_creation_time
=
864 dsl_dataset_phys(to_ds
)->ds_creation_time
;
865 drr
->drr_u
.drr_begin
.drr_type
= dmu_objset_type(os
);
867 drr
->drr_u
.drr_begin
.drr_flags
|= DRR_FLAG_CLONE
;
868 drr
->drr_u
.drr_begin
.drr_toguid
= dsl_dataset_phys(to_ds
)->ds_guid
;
869 if (dsl_dataset_phys(to_ds
)->ds_flags
& DS_FLAG_CI_DATASET
)
870 drr
->drr_u
.drr_begin
.drr_flags
|= DRR_FLAG_CI_DATA
;
871 if (zfs_send_set_freerecords_bit
)
872 drr
->drr_u
.drr_begin
.drr_flags
|= DRR_FLAG_FREERECORDS
;
874 if (ancestor_zb
!= NULL
) {
875 drr
->drr_u
.drr_begin
.drr_fromguid
=
876 ancestor_zb
->zbm_guid
;
877 fromtxg
= ancestor_zb
->zbm_creation_txg
;
879 dsl_dataset_name(to_ds
, drr
->drr_u
.drr_begin
.drr_toname
);
880 if (!to_ds
->ds_is_snapshot
) {
881 (void) strlcat(drr
->drr_u
.drr_begin
.drr_toname
, "@--head--",
882 sizeof (drr
->drr_u
.drr_begin
.drr_toname
));
885 dsp
= kmem_zalloc(sizeof (dmu_sendarg_t
), KM_SLEEP
);
889 dsp
->dsa_outfd
= outfd
;
890 dsp
->dsa_proc
= curproc
;
893 dsp
->dsa_toguid
= dsl_dataset_phys(to_ds
)->ds_guid
;
894 dsp
->dsa_pending_op
= PENDING_NONE
;
895 dsp
->dsa_featureflags
= featureflags
;
896 dsp
->dsa_resume_object
= resumeobj
;
897 dsp
->dsa_resume_offset
= resumeoff
;
899 mutex_enter(&to_ds
->ds_sendstream_lock
);
900 list_insert_head(&to_ds
->ds_sendstreams
, dsp
);
901 mutex_exit(&to_ds
->ds_sendstream_lock
);
903 dsl_dataset_long_hold(to_ds
, FTAG
);
904 dsl_pool_rele(dp
, tag
);
906 if (resumeobj
!= 0 || resumeoff
!= 0) {
907 dmu_object_info_t to_doi
;
909 err
= dmu_object_info(os
, resumeobj
, &to_doi
);
912 SET_BOOKMARK(&to_arg
.resume
, to_ds
->ds_object
, resumeobj
, 0,
913 resumeoff
/ to_doi
.doi_data_block_size
);
915 nvl
= fnvlist_alloc();
916 fnvlist_add_uint64(nvl
, "resume_object", resumeobj
);
917 fnvlist_add_uint64(nvl
, "resume_offset", resumeoff
);
918 payload
= fnvlist_pack(nvl
, &payload_len
);
919 drr
->drr_payloadlen
= payload_len
;
923 err
= dump_record(dsp
, payload
, payload_len
);
924 fnvlist_pack_free(payload
, payload_len
);
930 err
= bqueue_init(&to_arg
.q
, zfs_send_queue_length
,
931 offsetof(struct send_block_record
, ln
));
932 to_arg
.error_code
= 0;
933 to_arg
.cancel
= B_FALSE
;
935 to_arg
.fromtxg
= fromtxg
;
936 to_arg
.flags
= TRAVERSE_PRE
| TRAVERSE_PREFETCH
;
937 (void) thread_create(NULL
, 0, send_traverse_thread
, &to_arg
, 0, curproc
,
938 TS_RUN
, minclsyspri
);
940 to_data
= bqueue_dequeue(&to_arg
.q
);
942 while (!to_data
->eos_marker
&& err
== 0) {
943 err
= do_dump(dsp
, to_data
);
944 to_data
= get_next_record(&to_arg
.q
, to_data
);
945 if (issig(JUSTLOOKING
) && issig(FORREAL
))
950 to_arg
.cancel
= B_TRUE
;
951 while (!to_data
->eos_marker
) {
952 to_data
= get_next_record(&to_arg
.q
, to_data
);
955 kmem_free(to_data
, sizeof (*to_data
));
957 bqueue_destroy(&to_arg
.q
);
959 if (err
== 0 && to_arg
.error_code
!= 0)
960 err
= to_arg
.error_code
;
965 if (dsp
->dsa_pending_op
!= PENDING_NONE
)
966 if (dump_record(dsp
, NULL
, 0) != 0)
967 err
= SET_ERROR(EINTR
);
970 if (err
== EINTR
&& dsp
->dsa_err
!= 0)
975 bzero(drr
, sizeof (dmu_replay_record_t
));
976 drr
->drr_type
= DRR_END
;
977 drr
->drr_u
.drr_end
.drr_checksum
= dsp
->dsa_zc
;
978 drr
->drr_u
.drr_end
.drr_toguid
= dsp
->dsa_toguid
;
980 if (dump_record(dsp
, NULL
, 0) != 0)
984 mutex_enter(&to_ds
->ds_sendstream_lock
);
985 list_remove(&to_ds
->ds_sendstreams
, dsp
);
986 mutex_exit(&to_ds
->ds_sendstream_lock
);
988 VERIFY(err
!= 0 || (dsp
->dsa_sent_begin
&& dsp
->dsa_sent_end
));
990 kmem_free(drr
, sizeof (dmu_replay_record_t
));
991 kmem_free(dsp
, sizeof (dmu_sendarg_t
));
993 dsl_dataset_long_rele(to_ds
, FTAG
);
999 dmu_send_obj(const char *pool
, uint64_t tosnap
, uint64_t fromsnap
,
1000 boolean_t embedok
, boolean_t large_block_ok
, boolean_t compressok
,
1001 int outfd
, vnode_t
*vp
, offset_t
*off
)
1005 dsl_dataset_t
*fromds
= NULL
;
1008 err
= dsl_pool_hold(pool
, FTAG
, &dp
);
1012 err
= dsl_dataset_hold_obj(dp
, tosnap
, FTAG
, &ds
);
1014 dsl_pool_rele(dp
, FTAG
);
1018 if (fromsnap
!= 0) {
1019 zfs_bookmark_phys_t zb
;
1022 err
= dsl_dataset_hold_obj(dp
, fromsnap
, FTAG
, &fromds
);
1024 dsl_dataset_rele(ds
, FTAG
);
1025 dsl_pool_rele(dp
, FTAG
);
1028 if (!dsl_dataset_is_before(ds
, fromds
, 0))
1029 err
= SET_ERROR(EXDEV
);
1030 zb
.zbm_creation_time
=
1031 dsl_dataset_phys(fromds
)->ds_creation_time
;
1032 zb
.zbm_creation_txg
= dsl_dataset_phys(fromds
)->ds_creation_txg
;
1033 zb
.zbm_guid
= dsl_dataset_phys(fromds
)->ds_guid
;
1034 is_clone
= (fromds
->ds_dir
!= ds
->ds_dir
);
1035 dsl_dataset_rele(fromds
, FTAG
);
1036 err
= dmu_send_impl(FTAG
, dp
, ds
, &zb
, is_clone
,
1037 embedok
, large_block_ok
, compressok
, outfd
, 0, 0, vp
, off
);
1039 err
= dmu_send_impl(FTAG
, dp
, ds
, NULL
, B_FALSE
,
1040 embedok
, large_block_ok
, compressok
, outfd
, 0, 0, vp
, off
);
1042 dsl_dataset_rele(ds
, FTAG
);
1047 dmu_send(const char *tosnap
, const char *fromsnap
, boolean_t embedok
,
1048 boolean_t large_block_ok
, boolean_t compressok
, int outfd
,
1049 uint64_t resumeobj
, uint64_t resumeoff
,
1050 vnode_t
*vp
, offset_t
*off
)
1055 boolean_t owned
= B_FALSE
;
1057 if (fromsnap
!= NULL
&& strpbrk(fromsnap
, "@#") == NULL
)
1058 return (SET_ERROR(EINVAL
));
1060 err
= dsl_pool_hold(tosnap
, FTAG
, &dp
);
1064 if (strchr(tosnap
, '@') == NULL
&& spa_writeable(dp
->dp_spa
)) {
1066 * We are sending a filesystem or volume. Ensure
1067 * that it doesn't change by owning the dataset.
1069 err
= dsl_dataset_own(dp
, tosnap
, FTAG
, &ds
);
1072 err
= dsl_dataset_hold(dp
, tosnap
, FTAG
, &ds
);
1075 dsl_pool_rele(dp
, FTAG
);
1079 if (fromsnap
!= NULL
) {
1080 zfs_bookmark_phys_t zb
;
1081 boolean_t is_clone
= B_FALSE
;
1082 int fsnamelen
= strchr(tosnap
, '@') - tosnap
;
1085 * If the fromsnap is in a different filesystem, then
1086 * mark the send stream as a clone.
1088 if (strncmp(tosnap
, fromsnap
, fsnamelen
) != 0 ||
1089 (fromsnap
[fsnamelen
] != '@' &&
1090 fromsnap
[fsnamelen
] != '#')) {
1094 if (strchr(fromsnap
, '@')) {
1095 dsl_dataset_t
*fromds
;
1096 err
= dsl_dataset_hold(dp
, fromsnap
, FTAG
, &fromds
);
1098 if (!dsl_dataset_is_before(ds
, fromds
, 0))
1099 err
= SET_ERROR(EXDEV
);
1100 zb
.zbm_creation_time
=
1101 dsl_dataset_phys(fromds
)->ds_creation_time
;
1102 zb
.zbm_creation_txg
=
1103 dsl_dataset_phys(fromds
)->ds_creation_txg
;
1104 zb
.zbm_guid
= dsl_dataset_phys(fromds
)->ds_guid
;
1105 is_clone
= (ds
->ds_dir
!= fromds
->ds_dir
);
1106 dsl_dataset_rele(fromds
, FTAG
);
1109 err
= dsl_bookmark_lookup(dp
, fromsnap
, ds
, &zb
);
1112 dsl_dataset_rele(ds
, FTAG
);
1113 dsl_pool_rele(dp
, FTAG
);
1116 err
= dmu_send_impl(FTAG
, dp
, ds
, &zb
, is_clone
,
1117 embedok
, large_block_ok
, compressok
,
1118 outfd
, resumeobj
, resumeoff
, vp
, off
);
1120 err
= dmu_send_impl(FTAG
, dp
, ds
, NULL
, B_FALSE
,
1121 embedok
, large_block_ok
, compressok
,
1122 outfd
, resumeobj
, resumeoff
, vp
, off
);
1125 dsl_dataset_disown(ds
, FTAG
);
1127 dsl_dataset_rele(ds
, FTAG
);
1132 dmu_adjust_send_estimate_for_indirects(dsl_dataset_t
*ds
, uint64_t uncompressed
,
1133 uint64_t compressed
, boolean_t stream_compressed
, uint64_t *sizep
)
1138 * Assume that space (both on-disk and in-stream) is dominated by
1139 * data. We will adjust for indirect blocks and the copies property,
1140 * but ignore per-object space used (eg, dnodes and DRR_OBJECT records).
1143 uint64_t recordsize
;
1144 uint64_t record_count
;
1146 /* Assume all (uncompressed) blocks are recordsize. */
1147 err
= dsl_prop_get_int_ds(ds
, zfs_prop_to_name(ZFS_PROP_RECORDSIZE
),
1151 record_count
= uncompressed
/ recordsize
;
1154 * If we're estimating a send size for a compressed stream, use the
1155 * compressed data size to estimate the stream size. Otherwise, use the
1156 * uncompressed data size.
1158 size
= stream_compressed
? compressed
: uncompressed
;
1161 * Subtract out approximate space used by indirect blocks.
1162 * Assume most space is used by data blocks (non-indirect, non-dnode).
1163 * Assume no ditto blocks or internal fragmentation.
1165 * Therefore, space used by indirect blocks is sizeof(blkptr_t) per
1168 size
-= record_count
* sizeof (blkptr_t
);
1170 /* Add in the space for the record associated with each block. */
1171 size
+= record_count
* sizeof (dmu_replay_record_t
);
1179 dmu_send_estimate(dsl_dataset_t
*ds
, dsl_dataset_t
*fromds
,
1180 boolean_t stream_compressed
, uint64_t *sizep
)
1183 uint64_t uncomp
, comp
;
1185 ASSERT(dsl_pool_config_held(ds
->ds_dir
->dd_pool
));
1187 /* tosnap must be a snapshot */
1188 if (!ds
->ds_is_snapshot
)
1189 return (SET_ERROR(EINVAL
));
1191 /* fromsnap, if provided, must be a snapshot */
1192 if (fromds
!= NULL
&& !fromds
->ds_is_snapshot
)
1193 return (SET_ERROR(EINVAL
));
1196 * fromsnap must be an earlier snapshot from the same fs as tosnap,
1197 * or the origin's fs.
1199 if (fromds
!= NULL
&& !dsl_dataset_is_before(ds
, fromds
, 0))
1200 return (SET_ERROR(EXDEV
));
1202 /* Get compressed and uncompressed size estimates of changed data. */
1203 if (fromds
== NULL
) {
1204 uncomp
= dsl_dataset_phys(ds
)->ds_uncompressed_bytes
;
1205 comp
= dsl_dataset_phys(ds
)->ds_compressed_bytes
;
1208 err
= dsl_dataset_space_written(fromds
, ds
,
1209 &used
, &comp
, &uncomp
);
1214 err
= dmu_adjust_send_estimate_for_indirects(ds
, uncomp
, comp
,
1215 stream_compressed
, sizep
);
1219 struct calculate_send_arg
{
1220 uint64_t uncompressed
;
1221 uint64_t compressed
;
1225 * Simple callback used to traverse the blocks of a snapshot and sum their
1226 * uncompressed and compressed sizes.
1230 dmu_calculate_send_traversal(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
1231 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
1233 struct calculate_send_arg
*space
= arg
;
1234 if (bp
!= NULL
&& !BP_IS_HOLE(bp
)) {
1235 space
->uncompressed
+= BP_GET_UCSIZE(bp
);
1236 space
->compressed
+= BP_GET_PSIZE(bp
);
1242 * Given a desination snapshot and a TXG, calculate the approximate size of a
1243 * send stream sent from that TXG. from_txg may be zero, indicating that the
1244 * whole snapshot will be sent.
1247 dmu_send_estimate_from_txg(dsl_dataset_t
*ds
, uint64_t from_txg
,
1248 boolean_t stream_compressed
, uint64_t *sizep
)
1251 struct calculate_send_arg size
= { 0 };
1253 ASSERT(dsl_pool_config_held(ds
->ds_dir
->dd_pool
));
1255 /* tosnap must be a snapshot */
1256 if (!dsl_dataset_is_snapshot(ds
))
1257 return (SET_ERROR(EINVAL
));
1259 /* verify that from_txg is before the provided snapshot was taken */
1260 if (from_txg
>= dsl_dataset_phys(ds
)->ds_creation_txg
) {
1261 return (SET_ERROR(EXDEV
));
1264 * traverse the blocks of the snapshot with birth times after
1265 * from_txg, summing their uncompressed size
1267 err
= traverse_dataset(ds
, from_txg
, TRAVERSE_POST
,
1268 dmu_calculate_send_traversal
, &size
);
1273 err
= dmu_adjust_send_estimate_for_indirects(ds
, size
.uncompressed
,
1274 size
.compressed
, stream_compressed
, sizep
);
1278 typedef struct dmu_recv_begin_arg
{
1279 const char *drba_origin
;
1280 dmu_recv_cookie_t
*drba_cookie
;
1282 uint64_t drba_snapobj
;
1283 } dmu_recv_begin_arg_t
;
1286 recv_begin_check_existing_impl(dmu_recv_begin_arg_t
*drba
, dsl_dataset_t
*ds
,
1291 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
1293 /* temporary clone name must not exist */
1294 error
= zap_lookup(dp
->dp_meta_objset
,
1295 dsl_dir_phys(ds
->ds_dir
)->dd_child_dir_zapobj
, recv_clone_name
,
1297 if (error
!= ENOENT
)
1298 return (error
== 0 ? EBUSY
: error
);
1300 /* new snapshot name must not exist */
1301 error
= zap_lookup(dp
->dp_meta_objset
,
1302 dsl_dataset_phys(ds
)->ds_snapnames_zapobj
,
1303 drba
->drba_cookie
->drc_tosnap
, 8, 1, &val
);
1304 if (error
!= ENOENT
)
1305 return (error
== 0 ? EEXIST
: error
);
1308 * Check snapshot limit before receiving. We'll recheck again at the
1309 * end, but might as well abort before receiving if we're already over
1312 * Note that we do not check the file system limit with
1313 * dsl_dir_fscount_check because the temporary %clones don't count
1314 * against that limit.
1316 error
= dsl_fs_ss_limit_check(ds
->ds_dir
, 1, ZFS_PROP_SNAPSHOT_LIMIT
,
1317 NULL
, drba
->drba_cred
);
1321 if (fromguid
!= 0) {
1322 dsl_dataset_t
*snap
;
1323 uint64_t obj
= dsl_dataset_phys(ds
)->ds_prev_snap_obj
;
1325 /* Find snapshot in this dir that matches fromguid. */
1327 error
= dsl_dataset_hold_obj(dp
, obj
, FTAG
,
1330 return (SET_ERROR(ENODEV
));
1331 if (snap
->ds_dir
!= ds
->ds_dir
) {
1332 dsl_dataset_rele(snap
, FTAG
);
1333 return (SET_ERROR(ENODEV
));
1335 if (dsl_dataset_phys(snap
)->ds_guid
== fromguid
)
1337 obj
= dsl_dataset_phys(snap
)->ds_prev_snap_obj
;
1338 dsl_dataset_rele(snap
, FTAG
);
1341 return (SET_ERROR(ENODEV
));
1343 if (drba
->drba_cookie
->drc_force
) {
1344 drba
->drba_snapobj
= obj
;
1347 * If we are not forcing, there must be no
1348 * changes since fromsnap.
1350 if (dsl_dataset_modified_since_snap(ds
, snap
)) {
1351 dsl_dataset_rele(snap
, FTAG
);
1352 return (SET_ERROR(ETXTBSY
));
1354 drba
->drba_snapobj
= ds
->ds_prev
->ds_object
;
1357 dsl_dataset_rele(snap
, FTAG
);
1359 /* if full, then must be forced */
1360 if (!drba
->drba_cookie
->drc_force
)
1361 return (SET_ERROR(EEXIST
));
1362 /* start from $ORIGIN@$ORIGIN, if supported */
1363 drba
->drba_snapobj
= dp
->dp_origin_snap
!= NULL
?
1364 dp
->dp_origin_snap
->ds_object
: 0;
1372 dmu_recv_begin_check(void *arg
, dmu_tx_t
*tx
)
1374 dmu_recv_begin_arg_t
*drba
= arg
;
1375 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1376 struct drr_begin
*drrb
= drba
->drba_cookie
->drc_drrb
;
1377 uint64_t fromguid
= drrb
->drr_fromguid
;
1378 int flags
= drrb
->drr_flags
;
1380 uint64_t featureflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
1382 const char *tofs
= drba
->drba_cookie
->drc_tofs
;
1384 /* already checked */
1385 ASSERT3U(drrb
->drr_magic
, ==, DMU_BACKUP_MAGIC
);
1386 ASSERT(!(featureflags
& DMU_BACKUP_FEATURE_RESUMING
));
1388 if (DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) ==
1389 DMU_COMPOUNDSTREAM
||
1390 drrb
->drr_type
>= DMU_OST_NUMTYPES
||
1391 ((flags
& DRR_FLAG_CLONE
) && drba
->drba_origin
== NULL
))
1392 return (SET_ERROR(EINVAL
));
1394 /* Verify pool version supports SA if SA_SPILL feature set */
1395 if ((featureflags
& DMU_BACKUP_FEATURE_SA_SPILL
) &&
1396 spa_version(dp
->dp_spa
) < SPA_VERSION_SA
)
1397 return (SET_ERROR(ENOTSUP
));
1399 if (drba
->drba_cookie
->drc_resumable
&&
1400 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_EXTENSIBLE_DATASET
))
1401 return (SET_ERROR(ENOTSUP
));
1404 * The receiving code doesn't know how to translate a WRITE_EMBEDDED
1405 * record to a plain WRITE record, so the pool must have the
1406 * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
1407 * records. Same with WRITE_EMBEDDED records that use LZ4 compression.
1409 if ((featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA
) &&
1410 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_EMBEDDED_DATA
))
1411 return (SET_ERROR(ENOTSUP
));
1412 if ((featureflags
& DMU_BACKUP_FEATURE_LZ4
) &&
1413 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_LZ4_COMPRESS
))
1414 return (SET_ERROR(ENOTSUP
));
1417 * The receiving code doesn't know how to translate large blocks
1418 * to smaller ones, so the pool must have the LARGE_BLOCKS
1419 * feature enabled if the stream has LARGE_BLOCKS.
1421 if ((featureflags
& DMU_BACKUP_FEATURE_LARGE_BLOCKS
) &&
1422 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_LARGE_BLOCKS
))
1423 return (SET_ERROR(ENOTSUP
));
1426 * The receiving code doesn't know how to translate large dnodes
1427 * to smaller ones, so the pool must have the LARGE_DNODE
1428 * feature enabled if the stream has LARGE_DNODE.
1430 if ((featureflags
& DMU_BACKUP_FEATURE_LARGE_DNODE
) &&
1431 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_LARGE_DNODE
))
1432 return (SET_ERROR(ENOTSUP
));
1434 error
= dsl_dataset_hold(dp
, tofs
, FTAG
, &ds
);
1436 /* target fs already exists; recv into temp clone */
1438 /* Can't recv a clone into an existing fs */
1439 if (flags
& DRR_FLAG_CLONE
|| drba
->drba_origin
) {
1440 dsl_dataset_rele(ds
, FTAG
);
1441 return (SET_ERROR(EINVAL
));
1444 error
= recv_begin_check_existing_impl(drba
, ds
, fromguid
);
1445 dsl_dataset_rele(ds
, FTAG
);
1446 } else if (error
== ENOENT
) {
1447 /* target fs does not exist; must be a full backup or clone */
1448 char buf
[ZFS_MAX_DATASET_NAME_LEN
];
1451 * If it's a non-clone incremental, we are missing the
1452 * target fs, so fail the recv.
1454 if (fromguid
!= 0 && !(flags
& DRR_FLAG_CLONE
||
1456 return (SET_ERROR(ENOENT
));
1459 * If we're receiving a full send as a clone, and it doesn't
1460 * contain all the necessary free records and freeobject
1461 * records, reject it.
1463 if (fromguid
== 0 && drba
->drba_origin
&&
1464 !(flags
& DRR_FLAG_FREERECORDS
))
1465 return (SET_ERROR(EINVAL
));
1467 /* Open the parent of tofs */
1468 ASSERT3U(strlen(tofs
), <, sizeof (buf
));
1469 (void) strlcpy(buf
, tofs
, strrchr(tofs
, '/') - tofs
+ 1);
1470 error
= dsl_dataset_hold(dp
, buf
, FTAG
, &ds
);
1475 * Check filesystem and snapshot limits before receiving. We'll
1476 * recheck snapshot limits again at the end (we create the
1477 * filesystems and increment those counts during begin_sync).
1479 error
= dsl_fs_ss_limit_check(ds
->ds_dir
, 1,
1480 ZFS_PROP_FILESYSTEM_LIMIT
, NULL
, drba
->drba_cred
);
1482 dsl_dataset_rele(ds
, FTAG
);
1486 error
= dsl_fs_ss_limit_check(ds
->ds_dir
, 1,
1487 ZFS_PROP_SNAPSHOT_LIMIT
, NULL
, drba
->drba_cred
);
1489 dsl_dataset_rele(ds
, FTAG
);
1493 if (drba
->drba_origin
!= NULL
) {
1494 dsl_dataset_t
*origin
;
1495 error
= dsl_dataset_hold(dp
, drba
->drba_origin
,
1498 dsl_dataset_rele(ds
, FTAG
);
1501 if (!origin
->ds_is_snapshot
) {
1502 dsl_dataset_rele(origin
, FTAG
);
1503 dsl_dataset_rele(ds
, FTAG
);
1504 return (SET_ERROR(EINVAL
));
1506 if (dsl_dataset_phys(origin
)->ds_guid
!= fromguid
&&
1508 dsl_dataset_rele(origin
, FTAG
);
1509 dsl_dataset_rele(ds
, FTAG
);
1510 return (SET_ERROR(ENODEV
));
1512 dsl_dataset_rele(origin
, FTAG
);
1514 dsl_dataset_rele(ds
, FTAG
);
1521 dmu_recv_begin_sync(void *arg
, dmu_tx_t
*tx
)
1523 dmu_recv_begin_arg_t
*drba
= arg
;
1524 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1525 objset_t
*mos
= dp
->dp_meta_objset
;
1526 struct drr_begin
*drrb
= drba
->drba_cookie
->drc_drrb
;
1527 const char *tofs
= drba
->drba_cookie
->drc_tofs
;
1528 dsl_dataset_t
*ds
, *newds
;
1531 uint64_t crflags
= 0;
1533 if (drrb
->drr_flags
& DRR_FLAG_CI_DATA
)
1534 crflags
|= DS_FLAG_CI_DATASET
;
1536 error
= dsl_dataset_hold(dp
, tofs
, FTAG
, &ds
);
1538 /* create temporary clone */
1539 dsl_dataset_t
*snap
= NULL
;
1540 if (drba
->drba_snapobj
!= 0) {
1541 VERIFY0(dsl_dataset_hold_obj(dp
,
1542 drba
->drba_snapobj
, FTAG
, &snap
));
1544 dsobj
= dsl_dataset_create_sync(ds
->ds_dir
, recv_clone_name
,
1545 snap
, crflags
, drba
->drba_cred
, tx
);
1546 if (drba
->drba_snapobj
!= 0)
1547 dsl_dataset_rele(snap
, FTAG
);
1548 dsl_dataset_rele(ds
, FTAG
);
1552 dsl_dataset_t
*origin
= NULL
;
1554 VERIFY0(dsl_dir_hold(dp
, tofs
, FTAG
, &dd
, &tail
));
1556 if (drba
->drba_origin
!= NULL
) {
1557 VERIFY0(dsl_dataset_hold(dp
, drba
->drba_origin
,
1561 /* Create new dataset. */
1562 dsobj
= dsl_dataset_create_sync(dd
,
1563 strrchr(tofs
, '/') + 1,
1564 origin
, crflags
, drba
->drba_cred
, tx
);
1566 dsl_dataset_rele(origin
, FTAG
);
1567 dsl_dir_rele(dd
, FTAG
);
1568 drba
->drba_cookie
->drc_newfs
= B_TRUE
;
1570 VERIFY0(dsl_dataset_own_obj(dp
, dsobj
, dmu_recv_tag
, &newds
));
1572 if (drba
->drba_cookie
->drc_resumable
) {
1576 dsl_dataset_zapify(newds
, tx
);
1577 if (drrb
->drr_fromguid
!= 0) {
1578 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_FROMGUID
,
1579 8, 1, &drrb
->drr_fromguid
, tx
));
1581 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_TOGUID
,
1582 8, 1, &drrb
->drr_toguid
, tx
));
1583 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_TONAME
,
1584 1, strlen(drrb
->drr_toname
) + 1, drrb
->drr_toname
, tx
));
1585 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_OBJECT
,
1587 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_OFFSET
,
1589 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_BYTES
,
1591 if (DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
) &
1592 DMU_BACKUP_FEATURE_LARGE_BLOCKS
) {
1593 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_LARGEBLOCK
,
1596 if (DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
) &
1597 DMU_BACKUP_FEATURE_EMBED_DATA
) {
1598 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_EMBEDOK
,
1601 if (DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
) &
1602 DMU_BACKUP_FEATURE_COMPRESSED
) {
1603 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_COMPRESSOK
,
1608 dmu_buf_will_dirty(newds
->ds_dbuf
, tx
);
1609 dsl_dataset_phys(newds
)->ds_flags
|= DS_FLAG_INCONSISTENT
;
1612 * If we actually created a non-clone, we need to create the
1613 * objset in our new dataset.
1615 if (BP_IS_HOLE(dsl_dataset_get_blkptr(newds
))) {
1616 (void) dmu_objset_create_impl(dp
->dp_spa
,
1617 newds
, dsl_dataset_get_blkptr(newds
), drrb
->drr_type
, tx
);
1620 drba
->drba_cookie
->drc_ds
= newds
;
1622 spa_history_log_internal_ds(newds
, "receive", tx
, "");
1626 dmu_recv_resume_begin_check(void *arg
, dmu_tx_t
*tx
)
1628 dmu_recv_begin_arg_t
*drba
= arg
;
1629 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1630 struct drr_begin
*drrb
= drba
->drba_cookie
->drc_drrb
;
1632 uint64_t featureflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
1634 const char *tofs
= drba
->drba_cookie
->drc_tofs
;
1637 /* 6 extra bytes for /%recv */
1638 char recvname
[ZFS_MAX_DATASET_NAME_LEN
+ 6];
1640 /* already checked */
1641 ASSERT3U(drrb
->drr_magic
, ==, DMU_BACKUP_MAGIC
);
1642 ASSERT(featureflags
& DMU_BACKUP_FEATURE_RESUMING
);
1644 if (DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) ==
1645 DMU_COMPOUNDSTREAM
||
1646 drrb
->drr_type
>= DMU_OST_NUMTYPES
)
1647 return (SET_ERROR(EINVAL
));
1649 /* Verify pool version supports SA if SA_SPILL feature set */
1650 if ((featureflags
& DMU_BACKUP_FEATURE_SA_SPILL
) &&
1651 spa_version(dp
->dp_spa
) < SPA_VERSION_SA
)
1652 return (SET_ERROR(ENOTSUP
));
1655 * The receiving code doesn't know how to translate a WRITE_EMBEDDED
1656 * record to a plain WRITE record, so the pool must have the
1657 * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
1658 * records. Same with WRITE_EMBEDDED records that use LZ4 compression.
1660 if ((featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA
) &&
1661 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_EMBEDDED_DATA
))
1662 return (SET_ERROR(ENOTSUP
));
1663 if ((featureflags
& DMU_BACKUP_FEATURE_LZ4
) &&
1664 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_LZ4_COMPRESS
))
1665 return (SET_ERROR(ENOTSUP
));
1667 (void) snprintf(recvname
, sizeof (recvname
), "%s/%s",
1668 tofs
, recv_clone_name
);
1670 if (dsl_dataset_hold(dp
, recvname
, FTAG
, &ds
) != 0) {
1671 /* %recv does not exist; continue in tofs */
1672 error
= dsl_dataset_hold(dp
, tofs
, FTAG
, &ds
);
1677 /* check that ds is marked inconsistent */
1678 if (!DS_IS_INCONSISTENT(ds
)) {
1679 dsl_dataset_rele(ds
, FTAG
);
1680 return (SET_ERROR(EINVAL
));
1683 /* check that there is resuming data, and that the toguid matches */
1684 if (!dsl_dataset_is_zapified(ds
)) {
1685 dsl_dataset_rele(ds
, FTAG
);
1686 return (SET_ERROR(EINVAL
));
1688 error
= zap_lookup(dp
->dp_meta_objset
, ds
->ds_object
,
1689 DS_FIELD_RESUME_TOGUID
, sizeof (val
), 1, &val
);
1690 if (error
!= 0 || drrb
->drr_toguid
!= val
) {
1691 dsl_dataset_rele(ds
, FTAG
);
1692 return (SET_ERROR(EINVAL
));
1696 * Check if the receive is still running. If so, it will be owned.
1697 * Note that nothing else can own the dataset (e.g. after the receive
1698 * fails) because it will be marked inconsistent.
1700 if (dsl_dataset_has_owner(ds
)) {
1701 dsl_dataset_rele(ds
, FTAG
);
1702 return (SET_ERROR(EBUSY
));
1705 /* There should not be any snapshots of this fs yet. */
1706 if (ds
->ds_prev
!= NULL
&& ds
->ds_prev
->ds_dir
== ds
->ds_dir
) {
1707 dsl_dataset_rele(ds
, FTAG
);
1708 return (SET_ERROR(EINVAL
));
1712 * Note: resume point will be checked when we process the first WRITE
1716 /* check that the origin matches */
1718 (void) zap_lookup(dp
->dp_meta_objset
, ds
->ds_object
,
1719 DS_FIELD_RESUME_FROMGUID
, sizeof (val
), 1, &val
);
1720 if (drrb
->drr_fromguid
!= val
) {
1721 dsl_dataset_rele(ds
, FTAG
);
1722 return (SET_ERROR(EINVAL
));
1725 dsl_dataset_rele(ds
, FTAG
);
1730 dmu_recv_resume_begin_sync(void *arg
, dmu_tx_t
*tx
)
1732 dmu_recv_begin_arg_t
*drba
= arg
;
1733 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1734 const char *tofs
= drba
->drba_cookie
->drc_tofs
;
1737 /* 6 extra bytes for /%recv */
1738 char recvname
[ZFS_MAX_DATASET_NAME_LEN
+ 6];
1740 (void) snprintf(recvname
, sizeof (recvname
), "%s/%s",
1741 tofs
, recv_clone_name
);
1743 if (dsl_dataset_hold(dp
, recvname
, FTAG
, &ds
) != 0) {
1744 /* %recv does not exist; continue in tofs */
1745 VERIFY0(dsl_dataset_hold(dp
, tofs
, FTAG
, &ds
));
1746 drba
->drba_cookie
->drc_newfs
= B_TRUE
;
1749 /* clear the inconsistent flag so that we can own it */
1750 ASSERT(DS_IS_INCONSISTENT(ds
));
1751 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
1752 dsl_dataset_phys(ds
)->ds_flags
&= ~DS_FLAG_INCONSISTENT
;
1753 dsobj
= ds
->ds_object
;
1754 dsl_dataset_rele(ds
, FTAG
);
1756 VERIFY0(dsl_dataset_own_obj(dp
, dsobj
, dmu_recv_tag
, &ds
));
1758 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
1759 dsl_dataset_phys(ds
)->ds_flags
|= DS_FLAG_INCONSISTENT
;
1761 ASSERT(!BP_IS_HOLE(dsl_dataset_get_blkptr(ds
)));
1763 drba
->drba_cookie
->drc_ds
= ds
;
1765 spa_history_log_internal_ds(ds
, "resume receive", tx
, "");
1769 * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
1770 * succeeds; otherwise we will leak the holds on the datasets.
1773 dmu_recv_begin(char *tofs
, char *tosnap
, dmu_replay_record_t
*drr_begin
,
1774 boolean_t force
, boolean_t resumable
, char *origin
, dmu_recv_cookie_t
*drc
)
1776 dmu_recv_begin_arg_t drba
= { 0 };
1778 bzero(drc
, sizeof (dmu_recv_cookie_t
));
1779 drc
->drc_drr_begin
= drr_begin
;
1780 drc
->drc_drrb
= &drr_begin
->drr_u
.drr_begin
;
1781 drc
->drc_tosnap
= tosnap
;
1782 drc
->drc_tofs
= tofs
;
1783 drc
->drc_force
= force
;
1784 drc
->drc_resumable
= resumable
;
1785 drc
->drc_cred
= CRED();
1787 if (drc
->drc_drrb
->drr_magic
== BSWAP_64(DMU_BACKUP_MAGIC
)) {
1788 drc
->drc_byteswap
= B_TRUE
;
1789 (void) fletcher_4_incremental_byteswap(drr_begin
,
1790 sizeof (dmu_replay_record_t
), &drc
->drc_cksum
);
1791 byteswap_record(drr_begin
);
1792 } else if (drc
->drc_drrb
->drr_magic
== DMU_BACKUP_MAGIC
) {
1793 (void) fletcher_4_incremental_native(drr_begin
,
1794 sizeof (dmu_replay_record_t
), &drc
->drc_cksum
);
1796 return (SET_ERROR(EINVAL
));
1799 drba
.drba_origin
= origin
;
1800 drba
.drba_cookie
= drc
;
1801 drba
.drba_cred
= CRED();
1803 if (DMU_GET_FEATUREFLAGS(drc
->drc_drrb
->drr_versioninfo
) &
1804 DMU_BACKUP_FEATURE_RESUMING
) {
1805 return (dsl_sync_task(tofs
,
1806 dmu_recv_resume_begin_check
, dmu_recv_resume_begin_sync
,
1807 &drba
, 5, ZFS_SPACE_CHECK_NORMAL
));
1809 return (dsl_sync_task(tofs
,
1810 dmu_recv_begin_check
, dmu_recv_begin_sync
,
1811 &drba
, 5, ZFS_SPACE_CHECK_NORMAL
));
1815 struct receive_record_arg
{
1816 dmu_replay_record_t header
;
1817 void *payload
; /* Pointer to a buffer containing the payload */
1819 * If the record is a write, pointer to the arc_buf_t containing the
1822 arc_buf_t
*write_buf
;
1824 uint64_t bytes_read
; /* bytes read from stream when record created */
1825 boolean_t eos_marker
; /* Marks the end of the stream */
1829 struct receive_writer_arg
{
1835 * These three args are used to signal to the main thread that we're
1843 /* A map from guid to dataset to help handle dedup'd streams. */
1844 avl_tree_t
*guid_to_ds_map
;
1845 boolean_t resumable
;
1846 uint64_t last_object
, last_offset
;
1847 uint64_t bytes_read
; /* bytes read when current record created */
1851 list_t list
; /* List of struct receive_objnode. */
1853 * Last object looked up. Used to assert that objects are being looked
1854 * up in ascending order.
1856 uint64_t last_lookup
;
1859 struct receive_objnode
{
1864 struct receive_arg
{
1866 vnode_t
*vp
; /* The vnode to read the stream from */
1867 uint64_t voff
; /* The current offset in the stream */
1868 uint64_t bytes_read
;
1870 * A record that has had its payload read in, but hasn't yet been handed
1871 * off to the worker thread.
1873 struct receive_record_arg
*rrd
;
1874 /* A record that has had its header read in, but not its payload. */
1875 struct receive_record_arg
*next_rrd
;
1877 zio_cksum_t prev_cksum
;
1880 /* Sorted list of objects not to issue prefetches for. */
1881 struct objlist ignore_objlist
;
1884 typedef struct guid_map_entry
{
1886 dsl_dataset_t
*gme_ds
;
1891 guid_compare(const void *arg1
, const void *arg2
)
1893 const guid_map_entry_t
*gmep1
= (const guid_map_entry_t
*)arg1
;
1894 const guid_map_entry_t
*gmep2
= (const guid_map_entry_t
*)arg2
;
1896 return (AVL_CMP(gmep1
->guid
, gmep2
->guid
));
1900 free_guid_map_onexit(void *arg
)
1902 avl_tree_t
*ca
= arg
;
1903 void *cookie
= NULL
;
1904 guid_map_entry_t
*gmep
;
1906 while ((gmep
= avl_destroy_nodes(ca
, &cookie
)) != NULL
) {
1907 dsl_dataset_long_rele(gmep
->gme_ds
, gmep
);
1908 dsl_dataset_rele(gmep
->gme_ds
, gmep
);
1909 kmem_free(gmep
, sizeof (guid_map_entry_t
));
1912 kmem_free(ca
, sizeof (avl_tree_t
));
1916 receive_read(struct receive_arg
*ra
, int len
, void *buf
)
1921 * The code doesn't rely on this (lengths being multiples of 8). See
1922 * comment in dump_bytes.
1926 while (done
< len
) {
1929 ra
->err
= vn_rdwr(UIO_READ
, ra
->vp
,
1930 (char *)buf
+ done
, len
- done
,
1931 ra
->voff
, UIO_SYSSPACE
, FAPPEND
,
1932 RLIM64_INFINITY
, CRED(), &resid
);
1934 if (resid
== len
- done
) {
1936 * Note: ECKSUM indicates that the receive
1937 * was interrupted and can potentially be resumed.
1939 ra
->err
= SET_ERROR(ECKSUM
);
1941 ra
->voff
+= len
- done
- resid
;
1947 ra
->bytes_read
+= len
;
1949 ASSERT3U(done
, ==, len
);
1953 noinline
static void
1954 byteswap_record(dmu_replay_record_t
*drr
)
1956 #define DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
1957 #define DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
1958 drr
->drr_type
= BSWAP_32(drr
->drr_type
);
1959 drr
->drr_payloadlen
= BSWAP_32(drr
->drr_payloadlen
);
1961 switch (drr
->drr_type
) {
1963 DO64(drr_begin
.drr_magic
);
1964 DO64(drr_begin
.drr_versioninfo
);
1965 DO64(drr_begin
.drr_creation_time
);
1966 DO32(drr_begin
.drr_type
);
1967 DO32(drr_begin
.drr_flags
);
1968 DO64(drr_begin
.drr_toguid
);
1969 DO64(drr_begin
.drr_fromguid
);
1972 DO64(drr_object
.drr_object
);
1973 DO32(drr_object
.drr_type
);
1974 DO32(drr_object
.drr_bonustype
);
1975 DO32(drr_object
.drr_blksz
);
1976 DO32(drr_object
.drr_bonuslen
);
1977 DO64(drr_object
.drr_toguid
);
1979 case DRR_FREEOBJECTS
:
1980 DO64(drr_freeobjects
.drr_firstobj
);
1981 DO64(drr_freeobjects
.drr_numobjs
);
1982 DO64(drr_freeobjects
.drr_toguid
);
1985 DO64(drr_write
.drr_object
);
1986 DO32(drr_write
.drr_type
);
1987 DO64(drr_write
.drr_offset
);
1988 DO64(drr_write
.drr_logical_size
);
1989 DO64(drr_write
.drr_toguid
);
1990 ZIO_CHECKSUM_BSWAP(&drr
->drr_u
.drr_write
.drr_key
.ddk_cksum
);
1991 DO64(drr_write
.drr_key
.ddk_prop
);
1992 DO64(drr_write
.drr_compressed_size
);
1994 case DRR_WRITE_BYREF
:
1995 DO64(drr_write_byref
.drr_object
);
1996 DO64(drr_write_byref
.drr_offset
);
1997 DO64(drr_write_byref
.drr_length
);
1998 DO64(drr_write_byref
.drr_toguid
);
1999 DO64(drr_write_byref
.drr_refguid
);
2000 DO64(drr_write_byref
.drr_refobject
);
2001 DO64(drr_write_byref
.drr_refoffset
);
2002 ZIO_CHECKSUM_BSWAP(&drr
->drr_u
.drr_write_byref
.
2004 DO64(drr_write_byref
.drr_key
.ddk_prop
);
2006 case DRR_WRITE_EMBEDDED
:
2007 DO64(drr_write_embedded
.drr_object
);
2008 DO64(drr_write_embedded
.drr_offset
);
2009 DO64(drr_write_embedded
.drr_length
);
2010 DO64(drr_write_embedded
.drr_toguid
);
2011 DO32(drr_write_embedded
.drr_lsize
);
2012 DO32(drr_write_embedded
.drr_psize
);
2015 DO64(drr_free
.drr_object
);
2016 DO64(drr_free
.drr_offset
);
2017 DO64(drr_free
.drr_length
);
2018 DO64(drr_free
.drr_toguid
);
2021 DO64(drr_spill
.drr_object
);
2022 DO64(drr_spill
.drr_length
);
2023 DO64(drr_spill
.drr_toguid
);
2026 DO64(drr_end
.drr_toguid
);
2027 ZIO_CHECKSUM_BSWAP(&drr
->drr_u
.drr_end
.drr_checksum
);
2033 if (drr
->drr_type
!= DRR_BEGIN
) {
2034 ZIO_CHECKSUM_BSWAP(&drr
->drr_u
.drr_checksum
.drr_checksum
);
2041 static inline uint8_t
2042 deduce_nblkptr(dmu_object_type_t bonus_type
, uint64_t bonus_size
)
2044 if (bonus_type
== DMU_OT_SA
) {
2048 ((DN_OLD_MAX_BONUSLEN
-
2049 MIN(DN_OLD_MAX_BONUSLEN
, bonus_size
)) >> SPA_BLKPTRSHIFT
));
2054 save_resume_state(struct receive_writer_arg
*rwa
,
2055 uint64_t object
, uint64_t offset
, dmu_tx_t
*tx
)
2057 int txgoff
= dmu_tx_get_txg(tx
) & TXG_MASK
;
2059 if (!rwa
->resumable
)
2063 * We use ds_resume_bytes[] != 0 to indicate that we need to
2064 * update this on disk, so it must not be 0.
2066 ASSERT(rwa
->bytes_read
!= 0);
2069 * We only resume from write records, which have a valid
2070 * (non-meta-dnode) object number.
2072 ASSERT(object
!= 0);
2075 * For resuming to work correctly, we must receive records in order,
2076 * sorted by object,offset. This is checked by the callers, but
2077 * assert it here for good measure.
2079 ASSERT3U(object
, >=, rwa
->os
->os_dsl_dataset
->ds_resume_object
[txgoff
]);
2080 ASSERT(object
!= rwa
->os
->os_dsl_dataset
->ds_resume_object
[txgoff
] ||
2081 offset
>= rwa
->os
->os_dsl_dataset
->ds_resume_offset
[txgoff
]);
2082 ASSERT3U(rwa
->bytes_read
, >=,
2083 rwa
->os
->os_dsl_dataset
->ds_resume_bytes
[txgoff
]);
2085 rwa
->os
->os_dsl_dataset
->ds_resume_object
[txgoff
] = object
;
2086 rwa
->os
->os_dsl_dataset
->ds_resume_offset
[txgoff
] = offset
;
2087 rwa
->os
->os_dsl_dataset
->ds_resume_bytes
[txgoff
] = rwa
->bytes_read
;
2091 receive_object(struct receive_writer_arg
*rwa
, struct drr_object
*drro
,
2094 dmu_object_info_t doi
;
2099 if (drro
->drr_type
== DMU_OT_NONE
||
2100 !DMU_OT_IS_VALID(drro
->drr_type
) ||
2101 !DMU_OT_IS_VALID(drro
->drr_bonustype
) ||
2102 drro
->drr_checksumtype
>= ZIO_CHECKSUM_FUNCTIONS
||
2103 drro
->drr_compress
>= ZIO_COMPRESS_FUNCTIONS
||
2104 P2PHASE(drro
->drr_blksz
, SPA_MINBLOCKSIZE
) ||
2105 drro
->drr_blksz
< SPA_MINBLOCKSIZE
||
2106 drro
->drr_blksz
> spa_maxblocksize(dmu_objset_spa(rwa
->os
)) ||
2107 drro
->drr_bonuslen
>
2108 DN_BONUS_SIZE(spa_maxdnodesize(dmu_objset_spa(rwa
->os
)))) {
2109 return (SET_ERROR(EINVAL
));
2112 err
= dmu_object_info(rwa
->os
, drro
->drr_object
, &doi
);
2114 if (err
!= 0 && err
!= ENOENT
)
2115 return (SET_ERROR(EINVAL
));
2116 object
= err
== 0 ? drro
->drr_object
: DMU_NEW_OBJECT
;
2119 * If we are losing blkptrs or changing the block size this must
2120 * be a new file instance. We must clear out the previous file
2121 * contents before we can change this type of metadata in the dnode.
2126 nblkptr
= deduce_nblkptr(drro
->drr_bonustype
,
2127 drro
->drr_bonuslen
);
2129 if (drro
->drr_blksz
!= doi
.doi_data_block_size
||
2130 nblkptr
< doi
.doi_nblkptr
) {
2131 err
= dmu_free_long_range(rwa
->os
, drro
->drr_object
,
2134 return (SET_ERROR(EINVAL
));
2138 tx
= dmu_tx_create(rwa
->os
);
2139 dmu_tx_hold_bonus(tx
, object
);
2140 err
= dmu_tx_assign(tx
, TXG_WAIT
);
2146 if (object
== DMU_NEW_OBJECT
) {
2147 /* currently free, want to be allocated */
2148 err
= dmu_object_claim_dnsize(rwa
->os
, drro
->drr_object
,
2149 drro
->drr_type
, drro
->drr_blksz
,
2150 drro
->drr_bonustype
, drro
->drr_bonuslen
,
2151 drro
->drr_dn_slots
<< DNODE_SHIFT
, tx
);
2152 } else if (drro
->drr_type
!= doi
.doi_type
||
2153 drro
->drr_blksz
!= doi
.doi_data_block_size
||
2154 drro
->drr_bonustype
!= doi
.doi_bonus_type
||
2155 drro
->drr_bonuslen
!= doi
.doi_bonus_size
) {
2156 /* currently allocated, but with different properties */
2157 err
= dmu_object_reclaim(rwa
->os
, drro
->drr_object
,
2158 drro
->drr_type
, drro
->drr_blksz
,
2159 drro
->drr_bonustype
, drro
->drr_bonuslen
, tx
);
2163 return (SET_ERROR(EINVAL
));
2166 dmu_object_set_checksum(rwa
->os
, drro
->drr_object
,
2167 drro
->drr_checksumtype
, tx
);
2168 dmu_object_set_compress(rwa
->os
, drro
->drr_object
,
2169 drro
->drr_compress
, tx
);
2174 VERIFY0(dmu_bonus_hold(rwa
->os
, drro
->drr_object
, FTAG
, &db
));
2175 dmu_buf_will_dirty(db
, tx
);
2177 ASSERT3U(db
->db_size
, >=, drro
->drr_bonuslen
);
2178 bcopy(data
, db
->db_data
, drro
->drr_bonuslen
);
2179 if (rwa
->byteswap
) {
2180 dmu_object_byteswap_t byteswap
=
2181 DMU_OT_BYTESWAP(drro
->drr_bonustype
);
2182 dmu_ot_byteswap
[byteswap
].ob_func(db
->db_data
,
2183 drro
->drr_bonuslen
);
2185 dmu_buf_rele(db
, FTAG
);
2194 receive_freeobjects(struct receive_writer_arg
*rwa
,
2195 struct drr_freeobjects
*drrfo
)
2200 if (drrfo
->drr_firstobj
+ drrfo
->drr_numobjs
< drrfo
->drr_firstobj
)
2201 return (SET_ERROR(EINVAL
));
2203 for (obj
= drrfo
->drr_firstobj
== 0 ? 1 : drrfo
->drr_firstobj
;
2204 obj
< drrfo
->drr_firstobj
+ drrfo
->drr_numobjs
&& next_err
== 0;
2205 next_err
= dmu_object_next(rwa
->os
, &obj
, FALSE
, 0)) {
2206 dmu_object_info_t doi
;
2209 err
= dmu_object_info(rwa
->os
, obj
, &doi
);
2210 if (err
== ENOENT
) {
2213 } else if (err
!= 0) {
2217 err
= dmu_free_long_object(rwa
->os
, obj
);
2221 if (next_err
!= ESRCH
)
2227 receive_write(struct receive_writer_arg
*rwa
, struct drr_write
*drrw
,
2234 if (drrw
->drr_offset
+ drrw
->drr_logical_size
< drrw
->drr_offset
||
2235 !DMU_OT_IS_VALID(drrw
->drr_type
))
2236 return (SET_ERROR(EINVAL
));
2239 * For resuming to work, records must be in increasing order
2240 * by (object, offset).
2242 if (drrw
->drr_object
< rwa
->last_object
||
2243 (drrw
->drr_object
== rwa
->last_object
&&
2244 drrw
->drr_offset
< rwa
->last_offset
)) {
2245 return (SET_ERROR(EINVAL
));
2247 rwa
->last_object
= drrw
->drr_object
;
2248 rwa
->last_offset
= drrw
->drr_offset
;
2250 if (dmu_object_info(rwa
->os
, drrw
->drr_object
, NULL
) != 0)
2251 return (SET_ERROR(EINVAL
));
2253 tx
= dmu_tx_create(rwa
->os
);
2255 dmu_tx_hold_write(tx
, drrw
->drr_object
,
2256 drrw
->drr_offset
, drrw
->drr_logical_size
);
2257 err
= dmu_tx_assign(tx
, TXG_WAIT
);
2262 if (rwa
->byteswap
) {
2263 dmu_object_byteswap_t byteswap
=
2264 DMU_OT_BYTESWAP(drrw
->drr_type
);
2265 dmu_ot_byteswap
[byteswap
].ob_func(abuf
->b_data
,
2266 DRR_WRITE_PAYLOAD_SIZE(drrw
));
2269 /* use the bonus buf to look up the dnode in dmu_assign_arcbuf */
2270 if (dmu_bonus_hold(rwa
->os
, drrw
->drr_object
, FTAG
, &bonus
) != 0)
2271 return (SET_ERROR(EINVAL
));
2272 dmu_assign_arcbuf(bonus
, drrw
->drr_offset
, abuf
, tx
);
2275 * Note: If the receive fails, we want the resume stream to start
2276 * with the same record that we last successfully received (as opposed
2277 * to the next record), so that we can verify that we are
2278 * resuming from the correct location.
2280 save_resume_state(rwa
, drrw
->drr_object
, drrw
->drr_offset
, tx
);
2282 dmu_buf_rele(bonus
, FTAG
);
2288 * Handle a DRR_WRITE_BYREF record. This record is used in dedup'ed
2289 * streams to refer to a copy of the data that is already on the
2290 * system because it came in earlier in the stream. This function
2291 * finds the earlier copy of the data, and uses that copy instead of
2292 * data from the stream to fulfill this write.
2295 receive_write_byref(struct receive_writer_arg
*rwa
,
2296 struct drr_write_byref
*drrwbr
)
2300 guid_map_entry_t gmesrch
;
2301 guid_map_entry_t
*gmep
;
2303 objset_t
*ref_os
= NULL
;
2306 if (drrwbr
->drr_offset
+ drrwbr
->drr_length
< drrwbr
->drr_offset
)
2307 return (SET_ERROR(EINVAL
));
2310 * If the GUID of the referenced dataset is different from the
2311 * GUID of the target dataset, find the referenced dataset.
2313 if (drrwbr
->drr_toguid
!= drrwbr
->drr_refguid
) {
2314 gmesrch
.guid
= drrwbr
->drr_refguid
;
2315 if ((gmep
= avl_find(rwa
->guid_to_ds_map
, &gmesrch
,
2317 return (SET_ERROR(EINVAL
));
2319 if (dmu_objset_from_ds(gmep
->gme_ds
, &ref_os
))
2320 return (SET_ERROR(EINVAL
));
2325 err
= dmu_buf_hold(ref_os
, drrwbr
->drr_refobject
,
2326 drrwbr
->drr_refoffset
, FTAG
, &dbp
, DMU_READ_PREFETCH
);
2330 tx
= dmu_tx_create(rwa
->os
);
2332 dmu_tx_hold_write(tx
, drrwbr
->drr_object
,
2333 drrwbr
->drr_offset
, drrwbr
->drr_length
);
2334 err
= dmu_tx_assign(tx
, TXG_WAIT
);
2339 dmu_write(rwa
->os
, drrwbr
->drr_object
,
2340 drrwbr
->drr_offset
, drrwbr
->drr_length
, dbp
->db_data
, tx
);
2341 dmu_buf_rele(dbp
, FTAG
);
2343 /* See comment in restore_write. */
2344 save_resume_state(rwa
, drrwbr
->drr_object
, drrwbr
->drr_offset
, tx
);
2350 receive_write_embedded(struct receive_writer_arg
*rwa
,
2351 struct drr_write_embedded
*drrwe
, void *data
)
2356 if (drrwe
->drr_offset
+ drrwe
->drr_length
< drrwe
->drr_offset
)
2359 if (drrwe
->drr_psize
> BPE_PAYLOAD_SIZE
)
2362 if (drrwe
->drr_etype
>= NUM_BP_EMBEDDED_TYPES
)
2364 if (drrwe
->drr_compression
>= ZIO_COMPRESS_FUNCTIONS
)
2367 tx
= dmu_tx_create(rwa
->os
);
2369 dmu_tx_hold_write(tx
, drrwe
->drr_object
,
2370 drrwe
->drr_offset
, drrwe
->drr_length
);
2371 err
= dmu_tx_assign(tx
, TXG_WAIT
);
2377 dmu_write_embedded(rwa
->os
, drrwe
->drr_object
,
2378 drrwe
->drr_offset
, data
, drrwe
->drr_etype
,
2379 drrwe
->drr_compression
, drrwe
->drr_lsize
, drrwe
->drr_psize
,
2380 rwa
->byteswap
^ ZFS_HOST_BYTEORDER
, tx
);
2382 /* See comment in restore_write. */
2383 save_resume_state(rwa
, drrwe
->drr_object
, drrwe
->drr_offset
, tx
);
2389 receive_spill(struct receive_writer_arg
*rwa
, struct drr_spill
*drrs
,
2393 dmu_buf_t
*db
, *db_spill
;
2396 if (drrs
->drr_length
< SPA_MINBLOCKSIZE
||
2397 drrs
->drr_length
> spa_maxblocksize(dmu_objset_spa(rwa
->os
)))
2398 return (SET_ERROR(EINVAL
));
2400 if (dmu_object_info(rwa
->os
, drrs
->drr_object
, NULL
) != 0)
2401 return (SET_ERROR(EINVAL
));
2403 VERIFY0(dmu_bonus_hold(rwa
->os
, drrs
->drr_object
, FTAG
, &db
));
2404 if ((err
= dmu_spill_hold_by_bonus(db
, FTAG
, &db_spill
)) != 0) {
2405 dmu_buf_rele(db
, FTAG
);
2409 tx
= dmu_tx_create(rwa
->os
);
2411 dmu_tx_hold_spill(tx
, db
->db_object
);
2413 err
= dmu_tx_assign(tx
, TXG_WAIT
);
2415 dmu_buf_rele(db
, FTAG
);
2416 dmu_buf_rele(db_spill
, FTAG
);
2420 dmu_buf_will_dirty(db_spill
, tx
);
2422 if (db_spill
->db_size
< drrs
->drr_length
)
2423 VERIFY(0 == dbuf_spill_set_blksz(db_spill
,
2424 drrs
->drr_length
, tx
));
2425 bcopy(data
, db_spill
->db_data
, drrs
->drr_length
);
2427 dmu_buf_rele(db
, FTAG
);
2428 dmu_buf_rele(db_spill
, FTAG
);
2436 receive_free(struct receive_writer_arg
*rwa
, struct drr_free
*drrf
)
2440 if (drrf
->drr_length
!= -1ULL &&
2441 drrf
->drr_offset
+ drrf
->drr_length
< drrf
->drr_offset
)
2442 return (SET_ERROR(EINVAL
));
2444 if (dmu_object_info(rwa
->os
, drrf
->drr_object
, NULL
) != 0)
2445 return (SET_ERROR(EINVAL
));
2447 err
= dmu_free_long_range(rwa
->os
, drrf
->drr_object
,
2448 drrf
->drr_offset
, drrf
->drr_length
);
2453 /* used to destroy the drc_ds on error */
2455 dmu_recv_cleanup_ds(dmu_recv_cookie_t
*drc
)
2457 if (drc
->drc_resumable
) {
2458 /* wait for our resume state to be written to disk */
2459 txg_wait_synced(drc
->drc_ds
->ds_dir
->dd_pool
, 0);
2460 dsl_dataset_disown(drc
->drc_ds
, dmu_recv_tag
);
2462 char name
[ZFS_MAX_DATASET_NAME_LEN
];
2463 dsl_dataset_name(drc
->drc_ds
, name
);
2464 dsl_dataset_disown(drc
->drc_ds
, dmu_recv_tag
);
2465 (void) dsl_destroy_head(name
);
2470 receive_cksum(struct receive_arg
*ra
, int len
, void *buf
)
2473 (void) fletcher_4_incremental_byteswap(buf
, len
, &ra
->cksum
);
2475 (void) fletcher_4_incremental_native(buf
, len
, &ra
->cksum
);
2480 * Read the payload into a buffer of size len, and update the current record's
2482 * Allocate ra->next_rrd and read the next record's header into
2483 * ra->next_rrd->header.
2484 * Verify checksum of payload and next record.
2487 receive_read_payload_and_next_header(struct receive_arg
*ra
, int len
, void *buf
)
2490 zio_cksum_t cksum_orig
;
2491 zio_cksum_t
*cksump
;
2494 ASSERT3U(len
, <=, SPA_MAXBLOCKSIZE
);
2495 err
= receive_read(ra
, len
, buf
);
2498 receive_cksum(ra
, len
, buf
);
2500 /* note: rrd is NULL when reading the begin record's payload */
2501 if (ra
->rrd
!= NULL
) {
2502 ra
->rrd
->payload
= buf
;
2503 ra
->rrd
->payload_size
= len
;
2504 ra
->rrd
->bytes_read
= ra
->bytes_read
;
2508 ra
->prev_cksum
= ra
->cksum
;
2510 ra
->next_rrd
= kmem_zalloc(sizeof (*ra
->next_rrd
), KM_SLEEP
);
2511 err
= receive_read(ra
, sizeof (ra
->next_rrd
->header
),
2512 &ra
->next_rrd
->header
);
2513 ra
->next_rrd
->bytes_read
= ra
->bytes_read
;
2515 kmem_free(ra
->next_rrd
, sizeof (*ra
->next_rrd
));
2516 ra
->next_rrd
= NULL
;
2519 if (ra
->next_rrd
->header
.drr_type
== DRR_BEGIN
) {
2520 kmem_free(ra
->next_rrd
, sizeof (*ra
->next_rrd
));
2521 ra
->next_rrd
= NULL
;
2522 return (SET_ERROR(EINVAL
));
2526 * Note: checksum is of everything up to but not including the
2529 ASSERT3U(offsetof(dmu_replay_record_t
, drr_u
.drr_checksum
.drr_checksum
),
2530 ==, sizeof (dmu_replay_record_t
) - sizeof (zio_cksum_t
));
2532 offsetof(dmu_replay_record_t
, drr_u
.drr_checksum
.drr_checksum
),
2533 &ra
->next_rrd
->header
);
2535 cksum_orig
= ra
->next_rrd
->header
.drr_u
.drr_checksum
.drr_checksum
;
2536 cksump
= &ra
->next_rrd
->header
.drr_u
.drr_checksum
.drr_checksum
;
2539 byteswap_record(&ra
->next_rrd
->header
);
2541 if ((!ZIO_CHECKSUM_IS_ZERO(cksump
)) &&
2542 !ZIO_CHECKSUM_EQUAL(ra
->cksum
, *cksump
)) {
2543 kmem_free(ra
->next_rrd
, sizeof (*ra
->next_rrd
));
2544 ra
->next_rrd
= NULL
;
2545 return (SET_ERROR(ECKSUM
));
2548 receive_cksum(ra
, sizeof (cksum_orig
), &cksum_orig
);
2554 objlist_create(struct objlist
*list
)
2556 list_create(&list
->list
, sizeof (struct receive_objnode
),
2557 offsetof(struct receive_objnode
, node
));
2558 list
->last_lookup
= 0;
2562 objlist_destroy(struct objlist
*list
)
2564 struct receive_objnode
*n
;
2566 for (n
= list_remove_head(&list
->list
);
2567 n
!= NULL
; n
= list_remove_head(&list
->list
)) {
2568 kmem_free(n
, sizeof (*n
));
2570 list_destroy(&list
->list
);
2574 * This function looks through the objlist to see if the specified object number
2575 * is contained in the objlist. In the process, it will remove all object
2576 * numbers in the list that are smaller than the specified object number. Thus,
2577 * any lookup of an object number smaller than a previously looked up object
2578 * number will always return false; therefore, all lookups should be done in
2582 objlist_exists(struct objlist
*list
, uint64_t object
)
2584 struct receive_objnode
*node
= list_head(&list
->list
);
2585 ASSERT3U(object
, >=, list
->last_lookup
);
2586 list
->last_lookup
= object
;
2587 while (node
!= NULL
&& node
->object
< object
) {
2588 VERIFY3P(node
, ==, list_remove_head(&list
->list
));
2589 kmem_free(node
, sizeof (*node
));
2590 node
= list_head(&list
->list
);
2592 return (node
!= NULL
&& node
->object
== object
);
2596 * The objlist is a list of object numbers stored in ascending order. However,
2597 * the insertion of new object numbers does not seek out the correct location to
2598 * store a new object number; instead, it appends it to the list for simplicity.
2599 * Thus, any users must take care to only insert new object numbers in ascending
2603 objlist_insert(struct objlist
*list
, uint64_t object
)
2605 struct receive_objnode
*node
= kmem_zalloc(sizeof (*node
), KM_SLEEP
);
2606 node
->object
= object
;
2609 struct receive_objnode
*last_object
= list_tail(&list
->list
);
2610 uint64_t last_objnum
= (last_object
!= NULL
? last_object
->object
: 0);
2611 ASSERT3U(node
->object
, >, last_objnum
);
2614 list_insert_tail(&list
->list
, node
);
2618 * Issue the prefetch reads for any necessary indirect blocks.
2620 * We use the object ignore list to tell us whether or not to issue prefetches
2621 * for a given object. We do this for both correctness (in case the blocksize
2622 * of an object has changed) and performance (if the object doesn't exist, don't
2623 * needlessly try to issue prefetches). We also trim the list as we go through
2624 * the stream to prevent it from growing to an unbounded size.
2626 * The object numbers within will always be in sorted order, and any write
2627 * records we see will also be in sorted order, but they're not sorted with
2628 * respect to each other (i.e. we can get several object records before
2629 * receiving each object's write records). As a result, once we've reached a
2630 * given object number, we can safely remove any reference to lower object
2631 * numbers in the ignore list. In practice, we receive up to 32 object records
2632 * before receiving write records, so the list can have up to 32 nodes in it.
2636 receive_read_prefetch(struct receive_arg
*ra
,
2637 uint64_t object
, uint64_t offset
, uint64_t length
)
2639 if (!objlist_exists(&ra
->ignore_objlist
, object
)) {
2640 dmu_prefetch(ra
->os
, object
, 1, offset
, length
,
2641 ZIO_PRIORITY_SYNC_READ
);
2646 * Read records off the stream, issuing any necessary prefetches.
2649 receive_read_record(struct receive_arg
*ra
)
2653 switch (ra
->rrd
->header
.drr_type
) {
2656 struct drr_object
*drro
= &ra
->rrd
->header
.drr_u
.drr_object
;
2657 uint32_t size
= P2ROUNDUP(drro
->drr_bonuslen
, 8);
2658 void *buf
= kmem_zalloc(size
, KM_SLEEP
);
2659 dmu_object_info_t doi
;
2660 err
= receive_read_payload_and_next_header(ra
, size
, buf
);
2662 kmem_free(buf
, size
);
2665 err
= dmu_object_info(ra
->os
, drro
->drr_object
, &doi
);
2667 * See receive_read_prefetch for an explanation why we're
2668 * storing this object in the ignore_obj_list.
2670 if (err
== ENOENT
||
2671 (err
== 0 && doi
.doi_data_block_size
!= drro
->drr_blksz
)) {
2672 objlist_insert(&ra
->ignore_objlist
, drro
->drr_object
);
2677 case DRR_FREEOBJECTS
:
2679 err
= receive_read_payload_and_next_header(ra
, 0, NULL
);
2684 struct drr_write
*drrw
= &ra
->rrd
->header
.drr_u
.drr_write
;
2686 boolean_t is_meta
= DMU_OT_IS_METADATA(drrw
->drr_type
);
2687 if (DRR_WRITE_COMPRESSED(drrw
)) {
2688 ASSERT3U(drrw
->drr_compressed_size
, >, 0);
2689 ASSERT3U(drrw
->drr_logical_size
, >=,
2690 drrw
->drr_compressed_size
);
2692 abuf
= arc_loan_compressed_buf(
2693 dmu_objset_spa(ra
->os
),
2694 drrw
->drr_compressed_size
, drrw
->drr_logical_size
,
2695 drrw
->drr_compressiontype
);
2697 abuf
= arc_loan_buf(dmu_objset_spa(ra
->os
),
2698 is_meta
, drrw
->drr_logical_size
);
2701 err
= receive_read_payload_and_next_header(ra
,
2702 DRR_WRITE_PAYLOAD_SIZE(drrw
), abuf
->b_data
);
2704 dmu_return_arcbuf(abuf
);
2707 ra
->rrd
->write_buf
= abuf
;
2708 receive_read_prefetch(ra
, drrw
->drr_object
, drrw
->drr_offset
,
2709 drrw
->drr_logical_size
);
2712 case DRR_WRITE_BYREF
:
2714 struct drr_write_byref
*drrwb
=
2715 &ra
->rrd
->header
.drr_u
.drr_write_byref
;
2716 err
= receive_read_payload_and_next_header(ra
, 0, NULL
);
2717 receive_read_prefetch(ra
, drrwb
->drr_object
, drrwb
->drr_offset
,
2721 case DRR_WRITE_EMBEDDED
:
2723 struct drr_write_embedded
*drrwe
=
2724 &ra
->rrd
->header
.drr_u
.drr_write_embedded
;
2725 uint32_t size
= P2ROUNDUP(drrwe
->drr_psize
, 8);
2726 void *buf
= kmem_zalloc(size
, KM_SLEEP
);
2728 err
= receive_read_payload_and_next_header(ra
, size
, buf
);
2730 kmem_free(buf
, size
);
2734 receive_read_prefetch(ra
, drrwe
->drr_object
, drrwe
->drr_offset
,
2741 * It might be beneficial to prefetch indirect blocks here, but
2742 * we don't really have the data to decide for sure.
2744 err
= receive_read_payload_and_next_header(ra
, 0, NULL
);
2749 struct drr_end
*drre
= &ra
->rrd
->header
.drr_u
.drr_end
;
2750 if (!ZIO_CHECKSUM_EQUAL(ra
->prev_cksum
, drre
->drr_checksum
))
2751 return (SET_ERROR(ECKSUM
));
2756 struct drr_spill
*drrs
= &ra
->rrd
->header
.drr_u
.drr_spill
;
2757 void *buf
= kmem_zalloc(drrs
->drr_length
, KM_SLEEP
);
2758 err
= receive_read_payload_and_next_header(ra
, drrs
->drr_length
,
2761 kmem_free(buf
, drrs
->drr_length
);
2765 return (SET_ERROR(EINVAL
));
2770 * Commit the records to the pool.
2773 receive_process_record(struct receive_writer_arg
*rwa
,
2774 struct receive_record_arg
*rrd
)
2778 /* Processing in order, therefore bytes_read should be increasing. */
2779 ASSERT3U(rrd
->bytes_read
, >=, rwa
->bytes_read
);
2780 rwa
->bytes_read
= rrd
->bytes_read
;
2782 switch (rrd
->header
.drr_type
) {
2785 struct drr_object
*drro
= &rrd
->header
.drr_u
.drr_object
;
2786 err
= receive_object(rwa
, drro
, rrd
->payload
);
2787 kmem_free(rrd
->payload
, rrd
->payload_size
);
2788 rrd
->payload
= NULL
;
2791 case DRR_FREEOBJECTS
:
2793 struct drr_freeobjects
*drrfo
=
2794 &rrd
->header
.drr_u
.drr_freeobjects
;
2795 return (receive_freeobjects(rwa
, drrfo
));
2799 struct drr_write
*drrw
= &rrd
->header
.drr_u
.drr_write
;
2800 err
= receive_write(rwa
, drrw
, rrd
->write_buf
);
2801 /* if receive_write() is successful, it consumes the arc_buf */
2803 dmu_return_arcbuf(rrd
->write_buf
);
2804 rrd
->write_buf
= NULL
;
2805 rrd
->payload
= NULL
;
2808 case DRR_WRITE_BYREF
:
2810 struct drr_write_byref
*drrwbr
=
2811 &rrd
->header
.drr_u
.drr_write_byref
;
2812 return (receive_write_byref(rwa
, drrwbr
));
2814 case DRR_WRITE_EMBEDDED
:
2816 struct drr_write_embedded
*drrwe
=
2817 &rrd
->header
.drr_u
.drr_write_embedded
;
2818 err
= receive_write_embedded(rwa
, drrwe
, rrd
->payload
);
2819 kmem_free(rrd
->payload
, rrd
->payload_size
);
2820 rrd
->payload
= NULL
;
2825 struct drr_free
*drrf
= &rrd
->header
.drr_u
.drr_free
;
2826 return (receive_free(rwa
, drrf
));
2830 struct drr_spill
*drrs
= &rrd
->header
.drr_u
.drr_spill
;
2831 err
= receive_spill(rwa
, drrs
, rrd
->payload
);
2832 kmem_free(rrd
->payload
, rrd
->payload_size
);
2833 rrd
->payload
= NULL
;
2837 return (SET_ERROR(EINVAL
));
2842 * dmu_recv_stream's worker thread; pull records off the queue, and then call
2843 * receive_process_record When we're done, signal the main thread and exit.
2846 receive_writer_thread(void *arg
)
2848 struct receive_writer_arg
*rwa
= arg
;
2849 struct receive_record_arg
*rrd
;
2850 fstrans_cookie_t cookie
= spl_fstrans_mark();
2852 for (rrd
= bqueue_dequeue(&rwa
->q
); !rrd
->eos_marker
;
2853 rrd
= bqueue_dequeue(&rwa
->q
)) {
2855 * If there's an error, the main thread will stop putting things
2856 * on the queue, but we need to clear everything in it before we
2859 if (rwa
->err
== 0) {
2860 rwa
->err
= receive_process_record(rwa
, rrd
);
2861 } else if (rrd
->write_buf
!= NULL
) {
2862 dmu_return_arcbuf(rrd
->write_buf
);
2863 rrd
->write_buf
= NULL
;
2864 rrd
->payload
= NULL
;
2865 } else if (rrd
->payload
!= NULL
) {
2866 kmem_free(rrd
->payload
, rrd
->payload_size
);
2867 rrd
->payload
= NULL
;
2869 kmem_free(rrd
, sizeof (*rrd
));
2871 kmem_free(rrd
, sizeof (*rrd
));
2872 mutex_enter(&rwa
->mutex
);
2874 cv_signal(&rwa
->cv
);
2875 mutex_exit(&rwa
->mutex
);
2876 spl_fstrans_unmark(cookie
);
2880 resume_check(struct receive_arg
*ra
, nvlist_t
*begin_nvl
)
2883 objset_t
*mos
= dmu_objset_pool(ra
->os
)->dp_meta_objset
;
2884 uint64_t dsobj
= dmu_objset_id(ra
->os
);
2885 uint64_t resume_obj
, resume_off
;
2887 if (nvlist_lookup_uint64(begin_nvl
,
2888 "resume_object", &resume_obj
) != 0 ||
2889 nvlist_lookup_uint64(begin_nvl
,
2890 "resume_offset", &resume_off
) != 0) {
2891 return (SET_ERROR(EINVAL
));
2893 VERIFY0(zap_lookup(mos
, dsobj
,
2894 DS_FIELD_RESUME_OBJECT
, sizeof (val
), 1, &val
));
2895 if (resume_obj
!= val
)
2896 return (SET_ERROR(EINVAL
));
2897 VERIFY0(zap_lookup(mos
, dsobj
,
2898 DS_FIELD_RESUME_OFFSET
, sizeof (val
), 1, &val
));
2899 if (resume_off
!= val
)
2900 return (SET_ERROR(EINVAL
));
2906 * Read in the stream's records, one by one, and apply them to the pool. There
2907 * are two threads involved; the thread that calls this function will spin up a
2908 * worker thread, read the records off the stream one by one, and issue
2909 * prefetches for any necessary indirect blocks. It will then push the records
2910 * onto an internal blocking queue. The worker thread will pull the records off
2911 * the queue, and actually write the data into the DMU. This way, the worker
2912 * thread doesn't have to wait for reads to complete, since everything it needs
2913 * (the indirect blocks) will be prefetched.
2915 * NB: callers *must* call dmu_recv_end() if this succeeds.
2918 dmu_recv_stream(dmu_recv_cookie_t
*drc
, vnode_t
*vp
, offset_t
*voffp
,
2919 int cleanup_fd
, uint64_t *action_handlep
)
2922 struct receive_arg
*ra
;
2923 struct receive_writer_arg
*rwa
;
2925 uint32_t payloadlen
;
2927 nvlist_t
*begin_nvl
= NULL
;
2929 ra
= kmem_zalloc(sizeof (*ra
), KM_SLEEP
);
2930 rwa
= kmem_zalloc(sizeof (*rwa
), KM_SLEEP
);
2932 ra
->byteswap
= drc
->drc_byteswap
;
2933 ra
->cksum
= drc
->drc_cksum
;
2937 if (dsl_dataset_is_zapified(drc
->drc_ds
)) {
2938 (void) zap_lookup(drc
->drc_ds
->ds_dir
->dd_pool
->dp_meta_objset
,
2939 drc
->drc_ds
->ds_object
, DS_FIELD_RESUME_BYTES
,
2940 sizeof (ra
->bytes_read
), 1, &ra
->bytes_read
);
2943 objlist_create(&ra
->ignore_objlist
);
2945 /* these were verified in dmu_recv_begin */
2946 ASSERT3U(DMU_GET_STREAM_HDRTYPE(drc
->drc_drrb
->drr_versioninfo
), ==,
2948 ASSERT3U(drc
->drc_drrb
->drr_type
, <, DMU_OST_NUMTYPES
);
2951 * Open the objset we are modifying.
2953 VERIFY0(dmu_objset_from_ds(drc
->drc_ds
, &ra
->os
));
2955 ASSERT(dsl_dataset_phys(drc
->drc_ds
)->ds_flags
& DS_FLAG_INCONSISTENT
);
2957 featureflags
= DMU_GET_FEATUREFLAGS(drc
->drc_drrb
->drr_versioninfo
);
2959 /* if this stream is dedup'ed, set up the avl tree for guid mapping */
2960 if (featureflags
& DMU_BACKUP_FEATURE_DEDUP
) {
2963 if (cleanup_fd
== -1) {
2964 ra
->err
= SET_ERROR(EBADF
);
2967 ra
->err
= zfs_onexit_fd_hold(cleanup_fd
, &minor
);
2973 if (*action_handlep
== 0) {
2974 rwa
->guid_to_ds_map
=
2975 kmem_alloc(sizeof (avl_tree_t
), KM_SLEEP
);
2976 avl_create(rwa
->guid_to_ds_map
, guid_compare
,
2977 sizeof (guid_map_entry_t
),
2978 offsetof(guid_map_entry_t
, avlnode
));
2979 err
= zfs_onexit_add_cb(minor
,
2980 free_guid_map_onexit
, rwa
->guid_to_ds_map
,
2985 err
= zfs_onexit_cb_data(minor
, *action_handlep
,
2986 (void **)&rwa
->guid_to_ds_map
);
2991 drc
->drc_guid_to_ds_map
= rwa
->guid_to_ds_map
;
2994 payloadlen
= drc
->drc_drr_begin
->drr_payloadlen
;
2996 if (payloadlen
!= 0)
2997 payload
= kmem_alloc(payloadlen
, KM_SLEEP
);
2999 err
= receive_read_payload_and_next_header(ra
, payloadlen
, payload
);
3001 if (payloadlen
!= 0)
3002 kmem_free(payload
, payloadlen
);
3005 if (payloadlen
!= 0) {
3006 err
= nvlist_unpack(payload
, payloadlen
, &begin_nvl
, KM_SLEEP
);
3007 kmem_free(payload
, payloadlen
);
3012 if (featureflags
& DMU_BACKUP_FEATURE_RESUMING
) {
3013 err
= resume_check(ra
, begin_nvl
);
3018 (void) bqueue_init(&rwa
->q
, zfs_recv_queue_length
,
3019 offsetof(struct receive_record_arg
, node
));
3020 cv_init(&rwa
->cv
, NULL
, CV_DEFAULT
, NULL
);
3021 mutex_init(&rwa
->mutex
, NULL
, MUTEX_DEFAULT
, NULL
);
3023 rwa
->byteswap
= drc
->drc_byteswap
;
3024 rwa
->resumable
= drc
->drc_resumable
;
3026 (void) thread_create(NULL
, 0, receive_writer_thread
, rwa
, 0, curproc
,
3027 TS_RUN
, minclsyspri
);
3029 * We're reading rwa->err without locks, which is safe since we are the
3030 * only reader, and the worker thread is the only writer. It's ok if we
3031 * miss a write for an iteration or two of the loop, since the writer
3032 * thread will keep freeing records we send it until we send it an eos
3035 * We can leave this loop in 3 ways: First, if rwa->err is
3036 * non-zero. In that case, the writer thread will free the rrd we just
3037 * pushed. Second, if we're interrupted; in that case, either it's the
3038 * first loop and ra->rrd was never allocated, or it's later and ra->rrd
3039 * has been handed off to the writer thread who will free it. Finally,
3040 * if receive_read_record fails or we're at the end of the stream, then
3041 * we free ra->rrd and exit.
3043 while (rwa
->err
== 0) {
3044 if (issig(JUSTLOOKING
) && issig(FORREAL
)) {
3045 err
= SET_ERROR(EINTR
);
3049 ASSERT3P(ra
->rrd
, ==, NULL
);
3050 ra
->rrd
= ra
->next_rrd
;
3051 ra
->next_rrd
= NULL
;
3052 /* Allocates and loads header into ra->next_rrd */
3053 err
= receive_read_record(ra
);
3055 if (ra
->rrd
->header
.drr_type
== DRR_END
|| err
!= 0) {
3056 kmem_free(ra
->rrd
, sizeof (*ra
->rrd
));
3061 bqueue_enqueue(&rwa
->q
, ra
->rrd
,
3062 sizeof (struct receive_record_arg
) + ra
->rrd
->payload_size
);
3065 if (ra
->next_rrd
== NULL
)
3066 ra
->next_rrd
= kmem_zalloc(sizeof (*ra
->next_rrd
), KM_SLEEP
);
3067 ra
->next_rrd
->eos_marker
= B_TRUE
;
3068 bqueue_enqueue(&rwa
->q
, ra
->next_rrd
, 1);
3070 mutex_enter(&rwa
->mutex
);
3071 while (!rwa
->done
) {
3072 cv_wait(&rwa
->cv
, &rwa
->mutex
);
3074 mutex_exit(&rwa
->mutex
);
3076 cv_destroy(&rwa
->cv
);
3077 mutex_destroy(&rwa
->mutex
);
3078 bqueue_destroy(&rwa
->q
);
3083 nvlist_free(begin_nvl
);
3084 if ((featureflags
& DMU_BACKUP_FEATURE_DEDUP
) && (cleanup_fd
!= -1))
3085 zfs_onexit_fd_rele(cleanup_fd
);
3089 * Clean up references. If receive is not resumable,
3090 * destroy what we created, so we don't leave it in
3091 * the inconsistent state.
3093 dmu_recv_cleanup_ds(drc
);
3097 objlist_destroy(&ra
->ignore_objlist
);
3098 kmem_free(ra
, sizeof (*ra
));
3099 kmem_free(rwa
, sizeof (*rwa
));
3104 dmu_recv_end_check(void *arg
, dmu_tx_t
*tx
)
3106 dmu_recv_cookie_t
*drc
= arg
;
3107 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
3110 ASSERT3P(drc
->drc_ds
->ds_owner
, ==, dmu_recv_tag
);
3112 if (!drc
->drc_newfs
) {
3113 dsl_dataset_t
*origin_head
;
3115 error
= dsl_dataset_hold(dp
, drc
->drc_tofs
, FTAG
, &origin_head
);
3118 if (drc
->drc_force
) {
3120 * We will destroy any snapshots in tofs (i.e. before
3121 * origin_head) that are after the origin (which is
3122 * the snap before drc_ds, because drc_ds can not
3123 * have any snaps of its own).
3127 obj
= dsl_dataset_phys(origin_head
)->ds_prev_snap_obj
;
3129 dsl_dataset_phys(drc
->drc_ds
)->ds_prev_snap_obj
) {
3130 dsl_dataset_t
*snap
;
3131 error
= dsl_dataset_hold_obj(dp
, obj
, FTAG
,
3135 if (snap
->ds_dir
!= origin_head
->ds_dir
)
3136 error
= SET_ERROR(EINVAL
);
3138 error
= dsl_destroy_snapshot_check_impl(
3141 obj
= dsl_dataset_phys(snap
)->ds_prev_snap_obj
;
3142 dsl_dataset_rele(snap
, FTAG
);
3147 dsl_dataset_rele(origin_head
, FTAG
);
3151 error
= dsl_dataset_clone_swap_check_impl(drc
->drc_ds
,
3152 origin_head
, drc
->drc_force
, drc
->drc_owner
, tx
);
3154 dsl_dataset_rele(origin_head
, FTAG
);
3157 error
= dsl_dataset_snapshot_check_impl(origin_head
,
3158 drc
->drc_tosnap
, tx
, B_TRUE
, 1, drc
->drc_cred
);
3159 dsl_dataset_rele(origin_head
, FTAG
);
3163 error
= dsl_destroy_head_check_impl(drc
->drc_ds
, 1);
3165 error
= dsl_dataset_snapshot_check_impl(drc
->drc_ds
,
3166 drc
->drc_tosnap
, tx
, B_TRUE
, 1, drc
->drc_cred
);
3172 dmu_recv_end_sync(void *arg
, dmu_tx_t
*tx
)
3174 dmu_recv_cookie_t
*drc
= arg
;
3175 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
3177 spa_history_log_internal_ds(drc
->drc_ds
, "finish receiving",
3178 tx
, "snap=%s", drc
->drc_tosnap
);
3180 if (!drc
->drc_newfs
) {
3181 dsl_dataset_t
*origin_head
;
3183 VERIFY0(dsl_dataset_hold(dp
, drc
->drc_tofs
, FTAG
,
3186 if (drc
->drc_force
) {
3188 * Destroy any snapshots of drc_tofs (origin_head)
3189 * after the origin (the snap before drc_ds).
3193 obj
= dsl_dataset_phys(origin_head
)->ds_prev_snap_obj
;
3195 dsl_dataset_phys(drc
->drc_ds
)->ds_prev_snap_obj
) {
3196 dsl_dataset_t
*snap
;
3197 VERIFY0(dsl_dataset_hold_obj(dp
, obj
, FTAG
,
3199 ASSERT3P(snap
->ds_dir
, ==, origin_head
->ds_dir
);
3200 obj
= dsl_dataset_phys(snap
)->ds_prev_snap_obj
;
3201 dsl_destroy_snapshot_sync_impl(snap
,
3203 dsl_dataset_rele(snap
, FTAG
);
3206 VERIFY3P(drc
->drc_ds
->ds_prev
, ==,
3207 origin_head
->ds_prev
);
3209 dsl_dataset_clone_swap_sync_impl(drc
->drc_ds
,
3211 dsl_dataset_snapshot_sync_impl(origin_head
,
3212 drc
->drc_tosnap
, tx
);
3214 /* set snapshot's creation time and guid */
3215 dmu_buf_will_dirty(origin_head
->ds_prev
->ds_dbuf
, tx
);
3216 dsl_dataset_phys(origin_head
->ds_prev
)->ds_creation_time
=
3217 drc
->drc_drrb
->drr_creation_time
;
3218 dsl_dataset_phys(origin_head
->ds_prev
)->ds_guid
=
3219 drc
->drc_drrb
->drr_toguid
;
3220 dsl_dataset_phys(origin_head
->ds_prev
)->ds_flags
&=
3221 ~DS_FLAG_INCONSISTENT
;
3223 dmu_buf_will_dirty(origin_head
->ds_dbuf
, tx
);
3224 dsl_dataset_phys(origin_head
)->ds_flags
&=
3225 ~DS_FLAG_INCONSISTENT
;
3227 dsl_dataset_rele(origin_head
, FTAG
);
3228 dsl_destroy_head_sync_impl(drc
->drc_ds
, tx
);
3230 if (drc
->drc_owner
!= NULL
)
3231 VERIFY3P(origin_head
->ds_owner
, ==, drc
->drc_owner
);
3233 dsl_dataset_t
*ds
= drc
->drc_ds
;
3235 dsl_dataset_snapshot_sync_impl(ds
, drc
->drc_tosnap
, tx
);
3237 /* set snapshot's creation time and guid */
3238 dmu_buf_will_dirty(ds
->ds_prev
->ds_dbuf
, tx
);
3239 dsl_dataset_phys(ds
->ds_prev
)->ds_creation_time
=
3240 drc
->drc_drrb
->drr_creation_time
;
3241 dsl_dataset_phys(ds
->ds_prev
)->ds_guid
=
3242 drc
->drc_drrb
->drr_toguid
;
3243 dsl_dataset_phys(ds
->ds_prev
)->ds_flags
&=
3244 ~DS_FLAG_INCONSISTENT
;
3246 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
3247 dsl_dataset_phys(ds
)->ds_flags
&= ~DS_FLAG_INCONSISTENT
;
3248 if (dsl_dataset_has_resume_receive_state(ds
)) {
3249 (void) zap_remove(dp
->dp_meta_objset
, ds
->ds_object
,
3250 DS_FIELD_RESUME_FROMGUID
, tx
);
3251 (void) zap_remove(dp
->dp_meta_objset
, ds
->ds_object
,
3252 DS_FIELD_RESUME_OBJECT
, tx
);
3253 (void) zap_remove(dp
->dp_meta_objset
, ds
->ds_object
,
3254 DS_FIELD_RESUME_OFFSET
, tx
);
3255 (void) zap_remove(dp
->dp_meta_objset
, ds
->ds_object
,
3256 DS_FIELD_RESUME_BYTES
, tx
);
3257 (void) zap_remove(dp
->dp_meta_objset
, ds
->ds_object
,
3258 DS_FIELD_RESUME_TOGUID
, tx
);
3259 (void) zap_remove(dp
->dp_meta_objset
, ds
->ds_object
,
3260 DS_FIELD_RESUME_TONAME
, tx
);
3263 drc
->drc_newsnapobj
= dsl_dataset_phys(drc
->drc_ds
)->ds_prev_snap_obj
;
3264 zvol_create_minors(dp
->dp_spa
, drc
->drc_tofs
, B_TRUE
);
3266 * Release the hold from dmu_recv_begin. This must be done before
3267 * we return to open context, so that when we free the dataset's dnode,
3268 * we can evict its bonus buffer.
3270 dsl_dataset_disown(drc
->drc_ds
, dmu_recv_tag
);
3275 add_ds_to_guidmap(const char *name
, avl_tree_t
*guid_map
, uint64_t snapobj
)
3278 dsl_dataset_t
*snapds
;
3279 guid_map_entry_t
*gmep
;
3282 ASSERT(guid_map
!= NULL
);
3284 err
= dsl_pool_hold(name
, FTAG
, &dp
);
3287 gmep
= kmem_alloc(sizeof (*gmep
), KM_SLEEP
);
3288 err
= dsl_dataset_hold_obj(dp
, snapobj
, gmep
, &snapds
);
3290 gmep
->guid
= dsl_dataset_phys(snapds
)->ds_guid
;
3291 gmep
->gme_ds
= snapds
;
3292 avl_add(guid_map
, gmep
);
3293 dsl_dataset_long_hold(snapds
, gmep
);
3295 kmem_free(gmep
, sizeof (*gmep
));
3298 dsl_pool_rele(dp
, FTAG
);
3302 static int dmu_recv_end_modified_blocks
= 3;
3305 dmu_recv_existing_end(dmu_recv_cookie_t
*drc
)
3311 * We will be destroying the ds; make sure its origin is unmounted if
3314 char name
[ZFS_MAX_DATASET_NAME_LEN
];
3315 dsl_dataset_name(drc
->drc_ds
, name
);
3316 zfs_destroy_unmount_origin(name
);
3319 error
= dsl_sync_task(drc
->drc_tofs
,
3320 dmu_recv_end_check
, dmu_recv_end_sync
, drc
,
3321 dmu_recv_end_modified_blocks
, ZFS_SPACE_CHECK_NORMAL
);
3324 dmu_recv_cleanup_ds(drc
);
3329 dmu_recv_new_end(dmu_recv_cookie_t
*drc
)
3333 error
= dsl_sync_task(drc
->drc_tofs
,
3334 dmu_recv_end_check
, dmu_recv_end_sync
, drc
,
3335 dmu_recv_end_modified_blocks
, ZFS_SPACE_CHECK_NORMAL
);
3338 dmu_recv_cleanup_ds(drc
);
3339 } else if (drc
->drc_guid_to_ds_map
!= NULL
) {
3340 (void) add_ds_to_guidmap(drc
->drc_tofs
,
3341 drc
->drc_guid_to_ds_map
,
3342 drc
->drc_newsnapobj
);
3348 dmu_recv_end(dmu_recv_cookie_t
*drc
, void *owner
)
3350 drc
->drc_owner
= owner
;
3353 return (dmu_recv_new_end(drc
));
3355 return (dmu_recv_existing_end(drc
));
3359 * Return TRUE if this objset is currently being received into.
3362 dmu_objset_is_receiving(objset_t
*os
)
3364 return (os
->os_dsl_dataset
!= NULL
&&
3365 os
->os_dsl_dataset
->ds_owner
== dmu_recv_tag
);
3368 #if defined(_KERNEL)
3369 module_param(zfs_send_corrupt_data
, int, 0644);
3370 MODULE_PARM_DESC(zfs_send_corrupt_data
, "Allow sending corrupt data");