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>
65 /* in libzfs_dataset.c */
66 extern void zfs_setprop_error(libzfs_handle_t
*, zfs_prop_t
, int, char *);
68 static int zfs_receive_impl(libzfs_handle_t
*, const char *, const char *,
69 recvflags_t
*, int, const char *, nvlist_t
*, avl_tree_t
*, char **, int,
70 uint64_t *, const char *);
71 static int guid_to_name(libzfs_handle_t
*, const char *,
72 uint64_t, boolean_t
, char *);
74 static const zio_cksum_t zero_cksum
= { { 0 } };
76 typedef struct dedup_arg
{
79 libzfs_handle_t
*dedup_hdl
;
82 typedef struct progress_arg
{
85 boolean_t pa_parsable
;
88 typedef struct dataref
{
94 typedef struct dedup_entry
{
95 struct dedup_entry
*dde_next
;
96 zio_cksum_t dde_chksum
;
101 #define MAX_DDT_PHYSMEM_PERCENT 20
102 #define SMALLEST_POSSIBLE_MAX_DDT_MB 128
104 typedef struct dedup_table
{
105 dedup_entry_t
**dedup_hash_array
;
106 umem_cache_t
*ddecache
;
107 uint64_t max_ddt_size
; /* max dedup table size in bytes */
108 uint64_t cur_ddt_size
; /* current dedup table size in bytes */
115 high_order_bit(uint64_t n
)
119 for (count
= 0; n
!= 0; count
++)
125 ssread(void *buf
, size_t len
, FILE *stream
)
129 if ((outlen
= fread(buf
, len
, 1, stream
)) == 0)
136 ddt_hash_append(libzfs_handle_t
*hdl
, dedup_table_t
*ddt
, dedup_entry_t
**ddepp
,
137 zio_cksum_t
*cs
, uint64_t prop
, dataref_t
*dr
)
141 if (ddt
->cur_ddt_size
>= ddt
->max_ddt_size
) {
142 if (ddt
->ddt_full
== B_FALSE
) {
143 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
144 "Dedup table full. Deduplication will continue "
145 "with existing table entries"));
146 ddt
->ddt_full
= B_TRUE
;
151 if ((dde
= umem_cache_alloc(ddt
->ddecache
, UMEM_DEFAULT
))
153 assert(*ddepp
== NULL
);
154 dde
->dde_next
= NULL
;
155 dde
->dde_chksum
= *cs
;
156 dde
->dde_prop
= prop
;
159 ddt
->cur_ddt_size
+= sizeof (dedup_entry_t
);
165 * Using the specified dedup table, do a lookup for an entry with
166 * the checksum cs. If found, return the block's reference info
167 * in *dr. Otherwise, insert a new entry in the dedup table, using
168 * the reference information specified by *dr.
170 * return value: true - entry was found
171 * false - entry was not found
174 ddt_update(libzfs_handle_t
*hdl
, dedup_table_t
*ddt
, zio_cksum_t
*cs
,
175 uint64_t prop
, dataref_t
*dr
)
178 dedup_entry_t
**ddepp
;
180 hashcode
= BF64_GET(cs
->zc_word
[0], 0, ddt
->numhashbits
);
182 for (ddepp
= &(ddt
->dedup_hash_array
[hashcode
]); *ddepp
!= NULL
;
183 ddepp
= &((*ddepp
)->dde_next
)) {
184 if (ZIO_CHECKSUM_EQUAL(((*ddepp
)->dde_chksum
), *cs
) &&
185 (*ddepp
)->dde_prop
== prop
) {
186 *dr
= (*ddepp
)->dde_ref
;
190 ddt_hash_append(hdl
, ddt
, ddepp
, cs
, prop
, dr
);
195 dump_record(dmu_replay_record_t
*drr
, void *payload
, int payload_len
,
196 zio_cksum_t
*zc
, int outfd
)
198 ASSERT3U(offsetof(dmu_replay_record_t
, drr_u
.drr_checksum
.drr_checksum
),
199 ==, sizeof (dmu_replay_record_t
) - sizeof (zio_cksum_t
));
200 fletcher_4_incremental_native(drr
,
201 offsetof(dmu_replay_record_t
, drr_u
.drr_checksum
.drr_checksum
), zc
);
202 if (drr
->drr_type
!= DRR_BEGIN
) {
203 ASSERT(ZIO_CHECKSUM_IS_ZERO(&drr
->drr_u
.
204 drr_checksum
.drr_checksum
));
205 drr
->drr_u
.drr_checksum
.drr_checksum
= *zc
;
207 fletcher_4_incremental_native(&drr
->drr_u
.drr_checksum
.drr_checksum
,
208 sizeof (zio_cksum_t
), zc
);
209 if (write(outfd
, drr
, sizeof (*drr
)) == -1)
211 if (payload_len
!= 0) {
212 fletcher_4_incremental_native(payload
, payload_len
, zc
);
213 if (write(outfd
, payload
, payload_len
) == -1)
220 * This function is started in a separate thread when the dedup option
221 * has been requested. The main send thread determines the list of
222 * snapshots to be included in the send stream and makes the ioctl calls
223 * for each one. But instead of having the ioctl send the output to the
224 * the output fd specified by the caller of zfs_send()), the
225 * ioctl is told to direct the output to a pipe, which is read by the
226 * alternate thread running THIS function. This function does the
228 * 1. building a dedup table (the DDT)
229 * 2. doing checksums on each data block and inserting a record in the DDT
230 * 3. looking for matching checksums, and
231 * 4. sending a DRR_WRITE_BYREF record instead of a write record whenever
232 * a duplicate block is found.
233 * The output of this function then goes to the output fd requested
234 * by the caller of zfs_send().
239 dedup_arg_t
*dda
= arg
;
240 char *buf
= zfs_alloc(dda
->dedup_hdl
, SPA_MAXBLOCKSIZE
);
241 dmu_replay_record_t thedrr
= { 0 };
242 dmu_replay_record_t
*drr
= &thedrr
;
246 zio_cksum_t stream_cksum
;
247 uint64_t physmem
= sysconf(_SC_PHYS_PAGES
) * sysconf(_SC_PAGESIZE
);
251 MAX((physmem
* MAX_DDT_PHYSMEM_PERCENT
) / 100,
252 SMALLEST_POSSIBLE_MAX_DDT_MB
<< 20);
254 numbuckets
= ddt
.max_ddt_size
/ (sizeof (dedup_entry_t
));
257 * numbuckets must be a power of 2. Increase number to
258 * a power of 2 if necessary.
260 if (!ISP2(numbuckets
))
261 numbuckets
= 1 << high_order_bit(numbuckets
);
263 ddt
.dedup_hash_array
= calloc(numbuckets
, sizeof (dedup_entry_t
*));
264 ddt
.ddecache
= umem_cache_create("dde", sizeof (dedup_entry_t
), 0,
265 NULL
, NULL
, NULL
, NULL
, NULL
, 0);
266 ddt
.cur_ddt_size
= numbuckets
* sizeof (dedup_entry_t
*);
267 ddt
.numhashbits
= high_order_bit(numbuckets
) - 1;
268 ddt
.ddt_full
= B_FALSE
;
270 outfd
= dda
->outputfd
;
271 ofp
= fdopen(dda
->inputfd
, "r");
272 while (ssread(drr
, sizeof (*drr
), ofp
) != 0) {
274 switch (drr
->drr_type
) {
277 struct drr_begin
*drrb
= &drr
->drr_u
.drr_begin
;
280 ZIO_SET_CHECKSUM(&stream_cksum
, 0, 0, 0, 0);
282 ASSERT3U(drrb
->drr_magic
, ==, DMU_BACKUP_MAGIC
);
284 /* set the DEDUP feature flag for this stream */
285 fflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
286 fflags
|= (DMU_BACKUP_FEATURE_DEDUP
|
287 DMU_BACKUP_FEATURE_DEDUPPROPS
);
288 DMU_SET_FEATUREFLAGS(drrb
->drr_versioninfo
, fflags
);
290 if (drr
->drr_payloadlen
!= 0) {
291 sz
= drr
->drr_payloadlen
;
293 if (sz
> SPA_MAXBLOCKSIZE
) {
294 buf
= zfs_realloc(dda
->dedup_hdl
, buf
,
295 SPA_MAXBLOCKSIZE
, sz
);
297 (void) ssread(buf
, sz
, ofp
);
301 if (dump_record(drr
, buf
, sz
, &stream_cksum
,
309 struct drr_end
*drre
= &drr
->drr_u
.drr_end
;
310 /* use the recalculated checksum */
311 drre
->drr_checksum
= stream_cksum
;
312 if (dump_record(drr
, NULL
, 0, &stream_cksum
,
320 struct drr_object
*drro
= &drr
->drr_u
.drr_object
;
321 if (drro
->drr_bonuslen
> 0) {
323 P2ROUNDUP((uint64_t)drro
->drr_bonuslen
, 8),
326 if (dump_record(drr
, buf
,
327 P2ROUNDUP((uint64_t)drro
->drr_bonuslen
, 8),
328 &stream_cksum
, outfd
) != 0)
335 struct drr_spill
*drrs
= &drr
->drr_u
.drr_spill
;
336 (void) ssread(buf
, drrs
->drr_length
, ofp
);
337 if (dump_record(drr
, buf
, drrs
->drr_length
,
338 &stream_cksum
, outfd
) != 0)
343 case DRR_FREEOBJECTS
:
345 if (dump_record(drr
, NULL
, 0, &stream_cksum
,
353 struct drr_write
*drrw
= &drr
->drr_u
.drr_write
;
355 uint64_t payload_size
;
357 payload_size
= DRR_WRITE_PAYLOAD_SIZE(drrw
);
358 (void) ssread(buf
, payload_size
, ofp
);
361 * Use the existing checksum if it's dedup-capable,
362 * else calculate a SHA256 checksum for it.
365 if (ZIO_CHECKSUM_EQUAL(drrw
->drr_key
.ddk_cksum
,
367 !DRR_IS_DEDUP_CAPABLE(drrw
->drr_checksumflags
)) {
368 zio_cksum_t tmpsha256
;
370 zio_checksum_SHA256(buf
,
371 payload_size
, &tmpsha256
);
373 drrw
->drr_key
.ddk_cksum
.zc_word
[0] =
374 BE_64(tmpsha256
.zc_word
[0]);
375 drrw
->drr_key
.ddk_cksum
.zc_word
[1] =
376 BE_64(tmpsha256
.zc_word
[1]);
377 drrw
->drr_key
.ddk_cksum
.zc_word
[2] =
378 BE_64(tmpsha256
.zc_word
[2]);
379 drrw
->drr_key
.ddk_cksum
.zc_word
[3] =
380 BE_64(tmpsha256
.zc_word
[3]);
381 drrw
->drr_checksumtype
= ZIO_CHECKSUM_SHA256
;
382 drrw
->drr_checksumflags
= DRR_CHECKSUM_DEDUP
;
385 dataref
.ref_guid
= drrw
->drr_toguid
;
386 dataref
.ref_object
= drrw
->drr_object
;
387 dataref
.ref_offset
= drrw
->drr_offset
;
389 if (ddt_update(dda
->dedup_hdl
, &ddt
,
390 &drrw
->drr_key
.ddk_cksum
, drrw
->drr_key
.ddk_prop
,
392 dmu_replay_record_t wbr_drr
= {0};
393 struct drr_write_byref
*wbr_drrr
=
394 &wbr_drr
.drr_u
.drr_write_byref
;
396 /* block already present in stream */
397 wbr_drr
.drr_type
= DRR_WRITE_BYREF
;
399 wbr_drrr
->drr_object
= drrw
->drr_object
;
400 wbr_drrr
->drr_offset
= drrw
->drr_offset
;
401 wbr_drrr
->drr_length
= drrw
->drr_logical_size
;
402 wbr_drrr
->drr_toguid
= drrw
->drr_toguid
;
403 wbr_drrr
->drr_refguid
= dataref
.ref_guid
;
404 wbr_drrr
->drr_refobject
=
406 wbr_drrr
->drr_refoffset
=
409 wbr_drrr
->drr_checksumtype
=
410 drrw
->drr_checksumtype
;
411 wbr_drrr
->drr_checksumflags
=
412 drrw
->drr_checksumtype
;
413 wbr_drrr
->drr_key
.ddk_cksum
=
414 drrw
->drr_key
.ddk_cksum
;
415 wbr_drrr
->drr_key
.ddk_prop
=
416 drrw
->drr_key
.ddk_prop
;
418 if (dump_record(&wbr_drr
, NULL
, 0,
419 &stream_cksum
, outfd
) != 0)
422 /* block not previously seen */
423 if (dump_record(drr
, buf
, payload_size
,
424 &stream_cksum
, outfd
) != 0)
430 case DRR_WRITE_EMBEDDED
:
432 struct drr_write_embedded
*drrwe
=
433 &drr
->drr_u
.drr_write_embedded
;
435 P2ROUNDUP((uint64_t)drrwe
->drr_psize
, 8), ofp
);
436 if (dump_record(drr
, buf
,
437 P2ROUNDUP((uint64_t)drrwe
->drr_psize
, 8),
438 &stream_cksum
, outfd
) != 0)
445 if (dump_record(drr
, NULL
, 0, &stream_cksum
,
452 (void) fprintf(stderr
, "INVALID record type 0x%x\n",
454 /* should never happen, so assert */
459 umem_cache_destroy(ddt
.ddecache
);
460 free(ddt
.dedup_hash_array
);
468 * Routines for dealing with the AVL tree of fs-nvlists
470 typedef struct fsavl_node
{
478 fsavl_compare(const void *arg1
, const void *arg2
)
480 const fsavl_node_t
*fn1
= (const fsavl_node_t
*)arg1
;
481 const fsavl_node_t
*fn2
= (const fsavl_node_t
*)arg2
;
483 return (AVL_CMP(fn1
->fn_guid
, fn2
->fn_guid
));
487 * Given the GUID of a snapshot, find its containing filesystem and
491 fsavl_find(avl_tree_t
*avl
, uint64_t snapguid
, char **snapname
)
493 fsavl_node_t fn_find
;
496 fn_find
.fn_guid
= snapguid
;
498 fn
= avl_find(avl
, &fn_find
, NULL
);
501 *snapname
= fn
->fn_snapname
;
502 return (fn
->fn_nvfs
);
508 fsavl_destroy(avl_tree_t
*avl
)
517 while ((fn
= avl_destroy_nodes(avl
, &cookie
)) != NULL
)
524 * Given an nvlist, produce an avl tree of snapshots, ordered by guid
527 fsavl_create(nvlist_t
*fss
)
530 nvpair_t
*fselem
= NULL
;
532 if ((fsavl
= malloc(sizeof (avl_tree_t
))) == NULL
)
535 avl_create(fsavl
, fsavl_compare
, sizeof (fsavl_node_t
),
536 offsetof(fsavl_node_t
, fn_node
));
538 while ((fselem
= nvlist_next_nvpair(fss
, fselem
)) != NULL
) {
539 nvlist_t
*nvfs
, *snaps
;
540 nvpair_t
*snapelem
= NULL
;
542 VERIFY(0 == nvpair_value_nvlist(fselem
, &nvfs
));
543 VERIFY(0 == nvlist_lookup_nvlist(nvfs
, "snaps", &snaps
));
546 nvlist_next_nvpair(snaps
, snapelem
)) != NULL
) {
550 VERIFY(0 == nvpair_value_uint64(snapelem
, &guid
));
551 if ((fn
= malloc(sizeof (fsavl_node_t
))) == NULL
) {
552 fsavl_destroy(fsavl
);
556 fn
->fn_snapname
= nvpair_name(snapelem
);
560 * Note: if there are multiple snaps with the
561 * same GUID, we ignore all but one.
563 if (avl_find(fsavl
, fn
, NULL
) == NULL
)
574 * Routines for dealing with the giant nvlist of fs-nvlists, etc.
576 typedef struct send_data
{
578 * assigned inside every recursive call,
579 * restored from *_save on return:
581 * guid of fromsnap snapshot in parent dataset
582 * txg of fromsnap snapshot in current dataset
583 * txg of tosnap snapshot in current dataset
586 uint64_t parent_fromsnap_guid
;
587 uint64_t fromsnap_txg
;
590 /* the nvlists get accumulated during depth-first traversal */
591 nvlist_t
*parent_snaps
;
595 /* send-receive configuration, does not change during traversal */
597 const char *fromsnap
;
605 * The header nvlist is of the following format:
608 * "fromsnap" -> string (if incremental)
612 * "name" -> string (full name; for debugging)
613 * "parentfromsnap" -> number (guid of fromsnap in parent)
615 * "props" -> { name -> value (only if set here) }
616 * "snaps" -> { name (lastname) -> number (guid) }
617 * "snapprops" -> { name (lastname) -> { name -> value } }
619 * "origin" -> number (guid) (if clone)
620 * "sent" -> boolean (not on-disk)
628 static void send_iterate_prop(zfs_handle_t
*zhp
, nvlist_t
*nv
);
631 send_iterate_snap(zfs_handle_t
*zhp
, void *arg
)
633 send_data_t
*sd
= arg
;
634 uint64_t guid
= zhp
->zfs_dmustats
.dds_guid
;
635 uint64_t txg
= zhp
->zfs_dmustats
.dds_creation_txg
;
638 boolean_t isfromsnap
, istosnap
, istosnapwithnofrom
;
640 snapname
= strrchr(zhp
->zfs_name
, '@')+1;
641 isfromsnap
= (sd
->fromsnap
!= NULL
&&
642 strcmp(sd
->fromsnap
, snapname
) == 0);
643 istosnap
= (sd
->tosnap
!= NULL
&& (strcmp(sd
->tosnap
, snapname
) == 0));
644 istosnapwithnofrom
= (istosnap
&& sd
->fromsnap
== NULL
);
646 if (sd
->tosnap_txg
!= 0 && txg
> sd
->tosnap_txg
) {
648 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
649 "skipping snapshot %s because it was created "
650 "after the destination snapshot (%s)\n"),
651 zhp
->zfs_name
, sd
->tosnap
);
657 VERIFY(0 == nvlist_add_uint64(sd
->parent_snaps
, snapname
, guid
));
659 * NB: if there is no fromsnap here (it's a newly created fs in
660 * an incremental replication), we will substitute the tosnap.
662 if (isfromsnap
|| (sd
->parent_fromsnap_guid
== 0 && istosnap
)) {
663 sd
->parent_fromsnap_guid
= guid
;
666 if (!sd
->recursive
) {
667 if (!sd
->seenfrom
&& isfromsnap
) {
668 sd
->seenfrom
= B_TRUE
;
673 if ((sd
->seento
|| !sd
->seenfrom
) && !istosnapwithnofrom
) {
682 VERIFY(0 == nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0));
683 send_iterate_prop(zhp
, nv
);
684 VERIFY(0 == nvlist_add_nvlist(sd
->snapprops
, snapname
, nv
));
692 send_iterate_prop(zfs_handle_t
*zhp
, nvlist_t
*nv
)
694 nvpair_t
*elem
= NULL
;
696 while ((elem
= nvlist_next_nvpair(zhp
->zfs_props
, elem
)) != NULL
) {
697 char *propname
= nvpair_name(elem
);
698 zfs_prop_t prop
= zfs_name_to_prop(propname
);
701 if (!zfs_prop_user(propname
)) {
703 * Realistically, this should never happen. However,
704 * we want the ability to add DSL properties without
705 * needing to make incompatible version changes. We
706 * need to ignore unknown properties to allow older
707 * software to still send datasets containing these
708 * properties, with the unknown properties elided.
710 if (prop
== ZPROP_INVAL
)
713 if (zfs_prop_readonly(prop
))
717 verify(nvpair_value_nvlist(elem
, &propnv
) == 0);
718 if (prop
== ZFS_PROP_QUOTA
|| prop
== ZFS_PROP_RESERVATION
||
719 prop
== ZFS_PROP_REFQUOTA
||
720 prop
== ZFS_PROP_REFRESERVATION
) {
723 verify(nvlist_lookup_uint64(propnv
,
724 ZPROP_VALUE
, &value
) == 0);
725 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
)
728 * May have no source before SPA_VERSION_RECVD_PROPS,
729 * but is still modifiable.
731 if (nvlist_lookup_string(propnv
,
732 ZPROP_SOURCE
, &source
) == 0) {
733 if ((strcmp(source
, zhp
->zfs_name
) != 0) &&
735 ZPROP_SOURCE_VAL_RECVD
) != 0))
740 if (nvlist_lookup_string(propnv
,
741 ZPROP_SOURCE
, &source
) != 0)
743 if ((strcmp(source
, zhp
->zfs_name
) != 0) &&
744 (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) != 0))
748 if (zfs_prop_user(propname
) ||
749 zfs_prop_get_type(prop
) == PROP_TYPE_STRING
) {
751 verify(nvlist_lookup_string(propnv
,
752 ZPROP_VALUE
, &value
) == 0);
753 VERIFY(0 == nvlist_add_string(nv
, propname
, value
));
756 verify(nvlist_lookup_uint64(propnv
,
757 ZPROP_VALUE
, &value
) == 0);
758 VERIFY(0 == nvlist_add_uint64(nv
, propname
, value
));
764 * returns snapshot creation txg
765 * and returns 0 if the snapshot does not exist
768 get_snap_txg(libzfs_handle_t
*hdl
, const char *fs
, const char *snap
)
770 char name
[ZFS_MAX_DATASET_NAME_LEN
];
773 if (fs
== NULL
|| fs
[0] == '\0' || snap
== NULL
|| snap
[0] == '\0')
776 (void) snprintf(name
, sizeof (name
), "%s@%s", fs
, snap
);
777 if (zfs_dataset_exists(hdl
, name
, ZFS_TYPE_SNAPSHOT
)) {
778 zfs_handle_t
*zhp
= zfs_open(hdl
, name
, ZFS_TYPE_SNAPSHOT
);
780 txg
= zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
);
789 * recursively generate nvlists describing datasets. See comment
790 * for the data structure send_data_t above for description of contents
794 send_iterate_fs(zfs_handle_t
*zhp
, void *arg
)
796 send_data_t
*sd
= arg
;
799 uint64_t parent_fromsnap_guid_save
= sd
->parent_fromsnap_guid
;
800 uint64_t fromsnap_txg_save
= sd
->fromsnap_txg
;
801 uint64_t tosnap_txg_save
= sd
->tosnap_txg
;
802 uint64_t txg
= zhp
->zfs_dmustats
.dds_creation_txg
;
803 uint64_t guid
= zhp
->zfs_dmustats
.dds_guid
;
804 uint64_t fromsnap_txg
, tosnap_txg
;
807 fromsnap_txg
= get_snap_txg(zhp
->zfs_hdl
, zhp
->zfs_name
, sd
->fromsnap
);
808 if (fromsnap_txg
!= 0)
809 sd
->fromsnap_txg
= fromsnap_txg
;
811 tosnap_txg
= get_snap_txg(zhp
->zfs_hdl
, zhp
->zfs_name
, sd
->tosnap
);
813 sd
->tosnap_txg
= tosnap_txg
;
816 * on the send side, if the current dataset does not have tosnap,
817 * perform two additional checks:
819 * - skip sending the current dataset if it was created later than
821 * - return error if the current dataset was created earlier than
824 if (sd
->tosnap
!= NULL
&& tosnap_txg
== 0) {
825 if (sd
->tosnap_txg
!= 0 && txg
> sd
->tosnap_txg
) {
827 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
828 "skipping dataset %s: snapshot %s does "
829 "not exist\n"), zhp
->zfs_name
, sd
->tosnap
);
832 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
833 "cannot send %s@%s%s: snapshot %s@%s does not "
834 "exist\n"), sd
->fsname
, sd
->tosnap
, sd
->recursive
?
835 dgettext(TEXT_DOMAIN
, " recursively") : "",
836 zhp
->zfs_name
, sd
->tosnap
);
842 VERIFY(0 == nvlist_alloc(&nvfs
, NV_UNIQUE_NAME
, 0));
843 VERIFY(0 == nvlist_add_string(nvfs
, "name", zhp
->zfs_name
));
844 VERIFY(0 == nvlist_add_uint64(nvfs
, "parentfromsnap",
845 sd
->parent_fromsnap_guid
));
847 if (zhp
->zfs_dmustats
.dds_origin
[0]) {
848 zfs_handle_t
*origin
= zfs_open(zhp
->zfs_hdl
,
849 zhp
->zfs_dmustats
.dds_origin
, ZFS_TYPE_SNAPSHOT
);
850 if (origin
== NULL
) {
854 VERIFY(0 == nvlist_add_uint64(nvfs
, "origin",
855 origin
->zfs_dmustats
.dds_guid
));
858 /* iterate over props */
859 VERIFY(0 == nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0));
860 send_iterate_prop(zhp
, nv
);
861 VERIFY(0 == nvlist_add_nvlist(nvfs
, "props", nv
));
864 /* iterate over snaps, and set sd->parent_fromsnap_guid */
865 sd
->parent_fromsnap_guid
= 0;
866 VERIFY(0 == nvlist_alloc(&sd
->parent_snaps
, NV_UNIQUE_NAME
, 0));
867 VERIFY(0 == nvlist_alloc(&sd
->snapprops
, NV_UNIQUE_NAME
, 0));
868 (void) zfs_iter_snapshots_sorted(zhp
, send_iterate_snap
, sd
);
869 VERIFY(0 == nvlist_add_nvlist(nvfs
, "snaps", sd
->parent_snaps
));
870 VERIFY(0 == nvlist_add_nvlist(nvfs
, "snapprops", sd
->snapprops
));
871 nvlist_free(sd
->parent_snaps
);
872 nvlist_free(sd
->snapprops
);
874 /* add this fs to nvlist */
875 (void) snprintf(guidstring
, sizeof (guidstring
),
876 "0x%llx", (longlong_t
)guid
);
877 VERIFY(0 == nvlist_add_nvlist(sd
->fss
, guidstring
, nvfs
));
880 /* iterate over children */
882 rv
= zfs_iter_filesystems(zhp
, send_iterate_fs
, sd
);
885 sd
->parent_fromsnap_guid
= parent_fromsnap_guid_save
;
886 sd
->fromsnap_txg
= fromsnap_txg_save
;
887 sd
->tosnap_txg
= tosnap_txg_save
;
894 gather_nvlist(libzfs_handle_t
*hdl
, const char *fsname
, const char *fromsnap
,
895 const char *tosnap
, boolean_t recursive
, boolean_t verbose
,
896 nvlist_t
**nvlp
, avl_tree_t
**avlp
)
899 send_data_t sd
= { 0 };
902 zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
904 return (EZFS_BADTYPE
);
906 VERIFY(0 == nvlist_alloc(&sd
.fss
, NV_UNIQUE_NAME
, 0));
908 sd
.fromsnap
= fromsnap
;
910 sd
.recursive
= recursive
;
911 sd
.verbose
= verbose
;
913 if ((error
= send_iterate_fs(zhp
, &sd
)) != 0) {
921 if (avlp
!= NULL
&& (*avlp
= fsavl_create(sd
.fss
)) == NULL
) {
932 * Routines specific to "zfs send"
934 typedef struct send_dump_data
{
935 /* these are all just the short snapname (the part after the @) */
936 const char *fromsnap
;
938 char prevsnap
[ZFS_MAX_DATASET_NAME_LEN
];
939 uint64_t prevsnap_obj
;
940 boolean_t seenfrom
, seento
, replicate
, doall
, fromorigin
;
941 boolean_t verbose
, dryrun
, parsable
, progress
, embed_data
, std_out
;
942 boolean_t large_block
, compress
;
948 snapfilter_cb_t
*filter_cb
;
951 char holdtag
[ZFS_MAX_DATASET_NAME_LEN
];
957 estimate_ioctl(zfs_handle_t
*zhp
, uint64_t fromsnap_obj
,
958 boolean_t fromorigin
, enum lzc_send_flags flags
, uint64_t *sizep
)
960 zfs_cmd_t zc
= {"\0"};
961 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
963 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
964 assert(fromsnap_obj
== 0 || !fromorigin
);
966 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
967 zc
.zc_obj
= fromorigin
;
968 zc
.zc_sendobj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
969 zc
.zc_fromobj
= fromsnap_obj
;
970 zc
.zc_guid
= 1; /* estimate flag */
973 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SEND
, &zc
) != 0) {
975 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
976 "warning: cannot estimate space for '%s'"), zhp
->zfs_name
);
980 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
981 "not an earlier snapshot from the same fs"));
982 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
985 if (zfs_dataset_exists(hdl
, zc
.zc_name
,
986 ZFS_TYPE_SNAPSHOT
)) {
987 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
988 "incremental source (@%s) does not exist"),
991 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
1004 zfs_error_aux(hdl
, strerror(errno
));
1005 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
1008 return (zfs_standard_error(hdl
, errno
, errbuf
));
1012 *sizep
= zc
.zc_objset_type
;
1018 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
1019 * NULL) to the file descriptor specified by outfd.
1022 dump_ioctl(zfs_handle_t
*zhp
, const char *fromsnap
, uint64_t fromsnap_obj
,
1023 boolean_t fromorigin
, int outfd
, enum lzc_send_flags flags
,
1026 zfs_cmd_t zc
= {"\0"};
1027 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1030 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
1031 assert(fromsnap_obj
== 0 || !fromorigin
);
1033 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1034 zc
.zc_cookie
= outfd
;
1035 zc
.zc_obj
= fromorigin
;
1036 zc
.zc_sendobj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
1037 zc
.zc_fromobj
= fromsnap_obj
;
1038 zc
.zc_flags
= flags
;
1040 VERIFY(0 == nvlist_alloc(&thisdbg
, NV_UNIQUE_NAME
, 0));
1041 if (fromsnap
&& fromsnap
[0] != '\0') {
1042 VERIFY(0 == nvlist_add_string(thisdbg
,
1043 "fromsnap", fromsnap
));
1046 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SEND
, &zc
) != 0) {
1048 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1049 "warning: cannot send '%s'"), zhp
->zfs_name
);
1051 VERIFY(0 == nvlist_add_uint64(thisdbg
, "error", errno
));
1053 VERIFY(0 == nvlist_add_nvlist(debugnv
,
1054 zhp
->zfs_name
, thisdbg
));
1056 nvlist_free(thisdbg
);
1060 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1061 "not an earlier snapshot from the same fs"));
1062 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
1065 if (zfs_dataset_exists(hdl
, zc
.zc_name
,
1066 ZFS_TYPE_SNAPSHOT
)) {
1067 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1068 "incremental source (@%s) does not exist"),
1071 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
1084 zfs_error_aux(hdl
, strerror(errno
));
1085 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
1088 return (zfs_standard_error(hdl
, errno
, errbuf
));
1093 VERIFY(0 == nvlist_add_nvlist(debugnv
, zhp
->zfs_name
, thisdbg
));
1094 nvlist_free(thisdbg
);
1100 gather_holds(zfs_handle_t
*zhp
, send_dump_data_t
*sdd
)
1102 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
1105 * zfs_send() only sets snapholds for sends that need them,
1106 * e.g. replication and doall.
1108 if (sdd
->snapholds
== NULL
)
1111 fnvlist_add_string(sdd
->snapholds
, zhp
->zfs_name
, sdd
->holdtag
);
1115 send_progress_thread(void *arg
)
1117 progress_arg_t
*pa
= arg
;
1118 zfs_cmd_t zc
= {"\0"};
1119 zfs_handle_t
*zhp
= pa
->pa_zhp
;
1120 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1121 unsigned long long bytes
;
1126 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1128 if (!pa
->pa_parsable
)
1129 (void) fprintf(stderr
, "TIME SENT SNAPSHOT\n");
1132 * Print the progress from ZFS_IOC_SEND_PROGRESS every second.
1137 zc
.zc_cookie
= pa
->pa_fd
;
1138 if (zfs_ioctl(hdl
, ZFS_IOC_SEND_PROGRESS
, &zc
) != 0)
1139 return ((void *)-1);
1143 bytes
= zc
.zc_cookie
;
1145 if (pa
->pa_parsable
) {
1146 (void) fprintf(stderr
, "%02d:%02d:%02d\t%llu\t%s\n",
1147 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
1148 bytes
, zhp
->zfs_name
);
1150 zfs_nicenum(bytes
, buf
, sizeof (buf
));
1151 (void) fprintf(stderr
, "%02d:%02d:%02d %5s %s\n",
1152 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
1153 buf
, zhp
->zfs_name
);
1159 send_print_verbose(FILE *fout
, const char *tosnap
, const char *fromsnap
,
1160 uint64_t size
, boolean_t parsable
)
1163 if (fromsnap
!= NULL
) {
1164 (void) fprintf(fout
, "incremental\t%s\t%s",
1167 (void) fprintf(fout
, "full\t%s",
1171 if (fromsnap
!= NULL
) {
1172 if (strchr(fromsnap
, '@') == NULL
&&
1173 strchr(fromsnap
, '#') == NULL
) {
1174 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1175 "send from @%s to %s"),
1178 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1179 "send from %s to %s"),
1183 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1191 (void) fprintf(fout
, "\t%llu",
1195 zfs_nicenum(size
, buf
, sizeof (buf
));
1196 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1197 " estimated size is %s"), buf
);
1200 (void) fprintf(fout
, "\n");
1204 dump_snapshot(zfs_handle_t
*zhp
, void *arg
)
1206 send_dump_data_t
*sdd
= arg
;
1207 progress_arg_t pa
= { 0 };
1210 enum lzc_send_flags flags
= 0;
1212 boolean_t isfromsnap
, istosnap
, fromorigin
;
1213 boolean_t exclude
= B_FALSE
;
1214 FILE *fout
= sdd
->std_out
? stdout
: stderr
;
1217 thissnap
= strchr(zhp
->zfs_name
, '@') + 1;
1218 isfromsnap
= (sdd
->fromsnap
!= NULL
&&
1219 strcmp(sdd
->fromsnap
, thissnap
) == 0);
1221 if (!sdd
->seenfrom
&& isfromsnap
) {
1222 gather_holds(zhp
, sdd
);
1223 sdd
->seenfrom
= B_TRUE
;
1224 (void) strcpy(sdd
->prevsnap
, thissnap
);
1225 sdd
->prevsnap_obj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
1230 if (sdd
->seento
|| !sdd
->seenfrom
) {
1235 istosnap
= (strcmp(sdd
->tosnap
, thissnap
) == 0);
1237 sdd
->seento
= B_TRUE
;
1239 if (sdd
->large_block
)
1240 flags
|= LZC_SEND_FLAG_LARGE_BLOCK
;
1241 if (sdd
->embed_data
)
1242 flags
|= LZC_SEND_FLAG_EMBED_DATA
;
1244 flags
|= LZC_SEND_FLAG_COMPRESS
;
1246 if (!sdd
->doall
&& !isfromsnap
&& !istosnap
) {
1247 if (sdd
->replicate
) {
1249 nvlist_t
*snapprops
;
1251 * Filter out all intermediate snapshots except origin
1252 * snapshots needed to replicate clones.
1254 nvlist_t
*nvfs
= fsavl_find(sdd
->fsavl
,
1255 zhp
->zfs_dmustats
.dds_guid
, &snapname
);
1257 VERIFY(0 == nvlist_lookup_nvlist(nvfs
,
1258 "snapprops", &snapprops
));
1259 VERIFY(0 == nvlist_lookup_nvlist(snapprops
,
1260 thissnap
, &snapprops
));
1261 exclude
= !nvlist_exists(snapprops
, "is_clone_origin");
1268 * If a filter function exists, call it to determine whether
1269 * this snapshot will be sent.
1271 if (exclude
|| (sdd
->filter_cb
!= NULL
&&
1272 sdd
->filter_cb(zhp
, sdd
->filter_cb_arg
) == B_FALSE
)) {
1274 * This snapshot is filtered out. Don't send it, and don't
1275 * set prevsnap_obj, so it will be as if this snapshot didn't
1276 * exist, and the next accepted snapshot will be sent as
1277 * an incremental from the last accepted one, or as the
1278 * first (and full) snapshot in the case of a replication,
1279 * non-incremental send.
1285 gather_holds(zhp
, sdd
);
1286 fromorigin
= sdd
->prevsnap
[0] == '\0' &&
1287 (sdd
->fromorigin
|| sdd
->replicate
);
1291 (void) estimate_ioctl(zhp
, sdd
->prevsnap_obj
,
1292 fromorigin
, flags
, &size
);
1294 send_print_verbose(fout
, zhp
->zfs_name
,
1295 sdd
->prevsnap
[0] ? sdd
->prevsnap
: NULL
,
1296 size
, sdd
->parsable
);
1302 * If progress reporting is requested, spawn a new thread to
1303 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1305 if (sdd
->progress
) {
1307 pa
.pa_fd
= sdd
->outfd
;
1308 pa
.pa_parsable
= sdd
->parsable
;
1310 if ((err
= pthread_create(&tid
, NULL
,
1311 send_progress_thread
, &pa
))) {
1317 err
= dump_ioctl(zhp
, sdd
->prevsnap
, sdd
->prevsnap_obj
,
1318 fromorigin
, sdd
->outfd
, flags
, sdd
->debugnv
);
1320 if (sdd
->progress
) {
1321 (void) pthread_cancel(tid
);
1322 (void) pthread_join(tid
, NULL
);
1326 (void) strcpy(sdd
->prevsnap
, thissnap
);
1327 sdd
->prevsnap_obj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
1333 dump_filesystem(zfs_handle_t
*zhp
, void *arg
)
1336 send_dump_data_t
*sdd
= arg
;
1337 boolean_t missingfrom
= B_FALSE
;
1338 zfs_cmd_t zc
= {"\0"};
1340 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
), "%s@%s",
1341 zhp
->zfs_name
, sdd
->tosnap
);
1342 if (ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0) {
1343 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1344 "WARNING: could not send %s@%s: does not exist\n"),
1345 zhp
->zfs_name
, sdd
->tosnap
);
1350 if (sdd
->replicate
&& sdd
->fromsnap
) {
1352 * If this fs does not have fromsnap, and we're doing
1353 * recursive, we need to send a full stream from the
1354 * beginning (or an incremental from the origin if this
1355 * is a clone). If we're doing non-recursive, then let
1356 * them get the error.
1358 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
), "%s@%s",
1359 zhp
->zfs_name
, sdd
->fromsnap
);
1360 if (ioctl(zhp
->zfs_hdl
->libzfs_fd
,
1361 ZFS_IOC_OBJSET_STATS
, &zc
) != 0) {
1362 missingfrom
= B_TRUE
;
1366 sdd
->seenfrom
= sdd
->seento
= sdd
->prevsnap
[0] = 0;
1367 sdd
->prevsnap_obj
= 0;
1368 if (sdd
->fromsnap
== NULL
|| missingfrom
)
1369 sdd
->seenfrom
= B_TRUE
;
1371 rv
= zfs_iter_snapshots_sorted(zhp
, dump_snapshot
, arg
);
1372 if (!sdd
->seenfrom
) {
1373 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1374 "WARNING: could not send %s@%s:\n"
1375 "incremental source (%s@%s) does not exist\n"),
1376 zhp
->zfs_name
, sdd
->tosnap
,
1377 zhp
->zfs_name
, sdd
->fromsnap
);
1379 } else if (!sdd
->seento
) {
1380 if (sdd
->fromsnap
) {
1381 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1382 "WARNING: could not send %s@%s:\n"
1383 "incremental source (%s@%s) "
1384 "is not earlier than it\n"),
1385 zhp
->zfs_name
, sdd
->tosnap
,
1386 zhp
->zfs_name
, sdd
->fromsnap
);
1388 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1390 "could not send %s@%s: does not exist\n"),
1391 zhp
->zfs_name
, sdd
->tosnap
);
1400 dump_filesystems(zfs_handle_t
*rzhp
, void *arg
)
1402 send_dump_data_t
*sdd
= arg
;
1404 boolean_t needagain
, progress
;
1406 if (!sdd
->replicate
)
1407 return (dump_filesystem(rzhp
, sdd
));
1409 /* Mark the clone origin snapshots. */
1410 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1411 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1413 uint64_t origin_guid
= 0;
1415 VERIFY(0 == nvpair_value_nvlist(fspair
, &nvfs
));
1416 (void) nvlist_lookup_uint64(nvfs
, "origin", &origin_guid
);
1417 if (origin_guid
!= 0) {
1419 nvlist_t
*origin_nv
= fsavl_find(sdd
->fsavl
,
1420 origin_guid
, &snapname
);
1421 if (origin_nv
!= NULL
) {
1422 nvlist_t
*snapprops
;
1423 VERIFY(0 == nvlist_lookup_nvlist(origin_nv
,
1424 "snapprops", &snapprops
));
1425 VERIFY(0 == nvlist_lookup_nvlist(snapprops
,
1426 snapname
, &snapprops
));
1427 VERIFY(0 == nvlist_add_boolean(
1428 snapprops
, "is_clone_origin"));
1433 needagain
= progress
= B_FALSE
;
1434 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1435 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1436 nvlist_t
*fslist
, *parent_nv
;
1440 uint64_t origin_guid
= 0;
1441 uint64_t parent_guid
= 0;
1443 VERIFY(nvpair_value_nvlist(fspair
, &fslist
) == 0);
1444 if (nvlist_lookup_boolean(fslist
, "sent") == 0)
1447 VERIFY(nvlist_lookup_string(fslist
, "name", &fsname
) == 0);
1448 (void) nvlist_lookup_uint64(fslist
, "origin", &origin_guid
);
1449 (void) nvlist_lookup_uint64(fslist
, "parentfromsnap",
1452 if (parent_guid
!= 0) {
1453 parent_nv
= fsavl_find(sdd
->fsavl
, parent_guid
, NULL
);
1454 if (!nvlist_exists(parent_nv
, "sent")) {
1455 /* parent has not been sent; skip this one */
1461 if (origin_guid
!= 0) {
1462 nvlist_t
*origin_nv
= fsavl_find(sdd
->fsavl
,
1464 if (origin_nv
!= NULL
&&
1465 !nvlist_exists(origin_nv
, "sent")) {
1467 * origin has not been sent yet;
1475 zhp
= zfs_open(rzhp
->zfs_hdl
, fsname
, ZFS_TYPE_DATASET
);
1478 err
= dump_filesystem(zhp
, sdd
);
1479 VERIFY(nvlist_add_boolean(fslist
, "sent") == 0);
1490 /* clean out the sent flags in case we reuse this fss */
1491 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1492 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1495 VERIFY(nvpair_value_nvlist(fspair
, &fslist
) == 0);
1496 (void) nvlist_remove_all(fslist
, "sent");
1503 zfs_send_resume_token_to_nvlist(libzfs_handle_t
*hdl
, const char *token
)
1505 unsigned int version
;
1507 unsigned long long checksum
, packed_len
;
1510 * Decode token header, which is:
1511 * <token version>-<checksum of payload>-<uncompressed payload length>
1512 * Note that the only supported token version is 1.
1514 nread
= sscanf(token
, "%u-%llx-%llx-",
1515 &version
, &checksum
, &packed_len
);
1517 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1518 "resume token is corrupt (invalid format)"));
1522 if (version
!= ZFS_SEND_RESUME_TOKEN_VERSION
) {
1523 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1524 "resume token is corrupt (invalid version %u)"),
1529 /* convert hexadecimal representation to binary */
1530 token
= strrchr(token
, '-') + 1;
1531 int len
= strlen(token
) / 2;
1532 unsigned char *compressed
= zfs_alloc(hdl
, len
);
1533 for (i
= 0; i
< len
; i
++) {
1534 nread
= sscanf(token
+ i
* 2, "%2hhx", compressed
+ i
);
1537 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1538 "resume token is corrupt "
1539 "(payload is not hex-encoded)"));
1544 /* verify checksum */
1546 fletcher_4_native_varsize(compressed
, len
, &cksum
);
1547 if (cksum
.zc_word
[0] != checksum
) {
1549 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1550 "resume token is corrupt (incorrect checksum)"));
1555 void *packed
= zfs_alloc(hdl
, packed_len
);
1556 uLongf packed_len_long
= packed_len
;
1557 if (uncompress(packed
, &packed_len_long
, compressed
, len
) != Z_OK
||
1558 packed_len_long
!= packed_len
) {
1561 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1562 "resume token is corrupt (decompression failed)"));
1568 int error
= nvlist_unpack(packed
, packed_len
, &nv
, KM_SLEEP
);
1572 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1573 "resume token is corrupt (nvlist_unpack failed)"));
1580 zfs_send_resume(libzfs_handle_t
*hdl
, sendflags_t
*flags
, int outfd
,
1581 const char *resume_token
)
1585 char *fromname
= NULL
;
1586 uint64_t resumeobj
, resumeoff
, toguid
, fromguid
, bytes
;
1589 char name
[ZFS_MAX_DATASET_NAME_LEN
];
1590 enum lzc_send_flags lzc_flags
= 0;
1592 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1593 "cannot resume send"));
1595 nvlist_t
*resume_nvl
=
1596 zfs_send_resume_token_to_nvlist(hdl
, resume_token
);
1597 if (resume_nvl
== NULL
) {
1599 * zfs_error_aux has already been set by
1600 * zfs_send_resume_token_to_nvlist
1602 return (zfs_error(hdl
, EZFS_FAULT
, errbuf
));
1604 if (flags
->verbose
) {
1605 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1606 "resume token contents:\n"));
1607 nvlist_print(stderr
, resume_nvl
);
1610 if (nvlist_lookup_string(resume_nvl
, "toname", &toname
) != 0 ||
1611 nvlist_lookup_uint64(resume_nvl
, "object", &resumeobj
) != 0 ||
1612 nvlist_lookup_uint64(resume_nvl
, "offset", &resumeoff
) != 0 ||
1613 nvlist_lookup_uint64(resume_nvl
, "bytes", &bytes
) != 0 ||
1614 nvlist_lookup_uint64(resume_nvl
, "toguid", &toguid
) != 0) {
1615 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1616 "resume token is corrupt"));
1617 return (zfs_error(hdl
, EZFS_FAULT
, errbuf
));
1620 (void) nvlist_lookup_uint64(resume_nvl
, "fromguid", &fromguid
);
1622 if (flags
->largeblock
|| nvlist_exists(resume_nvl
, "largeblockok"))
1623 lzc_flags
|= LZC_SEND_FLAG_LARGE_BLOCK
;
1624 if (flags
->embed_data
|| nvlist_exists(resume_nvl
, "embedok"))
1625 lzc_flags
|= LZC_SEND_FLAG_EMBED_DATA
;
1626 if (flags
->compress
|| nvlist_exists(resume_nvl
, "compressok"))
1627 lzc_flags
|= LZC_SEND_FLAG_COMPRESS
;
1629 if (guid_to_name(hdl
, toname
, toguid
, B_FALSE
, name
) != 0) {
1630 if (zfs_dataset_exists(hdl
, toname
, ZFS_TYPE_DATASET
)) {
1631 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1632 "'%s' is no longer the same snapshot used in "
1633 "the initial send"), toname
);
1635 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1636 "'%s' used in the initial send no longer exists"),
1639 return (zfs_error(hdl
, EZFS_BADPATH
, errbuf
));
1641 zhp
= zfs_open(hdl
, name
, ZFS_TYPE_DATASET
);
1643 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1644 "unable to access '%s'"), name
);
1645 return (zfs_error(hdl
, EZFS_BADPATH
, errbuf
));
1648 if (fromguid
!= 0) {
1649 if (guid_to_name(hdl
, toname
, fromguid
, B_TRUE
, name
) != 0) {
1650 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1651 "incremental source %#llx no longer exists"),
1652 (longlong_t
)fromguid
);
1653 return (zfs_error(hdl
, EZFS_BADPATH
, errbuf
));
1658 if (flags
->verbose
) {
1660 error
= lzc_send_space(zhp
->zfs_name
, fromname
,
1663 size
= MAX(0, (int64_t)(size
- bytes
));
1664 send_print_verbose(stderr
, zhp
->zfs_name
, fromname
,
1665 size
, flags
->parsable
);
1668 if (!flags
->dryrun
) {
1669 progress_arg_t pa
= { 0 };
1672 * If progress reporting is requested, spawn a new thread to
1673 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1675 if (flags
->progress
) {
1678 pa
.pa_parsable
= flags
->parsable
;
1680 error
= pthread_create(&tid
, NULL
,
1681 send_progress_thread
, &pa
);
1688 error
= lzc_send_resume(zhp
->zfs_name
, fromname
, outfd
,
1689 lzc_flags
, resumeobj
, resumeoff
);
1691 if (flags
->progress
) {
1692 (void) pthread_cancel(tid
);
1693 (void) pthread_join(tid
, NULL
);
1697 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1698 "warning: cannot send '%s'"), zhp
->zfs_name
);
1718 zfs_error_aux(hdl
, strerror(errno
));
1719 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
1722 return (zfs_standard_error(hdl
, errno
, errbuf
));
1733 * Generate a send stream for the dataset identified by the argument zhp.
1735 * The content of the send stream is the snapshot identified by
1736 * 'tosnap'. Incremental streams are requested in two ways:
1737 * - from the snapshot identified by "fromsnap" (if non-null) or
1738 * - from the origin of the dataset identified by zhp, which must
1739 * be a clone. In this case, "fromsnap" is null and "fromorigin"
1742 * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
1743 * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
1744 * if "replicate" is set. If "doall" is set, dump all the intermediate
1745 * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
1746 * case too. If "props" is set, send properties.
1749 zfs_send(zfs_handle_t
*zhp
, const char *fromsnap
, const char *tosnap
,
1750 sendflags_t
*flags
, int outfd
, snapfilter_cb_t filter_func
,
1751 void *cb_arg
, nvlist_t
**debugnvp
)
1754 send_dump_data_t sdd
= { 0 };
1756 nvlist_t
*fss
= NULL
;
1757 avl_tree_t
*fsavl
= NULL
;
1758 static uint64_t holdseq
;
1762 dedup_arg_t dda
= { 0 };
1763 int featureflags
= 0;
1766 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1767 "cannot send '%s'"), zhp
->zfs_name
);
1769 if (fromsnap
&& fromsnap
[0] == '\0') {
1770 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1771 "zero-length incremental source"));
1772 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
1775 if (zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
) {
1777 version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1778 if (version
>= ZPL_VERSION_SA
) {
1779 featureflags
|= DMU_BACKUP_FEATURE_SA_SPILL
;
1783 if (flags
->dedup
&& !flags
->dryrun
) {
1784 featureflags
|= (DMU_BACKUP_FEATURE_DEDUP
|
1785 DMU_BACKUP_FEATURE_DEDUPPROPS
);
1786 if ((err
= socketpair(AF_UNIX
, SOCK_STREAM
, 0, pipefd
))) {
1787 zfs_error_aux(zhp
->zfs_hdl
, strerror(errno
));
1788 return (zfs_error(zhp
->zfs_hdl
, EZFS_PIPEFAILED
,
1791 dda
.outputfd
= outfd
;
1792 dda
.inputfd
= pipefd
[1];
1793 dda
.dedup_hdl
= zhp
->zfs_hdl
;
1794 if ((err
= pthread_create(&tid
, NULL
, cksummer
, &dda
))) {
1795 (void) close(pipefd
[0]);
1796 (void) close(pipefd
[1]);
1797 zfs_error_aux(zhp
->zfs_hdl
, strerror(errno
));
1798 return (zfs_error(zhp
->zfs_hdl
,
1799 EZFS_THREADCREATEFAILED
, errbuf
));
1803 if (flags
->replicate
|| flags
->doall
|| flags
->props
) {
1804 dmu_replay_record_t drr
= { 0 };
1805 char *packbuf
= NULL
;
1809 ZIO_SET_CHECKSUM(&zc
, 0, 0, 0, 0);
1811 if (flags
->replicate
|| flags
->props
) {
1814 VERIFY(0 == nvlist_alloc(&hdrnv
, NV_UNIQUE_NAME
, 0));
1816 VERIFY(0 == nvlist_add_string(hdrnv
,
1817 "fromsnap", fromsnap
));
1819 VERIFY(0 == nvlist_add_string(hdrnv
, "tosnap", tosnap
));
1820 if (!flags
->replicate
) {
1821 VERIFY(0 == nvlist_add_boolean(hdrnv
,
1825 err
= gather_nvlist(zhp
->zfs_hdl
, zhp
->zfs_name
,
1826 fromsnap
, tosnap
, flags
->replicate
, flags
->verbose
,
1830 VERIFY(0 == nvlist_add_nvlist(hdrnv
, "fss", fss
));
1831 err
= nvlist_pack(hdrnv
, &packbuf
, &buflen
,
1841 if (!flags
->dryrun
) {
1842 /* write first begin record */
1843 drr
.drr_type
= DRR_BEGIN
;
1844 drr
.drr_u
.drr_begin
.drr_magic
= DMU_BACKUP_MAGIC
;
1845 DMU_SET_STREAM_HDRTYPE(drr
.drr_u
.drr_begin
.
1846 drr_versioninfo
, DMU_COMPOUNDSTREAM
);
1847 DMU_SET_FEATUREFLAGS(drr
.drr_u
.drr_begin
.
1848 drr_versioninfo
, featureflags
);
1849 (void) snprintf(drr
.drr_u
.drr_begin
.drr_toname
,
1850 sizeof (drr
.drr_u
.drr_begin
.drr_toname
),
1851 "%s@%s", zhp
->zfs_name
, tosnap
);
1852 drr
.drr_payloadlen
= buflen
;
1854 err
= dump_record(&drr
, packbuf
, buflen
, &zc
, outfd
);
1859 /* write end record */
1860 bzero(&drr
, sizeof (drr
));
1861 drr
.drr_type
= DRR_END
;
1862 drr
.drr_u
.drr_end
.drr_checksum
= zc
;
1863 err
= write(outfd
, &drr
, sizeof (drr
));
1873 /* dump each stream */
1874 sdd
.fromsnap
= fromsnap
;
1875 sdd
.tosnap
= tosnap
;
1877 sdd
.outfd
= pipefd
[0];
1880 sdd
.replicate
= flags
->replicate
;
1881 sdd
.doall
= flags
->doall
;
1882 sdd
.fromorigin
= flags
->fromorigin
;
1885 sdd
.verbose
= flags
->verbose
;
1886 sdd
.parsable
= flags
->parsable
;
1887 sdd
.progress
= flags
->progress
;
1888 sdd
.dryrun
= flags
->dryrun
;
1889 sdd
.large_block
= flags
->largeblock
;
1890 sdd
.embed_data
= flags
->embed_data
;
1891 sdd
.compress
= flags
->compress
;
1892 sdd
.filter_cb
= filter_func
;
1893 sdd
.filter_cb_arg
= cb_arg
;
1895 sdd
.debugnv
= *debugnvp
;
1896 if (sdd
.verbose
&& sdd
.dryrun
)
1897 sdd
.std_out
= B_TRUE
;
1898 fout
= sdd
.std_out
? stdout
: stderr
;
1901 * Some flags require that we place user holds on the datasets that are
1902 * being sent so they don't get destroyed during the send. We can skip
1903 * this step if the pool is imported read-only since the datasets cannot
1906 if (!flags
->dryrun
&& !zpool_get_prop_int(zfs_get_pool_handle(zhp
),
1907 ZPOOL_PROP_READONLY
, NULL
) &&
1908 zfs_spa_version(zhp
, &spa_version
) == 0 &&
1909 spa_version
>= SPA_VERSION_USERREFS
&&
1910 (flags
->doall
|| flags
->replicate
)) {
1912 (void) snprintf(sdd
.holdtag
, sizeof (sdd
.holdtag
),
1913 ".send-%d-%llu", getpid(), (u_longlong_t
)holdseq
);
1914 sdd
.cleanup_fd
= open(ZFS_DEV
, O_RDWR
);
1915 if (sdd
.cleanup_fd
< 0) {
1919 sdd
.snapholds
= fnvlist_alloc();
1921 sdd
.cleanup_fd
= -1;
1922 sdd
.snapholds
= NULL
;
1924 if (flags
->verbose
|| sdd
.snapholds
!= NULL
) {
1926 * Do a verbose no-op dry run to get all the verbose output
1927 * or to gather snapshot hold's before generating any data,
1928 * then do a non-verbose real run to generate the streams.
1930 sdd
.dryrun
= B_TRUE
;
1931 err
= dump_filesystems(zhp
, &sdd
);
1936 if (flags
->verbose
) {
1937 if (flags
->parsable
) {
1938 (void) fprintf(fout
, "size\t%llu\n",
1939 (longlong_t
)sdd
.size
);
1942 zfs_nicenum(sdd
.size
, buf
, sizeof (buf
));
1943 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1944 "total estimated size is %s\n"), buf
);
1948 /* Ensure no snaps found is treated as an error. */
1954 /* Skip the second run if dryrun was requested. */
1958 if (sdd
.snapholds
!= NULL
) {
1959 err
= zfs_hold_nvl(zhp
, sdd
.cleanup_fd
, sdd
.snapholds
);
1963 fnvlist_free(sdd
.snapholds
);
1964 sdd
.snapholds
= NULL
;
1967 sdd
.dryrun
= B_FALSE
;
1968 sdd
.verbose
= B_FALSE
;
1971 err
= dump_filesystems(zhp
, &sdd
);
1972 fsavl_destroy(fsavl
);
1975 /* Ensure no snaps found is treated as an error. */
1976 if (err
== 0 && !sdd
.seento
)
1981 (void) pthread_cancel(tid
);
1982 (void) close(pipefd
[0]);
1983 (void) pthread_join(tid
, NULL
);
1986 if (sdd
.cleanup_fd
!= -1) {
1987 VERIFY(0 == close(sdd
.cleanup_fd
));
1988 sdd
.cleanup_fd
= -1;
1991 if (!flags
->dryrun
&& (flags
->replicate
|| flags
->doall
||
1994 * write final end record. NB: want to do this even if
1995 * there was some error, because it might not be totally
1998 dmu_replay_record_t drr
= { 0 };
1999 drr
.drr_type
= DRR_END
;
2000 if (write(outfd
, &drr
, sizeof (drr
)) == -1) {
2001 return (zfs_standard_error(zhp
->zfs_hdl
,
2006 return (err
|| sdd
.err
);
2009 err
= zfs_standard_error(zhp
->zfs_hdl
, err
, errbuf
);
2011 fsavl_destroy(fsavl
);
2013 fnvlist_free(sdd
.snapholds
);
2015 if (sdd
.cleanup_fd
!= -1)
2016 VERIFY(0 == close(sdd
.cleanup_fd
));
2018 (void) pthread_cancel(tid
);
2019 (void) close(pipefd
[0]);
2020 (void) pthread_join(tid
, NULL
);
2026 zfs_send_one(zfs_handle_t
*zhp
, const char *from
, int fd
,
2027 enum lzc_send_flags flags
)
2030 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
2033 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2034 "warning: cannot send '%s'"), zhp
->zfs_name
);
2036 err
= lzc_send(zhp
->zfs_name
, from
, fd
, flags
);
2040 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2041 "not an earlier snapshot from the same fs"));
2042 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
2046 if (lzc_exists(zhp
->zfs_name
)) {
2047 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2048 "incremental source (%s) does not exist"),
2051 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2054 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2055 "target is busy; if a filesystem, "
2056 "it must not be mounted"));
2057 return (zfs_error(hdl
, EZFS_BUSY
, errbuf
));
2070 zfs_error_aux(hdl
, strerror(errno
));
2071 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
2074 return (zfs_standard_error(hdl
, errno
, errbuf
));
2081 * Routines specific to "zfs recv"
2085 recv_read(libzfs_handle_t
*hdl
, int fd
, void *buf
, int ilen
,
2086 boolean_t byteswap
, zio_cksum_t
*zc
)
2092 assert(ilen
<= SPA_MAXBLOCKSIZE
);
2095 rv
= read(fd
, cp
, len
);
2100 if (rv
< 0 || len
!= 0) {
2101 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2102 "failed to read from stream"));
2103 return (zfs_error(hdl
, EZFS_BADSTREAM
, dgettext(TEXT_DOMAIN
,
2104 "cannot receive")));
2109 fletcher_4_incremental_byteswap(buf
, ilen
, zc
);
2111 fletcher_4_incremental_native(buf
, ilen
, zc
);
2117 recv_read_nvlist(libzfs_handle_t
*hdl
, int fd
, int len
, nvlist_t
**nvp
,
2118 boolean_t byteswap
, zio_cksum_t
*zc
)
2123 buf
= zfs_alloc(hdl
, len
);
2127 err
= recv_read(hdl
, fd
, buf
, len
, byteswap
, zc
);
2133 err
= nvlist_unpack(buf
, len
, nvp
, 0);
2136 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
2137 "stream (malformed nvlist)"));
2144 recv_rename(libzfs_handle_t
*hdl
, const char *name
, const char *tryname
,
2145 int baselen
, char *newname
, recvflags_t
*flags
)
2148 zfs_cmd_t zc
= {"\0"};
2150 prop_changelist_t
*clp
;
2153 zhp
= zfs_open(hdl
, name
, ZFS_TYPE_DATASET
);
2156 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
2157 flags
->force
? MS_FORCE
: 0);
2161 err
= changelist_prefix(clp
);
2165 zc
.zc_objset_type
= DMU_OST_ZFS
;
2166 (void) strlcpy(zc
.zc_name
, name
, sizeof (zc
.zc_name
));
2169 (void) strcpy(newname
, tryname
);
2171 (void) strlcpy(zc
.zc_value
, tryname
, sizeof (zc
.zc_value
));
2173 if (flags
->verbose
) {
2174 (void) printf("attempting rename %s to %s\n",
2175 zc
.zc_name
, zc
.zc_value
);
2177 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_RENAME
, &zc
);
2179 changelist_rename(clp
, name
, tryname
);
2184 if (err
!= 0 && strncmp(name
+ baselen
, "recv-", 5) != 0) {
2187 (void) snprintf(newname
, ZFS_MAX_DATASET_NAME_LEN
,
2188 "%.*srecv-%u-%u", baselen
, name
, getpid(), seq
);
2189 (void) strlcpy(zc
.zc_value
, newname
, sizeof (zc
.zc_value
));
2191 if (flags
->verbose
) {
2192 (void) printf("failed - trying rename %s to %s\n",
2193 zc
.zc_name
, zc
.zc_value
);
2195 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_RENAME
, &zc
);
2197 changelist_rename(clp
, name
, newname
);
2198 if (err
&& flags
->verbose
) {
2199 (void) printf("failed (%u) - "
2200 "will try again on next pass\n", errno
);
2203 } else if (flags
->verbose
) {
2205 (void) printf("success\n");
2207 (void) printf("failed (%u)\n", errno
);
2210 (void) changelist_postfix(clp
);
2211 changelist_free(clp
);
2217 recv_destroy(libzfs_handle_t
*hdl
, const char *name
, int baselen
,
2218 char *newname
, recvflags_t
*flags
)
2220 zfs_cmd_t zc
= {"\0"};
2222 prop_changelist_t
*clp
;
2224 boolean_t defer
= B_FALSE
;
2227 zhp
= zfs_open(hdl
, name
, ZFS_TYPE_DATASET
);
2230 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
2231 flags
->force
? MS_FORCE
: 0);
2232 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
&&
2233 zfs_spa_version(zhp
, &spa_version
) == 0 &&
2234 spa_version
>= SPA_VERSION_USERREFS
)
2239 err
= changelist_prefix(clp
);
2243 zc
.zc_objset_type
= DMU_OST_ZFS
;
2244 zc
.zc_defer_destroy
= defer
;
2245 (void) strlcpy(zc
.zc_name
, name
, sizeof (zc
.zc_name
));
2248 (void) printf("attempting destroy %s\n", zc
.zc_name
);
2249 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_DESTROY
, &zc
);
2252 (void) printf("success\n");
2253 changelist_remove(clp
, zc
.zc_name
);
2256 (void) changelist_postfix(clp
);
2257 changelist_free(clp
);
2260 * Deferred destroy might destroy the snapshot or only mark it to be
2261 * destroyed later, and it returns success in either case.
2263 if (err
!= 0 || (defer
&& zfs_dataset_exists(hdl
, name
,
2264 ZFS_TYPE_SNAPSHOT
))) {
2265 err
= recv_rename(hdl
, name
, NULL
, baselen
, newname
, flags
);
2271 typedef struct guid_to_name_data
{
2273 boolean_t bookmark_ok
;
2276 } guid_to_name_data_t
;
2279 guid_to_name_cb(zfs_handle_t
*zhp
, void *arg
)
2281 guid_to_name_data_t
*gtnd
= arg
;
2285 if (gtnd
->skip
!= NULL
&&
2286 (slash
= strrchr(zhp
->zfs_name
, '/')) != NULL
&&
2287 strcmp(slash
+ 1, gtnd
->skip
) == 0) {
2292 if (zfs_prop_get_int(zhp
, ZFS_PROP_GUID
) == gtnd
->guid
) {
2293 (void) strcpy(gtnd
->name
, zhp
->zfs_name
);
2298 err
= zfs_iter_children(zhp
, guid_to_name_cb
, gtnd
);
2299 if (err
!= EEXIST
&& gtnd
->bookmark_ok
)
2300 err
= zfs_iter_bookmarks(zhp
, guid_to_name_cb
, gtnd
);
2306 * Attempt to find the local dataset associated with this guid. In the case of
2307 * multiple matches, we attempt to find the "best" match by searching
2308 * progressively larger portions of the hierarchy. This allows one to send a
2309 * tree of datasets individually and guarantee that we will find the source
2310 * guid within that hierarchy, even if there are multiple matches elsewhere.
2313 guid_to_name(libzfs_handle_t
*hdl
, const char *parent
, uint64_t guid
,
2314 boolean_t bookmark_ok
, char *name
)
2316 char pname
[ZFS_MAX_DATASET_NAME_LEN
];
2317 guid_to_name_data_t gtnd
;
2320 gtnd
.bookmark_ok
= bookmark_ok
;
2325 * Search progressively larger portions of the hierarchy, starting
2326 * with the filesystem specified by 'parent'. This will
2327 * select the "most local" version of the origin snapshot in the case
2328 * that there are multiple matching snapshots in the system.
2330 (void) strlcpy(pname
, parent
, sizeof (pname
));
2331 char *cp
= strrchr(pname
, '@');
2333 cp
= strchr(pname
, '\0');
2334 for (; cp
!= NULL
; cp
= strrchr(pname
, '/')) {
2335 /* Chop off the last component and open the parent */
2337 zfs_handle_t
*zhp
= make_dataset_handle(hdl
, pname
);
2341 int err
= guid_to_name_cb(zfs_handle_dup(zhp
), >nd
);
2343 err
= zfs_iter_children(zhp
, guid_to_name_cb
, >nd
);
2344 if (err
!= EEXIST
&& bookmark_ok
)
2345 err
= zfs_iter_bookmarks(zhp
, guid_to_name_cb
, >nd
);
2351 * Remember the last portion of the dataset so we skip it next
2352 * time through (as we've already searched that portion of the
2355 gtnd
.skip
= strrchr(pname
, '/') + 1;
2362 * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if
2363 * guid1 is after guid2.
2366 created_before(libzfs_handle_t
*hdl
, avl_tree_t
*avl
,
2367 uint64_t guid1
, uint64_t guid2
)
2370 char *fsname
= NULL
, *snapname
= NULL
;
2371 char buf
[ZFS_MAX_DATASET_NAME_LEN
];
2373 zfs_handle_t
*guid1hdl
, *guid2hdl
;
2374 uint64_t create1
, create2
;
2381 nvfs
= fsavl_find(avl
, guid1
, &snapname
);
2382 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
2383 (void) snprintf(buf
, sizeof (buf
), "%s@%s", fsname
, snapname
);
2384 guid1hdl
= zfs_open(hdl
, buf
, ZFS_TYPE_SNAPSHOT
);
2385 if (guid1hdl
== NULL
)
2388 nvfs
= fsavl_find(avl
, guid2
, &snapname
);
2389 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
2390 (void) snprintf(buf
, sizeof (buf
), "%s@%s", fsname
, snapname
);
2391 guid2hdl
= zfs_open(hdl
, buf
, ZFS_TYPE_SNAPSHOT
);
2392 if (guid2hdl
== NULL
) {
2393 zfs_close(guid1hdl
);
2397 create1
= zfs_prop_get_int(guid1hdl
, ZFS_PROP_CREATETXG
);
2398 create2
= zfs_prop_get_int(guid2hdl
, ZFS_PROP_CREATETXG
);
2400 if (create1
< create2
)
2402 else if (create1
> create2
)
2407 zfs_close(guid1hdl
);
2408 zfs_close(guid2hdl
);
2414 recv_incremental_replication(libzfs_handle_t
*hdl
, const char *tofs
,
2415 recvflags_t
*flags
, nvlist_t
*stream_nv
, avl_tree_t
*stream_avl
,
2418 nvlist_t
*local_nv
, *deleted
= NULL
;
2419 avl_tree_t
*local_avl
;
2420 nvpair_t
*fselem
, *nextfselem
;
2422 char newname
[ZFS_MAX_DATASET_NAME_LEN
];
2425 boolean_t needagain
, progress
, recursive
;
2428 VERIFY(0 == nvlist_lookup_string(stream_nv
, "fromsnap", &fromsnap
));
2430 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
2437 needagain
= progress
= B_FALSE
;
2439 VERIFY(0 == nvlist_alloc(&deleted
, NV_UNIQUE_NAME
, 0));
2441 if ((error
= gather_nvlist(hdl
, tofs
, fromsnap
, NULL
,
2442 recursive
, B_FALSE
, &local_nv
, &local_avl
)) != 0)
2446 * Process deletes and renames
2448 for (fselem
= nvlist_next_nvpair(local_nv
, NULL
);
2449 fselem
; fselem
= nextfselem
) {
2450 nvlist_t
*nvfs
, *snaps
;
2451 nvlist_t
*stream_nvfs
= NULL
;
2452 nvpair_t
*snapelem
, *nextsnapelem
;
2453 uint64_t fromguid
= 0;
2454 uint64_t originguid
= 0;
2455 uint64_t stream_originguid
= 0;
2456 uint64_t parent_fromsnap_guid
, stream_parent_fromsnap_guid
;
2457 char *fsname
, *stream_fsname
;
2459 nextfselem
= nvlist_next_nvpair(local_nv
, fselem
);
2461 VERIFY(0 == nvpair_value_nvlist(fselem
, &nvfs
));
2462 VERIFY(0 == nvlist_lookup_nvlist(nvfs
, "snaps", &snaps
));
2463 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
2464 VERIFY(0 == nvlist_lookup_uint64(nvfs
, "parentfromsnap",
2465 &parent_fromsnap_guid
));
2466 (void) nvlist_lookup_uint64(nvfs
, "origin", &originguid
);
2469 * First find the stream's fs, so we can check for
2470 * a different origin (due to "zfs promote")
2472 for (snapelem
= nvlist_next_nvpair(snaps
, NULL
);
2473 snapelem
; snapelem
= nvlist_next_nvpair(snaps
, snapelem
)) {
2476 VERIFY(0 == nvpair_value_uint64(snapelem
, &thisguid
));
2477 stream_nvfs
= fsavl_find(stream_avl
, thisguid
, NULL
);
2479 if (stream_nvfs
!= NULL
)
2483 /* check for promote */
2484 (void) nvlist_lookup_uint64(stream_nvfs
, "origin",
2485 &stream_originguid
);
2486 if (stream_nvfs
&& originguid
!= stream_originguid
) {
2487 switch (created_before(hdl
, local_avl
,
2488 stream_originguid
, originguid
)) {
2491 zfs_cmd_t zc
= {"\0"};
2492 nvlist_t
*origin_nvfs
;
2493 char *origin_fsname
;
2496 (void) printf("promoting %s\n", fsname
);
2498 origin_nvfs
= fsavl_find(local_avl
, originguid
,
2500 VERIFY(0 == nvlist_lookup_string(origin_nvfs
,
2501 "name", &origin_fsname
));
2502 (void) strlcpy(zc
.zc_value
, origin_fsname
,
2503 sizeof (zc
.zc_value
));
2504 (void) strlcpy(zc
.zc_name
, fsname
,
2505 sizeof (zc
.zc_name
));
2506 error
= zfs_ioctl(hdl
, ZFS_IOC_PROMOTE
, &zc
);
2514 fsavl_destroy(local_avl
);
2515 nvlist_free(local_nv
);
2519 * We had/have the wrong origin, therefore our
2520 * list of snapshots is wrong. Need to handle
2521 * them on the next pass.
2527 for (snapelem
= nvlist_next_nvpair(snaps
, NULL
);
2528 snapelem
; snapelem
= nextsnapelem
) {
2530 char *stream_snapname
;
2531 nvlist_t
*found
, *props
;
2533 nextsnapelem
= nvlist_next_nvpair(snaps
, snapelem
);
2535 VERIFY(0 == nvpair_value_uint64(snapelem
, &thisguid
));
2536 found
= fsavl_find(stream_avl
, thisguid
,
2539 /* check for delete */
2540 if (found
== NULL
) {
2541 char name
[ZFS_MAX_DATASET_NAME_LEN
];
2546 (void) snprintf(name
, sizeof (name
), "%s@%s",
2547 fsname
, nvpair_name(snapelem
));
2549 error
= recv_destroy(hdl
, name
,
2550 strlen(fsname
)+1, newname
, flags
);
2555 sprintf(guidname
, "%llu",
2556 (u_longlong_t
)thisguid
);
2557 nvlist_add_boolean(deleted
, guidname
);
2561 stream_nvfs
= found
;
2563 if (0 == nvlist_lookup_nvlist(stream_nvfs
, "snapprops",
2564 &props
) && 0 == nvlist_lookup_nvlist(props
,
2565 stream_snapname
, &props
)) {
2566 zfs_cmd_t zc
= {"\0"};
2568 zc
.zc_cookie
= B_TRUE
; /* received */
2569 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
),
2570 "%s@%s", fsname
, nvpair_name(snapelem
));
2571 if (zcmd_write_src_nvlist(hdl
, &zc
,
2573 (void) zfs_ioctl(hdl
,
2574 ZFS_IOC_SET_PROP
, &zc
);
2575 zcmd_free_nvlists(&zc
);
2579 /* check for different snapname */
2580 if (strcmp(nvpair_name(snapelem
),
2581 stream_snapname
) != 0) {
2582 char name
[ZFS_MAX_DATASET_NAME_LEN
];
2583 char tryname
[ZFS_MAX_DATASET_NAME_LEN
];
2585 (void) snprintf(name
, sizeof (name
), "%s@%s",
2586 fsname
, nvpair_name(snapelem
));
2587 (void) snprintf(tryname
, sizeof (name
), "%s@%s",
2588 fsname
, stream_snapname
);
2590 error
= recv_rename(hdl
, name
, tryname
,
2591 strlen(fsname
)+1, newname
, flags
);
2598 if (strcmp(stream_snapname
, fromsnap
) == 0)
2599 fromguid
= thisguid
;
2602 /* check for delete */
2603 if (stream_nvfs
== NULL
) {
2607 error
= recv_destroy(hdl
, fsname
, strlen(tofs
)+1,
2613 sprintf(guidname
, "%llu",
2614 (u_longlong_t
) parent_fromsnap_guid
);
2615 nvlist_add_boolean(deleted
, guidname
);
2619 if (fromguid
== 0) {
2620 if (flags
->verbose
) {
2621 (void) printf("local fs %s does not have "
2622 "fromsnap (%s in stream); must have "
2623 "been deleted locally; ignoring\n",
2629 VERIFY(0 == nvlist_lookup_string(stream_nvfs
,
2630 "name", &stream_fsname
));
2631 VERIFY(0 == nvlist_lookup_uint64(stream_nvfs
,
2632 "parentfromsnap", &stream_parent_fromsnap_guid
));
2634 s1
= strrchr(fsname
, '/');
2635 s2
= strrchr(stream_fsname
, '/');
2638 * Check if we're going to rename based on parent guid change
2639 * and the current parent guid was also deleted. If it was then
2640 * rename will fail and is likely unneeded, so avoid this and
2641 * force an early retry to determine the new
2642 * parent_fromsnap_guid.
2644 if (stream_parent_fromsnap_guid
!= 0 &&
2645 parent_fromsnap_guid
!= 0 &&
2646 stream_parent_fromsnap_guid
!= parent_fromsnap_guid
) {
2647 sprintf(guidname
, "%llu",
2648 (u_longlong_t
) parent_fromsnap_guid
);
2649 if (nvlist_exists(deleted
, guidname
)) {
2657 * Check for rename. If the exact receive path is specified, it
2658 * does not count as a rename, but we still need to check the
2659 * datasets beneath it.
2661 if ((stream_parent_fromsnap_guid
!= 0 &&
2662 parent_fromsnap_guid
!= 0 &&
2663 stream_parent_fromsnap_guid
!= parent_fromsnap_guid
) ||
2664 ((flags
->isprefix
|| strcmp(tofs
, fsname
) != 0) &&
2665 (s1
!= NULL
) && (s2
!= NULL
) && strcmp(s1
, s2
) != 0)) {
2667 char tryname
[ZFS_MAX_DATASET_NAME_LEN
];
2669 parent
= fsavl_find(local_avl
,
2670 stream_parent_fromsnap_guid
, NULL
);
2672 * NB: parent might not be found if we used the
2673 * tosnap for stream_parent_fromsnap_guid,
2674 * because the parent is a newly-created fs;
2675 * we'll be able to rename it after we recv the
2678 if (parent
!= NULL
) {
2681 VERIFY(0 == nvlist_lookup_string(parent
, "name",
2683 (void) snprintf(tryname
, sizeof (tryname
),
2684 "%s%s", pname
, strrchr(stream_fsname
, '/'));
2687 if (flags
->verbose
) {
2688 (void) printf("local fs %s new parent "
2689 "not found\n", fsname
);
2695 error
= recv_rename(hdl
, fsname
, tryname
,
2696 strlen(tofs
)+1, newname
, flags
);
2698 if (renamed
!= NULL
&& newname
[0] != '\0') {
2699 VERIFY(0 == nvlist_add_boolean(renamed
,
2711 fsavl_destroy(local_avl
);
2712 nvlist_free(local_nv
);
2713 nvlist_free(deleted
);
2715 if (needagain
&& progress
) {
2716 /* do another pass to fix up temporary names */
2718 (void) printf("another pass:\n");
2726 zfs_receive_package(libzfs_handle_t
*hdl
, int fd
, const char *destname
,
2727 recvflags_t
*flags
, dmu_replay_record_t
*drr
, zio_cksum_t
*zc
,
2728 char **top_zfs
, int cleanup_fd
, uint64_t *action_handlep
)
2730 nvlist_t
*stream_nv
= NULL
;
2731 avl_tree_t
*stream_avl
= NULL
;
2732 char *fromsnap
= NULL
;
2733 char *sendsnap
= NULL
;
2735 char tofs
[ZFS_MAX_DATASET_NAME_LEN
];
2736 char sendfs
[ZFS_MAX_DATASET_NAME_LEN
];
2738 dmu_replay_record_t drre
;
2740 boolean_t anyerr
= B_FALSE
;
2741 boolean_t softerr
= B_FALSE
;
2742 boolean_t recursive
;
2744 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2747 assert(drr
->drr_type
== DRR_BEGIN
);
2748 assert(drr
->drr_u
.drr_begin
.drr_magic
== DMU_BACKUP_MAGIC
);
2749 assert(DMU_GET_STREAM_HDRTYPE(drr
->drr_u
.drr_begin
.drr_versioninfo
) ==
2750 DMU_COMPOUNDSTREAM
);
2753 * Read in the nvlist from the stream.
2755 if (drr
->drr_payloadlen
!= 0) {
2756 error
= recv_read_nvlist(hdl
, fd
, drr
->drr_payloadlen
,
2757 &stream_nv
, flags
->byteswap
, zc
);
2759 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2764 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
2767 if (recursive
&& strchr(destname
, '@')) {
2768 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2769 "cannot specify snapshot name for multi-snapshot stream"));
2770 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2775 * Read in the end record and verify checksum.
2777 if (0 != (error
= recv_read(hdl
, fd
, &drre
, sizeof (drre
),
2778 flags
->byteswap
, NULL
)))
2780 if (flags
->byteswap
) {
2781 drre
.drr_type
= BSWAP_32(drre
.drr_type
);
2782 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[0] =
2783 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[0]);
2784 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[1] =
2785 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[1]);
2786 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[2] =
2787 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[2]);
2788 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[3] =
2789 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[3]);
2791 if (drre
.drr_type
!= DRR_END
) {
2792 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2795 if (!ZIO_CHECKSUM_EQUAL(drre
.drr_u
.drr_end
.drr_checksum
, *zc
)) {
2796 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2797 "incorrect header checksum"));
2798 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2802 (void) nvlist_lookup_string(stream_nv
, "fromsnap", &fromsnap
);
2804 if (drr
->drr_payloadlen
!= 0) {
2805 nvlist_t
*stream_fss
;
2807 VERIFY(0 == nvlist_lookup_nvlist(stream_nv
, "fss",
2809 if ((stream_avl
= fsavl_create(stream_fss
)) == NULL
) {
2810 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2811 "couldn't allocate avl tree"));
2812 error
= zfs_error(hdl
, EZFS_NOMEM
, errbuf
);
2816 if (fromsnap
!= NULL
) {
2817 nvlist_t
*renamed
= NULL
;
2818 nvpair_t
*pair
= NULL
;
2820 (void) strlcpy(tofs
, destname
, sizeof (tofs
));
2821 if (flags
->isprefix
) {
2822 struct drr_begin
*drrb
= &drr
->drr_u
.drr_begin
;
2825 if (flags
->istail
) {
2826 cp
= strrchr(drrb
->drr_toname
, '/');
2828 (void) strlcat(tofs
, "/",
2832 i
= (cp
- drrb
->drr_toname
);
2835 i
= strcspn(drrb
->drr_toname
, "/@");
2837 /* zfs_receive_one() will create_parents() */
2838 (void) strlcat(tofs
, &drrb
->drr_toname
[i
],
2840 *strchr(tofs
, '@') = '\0';
2843 if (recursive
&& !flags
->dryrun
&& !flags
->nomount
) {
2844 VERIFY(0 == nvlist_alloc(&renamed
,
2845 NV_UNIQUE_NAME
, 0));
2848 softerr
= recv_incremental_replication(hdl
, tofs
, flags
,
2849 stream_nv
, stream_avl
, renamed
);
2851 /* Unmount renamed filesystems before receiving. */
2852 while ((pair
= nvlist_next_nvpair(renamed
,
2855 prop_changelist_t
*clp
= NULL
;
2857 zhp
= zfs_open(hdl
, nvpair_name(pair
),
2858 ZFS_TYPE_FILESYSTEM
);
2860 clp
= changelist_gather(zhp
,
2861 ZFS_PROP_MOUNTPOINT
, 0, 0);
2865 changelist_prefix(clp
);
2866 changelist_free(clp
);
2871 nvlist_free(renamed
);
2876 * Get the fs specified by the first path in the stream (the top level
2877 * specified by 'zfs send') and pass it to each invocation of
2878 * zfs_receive_one().
2880 (void) strlcpy(sendfs
, drr
->drr_u
.drr_begin
.drr_toname
,
2882 if ((cp
= strchr(sendfs
, '@')) != NULL
) {
2885 * Find the "sendsnap", the final snapshot in a replication
2886 * stream. zfs_receive_one() handles certain errors
2887 * differently, depending on if the contained stream is the
2890 sendsnap
= (cp
+ 1);
2893 /* Finally, receive each contained stream */
2896 * we should figure out if it has a recoverable
2897 * error, in which case do a recv_skip() and drive on.
2898 * Note, if we fail due to already having this guid,
2899 * zfs_receive_one() will take care of it (ie,
2900 * recv_skip() and return 0).
2902 error
= zfs_receive_impl(hdl
, destname
, NULL
, flags
, fd
,
2903 sendfs
, stream_nv
, stream_avl
, top_zfs
, cleanup_fd
,
2904 action_handlep
, sendsnap
);
2905 if (error
== ENODATA
) {
2910 } while (error
== 0);
2912 if (drr
->drr_payloadlen
!= 0 && fromsnap
!= NULL
) {
2914 * Now that we have the fs's they sent us, try the
2917 softerr
= recv_incremental_replication(hdl
, tofs
, flags
,
2918 stream_nv
, stream_avl
, NULL
);
2922 fsavl_destroy(stream_avl
);
2923 nvlist_free(stream_nv
);
2932 trunc_prop_errs(int truncated
)
2934 ASSERT(truncated
!= 0);
2937 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
2938 "1 more property could not be set\n"));
2940 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
2941 "%d more properties could not be set\n"), truncated
);
2945 recv_skip(libzfs_handle_t
*hdl
, int fd
, boolean_t byteswap
)
2947 dmu_replay_record_t
*drr
;
2948 void *buf
= zfs_alloc(hdl
, SPA_MAXBLOCKSIZE
);
2951 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2952 "cannot receive:"));
2954 /* XXX would be great to use lseek if possible... */
2957 while (recv_read(hdl
, fd
, drr
, sizeof (dmu_replay_record_t
),
2958 byteswap
, NULL
) == 0) {
2960 drr
->drr_type
= BSWAP_32(drr
->drr_type
);
2962 switch (drr
->drr_type
) {
2964 if (drr
->drr_payloadlen
!= 0) {
2965 (void) recv_read(hdl
, fd
, buf
,
2966 drr
->drr_payloadlen
, B_FALSE
, NULL
);
2976 drr
->drr_u
.drr_object
.drr_bonuslen
=
2977 BSWAP_32(drr
->drr_u
.drr_object
.
2980 (void) recv_read(hdl
, fd
, buf
,
2981 P2ROUNDUP(drr
->drr_u
.drr_object
.drr_bonuslen
, 8),
2987 drr
->drr_u
.drr_write
.drr_logical_size
=
2989 drr
->drr_u
.drr_write
.drr_logical_size
);
2990 drr
->drr_u
.drr_write
.drr_compressed_size
=
2992 drr
->drr_u
.drr_write
.drr_compressed_size
);
2994 uint64_t payload_size
=
2995 DRR_WRITE_PAYLOAD_SIZE(&drr
->drr_u
.drr_write
);
2996 (void) recv_read(hdl
, fd
, buf
,
2997 payload_size
, B_FALSE
, NULL
);
3001 drr
->drr_u
.drr_spill
.drr_length
=
3002 BSWAP_64(drr
->drr_u
.drr_spill
.drr_length
);
3004 (void) recv_read(hdl
, fd
, buf
,
3005 drr
->drr_u
.drr_spill
.drr_length
, B_FALSE
, NULL
);
3007 case DRR_WRITE_EMBEDDED
:
3009 drr
->drr_u
.drr_write_embedded
.drr_psize
=
3010 BSWAP_32(drr
->drr_u
.drr_write_embedded
.
3013 (void) recv_read(hdl
, fd
, buf
,
3014 P2ROUNDUP(drr
->drr_u
.drr_write_embedded
.drr_psize
,
3017 case DRR_WRITE_BYREF
:
3018 case DRR_FREEOBJECTS
:
3023 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3024 "invalid record type"));
3025 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3034 recv_ecksum_set_aux(libzfs_handle_t
*hdl
, const char *target_snap
,
3035 boolean_t resumable
)
3037 char target_fs
[ZFS_MAX_DATASET_NAME_LEN
];
3039 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3040 "checksum mismatch or incomplete stream"));
3044 (void) strlcpy(target_fs
, target_snap
, sizeof (target_fs
));
3045 *strchr(target_fs
, '@') = '\0';
3046 zfs_handle_t
*zhp
= zfs_open(hdl
, target_fs
,
3047 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3051 char token_buf
[ZFS_MAXPROPLEN
];
3052 int error
= zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
3053 token_buf
, sizeof (token_buf
),
3054 NULL
, NULL
, 0, B_TRUE
);
3056 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3057 "checksum mismatch or incomplete stream.\n"
3058 "Partially received snapshot is saved.\n"
3059 "A resuming stream can be generated on the sending "
3060 "system by running:\n"
3068 * Restores a backup of tosnap from the file descriptor specified by infd.
3071 zfs_receive_one(libzfs_handle_t
*hdl
, int infd
, const char *tosnap
,
3072 const char *originsnap
, recvflags_t
*flags
, dmu_replay_record_t
*drr
,
3073 dmu_replay_record_t
*drr_noswap
, const char *sendfs
, nvlist_t
*stream_nv
,
3074 avl_tree_t
*stream_avl
, char **top_zfs
, int cleanup_fd
,
3075 uint64_t *action_handlep
, const char *finalsnap
)
3078 int ioctl_err
, ioctl_errno
, err
;
3080 struct drr_begin
*drrb
= &drr
->drr_u
.drr_begin
;
3082 const char *chopprefix
;
3083 boolean_t newfs
= B_FALSE
;
3084 boolean_t stream_wantsnewfs
;
3085 boolean_t newprops
= B_FALSE
;
3086 uint64_t read_bytes
= 0;
3087 uint64_t errflags
= 0;
3088 uint64_t parent_snapguid
= 0;
3089 prop_changelist_t
*clp
= NULL
;
3090 nvlist_t
*snapprops_nvlist
= NULL
;
3091 zprop_errflags_t prop_errflags
;
3092 nvlist_t
*prop_errors
= NULL
;
3093 boolean_t recursive
;
3094 char *snapname
= NULL
;
3095 char destsnap
[MAXPATHLEN
* 2];
3096 char origin
[MAXNAMELEN
];
3097 char name
[MAXPATHLEN
];
3098 nvlist_t
*props
= NULL
;
3100 begin_time
= time(NULL
);
3101 bzero(origin
, MAXNAMELEN
);
3103 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3106 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
3109 if (stream_avl
!= NULL
) {
3110 nvlist_t
*fs
= fsavl_find(stream_avl
, drrb
->drr_toguid
,
3113 (void) nvlist_lookup_uint64(fs
, "parentfromsnap",
3115 err
= nvlist_lookup_nvlist(fs
, "props", &props
);
3117 VERIFY(0 == nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0));
3121 if (flags
->canmountoff
) {
3122 VERIFY(0 == nvlist_add_uint64(props
,
3123 zfs_prop_to_name(ZFS_PROP_CANMOUNT
), 0));
3130 * Determine how much of the snapshot name stored in the stream
3131 * we are going to tack on to the name they specified on the
3132 * command line, and how much we are going to chop off.
3134 * If they specified a snapshot, chop the entire name stored in
3137 if (flags
->istail
) {
3139 * A filesystem was specified with -e. We want to tack on only
3140 * the tail of the sent snapshot path.
3142 if (strchr(tosnap
, '@')) {
3143 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
3144 "argument - snapshot not allowed with -e"));
3145 err
= zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
3149 chopprefix
= strrchr(sendfs
, '/');
3151 if (chopprefix
== NULL
) {
3153 * The tail is the poolname, so we need to
3154 * prepend a path separator.
3156 int len
= strlen(drrb
->drr_toname
);
3157 cp
= malloc(len
+ 2);
3159 (void) strcpy(&cp
[1], drrb
->drr_toname
);
3162 chopprefix
= drrb
->drr_toname
+ (chopprefix
- sendfs
);
3164 } else if (flags
->isprefix
) {
3166 * A filesystem was specified with -d. We want to tack on
3167 * everything but the first element of the sent snapshot path
3168 * (all but the pool name).
3170 if (strchr(tosnap
, '@')) {
3171 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
3172 "argument - snapshot not allowed with -d"));
3173 err
= zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
3177 chopprefix
= strchr(drrb
->drr_toname
, '/');
3178 if (chopprefix
== NULL
)
3179 chopprefix
= strchr(drrb
->drr_toname
, '@');
3180 } else if (strchr(tosnap
, '@') == NULL
) {
3182 * If a filesystem was specified without -d or -e, we want to
3183 * tack on everything after the fs specified by 'zfs send'.
3185 chopprefix
= drrb
->drr_toname
+ strlen(sendfs
);
3187 /* A snapshot was specified as an exact path (no -d or -e). */
3189 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3190 "cannot specify snapshot name for multi-snapshot "
3192 err
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
3195 chopprefix
= drrb
->drr_toname
+ strlen(drrb
->drr_toname
);
3198 ASSERT(strstr(drrb
->drr_toname
, sendfs
) == drrb
->drr_toname
);
3199 ASSERT(chopprefix
> drrb
->drr_toname
);
3200 ASSERT(chopprefix
<= drrb
->drr_toname
+ strlen(drrb
->drr_toname
));
3201 ASSERT(chopprefix
[0] == '/' || chopprefix
[0] == '@' ||
3202 chopprefix
[0] == '\0');
3205 * Determine name of destination snapshot.
3207 (void) strcpy(destsnap
, tosnap
);
3208 (void) strlcat(destsnap
, chopprefix
, sizeof (destsnap
));
3210 if (!zfs_name_valid(destsnap
, ZFS_TYPE_SNAPSHOT
)) {
3211 err
= zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
3216 * Determine the name of the origin snapshot.
3218 if (drrb
->drr_flags
& DRR_FLAG_CLONE
) {
3219 if (guid_to_name(hdl
, destsnap
,
3220 drrb
->drr_fromguid
, B_FALSE
, origin
) != 0) {
3221 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3222 "local origin for clone %s does not exist"),
3224 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
3228 (void) printf("found clone origin %s\n", origin
);
3229 } else if (originsnap
) {
3230 (void) strncpy(origin
, originsnap
, sizeof (origin
));
3232 (void) printf("using provided clone origin %s\n",
3236 boolean_t resuming
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
) &
3237 DMU_BACKUP_FEATURE_RESUMING
;
3238 stream_wantsnewfs
= (drrb
->drr_fromguid
== 0 ||
3239 (drrb
->drr_flags
& DRR_FLAG_CLONE
) || originsnap
) && !resuming
;
3241 if (stream_wantsnewfs
) {
3243 * if the parent fs does not exist, look for it based on
3244 * the parent snap GUID
3246 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3247 "cannot receive new filesystem stream"));
3249 (void) strcpy(name
, destsnap
);
3250 cp
= strrchr(name
, '/');
3254 !zfs_dataset_exists(hdl
, name
, ZFS_TYPE_DATASET
)) {
3255 char suffix
[ZFS_MAX_DATASET_NAME_LEN
];
3256 (void) strcpy(suffix
, strrchr(destsnap
, '/'));
3257 if (guid_to_name(hdl
, name
, parent_snapguid
,
3258 B_FALSE
, destsnap
) == 0) {
3259 *strchr(destsnap
, '@') = '\0';
3260 (void) strcat(destsnap
, suffix
);
3265 * if the fs does not exist, look for it based on the
3268 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3269 "cannot receive incremental stream"));
3271 (void) strcpy(name
, destsnap
);
3272 *strchr(name
, '@') = '\0';
3275 * If the exact receive path was specified and this is the
3276 * topmost path in the stream, then if the fs does not exist we
3277 * should look no further.
3279 if ((flags
->isprefix
|| (*(chopprefix
= drrb
->drr_toname
+
3280 strlen(sendfs
)) != '\0' && *chopprefix
!= '@')) &&
3281 !zfs_dataset_exists(hdl
, name
, ZFS_TYPE_DATASET
)) {
3282 char snap
[ZFS_MAX_DATASET_NAME_LEN
];
3283 (void) strcpy(snap
, strchr(destsnap
, '@'));
3284 if (guid_to_name(hdl
, name
, drrb
->drr_fromguid
,
3285 B_FALSE
, destsnap
) == 0) {
3286 *strchr(destsnap
, '@') = '\0';
3287 (void) strcat(destsnap
, snap
);
3292 (void) strcpy(name
, destsnap
);
3293 *strchr(name
, '@') = '\0';
3295 if (zfs_dataset_exists(hdl
, name
, ZFS_TYPE_DATASET
)) {
3296 zfs_cmd_t zc
= {"\0"};
3299 (void) strcpy(zc
.zc_name
, name
);
3302 * Destination fs exists. It must be one of these cases:
3303 * - an incremental send stream
3304 * - the stream specifies a new fs (full stream or clone)
3305 * and they want us to blow away the existing fs (and
3306 * have therefore specified -F and removed any snapshots)
3307 * - we are resuming a failed receive.
3309 if (stream_wantsnewfs
) {
3310 if (!flags
->force
) {
3311 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3312 "destination '%s' exists\n"
3313 "must specify -F to overwrite it"), name
);
3314 err
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3317 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_SNAPSHOT_LIST_NEXT
,
3319 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3320 "destination has snapshots (eg. %s)\n"
3321 "must destroy them to overwrite it"),
3323 err
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3328 if ((zhp
= zfs_open(hdl
, name
,
3329 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
)) == NULL
) {
3334 if (stream_wantsnewfs
&&
3335 zhp
->zfs_dmustats
.dds_origin
[0]) {
3337 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3338 "destination '%s' is a clone\n"
3339 "must destroy it to overwrite it"), name
);
3340 err
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3344 if (!flags
->dryrun
&& zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
&&
3345 stream_wantsnewfs
) {
3346 /* We can't do online recv in this case */
3347 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0, 0);
3353 if (changelist_prefix(clp
) != 0) {
3354 changelist_free(clp
);
3362 * If we are resuming a newfs, set newfs here so that we will
3363 * mount it if the recv succeeds this time. We can tell
3364 * that it was a newfs on the first recv because the fs
3365 * itself will be inconsistent (if the fs existed when we
3366 * did the first recv, we would have received it into
3369 if (resuming
&& zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
))
3375 * Destination filesystem does not exist. Therefore we better
3376 * be creating a new filesystem (either from a full backup, or
3377 * a clone). It would therefore be invalid if the user
3378 * specified only the pool name (i.e. if the destination name
3379 * contained no slash character).
3381 cp
= strrchr(name
, '/');
3383 if (!stream_wantsnewfs
|| cp
== NULL
) {
3384 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3385 "destination '%s' does not exist"), name
);
3386 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
3391 * Trim off the final dataset component so we perform the
3392 * recvbackup ioctl to the filesystems's parent.
3396 if (flags
->isprefix
&& !flags
->istail
&& !flags
->dryrun
&&
3397 create_parents(hdl
, destsnap
, strlen(tosnap
)) != 0) {
3398 err
= zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
3405 if (flags
->verbose
) {
3406 (void) printf("%s %s stream of %s into %s\n",
3407 flags
->dryrun
? "would receive" : "receiving",
3408 drrb
->drr_fromguid
? "incremental" : "full",
3409 drrb
->drr_toname
, destsnap
);
3410 (void) fflush(stdout
);
3413 if (flags
->dryrun
) {
3414 err
= recv_skip(hdl
, infd
, flags
->byteswap
);
3418 err
= ioctl_err
= lzc_receive_one(destsnap
, props
, origin
,
3419 flags
->force
, flags
->resumable
, infd
, drr_noswap
, cleanup_fd
,
3420 &read_bytes
, &errflags
, action_handlep
, &prop_errors
);
3421 ioctl_errno
= ioctl_err
;
3422 prop_errflags
= errflags
;
3425 nvpair_t
*prop_err
= NULL
;
3427 while ((prop_err
= nvlist_next_nvpair(prop_errors
,
3428 prop_err
)) != NULL
) {
3433 prop
= zfs_name_to_prop(nvpair_name(prop_err
));
3434 (void) nvpair_value_int32(prop_err
, &intval
);
3435 if (strcmp(nvpair_name(prop_err
),
3436 ZPROP_N_MORE_ERRORS
) == 0) {
3437 trunc_prop_errs(intval
);
3439 } else if (snapname
== NULL
|| finalsnap
== NULL
||
3440 strcmp(finalsnap
, snapname
) == 0 ||
3441 strcmp(nvpair_name(prop_err
),
3442 zfs_prop_to_name(ZFS_PROP_REFQUOTA
)) != 0) {
3444 * Skip the special case of, for example,
3445 * "refquota", errors on intermediate
3446 * snapshots leading up to a final one.
3447 * That's why we have all of the checks above.
3449 * See zfs_ioctl.c's extract_delay_props() for
3450 * a list of props which can fail on
3451 * intermediate snapshots, but shouldn't
3452 * affect the overall receive.
3454 (void) snprintf(tbuf
, sizeof (tbuf
),
3455 dgettext(TEXT_DOMAIN
,
3456 "cannot receive %s property on %s"),
3457 nvpair_name(prop_err
), name
);
3458 zfs_setprop_error(hdl
, prop
, intval
, tbuf
);
3463 if (err
== 0 && snapprops_nvlist
) {
3464 zfs_cmd_t zc
= {"\0"};
3466 (void) strcpy(zc
.zc_name
, destsnap
);
3467 zc
.zc_cookie
= B_TRUE
; /* received */
3468 if (zcmd_write_src_nvlist(hdl
, &zc
, snapprops_nvlist
) == 0) {
3469 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
3470 zcmd_free_nvlists(&zc
);
3474 if (err
&& (ioctl_errno
== ENOENT
|| ioctl_errno
== EEXIST
)) {
3476 * It may be that this snapshot already exists,
3477 * in which case we want to consume & ignore it
3478 * rather than failing.
3480 avl_tree_t
*local_avl
;
3481 nvlist_t
*local_nv
, *fs
;
3482 cp
= strchr(destsnap
, '@');
3485 * XXX Do this faster by just iterating over snaps in
3486 * this fs. Also if zc_value does not exist, we will
3487 * get a strange "does not exist" error message.
3490 if (gather_nvlist(hdl
, destsnap
, NULL
, NULL
, B_FALSE
,
3491 B_FALSE
, &local_nv
, &local_avl
) == 0) {
3493 fs
= fsavl_find(local_avl
, drrb
->drr_toguid
, NULL
);
3494 fsavl_destroy(local_avl
);
3495 nvlist_free(local_nv
);
3498 if (flags
->verbose
) {
3499 (void) printf("snap %s already exists; "
3500 "ignoring\n", destsnap
);
3502 err
= ioctl_err
= recv_skip(hdl
, infd
,
3509 if (ioctl_err
!= 0) {
3510 switch (ioctl_errno
) {
3512 cp
= strchr(destsnap
, '@');
3514 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3515 "most recent snapshot of %s does not\n"
3516 "match incremental source"), destsnap
);
3517 (void) zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
3521 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3522 "destination %s has been modified\n"
3523 "since most recent snapshot"), name
);
3524 (void) zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
3527 cp
= strchr(destsnap
, '@');
3529 /* it's the containing fs that exists */
3532 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3533 "destination already exists"));
3534 (void) zfs_error_fmt(hdl
, EZFS_EXISTS
,
3535 dgettext(TEXT_DOMAIN
, "cannot restore to %s"),
3540 if (flags
->resumable
)
3541 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3542 "kernel modules must be upgraded to "
3543 "receive this stream."));
3544 (void) zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
3547 recv_ecksum_set_aux(hdl
, destsnap
, flags
->resumable
);
3548 (void) zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
3551 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3552 "pool must be upgraded to receive this stream."));
3553 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
3556 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3557 "destination %s space quota exceeded"), name
);
3558 (void) zfs_error(hdl
, EZFS_NOSPC
, errbuf
);
3561 (void) zfs_standard_error(hdl
, ioctl_errno
, errbuf
);
3566 * Mount the target filesystem (if created). Also mount any
3567 * children of the target filesystem if we did a replication
3568 * receive (indicated by stream_avl being non-NULL).
3570 cp
= strchr(destsnap
, '@');
3571 if (cp
&& (ioctl_err
== 0 || !newfs
)) {
3575 h
= zfs_open(hdl
, destsnap
,
3576 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3578 if (h
->zfs_type
== ZFS_TYPE_VOLUME
) {
3580 } else if (newfs
|| stream_avl
) {
3582 * Track the first/top of hierarchy fs,
3583 * for mounting and sharing later.
3585 if (top_zfs
&& *top_zfs
== NULL
)
3586 *top_zfs
= zfs_strdup(hdl
, destsnap
);
3594 err
|= changelist_postfix(clp
);
3595 changelist_free(clp
);
3598 if (prop_errflags
& ZPROP_ERR_NOCLEAR
) {
3599 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Warning: "
3600 "failed to clear unreceived properties on %s"), name
);
3601 (void) fprintf(stderr
, "\n");
3603 if (prop_errflags
& ZPROP_ERR_NORESTORE
) {
3604 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Warning: "
3605 "failed to restore original properties on %s"), name
);
3606 (void) fprintf(stderr
, "\n");
3609 if (err
|| ioctl_err
) {
3614 if (flags
->verbose
) {
3617 uint64_t bytes
= read_bytes
;
3618 time_t delta
= time(NULL
) - begin_time
;
3621 zfs_nicenum(bytes
, buf1
, sizeof (buf1
));
3622 zfs_nicenum(bytes
/delta
, buf2
, sizeof (buf1
));
3624 (void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
3630 if (prop_errors
!= NULL
)
3631 nvlist_free(prop_errors
);
3640 zfs_receive_impl(libzfs_handle_t
*hdl
, const char *tosnap
,
3641 const char *originsnap
, recvflags_t
*flags
, int infd
, const char *sendfs
,
3642 nvlist_t
*stream_nv
, avl_tree_t
*stream_avl
, char **top_zfs
, int cleanup_fd
,
3643 uint64_t *action_handlep
, const char *finalsnap
)
3646 dmu_replay_record_t drr
, drr_noswap
;
3647 struct drr_begin
*drrb
= &drr
.drr_u
.drr_begin
;
3649 zio_cksum_t zcksum
= { { 0 } };
3650 uint64_t featureflags
;
3653 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3656 if (flags
->isprefix
&&
3657 !zfs_dataset_exists(hdl
, tosnap
, ZFS_TYPE_DATASET
)) {
3658 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "specified fs "
3659 "(%s) does not exist"), tosnap
);
3660 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3663 !zfs_dataset_exists(hdl
, originsnap
, ZFS_TYPE_DATASET
)) {
3664 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "specified origin fs "
3665 "(%s) does not exist"), originsnap
);
3666 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3669 /* read in the BEGIN record */
3670 if (0 != (err
= recv_read(hdl
, infd
, &drr
, sizeof (drr
), B_FALSE
,
3674 if (drr
.drr_type
== DRR_END
|| drr
.drr_type
== BSWAP_32(DRR_END
)) {
3675 /* It's the double end record at the end of a package */
3679 /* the kernel needs the non-byteswapped begin record */
3682 flags
->byteswap
= B_FALSE
;
3683 if (drrb
->drr_magic
== BSWAP_64(DMU_BACKUP_MAGIC
)) {
3685 * We computed the checksum in the wrong byteorder in
3686 * recv_read() above; do it again correctly.
3688 bzero(&zcksum
, sizeof (zio_cksum_t
));
3689 fletcher_4_incremental_byteswap(&drr
, sizeof (drr
), &zcksum
);
3690 flags
->byteswap
= B_TRUE
;
3692 drr
.drr_type
= BSWAP_32(drr
.drr_type
);
3693 drr
.drr_payloadlen
= BSWAP_32(drr
.drr_payloadlen
);
3694 drrb
->drr_magic
= BSWAP_64(drrb
->drr_magic
);
3695 drrb
->drr_versioninfo
= BSWAP_64(drrb
->drr_versioninfo
);
3696 drrb
->drr_creation_time
= BSWAP_64(drrb
->drr_creation_time
);
3697 drrb
->drr_type
= BSWAP_32(drrb
->drr_type
);
3698 drrb
->drr_flags
= BSWAP_32(drrb
->drr_flags
);
3699 drrb
->drr_toguid
= BSWAP_64(drrb
->drr_toguid
);
3700 drrb
->drr_fromguid
= BSWAP_64(drrb
->drr_fromguid
);
3703 if (drrb
->drr_magic
!= DMU_BACKUP_MAGIC
|| drr
.drr_type
!= DRR_BEGIN
) {
3704 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
3705 "stream (bad magic number)"));
3706 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3709 featureflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
3710 hdrtype
= DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
);
3712 if (!DMU_STREAM_SUPPORTED(featureflags
) ||
3713 (hdrtype
!= DMU_SUBSTREAM
&& hdrtype
!= DMU_COMPOUNDSTREAM
)) {
3714 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3715 "stream has unsupported feature, feature flags = %lx"),
3717 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3720 if (strchr(drrb
->drr_toname
, '@') == NULL
) {
3721 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
3722 "stream (bad snapshot name)"));
3723 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3726 if (DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) == DMU_SUBSTREAM
) {
3727 char nonpackage_sendfs
[ZFS_MAX_DATASET_NAME_LEN
];
3728 if (sendfs
== NULL
) {
3730 * We were not called from zfs_receive_package(). Get
3731 * the fs specified by 'zfs send'.
3734 (void) strlcpy(nonpackage_sendfs
,
3735 drr
.drr_u
.drr_begin
.drr_toname
,
3736 sizeof (nonpackage_sendfs
));
3737 if ((cp
= strchr(nonpackage_sendfs
, '@')) != NULL
)
3739 sendfs
= nonpackage_sendfs
;
3740 VERIFY(finalsnap
== NULL
);
3742 return (zfs_receive_one(hdl
, infd
, tosnap
, originsnap
, flags
,
3743 &drr
, &drr_noswap
, sendfs
, stream_nv
, stream_avl
, top_zfs
,
3744 cleanup_fd
, action_handlep
, finalsnap
));
3746 assert(DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) ==
3747 DMU_COMPOUNDSTREAM
);
3748 return (zfs_receive_package(hdl
, infd
, tosnap
, flags
, &drr
,
3749 &zcksum
, top_zfs
, cleanup_fd
, action_handlep
));
3754 * Restores a backup of tosnap from the file descriptor specified by infd.
3755 * Return 0 on total success, -2 if some things couldn't be
3756 * destroyed/renamed/promoted, -1 if some things couldn't be received.
3757 * (-1 will override -2, if -1 and the resumable flag was specified the
3758 * transfer can be resumed if the sending side supports it).
3761 zfs_receive(libzfs_handle_t
*hdl
, const char *tosnap
, nvlist_t
*props
,
3762 recvflags_t
*flags
, int infd
, avl_tree_t
*stream_avl
)
3764 char *top_zfs
= NULL
;
3767 uint64_t action_handle
= 0;
3769 char *originsnap
= NULL
;
3772 * The only way fstat can fail is if we do not have a valid file
3775 if (fstat(infd
, &sb
) == -1) {
3781 #ifndef F_SETPIPE_SZ
3782 #define F_SETPIPE_SZ (F_SETLEASE + 7)
3783 #endif /* F_SETPIPE_SZ */
3785 #ifndef F_GETPIPE_SZ
3786 #define F_GETPIPE_SZ (F_GETLEASE + 7)
3787 #endif /* F_GETPIPE_SZ */
3790 * It is not uncommon for gigabytes to be processed in zfs receive.
3791 * Speculatively increase the buffer size via Linux-specific fcntl()
3794 if (S_ISFIFO(sb
.st_mode
)) {
3795 FILE *procf
= fopen("/proc/sys/fs/pipe-max-size", "r");
3797 if (procf
!= NULL
) {
3798 unsigned long max_psize
;
3800 if (fscanf(procf
, "%lu", &max_psize
) > 0) {
3801 cur_psize
= fcntl(infd
, F_GETPIPE_SZ
);
3802 if (cur_psize
> 0 &&
3803 max_psize
> (unsigned long) cur_psize
)
3804 (void) fcntl(infd
, F_SETPIPE_SZ
,
3810 #endif /* __linux__ */
3813 err
= nvlist_lookup_string(props
, "origin", &originsnap
);
3814 if (err
&& err
!= ENOENT
)
3818 cleanup_fd
= open(ZFS_DEV
, O_RDWR
);
3819 VERIFY(cleanup_fd
>= 0);
3821 err
= zfs_receive_impl(hdl
, tosnap
, originsnap
, flags
, infd
, NULL
, NULL
,
3822 stream_avl
, &top_zfs
, cleanup_fd
, &action_handle
, NULL
);
3824 VERIFY(0 == close(cleanup_fd
));
3826 if (err
== 0 && !flags
->nomount
&& top_zfs
) {
3827 zfs_handle_t
*zhp
= NULL
;
3828 prop_changelist_t
*clp
= NULL
;
3830 zhp
= zfs_open(hdl
, top_zfs
, ZFS_TYPE_FILESYSTEM
);
3832 clp
= changelist_gather(zhp
, ZFS_PROP_MOUNTPOINT
,
3833 CL_GATHER_MOUNT_ALWAYS
, 0);
3836 /* mount and share received datasets */
3837 err
= changelist_postfix(clp
);
3838 changelist_free(clp
);
3841 if (zhp
== NULL
|| clp
== NULL
|| err
)