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 2011 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2013 by Delphix. All rights reserved.
25 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
29 #include <sys/dmu_impl.h>
30 #include <sys/dmu_tx.h>
32 #include <sys/dnode.h>
33 #include <sys/zfs_context.h>
34 #include <sys/dmu_objset.h>
35 #include <sys/dmu_traverse.h>
36 #include <sys/dsl_dataset.h>
37 #include <sys/dsl_dir.h>
38 #include <sys/dsl_prop.h>
39 #include <sys/dsl_pool.h>
40 #include <sys/dsl_synctask.h>
41 #include <sys/zfs_ioctl.h>
43 #include <sys/zio_checksum.h>
44 #include <sys/zfs_znode.h>
45 #include <zfs_fletcher.h>
48 #include <sys/zfs_onexit.h>
49 #include <sys/dmu_send.h>
50 #include <sys/dsl_destroy.h>
52 /* Set this tunable to TRUE to replace corrupt data with 0x2f5baddb10c */
53 int zfs_send_corrupt_data
= B_FALSE
;
55 static char *dmu_recv_tag
= "dmu_recv_tag";
56 static const char *recv_clone_name
= "%recv";
59 dump_bytes(dmu_sendarg_t
*dsp
, void *buf
, int len
)
61 dsl_dataset_t
*ds
= dsp
->dsa_os
->os_dsl_dataset
;
62 ssize_t resid
; /* have to get resid to get detailed errno */
65 fletcher_4_incremental_native(buf
, len
, &dsp
->dsa_zc
);
66 dsp
->dsa_err
= vn_rdwr(UIO_WRITE
, dsp
->dsa_vp
,
68 0, UIO_SYSSPACE
, FAPPEND
, RLIM64_INFINITY
, CRED(), &resid
);
70 mutex_enter(&ds
->ds_sendstream_lock
);
72 mutex_exit(&ds
->ds_sendstream_lock
);
74 return (dsp
->dsa_err
);
78 dump_free(dmu_sendarg_t
*dsp
, uint64_t object
, uint64_t offset
,
81 struct drr_free
*drrf
= &(dsp
->dsa_drr
->drr_u
.drr_free
);
83 if (length
!= -1ULL && offset
+ length
< offset
)
87 * If there is a pending op, but it's not PENDING_FREE, push it out,
88 * since free block aggregation can only be done for blocks of the
89 * same type (i.e., DRR_FREE records can only be aggregated with
90 * other DRR_FREE records. DRR_FREEOBJECTS records can only be
91 * aggregated with other DRR_FREEOBJECTS records.
93 if (dsp
->dsa_pending_op
!= PENDING_NONE
&&
94 dsp
->dsa_pending_op
!= PENDING_FREE
) {
95 if (dump_bytes(dsp
, dsp
->dsa_drr
,
96 sizeof (dmu_replay_record_t
)) != 0)
97 return (SET_ERROR(EINTR
));
98 dsp
->dsa_pending_op
= PENDING_NONE
;
101 if (dsp
->dsa_pending_op
== PENDING_FREE
) {
103 * There should never be a PENDING_FREE if length is -1
104 * (because dump_dnode is the only place where this
105 * function is called with a -1, and only after flushing
106 * any pending record).
108 ASSERT(length
!= -1ULL);
110 * Check to see whether this free block can be aggregated
113 if (drrf
->drr_object
== object
&& drrf
->drr_offset
+
114 drrf
->drr_length
== offset
) {
115 drrf
->drr_length
+= length
;
118 /* not a continuation. Push out pending record */
119 if (dump_bytes(dsp
, dsp
->dsa_drr
,
120 sizeof (dmu_replay_record_t
)) != 0)
121 return (SET_ERROR(EINTR
));
122 dsp
->dsa_pending_op
= PENDING_NONE
;
125 /* create a FREE record and make it pending */
126 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
127 dsp
->dsa_drr
->drr_type
= DRR_FREE
;
128 drrf
->drr_object
= object
;
129 drrf
->drr_offset
= offset
;
130 drrf
->drr_length
= length
;
131 drrf
->drr_toguid
= dsp
->dsa_toguid
;
132 if (length
== -1ULL) {
133 if (dump_bytes(dsp
, dsp
->dsa_drr
,
134 sizeof (dmu_replay_record_t
)) != 0)
135 return (SET_ERROR(EINTR
));
137 dsp
->dsa_pending_op
= PENDING_FREE
;
144 dump_data(dmu_sendarg_t
*dsp
, dmu_object_type_t type
,
145 uint64_t object
, uint64_t offset
, int blksz
, const blkptr_t
*bp
, void *data
)
147 struct drr_write
*drrw
= &(dsp
->dsa_drr
->drr_u
.drr_write
);
151 * If there is any kind of pending aggregation (currently either
152 * a grouping of free objects or free blocks), push it out to
153 * the stream, since aggregation can't be done across operations
154 * of different types.
156 if (dsp
->dsa_pending_op
!= PENDING_NONE
) {
157 if (dump_bytes(dsp
, dsp
->dsa_drr
,
158 sizeof (dmu_replay_record_t
)) != 0)
159 return (SET_ERROR(EINTR
));
160 dsp
->dsa_pending_op
= PENDING_NONE
;
162 /* write a DATA record */
163 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
164 dsp
->dsa_drr
->drr_type
= DRR_WRITE
;
165 drrw
->drr_object
= object
;
166 drrw
->drr_type
= type
;
167 drrw
->drr_offset
= offset
;
168 drrw
->drr_length
= blksz
;
169 drrw
->drr_toguid
= dsp
->dsa_toguid
;
170 drrw
->drr_checksumtype
= BP_GET_CHECKSUM(bp
);
171 if (zio_checksum_table
[drrw
->drr_checksumtype
].ci_dedup
)
172 drrw
->drr_checksumflags
|= DRR_CHECKSUM_DEDUP
;
173 DDK_SET_LSIZE(&drrw
->drr_key
, BP_GET_LSIZE(bp
));
174 DDK_SET_PSIZE(&drrw
->drr_key
, BP_GET_PSIZE(bp
));
175 DDK_SET_COMPRESS(&drrw
->drr_key
, BP_GET_COMPRESS(bp
));
176 drrw
->drr_key
.ddk_cksum
= bp
->blk_cksum
;
178 if (dump_bytes(dsp
, dsp
->dsa_drr
, sizeof (dmu_replay_record_t
)) != 0)
179 return (SET_ERROR(EINTR
));
180 if (dump_bytes(dsp
, data
, blksz
) != 0)
181 return (SET_ERROR(EINTR
));
186 dump_spill(dmu_sendarg_t
*dsp
, uint64_t object
, int blksz
, void *data
)
188 struct drr_spill
*drrs
= &(dsp
->dsa_drr
->drr_u
.drr_spill
);
190 if (dsp
->dsa_pending_op
!= PENDING_NONE
) {
191 if (dump_bytes(dsp
, dsp
->dsa_drr
,
192 sizeof (dmu_replay_record_t
)) != 0)
193 return (SET_ERROR(EINTR
));
194 dsp
->dsa_pending_op
= PENDING_NONE
;
197 /* write a SPILL record */
198 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
199 dsp
->dsa_drr
->drr_type
= DRR_SPILL
;
200 drrs
->drr_object
= object
;
201 drrs
->drr_length
= blksz
;
202 drrs
->drr_toguid
= dsp
->dsa_toguid
;
204 if (dump_bytes(dsp
, dsp
->dsa_drr
, sizeof (dmu_replay_record_t
)))
205 return (SET_ERROR(EINTR
));
206 if (dump_bytes(dsp
, data
, blksz
))
207 return (SET_ERROR(EINTR
));
212 dump_freeobjects(dmu_sendarg_t
*dsp
, uint64_t firstobj
, uint64_t numobjs
)
214 struct drr_freeobjects
*drrfo
= &(dsp
->dsa_drr
->drr_u
.drr_freeobjects
);
217 * If there is a pending op, but it's not PENDING_FREEOBJECTS,
218 * push it out, since free block aggregation can only be done for
219 * blocks of the same type (i.e., DRR_FREE records can only be
220 * aggregated with other DRR_FREE records. DRR_FREEOBJECTS records
221 * can only be aggregated with other DRR_FREEOBJECTS records.
223 if (dsp
->dsa_pending_op
!= PENDING_NONE
&&
224 dsp
->dsa_pending_op
!= PENDING_FREEOBJECTS
) {
225 if (dump_bytes(dsp
, dsp
->dsa_drr
,
226 sizeof (dmu_replay_record_t
)) != 0)
227 return (SET_ERROR(EINTR
));
228 dsp
->dsa_pending_op
= PENDING_NONE
;
230 if (dsp
->dsa_pending_op
== PENDING_FREEOBJECTS
) {
232 * See whether this free object array can be aggregated
235 if (drrfo
->drr_firstobj
+ drrfo
->drr_numobjs
== firstobj
) {
236 drrfo
->drr_numobjs
+= numobjs
;
239 /* can't be aggregated. Push out pending record */
240 if (dump_bytes(dsp
, dsp
->dsa_drr
,
241 sizeof (dmu_replay_record_t
)) != 0)
242 return (SET_ERROR(EINTR
));
243 dsp
->dsa_pending_op
= PENDING_NONE
;
247 /* write a FREEOBJECTS record */
248 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
249 dsp
->dsa_drr
->drr_type
= DRR_FREEOBJECTS
;
250 drrfo
->drr_firstobj
= firstobj
;
251 drrfo
->drr_numobjs
= numobjs
;
252 drrfo
->drr_toguid
= dsp
->dsa_toguid
;
254 dsp
->dsa_pending_op
= PENDING_FREEOBJECTS
;
260 dump_dnode(dmu_sendarg_t
*dsp
, uint64_t object
, dnode_phys_t
*dnp
)
262 struct drr_object
*drro
= &(dsp
->dsa_drr
->drr_u
.drr_object
);
264 if (dnp
== NULL
|| dnp
->dn_type
== DMU_OT_NONE
)
265 return (dump_freeobjects(dsp
, object
, 1));
267 if (dsp
->dsa_pending_op
!= PENDING_NONE
) {
268 if (dump_bytes(dsp
, dsp
->dsa_drr
,
269 sizeof (dmu_replay_record_t
)) != 0)
270 return (SET_ERROR(EINTR
));
271 dsp
->dsa_pending_op
= PENDING_NONE
;
274 /* write an OBJECT record */
275 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
276 dsp
->dsa_drr
->drr_type
= DRR_OBJECT
;
277 drro
->drr_object
= object
;
278 drro
->drr_type
= dnp
->dn_type
;
279 drro
->drr_bonustype
= dnp
->dn_bonustype
;
280 drro
->drr_blksz
= dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
;
281 drro
->drr_bonuslen
= dnp
->dn_bonuslen
;
282 drro
->drr_checksumtype
= dnp
->dn_checksum
;
283 drro
->drr_compress
= dnp
->dn_compress
;
284 drro
->drr_toguid
= dsp
->dsa_toguid
;
286 if (dump_bytes(dsp
, dsp
->dsa_drr
, sizeof (dmu_replay_record_t
)) != 0)
287 return (SET_ERROR(EINTR
));
289 if (dump_bytes(dsp
, DN_BONUS(dnp
), P2ROUNDUP(dnp
->dn_bonuslen
, 8)) != 0)
290 return (SET_ERROR(EINTR
));
292 /* free anything past the end of the file */
293 if (dump_free(dsp
, object
, (dnp
->dn_maxblkid
+ 1) *
294 (dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
), -1ULL))
295 return (SET_ERROR(EINTR
));
296 if (dsp
->dsa_err
!= 0)
297 return (SET_ERROR(EINTR
));
301 #define BP_SPAN(dnp, level) \
302 (((uint64_t)dnp->dn_datablkszsec) << (SPA_MINBLOCKSHIFT + \
303 (level) * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT)))
307 backup_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
308 const zbookmark_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
310 dmu_sendarg_t
*dsp
= arg
;
311 dmu_object_type_t type
= bp
? BP_GET_TYPE(bp
) : DMU_OT_NONE
;
314 if (issig(JUSTLOOKING
) && issig(FORREAL
))
315 return (SET_ERROR(EINTR
));
317 if (zb
->zb_object
!= DMU_META_DNODE_OBJECT
&&
318 DMU_OBJECT_IS_SPECIAL(zb
->zb_object
)) {
320 } else if (bp
== NULL
&& zb
->zb_object
== DMU_META_DNODE_OBJECT
) {
321 uint64_t span
= BP_SPAN(dnp
, zb
->zb_level
);
322 uint64_t dnobj
= (zb
->zb_blkid
* span
) >> DNODE_SHIFT
;
323 err
= dump_freeobjects(dsp
, dnobj
, span
>> DNODE_SHIFT
);
324 } else if (bp
== NULL
) {
325 uint64_t span
= BP_SPAN(dnp
, zb
->zb_level
);
326 err
= dump_free(dsp
, zb
->zb_object
, zb
->zb_blkid
* span
, span
);
327 } else if (zb
->zb_level
> 0 || type
== DMU_OT_OBJSET
) {
329 } else if (type
== DMU_OT_DNODE
) {
332 int blksz
= BP_GET_LSIZE(bp
);
333 uint32_t aflags
= ARC_WAIT
;
336 if (arc_read(NULL
, spa
, bp
, arc_getbuf_func
, &abuf
,
337 ZIO_PRIORITY_ASYNC_READ
, ZIO_FLAG_CANFAIL
,
339 return (SET_ERROR(EIO
));
342 for (i
= 0; i
< blksz
>> DNODE_SHIFT
; i
++) {
343 uint64_t dnobj
= (zb
->zb_blkid
<<
344 (DNODE_BLOCK_SHIFT
- DNODE_SHIFT
)) + i
;
345 err
= dump_dnode(dsp
, dnobj
, blk
+i
);
349 (void) arc_buf_remove_ref(abuf
, &abuf
);
350 } else if (type
== DMU_OT_SA
) {
351 uint32_t aflags
= ARC_WAIT
;
353 int blksz
= BP_GET_LSIZE(bp
);
355 if (arc_read(NULL
, spa
, bp
, arc_getbuf_func
, &abuf
,
356 ZIO_PRIORITY_ASYNC_READ
, ZIO_FLAG_CANFAIL
,
358 return (SET_ERROR(EIO
));
360 err
= dump_spill(dsp
, zb
->zb_object
, blksz
, abuf
->b_data
);
361 (void) arc_buf_remove_ref(abuf
, &abuf
);
362 } else { /* it's a level-0 block of a regular object */
363 uint32_t aflags
= ARC_WAIT
;
365 int blksz
= BP_GET_LSIZE(bp
);
367 if (arc_read(NULL
, spa
, bp
, arc_getbuf_func
, &abuf
,
368 ZIO_PRIORITY_ASYNC_READ
, ZIO_FLAG_CANFAIL
,
370 if (zfs_send_corrupt_data
) {
371 /* Send a block filled with 0x"zfs badd bloc" */
372 abuf
= arc_buf_alloc(spa
, blksz
, &abuf
,
375 for (ptr
= abuf
->b_data
;
376 (char *)ptr
< (char *)abuf
->b_data
+ blksz
;
378 *ptr
= 0x2f5baddb10c;
380 return (SET_ERROR(EIO
));
384 err
= dump_data(dsp
, type
, zb
->zb_object
, zb
->zb_blkid
* blksz
,
385 blksz
, bp
, abuf
->b_data
);
386 (void) arc_buf_remove_ref(abuf
, &abuf
);
389 ASSERT(err
== 0 || err
== EINTR
);
394 * Releases dp, ds, and fromds, using the specified tag.
397 dmu_send_impl(void *tag
, dsl_pool_t
*dp
, dsl_dataset_t
*ds
,
398 dsl_dataset_t
*fromds
, int outfd
, vnode_t
*vp
, offset_t
*off
)
401 dmu_replay_record_t
*drr
;
404 uint64_t fromtxg
= 0;
406 if (fromds
!= NULL
&& !dsl_dataset_is_before(ds
, fromds
)) {
407 dsl_dataset_rele(fromds
, tag
);
408 dsl_dataset_rele(ds
, tag
);
409 dsl_pool_rele(dp
, tag
);
410 return (SET_ERROR(EXDEV
));
413 err
= dmu_objset_from_ds(ds
, &os
);
416 dsl_dataset_rele(fromds
, tag
);
417 dsl_dataset_rele(ds
, tag
);
418 dsl_pool_rele(dp
, tag
);
422 drr
= kmem_zalloc(sizeof (dmu_replay_record_t
), KM_SLEEP
);
423 drr
->drr_type
= DRR_BEGIN
;
424 drr
->drr_u
.drr_begin
.drr_magic
= DMU_BACKUP_MAGIC
;
425 DMU_SET_STREAM_HDRTYPE(drr
->drr_u
.drr_begin
.drr_versioninfo
,
429 if (dmu_objset_type(os
) == DMU_OST_ZFS
) {
431 if (zfs_get_zplprop(os
, ZFS_PROP_VERSION
, &version
) != 0) {
432 kmem_free(drr
, sizeof (dmu_replay_record_t
));
434 dsl_dataset_rele(fromds
, tag
);
435 dsl_dataset_rele(ds
, tag
);
436 dsl_pool_rele(dp
, tag
);
437 return (SET_ERROR(EINVAL
));
439 if (version
>= ZPL_VERSION_SA
) {
440 DMU_SET_FEATUREFLAGS(
441 drr
->drr_u
.drr_begin
.drr_versioninfo
,
442 DMU_BACKUP_FEATURE_SA_SPILL
);
447 drr
->drr_u
.drr_begin
.drr_creation_time
=
448 ds
->ds_phys
->ds_creation_time
;
449 drr
->drr_u
.drr_begin
.drr_type
= dmu_objset_type(os
);
450 if (fromds
!= NULL
&& ds
->ds_dir
!= fromds
->ds_dir
)
451 drr
->drr_u
.drr_begin
.drr_flags
|= DRR_FLAG_CLONE
;
452 drr
->drr_u
.drr_begin
.drr_toguid
= ds
->ds_phys
->ds_guid
;
453 if (ds
->ds_phys
->ds_flags
& DS_FLAG_CI_DATASET
)
454 drr
->drr_u
.drr_begin
.drr_flags
|= DRR_FLAG_CI_DATA
;
457 drr
->drr_u
.drr_begin
.drr_fromguid
= fromds
->ds_phys
->ds_guid
;
458 dsl_dataset_name(ds
, drr
->drr_u
.drr_begin
.drr_toname
);
460 if (fromds
!= NULL
) {
461 fromtxg
= fromds
->ds_phys
->ds_creation_txg
;
462 dsl_dataset_rele(fromds
, tag
);
466 dsp
= kmem_zalloc(sizeof (dmu_sendarg_t
), KM_SLEEP
);
470 dsp
->dsa_outfd
= outfd
;
471 dsp
->dsa_proc
= curproc
;
474 dsp
->dsa_toguid
= ds
->ds_phys
->ds_guid
;
475 ZIO_SET_CHECKSUM(&dsp
->dsa_zc
, 0, 0, 0, 0);
476 dsp
->dsa_pending_op
= PENDING_NONE
;
478 mutex_enter(&ds
->ds_sendstream_lock
);
479 list_insert_head(&ds
->ds_sendstreams
, dsp
);
480 mutex_exit(&ds
->ds_sendstream_lock
);
482 dsl_dataset_long_hold(ds
, FTAG
);
483 dsl_pool_rele(dp
, tag
);
485 if (dump_bytes(dsp
, drr
, sizeof (dmu_replay_record_t
)) != 0) {
490 err
= traverse_dataset(ds
, fromtxg
, TRAVERSE_PRE
| TRAVERSE_PREFETCH
,
493 if (dsp
->dsa_pending_op
!= PENDING_NONE
)
494 if (dump_bytes(dsp
, drr
, sizeof (dmu_replay_record_t
)) != 0)
495 err
= SET_ERROR(EINTR
);
498 if (err
== EINTR
&& dsp
->dsa_err
!= 0)
503 bzero(drr
, sizeof (dmu_replay_record_t
));
504 drr
->drr_type
= DRR_END
;
505 drr
->drr_u
.drr_end
.drr_checksum
= dsp
->dsa_zc
;
506 drr
->drr_u
.drr_end
.drr_toguid
= dsp
->dsa_toguid
;
508 if (dump_bytes(dsp
, drr
, sizeof (dmu_replay_record_t
)) != 0) {
514 mutex_enter(&ds
->ds_sendstream_lock
);
515 list_remove(&ds
->ds_sendstreams
, dsp
);
516 mutex_exit(&ds
->ds_sendstream_lock
);
518 kmem_free(drr
, sizeof (dmu_replay_record_t
));
519 kmem_free(dsp
, sizeof (dmu_sendarg_t
));
521 dsl_dataset_long_rele(ds
, FTAG
);
522 dsl_dataset_rele(ds
, tag
);
528 dmu_send_obj(const char *pool
, uint64_t tosnap
, uint64_t fromsnap
,
529 int outfd
, vnode_t
*vp
, offset_t
*off
)
533 dsl_dataset_t
*fromds
= NULL
;
536 err
= dsl_pool_hold(pool
, FTAG
, &dp
);
540 err
= dsl_dataset_hold_obj(dp
, tosnap
, FTAG
, &ds
);
542 dsl_pool_rele(dp
, FTAG
);
547 err
= dsl_dataset_hold_obj(dp
, fromsnap
, FTAG
, &fromds
);
549 dsl_dataset_rele(ds
, FTAG
);
550 dsl_pool_rele(dp
, FTAG
);
555 return (dmu_send_impl(FTAG
, dp
, ds
, fromds
, outfd
, vp
, off
));
559 dmu_send(const char *tosnap
, const char *fromsnap
,
560 int outfd
, vnode_t
*vp
, offset_t
*off
)
564 dsl_dataset_t
*fromds
= NULL
;
567 if (strchr(tosnap
, '@') == NULL
)
568 return (SET_ERROR(EINVAL
));
569 if (fromsnap
!= NULL
&& strchr(fromsnap
, '@') == NULL
)
570 return (SET_ERROR(EINVAL
));
572 err
= dsl_pool_hold(tosnap
, FTAG
, &dp
);
576 err
= dsl_dataset_hold(dp
, tosnap
, FTAG
, &ds
);
578 dsl_pool_rele(dp
, FTAG
);
582 if (fromsnap
!= NULL
) {
583 err
= dsl_dataset_hold(dp
, fromsnap
, FTAG
, &fromds
);
585 dsl_dataset_rele(ds
, FTAG
);
586 dsl_pool_rele(dp
, FTAG
);
590 return (dmu_send_impl(FTAG
, dp
, ds
, fromds
, outfd
, vp
, off
));
594 dmu_send_estimate(dsl_dataset_t
*ds
, dsl_dataset_t
*fromds
, uint64_t *sizep
)
596 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
600 ASSERT(dsl_pool_config_held(dp
));
602 /* tosnap must be a snapshot */
603 if (!dsl_dataset_is_snapshot(ds
))
604 return (SET_ERROR(EINVAL
));
607 * fromsnap must be an earlier snapshot from the same fs as tosnap,
608 * or the origin's fs.
610 if (fromds
!= NULL
&& !dsl_dataset_is_before(ds
, fromds
))
611 return (SET_ERROR(EXDEV
));
613 /* Get uncompressed size estimate of changed data. */
614 if (fromds
== NULL
) {
615 size
= ds
->ds_phys
->ds_uncompressed_bytes
;
618 err
= dsl_dataset_space_written(fromds
, ds
,
619 &used
, &comp
, &size
);
625 * Assume that space (both on-disk and in-stream) is dominated by
626 * data. We will adjust for indirect blocks and the copies property,
627 * but ignore per-object space used (eg, dnodes and DRR_OBJECT records).
631 * Subtract out approximate space used by indirect blocks.
632 * Assume most space is used by data blocks (non-indirect, non-dnode).
633 * Assume all blocks are recordsize. Assume ditto blocks and
634 * internal fragmentation counter out compression.
636 * Therefore, space used by indirect blocks is sizeof(blkptr_t) per
637 * block, which we observe in practice.
640 err
= dsl_prop_get_int_ds(ds
, "recordsize", &recordsize
);
643 size
-= size
/ recordsize
* sizeof (blkptr_t
);
645 /* Add in the space for the record associated with each block. */
646 size
+= size
/ recordsize
* sizeof (dmu_replay_record_t
);
653 typedef struct dmu_recv_begin_arg
{
654 const char *drba_origin
;
655 dmu_recv_cookie_t
*drba_cookie
;
657 uint64_t drba_snapobj
;
658 } dmu_recv_begin_arg_t
;
661 recv_begin_check_existing_impl(dmu_recv_begin_arg_t
*drba
, dsl_dataset_t
*ds
,
666 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
668 /* temporary clone name must not exist */
669 error
= zap_lookup(dp
->dp_meta_objset
,
670 ds
->ds_dir
->dd_phys
->dd_child_dir_zapobj
, recv_clone_name
,
673 return (error
== 0 ? EBUSY
: error
);
675 /* new snapshot name must not exist */
676 error
= zap_lookup(dp
->dp_meta_objset
,
677 ds
->ds_phys
->ds_snapnames_zapobj
, drba
->drba_cookie
->drc_tosnap
,
680 return (error
== 0 ? EEXIST
: error
);
684 uint64_t obj
= ds
->ds_phys
->ds_prev_snap_obj
;
686 /* Find snapshot in this dir that matches fromguid. */
688 error
= dsl_dataset_hold_obj(dp
, obj
, FTAG
,
691 return (SET_ERROR(ENODEV
));
692 if (snap
->ds_dir
!= ds
->ds_dir
) {
693 dsl_dataset_rele(snap
, FTAG
);
694 return (SET_ERROR(ENODEV
));
696 if (snap
->ds_phys
->ds_guid
== fromguid
)
698 obj
= snap
->ds_phys
->ds_prev_snap_obj
;
699 dsl_dataset_rele(snap
, FTAG
);
702 return (SET_ERROR(ENODEV
));
704 if (drba
->drba_cookie
->drc_force
) {
705 drba
->drba_snapobj
= obj
;
708 * If we are not forcing, there must be no
709 * changes since fromsnap.
711 if (dsl_dataset_modified_since_snap(ds
, snap
)) {
712 dsl_dataset_rele(snap
, FTAG
);
713 return (SET_ERROR(ETXTBSY
));
715 drba
->drba_snapobj
= ds
->ds_prev
->ds_object
;
718 dsl_dataset_rele(snap
, FTAG
);
720 /* if full, most recent snapshot must be $ORIGIN */
721 if (ds
->ds_phys
->ds_prev_snap_txg
>= TXG_INITIAL
)
722 return (SET_ERROR(ENODEV
));
723 drba
->drba_snapobj
= ds
->ds_phys
->ds_prev_snap_obj
;
731 dmu_recv_begin_check(void *arg
, dmu_tx_t
*tx
)
733 dmu_recv_begin_arg_t
*drba
= arg
;
734 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
735 struct drr_begin
*drrb
= drba
->drba_cookie
->drc_drrb
;
736 uint64_t fromguid
= drrb
->drr_fromguid
;
737 int flags
= drrb
->drr_flags
;
740 const char *tofs
= drba
->drba_cookie
->drc_tofs
;
742 /* already checked */
743 ASSERT3U(drrb
->drr_magic
, ==, DMU_BACKUP_MAGIC
);
745 if (DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) ==
746 DMU_COMPOUNDSTREAM
||
747 drrb
->drr_type
>= DMU_OST_NUMTYPES
||
748 ((flags
& DRR_FLAG_CLONE
) && drba
->drba_origin
== NULL
))
749 return (SET_ERROR(EINVAL
));
751 /* Verify pool version supports SA if SA_SPILL feature set */
752 if ((DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
) &
753 DMU_BACKUP_FEATURE_SA_SPILL
) &&
754 spa_version(dp
->dp_spa
) < SPA_VERSION_SA
) {
755 return (SET_ERROR(ENOTSUP
));
758 error
= dsl_dataset_hold(dp
, tofs
, FTAG
, &ds
);
760 /* target fs already exists; recv into temp clone */
762 /* Can't recv a clone into an existing fs */
763 if (flags
& DRR_FLAG_CLONE
) {
764 dsl_dataset_rele(ds
, FTAG
);
765 return (SET_ERROR(EINVAL
));
768 error
= recv_begin_check_existing_impl(drba
, ds
, fromguid
);
769 dsl_dataset_rele(ds
, FTAG
);
770 } else if (error
== ENOENT
) {
771 /* target fs does not exist; must be a full backup or clone */
772 char buf
[MAXNAMELEN
];
775 * If it's a non-clone incremental, we are missing the
776 * target fs, so fail the recv.
778 if (fromguid
!= 0 && !(flags
& DRR_FLAG_CLONE
))
779 return (SET_ERROR(ENOENT
));
781 /* Open the parent of tofs */
782 ASSERT3U(strlen(tofs
), <, MAXNAMELEN
);
783 (void) strlcpy(buf
, tofs
, strrchr(tofs
, '/') - tofs
+ 1);
784 error
= dsl_dataset_hold(dp
, buf
, FTAG
, &ds
);
788 if (drba
->drba_origin
!= NULL
) {
789 dsl_dataset_t
*origin
;
790 error
= dsl_dataset_hold(dp
, drba
->drba_origin
,
793 dsl_dataset_rele(ds
, FTAG
);
796 if (!dsl_dataset_is_snapshot(origin
)) {
797 dsl_dataset_rele(origin
, FTAG
);
798 dsl_dataset_rele(ds
, FTAG
);
799 return (SET_ERROR(EINVAL
));
801 if (origin
->ds_phys
->ds_guid
!= fromguid
) {
802 dsl_dataset_rele(origin
, FTAG
);
803 dsl_dataset_rele(ds
, FTAG
);
804 return (SET_ERROR(ENODEV
));
806 dsl_dataset_rele(origin
, FTAG
);
808 dsl_dataset_rele(ds
, FTAG
);
815 dmu_recv_begin_sync(void *arg
, dmu_tx_t
*tx
)
817 dmu_recv_begin_arg_t
*drba
= arg
;
818 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
819 struct drr_begin
*drrb
= drba
->drba_cookie
->drc_drrb
;
820 const char *tofs
= drba
->drba_cookie
->drc_tofs
;
821 dsl_dataset_t
*ds
, *newds
;
826 crflags
= (drrb
->drr_flags
& DRR_FLAG_CI_DATA
) ?
827 DS_FLAG_CI_DATASET
: 0;
829 error
= dsl_dataset_hold(dp
, tofs
, FTAG
, &ds
);
831 /* create temporary clone */
832 dsl_dataset_t
*snap
= NULL
;
833 if (drba
->drba_snapobj
!= 0) {
834 VERIFY0(dsl_dataset_hold_obj(dp
,
835 drba
->drba_snapobj
, FTAG
, &snap
));
837 dsobj
= dsl_dataset_create_sync(ds
->ds_dir
, recv_clone_name
,
838 snap
, crflags
, drba
->drba_cred
, tx
);
839 dsl_dataset_rele(snap
, FTAG
);
840 dsl_dataset_rele(ds
, FTAG
);
844 dsl_dataset_t
*origin
= NULL
;
846 VERIFY0(dsl_dir_hold(dp
, tofs
, FTAG
, &dd
, &tail
));
848 if (drba
->drba_origin
!= NULL
) {
849 VERIFY0(dsl_dataset_hold(dp
, drba
->drba_origin
,
853 /* Create new dataset. */
854 dsobj
= dsl_dataset_create_sync(dd
,
855 strrchr(tofs
, '/') + 1,
856 origin
, crflags
, drba
->drba_cred
, tx
);
858 dsl_dataset_rele(origin
, FTAG
);
859 dsl_dir_rele(dd
, FTAG
);
860 drba
->drba_cookie
->drc_newfs
= B_TRUE
;
862 VERIFY0(dsl_dataset_own_obj(dp
, dsobj
, dmu_recv_tag
, &newds
));
864 dmu_buf_will_dirty(newds
->ds_dbuf
, tx
);
865 newds
->ds_phys
->ds_flags
|= DS_FLAG_INCONSISTENT
;
868 * If we actually created a non-clone, we need to create the
869 * objset in our new dataset.
871 if (BP_IS_HOLE(dsl_dataset_get_blkptr(newds
))) {
872 (void) dmu_objset_create_impl(dp
->dp_spa
,
873 newds
, dsl_dataset_get_blkptr(newds
), drrb
->drr_type
, tx
);
876 drba
->drba_cookie
->drc_ds
= newds
;
878 spa_history_log_internal_ds(newds
, "receive", tx
, "");
882 * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
883 * succeeds; otherwise we will leak the holds on the datasets.
886 dmu_recv_begin(char *tofs
, char *tosnap
, struct drr_begin
*drrb
,
887 boolean_t force
, char *origin
, dmu_recv_cookie_t
*drc
)
889 dmu_recv_begin_arg_t drba
= { 0 };
890 dmu_replay_record_t
*drr
;
892 bzero(drc
, sizeof (dmu_recv_cookie_t
));
893 drc
->drc_drrb
= drrb
;
894 drc
->drc_tosnap
= tosnap
;
895 drc
->drc_tofs
= tofs
;
896 drc
->drc_force
= force
;
898 if (drrb
->drr_magic
== BSWAP_64(DMU_BACKUP_MAGIC
))
899 drc
->drc_byteswap
= B_TRUE
;
900 else if (drrb
->drr_magic
!= DMU_BACKUP_MAGIC
)
901 return (SET_ERROR(EINVAL
));
903 drr
= kmem_zalloc(sizeof (dmu_replay_record_t
), KM_SLEEP
);
904 drr
->drr_type
= DRR_BEGIN
;
905 drr
->drr_u
.drr_begin
= *drc
->drc_drrb
;
906 if (drc
->drc_byteswap
) {
907 fletcher_4_incremental_byteswap(drr
,
908 sizeof (dmu_replay_record_t
), &drc
->drc_cksum
);
910 fletcher_4_incremental_native(drr
,
911 sizeof (dmu_replay_record_t
), &drc
->drc_cksum
);
913 kmem_free(drr
, sizeof (dmu_replay_record_t
));
915 if (drc
->drc_byteswap
) {
916 drrb
->drr_magic
= BSWAP_64(drrb
->drr_magic
);
917 drrb
->drr_versioninfo
= BSWAP_64(drrb
->drr_versioninfo
);
918 drrb
->drr_creation_time
= BSWAP_64(drrb
->drr_creation_time
);
919 drrb
->drr_type
= BSWAP_32(drrb
->drr_type
);
920 drrb
->drr_toguid
= BSWAP_64(drrb
->drr_toguid
);
921 drrb
->drr_fromguid
= BSWAP_64(drrb
->drr_fromguid
);
924 drba
.drba_origin
= origin
;
925 drba
.drba_cookie
= drc
;
926 drba
.drba_cred
= CRED();
928 return (dsl_sync_task(tofs
, dmu_recv_begin_check
, dmu_recv_begin_sync
,
938 int bufsize
; /* amount of memory allocated for buf */
940 avl_tree_t
*guid_to_ds_map
;
943 typedef struct guid_map_entry
{
945 dsl_dataset_t
*gme_ds
;
950 guid_compare(const void *arg1
, const void *arg2
)
952 const guid_map_entry_t
*gmep1
= arg1
;
953 const guid_map_entry_t
*gmep2
= arg2
;
955 if (gmep1
->guid
< gmep2
->guid
)
957 else if (gmep1
->guid
> gmep2
->guid
)
963 free_guid_map_onexit(void *arg
)
965 avl_tree_t
*ca
= arg
;
967 guid_map_entry_t
*gmep
;
969 while ((gmep
= avl_destroy_nodes(ca
, &cookie
)) != NULL
) {
970 dsl_dataset_long_rele(gmep
->gme_ds
, gmep
);
971 dsl_dataset_rele(gmep
->gme_ds
, gmep
);
972 kmem_free(gmep
, sizeof (guid_map_entry_t
));
975 kmem_free(ca
, sizeof (avl_tree_t
));
979 restore_read(struct restorearg
*ra
, int len
)
984 /* some things will require 8-byte alignment, so everything must */
990 ra
->err
= vn_rdwr(UIO_READ
, ra
->vp
,
991 (caddr_t
)ra
->buf
+ done
, len
- done
,
992 ra
->voff
, UIO_SYSSPACE
, FAPPEND
,
993 RLIM64_INFINITY
, CRED(), &resid
);
995 if (resid
== len
- done
)
996 ra
->err
= SET_ERROR(EINVAL
);
997 ra
->voff
+= len
- done
- resid
;
1003 ASSERT3U(done
, ==, len
);
1006 fletcher_4_incremental_byteswap(rv
, len
, &ra
->cksum
);
1008 fletcher_4_incremental_native(rv
, len
, &ra
->cksum
);
1013 backup_byteswap(dmu_replay_record_t
*drr
)
1015 #define DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
1016 #define DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
1017 drr
->drr_type
= BSWAP_32(drr
->drr_type
);
1018 drr
->drr_payloadlen
= BSWAP_32(drr
->drr_payloadlen
);
1019 switch (drr
->drr_type
) {
1021 DO64(drr_begin
.drr_magic
);
1022 DO64(drr_begin
.drr_versioninfo
);
1023 DO64(drr_begin
.drr_creation_time
);
1024 DO32(drr_begin
.drr_type
);
1025 DO32(drr_begin
.drr_flags
);
1026 DO64(drr_begin
.drr_toguid
);
1027 DO64(drr_begin
.drr_fromguid
);
1030 DO64(drr_object
.drr_object
);
1031 /* DO64(drr_object.drr_allocation_txg); */
1032 DO32(drr_object
.drr_type
);
1033 DO32(drr_object
.drr_bonustype
);
1034 DO32(drr_object
.drr_blksz
);
1035 DO32(drr_object
.drr_bonuslen
);
1036 DO64(drr_object
.drr_toguid
);
1038 case DRR_FREEOBJECTS
:
1039 DO64(drr_freeobjects
.drr_firstobj
);
1040 DO64(drr_freeobjects
.drr_numobjs
);
1041 DO64(drr_freeobjects
.drr_toguid
);
1044 DO64(drr_write
.drr_object
);
1045 DO32(drr_write
.drr_type
);
1046 DO64(drr_write
.drr_offset
);
1047 DO64(drr_write
.drr_length
);
1048 DO64(drr_write
.drr_toguid
);
1049 DO64(drr_write
.drr_key
.ddk_cksum
.zc_word
[0]);
1050 DO64(drr_write
.drr_key
.ddk_cksum
.zc_word
[1]);
1051 DO64(drr_write
.drr_key
.ddk_cksum
.zc_word
[2]);
1052 DO64(drr_write
.drr_key
.ddk_cksum
.zc_word
[3]);
1053 DO64(drr_write
.drr_key
.ddk_prop
);
1055 case DRR_WRITE_BYREF
:
1056 DO64(drr_write_byref
.drr_object
);
1057 DO64(drr_write_byref
.drr_offset
);
1058 DO64(drr_write_byref
.drr_length
);
1059 DO64(drr_write_byref
.drr_toguid
);
1060 DO64(drr_write_byref
.drr_refguid
);
1061 DO64(drr_write_byref
.drr_refobject
);
1062 DO64(drr_write_byref
.drr_refoffset
);
1063 DO64(drr_write_byref
.drr_key
.ddk_cksum
.zc_word
[0]);
1064 DO64(drr_write_byref
.drr_key
.ddk_cksum
.zc_word
[1]);
1065 DO64(drr_write_byref
.drr_key
.ddk_cksum
.zc_word
[2]);
1066 DO64(drr_write_byref
.drr_key
.ddk_cksum
.zc_word
[3]);
1067 DO64(drr_write_byref
.drr_key
.ddk_prop
);
1070 DO64(drr_free
.drr_object
);
1071 DO64(drr_free
.drr_offset
);
1072 DO64(drr_free
.drr_length
);
1073 DO64(drr_free
.drr_toguid
);
1076 DO64(drr_spill
.drr_object
);
1077 DO64(drr_spill
.drr_length
);
1078 DO64(drr_spill
.drr_toguid
);
1081 DO64(drr_end
.drr_checksum
.zc_word
[0]);
1082 DO64(drr_end
.drr_checksum
.zc_word
[1]);
1083 DO64(drr_end
.drr_checksum
.zc_word
[2]);
1084 DO64(drr_end
.drr_checksum
.zc_word
[3]);
1085 DO64(drr_end
.drr_toguid
);
1093 restore_object(struct restorearg
*ra
, objset_t
*os
, struct drr_object
*drro
)
1099 if (drro
->drr_type
== DMU_OT_NONE
||
1100 !DMU_OT_IS_VALID(drro
->drr_type
) ||
1101 !DMU_OT_IS_VALID(drro
->drr_bonustype
) ||
1102 drro
->drr_checksumtype
>= ZIO_CHECKSUM_FUNCTIONS
||
1103 drro
->drr_compress
>= ZIO_COMPRESS_FUNCTIONS
||
1104 P2PHASE(drro
->drr_blksz
, SPA_MINBLOCKSIZE
) ||
1105 drro
->drr_blksz
< SPA_MINBLOCKSIZE
||
1106 drro
->drr_blksz
> SPA_MAXBLOCKSIZE
||
1107 drro
->drr_bonuslen
> DN_MAX_BONUSLEN
) {
1108 return (SET_ERROR(EINVAL
));
1111 err
= dmu_object_info(os
, drro
->drr_object
, NULL
);
1113 if (err
!= 0 && err
!= ENOENT
)
1114 return (SET_ERROR(EINVAL
));
1116 if (drro
->drr_bonuslen
) {
1117 data
= restore_read(ra
, P2ROUNDUP(drro
->drr_bonuslen
, 8));
1122 if (err
== ENOENT
) {
1123 /* currently free, want to be allocated */
1124 tx
= dmu_tx_create(os
);
1125 dmu_tx_hold_bonus(tx
, DMU_NEW_OBJECT
);
1126 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1131 err
= dmu_object_claim(os
, drro
->drr_object
,
1132 drro
->drr_type
, drro
->drr_blksz
,
1133 drro
->drr_bonustype
, drro
->drr_bonuslen
, tx
);
1136 /* currently allocated, want to be allocated */
1137 err
= dmu_object_reclaim(os
, drro
->drr_object
,
1138 drro
->drr_type
, drro
->drr_blksz
,
1139 drro
->drr_bonustype
, drro
->drr_bonuslen
);
1142 return (SET_ERROR(EINVAL
));
1145 tx
= dmu_tx_create(os
);
1146 dmu_tx_hold_bonus(tx
, drro
->drr_object
);
1147 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1153 dmu_object_set_checksum(os
, drro
->drr_object
, drro
->drr_checksumtype
,
1155 dmu_object_set_compress(os
, drro
->drr_object
, drro
->drr_compress
, tx
);
1160 VERIFY(0 == dmu_bonus_hold(os
, drro
->drr_object
, FTAG
, &db
));
1161 dmu_buf_will_dirty(db
, tx
);
1163 ASSERT3U(db
->db_size
, >=, drro
->drr_bonuslen
);
1164 bcopy(data
, db
->db_data
, drro
->drr_bonuslen
);
1166 dmu_object_byteswap_t byteswap
=
1167 DMU_OT_BYTESWAP(drro
->drr_bonustype
);
1168 dmu_ot_byteswap
[byteswap
].ob_func(db
->db_data
,
1169 drro
->drr_bonuslen
);
1171 dmu_buf_rele(db
, FTAG
);
1179 restore_freeobjects(struct restorearg
*ra
, objset_t
*os
,
1180 struct drr_freeobjects
*drrfo
)
1184 if (drrfo
->drr_firstobj
+ drrfo
->drr_numobjs
< drrfo
->drr_firstobj
)
1185 return (SET_ERROR(EINVAL
));
1187 for (obj
= drrfo
->drr_firstobj
;
1188 obj
< drrfo
->drr_firstobj
+ drrfo
->drr_numobjs
;
1189 (void) dmu_object_next(os
, &obj
, FALSE
, 0)) {
1192 if (dmu_object_info(os
, obj
, NULL
) != 0)
1195 err
= dmu_free_object(os
, obj
);
1203 restore_write(struct restorearg
*ra
, objset_t
*os
,
1204 struct drr_write
*drrw
)
1210 if (drrw
->drr_offset
+ drrw
->drr_length
< drrw
->drr_offset
||
1211 !DMU_OT_IS_VALID(drrw
->drr_type
))
1212 return (SET_ERROR(EINVAL
));
1214 data
= restore_read(ra
, drrw
->drr_length
);
1218 if (dmu_object_info(os
, drrw
->drr_object
, NULL
) != 0)
1219 return (SET_ERROR(EINVAL
));
1221 tx
= dmu_tx_create(os
);
1223 dmu_tx_hold_write(tx
, drrw
->drr_object
,
1224 drrw
->drr_offset
, drrw
->drr_length
);
1225 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1231 dmu_object_byteswap_t byteswap
=
1232 DMU_OT_BYTESWAP(drrw
->drr_type
);
1233 dmu_ot_byteswap
[byteswap
].ob_func(data
, drrw
->drr_length
);
1235 dmu_write(os
, drrw
->drr_object
,
1236 drrw
->drr_offset
, drrw
->drr_length
, data
, tx
);
1242 * Handle a DRR_WRITE_BYREF record. This record is used in dedup'ed
1243 * streams to refer to a copy of the data that is already on the
1244 * system because it came in earlier in the stream. This function
1245 * finds the earlier copy of the data, and uses that copy instead of
1246 * data from the stream to fulfill this write.
1249 restore_write_byref(struct restorearg
*ra
, objset_t
*os
,
1250 struct drr_write_byref
*drrwbr
)
1254 guid_map_entry_t gmesrch
;
1255 guid_map_entry_t
*gmep
;
1257 objset_t
*ref_os
= NULL
;
1260 if (drrwbr
->drr_offset
+ drrwbr
->drr_length
< drrwbr
->drr_offset
)
1261 return (SET_ERROR(EINVAL
));
1264 * If the GUID of the referenced dataset is different from the
1265 * GUID of the target dataset, find the referenced dataset.
1267 if (drrwbr
->drr_toguid
!= drrwbr
->drr_refguid
) {
1268 gmesrch
.guid
= drrwbr
->drr_refguid
;
1269 if ((gmep
= avl_find(ra
->guid_to_ds_map
, &gmesrch
,
1271 return (SET_ERROR(EINVAL
));
1273 if (dmu_objset_from_ds(gmep
->gme_ds
, &ref_os
))
1274 return (SET_ERROR(EINVAL
));
1279 if (err
= dmu_buf_hold(ref_os
, drrwbr
->drr_refobject
,
1280 drrwbr
->drr_refoffset
, FTAG
, &dbp
, DMU_READ_PREFETCH
))
1283 tx
= dmu_tx_create(os
);
1285 dmu_tx_hold_write(tx
, drrwbr
->drr_object
,
1286 drrwbr
->drr_offset
, drrwbr
->drr_length
);
1287 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1292 dmu_write(os
, drrwbr
->drr_object
,
1293 drrwbr
->drr_offset
, drrwbr
->drr_length
, dbp
->db_data
, tx
);
1294 dmu_buf_rele(dbp
, FTAG
);
1300 restore_spill(struct restorearg
*ra
, objset_t
*os
, struct drr_spill
*drrs
)
1304 dmu_buf_t
*db
, *db_spill
;
1307 if (drrs
->drr_length
< SPA_MINBLOCKSIZE
||
1308 drrs
->drr_length
> SPA_MAXBLOCKSIZE
)
1309 return (SET_ERROR(EINVAL
));
1311 data
= restore_read(ra
, drrs
->drr_length
);
1315 if (dmu_object_info(os
, drrs
->drr_object
, NULL
) != 0)
1316 return (SET_ERROR(EINVAL
));
1318 VERIFY(0 == dmu_bonus_hold(os
, drrs
->drr_object
, FTAG
, &db
));
1319 if ((err
= dmu_spill_hold_by_bonus(db
, FTAG
, &db_spill
)) != 0) {
1320 dmu_buf_rele(db
, FTAG
);
1324 tx
= dmu_tx_create(os
);
1326 dmu_tx_hold_spill(tx
, db
->db_object
);
1328 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1330 dmu_buf_rele(db
, FTAG
);
1331 dmu_buf_rele(db_spill
, FTAG
);
1335 dmu_buf_will_dirty(db_spill
, tx
);
1337 if (db_spill
->db_size
< drrs
->drr_length
)
1338 VERIFY(0 == dbuf_spill_set_blksz(db_spill
,
1339 drrs
->drr_length
, tx
));
1340 bcopy(data
, db_spill
->db_data
, drrs
->drr_length
);
1342 dmu_buf_rele(db
, FTAG
);
1343 dmu_buf_rele(db_spill
, FTAG
);
1351 restore_free(struct restorearg
*ra
, objset_t
*os
,
1352 struct drr_free
*drrf
)
1356 if (drrf
->drr_length
!= -1ULL &&
1357 drrf
->drr_offset
+ drrf
->drr_length
< drrf
->drr_offset
)
1358 return (SET_ERROR(EINVAL
));
1360 if (dmu_object_info(os
, drrf
->drr_object
, NULL
) != 0)
1361 return (SET_ERROR(EINVAL
));
1363 err
= dmu_free_long_range(os
, drrf
->drr_object
,
1364 drrf
->drr_offset
, drrf
->drr_length
);
1368 /* used to destroy the drc_ds on error */
1370 dmu_recv_cleanup_ds(dmu_recv_cookie_t
*drc
)
1372 char name
[MAXNAMELEN
];
1373 dsl_dataset_name(drc
->drc_ds
, name
);
1374 dsl_dataset_disown(drc
->drc_ds
, dmu_recv_tag
);
1375 (void) dsl_destroy_head(name
);
1379 * NB: callers *must* call dmu_recv_end() if this succeeds.
1382 dmu_recv_stream(dmu_recv_cookie_t
*drc
, vnode_t
*vp
, offset_t
*voffp
,
1383 int cleanup_fd
, uint64_t *action_handlep
)
1385 struct restorearg ra
= { 0 };
1386 dmu_replay_record_t
*drr
;
1391 ra
.byteswap
= drc
->drc_byteswap
;
1392 ra
.cksum
= drc
->drc_cksum
;
1396 ra
.buf
= kmem_alloc(ra
.bufsize
, KM_SLEEP
);
1398 /* these were verified in dmu_recv_begin */
1399 ASSERT3U(DMU_GET_STREAM_HDRTYPE(drc
->drc_drrb
->drr_versioninfo
), ==,
1401 ASSERT3U(drc
->drc_drrb
->drr_type
, <, DMU_OST_NUMTYPES
);
1404 * Open the objset we are modifying.
1406 VERIFY0(dmu_objset_from_ds(drc
->drc_ds
, &os
));
1408 ASSERT(drc
->drc_ds
->ds_phys
->ds_flags
& DS_FLAG_INCONSISTENT
);
1410 featureflags
= DMU_GET_FEATUREFLAGS(drc
->drc_drrb
->drr_versioninfo
);
1412 /* if this stream is dedup'ed, set up the avl tree for guid mapping */
1413 if (featureflags
& DMU_BACKUP_FEATURE_DEDUP
) {
1416 if (cleanup_fd
== -1) {
1417 ra
.err
= SET_ERROR(EBADF
);
1420 ra
.err
= zfs_onexit_fd_hold(cleanup_fd
, &minor
);
1426 if (*action_handlep
== 0) {
1428 kmem_alloc(sizeof (avl_tree_t
), KM_SLEEP
);
1429 avl_create(ra
.guid_to_ds_map
, guid_compare
,
1430 sizeof (guid_map_entry_t
),
1431 offsetof(guid_map_entry_t
, avlnode
));
1432 ra
.err
= zfs_onexit_add_cb(minor
,
1433 free_guid_map_onexit
, ra
.guid_to_ds_map
,
1438 ra
.err
= zfs_onexit_cb_data(minor
, *action_handlep
,
1439 (void **)&ra
.guid_to_ds_map
);
1444 drc
->drc_guid_to_ds_map
= ra
.guid_to_ds_map
;
1448 * Read records and process them.
1451 while (ra
.err
== 0 &&
1452 NULL
!= (drr
= restore_read(&ra
, sizeof (*drr
)))) {
1453 if (issig(JUSTLOOKING
) && issig(FORREAL
)) {
1454 ra
.err
= SET_ERROR(EINTR
);
1459 backup_byteswap(drr
);
1461 switch (drr
->drr_type
) {
1465 * We need to make a copy of the record header,
1466 * because restore_{object,write} may need to
1467 * restore_read(), which will invalidate drr.
1469 struct drr_object drro
= drr
->drr_u
.drr_object
;
1470 ra
.err
= restore_object(&ra
, os
, &drro
);
1473 case DRR_FREEOBJECTS
:
1475 struct drr_freeobjects drrfo
=
1476 drr
->drr_u
.drr_freeobjects
;
1477 ra
.err
= restore_freeobjects(&ra
, os
, &drrfo
);
1482 struct drr_write drrw
= drr
->drr_u
.drr_write
;
1483 ra
.err
= restore_write(&ra
, os
, &drrw
);
1486 case DRR_WRITE_BYREF
:
1488 struct drr_write_byref drrwbr
=
1489 drr
->drr_u
.drr_write_byref
;
1490 ra
.err
= restore_write_byref(&ra
, os
, &drrwbr
);
1495 struct drr_free drrf
= drr
->drr_u
.drr_free
;
1496 ra
.err
= restore_free(&ra
, os
, &drrf
);
1501 struct drr_end drre
= drr
->drr_u
.drr_end
;
1503 * We compare against the *previous* checksum
1504 * value, because the stored checksum is of
1505 * everything before the DRR_END record.
1507 if (!ZIO_CHECKSUM_EQUAL(drre
.drr_checksum
, pcksum
))
1508 ra
.err
= SET_ERROR(ECKSUM
);
1513 struct drr_spill drrs
= drr
->drr_u
.drr_spill
;
1514 ra
.err
= restore_spill(&ra
, os
, &drrs
);
1518 ra
.err
= SET_ERROR(EINVAL
);
1523 ASSERT(ra
.err
!= 0);
1526 if ((featureflags
& DMU_BACKUP_FEATURE_DEDUP
) && (cleanup_fd
!= -1))
1527 zfs_onexit_fd_rele(cleanup_fd
);
1531 * destroy what we created, so we don't leave it in the
1532 * inconsistent restoring state.
1534 dmu_recv_cleanup_ds(drc
);
1537 kmem_free(ra
.buf
, ra
.bufsize
);
1543 dmu_recv_end_check(void *arg
, dmu_tx_t
*tx
)
1545 dmu_recv_cookie_t
*drc
= arg
;
1546 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1549 ASSERT3P(drc
->drc_ds
->ds_owner
, ==, dmu_recv_tag
);
1551 if (!drc
->drc_newfs
) {
1552 dsl_dataset_t
*origin_head
;
1554 error
= dsl_dataset_hold(dp
, drc
->drc_tofs
, FTAG
, &origin_head
);
1557 if (drc
->drc_force
) {
1559 * We will destroy any snapshots in tofs (i.e. before
1560 * origin_head) that are after the origin (which is
1561 * the snap before drc_ds, because drc_ds can not
1562 * have any snaps of its own).
1564 uint64_t obj
= origin_head
->ds_phys
->ds_prev_snap_obj
;
1565 while (obj
!= drc
->drc_ds
->ds_phys
->ds_prev_snap_obj
) {
1566 dsl_dataset_t
*snap
;
1567 error
= dsl_dataset_hold_obj(dp
, obj
, FTAG
,
1571 if (snap
->ds_dir
!= origin_head
->ds_dir
)
1572 error
= SET_ERROR(EINVAL
);
1574 error
= dsl_destroy_snapshot_check_impl(
1577 obj
= snap
->ds_phys
->ds_prev_snap_obj
;
1578 dsl_dataset_rele(snap
, FTAG
);
1583 error
= dsl_dataset_clone_swap_check_impl(drc
->drc_ds
,
1584 origin_head
, drc
->drc_force
, drc
->drc_owner
, tx
);
1586 dsl_dataset_rele(origin_head
, FTAG
);
1589 error
= dsl_dataset_snapshot_check_impl(origin_head
,
1590 drc
->drc_tosnap
, tx
, B_TRUE
);
1591 dsl_dataset_rele(origin_head
, FTAG
);
1595 error
= dsl_destroy_head_check_impl(drc
->drc_ds
, 1);
1597 error
= dsl_dataset_snapshot_check_impl(drc
->drc_ds
,
1598 drc
->drc_tosnap
, tx
, B_TRUE
);
1604 dmu_recv_end_sync(void *arg
, dmu_tx_t
*tx
)
1606 dmu_recv_cookie_t
*drc
= arg
;
1607 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1609 spa_history_log_internal_ds(drc
->drc_ds
, "finish receiving",
1610 tx
, "snap=%s", drc
->drc_tosnap
);
1612 if (!drc
->drc_newfs
) {
1613 dsl_dataset_t
*origin_head
;
1615 VERIFY0(dsl_dataset_hold(dp
, drc
->drc_tofs
, FTAG
,
1618 if (drc
->drc_force
) {
1620 * Destroy any snapshots of drc_tofs (origin_head)
1621 * after the origin (the snap before drc_ds).
1623 uint64_t obj
= origin_head
->ds_phys
->ds_prev_snap_obj
;
1624 while (obj
!= drc
->drc_ds
->ds_phys
->ds_prev_snap_obj
) {
1625 dsl_dataset_t
*snap
;
1626 VERIFY0(dsl_dataset_hold_obj(dp
, obj
, FTAG
,
1628 ASSERT3P(snap
->ds_dir
, ==, origin_head
->ds_dir
);
1629 obj
= snap
->ds_phys
->ds_prev_snap_obj
;
1630 dsl_destroy_snapshot_sync_impl(snap
,
1632 dsl_dataset_rele(snap
, FTAG
);
1635 VERIFY3P(drc
->drc_ds
->ds_prev
, ==,
1636 origin_head
->ds_prev
);
1638 dsl_dataset_clone_swap_sync_impl(drc
->drc_ds
,
1640 dsl_dataset_snapshot_sync_impl(origin_head
,
1641 drc
->drc_tosnap
, tx
);
1643 /* set snapshot's creation time and guid */
1644 dmu_buf_will_dirty(origin_head
->ds_prev
->ds_dbuf
, tx
);
1645 origin_head
->ds_prev
->ds_phys
->ds_creation_time
=
1646 drc
->drc_drrb
->drr_creation_time
;
1647 origin_head
->ds_prev
->ds_phys
->ds_guid
=
1648 drc
->drc_drrb
->drr_toguid
;
1649 origin_head
->ds_prev
->ds_phys
->ds_flags
&=
1650 ~DS_FLAG_INCONSISTENT
;
1652 dmu_buf_will_dirty(origin_head
->ds_dbuf
, tx
);
1653 origin_head
->ds_phys
->ds_flags
&= ~DS_FLAG_INCONSISTENT
;
1655 dsl_dataset_rele(origin_head
, FTAG
);
1656 dsl_destroy_head_sync_impl(drc
->drc_ds
, tx
);
1658 if (drc
->drc_owner
!= NULL
)
1659 VERIFY3P(origin_head
->ds_owner
, ==, drc
->drc_owner
);
1661 dsl_dataset_t
*ds
= drc
->drc_ds
;
1663 dsl_dataset_snapshot_sync_impl(ds
, drc
->drc_tosnap
, tx
);
1665 /* set snapshot's creation time and guid */
1666 dmu_buf_will_dirty(ds
->ds_prev
->ds_dbuf
, tx
);
1667 ds
->ds_prev
->ds_phys
->ds_creation_time
=
1668 drc
->drc_drrb
->drr_creation_time
;
1669 ds
->ds_prev
->ds_phys
->ds_guid
= drc
->drc_drrb
->drr_toguid
;
1670 ds
->ds_prev
->ds_phys
->ds_flags
&= ~DS_FLAG_INCONSISTENT
;
1672 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
1673 ds
->ds_phys
->ds_flags
&= ~DS_FLAG_INCONSISTENT
;
1675 drc
->drc_newsnapobj
= drc
->drc_ds
->ds_phys
->ds_prev_snap_obj
;
1677 * Release the hold from dmu_recv_begin. This must be done before
1678 * we return to open context, so that when we free the dataset's dnode,
1679 * we can evict its bonus buffer.
1681 dsl_dataset_disown(drc
->drc_ds
, dmu_recv_tag
);
1686 add_ds_to_guidmap(const char *name
, avl_tree_t
*guid_map
, uint64_t snapobj
)
1689 dsl_dataset_t
*snapds
;
1690 guid_map_entry_t
*gmep
;
1693 ASSERT(guid_map
!= NULL
);
1695 err
= dsl_pool_hold(name
, FTAG
, &dp
);
1698 gmep
= kmem_alloc(sizeof (*gmep
), KM_SLEEP
);
1699 err
= dsl_dataset_hold_obj(dp
, snapobj
, gmep
, &snapds
);
1701 gmep
->guid
= snapds
->ds_phys
->ds_guid
;
1702 gmep
->gme_ds
= snapds
;
1703 avl_add(guid_map
, gmep
);
1704 dsl_dataset_long_hold(snapds
, gmep
);
1706 kmem_free(gmep
, sizeof (*gmep
));
1709 dsl_pool_rele(dp
, FTAG
);
1713 static int dmu_recv_end_modified_blocks
= 3;
1716 dmu_recv_existing_end(dmu_recv_cookie_t
*drc
)
1719 char name
[MAXNAMELEN
];
1723 * We will be destroying the ds; make sure its origin is unmounted if
1726 dsl_dataset_name(drc
->drc_ds
, name
);
1727 zfs_destroy_unmount_origin(name
);
1730 error
= dsl_sync_task(drc
->drc_tofs
,
1731 dmu_recv_end_check
, dmu_recv_end_sync
, drc
,
1732 dmu_recv_end_modified_blocks
);
1735 dmu_recv_cleanup_ds(drc
);
1740 dmu_recv_new_end(dmu_recv_cookie_t
*drc
)
1744 error
= dsl_sync_task(drc
->drc_tofs
,
1745 dmu_recv_end_check
, dmu_recv_end_sync
, drc
,
1746 dmu_recv_end_modified_blocks
);
1749 dmu_recv_cleanup_ds(drc
);
1750 } else if (drc
->drc_guid_to_ds_map
!= NULL
) {
1751 (void) add_ds_to_guidmap(drc
->drc_tofs
,
1752 drc
->drc_guid_to_ds_map
,
1753 drc
->drc_newsnapobj
);
1759 dmu_recv_end(dmu_recv_cookie_t
*drc
, void *owner
)
1761 drc
->drc_owner
= owner
;
1764 return (dmu_recv_new_end(drc
));
1766 return (dmu_recv_existing_end(drc
));