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]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
25 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
26 * Copyright (c) 2013 Steven Hartland. All rights reserved.
39 #include <sys/mount.h>
45 #include <libzfs_core.h>
47 #include "zfs_namecheck.h"
49 #include "zfs_fletcher.h"
50 #include "libzfs_impl.h"
52 #include <sys/zio_checksum.h>
55 /* in libzfs_dataset.c */
56 extern void zfs_setprop_error(libzfs_handle_t
*, zfs_prop_t
, int, char *);
58 static int zfs_receive_impl(libzfs_handle_t
*, const char *, const char *,
59 recvflags_t
*, int, const char *, nvlist_t
*, avl_tree_t
*, char **, int,
62 static const zio_cksum_t zero_cksum
= { 0 };
64 typedef struct dedup_arg
{
67 libzfs_handle_t
*dedup_hdl
;
70 typedef struct progress_arg
{
73 boolean_t pa_parsable
;
76 typedef struct dataref
{
82 typedef struct dedup_entry
{
83 struct dedup_entry
*dde_next
;
84 zio_cksum_t dde_chksum
;
89 #define MAX_DDT_PHYSMEM_PERCENT 20
90 #define SMALLEST_POSSIBLE_MAX_DDT_MB 128
92 typedef struct dedup_table
{
93 dedup_entry_t
**dedup_hash_array
;
94 umem_cache_t
*ddecache
;
95 uint64_t max_ddt_size
; /* max dedup table size in bytes */
96 uint64_t cur_ddt_size
; /* current dedup table size in bytes */
103 high_order_bit(uint64_t n
)
107 for (count
= 0; n
!= 0; count
++)
113 ssread(void *buf
, size_t len
, FILE *stream
)
117 if ((outlen
= fread(buf
, len
, 1, stream
)) == 0)
124 ddt_hash_append(libzfs_handle_t
*hdl
, dedup_table_t
*ddt
, dedup_entry_t
**ddepp
,
125 zio_cksum_t
*cs
, uint64_t prop
, dataref_t
*dr
)
129 if (ddt
->cur_ddt_size
>= ddt
->max_ddt_size
) {
130 if (ddt
->ddt_full
== B_FALSE
) {
131 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
132 "Dedup table full. Deduplication will continue "
133 "with existing table entries"));
134 ddt
->ddt_full
= B_TRUE
;
139 if ((dde
= umem_cache_alloc(ddt
->ddecache
, UMEM_DEFAULT
))
141 assert(*ddepp
== NULL
);
142 dde
->dde_next
= NULL
;
143 dde
->dde_chksum
= *cs
;
144 dde
->dde_prop
= prop
;
147 ddt
->cur_ddt_size
+= sizeof (dedup_entry_t
);
153 * Using the specified dedup table, do a lookup for an entry with
154 * the checksum cs. If found, return the block's reference info
155 * in *dr. Otherwise, insert a new entry in the dedup table, using
156 * the reference information specified by *dr.
158 * return value: true - entry was found
159 * false - entry was not found
162 ddt_update(libzfs_handle_t
*hdl
, dedup_table_t
*ddt
, zio_cksum_t
*cs
,
163 uint64_t prop
, dataref_t
*dr
)
166 dedup_entry_t
**ddepp
;
168 hashcode
= BF64_GET(cs
->zc_word
[0], 0, ddt
->numhashbits
);
170 for (ddepp
= &(ddt
->dedup_hash_array
[hashcode
]); *ddepp
!= NULL
;
171 ddepp
= &((*ddepp
)->dde_next
)) {
172 if (ZIO_CHECKSUM_EQUAL(((*ddepp
)->dde_chksum
), *cs
) &&
173 (*ddepp
)->dde_prop
== prop
) {
174 *dr
= (*ddepp
)->dde_ref
;
178 ddt_hash_append(hdl
, ddt
, ddepp
, cs
, prop
, dr
);
183 dump_record(dmu_replay_record_t
*drr
, void *payload
, int payload_len
,
184 zio_cksum_t
*zc
, int outfd
)
186 ASSERT3U(offsetof(dmu_replay_record_t
, drr_u
.drr_checksum
.drr_checksum
),
187 ==, sizeof (dmu_replay_record_t
) - sizeof (zio_cksum_t
));
188 fletcher_4_incremental_native(drr
,
189 offsetof(dmu_replay_record_t
, drr_u
.drr_checksum
.drr_checksum
), zc
);
190 if (drr
->drr_type
!= DRR_BEGIN
) {
191 ASSERT(ZIO_CHECKSUM_IS_ZERO(&drr
->drr_u
.
192 drr_checksum
.drr_checksum
));
193 drr
->drr_u
.drr_checksum
.drr_checksum
= *zc
;
195 fletcher_4_incremental_native(&drr
->drr_u
.drr_checksum
.drr_checksum
,
196 sizeof (zio_cksum_t
), zc
);
197 if (write(outfd
, drr
, sizeof (*drr
)) == -1)
199 if (payload_len
!= 0) {
200 fletcher_4_incremental_native(payload
, payload_len
, zc
);
201 if (write(outfd
, payload
, payload_len
) == -1)
208 * This function is started in a separate thread when the dedup option
209 * has been requested. The main send thread determines the list of
210 * snapshots to be included in the send stream and makes the ioctl calls
211 * for each one. But instead of having the ioctl send the output to the
212 * the output fd specified by the caller of zfs_send()), the
213 * ioctl is told to direct the output to a pipe, which is read by the
214 * alternate thread running THIS function. This function does the
216 * 1. building a dedup table (the DDT)
217 * 2. doing checksums on each data block and inserting a record in the DDT
218 * 3. looking for matching checksums, and
219 * 4. sending a DRR_WRITE_BYREF record instead of a write record whenever
220 * a duplicate block is found.
221 * The output of this function then goes to the output fd requested
222 * by the caller of zfs_send().
227 dedup_arg_t
*dda
= arg
;
228 char *buf
= zfs_alloc(dda
->dedup_hdl
, SPA_MAXBLOCKSIZE
);
229 dmu_replay_record_t thedrr
;
230 dmu_replay_record_t
*drr
= &thedrr
;
234 zio_cksum_t stream_cksum
;
235 uint64_t physmem
= sysconf(_SC_PHYS_PAGES
) * sysconf(_SC_PAGESIZE
);
239 MAX((physmem
* MAX_DDT_PHYSMEM_PERCENT
) / 100,
240 SMALLEST_POSSIBLE_MAX_DDT_MB
<< 20);
242 numbuckets
= ddt
.max_ddt_size
/ (sizeof (dedup_entry_t
));
245 * numbuckets must be a power of 2. Increase number to
246 * a power of 2 if necessary.
248 if (!ISP2(numbuckets
))
249 numbuckets
= 1 << high_order_bit(numbuckets
);
251 ddt
.dedup_hash_array
= calloc(numbuckets
, sizeof (dedup_entry_t
*));
252 ddt
.ddecache
= umem_cache_create("dde", sizeof (dedup_entry_t
), 0,
253 NULL
, NULL
, NULL
, NULL
, NULL
, 0);
254 ddt
.cur_ddt_size
= numbuckets
* sizeof (dedup_entry_t
*);
255 ddt
.numhashbits
= high_order_bit(numbuckets
) - 1;
256 ddt
.ddt_full
= B_FALSE
;
258 outfd
= dda
->outputfd
;
259 ofp
= fdopen(dda
->inputfd
, "r");
260 while (ssread(drr
, sizeof (*drr
), ofp
) != 0) {
262 switch (drr
->drr_type
) {
265 struct drr_begin
*drrb
= &drr
->drr_u
.drr_begin
;
268 ZIO_SET_CHECKSUM(&stream_cksum
, 0, 0, 0, 0);
270 ASSERT3U(drrb
->drr_magic
, ==, DMU_BACKUP_MAGIC
);
272 /* set the DEDUP feature flag for this stream */
273 fflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
274 fflags
|= (DMU_BACKUP_FEATURE_DEDUP
|
275 DMU_BACKUP_FEATURE_DEDUPPROPS
);
276 DMU_SET_FEATUREFLAGS(drrb
->drr_versioninfo
, fflags
);
278 if (DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) ==
279 DMU_COMPOUNDSTREAM
&& drr
->drr_payloadlen
!= 0) {
280 sz
= drr
->drr_payloadlen
;
282 if (sz
> SPA_MAXBLOCKSIZE
) {
283 buf
= zfs_realloc(dda
->dedup_hdl
, buf
,
284 SPA_MAXBLOCKSIZE
, sz
);
286 (void) ssread(buf
, sz
, ofp
);
290 if (dump_record(drr
, buf
, sz
, &stream_cksum
,
298 struct drr_end
*drre
= &drr
->drr_u
.drr_end
;
299 /* use the recalculated checksum */
300 drre
->drr_checksum
= stream_cksum
;
301 if (dump_record(drr
, NULL
, 0, &stream_cksum
,
309 struct drr_object
*drro
= &drr
->drr_u
.drr_object
;
310 if (drro
->drr_bonuslen
> 0) {
312 P2ROUNDUP((uint64_t)drro
->drr_bonuslen
, 8),
315 if (dump_record(drr
, buf
,
316 P2ROUNDUP((uint64_t)drro
->drr_bonuslen
, 8),
317 &stream_cksum
, outfd
) != 0)
324 struct drr_spill
*drrs
= &drr
->drr_u
.drr_spill
;
325 (void) ssread(buf
, drrs
->drr_length
, ofp
);
326 if (dump_record(drr
, buf
, drrs
->drr_length
,
327 &stream_cksum
, outfd
) != 0)
332 case DRR_FREEOBJECTS
:
334 if (dump_record(drr
, NULL
, 0, &stream_cksum
,
342 struct drr_write
*drrw
= &drr
->drr_u
.drr_write
;
345 (void) ssread(buf
, drrw
->drr_length
, ofp
);
348 * Use the existing checksum if it's dedup-capable,
349 * else calculate a SHA256 checksum for it.
352 if (ZIO_CHECKSUM_EQUAL(drrw
->drr_key
.ddk_cksum
,
354 !DRR_IS_DEDUP_CAPABLE(drrw
->drr_checksumflags
)) {
356 zio_cksum_t tmpsha256
;
359 SHA256Update(&ctx
, buf
, drrw
->drr_length
);
360 SHA256Final(&tmpsha256
, &ctx
);
361 drrw
->drr_key
.ddk_cksum
.zc_word
[0] =
362 BE_64(tmpsha256
.zc_word
[0]);
363 drrw
->drr_key
.ddk_cksum
.zc_word
[1] =
364 BE_64(tmpsha256
.zc_word
[1]);
365 drrw
->drr_key
.ddk_cksum
.zc_word
[2] =
366 BE_64(tmpsha256
.zc_word
[2]);
367 drrw
->drr_key
.ddk_cksum
.zc_word
[3] =
368 BE_64(tmpsha256
.zc_word
[3]);
369 drrw
->drr_checksumtype
= ZIO_CHECKSUM_SHA256
;
370 drrw
->drr_checksumflags
= DRR_CHECKSUM_DEDUP
;
373 dataref
.ref_guid
= drrw
->drr_toguid
;
374 dataref
.ref_object
= drrw
->drr_object
;
375 dataref
.ref_offset
= drrw
->drr_offset
;
377 if (ddt_update(dda
->dedup_hdl
, &ddt
,
378 &drrw
->drr_key
.ddk_cksum
, drrw
->drr_key
.ddk_prop
,
380 dmu_replay_record_t wbr_drr
= {0};
381 struct drr_write_byref
*wbr_drrr
=
382 &wbr_drr
.drr_u
.drr_write_byref
;
384 /* block already present in stream */
385 wbr_drr
.drr_type
= DRR_WRITE_BYREF
;
387 wbr_drrr
->drr_object
= drrw
->drr_object
;
388 wbr_drrr
->drr_offset
= drrw
->drr_offset
;
389 wbr_drrr
->drr_length
= drrw
->drr_length
;
390 wbr_drrr
->drr_toguid
= drrw
->drr_toguid
;
391 wbr_drrr
->drr_refguid
= dataref
.ref_guid
;
392 wbr_drrr
->drr_refobject
=
394 wbr_drrr
->drr_refoffset
=
397 wbr_drrr
->drr_checksumtype
=
398 drrw
->drr_checksumtype
;
399 wbr_drrr
->drr_checksumflags
=
400 drrw
->drr_checksumtype
;
401 wbr_drrr
->drr_key
.ddk_cksum
=
402 drrw
->drr_key
.ddk_cksum
;
403 wbr_drrr
->drr_key
.ddk_prop
=
404 drrw
->drr_key
.ddk_prop
;
406 if (dump_record(&wbr_drr
, NULL
, 0,
407 &stream_cksum
, outfd
) != 0)
410 /* block not previously seen */
411 if (dump_record(drr
, buf
, drrw
->drr_length
,
412 &stream_cksum
, outfd
) != 0)
418 case DRR_WRITE_EMBEDDED
:
420 struct drr_write_embedded
*drrwe
=
421 &drr
->drr_u
.drr_write_embedded
;
423 P2ROUNDUP((uint64_t)drrwe
->drr_psize
, 8), ofp
);
424 if (dump_record(drr
, buf
,
425 P2ROUNDUP((uint64_t)drrwe
->drr_psize
, 8),
426 &stream_cksum
, outfd
) != 0)
433 if (dump_record(drr
, NULL
, 0, &stream_cksum
,
440 (void) fprintf(stderr
, "INVALID record type 0x%x\n",
442 /* should never happen, so assert */
447 umem_cache_destroy(ddt
.ddecache
);
448 free(ddt
.dedup_hash_array
);
456 * Routines for dealing with the AVL tree of fs-nvlists
458 typedef struct fsavl_node
{
466 fsavl_compare(const void *arg1
, const void *arg2
)
468 const fsavl_node_t
*fn1
= arg1
;
469 const fsavl_node_t
*fn2
= arg2
;
471 if (fn1
->fn_guid
> fn2
->fn_guid
)
473 else if (fn1
->fn_guid
< fn2
->fn_guid
)
480 * Given the GUID of a snapshot, find its containing filesystem and
484 fsavl_find(avl_tree_t
*avl
, uint64_t snapguid
, char **snapname
)
486 fsavl_node_t fn_find
;
489 fn_find
.fn_guid
= snapguid
;
491 fn
= avl_find(avl
, &fn_find
, NULL
);
494 *snapname
= fn
->fn_snapname
;
495 return (fn
->fn_nvfs
);
501 fsavl_destroy(avl_tree_t
*avl
)
510 while ((fn
= avl_destroy_nodes(avl
, &cookie
)) != NULL
)
517 * Given an nvlist, produce an avl tree of snapshots, ordered by guid
520 fsavl_create(nvlist_t
*fss
)
523 nvpair_t
*fselem
= NULL
;
525 if ((fsavl
= malloc(sizeof (avl_tree_t
))) == NULL
)
528 avl_create(fsavl
, fsavl_compare
, sizeof (fsavl_node_t
),
529 offsetof(fsavl_node_t
, fn_node
));
531 while ((fselem
= nvlist_next_nvpair(fss
, fselem
)) != NULL
) {
532 nvlist_t
*nvfs
, *snaps
;
533 nvpair_t
*snapelem
= NULL
;
535 VERIFY(0 == nvpair_value_nvlist(fselem
, &nvfs
));
536 VERIFY(0 == nvlist_lookup_nvlist(nvfs
, "snaps", &snaps
));
539 nvlist_next_nvpair(snaps
, snapelem
)) != NULL
) {
543 VERIFY(0 == nvpair_value_uint64(snapelem
, &guid
));
544 if ((fn
= malloc(sizeof (fsavl_node_t
))) == NULL
) {
545 fsavl_destroy(fsavl
);
549 fn
->fn_snapname
= nvpair_name(snapelem
);
553 * Note: if there are multiple snaps with the
554 * same GUID, we ignore all but one.
556 if (avl_find(fsavl
, fn
, NULL
) == NULL
)
567 * Routines for dealing with the giant nvlist of fs-nvlists, etc.
569 typedef struct send_data
{
570 uint64_t parent_fromsnap_guid
;
571 nvlist_t
*parent_snaps
;
574 const char *fromsnap
;
579 * The header nvlist is of the following format:
582 * "fromsnap" -> string (if incremental)
586 * "name" -> string (full name; for debugging)
587 * "parentfromsnap" -> number (guid of fromsnap in parent)
589 * "props" -> { name -> value (only if set here) }
590 * "snaps" -> { name (lastname) -> number (guid) }
591 * "snapprops" -> { name (lastname) -> { name -> value } }
593 * "origin" -> number (guid) (if clone)
594 * "sent" -> boolean (not on-disk)
602 static void send_iterate_prop(zfs_handle_t
*zhp
, nvlist_t
*nv
);
605 send_iterate_snap(zfs_handle_t
*zhp
, void *arg
)
607 send_data_t
*sd
= arg
;
608 uint64_t guid
= zhp
->zfs_dmustats
.dds_guid
;
612 snapname
= strrchr(zhp
->zfs_name
, '@')+1;
614 VERIFY(0 == nvlist_add_uint64(sd
->parent_snaps
, snapname
, guid
));
616 * NB: if there is no fromsnap here (it's a newly created fs in
617 * an incremental replication), we will substitute the tosnap.
619 if ((sd
->fromsnap
&& strcmp(snapname
, sd
->fromsnap
) == 0) ||
620 (sd
->parent_fromsnap_guid
== 0 && sd
->tosnap
&&
621 strcmp(snapname
, sd
->tosnap
) == 0)) {
622 sd
->parent_fromsnap_guid
= guid
;
625 VERIFY(0 == nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0));
626 send_iterate_prop(zhp
, nv
);
627 VERIFY(0 == nvlist_add_nvlist(sd
->snapprops
, snapname
, nv
));
635 send_iterate_prop(zfs_handle_t
*zhp
, nvlist_t
*nv
)
637 nvpair_t
*elem
= NULL
;
639 while ((elem
= nvlist_next_nvpair(zhp
->zfs_props
, elem
)) != NULL
) {
640 char *propname
= nvpair_name(elem
);
641 zfs_prop_t prop
= zfs_name_to_prop(propname
);
644 if (!zfs_prop_user(propname
)) {
646 * Realistically, this should never happen. However,
647 * we want the ability to add DSL properties without
648 * needing to make incompatible version changes. We
649 * need to ignore unknown properties to allow older
650 * software to still send datasets containing these
651 * properties, with the unknown properties elided.
653 if (prop
== ZPROP_INVAL
)
656 if (zfs_prop_readonly(prop
))
660 verify(nvpair_value_nvlist(elem
, &propnv
) == 0);
661 if (prop
== ZFS_PROP_QUOTA
|| prop
== ZFS_PROP_RESERVATION
||
662 prop
== ZFS_PROP_REFQUOTA
||
663 prop
== ZFS_PROP_REFRESERVATION
) {
666 verify(nvlist_lookup_uint64(propnv
,
667 ZPROP_VALUE
, &value
) == 0);
668 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
)
671 * May have no source before SPA_VERSION_RECVD_PROPS,
672 * but is still modifiable.
674 if (nvlist_lookup_string(propnv
,
675 ZPROP_SOURCE
, &source
) == 0) {
676 if ((strcmp(source
, zhp
->zfs_name
) != 0) &&
678 ZPROP_SOURCE_VAL_RECVD
) != 0))
683 if (nvlist_lookup_string(propnv
,
684 ZPROP_SOURCE
, &source
) != 0)
686 if ((strcmp(source
, zhp
->zfs_name
) != 0) &&
687 (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) != 0))
691 if (zfs_prop_user(propname
) ||
692 zfs_prop_get_type(prop
) == PROP_TYPE_STRING
) {
694 verify(nvlist_lookup_string(propnv
,
695 ZPROP_VALUE
, &value
) == 0);
696 VERIFY(0 == nvlist_add_string(nv
, propname
, value
));
699 verify(nvlist_lookup_uint64(propnv
,
700 ZPROP_VALUE
, &value
) == 0);
701 VERIFY(0 == nvlist_add_uint64(nv
, propname
, value
));
707 * recursively generate nvlists describing datasets. See comment
708 * for the data structure send_data_t above for description of contents
712 send_iterate_fs(zfs_handle_t
*zhp
, void *arg
)
714 send_data_t
*sd
= arg
;
717 uint64_t parent_fromsnap_guid_save
= sd
->parent_fromsnap_guid
;
718 uint64_t guid
= zhp
->zfs_dmustats
.dds_guid
;
721 VERIFY(0 == nvlist_alloc(&nvfs
, NV_UNIQUE_NAME
, 0));
722 VERIFY(0 == nvlist_add_string(nvfs
, "name", zhp
->zfs_name
));
723 VERIFY(0 == nvlist_add_uint64(nvfs
, "parentfromsnap",
724 sd
->parent_fromsnap_guid
));
726 if (zhp
->zfs_dmustats
.dds_origin
[0]) {
727 zfs_handle_t
*origin
= zfs_open(zhp
->zfs_hdl
,
728 zhp
->zfs_dmustats
.dds_origin
, ZFS_TYPE_SNAPSHOT
);
731 VERIFY(0 == nvlist_add_uint64(nvfs
, "origin",
732 origin
->zfs_dmustats
.dds_guid
));
735 /* iterate over props */
736 VERIFY(0 == nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0));
737 send_iterate_prop(zhp
, nv
);
738 VERIFY(0 == nvlist_add_nvlist(nvfs
, "props", nv
));
741 /* iterate over snaps, and set sd->parent_fromsnap_guid */
742 sd
->parent_fromsnap_guid
= 0;
743 VERIFY(0 == nvlist_alloc(&sd
->parent_snaps
, NV_UNIQUE_NAME
, 0));
744 VERIFY(0 == nvlist_alloc(&sd
->snapprops
, NV_UNIQUE_NAME
, 0));
745 (void) zfs_iter_snapshots(zhp
, send_iterate_snap
, sd
);
746 VERIFY(0 == nvlist_add_nvlist(nvfs
, "snaps", sd
->parent_snaps
));
747 VERIFY(0 == nvlist_add_nvlist(nvfs
, "snapprops", sd
->snapprops
));
748 nvlist_free(sd
->parent_snaps
);
749 nvlist_free(sd
->snapprops
);
751 /* add this fs to nvlist */
752 (void) snprintf(guidstring
, sizeof (guidstring
),
753 "0x%llx", (longlong_t
)guid
);
754 VERIFY(0 == nvlist_add_nvlist(sd
->fss
, guidstring
, nvfs
));
757 /* iterate over children */
759 rv
= zfs_iter_filesystems(zhp
, send_iterate_fs
, sd
);
761 sd
->parent_fromsnap_guid
= parent_fromsnap_guid_save
;
768 gather_nvlist(libzfs_handle_t
*hdl
, const char *fsname
, const char *fromsnap
,
769 const char *tosnap
, boolean_t recursive
, nvlist_t
**nvlp
, avl_tree_t
**avlp
)
772 send_data_t sd
= { 0 };
775 zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
777 return (EZFS_BADTYPE
);
779 VERIFY(0 == nvlist_alloc(&sd
.fss
, NV_UNIQUE_NAME
, 0));
780 sd
.fromsnap
= fromsnap
;
782 sd
.recursive
= recursive
;
784 if ((error
= send_iterate_fs(zhp
, &sd
)) != 0) {
792 if (avlp
!= NULL
&& (*avlp
= fsavl_create(sd
.fss
)) == NULL
) {
803 * Routines specific to "zfs send"
805 typedef struct send_dump_data
{
806 /* these are all just the short snapname (the part after the @) */
807 const char *fromsnap
;
809 char prevsnap
[ZFS_MAXNAMELEN
];
810 uint64_t prevsnap_obj
;
811 boolean_t seenfrom
, seento
, replicate
, doall
, fromorigin
;
812 boolean_t verbose
, dryrun
, parsable
, progress
, embed_data
, std_out
;
813 boolean_t large_block
;
819 snapfilter_cb_t
*filter_cb
;
822 char holdtag
[ZFS_MAXNAMELEN
];
828 estimate_ioctl(zfs_handle_t
*zhp
, uint64_t fromsnap_obj
,
829 boolean_t fromorigin
, uint64_t *sizep
)
831 zfs_cmd_t zc
= { 0 };
832 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
834 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
835 assert(fromsnap_obj
== 0 || !fromorigin
);
837 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
838 zc
.zc_obj
= fromorigin
;
839 zc
.zc_sendobj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
840 zc
.zc_fromobj
= fromsnap_obj
;
841 zc
.zc_guid
= 1; /* estimate flag */
843 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SEND
, &zc
) != 0) {
845 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
846 "warning: cannot estimate space for '%s'"), zhp
->zfs_name
);
850 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
851 "not an earlier snapshot from the same fs"));
852 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
855 if (zfs_dataset_exists(hdl
, zc
.zc_name
,
856 ZFS_TYPE_SNAPSHOT
)) {
857 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
858 "incremental source (@%s) does not exist"),
861 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
874 zfs_error_aux(hdl
, strerror(errno
));
875 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
878 return (zfs_standard_error(hdl
, errno
, errbuf
));
882 *sizep
= zc
.zc_objset_type
;
888 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
889 * NULL) to the file descriptor specified by outfd.
892 dump_ioctl(zfs_handle_t
*zhp
, const char *fromsnap
, uint64_t fromsnap_obj
,
893 boolean_t fromorigin
, int outfd
, enum lzc_send_flags flags
,
896 zfs_cmd_t zc
= { 0 };
897 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
900 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
901 assert(fromsnap_obj
== 0 || !fromorigin
);
903 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
904 zc
.zc_cookie
= outfd
;
905 zc
.zc_obj
= fromorigin
;
906 zc
.zc_sendobj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
907 zc
.zc_fromobj
= fromsnap_obj
;
910 VERIFY(0 == nvlist_alloc(&thisdbg
, NV_UNIQUE_NAME
, 0));
911 if (fromsnap
&& fromsnap
[0] != '\0') {
912 VERIFY(0 == nvlist_add_string(thisdbg
,
913 "fromsnap", fromsnap
));
916 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SEND
, &zc
) != 0) {
918 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
919 "warning: cannot send '%s'"), zhp
->zfs_name
);
921 VERIFY(0 == nvlist_add_uint64(thisdbg
, "error", errno
));
923 VERIFY(0 == nvlist_add_nvlist(debugnv
,
924 zhp
->zfs_name
, thisdbg
));
926 nvlist_free(thisdbg
);
930 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
931 "not an earlier snapshot from the same fs"));
932 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
935 if (zfs_dataset_exists(hdl
, zc
.zc_name
,
936 ZFS_TYPE_SNAPSHOT
)) {
937 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
938 "incremental source (@%s) does not exist"),
941 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
954 zfs_error_aux(hdl
, strerror(errno
));
955 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
958 return (zfs_standard_error(hdl
, errno
, errbuf
));
963 VERIFY(0 == nvlist_add_nvlist(debugnv
, zhp
->zfs_name
, thisdbg
));
964 nvlist_free(thisdbg
);
970 gather_holds(zfs_handle_t
*zhp
, send_dump_data_t
*sdd
)
972 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
975 * zfs_send() only sets snapholds for sends that need them,
976 * e.g. replication and doall.
978 if (sdd
->snapholds
== NULL
)
981 fnvlist_add_string(sdd
->snapholds
, zhp
->zfs_name
, sdd
->holdtag
);
985 send_progress_thread(void *arg
)
987 progress_arg_t
*pa
= arg
;
989 zfs_cmd_t zc
= { 0 };
990 zfs_handle_t
*zhp
= pa
->pa_zhp
;
991 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
992 unsigned long long bytes
;
998 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
999 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1001 if (!pa
->pa_parsable
)
1002 (void) fprintf(stderr
, "TIME SENT SNAPSHOT\n");
1005 * Print the progress from ZFS_IOC_SEND_PROGRESS every second.
1010 zc
.zc_cookie
= pa
->pa_fd
;
1011 if (zfs_ioctl(hdl
, ZFS_IOC_SEND_PROGRESS
, &zc
) != 0)
1012 return ((void *)-1);
1016 bytes
= zc
.zc_cookie
;
1018 if (pa
->pa_parsable
) {
1019 (void) fprintf(stderr
, "%02d:%02d:%02d\t%llu\t%s\n",
1020 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
1021 bytes
, zhp
->zfs_name
);
1023 zfs_nicenum(bytes
, buf
, sizeof (buf
));
1024 (void) fprintf(stderr
, "%02d:%02d:%02d %5s %s\n",
1025 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
1026 buf
, zhp
->zfs_name
);
1032 dump_snapshot(zfs_handle_t
*zhp
, void *arg
)
1034 send_dump_data_t
*sdd
= arg
;
1035 progress_arg_t pa
= { 0 };
1039 boolean_t isfromsnap
, istosnap
, fromorigin
;
1040 boolean_t exclude
= B_FALSE
;
1041 FILE *fout
= sdd
->std_out
? stdout
: stderr
;
1044 thissnap
= strchr(zhp
->zfs_name
, '@') + 1;
1045 isfromsnap
= (sdd
->fromsnap
!= NULL
&&
1046 strcmp(sdd
->fromsnap
, thissnap
) == 0);
1048 if (!sdd
->seenfrom
&& isfromsnap
) {
1049 gather_holds(zhp
, sdd
);
1050 sdd
->seenfrom
= B_TRUE
;
1051 (void) strcpy(sdd
->prevsnap
, thissnap
);
1052 sdd
->prevsnap_obj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
1057 if (sdd
->seento
|| !sdd
->seenfrom
) {
1062 istosnap
= (strcmp(sdd
->tosnap
, thissnap
) == 0);
1064 sdd
->seento
= B_TRUE
;
1066 if (!sdd
->doall
&& !isfromsnap
&& !istosnap
) {
1067 if (sdd
->replicate
) {
1069 nvlist_t
*snapprops
;
1071 * Filter out all intermediate snapshots except origin
1072 * snapshots needed to replicate clones.
1074 nvlist_t
*nvfs
= fsavl_find(sdd
->fsavl
,
1075 zhp
->zfs_dmustats
.dds_guid
, &snapname
);
1077 VERIFY(0 == nvlist_lookup_nvlist(nvfs
,
1078 "snapprops", &snapprops
));
1079 VERIFY(0 == nvlist_lookup_nvlist(snapprops
,
1080 thissnap
, &snapprops
));
1081 exclude
= !nvlist_exists(snapprops
, "is_clone_origin");
1088 * If a filter function exists, call it to determine whether
1089 * this snapshot will be sent.
1091 if (exclude
|| (sdd
->filter_cb
!= NULL
&&
1092 sdd
->filter_cb(zhp
, sdd
->filter_cb_arg
) == B_FALSE
)) {
1094 * This snapshot is filtered out. Don't send it, and don't
1095 * set prevsnap_obj, so it will be as if this snapshot didn't
1096 * exist, and the next accepted snapshot will be sent as
1097 * an incremental from the last accepted one, or as the
1098 * first (and full) snapshot in the case of a replication,
1099 * non-incremental send.
1105 gather_holds(zhp
, sdd
);
1106 fromorigin
= sdd
->prevsnap
[0] == '\0' &&
1107 (sdd
->fromorigin
|| sdd
->replicate
);
1111 err
= estimate_ioctl(zhp
, sdd
->prevsnap_obj
,
1114 if (sdd
->parsable
) {
1115 if (sdd
->prevsnap
[0] != '\0') {
1116 (void) fprintf(fout
, "incremental\t%s\t%s",
1117 sdd
->prevsnap
, zhp
->zfs_name
);
1119 (void) fprintf(fout
, "full\t%s",
1123 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1124 "send from @%s to %s"),
1125 sdd
->prevsnap
, zhp
->zfs_name
);
1128 if (sdd
->parsable
) {
1129 (void) fprintf(fout
, "\t%llu\n",
1133 zfs_nicenum(size
, buf
, sizeof (buf
));
1134 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1135 " estimated size is %s\n"), buf
);
1139 (void) fprintf(fout
, "\n");
1145 * If progress reporting is requested, spawn a new thread to
1146 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1148 if (sdd
->progress
) {
1150 pa
.pa_fd
= sdd
->outfd
;
1151 pa
.pa_parsable
= sdd
->parsable
;
1153 if (err
= pthread_create(&tid
, NULL
,
1154 send_progress_thread
, &pa
)) {
1160 enum lzc_send_flags flags
= 0;
1161 if (sdd
->large_block
)
1162 flags
|= LZC_SEND_FLAG_LARGE_BLOCK
;
1163 if (sdd
->embed_data
)
1164 flags
|= LZC_SEND_FLAG_EMBED_DATA
;
1166 err
= dump_ioctl(zhp
, sdd
->prevsnap
, sdd
->prevsnap_obj
,
1167 fromorigin
, sdd
->outfd
, flags
, sdd
->debugnv
);
1169 if (sdd
->progress
) {
1170 (void) pthread_cancel(tid
);
1171 (void) pthread_join(tid
, NULL
);
1175 (void) strcpy(sdd
->prevsnap
, thissnap
);
1176 sdd
->prevsnap_obj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
1182 dump_filesystem(zfs_handle_t
*zhp
, void *arg
)
1185 send_dump_data_t
*sdd
= arg
;
1186 boolean_t missingfrom
= B_FALSE
;
1187 zfs_cmd_t zc
= { 0 };
1189 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
), "%s@%s",
1190 zhp
->zfs_name
, sdd
->tosnap
);
1191 if (ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0) {
1192 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1193 "WARNING: could not send %s@%s: does not exist\n"),
1194 zhp
->zfs_name
, sdd
->tosnap
);
1199 if (sdd
->replicate
&& sdd
->fromsnap
) {
1201 * If this fs does not have fromsnap, and we're doing
1202 * recursive, we need to send a full stream from the
1203 * beginning (or an incremental from the origin if this
1204 * is a clone). If we're doing non-recursive, then let
1205 * them get the error.
1207 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
), "%s@%s",
1208 zhp
->zfs_name
, sdd
->fromsnap
);
1209 if (ioctl(zhp
->zfs_hdl
->libzfs_fd
,
1210 ZFS_IOC_OBJSET_STATS
, &zc
) != 0) {
1211 missingfrom
= B_TRUE
;
1215 sdd
->seenfrom
= sdd
->seento
= sdd
->prevsnap
[0] = 0;
1216 sdd
->prevsnap_obj
= 0;
1217 if (sdd
->fromsnap
== NULL
|| missingfrom
)
1218 sdd
->seenfrom
= B_TRUE
;
1220 rv
= zfs_iter_snapshots_sorted(zhp
, dump_snapshot
, arg
);
1221 if (!sdd
->seenfrom
) {
1222 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1223 "WARNING: could not send %s@%s:\n"
1224 "incremental source (%s@%s) does not exist\n"),
1225 zhp
->zfs_name
, sdd
->tosnap
,
1226 zhp
->zfs_name
, sdd
->fromsnap
);
1228 } else if (!sdd
->seento
) {
1229 if (sdd
->fromsnap
) {
1230 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1231 "WARNING: could not send %s@%s:\n"
1232 "incremental source (%s@%s) "
1233 "is not earlier than it\n"),
1234 zhp
->zfs_name
, sdd
->tosnap
,
1235 zhp
->zfs_name
, sdd
->fromsnap
);
1237 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1239 "could not send %s@%s: does not exist\n"),
1240 zhp
->zfs_name
, sdd
->tosnap
);
1249 dump_filesystems(zfs_handle_t
*rzhp
, void *arg
)
1251 send_dump_data_t
*sdd
= arg
;
1253 boolean_t needagain
, progress
;
1255 if (!sdd
->replicate
)
1256 return (dump_filesystem(rzhp
, sdd
));
1258 /* Mark the clone origin snapshots. */
1259 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1260 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1262 uint64_t origin_guid
= 0;
1264 VERIFY(0 == nvpair_value_nvlist(fspair
, &nvfs
));
1265 (void) nvlist_lookup_uint64(nvfs
, "origin", &origin_guid
);
1266 if (origin_guid
!= 0) {
1268 nvlist_t
*origin_nv
= fsavl_find(sdd
->fsavl
,
1269 origin_guid
, &snapname
);
1270 if (origin_nv
!= NULL
) {
1271 nvlist_t
*snapprops
;
1272 VERIFY(0 == nvlist_lookup_nvlist(origin_nv
,
1273 "snapprops", &snapprops
));
1274 VERIFY(0 == nvlist_lookup_nvlist(snapprops
,
1275 snapname
, &snapprops
));
1276 VERIFY(0 == nvlist_add_boolean(
1277 snapprops
, "is_clone_origin"));
1282 needagain
= progress
= B_FALSE
;
1283 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1284 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1285 nvlist_t
*fslist
, *parent_nv
;
1289 uint64_t origin_guid
= 0;
1290 uint64_t parent_guid
= 0;
1292 VERIFY(nvpair_value_nvlist(fspair
, &fslist
) == 0);
1293 if (nvlist_lookup_boolean(fslist
, "sent") == 0)
1296 VERIFY(nvlist_lookup_string(fslist
, "name", &fsname
) == 0);
1297 (void) nvlist_lookup_uint64(fslist
, "origin", &origin_guid
);
1298 (void) nvlist_lookup_uint64(fslist
, "parentfromsnap",
1301 if (parent_guid
!= 0) {
1302 parent_nv
= fsavl_find(sdd
->fsavl
, parent_guid
, NULL
);
1303 if (!nvlist_exists(parent_nv
, "sent")) {
1304 /* parent has not been sent; skip this one */
1310 if (origin_guid
!= 0) {
1311 nvlist_t
*origin_nv
= fsavl_find(sdd
->fsavl
,
1313 if (origin_nv
!= NULL
&&
1314 !nvlist_exists(origin_nv
, "sent")) {
1316 * origin has not been sent yet;
1324 zhp
= zfs_open(rzhp
->zfs_hdl
, fsname
, ZFS_TYPE_DATASET
);
1327 err
= dump_filesystem(zhp
, sdd
);
1328 VERIFY(nvlist_add_boolean(fslist
, "sent") == 0);
1339 /* clean out the sent flags in case we reuse this fss */
1340 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1341 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1344 VERIFY(nvpair_value_nvlist(fspair
, &fslist
) == 0);
1345 (void) nvlist_remove_all(fslist
, "sent");
1352 * Generate a send stream for the dataset identified by the argument zhp.
1354 * The content of the send stream is the snapshot identified by
1355 * 'tosnap'. Incremental streams are requested in two ways:
1356 * - from the snapshot identified by "fromsnap" (if non-null) or
1357 * - from the origin of the dataset identified by zhp, which must
1358 * be a clone. In this case, "fromsnap" is null and "fromorigin"
1361 * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
1362 * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
1363 * if "replicate" is set. If "doall" is set, dump all the intermediate
1364 * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
1365 * case too. If "props" is set, send properties.
1368 zfs_send(zfs_handle_t
*zhp
, const char *fromsnap
, const char *tosnap
,
1369 sendflags_t
*flags
, int outfd
, snapfilter_cb_t filter_func
,
1370 void *cb_arg
, nvlist_t
**debugnvp
)
1373 send_dump_data_t sdd
= { 0 };
1375 nvlist_t
*fss
= NULL
;
1376 avl_tree_t
*fsavl
= NULL
;
1377 static uint64_t holdseq
;
1381 dedup_arg_t dda
= { 0 };
1382 int featureflags
= 0;
1385 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1386 "cannot send '%s'"), zhp
->zfs_name
);
1388 if (fromsnap
&& fromsnap
[0] == '\0') {
1389 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1390 "zero-length incremental source"));
1391 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
1394 if (zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
) {
1396 version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1397 if (version
>= ZPL_VERSION_SA
) {
1398 featureflags
|= DMU_BACKUP_FEATURE_SA_SPILL
;
1402 if (flags
->dedup
&& !flags
->dryrun
) {
1403 featureflags
|= (DMU_BACKUP_FEATURE_DEDUP
|
1404 DMU_BACKUP_FEATURE_DEDUPPROPS
);
1405 if (err
= pipe(pipefd
)) {
1406 zfs_error_aux(zhp
->zfs_hdl
, strerror(errno
));
1407 return (zfs_error(zhp
->zfs_hdl
, EZFS_PIPEFAILED
,
1410 dda
.outputfd
= outfd
;
1411 dda
.inputfd
= pipefd
[1];
1412 dda
.dedup_hdl
= zhp
->zfs_hdl
;
1413 if (err
= pthread_create(&tid
, NULL
, cksummer
, &dda
)) {
1414 (void) close(pipefd
[0]);
1415 (void) close(pipefd
[1]);
1416 zfs_error_aux(zhp
->zfs_hdl
, strerror(errno
));
1417 return (zfs_error(zhp
->zfs_hdl
,
1418 EZFS_THREADCREATEFAILED
, errbuf
));
1422 if (flags
->replicate
|| flags
->doall
|| flags
->props
) {
1423 dmu_replay_record_t drr
= { 0 };
1424 char *packbuf
= NULL
;
1426 zio_cksum_t zc
= { 0 };
1428 if (flags
->replicate
|| flags
->props
) {
1431 VERIFY(0 == nvlist_alloc(&hdrnv
, NV_UNIQUE_NAME
, 0));
1433 VERIFY(0 == nvlist_add_string(hdrnv
,
1434 "fromsnap", fromsnap
));
1436 VERIFY(0 == nvlist_add_string(hdrnv
, "tosnap", tosnap
));
1437 if (!flags
->replicate
) {
1438 VERIFY(0 == nvlist_add_boolean(hdrnv
,
1442 err
= gather_nvlist(zhp
->zfs_hdl
, zhp
->zfs_name
,
1443 fromsnap
, tosnap
, flags
->replicate
, &fss
, &fsavl
);
1446 VERIFY(0 == nvlist_add_nvlist(hdrnv
, "fss", fss
));
1447 err
= nvlist_pack(hdrnv
, &packbuf
, &buflen
,
1457 if (!flags
->dryrun
) {
1458 /* write first begin record */
1459 drr
.drr_type
= DRR_BEGIN
;
1460 drr
.drr_u
.drr_begin
.drr_magic
= DMU_BACKUP_MAGIC
;
1461 DMU_SET_STREAM_HDRTYPE(drr
.drr_u
.drr_begin
.
1462 drr_versioninfo
, DMU_COMPOUNDSTREAM
);
1463 DMU_SET_FEATUREFLAGS(drr
.drr_u
.drr_begin
.
1464 drr_versioninfo
, featureflags
);
1465 (void) snprintf(drr
.drr_u
.drr_begin
.drr_toname
,
1466 sizeof (drr
.drr_u
.drr_begin
.drr_toname
),
1467 "%s@%s", zhp
->zfs_name
, tosnap
);
1468 drr
.drr_payloadlen
= buflen
;
1470 err
= dump_record(&drr
, packbuf
, buflen
, &zc
, outfd
);
1475 /* write end record */
1476 bzero(&drr
, sizeof (drr
));
1477 drr
.drr_type
= DRR_END
;
1478 drr
.drr_u
.drr_end
.drr_checksum
= zc
;
1479 err
= write(outfd
, &drr
, sizeof (drr
));
1489 /* dump each stream */
1490 sdd
.fromsnap
= fromsnap
;
1491 sdd
.tosnap
= tosnap
;
1493 sdd
.outfd
= pipefd
[0];
1496 sdd
.replicate
= flags
->replicate
;
1497 sdd
.doall
= flags
->doall
;
1498 sdd
.fromorigin
= flags
->fromorigin
;
1501 sdd
.verbose
= flags
->verbose
;
1502 sdd
.parsable
= flags
->parsable
;
1503 sdd
.progress
= flags
->progress
;
1504 sdd
.dryrun
= flags
->dryrun
;
1505 sdd
.large_block
= flags
->largeblock
;
1506 sdd
.embed_data
= flags
->embed_data
;
1507 sdd
.filter_cb
= filter_func
;
1508 sdd
.filter_cb_arg
= cb_arg
;
1510 sdd
.debugnv
= *debugnvp
;
1511 if (sdd
.verbose
&& sdd
.dryrun
)
1512 sdd
.std_out
= B_TRUE
;
1513 fout
= sdd
.std_out
? stdout
: stderr
;
1516 * Some flags require that we place user holds on the datasets that are
1517 * being sent so they don't get destroyed during the send. We can skip
1518 * this step if the pool is imported read-only since the datasets cannot
1521 if (!flags
->dryrun
&& !zpool_get_prop_int(zfs_get_pool_handle(zhp
),
1522 ZPOOL_PROP_READONLY
, NULL
) &&
1523 zfs_spa_version(zhp
, &spa_version
) == 0 &&
1524 spa_version
>= SPA_VERSION_USERREFS
&&
1525 (flags
->doall
|| flags
->replicate
)) {
1527 (void) snprintf(sdd
.holdtag
, sizeof (sdd
.holdtag
),
1528 ".send-%d-%llu", getpid(), (u_longlong_t
)holdseq
);
1529 sdd
.cleanup_fd
= open(ZFS_DEV
, O_RDWR
|O_EXCL
);
1530 if (sdd
.cleanup_fd
< 0) {
1534 sdd
.snapholds
= fnvlist_alloc();
1536 sdd
.cleanup_fd
= -1;
1537 sdd
.snapholds
= NULL
;
1539 if (flags
->verbose
|| sdd
.snapholds
!= NULL
) {
1541 * Do a verbose no-op dry run to get all the verbose output
1542 * or to gather snapshot hold's before generating any data,
1543 * then do a non-verbose real run to generate the streams.
1545 sdd
.dryrun
= B_TRUE
;
1546 err
= dump_filesystems(zhp
, &sdd
);
1551 if (flags
->verbose
) {
1552 if (flags
->parsable
) {
1553 (void) fprintf(fout
, "size\t%llu\n",
1554 (longlong_t
)sdd
.size
);
1557 zfs_nicenum(sdd
.size
, buf
, sizeof (buf
));
1558 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1559 "total estimated size is %s\n"), buf
);
1563 /* Ensure no snaps found is treated as an error. */
1569 /* Skip the second run if dryrun was requested. */
1573 if (sdd
.snapholds
!= NULL
) {
1574 err
= zfs_hold_nvl(zhp
, sdd
.cleanup_fd
, sdd
.snapholds
);
1578 fnvlist_free(sdd
.snapholds
);
1579 sdd
.snapholds
= NULL
;
1582 sdd
.dryrun
= B_FALSE
;
1583 sdd
.verbose
= B_FALSE
;
1586 err
= dump_filesystems(zhp
, &sdd
);
1587 fsavl_destroy(fsavl
);
1590 /* Ensure no snaps found is treated as an error. */
1591 if (err
== 0 && !sdd
.seento
)
1596 (void) pthread_cancel(tid
);
1597 (void) close(pipefd
[0]);
1598 (void) pthread_join(tid
, NULL
);
1601 if (sdd
.cleanup_fd
!= -1) {
1602 VERIFY(0 == close(sdd
.cleanup_fd
));
1603 sdd
.cleanup_fd
= -1;
1606 if (!flags
->dryrun
&& (flags
->replicate
|| flags
->doall
||
1609 * write final end record. NB: want to do this even if
1610 * there was some error, because it might not be totally
1613 dmu_replay_record_t drr
= { 0 };
1614 drr
.drr_type
= DRR_END
;
1615 if (write(outfd
, &drr
, sizeof (drr
)) == -1) {
1616 return (zfs_standard_error(zhp
->zfs_hdl
,
1621 return (err
|| sdd
.err
);
1624 err
= zfs_standard_error(zhp
->zfs_hdl
, err
, errbuf
);
1626 fsavl_destroy(fsavl
);
1628 fnvlist_free(sdd
.snapholds
);
1630 if (sdd
.cleanup_fd
!= -1)
1631 VERIFY(0 == close(sdd
.cleanup_fd
));
1633 (void) pthread_cancel(tid
);
1634 (void) close(pipefd
[0]);
1635 (void) pthread_join(tid
, NULL
);
1641 zfs_send_one(zfs_handle_t
*zhp
, const char *from
, int fd
,
1642 enum lzc_send_flags flags
)
1645 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1648 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1649 "warning: cannot send '%s'"), zhp
->zfs_name
);
1651 err
= lzc_send(zhp
->zfs_name
, from
, fd
, flags
);
1655 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1656 "not an earlier snapshot from the same fs"));
1657 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
1661 if (lzc_exists(zhp
->zfs_name
)) {
1662 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1663 "incremental source (%s) does not exist"),
1666 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
1669 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1670 "target is busy; if a filesystem, "
1671 "it must not be mounted"));
1672 return (zfs_error(hdl
, EZFS_BUSY
, errbuf
));
1685 zfs_error_aux(hdl
, strerror(errno
));
1686 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
1689 return (zfs_standard_error(hdl
, errno
, errbuf
));
1696 * Routines specific to "zfs recv"
1700 recv_read(libzfs_handle_t
*hdl
, int fd
, void *buf
, int ilen
,
1701 boolean_t byteswap
, zio_cksum_t
*zc
)
1707 assert(ilen
<= SPA_MAXBLOCKSIZE
);
1710 rv
= read(fd
, cp
, len
);
1715 if (rv
< 0 || len
!= 0) {
1716 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1717 "failed to read from stream"));
1718 return (zfs_error(hdl
, EZFS_BADSTREAM
, dgettext(TEXT_DOMAIN
,
1719 "cannot receive")));
1724 fletcher_4_incremental_byteswap(buf
, ilen
, zc
);
1726 fletcher_4_incremental_native(buf
, ilen
, zc
);
1732 recv_read_nvlist(libzfs_handle_t
*hdl
, int fd
, int len
, nvlist_t
**nvp
,
1733 boolean_t byteswap
, zio_cksum_t
*zc
)
1738 buf
= zfs_alloc(hdl
, len
);
1742 err
= recv_read(hdl
, fd
, buf
, len
, byteswap
, zc
);
1748 err
= nvlist_unpack(buf
, len
, nvp
, 0);
1751 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
1752 "stream (malformed nvlist)"));
1759 recv_rename(libzfs_handle_t
*hdl
, const char *name
, const char *tryname
,
1760 int baselen
, char *newname
, recvflags_t
*flags
)
1763 zfs_cmd_t zc
= { 0 };
1765 prop_changelist_t
*clp
;
1768 zhp
= zfs_open(hdl
, name
, ZFS_TYPE_DATASET
);
1771 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
1772 flags
->force
? MS_FORCE
: 0);
1776 err
= changelist_prefix(clp
);
1780 zc
.zc_objset_type
= DMU_OST_ZFS
;
1781 (void) strlcpy(zc
.zc_name
, name
, sizeof (zc
.zc_name
));
1784 (void) strcpy(newname
, tryname
);
1786 (void) strlcpy(zc
.zc_value
, tryname
, sizeof (zc
.zc_value
));
1788 if (flags
->verbose
) {
1789 (void) printf("attempting rename %s to %s\n",
1790 zc
.zc_name
, zc
.zc_value
);
1792 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_RENAME
, &zc
);
1794 changelist_rename(clp
, name
, tryname
);
1799 if (err
!= 0 && strncmp(name
+ baselen
, "recv-", 5) != 0) {
1802 (void) snprintf(newname
, ZFS_MAXNAMELEN
, "%.*srecv-%u-%u",
1803 baselen
, name
, getpid(), seq
);
1804 (void) strlcpy(zc
.zc_value
, newname
, sizeof (zc
.zc_value
));
1806 if (flags
->verbose
) {
1807 (void) printf("failed - trying rename %s to %s\n",
1808 zc
.zc_name
, zc
.zc_value
);
1810 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_RENAME
, &zc
);
1812 changelist_rename(clp
, name
, newname
);
1813 if (err
&& flags
->verbose
) {
1814 (void) printf("failed (%u) - "
1815 "will try again on next pass\n", errno
);
1818 } else if (flags
->verbose
) {
1820 (void) printf("success\n");
1822 (void) printf("failed (%u)\n", errno
);
1825 (void) changelist_postfix(clp
);
1826 changelist_free(clp
);
1832 recv_destroy(libzfs_handle_t
*hdl
, const char *name
, int baselen
,
1833 char *newname
, recvflags_t
*flags
)
1835 zfs_cmd_t zc
= { 0 };
1837 prop_changelist_t
*clp
;
1839 boolean_t defer
= B_FALSE
;
1842 zhp
= zfs_open(hdl
, name
, ZFS_TYPE_DATASET
);
1845 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
1846 flags
->force
? MS_FORCE
: 0);
1847 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
&&
1848 zfs_spa_version(zhp
, &spa_version
) == 0 &&
1849 spa_version
>= SPA_VERSION_USERREFS
)
1854 err
= changelist_prefix(clp
);
1858 zc
.zc_objset_type
= DMU_OST_ZFS
;
1859 zc
.zc_defer_destroy
= defer
;
1860 (void) strlcpy(zc
.zc_name
, name
, sizeof (zc
.zc_name
));
1863 (void) printf("attempting destroy %s\n", zc
.zc_name
);
1864 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_DESTROY
, &zc
);
1867 (void) printf("success\n");
1868 changelist_remove(clp
, zc
.zc_name
);
1871 (void) changelist_postfix(clp
);
1872 changelist_free(clp
);
1875 * Deferred destroy might destroy the snapshot or only mark it to be
1876 * destroyed later, and it returns success in either case.
1878 if (err
!= 0 || (defer
&& zfs_dataset_exists(hdl
, name
,
1879 ZFS_TYPE_SNAPSHOT
))) {
1880 err
= recv_rename(hdl
, name
, NULL
, baselen
, newname
, flags
);
1886 typedef struct guid_to_name_data
{
1890 } guid_to_name_data_t
;
1893 guid_to_name_cb(zfs_handle_t
*zhp
, void *arg
)
1895 guid_to_name_data_t
*gtnd
= arg
;
1898 if (gtnd
->skip
!= NULL
&&
1899 strcmp(zhp
->zfs_name
, gtnd
->skip
) == 0) {
1903 if (zhp
->zfs_dmustats
.dds_guid
== gtnd
->guid
) {
1904 (void) strcpy(gtnd
->name
, zhp
->zfs_name
);
1909 err
= zfs_iter_children(zhp
, guid_to_name_cb
, gtnd
);
1915 * Attempt to find the local dataset associated with this guid. In the case of
1916 * multiple matches, we attempt to find the "best" match by searching
1917 * progressively larger portions of the hierarchy. This allows one to send a
1918 * tree of datasets individually and guarantee that we will find the source
1919 * guid within that hierarchy, even if there are multiple matches elsewhere.
1922 guid_to_name(libzfs_handle_t
*hdl
, const char *parent
, uint64_t guid
,
1925 /* exhaustive search all local snapshots */
1926 char pname
[ZFS_MAXNAMELEN
];
1927 guid_to_name_data_t gtnd
;
1936 (void) strlcpy(pname
, parent
, sizeof (pname
));
1939 * Search progressively larger portions of the hierarchy. This will
1940 * select the "most local" version of the origin snapshot in the case
1941 * that there are multiple matching snapshots in the system.
1943 while ((cp
= strrchr(pname
, '/')) != NULL
) {
1945 /* Chop off the last component and open the parent */
1947 zhp
= make_dataset_handle(hdl
, pname
);
1952 err
= zfs_iter_children(zhp
, guid_to_name_cb
, >nd
);
1958 * Remember the dataset that we already searched, so we
1959 * skip it next time through.
1968 * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if
1969 * guid1 is after guid2.
1972 created_before(libzfs_handle_t
*hdl
, avl_tree_t
*avl
,
1973 uint64_t guid1
, uint64_t guid2
)
1976 char *fsname
, *snapname
;
1977 char buf
[ZFS_MAXNAMELEN
];
1979 zfs_handle_t
*guid1hdl
, *guid2hdl
;
1980 uint64_t create1
, create2
;
1987 nvfs
= fsavl_find(avl
, guid1
, &snapname
);
1988 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
1989 (void) snprintf(buf
, sizeof (buf
), "%s@%s", fsname
, snapname
);
1990 guid1hdl
= zfs_open(hdl
, buf
, ZFS_TYPE_SNAPSHOT
);
1991 if (guid1hdl
== NULL
)
1994 nvfs
= fsavl_find(avl
, guid2
, &snapname
);
1995 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
1996 (void) snprintf(buf
, sizeof (buf
), "%s@%s", fsname
, snapname
);
1997 guid2hdl
= zfs_open(hdl
, buf
, ZFS_TYPE_SNAPSHOT
);
1998 if (guid2hdl
== NULL
) {
1999 zfs_close(guid1hdl
);
2003 create1
= zfs_prop_get_int(guid1hdl
, ZFS_PROP_CREATETXG
);
2004 create2
= zfs_prop_get_int(guid2hdl
, ZFS_PROP_CREATETXG
);
2006 if (create1
< create2
)
2008 else if (create1
> create2
)
2013 zfs_close(guid1hdl
);
2014 zfs_close(guid2hdl
);
2020 recv_incremental_replication(libzfs_handle_t
*hdl
, const char *tofs
,
2021 recvflags_t
*flags
, nvlist_t
*stream_nv
, avl_tree_t
*stream_avl
,
2025 avl_tree_t
*local_avl
;
2026 nvpair_t
*fselem
, *nextfselem
;
2028 char newname
[ZFS_MAXNAMELEN
];
2030 boolean_t needagain
, progress
, recursive
;
2033 VERIFY(0 == nvlist_lookup_string(stream_nv
, "fromsnap", &fromsnap
));
2035 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
2042 needagain
= progress
= B_FALSE
;
2044 if ((error
= gather_nvlist(hdl
, tofs
, fromsnap
, NULL
,
2045 recursive
, &local_nv
, &local_avl
)) != 0)
2049 * Process deletes and renames
2051 for (fselem
= nvlist_next_nvpair(local_nv
, NULL
);
2052 fselem
; fselem
= nextfselem
) {
2053 nvlist_t
*nvfs
, *snaps
;
2054 nvlist_t
*stream_nvfs
= NULL
;
2055 nvpair_t
*snapelem
, *nextsnapelem
;
2056 uint64_t fromguid
= 0;
2057 uint64_t originguid
= 0;
2058 uint64_t stream_originguid
= 0;
2059 uint64_t parent_fromsnap_guid
, stream_parent_fromsnap_guid
;
2060 char *fsname
, *stream_fsname
;
2062 nextfselem
= nvlist_next_nvpair(local_nv
, fselem
);
2064 VERIFY(0 == nvpair_value_nvlist(fselem
, &nvfs
));
2065 VERIFY(0 == nvlist_lookup_nvlist(nvfs
, "snaps", &snaps
));
2066 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
2067 VERIFY(0 == nvlist_lookup_uint64(nvfs
, "parentfromsnap",
2068 &parent_fromsnap_guid
));
2069 (void) nvlist_lookup_uint64(nvfs
, "origin", &originguid
);
2072 * First find the stream's fs, so we can check for
2073 * a different origin (due to "zfs promote")
2075 for (snapelem
= nvlist_next_nvpair(snaps
, NULL
);
2076 snapelem
; snapelem
= nvlist_next_nvpair(snaps
, snapelem
)) {
2079 VERIFY(0 == nvpair_value_uint64(snapelem
, &thisguid
));
2080 stream_nvfs
= fsavl_find(stream_avl
, thisguid
, NULL
);
2082 if (stream_nvfs
!= NULL
)
2086 /* check for promote */
2087 (void) nvlist_lookup_uint64(stream_nvfs
, "origin",
2088 &stream_originguid
);
2089 if (stream_nvfs
&& originguid
!= stream_originguid
) {
2090 switch (created_before(hdl
, local_avl
,
2091 stream_originguid
, originguid
)) {
2094 zfs_cmd_t zc
= { 0 };
2095 nvlist_t
*origin_nvfs
;
2096 char *origin_fsname
;
2099 (void) printf("promoting %s\n", fsname
);
2101 origin_nvfs
= fsavl_find(local_avl
, originguid
,
2103 VERIFY(0 == nvlist_lookup_string(origin_nvfs
,
2104 "name", &origin_fsname
));
2105 (void) strlcpy(zc
.zc_value
, origin_fsname
,
2106 sizeof (zc
.zc_value
));
2107 (void) strlcpy(zc
.zc_name
, fsname
,
2108 sizeof (zc
.zc_name
));
2109 error
= zfs_ioctl(hdl
, ZFS_IOC_PROMOTE
, &zc
);
2117 fsavl_destroy(local_avl
);
2118 nvlist_free(local_nv
);
2122 * We had/have the wrong origin, therefore our
2123 * list of snapshots is wrong. Need to handle
2124 * them on the next pass.
2130 for (snapelem
= nvlist_next_nvpair(snaps
, NULL
);
2131 snapelem
; snapelem
= nextsnapelem
) {
2133 char *stream_snapname
;
2134 nvlist_t
*found
, *props
;
2136 nextsnapelem
= nvlist_next_nvpair(snaps
, snapelem
);
2138 VERIFY(0 == nvpair_value_uint64(snapelem
, &thisguid
));
2139 found
= fsavl_find(stream_avl
, thisguid
,
2142 /* check for delete */
2143 if (found
== NULL
) {
2144 char name
[ZFS_MAXNAMELEN
];
2149 (void) snprintf(name
, sizeof (name
), "%s@%s",
2150 fsname
, nvpair_name(snapelem
));
2152 error
= recv_destroy(hdl
, name
,
2153 strlen(fsname
)+1, newname
, flags
);
2161 stream_nvfs
= found
;
2163 if (0 == nvlist_lookup_nvlist(stream_nvfs
, "snapprops",
2164 &props
) && 0 == nvlist_lookup_nvlist(props
,
2165 stream_snapname
, &props
)) {
2166 zfs_cmd_t zc
= { 0 };
2168 zc
.zc_cookie
= B_TRUE
; /* received */
2169 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
),
2170 "%s@%s", fsname
, nvpair_name(snapelem
));
2171 if (zcmd_write_src_nvlist(hdl
, &zc
,
2173 (void) zfs_ioctl(hdl
,
2174 ZFS_IOC_SET_PROP
, &zc
);
2175 zcmd_free_nvlists(&zc
);
2179 /* check for different snapname */
2180 if (strcmp(nvpair_name(snapelem
),
2181 stream_snapname
) != 0) {
2182 char name
[ZFS_MAXNAMELEN
];
2183 char tryname
[ZFS_MAXNAMELEN
];
2185 (void) snprintf(name
, sizeof (name
), "%s@%s",
2186 fsname
, nvpair_name(snapelem
));
2187 (void) snprintf(tryname
, sizeof (name
), "%s@%s",
2188 fsname
, stream_snapname
);
2190 error
= recv_rename(hdl
, name
, tryname
,
2191 strlen(fsname
)+1, newname
, flags
);
2198 if (strcmp(stream_snapname
, fromsnap
) == 0)
2199 fromguid
= thisguid
;
2202 /* check for delete */
2203 if (stream_nvfs
== NULL
) {
2207 error
= recv_destroy(hdl
, fsname
, strlen(tofs
)+1,
2216 if (fromguid
== 0) {
2217 if (flags
->verbose
) {
2218 (void) printf("local fs %s does not have "
2219 "fromsnap (%s in stream); must have "
2220 "been deleted locally; ignoring\n",
2226 VERIFY(0 == nvlist_lookup_string(stream_nvfs
,
2227 "name", &stream_fsname
));
2228 VERIFY(0 == nvlist_lookup_uint64(stream_nvfs
,
2229 "parentfromsnap", &stream_parent_fromsnap_guid
));
2231 s1
= strrchr(fsname
, '/');
2232 s2
= strrchr(stream_fsname
, '/');
2235 * Check for rename. If the exact receive path is specified, it
2236 * does not count as a rename, but we still need to check the
2237 * datasets beneath it.
2239 if ((stream_parent_fromsnap_guid
!= 0 &&
2240 parent_fromsnap_guid
!= 0 &&
2241 stream_parent_fromsnap_guid
!= parent_fromsnap_guid
) ||
2242 ((flags
->isprefix
|| strcmp(tofs
, fsname
) != 0) &&
2243 (s1
!= NULL
) && (s2
!= NULL
) && strcmp(s1
, s2
) != 0)) {
2245 char tryname
[ZFS_MAXNAMELEN
];
2247 parent
= fsavl_find(local_avl
,
2248 stream_parent_fromsnap_guid
, NULL
);
2250 * NB: parent might not be found if we used the
2251 * tosnap for stream_parent_fromsnap_guid,
2252 * because the parent is a newly-created fs;
2253 * we'll be able to rename it after we recv the
2256 if (parent
!= NULL
) {
2259 VERIFY(0 == nvlist_lookup_string(parent
, "name",
2261 (void) snprintf(tryname
, sizeof (tryname
),
2262 "%s%s", pname
, strrchr(stream_fsname
, '/'));
2265 if (flags
->verbose
) {
2266 (void) printf("local fs %s new parent "
2267 "not found\n", fsname
);
2273 error
= recv_rename(hdl
, fsname
, tryname
,
2274 strlen(tofs
)+1, newname
, flags
);
2276 if (renamed
!= NULL
&& newname
[0] != '\0') {
2277 VERIFY(0 == nvlist_add_boolean(renamed
,
2288 fsavl_destroy(local_avl
);
2289 nvlist_free(local_nv
);
2291 if (needagain
&& progress
) {
2292 /* do another pass to fix up temporary names */
2294 (void) printf("another pass:\n");
2302 zfs_receive_package(libzfs_handle_t
*hdl
, int fd
, const char *destname
,
2303 recvflags_t
*flags
, dmu_replay_record_t
*drr
, zio_cksum_t
*zc
,
2304 char **top_zfs
, int cleanup_fd
, uint64_t *action_handlep
)
2306 nvlist_t
*stream_nv
= NULL
;
2307 avl_tree_t
*stream_avl
= NULL
;
2308 char *fromsnap
= NULL
;
2310 char tofs
[ZFS_MAXNAMELEN
];
2311 char sendfs
[ZFS_MAXNAMELEN
];
2313 dmu_replay_record_t drre
;
2315 boolean_t anyerr
= B_FALSE
;
2316 boolean_t softerr
= B_FALSE
;
2317 boolean_t recursive
;
2319 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2322 assert(drr
->drr_type
== DRR_BEGIN
);
2323 assert(drr
->drr_u
.drr_begin
.drr_magic
== DMU_BACKUP_MAGIC
);
2324 assert(DMU_GET_STREAM_HDRTYPE(drr
->drr_u
.drr_begin
.drr_versioninfo
) ==
2325 DMU_COMPOUNDSTREAM
);
2328 * Read in the nvlist from the stream.
2330 if (drr
->drr_payloadlen
!= 0) {
2331 error
= recv_read_nvlist(hdl
, fd
, drr
->drr_payloadlen
,
2332 &stream_nv
, flags
->byteswap
, zc
);
2334 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2339 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
2342 if (recursive
&& strchr(destname
, '@')) {
2343 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2344 "cannot specify snapshot name for multi-snapshot stream"));
2345 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2350 * Read in the end record and verify checksum.
2352 if (0 != (error
= recv_read(hdl
, fd
, &drre
, sizeof (drre
),
2353 flags
->byteswap
, NULL
)))
2355 if (flags
->byteswap
) {
2356 drre
.drr_type
= BSWAP_32(drre
.drr_type
);
2357 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[0] =
2358 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[0]);
2359 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[1] =
2360 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[1]);
2361 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[2] =
2362 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[2]);
2363 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[3] =
2364 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[3]);
2366 if (drre
.drr_type
!= DRR_END
) {
2367 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2370 if (!ZIO_CHECKSUM_EQUAL(drre
.drr_u
.drr_end
.drr_checksum
, *zc
)) {
2371 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2372 "incorrect header checksum"));
2373 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2377 (void) nvlist_lookup_string(stream_nv
, "fromsnap", &fromsnap
);
2379 if (drr
->drr_payloadlen
!= 0) {
2380 nvlist_t
*stream_fss
;
2382 VERIFY(0 == nvlist_lookup_nvlist(stream_nv
, "fss",
2384 if ((stream_avl
= fsavl_create(stream_fss
)) == NULL
) {
2385 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2386 "couldn't allocate avl tree"));
2387 error
= zfs_error(hdl
, EZFS_NOMEM
, errbuf
);
2391 if (fromsnap
!= NULL
) {
2392 nvlist_t
*renamed
= NULL
;
2393 nvpair_t
*pair
= NULL
;
2395 (void) strlcpy(tofs
, destname
, ZFS_MAXNAMELEN
);
2396 if (flags
->isprefix
) {
2397 struct drr_begin
*drrb
= &drr
->drr_u
.drr_begin
;
2400 if (flags
->istail
) {
2401 cp
= strrchr(drrb
->drr_toname
, '/');
2403 (void) strlcat(tofs
, "/",
2407 i
= (cp
- drrb
->drr_toname
);
2410 i
= strcspn(drrb
->drr_toname
, "/@");
2412 /* zfs_receive_one() will create_parents() */
2413 (void) strlcat(tofs
, &drrb
->drr_toname
[i
],
2415 *strchr(tofs
, '@') = '\0';
2418 if (recursive
&& !flags
->dryrun
&& !flags
->nomount
) {
2419 VERIFY(0 == nvlist_alloc(&renamed
,
2420 NV_UNIQUE_NAME
, 0));
2423 softerr
= recv_incremental_replication(hdl
, tofs
, flags
,
2424 stream_nv
, stream_avl
, renamed
);
2426 /* Unmount renamed filesystems before receiving. */
2427 while ((pair
= nvlist_next_nvpair(renamed
,
2430 prop_changelist_t
*clp
= NULL
;
2432 zhp
= zfs_open(hdl
, nvpair_name(pair
),
2433 ZFS_TYPE_FILESYSTEM
);
2435 clp
= changelist_gather(zhp
,
2436 ZFS_PROP_MOUNTPOINT
, 0, 0);
2440 changelist_prefix(clp
);
2441 changelist_free(clp
);
2446 nvlist_free(renamed
);
2451 * Get the fs specified by the first path in the stream (the top level
2452 * specified by 'zfs send') and pass it to each invocation of
2453 * zfs_receive_one().
2455 (void) strlcpy(sendfs
, drr
->drr_u
.drr_begin
.drr_toname
,
2457 if ((cp
= strchr(sendfs
, '@')) != NULL
)
2460 /* Finally, receive each contained stream */
2463 * we should figure out if it has a recoverable
2464 * error, in which case do a recv_skip() and drive on.
2465 * Note, if we fail due to already having this guid,
2466 * zfs_receive_one() will take care of it (ie,
2467 * recv_skip() and return 0).
2469 error
= zfs_receive_impl(hdl
, destname
, NULL
, flags
, fd
,
2470 sendfs
, stream_nv
, stream_avl
, top_zfs
, cleanup_fd
,
2472 if (error
== ENODATA
) {
2477 } while (error
== 0);
2479 if (drr
->drr_payloadlen
!= 0 && fromsnap
!= NULL
) {
2481 * Now that we have the fs's they sent us, try the
2484 softerr
= recv_incremental_replication(hdl
, tofs
, flags
,
2485 stream_nv
, stream_avl
, NULL
);
2489 fsavl_destroy(stream_avl
);
2491 nvlist_free(stream_nv
);
2500 trunc_prop_errs(int truncated
)
2502 ASSERT(truncated
!= 0);
2505 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
2506 "1 more property could not be set\n"));
2508 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
2509 "%d more properties could not be set\n"), truncated
);
2513 recv_skip(libzfs_handle_t
*hdl
, int fd
, boolean_t byteswap
)
2515 dmu_replay_record_t
*drr
;
2516 void *buf
= zfs_alloc(hdl
, SPA_MAXBLOCKSIZE
);
2519 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2520 "cannot receive:"));
2522 /* XXX would be great to use lseek if possible... */
2525 while (recv_read(hdl
, fd
, drr
, sizeof (dmu_replay_record_t
),
2526 byteswap
, NULL
) == 0) {
2528 drr
->drr_type
= BSWAP_32(drr
->drr_type
);
2530 switch (drr
->drr_type
) {
2532 /* NB: not to be used on v2 stream packages */
2533 if (drr
->drr_payloadlen
!= 0) {
2534 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2535 "invalid substream header"));
2536 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
2546 drr
->drr_u
.drr_object
.drr_bonuslen
=
2547 BSWAP_32(drr
->drr_u
.drr_object
.
2550 (void) recv_read(hdl
, fd
, buf
,
2551 P2ROUNDUP(drr
->drr_u
.drr_object
.drr_bonuslen
, 8),
2557 drr
->drr_u
.drr_write
.drr_length
=
2558 BSWAP_64(drr
->drr_u
.drr_write
.drr_length
);
2560 (void) recv_read(hdl
, fd
, buf
,
2561 drr
->drr_u
.drr_write
.drr_length
, B_FALSE
, NULL
);
2565 drr
->drr_u
.drr_write
.drr_length
=
2566 BSWAP_64(drr
->drr_u
.drr_spill
.drr_length
);
2568 (void) recv_read(hdl
, fd
, buf
,
2569 drr
->drr_u
.drr_spill
.drr_length
, B_FALSE
, NULL
);
2571 case DRR_WRITE_EMBEDDED
:
2573 drr
->drr_u
.drr_write_embedded
.drr_psize
=
2574 BSWAP_32(drr
->drr_u
.drr_write_embedded
.
2577 (void) recv_read(hdl
, fd
, buf
,
2578 P2ROUNDUP(drr
->drr_u
.drr_write_embedded
.drr_psize
,
2581 case DRR_WRITE_BYREF
:
2582 case DRR_FREEOBJECTS
:
2587 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2588 "invalid record type"));
2589 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
2598 * Restores a backup of tosnap from the file descriptor specified by infd.
2601 zfs_receive_one(libzfs_handle_t
*hdl
, int infd
, const char *tosnap
,
2602 const char *originsnap
, recvflags_t
*flags
, dmu_replay_record_t
*drr
,
2603 dmu_replay_record_t
*drr_noswap
, const char *sendfs
, nvlist_t
*stream_nv
,
2604 avl_tree_t
*stream_avl
, char **top_zfs
, int cleanup_fd
,
2605 uint64_t *action_handlep
)
2607 zfs_cmd_t zc
= { 0 };
2609 int ioctl_err
, ioctl_errno
, err
;
2611 struct drr_begin
*drrb
= &drr
->drr_u
.drr_begin
;
2613 char prop_errbuf
[1024];
2614 const char *chopprefix
;
2615 boolean_t newfs
= B_FALSE
;
2616 boolean_t stream_wantsnewfs
;
2617 uint64_t parent_snapguid
= 0;
2618 prop_changelist_t
*clp
= NULL
;
2619 nvlist_t
*snapprops_nvlist
= NULL
;
2620 zprop_errflags_t prop_errflags
;
2621 boolean_t recursive
;
2623 begin_time
= time(NULL
);
2625 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2628 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
2631 if (stream_avl
!= NULL
) {
2633 nvlist_t
*fs
= fsavl_find(stream_avl
, drrb
->drr_toguid
,
2638 (void) nvlist_lookup_uint64(fs
, "parentfromsnap",
2640 err
= nvlist_lookup_nvlist(fs
, "props", &props
);
2642 VERIFY(0 == nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0));
2644 if (flags
->canmountoff
) {
2645 VERIFY(0 == nvlist_add_uint64(props
,
2646 zfs_prop_to_name(ZFS_PROP_CANMOUNT
), 0));
2648 ret
= zcmd_write_src_nvlist(hdl
, &zc
, props
);
2652 if (0 == nvlist_lookup_nvlist(fs
, "snapprops", &props
)) {
2653 VERIFY(0 == nvlist_lookup_nvlist(props
,
2654 snapname
, &snapprops_nvlist
));
2664 * Determine how much of the snapshot name stored in the stream
2665 * we are going to tack on to the name they specified on the
2666 * command line, and how much we are going to chop off.
2668 * If they specified a snapshot, chop the entire name stored in
2671 if (flags
->istail
) {
2673 * A filesystem was specified with -e. We want to tack on only
2674 * the tail of the sent snapshot path.
2676 if (strchr(tosnap
, '@')) {
2677 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
2678 "argument - snapshot not allowed with -e"));
2679 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2682 chopprefix
= strrchr(sendfs
, '/');
2684 if (chopprefix
== NULL
) {
2686 * The tail is the poolname, so we need to
2687 * prepend a path separator.
2689 int len
= strlen(drrb
->drr_toname
);
2690 cp
= malloc(len
+ 2);
2692 (void) strcpy(&cp
[1], drrb
->drr_toname
);
2695 chopprefix
= drrb
->drr_toname
+ (chopprefix
- sendfs
);
2697 } else if (flags
->isprefix
) {
2699 * A filesystem was specified with -d. We want to tack on
2700 * everything but the first element of the sent snapshot path
2701 * (all but the pool name).
2703 if (strchr(tosnap
, '@')) {
2704 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
2705 "argument - snapshot not allowed with -d"));
2706 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2709 chopprefix
= strchr(drrb
->drr_toname
, '/');
2710 if (chopprefix
== NULL
)
2711 chopprefix
= strchr(drrb
->drr_toname
, '@');
2712 } else if (strchr(tosnap
, '@') == NULL
) {
2714 * If a filesystem was specified without -d or -e, we want to
2715 * tack on everything after the fs specified by 'zfs send'.
2717 chopprefix
= drrb
->drr_toname
+ strlen(sendfs
);
2719 /* A snapshot was specified as an exact path (no -d or -e). */
2721 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2722 "cannot specify snapshot name for multi-snapshot "
2724 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
2726 chopprefix
= drrb
->drr_toname
+ strlen(drrb
->drr_toname
);
2729 ASSERT(strstr(drrb
->drr_toname
, sendfs
) == drrb
->drr_toname
);
2730 ASSERT(chopprefix
> drrb
->drr_toname
);
2731 ASSERT(chopprefix
<= drrb
->drr_toname
+ strlen(drrb
->drr_toname
));
2732 ASSERT(chopprefix
[0] == '/' || chopprefix
[0] == '@' ||
2733 chopprefix
[0] == '\0');
2736 * Determine name of destination snapshot, store in zc_value.
2738 (void) strcpy(zc
.zc_value
, tosnap
);
2739 (void) strncat(zc
.zc_value
, chopprefix
, sizeof (zc
.zc_value
));
2741 if (!zfs_name_valid(zc
.zc_value
, ZFS_TYPE_SNAPSHOT
)) {
2742 zcmd_free_nvlists(&zc
);
2743 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2747 * Determine the name of the origin snapshot, store in zc_string.
2749 if (drrb
->drr_flags
& DRR_FLAG_CLONE
) {
2750 if (guid_to_name(hdl
, zc
.zc_value
,
2751 drrb
->drr_fromguid
, zc
.zc_string
) != 0) {
2752 zcmd_free_nvlists(&zc
);
2753 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2754 "local origin for clone %s does not exist"),
2756 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2759 (void) printf("found clone origin %s\n", zc
.zc_string
);
2760 } else if (originsnap
) {
2761 (void) strncpy(zc
.zc_string
, originsnap
, ZFS_MAXNAMELEN
);
2763 (void) printf("using provided clone origin %s\n",
2767 stream_wantsnewfs
= (drrb
->drr_fromguid
== NULL
||
2768 (drrb
->drr_flags
& DRR_FLAG_CLONE
) || originsnap
);
2770 if (stream_wantsnewfs
) {
2772 * if the parent fs does not exist, look for it based on
2773 * the parent snap GUID
2775 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2776 "cannot receive new filesystem stream"));
2778 (void) strcpy(zc
.zc_name
, zc
.zc_value
);
2779 cp
= strrchr(zc
.zc_name
, '/');
2783 !zfs_dataset_exists(hdl
, zc
.zc_name
, ZFS_TYPE_DATASET
)) {
2784 char suffix
[ZFS_MAXNAMELEN
];
2785 (void) strcpy(suffix
, strrchr(zc
.zc_value
, '/'));
2786 if (guid_to_name(hdl
, zc
.zc_name
, parent_snapguid
,
2787 zc
.zc_value
) == 0) {
2788 *strchr(zc
.zc_value
, '@') = '\0';
2789 (void) strcat(zc
.zc_value
, suffix
);
2794 * if the fs does not exist, look for it based on the
2797 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2798 "cannot receive incremental stream"));
2800 (void) strcpy(zc
.zc_name
, zc
.zc_value
);
2801 *strchr(zc
.zc_name
, '@') = '\0';
2804 * If the exact receive path was specified and this is the
2805 * topmost path in the stream, then if the fs does not exist we
2806 * should look no further.
2808 if ((flags
->isprefix
|| (*(chopprefix
= drrb
->drr_toname
+
2809 strlen(sendfs
)) != '\0' && *chopprefix
!= '@')) &&
2810 !zfs_dataset_exists(hdl
, zc
.zc_name
, ZFS_TYPE_DATASET
)) {
2811 char snap
[ZFS_MAXNAMELEN
];
2812 (void) strcpy(snap
, strchr(zc
.zc_value
, '@'));
2813 if (guid_to_name(hdl
, zc
.zc_name
, drrb
->drr_fromguid
,
2814 zc
.zc_value
) == 0) {
2815 *strchr(zc
.zc_value
, '@') = '\0';
2816 (void) strcat(zc
.zc_value
, snap
);
2821 (void) strcpy(zc
.zc_name
, zc
.zc_value
);
2822 *strchr(zc
.zc_name
, '@') = '\0';
2824 if (zfs_dataset_exists(hdl
, zc
.zc_name
, ZFS_TYPE_DATASET
)) {
2828 * Destination fs exists. Therefore this should either
2829 * be an incremental, or the stream specifies a new fs
2830 * (full stream or clone) and they want us to blow it
2831 * away (and have therefore specified -F and removed any
2834 if (stream_wantsnewfs
) {
2835 if (!flags
->force
) {
2836 zcmd_free_nvlists(&zc
);
2837 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2838 "destination '%s' exists\n"
2839 "must specify -F to overwrite it"),
2841 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
2843 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_SNAPSHOT_LIST_NEXT
,
2845 zcmd_free_nvlists(&zc
);
2846 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2847 "destination has snapshots (eg. %s)\n"
2848 "must destroy them to overwrite it"),
2850 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
2854 if ((zhp
= zfs_open(hdl
, zc
.zc_name
,
2855 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
)) == NULL
) {
2856 zcmd_free_nvlists(&zc
);
2860 if (stream_wantsnewfs
&&
2861 zhp
->zfs_dmustats
.dds_origin
[0]) {
2862 zcmd_free_nvlists(&zc
);
2864 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2865 "destination '%s' is a clone\n"
2866 "must destroy it to overwrite it"),
2868 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
2871 if (!flags
->dryrun
&& zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
&&
2872 stream_wantsnewfs
) {
2873 /* We can't do online recv in this case */
2874 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0, 0);
2877 zcmd_free_nvlists(&zc
);
2880 if (changelist_prefix(clp
) != 0) {
2881 changelist_free(clp
);
2883 zcmd_free_nvlists(&zc
);
2890 * Destination filesystem does not exist. Therefore we better
2891 * be creating a new filesystem (either from a full backup, or
2892 * a clone). It would therefore be invalid if the user
2893 * specified only the pool name (i.e. if the destination name
2894 * contained no slash character).
2896 if (!stream_wantsnewfs
||
2897 (cp
= strrchr(zc
.zc_name
, '/')) == NULL
) {
2898 zcmd_free_nvlists(&zc
);
2899 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2900 "destination '%s' does not exist"), zc
.zc_name
);
2901 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2905 * Trim off the final dataset component so we perform the
2906 * recvbackup ioctl to the filesystems's parent.
2910 if (flags
->isprefix
&& !flags
->istail
&& !flags
->dryrun
&&
2911 create_parents(hdl
, zc
.zc_value
, strlen(tosnap
)) != 0) {
2912 zcmd_free_nvlists(&zc
);
2913 return (zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
));
2919 zc
.zc_begin_record
= drr_noswap
->drr_u
.drr_begin
;
2920 zc
.zc_cookie
= infd
;
2921 zc
.zc_guid
= flags
->force
;
2922 if (flags
->verbose
) {
2923 (void) printf("%s %s stream of %s into %s\n",
2924 flags
->dryrun
? "would receive" : "receiving",
2925 drrb
->drr_fromguid
? "incremental" : "full",
2926 drrb
->drr_toname
, zc
.zc_value
);
2927 (void) fflush(stdout
);
2930 if (flags
->dryrun
) {
2931 zcmd_free_nvlists(&zc
);
2932 return (recv_skip(hdl
, infd
, flags
->byteswap
));
2935 zc
.zc_nvlist_dst
= (uint64_t)(uintptr_t)prop_errbuf
;
2936 zc
.zc_nvlist_dst_size
= sizeof (prop_errbuf
);
2937 zc
.zc_cleanup_fd
= cleanup_fd
;
2938 zc
.zc_action_handle
= *action_handlep
;
2940 err
= ioctl_err
= zfs_ioctl(hdl
, ZFS_IOC_RECV
, &zc
);
2941 ioctl_errno
= errno
;
2942 prop_errflags
= (zprop_errflags_t
)zc
.zc_obj
;
2945 nvlist_t
*prop_errors
;
2946 VERIFY(0 == nvlist_unpack((void *)(uintptr_t)zc
.zc_nvlist_dst
,
2947 zc
.zc_nvlist_dst_size
, &prop_errors
, 0));
2949 nvpair_t
*prop_err
= NULL
;
2951 while ((prop_err
= nvlist_next_nvpair(prop_errors
,
2952 prop_err
)) != NULL
) {
2957 prop
= zfs_name_to_prop(nvpair_name(prop_err
));
2958 (void) nvpair_value_int32(prop_err
, &intval
);
2959 if (strcmp(nvpair_name(prop_err
),
2960 ZPROP_N_MORE_ERRORS
) == 0) {
2961 trunc_prop_errs(intval
);
2964 (void) snprintf(tbuf
, sizeof (tbuf
),
2965 dgettext(TEXT_DOMAIN
,
2966 "cannot receive %s property on %s"),
2967 nvpair_name(prop_err
), zc
.zc_name
);
2968 zfs_setprop_error(hdl
, prop
, intval
, tbuf
);
2971 nvlist_free(prop_errors
);
2974 zc
.zc_nvlist_dst
= 0;
2975 zc
.zc_nvlist_dst_size
= 0;
2976 zcmd_free_nvlists(&zc
);
2978 if (err
== 0 && snapprops_nvlist
) {
2979 zfs_cmd_t zc2
= { 0 };
2981 (void) strcpy(zc2
.zc_name
, zc
.zc_value
);
2982 zc2
.zc_cookie
= B_TRUE
; /* received */
2983 if (zcmd_write_src_nvlist(hdl
, &zc2
, snapprops_nvlist
) == 0) {
2984 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc2
);
2985 zcmd_free_nvlists(&zc2
);
2989 if (err
&& (ioctl_errno
== ENOENT
|| ioctl_errno
== EEXIST
)) {
2991 * It may be that this snapshot already exists,
2992 * in which case we want to consume & ignore it
2993 * rather than failing.
2995 avl_tree_t
*local_avl
;
2996 nvlist_t
*local_nv
, *fs
;
2997 cp
= strchr(zc
.zc_value
, '@');
3000 * XXX Do this faster by just iterating over snaps in
3001 * this fs. Also if zc_value does not exist, we will
3002 * get a strange "does not exist" error message.
3005 if (gather_nvlist(hdl
, zc
.zc_value
, NULL
, NULL
, B_FALSE
,
3006 &local_nv
, &local_avl
) == 0) {
3008 fs
= fsavl_find(local_avl
, drrb
->drr_toguid
, NULL
);
3009 fsavl_destroy(local_avl
);
3010 nvlist_free(local_nv
);
3013 if (flags
->verbose
) {
3014 (void) printf("snap %s already exists; "
3015 "ignoring\n", zc
.zc_value
);
3017 err
= ioctl_err
= recv_skip(hdl
, infd
,
3024 if (ioctl_err
!= 0) {
3025 switch (ioctl_errno
) {
3027 cp
= strchr(zc
.zc_value
, '@');
3029 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3030 "most recent snapshot of %s does not\n"
3031 "match incremental source"), zc
.zc_value
);
3032 (void) zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
3036 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3037 "destination %s has been modified\n"
3038 "since most recent snapshot"), zc
.zc_name
);
3039 (void) zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
3042 cp
= strchr(zc
.zc_value
, '@');
3044 /* it's the containing fs that exists */
3047 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3048 "destination already exists"));
3049 (void) zfs_error_fmt(hdl
, EZFS_EXISTS
,
3050 dgettext(TEXT_DOMAIN
, "cannot restore to %s"),
3055 (void) zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
3058 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3059 "invalid stream (checksum mismatch)"));
3060 (void) zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
3063 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3064 "pool must be upgraded to receive this stream."));
3065 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
3068 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3069 "destination %s space quota exceeded"), zc
.zc_name
);
3070 (void) zfs_error(hdl
, EZFS_NOSPC
, errbuf
);
3073 (void) zfs_standard_error(hdl
, ioctl_errno
, errbuf
);
3078 * Mount the target filesystem (if created). Also mount any
3079 * children of the target filesystem if we did a replication
3080 * receive (indicated by stream_avl being non-NULL).
3082 cp
= strchr(zc
.zc_value
, '@');
3083 if (cp
&& (ioctl_err
== 0 || !newfs
)) {
3087 h
= zfs_open(hdl
, zc
.zc_value
,
3088 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3090 if (h
->zfs_type
== ZFS_TYPE_VOLUME
) {
3092 } else if (newfs
|| stream_avl
) {
3094 * Track the first/top of hierarchy fs,
3095 * for mounting and sharing later.
3097 if (top_zfs
&& *top_zfs
== NULL
)
3098 *top_zfs
= zfs_strdup(hdl
, zc
.zc_value
);
3106 err
|= changelist_postfix(clp
);
3107 changelist_free(clp
);
3110 if (prop_errflags
& ZPROP_ERR_NOCLEAR
) {
3111 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Warning: "
3112 "failed to clear unreceived properties on %s"),
3114 (void) fprintf(stderr
, "\n");
3116 if (prop_errflags
& ZPROP_ERR_NORESTORE
) {
3117 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Warning: "
3118 "failed to restore original properties on %s"),
3120 (void) fprintf(stderr
, "\n");
3123 if (err
|| ioctl_err
)
3126 *action_handlep
= zc
.zc_action_handle
;
3128 if (flags
->verbose
) {
3131 uint64_t bytes
= zc
.zc_cookie
;
3132 time_t delta
= time(NULL
) - begin_time
;
3135 zfs_nicenum(bytes
, buf1
, sizeof (buf1
));
3136 zfs_nicenum(bytes
/delta
, buf2
, sizeof (buf1
));
3138 (void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
3146 zfs_receive_impl(libzfs_handle_t
*hdl
, const char *tosnap
,
3147 const char *originsnap
, recvflags_t
*flags
, int infd
, const char *sendfs
,
3148 nvlist_t
*stream_nv
, avl_tree_t
*stream_avl
, char **top_zfs
, int cleanup_fd
,
3149 uint64_t *action_handlep
)
3152 dmu_replay_record_t drr
, drr_noswap
;
3153 struct drr_begin
*drrb
= &drr
.drr_u
.drr_begin
;
3155 zio_cksum_t zcksum
= { 0 };
3156 uint64_t featureflags
;
3159 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3162 if (flags
->isprefix
&&
3163 !zfs_dataset_exists(hdl
, tosnap
, ZFS_TYPE_DATASET
)) {
3164 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "specified fs "
3165 "(%s) does not exist"), tosnap
);
3166 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3169 !zfs_dataset_exists(hdl
, originsnap
, ZFS_TYPE_DATASET
)) {
3170 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "specified origin fs "
3171 "(%s) does not exist"), originsnap
);
3172 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3175 /* read in the BEGIN record */
3176 if (0 != (err
= recv_read(hdl
, infd
, &drr
, sizeof (drr
), B_FALSE
,
3180 if (drr
.drr_type
== DRR_END
|| drr
.drr_type
== BSWAP_32(DRR_END
)) {
3181 /* It's the double end record at the end of a package */
3185 /* the kernel needs the non-byteswapped begin record */
3188 flags
->byteswap
= B_FALSE
;
3189 if (drrb
->drr_magic
== BSWAP_64(DMU_BACKUP_MAGIC
)) {
3191 * We computed the checksum in the wrong byteorder in
3192 * recv_read() above; do it again correctly.
3194 bzero(&zcksum
, sizeof (zio_cksum_t
));
3195 fletcher_4_incremental_byteswap(&drr
, sizeof (drr
), &zcksum
);
3196 flags
->byteswap
= B_TRUE
;
3198 drr
.drr_type
= BSWAP_32(drr
.drr_type
);
3199 drr
.drr_payloadlen
= BSWAP_32(drr
.drr_payloadlen
);
3200 drrb
->drr_magic
= BSWAP_64(drrb
->drr_magic
);
3201 drrb
->drr_versioninfo
= BSWAP_64(drrb
->drr_versioninfo
);
3202 drrb
->drr_creation_time
= BSWAP_64(drrb
->drr_creation_time
);
3203 drrb
->drr_type
= BSWAP_32(drrb
->drr_type
);
3204 drrb
->drr_flags
= BSWAP_32(drrb
->drr_flags
);
3205 drrb
->drr_toguid
= BSWAP_64(drrb
->drr_toguid
);
3206 drrb
->drr_fromguid
= BSWAP_64(drrb
->drr_fromguid
);
3209 if (drrb
->drr_magic
!= DMU_BACKUP_MAGIC
|| drr
.drr_type
!= DRR_BEGIN
) {
3210 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
3211 "stream (bad magic number)"));
3212 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3215 featureflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
3216 hdrtype
= DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
);
3218 if (!DMU_STREAM_SUPPORTED(featureflags
) ||
3219 (hdrtype
!= DMU_SUBSTREAM
&& hdrtype
!= DMU_COMPOUNDSTREAM
)) {
3220 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3221 "stream has unsupported feature, feature flags = %lx"),
3223 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3226 if (strchr(drrb
->drr_toname
, '@') == NULL
) {
3227 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
3228 "stream (bad snapshot name)"));
3229 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3232 if (DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) == DMU_SUBSTREAM
) {
3233 char nonpackage_sendfs
[ZFS_MAXNAMELEN
];
3234 if (sendfs
== NULL
) {
3236 * We were not called from zfs_receive_package(). Get
3237 * the fs specified by 'zfs send'.
3240 (void) strlcpy(nonpackage_sendfs
,
3241 drr
.drr_u
.drr_begin
.drr_toname
, ZFS_MAXNAMELEN
);
3242 if ((cp
= strchr(nonpackage_sendfs
, '@')) != NULL
)
3244 sendfs
= nonpackage_sendfs
;
3246 return (zfs_receive_one(hdl
, infd
, tosnap
, originsnap
, flags
,
3247 &drr
, &drr_noswap
, sendfs
, stream_nv
, stream_avl
, top_zfs
,
3248 cleanup_fd
, action_handlep
));
3250 assert(DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) ==
3251 DMU_COMPOUNDSTREAM
);
3252 return (zfs_receive_package(hdl
, infd
, tosnap
, flags
, &drr
,
3253 &zcksum
, top_zfs
, cleanup_fd
, action_handlep
));
3258 * Restores a backup of tosnap from the file descriptor specified by infd.
3259 * Return 0 on total success, -2 if some things couldn't be
3260 * destroyed/renamed/promoted, -1 if some things couldn't be received.
3261 * (-1 will override -2).
3264 zfs_receive(libzfs_handle_t
*hdl
, const char *tosnap
, nvlist_t
*props
,
3265 recvflags_t
*flags
, int infd
, avl_tree_t
*stream_avl
)
3267 char *top_zfs
= NULL
;
3270 uint64_t action_handle
= 0;
3271 char *originsnap
= NULL
;
3273 err
= nvlist_lookup_string(props
, "origin", &originsnap
);
3274 if (err
&& err
!= ENOENT
)
3278 cleanup_fd
= open(ZFS_DEV
, O_RDWR
|O_EXCL
);
3279 VERIFY(cleanup_fd
>= 0);
3281 err
= zfs_receive_impl(hdl
, tosnap
, originsnap
, flags
, infd
, NULL
, NULL
,
3282 stream_avl
, &top_zfs
, cleanup_fd
, &action_handle
);
3284 VERIFY(0 == close(cleanup_fd
));
3286 if (err
== 0 && !flags
->nomount
&& top_zfs
) {
3288 prop_changelist_t
*clp
;
3290 zhp
= zfs_open(hdl
, top_zfs
, ZFS_TYPE_FILESYSTEM
);
3292 clp
= changelist_gather(zhp
, ZFS_PROP_MOUNTPOINT
,
3293 CL_GATHER_MOUNT_ALWAYS
, 0);
3296 /* mount and share received datasets */
3297 err
= changelist_postfix(clp
);
3298 changelist_free(clp
);
3301 if (zhp
== NULL
|| clp
== NULL
|| err
)