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_IS_EMBEDDED(bp
)) {
239 * There's no pre-computed checksum of embedded BP's, so
240 * (like fletcher4-checkummed blocks) userland will have
241 * to compute a dedup-capable checksum itself.
243 drrw
->drr_checksumtype
= ZIO_CHECKSUM_OFF
;
245 drrw
->drr_checksumtype
= BP_GET_CHECKSUM(bp
);
246 if (zio_checksum_table
[drrw
->drr_checksumtype
].ci_dedup
)
247 drrw
->drr_checksumflags
|= DRR_CHECKSUM_DEDUP
;
248 DDK_SET_LSIZE(&drrw
->drr_key
, BP_GET_LSIZE(bp
));
249 DDK_SET_PSIZE(&drrw
->drr_key
, BP_GET_PSIZE(bp
));
250 DDK_SET_COMPRESS(&drrw
->drr_key
, BP_GET_COMPRESS(bp
));
251 drrw
->drr_key
.ddk_cksum
= bp
->blk_cksum
;
254 if (dump_bytes(dsp
, dsp
->dsa_drr
, sizeof (dmu_replay_record_t
)) != 0)
255 return (SET_ERROR(EINTR
));
256 if (dump_bytes(dsp
, data
, blksz
) != 0)
257 return (SET_ERROR(EINTR
));
262 dump_write_embedded(dmu_sendarg_t
*dsp
, uint64_t object
, uint64_t offset
,
263 int blksz
, const blkptr_t
*bp
)
265 char buf
[BPE_PAYLOAD_SIZE
];
266 struct drr_write_embedded
*drrw
=
267 &(dsp
->dsa_drr
->drr_u
.drr_write_embedded
);
269 if (dsp
->dsa_pending_op
!= PENDING_NONE
) {
270 if (dump_bytes(dsp
, dsp
->dsa_drr
,
271 sizeof (dmu_replay_record_t
)) != 0)
273 dsp
->dsa_pending_op
= PENDING_NONE
;
276 ASSERT(BP_IS_EMBEDDED(bp
));
278 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
279 dsp
->dsa_drr
->drr_type
= DRR_WRITE_EMBEDDED
;
280 drrw
->drr_object
= object
;
281 drrw
->drr_offset
= offset
;
282 drrw
->drr_length
= blksz
;
283 drrw
->drr_toguid
= dsp
->dsa_toguid
;
284 drrw
->drr_compression
= BP_GET_COMPRESS(bp
);
285 drrw
->drr_etype
= BPE_GET_ETYPE(bp
);
286 drrw
->drr_lsize
= BPE_GET_LSIZE(bp
);
287 drrw
->drr_psize
= BPE_GET_PSIZE(bp
);
289 decode_embedded_bp_compressed(bp
, buf
);
291 if (dump_bytes(dsp
, dsp
->dsa_drr
, sizeof (dmu_replay_record_t
)) != 0)
293 if (dump_bytes(dsp
, buf
, P2ROUNDUP(drrw
->drr_psize
, 8)) != 0)
299 dump_spill(dmu_sendarg_t
*dsp
, uint64_t object
, int blksz
, void *data
)
301 struct drr_spill
*drrs
= &(dsp
->dsa_drr
->drr_u
.drr_spill
);
303 if (dsp
->dsa_pending_op
!= PENDING_NONE
) {
304 if (dump_bytes(dsp
, dsp
->dsa_drr
,
305 sizeof (dmu_replay_record_t
)) != 0)
306 return (SET_ERROR(EINTR
));
307 dsp
->dsa_pending_op
= PENDING_NONE
;
310 /* write a SPILL record */
311 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
312 dsp
->dsa_drr
->drr_type
= DRR_SPILL
;
313 drrs
->drr_object
= object
;
314 drrs
->drr_length
= blksz
;
315 drrs
->drr_toguid
= dsp
->dsa_toguid
;
317 if (dump_bytes(dsp
, dsp
->dsa_drr
, sizeof (dmu_replay_record_t
)))
318 return (SET_ERROR(EINTR
));
319 if (dump_bytes(dsp
, data
, blksz
))
320 return (SET_ERROR(EINTR
));
325 dump_freeobjects(dmu_sendarg_t
*dsp
, uint64_t firstobj
, uint64_t numobjs
)
327 struct drr_freeobjects
*drrfo
= &(dsp
->dsa_drr
->drr_u
.drr_freeobjects
);
329 /* See comment in dump_free(). */
330 if (!dsp
->dsa_incremental
)
334 * If there is a pending op, but it's not PENDING_FREEOBJECTS,
335 * push it out, since free block aggregation can only be done for
336 * blocks of the same type (i.e., DRR_FREE records can only be
337 * aggregated with other DRR_FREE records. DRR_FREEOBJECTS records
338 * can only be aggregated with other DRR_FREEOBJECTS records.
340 if (dsp
->dsa_pending_op
!= PENDING_NONE
&&
341 dsp
->dsa_pending_op
!= PENDING_FREEOBJECTS
) {
342 if (dump_bytes(dsp
, dsp
->dsa_drr
,
343 sizeof (dmu_replay_record_t
)) != 0)
344 return (SET_ERROR(EINTR
));
345 dsp
->dsa_pending_op
= PENDING_NONE
;
347 if (dsp
->dsa_pending_op
== PENDING_FREEOBJECTS
) {
349 * See whether this free object array can be aggregated
352 if (drrfo
->drr_firstobj
+ drrfo
->drr_numobjs
== firstobj
) {
353 drrfo
->drr_numobjs
+= numobjs
;
356 /* can't be aggregated. Push out pending record */
357 if (dump_bytes(dsp
, dsp
->dsa_drr
,
358 sizeof (dmu_replay_record_t
)) != 0)
359 return (SET_ERROR(EINTR
));
360 dsp
->dsa_pending_op
= PENDING_NONE
;
364 /* write a FREEOBJECTS record */
365 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
366 dsp
->dsa_drr
->drr_type
= DRR_FREEOBJECTS
;
367 drrfo
->drr_firstobj
= firstobj
;
368 drrfo
->drr_numobjs
= numobjs
;
369 drrfo
->drr_toguid
= dsp
->dsa_toguid
;
371 dsp
->dsa_pending_op
= PENDING_FREEOBJECTS
;
377 dump_dnode(dmu_sendarg_t
*dsp
, uint64_t object
, dnode_phys_t
*dnp
)
379 struct drr_object
*drro
= &(dsp
->dsa_drr
->drr_u
.drr_object
);
381 if (dnp
== NULL
|| dnp
->dn_type
== DMU_OT_NONE
)
382 return (dump_freeobjects(dsp
, object
, 1));
384 if (dsp
->dsa_pending_op
!= PENDING_NONE
) {
385 if (dump_bytes(dsp
, dsp
->dsa_drr
,
386 sizeof (dmu_replay_record_t
)) != 0)
387 return (SET_ERROR(EINTR
));
388 dsp
->dsa_pending_op
= PENDING_NONE
;
391 /* write an OBJECT record */
392 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
393 dsp
->dsa_drr
->drr_type
= DRR_OBJECT
;
394 drro
->drr_object
= object
;
395 drro
->drr_type
= dnp
->dn_type
;
396 drro
->drr_bonustype
= dnp
->dn_bonustype
;
397 drro
->drr_blksz
= dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
;
398 drro
->drr_bonuslen
= dnp
->dn_bonuslen
;
399 drro
->drr_checksumtype
= dnp
->dn_checksum
;
400 drro
->drr_compress
= dnp
->dn_compress
;
401 drro
->drr_toguid
= dsp
->dsa_toguid
;
403 if (dump_bytes(dsp
, dsp
->dsa_drr
, sizeof (dmu_replay_record_t
)) != 0)
404 return (SET_ERROR(EINTR
));
406 if (dump_bytes(dsp
, DN_BONUS(dnp
), P2ROUNDUP(dnp
->dn_bonuslen
, 8)) != 0)
407 return (SET_ERROR(EINTR
));
409 /* Free anything past the end of the file. */
410 if (dump_free(dsp
, object
, (dnp
->dn_maxblkid
+ 1) *
411 (dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
), -1ULL) != 0)
412 return (SET_ERROR(EINTR
));
413 if (dsp
->dsa_err
!= 0)
414 return (SET_ERROR(EINTR
));
419 backup_do_embed(dmu_sendarg_t
*dsp
, const blkptr_t
*bp
)
421 if (!BP_IS_EMBEDDED(bp
))
425 * Compression function must be legacy, or explicitly enabled.
427 if ((BP_GET_COMPRESS(bp
) >= ZIO_COMPRESS_LEGACY_FUNCTIONS
&&
428 !(dsp
->dsa_featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA_LZ4
)))
432 * Embed type must be explicitly enabled.
434 switch (BPE_GET_ETYPE(bp
)) {
435 case BP_EMBEDDED_TYPE_DATA
:
436 if (dsp
->dsa_featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA
)
445 #define BP_SPAN(dnp, level) \
446 (((uint64_t)dnp->dn_datablkszsec) << (SPA_MINBLOCKSHIFT + \
447 (level) * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT)))
451 backup_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
452 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
454 dmu_sendarg_t
*dsp
= arg
;
455 dmu_object_type_t type
= bp
? BP_GET_TYPE(bp
) : DMU_OT_NONE
;
458 if (issig(JUSTLOOKING
) && issig(FORREAL
))
459 return (SET_ERROR(EINTR
));
461 if (zb
->zb_object
!= DMU_META_DNODE_OBJECT
&&
462 DMU_OBJECT_IS_SPECIAL(zb
->zb_object
)) {
464 } else if (zb
->zb_level
== ZB_ZIL_LEVEL
) {
466 * If we are sending a non-snapshot (which is allowed on
467 * read-only pools), it may have a ZIL, which must be ignored.
470 } else if (BP_IS_HOLE(bp
) &&
471 zb
->zb_object
== DMU_META_DNODE_OBJECT
) {
472 uint64_t span
= BP_SPAN(dnp
, zb
->zb_level
);
473 uint64_t dnobj
= (zb
->zb_blkid
* span
) >> DNODE_SHIFT
;
474 err
= dump_freeobjects(dsp
, dnobj
, span
>> DNODE_SHIFT
);
475 } else if (BP_IS_HOLE(bp
)) {
476 uint64_t span
= BP_SPAN(dnp
, zb
->zb_level
);
477 err
= dump_free(dsp
, zb
->zb_object
, zb
->zb_blkid
* span
, span
);
478 } else if (zb
->zb_level
> 0 || type
== DMU_OT_OBJSET
) {
480 } else if (type
== DMU_OT_DNODE
) {
483 int blksz
= BP_GET_LSIZE(bp
);
484 uint32_t aflags
= ARC_WAIT
;
487 if (arc_read(NULL
, spa
, bp
, arc_getbuf_func
, &abuf
,
488 ZIO_PRIORITY_ASYNC_READ
, ZIO_FLAG_CANFAIL
,
490 return (SET_ERROR(EIO
));
493 for (i
= 0; i
< blksz
>> DNODE_SHIFT
; i
++) {
494 uint64_t dnobj
= (zb
->zb_blkid
<<
495 (DNODE_BLOCK_SHIFT
- DNODE_SHIFT
)) + i
;
496 err
= dump_dnode(dsp
, dnobj
, blk
+i
);
500 (void) arc_buf_remove_ref(abuf
, &abuf
);
501 } else if (type
== DMU_OT_SA
) {
502 uint32_t aflags
= ARC_WAIT
;
504 int blksz
= BP_GET_LSIZE(bp
);
506 if (arc_read(NULL
, spa
, bp
, arc_getbuf_func
, &abuf
,
507 ZIO_PRIORITY_ASYNC_READ
, ZIO_FLAG_CANFAIL
,
509 return (SET_ERROR(EIO
));
511 err
= dump_spill(dsp
, zb
->zb_object
, blksz
, abuf
->b_data
);
512 (void) arc_buf_remove_ref(abuf
, &abuf
);
513 } else if (backup_do_embed(dsp
, bp
)) {
514 /* it's an embedded level-0 block of a regular object */
515 int blksz
= dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
;
516 err
= dump_write_embedded(dsp
, zb
->zb_object
,
517 zb
->zb_blkid
* blksz
, blksz
, bp
);
518 } else { /* it's a level-0 block of a regular object */
519 uint32_t aflags
= ARC_WAIT
;
521 int blksz
= BP_GET_LSIZE(bp
);
523 ASSERT3U(blksz
, ==, dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
);
524 ASSERT0(zb
->zb_level
);
525 if (arc_read(NULL
, spa
, bp
, arc_getbuf_func
, &abuf
,
526 ZIO_PRIORITY_ASYNC_READ
, ZIO_FLAG_CANFAIL
,
528 if (zfs_send_corrupt_data
) {
530 /* Send a block filled with 0x"zfs badd bloc" */
531 abuf
= arc_buf_alloc(spa
, blksz
, &abuf
,
533 for (ptr
= abuf
->b_data
;
534 (char *)ptr
< (char *)abuf
->b_data
+ blksz
;
536 *ptr
= 0x2f5baddb10cULL
;
538 return (SET_ERROR(EIO
));
542 err
= dump_write(dsp
, type
, zb
->zb_object
, zb
->zb_blkid
* blksz
,
543 blksz
, bp
, abuf
->b_data
);
544 (void) arc_buf_remove_ref(abuf
, &abuf
);
547 ASSERT(err
== 0 || err
== EINTR
);
552 * Releases dp using the specified tag.
555 dmu_send_impl(void *tag
, dsl_pool_t
*dp
, dsl_dataset_t
*ds
,
556 zfs_bookmark_phys_t
*fromzb
, boolean_t is_clone
, boolean_t embedok
,
557 int outfd
, vnode_t
*vp
, offset_t
*off
)
560 dmu_replay_record_t
*drr
;
563 uint64_t fromtxg
= 0;
564 uint64_t featureflags
= 0;
566 err
= dmu_objset_from_ds(ds
, &os
);
568 dsl_pool_rele(dp
, tag
);
572 drr
= kmem_zalloc(sizeof (dmu_replay_record_t
), KM_SLEEP
);
573 drr
->drr_type
= DRR_BEGIN
;
574 drr
->drr_u
.drr_begin
.drr_magic
= DMU_BACKUP_MAGIC
;
575 DMU_SET_STREAM_HDRTYPE(drr
->drr_u
.drr_begin
.drr_versioninfo
,
579 if (dmu_objset_type(os
) == DMU_OST_ZFS
) {
581 if (zfs_get_zplprop(os
, ZFS_PROP_VERSION
, &version
) != 0) {
582 kmem_free(drr
, sizeof (dmu_replay_record_t
));
583 dsl_pool_rele(dp
, tag
);
584 return (SET_ERROR(EINVAL
));
586 if (version
>= ZPL_VERSION_SA
) {
587 featureflags
|= DMU_BACKUP_FEATURE_SA_SPILL
;
593 spa_feature_is_active(dp
->dp_spa
, SPA_FEATURE_EMBEDDED_DATA
)) {
594 featureflags
|= DMU_BACKUP_FEATURE_EMBED_DATA
;
595 if (spa_feature_is_active(dp
->dp_spa
, SPA_FEATURE_LZ4_COMPRESS
))
596 featureflags
|= DMU_BACKUP_FEATURE_EMBED_DATA_LZ4
;
601 DMU_SET_FEATUREFLAGS(drr
->drr_u
.drr_begin
.drr_versioninfo
,
604 drr
->drr_u
.drr_begin
.drr_creation_time
=
605 dsl_dataset_phys(ds
)->ds_creation_time
;
606 drr
->drr_u
.drr_begin
.drr_type
= dmu_objset_type(os
);
608 drr
->drr_u
.drr_begin
.drr_flags
|= DRR_FLAG_CLONE
;
609 drr
->drr_u
.drr_begin
.drr_toguid
= dsl_dataset_phys(ds
)->ds_guid
;
610 if (dsl_dataset_phys(ds
)->ds_flags
& DS_FLAG_CI_DATASET
)
611 drr
->drr_u
.drr_begin
.drr_flags
|= DRR_FLAG_CI_DATA
;
613 if (fromzb
!= NULL
) {
614 drr
->drr_u
.drr_begin
.drr_fromguid
= fromzb
->zbm_guid
;
615 fromtxg
= fromzb
->zbm_creation_txg
;
617 dsl_dataset_name(ds
, drr
->drr_u
.drr_begin
.drr_toname
);
618 if (!ds
->ds_is_snapshot
) {
619 (void) strlcat(drr
->drr_u
.drr_begin
.drr_toname
, "@--head--",
620 sizeof (drr
->drr_u
.drr_begin
.drr_toname
));
623 dsp
= kmem_zalloc(sizeof (dmu_sendarg_t
), KM_SLEEP
);
627 dsp
->dsa_outfd
= outfd
;
628 dsp
->dsa_proc
= curproc
;
631 dsp
->dsa_toguid
= dsl_dataset_phys(ds
)->ds_guid
;
632 ZIO_SET_CHECKSUM(&dsp
->dsa_zc
, 0, 0, 0, 0);
633 dsp
->dsa_pending_op
= PENDING_NONE
;
634 dsp
->dsa_incremental
= (fromzb
!= NULL
);
635 dsp
->dsa_featureflags
= featureflags
;
637 mutex_enter(&ds
->ds_sendstream_lock
);
638 list_insert_head(&ds
->ds_sendstreams
, dsp
);
639 mutex_exit(&ds
->ds_sendstream_lock
);
641 dsl_dataset_long_hold(ds
, FTAG
);
642 dsl_pool_rele(dp
, tag
);
644 if (dump_bytes(dsp
, drr
, sizeof (dmu_replay_record_t
)) != 0) {
649 err
= traverse_dataset(ds
, fromtxg
, TRAVERSE_PRE
| TRAVERSE_PREFETCH
,
652 if (dsp
->dsa_pending_op
!= PENDING_NONE
)
653 if (dump_bytes(dsp
, drr
, sizeof (dmu_replay_record_t
)) != 0)
654 err
= SET_ERROR(EINTR
);
657 if (err
== EINTR
&& dsp
->dsa_err
!= 0)
662 bzero(drr
, sizeof (dmu_replay_record_t
));
663 drr
->drr_type
= DRR_END
;
664 drr
->drr_u
.drr_end
.drr_checksum
= dsp
->dsa_zc
;
665 drr
->drr_u
.drr_end
.drr_toguid
= dsp
->dsa_toguid
;
667 if (dump_bytes(dsp
, drr
, sizeof (dmu_replay_record_t
)) != 0) {
673 mutex_enter(&ds
->ds_sendstream_lock
);
674 list_remove(&ds
->ds_sendstreams
, dsp
);
675 mutex_exit(&ds
->ds_sendstream_lock
);
677 kmem_free(drr
, sizeof (dmu_replay_record_t
));
678 kmem_free(dsp
, sizeof (dmu_sendarg_t
));
680 dsl_dataset_long_rele(ds
, FTAG
);
686 dmu_send_obj(const char *pool
, uint64_t tosnap
, uint64_t fromsnap
,
687 boolean_t embedok
, int outfd
, vnode_t
*vp
, offset_t
*off
)
691 dsl_dataset_t
*fromds
= NULL
;
694 err
= dsl_pool_hold(pool
, FTAG
, &dp
);
698 err
= dsl_dataset_hold_obj(dp
, tosnap
, FTAG
, &ds
);
700 dsl_pool_rele(dp
, FTAG
);
705 zfs_bookmark_phys_t zb
;
708 err
= dsl_dataset_hold_obj(dp
, fromsnap
, FTAG
, &fromds
);
710 dsl_dataset_rele(ds
, FTAG
);
711 dsl_pool_rele(dp
, FTAG
);
714 if (!dsl_dataset_is_before(ds
, fromds
, 0))
715 err
= SET_ERROR(EXDEV
);
716 zb
.zbm_creation_time
=
717 dsl_dataset_phys(fromds
)->ds_creation_time
;
718 zb
.zbm_creation_txg
= dsl_dataset_phys(fromds
)->ds_creation_txg
;
719 zb
.zbm_guid
= dsl_dataset_phys(fromds
)->ds_guid
;
720 is_clone
= (fromds
->ds_dir
!= ds
->ds_dir
);
721 dsl_dataset_rele(fromds
, FTAG
);
722 err
= dmu_send_impl(FTAG
, dp
, ds
, &zb
, is_clone
, embedok
,
725 err
= dmu_send_impl(FTAG
, dp
, ds
, NULL
, B_FALSE
, embedok
,
728 dsl_dataset_rele(ds
, FTAG
);
733 dmu_send(const char *tosnap
, const char *fromsnap
, boolean_t embedok
,
734 int outfd
, vnode_t
*vp
, offset_t
*off
)
739 boolean_t owned
= B_FALSE
;
741 if (fromsnap
!= NULL
&& strpbrk(fromsnap
, "@#") == NULL
)
742 return (SET_ERROR(EINVAL
));
744 err
= dsl_pool_hold(tosnap
, FTAG
, &dp
);
748 if (strchr(tosnap
, '@') == NULL
&& spa_writeable(dp
->dp_spa
)) {
750 * We are sending a filesystem or volume. Ensure
751 * that it doesn't change by owning the dataset.
753 err
= dsl_dataset_own(dp
, tosnap
, FTAG
, &ds
);
756 err
= dsl_dataset_hold(dp
, tosnap
, FTAG
, &ds
);
759 dsl_pool_rele(dp
, FTAG
);
763 if (fromsnap
!= NULL
) {
764 zfs_bookmark_phys_t zb
;
765 boolean_t is_clone
= B_FALSE
;
766 int fsnamelen
= strchr(tosnap
, '@') - tosnap
;
769 * If the fromsnap is in a different filesystem, then
770 * mark the send stream as a clone.
772 if (strncmp(tosnap
, fromsnap
, fsnamelen
) != 0 ||
773 (fromsnap
[fsnamelen
] != '@' &&
774 fromsnap
[fsnamelen
] != '#')) {
778 if (strchr(fromsnap
, '@')) {
779 dsl_dataset_t
*fromds
;
780 err
= dsl_dataset_hold(dp
, fromsnap
, FTAG
, &fromds
);
782 if (!dsl_dataset_is_before(ds
, fromds
, 0))
783 err
= SET_ERROR(EXDEV
);
784 zb
.zbm_creation_time
=
785 dsl_dataset_phys(fromds
)->ds_creation_time
;
786 zb
.zbm_creation_txg
=
787 dsl_dataset_phys(fromds
)->ds_creation_txg
;
788 zb
.zbm_guid
= dsl_dataset_phys(fromds
)->ds_guid
;
789 is_clone
= (ds
->ds_dir
!= fromds
->ds_dir
);
790 dsl_dataset_rele(fromds
, FTAG
);
793 err
= dsl_bookmark_lookup(dp
, fromsnap
, ds
, &zb
);
796 dsl_dataset_rele(ds
, FTAG
);
797 dsl_pool_rele(dp
, FTAG
);
800 err
= dmu_send_impl(FTAG
, dp
, ds
, &zb
, is_clone
, embedok
,
803 err
= dmu_send_impl(FTAG
, dp
, ds
, NULL
, B_FALSE
, embedok
,
807 dsl_dataset_disown(ds
, FTAG
);
809 dsl_dataset_rele(ds
, FTAG
);
814 dmu_send_estimate(dsl_dataset_t
*ds
, dsl_dataset_t
*fromds
, uint64_t *sizep
)
817 uint64_t size
, recordsize
;
818 ASSERTV(dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
);
820 ASSERT(dsl_pool_config_held(dp
));
822 /* tosnap must be a snapshot */
823 if (!ds
->ds_is_snapshot
)
824 return (SET_ERROR(EINVAL
));
827 * fromsnap must be an earlier snapshot from the same fs as tosnap,
828 * or the origin's fs.
830 if (fromds
!= NULL
&& !dsl_dataset_is_before(ds
, fromds
, 0))
831 return (SET_ERROR(EXDEV
));
833 /* Get uncompressed size estimate of changed data. */
834 if (fromds
== NULL
) {
835 size
= dsl_dataset_phys(ds
)->ds_uncompressed_bytes
;
838 err
= dsl_dataset_space_written(fromds
, ds
,
839 &used
, &comp
, &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.
859 err
= dsl_prop_get_int_ds(ds
, "recordsize", &recordsize
);
862 size
-= size
/ recordsize
* sizeof (blkptr_t
);
864 /* Add in the space for the record associated with each block. */
865 size
+= size
/ recordsize
* sizeof (dmu_replay_record_t
);
872 typedef struct dmu_recv_begin_arg
{
873 const char *drba_origin
;
874 dmu_recv_cookie_t
*drba_cookie
;
876 uint64_t drba_snapobj
;
877 } dmu_recv_begin_arg_t
;
880 recv_begin_check_existing_impl(dmu_recv_begin_arg_t
*drba
, dsl_dataset_t
*ds
,
885 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
887 /* temporary clone name must not exist */
888 error
= zap_lookup(dp
->dp_meta_objset
,
889 dsl_dir_phys(ds
->ds_dir
)->dd_child_dir_zapobj
, recv_clone_name
,
892 return (error
== 0 ? EBUSY
: error
);
894 /* new snapshot name must not exist */
895 error
= zap_lookup(dp
->dp_meta_objset
,
896 dsl_dataset_phys(ds
)->ds_snapnames_zapobj
,
897 drba
->drba_cookie
->drc_tosnap
, 8, 1, &val
);
899 return (error
== 0 ? EEXIST
: error
);
902 * Check snapshot limit before receiving. We'll recheck again at the
903 * end, but might as well abort before receiving if we're already over
906 * Note that we do not check the file system limit with
907 * dsl_dir_fscount_check because the temporary %clones don't count
908 * against that limit.
910 error
= dsl_fs_ss_limit_check(ds
->ds_dir
, 1, ZFS_PROP_SNAPSHOT_LIMIT
,
911 NULL
, drba
->drba_cred
);
917 uint64_t obj
= dsl_dataset_phys(ds
)->ds_prev_snap_obj
;
919 /* Find snapshot in this dir that matches fromguid. */
921 error
= dsl_dataset_hold_obj(dp
, obj
, FTAG
,
924 return (SET_ERROR(ENODEV
));
925 if (snap
->ds_dir
!= ds
->ds_dir
) {
926 dsl_dataset_rele(snap
, FTAG
);
927 return (SET_ERROR(ENODEV
));
929 if (dsl_dataset_phys(snap
)->ds_guid
== fromguid
)
931 obj
= dsl_dataset_phys(snap
)->ds_prev_snap_obj
;
932 dsl_dataset_rele(snap
, FTAG
);
935 return (SET_ERROR(ENODEV
));
937 if (drba
->drba_cookie
->drc_force
) {
938 drba
->drba_snapobj
= obj
;
941 * If we are not forcing, there must be no
942 * changes since fromsnap.
944 if (dsl_dataset_modified_since_snap(ds
, snap
)) {
945 dsl_dataset_rele(snap
, FTAG
);
946 return (SET_ERROR(ETXTBSY
));
948 drba
->drba_snapobj
= ds
->ds_prev
->ds_object
;
951 dsl_dataset_rele(snap
, FTAG
);
953 /* if full, most recent snapshot must be $ORIGIN */
954 if (dsl_dataset_phys(ds
)->ds_prev_snap_txg
>= TXG_INITIAL
)
955 return (SET_ERROR(ENODEV
));
956 drba
->drba_snapobj
= dsl_dataset_phys(ds
)->ds_prev_snap_obj
;
964 dmu_recv_begin_check(void *arg
, dmu_tx_t
*tx
)
966 dmu_recv_begin_arg_t
*drba
= arg
;
967 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
968 struct drr_begin
*drrb
= drba
->drba_cookie
->drc_drrb
;
969 uint64_t fromguid
= drrb
->drr_fromguid
;
970 int flags
= drrb
->drr_flags
;
972 uint64_t featureflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
974 const char *tofs
= drba
->drba_cookie
->drc_tofs
;
976 /* already checked */
977 ASSERT3U(drrb
->drr_magic
, ==, DMU_BACKUP_MAGIC
);
979 if (DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) ==
980 DMU_COMPOUNDSTREAM
||
981 drrb
->drr_type
>= DMU_OST_NUMTYPES
||
982 ((flags
& DRR_FLAG_CLONE
) && drba
->drba_origin
== NULL
))
983 return (SET_ERROR(EINVAL
));
985 /* Verify pool version supports SA if SA_SPILL feature set */
986 if ((featureflags
& DMU_BACKUP_FEATURE_SA_SPILL
) &&
987 spa_version(dp
->dp_spa
) < SPA_VERSION_SA
)
988 return (SET_ERROR(ENOTSUP
));
991 * The receiving code doesn't know how to translate a WRITE_EMBEDDED
992 * record to a plan WRITE record, so the pool must have the
993 * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
994 * records. Same with WRITE_EMBEDDED records that use LZ4 compression.
996 if ((featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA
) &&
997 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_EMBEDDED_DATA
))
998 return (SET_ERROR(ENOTSUP
));
999 if ((featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA_LZ4
) &&
1000 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_LZ4_COMPRESS
))
1001 return (SET_ERROR(ENOTSUP
));
1003 error
= dsl_dataset_hold(dp
, tofs
, FTAG
, &ds
);
1005 /* target fs already exists; recv into temp clone */
1007 /* Can't recv a clone into an existing fs */
1008 if (flags
& DRR_FLAG_CLONE
) {
1009 dsl_dataset_rele(ds
, FTAG
);
1010 return (SET_ERROR(EINVAL
));
1013 error
= recv_begin_check_existing_impl(drba
, ds
, fromguid
);
1014 dsl_dataset_rele(ds
, FTAG
);
1015 } else if (error
== ENOENT
) {
1016 /* target fs does not exist; must be a full backup or clone */
1017 char buf
[MAXNAMELEN
];
1020 * If it's a non-clone incremental, we are missing the
1021 * target fs, so fail the recv.
1023 if (fromguid
!= 0 && !(flags
& DRR_FLAG_CLONE
))
1024 return (SET_ERROR(ENOENT
));
1026 /* Open the parent of tofs */
1027 ASSERT3U(strlen(tofs
), <, MAXNAMELEN
);
1028 (void) strlcpy(buf
, tofs
, strrchr(tofs
, '/') - tofs
+ 1);
1029 error
= dsl_dataset_hold(dp
, buf
, FTAG
, &ds
);
1034 * Check filesystem and snapshot limits before receiving. We'll
1035 * recheck snapshot limits again at the end (we create the
1036 * filesystems and increment those counts during begin_sync).
1038 error
= dsl_fs_ss_limit_check(ds
->ds_dir
, 1,
1039 ZFS_PROP_FILESYSTEM_LIMIT
, NULL
, drba
->drba_cred
);
1041 dsl_dataset_rele(ds
, FTAG
);
1045 error
= dsl_fs_ss_limit_check(ds
->ds_dir
, 1,
1046 ZFS_PROP_SNAPSHOT_LIMIT
, NULL
, drba
->drba_cred
);
1048 dsl_dataset_rele(ds
, FTAG
);
1052 if (drba
->drba_origin
!= NULL
) {
1053 dsl_dataset_t
*origin
;
1054 error
= dsl_dataset_hold(dp
, drba
->drba_origin
,
1057 dsl_dataset_rele(ds
, FTAG
);
1060 if (!origin
->ds_is_snapshot
) {
1061 dsl_dataset_rele(origin
, FTAG
);
1062 dsl_dataset_rele(ds
, FTAG
);
1063 return (SET_ERROR(EINVAL
));
1065 if (dsl_dataset_phys(origin
)->ds_guid
!= fromguid
) {
1066 dsl_dataset_rele(origin
, FTAG
);
1067 dsl_dataset_rele(ds
, FTAG
);
1068 return (SET_ERROR(ENODEV
));
1070 dsl_dataset_rele(origin
, FTAG
);
1072 dsl_dataset_rele(ds
, FTAG
);
1079 dmu_recv_begin_sync(void *arg
, dmu_tx_t
*tx
)
1081 dmu_recv_begin_arg_t
*drba
= arg
;
1082 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1083 struct drr_begin
*drrb
= drba
->drba_cookie
->drc_drrb
;
1084 const char *tofs
= drba
->drba_cookie
->drc_tofs
;
1085 dsl_dataset_t
*ds
, *newds
;
1090 crflags
= (drrb
->drr_flags
& DRR_FLAG_CI_DATA
) ?
1091 DS_FLAG_CI_DATASET
: 0;
1093 error
= dsl_dataset_hold(dp
, tofs
, FTAG
, &ds
);
1095 /* create temporary clone */
1096 dsl_dataset_t
*snap
= NULL
;
1097 if (drba
->drba_snapobj
!= 0) {
1098 VERIFY0(dsl_dataset_hold_obj(dp
,
1099 drba
->drba_snapobj
, FTAG
, &snap
));
1101 dsobj
= dsl_dataset_create_sync(ds
->ds_dir
, recv_clone_name
,
1102 snap
, crflags
, drba
->drba_cred
, tx
);
1103 dsl_dataset_rele(snap
, FTAG
);
1104 dsl_dataset_rele(ds
, FTAG
);
1108 dsl_dataset_t
*origin
= NULL
;
1110 VERIFY0(dsl_dir_hold(dp
, tofs
, FTAG
, &dd
, &tail
));
1112 if (drba
->drba_origin
!= NULL
) {
1113 VERIFY0(dsl_dataset_hold(dp
, drba
->drba_origin
,
1117 /* Create new dataset. */
1118 dsobj
= dsl_dataset_create_sync(dd
,
1119 strrchr(tofs
, '/') + 1,
1120 origin
, crflags
, drba
->drba_cred
, tx
);
1122 dsl_dataset_rele(origin
, FTAG
);
1123 dsl_dir_rele(dd
, FTAG
);
1124 drba
->drba_cookie
->drc_newfs
= B_TRUE
;
1126 VERIFY0(dsl_dataset_own_obj(dp
, dsobj
, dmu_recv_tag
, &newds
));
1128 dmu_buf_will_dirty(newds
->ds_dbuf
, tx
);
1129 dsl_dataset_phys(newds
)->ds_flags
|= DS_FLAG_INCONSISTENT
;
1132 * If we actually created a non-clone, we need to create the
1133 * objset in our new dataset.
1135 if (BP_IS_HOLE(dsl_dataset_get_blkptr(newds
))) {
1136 (void) dmu_objset_create_impl(dp
->dp_spa
,
1137 newds
, dsl_dataset_get_blkptr(newds
), drrb
->drr_type
, tx
);
1140 drba
->drba_cookie
->drc_ds
= newds
;
1142 spa_history_log_internal_ds(newds
, "receive", tx
, "");
1146 * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
1147 * succeeds; otherwise we will leak the holds on the datasets.
1150 dmu_recv_begin(char *tofs
, char *tosnap
, struct drr_begin
*drrb
,
1151 boolean_t force
, char *origin
, dmu_recv_cookie_t
*drc
)
1153 dmu_recv_begin_arg_t drba
= { 0 };
1154 dmu_replay_record_t
*drr
;
1156 bzero(drc
, sizeof (dmu_recv_cookie_t
));
1157 drc
->drc_drrb
= drrb
;
1158 drc
->drc_tosnap
= tosnap
;
1159 drc
->drc_tofs
= tofs
;
1160 drc
->drc_force
= force
;
1161 drc
->drc_cred
= CRED();
1163 if (drrb
->drr_magic
== BSWAP_64(DMU_BACKUP_MAGIC
))
1164 drc
->drc_byteswap
= B_TRUE
;
1165 else if (drrb
->drr_magic
!= DMU_BACKUP_MAGIC
)
1166 return (SET_ERROR(EINVAL
));
1168 drr
= kmem_zalloc(sizeof (dmu_replay_record_t
), KM_SLEEP
);
1169 drr
->drr_type
= DRR_BEGIN
;
1170 drr
->drr_u
.drr_begin
= *drc
->drc_drrb
;
1171 if (drc
->drc_byteswap
) {
1172 fletcher_4_incremental_byteswap(drr
,
1173 sizeof (dmu_replay_record_t
), &drc
->drc_cksum
);
1175 fletcher_4_incremental_native(drr
,
1176 sizeof (dmu_replay_record_t
), &drc
->drc_cksum
);
1178 kmem_free(drr
, sizeof (dmu_replay_record_t
));
1180 if (drc
->drc_byteswap
) {
1181 drrb
->drr_magic
= BSWAP_64(drrb
->drr_magic
);
1182 drrb
->drr_versioninfo
= BSWAP_64(drrb
->drr_versioninfo
);
1183 drrb
->drr_creation_time
= BSWAP_64(drrb
->drr_creation_time
);
1184 drrb
->drr_type
= BSWAP_32(drrb
->drr_type
);
1185 drrb
->drr_toguid
= BSWAP_64(drrb
->drr_toguid
);
1186 drrb
->drr_fromguid
= BSWAP_64(drrb
->drr_fromguid
);
1189 drba
.drba_origin
= origin
;
1190 drba
.drba_cookie
= drc
;
1191 drba
.drba_cred
= CRED();
1193 return (dsl_sync_task(tofs
, dmu_recv_begin_check
, dmu_recv_begin_sync
,
1194 &drba
, 5, ZFS_SPACE_CHECK_NORMAL
));
1203 int bufsize
; /* amount of memory allocated for buf */
1205 avl_tree_t
*guid_to_ds_map
;
1208 typedef struct guid_map_entry
{
1210 dsl_dataset_t
*gme_ds
;
1215 guid_compare(const void *arg1
, const void *arg2
)
1217 const guid_map_entry_t
*gmep1
= arg1
;
1218 const guid_map_entry_t
*gmep2
= arg2
;
1220 if (gmep1
->guid
< gmep2
->guid
)
1222 else if (gmep1
->guid
> gmep2
->guid
)
1228 free_guid_map_onexit(void *arg
)
1230 avl_tree_t
*ca
= arg
;
1231 void *cookie
= NULL
;
1232 guid_map_entry_t
*gmep
;
1234 while ((gmep
= avl_destroy_nodes(ca
, &cookie
)) != NULL
) {
1235 dsl_dataset_long_rele(gmep
->gme_ds
, gmep
);
1236 dsl_dataset_rele(gmep
->gme_ds
, gmep
);
1237 kmem_free(gmep
, sizeof (guid_map_entry_t
));
1240 kmem_free(ca
, sizeof (avl_tree_t
));
1244 restore_read(struct restorearg
*ra
, int len
, char *buf
)
1251 /* some things will require 8-byte alignment, so everything must */
1254 while (done
< len
) {
1257 ra
->err
= vn_rdwr(UIO_READ
, ra
->vp
,
1258 buf
+ done
, len
- done
,
1259 ra
->voff
, UIO_SYSSPACE
, FAPPEND
,
1260 RLIM64_INFINITY
, CRED(), &resid
);
1262 if (resid
== len
- done
)
1263 ra
->err
= SET_ERROR(EINVAL
);
1264 ra
->voff
+= len
- done
- resid
;
1270 ASSERT3U(done
, ==, len
);
1272 fletcher_4_incremental_byteswap(buf
, len
, &ra
->cksum
);
1274 fletcher_4_incremental_native(buf
, len
, &ra
->cksum
);
1278 noinline
static void
1279 backup_byteswap(dmu_replay_record_t
*drr
)
1281 #define DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
1282 #define DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
1283 drr
->drr_type
= BSWAP_32(drr
->drr_type
);
1284 drr
->drr_payloadlen
= BSWAP_32(drr
->drr_payloadlen
);
1285 switch (drr
->drr_type
) {
1287 DO64(drr_begin
.drr_magic
);
1288 DO64(drr_begin
.drr_versioninfo
);
1289 DO64(drr_begin
.drr_creation_time
);
1290 DO32(drr_begin
.drr_type
);
1291 DO32(drr_begin
.drr_flags
);
1292 DO64(drr_begin
.drr_toguid
);
1293 DO64(drr_begin
.drr_fromguid
);
1296 DO64(drr_object
.drr_object
);
1297 DO32(drr_object
.drr_type
);
1298 DO32(drr_object
.drr_bonustype
);
1299 DO32(drr_object
.drr_blksz
);
1300 DO32(drr_object
.drr_bonuslen
);
1301 DO64(drr_object
.drr_toguid
);
1303 case DRR_FREEOBJECTS
:
1304 DO64(drr_freeobjects
.drr_firstobj
);
1305 DO64(drr_freeobjects
.drr_numobjs
);
1306 DO64(drr_freeobjects
.drr_toguid
);
1309 DO64(drr_write
.drr_object
);
1310 DO32(drr_write
.drr_type
);
1311 DO64(drr_write
.drr_offset
);
1312 DO64(drr_write
.drr_length
);
1313 DO64(drr_write
.drr_toguid
);
1314 DO64(drr_write
.drr_key
.ddk_cksum
.zc_word
[0]);
1315 DO64(drr_write
.drr_key
.ddk_cksum
.zc_word
[1]);
1316 DO64(drr_write
.drr_key
.ddk_cksum
.zc_word
[2]);
1317 DO64(drr_write
.drr_key
.ddk_cksum
.zc_word
[3]);
1318 DO64(drr_write
.drr_key
.ddk_prop
);
1320 case DRR_WRITE_BYREF
:
1321 DO64(drr_write_byref
.drr_object
);
1322 DO64(drr_write_byref
.drr_offset
);
1323 DO64(drr_write_byref
.drr_length
);
1324 DO64(drr_write_byref
.drr_toguid
);
1325 DO64(drr_write_byref
.drr_refguid
);
1326 DO64(drr_write_byref
.drr_refobject
);
1327 DO64(drr_write_byref
.drr_refoffset
);
1328 DO64(drr_write_byref
.drr_key
.ddk_cksum
.zc_word
[0]);
1329 DO64(drr_write_byref
.drr_key
.ddk_cksum
.zc_word
[1]);
1330 DO64(drr_write_byref
.drr_key
.ddk_cksum
.zc_word
[2]);
1331 DO64(drr_write_byref
.drr_key
.ddk_cksum
.zc_word
[3]);
1332 DO64(drr_write_byref
.drr_key
.ddk_prop
);
1334 case DRR_WRITE_EMBEDDED
:
1335 DO64(drr_write_embedded
.drr_object
);
1336 DO64(drr_write_embedded
.drr_offset
);
1337 DO64(drr_write_embedded
.drr_length
);
1338 DO64(drr_write_embedded
.drr_toguid
);
1339 DO32(drr_write_embedded
.drr_lsize
);
1340 DO32(drr_write_embedded
.drr_psize
);
1343 DO64(drr_free
.drr_object
);
1344 DO64(drr_free
.drr_offset
);
1345 DO64(drr_free
.drr_length
);
1346 DO64(drr_free
.drr_toguid
);
1349 DO64(drr_spill
.drr_object
);
1350 DO64(drr_spill
.drr_length
);
1351 DO64(drr_spill
.drr_toguid
);
1354 DO64(drr_end
.drr_checksum
.zc_word
[0]);
1355 DO64(drr_end
.drr_checksum
.zc_word
[1]);
1356 DO64(drr_end
.drr_checksum
.zc_word
[2]);
1357 DO64(drr_end
.drr_checksum
.zc_word
[3]);
1358 DO64(drr_end
.drr_toguid
);
1367 static inline uint8_t
1368 deduce_nblkptr(dmu_object_type_t bonus_type
, uint64_t bonus_size
)
1370 if (bonus_type
== DMU_OT_SA
) {
1374 ((DN_MAX_BONUSLEN
- bonus_size
) >> SPA_BLKPTRSHIFT
));
1379 restore_object(struct restorearg
*ra
, objset_t
*os
, struct drr_object
*drro
)
1381 dmu_object_info_t doi
;
1387 if (drro
->drr_type
== DMU_OT_NONE
||
1388 !DMU_OT_IS_VALID(drro
->drr_type
) ||
1389 !DMU_OT_IS_VALID(drro
->drr_bonustype
) ||
1390 drro
->drr_checksumtype
>= ZIO_CHECKSUM_FUNCTIONS
||
1391 drro
->drr_compress
>= ZIO_COMPRESS_FUNCTIONS
||
1392 P2PHASE(drro
->drr_blksz
, SPA_MINBLOCKSIZE
) ||
1393 drro
->drr_blksz
< SPA_MINBLOCKSIZE
||
1394 drro
->drr_blksz
> SPA_MAXBLOCKSIZE
||
1395 drro
->drr_bonuslen
> DN_MAX_BONUSLEN
) {
1396 return (SET_ERROR(EINVAL
));
1399 err
= dmu_object_info(os
, drro
->drr_object
, &doi
);
1401 if (err
!= 0 && err
!= ENOENT
)
1402 return (SET_ERROR(EINVAL
));
1403 object
= err
== 0 ? drro
->drr_object
: DMU_NEW_OBJECT
;
1405 if (drro
->drr_bonuslen
) {
1406 data
= restore_read(ra
, P2ROUNDUP(drro
->drr_bonuslen
, 8), NULL
);
1412 * If we are losing blkptrs or changing the block size this must
1413 * be a new file instance. We must clear out the previous file
1414 * contents before we can change this type of metadata in the dnode.
1419 nblkptr
= deduce_nblkptr(drro
->drr_bonustype
,
1420 drro
->drr_bonuslen
);
1422 if (drro
->drr_blksz
!= doi
.doi_data_block_size
||
1423 nblkptr
< doi
.doi_nblkptr
) {
1424 err
= dmu_free_long_range(os
, drro
->drr_object
,
1427 return (SET_ERROR(EINVAL
));
1431 tx
= dmu_tx_create(os
);
1432 dmu_tx_hold_bonus(tx
, object
);
1433 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1439 if (object
== DMU_NEW_OBJECT
) {
1440 /* currently free, want to be allocated */
1441 err
= dmu_object_claim(os
, drro
->drr_object
,
1442 drro
->drr_type
, drro
->drr_blksz
,
1443 drro
->drr_bonustype
, drro
->drr_bonuslen
, tx
);
1444 } else if (drro
->drr_type
!= doi
.doi_type
||
1445 drro
->drr_blksz
!= doi
.doi_data_block_size
||
1446 drro
->drr_bonustype
!= doi
.doi_bonus_type
||
1447 drro
->drr_bonuslen
!= doi
.doi_bonus_size
) {
1448 /* currently allocated, but with different properties */
1449 err
= dmu_object_reclaim(os
, drro
->drr_object
,
1450 drro
->drr_type
, drro
->drr_blksz
,
1451 drro
->drr_bonustype
, drro
->drr_bonuslen
, tx
);
1455 return (SET_ERROR(EINVAL
));
1458 dmu_object_set_checksum(os
, drro
->drr_object
, drro
->drr_checksumtype
,
1460 dmu_object_set_compress(os
, drro
->drr_object
, drro
->drr_compress
, tx
);
1465 VERIFY(0 == dmu_bonus_hold(os
, drro
->drr_object
, FTAG
, &db
));
1466 dmu_buf_will_dirty(db
, tx
);
1468 ASSERT3U(db
->db_size
, >=, drro
->drr_bonuslen
);
1469 bcopy(data
, db
->db_data
, drro
->drr_bonuslen
);
1471 dmu_object_byteswap_t byteswap
=
1472 DMU_OT_BYTESWAP(drro
->drr_bonustype
);
1473 dmu_ot_byteswap
[byteswap
].ob_func(db
->db_data
,
1474 drro
->drr_bonuslen
);
1476 dmu_buf_rele(db
, FTAG
);
1484 restore_freeobjects(struct restorearg
*ra
, objset_t
*os
,
1485 struct drr_freeobjects
*drrfo
)
1489 if (drrfo
->drr_firstobj
+ drrfo
->drr_numobjs
< drrfo
->drr_firstobj
)
1490 return (SET_ERROR(EINVAL
));
1492 for (obj
= drrfo
->drr_firstobj
;
1493 obj
< drrfo
->drr_firstobj
+ drrfo
->drr_numobjs
;
1494 (void) dmu_object_next(os
, &obj
, FALSE
, 0)) {
1497 if (dmu_object_info(os
, obj
, NULL
) != 0)
1500 err
= dmu_free_long_object(os
, obj
);
1508 restore_write(struct restorearg
*ra
, objset_t
*os
,
1509 struct drr_write
*drrw
)
1517 if (drrw
->drr_offset
+ drrw
->drr_length
< drrw
->drr_offset
||
1518 !DMU_OT_IS_VALID(drrw
->drr_type
))
1519 return (SET_ERROR(EINVAL
));
1521 if (dmu_object_info(os
, drrw
->drr_object
, NULL
) != 0)
1522 return (SET_ERROR(EINVAL
));
1524 if (dmu_bonus_hold(os
, drrw
->drr_object
, FTAG
, &bonus
) != 0)
1525 return (SET_ERROR(EINVAL
));
1527 abuf
= dmu_request_arcbuf(bonus
, drrw
->drr_length
);
1529 data
= restore_read(ra
, drrw
->drr_length
, abuf
->b_data
);
1531 dmu_return_arcbuf(abuf
);
1532 dmu_buf_rele(bonus
, FTAG
);
1536 tx
= dmu_tx_create(os
);
1538 dmu_tx_hold_write(tx
, drrw
->drr_object
,
1539 drrw
->drr_offset
, drrw
->drr_length
);
1540 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1542 dmu_return_arcbuf(abuf
);
1543 dmu_buf_rele(bonus
, FTAG
);
1548 dmu_object_byteswap_t byteswap
=
1549 DMU_OT_BYTESWAP(drrw
->drr_type
);
1550 dmu_ot_byteswap
[byteswap
].ob_func(data
, drrw
->drr_length
);
1552 dmu_assign_arcbuf(bonus
, drrw
->drr_offset
, abuf
, tx
);
1554 dmu_buf_rele(bonus
, FTAG
);
1559 * Handle a DRR_WRITE_BYREF record. This record is used in dedup'ed
1560 * streams to refer to a copy of the data that is already on the
1561 * system because it came in earlier in the stream. This function
1562 * finds the earlier copy of the data, and uses that copy instead of
1563 * data from the stream to fulfill this write.
1566 restore_write_byref(struct restorearg
*ra
, objset_t
*os
,
1567 struct drr_write_byref
*drrwbr
)
1571 guid_map_entry_t gmesrch
;
1572 guid_map_entry_t
*gmep
;
1574 objset_t
*ref_os
= NULL
;
1577 if (drrwbr
->drr_offset
+ drrwbr
->drr_length
< drrwbr
->drr_offset
)
1578 return (SET_ERROR(EINVAL
));
1581 * If the GUID of the referenced dataset is different from the
1582 * GUID of the target dataset, find the referenced dataset.
1584 if (drrwbr
->drr_toguid
!= drrwbr
->drr_refguid
) {
1585 gmesrch
.guid
= drrwbr
->drr_refguid
;
1586 if ((gmep
= avl_find(ra
->guid_to_ds_map
, &gmesrch
,
1588 return (SET_ERROR(EINVAL
));
1590 if (dmu_objset_from_ds(gmep
->gme_ds
, &ref_os
))
1591 return (SET_ERROR(EINVAL
));
1596 err
= dmu_buf_hold(ref_os
, drrwbr
->drr_refobject
,
1597 drrwbr
->drr_refoffset
, FTAG
, &dbp
, DMU_READ_PREFETCH
);
1601 tx
= dmu_tx_create(os
);
1603 dmu_tx_hold_write(tx
, drrwbr
->drr_object
,
1604 drrwbr
->drr_offset
, drrwbr
->drr_length
);
1605 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1610 dmu_write(os
, drrwbr
->drr_object
,
1611 drrwbr
->drr_offset
, drrwbr
->drr_length
, dbp
->db_data
, tx
);
1612 dmu_buf_rele(dbp
, FTAG
);
1618 restore_write_embedded(struct restorearg
*ra
, objset_t
*os
,
1619 struct drr_write_embedded
*drrwnp
)
1625 if (drrwnp
->drr_offset
+ drrwnp
->drr_length
< drrwnp
->drr_offset
)
1628 if (drrwnp
->drr_psize
> BPE_PAYLOAD_SIZE
)
1631 if (drrwnp
->drr_etype
>= NUM_BP_EMBEDDED_TYPES
)
1633 if (drrwnp
->drr_compression
>= ZIO_COMPRESS_FUNCTIONS
)
1636 data
= restore_read(ra
, P2ROUNDUP(drrwnp
->drr_psize
, 8), NULL
);
1640 tx
= dmu_tx_create(os
);
1642 dmu_tx_hold_write(tx
, drrwnp
->drr_object
,
1643 drrwnp
->drr_offset
, drrwnp
->drr_length
);
1644 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1650 dmu_write_embedded(os
, drrwnp
->drr_object
,
1651 drrwnp
->drr_offset
, data
, drrwnp
->drr_etype
,
1652 drrwnp
->drr_compression
, drrwnp
->drr_lsize
, drrwnp
->drr_psize
,
1653 ra
->byteswap
^ ZFS_HOST_BYTEORDER
, tx
);
1660 restore_spill(struct restorearg
*ra
, objset_t
*os
, struct drr_spill
*drrs
)
1664 dmu_buf_t
*db
, *db_spill
;
1667 if (drrs
->drr_length
< SPA_MINBLOCKSIZE
||
1668 drrs
->drr_length
> SPA_MAXBLOCKSIZE
)
1669 return (SET_ERROR(EINVAL
));
1671 data
= restore_read(ra
, drrs
->drr_length
, NULL
);
1675 if (dmu_object_info(os
, drrs
->drr_object
, NULL
) != 0)
1676 return (SET_ERROR(EINVAL
));
1678 VERIFY(0 == dmu_bonus_hold(os
, drrs
->drr_object
, FTAG
, &db
));
1679 if ((err
= dmu_spill_hold_by_bonus(db
, FTAG
, &db_spill
)) != 0) {
1680 dmu_buf_rele(db
, FTAG
);
1684 tx
= dmu_tx_create(os
);
1686 dmu_tx_hold_spill(tx
, db
->db_object
);
1688 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1690 dmu_buf_rele(db
, FTAG
);
1691 dmu_buf_rele(db_spill
, FTAG
);
1695 dmu_buf_will_dirty(db_spill
, tx
);
1697 if (db_spill
->db_size
< drrs
->drr_length
)
1698 VERIFY(0 == dbuf_spill_set_blksz(db_spill
,
1699 drrs
->drr_length
, tx
));
1700 bcopy(data
, db_spill
->db_data
, drrs
->drr_length
);
1702 dmu_buf_rele(db
, FTAG
);
1703 dmu_buf_rele(db_spill
, FTAG
);
1711 restore_free(struct restorearg
*ra
, objset_t
*os
,
1712 struct drr_free
*drrf
)
1716 if (drrf
->drr_length
!= -1ULL &&
1717 drrf
->drr_offset
+ drrf
->drr_length
< drrf
->drr_offset
)
1718 return (SET_ERROR(EINVAL
));
1720 if (dmu_object_info(os
, drrf
->drr_object
, NULL
) != 0)
1721 return (SET_ERROR(EINVAL
));
1723 err
= dmu_free_long_range(os
, drrf
->drr_object
,
1724 drrf
->drr_offset
, drrf
->drr_length
);
1728 /* used to destroy the drc_ds on error */
1730 dmu_recv_cleanup_ds(dmu_recv_cookie_t
*drc
)
1732 char name
[MAXNAMELEN
];
1733 dsl_dataset_name(drc
->drc_ds
, name
);
1734 dsl_dataset_disown(drc
->drc_ds
, dmu_recv_tag
);
1735 (void) dsl_destroy_head(name
);
1739 * NB: callers *must* call dmu_recv_end() if this succeeds.
1742 dmu_recv_stream(dmu_recv_cookie_t
*drc
, vnode_t
*vp
, offset_t
*voffp
,
1743 int cleanup_fd
, uint64_t *action_handlep
)
1745 struct restorearg ra
= { 0 };
1746 dmu_replay_record_t
*drr
;
1751 ra
.byteswap
= drc
->drc_byteswap
;
1752 ra
.cksum
= drc
->drc_cksum
;
1756 ra
.buf
= vmem_alloc(ra
.bufsize
, KM_SLEEP
);
1758 /* these were verified in dmu_recv_begin */
1759 ASSERT3U(DMU_GET_STREAM_HDRTYPE(drc
->drc_drrb
->drr_versioninfo
), ==,
1761 ASSERT3U(drc
->drc_drrb
->drr_type
, <, DMU_OST_NUMTYPES
);
1764 * Open the objset we are modifying.
1766 VERIFY0(dmu_objset_from_ds(drc
->drc_ds
, &os
));
1768 ASSERT(dsl_dataset_phys(drc
->drc_ds
)->ds_flags
& DS_FLAG_INCONSISTENT
);
1770 featureflags
= DMU_GET_FEATUREFLAGS(drc
->drc_drrb
->drr_versioninfo
);
1772 /* if this stream is dedup'ed, set up the avl tree for guid mapping */
1773 if (featureflags
& DMU_BACKUP_FEATURE_DEDUP
) {
1776 if (cleanup_fd
== -1) {
1777 ra
.err
= SET_ERROR(EBADF
);
1780 ra
.err
= zfs_onexit_fd_hold(cleanup_fd
, &minor
);
1786 if (*action_handlep
== 0) {
1788 kmem_alloc(sizeof (avl_tree_t
), KM_SLEEP
);
1789 avl_create(ra
.guid_to_ds_map
, guid_compare
,
1790 sizeof (guid_map_entry_t
),
1791 offsetof(guid_map_entry_t
, avlnode
));
1792 ra
.err
= zfs_onexit_add_cb(minor
,
1793 free_guid_map_onexit
, ra
.guid_to_ds_map
,
1798 ra
.err
= zfs_onexit_cb_data(minor
, *action_handlep
,
1799 (void **)&ra
.guid_to_ds_map
);
1804 drc
->drc_guid_to_ds_map
= ra
.guid_to_ds_map
;
1808 * Read records and process them.
1811 while (ra
.err
== 0 &&
1812 NULL
!= (drr
= restore_read(&ra
, sizeof (*drr
), NULL
))) {
1813 if (issig(JUSTLOOKING
) && issig(FORREAL
)) {
1814 ra
.err
= SET_ERROR(EINTR
);
1819 backup_byteswap(drr
);
1821 switch (drr
->drr_type
) {
1825 * We need to make a copy of the record header,
1826 * because restore_{object,write} may need to
1827 * restore_read(), which will invalidate drr.
1829 struct drr_object drro
= drr
->drr_u
.drr_object
;
1830 ra
.err
= restore_object(&ra
, os
, &drro
);
1833 case DRR_FREEOBJECTS
:
1835 struct drr_freeobjects drrfo
=
1836 drr
->drr_u
.drr_freeobjects
;
1837 ra
.err
= restore_freeobjects(&ra
, os
, &drrfo
);
1842 struct drr_write drrw
= drr
->drr_u
.drr_write
;
1843 ra
.err
= restore_write(&ra
, os
, &drrw
);
1846 case DRR_WRITE_BYREF
:
1848 struct drr_write_byref drrwbr
=
1849 drr
->drr_u
.drr_write_byref
;
1850 ra
.err
= restore_write_byref(&ra
, os
, &drrwbr
);
1853 case DRR_WRITE_EMBEDDED
:
1855 struct drr_write_embedded drrwe
=
1856 drr
->drr_u
.drr_write_embedded
;
1857 ra
.err
= restore_write_embedded(&ra
, os
, &drrwe
);
1862 struct drr_free drrf
= drr
->drr_u
.drr_free
;
1863 ra
.err
= restore_free(&ra
, os
, &drrf
);
1868 struct drr_end drre
= drr
->drr_u
.drr_end
;
1870 * We compare against the *previous* checksum
1871 * value, because the stored checksum is of
1872 * everything before the DRR_END record.
1874 if (!ZIO_CHECKSUM_EQUAL(drre
.drr_checksum
, pcksum
))
1875 ra
.err
= SET_ERROR(ECKSUM
);
1880 struct drr_spill drrs
= drr
->drr_u
.drr_spill
;
1881 ra
.err
= restore_spill(&ra
, os
, &drrs
);
1885 ra
.err
= SET_ERROR(EINVAL
);
1890 ASSERT(ra
.err
!= 0);
1893 if ((featureflags
& DMU_BACKUP_FEATURE_DEDUP
) && (cleanup_fd
!= -1))
1894 zfs_onexit_fd_rele(cleanup_fd
);
1898 * destroy what we created, so we don't leave it in the
1899 * inconsistent restoring state.
1901 dmu_recv_cleanup_ds(drc
);
1904 vmem_free(ra
.buf
, ra
.bufsize
);
1910 dmu_recv_end_check(void *arg
, dmu_tx_t
*tx
)
1912 dmu_recv_cookie_t
*drc
= arg
;
1913 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1916 ASSERT3P(drc
->drc_ds
->ds_owner
, ==, dmu_recv_tag
);
1918 if (!drc
->drc_newfs
) {
1919 dsl_dataset_t
*origin_head
;
1921 error
= dsl_dataset_hold(dp
, drc
->drc_tofs
, FTAG
, &origin_head
);
1924 if (drc
->drc_force
) {
1926 * We will destroy any snapshots in tofs (i.e. before
1927 * origin_head) that are after the origin (which is
1928 * the snap before drc_ds, because drc_ds can not
1929 * have any snaps of its own).
1933 obj
= dsl_dataset_phys(origin_head
)->ds_prev_snap_obj
;
1935 dsl_dataset_phys(drc
->drc_ds
)->ds_prev_snap_obj
) {
1936 dsl_dataset_t
*snap
;
1937 error
= dsl_dataset_hold_obj(dp
, obj
, FTAG
,
1941 if (snap
->ds_dir
!= origin_head
->ds_dir
)
1942 error
= SET_ERROR(EINVAL
);
1944 error
= dsl_destroy_snapshot_check_impl(
1947 obj
= dsl_dataset_phys(snap
)->ds_prev_snap_obj
;
1948 dsl_dataset_rele(snap
, FTAG
);
1953 error
= dsl_dataset_clone_swap_check_impl(drc
->drc_ds
,
1954 origin_head
, drc
->drc_force
, drc
->drc_owner
, tx
);
1956 dsl_dataset_rele(origin_head
, FTAG
);
1959 error
= dsl_dataset_snapshot_check_impl(origin_head
,
1960 drc
->drc_tosnap
, tx
, B_TRUE
, 1, drc
->drc_cred
);
1961 dsl_dataset_rele(origin_head
, FTAG
);
1965 error
= dsl_destroy_head_check_impl(drc
->drc_ds
, 1);
1967 error
= dsl_dataset_snapshot_check_impl(drc
->drc_ds
,
1968 drc
->drc_tosnap
, tx
, B_TRUE
, 1, drc
->drc_cred
);
1974 dmu_recv_end_sync(void *arg
, dmu_tx_t
*tx
)
1976 dmu_recv_cookie_t
*drc
= arg
;
1977 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1979 spa_history_log_internal_ds(drc
->drc_ds
, "finish receiving",
1980 tx
, "snap=%s", drc
->drc_tosnap
);
1982 if (!drc
->drc_newfs
) {
1983 dsl_dataset_t
*origin_head
;
1985 VERIFY0(dsl_dataset_hold(dp
, drc
->drc_tofs
, FTAG
,
1988 if (drc
->drc_force
) {
1990 * Destroy any snapshots of drc_tofs (origin_head)
1991 * after the origin (the snap before drc_ds).
1995 obj
= dsl_dataset_phys(origin_head
)->ds_prev_snap_obj
;
1997 dsl_dataset_phys(drc
->drc_ds
)->ds_prev_snap_obj
) {
1998 dsl_dataset_t
*snap
;
1999 VERIFY0(dsl_dataset_hold_obj(dp
, obj
, FTAG
,
2001 ASSERT3P(snap
->ds_dir
, ==, origin_head
->ds_dir
);
2002 obj
= dsl_dataset_phys(snap
)->ds_prev_snap_obj
;
2003 dsl_destroy_snapshot_sync_impl(snap
,
2005 dsl_dataset_rele(snap
, FTAG
);
2008 VERIFY3P(drc
->drc_ds
->ds_prev
, ==,
2009 origin_head
->ds_prev
);
2011 dsl_dataset_clone_swap_sync_impl(drc
->drc_ds
,
2013 dsl_dataset_snapshot_sync_impl(origin_head
,
2014 drc
->drc_tosnap
, tx
);
2016 /* set snapshot's creation time and guid */
2017 dmu_buf_will_dirty(origin_head
->ds_prev
->ds_dbuf
, tx
);
2018 dsl_dataset_phys(origin_head
->ds_prev
)->ds_creation_time
=
2019 drc
->drc_drrb
->drr_creation_time
;
2020 dsl_dataset_phys(origin_head
->ds_prev
)->ds_guid
=
2021 drc
->drc_drrb
->drr_toguid
;
2022 dsl_dataset_phys(origin_head
->ds_prev
)->ds_flags
&=
2023 ~DS_FLAG_INCONSISTENT
;
2025 dmu_buf_will_dirty(origin_head
->ds_dbuf
, tx
);
2026 dsl_dataset_phys(origin_head
)->ds_flags
&=
2027 ~DS_FLAG_INCONSISTENT
;
2029 dsl_dataset_rele(origin_head
, FTAG
);
2030 dsl_destroy_head_sync_impl(drc
->drc_ds
, tx
);
2032 if (drc
->drc_owner
!= NULL
)
2033 VERIFY3P(origin_head
->ds_owner
, ==, drc
->drc_owner
);
2035 dsl_dataset_t
*ds
= drc
->drc_ds
;
2037 dsl_dataset_snapshot_sync_impl(ds
, drc
->drc_tosnap
, tx
);
2039 /* set snapshot's creation time and guid */
2040 dmu_buf_will_dirty(ds
->ds_prev
->ds_dbuf
, tx
);
2041 dsl_dataset_phys(ds
->ds_prev
)->ds_creation_time
=
2042 drc
->drc_drrb
->drr_creation_time
;
2043 dsl_dataset_phys(ds
->ds_prev
)->ds_guid
=
2044 drc
->drc_drrb
->drr_toguid
;
2045 dsl_dataset_phys(ds
->ds_prev
)->ds_flags
&=
2046 ~DS_FLAG_INCONSISTENT
;
2048 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
2049 dsl_dataset_phys(ds
)->ds_flags
&= ~DS_FLAG_INCONSISTENT
;
2051 drc
->drc_newsnapobj
= dsl_dataset_phys(drc
->drc_ds
)->ds_prev_snap_obj
;
2053 * Release the hold from dmu_recv_begin. This must be done before
2054 * we return to open context, so that when we free the dataset's dnode,
2055 * we can evict its bonus buffer.
2057 dsl_dataset_disown(drc
->drc_ds
, dmu_recv_tag
);
2062 add_ds_to_guidmap(const char *name
, avl_tree_t
*guid_map
, uint64_t snapobj
)
2065 dsl_dataset_t
*snapds
;
2066 guid_map_entry_t
*gmep
;
2069 ASSERT(guid_map
!= NULL
);
2071 err
= dsl_pool_hold(name
, FTAG
, &dp
);
2074 gmep
= kmem_alloc(sizeof (*gmep
), KM_SLEEP
);
2075 err
= dsl_dataset_hold_obj(dp
, snapobj
, gmep
, &snapds
);
2077 gmep
->guid
= dsl_dataset_phys(snapds
)->ds_guid
;
2078 gmep
->gme_ds
= snapds
;
2079 avl_add(guid_map
, gmep
);
2080 dsl_dataset_long_hold(snapds
, gmep
);
2082 kmem_free(gmep
, sizeof (*gmep
));
2085 dsl_pool_rele(dp
, FTAG
);
2089 static int dmu_recv_end_modified_blocks
= 3;
2092 dmu_recv_existing_end(dmu_recv_cookie_t
*drc
)
2100 * We will be destroying the ds; make sure its origin is unmounted if
2103 name
= kmem_alloc(MAXNAMELEN
, KM_SLEEP
);
2104 dsl_dataset_name(drc
->drc_ds
, name
);
2105 zfs_destroy_unmount_origin(name
);
2106 kmem_free(name
, MAXNAMELEN
);
2109 error
= dsl_sync_task(drc
->drc_tofs
,
2110 dmu_recv_end_check
, dmu_recv_end_sync
, drc
,
2111 dmu_recv_end_modified_blocks
, ZFS_SPACE_CHECK_NORMAL
);
2114 dmu_recv_cleanup_ds(drc
);
2119 dmu_recv_new_end(dmu_recv_cookie_t
*drc
)
2123 error
= dsl_sync_task(drc
->drc_tofs
,
2124 dmu_recv_end_check
, dmu_recv_end_sync
, drc
,
2125 dmu_recv_end_modified_blocks
, ZFS_SPACE_CHECK_NORMAL
);
2128 dmu_recv_cleanup_ds(drc
);
2129 } else if (drc
->drc_guid_to_ds_map
!= NULL
) {
2130 (void) add_ds_to_guidmap(drc
->drc_tofs
,
2131 drc
->drc_guid_to_ds_map
,
2132 drc
->drc_newsnapobj
);
2138 dmu_recv_end(dmu_recv_cookie_t
*drc
, void *owner
)
2140 drc
->drc_owner
= owner
;
2143 return (dmu_recv_new_end(drc
));
2145 return (dmu_recv_existing_end(drc
));
2149 * Return TRUE if this objset is currently being received into.
2152 dmu_objset_is_receiving(objset_t
*os
)
2154 return (os
->os_dsl_dataset
!= NULL
&&
2155 os
->os_dsl_dataset
->ds_owner
== dmu_recv_tag
);
2158 #if defined(_KERNEL)
2159 module_param(zfs_send_corrupt_data
, int, 0644);
2160 MODULE_PARM_DESC(zfs_send_corrupt_data
, "Allow sending corrupt data");