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) 2011 by Delphix. 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>
50 /* Set this tunable to TRUE to replace corrupt data with 0x2f5baddb10c */
51 int zfs_send_corrupt_data
= B_FALSE
;
53 static char *dmu_recv_tag
= "dmu_recv_tag";
56 * The list of data whose inclusion in a send stream can be pending from
57 * one call to backup_cb to another. Multiple calls to dump_free() and
58 * dump_freeobjects() can be aggregated into a single DRR_FREE or
59 * DRR_FREEOBJECTS replay record.
68 dmu_replay_record_t
*drr
;
79 dump_bytes(struct backuparg
*ba
, void *buf
, int len
)
81 ssize_t resid
; /* have to get resid to get detailed errno */
82 ASSERT3U(len
% 8, ==, 0);
84 fletcher_4_incremental_native(buf
, len
, &ba
->zc
);
85 ba
->err
= vn_rdwr(UIO_WRITE
, ba
->vp
,
87 0, UIO_SYSSPACE
, FAPPEND
, RLIM64_INFINITY
, CRED(), &resid
);
93 dump_free(struct backuparg
*ba
, uint64_t object
, uint64_t offset
,
96 struct drr_free
*drrf
= &(ba
->drr
->drr_u
.drr_free
);
98 if (length
!= -1ULL && offset
+ length
< offset
)
102 * If there is a pending op, but it's not PENDING_FREE, push it out,
103 * since free block aggregation can only be done for blocks of the
104 * same type (i.e., DRR_FREE records can only be aggregated with
105 * other DRR_FREE records. DRR_FREEOBJECTS records can only be
106 * aggregated with other DRR_FREEOBJECTS records.
108 if (ba
->pending_op
!= PENDING_NONE
&& ba
->pending_op
!= PENDING_FREE
) {
109 if (dump_bytes(ba
, ba
->drr
, sizeof (dmu_replay_record_t
)) != 0)
111 ba
->pending_op
= PENDING_NONE
;
114 if (ba
->pending_op
== PENDING_FREE
) {
116 * There should never be a PENDING_FREE if length is -1
117 * (because dump_dnode is the only place where this
118 * function is called with a -1, and only after flushing
119 * any pending record).
121 ASSERT(length
!= -1ULL);
123 * Check to see whether this free block can be aggregated
126 if (drrf
->drr_object
== object
&& drrf
->drr_offset
+
127 drrf
->drr_length
== offset
) {
128 drrf
->drr_length
+= length
;
131 /* not a continuation. Push out pending record */
132 if (dump_bytes(ba
, ba
->drr
,
133 sizeof (dmu_replay_record_t
)) != 0)
135 ba
->pending_op
= PENDING_NONE
;
138 /* create a FREE record and make it pending */
139 bzero(ba
->drr
, sizeof (dmu_replay_record_t
));
140 ba
->drr
->drr_type
= DRR_FREE
;
141 drrf
->drr_object
= object
;
142 drrf
->drr_offset
= offset
;
143 drrf
->drr_length
= length
;
144 drrf
->drr_toguid
= ba
->toguid
;
145 if (length
== -1ULL) {
146 if (dump_bytes(ba
, ba
->drr
, sizeof (dmu_replay_record_t
)) != 0)
149 ba
->pending_op
= PENDING_FREE
;
156 dump_data(struct backuparg
*ba
, dmu_object_type_t type
,
157 uint64_t object
, uint64_t offset
, int blksz
, const blkptr_t
*bp
, void *data
)
159 struct drr_write
*drrw
= &(ba
->drr
->drr_u
.drr_write
);
163 * If there is any kind of pending aggregation (currently either
164 * a grouping of free objects or free blocks), push it out to
165 * the stream, since aggregation can't be done across operations
166 * of different types.
168 if (ba
->pending_op
!= PENDING_NONE
) {
169 if (dump_bytes(ba
, ba
->drr
, sizeof (dmu_replay_record_t
)) != 0)
171 ba
->pending_op
= PENDING_NONE
;
173 /* write a DATA record */
174 bzero(ba
->drr
, sizeof (dmu_replay_record_t
));
175 ba
->drr
->drr_type
= DRR_WRITE
;
176 drrw
->drr_object
= object
;
177 drrw
->drr_type
= type
;
178 drrw
->drr_offset
= offset
;
179 drrw
->drr_length
= blksz
;
180 drrw
->drr_toguid
= ba
->toguid
;
181 drrw
->drr_checksumtype
= BP_GET_CHECKSUM(bp
);
182 if (zio_checksum_table
[drrw
->drr_checksumtype
].ci_dedup
)
183 drrw
->drr_checksumflags
|= DRR_CHECKSUM_DEDUP
;
184 DDK_SET_LSIZE(&drrw
->drr_key
, BP_GET_LSIZE(bp
));
185 DDK_SET_PSIZE(&drrw
->drr_key
, BP_GET_PSIZE(bp
));
186 DDK_SET_COMPRESS(&drrw
->drr_key
, BP_GET_COMPRESS(bp
));
187 drrw
->drr_key
.ddk_cksum
= bp
->blk_cksum
;
189 if (dump_bytes(ba
, ba
->drr
, sizeof (dmu_replay_record_t
)) != 0)
191 if (dump_bytes(ba
, data
, blksz
) != 0)
197 dump_spill(struct backuparg
*ba
, uint64_t object
, int blksz
, void *data
)
199 struct drr_spill
*drrs
= &(ba
->drr
->drr_u
.drr_spill
);
201 if (ba
->pending_op
!= PENDING_NONE
) {
202 if (dump_bytes(ba
, ba
->drr
, sizeof (dmu_replay_record_t
)) != 0)
204 ba
->pending_op
= PENDING_NONE
;
207 /* write a SPILL record */
208 bzero(ba
->drr
, sizeof (dmu_replay_record_t
));
209 ba
->drr
->drr_type
= DRR_SPILL
;
210 drrs
->drr_object
= object
;
211 drrs
->drr_length
= blksz
;
212 drrs
->drr_toguid
= ba
->toguid
;
214 if (dump_bytes(ba
, ba
->drr
, sizeof (dmu_replay_record_t
)))
216 if (dump_bytes(ba
, data
, blksz
))
222 dump_freeobjects(struct backuparg
*ba
, uint64_t firstobj
, uint64_t numobjs
)
224 struct drr_freeobjects
*drrfo
= &(ba
->drr
->drr_u
.drr_freeobjects
);
227 * If there is a pending op, but it's not PENDING_FREEOBJECTS,
228 * push it out, since free block aggregation can only be done for
229 * blocks of the same type (i.e., DRR_FREE records can only be
230 * aggregated with other DRR_FREE records. DRR_FREEOBJECTS records
231 * can only be aggregated with other DRR_FREEOBJECTS records.
233 if (ba
->pending_op
!= PENDING_NONE
&&
234 ba
->pending_op
!= PENDING_FREEOBJECTS
) {
235 if (dump_bytes(ba
, ba
->drr
, sizeof (dmu_replay_record_t
)) != 0)
237 ba
->pending_op
= PENDING_NONE
;
239 if (ba
->pending_op
== PENDING_FREEOBJECTS
) {
241 * See whether this free object array can be aggregated
244 if (drrfo
->drr_firstobj
+ drrfo
->drr_numobjs
== firstobj
) {
245 drrfo
->drr_numobjs
+= numobjs
;
248 /* can't be aggregated. Push out pending record */
249 if (dump_bytes(ba
, ba
->drr
,
250 sizeof (dmu_replay_record_t
)) != 0)
252 ba
->pending_op
= PENDING_NONE
;
256 /* write a FREEOBJECTS record */
257 bzero(ba
->drr
, sizeof (dmu_replay_record_t
));
258 ba
->drr
->drr_type
= DRR_FREEOBJECTS
;
259 drrfo
->drr_firstobj
= firstobj
;
260 drrfo
->drr_numobjs
= numobjs
;
261 drrfo
->drr_toguid
= ba
->toguid
;
263 ba
->pending_op
= PENDING_FREEOBJECTS
;
269 dump_dnode(struct backuparg
*ba
, uint64_t object
, dnode_phys_t
*dnp
)
271 struct drr_object
*drro
= &(ba
->drr
->drr_u
.drr_object
);
273 if (dnp
== NULL
|| dnp
->dn_type
== DMU_OT_NONE
)
274 return (dump_freeobjects(ba
, object
, 1));
276 if (ba
->pending_op
!= PENDING_NONE
) {
277 if (dump_bytes(ba
, ba
->drr
, sizeof (dmu_replay_record_t
)) != 0)
279 ba
->pending_op
= PENDING_NONE
;
282 /* write an OBJECT record */
283 bzero(ba
->drr
, sizeof (dmu_replay_record_t
));
284 ba
->drr
->drr_type
= DRR_OBJECT
;
285 drro
->drr_object
= object
;
286 drro
->drr_type
= dnp
->dn_type
;
287 drro
->drr_bonustype
= dnp
->dn_bonustype
;
288 drro
->drr_blksz
= dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
;
289 drro
->drr_bonuslen
= dnp
->dn_bonuslen
;
290 drro
->drr_checksumtype
= dnp
->dn_checksum
;
291 drro
->drr_compress
= dnp
->dn_compress
;
292 drro
->drr_toguid
= ba
->toguid
;
294 if (dump_bytes(ba
, ba
->drr
, sizeof (dmu_replay_record_t
)) != 0)
297 if (dump_bytes(ba
, DN_BONUS(dnp
), P2ROUNDUP(dnp
->dn_bonuslen
, 8)) != 0)
300 /* free anything past the end of the file */
301 if (dump_free(ba
, object
, (dnp
->dn_maxblkid
+ 1) *
302 (dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
), -1ULL))
309 #define BP_SPAN(dnp, level) \
310 (((uint64_t)dnp->dn_datablkszsec) << (SPA_MINBLOCKSHIFT + \
311 (level) * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT)))
315 backup_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
, arc_buf_t
*pbuf
,
316 const zbookmark_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
318 struct backuparg
*ba
= arg
;
319 dmu_object_type_t type
= bp
? BP_GET_TYPE(bp
) : DMU_OT_NONE
;
322 if (issig(JUSTLOOKING
) && issig(FORREAL
))
325 if (zb
->zb_object
!= DMU_META_DNODE_OBJECT
&&
326 DMU_OBJECT_IS_SPECIAL(zb
->zb_object
)) {
328 } else if (bp
== NULL
&& zb
->zb_object
== DMU_META_DNODE_OBJECT
) {
329 uint64_t span
= BP_SPAN(dnp
, zb
->zb_level
);
330 uint64_t dnobj
= (zb
->zb_blkid
* span
) >> DNODE_SHIFT
;
331 err
= dump_freeobjects(ba
, dnobj
, span
>> DNODE_SHIFT
);
332 } else if (bp
== NULL
) {
333 uint64_t span
= BP_SPAN(dnp
, zb
->zb_level
);
334 err
= dump_free(ba
, zb
->zb_object
, zb
->zb_blkid
* span
, span
);
335 } else if (zb
->zb_level
> 0 || type
== DMU_OT_OBJSET
) {
337 } else if (type
== DMU_OT_DNODE
) {
340 int blksz
= BP_GET_LSIZE(bp
);
341 uint32_t aflags
= ARC_WAIT
;
344 if (dsl_read(NULL
, spa
, bp
, pbuf
,
345 arc_getbuf_func
, &abuf
, ZIO_PRIORITY_ASYNC_READ
,
346 ZIO_FLAG_CANFAIL
, &aflags
, zb
) != 0)
350 for (i
= 0; i
< blksz
>> DNODE_SHIFT
; i
++) {
351 uint64_t dnobj
= (zb
->zb_blkid
<<
352 (DNODE_BLOCK_SHIFT
- DNODE_SHIFT
)) + i
;
353 err
= dump_dnode(ba
, dnobj
, blk
+i
);
357 (void) arc_buf_remove_ref(abuf
, &abuf
);
358 } else if (type
== DMU_OT_SA
) {
359 uint32_t aflags
= ARC_WAIT
;
361 int blksz
= BP_GET_LSIZE(bp
);
363 if (arc_read_nolock(NULL
, spa
, bp
,
364 arc_getbuf_func
, &abuf
, ZIO_PRIORITY_ASYNC_READ
,
365 ZIO_FLAG_CANFAIL
, &aflags
, zb
) != 0)
368 err
= dump_spill(ba
, zb
->zb_object
, blksz
, abuf
->b_data
);
369 (void) arc_buf_remove_ref(abuf
, &abuf
);
370 } else { /* it's a level-0 block of a regular object */
371 uint32_t aflags
= ARC_WAIT
;
373 int blksz
= BP_GET_LSIZE(bp
);
375 if (dsl_read(NULL
, spa
, bp
, pbuf
,
376 arc_getbuf_func
, &abuf
, ZIO_PRIORITY_ASYNC_READ
,
377 ZIO_FLAG_CANFAIL
, &aflags
, zb
) != 0) {
378 if (zfs_send_corrupt_data
) {
380 /* Send a block filled with 0x"zfs badd bloc" */
381 abuf
= arc_buf_alloc(spa
, blksz
, &abuf
,
383 for (ptr
= abuf
->b_data
;
384 (char *)ptr
< (char *)abuf
->b_data
+ blksz
;
386 *ptr
= 0x2f5baddb10c;
392 err
= dump_data(ba
, type
, zb
->zb_object
, zb
->zb_blkid
* blksz
,
393 blksz
, bp
, abuf
->b_data
);
394 (void) arc_buf_remove_ref(abuf
, &abuf
);
397 ASSERT(err
== 0 || err
== EINTR
);
402 dmu_sendbackup(objset_t
*tosnap
, objset_t
*fromsnap
, boolean_t fromorigin
,
403 vnode_t
*vp
, offset_t
*off
)
405 dsl_dataset_t
*ds
= tosnap
->os_dsl_dataset
;
406 dsl_dataset_t
*fromds
= fromsnap
? fromsnap
->os_dsl_dataset
: NULL
;
407 dmu_replay_record_t
*drr
;
410 uint64_t fromtxg
= 0;
412 /* tosnap must be a snapshot */
413 if (ds
->ds_phys
->ds_next_snap_obj
== 0)
416 /* fromsnap must be an earlier snapshot from the same fs as tosnap */
417 if (fromds
&& (ds
->ds_dir
!= fromds
->ds_dir
||
418 fromds
->ds_phys
->ds_creation_txg
>= ds
->ds_phys
->ds_creation_txg
))
422 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
427 if (dsl_dir_is_clone(ds
->ds_dir
)) {
428 rw_enter(&dp
->dp_config_rwlock
, RW_READER
);
429 err
= dsl_dataset_hold_obj(dp
,
430 ds
->ds_dir
->dd_phys
->dd_origin_obj
, FTAG
, &fromds
);
431 rw_exit(&dp
->dp_config_rwlock
);
435 fromorigin
= B_FALSE
;
440 drr
= kmem_zalloc(sizeof (dmu_replay_record_t
), KM_SLEEP
);
441 drr
->drr_type
= DRR_BEGIN
;
442 drr
->drr_u
.drr_begin
.drr_magic
= DMU_BACKUP_MAGIC
;
443 DMU_SET_STREAM_HDRTYPE(drr
->drr_u
.drr_begin
.drr_versioninfo
,
447 if (dmu_objset_type(tosnap
) == DMU_OST_ZFS
) {
449 if (zfs_get_zplprop(tosnap
, ZFS_PROP_VERSION
, &version
) != 0)
451 if (version
== ZPL_VERSION_SA
) {
452 DMU_SET_FEATUREFLAGS(
453 drr
->drr_u
.drr_begin
.drr_versioninfo
,
454 DMU_BACKUP_FEATURE_SA_SPILL
);
459 drr
->drr_u
.drr_begin
.drr_creation_time
=
460 ds
->ds_phys
->ds_creation_time
;
461 drr
->drr_u
.drr_begin
.drr_type
= tosnap
->os_phys
->os_type
;
463 drr
->drr_u
.drr_begin
.drr_flags
|= DRR_FLAG_CLONE
;
464 drr
->drr_u
.drr_begin
.drr_toguid
= ds
->ds_phys
->ds_guid
;
465 if (ds
->ds_phys
->ds_flags
& DS_FLAG_CI_DATASET
)
466 drr
->drr_u
.drr_begin
.drr_flags
|= DRR_FLAG_CI_DATA
;
469 drr
->drr_u
.drr_begin
.drr_fromguid
= fromds
->ds_phys
->ds_guid
;
470 dsl_dataset_name(ds
, drr
->drr_u
.drr_begin
.drr_toname
);
473 fromtxg
= fromds
->ds_phys
->ds_creation_txg
;
475 dsl_dataset_rele(fromds
, FTAG
);
481 ba
.toguid
= ds
->ds_phys
->ds_guid
;
482 ZIO_SET_CHECKSUM(&ba
.zc
, 0, 0, 0, 0);
483 ba
.pending_op
= PENDING_NONE
;
485 if (dump_bytes(&ba
, drr
, sizeof (dmu_replay_record_t
)) != 0) {
486 kmem_free(drr
, sizeof (dmu_replay_record_t
));
490 err
= traverse_dataset(ds
, fromtxg
, TRAVERSE_PRE
| TRAVERSE_PREFETCH
,
493 if (ba
.pending_op
!= PENDING_NONE
)
494 if (dump_bytes(&ba
, drr
, sizeof (dmu_replay_record_t
)) != 0)
498 if (err
== EINTR
&& ba
.err
)
500 kmem_free(drr
, sizeof (dmu_replay_record_t
));
504 bzero(drr
, sizeof (dmu_replay_record_t
));
505 drr
->drr_type
= DRR_END
;
506 drr
->drr_u
.drr_end
.drr_checksum
= ba
.zc
;
507 drr
->drr_u
.drr_end
.drr_toguid
= ba
.toguid
;
509 if (dump_bytes(&ba
, drr
, sizeof (dmu_replay_record_t
)) != 0) {
510 kmem_free(drr
, sizeof (dmu_replay_record_t
));
514 kmem_free(drr
, sizeof (dmu_replay_record_t
));
520 dmu_send_estimate(objset_t
*tosnap
, objset_t
*fromsnap
, boolean_t fromorigin
,
523 dsl_dataset_t
*ds
= tosnap
->os_dsl_dataset
;
524 dsl_dataset_t
*fromds
= fromsnap
? fromsnap
->os_dsl_dataset
: NULL
;
525 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
527 uint64_t size
, recordsize
;
529 /* tosnap must be a snapshot */
530 if (ds
->ds_phys
->ds_next_snap_obj
== 0)
533 /* fromsnap must be an earlier snapshot from the same fs as tosnap */
534 if (fromds
&& (ds
->ds_dir
!= fromds
->ds_dir
||
535 fromds
->ds_phys
->ds_creation_txg
>= ds
->ds_phys
->ds_creation_txg
))
542 if (dsl_dir_is_clone(ds
->ds_dir
)) {
543 rw_enter(&dp
->dp_config_rwlock
, RW_READER
);
544 err
= dsl_dataset_hold_obj(dp
,
545 ds
->ds_dir
->dd_phys
->dd_origin_obj
, FTAG
, &fromds
);
546 rw_exit(&dp
->dp_config_rwlock
);
550 fromorigin
= B_FALSE
;
554 /* Get uncompressed size estimate of changed data. */
555 if (fromds
== NULL
) {
556 size
= ds
->ds_phys
->ds_uncompressed_bytes
;
559 err
= dsl_dataset_space_written(fromds
, ds
,
560 &used
, &comp
, &size
);
562 dsl_dataset_rele(fromds
, FTAG
);
568 * Assume that space (both on-disk and in-stream) is dominated by
569 * data. We will adjust for indirect blocks and the copies property,
570 * but ignore per-object space used (eg, dnodes and DRR_OBJECT records).
574 * Subtract out approximate space used by indirect blocks.
575 * Assume most space is used by data blocks (non-indirect, non-dnode).
576 * Assume all blocks are recordsize. Assume ditto blocks and
577 * internal fragmentation counter out compression.
579 * Therefore, space used by indirect blocks is sizeof(blkptr_t) per
580 * block, which we observe in practice.
582 rw_enter(&dp
->dp_config_rwlock
, RW_READER
);
583 err
= dsl_prop_get_ds(ds
, "recordsize",
584 sizeof (recordsize
), 1, &recordsize
, NULL
);
585 rw_exit(&dp
->dp_config_rwlock
);
588 size
-= size
/ recordsize
* sizeof (blkptr_t
);
590 /* Add in the space for the record associated with each block. */
591 size
+= size
/ recordsize
* sizeof (dmu_replay_record_t
);
598 struct recvbeginsyncarg
{
601 dsl_dataset_t
*origin
;
603 dmu_objset_type_t type
;
607 char clonelastname
[MAXNAMELEN
];
608 dsl_dataset_t
*ds
; /* the ds to recv into; returned from the syncfunc */
614 recv_new_check(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
616 dsl_dir_t
*dd
= arg1
;
617 struct recvbeginsyncarg
*rbsa
= arg2
;
618 objset_t
*mos
= dd
->dd_pool
->dp_meta_objset
;
622 err
= zap_lookup(mos
, dd
->dd_phys
->dd_child_dir_zapobj
,
623 strrchr(rbsa
->tofs
, '/') + 1, sizeof (uint64_t), 1, &val
);
626 return (err
? err
: EEXIST
);
629 /* make sure it's a snap in the same pool */
630 if (rbsa
->origin
->ds_dir
->dd_pool
!= dd
->dd_pool
)
632 if (!dsl_dataset_is_snapshot(rbsa
->origin
))
634 if (rbsa
->origin
->ds_phys
->ds_guid
!= rbsa
->fromguid
)
642 recv_new_sync(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
644 dsl_dir_t
*dd
= arg1
;
645 struct recvbeginsyncarg
*rbsa
= arg2
;
646 uint64_t flags
= DS_FLAG_INCONSISTENT
| rbsa
->dsflags
;
649 /* Create and open new dataset. */
650 dsobj
= dsl_dataset_create_sync(dd
, strrchr(rbsa
->tofs
, '/') + 1,
651 rbsa
->origin
, flags
, rbsa
->cr
, tx
);
652 VERIFY(0 == dsl_dataset_own_obj(dd
->dd_pool
, dsobj
,
653 B_TRUE
, dmu_recv_tag
, &rbsa
->ds
));
655 if (rbsa
->origin
== NULL
) {
656 (void) dmu_objset_create_impl(dd
->dd_pool
->dp_spa
,
657 rbsa
->ds
, &rbsa
->ds
->ds_phys
->ds_bp
, rbsa
->type
, tx
);
660 spa_history_log_internal(LOG_DS_REPLAY_FULL_SYNC
,
661 dd
->dd_pool
->dp_spa
, tx
, "dataset = %lld", dsobj
);
666 recv_existing_check(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
668 dsl_dataset_t
*ds
= arg1
;
669 struct recvbeginsyncarg
*rbsa
= arg2
;
673 /* must not have any changes since most recent snapshot */
674 if (!rbsa
->force
&& dsl_dataset_modified_since_lastsnap(ds
))
677 /* new snapshot name must not exist */
678 err
= zap_lookup(ds
->ds_dir
->dd_pool
->dp_meta_objset
,
679 ds
->ds_phys
->ds_snapnames_zapobj
, rbsa
->tosnap
, 8, 1, &val
);
685 if (rbsa
->fromguid
) {
686 /* if incremental, most recent snapshot must match fromguid */
687 if (ds
->ds_prev
== NULL
)
691 * most recent snapshot must match fromguid, or there are no
692 * changes since the fromguid one
694 if (ds
->ds_prev
->ds_phys
->ds_guid
!= rbsa
->fromguid
) {
695 uint64_t birth
= ds
->ds_prev
->ds_phys
->ds_bp
.blk_birth
;
696 uint64_t obj
= ds
->ds_prev
->ds_phys
->ds_prev_snap_obj
;
699 err
= dsl_dataset_hold_obj(ds
->ds_dir
->dd_pool
,
703 if (snap
->ds_phys
->ds_creation_txg
< birth
) {
704 dsl_dataset_rele(snap
, FTAG
);
707 if (snap
->ds_phys
->ds_guid
== rbsa
->fromguid
) {
708 dsl_dataset_rele(snap
, FTAG
);
711 obj
= snap
->ds_phys
->ds_prev_snap_obj
;
712 dsl_dataset_rele(snap
, FTAG
);
718 /* if full, most recent snapshot must be $ORIGIN */
719 if (ds
->ds_phys
->ds_prev_snap_txg
>= TXG_INITIAL
)
723 /* temporary clone name must not exist */
724 err
= zap_lookup(ds
->ds_dir
->dd_pool
->dp_meta_objset
,
725 ds
->ds_dir
->dd_phys
->dd_child_dir_zapobj
,
726 rbsa
->clonelastname
, 8, 1, &val
);
737 recv_existing_sync(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
739 dsl_dataset_t
*ohds
= arg1
;
740 struct recvbeginsyncarg
*rbsa
= arg2
;
741 dsl_pool_t
*dp
= ohds
->ds_dir
->dd_pool
;
743 uint64_t flags
= DS_FLAG_INCONSISTENT
| rbsa
->dsflags
;
746 /* create and open the temporary clone */
747 dsobj
= dsl_dataset_create_sync(ohds
->ds_dir
, rbsa
->clonelastname
,
748 ohds
->ds_prev
, flags
, rbsa
->cr
, tx
);
749 VERIFY(0 == dsl_dataset_own_obj(dp
, dsobj
, B_TRUE
, dmu_recv_tag
, &cds
));
752 * If we actually created a non-clone, we need to create the
753 * objset in our new dataset.
755 if (BP_IS_HOLE(dsl_dataset_get_blkptr(cds
))) {
756 (void) dmu_objset_create_impl(dp
->dp_spa
,
757 cds
, dsl_dataset_get_blkptr(cds
), rbsa
->type
, tx
);
762 spa_history_log_internal(LOG_DS_REPLAY_INC_SYNC
,
763 dp
->dp_spa
, tx
, "dataset = %lld", dsobj
);
767 dmu_recv_verify_features(dsl_dataset_t
*ds
, struct drr_begin
*drrb
)
771 featureflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
773 /* Verify pool version supports SA if SA_SPILL feature set */
774 return ((featureflags
& DMU_BACKUP_FEATURE_SA_SPILL
) &&
775 (spa_version(dsl_dataset_get_spa(ds
)) < SPA_VERSION_SA
));
779 * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
780 * succeeds; otherwise we will leak the holds on the datasets.
783 dmu_recv_begin(char *tofs
, char *tosnap
, char *top_ds
, struct drr_begin
*drrb
,
784 boolean_t force
, objset_t
*origin
, dmu_recv_cookie_t
*drc
)
788 struct recvbeginsyncarg rbsa
= { 0 };
789 uint64_t versioninfo
;
793 if (drrb
->drr_magic
== DMU_BACKUP_MAGIC
)
795 else if (drrb
->drr_magic
== BSWAP_64(DMU_BACKUP_MAGIC
))
801 rbsa
.tosnap
= tosnap
;
802 rbsa
.origin
= origin
? origin
->os_dsl_dataset
: NULL
;
803 rbsa
.fromguid
= drrb
->drr_fromguid
;
804 rbsa
.type
= drrb
->drr_type
;
808 versioninfo
= drrb
->drr_versioninfo
;
809 flags
= drrb
->drr_flags
;
812 rbsa
.type
= BSWAP_32(rbsa
.type
);
813 rbsa
.fromguid
= BSWAP_64(rbsa
.fromguid
);
814 versioninfo
= BSWAP_64(versioninfo
);
815 flags
= BSWAP_32(flags
);
818 if (DMU_GET_STREAM_HDRTYPE(versioninfo
) == DMU_COMPOUNDSTREAM
||
819 rbsa
.type
>= DMU_OST_NUMTYPES
||
820 ((flags
& DRR_FLAG_CLONE
) && origin
== NULL
))
823 if (flags
& DRR_FLAG_CI_DATA
)
824 rbsa
.dsflags
= DS_FLAG_CI_DATASET
;
826 bzero(drc
, sizeof (dmu_recv_cookie_t
));
827 drc
->drc_drrb
= drrb
;
828 drc
->drc_tosnap
= tosnap
;
829 drc
->drc_top_ds
= top_ds
;
830 drc
->drc_force
= force
;
833 * Process the begin in syncing context.
836 /* open the dataset we are logically receiving into */
837 err
= dsl_dataset_hold(tofs
, dmu_recv_tag
, &ds
);
839 if (dmu_recv_verify_features(ds
, drrb
)) {
840 dsl_dataset_rele(ds
, dmu_recv_tag
);
843 /* target fs already exists; recv into temp clone */
845 /* Can't recv a clone into an existing fs */
846 if (flags
& DRR_FLAG_CLONE
) {
847 dsl_dataset_rele(ds
, dmu_recv_tag
);
851 /* must not have an incremental recv already in progress */
852 if (!mutex_tryenter(&ds
->ds_recvlock
)) {
853 dsl_dataset_rele(ds
, dmu_recv_tag
);
857 /* tmp clone name is: tofs/%tosnap" */
858 (void) snprintf(rbsa
.clonelastname
, sizeof (rbsa
.clonelastname
),
861 err
= dsl_sync_task_do(ds
->ds_dir
->dd_pool
,
862 recv_existing_check
, recv_existing_sync
, ds
, &rbsa
, 5);
864 mutex_exit(&ds
->ds_recvlock
);
865 dsl_dataset_rele(ds
, dmu_recv_tag
);
868 drc
->drc_logical_ds
= ds
;
869 drc
->drc_real_ds
= rbsa
.ds
;
870 } else if (err
== ENOENT
) {
871 /* target fs does not exist; must be a full backup or clone */
875 * If it's a non-clone incremental, we are missing the
876 * target fs, so fail the recv.
878 if (rbsa
.fromguid
&& !(flags
& DRR_FLAG_CLONE
))
881 /* Open the parent of tofs */
882 cp
= strrchr(tofs
, '/');
884 err
= dsl_dataset_hold(tofs
, FTAG
, &ds
);
889 if (dmu_recv_verify_features(ds
, drrb
)) {
890 dsl_dataset_rele(ds
, FTAG
);
894 err
= dsl_sync_task_do(ds
->ds_dir
->dd_pool
,
895 recv_new_check
, recv_new_sync
, ds
->ds_dir
, &rbsa
, 5);
896 dsl_dataset_rele(ds
, FTAG
);
899 drc
->drc_logical_ds
= drc
->drc_real_ds
= rbsa
.ds
;
900 drc
->drc_newfs
= B_TRUE
;
912 int bufsize
; /* amount of memory allocated for buf */
914 avl_tree_t
*guid_to_ds_map
;
917 typedef struct guid_map_entry
{
919 dsl_dataset_t
*gme_ds
;
924 guid_compare(const void *arg1
, const void *arg2
)
926 const guid_map_entry_t
*gmep1
= arg1
;
927 const guid_map_entry_t
*gmep2
= arg2
;
929 if (gmep1
->guid
< gmep2
->guid
)
931 else if (gmep1
->guid
> gmep2
->guid
)
937 free_guid_map_onexit(void *arg
)
939 avl_tree_t
*ca
= arg
;
941 guid_map_entry_t
*gmep
;
943 while ((gmep
= avl_destroy_nodes(ca
, &cookie
)) != NULL
) {
944 dsl_dataset_rele(gmep
->gme_ds
, ca
);
945 kmem_free(gmep
, sizeof (guid_map_entry_t
));
948 kmem_free(ca
, sizeof (avl_tree_t
));
952 restore_read(struct restorearg
*ra
, int len
)
957 /* some things will require 8-byte alignment, so everything must */
958 ASSERT3U(len
% 8, ==, 0);
963 ra
->err
= vn_rdwr(UIO_READ
, ra
->vp
,
964 (caddr_t
)ra
->buf
+ done
, len
- done
,
965 ra
->voff
, UIO_SYSSPACE
, FAPPEND
,
966 RLIM64_INFINITY
, CRED(), &resid
);
968 if (resid
== len
- done
)
970 ra
->voff
+= len
- done
- resid
;
976 ASSERT3U(done
, ==, len
);
979 fletcher_4_incremental_byteswap(rv
, len
, &ra
->cksum
);
981 fletcher_4_incremental_native(rv
, len
, &ra
->cksum
);
986 backup_byteswap(dmu_replay_record_t
*drr
)
988 #define DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
989 #define DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
990 drr
->drr_type
= BSWAP_32(drr
->drr_type
);
991 drr
->drr_payloadlen
= BSWAP_32(drr
->drr_payloadlen
);
992 switch (drr
->drr_type
) {
994 DO64(drr_begin
.drr_magic
);
995 DO64(drr_begin
.drr_versioninfo
);
996 DO64(drr_begin
.drr_creation_time
);
997 DO32(drr_begin
.drr_type
);
998 DO32(drr_begin
.drr_flags
);
999 DO64(drr_begin
.drr_toguid
);
1000 DO64(drr_begin
.drr_fromguid
);
1003 DO64(drr_object
.drr_object
);
1004 /* DO64(drr_object.drr_allocation_txg); */
1005 DO32(drr_object
.drr_type
);
1006 DO32(drr_object
.drr_bonustype
);
1007 DO32(drr_object
.drr_blksz
);
1008 DO32(drr_object
.drr_bonuslen
);
1009 DO64(drr_object
.drr_toguid
);
1011 case DRR_FREEOBJECTS
:
1012 DO64(drr_freeobjects
.drr_firstobj
);
1013 DO64(drr_freeobjects
.drr_numobjs
);
1014 DO64(drr_freeobjects
.drr_toguid
);
1017 DO64(drr_write
.drr_object
);
1018 DO32(drr_write
.drr_type
);
1019 DO64(drr_write
.drr_offset
);
1020 DO64(drr_write
.drr_length
);
1021 DO64(drr_write
.drr_toguid
);
1022 DO64(drr_write
.drr_key
.ddk_cksum
.zc_word
[0]);
1023 DO64(drr_write
.drr_key
.ddk_cksum
.zc_word
[1]);
1024 DO64(drr_write
.drr_key
.ddk_cksum
.zc_word
[2]);
1025 DO64(drr_write
.drr_key
.ddk_cksum
.zc_word
[3]);
1026 DO64(drr_write
.drr_key
.ddk_prop
);
1028 case DRR_WRITE_BYREF
:
1029 DO64(drr_write_byref
.drr_object
);
1030 DO64(drr_write_byref
.drr_offset
);
1031 DO64(drr_write_byref
.drr_length
);
1032 DO64(drr_write_byref
.drr_toguid
);
1033 DO64(drr_write_byref
.drr_refguid
);
1034 DO64(drr_write_byref
.drr_refobject
);
1035 DO64(drr_write_byref
.drr_refoffset
);
1036 DO64(drr_write_byref
.drr_key
.ddk_cksum
.zc_word
[0]);
1037 DO64(drr_write_byref
.drr_key
.ddk_cksum
.zc_word
[1]);
1038 DO64(drr_write_byref
.drr_key
.ddk_cksum
.zc_word
[2]);
1039 DO64(drr_write_byref
.drr_key
.ddk_cksum
.zc_word
[3]);
1040 DO64(drr_write_byref
.drr_key
.ddk_prop
);
1043 DO64(drr_free
.drr_object
);
1044 DO64(drr_free
.drr_offset
);
1045 DO64(drr_free
.drr_length
);
1046 DO64(drr_free
.drr_toguid
);
1049 DO64(drr_spill
.drr_object
);
1050 DO64(drr_spill
.drr_length
);
1051 DO64(drr_spill
.drr_toguid
);
1054 DO64(drr_end
.drr_checksum
.zc_word
[0]);
1055 DO64(drr_end
.drr_checksum
.zc_word
[1]);
1056 DO64(drr_end
.drr_checksum
.zc_word
[2]);
1057 DO64(drr_end
.drr_checksum
.zc_word
[3]);
1058 DO64(drr_end
.drr_toguid
);
1068 restore_object(struct restorearg
*ra
, objset_t
*os
, struct drr_object
*drro
)
1074 if (drro
->drr_type
== DMU_OT_NONE
||
1075 drro
->drr_type
>= DMU_OT_NUMTYPES
||
1076 drro
->drr_bonustype
>= DMU_OT_NUMTYPES
||
1077 drro
->drr_checksumtype
>= ZIO_CHECKSUM_FUNCTIONS
||
1078 drro
->drr_compress
>= ZIO_COMPRESS_FUNCTIONS
||
1079 P2PHASE(drro
->drr_blksz
, SPA_MINBLOCKSIZE
) ||
1080 drro
->drr_blksz
< SPA_MINBLOCKSIZE
||
1081 drro
->drr_blksz
> SPA_MAXBLOCKSIZE
||
1082 drro
->drr_bonuslen
> DN_MAX_BONUSLEN
) {
1086 err
= dmu_object_info(os
, drro
->drr_object
, NULL
);
1088 if (err
!= 0 && err
!= ENOENT
)
1091 if (drro
->drr_bonuslen
) {
1092 data
= restore_read(ra
, P2ROUNDUP(drro
->drr_bonuslen
, 8));
1097 if (err
== ENOENT
) {
1098 /* currently free, want to be allocated */
1099 tx
= dmu_tx_create(os
);
1100 dmu_tx_hold_bonus(tx
, DMU_NEW_OBJECT
);
1101 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1106 err
= dmu_object_claim(os
, drro
->drr_object
,
1107 drro
->drr_type
, drro
->drr_blksz
,
1108 drro
->drr_bonustype
, drro
->drr_bonuslen
, tx
);
1111 /* currently allocated, want to be allocated */
1112 err
= dmu_object_reclaim(os
, drro
->drr_object
,
1113 drro
->drr_type
, drro
->drr_blksz
,
1114 drro
->drr_bonustype
, drro
->drr_bonuslen
);
1120 tx
= dmu_tx_create(os
);
1121 dmu_tx_hold_bonus(tx
, drro
->drr_object
);
1122 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1128 dmu_object_set_checksum(os
, drro
->drr_object
, drro
->drr_checksumtype
,
1130 dmu_object_set_compress(os
, drro
->drr_object
, drro
->drr_compress
, tx
);
1135 VERIFY(0 == dmu_bonus_hold(os
, drro
->drr_object
, FTAG
, &db
));
1136 dmu_buf_will_dirty(db
, tx
);
1138 ASSERT3U(db
->db_size
, >=, drro
->drr_bonuslen
);
1139 bcopy(data
, db
->db_data
, drro
->drr_bonuslen
);
1141 dmu_ot
[drro
->drr_bonustype
].ot_byteswap(db
->db_data
,
1142 drro
->drr_bonuslen
);
1144 dmu_buf_rele(db
, FTAG
);
1152 restore_freeobjects(struct restorearg
*ra
, objset_t
*os
,
1153 struct drr_freeobjects
*drrfo
)
1157 if (drrfo
->drr_firstobj
+ drrfo
->drr_numobjs
< drrfo
->drr_firstobj
)
1160 for (obj
= drrfo
->drr_firstobj
;
1161 obj
< drrfo
->drr_firstobj
+ drrfo
->drr_numobjs
;
1162 (void) dmu_object_next(os
, &obj
, FALSE
, 0)) {
1165 if (dmu_object_info(os
, obj
, NULL
) != 0)
1168 err
= dmu_free_object(os
, obj
);
1176 restore_write(struct restorearg
*ra
, objset_t
*os
,
1177 struct drr_write
*drrw
)
1183 if (drrw
->drr_offset
+ drrw
->drr_length
< drrw
->drr_offset
||
1184 drrw
->drr_type
>= DMU_OT_NUMTYPES
)
1187 data
= restore_read(ra
, drrw
->drr_length
);
1191 if (dmu_object_info(os
, drrw
->drr_object
, NULL
) != 0)
1194 tx
= dmu_tx_create(os
);
1196 dmu_tx_hold_write(tx
, drrw
->drr_object
,
1197 drrw
->drr_offset
, drrw
->drr_length
);
1198 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1204 dmu_ot
[drrw
->drr_type
].ot_byteswap(data
, drrw
->drr_length
);
1205 dmu_write(os
, drrw
->drr_object
,
1206 drrw
->drr_offset
, drrw
->drr_length
, data
, tx
);
1212 * Handle a DRR_WRITE_BYREF record. This record is used in dedup'ed
1213 * streams to refer to a copy of the data that is already on the
1214 * system because it came in earlier in the stream. This function
1215 * finds the earlier copy of the data, and uses that copy instead of
1216 * data from the stream to fulfill this write.
1219 restore_write_byref(struct restorearg
*ra
, objset_t
*os
,
1220 struct drr_write_byref
*drrwbr
)
1224 guid_map_entry_t gmesrch
;
1225 guid_map_entry_t
*gmep
;
1227 objset_t
*ref_os
= NULL
;
1230 if (drrwbr
->drr_offset
+ drrwbr
->drr_length
< drrwbr
->drr_offset
)
1234 * If the GUID of the referenced dataset is different from the
1235 * GUID of the target dataset, find the referenced dataset.
1237 if (drrwbr
->drr_toguid
!= drrwbr
->drr_refguid
) {
1238 gmesrch
.guid
= drrwbr
->drr_refguid
;
1239 if ((gmep
= avl_find(ra
->guid_to_ds_map
, &gmesrch
,
1243 if (dmu_objset_from_ds(gmep
->gme_ds
, &ref_os
))
1249 err
= dmu_buf_hold(ref_os
, drrwbr
->drr_refobject
,
1250 drrwbr
->drr_refoffset
, FTAG
, &dbp
, DMU_READ_PREFETCH
);
1254 tx
= dmu_tx_create(os
);
1256 dmu_tx_hold_write(tx
, drrwbr
->drr_object
,
1257 drrwbr
->drr_offset
, drrwbr
->drr_length
);
1258 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1263 dmu_write(os
, drrwbr
->drr_object
,
1264 drrwbr
->drr_offset
, drrwbr
->drr_length
, dbp
->db_data
, tx
);
1265 dmu_buf_rele(dbp
, FTAG
);
1271 restore_spill(struct restorearg
*ra
, objset_t
*os
, struct drr_spill
*drrs
)
1275 dmu_buf_t
*db
, *db_spill
;
1278 if (drrs
->drr_length
< SPA_MINBLOCKSIZE
||
1279 drrs
->drr_length
> SPA_MAXBLOCKSIZE
)
1282 data
= restore_read(ra
, drrs
->drr_length
);
1286 if (dmu_object_info(os
, drrs
->drr_object
, NULL
) != 0)
1289 VERIFY(0 == dmu_bonus_hold(os
, drrs
->drr_object
, FTAG
, &db
));
1290 if ((err
= dmu_spill_hold_by_bonus(db
, FTAG
, &db_spill
)) != 0) {
1291 dmu_buf_rele(db
, FTAG
);
1295 tx
= dmu_tx_create(os
);
1297 dmu_tx_hold_spill(tx
, db
->db_object
);
1299 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1301 dmu_buf_rele(db
, FTAG
);
1302 dmu_buf_rele(db_spill
, FTAG
);
1306 dmu_buf_will_dirty(db_spill
, tx
);
1308 if (db_spill
->db_size
< drrs
->drr_length
)
1309 VERIFY(0 == dbuf_spill_set_blksz(db_spill
,
1310 drrs
->drr_length
, tx
));
1311 bcopy(data
, db_spill
->db_data
, drrs
->drr_length
);
1313 dmu_buf_rele(db
, FTAG
);
1314 dmu_buf_rele(db_spill
, FTAG
);
1322 restore_free(struct restorearg
*ra
, objset_t
*os
,
1323 struct drr_free
*drrf
)
1327 if (drrf
->drr_length
!= -1ULL &&
1328 drrf
->drr_offset
+ drrf
->drr_length
< drrf
->drr_offset
)
1331 if (dmu_object_info(os
, drrf
->drr_object
, NULL
) != 0)
1334 err
= dmu_free_long_range(os
, drrf
->drr_object
,
1335 drrf
->drr_offset
, drrf
->drr_length
);
1340 * NB: callers *must* call dmu_recv_end() if this succeeds.
1343 dmu_recv_stream(dmu_recv_cookie_t
*drc
, vnode_t
*vp
, offset_t
*voffp
,
1344 int cleanup_fd
, uint64_t *action_handlep
)
1346 struct restorearg ra
= { 0 };
1347 dmu_replay_record_t
*drr
;
1352 if (drc
->drc_drrb
->drr_magic
== BSWAP_64(DMU_BACKUP_MAGIC
))
1356 /* compute checksum of drr_begin record */
1357 dmu_replay_record_t
*drr
;
1358 drr
= kmem_zalloc(sizeof (dmu_replay_record_t
), KM_SLEEP
);
1360 drr
->drr_type
= DRR_BEGIN
;
1361 drr
->drr_u
.drr_begin
= *drc
->drc_drrb
;
1363 fletcher_4_incremental_byteswap(drr
,
1364 sizeof (dmu_replay_record_t
), &ra
.cksum
);
1366 fletcher_4_incremental_native(drr
,
1367 sizeof (dmu_replay_record_t
), &ra
.cksum
);
1369 kmem_free(drr
, sizeof (dmu_replay_record_t
));
1373 struct drr_begin
*drrb
= drc
->drc_drrb
;
1374 drrb
->drr_magic
= BSWAP_64(drrb
->drr_magic
);
1375 drrb
->drr_versioninfo
= BSWAP_64(drrb
->drr_versioninfo
);
1376 drrb
->drr_creation_time
= BSWAP_64(drrb
->drr_creation_time
);
1377 drrb
->drr_type
= BSWAP_32(drrb
->drr_type
);
1378 drrb
->drr_toguid
= BSWAP_64(drrb
->drr_toguid
);
1379 drrb
->drr_fromguid
= BSWAP_64(drrb
->drr_fromguid
);
1385 ra
.buf
= vmem_alloc(ra
.bufsize
, KM_SLEEP
);
1387 /* these were verified in dmu_recv_begin */
1388 ASSERT(DMU_GET_STREAM_HDRTYPE(drc
->drc_drrb
->drr_versioninfo
) ==
1390 ASSERT(drc
->drc_drrb
->drr_type
< DMU_OST_NUMTYPES
);
1393 * Open the objset we are modifying.
1395 VERIFY(dmu_objset_from_ds(drc
->drc_real_ds
, &os
) == 0);
1397 ASSERT(drc
->drc_real_ds
->ds_phys
->ds_flags
& DS_FLAG_INCONSISTENT
);
1399 featureflags
= DMU_GET_FEATUREFLAGS(drc
->drc_drrb
->drr_versioninfo
);
1401 /* if this stream is dedup'ed, set up the avl tree for guid mapping */
1402 if (featureflags
& DMU_BACKUP_FEATURE_DEDUP
) {
1405 if (cleanup_fd
== -1) {
1409 ra
.err
= zfs_onexit_fd_hold(cleanup_fd
, &minor
);
1415 if (*action_handlep
== 0) {
1417 kmem_alloc(sizeof (avl_tree_t
), KM_SLEEP
);
1418 avl_create(ra
.guid_to_ds_map
, guid_compare
,
1419 sizeof (guid_map_entry_t
),
1420 offsetof(guid_map_entry_t
, avlnode
));
1421 ra
.err
= zfs_onexit_add_cb(minor
,
1422 free_guid_map_onexit
, ra
.guid_to_ds_map
,
1427 ra
.err
= zfs_onexit_cb_data(minor
, *action_handlep
,
1428 (void **)&ra
.guid_to_ds_map
);
1433 drc
->drc_guid_to_ds_map
= ra
.guid_to_ds_map
;
1437 * Read records and process them.
1440 while (ra
.err
== 0 &&
1441 NULL
!= (drr
= restore_read(&ra
, sizeof (*drr
)))) {
1442 if (issig(JUSTLOOKING
) && issig(FORREAL
)) {
1448 backup_byteswap(drr
);
1450 switch (drr
->drr_type
) {
1454 * We need to make a copy of the record header,
1455 * because restore_{object,write} may need to
1456 * restore_read(), which will invalidate drr.
1458 struct drr_object drro
= drr
->drr_u
.drr_object
;
1459 ra
.err
= restore_object(&ra
, os
, &drro
);
1462 case DRR_FREEOBJECTS
:
1464 struct drr_freeobjects drrfo
=
1465 drr
->drr_u
.drr_freeobjects
;
1466 ra
.err
= restore_freeobjects(&ra
, os
, &drrfo
);
1471 struct drr_write drrw
= drr
->drr_u
.drr_write
;
1472 ra
.err
= restore_write(&ra
, os
, &drrw
);
1475 case DRR_WRITE_BYREF
:
1477 struct drr_write_byref drrwbr
=
1478 drr
->drr_u
.drr_write_byref
;
1479 ra
.err
= restore_write_byref(&ra
, os
, &drrwbr
);
1484 struct drr_free drrf
= drr
->drr_u
.drr_free
;
1485 ra
.err
= restore_free(&ra
, os
, &drrf
);
1490 struct drr_end drre
= drr
->drr_u
.drr_end
;
1492 * We compare against the *previous* checksum
1493 * value, because the stored checksum is of
1494 * everything before the DRR_END record.
1496 if (!ZIO_CHECKSUM_EQUAL(drre
.drr_checksum
, pcksum
))
1502 struct drr_spill drrs
= drr
->drr_u
.drr_spill
;
1503 ra
.err
= restore_spill(&ra
, os
, &drrs
);
1512 ASSERT(ra
.err
!= 0);
1515 if ((featureflags
& DMU_BACKUP_FEATURE_DEDUP
) && (cleanup_fd
!= -1))
1516 zfs_onexit_fd_rele(cleanup_fd
);
1520 * destroy what we created, so we don't leave it in the
1521 * inconsistent restoring state.
1523 txg_wait_synced(drc
->drc_real_ds
->ds_dir
->dd_pool
, 0);
1525 (void) dsl_dataset_destroy(drc
->drc_real_ds
, dmu_recv_tag
,
1527 if (drc
->drc_real_ds
!= drc
->drc_logical_ds
) {
1528 mutex_exit(&drc
->drc_logical_ds
->ds_recvlock
);
1529 dsl_dataset_rele(drc
->drc_logical_ds
, dmu_recv_tag
);
1533 vmem_free(ra
.buf
, ra
.bufsize
);
1538 struct recvendsyncarg
{
1540 uint64_t creation_time
;
1545 recv_end_check(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
1547 dsl_dataset_t
*ds
= arg1
;
1548 struct recvendsyncarg
*resa
= arg2
;
1550 return (dsl_dataset_snapshot_check(ds
, resa
->tosnap
, tx
));
1554 recv_end_sync(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
1556 dsl_dataset_t
*ds
= arg1
;
1557 struct recvendsyncarg
*resa
= arg2
;
1559 dsl_dataset_snapshot_sync(ds
, resa
->tosnap
, tx
);
1561 /* set snapshot's creation time and guid */
1562 dmu_buf_will_dirty(ds
->ds_prev
->ds_dbuf
, tx
);
1563 ds
->ds_prev
->ds_phys
->ds_creation_time
= resa
->creation_time
;
1564 ds
->ds_prev
->ds_phys
->ds_guid
= resa
->toguid
;
1565 ds
->ds_prev
->ds_phys
->ds_flags
&= ~DS_FLAG_INCONSISTENT
;
1567 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
1568 ds
->ds_phys
->ds_flags
&= ~DS_FLAG_INCONSISTENT
;
1572 add_ds_to_guidmap(avl_tree_t
*guid_map
, dsl_dataset_t
*ds
)
1574 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
1575 uint64_t snapobj
= ds
->ds_phys
->ds_prev_snap_obj
;
1576 dsl_dataset_t
*snapds
;
1577 guid_map_entry_t
*gmep
;
1580 ASSERT(guid_map
!= NULL
);
1582 rw_enter(&dp
->dp_config_rwlock
, RW_READER
);
1583 err
= dsl_dataset_hold_obj(dp
, snapobj
, guid_map
, &snapds
);
1585 gmep
= kmem_alloc(sizeof (guid_map_entry_t
), KM_SLEEP
);
1586 gmep
->guid
= snapds
->ds_phys
->ds_guid
;
1587 gmep
->gme_ds
= snapds
;
1588 avl_add(guid_map
, gmep
);
1591 rw_exit(&dp
->dp_config_rwlock
);
1596 dmu_recv_existing_end(dmu_recv_cookie_t
*drc
)
1598 struct recvendsyncarg resa
;
1599 dsl_dataset_t
*ds
= drc
->drc_logical_ds
;
1603 * XXX hack; seems the ds is still dirty and dsl_pool_zil_clean()
1604 * expects it to have a ds_user_ptr (and zil), but clone_swap()
1607 txg_wait_synced(ds
->ds_dir
->dd_pool
, 0);
1609 if (dsl_dataset_tryown(ds
, FALSE
, dmu_recv_tag
)) {
1610 err
= dsl_dataset_clone_swap(drc
->drc_real_ds
, ds
,
1615 mutex_exit(&ds
->ds_recvlock
);
1616 dsl_dataset_rele(ds
, dmu_recv_tag
);
1617 (void) dsl_dataset_destroy(drc
->drc_real_ds
, dmu_recv_tag
,
1622 resa
.creation_time
= drc
->drc_drrb
->drr_creation_time
;
1623 resa
.toguid
= drc
->drc_drrb
->drr_toguid
;
1624 resa
.tosnap
= drc
->drc_tosnap
;
1626 err
= dsl_sync_task_do(ds
->ds_dir
->dd_pool
,
1627 recv_end_check
, recv_end_sync
, ds
, &resa
, 3);
1630 (void) dsl_dataset_clone_swap(drc
->drc_real_ds
, ds
, B_TRUE
);
1634 mutex_exit(&ds
->ds_recvlock
);
1635 if (err
== 0 && drc
->drc_guid_to_ds_map
!= NULL
)
1636 (void) add_ds_to_guidmap(drc
->drc_guid_to_ds_map
, ds
);
1637 dsl_dataset_disown(ds
, dmu_recv_tag
);
1638 myerr
= dsl_dataset_destroy(drc
->drc_real_ds
, dmu_recv_tag
, B_FALSE
);
1639 ASSERT3U(myerr
, ==, 0);
1644 dmu_recv_new_end(dmu_recv_cookie_t
*drc
)
1646 struct recvendsyncarg resa
;
1647 dsl_dataset_t
*ds
= drc
->drc_logical_ds
;
1651 * XXX hack; seems the ds is still dirty and dsl_pool_zil_clean()
1652 * expects it to have a ds_user_ptr (and zil), but clone_swap()
1655 txg_wait_synced(ds
->ds_dir
->dd_pool
, 0);
1657 resa
.creation_time
= drc
->drc_drrb
->drr_creation_time
;
1658 resa
.toguid
= drc
->drc_drrb
->drr_toguid
;
1659 resa
.tosnap
= drc
->drc_tosnap
;
1661 err
= dsl_sync_task_do(ds
->ds_dir
->dd_pool
,
1662 recv_end_check
, recv_end_sync
, ds
, &resa
, 3);
1664 /* clean up the fs we just recv'd into */
1665 (void) dsl_dataset_destroy(ds
, dmu_recv_tag
, B_FALSE
);
1667 if (drc
->drc_guid_to_ds_map
!= NULL
)
1668 (void) add_ds_to_guidmap(drc
->drc_guid_to_ds_map
, ds
);
1669 /* release the hold from dmu_recv_begin */
1670 dsl_dataset_disown(ds
, dmu_recv_tag
);
1676 dmu_recv_end(dmu_recv_cookie_t
*drc
)
1678 if (drc
->drc_logical_ds
!= drc
->drc_real_ds
)
1679 return (dmu_recv_existing_end(drc
));
1681 return (dmu_recv_new_end(drc
));