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) 2012, Joyent, Inc. All rights reserved.
26 * Copyright (c) 2013 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/dsl_bookmark.h>
55 /* Set this tunable to TRUE to replace corrupt data with 0x2f5baddb10c */
56 int zfs_send_corrupt_data
= B_FALSE
;
58 static char *dmu_recv_tag
= "dmu_recv_tag";
59 static const char *recv_clone_name
= "%recv";
61 typedef struct dump_bytes_io
{
62 dmu_sendarg_t
*dbi_dsp
;
68 dump_bytes_strategy(void *arg
)
70 dump_bytes_io_t
*dbi
= (dump_bytes_io_t
*)arg
;
71 dmu_sendarg_t
*dsp
= dbi
->dbi_dsp
;
72 dsl_dataset_t
*ds
= dsp
->dsa_os
->os_dsl_dataset
;
73 ssize_t resid
; /* have to get resid to get detailed errno */
74 ASSERT0(dbi
->dbi_len
% 8);
76 fletcher_4_incremental_native(dbi
->dbi_buf
, dbi
->dbi_len
, &dsp
->dsa_zc
);
77 dsp
->dsa_err
= vn_rdwr(UIO_WRITE
, dsp
->dsa_vp
,
78 (caddr_t
)dbi
->dbi_buf
, dbi
->dbi_len
,
79 0, UIO_SYSSPACE
, FAPPEND
, RLIM64_INFINITY
, CRED(), &resid
);
81 mutex_enter(&ds
->ds_sendstream_lock
);
82 *dsp
->dsa_off
+= dbi
->dbi_len
;
83 mutex_exit(&ds
->ds_sendstream_lock
);
87 dump_bytes(dmu_sendarg_t
*dsp
, void *buf
, int len
)
96 * The vn_rdwr() call is performed in a taskq to ensure that there is
97 * always enough stack space to write safely to the target filesystem.
98 * The ZIO_TYPE_FREE threads are used because there can be a lot of
99 * them and they are used in vdev_file.c for a similar purpose.
101 spa_taskq_dispatch_sync(dmu_objset_spa(dsp
->dsa_os
), ZIO_TYPE_FREE
,
102 ZIO_TASKQ_ISSUE
, dump_bytes_strategy
, &dbi
, TQ_SLEEP
);
104 return (dsp
->dsa_err
);
108 dump_free(dmu_sendarg_t
*dsp
, uint64_t object
, uint64_t offset
,
111 struct drr_free
*drrf
= &(dsp
->dsa_drr
->drr_u
.drr_free
);
114 * When we receive a free record, dbuf_free_range() assumes
115 * that the receiving system doesn't have any dbufs in the range
116 * being freed. This is always true because there is a one-record
117 * constraint: we only send one WRITE record for any given
118 * object+offset. We know that the one-record constraint is
119 * true because we always send data in increasing order by
122 * If the increasing-order constraint ever changes, we should find
123 * another way to assert that the one-record constraint is still
126 ASSERT(object
> dsp
->dsa_last_data_object
||
127 (object
== dsp
->dsa_last_data_object
&&
128 offset
> dsp
->dsa_last_data_offset
));
131 * If we are doing a non-incremental send, then there can't
132 * be any data in the dataset we're receiving into. Therefore
133 * a free record would simply be a no-op. Save space by not
134 * sending it to begin with.
136 if (!dsp
->dsa_incremental
)
139 if (length
!= -1ULL && offset
+ length
< offset
)
143 * If there is a pending op, but it's not PENDING_FREE, push it out,
144 * since free block aggregation can only be done for blocks of the
145 * same type (i.e., DRR_FREE records can only be aggregated with
146 * other DRR_FREE records. DRR_FREEOBJECTS records can only be
147 * aggregated with other DRR_FREEOBJECTS records.
149 if (dsp
->dsa_pending_op
!= PENDING_NONE
&&
150 dsp
->dsa_pending_op
!= PENDING_FREE
) {
151 if (dump_bytes(dsp
, dsp
->dsa_drr
,
152 sizeof (dmu_replay_record_t
)) != 0)
153 return (SET_ERROR(EINTR
));
154 dsp
->dsa_pending_op
= PENDING_NONE
;
157 if (dsp
->dsa_pending_op
== PENDING_FREE
) {
159 * There should never be a PENDING_FREE if length is -1
160 * (because dump_dnode is the only place where this
161 * function is called with a -1, and only after flushing
162 * any pending record).
164 ASSERT(length
!= -1ULL);
166 * Check to see whether this free block can be aggregated
169 if (drrf
->drr_object
== object
&& drrf
->drr_offset
+
170 drrf
->drr_length
== offset
) {
171 drrf
->drr_length
+= length
;
174 /* not a continuation. Push out pending record */
175 if (dump_bytes(dsp
, dsp
->dsa_drr
,
176 sizeof (dmu_replay_record_t
)) != 0)
177 return (SET_ERROR(EINTR
));
178 dsp
->dsa_pending_op
= PENDING_NONE
;
181 /* create a FREE record and make it pending */
182 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
183 dsp
->dsa_drr
->drr_type
= DRR_FREE
;
184 drrf
->drr_object
= object
;
185 drrf
->drr_offset
= offset
;
186 drrf
->drr_length
= length
;
187 drrf
->drr_toguid
= dsp
->dsa_toguid
;
188 if (length
== -1ULL) {
189 if (dump_bytes(dsp
, dsp
->dsa_drr
,
190 sizeof (dmu_replay_record_t
)) != 0)
191 return (SET_ERROR(EINTR
));
193 dsp
->dsa_pending_op
= PENDING_FREE
;
200 dump_data(dmu_sendarg_t
*dsp
, dmu_object_type_t type
,
201 uint64_t object
, uint64_t offset
, int blksz
, const blkptr_t
*bp
, void *data
)
203 struct drr_write
*drrw
= &(dsp
->dsa_drr
->drr_u
.drr_write
);
206 * We send data in increasing object, offset order.
207 * See comment in dump_free() for details.
209 ASSERT(object
> dsp
->dsa_last_data_object
||
210 (object
== dsp
->dsa_last_data_object
&&
211 offset
> dsp
->dsa_last_data_offset
));
212 dsp
->dsa_last_data_object
= object
;
213 dsp
->dsa_last_data_offset
= offset
+ blksz
- 1;
216 * If there is any kind of pending aggregation (currently either
217 * a grouping of free objects or free blocks), push it out to
218 * the stream, since aggregation can't be done across operations
219 * of different types.
221 if (dsp
->dsa_pending_op
!= PENDING_NONE
) {
222 if (dump_bytes(dsp
, dsp
->dsa_drr
,
223 sizeof (dmu_replay_record_t
)) != 0)
224 return (SET_ERROR(EINTR
));
225 dsp
->dsa_pending_op
= PENDING_NONE
;
227 /* write a DATA record */
228 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
229 dsp
->dsa_drr
->drr_type
= DRR_WRITE
;
230 drrw
->drr_object
= object
;
231 drrw
->drr_type
= type
;
232 drrw
->drr_offset
= offset
;
233 drrw
->drr_length
= blksz
;
234 drrw
->drr_toguid
= dsp
->dsa_toguid
;
235 drrw
->drr_checksumtype
= BP_GET_CHECKSUM(bp
);
236 if (zio_checksum_table
[drrw
->drr_checksumtype
].ci_dedup
)
237 drrw
->drr_checksumflags
|= DRR_CHECKSUM_DEDUP
;
238 DDK_SET_LSIZE(&drrw
->drr_key
, BP_GET_LSIZE(bp
));
239 DDK_SET_PSIZE(&drrw
->drr_key
, BP_GET_PSIZE(bp
));
240 DDK_SET_COMPRESS(&drrw
->drr_key
, BP_GET_COMPRESS(bp
));
241 drrw
->drr_key
.ddk_cksum
= bp
->blk_cksum
;
243 if (dump_bytes(dsp
, dsp
->dsa_drr
, sizeof (dmu_replay_record_t
)) != 0)
244 return (SET_ERROR(EINTR
));
245 if (dump_bytes(dsp
, data
, blksz
) != 0)
246 return (SET_ERROR(EINTR
));
251 dump_spill(dmu_sendarg_t
*dsp
, uint64_t object
, int blksz
, void *data
)
253 struct drr_spill
*drrs
= &(dsp
->dsa_drr
->drr_u
.drr_spill
);
255 if (dsp
->dsa_pending_op
!= PENDING_NONE
) {
256 if (dump_bytes(dsp
, dsp
->dsa_drr
,
257 sizeof (dmu_replay_record_t
)) != 0)
258 return (SET_ERROR(EINTR
));
259 dsp
->dsa_pending_op
= PENDING_NONE
;
262 /* write a SPILL record */
263 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
264 dsp
->dsa_drr
->drr_type
= DRR_SPILL
;
265 drrs
->drr_object
= object
;
266 drrs
->drr_length
= blksz
;
267 drrs
->drr_toguid
= dsp
->dsa_toguid
;
269 if (dump_bytes(dsp
, dsp
->dsa_drr
, sizeof (dmu_replay_record_t
)))
270 return (SET_ERROR(EINTR
));
271 if (dump_bytes(dsp
, data
, blksz
))
272 return (SET_ERROR(EINTR
));
277 dump_freeobjects(dmu_sendarg_t
*dsp
, uint64_t firstobj
, uint64_t numobjs
)
279 struct drr_freeobjects
*drrfo
= &(dsp
->dsa_drr
->drr_u
.drr_freeobjects
);
281 /* See comment in dump_free(). */
282 if (!dsp
->dsa_incremental
)
286 * If there is a pending op, but it's not PENDING_FREEOBJECTS,
287 * push it out, since free block aggregation can only be done for
288 * blocks of the same type (i.e., DRR_FREE records can only be
289 * aggregated with other DRR_FREE records. DRR_FREEOBJECTS records
290 * can only be aggregated with other DRR_FREEOBJECTS records.
292 if (dsp
->dsa_pending_op
!= PENDING_NONE
&&
293 dsp
->dsa_pending_op
!= PENDING_FREEOBJECTS
) {
294 if (dump_bytes(dsp
, dsp
->dsa_drr
,
295 sizeof (dmu_replay_record_t
)) != 0)
296 return (SET_ERROR(EINTR
));
297 dsp
->dsa_pending_op
= PENDING_NONE
;
299 if (dsp
->dsa_pending_op
== PENDING_FREEOBJECTS
) {
301 * See whether this free object array can be aggregated
304 if (drrfo
->drr_firstobj
+ drrfo
->drr_numobjs
== firstobj
) {
305 drrfo
->drr_numobjs
+= numobjs
;
308 /* can't be aggregated. Push out pending record */
309 if (dump_bytes(dsp
, dsp
->dsa_drr
,
310 sizeof (dmu_replay_record_t
)) != 0)
311 return (SET_ERROR(EINTR
));
312 dsp
->dsa_pending_op
= PENDING_NONE
;
316 /* write a FREEOBJECTS record */
317 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
318 dsp
->dsa_drr
->drr_type
= DRR_FREEOBJECTS
;
319 drrfo
->drr_firstobj
= firstobj
;
320 drrfo
->drr_numobjs
= numobjs
;
321 drrfo
->drr_toguid
= dsp
->dsa_toguid
;
323 dsp
->dsa_pending_op
= PENDING_FREEOBJECTS
;
329 dump_dnode(dmu_sendarg_t
*dsp
, uint64_t object
, dnode_phys_t
*dnp
)
331 struct drr_object
*drro
= &(dsp
->dsa_drr
->drr_u
.drr_object
);
333 if (dnp
== NULL
|| dnp
->dn_type
== DMU_OT_NONE
)
334 return (dump_freeobjects(dsp
, object
, 1));
336 if (dsp
->dsa_pending_op
!= PENDING_NONE
) {
337 if (dump_bytes(dsp
, dsp
->dsa_drr
,
338 sizeof (dmu_replay_record_t
)) != 0)
339 return (SET_ERROR(EINTR
));
340 dsp
->dsa_pending_op
= PENDING_NONE
;
343 /* write an OBJECT record */
344 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
345 dsp
->dsa_drr
->drr_type
= DRR_OBJECT
;
346 drro
->drr_object
= object
;
347 drro
->drr_type
= dnp
->dn_type
;
348 drro
->drr_bonustype
= dnp
->dn_bonustype
;
349 drro
->drr_blksz
= dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
;
350 drro
->drr_bonuslen
= dnp
->dn_bonuslen
;
351 drro
->drr_checksumtype
= dnp
->dn_checksum
;
352 drro
->drr_compress
= dnp
->dn_compress
;
353 drro
->drr_toguid
= dsp
->dsa_toguid
;
355 if (dump_bytes(dsp
, dsp
->dsa_drr
, sizeof (dmu_replay_record_t
)) != 0)
356 return (SET_ERROR(EINTR
));
358 if (dump_bytes(dsp
, DN_BONUS(dnp
), P2ROUNDUP(dnp
->dn_bonuslen
, 8)) != 0)
359 return (SET_ERROR(EINTR
));
361 /* Free anything past the end of the file. */
362 if (dump_free(dsp
, object
, (dnp
->dn_maxblkid
+ 1) *
363 (dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
), -1ULL) != 0)
364 return (SET_ERROR(EINTR
));
365 if (dsp
->dsa_err
!= 0)
366 return (SET_ERROR(EINTR
));
370 #define BP_SPAN(dnp, level) \
371 (((uint64_t)dnp->dn_datablkszsec) << (SPA_MINBLOCKSHIFT + \
372 (level) * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT)))
376 backup_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
377 const zbookmark_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
379 dmu_sendarg_t
*dsp
= arg
;
380 dmu_object_type_t type
= bp
? BP_GET_TYPE(bp
) : DMU_OT_NONE
;
383 if (issig(JUSTLOOKING
) && issig(FORREAL
))
384 return (SET_ERROR(EINTR
));
386 if (zb
->zb_object
!= DMU_META_DNODE_OBJECT
&&
387 DMU_OBJECT_IS_SPECIAL(zb
->zb_object
)) {
389 } else if (zb
->zb_level
== ZB_ZIL_LEVEL
) {
391 * If we are sending a non-snapshot (which is allowed on
392 * read-only pools), it may have a ZIL, which must be ignored.
395 } else if (BP_IS_HOLE(bp
) &&
396 zb
->zb_object
== DMU_META_DNODE_OBJECT
) {
397 uint64_t span
= BP_SPAN(dnp
, zb
->zb_level
);
398 uint64_t dnobj
= (zb
->zb_blkid
* span
) >> DNODE_SHIFT
;
399 err
= dump_freeobjects(dsp
, dnobj
, span
>> DNODE_SHIFT
);
400 } else if (BP_IS_HOLE(bp
)) {
401 uint64_t span
= BP_SPAN(dnp
, zb
->zb_level
);
402 err
= dump_free(dsp
, zb
->zb_object
, zb
->zb_blkid
* span
, span
);
403 } else if (zb
->zb_level
> 0 || type
== DMU_OT_OBJSET
) {
405 } else if (type
== DMU_OT_DNODE
) {
408 int blksz
= BP_GET_LSIZE(bp
);
409 uint32_t aflags
= ARC_WAIT
;
412 if (arc_read(NULL
, spa
, bp
, arc_getbuf_func
, &abuf
,
413 ZIO_PRIORITY_ASYNC_READ
, ZIO_FLAG_CANFAIL
,
415 return (SET_ERROR(EIO
));
418 for (i
= 0; i
< blksz
>> DNODE_SHIFT
; i
++) {
419 uint64_t dnobj
= (zb
->zb_blkid
<<
420 (DNODE_BLOCK_SHIFT
- DNODE_SHIFT
)) + i
;
421 err
= dump_dnode(dsp
, dnobj
, blk
+i
);
425 (void) arc_buf_remove_ref(abuf
, &abuf
);
426 } else if (type
== DMU_OT_SA
) {
427 uint32_t aflags
= ARC_WAIT
;
429 int blksz
= BP_GET_LSIZE(bp
);
431 if (arc_read(NULL
, spa
, bp
, arc_getbuf_func
, &abuf
,
432 ZIO_PRIORITY_ASYNC_READ
, ZIO_FLAG_CANFAIL
,
434 return (SET_ERROR(EIO
));
436 err
= dump_spill(dsp
, zb
->zb_object
, blksz
, abuf
->b_data
);
437 (void) arc_buf_remove_ref(abuf
, &abuf
);
438 } else { /* it's a level-0 block of a regular object */
439 uint32_t aflags
= ARC_WAIT
;
441 int blksz
= BP_GET_LSIZE(bp
);
443 ASSERT0(zb
->zb_level
);
444 if (arc_read(NULL
, spa
, bp
, arc_getbuf_func
, &abuf
,
445 ZIO_PRIORITY_ASYNC_READ
, ZIO_FLAG_CANFAIL
,
447 if (zfs_send_corrupt_data
) {
449 /* Send a block filled with 0x"zfs badd bloc" */
450 abuf
= arc_buf_alloc(spa
, blksz
, &abuf
,
452 for (ptr
= abuf
->b_data
;
453 (char *)ptr
< (char *)abuf
->b_data
+ blksz
;
455 *ptr
= 0x2f5baddb10cULL
;
457 return (SET_ERROR(EIO
));
461 err
= dump_data(dsp
, type
, zb
->zb_object
, zb
->zb_blkid
* blksz
,
462 blksz
, bp
, abuf
->b_data
);
463 (void) arc_buf_remove_ref(abuf
, &abuf
);
466 ASSERT(err
== 0 || err
== EINTR
);
471 * Releases dp using the specified tag.
474 dmu_send_impl(void *tag
, dsl_pool_t
*dp
, dsl_dataset_t
*ds
,
475 zfs_bookmark_phys_t
*fromzb
, boolean_t is_clone
, int outfd
,
476 vnode_t
*vp
, offset_t
*off
)
479 dmu_replay_record_t
*drr
;
482 uint64_t fromtxg
= 0;
484 err
= dmu_objset_from_ds(ds
, &os
);
486 dsl_pool_rele(dp
, tag
);
490 drr
= kmem_zalloc(sizeof (dmu_replay_record_t
), KM_SLEEP
);
491 drr
->drr_type
= DRR_BEGIN
;
492 drr
->drr_u
.drr_begin
.drr_magic
= DMU_BACKUP_MAGIC
;
493 DMU_SET_STREAM_HDRTYPE(drr
->drr_u
.drr_begin
.drr_versioninfo
,
497 if (dmu_objset_type(os
) == DMU_OST_ZFS
) {
499 if (zfs_get_zplprop(os
, ZFS_PROP_VERSION
, &version
) != 0) {
500 kmem_free(drr
, sizeof (dmu_replay_record_t
));
501 dsl_pool_rele(dp
, tag
);
502 return (SET_ERROR(EINVAL
));
504 if (version
>= ZPL_VERSION_SA
) {
505 DMU_SET_FEATUREFLAGS(
506 drr
->drr_u
.drr_begin
.drr_versioninfo
,
507 DMU_BACKUP_FEATURE_SA_SPILL
);
512 drr
->drr_u
.drr_begin
.drr_creation_time
=
513 ds
->ds_phys
->ds_creation_time
;
514 drr
->drr_u
.drr_begin
.drr_type
= dmu_objset_type(os
);
516 drr
->drr_u
.drr_begin
.drr_flags
|= DRR_FLAG_CLONE
;
517 drr
->drr_u
.drr_begin
.drr_toguid
= ds
->ds_phys
->ds_guid
;
518 if (ds
->ds_phys
->ds_flags
& DS_FLAG_CI_DATASET
)
519 drr
->drr_u
.drr_begin
.drr_flags
|= DRR_FLAG_CI_DATA
;
521 if (fromzb
!= NULL
) {
522 drr
->drr_u
.drr_begin
.drr_fromguid
= fromzb
->zbm_guid
;
523 fromtxg
= fromzb
->zbm_creation_txg
;
525 dsl_dataset_name(ds
, drr
->drr_u
.drr_begin
.drr_toname
);
526 if (!dsl_dataset_is_snapshot(ds
)) {
527 (void) strlcat(drr
->drr_u
.drr_begin
.drr_toname
, "@--head--",
528 sizeof (drr
->drr_u
.drr_begin
.drr_toname
));
531 dsp
= kmem_zalloc(sizeof (dmu_sendarg_t
), KM_SLEEP
);
535 dsp
->dsa_outfd
= outfd
;
536 dsp
->dsa_proc
= curproc
;
539 dsp
->dsa_toguid
= ds
->ds_phys
->ds_guid
;
540 ZIO_SET_CHECKSUM(&dsp
->dsa_zc
, 0, 0, 0, 0);
541 dsp
->dsa_pending_op
= PENDING_NONE
;
542 dsp
->dsa_incremental
= (fromzb
!= NULL
);
544 mutex_enter(&ds
->ds_sendstream_lock
);
545 list_insert_head(&ds
->ds_sendstreams
, dsp
);
546 mutex_exit(&ds
->ds_sendstream_lock
);
548 dsl_dataset_long_hold(ds
, FTAG
);
549 dsl_pool_rele(dp
, tag
);
551 if (dump_bytes(dsp
, drr
, sizeof (dmu_replay_record_t
)) != 0) {
556 err
= traverse_dataset(ds
, fromtxg
, TRAVERSE_PRE
| TRAVERSE_PREFETCH
,
559 if (dsp
->dsa_pending_op
!= PENDING_NONE
)
560 if (dump_bytes(dsp
, drr
, sizeof (dmu_replay_record_t
)) != 0)
561 err
= SET_ERROR(EINTR
);
564 if (err
== EINTR
&& dsp
->dsa_err
!= 0)
569 bzero(drr
, sizeof (dmu_replay_record_t
));
570 drr
->drr_type
= DRR_END
;
571 drr
->drr_u
.drr_end
.drr_checksum
= dsp
->dsa_zc
;
572 drr
->drr_u
.drr_end
.drr_toguid
= dsp
->dsa_toguid
;
574 if (dump_bytes(dsp
, drr
, sizeof (dmu_replay_record_t
)) != 0) {
580 mutex_enter(&ds
->ds_sendstream_lock
);
581 list_remove(&ds
->ds_sendstreams
, dsp
);
582 mutex_exit(&ds
->ds_sendstream_lock
);
584 kmem_free(drr
, sizeof (dmu_replay_record_t
));
585 kmem_free(dsp
, sizeof (dmu_sendarg_t
));
587 dsl_dataset_long_rele(ds
, FTAG
);
593 dmu_send_obj(const char *pool
, uint64_t tosnap
, uint64_t fromsnap
,
594 int outfd
, vnode_t
*vp
, offset_t
*off
)
598 dsl_dataset_t
*fromds
= NULL
;
601 err
= dsl_pool_hold(pool
, FTAG
, &dp
);
605 err
= dsl_dataset_hold_obj(dp
, tosnap
, FTAG
, &ds
);
607 dsl_pool_rele(dp
, FTAG
);
612 zfs_bookmark_phys_t zb
;
615 err
= dsl_dataset_hold_obj(dp
, fromsnap
, FTAG
, &fromds
);
617 dsl_dataset_rele(ds
, FTAG
);
618 dsl_pool_rele(dp
, FTAG
);
621 if (!dsl_dataset_is_before(ds
, fromds
, 0))
622 err
= SET_ERROR(EXDEV
);
623 zb
.zbm_creation_time
= fromds
->ds_phys
->ds_creation_time
;
624 zb
.zbm_creation_txg
= fromds
->ds_phys
->ds_creation_txg
;
625 zb
.zbm_guid
= fromds
->ds_phys
->ds_guid
;
626 is_clone
= (fromds
->ds_dir
!= ds
->ds_dir
);
627 dsl_dataset_rele(fromds
, FTAG
);
628 err
= dmu_send_impl(FTAG
, dp
, ds
, &zb
, is_clone
,
631 err
= dmu_send_impl(FTAG
, dp
, ds
, NULL
, B_FALSE
,
634 dsl_dataset_rele(ds
, FTAG
);
639 dmu_send(const char *tosnap
, const char *fromsnap
,
640 int outfd
, vnode_t
*vp
, offset_t
*off
)
645 boolean_t owned
= B_FALSE
;
647 if (fromsnap
!= NULL
&& strpbrk(fromsnap
, "@#") == NULL
)
648 return (SET_ERROR(EINVAL
));
650 err
= dsl_pool_hold(tosnap
, FTAG
, &dp
);
654 if (strchr(tosnap
, '@') == NULL
&& spa_writeable(dp
->dp_spa
)) {
656 * We are sending a filesystem or volume. Ensure
657 * that it doesn't change by owning the dataset.
659 err
= dsl_dataset_own(dp
, tosnap
, FTAG
, &ds
);
662 err
= dsl_dataset_hold(dp
, tosnap
, FTAG
, &ds
);
665 dsl_pool_rele(dp
, FTAG
);
669 if (fromsnap
!= NULL
) {
670 zfs_bookmark_phys_t zb
;
671 boolean_t is_clone
= B_FALSE
;
672 int fsnamelen
= strchr(tosnap
, '@') - tosnap
;
675 * If the fromsnap is in a different filesystem, then
676 * mark the send stream as a clone.
678 if (strncmp(tosnap
, fromsnap
, fsnamelen
) != 0 ||
679 (fromsnap
[fsnamelen
] != '@' &&
680 fromsnap
[fsnamelen
] != '#')) {
684 if (strchr(fromsnap
, '@')) {
685 dsl_dataset_t
*fromds
;
686 err
= dsl_dataset_hold(dp
, fromsnap
, FTAG
, &fromds
);
688 if (!dsl_dataset_is_before(ds
, fromds
, 0))
689 err
= SET_ERROR(EXDEV
);
690 zb
.zbm_creation_time
=
691 fromds
->ds_phys
->ds_creation_time
;
692 zb
.zbm_creation_txg
=
693 fromds
->ds_phys
->ds_creation_txg
;
694 zb
.zbm_guid
= fromds
->ds_phys
->ds_guid
;
695 is_clone
= (ds
->ds_dir
!= fromds
->ds_dir
);
696 dsl_dataset_rele(fromds
, FTAG
);
699 err
= dsl_bookmark_lookup(dp
, fromsnap
, ds
, &zb
);
702 dsl_dataset_rele(ds
, FTAG
);
703 dsl_pool_rele(dp
, FTAG
);
706 err
= dmu_send_impl(FTAG
, dp
, ds
, &zb
, is_clone
,
709 err
= dmu_send_impl(FTAG
, dp
, ds
, NULL
, B_FALSE
,
713 dsl_dataset_disown(ds
, FTAG
);
715 dsl_dataset_rele(ds
, FTAG
);
720 dmu_send_estimate(dsl_dataset_t
*ds
, dsl_dataset_t
*fromds
, uint64_t *sizep
)
723 uint64_t size
, recordsize
;
724 ASSERTV(dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
);
726 ASSERT(dsl_pool_config_held(dp
));
728 /* tosnap must be a snapshot */
729 if (!dsl_dataset_is_snapshot(ds
))
730 return (SET_ERROR(EINVAL
));
733 * fromsnap must be an earlier snapshot from the same fs as tosnap,
734 * or the origin's fs.
736 if (fromds
!= NULL
&& !dsl_dataset_is_before(ds
, fromds
, 0))
737 return (SET_ERROR(EXDEV
));
739 /* Get uncompressed size estimate of changed data. */
740 if (fromds
== NULL
) {
741 size
= ds
->ds_phys
->ds_uncompressed_bytes
;
744 err
= dsl_dataset_space_written(fromds
, ds
,
745 &used
, &comp
, &size
);
751 * Assume that space (both on-disk and in-stream) is dominated by
752 * data. We will adjust for indirect blocks and the copies property,
753 * but ignore per-object space used (eg, dnodes and DRR_OBJECT records).
757 * Subtract out approximate space used by indirect blocks.
758 * Assume most space is used by data blocks (non-indirect, non-dnode).
759 * Assume all blocks are recordsize. Assume ditto blocks and
760 * internal fragmentation counter out compression.
762 * Therefore, space used by indirect blocks is sizeof(blkptr_t) per
763 * block, which we observe in practice.
765 err
= dsl_prop_get_int_ds(ds
, "recordsize", &recordsize
);
768 size
-= size
/ recordsize
* sizeof (blkptr_t
);
770 /* Add in the space for the record associated with each block. */
771 size
+= size
/ recordsize
* sizeof (dmu_replay_record_t
);
778 typedef struct dmu_recv_begin_arg
{
779 const char *drba_origin
;
780 dmu_recv_cookie_t
*drba_cookie
;
782 uint64_t drba_snapobj
;
783 } dmu_recv_begin_arg_t
;
786 recv_begin_check_existing_impl(dmu_recv_begin_arg_t
*drba
, dsl_dataset_t
*ds
,
791 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
793 /* temporary clone name must not exist */
794 error
= zap_lookup(dp
->dp_meta_objset
,
795 ds
->ds_dir
->dd_phys
->dd_child_dir_zapobj
, recv_clone_name
,
798 return (error
== 0 ? EBUSY
: error
);
800 /* new snapshot name must not exist */
801 error
= zap_lookup(dp
->dp_meta_objset
,
802 ds
->ds_phys
->ds_snapnames_zapobj
, drba
->drba_cookie
->drc_tosnap
,
805 return (error
== 0 ? EEXIST
: error
);
809 uint64_t obj
= ds
->ds_phys
->ds_prev_snap_obj
;
811 /* Find snapshot in this dir that matches fromguid. */
813 error
= dsl_dataset_hold_obj(dp
, obj
, FTAG
,
816 return (SET_ERROR(ENODEV
));
817 if (snap
->ds_dir
!= ds
->ds_dir
) {
818 dsl_dataset_rele(snap
, FTAG
);
819 return (SET_ERROR(ENODEV
));
821 if (snap
->ds_phys
->ds_guid
== fromguid
)
823 obj
= snap
->ds_phys
->ds_prev_snap_obj
;
824 dsl_dataset_rele(snap
, FTAG
);
827 return (SET_ERROR(ENODEV
));
829 if (drba
->drba_cookie
->drc_force
) {
830 drba
->drba_snapobj
= obj
;
833 * If we are not forcing, there must be no
834 * changes since fromsnap.
836 if (dsl_dataset_modified_since_snap(ds
, snap
)) {
837 dsl_dataset_rele(snap
, FTAG
);
838 return (SET_ERROR(ETXTBSY
));
840 drba
->drba_snapobj
= ds
->ds_prev
->ds_object
;
843 dsl_dataset_rele(snap
, FTAG
);
845 /* if full, most recent snapshot must be $ORIGIN */
846 if (ds
->ds_phys
->ds_prev_snap_txg
>= TXG_INITIAL
)
847 return (SET_ERROR(ENODEV
));
848 drba
->drba_snapobj
= ds
->ds_phys
->ds_prev_snap_obj
;
856 dmu_recv_begin_check(void *arg
, dmu_tx_t
*tx
)
858 dmu_recv_begin_arg_t
*drba
= arg
;
859 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
860 struct drr_begin
*drrb
= drba
->drba_cookie
->drc_drrb
;
861 uint64_t fromguid
= drrb
->drr_fromguid
;
862 int flags
= drrb
->drr_flags
;
865 const char *tofs
= drba
->drba_cookie
->drc_tofs
;
867 /* already checked */
868 ASSERT3U(drrb
->drr_magic
, ==, DMU_BACKUP_MAGIC
);
870 if (DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) ==
871 DMU_COMPOUNDSTREAM
||
872 drrb
->drr_type
>= DMU_OST_NUMTYPES
||
873 ((flags
& DRR_FLAG_CLONE
) && drba
->drba_origin
== NULL
))
874 return (SET_ERROR(EINVAL
));
876 /* Verify pool version supports SA if SA_SPILL feature set */
877 if ((DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
) &
878 DMU_BACKUP_FEATURE_SA_SPILL
) &&
879 spa_version(dp
->dp_spa
) < SPA_VERSION_SA
) {
880 return (SET_ERROR(ENOTSUP
));
883 error
= dsl_dataset_hold(dp
, tofs
, FTAG
, &ds
);
885 /* target fs already exists; recv into temp clone */
887 /* Can't recv a clone into an existing fs */
888 if (flags
& DRR_FLAG_CLONE
) {
889 dsl_dataset_rele(ds
, FTAG
);
890 return (SET_ERROR(EINVAL
));
893 error
= recv_begin_check_existing_impl(drba
, ds
, fromguid
);
894 dsl_dataset_rele(ds
, FTAG
);
895 } else if (error
== ENOENT
) {
896 /* target fs does not exist; must be a full backup or clone */
897 char buf
[MAXNAMELEN
];
900 * If it's a non-clone incremental, we are missing the
901 * target fs, so fail the recv.
903 if (fromguid
!= 0 && !(flags
& DRR_FLAG_CLONE
))
904 return (SET_ERROR(ENOENT
));
906 /* Open the parent of tofs */
907 ASSERT3U(strlen(tofs
), <, MAXNAMELEN
);
908 (void) strlcpy(buf
, tofs
, strrchr(tofs
, '/') - tofs
+ 1);
909 error
= dsl_dataset_hold(dp
, buf
, FTAG
, &ds
);
913 if (drba
->drba_origin
!= NULL
) {
914 dsl_dataset_t
*origin
;
915 error
= dsl_dataset_hold(dp
, drba
->drba_origin
,
918 dsl_dataset_rele(ds
, FTAG
);
921 if (!dsl_dataset_is_snapshot(origin
)) {
922 dsl_dataset_rele(origin
, FTAG
);
923 dsl_dataset_rele(ds
, FTAG
);
924 return (SET_ERROR(EINVAL
));
926 if (origin
->ds_phys
->ds_guid
!= fromguid
) {
927 dsl_dataset_rele(origin
, FTAG
);
928 dsl_dataset_rele(ds
, FTAG
);
929 return (SET_ERROR(ENODEV
));
931 dsl_dataset_rele(origin
, FTAG
);
933 dsl_dataset_rele(ds
, FTAG
);
940 dmu_recv_begin_sync(void *arg
, dmu_tx_t
*tx
)
942 dmu_recv_begin_arg_t
*drba
= arg
;
943 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
944 struct drr_begin
*drrb
= drba
->drba_cookie
->drc_drrb
;
945 const char *tofs
= drba
->drba_cookie
->drc_tofs
;
946 dsl_dataset_t
*ds
, *newds
;
951 crflags
= (drrb
->drr_flags
& DRR_FLAG_CI_DATA
) ?
952 DS_FLAG_CI_DATASET
: 0;
954 error
= dsl_dataset_hold(dp
, tofs
, FTAG
, &ds
);
956 /* create temporary clone */
957 dsl_dataset_t
*snap
= NULL
;
958 if (drba
->drba_snapobj
!= 0) {
959 VERIFY0(dsl_dataset_hold_obj(dp
,
960 drba
->drba_snapobj
, FTAG
, &snap
));
962 dsobj
= dsl_dataset_create_sync(ds
->ds_dir
, recv_clone_name
,
963 snap
, crflags
, drba
->drba_cred
, tx
);
964 dsl_dataset_rele(snap
, FTAG
);
965 dsl_dataset_rele(ds
, FTAG
);
969 dsl_dataset_t
*origin
= NULL
;
971 VERIFY0(dsl_dir_hold(dp
, tofs
, FTAG
, &dd
, &tail
));
973 if (drba
->drba_origin
!= NULL
) {
974 VERIFY0(dsl_dataset_hold(dp
, drba
->drba_origin
,
978 /* Create new dataset. */
979 dsobj
= dsl_dataset_create_sync(dd
,
980 strrchr(tofs
, '/') + 1,
981 origin
, crflags
, drba
->drba_cred
, tx
);
983 dsl_dataset_rele(origin
, FTAG
);
984 dsl_dir_rele(dd
, FTAG
);
985 drba
->drba_cookie
->drc_newfs
= B_TRUE
;
987 VERIFY0(dsl_dataset_own_obj(dp
, dsobj
, dmu_recv_tag
, &newds
));
989 dmu_buf_will_dirty(newds
->ds_dbuf
, tx
);
990 newds
->ds_phys
->ds_flags
|= DS_FLAG_INCONSISTENT
;
993 * If we actually created a non-clone, we need to create the
994 * objset in our new dataset.
996 if (BP_IS_HOLE(dsl_dataset_get_blkptr(newds
))) {
997 (void) dmu_objset_create_impl(dp
->dp_spa
,
998 newds
, dsl_dataset_get_blkptr(newds
), drrb
->drr_type
, tx
);
1001 drba
->drba_cookie
->drc_ds
= newds
;
1003 spa_history_log_internal_ds(newds
, "receive", tx
, "");
1007 * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
1008 * succeeds; otherwise we will leak the holds on the datasets.
1011 dmu_recv_begin(char *tofs
, char *tosnap
, struct drr_begin
*drrb
,
1012 boolean_t force
, char *origin
, dmu_recv_cookie_t
*drc
)
1014 dmu_recv_begin_arg_t drba
= { 0 };
1015 dmu_replay_record_t
*drr
;
1017 bzero(drc
, sizeof (dmu_recv_cookie_t
));
1018 drc
->drc_drrb
= drrb
;
1019 drc
->drc_tosnap
= tosnap
;
1020 drc
->drc_tofs
= tofs
;
1021 drc
->drc_force
= force
;
1023 if (drrb
->drr_magic
== BSWAP_64(DMU_BACKUP_MAGIC
))
1024 drc
->drc_byteswap
= B_TRUE
;
1025 else if (drrb
->drr_magic
!= DMU_BACKUP_MAGIC
)
1026 return (SET_ERROR(EINVAL
));
1028 drr
= kmem_zalloc(sizeof (dmu_replay_record_t
), KM_SLEEP
);
1029 drr
->drr_type
= DRR_BEGIN
;
1030 drr
->drr_u
.drr_begin
= *drc
->drc_drrb
;
1031 if (drc
->drc_byteswap
) {
1032 fletcher_4_incremental_byteswap(drr
,
1033 sizeof (dmu_replay_record_t
), &drc
->drc_cksum
);
1035 fletcher_4_incremental_native(drr
,
1036 sizeof (dmu_replay_record_t
), &drc
->drc_cksum
);
1038 kmem_free(drr
, sizeof (dmu_replay_record_t
));
1040 if (drc
->drc_byteswap
) {
1041 drrb
->drr_magic
= BSWAP_64(drrb
->drr_magic
);
1042 drrb
->drr_versioninfo
= BSWAP_64(drrb
->drr_versioninfo
);
1043 drrb
->drr_creation_time
= BSWAP_64(drrb
->drr_creation_time
);
1044 drrb
->drr_type
= BSWAP_32(drrb
->drr_type
);
1045 drrb
->drr_toguid
= BSWAP_64(drrb
->drr_toguid
);
1046 drrb
->drr_fromguid
= BSWAP_64(drrb
->drr_fromguid
);
1049 drba
.drba_origin
= origin
;
1050 drba
.drba_cookie
= drc
;
1051 drba
.drba_cred
= CRED();
1053 return (dsl_sync_task(tofs
, dmu_recv_begin_check
, dmu_recv_begin_sync
,
1063 int bufsize
; /* amount of memory allocated for buf */
1065 avl_tree_t
*guid_to_ds_map
;
1068 typedef struct guid_map_entry
{
1070 dsl_dataset_t
*gme_ds
;
1075 guid_compare(const void *arg1
, const void *arg2
)
1077 const guid_map_entry_t
*gmep1
= arg1
;
1078 const guid_map_entry_t
*gmep2
= arg2
;
1080 if (gmep1
->guid
< gmep2
->guid
)
1082 else if (gmep1
->guid
> gmep2
->guid
)
1088 free_guid_map_onexit(void *arg
)
1090 avl_tree_t
*ca
= arg
;
1091 void *cookie
= NULL
;
1092 guid_map_entry_t
*gmep
;
1094 while ((gmep
= avl_destroy_nodes(ca
, &cookie
)) != NULL
) {
1095 dsl_dataset_long_rele(gmep
->gme_ds
, gmep
);
1096 dsl_dataset_rele(gmep
->gme_ds
, gmep
);
1097 kmem_free(gmep
, sizeof (guid_map_entry_t
));
1100 kmem_free(ca
, sizeof (avl_tree_t
));
1104 restore_read(struct restorearg
*ra
, int len
)
1109 /* some things will require 8-byte alignment, so everything must */
1112 while (done
< len
) {
1115 ra
->err
= vn_rdwr(UIO_READ
, ra
->vp
,
1116 (caddr_t
)ra
->buf
+ done
, len
- done
,
1117 ra
->voff
, UIO_SYSSPACE
, FAPPEND
,
1118 RLIM64_INFINITY
, CRED(), &resid
);
1120 if (resid
== len
- done
)
1121 ra
->err
= SET_ERROR(EINVAL
);
1122 ra
->voff
+= len
- done
- resid
;
1128 ASSERT3U(done
, ==, len
);
1131 fletcher_4_incremental_byteswap(rv
, len
, &ra
->cksum
);
1133 fletcher_4_incremental_native(rv
, len
, &ra
->cksum
);
1137 noinline
static void
1138 backup_byteswap(dmu_replay_record_t
*drr
)
1140 #define DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
1141 #define DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
1142 drr
->drr_type
= BSWAP_32(drr
->drr_type
);
1143 drr
->drr_payloadlen
= BSWAP_32(drr
->drr_payloadlen
);
1144 switch (drr
->drr_type
) {
1146 DO64(drr_begin
.drr_magic
);
1147 DO64(drr_begin
.drr_versioninfo
);
1148 DO64(drr_begin
.drr_creation_time
);
1149 DO32(drr_begin
.drr_type
);
1150 DO32(drr_begin
.drr_flags
);
1151 DO64(drr_begin
.drr_toguid
);
1152 DO64(drr_begin
.drr_fromguid
);
1155 DO64(drr_object
.drr_object
);
1156 /* DO64(drr_object.drr_allocation_txg); */
1157 DO32(drr_object
.drr_type
);
1158 DO32(drr_object
.drr_bonustype
);
1159 DO32(drr_object
.drr_blksz
);
1160 DO32(drr_object
.drr_bonuslen
);
1161 DO64(drr_object
.drr_toguid
);
1163 case DRR_FREEOBJECTS
:
1164 DO64(drr_freeobjects
.drr_firstobj
);
1165 DO64(drr_freeobjects
.drr_numobjs
);
1166 DO64(drr_freeobjects
.drr_toguid
);
1169 DO64(drr_write
.drr_object
);
1170 DO32(drr_write
.drr_type
);
1171 DO64(drr_write
.drr_offset
);
1172 DO64(drr_write
.drr_length
);
1173 DO64(drr_write
.drr_toguid
);
1174 DO64(drr_write
.drr_key
.ddk_cksum
.zc_word
[0]);
1175 DO64(drr_write
.drr_key
.ddk_cksum
.zc_word
[1]);
1176 DO64(drr_write
.drr_key
.ddk_cksum
.zc_word
[2]);
1177 DO64(drr_write
.drr_key
.ddk_cksum
.zc_word
[3]);
1178 DO64(drr_write
.drr_key
.ddk_prop
);
1180 case DRR_WRITE_BYREF
:
1181 DO64(drr_write_byref
.drr_object
);
1182 DO64(drr_write_byref
.drr_offset
);
1183 DO64(drr_write_byref
.drr_length
);
1184 DO64(drr_write_byref
.drr_toguid
);
1185 DO64(drr_write_byref
.drr_refguid
);
1186 DO64(drr_write_byref
.drr_refobject
);
1187 DO64(drr_write_byref
.drr_refoffset
);
1188 DO64(drr_write_byref
.drr_key
.ddk_cksum
.zc_word
[0]);
1189 DO64(drr_write_byref
.drr_key
.ddk_cksum
.zc_word
[1]);
1190 DO64(drr_write_byref
.drr_key
.ddk_cksum
.zc_word
[2]);
1191 DO64(drr_write_byref
.drr_key
.ddk_cksum
.zc_word
[3]);
1192 DO64(drr_write_byref
.drr_key
.ddk_prop
);
1195 DO64(drr_free
.drr_object
);
1196 DO64(drr_free
.drr_offset
);
1197 DO64(drr_free
.drr_length
);
1198 DO64(drr_free
.drr_toguid
);
1201 DO64(drr_spill
.drr_object
);
1202 DO64(drr_spill
.drr_length
);
1203 DO64(drr_spill
.drr_toguid
);
1206 DO64(drr_end
.drr_checksum
.zc_word
[0]);
1207 DO64(drr_end
.drr_checksum
.zc_word
[1]);
1208 DO64(drr_end
.drr_checksum
.zc_word
[2]);
1209 DO64(drr_end
.drr_checksum
.zc_word
[3]);
1210 DO64(drr_end
.drr_toguid
);
1220 restore_object(struct restorearg
*ra
, objset_t
*os
, struct drr_object
*drro
)
1226 if (drro
->drr_type
== DMU_OT_NONE
||
1227 !DMU_OT_IS_VALID(drro
->drr_type
) ||
1228 !DMU_OT_IS_VALID(drro
->drr_bonustype
) ||
1229 drro
->drr_checksumtype
>= ZIO_CHECKSUM_FUNCTIONS
||
1230 drro
->drr_compress
>= ZIO_COMPRESS_FUNCTIONS
||
1231 P2PHASE(drro
->drr_blksz
, SPA_MINBLOCKSIZE
) ||
1232 drro
->drr_blksz
< SPA_MINBLOCKSIZE
||
1233 drro
->drr_blksz
> SPA_MAXBLOCKSIZE
||
1234 drro
->drr_bonuslen
> DN_MAX_BONUSLEN
) {
1235 return (SET_ERROR(EINVAL
));
1238 err
= dmu_object_info(os
, drro
->drr_object
, NULL
);
1240 if (err
!= 0 && err
!= ENOENT
)
1241 return (SET_ERROR(EINVAL
));
1243 if (drro
->drr_bonuslen
) {
1244 data
= restore_read(ra
, P2ROUNDUP(drro
->drr_bonuslen
, 8));
1249 if (err
== ENOENT
) {
1250 /* currently free, want to be allocated */
1251 tx
= dmu_tx_create(os
);
1252 dmu_tx_hold_bonus(tx
, DMU_NEW_OBJECT
);
1253 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1258 err
= dmu_object_claim(os
, drro
->drr_object
,
1259 drro
->drr_type
, drro
->drr_blksz
,
1260 drro
->drr_bonustype
, drro
->drr_bonuslen
, tx
);
1263 /* currently allocated, want to be allocated */
1264 err
= dmu_object_reclaim(os
, drro
->drr_object
,
1265 drro
->drr_type
, drro
->drr_blksz
,
1266 drro
->drr_bonustype
, drro
->drr_bonuslen
);
1269 return (SET_ERROR(EINVAL
));
1272 tx
= dmu_tx_create(os
);
1273 dmu_tx_hold_bonus(tx
, drro
->drr_object
);
1274 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1280 dmu_object_set_checksum(os
, drro
->drr_object
, drro
->drr_checksumtype
,
1282 dmu_object_set_compress(os
, drro
->drr_object
, drro
->drr_compress
, tx
);
1287 VERIFY(0 == dmu_bonus_hold(os
, drro
->drr_object
, FTAG
, &db
));
1288 dmu_buf_will_dirty(db
, tx
);
1290 ASSERT3U(db
->db_size
, >=, drro
->drr_bonuslen
);
1291 bcopy(data
, db
->db_data
, drro
->drr_bonuslen
);
1293 dmu_object_byteswap_t byteswap
=
1294 DMU_OT_BYTESWAP(drro
->drr_bonustype
);
1295 dmu_ot_byteswap
[byteswap
].ob_func(db
->db_data
,
1296 drro
->drr_bonuslen
);
1298 dmu_buf_rele(db
, FTAG
);
1306 restore_freeobjects(struct restorearg
*ra
, objset_t
*os
,
1307 struct drr_freeobjects
*drrfo
)
1311 if (drrfo
->drr_firstobj
+ drrfo
->drr_numobjs
< drrfo
->drr_firstobj
)
1312 return (SET_ERROR(EINVAL
));
1314 for (obj
= drrfo
->drr_firstobj
;
1315 obj
< drrfo
->drr_firstobj
+ drrfo
->drr_numobjs
;
1316 (void) dmu_object_next(os
, &obj
, FALSE
, 0)) {
1319 if (dmu_object_info(os
, obj
, NULL
) != 0)
1322 err
= dmu_free_long_object(os
, obj
);
1330 restore_write(struct restorearg
*ra
, objset_t
*os
,
1331 struct drr_write
*drrw
)
1337 if (drrw
->drr_offset
+ drrw
->drr_length
< drrw
->drr_offset
||
1338 !DMU_OT_IS_VALID(drrw
->drr_type
))
1339 return (SET_ERROR(EINVAL
));
1341 data
= restore_read(ra
, drrw
->drr_length
);
1345 if (dmu_object_info(os
, drrw
->drr_object
, NULL
) != 0)
1346 return (SET_ERROR(EINVAL
));
1348 tx
= dmu_tx_create(os
);
1350 dmu_tx_hold_write(tx
, drrw
->drr_object
,
1351 drrw
->drr_offset
, drrw
->drr_length
);
1352 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1358 dmu_object_byteswap_t byteswap
=
1359 DMU_OT_BYTESWAP(drrw
->drr_type
);
1360 dmu_ot_byteswap
[byteswap
].ob_func(data
, drrw
->drr_length
);
1362 dmu_write(os
, drrw
->drr_object
,
1363 drrw
->drr_offset
, drrw
->drr_length
, data
, tx
);
1369 * Handle a DRR_WRITE_BYREF record. This record is used in dedup'ed
1370 * streams to refer to a copy of the data that is already on the
1371 * system because it came in earlier in the stream. This function
1372 * finds the earlier copy of the data, and uses that copy instead of
1373 * data from the stream to fulfill this write.
1376 restore_write_byref(struct restorearg
*ra
, objset_t
*os
,
1377 struct drr_write_byref
*drrwbr
)
1381 guid_map_entry_t gmesrch
;
1382 guid_map_entry_t
*gmep
;
1384 objset_t
*ref_os
= NULL
;
1387 if (drrwbr
->drr_offset
+ drrwbr
->drr_length
< drrwbr
->drr_offset
)
1388 return (SET_ERROR(EINVAL
));
1391 * If the GUID of the referenced dataset is different from the
1392 * GUID of the target dataset, find the referenced dataset.
1394 if (drrwbr
->drr_toguid
!= drrwbr
->drr_refguid
) {
1395 gmesrch
.guid
= drrwbr
->drr_refguid
;
1396 if ((gmep
= avl_find(ra
->guid_to_ds_map
, &gmesrch
,
1398 return (SET_ERROR(EINVAL
));
1400 if (dmu_objset_from_ds(gmep
->gme_ds
, &ref_os
))
1401 return (SET_ERROR(EINVAL
));
1406 err
= dmu_buf_hold(ref_os
, drrwbr
->drr_refobject
,
1407 drrwbr
->drr_refoffset
, FTAG
, &dbp
, DMU_READ_PREFETCH
);
1411 tx
= dmu_tx_create(os
);
1413 dmu_tx_hold_write(tx
, drrwbr
->drr_object
,
1414 drrwbr
->drr_offset
, drrwbr
->drr_length
);
1415 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1420 dmu_write(os
, drrwbr
->drr_object
,
1421 drrwbr
->drr_offset
, drrwbr
->drr_length
, dbp
->db_data
, tx
);
1422 dmu_buf_rele(dbp
, FTAG
);
1428 restore_spill(struct restorearg
*ra
, objset_t
*os
, struct drr_spill
*drrs
)
1432 dmu_buf_t
*db
, *db_spill
;
1435 if (drrs
->drr_length
< SPA_MINBLOCKSIZE
||
1436 drrs
->drr_length
> SPA_MAXBLOCKSIZE
)
1437 return (SET_ERROR(EINVAL
));
1439 data
= restore_read(ra
, drrs
->drr_length
);
1443 if (dmu_object_info(os
, drrs
->drr_object
, NULL
) != 0)
1444 return (SET_ERROR(EINVAL
));
1446 VERIFY(0 == dmu_bonus_hold(os
, drrs
->drr_object
, FTAG
, &db
));
1447 if ((err
= dmu_spill_hold_by_bonus(db
, FTAG
, &db_spill
)) != 0) {
1448 dmu_buf_rele(db
, FTAG
);
1452 tx
= dmu_tx_create(os
);
1454 dmu_tx_hold_spill(tx
, db
->db_object
);
1456 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1458 dmu_buf_rele(db
, FTAG
);
1459 dmu_buf_rele(db_spill
, FTAG
);
1463 dmu_buf_will_dirty(db_spill
, tx
);
1465 if (db_spill
->db_size
< drrs
->drr_length
)
1466 VERIFY(0 == dbuf_spill_set_blksz(db_spill
,
1467 drrs
->drr_length
, tx
));
1468 bcopy(data
, db_spill
->db_data
, drrs
->drr_length
);
1470 dmu_buf_rele(db
, FTAG
);
1471 dmu_buf_rele(db_spill
, FTAG
);
1479 restore_free(struct restorearg
*ra
, objset_t
*os
,
1480 struct drr_free
*drrf
)
1484 if (drrf
->drr_length
!= -1ULL &&
1485 drrf
->drr_offset
+ drrf
->drr_length
< drrf
->drr_offset
)
1486 return (SET_ERROR(EINVAL
));
1488 if (dmu_object_info(os
, drrf
->drr_object
, NULL
) != 0)
1489 return (SET_ERROR(EINVAL
));
1491 err
= dmu_free_long_range(os
, drrf
->drr_object
,
1492 drrf
->drr_offset
, drrf
->drr_length
);
1496 /* used to destroy the drc_ds on error */
1498 dmu_recv_cleanup_ds(dmu_recv_cookie_t
*drc
)
1500 char name
[MAXNAMELEN
];
1501 dsl_dataset_name(drc
->drc_ds
, name
);
1502 dsl_dataset_disown(drc
->drc_ds
, dmu_recv_tag
);
1503 (void) dsl_destroy_head(name
);
1507 * NB: callers *must* call dmu_recv_end() if this succeeds.
1510 dmu_recv_stream(dmu_recv_cookie_t
*drc
, vnode_t
*vp
, offset_t
*voffp
,
1511 int cleanup_fd
, uint64_t *action_handlep
)
1513 struct restorearg ra
= { 0 };
1514 dmu_replay_record_t
*drr
;
1519 ra
.byteswap
= drc
->drc_byteswap
;
1520 ra
.cksum
= drc
->drc_cksum
;
1524 ra
.buf
= vmem_alloc(ra
.bufsize
, KM_SLEEP
);
1526 /* these were verified in dmu_recv_begin */
1527 ASSERT3U(DMU_GET_STREAM_HDRTYPE(drc
->drc_drrb
->drr_versioninfo
), ==,
1529 ASSERT3U(drc
->drc_drrb
->drr_type
, <, DMU_OST_NUMTYPES
);
1532 * Open the objset we are modifying.
1534 VERIFY0(dmu_objset_from_ds(drc
->drc_ds
, &os
));
1536 ASSERT(drc
->drc_ds
->ds_phys
->ds_flags
& DS_FLAG_INCONSISTENT
);
1538 featureflags
= DMU_GET_FEATUREFLAGS(drc
->drc_drrb
->drr_versioninfo
);
1540 /* if this stream is dedup'ed, set up the avl tree for guid mapping */
1541 if (featureflags
& DMU_BACKUP_FEATURE_DEDUP
) {
1544 if (cleanup_fd
== -1) {
1545 ra
.err
= SET_ERROR(EBADF
);
1548 ra
.err
= zfs_onexit_fd_hold(cleanup_fd
, &minor
);
1554 if (*action_handlep
== 0) {
1556 kmem_alloc(sizeof (avl_tree_t
), KM_SLEEP
);
1557 avl_create(ra
.guid_to_ds_map
, guid_compare
,
1558 sizeof (guid_map_entry_t
),
1559 offsetof(guid_map_entry_t
, avlnode
));
1560 ra
.err
= zfs_onexit_add_cb(minor
,
1561 free_guid_map_onexit
, ra
.guid_to_ds_map
,
1566 ra
.err
= zfs_onexit_cb_data(minor
, *action_handlep
,
1567 (void **)&ra
.guid_to_ds_map
);
1572 drc
->drc_guid_to_ds_map
= ra
.guid_to_ds_map
;
1576 * Read records and process them.
1579 while (ra
.err
== 0 &&
1580 NULL
!= (drr
= restore_read(&ra
, sizeof (*drr
)))) {
1581 if (issig(JUSTLOOKING
) && issig(FORREAL
)) {
1582 ra
.err
= SET_ERROR(EINTR
);
1587 backup_byteswap(drr
);
1589 switch (drr
->drr_type
) {
1593 * We need to make a copy of the record header,
1594 * because restore_{object,write} may need to
1595 * restore_read(), which will invalidate drr.
1597 struct drr_object drro
= drr
->drr_u
.drr_object
;
1598 ra
.err
= restore_object(&ra
, os
, &drro
);
1601 case DRR_FREEOBJECTS
:
1603 struct drr_freeobjects drrfo
=
1604 drr
->drr_u
.drr_freeobjects
;
1605 ra
.err
= restore_freeobjects(&ra
, os
, &drrfo
);
1610 struct drr_write drrw
= drr
->drr_u
.drr_write
;
1611 ra
.err
= restore_write(&ra
, os
, &drrw
);
1614 case DRR_WRITE_BYREF
:
1616 struct drr_write_byref drrwbr
=
1617 drr
->drr_u
.drr_write_byref
;
1618 ra
.err
= restore_write_byref(&ra
, os
, &drrwbr
);
1623 struct drr_free drrf
= drr
->drr_u
.drr_free
;
1624 ra
.err
= restore_free(&ra
, os
, &drrf
);
1629 struct drr_end drre
= drr
->drr_u
.drr_end
;
1631 * We compare against the *previous* checksum
1632 * value, because the stored checksum is of
1633 * everything before the DRR_END record.
1635 if (!ZIO_CHECKSUM_EQUAL(drre
.drr_checksum
, pcksum
))
1636 ra
.err
= SET_ERROR(ECKSUM
);
1641 struct drr_spill drrs
= drr
->drr_u
.drr_spill
;
1642 ra
.err
= restore_spill(&ra
, os
, &drrs
);
1646 ra
.err
= SET_ERROR(EINVAL
);
1651 ASSERT(ra
.err
!= 0);
1654 if ((featureflags
& DMU_BACKUP_FEATURE_DEDUP
) && (cleanup_fd
!= -1))
1655 zfs_onexit_fd_rele(cleanup_fd
);
1659 * destroy what we created, so we don't leave it in the
1660 * inconsistent restoring state.
1662 dmu_recv_cleanup_ds(drc
);
1665 vmem_free(ra
.buf
, ra
.bufsize
);
1671 dmu_recv_end_check(void *arg
, dmu_tx_t
*tx
)
1673 dmu_recv_cookie_t
*drc
= arg
;
1674 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1677 ASSERT3P(drc
->drc_ds
->ds_owner
, ==, dmu_recv_tag
);
1679 if (!drc
->drc_newfs
) {
1680 dsl_dataset_t
*origin_head
;
1682 error
= dsl_dataset_hold(dp
, drc
->drc_tofs
, FTAG
, &origin_head
);
1685 if (drc
->drc_force
) {
1687 * We will destroy any snapshots in tofs (i.e. before
1688 * origin_head) that are after the origin (which is
1689 * the snap before drc_ds, because drc_ds can not
1690 * have any snaps of its own).
1692 uint64_t obj
= origin_head
->ds_phys
->ds_prev_snap_obj
;
1693 while (obj
!= drc
->drc_ds
->ds_phys
->ds_prev_snap_obj
) {
1694 dsl_dataset_t
*snap
;
1695 error
= dsl_dataset_hold_obj(dp
, obj
, FTAG
,
1699 if (snap
->ds_dir
!= origin_head
->ds_dir
)
1700 error
= SET_ERROR(EINVAL
);
1702 error
= dsl_destroy_snapshot_check_impl(
1705 obj
= snap
->ds_phys
->ds_prev_snap_obj
;
1706 dsl_dataset_rele(snap
, FTAG
);
1711 error
= dsl_dataset_clone_swap_check_impl(drc
->drc_ds
,
1712 origin_head
, drc
->drc_force
, drc
->drc_owner
, tx
);
1714 dsl_dataset_rele(origin_head
, FTAG
);
1717 error
= dsl_dataset_snapshot_check_impl(origin_head
,
1718 drc
->drc_tosnap
, tx
, B_TRUE
);
1719 dsl_dataset_rele(origin_head
, FTAG
);
1723 error
= dsl_destroy_head_check_impl(drc
->drc_ds
, 1);
1725 error
= dsl_dataset_snapshot_check_impl(drc
->drc_ds
,
1726 drc
->drc_tosnap
, tx
, B_TRUE
);
1732 dmu_recv_end_sync(void *arg
, dmu_tx_t
*tx
)
1734 dmu_recv_cookie_t
*drc
= arg
;
1735 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1737 spa_history_log_internal_ds(drc
->drc_ds
, "finish receiving",
1738 tx
, "snap=%s", drc
->drc_tosnap
);
1740 if (!drc
->drc_newfs
) {
1741 dsl_dataset_t
*origin_head
;
1743 VERIFY0(dsl_dataset_hold(dp
, drc
->drc_tofs
, FTAG
,
1746 if (drc
->drc_force
) {
1748 * Destroy any snapshots of drc_tofs (origin_head)
1749 * after the origin (the snap before drc_ds).
1751 uint64_t obj
= origin_head
->ds_phys
->ds_prev_snap_obj
;
1752 while (obj
!= drc
->drc_ds
->ds_phys
->ds_prev_snap_obj
) {
1753 dsl_dataset_t
*snap
;
1754 VERIFY0(dsl_dataset_hold_obj(dp
, obj
, FTAG
,
1756 ASSERT3P(snap
->ds_dir
, ==, origin_head
->ds_dir
);
1757 obj
= snap
->ds_phys
->ds_prev_snap_obj
;
1758 dsl_destroy_snapshot_sync_impl(snap
,
1760 dsl_dataset_rele(snap
, FTAG
);
1763 VERIFY3P(drc
->drc_ds
->ds_prev
, ==,
1764 origin_head
->ds_prev
);
1766 dsl_dataset_clone_swap_sync_impl(drc
->drc_ds
,
1768 dsl_dataset_snapshot_sync_impl(origin_head
,
1769 drc
->drc_tosnap
, tx
);
1771 /* set snapshot's creation time and guid */
1772 dmu_buf_will_dirty(origin_head
->ds_prev
->ds_dbuf
, tx
);
1773 origin_head
->ds_prev
->ds_phys
->ds_creation_time
=
1774 drc
->drc_drrb
->drr_creation_time
;
1775 origin_head
->ds_prev
->ds_phys
->ds_guid
=
1776 drc
->drc_drrb
->drr_toguid
;
1777 origin_head
->ds_prev
->ds_phys
->ds_flags
&=
1778 ~DS_FLAG_INCONSISTENT
;
1780 dmu_buf_will_dirty(origin_head
->ds_dbuf
, tx
);
1781 origin_head
->ds_phys
->ds_flags
&= ~DS_FLAG_INCONSISTENT
;
1783 dsl_dataset_rele(origin_head
, FTAG
);
1784 dsl_destroy_head_sync_impl(drc
->drc_ds
, tx
);
1786 if (drc
->drc_owner
!= NULL
)
1787 VERIFY3P(origin_head
->ds_owner
, ==, drc
->drc_owner
);
1789 dsl_dataset_t
*ds
= drc
->drc_ds
;
1791 dsl_dataset_snapshot_sync_impl(ds
, drc
->drc_tosnap
, tx
);
1793 /* set snapshot's creation time and guid */
1794 dmu_buf_will_dirty(ds
->ds_prev
->ds_dbuf
, tx
);
1795 ds
->ds_prev
->ds_phys
->ds_creation_time
=
1796 drc
->drc_drrb
->drr_creation_time
;
1797 ds
->ds_prev
->ds_phys
->ds_guid
= drc
->drc_drrb
->drr_toguid
;
1798 ds
->ds_prev
->ds_phys
->ds_flags
&= ~DS_FLAG_INCONSISTENT
;
1800 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
1801 ds
->ds_phys
->ds_flags
&= ~DS_FLAG_INCONSISTENT
;
1803 drc
->drc_newsnapobj
= drc
->drc_ds
->ds_phys
->ds_prev_snap_obj
;
1805 * Release the hold from dmu_recv_begin. This must be done before
1806 * we return to open context, so that when we free the dataset's dnode,
1807 * we can evict its bonus buffer.
1809 dsl_dataset_disown(drc
->drc_ds
, dmu_recv_tag
);
1814 add_ds_to_guidmap(const char *name
, avl_tree_t
*guid_map
, uint64_t snapobj
)
1817 dsl_dataset_t
*snapds
;
1818 guid_map_entry_t
*gmep
;
1821 ASSERT(guid_map
!= NULL
);
1823 err
= dsl_pool_hold(name
, FTAG
, &dp
);
1826 gmep
= kmem_alloc(sizeof (*gmep
), KM_SLEEP
);
1827 err
= dsl_dataset_hold_obj(dp
, snapobj
, gmep
, &snapds
);
1829 gmep
->guid
= snapds
->ds_phys
->ds_guid
;
1830 gmep
->gme_ds
= snapds
;
1831 avl_add(guid_map
, gmep
);
1832 dsl_dataset_long_hold(snapds
, gmep
);
1834 kmem_free(gmep
, sizeof (*gmep
));
1837 dsl_pool_rele(dp
, FTAG
);
1841 static int dmu_recv_end_modified_blocks
= 3;
1844 dmu_recv_existing_end(dmu_recv_cookie_t
*drc
)
1852 * We will be destroying the ds; make sure its origin is unmounted if
1855 name
= kmem_alloc(MAXNAMELEN
, KM_SLEEP
);
1856 dsl_dataset_name(drc
->drc_ds
, name
);
1857 zfs_destroy_unmount_origin(name
);
1858 kmem_free(name
, MAXNAMELEN
);
1861 error
= dsl_sync_task(drc
->drc_tofs
,
1862 dmu_recv_end_check
, dmu_recv_end_sync
, drc
,
1863 dmu_recv_end_modified_blocks
);
1866 dmu_recv_cleanup_ds(drc
);
1871 dmu_recv_new_end(dmu_recv_cookie_t
*drc
)
1875 error
= dsl_sync_task(drc
->drc_tofs
,
1876 dmu_recv_end_check
, dmu_recv_end_sync
, drc
,
1877 dmu_recv_end_modified_blocks
);
1880 dmu_recv_cleanup_ds(drc
);
1881 } else if (drc
->drc_guid_to_ds_map
!= NULL
) {
1882 (void) add_ds_to_guidmap(drc
->drc_tofs
,
1883 drc
->drc_guid_to_ds_map
,
1884 drc
->drc_newsnapobj
);
1890 dmu_recv_end(dmu_recv_cookie_t
*drc
, void *owner
)
1892 drc
->drc_owner
= owner
;
1895 return (dmu_recv_new_end(drc
));
1897 return (dmu_recv_existing_end(drc
));
1901 * Return TRUE if this objset is currently being received into.
1904 dmu_objset_is_receiving(objset_t
*os
)
1906 return (os
->os_dsl_dataset
!= NULL
&&
1907 os
->os_dsl_dataset
->ds_owner
== dmu_recv_tag
);
1910 #if defined(_KERNEL)
1911 module_param(zfs_send_corrupt_data
, int, 0644);
1912 MODULE_PARM_DESC(zfs_send_corrupt_data
, "Allow sending corrupt data");