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>
54 /* Set this tunable to TRUE to replace corrupt data with 0x2f5baddb10c */
55 int zfs_send_corrupt_data
= B_FALSE
;
57 static char *dmu_recv_tag
= "dmu_recv_tag";
58 static const char *recv_clone_name
= "%recv";
60 typedef struct dump_bytes_io
{
61 dmu_sendarg_t
*dbi_dsp
;
67 dump_bytes_strategy(void *arg
)
69 dump_bytes_io_t
*dbi
= (dump_bytes_io_t
*)arg
;
70 dmu_sendarg_t
*dsp
= dbi
->dbi_dsp
;
71 dsl_dataset_t
*ds
= dsp
->dsa_os
->os_dsl_dataset
;
72 ssize_t resid
; /* have to get resid to get detailed errno */
73 ASSERT0(dbi
->dbi_len
% 8);
75 fletcher_4_incremental_native(dbi
->dbi_buf
, dbi
->dbi_len
, &dsp
->dsa_zc
);
76 dsp
->dsa_err
= vn_rdwr(UIO_WRITE
, dsp
->dsa_vp
,
77 (caddr_t
)dbi
->dbi_buf
, dbi
->dbi_len
,
78 0, UIO_SYSSPACE
, FAPPEND
, RLIM64_INFINITY
, CRED(), &resid
);
80 mutex_enter(&ds
->ds_sendstream_lock
);
81 *dsp
->dsa_off
+= dbi
->dbi_len
;
82 mutex_exit(&ds
->ds_sendstream_lock
);
86 dump_bytes(dmu_sendarg_t
*dsp
, void *buf
, int len
)
95 * The vn_rdwr() call is performed in a taskq to ensure that there is
96 * always enough stack space to write safely to the target filesystem.
97 * The ZIO_TYPE_FREE threads are used because there can be a lot of
98 * them and they are used in vdev_file.c for a similar purpose.
100 spa_taskq_dispatch_sync(dmu_objset_spa(dsp
->dsa_os
), ZIO_TYPE_FREE
,
101 ZIO_TASKQ_ISSUE
, dump_bytes_strategy
, &dbi
, TQ_SLEEP
);
103 return (dsp
->dsa_err
);
107 dump_free(dmu_sendarg_t
*dsp
, uint64_t object
, uint64_t offset
,
110 struct drr_free
*drrf
= &(dsp
->dsa_drr
->drr_u
.drr_free
);
112 if (length
!= -1ULL && offset
+ length
< offset
)
116 * If there is a pending op, but it's not PENDING_FREE, push it out,
117 * since free block aggregation can only be done for blocks of the
118 * same type (i.e., DRR_FREE records can only be aggregated with
119 * other DRR_FREE records. DRR_FREEOBJECTS records can only be
120 * aggregated with other DRR_FREEOBJECTS records.
122 if (dsp
->dsa_pending_op
!= PENDING_NONE
&&
123 dsp
->dsa_pending_op
!= PENDING_FREE
) {
124 if (dump_bytes(dsp
, dsp
->dsa_drr
,
125 sizeof (dmu_replay_record_t
)) != 0)
126 return (SET_ERROR(EINTR
));
127 dsp
->dsa_pending_op
= PENDING_NONE
;
130 if (dsp
->dsa_pending_op
== PENDING_FREE
) {
132 * There should never be a PENDING_FREE if length is -1
133 * (because dump_dnode is the only place where this
134 * function is called with a -1, and only after flushing
135 * any pending record).
137 ASSERT(length
!= -1ULL);
139 * Check to see whether this free block can be aggregated
142 if (drrf
->drr_object
== object
&& drrf
->drr_offset
+
143 drrf
->drr_length
== offset
) {
144 drrf
->drr_length
+= length
;
147 /* not a continuation. Push out pending record */
148 if (dump_bytes(dsp
, dsp
->dsa_drr
,
149 sizeof (dmu_replay_record_t
)) != 0)
150 return (SET_ERROR(EINTR
));
151 dsp
->dsa_pending_op
= PENDING_NONE
;
154 /* create a FREE record and make it pending */
155 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
156 dsp
->dsa_drr
->drr_type
= DRR_FREE
;
157 drrf
->drr_object
= object
;
158 drrf
->drr_offset
= offset
;
159 drrf
->drr_length
= length
;
160 drrf
->drr_toguid
= dsp
->dsa_toguid
;
161 if (length
== -1ULL) {
162 if (dump_bytes(dsp
, dsp
->dsa_drr
,
163 sizeof (dmu_replay_record_t
)) != 0)
164 return (SET_ERROR(EINTR
));
166 dsp
->dsa_pending_op
= PENDING_FREE
;
173 dump_data(dmu_sendarg_t
*dsp
, dmu_object_type_t type
,
174 uint64_t object
, uint64_t offset
, int blksz
, const blkptr_t
*bp
, void *data
)
176 struct drr_write
*drrw
= &(dsp
->dsa_drr
->drr_u
.drr_write
);
180 * If there is any kind of pending aggregation (currently either
181 * a grouping of free objects or free blocks), push it out to
182 * the stream, since aggregation can't be done across operations
183 * of different types.
185 if (dsp
->dsa_pending_op
!= PENDING_NONE
) {
186 if (dump_bytes(dsp
, dsp
->dsa_drr
,
187 sizeof (dmu_replay_record_t
)) != 0)
188 return (SET_ERROR(EINTR
));
189 dsp
->dsa_pending_op
= PENDING_NONE
;
191 /* write a DATA record */
192 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
193 dsp
->dsa_drr
->drr_type
= DRR_WRITE
;
194 drrw
->drr_object
= object
;
195 drrw
->drr_type
= type
;
196 drrw
->drr_offset
= offset
;
197 drrw
->drr_length
= blksz
;
198 drrw
->drr_toguid
= dsp
->dsa_toguid
;
199 drrw
->drr_checksumtype
= BP_GET_CHECKSUM(bp
);
200 if (zio_checksum_table
[drrw
->drr_checksumtype
].ci_dedup
)
201 drrw
->drr_checksumflags
|= DRR_CHECKSUM_DEDUP
;
202 DDK_SET_LSIZE(&drrw
->drr_key
, BP_GET_LSIZE(bp
));
203 DDK_SET_PSIZE(&drrw
->drr_key
, BP_GET_PSIZE(bp
));
204 DDK_SET_COMPRESS(&drrw
->drr_key
, BP_GET_COMPRESS(bp
));
205 drrw
->drr_key
.ddk_cksum
= bp
->blk_cksum
;
207 if (dump_bytes(dsp
, dsp
->dsa_drr
, sizeof (dmu_replay_record_t
)) != 0)
208 return (SET_ERROR(EINTR
));
209 if (dump_bytes(dsp
, data
, blksz
) != 0)
210 return (SET_ERROR(EINTR
));
215 dump_spill(dmu_sendarg_t
*dsp
, uint64_t object
, int blksz
, void *data
)
217 struct drr_spill
*drrs
= &(dsp
->dsa_drr
->drr_u
.drr_spill
);
219 if (dsp
->dsa_pending_op
!= PENDING_NONE
) {
220 if (dump_bytes(dsp
, dsp
->dsa_drr
,
221 sizeof (dmu_replay_record_t
)) != 0)
222 return (SET_ERROR(EINTR
));
223 dsp
->dsa_pending_op
= PENDING_NONE
;
226 /* write a SPILL record */
227 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
228 dsp
->dsa_drr
->drr_type
= DRR_SPILL
;
229 drrs
->drr_object
= object
;
230 drrs
->drr_length
= blksz
;
231 drrs
->drr_toguid
= dsp
->dsa_toguid
;
233 if (dump_bytes(dsp
, dsp
->dsa_drr
, sizeof (dmu_replay_record_t
)))
234 return (SET_ERROR(EINTR
));
235 if (dump_bytes(dsp
, data
, blksz
))
236 return (SET_ERROR(EINTR
));
241 dump_freeobjects(dmu_sendarg_t
*dsp
, uint64_t firstobj
, uint64_t numobjs
)
243 struct drr_freeobjects
*drrfo
= &(dsp
->dsa_drr
->drr_u
.drr_freeobjects
);
246 * If there is a pending op, but it's not PENDING_FREEOBJECTS,
247 * push it out, since free block aggregation can only be done for
248 * blocks of the same type (i.e., DRR_FREE records can only be
249 * aggregated with other DRR_FREE records. DRR_FREEOBJECTS records
250 * can only be aggregated with other DRR_FREEOBJECTS records.
252 if (dsp
->dsa_pending_op
!= PENDING_NONE
&&
253 dsp
->dsa_pending_op
!= PENDING_FREEOBJECTS
) {
254 if (dump_bytes(dsp
, dsp
->dsa_drr
,
255 sizeof (dmu_replay_record_t
)) != 0)
256 return (SET_ERROR(EINTR
));
257 dsp
->dsa_pending_op
= PENDING_NONE
;
259 if (dsp
->dsa_pending_op
== PENDING_FREEOBJECTS
) {
261 * See whether this free object array can be aggregated
264 if (drrfo
->drr_firstobj
+ drrfo
->drr_numobjs
== firstobj
) {
265 drrfo
->drr_numobjs
+= numobjs
;
268 /* can't be aggregated. Push out pending record */
269 if (dump_bytes(dsp
, dsp
->dsa_drr
,
270 sizeof (dmu_replay_record_t
)) != 0)
271 return (SET_ERROR(EINTR
));
272 dsp
->dsa_pending_op
= PENDING_NONE
;
276 /* write a FREEOBJECTS record */
277 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
278 dsp
->dsa_drr
->drr_type
= DRR_FREEOBJECTS
;
279 drrfo
->drr_firstobj
= firstobj
;
280 drrfo
->drr_numobjs
= numobjs
;
281 drrfo
->drr_toguid
= dsp
->dsa_toguid
;
283 dsp
->dsa_pending_op
= PENDING_FREEOBJECTS
;
289 dump_dnode(dmu_sendarg_t
*dsp
, uint64_t object
, dnode_phys_t
*dnp
)
291 struct drr_object
*drro
= &(dsp
->dsa_drr
->drr_u
.drr_object
);
293 if (dnp
== NULL
|| dnp
->dn_type
== DMU_OT_NONE
)
294 return (dump_freeobjects(dsp
, object
, 1));
296 if (dsp
->dsa_pending_op
!= PENDING_NONE
) {
297 if (dump_bytes(dsp
, dsp
->dsa_drr
,
298 sizeof (dmu_replay_record_t
)) != 0)
299 return (SET_ERROR(EINTR
));
300 dsp
->dsa_pending_op
= PENDING_NONE
;
303 /* write an OBJECT record */
304 bzero(dsp
->dsa_drr
, sizeof (dmu_replay_record_t
));
305 dsp
->dsa_drr
->drr_type
= DRR_OBJECT
;
306 drro
->drr_object
= object
;
307 drro
->drr_type
= dnp
->dn_type
;
308 drro
->drr_bonustype
= dnp
->dn_bonustype
;
309 drro
->drr_blksz
= dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
;
310 drro
->drr_bonuslen
= dnp
->dn_bonuslen
;
311 drro
->drr_checksumtype
= dnp
->dn_checksum
;
312 drro
->drr_compress
= dnp
->dn_compress
;
313 drro
->drr_toguid
= dsp
->dsa_toguid
;
315 if (dump_bytes(dsp
, dsp
->dsa_drr
, sizeof (dmu_replay_record_t
)) != 0)
316 return (SET_ERROR(EINTR
));
318 if (dump_bytes(dsp
, DN_BONUS(dnp
), P2ROUNDUP(dnp
->dn_bonuslen
, 8)) != 0)
319 return (SET_ERROR(EINTR
));
321 /* free anything past the end of the file */
322 if (dump_free(dsp
, object
, (dnp
->dn_maxblkid
+ 1) *
323 (dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
), -1ULL))
324 return (SET_ERROR(EINTR
));
325 if (dsp
->dsa_err
!= 0)
326 return (SET_ERROR(EINTR
));
330 #define BP_SPAN(dnp, level) \
331 (((uint64_t)dnp->dn_datablkszsec) << (SPA_MINBLOCKSHIFT + \
332 (level) * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT)))
336 backup_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
337 const zbookmark_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
339 dmu_sendarg_t
*dsp
= arg
;
340 dmu_object_type_t type
= bp
? BP_GET_TYPE(bp
) : DMU_OT_NONE
;
343 if (issig(JUSTLOOKING
) && issig(FORREAL
))
344 return (SET_ERROR(EINTR
));
346 if (zb
->zb_object
!= DMU_META_DNODE_OBJECT
&&
347 DMU_OBJECT_IS_SPECIAL(zb
->zb_object
)) {
349 } else if (bp
== NULL
&& zb
->zb_object
== DMU_META_DNODE_OBJECT
) {
350 uint64_t span
= BP_SPAN(dnp
, zb
->zb_level
);
351 uint64_t dnobj
= (zb
->zb_blkid
* span
) >> DNODE_SHIFT
;
352 err
= dump_freeobjects(dsp
, dnobj
, span
>> DNODE_SHIFT
);
353 } else if (bp
== NULL
) {
354 uint64_t span
= BP_SPAN(dnp
, zb
->zb_level
);
355 err
= dump_free(dsp
, zb
->zb_object
, zb
->zb_blkid
* span
, span
);
356 } else if (zb
->zb_level
> 0 || type
== DMU_OT_OBJSET
) {
358 } else if (type
== DMU_OT_DNODE
) {
361 int blksz
= BP_GET_LSIZE(bp
);
362 uint32_t aflags
= ARC_WAIT
;
365 if (arc_read(NULL
, spa
, bp
, arc_getbuf_func
, &abuf
,
366 ZIO_PRIORITY_ASYNC_READ
, ZIO_FLAG_CANFAIL
,
368 return (SET_ERROR(EIO
));
371 for (i
= 0; i
< blksz
>> DNODE_SHIFT
; i
++) {
372 uint64_t dnobj
= (zb
->zb_blkid
<<
373 (DNODE_BLOCK_SHIFT
- DNODE_SHIFT
)) + i
;
374 err
= dump_dnode(dsp
, dnobj
, blk
+i
);
378 (void) arc_buf_remove_ref(abuf
, &abuf
);
379 } else if (type
== DMU_OT_SA
) {
380 uint32_t aflags
= ARC_WAIT
;
382 int blksz
= BP_GET_LSIZE(bp
);
384 if (arc_read(NULL
, spa
, bp
, arc_getbuf_func
, &abuf
,
385 ZIO_PRIORITY_ASYNC_READ
, ZIO_FLAG_CANFAIL
,
387 return (SET_ERROR(EIO
));
389 err
= dump_spill(dsp
, zb
->zb_object
, blksz
, abuf
->b_data
);
390 (void) arc_buf_remove_ref(abuf
, &abuf
);
391 } else { /* it's a level-0 block of a regular object */
392 uint32_t aflags
= ARC_WAIT
;
394 int blksz
= BP_GET_LSIZE(bp
);
396 if (arc_read(NULL
, spa
, bp
, arc_getbuf_func
, &abuf
,
397 ZIO_PRIORITY_ASYNC_READ
, ZIO_FLAG_CANFAIL
,
399 if (zfs_send_corrupt_data
) {
401 /* Send a block filled with 0x"zfs badd bloc" */
402 abuf
= arc_buf_alloc(spa
, blksz
, &abuf
,
404 for (ptr
= abuf
->b_data
;
405 (char *)ptr
< (char *)abuf
->b_data
+ blksz
;
407 *ptr
= 0x2f5baddb10cULL
;
409 return (SET_ERROR(EIO
));
413 err
= dump_data(dsp
, type
, zb
->zb_object
, zb
->zb_blkid
* blksz
,
414 blksz
, bp
, abuf
->b_data
);
415 (void) arc_buf_remove_ref(abuf
, &abuf
);
418 ASSERT(err
== 0 || err
== EINTR
);
423 * Releases dp, ds, and fromds, using the specified tag.
426 dmu_send_impl(void *tag
, dsl_pool_t
*dp
, dsl_dataset_t
*ds
,
427 dsl_dataset_t
*fromds
, int outfd
, vnode_t
*vp
, offset_t
*off
)
430 dmu_replay_record_t
*drr
;
433 uint64_t fromtxg
= 0;
435 if (fromds
!= NULL
&& !dsl_dataset_is_before(ds
, fromds
)) {
436 dsl_dataset_rele(fromds
, tag
);
437 dsl_dataset_rele(ds
, tag
);
438 dsl_pool_rele(dp
, tag
);
439 return (SET_ERROR(EXDEV
));
442 err
= dmu_objset_from_ds(ds
, &os
);
445 dsl_dataset_rele(fromds
, tag
);
446 dsl_dataset_rele(ds
, tag
);
447 dsl_pool_rele(dp
, tag
);
451 drr
= kmem_zalloc(sizeof (dmu_replay_record_t
), KM_SLEEP
);
452 drr
->drr_type
= DRR_BEGIN
;
453 drr
->drr_u
.drr_begin
.drr_magic
= DMU_BACKUP_MAGIC
;
454 DMU_SET_STREAM_HDRTYPE(drr
->drr_u
.drr_begin
.drr_versioninfo
,
458 if (dmu_objset_type(os
) == DMU_OST_ZFS
) {
460 if (zfs_get_zplprop(os
, ZFS_PROP_VERSION
, &version
) != 0) {
461 kmem_free(drr
, sizeof (dmu_replay_record_t
));
463 dsl_dataset_rele(fromds
, tag
);
464 dsl_dataset_rele(ds
, tag
);
465 dsl_pool_rele(dp
, tag
);
466 return (SET_ERROR(EINVAL
));
468 if (version
>= ZPL_VERSION_SA
) {
469 DMU_SET_FEATUREFLAGS(
470 drr
->drr_u
.drr_begin
.drr_versioninfo
,
471 DMU_BACKUP_FEATURE_SA_SPILL
);
476 drr
->drr_u
.drr_begin
.drr_creation_time
=
477 ds
->ds_phys
->ds_creation_time
;
478 drr
->drr_u
.drr_begin
.drr_type
= dmu_objset_type(os
);
479 if (fromds
!= NULL
&& ds
->ds_dir
!= fromds
->ds_dir
)
480 drr
->drr_u
.drr_begin
.drr_flags
|= DRR_FLAG_CLONE
;
481 drr
->drr_u
.drr_begin
.drr_toguid
= ds
->ds_phys
->ds_guid
;
482 if (ds
->ds_phys
->ds_flags
& DS_FLAG_CI_DATASET
)
483 drr
->drr_u
.drr_begin
.drr_flags
|= DRR_FLAG_CI_DATA
;
486 drr
->drr_u
.drr_begin
.drr_fromguid
= fromds
->ds_phys
->ds_guid
;
487 dsl_dataset_name(ds
, drr
->drr_u
.drr_begin
.drr_toname
);
489 if (fromds
!= NULL
) {
490 fromtxg
= fromds
->ds_phys
->ds_creation_txg
;
491 dsl_dataset_rele(fromds
, tag
);
495 dsp
= kmem_zalloc(sizeof (dmu_sendarg_t
), KM_SLEEP
);
499 dsp
->dsa_outfd
= outfd
;
500 dsp
->dsa_proc
= curproc
;
503 dsp
->dsa_toguid
= ds
->ds_phys
->ds_guid
;
504 ZIO_SET_CHECKSUM(&dsp
->dsa_zc
, 0, 0, 0, 0);
505 dsp
->dsa_pending_op
= PENDING_NONE
;
507 mutex_enter(&ds
->ds_sendstream_lock
);
508 list_insert_head(&ds
->ds_sendstreams
, dsp
);
509 mutex_exit(&ds
->ds_sendstream_lock
);
511 dsl_dataset_long_hold(ds
, FTAG
);
512 dsl_pool_rele(dp
, tag
);
514 if (dump_bytes(dsp
, drr
, sizeof (dmu_replay_record_t
)) != 0) {
519 err
= traverse_dataset(ds
, fromtxg
, TRAVERSE_PRE
| TRAVERSE_PREFETCH
,
522 if (dsp
->dsa_pending_op
!= PENDING_NONE
)
523 if (dump_bytes(dsp
, drr
, sizeof (dmu_replay_record_t
)) != 0)
524 err
= SET_ERROR(EINTR
);
527 if (err
== EINTR
&& dsp
->dsa_err
!= 0)
532 bzero(drr
, sizeof (dmu_replay_record_t
));
533 drr
->drr_type
= DRR_END
;
534 drr
->drr_u
.drr_end
.drr_checksum
= dsp
->dsa_zc
;
535 drr
->drr_u
.drr_end
.drr_toguid
= dsp
->dsa_toguid
;
537 if (dump_bytes(dsp
, drr
, sizeof (dmu_replay_record_t
)) != 0) {
543 mutex_enter(&ds
->ds_sendstream_lock
);
544 list_remove(&ds
->ds_sendstreams
, dsp
);
545 mutex_exit(&ds
->ds_sendstream_lock
);
547 kmem_free(drr
, sizeof (dmu_replay_record_t
));
548 kmem_free(dsp
, sizeof (dmu_sendarg_t
));
550 dsl_dataset_long_rele(ds
, FTAG
);
551 dsl_dataset_rele(ds
, tag
);
557 dmu_send_obj(const char *pool
, uint64_t tosnap
, uint64_t fromsnap
,
558 int outfd
, vnode_t
*vp
, offset_t
*off
)
562 dsl_dataset_t
*fromds
= NULL
;
565 err
= dsl_pool_hold(pool
, FTAG
, &dp
);
569 err
= dsl_dataset_hold_obj(dp
, tosnap
, FTAG
, &ds
);
571 dsl_pool_rele(dp
, FTAG
);
576 err
= dsl_dataset_hold_obj(dp
, fromsnap
, FTAG
, &fromds
);
578 dsl_dataset_rele(ds
, FTAG
);
579 dsl_pool_rele(dp
, FTAG
);
584 return (dmu_send_impl(FTAG
, dp
, ds
, fromds
, outfd
, vp
, off
));
588 dmu_send(const char *tosnap
, const char *fromsnap
,
589 int outfd
, vnode_t
*vp
, offset_t
*off
)
593 dsl_dataset_t
*fromds
= NULL
;
596 if (strchr(tosnap
, '@') == NULL
)
597 return (SET_ERROR(EINVAL
));
598 if (fromsnap
!= NULL
&& strchr(fromsnap
, '@') == NULL
)
599 return (SET_ERROR(EINVAL
));
601 err
= dsl_pool_hold(tosnap
, FTAG
, &dp
);
605 err
= dsl_dataset_hold(dp
, tosnap
, FTAG
, &ds
);
607 dsl_pool_rele(dp
, FTAG
);
611 if (fromsnap
!= NULL
) {
612 err
= dsl_dataset_hold(dp
, fromsnap
, FTAG
, &fromds
);
614 dsl_dataset_rele(ds
, FTAG
);
615 dsl_pool_rele(dp
, FTAG
);
619 return (dmu_send_impl(FTAG
, dp
, ds
, fromds
, outfd
, vp
, off
));
623 dmu_send_estimate(dsl_dataset_t
*ds
, dsl_dataset_t
*fromds
, uint64_t *sizep
)
626 uint64_t size
, recordsize
;
627 ASSERTV(dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
);
629 ASSERT(dsl_pool_config_held(dp
));
631 /* tosnap must be a snapshot */
632 if (!dsl_dataset_is_snapshot(ds
))
633 return (SET_ERROR(EINVAL
));
636 * fromsnap must be an earlier snapshot from the same fs as tosnap,
637 * or the origin's fs.
639 if (fromds
!= NULL
&& !dsl_dataset_is_before(ds
, fromds
))
640 return (SET_ERROR(EXDEV
));
642 /* Get uncompressed size estimate of changed data. */
643 if (fromds
== NULL
) {
644 size
= ds
->ds_phys
->ds_uncompressed_bytes
;
647 err
= dsl_dataset_space_written(fromds
, ds
,
648 &used
, &comp
, &size
);
654 * Assume that space (both on-disk and in-stream) is dominated by
655 * data. We will adjust for indirect blocks and the copies property,
656 * but ignore per-object space used (eg, dnodes and DRR_OBJECT records).
660 * Subtract out approximate space used by indirect blocks.
661 * Assume most space is used by data blocks (non-indirect, non-dnode).
662 * Assume all blocks are recordsize. Assume ditto blocks and
663 * internal fragmentation counter out compression.
665 * Therefore, space used by indirect blocks is sizeof(blkptr_t) per
666 * block, which we observe in practice.
668 err
= dsl_prop_get_int_ds(ds
, "recordsize", &recordsize
);
671 size
-= size
/ recordsize
* sizeof (blkptr_t
);
673 /* Add in the space for the record associated with each block. */
674 size
+= size
/ recordsize
* sizeof (dmu_replay_record_t
);
681 typedef struct dmu_recv_begin_arg
{
682 const char *drba_origin
;
683 dmu_recv_cookie_t
*drba_cookie
;
685 uint64_t drba_snapobj
;
686 } dmu_recv_begin_arg_t
;
689 recv_begin_check_existing_impl(dmu_recv_begin_arg_t
*drba
, dsl_dataset_t
*ds
,
694 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
696 /* temporary clone name must not exist */
697 error
= zap_lookup(dp
->dp_meta_objset
,
698 ds
->ds_dir
->dd_phys
->dd_child_dir_zapobj
, recv_clone_name
,
701 return (error
== 0 ? EBUSY
: error
);
703 /* new snapshot name must not exist */
704 error
= zap_lookup(dp
->dp_meta_objset
,
705 ds
->ds_phys
->ds_snapnames_zapobj
, drba
->drba_cookie
->drc_tosnap
,
708 return (error
== 0 ? EEXIST
: error
);
712 uint64_t obj
= ds
->ds_phys
->ds_prev_snap_obj
;
714 /* Find snapshot in this dir that matches fromguid. */
716 error
= dsl_dataset_hold_obj(dp
, obj
, FTAG
,
719 return (SET_ERROR(ENODEV
));
720 if (snap
->ds_dir
!= ds
->ds_dir
) {
721 dsl_dataset_rele(snap
, FTAG
);
722 return (SET_ERROR(ENODEV
));
724 if (snap
->ds_phys
->ds_guid
== fromguid
)
726 obj
= snap
->ds_phys
->ds_prev_snap_obj
;
727 dsl_dataset_rele(snap
, FTAG
);
730 return (SET_ERROR(ENODEV
));
732 if (drba
->drba_cookie
->drc_force
) {
733 drba
->drba_snapobj
= obj
;
736 * If we are not forcing, there must be no
737 * changes since fromsnap.
739 if (dsl_dataset_modified_since_snap(ds
, snap
)) {
740 dsl_dataset_rele(snap
, FTAG
);
741 return (SET_ERROR(ETXTBSY
));
743 drba
->drba_snapobj
= ds
->ds_prev
->ds_object
;
746 dsl_dataset_rele(snap
, FTAG
);
748 /* if full, most recent snapshot must be $ORIGIN */
749 if (ds
->ds_phys
->ds_prev_snap_txg
>= TXG_INITIAL
)
750 return (SET_ERROR(ENODEV
));
751 drba
->drba_snapobj
= ds
->ds_phys
->ds_prev_snap_obj
;
759 dmu_recv_begin_check(void *arg
, dmu_tx_t
*tx
)
761 dmu_recv_begin_arg_t
*drba
= arg
;
762 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
763 struct drr_begin
*drrb
= drba
->drba_cookie
->drc_drrb
;
764 uint64_t fromguid
= drrb
->drr_fromguid
;
765 int flags
= drrb
->drr_flags
;
768 const char *tofs
= drba
->drba_cookie
->drc_tofs
;
770 /* already checked */
771 ASSERT3U(drrb
->drr_magic
, ==, DMU_BACKUP_MAGIC
);
773 if (DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) ==
774 DMU_COMPOUNDSTREAM
||
775 drrb
->drr_type
>= DMU_OST_NUMTYPES
||
776 ((flags
& DRR_FLAG_CLONE
) && drba
->drba_origin
== NULL
))
777 return (SET_ERROR(EINVAL
));
779 /* Verify pool version supports SA if SA_SPILL feature set */
780 if ((DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
) &
781 DMU_BACKUP_FEATURE_SA_SPILL
) &&
782 spa_version(dp
->dp_spa
) < SPA_VERSION_SA
) {
783 return (SET_ERROR(ENOTSUP
));
786 error
= dsl_dataset_hold(dp
, tofs
, FTAG
, &ds
);
788 /* target fs already exists; recv into temp clone */
790 /* Can't recv a clone into an existing fs */
791 if (flags
& DRR_FLAG_CLONE
) {
792 dsl_dataset_rele(ds
, FTAG
);
793 return (SET_ERROR(EINVAL
));
796 error
= recv_begin_check_existing_impl(drba
, ds
, fromguid
);
797 dsl_dataset_rele(ds
, FTAG
);
798 } else if (error
== ENOENT
) {
799 /* target fs does not exist; must be a full backup or clone */
800 char buf
[MAXNAMELEN
];
803 * If it's a non-clone incremental, we are missing the
804 * target fs, so fail the recv.
806 if (fromguid
!= 0 && !(flags
& DRR_FLAG_CLONE
))
807 return (SET_ERROR(ENOENT
));
809 /* Open the parent of tofs */
810 ASSERT3U(strlen(tofs
), <, MAXNAMELEN
);
811 (void) strlcpy(buf
, tofs
, strrchr(tofs
, '/') - tofs
+ 1);
812 error
= dsl_dataset_hold(dp
, buf
, FTAG
, &ds
);
816 if (drba
->drba_origin
!= NULL
) {
817 dsl_dataset_t
*origin
;
818 error
= dsl_dataset_hold(dp
, drba
->drba_origin
,
821 dsl_dataset_rele(ds
, FTAG
);
824 if (!dsl_dataset_is_snapshot(origin
)) {
825 dsl_dataset_rele(origin
, FTAG
);
826 dsl_dataset_rele(ds
, FTAG
);
827 return (SET_ERROR(EINVAL
));
829 if (origin
->ds_phys
->ds_guid
!= fromguid
) {
830 dsl_dataset_rele(origin
, FTAG
);
831 dsl_dataset_rele(ds
, FTAG
);
832 return (SET_ERROR(ENODEV
));
834 dsl_dataset_rele(origin
, FTAG
);
836 dsl_dataset_rele(ds
, FTAG
);
843 dmu_recv_begin_sync(void *arg
, dmu_tx_t
*tx
)
845 dmu_recv_begin_arg_t
*drba
= arg
;
846 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
847 struct drr_begin
*drrb
= drba
->drba_cookie
->drc_drrb
;
848 const char *tofs
= drba
->drba_cookie
->drc_tofs
;
849 dsl_dataset_t
*ds
, *newds
;
854 crflags
= (drrb
->drr_flags
& DRR_FLAG_CI_DATA
) ?
855 DS_FLAG_CI_DATASET
: 0;
857 error
= dsl_dataset_hold(dp
, tofs
, FTAG
, &ds
);
859 /* create temporary clone */
860 dsl_dataset_t
*snap
= NULL
;
861 if (drba
->drba_snapobj
!= 0) {
862 VERIFY0(dsl_dataset_hold_obj(dp
,
863 drba
->drba_snapobj
, FTAG
, &snap
));
865 dsobj
= dsl_dataset_create_sync(ds
->ds_dir
, recv_clone_name
,
866 snap
, crflags
, drba
->drba_cred
, tx
);
867 dsl_dataset_rele(snap
, FTAG
);
868 dsl_dataset_rele(ds
, FTAG
);
872 dsl_dataset_t
*origin
= NULL
;
874 VERIFY0(dsl_dir_hold(dp
, tofs
, FTAG
, &dd
, &tail
));
876 if (drba
->drba_origin
!= NULL
) {
877 VERIFY0(dsl_dataset_hold(dp
, drba
->drba_origin
,
881 /* Create new dataset. */
882 dsobj
= dsl_dataset_create_sync(dd
,
883 strrchr(tofs
, '/') + 1,
884 origin
, crflags
, drba
->drba_cred
, tx
);
886 dsl_dataset_rele(origin
, FTAG
);
887 dsl_dir_rele(dd
, FTAG
);
888 drba
->drba_cookie
->drc_newfs
= B_TRUE
;
890 VERIFY0(dsl_dataset_own_obj(dp
, dsobj
, dmu_recv_tag
, &newds
));
892 dmu_buf_will_dirty(newds
->ds_dbuf
, tx
);
893 newds
->ds_phys
->ds_flags
|= DS_FLAG_INCONSISTENT
;
896 * If we actually created a non-clone, we need to create the
897 * objset in our new dataset.
899 if (BP_IS_HOLE(dsl_dataset_get_blkptr(newds
))) {
900 (void) dmu_objset_create_impl(dp
->dp_spa
,
901 newds
, dsl_dataset_get_blkptr(newds
), drrb
->drr_type
, tx
);
904 drba
->drba_cookie
->drc_ds
= newds
;
906 spa_history_log_internal_ds(newds
, "receive", tx
, "");
910 * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
911 * succeeds; otherwise we will leak the holds on the datasets.
914 dmu_recv_begin(char *tofs
, char *tosnap
, struct drr_begin
*drrb
,
915 boolean_t force
, char *origin
, dmu_recv_cookie_t
*drc
)
917 dmu_recv_begin_arg_t drba
= { 0 };
918 dmu_replay_record_t
*drr
;
920 bzero(drc
, sizeof (dmu_recv_cookie_t
));
921 drc
->drc_drrb
= drrb
;
922 drc
->drc_tosnap
= tosnap
;
923 drc
->drc_tofs
= tofs
;
924 drc
->drc_force
= force
;
926 if (drrb
->drr_magic
== BSWAP_64(DMU_BACKUP_MAGIC
))
927 drc
->drc_byteswap
= B_TRUE
;
928 else if (drrb
->drr_magic
!= DMU_BACKUP_MAGIC
)
929 return (SET_ERROR(EINVAL
));
931 drr
= kmem_zalloc(sizeof (dmu_replay_record_t
), KM_SLEEP
);
932 drr
->drr_type
= DRR_BEGIN
;
933 drr
->drr_u
.drr_begin
= *drc
->drc_drrb
;
934 if (drc
->drc_byteswap
) {
935 fletcher_4_incremental_byteswap(drr
,
936 sizeof (dmu_replay_record_t
), &drc
->drc_cksum
);
938 fletcher_4_incremental_native(drr
,
939 sizeof (dmu_replay_record_t
), &drc
->drc_cksum
);
941 kmem_free(drr
, sizeof (dmu_replay_record_t
));
943 if (drc
->drc_byteswap
) {
944 drrb
->drr_magic
= BSWAP_64(drrb
->drr_magic
);
945 drrb
->drr_versioninfo
= BSWAP_64(drrb
->drr_versioninfo
);
946 drrb
->drr_creation_time
= BSWAP_64(drrb
->drr_creation_time
);
947 drrb
->drr_type
= BSWAP_32(drrb
->drr_type
);
948 drrb
->drr_toguid
= BSWAP_64(drrb
->drr_toguid
);
949 drrb
->drr_fromguid
= BSWAP_64(drrb
->drr_fromguid
);
952 drba
.drba_origin
= origin
;
953 drba
.drba_cookie
= drc
;
954 drba
.drba_cred
= CRED();
956 return (dsl_sync_task(tofs
, dmu_recv_begin_check
, dmu_recv_begin_sync
,
966 int bufsize
; /* amount of memory allocated for buf */
968 avl_tree_t
*guid_to_ds_map
;
971 typedef struct guid_map_entry
{
973 dsl_dataset_t
*gme_ds
;
978 guid_compare(const void *arg1
, const void *arg2
)
980 const guid_map_entry_t
*gmep1
= arg1
;
981 const guid_map_entry_t
*gmep2
= arg2
;
983 if (gmep1
->guid
< gmep2
->guid
)
985 else if (gmep1
->guid
> gmep2
->guid
)
991 free_guid_map_onexit(void *arg
)
993 avl_tree_t
*ca
= arg
;
995 guid_map_entry_t
*gmep
;
997 while ((gmep
= avl_destroy_nodes(ca
, &cookie
)) != NULL
) {
998 dsl_dataset_long_rele(gmep
->gme_ds
, gmep
);
999 dsl_dataset_rele(gmep
->gme_ds
, gmep
);
1000 kmem_free(gmep
, sizeof (guid_map_entry_t
));
1003 kmem_free(ca
, sizeof (avl_tree_t
));
1007 restore_read(struct restorearg
*ra
, int len
)
1012 /* some things will require 8-byte alignment, so everything must */
1015 while (done
< len
) {
1018 ra
->err
= vn_rdwr(UIO_READ
, ra
->vp
,
1019 (caddr_t
)ra
->buf
+ done
, len
- done
,
1020 ra
->voff
, UIO_SYSSPACE
, FAPPEND
,
1021 RLIM64_INFINITY
, CRED(), &resid
);
1023 if (resid
== len
- done
)
1024 ra
->err
= SET_ERROR(EINVAL
);
1025 ra
->voff
+= len
- done
- resid
;
1031 ASSERT3U(done
, ==, len
);
1034 fletcher_4_incremental_byteswap(rv
, len
, &ra
->cksum
);
1036 fletcher_4_incremental_native(rv
, len
, &ra
->cksum
);
1040 noinline
static void
1041 backup_byteswap(dmu_replay_record_t
*drr
)
1043 #define DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
1044 #define DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
1045 drr
->drr_type
= BSWAP_32(drr
->drr_type
);
1046 drr
->drr_payloadlen
= BSWAP_32(drr
->drr_payloadlen
);
1047 switch (drr
->drr_type
) {
1049 DO64(drr_begin
.drr_magic
);
1050 DO64(drr_begin
.drr_versioninfo
);
1051 DO64(drr_begin
.drr_creation_time
);
1052 DO32(drr_begin
.drr_type
);
1053 DO32(drr_begin
.drr_flags
);
1054 DO64(drr_begin
.drr_toguid
);
1055 DO64(drr_begin
.drr_fromguid
);
1058 DO64(drr_object
.drr_object
);
1059 /* DO64(drr_object.drr_allocation_txg); */
1060 DO32(drr_object
.drr_type
);
1061 DO32(drr_object
.drr_bonustype
);
1062 DO32(drr_object
.drr_blksz
);
1063 DO32(drr_object
.drr_bonuslen
);
1064 DO64(drr_object
.drr_toguid
);
1066 case DRR_FREEOBJECTS
:
1067 DO64(drr_freeobjects
.drr_firstobj
);
1068 DO64(drr_freeobjects
.drr_numobjs
);
1069 DO64(drr_freeobjects
.drr_toguid
);
1072 DO64(drr_write
.drr_object
);
1073 DO32(drr_write
.drr_type
);
1074 DO64(drr_write
.drr_offset
);
1075 DO64(drr_write
.drr_length
);
1076 DO64(drr_write
.drr_toguid
);
1077 DO64(drr_write
.drr_key
.ddk_cksum
.zc_word
[0]);
1078 DO64(drr_write
.drr_key
.ddk_cksum
.zc_word
[1]);
1079 DO64(drr_write
.drr_key
.ddk_cksum
.zc_word
[2]);
1080 DO64(drr_write
.drr_key
.ddk_cksum
.zc_word
[3]);
1081 DO64(drr_write
.drr_key
.ddk_prop
);
1083 case DRR_WRITE_BYREF
:
1084 DO64(drr_write_byref
.drr_object
);
1085 DO64(drr_write_byref
.drr_offset
);
1086 DO64(drr_write_byref
.drr_length
);
1087 DO64(drr_write_byref
.drr_toguid
);
1088 DO64(drr_write_byref
.drr_refguid
);
1089 DO64(drr_write_byref
.drr_refobject
);
1090 DO64(drr_write_byref
.drr_refoffset
);
1091 DO64(drr_write_byref
.drr_key
.ddk_cksum
.zc_word
[0]);
1092 DO64(drr_write_byref
.drr_key
.ddk_cksum
.zc_word
[1]);
1093 DO64(drr_write_byref
.drr_key
.ddk_cksum
.zc_word
[2]);
1094 DO64(drr_write_byref
.drr_key
.ddk_cksum
.zc_word
[3]);
1095 DO64(drr_write_byref
.drr_key
.ddk_prop
);
1098 DO64(drr_free
.drr_object
);
1099 DO64(drr_free
.drr_offset
);
1100 DO64(drr_free
.drr_length
);
1101 DO64(drr_free
.drr_toguid
);
1104 DO64(drr_spill
.drr_object
);
1105 DO64(drr_spill
.drr_length
);
1106 DO64(drr_spill
.drr_toguid
);
1109 DO64(drr_end
.drr_checksum
.zc_word
[0]);
1110 DO64(drr_end
.drr_checksum
.zc_word
[1]);
1111 DO64(drr_end
.drr_checksum
.zc_word
[2]);
1112 DO64(drr_end
.drr_checksum
.zc_word
[3]);
1113 DO64(drr_end
.drr_toguid
);
1123 restore_object(struct restorearg
*ra
, objset_t
*os
, struct drr_object
*drro
)
1129 if (drro
->drr_type
== DMU_OT_NONE
||
1130 !DMU_OT_IS_VALID(drro
->drr_type
) ||
1131 !DMU_OT_IS_VALID(drro
->drr_bonustype
) ||
1132 drro
->drr_checksumtype
>= ZIO_CHECKSUM_FUNCTIONS
||
1133 drro
->drr_compress
>= ZIO_COMPRESS_FUNCTIONS
||
1134 P2PHASE(drro
->drr_blksz
, SPA_MINBLOCKSIZE
) ||
1135 drro
->drr_blksz
< SPA_MINBLOCKSIZE
||
1136 drro
->drr_blksz
> SPA_MAXBLOCKSIZE
||
1137 drro
->drr_bonuslen
> DN_MAX_BONUSLEN
) {
1138 return (SET_ERROR(EINVAL
));
1141 err
= dmu_object_info(os
, drro
->drr_object
, NULL
);
1143 if (err
!= 0 && err
!= ENOENT
)
1144 return (SET_ERROR(EINVAL
));
1146 if (drro
->drr_bonuslen
) {
1147 data
= restore_read(ra
, P2ROUNDUP(drro
->drr_bonuslen
, 8));
1152 if (err
== ENOENT
) {
1153 /* currently free, want to be allocated */
1154 tx
= dmu_tx_create(os
);
1155 dmu_tx_hold_bonus(tx
, DMU_NEW_OBJECT
);
1156 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1161 err
= dmu_object_claim(os
, drro
->drr_object
,
1162 drro
->drr_type
, drro
->drr_blksz
,
1163 drro
->drr_bonustype
, drro
->drr_bonuslen
, tx
);
1166 /* currently allocated, want to be allocated */
1167 err
= dmu_object_reclaim(os
, drro
->drr_object
,
1168 drro
->drr_type
, drro
->drr_blksz
,
1169 drro
->drr_bonustype
, drro
->drr_bonuslen
);
1172 return (SET_ERROR(EINVAL
));
1175 tx
= dmu_tx_create(os
);
1176 dmu_tx_hold_bonus(tx
, drro
->drr_object
);
1177 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1183 dmu_object_set_checksum(os
, drro
->drr_object
, drro
->drr_checksumtype
,
1185 dmu_object_set_compress(os
, drro
->drr_object
, drro
->drr_compress
, tx
);
1190 VERIFY(0 == dmu_bonus_hold(os
, drro
->drr_object
, FTAG
, &db
));
1191 dmu_buf_will_dirty(db
, tx
);
1193 ASSERT3U(db
->db_size
, >=, drro
->drr_bonuslen
);
1194 bcopy(data
, db
->db_data
, drro
->drr_bonuslen
);
1196 dmu_object_byteswap_t byteswap
=
1197 DMU_OT_BYTESWAP(drro
->drr_bonustype
);
1198 dmu_ot_byteswap
[byteswap
].ob_func(db
->db_data
,
1199 drro
->drr_bonuslen
);
1201 dmu_buf_rele(db
, FTAG
);
1209 restore_freeobjects(struct restorearg
*ra
, objset_t
*os
,
1210 struct drr_freeobjects
*drrfo
)
1214 if (drrfo
->drr_firstobj
+ drrfo
->drr_numobjs
< drrfo
->drr_firstobj
)
1215 return (SET_ERROR(EINVAL
));
1217 for (obj
= drrfo
->drr_firstobj
;
1218 obj
< drrfo
->drr_firstobj
+ drrfo
->drr_numobjs
;
1219 (void) dmu_object_next(os
, &obj
, FALSE
, 0)) {
1222 if (dmu_object_info(os
, obj
, NULL
) != 0)
1225 err
= dmu_free_object(os
, obj
);
1233 restore_write(struct restorearg
*ra
, objset_t
*os
,
1234 struct drr_write
*drrw
)
1240 if (drrw
->drr_offset
+ drrw
->drr_length
< drrw
->drr_offset
||
1241 !DMU_OT_IS_VALID(drrw
->drr_type
))
1242 return (SET_ERROR(EINVAL
));
1244 data
= restore_read(ra
, drrw
->drr_length
);
1248 if (dmu_object_info(os
, drrw
->drr_object
, NULL
) != 0)
1249 return (SET_ERROR(EINVAL
));
1251 tx
= dmu_tx_create(os
);
1253 dmu_tx_hold_write(tx
, drrw
->drr_object
,
1254 drrw
->drr_offset
, drrw
->drr_length
);
1255 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1261 dmu_object_byteswap_t byteswap
=
1262 DMU_OT_BYTESWAP(drrw
->drr_type
);
1263 dmu_ot_byteswap
[byteswap
].ob_func(data
, drrw
->drr_length
);
1265 dmu_write(os
, drrw
->drr_object
,
1266 drrw
->drr_offset
, drrw
->drr_length
, data
, tx
);
1272 * Handle a DRR_WRITE_BYREF record. This record is used in dedup'ed
1273 * streams to refer to a copy of the data that is already on the
1274 * system because it came in earlier in the stream. This function
1275 * finds the earlier copy of the data, and uses that copy instead of
1276 * data from the stream to fulfill this write.
1279 restore_write_byref(struct restorearg
*ra
, objset_t
*os
,
1280 struct drr_write_byref
*drrwbr
)
1284 guid_map_entry_t gmesrch
;
1285 guid_map_entry_t
*gmep
;
1287 objset_t
*ref_os
= NULL
;
1290 if (drrwbr
->drr_offset
+ drrwbr
->drr_length
< drrwbr
->drr_offset
)
1291 return (SET_ERROR(EINVAL
));
1294 * If the GUID of the referenced dataset is different from the
1295 * GUID of the target dataset, find the referenced dataset.
1297 if (drrwbr
->drr_toguid
!= drrwbr
->drr_refguid
) {
1298 gmesrch
.guid
= drrwbr
->drr_refguid
;
1299 if ((gmep
= avl_find(ra
->guid_to_ds_map
, &gmesrch
,
1301 return (SET_ERROR(EINVAL
));
1303 if (dmu_objset_from_ds(gmep
->gme_ds
, &ref_os
))
1304 return (SET_ERROR(EINVAL
));
1309 err
= dmu_buf_hold(ref_os
, drrwbr
->drr_refobject
,
1310 drrwbr
->drr_refoffset
, FTAG
, &dbp
, DMU_READ_PREFETCH
);
1314 tx
= dmu_tx_create(os
);
1316 dmu_tx_hold_write(tx
, drrwbr
->drr_object
,
1317 drrwbr
->drr_offset
, drrwbr
->drr_length
);
1318 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1323 dmu_write(os
, drrwbr
->drr_object
,
1324 drrwbr
->drr_offset
, drrwbr
->drr_length
, dbp
->db_data
, tx
);
1325 dmu_buf_rele(dbp
, FTAG
);
1331 restore_spill(struct restorearg
*ra
, objset_t
*os
, struct drr_spill
*drrs
)
1335 dmu_buf_t
*db
, *db_spill
;
1338 if (drrs
->drr_length
< SPA_MINBLOCKSIZE
||
1339 drrs
->drr_length
> SPA_MAXBLOCKSIZE
)
1340 return (SET_ERROR(EINVAL
));
1342 data
= restore_read(ra
, drrs
->drr_length
);
1346 if (dmu_object_info(os
, drrs
->drr_object
, NULL
) != 0)
1347 return (SET_ERROR(EINVAL
));
1349 VERIFY(0 == dmu_bonus_hold(os
, drrs
->drr_object
, FTAG
, &db
));
1350 if ((err
= dmu_spill_hold_by_bonus(db
, FTAG
, &db_spill
)) != 0) {
1351 dmu_buf_rele(db
, FTAG
);
1355 tx
= dmu_tx_create(os
);
1357 dmu_tx_hold_spill(tx
, db
->db_object
);
1359 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1361 dmu_buf_rele(db
, FTAG
);
1362 dmu_buf_rele(db_spill
, FTAG
);
1366 dmu_buf_will_dirty(db_spill
, tx
);
1368 if (db_spill
->db_size
< drrs
->drr_length
)
1369 VERIFY(0 == dbuf_spill_set_blksz(db_spill
,
1370 drrs
->drr_length
, tx
));
1371 bcopy(data
, db_spill
->db_data
, drrs
->drr_length
);
1373 dmu_buf_rele(db
, FTAG
);
1374 dmu_buf_rele(db_spill
, FTAG
);
1382 restore_free(struct restorearg
*ra
, objset_t
*os
,
1383 struct drr_free
*drrf
)
1387 if (drrf
->drr_length
!= -1ULL &&
1388 drrf
->drr_offset
+ drrf
->drr_length
< drrf
->drr_offset
)
1389 return (SET_ERROR(EINVAL
));
1391 if (dmu_object_info(os
, drrf
->drr_object
, NULL
) != 0)
1392 return (SET_ERROR(EINVAL
));
1394 err
= dmu_free_long_range(os
, drrf
->drr_object
,
1395 drrf
->drr_offset
, drrf
->drr_length
);
1399 /* used to destroy the drc_ds on error */
1401 dmu_recv_cleanup_ds(dmu_recv_cookie_t
*drc
)
1403 char name
[MAXNAMELEN
];
1404 dsl_dataset_name(drc
->drc_ds
, name
);
1405 dsl_dataset_disown(drc
->drc_ds
, dmu_recv_tag
);
1406 (void) dsl_destroy_head(name
);
1410 * NB: callers *must* call dmu_recv_end() if this succeeds.
1413 dmu_recv_stream(dmu_recv_cookie_t
*drc
, vnode_t
*vp
, offset_t
*voffp
,
1414 int cleanup_fd
, uint64_t *action_handlep
)
1416 struct restorearg ra
= { 0 };
1417 dmu_replay_record_t
*drr
;
1422 ra
.byteswap
= drc
->drc_byteswap
;
1423 ra
.cksum
= drc
->drc_cksum
;
1427 ra
.buf
= vmem_alloc(ra
.bufsize
, KM_SLEEP
);
1429 /* these were verified in dmu_recv_begin */
1430 ASSERT3U(DMU_GET_STREAM_HDRTYPE(drc
->drc_drrb
->drr_versioninfo
), ==,
1432 ASSERT3U(drc
->drc_drrb
->drr_type
, <, DMU_OST_NUMTYPES
);
1435 * Open the objset we are modifying.
1437 VERIFY0(dmu_objset_from_ds(drc
->drc_ds
, &os
));
1439 ASSERT(drc
->drc_ds
->ds_phys
->ds_flags
& DS_FLAG_INCONSISTENT
);
1441 featureflags
= DMU_GET_FEATUREFLAGS(drc
->drc_drrb
->drr_versioninfo
);
1443 /* if this stream is dedup'ed, set up the avl tree for guid mapping */
1444 if (featureflags
& DMU_BACKUP_FEATURE_DEDUP
) {
1447 if (cleanup_fd
== -1) {
1448 ra
.err
= SET_ERROR(EBADF
);
1451 ra
.err
= zfs_onexit_fd_hold(cleanup_fd
, &minor
);
1457 if (*action_handlep
== 0) {
1459 kmem_alloc(sizeof (avl_tree_t
), KM_SLEEP
);
1460 avl_create(ra
.guid_to_ds_map
, guid_compare
,
1461 sizeof (guid_map_entry_t
),
1462 offsetof(guid_map_entry_t
, avlnode
));
1463 ra
.err
= zfs_onexit_add_cb(minor
,
1464 free_guid_map_onexit
, ra
.guid_to_ds_map
,
1469 ra
.err
= zfs_onexit_cb_data(minor
, *action_handlep
,
1470 (void **)&ra
.guid_to_ds_map
);
1475 drc
->drc_guid_to_ds_map
= ra
.guid_to_ds_map
;
1479 * Read records and process them.
1482 while (ra
.err
== 0 &&
1483 NULL
!= (drr
= restore_read(&ra
, sizeof (*drr
)))) {
1484 if (issig(JUSTLOOKING
) && issig(FORREAL
)) {
1485 ra
.err
= SET_ERROR(EINTR
);
1490 backup_byteswap(drr
);
1492 switch (drr
->drr_type
) {
1496 * We need to make a copy of the record header,
1497 * because restore_{object,write} may need to
1498 * restore_read(), which will invalidate drr.
1500 struct drr_object drro
= drr
->drr_u
.drr_object
;
1501 ra
.err
= restore_object(&ra
, os
, &drro
);
1504 case DRR_FREEOBJECTS
:
1506 struct drr_freeobjects drrfo
=
1507 drr
->drr_u
.drr_freeobjects
;
1508 ra
.err
= restore_freeobjects(&ra
, os
, &drrfo
);
1513 struct drr_write drrw
= drr
->drr_u
.drr_write
;
1514 ra
.err
= restore_write(&ra
, os
, &drrw
);
1517 case DRR_WRITE_BYREF
:
1519 struct drr_write_byref drrwbr
=
1520 drr
->drr_u
.drr_write_byref
;
1521 ra
.err
= restore_write_byref(&ra
, os
, &drrwbr
);
1526 struct drr_free drrf
= drr
->drr_u
.drr_free
;
1527 ra
.err
= restore_free(&ra
, os
, &drrf
);
1532 struct drr_end drre
= drr
->drr_u
.drr_end
;
1534 * We compare against the *previous* checksum
1535 * value, because the stored checksum is of
1536 * everything before the DRR_END record.
1538 if (!ZIO_CHECKSUM_EQUAL(drre
.drr_checksum
, pcksum
))
1539 ra
.err
= SET_ERROR(ECKSUM
);
1544 struct drr_spill drrs
= drr
->drr_u
.drr_spill
;
1545 ra
.err
= restore_spill(&ra
, os
, &drrs
);
1549 ra
.err
= SET_ERROR(EINVAL
);
1554 ASSERT(ra
.err
!= 0);
1557 if ((featureflags
& DMU_BACKUP_FEATURE_DEDUP
) && (cleanup_fd
!= -1))
1558 zfs_onexit_fd_rele(cleanup_fd
);
1562 * destroy what we created, so we don't leave it in the
1563 * inconsistent restoring state.
1565 dmu_recv_cleanup_ds(drc
);
1568 vmem_free(ra
.buf
, ra
.bufsize
);
1574 dmu_recv_end_check(void *arg
, dmu_tx_t
*tx
)
1576 dmu_recv_cookie_t
*drc
= arg
;
1577 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1580 ASSERT3P(drc
->drc_ds
->ds_owner
, ==, dmu_recv_tag
);
1582 if (!drc
->drc_newfs
) {
1583 dsl_dataset_t
*origin_head
;
1585 error
= dsl_dataset_hold(dp
, drc
->drc_tofs
, FTAG
, &origin_head
);
1588 if (drc
->drc_force
) {
1590 * We will destroy any snapshots in tofs (i.e. before
1591 * origin_head) that are after the origin (which is
1592 * the snap before drc_ds, because drc_ds can not
1593 * have any snaps of its own).
1595 uint64_t obj
= origin_head
->ds_phys
->ds_prev_snap_obj
;
1596 while (obj
!= drc
->drc_ds
->ds_phys
->ds_prev_snap_obj
) {
1597 dsl_dataset_t
*snap
;
1598 error
= dsl_dataset_hold_obj(dp
, obj
, FTAG
,
1602 if (snap
->ds_dir
!= origin_head
->ds_dir
)
1603 error
= SET_ERROR(EINVAL
);
1605 error
= dsl_destroy_snapshot_check_impl(
1608 obj
= snap
->ds_phys
->ds_prev_snap_obj
;
1609 dsl_dataset_rele(snap
, FTAG
);
1614 error
= dsl_dataset_clone_swap_check_impl(drc
->drc_ds
,
1615 origin_head
, drc
->drc_force
);
1617 dsl_dataset_rele(origin_head
, FTAG
);
1620 error
= dsl_dataset_snapshot_check_impl(origin_head
,
1621 drc
->drc_tosnap
, tx
, B_TRUE
);
1622 dsl_dataset_rele(origin_head
, FTAG
);
1626 error
= dsl_destroy_head_check_impl(drc
->drc_ds
, 1);
1628 error
= dsl_dataset_snapshot_check_impl(drc
->drc_ds
,
1629 drc
->drc_tosnap
, tx
, B_TRUE
);
1635 dmu_recv_end_sync(void *arg
, dmu_tx_t
*tx
)
1637 dmu_recv_cookie_t
*drc
= arg
;
1638 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1640 spa_history_log_internal_ds(drc
->drc_ds
, "finish receiving",
1641 tx
, "snap=%s", drc
->drc_tosnap
);
1643 if (!drc
->drc_newfs
) {
1644 dsl_dataset_t
*origin_head
;
1646 VERIFY0(dsl_dataset_hold(dp
, drc
->drc_tofs
, FTAG
,
1649 if (drc
->drc_force
) {
1651 * Destroy any snapshots of drc_tofs (origin_head)
1652 * after the origin (the snap before drc_ds).
1654 uint64_t obj
= origin_head
->ds_phys
->ds_prev_snap_obj
;
1655 while (obj
!= drc
->drc_ds
->ds_phys
->ds_prev_snap_obj
) {
1656 dsl_dataset_t
*snap
;
1657 VERIFY0(dsl_dataset_hold_obj(dp
, obj
, FTAG
,
1659 ASSERT3P(snap
->ds_dir
, ==, origin_head
->ds_dir
);
1660 obj
= snap
->ds_phys
->ds_prev_snap_obj
;
1661 dsl_destroy_snapshot_sync_impl(snap
,
1663 dsl_dataset_rele(snap
, FTAG
);
1666 VERIFY3P(drc
->drc_ds
->ds_prev
, ==,
1667 origin_head
->ds_prev
);
1669 dsl_dataset_clone_swap_sync_impl(drc
->drc_ds
,
1671 dsl_dataset_snapshot_sync_impl(origin_head
,
1672 drc
->drc_tosnap
, tx
);
1674 /* set snapshot's creation time and guid */
1675 dmu_buf_will_dirty(origin_head
->ds_prev
->ds_dbuf
, tx
);
1676 origin_head
->ds_prev
->ds_phys
->ds_creation_time
=
1677 drc
->drc_drrb
->drr_creation_time
;
1678 origin_head
->ds_prev
->ds_phys
->ds_guid
=
1679 drc
->drc_drrb
->drr_toguid
;
1680 origin_head
->ds_prev
->ds_phys
->ds_flags
&=
1681 ~DS_FLAG_INCONSISTENT
;
1683 dmu_buf_will_dirty(origin_head
->ds_dbuf
, tx
);
1684 origin_head
->ds_phys
->ds_flags
&= ~DS_FLAG_INCONSISTENT
;
1686 dsl_dataset_rele(origin_head
, FTAG
);
1687 dsl_destroy_head_sync_impl(drc
->drc_ds
, tx
);
1689 dsl_dataset_t
*ds
= drc
->drc_ds
;
1691 dsl_dataset_snapshot_sync_impl(ds
, drc
->drc_tosnap
, tx
);
1693 /* set snapshot's creation time and guid */
1694 dmu_buf_will_dirty(ds
->ds_prev
->ds_dbuf
, tx
);
1695 ds
->ds_prev
->ds_phys
->ds_creation_time
=
1696 drc
->drc_drrb
->drr_creation_time
;
1697 ds
->ds_prev
->ds_phys
->ds_guid
= drc
->drc_drrb
->drr_toguid
;
1698 ds
->ds_prev
->ds_phys
->ds_flags
&= ~DS_FLAG_INCONSISTENT
;
1700 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
1701 ds
->ds_phys
->ds_flags
&= ~DS_FLAG_INCONSISTENT
;
1703 drc
->drc_newsnapobj
= drc
->drc_ds
->ds_phys
->ds_prev_snap_obj
;
1705 * Release the hold from dmu_recv_begin. This must be done before
1706 * we return to open context, so that when we free the dataset's dnode,
1707 * we can evict its bonus buffer.
1709 dsl_dataset_disown(drc
->drc_ds
, dmu_recv_tag
);
1714 add_ds_to_guidmap(const char *name
, avl_tree_t
*guid_map
, uint64_t snapobj
)
1717 dsl_dataset_t
*snapds
;
1718 guid_map_entry_t
*gmep
;
1721 ASSERT(guid_map
!= NULL
);
1723 err
= dsl_pool_hold(name
, FTAG
, &dp
);
1726 gmep
= kmem_alloc(sizeof (*gmep
), KM_SLEEP
);
1727 err
= dsl_dataset_hold_obj(dp
, snapobj
, gmep
, &snapds
);
1729 gmep
->guid
= snapds
->ds_phys
->ds_guid
;
1730 gmep
->gme_ds
= snapds
;
1731 avl_add(guid_map
, gmep
);
1732 dsl_dataset_long_hold(snapds
, gmep
);
1734 kmem_free(gmep
, sizeof (*gmep
));
1737 dsl_pool_rele(dp
, FTAG
);
1741 static int dmu_recv_end_modified_blocks
= 3;
1744 dmu_recv_existing_end(dmu_recv_cookie_t
*drc
)
1752 * We will be destroying the ds; make sure its origin is unmounted if
1755 name
= kmem_alloc(MAXNAMELEN
, KM_SLEEP
);
1756 dsl_dataset_name(drc
->drc_ds
, name
);
1757 zfs_destroy_unmount_origin(name
);
1758 kmem_free(name
, MAXNAMELEN
);
1761 error
= dsl_sync_task(drc
->drc_tofs
,
1762 dmu_recv_end_check
, dmu_recv_end_sync
, drc
,
1763 dmu_recv_end_modified_blocks
);
1766 dmu_recv_cleanup_ds(drc
);
1771 dmu_recv_new_end(dmu_recv_cookie_t
*drc
)
1775 error
= dsl_sync_task(drc
->drc_tofs
,
1776 dmu_recv_end_check
, dmu_recv_end_sync
, drc
,
1777 dmu_recv_end_modified_blocks
);
1780 dmu_recv_cleanup_ds(drc
);
1781 } else if (drc
->drc_guid_to_ds_map
!= NULL
) {
1782 (void) add_ds_to_guidmap(drc
->drc_tofs
,
1783 drc
->drc_guid_to_ds_map
,
1784 drc
->drc_newsnapobj
);
1790 dmu_recv_end(dmu_recv_cookie_t
*drc
)
1793 return (dmu_recv_new_end(drc
));
1795 return (dmu_recv_existing_end(drc
));