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 (c) 2011 by Delphix. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2014, Joyent, Inc. All rights reserved.
26 * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
27 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
31 #include <sys/dmu_impl.h>
32 #include <sys/dmu_tx.h>
34 #include <sys/dnode.h>
35 #include <sys/zfs_context.h>
36 #include <sys/dmu_objset.h>
37 #include <sys/dmu_traverse.h>
38 #include <sys/dsl_dataset.h>
39 #include <sys/dsl_dir.h>
40 #include <sys/dsl_prop.h>
41 #include <sys/dsl_pool.h>
42 #include <sys/dsl_synctask.h>
43 #include <sys/spa_impl.h>
44 #include <sys/zfs_ioctl.h>
46 #include <sys/zio_checksum.h>
47 #include <sys/zfs_znode.h>
48 #include <zfs_fletcher.h>
51 #include <sys/zfs_onexit.h>
52 #include <sys/dmu_send.h>
53 #include <sys/dsl_destroy.h>
54 #include <sys/blkptr.h>
55 #include <sys/dsl_bookmark.h>
56 #include <sys/zfeature.h>
59 /* Set this tunable to TRUE to replace corrupt data with 0x2f5baddb10c */
60 int zfs_send_corrupt_data
= B_FALSE
;
62 static char *dmu_recv_tag
= "dmu_recv_tag";
63 static const char *recv_clone_name
= "%recv";
65 typedef struct dump_bytes_io
{
66 dmu_sendarg_t
*dbi_dsp
;
72 dump_bytes_strategy(void *arg
)
74 dump_bytes_io_t
*dbi
= (dump_bytes_io_t
*)arg
;
75 dmu_sendarg_t
*dsp
= dbi
->dbi_dsp
;
76 dsl_dataset_t
*ds
= dsp
->dsa_os
->os_dsl_dataset
;
77 ssize_t resid
; /* have to get resid to get detailed errno */
78 ASSERT0(dbi
->dbi_len
% 8);
80 fletcher_4_incremental_native(dbi
->dbi_buf
, dbi
->dbi_len
, &dsp
->dsa_zc
);
81 dsp
->dsa_err
= vn_rdwr(UIO_WRITE
, dsp
->dsa_vp
,
82 (caddr_t
)dbi
->dbi_buf
, dbi
->dbi_len
,
83 0, UIO_SYSSPACE
, FAPPEND
, RLIM64_INFINITY
, CRED(), &resid
);
85 mutex_enter(&ds
->ds_sendstream_lock
);
86 *dsp
->dsa_off
+= dbi
->dbi_len
;
87 mutex_exit(&ds
->ds_sendstream_lock
);
91 dump_bytes(dmu_sendarg_t
*dsp
, void *buf
, int len
)
100 * The vn_rdwr() call is performed in a taskq to ensure that there is
101 * always enough stack space to write safely to the target filesystem.
102 * The ZIO_TYPE_FREE threads are used because there can be a lot of
103 * them and they are used in vdev_file.c for a similar purpose.
105 spa_taskq_dispatch_sync(dmu_objset_spa(dsp
->dsa_os
), ZIO_TYPE_FREE
,
106 ZIO_TASKQ_ISSUE
, dump_bytes_strategy
, &dbi
, TQ_SLEEP
);
108 return (dsp
->dsa_err
);
112 dump_free(dmu_sendarg_t
*dsp
, uint64_t object
, uint64_t offset
,
115 struct drr_free
*drrf
= &(dsp
->dsa_drr
->drr_u
.drr_free
);
118 * When we receive a free record, dbuf_free_range() assumes
119 * that the receiving system doesn't have any dbufs in the range
120 * being freed. This is always true because there is a one-record
121 * constraint: we only send one WRITE record for any given
122 * object+offset. We know that the one-record constraint is
123 * true because we always send data in increasing order by
126 * If the increasing-order constraint ever changes, we should find
127 * another way to assert that the one-record constraint is still
130 ASSERT(object
> dsp
->dsa_last_data_object
||
131 (object
== dsp
->dsa_last_data_object
&&
132 offset
> dsp
->dsa_last_data_offset
));
135 * If we are doing a non-incremental send, then there can't
136 * be any data in the dataset we're receiving into. Therefore
137 * a free record would simply be a no-op. Save space by not
138 * sending it to begin with.
140 if (!dsp
->dsa_incremental
)
143 if (length
!= -1ULL && offset
+ length
< offset
)
147 * If there is a pending op, but it's not PENDING_FREE, push it out,
148 * since free block aggregation can only be done for blocks of the
149 * same type (i.e., DRR_FREE records can only be aggregated with
150 * other DRR_FREE records. DRR_FREEOBJECTS records can only be
151 * aggregated with other DRR_FREEOBJECTS records.
153 if (dsp
->dsa_pending_op
!= PENDING_NONE
&&
154 dsp
->dsa_pending_op
!= PENDING_FREE
) {
155 if (dump_bytes(dsp
, dsp
->dsa_drr
,
156 sizeof (dmu_replay_record_t
)) != 0)
157 return (SET_ERROR(EINTR
));
158 dsp
->dsa_pending_op
= PENDING_NONE
;
161 if (dsp
->dsa_pending_op
== PENDING_FREE
) {
163 * There should never be a PENDING_FREE if length is -1
164 * (because dump_dnode is the only place where this
165 * function is called with a -1, and only after flushing
166 * any pending record).
168 ASSERT(length
!= -1ULL);
170 * Check to see whether this free block can be aggregated
173 if (drrf
->drr_object
== object
&& drrf
->drr_offset
+
174 drrf
->drr_length
== offset
) {
175 drrf
->drr_length
+= length
;
178 /* not a continuation. Push out pending record */
179 if (dump_bytes(dsp
, dsp
->dsa_drr
,
180 sizeof (dmu_replay_record_t
)) != 0)
181 return (SET_ERROR(EINTR
));
182 dsp
->dsa_pending_op
= PENDING_NONE
;
185 /* create a FREE record and make it pending */
186 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
187 dsp
->dsa_drr
->drr_type
= DRR_FREE
;
188 drrf
->drr_object
= object
;
189 drrf
->drr_offset
= offset
;
190 drrf
->drr_length
= length
;
191 drrf
->drr_toguid
= dsp
->dsa_toguid
;
192 if (length
== -1ULL) {
193 if (dump_bytes(dsp
, dsp
->dsa_drr
,
194 sizeof (dmu_replay_record_t
)) != 0)
195 return (SET_ERROR(EINTR
));
197 dsp
->dsa_pending_op
= PENDING_FREE
;
204 dump_write(dmu_sendarg_t
*dsp
, dmu_object_type_t type
,
205 uint64_t object
, uint64_t offset
, int blksz
, const blkptr_t
*bp
, void *data
)
207 struct drr_write
*drrw
= &(dsp
->dsa_drr
->drr_u
.drr_write
);
210 * We send data in increasing object, offset order.
211 * See comment in dump_free() for details.
213 ASSERT(object
> dsp
->dsa_last_data_object
||
214 (object
== dsp
->dsa_last_data_object
&&
215 offset
> dsp
->dsa_last_data_offset
));
216 dsp
->dsa_last_data_object
= object
;
217 dsp
->dsa_last_data_offset
= offset
+ blksz
- 1;
220 * If there is any kind of pending aggregation (currently either
221 * a grouping of free objects or free blocks), push it out to
222 * the stream, since aggregation can't be done across operations
223 * of different types.
225 if (dsp
->dsa_pending_op
!= PENDING_NONE
) {
226 if (dump_bytes(dsp
, dsp
->dsa_drr
,
227 sizeof (dmu_replay_record_t
)) != 0)
228 return (SET_ERROR(EINTR
));
229 dsp
->dsa_pending_op
= PENDING_NONE
;
231 /* write a DATA record */
232 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
233 dsp
->dsa_drr
->drr_type
= DRR_WRITE
;
234 drrw
->drr_object
= object
;
235 drrw
->drr_type
= type
;
236 drrw
->drr_offset
= offset
;
237 drrw
->drr_length
= blksz
;
238 drrw
->drr_toguid
= dsp
->dsa_toguid
;
239 if (bp
== NULL
|| BP_IS_EMBEDDED(bp
)) {
241 * There's no pre-computed checksum for partial-block
242 * writes or embedded BP's, so (like
243 * fletcher4-checkummed blocks) userland will have to
244 * compute a dedup-capable checksum itself.
246 drrw
->drr_checksumtype
= ZIO_CHECKSUM_OFF
;
248 drrw
->drr_checksumtype
= BP_GET_CHECKSUM(bp
);
249 if (zio_checksum_table
[drrw
->drr_checksumtype
].ci_dedup
)
250 drrw
->drr_checksumflags
|= DRR_CHECKSUM_DEDUP
;
251 DDK_SET_LSIZE(&drrw
->drr_key
, BP_GET_LSIZE(bp
));
252 DDK_SET_PSIZE(&drrw
->drr_key
, BP_GET_PSIZE(bp
));
253 DDK_SET_COMPRESS(&drrw
->drr_key
, BP_GET_COMPRESS(bp
));
254 drrw
->drr_key
.ddk_cksum
= bp
->blk_cksum
;
257 if (dump_bytes(dsp
, dsp
->dsa_drr
, sizeof (dmu_replay_record_t
)) != 0)
258 return (SET_ERROR(EINTR
));
259 if (dump_bytes(dsp
, data
, blksz
) != 0)
260 return (SET_ERROR(EINTR
));
265 dump_write_embedded(dmu_sendarg_t
*dsp
, uint64_t object
, uint64_t offset
,
266 int blksz
, const blkptr_t
*bp
)
268 char buf
[BPE_PAYLOAD_SIZE
];
269 struct drr_write_embedded
*drrw
=
270 &(dsp
->dsa_drr
->drr_u
.drr_write_embedded
);
272 if (dsp
->dsa_pending_op
!= PENDING_NONE
) {
273 if (dump_bytes(dsp
, dsp
->dsa_drr
,
274 sizeof (dmu_replay_record_t
)) != 0)
276 dsp
->dsa_pending_op
= PENDING_NONE
;
279 ASSERT(BP_IS_EMBEDDED(bp
));
281 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
282 dsp
->dsa_drr
->drr_type
= DRR_WRITE_EMBEDDED
;
283 drrw
->drr_object
= object
;
284 drrw
->drr_offset
= offset
;
285 drrw
->drr_length
= blksz
;
286 drrw
->drr_toguid
= dsp
->dsa_toguid
;
287 drrw
->drr_compression
= BP_GET_COMPRESS(bp
);
288 drrw
->drr_etype
= BPE_GET_ETYPE(bp
);
289 drrw
->drr_lsize
= BPE_GET_LSIZE(bp
);
290 drrw
->drr_psize
= BPE_GET_PSIZE(bp
);
292 decode_embedded_bp_compressed(bp
, buf
);
294 if (dump_bytes(dsp
, dsp
->dsa_drr
, sizeof (dmu_replay_record_t
)) != 0)
296 if (dump_bytes(dsp
, buf
, P2ROUNDUP(drrw
->drr_psize
, 8)) != 0)
302 dump_spill(dmu_sendarg_t
*dsp
, uint64_t object
, int blksz
, void *data
)
304 struct drr_spill
*drrs
= &(dsp
->dsa_drr
->drr_u
.drr_spill
);
306 if (dsp
->dsa_pending_op
!= PENDING_NONE
) {
307 if (dump_bytes(dsp
, dsp
->dsa_drr
,
308 sizeof (dmu_replay_record_t
)) != 0)
309 return (SET_ERROR(EINTR
));
310 dsp
->dsa_pending_op
= PENDING_NONE
;
313 /* write a SPILL record */
314 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
315 dsp
->dsa_drr
->drr_type
= DRR_SPILL
;
316 drrs
->drr_object
= object
;
317 drrs
->drr_length
= blksz
;
318 drrs
->drr_toguid
= dsp
->dsa_toguid
;
320 if (dump_bytes(dsp
, dsp
->dsa_drr
, sizeof (dmu_replay_record_t
)))
321 return (SET_ERROR(EINTR
));
322 if (dump_bytes(dsp
, data
, blksz
))
323 return (SET_ERROR(EINTR
));
328 dump_freeobjects(dmu_sendarg_t
*dsp
, uint64_t firstobj
, uint64_t numobjs
)
330 struct drr_freeobjects
*drrfo
= &(dsp
->dsa_drr
->drr_u
.drr_freeobjects
);
332 /* See comment in dump_free(). */
333 if (!dsp
->dsa_incremental
)
337 * If there is a pending op, but it's not PENDING_FREEOBJECTS,
338 * push it out, since free block aggregation can only be done for
339 * blocks of the same type (i.e., DRR_FREE records can only be
340 * aggregated with other DRR_FREE records. DRR_FREEOBJECTS records
341 * can only be aggregated with other DRR_FREEOBJECTS records.
343 if (dsp
->dsa_pending_op
!= PENDING_NONE
&&
344 dsp
->dsa_pending_op
!= PENDING_FREEOBJECTS
) {
345 if (dump_bytes(dsp
, dsp
->dsa_drr
,
346 sizeof (dmu_replay_record_t
)) != 0)
347 return (SET_ERROR(EINTR
));
348 dsp
->dsa_pending_op
= PENDING_NONE
;
350 if (dsp
->dsa_pending_op
== PENDING_FREEOBJECTS
) {
352 * See whether this free object array can be aggregated
355 if (drrfo
->drr_firstobj
+ drrfo
->drr_numobjs
== firstobj
) {
356 drrfo
->drr_numobjs
+= numobjs
;
359 /* can't be aggregated. Push out pending record */
360 if (dump_bytes(dsp
, dsp
->dsa_drr
,
361 sizeof (dmu_replay_record_t
)) != 0)
362 return (SET_ERROR(EINTR
));
363 dsp
->dsa_pending_op
= PENDING_NONE
;
367 /* write a FREEOBJECTS record */
368 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
369 dsp
->dsa_drr
->drr_type
= DRR_FREEOBJECTS
;
370 drrfo
->drr_firstobj
= firstobj
;
371 drrfo
->drr_numobjs
= numobjs
;
372 drrfo
->drr_toguid
= dsp
->dsa_toguid
;
374 dsp
->dsa_pending_op
= PENDING_FREEOBJECTS
;
380 dump_dnode(dmu_sendarg_t
*dsp
, uint64_t object
, dnode_phys_t
*dnp
)
382 struct drr_object
*drro
= &(dsp
->dsa_drr
->drr_u
.drr_object
);
384 if (dnp
== NULL
|| dnp
->dn_type
== DMU_OT_NONE
)
385 return (dump_freeobjects(dsp
, object
, 1));
387 if (dsp
->dsa_pending_op
!= PENDING_NONE
) {
388 if (dump_bytes(dsp
, dsp
->dsa_drr
,
389 sizeof (dmu_replay_record_t
)) != 0)
390 return (SET_ERROR(EINTR
));
391 dsp
->dsa_pending_op
= PENDING_NONE
;
394 /* write an OBJECT record */
395 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
396 dsp
->dsa_drr
->drr_type
= DRR_OBJECT
;
397 drro
->drr_object
= object
;
398 drro
->drr_type
= dnp
->dn_type
;
399 drro
->drr_bonustype
= dnp
->dn_bonustype
;
400 drro
->drr_blksz
= dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
;
401 drro
->drr_bonuslen
= dnp
->dn_bonuslen
;
402 drro
->drr_checksumtype
= dnp
->dn_checksum
;
403 drro
->drr_compress
= dnp
->dn_compress
;
404 drro
->drr_toguid
= dsp
->dsa_toguid
;
406 if (!(dsp
->dsa_featureflags
& DMU_BACKUP_FEATURE_LARGE_BLOCKS
) &&
407 drro
->drr_blksz
> SPA_OLD_MAXBLOCKSIZE
)
408 drro
->drr_blksz
= SPA_OLD_MAXBLOCKSIZE
;
410 if (dump_bytes(dsp
, dsp
->dsa_drr
, sizeof (dmu_replay_record_t
)) != 0)
411 return (SET_ERROR(EINTR
));
413 if (dump_bytes(dsp
, DN_BONUS(dnp
), P2ROUNDUP(dnp
->dn_bonuslen
, 8)) != 0)
414 return (SET_ERROR(EINTR
));
416 /* Free anything past the end of the file. */
417 if (dump_free(dsp
, object
, (dnp
->dn_maxblkid
+ 1) *
418 (dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
), -1ULL) != 0)
419 return (SET_ERROR(EINTR
));
420 if (dsp
->dsa_err
!= 0)
421 return (SET_ERROR(EINTR
));
426 backup_do_embed(dmu_sendarg_t
*dsp
, const blkptr_t
*bp
)
428 if (!BP_IS_EMBEDDED(bp
))
432 * Compression function must be legacy, or explicitly enabled.
434 if ((BP_GET_COMPRESS(bp
) >= ZIO_COMPRESS_LEGACY_FUNCTIONS
&&
435 !(dsp
->dsa_featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA_LZ4
)))
439 * Embed type must be explicitly enabled.
441 switch (BPE_GET_ETYPE(bp
)) {
442 case BP_EMBEDDED_TYPE_DATA
:
443 if (dsp
->dsa_featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA
)
452 #define BP_SPAN(dnp, level) \
453 (((uint64_t)dnp->dn_datablkszsec) << (SPA_MINBLOCKSHIFT + \
454 (level) * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT)))
458 backup_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
459 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
461 dmu_sendarg_t
*dsp
= arg
;
462 dmu_object_type_t type
= bp
? BP_GET_TYPE(bp
) : DMU_OT_NONE
;
465 if (issig(JUSTLOOKING
) && issig(FORREAL
))
466 return (SET_ERROR(EINTR
));
468 if (zb
->zb_object
!= DMU_META_DNODE_OBJECT
&&
469 DMU_OBJECT_IS_SPECIAL(zb
->zb_object
)) {
471 } else if (zb
->zb_level
== ZB_ZIL_LEVEL
) {
473 * If we are sending a non-snapshot (which is allowed on
474 * read-only pools), it may have a ZIL, which must be ignored.
477 } else if (BP_IS_HOLE(bp
) &&
478 zb
->zb_object
== DMU_META_DNODE_OBJECT
) {
479 uint64_t span
= BP_SPAN(dnp
, zb
->zb_level
);
480 uint64_t dnobj
= (zb
->zb_blkid
* span
) >> DNODE_SHIFT
;
481 err
= dump_freeobjects(dsp
, dnobj
, span
>> DNODE_SHIFT
);
482 } else if (BP_IS_HOLE(bp
)) {
483 uint64_t span
= BP_SPAN(dnp
, zb
->zb_level
);
484 err
= dump_free(dsp
, zb
->zb_object
, zb
->zb_blkid
* span
, span
);
485 } else if (zb
->zb_level
> 0 || type
== DMU_OT_OBJSET
) {
487 } else if (type
== DMU_OT_DNODE
) {
490 int blksz
= BP_GET_LSIZE(bp
);
491 arc_flags_t aflags
= ARC_FLAG_WAIT
;
494 if (arc_read(NULL
, spa
, bp
, arc_getbuf_func
, &abuf
,
495 ZIO_PRIORITY_ASYNC_READ
, ZIO_FLAG_CANFAIL
,
497 return (SET_ERROR(EIO
));
500 for (i
= 0; i
< blksz
>> DNODE_SHIFT
; i
++) {
501 uint64_t dnobj
= (zb
->zb_blkid
<<
502 (DNODE_BLOCK_SHIFT
- DNODE_SHIFT
)) + i
;
503 err
= dump_dnode(dsp
, dnobj
, blk
+i
);
507 (void) arc_buf_remove_ref(abuf
, &abuf
);
508 } else if (type
== DMU_OT_SA
) {
509 arc_flags_t aflags
= ARC_FLAG_WAIT
;
511 int blksz
= BP_GET_LSIZE(bp
);
513 if (arc_read(NULL
, spa
, bp
, arc_getbuf_func
, &abuf
,
514 ZIO_PRIORITY_ASYNC_READ
, ZIO_FLAG_CANFAIL
,
516 return (SET_ERROR(EIO
));
518 err
= dump_spill(dsp
, zb
->zb_object
, blksz
, abuf
->b_data
);
519 (void) arc_buf_remove_ref(abuf
, &abuf
);
520 } else if (backup_do_embed(dsp
, bp
)) {
521 /* it's an embedded level-0 block of a regular object */
522 int blksz
= dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
;
523 err
= dump_write_embedded(dsp
, zb
->zb_object
,
524 zb
->zb_blkid
* blksz
, blksz
, bp
);
525 } else { /* it's a level-0 block of a regular object */
527 arc_flags_t aflags
= ARC_FLAG_WAIT
;
529 int blksz
= BP_GET_LSIZE(bp
);
531 ASSERT3U(blksz
, ==, dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
);
532 ASSERT0(zb
->zb_level
);
533 if (arc_read(NULL
, spa
, bp
, arc_getbuf_func
, &abuf
,
534 ZIO_PRIORITY_ASYNC_READ
, ZIO_FLAG_CANFAIL
,
536 if (zfs_send_corrupt_data
) {
538 /* Send a block filled with 0x"zfs badd bloc" */
539 abuf
= arc_buf_alloc(spa
, blksz
, &abuf
,
541 for (ptr
= abuf
->b_data
;
542 (char *)ptr
< (char *)abuf
->b_data
+ blksz
;
544 *ptr
= 0x2f5baddb10cULL
;
546 return (SET_ERROR(EIO
));
550 offset
= zb
->zb_blkid
* blksz
;
552 if (!(dsp
->dsa_featureflags
&
553 DMU_BACKUP_FEATURE_LARGE_BLOCKS
) &&
554 blksz
> SPA_OLD_MAXBLOCKSIZE
) {
555 char *buf
= abuf
->b_data
;
556 while (blksz
> 0 && err
== 0) {
557 int n
= MIN(blksz
, SPA_OLD_MAXBLOCKSIZE
);
558 err
= dump_write(dsp
, type
, zb
->zb_object
,
559 offset
, n
, NULL
, buf
);
565 err
= dump_write(dsp
, type
, zb
->zb_object
,
566 offset
, blksz
, bp
, abuf
->b_data
);
568 (void) arc_buf_remove_ref(abuf
, &abuf
);
571 ASSERT(err
== 0 || err
== EINTR
);
576 * Releases dp using the specified tag.
579 dmu_send_impl(void *tag
, dsl_pool_t
*dp
, dsl_dataset_t
*ds
,
580 zfs_bookmark_phys_t
*fromzb
, boolean_t is_clone
, boolean_t embedok
,
581 boolean_t large_block_ok
, int outfd
, vnode_t
*vp
, offset_t
*off
)
584 dmu_replay_record_t
*drr
;
587 uint64_t fromtxg
= 0;
588 uint64_t featureflags
= 0;
590 err
= dmu_objset_from_ds(ds
, &os
);
592 dsl_pool_rele(dp
, tag
);
596 drr
= kmem_zalloc(sizeof (dmu_replay_record_t
), KM_SLEEP
);
597 drr
->drr_type
= DRR_BEGIN
;
598 drr
->drr_u
.drr_begin
.drr_magic
= DMU_BACKUP_MAGIC
;
599 DMU_SET_STREAM_HDRTYPE(drr
->drr_u
.drr_begin
.drr_versioninfo
,
603 if (dmu_objset_type(os
) == DMU_OST_ZFS
) {
605 if (zfs_get_zplprop(os
, ZFS_PROP_VERSION
, &version
) != 0) {
606 kmem_free(drr
, sizeof (dmu_replay_record_t
));
607 dsl_pool_rele(dp
, tag
);
608 return (SET_ERROR(EINVAL
));
610 if (version
>= ZPL_VERSION_SA
) {
611 featureflags
|= DMU_BACKUP_FEATURE_SA_SPILL
;
616 if (large_block_ok
&& ds
->ds_large_blocks
)
617 featureflags
|= DMU_BACKUP_FEATURE_LARGE_BLOCKS
;
619 spa_feature_is_active(dp
->dp_spa
, SPA_FEATURE_EMBEDDED_DATA
)) {
620 featureflags
|= DMU_BACKUP_FEATURE_EMBED_DATA
;
621 if (spa_feature_is_active(dp
->dp_spa
, SPA_FEATURE_LZ4_COMPRESS
))
622 featureflags
|= DMU_BACKUP_FEATURE_EMBED_DATA_LZ4
;
627 DMU_SET_FEATUREFLAGS(drr
->drr_u
.drr_begin
.drr_versioninfo
,
630 drr
->drr_u
.drr_begin
.drr_creation_time
=
631 dsl_dataset_phys(ds
)->ds_creation_time
;
632 drr
->drr_u
.drr_begin
.drr_type
= dmu_objset_type(os
);
634 drr
->drr_u
.drr_begin
.drr_flags
|= DRR_FLAG_CLONE
;
635 drr
->drr_u
.drr_begin
.drr_toguid
= dsl_dataset_phys(ds
)->ds_guid
;
636 if (dsl_dataset_phys(ds
)->ds_flags
& DS_FLAG_CI_DATASET
)
637 drr
->drr_u
.drr_begin
.drr_flags
|= DRR_FLAG_CI_DATA
;
639 if (fromzb
!= NULL
) {
640 drr
->drr_u
.drr_begin
.drr_fromguid
= fromzb
->zbm_guid
;
641 fromtxg
= fromzb
->zbm_creation_txg
;
643 dsl_dataset_name(ds
, drr
->drr_u
.drr_begin
.drr_toname
);
644 if (!ds
->ds_is_snapshot
) {
645 (void) strlcat(drr
->drr_u
.drr_begin
.drr_toname
, "@--head--",
646 sizeof (drr
->drr_u
.drr_begin
.drr_toname
));
649 dsp
= kmem_zalloc(sizeof (dmu_sendarg_t
), KM_SLEEP
);
653 dsp
->dsa_outfd
= outfd
;
654 dsp
->dsa_proc
= curproc
;
657 dsp
->dsa_toguid
= dsl_dataset_phys(ds
)->ds_guid
;
658 ZIO_SET_CHECKSUM(&dsp
->dsa_zc
, 0, 0, 0, 0);
659 dsp
->dsa_pending_op
= PENDING_NONE
;
660 dsp
->dsa_incremental
= (fromzb
!= NULL
);
661 dsp
->dsa_featureflags
= featureflags
;
663 mutex_enter(&ds
->ds_sendstream_lock
);
664 list_insert_head(&ds
->ds_sendstreams
, dsp
);
665 mutex_exit(&ds
->ds_sendstream_lock
);
667 dsl_dataset_long_hold(ds
, FTAG
);
668 dsl_pool_rele(dp
, tag
);
670 if (dump_bytes(dsp
, drr
, sizeof (dmu_replay_record_t
)) != 0) {
675 err
= traverse_dataset(ds
, fromtxg
, TRAVERSE_PRE
| TRAVERSE_PREFETCH
,
678 if (dsp
->dsa_pending_op
!= PENDING_NONE
)
679 if (dump_bytes(dsp
, drr
, sizeof (dmu_replay_record_t
)) != 0)
680 err
= SET_ERROR(EINTR
);
683 if (err
== EINTR
&& dsp
->dsa_err
!= 0)
688 bzero(drr
, sizeof (dmu_replay_record_t
));
689 drr
->drr_type
= DRR_END
;
690 drr
->drr_u
.drr_end
.drr_checksum
= dsp
->dsa_zc
;
691 drr
->drr_u
.drr_end
.drr_toguid
= dsp
->dsa_toguid
;
693 if (dump_bytes(dsp
, drr
, sizeof (dmu_replay_record_t
)) != 0) {
699 mutex_enter(&ds
->ds_sendstream_lock
);
700 list_remove(&ds
->ds_sendstreams
, dsp
);
701 mutex_exit(&ds
->ds_sendstream_lock
);
703 kmem_free(drr
, sizeof (dmu_replay_record_t
));
704 kmem_free(dsp
, sizeof (dmu_sendarg_t
));
706 dsl_dataset_long_rele(ds
, FTAG
);
712 dmu_send_obj(const char *pool
, uint64_t tosnap
, uint64_t fromsnap
,
713 boolean_t embedok
, boolean_t large_block_ok
,
714 int outfd
, vnode_t
*vp
, offset_t
*off
)
718 dsl_dataset_t
*fromds
= NULL
;
721 err
= dsl_pool_hold(pool
, FTAG
, &dp
);
725 err
= dsl_dataset_hold_obj(dp
, tosnap
, FTAG
, &ds
);
727 dsl_pool_rele(dp
, FTAG
);
732 zfs_bookmark_phys_t zb
;
735 err
= dsl_dataset_hold_obj(dp
, fromsnap
, FTAG
, &fromds
);
737 dsl_dataset_rele(ds
, FTAG
);
738 dsl_pool_rele(dp
, FTAG
);
741 if (!dsl_dataset_is_before(ds
, fromds
, 0))
742 err
= SET_ERROR(EXDEV
);
743 zb
.zbm_creation_time
=
744 dsl_dataset_phys(fromds
)->ds_creation_time
;
745 zb
.zbm_creation_txg
= dsl_dataset_phys(fromds
)->ds_creation_txg
;
746 zb
.zbm_guid
= dsl_dataset_phys(fromds
)->ds_guid
;
747 is_clone
= (fromds
->ds_dir
!= ds
->ds_dir
);
748 dsl_dataset_rele(fromds
, FTAG
);
749 err
= dmu_send_impl(FTAG
, dp
, ds
, &zb
, is_clone
,
750 embedok
, large_block_ok
, outfd
, vp
, off
);
752 err
= dmu_send_impl(FTAG
, dp
, ds
, NULL
, B_FALSE
,
753 embedok
, large_block_ok
, outfd
, vp
, off
);
755 dsl_dataset_rele(ds
, FTAG
);
760 dmu_send(const char *tosnap
, const char *fromsnap
,
761 boolean_t embedok
, boolean_t large_block_ok
,
762 int outfd
, vnode_t
*vp
, offset_t
*off
)
767 boolean_t owned
= B_FALSE
;
769 if (fromsnap
!= NULL
&& strpbrk(fromsnap
, "@#") == NULL
)
770 return (SET_ERROR(EINVAL
));
772 err
= dsl_pool_hold(tosnap
, FTAG
, &dp
);
776 if (strchr(tosnap
, '@') == NULL
&& spa_writeable(dp
->dp_spa
)) {
778 * We are sending a filesystem or volume. Ensure
779 * that it doesn't change by owning the dataset.
781 err
= dsl_dataset_own(dp
, tosnap
, FTAG
, &ds
);
784 err
= dsl_dataset_hold(dp
, tosnap
, FTAG
, &ds
);
787 dsl_pool_rele(dp
, FTAG
);
791 if (fromsnap
!= NULL
) {
792 zfs_bookmark_phys_t zb
;
793 boolean_t is_clone
= B_FALSE
;
794 int fsnamelen
= strchr(tosnap
, '@') - tosnap
;
797 * If the fromsnap is in a different filesystem, then
798 * mark the send stream as a clone.
800 if (strncmp(tosnap
, fromsnap
, fsnamelen
) != 0 ||
801 (fromsnap
[fsnamelen
] != '@' &&
802 fromsnap
[fsnamelen
] != '#')) {
806 if (strchr(fromsnap
, '@')) {
807 dsl_dataset_t
*fromds
;
808 err
= dsl_dataset_hold(dp
, fromsnap
, FTAG
, &fromds
);
810 if (!dsl_dataset_is_before(ds
, fromds
, 0))
811 err
= SET_ERROR(EXDEV
);
812 zb
.zbm_creation_time
=
813 dsl_dataset_phys(fromds
)->ds_creation_time
;
814 zb
.zbm_creation_txg
=
815 dsl_dataset_phys(fromds
)->ds_creation_txg
;
816 zb
.zbm_guid
= dsl_dataset_phys(fromds
)->ds_guid
;
817 is_clone
= (ds
->ds_dir
!= fromds
->ds_dir
);
818 dsl_dataset_rele(fromds
, FTAG
);
821 err
= dsl_bookmark_lookup(dp
, fromsnap
, ds
, &zb
);
824 dsl_dataset_rele(ds
, FTAG
);
825 dsl_pool_rele(dp
, FTAG
);
828 err
= dmu_send_impl(FTAG
, dp
, ds
, &zb
, is_clone
,
829 embedok
, large_block_ok
, outfd
, vp
, off
);
831 err
= dmu_send_impl(FTAG
, dp
, ds
, NULL
, B_FALSE
,
832 embedok
, large_block_ok
, outfd
, vp
, off
);
835 dsl_dataset_disown(ds
, FTAG
);
837 dsl_dataset_rele(ds
, FTAG
);
842 dmu_adjust_send_estimate_for_indirects(dsl_dataset_t
*ds
, uint64_t size
,
847 * Assume that space (both on-disk and in-stream) is dominated by
848 * data. We will adjust for indirect blocks and the copies property,
849 * but ignore per-object space used (eg, dnodes and DRR_OBJECT records).
853 * Subtract out approximate space used by indirect blocks.
854 * Assume most space is used by data blocks (non-indirect, non-dnode).
855 * Assume all blocks are recordsize. Assume ditto blocks and
856 * internal fragmentation counter out compression.
858 * Therefore, space used by indirect blocks is sizeof(blkptr_t) per
859 * block, which we observe in practice.
862 err
= dsl_prop_get_int_ds(ds
, "recordsize", &recordsize
);
865 size
-= size
/ recordsize
* sizeof (blkptr_t
);
867 /* Add in the space for the record associated with each block. */
868 size
+= size
/ recordsize
* sizeof (dmu_replay_record_t
);
876 dmu_send_estimate(dsl_dataset_t
*ds
, dsl_dataset_t
*fromds
, uint64_t *sizep
)
881 ASSERT(dsl_pool_config_held(ds
->ds_dir
->dd_pool
));
883 /* tosnap must be a snapshot */
884 if (!ds
->ds_is_snapshot
)
885 return (SET_ERROR(EINVAL
));
887 /* fromsnap, if provided, must be a snapshot */
888 if (fromds
!= NULL
&& !fromds
->ds_is_snapshot
)
889 return (SET_ERROR(EINVAL
));
892 * fromsnap must be an earlier snapshot from the same fs as tosnap,
893 * or the origin's fs.
895 if (fromds
!= NULL
&& !dsl_dataset_is_before(ds
, fromds
, 0))
896 return (SET_ERROR(EXDEV
));
898 /* Get uncompressed size estimate of changed data. */
899 if (fromds
== NULL
) {
900 size
= dsl_dataset_phys(ds
)->ds_uncompressed_bytes
;
903 err
= dsl_dataset_space_written(fromds
, ds
,
904 &used
, &comp
, &size
);
909 err
= dmu_adjust_send_estimate_for_indirects(ds
, size
, sizep
);
914 * Simple callback used to traverse the blocks of a snapshot and sum their
919 dmu_calculate_send_traversal(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
920 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
922 uint64_t *spaceptr
= arg
;
923 if (bp
!= NULL
&& !BP_IS_HOLE(bp
)) {
924 *spaceptr
+= BP_GET_UCSIZE(bp
);
930 * Given a desination snapshot and a TXG, calculate the approximate size of a
931 * send stream sent from that TXG. from_txg may be zero, indicating that the
932 * whole snapshot will be sent.
935 dmu_send_estimate_from_txg(dsl_dataset_t
*ds
, uint64_t from_txg
,
941 ASSERT(dsl_pool_config_held(ds
->ds_dir
->dd_pool
));
943 /* tosnap must be a snapshot */
944 if (!dsl_dataset_is_snapshot(ds
))
945 return (SET_ERROR(EINVAL
));
947 /* verify that from_txg is before the provided snapshot was taken */
948 if (from_txg
>= dsl_dataset_phys(ds
)->ds_creation_txg
) {
949 return (SET_ERROR(EXDEV
));
952 * traverse the blocks of the snapshot with birth times after
953 * from_txg, summing their uncompressed size
955 err
= traverse_dataset(ds
, from_txg
, TRAVERSE_POST
,
956 dmu_calculate_send_traversal
, &size
);
960 err
= dmu_adjust_send_estimate_for_indirects(ds
, size
, sizep
);
964 typedef struct dmu_recv_begin_arg
{
965 const char *drba_origin
;
966 dmu_recv_cookie_t
*drba_cookie
;
968 uint64_t drba_snapobj
;
969 } dmu_recv_begin_arg_t
;
972 recv_begin_check_existing_impl(dmu_recv_begin_arg_t
*drba
, dsl_dataset_t
*ds
,
977 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
979 /* temporary clone name must not exist */
980 error
= zap_lookup(dp
->dp_meta_objset
,
981 dsl_dir_phys(ds
->ds_dir
)->dd_child_dir_zapobj
, recv_clone_name
,
984 return (error
== 0 ? EBUSY
: error
);
986 /* new snapshot name must not exist */
987 error
= zap_lookup(dp
->dp_meta_objset
,
988 dsl_dataset_phys(ds
)->ds_snapnames_zapobj
,
989 drba
->drba_cookie
->drc_tosnap
, 8, 1, &val
);
991 return (error
== 0 ? EEXIST
: error
);
994 * Check snapshot limit before receiving. We'll recheck again at the
995 * end, but might as well abort before receiving if we're already over
998 * Note that we do not check the file system limit with
999 * dsl_dir_fscount_check because the temporary %clones don't count
1000 * against that limit.
1002 error
= dsl_fs_ss_limit_check(ds
->ds_dir
, 1, ZFS_PROP_SNAPSHOT_LIMIT
,
1003 NULL
, drba
->drba_cred
);
1007 if (fromguid
!= 0) {
1008 dsl_dataset_t
*snap
;
1009 uint64_t obj
= dsl_dataset_phys(ds
)->ds_prev_snap_obj
;
1011 /* Find snapshot in this dir that matches fromguid. */
1013 error
= dsl_dataset_hold_obj(dp
, obj
, FTAG
,
1016 return (SET_ERROR(ENODEV
));
1017 if (snap
->ds_dir
!= ds
->ds_dir
) {
1018 dsl_dataset_rele(snap
, FTAG
);
1019 return (SET_ERROR(ENODEV
));
1021 if (dsl_dataset_phys(snap
)->ds_guid
== fromguid
)
1023 obj
= dsl_dataset_phys(snap
)->ds_prev_snap_obj
;
1024 dsl_dataset_rele(snap
, FTAG
);
1027 return (SET_ERROR(ENODEV
));
1029 if (drba
->drba_cookie
->drc_force
) {
1030 drba
->drba_snapobj
= obj
;
1033 * If we are not forcing, there must be no
1034 * changes since fromsnap.
1036 if (dsl_dataset_modified_since_snap(ds
, snap
)) {
1037 dsl_dataset_rele(snap
, FTAG
);
1038 return (SET_ERROR(ETXTBSY
));
1040 drba
->drba_snapobj
= ds
->ds_prev
->ds_object
;
1043 dsl_dataset_rele(snap
, FTAG
);
1045 /* if full, then must be forced */
1046 if (!drba
->drba_cookie
->drc_force
)
1047 return (SET_ERROR(EEXIST
));
1048 /* start from $ORIGIN@$ORIGIN, if supported */
1049 drba
->drba_snapobj
= dp
->dp_origin_snap
!= NULL
?
1050 dp
->dp_origin_snap
->ds_object
: 0;
1058 dmu_recv_begin_check(void *arg
, dmu_tx_t
*tx
)
1060 dmu_recv_begin_arg_t
*drba
= arg
;
1061 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1062 struct drr_begin
*drrb
= drba
->drba_cookie
->drc_drrb
;
1063 uint64_t fromguid
= drrb
->drr_fromguid
;
1064 int flags
= drrb
->drr_flags
;
1066 uint64_t featureflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
1068 const char *tofs
= drba
->drba_cookie
->drc_tofs
;
1070 /* already checked */
1071 ASSERT3U(drrb
->drr_magic
, ==, DMU_BACKUP_MAGIC
);
1073 if (DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) ==
1074 DMU_COMPOUNDSTREAM
||
1075 drrb
->drr_type
>= DMU_OST_NUMTYPES
||
1076 ((flags
& DRR_FLAG_CLONE
) && drba
->drba_origin
== NULL
))
1077 return (SET_ERROR(EINVAL
));
1079 /* Verify pool version supports SA if SA_SPILL feature set */
1080 if ((featureflags
& DMU_BACKUP_FEATURE_SA_SPILL
) &&
1081 spa_version(dp
->dp_spa
) < SPA_VERSION_SA
)
1082 return (SET_ERROR(ENOTSUP
));
1085 * The receiving code doesn't know how to translate a WRITE_EMBEDDED
1086 * record to a plan WRITE record, so the pool must have the
1087 * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
1088 * records. Same with WRITE_EMBEDDED records that use LZ4 compression.
1090 if ((featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA
) &&
1091 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_EMBEDDED_DATA
))
1092 return (SET_ERROR(ENOTSUP
));
1093 if ((featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA_LZ4
) &&
1094 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_LZ4_COMPRESS
))
1095 return (SET_ERROR(ENOTSUP
));
1098 * The receiving code doesn't know how to translate large blocks
1099 * to smaller ones, so the pool must have the LARGE_BLOCKS
1100 * feature enabled if the stream has LARGE_BLOCKS.
1102 if ((featureflags
& DMU_BACKUP_FEATURE_LARGE_BLOCKS
) &&
1103 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_LARGE_BLOCKS
))
1104 return (SET_ERROR(ENOTSUP
));
1106 error
= dsl_dataset_hold(dp
, tofs
, FTAG
, &ds
);
1108 /* target fs already exists; recv into temp clone */
1110 /* Can't recv a clone into an existing fs */
1111 if (flags
& DRR_FLAG_CLONE
) {
1112 dsl_dataset_rele(ds
, FTAG
);
1113 return (SET_ERROR(EINVAL
));
1116 error
= recv_begin_check_existing_impl(drba
, ds
, fromguid
);
1117 dsl_dataset_rele(ds
, FTAG
);
1118 } else if (error
== ENOENT
) {
1119 /* target fs does not exist; must be a full backup or clone */
1120 char buf
[MAXNAMELEN
];
1123 * If it's a non-clone incremental, we are missing the
1124 * target fs, so fail the recv.
1126 if (fromguid
!= 0 && !(flags
& DRR_FLAG_CLONE
))
1127 return (SET_ERROR(ENOENT
));
1129 /* Open the parent of tofs */
1130 ASSERT3U(strlen(tofs
), <, MAXNAMELEN
);
1131 (void) strlcpy(buf
, tofs
, strrchr(tofs
, '/') - tofs
+ 1);
1132 error
= dsl_dataset_hold(dp
, buf
, FTAG
, &ds
);
1137 * Check filesystem and snapshot limits before receiving. We'll
1138 * recheck snapshot limits again at the end (we create the
1139 * filesystems and increment those counts during begin_sync).
1141 error
= dsl_fs_ss_limit_check(ds
->ds_dir
, 1,
1142 ZFS_PROP_FILESYSTEM_LIMIT
, NULL
, drba
->drba_cred
);
1144 dsl_dataset_rele(ds
, FTAG
);
1148 error
= dsl_fs_ss_limit_check(ds
->ds_dir
, 1,
1149 ZFS_PROP_SNAPSHOT_LIMIT
, NULL
, drba
->drba_cred
);
1151 dsl_dataset_rele(ds
, FTAG
);
1155 if (drba
->drba_origin
!= NULL
) {
1156 dsl_dataset_t
*origin
;
1157 error
= dsl_dataset_hold(dp
, drba
->drba_origin
,
1160 dsl_dataset_rele(ds
, FTAG
);
1163 if (!origin
->ds_is_snapshot
) {
1164 dsl_dataset_rele(origin
, FTAG
);
1165 dsl_dataset_rele(ds
, FTAG
);
1166 return (SET_ERROR(EINVAL
));
1168 if (dsl_dataset_phys(origin
)->ds_guid
!= fromguid
) {
1169 dsl_dataset_rele(origin
, FTAG
);
1170 dsl_dataset_rele(ds
, FTAG
);
1171 return (SET_ERROR(ENODEV
));
1173 dsl_dataset_rele(origin
, FTAG
);
1175 dsl_dataset_rele(ds
, FTAG
);
1182 dmu_recv_begin_sync(void *arg
, dmu_tx_t
*tx
)
1184 dmu_recv_begin_arg_t
*drba
= arg
;
1185 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1186 struct drr_begin
*drrb
= drba
->drba_cookie
->drc_drrb
;
1187 const char *tofs
= drba
->drba_cookie
->drc_tofs
;
1188 dsl_dataset_t
*ds
, *newds
;
1193 crflags
= (drrb
->drr_flags
& DRR_FLAG_CI_DATA
) ?
1194 DS_FLAG_CI_DATASET
: 0;
1196 error
= dsl_dataset_hold(dp
, tofs
, FTAG
, &ds
);
1198 /* create temporary clone */
1199 dsl_dataset_t
*snap
= NULL
;
1200 if (drba
->drba_snapobj
!= 0) {
1201 VERIFY0(dsl_dataset_hold_obj(dp
,
1202 drba
->drba_snapobj
, FTAG
, &snap
));
1204 dsobj
= dsl_dataset_create_sync(ds
->ds_dir
, recv_clone_name
,
1205 snap
, crflags
, drba
->drba_cred
, tx
);
1206 dsl_dataset_rele(snap
, FTAG
);
1207 dsl_dataset_rele(ds
, FTAG
);
1211 dsl_dataset_t
*origin
= NULL
;
1213 VERIFY0(dsl_dir_hold(dp
, tofs
, FTAG
, &dd
, &tail
));
1215 if (drba
->drba_origin
!= NULL
) {
1216 VERIFY0(dsl_dataset_hold(dp
, drba
->drba_origin
,
1220 /* Create new dataset. */
1221 dsobj
= dsl_dataset_create_sync(dd
,
1222 strrchr(tofs
, '/') + 1,
1223 origin
, crflags
, drba
->drba_cred
, tx
);
1225 dsl_dataset_rele(origin
, FTAG
);
1226 dsl_dir_rele(dd
, FTAG
);
1227 drba
->drba_cookie
->drc_newfs
= B_TRUE
;
1229 VERIFY0(dsl_dataset_own_obj(dp
, dsobj
, dmu_recv_tag
, &newds
));
1231 if ((DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
) &
1232 DMU_BACKUP_FEATURE_LARGE_BLOCKS
) &&
1233 !newds
->ds_large_blocks
) {
1234 dsl_dataset_activate_large_blocks_sync_impl(dsobj
, tx
);
1235 newds
->ds_large_blocks
= B_TRUE
;
1238 dmu_buf_will_dirty(newds
->ds_dbuf
, tx
);
1239 dsl_dataset_phys(newds
)->ds_flags
|= DS_FLAG_INCONSISTENT
;
1242 * If we actually created a non-clone, we need to create the
1243 * objset in our new dataset.
1245 if (BP_IS_HOLE(dsl_dataset_get_blkptr(newds
))) {
1246 (void) dmu_objset_create_impl(dp
->dp_spa
,
1247 newds
, dsl_dataset_get_blkptr(newds
), drrb
->drr_type
, tx
);
1250 drba
->drba_cookie
->drc_ds
= newds
;
1252 spa_history_log_internal_ds(newds
, "receive", tx
, "");
1256 * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
1257 * succeeds; otherwise we will leak the holds on the datasets.
1260 dmu_recv_begin(char *tofs
, char *tosnap
, struct drr_begin
*drrb
,
1261 boolean_t force
, char *origin
, dmu_recv_cookie_t
*drc
)
1263 dmu_recv_begin_arg_t drba
= { 0 };
1264 dmu_replay_record_t
*drr
;
1266 bzero(drc
, sizeof (dmu_recv_cookie_t
));
1267 drc
->drc_drrb
= drrb
;
1268 drc
->drc_tosnap
= tosnap
;
1269 drc
->drc_tofs
= tofs
;
1270 drc
->drc_force
= force
;
1271 drc
->drc_cred
= CRED();
1273 if (drrb
->drr_magic
== BSWAP_64(DMU_BACKUP_MAGIC
))
1274 drc
->drc_byteswap
= B_TRUE
;
1275 else if (drrb
->drr_magic
!= DMU_BACKUP_MAGIC
)
1276 return (SET_ERROR(EINVAL
));
1278 drr
= kmem_zalloc(sizeof (dmu_replay_record_t
), KM_SLEEP
);
1279 drr
->drr_type
= DRR_BEGIN
;
1280 drr
->drr_u
.drr_begin
= *drc
->drc_drrb
;
1281 if (drc
->drc_byteswap
) {
1282 fletcher_4_incremental_byteswap(drr
,
1283 sizeof (dmu_replay_record_t
), &drc
->drc_cksum
);
1285 fletcher_4_incremental_native(drr
,
1286 sizeof (dmu_replay_record_t
), &drc
->drc_cksum
);
1288 kmem_free(drr
, sizeof (dmu_replay_record_t
));
1290 if (drc
->drc_byteswap
) {
1291 drrb
->drr_magic
= BSWAP_64(drrb
->drr_magic
);
1292 drrb
->drr_versioninfo
= BSWAP_64(drrb
->drr_versioninfo
);
1293 drrb
->drr_creation_time
= BSWAP_64(drrb
->drr_creation_time
);
1294 drrb
->drr_type
= BSWAP_32(drrb
->drr_type
);
1295 drrb
->drr_toguid
= BSWAP_64(drrb
->drr_toguid
);
1296 drrb
->drr_fromguid
= BSWAP_64(drrb
->drr_fromguid
);
1299 drba
.drba_origin
= origin
;
1300 drba
.drba_cookie
= drc
;
1301 drba
.drba_cred
= CRED();
1303 return (dsl_sync_task(tofs
, dmu_recv_begin_check
, dmu_recv_begin_sync
,
1304 &drba
, 5, ZFS_SPACE_CHECK_NORMAL
));
1313 int bufsize
; /* amount of memory allocated for buf */
1315 avl_tree_t
*guid_to_ds_map
;
1318 typedef struct guid_map_entry
{
1320 dsl_dataset_t
*gme_ds
;
1325 guid_compare(const void *arg1
, const void *arg2
)
1327 const guid_map_entry_t
*gmep1
= arg1
;
1328 const guid_map_entry_t
*gmep2
= arg2
;
1330 if (gmep1
->guid
< gmep2
->guid
)
1332 else if (gmep1
->guid
> gmep2
->guid
)
1338 free_guid_map_onexit(void *arg
)
1340 avl_tree_t
*ca
= arg
;
1341 void *cookie
= NULL
;
1342 guid_map_entry_t
*gmep
;
1344 while ((gmep
= avl_destroy_nodes(ca
, &cookie
)) != NULL
) {
1345 dsl_dataset_long_rele(gmep
->gme_ds
, gmep
);
1346 dsl_dataset_rele(gmep
->gme_ds
, gmep
);
1347 kmem_free(gmep
, sizeof (guid_map_entry_t
));
1350 kmem_free(ca
, sizeof (avl_tree_t
));
1354 restore_read(struct restorearg
*ra
, int len
, char *buf
)
1361 /* some things will require 8-byte alignment, so everything must */
1363 ASSERT3U(len
, <=, ra
->bufsize
);
1365 while (done
< len
) {
1368 ra
->err
= vn_rdwr(UIO_READ
, ra
->vp
,
1369 buf
+ done
, len
- done
,
1370 ra
->voff
, UIO_SYSSPACE
, FAPPEND
,
1371 RLIM64_INFINITY
, CRED(), &resid
);
1373 if (resid
== len
- done
)
1374 ra
->err
= SET_ERROR(EINVAL
);
1375 ra
->voff
+= len
- done
- resid
;
1381 ASSERT3U(done
, ==, len
);
1383 fletcher_4_incremental_byteswap(buf
, len
, &ra
->cksum
);
1385 fletcher_4_incremental_native(buf
, len
, &ra
->cksum
);
1389 noinline
static void
1390 backup_byteswap(dmu_replay_record_t
*drr
)
1392 #define DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
1393 #define DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
1394 drr
->drr_type
= BSWAP_32(drr
->drr_type
);
1395 drr
->drr_payloadlen
= BSWAP_32(drr
->drr_payloadlen
);
1396 switch (drr
->drr_type
) {
1398 DO64(drr_begin
.drr_magic
);
1399 DO64(drr_begin
.drr_versioninfo
);
1400 DO64(drr_begin
.drr_creation_time
);
1401 DO32(drr_begin
.drr_type
);
1402 DO32(drr_begin
.drr_flags
);
1403 DO64(drr_begin
.drr_toguid
);
1404 DO64(drr_begin
.drr_fromguid
);
1407 DO64(drr_object
.drr_object
);
1408 DO32(drr_object
.drr_type
);
1409 DO32(drr_object
.drr_bonustype
);
1410 DO32(drr_object
.drr_blksz
);
1411 DO32(drr_object
.drr_bonuslen
);
1412 DO64(drr_object
.drr_toguid
);
1414 case DRR_FREEOBJECTS
:
1415 DO64(drr_freeobjects
.drr_firstobj
);
1416 DO64(drr_freeobjects
.drr_numobjs
);
1417 DO64(drr_freeobjects
.drr_toguid
);
1420 DO64(drr_write
.drr_object
);
1421 DO32(drr_write
.drr_type
);
1422 DO64(drr_write
.drr_offset
);
1423 DO64(drr_write
.drr_length
);
1424 DO64(drr_write
.drr_toguid
);
1425 DO64(drr_write
.drr_key
.ddk_cksum
.zc_word
[0]);
1426 DO64(drr_write
.drr_key
.ddk_cksum
.zc_word
[1]);
1427 DO64(drr_write
.drr_key
.ddk_cksum
.zc_word
[2]);
1428 DO64(drr_write
.drr_key
.ddk_cksum
.zc_word
[3]);
1429 DO64(drr_write
.drr_key
.ddk_prop
);
1431 case DRR_WRITE_BYREF
:
1432 DO64(drr_write_byref
.drr_object
);
1433 DO64(drr_write_byref
.drr_offset
);
1434 DO64(drr_write_byref
.drr_length
);
1435 DO64(drr_write_byref
.drr_toguid
);
1436 DO64(drr_write_byref
.drr_refguid
);
1437 DO64(drr_write_byref
.drr_refobject
);
1438 DO64(drr_write_byref
.drr_refoffset
);
1439 DO64(drr_write_byref
.drr_key
.ddk_cksum
.zc_word
[0]);
1440 DO64(drr_write_byref
.drr_key
.ddk_cksum
.zc_word
[1]);
1441 DO64(drr_write_byref
.drr_key
.ddk_cksum
.zc_word
[2]);
1442 DO64(drr_write_byref
.drr_key
.ddk_cksum
.zc_word
[3]);
1443 DO64(drr_write_byref
.drr_key
.ddk_prop
);
1445 case DRR_WRITE_EMBEDDED
:
1446 DO64(drr_write_embedded
.drr_object
);
1447 DO64(drr_write_embedded
.drr_offset
);
1448 DO64(drr_write_embedded
.drr_length
);
1449 DO64(drr_write_embedded
.drr_toguid
);
1450 DO32(drr_write_embedded
.drr_lsize
);
1451 DO32(drr_write_embedded
.drr_psize
);
1454 DO64(drr_free
.drr_object
);
1455 DO64(drr_free
.drr_offset
);
1456 DO64(drr_free
.drr_length
);
1457 DO64(drr_free
.drr_toguid
);
1460 DO64(drr_spill
.drr_object
);
1461 DO64(drr_spill
.drr_length
);
1462 DO64(drr_spill
.drr_toguid
);
1465 DO64(drr_end
.drr_checksum
.zc_word
[0]);
1466 DO64(drr_end
.drr_checksum
.zc_word
[1]);
1467 DO64(drr_end
.drr_checksum
.zc_word
[2]);
1468 DO64(drr_end
.drr_checksum
.zc_word
[3]);
1469 DO64(drr_end
.drr_toguid
);
1478 static inline uint8_t
1479 deduce_nblkptr(dmu_object_type_t bonus_type
, uint64_t bonus_size
)
1481 if (bonus_type
== DMU_OT_SA
) {
1485 ((DN_MAX_BONUSLEN
- bonus_size
) >> SPA_BLKPTRSHIFT
));
1490 restore_object(struct restorearg
*ra
, objset_t
*os
, struct drr_object
*drro
)
1492 dmu_object_info_t doi
;
1498 if (drro
->drr_type
== DMU_OT_NONE
||
1499 !DMU_OT_IS_VALID(drro
->drr_type
) ||
1500 !DMU_OT_IS_VALID(drro
->drr_bonustype
) ||
1501 drro
->drr_checksumtype
>= ZIO_CHECKSUM_FUNCTIONS
||
1502 drro
->drr_compress
>= ZIO_COMPRESS_FUNCTIONS
||
1503 P2PHASE(drro
->drr_blksz
, SPA_MINBLOCKSIZE
) ||
1504 drro
->drr_blksz
< SPA_MINBLOCKSIZE
||
1505 drro
->drr_blksz
> spa_maxblocksize(dmu_objset_spa(os
)) ||
1506 drro
->drr_bonuslen
> DN_MAX_BONUSLEN
) {
1507 return (SET_ERROR(EINVAL
));
1510 err
= dmu_object_info(os
, drro
->drr_object
, &doi
);
1512 if (err
!= 0 && err
!= ENOENT
)
1513 return (SET_ERROR(EINVAL
));
1514 object
= err
== 0 ? drro
->drr_object
: DMU_NEW_OBJECT
;
1516 if (drro
->drr_bonuslen
) {
1517 data
= restore_read(ra
, P2ROUNDUP(drro
->drr_bonuslen
, 8), NULL
);
1523 * If we are losing blkptrs or changing the block size this must
1524 * be a new file instance. We must clear out the previous file
1525 * contents before we can change this type of metadata in the dnode.
1530 nblkptr
= deduce_nblkptr(drro
->drr_bonustype
,
1531 drro
->drr_bonuslen
);
1533 if (drro
->drr_blksz
!= doi
.doi_data_block_size
||
1534 nblkptr
< doi
.doi_nblkptr
) {
1535 err
= dmu_free_long_range(os
, drro
->drr_object
,
1538 return (SET_ERROR(EINVAL
));
1542 tx
= dmu_tx_create(os
);
1543 dmu_tx_hold_bonus(tx
, object
);
1544 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1550 if (object
== DMU_NEW_OBJECT
) {
1551 /* currently free, want to be allocated */
1552 err
= dmu_object_claim(os
, drro
->drr_object
,
1553 drro
->drr_type
, drro
->drr_blksz
,
1554 drro
->drr_bonustype
, drro
->drr_bonuslen
, tx
);
1555 } else if (drro
->drr_type
!= doi
.doi_type
||
1556 drro
->drr_blksz
!= doi
.doi_data_block_size
||
1557 drro
->drr_bonustype
!= doi
.doi_bonus_type
||
1558 drro
->drr_bonuslen
!= doi
.doi_bonus_size
) {
1559 /* currently allocated, but with different properties */
1560 err
= dmu_object_reclaim(os
, drro
->drr_object
,
1561 drro
->drr_type
, drro
->drr_blksz
,
1562 drro
->drr_bonustype
, drro
->drr_bonuslen
, tx
);
1566 return (SET_ERROR(EINVAL
));
1569 dmu_object_set_checksum(os
, drro
->drr_object
, drro
->drr_checksumtype
,
1571 dmu_object_set_compress(os
, drro
->drr_object
, drro
->drr_compress
, tx
);
1576 VERIFY(0 == dmu_bonus_hold(os
, drro
->drr_object
, FTAG
, &db
));
1577 dmu_buf_will_dirty(db
, tx
);
1579 ASSERT3U(db
->db_size
, >=, drro
->drr_bonuslen
);
1580 bcopy(data
, db
->db_data
, drro
->drr_bonuslen
);
1582 dmu_object_byteswap_t byteswap
=
1583 DMU_OT_BYTESWAP(drro
->drr_bonustype
);
1584 dmu_ot_byteswap
[byteswap
].ob_func(db
->db_data
,
1585 drro
->drr_bonuslen
);
1587 dmu_buf_rele(db
, FTAG
);
1595 restore_freeobjects(struct restorearg
*ra
, objset_t
*os
,
1596 struct drr_freeobjects
*drrfo
)
1600 if (drrfo
->drr_firstobj
+ drrfo
->drr_numobjs
< drrfo
->drr_firstobj
)
1601 return (SET_ERROR(EINVAL
));
1603 for (obj
= drrfo
->drr_firstobj
;
1604 obj
< drrfo
->drr_firstobj
+ drrfo
->drr_numobjs
;
1605 (void) dmu_object_next(os
, &obj
, FALSE
, 0)) {
1608 if (dmu_object_info(os
, obj
, NULL
) != 0)
1611 err
= dmu_free_long_object(os
, obj
);
1619 restore_write(struct restorearg
*ra
, objset_t
*os
,
1620 struct drr_write
*drrw
)
1628 if (drrw
->drr_offset
+ drrw
->drr_length
< drrw
->drr_offset
||
1629 !DMU_OT_IS_VALID(drrw
->drr_type
))
1630 return (SET_ERROR(EINVAL
));
1632 if (dmu_object_info(os
, drrw
->drr_object
, NULL
) != 0)
1633 return (SET_ERROR(EINVAL
));
1635 if (dmu_bonus_hold(os
, drrw
->drr_object
, FTAG
, &bonus
) != 0)
1636 return (SET_ERROR(EINVAL
));
1638 abuf
= dmu_request_arcbuf(bonus
, drrw
->drr_length
);
1640 data
= restore_read(ra
, drrw
->drr_length
, abuf
->b_data
);
1642 dmu_return_arcbuf(abuf
);
1643 dmu_buf_rele(bonus
, FTAG
);
1647 tx
= dmu_tx_create(os
);
1649 dmu_tx_hold_write(tx
, drrw
->drr_object
,
1650 drrw
->drr_offset
, drrw
->drr_length
);
1651 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1653 dmu_return_arcbuf(abuf
);
1654 dmu_buf_rele(bonus
, FTAG
);
1659 dmu_object_byteswap_t byteswap
=
1660 DMU_OT_BYTESWAP(drrw
->drr_type
);
1661 dmu_ot_byteswap
[byteswap
].ob_func(data
, drrw
->drr_length
);
1663 dmu_assign_arcbuf(bonus
, drrw
->drr_offset
, abuf
, tx
);
1665 dmu_buf_rele(bonus
, FTAG
);
1670 * Handle a DRR_WRITE_BYREF record. This record is used in dedup'ed
1671 * streams to refer to a copy of the data that is already on the
1672 * system because it came in earlier in the stream. This function
1673 * finds the earlier copy of the data, and uses that copy instead of
1674 * data from the stream to fulfill this write.
1677 restore_write_byref(struct restorearg
*ra
, objset_t
*os
,
1678 struct drr_write_byref
*drrwbr
)
1682 guid_map_entry_t gmesrch
;
1683 guid_map_entry_t
*gmep
;
1685 objset_t
*ref_os
= NULL
;
1688 if (drrwbr
->drr_offset
+ drrwbr
->drr_length
< drrwbr
->drr_offset
)
1689 return (SET_ERROR(EINVAL
));
1692 * If the GUID of the referenced dataset is different from the
1693 * GUID of the target dataset, find the referenced dataset.
1695 if (drrwbr
->drr_toguid
!= drrwbr
->drr_refguid
) {
1696 gmesrch
.guid
= drrwbr
->drr_refguid
;
1697 if ((gmep
= avl_find(ra
->guid_to_ds_map
, &gmesrch
,
1699 return (SET_ERROR(EINVAL
));
1701 if (dmu_objset_from_ds(gmep
->gme_ds
, &ref_os
))
1702 return (SET_ERROR(EINVAL
));
1707 err
= dmu_buf_hold(ref_os
, drrwbr
->drr_refobject
,
1708 drrwbr
->drr_refoffset
, FTAG
, &dbp
, DMU_READ_PREFETCH
);
1712 tx
= dmu_tx_create(os
);
1714 dmu_tx_hold_write(tx
, drrwbr
->drr_object
,
1715 drrwbr
->drr_offset
, drrwbr
->drr_length
);
1716 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1721 dmu_write(os
, drrwbr
->drr_object
,
1722 drrwbr
->drr_offset
, drrwbr
->drr_length
, dbp
->db_data
, tx
);
1723 dmu_buf_rele(dbp
, FTAG
);
1729 restore_write_embedded(struct restorearg
*ra
, objset_t
*os
,
1730 struct drr_write_embedded
*drrwnp
)
1736 if (drrwnp
->drr_offset
+ drrwnp
->drr_length
< drrwnp
->drr_offset
)
1739 if (drrwnp
->drr_psize
> BPE_PAYLOAD_SIZE
)
1742 if (drrwnp
->drr_etype
>= NUM_BP_EMBEDDED_TYPES
)
1744 if (drrwnp
->drr_compression
>= ZIO_COMPRESS_FUNCTIONS
)
1747 data
= restore_read(ra
, P2ROUNDUP(drrwnp
->drr_psize
, 8), NULL
);
1751 tx
= dmu_tx_create(os
);
1753 dmu_tx_hold_write(tx
, drrwnp
->drr_object
,
1754 drrwnp
->drr_offset
, drrwnp
->drr_length
);
1755 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1761 dmu_write_embedded(os
, drrwnp
->drr_object
,
1762 drrwnp
->drr_offset
, data
, drrwnp
->drr_etype
,
1763 drrwnp
->drr_compression
, drrwnp
->drr_lsize
, drrwnp
->drr_psize
,
1764 ra
->byteswap
^ ZFS_HOST_BYTEORDER
, tx
);
1771 restore_spill(struct restorearg
*ra
, objset_t
*os
, struct drr_spill
*drrs
)
1775 dmu_buf_t
*db
, *db_spill
;
1778 if (drrs
->drr_length
< SPA_MINBLOCKSIZE
||
1779 drrs
->drr_length
> spa_maxblocksize(dmu_objset_spa(os
)))
1780 return (SET_ERROR(EINVAL
));
1782 data
= restore_read(ra
, drrs
->drr_length
, NULL
);
1786 if (dmu_object_info(os
, drrs
->drr_object
, NULL
) != 0)
1787 return (SET_ERROR(EINVAL
));
1789 VERIFY(0 == dmu_bonus_hold(os
, drrs
->drr_object
, FTAG
, &db
));
1790 if ((err
= dmu_spill_hold_by_bonus(db
, FTAG
, &db_spill
)) != 0) {
1791 dmu_buf_rele(db
, FTAG
);
1795 tx
= dmu_tx_create(os
);
1797 dmu_tx_hold_spill(tx
, db
->db_object
);
1799 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1801 dmu_buf_rele(db
, FTAG
);
1802 dmu_buf_rele(db_spill
, FTAG
);
1806 dmu_buf_will_dirty(db_spill
, tx
);
1808 if (db_spill
->db_size
< drrs
->drr_length
)
1809 VERIFY(0 == dbuf_spill_set_blksz(db_spill
,
1810 drrs
->drr_length
, tx
));
1811 bcopy(data
, db_spill
->db_data
, drrs
->drr_length
);
1813 dmu_buf_rele(db
, FTAG
);
1814 dmu_buf_rele(db_spill
, FTAG
);
1822 restore_free(struct restorearg
*ra
, objset_t
*os
,
1823 struct drr_free
*drrf
)
1827 if (drrf
->drr_length
!= -1ULL &&
1828 drrf
->drr_offset
+ drrf
->drr_length
< drrf
->drr_offset
)
1829 return (SET_ERROR(EINVAL
));
1831 if (dmu_object_info(os
, drrf
->drr_object
, NULL
) != 0)
1832 return (SET_ERROR(EINVAL
));
1834 err
= dmu_free_long_range(os
, drrf
->drr_object
,
1835 drrf
->drr_offset
, drrf
->drr_length
);
1839 /* used to destroy the drc_ds on error */
1841 dmu_recv_cleanup_ds(dmu_recv_cookie_t
*drc
)
1843 char name
[MAXNAMELEN
];
1844 dsl_dataset_name(drc
->drc_ds
, name
);
1845 dsl_dataset_disown(drc
->drc_ds
, dmu_recv_tag
);
1846 (void) dsl_destroy_head(name
);
1850 * NB: callers *must* call dmu_recv_end() if this succeeds.
1853 dmu_recv_stream(dmu_recv_cookie_t
*drc
, vnode_t
*vp
, offset_t
*voffp
,
1854 int cleanup_fd
, uint64_t *action_handlep
)
1856 struct restorearg ra
= { 0 };
1857 dmu_replay_record_t
*drr
;
1862 ra
.byteswap
= drc
->drc_byteswap
;
1863 ra
.cksum
= drc
->drc_cksum
;
1866 ra
.bufsize
= SPA_MAXBLOCKSIZE
;
1867 ra
.buf
= vmem_alloc(ra
.bufsize
, KM_SLEEP
);
1869 /* these were verified in dmu_recv_begin */
1870 ASSERT3U(DMU_GET_STREAM_HDRTYPE(drc
->drc_drrb
->drr_versioninfo
), ==,
1872 ASSERT3U(drc
->drc_drrb
->drr_type
, <, DMU_OST_NUMTYPES
);
1875 * Open the objset we are modifying.
1877 VERIFY0(dmu_objset_from_ds(drc
->drc_ds
, &os
));
1879 ASSERT(dsl_dataset_phys(drc
->drc_ds
)->ds_flags
& DS_FLAG_INCONSISTENT
);
1881 featureflags
= DMU_GET_FEATUREFLAGS(drc
->drc_drrb
->drr_versioninfo
);
1883 /* if this stream is dedup'ed, set up the avl tree for guid mapping */
1884 if (featureflags
& DMU_BACKUP_FEATURE_DEDUP
) {
1887 if (cleanup_fd
== -1) {
1888 ra
.err
= SET_ERROR(EBADF
);
1891 ra
.err
= zfs_onexit_fd_hold(cleanup_fd
, &minor
);
1897 if (*action_handlep
== 0) {
1899 kmem_alloc(sizeof (avl_tree_t
), KM_SLEEP
);
1900 avl_create(ra
.guid_to_ds_map
, guid_compare
,
1901 sizeof (guid_map_entry_t
),
1902 offsetof(guid_map_entry_t
, avlnode
));
1903 ra
.err
= zfs_onexit_add_cb(minor
,
1904 free_guid_map_onexit
, ra
.guid_to_ds_map
,
1909 ra
.err
= zfs_onexit_cb_data(minor
, *action_handlep
,
1910 (void **)&ra
.guid_to_ds_map
);
1915 drc
->drc_guid_to_ds_map
= ra
.guid_to_ds_map
;
1919 * Read records and process them.
1922 while (ra
.err
== 0 &&
1923 NULL
!= (drr
= restore_read(&ra
, sizeof (*drr
), NULL
))) {
1924 if (issig(JUSTLOOKING
) && issig(FORREAL
)) {
1925 ra
.err
= SET_ERROR(EINTR
);
1930 backup_byteswap(drr
);
1932 switch (drr
->drr_type
) {
1936 * We need to make a copy of the record header,
1937 * because restore_{object,write} may need to
1938 * restore_read(), which will invalidate drr.
1940 struct drr_object drro
= drr
->drr_u
.drr_object
;
1941 ra
.err
= restore_object(&ra
, os
, &drro
);
1944 case DRR_FREEOBJECTS
:
1946 struct drr_freeobjects drrfo
=
1947 drr
->drr_u
.drr_freeobjects
;
1948 ra
.err
= restore_freeobjects(&ra
, os
, &drrfo
);
1953 struct drr_write drrw
= drr
->drr_u
.drr_write
;
1954 ra
.err
= restore_write(&ra
, os
, &drrw
);
1957 case DRR_WRITE_BYREF
:
1959 struct drr_write_byref drrwbr
=
1960 drr
->drr_u
.drr_write_byref
;
1961 ra
.err
= restore_write_byref(&ra
, os
, &drrwbr
);
1964 case DRR_WRITE_EMBEDDED
:
1966 struct drr_write_embedded drrwe
=
1967 drr
->drr_u
.drr_write_embedded
;
1968 ra
.err
= restore_write_embedded(&ra
, os
, &drrwe
);
1973 struct drr_free drrf
= drr
->drr_u
.drr_free
;
1974 ra
.err
= restore_free(&ra
, os
, &drrf
);
1979 struct drr_end drre
= drr
->drr_u
.drr_end
;
1981 * We compare against the *previous* checksum
1982 * value, because the stored checksum is of
1983 * everything before the DRR_END record.
1985 if (!ZIO_CHECKSUM_EQUAL(drre
.drr_checksum
, pcksum
))
1986 ra
.err
= SET_ERROR(ECKSUM
);
1991 struct drr_spill drrs
= drr
->drr_u
.drr_spill
;
1992 ra
.err
= restore_spill(&ra
, os
, &drrs
);
1996 ra
.err
= SET_ERROR(EINVAL
);
2001 ASSERT(ra
.err
!= 0);
2004 if ((featureflags
& DMU_BACKUP_FEATURE_DEDUP
) && (cleanup_fd
!= -1))
2005 zfs_onexit_fd_rele(cleanup_fd
);
2009 * destroy what we created, so we don't leave it in the
2010 * inconsistent restoring state.
2012 dmu_recv_cleanup_ds(drc
);
2015 vmem_free(ra
.buf
, ra
.bufsize
);
2021 dmu_recv_end_check(void *arg
, dmu_tx_t
*tx
)
2023 dmu_recv_cookie_t
*drc
= arg
;
2024 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
2027 ASSERT3P(drc
->drc_ds
->ds_owner
, ==, dmu_recv_tag
);
2029 if (!drc
->drc_newfs
) {
2030 dsl_dataset_t
*origin_head
;
2032 error
= dsl_dataset_hold(dp
, drc
->drc_tofs
, FTAG
, &origin_head
);
2035 if (drc
->drc_force
) {
2037 * We will destroy any snapshots in tofs (i.e. before
2038 * origin_head) that are after the origin (which is
2039 * the snap before drc_ds, because drc_ds can not
2040 * have any snaps of its own).
2044 obj
= dsl_dataset_phys(origin_head
)->ds_prev_snap_obj
;
2046 dsl_dataset_phys(drc
->drc_ds
)->ds_prev_snap_obj
) {
2047 dsl_dataset_t
*snap
;
2048 error
= dsl_dataset_hold_obj(dp
, obj
, FTAG
,
2052 if (snap
->ds_dir
!= origin_head
->ds_dir
)
2053 error
= SET_ERROR(EINVAL
);
2055 error
= dsl_destroy_snapshot_check_impl(
2058 obj
= dsl_dataset_phys(snap
)->ds_prev_snap_obj
;
2059 dsl_dataset_rele(snap
, FTAG
);
2064 dsl_dataset_rele(origin_head
, FTAG
);
2068 error
= dsl_dataset_clone_swap_check_impl(drc
->drc_ds
,
2069 origin_head
, drc
->drc_force
, drc
->drc_owner
, tx
);
2071 dsl_dataset_rele(origin_head
, FTAG
);
2074 error
= dsl_dataset_snapshot_check_impl(origin_head
,
2075 drc
->drc_tosnap
, tx
, B_TRUE
, 1, drc
->drc_cred
);
2076 dsl_dataset_rele(origin_head
, FTAG
);
2080 error
= dsl_destroy_head_check_impl(drc
->drc_ds
, 1);
2082 error
= dsl_dataset_snapshot_check_impl(drc
->drc_ds
,
2083 drc
->drc_tosnap
, tx
, B_TRUE
, 1, drc
->drc_cred
);
2089 dmu_recv_end_sync(void *arg
, dmu_tx_t
*tx
)
2091 dmu_recv_cookie_t
*drc
= arg
;
2092 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
2094 spa_history_log_internal_ds(drc
->drc_ds
, "finish receiving",
2095 tx
, "snap=%s", drc
->drc_tosnap
);
2097 if (!drc
->drc_newfs
) {
2098 dsl_dataset_t
*origin_head
;
2100 VERIFY0(dsl_dataset_hold(dp
, drc
->drc_tofs
, FTAG
,
2103 if (drc
->drc_force
) {
2105 * Destroy any snapshots of drc_tofs (origin_head)
2106 * after the origin (the snap before drc_ds).
2110 obj
= dsl_dataset_phys(origin_head
)->ds_prev_snap_obj
;
2112 dsl_dataset_phys(drc
->drc_ds
)->ds_prev_snap_obj
) {
2113 dsl_dataset_t
*snap
;
2114 VERIFY0(dsl_dataset_hold_obj(dp
, obj
, FTAG
,
2116 ASSERT3P(snap
->ds_dir
, ==, origin_head
->ds_dir
);
2117 obj
= dsl_dataset_phys(snap
)->ds_prev_snap_obj
;
2118 dsl_destroy_snapshot_sync_impl(snap
,
2120 dsl_dataset_rele(snap
, FTAG
);
2123 VERIFY3P(drc
->drc_ds
->ds_prev
, ==,
2124 origin_head
->ds_prev
);
2126 dsl_dataset_clone_swap_sync_impl(drc
->drc_ds
,
2128 dsl_dataset_snapshot_sync_impl(origin_head
,
2129 drc
->drc_tosnap
, tx
);
2131 /* set snapshot's creation time and guid */
2132 dmu_buf_will_dirty(origin_head
->ds_prev
->ds_dbuf
, tx
);
2133 dsl_dataset_phys(origin_head
->ds_prev
)->ds_creation_time
=
2134 drc
->drc_drrb
->drr_creation_time
;
2135 dsl_dataset_phys(origin_head
->ds_prev
)->ds_guid
=
2136 drc
->drc_drrb
->drr_toguid
;
2137 dsl_dataset_phys(origin_head
->ds_prev
)->ds_flags
&=
2138 ~DS_FLAG_INCONSISTENT
;
2140 dmu_buf_will_dirty(origin_head
->ds_dbuf
, tx
);
2141 dsl_dataset_phys(origin_head
)->ds_flags
&=
2142 ~DS_FLAG_INCONSISTENT
;
2144 dsl_dataset_rele(origin_head
, FTAG
);
2145 dsl_destroy_head_sync_impl(drc
->drc_ds
, tx
);
2147 if (drc
->drc_owner
!= NULL
)
2148 VERIFY3P(origin_head
->ds_owner
, ==, drc
->drc_owner
);
2150 dsl_dataset_t
*ds
= drc
->drc_ds
;
2152 dsl_dataset_snapshot_sync_impl(ds
, drc
->drc_tosnap
, tx
);
2154 /* set snapshot's creation time and guid */
2155 dmu_buf_will_dirty(ds
->ds_prev
->ds_dbuf
, tx
);
2156 dsl_dataset_phys(ds
->ds_prev
)->ds_creation_time
=
2157 drc
->drc_drrb
->drr_creation_time
;
2158 dsl_dataset_phys(ds
->ds_prev
)->ds_guid
=
2159 drc
->drc_drrb
->drr_toguid
;
2160 dsl_dataset_phys(ds
->ds_prev
)->ds_flags
&=
2161 ~DS_FLAG_INCONSISTENT
;
2163 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
2164 dsl_dataset_phys(ds
)->ds_flags
&= ~DS_FLAG_INCONSISTENT
;
2166 drc
->drc_newsnapobj
= dsl_dataset_phys(drc
->drc_ds
)->ds_prev_snap_obj
;
2167 zvol_create_minors(dp
->dp_spa
, drc
->drc_tofs
, B_TRUE
);
2169 * Release the hold from dmu_recv_begin. This must be done before
2170 * we return to open context, so that when we free the dataset's dnode,
2171 * we can evict its bonus buffer.
2173 dsl_dataset_disown(drc
->drc_ds
, dmu_recv_tag
);
2178 add_ds_to_guidmap(const char *name
, avl_tree_t
*guid_map
, uint64_t snapobj
)
2181 dsl_dataset_t
*snapds
;
2182 guid_map_entry_t
*gmep
;
2185 ASSERT(guid_map
!= NULL
);
2187 err
= dsl_pool_hold(name
, FTAG
, &dp
);
2190 gmep
= kmem_alloc(sizeof (*gmep
), KM_SLEEP
);
2191 err
= dsl_dataset_hold_obj(dp
, snapobj
, gmep
, &snapds
);
2193 gmep
->guid
= dsl_dataset_phys(snapds
)->ds_guid
;
2194 gmep
->gme_ds
= snapds
;
2195 avl_add(guid_map
, gmep
);
2196 dsl_dataset_long_hold(snapds
, gmep
);
2198 kmem_free(gmep
, sizeof (*gmep
));
2201 dsl_pool_rele(dp
, FTAG
);
2205 static int dmu_recv_end_modified_blocks
= 3;
2208 dmu_recv_existing_end(dmu_recv_cookie_t
*drc
)
2216 * We will be destroying the ds; make sure its origin is unmounted if
2219 name
= kmem_alloc(MAXNAMELEN
, KM_SLEEP
);
2220 dsl_dataset_name(drc
->drc_ds
, name
);
2221 zfs_destroy_unmount_origin(name
);
2222 kmem_free(name
, MAXNAMELEN
);
2225 error
= dsl_sync_task(drc
->drc_tofs
,
2226 dmu_recv_end_check
, dmu_recv_end_sync
, drc
,
2227 dmu_recv_end_modified_blocks
, ZFS_SPACE_CHECK_NORMAL
);
2230 dmu_recv_cleanup_ds(drc
);
2235 dmu_recv_new_end(dmu_recv_cookie_t
*drc
)
2239 error
= dsl_sync_task(drc
->drc_tofs
,
2240 dmu_recv_end_check
, dmu_recv_end_sync
, drc
,
2241 dmu_recv_end_modified_blocks
, ZFS_SPACE_CHECK_NORMAL
);
2244 dmu_recv_cleanup_ds(drc
);
2245 } else if (drc
->drc_guid_to_ds_map
!= NULL
) {
2246 (void) add_ds_to_guidmap(drc
->drc_tofs
,
2247 drc
->drc_guid_to_ds_map
,
2248 drc
->drc_newsnapobj
);
2254 dmu_recv_end(dmu_recv_cookie_t
*drc
, void *owner
)
2256 drc
->drc_owner
= owner
;
2259 return (dmu_recv_new_end(drc
));
2261 return (dmu_recv_existing_end(drc
));
2265 * Return TRUE if this objset is currently being received into.
2268 dmu_objset_is_receiving(objset_t
*os
)
2270 return (os
->os_dsl_dataset
!= NULL
&&
2271 os
->os_dsl_dataset
->ds_owner
== dmu_recv_tag
);
2274 #if defined(_KERNEL)
2275 module_param(zfs_send_corrupt_data
, int, 0644);
2276 MODULE_PARM_DESC(zfs_send_corrupt_data
, "Allow sending corrupt data");