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, 2015 by Delphix. All rights reserved.
25 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
26 * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
28 * Copyright (c) 2013 Steven Hartland. All rights reserved.
29 * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
42 #include <sys/mount.h>
43 #include <sys/mntent.h>
44 #include <sys/mnttab.h>
46 #include <sys/debug.h>
54 #include <libzfs_core.h>
56 #include "zfs_namecheck.h"
58 #include "zfs_fletcher.h"
59 #include "libzfs_impl.h"
61 #include <sys/zio_checksum.h>
63 #include <sys/socket.h>
66 /* in libzfs_dataset.c */
67 extern void zfs_setprop_error(libzfs_handle_t
*, zfs_prop_t
, int, char *);
69 static int zfs_receive_impl(libzfs_handle_t
*, const char *, const char *,
70 recvflags_t
*, int, const char *, nvlist_t
*, avl_tree_t
*, char **, int,
71 uint64_t *, const char *);
72 static int guid_to_name(libzfs_handle_t
*, const char *,
73 uint64_t, boolean_t
, char *);
75 static const zio_cksum_t zero_cksum
= { { 0 } };
77 typedef struct dedup_arg
{
80 libzfs_handle_t
*dedup_hdl
;
83 typedef struct progress_arg
{
86 boolean_t pa_parsable
;
89 typedef struct dataref
{
95 typedef struct dedup_entry
{
96 struct dedup_entry
*dde_next
;
97 zio_cksum_t dde_chksum
;
102 #define MAX_DDT_PHYSMEM_PERCENT 20
103 #define SMALLEST_POSSIBLE_MAX_DDT_MB 128
105 typedef struct dedup_table
{
106 dedup_entry_t
**dedup_hash_array
;
107 umem_cache_t
*ddecache
;
108 uint64_t max_ddt_size
; /* max dedup table size in bytes */
109 uint64_t cur_ddt_size
; /* current dedup table size in bytes */
116 high_order_bit(uint64_t n
)
120 for (count
= 0; n
!= 0; count
++)
126 ssread(void *buf
, size_t len
, FILE *stream
)
130 if ((outlen
= fread(buf
, len
, 1, stream
)) == 0)
137 ddt_hash_append(libzfs_handle_t
*hdl
, dedup_table_t
*ddt
, dedup_entry_t
**ddepp
,
138 zio_cksum_t
*cs
, uint64_t prop
, dataref_t
*dr
)
142 if (ddt
->cur_ddt_size
>= ddt
->max_ddt_size
) {
143 if (ddt
->ddt_full
== B_FALSE
) {
144 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
145 "Dedup table full. Deduplication will continue "
146 "with existing table entries"));
147 ddt
->ddt_full
= B_TRUE
;
152 if ((dde
= umem_cache_alloc(ddt
->ddecache
, UMEM_DEFAULT
))
154 assert(*ddepp
== NULL
);
155 dde
->dde_next
= NULL
;
156 dde
->dde_chksum
= *cs
;
157 dde
->dde_prop
= prop
;
160 ddt
->cur_ddt_size
+= sizeof (dedup_entry_t
);
166 * Using the specified dedup table, do a lookup for an entry with
167 * the checksum cs. If found, return the block's reference info
168 * in *dr. Otherwise, insert a new entry in the dedup table, using
169 * the reference information specified by *dr.
171 * return value: true - entry was found
172 * false - entry was not found
175 ddt_update(libzfs_handle_t
*hdl
, dedup_table_t
*ddt
, zio_cksum_t
*cs
,
176 uint64_t prop
, dataref_t
*dr
)
179 dedup_entry_t
**ddepp
;
181 hashcode
= BF64_GET(cs
->zc_word
[0], 0, ddt
->numhashbits
);
183 for (ddepp
= &(ddt
->dedup_hash_array
[hashcode
]); *ddepp
!= NULL
;
184 ddepp
= &((*ddepp
)->dde_next
)) {
185 if (ZIO_CHECKSUM_EQUAL(((*ddepp
)->dde_chksum
), *cs
) &&
186 (*ddepp
)->dde_prop
== prop
) {
187 *dr
= (*ddepp
)->dde_ref
;
191 ddt_hash_append(hdl
, ddt
, ddepp
, cs
, prop
, dr
);
196 dump_record(dmu_replay_record_t
*drr
, void *payload
, int payload_len
,
197 zio_cksum_t
*zc
, int outfd
)
199 ASSERT3U(offsetof(dmu_replay_record_t
, drr_u
.drr_checksum
.drr_checksum
),
200 ==, sizeof (dmu_replay_record_t
) - sizeof (zio_cksum_t
));
201 fletcher_4_incremental_native(drr
,
202 offsetof(dmu_replay_record_t
, drr_u
.drr_checksum
.drr_checksum
), zc
);
203 if (drr
->drr_type
!= DRR_BEGIN
) {
204 ASSERT(ZIO_CHECKSUM_IS_ZERO(&drr
->drr_u
.
205 drr_checksum
.drr_checksum
));
206 drr
->drr_u
.drr_checksum
.drr_checksum
= *zc
;
208 fletcher_4_incremental_native(&drr
->drr_u
.drr_checksum
.drr_checksum
,
209 sizeof (zio_cksum_t
), zc
);
210 if (write(outfd
, drr
, sizeof (*drr
)) == -1)
212 if (payload_len
!= 0) {
213 fletcher_4_incremental_native(payload
, payload_len
, zc
);
214 if (write(outfd
, payload
, payload_len
) == -1)
221 * This function is started in a separate thread when the dedup option
222 * has been requested. The main send thread determines the list of
223 * snapshots to be included in the send stream and makes the ioctl calls
224 * for each one. But instead of having the ioctl send the output to the
225 * the output fd specified by the caller of zfs_send()), the
226 * ioctl is told to direct the output to a pipe, which is read by the
227 * alternate thread running THIS function. This function does the
229 * 1. building a dedup table (the DDT)
230 * 2. doing checksums on each data block and inserting a record in the DDT
231 * 3. looking for matching checksums, and
232 * 4. sending a DRR_WRITE_BYREF record instead of a write record whenever
233 * a duplicate block is found.
234 * The output of this function then goes to the output fd requested
235 * by the caller of zfs_send().
240 dedup_arg_t
*dda
= arg
;
241 char *buf
= zfs_alloc(dda
->dedup_hdl
, SPA_MAXBLOCKSIZE
);
242 dmu_replay_record_t thedrr
= { 0 };
243 dmu_replay_record_t
*drr
= &thedrr
;
247 zio_cksum_t stream_cksum
;
251 ddt
.max_ddt_size
= SMALLEST_POSSIBLE_MAX_DDT_MB
<< 20;
253 uint64_t physmem
= sysconf(_SC_PHYS_PAGES
) * sysconf(_SC_PAGESIZE
);
255 MAX((physmem
* MAX_DDT_PHYSMEM_PERCENT
) / 100,
256 SMALLEST_POSSIBLE_MAX_DDT_MB
<< 20);
259 numbuckets
= ddt
.max_ddt_size
/ (sizeof (dedup_entry_t
));
262 * numbuckets must be a power of 2. Increase number to
263 * a power of 2 if necessary.
265 if (!ISP2(numbuckets
))
266 numbuckets
= 1ULL << high_order_bit(numbuckets
);
268 ddt
.dedup_hash_array
= calloc(numbuckets
, sizeof (dedup_entry_t
*));
269 ddt
.ddecache
= umem_cache_create("dde", sizeof (dedup_entry_t
), 0,
270 NULL
, NULL
, NULL
, NULL
, NULL
, 0);
271 ddt
.cur_ddt_size
= numbuckets
* sizeof (dedup_entry_t
*);
272 ddt
.numhashbits
= high_order_bit(numbuckets
) - 1;
273 ddt
.ddt_full
= B_FALSE
;
275 outfd
= dda
->outputfd
;
276 ofp
= fdopen(dda
->inputfd
, "r");
277 while (ssread(drr
, sizeof (*drr
), ofp
) != 0) {
280 * kernel filled in checksum, we are going to write same
281 * record, but need to regenerate checksum.
283 if (drr
->drr_type
!= DRR_BEGIN
) {
284 bzero(&drr
->drr_u
.drr_checksum
.drr_checksum
,
285 sizeof (drr
->drr_u
.drr_checksum
.drr_checksum
));
288 switch (drr
->drr_type
) {
291 struct drr_begin
*drrb
= &drr
->drr_u
.drr_begin
;
294 ZIO_SET_CHECKSUM(&stream_cksum
, 0, 0, 0, 0);
296 ASSERT3U(drrb
->drr_magic
, ==, DMU_BACKUP_MAGIC
);
298 /* set the DEDUP feature flag for this stream */
299 fflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
300 fflags
|= (DMU_BACKUP_FEATURE_DEDUP
|
301 DMU_BACKUP_FEATURE_DEDUPPROPS
);
302 DMU_SET_FEATUREFLAGS(drrb
->drr_versioninfo
, fflags
);
304 if (drr
->drr_payloadlen
!= 0) {
305 sz
= drr
->drr_payloadlen
;
307 if (sz
> SPA_MAXBLOCKSIZE
) {
308 buf
= zfs_realloc(dda
->dedup_hdl
, buf
,
309 SPA_MAXBLOCKSIZE
, sz
);
311 (void) ssread(buf
, sz
, ofp
);
315 if (dump_record(drr
, buf
, sz
, &stream_cksum
,
323 struct drr_end
*drre
= &drr
->drr_u
.drr_end
;
324 /* use the recalculated checksum */
325 drre
->drr_checksum
= stream_cksum
;
326 if (dump_record(drr
, NULL
, 0, &stream_cksum
,
334 struct drr_object
*drro
= &drr
->drr_u
.drr_object
;
335 if (drro
->drr_bonuslen
> 0) {
337 P2ROUNDUP((uint64_t)drro
->drr_bonuslen
, 8),
340 if (dump_record(drr
, buf
,
341 P2ROUNDUP((uint64_t)drro
->drr_bonuslen
, 8),
342 &stream_cksum
, outfd
) != 0)
349 struct drr_spill
*drrs
= &drr
->drr_u
.drr_spill
;
350 (void) ssread(buf
, drrs
->drr_length
, ofp
);
351 if (dump_record(drr
, buf
, drrs
->drr_length
,
352 &stream_cksum
, outfd
) != 0)
357 case DRR_FREEOBJECTS
:
359 if (dump_record(drr
, NULL
, 0, &stream_cksum
,
367 struct drr_write
*drrw
= &drr
->drr_u
.drr_write
;
369 uint64_t payload_size
;
371 payload_size
= DRR_WRITE_PAYLOAD_SIZE(drrw
);
372 (void) ssread(buf
, payload_size
, ofp
);
375 * Use the existing checksum if it's dedup-capable,
376 * else calculate a SHA256 checksum for it.
379 if (ZIO_CHECKSUM_EQUAL(drrw
->drr_key
.ddk_cksum
,
381 !DRR_IS_DEDUP_CAPABLE(drrw
->drr_checksumflags
)) {
383 zio_cksum_t tmpsha256
;
385 SHA2Init(SHA256
, &ctx
);
386 SHA2Update(&ctx
, buf
, payload_size
);
387 SHA2Final(&tmpsha256
, &ctx
);
389 drrw
->drr_key
.ddk_cksum
.zc_word
[0] =
390 BE_64(tmpsha256
.zc_word
[0]);
391 drrw
->drr_key
.ddk_cksum
.zc_word
[1] =
392 BE_64(tmpsha256
.zc_word
[1]);
393 drrw
->drr_key
.ddk_cksum
.zc_word
[2] =
394 BE_64(tmpsha256
.zc_word
[2]);
395 drrw
->drr_key
.ddk_cksum
.zc_word
[3] =
396 BE_64(tmpsha256
.zc_word
[3]);
397 drrw
->drr_checksumtype
= ZIO_CHECKSUM_SHA256
;
398 drrw
->drr_checksumflags
= DRR_CHECKSUM_DEDUP
;
401 dataref
.ref_guid
= drrw
->drr_toguid
;
402 dataref
.ref_object
= drrw
->drr_object
;
403 dataref
.ref_offset
= drrw
->drr_offset
;
405 if (ddt_update(dda
->dedup_hdl
, &ddt
,
406 &drrw
->drr_key
.ddk_cksum
, drrw
->drr_key
.ddk_prop
,
408 dmu_replay_record_t wbr_drr
= {0};
409 struct drr_write_byref
*wbr_drrr
=
410 &wbr_drr
.drr_u
.drr_write_byref
;
412 /* block already present in stream */
413 wbr_drr
.drr_type
= DRR_WRITE_BYREF
;
415 wbr_drrr
->drr_object
= drrw
->drr_object
;
416 wbr_drrr
->drr_offset
= drrw
->drr_offset
;
417 wbr_drrr
->drr_length
= drrw
->drr_logical_size
;
418 wbr_drrr
->drr_toguid
= drrw
->drr_toguid
;
419 wbr_drrr
->drr_refguid
= dataref
.ref_guid
;
420 wbr_drrr
->drr_refobject
=
422 wbr_drrr
->drr_refoffset
=
425 wbr_drrr
->drr_checksumtype
=
426 drrw
->drr_checksumtype
;
427 wbr_drrr
->drr_checksumflags
=
428 drrw
->drr_checksumtype
;
429 wbr_drrr
->drr_key
.ddk_cksum
=
430 drrw
->drr_key
.ddk_cksum
;
431 wbr_drrr
->drr_key
.ddk_prop
=
432 drrw
->drr_key
.ddk_prop
;
434 if (dump_record(&wbr_drr
, NULL
, 0,
435 &stream_cksum
, outfd
) != 0)
438 /* block not previously seen */
439 if (dump_record(drr
, buf
, payload_size
,
440 &stream_cksum
, outfd
) != 0)
446 case DRR_WRITE_EMBEDDED
:
448 struct drr_write_embedded
*drrwe
=
449 &drr
->drr_u
.drr_write_embedded
;
451 P2ROUNDUP((uint64_t)drrwe
->drr_psize
, 8), ofp
);
452 if (dump_record(drr
, buf
,
453 P2ROUNDUP((uint64_t)drrwe
->drr_psize
, 8),
454 &stream_cksum
, outfd
) != 0)
461 if (dump_record(drr
, NULL
, 0, &stream_cksum
,
468 (void) fprintf(stderr
, "INVALID record type 0x%x\n",
470 /* should never happen, so assert */
475 umem_cache_destroy(ddt
.ddecache
);
476 free(ddt
.dedup_hash_array
);
484 * Routines for dealing with the AVL tree of fs-nvlists
486 typedef struct fsavl_node
{
494 fsavl_compare(const void *arg1
, const void *arg2
)
496 const fsavl_node_t
*fn1
= (const fsavl_node_t
*)arg1
;
497 const fsavl_node_t
*fn2
= (const fsavl_node_t
*)arg2
;
499 return (AVL_CMP(fn1
->fn_guid
, fn2
->fn_guid
));
503 * Given the GUID of a snapshot, find its containing filesystem and
507 fsavl_find(avl_tree_t
*avl
, uint64_t snapguid
, char **snapname
)
509 fsavl_node_t fn_find
;
512 fn_find
.fn_guid
= snapguid
;
514 fn
= avl_find(avl
, &fn_find
, NULL
);
517 *snapname
= fn
->fn_snapname
;
518 return (fn
->fn_nvfs
);
524 fsavl_destroy(avl_tree_t
*avl
)
533 while ((fn
= avl_destroy_nodes(avl
, &cookie
)) != NULL
)
540 * Given an nvlist, produce an avl tree of snapshots, ordered by guid
543 fsavl_create(nvlist_t
*fss
)
546 nvpair_t
*fselem
= NULL
;
548 if ((fsavl
= malloc(sizeof (avl_tree_t
))) == NULL
)
551 avl_create(fsavl
, fsavl_compare
, sizeof (fsavl_node_t
),
552 offsetof(fsavl_node_t
, fn_node
));
554 while ((fselem
= nvlist_next_nvpair(fss
, fselem
)) != NULL
) {
555 nvlist_t
*nvfs
, *snaps
;
556 nvpair_t
*snapelem
= NULL
;
558 VERIFY(0 == nvpair_value_nvlist(fselem
, &nvfs
));
559 VERIFY(0 == nvlist_lookup_nvlist(nvfs
, "snaps", &snaps
));
562 nvlist_next_nvpair(snaps
, snapelem
)) != NULL
) {
566 VERIFY(0 == nvpair_value_uint64(snapelem
, &guid
));
567 if ((fn
= malloc(sizeof (fsavl_node_t
))) == NULL
) {
568 fsavl_destroy(fsavl
);
572 fn
->fn_snapname
= nvpair_name(snapelem
);
576 * Note: if there are multiple snaps with the
577 * same GUID, we ignore all but one.
579 if (avl_find(fsavl
, fn
, NULL
) == NULL
)
590 * Routines for dealing with the giant nvlist of fs-nvlists, etc.
592 typedef struct send_data
{
594 * assigned inside every recursive call,
595 * restored from *_save on return:
597 * guid of fromsnap snapshot in parent dataset
598 * txg of fromsnap snapshot in current dataset
599 * txg of tosnap snapshot in current dataset
602 uint64_t parent_fromsnap_guid
;
603 uint64_t fromsnap_txg
;
606 /* the nvlists get accumulated during depth-first traversal */
607 nvlist_t
*parent_snaps
;
611 /* send-receive configuration, does not change during traversal */
613 const char *fromsnap
;
621 * The header nvlist is of the following format:
624 * "fromsnap" -> string (if incremental)
628 * "name" -> string (full name; for debugging)
629 * "parentfromsnap" -> number (guid of fromsnap in parent)
631 * "props" -> { name -> value (only if set here) }
632 * "snaps" -> { name (lastname) -> number (guid) }
633 * "snapprops" -> { name (lastname) -> { name -> value } }
635 * "origin" -> number (guid) (if clone)
636 * "sent" -> boolean (not on-disk)
644 static void send_iterate_prop(zfs_handle_t
*zhp
, nvlist_t
*nv
);
647 send_iterate_snap(zfs_handle_t
*zhp
, void *arg
)
649 send_data_t
*sd
= arg
;
650 uint64_t guid
= zhp
->zfs_dmustats
.dds_guid
;
651 uint64_t txg
= zhp
->zfs_dmustats
.dds_creation_txg
;
654 boolean_t isfromsnap
, istosnap
, istosnapwithnofrom
;
656 snapname
= strrchr(zhp
->zfs_name
, '@')+1;
657 isfromsnap
= (sd
->fromsnap
!= NULL
&&
658 strcmp(sd
->fromsnap
, snapname
) == 0);
659 istosnap
= (sd
->tosnap
!= NULL
&& (strcmp(sd
->tosnap
, snapname
) == 0));
660 istosnapwithnofrom
= (istosnap
&& sd
->fromsnap
== NULL
);
662 if (sd
->tosnap_txg
!= 0 && txg
> sd
->tosnap_txg
) {
664 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
665 "skipping snapshot %s because it was created "
666 "after the destination snapshot (%s)\n"),
667 zhp
->zfs_name
, sd
->tosnap
);
673 VERIFY(0 == nvlist_add_uint64(sd
->parent_snaps
, snapname
, guid
));
675 * NB: if there is no fromsnap here (it's a newly created fs in
676 * an incremental replication), we will substitute the tosnap.
678 if (isfromsnap
|| (sd
->parent_fromsnap_guid
== 0 && istosnap
)) {
679 sd
->parent_fromsnap_guid
= guid
;
682 if (!sd
->recursive
) {
683 if (!sd
->seenfrom
&& isfromsnap
) {
684 sd
->seenfrom
= B_TRUE
;
689 if ((sd
->seento
|| !sd
->seenfrom
) && !istosnapwithnofrom
) {
698 VERIFY(0 == nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0));
699 send_iterate_prop(zhp
, nv
);
700 VERIFY(0 == nvlist_add_nvlist(sd
->snapprops
, snapname
, nv
));
708 send_iterate_prop(zfs_handle_t
*zhp
, nvlist_t
*nv
)
710 nvpair_t
*elem
= NULL
;
712 while ((elem
= nvlist_next_nvpair(zhp
->zfs_props
, elem
)) != NULL
) {
713 char *propname
= nvpair_name(elem
);
714 zfs_prop_t prop
= zfs_name_to_prop(propname
);
717 if (!zfs_prop_user(propname
)) {
719 * Realistically, this should never happen. However,
720 * we want the ability to add DSL properties without
721 * needing to make incompatible version changes. We
722 * need to ignore unknown properties to allow older
723 * software to still send datasets containing these
724 * properties, with the unknown properties elided.
726 if (prop
== ZPROP_INVAL
)
729 if (zfs_prop_readonly(prop
))
733 verify(nvpair_value_nvlist(elem
, &propnv
) == 0);
734 if (prop
== ZFS_PROP_QUOTA
|| prop
== ZFS_PROP_RESERVATION
||
735 prop
== ZFS_PROP_REFQUOTA
||
736 prop
== ZFS_PROP_REFRESERVATION
) {
739 verify(nvlist_lookup_uint64(propnv
,
740 ZPROP_VALUE
, &value
) == 0);
741 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
)
744 * May have no source before SPA_VERSION_RECVD_PROPS,
745 * but is still modifiable.
747 if (nvlist_lookup_string(propnv
,
748 ZPROP_SOURCE
, &source
) == 0) {
749 if ((strcmp(source
, zhp
->zfs_name
) != 0) &&
751 ZPROP_SOURCE_VAL_RECVD
) != 0))
756 if (nvlist_lookup_string(propnv
,
757 ZPROP_SOURCE
, &source
) != 0)
759 if ((strcmp(source
, zhp
->zfs_name
) != 0) &&
760 (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) != 0))
764 if (zfs_prop_user(propname
) ||
765 zfs_prop_get_type(prop
) == PROP_TYPE_STRING
) {
767 verify(nvlist_lookup_string(propnv
,
768 ZPROP_VALUE
, &value
) == 0);
769 VERIFY(0 == nvlist_add_string(nv
, propname
, value
));
772 verify(nvlist_lookup_uint64(propnv
,
773 ZPROP_VALUE
, &value
) == 0);
774 VERIFY(0 == nvlist_add_uint64(nv
, propname
, value
));
780 * returns snapshot creation txg
781 * and returns 0 if the snapshot does not exist
784 get_snap_txg(libzfs_handle_t
*hdl
, const char *fs
, const char *snap
)
786 char name
[ZFS_MAX_DATASET_NAME_LEN
];
789 if (fs
== NULL
|| fs
[0] == '\0' || snap
== NULL
|| snap
[0] == '\0')
792 (void) snprintf(name
, sizeof (name
), "%s@%s", fs
, snap
);
793 if (zfs_dataset_exists(hdl
, name
, ZFS_TYPE_SNAPSHOT
)) {
794 zfs_handle_t
*zhp
= zfs_open(hdl
, name
, ZFS_TYPE_SNAPSHOT
);
796 txg
= zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
);
805 * recursively generate nvlists describing datasets. See comment
806 * for the data structure send_data_t above for description of contents
810 send_iterate_fs(zfs_handle_t
*zhp
, void *arg
)
812 send_data_t
*sd
= arg
;
815 uint64_t parent_fromsnap_guid_save
= sd
->parent_fromsnap_guid
;
816 uint64_t fromsnap_txg_save
= sd
->fromsnap_txg
;
817 uint64_t tosnap_txg_save
= sd
->tosnap_txg
;
818 uint64_t txg
= zhp
->zfs_dmustats
.dds_creation_txg
;
819 uint64_t guid
= zhp
->zfs_dmustats
.dds_guid
;
820 uint64_t fromsnap_txg
, tosnap_txg
;
823 fromsnap_txg
= get_snap_txg(zhp
->zfs_hdl
, zhp
->zfs_name
, sd
->fromsnap
);
824 if (fromsnap_txg
!= 0)
825 sd
->fromsnap_txg
= fromsnap_txg
;
827 tosnap_txg
= get_snap_txg(zhp
->zfs_hdl
, zhp
->zfs_name
, sd
->tosnap
);
829 sd
->tosnap_txg
= tosnap_txg
;
832 * on the send side, if the current dataset does not have tosnap,
833 * perform two additional checks:
835 * - skip sending the current dataset if it was created later than
837 * - return error if the current dataset was created earlier than
840 if (sd
->tosnap
!= NULL
&& tosnap_txg
== 0) {
841 if (sd
->tosnap_txg
!= 0 && txg
> sd
->tosnap_txg
) {
843 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
844 "skipping dataset %s: snapshot %s does "
845 "not exist\n"), zhp
->zfs_name
, sd
->tosnap
);
848 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
849 "cannot send %s@%s%s: snapshot %s@%s does not "
850 "exist\n"), sd
->fsname
, sd
->tosnap
, sd
->recursive
?
851 dgettext(TEXT_DOMAIN
, " recursively") : "",
852 zhp
->zfs_name
, sd
->tosnap
);
858 VERIFY(0 == nvlist_alloc(&nvfs
, NV_UNIQUE_NAME
, 0));
859 VERIFY(0 == nvlist_add_string(nvfs
, "name", zhp
->zfs_name
));
860 VERIFY(0 == nvlist_add_uint64(nvfs
, "parentfromsnap",
861 sd
->parent_fromsnap_guid
));
863 if (zhp
->zfs_dmustats
.dds_origin
[0]) {
864 zfs_handle_t
*origin
= zfs_open(zhp
->zfs_hdl
,
865 zhp
->zfs_dmustats
.dds_origin
, ZFS_TYPE_SNAPSHOT
);
866 if (origin
== NULL
) {
870 VERIFY(0 == nvlist_add_uint64(nvfs
, "origin",
871 origin
->zfs_dmustats
.dds_guid
));
874 /* iterate over props */
875 VERIFY(0 == nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0));
876 send_iterate_prop(zhp
, nv
);
877 VERIFY(0 == nvlist_add_nvlist(nvfs
, "props", nv
));
880 /* iterate over snaps, and set sd->parent_fromsnap_guid */
881 sd
->parent_fromsnap_guid
= 0;
882 VERIFY(0 == nvlist_alloc(&sd
->parent_snaps
, NV_UNIQUE_NAME
, 0));
883 VERIFY(0 == nvlist_alloc(&sd
->snapprops
, NV_UNIQUE_NAME
, 0));
884 (void) zfs_iter_snapshots_sorted(zhp
, send_iterate_snap
, sd
);
885 VERIFY(0 == nvlist_add_nvlist(nvfs
, "snaps", sd
->parent_snaps
));
886 VERIFY(0 == nvlist_add_nvlist(nvfs
, "snapprops", sd
->snapprops
));
887 nvlist_free(sd
->parent_snaps
);
888 nvlist_free(sd
->snapprops
);
890 /* add this fs to nvlist */
891 (void) snprintf(guidstring
, sizeof (guidstring
),
892 "0x%llx", (longlong_t
)guid
);
893 VERIFY(0 == nvlist_add_nvlist(sd
->fss
, guidstring
, nvfs
));
896 /* iterate over children */
898 rv
= zfs_iter_filesystems(zhp
, send_iterate_fs
, sd
);
901 sd
->parent_fromsnap_guid
= parent_fromsnap_guid_save
;
902 sd
->fromsnap_txg
= fromsnap_txg_save
;
903 sd
->tosnap_txg
= tosnap_txg_save
;
910 gather_nvlist(libzfs_handle_t
*hdl
, const char *fsname
, const char *fromsnap
,
911 const char *tosnap
, boolean_t recursive
, boolean_t verbose
,
912 nvlist_t
**nvlp
, avl_tree_t
**avlp
)
915 send_data_t sd
= { 0 };
918 zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
920 return (EZFS_BADTYPE
);
922 VERIFY(0 == nvlist_alloc(&sd
.fss
, NV_UNIQUE_NAME
, 0));
924 sd
.fromsnap
= fromsnap
;
926 sd
.recursive
= recursive
;
927 sd
.verbose
= verbose
;
929 if ((error
= send_iterate_fs(zhp
, &sd
)) != 0) {
937 if (avlp
!= NULL
&& (*avlp
= fsavl_create(sd
.fss
)) == NULL
) {
948 * Routines specific to "zfs send"
950 typedef struct send_dump_data
{
951 /* these are all just the short snapname (the part after the @) */
952 const char *fromsnap
;
954 char prevsnap
[ZFS_MAX_DATASET_NAME_LEN
];
955 uint64_t prevsnap_obj
;
956 boolean_t seenfrom
, seento
, replicate
, doall
, fromorigin
;
957 boolean_t verbose
, dryrun
, parsable
, progress
, embed_data
, std_out
;
958 boolean_t large_block
, compress
;
964 snapfilter_cb_t
*filter_cb
;
967 char holdtag
[ZFS_MAX_DATASET_NAME_LEN
];
973 estimate_ioctl(zfs_handle_t
*zhp
, uint64_t fromsnap_obj
,
974 boolean_t fromorigin
, enum lzc_send_flags flags
, uint64_t *sizep
)
976 zfs_cmd_t zc
= {"\0"};
977 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
979 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
980 assert(fromsnap_obj
== 0 || !fromorigin
);
982 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
983 zc
.zc_obj
= fromorigin
;
984 zc
.zc_sendobj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
985 zc
.zc_fromobj
= fromsnap_obj
;
986 zc
.zc_guid
= 1; /* estimate flag */
989 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SEND
, &zc
) != 0) {
991 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
992 "warning: cannot estimate space for '%s'"), zhp
->zfs_name
);
996 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
997 "not an earlier snapshot from the same fs"));
998 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
1001 if (zfs_dataset_exists(hdl
, zc
.zc_name
,
1002 ZFS_TYPE_SNAPSHOT
)) {
1003 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1004 "incremental source (@%s) does not exist"),
1007 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
1020 zfs_error_aux(hdl
, strerror(errno
));
1021 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
1024 return (zfs_standard_error(hdl
, errno
, errbuf
));
1028 *sizep
= zc
.zc_objset_type
;
1034 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
1035 * NULL) to the file descriptor specified by outfd.
1038 dump_ioctl(zfs_handle_t
*zhp
, const char *fromsnap
, uint64_t fromsnap_obj
,
1039 boolean_t fromorigin
, int outfd
, enum lzc_send_flags flags
,
1042 zfs_cmd_t zc
= {"\0"};
1043 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1046 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
1047 assert(fromsnap_obj
== 0 || !fromorigin
);
1049 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1050 zc
.zc_cookie
= outfd
;
1051 zc
.zc_obj
= fromorigin
;
1052 zc
.zc_sendobj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
1053 zc
.zc_fromobj
= fromsnap_obj
;
1054 zc
.zc_flags
= flags
;
1056 VERIFY(0 == nvlist_alloc(&thisdbg
, NV_UNIQUE_NAME
, 0));
1057 if (fromsnap
&& fromsnap
[0] != '\0') {
1058 VERIFY(0 == nvlist_add_string(thisdbg
,
1059 "fromsnap", fromsnap
));
1062 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SEND
, &zc
) != 0) {
1064 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1065 "warning: cannot send '%s'"), zhp
->zfs_name
);
1067 VERIFY(0 == nvlist_add_uint64(thisdbg
, "error", errno
));
1069 VERIFY(0 == nvlist_add_nvlist(debugnv
,
1070 zhp
->zfs_name
, thisdbg
));
1072 nvlist_free(thisdbg
);
1076 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1077 "not an earlier snapshot from the same fs"));
1078 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
1081 if (zfs_dataset_exists(hdl
, zc
.zc_name
,
1082 ZFS_TYPE_SNAPSHOT
)) {
1083 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1084 "incremental source (@%s) does not exist"),
1087 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
1100 zfs_error_aux(hdl
, strerror(errno
));
1101 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
1104 return (zfs_standard_error(hdl
, errno
, errbuf
));
1109 VERIFY(0 == nvlist_add_nvlist(debugnv
, zhp
->zfs_name
, thisdbg
));
1110 nvlist_free(thisdbg
);
1116 gather_holds(zfs_handle_t
*zhp
, send_dump_data_t
*sdd
)
1118 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
1121 * zfs_send() only sets snapholds for sends that need them,
1122 * e.g. replication and doall.
1124 if (sdd
->snapholds
== NULL
)
1127 fnvlist_add_string(sdd
->snapholds
, zhp
->zfs_name
, sdd
->holdtag
);
1131 send_progress_thread(void *arg
)
1133 progress_arg_t
*pa
= arg
;
1134 zfs_cmd_t zc
= {"\0"};
1135 zfs_handle_t
*zhp
= pa
->pa_zhp
;
1136 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1137 unsigned long long bytes
;
1142 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1144 if (!pa
->pa_parsable
)
1145 (void) fprintf(stderr
, "TIME SENT SNAPSHOT\n");
1148 * Print the progress from ZFS_IOC_SEND_PROGRESS every second.
1153 zc
.zc_cookie
= pa
->pa_fd
;
1154 if (zfs_ioctl(hdl
, ZFS_IOC_SEND_PROGRESS
, &zc
) != 0)
1155 return ((void *)-1);
1159 bytes
= zc
.zc_cookie
;
1161 if (pa
->pa_parsable
) {
1162 (void) fprintf(stderr
, "%02d:%02d:%02d\t%llu\t%s\n",
1163 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
1164 bytes
, zhp
->zfs_name
);
1166 zfs_nicenum(bytes
, buf
, sizeof (buf
));
1167 (void) fprintf(stderr
, "%02d:%02d:%02d %5s %s\n",
1168 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
1169 buf
, zhp
->zfs_name
);
1175 send_print_verbose(FILE *fout
, const char *tosnap
, const char *fromsnap
,
1176 uint64_t size
, boolean_t parsable
)
1179 if (fromsnap
!= NULL
) {
1180 (void) fprintf(fout
, "incremental\t%s\t%s",
1183 (void) fprintf(fout
, "full\t%s",
1187 if (fromsnap
!= NULL
) {
1188 if (strchr(fromsnap
, '@') == NULL
&&
1189 strchr(fromsnap
, '#') == NULL
) {
1190 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1191 "send from @%s to %s"),
1194 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1195 "send from %s to %s"),
1199 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1207 (void) fprintf(fout
, "\t%llu",
1211 zfs_nicenum(size
, buf
, sizeof (buf
));
1212 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1213 " estimated size is %s"), buf
);
1216 (void) fprintf(fout
, "\n");
1220 dump_snapshot(zfs_handle_t
*zhp
, void *arg
)
1222 send_dump_data_t
*sdd
= arg
;
1223 progress_arg_t pa
= { 0 };
1226 enum lzc_send_flags flags
= 0;
1228 boolean_t isfromsnap
, istosnap
, fromorigin
;
1229 boolean_t exclude
= B_FALSE
;
1230 FILE *fout
= sdd
->std_out
? stdout
: stderr
;
1233 thissnap
= strchr(zhp
->zfs_name
, '@') + 1;
1234 isfromsnap
= (sdd
->fromsnap
!= NULL
&&
1235 strcmp(sdd
->fromsnap
, thissnap
) == 0);
1237 if (!sdd
->seenfrom
&& isfromsnap
) {
1238 gather_holds(zhp
, sdd
);
1239 sdd
->seenfrom
= B_TRUE
;
1240 (void) strlcpy(sdd
->prevsnap
, thissnap
,
1241 sizeof (sdd
->prevsnap
));
1242 sdd
->prevsnap_obj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
1247 if (sdd
->seento
|| !sdd
->seenfrom
) {
1252 istosnap
= (strcmp(sdd
->tosnap
, thissnap
) == 0);
1254 sdd
->seento
= B_TRUE
;
1256 if (sdd
->large_block
)
1257 flags
|= LZC_SEND_FLAG_LARGE_BLOCK
;
1258 if (sdd
->embed_data
)
1259 flags
|= LZC_SEND_FLAG_EMBED_DATA
;
1261 flags
|= LZC_SEND_FLAG_COMPRESS
;
1263 if (!sdd
->doall
&& !isfromsnap
&& !istosnap
) {
1264 if (sdd
->replicate
) {
1266 nvlist_t
*snapprops
;
1268 * Filter out all intermediate snapshots except origin
1269 * snapshots needed to replicate clones.
1271 nvlist_t
*nvfs
= fsavl_find(sdd
->fsavl
,
1272 zhp
->zfs_dmustats
.dds_guid
, &snapname
);
1274 VERIFY(0 == nvlist_lookup_nvlist(nvfs
,
1275 "snapprops", &snapprops
));
1276 VERIFY(0 == nvlist_lookup_nvlist(snapprops
,
1277 thissnap
, &snapprops
));
1278 exclude
= !nvlist_exists(snapprops
, "is_clone_origin");
1285 * If a filter function exists, call it to determine whether
1286 * this snapshot will be sent.
1288 if (exclude
|| (sdd
->filter_cb
!= NULL
&&
1289 sdd
->filter_cb(zhp
, sdd
->filter_cb_arg
) == B_FALSE
)) {
1291 * This snapshot is filtered out. Don't send it, and don't
1292 * set prevsnap_obj, so it will be as if this snapshot didn't
1293 * exist, and the next accepted snapshot will be sent as
1294 * an incremental from the last accepted one, or as the
1295 * first (and full) snapshot in the case of a replication,
1296 * non-incremental send.
1302 gather_holds(zhp
, sdd
);
1303 fromorigin
= sdd
->prevsnap
[0] == '\0' &&
1304 (sdd
->fromorigin
|| sdd
->replicate
);
1308 (void) estimate_ioctl(zhp
, sdd
->prevsnap_obj
,
1309 fromorigin
, flags
, &size
);
1311 send_print_verbose(fout
, zhp
->zfs_name
,
1312 sdd
->prevsnap
[0] ? sdd
->prevsnap
: NULL
,
1313 size
, sdd
->parsable
);
1319 * If progress reporting is requested, spawn a new thread to
1320 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1322 if (sdd
->progress
) {
1324 pa
.pa_fd
= sdd
->outfd
;
1325 pa
.pa_parsable
= sdd
->parsable
;
1327 if ((err
= pthread_create(&tid
, NULL
,
1328 send_progress_thread
, &pa
))) {
1334 err
= dump_ioctl(zhp
, sdd
->prevsnap
, sdd
->prevsnap_obj
,
1335 fromorigin
, sdd
->outfd
, flags
, sdd
->debugnv
);
1337 if (sdd
->progress
) {
1338 (void) pthread_cancel(tid
);
1339 (void) pthread_join(tid
, NULL
);
1343 (void) strcpy(sdd
->prevsnap
, thissnap
);
1344 sdd
->prevsnap_obj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
1350 dump_filesystem(zfs_handle_t
*zhp
, void *arg
)
1353 send_dump_data_t
*sdd
= arg
;
1354 boolean_t missingfrom
= B_FALSE
;
1355 zfs_cmd_t zc
= {"\0"};
1357 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
), "%s@%s",
1358 zhp
->zfs_name
, sdd
->tosnap
);
1359 if (ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0) {
1360 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1361 "WARNING: could not send %s@%s: does not exist\n"),
1362 zhp
->zfs_name
, sdd
->tosnap
);
1367 if (sdd
->replicate
&& sdd
->fromsnap
) {
1369 * If this fs does not have fromsnap, and we're doing
1370 * recursive, we need to send a full stream from the
1371 * beginning (or an incremental from the origin if this
1372 * is a clone). If we're doing non-recursive, then let
1373 * them get the error.
1375 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
), "%s@%s",
1376 zhp
->zfs_name
, sdd
->fromsnap
);
1377 if (ioctl(zhp
->zfs_hdl
->libzfs_fd
,
1378 ZFS_IOC_OBJSET_STATS
, &zc
) != 0) {
1379 missingfrom
= B_TRUE
;
1383 sdd
->seenfrom
= sdd
->seento
= sdd
->prevsnap
[0] = 0;
1384 sdd
->prevsnap_obj
= 0;
1385 if (sdd
->fromsnap
== NULL
|| missingfrom
)
1386 sdd
->seenfrom
= B_TRUE
;
1388 rv
= zfs_iter_snapshots_sorted(zhp
, dump_snapshot
, arg
);
1389 if (!sdd
->seenfrom
) {
1390 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1391 "WARNING: could not send %s@%s:\n"
1392 "incremental source (%s@%s) does not exist\n"),
1393 zhp
->zfs_name
, sdd
->tosnap
,
1394 zhp
->zfs_name
, sdd
->fromsnap
);
1396 } else if (!sdd
->seento
) {
1397 if (sdd
->fromsnap
) {
1398 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1399 "WARNING: could not send %s@%s:\n"
1400 "incremental source (%s@%s) "
1401 "is not earlier than it\n"),
1402 zhp
->zfs_name
, sdd
->tosnap
,
1403 zhp
->zfs_name
, sdd
->fromsnap
);
1405 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1407 "could not send %s@%s: does not exist\n"),
1408 zhp
->zfs_name
, sdd
->tosnap
);
1417 dump_filesystems(zfs_handle_t
*rzhp
, void *arg
)
1419 send_dump_data_t
*sdd
= arg
;
1421 boolean_t needagain
, progress
;
1423 if (!sdd
->replicate
)
1424 return (dump_filesystem(rzhp
, sdd
));
1426 /* Mark the clone origin snapshots. */
1427 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1428 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1430 uint64_t origin_guid
= 0;
1432 VERIFY(0 == nvpair_value_nvlist(fspair
, &nvfs
));
1433 (void) nvlist_lookup_uint64(nvfs
, "origin", &origin_guid
);
1434 if (origin_guid
!= 0) {
1436 nvlist_t
*origin_nv
= fsavl_find(sdd
->fsavl
,
1437 origin_guid
, &snapname
);
1438 if (origin_nv
!= NULL
) {
1439 nvlist_t
*snapprops
;
1440 VERIFY(0 == nvlist_lookup_nvlist(origin_nv
,
1441 "snapprops", &snapprops
));
1442 VERIFY(0 == nvlist_lookup_nvlist(snapprops
,
1443 snapname
, &snapprops
));
1444 VERIFY(0 == nvlist_add_boolean(
1445 snapprops
, "is_clone_origin"));
1450 needagain
= progress
= B_FALSE
;
1451 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1452 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1453 nvlist_t
*fslist
, *parent_nv
;
1457 uint64_t origin_guid
= 0;
1458 uint64_t parent_guid
= 0;
1460 VERIFY(nvpair_value_nvlist(fspair
, &fslist
) == 0);
1461 if (nvlist_lookup_boolean(fslist
, "sent") == 0)
1464 VERIFY(nvlist_lookup_string(fslist
, "name", &fsname
) == 0);
1465 (void) nvlist_lookup_uint64(fslist
, "origin", &origin_guid
);
1466 (void) nvlist_lookup_uint64(fslist
, "parentfromsnap",
1469 if (parent_guid
!= 0) {
1470 parent_nv
= fsavl_find(sdd
->fsavl
, parent_guid
, NULL
);
1471 if (!nvlist_exists(parent_nv
, "sent")) {
1472 /* parent has not been sent; skip this one */
1478 if (origin_guid
!= 0) {
1479 nvlist_t
*origin_nv
= fsavl_find(sdd
->fsavl
,
1481 if (origin_nv
!= NULL
&&
1482 !nvlist_exists(origin_nv
, "sent")) {
1484 * origin has not been sent yet;
1492 zhp
= zfs_open(rzhp
->zfs_hdl
, fsname
, ZFS_TYPE_DATASET
);
1495 err
= dump_filesystem(zhp
, sdd
);
1496 VERIFY(nvlist_add_boolean(fslist
, "sent") == 0);
1507 /* clean out the sent flags in case we reuse this fss */
1508 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1509 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1512 VERIFY(nvpair_value_nvlist(fspair
, &fslist
) == 0);
1513 (void) nvlist_remove_all(fslist
, "sent");
1520 zfs_send_resume_token_to_nvlist(libzfs_handle_t
*hdl
, const char *token
)
1522 unsigned int version
;
1524 unsigned long long checksum
, packed_len
;
1527 * Decode token header, which is:
1528 * <token version>-<checksum of payload>-<uncompressed payload length>
1529 * Note that the only supported token version is 1.
1531 nread
= sscanf(token
, "%u-%llx-%llx-",
1532 &version
, &checksum
, &packed_len
);
1534 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1535 "resume token is corrupt (invalid format)"));
1539 if (version
!= ZFS_SEND_RESUME_TOKEN_VERSION
) {
1540 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1541 "resume token is corrupt (invalid version %u)"),
1546 /* convert hexadecimal representation to binary */
1547 token
= strrchr(token
, '-') + 1;
1548 int len
= strlen(token
) / 2;
1549 unsigned char *compressed
= zfs_alloc(hdl
, len
);
1550 for (i
= 0; i
< len
; i
++) {
1551 nread
= sscanf(token
+ i
* 2, "%2hhx", compressed
+ i
);
1554 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1555 "resume token is corrupt "
1556 "(payload is not hex-encoded)"));
1561 /* verify checksum */
1563 fletcher_4_native_varsize(compressed
, len
, &cksum
);
1564 if (cksum
.zc_word
[0] != checksum
) {
1566 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1567 "resume token is corrupt (incorrect checksum)"));
1572 void *packed
= zfs_alloc(hdl
, packed_len
);
1573 uLongf packed_len_long
= packed_len
;
1574 if (uncompress(packed
, &packed_len_long
, compressed
, len
) != Z_OK
||
1575 packed_len_long
!= packed_len
) {
1578 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1579 "resume token is corrupt (decompression failed)"));
1585 int error
= nvlist_unpack(packed
, packed_len
, &nv
, KM_SLEEP
);
1589 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1590 "resume token is corrupt (nvlist_unpack failed)"));
1597 zfs_send_resume(libzfs_handle_t
*hdl
, sendflags_t
*flags
, int outfd
,
1598 const char *resume_token
)
1602 char *fromname
= NULL
;
1603 uint64_t resumeobj
, resumeoff
, toguid
, fromguid
, bytes
;
1606 char name
[ZFS_MAX_DATASET_NAME_LEN
];
1607 enum lzc_send_flags lzc_flags
= 0;
1609 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1610 "cannot resume send"));
1612 nvlist_t
*resume_nvl
=
1613 zfs_send_resume_token_to_nvlist(hdl
, resume_token
);
1614 if (resume_nvl
== NULL
) {
1616 * zfs_error_aux has already been set by
1617 * zfs_send_resume_token_to_nvlist
1619 return (zfs_error(hdl
, EZFS_FAULT
, errbuf
));
1621 if (flags
->verbose
) {
1622 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1623 "resume token contents:\n"));
1624 nvlist_print(stderr
, resume_nvl
);
1627 if (nvlist_lookup_string(resume_nvl
, "toname", &toname
) != 0 ||
1628 nvlist_lookup_uint64(resume_nvl
, "object", &resumeobj
) != 0 ||
1629 nvlist_lookup_uint64(resume_nvl
, "offset", &resumeoff
) != 0 ||
1630 nvlist_lookup_uint64(resume_nvl
, "bytes", &bytes
) != 0 ||
1631 nvlist_lookup_uint64(resume_nvl
, "toguid", &toguid
) != 0) {
1632 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1633 "resume token is corrupt"));
1634 return (zfs_error(hdl
, EZFS_FAULT
, errbuf
));
1637 (void) nvlist_lookup_uint64(resume_nvl
, "fromguid", &fromguid
);
1639 if (flags
->largeblock
|| nvlist_exists(resume_nvl
, "largeblockok"))
1640 lzc_flags
|= LZC_SEND_FLAG_LARGE_BLOCK
;
1641 if (flags
->embed_data
|| nvlist_exists(resume_nvl
, "embedok"))
1642 lzc_flags
|= LZC_SEND_FLAG_EMBED_DATA
;
1643 if (flags
->compress
|| nvlist_exists(resume_nvl
, "compressok"))
1644 lzc_flags
|= LZC_SEND_FLAG_COMPRESS
;
1646 if (guid_to_name(hdl
, toname
, toguid
, B_FALSE
, name
) != 0) {
1647 if (zfs_dataset_exists(hdl
, toname
, ZFS_TYPE_DATASET
)) {
1648 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1649 "'%s' is no longer the same snapshot used in "
1650 "the initial send"), toname
);
1652 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1653 "'%s' used in the initial send no longer exists"),
1656 return (zfs_error(hdl
, EZFS_BADPATH
, errbuf
));
1658 zhp
= zfs_open(hdl
, name
, ZFS_TYPE_DATASET
);
1660 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1661 "unable to access '%s'"), name
);
1662 return (zfs_error(hdl
, EZFS_BADPATH
, errbuf
));
1665 if (fromguid
!= 0) {
1666 if (guid_to_name(hdl
, toname
, fromguid
, B_TRUE
, name
) != 0) {
1667 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1668 "incremental source %#llx no longer exists"),
1669 (longlong_t
)fromguid
);
1670 return (zfs_error(hdl
, EZFS_BADPATH
, errbuf
));
1675 if (flags
->verbose
) {
1677 error
= lzc_send_space(zhp
->zfs_name
, fromname
,
1680 size
= MAX(0, (int64_t)(size
- bytes
));
1681 send_print_verbose(stderr
, zhp
->zfs_name
, fromname
,
1682 size
, flags
->parsable
);
1685 if (!flags
->dryrun
) {
1686 progress_arg_t pa
= { 0 };
1689 * If progress reporting is requested, spawn a new thread to
1690 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1692 if (flags
->progress
) {
1695 pa
.pa_parsable
= flags
->parsable
;
1697 error
= pthread_create(&tid
, NULL
,
1698 send_progress_thread
, &pa
);
1705 error
= lzc_send_resume(zhp
->zfs_name
, fromname
, outfd
,
1706 lzc_flags
, resumeobj
, resumeoff
);
1708 if (flags
->progress
) {
1709 (void) pthread_cancel(tid
);
1710 (void) pthread_join(tid
, NULL
);
1714 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1715 "warning: cannot send '%s'"), zhp
->zfs_name
);
1735 zfs_error_aux(hdl
, strerror(errno
));
1736 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
1739 return (zfs_standard_error(hdl
, errno
, errbuf
));
1750 * Generate a send stream for the dataset identified by the argument zhp.
1752 * The content of the send stream is the snapshot identified by
1753 * 'tosnap'. Incremental streams are requested in two ways:
1754 * - from the snapshot identified by "fromsnap" (if non-null) or
1755 * - from the origin of the dataset identified by zhp, which must
1756 * be a clone. In this case, "fromsnap" is null and "fromorigin"
1759 * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
1760 * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
1761 * if "replicate" is set. If "doall" is set, dump all the intermediate
1762 * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
1763 * case too. If "props" is set, send properties.
1766 zfs_send(zfs_handle_t
*zhp
, const char *fromsnap
, const char *tosnap
,
1767 sendflags_t
*flags
, int outfd
, snapfilter_cb_t filter_func
,
1768 void *cb_arg
, nvlist_t
**debugnvp
)
1771 send_dump_data_t sdd
= { 0 };
1773 nvlist_t
*fss
= NULL
;
1774 avl_tree_t
*fsavl
= NULL
;
1775 static uint64_t holdseq
;
1779 dedup_arg_t dda
= { 0 };
1780 int featureflags
= 0;
1783 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1784 "cannot send '%s'"), zhp
->zfs_name
);
1786 if (fromsnap
&& fromsnap
[0] == '\0') {
1787 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1788 "zero-length incremental source"));
1789 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
1792 if (zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
) {
1794 version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1795 if (version
>= ZPL_VERSION_SA
) {
1796 featureflags
|= DMU_BACKUP_FEATURE_SA_SPILL
;
1800 if (flags
->dedup
&& !flags
->dryrun
) {
1801 featureflags
|= (DMU_BACKUP_FEATURE_DEDUP
|
1802 DMU_BACKUP_FEATURE_DEDUPPROPS
);
1803 if ((err
= socketpair(AF_UNIX
, SOCK_STREAM
, 0, pipefd
))) {
1804 zfs_error_aux(zhp
->zfs_hdl
, strerror(errno
));
1805 return (zfs_error(zhp
->zfs_hdl
, EZFS_PIPEFAILED
,
1808 dda
.outputfd
= outfd
;
1809 dda
.inputfd
= pipefd
[1];
1810 dda
.dedup_hdl
= zhp
->zfs_hdl
;
1811 if ((err
= pthread_create(&tid
, NULL
, cksummer
, &dda
))) {
1812 (void) close(pipefd
[0]);
1813 (void) close(pipefd
[1]);
1814 zfs_error_aux(zhp
->zfs_hdl
, strerror(errno
));
1815 return (zfs_error(zhp
->zfs_hdl
,
1816 EZFS_THREADCREATEFAILED
, errbuf
));
1820 if (flags
->replicate
|| flags
->doall
|| flags
->props
) {
1821 dmu_replay_record_t drr
= { 0 };
1822 char *packbuf
= NULL
;
1826 ZIO_SET_CHECKSUM(&zc
, 0, 0, 0, 0);
1828 if (flags
->replicate
|| flags
->props
) {
1831 VERIFY(0 == nvlist_alloc(&hdrnv
, NV_UNIQUE_NAME
, 0));
1833 VERIFY(0 == nvlist_add_string(hdrnv
,
1834 "fromsnap", fromsnap
));
1836 VERIFY(0 == nvlist_add_string(hdrnv
, "tosnap", tosnap
));
1837 if (!flags
->replicate
) {
1838 VERIFY(0 == nvlist_add_boolean(hdrnv
,
1842 err
= gather_nvlist(zhp
->zfs_hdl
, zhp
->zfs_name
,
1843 fromsnap
, tosnap
, flags
->replicate
, flags
->verbose
,
1847 VERIFY(0 == nvlist_add_nvlist(hdrnv
, "fss", fss
));
1848 err
= nvlist_pack(hdrnv
, &packbuf
, &buflen
,
1858 if (!flags
->dryrun
) {
1859 /* write first begin record */
1860 drr
.drr_type
= DRR_BEGIN
;
1861 drr
.drr_u
.drr_begin
.drr_magic
= DMU_BACKUP_MAGIC
;
1862 DMU_SET_STREAM_HDRTYPE(drr
.drr_u
.drr_begin
.
1863 drr_versioninfo
, DMU_COMPOUNDSTREAM
);
1864 DMU_SET_FEATUREFLAGS(drr
.drr_u
.drr_begin
.
1865 drr_versioninfo
, featureflags
);
1866 (void) snprintf(drr
.drr_u
.drr_begin
.drr_toname
,
1867 sizeof (drr
.drr_u
.drr_begin
.drr_toname
),
1868 "%s@%s", zhp
->zfs_name
, tosnap
);
1869 drr
.drr_payloadlen
= buflen
;
1871 err
= dump_record(&drr
, packbuf
, buflen
, &zc
, outfd
);
1876 /* write end record */
1877 bzero(&drr
, sizeof (drr
));
1878 drr
.drr_type
= DRR_END
;
1879 drr
.drr_u
.drr_end
.drr_checksum
= zc
;
1880 err
= write(outfd
, &drr
, sizeof (drr
));
1890 /* dump each stream */
1891 sdd
.fromsnap
= fromsnap
;
1892 sdd
.tosnap
= tosnap
;
1894 sdd
.outfd
= pipefd
[0];
1897 sdd
.replicate
= flags
->replicate
;
1898 sdd
.doall
= flags
->doall
;
1899 sdd
.fromorigin
= flags
->fromorigin
;
1902 sdd
.verbose
= flags
->verbose
;
1903 sdd
.parsable
= flags
->parsable
;
1904 sdd
.progress
= flags
->progress
;
1905 sdd
.dryrun
= flags
->dryrun
;
1906 sdd
.large_block
= flags
->largeblock
;
1907 sdd
.embed_data
= flags
->embed_data
;
1908 sdd
.compress
= flags
->compress
;
1909 sdd
.filter_cb
= filter_func
;
1910 sdd
.filter_cb_arg
= cb_arg
;
1912 sdd
.debugnv
= *debugnvp
;
1913 if (sdd
.verbose
&& sdd
.dryrun
)
1914 sdd
.std_out
= B_TRUE
;
1915 fout
= sdd
.std_out
? stdout
: stderr
;
1918 * Some flags require that we place user holds on the datasets that are
1919 * being sent so they don't get destroyed during the send. We can skip
1920 * this step if the pool is imported read-only since the datasets cannot
1923 if (!flags
->dryrun
&& !zpool_get_prop_int(zfs_get_pool_handle(zhp
),
1924 ZPOOL_PROP_READONLY
, NULL
) &&
1925 zfs_spa_version(zhp
, &spa_version
) == 0 &&
1926 spa_version
>= SPA_VERSION_USERREFS
&&
1927 (flags
->doall
|| flags
->replicate
)) {
1929 (void) snprintf(sdd
.holdtag
, sizeof (sdd
.holdtag
),
1930 ".send-%d-%llu", getpid(), (u_longlong_t
)holdseq
);
1931 sdd
.cleanup_fd
= open(ZFS_DEV
, O_RDWR
);
1932 if (sdd
.cleanup_fd
< 0) {
1936 sdd
.snapholds
= fnvlist_alloc();
1938 sdd
.cleanup_fd
= -1;
1939 sdd
.snapholds
= NULL
;
1941 if (flags
->verbose
|| sdd
.snapholds
!= NULL
) {
1943 * Do a verbose no-op dry run to get all the verbose output
1944 * or to gather snapshot hold's before generating any data,
1945 * then do a non-verbose real run to generate the streams.
1947 sdd
.dryrun
= B_TRUE
;
1948 err
= dump_filesystems(zhp
, &sdd
);
1953 if (flags
->verbose
) {
1954 if (flags
->parsable
) {
1955 (void) fprintf(fout
, "size\t%llu\n",
1956 (longlong_t
)sdd
.size
);
1959 zfs_nicenum(sdd
.size
, buf
, sizeof (buf
));
1960 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1961 "total estimated size is %s\n"), buf
);
1965 /* Ensure no snaps found is treated as an error. */
1971 /* Skip the second run if dryrun was requested. */
1975 if (sdd
.snapholds
!= NULL
) {
1976 err
= zfs_hold_nvl(zhp
, sdd
.cleanup_fd
, sdd
.snapholds
);
1980 fnvlist_free(sdd
.snapholds
);
1981 sdd
.snapholds
= NULL
;
1984 sdd
.dryrun
= B_FALSE
;
1985 sdd
.verbose
= B_FALSE
;
1988 err
= dump_filesystems(zhp
, &sdd
);
1989 fsavl_destroy(fsavl
);
1992 /* Ensure no snaps found is treated as an error. */
1993 if (err
== 0 && !sdd
.seento
)
1998 (void) pthread_cancel(tid
);
1999 (void) close(pipefd
[0]);
2000 (void) pthread_join(tid
, NULL
);
2003 if (sdd
.cleanup_fd
!= -1) {
2004 VERIFY(0 == close(sdd
.cleanup_fd
));
2005 sdd
.cleanup_fd
= -1;
2008 if (!flags
->dryrun
&& (flags
->replicate
|| flags
->doall
||
2011 * write final end record. NB: want to do this even if
2012 * there was some error, because it might not be totally
2015 dmu_replay_record_t drr
= { 0 };
2016 drr
.drr_type
= DRR_END
;
2017 if (write(outfd
, &drr
, sizeof (drr
)) == -1) {
2018 return (zfs_standard_error(zhp
->zfs_hdl
,
2023 return (err
|| sdd
.err
);
2026 err
= zfs_standard_error(zhp
->zfs_hdl
, err
, errbuf
);
2028 fsavl_destroy(fsavl
);
2030 fnvlist_free(sdd
.snapholds
);
2032 if (sdd
.cleanup_fd
!= -1)
2033 VERIFY(0 == close(sdd
.cleanup_fd
));
2035 (void) pthread_cancel(tid
);
2036 (void) close(pipefd
[0]);
2037 (void) pthread_join(tid
, NULL
);
2043 zfs_send_one(zfs_handle_t
*zhp
, const char *from
, int fd
,
2044 enum lzc_send_flags flags
)
2047 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
2050 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2051 "warning: cannot send '%s'"), zhp
->zfs_name
);
2053 err
= lzc_send(zhp
->zfs_name
, from
, fd
, flags
);
2057 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2058 "not an earlier snapshot from the same fs"));
2059 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
2063 if (lzc_exists(zhp
->zfs_name
)) {
2064 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2065 "incremental source (%s) does not exist"),
2068 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2071 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2072 "target is busy; if a filesystem, "
2073 "it must not be mounted"));
2074 return (zfs_error(hdl
, EZFS_BUSY
, errbuf
));
2087 zfs_error_aux(hdl
, strerror(errno
));
2088 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
2091 return (zfs_standard_error(hdl
, errno
, errbuf
));
2098 * Routines specific to "zfs recv"
2102 recv_read(libzfs_handle_t
*hdl
, int fd
, void *buf
, int ilen
,
2103 boolean_t byteswap
, zio_cksum_t
*zc
)
2109 assert(ilen
<= SPA_MAXBLOCKSIZE
);
2112 rv
= read(fd
, cp
, len
);
2117 if (rv
< 0 || len
!= 0) {
2118 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2119 "failed to read from stream"));
2120 return (zfs_error(hdl
, EZFS_BADSTREAM
, dgettext(TEXT_DOMAIN
,
2121 "cannot receive")));
2126 fletcher_4_incremental_byteswap(buf
, ilen
, zc
);
2128 fletcher_4_incremental_native(buf
, ilen
, zc
);
2134 recv_read_nvlist(libzfs_handle_t
*hdl
, int fd
, int len
, nvlist_t
**nvp
,
2135 boolean_t byteswap
, zio_cksum_t
*zc
)
2140 buf
= zfs_alloc(hdl
, len
);
2144 err
= recv_read(hdl
, fd
, buf
, len
, byteswap
, zc
);
2150 err
= nvlist_unpack(buf
, len
, nvp
, 0);
2153 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
2154 "stream (malformed nvlist)"));
2161 recv_rename(libzfs_handle_t
*hdl
, const char *name
, const char *tryname
,
2162 int baselen
, char *newname
, recvflags_t
*flags
)
2165 zfs_cmd_t zc
= {"\0"};
2167 prop_changelist_t
*clp
;
2170 zhp
= zfs_open(hdl
, name
, ZFS_TYPE_DATASET
);
2173 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
2174 flags
->force
? MS_FORCE
: 0);
2178 err
= changelist_prefix(clp
);
2182 zc
.zc_objset_type
= DMU_OST_ZFS
;
2183 (void) strlcpy(zc
.zc_name
, name
, sizeof (zc
.zc_name
));
2186 (void) strcpy(newname
, tryname
);
2188 (void) strlcpy(zc
.zc_value
, tryname
, sizeof (zc
.zc_value
));
2190 if (flags
->verbose
) {
2191 (void) printf("attempting rename %s to %s\n",
2192 zc
.zc_name
, zc
.zc_value
);
2194 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_RENAME
, &zc
);
2196 changelist_rename(clp
, name
, tryname
);
2201 if (err
!= 0 && strncmp(name
+ baselen
, "recv-", 5) != 0) {
2204 (void) snprintf(newname
, ZFS_MAX_DATASET_NAME_LEN
,
2205 "%.*srecv-%u-%u", baselen
, name
, getpid(), seq
);
2206 (void) strlcpy(zc
.zc_value
, newname
, sizeof (zc
.zc_value
));
2208 if (flags
->verbose
) {
2209 (void) printf("failed - trying rename %s to %s\n",
2210 zc
.zc_name
, zc
.zc_value
);
2212 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_RENAME
, &zc
);
2214 changelist_rename(clp
, name
, newname
);
2215 if (err
&& flags
->verbose
) {
2216 (void) printf("failed (%u) - "
2217 "will try again on next pass\n", errno
);
2220 } else if (flags
->verbose
) {
2222 (void) printf("success\n");
2224 (void) printf("failed (%u)\n", errno
);
2227 (void) changelist_postfix(clp
);
2228 changelist_free(clp
);
2234 recv_destroy(libzfs_handle_t
*hdl
, const char *name
, int baselen
,
2235 char *newname
, recvflags_t
*flags
)
2237 zfs_cmd_t zc
= {"\0"};
2239 prop_changelist_t
*clp
;
2241 boolean_t defer
= B_FALSE
;
2244 zhp
= zfs_open(hdl
, name
, ZFS_TYPE_DATASET
);
2247 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
2248 flags
->force
? MS_FORCE
: 0);
2249 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
&&
2250 zfs_spa_version(zhp
, &spa_version
) == 0 &&
2251 spa_version
>= SPA_VERSION_USERREFS
)
2256 err
= changelist_prefix(clp
);
2260 zc
.zc_objset_type
= DMU_OST_ZFS
;
2261 zc
.zc_defer_destroy
= defer
;
2262 (void) strlcpy(zc
.zc_name
, name
, sizeof (zc
.zc_name
));
2265 (void) printf("attempting destroy %s\n", zc
.zc_name
);
2266 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_DESTROY
, &zc
);
2269 (void) printf("success\n");
2270 changelist_remove(clp
, zc
.zc_name
);
2273 (void) changelist_postfix(clp
);
2274 changelist_free(clp
);
2277 * Deferred destroy might destroy the snapshot or only mark it to be
2278 * destroyed later, and it returns success in either case.
2280 if (err
!= 0 || (defer
&& zfs_dataset_exists(hdl
, name
,
2281 ZFS_TYPE_SNAPSHOT
))) {
2282 err
= recv_rename(hdl
, name
, NULL
, baselen
, newname
, flags
);
2288 typedef struct guid_to_name_data
{
2290 boolean_t bookmark_ok
;
2293 } guid_to_name_data_t
;
2296 guid_to_name_cb(zfs_handle_t
*zhp
, void *arg
)
2298 guid_to_name_data_t
*gtnd
= arg
;
2302 if (gtnd
->skip
!= NULL
&&
2303 (slash
= strrchr(zhp
->zfs_name
, '/')) != NULL
&&
2304 strcmp(slash
+ 1, gtnd
->skip
) == 0) {
2309 if (zfs_prop_get_int(zhp
, ZFS_PROP_GUID
) == gtnd
->guid
) {
2310 (void) strcpy(gtnd
->name
, zhp
->zfs_name
);
2315 err
= zfs_iter_children(zhp
, guid_to_name_cb
, gtnd
);
2316 if (err
!= EEXIST
&& gtnd
->bookmark_ok
)
2317 err
= zfs_iter_bookmarks(zhp
, guid_to_name_cb
, gtnd
);
2323 * Attempt to find the local dataset associated with this guid. In the case of
2324 * multiple matches, we attempt to find the "best" match by searching
2325 * progressively larger portions of the hierarchy. This allows one to send a
2326 * tree of datasets individually and guarantee that we will find the source
2327 * guid within that hierarchy, even if there are multiple matches elsewhere.
2330 guid_to_name(libzfs_handle_t
*hdl
, const char *parent
, uint64_t guid
,
2331 boolean_t bookmark_ok
, char *name
)
2333 char pname
[ZFS_MAX_DATASET_NAME_LEN
];
2334 guid_to_name_data_t gtnd
;
2337 gtnd
.bookmark_ok
= bookmark_ok
;
2342 * Search progressively larger portions of the hierarchy, starting
2343 * with the filesystem specified by 'parent'. This will
2344 * select the "most local" version of the origin snapshot in the case
2345 * that there are multiple matching snapshots in the system.
2347 (void) strlcpy(pname
, parent
, sizeof (pname
));
2348 char *cp
= strrchr(pname
, '@');
2350 cp
= strchr(pname
, '\0');
2351 for (; cp
!= NULL
; cp
= strrchr(pname
, '/')) {
2352 /* Chop off the last component and open the parent */
2354 zfs_handle_t
*zhp
= make_dataset_handle(hdl
, pname
);
2358 int err
= guid_to_name_cb(zfs_handle_dup(zhp
), >nd
);
2360 err
= zfs_iter_children(zhp
, guid_to_name_cb
, >nd
);
2361 if (err
!= EEXIST
&& bookmark_ok
)
2362 err
= zfs_iter_bookmarks(zhp
, guid_to_name_cb
, >nd
);
2368 * Remember the last portion of the dataset so we skip it next
2369 * time through (as we've already searched that portion of the
2372 gtnd
.skip
= strrchr(pname
, '/') + 1;
2379 * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if
2380 * guid1 is after guid2.
2383 created_before(libzfs_handle_t
*hdl
, avl_tree_t
*avl
,
2384 uint64_t guid1
, uint64_t guid2
)
2387 char *fsname
= NULL
, *snapname
= NULL
;
2388 char buf
[ZFS_MAX_DATASET_NAME_LEN
];
2390 zfs_handle_t
*guid1hdl
, *guid2hdl
;
2391 uint64_t create1
, create2
;
2398 nvfs
= fsavl_find(avl
, guid1
, &snapname
);
2399 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
2400 (void) snprintf(buf
, sizeof (buf
), "%s@%s", fsname
, snapname
);
2401 guid1hdl
= zfs_open(hdl
, buf
, ZFS_TYPE_SNAPSHOT
);
2402 if (guid1hdl
== NULL
)
2405 nvfs
= fsavl_find(avl
, guid2
, &snapname
);
2406 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
2407 (void) snprintf(buf
, sizeof (buf
), "%s@%s", fsname
, snapname
);
2408 guid2hdl
= zfs_open(hdl
, buf
, ZFS_TYPE_SNAPSHOT
);
2409 if (guid2hdl
== NULL
) {
2410 zfs_close(guid1hdl
);
2414 create1
= zfs_prop_get_int(guid1hdl
, ZFS_PROP_CREATETXG
);
2415 create2
= zfs_prop_get_int(guid2hdl
, ZFS_PROP_CREATETXG
);
2417 if (create1
< create2
)
2419 else if (create1
> create2
)
2424 zfs_close(guid1hdl
);
2425 zfs_close(guid2hdl
);
2431 recv_incremental_replication(libzfs_handle_t
*hdl
, const char *tofs
,
2432 recvflags_t
*flags
, nvlist_t
*stream_nv
, avl_tree_t
*stream_avl
,
2435 nvlist_t
*local_nv
, *deleted
= NULL
;
2436 avl_tree_t
*local_avl
;
2437 nvpair_t
*fselem
, *nextfselem
;
2439 char newname
[ZFS_MAX_DATASET_NAME_LEN
];
2442 boolean_t needagain
, progress
, recursive
;
2445 VERIFY(0 == nvlist_lookup_string(stream_nv
, "fromsnap", &fromsnap
));
2447 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
2454 needagain
= progress
= B_FALSE
;
2456 VERIFY(0 == nvlist_alloc(&deleted
, NV_UNIQUE_NAME
, 0));
2458 if ((error
= gather_nvlist(hdl
, tofs
, fromsnap
, NULL
,
2459 recursive
, B_FALSE
, &local_nv
, &local_avl
)) != 0)
2463 * Process deletes and renames
2465 for (fselem
= nvlist_next_nvpair(local_nv
, NULL
);
2466 fselem
; fselem
= nextfselem
) {
2467 nvlist_t
*nvfs
, *snaps
;
2468 nvlist_t
*stream_nvfs
= NULL
;
2469 nvpair_t
*snapelem
, *nextsnapelem
;
2470 uint64_t fromguid
= 0;
2471 uint64_t originguid
= 0;
2472 uint64_t stream_originguid
= 0;
2473 uint64_t parent_fromsnap_guid
, stream_parent_fromsnap_guid
;
2474 char *fsname
, *stream_fsname
;
2476 nextfselem
= nvlist_next_nvpair(local_nv
, fselem
);
2478 VERIFY(0 == nvpair_value_nvlist(fselem
, &nvfs
));
2479 VERIFY(0 == nvlist_lookup_nvlist(nvfs
, "snaps", &snaps
));
2480 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
2481 VERIFY(0 == nvlist_lookup_uint64(nvfs
, "parentfromsnap",
2482 &parent_fromsnap_guid
));
2483 (void) nvlist_lookup_uint64(nvfs
, "origin", &originguid
);
2486 * First find the stream's fs, so we can check for
2487 * a different origin (due to "zfs promote")
2489 for (snapelem
= nvlist_next_nvpair(snaps
, NULL
);
2490 snapelem
; snapelem
= nvlist_next_nvpair(snaps
, snapelem
)) {
2493 VERIFY(0 == nvpair_value_uint64(snapelem
, &thisguid
));
2494 stream_nvfs
= fsavl_find(stream_avl
, thisguid
, NULL
);
2496 if (stream_nvfs
!= NULL
)
2500 /* check for promote */
2501 (void) nvlist_lookup_uint64(stream_nvfs
, "origin",
2502 &stream_originguid
);
2503 if (stream_nvfs
&& originguid
!= stream_originguid
) {
2504 switch (created_before(hdl
, local_avl
,
2505 stream_originguid
, originguid
)) {
2508 zfs_cmd_t zc
= {"\0"};
2509 nvlist_t
*origin_nvfs
;
2510 char *origin_fsname
;
2513 (void) printf("promoting %s\n", fsname
);
2515 origin_nvfs
= fsavl_find(local_avl
, originguid
,
2517 VERIFY(0 == nvlist_lookup_string(origin_nvfs
,
2518 "name", &origin_fsname
));
2519 (void) strlcpy(zc
.zc_value
, origin_fsname
,
2520 sizeof (zc
.zc_value
));
2521 (void) strlcpy(zc
.zc_name
, fsname
,
2522 sizeof (zc
.zc_name
));
2523 error
= zfs_ioctl(hdl
, ZFS_IOC_PROMOTE
, &zc
);
2531 fsavl_destroy(local_avl
);
2532 nvlist_free(local_nv
);
2536 * We had/have the wrong origin, therefore our
2537 * list of snapshots is wrong. Need to handle
2538 * them on the next pass.
2544 for (snapelem
= nvlist_next_nvpair(snaps
, NULL
);
2545 snapelem
; snapelem
= nextsnapelem
) {
2547 char *stream_snapname
;
2548 nvlist_t
*found
, *props
;
2550 nextsnapelem
= nvlist_next_nvpair(snaps
, snapelem
);
2552 VERIFY(0 == nvpair_value_uint64(snapelem
, &thisguid
));
2553 found
= fsavl_find(stream_avl
, thisguid
,
2556 /* check for delete */
2557 if (found
== NULL
) {
2558 char name
[ZFS_MAX_DATASET_NAME_LEN
];
2563 (void) snprintf(name
, sizeof (name
), "%s@%s",
2564 fsname
, nvpair_name(snapelem
));
2566 error
= recv_destroy(hdl
, name
,
2567 strlen(fsname
)+1, newname
, flags
);
2572 sprintf(guidname
, "%llu",
2573 (u_longlong_t
)thisguid
);
2574 nvlist_add_boolean(deleted
, guidname
);
2578 stream_nvfs
= found
;
2580 if (0 == nvlist_lookup_nvlist(stream_nvfs
, "snapprops",
2581 &props
) && 0 == nvlist_lookup_nvlist(props
,
2582 stream_snapname
, &props
)) {
2583 zfs_cmd_t zc
= {"\0"};
2585 zc
.zc_cookie
= B_TRUE
; /* received */
2586 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
),
2587 "%s@%s", fsname
, nvpair_name(snapelem
));
2588 if (zcmd_write_src_nvlist(hdl
, &zc
,
2590 (void) zfs_ioctl(hdl
,
2591 ZFS_IOC_SET_PROP
, &zc
);
2592 zcmd_free_nvlists(&zc
);
2596 /* check for different snapname */
2597 if (strcmp(nvpair_name(snapelem
),
2598 stream_snapname
) != 0) {
2599 char name
[ZFS_MAX_DATASET_NAME_LEN
];
2600 char tryname
[ZFS_MAX_DATASET_NAME_LEN
];
2602 (void) snprintf(name
, sizeof (name
), "%s@%s",
2603 fsname
, nvpair_name(snapelem
));
2604 (void) snprintf(tryname
, sizeof (name
), "%s@%s",
2605 fsname
, stream_snapname
);
2607 error
= recv_rename(hdl
, name
, tryname
,
2608 strlen(fsname
)+1, newname
, flags
);
2615 if (strcmp(stream_snapname
, fromsnap
) == 0)
2616 fromguid
= thisguid
;
2619 /* check for delete */
2620 if (stream_nvfs
== NULL
) {
2624 error
= recv_destroy(hdl
, fsname
, strlen(tofs
)+1,
2630 sprintf(guidname
, "%llu",
2631 (u_longlong_t
)parent_fromsnap_guid
);
2632 nvlist_add_boolean(deleted
, guidname
);
2636 if (fromguid
== 0) {
2637 if (flags
->verbose
) {
2638 (void) printf("local fs %s does not have "
2639 "fromsnap (%s in stream); must have "
2640 "been deleted locally; ignoring\n",
2646 VERIFY(0 == nvlist_lookup_string(stream_nvfs
,
2647 "name", &stream_fsname
));
2648 VERIFY(0 == nvlist_lookup_uint64(stream_nvfs
,
2649 "parentfromsnap", &stream_parent_fromsnap_guid
));
2651 s1
= strrchr(fsname
, '/');
2652 s2
= strrchr(stream_fsname
, '/');
2655 * Check if we're going to rename based on parent guid change
2656 * and the current parent guid was also deleted. If it was then
2657 * rename will fail and is likely unneeded, so avoid this and
2658 * force an early retry to determine the new
2659 * parent_fromsnap_guid.
2661 if (stream_parent_fromsnap_guid
!= 0 &&
2662 parent_fromsnap_guid
!= 0 &&
2663 stream_parent_fromsnap_guid
!= parent_fromsnap_guid
) {
2664 sprintf(guidname
, "%llu",
2665 (u_longlong_t
)parent_fromsnap_guid
);
2666 if (nvlist_exists(deleted
, guidname
)) {
2674 * Check for rename. If the exact receive path is specified, it
2675 * does not count as a rename, but we still need to check the
2676 * datasets beneath it.
2678 if ((stream_parent_fromsnap_guid
!= 0 &&
2679 parent_fromsnap_guid
!= 0 &&
2680 stream_parent_fromsnap_guid
!= parent_fromsnap_guid
) ||
2681 ((flags
->isprefix
|| strcmp(tofs
, fsname
) != 0) &&
2682 (s1
!= NULL
) && (s2
!= NULL
) && strcmp(s1
, s2
) != 0)) {
2684 char tryname
[ZFS_MAX_DATASET_NAME_LEN
];
2686 parent
= fsavl_find(local_avl
,
2687 stream_parent_fromsnap_guid
, NULL
);
2689 * NB: parent might not be found if we used the
2690 * tosnap for stream_parent_fromsnap_guid,
2691 * because the parent is a newly-created fs;
2692 * we'll be able to rename it after we recv the
2695 if (parent
!= NULL
) {
2698 VERIFY(0 == nvlist_lookup_string(parent
, "name",
2700 (void) snprintf(tryname
, sizeof (tryname
),
2701 "%s%s", pname
, strrchr(stream_fsname
, '/'));
2704 if (flags
->verbose
) {
2705 (void) printf("local fs %s new parent "
2706 "not found\n", fsname
);
2712 error
= recv_rename(hdl
, fsname
, tryname
,
2713 strlen(tofs
)+1, newname
, flags
);
2715 if (renamed
!= NULL
&& newname
[0] != '\0') {
2716 VERIFY(0 == nvlist_add_boolean(renamed
,
2728 fsavl_destroy(local_avl
);
2729 nvlist_free(local_nv
);
2730 nvlist_free(deleted
);
2732 if (needagain
&& progress
) {
2733 /* do another pass to fix up temporary names */
2735 (void) printf("another pass:\n");
2743 zfs_receive_package(libzfs_handle_t
*hdl
, int fd
, const char *destname
,
2744 recvflags_t
*flags
, dmu_replay_record_t
*drr
, zio_cksum_t
*zc
,
2745 char **top_zfs
, int cleanup_fd
, uint64_t *action_handlep
)
2747 nvlist_t
*stream_nv
= NULL
;
2748 avl_tree_t
*stream_avl
= NULL
;
2749 char *fromsnap
= NULL
;
2750 char *sendsnap
= NULL
;
2752 char tofs
[ZFS_MAX_DATASET_NAME_LEN
];
2753 char sendfs
[ZFS_MAX_DATASET_NAME_LEN
];
2755 dmu_replay_record_t drre
;
2757 boolean_t anyerr
= B_FALSE
;
2758 boolean_t softerr
= B_FALSE
;
2759 boolean_t recursive
;
2761 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2764 assert(drr
->drr_type
== DRR_BEGIN
);
2765 assert(drr
->drr_u
.drr_begin
.drr_magic
== DMU_BACKUP_MAGIC
);
2766 assert(DMU_GET_STREAM_HDRTYPE(drr
->drr_u
.drr_begin
.drr_versioninfo
) ==
2767 DMU_COMPOUNDSTREAM
);
2770 * Read in the nvlist from the stream.
2772 if (drr
->drr_payloadlen
!= 0) {
2773 error
= recv_read_nvlist(hdl
, fd
, drr
->drr_payloadlen
,
2774 &stream_nv
, flags
->byteswap
, zc
);
2776 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2781 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
2784 if (recursive
&& strchr(destname
, '@')) {
2785 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2786 "cannot specify snapshot name for multi-snapshot stream"));
2787 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2792 * Read in the end record and verify checksum.
2794 if (0 != (error
= recv_read(hdl
, fd
, &drre
, sizeof (drre
),
2795 flags
->byteswap
, NULL
)))
2797 if (flags
->byteswap
) {
2798 drre
.drr_type
= BSWAP_32(drre
.drr_type
);
2799 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[0] =
2800 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[0]);
2801 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[1] =
2802 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[1]);
2803 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[2] =
2804 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[2]);
2805 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[3] =
2806 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[3]);
2808 if (drre
.drr_type
!= DRR_END
) {
2809 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2812 if (!ZIO_CHECKSUM_EQUAL(drre
.drr_u
.drr_end
.drr_checksum
, *zc
)) {
2813 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2814 "incorrect header checksum"));
2815 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2819 (void) nvlist_lookup_string(stream_nv
, "fromsnap", &fromsnap
);
2821 if (drr
->drr_payloadlen
!= 0) {
2822 nvlist_t
*stream_fss
;
2824 VERIFY(0 == nvlist_lookup_nvlist(stream_nv
, "fss",
2826 if ((stream_avl
= fsavl_create(stream_fss
)) == NULL
) {
2827 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2828 "couldn't allocate avl tree"));
2829 error
= zfs_error(hdl
, EZFS_NOMEM
, errbuf
);
2833 if (fromsnap
!= NULL
) {
2834 nvlist_t
*renamed
= NULL
;
2835 nvpair_t
*pair
= NULL
;
2837 (void) strlcpy(tofs
, destname
, sizeof (tofs
));
2838 if (flags
->isprefix
) {
2839 struct drr_begin
*drrb
= &drr
->drr_u
.drr_begin
;
2842 if (flags
->istail
) {
2843 cp
= strrchr(drrb
->drr_toname
, '/');
2845 (void) strlcat(tofs
, "/",
2849 i
= (cp
- drrb
->drr_toname
);
2852 i
= strcspn(drrb
->drr_toname
, "/@");
2854 /* zfs_receive_one() will create_parents() */
2855 (void) strlcat(tofs
, &drrb
->drr_toname
[i
],
2857 *strchr(tofs
, '@') = '\0';
2860 if (recursive
&& !flags
->dryrun
&& !flags
->nomount
) {
2861 VERIFY(0 == nvlist_alloc(&renamed
,
2862 NV_UNIQUE_NAME
, 0));
2865 softerr
= recv_incremental_replication(hdl
, tofs
, flags
,
2866 stream_nv
, stream_avl
, renamed
);
2868 /* Unmount renamed filesystems before receiving. */
2869 while ((pair
= nvlist_next_nvpair(renamed
,
2872 prop_changelist_t
*clp
= NULL
;
2874 zhp
= zfs_open(hdl
, nvpair_name(pair
),
2875 ZFS_TYPE_FILESYSTEM
);
2877 clp
= changelist_gather(zhp
,
2878 ZFS_PROP_MOUNTPOINT
, 0, 0);
2882 changelist_prefix(clp
);
2883 changelist_free(clp
);
2888 nvlist_free(renamed
);
2893 * Get the fs specified by the first path in the stream (the top level
2894 * specified by 'zfs send') and pass it to each invocation of
2895 * zfs_receive_one().
2897 (void) strlcpy(sendfs
, drr
->drr_u
.drr_begin
.drr_toname
,
2899 if ((cp
= strchr(sendfs
, '@')) != NULL
) {
2902 * Find the "sendsnap", the final snapshot in a replication
2903 * stream. zfs_receive_one() handles certain errors
2904 * differently, depending on if the contained stream is the
2907 sendsnap
= (cp
+ 1);
2910 /* Finally, receive each contained stream */
2913 * we should figure out if it has a recoverable
2914 * error, in which case do a recv_skip() and drive on.
2915 * Note, if we fail due to already having this guid,
2916 * zfs_receive_one() will take care of it (ie,
2917 * recv_skip() and return 0).
2919 error
= zfs_receive_impl(hdl
, destname
, NULL
, flags
, fd
,
2920 sendfs
, stream_nv
, stream_avl
, top_zfs
, cleanup_fd
,
2921 action_handlep
, sendsnap
);
2922 if (error
== ENODATA
) {
2927 } while (error
== 0);
2929 if (drr
->drr_payloadlen
!= 0 && fromsnap
!= NULL
) {
2931 * Now that we have the fs's they sent us, try the
2934 softerr
= recv_incremental_replication(hdl
, tofs
, flags
,
2935 stream_nv
, stream_avl
, NULL
);
2939 fsavl_destroy(stream_avl
);
2940 nvlist_free(stream_nv
);
2949 trunc_prop_errs(int truncated
)
2951 ASSERT(truncated
!= 0);
2954 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
2955 "1 more property could not be set\n"));
2957 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
2958 "%d more properties could not be set\n"), truncated
);
2962 recv_skip(libzfs_handle_t
*hdl
, int fd
, boolean_t byteswap
)
2964 dmu_replay_record_t
*drr
;
2965 void *buf
= zfs_alloc(hdl
, SPA_MAXBLOCKSIZE
);
2968 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2969 "cannot receive:"));
2971 /* XXX would be great to use lseek if possible... */
2974 while (recv_read(hdl
, fd
, drr
, sizeof (dmu_replay_record_t
),
2975 byteswap
, NULL
) == 0) {
2977 drr
->drr_type
= BSWAP_32(drr
->drr_type
);
2979 switch (drr
->drr_type
) {
2981 if (drr
->drr_payloadlen
!= 0) {
2982 (void) recv_read(hdl
, fd
, buf
,
2983 drr
->drr_payloadlen
, B_FALSE
, NULL
);
2993 drr
->drr_u
.drr_object
.drr_bonuslen
=
2994 BSWAP_32(drr
->drr_u
.drr_object
.
2997 (void) recv_read(hdl
, fd
, buf
,
2998 P2ROUNDUP(drr
->drr_u
.drr_object
.drr_bonuslen
, 8),
3004 drr
->drr_u
.drr_write
.drr_logical_size
=
3006 drr
->drr_u
.drr_write
.drr_logical_size
);
3007 drr
->drr_u
.drr_write
.drr_compressed_size
=
3009 drr
->drr_u
.drr_write
.drr_compressed_size
);
3011 uint64_t payload_size
=
3012 DRR_WRITE_PAYLOAD_SIZE(&drr
->drr_u
.drr_write
);
3013 (void) recv_read(hdl
, fd
, buf
,
3014 payload_size
, B_FALSE
, NULL
);
3018 drr
->drr_u
.drr_spill
.drr_length
=
3019 BSWAP_64(drr
->drr_u
.drr_spill
.drr_length
);
3021 (void) recv_read(hdl
, fd
, buf
,
3022 drr
->drr_u
.drr_spill
.drr_length
, B_FALSE
, NULL
);
3024 case DRR_WRITE_EMBEDDED
:
3026 drr
->drr_u
.drr_write_embedded
.drr_psize
=
3027 BSWAP_32(drr
->drr_u
.drr_write_embedded
.
3030 (void) recv_read(hdl
, fd
, buf
,
3031 P2ROUNDUP(drr
->drr_u
.drr_write_embedded
.drr_psize
,
3034 case DRR_WRITE_BYREF
:
3035 case DRR_FREEOBJECTS
:
3040 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3041 "invalid record type"));
3043 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3052 recv_ecksum_set_aux(libzfs_handle_t
*hdl
, const char *target_snap
,
3053 boolean_t resumable
)
3055 char target_fs
[ZFS_MAX_DATASET_NAME_LEN
];
3057 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3058 "checksum mismatch or incomplete stream"));
3062 (void) strlcpy(target_fs
, target_snap
, sizeof (target_fs
));
3063 *strchr(target_fs
, '@') = '\0';
3064 zfs_handle_t
*zhp
= zfs_open(hdl
, target_fs
,
3065 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3069 char token_buf
[ZFS_MAXPROPLEN
];
3070 int error
= zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
3071 token_buf
, sizeof (token_buf
),
3072 NULL
, NULL
, 0, B_TRUE
);
3074 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3075 "checksum mismatch or incomplete stream.\n"
3076 "Partially received snapshot is saved.\n"
3077 "A resuming stream can be generated on the sending "
3078 "system by running:\n"
3086 * Restores a backup of tosnap from the file descriptor specified by infd.
3089 zfs_receive_one(libzfs_handle_t
*hdl
, int infd
, const char *tosnap
,
3090 const char *originsnap
, recvflags_t
*flags
, dmu_replay_record_t
*drr
,
3091 dmu_replay_record_t
*drr_noswap
, const char *sendfs
, nvlist_t
*stream_nv
,
3092 avl_tree_t
*stream_avl
, char **top_zfs
, int cleanup_fd
,
3093 uint64_t *action_handlep
, const char *finalsnap
)
3096 int ioctl_err
, ioctl_errno
, err
;
3098 struct drr_begin
*drrb
= &drr
->drr_u
.drr_begin
;
3100 const char *chopprefix
;
3101 boolean_t newfs
= B_FALSE
;
3102 boolean_t stream_wantsnewfs
;
3103 boolean_t newprops
= B_FALSE
;
3104 uint64_t read_bytes
= 0;
3105 uint64_t errflags
= 0;
3106 uint64_t parent_snapguid
= 0;
3107 prop_changelist_t
*clp
= NULL
;
3108 nvlist_t
*snapprops_nvlist
= NULL
;
3109 zprop_errflags_t prop_errflags
;
3110 nvlist_t
*prop_errors
= NULL
;
3111 boolean_t recursive
;
3112 char *snapname
= NULL
;
3113 char destsnap
[MAXPATHLEN
* 2];
3114 char origin
[MAXNAMELEN
];
3115 char name
[MAXPATHLEN
];
3116 nvlist_t
*props
= NULL
;
3118 begin_time
= time(NULL
);
3119 bzero(origin
, MAXNAMELEN
);
3121 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3124 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
3127 if (stream_avl
!= NULL
) {
3128 nvlist_t
*lookup
= NULL
;
3129 nvlist_t
*fs
= fsavl_find(stream_avl
, drrb
->drr_toguid
,
3132 (void) nvlist_lookup_uint64(fs
, "parentfromsnap",
3134 err
= nvlist_lookup_nvlist(fs
, "props", &props
);
3136 VERIFY(0 == nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0));
3140 if (flags
->canmountoff
) {
3141 VERIFY(0 == nvlist_add_uint64(props
,
3142 zfs_prop_to_name(ZFS_PROP_CANMOUNT
), 0));
3144 if (0 == nvlist_lookup_nvlist(fs
, "snapprops", &lookup
)) {
3145 VERIFY(0 == nvlist_lookup_nvlist(lookup
,
3146 snapname
, &snapprops_nvlist
));
3153 * Determine how much of the snapshot name stored in the stream
3154 * we are going to tack on to the name they specified on the
3155 * command line, and how much we are going to chop off.
3157 * If they specified a snapshot, chop the entire name stored in
3160 if (flags
->istail
) {
3162 * A filesystem was specified with -e. We want to tack on only
3163 * the tail of the sent snapshot path.
3165 if (strchr(tosnap
, '@')) {
3166 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
3167 "argument - snapshot not allowed with -e"));
3168 err
= zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
3172 chopprefix
= strrchr(sendfs
, '/');
3174 if (chopprefix
== NULL
) {
3176 * The tail is the poolname, so we need to
3177 * prepend a path separator.
3179 int len
= strlen(drrb
->drr_toname
);
3180 cp
= malloc(len
+ 2);
3182 (void) strcpy(&cp
[1], drrb
->drr_toname
);
3185 chopprefix
= drrb
->drr_toname
+ (chopprefix
- sendfs
);
3187 } else if (flags
->isprefix
) {
3189 * A filesystem was specified with -d. We want to tack on
3190 * everything but the first element of the sent snapshot path
3191 * (all but the pool name).
3193 if (strchr(tosnap
, '@')) {
3194 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
3195 "argument - snapshot not allowed with -d"));
3196 err
= zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
3200 chopprefix
= strchr(drrb
->drr_toname
, '/');
3201 if (chopprefix
== NULL
)
3202 chopprefix
= strchr(drrb
->drr_toname
, '@');
3203 } else if (strchr(tosnap
, '@') == NULL
) {
3205 * If a filesystem was specified without -d or -e, we want to
3206 * tack on everything after the fs specified by 'zfs send'.
3208 chopprefix
= drrb
->drr_toname
+ strlen(sendfs
);
3210 /* A snapshot was specified as an exact path (no -d or -e). */
3212 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3213 "cannot specify snapshot name for multi-snapshot "
3215 err
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
3218 chopprefix
= drrb
->drr_toname
+ strlen(drrb
->drr_toname
);
3221 ASSERT(strstr(drrb
->drr_toname
, sendfs
) == drrb
->drr_toname
);
3222 ASSERT(chopprefix
> drrb
->drr_toname
);
3223 ASSERT(chopprefix
<= drrb
->drr_toname
+ strlen(drrb
->drr_toname
));
3224 ASSERT(chopprefix
[0] == '/' || chopprefix
[0] == '@' ||
3225 chopprefix
[0] == '\0');
3228 * Determine name of destination snapshot.
3230 (void) strlcpy(destsnap
, tosnap
, sizeof (destsnap
));
3231 (void) strlcat(destsnap
, chopprefix
, sizeof (destsnap
));
3233 if (!zfs_name_valid(destsnap
, ZFS_TYPE_SNAPSHOT
)) {
3234 err
= zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
3239 * Determine the name of the origin snapshot.
3242 (void) strncpy(origin
, originsnap
, sizeof (origin
));
3244 (void) printf("using provided clone origin %s\n",
3246 } else if (drrb
->drr_flags
& DRR_FLAG_CLONE
) {
3247 if (guid_to_name(hdl
, destsnap
,
3248 drrb
->drr_fromguid
, B_FALSE
, origin
) != 0) {
3249 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3250 "local origin for clone %s does not exist"),
3252 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
3256 (void) printf("found clone origin %s\n", origin
);
3259 boolean_t resuming
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
) &
3260 DMU_BACKUP_FEATURE_RESUMING
;
3261 stream_wantsnewfs
= (drrb
->drr_fromguid
== 0 ||
3262 (drrb
->drr_flags
& DRR_FLAG_CLONE
) || originsnap
) && !resuming
;
3264 if (stream_wantsnewfs
) {
3266 * if the parent fs does not exist, look for it based on
3267 * the parent snap GUID
3269 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3270 "cannot receive new filesystem stream"));
3272 (void) strcpy(name
, destsnap
);
3273 cp
= strrchr(name
, '/');
3277 !zfs_dataset_exists(hdl
, name
, ZFS_TYPE_DATASET
)) {
3278 char suffix
[ZFS_MAX_DATASET_NAME_LEN
];
3279 (void) strcpy(suffix
, strrchr(destsnap
, '/'));
3280 if (guid_to_name(hdl
, name
, parent_snapguid
,
3281 B_FALSE
, destsnap
) == 0) {
3282 *strchr(destsnap
, '@') = '\0';
3283 (void) strcat(destsnap
, suffix
);
3288 * if the fs does not exist, look for it based on the
3291 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3292 "cannot receive incremental stream"));
3294 (void) strcpy(name
, destsnap
);
3295 *strchr(name
, '@') = '\0';
3298 * If the exact receive path was specified and this is the
3299 * topmost path in the stream, then if the fs does not exist we
3300 * should look no further.
3302 if ((flags
->isprefix
|| (*(chopprefix
= drrb
->drr_toname
+
3303 strlen(sendfs
)) != '\0' && *chopprefix
!= '@')) &&
3304 !zfs_dataset_exists(hdl
, name
, ZFS_TYPE_DATASET
)) {
3305 char snap
[ZFS_MAX_DATASET_NAME_LEN
];
3306 (void) strcpy(snap
, strchr(destsnap
, '@'));
3307 if (guid_to_name(hdl
, name
, drrb
->drr_fromguid
,
3308 B_FALSE
, destsnap
) == 0) {
3309 *strchr(destsnap
, '@') = '\0';
3310 (void) strcat(destsnap
, snap
);
3315 (void) strcpy(name
, destsnap
);
3316 *strchr(name
, '@') = '\0';
3318 if (zfs_dataset_exists(hdl
, name
, ZFS_TYPE_DATASET
)) {
3319 zfs_cmd_t zc
= {"\0"};
3322 (void) strcpy(zc
.zc_name
, name
);
3325 * Destination fs exists. It must be one of these cases:
3326 * - an incremental send stream
3327 * - the stream specifies a new fs (full stream or clone)
3328 * and they want us to blow away the existing fs (and
3329 * have therefore specified -F and removed any snapshots)
3330 * - we are resuming a failed receive.
3332 if (stream_wantsnewfs
) {
3333 if (!flags
->force
) {
3334 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3335 "destination '%s' exists\n"
3336 "must specify -F to overwrite it"), name
);
3337 err
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3340 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_SNAPSHOT_LIST_NEXT
,
3342 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3343 "destination has snapshots (eg. %s)\n"
3344 "must destroy them to overwrite it"),
3346 err
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3351 if ((zhp
= zfs_open(hdl
, name
,
3352 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
)) == NULL
) {
3357 if (stream_wantsnewfs
&&
3358 zhp
->zfs_dmustats
.dds_origin
[0]) {
3360 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3361 "destination '%s' is a clone\n"
3362 "must destroy it to overwrite it"), name
);
3363 err
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3367 if (!flags
->dryrun
&& zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
&&
3368 stream_wantsnewfs
) {
3369 /* We can't do online recv in this case */
3370 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0, 0);
3376 if (changelist_prefix(clp
) != 0) {
3377 changelist_free(clp
);
3385 * If we are resuming a newfs, set newfs here so that we will
3386 * mount it if the recv succeeds this time. We can tell
3387 * that it was a newfs on the first recv because the fs
3388 * itself will be inconsistent (if the fs existed when we
3389 * did the first recv, we would have received it into
3392 if (resuming
&& zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
))
3398 * Destination filesystem does not exist. Therefore we better
3399 * be creating a new filesystem (either from a full backup, or
3400 * a clone). It would therefore be invalid if the user
3401 * specified only the pool name (i.e. if the destination name
3402 * contained no slash character).
3404 cp
= strrchr(name
, '/');
3406 if (!stream_wantsnewfs
|| cp
== NULL
) {
3407 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3408 "destination '%s' does not exist"), name
);
3409 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
3414 * Trim off the final dataset component so we perform the
3415 * recvbackup ioctl to the filesystems's parent.
3419 if (flags
->isprefix
&& !flags
->istail
&& !flags
->dryrun
&&
3420 create_parents(hdl
, destsnap
, strlen(tosnap
)) != 0) {
3421 err
= zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
3428 if (flags
->verbose
) {
3429 (void) printf("%s %s stream of %s into %s\n",
3430 flags
->dryrun
? "would receive" : "receiving",
3431 drrb
->drr_fromguid
? "incremental" : "full",
3432 drrb
->drr_toname
, destsnap
);
3433 (void) fflush(stdout
);
3436 if (flags
->dryrun
) {
3437 err
= recv_skip(hdl
, infd
, flags
->byteswap
);
3441 err
= ioctl_err
= lzc_receive_one(destsnap
, props
, origin
,
3442 flags
->force
, flags
->resumable
, infd
, drr_noswap
, cleanup_fd
,
3443 &read_bytes
, &errflags
, action_handlep
, &prop_errors
);
3444 ioctl_errno
= ioctl_err
;
3445 prop_errflags
= errflags
;
3448 nvpair_t
*prop_err
= NULL
;
3450 while ((prop_err
= nvlist_next_nvpair(prop_errors
,
3451 prop_err
)) != NULL
) {
3456 prop
= zfs_name_to_prop(nvpair_name(prop_err
));
3457 (void) nvpair_value_int32(prop_err
, &intval
);
3458 if (strcmp(nvpair_name(prop_err
),
3459 ZPROP_N_MORE_ERRORS
) == 0) {
3460 trunc_prop_errs(intval
);
3462 } else if (snapname
== NULL
|| finalsnap
== NULL
||
3463 strcmp(finalsnap
, snapname
) == 0 ||
3464 strcmp(nvpair_name(prop_err
),
3465 zfs_prop_to_name(ZFS_PROP_REFQUOTA
)) != 0) {
3467 * Skip the special case of, for example,
3468 * "refquota", errors on intermediate
3469 * snapshots leading up to a final one.
3470 * That's why we have all of the checks above.
3472 * See zfs_ioctl.c's extract_delay_props() for
3473 * a list of props which can fail on
3474 * intermediate snapshots, but shouldn't
3475 * affect the overall receive.
3477 (void) snprintf(tbuf
, sizeof (tbuf
),
3478 dgettext(TEXT_DOMAIN
,
3479 "cannot receive %s property on %s"),
3480 nvpair_name(prop_err
), name
);
3481 zfs_setprop_error(hdl
, prop
, intval
, tbuf
);
3486 if (err
== 0 && snapprops_nvlist
) {
3487 zfs_cmd_t zc
= {"\0"};
3489 (void) strcpy(zc
.zc_name
, destsnap
);
3490 zc
.zc_cookie
= B_TRUE
; /* received */
3491 if (zcmd_write_src_nvlist(hdl
, &zc
, snapprops_nvlist
) == 0) {
3492 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
3493 zcmd_free_nvlists(&zc
);
3497 if (err
&& (ioctl_errno
== ENOENT
|| ioctl_errno
== EEXIST
)) {
3499 * It may be that this snapshot already exists,
3500 * in which case we want to consume & ignore it
3501 * rather than failing.
3503 avl_tree_t
*local_avl
;
3504 nvlist_t
*local_nv
, *fs
;
3505 cp
= strchr(destsnap
, '@');
3508 * XXX Do this faster by just iterating over snaps in
3509 * this fs. Also if zc_value does not exist, we will
3510 * get a strange "does not exist" error message.
3513 if (gather_nvlist(hdl
, destsnap
, NULL
, NULL
, B_FALSE
,
3514 B_FALSE
, &local_nv
, &local_avl
) == 0) {
3516 fs
= fsavl_find(local_avl
, drrb
->drr_toguid
, NULL
);
3517 fsavl_destroy(local_avl
);
3518 nvlist_free(local_nv
);
3521 if (flags
->verbose
) {
3522 (void) printf("snap %s already exists; "
3523 "ignoring\n", destsnap
);
3525 err
= ioctl_err
= recv_skip(hdl
, infd
,
3532 if (ioctl_err
!= 0) {
3533 switch (ioctl_errno
) {
3535 cp
= strchr(destsnap
, '@');
3537 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3538 "most recent snapshot of %s does not\n"
3539 "match incremental source"), destsnap
);
3540 (void) zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
3544 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3545 "destination %s has been modified\n"
3546 "since most recent snapshot"), name
);
3547 (void) zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
3550 cp
= strchr(destsnap
, '@');
3552 /* it's the containing fs that exists */
3555 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3556 "destination already exists"));
3557 (void) zfs_error_fmt(hdl
, EZFS_EXISTS
,
3558 dgettext(TEXT_DOMAIN
, "cannot restore to %s"),
3563 if (flags
->resumable
)
3564 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3565 "kernel modules must be upgraded to "
3566 "receive this stream."));
3567 (void) zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
3570 recv_ecksum_set_aux(hdl
, destsnap
, flags
->resumable
);
3571 (void) zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
3574 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3575 "pool must be upgraded to receive this stream."));
3576 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
3579 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3580 "destination %s space quota exceeded"), name
);
3581 (void) zfs_error(hdl
, EZFS_NOSPC
, errbuf
);
3584 (void) zfs_standard_error(hdl
, ioctl_errno
, errbuf
);
3589 * Mount the target filesystem (if created). Also mount any
3590 * children of the target filesystem if we did a replication
3591 * receive (indicated by stream_avl being non-NULL).
3593 cp
= strchr(destsnap
, '@');
3594 if (cp
&& (ioctl_err
== 0 || !newfs
)) {
3598 h
= zfs_open(hdl
, destsnap
,
3599 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3601 if (h
->zfs_type
== ZFS_TYPE_VOLUME
) {
3603 } else if (newfs
|| stream_avl
) {
3605 * Track the first/top of hierarchy fs,
3606 * for mounting and sharing later.
3608 if (top_zfs
&& *top_zfs
== NULL
)
3609 *top_zfs
= zfs_strdup(hdl
, destsnap
);
3617 if (!flags
->nomount
)
3618 err
|= changelist_postfix(clp
);
3619 changelist_free(clp
);
3622 if (prop_errflags
& ZPROP_ERR_NOCLEAR
) {
3623 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Warning: "
3624 "failed to clear unreceived properties on %s"), name
);
3625 (void) fprintf(stderr
, "\n");
3627 if (prop_errflags
& ZPROP_ERR_NORESTORE
) {
3628 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Warning: "
3629 "failed to restore original properties on %s"), name
);
3630 (void) fprintf(stderr
, "\n");
3633 if (err
|| ioctl_err
) {
3638 if (flags
->verbose
) {
3641 uint64_t bytes
= read_bytes
;
3642 time_t delta
= time(NULL
) - begin_time
;
3645 zfs_nicenum(bytes
, buf1
, sizeof (buf1
));
3646 zfs_nicenum(bytes
/delta
, buf2
, sizeof (buf1
));
3648 (void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
3654 if (prop_errors
!= NULL
)
3655 nvlist_free(prop_errors
);
3664 zfs_receive_impl(libzfs_handle_t
*hdl
, const char *tosnap
,
3665 const char *originsnap
, recvflags_t
*flags
, int infd
, const char *sendfs
,
3666 nvlist_t
*stream_nv
, avl_tree_t
*stream_avl
, char **top_zfs
, int cleanup_fd
,
3667 uint64_t *action_handlep
, const char *finalsnap
)
3670 dmu_replay_record_t drr
, drr_noswap
;
3671 struct drr_begin
*drrb
= &drr
.drr_u
.drr_begin
;
3673 zio_cksum_t zcksum
= { { 0 } };
3674 uint64_t featureflags
;
3677 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3680 if (flags
->isprefix
&&
3681 !zfs_dataset_exists(hdl
, tosnap
, ZFS_TYPE_DATASET
)) {
3682 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "specified fs "
3683 "(%s) does not exist"), tosnap
);
3684 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3687 !zfs_dataset_exists(hdl
, originsnap
, ZFS_TYPE_DATASET
)) {
3688 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "specified origin fs "
3689 "(%s) does not exist"), originsnap
);
3690 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3693 /* read in the BEGIN record */
3694 if (0 != (err
= recv_read(hdl
, infd
, &drr
, sizeof (drr
), B_FALSE
,
3698 if (drr
.drr_type
== DRR_END
|| drr
.drr_type
== BSWAP_32(DRR_END
)) {
3699 /* It's the double end record at the end of a package */
3703 /* the kernel needs the non-byteswapped begin record */
3706 flags
->byteswap
= B_FALSE
;
3707 if (drrb
->drr_magic
== BSWAP_64(DMU_BACKUP_MAGIC
)) {
3709 * We computed the checksum in the wrong byteorder in
3710 * recv_read() above; do it again correctly.
3712 bzero(&zcksum
, sizeof (zio_cksum_t
));
3713 fletcher_4_incremental_byteswap(&drr
, sizeof (drr
), &zcksum
);
3714 flags
->byteswap
= B_TRUE
;
3716 drr
.drr_type
= BSWAP_32(drr
.drr_type
);
3717 drr
.drr_payloadlen
= BSWAP_32(drr
.drr_payloadlen
);
3718 drrb
->drr_magic
= BSWAP_64(drrb
->drr_magic
);
3719 drrb
->drr_versioninfo
= BSWAP_64(drrb
->drr_versioninfo
);
3720 drrb
->drr_creation_time
= BSWAP_64(drrb
->drr_creation_time
);
3721 drrb
->drr_type
= BSWAP_32(drrb
->drr_type
);
3722 drrb
->drr_flags
= BSWAP_32(drrb
->drr_flags
);
3723 drrb
->drr_toguid
= BSWAP_64(drrb
->drr_toguid
);
3724 drrb
->drr_fromguid
= BSWAP_64(drrb
->drr_fromguid
);
3727 if (drrb
->drr_magic
!= DMU_BACKUP_MAGIC
|| drr
.drr_type
!= DRR_BEGIN
) {
3728 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
3729 "stream (bad magic number)"));
3730 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3733 featureflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
3734 hdrtype
= DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
);
3736 if (!DMU_STREAM_SUPPORTED(featureflags
) ||
3737 (hdrtype
!= DMU_SUBSTREAM
&& hdrtype
!= DMU_COMPOUNDSTREAM
)) {
3738 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3739 "stream has unsupported feature, feature flags = %lx"),
3741 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3744 if (strchr(drrb
->drr_toname
, '@') == NULL
) {
3745 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
3746 "stream (bad snapshot name)"));
3747 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3750 if (DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) == DMU_SUBSTREAM
) {
3751 char nonpackage_sendfs
[ZFS_MAX_DATASET_NAME_LEN
];
3752 if (sendfs
== NULL
) {
3754 * We were not called from zfs_receive_package(). Get
3755 * the fs specified by 'zfs send'.
3758 (void) strlcpy(nonpackage_sendfs
,
3759 drr
.drr_u
.drr_begin
.drr_toname
,
3760 sizeof (nonpackage_sendfs
));
3761 if ((cp
= strchr(nonpackage_sendfs
, '@')) != NULL
)
3763 sendfs
= nonpackage_sendfs
;
3764 VERIFY(finalsnap
== NULL
);
3766 return (zfs_receive_one(hdl
, infd
, tosnap
, originsnap
, flags
,
3767 &drr
, &drr_noswap
, sendfs
, stream_nv
, stream_avl
, top_zfs
,
3768 cleanup_fd
, action_handlep
, finalsnap
));
3770 assert(DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) ==
3771 DMU_COMPOUNDSTREAM
);
3772 return (zfs_receive_package(hdl
, infd
, tosnap
, flags
, &drr
,
3773 &zcksum
, top_zfs
, cleanup_fd
, action_handlep
));
3778 * Restores a backup of tosnap from the file descriptor specified by infd.
3779 * Return 0 on total success, -2 if some things couldn't be
3780 * destroyed/renamed/promoted, -1 if some things couldn't be received.
3781 * (-1 will override -2, if -1 and the resumable flag was specified the
3782 * transfer can be resumed if the sending side supports it).
3785 zfs_receive(libzfs_handle_t
*hdl
, const char *tosnap
, nvlist_t
*props
,
3786 recvflags_t
*flags
, int infd
, avl_tree_t
*stream_avl
)
3788 char *top_zfs
= NULL
;
3791 uint64_t action_handle
= 0;
3793 char *originsnap
= NULL
;
3796 * The only way fstat can fail is if we do not have a valid file
3799 if (fstat(infd
, &sb
) == -1) {
3805 #ifndef F_SETPIPE_SZ
3806 #define F_SETPIPE_SZ (F_SETLEASE + 7)
3807 #endif /* F_SETPIPE_SZ */
3809 #ifndef F_GETPIPE_SZ
3810 #define F_GETPIPE_SZ (F_GETLEASE + 7)
3811 #endif /* F_GETPIPE_SZ */
3814 * It is not uncommon for gigabytes to be processed in zfs receive.
3815 * Speculatively increase the buffer size via Linux-specific fcntl()
3818 if (S_ISFIFO(sb
.st_mode
)) {
3819 FILE *procf
= fopen("/proc/sys/fs/pipe-max-size", "r");
3821 if (procf
!= NULL
) {
3822 unsigned long max_psize
;
3824 if (fscanf(procf
, "%lu", &max_psize
) > 0) {
3825 cur_psize
= fcntl(infd
, F_GETPIPE_SZ
);
3826 if (cur_psize
> 0 &&
3827 max_psize
> (unsigned long) cur_psize
)
3828 (void) fcntl(infd
, F_SETPIPE_SZ
,
3834 #endif /* __linux__ */
3837 err
= nvlist_lookup_string(props
, "origin", &originsnap
);
3838 if (err
&& err
!= ENOENT
)
3842 cleanup_fd
= open(ZFS_DEV
, O_RDWR
);
3843 VERIFY(cleanup_fd
>= 0);
3845 err
= zfs_receive_impl(hdl
, tosnap
, originsnap
, flags
, infd
, NULL
, NULL
,
3846 stream_avl
, &top_zfs
, cleanup_fd
, &action_handle
, NULL
);
3848 VERIFY(0 == close(cleanup_fd
));
3850 if (err
== 0 && !flags
->nomount
&& top_zfs
) {
3851 zfs_handle_t
*zhp
= NULL
;
3852 prop_changelist_t
*clp
= NULL
;
3854 zhp
= zfs_open(hdl
, top_zfs
, ZFS_TYPE_FILESYSTEM
);
3856 clp
= changelist_gather(zhp
, ZFS_PROP_MOUNTPOINT
,
3857 CL_GATHER_MOUNT_ALWAYS
, 0);
3860 /* mount and share received datasets */
3861 err
= changelist_postfix(clp
);
3862 changelist_free(clp
);
3865 if (zhp
== NULL
|| clp
== NULL
|| err
)