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 arc_flags_t aflags
= ARC_FLAG_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 arc_flags_t aflags
= ARC_FLAG_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 */
525 arc_flags_t aflags
= ARC_FLAG_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_adjust_send_estimate_for_indirects(dsl_dataset_t
*ds
, uint64_t size
,
845 * Assume that space (both on-disk and in-stream) is dominated by
846 * data. We will adjust for indirect blocks and the copies property,
847 * but ignore per-object space used (eg, dnodes and DRR_OBJECT records).
851 * Subtract out approximate space used by indirect blocks.
852 * Assume most space is used by data blocks (non-indirect, non-dnode).
853 * Assume all blocks are recordsize. Assume ditto blocks and
854 * internal fragmentation counter out compression.
856 * Therefore, space used by indirect blocks is sizeof(blkptr_t) per
857 * block, which we observe in practice.
860 err
= dsl_prop_get_int_ds(ds
, "recordsize", &recordsize
);
863 size
-= size
/ recordsize
* sizeof (blkptr_t
);
865 /* Add in the space for the record associated with each block. */
866 size
+= size
/ recordsize
* sizeof (dmu_replay_record_t
);
874 dmu_send_estimate(dsl_dataset_t
*ds
, dsl_dataset_t
*fromds
, uint64_t *sizep
)
879 ASSERT(dsl_pool_config_held(ds
->ds_dir
->dd_pool
));
881 /* tosnap must be a snapshot */
882 if (!ds
->ds_is_snapshot
)
883 return (SET_ERROR(EINVAL
));
885 /* fromsnap, if provided, must be a snapshot */
886 if (fromds
!= NULL
&& !fromds
->ds_is_snapshot
)
887 return (SET_ERROR(EINVAL
));
890 * fromsnap must be an earlier snapshot from the same fs as tosnap,
891 * or the origin's fs.
893 if (fromds
!= NULL
&& !dsl_dataset_is_before(ds
, fromds
, 0))
894 return (SET_ERROR(EXDEV
));
896 /* Get uncompressed size estimate of changed data. */
897 if (fromds
== NULL
) {
898 size
= dsl_dataset_phys(ds
)->ds_uncompressed_bytes
;
901 err
= dsl_dataset_space_written(fromds
, ds
,
902 &used
, &comp
, &size
);
907 err
= dmu_adjust_send_estimate_for_indirects(ds
, size
, sizep
);
912 * Simple callback used to traverse the blocks of a snapshot and sum their
917 dmu_calculate_send_traversal(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
918 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
920 uint64_t *spaceptr
= arg
;
921 if (bp
!= NULL
&& !BP_IS_HOLE(bp
)) {
922 *spaceptr
+= BP_GET_UCSIZE(bp
);
928 * Given a desination snapshot and a TXG, calculate the approximate size of a
929 * send stream sent from that TXG. from_txg may be zero, indicating that the
930 * whole snapshot will be sent.
933 dmu_send_estimate_from_txg(dsl_dataset_t
*ds
, uint64_t from_txg
,
939 ASSERT(dsl_pool_config_held(ds
->ds_dir
->dd_pool
));
941 /* tosnap must be a snapshot */
942 if (!dsl_dataset_is_snapshot(ds
))
943 return (SET_ERROR(EINVAL
));
945 /* verify that from_txg is before the provided snapshot was taken */
946 if (from_txg
>= dsl_dataset_phys(ds
)->ds_creation_txg
) {
947 return (SET_ERROR(EXDEV
));
950 * traverse the blocks of the snapshot with birth times after
951 * from_txg, summing their uncompressed size
953 err
= traverse_dataset(ds
, from_txg
, TRAVERSE_POST
,
954 dmu_calculate_send_traversal
, &size
);
958 err
= dmu_adjust_send_estimate_for_indirects(ds
, size
, sizep
);
962 typedef struct dmu_recv_begin_arg
{
963 const char *drba_origin
;
964 dmu_recv_cookie_t
*drba_cookie
;
966 uint64_t drba_snapobj
;
967 } dmu_recv_begin_arg_t
;
970 recv_begin_check_existing_impl(dmu_recv_begin_arg_t
*drba
, dsl_dataset_t
*ds
,
975 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
977 /* temporary clone name must not exist */
978 error
= zap_lookup(dp
->dp_meta_objset
,
979 dsl_dir_phys(ds
->ds_dir
)->dd_child_dir_zapobj
, recv_clone_name
,
982 return (error
== 0 ? EBUSY
: error
);
984 /* new snapshot name must not exist */
985 error
= zap_lookup(dp
->dp_meta_objset
,
986 dsl_dataset_phys(ds
)->ds_snapnames_zapobj
,
987 drba
->drba_cookie
->drc_tosnap
, 8, 1, &val
);
989 return (error
== 0 ? EEXIST
: error
);
992 * Check snapshot limit before receiving. We'll recheck again at the
993 * end, but might as well abort before receiving if we're already over
996 * Note that we do not check the file system limit with
997 * dsl_dir_fscount_check because the temporary %clones don't count
998 * against that limit.
1000 error
= dsl_fs_ss_limit_check(ds
->ds_dir
, 1, ZFS_PROP_SNAPSHOT_LIMIT
,
1001 NULL
, drba
->drba_cred
);
1005 if (fromguid
!= 0) {
1006 dsl_dataset_t
*snap
;
1007 uint64_t obj
= dsl_dataset_phys(ds
)->ds_prev_snap_obj
;
1009 /* Find snapshot in this dir that matches fromguid. */
1011 error
= dsl_dataset_hold_obj(dp
, obj
, FTAG
,
1014 return (SET_ERROR(ENODEV
));
1015 if (snap
->ds_dir
!= ds
->ds_dir
) {
1016 dsl_dataset_rele(snap
, FTAG
);
1017 return (SET_ERROR(ENODEV
));
1019 if (dsl_dataset_phys(snap
)->ds_guid
== fromguid
)
1021 obj
= dsl_dataset_phys(snap
)->ds_prev_snap_obj
;
1022 dsl_dataset_rele(snap
, FTAG
);
1025 return (SET_ERROR(ENODEV
));
1027 if (drba
->drba_cookie
->drc_force
) {
1028 drba
->drba_snapobj
= obj
;
1031 * If we are not forcing, there must be no
1032 * changes since fromsnap.
1034 if (dsl_dataset_modified_since_snap(ds
, snap
)) {
1035 dsl_dataset_rele(snap
, FTAG
);
1036 return (SET_ERROR(ETXTBSY
));
1038 drba
->drba_snapobj
= ds
->ds_prev
->ds_object
;
1041 dsl_dataset_rele(snap
, FTAG
);
1043 /* if full, then must be forced */
1044 if (!drba
->drba_cookie
->drc_force
)
1045 return (SET_ERROR(EEXIST
));
1046 /* start from $ORIGIN@$ORIGIN, if supported */
1047 drba
->drba_snapobj
= dp
->dp_origin_snap
!= NULL
?
1048 dp
->dp_origin_snap
->ds_object
: 0;
1056 dmu_recv_begin_check(void *arg
, dmu_tx_t
*tx
)
1058 dmu_recv_begin_arg_t
*drba
= arg
;
1059 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1060 struct drr_begin
*drrb
= drba
->drba_cookie
->drc_drrb
;
1061 uint64_t fromguid
= drrb
->drr_fromguid
;
1062 int flags
= drrb
->drr_flags
;
1064 uint64_t featureflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
1066 const char *tofs
= drba
->drba_cookie
->drc_tofs
;
1068 /* already checked */
1069 ASSERT3U(drrb
->drr_magic
, ==, DMU_BACKUP_MAGIC
);
1071 if (DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) ==
1072 DMU_COMPOUNDSTREAM
||
1073 drrb
->drr_type
>= DMU_OST_NUMTYPES
||
1074 ((flags
& DRR_FLAG_CLONE
) && drba
->drba_origin
== NULL
))
1075 return (SET_ERROR(EINVAL
));
1077 /* Verify pool version supports SA if SA_SPILL feature set */
1078 if ((featureflags
& DMU_BACKUP_FEATURE_SA_SPILL
) &&
1079 spa_version(dp
->dp_spa
) < SPA_VERSION_SA
)
1080 return (SET_ERROR(ENOTSUP
));
1083 * The receiving code doesn't know how to translate a WRITE_EMBEDDED
1084 * record to a plan WRITE record, so the pool must have the
1085 * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
1086 * records. Same with WRITE_EMBEDDED records that use LZ4 compression.
1088 if ((featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA
) &&
1089 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_EMBEDDED_DATA
))
1090 return (SET_ERROR(ENOTSUP
));
1091 if ((featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA_LZ4
) &&
1092 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_LZ4_COMPRESS
))
1093 return (SET_ERROR(ENOTSUP
));
1096 * The receiving code doesn't know how to translate large blocks
1097 * to smaller ones, so the pool must have the LARGE_BLOCKS
1098 * feature enabled if the stream has LARGE_BLOCKS.
1100 if ((featureflags
& DMU_BACKUP_FEATURE_LARGE_BLOCKS
) &&
1101 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_LARGE_BLOCKS
))
1102 return (SET_ERROR(ENOTSUP
));
1104 error
= dsl_dataset_hold(dp
, tofs
, FTAG
, &ds
);
1106 /* target fs already exists; recv into temp clone */
1108 /* Can't recv a clone into an existing fs */
1109 if (flags
& DRR_FLAG_CLONE
) {
1110 dsl_dataset_rele(ds
, FTAG
);
1111 return (SET_ERROR(EINVAL
));
1114 error
= recv_begin_check_existing_impl(drba
, ds
, fromguid
);
1115 dsl_dataset_rele(ds
, FTAG
);
1116 } else if (error
== ENOENT
) {
1117 /* target fs does not exist; must be a full backup or clone */
1118 char buf
[MAXNAMELEN
];
1121 * If it's a non-clone incremental, we are missing the
1122 * target fs, so fail the recv.
1124 if (fromguid
!= 0 && !(flags
& DRR_FLAG_CLONE
))
1125 return (SET_ERROR(ENOENT
));
1127 /* Open the parent of tofs */
1128 ASSERT3U(strlen(tofs
), <, MAXNAMELEN
);
1129 (void) strlcpy(buf
, tofs
, strrchr(tofs
, '/') - tofs
+ 1);
1130 error
= dsl_dataset_hold(dp
, buf
, FTAG
, &ds
);
1135 * Check filesystem and snapshot limits before receiving. We'll
1136 * recheck snapshot limits again at the end (we create the
1137 * filesystems and increment those counts during begin_sync).
1139 error
= dsl_fs_ss_limit_check(ds
->ds_dir
, 1,
1140 ZFS_PROP_FILESYSTEM_LIMIT
, NULL
, drba
->drba_cred
);
1142 dsl_dataset_rele(ds
, FTAG
);
1146 error
= dsl_fs_ss_limit_check(ds
->ds_dir
, 1,
1147 ZFS_PROP_SNAPSHOT_LIMIT
, NULL
, drba
->drba_cred
);
1149 dsl_dataset_rele(ds
, FTAG
);
1153 if (drba
->drba_origin
!= NULL
) {
1154 dsl_dataset_t
*origin
;
1155 error
= dsl_dataset_hold(dp
, drba
->drba_origin
,
1158 dsl_dataset_rele(ds
, FTAG
);
1161 if (!origin
->ds_is_snapshot
) {
1162 dsl_dataset_rele(origin
, FTAG
);
1163 dsl_dataset_rele(ds
, FTAG
);
1164 return (SET_ERROR(EINVAL
));
1166 if (dsl_dataset_phys(origin
)->ds_guid
!= fromguid
) {
1167 dsl_dataset_rele(origin
, FTAG
);
1168 dsl_dataset_rele(ds
, FTAG
);
1169 return (SET_ERROR(ENODEV
));
1171 dsl_dataset_rele(origin
, FTAG
);
1173 dsl_dataset_rele(ds
, FTAG
);
1180 dmu_recv_begin_sync(void *arg
, dmu_tx_t
*tx
)
1182 dmu_recv_begin_arg_t
*drba
= arg
;
1183 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1184 struct drr_begin
*drrb
= drba
->drba_cookie
->drc_drrb
;
1185 const char *tofs
= drba
->drba_cookie
->drc_tofs
;
1186 dsl_dataset_t
*ds
, *newds
;
1191 crflags
= (drrb
->drr_flags
& DRR_FLAG_CI_DATA
) ?
1192 DS_FLAG_CI_DATASET
: 0;
1194 error
= dsl_dataset_hold(dp
, tofs
, FTAG
, &ds
);
1196 /* create temporary clone */
1197 dsl_dataset_t
*snap
= NULL
;
1198 if (drba
->drba_snapobj
!= 0) {
1199 VERIFY0(dsl_dataset_hold_obj(dp
,
1200 drba
->drba_snapobj
, FTAG
, &snap
));
1202 dsobj
= dsl_dataset_create_sync(ds
->ds_dir
, recv_clone_name
,
1203 snap
, crflags
, drba
->drba_cred
, tx
);
1204 dsl_dataset_rele(snap
, FTAG
);
1205 dsl_dataset_rele(ds
, FTAG
);
1209 dsl_dataset_t
*origin
= NULL
;
1211 VERIFY0(dsl_dir_hold(dp
, tofs
, FTAG
, &dd
, &tail
));
1213 if (drba
->drba_origin
!= NULL
) {
1214 VERIFY0(dsl_dataset_hold(dp
, drba
->drba_origin
,
1218 /* Create new dataset. */
1219 dsobj
= dsl_dataset_create_sync(dd
,
1220 strrchr(tofs
, '/') + 1,
1221 origin
, crflags
, drba
->drba_cred
, tx
);
1223 dsl_dataset_rele(origin
, FTAG
);
1224 dsl_dir_rele(dd
, FTAG
);
1225 drba
->drba_cookie
->drc_newfs
= B_TRUE
;
1227 VERIFY0(dsl_dataset_own_obj(dp
, dsobj
, dmu_recv_tag
, &newds
));
1229 if ((DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
) &
1230 DMU_BACKUP_FEATURE_LARGE_BLOCKS
) &&
1231 !newds
->ds_large_blocks
) {
1232 dsl_dataset_activate_large_blocks_sync_impl(dsobj
, tx
);
1233 newds
->ds_large_blocks
= B_TRUE
;
1236 dmu_buf_will_dirty(newds
->ds_dbuf
, tx
);
1237 dsl_dataset_phys(newds
)->ds_flags
|= DS_FLAG_INCONSISTENT
;
1240 * If we actually created a non-clone, we need to create the
1241 * objset in our new dataset.
1243 if (BP_IS_HOLE(dsl_dataset_get_blkptr(newds
))) {
1244 (void) dmu_objset_create_impl(dp
->dp_spa
,
1245 newds
, dsl_dataset_get_blkptr(newds
), drrb
->drr_type
, tx
);
1248 drba
->drba_cookie
->drc_ds
= newds
;
1250 spa_history_log_internal_ds(newds
, "receive", tx
, "");
1254 * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
1255 * succeeds; otherwise we will leak the holds on the datasets.
1258 dmu_recv_begin(char *tofs
, char *tosnap
, struct drr_begin
*drrb
,
1259 boolean_t force
, char *origin
, dmu_recv_cookie_t
*drc
)
1261 dmu_recv_begin_arg_t drba
= { 0 };
1262 dmu_replay_record_t
*drr
;
1264 bzero(drc
, sizeof (dmu_recv_cookie_t
));
1265 drc
->drc_drrb
= drrb
;
1266 drc
->drc_tosnap
= tosnap
;
1267 drc
->drc_tofs
= tofs
;
1268 drc
->drc_force
= force
;
1269 drc
->drc_cred
= CRED();
1271 if (drrb
->drr_magic
== BSWAP_64(DMU_BACKUP_MAGIC
))
1272 drc
->drc_byteswap
= B_TRUE
;
1273 else if (drrb
->drr_magic
!= DMU_BACKUP_MAGIC
)
1274 return (SET_ERROR(EINVAL
));
1276 drr
= kmem_zalloc(sizeof (dmu_replay_record_t
), KM_SLEEP
);
1277 drr
->drr_type
= DRR_BEGIN
;
1278 drr
->drr_u
.drr_begin
= *drc
->drc_drrb
;
1279 if (drc
->drc_byteswap
) {
1280 fletcher_4_incremental_byteswap(drr
,
1281 sizeof (dmu_replay_record_t
), &drc
->drc_cksum
);
1283 fletcher_4_incremental_native(drr
,
1284 sizeof (dmu_replay_record_t
), &drc
->drc_cksum
);
1286 kmem_free(drr
, sizeof (dmu_replay_record_t
));
1288 if (drc
->drc_byteswap
) {
1289 drrb
->drr_magic
= BSWAP_64(drrb
->drr_magic
);
1290 drrb
->drr_versioninfo
= BSWAP_64(drrb
->drr_versioninfo
);
1291 drrb
->drr_creation_time
= BSWAP_64(drrb
->drr_creation_time
);
1292 drrb
->drr_type
= BSWAP_32(drrb
->drr_type
);
1293 drrb
->drr_toguid
= BSWAP_64(drrb
->drr_toguid
);
1294 drrb
->drr_fromguid
= BSWAP_64(drrb
->drr_fromguid
);
1297 drba
.drba_origin
= origin
;
1298 drba
.drba_cookie
= drc
;
1299 drba
.drba_cred
= CRED();
1301 return (dsl_sync_task(tofs
, dmu_recv_begin_check
, dmu_recv_begin_sync
,
1302 &drba
, 5, ZFS_SPACE_CHECK_NORMAL
));
1311 int bufsize
; /* amount of memory allocated for buf */
1313 avl_tree_t
*guid_to_ds_map
;
1316 typedef struct guid_map_entry
{
1318 dsl_dataset_t
*gme_ds
;
1323 guid_compare(const void *arg1
, const void *arg2
)
1325 const guid_map_entry_t
*gmep1
= arg1
;
1326 const guid_map_entry_t
*gmep2
= arg2
;
1328 if (gmep1
->guid
< gmep2
->guid
)
1330 else if (gmep1
->guid
> gmep2
->guid
)
1336 free_guid_map_onexit(void *arg
)
1338 avl_tree_t
*ca
= arg
;
1339 void *cookie
= NULL
;
1340 guid_map_entry_t
*gmep
;
1342 while ((gmep
= avl_destroy_nodes(ca
, &cookie
)) != NULL
) {
1343 dsl_dataset_long_rele(gmep
->gme_ds
, gmep
);
1344 dsl_dataset_rele(gmep
->gme_ds
, gmep
);
1345 kmem_free(gmep
, sizeof (guid_map_entry_t
));
1348 kmem_free(ca
, sizeof (avl_tree_t
));
1352 restore_read(struct restorearg
*ra
, int len
, char *buf
)
1359 /* some things will require 8-byte alignment, so everything must */
1361 ASSERT3U(len
, <=, ra
->bufsize
);
1363 while (done
< len
) {
1366 ra
->err
= vn_rdwr(UIO_READ
, ra
->vp
,
1367 buf
+ done
, len
- done
,
1368 ra
->voff
, UIO_SYSSPACE
, FAPPEND
,
1369 RLIM64_INFINITY
, CRED(), &resid
);
1371 if (resid
== len
- done
)
1372 ra
->err
= SET_ERROR(EINVAL
);
1373 ra
->voff
+= len
- done
- resid
;
1379 ASSERT3U(done
, ==, len
);
1381 fletcher_4_incremental_byteswap(buf
, len
, &ra
->cksum
);
1383 fletcher_4_incremental_native(buf
, len
, &ra
->cksum
);
1387 noinline
static void
1388 backup_byteswap(dmu_replay_record_t
*drr
)
1390 #define DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
1391 #define DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
1392 drr
->drr_type
= BSWAP_32(drr
->drr_type
);
1393 drr
->drr_payloadlen
= BSWAP_32(drr
->drr_payloadlen
);
1394 switch (drr
->drr_type
) {
1396 DO64(drr_begin
.drr_magic
);
1397 DO64(drr_begin
.drr_versioninfo
);
1398 DO64(drr_begin
.drr_creation_time
);
1399 DO32(drr_begin
.drr_type
);
1400 DO32(drr_begin
.drr_flags
);
1401 DO64(drr_begin
.drr_toguid
);
1402 DO64(drr_begin
.drr_fromguid
);
1405 DO64(drr_object
.drr_object
);
1406 DO32(drr_object
.drr_type
);
1407 DO32(drr_object
.drr_bonustype
);
1408 DO32(drr_object
.drr_blksz
);
1409 DO32(drr_object
.drr_bonuslen
);
1410 DO64(drr_object
.drr_toguid
);
1412 case DRR_FREEOBJECTS
:
1413 DO64(drr_freeobjects
.drr_firstobj
);
1414 DO64(drr_freeobjects
.drr_numobjs
);
1415 DO64(drr_freeobjects
.drr_toguid
);
1418 DO64(drr_write
.drr_object
);
1419 DO32(drr_write
.drr_type
);
1420 DO64(drr_write
.drr_offset
);
1421 DO64(drr_write
.drr_length
);
1422 DO64(drr_write
.drr_toguid
);
1423 DO64(drr_write
.drr_key
.ddk_cksum
.zc_word
[0]);
1424 DO64(drr_write
.drr_key
.ddk_cksum
.zc_word
[1]);
1425 DO64(drr_write
.drr_key
.ddk_cksum
.zc_word
[2]);
1426 DO64(drr_write
.drr_key
.ddk_cksum
.zc_word
[3]);
1427 DO64(drr_write
.drr_key
.ddk_prop
);
1429 case DRR_WRITE_BYREF
:
1430 DO64(drr_write_byref
.drr_object
);
1431 DO64(drr_write_byref
.drr_offset
);
1432 DO64(drr_write_byref
.drr_length
);
1433 DO64(drr_write_byref
.drr_toguid
);
1434 DO64(drr_write_byref
.drr_refguid
);
1435 DO64(drr_write_byref
.drr_refobject
);
1436 DO64(drr_write_byref
.drr_refoffset
);
1437 DO64(drr_write_byref
.drr_key
.ddk_cksum
.zc_word
[0]);
1438 DO64(drr_write_byref
.drr_key
.ddk_cksum
.zc_word
[1]);
1439 DO64(drr_write_byref
.drr_key
.ddk_cksum
.zc_word
[2]);
1440 DO64(drr_write_byref
.drr_key
.ddk_cksum
.zc_word
[3]);
1441 DO64(drr_write_byref
.drr_key
.ddk_prop
);
1443 case DRR_WRITE_EMBEDDED
:
1444 DO64(drr_write_embedded
.drr_object
);
1445 DO64(drr_write_embedded
.drr_offset
);
1446 DO64(drr_write_embedded
.drr_length
);
1447 DO64(drr_write_embedded
.drr_toguid
);
1448 DO32(drr_write_embedded
.drr_lsize
);
1449 DO32(drr_write_embedded
.drr_psize
);
1452 DO64(drr_free
.drr_object
);
1453 DO64(drr_free
.drr_offset
);
1454 DO64(drr_free
.drr_length
);
1455 DO64(drr_free
.drr_toguid
);
1458 DO64(drr_spill
.drr_object
);
1459 DO64(drr_spill
.drr_length
);
1460 DO64(drr_spill
.drr_toguid
);
1463 DO64(drr_end
.drr_checksum
.zc_word
[0]);
1464 DO64(drr_end
.drr_checksum
.zc_word
[1]);
1465 DO64(drr_end
.drr_checksum
.zc_word
[2]);
1466 DO64(drr_end
.drr_checksum
.zc_word
[3]);
1467 DO64(drr_end
.drr_toguid
);
1476 static inline uint8_t
1477 deduce_nblkptr(dmu_object_type_t bonus_type
, uint64_t bonus_size
)
1479 if (bonus_type
== DMU_OT_SA
) {
1483 ((DN_MAX_BONUSLEN
- bonus_size
) >> SPA_BLKPTRSHIFT
));
1488 restore_object(struct restorearg
*ra
, objset_t
*os
, struct drr_object
*drro
)
1490 dmu_object_info_t doi
;
1496 if (drro
->drr_type
== DMU_OT_NONE
||
1497 !DMU_OT_IS_VALID(drro
->drr_type
) ||
1498 !DMU_OT_IS_VALID(drro
->drr_bonustype
) ||
1499 drro
->drr_checksumtype
>= ZIO_CHECKSUM_FUNCTIONS
||
1500 drro
->drr_compress
>= ZIO_COMPRESS_FUNCTIONS
||
1501 P2PHASE(drro
->drr_blksz
, SPA_MINBLOCKSIZE
) ||
1502 drro
->drr_blksz
< SPA_MINBLOCKSIZE
||
1503 drro
->drr_blksz
> spa_maxblocksize(dmu_objset_spa(os
)) ||
1504 drro
->drr_bonuslen
> DN_MAX_BONUSLEN
) {
1505 return (SET_ERROR(EINVAL
));
1508 err
= dmu_object_info(os
, drro
->drr_object
, &doi
);
1510 if (err
!= 0 && err
!= ENOENT
)
1511 return (SET_ERROR(EINVAL
));
1512 object
= err
== 0 ? drro
->drr_object
: DMU_NEW_OBJECT
;
1514 if (drro
->drr_bonuslen
) {
1515 data
= restore_read(ra
, P2ROUNDUP(drro
->drr_bonuslen
, 8), NULL
);
1521 * If we are losing blkptrs or changing the block size this must
1522 * be a new file instance. We must clear out the previous file
1523 * contents before we can change this type of metadata in the dnode.
1528 nblkptr
= deduce_nblkptr(drro
->drr_bonustype
,
1529 drro
->drr_bonuslen
);
1531 if (drro
->drr_blksz
!= doi
.doi_data_block_size
||
1532 nblkptr
< doi
.doi_nblkptr
) {
1533 err
= dmu_free_long_range(os
, drro
->drr_object
,
1536 return (SET_ERROR(EINVAL
));
1540 tx
= dmu_tx_create(os
);
1541 dmu_tx_hold_bonus(tx
, object
);
1542 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1548 if (object
== DMU_NEW_OBJECT
) {
1549 /* currently free, want to be allocated */
1550 err
= dmu_object_claim(os
, drro
->drr_object
,
1551 drro
->drr_type
, drro
->drr_blksz
,
1552 drro
->drr_bonustype
, drro
->drr_bonuslen
, tx
);
1553 } else if (drro
->drr_type
!= doi
.doi_type
||
1554 drro
->drr_blksz
!= doi
.doi_data_block_size
||
1555 drro
->drr_bonustype
!= doi
.doi_bonus_type
||
1556 drro
->drr_bonuslen
!= doi
.doi_bonus_size
) {
1557 /* currently allocated, but with different properties */
1558 err
= dmu_object_reclaim(os
, drro
->drr_object
,
1559 drro
->drr_type
, drro
->drr_blksz
,
1560 drro
->drr_bonustype
, drro
->drr_bonuslen
, tx
);
1564 return (SET_ERROR(EINVAL
));
1567 dmu_object_set_checksum(os
, drro
->drr_object
, drro
->drr_checksumtype
,
1569 dmu_object_set_compress(os
, drro
->drr_object
, drro
->drr_compress
, tx
);
1574 VERIFY(0 == dmu_bonus_hold(os
, drro
->drr_object
, FTAG
, &db
));
1575 dmu_buf_will_dirty(db
, tx
);
1577 ASSERT3U(db
->db_size
, >=, drro
->drr_bonuslen
);
1578 bcopy(data
, db
->db_data
, drro
->drr_bonuslen
);
1580 dmu_object_byteswap_t byteswap
=
1581 DMU_OT_BYTESWAP(drro
->drr_bonustype
);
1582 dmu_ot_byteswap
[byteswap
].ob_func(db
->db_data
,
1583 drro
->drr_bonuslen
);
1585 dmu_buf_rele(db
, FTAG
);
1593 restore_freeobjects(struct restorearg
*ra
, objset_t
*os
,
1594 struct drr_freeobjects
*drrfo
)
1598 if (drrfo
->drr_firstobj
+ drrfo
->drr_numobjs
< drrfo
->drr_firstobj
)
1599 return (SET_ERROR(EINVAL
));
1601 for (obj
= drrfo
->drr_firstobj
;
1602 obj
< drrfo
->drr_firstobj
+ drrfo
->drr_numobjs
;
1603 (void) dmu_object_next(os
, &obj
, FALSE
, 0)) {
1606 if (dmu_object_info(os
, obj
, NULL
) != 0)
1609 err
= dmu_free_long_object(os
, obj
);
1617 restore_write(struct restorearg
*ra
, objset_t
*os
,
1618 struct drr_write
*drrw
)
1626 if (drrw
->drr_offset
+ drrw
->drr_length
< drrw
->drr_offset
||
1627 !DMU_OT_IS_VALID(drrw
->drr_type
))
1628 return (SET_ERROR(EINVAL
));
1630 if (dmu_object_info(os
, drrw
->drr_object
, NULL
) != 0)
1631 return (SET_ERROR(EINVAL
));
1633 if (dmu_bonus_hold(os
, drrw
->drr_object
, FTAG
, &bonus
) != 0)
1634 return (SET_ERROR(EINVAL
));
1636 abuf
= dmu_request_arcbuf(bonus
, drrw
->drr_length
);
1638 data
= restore_read(ra
, drrw
->drr_length
, abuf
->b_data
);
1640 dmu_return_arcbuf(abuf
);
1641 dmu_buf_rele(bonus
, FTAG
);
1645 tx
= dmu_tx_create(os
);
1647 dmu_tx_hold_write(tx
, drrw
->drr_object
,
1648 drrw
->drr_offset
, drrw
->drr_length
);
1649 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1651 dmu_return_arcbuf(abuf
);
1652 dmu_buf_rele(bonus
, FTAG
);
1657 dmu_object_byteswap_t byteswap
=
1658 DMU_OT_BYTESWAP(drrw
->drr_type
);
1659 dmu_ot_byteswap
[byteswap
].ob_func(data
, drrw
->drr_length
);
1661 dmu_assign_arcbuf(bonus
, drrw
->drr_offset
, abuf
, tx
);
1663 dmu_buf_rele(bonus
, FTAG
);
1668 * Handle a DRR_WRITE_BYREF record. This record is used in dedup'ed
1669 * streams to refer to a copy of the data that is already on the
1670 * system because it came in earlier in the stream. This function
1671 * finds the earlier copy of the data, and uses that copy instead of
1672 * data from the stream to fulfill this write.
1675 restore_write_byref(struct restorearg
*ra
, objset_t
*os
,
1676 struct drr_write_byref
*drrwbr
)
1680 guid_map_entry_t gmesrch
;
1681 guid_map_entry_t
*gmep
;
1683 objset_t
*ref_os
= NULL
;
1686 if (drrwbr
->drr_offset
+ drrwbr
->drr_length
< drrwbr
->drr_offset
)
1687 return (SET_ERROR(EINVAL
));
1690 * If the GUID of the referenced dataset is different from the
1691 * GUID of the target dataset, find the referenced dataset.
1693 if (drrwbr
->drr_toguid
!= drrwbr
->drr_refguid
) {
1694 gmesrch
.guid
= drrwbr
->drr_refguid
;
1695 if ((gmep
= avl_find(ra
->guid_to_ds_map
, &gmesrch
,
1697 return (SET_ERROR(EINVAL
));
1699 if (dmu_objset_from_ds(gmep
->gme_ds
, &ref_os
))
1700 return (SET_ERROR(EINVAL
));
1705 err
= dmu_buf_hold(ref_os
, drrwbr
->drr_refobject
,
1706 drrwbr
->drr_refoffset
, FTAG
, &dbp
, DMU_READ_PREFETCH
);
1710 tx
= dmu_tx_create(os
);
1712 dmu_tx_hold_write(tx
, drrwbr
->drr_object
,
1713 drrwbr
->drr_offset
, drrwbr
->drr_length
);
1714 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1719 dmu_write(os
, drrwbr
->drr_object
,
1720 drrwbr
->drr_offset
, drrwbr
->drr_length
, dbp
->db_data
, tx
);
1721 dmu_buf_rele(dbp
, FTAG
);
1727 restore_write_embedded(struct restorearg
*ra
, objset_t
*os
,
1728 struct drr_write_embedded
*drrwnp
)
1734 if (drrwnp
->drr_offset
+ drrwnp
->drr_length
< drrwnp
->drr_offset
)
1737 if (drrwnp
->drr_psize
> BPE_PAYLOAD_SIZE
)
1740 if (drrwnp
->drr_etype
>= NUM_BP_EMBEDDED_TYPES
)
1742 if (drrwnp
->drr_compression
>= ZIO_COMPRESS_FUNCTIONS
)
1745 data
= restore_read(ra
, P2ROUNDUP(drrwnp
->drr_psize
, 8), NULL
);
1749 tx
= dmu_tx_create(os
);
1751 dmu_tx_hold_write(tx
, drrwnp
->drr_object
,
1752 drrwnp
->drr_offset
, drrwnp
->drr_length
);
1753 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1759 dmu_write_embedded(os
, drrwnp
->drr_object
,
1760 drrwnp
->drr_offset
, data
, drrwnp
->drr_etype
,
1761 drrwnp
->drr_compression
, drrwnp
->drr_lsize
, drrwnp
->drr_psize
,
1762 ra
->byteswap
^ ZFS_HOST_BYTEORDER
, tx
);
1769 restore_spill(struct restorearg
*ra
, objset_t
*os
, struct drr_spill
*drrs
)
1773 dmu_buf_t
*db
, *db_spill
;
1776 if (drrs
->drr_length
< SPA_MINBLOCKSIZE
||
1777 drrs
->drr_length
> spa_maxblocksize(dmu_objset_spa(os
)))
1778 return (SET_ERROR(EINVAL
));
1780 data
= restore_read(ra
, drrs
->drr_length
, NULL
);
1784 if (dmu_object_info(os
, drrs
->drr_object
, NULL
) != 0)
1785 return (SET_ERROR(EINVAL
));
1787 VERIFY(0 == dmu_bonus_hold(os
, drrs
->drr_object
, FTAG
, &db
));
1788 if ((err
= dmu_spill_hold_by_bonus(db
, FTAG
, &db_spill
)) != 0) {
1789 dmu_buf_rele(db
, FTAG
);
1793 tx
= dmu_tx_create(os
);
1795 dmu_tx_hold_spill(tx
, db
->db_object
);
1797 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1799 dmu_buf_rele(db
, FTAG
);
1800 dmu_buf_rele(db_spill
, FTAG
);
1804 dmu_buf_will_dirty(db_spill
, tx
);
1806 if (db_spill
->db_size
< drrs
->drr_length
)
1807 VERIFY(0 == dbuf_spill_set_blksz(db_spill
,
1808 drrs
->drr_length
, tx
));
1809 bcopy(data
, db_spill
->db_data
, drrs
->drr_length
);
1811 dmu_buf_rele(db
, FTAG
);
1812 dmu_buf_rele(db_spill
, FTAG
);
1820 restore_free(struct restorearg
*ra
, objset_t
*os
,
1821 struct drr_free
*drrf
)
1825 if (drrf
->drr_length
!= -1ULL &&
1826 drrf
->drr_offset
+ drrf
->drr_length
< drrf
->drr_offset
)
1827 return (SET_ERROR(EINVAL
));
1829 if (dmu_object_info(os
, drrf
->drr_object
, NULL
) != 0)
1830 return (SET_ERROR(EINVAL
));
1832 err
= dmu_free_long_range(os
, drrf
->drr_object
,
1833 drrf
->drr_offset
, drrf
->drr_length
);
1837 /* used to destroy the drc_ds on error */
1839 dmu_recv_cleanup_ds(dmu_recv_cookie_t
*drc
)
1841 char name
[MAXNAMELEN
];
1842 dsl_dataset_name(drc
->drc_ds
, name
);
1843 dsl_dataset_disown(drc
->drc_ds
, dmu_recv_tag
);
1844 (void) dsl_destroy_head(name
);
1848 * NB: callers *must* call dmu_recv_end() if this succeeds.
1851 dmu_recv_stream(dmu_recv_cookie_t
*drc
, vnode_t
*vp
, offset_t
*voffp
,
1852 int cleanup_fd
, uint64_t *action_handlep
)
1854 struct restorearg ra
= { 0 };
1855 dmu_replay_record_t
*drr
;
1860 ra
.byteswap
= drc
->drc_byteswap
;
1861 ra
.cksum
= drc
->drc_cksum
;
1864 ra
.bufsize
= SPA_MAXBLOCKSIZE
;
1865 ra
.buf
= vmem_alloc(ra
.bufsize
, KM_SLEEP
);
1867 /* these were verified in dmu_recv_begin */
1868 ASSERT3U(DMU_GET_STREAM_HDRTYPE(drc
->drc_drrb
->drr_versioninfo
), ==,
1870 ASSERT3U(drc
->drc_drrb
->drr_type
, <, DMU_OST_NUMTYPES
);
1873 * Open the objset we are modifying.
1875 VERIFY0(dmu_objset_from_ds(drc
->drc_ds
, &os
));
1877 ASSERT(dsl_dataset_phys(drc
->drc_ds
)->ds_flags
& DS_FLAG_INCONSISTENT
);
1879 featureflags
= DMU_GET_FEATUREFLAGS(drc
->drc_drrb
->drr_versioninfo
);
1881 /* if this stream is dedup'ed, set up the avl tree for guid mapping */
1882 if (featureflags
& DMU_BACKUP_FEATURE_DEDUP
) {
1885 if (cleanup_fd
== -1) {
1886 ra
.err
= SET_ERROR(EBADF
);
1889 ra
.err
= zfs_onexit_fd_hold(cleanup_fd
, &minor
);
1895 if (*action_handlep
== 0) {
1897 kmem_alloc(sizeof (avl_tree_t
), KM_SLEEP
);
1898 avl_create(ra
.guid_to_ds_map
, guid_compare
,
1899 sizeof (guid_map_entry_t
),
1900 offsetof(guid_map_entry_t
, avlnode
));
1901 ra
.err
= zfs_onexit_add_cb(minor
,
1902 free_guid_map_onexit
, ra
.guid_to_ds_map
,
1907 ra
.err
= zfs_onexit_cb_data(minor
, *action_handlep
,
1908 (void **)&ra
.guid_to_ds_map
);
1913 drc
->drc_guid_to_ds_map
= ra
.guid_to_ds_map
;
1917 * Read records and process them.
1920 while (ra
.err
== 0 &&
1921 NULL
!= (drr
= restore_read(&ra
, sizeof (*drr
), NULL
))) {
1922 if (issig(JUSTLOOKING
) && issig(FORREAL
)) {
1923 ra
.err
= SET_ERROR(EINTR
);
1928 backup_byteswap(drr
);
1930 switch (drr
->drr_type
) {
1934 * We need to make a copy of the record header,
1935 * because restore_{object,write} may need to
1936 * restore_read(), which will invalidate drr.
1938 struct drr_object drro
= drr
->drr_u
.drr_object
;
1939 ra
.err
= restore_object(&ra
, os
, &drro
);
1942 case DRR_FREEOBJECTS
:
1944 struct drr_freeobjects drrfo
=
1945 drr
->drr_u
.drr_freeobjects
;
1946 ra
.err
= restore_freeobjects(&ra
, os
, &drrfo
);
1951 struct drr_write drrw
= drr
->drr_u
.drr_write
;
1952 ra
.err
= restore_write(&ra
, os
, &drrw
);
1955 case DRR_WRITE_BYREF
:
1957 struct drr_write_byref drrwbr
=
1958 drr
->drr_u
.drr_write_byref
;
1959 ra
.err
= restore_write_byref(&ra
, os
, &drrwbr
);
1962 case DRR_WRITE_EMBEDDED
:
1964 struct drr_write_embedded drrwe
=
1965 drr
->drr_u
.drr_write_embedded
;
1966 ra
.err
= restore_write_embedded(&ra
, os
, &drrwe
);
1971 struct drr_free drrf
= drr
->drr_u
.drr_free
;
1972 ra
.err
= restore_free(&ra
, os
, &drrf
);
1977 struct drr_end drre
= drr
->drr_u
.drr_end
;
1979 * We compare against the *previous* checksum
1980 * value, because the stored checksum is of
1981 * everything before the DRR_END record.
1983 if (!ZIO_CHECKSUM_EQUAL(drre
.drr_checksum
, pcksum
))
1984 ra
.err
= SET_ERROR(ECKSUM
);
1989 struct drr_spill drrs
= drr
->drr_u
.drr_spill
;
1990 ra
.err
= restore_spill(&ra
, os
, &drrs
);
1994 ra
.err
= SET_ERROR(EINVAL
);
1999 ASSERT(ra
.err
!= 0);
2002 if ((featureflags
& DMU_BACKUP_FEATURE_DEDUP
) && (cleanup_fd
!= -1))
2003 zfs_onexit_fd_rele(cleanup_fd
);
2007 * destroy what we created, so we don't leave it in the
2008 * inconsistent restoring state.
2010 dmu_recv_cleanup_ds(drc
);
2013 vmem_free(ra
.buf
, ra
.bufsize
);
2019 dmu_recv_end_check(void *arg
, dmu_tx_t
*tx
)
2021 dmu_recv_cookie_t
*drc
= arg
;
2022 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
2025 ASSERT3P(drc
->drc_ds
->ds_owner
, ==, dmu_recv_tag
);
2027 if (!drc
->drc_newfs
) {
2028 dsl_dataset_t
*origin_head
;
2030 error
= dsl_dataset_hold(dp
, drc
->drc_tofs
, FTAG
, &origin_head
);
2033 if (drc
->drc_force
) {
2035 * We will destroy any snapshots in tofs (i.e. before
2036 * origin_head) that are after the origin (which is
2037 * the snap before drc_ds, because drc_ds can not
2038 * have any snaps of its own).
2042 obj
= dsl_dataset_phys(origin_head
)->ds_prev_snap_obj
;
2044 dsl_dataset_phys(drc
->drc_ds
)->ds_prev_snap_obj
) {
2045 dsl_dataset_t
*snap
;
2046 error
= dsl_dataset_hold_obj(dp
, obj
, FTAG
,
2050 if (snap
->ds_dir
!= origin_head
->ds_dir
)
2051 error
= SET_ERROR(EINVAL
);
2053 error
= dsl_destroy_snapshot_check_impl(
2056 obj
= dsl_dataset_phys(snap
)->ds_prev_snap_obj
;
2057 dsl_dataset_rele(snap
, FTAG
);
2062 dsl_dataset_rele(origin_head
, FTAG
);
2066 error
= dsl_dataset_clone_swap_check_impl(drc
->drc_ds
,
2067 origin_head
, drc
->drc_force
, drc
->drc_owner
, tx
);
2069 dsl_dataset_rele(origin_head
, FTAG
);
2072 error
= dsl_dataset_snapshot_check_impl(origin_head
,
2073 drc
->drc_tosnap
, tx
, B_TRUE
, 1, drc
->drc_cred
);
2074 dsl_dataset_rele(origin_head
, FTAG
);
2078 error
= dsl_destroy_head_check_impl(drc
->drc_ds
, 1);
2080 error
= dsl_dataset_snapshot_check_impl(drc
->drc_ds
,
2081 drc
->drc_tosnap
, tx
, B_TRUE
, 1, drc
->drc_cred
);
2087 dmu_recv_end_sync(void *arg
, dmu_tx_t
*tx
)
2089 dmu_recv_cookie_t
*drc
= arg
;
2090 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
2092 spa_history_log_internal_ds(drc
->drc_ds
, "finish receiving",
2093 tx
, "snap=%s", drc
->drc_tosnap
);
2095 if (!drc
->drc_newfs
) {
2096 dsl_dataset_t
*origin_head
;
2098 VERIFY0(dsl_dataset_hold(dp
, drc
->drc_tofs
, FTAG
,
2101 if (drc
->drc_force
) {
2103 * Destroy any snapshots of drc_tofs (origin_head)
2104 * after the origin (the snap before drc_ds).
2108 obj
= dsl_dataset_phys(origin_head
)->ds_prev_snap_obj
;
2110 dsl_dataset_phys(drc
->drc_ds
)->ds_prev_snap_obj
) {
2111 dsl_dataset_t
*snap
;
2112 VERIFY0(dsl_dataset_hold_obj(dp
, obj
, FTAG
,
2114 ASSERT3P(snap
->ds_dir
, ==, origin_head
->ds_dir
);
2115 obj
= dsl_dataset_phys(snap
)->ds_prev_snap_obj
;
2116 dsl_destroy_snapshot_sync_impl(snap
,
2118 dsl_dataset_rele(snap
, FTAG
);
2121 VERIFY3P(drc
->drc_ds
->ds_prev
, ==,
2122 origin_head
->ds_prev
);
2124 dsl_dataset_clone_swap_sync_impl(drc
->drc_ds
,
2126 dsl_dataset_snapshot_sync_impl(origin_head
,
2127 drc
->drc_tosnap
, tx
);
2129 /* set snapshot's creation time and guid */
2130 dmu_buf_will_dirty(origin_head
->ds_prev
->ds_dbuf
, tx
);
2131 dsl_dataset_phys(origin_head
->ds_prev
)->ds_creation_time
=
2132 drc
->drc_drrb
->drr_creation_time
;
2133 dsl_dataset_phys(origin_head
->ds_prev
)->ds_guid
=
2134 drc
->drc_drrb
->drr_toguid
;
2135 dsl_dataset_phys(origin_head
->ds_prev
)->ds_flags
&=
2136 ~DS_FLAG_INCONSISTENT
;
2138 dmu_buf_will_dirty(origin_head
->ds_dbuf
, tx
);
2139 dsl_dataset_phys(origin_head
)->ds_flags
&=
2140 ~DS_FLAG_INCONSISTENT
;
2142 dsl_dataset_rele(origin_head
, FTAG
);
2143 dsl_destroy_head_sync_impl(drc
->drc_ds
, tx
);
2145 if (drc
->drc_owner
!= NULL
)
2146 VERIFY3P(origin_head
->ds_owner
, ==, drc
->drc_owner
);
2148 dsl_dataset_t
*ds
= drc
->drc_ds
;
2150 dsl_dataset_snapshot_sync_impl(ds
, drc
->drc_tosnap
, tx
);
2152 /* set snapshot's creation time and guid */
2153 dmu_buf_will_dirty(ds
->ds_prev
->ds_dbuf
, tx
);
2154 dsl_dataset_phys(ds
->ds_prev
)->ds_creation_time
=
2155 drc
->drc_drrb
->drr_creation_time
;
2156 dsl_dataset_phys(ds
->ds_prev
)->ds_guid
=
2157 drc
->drc_drrb
->drr_toguid
;
2158 dsl_dataset_phys(ds
->ds_prev
)->ds_flags
&=
2159 ~DS_FLAG_INCONSISTENT
;
2161 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
2162 dsl_dataset_phys(ds
)->ds_flags
&= ~DS_FLAG_INCONSISTENT
;
2164 drc
->drc_newsnapobj
= dsl_dataset_phys(drc
->drc_ds
)->ds_prev_snap_obj
;
2166 * Release the hold from dmu_recv_begin. This must be done before
2167 * we return to open context, so that when we free the dataset's dnode,
2168 * we can evict its bonus buffer.
2170 dsl_dataset_disown(drc
->drc_ds
, dmu_recv_tag
);
2175 add_ds_to_guidmap(const char *name
, avl_tree_t
*guid_map
, uint64_t snapobj
)
2178 dsl_dataset_t
*snapds
;
2179 guid_map_entry_t
*gmep
;
2182 ASSERT(guid_map
!= NULL
);
2184 err
= dsl_pool_hold(name
, FTAG
, &dp
);
2187 gmep
= kmem_alloc(sizeof (*gmep
), KM_SLEEP
);
2188 err
= dsl_dataset_hold_obj(dp
, snapobj
, gmep
, &snapds
);
2190 gmep
->guid
= dsl_dataset_phys(snapds
)->ds_guid
;
2191 gmep
->gme_ds
= snapds
;
2192 avl_add(guid_map
, gmep
);
2193 dsl_dataset_long_hold(snapds
, gmep
);
2195 kmem_free(gmep
, sizeof (*gmep
));
2198 dsl_pool_rele(dp
, FTAG
);
2202 static int dmu_recv_end_modified_blocks
= 3;
2205 dmu_recv_existing_end(dmu_recv_cookie_t
*drc
)
2213 * We will be destroying the ds; make sure its origin is unmounted if
2216 name
= kmem_alloc(MAXNAMELEN
, KM_SLEEP
);
2217 dsl_dataset_name(drc
->drc_ds
, name
);
2218 zfs_destroy_unmount_origin(name
);
2219 kmem_free(name
, MAXNAMELEN
);
2222 error
= dsl_sync_task(drc
->drc_tofs
,
2223 dmu_recv_end_check
, dmu_recv_end_sync
, drc
,
2224 dmu_recv_end_modified_blocks
, ZFS_SPACE_CHECK_NORMAL
);
2227 dmu_recv_cleanup_ds(drc
);
2232 dmu_recv_new_end(dmu_recv_cookie_t
*drc
)
2236 error
= dsl_sync_task(drc
->drc_tofs
,
2237 dmu_recv_end_check
, dmu_recv_end_sync
, drc
,
2238 dmu_recv_end_modified_blocks
, ZFS_SPACE_CHECK_NORMAL
);
2241 dmu_recv_cleanup_ds(drc
);
2242 } else if (drc
->drc_guid_to_ds_map
!= NULL
) {
2243 (void) add_ds_to_guidmap(drc
->drc_tofs
,
2244 drc
->drc_guid_to_ds_map
,
2245 drc
->drc_newsnapobj
);
2251 dmu_recv_end(dmu_recv_cookie_t
*drc
, void *owner
)
2253 drc
->drc_owner
= owner
;
2256 return (dmu_recv_new_end(drc
));
2258 return (dmu_recv_existing_end(drc
));
2262 * Return TRUE if this objset is currently being received into.
2265 dmu_objset_is_receiving(objset_t
*os
)
2267 return (os
->os_dsl_dataset
!= NULL
&&
2268 os
->os_dsl_dataset
->ds_owner
== dmu_recv_tag
);
2271 #if defined(_KERNEL)
2272 module_param(zfs_send_corrupt_data
, int, 0644);
2273 MODULE_PARM_DESC(zfs_send_corrupt_data
, "Allow sending corrupt data");