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.
30 #include <sys/dmu_impl.h>
31 #include <sys/dmu_tx.h>
33 #include <sys/dnode.h>
34 #include <sys/zfs_context.h>
35 #include <sys/dmu_objset.h>
36 #include <sys/dmu_traverse.h>
37 #include <sys/dsl_dataset.h>
38 #include <sys/dsl_dir.h>
39 #include <sys/dsl_prop.h>
40 #include <sys/dsl_pool.h>
41 #include <sys/dsl_synctask.h>
42 #include <sys/spa_impl.h>
43 #include <sys/zfs_ioctl.h>
45 #include <sys/zio_checksum.h>
46 #include <sys/zfs_znode.h>
47 #include <zfs_fletcher.h>
50 #include <sys/zfs_onexit.h>
51 #include <sys/dmu_send.h>
52 #include <sys/dsl_destroy.h>
53 #include <sys/blkptr.h>
54 #include <sys/dsl_bookmark.h>
55 #include <sys/zfeature.h>
57 /* Set this tunable to TRUE to replace corrupt data with 0x2f5baddb10c */
58 int zfs_send_corrupt_data
= B_FALSE
;
60 static char *dmu_recv_tag
= "dmu_recv_tag";
61 static const char *recv_clone_name
= "%recv";
63 typedef struct dump_bytes_io
{
64 dmu_sendarg_t
*dbi_dsp
;
70 dump_bytes_strategy(void *arg
)
72 dump_bytes_io_t
*dbi
= (dump_bytes_io_t
*)arg
;
73 dmu_sendarg_t
*dsp
= dbi
->dbi_dsp
;
74 dsl_dataset_t
*ds
= dsp
->dsa_os
->os_dsl_dataset
;
75 ssize_t resid
; /* have to get resid to get detailed errno */
76 ASSERT0(dbi
->dbi_len
% 8);
78 fletcher_4_incremental_native(dbi
->dbi_buf
, dbi
->dbi_len
, &dsp
->dsa_zc
);
79 dsp
->dsa_err
= vn_rdwr(UIO_WRITE
, dsp
->dsa_vp
,
80 (caddr_t
)dbi
->dbi_buf
, dbi
->dbi_len
,
81 0, UIO_SYSSPACE
, FAPPEND
, RLIM64_INFINITY
, CRED(), &resid
);
83 mutex_enter(&ds
->ds_sendstream_lock
);
84 *dsp
->dsa_off
+= dbi
->dbi_len
;
85 mutex_exit(&ds
->ds_sendstream_lock
);
89 dump_bytes(dmu_sendarg_t
*dsp
, void *buf
, int len
)
98 * The vn_rdwr() call is performed in a taskq to ensure that there is
99 * always enough stack space to write safely to the target filesystem.
100 * The ZIO_TYPE_FREE threads are used because there can be a lot of
101 * them and they are used in vdev_file.c for a similar purpose.
103 spa_taskq_dispatch_sync(dmu_objset_spa(dsp
->dsa_os
), ZIO_TYPE_FREE
,
104 ZIO_TASKQ_ISSUE
, dump_bytes_strategy
, &dbi
, TQ_SLEEP
);
106 return (dsp
->dsa_err
);
110 dump_free(dmu_sendarg_t
*dsp
, uint64_t object
, uint64_t offset
,
113 struct drr_free
*drrf
= &(dsp
->dsa_drr
->drr_u
.drr_free
);
116 * When we receive a free record, dbuf_free_range() assumes
117 * that the receiving system doesn't have any dbufs in the range
118 * being freed. This is always true because there is a one-record
119 * constraint: we only send one WRITE record for any given
120 * object+offset. We know that the one-record constraint is
121 * true because we always send data in increasing order by
124 * If the increasing-order constraint ever changes, we should find
125 * another way to assert that the one-record constraint is still
128 ASSERT(object
> dsp
->dsa_last_data_object
||
129 (object
== dsp
->dsa_last_data_object
&&
130 offset
> dsp
->dsa_last_data_offset
));
133 * If we are doing a non-incremental send, then there can't
134 * be any data in the dataset we're receiving into. Therefore
135 * a free record would simply be a no-op. Save space by not
136 * sending it to begin with.
138 if (!dsp
->dsa_incremental
)
141 if (length
!= -1ULL && offset
+ length
< offset
)
145 * If there is a pending op, but it's not PENDING_FREE, push it out,
146 * since free block aggregation can only be done for blocks of the
147 * same type (i.e., DRR_FREE records can only be aggregated with
148 * other DRR_FREE records. DRR_FREEOBJECTS records can only be
149 * aggregated with other DRR_FREEOBJECTS records.
151 if (dsp
->dsa_pending_op
!= PENDING_NONE
&&
152 dsp
->dsa_pending_op
!= PENDING_FREE
) {
153 if (dump_bytes(dsp
, dsp
->dsa_drr
,
154 sizeof (dmu_replay_record_t
)) != 0)
155 return (SET_ERROR(EINTR
));
156 dsp
->dsa_pending_op
= PENDING_NONE
;
159 if (dsp
->dsa_pending_op
== PENDING_FREE
) {
161 * There should never be a PENDING_FREE if length is -1
162 * (because dump_dnode is the only place where this
163 * function is called with a -1, and only after flushing
164 * any pending record).
166 ASSERT(length
!= -1ULL);
168 * Check to see whether this free block can be aggregated
171 if (drrf
->drr_object
== object
&& drrf
->drr_offset
+
172 drrf
->drr_length
== offset
) {
173 drrf
->drr_length
+= length
;
176 /* not a continuation. Push out pending record */
177 if (dump_bytes(dsp
, dsp
->dsa_drr
,
178 sizeof (dmu_replay_record_t
)) != 0)
179 return (SET_ERROR(EINTR
));
180 dsp
->dsa_pending_op
= PENDING_NONE
;
183 /* create a FREE record and make it pending */
184 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
185 dsp
->dsa_drr
->drr_type
= DRR_FREE
;
186 drrf
->drr_object
= object
;
187 drrf
->drr_offset
= offset
;
188 drrf
->drr_length
= length
;
189 drrf
->drr_toguid
= dsp
->dsa_toguid
;
190 if (length
== -1ULL) {
191 if (dump_bytes(dsp
, dsp
->dsa_drr
,
192 sizeof (dmu_replay_record_t
)) != 0)
193 return (SET_ERROR(EINTR
));
195 dsp
->dsa_pending_op
= PENDING_FREE
;
202 dump_write(dmu_sendarg_t
*dsp
, dmu_object_type_t type
,
203 uint64_t object
, uint64_t offset
, int blksz
, const blkptr_t
*bp
, void *data
)
205 struct drr_write
*drrw
= &(dsp
->dsa_drr
->drr_u
.drr_write
);
208 * We send data in increasing object, offset order.
209 * See comment in dump_free() for details.
211 ASSERT(object
> dsp
->dsa_last_data_object
||
212 (object
== dsp
->dsa_last_data_object
&&
213 offset
> dsp
->dsa_last_data_offset
));
214 dsp
->dsa_last_data_object
= object
;
215 dsp
->dsa_last_data_offset
= offset
+ blksz
- 1;
218 * If there is any kind of pending aggregation (currently either
219 * a grouping of free objects or free blocks), push it out to
220 * the stream, since aggregation can't be done across operations
221 * of different types.
223 if (dsp
->dsa_pending_op
!= PENDING_NONE
) {
224 if (dump_bytes(dsp
, dsp
->dsa_drr
,
225 sizeof (dmu_replay_record_t
)) != 0)
226 return (SET_ERROR(EINTR
));
227 dsp
->dsa_pending_op
= PENDING_NONE
;
229 /* write a DATA record */
230 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
231 dsp
->dsa_drr
->drr_type
= DRR_WRITE
;
232 drrw
->drr_object
= object
;
233 drrw
->drr_type
= type
;
234 drrw
->drr_offset
= offset
;
235 drrw
->drr_length
= blksz
;
236 drrw
->drr_toguid
= dsp
->dsa_toguid
;
237 if (bp
== NULL
|| BP_IS_EMBEDDED(bp
)) {
239 * There's no pre-computed checksum for partial-block
240 * writes or embedded BP's, so (like
241 * fletcher4-checkummed blocks) userland will have to
242 * compute a dedup-capable checksum itself.
244 drrw
->drr_checksumtype
= ZIO_CHECKSUM_OFF
;
246 drrw
->drr_checksumtype
= BP_GET_CHECKSUM(bp
);
247 if (zio_checksum_table
[drrw
->drr_checksumtype
].ci_dedup
)
248 drrw
->drr_checksumflags
|= DRR_CHECKSUM_DEDUP
;
249 DDK_SET_LSIZE(&drrw
->drr_key
, BP_GET_LSIZE(bp
));
250 DDK_SET_PSIZE(&drrw
->drr_key
, BP_GET_PSIZE(bp
));
251 DDK_SET_COMPRESS(&drrw
->drr_key
, BP_GET_COMPRESS(bp
));
252 drrw
->drr_key
.ddk_cksum
= bp
->blk_cksum
;
255 if (dump_bytes(dsp
, dsp
->dsa_drr
, sizeof (dmu_replay_record_t
)) != 0)
256 return (SET_ERROR(EINTR
));
257 if (dump_bytes(dsp
, data
, blksz
) != 0)
258 return (SET_ERROR(EINTR
));
263 dump_write_embedded(dmu_sendarg_t
*dsp
, uint64_t object
, uint64_t offset
,
264 int blksz
, const blkptr_t
*bp
)
266 char buf
[BPE_PAYLOAD_SIZE
];
267 struct drr_write_embedded
*drrw
=
268 &(dsp
->dsa_drr
->drr_u
.drr_write_embedded
);
270 if (dsp
->dsa_pending_op
!= PENDING_NONE
) {
271 if (dump_bytes(dsp
, dsp
->dsa_drr
,
272 sizeof (dmu_replay_record_t
)) != 0)
274 dsp
->dsa_pending_op
= PENDING_NONE
;
277 ASSERT(BP_IS_EMBEDDED(bp
));
279 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
280 dsp
->dsa_drr
->drr_type
= DRR_WRITE_EMBEDDED
;
281 drrw
->drr_object
= object
;
282 drrw
->drr_offset
= offset
;
283 drrw
->drr_length
= blksz
;
284 drrw
->drr_toguid
= dsp
->dsa_toguid
;
285 drrw
->drr_compression
= BP_GET_COMPRESS(bp
);
286 drrw
->drr_etype
= BPE_GET_ETYPE(bp
);
287 drrw
->drr_lsize
= BPE_GET_LSIZE(bp
);
288 drrw
->drr_psize
= BPE_GET_PSIZE(bp
);
290 decode_embedded_bp_compressed(bp
, buf
);
292 if (dump_bytes(dsp
, dsp
->dsa_drr
, sizeof (dmu_replay_record_t
)) != 0)
294 if (dump_bytes(dsp
, buf
, P2ROUNDUP(drrw
->drr_psize
, 8)) != 0)
300 dump_spill(dmu_sendarg_t
*dsp
, uint64_t object
, int blksz
, void *data
)
302 struct drr_spill
*drrs
= &(dsp
->dsa_drr
->drr_u
.drr_spill
);
304 if (dsp
->dsa_pending_op
!= PENDING_NONE
) {
305 if (dump_bytes(dsp
, dsp
->dsa_drr
,
306 sizeof (dmu_replay_record_t
)) != 0)
307 return (SET_ERROR(EINTR
));
308 dsp
->dsa_pending_op
= PENDING_NONE
;
311 /* write a SPILL record */
312 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
313 dsp
->dsa_drr
->drr_type
= DRR_SPILL
;
314 drrs
->drr_object
= object
;
315 drrs
->drr_length
= blksz
;
316 drrs
->drr_toguid
= dsp
->dsa_toguid
;
318 if (dump_bytes(dsp
, dsp
->dsa_drr
, sizeof (dmu_replay_record_t
)))
319 return (SET_ERROR(EINTR
));
320 if (dump_bytes(dsp
, data
, blksz
))
321 return (SET_ERROR(EINTR
));
326 dump_freeobjects(dmu_sendarg_t
*dsp
, uint64_t firstobj
, uint64_t numobjs
)
328 struct drr_freeobjects
*drrfo
= &(dsp
->dsa_drr
->drr_u
.drr_freeobjects
);
330 /* See comment in dump_free(). */
331 if (!dsp
->dsa_incremental
)
335 * If there is a pending op, but it's not PENDING_FREEOBJECTS,
336 * push it out, since free block aggregation can only be done for
337 * blocks of the same type (i.e., DRR_FREE records can only be
338 * aggregated with other DRR_FREE records. DRR_FREEOBJECTS records
339 * can only be aggregated with other DRR_FREEOBJECTS records.
341 if (dsp
->dsa_pending_op
!= PENDING_NONE
&&
342 dsp
->dsa_pending_op
!= PENDING_FREEOBJECTS
) {
343 if (dump_bytes(dsp
, dsp
->dsa_drr
,
344 sizeof (dmu_replay_record_t
)) != 0)
345 return (SET_ERROR(EINTR
));
346 dsp
->dsa_pending_op
= PENDING_NONE
;
348 if (dsp
->dsa_pending_op
== PENDING_FREEOBJECTS
) {
350 * See whether this free object array can be aggregated
353 if (drrfo
->drr_firstobj
+ drrfo
->drr_numobjs
== firstobj
) {
354 drrfo
->drr_numobjs
+= numobjs
;
357 /* can't be aggregated. Push out pending record */
358 if (dump_bytes(dsp
, dsp
->dsa_drr
,
359 sizeof (dmu_replay_record_t
)) != 0)
360 return (SET_ERROR(EINTR
));
361 dsp
->dsa_pending_op
= PENDING_NONE
;
365 /* write a FREEOBJECTS record */
366 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
367 dsp
->dsa_drr
->drr_type
= DRR_FREEOBJECTS
;
368 drrfo
->drr_firstobj
= firstobj
;
369 drrfo
->drr_numobjs
= numobjs
;
370 drrfo
->drr_toguid
= dsp
->dsa_toguid
;
372 dsp
->dsa_pending_op
= PENDING_FREEOBJECTS
;
378 dump_dnode(dmu_sendarg_t
*dsp
, uint64_t object
, dnode_phys_t
*dnp
)
380 struct drr_object
*drro
= &(dsp
->dsa_drr
->drr_u
.drr_object
);
382 if (dnp
== NULL
|| dnp
->dn_type
== DMU_OT_NONE
)
383 return (dump_freeobjects(dsp
, object
, 1));
385 if (dsp
->dsa_pending_op
!= PENDING_NONE
) {
386 if (dump_bytes(dsp
, dsp
->dsa_drr
,
387 sizeof (dmu_replay_record_t
)) != 0)
388 return (SET_ERROR(EINTR
));
389 dsp
->dsa_pending_op
= PENDING_NONE
;
392 /* write an OBJECT record */
393 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
394 dsp
->dsa_drr
->drr_type
= DRR_OBJECT
;
395 drro
->drr_object
= object
;
396 drro
->drr_type
= dnp
->dn_type
;
397 drro
->drr_bonustype
= dnp
->dn_bonustype
;
398 drro
->drr_blksz
= dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
;
399 drro
->drr_bonuslen
= dnp
->dn_bonuslen
;
400 drro
->drr_checksumtype
= dnp
->dn_checksum
;
401 drro
->drr_compress
= dnp
->dn_compress
;
402 drro
->drr_toguid
= dsp
->dsa_toguid
;
404 if (!(dsp
->dsa_featureflags
& DMU_BACKUP_FEATURE_LARGE_BLOCKS
) &&
405 drro
->drr_blksz
> SPA_OLD_MAXBLOCKSIZE
)
406 drro
->drr_blksz
= SPA_OLD_MAXBLOCKSIZE
;
408 if (dump_bytes(dsp
, dsp
->dsa_drr
, sizeof (dmu_replay_record_t
)) != 0)
409 return (SET_ERROR(EINTR
));
411 if (dump_bytes(dsp
, DN_BONUS(dnp
), P2ROUNDUP(dnp
->dn_bonuslen
, 8)) != 0)
412 return (SET_ERROR(EINTR
));
414 /* Free anything past the end of the file. */
415 if (dump_free(dsp
, object
, (dnp
->dn_maxblkid
+ 1) *
416 (dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
), -1ULL) != 0)
417 return (SET_ERROR(EINTR
));
418 if (dsp
->dsa_err
!= 0)
419 return (SET_ERROR(EINTR
));
424 backup_do_embed(dmu_sendarg_t
*dsp
, const blkptr_t
*bp
)
426 if (!BP_IS_EMBEDDED(bp
))
430 * Compression function must be legacy, or explicitly enabled.
432 if ((BP_GET_COMPRESS(bp
) >= ZIO_COMPRESS_LEGACY_FUNCTIONS
&&
433 !(dsp
->dsa_featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA_LZ4
)))
437 * Embed type must be explicitly enabled.
439 switch (BPE_GET_ETYPE(bp
)) {
440 case BP_EMBEDDED_TYPE_DATA
:
441 if (dsp
->dsa_featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA
)
450 #define BP_SPAN(dnp, level) \
451 (((uint64_t)dnp->dn_datablkszsec) << (SPA_MINBLOCKSHIFT + \
452 (level) * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT)))
456 backup_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
457 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
459 dmu_sendarg_t
*dsp
= arg
;
460 dmu_object_type_t type
= bp
? BP_GET_TYPE(bp
) : DMU_OT_NONE
;
463 if (issig(JUSTLOOKING
) && issig(FORREAL
))
464 return (SET_ERROR(EINTR
));
466 if (zb
->zb_object
!= DMU_META_DNODE_OBJECT
&&
467 DMU_OBJECT_IS_SPECIAL(zb
->zb_object
)) {
469 } else if (zb
->zb_level
== ZB_ZIL_LEVEL
) {
471 * If we are sending a non-snapshot (which is allowed on
472 * read-only pools), it may have a ZIL, which must be ignored.
475 } else if (BP_IS_HOLE(bp
) &&
476 zb
->zb_object
== DMU_META_DNODE_OBJECT
) {
477 uint64_t span
= BP_SPAN(dnp
, zb
->zb_level
);
478 uint64_t dnobj
= (zb
->zb_blkid
* span
) >> DNODE_SHIFT
;
479 err
= dump_freeobjects(dsp
, dnobj
, span
>> DNODE_SHIFT
);
480 } else if (BP_IS_HOLE(bp
)) {
481 uint64_t span
= BP_SPAN(dnp
, zb
->zb_level
);
482 err
= dump_free(dsp
, zb
->zb_object
, zb
->zb_blkid
* span
, span
);
483 } else if (zb
->zb_level
> 0 || type
== DMU_OT_OBJSET
) {
485 } else if (type
== DMU_OT_DNODE
) {
488 int blksz
= BP_GET_LSIZE(bp
);
489 uint32_t aflags
= ARC_WAIT
;
492 if (arc_read(NULL
, spa
, bp
, arc_getbuf_func
, &abuf
,
493 ZIO_PRIORITY_ASYNC_READ
, ZIO_FLAG_CANFAIL
,
495 return (SET_ERROR(EIO
));
498 for (i
= 0; i
< blksz
>> DNODE_SHIFT
; i
++) {
499 uint64_t dnobj
= (zb
->zb_blkid
<<
500 (DNODE_BLOCK_SHIFT
- DNODE_SHIFT
)) + i
;
501 err
= dump_dnode(dsp
, dnobj
, blk
+i
);
505 (void) arc_buf_remove_ref(abuf
, &abuf
);
506 } else if (type
== DMU_OT_SA
) {
507 uint32_t aflags
= ARC_WAIT
;
509 int blksz
= BP_GET_LSIZE(bp
);
511 if (arc_read(NULL
, spa
, bp
, arc_getbuf_func
, &abuf
,
512 ZIO_PRIORITY_ASYNC_READ
, ZIO_FLAG_CANFAIL
,
514 return (SET_ERROR(EIO
));
516 err
= dump_spill(dsp
, zb
->zb_object
, blksz
, abuf
->b_data
);
517 (void) arc_buf_remove_ref(abuf
, &abuf
);
518 } else if (backup_do_embed(dsp
, bp
)) {
519 /* it's an embedded level-0 block of a regular object */
520 int blksz
= dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
;
521 err
= dump_write_embedded(dsp
, zb
->zb_object
,
522 zb
->zb_blkid
* blksz
, blksz
, bp
);
523 } else { /* it's a level-0 block of a regular object */
524 uint32_t aflags
= ARC_WAIT
;
527 int blksz
= BP_GET_LSIZE(bp
);
529 ASSERT3U(blksz
, ==, dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
);
530 ASSERT0(zb
->zb_level
);
531 if (arc_read(NULL
, spa
, bp
, arc_getbuf_func
, &abuf
,
532 ZIO_PRIORITY_ASYNC_READ
, ZIO_FLAG_CANFAIL
,
534 if (zfs_send_corrupt_data
) {
536 /* Send a block filled with 0x"zfs badd bloc" */
537 abuf
= arc_buf_alloc(spa
, blksz
, &abuf
,
539 for (ptr
= abuf
->b_data
;
540 (char *)ptr
< (char *)abuf
->b_data
+ blksz
;
542 *ptr
= 0x2f5baddb10cULL
;
544 return (SET_ERROR(EIO
));
548 offset
= zb
->zb_blkid
* blksz
;
550 if (!(dsp
->dsa_featureflags
&
551 DMU_BACKUP_FEATURE_LARGE_BLOCKS
) &&
552 blksz
> SPA_OLD_MAXBLOCKSIZE
) {
553 char *buf
= abuf
->b_data
;
554 while (blksz
> 0 && err
== 0) {
555 int n
= MIN(blksz
, SPA_OLD_MAXBLOCKSIZE
);
556 err
= dump_write(dsp
, type
, zb
->zb_object
,
557 offset
, n
, NULL
, buf
);
563 err
= dump_write(dsp
, type
, zb
->zb_object
,
564 offset
, blksz
, bp
, abuf
->b_data
);
566 (void) arc_buf_remove_ref(abuf
, &abuf
);
569 ASSERT(err
== 0 || err
== EINTR
);
574 * Releases dp using the specified tag.
577 dmu_send_impl(void *tag
, dsl_pool_t
*dp
, dsl_dataset_t
*ds
,
578 zfs_bookmark_phys_t
*fromzb
, boolean_t is_clone
, boolean_t embedok
,
579 boolean_t large_block_ok
, int outfd
, vnode_t
*vp
, offset_t
*off
)
582 dmu_replay_record_t
*drr
;
585 uint64_t fromtxg
= 0;
586 uint64_t featureflags
= 0;
588 err
= dmu_objset_from_ds(ds
, &os
);
590 dsl_pool_rele(dp
, tag
);
594 drr
= kmem_zalloc(sizeof (dmu_replay_record_t
), KM_SLEEP
);
595 drr
->drr_type
= DRR_BEGIN
;
596 drr
->drr_u
.drr_begin
.drr_magic
= DMU_BACKUP_MAGIC
;
597 DMU_SET_STREAM_HDRTYPE(drr
->drr_u
.drr_begin
.drr_versioninfo
,
601 if (dmu_objset_type(os
) == DMU_OST_ZFS
) {
603 if (zfs_get_zplprop(os
, ZFS_PROP_VERSION
, &version
) != 0) {
604 kmem_free(drr
, sizeof (dmu_replay_record_t
));
605 dsl_pool_rele(dp
, tag
);
606 return (SET_ERROR(EINVAL
));
608 if (version
>= ZPL_VERSION_SA
) {
609 featureflags
|= DMU_BACKUP_FEATURE_SA_SPILL
;
614 if (large_block_ok
&& ds
->ds_large_blocks
)
615 featureflags
|= DMU_BACKUP_FEATURE_LARGE_BLOCKS
;
617 spa_feature_is_active(dp
->dp_spa
, SPA_FEATURE_EMBEDDED_DATA
)) {
618 featureflags
|= DMU_BACKUP_FEATURE_EMBED_DATA
;
619 if (spa_feature_is_active(dp
->dp_spa
, SPA_FEATURE_LZ4_COMPRESS
))
620 featureflags
|= DMU_BACKUP_FEATURE_EMBED_DATA_LZ4
;
625 DMU_SET_FEATUREFLAGS(drr
->drr_u
.drr_begin
.drr_versioninfo
,
628 drr
->drr_u
.drr_begin
.drr_creation_time
=
629 dsl_dataset_phys(ds
)->ds_creation_time
;
630 drr
->drr_u
.drr_begin
.drr_type
= dmu_objset_type(os
);
632 drr
->drr_u
.drr_begin
.drr_flags
|= DRR_FLAG_CLONE
;
633 drr
->drr_u
.drr_begin
.drr_toguid
= dsl_dataset_phys(ds
)->ds_guid
;
634 if (dsl_dataset_phys(ds
)->ds_flags
& DS_FLAG_CI_DATASET
)
635 drr
->drr_u
.drr_begin
.drr_flags
|= DRR_FLAG_CI_DATA
;
637 if (fromzb
!= NULL
) {
638 drr
->drr_u
.drr_begin
.drr_fromguid
= fromzb
->zbm_guid
;
639 fromtxg
= fromzb
->zbm_creation_txg
;
641 dsl_dataset_name(ds
, drr
->drr_u
.drr_begin
.drr_toname
);
642 if (!ds
->ds_is_snapshot
) {
643 (void) strlcat(drr
->drr_u
.drr_begin
.drr_toname
, "@--head--",
644 sizeof (drr
->drr_u
.drr_begin
.drr_toname
));
647 dsp
= kmem_zalloc(sizeof (dmu_sendarg_t
), KM_SLEEP
);
651 dsp
->dsa_outfd
= outfd
;
652 dsp
->dsa_proc
= curproc
;
655 dsp
->dsa_toguid
= dsl_dataset_phys(ds
)->ds_guid
;
656 ZIO_SET_CHECKSUM(&dsp
->dsa_zc
, 0, 0, 0, 0);
657 dsp
->dsa_pending_op
= PENDING_NONE
;
658 dsp
->dsa_incremental
= (fromzb
!= NULL
);
659 dsp
->dsa_featureflags
= featureflags
;
661 mutex_enter(&ds
->ds_sendstream_lock
);
662 list_insert_head(&ds
->ds_sendstreams
, dsp
);
663 mutex_exit(&ds
->ds_sendstream_lock
);
665 dsl_dataset_long_hold(ds
, FTAG
);
666 dsl_pool_rele(dp
, tag
);
668 if (dump_bytes(dsp
, drr
, sizeof (dmu_replay_record_t
)) != 0) {
673 err
= traverse_dataset(ds
, fromtxg
, TRAVERSE_PRE
| TRAVERSE_PREFETCH
,
676 if (dsp
->dsa_pending_op
!= PENDING_NONE
)
677 if (dump_bytes(dsp
, drr
, sizeof (dmu_replay_record_t
)) != 0)
678 err
= SET_ERROR(EINTR
);
681 if (err
== EINTR
&& dsp
->dsa_err
!= 0)
686 bzero(drr
, sizeof (dmu_replay_record_t
));
687 drr
->drr_type
= DRR_END
;
688 drr
->drr_u
.drr_end
.drr_checksum
= dsp
->dsa_zc
;
689 drr
->drr_u
.drr_end
.drr_toguid
= dsp
->dsa_toguid
;
691 if (dump_bytes(dsp
, drr
, sizeof (dmu_replay_record_t
)) != 0) {
697 mutex_enter(&ds
->ds_sendstream_lock
);
698 list_remove(&ds
->ds_sendstreams
, dsp
);
699 mutex_exit(&ds
->ds_sendstream_lock
);
701 kmem_free(drr
, sizeof (dmu_replay_record_t
));
702 kmem_free(dsp
, sizeof (dmu_sendarg_t
));
704 dsl_dataset_long_rele(ds
, FTAG
);
710 dmu_send_obj(const char *pool
, uint64_t tosnap
, uint64_t fromsnap
,
711 boolean_t embedok
, boolean_t large_block_ok
,
712 int outfd
, vnode_t
*vp
, offset_t
*off
)
716 dsl_dataset_t
*fromds
= NULL
;
719 err
= dsl_pool_hold(pool
, FTAG
, &dp
);
723 err
= dsl_dataset_hold_obj(dp
, tosnap
, FTAG
, &ds
);
725 dsl_pool_rele(dp
, FTAG
);
730 zfs_bookmark_phys_t zb
;
733 err
= dsl_dataset_hold_obj(dp
, fromsnap
, FTAG
, &fromds
);
735 dsl_dataset_rele(ds
, FTAG
);
736 dsl_pool_rele(dp
, FTAG
);
739 if (!dsl_dataset_is_before(ds
, fromds
, 0))
740 err
= SET_ERROR(EXDEV
);
741 zb
.zbm_creation_time
=
742 dsl_dataset_phys(fromds
)->ds_creation_time
;
743 zb
.zbm_creation_txg
= dsl_dataset_phys(fromds
)->ds_creation_txg
;
744 zb
.zbm_guid
= dsl_dataset_phys(fromds
)->ds_guid
;
745 is_clone
= (fromds
->ds_dir
!= ds
->ds_dir
);
746 dsl_dataset_rele(fromds
, FTAG
);
747 err
= dmu_send_impl(FTAG
, dp
, ds
, &zb
, is_clone
,
748 embedok
, large_block_ok
, outfd
, vp
, off
);
750 err
= dmu_send_impl(FTAG
, dp
, ds
, NULL
, B_FALSE
,
751 embedok
, large_block_ok
, outfd
, vp
, off
);
753 dsl_dataset_rele(ds
, FTAG
);
758 dmu_send(const char *tosnap
, const char *fromsnap
,
759 boolean_t embedok
, boolean_t large_block_ok
,
760 int outfd
, vnode_t
*vp
, offset_t
*off
)
765 boolean_t owned
= B_FALSE
;
767 if (fromsnap
!= NULL
&& strpbrk(fromsnap
, "@#") == NULL
)
768 return (SET_ERROR(EINVAL
));
770 err
= dsl_pool_hold(tosnap
, FTAG
, &dp
);
774 if (strchr(tosnap
, '@') == NULL
&& spa_writeable(dp
->dp_spa
)) {
776 * We are sending a filesystem or volume. Ensure
777 * that it doesn't change by owning the dataset.
779 err
= dsl_dataset_own(dp
, tosnap
, FTAG
, &ds
);
782 err
= dsl_dataset_hold(dp
, tosnap
, FTAG
, &ds
);
785 dsl_pool_rele(dp
, FTAG
);
789 if (fromsnap
!= NULL
) {
790 zfs_bookmark_phys_t zb
;
791 boolean_t is_clone
= B_FALSE
;
792 int fsnamelen
= strchr(tosnap
, '@') - tosnap
;
795 * If the fromsnap is in a different filesystem, then
796 * mark the send stream as a clone.
798 if (strncmp(tosnap
, fromsnap
, fsnamelen
) != 0 ||
799 (fromsnap
[fsnamelen
] != '@' &&
800 fromsnap
[fsnamelen
] != '#')) {
804 if (strchr(fromsnap
, '@')) {
805 dsl_dataset_t
*fromds
;
806 err
= dsl_dataset_hold(dp
, fromsnap
, FTAG
, &fromds
);
808 if (!dsl_dataset_is_before(ds
, fromds
, 0))
809 err
= SET_ERROR(EXDEV
);
810 zb
.zbm_creation_time
=
811 dsl_dataset_phys(fromds
)->ds_creation_time
;
812 zb
.zbm_creation_txg
=
813 dsl_dataset_phys(fromds
)->ds_creation_txg
;
814 zb
.zbm_guid
= dsl_dataset_phys(fromds
)->ds_guid
;
815 is_clone
= (ds
->ds_dir
!= fromds
->ds_dir
);
816 dsl_dataset_rele(fromds
, FTAG
);
819 err
= dsl_bookmark_lookup(dp
, fromsnap
, ds
, &zb
);
822 dsl_dataset_rele(ds
, FTAG
);
823 dsl_pool_rele(dp
, FTAG
);
826 err
= dmu_send_impl(FTAG
, dp
, ds
, &zb
, is_clone
,
827 embedok
, large_block_ok
, outfd
, vp
, off
);
829 err
= dmu_send_impl(FTAG
, dp
, ds
, NULL
, B_FALSE
,
830 embedok
, large_block_ok
, outfd
, vp
, off
);
833 dsl_dataset_disown(ds
, FTAG
);
835 dsl_dataset_rele(ds
, FTAG
);
840 dmu_send_estimate(dsl_dataset_t
*ds
, dsl_dataset_t
*fromds
, uint64_t *sizep
)
843 uint64_t size
, recordsize
;
844 ASSERTV(dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
);
846 ASSERT(dsl_pool_config_held(dp
));
848 /* tosnap must be a snapshot */
849 if (!ds
->ds_is_snapshot
)
850 return (SET_ERROR(EINVAL
));
853 * fromsnap must be an earlier snapshot from the same fs as tosnap,
854 * or the origin's fs.
856 if (fromds
!= NULL
&& !dsl_dataset_is_before(ds
, fromds
, 0))
857 return (SET_ERROR(EXDEV
));
859 /* Get uncompressed size estimate of changed data. */
860 if (fromds
== NULL
) {
861 size
= dsl_dataset_phys(ds
)->ds_uncompressed_bytes
;
864 err
= dsl_dataset_space_written(fromds
, ds
,
865 &used
, &comp
, &size
);
871 * Assume that space (both on-disk and in-stream) is dominated by
872 * data. We will adjust for indirect blocks and the copies property,
873 * but ignore per-object space used (eg, dnodes and DRR_OBJECT records).
877 * Subtract out approximate space used by indirect blocks.
878 * Assume most space is used by data blocks (non-indirect, non-dnode).
879 * Assume all blocks are recordsize. Assume ditto blocks and
880 * internal fragmentation counter out compression.
882 * Therefore, space used by indirect blocks is sizeof(blkptr_t) per
883 * block, which we observe in practice.
885 err
= dsl_prop_get_int_ds(ds
, "recordsize", &recordsize
);
888 size
-= size
/ recordsize
* sizeof (blkptr_t
);
890 /* Add in the space for the record associated with each block. */
891 size
+= size
/ recordsize
* sizeof (dmu_replay_record_t
);
898 typedef struct dmu_recv_begin_arg
{
899 const char *drba_origin
;
900 dmu_recv_cookie_t
*drba_cookie
;
902 uint64_t drba_snapobj
;
903 } dmu_recv_begin_arg_t
;
906 recv_begin_check_existing_impl(dmu_recv_begin_arg_t
*drba
, dsl_dataset_t
*ds
,
911 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
913 /* temporary clone name must not exist */
914 error
= zap_lookup(dp
->dp_meta_objset
,
915 dsl_dir_phys(ds
->ds_dir
)->dd_child_dir_zapobj
, recv_clone_name
,
918 return (error
== 0 ? EBUSY
: error
);
920 /* new snapshot name must not exist */
921 error
= zap_lookup(dp
->dp_meta_objset
,
922 dsl_dataset_phys(ds
)->ds_snapnames_zapobj
,
923 drba
->drba_cookie
->drc_tosnap
, 8, 1, &val
);
925 return (error
== 0 ? EEXIST
: error
);
928 * Check snapshot limit before receiving. We'll recheck again at the
929 * end, but might as well abort before receiving if we're already over
932 * Note that we do not check the file system limit with
933 * dsl_dir_fscount_check because the temporary %clones don't count
934 * against that limit.
936 error
= dsl_fs_ss_limit_check(ds
->ds_dir
, 1, ZFS_PROP_SNAPSHOT_LIMIT
,
937 NULL
, drba
->drba_cred
);
943 uint64_t obj
= dsl_dataset_phys(ds
)->ds_prev_snap_obj
;
945 /* Find snapshot in this dir that matches fromguid. */
947 error
= dsl_dataset_hold_obj(dp
, obj
, FTAG
,
950 return (SET_ERROR(ENODEV
));
951 if (snap
->ds_dir
!= ds
->ds_dir
) {
952 dsl_dataset_rele(snap
, FTAG
);
953 return (SET_ERROR(ENODEV
));
955 if (dsl_dataset_phys(snap
)->ds_guid
== fromguid
)
957 obj
= dsl_dataset_phys(snap
)->ds_prev_snap_obj
;
958 dsl_dataset_rele(snap
, FTAG
);
961 return (SET_ERROR(ENODEV
));
963 if (drba
->drba_cookie
->drc_force
) {
964 drba
->drba_snapobj
= obj
;
967 * If we are not forcing, there must be no
968 * changes since fromsnap.
970 if (dsl_dataset_modified_since_snap(ds
, snap
)) {
971 dsl_dataset_rele(snap
, FTAG
);
972 return (SET_ERROR(ETXTBSY
));
974 drba
->drba_snapobj
= ds
->ds_prev
->ds_object
;
977 dsl_dataset_rele(snap
, FTAG
);
979 /* if full, most recent snapshot must be $ORIGIN */
980 if (dsl_dataset_phys(ds
)->ds_prev_snap_txg
>= TXG_INITIAL
)
981 return (SET_ERROR(ENODEV
));
982 drba
->drba_snapobj
= dsl_dataset_phys(ds
)->ds_prev_snap_obj
;
990 dmu_recv_begin_check(void *arg
, dmu_tx_t
*tx
)
992 dmu_recv_begin_arg_t
*drba
= arg
;
993 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
994 struct drr_begin
*drrb
= drba
->drba_cookie
->drc_drrb
;
995 uint64_t fromguid
= drrb
->drr_fromguid
;
996 int flags
= drrb
->drr_flags
;
998 uint64_t featureflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
1000 const char *tofs
= drba
->drba_cookie
->drc_tofs
;
1002 /* already checked */
1003 ASSERT3U(drrb
->drr_magic
, ==, DMU_BACKUP_MAGIC
);
1005 if (DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) ==
1006 DMU_COMPOUNDSTREAM
||
1007 drrb
->drr_type
>= DMU_OST_NUMTYPES
||
1008 ((flags
& DRR_FLAG_CLONE
) && drba
->drba_origin
== NULL
))
1009 return (SET_ERROR(EINVAL
));
1011 /* Verify pool version supports SA if SA_SPILL feature set */
1012 if ((featureflags
& DMU_BACKUP_FEATURE_SA_SPILL
) &&
1013 spa_version(dp
->dp_spa
) < SPA_VERSION_SA
)
1014 return (SET_ERROR(ENOTSUP
));
1017 * The receiving code doesn't know how to translate a WRITE_EMBEDDED
1018 * record to a plan WRITE record, so the pool must have the
1019 * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
1020 * records. Same with WRITE_EMBEDDED records that use LZ4 compression.
1022 if ((featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA
) &&
1023 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_EMBEDDED_DATA
))
1024 return (SET_ERROR(ENOTSUP
));
1025 if ((featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA_LZ4
) &&
1026 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_LZ4_COMPRESS
))
1027 return (SET_ERROR(ENOTSUP
));
1030 * The receiving code doesn't know how to translate large blocks
1031 * to smaller ones, so the pool must have the LARGE_BLOCKS
1032 * feature enabled if the stream has LARGE_BLOCKS.
1034 if ((featureflags
& DMU_BACKUP_FEATURE_LARGE_BLOCKS
) &&
1035 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_LARGE_BLOCKS
))
1036 return (SET_ERROR(ENOTSUP
));
1038 error
= dsl_dataset_hold(dp
, tofs
, FTAG
, &ds
);
1040 /* target fs already exists; recv into temp clone */
1042 /* Can't recv a clone into an existing fs */
1043 if (flags
& DRR_FLAG_CLONE
) {
1044 dsl_dataset_rele(ds
, FTAG
);
1045 return (SET_ERROR(EINVAL
));
1048 error
= recv_begin_check_existing_impl(drba
, ds
, fromguid
);
1049 dsl_dataset_rele(ds
, FTAG
);
1050 } else if (error
== ENOENT
) {
1051 /* target fs does not exist; must be a full backup or clone */
1052 char buf
[MAXNAMELEN
];
1055 * If it's a non-clone incremental, we are missing the
1056 * target fs, so fail the recv.
1058 if (fromguid
!= 0 && !(flags
& DRR_FLAG_CLONE
))
1059 return (SET_ERROR(ENOENT
));
1061 /* Open the parent of tofs */
1062 ASSERT3U(strlen(tofs
), <, MAXNAMELEN
);
1063 (void) strlcpy(buf
, tofs
, strrchr(tofs
, '/') - tofs
+ 1);
1064 error
= dsl_dataset_hold(dp
, buf
, FTAG
, &ds
);
1069 * Check filesystem and snapshot limits before receiving. We'll
1070 * recheck snapshot limits again at the end (we create the
1071 * filesystems and increment those counts during begin_sync).
1073 error
= dsl_fs_ss_limit_check(ds
->ds_dir
, 1,
1074 ZFS_PROP_FILESYSTEM_LIMIT
, NULL
, drba
->drba_cred
);
1076 dsl_dataset_rele(ds
, FTAG
);
1080 error
= dsl_fs_ss_limit_check(ds
->ds_dir
, 1,
1081 ZFS_PROP_SNAPSHOT_LIMIT
, NULL
, drba
->drba_cred
);
1083 dsl_dataset_rele(ds
, FTAG
);
1087 if (drba
->drba_origin
!= NULL
) {
1088 dsl_dataset_t
*origin
;
1089 error
= dsl_dataset_hold(dp
, drba
->drba_origin
,
1092 dsl_dataset_rele(ds
, FTAG
);
1095 if (!origin
->ds_is_snapshot
) {
1096 dsl_dataset_rele(origin
, FTAG
);
1097 dsl_dataset_rele(ds
, FTAG
);
1098 return (SET_ERROR(EINVAL
));
1100 if (dsl_dataset_phys(origin
)->ds_guid
!= fromguid
) {
1101 dsl_dataset_rele(origin
, FTAG
);
1102 dsl_dataset_rele(ds
, FTAG
);
1103 return (SET_ERROR(ENODEV
));
1105 dsl_dataset_rele(origin
, FTAG
);
1107 dsl_dataset_rele(ds
, FTAG
);
1114 dmu_recv_begin_sync(void *arg
, dmu_tx_t
*tx
)
1116 dmu_recv_begin_arg_t
*drba
= arg
;
1117 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1118 struct drr_begin
*drrb
= drba
->drba_cookie
->drc_drrb
;
1119 const char *tofs
= drba
->drba_cookie
->drc_tofs
;
1120 dsl_dataset_t
*ds
, *newds
;
1125 crflags
= (drrb
->drr_flags
& DRR_FLAG_CI_DATA
) ?
1126 DS_FLAG_CI_DATASET
: 0;
1128 error
= dsl_dataset_hold(dp
, tofs
, FTAG
, &ds
);
1130 /* create temporary clone */
1131 dsl_dataset_t
*snap
= NULL
;
1132 if (drba
->drba_snapobj
!= 0) {
1133 VERIFY0(dsl_dataset_hold_obj(dp
,
1134 drba
->drba_snapobj
, FTAG
, &snap
));
1136 dsobj
= dsl_dataset_create_sync(ds
->ds_dir
, recv_clone_name
,
1137 snap
, crflags
, drba
->drba_cred
, tx
);
1138 dsl_dataset_rele(snap
, FTAG
);
1139 dsl_dataset_rele(ds
, FTAG
);
1143 dsl_dataset_t
*origin
= NULL
;
1145 VERIFY0(dsl_dir_hold(dp
, tofs
, FTAG
, &dd
, &tail
));
1147 if (drba
->drba_origin
!= NULL
) {
1148 VERIFY0(dsl_dataset_hold(dp
, drba
->drba_origin
,
1152 /* Create new dataset. */
1153 dsobj
= dsl_dataset_create_sync(dd
,
1154 strrchr(tofs
, '/') + 1,
1155 origin
, crflags
, drba
->drba_cred
, tx
);
1157 dsl_dataset_rele(origin
, FTAG
);
1158 dsl_dir_rele(dd
, FTAG
);
1159 drba
->drba_cookie
->drc_newfs
= B_TRUE
;
1161 VERIFY0(dsl_dataset_own_obj(dp
, dsobj
, dmu_recv_tag
, &newds
));
1163 if ((DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
) &
1164 DMU_BACKUP_FEATURE_LARGE_BLOCKS
) &&
1165 !newds
->ds_large_blocks
) {
1166 dsl_dataset_activate_large_blocks_sync_impl(dsobj
, tx
);
1167 newds
->ds_large_blocks
= B_TRUE
;
1170 dmu_buf_will_dirty(newds
->ds_dbuf
, tx
);
1171 dsl_dataset_phys(newds
)->ds_flags
|= DS_FLAG_INCONSISTENT
;
1174 * If we actually created a non-clone, we need to create the
1175 * objset in our new dataset.
1177 if (BP_IS_HOLE(dsl_dataset_get_blkptr(newds
))) {
1178 (void) dmu_objset_create_impl(dp
->dp_spa
,
1179 newds
, dsl_dataset_get_blkptr(newds
), drrb
->drr_type
, tx
);
1182 drba
->drba_cookie
->drc_ds
= newds
;
1184 spa_history_log_internal_ds(newds
, "receive", tx
, "");
1188 * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
1189 * succeeds; otherwise we will leak the holds on the datasets.
1192 dmu_recv_begin(char *tofs
, char *tosnap
, struct drr_begin
*drrb
,
1193 boolean_t force
, char *origin
, dmu_recv_cookie_t
*drc
)
1195 dmu_recv_begin_arg_t drba
= { 0 };
1196 dmu_replay_record_t
*drr
;
1198 bzero(drc
, sizeof (dmu_recv_cookie_t
));
1199 drc
->drc_drrb
= drrb
;
1200 drc
->drc_tosnap
= tosnap
;
1201 drc
->drc_tofs
= tofs
;
1202 drc
->drc_force
= force
;
1203 drc
->drc_cred
= CRED();
1205 if (drrb
->drr_magic
== BSWAP_64(DMU_BACKUP_MAGIC
))
1206 drc
->drc_byteswap
= B_TRUE
;
1207 else if (drrb
->drr_magic
!= DMU_BACKUP_MAGIC
)
1208 return (SET_ERROR(EINVAL
));
1210 drr
= kmem_zalloc(sizeof (dmu_replay_record_t
), KM_SLEEP
);
1211 drr
->drr_type
= DRR_BEGIN
;
1212 drr
->drr_u
.drr_begin
= *drc
->drc_drrb
;
1213 if (drc
->drc_byteswap
) {
1214 fletcher_4_incremental_byteswap(drr
,
1215 sizeof (dmu_replay_record_t
), &drc
->drc_cksum
);
1217 fletcher_4_incremental_native(drr
,
1218 sizeof (dmu_replay_record_t
), &drc
->drc_cksum
);
1220 kmem_free(drr
, sizeof (dmu_replay_record_t
));
1222 if (drc
->drc_byteswap
) {
1223 drrb
->drr_magic
= BSWAP_64(drrb
->drr_magic
);
1224 drrb
->drr_versioninfo
= BSWAP_64(drrb
->drr_versioninfo
);
1225 drrb
->drr_creation_time
= BSWAP_64(drrb
->drr_creation_time
);
1226 drrb
->drr_type
= BSWAP_32(drrb
->drr_type
);
1227 drrb
->drr_toguid
= BSWAP_64(drrb
->drr_toguid
);
1228 drrb
->drr_fromguid
= BSWAP_64(drrb
->drr_fromguid
);
1231 drba
.drba_origin
= origin
;
1232 drba
.drba_cookie
= drc
;
1233 drba
.drba_cred
= CRED();
1235 return (dsl_sync_task(tofs
, dmu_recv_begin_check
, dmu_recv_begin_sync
,
1236 &drba
, 5, ZFS_SPACE_CHECK_NORMAL
));
1245 int bufsize
; /* amount of memory allocated for buf */
1247 avl_tree_t
*guid_to_ds_map
;
1250 typedef struct guid_map_entry
{
1252 dsl_dataset_t
*gme_ds
;
1257 guid_compare(const void *arg1
, const void *arg2
)
1259 const guid_map_entry_t
*gmep1
= arg1
;
1260 const guid_map_entry_t
*gmep2
= arg2
;
1262 if (gmep1
->guid
< gmep2
->guid
)
1264 else if (gmep1
->guid
> gmep2
->guid
)
1270 free_guid_map_onexit(void *arg
)
1272 avl_tree_t
*ca
= arg
;
1273 void *cookie
= NULL
;
1274 guid_map_entry_t
*gmep
;
1276 while ((gmep
= avl_destroy_nodes(ca
, &cookie
)) != NULL
) {
1277 dsl_dataset_long_rele(gmep
->gme_ds
, gmep
);
1278 dsl_dataset_rele(gmep
->gme_ds
, gmep
);
1279 kmem_free(gmep
, sizeof (guid_map_entry_t
));
1282 kmem_free(ca
, sizeof (avl_tree_t
));
1286 restore_read(struct restorearg
*ra
, int len
, char *buf
)
1293 /* some things will require 8-byte alignment, so everything must */
1295 ASSERT3U(len
, <=, ra
->bufsize
);
1297 while (done
< len
) {
1300 ra
->err
= vn_rdwr(UIO_READ
, ra
->vp
,
1301 buf
+ done
, len
- done
,
1302 ra
->voff
, UIO_SYSSPACE
, FAPPEND
,
1303 RLIM64_INFINITY
, CRED(), &resid
);
1305 if (resid
== len
- done
)
1306 ra
->err
= SET_ERROR(EINVAL
);
1307 ra
->voff
+= len
- done
- resid
;
1313 ASSERT3U(done
, ==, len
);
1315 fletcher_4_incremental_byteswap(buf
, len
, &ra
->cksum
);
1317 fletcher_4_incremental_native(buf
, len
, &ra
->cksum
);
1321 noinline
static void
1322 backup_byteswap(dmu_replay_record_t
*drr
)
1324 #define DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
1325 #define DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
1326 drr
->drr_type
= BSWAP_32(drr
->drr_type
);
1327 drr
->drr_payloadlen
= BSWAP_32(drr
->drr_payloadlen
);
1328 switch (drr
->drr_type
) {
1330 DO64(drr_begin
.drr_magic
);
1331 DO64(drr_begin
.drr_versioninfo
);
1332 DO64(drr_begin
.drr_creation_time
);
1333 DO32(drr_begin
.drr_type
);
1334 DO32(drr_begin
.drr_flags
);
1335 DO64(drr_begin
.drr_toguid
);
1336 DO64(drr_begin
.drr_fromguid
);
1339 DO64(drr_object
.drr_object
);
1340 DO32(drr_object
.drr_type
);
1341 DO32(drr_object
.drr_bonustype
);
1342 DO32(drr_object
.drr_blksz
);
1343 DO32(drr_object
.drr_bonuslen
);
1344 DO64(drr_object
.drr_toguid
);
1346 case DRR_FREEOBJECTS
:
1347 DO64(drr_freeobjects
.drr_firstobj
);
1348 DO64(drr_freeobjects
.drr_numobjs
);
1349 DO64(drr_freeobjects
.drr_toguid
);
1352 DO64(drr_write
.drr_object
);
1353 DO32(drr_write
.drr_type
);
1354 DO64(drr_write
.drr_offset
);
1355 DO64(drr_write
.drr_length
);
1356 DO64(drr_write
.drr_toguid
);
1357 DO64(drr_write
.drr_key
.ddk_cksum
.zc_word
[0]);
1358 DO64(drr_write
.drr_key
.ddk_cksum
.zc_word
[1]);
1359 DO64(drr_write
.drr_key
.ddk_cksum
.zc_word
[2]);
1360 DO64(drr_write
.drr_key
.ddk_cksum
.zc_word
[3]);
1361 DO64(drr_write
.drr_key
.ddk_prop
);
1363 case DRR_WRITE_BYREF
:
1364 DO64(drr_write_byref
.drr_object
);
1365 DO64(drr_write_byref
.drr_offset
);
1366 DO64(drr_write_byref
.drr_length
);
1367 DO64(drr_write_byref
.drr_toguid
);
1368 DO64(drr_write_byref
.drr_refguid
);
1369 DO64(drr_write_byref
.drr_refobject
);
1370 DO64(drr_write_byref
.drr_refoffset
);
1371 DO64(drr_write_byref
.drr_key
.ddk_cksum
.zc_word
[0]);
1372 DO64(drr_write_byref
.drr_key
.ddk_cksum
.zc_word
[1]);
1373 DO64(drr_write_byref
.drr_key
.ddk_cksum
.zc_word
[2]);
1374 DO64(drr_write_byref
.drr_key
.ddk_cksum
.zc_word
[3]);
1375 DO64(drr_write_byref
.drr_key
.ddk_prop
);
1377 case DRR_WRITE_EMBEDDED
:
1378 DO64(drr_write_embedded
.drr_object
);
1379 DO64(drr_write_embedded
.drr_offset
);
1380 DO64(drr_write_embedded
.drr_length
);
1381 DO64(drr_write_embedded
.drr_toguid
);
1382 DO32(drr_write_embedded
.drr_lsize
);
1383 DO32(drr_write_embedded
.drr_psize
);
1386 DO64(drr_free
.drr_object
);
1387 DO64(drr_free
.drr_offset
);
1388 DO64(drr_free
.drr_length
);
1389 DO64(drr_free
.drr_toguid
);
1392 DO64(drr_spill
.drr_object
);
1393 DO64(drr_spill
.drr_length
);
1394 DO64(drr_spill
.drr_toguid
);
1397 DO64(drr_end
.drr_checksum
.zc_word
[0]);
1398 DO64(drr_end
.drr_checksum
.zc_word
[1]);
1399 DO64(drr_end
.drr_checksum
.zc_word
[2]);
1400 DO64(drr_end
.drr_checksum
.zc_word
[3]);
1401 DO64(drr_end
.drr_toguid
);
1410 static inline uint8_t
1411 deduce_nblkptr(dmu_object_type_t bonus_type
, uint64_t bonus_size
)
1413 if (bonus_type
== DMU_OT_SA
) {
1417 ((DN_MAX_BONUSLEN
- bonus_size
) >> SPA_BLKPTRSHIFT
));
1422 restore_object(struct restorearg
*ra
, objset_t
*os
, struct drr_object
*drro
)
1424 dmu_object_info_t doi
;
1430 if (drro
->drr_type
== DMU_OT_NONE
||
1431 !DMU_OT_IS_VALID(drro
->drr_type
) ||
1432 !DMU_OT_IS_VALID(drro
->drr_bonustype
) ||
1433 drro
->drr_checksumtype
>= ZIO_CHECKSUM_FUNCTIONS
||
1434 drro
->drr_compress
>= ZIO_COMPRESS_FUNCTIONS
||
1435 P2PHASE(drro
->drr_blksz
, SPA_MINBLOCKSIZE
) ||
1436 drro
->drr_blksz
< SPA_MINBLOCKSIZE
||
1437 drro
->drr_blksz
> spa_maxblocksize(dmu_objset_spa(os
)) ||
1438 drro
->drr_bonuslen
> DN_MAX_BONUSLEN
) {
1439 return (SET_ERROR(EINVAL
));
1442 err
= dmu_object_info(os
, drro
->drr_object
, &doi
);
1444 if (err
!= 0 && err
!= ENOENT
)
1445 return (SET_ERROR(EINVAL
));
1446 object
= err
== 0 ? drro
->drr_object
: DMU_NEW_OBJECT
;
1448 if (drro
->drr_bonuslen
) {
1449 data
= restore_read(ra
, P2ROUNDUP(drro
->drr_bonuslen
, 8), NULL
);
1455 * If we are losing blkptrs or changing the block size this must
1456 * be a new file instance. We must clear out the previous file
1457 * contents before we can change this type of metadata in the dnode.
1462 nblkptr
= deduce_nblkptr(drro
->drr_bonustype
,
1463 drro
->drr_bonuslen
);
1465 if (drro
->drr_blksz
!= doi
.doi_data_block_size
||
1466 nblkptr
< doi
.doi_nblkptr
) {
1467 err
= dmu_free_long_range(os
, drro
->drr_object
,
1470 return (SET_ERROR(EINVAL
));
1474 tx
= dmu_tx_create(os
);
1475 dmu_tx_hold_bonus(tx
, object
);
1476 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1482 if (object
== DMU_NEW_OBJECT
) {
1483 /* currently free, want to be allocated */
1484 err
= dmu_object_claim(os
, drro
->drr_object
,
1485 drro
->drr_type
, drro
->drr_blksz
,
1486 drro
->drr_bonustype
, drro
->drr_bonuslen
, tx
);
1487 } else if (drro
->drr_type
!= doi
.doi_type
||
1488 drro
->drr_blksz
!= doi
.doi_data_block_size
||
1489 drro
->drr_bonustype
!= doi
.doi_bonus_type
||
1490 drro
->drr_bonuslen
!= doi
.doi_bonus_size
) {
1491 /* currently allocated, but with different properties */
1492 err
= dmu_object_reclaim(os
, drro
->drr_object
,
1493 drro
->drr_type
, drro
->drr_blksz
,
1494 drro
->drr_bonustype
, drro
->drr_bonuslen
, tx
);
1498 return (SET_ERROR(EINVAL
));
1501 dmu_object_set_checksum(os
, drro
->drr_object
, drro
->drr_checksumtype
,
1503 dmu_object_set_compress(os
, drro
->drr_object
, drro
->drr_compress
, tx
);
1508 VERIFY(0 == dmu_bonus_hold(os
, drro
->drr_object
, FTAG
, &db
));
1509 dmu_buf_will_dirty(db
, tx
);
1511 ASSERT3U(db
->db_size
, >=, drro
->drr_bonuslen
);
1512 bcopy(data
, db
->db_data
, drro
->drr_bonuslen
);
1514 dmu_object_byteswap_t byteswap
=
1515 DMU_OT_BYTESWAP(drro
->drr_bonustype
);
1516 dmu_ot_byteswap
[byteswap
].ob_func(db
->db_data
,
1517 drro
->drr_bonuslen
);
1519 dmu_buf_rele(db
, FTAG
);
1527 restore_freeobjects(struct restorearg
*ra
, objset_t
*os
,
1528 struct drr_freeobjects
*drrfo
)
1532 if (drrfo
->drr_firstobj
+ drrfo
->drr_numobjs
< drrfo
->drr_firstobj
)
1533 return (SET_ERROR(EINVAL
));
1535 for (obj
= drrfo
->drr_firstobj
;
1536 obj
< drrfo
->drr_firstobj
+ drrfo
->drr_numobjs
;
1537 (void) dmu_object_next(os
, &obj
, FALSE
, 0)) {
1540 if (dmu_object_info(os
, obj
, NULL
) != 0)
1543 err
= dmu_free_long_object(os
, obj
);
1551 restore_write(struct restorearg
*ra
, objset_t
*os
,
1552 struct drr_write
*drrw
)
1560 if (drrw
->drr_offset
+ drrw
->drr_length
< drrw
->drr_offset
||
1561 !DMU_OT_IS_VALID(drrw
->drr_type
))
1562 return (SET_ERROR(EINVAL
));
1564 if (dmu_object_info(os
, drrw
->drr_object
, NULL
) != 0)
1565 return (SET_ERROR(EINVAL
));
1567 if (dmu_bonus_hold(os
, drrw
->drr_object
, FTAG
, &bonus
) != 0)
1568 return (SET_ERROR(EINVAL
));
1570 abuf
= dmu_request_arcbuf(bonus
, drrw
->drr_length
);
1572 data
= restore_read(ra
, drrw
->drr_length
, abuf
->b_data
);
1574 dmu_return_arcbuf(abuf
);
1575 dmu_buf_rele(bonus
, FTAG
);
1579 tx
= dmu_tx_create(os
);
1581 dmu_tx_hold_write(tx
, drrw
->drr_object
,
1582 drrw
->drr_offset
, drrw
->drr_length
);
1583 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1585 dmu_return_arcbuf(abuf
);
1586 dmu_buf_rele(bonus
, FTAG
);
1591 dmu_object_byteswap_t byteswap
=
1592 DMU_OT_BYTESWAP(drrw
->drr_type
);
1593 dmu_ot_byteswap
[byteswap
].ob_func(data
, drrw
->drr_length
);
1595 dmu_assign_arcbuf(bonus
, drrw
->drr_offset
, abuf
, tx
);
1597 dmu_buf_rele(bonus
, FTAG
);
1602 * Handle a DRR_WRITE_BYREF record. This record is used in dedup'ed
1603 * streams to refer to a copy of the data that is already on the
1604 * system because it came in earlier in the stream. This function
1605 * finds the earlier copy of the data, and uses that copy instead of
1606 * data from the stream to fulfill this write.
1609 restore_write_byref(struct restorearg
*ra
, objset_t
*os
,
1610 struct drr_write_byref
*drrwbr
)
1614 guid_map_entry_t gmesrch
;
1615 guid_map_entry_t
*gmep
;
1617 objset_t
*ref_os
= NULL
;
1620 if (drrwbr
->drr_offset
+ drrwbr
->drr_length
< drrwbr
->drr_offset
)
1621 return (SET_ERROR(EINVAL
));
1624 * If the GUID of the referenced dataset is different from the
1625 * GUID of the target dataset, find the referenced dataset.
1627 if (drrwbr
->drr_toguid
!= drrwbr
->drr_refguid
) {
1628 gmesrch
.guid
= drrwbr
->drr_refguid
;
1629 if ((gmep
= avl_find(ra
->guid_to_ds_map
, &gmesrch
,
1631 return (SET_ERROR(EINVAL
));
1633 if (dmu_objset_from_ds(gmep
->gme_ds
, &ref_os
))
1634 return (SET_ERROR(EINVAL
));
1639 err
= dmu_buf_hold(ref_os
, drrwbr
->drr_refobject
,
1640 drrwbr
->drr_refoffset
, FTAG
, &dbp
, DMU_READ_PREFETCH
);
1644 tx
= dmu_tx_create(os
);
1646 dmu_tx_hold_write(tx
, drrwbr
->drr_object
,
1647 drrwbr
->drr_offset
, drrwbr
->drr_length
);
1648 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1653 dmu_write(os
, drrwbr
->drr_object
,
1654 drrwbr
->drr_offset
, drrwbr
->drr_length
, dbp
->db_data
, tx
);
1655 dmu_buf_rele(dbp
, FTAG
);
1661 restore_write_embedded(struct restorearg
*ra
, objset_t
*os
,
1662 struct drr_write_embedded
*drrwnp
)
1668 if (drrwnp
->drr_offset
+ drrwnp
->drr_length
< drrwnp
->drr_offset
)
1671 if (drrwnp
->drr_psize
> BPE_PAYLOAD_SIZE
)
1674 if (drrwnp
->drr_etype
>= NUM_BP_EMBEDDED_TYPES
)
1676 if (drrwnp
->drr_compression
>= ZIO_COMPRESS_FUNCTIONS
)
1679 data
= restore_read(ra
, P2ROUNDUP(drrwnp
->drr_psize
, 8), NULL
);
1683 tx
= dmu_tx_create(os
);
1685 dmu_tx_hold_write(tx
, drrwnp
->drr_object
,
1686 drrwnp
->drr_offset
, drrwnp
->drr_length
);
1687 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1693 dmu_write_embedded(os
, drrwnp
->drr_object
,
1694 drrwnp
->drr_offset
, data
, drrwnp
->drr_etype
,
1695 drrwnp
->drr_compression
, drrwnp
->drr_lsize
, drrwnp
->drr_psize
,
1696 ra
->byteswap
^ ZFS_HOST_BYTEORDER
, tx
);
1703 restore_spill(struct restorearg
*ra
, objset_t
*os
, struct drr_spill
*drrs
)
1707 dmu_buf_t
*db
, *db_spill
;
1710 if (drrs
->drr_length
< SPA_MINBLOCKSIZE
||
1711 drrs
->drr_length
> spa_maxblocksize(dmu_objset_spa(os
)))
1712 return (SET_ERROR(EINVAL
));
1714 data
= restore_read(ra
, drrs
->drr_length
, NULL
);
1718 if (dmu_object_info(os
, drrs
->drr_object
, NULL
) != 0)
1719 return (SET_ERROR(EINVAL
));
1721 VERIFY(0 == dmu_bonus_hold(os
, drrs
->drr_object
, FTAG
, &db
));
1722 if ((err
= dmu_spill_hold_by_bonus(db
, FTAG
, &db_spill
)) != 0) {
1723 dmu_buf_rele(db
, FTAG
);
1727 tx
= dmu_tx_create(os
);
1729 dmu_tx_hold_spill(tx
, db
->db_object
);
1731 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1733 dmu_buf_rele(db
, FTAG
);
1734 dmu_buf_rele(db_spill
, FTAG
);
1738 dmu_buf_will_dirty(db_spill
, tx
);
1740 if (db_spill
->db_size
< drrs
->drr_length
)
1741 VERIFY(0 == dbuf_spill_set_blksz(db_spill
,
1742 drrs
->drr_length
, tx
));
1743 bcopy(data
, db_spill
->db_data
, drrs
->drr_length
);
1745 dmu_buf_rele(db
, FTAG
);
1746 dmu_buf_rele(db_spill
, FTAG
);
1754 restore_free(struct restorearg
*ra
, objset_t
*os
,
1755 struct drr_free
*drrf
)
1759 if (drrf
->drr_length
!= -1ULL &&
1760 drrf
->drr_offset
+ drrf
->drr_length
< drrf
->drr_offset
)
1761 return (SET_ERROR(EINVAL
));
1763 if (dmu_object_info(os
, drrf
->drr_object
, NULL
) != 0)
1764 return (SET_ERROR(EINVAL
));
1766 err
= dmu_free_long_range(os
, drrf
->drr_object
,
1767 drrf
->drr_offset
, drrf
->drr_length
);
1771 /* used to destroy the drc_ds on error */
1773 dmu_recv_cleanup_ds(dmu_recv_cookie_t
*drc
)
1775 char name
[MAXNAMELEN
];
1776 dsl_dataset_name(drc
->drc_ds
, name
);
1777 dsl_dataset_disown(drc
->drc_ds
, dmu_recv_tag
);
1778 (void) dsl_destroy_head(name
);
1782 * NB: callers *must* call dmu_recv_end() if this succeeds.
1785 dmu_recv_stream(dmu_recv_cookie_t
*drc
, vnode_t
*vp
, offset_t
*voffp
,
1786 int cleanup_fd
, uint64_t *action_handlep
)
1788 struct restorearg ra
= { 0 };
1789 dmu_replay_record_t
*drr
;
1794 ra
.byteswap
= drc
->drc_byteswap
;
1795 ra
.cksum
= drc
->drc_cksum
;
1798 ra
.bufsize
= SPA_MAXBLOCKSIZE
;
1799 ra
.buf
= vmem_alloc(ra
.bufsize
, KM_SLEEP
);
1801 /* these were verified in dmu_recv_begin */
1802 ASSERT3U(DMU_GET_STREAM_HDRTYPE(drc
->drc_drrb
->drr_versioninfo
), ==,
1804 ASSERT3U(drc
->drc_drrb
->drr_type
, <, DMU_OST_NUMTYPES
);
1807 * Open the objset we are modifying.
1809 VERIFY0(dmu_objset_from_ds(drc
->drc_ds
, &os
));
1811 ASSERT(dsl_dataset_phys(drc
->drc_ds
)->ds_flags
& DS_FLAG_INCONSISTENT
);
1813 featureflags
= DMU_GET_FEATUREFLAGS(drc
->drc_drrb
->drr_versioninfo
);
1815 /* if this stream is dedup'ed, set up the avl tree for guid mapping */
1816 if (featureflags
& DMU_BACKUP_FEATURE_DEDUP
) {
1819 if (cleanup_fd
== -1) {
1820 ra
.err
= SET_ERROR(EBADF
);
1823 ra
.err
= zfs_onexit_fd_hold(cleanup_fd
, &minor
);
1829 if (*action_handlep
== 0) {
1831 kmem_alloc(sizeof (avl_tree_t
), KM_SLEEP
);
1832 avl_create(ra
.guid_to_ds_map
, guid_compare
,
1833 sizeof (guid_map_entry_t
),
1834 offsetof(guid_map_entry_t
, avlnode
));
1835 ra
.err
= zfs_onexit_add_cb(minor
,
1836 free_guid_map_onexit
, ra
.guid_to_ds_map
,
1841 ra
.err
= zfs_onexit_cb_data(minor
, *action_handlep
,
1842 (void **)&ra
.guid_to_ds_map
);
1847 drc
->drc_guid_to_ds_map
= ra
.guid_to_ds_map
;
1851 * Read records and process them.
1854 while (ra
.err
== 0 &&
1855 NULL
!= (drr
= restore_read(&ra
, sizeof (*drr
), NULL
))) {
1856 if (issig(JUSTLOOKING
) && issig(FORREAL
)) {
1857 ra
.err
= SET_ERROR(EINTR
);
1862 backup_byteswap(drr
);
1864 switch (drr
->drr_type
) {
1868 * We need to make a copy of the record header,
1869 * because restore_{object,write} may need to
1870 * restore_read(), which will invalidate drr.
1872 struct drr_object drro
= drr
->drr_u
.drr_object
;
1873 ra
.err
= restore_object(&ra
, os
, &drro
);
1876 case DRR_FREEOBJECTS
:
1878 struct drr_freeobjects drrfo
=
1879 drr
->drr_u
.drr_freeobjects
;
1880 ra
.err
= restore_freeobjects(&ra
, os
, &drrfo
);
1885 struct drr_write drrw
= drr
->drr_u
.drr_write
;
1886 ra
.err
= restore_write(&ra
, os
, &drrw
);
1889 case DRR_WRITE_BYREF
:
1891 struct drr_write_byref drrwbr
=
1892 drr
->drr_u
.drr_write_byref
;
1893 ra
.err
= restore_write_byref(&ra
, os
, &drrwbr
);
1896 case DRR_WRITE_EMBEDDED
:
1898 struct drr_write_embedded drrwe
=
1899 drr
->drr_u
.drr_write_embedded
;
1900 ra
.err
= restore_write_embedded(&ra
, os
, &drrwe
);
1905 struct drr_free drrf
= drr
->drr_u
.drr_free
;
1906 ra
.err
= restore_free(&ra
, os
, &drrf
);
1911 struct drr_end drre
= drr
->drr_u
.drr_end
;
1913 * We compare against the *previous* checksum
1914 * value, because the stored checksum is of
1915 * everything before the DRR_END record.
1917 if (!ZIO_CHECKSUM_EQUAL(drre
.drr_checksum
, pcksum
))
1918 ra
.err
= SET_ERROR(ECKSUM
);
1923 struct drr_spill drrs
= drr
->drr_u
.drr_spill
;
1924 ra
.err
= restore_spill(&ra
, os
, &drrs
);
1928 ra
.err
= SET_ERROR(EINVAL
);
1933 ASSERT(ra
.err
!= 0);
1936 if ((featureflags
& DMU_BACKUP_FEATURE_DEDUP
) && (cleanup_fd
!= -1))
1937 zfs_onexit_fd_rele(cleanup_fd
);
1941 * destroy what we created, so we don't leave it in the
1942 * inconsistent restoring state.
1944 dmu_recv_cleanup_ds(drc
);
1947 vmem_free(ra
.buf
, ra
.bufsize
);
1953 dmu_recv_end_check(void *arg
, dmu_tx_t
*tx
)
1955 dmu_recv_cookie_t
*drc
= arg
;
1956 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1959 ASSERT3P(drc
->drc_ds
->ds_owner
, ==, dmu_recv_tag
);
1961 if (!drc
->drc_newfs
) {
1962 dsl_dataset_t
*origin_head
;
1964 error
= dsl_dataset_hold(dp
, drc
->drc_tofs
, FTAG
, &origin_head
);
1967 if (drc
->drc_force
) {
1969 * We will destroy any snapshots in tofs (i.e. before
1970 * origin_head) that are after the origin (which is
1971 * the snap before drc_ds, because drc_ds can not
1972 * have any snaps of its own).
1976 obj
= dsl_dataset_phys(origin_head
)->ds_prev_snap_obj
;
1978 dsl_dataset_phys(drc
->drc_ds
)->ds_prev_snap_obj
) {
1979 dsl_dataset_t
*snap
;
1980 error
= dsl_dataset_hold_obj(dp
, obj
, FTAG
,
1984 if (snap
->ds_dir
!= origin_head
->ds_dir
)
1985 error
= SET_ERROR(EINVAL
);
1987 error
= dsl_destroy_snapshot_check_impl(
1990 obj
= dsl_dataset_phys(snap
)->ds_prev_snap_obj
;
1991 dsl_dataset_rele(snap
, FTAG
);
1996 error
= dsl_dataset_clone_swap_check_impl(drc
->drc_ds
,
1997 origin_head
, drc
->drc_force
, drc
->drc_owner
, tx
);
1999 dsl_dataset_rele(origin_head
, FTAG
);
2002 error
= dsl_dataset_snapshot_check_impl(origin_head
,
2003 drc
->drc_tosnap
, tx
, B_TRUE
, 1, drc
->drc_cred
);
2004 dsl_dataset_rele(origin_head
, FTAG
);
2008 error
= dsl_destroy_head_check_impl(drc
->drc_ds
, 1);
2010 error
= dsl_dataset_snapshot_check_impl(drc
->drc_ds
,
2011 drc
->drc_tosnap
, tx
, B_TRUE
, 1, drc
->drc_cred
);
2017 dmu_recv_end_sync(void *arg
, dmu_tx_t
*tx
)
2019 dmu_recv_cookie_t
*drc
= arg
;
2020 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
2022 spa_history_log_internal_ds(drc
->drc_ds
, "finish receiving",
2023 tx
, "snap=%s", drc
->drc_tosnap
);
2025 if (!drc
->drc_newfs
) {
2026 dsl_dataset_t
*origin_head
;
2028 VERIFY0(dsl_dataset_hold(dp
, drc
->drc_tofs
, FTAG
,
2031 if (drc
->drc_force
) {
2033 * Destroy any snapshots of drc_tofs (origin_head)
2034 * after the origin (the snap before drc_ds).
2038 obj
= dsl_dataset_phys(origin_head
)->ds_prev_snap_obj
;
2040 dsl_dataset_phys(drc
->drc_ds
)->ds_prev_snap_obj
) {
2041 dsl_dataset_t
*snap
;
2042 VERIFY0(dsl_dataset_hold_obj(dp
, obj
, FTAG
,
2044 ASSERT3P(snap
->ds_dir
, ==, origin_head
->ds_dir
);
2045 obj
= dsl_dataset_phys(snap
)->ds_prev_snap_obj
;
2046 dsl_destroy_snapshot_sync_impl(snap
,
2048 dsl_dataset_rele(snap
, FTAG
);
2051 VERIFY3P(drc
->drc_ds
->ds_prev
, ==,
2052 origin_head
->ds_prev
);
2054 dsl_dataset_clone_swap_sync_impl(drc
->drc_ds
,
2056 dsl_dataset_snapshot_sync_impl(origin_head
,
2057 drc
->drc_tosnap
, tx
);
2059 /* set snapshot's creation time and guid */
2060 dmu_buf_will_dirty(origin_head
->ds_prev
->ds_dbuf
, tx
);
2061 dsl_dataset_phys(origin_head
->ds_prev
)->ds_creation_time
=
2062 drc
->drc_drrb
->drr_creation_time
;
2063 dsl_dataset_phys(origin_head
->ds_prev
)->ds_guid
=
2064 drc
->drc_drrb
->drr_toguid
;
2065 dsl_dataset_phys(origin_head
->ds_prev
)->ds_flags
&=
2066 ~DS_FLAG_INCONSISTENT
;
2068 dmu_buf_will_dirty(origin_head
->ds_dbuf
, tx
);
2069 dsl_dataset_phys(origin_head
)->ds_flags
&=
2070 ~DS_FLAG_INCONSISTENT
;
2072 dsl_dataset_rele(origin_head
, FTAG
);
2073 dsl_destroy_head_sync_impl(drc
->drc_ds
, tx
);
2075 if (drc
->drc_owner
!= NULL
)
2076 VERIFY3P(origin_head
->ds_owner
, ==, drc
->drc_owner
);
2078 dsl_dataset_t
*ds
= drc
->drc_ds
;
2080 dsl_dataset_snapshot_sync_impl(ds
, drc
->drc_tosnap
, tx
);
2082 /* set snapshot's creation time and guid */
2083 dmu_buf_will_dirty(ds
->ds_prev
->ds_dbuf
, tx
);
2084 dsl_dataset_phys(ds
->ds_prev
)->ds_creation_time
=
2085 drc
->drc_drrb
->drr_creation_time
;
2086 dsl_dataset_phys(ds
->ds_prev
)->ds_guid
=
2087 drc
->drc_drrb
->drr_toguid
;
2088 dsl_dataset_phys(ds
->ds_prev
)->ds_flags
&=
2089 ~DS_FLAG_INCONSISTENT
;
2091 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
2092 dsl_dataset_phys(ds
)->ds_flags
&= ~DS_FLAG_INCONSISTENT
;
2094 drc
->drc_newsnapobj
= dsl_dataset_phys(drc
->drc_ds
)->ds_prev_snap_obj
;
2096 * Release the hold from dmu_recv_begin. This must be done before
2097 * we return to open context, so that when we free the dataset's dnode,
2098 * we can evict its bonus buffer.
2100 dsl_dataset_disown(drc
->drc_ds
, dmu_recv_tag
);
2105 add_ds_to_guidmap(const char *name
, avl_tree_t
*guid_map
, uint64_t snapobj
)
2108 dsl_dataset_t
*snapds
;
2109 guid_map_entry_t
*gmep
;
2112 ASSERT(guid_map
!= NULL
);
2114 err
= dsl_pool_hold(name
, FTAG
, &dp
);
2117 gmep
= kmem_alloc(sizeof (*gmep
), KM_SLEEP
);
2118 err
= dsl_dataset_hold_obj(dp
, snapobj
, gmep
, &snapds
);
2120 gmep
->guid
= dsl_dataset_phys(snapds
)->ds_guid
;
2121 gmep
->gme_ds
= snapds
;
2122 avl_add(guid_map
, gmep
);
2123 dsl_dataset_long_hold(snapds
, gmep
);
2125 kmem_free(gmep
, sizeof (*gmep
));
2128 dsl_pool_rele(dp
, FTAG
);
2132 static int dmu_recv_end_modified_blocks
= 3;
2135 dmu_recv_existing_end(dmu_recv_cookie_t
*drc
)
2143 * We will be destroying the ds; make sure its origin is unmounted if
2146 name
= kmem_alloc(MAXNAMELEN
, KM_SLEEP
);
2147 dsl_dataset_name(drc
->drc_ds
, name
);
2148 zfs_destroy_unmount_origin(name
);
2149 kmem_free(name
, MAXNAMELEN
);
2152 error
= dsl_sync_task(drc
->drc_tofs
,
2153 dmu_recv_end_check
, dmu_recv_end_sync
, drc
,
2154 dmu_recv_end_modified_blocks
, ZFS_SPACE_CHECK_NORMAL
);
2157 dmu_recv_cleanup_ds(drc
);
2162 dmu_recv_new_end(dmu_recv_cookie_t
*drc
)
2166 error
= dsl_sync_task(drc
->drc_tofs
,
2167 dmu_recv_end_check
, dmu_recv_end_sync
, drc
,
2168 dmu_recv_end_modified_blocks
, ZFS_SPACE_CHECK_NORMAL
);
2171 dmu_recv_cleanup_ds(drc
);
2172 } else if (drc
->drc_guid_to_ds_map
!= NULL
) {
2173 (void) add_ds_to_guidmap(drc
->drc_tofs
,
2174 drc
->drc_guid_to_ds_map
,
2175 drc
->drc_newsnapobj
);
2181 dmu_recv_end(dmu_recv_cookie_t
*drc
, void *owner
)
2183 drc
->drc_owner
= owner
;
2186 return (dmu_recv_new_end(drc
));
2188 return (dmu_recv_existing_end(drc
));
2192 * Return TRUE if this objset is currently being received into.
2195 dmu_objset_is_receiving(objset_t
*os
)
2197 return (os
->os_dsl_dataset
!= NULL
&&
2198 os
->os_dsl_dataset
->ds_owner
== dmu_recv_tag
);
2201 #if defined(_KERNEL)
2202 module_param(zfs_send_corrupt_data
, int, 0644);
2203 MODULE_PARM_DESC(zfs_send_corrupt_data
, "Allow sending corrupt data");