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 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 ASSERT(ZIO_CHECKSUM_IS_ZERO(&dsp
->dsa_drr
->drr_u
.
174 drr_checksum
.drr_checksum
));
175 dsp
->dsa_drr
->drr_u
.drr_checksum
.drr_checksum
= dsp
->dsa_zc
;
177 fletcher_4_incremental_native(&dsp
->dsa_drr
->
178 drr_u
.drr_checksum
.drr_checksum
,
179 sizeof (zio_cksum_t
), &dsp
->dsa_zc
);
180 if (dump_bytes(dsp
, dsp
->dsa_drr
, sizeof (dmu_replay_record_t
)) != 0)
181 return (SET_ERROR(EINTR
));
182 if (payload_len
!= 0) {
183 fletcher_4_incremental_native(payload
, payload_len
,
185 if (dump_bytes(dsp
, payload
, payload_len
) != 0)
186 return (SET_ERROR(EINTR
));
192 * Fill in the drr_free struct, or perform aggregation if the previous record is
193 * also a free record, and the two are adjacent.
195 * Note that we send free records even for a full send, because we want to be
196 * able to receive a full send as a clone, which requires a list of all the free
197 * and freeobject records that were generated on the source.
200 dump_free(dmu_sendarg_t
*dsp
, uint64_t object
, uint64_t offset
,
203 struct drr_free
*drrf
= &(dsp
->dsa_drr
->drr_u
.drr_free
);
206 * When we receive a free record, dbuf_free_range() assumes
207 * that the receiving system doesn't have any dbufs in the range
208 * being freed. This is always true because there is a one-record
209 * constraint: we only send one WRITE record for any given
210 * object,offset. We know that the one-record constraint is
211 * true because we always send data in increasing order by
214 * If the increasing-order constraint ever changes, we should find
215 * another way to assert that the one-record constraint is still
218 ASSERT(object
> dsp
->dsa_last_data_object
||
219 (object
== dsp
->dsa_last_data_object
&&
220 offset
> dsp
->dsa_last_data_offset
));
222 if (length
!= -1ULL && offset
+ length
< offset
)
226 * If there is a pending op, but it's not PENDING_FREE, push it out,
227 * since free block aggregation can only be done for blocks of the
228 * same type (i.e., DRR_FREE records can only be aggregated with
229 * other DRR_FREE records. DRR_FREEOBJECTS records can only be
230 * aggregated with other DRR_FREEOBJECTS records.
232 if (dsp
->dsa_pending_op
!= PENDING_NONE
&&
233 dsp
->dsa_pending_op
!= PENDING_FREE
) {
234 if (dump_record(dsp
, NULL
, 0) != 0)
235 return (SET_ERROR(EINTR
));
236 dsp
->dsa_pending_op
= PENDING_NONE
;
239 if (dsp
->dsa_pending_op
== PENDING_FREE
) {
241 * There should never be a PENDING_FREE if length is -1
242 * (because dump_dnode is the only place where this
243 * function is called with a -1, and only after flushing
244 * any pending record).
246 ASSERT(length
!= -1ULL);
248 * Check to see whether this free block can be aggregated
251 if (drrf
->drr_object
== object
&& drrf
->drr_offset
+
252 drrf
->drr_length
== offset
) {
253 drrf
->drr_length
+= length
;
256 /* not a continuation. Push out pending record */
257 if (dump_record(dsp
, NULL
, 0) != 0)
258 return (SET_ERROR(EINTR
));
259 dsp
->dsa_pending_op
= PENDING_NONE
;
262 /* create a FREE record and make it pending */
263 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
264 dsp
->dsa_drr
->drr_type
= DRR_FREE
;
265 drrf
->drr_object
= object
;
266 drrf
->drr_offset
= offset
;
267 drrf
->drr_length
= length
;
268 drrf
->drr_toguid
= dsp
->dsa_toguid
;
269 if (length
== -1ULL) {
270 if (dump_record(dsp
, NULL
, 0) != 0)
271 return (SET_ERROR(EINTR
));
273 dsp
->dsa_pending_op
= PENDING_FREE
;
280 dump_write(dmu_sendarg_t
*dsp
, dmu_object_type_t type
,
281 uint64_t object
, uint64_t offset
, int blksz
, const blkptr_t
*bp
, void *data
)
283 struct drr_write
*drrw
= &(dsp
->dsa_drr
->drr_u
.drr_write
);
286 * We send data in increasing object, offset order.
287 * See comment in dump_free() for details.
289 ASSERT(object
> dsp
->dsa_last_data_object
||
290 (object
== dsp
->dsa_last_data_object
&&
291 offset
> dsp
->dsa_last_data_offset
));
292 dsp
->dsa_last_data_object
= object
;
293 dsp
->dsa_last_data_offset
= offset
+ blksz
- 1;
296 * If there is any kind of pending aggregation (currently either
297 * a grouping of free objects or free blocks), push it out to
298 * the stream, since aggregation can't be done across operations
299 * of different types.
301 if (dsp
->dsa_pending_op
!= PENDING_NONE
) {
302 if (dump_record(dsp
, NULL
, 0) != 0)
303 return (SET_ERROR(EINTR
));
304 dsp
->dsa_pending_op
= PENDING_NONE
;
306 /* write a WRITE record */
307 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
308 dsp
->dsa_drr
->drr_type
= DRR_WRITE
;
309 drrw
->drr_object
= object
;
310 drrw
->drr_type
= type
;
311 drrw
->drr_offset
= offset
;
312 drrw
->drr_length
= blksz
;
313 drrw
->drr_toguid
= dsp
->dsa_toguid
;
314 if (bp
== NULL
|| BP_IS_EMBEDDED(bp
)) {
316 * There's no pre-computed checksum for partial-block
317 * writes or embedded BP's, so (like
318 * fletcher4-checkummed blocks) userland will have to
319 * compute a dedup-capable checksum itself.
321 drrw
->drr_checksumtype
= ZIO_CHECKSUM_OFF
;
323 drrw
->drr_checksumtype
= BP_GET_CHECKSUM(bp
);
324 if (zio_checksum_table
[drrw
->drr_checksumtype
].ci_dedup
)
325 drrw
->drr_checksumflags
|= DRR_CHECKSUM_DEDUP
;
326 DDK_SET_LSIZE(&drrw
->drr_key
, BP_GET_LSIZE(bp
));
327 DDK_SET_PSIZE(&drrw
->drr_key
, BP_GET_PSIZE(bp
));
328 DDK_SET_COMPRESS(&drrw
->drr_key
, BP_GET_COMPRESS(bp
));
329 drrw
->drr_key
.ddk_cksum
= bp
->blk_cksum
;
332 if (dump_record(dsp
, data
, blksz
) != 0)
333 return (SET_ERROR(EINTR
));
338 dump_write_embedded(dmu_sendarg_t
*dsp
, uint64_t object
, uint64_t offset
,
339 int blksz
, const blkptr_t
*bp
)
341 char buf
[BPE_PAYLOAD_SIZE
];
342 struct drr_write_embedded
*drrw
=
343 &(dsp
->dsa_drr
->drr_u
.drr_write_embedded
);
345 if (dsp
->dsa_pending_op
!= PENDING_NONE
) {
346 if (dump_record(dsp
, NULL
, 0) != 0)
348 dsp
->dsa_pending_op
= PENDING_NONE
;
351 ASSERT(BP_IS_EMBEDDED(bp
));
353 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
354 dsp
->dsa_drr
->drr_type
= DRR_WRITE_EMBEDDED
;
355 drrw
->drr_object
= object
;
356 drrw
->drr_offset
= offset
;
357 drrw
->drr_length
= blksz
;
358 drrw
->drr_toguid
= dsp
->dsa_toguid
;
359 drrw
->drr_compression
= BP_GET_COMPRESS(bp
);
360 drrw
->drr_etype
= BPE_GET_ETYPE(bp
);
361 drrw
->drr_lsize
= BPE_GET_LSIZE(bp
);
362 drrw
->drr_psize
= BPE_GET_PSIZE(bp
);
364 decode_embedded_bp_compressed(bp
, buf
);
366 if (dump_record(dsp
, buf
, P2ROUNDUP(drrw
->drr_psize
, 8)) != 0)
372 dump_spill(dmu_sendarg_t
*dsp
, uint64_t object
, int blksz
, void *data
)
374 struct drr_spill
*drrs
= &(dsp
->dsa_drr
->drr_u
.drr_spill
);
376 if (dsp
->dsa_pending_op
!= PENDING_NONE
) {
377 if (dump_record(dsp
, NULL
, 0) != 0)
378 return (SET_ERROR(EINTR
));
379 dsp
->dsa_pending_op
= PENDING_NONE
;
382 /* write a SPILL record */
383 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
384 dsp
->dsa_drr
->drr_type
= DRR_SPILL
;
385 drrs
->drr_object
= object
;
386 drrs
->drr_length
= blksz
;
387 drrs
->drr_toguid
= dsp
->dsa_toguid
;
389 if (dump_record(dsp
, data
, blksz
) != 0)
390 return (SET_ERROR(EINTR
));
395 dump_freeobjects(dmu_sendarg_t
*dsp
, uint64_t firstobj
, uint64_t numobjs
)
397 struct drr_freeobjects
*drrfo
= &(dsp
->dsa_drr
->drr_u
.drr_freeobjects
);
400 * If there is a pending op, but it's not PENDING_FREEOBJECTS,
401 * push it out, since free block aggregation can only be done for
402 * blocks of the same type (i.e., DRR_FREE records can only be
403 * aggregated with other DRR_FREE records. DRR_FREEOBJECTS records
404 * can only be aggregated with other DRR_FREEOBJECTS records.
406 if (dsp
->dsa_pending_op
!= PENDING_NONE
&&
407 dsp
->dsa_pending_op
!= PENDING_FREEOBJECTS
) {
408 if (dump_record(dsp
, NULL
, 0) != 0)
409 return (SET_ERROR(EINTR
));
410 dsp
->dsa_pending_op
= PENDING_NONE
;
412 if (dsp
->dsa_pending_op
== PENDING_FREEOBJECTS
) {
414 * See whether this free object array can be aggregated
417 if (drrfo
->drr_firstobj
+ drrfo
->drr_numobjs
== firstobj
) {
418 drrfo
->drr_numobjs
+= numobjs
;
421 /* can't be aggregated. Push out pending record */
422 if (dump_record(dsp
, NULL
, 0) != 0)
423 return (SET_ERROR(EINTR
));
424 dsp
->dsa_pending_op
= PENDING_NONE
;
428 /* write a FREEOBJECTS record */
429 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
430 dsp
->dsa_drr
->drr_type
= DRR_FREEOBJECTS
;
431 drrfo
->drr_firstobj
= firstobj
;
432 drrfo
->drr_numobjs
= numobjs
;
433 drrfo
->drr_toguid
= dsp
->dsa_toguid
;
435 dsp
->dsa_pending_op
= PENDING_FREEOBJECTS
;
441 dump_dnode(dmu_sendarg_t
*dsp
, uint64_t object
, dnode_phys_t
*dnp
)
443 struct drr_object
*drro
= &(dsp
->dsa_drr
->drr_u
.drr_object
);
445 if (object
< dsp
->dsa_resume_object
) {
447 * Note: when resuming, we will visit all the dnodes in
448 * the block of dnodes that we are resuming from. In
449 * this case it's unnecessary to send the dnodes prior to
450 * the one we are resuming from. We should be at most one
451 * block's worth of dnodes behind the resume point.
453 ASSERT3U(dsp
->dsa_resume_object
- object
, <,
454 1 << (DNODE_BLOCK_SHIFT
- DNODE_SHIFT
));
458 if (dnp
== NULL
|| dnp
->dn_type
== DMU_OT_NONE
)
459 return (dump_freeobjects(dsp
, object
, 1));
461 if (dsp
->dsa_pending_op
!= PENDING_NONE
) {
462 if (dump_record(dsp
, NULL
, 0) != 0)
463 return (SET_ERROR(EINTR
));
464 dsp
->dsa_pending_op
= PENDING_NONE
;
467 /* write an OBJECT record */
468 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
469 dsp
->dsa_drr
->drr_type
= DRR_OBJECT
;
470 drro
->drr_object
= object
;
471 drro
->drr_type
= dnp
->dn_type
;
472 drro
->drr_bonustype
= dnp
->dn_bonustype
;
473 drro
->drr_blksz
= dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
;
474 drro
->drr_bonuslen
= dnp
->dn_bonuslen
;
475 drro
->drr_dn_slots
= dnp
->dn_extra_slots
+ 1;
476 drro
->drr_checksumtype
= dnp
->dn_checksum
;
477 drro
->drr_compress
= dnp
->dn_compress
;
478 drro
->drr_toguid
= dsp
->dsa_toguid
;
480 if (!(dsp
->dsa_featureflags
& DMU_BACKUP_FEATURE_LARGE_BLOCKS
) &&
481 drro
->drr_blksz
> SPA_OLD_MAXBLOCKSIZE
)
482 drro
->drr_blksz
= SPA_OLD_MAXBLOCKSIZE
;
484 if (dump_record(dsp
, DN_BONUS(dnp
),
485 P2ROUNDUP(dnp
->dn_bonuslen
, 8)) != 0) {
486 return (SET_ERROR(EINTR
));
489 /* Free anything past the end of the file. */
490 if (dump_free(dsp
, object
, (dnp
->dn_maxblkid
+ 1) *
491 (dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
), -1ULL) != 0)
492 return (SET_ERROR(EINTR
));
493 if (dsp
->dsa_err
!= 0)
494 return (SET_ERROR(EINTR
));
499 backup_do_embed(dmu_sendarg_t
*dsp
, const blkptr_t
*bp
)
501 if (!BP_IS_EMBEDDED(bp
))
505 * Compression function must be legacy, or explicitly enabled.
507 if ((BP_GET_COMPRESS(bp
) >= ZIO_COMPRESS_LEGACY_FUNCTIONS
&&
508 !(dsp
->dsa_featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA_LZ4
)))
512 * Embed type must be explicitly enabled.
514 switch (BPE_GET_ETYPE(bp
)) {
515 case BP_EMBEDDED_TYPE_DATA
:
516 if (dsp
->dsa_featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA
)
526 * This is the callback function to traverse_dataset that acts as the worker
527 * thread for dmu_send_impl.
531 send_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
532 const zbookmark_phys_t
*zb
, const struct dnode_phys
*dnp
, void *arg
)
534 struct send_thread_arg
*sta
= arg
;
535 struct send_block_record
*record
;
536 uint64_t record_size
;
539 ASSERT(zb
->zb_object
== DMU_META_DNODE_OBJECT
||
540 zb
->zb_object
>= sta
->resume
.zb_object
);
543 return (SET_ERROR(EINTR
));
546 ASSERT3U(zb
->zb_level
, ==, ZB_DNODE_LEVEL
);
548 } else if (zb
->zb_level
< 0) {
552 record
= kmem_zalloc(sizeof (struct send_block_record
), KM_SLEEP
);
553 record
->eos_marker
= B_FALSE
;
556 record
->indblkshift
= dnp
->dn_indblkshift
;
557 record
->datablkszsec
= dnp
->dn_datablkszsec
;
558 record_size
= dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
;
559 bqueue_enqueue(&sta
->q
, record
, record_size
);
565 * This function kicks off the traverse_dataset. It also handles setting the
566 * error code of the thread in case something goes wrong, and pushes the End of
567 * Stream record when the traverse_dataset call has finished. If there is no
568 * dataset to traverse, the thread immediately pushes End of Stream marker.
571 send_traverse_thread(void *arg
)
573 struct send_thread_arg
*st_arg
= arg
;
575 struct send_block_record
*data
;
576 fstrans_cookie_t cookie
= spl_fstrans_mark();
578 if (st_arg
->ds
!= NULL
) {
579 err
= traverse_dataset_resume(st_arg
->ds
,
580 st_arg
->fromtxg
, &st_arg
->resume
,
581 st_arg
->flags
, send_cb
, st_arg
);
584 st_arg
->error_code
= err
;
586 data
= kmem_zalloc(sizeof (*data
), KM_SLEEP
);
587 data
->eos_marker
= B_TRUE
;
588 bqueue_enqueue(&st_arg
->q
, data
, 1);
589 spl_fstrans_unmark(cookie
);
593 * This function actually handles figuring out what kind of record needs to be
594 * dumped, reading the data (which has hopefully been prefetched), and calling
595 * the appropriate helper function.
598 do_dump(dmu_sendarg_t
*dsa
, struct send_block_record
*data
)
600 dsl_dataset_t
*ds
= dmu_objset_ds(dsa
->dsa_os
);
601 const blkptr_t
*bp
= &data
->bp
;
602 const zbookmark_phys_t
*zb
= &data
->zb
;
603 uint8_t indblkshift
= data
->indblkshift
;
604 uint16_t dblkszsec
= data
->datablkszsec
;
605 spa_t
*spa
= ds
->ds_dir
->dd_pool
->dp_spa
;
606 dmu_object_type_t type
= bp
? BP_GET_TYPE(bp
) : DMU_OT_NONE
;
610 ASSERT3U(zb
->zb_level
, >=, 0);
612 ASSERT(zb
->zb_object
== DMU_META_DNODE_OBJECT
||
613 zb
->zb_object
>= dsa
->dsa_resume_object
);
615 if (zb
->zb_object
!= DMU_META_DNODE_OBJECT
&&
616 DMU_OBJECT_IS_SPECIAL(zb
->zb_object
)) {
618 } else if (BP_IS_HOLE(bp
) &&
619 zb
->zb_object
== DMU_META_DNODE_OBJECT
) {
620 uint64_t span
= BP_SPAN(dblkszsec
, indblkshift
, zb
->zb_level
);
621 uint64_t dnobj
= (zb
->zb_blkid
* span
) >> DNODE_SHIFT
;
622 err
= dump_freeobjects(dsa
, dnobj
, span
>> DNODE_SHIFT
);
623 } else if (BP_IS_HOLE(bp
)) {
624 uint64_t span
= BP_SPAN(dblkszsec
, indblkshift
, zb
->zb_level
);
625 uint64_t offset
= zb
->zb_blkid
* span
;
626 err
= dump_free(dsa
, zb
->zb_object
, offset
, span
);
627 } else if (zb
->zb_level
> 0 || type
== DMU_OT_OBJSET
) {
629 } else if (type
== DMU_OT_DNODE
) {
631 int epb
= BP_GET_LSIZE(bp
) >> DNODE_SHIFT
;
632 arc_flags_t aflags
= ARC_FLAG_WAIT
;
636 ASSERT0(zb
->zb_level
);
638 if (arc_read(NULL
, spa
, bp
, arc_getbuf_func
, &abuf
,
639 ZIO_PRIORITY_ASYNC_READ
, ZIO_FLAG_CANFAIL
,
641 return (SET_ERROR(EIO
));
644 dnobj
= zb
->zb_blkid
* epb
;
645 for (i
= 0; i
< epb
; i
+= blk
[i
].dn_extra_slots
+ 1) {
646 err
= dump_dnode(dsa
, dnobj
+ i
, blk
+ i
);
650 arc_buf_destroy(abuf
, &abuf
);
651 } else if (type
== DMU_OT_SA
) {
652 arc_flags_t aflags
= ARC_FLAG_WAIT
;
654 int blksz
= BP_GET_LSIZE(bp
);
656 if (arc_read(NULL
, spa
, bp
, arc_getbuf_func
, &abuf
,
657 ZIO_PRIORITY_ASYNC_READ
, ZIO_FLAG_CANFAIL
,
659 return (SET_ERROR(EIO
));
661 err
= dump_spill(dsa
, zb
->zb_object
, blksz
, abuf
->b_data
);
662 arc_buf_destroy(abuf
, &abuf
);
663 } else if (backup_do_embed(dsa
, bp
)) {
664 /* it's an embedded level-0 block of a regular object */
665 int blksz
= dblkszsec
<< SPA_MINBLOCKSHIFT
;
666 ASSERT0(zb
->zb_level
);
667 err
= dump_write_embedded(dsa
, zb
->zb_object
,
668 zb
->zb_blkid
* blksz
, blksz
, bp
);
670 /* it's a level-0 block of a regular object */
671 arc_flags_t aflags
= ARC_FLAG_WAIT
;
673 int blksz
= dblkszsec
<< SPA_MINBLOCKSHIFT
;
676 ASSERT0(zb
->zb_level
);
677 ASSERT(zb
->zb_object
> dsa
->dsa_resume_object
||
678 (zb
->zb_object
== dsa
->dsa_resume_object
&&
679 zb
->zb_blkid
* blksz
>= dsa
->dsa_resume_offset
));
681 if (arc_read(NULL
, spa
, bp
, arc_getbuf_func
, &abuf
,
682 ZIO_PRIORITY_ASYNC_READ
, ZIO_FLAG_CANFAIL
,
684 if (zfs_send_corrupt_data
) {
686 /* Send a block filled with 0x"zfs badd bloc" */
687 abuf
= arc_alloc_buf(spa
, blksz
, &abuf
,
689 for (ptr
= abuf
->b_data
;
690 (char *)ptr
< (char *)abuf
->b_data
+ blksz
;
692 *ptr
= 0x2f5baddb10cULL
;
694 return (SET_ERROR(EIO
));
698 offset
= zb
->zb_blkid
* blksz
;
700 if (!(dsa
->dsa_featureflags
&
701 DMU_BACKUP_FEATURE_LARGE_BLOCKS
) &&
702 blksz
> SPA_OLD_MAXBLOCKSIZE
) {
703 char *buf
= abuf
->b_data
;
704 while (blksz
> 0 && err
== 0) {
705 int n
= MIN(blksz
, SPA_OLD_MAXBLOCKSIZE
);
706 err
= dump_write(dsa
, type
, zb
->zb_object
,
707 offset
, n
, NULL
, buf
);
713 err
= dump_write(dsa
, type
, zb
->zb_object
,
714 offset
, blksz
, bp
, abuf
->b_data
);
716 arc_buf_destroy(abuf
, &abuf
);
719 ASSERT(err
== 0 || err
== EINTR
);
724 * Pop the new data off the queue, and free the old data.
726 static struct send_block_record
*
727 get_next_record(bqueue_t
*bq
, struct send_block_record
*data
)
729 struct send_block_record
*tmp
= bqueue_dequeue(bq
);
730 kmem_free(data
, sizeof (*data
));
735 * Actually do the bulk of the work in a zfs send.
737 * Note: Releases dp using the specified tag.
740 dmu_send_impl(void *tag
, dsl_pool_t
*dp
, dsl_dataset_t
*to_ds
,
741 zfs_bookmark_phys_t
*ancestor_zb
,
742 boolean_t is_clone
, boolean_t embedok
, boolean_t large_block_ok
, int outfd
,
743 uint64_t resumeobj
, uint64_t resumeoff
,
744 vnode_t
*vp
, offset_t
*off
)
747 dmu_replay_record_t
*drr
;
750 uint64_t fromtxg
= 0;
751 uint64_t featureflags
= 0;
752 struct send_thread_arg to_arg
;
753 void *payload
= NULL
;
754 size_t payload_len
= 0;
755 struct send_block_record
*to_data
;
757 err
= dmu_objset_from_ds(to_ds
, &os
);
759 dsl_pool_rele(dp
, tag
);
763 drr
= kmem_zalloc(sizeof (dmu_replay_record_t
), KM_SLEEP
);
764 drr
->drr_type
= DRR_BEGIN
;
765 drr
->drr_u
.drr_begin
.drr_magic
= DMU_BACKUP_MAGIC
;
766 DMU_SET_STREAM_HDRTYPE(drr
->drr_u
.drr_begin
.drr_versioninfo
,
769 bzero(&to_arg
, sizeof (to_arg
));
772 if (dmu_objset_type(os
) == DMU_OST_ZFS
) {
774 if (zfs_get_zplprop(os
, ZFS_PROP_VERSION
, &version
) != 0) {
775 kmem_free(drr
, sizeof (dmu_replay_record_t
));
776 dsl_pool_rele(dp
, tag
);
777 return (SET_ERROR(EINVAL
));
779 if (version
>= ZPL_VERSION_SA
) {
780 featureflags
|= DMU_BACKUP_FEATURE_SA_SPILL
;
785 if (large_block_ok
&& to_ds
->ds_feature_inuse
[SPA_FEATURE_LARGE_BLOCKS
])
786 featureflags
|= DMU_BACKUP_FEATURE_LARGE_BLOCKS
;
787 if (to_ds
->ds_feature_inuse
[SPA_FEATURE_LARGE_DNODE
])
788 featureflags
|= DMU_BACKUP_FEATURE_LARGE_DNODE
;
790 spa_feature_is_active(dp
->dp_spa
, SPA_FEATURE_EMBEDDED_DATA
)) {
791 featureflags
|= DMU_BACKUP_FEATURE_EMBED_DATA
;
792 if (spa_feature_is_active(dp
->dp_spa
, SPA_FEATURE_LZ4_COMPRESS
))
793 featureflags
|= DMU_BACKUP_FEATURE_EMBED_DATA_LZ4
;
796 if (resumeobj
!= 0 || resumeoff
!= 0) {
797 featureflags
|= DMU_BACKUP_FEATURE_RESUMING
;
800 DMU_SET_FEATUREFLAGS(drr
->drr_u
.drr_begin
.drr_versioninfo
,
803 drr
->drr_u
.drr_begin
.drr_creation_time
=
804 dsl_dataset_phys(to_ds
)->ds_creation_time
;
805 drr
->drr_u
.drr_begin
.drr_type
= dmu_objset_type(os
);
807 drr
->drr_u
.drr_begin
.drr_flags
|= DRR_FLAG_CLONE
;
808 drr
->drr_u
.drr_begin
.drr_toguid
= dsl_dataset_phys(to_ds
)->ds_guid
;
809 if (dsl_dataset_phys(to_ds
)->ds_flags
& DS_FLAG_CI_DATASET
)
810 drr
->drr_u
.drr_begin
.drr_flags
|= DRR_FLAG_CI_DATA
;
811 if (zfs_send_set_freerecords_bit
)
812 drr
->drr_u
.drr_begin
.drr_flags
|= DRR_FLAG_FREERECORDS
;
814 if (ancestor_zb
!= NULL
) {
815 drr
->drr_u
.drr_begin
.drr_fromguid
=
816 ancestor_zb
->zbm_guid
;
817 fromtxg
= ancestor_zb
->zbm_creation_txg
;
819 dsl_dataset_name(to_ds
, drr
->drr_u
.drr_begin
.drr_toname
);
820 if (!to_ds
->ds_is_snapshot
) {
821 (void) strlcat(drr
->drr_u
.drr_begin
.drr_toname
, "@--head--",
822 sizeof (drr
->drr_u
.drr_begin
.drr_toname
));
825 dsp
= kmem_zalloc(sizeof (dmu_sendarg_t
), KM_SLEEP
);
829 dsp
->dsa_outfd
= outfd
;
830 dsp
->dsa_proc
= curproc
;
833 dsp
->dsa_toguid
= dsl_dataset_phys(to_ds
)->ds_guid
;
834 dsp
->dsa_pending_op
= PENDING_NONE
;
835 dsp
->dsa_featureflags
= featureflags
;
836 dsp
->dsa_resume_object
= resumeobj
;
837 dsp
->dsa_resume_offset
= resumeoff
;
839 mutex_enter(&to_ds
->ds_sendstream_lock
);
840 list_insert_head(&to_ds
->ds_sendstreams
, dsp
);
841 mutex_exit(&to_ds
->ds_sendstream_lock
);
843 dsl_dataset_long_hold(to_ds
, FTAG
);
844 dsl_pool_rele(dp
, tag
);
846 if (resumeobj
!= 0 || resumeoff
!= 0) {
847 dmu_object_info_t to_doi
;
849 err
= dmu_object_info(os
, resumeobj
, &to_doi
);
852 SET_BOOKMARK(&to_arg
.resume
, to_ds
->ds_object
, resumeobj
, 0,
853 resumeoff
/ to_doi
.doi_data_block_size
);
855 nvl
= fnvlist_alloc();
856 fnvlist_add_uint64(nvl
, "resume_object", resumeobj
);
857 fnvlist_add_uint64(nvl
, "resume_offset", resumeoff
);
858 payload
= fnvlist_pack(nvl
, &payload_len
);
859 drr
->drr_payloadlen
= payload_len
;
863 err
= dump_record(dsp
, payload
, payload_len
);
864 fnvlist_pack_free(payload
, payload_len
);
870 err
= bqueue_init(&to_arg
.q
, zfs_send_queue_length
,
871 offsetof(struct send_block_record
, ln
));
872 to_arg
.error_code
= 0;
873 to_arg
.cancel
= B_FALSE
;
875 to_arg
.fromtxg
= fromtxg
;
876 to_arg
.flags
= TRAVERSE_PRE
| TRAVERSE_PREFETCH
;
877 (void) thread_create(NULL
, 0, send_traverse_thread
, &to_arg
, 0, curproc
,
878 TS_RUN
, minclsyspri
);
880 to_data
= bqueue_dequeue(&to_arg
.q
);
882 while (!to_data
->eos_marker
&& err
== 0) {
883 err
= do_dump(dsp
, to_data
);
884 to_data
= get_next_record(&to_arg
.q
, to_data
);
885 if (issig(JUSTLOOKING
) && issig(FORREAL
))
890 to_arg
.cancel
= B_TRUE
;
891 while (!to_data
->eos_marker
) {
892 to_data
= get_next_record(&to_arg
.q
, to_data
);
895 kmem_free(to_data
, sizeof (*to_data
));
897 bqueue_destroy(&to_arg
.q
);
899 if (err
== 0 && to_arg
.error_code
!= 0)
900 err
= to_arg
.error_code
;
905 if (dsp
->dsa_pending_op
!= PENDING_NONE
)
906 if (dump_record(dsp
, NULL
, 0) != 0)
907 err
= SET_ERROR(EINTR
);
910 if (err
== EINTR
&& dsp
->dsa_err
!= 0)
915 bzero(drr
, sizeof (dmu_replay_record_t
));
916 drr
->drr_type
= DRR_END
;
917 drr
->drr_u
.drr_end
.drr_checksum
= dsp
->dsa_zc
;
918 drr
->drr_u
.drr_end
.drr_toguid
= dsp
->dsa_toguid
;
920 if (dump_record(dsp
, NULL
, 0) != 0)
924 mutex_enter(&to_ds
->ds_sendstream_lock
);
925 list_remove(&to_ds
->ds_sendstreams
, dsp
);
926 mutex_exit(&to_ds
->ds_sendstream_lock
);
928 kmem_free(drr
, sizeof (dmu_replay_record_t
));
929 kmem_free(dsp
, sizeof (dmu_sendarg_t
));
931 dsl_dataset_long_rele(to_ds
, FTAG
);
937 dmu_send_obj(const char *pool
, uint64_t tosnap
, uint64_t fromsnap
,
938 boolean_t embedok
, boolean_t large_block_ok
,
939 int outfd
, vnode_t
*vp
, offset_t
*off
)
943 dsl_dataset_t
*fromds
= NULL
;
946 err
= dsl_pool_hold(pool
, FTAG
, &dp
);
950 err
= dsl_dataset_hold_obj(dp
, tosnap
, FTAG
, &ds
);
952 dsl_pool_rele(dp
, FTAG
);
957 zfs_bookmark_phys_t zb
;
960 err
= dsl_dataset_hold_obj(dp
, fromsnap
, FTAG
, &fromds
);
962 dsl_dataset_rele(ds
, FTAG
);
963 dsl_pool_rele(dp
, FTAG
);
966 if (!dsl_dataset_is_before(ds
, fromds
, 0))
967 err
= SET_ERROR(EXDEV
);
968 zb
.zbm_creation_time
=
969 dsl_dataset_phys(fromds
)->ds_creation_time
;
970 zb
.zbm_creation_txg
= dsl_dataset_phys(fromds
)->ds_creation_txg
;
971 zb
.zbm_guid
= dsl_dataset_phys(fromds
)->ds_guid
;
972 is_clone
= (fromds
->ds_dir
!= ds
->ds_dir
);
973 dsl_dataset_rele(fromds
, FTAG
);
974 err
= dmu_send_impl(FTAG
, dp
, ds
, &zb
, is_clone
,
975 embedok
, large_block_ok
, outfd
, 0, 0, vp
, off
);
977 err
= dmu_send_impl(FTAG
, dp
, ds
, NULL
, B_FALSE
,
978 embedok
, large_block_ok
, outfd
, 0, 0, vp
, off
);
980 dsl_dataset_rele(ds
, FTAG
);
985 dmu_send(const char *tosnap
, const char *fromsnap
, boolean_t embedok
,
986 boolean_t large_block_ok
, int outfd
, uint64_t resumeobj
, uint64_t resumeoff
,
987 vnode_t
*vp
, offset_t
*off
)
992 boolean_t owned
= B_FALSE
;
994 if (fromsnap
!= NULL
&& strpbrk(fromsnap
, "@#") == NULL
)
995 return (SET_ERROR(EINVAL
));
997 err
= dsl_pool_hold(tosnap
, FTAG
, &dp
);
1001 if (strchr(tosnap
, '@') == NULL
&& spa_writeable(dp
->dp_spa
)) {
1003 * We are sending a filesystem or volume. Ensure
1004 * that it doesn't change by owning the dataset.
1006 err
= dsl_dataset_own(dp
, tosnap
, FTAG
, &ds
);
1009 err
= dsl_dataset_hold(dp
, tosnap
, FTAG
, &ds
);
1012 dsl_pool_rele(dp
, FTAG
);
1016 if (fromsnap
!= NULL
) {
1017 zfs_bookmark_phys_t zb
;
1018 boolean_t is_clone
= B_FALSE
;
1019 int fsnamelen
= strchr(tosnap
, '@') - tosnap
;
1022 * If the fromsnap is in a different filesystem, then
1023 * mark the send stream as a clone.
1025 if (strncmp(tosnap
, fromsnap
, fsnamelen
) != 0 ||
1026 (fromsnap
[fsnamelen
] != '@' &&
1027 fromsnap
[fsnamelen
] != '#')) {
1031 if (strchr(fromsnap
, '@')) {
1032 dsl_dataset_t
*fromds
;
1033 err
= dsl_dataset_hold(dp
, fromsnap
, FTAG
, &fromds
);
1035 if (!dsl_dataset_is_before(ds
, fromds
, 0))
1036 err
= SET_ERROR(EXDEV
);
1037 zb
.zbm_creation_time
=
1038 dsl_dataset_phys(fromds
)->ds_creation_time
;
1039 zb
.zbm_creation_txg
=
1040 dsl_dataset_phys(fromds
)->ds_creation_txg
;
1041 zb
.zbm_guid
= dsl_dataset_phys(fromds
)->ds_guid
;
1042 is_clone
= (ds
->ds_dir
!= fromds
->ds_dir
);
1043 dsl_dataset_rele(fromds
, FTAG
);
1046 err
= dsl_bookmark_lookup(dp
, fromsnap
, ds
, &zb
);
1049 dsl_dataset_rele(ds
, FTAG
);
1050 dsl_pool_rele(dp
, FTAG
);
1053 err
= dmu_send_impl(FTAG
, dp
, ds
, &zb
, is_clone
,
1054 embedok
, large_block_ok
,
1055 outfd
, resumeobj
, resumeoff
, vp
, off
);
1057 err
= dmu_send_impl(FTAG
, dp
, ds
, NULL
, B_FALSE
,
1058 embedok
, large_block_ok
,
1059 outfd
, resumeobj
, resumeoff
, vp
, off
);
1062 dsl_dataset_disown(ds
, FTAG
);
1064 dsl_dataset_rele(ds
, FTAG
);
1069 dmu_adjust_send_estimate_for_indirects(dsl_dataset_t
*ds
, uint64_t size
,
1074 * Assume that space (both on-disk and in-stream) is dominated by
1075 * data. We will adjust for indirect blocks and the copies property,
1076 * but ignore per-object space used (eg, dnodes and DRR_OBJECT records).
1080 * Subtract out approximate space used by indirect blocks.
1081 * Assume most space is used by data blocks (non-indirect, non-dnode).
1082 * Assume all blocks are recordsize. Assume ditto blocks and
1083 * internal fragmentation counter out compression.
1085 * Therefore, space used by indirect blocks is sizeof(blkptr_t) per
1086 * block, which we observe in practice.
1088 uint64_t recordsize
;
1089 err
= dsl_prop_get_int_ds(ds
, "recordsize", &recordsize
);
1092 size
-= size
/ recordsize
* sizeof (blkptr_t
);
1094 /* Add in the space for the record associated with each block. */
1095 size
+= size
/ recordsize
* sizeof (dmu_replay_record_t
);
1103 dmu_send_estimate(dsl_dataset_t
*ds
, dsl_dataset_t
*fromds
, uint64_t *sizep
)
1108 ASSERT(dsl_pool_config_held(ds
->ds_dir
->dd_pool
));
1110 /* tosnap must be a snapshot */
1111 if (!ds
->ds_is_snapshot
)
1112 return (SET_ERROR(EINVAL
));
1114 /* fromsnap, if provided, must be a snapshot */
1115 if (fromds
!= NULL
&& !fromds
->ds_is_snapshot
)
1116 return (SET_ERROR(EINVAL
));
1119 * fromsnap must be an earlier snapshot from the same fs as tosnap,
1120 * or the origin's fs.
1122 if (fromds
!= NULL
&& !dsl_dataset_is_before(ds
, fromds
, 0))
1123 return (SET_ERROR(EXDEV
));
1125 /* Get uncompressed size estimate of changed data. */
1126 if (fromds
== NULL
) {
1127 size
= dsl_dataset_phys(ds
)->ds_uncompressed_bytes
;
1129 uint64_t used
, comp
;
1130 err
= dsl_dataset_space_written(fromds
, ds
,
1131 &used
, &comp
, &size
);
1136 err
= dmu_adjust_send_estimate_for_indirects(ds
, size
, sizep
);
1141 * Simple callback used to traverse the blocks of a snapshot and sum their
1146 dmu_calculate_send_traversal(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
1147 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
1149 uint64_t *spaceptr
= arg
;
1150 if (bp
!= NULL
&& !BP_IS_HOLE(bp
)) {
1151 *spaceptr
+= BP_GET_UCSIZE(bp
);
1157 * Given a desination snapshot and a TXG, calculate the approximate size of a
1158 * send stream sent from that TXG. from_txg may be zero, indicating that the
1159 * whole snapshot will be sent.
1162 dmu_send_estimate_from_txg(dsl_dataset_t
*ds
, uint64_t from_txg
,
1168 ASSERT(dsl_pool_config_held(ds
->ds_dir
->dd_pool
));
1170 /* tosnap must be a snapshot */
1171 if (!dsl_dataset_is_snapshot(ds
))
1172 return (SET_ERROR(EINVAL
));
1174 /* verify that from_txg is before the provided snapshot was taken */
1175 if (from_txg
>= dsl_dataset_phys(ds
)->ds_creation_txg
) {
1176 return (SET_ERROR(EXDEV
));
1179 * traverse the blocks of the snapshot with birth times after
1180 * from_txg, summing their uncompressed size
1182 err
= traverse_dataset(ds
, from_txg
, TRAVERSE_POST
,
1183 dmu_calculate_send_traversal
, &size
);
1187 err
= dmu_adjust_send_estimate_for_indirects(ds
, size
, sizep
);
1191 typedef struct dmu_recv_begin_arg
{
1192 const char *drba_origin
;
1193 dmu_recv_cookie_t
*drba_cookie
;
1195 uint64_t drba_snapobj
;
1196 } dmu_recv_begin_arg_t
;
1199 recv_begin_check_existing_impl(dmu_recv_begin_arg_t
*drba
, dsl_dataset_t
*ds
,
1204 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
1206 /* temporary clone name must not exist */
1207 error
= zap_lookup(dp
->dp_meta_objset
,
1208 dsl_dir_phys(ds
->ds_dir
)->dd_child_dir_zapobj
, recv_clone_name
,
1210 if (error
!= ENOENT
)
1211 return (error
== 0 ? EBUSY
: error
);
1213 /* new snapshot name must not exist */
1214 error
= zap_lookup(dp
->dp_meta_objset
,
1215 dsl_dataset_phys(ds
)->ds_snapnames_zapobj
,
1216 drba
->drba_cookie
->drc_tosnap
, 8, 1, &val
);
1217 if (error
!= ENOENT
)
1218 return (error
== 0 ? EEXIST
: error
);
1221 * Check snapshot limit before receiving. We'll recheck again at the
1222 * end, but might as well abort before receiving if we're already over
1225 * Note that we do not check the file system limit with
1226 * dsl_dir_fscount_check because the temporary %clones don't count
1227 * against that limit.
1229 error
= dsl_fs_ss_limit_check(ds
->ds_dir
, 1, ZFS_PROP_SNAPSHOT_LIMIT
,
1230 NULL
, drba
->drba_cred
);
1234 if (fromguid
!= 0) {
1235 dsl_dataset_t
*snap
;
1236 uint64_t obj
= dsl_dataset_phys(ds
)->ds_prev_snap_obj
;
1238 /* Find snapshot in this dir that matches fromguid. */
1240 error
= dsl_dataset_hold_obj(dp
, obj
, FTAG
,
1243 return (SET_ERROR(ENODEV
));
1244 if (snap
->ds_dir
!= ds
->ds_dir
) {
1245 dsl_dataset_rele(snap
, FTAG
);
1246 return (SET_ERROR(ENODEV
));
1248 if (dsl_dataset_phys(snap
)->ds_guid
== fromguid
)
1250 obj
= dsl_dataset_phys(snap
)->ds_prev_snap_obj
;
1251 dsl_dataset_rele(snap
, FTAG
);
1254 return (SET_ERROR(ENODEV
));
1256 if (drba
->drba_cookie
->drc_force
) {
1257 drba
->drba_snapobj
= obj
;
1260 * If we are not forcing, there must be no
1261 * changes since fromsnap.
1263 if (dsl_dataset_modified_since_snap(ds
, snap
)) {
1264 dsl_dataset_rele(snap
, FTAG
);
1265 return (SET_ERROR(ETXTBSY
));
1267 drba
->drba_snapobj
= ds
->ds_prev
->ds_object
;
1270 dsl_dataset_rele(snap
, FTAG
);
1272 /* if full, then must be forced */
1273 if (!drba
->drba_cookie
->drc_force
)
1274 return (SET_ERROR(EEXIST
));
1275 /* start from $ORIGIN@$ORIGIN, if supported */
1276 drba
->drba_snapobj
= dp
->dp_origin_snap
!= NULL
?
1277 dp
->dp_origin_snap
->ds_object
: 0;
1285 dmu_recv_begin_check(void *arg
, dmu_tx_t
*tx
)
1287 dmu_recv_begin_arg_t
*drba
= arg
;
1288 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1289 struct drr_begin
*drrb
= drba
->drba_cookie
->drc_drrb
;
1290 uint64_t fromguid
= drrb
->drr_fromguid
;
1291 int flags
= drrb
->drr_flags
;
1293 uint64_t featureflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
1295 const char *tofs
= drba
->drba_cookie
->drc_tofs
;
1297 /* already checked */
1298 ASSERT3U(drrb
->drr_magic
, ==, DMU_BACKUP_MAGIC
);
1299 ASSERT(!(featureflags
& DMU_BACKUP_FEATURE_RESUMING
));
1301 if (DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) ==
1302 DMU_COMPOUNDSTREAM
||
1303 drrb
->drr_type
>= DMU_OST_NUMTYPES
||
1304 ((flags
& DRR_FLAG_CLONE
) && drba
->drba_origin
== NULL
))
1305 return (SET_ERROR(EINVAL
));
1307 /* Verify pool version supports SA if SA_SPILL feature set */
1308 if ((featureflags
& DMU_BACKUP_FEATURE_SA_SPILL
) &&
1309 spa_version(dp
->dp_spa
) < SPA_VERSION_SA
)
1310 return (SET_ERROR(ENOTSUP
));
1312 if (drba
->drba_cookie
->drc_resumable
&&
1313 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_EXTENSIBLE_DATASET
))
1314 return (SET_ERROR(ENOTSUP
));
1317 * The receiving code doesn't know how to translate a WRITE_EMBEDDED
1318 * record to a plan WRITE record, so the pool must have the
1319 * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
1320 * records. Same with WRITE_EMBEDDED records that use LZ4 compression.
1322 if ((featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA
) &&
1323 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_EMBEDDED_DATA
))
1324 return (SET_ERROR(ENOTSUP
));
1325 if ((featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA_LZ4
) &&
1326 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_LZ4_COMPRESS
))
1327 return (SET_ERROR(ENOTSUP
));
1330 * The receiving code doesn't know how to translate large blocks
1331 * to smaller ones, so the pool must have the LARGE_BLOCKS
1332 * feature enabled if the stream has LARGE_BLOCKS.
1334 if ((featureflags
& DMU_BACKUP_FEATURE_LARGE_BLOCKS
) &&
1335 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_LARGE_BLOCKS
))
1336 return (SET_ERROR(ENOTSUP
));
1339 * The receiving code doesn't know how to translate large dnodes
1340 * to smaller ones, so the pool must have the LARGE_DNODE
1341 * feature enabled if the stream has LARGE_DNODE.
1343 if ((featureflags
& DMU_BACKUP_FEATURE_LARGE_DNODE
) &&
1344 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_LARGE_DNODE
))
1345 return (SET_ERROR(ENOTSUP
));
1347 error
= dsl_dataset_hold(dp
, tofs
, FTAG
, &ds
);
1349 /* target fs already exists; recv into temp clone */
1351 /* Can't recv a clone into an existing fs */
1352 if (flags
& DRR_FLAG_CLONE
|| drba
->drba_origin
) {
1353 dsl_dataset_rele(ds
, FTAG
);
1354 return (SET_ERROR(EINVAL
));
1357 error
= recv_begin_check_existing_impl(drba
, ds
, fromguid
);
1358 dsl_dataset_rele(ds
, FTAG
);
1359 } else if (error
== ENOENT
) {
1360 /* target fs does not exist; must be a full backup or clone */
1361 char buf
[ZFS_MAX_DATASET_NAME_LEN
];
1364 * If it's a non-clone incremental, we are missing the
1365 * target fs, so fail the recv.
1367 if (fromguid
!= 0 && !(flags
& DRR_FLAG_CLONE
||
1369 return (SET_ERROR(ENOENT
));
1372 * If we're receiving a full send as a clone, and it doesn't
1373 * contain all the necessary free records and freeobject
1374 * records, reject it.
1376 if (fromguid
== 0 && drba
->drba_origin
&&
1377 !(flags
& DRR_FLAG_FREERECORDS
))
1378 return (SET_ERROR(EINVAL
));
1380 /* Open the parent of tofs */
1381 ASSERT3U(strlen(tofs
), <, sizeof (buf
));
1382 (void) strlcpy(buf
, tofs
, strrchr(tofs
, '/') - tofs
+ 1);
1383 error
= dsl_dataset_hold(dp
, buf
, FTAG
, &ds
);
1388 * Check filesystem and snapshot limits before receiving. We'll
1389 * recheck snapshot limits again at the end (we create the
1390 * filesystems and increment those counts during begin_sync).
1392 error
= dsl_fs_ss_limit_check(ds
->ds_dir
, 1,
1393 ZFS_PROP_FILESYSTEM_LIMIT
, NULL
, drba
->drba_cred
);
1395 dsl_dataset_rele(ds
, FTAG
);
1399 error
= dsl_fs_ss_limit_check(ds
->ds_dir
, 1,
1400 ZFS_PROP_SNAPSHOT_LIMIT
, NULL
, drba
->drba_cred
);
1402 dsl_dataset_rele(ds
, FTAG
);
1406 if (drba
->drba_origin
!= NULL
) {
1407 dsl_dataset_t
*origin
;
1408 error
= dsl_dataset_hold(dp
, drba
->drba_origin
,
1411 dsl_dataset_rele(ds
, FTAG
);
1414 if (!origin
->ds_is_snapshot
) {
1415 dsl_dataset_rele(origin
, FTAG
);
1416 dsl_dataset_rele(ds
, FTAG
);
1417 return (SET_ERROR(EINVAL
));
1419 if (dsl_dataset_phys(origin
)->ds_guid
!= fromguid
&&
1421 dsl_dataset_rele(origin
, FTAG
);
1422 dsl_dataset_rele(ds
, FTAG
);
1423 return (SET_ERROR(ENODEV
));
1425 dsl_dataset_rele(origin
, FTAG
);
1427 dsl_dataset_rele(ds
, FTAG
);
1434 dmu_recv_begin_sync(void *arg
, dmu_tx_t
*tx
)
1436 dmu_recv_begin_arg_t
*drba
= arg
;
1437 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1438 objset_t
*mos
= dp
->dp_meta_objset
;
1439 struct drr_begin
*drrb
= drba
->drba_cookie
->drc_drrb
;
1440 const char *tofs
= drba
->drba_cookie
->drc_tofs
;
1441 dsl_dataset_t
*ds
, *newds
;
1444 uint64_t crflags
= 0;
1446 if (drrb
->drr_flags
& DRR_FLAG_CI_DATA
)
1447 crflags
|= DS_FLAG_CI_DATASET
;
1449 error
= dsl_dataset_hold(dp
, tofs
, FTAG
, &ds
);
1451 /* create temporary clone */
1452 dsl_dataset_t
*snap
= NULL
;
1453 if (drba
->drba_snapobj
!= 0) {
1454 VERIFY0(dsl_dataset_hold_obj(dp
,
1455 drba
->drba_snapobj
, FTAG
, &snap
));
1457 dsobj
= dsl_dataset_create_sync(ds
->ds_dir
, recv_clone_name
,
1458 snap
, crflags
, drba
->drba_cred
, tx
);
1459 if (drba
->drba_snapobj
!= 0)
1460 dsl_dataset_rele(snap
, FTAG
);
1461 dsl_dataset_rele(ds
, FTAG
);
1465 dsl_dataset_t
*origin
= NULL
;
1467 VERIFY0(dsl_dir_hold(dp
, tofs
, FTAG
, &dd
, &tail
));
1469 if (drba
->drba_origin
!= NULL
) {
1470 VERIFY0(dsl_dataset_hold(dp
, drba
->drba_origin
,
1474 /* Create new dataset. */
1475 dsobj
= dsl_dataset_create_sync(dd
,
1476 strrchr(tofs
, '/') + 1,
1477 origin
, crflags
, drba
->drba_cred
, tx
);
1479 dsl_dataset_rele(origin
, FTAG
);
1480 dsl_dir_rele(dd
, FTAG
);
1481 drba
->drba_cookie
->drc_newfs
= B_TRUE
;
1483 VERIFY0(dsl_dataset_own_obj(dp
, dsobj
, dmu_recv_tag
, &newds
));
1485 if (drba
->drba_cookie
->drc_resumable
) {
1489 dsl_dataset_zapify(newds
, tx
);
1490 if (drrb
->drr_fromguid
!= 0) {
1491 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_FROMGUID
,
1492 8, 1, &drrb
->drr_fromguid
, tx
));
1494 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_TOGUID
,
1495 8, 1, &drrb
->drr_toguid
, tx
));
1496 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_TONAME
,
1497 1, strlen(drrb
->drr_toname
) + 1, drrb
->drr_toname
, tx
));
1498 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_OBJECT
,
1500 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_OFFSET
,
1502 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_BYTES
,
1504 if (DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
) &
1505 DMU_BACKUP_FEATURE_EMBED_DATA
) {
1506 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_EMBEDOK
,
1511 dmu_buf_will_dirty(newds
->ds_dbuf
, tx
);
1512 dsl_dataset_phys(newds
)->ds_flags
|= DS_FLAG_INCONSISTENT
;
1515 * If we actually created a non-clone, we need to create the
1516 * objset in our new dataset.
1518 if (BP_IS_HOLE(dsl_dataset_get_blkptr(newds
))) {
1519 (void) dmu_objset_create_impl(dp
->dp_spa
,
1520 newds
, dsl_dataset_get_blkptr(newds
), drrb
->drr_type
, tx
);
1523 drba
->drba_cookie
->drc_ds
= newds
;
1525 spa_history_log_internal_ds(newds
, "receive", tx
, "");
1529 dmu_recv_resume_begin_check(void *arg
, dmu_tx_t
*tx
)
1531 dmu_recv_begin_arg_t
*drba
= arg
;
1532 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1533 struct drr_begin
*drrb
= drba
->drba_cookie
->drc_drrb
;
1535 uint64_t featureflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
1537 const char *tofs
= drba
->drba_cookie
->drc_tofs
;
1540 /* 6 extra bytes for /%recv */
1541 char recvname
[ZFS_MAX_DATASET_NAME_LEN
+ 6];
1543 /* already checked */
1544 ASSERT3U(drrb
->drr_magic
, ==, DMU_BACKUP_MAGIC
);
1545 ASSERT(featureflags
& DMU_BACKUP_FEATURE_RESUMING
);
1547 if (DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) ==
1548 DMU_COMPOUNDSTREAM
||
1549 drrb
->drr_type
>= DMU_OST_NUMTYPES
)
1550 return (SET_ERROR(EINVAL
));
1552 /* Verify pool version supports SA if SA_SPILL feature set */
1553 if ((featureflags
& DMU_BACKUP_FEATURE_SA_SPILL
) &&
1554 spa_version(dp
->dp_spa
) < SPA_VERSION_SA
)
1555 return (SET_ERROR(ENOTSUP
));
1558 * The receiving code doesn't know how to translate a WRITE_EMBEDDED
1559 * record to a plain WRITE record, so the pool must have the
1560 * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
1561 * records. Same with WRITE_EMBEDDED records that use LZ4 compression.
1563 if ((featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA
) &&
1564 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_EMBEDDED_DATA
))
1565 return (SET_ERROR(ENOTSUP
));
1566 if ((featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA_LZ4
) &&
1567 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_LZ4_COMPRESS
))
1568 return (SET_ERROR(ENOTSUP
));
1570 (void) snprintf(recvname
, sizeof (recvname
), "%s/%s",
1571 tofs
, recv_clone_name
);
1573 if (dsl_dataset_hold(dp
, recvname
, FTAG
, &ds
) != 0) {
1574 /* %recv does not exist; continue in tofs */
1575 error
= dsl_dataset_hold(dp
, tofs
, FTAG
, &ds
);
1580 /* check that ds is marked inconsistent */
1581 if (!DS_IS_INCONSISTENT(ds
)) {
1582 dsl_dataset_rele(ds
, FTAG
);
1583 return (SET_ERROR(EINVAL
));
1586 /* check that there is resuming data, and that the toguid matches */
1587 if (!dsl_dataset_is_zapified(ds
)) {
1588 dsl_dataset_rele(ds
, FTAG
);
1589 return (SET_ERROR(EINVAL
));
1591 error
= zap_lookup(dp
->dp_meta_objset
, ds
->ds_object
,
1592 DS_FIELD_RESUME_TOGUID
, sizeof (val
), 1, &val
);
1593 if (error
!= 0 || drrb
->drr_toguid
!= val
) {
1594 dsl_dataset_rele(ds
, FTAG
);
1595 return (SET_ERROR(EINVAL
));
1599 * Check if the receive is still running. If so, it will be owned.
1600 * Note that nothing else can own the dataset (e.g. after the receive
1601 * fails) because it will be marked inconsistent.
1603 if (dsl_dataset_has_owner(ds
)) {
1604 dsl_dataset_rele(ds
, FTAG
);
1605 return (SET_ERROR(EBUSY
));
1608 /* There should not be any snapshots of this fs yet. */
1609 if (ds
->ds_prev
!= NULL
&& ds
->ds_prev
->ds_dir
== ds
->ds_dir
) {
1610 dsl_dataset_rele(ds
, FTAG
);
1611 return (SET_ERROR(EINVAL
));
1615 * Note: resume point will be checked when we process the first WRITE
1619 /* check that the origin matches */
1621 (void) zap_lookup(dp
->dp_meta_objset
, ds
->ds_object
,
1622 DS_FIELD_RESUME_FROMGUID
, sizeof (val
), 1, &val
);
1623 if (drrb
->drr_fromguid
!= val
) {
1624 dsl_dataset_rele(ds
, FTAG
);
1625 return (SET_ERROR(EINVAL
));
1628 dsl_dataset_rele(ds
, FTAG
);
1633 dmu_recv_resume_begin_sync(void *arg
, dmu_tx_t
*tx
)
1635 dmu_recv_begin_arg_t
*drba
= arg
;
1636 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1637 const char *tofs
= drba
->drba_cookie
->drc_tofs
;
1640 /* 6 extra bytes for /%recv */
1641 char recvname
[ZFS_MAX_DATASET_NAME_LEN
+ 6];
1643 (void) snprintf(recvname
, sizeof (recvname
), "%s/%s",
1644 tofs
, recv_clone_name
);
1646 if (dsl_dataset_hold(dp
, recvname
, FTAG
, &ds
) != 0) {
1647 /* %recv does not exist; continue in tofs */
1648 VERIFY0(dsl_dataset_hold(dp
, tofs
, FTAG
, &ds
));
1649 drba
->drba_cookie
->drc_newfs
= B_TRUE
;
1652 /* clear the inconsistent flag so that we can own it */
1653 ASSERT(DS_IS_INCONSISTENT(ds
));
1654 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
1655 dsl_dataset_phys(ds
)->ds_flags
&= ~DS_FLAG_INCONSISTENT
;
1656 dsobj
= ds
->ds_object
;
1657 dsl_dataset_rele(ds
, FTAG
);
1659 VERIFY0(dsl_dataset_own_obj(dp
, dsobj
, dmu_recv_tag
, &ds
));
1661 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
1662 dsl_dataset_phys(ds
)->ds_flags
|= DS_FLAG_INCONSISTENT
;
1664 ASSERT(!BP_IS_HOLE(dsl_dataset_get_blkptr(ds
)));
1666 drba
->drba_cookie
->drc_ds
= ds
;
1668 spa_history_log_internal_ds(ds
, "resume receive", tx
, "");
1672 * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
1673 * succeeds; otherwise we will leak the holds on the datasets.
1676 dmu_recv_begin(char *tofs
, char *tosnap
, dmu_replay_record_t
*drr_begin
,
1677 boolean_t force
, boolean_t resumable
, char *origin
, dmu_recv_cookie_t
*drc
)
1679 dmu_recv_begin_arg_t drba
= { 0 };
1681 bzero(drc
, sizeof (dmu_recv_cookie_t
));
1682 drc
->drc_drr_begin
= drr_begin
;
1683 drc
->drc_drrb
= &drr_begin
->drr_u
.drr_begin
;
1684 drc
->drc_tosnap
= tosnap
;
1685 drc
->drc_tofs
= tofs
;
1686 drc
->drc_force
= force
;
1687 drc
->drc_resumable
= resumable
;
1688 drc
->drc_cred
= CRED();
1690 if (drc
->drc_drrb
->drr_magic
== BSWAP_64(DMU_BACKUP_MAGIC
)) {
1691 drc
->drc_byteswap
= B_TRUE
;
1692 fletcher_4_incremental_byteswap(drr_begin
,
1693 sizeof (dmu_replay_record_t
), &drc
->drc_cksum
);
1694 byteswap_record(drr_begin
);
1695 } else if (drc
->drc_drrb
->drr_magic
== DMU_BACKUP_MAGIC
) {
1696 fletcher_4_incremental_native(drr_begin
,
1697 sizeof (dmu_replay_record_t
), &drc
->drc_cksum
);
1699 return (SET_ERROR(EINVAL
));
1702 drba
.drba_origin
= origin
;
1703 drba
.drba_cookie
= drc
;
1704 drba
.drba_cred
= CRED();
1706 if (DMU_GET_FEATUREFLAGS(drc
->drc_drrb
->drr_versioninfo
) &
1707 DMU_BACKUP_FEATURE_RESUMING
) {
1708 return (dsl_sync_task(tofs
,
1709 dmu_recv_resume_begin_check
, dmu_recv_resume_begin_sync
,
1710 &drba
, 5, ZFS_SPACE_CHECK_NORMAL
));
1712 return (dsl_sync_task(tofs
,
1713 dmu_recv_begin_check
, dmu_recv_begin_sync
,
1714 &drba
, 5, ZFS_SPACE_CHECK_NORMAL
));
1718 struct receive_record_arg
{
1719 dmu_replay_record_t header
;
1720 void *payload
; /* Pointer to a buffer containing the payload */
1722 * If the record is a write, pointer to the arc_buf_t containing the
1725 arc_buf_t
*write_buf
;
1727 uint64_t bytes_read
; /* bytes read from stream when record created */
1728 boolean_t eos_marker
; /* Marks the end of the stream */
1732 struct receive_writer_arg
{
1738 * These three args are used to signal to the main thread that we're
1746 /* A map from guid to dataset to help handle dedup'd streams. */
1747 avl_tree_t
*guid_to_ds_map
;
1748 boolean_t resumable
;
1749 uint64_t last_object
, last_offset
;
1750 uint64_t bytes_read
; /* bytes read when current record created */
1754 list_t list
; /* List of struct receive_objnode. */
1756 * Last object looked up. Used to assert that objects are being looked
1757 * up in ascending order.
1759 uint64_t last_lookup
;
1762 struct receive_objnode
{
1767 struct receive_arg
{
1769 vnode_t
*vp
; /* The vnode to read the stream from */
1770 uint64_t voff
; /* The current offset in the stream */
1771 uint64_t bytes_read
;
1773 * A record that has had its payload read in, but hasn't yet been handed
1774 * off to the worker thread.
1776 struct receive_record_arg
*rrd
;
1777 /* A record that has had its header read in, but not its payload. */
1778 struct receive_record_arg
*next_rrd
;
1780 zio_cksum_t prev_cksum
;
1783 /* Sorted list of objects not to issue prefetches for. */
1784 struct objlist ignore_objlist
;
1787 typedef struct guid_map_entry
{
1789 dsl_dataset_t
*gme_ds
;
1794 guid_compare(const void *arg1
, const void *arg2
)
1796 const guid_map_entry_t
*gmep1
= (const guid_map_entry_t
*)arg1
;
1797 const guid_map_entry_t
*gmep2
= (const guid_map_entry_t
*)arg2
;
1799 return (AVL_CMP(gmep1
->guid
, gmep2
->guid
));
1803 free_guid_map_onexit(void *arg
)
1805 avl_tree_t
*ca
= arg
;
1806 void *cookie
= NULL
;
1807 guid_map_entry_t
*gmep
;
1809 while ((gmep
= avl_destroy_nodes(ca
, &cookie
)) != NULL
) {
1810 dsl_dataset_long_rele(gmep
->gme_ds
, gmep
);
1811 dsl_dataset_rele(gmep
->gme_ds
, gmep
);
1812 kmem_free(gmep
, sizeof (guid_map_entry_t
));
1815 kmem_free(ca
, sizeof (avl_tree_t
));
1819 receive_read(struct receive_arg
*ra
, int len
, void *buf
)
1824 * The code doesn't rely on this (lengths being multiples of 8). See
1825 * comment in dump_bytes.
1829 while (done
< len
) {
1832 ra
->err
= vn_rdwr(UIO_READ
, ra
->vp
,
1833 (char *)buf
+ done
, len
- done
,
1834 ra
->voff
, UIO_SYSSPACE
, FAPPEND
,
1835 RLIM64_INFINITY
, CRED(), &resid
);
1837 if (resid
== len
- done
) {
1839 * Note: ECKSUM indicates that the receive
1840 * was interrupted and can potentially be resumed.
1842 ra
->err
= SET_ERROR(ECKSUM
);
1844 ra
->voff
+= len
- done
- resid
;
1850 ra
->bytes_read
+= len
;
1852 ASSERT3U(done
, ==, len
);
1856 noinline
static void
1857 byteswap_record(dmu_replay_record_t
*drr
)
1859 #define DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
1860 #define DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
1861 drr
->drr_type
= BSWAP_32(drr
->drr_type
);
1862 drr
->drr_payloadlen
= BSWAP_32(drr
->drr_payloadlen
);
1864 switch (drr
->drr_type
) {
1866 DO64(drr_begin
.drr_magic
);
1867 DO64(drr_begin
.drr_versioninfo
);
1868 DO64(drr_begin
.drr_creation_time
);
1869 DO32(drr_begin
.drr_type
);
1870 DO32(drr_begin
.drr_flags
);
1871 DO64(drr_begin
.drr_toguid
);
1872 DO64(drr_begin
.drr_fromguid
);
1875 DO64(drr_object
.drr_object
);
1876 DO32(drr_object
.drr_type
);
1877 DO32(drr_object
.drr_bonustype
);
1878 DO32(drr_object
.drr_blksz
);
1879 DO32(drr_object
.drr_bonuslen
);
1880 DO64(drr_object
.drr_toguid
);
1882 case DRR_FREEOBJECTS
:
1883 DO64(drr_freeobjects
.drr_firstobj
);
1884 DO64(drr_freeobjects
.drr_numobjs
);
1885 DO64(drr_freeobjects
.drr_toguid
);
1888 DO64(drr_write
.drr_object
);
1889 DO32(drr_write
.drr_type
);
1890 DO64(drr_write
.drr_offset
);
1891 DO64(drr_write
.drr_length
);
1892 DO64(drr_write
.drr_toguid
);
1893 ZIO_CHECKSUM_BSWAP(&drr
->drr_u
.drr_write
.drr_key
.ddk_cksum
);
1894 DO64(drr_write
.drr_key
.ddk_prop
);
1896 case DRR_WRITE_BYREF
:
1897 DO64(drr_write_byref
.drr_object
);
1898 DO64(drr_write_byref
.drr_offset
);
1899 DO64(drr_write_byref
.drr_length
);
1900 DO64(drr_write_byref
.drr_toguid
);
1901 DO64(drr_write_byref
.drr_refguid
);
1902 DO64(drr_write_byref
.drr_refobject
);
1903 DO64(drr_write_byref
.drr_refoffset
);
1904 ZIO_CHECKSUM_BSWAP(&drr
->drr_u
.drr_write_byref
.
1906 DO64(drr_write_byref
.drr_key
.ddk_prop
);
1908 case DRR_WRITE_EMBEDDED
:
1909 DO64(drr_write_embedded
.drr_object
);
1910 DO64(drr_write_embedded
.drr_offset
);
1911 DO64(drr_write_embedded
.drr_length
);
1912 DO64(drr_write_embedded
.drr_toguid
);
1913 DO32(drr_write_embedded
.drr_lsize
);
1914 DO32(drr_write_embedded
.drr_psize
);
1917 DO64(drr_free
.drr_object
);
1918 DO64(drr_free
.drr_offset
);
1919 DO64(drr_free
.drr_length
);
1920 DO64(drr_free
.drr_toguid
);
1923 DO64(drr_spill
.drr_object
);
1924 DO64(drr_spill
.drr_length
);
1925 DO64(drr_spill
.drr_toguid
);
1928 DO64(drr_end
.drr_toguid
);
1929 ZIO_CHECKSUM_BSWAP(&drr
->drr_u
.drr_end
.drr_checksum
);
1935 if (drr
->drr_type
!= DRR_BEGIN
) {
1936 ZIO_CHECKSUM_BSWAP(&drr
->drr_u
.drr_checksum
.drr_checksum
);
1943 static inline uint8_t
1944 deduce_nblkptr(dmu_object_type_t bonus_type
, uint64_t bonus_size
)
1946 if (bonus_type
== DMU_OT_SA
) {
1950 ((DN_OLD_MAX_BONUSLEN
-
1951 MIN(DN_OLD_MAX_BONUSLEN
, bonus_size
)) >> SPA_BLKPTRSHIFT
));
1956 save_resume_state(struct receive_writer_arg
*rwa
,
1957 uint64_t object
, uint64_t offset
, dmu_tx_t
*tx
)
1959 int txgoff
= dmu_tx_get_txg(tx
) & TXG_MASK
;
1961 if (!rwa
->resumable
)
1965 * We use ds_resume_bytes[] != 0 to indicate that we need to
1966 * update this on disk, so it must not be 0.
1968 ASSERT(rwa
->bytes_read
!= 0);
1971 * We only resume from write records, which have a valid
1972 * (non-meta-dnode) object number.
1974 ASSERT(object
!= 0);
1977 * For resuming to work correctly, we must receive records in order,
1978 * sorted by object,offset. This is checked by the callers, but
1979 * assert it here for good measure.
1981 ASSERT3U(object
, >=, rwa
->os
->os_dsl_dataset
->ds_resume_object
[txgoff
]);
1982 ASSERT(object
!= rwa
->os
->os_dsl_dataset
->ds_resume_object
[txgoff
] ||
1983 offset
>= rwa
->os
->os_dsl_dataset
->ds_resume_offset
[txgoff
]);
1984 ASSERT3U(rwa
->bytes_read
, >=,
1985 rwa
->os
->os_dsl_dataset
->ds_resume_bytes
[txgoff
]);
1987 rwa
->os
->os_dsl_dataset
->ds_resume_object
[txgoff
] = object
;
1988 rwa
->os
->os_dsl_dataset
->ds_resume_offset
[txgoff
] = offset
;
1989 rwa
->os
->os_dsl_dataset
->ds_resume_bytes
[txgoff
] = rwa
->bytes_read
;
1993 receive_object(struct receive_writer_arg
*rwa
, struct drr_object
*drro
,
1996 dmu_object_info_t doi
;
2001 if (drro
->drr_type
== DMU_OT_NONE
||
2002 !DMU_OT_IS_VALID(drro
->drr_type
) ||
2003 !DMU_OT_IS_VALID(drro
->drr_bonustype
) ||
2004 drro
->drr_checksumtype
>= ZIO_CHECKSUM_FUNCTIONS
||
2005 drro
->drr_compress
>= ZIO_COMPRESS_FUNCTIONS
||
2006 P2PHASE(drro
->drr_blksz
, SPA_MINBLOCKSIZE
) ||
2007 drro
->drr_blksz
< SPA_MINBLOCKSIZE
||
2008 drro
->drr_blksz
> spa_maxblocksize(dmu_objset_spa(rwa
->os
)) ||
2009 drro
->drr_bonuslen
>
2010 DN_BONUS_SIZE(spa_maxdnodesize(dmu_objset_spa(rwa
->os
)))) {
2011 return (SET_ERROR(EINVAL
));
2014 err
= dmu_object_info(rwa
->os
, drro
->drr_object
, &doi
);
2016 if (err
!= 0 && err
!= ENOENT
)
2017 return (SET_ERROR(EINVAL
));
2018 object
= err
== 0 ? drro
->drr_object
: DMU_NEW_OBJECT
;
2021 * If we are losing blkptrs or changing the block size this must
2022 * be a new file instance. We must clear out the previous file
2023 * contents before we can change this type of metadata in the dnode.
2028 nblkptr
= deduce_nblkptr(drro
->drr_bonustype
,
2029 drro
->drr_bonuslen
);
2031 if (drro
->drr_blksz
!= doi
.doi_data_block_size
||
2032 nblkptr
< doi
.doi_nblkptr
) {
2033 err
= dmu_free_long_range(rwa
->os
, drro
->drr_object
,
2036 return (SET_ERROR(EINVAL
));
2040 tx
= dmu_tx_create(rwa
->os
);
2041 dmu_tx_hold_bonus(tx
, object
);
2042 err
= dmu_tx_assign(tx
, TXG_WAIT
);
2048 if (object
== DMU_NEW_OBJECT
) {
2049 /* currently free, want to be allocated */
2050 err
= dmu_object_claim_dnsize(rwa
->os
, drro
->drr_object
,
2051 drro
->drr_type
, drro
->drr_blksz
,
2052 drro
->drr_bonustype
, drro
->drr_bonuslen
,
2053 drro
->drr_dn_slots
<< DNODE_SHIFT
, tx
);
2054 } else if (drro
->drr_type
!= doi
.doi_type
||
2055 drro
->drr_blksz
!= doi
.doi_data_block_size
||
2056 drro
->drr_bonustype
!= doi
.doi_bonus_type
||
2057 drro
->drr_bonuslen
!= doi
.doi_bonus_size
) {
2058 /* currently allocated, but with different properties */
2059 err
= dmu_object_reclaim(rwa
->os
, drro
->drr_object
,
2060 drro
->drr_type
, drro
->drr_blksz
,
2061 drro
->drr_bonustype
, drro
->drr_bonuslen
, tx
);
2065 return (SET_ERROR(EINVAL
));
2068 dmu_object_set_checksum(rwa
->os
, drro
->drr_object
,
2069 drro
->drr_checksumtype
, tx
);
2070 dmu_object_set_compress(rwa
->os
, drro
->drr_object
,
2071 drro
->drr_compress
, tx
);
2076 VERIFY0(dmu_bonus_hold(rwa
->os
, drro
->drr_object
, FTAG
, &db
));
2077 dmu_buf_will_dirty(db
, tx
);
2079 ASSERT3U(db
->db_size
, >=, drro
->drr_bonuslen
);
2080 bcopy(data
, db
->db_data
, drro
->drr_bonuslen
);
2081 if (rwa
->byteswap
) {
2082 dmu_object_byteswap_t byteswap
=
2083 DMU_OT_BYTESWAP(drro
->drr_bonustype
);
2084 dmu_ot_byteswap
[byteswap
].ob_func(db
->db_data
,
2085 drro
->drr_bonuslen
);
2087 dmu_buf_rele(db
, FTAG
);
2096 receive_freeobjects(struct receive_writer_arg
*rwa
,
2097 struct drr_freeobjects
*drrfo
)
2102 if (drrfo
->drr_firstobj
+ drrfo
->drr_numobjs
< drrfo
->drr_firstobj
)
2103 return (SET_ERROR(EINVAL
));
2105 for (obj
= drrfo
->drr_firstobj
== 0 ? 1 : drrfo
->drr_firstobj
;
2106 obj
< drrfo
->drr_firstobj
+ drrfo
->drr_numobjs
&& next_err
== 0;
2107 next_err
= dmu_object_next(rwa
->os
, &obj
, FALSE
, 0)) {
2108 dmu_object_info_t doi
;
2111 err
= dmu_object_info(rwa
->os
, obj
, &doi
);
2112 if (err
== ENOENT
) {
2115 } else if (err
!= 0) {
2119 err
= dmu_free_long_object(rwa
->os
, obj
);
2123 if (next_err
!= ESRCH
)
2129 receive_write(struct receive_writer_arg
*rwa
, struct drr_write
*drrw
,
2136 if (drrw
->drr_offset
+ drrw
->drr_length
< drrw
->drr_offset
||
2137 !DMU_OT_IS_VALID(drrw
->drr_type
))
2138 return (SET_ERROR(EINVAL
));
2141 * For resuming to work, records must be in increasing order
2142 * by (object, offset).
2144 if (drrw
->drr_object
< rwa
->last_object
||
2145 (drrw
->drr_object
== rwa
->last_object
&&
2146 drrw
->drr_offset
< rwa
->last_offset
)) {
2147 return (SET_ERROR(EINVAL
));
2149 rwa
->last_object
= drrw
->drr_object
;
2150 rwa
->last_offset
= drrw
->drr_offset
;
2152 if (dmu_object_info(rwa
->os
, drrw
->drr_object
, NULL
) != 0)
2153 return (SET_ERROR(EINVAL
));
2155 tx
= dmu_tx_create(rwa
->os
);
2157 dmu_tx_hold_write(tx
, drrw
->drr_object
,
2158 drrw
->drr_offset
, drrw
->drr_length
);
2159 err
= dmu_tx_assign(tx
, TXG_WAIT
);
2164 if (rwa
->byteswap
) {
2165 dmu_object_byteswap_t byteswap
=
2166 DMU_OT_BYTESWAP(drrw
->drr_type
);
2167 dmu_ot_byteswap
[byteswap
].ob_func(abuf
->b_data
,
2171 if (dmu_bonus_hold(rwa
->os
, drrw
->drr_object
, FTAG
, &bonus
) != 0)
2172 return (SET_ERROR(EINVAL
));
2173 dmu_assign_arcbuf(bonus
, drrw
->drr_offset
, abuf
, tx
);
2176 * Note: If the receive fails, we want the resume stream to start
2177 * with the same record that we last successfully received (as opposed
2178 * to the next record), so that we can verify that we are
2179 * resuming from the correct location.
2181 save_resume_state(rwa
, drrw
->drr_object
, drrw
->drr_offset
, tx
);
2183 dmu_buf_rele(bonus
, FTAG
);
2189 * Handle a DRR_WRITE_BYREF record. This record is used in dedup'ed
2190 * streams to refer to a copy of the data that is already on the
2191 * system because it came in earlier in the stream. This function
2192 * finds the earlier copy of the data, and uses that copy instead of
2193 * data from the stream to fulfill this write.
2196 receive_write_byref(struct receive_writer_arg
*rwa
,
2197 struct drr_write_byref
*drrwbr
)
2201 guid_map_entry_t gmesrch
;
2202 guid_map_entry_t
*gmep
;
2204 objset_t
*ref_os
= NULL
;
2207 if (drrwbr
->drr_offset
+ drrwbr
->drr_length
< drrwbr
->drr_offset
)
2208 return (SET_ERROR(EINVAL
));
2211 * If the GUID of the referenced dataset is different from the
2212 * GUID of the target dataset, find the referenced dataset.
2214 if (drrwbr
->drr_toguid
!= drrwbr
->drr_refguid
) {
2215 gmesrch
.guid
= drrwbr
->drr_refguid
;
2216 if ((gmep
= avl_find(rwa
->guid_to_ds_map
, &gmesrch
,
2218 return (SET_ERROR(EINVAL
));
2220 if (dmu_objset_from_ds(gmep
->gme_ds
, &ref_os
))
2221 return (SET_ERROR(EINVAL
));
2226 err
= dmu_buf_hold(ref_os
, drrwbr
->drr_refobject
,
2227 drrwbr
->drr_refoffset
, FTAG
, &dbp
, DMU_READ_PREFETCH
);
2231 tx
= dmu_tx_create(rwa
->os
);
2233 dmu_tx_hold_write(tx
, drrwbr
->drr_object
,
2234 drrwbr
->drr_offset
, drrwbr
->drr_length
);
2235 err
= dmu_tx_assign(tx
, TXG_WAIT
);
2240 dmu_write(rwa
->os
, drrwbr
->drr_object
,
2241 drrwbr
->drr_offset
, drrwbr
->drr_length
, dbp
->db_data
, tx
);
2242 dmu_buf_rele(dbp
, FTAG
);
2244 /* See comment in restore_write. */
2245 save_resume_state(rwa
, drrwbr
->drr_object
, drrwbr
->drr_offset
, tx
);
2251 receive_write_embedded(struct receive_writer_arg
*rwa
,
2252 struct drr_write_embedded
*drrwe
, void *data
)
2257 if (drrwe
->drr_offset
+ drrwe
->drr_length
< drrwe
->drr_offset
)
2260 if (drrwe
->drr_psize
> BPE_PAYLOAD_SIZE
)
2263 if (drrwe
->drr_etype
>= NUM_BP_EMBEDDED_TYPES
)
2265 if (drrwe
->drr_compression
>= ZIO_COMPRESS_FUNCTIONS
)
2268 tx
= dmu_tx_create(rwa
->os
);
2270 dmu_tx_hold_write(tx
, drrwe
->drr_object
,
2271 drrwe
->drr_offset
, drrwe
->drr_length
);
2272 err
= dmu_tx_assign(tx
, TXG_WAIT
);
2278 dmu_write_embedded(rwa
->os
, drrwe
->drr_object
,
2279 drrwe
->drr_offset
, data
, drrwe
->drr_etype
,
2280 drrwe
->drr_compression
, drrwe
->drr_lsize
, drrwe
->drr_psize
,
2281 rwa
->byteswap
^ ZFS_HOST_BYTEORDER
, tx
);
2283 /* See comment in restore_write. */
2284 save_resume_state(rwa
, drrwe
->drr_object
, drrwe
->drr_offset
, tx
);
2290 receive_spill(struct receive_writer_arg
*rwa
, struct drr_spill
*drrs
,
2294 dmu_buf_t
*db
, *db_spill
;
2297 if (drrs
->drr_length
< SPA_MINBLOCKSIZE
||
2298 drrs
->drr_length
> spa_maxblocksize(dmu_objset_spa(rwa
->os
)))
2299 return (SET_ERROR(EINVAL
));
2301 if (dmu_object_info(rwa
->os
, drrs
->drr_object
, NULL
) != 0)
2302 return (SET_ERROR(EINVAL
));
2304 VERIFY0(dmu_bonus_hold(rwa
->os
, drrs
->drr_object
, FTAG
, &db
));
2305 if ((err
= dmu_spill_hold_by_bonus(db
, FTAG
, &db_spill
)) != 0) {
2306 dmu_buf_rele(db
, FTAG
);
2310 tx
= dmu_tx_create(rwa
->os
);
2312 dmu_tx_hold_spill(tx
, db
->db_object
);
2314 err
= dmu_tx_assign(tx
, TXG_WAIT
);
2316 dmu_buf_rele(db
, FTAG
);
2317 dmu_buf_rele(db_spill
, FTAG
);
2321 dmu_buf_will_dirty(db_spill
, tx
);
2323 if (db_spill
->db_size
< drrs
->drr_length
)
2324 VERIFY(0 == dbuf_spill_set_blksz(db_spill
,
2325 drrs
->drr_length
, tx
));
2326 bcopy(data
, db_spill
->db_data
, drrs
->drr_length
);
2328 dmu_buf_rele(db
, FTAG
);
2329 dmu_buf_rele(db_spill
, FTAG
);
2337 receive_free(struct receive_writer_arg
*rwa
, struct drr_free
*drrf
)
2341 if (drrf
->drr_length
!= -1ULL &&
2342 drrf
->drr_offset
+ drrf
->drr_length
< drrf
->drr_offset
)
2343 return (SET_ERROR(EINVAL
));
2345 if (dmu_object_info(rwa
->os
, drrf
->drr_object
, NULL
) != 0)
2346 return (SET_ERROR(EINVAL
));
2348 err
= dmu_free_long_range(rwa
->os
, drrf
->drr_object
,
2349 drrf
->drr_offset
, drrf
->drr_length
);
2354 /* used to destroy the drc_ds on error */
2356 dmu_recv_cleanup_ds(dmu_recv_cookie_t
*drc
)
2358 if (drc
->drc_resumable
) {
2359 /* wait for our resume state to be written to disk */
2360 txg_wait_synced(drc
->drc_ds
->ds_dir
->dd_pool
, 0);
2361 dsl_dataset_disown(drc
->drc_ds
, dmu_recv_tag
);
2363 char name
[ZFS_MAX_DATASET_NAME_LEN
];
2364 dsl_dataset_name(drc
->drc_ds
, name
);
2365 dsl_dataset_disown(drc
->drc_ds
, dmu_recv_tag
);
2366 (void) dsl_destroy_head(name
);
2371 receive_cksum(struct receive_arg
*ra
, int len
, void *buf
)
2374 fletcher_4_incremental_byteswap(buf
, len
, &ra
->cksum
);
2376 fletcher_4_incremental_native(buf
, len
, &ra
->cksum
);
2381 * Read the payload into a buffer of size len, and update the current record's
2383 * Allocate ra->next_rrd and read the next record's header into
2384 * ra->next_rrd->header.
2385 * Verify checksum of payload and next record.
2388 receive_read_payload_and_next_header(struct receive_arg
*ra
, int len
, void *buf
)
2391 zio_cksum_t cksum_orig
;
2392 zio_cksum_t
*cksump
;
2395 ASSERT3U(len
, <=, SPA_MAXBLOCKSIZE
);
2396 err
= receive_read(ra
, len
, buf
);
2399 receive_cksum(ra
, len
, buf
);
2401 /* note: rrd is NULL when reading the begin record's payload */
2402 if (ra
->rrd
!= NULL
) {
2403 ra
->rrd
->payload
= buf
;
2404 ra
->rrd
->payload_size
= len
;
2405 ra
->rrd
->bytes_read
= ra
->bytes_read
;
2409 ra
->prev_cksum
= ra
->cksum
;
2411 ra
->next_rrd
= kmem_zalloc(sizeof (*ra
->next_rrd
), KM_SLEEP
);
2412 err
= receive_read(ra
, sizeof (ra
->next_rrd
->header
),
2413 &ra
->next_rrd
->header
);
2414 ra
->next_rrd
->bytes_read
= ra
->bytes_read
;
2416 kmem_free(ra
->next_rrd
, sizeof (*ra
->next_rrd
));
2417 ra
->next_rrd
= NULL
;
2420 if (ra
->next_rrd
->header
.drr_type
== DRR_BEGIN
) {
2421 kmem_free(ra
->next_rrd
, sizeof (*ra
->next_rrd
));
2422 ra
->next_rrd
= NULL
;
2423 return (SET_ERROR(EINVAL
));
2427 * Note: checksum is of everything up to but not including the
2430 ASSERT3U(offsetof(dmu_replay_record_t
, drr_u
.drr_checksum
.drr_checksum
),
2431 ==, sizeof (dmu_replay_record_t
) - sizeof (zio_cksum_t
));
2433 offsetof(dmu_replay_record_t
, drr_u
.drr_checksum
.drr_checksum
),
2434 &ra
->next_rrd
->header
);
2436 cksum_orig
= ra
->next_rrd
->header
.drr_u
.drr_checksum
.drr_checksum
;
2437 cksump
= &ra
->next_rrd
->header
.drr_u
.drr_checksum
.drr_checksum
;
2440 byteswap_record(&ra
->next_rrd
->header
);
2442 if ((!ZIO_CHECKSUM_IS_ZERO(cksump
)) &&
2443 !ZIO_CHECKSUM_EQUAL(ra
->cksum
, *cksump
)) {
2444 kmem_free(ra
->next_rrd
, sizeof (*ra
->next_rrd
));
2445 ra
->next_rrd
= NULL
;
2446 return (SET_ERROR(ECKSUM
));
2449 receive_cksum(ra
, sizeof (cksum_orig
), &cksum_orig
);
2455 objlist_create(struct objlist
*list
)
2457 list_create(&list
->list
, sizeof (struct receive_objnode
),
2458 offsetof(struct receive_objnode
, node
));
2459 list
->last_lookup
= 0;
2463 objlist_destroy(struct objlist
*list
)
2465 struct receive_objnode
*n
;
2467 for (n
= list_remove_head(&list
->list
);
2468 n
!= NULL
; n
= list_remove_head(&list
->list
)) {
2469 kmem_free(n
, sizeof (*n
));
2471 list_destroy(&list
->list
);
2475 * This function looks through the objlist to see if the specified object number
2476 * is contained in the objlist. In the process, it will remove all object
2477 * numbers in the list that are smaller than the specified object number. Thus,
2478 * any lookup of an object number smaller than a previously looked up object
2479 * number will always return false; therefore, all lookups should be done in
2483 objlist_exists(struct objlist
*list
, uint64_t object
)
2485 struct receive_objnode
*node
= list_head(&list
->list
);
2486 ASSERT3U(object
, >=, list
->last_lookup
);
2487 list
->last_lookup
= object
;
2488 while (node
!= NULL
&& node
->object
< object
) {
2489 VERIFY3P(node
, ==, list_remove_head(&list
->list
));
2490 kmem_free(node
, sizeof (*node
));
2491 node
= list_head(&list
->list
);
2493 return (node
!= NULL
&& node
->object
== object
);
2497 * The objlist is a list of object numbers stored in ascending order. However,
2498 * the insertion of new object numbers does not seek out the correct location to
2499 * store a new object number; instead, it appends it to the list for simplicity.
2500 * Thus, any users must take care to only insert new object numbers in ascending
2504 objlist_insert(struct objlist
*list
, uint64_t object
)
2506 struct receive_objnode
*node
= kmem_zalloc(sizeof (*node
), KM_SLEEP
);
2507 node
->object
= object
;
2510 struct receive_objnode
*last_object
= list_tail(&list
->list
);
2511 uint64_t last_objnum
= (last_object
!= NULL
? last_object
->object
: 0);
2512 ASSERT3U(node
->object
, >, last_objnum
);
2515 list_insert_tail(&list
->list
, node
);
2519 * Issue the prefetch reads for any necessary indirect blocks.
2521 * We use the object ignore list to tell us whether or not to issue prefetches
2522 * for a given object. We do this for both correctness (in case the blocksize
2523 * of an object has changed) and performance (if the object doesn't exist, don't
2524 * needlessly try to issue prefetches). We also trim the list as we go through
2525 * the stream to prevent it from growing to an unbounded size.
2527 * The object numbers within will always be in sorted order, and any write
2528 * records we see will also be in sorted order, but they're not sorted with
2529 * respect to each other (i.e. we can get several object records before
2530 * receiving each object's write records). As a result, once we've reached a
2531 * given object number, we can safely remove any reference to lower object
2532 * numbers in the ignore list. In practice, we receive up to 32 object records
2533 * before receiving write records, so the list can have up to 32 nodes in it.
2537 receive_read_prefetch(struct receive_arg
*ra
,
2538 uint64_t object
, uint64_t offset
, uint64_t length
)
2540 if (!objlist_exists(&ra
->ignore_objlist
, object
)) {
2541 dmu_prefetch(ra
->os
, object
, 1, offset
, length
,
2542 ZIO_PRIORITY_SYNC_READ
);
2547 * Read records off the stream, issuing any necessary prefetches.
2550 receive_read_record(struct receive_arg
*ra
)
2554 switch (ra
->rrd
->header
.drr_type
) {
2557 struct drr_object
*drro
= &ra
->rrd
->header
.drr_u
.drr_object
;
2558 uint32_t size
= P2ROUNDUP(drro
->drr_bonuslen
, 8);
2559 void *buf
= kmem_zalloc(size
, KM_SLEEP
);
2560 dmu_object_info_t doi
;
2561 err
= receive_read_payload_and_next_header(ra
, size
, buf
);
2563 kmem_free(buf
, size
);
2566 err
= dmu_object_info(ra
->os
, drro
->drr_object
, &doi
);
2568 * See receive_read_prefetch for an explanation why we're
2569 * storing this object in the ignore_obj_list.
2571 if (err
== ENOENT
||
2572 (err
== 0 && doi
.doi_data_block_size
!= drro
->drr_blksz
)) {
2573 objlist_insert(&ra
->ignore_objlist
, drro
->drr_object
);
2578 case DRR_FREEOBJECTS
:
2580 err
= receive_read_payload_and_next_header(ra
, 0, NULL
);
2585 struct drr_write
*drrw
= &ra
->rrd
->header
.drr_u
.drr_write
;
2586 arc_buf_t
*abuf
= arc_loan_buf(dmu_objset_spa(ra
->os
),
2589 err
= receive_read_payload_and_next_header(ra
,
2590 drrw
->drr_length
, abuf
->b_data
);
2592 dmu_return_arcbuf(abuf
);
2595 ra
->rrd
->write_buf
= abuf
;
2596 receive_read_prefetch(ra
, drrw
->drr_object
, drrw
->drr_offset
,
2600 case DRR_WRITE_BYREF
:
2602 struct drr_write_byref
*drrwb
=
2603 &ra
->rrd
->header
.drr_u
.drr_write_byref
;
2604 err
= receive_read_payload_and_next_header(ra
, 0, NULL
);
2605 receive_read_prefetch(ra
, drrwb
->drr_object
, drrwb
->drr_offset
,
2609 case DRR_WRITE_EMBEDDED
:
2611 struct drr_write_embedded
*drrwe
=
2612 &ra
->rrd
->header
.drr_u
.drr_write_embedded
;
2613 uint32_t size
= P2ROUNDUP(drrwe
->drr_psize
, 8);
2614 void *buf
= kmem_zalloc(size
, KM_SLEEP
);
2616 err
= receive_read_payload_and_next_header(ra
, size
, buf
);
2618 kmem_free(buf
, size
);
2622 receive_read_prefetch(ra
, drrwe
->drr_object
, drrwe
->drr_offset
,
2629 * It might be beneficial to prefetch indirect blocks here, but
2630 * we don't really have the data to decide for sure.
2632 err
= receive_read_payload_and_next_header(ra
, 0, NULL
);
2637 struct drr_end
*drre
= &ra
->rrd
->header
.drr_u
.drr_end
;
2638 if (!ZIO_CHECKSUM_EQUAL(ra
->prev_cksum
, drre
->drr_checksum
))
2639 return (SET_ERROR(ECKSUM
));
2644 struct drr_spill
*drrs
= &ra
->rrd
->header
.drr_u
.drr_spill
;
2645 void *buf
= kmem_zalloc(drrs
->drr_length
, KM_SLEEP
);
2646 err
= receive_read_payload_and_next_header(ra
, drrs
->drr_length
,
2649 kmem_free(buf
, drrs
->drr_length
);
2653 return (SET_ERROR(EINVAL
));
2658 * Commit the records to the pool.
2661 receive_process_record(struct receive_writer_arg
*rwa
,
2662 struct receive_record_arg
*rrd
)
2666 /* Processing in order, therefore bytes_read should be increasing. */
2667 ASSERT3U(rrd
->bytes_read
, >=, rwa
->bytes_read
);
2668 rwa
->bytes_read
= rrd
->bytes_read
;
2670 switch (rrd
->header
.drr_type
) {
2673 struct drr_object
*drro
= &rrd
->header
.drr_u
.drr_object
;
2674 err
= receive_object(rwa
, drro
, rrd
->payload
);
2675 kmem_free(rrd
->payload
, rrd
->payload_size
);
2676 rrd
->payload
= NULL
;
2679 case DRR_FREEOBJECTS
:
2681 struct drr_freeobjects
*drrfo
=
2682 &rrd
->header
.drr_u
.drr_freeobjects
;
2683 return (receive_freeobjects(rwa
, drrfo
));
2687 struct drr_write
*drrw
= &rrd
->header
.drr_u
.drr_write
;
2688 err
= receive_write(rwa
, drrw
, rrd
->write_buf
);
2689 /* if receive_write() is successful, it consumes the arc_buf */
2691 dmu_return_arcbuf(rrd
->write_buf
);
2692 rrd
->write_buf
= NULL
;
2693 rrd
->payload
= NULL
;
2696 case DRR_WRITE_BYREF
:
2698 struct drr_write_byref
*drrwbr
=
2699 &rrd
->header
.drr_u
.drr_write_byref
;
2700 return (receive_write_byref(rwa
, drrwbr
));
2702 case DRR_WRITE_EMBEDDED
:
2704 struct drr_write_embedded
*drrwe
=
2705 &rrd
->header
.drr_u
.drr_write_embedded
;
2706 err
= receive_write_embedded(rwa
, drrwe
, rrd
->payload
);
2707 kmem_free(rrd
->payload
, rrd
->payload_size
);
2708 rrd
->payload
= NULL
;
2713 struct drr_free
*drrf
= &rrd
->header
.drr_u
.drr_free
;
2714 return (receive_free(rwa
, drrf
));
2718 struct drr_spill
*drrs
= &rrd
->header
.drr_u
.drr_spill
;
2719 err
= receive_spill(rwa
, drrs
, rrd
->payload
);
2720 kmem_free(rrd
->payload
, rrd
->payload_size
);
2721 rrd
->payload
= NULL
;
2725 return (SET_ERROR(EINVAL
));
2730 * dmu_recv_stream's worker thread; pull records off the queue, and then call
2731 * receive_process_record When we're done, signal the main thread and exit.
2734 receive_writer_thread(void *arg
)
2736 struct receive_writer_arg
*rwa
= arg
;
2737 struct receive_record_arg
*rrd
;
2738 fstrans_cookie_t cookie
= spl_fstrans_mark();
2740 for (rrd
= bqueue_dequeue(&rwa
->q
); !rrd
->eos_marker
;
2741 rrd
= bqueue_dequeue(&rwa
->q
)) {
2743 * If there's an error, the main thread will stop putting things
2744 * on the queue, but we need to clear everything in it before we
2747 if (rwa
->err
== 0) {
2748 rwa
->err
= receive_process_record(rwa
, rrd
);
2749 } else if (rrd
->write_buf
!= NULL
) {
2750 dmu_return_arcbuf(rrd
->write_buf
);
2751 rrd
->write_buf
= NULL
;
2752 rrd
->payload
= NULL
;
2753 } else if (rrd
->payload
!= NULL
) {
2754 kmem_free(rrd
->payload
, rrd
->payload_size
);
2755 rrd
->payload
= NULL
;
2757 kmem_free(rrd
, sizeof (*rrd
));
2759 kmem_free(rrd
, sizeof (*rrd
));
2760 mutex_enter(&rwa
->mutex
);
2762 cv_signal(&rwa
->cv
);
2763 mutex_exit(&rwa
->mutex
);
2764 spl_fstrans_unmark(cookie
);
2768 resume_check(struct receive_arg
*ra
, nvlist_t
*begin_nvl
)
2771 objset_t
*mos
= dmu_objset_pool(ra
->os
)->dp_meta_objset
;
2772 uint64_t dsobj
= dmu_objset_id(ra
->os
);
2773 uint64_t resume_obj
, resume_off
;
2775 if (nvlist_lookup_uint64(begin_nvl
,
2776 "resume_object", &resume_obj
) != 0 ||
2777 nvlist_lookup_uint64(begin_nvl
,
2778 "resume_offset", &resume_off
) != 0) {
2779 return (SET_ERROR(EINVAL
));
2781 VERIFY0(zap_lookup(mos
, dsobj
,
2782 DS_FIELD_RESUME_OBJECT
, sizeof (val
), 1, &val
));
2783 if (resume_obj
!= val
)
2784 return (SET_ERROR(EINVAL
));
2785 VERIFY0(zap_lookup(mos
, dsobj
,
2786 DS_FIELD_RESUME_OFFSET
, sizeof (val
), 1, &val
));
2787 if (resume_off
!= val
)
2788 return (SET_ERROR(EINVAL
));
2794 * Read in the stream's records, one by one, and apply them to the pool. There
2795 * are two threads involved; the thread that calls this function will spin up a
2796 * worker thread, read the records off the stream one by one, and issue
2797 * prefetches for any necessary indirect blocks. It will then push the records
2798 * onto an internal blocking queue. The worker thread will pull the records off
2799 * the queue, and actually write the data into the DMU. This way, the worker
2800 * thread doesn't have to wait for reads to complete, since everything it needs
2801 * (the indirect blocks) will be prefetched.
2803 * NB: callers *must* call dmu_recv_end() if this succeeds.
2806 dmu_recv_stream(dmu_recv_cookie_t
*drc
, vnode_t
*vp
, offset_t
*voffp
,
2807 int cleanup_fd
, uint64_t *action_handlep
)
2810 struct receive_arg
*ra
;
2811 struct receive_writer_arg
*rwa
;
2813 uint32_t payloadlen
;
2815 nvlist_t
*begin_nvl
= NULL
;
2817 ra
= kmem_zalloc(sizeof (*ra
), KM_SLEEP
);
2818 rwa
= kmem_zalloc(sizeof (*rwa
), KM_SLEEP
);
2820 ra
->byteswap
= drc
->drc_byteswap
;
2821 ra
->cksum
= drc
->drc_cksum
;
2825 if (dsl_dataset_is_zapified(drc
->drc_ds
)) {
2826 (void) zap_lookup(drc
->drc_ds
->ds_dir
->dd_pool
->dp_meta_objset
,
2827 drc
->drc_ds
->ds_object
, DS_FIELD_RESUME_BYTES
,
2828 sizeof (ra
->bytes_read
), 1, &ra
->bytes_read
);
2831 objlist_create(&ra
->ignore_objlist
);
2833 /* these were verified in dmu_recv_begin */
2834 ASSERT3U(DMU_GET_STREAM_HDRTYPE(drc
->drc_drrb
->drr_versioninfo
), ==,
2836 ASSERT3U(drc
->drc_drrb
->drr_type
, <, DMU_OST_NUMTYPES
);
2839 * Open the objset we are modifying.
2841 VERIFY0(dmu_objset_from_ds(drc
->drc_ds
, &ra
->os
));
2843 ASSERT(dsl_dataset_phys(drc
->drc_ds
)->ds_flags
& DS_FLAG_INCONSISTENT
);
2845 featureflags
= DMU_GET_FEATUREFLAGS(drc
->drc_drrb
->drr_versioninfo
);
2847 /* if this stream is dedup'ed, set up the avl tree for guid mapping */
2848 if (featureflags
& DMU_BACKUP_FEATURE_DEDUP
) {
2851 if (cleanup_fd
== -1) {
2852 ra
->err
= SET_ERROR(EBADF
);
2855 ra
->err
= zfs_onexit_fd_hold(cleanup_fd
, &minor
);
2861 if (*action_handlep
== 0) {
2862 rwa
->guid_to_ds_map
=
2863 kmem_alloc(sizeof (avl_tree_t
), KM_SLEEP
);
2864 avl_create(rwa
->guid_to_ds_map
, guid_compare
,
2865 sizeof (guid_map_entry_t
),
2866 offsetof(guid_map_entry_t
, avlnode
));
2867 err
= zfs_onexit_add_cb(minor
,
2868 free_guid_map_onexit
, rwa
->guid_to_ds_map
,
2873 err
= zfs_onexit_cb_data(minor
, *action_handlep
,
2874 (void **)&rwa
->guid_to_ds_map
);
2879 drc
->drc_guid_to_ds_map
= rwa
->guid_to_ds_map
;
2882 payloadlen
= drc
->drc_drr_begin
->drr_payloadlen
;
2884 if (payloadlen
!= 0)
2885 payload
= kmem_alloc(payloadlen
, KM_SLEEP
);
2887 err
= receive_read_payload_and_next_header(ra
, payloadlen
, payload
);
2889 if (payloadlen
!= 0)
2890 kmem_free(payload
, payloadlen
);
2893 if (payloadlen
!= 0) {
2894 err
= nvlist_unpack(payload
, payloadlen
, &begin_nvl
, KM_SLEEP
);
2895 kmem_free(payload
, payloadlen
);
2900 if (featureflags
& DMU_BACKUP_FEATURE_RESUMING
) {
2901 err
= resume_check(ra
, begin_nvl
);
2906 (void) bqueue_init(&rwa
->q
, zfs_recv_queue_length
,
2907 offsetof(struct receive_record_arg
, node
));
2908 cv_init(&rwa
->cv
, NULL
, CV_DEFAULT
, NULL
);
2909 mutex_init(&rwa
->mutex
, NULL
, MUTEX_DEFAULT
, NULL
);
2911 rwa
->byteswap
= drc
->drc_byteswap
;
2912 rwa
->resumable
= drc
->drc_resumable
;
2914 (void) thread_create(NULL
, 0, receive_writer_thread
, rwa
, 0, curproc
,
2915 TS_RUN
, minclsyspri
);
2917 * We're reading rwa->err without locks, which is safe since we are the
2918 * only reader, and the worker thread is the only writer. It's ok if we
2919 * miss a write for an iteration or two of the loop, since the writer
2920 * thread will keep freeing records we send it until we send it an eos
2923 * We can leave this loop in 3 ways: First, if rwa->err is
2924 * non-zero. In that case, the writer thread will free the rrd we just
2925 * pushed. Second, if we're interrupted; in that case, either it's the
2926 * first loop and ra->rrd was never allocated, or it's later and ra->rrd
2927 * has been handed off to the writer thread who will free it. Finally,
2928 * if receive_read_record fails or we're at the end of the stream, then
2929 * we free ra->rrd and exit.
2931 while (rwa
->err
== 0) {
2932 if (issig(JUSTLOOKING
) && issig(FORREAL
)) {
2933 err
= SET_ERROR(EINTR
);
2937 ASSERT3P(ra
->rrd
, ==, NULL
);
2938 ra
->rrd
= ra
->next_rrd
;
2939 ra
->next_rrd
= NULL
;
2940 /* Allocates and loads header into ra->next_rrd */
2941 err
= receive_read_record(ra
);
2943 if (ra
->rrd
->header
.drr_type
== DRR_END
|| err
!= 0) {
2944 kmem_free(ra
->rrd
, sizeof (*ra
->rrd
));
2949 bqueue_enqueue(&rwa
->q
, ra
->rrd
,
2950 sizeof (struct receive_record_arg
) + ra
->rrd
->payload_size
);
2953 if (ra
->next_rrd
== NULL
)
2954 ra
->next_rrd
= kmem_zalloc(sizeof (*ra
->next_rrd
), KM_SLEEP
);
2955 ra
->next_rrd
->eos_marker
= B_TRUE
;
2956 bqueue_enqueue(&rwa
->q
, ra
->next_rrd
, 1);
2958 mutex_enter(&rwa
->mutex
);
2959 while (!rwa
->done
) {
2960 cv_wait(&rwa
->cv
, &rwa
->mutex
);
2962 mutex_exit(&rwa
->mutex
);
2964 cv_destroy(&rwa
->cv
);
2965 mutex_destroy(&rwa
->mutex
);
2966 bqueue_destroy(&rwa
->q
);
2971 nvlist_free(begin_nvl
);
2972 if ((featureflags
& DMU_BACKUP_FEATURE_DEDUP
) && (cleanup_fd
!= -1))
2973 zfs_onexit_fd_rele(cleanup_fd
);
2977 * Clean up references. If receive is not resumable,
2978 * destroy what we created, so we don't leave it in
2979 * the inconsistent state.
2981 dmu_recv_cleanup_ds(drc
);
2985 objlist_destroy(&ra
->ignore_objlist
);
2986 kmem_free(ra
, sizeof (*ra
));
2987 kmem_free(rwa
, sizeof (*rwa
));
2992 dmu_recv_end_check(void *arg
, dmu_tx_t
*tx
)
2994 dmu_recv_cookie_t
*drc
= arg
;
2995 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
2998 ASSERT3P(drc
->drc_ds
->ds_owner
, ==, dmu_recv_tag
);
3000 if (!drc
->drc_newfs
) {
3001 dsl_dataset_t
*origin_head
;
3003 error
= dsl_dataset_hold(dp
, drc
->drc_tofs
, FTAG
, &origin_head
);
3006 if (drc
->drc_force
) {
3008 * We will destroy any snapshots in tofs (i.e. before
3009 * origin_head) that are after the origin (which is
3010 * the snap before drc_ds, because drc_ds can not
3011 * have any snaps of its own).
3015 obj
= dsl_dataset_phys(origin_head
)->ds_prev_snap_obj
;
3017 dsl_dataset_phys(drc
->drc_ds
)->ds_prev_snap_obj
) {
3018 dsl_dataset_t
*snap
;
3019 error
= dsl_dataset_hold_obj(dp
, obj
, FTAG
,
3023 if (snap
->ds_dir
!= origin_head
->ds_dir
)
3024 error
= SET_ERROR(EINVAL
);
3026 error
= dsl_destroy_snapshot_check_impl(
3029 obj
= dsl_dataset_phys(snap
)->ds_prev_snap_obj
;
3030 dsl_dataset_rele(snap
, FTAG
);
3035 dsl_dataset_rele(origin_head
, FTAG
);
3039 error
= dsl_dataset_clone_swap_check_impl(drc
->drc_ds
,
3040 origin_head
, drc
->drc_force
, drc
->drc_owner
, tx
);
3042 dsl_dataset_rele(origin_head
, FTAG
);
3045 error
= dsl_dataset_snapshot_check_impl(origin_head
,
3046 drc
->drc_tosnap
, tx
, B_TRUE
, 1, drc
->drc_cred
);
3047 dsl_dataset_rele(origin_head
, FTAG
);
3051 error
= dsl_destroy_head_check_impl(drc
->drc_ds
, 1);
3053 error
= dsl_dataset_snapshot_check_impl(drc
->drc_ds
,
3054 drc
->drc_tosnap
, tx
, B_TRUE
, 1, drc
->drc_cred
);
3060 dmu_recv_end_sync(void *arg
, dmu_tx_t
*tx
)
3062 dmu_recv_cookie_t
*drc
= arg
;
3063 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
3065 spa_history_log_internal_ds(drc
->drc_ds
, "finish receiving",
3066 tx
, "snap=%s", drc
->drc_tosnap
);
3068 if (!drc
->drc_newfs
) {
3069 dsl_dataset_t
*origin_head
;
3071 VERIFY0(dsl_dataset_hold(dp
, drc
->drc_tofs
, FTAG
,
3074 if (drc
->drc_force
) {
3076 * Destroy any snapshots of drc_tofs (origin_head)
3077 * after the origin (the snap before drc_ds).
3081 obj
= dsl_dataset_phys(origin_head
)->ds_prev_snap_obj
;
3083 dsl_dataset_phys(drc
->drc_ds
)->ds_prev_snap_obj
) {
3084 dsl_dataset_t
*snap
;
3085 VERIFY0(dsl_dataset_hold_obj(dp
, obj
, FTAG
,
3087 ASSERT3P(snap
->ds_dir
, ==, origin_head
->ds_dir
);
3088 obj
= dsl_dataset_phys(snap
)->ds_prev_snap_obj
;
3089 dsl_destroy_snapshot_sync_impl(snap
,
3091 dsl_dataset_rele(snap
, FTAG
);
3094 VERIFY3P(drc
->drc_ds
->ds_prev
, ==,
3095 origin_head
->ds_prev
);
3097 dsl_dataset_clone_swap_sync_impl(drc
->drc_ds
,
3099 dsl_dataset_snapshot_sync_impl(origin_head
,
3100 drc
->drc_tosnap
, tx
);
3102 /* set snapshot's creation time and guid */
3103 dmu_buf_will_dirty(origin_head
->ds_prev
->ds_dbuf
, tx
);
3104 dsl_dataset_phys(origin_head
->ds_prev
)->ds_creation_time
=
3105 drc
->drc_drrb
->drr_creation_time
;
3106 dsl_dataset_phys(origin_head
->ds_prev
)->ds_guid
=
3107 drc
->drc_drrb
->drr_toguid
;
3108 dsl_dataset_phys(origin_head
->ds_prev
)->ds_flags
&=
3109 ~DS_FLAG_INCONSISTENT
;
3111 dmu_buf_will_dirty(origin_head
->ds_dbuf
, tx
);
3112 dsl_dataset_phys(origin_head
)->ds_flags
&=
3113 ~DS_FLAG_INCONSISTENT
;
3115 dsl_dataset_rele(origin_head
, FTAG
);
3116 dsl_destroy_head_sync_impl(drc
->drc_ds
, tx
);
3118 if (drc
->drc_owner
!= NULL
)
3119 VERIFY3P(origin_head
->ds_owner
, ==, drc
->drc_owner
);
3121 dsl_dataset_t
*ds
= drc
->drc_ds
;
3123 dsl_dataset_snapshot_sync_impl(ds
, drc
->drc_tosnap
, tx
);
3125 /* set snapshot's creation time and guid */
3126 dmu_buf_will_dirty(ds
->ds_prev
->ds_dbuf
, tx
);
3127 dsl_dataset_phys(ds
->ds_prev
)->ds_creation_time
=
3128 drc
->drc_drrb
->drr_creation_time
;
3129 dsl_dataset_phys(ds
->ds_prev
)->ds_guid
=
3130 drc
->drc_drrb
->drr_toguid
;
3131 dsl_dataset_phys(ds
->ds_prev
)->ds_flags
&=
3132 ~DS_FLAG_INCONSISTENT
;
3134 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
3135 dsl_dataset_phys(ds
)->ds_flags
&= ~DS_FLAG_INCONSISTENT
;
3136 if (dsl_dataset_has_resume_receive_state(ds
)) {
3137 (void) zap_remove(dp
->dp_meta_objset
, ds
->ds_object
,
3138 DS_FIELD_RESUME_FROMGUID
, tx
);
3139 (void) zap_remove(dp
->dp_meta_objset
, ds
->ds_object
,
3140 DS_FIELD_RESUME_OBJECT
, tx
);
3141 (void) zap_remove(dp
->dp_meta_objset
, ds
->ds_object
,
3142 DS_FIELD_RESUME_OFFSET
, tx
);
3143 (void) zap_remove(dp
->dp_meta_objset
, ds
->ds_object
,
3144 DS_FIELD_RESUME_BYTES
, tx
);
3145 (void) zap_remove(dp
->dp_meta_objset
, ds
->ds_object
,
3146 DS_FIELD_RESUME_TOGUID
, tx
);
3147 (void) zap_remove(dp
->dp_meta_objset
, ds
->ds_object
,
3148 DS_FIELD_RESUME_TONAME
, tx
);
3151 drc
->drc_newsnapobj
= dsl_dataset_phys(drc
->drc_ds
)->ds_prev_snap_obj
;
3152 zvol_create_minors(dp
->dp_spa
, drc
->drc_tofs
, B_TRUE
);
3154 * Release the hold from dmu_recv_begin. This must be done before
3155 * we return to open context, so that when we free the dataset's dnode,
3156 * we can evict its bonus buffer.
3158 dsl_dataset_disown(drc
->drc_ds
, dmu_recv_tag
);
3163 add_ds_to_guidmap(const char *name
, avl_tree_t
*guid_map
, uint64_t snapobj
)
3166 dsl_dataset_t
*snapds
;
3167 guid_map_entry_t
*gmep
;
3170 ASSERT(guid_map
!= NULL
);
3172 err
= dsl_pool_hold(name
, FTAG
, &dp
);
3175 gmep
= kmem_alloc(sizeof (*gmep
), KM_SLEEP
);
3176 err
= dsl_dataset_hold_obj(dp
, snapobj
, gmep
, &snapds
);
3178 gmep
->guid
= dsl_dataset_phys(snapds
)->ds_guid
;
3179 gmep
->gme_ds
= snapds
;
3180 avl_add(guid_map
, gmep
);
3181 dsl_dataset_long_hold(snapds
, gmep
);
3183 kmem_free(gmep
, sizeof (*gmep
));
3186 dsl_pool_rele(dp
, FTAG
);
3190 static int dmu_recv_end_modified_blocks
= 3;
3193 dmu_recv_existing_end(dmu_recv_cookie_t
*drc
)
3199 * We will be destroying the ds; make sure its origin is unmounted if
3202 char name
[ZFS_MAX_DATASET_NAME_LEN
];
3203 dsl_dataset_name(drc
->drc_ds
, name
);
3204 zfs_destroy_unmount_origin(name
);
3207 error
= dsl_sync_task(drc
->drc_tofs
,
3208 dmu_recv_end_check
, dmu_recv_end_sync
, drc
,
3209 dmu_recv_end_modified_blocks
, ZFS_SPACE_CHECK_NORMAL
);
3212 dmu_recv_cleanup_ds(drc
);
3217 dmu_recv_new_end(dmu_recv_cookie_t
*drc
)
3221 error
= dsl_sync_task(drc
->drc_tofs
,
3222 dmu_recv_end_check
, dmu_recv_end_sync
, drc
,
3223 dmu_recv_end_modified_blocks
, ZFS_SPACE_CHECK_NORMAL
);
3226 dmu_recv_cleanup_ds(drc
);
3227 } else if (drc
->drc_guid_to_ds_map
!= NULL
) {
3228 (void) add_ds_to_guidmap(drc
->drc_tofs
,
3229 drc
->drc_guid_to_ds_map
,
3230 drc
->drc_newsnapobj
);
3236 dmu_recv_end(dmu_recv_cookie_t
*drc
, void *owner
)
3238 drc
->drc_owner
= owner
;
3241 return (dmu_recv_new_end(drc
));
3243 return (dmu_recv_existing_end(drc
));
3247 * Return TRUE if this objset is currently being received into.
3250 dmu_objset_is_receiving(objset_t
*os
)
3252 return (os
->os_dsl_dataset
!= NULL
&&
3253 os
->os_dsl_dataset
->ds_owner
== dmu_recv_tag
);
3256 #if defined(_KERNEL)
3257 module_param(zfs_send_corrupt_data
, int, 0644);
3258 MODULE_PARM_DESC(zfs_send_corrupt_data
, "Allow sending corrupt data");